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 boolean_t is_streams) 4839 { 4840 conn_t *connp = tcp->tcp_connp; 4841 conn_t *econnp; 4842 squeue_t *new_sqp; 4843 mblk_t *first_mp = *mpp; 4844 mblk_t *mp = *mpp; 4845 boolean_t mctl_present = B_FALSE; 4846 uint_t ipvers; 4847 4848 econnp = tcp_get_conn(sqp, tcp->tcp_tcps, is_streams); 4849 if (econnp == NULL) { 4850 freemsg(first_mp); 4851 return (NULL); 4852 } 4853 if (DB_TYPE(mp) == M_CTL) { 4854 if (mp->b_cont == NULL || 4855 mp->b_cont->b_datap->db_type != M_DATA) { 4856 freemsg(first_mp); 4857 return (NULL); 4858 } 4859 mp = mp->b_cont; 4860 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4861 freemsg(first_mp); 4862 return (NULL); 4863 } 4864 4865 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4866 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4867 mctl_present = B_TRUE; 4868 } else { 4869 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4870 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4871 } 4872 4873 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 4874 DB_CKSUMSTART(mp) = 0; 4875 4876 ASSERT(OK_32PTR(mp->b_rptr)); 4877 ipvers = IPH_HDR_VERSION(mp->b_rptr); 4878 if (ipvers == IPV4_VERSION) { 4879 uint16_t *up; 4880 uint32_t ports; 4881 ipha_t *ipha; 4882 4883 ipha = (ipha_t *)mp->b_rptr; 4884 up = (uint16_t *)((uchar_t *)ipha + 4885 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 4886 ports = *(uint32_t *)up; 4887 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 4888 ipha->ipha_dst, ipha->ipha_src, ports); 4889 } else { 4890 uint16_t *up; 4891 uint32_t ports; 4892 uint16_t ip_hdr_len; 4893 uint8_t *nexthdrp; 4894 ip6_t *ip6h; 4895 tcph_t *tcph; 4896 4897 ip6h = (ip6_t *)mp->b_rptr; 4898 if (ip6h->ip6_nxt == IPPROTO_TCP) { 4899 ip_hdr_len = IPV6_HDR_LEN; 4900 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 4901 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 4902 CONN_DEC_REF(econnp); 4903 freemsg(first_mp); 4904 return (NULL); 4905 } 4906 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 4907 up = (uint16_t *)tcph->th_lport; 4908 ports = *(uint32_t *)up; 4909 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 4910 ip6h->ip6_dst, ip6h->ip6_src, ports); 4911 } 4912 4913 /* 4914 * The caller already ensured that there is a sqp present. 4915 */ 4916 econnp->conn_sqp = new_sqp; 4917 econnp->conn_initial_sqp = new_sqp; 4918 4919 if (connp->conn_policy != NULL) { 4920 ipsec_in_t *ii; 4921 ii = (ipsec_in_t *)(first_mp->b_rptr); 4922 ASSERT(ii->ipsec_in_policy == NULL); 4923 IPPH_REFHOLD(connp->conn_policy); 4924 ii->ipsec_in_policy = connp->conn_policy; 4925 4926 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 4927 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 4928 CONN_DEC_REF(econnp); 4929 freemsg(first_mp); 4930 return (NULL); 4931 } 4932 } 4933 4934 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 4935 CONN_DEC_REF(econnp); 4936 freemsg(first_mp); 4937 return (NULL); 4938 } 4939 4940 /* 4941 * If we know we have some policy, pass the "IPSEC" 4942 * options size TCP uses this adjust the MSS. 4943 */ 4944 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 4945 if (mctl_present) { 4946 freeb(first_mp); 4947 *mpp = mp; 4948 } 4949 4950 return (econnp); 4951 } 4952 4953 /* 4954 * tcp_get_conn/tcp_free_conn 4955 * 4956 * tcp_get_conn is used to get a clean tcp connection structure. 4957 * It tries to reuse the connections put on the freelist by the 4958 * time_wait_collector failing which it goes to kmem_cache. This 4959 * way has two benefits compared to just allocating from and 4960 * freeing to kmem_cache. 4961 * 1) The time_wait_collector can free (which includes the cleanup) 4962 * outside the squeue. So when the interrupt comes, we have a clean 4963 * connection sitting in the freelist. Obviously, this buys us 4964 * performance. 4965 * 4966 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 4967 * has multiple disadvantages - tying up the squeue during alloc, and the 4968 * fact that IPSec policy initialization has to happen here which 4969 * requires us sending a M_CTL and checking for it i.e. real ugliness. 4970 * But allocating the conn/tcp in IP land is also not the best since 4971 * we can't check the 'q' and 'q0' which are protected by squeue and 4972 * blindly allocate memory which might have to be freed here if we are 4973 * not allowed to accept the connection. By using the freelist and 4974 * putting the conn/tcp back in freelist, we don't pay a penalty for 4975 * allocating memory without checking 'q/q0' and freeing it if we can't 4976 * accept the connection. 4977 * 4978 * Care should be taken to put the conn back in the same squeue's freelist 4979 * from which it was allocated. Best results are obtained if conn is 4980 * allocated from listener's squeue and freed to the same. Time wait 4981 * collector will free up the freelist is the connection ends up sitting 4982 * there for too long. 4983 */ 4984 void * 4985 tcp_get_conn(void *arg, tcp_stack_t *tcps, boolean_t is_streams) 4986 { 4987 tcp_t *tcp = NULL; 4988 conn_t *connp = NULL; 4989 squeue_t *sqp = (squeue_t *)arg; 4990 tcp_squeue_priv_t *tcp_time_wait; 4991 netstack_t *ns; 4992 mblk_t *tcp_rsrv_mp = NULL; 4993 4994 tcp_time_wait = 4995 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 4996 4997 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 4998 tcp = tcp_time_wait->tcp_free_list; 4999 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5000 if (tcp != NULL) { 5001 if (is_streams && tcp->tcp_rsrv_mp == NULL) { 5002 /* 5003 * Pre-allocate the tcp_rsrv_mp if neccessary. 5004 * This mblk will not be freed until this conn_t/tcp_t 5005 * is freed at ipcl_conn_destroy(). 5006 */ 5007 if ((tcp->tcp_rsrv_mp = allocb(0, BPRI_HI)) == NULL) { 5008 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5009 return (NULL); 5010 } 5011 mutex_init(&tcp->tcp_rsrv_mp_lock, 5012 NULL, MUTEX_DEFAULT, NULL); 5013 } 5014 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5015 tcp_time_wait->tcp_free_list_cnt--; 5016 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5017 tcp->tcp_time_wait_next = NULL; 5018 connp = tcp->tcp_connp; 5019 connp->conn_flags |= IPCL_REUSED; 5020 5021 ASSERT(tcp->tcp_tcps == NULL); 5022 ASSERT(connp->conn_netstack == NULL); 5023 ASSERT(!is_streams || tcp->tcp_rsrv_mp != NULL); 5024 ns = tcps->tcps_netstack; 5025 netstack_hold(ns); 5026 connp->conn_netstack = ns; 5027 tcp->tcp_tcps = tcps; 5028 TCPS_REFHOLD(tcps); 5029 ipcl_globalhash_insert(connp); 5030 return ((void *)connp); 5031 } 5032 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5033 if (is_streams) { 5034 /* 5035 * Pre-allocate the tcp_rsrv_mp if neccessary. 5036 * This mblk will not be freed until this conn_t/tcp_t 5037 * is freed at ipcl_conn_destroy(). 5038 */ 5039 tcp_rsrv_mp = allocb(0, BPRI_HI); 5040 if (tcp_rsrv_mp == NULL) 5041 return (NULL); 5042 } 5043 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5044 tcps->tcps_netstack)) == NULL) { 5045 if (is_streams) { 5046 ASSERT(tcp_rsrv_mp != NULL); 5047 freeb(tcp_rsrv_mp); 5048 } 5049 return (NULL); 5050 } 5051 5052 tcp = connp->conn_tcp; 5053 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 5054 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5055 5056 tcp->tcp_tcps = tcps; 5057 TCPS_REFHOLD(tcps); 5058 5059 return ((void *)connp); 5060 } 5061 5062 /* 5063 * Update the cached label for the given tcp_t. This should be called once per 5064 * connection, and before any packets are sent or tcp_process_options is 5065 * invoked. Returns B_FALSE if the correct label could not be constructed. 5066 */ 5067 static boolean_t 5068 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5069 { 5070 conn_t *connp = tcp->tcp_connp; 5071 5072 if (tcp->tcp_ipversion == IPV4_VERSION) { 5073 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5074 int added; 5075 5076 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5077 connp->conn_mac_exempt, 5078 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5079 return (B_FALSE); 5080 5081 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5082 if (added == -1) 5083 return (B_FALSE); 5084 tcp->tcp_hdr_len += added; 5085 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5086 tcp->tcp_ip_hdr_len += added; 5087 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5088 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5089 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5090 tcp->tcp_hdr_len); 5091 if (added == -1) 5092 return (B_FALSE); 5093 tcp->tcp_hdr_len += added; 5094 tcp->tcp_tcph = (tcph_t *) 5095 ((uchar_t *)tcp->tcp_tcph + added); 5096 tcp->tcp_ip_hdr_len += added; 5097 } 5098 } else { 5099 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5100 5101 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5102 connp->conn_mac_exempt, 5103 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5104 return (B_FALSE); 5105 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5106 &tcp->tcp_label_len, optbuf) != 0) 5107 return (B_FALSE); 5108 if (tcp_build_hdrs(tcp) != 0) 5109 return (B_FALSE); 5110 } 5111 5112 connp->conn_ulp_labeled = 1; 5113 5114 return (B_TRUE); 5115 } 5116 5117 /* BEGIN CSTYLED */ 5118 /* 5119 * 5120 * The sockfs ACCEPT path: 5121 * ======================= 5122 * 5123 * The eager is now established in its own perimeter as soon as SYN is 5124 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5125 * completes the accept processing on the acceptor STREAM. The sending 5126 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5127 * listener but a TLI/XTI listener completes the accept processing 5128 * on the listener perimeter. 5129 * 5130 * Common control flow for 3 way handshake: 5131 * ---------------------------------------- 5132 * 5133 * incoming SYN (listener perimeter) -> tcp_rput_data() 5134 * -> tcp_conn_request() 5135 * 5136 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5137 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5138 * 5139 * Sockfs ACCEPT Path: 5140 * ------------------- 5141 * 5142 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5143 * as STREAM entry point) 5144 * 5145 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5146 * 5147 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5148 * association (we are not behind eager's squeue but sockfs is protecting us 5149 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5150 * is changed to point at tcp_wput(). 5151 * 5152 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5153 * listener (done on listener's perimeter). 5154 * 5155 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5156 * accept. 5157 * 5158 * TLI/XTI client ACCEPT path: 5159 * --------------------------- 5160 * 5161 * soaccept() sends T_CONN_RES on the listener STREAM. 5162 * 5163 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5164 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5165 * 5166 * Locks: 5167 * ====== 5168 * 5169 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5170 * and listeners->tcp_eager_next_q. 5171 * 5172 * Referencing: 5173 * ============ 5174 * 5175 * 1) We start out in tcp_conn_request by eager placing a ref on 5176 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5177 * 5178 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5179 * doing so we place a ref on the eager. This ref is finally dropped at the 5180 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5181 * reference is dropped by the squeue framework. 5182 * 5183 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5184 * 5185 * The reference must be released by the same entity that added the reference 5186 * In the above scheme, the eager is the entity that adds and releases the 5187 * references. Note that tcp_accept_finish executes in the squeue of the eager 5188 * (albeit after it is attached to the acceptor stream). Though 1. executes 5189 * in the listener's squeue, the eager is nascent at this point and the 5190 * reference can be considered to have been added on behalf of the eager. 5191 * 5192 * Eager getting a Reset or listener closing: 5193 * ========================================== 5194 * 5195 * Once the listener and eager are linked, the listener never does the unlink. 5196 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5197 * a message on all eager perimeter. The eager then does the unlink, clears 5198 * any pointers to the listener's queue and drops the reference to the 5199 * listener. The listener waits in tcp_close outside the squeue until its 5200 * refcount has dropped to 1. This ensures that the listener has waited for 5201 * all eagers to clear their association with the listener. 5202 * 5203 * Similarly, if eager decides to go away, it can unlink itself and close. 5204 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5205 * the reference to eager is still valid because of the extra ref we put 5206 * in tcp_send_conn_ind. 5207 * 5208 * Listener can always locate the eager under the protection 5209 * of the listener->tcp_eager_lock, and then do a refhold 5210 * on the eager during the accept processing. 5211 * 5212 * The acceptor stream accesses the eager in the accept processing 5213 * based on the ref placed on eager before sending T_conn_ind. 5214 * The only entity that can negate this refhold is a listener close 5215 * which is mutually exclusive with an active acceptor stream. 5216 * 5217 * Eager's reference on the listener 5218 * =================================== 5219 * 5220 * If the accept happens (even on a closed eager) the eager drops its 5221 * reference on the listener at the start of tcp_accept_finish. If the 5222 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5223 * the reference is dropped in tcp_closei_local. If the listener closes, 5224 * the reference is dropped in tcp_eager_kill. In all cases the reference 5225 * is dropped while executing in the eager's context (squeue). 5226 */ 5227 /* END CSTYLED */ 5228 5229 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5230 5231 /* 5232 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5233 * tcp_rput_data will not see any SYN packets. 5234 */ 5235 /* ARGSUSED */ 5236 void 5237 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5238 { 5239 tcph_t *tcph; 5240 uint32_t seg_seq; 5241 tcp_t *eager; 5242 uint_t ipvers; 5243 ipha_t *ipha; 5244 ip6_t *ip6h; 5245 int err; 5246 conn_t *econnp = NULL; 5247 squeue_t *new_sqp; 5248 mblk_t *mp1; 5249 uint_t ip_hdr_len; 5250 conn_t *connp = (conn_t *)arg; 5251 tcp_t *tcp = connp->conn_tcp; 5252 cred_t *credp; 5253 tcp_stack_t *tcps = tcp->tcp_tcps; 5254 ip_stack_t *ipst; 5255 5256 if (tcp->tcp_state != TCPS_LISTEN) 5257 goto error2; 5258 5259 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5260 5261 mutex_enter(&tcp->tcp_eager_lock); 5262 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5263 mutex_exit(&tcp->tcp_eager_lock); 5264 TCP_STAT(tcps, tcp_listendrop); 5265 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5266 if (tcp->tcp_debug) { 5267 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5268 "tcp_conn_request: listen backlog (max=%d) " 5269 "overflow (%d pending) on %s", 5270 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5271 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5272 } 5273 goto error2; 5274 } 5275 5276 if (tcp->tcp_conn_req_cnt_q0 >= 5277 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5278 /* 5279 * Q0 is full. Drop a pending half-open req from the queue 5280 * to make room for the new SYN req. Also mark the time we 5281 * drop a SYN. 5282 * 5283 * A more aggressive defense against SYN attack will 5284 * be to set the "tcp_syn_defense" flag now. 5285 */ 5286 TCP_STAT(tcps, tcp_listendropq0); 5287 tcp->tcp_last_rcv_lbolt = lbolt64; 5288 if (!tcp_drop_q0(tcp)) { 5289 mutex_exit(&tcp->tcp_eager_lock); 5290 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5291 if (tcp->tcp_debug) { 5292 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5293 "tcp_conn_request: listen half-open queue " 5294 "(max=%d) full (%d pending) on %s", 5295 tcps->tcps_conn_req_max_q0, 5296 tcp->tcp_conn_req_cnt_q0, 5297 tcp_display(tcp, NULL, 5298 DISP_PORT_ONLY)); 5299 } 5300 goto error2; 5301 } 5302 } 5303 mutex_exit(&tcp->tcp_eager_lock); 5304 5305 /* 5306 * IP adds STRUIO_EAGER and ensures that the received packet is 5307 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5308 * link local address. If IPSec is enabled, db_struioflag has 5309 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5310 * otherwise an error case if neither of them is set. 5311 */ 5312 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5313 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5314 DB_CKSUMSTART(mp) = 0; 5315 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5316 econnp = (conn_t *)tcp_get_conn(arg2, tcps, 5317 !IPCL_IS_NONSTR(connp)); 5318 if (econnp == NULL) 5319 goto error2; 5320 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5321 econnp->conn_sqp = new_sqp; 5322 econnp->conn_initial_sqp = new_sqp; 5323 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5324 /* 5325 * mp is updated in tcp_get_ipsec_conn(). 5326 */ 5327 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp, 5328 !IPCL_IS_NONSTR(connp)); 5329 if (econnp == NULL) { 5330 /* 5331 * mp freed by tcp_get_ipsec_conn. 5332 */ 5333 return; 5334 } 5335 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5336 } else { 5337 goto error2; 5338 } 5339 5340 ASSERT(DB_TYPE(mp) == M_DATA); 5341 5342 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5343 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5344 ASSERT(OK_32PTR(mp->b_rptr)); 5345 if (ipvers == IPV4_VERSION) { 5346 ipha = (ipha_t *)mp->b_rptr; 5347 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5348 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5349 } else { 5350 ip6h = (ip6_t *)mp->b_rptr; 5351 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5352 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5353 } 5354 5355 if (tcp->tcp_family == AF_INET) { 5356 ASSERT(ipvers == IPV4_VERSION); 5357 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5358 } else { 5359 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5360 } 5361 5362 if (err) 5363 goto error3; 5364 5365 eager = econnp->conn_tcp; 5366 ASSERT(eager->tcp_ordrel_mp == NULL); 5367 5368 if (!IPCL_IS_NONSTR(econnp)) { 5369 /* 5370 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that 5371 * at close time, we will always have that to send up. 5372 * Otherwise, we need to do special handling in case the 5373 * allocation fails at that time. 5374 */ 5375 if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5376 goto error3; 5377 } 5378 /* Inherit various TCP parameters from the listener */ 5379 eager->tcp_naglim = tcp->tcp_naglim; 5380 eager->tcp_first_timer_threshold = tcp->tcp_first_timer_threshold; 5381 eager->tcp_second_timer_threshold = tcp->tcp_second_timer_threshold; 5382 5383 eager->tcp_first_ctimer_threshold = tcp->tcp_first_ctimer_threshold; 5384 eager->tcp_second_ctimer_threshold = tcp->tcp_second_ctimer_threshold; 5385 5386 /* 5387 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5388 * If it does not, the eager's receive window will be set to the 5389 * listener's receive window later in this function. 5390 */ 5391 eager->tcp_rwnd = 0; 5392 5393 /* 5394 * Inherit listener's tcp_init_cwnd. Need to do this before 5395 * calling tcp_process_options() where tcp_mss_set() is called 5396 * to set the initial cwnd. 5397 */ 5398 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5399 5400 /* 5401 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5402 * zone id before the accept is completed in tcp_wput_accept(). 5403 */ 5404 econnp->conn_zoneid = connp->conn_zoneid; 5405 econnp->conn_allzones = connp->conn_allzones; 5406 5407 /* Copy nexthop information from listener to eager */ 5408 if (connp->conn_nexthop_set) { 5409 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5410 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5411 } 5412 5413 /* 5414 * TSOL: tsol_input_proc() needs the eager's cred before the 5415 * eager is accepted 5416 */ 5417 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5418 crhold(credp); 5419 5420 /* 5421 * If the caller has the process-wide flag set, then default to MAC 5422 * exempt mode. This allows read-down to unlabeled hosts. 5423 */ 5424 if (getpflags(NET_MAC_AWARE, credp) != 0) 5425 econnp->conn_mac_exempt = B_TRUE; 5426 5427 if (is_system_labeled()) { 5428 cred_t *cr; 5429 5430 if (connp->conn_mlp_type != mlptSingle) { 5431 cr = econnp->conn_peercred = msg_getcred(mp, NULL); 5432 if (cr != NULL) 5433 crhold(cr); 5434 else 5435 cr = econnp->conn_cred; 5436 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5437 econnp, cred_t *, cr) 5438 } else { 5439 cr = econnp->conn_cred; 5440 DTRACE_PROBE2(syn_accept, conn_t *, 5441 econnp, cred_t *, cr) 5442 } 5443 5444 if (!tcp_update_label(eager, cr)) { 5445 DTRACE_PROBE3( 5446 tx__ip__log__error__connrequest__tcp, 5447 char *, "eager connp(1) label on SYN mp(2) failed", 5448 conn_t *, econnp, mblk_t *, mp); 5449 goto error3; 5450 } 5451 } 5452 5453 eager->tcp_hard_binding = B_TRUE; 5454 5455 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5456 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5457 5458 CL_INET_CONNECT(connp, eager, B_FALSE, err); 5459 if (err != 0) { 5460 tcp_bind_hash_remove(eager); 5461 goto error3; 5462 } 5463 5464 /* 5465 * No need to check for multicast destination since ip will only pass 5466 * up multicasts to those that have expressed interest 5467 * TODO: what about rejecting broadcasts? 5468 * Also check that source is not a multicast or broadcast address. 5469 */ 5470 eager->tcp_state = TCPS_SYN_RCVD; 5471 5472 5473 /* 5474 * There should be no ire in the mp as we are being called after 5475 * receiving the SYN. 5476 */ 5477 ASSERT(tcp_ire_mp(&mp) == NULL); 5478 5479 /* 5480 * Adapt our mss, ttl, ... according to information provided in IRE. 5481 */ 5482 5483 if (tcp_adapt_ire(eager, NULL) == 0) { 5484 /* Undo the bind_hash_insert */ 5485 tcp_bind_hash_remove(eager); 5486 goto error3; 5487 } 5488 5489 /* Process all TCP options. */ 5490 tcp_process_options(eager, tcph); 5491 5492 /* Is the other end ECN capable? */ 5493 if (tcps->tcps_ecn_permitted >= 1 && 5494 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5495 eager->tcp_ecn_ok = B_TRUE; 5496 } 5497 5498 /* 5499 * listener->tcp_rq->q_hiwat should be the default window size or a 5500 * window size changed via SO_RCVBUF option. First round up the 5501 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5502 * scale option value if needed. Call tcp_rwnd_set() to finish the 5503 * setting. 5504 * 5505 * Note if there is a rpipe metric associated with the remote host, 5506 * we should not inherit receive window size from listener. 5507 */ 5508 eager->tcp_rwnd = MSS_ROUNDUP( 5509 (eager->tcp_rwnd == 0 ? tcp->tcp_recv_hiwater: 5510 eager->tcp_rwnd), eager->tcp_mss); 5511 if (eager->tcp_snd_ws_ok) 5512 tcp_set_ws_value(eager); 5513 /* 5514 * Note that this is the only place tcp_rwnd_set() is called for 5515 * accepting a connection. We need to call it here instead of 5516 * after the 3-way handshake because we need to tell the other 5517 * side our rwnd in the SYN-ACK segment. 5518 */ 5519 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5520 5521 /* 5522 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5523 * via soaccept()->soinheritoptions() which essentially applies 5524 * all the listener options to the new STREAM. The options that we 5525 * need to take care of are: 5526 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5527 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5528 * SO_SNDBUF, SO_RCVBUF. 5529 * 5530 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5531 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5532 * tcp_maxpsz_set() gets called later from 5533 * tcp_accept_finish(), the option takes effect. 5534 * 5535 */ 5536 /* Set the TCP options */ 5537 eager->tcp_recv_hiwater = tcp->tcp_recv_hiwater; 5538 eager->tcp_recv_lowater = tcp->tcp_recv_lowater; 5539 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5540 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5541 eager->tcp_oobinline = tcp->tcp_oobinline; 5542 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5543 eager->tcp_broadcast = tcp->tcp_broadcast; 5544 eager->tcp_useloopback = tcp->tcp_useloopback; 5545 eager->tcp_dontroute = tcp->tcp_dontroute; 5546 eager->tcp_debug = tcp->tcp_debug; 5547 eager->tcp_linger = tcp->tcp_linger; 5548 eager->tcp_lingertime = tcp->tcp_lingertime; 5549 if (tcp->tcp_ka_enabled) 5550 eager->tcp_ka_enabled = 1; 5551 5552 /* Set the IP options */ 5553 econnp->conn_broadcast = connp->conn_broadcast; 5554 econnp->conn_loopback = connp->conn_loopback; 5555 econnp->conn_dontroute = connp->conn_dontroute; 5556 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5557 5558 /* Put a ref on the listener for the eager. */ 5559 CONN_INC_REF(connp); 5560 mutex_enter(&tcp->tcp_eager_lock); 5561 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5562 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5563 tcp->tcp_eager_next_q0 = eager; 5564 eager->tcp_eager_prev_q0 = tcp; 5565 5566 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5567 eager->tcp_listener = tcp; 5568 eager->tcp_saved_listener = tcp; 5569 5570 /* 5571 * Tag this detached tcp vector for later retrieval 5572 * by our listener client in tcp_accept(). 5573 */ 5574 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5575 tcp->tcp_conn_req_cnt_q0++; 5576 if (++tcp->tcp_conn_req_seqnum == -1) { 5577 /* 5578 * -1 is "special" and defined in TPI as something 5579 * that should never be used in T_CONN_IND 5580 */ 5581 ++tcp->tcp_conn_req_seqnum; 5582 } 5583 mutex_exit(&tcp->tcp_eager_lock); 5584 5585 if (tcp->tcp_syn_defense) { 5586 /* Don't drop the SYN that comes from a good IP source */ 5587 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5588 if (addr_cache != NULL && eager->tcp_remote == 5589 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5590 eager->tcp_dontdrop = B_TRUE; 5591 } 5592 } 5593 5594 /* 5595 * We need to insert the eager in its own perimeter but as soon 5596 * as we do that, we expose the eager to the classifier and 5597 * should not touch any field outside the eager's perimeter. 5598 * So do all the work necessary before inserting the eager 5599 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5600 * will succeed but undo everything if it fails. 5601 */ 5602 seg_seq = ABE32_TO_U32(tcph->th_seq); 5603 eager->tcp_irs = seg_seq; 5604 eager->tcp_rack = seg_seq; 5605 eager->tcp_rnxt = seg_seq + 1; 5606 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5607 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5608 eager->tcp_state = TCPS_SYN_RCVD; 5609 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5610 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5611 if (mp1 == NULL) { 5612 /* 5613 * Increment the ref count as we are going to 5614 * enqueueing an mp in squeue 5615 */ 5616 CONN_INC_REF(econnp); 5617 goto error; 5618 } 5619 5620 /* 5621 * Note that in theory this should use the current pid 5622 * so that getpeerucred on the client returns the actual listener 5623 * that does accept. But accept() hasn't been called yet. We could use 5624 * the pid of the process that did bind/listen on the server. 5625 * However, with common usage like inetd() the bind/listen can be done 5626 * by a different process than the accept(). 5627 * Hence we do the simple thing of using the open pid here. 5628 * Note that db_credp is set later in tcp_send_data(). 5629 */ 5630 mblk_setcred(mp1, credp, tcp->tcp_cpid); 5631 eager->tcp_cpid = tcp->tcp_cpid; 5632 eager->tcp_open_time = lbolt64; 5633 5634 /* 5635 * We need to start the rto timer. In normal case, we start 5636 * the timer after sending the packet on the wire (or at 5637 * least believing that packet was sent by waiting for 5638 * CALL_IP_WPUT() to return). Since this is the first packet 5639 * being sent on the wire for the eager, our initial tcp_rto 5640 * is at least tcp_rexmit_interval_min which is a fairly 5641 * large value to allow the algorithm to adjust slowly to large 5642 * fluctuations of RTT during first few transmissions. 5643 * 5644 * Starting the timer first and then sending the packet in this 5645 * case shouldn't make much difference since tcp_rexmit_interval_min 5646 * is of the order of several 100ms and starting the timer 5647 * first and then sending the packet will result in difference 5648 * of few micro seconds. 5649 * 5650 * Without this optimization, we are forced to hold the fanout 5651 * lock across the ipcl_bind_insert() and sending the packet 5652 * so that we don't race against an incoming packet (maybe RST) 5653 * for this eager. 5654 * 5655 * It is necessary to acquire an extra reference on the eager 5656 * at this point and hold it until after tcp_send_data() to 5657 * ensure against an eager close race. 5658 */ 5659 5660 CONN_INC_REF(eager->tcp_connp); 5661 5662 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5663 5664 /* 5665 * Insert the eager in its own perimeter now. We are ready to deal 5666 * with any packets on eager. 5667 */ 5668 if (eager->tcp_ipversion == IPV4_VERSION) { 5669 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5670 goto error; 5671 } 5672 } else { 5673 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5674 goto error; 5675 } 5676 } 5677 5678 /* mark conn as fully-bound */ 5679 econnp->conn_fully_bound = B_TRUE; 5680 5681 /* Send the SYN-ACK */ 5682 tcp_send_data(eager, eager->tcp_wq, mp1); 5683 CONN_DEC_REF(eager->tcp_connp); 5684 freemsg(mp); 5685 5686 return; 5687 error: 5688 freemsg(mp1); 5689 eager->tcp_closemp_used = B_TRUE; 5690 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 5691 mp1 = &eager->tcp_closemp; 5692 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill, 5693 econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_2); 5694 5695 /* 5696 * If a connection already exists, send the mp to that connections so 5697 * that it can be appropriately dealt with. 5698 */ 5699 ipst = tcps->tcps_netstack->netstack_ip; 5700 5701 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 5702 if (!IPCL_IS_CONNECTED(econnp)) { 5703 /* 5704 * Something bad happened. ipcl_conn_insert() 5705 * failed because a connection already existed 5706 * in connected hash but we can't find it 5707 * anymore (someone blew it away). Just 5708 * free this message and hopefully remote 5709 * will retransmit at which time the SYN can be 5710 * treated as a new connection or dealth with 5711 * a TH_RST if a connection already exists. 5712 */ 5713 CONN_DEC_REF(econnp); 5714 freemsg(mp); 5715 } else { 5716 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, 5717 tcp_input, econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_1); 5718 } 5719 } else { 5720 /* Nobody wants this packet */ 5721 freemsg(mp); 5722 } 5723 return; 5724 error3: 5725 CONN_DEC_REF(econnp); 5726 error2: 5727 freemsg(mp); 5728 } 5729 5730 /* 5731 * In an ideal case of vertical partition in NUMA architecture, its 5732 * beneficial to have the listener and all the incoming connections 5733 * tied to the same squeue. The other constraint is that incoming 5734 * connections should be tied to the squeue attached to interrupted 5735 * CPU for obvious locality reason so this leaves the listener to 5736 * be tied to the same squeue. Our only problem is that when listener 5737 * is binding, the CPU that will get interrupted by the NIC whose 5738 * IP address the listener is binding to is not even known. So 5739 * the code below allows us to change that binding at the time the 5740 * CPU is interrupted by virtue of incoming connection's squeue. 5741 * 5742 * This is usefull only in case of a listener bound to a specific IP 5743 * address. For other kind of listeners, they get bound the 5744 * very first time and there is no attempt to rebind them. 5745 */ 5746 void 5747 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5748 { 5749 conn_t *connp = (conn_t *)arg; 5750 squeue_t *sqp = (squeue_t *)arg2; 5751 squeue_t *new_sqp; 5752 uint32_t conn_flags; 5753 5754 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5755 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5756 } else { 5757 goto done; 5758 } 5759 5760 if (connp->conn_fanout == NULL) 5761 goto done; 5762 5763 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5764 mutex_enter(&connp->conn_fanout->connf_lock); 5765 mutex_enter(&connp->conn_lock); 5766 /* 5767 * No one from read or write side can access us now 5768 * except for already queued packets on this squeue. 5769 * But since we haven't changed the squeue yet, they 5770 * can't execute. If they are processed after we have 5771 * changed the squeue, they are sent back to the 5772 * correct squeue down below. 5773 * But a listner close can race with processing of 5774 * incoming SYN. If incoming SYN processing changes 5775 * the squeue then the listener close which is waiting 5776 * to enter the squeue would operate on the wrong 5777 * squeue. Hence we don't change the squeue here unless 5778 * the refcount is exactly the minimum refcount. The 5779 * minimum refcount of 4 is counted as - 1 each for 5780 * TCP and IP, 1 for being in the classifier hash, and 5781 * 1 for the mblk being processed. 5782 */ 5783 5784 if (connp->conn_ref != 4 || 5785 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 5786 mutex_exit(&connp->conn_lock); 5787 mutex_exit(&connp->conn_fanout->connf_lock); 5788 goto done; 5789 } 5790 if (connp->conn_sqp != new_sqp) { 5791 while (connp->conn_sqp != new_sqp) 5792 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5793 } 5794 5795 do { 5796 conn_flags = connp->conn_flags; 5797 conn_flags |= IPCL_FULLY_BOUND; 5798 (void) cas32(&connp->conn_flags, connp->conn_flags, 5799 conn_flags); 5800 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5801 5802 mutex_exit(&connp->conn_fanout->connf_lock); 5803 mutex_exit(&connp->conn_lock); 5804 } 5805 5806 done: 5807 if (connp->conn_sqp != sqp) { 5808 CONN_INC_REF(connp); 5809 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 5810 SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND); 5811 } else { 5812 tcp_conn_request(connp, mp, sqp); 5813 } 5814 } 5815 5816 /* 5817 * Successful connect request processing begins when our client passes 5818 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5819 * our T_OK_ACK reply message upstream. The control flow looks like this: 5820 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_tpi_connect() -> IP 5821 * upstream <- tcp_rput() <- IP 5822 * After various error checks are completed, tcp_tpi_connect() lays 5823 * the target address and port into the composite header template, 5824 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5825 * request followed by an IRE request, and passes the three mblk message 5826 * down to IP looking like this: 5827 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5828 * Processing continues in tcp_rput() when we receive the following message: 5829 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5830 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5831 * to fire off the connection request, and then passes the T_OK_ACK mblk 5832 * upstream that we filled in below. There are, of course, numerous 5833 * error conditions along the way which truncate the processing described 5834 * above. 5835 */ 5836 static void 5837 tcp_tpi_connect(tcp_t *tcp, mblk_t *mp) 5838 { 5839 sin_t *sin; 5840 queue_t *q = tcp->tcp_wq; 5841 struct T_conn_req *tcr; 5842 struct sockaddr *sa; 5843 socklen_t len; 5844 int error; 5845 cred_t *cr; 5846 pid_t cpid; 5847 5848 /* 5849 * All Solaris components should pass a db_credp 5850 * for this TPI message, hence we ASSERT. 5851 * But in case there is some other M_PROTO that looks 5852 * like a TPI message sent by some other kernel 5853 * component, we check and return an error. 5854 */ 5855 cr = msg_getcred(mp, &cpid); 5856 ASSERT(cr != NULL); 5857 if (cr == NULL) { 5858 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5859 return; 5860 } 5861 5862 tcr = (struct T_conn_req *)mp->b_rptr; 5863 5864 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5865 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5866 tcp_err_ack(tcp, mp, TPROTO, 0); 5867 return; 5868 } 5869 5870 /* 5871 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5872 * will always have that to send up. Otherwise, we need to do 5873 * special handling in case the allocation fails at that time. 5874 * If the end point is TPI, the tcp_t can be reused and the 5875 * tcp_ordrel_mp may be allocated already. 5876 */ 5877 if (tcp->tcp_ordrel_mp == NULL) { 5878 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 5879 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5880 return; 5881 } 5882 } 5883 5884 /* 5885 * Determine packet type based on type of address passed in 5886 * the request should contain an IPv4 or IPv6 address. 5887 * Make sure that address family matches the type of 5888 * family of the the address passed down 5889 */ 5890 switch (tcr->DEST_length) { 5891 default: 5892 tcp_err_ack(tcp, mp, TBADADDR, 0); 5893 return; 5894 5895 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5896 /* 5897 * XXX: The check for valid DEST_length was not there 5898 * in earlier releases and some buggy 5899 * TLI apps (e.g Sybase) got away with not feeding 5900 * in sin_zero part of address. 5901 * We allow that bug to keep those buggy apps humming. 5902 * Test suites require the check on DEST_length. 5903 * We construct a new mblk with valid DEST_length 5904 * free the original so the rest of the code does 5905 * not have to keep track of this special shorter 5906 * length address case. 5907 */ 5908 mblk_t *nmp; 5909 struct T_conn_req *ntcr; 5910 sin_t *nsin; 5911 5912 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 5913 tcr->OPT_length, BPRI_HI); 5914 if (nmp == NULL) { 5915 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5916 return; 5917 } 5918 ntcr = (struct T_conn_req *)nmp->b_rptr; 5919 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 5920 ntcr->PRIM_type = T_CONN_REQ; 5921 ntcr->DEST_length = sizeof (sin_t); 5922 ntcr->DEST_offset = sizeof (struct T_conn_req); 5923 5924 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 5925 *nsin = sin_null; 5926 /* Get pointer to shorter address to copy from original mp */ 5927 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 5928 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 5929 if (sin == NULL || !OK_32PTR((char *)sin)) { 5930 freemsg(nmp); 5931 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5932 return; 5933 } 5934 nsin->sin_family = sin->sin_family; 5935 nsin->sin_port = sin->sin_port; 5936 nsin->sin_addr = sin->sin_addr; 5937 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 5938 nmp->b_wptr = (uchar_t *)&nsin[1]; 5939 if (tcr->OPT_length != 0) { 5940 ntcr->OPT_length = tcr->OPT_length; 5941 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 5942 bcopy((uchar_t *)tcr + tcr->OPT_offset, 5943 (uchar_t *)ntcr + ntcr->OPT_offset, 5944 tcr->OPT_length); 5945 nmp->b_wptr += tcr->OPT_length; 5946 } 5947 freemsg(mp); /* original mp freed */ 5948 mp = nmp; /* re-initialize original variables */ 5949 tcr = ntcr; 5950 } 5951 /* FALLTHRU */ 5952 5953 case sizeof (sin_t): 5954 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 5955 sizeof (sin_t)); 5956 len = sizeof (sin_t); 5957 break; 5958 5959 case sizeof (sin6_t): 5960 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 5961 sizeof (sin6_t)); 5962 len = sizeof (sin6_t); 5963 break; 5964 } 5965 5966 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 5967 if (error != 0) { 5968 tcp_err_ack(tcp, mp, TSYSERR, error); 5969 return; 5970 } 5971 5972 /* 5973 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 5974 * should key on their sequence number and cut them loose. 5975 */ 5976 5977 /* 5978 * If options passed in, feed it for verification and handling 5979 */ 5980 if (tcr->OPT_length != 0) { 5981 mblk_t *ok_mp; 5982 mblk_t *discon_mp; 5983 mblk_t *conn_opts_mp; 5984 int t_error, sys_error, do_disconnect; 5985 5986 conn_opts_mp = NULL; 5987 5988 if (tcp_conprim_opt_process(tcp, mp, 5989 &do_disconnect, &t_error, &sys_error) < 0) { 5990 if (do_disconnect) { 5991 ASSERT(t_error == 0 && sys_error == 0); 5992 discon_mp = mi_tpi_discon_ind(NULL, 5993 ECONNREFUSED, 0); 5994 if (!discon_mp) { 5995 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 5996 TSYSERR, ENOMEM); 5997 return; 5998 } 5999 ok_mp = mi_tpi_ok_ack_alloc(mp); 6000 if (!ok_mp) { 6001 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6002 TSYSERR, ENOMEM); 6003 return; 6004 } 6005 qreply(q, ok_mp); 6006 qreply(q, discon_mp); /* no flush! */ 6007 } else { 6008 ASSERT(t_error != 0); 6009 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6010 sys_error); 6011 } 6012 return; 6013 } 6014 /* 6015 * Success in setting options, the mp option buffer represented 6016 * by OPT_length/offset has been potentially modified and 6017 * contains results of option processing. We copy it in 6018 * another mp to save it for potentially influencing returning 6019 * it in T_CONN_CONN. 6020 */ 6021 if (tcr->OPT_length != 0) { /* there are resulting options */ 6022 conn_opts_mp = copyb(mp); 6023 if (!conn_opts_mp) { 6024 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6025 TSYSERR, ENOMEM); 6026 return; 6027 } 6028 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6029 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6030 /* 6031 * Note: 6032 * These resulting option negotiation can include any 6033 * end-to-end negotiation options but there no such 6034 * thing (yet?) in our TCP/IP. 6035 */ 6036 } 6037 } 6038 6039 /* call the non-TPI version */ 6040 error = tcp_do_connect(tcp->tcp_connp, sa, len, cr, cpid); 6041 if (error < 0) { 6042 mp = mi_tpi_err_ack_alloc(mp, -error, 0); 6043 } else if (error > 0) { 6044 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 6045 } else { 6046 mp = mi_tpi_ok_ack_alloc(mp); 6047 } 6048 6049 /* 6050 * Note: Code below is the "failure" case 6051 */ 6052 /* return error ack and blow away saved option results if any */ 6053 connect_failed: 6054 if (mp != NULL) 6055 putnext(tcp->tcp_rq, mp); 6056 else { 6057 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6058 TSYSERR, ENOMEM); 6059 } 6060 } 6061 6062 /* 6063 * Handle connect to IPv4 destinations, including connections for AF_INET6 6064 * sockets connecting to IPv4 mapped IPv6 destinations. 6065 */ 6066 static int 6067 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport, 6068 uint_t srcid, cred_t *cr, pid_t pid) 6069 { 6070 tcph_t *tcph; 6071 mblk_t *mp; 6072 ipaddr_t dstaddr = *dstaddrp; 6073 int32_t oldstate; 6074 uint16_t lport; 6075 int error = 0; 6076 tcp_stack_t *tcps = tcp->tcp_tcps; 6077 6078 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6079 6080 /* Check for attempt to connect to INADDR_ANY */ 6081 if (dstaddr == INADDR_ANY) { 6082 /* 6083 * SunOS 4.x and 4.3 BSD allow an application 6084 * to connect a TCP socket to INADDR_ANY. 6085 * When they do this, the kernel picks the 6086 * address of one interface and uses it 6087 * instead. The kernel usually ends up 6088 * picking the address of the loopback 6089 * interface. This is an undocumented feature. 6090 * However, we provide the same thing here 6091 * in order to have source and binary 6092 * compatibility with SunOS 4.x. 6093 * Update the T_CONN_REQ (sin/sin6) since it is used to 6094 * generate the T_CONN_CON. 6095 */ 6096 dstaddr = htonl(INADDR_LOOPBACK); 6097 *dstaddrp = dstaddr; 6098 } 6099 6100 /* Handle __sin6_src_id if socket not bound to an IP address */ 6101 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6102 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6103 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6104 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6105 tcp->tcp_ipha->ipha_src); 6106 } 6107 6108 /* 6109 * Don't let an endpoint connect to itself. Note that 6110 * the test here does not catch the case where the 6111 * source IP addr was left unspecified by the user. In 6112 * this case, the source addr is set in tcp_adapt_ire() 6113 * using the reply to the T_BIND message that we send 6114 * down to IP here and the check is repeated in tcp_rput_other. 6115 */ 6116 if (dstaddr == tcp->tcp_ipha->ipha_src && 6117 dstport == tcp->tcp_lport) { 6118 error = -TBADADDR; 6119 goto failed; 6120 } 6121 6122 tcp->tcp_ipha->ipha_dst = dstaddr; 6123 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6124 6125 /* 6126 * Massage a source route if any putting the first hop 6127 * in iph_dst. Compute a starting value for the checksum which 6128 * takes into account that the original iph_dst should be 6129 * included in the checksum but that ip will include the 6130 * first hop in the source route in the tcp checksum. 6131 */ 6132 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6133 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6134 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6135 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6136 if ((int)tcp->tcp_sum < 0) 6137 tcp->tcp_sum--; 6138 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6139 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6140 (tcp->tcp_sum >> 16)); 6141 tcph = tcp->tcp_tcph; 6142 *(uint16_t *)tcph->th_fport = dstport; 6143 tcp->tcp_fport = dstport; 6144 6145 oldstate = tcp->tcp_state; 6146 /* 6147 * At this point the remote destination address and remote port fields 6148 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6149 * have to see which state tcp was in so we can take apropriate action. 6150 */ 6151 if (oldstate == TCPS_IDLE) { 6152 /* 6153 * We support a quick connect capability here, allowing 6154 * clients to transition directly from IDLE to SYN_SENT 6155 * tcp_bindi will pick an unused port, insert the connection 6156 * in the bind hash and transition to BOUND state. 6157 */ 6158 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6159 tcp, B_TRUE); 6160 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6161 B_FALSE, B_FALSE); 6162 if (lport == 0) { 6163 error = -TNOADDR; 6164 goto failed; 6165 } 6166 } 6167 tcp->tcp_state = TCPS_SYN_SENT; 6168 6169 mp = allocb(sizeof (ire_t), BPRI_HI); 6170 if (mp == NULL) { 6171 tcp->tcp_state = oldstate; 6172 error = ENOMEM; 6173 goto failed; 6174 } 6175 6176 mp->b_wptr += sizeof (ire_t); 6177 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6178 tcp->tcp_hard_binding = 1; 6179 6180 /* 6181 * We need to make sure that the conn_recv is set to a non-null 6182 * value before we insert the conn_t into the classifier table. 6183 * This is to avoid a race with an incoming packet which does 6184 * an ipcl_classify(). 6185 */ 6186 tcp->tcp_connp->conn_recv = tcp_input; 6187 6188 if (tcp->tcp_family == AF_INET) { 6189 error = ip_proto_bind_connected_v4(tcp->tcp_connp, &mp, 6190 IPPROTO_TCP, &tcp->tcp_ipha->ipha_src, tcp->tcp_lport, 6191 tcp->tcp_remote, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6192 } else { 6193 in6_addr_t v6src; 6194 if (tcp->tcp_ipversion == IPV4_VERSION) { 6195 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6196 } else { 6197 v6src = tcp->tcp_ip6h->ip6_src; 6198 } 6199 error = ip_proto_bind_connected_v6(tcp->tcp_connp, &mp, 6200 IPPROTO_TCP, &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6201 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6202 } 6203 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6204 tcp->tcp_active_open = 1; 6205 6206 6207 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6208 failed: 6209 /* return error ack and blow away saved option results if any */ 6210 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6211 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6212 return (error); 6213 } 6214 6215 /* 6216 * Handle connect to IPv6 destinations. 6217 */ 6218 static int 6219 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport, 6220 uint32_t flowinfo, uint_t srcid, uint32_t scope_id, cred_t *cr, pid_t pid) 6221 { 6222 tcph_t *tcph; 6223 mblk_t *mp; 6224 ip6_rthdr_t *rth; 6225 int32_t oldstate; 6226 uint16_t lport; 6227 tcp_stack_t *tcps = tcp->tcp_tcps; 6228 int error = 0; 6229 conn_t *connp = tcp->tcp_connp; 6230 6231 ASSERT(tcp->tcp_family == AF_INET6); 6232 6233 /* 6234 * If we're here, it means that the destination address is a native 6235 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6236 * reason why it might not be IPv6 is if the socket was bound to an 6237 * IPv4-mapped IPv6 address. 6238 */ 6239 if (tcp->tcp_ipversion != IPV6_VERSION) { 6240 return (-TBADADDR); 6241 } 6242 6243 /* 6244 * Interpret a zero destination to mean loopback. 6245 * Update the T_CONN_REQ (sin/sin6) since it is used to 6246 * generate the T_CONN_CON. 6247 */ 6248 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6249 *dstaddrp = ipv6_loopback; 6250 } 6251 6252 /* Handle __sin6_src_id if socket not bound to an IP address */ 6253 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6254 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6255 connp->conn_zoneid, tcps->tcps_netstack); 6256 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6257 } 6258 6259 /* 6260 * Take care of the scope_id now and add ip6i_t 6261 * if ip6i_t is not already allocated through TCP 6262 * sticky options. At this point tcp_ip6h does not 6263 * have dst info, thus use dstaddrp. 6264 */ 6265 if (scope_id != 0 && 6266 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6267 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6268 ip6i_t *ip6i; 6269 6270 ipp->ipp_ifindex = scope_id; 6271 ip6i = (ip6i_t *)tcp->tcp_iphc; 6272 6273 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6274 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6275 /* Already allocated */ 6276 ip6i->ip6i_flags |= IP6I_IFINDEX; 6277 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6278 ipp->ipp_fields |= IPPF_SCOPE_ID; 6279 } else { 6280 int reterr; 6281 6282 ipp->ipp_fields |= IPPF_SCOPE_ID; 6283 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6284 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6285 reterr = tcp_build_hdrs(tcp); 6286 if (reterr != 0) 6287 goto failed; 6288 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6289 } 6290 } 6291 6292 /* 6293 * Don't let an endpoint connect to itself. Note that 6294 * the test here does not catch the case where the 6295 * source IP addr was left unspecified by the user. In 6296 * this case, the source addr is set in tcp_adapt_ire() 6297 * using the reply to the T_BIND message that we send 6298 * down to IP here and the check is repeated in tcp_rput_other. 6299 */ 6300 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6301 (dstport == tcp->tcp_lport)) { 6302 error = -TBADADDR; 6303 goto failed; 6304 } 6305 6306 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6307 tcp->tcp_remote_v6 = *dstaddrp; 6308 tcp->tcp_ip6h->ip6_vcf = 6309 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6310 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6311 6312 /* 6313 * Massage a routing header (if present) putting the first hop 6314 * in ip6_dst. Compute a starting value for the checksum which 6315 * takes into account that the original ip6_dst should be 6316 * included in the checksum but that ip will include the 6317 * first hop in the source route in the tcp checksum. 6318 */ 6319 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6320 if (rth != NULL) { 6321 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6322 tcps->tcps_netstack); 6323 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6324 (tcp->tcp_sum >> 16)); 6325 } else { 6326 tcp->tcp_sum = 0; 6327 } 6328 6329 tcph = tcp->tcp_tcph; 6330 *(uint16_t *)tcph->th_fport = dstport; 6331 tcp->tcp_fport = dstport; 6332 6333 oldstate = tcp->tcp_state; 6334 /* 6335 * At this point the remote destination address and remote port fields 6336 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6337 * have to see which state tcp was in so we can take apropriate action. 6338 */ 6339 if (oldstate == TCPS_IDLE) { 6340 /* 6341 * We support a quick connect capability here, allowing 6342 * clients to transition directly from IDLE to SYN_SENT 6343 * tcp_bindi will pick an unused port, insert the connection 6344 * in the bind hash and transition to BOUND state. 6345 */ 6346 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6347 tcp, B_TRUE); 6348 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6349 B_FALSE, B_FALSE); 6350 if (lport == 0) { 6351 error = -TNOADDR; 6352 goto failed; 6353 } 6354 } 6355 tcp->tcp_state = TCPS_SYN_SENT; 6356 6357 mp = allocb(sizeof (ire_t), BPRI_HI); 6358 if (mp != NULL) { 6359 in6_addr_t v6src; 6360 6361 mp->b_wptr += sizeof (ire_t); 6362 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6363 6364 tcp->tcp_hard_binding = 1; 6365 6366 /* 6367 * We need to make sure that the conn_recv is set to a non-null 6368 * value before we insert the conn_t into the classifier table. 6369 * This is to avoid a race with an incoming packet which does 6370 * an ipcl_classify(). 6371 */ 6372 tcp->tcp_connp->conn_recv = tcp_input; 6373 6374 if (tcp->tcp_ipversion == IPV4_VERSION) { 6375 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6376 } else { 6377 v6src = tcp->tcp_ip6h->ip6_src; 6378 } 6379 error = ip_proto_bind_connected_v6(connp, &mp, IPPROTO_TCP, 6380 &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6381 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6382 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6383 tcp->tcp_active_open = 1; 6384 6385 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6386 } 6387 /* Error case */ 6388 tcp->tcp_state = oldstate; 6389 error = ENOMEM; 6390 6391 failed: 6392 /* return error ack and blow away saved option results if any */ 6393 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6394 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6395 return (error); 6396 } 6397 6398 /* 6399 * We need a stream q for detached closing tcp connections 6400 * to use. Our client hereby indicates that this q is the 6401 * one to use. 6402 */ 6403 static void 6404 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6405 { 6406 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6407 queue_t *q = tcp->tcp_wq; 6408 tcp_stack_t *tcps = tcp->tcp_tcps; 6409 6410 #ifdef NS_DEBUG 6411 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6412 tcps->tcps_netstack->netstack_stackid); 6413 #endif 6414 mp->b_datap->db_type = M_IOCACK; 6415 iocp->ioc_count = 0; 6416 mutex_enter(&tcps->tcps_g_q_lock); 6417 if (tcps->tcps_g_q != NULL) { 6418 mutex_exit(&tcps->tcps_g_q_lock); 6419 iocp->ioc_error = EALREADY; 6420 } else { 6421 int error = 0; 6422 conn_t *connp = tcp->tcp_connp; 6423 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6424 6425 tcps->tcps_g_q = tcp->tcp_rq; 6426 mutex_exit(&tcps->tcps_g_q_lock); 6427 iocp->ioc_error = 0; 6428 iocp->ioc_rval = 0; 6429 /* 6430 * We are passing tcp_sticky_ipp as NULL 6431 * as it is not useful for tcp_default queue 6432 * 6433 * Set conn_recv just in case. 6434 */ 6435 tcp->tcp_connp->conn_recv = tcp_conn_request; 6436 6437 ASSERT(connp->conn_af_isv6); 6438 connp->conn_ulp = IPPROTO_TCP; 6439 6440 if (ipst->ips_ipcl_proto_fanout_v6[IPPROTO_TCP].connf_head != 6441 NULL || connp->conn_mac_exempt) { 6442 error = -TBADADDR; 6443 } else { 6444 connp->conn_srcv6 = ipv6_all_zeros; 6445 ipcl_proto_insert_v6(connp, IPPROTO_TCP); 6446 } 6447 6448 (void) tcp_post_ip_bind(tcp, NULL, error, NULL, 0); 6449 } 6450 qreply(q, mp); 6451 } 6452 6453 static int 6454 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum) 6455 { 6456 tcp_t *ltcp = NULL; 6457 conn_t *connp; 6458 tcp_stack_t *tcps = tcp->tcp_tcps; 6459 6460 /* 6461 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6462 * when the stream is in BOUND state. Do not send a reset, 6463 * since the destination IP address is not valid, and it can 6464 * be the initialized value of all zeros (broadcast address). 6465 * 6466 * XXX There won't be any pending bind request to IP. 6467 */ 6468 if (tcp->tcp_state <= TCPS_BOUND) { 6469 if (tcp->tcp_debug) { 6470 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6471 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6472 } 6473 return (TOUTSTATE); 6474 } 6475 6476 6477 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6478 6479 /* 6480 * According to TPI, for non-listeners, ignore seqnum 6481 * and disconnect. 6482 * Following interpretation of -1 seqnum is historical 6483 * and implied TPI ? (TPI only states that for T_CONN_IND, 6484 * a valid seqnum should not be -1). 6485 * 6486 * -1 means disconnect everything 6487 * regardless even on a listener. 6488 */ 6489 6490 int old_state = tcp->tcp_state; 6491 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6492 6493 /* 6494 * The connection can't be on the tcp_time_wait_head list 6495 * since it is not detached. 6496 */ 6497 ASSERT(tcp->tcp_time_wait_next == NULL); 6498 ASSERT(tcp->tcp_time_wait_prev == NULL); 6499 ASSERT(tcp->tcp_time_wait_expire == 0); 6500 ltcp = NULL; 6501 /* 6502 * If it used to be a listener, check to make sure no one else 6503 * has taken the port before switching back to LISTEN state. 6504 */ 6505 if (tcp->tcp_ipversion == IPV4_VERSION) { 6506 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6507 tcp->tcp_ipha->ipha_src, 6508 tcp->tcp_connp->conn_zoneid, ipst); 6509 if (connp != NULL) 6510 ltcp = connp->conn_tcp; 6511 } else { 6512 /* Allow tcp_bound_if listeners? */ 6513 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6514 &tcp->tcp_ip6h->ip6_src, 0, 6515 tcp->tcp_connp->conn_zoneid, ipst); 6516 if (connp != NULL) 6517 ltcp = connp->conn_tcp; 6518 } 6519 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6520 tcp->tcp_state = TCPS_LISTEN; 6521 } else if (old_state > TCPS_BOUND) { 6522 tcp->tcp_conn_req_max = 0; 6523 tcp->tcp_state = TCPS_BOUND; 6524 } 6525 if (ltcp != NULL) 6526 CONN_DEC_REF(ltcp->tcp_connp); 6527 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6528 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6529 } else if (old_state == TCPS_ESTABLISHED || 6530 old_state == TCPS_CLOSE_WAIT) { 6531 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6532 } 6533 6534 if (tcp->tcp_fused) 6535 tcp_unfuse(tcp); 6536 6537 mutex_enter(&tcp->tcp_eager_lock); 6538 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6539 (tcp->tcp_conn_req_cnt_q != 0)) { 6540 tcp_eager_cleanup(tcp, 0); 6541 } 6542 mutex_exit(&tcp->tcp_eager_lock); 6543 6544 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6545 tcp->tcp_rnxt, TH_RST | TH_ACK); 6546 6547 tcp_reinit(tcp); 6548 6549 return (0); 6550 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6551 return (TBADSEQ); 6552 } 6553 return (0); 6554 } 6555 6556 /* 6557 * Our client hereby directs us to reject the connection request 6558 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6559 * of sending the appropriate RST, not an ICMP error. 6560 */ 6561 static void 6562 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6563 { 6564 t_scalar_t seqnum; 6565 int error; 6566 6567 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6568 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6569 tcp_err_ack(tcp, mp, TPROTO, 0); 6570 return; 6571 } 6572 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6573 error = tcp_disconnect_common(tcp, seqnum); 6574 if (error != 0) 6575 tcp_err_ack(tcp, mp, error, 0); 6576 else { 6577 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6578 /* Send M_FLUSH according to TPI */ 6579 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6580 } 6581 mp = mi_tpi_ok_ack_alloc(mp); 6582 if (mp) 6583 putnext(tcp->tcp_rq, mp); 6584 } 6585 } 6586 6587 /* 6588 * Diagnostic routine used to return a string associated with the tcp state. 6589 * Note that if the caller does not supply a buffer, it will use an internal 6590 * static string. This means that if multiple threads call this function at 6591 * the same time, output can be corrupted... Note also that this function 6592 * does not check the size of the supplied buffer. The caller has to make 6593 * sure that it is big enough. 6594 */ 6595 static char * 6596 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6597 { 6598 char buf1[30]; 6599 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6600 char *buf; 6601 char *cp; 6602 in6_addr_t local, remote; 6603 char local_addrbuf[INET6_ADDRSTRLEN]; 6604 char remote_addrbuf[INET6_ADDRSTRLEN]; 6605 6606 if (sup_buf != NULL) 6607 buf = sup_buf; 6608 else 6609 buf = priv_buf; 6610 6611 if (tcp == NULL) 6612 return ("NULL_TCP"); 6613 switch (tcp->tcp_state) { 6614 case TCPS_CLOSED: 6615 cp = "TCP_CLOSED"; 6616 break; 6617 case TCPS_IDLE: 6618 cp = "TCP_IDLE"; 6619 break; 6620 case TCPS_BOUND: 6621 cp = "TCP_BOUND"; 6622 break; 6623 case TCPS_LISTEN: 6624 cp = "TCP_LISTEN"; 6625 break; 6626 case TCPS_SYN_SENT: 6627 cp = "TCP_SYN_SENT"; 6628 break; 6629 case TCPS_SYN_RCVD: 6630 cp = "TCP_SYN_RCVD"; 6631 break; 6632 case TCPS_ESTABLISHED: 6633 cp = "TCP_ESTABLISHED"; 6634 break; 6635 case TCPS_CLOSE_WAIT: 6636 cp = "TCP_CLOSE_WAIT"; 6637 break; 6638 case TCPS_FIN_WAIT_1: 6639 cp = "TCP_FIN_WAIT_1"; 6640 break; 6641 case TCPS_CLOSING: 6642 cp = "TCP_CLOSING"; 6643 break; 6644 case TCPS_LAST_ACK: 6645 cp = "TCP_LAST_ACK"; 6646 break; 6647 case TCPS_FIN_WAIT_2: 6648 cp = "TCP_FIN_WAIT_2"; 6649 break; 6650 case TCPS_TIME_WAIT: 6651 cp = "TCP_TIME_WAIT"; 6652 break; 6653 default: 6654 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6655 cp = buf1; 6656 break; 6657 } 6658 switch (format) { 6659 case DISP_ADDR_AND_PORT: 6660 if (tcp->tcp_ipversion == IPV4_VERSION) { 6661 /* 6662 * Note that we use the remote address in the tcp_b 6663 * structure. This means that it will print out 6664 * the real destination address, not the next hop's 6665 * address if source routing is used. 6666 */ 6667 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6668 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6669 6670 } else { 6671 local = tcp->tcp_ip_src_v6; 6672 remote = tcp->tcp_remote_v6; 6673 } 6674 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6675 sizeof (local_addrbuf)); 6676 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6677 sizeof (remote_addrbuf)); 6678 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6679 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6680 ntohs(tcp->tcp_fport), cp); 6681 break; 6682 case DISP_PORT_ONLY: 6683 default: 6684 (void) mi_sprintf(buf, "[%u, %u] %s", 6685 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6686 break; 6687 } 6688 6689 return (buf); 6690 } 6691 6692 /* 6693 * Called via squeue to get on to eager's perimeter. It sends a 6694 * TH_RST if eager is in the fanout table. The listener wants the 6695 * eager to disappear either by means of tcp_eager_blowoff() or 6696 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 6697 * called (via squeue) if the eager cannot be inserted in the 6698 * fanout table in tcp_conn_request(). 6699 */ 6700 /* ARGSUSED */ 6701 void 6702 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6703 { 6704 conn_t *econnp = (conn_t *)arg; 6705 tcp_t *eager = econnp->conn_tcp; 6706 tcp_t *listener = eager->tcp_listener; 6707 tcp_stack_t *tcps = eager->tcp_tcps; 6708 6709 /* 6710 * We could be called because listener is closing. Since 6711 * the eager is using listener's queue's, its not safe. 6712 * Better use the default queue just to send the TH_RST 6713 * out. 6714 */ 6715 ASSERT(tcps->tcps_g_q != NULL); 6716 eager->tcp_rq = tcps->tcps_g_q; 6717 eager->tcp_wq = WR(tcps->tcps_g_q); 6718 6719 /* 6720 * An eager's conn_fanout will be NULL if it's a duplicate 6721 * for an existing 4-tuples in the conn fanout table. 6722 * We don't want to send an RST out in such case. 6723 */ 6724 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 6725 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6726 eager, eager->tcp_snxt, 0, TH_RST); 6727 } 6728 6729 /* We are here because listener wants this eager gone */ 6730 if (listener != NULL) { 6731 mutex_enter(&listener->tcp_eager_lock); 6732 tcp_eager_unlink(eager); 6733 if (eager->tcp_tconnind_started) { 6734 /* 6735 * The eager has sent a conn_ind up to the 6736 * listener but listener decides to close 6737 * instead. We need to drop the extra ref 6738 * placed on eager in tcp_rput_data() before 6739 * sending the conn_ind to listener. 6740 */ 6741 CONN_DEC_REF(econnp); 6742 } 6743 mutex_exit(&listener->tcp_eager_lock); 6744 CONN_DEC_REF(listener->tcp_connp); 6745 } 6746 6747 if (eager->tcp_state > TCPS_BOUND) 6748 tcp_close_detached(eager); 6749 } 6750 6751 /* 6752 * Reset any eager connection hanging off this listener marked 6753 * with 'seqnum' and then reclaim it's resources. 6754 */ 6755 static boolean_t 6756 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6757 { 6758 tcp_t *eager; 6759 mblk_t *mp; 6760 tcp_stack_t *tcps = listener->tcp_tcps; 6761 6762 TCP_STAT(tcps, tcp_eager_blowoff_calls); 6763 eager = listener; 6764 mutex_enter(&listener->tcp_eager_lock); 6765 do { 6766 eager = eager->tcp_eager_next_q; 6767 if (eager == NULL) { 6768 mutex_exit(&listener->tcp_eager_lock); 6769 return (B_FALSE); 6770 } 6771 } while (eager->tcp_conn_req_seqnum != seqnum); 6772 6773 if (eager->tcp_closemp_used) { 6774 mutex_exit(&listener->tcp_eager_lock); 6775 return (B_TRUE); 6776 } 6777 eager->tcp_closemp_used = B_TRUE; 6778 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6779 CONN_INC_REF(eager->tcp_connp); 6780 mutex_exit(&listener->tcp_eager_lock); 6781 mp = &eager->tcp_closemp; 6782 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6783 eager->tcp_connp, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF); 6784 return (B_TRUE); 6785 } 6786 6787 /* 6788 * Reset any eager connection hanging off this listener 6789 * and then reclaim it's resources. 6790 */ 6791 static void 6792 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6793 { 6794 tcp_t *eager; 6795 mblk_t *mp; 6796 tcp_stack_t *tcps = listener->tcp_tcps; 6797 6798 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6799 6800 if (!q0_only) { 6801 /* First cleanup q */ 6802 TCP_STAT(tcps, tcp_eager_blowoff_q); 6803 eager = listener->tcp_eager_next_q; 6804 while (eager != NULL) { 6805 if (!eager->tcp_closemp_used) { 6806 eager->tcp_closemp_used = B_TRUE; 6807 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6808 CONN_INC_REF(eager->tcp_connp); 6809 mp = &eager->tcp_closemp; 6810 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6811 tcp_eager_kill, eager->tcp_connp, 6812 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP); 6813 } 6814 eager = eager->tcp_eager_next_q; 6815 } 6816 } 6817 /* Then cleanup q0 */ 6818 TCP_STAT(tcps, tcp_eager_blowoff_q0); 6819 eager = listener->tcp_eager_next_q0; 6820 while (eager != listener) { 6821 if (!eager->tcp_closemp_used) { 6822 eager->tcp_closemp_used = B_TRUE; 6823 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6824 CONN_INC_REF(eager->tcp_connp); 6825 mp = &eager->tcp_closemp; 6826 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6827 tcp_eager_kill, eager->tcp_connp, SQ_FILL, 6828 SQTAG_TCP_EAGER_CLEANUP_Q0); 6829 } 6830 eager = eager->tcp_eager_next_q0; 6831 } 6832 } 6833 6834 /* 6835 * If we are an eager connection hanging off a listener that hasn't 6836 * formally accepted the connection yet, get off his list and blow off 6837 * any data that we have accumulated. 6838 */ 6839 static void 6840 tcp_eager_unlink(tcp_t *tcp) 6841 { 6842 tcp_t *listener = tcp->tcp_listener; 6843 6844 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6845 ASSERT(listener != NULL); 6846 if (tcp->tcp_eager_next_q0 != NULL) { 6847 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6848 6849 /* Remove the eager tcp from q0 */ 6850 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6851 tcp->tcp_eager_prev_q0; 6852 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6853 tcp->tcp_eager_next_q0; 6854 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6855 listener->tcp_conn_req_cnt_q0--; 6856 6857 tcp->tcp_eager_next_q0 = NULL; 6858 tcp->tcp_eager_prev_q0 = NULL; 6859 6860 /* 6861 * Take the eager out, if it is in the list of droppable 6862 * eagers. 6863 */ 6864 MAKE_UNDROPPABLE(tcp); 6865 6866 if (tcp->tcp_syn_rcvd_timeout != 0) { 6867 /* we have timed out before */ 6868 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6869 listener->tcp_syn_rcvd_timeout--; 6870 } 6871 } else { 6872 tcp_t **tcpp = &listener->tcp_eager_next_q; 6873 tcp_t *prev = NULL; 6874 6875 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6876 if (tcpp[0] == tcp) { 6877 if (listener->tcp_eager_last_q == tcp) { 6878 /* 6879 * If we are unlinking the last 6880 * element on the list, adjust 6881 * tail pointer. Set tail pointer 6882 * to nil when list is empty. 6883 */ 6884 ASSERT(tcp->tcp_eager_next_q == NULL); 6885 if (listener->tcp_eager_last_q == 6886 listener->tcp_eager_next_q) { 6887 listener->tcp_eager_last_q = 6888 NULL; 6889 } else { 6890 /* 6891 * We won't get here if there 6892 * is only one eager in the 6893 * list. 6894 */ 6895 ASSERT(prev != NULL); 6896 listener->tcp_eager_last_q = 6897 prev; 6898 } 6899 } 6900 tcpp[0] = tcp->tcp_eager_next_q; 6901 tcp->tcp_eager_next_q = NULL; 6902 tcp->tcp_eager_last_q = NULL; 6903 ASSERT(listener->tcp_conn_req_cnt_q > 0); 6904 listener->tcp_conn_req_cnt_q--; 6905 break; 6906 } 6907 prev = tcpp[0]; 6908 } 6909 } 6910 tcp->tcp_listener = NULL; 6911 } 6912 6913 /* Shorthand to generate and send TPI error acks to our client */ 6914 static void 6915 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 6916 { 6917 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 6918 putnext(tcp->tcp_rq, mp); 6919 } 6920 6921 /* Shorthand to generate and send TPI error acks to our client */ 6922 static void 6923 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 6924 int t_error, int sys_error) 6925 { 6926 struct T_error_ack *teackp; 6927 6928 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 6929 M_PCPROTO, T_ERROR_ACK)) != NULL) { 6930 teackp = (struct T_error_ack *)mp->b_rptr; 6931 teackp->ERROR_prim = primitive; 6932 teackp->TLI_error = t_error; 6933 teackp->UNIX_error = sys_error; 6934 putnext(tcp->tcp_rq, mp); 6935 } 6936 } 6937 6938 /* 6939 * Note: No locks are held when inspecting tcp_g_*epriv_ports 6940 * but instead the code relies on: 6941 * - the fact that the address of the array and its size never changes 6942 * - the atomic assignment of the elements of the array 6943 */ 6944 /* ARGSUSED */ 6945 static int 6946 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 6947 { 6948 int i; 6949 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 6950 6951 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 6952 if (tcps->tcps_g_epriv_ports[i] != 0) 6953 (void) mi_mpprintf(mp, "%d ", 6954 tcps->tcps_g_epriv_ports[i]); 6955 } 6956 return (0); 6957 } 6958 6959 /* 6960 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 6961 * threads from changing it at the same time. 6962 */ 6963 /* ARGSUSED */ 6964 static int 6965 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 6966 cred_t *cr) 6967 { 6968 long new_value; 6969 int i; 6970 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 6971 6972 /* 6973 * Fail the request if the new value does not lie within the 6974 * port number limits. 6975 */ 6976 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 6977 new_value <= 0 || new_value >= 65536) { 6978 return (EINVAL); 6979 } 6980 6981 mutex_enter(&tcps->tcps_epriv_port_lock); 6982 /* Check if the value is already in the list */ 6983 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 6984 if (new_value == tcps->tcps_g_epriv_ports[i]) { 6985 mutex_exit(&tcps->tcps_epriv_port_lock); 6986 return (EEXIST); 6987 } 6988 } 6989 /* Find an empty slot */ 6990 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 6991 if (tcps->tcps_g_epriv_ports[i] == 0) 6992 break; 6993 } 6994 if (i == tcps->tcps_g_num_epriv_ports) { 6995 mutex_exit(&tcps->tcps_epriv_port_lock); 6996 return (EOVERFLOW); 6997 } 6998 /* Set the new value */ 6999 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7000 mutex_exit(&tcps->tcps_epriv_port_lock); 7001 return (0); 7002 } 7003 7004 /* 7005 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7006 * threads from changing it at the same time. 7007 */ 7008 /* ARGSUSED */ 7009 static int 7010 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7011 cred_t *cr) 7012 { 7013 long new_value; 7014 int i; 7015 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7016 7017 /* 7018 * Fail the request if the new value does not lie within the 7019 * port number limits. 7020 */ 7021 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7022 new_value >= 65536) { 7023 return (EINVAL); 7024 } 7025 7026 mutex_enter(&tcps->tcps_epriv_port_lock); 7027 /* Check that the value is already in the list */ 7028 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7029 if (tcps->tcps_g_epriv_ports[i] == new_value) 7030 break; 7031 } 7032 if (i == tcps->tcps_g_num_epriv_ports) { 7033 mutex_exit(&tcps->tcps_epriv_port_lock); 7034 return (ESRCH); 7035 } 7036 /* Clear the value */ 7037 tcps->tcps_g_epriv_ports[i] = 0; 7038 mutex_exit(&tcps->tcps_epriv_port_lock); 7039 return (0); 7040 } 7041 7042 /* Return the TPI/TLI equivalent of our current tcp_state */ 7043 static int 7044 tcp_tpistate(tcp_t *tcp) 7045 { 7046 switch (tcp->tcp_state) { 7047 case TCPS_IDLE: 7048 return (TS_UNBND); 7049 case TCPS_LISTEN: 7050 /* 7051 * Return whether there are outstanding T_CONN_IND waiting 7052 * for the matching T_CONN_RES. Therefore don't count q0. 7053 */ 7054 if (tcp->tcp_conn_req_cnt_q > 0) 7055 return (TS_WRES_CIND); 7056 else 7057 return (TS_IDLE); 7058 case TCPS_BOUND: 7059 return (TS_IDLE); 7060 case TCPS_SYN_SENT: 7061 return (TS_WCON_CREQ); 7062 case TCPS_SYN_RCVD: 7063 /* 7064 * Note: assumption: this has to the active open SYN_RCVD. 7065 * The passive instance is detached in SYN_RCVD stage of 7066 * incoming connection processing so we cannot get request 7067 * for T_info_ack on it. 7068 */ 7069 return (TS_WACK_CRES); 7070 case TCPS_ESTABLISHED: 7071 return (TS_DATA_XFER); 7072 case TCPS_CLOSE_WAIT: 7073 return (TS_WREQ_ORDREL); 7074 case TCPS_FIN_WAIT_1: 7075 return (TS_WIND_ORDREL); 7076 case TCPS_FIN_WAIT_2: 7077 return (TS_WIND_ORDREL); 7078 7079 case TCPS_CLOSING: 7080 case TCPS_LAST_ACK: 7081 case TCPS_TIME_WAIT: 7082 case TCPS_CLOSED: 7083 /* 7084 * Following TS_WACK_DREQ7 is a rendition of "not 7085 * yet TS_IDLE" TPI state. There is no best match to any 7086 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7087 * choose a value chosen that will map to TLI/XTI level 7088 * state of TSTATECHNG (state is process of changing) which 7089 * captures what this dummy state represents. 7090 */ 7091 return (TS_WACK_DREQ7); 7092 default: 7093 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7094 tcp->tcp_state, tcp_display(tcp, NULL, 7095 DISP_PORT_ONLY)); 7096 return (TS_UNBND); 7097 } 7098 } 7099 7100 static void 7101 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7102 { 7103 tcp_stack_t *tcps = tcp->tcp_tcps; 7104 7105 if (tcp->tcp_family == AF_INET6) 7106 *tia = tcp_g_t_info_ack_v6; 7107 else 7108 *tia = tcp_g_t_info_ack; 7109 tia->CURRENT_state = tcp_tpistate(tcp); 7110 tia->OPT_size = tcp_max_optsize; 7111 if (tcp->tcp_mss == 0) { 7112 /* Not yet set - tcp_open does not set mss */ 7113 if (tcp->tcp_ipversion == IPV4_VERSION) 7114 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7115 else 7116 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7117 } else { 7118 tia->TIDU_size = tcp->tcp_mss; 7119 } 7120 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7121 } 7122 7123 static void 7124 tcp_do_capability_ack(tcp_t *tcp, struct T_capability_ack *tcap, 7125 t_uscalar_t cap_bits1) 7126 { 7127 tcap->CAP_bits1 = 0; 7128 7129 if (cap_bits1 & TC1_INFO) { 7130 tcp_copy_info(&tcap->INFO_ack, tcp); 7131 tcap->CAP_bits1 |= TC1_INFO; 7132 } 7133 7134 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7135 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7136 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7137 } 7138 7139 } 7140 7141 /* 7142 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7143 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7144 * tcp_g_t_info_ack. The current state of the stream is copied from 7145 * tcp_state. 7146 */ 7147 static void 7148 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7149 { 7150 t_uscalar_t cap_bits1; 7151 struct T_capability_ack *tcap; 7152 7153 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7154 freemsg(mp); 7155 return; 7156 } 7157 7158 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7159 7160 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7161 mp->b_datap->db_type, T_CAPABILITY_ACK); 7162 if (mp == NULL) 7163 return; 7164 7165 tcap = (struct T_capability_ack *)mp->b_rptr; 7166 tcp_do_capability_ack(tcp, tcap, cap_bits1); 7167 7168 putnext(tcp->tcp_rq, mp); 7169 } 7170 7171 /* 7172 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7173 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7174 * The current state of the stream is copied from tcp_state. 7175 */ 7176 static void 7177 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7178 { 7179 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7180 T_INFO_ACK); 7181 if (!mp) { 7182 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7183 return; 7184 } 7185 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7186 putnext(tcp->tcp_rq, mp); 7187 } 7188 7189 /* Respond to the TPI addr request */ 7190 static void 7191 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7192 { 7193 sin_t *sin; 7194 mblk_t *ackmp; 7195 struct T_addr_ack *taa; 7196 7197 /* Make it large enough for worst case */ 7198 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7199 2 * sizeof (sin6_t), 1); 7200 if (ackmp == NULL) { 7201 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7202 return; 7203 } 7204 7205 if (tcp->tcp_ipversion == IPV6_VERSION) { 7206 tcp_addr_req_ipv6(tcp, ackmp); 7207 return; 7208 } 7209 taa = (struct T_addr_ack *)ackmp->b_rptr; 7210 7211 bzero(taa, sizeof (struct T_addr_ack)); 7212 ackmp->b_wptr = (uchar_t *)&taa[1]; 7213 7214 taa->PRIM_type = T_ADDR_ACK; 7215 ackmp->b_datap->db_type = M_PCPROTO; 7216 7217 /* 7218 * Note: Following code assumes 32 bit alignment of basic 7219 * data structures like sin_t and struct T_addr_ack. 7220 */ 7221 if (tcp->tcp_state >= TCPS_BOUND) { 7222 /* 7223 * Fill in local address 7224 */ 7225 taa->LOCADDR_length = sizeof (sin_t); 7226 taa->LOCADDR_offset = sizeof (*taa); 7227 7228 sin = (sin_t *)&taa[1]; 7229 7230 /* Fill zeroes and then intialize non-zero fields */ 7231 *sin = sin_null; 7232 7233 sin->sin_family = AF_INET; 7234 7235 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7236 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7237 7238 ackmp->b_wptr = (uchar_t *)&sin[1]; 7239 7240 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7241 /* 7242 * Fill in Remote address 7243 */ 7244 taa->REMADDR_length = sizeof (sin_t); 7245 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7246 taa->LOCADDR_length); 7247 7248 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7249 *sin = sin_null; 7250 sin->sin_family = AF_INET; 7251 sin->sin_addr.s_addr = tcp->tcp_remote; 7252 sin->sin_port = tcp->tcp_fport; 7253 7254 ackmp->b_wptr = (uchar_t *)&sin[1]; 7255 } 7256 } 7257 putnext(tcp->tcp_rq, ackmp); 7258 } 7259 7260 /* Assumes that tcp_addr_req gets enough space and alignment */ 7261 static void 7262 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7263 { 7264 sin6_t *sin6; 7265 struct T_addr_ack *taa; 7266 7267 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7268 ASSERT(OK_32PTR(ackmp->b_rptr)); 7269 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7270 2 * sizeof (sin6_t)); 7271 7272 taa = (struct T_addr_ack *)ackmp->b_rptr; 7273 7274 bzero(taa, sizeof (struct T_addr_ack)); 7275 ackmp->b_wptr = (uchar_t *)&taa[1]; 7276 7277 taa->PRIM_type = T_ADDR_ACK; 7278 ackmp->b_datap->db_type = M_PCPROTO; 7279 7280 /* 7281 * Note: Following code assumes 32 bit alignment of basic 7282 * data structures like sin6_t and struct T_addr_ack. 7283 */ 7284 if (tcp->tcp_state >= TCPS_BOUND) { 7285 /* 7286 * Fill in local address 7287 */ 7288 taa->LOCADDR_length = sizeof (sin6_t); 7289 taa->LOCADDR_offset = sizeof (*taa); 7290 7291 sin6 = (sin6_t *)&taa[1]; 7292 *sin6 = sin6_null; 7293 7294 sin6->sin6_family = AF_INET6; 7295 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7296 sin6->sin6_port = tcp->tcp_lport; 7297 7298 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7299 7300 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7301 /* 7302 * Fill in Remote address 7303 */ 7304 taa->REMADDR_length = sizeof (sin6_t); 7305 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7306 taa->LOCADDR_length); 7307 7308 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7309 *sin6 = sin6_null; 7310 sin6->sin6_family = AF_INET6; 7311 sin6->sin6_flowinfo = 7312 tcp->tcp_ip6h->ip6_vcf & 7313 ~IPV6_VERS_AND_FLOW_MASK; 7314 sin6->sin6_addr = tcp->tcp_remote_v6; 7315 sin6->sin6_port = tcp->tcp_fport; 7316 7317 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7318 } 7319 } 7320 putnext(tcp->tcp_rq, ackmp); 7321 } 7322 7323 /* 7324 * Handle reinitialization of a tcp structure. 7325 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7326 */ 7327 static void 7328 tcp_reinit(tcp_t *tcp) 7329 { 7330 mblk_t *mp; 7331 int err; 7332 tcp_stack_t *tcps = tcp->tcp_tcps; 7333 7334 TCP_STAT(tcps, tcp_reinit_calls); 7335 7336 /* tcp_reinit should never be called for detached tcp_t's */ 7337 ASSERT(tcp->tcp_listener == NULL); 7338 ASSERT((tcp->tcp_family == AF_INET && 7339 tcp->tcp_ipversion == IPV4_VERSION) || 7340 (tcp->tcp_family == AF_INET6 && 7341 (tcp->tcp_ipversion == IPV4_VERSION || 7342 tcp->tcp_ipversion == IPV6_VERSION))); 7343 7344 /* Cancel outstanding timers */ 7345 tcp_timers_stop(tcp); 7346 7347 /* 7348 * Reset everything in the state vector, after updating global 7349 * MIB data from instance counters. 7350 */ 7351 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7352 tcp->tcp_ibsegs = 0; 7353 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7354 tcp->tcp_obsegs = 0; 7355 7356 tcp_close_mpp(&tcp->tcp_xmit_head); 7357 if (tcp->tcp_snd_zcopy_aware) 7358 tcp_zcopy_notify(tcp); 7359 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7360 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7361 mutex_enter(&tcp->tcp_non_sq_lock); 7362 if (tcp->tcp_flow_stopped && 7363 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7364 tcp_clrqfull(tcp); 7365 } 7366 mutex_exit(&tcp->tcp_non_sq_lock); 7367 tcp_close_mpp(&tcp->tcp_reass_head); 7368 tcp->tcp_reass_tail = NULL; 7369 if (tcp->tcp_rcv_list != NULL) { 7370 /* Free b_next chain */ 7371 tcp_close_mpp(&tcp->tcp_rcv_list); 7372 tcp->tcp_rcv_last_head = NULL; 7373 tcp->tcp_rcv_last_tail = NULL; 7374 tcp->tcp_rcv_cnt = 0; 7375 } 7376 tcp->tcp_rcv_last_tail = NULL; 7377 7378 if ((mp = tcp->tcp_urp_mp) != NULL) { 7379 freemsg(mp); 7380 tcp->tcp_urp_mp = NULL; 7381 } 7382 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7383 freemsg(mp); 7384 tcp->tcp_urp_mark_mp = NULL; 7385 } 7386 if (tcp->tcp_fused_sigurg_mp != NULL) { 7387 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7388 freeb(tcp->tcp_fused_sigurg_mp); 7389 tcp->tcp_fused_sigurg_mp = NULL; 7390 } 7391 if (tcp->tcp_ordrel_mp != NULL) { 7392 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7393 freeb(tcp->tcp_ordrel_mp); 7394 tcp->tcp_ordrel_mp = NULL; 7395 } 7396 7397 /* 7398 * Following is a union with two members which are 7399 * identical types and size so the following cleanup 7400 * is enough. 7401 */ 7402 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7403 7404 CL_INET_DISCONNECT(tcp->tcp_connp, tcp); 7405 7406 /* 7407 * The connection can't be on the tcp_time_wait_head list 7408 * since it is not detached. 7409 */ 7410 ASSERT(tcp->tcp_time_wait_next == NULL); 7411 ASSERT(tcp->tcp_time_wait_prev == NULL); 7412 ASSERT(tcp->tcp_time_wait_expire == 0); 7413 7414 if (tcp->tcp_kssl_pending) { 7415 tcp->tcp_kssl_pending = B_FALSE; 7416 7417 /* Don't reset if the initialized by bind. */ 7418 if (tcp->tcp_kssl_ent != NULL) { 7419 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7420 KSSL_NO_PROXY); 7421 } 7422 } 7423 if (tcp->tcp_kssl_ctx != NULL) { 7424 kssl_release_ctx(tcp->tcp_kssl_ctx); 7425 tcp->tcp_kssl_ctx = NULL; 7426 } 7427 7428 /* 7429 * Reset/preserve other values 7430 */ 7431 tcp_reinit_values(tcp); 7432 ipcl_hash_remove(tcp->tcp_connp); 7433 conn_delete_ire(tcp->tcp_connp, NULL); 7434 tcp_ipsec_cleanup(tcp); 7435 7436 if (tcp->tcp_conn_req_max != 0) { 7437 /* 7438 * This is the case when a TLI program uses the same 7439 * transport end point to accept a connection. This 7440 * makes the TCP both a listener and acceptor. When 7441 * this connection is closed, we need to set the state 7442 * back to TCPS_LISTEN. Make sure that the eager list 7443 * is reinitialized. 7444 * 7445 * Note that this stream is still bound to the four 7446 * tuples of the previous connection in IP. If a new 7447 * SYN with different foreign address comes in, IP will 7448 * not find it and will send it to the global queue. In 7449 * the global queue, TCP will do a tcp_lookup_listener() 7450 * to find this stream. This works because this stream 7451 * is only removed from connected hash. 7452 * 7453 */ 7454 tcp->tcp_state = TCPS_LISTEN; 7455 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7456 tcp->tcp_eager_next_drop_q0 = tcp; 7457 tcp->tcp_eager_prev_drop_q0 = tcp; 7458 tcp->tcp_connp->conn_recv = tcp_conn_request; 7459 if (tcp->tcp_family == AF_INET6) { 7460 ASSERT(tcp->tcp_connp->conn_af_isv6); 7461 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7462 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7463 } else { 7464 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7465 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7466 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7467 } 7468 } else { 7469 tcp->tcp_state = TCPS_BOUND; 7470 } 7471 7472 /* 7473 * Initialize to default values 7474 * Can't fail since enough header template space already allocated 7475 * at open(). 7476 */ 7477 err = tcp_init_values(tcp); 7478 ASSERT(err == 0); 7479 /* Restore state in tcp_tcph */ 7480 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7481 if (tcp->tcp_ipversion == IPV4_VERSION) 7482 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7483 else 7484 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7485 /* 7486 * Copy of the src addr. in tcp_t is needed in tcp_t 7487 * since the lookup funcs can only lookup on tcp_t 7488 */ 7489 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7490 7491 ASSERT(tcp->tcp_ptpbhn != NULL); 7492 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 7493 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7494 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 7495 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 7496 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7497 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7498 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7499 } 7500 7501 /* 7502 * Force values to zero that need be zero. 7503 * Do not touch values asociated with the BOUND or LISTEN state 7504 * since the connection will end up in that state after the reinit. 7505 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7506 * structure! 7507 */ 7508 static void 7509 tcp_reinit_values(tcp) 7510 tcp_t *tcp; 7511 { 7512 tcp_stack_t *tcps = tcp->tcp_tcps; 7513 7514 #ifndef lint 7515 #define DONTCARE(x) 7516 #define PRESERVE(x) 7517 #else 7518 #define DONTCARE(x) ((x) = (x)) 7519 #define PRESERVE(x) ((x) = (x)) 7520 #endif /* lint */ 7521 7522 PRESERVE(tcp->tcp_bind_hash_port); 7523 PRESERVE(tcp->tcp_bind_hash); 7524 PRESERVE(tcp->tcp_ptpbhn); 7525 PRESERVE(tcp->tcp_acceptor_hash); 7526 PRESERVE(tcp->tcp_ptpahn); 7527 7528 /* Should be ASSERT NULL on these with new code! */ 7529 ASSERT(tcp->tcp_time_wait_next == NULL); 7530 ASSERT(tcp->tcp_time_wait_prev == NULL); 7531 ASSERT(tcp->tcp_time_wait_expire == 0); 7532 PRESERVE(tcp->tcp_state); 7533 PRESERVE(tcp->tcp_rq); 7534 PRESERVE(tcp->tcp_wq); 7535 7536 ASSERT(tcp->tcp_xmit_head == NULL); 7537 ASSERT(tcp->tcp_xmit_last == NULL); 7538 ASSERT(tcp->tcp_unsent == 0); 7539 ASSERT(tcp->tcp_xmit_tail == NULL); 7540 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7541 7542 tcp->tcp_snxt = 0; /* Displayed in mib */ 7543 tcp->tcp_suna = 0; /* Displayed in mib */ 7544 tcp->tcp_swnd = 0; 7545 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7546 7547 ASSERT(tcp->tcp_ibsegs == 0); 7548 ASSERT(tcp->tcp_obsegs == 0); 7549 7550 if (tcp->tcp_iphc != NULL) { 7551 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7552 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7553 } 7554 7555 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7556 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7557 DONTCARE(tcp->tcp_ipha); 7558 DONTCARE(tcp->tcp_ip6h); 7559 DONTCARE(tcp->tcp_ip_hdr_len); 7560 DONTCARE(tcp->tcp_tcph); 7561 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7562 tcp->tcp_valid_bits = 0; 7563 7564 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7565 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7566 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7567 tcp->tcp_last_rcv_lbolt = 0; 7568 7569 tcp->tcp_init_cwnd = 0; 7570 7571 tcp->tcp_urp_last_valid = 0; 7572 tcp->tcp_hard_binding = 0; 7573 tcp->tcp_hard_bound = 0; 7574 PRESERVE(tcp->tcp_cred); 7575 PRESERVE(tcp->tcp_cpid); 7576 PRESERVE(tcp->tcp_open_time); 7577 PRESERVE(tcp->tcp_exclbind); 7578 7579 tcp->tcp_fin_acked = 0; 7580 tcp->tcp_fin_rcvd = 0; 7581 tcp->tcp_fin_sent = 0; 7582 tcp->tcp_ordrel_done = 0; 7583 7584 tcp->tcp_debug = 0; 7585 tcp->tcp_dontroute = 0; 7586 tcp->tcp_broadcast = 0; 7587 7588 tcp->tcp_useloopback = 0; 7589 tcp->tcp_reuseaddr = 0; 7590 tcp->tcp_oobinline = 0; 7591 tcp->tcp_dgram_errind = 0; 7592 7593 tcp->tcp_detached = 0; 7594 tcp->tcp_bind_pending = 0; 7595 tcp->tcp_unbind_pending = 0; 7596 7597 tcp->tcp_snd_ws_ok = B_FALSE; 7598 tcp->tcp_snd_ts_ok = B_FALSE; 7599 tcp->tcp_linger = 0; 7600 tcp->tcp_ka_enabled = 0; 7601 tcp->tcp_zero_win_probe = 0; 7602 7603 tcp->tcp_loopback = 0; 7604 tcp->tcp_refuse = 0; 7605 tcp->tcp_localnet = 0; 7606 tcp->tcp_syn_defense = 0; 7607 tcp->tcp_set_timer = 0; 7608 7609 tcp->tcp_active_open = 0; 7610 tcp->tcp_rexmit = B_FALSE; 7611 tcp->tcp_xmit_zc_clean = B_FALSE; 7612 7613 tcp->tcp_snd_sack_ok = B_FALSE; 7614 PRESERVE(tcp->tcp_recvdstaddr); 7615 tcp->tcp_hwcksum = B_FALSE; 7616 7617 tcp->tcp_ire_ill_check_done = B_FALSE; 7618 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7619 7620 tcp->tcp_mdt = B_FALSE; 7621 tcp->tcp_mdt_hdr_head = 0; 7622 tcp->tcp_mdt_hdr_tail = 0; 7623 7624 tcp->tcp_conn_def_q0 = 0; 7625 tcp->tcp_ip_forward_progress = B_FALSE; 7626 tcp->tcp_anon_priv_bind = 0; 7627 tcp->tcp_ecn_ok = B_FALSE; 7628 7629 tcp->tcp_cwr = B_FALSE; 7630 tcp->tcp_ecn_echo_on = B_FALSE; 7631 7632 if (tcp->tcp_sack_info != NULL) { 7633 if (tcp->tcp_notsack_list != NULL) { 7634 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7635 } 7636 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7637 tcp->tcp_sack_info = NULL; 7638 } 7639 7640 tcp->tcp_rcv_ws = 0; 7641 tcp->tcp_snd_ws = 0; 7642 tcp->tcp_ts_recent = 0; 7643 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7644 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7645 tcp->tcp_if_mtu = 0; 7646 7647 ASSERT(tcp->tcp_reass_head == NULL); 7648 ASSERT(tcp->tcp_reass_tail == NULL); 7649 7650 tcp->tcp_cwnd_cnt = 0; 7651 7652 ASSERT(tcp->tcp_rcv_list == NULL); 7653 ASSERT(tcp->tcp_rcv_last_head == NULL); 7654 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7655 ASSERT(tcp->tcp_rcv_cnt == 0); 7656 7657 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7658 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7659 tcp->tcp_csuna = 0; 7660 7661 tcp->tcp_rto = 0; /* Displayed in MIB */ 7662 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7663 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7664 tcp->tcp_rtt_update = 0; 7665 7666 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7667 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7668 7669 tcp->tcp_rack = 0; /* Displayed in mib */ 7670 tcp->tcp_rack_cnt = 0; 7671 tcp->tcp_rack_cur_max = 0; 7672 tcp->tcp_rack_abs_max = 0; 7673 7674 tcp->tcp_max_swnd = 0; 7675 7676 ASSERT(tcp->tcp_listener == NULL); 7677 7678 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7679 7680 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7681 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7682 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7683 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7684 7685 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7686 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7687 PRESERVE(tcp->tcp_conn_req_max); 7688 PRESERVE(tcp->tcp_conn_req_seqnum); 7689 7690 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7691 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7692 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7693 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7694 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7695 7696 tcp->tcp_lingertime = 0; 7697 7698 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7699 ASSERT(tcp->tcp_urp_mp == NULL); 7700 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7701 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7702 7703 ASSERT(tcp->tcp_eager_next_q == NULL); 7704 ASSERT(tcp->tcp_eager_last_q == NULL); 7705 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7706 tcp->tcp_eager_prev_q0 == NULL) || 7707 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7708 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7709 7710 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 7711 tcp->tcp_eager_prev_drop_q0 == NULL) || 7712 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 7713 7714 tcp->tcp_client_errno = 0; 7715 7716 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7717 7718 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7719 7720 PRESERVE(tcp->tcp_bound_source_v6); 7721 tcp->tcp_last_sent_len = 0; 7722 tcp->tcp_dupack_cnt = 0; 7723 7724 tcp->tcp_fport = 0; /* Displayed in MIB */ 7725 PRESERVE(tcp->tcp_lport); 7726 7727 PRESERVE(tcp->tcp_acceptor_lockp); 7728 7729 ASSERT(tcp->tcp_ordrel_mp == NULL); 7730 PRESERVE(tcp->tcp_acceptor_id); 7731 DONTCARE(tcp->tcp_ipsec_overhead); 7732 7733 PRESERVE(tcp->tcp_family); 7734 if (tcp->tcp_family == AF_INET6) { 7735 tcp->tcp_ipversion = IPV6_VERSION; 7736 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 7737 } else { 7738 tcp->tcp_ipversion = IPV4_VERSION; 7739 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 7740 } 7741 7742 tcp->tcp_bound_if = 0; 7743 tcp->tcp_ipv6_recvancillary = 0; 7744 tcp->tcp_recvifindex = 0; 7745 tcp->tcp_recvhops = 0; 7746 tcp->tcp_closed = 0; 7747 tcp->tcp_cleandeathtag = 0; 7748 if (tcp->tcp_hopopts != NULL) { 7749 mi_free(tcp->tcp_hopopts); 7750 tcp->tcp_hopopts = NULL; 7751 tcp->tcp_hopoptslen = 0; 7752 } 7753 ASSERT(tcp->tcp_hopoptslen == 0); 7754 if (tcp->tcp_dstopts != NULL) { 7755 mi_free(tcp->tcp_dstopts); 7756 tcp->tcp_dstopts = NULL; 7757 tcp->tcp_dstoptslen = 0; 7758 } 7759 ASSERT(tcp->tcp_dstoptslen == 0); 7760 if (tcp->tcp_rtdstopts != NULL) { 7761 mi_free(tcp->tcp_rtdstopts); 7762 tcp->tcp_rtdstopts = NULL; 7763 tcp->tcp_rtdstoptslen = 0; 7764 } 7765 ASSERT(tcp->tcp_rtdstoptslen == 0); 7766 if (tcp->tcp_rthdr != NULL) { 7767 mi_free(tcp->tcp_rthdr); 7768 tcp->tcp_rthdr = NULL; 7769 tcp->tcp_rthdrlen = 0; 7770 } 7771 ASSERT(tcp->tcp_rthdrlen == 0); 7772 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7773 7774 /* Reset fusion-related fields */ 7775 tcp->tcp_fused = B_FALSE; 7776 tcp->tcp_unfusable = B_FALSE; 7777 tcp->tcp_fused_sigurg = B_FALSE; 7778 tcp->tcp_direct_sockfs = B_FALSE; 7779 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7780 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7781 tcp->tcp_loopback_peer = NULL; 7782 tcp->tcp_fuse_rcv_hiwater = 0; 7783 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7784 tcp->tcp_fuse_rcv_unread_cnt = 0; 7785 7786 tcp->tcp_lso = B_FALSE; 7787 7788 tcp->tcp_in_ack_unsent = 0; 7789 tcp->tcp_cork = B_FALSE; 7790 tcp->tcp_tconnind_started = B_FALSE; 7791 7792 PRESERVE(tcp->tcp_squeue_bytes); 7793 7794 ASSERT(tcp->tcp_kssl_ctx == NULL); 7795 ASSERT(!tcp->tcp_kssl_pending); 7796 PRESERVE(tcp->tcp_kssl_ent); 7797 7798 tcp->tcp_closemp_used = B_FALSE; 7799 7800 PRESERVE(tcp->tcp_rsrv_mp); 7801 PRESERVE(tcp->tcp_rsrv_mp_lock); 7802 7803 #ifdef DEBUG 7804 DONTCARE(tcp->tcmp_stk[0]); 7805 #endif 7806 7807 PRESERVE(tcp->tcp_connid); 7808 7809 7810 #undef DONTCARE 7811 #undef PRESERVE 7812 } 7813 7814 /* 7815 * Allocate necessary resources and initialize state vector. 7816 * Guaranteed not to fail so that when an error is returned, 7817 * the caller doesn't need to do any additional cleanup. 7818 */ 7819 int 7820 tcp_init(tcp_t *tcp, queue_t *q) 7821 { 7822 int err; 7823 7824 tcp->tcp_rq = q; 7825 tcp->tcp_wq = WR(q); 7826 tcp->tcp_state = TCPS_IDLE; 7827 if ((err = tcp_init_values(tcp)) != 0) 7828 tcp_timers_stop(tcp); 7829 return (err); 7830 } 7831 7832 static int 7833 tcp_init_values(tcp_t *tcp) 7834 { 7835 int err; 7836 tcp_stack_t *tcps = tcp->tcp_tcps; 7837 7838 ASSERT((tcp->tcp_family == AF_INET && 7839 tcp->tcp_ipversion == IPV4_VERSION) || 7840 (tcp->tcp_family == AF_INET6 && 7841 (tcp->tcp_ipversion == IPV4_VERSION || 7842 tcp->tcp_ipversion == IPV6_VERSION))); 7843 7844 /* 7845 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7846 * will be close to tcp_rexmit_interval_initial. By doing this, we 7847 * allow the algorithm to adjust slowly to large fluctuations of RTT 7848 * during first few transmissions of a connection as seen in slow 7849 * links. 7850 */ 7851 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 7852 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 7853 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7854 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7855 tcps->tcps_conn_grace_period; 7856 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 7857 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 7858 tcp->tcp_timer_backoff = 0; 7859 tcp->tcp_ms_we_have_waited = 0; 7860 tcp->tcp_last_recv_time = lbolt; 7861 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 7862 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7863 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7864 7865 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 7866 7867 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 7868 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 7869 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 7870 /* 7871 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7872 * passive open. 7873 */ 7874 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 7875 7876 tcp->tcp_naglim = tcps->tcps_naglim_def; 7877 7878 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7879 7880 tcp->tcp_mdt_hdr_head = 0; 7881 tcp->tcp_mdt_hdr_tail = 0; 7882 7883 /* Reset fusion-related fields */ 7884 tcp->tcp_fused = B_FALSE; 7885 tcp->tcp_unfusable = B_FALSE; 7886 tcp->tcp_fused_sigurg = B_FALSE; 7887 tcp->tcp_direct_sockfs = B_FALSE; 7888 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7889 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7890 tcp->tcp_loopback_peer = NULL; 7891 tcp->tcp_fuse_rcv_hiwater = 0; 7892 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7893 tcp->tcp_fuse_rcv_unread_cnt = 0; 7894 7895 /* Initialize the header template */ 7896 if (tcp->tcp_ipversion == IPV4_VERSION) { 7897 err = tcp_header_init_ipv4(tcp); 7898 } else { 7899 err = tcp_header_init_ipv6(tcp); 7900 } 7901 if (err) 7902 return (err); 7903 7904 /* 7905 * Init the window scale to the max so tcp_rwnd_set() won't pare 7906 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 7907 */ 7908 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 7909 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 7910 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 7911 7912 tcp->tcp_cork = B_FALSE; 7913 /* 7914 * Init the tcp_debug option. This value determines whether TCP 7915 * calls strlog() to print out debug messages. Doing this 7916 * initialization here means that this value is not inherited thru 7917 * tcp_reinit(). 7918 */ 7919 tcp->tcp_debug = tcps->tcps_dbg; 7920 7921 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 7922 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 7923 7924 return (0); 7925 } 7926 7927 /* 7928 * Initialize the IPv4 header. Loses any record of any IP options. 7929 */ 7930 static int 7931 tcp_header_init_ipv4(tcp_t *tcp) 7932 { 7933 tcph_t *tcph; 7934 uint32_t sum; 7935 conn_t *connp; 7936 tcp_stack_t *tcps = tcp->tcp_tcps; 7937 7938 /* 7939 * This is a simple initialization. If there's 7940 * already a template, it should never be too small, 7941 * so reuse it. Otherwise, allocate space for the new one. 7942 */ 7943 if (tcp->tcp_iphc == NULL) { 7944 ASSERT(tcp->tcp_iphc_len == 0); 7945 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 7946 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 7947 if (tcp->tcp_iphc == NULL) { 7948 tcp->tcp_iphc_len = 0; 7949 return (ENOMEM); 7950 } 7951 } 7952 7953 /* options are gone; may need a new label */ 7954 connp = tcp->tcp_connp; 7955 connp->conn_mlp_type = mlptSingle; 7956 connp->conn_ulp_labeled = !is_system_labeled(); 7957 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7958 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 7959 tcp->tcp_ip6h = NULL; 7960 tcp->tcp_ipversion = IPV4_VERSION; 7961 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 7962 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 7963 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 7964 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 7965 tcp->tcp_ipha->ipha_version_and_hdr_length 7966 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 7967 tcp->tcp_ipha->ipha_ident = 0; 7968 7969 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 7970 tcp->tcp_tos = 0; 7971 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 7972 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 7973 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 7974 7975 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 7976 tcp->tcp_tcph = tcph; 7977 tcph->th_offset_and_rsrvd[0] = (5 << 4); 7978 /* 7979 * IP wants our header length in the checksum field to 7980 * allow it to perform a single pseudo-header+checksum 7981 * calculation on behalf of TCP. 7982 * Include the adjustment for a source route once IP_OPTIONS is set. 7983 */ 7984 sum = sizeof (tcph_t) + tcp->tcp_sum; 7985 sum = (sum >> 16) + (sum & 0xFFFF); 7986 U16_TO_ABE16(sum, tcph->th_sum); 7987 return (0); 7988 } 7989 7990 /* 7991 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 7992 */ 7993 static int 7994 tcp_header_init_ipv6(tcp_t *tcp) 7995 { 7996 tcph_t *tcph; 7997 uint32_t sum; 7998 conn_t *connp; 7999 tcp_stack_t *tcps = tcp->tcp_tcps; 8000 8001 /* 8002 * This is a simple initialization. If there's 8003 * already a template, it should never be too small, 8004 * so reuse it. Otherwise, allocate space for the new one. 8005 * Ensure that there is enough space to "downgrade" the tcp_t 8006 * to an IPv4 tcp_t. This requires having space for a full load 8007 * of IPv4 options, as well as a full load of TCP options 8008 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8009 * than a v6 header and a TCP header with a full load of TCP options 8010 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8011 * We want to avoid reallocation in the "downgraded" case when 8012 * processing outbound IPv4 options. 8013 */ 8014 if (tcp->tcp_iphc == NULL) { 8015 ASSERT(tcp->tcp_iphc_len == 0); 8016 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8017 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8018 if (tcp->tcp_iphc == NULL) { 8019 tcp->tcp_iphc_len = 0; 8020 return (ENOMEM); 8021 } 8022 } 8023 8024 /* options are gone; may need a new label */ 8025 connp = tcp->tcp_connp; 8026 connp->conn_mlp_type = mlptSingle; 8027 connp->conn_ulp_labeled = !is_system_labeled(); 8028 8029 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8030 tcp->tcp_ipversion = IPV6_VERSION; 8031 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8032 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8033 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8034 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8035 tcp->tcp_ipha = NULL; 8036 8037 /* Initialize the header template */ 8038 8039 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8040 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8041 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8042 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8043 8044 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8045 tcp->tcp_tcph = tcph; 8046 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8047 /* 8048 * IP wants our header length in the checksum field to 8049 * allow it to perform a single psuedo-header+checksum 8050 * calculation on behalf of TCP. 8051 * Include the adjustment for a source route when IPV6_RTHDR is set. 8052 */ 8053 sum = sizeof (tcph_t) + tcp->tcp_sum; 8054 sum = (sum >> 16) + (sum & 0xFFFF); 8055 U16_TO_ABE16(sum, tcph->th_sum); 8056 return (0); 8057 } 8058 8059 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8060 #define ICMP_MIN_TCP_HDR 8 8061 8062 /* 8063 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8064 * passed up by IP. The message is always received on the correct tcp_t. 8065 * Assumes that IP has pulled up everything up to and including the ICMP header. 8066 */ 8067 void 8068 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8069 { 8070 icmph_t *icmph; 8071 ipha_t *ipha; 8072 int iph_hdr_length; 8073 tcph_t *tcph; 8074 boolean_t ipsec_mctl = B_FALSE; 8075 boolean_t secure; 8076 mblk_t *first_mp = mp; 8077 int32_t new_mss; 8078 uint32_t ratio; 8079 size_t mp_size = MBLKL(mp); 8080 uint32_t seg_seq; 8081 tcp_stack_t *tcps = tcp->tcp_tcps; 8082 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8083 8084 /* Assume IP provides aligned packets - otherwise toss */ 8085 if (!OK_32PTR(mp->b_rptr)) { 8086 freemsg(mp); 8087 return; 8088 } 8089 8090 /* 8091 * Since ICMP errors are normal data marked with M_CTL when sent 8092 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8093 * packets starting with an ipsec_info_t, see ipsec_info.h. 8094 */ 8095 if ((mp_size == sizeof (ipsec_info_t)) && 8096 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8097 ASSERT(mp->b_cont != NULL); 8098 mp = mp->b_cont; 8099 /* IP should have done this */ 8100 ASSERT(OK_32PTR(mp->b_rptr)); 8101 mp_size = MBLKL(mp); 8102 ipsec_mctl = B_TRUE; 8103 } 8104 8105 /* 8106 * Verify that we have a complete outer IP header. If not, drop it. 8107 */ 8108 if (mp_size < sizeof (ipha_t)) { 8109 noticmpv4: 8110 freemsg(first_mp); 8111 return; 8112 } 8113 8114 ipha = (ipha_t *)mp->b_rptr; 8115 /* 8116 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8117 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8118 */ 8119 switch (IPH_HDR_VERSION(ipha)) { 8120 case IPV6_VERSION: 8121 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8122 return; 8123 case IPV4_VERSION: 8124 break; 8125 default: 8126 goto noticmpv4; 8127 } 8128 8129 /* Skip past the outer IP and ICMP headers */ 8130 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8131 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8132 /* 8133 * If we don't have the correct outer IP header length or if the ULP 8134 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8135 * send it upstream. 8136 */ 8137 if (iph_hdr_length < sizeof (ipha_t) || 8138 ipha->ipha_protocol != IPPROTO_ICMP || 8139 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8140 goto noticmpv4; 8141 } 8142 ipha = (ipha_t *)&icmph[1]; 8143 8144 /* Skip past the inner IP and find the ULP header */ 8145 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8146 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8147 /* 8148 * If we don't have the correct inner IP header length or if the ULP 8149 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8150 * bytes of TCP header, drop it. 8151 */ 8152 if (iph_hdr_length < sizeof (ipha_t) || 8153 ipha->ipha_protocol != IPPROTO_TCP || 8154 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8155 goto noticmpv4; 8156 } 8157 8158 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8159 if (ipsec_mctl) { 8160 secure = ipsec_in_is_secure(first_mp); 8161 } else { 8162 secure = B_FALSE; 8163 } 8164 if (secure) { 8165 /* 8166 * If we are willing to accept this in clear 8167 * we don't have to verify policy. 8168 */ 8169 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8170 if (!tcp_check_policy(tcp, first_mp, 8171 ipha, NULL, secure, ipsec_mctl)) { 8172 /* 8173 * tcp_check_policy called 8174 * ip_drop_packet() on failure. 8175 */ 8176 return; 8177 } 8178 } 8179 } 8180 } else if (ipsec_mctl) { 8181 /* 8182 * This is a hard_bound connection. IP has already 8183 * verified policy. We don't have to do it again. 8184 */ 8185 freeb(first_mp); 8186 first_mp = mp; 8187 ipsec_mctl = B_FALSE; 8188 } 8189 8190 seg_seq = ABE32_TO_U32(tcph->th_seq); 8191 /* 8192 * TCP SHOULD check that the TCP sequence number contained in 8193 * payload of the ICMP error message is within the range 8194 * SND.UNA <= SEG.SEQ < SND.NXT. 8195 */ 8196 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8197 /* 8198 * The ICMP message is bogus, just drop it. But if this is 8199 * an ICMP too big message, IP has already changed 8200 * the ire_max_frag to the bogus value. We need to change 8201 * it back. 8202 */ 8203 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8204 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8205 conn_t *connp = tcp->tcp_connp; 8206 ire_t *ire; 8207 int flag; 8208 8209 if (tcp->tcp_ipversion == IPV4_VERSION) { 8210 flag = tcp->tcp_ipha-> 8211 ipha_fragment_offset_and_flags; 8212 } else { 8213 flag = 0; 8214 } 8215 mutex_enter(&connp->conn_lock); 8216 if ((ire = connp->conn_ire_cache) != NULL) { 8217 mutex_enter(&ire->ire_lock); 8218 mutex_exit(&connp->conn_lock); 8219 ire->ire_max_frag = tcp->tcp_if_mtu; 8220 ire->ire_frag_flag |= flag; 8221 mutex_exit(&ire->ire_lock); 8222 } else { 8223 mutex_exit(&connp->conn_lock); 8224 } 8225 } 8226 goto noticmpv4; 8227 } 8228 8229 switch (icmph->icmph_type) { 8230 case ICMP_DEST_UNREACHABLE: 8231 switch (icmph->icmph_code) { 8232 case ICMP_FRAGMENTATION_NEEDED: 8233 /* 8234 * Reduce the MSS based on the new MTU. This will 8235 * eliminate any fragmentation locally. 8236 * N.B. There may well be some funny side-effects on 8237 * the local send policy and the remote receive policy. 8238 * Pending further research, we provide 8239 * tcp_ignore_path_mtu just in case this proves 8240 * disastrous somewhere. 8241 * 8242 * After updating the MSS, retransmit part of the 8243 * dropped segment using the new mss by calling 8244 * tcp_wput_data(). Need to adjust all those 8245 * params to make sure tcp_wput_data() work properly. 8246 */ 8247 if (tcps->tcps_ignore_path_mtu || 8248 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8249 break; 8250 8251 /* 8252 * Decrease the MSS by time stamp options 8253 * IP options and IPSEC options. tcp_hdr_len 8254 * includes time stamp option and IP option 8255 * length. Note that new_mss may be negative 8256 * if tcp_ipsec_overhead is large and the 8257 * icmph_du_mtu is the minimum value, which is 68. 8258 */ 8259 new_mss = ntohs(icmph->icmph_du_mtu) - 8260 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8261 8262 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8263 new_mss); 8264 8265 /* 8266 * Only update the MSS if the new one is 8267 * smaller than the previous one. This is 8268 * to avoid problems when getting multiple 8269 * ICMP errors for the same MTU. 8270 */ 8271 if (new_mss >= tcp->tcp_mss) 8272 break; 8273 8274 /* 8275 * Note that we are using the template header's DF 8276 * bit in the fast path sending. So we need to compare 8277 * the new mss with both tcps_mss_min and ip_pmtu_min. 8278 * And stop doing IPv4 PMTUd if new_mss is less than 8279 * MAX(tcps_mss_min, ip_pmtu_min). 8280 */ 8281 if (new_mss < tcps->tcps_mss_min || 8282 new_mss < ipst->ips_ip_pmtu_min) { 8283 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8284 0; 8285 } 8286 8287 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8288 ASSERT(ratio >= 1); 8289 tcp_mss_set(tcp, new_mss, B_TRUE); 8290 8291 /* 8292 * Make sure we have something to 8293 * send. 8294 */ 8295 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8296 (tcp->tcp_xmit_head != NULL)) { 8297 /* 8298 * Shrink tcp_cwnd in 8299 * proportion to the old MSS/new MSS. 8300 */ 8301 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8302 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8303 (tcp->tcp_unsent == 0)) { 8304 tcp->tcp_rexmit_max = tcp->tcp_fss; 8305 } else { 8306 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8307 } 8308 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8309 tcp->tcp_rexmit = B_TRUE; 8310 tcp->tcp_dupack_cnt = 0; 8311 tcp->tcp_snd_burst = TCP_CWND_SS; 8312 tcp_ss_rexmit(tcp); 8313 } 8314 break; 8315 case ICMP_PORT_UNREACHABLE: 8316 case ICMP_PROTOCOL_UNREACHABLE: 8317 switch (tcp->tcp_state) { 8318 case TCPS_SYN_SENT: 8319 case TCPS_SYN_RCVD: 8320 /* 8321 * ICMP can snipe away incipient 8322 * TCP connections as long as 8323 * seq number is same as initial 8324 * send seq number. 8325 */ 8326 if (seg_seq == tcp->tcp_iss) { 8327 (void) tcp_clean_death(tcp, 8328 ECONNREFUSED, 6); 8329 } 8330 break; 8331 } 8332 break; 8333 case ICMP_HOST_UNREACHABLE: 8334 case ICMP_NET_UNREACHABLE: 8335 /* Record the error in case we finally time out. */ 8336 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8337 tcp->tcp_client_errno = EHOSTUNREACH; 8338 else 8339 tcp->tcp_client_errno = ENETUNREACH; 8340 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8341 if (tcp->tcp_listener != NULL && 8342 tcp->tcp_listener->tcp_syn_defense) { 8343 /* 8344 * Ditch the half-open connection if we 8345 * suspect a SYN attack is under way. 8346 */ 8347 tcp_ip_ire_mark_advice(tcp); 8348 (void) tcp_clean_death(tcp, 8349 tcp->tcp_client_errno, 7); 8350 } 8351 } 8352 break; 8353 default: 8354 break; 8355 } 8356 break; 8357 case ICMP_SOURCE_QUENCH: { 8358 /* 8359 * use a global boolean to control 8360 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8361 * The default is false. 8362 */ 8363 if (tcp_icmp_source_quench) { 8364 /* 8365 * Reduce the sending rate as if we got a 8366 * retransmit timeout 8367 */ 8368 uint32_t npkt; 8369 8370 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8371 tcp->tcp_mss; 8372 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8373 tcp->tcp_cwnd = tcp->tcp_mss; 8374 tcp->tcp_cwnd_cnt = 0; 8375 } 8376 break; 8377 } 8378 } 8379 freemsg(first_mp); 8380 } 8381 8382 /* 8383 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8384 * error messages passed up by IP. 8385 * Assumes that IP has pulled up all the extension headers as well 8386 * as the ICMPv6 header. 8387 */ 8388 static void 8389 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8390 { 8391 icmp6_t *icmp6; 8392 ip6_t *ip6h; 8393 uint16_t iph_hdr_length; 8394 tcpha_t *tcpha; 8395 uint8_t *nexthdrp; 8396 uint32_t new_mss; 8397 uint32_t ratio; 8398 boolean_t secure; 8399 mblk_t *first_mp = mp; 8400 size_t mp_size; 8401 uint32_t seg_seq; 8402 tcp_stack_t *tcps = tcp->tcp_tcps; 8403 8404 /* 8405 * The caller has determined if this is an IPSEC_IN packet and 8406 * set ipsec_mctl appropriately (see tcp_icmp_error). 8407 */ 8408 if (ipsec_mctl) 8409 mp = mp->b_cont; 8410 8411 mp_size = MBLKL(mp); 8412 8413 /* 8414 * Verify that we have a complete IP header. If not, send it upstream. 8415 */ 8416 if (mp_size < sizeof (ip6_t)) { 8417 noticmpv6: 8418 freemsg(first_mp); 8419 return; 8420 } 8421 8422 /* 8423 * Verify this is an ICMPV6 packet, else send it upstream. 8424 */ 8425 ip6h = (ip6_t *)mp->b_rptr; 8426 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8427 iph_hdr_length = IPV6_HDR_LEN; 8428 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8429 &nexthdrp) || 8430 *nexthdrp != IPPROTO_ICMPV6) { 8431 goto noticmpv6; 8432 } 8433 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8434 ip6h = (ip6_t *)&icmp6[1]; 8435 /* 8436 * Verify if we have a complete ICMP and inner IP header. 8437 */ 8438 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8439 goto noticmpv6; 8440 8441 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8442 goto noticmpv6; 8443 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8444 /* 8445 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8446 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8447 * packet. 8448 */ 8449 if ((*nexthdrp != IPPROTO_TCP) || 8450 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8451 goto noticmpv6; 8452 } 8453 8454 /* 8455 * ICMP errors come on the right queue or come on 8456 * listener/global queue for detached connections and 8457 * get switched to the right queue. If it comes on the 8458 * right queue, policy check has already been done by IP 8459 * and thus free the first_mp without verifying the policy. 8460 * If it has come for a non-hard bound connection, we need 8461 * to verify policy as IP may not have done it. 8462 */ 8463 if (!tcp->tcp_hard_bound) { 8464 if (ipsec_mctl) { 8465 secure = ipsec_in_is_secure(first_mp); 8466 } else { 8467 secure = B_FALSE; 8468 } 8469 if (secure) { 8470 /* 8471 * If we are willing to accept this in clear 8472 * we don't have to verify policy. 8473 */ 8474 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8475 if (!tcp_check_policy(tcp, first_mp, 8476 NULL, ip6h, secure, ipsec_mctl)) { 8477 /* 8478 * tcp_check_policy called 8479 * ip_drop_packet() on failure. 8480 */ 8481 return; 8482 } 8483 } 8484 } 8485 } else if (ipsec_mctl) { 8486 /* 8487 * This is a hard_bound connection. IP has already 8488 * verified policy. We don't have to do it again. 8489 */ 8490 freeb(first_mp); 8491 first_mp = mp; 8492 ipsec_mctl = B_FALSE; 8493 } 8494 8495 seg_seq = ntohl(tcpha->tha_seq); 8496 /* 8497 * TCP SHOULD check that the TCP sequence number contained in 8498 * payload of the ICMP error message is within the range 8499 * SND.UNA <= SEG.SEQ < SND.NXT. 8500 */ 8501 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8502 /* 8503 * If the ICMP message is bogus, should we kill the 8504 * connection, or should we just drop the bogus ICMP 8505 * message? It would probably make more sense to just 8506 * drop the message so that if this one managed to get 8507 * in, the real connection should not suffer. 8508 */ 8509 goto noticmpv6; 8510 } 8511 8512 switch (icmp6->icmp6_type) { 8513 case ICMP6_PACKET_TOO_BIG: 8514 /* 8515 * Reduce the MSS based on the new MTU. This will 8516 * eliminate any fragmentation locally. 8517 * N.B. There may well be some funny side-effects on 8518 * the local send policy and the remote receive policy. 8519 * Pending further research, we provide 8520 * tcp_ignore_path_mtu just in case this proves 8521 * disastrous somewhere. 8522 * 8523 * After updating the MSS, retransmit part of the 8524 * dropped segment using the new mss by calling 8525 * tcp_wput_data(). Need to adjust all those 8526 * params to make sure tcp_wput_data() work properly. 8527 */ 8528 if (tcps->tcps_ignore_path_mtu) 8529 break; 8530 8531 /* 8532 * Decrease the MSS by time stamp options 8533 * IP options and IPSEC options. tcp_hdr_len 8534 * includes time stamp option and IP option 8535 * length. 8536 */ 8537 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8538 tcp->tcp_ipsec_overhead; 8539 8540 /* 8541 * Only update the MSS if the new one is 8542 * smaller than the previous one. This is 8543 * to avoid problems when getting multiple 8544 * ICMP errors for the same MTU. 8545 */ 8546 if (new_mss >= tcp->tcp_mss) 8547 break; 8548 8549 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8550 ASSERT(ratio >= 1); 8551 tcp_mss_set(tcp, new_mss, B_TRUE); 8552 8553 /* 8554 * Make sure we have something to 8555 * send. 8556 */ 8557 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8558 (tcp->tcp_xmit_head != NULL)) { 8559 /* 8560 * Shrink tcp_cwnd in 8561 * proportion to the old MSS/new MSS. 8562 */ 8563 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8564 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8565 (tcp->tcp_unsent == 0)) { 8566 tcp->tcp_rexmit_max = tcp->tcp_fss; 8567 } else { 8568 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8569 } 8570 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8571 tcp->tcp_rexmit = B_TRUE; 8572 tcp->tcp_dupack_cnt = 0; 8573 tcp->tcp_snd_burst = TCP_CWND_SS; 8574 tcp_ss_rexmit(tcp); 8575 } 8576 break; 8577 8578 case ICMP6_DST_UNREACH: 8579 switch (icmp6->icmp6_code) { 8580 case ICMP6_DST_UNREACH_NOPORT: 8581 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8582 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8583 (seg_seq == tcp->tcp_iss)) { 8584 (void) tcp_clean_death(tcp, 8585 ECONNREFUSED, 8); 8586 } 8587 break; 8588 8589 case ICMP6_DST_UNREACH_ADMIN: 8590 case ICMP6_DST_UNREACH_NOROUTE: 8591 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8592 case ICMP6_DST_UNREACH_ADDR: 8593 /* Record the error in case we finally time out. */ 8594 tcp->tcp_client_errno = EHOSTUNREACH; 8595 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8596 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8597 (seg_seq == tcp->tcp_iss)) { 8598 if (tcp->tcp_listener != NULL && 8599 tcp->tcp_listener->tcp_syn_defense) { 8600 /* 8601 * Ditch the half-open connection if we 8602 * suspect a SYN attack is under way. 8603 */ 8604 tcp_ip_ire_mark_advice(tcp); 8605 (void) tcp_clean_death(tcp, 8606 tcp->tcp_client_errno, 9); 8607 } 8608 } 8609 8610 8611 break; 8612 default: 8613 break; 8614 } 8615 break; 8616 8617 case ICMP6_PARAM_PROB: 8618 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8619 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8620 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8621 (uchar_t *)nexthdrp) { 8622 if (tcp->tcp_state == TCPS_SYN_SENT || 8623 tcp->tcp_state == TCPS_SYN_RCVD) { 8624 (void) tcp_clean_death(tcp, 8625 ECONNREFUSED, 10); 8626 } 8627 break; 8628 } 8629 break; 8630 8631 case ICMP6_TIME_EXCEEDED: 8632 default: 8633 break; 8634 } 8635 freemsg(first_mp); 8636 } 8637 8638 /* 8639 * Notify IP that we are having trouble with this connection. IP should 8640 * blow the IRE away and start over. 8641 */ 8642 static void 8643 tcp_ip_notify(tcp_t *tcp) 8644 { 8645 struct iocblk *iocp; 8646 ipid_t *ipid; 8647 mblk_t *mp; 8648 8649 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8650 if (tcp->tcp_ipversion == IPV6_VERSION) 8651 return; 8652 8653 mp = mkiocb(IP_IOCTL); 8654 if (mp == NULL) 8655 return; 8656 8657 iocp = (struct iocblk *)mp->b_rptr; 8658 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8659 8660 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8661 if (!mp->b_cont) { 8662 freeb(mp); 8663 return; 8664 } 8665 8666 ipid = (ipid_t *)mp->b_cont->b_rptr; 8667 mp->b_cont->b_wptr += iocp->ioc_count; 8668 bzero(ipid, sizeof (*ipid)); 8669 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8670 ipid->ipid_ire_type = IRE_CACHE; 8671 ipid->ipid_addr_offset = sizeof (ipid_t); 8672 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8673 /* 8674 * Note: in the case of source routing we want to blow away the 8675 * route to the first source route hop. 8676 */ 8677 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8678 sizeof (tcp->tcp_ipha->ipha_dst)); 8679 8680 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8681 } 8682 8683 /* Unlink and return any mblk that looks like it contains an ire */ 8684 static mblk_t * 8685 tcp_ire_mp(mblk_t **mpp) 8686 { 8687 mblk_t *mp = *mpp; 8688 mblk_t *prev_mp = NULL; 8689 8690 for (;;) { 8691 switch (DB_TYPE(mp)) { 8692 case IRE_DB_TYPE: 8693 case IRE_DB_REQ_TYPE: 8694 if (mp == *mpp) { 8695 *mpp = mp->b_cont; 8696 } else { 8697 prev_mp->b_cont = mp->b_cont; 8698 } 8699 mp->b_cont = NULL; 8700 return (mp); 8701 default: 8702 break; 8703 } 8704 prev_mp = mp; 8705 mp = mp->b_cont; 8706 if (mp == NULL) 8707 break; 8708 } 8709 return (mp); 8710 } 8711 8712 /* 8713 * Timer callback routine for keepalive probe. We do a fake resend of 8714 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8715 * check to see if we have heard anything from the other end for the last 8716 * RTO period. If we have, set the timer to expire for another 8717 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8718 * RTO << 1 and check again when it expires. Keep exponentially increasing 8719 * the timeout if we have not heard from the other side. If for more than 8720 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8721 * kill the connection unless the keepalive abort threshold is 0. In 8722 * that case, we will probe "forever." 8723 */ 8724 static void 8725 tcp_keepalive_killer(void *arg) 8726 { 8727 mblk_t *mp; 8728 conn_t *connp = (conn_t *)arg; 8729 tcp_t *tcp = connp->conn_tcp; 8730 int32_t firetime; 8731 int32_t idletime; 8732 int32_t ka_intrvl; 8733 tcp_stack_t *tcps = tcp->tcp_tcps; 8734 8735 tcp->tcp_ka_tid = 0; 8736 8737 if (tcp->tcp_fused) 8738 return; 8739 8740 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 8741 ka_intrvl = tcp->tcp_ka_interval; 8742 8743 /* 8744 * Keepalive probe should only be sent if the application has not 8745 * done a close on the connection. 8746 */ 8747 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8748 return; 8749 } 8750 /* Timer fired too early, restart it. */ 8751 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8752 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8753 MSEC_TO_TICK(ka_intrvl)); 8754 return; 8755 } 8756 8757 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8758 /* 8759 * If we have not heard from the other side for a long 8760 * time, kill the connection unless the keepalive abort 8761 * threshold is 0. In that case, we will probe "forever." 8762 */ 8763 if (tcp->tcp_ka_abort_thres != 0 && 8764 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8765 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 8766 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8767 tcp->tcp_client_errno : ETIMEDOUT, 11); 8768 return; 8769 } 8770 8771 if (tcp->tcp_snxt == tcp->tcp_suna && 8772 idletime >= ka_intrvl) { 8773 /* Fake resend of last ACKed byte. */ 8774 mblk_t *mp1 = allocb(1, BPRI_LO); 8775 8776 if (mp1 != NULL) { 8777 *mp1->b_wptr++ = '\0'; 8778 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8779 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8780 freeb(mp1); 8781 /* 8782 * if allocation failed, fall through to start the 8783 * timer back. 8784 */ 8785 if (mp != NULL) { 8786 tcp_send_data(tcp, tcp->tcp_wq, mp); 8787 BUMP_MIB(&tcps->tcps_mib, 8788 tcpTimKeepaliveProbe); 8789 if (tcp->tcp_ka_last_intrvl != 0) { 8790 int max; 8791 /* 8792 * We should probe again at least 8793 * in ka_intrvl, but not more than 8794 * tcp_rexmit_interval_max. 8795 */ 8796 max = tcps->tcps_rexmit_interval_max; 8797 firetime = MIN(ka_intrvl - 1, 8798 tcp->tcp_ka_last_intrvl << 1); 8799 if (firetime > max) 8800 firetime = max; 8801 } else { 8802 firetime = tcp->tcp_rto; 8803 } 8804 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8805 tcp_keepalive_killer, 8806 MSEC_TO_TICK(firetime)); 8807 tcp->tcp_ka_last_intrvl = firetime; 8808 return; 8809 } 8810 } 8811 } else { 8812 tcp->tcp_ka_last_intrvl = 0; 8813 } 8814 8815 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8816 if ((firetime = ka_intrvl - idletime) < 0) { 8817 firetime = ka_intrvl; 8818 } 8819 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8820 MSEC_TO_TICK(firetime)); 8821 } 8822 8823 int 8824 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8825 { 8826 queue_t *q = tcp->tcp_rq; 8827 int32_t mss = tcp->tcp_mss; 8828 int maxpsz; 8829 conn_t *connp = tcp->tcp_connp; 8830 8831 if (TCP_IS_DETACHED(tcp)) 8832 return (mss); 8833 if (tcp->tcp_fused) { 8834 maxpsz = tcp_fuse_maxpsz_set(tcp); 8835 mss = INFPSZ; 8836 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 8837 /* 8838 * Set the sd_qn_maxpsz according to the socket send buffer 8839 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8840 * instruct the stream head to copyin user data into contiguous 8841 * kernel-allocated buffers without breaking it up into smaller 8842 * chunks. We round up the buffer size to the nearest SMSS. 8843 */ 8844 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8845 if (tcp->tcp_kssl_ctx == NULL) 8846 mss = INFPSZ; 8847 else 8848 mss = SSL3_MAX_RECORD_LEN; 8849 } else { 8850 /* 8851 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8852 * (and a multiple of the mss). This instructs the stream 8853 * head to break down larger than SMSS writes into SMSS- 8854 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8855 */ 8856 /* XXX tune this with ndd tcp_maxpsz_multiplier */ 8857 maxpsz = tcp->tcp_maxpsz * mss; 8858 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8859 maxpsz = tcp->tcp_xmit_hiwater/2; 8860 /* Round up to nearest mss */ 8861 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8862 } 8863 } 8864 8865 (void) proto_set_maxpsz(q, connp, maxpsz); 8866 if (!(IPCL_IS_NONSTR(connp))) { 8867 /* XXX do it in set_maxpsz()? */ 8868 tcp->tcp_wq->q_maxpsz = maxpsz; 8869 } 8870 8871 if (set_maxblk) 8872 (void) proto_set_tx_maxblk(q, connp, mss); 8873 return (mss); 8874 } 8875 8876 /* 8877 * Extract option values from a tcp header. We put any found values into the 8878 * tcpopt struct and return a bitmask saying which options were found. 8879 */ 8880 static int 8881 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8882 { 8883 uchar_t *endp; 8884 int len; 8885 uint32_t mss; 8886 uchar_t *up = (uchar_t *)tcph; 8887 int found = 0; 8888 int32_t sack_len; 8889 tcp_seq sack_begin, sack_end; 8890 tcp_t *tcp; 8891 8892 endp = up + TCP_HDR_LENGTH(tcph); 8893 up += TCP_MIN_HEADER_LENGTH; 8894 while (up < endp) { 8895 len = endp - up; 8896 switch (*up) { 8897 case TCPOPT_EOL: 8898 break; 8899 8900 case TCPOPT_NOP: 8901 up++; 8902 continue; 8903 8904 case TCPOPT_MAXSEG: 8905 if (len < TCPOPT_MAXSEG_LEN || 8906 up[1] != TCPOPT_MAXSEG_LEN) 8907 break; 8908 8909 mss = BE16_TO_U16(up+2); 8910 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 8911 tcpopt->tcp_opt_mss = mss; 8912 found |= TCP_OPT_MSS_PRESENT; 8913 8914 up += TCPOPT_MAXSEG_LEN; 8915 continue; 8916 8917 case TCPOPT_WSCALE: 8918 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 8919 break; 8920 8921 if (up[2] > TCP_MAX_WINSHIFT) 8922 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 8923 else 8924 tcpopt->tcp_opt_wscale = up[2]; 8925 found |= TCP_OPT_WSCALE_PRESENT; 8926 8927 up += TCPOPT_WS_LEN; 8928 continue; 8929 8930 case TCPOPT_SACK_PERMITTED: 8931 if (len < TCPOPT_SACK_OK_LEN || 8932 up[1] != TCPOPT_SACK_OK_LEN) 8933 break; 8934 found |= TCP_OPT_SACK_OK_PRESENT; 8935 up += TCPOPT_SACK_OK_LEN; 8936 continue; 8937 8938 case TCPOPT_SACK: 8939 if (len <= 2 || up[1] <= 2 || len < up[1]) 8940 break; 8941 8942 /* If TCP is not interested in SACK blks... */ 8943 if ((tcp = tcpopt->tcp) == NULL) { 8944 up += up[1]; 8945 continue; 8946 } 8947 sack_len = up[1] - TCPOPT_HEADER_LEN; 8948 up += TCPOPT_HEADER_LEN; 8949 8950 /* 8951 * If the list is empty, allocate one and assume 8952 * nothing is sack'ed. 8953 */ 8954 ASSERT(tcp->tcp_sack_info != NULL); 8955 if (tcp->tcp_notsack_list == NULL) { 8956 tcp_notsack_update(&(tcp->tcp_notsack_list), 8957 tcp->tcp_suna, tcp->tcp_snxt, 8958 &(tcp->tcp_num_notsack_blk), 8959 &(tcp->tcp_cnt_notsack_list)); 8960 8961 /* 8962 * Make sure tcp_notsack_list is not NULL. 8963 * This happens when kmem_alloc(KM_NOSLEEP) 8964 * returns NULL. 8965 */ 8966 if (tcp->tcp_notsack_list == NULL) { 8967 up += sack_len; 8968 continue; 8969 } 8970 tcp->tcp_fack = tcp->tcp_suna; 8971 } 8972 8973 while (sack_len > 0) { 8974 if (up + 8 > endp) { 8975 up = endp; 8976 break; 8977 } 8978 sack_begin = BE32_TO_U32(up); 8979 up += 4; 8980 sack_end = BE32_TO_U32(up); 8981 up += 4; 8982 sack_len -= 8; 8983 /* 8984 * Bounds checking. Make sure the SACK 8985 * info is within tcp_suna and tcp_snxt. 8986 * If this SACK blk is out of bound, ignore 8987 * it but continue to parse the following 8988 * blks. 8989 */ 8990 if (SEQ_LEQ(sack_end, sack_begin) || 8991 SEQ_LT(sack_begin, tcp->tcp_suna) || 8992 SEQ_GT(sack_end, tcp->tcp_snxt)) { 8993 continue; 8994 } 8995 tcp_notsack_insert(&(tcp->tcp_notsack_list), 8996 sack_begin, sack_end, 8997 &(tcp->tcp_num_notsack_blk), 8998 &(tcp->tcp_cnt_notsack_list)); 8999 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9000 tcp->tcp_fack = sack_end; 9001 } 9002 } 9003 found |= TCP_OPT_SACK_PRESENT; 9004 continue; 9005 9006 case TCPOPT_TSTAMP: 9007 if (len < TCPOPT_TSTAMP_LEN || 9008 up[1] != TCPOPT_TSTAMP_LEN) 9009 break; 9010 9011 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9012 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9013 9014 found |= TCP_OPT_TSTAMP_PRESENT; 9015 9016 up += TCPOPT_TSTAMP_LEN; 9017 continue; 9018 9019 default: 9020 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9021 break; 9022 up += up[1]; 9023 continue; 9024 } 9025 break; 9026 } 9027 return (found); 9028 } 9029 9030 /* 9031 * Set the mss associated with a particular tcp based on its current value, 9032 * and a new one passed in. Observe minimums and maximums, and reset 9033 * other state variables that we want to view as multiples of mss. 9034 * 9035 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9036 * highwater marks etc. need to be initialized or adjusted. 9037 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9038 * packet arrives. 9039 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9040 * ICMP6_PACKET_TOO_BIG arrives. 9041 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9042 * to increase the MSS to use the extra bytes available. 9043 * 9044 * Callers except tcp_paws_check() ensure that they only reduce mss. 9045 */ 9046 static void 9047 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9048 { 9049 uint32_t mss_max; 9050 tcp_stack_t *tcps = tcp->tcp_tcps; 9051 9052 if (tcp->tcp_ipversion == IPV4_VERSION) 9053 mss_max = tcps->tcps_mss_max_ipv4; 9054 else 9055 mss_max = tcps->tcps_mss_max_ipv6; 9056 9057 if (mss < tcps->tcps_mss_min) 9058 mss = tcps->tcps_mss_min; 9059 if (mss > mss_max) 9060 mss = mss_max; 9061 /* 9062 * Unless naglim has been set by our client to 9063 * a non-mss value, force naglim to track mss. 9064 * This can help to aggregate small writes. 9065 */ 9066 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9067 tcp->tcp_naglim = mss; 9068 /* 9069 * TCP should be able to buffer at least 4 MSS data for obvious 9070 * performance reason. 9071 */ 9072 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9073 tcp->tcp_xmit_hiwater = mss << 2; 9074 9075 if (do_ss) { 9076 /* 9077 * Either the tcp_cwnd is as yet uninitialized, or mss is 9078 * changing due to a reduction in MTU, presumably as a 9079 * result of a new path component, reset cwnd to its 9080 * "initial" value, as a multiple of the new mss. 9081 */ 9082 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9083 } else { 9084 /* 9085 * Called by tcp_paws_check(), the mss increased 9086 * marginally to allow use of space previously taken 9087 * by the timestamp option. It would be inappropriate 9088 * to apply slow start or tcp_init_cwnd values to 9089 * tcp_cwnd, simply adjust to a multiple of the new mss. 9090 */ 9091 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9092 tcp->tcp_cwnd_cnt = 0; 9093 } 9094 tcp->tcp_mss = mss; 9095 (void) tcp_maxpsz_set(tcp, B_TRUE); 9096 } 9097 9098 /* For /dev/tcp aka AF_INET open */ 9099 static int 9100 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9101 { 9102 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9103 } 9104 9105 /* For /dev/tcp6 aka AF_INET6 open */ 9106 static int 9107 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9108 { 9109 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9110 } 9111 9112 static conn_t * 9113 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9114 boolean_t issocket, int *errorp) 9115 { 9116 tcp_t *tcp = NULL; 9117 conn_t *connp; 9118 int err; 9119 zoneid_t zoneid; 9120 tcp_stack_t *tcps; 9121 squeue_t *sqp; 9122 9123 ASSERT(errorp != NULL); 9124 /* 9125 * Find the proper zoneid and netstack. 9126 */ 9127 /* 9128 * Special case for install: miniroot needs to be able to 9129 * access files via NFS as though it were always in the 9130 * global zone. 9131 */ 9132 if (credp == kcred && nfs_global_client_only != 0) { 9133 zoneid = GLOBAL_ZONEID; 9134 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9135 netstack_tcp; 9136 ASSERT(tcps != NULL); 9137 } else { 9138 netstack_t *ns; 9139 9140 ns = netstack_find_by_cred(credp); 9141 ASSERT(ns != NULL); 9142 tcps = ns->netstack_tcp; 9143 ASSERT(tcps != NULL); 9144 9145 /* 9146 * For exclusive stacks we set the zoneid to zero 9147 * to make TCP operate as if in the global zone. 9148 */ 9149 if (tcps->tcps_netstack->netstack_stackid != 9150 GLOBAL_NETSTACKID) 9151 zoneid = GLOBAL_ZONEID; 9152 else 9153 zoneid = crgetzoneid(credp); 9154 } 9155 /* 9156 * For stackid zero this is done from strplumb.c, but 9157 * non-zero stackids are handled here. 9158 */ 9159 if (tcps->tcps_g_q == NULL && 9160 tcps->tcps_netstack->netstack_stackid != 9161 GLOBAL_NETSTACKID) { 9162 tcp_g_q_setup(tcps); 9163 } 9164 9165 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9166 connp = (conn_t *)tcp_get_conn(sqp, tcps, q != NULL ? B_TRUE : B_FALSE); 9167 /* 9168 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9169 * so we drop it by one. 9170 */ 9171 netstack_rele(tcps->tcps_netstack); 9172 if (connp == NULL) { 9173 *errorp = ENOSR; 9174 return (NULL); 9175 } 9176 connp->conn_sqp = sqp; 9177 connp->conn_initial_sqp = connp->conn_sqp; 9178 tcp = connp->conn_tcp; 9179 9180 if (isv6) { 9181 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9182 connp->conn_send = ip_output_v6; 9183 connp->conn_af_isv6 = B_TRUE; 9184 connp->conn_pkt_isv6 = B_TRUE; 9185 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9186 tcp->tcp_ipversion = IPV6_VERSION; 9187 tcp->tcp_family = AF_INET6; 9188 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9189 } else { 9190 connp->conn_flags |= IPCL_TCP4; 9191 connp->conn_send = ip_output; 9192 connp->conn_af_isv6 = B_FALSE; 9193 connp->conn_pkt_isv6 = B_FALSE; 9194 tcp->tcp_ipversion = IPV4_VERSION; 9195 tcp->tcp_family = AF_INET; 9196 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9197 } 9198 9199 /* 9200 * TCP keeps a copy of cred for cache locality reasons but 9201 * we put a reference only once. If connp->conn_cred 9202 * becomes invalid, tcp_cred should also be set to NULL. 9203 */ 9204 tcp->tcp_cred = connp->conn_cred = credp; 9205 crhold(connp->conn_cred); 9206 tcp->tcp_cpid = curproc->p_pid; 9207 tcp->tcp_open_time = lbolt64; 9208 connp->conn_zoneid = zoneid; 9209 connp->conn_mlp_type = mlptSingle; 9210 connp->conn_ulp_labeled = !is_system_labeled(); 9211 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9212 ASSERT(tcp->tcp_tcps == tcps); 9213 9214 /* 9215 * If the caller has the process-wide flag set, then default to MAC 9216 * exempt mode. This allows read-down to unlabeled hosts. 9217 */ 9218 if (getpflags(NET_MAC_AWARE, credp) != 0) 9219 connp->conn_mac_exempt = B_TRUE; 9220 9221 connp->conn_dev = NULL; 9222 if (issocket) { 9223 connp->conn_flags |= IPCL_SOCKET; 9224 tcp->tcp_issocket = 1; 9225 } 9226 9227 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9228 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9229 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9230 9231 /* Non-zero default values */ 9232 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9233 9234 if (q == NULL) { 9235 /* 9236 * Create a helper stream for non-STREAMS socket. 9237 */ 9238 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9239 if (err != 0) { 9240 ip1dbg(("tcp_create_common: create of IP helper stream " 9241 "failed\n")); 9242 CONN_DEC_REF(connp); 9243 *errorp = err; 9244 return (NULL); 9245 } 9246 q = connp->conn_rq; 9247 } else { 9248 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9249 } 9250 9251 SOCK_CONNID_INIT(tcp->tcp_connid); 9252 err = tcp_init(tcp, q); 9253 if (err != 0) { 9254 CONN_DEC_REF(connp); 9255 *errorp = err; 9256 return (NULL); 9257 } 9258 9259 return (connp); 9260 } 9261 9262 static int 9263 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9264 boolean_t isv6) 9265 { 9266 tcp_t *tcp = NULL; 9267 conn_t *connp = NULL; 9268 int err; 9269 vmem_t *minor_arena = NULL; 9270 dev_t conn_dev; 9271 boolean_t issocket; 9272 9273 if (q->q_ptr != NULL) 9274 return (0); 9275 9276 if (sflag == MODOPEN) 9277 return (EINVAL); 9278 9279 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9280 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9281 minor_arena = ip_minor_arena_la; 9282 } else { 9283 /* 9284 * Either minor numbers in the large arena were exhausted 9285 * or a non socket application is doing the open. 9286 * Try to allocate from the small arena. 9287 */ 9288 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9289 return (EBUSY); 9290 } 9291 minor_arena = ip_minor_arena_sa; 9292 } 9293 9294 ASSERT(minor_arena != NULL); 9295 9296 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9297 9298 if (flag & SO_FALLBACK) { 9299 /* 9300 * Non streams socket needs a stream to fallback to 9301 */ 9302 RD(q)->q_ptr = (void *)conn_dev; 9303 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9304 WR(q)->q_ptr = (void *)minor_arena; 9305 qprocson(q); 9306 return (0); 9307 } else if (flag & SO_ACCEPTOR) { 9308 q->q_qinfo = &tcp_acceptor_rinit; 9309 /* 9310 * the conn_dev and minor_arena will be subsequently used by 9311 * tcp_wput_accept() and tcp_tpi_close_accept() to figure out 9312 * the minor device number for this connection from the q_ptr. 9313 */ 9314 RD(q)->q_ptr = (void *)conn_dev; 9315 WR(q)->q_qinfo = &tcp_acceptor_winit; 9316 WR(q)->q_ptr = (void *)minor_arena; 9317 qprocson(q); 9318 return (0); 9319 } 9320 9321 issocket = flag & SO_SOCKSTR; 9322 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9323 9324 if (connp == NULL) { 9325 inet_minor_free(minor_arena, conn_dev); 9326 q->q_ptr = WR(q)->q_ptr = NULL; 9327 return (err); 9328 } 9329 9330 q->q_ptr = WR(q)->q_ptr = connp; 9331 9332 connp->conn_dev = conn_dev; 9333 connp->conn_minor_arena = minor_arena; 9334 9335 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9336 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9337 9338 tcp = connp->conn_tcp; 9339 9340 if (issocket) { 9341 WR(q)->q_qinfo = &tcp_sock_winit; 9342 } else { 9343 #ifdef _ILP32 9344 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9345 #else 9346 tcp->tcp_acceptor_id = conn_dev; 9347 #endif /* _ILP32 */ 9348 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9349 } 9350 9351 /* 9352 * Put the ref for TCP. Ref for IP was already put 9353 * by ipcl_conn_create. Also Make the conn_t globally 9354 * visible to walkers 9355 */ 9356 mutex_enter(&connp->conn_lock); 9357 CONN_INC_REF_LOCKED(connp); 9358 ASSERT(connp->conn_ref == 2); 9359 connp->conn_state_flags &= ~CONN_INCIPIENT; 9360 mutex_exit(&connp->conn_lock); 9361 9362 qprocson(q); 9363 return (0); 9364 } 9365 9366 /* 9367 * Some TCP options can be "set" by requesting them in the option 9368 * buffer. This is needed for XTI feature test though we do not 9369 * allow it in general. We interpret that this mechanism is more 9370 * applicable to OSI protocols and need not be allowed in general. 9371 * This routine filters out options for which it is not allowed (most) 9372 * and lets through those (few) for which it is. [ The XTI interface 9373 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9374 * ever implemented will have to be allowed here ]. 9375 */ 9376 static boolean_t 9377 tcp_allow_connopt_set(int level, int name) 9378 { 9379 9380 switch (level) { 9381 case IPPROTO_TCP: 9382 switch (name) { 9383 case TCP_NODELAY: 9384 return (B_TRUE); 9385 default: 9386 return (B_FALSE); 9387 } 9388 /*NOTREACHED*/ 9389 default: 9390 return (B_FALSE); 9391 } 9392 /*NOTREACHED*/ 9393 } 9394 9395 /* 9396 * this routine gets default values of certain options whose default 9397 * values are maintained by protocol specific code 9398 */ 9399 /* ARGSUSED */ 9400 int 9401 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9402 { 9403 int32_t *i1 = (int32_t *)ptr; 9404 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9405 9406 switch (level) { 9407 case IPPROTO_TCP: 9408 switch (name) { 9409 case TCP_NOTIFY_THRESHOLD: 9410 *i1 = tcps->tcps_ip_notify_interval; 9411 break; 9412 case TCP_ABORT_THRESHOLD: 9413 *i1 = tcps->tcps_ip_abort_interval; 9414 break; 9415 case TCP_CONN_NOTIFY_THRESHOLD: 9416 *i1 = tcps->tcps_ip_notify_cinterval; 9417 break; 9418 case TCP_CONN_ABORT_THRESHOLD: 9419 *i1 = tcps->tcps_ip_abort_cinterval; 9420 break; 9421 default: 9422 return (-1); 9423 } 9424 break; 9425 case IPPROTO_IP: 9426 switch (name) { 9427 case IP_TTL: 9428 *i1 = tcps->tcps_ipv4_ttl; 9429 break; 9430 default: 9431 return (-1); 9432 } 9433 break; 9434 case IPPROTO_IPV6: 9435 switch (name) { 9436 case IPV6_UNICAST_HOPS: 9437 *i1 = tcps->tcps_ipv6_hoplimit; 9438 break; 9439 default: 9440 return (-1); 9441 } 9442 break; 9443 default: 9444 return (-1); 9445 } 9446 return (sizeof (int)); 9447 } 9448 9449 static int 9450 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9451 { 9452 int *i1 = (int *)ptr; 9453 tcp_t *tcp = connp->conn_tcp; 9454 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9455 9456 switch (level) { 9457 case SOL_SOCKET: 9458 switch (name) { 9459 case SO_LINGER: { 9460 struct linger *lgr = (struct linger *)ptr; 9461 9462 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9463 lgr->l_linger = tcp->tcp_lingertime; 9464 } 9465 return (sizeof (struct linger)); 9466 case SO_DEBUG: 9467 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9468 break; 9469 case SO_KEEPALIVE: 9470 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9471 break; 9472 case SO_DONTROUTE: 9473 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9474 break; 9475 case SO_USELOOPBACK: 9476 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9477 break; 9478 case SO_BROADCAST: 9479 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9480 break; 9481 case SO_REUSEADDR: 9482 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9483 break; 9484 case SO_OOBINLINE: 9485 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9486 break; 9487 case SO_DGRAM_ERRIND: 9488 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9489 break; 9490 case SO_TYPE: 9491 *i1 = SOCK_STREAM; 9492 break; 9493 case SO_SNDBUF: 9494 *i1 = tcp->tcp_xmit_hiwater; 9495 break; 9496 case SO_RCVBUF: 9497 *i1 = tcp->tcp_recv_hiwater; 9498 break; 9499 case SO_SND_COPYAVOID: 9500 *i1 = tcp->tcp_snd_zcopy_on ? 9501 SO_SND_COPYAVOID : 0; 9502 break; 9503 case SO_ALLZONES: 9504 *i1 = connp->conn_allzones ? 1 : 0; 9505 break; 9506 case SO_ANON_MLP: 9507 *i1 = connp->conn_anon_mlp; 9508 break; 9509 case SO_MAC_EXEMPT: 9510 *i1 = connp->conn_mac_exempt; 9511 break; 9512 case SO_EXCLBIND: 9513 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9514 break; 9515 case SO_PROTOTYPE: 9516 *i1 = IPPROTO_TCP; 9517 break; 9518 case SO_DOMAIN: 9519 *i1 = tcp->tcp_family; 9520 break; 9521 case SO_ACCEPTCONN: 9522 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9523 default: 9524 return (-1); 9525 } 9526 break; 9527 case IPPROTO_TCP: 9528 switch (name) { 9529 case TCP_NODELAY: 9530 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9531 break; 9532 case TCP_MAXSEG: 9533 *i1 = tcp->tcp_mss; 9534 break; 9535 case TCP_NOTIFY_THRESHOLD: 9536 *i1 = (int)tcp->tcp_first_timer_threshold; 9537 break; 9538 case TCP_ABORT_THRESHOLD: 9539 *i1 = tcp->tcp_second_timer_threshold; 9540 break; 9541 case TCP_CONN_NOTIFY_THRESHOLD: 9542 *i1 = tcp->tcp_first_ctimer_threshold; 9543 break; 9544 case TCP_CONN_ABORT_THRESHOLD: 9545 *i1 = tcp->tcp_second_ctimer_threshold; 9546 break; 9547 case TCP_RECVDSTADDR: 9548 *i1 = tcp->tcp_recvdstaddr; 9549 break; 9550 case TCP_ANONPRIVBIND: 9551 *i1 = tcp->tcp_anon_priv_bind; 9552 break; 9553 case TCP_EXCLBIND: 9554 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9555 break; 9556 case TCP_INIT_CWND: 9557 *i1 = tcp->tcp_init_cwnd; 9558 break; 9559 case TCP_KEEPALIVE_THRESHOLD: 9560 *i1 = tcp->tcp_ka_interval; 9561 break; 9562 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9563 *i1 = tcp->tcp_ka_abort_thres; 9564 break; 9565 case TCP_CORK: 9566 *i1 = tcp->tcp_cork; 9567 break; 9568 default: 9569 return (-1); 9570 } 9571 break; 9572 case IPPROTO_IP: 9573 if (tcp->tcp_family != AF_INET) 9574 return (-1); 9575 switch (name) { 9576 case IP_OPTIONS: 9577 case T_IP_OPTIONS: { 9578 /* 9579 * This is compatible with BSD in that in only return 9580 * the reverse source route with the final destination 9581 * as the last entry. The first 4 bytes of the option 9582 * will contain the final destination. 9583 */ 9584 int opt_len; 9585 9586 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9587 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9588 ASSERT(opt_len >= 0); 9589 /* Caller ensures enough space */ 9590 if (opt_len > 0) { 9591 /* 9592 * TODO: Do we have to handle getsockopt on an 9593 * initiator as well? 9594 */ 9595 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9596 } 9597 return (0); 9598 } 9599 case IP_TOS: 9600 case T_IP_TOS: 9601 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9602 break; 9603 case IP_TTL: 9604 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9605 break; 9606 case IP_NEXTHOP: 9607 /* Handled at IP level */ 9608 return (-EINVAL); 9609 default: 9610 return (-1); 9611 } 9612 break; 9613 case IPPROTO_IPV6: 9614 /* 9615 * IPPROTO_IPV6 options are only supported for sockets 9616 * that are using IPv6 on the wire. 9617 */ 9618 if (tcp->tcp_ipversion != IPV6_VERSION) { 9619 return (-1); 9620 } 9621 switch (name) { 9622 case IPV6_UNICAST_HOPS: 9623 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9624 break; /* goto sizeof (int) option return */ 9625 case IPV6_BOUND_IF: 9626 /* Zero if not set */ 9627 *i1 = tcp->tcp_bound_if; 9628 break; /* goto sizeof (int) option return */ 9629 case IPV6_RECVPKTINFO: 9630 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9631 *i1 = 1; 9632 else 9633 *i1 = 0; 9634 break; /* goto sizeof (int) option return */ 9635 case IPV6_RECVTCLASS: 9636 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9637 *i1 = 1; 9638 else 9639 *i1 = 0; 9640 break; /* goto sizeof (int) option return */ 9641 case IPV6_RECVHOPLIMIT: 9642 if (tcp->tcp_ipv6_recvancillary & 9643 TCP_IPV6_RECVHOPLIMIT) 9644 *i1 = 1; 9645 else 9646 *i1 = 0; 9647 break; /* goto sizeof (int) option return */ 9648 case IPV6_RECVHOPOPTS: 9649 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9650 *i1 = 1; 9651 else 9652 *i1 = 0; 9653 break; /* goto sizeof (int) option return */ 9654 case IPV6_RECVDSTOPTS: 9655 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9656 *i1 = 1; 9657 else 9658 *i1 = 0; 9659 break; /* goto sizeof (int) option return */ 9660 case _OLD_IPV6_RECVDSTOPTS: 9661 if (tcp->tcp_ipv6_recvancillary & 9662 TCP_OLD_IPV6_RECVDSTOPTS) 9663 *i1 = 1; 9664 else 9665 *i1 = 0; 9666 break; /* goto sizeof (int) option return */ 9667 case IPV6_RECVRTHDR: 9668 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9669 *i1 = 1; 9670 else 9671 *i1 = 0; 9672 break; /* goto sizeof (int) option return */ 9673 case IPV6_RECVRTHDRDSTOPTS: 9674 if (tcp->tcp_ipv6_recvancillary & 9675 TCP_IPV6_RECVRTDSTOPTS) 9676 *i1 = 1; 9677 else 9678 *i1 = 0; 9679 break; /* goto sizeof (int) option return */ 9680 case IPV6_PKTINFO: { 9681 /* XXX assumes that caller has room for max size! */ 9682 struct in6_pktinfo *pkti; 9683 9684 pkti = (struct in6_pktinfo *)ptr; 9685 if (ipp->ipp_fields & IPPF_IFINDEX) 9686 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9687 else 9688 pkti->ipi6_ifindex = 0; 9689 if (ipp->ipp_fields & IPPF_ADDR) 9690 pkti->ipi6_addr = ipp->ipp_addr; 9691 else 9692 pkti->ipi6_addr = ipv6_all_zeros; 9693 return (sizeof (struct in6_pktinfo)); 9694 } 9695 case IPV6_TCLASS: 9696 if (ipp->ipp_fields & IPPF_TCLASS) 9697 *i1 = ipp->ipp_tclass; 9698 else 9699 *i1 = IPV6_FLOW_TCLASS( 9700 IPV6_DEFAULT_VERS_AND_FLOW); 9701 break; /* goto sizeof (int) option return */ 9702 case IPV6_NEXTHOP: { 9703 sin6_t *sin6 = (sin6_t *)ptr; 9704 9705 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9706 return (0); 9707 *sin6 = sin6_null; 9708 sin6->sin6_family = AF_INET6; 9709 sin6->sin6_addr = ipp->ipp_nexthop; 9710 return (sizeof (sin6_t)); 9711 } 9712 case IPV6_HOPOPTS: 9713 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9714 return (0); 9715 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9716 return (0); 9717 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9718 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9719 if (tcp->tcp_label_len > 0) { 9720 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9721 ptr[1] = (ipp->ipp_hopoptslen - 9722 tcp->tcp_label_len + 7) / 8 - 1; 9723 } 9724 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9725 case IPV6_RTHDRDSTOPTS: 9726 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9727 return (0); 9728 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9729 return (ipp->ipp_rtdstoptslen); 9730 case IPV6_RTHDR: 9731 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9732 return (0); 9733 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9734 return (ipp->ipp_rthdrlen); 9735 case IPV6_DSTOPTS: 9736 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9737 return (0); 9738 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9739 return (ipp->ipp_dstoptslen); 9740 case IPV6_SRC_PREFERENCES: 9741 return (ip6_get_src_preferences(connp, 9742 (uint32_t *)ptr)); 9743 case IPV6_PATHMTU: { 9744 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9745 9746 if (tcp->tcp_state < TCPS_ESTABLISHED) 9747 return (-1); 9748 9749 return (ip_fill_mtuinfo(&connp->conn_remv6, 9750 connp->conn_fport, mtuinfo, 9751 connp->conn_netstack)); 9752 } 9753 default: 9754 return (-1); 9755 } 9756 break; 9757 default: 9758 return (-1); 9759 } 9760 return (sizeof (int)); 9761 } 9762 9763 /* 9764 * TCP routine to get the values of options. 9765 */ 9766 int 9767 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9768 { 9769 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9770 } 9771 9772 /* returns UNIX error, the optlen is a value-result arg */ 9773 int 9774 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9775 void *optvalp, socklen_t *optlen, cred_t *cr) 9776 { 9777 conn_t *connp = (conn_t *)proto_handle; 9778 squeue_t *sqp = connp->conn_sqp; 9779 int error; 9780 t_uscalar_t max_optbuf_len; 9781 void *optvalp_buf; 9782 int len; 9783 9784 ASSERT(connp->conn_upper_handle != NULL); 9785 9786 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9787 tcp_opt_obj.odb_opt_des_arr, 9788 tcp_opt_obj.odb_opt_arr_cnt, 9789 tcp_opt_obj.odb_topmost_tpiprovider, 9790 B_FALSE, B_TRUE, cr); 9791 if (error != 0) { 9792 if (error < 0) { 9793 error = proto_tlitosyserr(-error); 9794 } 9795 return (error); 9796 } 9797 9798 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9799 9800 error = squeue_synch_enter(sqp, connp, 0); 9801 if (error == ENOMEM) { 9802 return (ENOMEM); 9803 } 9804 9805 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9806 squeue_synch_exit(sqp, connp); 9807 9808 if (len < 0) { 9809 /* 9810 * Pass on to IP 9811 */ 9812 kmem_free(optvalp_buf, max_optbuf_len); 9813 return (ip_get_options(connp, level, option_name, 9814 optvalp, optlen, cr)); 9815 } else { 9816 /* 9817 * update optlen and copy option value 9818 */ 9819 t_uscalar_t size = MIN(len, *optlen); 9820 bcopy(optvalp_buf, optvalp, size); 9821 bcopy(&size, optlen, sizeof (size)); 9822 9823 kmem_free(optvalp_buf, max_optbuf_len); 9824 return (0); 9825 } 9826 } 9827 9828 /* 9829 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9830 * Parameters are assumed to be verified by the caller. 9831 */ 9832 /* ARGSUSED */ 9833 int 9834 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9835 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9836 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9837 { 9838 tcp_t *tcp = connp->conn_tcp; 9839 int *i1 = (int *)invalp; 9840 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9841 boolean_t checkonly; 9842 int reterr; 9843 tcp_stack_t *tcps = tcp->tcp_tcps; 9844 9845 switch (optset_context) { 9846 case SETFN_OPTCOM_CHECKONLY: 9847 checkonly = B_TRUE; 9848 /* 9849 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9850 * inlen != 0 implies value supplied and 9851 * we have to "pretend" to set it. 9852 * inlen == 0 implies that there is no 9853 * value part in T_CHECK request and just validation 9854 * done elsewhere should be enough, we just return here. 9855 */ 9856 if (inlen == 0) { 9857 *outlenp = 0; 9858 return (0); 9859 } 9860 break; 9861 case SETFN_OPTCOM_NEGOTIATE: 9862 checkonly = B_FALSE; 9863 break; 9864 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9865 case SETFN_CONN_NEGOTIATE: 9866 checkonly = B_FALSE; 9867 /* 9868 * Negotiating local and "association-related" options 9869 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9870 * primitives is allowed by XTI, but we choose 9871 * to not implement this style negotiation for Internet 9872 * protocols (We interpret it is a must for OSI world but 9873 * optional for Internet protocols) for all options. 9874 * [ Will do only for the few options that enable test 9875 * suites that our XTI implementation of this feature 9876 * works for transports that do allow it ] 9877 */ 9878 if (!tcp_allow_connopt_set(level, name)) { 9879 *outlenp = 0; 9880 return (EINVAL); 9881 } 9882 break; 9883 default: 9884 /* 9885 * We should never get here 9886 */ 9887 *outlenp = 0; 9888 return (EINVAL); 9889 } 9890 9891 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9892 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9893 9894 /* 9895 * For TCP, we should have no ancillary data sent down 9896 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9897 * has to be zero. 9898 */ 9899 ASSERT(thisdg_attrs == NULL); 9900 9901 /* 9902 * For fixed length options, no sanity check 9903 * of passed in length is done. It is assumed *_optcom_req() 9904 * routines do the right thing. 9905 */ 9906 switch (level) { 9907 case SOL_SOCKET: 9908 switch (name) { 9909 case SO_LINGER: { 9910 struct linger *lgr = (struct linger *)invalp; 9911 9912 if (!checkonly) { 9913 if (lgr->l_onoff) { 9914 tcp->tcp_linger = 1; 9915 tcp->tcp_lingertime = lgr->l_linger; 9916 } else { 9917 tcp->tcp_linger = 0; 9918 tcp->tcp_lingertime = 0; 9919 } 9920 /* struct copy */ 9921 *(struct linger *)outvalp = *lgr; 9922 } else { 9923 if (!lgr->l_onoff) { 9924 ((struct linger *) 9925 outvalp)->l_onoff = 0; 9926 ((struct linger *) 9927 outvalp)->l_linger = 0; 9928 } else { 9929 /* struct copy */ 9930 *(struct linger *)outvalp = *lgr; 9931 } 9932 } 9933 *outlenp = sizeof (struct linger); 9934 return (0); 9935 } 9936 case SO_DEBUG: 9937 if (!checkonly) 9938 tcp->tcp_debug = onoff; 9939 break; 9940 case SO_KEEPALIVE: 9941 if (checkonly) { 9942 /* check only case */ 9943 break; 9944 } 9945 9946 if (!onoff) { 9947 if (tcp->tcp_ka_enabled) { 9948 if (tcp->tcp_ka_tid != 0) { 9949 (void) TCP_TIMER_CANCEL(tcp, 9950 tcp->tcp_ka_tid); 9951 tcp->tcp_ka_tid = 0; 9952 } 9953 tcp->tcp_ka_enabled = 0; 9954 } 9955 break; 9956 } 9957 if (!tcp->tcp_ka_enabled) { 9958 /* Crank up the keepalive timer */ 9959 tcp->tcp_ka_last_intrvl = 0; 9960 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9961 tcp_keepalive_killer, 9962 MSEC_TO_TICK(tcp->tcp_ka_interval)); 9963 tcp->tcp_ka_enabled = 1; 9964 } 9965 break; 9966 case SO_DONTROUTE: 9967 /* 9968 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 9969 * only of interest to IP. We track them here only so 9970 * that we can report their current value. 9971 */ 9972 if (!checkonly) { 9973 tcp->tcp_dontroute = onoff; 9974 tcp->tcp_connp->conn_dontroute = onoff; 9975 } 9976 break; 9977 case SO_USELOOPBACK: 9978 if (!checkonly) { 9979 tcp->tcp_useloopback = onoff; 9980 tcp->tcp_connp->conn_loopback = onoff; 9981 } 9982 break; 9983 case SO_BROADCAST: 9984 if (!checkonly) { 9985 tcp->tcp_broadcast = onoff; 9986 tcp->tcp_connp->conn_broadcast = onoff; 9987 } 9988 break; 9989 case SO_REUSEADDR: 9990 if (!checkonly) { 9991 tcp->tcp_reuseaddr = onoff; 9992 tcp->tcp_connp->conn_reuseaddr = onoff; 9993 } 9994 break; 9995 case SO_OOBINLINE: 9996 if (!checkonly) { 9997 tcp->tcp_oobinline = onoff; 9998 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 9999 proto_set_rx_oob_opt(connp, onoff); 10000 } 10001 break; 10002 case SO_DGRAM_ERRIND: 10003 if (!checkonly) 10004 tcp->tcp_dgram_errind = onoff; 10005 break; 10006 case SO_SNDBUF: { 10007 if (*i1 > tcps->tcps_max_buf) { 10008 *outlenp = 0; 10009 return (ENOBUFS); 10010 } 10011 if (checkonly) 10012 break; 10013 10014 tcp->tcp_xmit_hiwater = *i1; 10015 if (tcps->tcps_snd_lowat_fraction != 0) 10016 tcp->tcp_xmit_lowater = 10017 tcp->tcp_xmit_hiwater / 10018 tcps->tcps_snd_lowat_fraction; 10019 (void) tcp_maxpsz_set(tcp, B_TRUE); 10020 /* 10021 * If we are flow-controlled, recheck the condition. 10022 * There are apps that increase SO_SNDBUF size when 10023 * flow-controlled (EWOULDBLOCK), and expect the flow 10024 * control condition to be lifted right away. 10025 */ 10026 mutex_enter(&tcp->tcp_non_sq_lock); 10027 if (tcp->tcp_flow_stopped && 10028 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10029 tcp_clrqfull(tcp); 10030 } 10031 mutex_exit(&tcp->tcp_non_sq_lock); 10032 break; 10033 } 10034 case SO_RCVBUF: 10035 if (*i1 > tcps->tcps_max_buf) { 10036 *outlenp = 0; 10037 return (ENOBUFS); 10038 } 10039 /* Silently ignore zero */ 10040 if (!checkonly && *i1 != 0) { 10041 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10042 (void) tcp_rwnd_set(tcp, *i1); 10043 } 10044 /* 10045 * XXX should we return the rwnd here 10046 * and tcp_opt_get ? 10047 */ 10048 break; 10049 case SO_SND_COPYAVOID: 10050 if (!checkonly) { 10051 /* we only allow enable at most once for now */ 10052 if (tcp->tcp_loopback || 10053 (tcp->tcp_kssl_ctx != NULL) || 10054 (!tcp->tcp_snd_zcopy_aware && 10055 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10056 *outlenp = 0; 10057 return (EOPNOTSUPP); 10058 } 10059 tcp->tcp_snd_zcopy_aware = 1; 10060 } 10061 break; 10062 case SO_RCVTIMEO: 10063 case SO_SNDTIMEO: 10064 /* 10065 * Pass these two options in order for third part 10066 * protocol usage. Here just return directly. 10067 */ 10068 return (0); 10069 case SO_ALLZONES: 10070 /* Pass option along to IP level for handling */ 10071 return (-EINVAL); 10072 case SO_ANON_MLP: 10073 /* Pass option along to IP level for handling */ 10074 return (-EINVAL); 10075 case SO_MAC_EXEMPT: 10076 /* Pass option along to IP level for handling */ 10077 return (-EINVAL); 10078 case SO_EXCLBIND: 10079 if (!checkonly) 10080 tcp->tcp_exclbind = onoff; 10081 break; 10082 default: 10083 *outlenp = 0; 10084 return (EINVAL); 10085 } 10086 break; 10087 case IPPROTO_TCP: 10088 switch (name) { 10089 case TCP_NODELAY: 10090 if (!checkonly) 10091 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10092 break; 10093 case TCP_NOTIFY_THRESHOLD: 10094 if (!checkonly) 10095 tcp->tcp_first_timer_threshold = *i1; 10096 break; 10097 case TCP_ABORT_THRESHOLD: 10098 if (!checkonly) 10099 tcp->tcp_second_timer_threshold = *i1; 10100 break; 10101 case TCP_CONN_NOTIFY_THRESHOLD: 10102 if (!checkonly) 10103 tcp->tcp_first_ctimer_threshold = *i1; 10104 break; 10105 case TCP_CONN_ABORT_THRESHOLD: 10106 if (!checkonly) 10107 tcp->tcp_second_ctimer_threshold = *i1; 10108 break; 10109 case TCP_RECVDSTADDR: 10110 if (tcp->tcp_state > TCPS_LISTEN) 10111 return (EOPNOTSUPP); 10112 if (!checkonly) 10113 tcp->tcp_recvdstaddr = onoff; 10114 break; 10115 case TCP_ANONPRIVBIND: 10116 if ((reterr = secpolicy_net_privaddr(cr, 0, 10117 IPPROTO_TCP)) != 0) { 10118 *outlenp = 0; 10119 return (reterr); 10120 } 10121 if (!checkonly) { 10122 tcp->tcp_anon_priv_bind = onoff; 10123 } 10124 break; 10125 case TCP_EXCLBIND: 10126 if (!checkonly) 10127 tcp->tcp_exclbind = onoff; 10128 break; /* goto sizeof (int) option return */ 10129 case TCP_INIT_CWND: { 10130 uint32_t init_cwnd = *((uint32_t *)invalp); 10131 10132 if (checkonly) 10133 break; 10134 10135 /* 10136 * Only allow socket with network configuration 10137 * privilege to set the initial cwnd to be larger 10138 * than allowed by RFC 3390. 10139 */ 10140 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10141 tcp->tcp_init_cwnd = init_cwnd; 10142 break; 10143 } 10144 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10145 *outlenp = 0; 10146 return (reterr); 10147 } 10148 if (init_cwnd > TCP_MAX_INIT_CWND) { 10149 *outlenp = 0; 10150 return (EINVAL); 10151 } 10152 tcp->tcp_init_cwnd = init_cwnd; 10153 break; 10154 } 10155 case TCP_KEEPALIVE_THRESHOLD: 10156 if (checkonly) 10157 break; 10158 10159 if (*i1 < tcps->tcps_keepalive_interval_low || 10160 *i1 > tcps->tcps_keepalive_interval_high) { 10161 *outlenp = 0; 10162 return (EINVAL); 10163 } 10164 if (*i1 != tcp->tcp_ka_interval) { 10165 tcp->tcp_ka_interval = *i1; 10166 /* 10167 * Check if we need to restart the 10168 * keepalive timer. 10169 */ 10170 if (tcp->tcp_ka_tid != 0) { 10171 ASSERT(tcp->tcp_ka_enabled); 10172 (void) TCP_TIMER_CANCEL(tcp, 10173 tcp->tcp_ka_tid); 10174 tcp->tcp_ka_last_intrvl = 0; 10175 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10176 tcp_keepalive_killer, 10177 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10178 } 10179 } 10180 break; 10181 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10182 if (!checkonly) { 10183 if (*i1 < 10184 tcps->tcps_keepalive_abort_interval_low || 10185 *i1 > 10186 tcps->tcps_keepalive_abort_interval_high) { 10187 *outlenp = 0; 10188 return (EINVAL); 10189 } 10190 tcp->tcp_ka_abort_thres = *i1; 10191 } 10192 break; 10193 case TCP_CORK: 10194 if (!checkonly) { 10195 /* 10196 * if tcp->tcp_cork was set and is now 10197 * being unset, we have to make sure that 10198 * the remaining data gets sent out. Also 10199 * unset tcp->tcp_cork so that tcp_wput_data() 10200 * can send data even if it is less than mss 10201 */ 10202 if (tcp->tcp_cork && onoff == 0 && 10203 tcp->tcp_unsent > 0) { 10204 tcp->tcp_cork = B_FALSE; 10205 tcp_wput_data(tcp, NULL, B_FALSE); 10206 } 10207 tcp->tcp_cork = onoff; 10208 } 10209 break; 10210 default: 10211 *outlenp = 0; 10212 return (EINVAL); 10213 } 10214 break; 10215 case IPPROTO_IP: 10216 if (tcp->tcp_family != AF_INET) { 10217 *outlenp = 0; 10218 return (ENOPROTOOPT); 10219 } 10220 switch (name) { 10221 case IP_OPTIONS: 10222 case T_IP_OPTIONS: 10223 reterr = tcp_opt_set_header(tcp, checkonly, 10224 invalp, inlen); 10225 if (reterr) { 10226 *outlenp = 0; 10227 return (reterr); 10228 } 10229 /* OK return - copy input buffer into output buffer */ 10230 if (invalp != outvalp) { 10231 /* don't trust bcopy for identical src/dst */ 10232 bcopy(invalp, outvalp, inlen); 10233 } 10234 *outlenp = inlen; 10235 return (0); 10236 case IP_TOS: 10237 case T_IP_TOS: 10238 if (!checkonly) { 10239 tcp->tcp_ipha->ipha_type_of_service = 10240 (uchar_t)*i1; 10241 tcp->tcp_tos = (uchar_t)*i1; 10242 } 10243 break; 10244 case IP_TTL: 10245 if (!checkonly) { 10246 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10247 tcp->tcp_ttl = (uchar_t)*i1; 10248 } 10249 break; 10250 case IP_BOUND_IF: 10251 case IP_NEXTHOP: 10252 /* Handled at the IP level */ 10253 return (-EINVAL); 10254 case IP_SEC_OPT: 10255 /* 10256 * We should not allow policy setting after 10257 * we start listening for connections. 10258 */ 10259 if (tcp->tcp_state == TCPS_LISTEN) { 10260 return (EINVAL); 10261 } else { 10262 /* Handled at the IP level */ 10263 return (-EINVAL); 10264 } 10265 default: 10266 *outlenp = 0; 10267 return (EINVAL); 10268 } 10269 break; 10270 case IPPROTO_IPV6: { 10271 ip6_pkt_t *ipp; 10272 10273 /* 10274 * IPPROTO_IPV6 options are only supported for sockets 10275 * that are using IPv6 on the wire. 10276 */ 10277 if (tcp->tcp_ipversion != IPV6_VERSION) { 10278 *outlenp = 0; 10279 return (ENOPROTOOPT); 10280 } 10281 /* 10282 * Only sticky options; no ancillary data 10283 */ 10284 ipp = &tcp->tcp_sticky_ipp; 10285 10286 switch (name) { 10287 case IPV6_UNICAST_HOPS: 10288 /* -1 means use default */ 10289 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10290 *outlenp = 0; 10291 return (EINVAL); 10292 } 10293 if (!checkonly) { 10294 if (*i1 == -1) { 10295 tcp->tcp_ip6h->ip6_hops = 10296 ipp->ipp_unicast_hops = 10297 (uint8_t)tcps->tcps_ipv6_hoplimit; 10298 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10299 /* Pass modified value to IP. */ 10300 *i1 = tcp->tcp_ip6h->ip6_hops; 10301 } else { 10302 tcp->tcp_ip6h->ip6_hops = 10303 ipp->ipp_unicast_hops = 10304 (uint8_t)*i1; 10305 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10306 } 10307 reterr = tcp_build_hdrs(tcp); 10308 if (reterr != 0) 10309 return (reterr); 10310 } 10311 break; 10312 case IPV6_BOUND_IF: 10313 if (!checkonly) { 10314 tcp->tcp_bound_if = *i1; 10315 PASS_OPT_TO_IP(connp); 10316 } 10317 break; 10318 /* 10319 * Set boolean switches for ancillary data delivery 10320 */ 10321 case IPV6_RECVPKTINFO: 10322 if (!checkonly) { 10323 if (onoff) 10324 tcp->tcp_ipv6_recvancillary |= 10325 TCP_IPV6_RECVPKTINFO; 10326 else 10327 tcp->tcp_ipv6_recvancillary &= 10328 ~TCP_IPV6_RECVPKTINFO; 10329 /* Force it to be sent up with the next msg */ 10330 tcp->tcp_recvifindex = 0; 10331 PASS_OPT_TO_IP(connp); 10332 } 10333 break; 10334 case IPV6_RECVTCLASS: 10335 if (!checkonly) { 10336 if (onoff) 10337 tcp->tcp_ipv6_recvancillary |= 10338 TCP_IPV6_RECVTCLASS; 10339 else 10340 tcp->tcp_ipv6_recvancillary &= 10341 ~TCP_IPV6_RECVTCLASS; 10342 PASS_OPT_TO_IP(connp); 10343 } 10344 break; 10345 case IPV6_RECVHOPLIMIT: 10346 if (!checkonly) { 10347 if (onoff) 10348 tcp->tcp_ipv6_recvancillary |= 10349 TCP_IPV6_RECVHOPLIMIT; 10350 else 10351 tcp->tcp_ipv6_recvancillary &= 10352 ~TCP_IPV6_RECVHOPLIMIT; 10353 /* Force it to be sent up with the next msg */ 10354 tcp->tcp_recvhops = 0xffffffffU; 10355 PASS_OPT_TO_IP(connp); 10356 } 10357 break; 10358 case IPV6_RECVHOPOPTS: 10359 if (!checkonly) { 10360 if (onoff) 10361 tcp->tcp_ipv6_recvancillary |= 10362 TCP_IPV6_RECVHOPOPTS; 10363 else 10364 tcp->tcp_ipv6_recvancillary &= 10365 ~TCP_IPV6_RECVHOPOPTS; 10366 PASS_OPT_TO_IP(connp); 10367 } 10368 break; 10369 case IPV6_RECVDSTOPTS: 10370 if (!checkonly) { 10371 if (onoff) 10372 tcp->tcp_ipv6_recvancillary |= 10373 TCP_IPV6_RECVDSTOPTS; 10374 else 10375 tcp->tcp_ipv6_recvancillary &= 10376 ~TCP_IPV6_RECVDSTOPTS; 10377 PASS_OPT_TO_IP(connp); 10378 } 10379 break; 10380 case _OLD_IPV6_RECVDSTOPTS: 10381 if (!checkonly) { 10382 if (onoff) 10383 tcp->tcp_ipv6_recvancillary |= 10384 TCP_OLD_IPV6_RECVDSTOPTS; 10385 else 10386 tcp->tcp_ipv6_recvancillary &= 10387 ~TCP_OLD_IPV6_RECVDSTOPTS; 10388 } 10389 break; 10390 case IPV6_RECVRTHDR: 10391 if (!checkonly) { 10392 if (onoff) 10393 tcp->tcp_ipv6_recvancillary |= 10394 TCP_IPV6_RECVRTHDR; 10395 else 10396 tcp->tcp_ipv6_recvancillary &= 10397 ~TCP_IPV6_RECVRTHDR; 10398 PASS_OPT_TO_IP(connp); 10399 } 10400 break; 10401 case IPV6_RECVRTHDRDSTOPTS: 10402 if (!checkonly) { 10403 if (onoff) 10404 tcp->tcp_ipv6_recvancillary |= 10405 TCP_IPV6_RECVRTDSTOPTS; 10406 else 10407 tcp->tcp_ipv6_recvancillary &= 10408 ~TCP_IPV6_RECVRTDSTOPTS; 10409 PASS_OPT_TO_IP(connp); 10410 } 10411 break; 10412 case IPV6_PKTINFO: 10413 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10414 return (EINVAL); 10415 if (checkonly) 10416 break; 10417 10418 if (inlen == 0) { 10419 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10420 } else { 10421 struct in6_pktinfo *pkti; 10422 10423 pkti = (struct in6_pktinfo *)invalp; 10424 /* 10425 * RFC 3542 states that ipi6_addr must be 10426 * the unspecified address when setting the 10427 * IPV6_PKTINFO sticky socket option on a 10428 * TCP socket. 10429 */ 10430 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10431 return (EINVAL); 10432 /* 10433 * IP will validate the source address and 10434 * interface index. 10435 */ 10436 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 10437 reterr = ip_set_options(tcp->tcp_connp, 10438 level, name, invalp, inlen, cr); 10439 } else { 10440 reterr = ip6_set_pktinfo(cr, 10441 tcp->tcp_connp, pkti); 10442 } 10443 if (reterr != 0) 10444 return (reterr); 10445 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10446 ipp->ipp_addr = pkti->ipi6_addr; 10447 if (ipp->ipp_ifindex != 0) 10448 ipp->ipp_fields |= IPPF_IFINDEX; 10449 else 10450 ipp->ipp_fields &= ~IPPF_IFINDEX; 10451 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10452 ipp->ipp_fields |= IPPF_ADDR; 10453 else 10454 ipp->ipp_fields &= ~IPPF_ADDR; 10455 } 10456 reterr = tcp_build_hdrs(tcp); 10457 if (reterr != 0) 10458 return (reterr); 10459 break; 10460 case IPV6_TCLASS: 10461 if (inlen != 0 && inlen != sizeof (int)) 10462 return (EINVAL); 10463 if (checkonly) 10464 break; 10465 10466 if (inlen == 0) { 10467 ipp->ipp_fields &= ~IPPF_TCLASS; 10468 } else { 10469 if (*i1 > 255 || *i1 < -1) 10470 return (EINVAL); 10471 if (*i1 == -1) { 10472 ipp->ipp_tclass = 0; 10473 *i1 = 0; 10474 } else { 10475 ipp->ipp_tclass = *i1; 10476 } 10477 ipp->ipp_fields |= IPPF_TCLASS; 10478 } 10479 reterr = tcp_build_hdrs(tcp); 10480 if (reterr != 0) 10481 return (reterr); 10482 break; 10483 case IPV6_NEXTHOP: 10484 /* 10485 * IP will verify that the nexthop is reachable 10486 * and fail for sticky options. 10487 */ 10488 if (inlen != 0 && inlen != sizeof (sin6_t)) 10489 return (EINVAL); 10490 if (checkonly) 10491 break; 10492 10493 if (inlen == 0) { 10494 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10495 } else { 10496 sin6_t *sin6 = (sin6_t *)invalp; 10497 10498 if (sin6->sin6_family != AF_INET6) 10499 return (EAFNOSUPPORT); 10500 if (IN6_IS_ADDR_V4MAPPED( 10501 &sin6->sin6_addr)) 10502 return (EADDRNOTAVAIL); 10503 ipp->ipp_nexthop = sin6->sin6_addr; 10504 if (!IN6_IS_ADDR_UNSPECIFIED( 10505 &ipp->ipp_nexthop)) 10506 ipp->ipp_fields |= IPPF_NEXTHOP; 10507 else 10508 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10509 } 10510 reterr = tcp_build_hdrs(tcp); 10511 if (reterr != 0) 10512 return (reterr); 10513 PASS_OPT_TO_IP(connp); 10514 break; 10515 case IPV6_HOPOPTS: { 10516 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10517 10518 /* 10519 * Sanity checks - minimum size, size a multiple of 10520 * eight bytes, and matching size passed in. 10521 */ 10522 if (inlen != 0 && 10523 inlen != (8 * (hopts->ip6h_len + 1))) 10524 return (EINVAL); 10525 10526 if (checkonly) 10527 break; 10528 10529 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10530 (uchar_t **)&ipp->ipp_hopopts, 10531 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10532 if (reterr != 0) 10533 return (reterr); 10534 if (ipp->ipp_hopoptslen == 0) 10535 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10536 else 10537 ipp->ipp_fields |= IPPF_HOPOPTS; 10538 reterr = tcp_build_hdrs(tcp); 10539 if (reterr != 0) 10540 return (reterr); 10541 break; 10542 } 10543 case IPV6_RTHDRDSTOPTS: { 10544 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10545 10546 /* 10547 * Sanity checks - minimum size, size a multiple of 10548 * eight bytes, and matching size passed in. 10549 */ 10550 if (inlen != 0 && 10551 inlen != (8 * (dopts->ip6d_len + 1))) 10552 return (EINVAL); 10553 10554 if (checkonly) 10555 break; 10556 10557 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10558 (uchar_t **)&ipp->ipp_rtdstopts, 10559 &ipp->ipp_rtdstoptslen, 0); 10560 if (reterr != 0) 10561 return (reterr); 10562 if (ipp->ipp_rtdstoptslen == 0) 10563 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10564 else 10565 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10566 reterr = tcp_build_hdrs(tcp); 10567 if (reterr != 0) 10568 return (reterr); 10569 break; 10570 } 10571 case IPV6_DSTOPTS: { 10572 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10573 10574 /* 10575 * Sanity checks - minimum size, size a multiple of 10576 * eight bytes, and matching size passed in. 10577 */ 10578 if (inlen != 0 && 10579 inlen != (8 * (dopts->ip6d_len + 1))) 10580 return (EINVAL); 10581 10582 if (checkonly) 10583 break; 10584 10585 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10586 (uchar_t **)&ipp->ipp_dstopts, 10587 &ipp->ipp_dstoptslen, 0); 10588 if (reterr != 0) 10589 return (reterr); 10590 if (ipp->ipp_dstoptslen == 0) 10591 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10592 else 10593 ipp->ipp_fields |= IPPF_DSTOPTS; 10594 reterr = tcp_build_hdrs(tcp); 10595 if (reterr != 0) 10596 return (reterr); 10597 break; 10598 } 10599 case IPV6_RTHDR: { 10600 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10601 10602 /* 10603 * Sanity checks - minimum size, size a multiple of 10604 * eight bytes, and matching size passed in. 10605 */ 10606 if (inlen != 0 && 10607 inlen != (8 * (rt->ip6r_len + 1))) 10608 return (EINVAL); 10609 10610 if (checkonly) 10611 break; 10612 10613 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10614 (uchar_t **)&ipp->ipp_rthdr, 10615 &ipp->ipp_rthdrlen, 0); 10616 if (reterr != 0) 10617 return (reterr); 10618 if (ipp->ipp_rthdrlen == 0) 10619 ipp->ipp_fields &= ~IPPF_RTHDR; 10620 else 10621 ipp->ipp_fields |= IPPF_RTHDR; 10622 reterr = tcp_build_hdrs(tcp); 10623 if (reterr != 0) 10624 return (reterr); 10625 break; 10626 } 10627 case IPV6_V6ONLY: 10628 if (!checkonly) { 10629 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10630 } 10631 break; 10632 case IPV6_USE_MIN_MTU: 10633 if (inlen != sizeof (int)) 10634 return (EINVAL); 10635 10636 if (*i1 < -1 || *i1 > 1) 10637 return (EINVAL); 10638 10639 if (checkonly) 10640 break; 10641 10642 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10643 ipp->ipp_use_min_mtu = *i1; 10644 break; 10645 case IPV6_SEC_OPT: 10646 /* 10647 * We should not allow policy setting after 10648 * we start listening for connections. 10649 */ 10650 if (tcp->tcp_state == TCPS_LISTEN) { 10651 return (EINVAL); 10652 } else { 10653 /* Handled at the IP level */ 10654 return (-EINVAL); 10655 } 10656 case IPV6_SRC_PREFERENCES: 10657 if (inlen != sizeof (uint32_t)) 10658 return (EINVAL); 10659 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10660 *(uint32_t *)invalp); 10661 if (reterr != 0) { 10662 *outlenp = 0; 10663 return (reterr); 10664 } 10665 break; 10666 default: 10667 *outlenp = 0; 10668 return (EINVAL); 10669 } 10670 break; 10671 } /* end IPPROTO_IPV6 */ 10672 default: 10673 *outlenp = 0; 10674 return (EINVAL); 10675 } 10676 /* 10677 * Common case of OK return with outval same as inval 10678 */ 10679 if (invalp != outvalp) { 10680 /* don't trust bcopy for identical src/dst */ 10681 (void) bcopy(invalp, outvalp, inlen); 10682 } 10683 *outlenp = inlen; 10684 return (0); 10685 } 10686 10687 /* ARGSUSED */ 10688 int 10689 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10690 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10691 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10692 { 10693 conn_t *connp = Q_TO_CONN(q); 10694 10695 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10696 outlenp, outvalp, thisdg_attrs, cr, mblk)); 10697 } 10698 10699 int 10700 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10701 const void *optvalp, socklen_t optlen, cred_t *cr) 10702 { 10703 conn_t *connp = (conn_t *)proto_handle; 10704 squeue_t *sqp = connp->conn_sqp; 10705 int error; 10706 10707 ASSERT(connp->conn_upper_handle != NULL); 10708 /* 10709 * Entering the squeue synchronously can result in a context switch, 10710 * which can cause a rather sever performance degradation. So we try to 10711 * handle whatever options we can without entering the squeue. 10712 */ 10713 if (level == IPPROTO_TCP) { 10714 switch (option_name) { 10715 case TCP_NODELAY: 10716 if (optlen != sizeof (int32_t)) 10717 return (EINVAL); 10718 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10719 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10720 connp->conn_tcp->tcp_mss; 10721 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10722 return (0); 10723 default: 10724 break; 10725 } 10726 } 10727 10728 error = squeue_synch_enter(sqp, connp, 0); 10729 if (error == ENOMEM) { 10730 return (ENOMEM); 10731 } 10732 10733 error = proto_opt_check(level, option_name, optlen, NULL, 10734 tcp_opt_obj.odb_opt_des_arr, 10735 tcp_opt_obj.odb_opt_arr_cnt, 10736 tcp_opt_obj.odb_topmost_tpiprovider, 10737 B_TRUE, B_FALSE, cr); 10738 10739 if (error != 0) { 10740 if (error < 0) { 10741 error = proto_tlitosyserr(-error); 10742 } 10743 squeue_synch_exit(sqp, connp); 10744 return (error); 10745 } 10746 10747 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10748 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10749 NULL, cr, NULL); 10750 squeue_synch_exit(sqp, connp); 10751 10752 if (error < 0) { 10753 /* 10754 * Pass on to ip 10755 */ 10756 error = ip_set_options(connp, level, option_name, optvalp, 10757 optlen, cr); 10758 } 10759 return (error); 10760 } 10761 10762 /* 10763 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10764 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10765 * headers, and the maximum size tcp header (to avoid reallocation 10766 * on the fly for additional tcp options). 10767 * Returns failure if can't allocate memory. 10768 */ 10769 static int 10770 tcp_build_hdrs(tcp_t *tcp) 10771 { 10772 char *hdrs; 10773 uint_t hdrs_len; 10774 ip6i_t *ip6i; 10775 char buf[TCP_MAX_HDR_LENGTH]; 10776 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10777 in6_addr_t src, dst; 10778 tcp_stack_t *tcps = tcp->tcp_tcps; 10779 conn_t *connp = tcp->tcp_connp; 10780 10781 /* 10782 * save the existing tcp header and source/dest IP addresses 10783 */ 10784 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10785 src = tcp->tcp_ip6h->ip6_src; 10786 dst = tcp->tcp_ip6h->ip6_dst; 10787 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10788 ASSERT(hdrs_len != 0); 10789 if (hdrs_len > tcp->tcp_iphc_len) { 10790 /* Need to reallocate */ 10791 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10792 if (hdrs == NULL) 10793 return (ENOMEM); 10794 if (tcp->tcp_iphc != NULL) { 10795 if (tcp->tcp_hdr_grown) { 10796 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10797 } else { 10798 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10799 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10800 } 10801 tcp->tcp_iphc_len = 0; 10802 } 10803 ASSERT(tcp->tcp_iphc_len == 0); 10804 tcp->tcp_iphc = hdrs; 10805 tcp->tcp_iphc_len = hdrs_len; 10806 tcp->tcp_hdr_grown = B_TRUE; 10807 } 10808 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10809 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10810 10811 /* Set header fields not in ipp */ 10812 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10813 ip6i = (ip6i_t *)tcp->tcp_iphc; 10814 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10815 } else { 10816 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10817 } 10818 /* 10819 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10820 * 10821 * tcp->tcp_tcp_hdr_len doesn't change here. 10822 */ 10823 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10824 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10825 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10826 10827 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10828 10829 tcp->tcp_ip6h->ip6_src = src; 10830 tcp->tcp_ip6h->ip6_dst = dst; 10831 10832 /* 10833 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10834 * the default value for TCP. 10835 */ 10836 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10837 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10838 10839 /* 10840 * If we're setting extension headers after a connection 10841 * has been established, and if we have a routing header 10842 * among the extension headers, call ip_massage_options_v6 to 10843 * manipulate the routing header/ip6_dst set the checksum 10844 * difference in the tcp header template. 10845 * (This happens in tcp_connect_ipv6 if the routing header 10846 * is set prior to the connect.) 10847 * Set the tcp_sum to zero first in case we've cleared a 10848 * routing header or don't have one at all. 10849 */ 10850 tcp->tcp_sum = 0; 10851 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10852 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10853 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10854 (uint8_t *)tcp->tcp_tcph); 10855 if (rth != NULL) { 10856 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10857 rth, tcps->tcps_netstack); 10858 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10859 (tcp->tcp_sum >> 16)); 10860 } 10861 } 10862 10863 /* Try to get everything in a single mblk */ 10864 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10865 hdrs_len + tcps->tcps_wroff_xtra); 10866 return (0); 10867 } 10868 10869 /* 10870 * Transfer any source route option from ipha to buf/dst in reversed form. 10871 */ 10872 static int 10873 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10874 { 10875 ipoptp_t opts; 10876 uchar_t *opt; 10877 uint8_t optval; 10878 uint8_t optlen; 10879 uint32_t len = 0; 10880 10881 for (optval = ipoptp_first(&opts, ipha); 10882 optval != IPOPT_EOL; 10883 optval = ipoptp_next(&opts)) { 10884 opt = opts.ipoptp_cur; 10885 optlen = opts.ipoptp_len; 10886 switch (optval) { 10887 int off1, off2; 10888 case IPOPT_SSRR: 10889 case IPOPT_LSRR: 10890 10891 /* Reverse source route */ 10892 /* 10893 * First entry should be the next to last one in the 10894 * current source route (the last entry is our 10895 * address.) 10896 * The last entry should be the final destination. 10897 */ 10898 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10899 buf[IPOPT_OLEN] = (uint8_t)optlen; 10900 off1 = IPOPT_MINOFF_SR - 1; 10901 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10902 if (off2 < 0) { 10903 /* No entries in source route */ 10904 break; 10905 } 10906 bcopy(opt + off2, dst, IP_ADDR_LEN); 10907 /* 10908 * Note: use src since ipha has not had its src 10909 * and dst reversed (it is in the state it was 10910 * received. 10911 */ 10912 bcopy(&ipha->ipha_src, buf + off2, 10913 IP_ADDR_LEN); 10914 off2 -= IP_ADDR_LEN; 10915 10916 while (off2 > 0) { 10917 bcopy(opt + off2, buf + off1, 10918 IP_ADDR_LEN); 10919 off1 += IP_ADDR_LEN; 10920 off2 -= IP_ADDR_LEN; 10921 } 10922 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 10923 buf += optlen; 10924 len += optlen; 10925 break; 10926 } 10927 } 10928 done: 10929 /* Pad the resulting options */ 10930 while (len & 0x3) { 10931 *buf++ = IPOPT_EOL; 10932 len++; 10933 } 10934 return (len); 10935 } 10936 10937 10938 /* 10939 * Extract and revert a source route from ipha (if any) 10940 * and then update the relevant fields in both tcp_t and the standard header. 10941 */ 10942 static void 10943 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 10944 { 10945 char buf[TCP_MAX_HDR_LENGTH]; 10946 uint_t tcph_len; 10947 int len; 10948 10949 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 10950 len = IPH_HDR_LENGTH(ipha); 10951 if (len == IP_SIMPLE_HDR_LENGTH) 10952 /* Nothing to do */ 10953 return; 10954 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 10955 (len & 0x3)) 10956 return; 10957 10958 tcph_len = tcp->tcp_tcp_hdr_len; 10959 bcopy(tcp->tcp_tcph, buf, tcph_len); 10960 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 10961 (tcp->tcp_ipha->ipha_dst & 0xffff); 10962 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 10963 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 10964 len += IP_SIMPLE_HDR_LENGTH; 10965 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 10966 (tcp->tcp_ipha->ipha_dst & 0xffff)); 10967 if ((int)tcp->tcp_sum < 0) 10968 tcp->tcp_sum--; 10969 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 10970 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 10971 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 10972 bcopy(buf, tcp->tcp_tcph, tcph_len); 10973 tcp->tcp_ip_hdr_len = len; 10974 tcp->tcp_ipha->ipha_version_and_hdr_length = 10975 (IP_VERSION << 4) | (len >> 2); 10976 len += tcph_len; 10977 tcp->tcp_hdr_len = len; 10978 } 10979 10980 /* 10981 * Copy the standard header into its new location, 10982 * lay in the new options and then update the relevant 10983 * fields in both tcp_t and the standard header. 10984 */ 10985 static int 10986 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 10987 { 10988 uint_t tcph_len; 10989 uint8_t *ip_optp; 10990 tcph_t *new_tcph; 10991 tcp_stack_t *tcps = tcp->tcp_tcps; 10992 conn_t *connp = tcp->tcp_connp; 10993 10994 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 10995 return (EINVAL); 10996 10997 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 10998 return (EINVAL); 10999 11000 if (checkonly) { 11001 /* 11002 * do not really set, just pretend to - T_CHECK 11003 */ 11004 return (0); 11005 } 11006 11007 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11008 if (tcp->tcp_label_len > 0) { 11009 int padlen; 11010 uint8_t opt; 11011 11012 /* convert list termination to no-ops */ 11013 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11014 ip_optp += ip_optp[IPOPT_OLEN]; 11015 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11016 while (--padlen >= 0) 11017 *ip_optp++ = opt; 11018 } 11019 tcph_len = tcp->tcp_tcp_hdr_len; 11020 new_tcph = (tcph_t *)(ip_optp + len); 11021 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11022 tcp->tcp_tcph = new_tcph; 11023 bcopy(ptr, ip_optp, len); 11024 11025 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11026 11027 tcp->tcp_ip_hdr_len = len; 11028 tcp->tcp_ipha->ipha_version_and_hdr_length = 11029 (IP_VERSION << 4) | (len >> 2); 11030 tcp->tcp_hdr_len = len + tcph_len; 11031 if (!TCP_IS_DETACHED(tcp)) { 11032 /* Always allocate room for all options. */ 11033 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11034 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11035 } 11036 return (0); 11037 } 11038 11039 /* Get callback routine passed to nd_load by tcp_param_register */ 11040 /* ARGSUSED */ 11041 static int 11042 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11043 { 11044 tcpparam_t *tcppa = (tcpparam_t *)cp; 11045 11046 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11047 return (0); 11048 } 11049 11050 /* 11051 * Walk through the param array specified registering each element with the 11052 * named dispatch handler. 11053 */ 11054 static boolean_t 11055 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11056 { 11057 for (; cnt-- > 0; tcppa++) { 11058 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11059 if (!nd_load(ndp, tcppa->tcp_param_name, 11060 tcp_param_get, tcp_param_set, 11061 (caddr_t)tcppa)) { 11062 nd_free(ndp); 11063 return (B_FALSE); 11064 } 11065 } 11066 } 11067 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11068 KM_SLEEP); 11069 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11070 sizeof (tcpparam_t)); 11071 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11072 tcp_param_get, tcp_param_set_aligned, 11073 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11074 nd_free(ndp); 11075 return (B_FALSE); 11076 } 11077 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11078 KM_SLEEP); 11079 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11080 sizeof (tcpparam_t)); 11081 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11082 tcp_param_get, tcp_param_set_aligned, 11083 (caddr_t)tcps->tcps_mdt_head_param)) { 11084 nd_free(ndp); 11085 return (B_FALSE); 11086 } 11087 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11088 KM_SLEEP); 11089 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11090 sizeof (tcpparam_t)); 11091 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11092 tcp_param_get, tcp_param_set_aligned, 11093 (caddr_t)tcps->tcps_mdt_tail_param)) { 11094 nd_free(ndp); 11095 return (B_FALSE); 11096 } 11097 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11098 KM_SLEEP); 11099 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11100 sizeof (tcpparam_t)); 11101 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11102 tcp_param_get, tcp_param_set_aligned, 11103 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11104 nd_free(ndp); 11105 return (B_FALSE); 11106 } 11107 if (!nd_load(ndp, "tcp_extra_priv_ports", 11108 tcp_extra_priv_ports_get, NULL, NULL)) { 11109 nd_free(ndp); 11110 return (B_FALSE); 11111 } 11112 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11113 NULL, tcp_extra_priv_ports_add, NULL)) { 11114 nd_free(ndp); 11115 return (B_FALSE); 11116 } 11117 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11118 NULL, tcp_extra_priv_ports_del, NULL)) { 11119 nd_free(ndp); 11120 return (B_FALSE); 11121 } 11122 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11123 tcp_1948_phrase_set, NULL)) { 11124 nd_free(ndp); 11125 return (B_FALSE); 11126 } 11127 /* 11128 * Dummy ndd variables - only to convey obsolescence information 11129 * through printing of their name (no get or set routines) 11130 * XXX Remove in future releases ? 11131 */ 11132 if (!nd_load(ndp, 11133 "tcp_close_wait_interval(obsoleted - " 11134 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11135 nd_free(ndp); 11136 return (B_FALSE); 11137 } 11138 return (B_TRUE); 11139 } 11140 11141 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11142 /* ARGSUSED */ 11143 static int 11144 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11145 cred_t *cr) 11146 { 11147 long new_value; 11148 tcpparam_t *tcppa = (tcpparam_t *)cp; 11149 11150 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11151 new_value < tcppa->tcp_param_min || 11152 new_value > tcppa->tcp_param_max) { 11153 return (EINVAL); 11154 } 11155 /* 11156 * Need to make sure new_value is a multiple of 4. If it is not, 11157 * round it up. For future 64 bit requirement, we actually make it 11158 * a multiple of 8. 11159 */ 11160 if (new_value & 0x7) { 11161 new_value = (new_value & ~0x7) + 0x8; 11162 } 11163 tcppa->tcp_param_val = new_value; 11164 return (0); 11165 } 11166 11167 /* Set callback routine passed to nd_load by tcp_param_register */ 11168 /* ARGSUSED */ 11169 static int 11170 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11171 { 11172 long new_value; 11173 tcpparam_t *tcppa = (tcpparam_t *)cp; 11174 11175 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11176 new_value < tcppa->tcp_param_min || 11177 new_value > tcppa->tcp_param_max) { 11178 return (EINVAL); 11179 } 11180 tcppa->tcp_param_val = new_value; 11181 return (0); 11182 } 11183 11184 /* 11185 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11186 * is filled, return as much as we can. The message passed in may be 11187 * multi-part, chained using b_cont. "start" is the starting sequence 11188 * number for this piece. 11189 */ 11190 static mblk_t * 11191 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11192 { 11193 uint32_t end; 11194 mblk_t *mp1; 11195 mblk_t *mp2; 11196 mblk_t *next_mp; 11197 uint32_t u1; 11198 tcp_stack_t *tcps = tcp->tcp_tcps; 11199 11200 /* Walk through all the new pieces. */ 11201 do { 11202 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11203 (uintptr_t)INT_MAX); 11204 end = start + (int)(mp->b_wptr - mp->b_rptr); 11205 next_mp = mp->b_cont; 11206 if (start == end) { 11207 /* Empty. Blast it. */ 11208 freeb(mp); 11209 continue; 11210 } 11211 mp->b_cont = NULL; 11212 TCP_REASS_SET_SEQ(mp, start); 11213 TCP_REASS_SET_END(mp, end); 11214 mp1 = tcp->tcp_reass_tail; 11215 if (!mp1) { 11216 tcp->tcp_reass_tail = mp; 11217 tcp->tcp_reass_head = mp; 11218 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11219 UPDATE_MIB(&tcps->tcps_mib, 11220 tcpInDataUnorderBytes, end - start); 11221 continue; 11222 } 11223 /* New stuff completely beyond tail? */ 11224 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11225 /* Link it on end. */ 11226 mp1->b_cont = mp; 11227 tcp->tcp_reass_tail = mp; 11228 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11229 UPDATE_MIB(&tcps->tcps_mib, 11230 tcpInDataUnorderBytes, end - start); 11231 continue; 11232 } 11233 mp1 = tcp->tcp_reass_head; 11234 u1 = TCP_REASS_SEQ(mp1); 11235 /* New stuff at the front? */ 11236 if (SEQ_LT(start, u1)) { 11237 /* Yes... Check for overlap. */ 11238 mp->b_cont = mp1; 11239 tcp->tcp_reass_head = mp; 11240 tcp_reass_elim_overlap(tcp, mp); 11241 continue; 11242 } 11243 /* 11244 * The new piece fits somewhere between the head and tail. 11245 * We find our slot, where mp1 precedes us and mp2 trails. 11246 */ 11247 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11248 u1 = TCP_REASS_SEQ(mp2); 11249 if (SEQ_LEQ(start, u1)) 11250 break; 11251 } 11252 /* Link ourselves in */ 11253 mp->b_cont = mp2; 11254 mp1->b_cont = mp; 11255 11256 /* Trim overlap with following mblk(s) first */ 11257 tcp_reass_elim_overlap(tcp, mp); 11258 11259 /* Trim overlap with preceding mblk */ 11260 tcp_reass_elim_overlap(tcp, mp1); 11261 11262 } while (start = end, mp = next_mp); 11263 mp1 = tcp->tcp_reass_head; 11264 /* Anything ready to go? */ 11265 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11266 return (NULL); 11267 /* Eat what we can off the queue */ 11268 for (;;) { 11269 mp = mp1->b_cont; 11270 end = TCP_REASS_END(mp1); 11271 TCP_REASS_SET_SEQ(mp1, 0); 11272 TCP_REASS_SET_END(mp1, 0); 11273 if (!mp) { 11274 tcp->tcp_reass_tail = NULL; 11275 break; 11276 } 11277 if (end != TCP_REASS_SEQ(mp)) { 11278 mp1->b_cont = NULL; 11279 break; 11280 } 11281 mp1 = mp; 11282 } 11283 mp1 = tcp->tcp_reass_head; 11284 tcp->tcp_reass_head = mp; 11285 return (mp1); 11286 } 11287 11288 /* Eliminate any overlap that mp may have over later mblks */ 11289 static void 11290 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11291 { 11292 uint32_t end; 11293 mblk_t *mp1; 11294 uint32_t u1; 11295 tcp_stack_t *tcps = tcp->tcp_tcps; 11296 11297 end = TCP_REASS_END(mp); 11298 while ((mp1 = mp->b_cont) != NULL) { 11299 u1 = TCP_REASS_SEQ(mp1); 11300 if (!SEQ_GT(end, u1)) 11301 break; 11302 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11303 mp->b_wptr -= end - u1; 11304 TCP_REASS_SET_END(mp, u1); 11305 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11306 UPDATE_MIB(&tcps->tcps_mib, 11307 tcpInDataPartDupBytes, end - u1); 11308 break; 11309 } 11310 mp->b_cont = mp1->b_cont; 11311 TCP_REASS_SET_SEQ(mp1, 0); 11312 TCP_REASS_SET_END(mp1, 0); 11313 freeb(mp1); 11314 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11315 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11316 } 11317 if (!mp1) 11318 tcp->tcp_reass_tail = mp; 11319 } 11320 11321 static uint_t 11322 tcp_rwnd_reopen(tcp_t *tcp) 11323 { 11324 uint_t ret = 0; 11325 uint_t thwin; 11326 11327 /* Learn the latest rwnd information that we sent to the other side. */ 11328 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11329 << tcp->tcp_rcv_ws; 11330 /* This is peer's calculated send window (our receive window). */ 11331 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11332 /* 11333 * Increase the receive window to max. But we need to do receiver 11334 * SWS avoidance. This means that we need to check the increase of 11335 * of receive window is at least 1 MSS. 11336 */ 11337 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11338 /* 11339 * If the window that the other side knows is less than max 11340 * deferred acks segments, send an update immediately. 11341 */ 11342 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11343 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11344 ret = TH_ACK_NEEDED; 11345 } 11346 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11347 } 11348 return (ret); 11349 } 11350 11351 /* 11352 * Send up all messages queued on tcp_rcv_list. 11353 */ 11354 static uint_t 11355 tcp_rcv_drain(tcp_t *tcp) 11356 { 11357 mblk_t *mp; 11358 uint_t ret = 0; 11359 #ifdef DEBUG 11360 uint_t cnt = 0; 11361 #endif 11362 queue_t *q = tcp->tcp_rq; 11363 11364 /* Can't drain on an eager connection */ 11365 if (tcp->tcp_listener != NULL) 11366 return (ret); 11367 11368 /* Can't be a non-STREAMS connection */ 11369 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 11370 11371 /* No need for the push timer now. */ 11372 if (tcp->tcp_push_tid != 0) { 11373 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11374 tcp->tcp_push_tid = 0; 11375 } 11376 11377 /* 11378 * Handle two cases here: we are currently fused or we were 11379 * previously fused and have some urgent data to be delivered 11380 * upstream. The latter happens because we either ran out of 11381 * memory or were detached and therefore sending the SIGURG was 11382 * deferred until this point. In either case we pass control 11383 * over to tcp_fuse_rcv_drain() since it may need to complete 11384 * some work. 11385 */ 11386 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11387 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11388 tcp->tcp_fused_sigurg_mp != NULL); 11389 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11390 &tcp->tcp_fused_sigurg_mp)) 11391 return (ret); 11392 } 11393 11394 while ((mp = tcp->tcp_rcv_list) != NULL) { 11395 tcp->tcp_rcv_list = mp->b_next; 11396 mp->b_next = NULL; 11397 #ifdef DEBUG 11398 cnt += msgdsize(mp); 11399 #endif 11400 /* Does this need SSL processing first? */ 11401 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11402 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11403 mblk_t *, mp); 11404 tcp_kssl_input(tcp, mp); 11405 continue; 11406 } 11407 putnext(q, mp); 11408 } 11409 #ifdef DEBUG 11410 ASSERT(cnt == tcp->tcp_rcv_cnt); 11411 #endif 11412 tcp->tcp_rcv_last_head = NULL; 11413 tcp->tcp_rcv_last_tail = NULL; 11414 tcp->tcp_rcv_cnt = 0; 11415 11416 if (canputnext(q)) 11417 return (tcp_rwnd_reopen(tcp)); 11418 11419 return (ret); 11420 } 11421 11422 /* 11423 * Queue data on tcp_rcv_list which is a b_next chain. 11424 * tcp_rcv_last_head/tail is the last element of this chain. 11425 * Each element of the chain is a b_cont chain. 11426 * 11427 * M_DATA messages are added to the current element. 11428 * Other messages are added as new (b_next) elements. 11429 */ 11430 void 11431 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11432 { 11433 ASSERT(seg_len == msgdsize(mp)); 11434 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11435 11436 if (tcp->tcp_rcv_list == NULL) { 11437 ASSERT(tcp->tcp_rcv_last_head == NULL); 11438 tcp->tcp_rcv_list = mp; 11439 tcp->tcp_rcv_last_head = mp; 11440 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11441 tcp->tcp_rcv_last_tail->b_cont = mp; 11442 } else { 11443 tcp->tcp_rcv_last_head->b_next = mp; 11444 tcp->tcp_rcv_last_head = mp; 11445 } 11446 11447 while (mp->b_cont) 11448 mp = mp->b_cont; 11449 11450 tcp->tcp_rcv_last_tail = mp; 11451 tcp->tcp_rcv_cnt += seg_len; 11452 tcp->tcp_rwnd -= seg_len; 11453 } 11454 11455 /* 11456 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11457 * 11458 * This is the default entry function into TCP on the read side. TCP is 11459 * always entered via squeue i.e. using squeue's for mutual exclusion. 11460 * When classifier does a lookup to find the tcp, it also puts a reference 11461 * on the conn structure associated so the tcp is guaranteed to exist 11462 * when we come here. We still need to check the state because it might 11463 * as well has been closed. The squeue processing function i.e. squeue_enter, 11464 * is responsible for doing the CONN_DEC_REF. 11465 * 11466 * Apart from the default entry point, IP also sends packets directly to 11467 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11468 * connections. 11469 */ 11470 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11471 void 11472 tcp_input(void *arg, mblk_t *mp, void *arg2) 11473 { 11474 conn_t *connp = (conn_t *)arg; 11475 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11476 11477 /* arg2 is the sqp */ 11478 ASSERT(arg2 != NULL); 11479 ASSERT(mp != NULL); 11480 11481 /* 11482 * Don't accept any input on a closed tcp as this TCP logically does 11483 * not exist on the system. Don't proceed further with this TCP. 11484 * For eg. this packet could trigger another close of this tcp 11485 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11486 * tcp_clean_death / tcp_closei_local must be called at most once 11487 * on a TCP. In this case we need to refeed the packet into the 11488 * classifier and figure out where the packet should go. Need to 11489 * preserve the recv_ill somehow. Until we figure that out, for 11490 * now just drop the packet if we can't classify the packet. 11491 */ 11492 if (tcp->tcp_state == TCPS_CLOSED || 11493 tcp->tcp_state == TCPS_BOUND) { 11494 conn_t *new_connp; 11495 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11496 11497 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11498 if (new_connp != NULL) { 11499 tcp_reinput(new_connp, mp, arg2); 11500 return; 11501 } 11502 /* We failed to classify. For now just drop the packet */ 11503 freemsg(mp); 11504 return; 11505 } 11506 11507 if (DB_TYPE(mp) != M_DATA) { 11508 tcp_rput_common(tcp, mp); 11509 return; 11510 } 11511 11512 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11513 squeue_t *final_sqp; 11514 11515 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11516 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11517 DB_CKSUMSTART(mp) = 0; 11518 if (tcp->tcp_state == TCPS_SYN_SENT && 11519 connp->conn_final_sqp == NULL && 11520 tcp_outbound_squeue_switch) { 11521 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11522 connp->conn_final_sqp = final_sqp; 11523 if (connp->conn_final_sqp != connp->conn_sqp) { 11524 CONN_INC_REF(connp); 11525 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11526 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11527 tcp_rput_data, connp, ip_squeue_flag, 11528 SQTAG_CONNECT_FINISH); 11529 return; 11530 } 11531 } 11532 } 11533 tcp_rput_data(connp, mp, arg2); 11534 } 11535 11536 /* 11537 * The read side put procedure. 11538 * The packets passed up by ip are assume to be aligned according to 11539 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11540 */ 11541 static void 11542 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11543 { 11544 /* 11545 * tcp_rput_data() does not expect M_CTL except for the case 11546 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11547 * type. Need to make sure that any other M_CTLs don't make 11548 * it to tcp_rput_data since it is not expecting any and doesn't 11549 * check for it. 11550 */ 11551 if (DB_TYPE(mp) == M_CTL) { 11552 switch (*(uint32_t *)(mp->b_rptr)) { 11553 case TCP_IOC_ABORT_CONN: 11554 /* 11555 * Handle connection abort request. 11556 */ 11557 tcp_ioctl_abort_handler(tcp, mp); 11558 return; 11559 case IPSEC_IN: 11560 /* 11561 * Only secure icmp arrive in TCP and they 11562 * don't go through data path. 11563 */ 11564 tcp_icmp_error(tcp, mp); 11565 return; 11566 case IN_PKTINFO: 11567 /* 11568 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11569 * sockets that are receiving IPv4 traffic. tcp 11570 */ 11571 ASSERT(tcp->tcp_family == AF_INET6); 11572 ASSERT(tcp->tcp_ipv6_recvancillary & 11573 TCP_IPV6_RECVPKTINFO); 11574 tcp_rput_data(tcp->tcp_connp, mp, 11575 tcp->tcp_connp->conn_sqp); 11576 return; 11577 case MDT_IOC_INFO_UPDATE: 11578 /* 11579 * Handle Multidata information update; the 11580 * following routine will free the message. 11581 */ 11582 if (tcp->tcp_connp->conn_mdt_ok) { 11583 tcp_mdt_update(tcp, 11584 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11585 B_FALSE); 11586 } 11587 freemsg(mp); 11588 return; 11589 case LSO_IOC_INFO_UPDATE: 11590 /* 11591 * Handle LSO information update; the following 11592 * routine will free the message. 11593 */ 11594 if (tcp->tcp_connp->conn_lso_ok) { 11595 tcp_lso_update(tcp, 11596 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11597 } 11598 freemsg(mp); 11599 return; 11600 default: 11601 /* 11602 * tcp_icmp_err() will process the M_CTL packets. 11603 * Non-ICMP packets, if any, will be discarded in 11604 * tcp_icmp_err(). We will process the ICMP packet 11605 * even if we are TCP_IS_DETACHED_NONEAGER as the 11606 * incoming ICMP packet may result in changing 11607 * the tcp_mss, which we would need if we have 11608 * packets to retransmit. 11609 */ 11610 tcp_icmp_error(tcp, mp); 11611 return; 11612 } 11613 } 11614 11615 /* No point processing the message if tcp is already closed */ 11616 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11617 freemsg(mp); 11618 return; 11619 } 11620 11621 tcp_rput_other(tcp, mp); 11622 } 11623 11624 11625 /* The minimum of smoothed mean deviation in RTO calculation. */ 11626 #define TCP_SD_MIN 400 11627 11628 /* 11629 * Set RTO for this connection. The formula is from Jacobson and Karels' 11630 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11631 * are the same as those in Appendix A.2 of that paper. 11632 * 11633 * m = new measurement 11634 * sa = smoothed RTT average (8 * average estimates). 11635 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11636 */ 11637 static void 11638 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11639 { 11640 long m = TICK_TO_MSEC(rtt); 11641 clock_t sa = tcp->tcp_rtt_sa; 11642 clock_t sv = tcp->tcp_rtt_sd; 11643 clock_t rto; 11644 tcp_stack_t *tcps = tcp->tcp_tcps; 11645 11646 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11647 tcp->tcp_rtt_update++; 11648 11649 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11650 if (sa != 0) { 11651 /* 11652 * Update average estimator: 11653 * new rtt = 7/8 old rtt + 1/8 Error 11654 */ 11655 11656 /* m is now Error in estimate. */ 11657 m -= sa >> 3; 11658 if ((sa += m) <= 0) { 11659 /* 11660 * Don't allow the smoothed average to be negative. 11661 * We use 0 to denote reinitialization of the 11662 * variables. 11663 */ 11664 sa = 1; 11665 } 11666 11667 /* 11668 * Update deviation estimator: 11669 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11670 */ 11671 if (m < 0) 11672 m = -m; 11673 m -= sv >> 2; 11674 sv += m; 11675 } else { 11676 /* 11677 * This follows BSD's implementation. So the reinitialized 11678 * RTO is 3 * m. We cannot go less than 2 because if the 11679 * link is bandwidth dominated, doubling the window size 11680 * during slow start means doubling the RTT. We want to be 11681 * more conservative when we reinitialize our estimates. 3 11682 * is just a convenient number. 11683 */ 11684 sa = m << 3; 11685 sv = m << 1; 11686 } 11687 if (sv < TCP_SD_MIN) { 11688 /* 11689 * We do not know that if sa captures the delay ACK 11690 * effect as in a long train of segments, a receiver 11691 * does not delay its ACKs. So set the minimum of sv 11692 * to be TCP_SD_MIN, which is default to 400 ms, twice 11693 * of BSD DATO. That means the minimum of mean 11694 * deviation is 100 ms. 11695 * 11696 */ 11697 sv = TCP_SD_MIN; 11698 } 11699 tcp->tcp_rtt_sa = sa; 11700 tcp->tcp_rtt_sd = sv; 11701 /* 11702 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11703 * 11704 * Add tcp_rexmit_interval extra in case of extreme environment 11705 * where the algorithm fails to work. The default value of 11706 * tcp_rexmit_interval_extra should be 0. 11707 * 11708 * As we use a finer grained clock than BSD and update 11709 * RTO for every ACKs, add in another .25 of RTT to the 11710 * deviation of RTO to accomodate burstiness of 1/4 of 11711 * window size. 11712 */ 11713 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 11714 11715 if (rto > tcps->tcps_rexmit_interval_max) { 11716 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 11717 } else if (rto < tcps->tcps_rexmit_interval_min) { 11718 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 11719 } else { 11720 tcp->tcp_rto = rto; 11721 } 11722 11723 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11724 tcp->tcp_timer_backoff = 0; 11725 } 11726 11727 /* 11728 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11729 * send queue which starts at the given seq. no. 11730 * 11731 * Parameters: 11732 * tcp_t *tcp: the tcp instance pointer. 11733 * uint32_t seq: the starting seq. no of the requested segment. 11734 * int32_t *off: after the execution, *off will be the offset to 11735 * the returned mblk which points to the requested seq no. 11736 * It is the caller's responsibility to send in a non-null off. 11737 * 11738 * Return: 11739 * A mblk_t pointer pointing to the requested segment in send queue. 11740 */ 11741 static mblk_t * 11742 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11743 { 11744 int32_t cnt; 11745 mblk_t *mp; 11746 11747 /* Defensive coding. Make sure we don't send incorrect data. */ 11748 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 11749 return (NULL); 11750 11751 cnt = seq - tcp->tcp_suna; 11752 mp = tcp->tcp_xmit_head; 11753 while (cnt > 0 && mp != NULL) { 11754 cnt -= mp->b_wptr - mp->b_rptr; 11755 if (cnt < 0) { 11756 cnt += mp->b_wptr - mp->b_rptr; 11757 break; 11758 } 11759 mp = mp->b_cont; 11760 } 11761 ASSERT(mp != NULL); 11762 *off = cnt; 11763 return (mp); 11764 } 11765 11766 /* 11767 * This function handles all retransmissions if SACK is enabled for this 11768 * connection. First it calculates how many segments can be retransmitted 11769 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11770 * segments. A segment is eligible if sack_cnt for that segment is greater 11771 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11772 * all eligible segments, it checks to see if TCP can send some new segments 11773 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11774 * 11775 * Parameters: 11776 * tcp_t *tcp: the tcp structure of the connection. 11777 * uint_t *flags: in return, appropriate value will be set for 11778 * tcp_rput_data(). 11779 */ 11780 static void 11781 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11782 { 11783 notsack_blk_t *notsack_blk; 11784 int32_t usable_swnd; 11785 int32_t mss; 11786 uint32_t seg_len; 11787 mblk_t *xmit_mp; 11788 tcp_stack_t *tcps = tcp->tcp_tcps; 11789 11790 ASSERT(tcp->tcp_sack_info != NULL); 11791 ASSERT(tcp->tcp_notsack_list != NULL); 11792 ASSERT(tcp->tcp_rexmit == B_FALSE); 11793 11794 /* Defensive coding in case there is a bug... */ 11795 if (tcp->tcp_notsack_list == NULL) { 11796 return; 11797 } 11798 notsack_blk = tcp->tcp_notsack_list; 11799 mss = tcp->tcp_mss; 11800 11801 /* 11802 * Limit the num of outstanding data in the network to be 11803 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11804 */ 11805 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11806 11807 /* At least retransmit 1 MSS of data. */ 11808 if (usable_swnd <= 0) { 11809 usable_swnd = mss; 11810 } 11811 11812 /* Make sure no new RTT samples will be taken. */ 11813 tcp->tcp_csuna = tcp->tcp_snxt; 11814 11815 notsack_blk = tcp->tcp_notsack_list; 11816 while (usable_swnd > 0) { 11817 mblk_t *snxt_mp, *tmp_mp; 11818 tcp_seq begin = tcp->tcp_sack_snxt; 11819 tcp_seq end; 11820 int32_t off; 11821 11822 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11823 if (SEQ_GT(notsack_blk->end, begin) && 11824 (notsack_blk->sack_cnt >= 11825 tcps->tcps_dupack_fast_retransmit)) { 11826 end = notsack_blk->end; 11827 if (SEQ_LT(begin, notsack_blk->begin)) { 11828 begin = notsack_blk->begin; 11829 } 11830 break; 11831 } 11832 } 11833 /* 11834 * All holes are filled. Manipulate tcp_cwnd to send more 11835 * if we can. Note that after the SACK recovery, tcp_cwnd is 11836 * set to tcp_cwnd_ssthresh. 11837 */ 11838 if (notsack_blk == NULL) { 11839 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11840 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11841 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11842 ASSERT(tcp->tcp_cwnd > 0); 11843 return; 11844 } else { 11845 usable_swnd = usable_swnd / mss; 11846 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 11847 MAX(usable_swnd * mss, mss); 11848 *flags |= TH_XMIT_NEEDED; 11849 return; 11850 } 11851 } 11852 11853 /* 11854 * Note that we may send more than usable_swnd allows here 11855 * because of round off, but no more than 1 MSS of data. 11856 */ 11857 seg_len = end - begin; 11858 if (seg_len > mss) 11859 seg_len = mss; 11860 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 11861 ASSERT(snxt_mp != NULL); 11862 /* This should not happen. Defensive coding again... */ 11863 if (snxt_mp == NULL) { 11864 return; 11865 } 11866 11867 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 11868 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 11869 if (xmit_mp == NULL) 11870 return; 11871 11872 usable_swnd -= seg_len; 11873 tcp->tcp_pipe += seg_len; 11874 tcp->tcp_sack_snxt = begin + seg_len; 11875 11876 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11877 11878 /* 11879 * Update the send timestamp to avoid false retransmission. 11880 */ 11881 snxt_mp->b_prev = (mblk_t *)lbolt; 11882 11883 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 11884 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 11885 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 11886 /* 11887 * Update tcp_rexmit_max to extend this SACK recovery phase. 11888 * This happens when new data sent during fast recovery is 11889 * also lost. If TCP retransmits those new data, it needs 11890 * to extend SACK recover phase to avoid starting another 11891 * fast retransmit/recovery unnecessarily. 11892 */ 11893 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 11894 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 11895 } 11896 } 11897 } 11898 11899 /* 11900 * This function handles policy checking at TCP level for non-hard_bound/ 11901 * detached connections. 11902 */ 11903 static boolean_t 11904 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 11905 boolean_t secure, boolean_t mctl_present) 11906 { 11907 ipsec_latch_t *ipl = NULL; 11908 ipsec_action_t *act = NULL; 11909 mblk_t *data_mp; 11910 ipsec_in_t *ii; 11911 const char *reason; 11912 kstat_named_t *counter; 11913 tcp_stack_t *tcps = tcp->tcp_tcps; 11914 ipsec_stack_t *ipss; 11915 ip_stack_t *ipst; 11916 11917 ASSERT(mctl_present || !secure); 11918 11919 ASSERT((ipha == NULL && ip6h != NULL) || 11920 (ip6h == NULL && ipha != NULL)); 11921 11922 /* 11923 * We don't necessarily have an ipsec_in_act action to verify 11924 * policy because of assymetrical policy where we have only 11925 * outbound policy and no inbound policy (possible with global 11926 * policy). 11927 */ 11928 if (!secure) { 11929 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 11930 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 11931 return (B_TRUE); 11932 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 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_clear), 11939 &tcps->tcps_dropper); 11940 return (B_FALSE); 11941 } 11942 11943 /* 11944 * We have a secure packet. 11945 */ 11946 if (act == NULL) { 11947 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 11948 "tcp_check_policy", ipha, ip6h, secure, 11949 tcps->tcps_netstack); 11950 ipss = tcps->tcps_netstack->netstack_ipsec; 11951 11952 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11953 DROPPER(ipss, ipds_tcp_secure), 11954 &tcps->tcps_dropper); 11955 return (B_FALSE); 11956 } 11957 11958 /* 11959 * XXX This whole routine is currently incorrect. ipl should 11960 * be set to the latch pointer, but is currently not set, so 11961 * we initialize it to NULL to avoid picking up random garbage. 11962 */ 11963 if (ipl == NULL) 11964 return (B_TRUE); 11965 11966 data_mp = first_mp->b_cont; 11967 11968 ii = (ipsec_in_t *)first_mp->b_rptr; 11969 11970 ipst = tcps->tcps_netstack->netstack_ip; 11971 11972 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 11973 &counter, tcp->tcp_connp)) { 11974 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 11975 return (B_TRUE); 11976 } 11977 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 11978 "tcp inbound policy mismatch: %s, packet dropped\n", 11979 reason); 11980 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 11981 11982 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 11983 &tcps->tcps_dropper); 11984 return (B_FALSE); 11985 } 11986 11987 /* 11988 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 11989 * retransmission after a timeout. 11990 * 11991 * To limit the number of duplicate segments, we limit the number of segment 11992 * to be sent in one time to tcp_snd_burst, the burst variable. 11993 */ 11994 static void 11995 tcp_ss_rexmit(tcp_t *tcp) 11996 { 11997 uint32_t snxt; 11998 uint32_t smax; 11999 int32_t win; 12000 int32_t mss; 12001 int32_t off; 12002 int32_t burst = tcp->tcp_snd_burst; 12003 mblk_t *snxt_mp; 12004 tcp_stack_t *tcps = tcp->tcp_tcps; 12005 12006 /* 12007 * Note that tcp_rexmit can be set even though TCP has retransmitted 12008 * all unack'ed segments. 12009 */ 12010 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12011 smax = tcp->tcp_rexmit_max; 12012 snxt = tcp->tcp_rexmit_nxt; 12013 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12014 snxt = tcp->tcp_suna; 12015 } 12016 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12017 win -= snxt - tcp->tcp_suna; 12018 mss = tcp->tcp_mss; 12019 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12020 12021 while (SEQ_LT(snxt, smax) && (win > 0) && 12022 (burst > 0) && (snxt_mp != NULL)) { 12023 mblk_t *xmit_mp; 12024 mblk_t *old_snxt_mp = snxt_mp; 12025 uint32_t cnt = mss; 12026 12027 if (win < cnt) { 12028 cnt = win; 12029 } 12030 if (SEQ_GT(snxt + cnt, smax)) { 12031 cnt = smax - snxt; 12032 } 12033 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12034 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12035 if (xmit_mp == NULL) 12036 return; 12037 12038 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12039 12040 snxt += cnt; 12041 win -= cnt; 12042 /* 12043 * Update the send timestamp to avoid false 12044 * retransmission. 12045 */ 12046 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12047 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12048 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12049 12050 tcp->tcp_rexmit_nxt = snxt; 12051 burst--; 12052 } 12053 /* 12054 * If we have transmitted all we have at the time 12055 * we started the retranmission, we can leave 12056 * the rest of the job to tcp_wput_data(). But we 12057 * need to check the send window first. If the 12058 * win is not 0, go on with tcp_wput_data(). 12059 */ 12060 if (SEQ_LT(snxt, smax) || win == 0) { 12061 return; 12062 } 12063 } 12064 /* Only call tcp_wput_data() if there is data to be sent. */ 12065 if (tcp->tcp_unsent) { 12066 tcp_wput_data(tcp, NULL, B_FALSE); 12067 } 12068 } 12069 12070 /* 12071 * Process all TCP option in SYN segment. Note that this function should 12072 * be called after tcp_adapt_ire() is called so that the necessary info 12073 * from IRE is already set in the tcp structure. 12074 * 12075 * This function sets up the correct tcp_mss value according to the 12076 * MSS option value and our header size. It also sets up the window scale 12077 * and timestamp values, and initialize SACK info blocks. But it does not 12078 * change receive window size after setting the tcp_mss value. The caller 12079 * should do the appropriate change. 12080 */ 12081 void 12082 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12083 { 12084 int options; 12085 tcp_opt_t tcpopt; 12086 uint32_t mss_max; 12087 char *tmp_tcph; 12088 tcp_stack_t *tcps = tcp->tcp_tcps; 12089 12090 tcpopt.tcp = NULL; 12091 options = tcp_parse_options(tcph, &tcpopt); 12092 12093 /* 12094 * Process MSS option. Note that MSS option value does not account 12095 * for IP or TCP options. This means that it is equal to MTU - minimum 12096 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12097 * IPv6. 12098 */ 12099 if (!(options & TCP_OPT_MSS_PRESENT)) { 12100 if (tcp->tcp_ipversion == IPV4_VERSION) 12101 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12102 else 12103 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12104 } else { 12105 if (tcp->tcp_ipversion == IPV4_VERSION) 12106 mss_max = tcps->tcps_mss_max_ipv4; 12107 else 12108 mss_max = tcps->tcps_mss_max_ipv6; 12109 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12110 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12111 else if (tcpopt.tcp_opt_mss > mss_max) 12112 tcpopt.tcp_opt_mss = mss_max; 12113 } 12114 12115 /* Process Window Scale option. */ 12116 if (options & TCP_OPT_WSCALE_PRESENT) { 12117 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12118 tcp->tcp_snd_ws_ok = B_TRUE; 12119 } else { 12120 tcp->tcp_snd_ws = B_FALSE; 12121 tcp->tcp_snd_ws_ok = B_FALSE; 12122 tcp->tcp_rcv_ws = B_FALSE; 12123 } 12124 12125 /* Process Timestamp option. */ 12126 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12127 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12128 tmp_tcph = (char *)tcp->tcp_tcph; 12129 12130 tcp->tcp_snd_ts_ok = B_TRUE; 12131 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12132 tcp->tcp_last_rcv_lbolt = lbolt64; 12133 ASSERT(OK_32PTR(tmp_tcph)); 12134 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12135 12136 /* Fill in our template header with basic timestamp option. */ 12137 tmp_tcph += tcp->tcp_tcp_hdr_len; 12138 tmp_tcph[0] = TCPOPT_NOP; 12139 tmp_tcph[1] = TCPOPT_NOP; 12140 tmp_tcph[2] = TCPOPT_TSTAMP; 12141 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12142 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12143 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12144 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12145 } else { 12146 tcp->tcp_snd_ts_ok = B_FALSE; 12147 } 12148 12149 /* 12150 * Process SACK options. If SACK is enabled for this connection, 12151 * then allocate the SACK info structure. Note the following ways 12152 * when tcp_snd_sack_ok is set to true. 12153 * 12154 * For active connection: in tcp_adapt_ire() called in 12155 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12156 * is checked. 12157 * 12158 * For passive connection: in tcp_adapt_ire() called in 12159 * tcp_accept_comm(). 12160 * 12161 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12162 * That check makes sure that if we did not send a SACK OK option, 12163 * we will not enable SACK for this connection even though the other 12164 * side sends us SACK OK option. For active connection, the SACK 12165 * info structure has already been allocated. So we need to free 12166 * it if SACK is disabled. 12167 */ 12168 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12169 (tcp->tcp_snd_sack_ok || 12170 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12171 /* This should be true only in the passive case. */ 12172 if (tcp->tcp_sack_info == NULL) { 12173 ASSERT(TCP_IS_DETACHED(tcp)); 12174 tcp->tcp_sack_info = 12175 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12176 } 12177 if (tcp->tcp_sack_info == NULL) { 12178 tcp->tcp_snd_sack_ok = B_FALSE; 12179 } else { 12180 tcp->tcp_snd_sack_ok = B_TRUE; 12181 if (tcp->tcp_snd_ts_ok) { 12182 tcp->tcp_max_sack_blk = 3; 12183 } else { 12184 tcp->tcp_max_sack_blk = 4; 12185 } 12186 } 12187 } else { 12188 /* 12189 * Resetting tcp_snd_sack_ok to B_FALSE so that 12190 * no SACK info will be used for this 12191 * connection. This assumes that SACK usage 12192 * permission is negotiated. This may need 12193 * to be changed once this is clarified. 12194 */ 12195 if (tcp->tcp_sack_info != NULL) { 12196 ASSERT(tcp->tcp_notsack_list == NULL); 12197 kmem_cache_free(tcp_sack_info_cache, 12198 tcp->tcp_sack_info); 12199 tcp->tcp_sack_info = NULL; 12200 } 12201 tcp->tcp_snd_sack_ok = B_FALSE; 12202 } 12203 12204 /* 12205 * Now we know the exact TCP/IP header length, subtract 12206 * that from tcp_mss to get our side's MSS. 12207 */ 12208 tcp->tcp_mss -= tcp->tcp_hdr_len; 12209 /* 12210 * Here we assume that the other side's header size will be equal to 12211 * our header size. We calculate the real MSS accordingly. Need to 12212 * take into additional stuffs IPsec puts in. 12213 * 12214 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12215 */ 12216 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12217 ((tcp->tcp_ipversion == IPV4_VERSION ? 12218 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12219 12220 /* 12221 * Set MSS to the smaller one of both ends of the connection. 12222 * We should not have called tcp_mss_set() before, but our 12223 * side of the MSS should have been set to a proper value 12224 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12225 * STREAM head parameters properly. 12226 * 12227 * If we have a larger-than-16-bit window but the other side 12228 * didn't want to do window scale, tcp_rwnd_set() will take 12229 * care of that. 12230 */ 12231 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12232 } 12233 12234 /* 12235 * Sends the T_CONN_IND to the listener. The caller calls this 12236 * functions via squeue to get inside the listener's perimeter 12237 * once the 3 way hand shake is done a T_CONN_IND needs to be 12238 * sent. As an optimization, the caller can call this directly 12239 * if listener's perimeter is same as eager's. 12240 */ 12241 /* ARGSUSED */ 12242 void 12243 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12244 { 12245 conn_t *lconnp = (conn_t *)arg; 12246 tcp_t *listener = lconnp->conn_tcp; 12247 tcp_t *tcp; 12248 struct T_conn_ind *conn_ind; 12249 ipaddr_t *addr_cache; 12250 boolean_t need_send_conn_ind = B_FALSE; 12251 tcp_stack_t *tcps = listener->tcp_tcps; 12252 12253 /* retrieve the eager */ 12254 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12255 ASSERT(conn_ind->OPT_offset != 0 && 12256 conn_ind->OPT_length == sizeof (intptr_t)); 12257 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12258 conn_ind->OPT_length); 12259 12260 /* 12261 * TLI/XTI applications will get confused by 12262 * sending eager as an option since it violates 12263 * the option semantics. So remove the eager as 12264 * option since TLI/XTI app doesn't need it anyway. 12265 */ 12266 if (!TCP_IS_SOCKET(listener)) { 12267 conn_ind->OPT_length = 0; 12268 conn_ind->OPT_offset = 0; 12269 } 12270 if (listener->tcp_state == TCPS_CLOSED || 12271 TCP_IS_DETACHED(listener)) { 12272 /* 12273 * If listener has closed, it would have caused a 12274 * a cleanup/blowoff to happen for the eager. We 12275 * just need to return. 12276 */ 12277 freemsg(mp); 12278 return; 12279 } 12280 12281 12282 /* 12283 * if the conn_req_q is full defer passing up the 12284 * T_CONN_IND until space is availabe after t_accept() 12285 * processing 12286 */ 12287 mutex_enter(&listener->tcp_eager_lock); 12288 12289 /* 12290 * Take the eager out, if it is in the list of droppable eagers 12291 * as we are here because the 3W handshake is over. 12292 */ 12293 MAKE_UNDROPPABLE(tcp); 12294 12295 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12296 tcp_t *tail; 12297 12298 /* 12299 * The eager already has an extra ref put in tcp_rput_data 12300 * so that it stays till accept comes back even though it 12301 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12302 */ 12303 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12304 listener->tcp_conn_req_cnt_q0--; 12305 listener->tcp_conn_req_cnt_q++; 12306 12307 /* Move from SYN_RCVD to ESTABLISHED list */ 12308 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12309 tcp->tcp_eager_prev_q0; 12310 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12311 tcp->tcp_eager_next_q0; 12312 tcp->tcp_eager_prev_q0 = NULL; 12313 tcp->tcp_eager_next_q0 = NULL; 12314 12315 /* 12316 * Insert at end of the queue because sockfs 12317 * sends down T_CONN_RES in chronological 12318 * order. Leaving the older conn indications 12319 * at front of the queue helps reducing search 12320 * time. 12321 */ 12322 tail = listener->tcp_eager_last_q; 12323 if (tail != NULL) 12324 tail->tcp_eager_next_q = tcp; 12325 else 12326 listener->tcp_eager_next_q = tcp; 12327 listener->tcp_eager_last_q = tcp; 12328 tcp->tcp_eager_next_q = NULL; 12329 /* 12330 * Delay sending up the T_conn_ind until we are 12331 * done with the eager. Once we have have sent up 12332 * the T_conn_ind, the accept can potentially complete 12333 * any time and release the refhold we have on the eager. 12334 */ 12335 need_send_conn_ind = B_TRUE; 12336 } else { 12337 /* 12338 * Defer connection on q0 and set deferred 12339 * connection bit true 12340 */ 12341 tcp->tcp_conn_def_q0 = B_TRUE; 12342 12343 /* take tcp out of q0 ... */ 12344 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12345 tcp->tcp_eager_next_q0; 12346 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12347 tcp->tcp_eager_prev_q0; 12348 12349 /* ... and place it at the end of q0 */ 12350 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12351 tcp->tcp_eager_next_q0 = listener; 12352 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12353 listener->tcp_eager_prev_q0 = tcp; 12354 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12355 } 12356 12357 /* we have timed out before */ 12358 if (tcp->tcp_syn_rcvd_timeout != 0) { 12359 tcp->tcp_syn_rcvd_timeout = 0; 12360 listener->tcp_syn_rcvd_timeout--; 12361 if (listener->tcp_syn_defense && 12362 listener->tcp_syn_rcvd_timeout <= 12363 (tcps->tcps_conn_req_max_q0 >> 5) && 12364 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12365 listener->tcp_last_rcv_lbolt)) { 12366 /* 12367 * Turn off the defense mode if we 12368 * believe the SYN attack is over. 12369 */ 12370 listener->tcp_syn_defense = B_FALSE; 12371 if (listener->tcp_ip_addr_cache) { 12372 kmem_free((void *)listener->tcp_ip_addr_cache, 12373 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12374 listener->tcp_ip_addr_cache = NULL; 12375 } 12376 } 12377 } 12378 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12379 if (addr_cache != NULL) { 12380 /* 12381 * We have finished a 3-way handshake with this 12382 * remote host. This proves the IP addr is good. 12383 * Cache it! 12384 */ 12385 addr_cache[IP_ADDR_CACHE_HASH( 12386 tcp->tcp_remote)] = tcp->tcp_remote; 12387 } 12388 mutex_exit(&listener->tcp_eager_lock); 12389 if (need_send_conn_ind) 12390 tcp_ulp_newconn(lconnp, tcp->tcp_connp, mp); 12391 } 12392 12393 /* 12394 * Send the newconn notification to ulp. The eager is blown off if the 12395 * notification fails. 12396 */ 12397 static void 12398 tcp_ulp_newconn(conn_t *lconnp, conn_t *econnp, mblk_t *mp) 12399 { 12400 if (IPCL_IS_NONSTR(lconnp)) { 12401 cred_t *cr; 12402 pid_t cpid; 12403 12404 cr = msg_getcred(mp, &cpid); 12405 12406 ASSERT(econnp->conn_tcp->tcp_listener == lconnp->conn_tcp); 12407 ASSERT(econnp->conn_tcp->tcp_saved_listener == 12408 lconnp->conn_tcp); 12409 12410 /* Keep the message around in case of a fallback to TPI */ 12411 econnp->conn_tcp->tcp_conn.tcp_eager_conn_ind = mp; 12412 12413 /* 12414 * Notify the ULP about the newconn. It is guaranteed that no 12415 * tcp_accept() call will be made for the eager if the 12416 * notification fails, so it's safe to blow it off in that 12417 * case. 12418 * 12419 * The upper handle will be assigned when tcp_accept() is 12420 * called. 12421 */ 12422 if ((*lconnp->conn_upcalls->su_newconn) 12423 (lconnp->conn_upper_handle, 12424 (sock_lower_handle_t)econnp, 12425 &sock_tcp_downcalls, cr, cpid, 12426 &econnp->conn_upcalls) == NULL) { 12427 /* Failed to allocate a socket */ 12428 BUMP_MIB(&lconnp->conn_tcp->tcp_tcps->tcps_mib, 12429 tcpEstabResets); 12430 (void) tcp_eager_blowoff(lconnp->conn_tcp, 12431 econnp->conn_tcp->tcp_conn_req_seqnum); 12432 } 12433 } else { 12434 putnext(lconnp->conn_tcp->tcp_rq, mp); 12435 } 12436 } 12437 12438 mblk_t * 12439 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12440 uint_t *ifindexp, ip6_pkt_t *ippp) 12441 { 12442 ip_pktinfo_t *pinfo; 12443 ip6_t *ip6h; 12444 uchar_t *rptr; 12445 mblk_t *first_mp = mp; 12446 boolean_t mctl_present = B_FALSE; 12447 uint_t ifindex = 0; 12448 ip6_pkt_t ipp; 12449 uint_t ipvers; 12450 uint_t ip_hdr_len; 12451 tcp_stack_t *tcps = tcp->tcp_tcps; 12452 12453 rptr = mp->b_rptr; 12454 ASSERT(OK_32PTR(rptr)); 12455 ASSERT(tcp != NULL); 12456 ipp.ipp_fields = 0; 12457 12458 switch DB_TYPE(mp) { 12459 case M_CTL: 12460 mp = mp->b_cont; 12461 if (mp == NULL) { 12462 freemsg(first_mp); 12463 return (NULL); 12464 } 12465 if (DB_TYPE(mp) != M_DATA) { 12466 freemsg(first_mp); 12467 return (NULL); 12468 } 12469 mctl_present = B_TRUE; 12470 break; 12471 case M_DATA: 12472 break; 12473 default: 12474 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12475 freemsg(mp); 12476 return (NULL); 12477 } 12478 ipvers = IPH_HDR_VERSION(rptr); 12479 if (ipvers == IPV4_VERSION) { 12480 if (tcp == NULL) { 12481 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12482 goto done; 12483 } 12484 12485 ipp.ipp_fields |= IPPF_HOPLIMIT; 12486 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12487 12488 /* 12489 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12490 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12491 */ 12492 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12493 mctl_present) { 12494 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12495 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12496 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12497 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12498 ipp.ipp_fields |= IPPF_IFINDEX; 12499 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12500 ifindex = pinfo->ip_pkt_ifindex; 12501 } 12502 freeb(first_mp); 12503 mctl_present = B_FALSE; 12504 } 12505 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12506 } else { 12507 ip6h = (ip6_t *)rptr; 12508 12509 ASSERT(ipvers == IPV6_VERSION); 12510 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12511 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12512 ipp.ipp_hoplimit = ip6h->ip6_hops; 12513 12514 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12515 uint8_t nexthdrp; 12516 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12517 12518 /* Look for ifindex information */ 12519 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12520 ip6i_t *ip6i = (ip6i_t *)ip6h; 12521 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12522 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12523 freemsg(first_mp); 12524 return (NULL); 12525 } 12526 12527 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12528 ASSERT(ip6i->ip6i_ifindex != 0); 12529 ipp.ipp_fields |= IPPF_IFINDEX; 12530 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12531 ifindex = ip6i->ip6i_ifindex; 12532 } 12533 rptr = (uchar_t *)&ip6i[1]; 12534 mp->b_rptr = rptr; 12535 if (rptr == mp->b_wptr) { 12536 mblk_t *mp1; 12537 mp1 = mp->b_cont; 12538 freeb(mp); 12539 mp = mp1; 12540 rptr = mp->b_rptr; 12541 } 12542 if (MBLKL(mp) < IPV6_HDR_LEN + 12543 sizeof (tcph_t)) { 12544 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12545 freemsg(first_mp); 12546 return (NULL); 12547 } 12548 ip6h = (ip6_t *)rptr; 12549 } 12550 12551 /* 12552 * Find any potentially interesting extension headers 12553 * as well as the length of the IPv6 + extension 12554 * headers. 12555 */ 12556 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12557 /* Verify if this is a TCP packet */ 12558 if (nexthdrp != IPPROTO_TCP) { 12559 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12560 freemsg(first_mp); 12561 return (NULL); 12562 } 12563 } else { 12564 ip_hdr_len = IPV6_HDR_LEN; 12565 } 12566 } 12567 12568 done: 12569 if (ipversp != NULL) 12570 *ipversp = ipvers; 12571 if (ip_hdr_lenp != NULL) 12572 *ip_hdr_lenp = ip_hdr_len; 12573 if (ippp != NULL) 12574 *ippp = ipp; 12575 if (ifindexp != NULL) 12576 *ifindexp = ifindex; 12577 if (mctl_present) { 12578 freeb(first_mp); 12579 } 12580 return (mp); 12581 } 12582 12583 /* 12584 * Handle M_DATA messages from IP. Its called directly from IP via 12585 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12586 * in this path. 12587 * 12588 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12589 * v4 and v6), we are called through tcp_input() and a M_CTL can 12590 * be present for options but tcp_find_pktinfo() deals with it. We 12591 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12592 * 12593 * The first argument is always the connp/tcp to which the mp belongs. 12594 * There are no exceptions to this rule. The caller has already put 12595 * a reference on this connp/tcp and once tcp_rput_data() returns, 12596 * the squeue will do the refrele. 12597 * 12598 * The TH_SYN for the listener directly go to tcp_conn_request via 12599 * squeue. 12600 * 12601 * sqp: NULL = recursive, sqp != NULL means called from squeue 12602 */ 12603 void 12604 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12605 { 12606 int32_t bytes_acked; 12607 int32_t gap; 12608 mblk_t *mp1; 12609 uint_t flags; 12610 uint32_t new_swnd = 0; 12611 uchar_t *iphdr; 12612 uchar_t *rptr; 12613 int32_t rgap; 12614 uint32_t seg_ack; 12615 int seg_len; 12616 uint_t ip_hdr_len; 12617 uint32_t seg_seq; 12618 tcph_t *tcph; 12619 int urp; 12620 tcp_opt_t tcpopt; 12621 uint_t ipvers; 12622 ip6_pkt_t ipp; 12623 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12624 uint32_t cwnd; 12625 uint32_t add; 12626 int npkt; 12627 int mss; 12628 conn_t *connp = (conn_t *)arg; 12629 squeue_t *sqp = (squeue_t *)arg2; 12630 tcp_t *tcp = connp->conn_tcp; 12631 tcp_stack_t *tcps = tcp->tcp_tcps; 12632 12633 /* 12634 * RST from fused tcp loopback peer should trigger an unfuse. 12635 */ 12636 if (tcp->tcp_fused) { 12637 TCP_STAT(tcps, tcp_fusion_aborted); 12638 tcp_unfuse(tcp); 12639 } 12640 12641 iphdr = mp->b_rptr; 12642 rptr = mp->b_rptr; 12643 ASSERT(OK_32PTR(rptr)); 12644 12645 /* 12646 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12647 * processing here. For rest call tcp_find_pktinfo to fill up the 12648 * necessary information. 12649 */ 12650 if (IPCL_IS_TCP4(connp)) { 12651 ipvers = IPV4_VERSION; 12652 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12653 } else { 12654 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12655 NULL, &ipp); 12656 if (mp == NULL) { 12657 TCP_STAT(tcps, tcp_rput_v6_error); 12658 return; 12659 } 12660 iphdr = mp->b_rptr; 12661 rptr = mp->b_rptr; 12662 } 12663 ASSERT(DB_TYPE(mp) == M_DATA); 12664 ASSERT(mp->b_next == NULL); 12665 12666 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12667 seg_seq = ABE32_TO_U32(tcph->th_seq); 12668 seg_ack = ABE32_TO_U32(tcph->th_ack); 12669 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12670 seg_len = (int)(mp->b_wptr - rptr) - 12671 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12672 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12673 do { 12674 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12675 (uintptr_t)INT_MAX); 12676 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12677 } while ((mp1 = mp1->b_cont) != NULL && 12678 mp1->b_datap->db_type == M_DATA); 12679 } 12680 12681 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12682 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12683 seg_len, tcph); 12684 return; 12685 } 12686 12687 if (sqp != NULL) { 12688 /* 12689 * This is the correct place to update tcp_last_recv_time. Note 12690 * that it is also updated for tcp structure that belongs to 12691 * global and listener queues which do not really need updating. 12692 * But that should not cause any harm. And it is updated for 12693 * all kinds of incoming segments, not only for data segments. 12694 */ 12695 tcp->tcp_last_recv_time = lbolt; 12696 } 12697 12698 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12699 12700 BUMP_LOCAL(tcp->tcp_ibsegs); 12701 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 12702 12703 if ((flags & TH_URG) && sqp != NULL) { 12704 /* 12705 * TCP can't handle urgent pointers that arrive before 12706 * the connection has been accept()ed since it can't 12707 * buffer OOB data. Discard segment if this happens. 12708 * 12709 * We can't just rely on a non-null tcp_listener to indicate 12710 * that the accept() has completed since unlinking of the 12711 * eager and completion of the accept are not atomic. 12712 * tcp_detached, when it is not set (B_FALSE) indicates 12713 * that the accept() has completed. 12714 * 12715 * Nor can it reassemble urgent pointers, so discard 12716 * if it's not the next segment expected. 12717 * 12718 * Otherwise, collapse chain into one mblk (discard if 12719 * that fails). This makes sure the headers, retransmitted 12720 * data, and new data all are in the same mblk. 12721 */ 12722 ASSERT(mp != NULL); 12723 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 12724 freemsg(mp); 12725 return; 12726 } 12727 /* Update pointers into message */ 12728 iphdr = rptr = mp->b_rptr; 12729 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12730 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12731 /* 12732 * Since we can't handle any data with this urgent 12733 * pointer that is out of sequence, we expunge 12734 * the data. This allows us to still register 12735 * the urgent mark and generate the M_PCSIG, 12736 * which we can do. 12737 */ 12738 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12739 seg_len = 0; 12740 } 12741 } 12742 12743 switch (tcp->tcp_state) { 12744 case TCPS_SYN_SENT: 12745 if (flags & TH_ACK) { 12746 /* 12747 * Note that our stack cannot send data before a 12748 * connection is established, therefore the 12749 * following check is valid. Otherwise, it has 12750 * to be changed. 12751 */ 12752 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12753 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12754 freemsg(mp); 12755 if (flags & TH_RST) 12756 return; 12757 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12758 tcp, seg_ack, 0, TH_RST); 12759 return; 12760 } 12761 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12762 } 12763 if (flags & TH_RST) { 12764 freemsg(mp); 12765 if (flags & TH_ACK) 12766 (void) tcp_clean_death(tcp, 12767 ECONNREFUSED, 13); 12768 return; 12769 } 12770 if (!(flags & TH_SYN)) { 12771 freemsg(mp); 12772 return; 12773 } 12774 12775 /* Process all TCP options. */ 12776 tcp_process_options(tcp, tcph); 12777 /* 12778 * The following changes our rwnd to be a multiple of the 12779 * MIN(peer MSS, our MSS) for performance reason. 12780 */ 12781 (void) tcp_rwnd_set(tcp, 12782 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 12783 12784 /* Is the other end ECN capable? */ 12785 if (tcp->tcp_ecn_ok) { 12786 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12787 tcp->tcp_ecn_ok = B_FALSE; 12788 } 12789 } 12790 /* 12791 * Clear ECN flags because it may interfere with later 12792 * processing. 12793 */ 12794 flags &= ~(TH_ECE|TH_CWR); 12795 12796 tcp->tcp_irs = seg_seq; 12797 tcp->tcp_rack = seg_seq; 12798 tcp->tcp_rnxt = seg_seq + 1; 12799 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12800 if (!TCP_IS_DETACHED(tcp)) { 12801 /* Allocate room for SACK options if needed. */ 12802 if (tcp->tcp_snd_sack_ok) { 12803 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12804 tcp->tcp_hdr_len + 12805 TCPOPT_MAX_SACK_LEN + 12806 (tcp->tcp_loopback ? 0 : 12807 tcps->tcps_wroff_xtra)); 12808 } else { 12809 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12810 tcp->tcp_hdr_len + 12811 (tcp->tcp_loopback ? 0 : 12812 tcps->tcps_wroff_xtra)); 12813 } 12814 } 12815 if (flags & TH_ACK) { 12816 /* 12817 * If we can't get the confirmation upstream, pretend 12818 * we didn't even see this one. 12819 * 12820 * XXX: how can we pretend we didn't see it if we 12821 * have updated rnxt et. al. 12822 * 12823 * For loopback we defer sending up the T_CONN_CON 12824 * until after some checks below. 12825 */ 12826 mp1 = NULL; 12827 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12828 tcp->tcp_loopback ? &mp1 : NULL)) { 12829 freemsg(mp); 12830 return; 12831 } 12832 /* SYN was acked - making progress */ 12833 if (tcp->tcp_ipversion == IPV6_VERSION) 12834 tcp->tcp_ip_forward_progress = B_TRUE; 12835 12836 /* One for the SYN */ 12837 tcp->tcp_suna = tcp->tcp_iss + 1; 12838 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12839 tcp->tcp_state = TCPS_ESTABLISHED; 12840 12841 /* 12842 * If SYN was retransmitted, need to reset all 12843 * retransmission info. This is because this 12844 * segment will be treated as a dup ACK. 12845 */ 12846 if (tcp->tcp_rexmit) { 12847 tcp->tcp_rexmit = B_FALSE; 12848 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12849 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12850 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12851 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12852 tcp->tcp_ms_we_have_waited = 0; 12853 12854 /* 12855 * Set tcp_cwnd back to 1 MSS, per 12856 * recommendation from 12857 * draft-floyd-incr-init-win-01.txt, 12858 * Increasing TCP's Initial Window. 12859 */ 12860 tcp->tcp_cwnd = tcp->tcp_mss; 12861 } 12862 12863 tcp->tcp_swl1 = seg_seq; 12864 tcp->tcp_swl2 = seg_ack; 12865 12866 new_swnd = BE16_TO_U16(tcph->th_win); 12867 tcp->tcp_swnd = new_swnd; 12868 if (new_swnd > tcp->tcp_max_swnd) 12869 tcp->tcp_max_swnd = new_swnd; 12870 12871 /* 12872 * Always send the three-way handshake ack immediately 12873 * in order to make the connection complete as soon as 12874 * possible on the accepting host. 12875 */ 12876 flags |= TH_ACK_NEEDED; 12877 12878 /* 12879 * Special case for loopback. At this point we have 12880 * received SYN-ACK from the remote endpoint. In 12881 * order to ensure that both endpoints reach the 12882 * fused state prior to any data exchange, the final 12883 * ACK needs to be sent before we indicate T_CONN_CON 12884 * to the module upstream. 12885 */ 12886 if (tcp->tcp_loopback) { 12887 mblk_t *ack_mp; 12888 12889 ASSERT(!tcp->tcp_unfusable); 12890 ASSERT(mp1 != NULL); 12891 /* 12892 * For loopback, we always get a pure SYN-ACK 12893 * and only need to send back the final ACK 12894 * with no data (this is because the other 12895 * tcp is ours and we don't do T/TCP). This 12896 * final ACK triggers the passive side to 12897 * perform fusion in ESTABLISHED state. 12898 */ 12899 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 12900 if (tcp->tcp_ack_tid != 0) { 12901 (void) TCP_TIMER_CANCEL(tcp, 12902 tcp->tcp_ack_tid); 12903 tcp->tcp_ack_tid = 0; 12904 } 12905 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 12906 BUMP_LOCAL(tcp->tcp_obsegs); 12907 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 12908 12909 if (!IPCL_IS_NONSTR(connp)) { 12910 /* Send up T_CONN_CON */ 12911 putnext(tcp->tcp_rq, mp1); 12912 } else { 12913 cred_t *cr; 12914 pid_t cpid; 12915 12916 cr = msg_getcred(mp1, &cpid); 12917 (*connp->conn_upcalls-> 12918 su_connected) 12919 (connp->conn_upper_handle, 12920 tcp->tcp_connid, cr, cpid); 12921 freemsg(mp1); 12922 } 12923 12924 freemsg(mp); 12925 return; 12926 } 12927 /* 12928 * Forget fusion; we need to handle more 12929 * complex cases below. Send the deferred 12930 * T_CONN_CON message upstream and proceed 12931 * as usual. Mark this tcp as not capable 12932 * of fusion. 12933 */ 12934 TCP_STAT(tcps, tcp_fusion_unfusable); 12935 tcp->tcp_unfusable = B_TRUE; 12936 if (!IPCL_IS_NONSTR(connp)) { 12937 putnext(tcp->tcp_rq, mp1); 12938 } else { 12939 cred_t *cr; 12940 pid_t cpid; 12941 12942 cr = msg_getcred(mp1, &cpid); 12943 (*connp->conn_upcalls->su_connected) 12944 (connp->conn_upper_handle, 12945 tcp->tcp_connid, cr, cpid); 12946 freemsg(mp1); 12947 } 12948 } 12949 12950 /* 12951 * Check to see if there is data to be sent. If 12952 * yes, set the transmit flag. Then check to see 12953 * if received data processing needs to be done. 12954 * If not, go straight to xmit_check. This short 12955 * cut is OK as we don't support T/TCP. 12956 */ 12957 if (tcp->tcp_unsent) 12958 flags |= TH_XMIT_NEEDED; 12959 12960 if (seg_len == 0 && !(flags & TH_URG)) { 12961 freemsg(mp); 12962 goto xmit_check; 12963 } 12964 12965 flags &= ~TH_SYN; 12966 seg_seq++; 12967 break; 12968 } 12969 tcp->tcp_state = TCPS_SYN_RCVD; 12970 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 12971 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 12972 if (mp1) { 12973 /* 12974 * See comment in tcp_conn_request() for why we use 12975 * the open() time pid here. 12976 */ 12977 DB_CPID(mp1) = tcp->tcp_cpid; 12978 tcp_send_data(tcp, tcp->tcp_wq, mp1); 12979 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 12980 } 12981 freemsg(mp); 12982 return; 12983 case TCPS_SYN_RCVD: 12984 if (flags & TH_ACK) { 12985 /* 12986 * In this state, a SYN|ACK packet is either bogus 12987 * because the other side must be ACKing our SYN which 12988 * indicates it has seen the ACK for their SYN and 12989 * shouldn't retransmit it or we're crossing SYNs 12990 * on active open. 12991 */ 12992 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 12993 freemsg(mp); 12994 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 12995 tcp, seg_ack, 0, TH_RST); 12996 return; 12997 } 12998 /* 12999 * NOTE: RFC 793 pg. 72 says this should be 13000 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13001 * but that would mean we have an ack that ignored 13002 * our SYN. 13003 */ 13004 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13005 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13006 freemsg(mp); 13007 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13008 tcp, seg_ack, 0, TH_RST); 13009 return; 13010 } 13011 } 13012 break; 13013 case TCPS_LISTEN: 13014 /* 13015 * Only a TLI listener can come through this path when a 13016 * acceptor is going back to be a listener and a packet 13017 * for the acceptor hits the classifier. For a socket 13018 * listener, this can never happen because a listener 13019 * can never accept connection on itself and hence a 13020 * socket acceptor can not go back to being a listener. 13021 */ 13022 ASSERT(!TCP_IS_SOCKET(tcp)); 13023 /*FALLTHRU*/ 13024 case TCPS_CLOSED: 13025 case TCPS_BOUND: { 13026 conn_t *new_connp; 13027 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13028 13029 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13030 if (new_connp != NULL) { 13031 tcp_reinput(new_connp, mp, connp->conn_sqp); 13032 return; 13033 } 13034 /* We failed to classify. For now just drop the packet */ 13035 freemsg(mp); 13036 return; 13037 } 13038 case TCPS_IDLE: 13039 /* 13040 * Handle the case where the tcp_clean_death() has happened 13041 * on a connection (application hasn't closed yet) but a packet 13042 * was already queued on squeue before tcp_clean_death() 13043 * was processed. Calling tcp_clean_death() twice on same 13044 * connection can result in weird behaviour. 13045 */ 13046 freemsg(mp); 13047 return; 13048 default: 13049 break; 13050 } 13051 13052 /* 13053 * Already on the correct queue/perimeter. 13054 * If this is a detached connection and not an eager 13055 * connection hanging off a listener then new data 13056 * (past the FIN) will cause a reset. 13057 * We do a special check here where it 13058 * is out of the main line, rather than check 13059 * if we are detached every time we see new 13060 * data down below. 13061 */ 13062 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13063 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13064 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13065 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13066 13067 freemsg(mp); 13068 /* 13069 * This could be an SSL closure alert. We're detached so just 13070 * acknowledge it this last time. 13071 */ 13072 if (tcp->tcp_kssl_ctx != NULL) { 13073 kssl_release_ctx(tcp->tcp_kssl_ctx); 13074 tcp->tcp_kssl_ctx = NULL; 13075 13076 tcp->tcp_rnxt += seg_len; 13077 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13078 flags |= TH_ACK_NEEDED; 13079 goto ack_check; 13080 } 13081 13082 tcp_xmit_ctl("new data when detached", tcp, 13083 tcp->tcp_snxt, 0, TH_RST); 13084 (void) tcp_clean_death(tcp, EPROTO, 12); 13085 return; 13086 } 13087 13088 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13089 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13090 new_swnd = BE16_TO_U16(tcph->th_win) << 13091 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13092 13093 if (tcp->tcp_snd_ts_ok) { 13094 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13095 /* 13096 * This segment is not acceptable. 13097 * Drop it and send back an ACK. 13098 */ 13099 freemsg(mp); 13100 flags |= TH_ACK_NEEDED; 13101 goto ack_check; 13102 } 13103 } else if (tcp->tcp_snd_sack_ok) { 13104 ASSERT(tcp->tcp_sack_info != NULL); 13105 tcpopt.tcp = tcp; 13106 /* 13107 * SACK info in already updated in tcp_parse_options. Ignore 13108 * all other TCP options... 13109 */ 13110 (void) tcp_parse_options(tcph, &tcpopt); 13111 } 13112 try_again:; 13113 mss = tcp->tcp_mss; 13114 gap = seg_seq - tcp->tcp_rnxt; 13115 rgap = tcp->tcp_rwnd - (gap + seg_len); 13116 /* 13117 * gap is the amount of sequence space between what we expect to see 13118 * and what we got for seg_seq. A positive value for gap means 13119 * something got lost. A negative value means we got some old stuff. 13120 */ 13121 if (gap < 0) { 13122 /* Old stuff present. Is the SYN in there? */ 13123 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13124 (seg_len != 0)) { 13125 flags &= ~TH_SYN; 13126 seg_seq++; 13127 urp--; 13128 /* Recompute the gaps after noting the SYN. */ 13129 goto try_again; 13130 } 13131 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13132 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13133 (seg_len > -gap ? -gap : seg_len)); 13134 /* Remove the old stuff from seg_len. */ 13135 seg_len += gap; 13136 /* 13137 * Anything left? 13138 * Make sure to check for unack'd FIN when rest of data 13139 * has been previously ack'd. 13140 */ 13141 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13142 /* 13143 * Resets are only valid if they lie within our offered 13144 * window. If the RST bit is set, we just ignore this 13145 * segment. 13146 */ 13147 if (flags & TH_RST) { 13148 freemsg(mp); 13149 return; 13150 } 13151 13152 /* 13153 * The arriving of dup data packets indicate that we 13154 * may have postponed an ack for too long, or the other 13155 * side's RTT estimate is out of shape. Start acking 13156 * more often. 13157 */ 13158 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13159 tcp->tcp_rack_cnt >= 1 && 13160 tcp->tcp_rack_abs_max > 2) { 13161 tcp->tcp_rack_abs_max--; 13162 } 13163 tcp->tcp_rack_cur_max = 1; 13164 13165 /* 13166 * This segment is "unacceptable". None of its 13167 * sequence space lies within our advertized window. 13168 * 13169 * Adjust seg_len to the original value for tracing. 13170 */ 13171 seg_len -= gap; 13172 if (tcp->tcp_debug) { 13173 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13174 "tcp_rput: unacceptable, gap %d, rgap %d, " 13175 "flags 0x%x, seg_seq %u, seg_ack %u, " 13176 "seg_len %d, rnxt %u, snxt %u, %s", 13177 gap, rgap, flags, seg_seq, seg_ack, 13178 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13179 tcp_display(tcp, NULL, 13180 DISP_ADDR_AND_PORT)); 13181 } 13182 13183 /* 13184 * Arrange to send an ACK in response to the 13185 * unacceptable segment per RFC 793 page 69. There 13186 * is only one small difference between ours and the 13187 * acceptability test in the RFC - we accept ACK-only 13188 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13189 * will be generated. 13190 * 13191 * Note that we have to ACK an ACK-only packet at least 13192 * for stacks that send 0-length keep-alives with 13193 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13194 * section 4.2.3.6. As long as we don't ever generate 13195 * an unacceptable packet in response to an incoming 13196 * packet that is unacceptable, it should not cause 13197 * "ACK wars". 13198 */ 13199 flags |= TH_ACK_NEEDED; 13200 13201 /* 13202 * Continue processing this segment in order to use the 13203 * ACK information it contains, but skip all other 13204 * sequence-number processing. Processing the ACK 13205 * information is necessary in order to 13206 * re-synchronize connections that may have lost 13207 * synchronization. 13208 * 13209 * We clear seg_len and flag fields related to 13210 * sequence number processing as they are not 13211 * to be trusted for an unacceptable segment. 13212 */ 13213 seg_len = 0; 13214 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13215 goto process_ack; 13216 } 13217 13218 /* Fix seg_seq, and chew the gap off the front. */ 13219 seg_seq = tcp->tcp_rnxt; 13220 urp += gap; 13221 do { 13222 mblk_t *mp2; 13223 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13224 (uintptr_t)UINT_MAX); 13225 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13226 if (gap > 0) { 13227 mp->b_rptr = mp->b_wptr - gap; 13228 break; 13229 } 13230 mp2 = mp; 13231 mp = mp->b_cont; 13232 freeb(mp2); 13233 } while (gap < 0); 13234 /* 13235 * If the urgent data has already been acknowledged, we 13236 * should ignore TH_URG below 13237 */ 13238 if (urp < 0) 13239 flags &= ~TH_URG; 13240 } 13241 /* 13242 * rgap is the amount of stuff received out of window. A negative 13243 * value is the amount out of window. 13244 */ 13245 if (rgap < 0) { 13246 mblk_t *mp2; 13247 13248 if (tcp->tcp_rwnd == 0) { 13249 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13250 } else { 13251 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13252 UPDATE_MIB(&tcps->tcps_mib, 13253 tcpInDataPastWinBytes, -rgap); 13254 } 13255 13256 /* 13257 * seg_len does not include the FIN, so if more than 13258 * just the FIN is out of window, we act like we don't 13259 * see it. (If just the FIN is out of window, rgap 13260 * will be zero and we will go ahead and acknowledge 13261 * the FIN.) 13262 */ 13263 flags &= ~TH_FIN; 13264 13265 /* Fix seg_len and make sure there is something left. */ 13266 seg_len += rgap; 13267 if (seg_len <= 0) { 13268 /* 13269 * Resets are only valid if they lie within our offered 13270 * window. If the RST bit is set, we just ignore this 13271 * segment. 13272 */ 13273 if (flags & TH_RST) { 13274 freemsg(mp); 13275 return; 13276 } 13277 13278 /* Per RFC 793, we need to send back an ACK. */ 13279 flags |= TH_ACK_NEEDED; 13280 13281 /* 13282 * Send SIGURG as soon as possible i.e. even 13283 * if the TH_URG was delivered in a window probe 13284 * packet (which will be unacceptable). 13285 * 13286 * We generate a signal if none has been generated 13287 * for this connection or if this is a new urgent 13288 * byte. Also send a zero-length "unmarked" message 13289 * to inform SIOCATMARK that this is not the mark. 13290 * 13291 * tcp_urp_last_valid is cleared when the T_exdata_ind 13292 * is sent up. This plus the check for old data 13293 * (gap >= 0) handles the wraparound of the sequence 13294 * number space without having to always track the 13295 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13296 * this max in its rcv_up variable). 13297 * 13298 * This prevents duplicate SIGURGS due to a "late" 13299 * zero-window probe when the T_EXDATA_IND has already 13300 * been sent up. 13301 */ 13302 if ((flags & TH_URG) && 13303 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13304 tcp->tcp_urp_last))) { 13305 if (IPCL_IS_NONSTR(connp)) { 13306 if (!TCP_IS_DETACHED(tcp)) { 13307 (*connp->conn_upcalls-> 13308 su_signal_oob) 13309 (connp->conn_upper_handle, 13310 urp); 13311 } 13312 } else { 13313 mp1 = allocb(0, BPRI_MED); 13314 if (mp1 == NULL) { 13315 freemsg(mp); 13316 return; 13317 } 13318 if (!TCP_IS_DETACHED(tcp) && 13319 !putnextctl1(tcp->tcp_rq, 13320 M_PCSIG, SIGURG)) { 13321 /* Try again on the rexmit. */ 13322 freemsg(mp1); 13323 freemsg(mp); 13324 return; 13325 } 13326 /* 13327 * If the next byte would be the mark 13328 * then mark with MARKNEXT else mark 13329 * with NOTMARKNEXT. 13330 */ 13331 if (gap == 0 && urp == 0) 13332 mp1->b_flag |= MSGMARKNEXT; 13333 else 13334 mp1->b_flag |= MSGNOTMARKNEXT; 13335 freemsg(tcp->tcp_urp_mark_mp); 13336 tcp->tcp_urp_mark_mp = mp1; 13337 flags |= TH_SEND_URP_MARK; 13338 } 13339 tcp->tcp_urp_last_valid = B_TRUE; 13340 tcp->tcp_urp_last = urp + seg_seq; 13341 } 13342 /* 13343 * If this is a zero window probe, continue to 13344 * process the ACK part. But we need to set seg_len 13345 * to 0 to avoid data processing. Otherwise just 13346 * drop the segment and send back an ACK. 13347 */ 13348 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13349 flags &= ~(TH_SYN | TH_URG); 13350 seg_len = 0; 13351 goto process_ack; 13352 } else { 13353 freemsg(mp); 13354 goto ack_check; 13355 } 13356 } 13357 /* Pitch out of window stuff off the end. */ 13358 rgap = seg_len; 13359 mp2 = mp; 13360 do { 13361 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13362 (uintptr_t)INT_MAX); 13363 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13364 if (rgap < 0) { 13365 mp2->b_wptr += rgap; 13366 if ((mp1 = mp2->b_cont) != NULL) { 13367 mp2->b_cont = NULL; 13368 freemsg(mp1); 13369 } 13370 break; 13371 } 13372 } while ((mp2 = mp2->b_cont) != NULL); 13373 } 13374 ok:; 13375 /* 13376 * TCP should check ECN info for segments inside the window only. 13377 * Therefore the check should be done here. 13378 */ 13379 if (tcp->tcp_ecn_ok) { 13380 if (flags & TH_CWR) { 13381 tcp->tcp_ecn_echo_on = B_FALSE; 13382 } 13383 /* 13384 * Note that both ECN_CE and CWR can be set in the 13385 * same segment. In this case, we once again turn 13386 * on ECN_ECHO. 13387 */ 13388 if (tcp->tcp_ipversion == IPV4_VERSION) { 13389 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13390 13391 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13392 tcp->tcp_ecn_echo_on = B_TRUE; 13393 } 13394 } else { 13395 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13396 13397 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13398 htonl(IPH_ECN_CE << 20)) { 13399 tcp->tcp_ecn_echo_on = B_TRUE; 13400 } 13401 } 13402 } 13403 13404 /* 13405 * Check whether we can update tcp_ts_recent. This test is 13406 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13407 * Extensions for High Performance: An Update", Internet Draft. 13408 */ 13409 if (tcp->tcp_snd_ts_ok && 13410 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13411 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13412 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13413 tcp->tcp_last_rcv_lbolt = lbolt64; 13414 } 13415 13416 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13417 /* 13418 * FIN in an out of order segment. We record this in 13419 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13420 * Clear the FIN so that any check on FIN flag will fail. 13421 * Remember that FIN also counts in the sequence number 13422 * space. So we need to ack out of order FIN only segments. 13423 */ 13424 if (flags & TH_FIN) { 13425 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13426 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13427 flags &= ~TH_FIN; 13428 flags |= TH_ACK_NEEDED; 13429 } 13430 if (seg_len > 0) { 13431 /* Fill in the SACK blk list. */ 13432 if (tcp->tcp_snd_sack_ok) { 13433 ASSERT(tcp->tcp_sack_info != NULL); 13434 tcp_sack_insert(tcp->tcp_sack_list, 13435 seg_seq, seg_seq + seg_len, 13436 &(tcp->tcp_num_sack_blk)); 13437 } 13438 13439 /* 13440 * Attempt reassembly and see if we have something 13441 * ready to go. 13442 */ 13443 mp = tcp_reass(tcp, mp, seg_seq); 13444 /* Always ack out of order packets */ 13445 flags |= TH_ACK_NEEDED | TH_PUSH; 13446 if (mp) { 13447 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13448 (uintptr_t)INT_MAX); 13449 seg_len = mp->b_cont ? msgdsize(mp) : 13450 (int)(mp->b_wptr - mp->b_rptr); 13451 seg_seq = tcp->tcp_rnxt; 13452 /* 13453 * A gap is filled and the seq num and len 13454 * of the gap match that of a previously 13455 * received FIN, put the FIN flag back in. 13456 */ 13457 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13458 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13459 flags |= TH_FIN; 13460 tcp->tcp_valid_bits &= 13461 ~TCP_OFO_FIN_VALID; 13462 } 13463 } else { 13464 /* 13465 * Keep going even with NULL mp. 13466 * There may be a useful ACK or something else 13467 * we don't want to miss. 13468 * 13469 * But TCP should not perform fast retransmit 13470 * because of the ack number. TCP uses 13471 * seg_len == 0 to determine if it is a pure 13472 * ACK. And this is not a pure ACK. 13473 */ 13474 seg_len = 0; 13475 ofo_seg = B_TRUE; 13476 } 13477 } 13478 } else if (seg_len > 0) { 13479 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13480 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13481 /* 13482 * If an out of order FIN was received before, and the seq 13483 * num and len of the new segment match that of the FIN, 13484 * put the FIN flag back in. 13485 */ 13486 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13487 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13488 flags |= TH_FIN; 13489 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13490 } 13491 } 13492 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13493 if (flags & TH_RST) { 13494 freemsg(mp); 13495 switch (tcp->tcp_state) { 13496 case TCPS_SYN_RCVD: 13497 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13498 break; 13499 case TCPS_ESTABLISHED: 13500 case TCPS_FIN_WAIT_1: 13501 case TCPS_FIN_WAIT_2: 13502 case TCPS_CLOSE_WAIT: 13503 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13504 break; 13505 case TCPS_CLOSING: 13506 case TCPS_LAST_ACK: 13507 (void) tcp_clean_death(tcp, 0, 16); 13508 break; 13509 default: 13510 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13511 (void) tcp_clean_death(tcp, ENXIO, 17); 13512 break; 13513 } 13514 return; 13515 } 13516 if (flags & TH_SYN) { 13517 /* 13518 * See RFC 793, Page 71 13519 * 13520 * The seq number must be in the window as it should 13521 * be "fixed" above. If it is outside window, it should 13522 * be already rejected. Note that we allow seg_seq to be 13523 * rnxt + rwnd because we want to accept 0 window probe. 13524 */ 13525 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13526 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13527 freemsg(mp); 13528 /* 13529 * If the ACK flag is not set, just use our snxt as the 13530 * seq number of the RST segment. 13531 */ 13532 if (!(flags & TH_ACK)) { 13533 seg_ack = tcp->tcp_snxt; 13534 } 13535 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13536 TH_RST|TH_ACK); 13537 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13538 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13539 return; 13540 } 13541 /* 13542 * urp could be -1 when the urp field in the packet is 0 13543 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13544 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13545 */ 13546 if (flags & TH_URG && urp >= 0) { 13547 if (!tcp->tcp_urp_last_valid || 13548 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13549 if (IPCL_IS_NONSTR(connp)) { 13550 if (!TCP_IS_DETACHED(tcp)) { 13551 (*connp->conn_upcalls->su_signal_oob) 13552 (connp->conn_upper_handle, urp); 13553 } 13554 } else { 13555 /* 13556 * If we haven't generated the signal yet for 13557 * this urgent pointer value, do it now. Also, 13558 * send up a zero-length M_DATA indicating 13559 * whether or not this is the mark. The latter 13560 * is not needed when a T_EXDATA_IND is sent up. 13561 * However, if there are allocation failures 13562 * this code relies on the sender retransmitting 13563 * and the socket code for determining the mark 13564 * should not block waiting for the peer to 13565 * transmit. Thus, for simplicity we always 13566 * send up the mark indication. 13567 */ 13568 mp1 = allocb(0, BPRI_MED); 13569 if (mp1 == NULL) { 13570 freemsg(mp); 13571 return; 13572 } 13573 if (!TCP_IS_DETACHED(tcp) && 13574 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13575 SIGURG)) { 13576 /* Try again on the rexmit. */ 13577 freemsg(mp1); 13578 freemsg(mp); 13579 return; 13580 } 13581 /* 13582 * Mark with NOTMARKNEXT for now. 13583 * The code below will change this to MARKNEXT 13584 * if we are at the mark. 13585 * 13586 * If there are allocation failures (e.g. in 13587 * dupmsg below) the next time tcp_rput_data 13588 * sees the urgent segment it will send up the 13589 * MSGMARKNEXT message. 13590 */ 13591 mp1->b_flag |= MSGNOTMARKNEXT; 13592 freemsg(tcp->tcp_urp_mark_mp); 13593 tcp->tcp_urp_mark_mp = mp1; 13594 flags |= TH_SEND_URP_MARK; 13595 #ifdef DEBUG 13596 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13597 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13598 "last %x, %s", 13599 seg_seq, urp, tcp->tcp_urp_last, 13600 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13601 #endif /* DEBUG */ 13602 } 13603 tcp->tcp_urp_last_valid = B_TRUE; 13604 tcp->tcp_urp_last = urp + seg_seq; 13605 } else if (tcp->tcp_urp_mark_mp != NULL) { 13606 /* 13607 * An allocation failure prevented the previous 13608 * tcp_rput_data from sending up the allocated 13609 * MSG*MARKNEXT message - send it up this time 13610 * around. 13611 */ 13612 flags |= TH_SEND_URP_MARK; 13613 } 13614 13615 /* 13616 * If the urgent byte is in this segment, make sure that it is 13617 * all by itself. This makes it much easier to deal with the 13618 * possibility of an allocation failure on the T_exdata_ind. 13619 * Note that seg_len is the number of bytes in the segment, and 13620 * urp is the offset into the segment of the urgent byte. 13621 * urp < seg_len means that the urgent byte is in this segment. 13622 */ 13623 if (urp < seg_len) { 13624 if (seg_len != 1) { 13625 uint32_t tmp_rnxt; 13626 /* 13627 * Break it up and feed it back in. 13628 * Re-attach the IP header. 13629 */ 13630 mp->b_rptr = iphdr; 13631 if (urp > 0) { 13632 /* 13633 * There is stuff before the urgent 13634 * byte. 13635 */ 13636 mp1 = dupmsg(mp); 13637 if (!mp1) { 13638 /* 13639 * Trim from urgent byte on. 13640 * The rest will come back. 13641 */ 13642 (void) adjmsg(mp, 13643 urp - seg_len); 13644 tcp_rput_data(connp, 13645 mp, NULL); 13646 return; 13647 } 13648 (void) adjmsg(mp1, urp - seg_len); 13649 /* Feed this piece back in. */ 13650 tmp_rnxt = tcp->tcp_rnxt; 13651 tcp_rput_data(connp, mp1, NULL); 13652 /* 13653 * If the data passed back in was not 13654 * processed (ie: bad ACK) sending 13655 * the remainder back in will cause a 13656 * loop. In this case, drop the 13657 * packet and let the sender try 13658 * sending a good packet. 13659 */ 13660 if (tmp_rnxt == tcp->tcp_rnxt) { 13661 freemsg(mp); 13662 return; 13663 } 13664 } 13665 if (urp != seg_len - 1) { 13666 uint32_t tmp_rnxt; 13667 /* 13668 * There is stuff after the urgent 13669 * byte. 13670 */ 13671 mp1 = dupmsg(mp); 13672 if (!mp1) { 13673 /* 13674 * Trim everything beyond the 13675 * urgent byte. The rest will 13676 * come back. 13677 */ 13678 (void) adjmsg(mp, 13679 urp + 1 - seg_len); 13680 tcp_rput_data(connp, 13681 mp, NULL); 13682 return; 13683 } 13684 (void) adjmsg(mp1, urp + 1 - seg_len); 13685 tmp_rnxt = tcp->tcp_rnxt; 13686 tcp_rput_data(connp, mp1, NULL); 13687 /* 13688 * If the data passed back in was not 13689 * processed (ie: bad ACK) sending 13690 * the remainder back in will cause a 13691 * loop. In this case, drop the 13692 * packet and let the sender try 13693 * sending a good packet. 13694 */ 13695 if (tmp_rnxt == tcp->tcp_rnxt) { 13696 freemsg(mp); 13697 return; 13698 } 13699 } 13700 tcp_rput_data(connp, mp, NULL); 13701 return; 13702 } 13703 /* 13704 * This segment contains only the urgent byte. We 13705 * have to allocate the T_exdata_ind, if we can. 13706 */ 13707 if (IPCL_IS_NONSTR(connp)) { 13708 int error; 13709 13710 (*connp->conn_upcalls->su_recv) 13711 (connp->conn_upper_handle, mp, seg_len, 13712 MSG_OOB, &error, NULL); 13713 /* 13714 * We should never be in middle of a 13715 * fallback, the squeue guarantees that. 13716 */ 13717 ASSERT(error != EOPNOTSUPP); 13718 mp = NULL; 13719 goto update_ack; 13720 } else if (!tcp->tcp_urp_mp) { 13721 struct T_exdata_ind *tei; 13722 mp1 = allocb(sizeof (struct T_exdata_ind), 13723 BPRI_MED); 13724 if (!mp1) { 13725 /* 13726 * Sigh... It'll be back. 13727 * Generate any MSG*MARK message now. 13728 */ 13729 freemsg(mp); 13730 seg_len = 0; 13731 if (flags & TH_SEND_URP_MARK) { 13732 13733 13734 ASSERT(tcp->tcp_urp_mark_mp); 13735 tcp->tcp_urp_mark_mp->b_flag &= 13736 ~MSGNOTMARKNEXT; 13737 tcp->tcp_urp_mark_mp->b_flag |= 13738 MSGMARKNEXT; 13739 } 13740 goto ack_check; 13741 } 13742 mp1->b_datap->db_type = M_PROTO; 13743 tei = (struct T_exdata_ind *)mp1->b_rptr; 13744 tei->PRIM_type = T_EXDATA_IND; 13745 tei->MORE_flag = 0; 13746 mp1->b_wptr = (uchar_t *)&tei[1]; 13747 tcp->tcp_urp_mp = mp1; 13748 #ifdef DEBUG 13749 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13750 "tcp_rput: allocated exdata_ind %s", 13751 tcp_display(tcp, NULL, 13752 DISP_PORT_ONLY)); 13753 #endif /* DEBUG */ 13754 /* 13755 * There is no need to send a separate MSG*MARK 13756 * message since the T_EXDATA_IND will be sent 13757 * now. 13758 */ 13759 flags &= ~TH_SEND_URP_MARK; 13760 freemsg(tcp->tcp_urp_mark_mp); 13761 tcp->tcp_urp_mark_mp = NULL; 13762 } 13763 /* 13764 * Now we are all set. On the next putnext upstream, 13765 * tcp_urp_mp will be non-NULL and will get prepended 13766 * to what has to be this piece containing the urgent 13767 * byte. If for any reason we abort this segment below, 13768 * if it comes back, we will have this ready, or it 13769 * will get blown off in close. 13770 */ 13771 } else if (urp == seg_len) { 13772 /* 13773 * The urgent byte is the next byte after this sequence 13774 * number. If there is data it is marked with 13775 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13776 * since it is not needed. Otherwise, if the code 13777 * above just allocated a zero-length tcp_urp_mark_mp 13778 * message, that message is tagged with MSGMARKNEXT. 13779 * Sending up these MSGMARKNEXT messages makes 13780 * SIOCATMARK work correctly even though 13781 * the T_EXDATA_IND will not be sent up until the 13782 * urgent byte arrives. 13783 */ 13784 if (seg_len != 0) { 13785 flags |= TH_MARKNEXT_NEEDED; 13786 freemsg(tcp->tcp_urp_mark_mp); 13787 tcp->tcp_urp_mark_mp = NULL; 13788 flags &= ~TH_SEND_URP_MARK; 13789 } else if (tcp->tcp_urp_mark_mp != NULL) { 13790 flags |= TH_SEND_URP_MARK; 13791 tcp->tcp_urp_mark_mp->b_flag &= 13792 ~MSGNOTMARKNEXT; 13793 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13794 } 13795 #ifdef DEBUG 13796 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13797 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13798 seg_len, flags, 13799 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13800 #endif /* DEBUG */ 13801 } 13802 #ifdef DEBUG 13803 else { 13804 /* Data left until we hit mark */ 13805 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13806 "tcp_rput: URP %d bytes left, %s", 13807 urp - seg_len, tcp_display(tcp, NULL, 13808 DISP_PORT_ONLY)); 13809 } 13810 #endif /* DEBUG */ 13811 } 13812 13813 process_ack: 13814 if (!(flags & TH_ACK)) { 13815 freemsg(mp); 13816 goto xmit_check; 13817 } 13818 } 13819 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13820 13821 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13822 tcp->tcp_ip_forward_progress = B_TRUE; 13823 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13824 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13825 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13826 /* 3-way handshake complete - pass up the T_CONN_IND */ 13827 tcp_t *listener = tcp->tcp_listener; 13828 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13829 13830 tcp->tcp_tconnind_started = B_TRUE; 13831 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13832 /* 13833 * We are here means eager is fine but it can 13834 * get a TH_RST at any point between now and till 13835 * accept completes and disappear. We need to 13836 * ensure that reference to eager is valid after 13837 * we get out of eager's perimeter. So we do 13838 * an extra refhold. 13839 */ 13840 CONN_INC_REF(connp); 13841 13842 /* 13843 * The listener also exists because of the refhold 13844 * done in tcp_conn_request. Its possible that it 13845 * might have closed. We will check that once we 13846 * get inside listeners context. 13847 */ 13848 CONN_INC_REF(listener->tcp_connp); 13849 if (listener->tcp_connp->conn_sqp == 13850 connp->conn_sqp) { 13851 /* 13852 * We optimize by not calling an SQUEUE_ENTER 13853 * on the listener since we know that the 13854 * listener and eager squeues are the same. 13855 * We are able to make this check safely only 13856 * because neither the eager nor the listener 13857 * can change its squeue. Only an active connect 13858 * can change its squeue 13859 */ 13860 tcp_send_conn_ind(listener->tcp_connp, mp, 13861 listener->tcp_connp->conn_sqp); 13862 CONN_DEC_REF(listener->tcp_connp); 13863 } else if (!tcp->tcp_loopback) { 13864 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13865 mp, tcp_send_conn_ind, 13866 listener->tcp_connp, SQ_FILL, 13867 SQTAG_TCP_CONN_IND); 13868 } else { 13869 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13870 mp, tcp_send_conn_ind, 13871 listener->tcp_connp, SQ_PROCESS, 13872 SQTAG_TCP_CONN_IND); 13873 } 13874 } 13875 13876 if (tcp->tcp_active_open) { 13877 /* 13878 * We are seeing the final ack in the three way 13879 * hand shake of a active open'ed connection 13880 * so we must send up a T_CONN_CON 13881 */ 13882 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13883 freemsg(mp); 13884 return; 13885 } 13886 /* 13887 * Don't fuse the loopback endpoints for 13888 * simultaneous active opens. 13889 */ 13890 if (tcp->tcp_loopback) { 13891 TCP_STAT(tcps, tcp_fusion_unfusable); 13892 tcp->tcp_unfusable = B_TRUE; 13893 } 13894 } 13895 13896 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 13897 bytes_acked--; 13898 /* SYN was acked - making progress */ 13899 if (tcp->tcp_ipversion == IPV6_VERSION) 13900 tcp->tcp_ip_forward_progress = B_TRUE; 13901 13902 /* 13903 * If SYN was retransmitted, need to reset all 13904 * retransmission info as this segment will be 13905 * treated as a dup ACK. 13906 */ 13907 if (tcp->tcp_rexmit) { 13908 tcp->tcp_rexmit = B_FALSE; 13909 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13910 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13911 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13912 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13913 tcp->tcp_ms_we_have_waited = 0; 13914 tcp->tcp_cwnd = mss; 13915 } 13916 13917 /* 13918 * We set the send window to zero here. 13919 * This is needed if there is data to be 13920 * processed already on the queue. 13921 * Later (at swnd_update label), the 13922 * "new_swnd > tcp_swnd" condition is satisfied 13923 * the XMIT_NEEDED flag is set in the current 13924 * (SYN_RCVD) state. This ensures tcp_wput_data() is 13925 * called if there is already data on queue in 13926 * this state. 13927 */ 13928 tcp->tcp_swnd = 0; 13929 13930 if (new_swnd > tcp->tcp_max_swnd) 13931 tcp->tcp_max_swnd = new_swnd; 13932 tcp->tcp_swl1 = seg_seq; 13933 tcp->tcp_swl2 = seg_ack; 13934 tcp->tcp_state = TCPS_ESTABLISHED; 13935 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13936 13937 /* Fuse when both sides are in ESTABLISHED state */ 13938 if (tcp->tcp_loopback && do_tcp_fusion) 13939 tcp_fuse(tcp, iphdr, tcph); 13940 13941 } 13942 /* This code follows 4.4BSD-Lite2 mostly. */ 13943 if (bytes_acked < 0) 13944 goto est; 13945 13946 /* 13947 * If TCP is ECN capable and the congestion experience bit is 13948 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 13949 * done once per window (or more loosely, per RTT). 13950 */ 13951 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 13952 tcp->tcp_cwr = B_FALSE; 13953 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 13954 if (!tcp->tcp_cwr) { 13955 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 13956 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 13957 tcp->tcp_cwnd = npkt * mss; 13958 /* 13959 * If the cwnd is 0, use the timer to clock out 13960 * new segments. This is required by the ECN spec. 13961 */ 13962 if (npkt == 0) { 13963 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13964 /* 13965 * This makes sure that when the ACK comes 13966 * back, we will increase tcp_cwnd by 1 MSS. 13967 */ 13968 tcp->tcp_cwnd_cnt = 0; 13969 } 13970 tcp->tcp_cwr = B_TRUE; 13971 /* 13972 * This marks the end of the current window of in 13973 * flight data. That is why we don't use 13974 * tcp_suna + tcp_swnd. Only data in flight can 13975 * provide ECN info. 13976 */ 13977 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13978 tcp->tcp_ecn_cwr_sent = B_FALSE; 13979 } 13980 } 13981 13982 mp1 = tcp->tcp_xmit_head; 13983 if (bytes_acked == 0) { 13984 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 13985 int dupack_cnt; 13986 13987 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 13988 /* 13989 * Fast retransmit. When we have seen exactly three 13990 * identical ACKs while we have unacked data 13991 * outstanding we take it as a hint that our peer 13992 * dropped something. 13993 * 13994 * If TCP is retransmitting, don't do fast retransmit. 13995 */ 13996 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 13997 ! tcp->tcp_rexmit) { 13998 /* Do Limited Transmit */ 13999 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14000 tcps->tcps_dupack_fast_retransmit) { 14001 /* 14002 * RFC 3042 14003 * 14004 * What we need to do is temporarily 14005 * increase tcp_cwnd so that new 14006 * data can be sent if it is allowed 14007 * by the receive window (tcp_rwnd). 14008 * tcp_wput_data() will take care of 14009 * the rest. 14010 * 14011 * If the connection is SACK capable, 14012 * only do limited xmit when there 14013 * is SACK info. 14014 * 14015 * Note how tcp_cwnd is incremented. 14016 * The first dup ACK will increase 14017 * it by 1 MSS. The second dup ACK 14018 * will increase it by 2 MSS. This 14019 * means that only 1 new segment will 14020 * be sent for each dup ACK. 14021 */ 14022 if (tcp->tcp_unsent > 0 && 14023 (!tcp->tcp_snd_sack_ok || 14024 (tcp->tcp_snd_sack_ok && 14025 tcp->tcp_notsack_list != NULL))) { 14026 tcp->tcp_cwnd += mss << 14027 (tcp->tcp_dupack_cnt - 1); 14028 flags |= TH_LIMIT_XMIT; 14029 } 14030 } else if (dupack_cnt == 14031 tcps->tcps_dupack_fast_retransmit) { 14032 14033 /* 14034 * If we have reduced tcp_ssthresh 14035 * because of ECN, do not reduce it again 14036 * unless it is already one window of data 14037 * away. After one window of data, tcp_cwr 14038 * should then be cleared. Note that 14039 * for non ECN capable connection, tcp_cwr 14040 * should always be false. 14041 * 14042 * Adjust cwnd since the duplicate 14043 * ack indicates that a packet was 14044 * dropped (due to congestion.) 14045 */ 14046 if (!tcp->tcp_cwr) { 14047 npkt = ((tcp->tcp_snxt - 14048 tcp->tcp_suna) >> 1) / mss; 14049 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14050 mss; 14051 tcp->tcp_cwnd = (npkt + 14052 tcp->tcp_dupack_cnt) * mss; 14053 } 14054 if (tcp->tcp_ecn_ok) { 14055 tcp->tcp_cwr = B_TRUE; 14056 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14057 tcp->tcp_ecn_cwr_sent = B_FALSE; 14058 } 14059 14060 /* 14061 * We do Hoe's algorithm. Refer to her 14062 * paper "Improving the Start-up Behavior 14063 * of a Congestion Control Scheme for TCP," 14064 * appeared in SIGCOMM'96. 14065 * 14066 * Save highest seq no we have sent so far. 14067 * Be careful about the invisible FIN byte. 14068 */ 14069 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14070 (tcp->tcp_unsent == 0)) { 14071 tcp->tcp_rexmit_max = tcp->tcp_fss; 14072 } else { 14073 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14074 } 14075 14076 /* 14077 * Do not allow bursty traffic during. 14078 * fast recovery. Refer to Fall and Floyd's 14079 * paper "Simulation-based Comparisons of 14080 * Tahoe, Reno and SACK TCP" (in CCR?) 14081 * This is a best current practise. 14082 */ 14083 tcp->tcp_snd_burst = TCP_CWND_SS; 14084 14085 /* 14086 * For SACK: 14087 * Calculate tcp_pipe, which is the 14088 * estimated number of bytes in 14089 * network. 14090 * 14091 * tcp_fack is the highest sack'ed seq num 14092 * TCP has received. 14093 * 14094 * tcp_pipe is explained in the above quoted 14095 * Fall and Floyd's paper. tcp_fack is 14096 * explained in Mathis and Mahdavi's 14097 * "Forward Acknowledgment: Refining TCP 14098 * Congestion Control" in SIGCOMM '96. 14099 */ 14100 if (tcp->tcp_snd_sack_ok) { 14101 ASSERT(tcp->tcp_sack_info != NULL); 14102 if (tcp->tcp_notsack_list != NULL) { 14103 tcp->tcp_pipe = tcp->tcp_snxt - 14104 tcp->tcp_fack; 14105 tcp->tcp_sack_snxt = seg_ack; 14106 flags |= TH_NEED_SACK_REXMIT; 14107 } else { 14108 /* 14109 * Always initialize tcp_pipe 14110 * even though we don't have 14111 * any SACK info. If later 14112 * we get SACK info and 14113 * tcp_pipe is not initialized, 14114 * funny things will happen. 14115 */ 14116 tcp->tcp_pipe = 14117 tcp->tcp_cwnd_ssthresh; 14118 } 14119 } else { 14120 flags |= TH_REXMIT_NEEDED; 14121 } /* tcp_snd_sack_ok */ 14122 14123 } else { 14124 /* 14125 * Here we perform congestion 14126 * avoidance, but NOT slow start. 14127 * This is known as the Fast 14128 * Recovery Algorithm. 14129 */ 14130 if (tcp->tcp_snd_sack_ok && 14131 tcp->tcp_notsack_list != NULL) { 14132 flags |= TH_NEED_SACK_REXMIT; 14133 tcp->tcp_pipe -= mss; 14134 if (tcp->tcp_pipe < 0) 14135 tcp->tcp_pipe = 0; 14136 } else { 14137 /* 14138 * We know that one more packet has 14139 * left the pipe thus we can update 14140 * cwnd. 14141 */ 14142 cwnd = tcp->tcp_cwnd + mss; 14143 if (cwnd > tcp->tcp_cwnd_max) 14144 cwnd = tcp->tcp_cwnd_max; 14145 tcp->tcp_cwnd = cwnd; 14146 if (tcp->tcp_unsent > 0) 14147 flags |= TH_XMIT_NEEDED; 14148 } 14149 } 14150 } 14151 } else if (tcp->tcp_zero_win_probe) { 14152 /* 14153 * If the window has opened, need to arrange 14154 * to send additional data. 14155 */ 14156 if (new_swnd != 0) { 14157 /* tcp_suna != tcp_snxt */ 14158 /* Packet contains a window update */ 14159 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14160 tcp->tcp_zero_win_probe = 0; 14161 tcp->tcp_timer_backoff = 0; 14162 tcp->tcp_ms_we_have_waited = 0; 14163 14164 /* 14165 * Transmit starting with tcp_suna since 14166 * the one byte probe is not ack'ed. 14167 * If TCP has sent more than one identical 14168 * probe, tcp_rexmit will be set. That means 14169 * tcp_ss_rexmit() will send out the one 14170 * byte along with new data. Otherwise, 14171 * fake the retransmission. 14172 */ 14173 flags |= TH_XMIT_NEEDED; 14174 if (!tcp->tcp_rexmit) { 14175 tcp->tcp_rexmit = B_TRUE; 14176 tcp->tcp_dupack_cnt = 0; 14177 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14178 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14179 } 14180 } 14181 } 14182 goto swnd_update; 14183 } 14184 14185 /* 14186 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14187 * If the ACK value acks something that we have not yet sent, it might 14188 * be an old duplicate segment. Send an ACK to re-synchronize the 14189 * other side. 14190 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14191 * state is handled above, so we can always just drop the segment and 14192 * send an ACK here. 14193 * 14194 * Should we send ACKs in response to ACK only segments? 14195 */ 14196 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14197 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14198 /* drop the received segment */ 14199 freemsg(mp); 14200 14201 /* 14202 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14203 * greater than 0, check if the number of such 14204 * bogus ACks is greater than that count. If yes, 14205 * don't send back any ACK. This prevents TCP from 14206 * getting into an ACK storm if somehow an attacker 14207 * successfully spoofs an acceptable segment to our 14208 * peer. 14209 */ 14210 if (tcp_drop_ack_unsent_cnt > 0 && 14211 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14212 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14213 return; 14214 } 14215 mp = tcp_ack_mp(tcp); 14216 if (mp != NULL) { 14217 BUMP_LOCAL(tcp->tcp_obsegs); 14218 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14219 tcp_send_data(tcp, tcp->tcp_wq, mp); 14220 } 14221 return; 14222 } 14223 14224 /* 14225 * TCP gets a new ACK, update the notsack'ed list to delete those 14226 * blocks that are covered by this ACK. 14227 */ 14228 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14229 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14230 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14231 } 14232 14233 /* 14234 * If we got an ACK after fast retransmit, check to see 14235 * if it is a partial ACK. If it is not and the congestion 14236 * window was inflated to account for the other side's 14237 * cached packets, retract it. If it is, do Hoe's algorithm. 14238 */ 14239 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14240 ASSERT(tcp->tcp_rexmit == B_FALSE); 14241 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14242 tcp->tcp_dupack_cnt = 0; 14243 /* 14244 * Restore the orig tcp_cwnd_ssthresh after 14245 * fast retransmit phase. 14246 */ 14247 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14248 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14249 } 14250 tcp->tcp_rexmit_max = seg_ack; 14251 tcp->tcp_cwnd_cnt = 0; 14252 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14253 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14254 14255 /* 14256 * Remove all notsack info to avoid confusion with 14257 * the next fast retrasnmit/recovery phase. 14258 */ 14259 if (tcp->tcp_snd_sack_ok && 14260 tcp->tcp_notsack_list != NULL) { 14261 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14262 } 14263 } else { 14264 if (tcp->tcp_snd_sack_ok && 14265 tcp->tcp_notsack_list != NULL) { 14266 flags |= TH_NEED_SACK_REXMIT; 14267 tcp->tcp_pipe -= mss; 14268 if (tcp->tcp_pipe < 0) 14269 tcp->tcp_pipe = 0; 14270 } else { 14271 /* 14272 * Hoe's algorithm: 14273 * 14274 * Retransmit the unack'ed segment and 14275 * restart fast recovery. Note that we 14276 * need to scale back tcp_cwnd to the 14277 * original value when we started fast 14278 * recovery. This is to prevent overly 14279 * aggressive behaviour in sending new 14280 * segments. 14281 */ 14282 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14283 tcps->tcps_dupack_fast_retransmit * mss; 14284 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14285 flags |= TH_REXMIT_NEEDED; 14286 } 14287 } 14288 } else { 14289 tcp->tcp_dupack_cnt = 0; 14290 if (tcp->tcp_rexmit) { 14291 /* 14292 * TCP is retranmitting. If the ACK ack's all 14293 * outstanding data, update tcp_rexmit_max and 14294 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14295 * to the correct value. 14296 * 14297 * Note that SEQ_LEQ() is used. This is to avoid 14298 * unnecessary fast retransmit caused by dup ACKs 14299 * received when TCP does slow start retransmission 14300 * after a time out. During this phase, TCP may 14301 * send out segments which are already received. 14302 * This causes dup ACKs to be sent back. 14303 */ 14304 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14305 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14306 tcp->tcp_rexmit_nxt = seg_ack; 14307 } 14308 if (seg_ack != tcp->tcp_rexmit_max) { 14309 flags |= TH_XMIT_NEEDED; 14310 } 14311 } else { 14312 tcp->tcp_rexmit = B_FALSE; 14313 tcp->tcp_xmit_zc_clean = B_FALSE; 14314 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14315 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14316 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14317 } 14318 tcp->tcp_ms_we_have_waited = 0; 14319 } 14320 } 14321 14322 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14323 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14324 tcp->tcp_suna = seg_ack; 14325 if (tcp->tcp_zero_win_probe != 0) { 14326 tcp->tcp_zero_win_probe = 0; 14327 tcp->tcp_timer_backoff = 0; 14328 } 14329 14330 /* 14331 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14332 * Note that it cannot be the SYN being ack'ed. The code flow 14333 * will not reach here. 14334 */ 14335 if (mp1 == NULL) { 14336 goto fin_acked; 14337 } 14338 14339 /* 14340 * Update the congestion window. 14341 * 14342 * If TCP is not ECN capable or TCP is ECN capable but the 14343 * congestion experience bit is not set, increase the tcp_cwnd as 14344 * usual. 14345 */ 14346 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14347 cwnd = tcp->tcp_cwnd; 14348 add = mss; 14349 14350 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14351 /* 14352 * This is to prevent an increase of less than 1 MSS of 14353 * tcp_cwnd. With partial increase, tcp_wput_data() 14354 * may send out tinygrams in order to preserve mblk 14355 * boundaries. 14356 * 14357 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14358 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14359 * increased by 1 MSS for every RTTs. 14360 */ 14361 if (tcp->tcp_cwnd_cnt <= 0) { 14362 tcp->tcp_cwnd_cnt = cwnd + add; 14363 } else { 14364 tcp->tcp_cwnd_cnt -= add; 14365 add = 0; 14366 } 14367 } 14368 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14369 } 14370 14371 /* See if the latest urgent data has been acknowledged */ 14372 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14373 SEQ_GT(seg_ack, tcp->tcp_urg)) 14374 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14375 14376 /* Can we update the RTT estimates? */ 14377 if (tcp->tcp_snd_ts_ok) { 14378 /* Ignore zero timestamp echo-reply. */ 14379 if (tcpopt.tcp_opt_ts_ecr != 0) { 14380 tcp_set_rto(tcp, (int32_t)lbolt - 14381 (int32_t)tcpopt.tcp_opt_ts_ecr); 14382 } 14383 14384 /* If needed, restart the timer. */ 14385 if (tcp->tcp_set_timer == 1) { 14386 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14387 tcp->tcp_set_timer = 0; 14388 } 14389 /* 14390 * Update tcp_csuna in case the other side stops sending 14391 * us timestamps. 14392 */ 14393 tcp->tcp_csuna = tcp->tcp_snxt; 14394 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14395 /* 14396 * An ACK sequence we haven't seen before, so get the RTT 14397 * and update the RTO. But first check if the timestamp is 14398 * valid to use. 14399 */ 14400 if ((mp1->b_next != NULL) && 14401 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14402 tcp_set_rto(tcp, (int32_t)lbolt - 14403 (int32_t)(intptr_t)mp1->b_prev); 14404 else 14405 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14406 14407 /* Remeber the last sequence to be ACKed */ 14408 tcp->tcp_csuna = seg_ack; 14409 if (tcp->tcp_set_timer == 1) { 14410 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14411 tcp->tcp_set_timer = 0; 14412 } 14413 } else { 14414 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14415 } 14416 14417 /* Eat acknowledged bytes off the xmit queue. */ 14418 for (;;) { 14419 mblk_t *mp2; 14420 uchar_t *wptr; 14421 14422 wptr = mp1->b_wptr; 14423 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14424 bytes_acked -= (int)(wptr - mp1->b_rptr); 14425 if (bytes_acked < 0) { 14426 mp1->b_rptr = wptr + bytes_acked; 14427 /* 14428 * Set a new timestamp if all the bytes timed by the 14429 * old timestamp have been ack'ed. 14430 */ 14431 if (SEQ_GT(seg_ack, 14432 (uint32_t)(uintptr_t)(mp1->b_next))) { 14433 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14434 mp1->b_next = NULL; 14435 } 14436 break; 14437 } 14438 mp1->b_next = NULL; 14439 mp1->b_prev = NULL; 14440 mp2 = mp1; 14441 mp1 = mp1->b_cont; 14442 14443 /* 14444 * This notification is required for some zero-copy 14445 * clients to maintain a copy semantic. After the data 14446 * is ack'ed, client is safe to modify or reuse the buffer. 14447 */ 14448 if (tcp->tcp_snd_zcopy_aware && 14449 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14450 tcp_zcopy_notify(tcp); 14451 freeb(mp2); 14452 if (bytes_acked == 0) { 14453 if (mp1 == NULL) { 14454 /* Everything is ack'ed, clear the tail. */ 14455 tcp->tcp_xmit_tail = NULL; 14456 /* 14457 * Cancel the timer unless we are still 14458 * waiting for an ACK for the FIN packet. 14459 */ 14460 if (tcp->tcp_timer_tid != 0 && 14461 tcp->tcp_snxt == tcp->tcp_suna) { 14462 (void) TCP_TIMER_CANCEL(tcp, 14463 tcp->tcp_timer_tid); 14464 tcp->tcp_timer_tid = 0; 14465 } 14466 goto pre_swnd_update; 14467 } 14468 if (mp2 != tcp->tcp_xmit_tail) 14469 break; 14470 tcp->tcp_xmit_tail = mp1; 14471 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14472 (uintptr_t)INT_MAX); 14473 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14474 mp1->b_rptr); 14475 break; 14476 } 14477 if (mp1 == NULL) { 14478 /* 14479 * More was acked but there is nothing more 14480 * outstanding. This means that the FIN was 14481 * just acked or that we're talking to a clown. 14482 */ 14483 fin_acked: 14484 ASSERT(tcp->tcp_fin_sent); 14485 tcp->tcp_xmit_tail = NULL; 14486 if (tcp->tcp_fin_sent) { 14487 /* FIN was acked - making progress */ 14488 if (tcp->tcp_ipversion == IPV6_VERSION && 14489 !tcp->tcp_fin_acked) 14490 tcp->tcp_ip_forward_progress = B_TRUE; 14491 tcp->tcp_fin_acked = B_TRUE; 14492 if (tcp->tcp_linger_tid != 0 && 14493 TCP_TIMER_CANCEL(tcp, 14494 tcp->tcp_linger_tid) >= 0) { 14495 tcp_stop_lingering(tcp); 14496 freemsg(mp); 14497 mp = NULL; 14498 } 14499 } else { 14500 /* 14501 * We should never get here because 14502 * we have already checked that the 14503 * number of bytes ack'ed should be 14504 * smaller than or equal to what we 14505 * have sent so far (it is the 14506 * acceptability check of the ACK). 14507 * We can only get here if the send 14508 * queue is corrupted. 14509 * 14510 * Terminate the connection and 14511 * panic the system. It is better 14512 * for us to panic instead of 14513 * continuing to avoid other disaster. 14514 */ 14515 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14516 tcp->tcp_rnxt, TH_RST|TH_ACK); 14517 panic("Memory corruption " 14518 "detected for connection %s.", 14519 tcp_display(tcp, NULL, 14520 DISP_ADDR_AND_PORT)); 14521 /*NOTREACHED*/ 14522 } 14523 goto pre_swnd_update; 14524 } 14525 ASSERT(mp2 != tcp->tcp_xmit_tail); 14526 } 14527 if (tcp->tcp_unsent) { 14528 flags |= TH_XMIT_NEEDED; 14529 } 14530 pre_swnd_update: 14531 tcp->tcp_xmit_head = mp1; 14532 swnd_update: 14533 /* 14534 * The following check is different from most other implementations. 14535 * For bi-directional transfer, when segments are dropped, the 14536 * "normal" check will not accept a window update in those 14537 * retransmitted segemnts. Failing to do that, TCP may send out 14538 * segments which are outside receiver's window. As TCP accepts 14539 * the ack in those retransmitted segments, if the window update in 14540 * the same segment is not accepted, TCP will incorrectly calculates 14541 * that it can send more segments. This can create a deadlock 14542 * with the receiver if its window becomes zero. 14543 */ 14544 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14545 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14546 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14547 /* 14548 * The criteria for update is: 14549 * 14550 * 1. the segment acknowledges some data. Or 14551 * 2. the segment is new, i.e. it has a higher seq num. Or 14552 * 3. the segment is not old and the advertised window is 14553 * larger than the previous advertised window. 14554 */ 14555 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14556 flags |= TH_XMIT_NEEDED; 14557 tcp->tcp_swnd = new_swnd; 14558 if (new_swnd > tcp->tcp_max_swnd) 14559 tcp->tcp_max_swnd = new_swnd; 14560 tcp->tcp_swl1 = seg_seq; 14561 tcp->tcp_swl2 = seg_ack; 14562 } 14563 est: 14564 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14565 14566 switch (tcp->tcp_state) { 14567 case TCPS_FIN_WAIT_1: 14568 if (tcp->tcp_fin_acked) { 14569 tcp->tcp_state = TCPS_FIN_WAIT_2; 14570 /* 14571 * We implement the non-standard BSD/SunOS 14572 * FIN_WAIT_2 flushing algorithm. 14573 * If there is no user attached to this 14574 * TCP endpoint, then this TCP struct 14575 * could hang around forever in FIN_WAIT_2 14576 * state if the peer forgets to send us 14577 * a FIN. To prevent this, we wait only 14578 * 2*MSL (a convenient time value) for 14579 * the FIN to arrive. If it doesn't show up, 14580 * we flush the TCP endpoint. This algorithm, 14581 * though a violation of RFC-793, has worked 14582 * for over 10 years in BSD systems. 14583 * Note: SunOS 4.x waits 675 seconds before 14584 * flushing the FIN_WAIT_2 connection. 14585 */ 14586 TCP_TIMER_RESTART(tcp, 14587 tcps->tcps_fin_wait_2_flush_interval); 14588 } 14589 break; 14590 case TCPS_FIN_WAIT_2: 14591 break; /* Shutdown hook? */ 14592 case TCPS_LAST_ACK: 14593 freemsg(mp); 14594 if (tcp->tcp_fin_acked) { 14595 (void) tcp_clean_death(tcp, 0, 19); 14596 return; 14597 } 14598 goto xmit_check; 14599 case TCPS_CLOSING: 14600 if (tcp->tcp_fin_acked) { 14601 tcp->tcp_state = TCPS_TIME_WAIT; 14602 /* 14603 * Unconditionally clear the exclusive binding 14604 * bit so this TIME-WAIT connection won't 14605 * interfere with new ones. 14606 */ 14607 tcp->tcp_exclbind = 0; 14608 if (!TCP_IS_DETACHED(tcp)) { 14609 TCP_TIMER_RESTART(tcp, 14610 tcps->tcps_time_wait_interval); 14611 } else { 14612 tcp_time_wait_append(tcp); 14613 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14614 } 14615 } 14616 /*FALLTHRU*/ 14617 case TCPS_CLOSE_WAIT: 14618 freemsg(mp); 14619 goto xmit_check; 14620 default: 14621 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14622 break; 14623 } 14624 } 14625 if (flags & TH_FIN) { 14626 /* Make sure we ack the fin */ 14627 flags |= TH_ACK_NEEDED; 14628 if (!tcp->tcp_fin_rcvd) { 14629 tcp->tcp_fin_rcvd = B_TRUE; 14630 tcp->tcp_rnxt++; 14631 tcph = tcp->tcp_tcph; 14632 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14633 14634 /* 14635 * Generate the ordrel_ind at the end unless we 14636 * are an eager guy. 14637 * In the eager case tcp_rsrv will do this when run 14638 * after tcp_accept is done. 14639 */ 14640 if (tcp->tcp_listener == NULL && 14641 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14642 flags |= TH_ORDREL_NEEDED; 14643 switch (tcp->tcp_state) { 14644 case TCPS_SYN_RCVD: 14645 case TCPS_ESTABLISHED: 14646 tcp->tcp_state = TCPS_CLOSE_WAIT; 14647 /* Keepalive? */ 14648 break; 14649 case TCPS_FIN_WAIT_1: 14650 if (!tcp->tcp_fin_acked) { 14651 tcp->tcp_state = TCPS_CLOSING; 14652 break; 14653 } 14654 /* FALLTHRU */ 14655 case TCPS_FIN_WAIT_2: 14656 tcp->tcp_state = TCPS_TIME_WAIT; 14657 /* 14658 * Unconditionally clear the exclusive binding 14659 * bit so this TIME-WAIT connection won't 14660 * interfere with new ones. 14661 */ 14662 tcp->tcp_exclbind = 0; 14663 if (!TCP_IS_DETACHED(tcp)) { 14664 TCP_TIMER_RESTART(tcp, 14665 tcps->tcps_time_wait_interval); 14666 } else { 14667 tcp_time_wait_append(tcp); 14668 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14669 } 14670 if (seg_len) { 14671 /* 14672 * implies data piggybacked on FIN. 14673 * break to handle data. 14674 */ 14675 break; 14676 } 14677 freemsg(mp); 14678 goto ack_check; 14679 } 14680 } 14681 } 14682 if (mp == NULL) 14683 goto xmit_check; 14684 if (seg_len == 0) { 14685 freemsg(mp); 14686 goto xmit_check; 14687 } 14688 if (mp->b_rptr == mp->b_wptr) { 14689 /* 14690 * The header has been consumed, so we remove the 14691 * zero-length mblk here. 14692 */ 14693 mp1 = mp; 14694 mp = mp->b_cont; 14695 freeb(mp1); 14696 } 14697 update_ack: 14698 tcph = tcp->tcp_tcph; 14699 tcp->tcp_rack_cnt++; 14700 { 14701 uint32_t cur_max; 14702 14703 cur_max = tcp->tcp_rack_cur_max; 14704 if (tcp->tcp_rack_cnt >= cur_max) { 14705 /* 14706 * We have more unacked data than we should - send 14707 * an ACK now. 14708 */ 14709 flags |= TH_ACK_NEEDED; 14710 cur_max++; 14711 if (cur_max > tcp->tcp_rack_abs_max) 14712 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14713 else 14714 tcp->tcp_rack_cur_max = cur_max; 14715 } else if (TCP_IS_DETACHED(tcp)) { 14716 /* We don't have an ACK timer for detached TCP. */ 14717 flags |= TH_ACK_NEEDED; 14718 } else if (seg_len < mss) { 14719 /* 14720 * If we get a segment that is less than an mss, and we 14721 * already have unacknowledged data, and the amount 14722 * unacknowledged is not a multiple of mss, then we 14723 * better generate an ACK now. Otherwise, this may be 14724 * the tail piece of a transaction, and we would rather 14725 * wait for the response. 14726 */ 14727 uint32_t udif; 14728 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14729 (uintptr_t)INT_MAX); 14730 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14731 if (udif && (udif % mss)) 14732 flags |= TH_ACK_NEEDED; 14733 else 14734 flags |= TH_ACK_TIMER_NEEDED; 14735 } else { 14736 /* Start delayed ack timer */ 14737 flags |= TH_ACK_TIMER_NEEDED; 14738 } 14739 } 14740 tcp->tcp_rnxt += seg_len; 14741 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14742 14743 if (mp == NULL) 14744 goto xmit_check; 14745 14746 /* Update SACK list */ 14747 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14748 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14749 &(tcp->tcp_num_sack_blk)); 14750 } 14751 14752 if (tcp->tcp_urp_mp) { 14753 tcp->tcp_urp_mp->b_cont = mp; 14754 mp = tcp->tcp_urp_mp; 14755 tcp->tcp_urp_mp = NULL; 14756 /* Ready for a new signal. */ 14757 tcp->tcp_urp_last_valid = B_FALSE; 14758 #ifdef DEBUG 14759 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14760 "tcp_rput: sending exdata_ind %s", 14761 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14762 #endif /* DEBUG */ 14763 } 14764 14765 /* 14766 * Check for ancillary data changes compared to last segment. 14767 */ 14768 if (tcp->tcp_ipv6_recvancillary != 0) { 14769 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14770 ASSERT(mp != NULL); 14771 } 14772 14773 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14774 /* 14775 * Side queue inbound data until the accept happens. 14776 * tcp_accept/tcp_rput drains this when the accept happens. 14777 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14778 * T_EXDATA_IND) it is queued on b_next. 14779 * XXX Make urgent data use this. Requires: 14780 * Removing tcp_listener check for TH_URG 14781 * Making M_PCPROTO and MARK messages skip the eager case 14782 */ 14783 14784 if (tcp->tcp_kssl_pending) { 14785 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 14786 mblk_t *, mp); 14787 tcp_kssl_input(tcp, mp); 14788 } else { 14789 tcp_rcv_enqueue(tcp, mp, seg_len); 14790 } 14791 } else { 14792 if (mp->b_datap->db_type != M_DATA || 14793 (flags & TH_MARKNEXT_NEEDED)) { 14794 if (IPCL_IS_NONSTR(connp)) { 14795 int error; 14796 14797 if ((*connp->conn_upcalls->su_recv) 14798 (connp->conn_upper_handle, mp, 14799 seg_len, 0, &error, NULL) <= 0) { 14800 /* 14801 * We should never be in middle of a 14802 * fallback, the squeue guarantees that. 14803 */ 14804 ASSERT(error != EOPNOTSUPP); 14805 if (error == ENOSPC) 14806 tcp->tcp_rwnd -= seg_len; 14807 } 14808 } else if (tcp->tcp_rcv_list != NULL) { 14809 flags |= tcp_rcv_drain(tcp); 14810 } 14811 ASSERT(tcp->tcp_rcv_list == NULL || 14812 tcp->tcp_fused_sigurg); 14813 14814 if (flags & TH_MARKNEXT_NEEDED) { 14815 #ifdef DEBUG 14816 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14817 "tcp_rput: sending MSGMARKNEXT %s", 14818 tcp_display(tcp, NULL, 14819 DISP_PORT_ONLY)); 14820 #endif /* DEBUG */ 14821 mp->b_flag |= MSGMARKNEXT; 14822 flags &= ~TH_MARKNEXT_NEEDED; 14823 } 14824 14825 /* Does this need SSL processing first? */ 14826 if ((tcp->tcp_kssl_ctx != NULL) && 14827 (DB_TYPE(mp) == M_DATA)) { 14828 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 14829 mblk_t *, mp); 14830 tcp_kssl_input(tcp, mp); 14831 } else if (!IPCL_IS_NONSTR(connp)) { 14832 /* Already handled non-STREAMS case. */ 14833 putnext(tcp->tcp_rq, mp); 14834 if (!canputnext(tcp->tcp_rq)) 14835 tcp->tcp_rwnd -= seg_len; 14836 } 14837 } else if ((tcp->tcp_kssl_ctx != NULL) && 14838 (DB_TYPE(mp) == M_DATA)) { 14839 /* Does this need SSL processing first? */ 14840 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 14841 tcp_kssl_input(tcp, mp); 14842 } else if (IPCL_IS_NONSTR(connp)) { 14843 /* Non-STREAMS socket */ 14844 boolean_t push = flags & (TH_PUSH|TH_FIN); 14845 int error; 14846 14847 if ((*connp->conn_upcalls->su_recv)( 14848 connp->conn_upper_handle, 14849 mp, seg_len, 0, &error, &push) <= 0) { 14850 /* 14851 * We should never be in middle of a 14852 * fallback, the squeue guarantees that. 14853 */ 14854 ASSERT(error != EOPNOTSUPP); 14855 if (error == ENOSPC) 14856 tcp->tcp_rwnd -= seg_len; 14857 } else if (push) { 14858 /* 14859 * PUSH bit set and sockfs is not 14860 * flow controlled 14861 */ 14862 flags |= tcp_rwnd_reopen(tcp); 14863 } 14864 } else if ((flags & (TH_PUSH|TH_FIN)) || 14865 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 14866 if (tcp->tcp_rcv_list != NULL) { 14867 /* 14868 * Enqueue the new segment first and then 14869 * call tcp_rcv_drain() to send all data 14870 * up. The other way to do this is to 14871 * send all queued data up and then call 14872 * putnext() to send the new segment up. 14873 * This way can remove the else part later 14874 * on. 14875 * 14876 * We don't do this to avoid one more call to 14877 * canputnext() as tcp_rcv_drain() needs to 14878 * call canputnext(). 14879 */ 14880 tcp_rcv_enqueue(tcp, mp, seg_len); 14881 flags |= tcp_rcv_drain(tcp); 14882 } else { 14883 putnext(tcp->tcp_rq, mp); 14884 if (!canputnext(tcp->tcp_rq)) 14885 tcp->tcp_rwnd -= seg_len; 14886 } 14887 } else { 14888 /* 14889 * Enqueue all packets when processing an mblk 14890 * from the co queue and also enqueue normal packets. 14891 */ 14892 tcp_rcv_enqueue(tcp, mp, seg_len); 14893 } 14894 /* 14895 * Make sure the timer is running if we have data waiting 14896 * for a push bit. This provides resiliency against 14897 * implementations that do not correctly generate push bits. 14898 */ 14899 if (!IPCL_IS_NONSTR(connp) && tcp->tcp_rcv_list != NULL && 14900 tcp->tcp_push_tid == 0) { 14901 /* 14902 * The connection may be closed at this point, so don't 14903 * do anything for a detached tcp. 14904 */ 14905 if (!TCP_IS_DETACHED(tcp)) 14906 tcp->tcp_push_tid = TCP_TIMER(tcp, 14907 tcp_push_timer, 14908 MSEC_TO_TICK( 14909 tcps->tcps_push_timer_interval)); 14910 } 14911 } 14912 14913 xmit_check: 14914 /* Is there anything left to do? */ 14915 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14916 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 14917 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 14918 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14919 goto done; 14920 14921 /* Any transmit work to do and a non-zero window? */ 14922 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 14923 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 14924 if (flags & TH_REXMIT_NEEDED) { 14925 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 14926 14927 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 14928 if (snd_size > mss) 14929 snd_size = mss; 14930 if (snd_size > tcp->tcp_swnd) 14931 snd_size = tcp->tcp_swnd; 14932 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 14933 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 14934 B_TRUE); 14935 14936 if (mp1 != NULL) { 14937 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14938 tcp->tcp_csuna = tcp->tcp_snxt; 14939 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 14940 UPDATE_MIB(&tcps->tcps_mib, 14941 tcpRetransBytes, snd_size); 14942 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14943 } 14944 } 14945 if (flags & TH_NEED_SACK_REXMIT) { 14946 tcp_sack_rxmit(tcp, &flags); 14947 } 14948 /* 14949 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 14950 * out new segment. Note that tcp_rexmit should not be 14951 * set, otherwise TH_LIMIT_XMIT should not be set. 14952 */ 14953 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 14954 if (!tcp->tcp_rexmit) { 14955 tcp_wput_data(tcp, NULL, B_FALSE); 14956 } else { 14957 tcp_ss_rexmit(tcp); 14958 } 14959 } 14960 /* 14961 * Adjust tcp_cwnd back to normal value after sending 14962 * new data segments. 14963 */ 14964 if (flags & TH_LIMIT_XMIT) { 14965 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 14966 /* 14967 * This will restart the timer. Restarting the 14968 * timer is used to avoid a timeout before the 14969 * limited transmitted segment's ACK gets back. 14970 */ 14971 if (tcp->tcp_xmit_head != NULL) 14972 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14973 } 14974 14975 /* Anything more to do? */ 14976 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 14977 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14978 goto done; 14979 } 14980 ack_check: 14981 if (flags & TH_SEND_URP_MARK) { 14982 ASSERT(tcp->tcp_urp_mark_mp); 14983 ASSERT(!IPCL_IS_NONSTR(connp)); 14984 /* 14985 * Send up any queued data and then send the mark message 14986 */ 14987 if (tcp->tcp_rcv_list != NULL) { 14988 flags |= tcp_rcv_drain(tcp); 14989 14990 } 14991 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14992 mp1 = tcp->tcp_urp_mark_mp; 14993 tcp->tcp_urp_mark_mp = NULL; 14994 putnext(tcp->tcp_rq, mp1); 14995 #ifdef DEBUG 14996 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14997 "tcp_rput: sending zero-length %s %s", 14998 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 14999 "MSGNOTMARKNEXT"), 15000 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15001 #endif /* DEBUG */ 15002 flags &= ~TH_SEND_URP_MARK; 15003 } 15004 if (flags & TH_ACK_NEEDED) { 15005 /* 15006 * Time to send an ack for some reason. 15007 */ 15008 mp1 = tcp_ack_mp(tcp); 15009 15010 if (mp1 != NULL) { 15011 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15012 BUMP_LOCAL(tcp->tcp_obsegs); 15013 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15014 } 15015 if (tcp->tcp_ack_tid != 0) { 15016 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15017 tcp->tcp_ack_tid = 0; 15018 } 15019 } 15020 if (flags & TH_ACK_TIMER_NEEDED) { 15021 /* 15022 * Arrange for deferred ACK or push wait timeout. 15023 * Start timer if it is not already running. 15024 */ 15025 if (tcp->tcp_ack_tid == 0) { 15026 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15027 MSEC_TO_TICK(tcp->tcp_localnet ? 15028 (clock_t)tcps->tcps_local_dack_interval : 15029 (clock_t)tcps->tcps_deferred_ack_interval)); 15030 } 15031 } 15032 if (flags & TH_ORDREL_NEEDED) { 15033 /* 15034 * Send up the ordrel_ind unless we are an eager guy. 15035 * In the eager case tcp_rsrv will do this when run 15036 * after tcp_accept is done. 15037 */ 15038 ASSERT(tcp->tcp_listener == NULL); 15039 15040 if (IPCL_IS_NONSTR(connp)) { 15041 ASSERT(tcp->tcp_ordrel_mp == NULL); 15042 tcp->tcp_ordrel_done = B_TRUE; 15043 (*connp->conn_upcalls->su_opctl) 15044 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15045 goto done; 15046 } 15047 15048 if (tcp->tcp_rcv_list != NULL) { 15049 /* 15050 * Push any mblk(s) enqueued from co processing. 15051 */ 15052 flags |= tcp_rcv_drain(tcp); 15053 } 15054 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15055 15056 mp1 = tcp->tcp_ordrel_mp; 15057 tcp->tcp_ordrel_mp = NULL; 15058 tcp->tcp_ordrel_done = B_TRUE; 15059 putnext(tcp->tcp_rq, mp1); 15060 } 15061 done: 15062 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15063 } 15064 15065 /* 15066 * This function does PAWS protection check. Returns B_TRUE if the 15067 * segment passes the PAWS test, else returns B_FALSE. 15068 */ 15069 boolean_t 15070 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15071 { 15072 uint8_t flags; 15073 int options; 15074 uint8_t *up; 15075 15076 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15077 /* 15078 * If timestamp option is aligned nicely, get values inline, 15079 * otherwise call general routine to parse. Only do that 15080 * if timestamp is the only option. 15081 */ 15082 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15083 TCPOPT_REAL_TS_LEN && 15084 OK_32PTR((up = ((uint8_t *)tcph) + 15085 TCP_MIN_HEADER_LENGTH)) && 15086 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15087 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15088 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15089 15090 options = TCP_OPT_TSTAMP_PRESENT; 15091 } else { 15092 if (tcp->tcp_snd_sack_ok) { 15093 tcpoptp->tcp = tcp; 15094 } else { 15095 tcpoptp->tcp = NULL; 15096 } 15097 options = tcp_parse_options(tcph, tcpoptp); 15098 } 15099 15100 if (options & TCP_OPT_TSTAMP_PRESENT) { 15101 /* 15102 * Do PAWS per RFC 1323 section 4.2. Accept RST 15103 * regardless of the timestamp, page 18 RFC 1323.bis. 15104 */ 15105 if ((flags & TH_RST) == 0 && 15106 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15107 tcp->tcp_ts_recent)) { 15108 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15109 PAWS_TIMEOUT)) { 15110 /* This segment is not acceptable. */ 15111 return (B_FALSE); 15112 } else { 15113 /* 15114 * Connection has been idle for 15115 * too long. Reset the timestamp 15116 * and assume the segment is valid. 15117 */ 15118 tcp->tcp_ts_recent = 15119 tcpoptp->tcp_opt_ts_val; 15120 } 15121 } 15122 } else { 15123 /* 15124 * If we don't get a timestamp on every packet, we 15125 * figure we can't really trust 'em, so we stop sending 15126 * and parsing them. 15127 */ 15128 tcp->tcp_snd_ts_ok = B_FALSE; 15129 15130 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15131 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15132 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15133 /* 15134 * Adjust the tcp_mss accordingly. We also need to 15135 * adjust tcp_cwnd here in accordance with the new mss. 15136 * But we avoid doing a slow start here so as to not 15137 * to lose on the transfer rate built up so far. 15138 */ 15139 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15140 if (tcp->tcp_snd_sack_ok) { 15141 ASSERT(tcp->tcp_sack_info != NULL); 15142 tcp->tcp_max_sack_blk = 4; 15143 } 15144 } 15145 return (B_TRUE); 15146 } 15147 15148 /* 15149 * Attach ancillary data to a received TCP segments for the 15150 * ancillary pieces requested by the application that are 15151 * different than they were in the previous data segment. 15152 * 15153 * Save the "current" values once memory allocation is ok so that 15154 * when memory allocation fails we can just wait for the next data segment. 15155 */ 15156 static mblk_t * 15157 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15158 { 15159 struct T_optdata_ind *todi; 15160 int optlen; 15161 uchar_t *optptr; 15162 struct T_opthdr *toh; 15163 uint_t addflag; /* Which pieces to add */ 15164 mblk_t *mp1; 15165 15166 optlen = 0; 15167 addflag = 0; 15168 /* If app asked for pktinfo and the index has changed ... */ 15169 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15170 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15171 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15172 optlen += sizeof (struct T_opthdr) + 15173 sizeof (struct in6_pktinfo); 15174 addflag |= TCP_IPV6_RECVPKTINFO; 15175 } 15176 /* If app asked for hoplimit and it has changed ... */ 15177 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15178 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15179 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15180 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15181 addflag |= TCP_IPV6_RECVHOPLIMIT; 15182 } 15183 /* If app asked for tclass and it has changed ... */ 15184 if ((ipp->ipp_fields & IPPF_TCLASS) && 15185 ipp->ipp_tclass != tcp->tcp_recvtclass && 15186 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15187 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15188 addflag |= TCP_IPV6_RECVTCLASS; 15189 } 15190 /* 15191 * If app asked for hopbyhop headers and it has changed ... 15192 * For security labels, note that (1) security labels can't change on 15193 * a connected socket at all, (2) we're connected to at most one peer, 15194 * (3) if anything changes, then it must be some other extra option. 15195 */ 15196 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15197 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15198 (ipp->ipp_fields & IPPF_HOPOPTS), 15199 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15200 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15201 tcp->tcp_label_len; 15202 addflag |= TCP_IPV6_RECVHOPOPTS; 15203 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15204 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15205 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15206 return (mp); 15207 } 15208 /* If app asked for dst headers before routing headers ... */ 15209 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15210 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15211 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15212 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15213 optlen += sizeof (struct T_opthdr) + 15214 ipp->ipp_rtdstoptslen; 15215 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15216 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15217 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15218 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15219 return (mp); 15220 } 15221 /* If app asked for routing headers and it has changed ... */ 15222 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15223 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15224 (ipp->ipp_fields & IPPF_RTHDR), 15225 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15226 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15227 addflag |= TCP_IPV6_RECVRTHDR; 15228 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15229 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15230 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15231 return (mp); 15232 } 15233 /* If app asked for dest headers and it has changed ... */ 15234 if ((tcp->tcp_ipv6_recvancillary & 15235 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15236 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15237 (ipp->ipp_fields & IPPF_DSTOPTS), 15238 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15239 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15240 addflag |= TCP_IPV6_RECVDSTOPTS; 15241 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15242 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15243 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15244 return (mp); 15245 } 15246 15247 if (optlen == 0) { 15248 /* Nothing to add */ 15249 return (mp); 15250 } 15251 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15252 if (mp1 == NULL) { 15253 /* 15254 * Defer sending ancillary data until the next TCP segment 15255 * arrives. 15256 */ 15257 return (mp); 15258 } 15259 mp1->b_cont = mp; 15260 mp = mp1; 15261 mp->b_wptr += sizeof (*todi) + optlen; 15262 mp->b_datap->db_type = M_PROTO; 15263 todi = (struct T_optdata_ind *)mp->b_rptr; 15264 todi->PRIM_type = T_OPTDATA_IND; 15265 todi->DATA_flag = 1; /* MORE data */ 15266 todi->OPT_length = optlen; 15267 todi->OPT_offset = sizeof (*todi); 15268 optptr = (uchar_t *)&todi[1]; 15269 /* 15270 * If app asked for pktinfo and the index has changed ... 15271 * Note that the local address never changes for the connection. 15272 */ 15273 if (addflag & TCP_IPV6_RECVPKTINFO) { 15274 struct in6_pktinfo *pkti; 15275 15276 toh = (struct T_opthdr *)optptr; 15277 toh->level = IPPROTO_IPV6; 15278 toh->name = IPV6_PKTINFO; 15279 toh->len = sizeof (*toh) + sizeof (*pkti); 15280 toh->status = 0; 15281 optptr += sizeof (*toh); 15282 pkti = (struct in6_pktinfo *)optptr; 15283 if (tcp->tcp_ipversion == IPV6_VERSION) 15284 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15285 else 15286 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15287 &pkti->ipi6_addr); 15288 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15289 optptr += sizeof (*pkti); 15290 ASSERT(OK_32PTR(optptr)); 15291 /* Save as "last" value */ 15292 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15293 } 15294 /* If app asked for hoplimit and it has changed ... */ 15295 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15296 toh = (struct T_opthdr *)optptr; 15297 toh->level = IPPROTO_IPV6; 15298 toh->name = IPV6_HOPLIMIT; 15299 toh->len = sizeof (*toh) + sizeof (uint_t); 15300 toh->status = 0; 15301 optptr += sizeof (*toh); 15302 *(uint_t *)optptr = ipp->ipp_hoplimit; 15303 optptr += sizeof (uint_t); 15304 ASSERT(OK_32PTR(optptr)); 15305 /* Save as "last" value */ 15306 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15307 } 15308 /* If app asked for tclass and it has changed ... */ 15309 if (addflag & TCP_IPV6_RECVTCLASS) { 15310 toh = (struct T_opthdr *)optptr; 15311 toh->level = IPPROTO_IPV6; 15312 toh->name = IPV6_TCLASS; 15313 toh->len = sizeof (*toh) + sizeof (uint_t); 15314 toh->status = 0; 15315 optptr += sizeof (*toh); 15316 *(uint_t *)optptr = ipp->ipp_tclass; 15317 optptr += sizeof (uint_t); 15318 ASSERT(OK_32PTR(optptr)); 15319 /* Save as "last" value */ 15320 tcp->tcp_recvtclass = ipp->ipp_tclass; 15321 } 15322 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15323 toh = (struct T_opthdr *)optptr; 15324 toh->level = IPPROTO_IPV6; 15325 toh->name = IPV6_HOPOPTS; 15326 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15327 tcp->tcp_label_len; 15328 toh->status = 0; 15329 optptr += sizeof (*toh); 15330 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15331 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15332 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15333 ASSERT(OK_32PTR(optptr)); 15334 /* Save as last value */ 15335 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15336 (ipp->ipp_fields & IPPF_HOPOPTS), 15337 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15338 } 15339 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15340 toh = (struct T_opthdr *)optptr; 15341 toh->level = IPPROTO_IPV6; 15342 toh->name = IPV6_RTHDRDSTOPTS; 15343 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15344 toh->status = 0; 15345 optptr += sizeof (*toh); 15346 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15347 optptr += ipp->ipp_rtdstoptslen; 15348 ASSERT(OK_32PTR(optptr)); 15349 /* Save as last value */ 15350 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15351 &tcp->tcp_rtdstoptslen, 15352 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15353 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15354 } 15355 if (addflag & TCP_IPV6_RECVRTHDR) { 15356 toh = (struct T_opthdr *)optptr; 15357 toh->level = IPPROTO_IPV6; 15358 toh->name = IPV6_RTHDR; 15359 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15360 toh->status = 0; 15361 optptr += sizeof (*toh); 15362 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15363 optptr += ipp->ipp_rthdrlen; 15364 ASSERT(OK_32PTR(optptr)); 15365 /* Save as last value */ 15366 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15367 (ipp->ipp_fields & IPPF_RTHDR), 15368 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15369 } 15370 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15371 toh = (struct T_opthdr *)optptr; 15372 toh->level = IPPROTO_IPV6; 15373 toh->name = IPV6_DSTOPTS; 15374 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15375 toh->status = 0; 15376 optptr += sizeof (*toh); 15377 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15378 optptr += ipp->ipp_dstoptslen; 15379 ASSERT(OK_32PTR(optptr)); 15380 /* Save as last value */ 15381 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15382 (ipp->ipp_fields & IPPF_DSTOPTS), 15383 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15384 } 15385 ASSERT(optptr == mp->b_wptr); 15386 return (mp); 15387 } 15388 15389 /* 15390 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15391 * messages. 15392 */ 15393 void 15394 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15395 { 15396 uchar_t *rptr = mp->b_rptr; 15397 queue_t *q = tcp->tcp_rq; 15398 struct T_error_ack *tea; 15399 15400 switch (mp->b_datap->db_type) { 15401 case M_PROTO: 15402 case M_PCPROTO: 15403 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15404 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15405 break; 15406 tea = (struct T_error_ack *)rptr; 15407 ASSERT(tea->PRIM_type != T_BIND_ACK); 15408 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15409 tea->ERROR_prim != T_BIND_REQ); 15410 switch (tea->PRIM_type) { 15411 case T_ERROR_ACK: 15412 if (tcp->tcp_debug) { 15413 (void) strlog(TCP_MOD_ID, 0, 1, 15414 SL_TRACE|SL_ERROR, 15415 "tcp_rput_other: case T_ERROR_ACK, " 15416 "ERROR_prim == %d", 15417 tea->ERROR_prim); 15418 } 15419 switch (tea->ERROR_prim) { 15420 case T_SVR4_OPTMGMT_REQ: 15421 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15422 /* T_OPTMGMT_REQ generated by TCP */ 15423 printf("T_SVR4_OPTMGMT_REQ failed " 15424 "%d/%d - dropped (cnt %d)\n", 15425 tea->TLI_error, tea->UNIX_error, 15426 tcp->tcp_drop_opt_ack_cnt); 15427 freemsg(mp); 15428 tcp->tcp_drop_opt_ack_cnt--; 15429 return; 15430 } 15431 break; 15432 } 15433 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15434 tcp->tcp_drop_opt_ack_cnt > 0) { 15435 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15436 "- dropped (cnt %d)\n", 15437 tea->TLI_error, tea->UNIX_error, 15438 tcp->tcp_drop_opt_ack_cnt); 15439 freemsg(mp); 15440 tcp->tcp_drop_opt_ack_cnt--; 15441 return; 15442 } 15443 break; 15444 case T_OPTMGMT_ACK: 15445 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15446 /* T_OPTMGMT_REQ generated by TCP */ 15447 freemsg(mp); 15448 tcp->tcp_drop_opt_ack_cnt--; 15449 return; 15450 } 15451 break; 15452 default: 15453 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15454 break; 15455 } 15456 break; 15457 case M_FLUSH: 15458 if (*rptr & FLUSHR) 15459 flushq(q, FLUSHDATA); 15460 break; 15461 default: 15462 /* M_CTL will be directly sent to tcp_icmp_error() */ 15463 ASSERT(DB_TYPE(mp) != M_CTL); 15464 break; 15465 } 15466 /* 15467 * Make sure we set this bit before sending the ACK for 15468 * bind. Otherwise accept could possibly run and free 15469 * this tcp struct. 15470 */ 15471 ASSERT(q != NULL); 15472 putnext(q, mp); 15473 } 15474 15475 /* ARGSUSED */ 15476 static void 15477 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15478 { 15479 conn_t *connp = (conn_t *)arg; 15480 tcp_t *tcp = connp->conn_tcp; 15481 queue_t *q = tcp->tcp_rq; 15482 uint_t thwin; 15483 tcp_stack_t *tcps = tcp->tcp_tcps; 15484 15485 ASSERT(!IPCL_IS_NONSTR(connp)); 15486 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15487 tcp->tcp_rsrv_mp = mp; 15488 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15489 15490 TCP_STAT(tcps, tcp_rsrv_calls); 15491 15492 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15493 return; 15494 } 15495 15496 if (tcp->tcp_fused) { 15497 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15498 15499 ASSERT(tcp->tcp_fused); 15500 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15501 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15502 ASSERT(!TCP_IS_DETACHED(tcp)); 15503 ASSERT(tcp->tcp_connp->conn_sqp == 15504 peer_tcp->tcp_connp->conn_sqp); 15505 15506 /* 15507 * Normally we would not get backenabled in synchronous 15508 * streams mode, but in case this happens, we need to plug 15509 * synchronous streams during our drain to prevent a race 15510 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15511 */ 15512 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15513 if (tcp->tcp_rcv_list != NULL) 15514 (void) tcp_rcv_drain(tcp); 15515 15516 if (peer_tcp > tcp) { 15517 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15518 mutex_enter(&tcp->tcp_non_sq_lock); 15519 } else { 15520 mutex_enter(&tcp->tcp_non_sq_lock); 15521 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15522 } 15523 15524 if (peer_tcp->tcp_flow_stopped && 15525 (TCP_UNSENT_BYTES(peer_tcp) <= 15526 peer_tcp->tcp_xmit_lowater)) { 15527 tcp_clrqfull(peer_tcp); 15528 } 15529 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15530 mutex_exit(&tcp->tcp_non_sq_lock); 15531 15532 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15533 TCP_STAT(tcps, tcp_fusion_backenabled); 15534 return; 15535 } 15536 15537 if (canputnext(q)) { 15538 /* Not flow-controlled, open rwnd */ 15539 tcp->tcp_rwnd = q->q_hiwat; 15540 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15541 << tcp->tcp_rcv_ws; 15542 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15543 /* 15544 * Send back a window update immediately if TCP is above 15545 * ESTABLISHED state and the increase of the rcv window 15546 * that the other side knows is at least 1 MSS after flow 15547 * control is lifted. 15548 */ 15549 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15550 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15551 tcp_xmit_ctl(NULL, tcp, 15552 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15553 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15554 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 15555 } 15556 } 15557 } 15558 15559 /* 15560 * The read side service routine is called mostly when we get back-enabled as a 15561 * result of flow control relief. Since we don't actually queue anything in 15562 * TCP, we have no data to send out of here. What we do is clear the receive 15563 * window, and send out a window update. 15564 */ 15565 static void 15566 tcp_rsrv(queue_t *q) 15567 { 15568 conn_t *connp = Q_TO_CONN(q); 15569 tcp_t *tcp = connp->conn_tcp; 15570 mblk_t *mp; 15571 tcp_stack_t *tcps = tcp->tcp_tcps; 15572 15573 /* No code does a putq on the read side */ 15574 ASSERT(q->q_first == NULL); 15575 15576 /* Nothing to do for the default queue */ 15577 if (q == tcps->tcps_g_q) { 15578 return; 15579 } 15580 15581 /* 15582 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 15583 * been run. So just return. 15584 */ 15585 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15586 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 15587 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15588 return; 15589 } 15590 tcp->tcp_rsrv_mp = NULL; 15591 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15592 15593 CONN_INC_REF(connp); 15594 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15595 SQ_PROCESS, SQTAG_TCP_RSRV); 15596 } 15597 15598 /* 15599 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15600 * We do not allow the receive window to shrink. After setting rwnd, 15601 * set the flow control hiwat of the stream. 15602 * 15603 * This function is called in 2 cases: 15604 * 15605 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15606 * connection (passive open) and in tcp_rput_data() for active connect. 15607 * This is called after tcp_mss_set() when the desired MSS value is known. 15608 * This makes sure that our window size is a mutiple of the other side's 15609 * MSS. 15610 * 2) Handling SO_RCVBUF option. 15611 * 15612 * It is ASSUMED that the requested size is a multiple of the current MSS. 15613 * 15614 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15615 * user requests so. 15616 */ 15617 static int 15618 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15619 { 15620 uint32_t mss = tcp->tcp_mss; 15621 uint32_t old_max_rwnd; 15622 uint32_t max_transmittable_rwnd; 15623 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15624 tcp_stack_t *tcps = tcp->tcp_tcps; 15625 15626 if (tcp->tcp_fused) { 15627 size_t sth_hiwat; 15628 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15629 15630 ASSERT(peer_tcp != NULL); 15631 /* 15632 * Record the stream head's high water mark for 15633 * this endpoint; this is used for flow-control 15634 * purposes in tcp_fuse_output(). 15635 */ 15636 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15637 if (!tcp_detached) { 15638 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15639 sth_hiwat); 15640 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 15641 conn_t *connp = tcp->tcp_connp; 15642 struct sock_proto_props sopp; 15643 15644 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 15645 sopp.sopp_rcvthresh = sth_hiwat >> 3; 15646 15647 (*connp->conn_upcalls->su_set_proto_props) 15648 (connp->conn_upper_handle, &sopp); 15649 } 15650 } 15651 15652 /* 15653 * In the fusion case, the maxpsz stream head value of 15654 * our peer is set according to its send buffer size 15655 * and our receive buffer size; since the latter may 15656 * have changed we need to update the peer's maxpsz. 15657 */ 15658 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15659 return (rwnd); 15660 } 15661 15662 if (tcp_detached) { 15663 old_max_rwnd = tcp->tcp_rwnd; 15664 } else { 15665 old_max_rwnd = tcp->tcp_recv_hiwater; 15666 } 15667 15668 /* 15669 * Insist on a receive window that is at least 15670 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15671 * funny TCP interactions of Nagle algorithm, SWS avoidance 15672 * and delayed acknowledgement. 15673 */ 15674 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 15675 15676 /* 15677 * If window size info has already been exchanged, TCP should not 15678 * shrink the window. Shrinking window is doable if done carefully. 15679 * We may add that support later. But so far there is not a real 15680 * need to do that. 15681 */ 15682 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15683 /* MSS may have changed, do a round up again. */ 15684 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15685 } 15686 15687 /* 15688 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15689 * can be applied even before the window scale option is decided. 15690 */ 15691 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15692 if (rwnd > max_transmittable_rwnd) { 15693 rwnd = max_transmittable_rwnd - 15694 (max_transmittable_rwnd % mss); 15695 if (rwnd < mss) 15696 rwnd = max_transmittable_rwnd; 15697 /* 15698 * If we're over the limit we may have to back down tcp_rwnd. 15699 * The increment below won't work for us. So we set all three 15700 * here and the increment below will have no effect. 15701 */ 15702 tcp->tcp_rwnd = old_max_rwnd = rwnd; 15703 } 15704 if (tcp->tcp_localnet) { 15705 tcp->tcp_rack_abs_max = 15706 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 15707 } else { 15708 /* 15709 * For a remote host on a different subnet (through a router), 15710 * we ack every other packet to be conforming to RFC1122. 15711 * tcp_deferred_acks_max is default to 2. 15712 */ 15713 tcp->tcp_rack_abs_max = 15714 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 15715 } 15716 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 15717 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15718 else 15719 tcp->tcp_rack_cur_max = 0; 15720 /* 15721 * Increment the current rwnd by the amount the maximum grew (we 15722 * can not overwrite it since we might be in the middle of a 15723 * connection.) 15724 */ 15725 tcp->tcp_rwnd += rwnd - old_max_rwnd; 15726 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 15727 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 15728 tcp->tcp_cwnd_max = rwnd; 15729 15730 if (tcp_detached) 15731 return (rwnd); 15732 /* 15733 * We set the maximum receive window into rq->q_hiwat if it is 15734 * a STREAMS socket. 15735 * This is not actually used for flow control. 15736 */ 15737 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 15738 tcp->tcp_rq->q_hiwat = rwnd; 15739 tcp->tcp_recv_hiwater = rwnd; 15740 /* 15741 * Set the STREAM head high water mark. This doesn't have to be 15742 * here, since we are simply using default values, but we would 15743 * prefer to choose these values algorithmically, with a likely 15744 * relationship to rwnd. 15745 */ 15746 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15747 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 15748 return (rwnd); 15749 } 15750 15751 /* 15752 * Return SNMP stuff in buffer in mpdata. 15753 */ 15754 mblk_t * 15755 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 15756 { 15757 mblk_t *mpdata; 15758 mblk_t *mp_conn_ctl = NULL; 15759 mblk_t *mp_conn_tail; 15760 mblk_t *mp_attr_ctl = NULL; 15761 mblk_t *mp_attr_tail; 15762 mblk_t *mp6_conn_ctl = NULL; 15763 mblk_t *mp6_conn_tail; 15764 mblk_t *mp6_attr_ctl = NULL; 15765 mblk_t *mp6_attr_tail; 15766 struct opthdr *optp; 15767 mib2_tcpConnEntry_t tce; 15768 mib2_tcp6ConnEntry_t tce6; 15769 mib2_transportMLPEntry_t mlp; 15770 connf_t *connfp; 15771 int i; 15772 boolean_t ispriv; 15773 zoneid_t zoneid; 15774 int v4_conn_idx; 15775 int v6_conn_idx; 15776 conn_t *connp = Q_TO_CONN(q); 15777 tcp_stack_t *tcps; 15778 ip_stack_t *ipst; 15779 mblk_t *mp2ctl; 15780 15781 /* 15782 * make a copy of the original message 15783 */ 15784 mp2ctl = copymsg(mpctl); 15785 15786 if (mpctl == NULL || 15787 (mpdata = mpctl->b_cont) == NULL || 15788 (mp_conn_ctl = copymsg(mpctl)) == NULL || 15789 (mp_attr_ctl = copymsg(mpctl)) == NULL || 15790 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 15791 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 15792 freemsg(mp_conn_ctl); 15793 freemsg(mp_attr_ctl); 15794 freemsg(mp6_conn_ctl); 15795 freemsg(mp6_attr_ctl); 15796 freemsg(mpctl); 15797 freemsg(mp2ctl); 15798 return (NULL); 15799 } 15800 15801 ipst = connp->conn_netstack->netstack_ip; 15802 tcps = connp->conn_netstack->netstack_tcp; 15803 15804 /* build table of connections -- need count in fixed part */ 15805 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 15806 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 15807 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 15808 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 15809 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 15810 15811 ispriv = 15812 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 15813 zoneid = Q_TO_CONN(q)->conn_zoneid; 15814 15815 v4_conn_idx = v6_conn_idx = 0; 15816 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 15817 15818 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 15819 ipst = tcps->tcps_netstack->netstack_ip; 15820 15821 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 15822 15823 connp = NULL; 15824 15825 while ((connp = 15826 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 15827 tcp_t *tcp; 15828 boolean_t needattr; 15829 15830 if (connp->conn_zoneid != zoneid) 15831 continue; /* not in this zone */ 15832 15833 tcp = connp->conn_tcp; 15834 UPDATE_MIB(&tcps->tcps_mib, 15835 tcpHCInSegs, tcp->tcp_ibsegs); 15836 tcp->tcp_ibsegs = 0; 15837 UPDATE_MIB(&tcps->tcps_mib, 15838 tcpHCOutSegs, tcp->tcp_obsegs); 15839 tcp->tcp_obsegs = 0; 15840 15841 tce6.tcp6ConnState = tce.tcpConnState = 15842 tcp_snmp_state(tcp); 15843 if (tce.tcpConnState == MIB2_TCP_established || 15844 tce.tcpConnState == MIB2_TCP_closeWait) 15845 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 15846 15847 needattr = B_FALSE; 15848 bzero(&mlp, sizeof (mlp)); 15849 if (connp->conn_mlp_type != mlptSingle) { 15850 if (connp->conn_mlp_type == mlptShared || 15851 connp->conn_mlp_type == mlptBoth) 15852 mlp.tme_flags |= MIB2_TMEF_SHARED; 15853 if (connp->conn_mlp_type == mlptPrivate || 15854 connp->conn_mlp_type == mlptBoth) 15855 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 15856 needattr = B_TRUE; 15857 } 15858 if (connp->conn_peercred != NULL) { 15859 ts_label_t *tsl; 15860 15861 tsl = crgetlabel(connp->conn_peercred); 15862 mlp.tme_doi = label2doi(tsl); 15863 mlp.tme_label = *label2bslabel(tsl); 15864 needattr = B_TRUE; 15865 } 15866 15867 /* Create a message to report on IPv6 entries */ 15868 if (tcp->tcp_ipversion == IPV6_VERSION) { 15869 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 15870 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 15871 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 15872 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 15873 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 15874 /* Don't want just anybody seeing these... */ 15875 if (ispriv) { 15876 tce6.tcp6ConnEntryInfo.ce_snxt = 15877 tcp->tcp_snxt; 15878 tce6.tcp6ConnEntryInfo.ce_suna = 15879 tcp->tcp_suna; 15880 tce6.tcp6ConnEntryInfo.ce_rnxt = 15881 tcp->tcp_rnxt; 15882 tce6.tcp6ConnEntryInfo.ce_rack = 15883 tcp->tcp_rack; 15884 } else { 15885 /* 15886 * Netstat, unfortunately, uses this to 15887 * get send/receive queue sizes. How to fix? 15888 * Why not compute the difference only? 15889 */ 15890 tce6.tcp6ConnEntryInfo.ce_snxt = 15891 tcp->tcp_snxt - tcp->tcp_suna; 15892 tce6.tcp6ConnEntryInfo.ce_suna = 0; 15893 tce6.tcp6ConnEntryInfo.ce_rnxt = 15894 tcp->tcp_rnxt - tcp->tcp_rack; 15895 tce6.tcp6ConnEntryInfo.ce_rack = 0; 15896 } 15897 15898 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15899 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15900 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 15901 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 15902 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 15903 15904 tce6.tcp6ConnCreationProcess = 15905 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 15906 tcp->tcp_cpid; 15907 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 15908 15909 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 15910 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 15911 15912 mlp.tme_connidx = v6_conn_idx++; 15913 if (needattr) 15914 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 15915 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 15916 } 15917 /* 15918 * Create an IPv4 table entry for IPv4 entries and also 15919 * for IPv6 entries which are bound to in6addr_any 15920 * but don't have IPV6_V6ONLY set. 15921 * (i.e. anything an IPv4 peer could connect to) 15922 */ 15923 if (tcp->tcp_ipversion == IPV4_VERSION || 15924 (tcp->tcp_state <= TCPS_LISTEN && 15925 !tcp->tcp_connp->conn_ipv6_v6only && 15926 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 15927 if (tcp->tcp_ipversion == IPV6_VERSION) { 15928 tce.tcpConnRemAddress = INADDR_ANY; 15929 tce.tcpConnLocalAddress = INADDR_ANY; 15930 } else { 15931 tce.tcpConnRemAddress = 15932 tcp->tcp_remote; 15933 tce.tcpConnLocalAddress = 15934 tcp->tcp_ip_src; 15935 } 15936 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 15937 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 15938 /* Don't want just anybody seeing these... */ 15939 if (ispriv) { 15940 tce.tcpConnEntryInfo.ce_snxt = 15941 tcp->tcp_snxt; 15942 tce.tcpConnEntryInfo.ce_suna = 15943 tcp->tcp_suna; 15944 tce.tcpConnEntryInfo.ce_rnxt = 15945 tcp->tcp_rnxt; 15946 tce.tcpConnEntryInfo.ce_rack = 15947 tcp->tcp_rack; 15948 } else { 15949 /* 15950 * Netstat, unfortunately, uses this to 15951 * get send/receive queue sizes. How 15952 * to fix? 15953 * Why not compute the difference only? 15954 */ 15955 tce.tcpConnEntryInfo.ce_snxt = 15956 tcp->tcp_snxt - tcp->tcp_suna; 15957 tce.tcpConnEntryInfo.ce_suna = 0; 15958 tce.tcpConnEntryInfo.ce_rnxt = 15959 tcp->tcp_rnxt - tcp->tcp_rack; 15960 tce.tcpConnEntryInfo.ce_rack = 0; 15961 } 15962 15963 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15964 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15965 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 15966 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 15967 tce.tcpConnEntryInfo.ce_state = 15968 tcp->tcp_state; 15969 15970 tce.tcpConnCreationProcess = 15971 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 15972 tcp->tcp_cpid; 15973 tce.tcpConnCreationTime = tcp->tcp_open_time; 15974 15975 (void) snmp_append_data2(mp_conn_ctl->b_cont, 15976 &mp_conn_tail, (char *)&tce, sizeof (tce)); 15977 15978 mlp.tme_connidx = v4_conn_idx++; 15979 if (needattr) 15980 (void) snmp_append_data2( 15981 mp_attr_ctl->b_cont, 15982 &mp_attr_tail, (char *)&mlp, 15983 sizeof (mlp)); 15984 } 15985 } 15986 } 15987 15988 /* fixed length structure for IPv4 and IPv6 counters */ 15989 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 15990 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 15991 sizeof (mib2_tcp6ConnEntry_t)); 15992 /* synchronize 32- and 64-bit counters */ 15993 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 15994 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 15995 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 15996 optp->level = MIB2_TCP; 15997 optp->name = 0; 15998 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 15999 sizeof (tcps->tcps_mib)); 16000 optp->len = msgdsize(mpdata); 16001 qreply(q, mpctl); 16002 16003 /* table of connections... */ 16004 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16005 sizeof (struct T_optmgmt_ack)]; 16006 optp->level = MIB2_TCP; 16007 optp->name = MIB2_TCP_CONN; 16008 optp->len = msgdsize(mp_conn_ctl->b_cont); 16009 qreply(q, mp_conn_ctl); 16010 16011 /* table of MLP attributes... */ 16012 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16013 sizeof (struct T_optmgmt_ack)]; 16014 optp->level = MIB2_TCP; 16015 optp->name = EXPER_XPORT_MLP; 16016 optp->len = msgdsize(mp_attr_ctl->b_cont); 16017 if (optp->len == 0) 16018 freemsg(mp_attr_ctl); 16019 else 16020 qreply(q, mp_attr_ctl); 16021 16022 /* table of IPv6 connections... */ 16023 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16024 sizeof (struct T_optmgmt_ack)]; 16025 optp->level = MIB2_TCP6; 16026 optp->name = MIB2_TCP6_CONN; 16027 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16028 qreply(q, mp6_conn_ctl); 16029 16030 /* table of IPv6 MLP attributes... */ 16031 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16032 sizeof (struct T_optmgmt_ack)]; 16033 optp->level = MIB2_TCP6; 16034 optp->name = EXPER_XPORT_MLP; 16035 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16036 if (optp->len == 0) 16037 freemsg(mp6_attr_ctl); 16038 else 16039 qreply(q, mp6_attr_ctl); 16040 return (mp2ctl); 16041 } 16042 16043 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16044 /* ARGSUSED */ 16045 int 16046 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16047 { 16048 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16049 16050 switch (level) { 16051 case MIB2_TCP: 16052 switch (name) { 16053 case 13: 16054 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16055 return (0); 16056 /* TODO: delete entry defined by tce */ 16057 return (1); 16058 default: 16059 return (0); 16060 } 16061 default: 16062 return (1); 16063 } 16064 } 16065 16066 /* Translate TCP state to MIB2 TCP state. */ 16067 static int 16068 tcp_snmp_state(tcp_t *tcp) 16069 { 16070 if (tcp == NULL) 16071 return (0); 16072 16073 switch (tcp->tcp_state) { 16074 case TCPS_CLOSED: 16075 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16076 case TCPS_BOUND: 16077 return (MIB2_TCP_closed); 16078 case TCPS_LISTEN: 16079 return (MIB2_TCP_listen); 16080 case TCPS_SYN_SENT: 16081 return (MIB2_TCP_synSent); 16082 case TCPS_SYN_RCVD: 16083 return (MIB2_TCP_synReceived); 16084 case TCPS_ESTABLISHED: 16085 return (MIB2_TCP_established); 16086 case TCPS_CLOSE_WAIT: 16087 return (MIB2_TCP_closeWait); 16088 case TCPS_FIN_WAIT_1: 16089 return (MIB2_TCP_finWait1); 16090 case TCPS_CLOSING: 16091 return (MIB2_TCP_closing); 16092 case TCPS_LAST_ACK: 16093 return (MIB2_TCP_lastAck); 16094 case TCPS_FIN_WAIT_2: 16095 return (MIB2_TCP_finWait2); 16096 case TCPS_TIME_WAIT: 16097 return (MIB2_TCP_timeWait); 16098 default: 16099 return (0); 16100 } 16101 } 16102 16103 /* 16104 * tcp_timer is the timer service routine. It handles the retransmission, 16105 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16106 * from the state of the tcp instance what kind of action needs to be done 16107 * at the time it is called. 16108 */ 16109 static void 16110 tcp_timer(void *arg) 16111 { 16112 mblk_t *mp; 16113 clock_t first_threshold; 16114 clock_t second_threshold; 16115 clock_t ms; 16116 uint32_t mss; 16117 conn_t *connp = (conn_t *)arg; 16118 tcp_t *tcp = connp->conn_tcp; 16119 tcp_stack_t *tcps = tcp->tcp_tcps; 16120 16121 tcp->tcp_timer_tid = 0; 16122 16123 if (tcp->tcp_fused) 16124 return; 16125 16126 first_threshold = tcp->tcp_first_timer_threshold; 16127 second_threshold = tcp->tcp_second_timer_threshold; 16128 switch (tcp->tcp_state) { 16129 case TCPS_IDLE: 16130 case TCPS_BOUND: 16131 case TCPS_LISTEN: 16132 return; 16133 case TCPS_SYN_RCVD: { 16134 tcp_t *listener = tcp->tcp_listener; 16135 16136 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16137 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16138 /* it's our first timeout */ 16139 tcp->tcp_syn_rcvd_timeout = 1; 16140 mutex_enter(&listener->tcp_eager_lock); 16141 listener->tcp_syn_rcvd_timeout++; 16142 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16143 /* 16144 * Make this eager available for drop if we 16145 * need to drop one to accomodate a new 16146 * incoming SYN request. 16147 */ 16148 MAKE_DROPPABLE(listener, tcp); 16149 } 16150 if (!listener->tcp_syn_defense && 16151 (listener->tcp_syn_rcvd_timeout > 16152 (tcps->tcps_conn_req_max_q0 >> 2)) && 16153 (tcps->tcps_conn_req_max_q0 > 200)) { 16154 /* We may be under attack. Put on a defense. */ 16155 listener->tcp_syn_defense = B_TRUE; 16156 cmn_err(CE_WARN, "High TCP connect timeout " 16157 "rate! System (port %d) may be under a " 16158 "SYN flood attack!", 16159 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16160 16161 listener->tcp_ip_addr_cache = kmem_zalloc( 16162 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16163 KM_NOSLEEP); 16164 } 16165 mutex_exit(&listener->tcp_eager_lock); 16166 } else if (listener != NULL) { 16167 mutex_enter(&listener->tcp_eager_lock); 16168 tcp->tcp_syn_rcvd_timeout++; 16169 if (tcp->tcp_syn_rcvd_timeout > 1 && 16170 !tcp->tcp_closemp_used) { 16171 /* 16172 * This is our second timeout. Put the tcp in 16173 * the list of droppable eagers to allow it to 16174 * be dropped, if needed. We don't check 16175 * whether tcp_dontdrop is set or not to 16176 * protect ourselve from a SYN attack where a 16177 * remote host can spoof itself as one of the 16178 * good IP source and continue to hold 16179 * resources too long. 16180 */ 16181 MAKE_DROPPABLE(listener, tcp); 16182 } 16183 mutex_exit(&listener->tcp_eager_lock); 16184 } 16185 } 16186 /* FALLTHRU */ 16187 case TCPS_SYN_SENT: 16188 first_threshold = tcp->tcp_first_ctimer_threshold; 16189 second_threshold = tcp->tcp_second_ctimer_threshold; 16190 break; 16191 case TCPS_ESTABLISHED: 16192 case TCPS_FIN_WAIT_1: 16193 case TCPS_CLOSING: 16194 case TCPS_CLOSE_WAIT: 16195 case TCPS_LAST_ACK: 16196 /* If we have data to rexmit */ 16197 if (tcp->tcp_suna != tcp->tcp_snxt) { 16198 clock_t time_to_wait; 16199 16200 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 16201 if (!tcp->tcp_xmit_head) 16202 break; 16203 time_to_wait = lbolt - 16204 (clock_t)tcp->tcp_xmit_head->b_prev; 16205 time_to_wait = tcp->tcp_rto - 16206 TICK_TO_MSEC(time_to_wait); 16207 /* 16208 * If the timer fires too early, 1 clock tick earlier, 16209 * restart the timer. 16210 */ 16211 if (time_to_wait > msec_per_tick) { 16212 TCP_STAT(tcps, tcp_timer_fire_early); 16213 TCP_TIMER_RESTART(tcp, time_to_wait); 16214 return; 16215 } 16216 /* 16217 * When we probe zero windows, we force the swnd open. 16218 * If our peer acks with a closed window swnd will be 16219 * set to zero by tcp_rput(). As long as we are 16220 * receiving acks tcp_rput will 16221 * reset 'tcp_ms_we_have_waited' so as not to trip the 16222 * first and second interval actions. NOTE: the timer 16223 * interval is allowed to continue its exponential 16224 * backoff. 16225 */ 16226 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16227 if (tcp->tcp_debug) { 16228 (void) strlog(TCP_MOD_ID, 0, 1, 16229 SL_TRACE, "tcp_timer: zero win"); 16230 } 16231 } else { 16232 /* 16233 * After retransmission, we need to do 16234 * slow start. Set the ssthresh to one 16235 * half of current effective window and 16236 * cwnd to one MSS. Also reset 16237 * tcp_cwnd_cnt. 16238 * 16239 * Note that if tcp_ssthresh is reduced because 16240 * of ECN, do not reduce it again unless it is 16241 * already one window of data away (tcp_cwr 16242 * should then be cleared) or this is a 16243 * timeout for a retransmitted segment. 16244 */ 16245 uint32_t npkt; 16246 16247 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16248 npkt = ((tcp->tcp_timer_backoff ? 16249 tcp->tcp_cwnd_ssthresh : 16250 tcp->tcp_snxt - 16251 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16252 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16253 tcp->tcp_mss; 16254 } 16255 tcp->tcp_cwnd = tcp->tcp_mss; 16256 tcp->tcp_cwnd_cnt = 0; 16257 if (tcp->tcp_ecn_ok) { 16258 tcp->tcp_cwr = B_TRUE; 16259 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16260 tcp->tcp_ecn_cwr_sent = B_FALSE; 16261 } 16262 } 16263 break; 16264 } 16265 /* 16266 * We have something to send yet we cannot send. The 16267 * reason can be: 16268 * 16269 * 1. Zero send window: we need to do zero window probe. 16270 * 2. Zero cwnd: because of ECN, we need to "clock out 16271 * segments. 16272 * 3. SWS avoidance: receiver may have shrunk window, 16273 * reset our knowledge. 16274 * 16275 * Note that condition 2 can happen with either 1 or 16276 * 3. But 1 and 3 are exclusive. 16277 */ 16278 if (tcp->tcp_unsent != 0) { 16279 if (tcp->tcp_cwnd == 0) { 16280 /* 16281 * Set tcp_cwnd to 1 MSS so that a 16282 * new segment can be sent out. We 16283 * are "clocking out" new data when 16284 * the network is really congested. 16285 */ 16286 ASSERT(tcp->tcp_ecn_ok); 16287 tcp->tcp_cwnd = tcp->tcp_mss; 16288 } 16289 if (tcp->tcp_swnd == 0) { 16290 /* Extend window for zero window probe */ 16291 tcp->tcp_swnd++; 16292 tcp->tcp_zero_win_probe = B_TRUE; 16293 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 16294 } else { 16295 /* 16296 * Handle timeout from sender SWS avoidance. 16297 * Reset our knowledge of the max send window 16298 * since the receiver might have reduced its 16299 * receive buffer. Avoid setting tcp_max_swnd 16300 * to one since that will essentially disable 16301 * the SWS checks. 16302 * 16303 * Note that since we don't have a SWS 16304 * state variable, if the timeout is set 16305 * for ECN but not for SWS, this 16306 * code will also be executed. This is 16307 * fine as tcp_max_swnd is updated 16308 * constantly and it will not affect 16309 * anything. 16310 */ 16311 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16312 } 16313 tcp_wput_data(tcp, NULL, B_FALSE); 16314 return; 16315 } 16316 /* Is there a FIN that needs to be to re retransmitted? */ 16317 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16318 !tcp->tcp_fin_acked) 16319 break; 16320 /* Nothing to do, return without restarting timer. */ 16321 TCP_STAT(tcps, tcp_timer_fire_miss); 16322 return; 16323 case TCPS_FIN_WAIT_2: 16324 /* 16325 * User closed the TCP endpoint and peer ACK'ed our FIN. 16326 * We waited some time for for peer's FIN, but it hasn't 16327 * arrived. We flush the connection now to avoid 16328 * case where the peer has rebooted. 16329 */ 16330 if (TCP_IS_DETACHED(tcp)) { 16331 (void) tcp_clean_death(tcp, 0, 23); 16332 } else { 16333 TCP_TIMER_RESTART(tcp, 16334 tcps->tcps_fin_wait_2_flush_interval); 16335 } 16336 return; 16337 case TCPS_TIME_WAIT: 16338 (void) tcp_clean_death(tcp, 0, 24); 16339 return; 16340 default: 16341 if (tcp->tcp_debug) { 16342 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16343 "tcp_timer: strange state (%d) %s", 16344 tcp->tcp_state, tcp_display(tcp, NULL, 16345 DISP_PORT_ONLY)); 16346 } 16347 return; 16348 } 16349 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16350 /* 16351 * For zero window probe, we need to send indefinitely, 16352 * unless we have not heard from the other side for some 16353 * time... 16354 */ 16355 if ((tcp->tcp_zero_win_probe == 0) || 16356 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16357 second_threshold)) { 16358 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 16359 /* 16360 * If TCP is in SYN_RCVD state, send back a 16361 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16362 * should be zero in TCPS_SYN_RCVD state. 16363 */ 16364 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16365 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16366 "in SYN_RCVD", 16367 tcp, tcp->tcp_snxt, 16368 tcp->tcp_rnxt, TH_RST | TH_ACK); 16369 } 16370 (void) tcp_clean_death(tcp, 16371 tcp->tcp_client_errno ? 16372 tcp->tcp_client_errno : ETIMEDOUT, 25); 16373 return; 16374 } else { 16375 /* 16376 * Set tcp_ms_we_have_waited to second_threshold 16377 * so that in next timeout, we will do the above 16378 * check (lbolt - tcp_last_recv_time). This is 16379 * also to avoid overflow. 16380 * 16381 * We don't need to decrement tcp_timer_backoff 16382 * to avoid overflow because it will be decremented 16383 * later if new timeout value is greater than 16384 * tcp_rexmit_interval_max. In the case when 16385 * tcp_rexmit_interval_max is greater than 16386 * second_threshold, it means that we will wait 16387 * longer than second_threshold to send the next 16388 * window probe. 16389 */ 16390 tcp->tcp_ms_we_have_waited = second_threshold; 16391 } 16392 } else if (ms > first_threshold) { 16393 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16394 tcp->tcp_xmit_head != NULL) { 16395 tcp->tcp_xmit_head = 16396 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16397 } 16398 /* 16399 * We have been retransmitting for too long... The RTT 16400 * we calculated is probably incorrect. Reinitialize it. 16401 * Need to compensate for 0 tcp_rtt_sa. Reset 16402 * tcp_rtt_update so that we won't accidentally cache a 16403 * bad value. But only do this if this is not a zero 16404 * window probe. 16405 */ 16406 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 16407 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 16408 (tcp->tcp_rtt_sa >> 5); 16409 tcp->tcp_rtt_sa = 0; 16410 tcp_ip_notify(tcp); 16411 tcp->tcp_rtt_update = 0; 16412 } 16413 } 16414 tcp->tcp_timer_backoff++; 16415 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 16416 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 16417 tcps->tcps_rexmit_interval_min) { 16418 /* 16419 * This means the original RTO is tcp_rexmit_interval_min. 16420 * So we will use tcp_rexmit_interval_min as the RTO value 16421 * and do the backoff. 16422 */ 16423 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 16424 } else { 16425 ms <<= tcp->tcp_timer_backoff; 16426 } 16427 if (ms > tcps->tcps_rexmit_interval_max) { 16428 ms = tcps->tcps_rexmit_interval_max; 16429 /* 16430 * ms is at max, decrement tcp_timer_backoff to avoid 16431 * overflow. 16432 */ 16433 tcp->tcp_timer_backoff--; 16434 } 16435 tcp->tcp_ms_we_have_waited += ms; 16436 if (tcp->tcp_zero_win_probe == 0) { 16437 tcp->tcp_rto = ms; 16438 } 16439 TCP_TIMER_RESTART(tcp, ms); 16440 /* 16441 * This is after a timeout and tcp_rto is backed off. Set 16442 * tcp_set_timer to 1 so that next time RTO is updated, we will 16443 * restart the timer with a correct value. 16444 */ 16445 tcp->tcp_set_timer = 1; 16446 mss = tcp->tcp_snxt - tcp->tcp_suna; 16447 if (mss > tcp->tcp_mss) 16448 mss = tcp->tcp_mss; 16449 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 16450 mss = tcp->tcp_swnd; 16451 16452 if ((mp = tcp->tcp_xmit_head) != NULL) 16453 mp->b_prev = (mblk_t *)lbolt; 16454 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 16455 B_TRUE); 16456 16457 /* 16458 * When slow start after retransmission begins, start with 16459 * this seq no. tcp_rexmit_max marks the end of special slow 16460 * start phase. tcp_snd_burst controls how many segments 16461 * can be sent because of an ack. 16462 */ 16463 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 16464 tcp->tcp_snd_burst = TCP_CWND_SS; 16465 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16466 (tcp->tcp_unsent == 0)) { 16467 tcp->tcp_rexmit_max = tcp->tcp_fss; 16468 } else { 16469 tcp->tcp_rexmit_max = tcp->tcp_snxt; 16470 } 16471 tcp->tcp_rexmit = B_TRUE; 16472 tcp->tcp_dupack_cnt = 0; 16473 16474 /* 16475 * Remove all rexmit SACK blk to start from fresh. 16476 */ 16477 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 16478 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 16479 tcp->tcp_num_notsack_blk = 0; 16480 tcp->tcp_cnt_notsack_list = 0; 16481 } 16482 if (mp == NULL) { 16483 return; 16484 } 16485 /* 16486 * Attach credentials to retransmitted initial SYNs. 16487 * In theory we should use the credentials from the connect() 16488 * call to ensure that getpeerucred() on the peer will be correct. 16489 * But we assume that SYN's are not dropped for loopback connections. 16490 */ 16491 if (tcp->tcp_state == TCPS_SYN_SENT) { 16492 mblk_setcred(mp, tcp->tcp_cred, tcp->tcp_cpid); 16493 } 16494 16495 tcp->tcp_csuna = tcp->tcp_snxt; 16496 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 16497 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 16498 tcp_send_data(tcp, tcp->tcp_wq, mp); 16499 16500 } 16501 16502 static int 16503 tcp_do_unbind(conn_t *connp) 16504 { 16505 tcp_t *tcp = connp->conn_tcp; 16506 int error = 0; 16507 16508 switch (tcp->tcp_state) { 16509 case TCPS_BOUND: 16510 case TCPS_LISTEN: 16511 break; 16512 default: 16513 return (-TOUTSTATE); 16514 } 16515 16516 /* 16517 * Need to clean up all the eagers since after the unbind, segments 16518 * will no longer be delivered to this listener stream. 16519 */ 16520 mutex_enter(&tcp->tcp_eager_lock); 16521 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 16522 tcp_eager_cleanup(tcp, 0); 16523 } 16524 mutex_exit(&tcp->tcp_eager_lock); 16525 16526 if (tcp->tcp_ipversion == IPV4_VERSION) { 16527 tcp->tcp_ipha->ipha_src = 0; 16528 } else { 16529 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16530 } 16531 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16532 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 16533 tcp_bind_hash_remove(tcp); 16534 tcp->tcp_state = TCPS_IDLE; 16535 tcp->tcp_mdt = B_FALSE; 16536 16537 connp = tcp->tcp_connp; 16538 connp->conn_mdt_ok = B_FALSE; 16539 ipcl_hash_remove(connp); 16540 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 16541 16542 return (error); 16543 } 16544 16545 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 16546 static void 16547 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 16548 { 16549 int error = tcp_do_unbind(tcp->tcp_connp); 16550 16551 if (error > 0) { 16552 tcp_err_ack(tcp, mp, TSYSERR, error); 16553 } else if (error < 0) { 16554 tcp_err_ack(tcp, mp, -error, 0); 16555 } else { 16556 /* Send M_FLUSH according to TPI */ 16557 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 16558 16559 mp = mi_tpi_ok_ack_alloc(mp); 16560 putnext(tcp->tcp_rq, mp); 16561 } 16562 } 16563 16564 /* 16565 * Don't let port fall into the privileged range. 16566 * Since the extra privileged ports can be arbitrary we also 16567 * ensure that we exclude those from consideration. 16568 * tcp_g_epriv_ports is not sorted thus we loop over it until 16569 * there are no changes. 16570 * 16571 * Note: No locks are held when inspecting tcp_g_*epriv_ports 16572 * but instead the code relies on: 16573 * - the fact that the address of the array and its size never changes 16574 * - the atomic assignment of the elements of the array 16575 * 16576 * Returns 0 if there are no more ports available. 16577 * 16578 * TS note: skip multilevel ports. 16579 */ 16580 static in_port_t 16581 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 16582 { 16583 int i; 16584 boolean_t restart = B_FALSE; 16585 tcp_stack_t *tcps = tcp->tcp_tcps; 16586 16587 if (random && tcp_random_anon_port != 0) { 16588 (void) random_get_pseudo_bytes((uint8_t *)&port, 16589 sizeof (in_port_t)); 16590 /* 16591 * Unless changed by a sys admin, the smallest anon port 16592 * is 32768 and the largest anon port is 65535. It is 16593 * very likely (50%) for the random port to be smaller 16594 * than the smallest anon port. When that happens, 16595 * add port % (anon port range) to the smallest anon 16596 * port to get the random port. It should fall into the 16597 * valid anon port range. 16598 */ 16599 if (port < tcps->tcps_smallest_anon_port) { 16600 port = tcps->tcps_smallest_anon_port + 16601 port % (tcps->tcps_largest_anon_port - 16602 tcps->tcps_smallest_anon_port); 16603 } 16604 } 16605 16606 retry: 16607 if (port < tcps->tcps_smallest_anon_port) 16608 port = (in_port_t)tcps->tcps_smallest_anon_port; 16609 16610 if (port > tcps->tcps_largest_anon_port) { 16611 if (restart) 16612 return (0); 16613 restart = B_TRUE; 16614 port = (in_port_t)tcps->tcps_smallest_anon_port; 16615 } 16616 16617 if (port < tcps->tcps_smallest_nonpriv_port) 16618 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 16619 16620 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 16621 if (port == tcps->tcps_g_epriv_ports[i]) { 16622 port++; 16623 /* 16624 * Make sure whether the port is in the 16625 * valid range. 16626 */ 16627 goto retry; 16628 } 16629 } 16630 if (is_system_labeled() && 16631 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 16632 IPPROTO_TCP, B_TRUE)) != 0) { 16633 port = i; 16634 goto retry; 16635 } 16636 return (port); 16637 } 16638 16639 /* 16640 * Return the next anonymous port in the privileged port range for 16641 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 16642 * downwards. This is the same behavior as documented in the userland 16643 * library call rresvport(3N). 16644 * 16645 * TS note: skip multilevel ports. 16646 */ 16647 static in_port_t 16648 tcp_get_next_priv_port(const tcp_t *tcp) 16649 { 16650 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 16651 in_port_t nextport; 16652 boolean_t restart = B_FALSE; 16653 tcp_stack_t *tcps = tcp->tcp_tcps; 16654 retry: 16655 if (next_priv_port < tcps->tcps_min_anonpriv_port || 16656 next_priv_port >= IPPORT_RESERVED) { 16657 next_priv_port = IPPORT_RESERVED - 1; 16658 if (restart) 16659 return (0); 16660 restart = B_TRUE; 16661 } 16662 if (is_system_labeled() && 16663 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 16664 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 16665 next_priv_port = nextport; 16666 goto retry; 16667 } 16668 return (next_priv_port--); 16669 } 16670 16671 /* The write side r/w procedure. */ 16672 16673 #if CCS_STATS 16674 struct { 16675 struct { 16676 int64_t count, bytes; 16677 } tot, hit; 16678 } wrw_stats; 16679 #endif 16680 16681 /* 16682 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 16683 * messages. 16684 */ 16685 /* ARGSUSED */ 16686 static void 16687 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 16688 { 16689 conn_t *connp = (conn_t *)arg; 16690 tcp_t *tcp = connp->conn_tcp; 16691 queue_t *q = tcp->tcp_wq; 16692 16693 ASSERT(DB_TYPE(mp) != M_IOCTL); 16694 /* 16695 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 16696 * Once the close starts, streamhead and sockfs will not let any data 16697 * packets come down (close ensures that there are no threads using the 16698 * queue and no new threads will come down) but since qprocsoff() 16699 * hasn't happened yet, a M_FLUSH or some non data message might 16700 * get reflected back (in response to our own FLUSHRW) and get 16701 * processed after tcp_close() is done. The conn would still be valid 16702 * because a ref would have added but we need to check the state 16703 * before actually processing the packet. 16704 */ 16705 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 16706 freemsg(mp); 16707 return; 16708 } 16709 16710 switch (DB_TYPE(mp)) { 16711 case M_IOCDATA: 16712 tcp_wput_iocdata(tcp, mp); 16713 break; 16714 case M_FLUSH: 16715 tcp_wput_flush(tcp, mp); 16716 break; 16717 default: 16718 CALL_IP_WPUT(connp, q, mp); 16719 break; 16720 } 16721 } 16722 16723 /* 16724 * The TCP fast path write put procedure. 16725 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 16726 */ 16727 /* ARGSUSED */ 16728 void 16729 tcp_output(void *arg, mblk_t *mp, void *arg2) 16730 { 16731 int len; 16732 int hdrlen; 16733 int plen; 16734 mblk_t *mp1; 16735 uchar_t *rptr; 16736 uint32_t snxt; 16737 tcph_t *tcph; 16738 struct datab *db; 16739 uint32_t suna; 16740 uint32_t mss; 16741 ipaddr_t *dst; 16742 ipaddr_t *src; 16743 uint32_t sum; 16744 int usable; 16745 conn_t *connp = (conn_t *)arg; 16746 tcp_t *tcp = connp->conn_tcp; 16747 uint32_t msize; 16748 tcp_stack_t *tcps = tcp->tcp_tcps; 16749 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 16750 16751 /* 16752 * Try and ASSERT the minimum possible references on the 16753 * conn early enough. Since we are executing on write side, 16754 * the connection is obviously not detached and that means 16755 * there is a ref each for TCP and IP. Since we are behind 16756 * the squeue, the minimum references needed are 3. If the 16757 * conn is in classifier hash list, there should be an 16758 * extra ref for that (we check both the possibilities). 16759 */ 16760 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 16761 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 16762 16763 ASSERT(DB_TYPE(mp) == M_DATA); 16764 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 16765 16766 mutex_enter(&tcp->tcp_non_sq_lock); 16767 tcp->tcp_squeue_bytes -= msize; 16768 mutex_exit(&tcp->tcp_non_sq_lock); 16769 16770 /* Check to see if this connection wants to be re-fused. */ 16771 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 16772 if (tcp->tcp_ipversion == IPV4_VERSION) { 16773 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 16774 &tcp->tcp_saved_tcph); 16775 } else { 16776 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 16777 &tcp->tcp_saved_tcph); 16778 } 16779 } 16780 /* Bypass tcp protocol for fused tcp loopback */ 16781 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 16782 return; 16783 16784 mss = tcp->tcp_mss; 16785 if (tcp->tcp_xmit_zc_clean) 16786 mp = tcp_zcopy_backoff(tcp, mp, 0); 16787 16788 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 16789 len = (int)(mp->b_wptr - mp->b_rptr); 16790 16791 /* 16792 * Criteria for fast path: 16793 * 16794 * 1. no unsent data 16795 * 2. single mblk in request 16796 * 3. connection established 16797 * 4. data in mblk 16798 * 5. len <= mss 16799 * 6. no tcp_valid bits 16800 */ 16801 if ((tcp->tcp_unsent != 0) || 16802 (tcp->tcp_cork) || 16803 (mp->b_cont != NULL) || 16804 (tcp->tcp_state != TCPS_ESTABLISHED) || 16805 (len == 0) || 16806 (len > mss) || 16807 (tcp->tcp_valid_bits != 0)) { 16808 tcp_wput_data(tcp, mp, B_FALSE); 16809 return; 16810 } 16811 16812 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 16813 ASSERT(tcp->tcp_fin_sent == 0); 16814 16815 /* queue new packet onto retransmission queue */ 16816 if (tcp->tcp_xmit_head == NULL) { 16817 tcp->tcp_xmit_head = mp; 16818 } else { 16819 tcp->tcp_xmit_last->b_cont = mp; 16820 } 16821 tcp->tcp_xmit_last = mp; 16822 tcp->tcp_xmit_tail = mp; 16823 16824 /* find out how much we can send */ 16825 /* BEGIN CSTYLED */ 16826 /* 16827 * un-acked usable 16828 * |--------------|-----------------| 16829 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 16830 */ 16831 /* END CSTYLED */ 16832 16833 /* start sending from tcp_snxt */ 16834 snxt = tcp->tcp_snxt; 16835 16836 /* 16837 * Check to see if this connection has been idled for some 16838 * time and no ACK is expected. If it is, we need to slow 16839 * start again to get back the connection's "self-clock" as 16840 * described in VJ's paper. 16841 * 16842 * Refer to the comment in tcp_mss_set() for the calculation 16843 * of tcp_cwnd after idle. 16844 */ 16845 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 16846 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 16847 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 16848 } 16849 16850 usable = tcp->tcp_swnd; /* tcp window size */ 16851 if (usable > tcp->tcp_cwnd) 16852 usable = tcp->tcp_cwnd; /* congestion window smaller */ 16853 usable -= snxt; /* subtract stuff already sent */ 16854 suna = tcp->tcp_suna; 16855 usable += suna; 16856 /* usable can be < 0 if the congestion window is smaller */ 16857 if (len > usable) { 16858 /* Can't send complete M_DATA in one shot */ 16859 goto slow; 16860 } 16861 16862 mutex_enter(&tcp->tcp_non_sq_lock); 16863 if (tcp->tcp_flow_stopped && 16864 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 16865 tcp_clrqfull(tcp); 16866 } 16867 mutex_exit(&tcp->tcp_non_sq_lock); 16868 16869 /* 16870 * determine if anything to send (Nagle). 16871 * 16872 * 1. len < tcp_mss (i.e. small) 16873 * 2. unacknowledged data present 16874 * 3. len < nagle limit 16875 * 4. last packet sent < nagle limit (previous packet sent) 16876 */ 16877 if ((len < mss) && (snxt != suna) && 16878 (len < (int)tcp->tcp_naglim) && 16879 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 16880 /* 16881 * This was the first unsent packet and normally 16882 * mss < xmit_hiwater so there is no need to worry 16883 * about flow control. The next packet will go 16884 * through the flow control check in tcp_wput_data(). 16885 */ 16886 /* leftover work from above */ 16887 tcp->tcp_unsent = len; 16888 tcp->tcp_xmit_tail_unsent = len; 16889 16890 return; 16891 } 16892 16893 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 16894 16895 if (snxt == suna) { 16896 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 16897 } 16898 16899 /* we have always sent something */ 16900 tcp->tcp_rack_cnt = 0; 16901 16902 tcp->tcp_snxt = snxt + len; 16903 tcp->tcp_rack = tcp->tcp_rnxt; 16904 16905 if ((mp1 = dupb(mp)) == 0) 16906 goto no_memory; 16907 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 16908 mp->b_next = (mblk_t *)(uintptr_t)snxt; 16909 16910 /* adjust tcp header information */ 16911 tcph = tcp->tcp_tcph; 16912 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 16913 16914 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 16915 sum = (sum >> 16) + (sum & 0xFFFF); 16916 U16_TO_ABE16(sum, tcph->th_sum); 16917 16918 U32_TO_ABE32(snxt, tcph->th_seq); 16919 16920 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 16921 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 16922 BUMP_LOCAL(tcp->tcp_obsegs); 16923 16924 /* Update the latest receive window size in TCP header. */ 16925 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 16926 tcph->th_win); 16927 16928 tcp->tcp_last_sent_len = (ushort_t)len; 16929 16930 plen = len + tcp->tcp_hdr_len; 16931 16932 if (tcp->tcp_ipversion == IPV4_VERSION) { 16933 tcp->tcp_ipha->ipha_length = htons(plen); 16934 } else { 16935 tcp->tcp_ip6h->ip6_plen = htons(plen - 16936 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 16937 } 16938 16939 /* see if we need to allocate a mblk for the headers */ 16940 hdrlen = tcp->tcp_hdr_len; 16941 rptr = mp1->b_rptr - hdrlen; 16942 db = mp1->b_datap; 16943 if ((db->db_ref != 2) || rptr < db->db_base || 16944 (!OK_32PTR(rptr))) { 16945 /* NOTE: we assume allocb returns an OK_32PTR */ 16946 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 16947 tcps->tcps_wroff_xtra, BPRI_MED); 16948 if (!mp) { 16949 freemsg(mp1); 16950 goto no_memory; 16951 } 16952 mp->b_cont = mp1; 16953 mp1 = mp; 16954 /* Leave room for Link Level header */ 16955 /* hdrlen = tcp->tcp_hdr_len; */ 16956 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 16957 mp1->b_wptr = &rptr[hdrlen]; 16958 } 16959 mp1->b_rptr = rptr; 16960 16961 /* Fill in the timestamp option. */ 16962 if (tcp->tcp_snd_ts_ok) { 16963 U32_TO_BE32((uint32_t)lbolt, 16964 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 16965 U32_TO_BE32(tcp->tcp_ts_recent, 16966 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 16967 } else { 16968 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 16969 } 16970 16971 /* copy header into outgoing packet */ 16972 dst = (ipaddr_t *)rptr; 16973 src = (ipaddr_t *)tcp->tcp_iphc; 16974 dst[0] = src[0]; 16975 dst[1] = src[1]; 16976 dst[2] = src[2]; 16977 dst[3] = src[3]; 16978 dst[4] = src[4]; 16979 dst[5] = src[5]; 16980 dst[6] = src[6]; 16981 dst[7] = src[7]; 16982 dst[8] = src[8]; 16983 dst[9] = src[9]; 16984 if (hdrlen -= 40) { 16985 hdrlen >>= 2; 16986 dst += 10; 16987 src += 10; 16988 do { 16989 *dst++ = *src++; 16990 } while (--hdrlen); 16991 } 16992 16993 /* 16994 * Set the ECN info in the TCP header. Note that this 16995 * is not the template header. 16996 */ 16997 if (tcp->tcp_ecn_ok) { 16998 SET_ECT(tcp, rptr); 16999 17000 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17001 if (tcp->tcp_ecn_echo_on) 17002 tcph->th_flags[0] |= TH_ECE; 17003 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17004 tcph->th_flags[0] |= TH_CWR; 17005 tcp->tcp_ecn_cwr_sent = B_TRUE; 17006 } 17007 } 17008 17009 if (tcp->tcp_ip_forward_progress) { 17010 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17011 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17012 tcp->tcp_ip_forward_progress = B_FALSE; 17013 } 17014 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17015 return; 17016 17017 /* 17018 * If we ran out of memory, we pretend to have sent the packet 17019 * and that it was lost on the wire. 17020 */ 17021 no_memory: 17022 return; 17023 17024 slow: 17025 /* leftover work from above */ 17026 tcp->tcp_unsent = len; 17027 tcp->tcp_xmit_tail_unsent = len; 17028 tcp_wput_data(tcp, NULL, B_FALSE); 17029 } 17030 17031 /* ARGSUSED */ 17032 void 17033 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17034 { 17035 conn_t *connp = (conn_t *)arg; 17036 tcp_t *tcp = connp->conn_tcp; 17037 queue_t *q = tcp->tcp_rq; 17038 struct tcp_options *tcpopt; 17039 tcp_stack_t *tcps = tcp->tcp_tcps; 17040 17041 /* socket options */ 17042 uint_t sopp_flags; 17043 ssize_t sopp_rxhiwat; 17044 ssize_t sopp_maxblk; 17045 ushort_t sopp_wroff; 17046 ushort_t sopp_tail; 17047 ushort_t sopp_copyopt; 17048 17049 tcpopt = (struct tcp_options *)mp->b_rptr; 17050 17051 /* 17052 * Drop the eager's ref on the listener, that was placed when 17053 * this eager began life in tcp_conn_request. 17054 */ 17055 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17056 if (IPCL_IS_NONSTR(connp)) { 17057 /* Safe to free conn_ind message */ 17058 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17059 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17060 } 17061 17062 tcp->tcp_detached = B_FALSE; 17063 17064 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17065 /* 17066 * Someone blewoff the eager before we could finish 17067 * the accept. 17068 * 17069 * The only reason eager exists it because we put in 17070 * a ref on it when conn ind went up. We need to send 17071 * a disconnect indication up while the last reference 17072 * on the eager will be dropped by the squeue when we 17073 * return. 17074 */ 17075 ASSERT(tcp->tcp_listener == NULL); 17076 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17077 if (IPCL_IS_NONSTR(connp)) { 17078 ASSERT(tcp->tcp_issocket); 17079 (*connp->conn_upcalls->su_disconnected)( 17080 connp->conn_upper_handle, tcp->tcp_connid, 17081 ECONNREFUSED); 17082 freemsg(mp); 17083 } else { 17084 struct T_discon_ind *tdi; 17085 17086 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17087 /* 17088 * Let us reuse the incoming mblk to avoid 17089 * memory allocation failure problems. We know 17090 * that the size of the incoming mblk i.e. 17091 * stroptions is greater than sizeof 17092 * T_discon_ind. So the reallocb below can't 17093 * fail. 17094 */ 17095 freemsg(mp->b_cont); 17096 mp->b_cont = NULL; 17097 ASSERT(DB_REF(mp) == 1); 17098 mp = reallocb(mp, sizeof (struct T_discon_ind), 17099 B_FALSE); 17100 ASSERT(mp != NULL); 17101 DB_TYPE(mp) = M_PROTO; 17102 ((union T_primitives *)mp->b_rptr)->type = 17103 T_DISCON_IND; 17104 tdi = (struct T_discon_ind *)mp->b_rptr; 17105 if (tcp->tcp_issocket) { 17106 tdi->DISCON_reason = ECONNREFUSED; 17107 tdi->SEQ_number = 0; 17108 } else { 17109 tdi->DISCON_reason = ENOPROTOOPT; 17110 tdi->SEQ_number = 17111 tcp->tcp_conn_req_seqnum; 17112 } 17113 mp->b_wptr = mp->b_rptr + 17114 sizeof (struct T_discon_ind); 17115 putnext(q, mp); 17116 return; 17117 } 17118 } 17119 if (tcp->tcp_hard_binding) { 17120 tcp->tcp_hard_binding = B_FALSE; 17121 tcp->tcp_hard_bound = B_TRUE; 17122 } 17123 return; 17124 } 17125 17126 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17127 int boundif = tcpopt->to_boundif; 17128 uint_t len = sizeof (int); 17129 17130 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17131 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17132 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17133 } 17134 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17135 uint_t on = 1; 17136 uint_t len = sizeof (uint_t); 17137 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17138 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17139 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17140 } 17141 17142 /* 17143 * For a loopback connection with tcp_direct_sockfs on, note that 17144 * we don't have to protect tcp_rcv_list yet because synchronous 17145 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17146 * possibly race with us. 17147 */ 17148 17149 /* 17150 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17151 * properly. This is the first time we know of the acceptor' 17152 * queue. So we do it here. 17153 * 17154 * XXX 17155 */ 17156 if (tcp->tcp_rcv_list == NULL) { 17157 /* 17158 * Recv queue is empty, tcp_rwnd should not have changed. 17159 * That means it should be equal to the listener's tcp_rwnd. 17160 */ 17161 if (!IPCL_IS_NONSTR(connp)) 17162 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17163 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 17164 } else { 17165 #ifdef DEBUG 17166 mblk_t *tmp; 17167 mblk_t *mp1; 17168 uint_t cnt = 0; 17169 17170 mp1 = tcp->tcp_rcv_list; 17171 while ((tmp = mp1) != NULL) { 17172 mp1 = tmp->b_next; 17173 cnt += msgdsize(tmp); 17174 } 17175 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17176 #endif 17177 /* There is some data, add them back to get the max. */ 17178 if (!IPCL_IS_NONSTR(connp)) 17179 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17180 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17181 } 17182 /* 17183 * This is the first time we run on the correct 17184 * queue after tcp_accept. So fix all the q parameters 17185 * here. 17186 */ 17187 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 17188 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17189 17190 /* 17191 * Record the stream head's high water mark for this endpoint; 17192 * this is used for flow-control purposes. 17193 */ 17194 sopp_rxhiwat = tcp->tcp_fused ? 17195 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 17196 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 17197 17198 /* 17199 * Determine what write offset value to use depending on SACK and 17200 * whether the endpoint is fused or not. 17201 */ 17202 if (tcp->tcp_fused) { 17203 ASSERT(tcp->tcp_loopback); 17204 ASSERT(tcp->tcp_loopback_peer != NULL); 17205 /* 17206 * For fused tcp loopback, set the stream head's write 17207 * offset value to zero since we won't be needing any room 17208 * for TCP/IP headers. This would also improve performance 17209 * since it would reduce the amount of work done by kmem. 17210 * Non-fused tcp loopback case is handled separately below. 17211 */ 17212 sopp_wroff = 0; 17213 /* 17214 * Update the peer's transmit parameters according to 17215 * our recently calculated high water mark value. 17216 */ 17217 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17218 } else if (tcp->tcp_snd_sack_ok) { 17219 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17220 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17221 } else { 17222 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17223 tcps->tcps_wroff_xtra); 17224 } 17225 17226 /* 17227 * If this is endpoint is handling SSL, then reserve extra 17228 * offset and space at the end. 17229 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17230 * overriding the previous setting. The extra cost of signing and 17231 * encrypting multiple MSS-size records (12 of them with Ethernet), 17232 * instead of a single contiguous one by the stream head 17233 * largely outweighs the statistical reduction of ACKs, when 17234 * applicable. The peer will also save on decryption and verification 17235 * costs. 17236 */ 17237 if (tcp->tcp_kssl_ctx != NULL) { 17238 sopp_wroff += SSL3_WROFFSET; 17239 17240 sopp_flags |= SOCKOPT_TAIL; 17241 sopp_tail = SSL3_MAX_TAIL_LEN; 17242 17243 sopp_flags |= SOCKOPT_ZCOPY; 17244 sopp_copyopt = ZCVMUNSAFE; 17245 17246 sopp_maxblk = SSL3_MAX_RECORD_LEN; 17247 } 17248 17249 /* Send the options up */ 17250 if (IPCL_IS_NONSTR(connp)) { 17251 struct sock_proto_props sopp; 17252 17253 sopp.sopp_flags = sopp_flags; 17254 sopp.sopp_wroff = sopp_wroff; 17255 sopp.sopp_maxblk = sopp_maxblk; 17256 sopp.sopp_rxhiwat = sopp_rxhiwat; 17257 if (sopp_flags & SOCKOPT_TAIL) { 17258 ASSERT(tcp->tcp_kssl_ctx != NULL); 17259 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 17260 sopp.sopp_tail = sopp_tail; 17261 sopp.sopp_zcopyflag = sopp_copyopt; 17262 } 17263 (*connp->conn_upcalls->su_set_proto_props) 17264 (connp->conn_upper_handle, &sopp); 17265 } else { 17266 struct stroptions *stropt; 17267 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17268 if (stropt_mp == NULL) { 17269 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 17270 return; 17271 } 17272 DB_TYPE(stropt_mp) = M_SETOPTS; 17273 stropt = (struct stroptions *)stropt_mp->b_rptr; 17274 stropt_mp->b_wptr += sizeof (struct stroptions); 17275 stropt = (struct stroptions *)stropt_mp->b_rptr; 17276 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 17277 stropt->so_hiwat = sopp_rxhiwat; 17278 stropt->so_wroff = sopp_wroff; 17279 stropt->so_maxblk = sopp_maxblk; 17280 17281 if (sopp_flags & SOCKOPT_TAIL) { 17282 ASSERT(tcp->tcp_kssl_ctx != NULL); 17283 17284 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 17285 stropt->so_tail = sopp_tail; 17286 stropt->so_copyopt = sopp_copyopt; 17287 } 17288 17289 /* Send the options up */ 17290 putnext(q, stropt_mp); 17291 } 17292 17293 freemsg(mp); 17294 /* 17295 * Pass up any data and/or a fin that has been received. 17296 * 17297 * Adjust receive window in case it had decreased 17298 * (because there is data <=> tcp_rcv_list != NULL) 17299 * while the connection was detached. Note that 17300 * in case the eager was flow-controlled, w/o this 17301 * code, the rwnd may never open up again! 17302 */ 17303 if (tcp->tcp_rcv_list != NULL) { 17304 if (IPCL_IS_NONSTR(connp)) { 17305 mblk_t *mp; 17306 int space_left; 17307 int error; 17308 boolean_t push = B_TRUE; 17309 17310 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 17311 (connp->conn_upper_handle, NULL, 0, 0, &error, 17312 &push) >= 0) { 17313 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 17314 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17315 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17316 tcp_xmit_ctl(NULL, 17317 tcp, (tcp->tcp_swnd == 0) ? 17318 tcp->tcp_suna : tcp->tcp_snxt, 17319 tcp->tcp_rnxt, TH_ACK); 17320 } 17321 } 17322 while ((mp = tcp->tcp_rcv_list) != NULL) { 17323 push = B_TRUE; 17324 tcp->tcp_rcv_list = mp->b_next; 17325 mp->b_next = NULL; 17326 space_left = (*connp->conn_upcalls->su_recv) 17327 (connp->conn_upper_handle, mp, msgdsize(mp), 17328 0, &error, &push); 17329 if (space_left < 0) { 17330 /* 17331 * We should never be in middle of a 17332 * fallback, the squeue guarantees that. 17333 */ 17334 ASSERT(error != EOPNOTSUPP); 17335 } 17336 } 17337 tcp->tcp_rcv_last_head = NULL; 17338 tcp->tcp_rcv_last_tail = NULL; 17339 tcp->tcp_rcv_cnt = 0; 17340 } else { 17341 /* We drain directly in case of fused tcp loopback */ 17342 17343 if (!tcp->tcp_fused && canputnext(q)) { 17344 tcp->tcp_rwnd = q->q_hiwat; 17345 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17346 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17347 tcp_xmit_ctl(NULL, 17348 tcp, (tcp->tcp_swnd == 0) ? 17349 tcp->tcp_suna : tcp->tcp_snxt, 17350 tcp->tcp_rnxt, TH_ACK); 17351 } 17352 } 17353 17354 (void) tcp_rcv_drain(tcp); 17355 } 17356 17357 /* 17358 * For fused tcp loopback, back-enable peer endpoint 17359 * if it's currently flow-controlled. 17360 */ 17361 if (tcp->tcp_fused) { 17362 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17363 17364 ASSERT(peer_tcp != NULL); 17365 ASSERT(peer_tcp->tcp_fused); 17366 /* 17367 * In order to change the peer's tcp_flow_stopped, 17368 * we need to take locks for both end points. The 17369 * highest address is taken first. 17370 */ 17371 if (peer_tcp > tcp) { 17372 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17373 mutex_enter(&tcp->tcp_non_sq_lock); 17374 } else { 17375 mutex_enter(&tcp->tcp_non_sq_lock); 17376 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17377 } 17378 if (peer_tcp->tcp_flow_stopped) { 17379 tcp_clrqfull(peer_tcp); 17380 TCP_STAT(tcps, tcp_fusion_backenabled); 17381 } 17382 mutex_exit(&peer_tcp->tcp_non_sq_lock); 17383 mutex_exit(&tcp->tcp_non_sq_lock); 17384 } 17385 } 17386 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17387 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17388 tcp->tcp_ordrel_done = B_TRUE; 17389 if (IPCL_IS_NONSTR(connp)) { 17390 ASSERT(tcp->tcp_ordrel_mp == NULL); 17391 (*connp->conn_upcalls->su_opctl)( 17392 connp->conn_upper_handle, 17393 SOCK_OPCTL_SHUT_RECV, 0); 17394 } else { 17395 mp = tcp->tcp_ordrel_mp; 17396 tcp->tcp_ordrel_mp = NULL; 17397 putnext(q, mp); 17398 } 17399 } 17400 if (tcp->tcp_hard_binding) { 17401 tcp->tcp_hard_binding = B_FALSE; 17402 tcp->tcp_hard_bound = B_TRUE; 17403 } 17404 17405 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 17406 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 17407 tcp->tcp_loopback_peer != NULL && 17408 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 17409 tcp_fuse_syncstr_enable_pair(tcp); 17410 } 17411 17412 if (tcp->tcp_ka_enabled) { 17413 tcp->tcp_ka_last_intrvl = 0; 17414 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17415 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17416 } 17417 17418 /* 17419 * At this point, eager is fully established and will 17420 * have the following references - 17421 * 17422 * 2 references for connection to exist (1 for TCP and 1 for IP). 17423 * 1 reference for the squeue which will be dropped by the squeue as 17424 * soon as this function returns. 17425 * There will be 1 additonal reference for being in classifier 17426 * hash list provided something bad hasn't happened. 17427 */ 17428 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17429 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17430 } 17431 17432 /* 17433 * The function called through squeue to get behind listener's perimeter to 17434 * send a deffered conn_ind. 17435 */ 17436 /* ARGSUSED */ 17437 void 17438 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17439 { 17440 conn_t *connp = (conn_t *)arg; 17441 tcp_t *listener = connp->conn_tcp; 17442 struct T_conn_ind *conn_ind; 17443 tcp_t *tcp; 17444 17445 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17446 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17447 conn_ind->OPT_length); 17448 17449 if (listener->tcp_state == TCPS_CLOSED || 17450 TCP_IS_DETACHED(listener)) { 17451 /* 17452 * If listener has closed, it would have caused a 17453 * a cleanup/blowoff to happen for the eager. 17454 * 17455 * We need to drop the ref on eager that was put 17456 * tcp_rput_data() before trying to send the conn_ind 17457 * to listener. The conn_ind was deferred in tcp_send_conn_ind 17458 * and tcp_wput_accept() is sending this deferred conn_ind but 17459 * listener is closed so we drop the ref. 17460 */ 17461 CONN_DEC_REF(tcp->tcp_connp); 17462 freemsg(mp); 17463 return; 17464 } 17465 17466 tcp_ulp_newconn(connp, tcp->tcp_connp, mp); 17467 } 17468 17469 /* ARGSUSED */ 17470 static int 17471 tcp_accept_common(conn_t *lconnp, conn_t *econnp, cred_t *cr) 17472 { 17473 tcp_t *listener, *eager; 17474 mblk_t *opt_mp; 17475 struct tcp_options *tcpopt; 17476 17477 listener = lconnp->conn_tcp; 17478 ASSERT(listener->tcp_state == TCPS_LISTEN); 17479 eager = econnp->conn_tcp; 17480 ASSERT(eager->tcp_listener != NULL); 17481 17482 ASSERT(eager->tcp_rq != NULL); 17483 17484 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 17485 if (opt_mp == NULL) { 17486 return (-TPROTO); 17487 } 17488 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 17489 eager->tcp_issocket = B_TRUE; 17490 17491 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17492 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 17493 ASSERT(econnp->conn_netstack == 17494 listener->tcp_connp->conn_netstack); 17495 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 17496 17497 /* Put the ref for IP */ 17498 CONN_INC_REF(econnp); 17499 17500 /* 17501 * We should have minimum of 3 references on the conn 17502 * at this point. One each for TCP and IP and one for 17503 * the T_conn_ind that was sent up when the 3-way handshake 17504 * completed. In the normal case we would also have another 17505 * reference (making a total of 4) for the conn being in the 17506 * classifier hash list. However the eager could have received 17507 * an RST subsequently and tcp_closei_local could have removed 17508 * the eager from the classifier hash list, hence we can't 17509 * assert that reference. 17510 */ 17511 ASSERT(econnp->conn_ref >= 3); 17512 17513 opt_mp->b_datap->db_type = M_SETOPTS; 17514 opt_mp->b_wptr += sizeof (struct tcp_options); 17515 17516 /* 17517 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 17518 * from listener to acceptor. 17519 */ 17520 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 17521 tcpopt->to_flags = 0; 17522 17523 if (listener->tcp_bound_if != 0) { 17524 tcpopt->to_flags |= TCPOPT_BOUNDIF; 17525 tcpopt->to_boundif = listener->tcp_bound_if; 17526 } 17527 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 17528 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 17529 } 17530 17531 mutex_enter(&listener->tcp_eager_lock); 17532 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 17533 17534 tcp_t *tail; 17535 tcp_t *tcp; 17536 mblk_t *mp1; 17537 17538 tcp = listener->tcp_eager_prev_q0; 17539 /* 17540 * listener->tcp_eager_prev_q0 points to the TAIL of the 17541 * deferred T_conn_ind queue. We need to get to the head 17542 * of the queue in order to send up T_conn_ind the same 17543 * order as how the 3WHS is completed. 17544 */ 17545 while (tcp != listener) { 17546 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 17547 !tcp->tcp_kssl_pending) 17548 break; 17549 else 17550 tcp = tcp->tcp_eager_prev_q0; 17551 } 17552 /* None of the pending eagers can be sent up now */ 17553 if (tcp == listener) 17554 goto no_more_eagers; 17555 17556 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 17557 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17558 /* Move from q0 to q */ 17559 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 17560 listener->tcp_conn_req_cnt_q0--; 17561 listener->tcp_conn_req_cnt_q++; 17562 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 17563 tcp->tcp_eager_prev_q0; 17564 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 17565 tcp->tcp_eager_next_q0; 17566 tcp->tcp_eager_prev_q0 = NULL; 17567 tcp->tcp_eager_next_q0 = NULL; 17568 tcp->tcp_conn_def_q0 = B_FALSE; 17569 17570 /* Make sure the tcp isn't in the list of droppables */ 17571 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 17572 tcp->tcp_eager_prev_drop_q0 == NULL); 17573 17574 /* 17575 * Insert at end of the queue because sockfs sends 17576 * down T_CONN_RES in chronological order. Leaving 17577 * the older conn indications at front of the queue 17578 * helps reducing search time. 17579 */ 17580 tail = listener->tcp_eager_last_q; 17581 if (tail != NULL) { 17582 tail->tcp_eager_next_q = tcp; 17583 } else { 17584 listener->tcp_eager_next_q = tcp; 17585 } 17586 listener->tcp_eager_last_q = tcp; 17587 tcp->tcp_eager_next_q = NULL; 17588 17589 /* Need to get inside the listener perimeter */ 17590 CONN_INC_REF(listener->tcp_connp); 17591 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 17592 tcp_send_pending, listener->tcp_connp, SQ_FILL, 17593 SQTAG_TCP_SEND_PENDING); 17594 } 17595 no_more_eagers: 17596 tcp_eager_unlink(eager); 17597 mutex_exit(&listener->tcp_eager_lock); 17598 17599 /* 17600 * At this point, the eager is detached from the listener 17601 * but we still have an extra refs on eager (apart from the 17602 * usual tcp references). The ref was placed in tcp_rput_data 17603 * before sending the conn_ind in tcp_send_conn_ind. 17604 * The ref will be dropped in tcp_accept_finish(). 17605 */ 17606 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 17607 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 17608 return (0); 17609 } 17610 17611 int 17612 tcp_accept(sock_lower_handle_t lproto_handle, 17613 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 17614 cred_t *cr) 17615 { 17616 conn_t *lconnp, *econnp; 17617 tcp_t *listener, *eager; 17618 tcp_stack_t *tcps; 17619 17620 lconnp = (conn_t *)lproto_handle; 17621 listener = lconnp->conn_tcp; 17622 ASSERT(listener->tcp_state == TCPS_LISTEN); 17623 econnp = (conn_t *)eproto_handle; 17624 eager = econnp->conn_tcp; 17625 ASSERT(eager->tcp_listener != NULL); 17626 tcps = eager->tcp_tcps; 17627 17628 /* 17629 * It is OK to manipulate these fields outside the eager's squeue 17630 * because they will not start being used until tcp_accept_finish 17631 * has been called. 17632 */ 17633 ASSERT(lconnp->conn_upper_handle != NULL); 17634 ASSERT(econnp->conn_upper_handle == NULL); 17635 econnp->conn_upper_handle = sock_handle; 17636 econnp->conn_upcalls = lconnp->conn_upcalls; 17637 ASSERT(IPCL_IS_NONSTR(econnp)); 17638 /* 17639 * Create helper stream if it is a non-TPI TCP connection. 17640 */ 17641 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 17642 ip1dbg(("tcp_accept: create of IP helper stream" 17643 " failed\n")); 17644 return (EPROTO); 17645 } 17646 eager->tcp_rq = econnp->conn_rq; 17647 eager->tcp_wq = econnp->conn_wq; 17648 17649 ASSERT(eager->tcp_rq != NULL); 17650 17651 return (tcp_accept_common(lconnp, econnp, cr)); 17652 } 17653 17654 17655 /* 17656 * This is the STREAMS entry point for T_CONN_RES coming down on 17657 * Acceptor STREAM when sockfs listener does accept processing. 17658 * Read the block comment on top of tcp_conn_request(). 17659 */ 17660 void 17661 tcp_tpi_accept(queue_t *q, mblk_t *mp) 17662 { 17663 queue_t *rq = RD(q); 17664 struct T_conn_res *conn_res; 17665 tcp_t *eager; 17666 tcp_t *listener; 17667 struct T_ok_ack *ok; 17668 t_scalar_t PRIM_type; 17669 conn_t *econnp; 17670 cred_t *cr; 17671 17672 ASSERT(DB_TYPE(mp) == M_PROTO); 17673 17674 /* 17675 * All Solaris components should pass a db_credp 17676 * for this TPI message, hence we ASSERT. 17677 * But in case there is some other M_PROTO that looks 17678 * like a TPI message sent by some other kernel 17679 * component, we check and return an error. 17680 */ 17681 cr = msg_getcred(mp, NULL); 17682 ASSERT(cr != NULL); 17683 if (cr == NULL) { 17684 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 17685 if (mp != NULL) 17686 putnext(rq, mp); 17687 return; 17688 } 17689 conn_res = (struct T_conn_res *)mp->b_rptr; 17690 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17691 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17692 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17693 if (mp != NULL) 17694 putnext(rq, mp); 17695 return; 17696 } 17697 switch (conn_res->PRIM_type) { 17698 case O_T_CONN_RES: 17699 case T_CONN_RES: 17700 /* 17701 * We pass up an err ack if allocb fails. This will 17702 * cause sockfs to issue a T_DISCON_REQ which will cause 17703 * tcp_eager_blowoff to be called. sockfs will then call 17704 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17705 * we need to do the allocb up here because we have to 17706 * make sure rq->q_qinfo->qi_qclose still points to the 17707 * correct function (tcp_tpi_close_accept) in case allocb 17708 * fails. 17709 */ 17710 bcopy(mp->b_rptr + conn_res->OPT_offset, 17711 &eager, conn_res->OPT_length); 17712 PRIM_type = conn_res->PRIM_type; 17713 mp->b_datap->db_type = M_PCPROTO; 17714 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 17715 ok = (struct T_ok_ack *)mp->b_rptr; 17716 ok->PRIM_type = T_OK_ACK; 17717 ok->CORRECT_prim = PRIM_type; 17718 econnp = eager->tcp_connp; 17719 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 17720 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 17721 eager->tcp_rq = rq; 17722 eager->tcp_wq = q; 17723 rq->q_ptr = econnp; 17724 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 17725 q->q_ptr = econnp; 17726 q->q_qinfo = &tcp_winit; 17727 listener = eager->tcp_listener; 17728 17729 if (tcp_accept_common(listener->tcp_connp, 17730 econnp, cr) < 0) { 17731 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17732 if (mp != NULL) 17733 putnext(rq, mp); 17734 return; 17735 } 17736 17737 /* 17738 * Send the new local address also up to sockfs. There 17739 * should already be enough space in the mp that came 17740 * down from soaccept(). 17741 */ 17742 if (eager->tcp_family == AF_INET) { 17743 sin_t *sin; 17744 17745 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17746 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 17747 sin = (sin_t *)mp->b_wptr; 17748 mp->b_wptr += sizeof (sin_t); 17749 sin->sin_family = AF_INET; 17750 sin->sin_port = eager->tcp_lport; 17751 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 17752 } else { 17753 sin6_t *sin6; 17754 17755 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17756 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 17757 sin6 = (sin6_t *)mp->b_wptr; 17758 mp->b_wptr += sizeof (sin6_t); 17759 sin6->sin6_family = AF_INET6; 17760 sin6->sin6_port = eager->tcp_lport; 17761 if (eager->tcp_ipversion == IPV4_VERSION) { 17762 sin6->sin6_flowinfo = 0; 17763 IN6_IPADDR_TO_V4MAPPED( 17764 eager->tcp_ipha->ipha_src, 17765 &sin6->sin6_addr); 17766 } else { 17767 ASSERT(eager->tcp_ip6h != NULL); 17768 sin6->sin6_flowinfo = 17769 eager->tcp_ip6h->ip6_vcf & 17770 ~IPV6_VERS_AND_FLOW_MASK; 17771 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 17772 } 17773 sin6->sin6_scope_id = 0; 17774 sin6->__sin6_src_id = 0; 17775 } 17776 17777 putnext(rq, mp); 17778 return; 17779 default: 17780 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 17781 if (mp != NULL) 17782 putnext(rq, mp); 17783 return; 17784 } 17785 } 17786 17787 static int 17788 tcp_do_getsockname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17789 { 17790 sin_t *sin = (sin_t *)sa; 17791 sin6_t *sin6 = (sin6_t *)sa; 17792 17793 switch (tcp->tcp_family) { 17794 case AF_INET: 17795 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17796 17797 if (*salenp < sizeof (sin_t)) 17798 return (EINVAL); 17799 17800 *sin = sin_null; 17801 sin->sin_family = AF_INET; 17802 if (tcp->tcp_state >= TCPS_BOUND) { 17803 sin->sin_port = tcp->tcp_lport; 17804 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 17805 } 17806 *salenp = sizeof (sin_t); 17807 break; 17808 17809 case AF_INET6: 17810 if (*salenp < sizeof (sin6_t)) 17811 return (EINVAL); 17812 17813 *sin6 = sin6_null; 17814 sin6->sin6_family = AF_INET6; 17815 if (tcp->tcp_state >= TCPS_BOUND) { 17816 sin6->sin6_port = tcp->tcp_lport; 17817 if (tcp->tcp_ipversion == IPV4_VERSION) { 17818 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 17819 &sin6->sin6_addr); 17820 } else { 17821 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 17822 } 17823 } 17824 *salenp = sizeof (sin6_t); 17825 break; 17826 } 17827 17828 return (0); 17829 } 17830 17831 static int 17832 tcp_do_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17833 { 17834 sin_t *sin = (sin_t *)sa; 17835 sin6_t *sin6 = (sin6_t *)sa; 17836 17837 if (tcp->tcp_state < TCPS_SYN_RCVD) 17838 return (ENOTCONN); 17839 17840 switch (tcp->tcp_family) { 17841 case AF_INET: 17842 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17843 17844 if (*salenp < sizeof (sin_t)) 17845 return (EINVAL); 17846 17847 *sin = sin_null; 17848 sin->sin_family = AF_INET; 17849 sin->sin_port = tcp->tcp_fport; 17850 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 17851 sin->sin_addr.s_addr); 17852 *salenp = sizeof (sin_t); 17853 break; 17854 17855 case AF_INET6: 17856 if (*salenp < sizeof (sin6_t)) 17857 return (EINVAL); 17858 17859 *sin6 = sin6_null; 17860 sin6->sin6_family = AF_INET6; 17861 sin6->sin6_port = tcp->tcp_fport; 17862 sin6->sin6_addr = tcp->tcp_remote_v6; 17863 if (tcp->tcp_ipversion == IPV6_VERSION) { 17864 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 17865 ~IPV6_VERS_AND_FLOW_MASK; 17866 } 17867 *salenp = sizeof (sin6_t); 17868 break; 17869 } 17870 17871 return (0); 17872 } 17873 17874 /* 17875 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 17876 */ 17877 static void 17878 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 17879 { 17880 void *data; 17881 mblk_t *datamp = mp->b_cont; 17882 tcp_t *tcp = Q_TO_TCP(q); 17883 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 17884 17885 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 17886 cmdp->cb_error = EPROTO; 17887 qreply(q, mp); 17888 return; 17889 } 17890 17891 data = datamp->b_rptr; 17892 17893 switch (cmdp->cb_cmd) { 17894 case TI_GETPEERNAME: 17895 cmdp->cb_error = tcp_do_getpeername(tcp, data, &cmdp->cb_len); 17896 break; 17897 case TI_GETMYNAME: 17898 cmdp->cb_error = tcp_do_getsockname(tcp, data, &cmdp->cb_len); 17899 break; 17900 default: 17901 cmdp->cb_error = EINVAL; 17902 break; 17903 } 17904 17905 qreply(q, mp); 17906 } 17907 17908 void 17909 tcp_wput(queue_t *q, mblk_t *mp) 17910 { 17911 conn_t *connp = Q_TO_CONN(q); 17912 tcp_t *tcp; 17913 void (*output_proc)(); 17914 t_scalar_t type; 17915 uchar_t *rptr; 17916 struct iocblk *iocp; 17917 size_t size; 17918 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 17919 17920 ASSERT(connp->conn_ref >= 2); 17921 17922 switch (DB_TYPE(mp)) { 17923 case M_DATA: 17924 tcp = connp->conn_tcp; 17925 ASSERT(tcp != NULL); 17926 17927 size = msgdsize(mp); 17928 17929 mutex_enter(&tcp->tcp_non_sq_lock); 17930 tcp->tcp_squeue_bytes += size; 17931 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 17932 tcp_setqfull(tcp); 17933 } 17934 mutex_exit(&tcp->tcp_non_sq_lock); 17935 17936 CONN_INC_REF(connp); 17937 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 17938 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 17939 return; 17940 17941 case M_CMD: 17942 tcp_wput_cmdblk(q, mp); 17943 return; 17944 17945 case M_PROTO: 17946 case M_PCPROTO: 17947 /* 17948 * if it is a snmp message, don't get behind the squeue 17949 */ 17950 tcp = connp->conn_tcp; 17951 rptr = mp->b_rptr; 17952 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 17953 type = ((union T_primitives *)rptr)->type; 17954 } else { 17955 if (tcp->tcp_debug) { 17956 (void) strlog(TCP_MOD_ID, 0, 1, 17957 SL_ERROR|SL_TRACE, 17958 "tcp_wput_proto, dropping one..."); 17959 } 17960 freemsg(mp); 17961 return; 17962 } 17963 if (type == T_SVR4_OPTMGMT_REQ) { 17964 /* 17965 * All Solaris components should pass a db_credp 17966 * for this TPI message, hence we ASSERT. 17967 * But in case there is some other M_PROTO that looks 17968 * like a TPI message sent by some other kernel 17969 * component, we check and return an error. 17970 */ 17971 cred_t *cr = msg_getcred(mp, NULL); 17972 17973 ASSERT(cr != NULL); 17974 if (cr == NULL) { 17975 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 17976 return; 17977 } 17978 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 17979 cr)) { 17980 /* 17981 * This was a SNMP request 17982 */ 17983 return; 17984 } else { 17985 output_proc = tcp_wput_proto; 17986 } 17987 } else { 17988 output_proc = tcp_wput_proto; 17989 } 17990 break; 17991 case M_IOCTL: 17992 /* 17993 * Most ioctls can be processed right away without going via 17994 * squeues - process them right here. Those that do require 17995 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 17996 * are processed by tcp_wput_ioctl(). 17997 */ 17998 iocp = (struct iocblk *)mp->b_rptr; 17999 tcp = connp->conn_tcp; 18000 18001 switch (iocp->ioc_cmd) { 18002 case TCP_IOC_ABORT_CONN: 18003 tcp_ioctl_abort_conn(q, mp); 18004 return; 18005 case TI_GETPEERNAME: 18006 case TI_GETMYNAME: 18007 mi_copyin(q, mp, NULL, 18008 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18009 return; 18010 case ND_SET: 18011 /* nd_getset does the necessary checks */ 18012 case ND_GET: 18013 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18014 CALL_IP_WPUT(connp, q, mp); 18015 return; 18016 } 18017 qreply(q, mp); 18018 return; 18019 case TCP_IOC_DEFAULT_Q: 18020 /* 18021 * Wants to be the default wq. Check the credentials 18022 * first, the rest is executed via squeue. 18023 */ 18024 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18025 iocp->ioc_error = EPERM; 18026 iocp->ioc_count = 0; 18027 mp->b_datap->db_type = M_IOCACK; 18028 qreply(q, mp); 18029 return; 18030 } 18031 output_proc = tcp_wput_ioctl; 18032 break; 18033 default: 18034 output_proc = tcp_wput_ioctl; 18035 break; 18036 } 18037 break; 18038 default: 18039 output_proc = tcp_wput_nondata; 18040 break; 18041 } 18042 18043 CONN_INC_REF(connp); 18044 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 18045 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 18046 } 18047 18048 /* 18049 * Initial STREAMS write side put() procedure for sockets. It tries to 18050 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18051 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18052 * are handled by tcp_wput() as usual. 18053 * 18054 * All further messages will also be handled by tcp_wput() because we cannot 18055 * be sure that the above short cut is safe later. 18056 */ 18057 static void 18058 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18059 { 18060 conn_t *connp = Q_TO_CONN(wq); 18061 tcp_t *tcp = connp->conn_tcp; 18062 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18063 18064 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18065 wq->q_qinfo = &tcp_winit; 18066 18067 ASSERT(IPCL_IS_TCP(connp)); 18068 ASSERT(TCP_IS_SOCKET(tcp)); 18069 18070 if (DB_TYPE(mp) == M_PCPROTO && 18071 MBLKL(mp) == sizeof (struct T_capability_req) && 18072 car->PRIM_type == T_CAPABILITY_REQ) { 18073 tcp_capability_req(tcp, mp); 18074 return; 18075 } 18076 18077 tcp_wput(wq, mp); 18078 } 18079 18080 /* ARGSUSED */ 18081 static void 18082 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18083 { 18084 #ifdef DEBUG 18085 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18086 #endif 18087 freemsg(mp); 18088 } 18089 18090 static boolean_t 18091 tcp_zcopy_check(tcp_t *tcp) 18092 { 18093 conn_t *connp = tcp->tcp_connp; 18094 ire_t *ire; 18095 boolean_t zc_enabled = B_FALSE; 18096 tcp_stack_t *tcps = tcp->tcp_tcps; 18097 18098 if (do_tcpzcopy == 2) 18099 zc_enabled = B_TRUE; 18100 else if (tcp->tcp_ipversion == IPV4_VERSION && 18101 IPCL_IS_CONNECTED(connp) && 18102 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18103 connp->conn_dontroute == 0 && 18104 !connp->conn_nexthop_set && 18105 connp->conn_outgoing_ill == NULL && 18106 do_tcpzcopy == 1) { 18107 /* 18108 * the checks above closely resemble the fast path checks 18109 * in tcp_send_data(). 18110 */ 18111 mutex_enter(&connp->conn_lock); 18112 ire = connp->conn_ire_cache; 18113 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18114 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18115 IRE_REFHOLD(ire); 18116 if (ire->ire_stq != NULL) { 18117 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18118 18119 zc_enabled = ill && (ill->ill_capabilities & 18120 ILL_CAPAB_ZEROCOPY) && 18121 (ill->ill_zerocopy_capab-> 18122 ill_zerocopy_flags != 0); 18123 } 18124 IRE_REFRELE(ire); 18125 } 18126 mutex_exit(&connp->conn_lock); 18127 } 18128 tcp->tcp_snd_zcopy_on = zc_enabled; 18129 if (!TCP_IS_DETACHED(tcp)) { 18130 if (zc_enabled) { 18131 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18132 ZCVMSAFE); 18133 TCP_STAT(tcps, tcp_zcopy_on); 18134 } else { 18135 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18136 ZCVMUNSAFE); 18137 TCP_STAT(tcps, tcp_zcopy_off); 18138 } 18139 } 18140 return (zc_enabled); 18141 } 18142 18143 static mblk_t * 18144 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18145 { 18146 tcp_stack_t *tcps = tcp->tcp_tcps; 18147 18148 if (do_tcpzcopy == 2) 18149 return (bp); 18150 else if (tcp->tcp_snd_zcopy_on) { 18151 tcp->tcp_snd_zcopy_on = B_FALSE; 18152 if (!TCP_IS_DETACHED(tcp)) { 18153 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 18154 ZCVMUNSAFE); 18155 TCP_STAT(tcps, tcp_zcopy_disable); 18156 } 18157 } 18158 return (tcp_zcopy_backoff(tcp, bp, 0)); 18159 } 18160 18161 /* 18162 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18163 * the original desballoca'ed segmapped mblk. 18164 */ 18165 static mblk_t * 18166 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18167 { 18168 mblk_t *head, *tail, *nbp; 18169 tcp_stack_t *tcps = tcp->tcp_tcps; 18170 18171 if (IS_VMLOANED_MBLK(bp)) { 18172 TCP_STAT(tcps, tcp_zcopy_backoff); 18173 if ((head = copyb(bp)) == NULL) { 18174 /* fail to backoff; leave it for the next backoff */ 18175 tcp->tcp_xmit_zc_clean = B_FALSE; 18176 return (bp); 18177 } 18178 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18179 if (fix_xmitlist) 18180 tcp_zcopy_notify(tcp); 18181 else 18182 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18183 } 18184 nbp = bp->b_cont; 18185 if (fix_xmitlist) { 18186 head->b_prev = bp->b_prev; 18187 head->b_next = bp->b_next; 18188 if (tcp->tcp_xmit_tail == bp) 18189 tcp->tcp_xmit_tail = head; 18190 } 18191 bp->b_next = NULL; 18192 bp->b_prev = NULL; 18193 freeb(bp); 18194 } else { 18195 head = bp; 18196 nbp = bp->b_cont; 18197 } 18198 tail = head; 18199 while (nbp) { 18200 if (IS_VMLOANED_MBLK(nbp)) { 18201 TCP_STAT(tcps, tcp_zcopy_backoff); 18202 if ((tail->b_cont = copyb(nbp)) == NULL) { 18203 tcp->tcp_xmit_zc_clean = B_FALSE; 18204 tail->b_cont = nbp; 18205 return (head); 18206 } 18207 tail = tail->b_cont; 18208 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18209 if (fix_xmitlist) 18210 tcp_zcopy_notify(tcp); 18211 else 18212 tail->b_datap->db_struioflag |= 18213 STRUIO_ZCNOTIFY; 18214 } 18215 bp = nbp; 18216 nbp = nbp->b_cont; 18217 if (fix_xmitlist) { 18218 tail->b_prev = bp->b_prev; 18219 tail->b_next = bp->b_next; 18220 if (tcp->tcp_xmit_tail == bp) 18221 tcp->tcp_xmit_tail = tail; 18222 } 18223 bp->b_next = NULL; 18224 bp->b_prev = NULL; 18225 freeb(bp); 18226 } else { 18227 tail->b_cont = nbp; 18228 tail = nbp; 18229 nbp = nbp->b_cont; 18230 } 18231 } 18232 if (fix_xmitlist) { 18233 tcp->tcp_xmit_last = tail; 18234 tcp->tcp_xmit_zc_clean = B_TRUE; 18235 } 18236 return (head); 18237 } 18238 18239 static void 18240 tcp_zcopy_notify(tcp_t *tcp) 18241 { 18242 struct stdata *stp; 18243 conn_t *connp; 18244 18245 if (tcp->tcp_detached) 18246 return; 18247 connp = tcp->tcp_connp; 18248 if (IPCL_IS_NONSTR(connp)) { 18249 (*connp->conn_upcalls->su_zcopy_notify) 18250 (connp->conn_upper_handle); 18251 return; 18252 } 18253 stp = STREAM(tcp->tcp_rq); 18254 mutex_enter(&stp->sd_lock); 18255 stp->sd_flag |= STZCNOTIFY; 18256 cv_broadcast(&stp->sd_zcopy_wait); 18257 mutex_exit(&stp->sd_lock); 18258 } 18259 18260 static boolean_t 18261 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18262 { 18263 ire_t *ire; 18264 conn_t *connp = tcp->tcp_connp; 18265 tcp_stack_t *tcps = tcp->tcp_tcps; 18266 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18267 18268 mutex_enter(&connp->conn_lock); 18269 ire = connp->conn_ire_cache; 18270 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18271 18272 if ((ire != NULL) && 18273 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18274 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18275 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18276 IRE_REFHOLD(ire); 18277 mutex_exit(&connp->conn_lock); 18278 } else { 18279 boolean_t cached = B_FALSE; 18280 ts_label_t *tsl; 18281 18282 /* force a recheck later on */ 18283 tcp->tcp_ire_ill_check_done = B_FALSE; 18284 18285 TCP_DBGSTAT(tcps, tcp_ire_null1); 18286 connp->conn_ire_cache = NULL; 18287 mutex_exit(&connp->conn_lock); 18288 18289 if (ire != NULL) 18290 IRE_REFRELE_NOTR(ire); 18291 18292 tsl = crgetlabel(CONN_CRED(connp)); 18293 ire = (dst ? 18294 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18295 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18296 connp->conn_zoneid, tsl, ipst)); 18297 18298 if (ire == NULL) { 18299 TCP_STAT(tcps, tcp_ire_null); 18300 return (B_FALSE); 18301 } 18302 18303 IRE_REFHOLD_NOTR(ire); 18304 18305 mutex_enter(&connp->conn_lock); 18306 if (CONN_CACHE_IRE(connp)) { 18307 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18308 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18309 TCP_CHECK_IREINFO(tcp, ire); 18310 connp->conn_ire_cache = ire; 18311 cached = B_TRUE; 18312 } 18313 rw_exit(&ire->ire_bucket->irb_lock); 18314 } 18315 mutex_exit(&connp->conn_lock); 18316 18317 /* 18318 * We can continue to use the ire but since it was 18319 * not cached, we should drop the extra reference. 18320 */ 18321 if (!cached) 18322 IRE_REFRELE_NOTR(ire); 18323 18324 /* 18325 * Rampart note: no need to select a new label here, since 18326 * labels are not allowed to change during the life of a TCP 18327 * connection. 18328 */ 18329 } 18330 18331 *irep = ire; 18332 18333 return (B_TRUE); 18334 } 18335 18336 /* 18337 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18338 * 18339 * 0 = success; 18340 * 1 = failed to find ire and ill. 18341 */ 18342 static boolean_t 18343 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18344 { 18345 ipha_t *ipha; 18346 ipaddr_t dst; 18347 ire_t *ire; 18348 ill_t *ill; 18349 mblk_t *ire_fp_mp; 18350 tcp_stack_t *tcps = tcp->tcp_tcps; 18351 18352 if (mp != NULL) 18353 ipha = (ipha_t *)mp->b_rptr; 18354 else 18355 ipha = tcp->tcp_ipha; 18356 dst = ipha->ipha_dst; 18357 18358 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18359 return (B_FALSE); 18360 18361 if ((ire->ire_flags & RTF_MULTIRT) || 18362 (ire->ire_stq == NULL) || 18363 (ire->ire_nce == NULL) || 18364 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18365 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18366 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18367 TCP_STAT(tcps, tcp_ip_ire_send); 18368 IRE_REFRELE(ire); 18369 return (B_FALSE); 18370 } 18371 18372 ill = ire_to_ill(ire); 18373 ASSERT(ill != NULL); 18374 18375 if (!tcp->tcp_ire_ill_check_done) { 18376 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18377 tcp->tcp_ire_ill_check_done = B_TRUE; 18378 } 18379 18380 *irep = ire; 18381 *illp = ill; 18382 18383 return (B_TRUE); 18384 } 18385 18386 static void 18387 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18388 { 18389 ipha_t *ipha; 18390 ipaddr_t src; 18391 ipaddr_t dst; 18392 uint32_t cksum; 18393 ire_t *ire; 18394 uint16_t *up; 18395 ill_t *ill; 18396 conn_t *connp = tcp->tcp_connp; 18397 uint32_t hcksum_txflags = 0; 18398 mblk_t *ire_fp_mp; 18399 uint_t ire_fp_mp_len; 18400 tcp_stack_t *tcps = tcp->tcp_tcps; 18401 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18402 cred_t *cr; 18403 pid_t cpid; 18404 18405 ASSERT(DB_TYPE(mp) == M_DATA); 18406 18407 /* 18408 * Here we need to handle the overloading of the cred_t for 18409 * both getpeerucred and TX. 18410 * If this is a SYN then the caller already set db_credp so 18411 * that getpeerucred will work. But if TX is in use we might have 18412 * a conn_peercred which is different, and we need to use that cred 18413 * to make TX use the correct label and label dependent route. 18414 */ 18415 if (is_system_labeled()) { 18416 cr = msg_getcred(mp, &cpid); 18417 if (cr == NULL || connp->conn_peercred != NULL) 18418 mblk_setcred(mp, CONN_CRED(connp), cpid); 18419 } 18420 18421 ipha = (ipha_t *)mp->b_rptr; 18422 src = ipha->ipha_src; 18423 dst = ipha->ipha_dst; 18424 18425 ASSERT(q != NULL); 18426 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 18427 18428 /* 18429 * Drop off fast path for IPv6 and also if options are present or 18430 * we need to resolve a TS label. 18431 */ 18432 if (tcp->tcp_ipversion != IPV4_VERSION || 18433 !IPCL_IS_CONNECTED(connp) || 18434 !CONN_IS_LSO_MD_FASTPATH(connp) || 18435 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18436 !connp->conn_ulp_labeled || 18437 ipha->ipha_ident == IP_HDR_INCLUDED || 18438 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18439 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18440 if (tcp->tcp_snd_zcopy_aware) 18441 mp = tcp_zcopy_disable(tcp, mp); 18442 TCP_STAT(tcps, tcp_ip_send); 18443 CALL_IP_WPUT(connp, q, mp); 18444 return; 18445 } 18446 18447 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18448 if (tcp->tcp_snd_zcopy_aware) 18449 mp = tcp_zcopy_backoff(tcp, mp, 0); 18450 CALL_IP_WPUT(connp, q, mp); 18451 return; 18452 } 18453 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18454 ire_fp_mp_len = MBLKL(ire_fp_mp); 18455 18456 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18457 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18458 #ifndef _BIG_ENDIAN 18459 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18460 #endif 18461 18462 /* 18463 * Check to see if we need to re-enable LSO/MDT for this connection 18464 * because it was previously disabled due to changes in the ill; 18465 * note that by doing it here, this re-enabling only applies when 18466 * the packet is not dispatched through CALL_IP_WPUT(). 18467 * 18468 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18469 * case, since that's how we ended up here. For IPv6, we do the 18470 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18471 */ 18472 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18473 /* 18474 * Restore LSO for this connection, so that next time around 18475 * it is eligible to go through tcp_lsosend() path again. 18476 */ 18477 TCP_STAT(tcps, tcp_lso_enabled); 18478 tcp->tcp_lso = B_TRUE; 18479 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18480 "interface %s\n", (void *)connp, ill->ill_name)); 18481 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18482 /* 18483 * Restore MDT for this connection, so that next time around 18484 * it is eligible to go through tcp_multisend() path again. 18485 */ 18486 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18487 tcp->tcp_mdt = B_TRUE; 18488 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18489 "interface %s\n", (void *)connp, ill->ill_name)); 18490 } 18491 18492 if (tcp->tcp_snd_zcopy_aware) { 18493 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18494 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18495 mp = tcp_zcopy_disable(tcp, mp); 18496 /* 18497 * we shouldn't need to reset ipha as the mp containing 18498 * ipha should never be a zero-copy mp. 18499 */ 18500 } 18501 18502 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18503 ASSERT(ill->ill_hcksum_capab != NULL); 18504 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18505 } 18506 18507 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18508 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18509 18510 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18511 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18512 18513 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18514 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18515 18516 /* Software checksum? */ 18517 if (DB_CKSUMFLAGS(mp) == 0) { 18518 TCP_STAT(tcps, tcp_out_sw_cksum); 18519 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18520 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18521 } 18522 18523 /* Calculate IP header checksum if hardware isn't capable */ 18524 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18525 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18526 ((uint16_t *)ipha)[4]); 18527 } 18528 18529 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18530 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18531 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18532 18533 UPDATE_OB_PKT_COUNT(ire); 18534 ire->ire_last_used_time = lbolt; 18535 18536 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 18537 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 18538 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 18539 ntohs(ipha->ipha_length)); 18540 18541 DTRACE_PROBE4(ip4__physical__out__start, 18542 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 18543 FW_HOOKS(ipst->ips_ip4_physical_out_event, 18544 ipst->ips_ipv4firewall_physical_out, 18545 NULL, ill, ipha, mp, mp, 0, ipst); 18546 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 18547 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 18548 18549 if (mp != NULL) { 18550 if (ipst->ips_ipobs_enabled) { 18551 zoneid_t szone; 18552 18553 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 18554 ipst, ALL_ZONES); 18555 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 18556 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 18557 } 18558 18559 ILL_SEND_TX(ill, ire, connp, mp, 0, NULL); 18560 } 18561 18562 IRE_REFRELE(ire); 18563 } 18564 18565 /* 18566 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18567 * if the receiver shrinks the window, i.e. moves the right window to the 18568 * left, the we should not send new data, but should retransmit normally the 18569 * old unacked data between suna and suna + swnd. We might has sent data 18570 * that is now outside the new window, pretend that we didn't send it. 18571 */ 18572 static void 18573 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18574 { 18575 uint32_t snxt = tcp->tcp_snxt; 18576 mblk_t *xmit_tail; 18577 int32_t offset; 18578 18579 ASSERT(shrunk_count > 0); 18580 18581 /* Pretend we didn't send the data outside the window */ 18582 snxt -= shrunk_count; 18583 18584 /* Get the mblk and the offset in it per the shrunk window */ 18585 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 18586 18587 ASSERT(xmit_tail != NULL); 18588 18589 /* Reset all the values per the now shrunk window */ 18590 tcp->tcp_snxt = snxt; 18591 tcp->tcp_xmit_tail = xmit_tail; 18592 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 18593 offset; 18594 tcp->tcp_unsent += shrunk_count; 18595 18596 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18597 /* 18598 * Make sure the timer is running so that we will probe a zero 18599 * window. 18600 */ 18601 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18602 } 18603 18604 18605 /* 18606 * The TCP normal data output path. 18607 * NOTE: the logic of the fast path is duplicated from this function. 18608 */ 18609 static void 18610 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18611 { 18612 int len; 18613 mblk_t *local_time; 18614 mblk_t *mp1; 18615 uint32_t snxt; 18616 int tail_unsent; 18617 int tcpstate; 18618 int usable = 0; 18619 mblk_t *xmit_tail; 18620 queue_t *q = tcp->tcp_wq; 18621 int32_t mss; 18622 int32_t num_sack_blk = 0; 18623 int32_t tcp_hdr_len; 18624 int32_t tcp_tcp_hdr_len; 18625 int mdt_thres; 18626 int rc; 18627 tcp_stack_t *tcps = tcp->tcp_tcps; 18628 ip_stack_t *ipst; 18629 18630 tcpstate = tcp->tcp_state; 18631 if (mp == NULL) { 18632 /* 18633 * tcp_wput_data() with NULL mp should only be called when 18634 * there is unsent data. 18635 */ 18636 ASSERT(tcp->tcp_unsent > 0); 18637 /* Really tacky... but we need this for detached closes. */ 18638 len = tcp->tcp_unsent; 18639 goto data_null; 18640 } 18641 18642 #if CCS_STATS 18643 wrw_stats.tot.count++; 18644 wrw_stats.tot.bytes += msgdsize(mp); 18645 #endif 18646 ASSERT(mp->b_datap->db_type == M_DATA); 18647 /* 18648 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18649 * or before a connection attempt has begun. 18650 */ 18651 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18652 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18653 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18654 #ifdef DEBUG 18655 cmn_err(CE_WARN, 18656 "tcp_wput_data: data after ordrel, %s", 18657 tcp_display(tcp, NULL, 18658 DISP_ADDR_AND_PORT)); 18659 #else 18660 if (tcp->tcp_debug) { 18661 (void) strlog(TCP_MOD_ID, 0, 1, 18662 SL_TRACE|SL_ERROR, 18663 "tcp_wput_data: data after ordrel, %s\n", 18664 tcp_display(tcp, NULL, 18665 DISP_ADDR_AND_PORT)); 18666 } 18667 #endif /* DEBUG */ 18668 } 18669 if (tcp->tcp_snd_zcopy_aware && 18670 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18671 tcp_zcopy_notify(tcp); 18672 freemsg(mp); 18673 mutex_enter(&tcp->tcp_non_sq_lock); 18674 if (tcp->tcp_flow_stopped && 18675 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18676 tcp_clrqfull(tcp); 18677 } 18678 mutex_exit(&tcp->tcp_non_sq_lock); 18679 return; 18680 } 18681 18682 /* Strip empties */ 18683 for (;;) { 18684 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18685 (uintptr_t)INT_MAX); 18686 len = (int)(mp->b_wptr - mp->b_rptr); 18687 if (len > 0) 18688 break; 18689 mp1 = mp; 18690 mp = mp->b_cont; 18691 freeb(mp1); 18692 if (!mp) { 18693 return; 18694 } 18695 } 18696 18697 /* If we are the first on the list ... */ 18698 if (tcp->tcp_xmit_head == NULL) { 18699 tcp->tcp_xmit_head = mp; 18700 tcp->tcp_xmit_tail = mp; 18701 tcp->tcp_xmit_tail_unsent = len; 18702 } else { 18703 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18704 struct datab *dp; 18705 18706 mp1 = tcp->tcp_xmit_last; 18707 if (len < tcp_tx_pull_len && 18708 (dp = mp1->b_datap)->db_ref == 1 && 18709 dp->db_lim - mp1->b_wptr >= len) { 18710 ASSERT(len > 0); 18711 ASSERT(!mp1->b_cont); 18712 if (len == 1) { 18713 *mp1->b_wptr++ = *mp->b_rptr; 18714 } else { 18715 bcopy(mp->b_rptr, mp1->b_wptr, len); 18716 mp1->b_wptr += len; 18717 } 18718 if (mp1 == tcp->tcp_xmit_tail) 18719 tcp->tcp_xmit_tail_unsent += len; 18720 mp1->b_cont = mp->b_cont; 18721 if (tcp->tcp_snd_zcopy_aware && 18722 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18723 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18724 freeb(mp); 18725 mp = mp1; 18726 } else { 18727 tcp->tcp_xmit_last->b_cont = mp; 18728 } 18729 len += tcp->tcp_unsent; 18730 } 18731 18732 /* Tack on however many more positive length mblks we have */ 18733 if ((mp1 = mp->b_cont) != NULL) { 18734 do { 18735 int tlen; 18736 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18737 (uintptr_t)INT_MAX); 18738 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18739 if (tlen <= 0) { 18740 mp->b_cont = mp1->b_cont; 18741 freeb(mp1); 18742 } else { 18743 len += tlen; 18744 mp = mp1; 18745 } 18746 } while ((mp1 = mp->b_cont) != NULL); 18747 } 18748 tcp->tcp_xmit_last = mp; 18749 tcp->tcp_unsent = len; 18750 18751 if (urgent) 18752 usable = 1; 18753 18754 data_null: 18755 snxt = tcp->tcp_snxt; 18756 xmit_tail = tcp->tcp_xmit_tail; 18757 tail_unsent = tcp->tcp_xmit_tail_unsent; 18758 18759 /* 18760 * Note that tcp_mss has been adjusted to take into account the 18761 * timestamp option if applicable. Because SACK options do not 18762 * appear in every TCP segments and they are of variable lengths, 18763 * they cannot be included in tcp_mss. Thus we need to calculate 18764 * the actual segment length when we need to send a segment which 18765 * includes SACK options. 18766 */ 18767 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 18768 int32_t opt_len; 18769 18770 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 18771 tcp->tcp_num_sack_blk); 18772 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 18773 2 + TCPOPT_HEADER_LEN; 18774 mss = tcp->tcp_mss - opt_len; 18775 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 18776 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 18777 } else { 18778 mss = tcp->tcp_mss; 18779 tcp_hdr_len = tcp->tcp_hdr_len; 18780 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 18781 } 18782 18783 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18784 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18785 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 18786 } 18787 if (tcpstate == TCPS_SYN_RCVD) { 18788 /* 18789 * The three-way connection establishment handshake is not 18790 * complete yet. We want to queue the data for transmission 18791 * after entering ESTABLISHED state (RFC793). A jump to 18792 * "done" label effectively leaves data on the queue. 18793 */ 18794 goto done; 18795 } else { 18796 int usable_r; 18797 18798 /* 18799 * In the special case when cwnd is zero, which can only 18800 * happen if the connection is ECN capable, return now. 18801 * New segments is sent using tcp_timer(). The timer 18802 * is set in tcp_rput_data(). 18803 */ 18804 if (tcp->tcp_cwnd == 0) { 18805 /* 18806 * Note that tcp_cwnd is 0 before 3-way handshake is 18807 * finished. 18808 */ 18809 ASSERT(tcp->tcp_ecn_ok || 18810 tcp->tcp_state < TCPS_ESTABLISHED); 18811 return; 18812 } 18813 18814 /* NOTE: trouble if xmitting while SYN not acked? */ 18815 usable_r = snxt - tcp->tcp_suna; 18816 usable_r = tcp->tcp_swnd - usable_r; 18817 18818 /* 18819 * Check if the receiver has shrunk the window. If 18820 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 18821 * cannot be set as there is unsent data, so FIN cannot 18822 * be sent out. Otherwise, we need to take into account 18823 * of FIN as it consumes an "invisible" sequence number. 18824 */ 18825 ASSERT(tcp->tcp_fin_sent == 0); 18826 if (usable_r < 0) { 18827 /* 18828 * The receiver has shrunk the window and we have sent 18829 * -usable_r date beyond the window, re-adjust. 18830 * 18831 * If TCP window scaling is enabled, there can be 18832 * round down error as the advertised receive window 18833 * is actually right shifted n bits. This means that 18834 * the lower n bits info is wiped out. It will look 18835 * like the window is shrunk. Do a check here to 18836 * see if the shrunk amount is actually within the 18837 * error in window calculation. If it is, just 18838 * return. Note that this check is inside the 18839 * shrunk window check. This makes sure that even 18840 * though tcp_process_shrunk_swnd() is not called, 18841 * we will stop further processing. 18842 */ 18843 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 18844 tcp_process_shrunk_swnd(tcp, -usable_r); 18845 } 18846 return; 18847 } 18848 18849 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 18850 if (tcp->tcp_swnd > tcp->tcp_cwnd) 18851 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 18852 18853 /* usable = MIN(usable, unsent) */ 18854 if (usable_r > len) 18855 usable_r = len; 18856 18857 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 18858 if (usable_r > 0) { 18859 usable = usable_r; 18860 } else { 18861 /* Bypass all other unnecessary processing. */ 18862 goto done; 18863 } 18864 } 18865 18866 local_time = (mblk_t *)lbolt; 18867 18868 /* 18869 * "Our" Nagle Algorithm. This is not the same as in the old 18870 * BSD. This is more in line with the true intent of Nagle. 18871 * 18872 * The conditions are: 18873 * 1. The amount of unsent data (or amount of data which can be 18874 * sent, whichever is smaller) is less than Nagle limit. 18875 * 2. The last sent size is also less than Nagle limit. 18876 * 3. There is unack'ed data. 18877 * 4. Urgent pointer is not set. Send urgent data ignoring the 18878 * Nagle algorithm. This reduces the probability that urgent 18879 * bytes get "merged" together. 18880 * 5. The app has not closed the connection. This eliminates the 18881 * wait time of the receiving side waiting for the last piece of 18882 * (small) data. 18883 * 18884 * If all are satisified, exit without sending anything. Note 18885 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 18886 * the smaller of 1 MSS and global tcp_naglim_def (default to be 18887 * 4095). 18888 */ 18889 if (usable < (int)tcp->tcp_naglim && 18890 tcp->tcp_naglim > tcp->tcp_last_sent_len && 18891 snxt != tcp->tcp_suna && 18892 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 18893 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 18894 goto done; 18895 } 18896 18897 if (tcp->tcp_cork) { 18898 /* 18899 * if the tcp->tcp_cork option is set, then we have to force 18900 * TCP not to send partial segment (smaller than MSS bytes). 18901 * We are calculating the usable now based on full mss and 18902 * will save the rest of remaining data for later. 18903 */ 18904 if (usable < mss) 18905 goto done; 18906 usable = (usable / mss) * mss; 18907 } 18908 18909 /* Update the latest receive window size in TCP header. */ 18910 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18911 tcp->tcp_tcph->th_win); 18912 18913 /* 18914 * Determine if it's worthwhile to attempt LSO or MDT, based on: 18915 * 18916 * 1. Simple TCP/IP{v4,v6} (no options). 18917 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 18918 * 3. If the TCP connection is in ESTABLISHED state. 18919 * 4. The TCP is not detached. 18920 * 18921 * If any of the above conditions have changed during the 18922 * connection, stop using LSO/MDT and restore the stream head 18923 * parameters accordingly. 18924 */ 18925 ipst = tcps->tcps_netstack->netstack_ip; 18926 18927 if ((tcp->tcp_lso || tcp->tcp_mdt) && 18928 ((tcp->tcp_ipversion == IPV4_VERSION && 18929 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 18930 (tcp->tcp_ipversion == IPV6_VERSION && 18931 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 18932 tcp->tcp_state != TCPS_ESTABLISHED || 18933 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 18934 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 18935 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 18936 if (tcp->tcp_lso) { 18937 tcp->tcp_connp->conn_lso_ok = B_FALSE; 18938 tcp->tcp_lso = B_FALSE; 18939 } else { 18940 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 18941 tcp->tcp_mdt = B_FALSE; 18942 } 18943 18944 /* Anything other than detached is considered pathological */ 18945 if (!TCP_IS_DETACHED(tcp)) { 18946 if (tcp->tcp_lso) 18947 TCP_STAT(tcps, tcp_lso_disabled); 18948 else 18949 TCP_STAT(tcps, tcp_mdt_conn_halted1); 18950 (void) tcp_maxpsz_set(tcp, B_TRUE); 18951 } 18952 } 18953 18954 /* Use MDT if sendable amount is greater than the threshold */ 18955 if (tcp->tcp_mdt && 18956 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 18957 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 18958 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 18959 (tcp->tcp_valid_bits == 0 || 18960 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 18961 ASSERT(tcp->tcp_connp->conn_mdt_ok); 18962 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18963 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18964 local_time, mdt_thres); 18965 } else { 18966 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18967 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18968 local_time, INT_MAX); 18969 } 18970 18971 /* Pretend that all we were trying to send really got sent */ 18972 if (rc < 0 && tail_unsent < 0) { 18973 do { 18974 xmit_tail = xmit_tail->b_cont; 18975 xmit_tail->b_prev = local_time; 18976 ASSERT((uintptr_t)(xmit_tail->b_wptr - 18977 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 18978 tail_unsent += (int)(xmit_tail->b_wptr - 18979 xmit_tail->b_rptr); 18980 } while (tail_unsent < 0); 18981 } 18982 done:; 18983 tcp->tcp_xmit_tail = xmit_tail; 18984 tcp->tcp_xmit_tail_unsent = tail_unsent; 18985 len = tcp->tcp_snxt - snxt; 18986 if (len) { 18987 /* 18988 * If new data was sent, need to update the notsack 18989 * list, which is, afterall, data blocks that have 18990 * not been sack'ed by the receiver. New data is 18991 * not sack'ed. 18992 */ 18993 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 18994 /* len is a negative value. */ 18995 tcp->tcp_pipe -= len; 18996 tcp_notsack_update(&(tcp->tcp_notsack_list), 18997 tcp->tcp_snxt, snxt, 18998 &(tcp->tcp_num_notsack_blk), 18999 &(tcp->tcp_cnt_notsack_list)); 19000 } 19001 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19002 tcp->tcp_rack = tcp->tcp_rnxt; 19003 tcp->tcp_rack_cnt = 0; 19004 if ((snxt + len) == tcp->tcp_suna) { 19005 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19006 } 19007 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19008 /* 19009 * Didn't send anything. Make sure the timer is running 19010 * so that we will probe a zero window. 19011 */ 19012 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19013 } 19014 /* Note that len is the amount we just sent but with a negative sign */ 19015 tcp->tcp_unsent += len; 19016 mutex_enter(&tcp->tcp_non_sq_lock); 19017 if (tcp->tcp_flow_stopped) { 19018 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19019 tcp_clrqfull(tcp); 19020 } 19021 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19022 tcp_setqfull(tcp); 19023 } 19024 mutex_exit(&tcp->tcp_non_sq_lock); 19025 } 19026 19027 /* 19028 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19029 * outgoing TCP header with the template header, as well as other 19030 * options such as time-stamp, ECN and/or SACK. 19031 */ 19032 static void 19033 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19034 { 19035 tcph_t *tcp_tmpl, *tcp_h; 19036 uint32_t *dst, *src; 19037 int hdrlen; 19038 19039 ASSERT(OK_32PTR(rptr)); 19040 19041 /* Template header */ 19042 tcp_tmpl = tcp->tcp_tcph; 19043 19044 /* Header of outgoing packet */ 19045 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19046 19047 /* dst and src are opaque 32-bit fields, used for copying */ 19048 dst = (uint32_t *)rptr; 19049 src = (uint32_t *)tcp->tcp_iphc; 19050 hdrlen = tcp->tcp_hdr_len; 19051 19052 /* Fill time-stamp option if needed */ 19053 if (tcp->tcp_snd_ts_ok) { 19054 U32_TO_BE32((uint32_t)now, 19055 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19056 U32_TO_BE32(tcp->tcp_ts_recent, 19057 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19058 } else { 19059 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19060 } 19061 19062 /* 19063 * Copy the template header; is this really more efficient than 19064 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19065 * but perhaps not for other scenarios. 19066 */ 19067 dst[0] = src[0]; 19068 dst[1] = src[1]; 19069 dst[2] = src[2]; 19070 dst[3] = src[3]; 19071 dst[4] = src[4]; 19072 dst[5] = src[5]; 19073 dst[6] = src[6]; 19074 dst[7] = src[7]; 19075 dst[8] = src[8]; 19076 dst[9] = src[9]; 19077 if (hdrlen -= 40) { 19078 hdrlen >>= 2; 19079 dst += 10; 19080 src += 10; 19081 do { 19082 *dst++ = *src++; 19083 } while (--hdrlen); 19084 } 19085 19086 /* 19087 * Set the ECN info in the TCP header if it is not a zero 19088 * window probe. Zero window probe is only sent in 19089 * tcp_wput_data() and tcp_timer(). 19090 */ 19091 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19092 SET_ECT(tcp, rptr); 19093 19094 if (tcp->tcp_ecn_echo_on) 19095 tcp_h->th_flags[0] |= TH_ECE; 19096 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19097 tcp_h->th_flags[0] |= TH_CWR; 19098 tcp->tcp_ecn_cwr_sent = B_TRUE; 19099 } 19100 } 19101 19102 /* Fill in SACK options */ 19103 if (num_sack_blk > 0) { 19104 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19105 sack_blk_t *tmp; 19106 int32_t i; 19107 19108 wptr[0] = TCPOPT_NOP; 19109 wptr[1] = TCPOPT_NOP; 19110 wptr[2] = TCPOPT_SACK; 19111 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19112 sizeof (sack_blk_t); 19113 wptr += TCPOPT_REAL_SACK_LEN; 19114 19115 tmp = tcp->tcp_sack_list; 19116 for (i = 0; i < num_sack_blk; i++) { 19117 U32_TO_BE32(tmp[i].begin, wptr); 19118 wptr += sizeof (tcp_seq); 19119 U32_TO_BE32(tmp[i].end, wptr); 19120 wptr += sizeof (tcp_seq); 19121 } 19122 tcp_h->th_offset_and_rsrvd[0] += 19123 ((num_sack_blk * 2 + 1) << 4); 19124 } 19125 } 19126 19127 /* 19128 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19129 * the destination address and SAP attribute, and if necessary, the 19130 * hardware checksum offload attribute to a Multidata message. 19131 */ 19132 static int 19133 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19134 const uint32_t start, const uint32_t stuff, const uint32_t end, 19135 const uint32_t flags, tcp_stack_t *tcps) 19136 { 19137 /* Add global destination address & SAP attribute */ 19138 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19139 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19140 "destination address+SAP\n")); 19141 19142 if (dlmp != NULL) 19143 TCP_STAT(tcps, tcp_mdt_allocfail); 19144 return (-1); 19145 } 19146 19147 /* Add global hwcksum attribute */ 19148 if (hwcksum && 19149 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19150 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19151 "checksum attribute\n")); 19152 19153 TCP_STAT(tcps, tcp_mdt_allocfail); 19154 return (-1); 19155 } 19156 19157 return (0); 19158 } 19159 19160 /* 19161 * Smaller and private version of pdescinfo_t used specifically for TCP, 19162 * which allows for only two payload spans per packet. 19163 */ 19164 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19165 19166 /* 19167 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19168 * scheme, and returns one the following: 19169 * 19170 * -1 = failed allocation. 19171 * 0 = success; burst count reached, or usable send window is too small, 19172 * and that we'd rather wait until later before sending again. 19173 */ 19174 static int 19175 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19176 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19177 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19178 const int mdt_thres) 19179 { 19180 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19181 multidata_t *mmd; 19182 uint_t obsegs, obbytes, hdr_frag_sz; 19183 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19184 int num_burst_seg, max_pld; 19185 pdesc_t *pkt; 19186 tcp_pdescinfo_t tcp_pkt_info; 19187 pdescinfo_t *pkt_info; 19188 int pbuf_idx, pbuf_idx_nxt; 19189 int seg_len, len, spill, af; 19190 boolean_t add_buffer, zcopy, clusterwide; 19191 boolean_t rconfirm = B_FALSE; 19192 boolean_t done = B_FALSE; 19193 uint32_t cksum; 19194 uint32_t hwcksum_flags; 19195 ire_t *ire = NULL; 19196 ill_t *ill; 19197 ipha_t *ipha; 19198 ip6_t *ip6h; 19199 ipaddr_t src, dst; 19200 ill_zerocopy_capab_t *zc_cap = NULL; 19201 uint16_t *up; 19202 int err; 19203 conn_t *connp; 19204 tcp_stack_t *tcps = tcp->tcp_tcps; 19205 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19206 int usable_mmd, tail_unsent_mmd; 19207 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 19208 mblk_t *xmit_tail_mmd; 19209 netstackid_t stack_id; 19210 19211 #ifdef _BIG_ENDIAN 19212 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19213 #else 19214 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19215 #endif 19216 19217 #define PREP_NEW_MULTIDATA() { \ 19218 mmd = NULL; \ 19219 md_mp = md_hbuf = NULL; \ 19220 cur_hdr_off = 0; \ 19221 max_pld = tcp->tcp_mdt_max_pld; \ 19222 pbuf_idx = pbuf_idx_nxt = -1; \ 19223 add_buffer = B_TRUE; \ 19224 zcopy = B_FALSE; \ 19225 } 19226 19227 #define PREP_NEW_PBUF() { \ 19228 md_pbuf = md_pbuf_nxt = NULL; \ 19229 pbuf_idx = pbuf_idx_nxt = -1; \ 19230 cur_pld_off = 0; \ 19231 first_snxt = *snxt; \ 19232 ASSERT(*tail_unsent > 0); \ 19233 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19234 } 19235 19236 ASSERT(mdt_thres >= mss); 19237 ASSERT(*usable > 0 && *usable > mdt_thres); 19238 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19239 ASSERT(!TCP_IS_DETACHED(tcp)); 19240 ASSERT(tcp->tcp_valid_bits == 0 || 19241 tcp->tcp_valid_bits == TCP_FSS_VALID); 19242 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19243 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19244 (tcp->tcp_ipversion == IPV6_VERSION && 19245 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19246 19247 connp = tcp->tcp_connp; 19248 ASSERT(connp != NULL); 19249 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19250 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19251 19252 stack_id = connp->conn_netstack->netstack_stackid; 19253 19254 usable_mmd = tail_unsent_mmd = 0; 19255 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 19256 xmit_tail_mmd = NULL; 19257 /* 19258 * Note that tcp will only declare at most 2 payload spans per 19259 * packet, which is much lower than the maximum allowable number 19260 * of packet spans per Multidata. For this reason, we use the 19261 * privately declared and smaller descriptor info structure, in 19262 * order to save some stack space. 19263 */ 19264 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19265 19266 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19267 if (af == AF_INET) { 19268 dst = tcp->tcp_ipha->ipha_dst; 19269 src = tcp->tcp_ipha->ipha_src; 19270 ASSERT(!CLASSD(dst)); 19271 } 19272 ASSERT(af == AF_INET || 19273 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19274 19275 obsegs = obbytes = 0; 19276 num_burst_seg = tcp->tcp_snd_burst; 19277 md_mp_head = NULL; 19278 PREP_NEW_MULTIDATA(); 19279 19280 /* 19281 * Before we go on further, make sure there is an IRE that we can 19282 * use, and that the ILL supports MDT. Otherwise, there's no point 19283 * in proceeding any further, and we should just hand everything 19284 * off to the legacy path. 19285 */ 19286 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19287 goto legacy_send_no_md; 19288 19289 ASSERT(ire != NULL); 19290 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19291 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19292 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19293 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19294 /* 19295 * If we do support loopback for MDT (which requires modifications 19296 * to the receiving paths), the following assertions should go away, 19297 * and we would be sending the Multidata to loopback conn later on. 19298 */ 19299 ASSERT(!IRE_IS_LOCAL(ire)); 19300 ASSERT(ire->ire_stq != NULL); 19301 19302 ill = ire_to_ill(ire); 19303 ASSERT(ill != NULL); 19304 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19305 19306 if (!tcp->tcp_ire_ill_check_done) { 19307 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19308 tcp->tcp_ire_ill_check_done = B_TRUE; 19309 } 19310 19311 /* 19312 * If the underlying interface conditions have changed, or if the 19313 * new interface does not support MDT, go back to legacy path. 19314 */ 19315 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19316 /* don't go through this path anymore for this connection */ 19317 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19318 tcp->tcp_mdt = B_FALSE; 19319 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19320 "interface %s\n", (void *)connp, ill->ill_name)); 19321 /* IRE will be released prior to returning */ 19322 goto legacy_send_no_md; 19323 } 19324 19325 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19326 zc_cap = ill->ill_zerocopy_capab; 19327 19328 /* 19329 * Check if we can take tcp fast-path. Note that "incomplete" 19330 * ire's (where the link-layer for next hop is not resolved 19331 * or where the fast-path header in nce_fp_mp is not available 19332 * yet) are sent down the legacy (slow) path. 19333 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19334 */ 19335 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19336 /* IRE will be released prior to returning */ 19337 goto legacy_send_no_md; 19338 } 19339 19340 /* go to legacy path if interface doesn't support zerocopy */ 19341 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19342 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19343 /* IRE will be released prior to returning */ 19344 goto legacy_send_no_md; 19345 } 19346 19347 /* does the interface support hardware checksum offload? */ 19348 hwcksum_flags = 0; 19349 if (ILL_HCKSUM_CAPABLE(ill) && 19350 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19351 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19352 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19353 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19354 HCKSUM_IPHDRCKSUM) 19355 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19356 19357 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19358 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19359 hwcksum_flags |= HCK_FULLCKSUM; 19360 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19361 HCKSUM_INET_PARTIAL) 19362 hwcksum_flags |= HCK_PARTIALCKSUM; 19363 } 19364 19365 /* 19366 * Each header fragment consists of the leading extra space, 19367 * followed by the TCP/IP header, and the trailing extra space. 19368 * We make sure that each header fragment begins on a 32-bit 19369 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19370 * aligned in tcp_mdt_update). 19371 */ 19372 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19373 tcp->tcp_mdt_hdr_tail), 4); 19374 19375 /* are we starting from the beginning of data block? */ 19376 if (*tail_unsent == 0) { 19377 *xmit_tail = (*xmit_tail)->b_cont; 19378 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19379 *tail_unsent = (int)MBLKL(*xmit_tail); 19380 } 19381 19382 /* 19383 * Here we create one or more Multidata messages, each made up of 19384 * one header buffer and up to N payload buffers. This entire 19385 * operation is done within two loops: 19386 * 19387 * The outer loop mostly deals with creating the Multidata message, 19388 * as well as the header buffer that gets added to it. It also 19389 * links the Multidata messages together such that all of them can 19390 * be sent down to the lower layer in a single putnext call; this 19391 * linking behavior depends on the tcp_mdt_chain tunable. 19392 * 19393 * The inner loop takes an existing Multidata message, and adds 19394 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19395 * packetizes those buffers by filling up the corresponding header 19396 * buffer fragments with the proper IP and TCP headers, and by 19397 * describing the layout of each packet in the packet descriptors 19398 * that get added to the Multidata. 19399 */ 19400 do { 19401 /* 19402 * If usable send window is too small, or data blocks in 19403 * transmit list are smaller than our threshold (i.e. app 19404 * performs large writes followed by small ones), we hand 19405 * off the control over to the legacy path. Note that we'll 19406 * get back the control once it encounters a large block. 19407 */ 19408 if (*usable < mss || (*tail_unsent <= mdt_thres && 19409 (*xmit_tail)->b_cont != NULL && 19410 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19411 /* send down what we've got so far */ 19412 if (md_mp_head != NULL) { 19413 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19414 obsegs, obbytes, &rconfirm); 19415 } 19416 /* 19417 * Pass control over to tcp_send(), but tell it to 19418 * return to us once a large-size transmission is 19419 * possible. 19420 */ 19421 TCP_STAT(tcps, tcp_mdt_legacy_small); 19422 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19423 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19424 tail_unsent, xmit_tail, local_time, 19425 mdt_thres)) <= 0) { 19426 /* burst count reached, or alloc failed */ 19427 IRE_REFRELE(ire); 19428 return (err); 19429 } 19430 19431 /* tcp_send() may have sent everything, so check */ 19432 if (*usable <= 0) { 19433 IRE_REFRELE(ire); 19434 return (0); 19435 } 19436 19437 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19438 /* 19439 * We may have delivered the Multidata, so make sure 19440 * to re-initialize before the next round. 19441 */ 19442 md_mp_head = NULL; 19443 obsegs = obbytes = 0; 19444 num_burst_seg = tcp->tcp_snd_burst; 19445 PREP_NEW_MULTIDATA(); 19446 19447 /* are we starting from the beginning of data block? */ 19448 if (*tail_unsent == 0) { 19449 *xmit_tail = (*xmit_tail)->b_cont; 19450 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19451 (uintptr_t)INT_MAX); 19452 *tail_unsent = (int)MBLKL(*xmit_tail); 19453 } 19454 } 19455 /* 19456 * Record current values for parameters we may need to pass 19457 * to tcp_send() or tcp_multisend_data(). We checkpoint at 19458 * each iteration of the outer loop (each multidata message 19459 * creation). If we have a failure in the inner loop, we send 19460 * any complete multidata messages we have before reverting 19461 * to using the traditional non-md path. 19462 */ 19463 snxt_mmd = *snxt; 19464 usable_mmd = *usable; 19465 xmit_tail_mmd = *xmit_tail; 19466 tail_unsent_mmd = *tail_unsent; 19467 obsegs_mmd = obsegs; 19468 obbytes_mmd = obbytes; 19469 19470 /* 19471 * max_pld limits the number of mblks in tcp's transmit 19472 * queue that can be added to a Multidata message. Once 19473 * this counter reaches zero, no more additional mblks 19474 * can be added to it. What happens afterwards depends 19475 * on whether or not we are set to chain the Multidata 19476 * messages. If we are to link them together, reset 19477 * max_pld to its original value (tcp_mdt_max_pld) and 19478 * prepare to create a new Multidata message which will 19479 * get linked to md_mp_head. Else, leave it alone and 19480 * let the inner loop break on its own. 19481 */ 19482 if (tcp_mdt_chain && max_pld == 0) 19483 PREP_NEW_MULTIDATA(); 19484 19485 /* adding a payload buffer; re-initialize values */ 19486 if (add_buffer) 19487 PREP_NEW_PBUF(); 19488 19489 /* 19490 * If we don't have a Multidata, either because we just 19491 * (re)entered this outer loop, or after we branched off 19492 * to tcp_send above, setup the Multidata and header 19493 * buffer to be used. 19494 */ 19495 if (md_mp == NULL) { 19496 int md_hbuflen; 19497 uint32_t start, stuff; 19498 19499 /* 19500 * Calculate Multidata header buffer size large enough 19501 * to hold all of the headers that can possibly be 19502 * sent at this moment. We'd rather over-estimate 19503 * the size than running out of space; this is okay 19504 * since this buffer is small anyway. 19505 */ 19506 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19507 19508 /* 19509 * Start and stuff offset for partial hardware 19510 * checksum offload; these are currently for IPv4. 19511 * For full checksum offload, they are set to zero. 19512 */ 19513 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19514 if (af == AF_INET) { 19515 start = IP_SIMPLE_HDR_LENGTH; 19516 stuff = IP_SIMPLE_HDR_LENGTH + 19517 TCP_CHECKSUM_OFFSET; 19518 } else { 19519 start = IPV6_HDR_LEN; 19520 stuff = IPV6_HDR_LEN + 19521 TCP_CHECKSUM_OFFSET; 19522 } 19523 } else { 19524 start = stuff = 0; 19525 } 19526 19527 /* 19528 * Create the header buffer, Multidata, as well as 19529 * any necessary attributes (destination address, 19530 * SAP and hardware checksum offload) that should 19531 * be associated with the Multidata message. 19532 */ 19533 ASSERT(cur_hdr_off == 0); 19534 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19535 ((md_hbuf->b_wptr += md_hbuflen), 19536 (mmd = mmd_alloc(md_hbuf, &md_mp, 19537 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19538 /* fastpath mblk */ 19539 ire->ire_nce->nce_res_mp, 19540 /* hardware checksum enabled */ 19541 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19542 /* hardware checksum offsets */ 19543 start, stuff, 0, 19544 /* hardware checksum flag */ 19545 hwcksum_flags, tcps) != 0)) { 19546 legacy_send: 19547 /* 19548 * We arrive here from a failure within the 19549 * inner (packetizer) loop or we fail one of 19550 * the conditionals above. We restore the 19551 * previously checkpointed values for: 19552 * xmit_tail 19553 * usable 19554 * tail_unsent 19555 * snxt 19556 * obbytes 19557 * obsegs 19558 * We should then be able to dispatch any 19559 * complete multidata before reverting to the 19560 * traditional path with consistent parameters 19561 * (the inner loop updates these as it 19562 * iterates). 19563 */ 19564 *xmit_tail = xmit_tail_mmd; 19565 *usable = usable_mmd; 19566 *tail_unsent = tail_unsent_mmd; 19567 *snxt = snxt_mmd; 19568 obbytes = obbytes_mmd; 19569 obsegs = obsegs_mmd; 19570 if (md_mp != NULL) { 19571 /* Unlink message from the chain */ 19572 if (md_mp_head != NULL) { 19573 err = (intptr_t)rmvb(md_mp_head, 19574 md_mp); 19575 /* 19576 * We can't assert that rmvb 19577 * did not return -1, since we 19578 * may get here before linkb 19579 * happens. We do, however, 19580 * check if we just removed the 19581 * only element in the list. 19582 */ 19583 if (err == 0) 19584 md_mp_head = NULL; 19585 } 19586 /* md_hbuf gets freed automatically */ 19587 TCP_STAT(tcps, tcp_mdt_discarded); 19588 freeb(md_mp); 19589 } else { 19590 /* Either allocb or mmd_alloc failed */ 19591 TCP_STAT(tcps, tcp_mdt_allocfail); 19592 if (md_hbuf != NULL) 19593 freeb(md_hbuf); 19594 } 19595 19596 /* send down what we've got so far */ 19597 if (md_mp_head != NULL) { 19598 tcp_multisend_data(tcp, ire, ill, 19599 md_mp_head, obsegs, obbytes, 19600 &rconfirm); 19601 } 19602 legacy_send_no_md: 19603 if (ire != NULL) 19604 IRE_REFRELE(ire); 19605 /* 19606 * Too bad; let the legacy path handle this. 19607 * We specify INT_MAX for the threshold, since 19608 * we gave up with the Multidata processings 19609 * and let the old path have it all. 19610 */ 19611 TCP_STAT(tcps, tcp_mdt_legacy_all); 19612 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19613 tcp_tcp_hdr_len, num_sack_blk, usable, 19614 snxt, tail_unsent, xmit_tail, local_time, 19615 INT_MAX)); 19616 } 19617 19618 /* link to any existing ones, if applicable */ 19619 TCP_STAT(tcps, tcp_mdt_allocd); 19620 if (md_mp_head == NULL) { 19621 md_mp_head = md_mp; 19622 } else if (tcp_mdt_chain) { 19623 TCP_STAT(tcps, tcp_mdt_linked); 19624 linkb(md_mp_head, md_mp); 19625 } 19626 } 19627 19628 ASSERT(md_mp_head != NULL); 19629 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19630 ASSERT(md_mp != NULL && mmd != NULL); 19631 ASSERT(md_hbuf != NULL); 19632 19633 /* 19634 * Packetize the transmittable portion of the data block; 19635 * each data block is essentially added to the Multidata 19636 * as a payload buffer. We also deal with adding more 19637 * than one payload buffers, which happens when the remaining 19638 * packetized portion of the current payload buffer is less 19639 * than MSS, while the next data block in transmit queue 19640 * has enough data to make up for one. This "spillover" 19641 * case essentially creates a split-packet, where portions 19642 * of the packet's payload fragments may span across two 19643 * virtually discontiguous address blocks. 19644 */ 19645 seg_len = mss; 19646 do { 19647 len = seg_len; 19648 19649 /* one must remain NULL for DTRACE_IP_FASTPATH */ 19650 ipha = NULL; 19651 ip6h = NULL; 19652 19653 ASSERT(len > 0); 19654 ASSERT(max_pld >= 0); 19655 ASSERT(!add_buffer || cur_pld_off == 0); 19656 19657 /* 19658 * First time around for this payload buffer; note 19659 * in the case of a spillover, the following has 19660 * been done prior to adding the split-packet 19661 * descriptor to Multidata, and we don't want to 19662 * repeat the process. 19663 */ 19664 if (add_buffer) { 19665 ASSERT(mmd != NULL); 19666 ASSERT(md_pbuf == NULL); 19667 ASSERT(md_pbuf_nxt == NULL); 19668 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19669 19670 /* 19671 * Have we reached the limit? We'd get to 19672 * this case when we're not chaining the 19673 * Multidata messages together, and since 19674 * we're done, terminate this loop. 19675 */ 19676 if (max_pld == 0) 19677 break; /* done */ 19678 19679 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19680 TCP_STAT(tcps, tcp_mdt_allocfail); 19681 goto legacy_send; /* out_of_mem */ 19682 } 19683 19684 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19685 zc_cap != NULL) { 19686 if (!ip_md_zcopy_attr(mmd, NULL, 19687 zc_cap->ill_zerocopy_flags)) { 19688 freeb(md_pbuf); 19689 TCP_STAT(tcps, 19690 tcp_mdt_allocfail); 19691 /* out_of_mem */ 19692 goto legacy_send; 19693 } 19694 zcopy = B_TRUE; 19695 } 19696 19697 md_pbuf->b_rptr += base_pld_off; 19698 19699 /* 19700 * Add a payload buffer to the Multidata; this 19701 * operation must not fail, or otherwise our 19702 * logic in this routine is broken. There 19703 * is no memory allocation done by the 19704 * routine, so any returned failure simply 19705 * tells us that we've done something wrong. 19706 * 19707 * A failure tells us that either we're adding 19708 * the same payload buffer more than once, or 19709 * we're trying to add more buffers than 19710 * allowed (max_pld calculation is wrong). 19711 * None of the above cases should happen, and 19712 * we panic because either there's horrible 19713 * heap corruption, and/or programming mistake. 19714 */ 19715 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19716 if (pbuf_idx < 0) { 19717 cmn_err(CE_PANIC, "tcp_multisend: " 19718 "payload buffer logic error " 19719 "detected for tcp %p mmd %p " 19720 "pbuf %p (%d)\n", 19721 (void *)tcp, (void *)mmd, 19722 (void *)md_pbuf, pbuf_idx); 19723 } 19724 19725 ASSERT(max_pld > 0); 19726 --max_pld; 19727 add_buffer = B_FALSE; 19728 } 19729 19730 ASSERT(md_mp_head != NULL); 19731 ASSERT(md_pbuf != NULL); 19732 ASSERT(md_pbuf_nxt == NULL); 19733 ASSERT(pbuf_idx != -1); 19734 ASSERT(pbuf_idx_nxt == -1); 19735 ASSERT(*usable > 0); 19736 19737 /* 19738 * We spillover to the next payload buffer only 19739 * if all of the following is true: 19740 * 19741 * 1. There is not enough data on the current 19742 * payload buffer to make up `len', 19743 * 2. We are allowed to send `len', 19744 * 3. The next payload buffer length is large 19745 * enough to accomodate `spill'. 19746 */ 19747 if ((spill = len - *tail_unsent) > 0 && 19748 *usable >= len && 19749 MBLKL((*xmit_tail)->b_cont) >= spill && 19750 max_pld > 0) { 19751 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19752 if (md_pbuf_nxt == NULL) { 19753 TCP_STAT(tcps, tcp_mdt_allocfail); 19754 goto legacy_send; /* out_of_mem */ 19755 } 19756 19757 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19758 zc_cap != NULL) { 19759 if (!ip_md_zcopy_attr(mmd, NULL, 19760 zc_cap->ill_zerocopy_flags)) { 19761 freeb(md_pbuf_nxt); 19762 TCP_STAT(tcps, 19763 tcp_mdt_allocfail); 19764 /* out_of_mem */ 19765 goto legacy_send; 19766 } 19767 zcopy = B_TRUE; 19768 } 19769 19770 /* 19771 * See comments above on the first call to 19772 * mmd_addpldbuf for explanation on the panic. 19773 */ 19774 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19775 if (pbuf_idx_nxt < 0) { 19776 panic("tcp_multisend: " 19777 "next payload buffer logic error " 19778 "detected for tcp %p mmd %p " 19779 "pbuf %p (%d)\n", 19780 (void *)tcp, (void *)mmd, 19781 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19782 } 19783 19784 ASSERT(max_pld > 0); 19785 --max_pld; 19786 } else if (spill > 0) { 19787 /* 19788 * If there's a spillover, but the following 19789 * xmit_tail couldn't give us enough octets 19790 * to reach "len", then stop the current 19791 * Multidata creation and let the legacy 19792 * tcp_send() path take over. We don't want 19793 * to send the tiny segment as part of this 19794 * Multidata for performance reasons; instead, 19795 * we let the legacy path deal with grouping 19796 * it with the subsequent small mblks. 19797 */ 19798 if (*usable >= len && 19799 MBLKL((*xmit_tail)->b_cont) < spill) { 19800 max_pld = 0; 19801 break; /* done */ 19802 } 19803 19804 /* 19805 * We can't spillover, and we are near 19806 * the end of the current payload buffer, 19807 * so send what's left. 19808 */ 19809 ASSERT(*tail_unsent > 0); 19810 len = *tail_unsent; 19811 } 19812 19813 /* tail_unsent is negated if there is a spillover */ 19814 *tail_unsent -= len; 19815 *usable -= len; 19816 ASSERT(*usable >= 0); 19817 19818 if (*usable < mss) 19819 seg_len = *usable; 19820 /* 19821 * Sender SWS avoidance; see comments in tcp_send(); 19822 * everything else is the same, except that we only 19823 * do this here if there is no more data to be sent 19824 * following the current xmit_tail. We don't check 19825 * for 1-byte urgent data because we shouldn't get 19826 * here if TCP_URG_VALID is set. 19827 */ 19828 if (*usable > 0 && *usable < mss && 19829 ((md_pbuf_nxt == NULL && 19830 (*xmit_tail)->b_cont == NULL) || 19831 (md_pbuf_nxt != NULL && 19832 (*xmit_tail)->b_cont->b_cont == NULL)) && 19833 seg_len < (tcp->tcp_max_swnd >> 1) && 19834 (tcp->tcp_unsent - 19835 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 19836 !tcp->tcp_zero_win_probe) { 19837 if ((*snxt + len) == tcp->tcp_snxt && 19838 (*snxt + len) == tcp->tcp_suna) { 19839 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19840 } 19841 done = B_TRUE; 19842 } 19843 19844 /* 19845 * Prime pump for IP's checksumming on our behalf; 19846 * include the adjustment for a source route if any. 19847 * Do this only for software/partial hardware checksum 19848 * offload, as this field gets zeroed out later for 19849 * the full hardware checksum offload case. 19850 */ 19851 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 19852 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19853 cksum = (cksum >> 16) + (cksum & 0xFFFF); 19854 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 19855 } 19856 19857 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 19858 *snxt += len; 19859 19860 tcp->tcp_tcph->th_flags[0] = TH_ACK; 19861 /* 19862 * We set the PUSH bit only if TCP has no more buffered 19863 * data to be transmitted (or if sender SWS avoidance 19864 * takes place), as opposed to setting it for every 19865 * last packet in the burst. 19866 */ 19867 if (done || 19868 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 19869 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 19870 19871 /* 19872 * Set FIN bit if this is our last segment; snxt 19873 * already includes its length, and it will not 19874 * be adjusted after this point. 19875 */ 19876 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 19877 *snxt == tcp->tcp_fss) { 19878 if (!tcp->tcp_fin_acked) { 19879 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 19880 BUMP_MIB(&tcps->tcps_mib, 19881 tcpOutControl); 19882 } 19883 if (!tcp->tcp_fin_sent) { 19884 tcp->tcp_fin_sent = B_TRUE; 19885 /* 19886 * tcp state must be ESTABLISHED 19887 * in order for us to get here in 19888 * the first place. 19889 */ 19890 tcp->tcp_state = TCPS_FIN_WAIT_1; 19891 19892 /* 19893 * Upon returning from this routine, 19894 * tcp_wput_data() will set tcp_snxt 19895 * to be equal to snxt + tcp_fin_sent. 19896 * This is essentially the same as 19897 * setting it to tcp_fss + 1. 19898 */ 19899 } 19900 } 19901 19902 tcp->tcp_last_sent_len = (ushort_t)len; 19903 19904 len += tcp_hdr_len; 19905 if (tcp->tcp_ipversion == IPV4_VERSION) 19906 tcp->tcp_ipha->ipha_length = htons(len); 19907 else 19908 tcp->tcp_ip6h->ip6_plen = htons(len - 19909 ((char *)&tcp->tcp_ip6h[1] - 19910 tcp->tcp_iphc)); 19911 19912 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 19913 19914 /* setup header fragment */ 19915 PDESC_HDR_ADD(pkt_info, 19916 md_hbuf->b_rptr + cur_hdr_off, /* base */ 19917 tcp->tcp_mdt_hdr_head, /* head room */ 19918 tcp_hdr_len, /* len */ 19919 tcp->tcp_mdt_hdr_tail); /* tail room */ 19920 19921 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 19922 hdr_frag_sz); 19923 ASSERT(MBLKIN(md_hbuf, 19924 (pkt_info->hdr_base - md_hbuf->b_rptr), 19925 PDESC_HDRSIZE(pkt_info))); 19926 19927 /* setup first payload fragment */ 19928 PDESC_PLD_INIT(pkt_info); 19929 PDESC_PLD_SPAN_ADD(pkt_info, 19930 pbuf_idx, /* index */ 19931 md_pbuf->b_rptr + cur_pld_off, /* start */ 19932 tcp->tcp_last_sent_len); /* len */ 19933 19934 /* create a split-packet in case of a spillover */ 19935 if (md_pbuf_nxt != NULL) { 19936 ASSERT(spill > 0); 19937 ASSERT(pbuf_idx_nxt > pbuf_idx); 19938 ASSERT(!add_buffer); 19939 19940 md_pbuf = md_pbuf_nxt; 19941 md_pbuf_nxt = NULL; 19942 pbuf_idx = pbuf_idx_nxt; 19943 pbuf_idx_nxt = -1; 19944 cur_pld_off = spill; 19945 19946 /* trim out first payload fragment */ 19947 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 19948 19949 /* setup second payload fragment */ 19950 PDESC_PLD_SPAN_ADD(pkt_info, 19951 pbuf_idx, /* index */ 19952 md_pbuf->b_rptr, /* start */ 19953 spill); /* len */ 19954 19955 if ((*xmit_tail)->b_next == NULL) { 19956 /* 19957 * Store the lbolt used for RTT 19958 * estimation. We can only record one 19959 * timestamp per mblk so we do it when 19960 * we reach the end of the payload 19961 * buffer. Also we only take a new 19962 * timestamp sample when the previous 19963 * timed data from the same mblk has 19964 * been ack'ed. 19965 */ 19966 (*xmit_tail)->b_prev = local_time; 19967 (*xmit_tail)->b_next = 19968 (mblk_t *)(uintptr_t)first_snxt; 19969 } 19970 19971 first_snxt = *snxt - spill; 19972 19973 /* 19974 * Advance xmit_tail; usable could be 0 by 19975 * the time we got here, but we made sure 19976 * above that we would only spillover to 19977 * the next data block if usable includes 19978 * the spilled-over amount prior to the 19979 * subtraction. Therefore, we are sure 19980 * that xmit_tail->b_cont can't be NULL. 19981 */ 19982 ASSERT((*xmit_tail)->b_cont != NULL); 19983 *xmit_tail = (*xmit_tail)->b_cont; 19984 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19985 (uintptr_t)INT_MAX); 19986 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 19987 } else { 19988 cur_pld_off += tcp->tcp_last_sent_len; 19989 } 19990 19991 /* 19992 * Fill in the header using the template header, and 19993 * add options such as time-stamp, ECN and/or SACK, 19994 * as needed. 19995 */ 19996 tcp_fill_header(tcp, pkt_info->hdr_rptr, 19997 (clock_t)local_time, num_sack_blk); 19998 19999 /* take care of some IP header businesses */ 20000 if (af == AF_INET) { 20001 ipha = (ipha_t *)pkt_info->hdr_rptr; 20002 20003 ASSERT(OK_32PTR((uchar_t *)ipha)); 20004 ASSERT(PDESC_HDRL(pkt_info) >= 20005 IP_SIMPLE_HDR_LENGTH); 20006 ASSERT(ipha->ipha_version_and_hdr_length == 20007 IP_SIMPLE_HDR_VERSION); 20008 20009 /* 20010 * Assign ident value for current packet; see 20011 * related comments in ip_wput_ire() about the 20012 * contract private interface with clustering 20013 * group. 20014 */ 20015 clusterwide = B_FALSE; 20016 if (cl_inet_ipident != NULL) { 20017 ASSERT(cl_inet_isclusterwide != NULL); 20018 if ((*cl_inet_isclusterwide)(stack_id, 20019 IPPROTO_IP, AF_INET, 20020 (uint8_t *)(uintptr_t)src, NULL)) { 20021 ipha->ipha_ident = 20022 (*cl_inet_ipident)(stack_id, 20023 IPPROTO_IP, AF_INET, 20024 (uint8_t *)(uintptr_t)src, 20025 (uint8_t *)(uintptr_t)dst, 20026 NULL); 20027 clusterwide = B_TRUE; 20028 } 20029 } 20030 20031 if (!clusterwide) { 20032 ipha->ipha_ident = (uint16_t) 20033 atomic_add_32_nv( 20034 &ire->ire_ident, 1); 20035 } 20036 #ifndef _BIG_ENDIAN 20037 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20038 (ipha->ipha_ident >> 8); 20039 #endif 20040 } else { 20041 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20042 20043 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20044 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20045 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20046 ASSERT(PDESC_HDRL(pkt_info) >= 20047 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20048 TCP_CHECKSUM_SIZE)); 20049 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20050 20051 if (tcp->tcp_ip_forward_progress) { 20052 rconfirm = B_TRUE; 20053 tcp->tcp_ip_forward_progress = B_FALSE; 20054 } 20055 } 20056 20057 /* at least one payload span, and at most two */ 20058 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20059 20060 /* add the packet descriptor to Multidata */ 20061 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20062 KM_NOSLEEP)) == NULL) { 20063 /* 20064 * Any failure other than ENOMEM indicates 20065 * that we have passed in invalid pkt_info 20066 * or parameters to mmd_addpdesc, which must 20067 * not happen. 20068 * 20069 * EINVAL is a result of failure on boundary 20070 * checks against the pkt_info contents. It 20071 * should not happen, and we panic because 20072 * either there's horrible heap corruption, 20073 * and/or programming mistake. 20074 */ 20075 if (err != ENOMEM) { 20076 cmn_err(CE_PANIC, "tcp_multisend: " 20077 "pdesc logic error detected for " 20078 "tcp %p mmd %p pinfo %p (%d)\n", 20079 (void *)tcp, (void *)mmd, 20080 (void *)pkt_info, err); 20081 } 20082 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20083 goto legacy_send; /* out_of_mem */ 20084 } 20085 ASSERT(pkt != NULL); 20086 20087 /* calculate IP header and TCP checksums */ 20088 if (af == AF_INET) { 20089 /* calculate pseudo-header checksum */ 20090 cksum = (dst >> 16) + (dst & 0xFFFF) + 20091 (src >> 16) + (src & 0xFFFF); 20092 20093 /* offset for TCP header checksum */ 20094 up = IPH_TCPH_CHECKSUMP(ipha, 20095 IP_SIMPLE_HDR_LENGTH); 20096 } else { 20097 up = (uint16_t *)&ip6h->ip6_src; 20098 20099 /* calculate pseudo-header checksum */ 20100 cksum = up[0] + up[1] + up[2] + up[3] + 20101 up[4] + up[5] + up[6] + up[7] + 20102 up[8] + up[9] + up[10] + up[11] + 20103 up[12] + up[13] + up[14] + up[15]; 20104 20105 /* Fold the initial sum */ 20106 cksum = (cksum & 0xffff) + (cksum >> 16); 20107 20108 up = (uint16_t *)(((uchar_t *)ip6h) + 20109 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20110 } 20111 20112 if (hwcksum_flags & HCK_FULLCKSUM) { 20113 /* clear checksum field for hardware */ 20114 *up = 0; 20115 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20116 uint32_t sum; 20117 20118 /* pseudo-header checksumming */ 20119 sum = *up + cksum + IP_TCP_CSUM_COMP; 20120 sum = (sum & 0xFFFF) + (sum >> 16); 20121 *up = (sum & 0xFFFF) + (sum >> 16); 20122 } else { 20123 /* software checksumming */ 20124 TCP_STAT(tcps, tcp_out_sw_cksum); 20125 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20126 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20127 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20128 cksum + IP_TCP_CSUM_COMP); 20129 if (*up == 0) 20130 *up = 0xFFFF; 20131 } 20132 20133 /* IPv4 header checksum */ 20134 if (af == AF_INET) { 20135 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20136 ipha->ipha_hdr_checksum = 0; 20137 } else { 20138 IP_HDR_CKSUM(ipha, cksum, 20139 ((uint32_t *)ipha)[0], 20140 ((uint16_t *)ipha)[4]); 20141 } 20142 } 20143 20144 if (af == AF_INET && 20145 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20146 af == AF_INET6 && 20147 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20148 mblk_t *mp, *mp1; 20149 uchar_t *hdr_rptr, *hdr_wptr; 20150 uchar_t *pld_rptr, *pld_wptr; 20151 20152 /* 20153 * We reconstruct a pseudo packet for the hooks 20154 * framework using mmd_transform_link(). 20155 * If it is a split packet we pullup the 20156 * payload. FW_HOOKS expects a pkt comprising 20157 * of two mblks: a header and the payload. 20158 */ 20159 if ((mp = mmd_transform_link(pkt)) == NULL) { 20160 TCP_STAT(tcps, tcp_mdt_allocfail); 20161 goto legacy_send; 20162 } 20163 20164 if (pkt_info->pld_cnt > 1) { 20165 /* split payload, more than one pld */ 20166 if ((mp1 = msgpullup(mp->b_cont, -1)) == 20167 NULL) { 20168 freemsg(mp); 20169 TCP_STAT(tcps, 20170 tcp_mdt_allocfail); 20171 goto legacy_send; 20172 } 20173 freemsg(mp->b_cont); 20174 mp->b_cont = mp1; 20175 } else { 20176 mp1 = mp->b_cont; 20177 } 20178 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 20179 20180 /* 20181 * Remember the message offsets. This is so we 20182 * can detect changes when we return from the 20183 * FW_HOOKS callbacks. 20184 */ 20185 hdr_rptr = mp->b_rptr; 20186 hdr_wptr = mp->b_wptr; 20187 pld_rptr = mp->b_cont->b_rptr; 20188 pld_wptr = mp->b_cont->b_wptr; 20189 20190 if (af == AF_INET) { 20191 DTRACE_PROBE4( 20192 ip4__physical__out__start, 20193 ill_t *, NULL, 20194 ill_t *, ill, 20195 ipha_t *, ipha, 20196 mblk_t *, mp); 20197 FW_HOOKS( 20198 ipst->ips_ip4_physical_out_event, 20199 ipst->ips_ipv4firewall_physical_out, 20200 NULL, ill, ipha, mp, mp, 0, ipst); 20201 DTRACE_PROBE1( 20202 ip4__physical__out__end, 20203 mblk_t *, mp); 20204 } else { 20205 DTRACE_PROBE4( 20206 ip6__physical__out_start, 20207 ill_t *, NULL, 20208 ill_t *, ill, 20209 ip6_t *, ip6h, 20210 mblk_t *, mp); 20211 FW_HOOKS6( 20212 ipst->ips_ip6_physical_out_event, 20213 ipst->ips_ipv6firewall_physical_out, 20214 NULL, ill, ip6h, mp, mp, 0, ipst); 20215 DTRACE_PROBE1( 20216 ip6__physical__out__end, 20217 mblk_t *, mp); 20218 } 20219 20220 if (mp == NULL || 20221 (mp1 = mp->b_cont) == NULL || 20222 mp->b_rptr != hdr_rptr || 20223 mp->b_wptr != hdr_wptr || 20224 mp1->b_rptr != pld_rptr || 20225 mp1->b_wptr != pld_wptr || 20226 mp1->b_cont != NULL) { 20227 /* 20228 * We abandon multidata processing and 20229 * return to the normal path, either 20230 * when a packet is blocked, or when 20231 * the boundaries of header buffer or 20232 * payload buffer have been changed by 20233 * FW_HOOKS[6]. 20234 */ 20235 if (mp != NULL) 20236 freemsg(mp); 20237 goto legacy_send; 20238 } 20239 /* Finished with the pseudo packet */ 20240 freemsg(mp); 20241 } 20242 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 20243 ill, ipha, ip6h); 20244 /* advance header offset */ 20245 cur_hdr_off += hdr_frag_sz; 20246 20247 obbytes += tcp->tcp_last_sent_len; 20248 ++obsegs; 20249 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20250 *tail_unsent > 0); 20251 20252 if ((*xmit_tail)->b_next == NULL) { 20253 /* 20254 * Store the lbolt used for RTT estimation. We can only 20255 * record one timestamp per mblk so we do it when we 20256 * reach the end of the payload buffer. Also we only 20257 * take a new timestamp sample when the previous timed 20258 * data from the same mblk has been ack'ed. 20259 */ 20260 (*xmit_tail)->b_prev = local_time; 20261 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20262 } 20263 20264 ASSERT(*tail_unsent >= 0); 20265 if (*tail_unsent > 0) { 20266 /* 20267 * We got here because we broke out of the above 20268 * loop due to of one of the following cases: 20269 * 20270 * 1. len < adjusted MSS (i.e. small), 20271 * 2. Sender SWS avoidance, 20272 * 3. max_pld is zero. 20273 * 20274 * We are done for this Multidata, so trim our 20275 * last payload buffer (if any) accordingly. 20276 */ 20277 if (md_pbuf != NULL) 20278 md_pbuf->b_wptr -= *tail_unsent; 20279 } else if (*usable > 0) { 20280 *xmit_tail = (*xmit_tail)->b_cont; 20281 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20282 (uintptr_t)INT_MAX); 20283 *tail_unsent = (int)MBLKL(*xmit_tail); 20284 add_buffer = B_TRUE; 20285 } 20286 } while (!done && *usable > 0 && num_burst_seg > 0 && 20287 (tcp_mdt_chain || max_pld > 0)); 20288 20289 if (md_mp_head != NULL) { 20290 /* send everything down */ 20291 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20292 &rconfirm); 20293 } 20294 20295 #undef PREP_NEW_MULTIDATA 20296 #undef PREP_NEW_PBUF 20297 #undef IPVER 20298 20299 IRE_REFRELE(ire); 20300 return (0); 20301 } 20302 20303 /* 20304 * A wrapper function for sending one or more Multidata messages down to 20305 * the module below ip; this routine does not release the reference of the 20306 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20307 */ 20308 static void 20309 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20310 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20311 { 20312 uint64_t delta; 20313 nce_t *nce; 20314 tcp_stack_t *tcps = tcp->tcp_tcps; 20315 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20316 20317 ASSERT(ire != NULL && ill != NULL); 20318 ASSERT(ire->ire_stq != NULL); 20319 ASSERT(md_mp_head != NULL); 20320 ASSERT(rconfirm != NULL); 20321 20322 /* adjust MIBs and IRE timestamp */ 20323 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 20324 tcp->tcp_obsegs += obsegs; 20325 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20326 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20327 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20328 20329 if (tcp->tcp_ipversion == IPV4_VERSION) { 20330 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20331 } else { 20332 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20333 } 20334 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20335 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20336 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20337 20338 ire->ire_ob_pkt_count += obsegs; 20339 if (ire->ire_ipif != NULL) 20340 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20341 ire->ire_last_used_time = lbolt; 20342 20343 if (ipst->ips_ipobs_enabled) { 20344 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 20345 pdesc_t *dl_pkt; 20346 pdescinfo_t pinfo; 20347 mblk_t *nmp; 20348 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 20349 20350 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 20351 (dl_pkt != NULL); 20352 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 20353 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 20354 continue; 20355 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 20356 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 20357 freemsg(nmp); 20358 } 20359 } 20360 20361 /* send it down */ 20362 putnext(ire->ire_stq, md_mp_head); 20363 20364 /* we're done for TCP/IPv4 */ 20365 if (tcp->tcp_ipversion == IPV4_VERSION) 20366 return; 20367 20368 nce = ire->ire_nce; 20369 20370 ASSERT(nce != NULL); 20371 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20372 ASSERT(nce->nce_state != ND_INCOMPLETE); 20373 20374 /* reachability confirmation? */ 20375 if (*rconfirm) { 20376 nce->nce_last = TICK_TO_MSEC(lbolt64); 20377 if (nce->nce_state != ND_REACHABLE) { 20378 mutex_enter(&nce->nce_lock); 20379 nce->nce_state = ND_REACHABLE; 20380 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20381 mutex_exit(&nce->nce_lock); 20382 (void) untimeout(nce->nce_timeout_id); 20383 if (ip_debug > 2) { 20384 /* ip1dbg */ 20385 pr_addr_dbg("tcp_multisend_data: state " 20386 "for %s changed to REACHABLE\n", 20387 AF_INET6, &ire->ire_addr_v6); 20388 } 20389 } 20390 /* reset transport reachability confirmation */ 20391 *rconfirm = B_FALSE; 20392 } 20393 20394 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20395 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20396 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20397 20398 if (delta > (uint64_t)ill->ill_reachable_time) { 20399 mutex_enter(&nce->nce_lock); 20400 switch (nce->nce_state) { 20401 case ND_REACHABLE: 20402 case ND_STALE: 20403 /* 20404 * ND_REACHABLE is identical to ND_STALE in this 20405 * specific case. If reachable time has expired for 20406 * this neighbor (delta is greater than reachable 20407 * time), conceptually, the neighbor cache is no 20408 * longer in REACHABLE state, but already in STALE 20409 * state. So the correct transition here is to 20410 * ND_DELAY. 20411 */ 20412 nce->nce_state = ND_DELAY; 20413 mutex_exit(&nce->nce_lock); 20414 NDP_RESTART_TIMER(nce, 20415 ipst->ips_delay_first_probe_time); 20416 if (ip_debug > 3) { 20417 /* ip2dbg */ 20418 pr_addr_dbg("tcp_multisend_data: state " 20419 "for %s changed to DELAY\n", 20420 AF_INET6, &ire->ire_addr_v6); 20421 } 20422 break; 20423 case ND_DELAY: 20424 case ND_PROBE: 20425 mutex_exit(&nce->nce_lock); 20426 /* Timers have already started */ 20427 break; 20428 case ND_UNREACHABLE: 20429 /* 20430 * ndp timer has detected that this nce is 20431 * unreachable and initiated deleting this nce 20432 * and all its associated IREs. This is a race 20433 * where we found the ire before it was deleted 20434 * and have just sent out a packet using this 20435 * unreachable nce. 20436 */ 20437 mutex_exit(&nce->nce_lock); 20438 break; 20439 default: 20440 ASSERT(0); 20441 } 20442 } 20443 } 20444 20445 /* 20446 * Derived from tcp_send_data(). 20447 */ 20448 static void 20449 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20450 int num_lso_seg) 20451 { 20452 ipha_t *ipha; 20453 mblk_t *ire_fp_mp; 20454 uint_t ire_fp_mp_len; 20455 uint32_t hcksum_txflags = 0; 20456 ipaddr_t src; 20457 ipaddr_t dst; 20458 uint32_t cksum; 20459 uint16_t *up; 20460 tcp_stack_t *tcps = tcp->tcp_tcps; 20461 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20462 20463 ASSERT(DB_TYPE(mp) == M_DATA); 20464 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20465 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20466 ASSERT(tcp->tcp_connp != NULL); 20467 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20468 20469 ipha = (ipha_t *)mp->b_rptr; 20470 src = ipha->ipha_src; 20471 dst = ipha->ipha_dst; 20472 20473 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 20474 20475 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20476 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20477 num_lso_seg); 20478 #ifndef _BIG_ENDIAN 20479 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20480 #endif 20481 if (tcp->tcp_snd_zcopy_aware) { 20482 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20483 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20484 mp = tcp_zcopy_disable(tcp, mp); 20485 } 20486 20487 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20488 ASSERT(ill->ill_hcksum_capab != NULL); 20489 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20490 } 20491 20492 /* 20493 * Since the TCP checksum should be recalculated by h/w, we can just 20494 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20495 * pseudo-header checksum for HCK_PARTIALCKSUM. 20496 * The partial pseudo-header excludes TCP length, that was calculated 20497 * in tcp_send(), so to zero *up before further processing. 20498 */ 20499 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20500 20501 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20502 *up = 0; 20503 20504 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20505 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20506 20507 /* 20508 * Append LSO flags and mss to the mp. 20509 */ 20510 lso_info_set(mp, mss, HW_LSO); 20511 20512 ipha->ipha_fragment_offset_and_flags |= 20513 (uint32_t)htons(ire->ire_frag_flag); 20514 20515 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20516 ire_fp_mp_len = MBLKL(ire_fp_mp); 20517 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20518 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20519 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20520 20521 UPDATE_OB_PKT_COUNT(ire); 20522 ire->ire_last_used_time = lbolt; 20523 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20524 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20525 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20526 ntohs(ipha->ipha_length)); 20527 20528 DTRACE_PROBE4(ip4__physical__out__start, 20529 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 20530 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20531 ipst->ips_ipv4firewall_physical_out, NULL, 20532 ill, ipha, mp, mp, 0, ipst); 20533 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20534 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 20535 20536 if (mp != NULL) { 20537 if (ipst->ips_ipobs_enabled) { 20538 zoneid_t szone; 20539 20540 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 20541 ipst, ALL_ZONES); 20542 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 20543 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 20544 } 20545 20546 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0, NULL); 20547 } 20548 } 20549 20550 /* 20551 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20552 * scheme, and returns one of the following: 20553 * 20554 * -1 = failed allocation. 20555 * 0 = success; burst count reached, or usable send window is too small, 20556 * and that we'd rather wait until later before sending again. 20557 * 1 = success; we are called from tcp_multisend(), and both usable send 20558 * window and tail_unsent are greater than the MDT threshold, and thus 20559 * Multidata Transmit should be used instead. 20560 */ 20561 static int 20562 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20563 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20564 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20565 const int mdt_thres) 20566 { 20567 int num_burst_seg = tcp->tcp_snd_burst; 20568 ire_t *ire = NULL; 20569 ill_t *ill = NULL; 20570 mblk_t *ire_fp_mp = NULL; 20571 uint_t ire_fp_mp_len = 0; 20572 int num_lso_seg = 1; 20573 uint_t lso_usable; 20574 boolean_t do_lso_send = B_FALSE; 20575 tcp_stack_t *tcps = tcp->tcp_tcps; 20576 20577 /* 20578 * Check LSO capability before any further work. And the similar check 20579 * need to be done in for(;;) loop. 20580 * LSO will be deployed when therer is more than one mss of available 20581 * data and a burst transmission is allowed. 20582 */ 20583 if (tcp->tcp_lso && 20584 (tcp->tcp_valid_bits == 0 || 20585 tcp->tcp_valid_bits == TCP_FSS_VALID) && 20586 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20587 /* 20588 * Try to find usable IRE/ILL and do basic check to the ILL. 20589 * Double check LSO usability before going further, since the 20590 * underlying interface could have been changed. In case of any 20591 * change of LSO capability, set tcp_ire_ill_check_done to 20592 * B_FALSE to force to check the ILL with the next send. 20593 */ 20594 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill) && 20595 tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 20596 /* 20597 * Enable LSO with this transmission. 20598 * Since IRE has been hold in tcp_send_find_ire_ill(), 20599 * IRE_REFRELE(ire) should be called before return. 20600 */ 20601 do_lso_send = B_TRUE; 20602 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20603 ire_fp_mp_len = MBLKL(ire_fp_mp); 20604 /* Round up to multiple of 4 */ 20605 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 20606 } else { 20607 tcp->tcp_lso = B_FALSE; 20608 tcp->tcp_ire_ill_check_done = B_FALSE; 20609 do_lso_send = B_FALSE; 20610 ill = NULL; 20611 } 20612 } 20613 20614 for (;;) { 20615 struct datab *db; 20616 tcph_t *tcph; 20617 uint32_t sum; 20618 mblk_t *mp, *mp1; 20619 uchar_t *rptr; 20620 int len; 20621 20622 /* 20623 * If we're called by tcp_multisend(), and the amount of 20624 * sendable data as well as the size of current xmit_tail 20625 * is beyond the MDT threshold, return to the caller and 20626 * let the large data transmit be done using MDT. 20627 */ 20628 if (*usable > 0 && *usable > mdt_thres && 20629 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 20630 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 20631 ASSERT(tcp->tcp_mdt); 20632 return (1); /* success; do large send */ 20633 } 20634 20635 if (num_burst_seg == 0) 20636 break; /* success; burst count reached */ 20637 20638 /* 20639 * Calculate the maximum payload length we can send in *one* 20640 * time. 20641 */ 20642 if (do_lso_send) { 20643 /* 20644 * Check whether need to do LSO any more. 20645 */ 20646 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20647 lso_usable = MIN(tcp->tcp_lso_max, *usable); 20648 lso_usable = MIN(lso_usable, 20649 num_burst_seg * mss); 20650 20651 num_lso_seg = lso_usable / mss; 20652 if (lso_usable % mss) { 20653 num_lso_seg++; 20654 tcp->tcp_last_sent_len = (ushort_t) 20655 (lso_usable % mss); 20656 } else { 20657 tcp->tcp_last_sent_len = (ushort_t)mss; 20658 } 20659 } else { 20660 do_lso_send = B_FALSE; 20661 num_lso_seg = 1; 20662 lso_usable = mss; 20663 } 20664 } 20665 20666 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 20667 20668 /* 20669 * Adjust num_burst_seg here. 20670 */ 20671 num_burst_seg -= num_lso_seg; 20672 20673 len = mss; 20674 if (len > *usable) { 20675 ASSERT(do_lso_send == B_FALSE); 20676 20677 len = *usable; 20678 if (len <= 0) { 20679 /* Terminate the loop */ 20680 break; /* success; too small */ 20681 } 20682 /* 20683 * Sender silly-window avoidance. 20684 * Ignore this if we are going to send a 20685 * zero window probe out. 20686 * 20687 * TODO: force data into microscopic window? 20688 * ==> (!pushed || (unsent > usable)) 20689 */ 20690 if (len < (tcp->tcp_max_swnd >> 1) && 20691 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 20692 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 20693 len == 1) && (! tcp->tcp_zero_win_probe)) { 20694 /* 20695 * If the retransmit timer is not running 20696 * we start it so that we will retransmit 20697 * in the case when the the receiver has 20698 * decremented the window. 20699 */ 20700 if (*snxt == tcp->tcp_snxt && 20701 *snxt == tcp->tcp_suna) { 20702 /* 20703 * We are not supposed to send 20704 * anything. So let's wait a little 20705 * bit longer before breaking SWS 20706 * avoidance. 20707 * 20708 * What should the value be? 20709 * Suggestion: MAX(init rexmit time, 20710 * tcp->tcp_rto) 20711 */ 20712 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20713 } 20714 break; /* success; too small */ 20715 } 20716 } 20717 20718 tcph = tcp->tcp_tcph; 20719 20720 /* 20721 * The reason to adjust len here is that we need to set flags 20722 * and calculate checksum. 20723 */ 20724 if (do_lso_send) 20725 len = lso_usable; 20726 20727 *usable -= len; /* Approximate - can be adjusted later */ 20728 if (*usable > 0) 20729 tcph->th_flags[0] = TH_ACK; 20730 else 20731 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 20732 20733 /* 20734 * Prime pump for IP's checksumming on our behalf 20735 * Include the adjustment for a source route if any. 20736 */ 20737 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20738 sum = (sum >> 16) + (sum & 0xFFFF); 20739 U16_TO_ABE16(sum, tcph->th_sum); 20740 20741 U32_TO_ABE32(*snxt, tcph->th_seq); 20742 20743 /* 20744 * Branch off to tcp_xmit_mp() if any of the VALID bits is 20745 * set. For the case when TCP_FSS_VALID is the only valid 20746 * bit (normal active close), branch off only when we think 20747 * that the FIN flag needs to be set. Note for this case, 20748 * that (snxt + len) may not reflect the actual seg_len, 20749 * as len may be further reduced in tcp_xmit_mp(). If len 20750 * gets modified, we will end up here again. 20751 */ 20752 if (tcp->tcp_valid_bits != 0 && 20753 (tcp->tcp_valid_bits != TCP_FSS_VALID || 20754 ((*snxt + len) == tcp->tcp_fss))) { 20755 uchar_t *prev_rptr; 20756 uint32_t prev_snxt = tcp->tcp_snxt; 20757 20758 if (*tail_unsent == 0) { 20759 ASSERT((*xmit_tail)->b_cont != NULL); 20760 *xmit_tail = (*xmit_tail)->b_cont; 20761 prev_rptr = (*xmit_tail)->b_rptr; 20762 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20763 (*xmit_tail)->b_rptr); 20764 } else { 20765 prev_rptr = (*xmit_tail)->b_rptr; 20766 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 20767 *tail_unsent; 20768 } 20769 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 20770 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20771 /* Restore tcp_snxt so we get amount sent right. */ 20772 tcp->tcp_snxt = prev_snxt; 20773 if (prev_rptr == (*xmit_tail)->b_rptr) { 20774 /* 20775 * If the previous timestamp is still in use, 20776 * don't stomp on it. 20777 */ 20778 if ((*xmit_tail)->b_next == NULL) { 20779 (*xmit_tail)->b_prev = local_time; 20780 (*xmit_tail)->b_next = 20781 (mblk_t *)(uintptr_t)(*snxt); 20782 } 20783 } else 20784 (*xmit_tail)->b_rptr = prev_rptr; 20785 20786 if (mp == NULL) { 20787 if (ire != NULL) 20788 IRE_REFRELE(ire); 20789 return (-1); 20790 } 20791 mp1 = mp->b_cont; 20792 20793 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20794 tcp->tcp_last_sent_len = (ushort_t)len; 20795 while (mp1->b_cont) { 20796 *xmit_tail = (*xmit_tail)->b_cont; 20797 (*xmit_tail)->b_prev = local_time; 20798 (*xmit_tail)->b_next = 20799 (mblk_t *)(uintptr_t)(*snxt); 20800 mp1 = mp1->b_cont; 20801 } 20802 *snxt += len; 20803 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20804 BUMP_LOCAL(tcp->tcp_obsegs); 20805 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20806 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20807 tcp_send_data(tcp, q, mp); 20808 continue; 20809 } 20810 20811 *snxt += len; /* Adjust later if we don't send all of len */ 20812 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20813 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20814 20815 if (*tail_unsent) { 20816 /* Are the bytes above us in flight? */ 20817 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20818 if (rptr != (*xmit_tail)->b_rptr) { 20819 *tail_unsent -= len; 20820 if (len <= mss) /* LSO is unusable */ 20821 tcp->tcp_last_sent_len = (ushort_t)len; 20822 len += tcp_hdr_len; 20823 if (tcp->tcp_ipversion == IPV4_VERSION) 20824 tcp->tcp_ipha->ipha_length = htons(len); 20825 else 20826 tcp->tcp_ip6h->ip6_plen = 20827 htons(len - 20828 ((char *)&tcp->tcp_ip6h[1] - 20829 tcp->tcp_iphc)); 20830 mp = dupb(*xmit_tail); 20831 if (mp == NULL) { 20832 if (ire != NULL) 20833 IRE_REFRELE(ire); 20834 return (-1); /* out_of_mem */ 20835 } 20836 mp->b_rptr = rptr; 20837 /* 20838 * If the old timestamp is no longer in use, 20839 * sample a new timestamp now. 20840 */ 20841 if ((*xmit_tail)->b_next == NULL) { 20842 (*xmit_tail)->b_prev = local_time; 20843 (*xmit_tail)->b_next = 20844 (mblk_t *)(uintptr_t)(*snxt-len); 20845 } 20846 goto must_alloc; 20847 } 20848 } else { 20849 *xmit_tail = (*xmit_tail)->b_cont; 20850 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 20851 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 20852 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20853 (*xmit_tail)->b_rptr); 20854 } 20855 20856 (*xmit_tail)->b_prev = local_time; 20857 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 20858 20859 *tail_unsent -= len; 20860 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20861 tcp->tcp_last_sent_len = (ushort_t)len; 20862 20863 len += tcp_hdr_len; 20864 if (tcp->tcp_ipversion == IPV4_VERSION) 20865 tcp->tcp_ipha->ipha_length = htons(len); 20866 else 20867 tcp->tcp_ip6h->ip6_plen = htons(len - 20868 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20869 20870 mp = dupb(*xmit_tail); 20871 if (mp == NULL) { 20872 if (ire != NULL) 20873 IRE_REFRELE(ire); 20874 return (-1); /* out_of_mem */ 20875 } 20876 20877 len = tcp_hdr_len; 20878 /* 20879 * There are four reasons to allocate a new hdr mblk: 20880 * 1) The bytes above us are in use by another packet 20881 * 2) We don't have good alignment 20882 * 3) The mblk is being shared 20883 * 4) We don't have enough room for a header 20884 */ 20885 rptr = mp->b_rptr - len; 20886 if (!OK_32PTR(rptr) || 20887 ((db = mp->b_datap), db->db_ref != 2) || 20888 rptr < db->db_base + ire_fp_mp_len) { 20889 /* NOTE: we assume allocb returns an OK_32PTR */ 20890 20891 must_alloc:; 20892 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 20893 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 20894 if (mp1 == NULL) { 20895 freemsg(mp); 20896 if (ire != NULL) 20897 IRE_REFRELE(ire); 20898 return (-1); /* out_of_mem */ 20899 } 20900 mp1->b_cont = mp; 20901 mp = mp1; 20902 /* Leave room for Link Level header */ 20903 len = tcp_hdr_len; 20904 rptr = 20905 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 20906 mp->b_wptr = &rptr[len]; 20907 } 20908 20909 /* 20910 * Fill in the header using the template header, and add 20911 * options such as time-stamp, ECN and/or SACK, as needed. 20912 */ 20913 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 20914 20915 mp->b_rptr = rptr; 20916 20917 if (*tail_unsent) { 20918 int spill = *tail_unsent; 20919 20920 mp1 = mp->b_cont; 20921 if (mp1 == NULL) 20922 mp1 = mp; 20923 20924 /* 20925 * If we're a little short, tack on more mblks until 20926 * there is no more spillover. 20927 */ 20928 while (spill < 0) { 20929 mblk_t *nmp; 20930 int nmpsz; 20931 20932 nmp = (*xmit_tail)->b_cont; 20933 nmpsz = MBLKL(nmp); 20934 20935 /* 20936 * Excess data in mblk; can we split it? 20937 * If MDT is enabled for the connection, 20938 * keep on splitting as this is a transient 20939 * send path. 20940 */ 20941 if (!do_lso_send && !tcp->tcp_mdt && 20942 (spill + nmpsz > 0)) { 20943 /* 20944 * Don't split if stream head was 20945 * told to break up larger writes 20946 * into smaller ones. 20947 */ 20948 if (tcp->tcp_maxpsz > 0) 20949 break; 20950 20951 /* 20952 * Next mblk is less than SMSS/2 20953 * rounded up to nearest 64-byte; 20954 * let it get sent as part of the 20955 * next segment. 20956 */ 20957 if (tcp->tcp_localnet && 20958 !tcp->tcp_cork && 20959 (nmpsz < roundup((mss >> 1), 64))) 20960 break; 20961 } 20962 20963 *xmit_tail = nmp; 20964 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 20965 /* Stash for rtt use later */ 20966 (*xmit_tail)->b_prev = local_time; 20967 (*xmit_tail)->b_next = 20968 (mblk_t *)(uintptr_t)(*snxt - len); 20969 mp1->b_cont = dupb(*xmit_tail); 20970 mp1 = mp1->b_cont; 20971 20972 spill += nmpsz; 20973 if (mp1 == NULL) { 20974 *tail_unsent = spill; 20975 freemsg(mp); 20976 if (ire != NULL) 20977 IRE_REFRELE(ire); 20978 return (-1); /* out_of_mem */ 20979 } 20980 } 20981 20982 /* Trim back any surplus on the last mblk */ 20983 if (spill >= 0) { 20984 mp1->b_wptr -= spill; 20985 *tail_unsent = spill; 20986 } else { 20987 /* 20988 * We did not send everything we could in 20989 * order to remain within the b_cont limit. 20990 */ 20991 *usable -= spill; 20992 *snxt += spill; 20993 tcp->tcp_last_sent_len += spill; 20994 UPDATE_MIB(&tcps->tcps_mib, 20995 tcpOutDataBytes, spill); 20996 /* 20997 * Adjust the checksum 20998 */ 20999 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21000 sum += spill; 21001 sum = (sum >> 16) + (sum & 0xFFFF); 21002 U16_TO_ABE16(sum, tcph->th_sum); 21003 if (tcp->tcp_ipversion == IPV4_VERSION) { 21004 sum = ntohs( 21005 ((ipha_t *)rptr)->ipha_length) + 21006 spill; 21007 ((ipha_t *)rptr)->ipha_length = 21008 htons(sum); 21009 } else { 21010 sum = ntohs( 21011 ((ip6_t *)rptr)->ip6_plen) + 21012 spill; 21013 ((ip6_t *)rptr)->ip6_plen = 21014 htons(sum); 21015 } 21016 *tail_unsent = 0; 21017 } 21018 } 21019 if (tcp->tcp_ip_forward_progress) { 21020 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21021 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21022 tcp->tcp_ip_forward_progress = B_FALSE; 21023 } 21024 21025 if (do_lso_send) { 21026 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21027 num_lso_seg); 21028 tcp->tcp_obsegs += num_lso_seg; 21029 21030 TCP_STAT(tcps, tcp_lso_times); 21031 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21032 } else { 21033 tcp_send_data(tcp, q, mp); 21034 BUMP_LOCAL(tcp->tcp_obsegs); 21035 } 21036 } 21037 21038 if (ire != NULL) 21039 IRE_REFRELE(ire); 21040 return (0); 21041 } 21042 21043 /* Unlink and return any mblk that looks like it contains a MDT info */ 21044 static mblk_t * 21045 tcp_mdt_info_mp(mblk_t *mp) 21046 { 21047 mblk_t *prev_mp; 21048 21049 for (;;) { 21050 prev_mp = mp; 21051 /* no more to process? */ 21052 if ((mp = mp->b_cont) == NULL) 21053 break; 21054 21055 switch (DB_TYPE(mp)) { 21056 case M_CTL: 21057 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21058 continue; 21059 ASSERT(prev_mp != NULL); 21060 prev_mp->b_cont = mp->b_cont; 21061 mp->b_cont = NULL; 21062 return (mp); 21063 default: 21064 break; 21065 } 21066 } 21067 return (mp); 21068 } 21069 21070 /* MDT info update routine, called when IP notifies us about MDT */ 21071 static void 21072 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21073 { 21074 boolean_t prev_state; 21075 tcp_stack_t *tcps = tcp->tcp_tcps; 21076 21077 /* 21078 * IP is telling us to abort MDT on this connection? We know 21079 * this because the capability is only turned off when IP 21080 * encounters some pathological cases, e.g. link-layer change 21081 * where the new driver doesn't support MDT, or in situation 21082 * where MDT usage on the link-layer has been switched off. 21083 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21084 * if the link-layer doesn't support MDT, and if it does, it 21085 * will indicate that the feature is to be turned on. 21086 */ 21087 prev_state = tcp->tcp_mdt; 21088 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21089 if (!tcp->tcp_mdt && !first) { 21090 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21091 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21092 (void *)tcp->tcp_connp)); 21093 } 21094 21095 /* 21096 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21097 * so disable MDT otherwise. The checks are done here 21098 * and in tcp_wput_data(). 21099 */ 21100 if (tcp->tcp_mdt && 21101 (tcp->tcp_ipversion == IPV4_VERSION && 21102 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21103 (tcp->tcp_ipversion == IPV6_VERSION && 21104 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21105 tcp->tcp_mdt = B_FALSE; 21106 21107 if (tcp->tcp_mdt) { 21108 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21109 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21110 "version (%d), expected version is %d", 21111 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21112 tcp->tcp_mdt = B_FALSE; 21113 return; 21114 } 21115 21116 /* 21117 * We need the driver to be able to handle at least three 21118 * spans per packet in order for tcp MDT to be utilized. 21119 * The first is for the header portion, while the rest are 21120 * needed to handle a packet that straddles across two 21121 * virtually non-contiguous buffers; a typical tcp packet 21122 * therefore consists of only two spans. Note that we take 21123 * a zero as "don't care". 21124 */ 21125 if (mdt_capab->ill_mdt_span_limit > 0 && 21126 mdt_capab->ill_mdt_span_limit < 3) { 21127 tcp->tcp_mdt = B_FALSE; 21128 return; 21129 } 21130 21131 /* a zero means driver wants default value */ 21132 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21133 tcps->tcps_mdt_max_pbufs); 21134 if (tcp->tcp_mdt_max_pld == 0) 21135 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21136 21137 /* ensure 32-bit alignment */ 21138 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21139 mdt_capab->ill_mdt_hdr_head), 4); 21140 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21141 mdt_capab->ill_mdt_hdr_tail), 4); 21142 21143 if (!first && !prev_state) { 21144 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21145 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21146 (void *)tcp->tcp_connp)); 21147 } 21148 } 21149 } 21150 21151 /* Unlink and return any mblk that looks like it contains a LSO info */ 21152 static mblk_t * 21153 tcp_lso_info_mp(mblk_t *mp) 21154 { 21155 mblk_t *prev_mp; 21156 21157 for (;;) { 21158 prev_mp = mp; 21159 /* no more to process? */ 21160 if ((mp = mp->b_cont) == NULL) 21161 break; 21162 21163 switch (DB_TYPE(mp)) { 21164 case M_CTL: 21165 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21166 continue; 21167 ASSERT(prev_mp != NULL); 21168 prev_mp->b_cont = mp->b_cont; 21169 mp->b_cont = NULL; 21170 return (mp); 21171 default: 21172 break; 21173 } 21174 } 21175 21176 return (mp); 21177 } 21178 21179 /* LSO info update routine, called when IP notifies us about LSO */ 21180 static void 21181 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21182 { 21183 tcp_stack_t *tcps = tcp->tcp_tcps; 21184 21185 /* 21186 * IP is telling us to abort LSO on this connection? We know 21187 * this because the capability is only turned off when IP 21188 * encounters some pathological cases, e.g. link-layer change 21189 * where the new NIC/driver doesn't support LSO, or in situation 21190 * where LSO usage on the link-layer has been switched off. 21191 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21192 * if the link-layer doesn't support LSO, and if it does, it 21193 * will indicate that the feature is to be turned on. 21194 */ 21195 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21196 TCP_STAT(tcps, tcp_lso_enabled); 21197 21198 /* 21199 * We currently only support LSO on simple TCP/IPv4, 21200 * so disable LSO otherwise. The checks are done here 21201 * and in tcp_wput_data(). 21202 */ 21203 if (tcp->tcp_lso && 21204 (tcp->tcp_ipversion == IPV4_VERSION && 21205 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21206 (tcp->tcp_ipversion == IPV6_VERSION)) { 21207 tcp->tcp_lso = B_FALSE; 21208 TCP_STAT(tcps, tcp_lso_disabled); 21209 } else { 21210 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21211 lso_capab->ill_lso_max); 21212 } 21213 } 21214 21215 static void 21216 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21217 { 21218 conn_t *connp = tcp->tcp_connp; 21219 tcp_stack_t *tcps = tcp->tcp_tcps; 21220 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21221 21222 ASSERT(ire != NULL); 21223 21224 /* 21225 * We may be in the fastpath here, and although we essentially do 21226 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21227 * we try to keep things as brief as possible. After all, these 21228 * are only best-effort checks, and we do more thorough ones prior 21229 * to calling tcp_send()/tcp_multisend(). 21230 */ 21231 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21232 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21233 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21234 !(ire->ire_flags & RTF_MULTIRT) && 21235 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21236 CONN_IS_LSO_MD_FASTPATH(connp)) { 21237 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21238 /* Cache the result */ 21239 connp->conn_lso_ok = B_TRUE; 21240 21241 ASSERT(ill->ill_lso_capab != NULL); 21242 if (!ill->ill_lso_capab->ill_lso_on) { 21243 ill->ill_lso_capab->ill_lso_on = 1; 21244 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21245 "LSO for interface %s\n", (void *)connp, 21246 ill->ill_name)); 21247 } 21248 tcp_lso_update(tcp, ill->ill_lso_capab); 21249 } else if (ipst->ips_ip_multidata_outbound && 21250 ILL_MDT_CAPABLE(ill)) { 21251 /* Cache the result */ 21252 connp->conn_mdt_ok = B_TRUE; 21253 21254 ASSERT(ill->ill_mdt_capab != NULL); 21255 if (!ill->ill_mdt_capab->ill_mdt_on) { 21256 ill->ill_mdt_capab->ill_mdt_on = 1; 21257 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21258 "MDT for interface %s\n", (void *)connp, 21259 ill->ill_name)); 21260 } 21261 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21262 } 21263 } 21264 21265 /* 21266 * The goal is to reduce the number of generated tcp segments by 21267 * setting the maxpsz multiplier to 0; this will have an affect on 21268 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21269 * into each packet, up to SMSS bytes. Doing this reduces the number 21270 * of outbound segments and incoming ACKs, thus allowing for better 21271 * network and system performance. In contrast the legacy behavior 21272 * may result in sending less than SMSS size, because the last mblk 21273 * for some packets may have more data than needed to make up SMSS, 21274 * and the legacy code refused to "split" it. 21275 * 21276 * We apply the new behavior on following situations: 21277 * 21278 * 1) Loopback connections, 21279 * 2) Connections in which the remote peer is not on local subnet, 21280 * 3) Local subnet connections over the bge interface (see below). 21281 * 21282 * Ideally, we would like this behavior to apply for interfaces other 21283 * than bge. However, doing so would negatively impact drivers which 21284 * perform dynamic mapping and unmapping of DMA resources, which are 21285 * increased by setting the maxpsz multiplier to 0 (more mblks per 21286 * packet will be generated by tcp). The bge driver does not suffer 21287 * from this, as it copies the mblks into pre-mapped buffers, and 21288 * therefore does not require more I/O resources than before. 21289 * 21290 * Otherwise, this behavior is present on all network interfaces when 21291 * the destination endpoint is non-local, since reducing the number 21292 * of packets in general is good for the network. 21293 * 21294 * TODO We need to remove this hard-coded conditional for bge once 21295 * a better "self-tuning" mechanism, or a way to comprehend 21296 * the driver transmit strategy is devised. Until the solution 21297 * is found and well understood, we live with this hack. 21298 */ 21299 if (!tcp_static_maxpsz && 21300 (tcp->tcp_loopback || !tcp->tcp_localnet || 21301 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21302 /* override the default value */ 21303 tcp->tcp_maxpsz = 0; 21304 21305 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21306 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21307 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21308 } 21309 21310 /* set the stream head parameters accordingly */ 21311 (void) tcp_maxpsz_set(tcp, B_TRUE); 21312 } 21313 21314 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21315 static void 21316 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21317 { 21318 uchar_t fval = *mp->b_rptr; 21319 mblk_t *tail; 21320 queue_t *q = tcp->tcp_wq; 21321 21322 /* TODO: How should flush interact with urgent data? */ 21323 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21324 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21325 /* 21326 * Flush only data that has not yet been put on the wire. If 21327 * we flush data that we have already transmitted, life, as we 21328 * know it, may come to an end. 21329 */ 21330 tail = tcp->tcp_xmit_tail; 21331 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21332 tcp->tcp_xmit_tail_unsent = 0; 21333 tcp->tcp_unsent = 0; 21334 if (tail->b_wptr != tail->b_rptr) 21335 tail = tail->b_cont; 21336 if (tail) { 21337 mblk_t **excess = &tcp->tcp_xmit_head; 21338 for (;;) { 21339 mblk_t *mp1 = *excess; 21340 if (mp1 == tail) 21341 break; 21342 tcp->tcp_xmit_tail = mp1; 21343 tcp->tcp_xmit_last = mp1; 21344 excess = &mp1->b_cont; 21345 } 21346 *excess = NULL; 21347 tcp_close_mpp(&tail); 21348 if (tcp->tcp_snd_zcopy_aware) 21349 tcp_zcopy_notify(tcp); 21350 } 21351 /* 21352 * We have no unsent data, so unsent must be less than 21353 * tcp_xmit_lowater, so re-enable flow. 21354 */ 21355 mutex_enter(&tcp->tcp_non_sq_lock); 21356 if (tcp->tcp_flow_stopped) { 21357 tcp_clrqfull(tcp); 21358 } 21359 mutex_exit(&tcp->tcp_non_sq_lock); 21360 } 21361 /* 21362 * TODO: you can't just flush these, you have to increase rwnd for one 21363 * thing. For another, how should urgent data interact? 21364 */ 21365 if (fval & FLUSHR) { 21366 *mp->b_rptr = fval & ~FLUSHW; 21367 /* XXX */ 21368 qreply(q, mp); 21369 return; 21370 } 21371 freemsg(mp); 21372 } 21373 21374 /* 21375 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21376 * messages. 21377 */ 21378 static void 21379 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21380 { 21381 mblk_t *mp1; 21382 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 21383 STRUCT_HANDLE(strbuf, sb); 21384 queue_t *q = tcp->tcp_wq; 21385 int error; 21386 uint_t addrlen; 21387 21388 /* Make sure it is one of ours. */ 21389 switch (iocp->ioc_cmd) { 21390 case TI_GETMYNAME: 21391 case TI_GETPEERNAME: 21392 break; 21393 default: 21394 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21395 return; 21396 } 21397 switch (mi_copy_state(q, mp, &mp1)) { 21398 case -1: 21399 return; 21400 case MI_COPY_CASE(MI_COPY_IN, 1): 21401 break; 21402 case MI_COPY_CASE(MI_COPY_OUT, 1): 21403 /* Copy out the strbuf. */ 21404 mi_copyout(q, mp); 21405 return; 21406 case MI_COPY_CASE(MI_COPY_OUT, 2): 21407 /* All done. */ 21408 mi_copy_done(q, mp, 0); 21409 return; 21410 default: 21411 mi_copy_done(q, mp, EPROTO); 21412 return; 21413 } 21414 /* Check alignment of the strbuf */ 21415 if (!OK_32PTR(mp1->b_rptr)) { 21416 mi_copy_done(q, mp, EINVAL); 21417 return; 21418 } 21419 21420 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 21421 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21422 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21423 mi_copy_done(q, mp, EINVAL); 21424 return; 21425 } 21426 21427 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21428 if (mp1 == NULL) 21429 return; 21430 21431 switch (iocp->ioc_cmd) { 21432 case TI_GETMYNAME: 21433 error = tcp_do_getsockname(tcp, (void *)mp1->b_rptr, &addrlen); 21434 break; 21435 case TI_GETPEERNAME: 21436 error = tcp_do_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 21437 break; 21438 } 21439 21440 if (error != 0) { 21441 mi_copy_done(q, mp, error); 21442 } else { 21443 mp1->b_wptr += addrlen; 21444 STRUCT_FSET(sb, len, addrlen); 21445 21446 /* Copy out the address */ 21447 mi_copyout(q, mp); 21448 } 21449 } 21450 21451 static void 21452 tcp_disable_direct_sockfs(tcp_t *tcp) 21453 { 21454 #ifdef _ILP32 21455 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 21456 #else 21457 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21458 #endif 21459 /* 21460 * Insert this socket into the acceptor hash. 21461 * We might need it for T_CONN_RES message 21462 */ 21463 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21464 21465 if (tcp->tcp_fused) { 21466 /* 21467 * This is a fused loopback tcp; disable 21468 * read-side synchronous streams interface 21469 * and drain any queued data. It is okay 21470 * to do this for non-synchronous streams 21471 * fused tcp as well. 21472 */ 21473 tcp_fuse_disable_pair(tcp, B_FALSE); 21474 } 21475 tcp->tcp_issocket = B_FALSE; 21476 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 21477 } 21478 21479 /* 21480 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21481 * messages. 21482 */ 21483 /* ARGSUSED */ 21484 static void 21485 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21486 { 21487 conn_t *connp = (conn_t *)arg; 21488 tcp_t *tcp = connp->conn_tcp; 21489 queue_t *q = tcp->tcp_wq; 21490 struct iocblk *iocp; 21491 21492 ASSERT(DB_TYPE(mp) == M_IOCTL); 21493 /* 21494 * Try and ASSERT the minimum possible references on the 21495 * conn early enough. Since we are executing on write side, 21496 * the connection is obviously not detached and that means 21497 * there is a ref each for TCP and IP. Since we are behind 21498 * the squeue, the minimum references needed are 3. If the 21499 * conn is in classifier hash list, there should be an 21500 * extra ref for that (we check both the possibilities). 21501 */ 21502 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21503 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21504 21505 iocp = (struct iocblk *)mp->b_rptr; 21506 switch (iocp->ioc_cmd) { 21507 case TCP_IOC_DEFAULT_Q: 21508 /* Wants to be the default wq. */ 21509 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21510 iocp->ioc_error = EPERM; 21511 iocp->ioc_count = 0; 21512 mp->b_datap->db_type = M_IOCACK; 21513 qreply(q, mp); 21514 return; 21515 } 21516 tcp_def_q_set(tcp, mp); 21517 return; 21518 case _SIOCSOCKFALLBACK: 21519 /* 21520 * Either sockmod is about to be popped and the socket 21521 * would now be treated as a plain stream, or a module 21522 * is about to be pushed so we could no longer use read- 21523 * side synchronous streams for fused loopback tcp. 21524 * Drain any queued data and disable direct sockfs 21525 * interface from now on. 21526 */ 21527 if (!tcp->tcp_issocket) { 21528 DB_TYPE(mp) = M_IOCNAK; 21529 iocp->ioc_error = EINVAL; 21530 } else { 21531 tcp_disable_direct_sockfs(tcp); 21532 DB_TYPE(mp) = M_IOCACK; 21533 iocp->ioc_error = 0; 21534 } 21535 iocp->ioc_count = 0; 21536 iocp->ioc_rval = 0; 21537 qreply(q, mp); 21538 return; 21539 } 21540 CALL_IP_WPUT(connp, q, mp); 21541 } 21542 21543 /* 21544 * This routine is called by tcp_wput() to handle all TPI requests. 21545 */ 21546 /* ARGSUSED */ 21547 static void 21548 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21549 { 21550 conn_t *connp = (conn_t *)arg; 21551 tcp_t *tcp = connp->conn_tcp; 21552 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21553 uchar_t *rptr; 21554 t_scalar_t type; 21555 cred_t *cr; 21556 21557 /* 21558 * Try and ASSERT the minimum possible references on the 21559 * conn early enough. Since we are executing on write side, 21560 * the connection is obviously not detached and that means 21561 * there is a ref each for TCP and IP. Since we are behind 21562 * the squeue, the minimum references needed are 3. If the 21563 * conn is in classifier hash list, there should be an 21564 * extra ref for that (we check both the possibilities). 21565 */ 21566 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21567 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21568 21569 rptr = mp->b_rptr; 21570 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21571 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 21572 type = ((union T_primitives *)rptr)->type; 21573 if (type == T_EXDATA_REQ) { 21574 tcp_output_urgent(connp, mp->b_cont, arg2); 21575 freeb(mp); 21576 } else if (type != T_DATA_REQ) { 21577 goto non_urgent_data; 21578 } else { 21579 /* TODO: options, flags, ... from user */ 21580 /* Set length to zero for reclamation below */ 21581 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 21582 freeb(mp); 21583 } 21584 return; 21585 } else { 21586 if (tcp->tcp_debug) { 21587 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21588 "tcp_wput_proto, dropping one..."); 21589 } 21590 freemsg(mp); 21591 return; 21592 } 21593 21594 non_urgent_data: 21595 21596 switch ((int)tprim->type) { 21597 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 21598 /* 21599 * save the kssl_ent_t from the next block, and convert this 21600 * back to a normal bind_req. 21601 */ 21602 if (mp->b_cont != NULL) { 21603 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 21604 21605 if (tcp->tcp_kssl_ent != NULL) { 21606 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 21607 KSSL_NO_PROXY); 21608 tcp->tcp_kssl_ent = NULL; 21609 } 21610 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 21611 sizeof (kssl_ent_t)); 21612 kssl_hold_ent(tcp->tcp_kssl_ent); 21613 freemsg(mp->b_cont); 21614 mp->b_cont = NULL; 21615 } 21616 tprim->type = T_BIND_REQ; 21617 21618 /* FALLTHROUGH */ 21619 case O_T_BIND_REQ: /* bind request */ 21620 case T_BIND_REQ: /* new semantics bind request */ 21621 tcp_tpi_bind(tcp, mp); 21622 break; 21623 case T_UNBIND_REQ: /* unbind request */ 21624 tcp_tpi_unbind(tcp, mp); 21625 break; 21626 case O_T_CONN_RES: /* old connection response XXX */ 21627 case T_CONN_RES: /* connection response */ 21628 tcp_tli_accept(tcp, mp); 21629 break; 21630 case T_CONN_REQ: /* connection request */ 21631 tcp_tpi_connect(tcp, mp); 21632 break; 21633 case T_DISCON_REQ: /* disconnect request */ 21634 tcp_disconnect(tcp, mp); 21635 break; 21636 case T_CAPABILITY_REQ: 21637 tcp_capability_req(tcp, mp); /* capability request */ 21638 break; 21639 case T_INFO_REQ: /* information request */ 21640 tcp_info_req(tcp, mp); 21641 break; 21642 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 21643 case T_OPTMGMT_REQ: 21644 /* 21645 * Note: no support for snmpcom_req() through new 21646 * T_OPTMGMT_REQ. See comments in ip.c 21647 */ 21648 21649 /* 21650 * All Solaris components should pass a db_credp 21651 * for this TPI message, hence we ASSERT. 21652 * But in case there is some other M_PROTO that looks 21653 * like a TPI message sent by some other kernel 21654 * component, we check and return an error. 21655 */ 21656 cr = msg_getcred(mp, NULL); 21657 ASSERT(cr != NULL); 21658 if (cr == NULL) { 21659 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 21660 return; 21661 } 21662 /* 21663 * If EINPROGRESS is returned, the request has been queued 21664 * for subsequent processing by ip_restart_optmgmt(), which 21665 * will do the CONN_DEC_REF(). 21666 */ 21667 CONN_INC_REF(connp); 21668 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) { 21669 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21670 B_TRUE) != EINPROGRESS) { 21671 CONN_DEC_REF(connp); 21672 } 21673 } else { 21674 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21675 B_TRUE) != EINPROGRESS) { 21676 CONN_DEC_REF(connp); 21677 } 21678 } 21679 break; 21680 21681 case T_UNITDATA_REQ: /* unitdata request */ 21682 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21683 break; 21684 case T_ORDREL_REQ: /* orderly release req */ 21685 freemsg(mp); 21686 21687 if (tcp->tcp_fused) 21688 tcp_unfuse(tcp); 21689 21690 if (tcp_xmit_end(tcp) != 0) { 21691 /* 21692 * We were crossing FINs and got a reset from 21693 * the other side. Just ignore it. 21694 */ 21695 if (tcp->tcp_debug) { 21696 (void) strlog(TCP_MOD_ID, 0, 1, 21697 SL_ERROR|SL_TRACE, 21698 "tcp_wput_proto, T_ORDREL_REQ out of " 21699 "state %s", 21700 tcp_display(tcp, NULL, 21701 DISP_ADDR_AND_PORT)); 21702 } 21703 } 21704 break; 21705 case T_ADDR_REQ: 21706 tcp_addr_req(tcp, mp); 21707 break; 21708 default: 21709 if (tcp->tcp_debug) { 21710 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21711 "tcp_wput_proto, bogus TPI msg, type %d", 21712 tprim->type); 21713 } 21714 /* 21715 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 21716 * to recover. 21717 */ 21718 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21719 break; 21720 } 21721 } 21722 21723 /* 21724 * The TCP write service routine should never be called... 21725 */ 21726 /* ARGSUSED */ 21727 static void 21728 tcp_wsrv(queue_t *q) 21729 { 21730 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 21731 21732 TCP_STAT(tcps, tcp_wsrv_called); 21733 } 21734 21735 /* Non overlapping byte exchanger */ 21736 static void 21737 tcp_xchg(uchar_t *a, uchar_t *b, int len) 21738 { 21739 uchar_t uch; 21740 21741 while (len-- > 0) { 21742 uch = a[len]; 21743 a[len] = b[len]; 21744 b[len] = uch; 21745 } 21746 } 21747 21748 /* 21749 * Send out a control packet on the tcp connection specified. This routine 21750 * is typically called where we need a simple ACK or RST generated. 21751 */ 21752 static void 21753 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 21754 { 21755 uchar_t *rptr; 21756 tcph_t *tcph; 21757 ipha_t *ipha = NULL; 21758 ip6_t *ip6h = NULL; 21759 uint32_t sum; 21760 int tcp_hdr_len; 21761 int tcp_ip_hdr_len; 21762 mblk_t *mp; 21763 tcp_stack_t *tcps = tcp->tcp_tcps; 21764 21765 /* 21766 * Save sum for use in source route later. 21767 */ 21768 ASSERT(tcp != NULL); 21769 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 21770 tcp_hdr_len = tcp->tcp_hdr_len; 21771 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 21772 21773 /* If a text string is passed in with the request, pass it to strlog. */ 21774 if (str != NULL && tcp->tcp_debug) { 21775 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21776 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 21777 str, seq, ack, ctl); 21778 } 21779 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 21780 BPRI_MED); 21781 if (mp == NULL) { 21782 return; 21783 } 21784 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 21785 mp->b_rptr = rptr; 21786 mp->b_wptr = &rptr[tcp_hdr_len]; 21787 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 21788 21789 if (tcp->tcp_ipversion == IPV4_VERSION) { 21790 ipha = (ipha_t *)rptr; 21791 ipha->ipha_length = htons(tcp_hdr_len); 21792 } else { 21793 ip6h = (ip6_t *)rptr; 21794 ASSERT(tcp != NULL); 21795 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 21796 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21797 } 21798 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 21799 tcph->th_flags[0] = (uint8_t)ctl; 21800 if (ctl & TH_RST) { 21801 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 21802 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 21803 /* 21804 * Don't send TSopt w/ TH_RST packets per RFC 1323. 21805 */ 21806 if (tcp->tcp_snd_ts_ok && 21807 tcp->tcp_state > TCPS_SYN_SENT) { 21808 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 21809 *(mp->b_wptr) = TCPOPT_EOL; 21810 if (tcp->tcp_ipversion == IPV4_VERSION) { 21811 ipha->ipha_length = htons(tcp_hdr_len - 21812 TCPOPT_REAL_TS_LEN); 21813 } else { 21814 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 21815 TCPOPT_REAL_TS_LEN); 21816 } 21817 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 21818 sum -= TCPOPT_REAL_TS_LEN; 21819 } 21820 } 21821 if (ctl & TH_ACK) { 21822 if (tcp->tcp_snd_ts_ok) { 21823 U32_TO_BE32(lbolt, 21824 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 21825 U32_TO_BE32(tcp->tcp_ts_recent, 21826 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 21827 } 21828 21829 /* Update the latest receive window size in TCP header. */ 21830 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21831 tcph->th_win); 21832 tcp->tcp_rack = ack; 21833 tcp->tcp_rack_cnt = 0; 21834 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 21835 } 21836 BUMP_LOCAL(tcp->tcp_obsegs); 21837 U32_TO_BE32(seq, tcph->th_seq); 21838 U32_TO_BE32(ack, tcph->th_ack); 21839 /* 21840 * Include the adjustment for a source route if any. 21841 */ 21842 sum = (sum >> 16) + (sum & 0xFFFF); 21843 U16_TO_BE16(sum, tcph->th_sum); 21844 tcp_send_data(tcp, tcp->tcp_wq, mp); 21845 } 21846 21847 /* 21848 * If this routine returns B_TRUE, TCP can generate a RST in response 21849 * to a segment. If it returns B_FALSE, TCP should not respond. 21850 */ 21851 static boolean_t 21852 tcp_send_rst_chk(tcp_stack_t *tcps) 21853 { 21854 clock_t now; 21855 21856 /* 21857 * TCP needs to protect itself from generating too many RSTs. 21858 * This can be a DoS attack by sending us random segments 21859 * soliciting RSTs. 21860 * 21861 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 21862 * in each 1 second interval. In this way, TCP still generate 21863 * RSTs in normal cases but when under attack, the impact is 21864 * limited. 21865 */ 21866 if (tcps->tcps_rst_sent_rate_enabled != 0) { 21867 now = lbolt; 21868 /* lbolt can wrap around. */ 21869 if ((tcps->tcps_last_rst_intrvl > now) || 21870 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 21871 1*SECONDS)) { 21872 tcps->tcps_last_rst_intrvl = now; 21873 tcps->tcps_rst_cnt = 1; 21874 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 21875 return (B_FALSE); 21876 } 21877 } 21878 return (B_TRUE); 21879 } 21880 21881 /* 21882 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 21883 */ 21884 static void 21885 tcp_ip_ire_mark_advice(tcp_t *tcp) 21886 { 21887 mblk_t *mp; 21888 ipic_t *ipic; 21889 21890 if (tcp->tcp_ipversion == IPV4_VERSION) { 21891 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21892 &ipic); 21893 } else { 21894 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21895 &ipic); 21896 } 21897 if (mp == NULL) 21898 return; 21899 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21900 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21901 } 21902 21903 /* 21904 * Return an IP advice ioctl mblk and set ipic to be the pointer 21905 * to the advice structure. 21906 */ 21907 static mblk_t * 21908 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 21909 { 21910 struct iocblk *ioc; 21911 mblk_t *mp, *mp1; 21912 21913 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 21914 if (mp == NULL) 21915 return (NULL); 21916 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 21917 *ipic = (ipic_t *)mp->b_rptr; 21918 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 21919 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 21920 21921 bcopy(addr, *ipic + 1, addr_len); 21922 21923 (*ipic)->ipic_addr_length = addr_len; 21924 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 21925 21926 mp1 = mkiocb(IP_IOCTL); 21927 if (mp1 == NULL) { 21928 freemsg(mp); 21929 return (NULL); 21930 } 21931 mp1->b_cont = mp; 21932 ioc = (struct iocblk *)mp1->b_rptr; 21933 ioc->ioc_count = sizeof (ipic_t) + addr_len; 21934 21935 return (mp1); 21936 } 21937 21938 /* 21939 * Generate a reset based on an inbound packet, connp is set by caller 21940 * when RST is in response to an unexpected inbound packet for which 21941 * there is active tcp state in the system. 21942 * 21943 * IPSEC NOTE : Try to send the reply with the same protection as it came 21944 * in. We still have the ipsec_mp that the packet was attached to. Thus 21945 * the packet will go out at the same level of protection as it came in by 21946 * converting the IPSEC_IN to IPSEC_OUT. 21947 */ 21948 static void 21949 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 21950 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 21951 tcp_stack_t *tcps, conn_t *connp) 21952 { 21953 ipha_t *ipha = NULL; 21954 ip6_t *ip6h = NULL; 21955 ushort_t len; 21956 tcph_t *tcph; 21957 int i; 21958 mblk_t *ipsec_mp; 21959 boolean_t mctl_present; 21960 ipic_t *ipic; 21961 ipaddr_t v4addr; 21962 in6_addr_t v6addr; 21963 int addr_len; 21964 void *addr; 21965 queue_t *q = tcps->tcps_g_q; 21966 tcp_t *tcp; 21967 cred_t *cr; 21968 mblk_t *nmp; 21969 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21970 21971 if (tcps->tcps_g_q == NULL) { 21972 /* 21973 * For non-zero stackids the default queue isn't created 21974 * until the first open, thus there can be a need to send 21975 * a reset before then. But we can't do that, hence we just 21976 * drop the packet. Later during boot, when the default queue 21977 * has been setup, a retransmitted packet from the peer 21978 * will result in a reset. 21979 */ 21980 ASSERT(tcps->tcps_netstack->netstack_stackid != 21981 GLOBAL_NETSTACKID); 21982 freemsg(mp); 21983 return; 21984 } 21985 21986 if (connp != NULL) 21987 tcp = connp->conn_tcp; 21988 else 21989 tcp = Q_TO_TCP(q); 21990 21991 if (!tcp_send_rst_chk(tcps)) { 21992 tcps->tcps_rst_unsent++; 21993 freemsg(mp); 21994 return; 21995 } 21996 21997 if (mp->b_datap->db_type == M_CTL) { 21998 ipsec_mp = mp; 21999 mp = mp->b_cont; 22000 mctl_present = B_TRUE; 22001 } else { 22002 ipsec_mp = mp; 22003 mctl_present = B_FALSE; 22004 } 22005 22006 if (str && q && tcps->tcps_dbg) { 22007 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22008 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22009 "flags 0x%x", 22010 str, seq, ack, ctl); 22011 } 22012 if (mp->b_datap->db_ref != 1) { 22013 mblk_t *mp1 = copyb(mp); 22014 freemsg(mp); 22015 mp = mp1; 22016 if (!mp) { 22017 if (mctl_present) 22018 freeb(ipsec_mp); 22019 return; 22020 } else { 22021 if (mctl_present) { 22022 ipsec_mp->b_cont = mp; 22023 } else { 22024 ipsec_mp = mp; 22025 } 22026 } 22027 } else if (mp->b_cont) { 22028 freemsg(mp->b_cont); 22029 mp->b_cont = NULL; 22030 } 22031 /* 22032 * We skip reversing source route here. 22033 * (for now we replace all IP options with EOL) 22034 */ 22035 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22036 ipha = (ipha_t *)mp->b_rptr; 22037 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22038 mp->b_rptr[i] = IPOPT_EOL; 22039 /* 22040 * Make sure that src address isn't flagrantly invalid. 22041 * Not all broadcast address checking for the src address 22042 * is possible, since we don't know the netmask of the src 22043 * addr. No check for destination address is done, since 22044 * IP will not pass up a packet with a broadcast dest 22045 * address to TCP. Similar checks are done below for IPv6. 22046 */ 22047 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22048 CLASSD(ipha->ipha_src)) { 22049 freemsg(ipsec_mp); 22050 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22051 return; 22052 } 22053 } else { 22054 ip6h = (ip6_t *)mp->b_rptr; 22055 22056 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22057 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22058 freemsg(ipsec_mp); 22059 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22060 return; 22061 } 22062 22063 /* Remove any extension headers assuming partial overlay */ 22064 if (ip_hdr_len > IPV6_HDR_LEN) { 22065 uint8_t *to; 22066 22067 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22068 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22069 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22070 ip_hdr_len = IPV6_HDR_LEN; 22071 ip6h = (ip6_t *)mp->b_rptr; 22072 ip6h->ip6_nxt = IPPROTO_TCP; 22073 } 22074 } 22075 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22076 if (tcph->th_flags[0] & TH_RST) { 22077 freemsg(ipsec_mp); 22078 return; 22079 } 22080 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22081 len = ip_hdr_len + sizeof (tcph_t); 22082 mp->b_wptr = &mp->b_rptr[len]; 22083 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22084 ipha->ipha_length = htons(len); 22085 /* Swap addresses */ 22086 v4addr = ipha->ipha_src; 22087 ipha->ipha_src = ipha->ipha_dst; 22088 ipha->ipha_dst = v4addr; 22089 ipha->ipha_ident = 0; 22090 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22091 addr_len = IP_ADDR_LEN; 22092 addr = &v4addr; 22093 } else { 22094 /* No ip6i_t in this case */ 22095 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22096 /* Swap addresses */ 22097 v6addr = ip6h->ip6_src; 22098 ip6h->ip6_src = ip6h->ip6_dst; 22099 ip6h->ip6_dst = v6addr; 22100 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22101 addr_len = IPV6_ADDR_LEN; 22102 addr = &v6addr; 22103 } 22104 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22105 U32_TO_BE32(ack, tcph->th_ack); 22106 U32_TO_BE32(seq, tcph->th_seq); 22107 U16_TO_BE16(0, tcph->th_win); 22108 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22109 tcph->th_flags[0] = (uint8_t)ctl; 22110 if (ctl & TH_RST) { 22111 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22112 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22113 } 22114 22115 /* IP trusts us to set up labels when required. */ 22116 if (is_system_labeled() && (cr = msg_getcred(mp, NULL)) != NULL && 22117 crgetlabel(cr) != NULL) { 22118 int err; 22119 22120 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22121 err = tsol_check_label(cr, &mp, 22122 tcp->tcp_connp->conn_mac_exempt, 22123 tcps->tcps_netstack->netstack_ip); 22124 else 22125 err = tsol_check_label_v6(cr, &mp, 22126 tcp->tcp_connp->conn_mac_exempt, 22127 tcps->tcps_netstack->netstack_ip); 22128 if (mctl_present) 22129 ipsec_mp->b_cont = mp; 22130 else 22131 ipsec_mp = mp; 22132 if (err != 0) { 22133 freemsg(ipsec_mp); 22134 return; 22135 } 22136 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22137 ipha = (ipha_t *)mp->b_rptr; 22138 } else { 22139 ip6h = (ip6_t *)mp->b_rptr; 22140 } 22141 } 22142 22143 if (mctl_present) { 22144 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22145 22146 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22147 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22148 return; 22149 } 22150 } 22151 if (zoneid == ALL_ZONES) 22152 zoneid = GLOBAL_ZONEID; 22153 22154 /* Add the zoneid so ip_output routes it properly */ 22155 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22156 freemsg(ipsec_mp); 22157 return; 22158 } 22159 ipsec_mp = nmp; 22160 22161 /* 22162 * NOTE: one might consider tracing a TCP packet here, but 22163 * this function has no active TCP state and no tcp structure 22164 * that has a trace buffer. If we traced here, we would have 22165 * to keep a local trace buffer in tcp_record_trace(). 22166 * 22167 * TSol note: The mblk that contains the incoming packet was 22168 * reused by tcp_xmit_listener_reset, so it already contains 22169 * the right credentials and we don't need to call mblk_setcred. 22170 * Also the conn's cred is not right since it is associated 22171 * with tcps_g_q. 22172 */ 22173 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22174 22175 /* 22176 * Tell IP to mark the IRE used for this destination temporary. 22177 * This way, we can limit our exposure to DoS attack because IP 22178 * creates an IRE for each destination. If there are too many, 22179 * the time to do any routing lookup will be extremely long. And 22180 * the lookup can be in interrupt context. 22181 * 22182 * Note that in normal circumstances, this marking should not 22183 * affect anything. It would be nice if only 1 message is 22184 * needed to inform IP that the IRE created for this RST should 22185 * not be added to the cache table. But there is currently 22186 * not such communication mechanism between TCP and IP. So 22187 * the best we can do now is to send the advice ioctl to IP 22188 * to mark the IRE temporary. 22189 */ 22190 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22191 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22192 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22193 } 22194 } 22195 22196 /* 22197 * Initiate closedown sequence on an active connection. (May be called as 22198 * writer.) Return value zero for OK return, non-zero for error return. 22199 */ 22200 static int 22201 tcp_xmit_end(tcp_t *tcp) 22202 { 22203 ipic_t *ipic; 22204 mblk_t *mp; 22205 tcp_stack_t *tcps = tcp->tcp_tcps; 22206 22207 if (tcp->tcp_state < TCPS_SYN_RCVD || 22208 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22209 /* 22210 * Invalid state, only states TCPS_SYN_RCVD, 22211 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22212 */ 22213 return (-1); 22214 } 22215 22216 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22217 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22218 /* 22219 * If there is nothing more unsent, send the FIN now. 22220 * Otherwise, it will go out with the last segment. 22221 */ 22222 if (tcp->tcp_unsent == 0) { 22223 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22224 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22225 22226 if (mp) { 22227 tcp_send_data(tcp, tcp->tcp_wq, mp); 22228 } else { 22229 /* 22230 * Couldn't allocate msg. Pretend we got it out. 22231 * Wait for rexmit timeout. 22232 */ 22233 tcp->tcp_snxt = tcp->tcp_fss + 1; 22234 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22235 } 22236 22237 /* 22238 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22239 * changed. 22240 */ 22241 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22242 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22243 } 22244 } else { 22245 /* 22246 * If tcp->tcp_cork is set, then the data will not get sent, 22247 * so we have to check that and unset it first. 22248 */ 22249 if (tcp->tcp_cork) 22250 tcp->tcp_cork = B_FALSE; 22251 tcp_wput_data(tcp, NULL, B_FALSE); 22252 } 22253 22254 /* 22255 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22256 * is 0, don't update the cache. 22257 */ 22258 if (tcps->tcps_rtt_updates == 0 || 22259 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22260 return (0); 22261 22262 /* 22263 * NOTE: should not update if source routes i.e. if tcp_remote if 22264 * different from the destination. 22265 */ 22266 if (tcp->tcp_ipversion == IPV4_VERSION) { 22267 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22268 return (0); 22269 } 22270 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22271 &ipic); 22272 } else { 22273 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22274 &tcp->tcp_ip6h->ip6_dst))) { 22275 return (0); 22276 } 22277 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22278 &ipic); 22279 } 22280 22281 /* Record route attributes in the IRE for use by future connections. */ 22282 if (mp == NULL) 22283 return (0); 22284 22285 /* 22286 * We do not have a good algorithm to update ssthresh at this time. 22287 * So don't do any update. 22288 */ 22289 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22290 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22291 22292 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22293 22294 return (0); 22295 } 22296 22297 /* ARGSUSED */ 22298 void 22299 tcp_xmit_reset(void *arg, mblk_t *mp, void *arg2) 22300 { 22301 conn_t *connp = (conn_t *)arg; 22302 mblk_t *mp1; 22303 tcp_t *tcp = connp->conn_tcp; 22304 tcp_xmit_reset_event_t *eventp; 22305 22306 ASSERT(mp->b_datap->db_type == M_PROTO && 22307 MBLKL(mp) == sizeof (tcp_xmit_reset_event_t)); 22308 22309 if (tcp->tcp_state != TCPS_LISTEN) { 22310 freemsg(mp); 22311 return; 22312 } 22313 22314 mp1 = mp->b_cont; 22315 mp->b_cont = NULL; 22316 eventp = (tcp_xmit_reset_event_t *)mp->b_rptr; 22317 ASSERT(eventp->tcp_xre_tcps->tcps_netstack == 22318 connp->conn_netstack); 22319 22320 tcp_xmit_listeners_reset(mp1, eventp->tcp_xre_iphdrlen, 22321 eventp->tcp_xre_zoneid, eventp->tcp_xre_tcps, connp); 22322 freemsg(mp); 22323 } 22324 22325 /* 22326 * Generate a "no listener here" RST in response to an "unknown" segment. 22327 * connp is set by caller when RST is in response to an unexpected 22328 * inbound packet for which there is active tcp state in the system. 22329 * Note that we are reusing the incoming mp to construct the outgoing RST. 22330 */ 22331 void 22332 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22333 tcp_stack_t *tcps, conn_t *connp) 22334 { 22335 uchar_t *rptr; 22336 uint32_t seg_len; 22337 tcph_t *tcph; 22338 uint32_t seg_seq; 22339 uint32_t seg_ack; 22340 uint_t flags; 22341 mblk_t *ipsec_mp; 22342 ipha_t *ipha; 22343 ip6_t *ip6h; 22344 boolean_t mctl_present = B_FALSE; 22345 boolean_t check = B_TRUE; 22346 boolean_t policy_present; 22347 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22348 22349 TCP_STAT(tcps, tcp_no_listener); 22350 22351 ipsec_mp = mp; 22352 22353 if (mp->b_datap->db_type == M_CTL) { 22354 ipsec_in_t *ii; 22355 22356 mctl_present = B_TRUE; 22357 mp = mp->b_cont; 22358 22359 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22360 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22361 if (ii->ipsec_in_dont_check) { 22362 check = B_FALSE; 22363 if (!ii->ipsec_in_secure) { 22364 freeb(ipsec_mp); 22365 mctl_present = B_FALSE; 22366 ipsec_mp = mp; 22367 } 22368 } 22369 } 22370 22371 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22372 policy_present = ipss->ipsec_inbound_v4_policy_present; 22373 ipha = (ipha_t *)mp->b_rptr; 22374 ip6h = NULL; 22375 } else { 22376 policy_present = ipss->ipsec_inbound_v6_policy_present; 22377 ipha = NULL; 22378 ip6h = (ip6_t *)mp->b_rptr; 22379 } 22380 22381 if (check && policy_present) { 22382 /* 22383 * The conn_t parameter is NULL because we already know 22384 * nobody's home. 22385 */ 22386 ipsec_mp = ipsec_check_global_policy( 22387 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22388 tcps->tcps_netstack); 22389 if (ipsec_mp == NULL) 22390 return; 22391 } 22392 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22393 DTRACE_PROBE2( 22394 tx__ip__log__error__nolistener__tcp, 22395 char *, "Could not reply with RST to mp(1)", 22396 mblk_t *, mp); 22397 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22398 freemsg(ipsec_mp); 22399 return; 22400 } 22401 22402 rptr = mp->b_rptr; 22403 22404 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22405 seg_seq = BE32_TO_U32(tcph->th_seq); 22406 seg_ack = BE32_TO_U32(tcph->th_ack); 22407 flags = tcph->th_flags[0]; 22408 22409 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22410 if (flags & TH_RST) { 22411 freemsg(ipsec_mp); 22412 } else if (flags & TH_ACK) { 22413 tcp_xmit_early_reset("no tcp, reset", 22414 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 22415 connp); 22416 } else { 22417 if (flags & TH_SYN) { 22418 seg_len++; 22419 } else { 22420 /* 22421 * Here we violate the RFC. Note that a normal 22422 * TCP will never send a segment without the ACK 22423 * flag, except for RST or SYN segment. This 22424 * segment is neither. Just drop it on the 22425 * floor. 22426 */ 22427 freemsg(ipsec_mp); 22428 tcps->tcps_rst_unsent++; 22429 return; 22430 } 22431 22432 tcp_xmit_early_reset("no tcp, reset/ack", 22433 ipsec_mp, 0, seg_seq + seg_len, 22434 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 22435 } 22436 } 22437 22438 /* 22439 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22440 * ip and tcp header ready to pass down to IP. If the mp passed in is 22441 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22442 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22443 * otherwise it will dup partial mblks.) 22444 * Otherwise, an appropriate ACK packet will be generated. This 22445 * routine is not usually called to send new data for the first time. It 22446 * is mostly called out of the timer for retransmits, and to generate ACKs. 22447 * 22448 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22449 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22450 * of the original mblk chain will be returned in *offset and *end_mp. 22451 */ 22452 mblk_t * 22453 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22454 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22455 boolean_t rexmit) 22456 { 22457 int data_length; 22458 int32_t off = 0; 22459 uint_t flags; 22460 mblk_t *mp1; 22461 mblk_t *mp2; 22462 uchar_t *rptr; 22463 tcph_t *tcph; 22464 int32_t num_sack_blk = 0; 22465 int32_t sack_opt_len = 0; 22466 tcp_stack_t *tcps = tcp->tcp_tcps; 22467 22468 /* Allocate for our maximum TCP header + link-level */ 22469 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22470 tcps->tcps_wroff_xtra, BPRI_MED); 22471 if (!mp1) 22472 return (NULL); 22473 data_length = 0; 22474 22475 /* 22476 * Note that tcp_mss has been adjusted to take into account the 22477 * timestamp option if applicable. Because SACK options do not 22478 * appear in every TCP segments and they are of variable lengths, 22479 * they cannot be included in tcp_mss. Thus we need to calculate 22480 * the actual segment length when we need to send a segment which 22481 * includes SACK options. 22482 */ 22483 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22484 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22485 tcp->tcp_num_sack_blk); 22486 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22487 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22488 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22489 max_to_send -= sack_opt_len; 22490 } 22491 22492 if (offset != NULL) { 22493 off = *offset; 22494 /* We use offset as an indicator that end_mp is not NULL. */ 22495 *end_mp = NULL; 22496 } 22497 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22498 /* This could be faster with cooperation from downstream */ 22499 if (mp2 != mp1 && !sendall && 22500 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22501 max_to_send) 22502 /* 22503 * Don't send the next mblk since the whole mblk 22504 * does not fit. 22505 */ 22506 break; 22507 mp2->b_cont = dupb(mp); 22508 mp2 = mp2->b_cont; 22509 if (!mp2) { 22510 freemsg(mp1); 22511 return (NULL); 22512 } 22513 mp2->b_rptr += off; 22514 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22515 (uintptr_t)INT_MAX); 22516 22517 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22518 if (data_length > max_to_send) { 22519 mp2->b_wptr -= data_length - max_to_send; 22520 data_length = max_to_send; 22521 off = mp2->b_wptr - mp->b_rptr; 22522 break; 22523 } else { 22524 off = 0; 22525 } 22526 } 22527 if (offset != NULL) { 22528 *offset = off; 22529 *end_mp = mp; 22530 } 22531 if (seg_len != NULL) { 22532 *seg_len = data_length; 22533 } 22534 22535 /* Update the latest receive window size in TCP header. */ 22536 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22537 tcp->tcp_tcph->th_win); 22538 22539 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22540 mp1->b_rptr = rptr; 22541 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22542 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22543 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22544 U32_TO_ABE32(seq, tcph->th_seq); 22545 22546 /* 22547 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22548 * that this function was called from tcp_wput_data. Thus, when called 22549 * to retransmit data the setting of the PUSH bit may appear some 22550 * what random in that it might get set when it should not. This 22551 * should not pose any performance issues. 22552 */ 22553 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22554 tcp->tcp_unsent == data_length)) { 22555 flags = TH_ACK | TH_PUSH; 22556 } else { 22557 flags = TH_ACK; 22558 } 22559 22560 if (tcp->tcp_ecn_ok) { 22561 if (tcp->tcp_ecn_echo_on) 22562 flags |= TH_ECE; 22563 22564 /* 22565 * Only set ECT bit and ECN_CWR if a segment contains new data. 22566 * There is no TCP flow control for non-data segments, and 22567 * only data segment is transmitted reliably. 22568 */ 22569 if (data_length > 0 && !rexmit) { 22570 SET_ECT(tcp, rptr); 22571 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 22572 flags |= TH_CWR; 22573 tcp->tcp_ecn_cwr_sent = B_TRUE; 22574 } 22575 } 22576 } 22577 22578 if (tcp->tcp_valid_bits) { 22579 uint32_t u1; 22580 22581 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 22582 seq == tcp->tcp_iss) { 22583 uchar_t *wptr; 22584 22585 /* 22586 * If TCP_ISS_VALID and the seq number is tcp_iss, 22587 * TCP can only be in SYN-SENT, SYN-RCVD or 22588 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 22589 * our SYN is not ack'ed but the app closes this 22590 * TCP connection. 22591 */ 22592 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 22593 tcp->tcp_state == TCPS_SYN_RCVD || 22594 tcp->tcp_state == TCPS_FIN_WAIT_1); 22595 22596 /* 22597 * Tack on the MSS option. It is always needed 22598 * for both active and passive open. 22599 * 22600 * MSS option value should be interface MTU - MIN 22601 * TCP/IP header according to RFC 793 as it means 22602 * the maximum segment size TCP can receive. But 22603 * to get around some broken middle boxes/end hosts 22604 * out there, we allow the option value to be the 22605 * same as the MSS option size on the peer side. 22606 * In this way, the other side will not send 22607 * anything larger than they can receive. 22608 * 22609 * Note that for SYN_SENT state, the ndd param 22610 * tcp_use_smss_as_mss_opt has no effect as we 22611 * don't know the peer's MSS option value. So 22612 * the only case we need to take care of is in 22613 * SYN_RCVD state, which is done later. 22614 */ 22615 wptr = mp1->b_wptr; 22616 wptr[0] = TCPOPT_MAXSEG; 22617 wptr[1] = TCPOPT_MAXSEG_LEN; 22618 wptr += 2; 22619 u1 = tcp->tcp_if_mtu - 22620 (tcp->tcp_ipversion == IPV4_VERSION ? 22621 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 22622 TCP_MIN_HEADER_LENGTH; 22623 U16_TO_BE16(u1, wptr); 22624 mp1->b_wptr = wptr + 2; 22625 /* Update the offset to cover the additional word */ 22626 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22627 22628 /* 22629 * Note that the following way of filling in 22630 * TCP options are not optimal. Some NOPs can 22631 * be saved. But there is no need at this time 22632 * to optimize it. When it is needed, we will 22633 * do it. 22634 */ 22635 switch (tcp->tcp_state) { 22636 case TCPS_SYN_SENT: 22637 flags = TH_SYN; 22638 22639 if (tcp->tcp_snd_ts_ok) { 22640 uint32_t llbolt = (uint32_t)lbolt; 22641 22642 wptr = mp1->b_wptr; 22643 wptr[0] = TCPOPT_NOP; 22644 wptr[1] = TCPOPT_NOP; 22645 wptr[2] = TCPOPT_TSTAMP; 22646 wptr[3] = TCPOPT_TSTAMP_LEN; 22647 wptr += 4; 22648 U32_TO_BE32(llbolt, wptr); 22649 wptr += 4; 22650 ASSERT(tcp->tcp_ts_recent == 0); 22651 U32_TO_BE32(0L, wptr); 22652 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 22653 tcph->th_offset_and_rsrvd[0] += 22654 (3 << 4); 22655 } 22656 22657 /* 22658 * Set up all the bits to tell other side 22659 * we are ECN capable. 22660 */ 22661 if (tcp->tcp_ecn_ok) { 22662 flags |= (TH_ECE | TH_CWR); 22663 } 22664 break; 22665 case TCPS_SYN_RCVD: 22666 flags |= TH_SYN; 22667 22668 /* 22669 * Reset the MSS option value to be SMSS 22670 * We should probably add back the bytes 22671 * for timestamp option and IPsec. We 22672 * don't do that as this is a workaround 22673 * for broken middle boxes/end hosts, it 22674 * is better for us to be more cautious. 22675 * They may not take these things into 22676 * account in their SMSS calculation. Thus 22677 * the peer's calculated SMSS may be smaller 22678 * than what it can be. This should be OK. 22679 */ 22680 if (tcps->tcps_use_smss_as_mss_opt) { 22681 u1 = tcp->tcp_mss; 22682 U16_TO_BE16(u1, wptr); 22683 } 22684 22685 /* 22686 * If the other side is ECN capable, reply 22687 * that we are also ECN capable. 22688 */ 22689 if (tcp->tcp_ecn_ok) 22690 flags |= TH_ECE; 22691 break; 22692 default: 22693 /* 22694 * The above ASSERT() makes sure that this 22695 * must be FIN-WAIT-1 state. Our SYN has 22696 * not been ack'ed so retransmit it. 22697 */ 22698 flags |= TH_SYN; 22699 break; 22700 } 22701 22702 if (tcp->tcp_snd_ws_ok) { 22703 wptr = mp1->b_wptr; 22704 wptr[0] = TCPOPT_NOP; 22705 wptr[1] = TCPOPT_WSCALE; 22706 wptr[2] = TCPOPT_WS_LEN; 22707 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 22708 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 22709 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22710 } 22711 22712 if (tcp->tcp_snd_sack_ok) { 22713 wptr = mp1->b_wptr; 22714 wptr[0] = TCPOPT_NOP; 22715 wptr[1] = TCPOPT_NOP; 22716 wptr[2] = TCPOPT_SACK_PERMITTED; 22717 wptr[3] = TCPOPT_SACK_OK_LEN; 22718 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 22719 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22720 } 22721 22722 /* allocb() of adequate mblk assures space */ 22723 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 22724 (uintptr_t)INT_MAX); 22725 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 22726 /* 22727 * Get IP set to checksum on our behalf 22728 * Include the adjustment for a source route if any. 22729 */ 22730 u1 += tcp->tcp_sum; 22731 u1 = (u1 >> 16) + (u1 & 0xFFFF); 22732 U16_TO_BE16(u1, tcph->th_sum); 22733 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22734 } 22735 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 22736 (seq + data_length) == tcp->tcp_fss) { 22737 if (!tcp->tcp_fin_acked) { 22738 flags |= TH_FIN; 22739 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22740 } 22741 if (!tcp->tcp_fin_sent) { 22742 tcp->tcp_fin_sent = B_TRUE; 22743 switch (tcp->tcp_state) { 22744 case TCPS_SYN_RCVD: 22745 case TCPS_ESTABLISHED: 22746 tcp->tcp_state = TCPS_FIN_WAIT_1; 22747 break; 22748 case TCPS_CLOSE_WAIT: 22749 tcp->tcp_state = TCPS_LAST_ACK; 22750 break; 22751 } 22752 if (tcp->tcp_suna == tcp->tcp_snxt) 22753 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22754 tcp->tcp_snxt = tcp->tcp_fss + 1; 22755 } 22756 } 22757 /* 22758 * Note the trick here. u1 is unsigned. When tcp_urg 22759 * is smaller than seq, u1 will become a very huge value. 22760 * So the comparison will fail. Also note that tcp_urp 22761 * should be positive, see RFC 793 page 17. 22762 */ 22763 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 22764 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 22765 u1 < (uint32_t)(64 * 1024)) { 22766 flags |= TH_URG; 22767 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 22768 U32_TO_ABE16(u1, tcph->th_urp); 22769 } 22770 } 22771 tcph->th_flags[0] = (uchar_t)flags; 22772 tcp->tcp_rack = tcp->tcp_rnxt; 22773 tcp->tcp_rack_cnt = 0; 22774 22775 if (tcp->tcp_snd_ts_ok) { 22776 if (tcp->tcp_state != TCPS_SYN_SENT) { 22777 uint32_t llbolt = (uint32_t)lbolt; 22778 22779 U32_TO_BE32(llbolt, 22780 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22781 U32_TO_BE32(tcp->tcp_ts_recent, 22782 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22783 } 22784 } 22785 22786 if (num_sack_blk > 0) { 22787 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22788 sack_blk_t *tmp; 22789 int32_t i; 22790 22791 wptr[0] = TCPOPT_NOP; 22792 wptr[1] = TCPOPT_NOP; 22793 wptr[2] = TCPOPT_SACK; 22794 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22795 sizeof (sack_blk_t); 22796 wptr += TCPOPT_REAL_SACK_LEN; 22797 22798 tmp = tcp->tcp_sack_list; 22799 for (i = 0; i < num_sack_blk; i++) { 22800 U32_TO_BE32(tmp[i].begin, wptr); 22801 wptr += sizeof (tcp_seq); 22802 U32_TO_BE32(tmp[i].end, wptr); 22803 wptr += sizeof (tcp_seq); 22804 } 22805 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 22806 } 22807 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22808 data_length += (int)(mp1->b_wptr - rptr); 22809 if (tcp->tcp_ipversion == IPV4_VERSION) { 22810 ((ipha_t *)rptr)->ipha_length = htons(data_length); 22811 } else { 22812 ip6_t *ip6 = (ip6_t *)(rptr + 22813 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22814 sizeof (ip6i_t) : 0)); 22815 22816 ip6->ip6_plen = htons(data_length - 22817 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22818 } 22819 22820 /* 22821 * Prime pump for IP 22822 * Include the adjustment for a source route if any. 22823 */ 22824 data_length -= tcp->tcp_ip_hdr_len; 22825 data_length += tcp->tcp_sum; 22826 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22827 U16_TO_ABE16(data_length, tcph->th_sum); 22828 if (tcp->tcp_ip_forward_progress) { 22829 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22830 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22831 tcp->tcp_ip_forward_progress = B_FALSE; 22832 } 22833 return (mp1); 22834 } 22835 22836 /* This function handles the push timeout. */ 22837 void 22838 tcp_push_timer(void *arg) 22839 { 22840 conn_t *connp = (conn_t *)arg; 22841 tcp_t *tcp = connp->conn_tcp; 22842 22843 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 22844 22845 ASSERT(tcp->tcp_listener == NULL); 22846 22847 ASSERT(!IPCL_IS_NONSTR(connp)); 22848 22849 /* 22850 * We need to plug synchronous streams during our drain to prevent 22851 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 22852 */ 22853 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 22854 tcp->tcp_push_tid = 0; 22855 22856 if (tcp->tcp_rcv_list != NULL && 22857 tcp_rcv_drain(tcp) == TH_ACK_NEEDED) 22858 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 22859 22860 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 22861 } 22862 22863 /* 22864 * This function handles delayed ACK timeout. 22865 */ 22866 static void 22867 tcp_ack_timer(void *arg) 22868 { 22869 conn_t *connp = (conn_t *)arg; 22870 tcp_t *tcp = connp->conn_tcp; 22871 mblk_t *mp; 22872 tcp_stack_t *tcps = tcp->tcp_tcps; 22873 22874 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 22875 22876 tcp->tcp_ack_tid = 0; 22877 22878 if (tcp->tcp_fused) 22879 return; 22880 22881 /* 22882 * Do not send ACK if there is no outstanding unack'ed data. 22883 */ 22884 if (tcp->tcp_rnxt == tcp->tcp_rack) { 22885 return; 22886 } 22887 22888 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 22889 /* 22890 * Make sure we don't allow deferred ACKs to result in 22891 * timer-based ACKing. If we have held off an ACK 22892 * when there was more than an mss here, and the timer 22893 * goes off, we have to worry about the possibility 22894 * that the sender isn't doing slow-start, or is out 22895 * of step with us for some other reason. We fall 22896 * permanently back in the direction of 22897 * ACK-every-other-packet as suggested in RFC 1122. 22898 */ 22899 if (tcp->tcp_rack_abs_max > 2) 22900 tcp->tcp_rack_abs_max--; 22901 tcp->tcp_rack_cur_max = 2; 22902 } 22903 mp = tcp_ack_mp(tcp); 22904 22905 if (mp != NULL) { 22906 BUMP_LOCAL(tcp->tcp_obsegs); 22907 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22908 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 22909 tcp_send_data(tcp, tcp->tcp_wq, mp); 22910 } 22911 } 22912 22913 22914 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 22915 static mblk_t * 22916 tcp_ack_mp(tcp_t *tcp) 22917 { 22918 uint32_t seq_no; 22919 tcp_stack_t *tcps = tcp->tcp_tcps; 22920 22921 /* 22922 * There are a few cases to be considered while setting the sequence no. 22923 * Essentially, we can come here while processing an unacceptable pkt 22924 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 22925 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 22926 * If we are here for a zero window probe, stick with suna. In all 22927 * other cases, we check if suna + swnd encompasses snxt and set 22928 * the sequence number to snxt, if so. If snxt falls outside the 22929 * window (the receiver probably shrunk its window), we will go with 22930 * suna + swnd, otherwise the sequence no will be unacceptable to the 22931 * receiver. 22932 */ 22933 if (tcp->tcp_zero_win_probe) { 22934 seq_no = tcp->tcp_suna; 22935 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 22936 ASSERT(tcp->tcp_swnd == 0); 22937 seq_no = tcp->tcp_snxt; 22938 } else { 22939 seq_no = SEQ_GT(tcp->tcp_snxt, 22940 (tcp->tcp_suna + tcp->tcp_swnd)) ? 22941 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 22942 } 22943 22944 if (tcp->tcp_valid_bits) { 22945 /* 22946 * For the complex case where we have to send some 22947 * controls (FIN or SYN), let tcp_xmit_mp do it. 22948 */ 22949 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 22950 NULL, B_FALSE)); 22951 } else { 22952 /* Generate a simple ACK */ 22953 int data_length; 22954 uchar_t *rptr; 22955 tcph_t *tcph; 22956 mblk_t *mp1; 22957 int32_t tcp_hdr_len; 22958 int32_t tcp_tcp_hdr_len; 22959 int32_t num_sack_blk = 0; 22960 int32_t sack_opt_len; 22961 22962 /* 22963 * Allocate space for TCP + IP headers 22964 * and link-level header 22965 */ 22966 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22967 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22968 tcp->tcp_num_sack_blk); 22969 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22970 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22971 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 22972 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 22973 } else { 22974 tcp_hdr_len = tcp->tcp_hdr_len; 22975 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 22976 } 22977 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 22978 if (!mp1) 22979 return (NULL); 22980 22981 /* Update the latest receive window size in TCP header. */ 22982 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22983 tcp->tcp_tcph->th_win); 22984 /* copy in prototype TCP + IP header */ 22985 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22986 mp1->b_rptr = rptr; 22987 mp1->b_wptr = rptr + tcp_hdr_len; 22988 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22989 22990 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22991 22992 /* Set the TCP sequence number. */ 22993 U32_TO_ABE32(seq_no, tcph->th_seq); 22994 22995 /* Set up the TCP flag field. */ 22996 tcph->th_flags[0] = (uchar_t)TH_ACK; 22997 if (tcp->tcp_ecn_echo_on) 22998 tcph->th_flags[0] |= TH_ECE; 22999 23000 tcp->tcp_rack = tcp->tcp_rnxt; 23001 tcp->tcp_rack_cnt = 0; 23002 23003 /* fill in timestamp option if in use */ 23004 if (tcp->tcp_snd_ts_ok) { 23005 uint32_t llbolt = (uint32_t)lbolt; 23006 23007 U32_TO_BE32(llbolt, 23008 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23009 U32_TO_BE32(tcp->tcp_ts_recent, 23010 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23011 } 23012 23013 /* Fill in SACK options */ 23014 if (num_sack_blk > 0) { 23015 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23016 sack_blk_t *tmp; 23017 int32_t i; 23018 23019 wptr[0] = TCPOPT_NOP; 23020 wptr[1] = TCPOPT_NOP; 23021 wptr[2] = TCPOPT_SACK; 23022 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23023 sizeof (sack_blk_t); 23024 wptr += TCPOPT_REAL_SACK_LEN; 23025 23026 tmp = tcp->tcp_sack_list; 23027 for (i = 0; i < num_sack_blk; i++) { 23028 U32_TO_BE32(tmp[i].begin, wptr); 23029 wptr += sizeof (tcp_seq); 23030 U32_TO_BE32(tmp[i].end, wptr); 23031 wptr += sizeof (tcp_seq); 23032 } 23033 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23034 << 4); 23035 } 23036 23037 if (tcp->tcp_ipversion == IPV4_VERSION) { 23038 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23039 } else { 23040 /* Check for ip6i_t header in sticky hdrs */ 23041 ip6_t *ip6 = (ip6_t *)(rptr + 23042 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23043 sizeof (ip6i_t) : 0)); 23044 23045 ip6->ip6_plen = htons(tcp_hdr_len - 23046 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23047 } 23048 23049 /* 23050 * Prime pump for checksum calculation in IP. Include the 23051 * adjustment for a source route if any. 23052 */ 23053 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23054 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23055 U16_TO_ABE16(data_length, tcph->th_sum); 23056 23057 if (tcp->tcp_ip_forward_progress) { 23058 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23059 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23060 tcp->tcp_ip_forward_progress = B_FALSE; 23061 } 23062 return (mp1); 23063 } 23064 } 23065 23066 /* 23067 * Hash list insertion routine for tcp_t structures. Each hash bucket 23068 * contains a list of tcp_t entries, and each entry is bound to a unique 23069 * port. If there are multiple tcp_t's that are bound to the same port, then 23070 * one of them will be linked into the hash bucket list, and the rest will 23071 * hang off of that one entry. For each port, entries bound to a specific IP 23072 * address will be inserted before those those bound to INADDR_ANY. 23073 */ 23074 static void 23075 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23076 { 23077 tcp_t **tcpp; 23078 tcp_t *tcpnext; 23079 tcp_t *tcphash; 23080 23081 if (tcp->tcp_ptpbhn != NULL) { 23082 ASSERT(!caller_holds_lock); 23083 tcp_bind_hash_remove(tcp); 23084 } 23085 tcpp = &tbf->tf_tcp; 23086 if (!caller_holds_lock) { 23087 mutex_enter(&tbf->tf_lock); 23088 } else { 23089 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23090 } 23091 tcphash = tcpp[0]; 23092 tcpnext = NULL; 23093 if (tcphash != NULL) { 23094 /* Look for an entry using the same port */ 23095 while ((tcphash = tcpp[0]) != NULL && 23096 tcp->tcp_lport != tcphash->tcp_lport) 23097 tcpp = &(tcphash->tcp_bind_hash); 23098 23099 /* The port was not found, just add to the end */ 23100 if (tcphash == NULL) 23101 goto insert; 23102 23103 /* 23104 * OK, there already exists an entry bound to the 23105 * same port. 23106 * 23107 * If the new tcp bound to the INADDR_ANY address 23108 * and the first one in the list is not bound to 23109 * INADDR_ANY we skip all entries until we find the 23110 * first one bound to INADDR_ANY. 23111 * This makes sure that applications binding to a 23112 * specific address get preference over those binding to 23113 * INADDR_ANY. 23114 */ 23115 tcpnext = tcphash; 23116 tcphash = NULL; 23117 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23118 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23119 while ((tcpnext = tcpp[0]) != NULL && 23120 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23121 tcpp = &(tcpnext->tcp_bind_hash_port); 23122 23123 if (tcpnext) { 23124 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23125 tcphash = tcpnext->tcp_bind_hash; 23126 if (tcphash != NULL) { 23127 tcphash->tcp_ptpbhn = 23128 &(tcp->tcp_bind_hash); 23129 tcpnext->tcp_bind_hash = NULL; 23130 } 23131 } 23132 } else { 23133 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23134 tcphash = tcpnext->tcp_bind_hash; 23135 if (tcphash != NULL) { 23136 tcphash->tcp_ptpbhn = 23137 &(tcp->tcp_bind_hash); 23138 tcpnext->tcp_bind_hash = NULL; 23139 } 23140 } 23141 } 23142 insert: 23143 tcp->tcp_bind_hash_port = tcpnext; 23144 tcp->tcp_bind_hash = tcphash; 23145 tcp->tcp_ptpbhn = tcpp; 23146 tcpp[0] = tcp; 23147 if (!caller_holds_lock) 23148 mutex_exit(&tbf->tf_lock); 23149 } 23150 23151 /* 23152 * Hash list removal routine for tcp_t structures. 23153 */ 23154 static void 23155 tcp_bind_hash_remove(tcp_t *tcp) 23156 { 23157 tcp_t *tcpnext; 23158 kmutex_t *lockp; 23159 tcp_stack_t *tcps = tcp->tcp_tcps; 23160 23161 if (tcp->tcp_ptpbhn == NULL) 23162 return; 23163 23164 /* 23165 * Extract the lock pointer in case there are concurrent 23166 * hash_remove's for this instance. 23167 */ 23168 ASSERT(tcp->tcp_lport != 0); 23169 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23170 23171 ASSERT(lockp != NULL); 23172 mutex_enter(lockp); 23173 if (tcp->tcp_ptpbhn) { 23174 tcpnext = tcp->tcp_bind_hash_port; 23175 if (tcpnext != NULL) { 23176 tcp->tcp_bind_hash_port = NULL; 23177 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23178 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 23179 if (tcpnext->tcp_bind_hash != NULL) { 23180 tcpnext->tcp_bind_hash->tcp_ptpbhn = 23181 &(tcpnext->tcp_bind_hash); 23182 tcp->tcp_bind_hash = NULL; 23183 } 23184 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 23185 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23186 tcp->tcp_bind_hash = NULL; 23187 } 23188 *tcp->tcp_ptpbhn = tcpnext; 23189 tcp->tcp_ptpbhn = NULL; 23190 } 23191 mutex_exit(lockp); 23192 } 23193 23194 23195 /* 23196 * Hash list lookup routine for tcp_t structures. 23197 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23198 */ 23199 static tcp_t * 23200 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23201 { 23202 tf_t *tf; 23203 tcp_t *tcp; 23204 23205 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23206 mutex_enter(&tf->tf_lock); 23207 for (tcp = tf->tf_tcp; tcp != NULL; 23208 tcp = tcp->tcp_acceptor_hash) { 23209 if (tcp->tcp_acceptor_id == id) { 23210 CONN_INC_REF(tcp->tcp_connp); 23211 mutex_exit(&tf->tf_lock); 23212 return (tcp); 23213 } 23214 } 23215 mutex_exit(&tf->tf_lock); 23216 return (NULL); 23217 } 23218 23219 23220 /* 23221 * Hash list insertion routine for tcp_t structures. 23222 */ 23223 void 23224 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23225 { 23226 tf_t *tf; 23227 tcp_t **tcpp; 23228 tcp_t *tcpnext; 23229 tcp_stack_t *tcps = tcp->tcp_tcps; 23230 23231 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23232 23233 if (tcp->tcp_ptpahn != NULL) 23234 tcp_acceptor_hash_remove(tcp); 23235 tcpp = &tf->tf_tcp; 23236 mutex_enter(&tf->tf_lock); 23237 tcpnext = tcpp[0]; 23238 if (tcpnext) 23239 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23240 tcp->tcp_acceptor_hash = tcpnext; 23241 tcp->tcp_ptpahn = tcpp; 23242 tcpp[0] = tcp; 23243 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23244 mutex_exit(&tf->tf_lock); 23245 } 23246 23247 /* 23248 * Hash list removal routine for tcp_t structures. 23249 */ 23250 static void 23251 tcp_acceptor_hash_remove(tcp_t *tcp) 23252 { 23253 tcp_t *tcpnext; 23254 kmutex_t *lockp; 23255 23256 /* 23257 * Extract the lock pointer in case there are concurrent 23258 * hash_remove's for this instance. 23259 */ 23260 lockp = tcp->tcp_acceptor_lockp; 23261 23262 if (tcp->tcp_ptpahn == NULL) 23263 return; 23264 23265 ASSERT(lockp != NULL); 23266 mutex_enter(lockp); 23267 if (tcp->tcp_ptpahn) { 23268 tcpnext = tcp->tcp_acceptor_hash; 23269 if (tcpnext) { 23270 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 23271 tcp->tcp_acceptor_hash = NULL; 23272 } 23273 *tcp->tcp_ptpahn = tcpnext; 23274 tcp->tcp_ptpahn = NULL; 23275 } 23276 mutex_exit(lockp); 23277 tcp->tcp_acceptor_lockp = NULL; 23278 } 23279 23280 /* 23281 * Type three generator adapted from the random() function in 4.4 BSD: 23282 */ 23283 23284 /* 23285 * Copyright (c) 1983, 1993 23286 * The Regents of the University of California. All rights reserved. 23287 * 23288 * Redistribution and use in source and binary forms, with or without 23289 * modification, are permitted provided that the following conditions 23290 * are met: 23291 * 1. Redistributions of source code must retain the above copyright 23292 * notice, this list of conditions and the following disclaimer. 23293 * 2. Redistributions in binary form must reproduce the above copyright 23294 * notice, this list of conditions and the following disclaimer in the 23295 * documentation and/or other materials provided with the distribution. 23296 * 3. All advertising materials mentioning features or use of this software 23297 * must display the following acknowledgement: 23298 * This product includes software developed by the University of 23299 * California, Berkeley and its contributors. 23300 * 4. Neither the name of the University nor the names of its contributors 23301 * may be used to endorse or promote products derived from this software 23302 * without specific prior written permission. 23303 * 23304 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23305 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23306 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23307 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23308 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23309 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23310 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23311 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23312 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23313 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23314 * SUCH DAMAGE. 23315 */ 23316 23317 /* Type 3 -- x**31 + x**3 + 1 */ 23318 #define DEG_3 31 23319 #define SEP_3 3 23320 23321 23322 /* Protected by tcp_random_lock */ 23323 static int tcp_randtbl[DEG_3 + 1]; 23324 23325 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23326 static int *tcp_random_rptr = &tcp_randtbl[1]; 23327 23328 static int *tcp_random_state = &tcp_randtbl[1]; 23329 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23330 23331 kmutex_t tcp_random_lock; 23332 23333 void 23334 tcp_random_init(void) 23335 { 23336 int i; 23337 hrtime_t hrt; 23338 time_t wallclock; 23339 uint64_t result; 23340 23341 /* 23342 * Use high-res timer and current time for seed. Gethrtime() returns 23343 * a longlong, which may contain resolution down to nanoseconds. 23344 * The current time will either be a 32-bit or a 64-bit quantity. 23345 * XOR the two together in a 64-bit result variable. 23346 * Convert the result to a 32-bit value by multiplying the high-order 23347 * 32-bits by the low-order 32-bits. 23348 */ 23349 23350 hrt = gethrtime(); 23351 (void) drv_getparm(TIME, &wallclock); 23352 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23353 mutex_enter(&tcp_random_lock); 23354 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23355 (result & 0xffffffff); 23356 23357 for (i = 1; i < DEG_3; i++) 23358 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23359 + 12345; 23360 tcp_random_fptr = &tcp_random_state[SEP_3]; 23361 tcp_random_rptr = &tcp_random_state[0]; 23362 mutex_exit(&tcp_random_lock); 23363 for (i = 0; i < 10 * DEG_3; i++) 23364 (void) tcp_random(); 23365 } 23366 23367 /* 23368 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23369 * This range is selected to be approximately centered on TCP_ISS / 2, 23370 * and easy to compute. We get this value by generating a 32-bit random 23371 * number, selecting out the high-order 17 bits, and then adding one so 23372 * that we never return zero. 23373 */ 23374 int 23375 tcp_random(void) 23376 { 23377 int i; 23378 23379 mutex_enter(&tcp_random_lock); 23380 *tcp_random_fptr += *tcp_random_rptr; 23381 23382 /* 23383 * The high-order bits are more random than the low-order bits, 23384 * so we select out the high-order 17 bits and add one so that 23385 * we never return zero. 23386 */ 23387 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23388 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23389 tcp_random_fptr = tcp_random_state; 23390 ++tcp_random_rptr; 23391 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23392 tcp_random_rptr = tcp_random_state; 23393 23394 mutex_exit(&tcp_random_lock); 23395 return (i); 23396 } 23397 23398 static int 23399 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 23400 int *t_errorp, int *sys_errorp) 23401 { 23402 int error; 23403 int is_absreq_failure; 23404 t_scalar_t *opt_lenp; 23405 t_scalar_t opt_offset; 23406 int prim_type; 23407 struct T_conn_req *tcreqp; 23408 struct T_conn_res *tcresp; 23409 cred_t *cr; 23410 23411 /* 23412 * All Solaris components should pass a db_credp 23413 * for this TPI message, hence we ASSERT. 23414 * But in case there is some other M_PROTO that looks 23415 * like a TPI message sent by some other kernel 23416 * component, we check and return an error. 23417 */ 23418 cr = msg_getcred(mp, NULL); 23419 ASSERT(cr != NULL); 23420 if (cr == NULL) 23421 return (-1); 23422 23423 prim_type = ((union T_primitives *)mp->b_rptr)->type; 23424 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 23425 prim_type == T_CONN_RES); 23426 23427 switch (prim_type) { 23428 case T_CONN_REQ: 23429 tcreqp = (struct T_conn_req *)mp->b_rptr; 23430 opt_offset = tcreqp->OPT_offset; 23431 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 23432 break; 23433 case O_T_CONN_RES: 23434 case T_CONN_RES: 23435 tcresp = (struct T_conn_res *)mp->b_rptr; 23436 opt_offset = tcresp->OPT_offset; 23437 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 23438 break; 23439 } 23440 23441 *t_errorp = 0; 23442 *sys_errorp = 0; 23443 *do_disconnectp = 0; 23444 23445 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 23446 opt_offset, cr, &tcp_opt_obj, 23447 NULL, &is_absreq_failure); 23448 23449 switch (error) { 23450 case 0: /* no error */ 23451 ASSERT(is_absreq_failure == 0); 23452 return (0); 23453 case ENOPROTOOPT: 23454 *t_errorp = TBADOPT; 23455 break; 23456 case EACCES: 23457 *t_errorp = TACCES; 23458 break; 23459 default: 23460 *t_errorp = TSYSERR; *sys_errorp = error; 23461 break; 23462 } 23463 if (is_absreq_failure != 0) { 23464 /* 23465 * The connection request should get the local ack 23466 * T_OK_ACK and then a T_DISCON_IND. 23467 */ 23468 *do_disconnectp = 1; 23469 } 23470 return (-1); 23471 } 23472 23473 /* 23474 * Split this function out so that if the secret changes, I'm okay. 23475 * 23476 * Initialize the tcp_iss_cookie and tcp_iss_key. 23477 */ 23478 23479 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 23480 23481 static void 23482 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 23483 { 23484 struct { 23485 int32_t current_time; 23486 uint32_t randnum; 23487 uint16_t pad; 23488 uint8_t ether[6]; 23489 uint8_t passwd[PASSWD_SIZE]; 23490 } tcp_iss_cookie; 23491 time_t t; 23492 23493 /* 23494 * Start with the current absolute time. 23495 */ 23496 (void) drv_getparm(TIME, &t); 23497 tcp_iss_cookie.current_time = t; 23498 23499 /* 23500 * XXX - Need a more random number per RFC 1750, not this crap. 23501 * OTOH, if what follows is pretty random, then I'm in better shape. 23502 */ 23503 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 23504 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 23505 23506 /* 23507 * The cpu_type_info is pretty non-random. Ugggh. It does serve 23508 * as a good template. 23509 */ 23510 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 23511 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 23512 23513 /* 23514 * The pass-phrase. Normally this is supplied by user-called NDD. 23515 */ 23516 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 23517 23518 /* 23519 * See 4010593 if this section becomes a problem again, 23520 * but the local ethernet address is useful here. 23521 */ 23522 (void) localetheraddr(NULL, 23523 (struct ether_addr *)&tcp_iss_cookie.ether); 23524 23525 /* 23526 * Hash 'em all together. The MD5Final is called per-connection. 23527 */ 23528 mutex_enter(&tcps->tcps_iss_key_lock); 23529 MD5Init(&tcps->tcps_iss_key); 23530 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 23531 sizeof (tcp_iss_cookie)); 23532 mutex_exit(&tcps->tcps_iss_key_lock); 23533 } 23534 23535 /* 23536 * Set the RFC 1948 pass phrase 23537 */ 23538 /* ARGSUSED */ 23539 static int 23540 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23541 cred_t *cr) 23542 { 23543 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23544 23545 /* 23546 * Basically, value contains a new pass phrase. Pass it along! 23547 */ 23548 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 23549 return (0); 23550 } 23551 23552 /* ARGSUSED */ 23553 static int 23554 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 23555 { 23556 bzero(buf, sizeof (tcp_sack_info_t)); 23557 return (0); 23558 } 23559 23560 /* ARGSUSED */ 23561 static int 23562 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 23563 { 23564 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 23565 return (0); 23566 } 23567 23568 /* 23569 * Make sure we wait until the default queue is setup, yet allow 23570 * tcp_g_q_create() to open a TCP stream. 23571 * We need to allow tcp_g_q_create() do do an open 23572 * of tcp, hence we compare curhread. 23573 * All others have to wait until the tcps_g_q has been 23574 * setup. 23575 */ 23576 void 23577 tcp_g_q_setup(tcp_stack_t *tcps) 23578 { 23579 mutex_enter(&tcps->tcps_g_q_lock); 23580 if (tcps->tcps_g_q != NULL) { 23581 mutex_exit(&tcps->tcps_g_q_lock); 23582 return; 23583 } 23584 if (tcps->tcps_g_q_creator == NULL) { 23585 /* This thread will set it up */ 23586 tcps->tcps_g_q_creator = curthread; 23587 mutex_exit(&tcps->tcps_g_q_lock); 23588 tcp_g_q_create(tcps); 23589 mutex_enter(&tcps->tcps_g_q_lock); 23590 ASSERT(tcps->tcps_g_q_creator == curthread); 23591 tcps->tcps_g_q_creator = NULL; 23592 cv_signal(&tcps->tcps_g_q_cv); 23593 ASSERT(tcps->tcps_g_q != NULL); 23594 mutex_exit(&tcps->tcps_g_q_lock); 23595 return; 23596 } 23597 /* Everybody but the creator has to wait */ 23598 if (tcps->tcps_g_q_creator != curthread) { 23599 while (tcps->tcps_g_q == NULL) 23600 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 23601 } 23602 mutex_exit(&tcps->tcps_g_q_lock); 23603 } 23604 23605 #define IP "ip" 23606 23607 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 23608 23609 /* 23610 * Create a default tcp queue here instead of in strplumb 23611 */ 23612 void 23613 tcp_g_q_create(tcp_stack_t *tcps) 23614 { 23615 int error; 23616 ldi_handle_t lh = NULL; 23617 ldi_ident_t li = NULL; 23618 int rval; 23619 cred_t *cr; 23620 major_t IP_MAJ; 23621 23622 #ifdef NS_DEBUG 23623 (void) printf("tcp_g_q_create()\n"); 23624 #endif 23625 23626 IP_MAJ = ddi_name_to_major(IP); 23627 23628 ASSERT(tcps->tcps_g_q_creator == curthread); 23629 23630 error = ldi_ident_from_major(IP_MAJ, &li); 23631 if (error) { 23632 #ifdef DEBUG 23633 printf("tcp_g_q_create: lyr ident get failed error %d\n", 23634 error); 23635 #endif 23636 return; 23637 } 23638 23639 cr = zone_get_kcred(netstackid_to_zoneid( 23640 tcps->tcps_netstack->netstack_stackid)); 23641 ASSERT(cr != NULL); 23642 /* 23643 * We set the tcp default queue to IPv6 because IPv4 falls 23644 * back to IPv6 when it can't find a client, but 23645 * IPv6 does not fall back to IPv4. 23646 */ 23647 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 23648 if (error) { 23649 #ifdef DEBUG 23650 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 23651 error); 23652 #endif 23653 goto out; 23654 } 23655 23656 /* 23657 * This ioctl causes the tcp framework to cache a pointer to 23658 * this stream, so we don't want to close the stream after 23659 * this operation. 23660 * Use the kernel credentials that are for the zone we're in. 23661 */ 23662 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 23663 (intptr_t)0, FKIOCTL, cr, &rval); 23664 if (error) { 23665 #ifdef DEBUG 23666 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 23667 "error %d\n", error); 23668 #endif 23669 goto out; 23670 } 23671 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 23672 lh = NULL; 23673 out: 23674 /* Close layered handles */ 23675 if (li) 23676 ldi_ident_release(li); 23677 /* Keep cred around until _inactive needs it */ 23678 tcps->tcps_g_q_cr = cr; 23679 } 23680 23681 /* 23682 * We keep tcp_g_q set until all other tcp_t's in the zone 23683 * has gone away, and then when tcp_g_q_inactive() is called 23684 * we clear it. 23685 */ 23686 void 23687 tcp_g_q_destroy(tcp_stack_t *tcps) 23688 { 23689 #ifdef NS_DEBUG 23690 (void) printf("tcp_g_q_destroy()for stack %d\n", 23691 tcps->tcps_netstack->netstack_stackid); 23692 #endif 23693 23694 if (tcps->tcps_g_q == NULL) { 23695 return; /* Nothing to cleanup */ 23696 } 23697 /* 23698 * Drop reference corresponding to the default queue. 23699 * This reference was added from tcp_open when the default queue 23700 * was created, hence we compensate for this extra drop in 23701 * tcp_g_q_close. If the refcnt drops to zero here it means 23702 * the default queue was the last one to be open, in which 23703 * case, then tcp_g_q_inactive will be 23704 * called as a result of the refrele. 23705 */ 23706 TCPS_REFRELE(tcps); 23707 } 23708 23709 /* 23710 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23711 * Run by tcp_q_q_inactive using a taskq. 23712 */ 23713 static void 23714 tcp_g_q_close(void *arg) 23715 { 23716 tcp_stack_t *tcps = arg; 23717 int error; 23718 ldi_handle_t lh = NULL; 23719 ldi_ident_t li = NULL; 23720 cred_t *cr; 23721 major_t IP_MAJ; 23722 23723 IP_MAJ = ddi_name_to_major(IP); 23724 23725 #ifdef NS_DEBUG 23726 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 23727 tcps->tcps_netstack->netstack_stackid, 23728 tcps->tcps_netstack->netstack_refcnt); 23729 #endif 23730 lh = tcps->tcps_g_q_lh; 23731 if (lh == NULL) 23732 return; /* Nothing to cleanup */ 23733 23734 ASSERT(tcps->tcps_refcnt == 1); 23735 ASSERT(tcps->tcps_g_q != NULL); 23736 23737 error = ldi_ident_from_major(IP_MAJ, &li); 23738 if (error) { 23739 #ifdef DEBUG 23740 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 23741 error); 23742 #endif 23743 return; 23744 } 23745 23746 cr = tcps->tcps_g_q_cr; 23747 tcps->tcps_g_q_cr = NULL; 23748 ASSERT(cr != NULL); 23749 23750 /* 23751 * Make sure we can break the recursion when tcp_close decrements 23752 * the reference count causing g_q_inactive to be called again. 23753 */ 23754 tcps->tcps_g_q_lh = NULL; 23755 23756 /* close the default queue */ 23757 (void) ldi_close(lh, FREAD|FWRITE, cr); 23758 /* 23759 * At this point in time tcps and the rest of netstack_t might 23760 * have been deleted. 23761 */ 23762 tcps = NULL; 23763 23764 /* Close layered handles */ 23765 ldi_ident_release(li); 23766 crfree(cr); 23767 } 23768 23769 /* 23770 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23771 * 23772 * Have to ensure that the ldi routines are not used by an 23773 * interrupt thread by using a taskq. 23774 */ 23775 void 23776 tcp_g_q_inactive(tcp_stack_t *tcps) 23777 { 23778 if (tcps->tcps_g_q_lh == NULL) 23779 return; /* Nothing to cleanup */ 23780 23781 ASSERT(tcps->tcps_refcnt == 0); 23782 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 23783 23784 if (servicing_interrupt()) { 23785 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 23786 (void *) tcps, TQ_SLEEP); 23787 } else { 23788 tcp_g_q_close(tcps); 23789 } 23790 } 23791 23792 /* 23793 * Called by IP when IP is loaded into the kernel 23794 */ 23795 void 23796 tcp_ddi_g_init(void) 23797 { 23798 tcp_timercache = kmem_cache_create("tcp_timercache", 23799 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 23800 NULL, NULL, NULL, NULL, NULL, 0); 23801 23802 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 23803 sizeof (tcp_sack_info_t), 0, 23804 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 23805 23806 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 23807 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 23808 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 23809 23810 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 23811 23812 /* Initialize the random number generator */ 23813 tcp_random_init(); 23814 23815 /* A single callback independently of how many netstacks we have */ 23816 ip_squeue_init(tcp_squeue_add); 23817 23818 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 23819 23820 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 23821 TASKQ_PREPOPULATE); 23822 23823 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 23824 23825 /* 23826 * We want to be informed each time a stack is created or 23827 * destroyed in the kernel, so we can maintain the 23828 * set of tcp_stack_t's. 23829 */ 23830 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 23831 tcp_stack_fini); 23832 } 23833 23834 23835 #define INET_NAME "ip" 23836 23837 /* 23838 * Initialize the TCP stack instance. 23839 */ 23840 static void * 23841 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 23842 { 23843 tcp_stack_t *tcps; 23844 tcpparam_t *pa; 23845 int i; 23846 int error = 0; 23847 major_t major; 23848 23849 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 23850 tcps->tcps_netstack = ns; 23851 23852 /* Initialize locks */ 23853 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 23854 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 23855 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 23856 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 23857 23858 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 23859 tcps->tcps_g_epriv_ports[0] = 2049; 23860 tcps->tcps_g_epriv_ports[1] = 4045; 23861 tcps->tcps_min_anonpriv_port = 512; 23862 23863 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 23864 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 23865 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 23866 TCP_FANOUT_SIZE, KM_SLEEP); 23867 23868 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 23869 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 23870 MUTEX_DEFAULT, NULL); 23871 } 23872 23873 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 23874 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 23875 MUTEX_DEFAULT, NULL); 23876 } 23877 23878 /* TCP's IPsec code calls the packet dropper. */ 23879 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 23880 23881 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 23882 tcps->tcps_params = pa; 23883 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 23884 23885 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 23886 A_CNT(lcl_tcp_param_arr), tcps); 23887 23888 /* 23889 * Note: To really walk the device tree you need the devinfo 23890 * pointer to your device which is only available after probe/attach. 23891 * The following is safe only because it uses ddi_root_node() 23892 */ 23893 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 23894 tcp_opt_obj.odb_opt_arr_cnt); 23895 23896 /* 23897 * Initialize RFC 1948 secret values. This will probably be reset once 23898 * by the boot scripts. 23899 * 23900 * Use NULL name, as the name is caught by the new lockstats. 23901 * 23902 * Initialize with some random, non-guessable string, like the global 23903 * T_INFO_ACK. 23904 */ 23905 23906 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 23907 sizeof (tcp_g_t_info_ack), tcps); 23908 23909 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 23910 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 23911 23912 major = mod_name_to_major(INET_NAME); 23913 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 23914 ASSERT(error == 0); 23915 return (tcps); 23916 } 23917 23918 /* 23919 * Called when the IP module is about to be unloaded. 23920 */ 23921 void 23922 tcp_ddi_g_destroy(void) 23923 { 23924 tcp_g_kstat_fini(tcp_g_kstat); 23925 tcp_g_kstat = NULL; 23926 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 23927 23928 mutex_destroy(&tcp_random_lock); 23929 23930 kmem_cache_destroy(tcp_timercache); 23931 kmem_cache_destroy(tcp_sack_info_cache); 23932 kmem_cache_destroy(tcp_iphc_cache); 23933 23934 netstack_unregister(NS_TCP); 23935 taskq_destroy(tcp_taskq); 23936 } 23937 23938 /* 23939 * Shut down the TCP stack instance. 23940 */ 23941 /* ARGSUSED */ 23942 static void 23943 tcp_stack_shutdown(netstackid_t stackid, void *arg) 23944 { 23945 tcp_stack_t *tcps = (tcp_stack_t *)arg; 23946 23947 tcp_g_q_destroy(tcps); 23948 } 23949 23950 /* 23951 * Free the TCP stack instance. 23952 */ 23953 static void 23954 tcp_stack_fini(netstackid_t stackid, void *arg) 23955 { 23956 tcp_stack_t *tcps = (tcp_stack_t *)arg; 23957 int i; 23958 23959 nd_free(&tcps->tcps_g_nd); 23960 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 23961 tcps->tcps_params = NULL; 23962 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 23963 tcps->tcps_wroff_xtra_param = NULL; 23964 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 23965 tcps->tcps_mdt_head_param = NULL; 23966 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 23967 tcps->tcps_mdt_tail_param = NULL; 23968 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 23969 tcps->tcps_mdt_max_pbufs_param = NULL; 23970 23971 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 23972 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 23973 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 23974 } 23975 23976 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 23977 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 23978 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 23979 } 23980 23981 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 23982 tcps->tcps_bind_fanout = NULL; 23983 23984 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 23985 tcps->tcps_acceptor_fanout = NULL; 23986 23987 mutex_destroy(&tcps->tcps_iss_key_lock); 23988 mutex_destroy(&tcps->tcps_g_q_lock); 23989 cv_destroy(&tcps->tcps_g_q_cv); 23990 mutex_destroy(&tcps->tcps_epriv_port_lock); 23991 23992 ip_drop_unregister(&tcps->tcps_dropper); 23993 23994 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 23995 tcps->tcps_kstat = NULL; 23996 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 23997 23998 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 23999 tcps->tcps_mibkp = NULL; 24000 24001 ldi_ident_release(tcps->tcps_ldi_ident); 24002 kmem_free(tcps, sizeof (*tcps)); 24003 } 24004 24005 /* 24006 * Generate ISS, taking into account NDD changes may happen halfway through. 24007 * (If the iss is not zero, set it.) 24008 */ 24009 24010 static void 24011 tcp_iss_init(tcp_t *tcp) 24012 { 24013 MD5_CTX context; 24014 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 24015 uint32_t answer[4]; 24016 tcp_stack_t *tcps = tcp->tcp_tcps; 24017 24018 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 24019 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 24020 switch (tcps->tcps_strong_iss) { 24021 case 2: 24022 mutex_enter(&tcps->tcps_iss_key_lock); 24023 context = tcps->tcps_iss_key; 24024 mutex_exit(&tcps->tcps_iss_key_lock); 24025 arg.ports = tcp->tcp_ports; 24026 if (tcp->tcp_ipversion == IPV4_VERSION) { 24027 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 24028 &arg.src); 24029 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 24030 &arg.dst); 24031 } else { 24032 arg.src = tcp->tcp_ip6h->ip6_src; 24033 arg.dst = tcp->tcp_ip6h->ip6_dst; 24034 } 24035 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 24036 MD5Final((uchar_t *)answer, &context); 24037 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 24038 /* 24039 * Now that we've hashed into a unique per-connection sequence 24040 * space, add a random increment per strong_iss == 1. So I 24041 * guess we'll have to... 24042 */ 24043 /* FALLTHRU */ 24044 case 1: 24045 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 24046 break; 24047 default: 24048 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24049 break; 24050 } 24051 tcp->tcp_valid_bits = TCP_ISS_VALID; 24052 tcp->tcp_fss = tcp->tcp_iss - 1; 24053 tcp->tcp_suna = tcp->tcp_iss; 24054 tcp->tcp_snxt = tcp->tcp_iss + 1; 24055 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 24056 tcp->tcp_csuna = tcp->tcp_snxt; 24057 } 24058 24059 /* 24060 * Exported routine for extracting active tcp connection status. 24061 * 24062 * This is used by the Solaris Cluster Networking software to 24063 * gather a list of connections that need to be forwarded to 24064 * specific nodes in the cluster when configuration changes occur. 24065 * 24066 * The callback is invoked for each tcp_t structure from all netstacks, 24067 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 24068 * from the netstack with the specified stack_id. Returning 24069 * non-zero from the callback routine terminates the search. 24070 */ 24071 int 24072 cl_tcp_walk_list(netstackid_t stack_id, 24073 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 24074 { 24075 netstack_handle_t nh; 24076 netstack_t *ns; 24077 int ret = 0; 24078 24079 if (stack_id >= 0) { 24080 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 24081 return (EINVAL); 24082 24083 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24084 ns->netstack_tcp); 24085 netstack_rele(ns); 24086 return (ret); 24087 } 24088 24089 netstack_next_init(&nh); 24090 while ((ns = netstack_next(&nh)) != NULL) { 24091 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24092 ns->netstack_tcp); 24093 netstack_rele(ns); 24094 } 24095 netstack_next_fini(&nh); 24096 return (ret); 24097 } 24098 24099 static int 24100 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 24101 tcp_stack_t *tcps) 24102 { 24103 tcp_t *tcp; 24104 cl_tcp_info_t cl_tcpi; 24105 connf_t *connfp; 24106 conn_t *connp; 24107 int i; 24108 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24109 24110 ASSERT(callback != NULL); 24111 24112 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24113 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 24114 connp = NULL; 24115 24116 while ((connp = 24117 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24118 24119 tcp = connp->conn_tcp; 24120 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 24121 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 24122 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 24123 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 24124 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 24125 /* 24126 * The macros tcp_laddr and tcp_faddr give the IPv4 24127 * addresses. They are copied implicitly below as 24128 * mapped addresses. 24129 */ 24130 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 24131 if (tcp->tcp_ipversion == IPV4_VERSION) { 24132 cl_tcpi.cl_tcpi_faddr = 24133 tcp->tcp_ipha->ipha_dst; 24134 } else { 24135 cl_tcpi.cl_tcpi_faddr_v6 = 24136 tcp->tcp_ip6h->ip6_dst; 24137 } 24138 24139 /* 24140 * If the callback returns non-zero 24141 * we terminate the traversal. 24142 */ 24143 if ((*callback)(&cl_tcpi, arg) != 0) { 24144 CONN_DEC_REF(tcp->tcp_connp); 24145 return (1); 24146 } 24147 } 24148 } 24149 24150 return (0); 24151 } 24152 24153 /* 24154 * Macros used for accessing the different types of sockaddr 24155 * structures inside a tcp_ioc_abort_conn_t. 24156 */ 24157 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 24158 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 24159 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 24160 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 24161 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 24162 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 24163 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 24164 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 24165 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 24166 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 24167 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 24168 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 24169 24170 /* 24171 * Return the correct error code to mimic the behavior 24172 * of a connection reset. 24173 */ 24174 #define TCP_AC_GET_ERRCODE(state, err) { \ 24175 switch ((state)) { \ 24176 case TCPS_SYN_SENT: \ 24177 case TCPS_SYN_RCVD: \ 24178 (err) = ECONNREFUSED; \ 24179 break; \ 24180 case TCPS_ESTABLISHED: \ 24181 case TCPS_FIN_WAIT_1: \ 24182 case TCPS_FIN_WAIT_2: \ 24183 case TCPS_CLOSE_WAIT: \ 24184 (err) = ECONNRESET; \ 24185 break; \ 24186 case TCPS_CLOSING: \ 24187 case TCPS_LAST_ACK: \ 24188 case TCPS_TIME_WAIT: \ 24189 (err) = 0; \ 24190 break; \ 24191 default: \ 24192 (err) = ENXIO; \ 24193 } \ 24194 } 24195 24196 /* 24197 * Check if a tcp structure matches the info in acp. 24198 */ 24199 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 24200 (((acp)->ac_local.ss_family == AF_INET) ? \ 24201 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 24202 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 24203 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 24204 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 24205 (TCP_AC_V4LPORT((acp)) == 0 || \ 24206 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 24207 (TCP_AC_V4RPORT((acp)) == 0 || \ 24208 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24209 (acp)->ac_start <= (tcp)->tcp_state && \ 24210 (acp)->ac_end >= (tcp)->tcp_state) : \ 24211 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24212 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24213 &(tcp)->tcp_ip_src_v6)) && \ 24214 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24215 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24216 &(tcp)->tcp_remote_v6)) && \ 24217 (TCP_AC_V6LPORT((acp)) == 0 || \ 24218 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24219 (TCP_AC_V6RPORT((acp)) == 0 || \ 24220 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24221 (acp)->ac_start <= (tcp)->tcp_state && \ 24222 (acp)->ac_end >= (tcp)->tcp_state)) 24223 24224 #define TCP_AC_MATCH(acp, tcp) \ 24225 (((acp)->ac_zoneid == ALL_ZONES || \ 24226 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24227 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24228 24229 /* 24230 * Build a message containing a tcp_ioc_abort_conn_t structure 24231 * which is filled in with information from acp and tp. 24232 */ 24233 static mblk_t * 24234 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24235 { 24236 mblk_t *mp; 24237 tcp_ioc_abort_conn_t *tacp; 24238 24239 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24240 if (mp == NULL) 24241 return (NULL); 24242 24243 mp->b_datap->db_type = M_CTL; 24244 24245 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24246 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24247 sizeof (uint32_t)); 24248 24249 tacp->ac_start = acp->ac_start; 24250 tacp->ac_end = acp->ac_end; 24251 tacp->ac_zoneid = acp->ac_zoneid; 24252 24253 if (acp->ac_local.ss_family == AF_INET) { 24254 tacp->ac_local.ss_family = AF_INET; 24255 tacp->ac_remote.ss_family = AF_INET; 24256 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24257 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24258 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24259 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24260 } else { 24261 tacp->ac_local.ss_family = AF_INET6; 24262 tacp->ac_remote.ss_family = AF_INET6; 24263 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24264 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24265 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24266 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24267 } 24268 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24269 return (mp); 24270 } 24271 24272 /* 24273 * Print a tcp_ioc_abort_conn_t structure. 24274 */ 24275 static void 24276 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24277 { 24278 char lbuf[128]; 24279 char rbuf[128]; 24280 sa_family_t af; 24281 in_port_t lport, rport; 24282 ushort_t logflags; 24283 24284 af = acp->ac_local.ss_family; 24285 24286 if (af == AF_INET) { 24287 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24288 lbuf, 128); 24289 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24290 rbuf, 128); 24291 lport = ntohs(TCP_AC_V4LPORT(acp)); 24292 rport = ntohs(TCP_AC_V4RPORT(acp)); 24293 } else { 24294 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24295 lbuf, 128); 24296 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24297 rbuf, 128); 24298 lport = ntohs(TCP_AC_V6LPORT(acp)); 24299 rport = ntohs(TCP_AC_V6RPORT(acp)); 24300 } 24301 24302 logflags = SL_TRACE | SL_NOTE; 24303 /* 24304 * Don't print this message to the console if the operation was done 24305 * to a non-global zone. 24306 */ 24307 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24308 logflags |= SL_CONSOLE; 24309 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24310 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24311 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24312 acp->ac_start, acp->ac_end); 24313 } 24314 24315 /* 24316 * Called inside tcp_rput when a message built using 24317 * tcp_ioctl_abort_build_msg is put into a queue. 24318 * Note that when we get here there is no wildcard in acp any more. 24319 */ 24320 static void 24321 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24322 { 24323 tcp_ioc_abort_conn_t *acp; 24324 24325 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24326 if (tcp->tcp_state <= acp->ac_end) { 24327 /* 24328 * If we get here, we are already on the correct 24329 * squeue. This ioctl follows the following path 24330 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24331 * ->tcp_ioctl_abort->squeue_enter (if on a 24332 * different squeue) 24333 */ 24334 int errcode; 24335 24336 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24337 (void) tcp_clean_death(tcp, errcode, 26); 24338 } 24339 freemsg(mp); 24340 } 24341 24342 /* 24343 * Abort all matching connections on a hash chain. 24344 */ 24345 static int 24346 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24347 boolean_t exact, tcp_stack_t *tcps) 24348 { 24349 int nmatch, err = 0; 24350 tcp_t *tcp; 24351 MBLKP mp, last, listhead = NULL; 24352 conn_t *tconnp; 24353 connf_t *connfp; 24354 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24355 24356 connfp = &ipst->ips_ipcl_conn_fanout[index]; 24357 24358 startover: 24359 nmatch = 0; 24360 24361 mutex_enter(&connfp->connf_lock); 24362 for (tconnp = connfp->connf_head; tconnp != NULL; 24363 tconnp = tconnp->conn_next) { 24364 tcp = tconnp->conn_tcp; 24365 if (TCP_AC_MATCH(acp, tcp)) { 24366 CONN_INC_REF(tcp->tcp_connp); 24367 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24368 if (mp == NULL) { 24369 err = ENOMEM; 24370 CONN_DEC_REF(tcp->tcp_connp); 24371 break; 24372 } 24373 mp->b_prev = (mblk_t *)tcp; 24374 24375 if (listhead == NULL) { 24376 listhead = mp; 24377 last = mp; 24378 } else { 24379 last->b_next = mp; 24380 last = mp; 24381 } 24382 nmatch++; 24383 if (exact) 24384 break; 24385 } 24386 24387 /* Avoid holding lock for too long. */ 24388 if (nmatch >= 500) 24389 break; 24390 } 24391 mutex_exit(&connfp->connf_lock); 24392 24393 /* Pass mp into the correct tcp */ 24394 while ((mp = listhead) != NULL) { 24395 listhead = listhead->b_next; 24396 tcp = (tcp_t *)mp->b_prev; 24397 mp->b_next = mp->b_prev = NULL; 24398 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 24399 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 24400 } 24401 24402 *count += nmatch; 24403 if (nmatch >= 500 && err == 0) 24404 goto startover; 24405 return (err); 24406 } 24407 24408 /* 24409 * Abort all connections that matches the attributes specified in acp. 24410 */ 24411 static int 24412 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 24413 { 24414 sa_family_t af; 24415 uint32_t ports; 24416 uint16_t *pports; 24417 int err = 0, count = 0; 24418 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24419 int index = -1; 24420 ushort_t logflags; 24421 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24422 24423 af = acp->ac_local.ss_family; 24424 24425 if (af == AF_INET) { 24426 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24427 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24428 pports = (uint16_t *)&ports; 24429 pports[1] = TCP_AC_V4LPORT(acp); 24430 pports[0] = TCP_AC_V4RPORT(acp); 24431 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24432 } 24433 } else { 24434 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 24435 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 24436 pports = (uint16_t *)&ports; 24437 pports[1] = TCP_AC_V6LPORT(acp); 24438 pports[0] = TCP_AC_V6RPORT(acp); 24439 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 24440 } 24441 } 24442 24443 /* 24444 * For cases where remote addr, local port, and remote port are non- 24445 * wildcards, tcp_ioctl_abort_bucket will only be called once. 24446 */ 24447 if (index != -1) { 24448 err = tcp_ioctl_abort_bucket(acp, index, 24449 &count, exact, tcps); 24450 } else { 24451 /* 24452 * loop through all entries for wildcard case 24453 */ 24454 for (index = 0; 24455 index < ipst->ips_ipcl_conn_fanout_size; 24456 index++) { 24457 err = tcp_ioctl_abort_bucket(acp, index, 24458 &count, exact, tcps); 24459 if (err != 0) 24460 break; 24461 } 24462 } 24463 24464 logflags = SL_TRACE | SL_NOTE; 24465 /* 24466 * Don't print this message to the console if the operation was done 24467 * to a non-global zone. 24468 */ 24469 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24470 logflags |= SL_CONSOLE; 24471 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 24472 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 24473 if (err == 0 && count == 0) 24474 err = ENOENT; 24475 return (err); 24476 } 24477 24478 /* 24479 * Process the TCP_IOC_ABORT_CONN ioctl request. 24480 */ 24481 static void 24482 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 24483 { 24484 int err; 24485 IOCP iocp; 24486 MBLKP mp1; 24487 sa_family_t laf, raf; 24488 tcp_ioc_abort_conn_t *acp; 24489 zone_t *zptr; 24490 conn_t *connp = Q_TO_CONN(q); 24491 zoneid_t zoneid = connp->conn_zoneid; 24492 tcp_t *tcp = connp->conn_tcp; 24493 tcp_stack_t *tcps = tcp->tcp_tcps; 24494 24495 iocp = (IOCP)mp->b_rptr; 24496 24497 if ((mp1 = mp->b_cont) == NULL || 24498 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 24499 err = EINVAL; 24500 goto out; 24501 } 24502 24503 /* check permissions */ 24504 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 24505 err = EPERM; 24506 goto out; 24507 } 24508 24509 if (mp1->b_cont != NULL) { 24510 freemsg(mp1->b_cont); 24511 mp1->b_cont = NULL; 24512 } 24513 24514 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 24515 laf = acp->ac_local.ss_family; 24516 raf = acp->ac_remote.ss_family; 24517 24518 /* check that a zone with the supplied zoneid exists */ 24519 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 24520 zptr = zone_find_by_id(zoneid); 24521 if (zptr != NULL) { 24522 zone_rele(zptr); 24523 } else { 24524 err = EINVAL; 24525 goto out; 24526 } 24527 } 24528 24529 /* 24530 * For exclusive stacks we set the zoneid to zero 24531 * to make TCP operate as if in the global zone. 24532 */ 24533 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 24534 acp->ac_zoneid = GLOBAL_ZONEID; 24535 24536 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24537 acp->ac_start > acp->ac_end || laf != raf || 24538 (laf != AF_INET && laf != AF_INET6)) { 24539 err = EINVAL; 24540 goto out; 24541 } 24542 24543 tcp_ioctl_abort_dump(acp); 24544 err = tcp_ioctl_abort(acp, tcps); 24545 24546 out: 24547 if (mp1 != NULL) { 24548 freemsg(mp1); 24549 mp->b_cont = NULL; 24550 } 24551 24552 if (err != 0) 24553 miocnak(q, mp, 0, err); 24554 else 24555 miocack(q, mp, 0, 0); 24556 } 24557 24558 /* 24559 * tcp_time_wait_processing() handles processing of incoming packets when 24560 * the tcp is in the TIME_WAIT state. 24561 * A TIME_WAIT tcp that has an associated open TCP stream is never put 24562 * on the time wait list. 24563 */ 24564 void 24565 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 24566 uint32_t seg_ack, int seg_len, tcph_t *tcph) 24567 { 24568 int32_t bytes_acked; 24569 int32_t gap; 24570 int32_t rgap; 24571 tcp_opt_t tcpopt; 24572 uint_t flags; 24573 uint32_t new_swnd = 0; 24574 conn_t *connp; 24575 tcp_stack_t *tcps = tcp->tcp_tcps; 24576 24577 BUMP_LOCAL(tcp->tcp_ibsegs); 24578 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 24579 24580 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 24581 new_swnd = BE16_TO_U16(tcph->th_win) << 24582 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 24583 if (tcp->tcp_snd_ts_ok) { 24584 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 24585 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24586 tcp->tcp_rnxt, TH_ACK); 24587 goto done; 24588 } 24589 } 24590 gap = seg_seq - tcp->tcp_rnxt; 24591 rgap = tcp->tcp_rwnd - (gap + seg_len); 24592 if (gap < 0) { 24593 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 24594 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 24595 (seg_len > -gap ? -gap : seg_len)); 24596 seg_len += gap; 24597 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 24598 if (flags & TH_RST) { 24599 goto done; 24600 } 24601 if ((flags & TH_FIN) && seg_len == -1) { 24602 /* 24603 * When TCP receives a duplicate FIN in 24604 * TIME_WAIT state, restart the 2 MSL timer. 24605 * See page 73 in RFC 793. Make sure this TCP 24606 * is already on the TIME_WAIT list. If not, 24607 * just restart the timer. 24608 */ 24609 if (TCP_IS_DETACHED(tcp)) { 24610 if (tcp_time_wait_remove(tcp, NULL) == 24611 B_TRUE) { 24612 tcp_time_wait_append(tcp); 24613 TCP_DBGSTAT(tcps, 24614 tcp_rput_time_wait); 24615 } 24616 } else { 24617 ASSERT(tcp != NULL); 24618 TCP_TIMER_RESTART(tcp, 24619 tcps->tcps_time_wait_interval); 24620 } 24621 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24622 tcp->tcp_rnxt, TH_ACK); 24623 goto done; 24624 } 24625 flags |= TH_ACK_NEEDED; 24626 seg_len = 0; 24627 goto process_ack; 24628 } 24629 24630 /* Fix seg_seq, and chew the gap off the front. */ 24631 seg_seq = tcp->tcp_rnxt; 24632 } 24633 24634 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 24635 /* 24636 * Make sure that when we accept the connection, pick 24637 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 24638 * old connection. 24639 * 24640 * The next ISS generated is equal to tcp_iss_incr_extra 24641 * + ISS_INCR/2 + other components depending on the 24642 * value of tcp_strong_iss. We pre-calculate the new 24643 * ISS here and compare with tcp_snxt to determine if 24644 * we need to make adjustment to tcp_iss_incr_extra. 24645 * 24646 * The above calculation is ugly and is a 24647 * waste of CPU cycles... 24648 */ 24649 uint32_t new_iss = tcps->tcps_iss_incr_extra; 24650 int32_t adj; 24651 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24652 24653 switch (tcps->tcps_strong_iss) { 24654 case 2: { 24655 /* Add time and MD5 components. */ 24656 uint32_t answer[4]; 24657 struct { 24658 uint32_t ports; 24659 in6_addr_t src; 24660 in6_addr_t dst; 24661 } arg; 24662 MD5_CTX context; 24663 24664 mutex_enter(&tcps->tcps_iss_key_lock); 24665 context = tcps->tcps_iss_key; 24666 mutex_exit(&tcps->tcps_iss_key_lock); 24667 arg.ports = tcp->tcp_ports; 24668 /* We use MAPPED addresses in tcp_iss_init */ 24669 arg.src = tcp->tcp_ip_src_v6; 24670 if (tcp->tcp_ipversion == IPV4_VERSION) { 24671 IN6_IPADDR_TO_V4MAPPED( 24672 tcp->tcp_ipha->ipha_dst, 24673 &arg.dst); 24674 } else { 24675 arg.dst = 24676 tcp->tcp_ip6h->ip6_dst; 24677 } 24678 MD5Update(&context, (uchar_t *)&arg, 24679 sizeof (arg)); 24680 MD5Final((uchar_t *)answer, &context); 24681 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 24682 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 24683 break; 24684 } 24685 case 1: 24686 /* Add time component and min random (i.e. 1). */ 24687 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 24688 break; 24689 default: 24690 /* Add only time component. */ 24691 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24692 break; 24693 } 24694 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 24695 /* 24696 * New ISS not guaranteed to be ISS_INCR/2 24697 * ahead of the current tcp_snxt, so add the 24698 * difference to tcp_iss_incr_extra. 24699 */ 24700 tcps->tcps_iss_incr_extra += adj; 24701 } 24702 /* 24703 * If tcp_clean_death() can not perform the task now, 24704 * drop the SYN packet and let the other side re-xmit. 24705 * Otherwise pass the SYN packet back in, since the 24706 * old tcp state has been cleaned up or freed. 24707 */ 24708 if (tcp_clean_death(tcp, 0, 27) == -1) 24709 goto done; 24710 /* 24711 * We will come back to tcp_rput_data 24712 * on the global queue. Packets destined 24713 * for the global queue will be checked 24714 * with global policy. But the policy for 24715 * this packet has already been checked as 24716 * this was destined for the detached 24717 * connection. We need to bypass policy 24718 * check this time by attaching a dummy 24719 * ipsec_in with ipsec_in_dont_check set. 24720 */ 24721 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 24722 if (connp != NULL) { 24723 TCP_STAT(tcps, tcp_time_wait_syn_success); 24724 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 24725 return; 24726 } 24727 goto done; 24728 } 24729 24730 /* 24731 * rgap is the amount of stuff received out of window. A negative 24732 * value is the amount out of window. 24733 */ 24734 if (rgap < 0) { 24735 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 24736 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 24737 /* Fix seg_len and make sure there is something left. */ 24738 seg_len += rgap; 24739 if (seg_len <= 0) { 24740 if (flags & TH_RST) { 24741 goto done; 24742 } 24743 flags |= TH_ACK_NEEDED; 24744 seg_len = 0; 24745 goto process_ack; 24746 } 24747 } 24748 /* 24749 * Check whether we can update tcp_ts_recent. This test is 24750 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 24751 * Extensions for High Performance: An Update", Internet Draft. 24752 */ 24753 if (tcp->tcp_snd_ts_ok && 24754 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 24755 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 24756 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 24757 tcp->tcp_last_rcv_lbolt = lbolt64; 24758 } 24759 24760 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 24761 /* Always ack out of order packets */ 24762 flags |= TH_ACK_NEEDED; 24763 seg_len = 0; 24764 } else if (seg_len > 0) { 24765 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 24766 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 24767 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 24768 } 24769 if (flags & TH_RST) { 24770 (void) tcp_clean_death(tcp, 0, 28); 24771 goto done; 24772 } 24773 if (flags & TH_SYN) { 24774 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 24775 TH_RST|TH_ACK); 24776 /* 24777 * Do not delete the TCP structure if it is in 24778 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 24779 */ 24780 goto done; 24781 } 24782 process_ack: 24783 if (flags & TH_ACK) { 24784 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 24785 if (bytes_acked <= 0) { 24786 if (bytes_acked == 0 && seg_len == 0 && 24787 new_swnd == tcp->tcp_swnd) 24788 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 24789 } else { 24790 /* Acks something not sent */ 24791 flags |= TH_ACK_NEEDED; 24792 } 24793 } 24794 if (flags & TH_ACK_NEEDED) { 24795 /* 24796 * Time to send an ack for some reason. 24797 */ 24798 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24799 tcp->tcp_rnxt, TH_ACK); 24800 } 24801 done: 24802 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 24803 DB_CKSUMSTART(mp) = 0; 24804 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 24805 TCP_STAT(tcps, tcp_time_wait_syn_fail); 24806 } 24807 freemsg(mp); 24808 } 24809 24810 /* 24811 * TCP Timers Implementation. 24812 */ 24813 timeout_id_t 24814 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 24815 { 24816 mblk_t *mp; 24817 tcp_timer_t *tcpt; 24818 tcp_t *tcp = connp->conn_tcp; 24819 24820 ASSERT(connp->conn_sqp != NULL); 24821 24822 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 24823 24824 if (tcp->tcp_timercache == NULL) { 24825 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 24826 } else { 24827 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 24828 mp = tcp->tcp_timercache; 24829 tcp->tcp_timercache = mp->b_next; 24830 mp->b_next = NULL; 24831 ASSERT(mp->b_wptr == NULL); 24832 } 24833 24834 CONN_INC_REF(connp); 24835 tcpt = (tcp_timer_t *)mp->b_rptr; 24836 tcpt->connp = connp; 24837 tcpt->tcpt_proc = f; 24838 /* 24839 * TCP timers are normal timeouts. Plus, they do not require more than 24840 * a 10 millisecond resolution. By choosing a coarser resolution and by 24841 * rounding up the expiration to the next resolution boundary, we can 24842 * batch timers in the callout subsystem to make TCP timers more 24843 * efficient. The roundup also protects short timers from expiring too 24844 * early before they have a chance to be cancelled. 24845 */ 24846 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 24847 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 24848 24849 return ((timeout_id_t)mp); 24850 } 24851 24852 static void 24853 tcp_timer_callback(void *arg) 24854 { 24855 mblk_t *mp = (mblk_t *)arg; 24856 tcp_timer_t *tcpt; 24857 conn_t *connp; 24858 24859 tcpt = (tcp_timer_t *)mp->b_rptr; 24860 connp = tcpt->connp; 24861 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 24862 SQ_FILL, SQTAG_TCP_TIMER); 24863 } 24864 24865 static void 24866 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 24867 { 24868 tcp_timer_t *tcpt; 24869 conn_t *connp = (conn_t *)arg; 24870 tcp_t *tcp = connp->conn_tcp; 24871 24872 tcpt = (tcp_timer_t *)mp->b_rptr; 24873 ASSERT(connp == tcpt->connp); 24874 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 24875 24876 /* 24877 * If the TCP has reached the closed state, don't proceed any 24878 * further. This TCP logically does not exist on the system. 24879 * tcpt_proc could for example access queues, that have already 24880 * been qprocoff'ed off. Also see comments at the start of tcp_input 24881 */ 24882 if (tcp->tcp_state != TCPS_CLOSED) { 24883 (*tcpt->tcpt_proc)(connp); 24884 } else { 24885 tcp->tcp_timer_tid = 0; 24886 } 24887 tcp_timer_free(connp->conn_tcp, mp); 24888 } 24889 24890 /* 24891 * There is potential race with untimeout and the handler firing at the same 24892 * time. The mblock may be freed by the handler while we are trying to use 24893 * it. But since both should execute on the same squeue, this race should not 24894 * occur. 24895 */ 24896 clock_t 24897 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 24898 { 24899 mblk_t *mp = (mblk_t *)id; 24900 tcp_timer_t *tcpt; 24901 clock_t delta; 24902 24903 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 24904 24905 if (mp == NULL) 24906 return (-1); 24907 24908 tcpt = (tcp_timer_t *)mp->b_rptr; 24909 ASSERT(tcpt->connp == connp); 24910 24911 delta = untimeout_default(tcpt->tcpt_tid, 0); 24912 24913 if (delta >= 0) { 24914 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 24915 tcp_timer_free(connp->conn_tcp, mp); 24916 CONN_DEC_REF(connp); 24917 } 24918 24919 return (delta); 24920 } 24921 24922 /* 24923 * Allocate space for the timer event. The allocation looks like mblk, but it is 24924 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 24925 * 24926 * Dealing with failures: If we can't allocate from the timer cache we try 24927 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 24928 * points to b_rptr. 24929 * If we can't allocate anything using allocb_tryhard(), we perform a last 24930 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 24931 * save the actual allocation size in b_datap. 24932 */ 24933 mblk_t * 24934 tcp_timermp_alloc(int kmflags) 24935 { 24936 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 24937 kmflags & ~KM_PANIC); 24938 24939 if (mp != NULL) { 24940 mp->b_next = mp->b_prev = NULL; 24941 mp->b_rptr = (uchar_t *)(&mp[1]); 24942 mp->b_wptr = NULL; 24943 mp->b_datap = NULL; 24944 mp->b_queue = NULL; 24945 mp->b_cont = NULL; 24946 } else if (kmflags & KM_PANIC) { 24947 /* 24948 * Failed to allocate memory for the timer. Try allocating from 24949 * dblock caches. 24950 */ 24951 /* ipclassifier calls this from a constructor - hence no tcps */ 24952 TCP_G_STAT(tcp_timermp_allocfail); 24953 mp = allocb_tryhard(sizeof (tcp_timer_t)); 24954 if (mp == NULL) { 24955 size_t size = 0; 24956 /* 24957 * Memory is really low. Try tryhard allocation. 24958 * 24959 * ipclassifier calls this from a constructor - 24960 * hence no tcps 24961 */ 24962 TCP_G_STAT(tcp_timermp_allocdblfail); 24963 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 24964 sizeof (tcp_timer_t), &size, kmflags); 24965 mp->b_rptr = (uchar_t *)(&mp[1]); 24966 mp->b_next = mp->b_prev = NULL; 24967 mp->b_wptr = (uchar_t *)-1; 24968 mp->b_datap = (dblk_t *)size; 24969 mp->b_queue = NULL; 24970 mp->b_cont = NULL; 24971 } 24972 ASSERT(mp->b_wptr != NULL); 24973 } 24974 /* ipclassifier calls this from a constructor - hence no tcps */ 24975 TCP_G_DBGSTAT(tcp_timermp_alloced); 24976 24977 return (mp); 24978 } 24979 24980 /* 24981 * Free per-tcp timer cache. 24982 * It can only contain entries from tcp_timercache. 24983 */ 24984 void 24985 tcp_timermp_free(tcp_t *tcp) 24986 { 24987 mblk_t *mp; 24988 24989 while ((mp = tcp->tcp_timercache) != NULL) { 24990 ASSERT(mp->b_wptr == NULL); 24991 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 24992 kmem_cache_free(tcp_timercache, mp); 24993 } 24994 } 24995 24996 /* 24997 * Free timer event. Put it on the per-tcp timer cache if there is not too many 24998 * events there already (currently at most two events are cached). 24999 * If the event is not allocated from the timer cache, free it right away. 25000 */ 25001 static void 25002 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 25003 { 25004 mblk_t *mp1 = tcp->tcp_timercache; 25005 25006 if (mp->b_wptr != NULL) { 25007 /* 25008 * This allocation is not from a timer cache, free it right 25009 * away. 25010 */ 25011 if (mp->b_wptr != (uchar_t *)-1) 25012 freeb(mp); 25013 else 25014 kmem_free(mp, (size_t)mp->b_datap); 25015 } else if (mp1 == NULL || mp1->b_next == NULL) { 25016 /* Cache this timer block for future allocations */ 25017 mp->b_rptr = (uchar_t *)(&mp[1]); 25018 mp->b_next = mp1; 25019 tcp->tcp_timercache = mp; 25020 } else { 25021 kmem_cache_free(tcp_timercache, mp); 25022 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 25023 } 25024 } 25025 25026 /* 25027 * End of TCP Timers implementation. 25028 */ 25029 25030 /* 25031 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 25032 * on the specified backing STREAMS q. Note, the caller may make the 25033 * decision to call based on the tcp_t.tcp_flow_stopped value which 25034 * when check outside the q's lock is only an advisory check ... 25035 */ 25036 void 25037 tcp_setqfull(tcp_t *tcp) 25038 { 25039 tcp_stack_t *tcps = tcp->tcp_tcps; 25040 conn_t *connp = tcp->tcp_connp; 25041 25042 if (tcp->tcp_closed) 25043 return; 25044 25045 if (IPCL_IS_NONSTR(connp)) { 25046 (*connp->conn_upcalls->su_txq_full) 25047 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 25048 tcp->tcp_flow_stopped = B_TRUE; 25049 } else { 25050 queue_t *q = tcp->tcp_wq; 25051 25052 if (!(q->q_flag & QFULL)) { 25053 mutex_enter(QLOCK(q)); 25054 if (!(q->q_flag & QFULL)) { 25055 /* still need to set QFULL */ 25056 q->q_flag |= QFULL; 25057 tcp->tcp_flow_stopped = B_TRUE; 25058 mutex_exit(QLOCK(q)); 25059 TCP_STAT(tcps, tcp_flwctl_on); 25060 } else { 25061 mutex_exit(QLOCK(q)); 25062 } 25063 } 25064 } 25065 } 25066 25067 void 25068 tcp_clrqfull(tcp_t *tcp) 25069 { 25070 conn_t *connp = tcp->tcp_connp; 25071 25072 if (tcp->tcp_closed) 25073 return; 25074 25075 if (IPCL_IS_NONSTR(connp)) { 25076 (*connp->conn_upcalls->su_txq_full) 25077 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 25078 tcp->tcp_flow_stopped = B_FALSE; 25079 } else { 25080 queue_t *q = tcp->tcp_wq; 25081 25082 if (q->q_flag & QFULL) { 25083 mutex_enter(QLOCK(q)); 25084 if (q->q_flag & QFULL) { 25085 q->q_flag &= ~QFULL; 25086 tcp->tcp_flow_stopped = B_FALSE; 25087 mutex_exit(QLOCK(q)); 25088 if (q->q_flag & QWANTW) 25089 qbackenable(q, 0); 25090 } else { 25091 mutex_exit(QLOCK(q)); 25092 } 25093 } 25094 } 25095 } 25096 25097 /* 25098 * kstats related to squeues i.e. not per IP instance 25099 */ 25100 static void * 25101 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 25102 { 25103 kstat_t *ksp; 25104 25105 tcp_g_stat_t template = { 25106 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 25107 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 25108 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 25109 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 25110 }; 25111 25112 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 25113 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25114 KSTAT_FLAG_VIRTUAL); 25115 25116 if (ksp == NULL) 25117 return (NULL); 25118 25119 bcopy(&template, tcp_g_statp, sizeof (template)); 25120 ksp->ks_data = (void *)tcp_g_statp; 25121 25122 kstat_install(ksp); 25123 return (ksp); 25124 } 25125 25126 static void 25127 tcp_g_kstat_fini(kstat_t *ksp) 25128 { 25129 if (ksp != NULL) { 25130 kstat_delete(ksp); 25131 } 25132 } 25133 25134 25135 static void * 25136 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 25137 { 25138 kstat_t *ksp; 25139 25140 tcp_stat_t template = { 25141 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 25142 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 25143 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 25144 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 25145 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 25146 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 25147 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 25148 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 25149 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 25150 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 25151 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 25152 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 25153 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 25154 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 25155 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 25156 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 25157 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 25158 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 25159 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 25160 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 25161 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 25162 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 25163 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 25164 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 25165 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 25166 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 25167 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 25168 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 25169 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 25170 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 25171 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 25172 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 25173 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 25174 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 25175 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 25176 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 25177 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 25178 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 25179 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 25180 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 25181 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 25182 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 25183 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 25184 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 25185 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 25186 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 25187 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 25188 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 25189 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 25190 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 25191 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 25192 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 25193 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 25194 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 25195 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 25196 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 25197 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 25198 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 25199 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 25200 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 25201 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 25202 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 25203 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 25204 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 25205 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 25206 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 25207 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 25208 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 25209 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 25210 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 25211 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 25212 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 25213 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 25214 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 25215 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 25216 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 25217 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 25218 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 25219 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 25220 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 25221 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 25222 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 25223 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 25224 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 25225 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 25226 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 25227 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 25228 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 25229 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 25230 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 25231 }; 25232 25233 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 25234 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25235 KSTAT_FLAG_VIRTUAL, stackid); 25236 25237 if (ksp == NULL) 25238 return (NULL); 25239 25240 bcopy(&template, tcps_statisticsp, sizeof (template)); 25241 ksp->ks_data = (void *)tcps_statisticsp; 25242 ksp->ks_private = (void *)(uintptr_t)stackid; 25243 25244 kstat_install(ksp); 25245 return (ksp); 25246 } 25247 25248 static void 25249 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 25250 { 25251 if (ksp != NULL) { 25252 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25253 kstat_delete_netstack(ksp, stackid); 25254 } 25255 } 25256 25257 /* 25258 * TCP Kstats implementation 25259 */ 25260 static void * 25261 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 25262 { 25263 kstat_t *ksp; 25264 25265 tcp_named_kstat_t template = { 25266 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 25267 { "rtoMin", KSTAT_DATA_INT32, 0 }, 25268 { "rtoMax", KSTAT_DATA_INT32, 0 }, 25269 { "maxConn", KSTAT_DATA_INT32, 0 }, 25270 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 25271 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 25272 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 25273 { "estabResets", KSTAT_DATA_UINT32, 0 }, 25274 { "currEstab", KSTAT_DATA_UINT32, 0 }, 25275 { "inSegs", KSTAT_DATA_UINT64, 0 }, 25276 { "outSegs", KSTAT_DATA_UINT64, 0 }, 25277 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 25278 { "connTableSize", KSTAT_DATA_INT32, 0 }, 25279 { "outRsts", KSTAT_DATA_UINT32, 0 }, 25280 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 25281 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 25282 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 25283 { "outAck", KSTAT_DATA_UINT32, 0 }, 25284 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 25285 { "outUrg", KSTAT_DATA_UINT32, 0 }, 25286 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 25287 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 25288 { "outControl", KSTAT_DATA_UINT32, 0 }, 25289 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 25290 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 25291 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 25292 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 25293 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 25294 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 25295 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 25296 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 25297 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 25298 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 25299 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 25300 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 25301 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 25302 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 25303 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 25304 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 25305 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 25306 { "inClosed", KSTAT_DATA_UINT32, 0 }, 25307 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 25308 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 25309 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 25310 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 25311 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 25312 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 25313 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 25314 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 25315 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 25316 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 25317 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 25318 { "connTableSize6", KSTAT_DATA_INT32, 0 } 25319 }; 25320 25321 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 25322 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 25323 25324 if (ksp == NULL) 25325 return (NULL); 25326 25327 template.rtoAlgorithm.value.ui32 = 4; 25328 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 25329 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 25330 template.maxConn.value.i32 = -1; 25331 25332 bcopy(&template, ksp->ks_data, sizeof (template)); 25333 ksp->ks_update = tcp_kstat_update; 25334 ksp->ks_private = (void *)(uintptr_t)stackid; 25335 25336 kstat_install(ksp); 25337 return (ksp); 25338 } 25339 25340 static void 25341 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 25342 { 25343 if (ksp != NULL) { 25344 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25345 kstat_delete_netstack(ksp, stackid); 25346 } 25347 } 25348 25349 static int 25350 tcp_kstat_update(kstat_t *kp, int rw) 25351 { 25352 tcp_named_kstat_t *tcpkp; 25353 tcp_t *tcp; 25354 connf_t *connfp; 25355 conn_t *connp; 25356 int i; 25357 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 25358 netstack_t *ns; 25359 tcp_stack_t *tcps; 25360 ip_stack_t *ipst; 25361 25362 if ((kp == NULL) || (kp->ks_data == NULL)) 25363 return (EIO); 25364 25365 if (rw == KSTAT_WRITE) 25366 return (EACCES); 25367 25368 ns = netstack_find_by_stackid(stackid); 25369 if (ns == NULL) 25370 return (-1); 25371 tcps = ns->netstack_tcp; 25372 if (tcps == NULL) { 25373 netstack_rele(ns); 25374 return (-1); 25375 } 25376 25377 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 25378 25379 tcpkp->currEstab.value.ui32 = 0; 25380 25381 ipst = ns->netstack_ip; 25382 25383 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25384 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25385 connp = NULL; 25386 while ((connp = 25387 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25388 tcp = connp->conn_tcp; 25389 switch (tcp_snmp_state(tcp)) { 25390 case MIB2_TCP_established: 25391 case MIB2_TCP_closeWait: 25392 tcpkp->currEstab.value.ui32++; 25393 break; 25394 } 25395 } 25396 } 25397 25398 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 25399 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 25400 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 25401 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 25402 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 25403 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 25404 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 25405 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 25406 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 25407 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 25408 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 25409 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 25410 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 25411 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 25412 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 25413 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 25414 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 25415 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 25416 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 25417 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 25418 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 25419 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 25420 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 25421 tcpkp->inDataInorderSegs.value.ui32 = 25422 tcps->tcps_mib.tcpInDataInorderSegs; 25423 tcpkp->inDataInorderBytes.value.ui32 = 25424 tcps->tcps_mib.tcpInDataInorderBytes; 25425 tcpkp->inDataUnorderSegs.value.ui32 = 25426 tcps->tcps_mib.tcpInDataUnorderSegs; 25427 tcpkp->inDataUnorderBytes.value.ui32 = 25428 tcps->tcps_mib.tcpInDataUnorderBytes; 25429 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 25430 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 25431 tcpkp->inDataPartDupSegs.value.ui32 = 25432 tcps->tcps_mib.tcpInDataPartDupSegs; 25433 tcpkp->inDataPartDupBytes.value.ui32 = 25434 tcps->tcps_mib.tcpInDataPartDupBytes; 25435 tcpkp->inDataPastWinSegs.value.ui32 = 25436 tcps->tcps_mib.tcpInDataPastWinSegs; 25437 tcpkp->inDataPastWinBytes.value.ui32 = 25438 tcps->tcps_mib.tcpInDataPastWinBytes; 25439 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 25440 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 25441 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 25442 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 25443 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 25444 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 25445 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 25446 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 25447 tcpkp->timKeepaliveProbe.value.ui32 = 25448 tcps->tcps_mib.tcpTimKeepaliveProbe; 25449 tcpkp->timKeepaliveDrop.value.ui32 = 25450 tcps->tcps_mib.tcpTimKeepaliveDrop; 25451 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 25452 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 25453 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 25454 tcpkp->outSackRetransSegs.value.ui32 = 25455 tcps->tcps_mib.tcpOutSackRetransSegs; 25456 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 25457 25458 netstack_rele(ns); 25459 return (0); 25460 } 25461 25462 void 25463 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25464 { 25465 uint16_t hdr_len; 25466 ipha_t *ipha; 25467 uint8_t *nexthdrp; 25468 tcph_t *tcph; 25469 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 25470 25471 /* Already has an eager */ 25472 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25473 TCP_STAT(tcps, tcp_reinput_syn); 25474 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25475 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 25476 return; 25477 } 25478 25479 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25480 case IPV4_VERSION: 25481 ipha = (ipha_t *)mp->b_rptr; 25482 hdr_len = IPH_HDR_LENGTH(ipha); 25483 break; 25484 case IPV6_VERSION: 25485 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25486 &hdr_len, &nexthdrp)) { 25487 CONN_DEC_REF(connp); 25488 freemsg(mp); 25489 return; 25490 } 25491 break; 25492 } 25493 25494 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25495 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25496 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25497 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25498 } 25499 25500 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25501 SQ_FILL, SQTAG_TCP_REINPUT); 25502 } 25503 25504 static int 25505 tcp_squeue_switch(int val) 25506 { 25507 int rval = SQ_FILL; 25508 25509 switch (val) { 25510 case 1: 25511 rval = SQ_NODRAIN; 25512 break; 25513 case 2: 25514 rval = SQ_PROCESS; 25515 break; 25516 default: 25517 break; 25518 } 25519 return (rval); 25520 } 25521 25522 /* 25523 * This is called once for each squeue - globally for all stack 25524 * instances. 25525 */ 25526 static void 25527 tcp_squeue_add(squeue_t *sqp) 25528 { 25529 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25530 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25531 25532 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25533 tcp_time_wait->tcp_time_wait_tid = 25534 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 25535 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 25536 CALLOUT_FLAG_ROUNDUP); 25537 if (tcp_free_list_max_cnt == 0) { 25538 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25539 max_ncpus : boot_max_ncpus); 25540 25541 /* 25542 * Limit number of entries to 1% of availble memory / tcp_ncpus 25543 */ 25544 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25545 (tcp_ncpus * sizeof (tcp_t) * 100); 25546 } 25547 tcp_time_wait->tcp_free_list_cnt = 0; 25548 } 25549 25550 static int 25551 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 25552 { 25553 mblk_t *ire_mp = NULL; 25554 mblk_t *syn_mp; 25555 mblk_t *mdti; 25556 mblk_t *lsoi; 25557 int retval; 25558 tcph_t *tcph; 25559 uint32_t mss; 25560 queue_t *q = tcp->tcp_rq; 25561 conn_t *connp = tcp->tcp_connp; 25562 tcp_stack_t *tcps = tcp->tcp_tcps; 25563 25564 if (error == 0) { 25565 /* 25566 * Adapt Multidata information, if any. The 25567 * following tcp_mdt_update routine will free 25568 * the message. 25569 */ 25570 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 25571 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 25572 b_rptr)->mdt_capab, B_TRUE); 25573 freemsg(mdti); 25574 } 25575 25576 /* 25577 * Check to update LSO information with tcp, and 25578 * tcp_lso_update routine will free the message. 25579 */ 25580 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 25581 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 25582 b_rptr)->lso_capab); 25583 freemsg(lsoi); 25584 } 25585 25586 /* Get the IRE, if we had requested for it */ 25587 if (mp != NULL) 25588 ire_mp = tcp_ire_mp(&mp); 25589 25590 if (tcp->tcp_hard_binding) { 25591 tcp->tcp_hard_binding = B_FALSE; 25592 tcp->tcp_hard_bound = B_TRUE; 25593 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 25594 if (retval != 0) { 25595 error = EADDRINUSE; 25596 goto bind_failed; 25597 } 25598 } else { 25599 if (ire_mp != NULL) 25600 freeb(ire_mp); 25601 goto after_syn_sent; 25602 } 25603 25604 retval = tcp_adapt_ire(tcp, ire_mp); 25605 if (ire_mp != NULL) 25606 freeb(ire_mp); 25607 if (retval == 0) { 25608 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 25609 ENETUNREACH : EADDRNOTAVAIL); 25610 goto ipcl_rm; 25611 } 25612 /* 25613 * Don't let an endpoint connect to itself. 25614 * Also checked in tcp_connect() but that 25615 * check can't handle the case when the 25616 * local IP address is INADDR_ANY. 25617 */ 25618 if (tcp->tcp_ipversion == IPV4_VERSION) { 25619 if ((tcp->tcp_ipha->ipha_dst == 25620 tcp->tcp_ipha->ipha_src) && 25621 (BE16_EQL(tcp->tcp_tcph->th_lport, 25622 tcp->tcp_tcph->th_fport))) { 25623 error = EADDRNOTAVAIL; 25624 goto ipcl_rm; 25625 } 25626 } else { 25627 if (IN6_ARE_ADDR_EQUAL( 25628 &tcp->tcp_ip6h->ip6_dst, 25629 &tcp->tcp_ip6h->ip6_src) && 25630 (BE16_EQL(tcp->tcp_tcph->th_lport, 25631 tcp->tcp_tcph->th_fport))) { 25632 error = EADDRNOTAVAIL; 25633 goto ipcl_rm; 25634 } 25635 } 25636 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 25637 /* 25638 * This should not be possible! Just for 25639 * defensive coding... 25640 */ 25641 if (tcp->tcp_state != TCPS_SYN_SENT) 25642 goto after_syn_sent; 25643 25644 if (is_system_labeled() && 25645 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 25646 error = EHOSTUNREACH; 25647 goto ipcl_rm; 25648 } 25649 25650 /* 25651 * tcp_adapt_ire() does not adjust 25652 * for TCP/IP header length. 25653 */ 25654 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 25655 25656 /* 25657 * Just make sure our rwnd is at 25658 * least tcp_recv_hiwat_mss * MSS 25659 * large, and round up to the nearest 25660 * MSS. 25661 * 25662 * We do the round up here because 25663 * we need to get the interface 25664 * MTU first before we can do the 25665 * round up. 25666 */ 25667 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 25668 tcps->tcps_recv_hiwat_minmss * mss); 25669 if (!IPCL_IS_NONSTR(connp)) 25670 q->q_hiwat = tcp->tcp_rwnd; 25671 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 25672 tcp_set_ws_value(tcp); 25673 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 25674 tcp->tcp_tcph->th_win); 25675 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 25676 tcp->tcp_snd_ws_ok = B_TRUE; 25677 25678 /* 25679 * Set tcp_snd_ts_ok to true 25680 * so that tcp_xmit_mp will 25681 * include the timestamp 25682 * option in the SYN segment. 25683 */ 25684 if (tcps->tcps_tstamp_always || 25685 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 25686 tcp->tcp_snd_ts_ok = B_TRUE; 25687 } 25688 25689 /* 25690 * tcp_snd_sack_ok can be set in 25691 * tcp_adapt_ire() if the sack metric 25692 * is set. So check it here also. 25693 */ 25694 if (tcps->tcps_sack_permitted == 2 || 25695 tcp->tcp_snd_sack_ok) { 25696 if (tcp->tcp_sack_info == NULL) { 25697 tcp->tcp_sack_info = 25698 kmem_cache_alloc(tcp_sack_info_cache, 25699 KM_SLEEP); 25700 } 25701 tcp->tcp_snd_sack_ok = B_TRUE; 25702 } 25703 25704 /* 25705 * Should we use ECN? Note that the current 25706 * default value (SunOS 5.9) of tcp_ecn_permitted 25707 * is 1. The reason for doing this is that there 25708 * are equipments out there that will drop ECN 25709 * enabled IP packets. Setting it to 1 avoids 25710 * compatibility problems. 25711 */ 25712 if (tcps->tcps_ecn_permitted == 2) 25713 tcp->tcp_ecn_ok = B_TRUE; 25714 25715 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 25716 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 25717 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 25718 if (syn_mp) { 25719 if (cr == NULL) { 25720 cr = tcp->tcp_cred; 25721 pid = tcp->tcp_cpid; 25722 } 25723 mblk_setcred(syn_mp, cr, pid); 25724 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 25725 } 25726 after_syn_sent: 25727 if (mp != NULL) { 25728 ASSERT(mp->b_cont == NULL); 25729 freeb(mp); 25730 } 25731 return (error); 25732 } else { 25733 /* error */ 25734 if (tcp->tcp_debug) { 25735 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 25736 "tcp_post_ip_bind: error == %d", error); 25737 } 25738 if (mp != NULL) { 25739 freeb(mp); 25740 } 25741 } 25742 25743 ipcl_rm: 25744 /* 25745 * Need to unbind with classifier since we were just 25746 * told that our bind succeeded. a.k.a error == 0 at the entry. 25747 */ 25748 tcp->tcp_hard_bound = B_FALSE; 25749 tcp->tcp_hard_binding = B_FALSE; 25750 25751 ipcl_hash_remove(connp); 25752 25753 bind_failed: 25754 tcp->tcp_state = TCPS_IDLE; 25755 if (tcp->tcp_ipversion == IPV4_VERSION) 25756 tcp->tcp_ipha->ipha_src = 0; 25757 else 25758 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 25759 /* 25760 * Copy of the src addr. in tcp_t is needed since 25761 * the lookup funcs. can only look at tcp_t 25762 */ 25763 V6_SET_ZERO(tcp->tcp_ip_src_v6); 25764 25765 tcph = tcp->tcp_tcph; 25766 tcph->th_lport[0] = 0; 25767 tcph->th_lport[1] = 0; 25768 tcp_bind_hash_remove(tcp); 25769 bzero(&connp->u_port, sizeof (connp->u_port)); 25770 /* blow away saved option results if any */ 25771 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 25772 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 25773 25774 conn_delete_ire(tcp->tcp_connp, NULL); 25775 25776 return (error); 25777 } 25778 25779 static int 25780 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 25781 boolean_t bind_to_req_port_only, cred_t *cr) 25782 { 25783 in_port_t mlp_port; 25784 mlp_type_t addrtype, mlptype; 25785 boolean_t user_specified; 25786 in_port_t allocated_port; 25787 in_port_t requested_port = *requested_port_ptr; 25788 conn_t *connp; 25789 zone_t *zone; 25790 tcp_stack_t *tcps = tcp->tcp_tcps; 25791 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 25792 25793 /* 25794 * XXX It's up to the caller to specify bind_to_req_port_only or not. 25795 */ 25796 if (cr == NULL) 25797 cr = tcp->tcp_cred; 25798 /* 25799 * Get a valid port (within the anonymous range and should not 25800 * be a privileged one) to use if the user has not given a port. 25801 * If multiple threads are here, they may all start with 25802 * with the same initial port. But, it should be fine as long as 25803 * tcp_bindi will ensure that no two threads will be assigned 25804 * the same port. 25805 * 25806 * NOTE: XXX If a privileged process asks for an anonymous port, we 25807 * still check for ports only in the range > tcp_smallest_non_priv_port, 25808 * unless TCP_ANONPRIVBIND option is set. 25809 */ 25810 mlptype = mlptSingle; 25811 mlp_port = requested_port; 25812 if (requested_port == 0) { 25813 requested_port = tcp->tcp_anon_priv_bind ? 25814 tcp_get_next_priv_port(tcp) : 25815 tcp_update_next_port(tcps->tcps_next_port_to_try, 25816 tcp, B_TRUE); 25817 if (requested_port == 0) { 25818 return (-TNOADDR); 25819 } 25820 user_specified = B_FALSE; 25821 25822 /* 25823 * If the user went through one of the RPC interfaces to create 25824 * this socket and RPC is MLP in this zone, then give him an 25825 * anonymous MLP. 25826 */ 25827 connp = tcp->tcp_connp; 25828 if (connp->conn_anon_mlp && is_system_labeled()) { 25829 zone = crgetzone(cr); 25830 addrtype = tsol_mlp_addr_type(zone->zone_id, 25831 IPV6_VERSION, &v6addr, 25832 tcps->tcps_netstack->netstack_ip); 25833 if (addrtype == mlptSingle) { 25834 return (-TNOADDR); 25835 } 25836 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 25837 PMAPPORT, addrtype); 25838 mlp_port = PMAPPORT; 25839 } 25840 } else { 25841 int i; 25842 boolean_t priv = B_FALSE; 25843 25844 /* 25845 * If the requested_port is in the well-known privileged range, 25846 * verify that the stream was opened by a privileged user. 25847 * Note: No locks are held when inspecting tcp_g_*epriv_ports 25848 * but instead the code relies on: 25849 * - the fact that the address of the array and its size never 25850 * changes 25851 * - the atomic assignment of the elements of the array 25852 */ 25853 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 25854 priv = B_TRUE; 25855 } else { 25856 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 25857 if (requested_port == 25858 tcps->tcps_g_epriv_ports[i]) { 25859 priv = B_TRUE; 25860 break; 25861 } 25862 } 25863 } 25864 if (priv) { 25865 if (secpolicy_net_privaddr(cr, requested_port, 25866 IPPROTO_TCP) != 0) { 25867 if (tcp->tcp_debug) { 25868 (void) strlog(TCP_MOD_ID, 0, 1, 25869 SL_ERROR|SL_TRACE, 25870 "tcp_bind: no priv for port %d", 25871 requested_port); 25872 } 25873 return (-TACCES); 25874 } 25875 } 25876 user_specified = B_TRUE; 25877 25878 connp = tcp->tcp_connp; 25879 if (is_system_labeled()) { 25880 zone = crgetzone(cr); 25881 addrtype = tsol_mlp_addr_type(zone->zone_id, 25882 IPV6_VERSION, &v6addr, 25883 tcps->tcps_netstack->netstack_ip); 25884 if (addrtype == mlptSingle) { 25885 return (-TNOADDR); 25886 } 25887 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 25888 requested_port, addrtype); 25889 } 25890 } 25891 25892 if (mlptype != mlptSingle) { 25893 if (secpolicy_net_bindmlp(cr) != 0) { 25894 if (tcp->tcp_debug) { 25895 (void) strlog(TCP_MOD_ID, 0, 1, 25896 SL_ERROR|SL_TRACE, 25897 "tcp_bind: no priv for multilevel port %d", 25898 requested_port); 25899 } 25900 return (-TACCES); 25901 } 25902 25903 /* 25904 * If we're specifically binding a shared IP address and the 25905 * port is MLP on shared addresses, then check to see if this 25906 * zone actually owns the MLP. Reject if not. 25907 */ 25908 if (mlptype == mlptShared && addrtype == mlptShared) { 25909 /* 25910 * No need to handle exclusive-stack zones since 25911 * ALL_ZONES only applies to the shared stack. 25912 */ 25913 zoneid_t mlpzone; 25914 25915 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 25916 htons(mlp_port)); 25917 if (connp->conn_zoneid != mlpzone) { 25918 if (tcp->tcp_debug) { 25919 (void) strlog(TCP_MOD_ID, 0, 1, 25920 SL_ERROR|SL_TRACE, 25921 "tcp_bind: attempt to bind port " 25922 "%d on shared addr in zone %d " 25923 "(should be %d)", 25924 mlp_port, connp->conn_zoneid, 25925 mlpzone); 25926 } 25927 return (-TACCES); 25928 } 25929 } 25930 25931 if (!user_specified) { 25932 int err; 25933 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 25934 requested_port, B_TRUE); 25935 if (err != 0) { 25936 if (tcp->tcp_debug) { 25937 (void) strlog(TCP_MOD_ID, 0, 1, 25938 SL_ERROR|SL_TRACE, 25939 "tcp_bind: cannot establish anon " 25940 "MLP for port %d", 25941 requested_port); 25942 } 25943 return (err); 25944 } 25945 connp->conn_anon_port = B_TRUE; 25946 } 25947 connp->conn_mlp_type = mlptype; 25948 } 25949 25950 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 25951 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 25952 25953 if (allocated_port == 0) { 25954 connp->conn_mlp_type = mlptSingle; 25955 if (connp->conn_anon_port) { 25956 connp->conn_anon_port = B_FALSE; 25957 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 25958 requested_port, B_FALSE); 25959 } 25960 if (bind_to_req_port_only) { 25961 if (tcp->tcp_debug) { 25962 (void) strlog(TCP_MOD_ID, 0, 1, 25963 SL_ERROR|SL_TRACE, 25964 "tcp_bind: requested addr busy"); 25965 } 25966 return (-TADDRBUSY); 25967 } else { 25968 /* If we are out of ports, fail the bind. */ 25969 if (tcp->tcp_debug) { 25970 (void) strlog(TCP_MOD_ID, 0, 1, 25971 SL_ERROR|SL_TRACE, 25972 "tcp_bind: out of ports?"); 25973 } 25974 return (-TNOADDR); 25975 } 25976 } 25977 25978 /* Pass the allocated port back */ 25979 *requested_port_ptr = allocated_port; 25980 return (0); 25981 } 25982 25983 static int 25984 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 25985 boolean_t bind_to_req_port_only) 25986 { 25987 tcp_t *tcp = connp->conn_tcp; 25988 sin_t *sin; 25989 sin6_t *sin6; 25990 in_port_t requested_port; 25991 ipaddr_t v4addr; 25992 in6_addr_t v6addr; 25993 uint_t origipversion; 25994 int error = 0; 25995 25996 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 25997 25998 if (tcp->tcp_state == TCPS_BOUND) { 25999 return (0); 26000 } else if (tcp->tcp_state > TCPS_BOUND) { 26001 if (tcp->tcp_debug) { 26002 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26003 "tcp_bind: bad state, %d", tcp->tcp_state); 26004 } 26005 return (-TOUTSTATE); 26006 } 26007 origipversion = tcp->tcp_ipversion; 26008 26009 ASSERT(sa != NULL && len != 0); 26010 26011 if (!OK_32PTR((char *)sa)) { 26012 if (tcp->tcp_debug) { 26013 (void) strlog(TCP_MOD_ID, 0, 1, 26014 SL_ERROR|SL_TRACE, 26015 "tcp_bind: bad address parameter, " 26016 "address %p, len %d", 26017 (void *)sa, len); 26018 } 26019 return (-TPROTO); 26020 } 26021 26022 switch (len) { 26023 case sizeof (sin_t): /* Complete IPv4 address */ 26024 sin = (sin_t *)sa; 26025 /* 26026 * With sockets sockfs will accept bogus sin_family in 26027 * bind() and replace it with the family used in the socket 26028 * call. 26029 */ 26030 if (sin->sin_family != AF_INET || 26031 tcp->tcp_family != AF_INET) { 26032 return (EAFNOSUPPORT); 26033 } 26034 requested_port = ntohs(sin->sin_port); 26035 tcp->tcp_ipversion = IPV4_VERSION; 26036 v4addr = sin->sin_addr.s_addr; 26037 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 26038 break; 26039 26040 case sizeof (sin6_t): /* Complete IPv6 address */ 26041 sin6 = (sin6_t *)sa; 26042 if (sin6->sin6_family != AF_INET6 || 26043 tcp->tcp_family != AF_INET6) { 26044 return (EAFNOSUPPORT); 26045 } 26046 requested_port = ntohs(sin6->sin6_port); 26047 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 26048 IPV4_VERSION : IPV6_VERSION; 26049 v6addr = sin6->sin6_addr; 26050 break; 26051 26052 default: 26053 if (tcp->tcp_debug) { 26054 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26055 "tcp_bind: bad address length, %d", len); 26056 } 26057 return (EAFNOSUPPORT); 26058 /* return (-TBADADDR); */ 26059 } 26060 26061 tcp->tcp_bound_source_v6 = v6addr; 26062 26063 /* Check for change in ipversion */ 26064 if (origipversion != tcp->tcp_ipversion) { 26065 ASSERT(tcp->tcp_family == AF_INET6); 26066 error = tcp->tcp_ipversion == IPV6_VERSION ? 26067 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 26068 if (error) { 26069 return (ENOMEM); 26070 } 26071 } 26072 26073 /* 26074 * Initialize family specific fields. Copy of the src addr. 26075 * in tcp_t is needed for the lookup funcs. 26076 */ 26077 if (tcp->tcp_ipversion == IPV6_VERSION) { 26078 tcp->tcp_ip6h->ip6_src = v6addr; 26079 } else { 26080 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 26081 } 26082 tcp->tcp_ip_src_v6 = v6addr; 26083 26084 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 26085 26086 error = tcp_bind_select_lport(tcp, &requested_port, 26087 bind_to_req_port_only, cr); 26088 26089 return (error); 26090 } 26091 26092 /* 26093 * Return unix error is tli error is TSYSERR, otherwise return a negative 26094 * tli error. 26095 */ 26096 int 26097 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26098 boolean_t bind_to_req_port_only) 26099 { 26100 int error; 26101 tcp_t *tcp = connp->conn_tcp; 26102 26103 if (tcp->tcp_state >= TCPS_BOUND) { 26104 if (tcp->tcp_debug) { 26105 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26106 "tcp_bind: bad state, %d", tcp->tcp_state); 26107 } 26108 return (-TOUTSTATE); 26109 } 26110 26111 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 26112 if (error != 0) 26113 return (error); 26114 26115 ASSERT(tcp->tcp_state == TCPS_BOUND); 26116 26117 tcp->tcp_conn_req_max = 0; 26118 26119 if (tcp->tcp_family == AF_INET6) { 26120 ASSERT(tcp->tcp_connp->conn_af_isv6); 26121 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 26122 &tcp->tcp_bound_source_v6, 0, B_FALSE); 26123 } else { 26124 ASSERT(!tcp->tcp_connp->conn_af_isv6); 26125 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 26126 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 26127 } 26128 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 26129 } 26130 26131 int 26132 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 26133 socklen_t len, cred_t *cr) 26134 { 26135 int error; 26136 conn_t *connp = (conn_t *)proto_handle; 26137 squeue_t *sqp = connp->conn_sqp; 26138 26139 /* All Solaris components should pass a cred for this operation. */ 26140 ASSERT(cr != NULL); 26141 26142 ASSERT(sqp != NULL); 26143 ASSERT(connp->conn_upper_handle != NULL); 26144 26145 error = squeue_synch_enter(sqp, connp, 0); 26146 if (error != 0) { 26147 /* failed to enter */ 26148 return (ENOSR); 26149 } 26150 26151 /* binding to a NULL address really means unbind */ 26152 if (sa == NULL) { 26153 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 26154 error = tcp_do_unbind(connp); 26155 else 26156 error = EINVAL; 26157 } else { 26158 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 26159 } 26160 26161 squeue_synch_exit(sqp, connp); 26162 26163 if (error < 0) { 26164 if (error == -TOUTSTATE) 26165 error = EINVAL; 26166 else 26167 error = proto_tlitosyserr(-error); 26168 } 26169 26170 return (error); 26171 } 26172 26173 /* 26174 * If the return value from this function is positive, it's a UNIX error. 26175 * Otherwise, if it's negative, then the absolute value is a TLI error. 26176 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 26177 */ 26178 int 26179 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 26180 cred_t *cr, pid_t pid) 26181 { 26182 tcp_t *tcp = connp->conn_tcp; 26183 sin_t *sin = (sin_t *)sa; 26184 sin6_t *sin6 = (sin6_t *)sa; 26185 ipaddr_t *dstaddrp; 26186 in_port_t dstport; 26187 uint_t srcid; 26188 int error = 0; 26189 26190 switch (len) { 26191 default: 26192 /* 26193 * Should never happen 26194 */ 26195 return (EINVAL); 26196 26197 case sizeof (sin_t): 26198 sin = (sin_t *)sa; 26199 if (sin->sin_port == 0) { 26200 return (-TBADADDR); 26201 } 26202 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 26203 return (EAFNOSUPPORT); 26204 } 26205 break; 26206 26207 case sizeof (sin6_t): 26208 sin6 = (sin6_t *)sa; 26209 if (sin6->sin6_port == 0) { 26210 return (-TBADADDR); 26211 } 26212 break; 26213 } 26214 /* 26215 * If we're connecting to an IPv4-mapped IPv6 address, we need to 26216 * make sure that the template IP header in the tcp structure is an 26217 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 26218 * need to this before we call tcp_bindi() so that the port lookup 26219 * code will look for ports in the correct port space (IPv4 and 26220 * IPv6 have separate port spaces). 26221 */ 26222 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 26223 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26224 int err = 0; 26225 26226 err = tcp_header_init_ipv4(tcp); 26227 if (err != 0) { 26228 error = ENOMEM; 26229 goto connect_failed; 26230 } 26231 if (tcp->tcp_lport != 0) 26232 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 26233 } 26234 26235 switch (tcp->tcp_state) { 26236 case TCPS_LISTEN: 26237 /* 26238 * Listening sockets are not allowed to issue connect(). 26239 */ 26240 if (IPCL_IS_NONSTR(connp)) 26241 return (EOPNOTSUPP); 26242 /* FALLTHRU */ 26243 case TCPS_IDLE: 26244 /* 26245 * We support quick connect, refer to comments in 26246 * tcp_connect_*() 26247 */ 26248 /* FALLTHRU */ 26249 case TCPS_BOUND: 26250 /* 26251 * We must bump the generation before the operation start. 26252 * This is done to ensure that any upcall made later on sends 26253 * up the right generation to the socket. 26254 */ 26255 SOCK_CONNID_BUMP(tcp->tcp_connid); 26256 26257 if (tcp->tcp_family == AF_INET6) { 26258 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26259 return (tcp_connect_ipv6(tcp, 26260 &sin6->sin6_addr, 26261 sin6->sin6_port, sin6->sin6_flowinfo, 26262 sin6->__sin6_src_id, sin6->sin6_scope_id, 26263 cr, pid)); 26264 } 26265 /* 26266 * Destination adress is mapped IPv6 address. 26267 * Source bound address should be unspecified or 26268 * IPv6 mapped address as well. 26269 */ 26270 if (!IN6_IS_ADDR_UNSPECIFIED( 26271 &tcp->tcp_bound_source_v6) && 26272 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 26273 return (EADDRNOTAVAIL); 26274 } 26275 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 26276 dstport = sin6->sin6_port; 26277 srcid = sin6->__sin6_src_id; 26278 } else { 26279 dstaddrp = &sin->sin_addr.s_addr; 26280 dstport = sin->sin_port; 26281 srcid = 0; 26282 } 26283 26284 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 26285 pid); 26286 break; 26287 default: 26288 return (-TOUTSTATE); 26289 } 26290 /* 26291 * Note: Code below is the "failure" case 26292 */ 26293 connect_failed: 26294 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26295 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26296 return (error); 26297 } 26298 26299 int 26300 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 26301 socklen_t len, sock_connid_t *id, cred_t *cr) 26302 { 26303 conn_t *connp = (conn_t *)proto_handle; 26304 tcp_t *tcp = connp->conn_tcp; 26305 squeue_t *sqp = connp->conn_sqp; 26306 int error; 26307 26308 ASSERT(connp->conn_upper_handle != NULL); 26309 26310 /* All Solaris components should pass a cred for this operation. */ 26311 ASSERT(cr != NULL); 26312 26313 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 26314 if (error != 0) { 26315 return (error); 26316 } 26317 26318 error = squeue_synch_enter(sqp, connp, 0); 26319 if (error != 0) { 26320 /* failed to enter */ 26321 return (ENOSR); 26322 } 26323 26324 /* 26325 * TCP supports quick connect, so no need to do an implicit bind 26326 */ 26327 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 26328 if (error == 0) { 26329 *id = connp->conn_tcp->tcp_connid; 26330 } else if (error < 0) { 26331 if (error == -TOUTSTATE) { 26332 switch (connp->conn_tcp->tcp_state) { 26333 case TCPS_SYN_SENT: 26334 error = EALREADY; 26335 break; 26336 case TCPS_ESTABLISHED: 26337 error = EISCONN; 26338 break; 26339 case TCPS_LISTEN: 26340 error = EOPNOTSUPP; 26341 break; 26342 default: 26343 error = EINVAL; 26344 break; 26345 } 26346 } else { 26347 error = proto_tlitosyserr(-error); 26348 } 26349 } 26350 done: 26351 squeue_synch_exit(sqp, connp); 26352 26353 return ((error == 0) ? EINPROGRESS : error); 26354 } 26355 26356 /* ARGSUSED */ 26357 sock_lower_handle_t 26358 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 26359 uint_t *smodep, int *errorp, int flags, cred_t *credp) 26360 { 26361 conn_t *connp; 26362 boolean_t isv6 = family == AF_INET6; 26363 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 26364 (proto != 0 && proto != IPPROTO_TCP)) { 26365 *errorp = EPROTONOSUPPORT; 26366 return (NULL); 26367 } 26368 26369 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 26370 if (connp == NULL) { 26371 return (NULL); 26372 } 26373 26374 /* 26375 * Put the ref for TCP. Ref for IP was already put 26376 * by ipcl_conn_create. Also Make the conn_t globally 26377 * visible to walkers 26378 */ 26379 mutex_enter(&connp->conn_lock); 26380 CONN_INC_REF_LOCKED(connp); 26381 ASSERT(connp->conn_ref == 2); 26382 connp->conn_state_flags &= ~CONN_INCIPIENT; 26383 26384 connp->conn_flags |= IPCL_NONSTR; 26385 mutex_exit(&connp->conn_lock); 26386 26387 ASSERT(errorp != NULL); 26388 *errorp = 0; 26389 *sock_downcalls = &sock_tcp_downcalls; 26390 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 26391 SM_SENDFILESUPP; 26392 26393 return ((sock_lower_handle_t)connp); 26394 } 26395 26396 /* ARGSUSED */ 26397 void 26398 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 26399 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 26400 { 26401 conn_t *connp = (conn_t *)proto_handle; 26402 struct sock_proto_props sopp; 26403 26404 ASSERT(connp->conn_upper_handle == NULL); 26405 26406 /* All Solaris components should pass a cred for this operation. */ 26407 ASSERT(cr != NULL); 26408 26409 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 26410 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 26411 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 26412 26413 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 26414 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 26415 sopp.sopp_maxpsz = INFPSZ; 26416 sopp.sopp_maxblk = INFPSZ; 26417 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 26418 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 26419 sopp.sopp_maxaddrlen = sizeof (sin6_t); 26420 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 26421 tcp_rinfo.mi_minpsz; 26422 26423 connp->conn_upcalls = sock_upcalls; 26424 connp->conn_upper_handle = sock_handle; 26425 26426 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 26427 } 26428 26429 /* ARGSUSED */ 26430 int 26431 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 26432 { 26433 conn_t *connp = (conn_t *)proto_handle; 26434 26435 ASSERT(connp->conn_upper_handle != NULL); 26436 26437 /* All Solaris components should pass a cred for this operation. */ 26438 ASSERT(cr != NULL); 26439 26440 tcp_close_common(connp, flags); 26441 26442 ip_free_helper_stream(connp); 26443 26444 /* 26445 * Drop IP's reference on the conn. This is the last reference 26446 * on the connp if the state was less than established. If the 26447 * connection has gone into timewait state, then we will have 26448 * one ref for the TCP and one more ref (total of two) for the 26449 * classifier connected hash list (a timewait connections stays 26450 * in connected hash till closed). 26451 * 26452 * We can't assert the references because there might be other 26453 * transient reference places because of some walkers or queued 26454 * packets in squeue for the timewait state. 26455 */ 26456 CONN_DEC_REF(connp); 26457 return (0); 26458 } 26459 26460 /* ARGSUSED */ 26461 int 26462 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 26463 cred_t *cr) 26464 { 26465 tcp_t *tcp; 26466 uint32_t msize; 26467 conn_t *connp = (conn_t *)proto_handle; 26468 int32_t tcpstate; 26469 26470 /* All Solaris components should pass a cred for this operation. */ 26471 ASSERT(cr != NULL); 26472 26473 ASSERT(connp->conn_ref >= 2); 26474 ASSERT(connp->conn_upper_handle != NULL); 26475 26476 if (msg->msg_controllen != 0) { 26477 return (EOPNOTSUPP); 26478 26479 } 26480 switch (DB_TYPE(mp)) { 26481 case M_DATA: 26482 tcp = connp->conn_tcp; 26483 ASSERT(tcp != NULL); 26484 26485 tcpstate = tcp->tcp_state; 26486 if (tcpstate < TCPS_ESTABLISHED) { 26487 freemsg(mp); 26488 return (ENOTCONN); 26489 } else if (tcpstate > TCPS_CLOSE_WAIT) { 26490 freemsg(mp); 26491 return (EPIPE); 26492 } 26493 26494 msize = msgdsize(mp); 26495 26496 mutex_enter(&tcp->tcp_non_sq_lock); 26497 tcp->tcp_squeue_bytes += msize; 26498 /* 26499 * Squeue Flow Control 26500 */ 26501 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 26502 tcp_setqfull(tcp); 26503 } 26504 mutex_exit(&tcp->tcp_non_sq_lock); 26505 26506 /* 26507 * The application may pass in an address in the msghdr, but 26508 * we ignore the address on connection-oriented sockets. 26509 * Just like BSD this code does not generate an error for 26510 * TCP (a CONNREQUIRED socket) when sending to an address 26511 * passed in with sendto/sendmsg. Instead the data is 26512 * delivered on the connection as if no address had been 26513 * supplied. 26514 */ 26515 CONN_INC_REF(connp); 26516 26517 if (msg != NULL && msg->msg_flags & MSG_OOB) { 26518 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 26519 tcp_output_urgent, connp, tcp_squeue_flag, 26520 SQTAG_TCP_OUTPUT); 26521 } else { 26522 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 26523 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 26524 } 26525 26526 return (0); 26527 26528 default: 26529 ASSERT(0); 26530 } 26531 26532 freemsg(mp); 26533 return (0); 26534 } 26535 26536 /* ARGSUSED */ 26537 void 26538 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 26539 { 26540 int len; 26541 uint32_t msize; 26542 conn_t *connp = (conn_t *)arg; 26543 tcp_t *tcp = connp->conn_tcp; 26544 26545 msize = msgdsize(mp); 26546 26547 len = msize - 1; 26548 if (len < 0) { 26549 freemsg(mp); 26550 return; 26551 } 26552 26553 /* 26554 * Try to force urgent data out on the wire. 26555 * Even if we have unsent data this will 26556 * at least send the urgent flag. 26557 * XXX does not handle more flag correctly. 26558 */ 26559 len += tcp->tcp_unsent; 26560 len += tcp->tcp_snxt; 26561 tcp->tcp_urg = len; 26562 tcp->tcp_valid_bits |= TCP_URG_VALID; 26563 26564 /* Bypass tcp protocol for fused tcp loopback */ 26565 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 26566 return; 26567 tcp_wput_data(tcp, mp, B_TRUE); 26568 } 26569 26570 /* ARGSUSED */ 26571 int 26572 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26573 socklen_t *addrlenp, cred_t *cr) 26574 { 26575 conn_t *connp = (conn_t *)proto_handle; 26576 tcp_t *tcp = connp->conn_tcp; 26577 26578 ASSERT(connp->conn_upper_handle != NULL); 26579 /* All Solaris components should pass a cred for this operation. */ 26580 ASSERT(cr != NULL); 26581 26582 ASSERT(tcp != NULL); 26583 26584 return (tcp_do_getpeername(tcp, addr, addrlenp)); 26585 } 26586 26587 /* ARGSUSED */ 26588 int 26589 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26590 socklen_t *addrlenp, cred_t *cr) 26591 { 26592 conn_t *connp = (conn_t *)proto_handle; 26593 tcp_t *tcp = connp->conn_tcp; 26594 26595 /* All Solaris components should pass a cred for this operation. */ 26596 ASSERT(cr != NULL); 26597 26598 ASSERT(connp->conn_upper_handle != NULL); 26599 26600 return (tcp_do_getsockname(tcp, addr, addrlenp)); 26601 } 26602 26603 /* 26604 * tcp_fallback 26605 * 26606 * A direct socket is falling back to using STREAMS. The queue 26607 * that is being passed down was created using tcp_open() with 26608 * the SO_FALLBACK flag set. As a result, the queue is not 26609 * associated with a conn, and the q_ptrs instead contain the 26610 * dev and minor area that should be used. 26611 * 26612 * The 'direct_sockfs' flag indicates whether the FireEngine 26613 * optimizations should be used. The common case would be that 26614 * optimizations are enabled, and they might be subsequently 26615 * disabled using the _SIOCSOCKFALLBACK ioctl. 26616 */ 26617 26618 /* 26619 * An active connection is falling back to TPI. Gather all the information 26620 * required by the STREAM head and TPI sonode and send it up. 26621 */ 26622 void 26623 tcp_fallback_noneager(tcp_t *tcp, mblk_t *stropt_mp, queue_t *q, 26624 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26625 { 26626 conn_t *connp = tcp->tcp_connp; 26627 struct stroptions *stropt; 26628 struct T_capability_ack tca; 26629 struct sockaddr_in6 laddr, faddr; 26630 socklen_t laddrlen, faddrlen; 26631 short opts; 26632 int error; 26633 mblk_t *mp; 26634 26635 connp->conn_dev = (dev_t)RD(q)->q_ptr; 26636 connp->conn_minor_arena = WR(q)->q_ptr; 26637 26638 RD(q)->q_ptr = WR(q)->q_ptr = connp; 26639 26640 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 26641 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 26642 26643 WR(q)->q_qinfo = &tcp_sock_winit; 26644 26645 if (!direct_sockfs) 26646 tcp_disable_direct_sockfs(tcp); 26647 26648 /* 26649 * free the helper stream 26650 */ 26651 ip_free_helper_stream(connp); 26652 26653 /* 26654 * Notify the STREAM head about options 26655 */ 26656 DB_TYPE(stropt_mp) = M_SETOPTS; 26657 stropt = (struct stroptions *)stropt_mp->b_rptr; 26658 stropt_mp->b_wptr += sizeof (struct stroptions); 26659 stropt = (struct stroptions *)stropt_mp->b_rptr; 26660 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 26661 26662 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 26663 tcp->tcp_tcps->tcps_wroff_xtra); 26664 if (tcp->tcp_snd_sack_ok) 26665 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 26666 stropt->so_hiwat = tcp->tcp_fused ? 26667 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 26668 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 26669 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 26670 26671 putnext(RD(q), stropt_mp); 26672 26673 /* 26674 * Collect the information needed to sync with the sonode 26675 */ 26676 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 26677 26678 laddrlen = faddrlen = sizeof (sin6_t); 26679 (void) tcp_do_getsockname(tcp, (struct sockaddr *)&laddr, &laddrlen); 26680 error = tcp_do_getpeername(tcp, (struct sockaddr *)&faddr, &faddrlen); 26681 if (error != 0) 26682 faddrlen = 0; 26683 26684 opts = 0; 26685 if (tcp->tcp_oobinline) 26686 opts |= SO_OOBINLINE; 26687 if (tcp->tcp_dontroute) 26688 opts |= SO_DONTROUTE; 26689 26690 /* 26691 * Notify the socket that the protocol is now quiescent, 26692 * and it's therefore safe move data from the socket 26693 * to the stream head. 26694 */ 26695 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 26696 (struct sockaddr *)&laddr, laddrlen, 26697 (struct sockaddr *)&faddr, faddrlen, opts); 26698 26699 while ((mp = tcp->tcp_rcv_list) != NULL) { 26700 tcp->tcp_rcv_list = mp->b_next; 26701 mp->b_next = NULL; 26702 putnext(q, mp); 26703 } 26704 tcp->tcp_rcv_last_head = NULL; 26705 tcp->tcp_rcv_last_tail = NULL; 26706 tcp->tcp_rcv_cnt = 0; 26707 } 26708 26709 /* 26710 * An eager is falling back to TPI. All we have to do is send 26711 * up a T_CONN_IND. 26712 */ 26713 void 26714 tcp_fallback_eager(tcp_t *eager, boolean_t direct_sockfs) 26715 { 26716 tcp_t *listener = eager->tcp_listener; 26717 mblk_t *mp = eager->tcp_conn.tcp_eager_conn_ind; 26718 26719 ASSERT(listener != NULL); 26720 ASSERT(mp != NULL); 26721 26722 eager->tcp_conn.tcp_eager_conn_ind = NULL; 26723 26724 /* 26725 * TLI/XTI applications will get confused by 26726 * sending eager as an option since it violates 26727 * the option semantics. So remove the eager as 26728 * option since TLI/XTI app doesn't need it anyway. 26729 */ 26730 if (!direct_sockfs) { 26731 struct T_conn_ind *conn_ind; 26732 26733 conn_ind = (struct T_conn_ind *)mp->b_rptr; 26734 conn_ind->OPT_length = 0; 26735 conn_ind->OPT_offset = 0; 26736 } 26737 26738 /* 26739 * Sockfs guarantees that the listener will not be closed 26740 * during fallback. So we can safely use the listener's queue. 26741 */ 26742 putnext(listener->tcp_rq, mp); 26743 } 26744 26745 int 26746 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 26747 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26748 { 26749 tcp_t *tcp; 26750 conn_t *connp = (conn_t *)proto_handle; 26751 int error; 26752 mblk_t *stropt_mp; 26753 mblk_t *ordrel_mp; 26754 mblk_t *fused_sigurp_mp; 26755 mblk_t *tcp_rsrv_mp; 26756 26757 tcp = connp->conn_tcp; 26758 26759 stropt_mp = allocb_wait(sizeof (struct stroptions), BPRI_HI, STR_NOSIG, 26760 NULL); 26761 26762 /* Pre-allocate the T_ordrel_ind mblk. */ 26763 ASSERT(tcp->tcp_ordrel_mp == NULL); 26764 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 26765 STR_NOSIG, NULL); 26766 ordrel_mp->b_datap->db_type = M_PROTO; 26767 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 26768 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 26769 26770 /* Pre-allocate the M_PCSIG used by fusion */ 26771 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 26772 26773 /* 26774 * Pre-allocate the tcp_rsrv_mp mblk. 26775 * It is possible that this conn was previously used for a streams 26776 * socket and already has tcp_rsrv_mp 26777 */ 26778 tcp_rsrv_mp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 26779 26780 /* 26781 * Enter the squeue so that no new packets can come in 26782 */ 26783 error = squeue_synch_enter(connp->conn_sqp, connp, 0); 26784 if (error != 0) { 26785 /* failed to enter, free all the pre-allocated messages. */ 26786 freeb(stropt_mp); 26787 freeb(ordrel_mp); 26788 freeb(fused_sigurp_mp); 26789 freeb(tcp_rsrv_mp); 26790 /* 26791 * We cannot process the eager, so at least send out a 26792 * RST so the peer can reconnect. 26793 */ 26794 if (tcp->tcp_listener != NULL) { 26795 (void) tcp_eager_blowoff(tcp->tcp_listener, 26796 tcp->tcp_conn_req_seqnum); 26797 } 26798 return (ENOMEM); 26799 } 26800 26801 /* 26802 * No longer a direct socket 26803 */ 26804 connp->conn_flags &= ~IPCL_NONSTR; 26805 26806 tcp->tcp_ordrel_mp = ordrel_mp; 26807 26808 if (tcp->tcp_fused) { 26809 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 26810 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 26811 } else { 26812 freeb(fused_sigurp_mp); 26813 } 26814 26815 if (tcp->tcp_rsrv_mp == NULL) { 26816 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 26817 } else { 26818 /* 26819 * reusing a conn that was previously used for streams socket 26820 */ 26821 freeb(tcp_rsrv_mp); 26822 } 26823 if (tcp->tcp_listener != NULL) { 26824 /* The eager will deal with opts when accept() is called */ 26825 freeb(stropt_mp); 26826 tcp_fallback_eager(tcp, direct_sockfs); 26827 } else { 26828 tcp_fallback_noneager(tcp, stropt_mp, q, direct_sockfs, 26829 quiesced_cb); 26830 } 26831 26832 /* 26833 * There should be atleast two ref's (IP + TCP) 26834 */ 26835 ASSERT(connp->conn_ref >= 2); 26836 squeue_synch_exit(connp->conn_sqp, connp); 26837 26838 return (0); 26839 } 26840 26841 /* ARGSUSED */ 26842 static void 26843 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 26844 { 26845 conn_t *connp = (conn_t *)arg; 26846 tcp_t *tcp = connp->conn_tcp; 26847 26848 freemsg(mp); 26849 26850 if (tcp->tcp_fused) 26851 tcp_unfuse(tcp); 26852 26853 if (tcp_xmit_end(tcp) != 0) { 26854 /* 26855 * We were crossing FINs and got a reset from 26856 * the other side. Just ignore it. 26857 */ 26858 if (tcp->tcp_debug) { 26859 (void) strlog(TCP_MOD_ID, 0, 1, 26860 SL_ERROR|SL_TRACE, 26861 "tcp_shutdown_output() out of state %s", 26862 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 26863 } 26864 } 26865 } 26866 26867 /* ARGSUSED */ 26868 int 26869 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 26870 { 26871 conn_t *connp = (conn_t *)proto_handle; 26872 tcp_t *tcp = connp->conn_tcp; 26873 26874 ASSERT(connp->conn_upper_handle != NULL); 26875 26876 /* All Solaris components should pass a cred for this operation. */ 26877 ASSERT(cr != NULL); 26878 26879 /* 26880 * X/Open requires that we check the connected state. 26881 */ 26882 if (tcp->tcp_state < TCPS_SYN_SENT) 26883 return (ENOTCONN); 26884 26885 /* shutdown the send side */ 26886 if (how != SHUT_RD) { 26887 mblk_t *bp; 26888 26889 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 26890 CONN_INC_REF(connp); 26891 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 26892 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 26893 26894 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26895 SOCK_OPCTL_SHUT_SEND, 0); 26896 } 26897 26898 /* shutdown the recv side */ 26899 if (how != SHUT_WR) 26900 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26901 SOCK_OPCTL_SHUT_RECV, 0); 26902 26903 return (0); 26904 } 26905 26906 /* 26907 * SOP_LISTEN() calls into tcp_listen(). 26908 */ 26909 /* ARGSUSED */ 26910 int 26911 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 26912 { 26913 conn_t *connp = (conn_t *)proto_handle; 26914 int error; 26915 squeue_t *sqp = connp->conn_sqp; 26916 26917 ASSERT(connp->conn_upper_handle != NULL); 26918 26919 /* All Solaris components should pass a cred for this operation. */ 26920 ASSERT(cr != NULL); 26921 26922 error = squeue_synch_enter(sqp, connp, 0); 26923 if (error != 0) { 26924 /* failed to enter */ 26925 return (ENOBUFS); 26926 } 26927 26928 error = tcp_do_listen(connp, NULL, 0, backlog, cr, FALSE); 26929 if (error == 0) { 26930 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26931 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 26932 } else if (error < 0) { 26933 if (error == -TOUTSTATE) 26934 error = EINVAL; 26935 else 26936 error = proto_tlitosyserr(-error); 26937 } 26938 squeue_synch_exit(sqp, connp); 26939 return (error); 26940 } 26941 26942 static int 26943 tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len, 26944 int backlog, cred_t *cr, boolean_t bind_to_req_port_only) 26945 { 26946 tcp_t *tcp = connp->conn_tcp; 26947 int error = 0; 26948 tcp_stack_t *tcps = tcp->tcp_tcps; 26949 26950 /* All Solaris components should pass a cred for this operation. */ 26951 ASSERT(cr != NULL); 26952 26953 if (tcp->tcp_state >= TCPS_BOUND) { 26954 if ((tcp->tcp_state == TCPS_BOUND || 26955 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 26956 /* 26957 * Handle listen() increasing backlog. 26958 * This is more "liberal" then what the TPI spec 26959 * requires but is needed to avoid a t_unbind 26960 * when handling listen() since the port number 26961 * might be "stolen" between the unbind and bind. 26962 */ 26963 goto do_listen; 26964 } 26965 if (tcp->tcp_debug) { 26966 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26967 "tcp_listen: bad state, %d", tcp->tcp_state); 26968 } 26969 return (-TOUTSTATE); 26970 } else { 26971 if (sa == NULL) { 26972 sin6_t addr; 26973 sin_t *sin; 26974 sin6_t *sin6; 26975 26976 ASSERT(IPCL_IS_NONSTR(connp)); 26977 26978 /* Do an implicit bind: Request for a generic port. */ 26979 if (tcp->tcp_family == AF_INET) { 26980 len = sizeof (sin_t); 26981 sin = (sin_t *)&addr; 26982 *sin = sin_null; 26983 sin->sin_family = AF_INET; 26984 tcp->tcp_ipversion = IPV4_VERSION; 26985 } else { 26986 ASSERT(tcp->tcp_family == AF_INET6); 26987 len = sizeof (sin6_t); 26988 sin6 = (sin6_t *)&addr; 26989 *sin6 = sin6_null; 26990 sin6->sin6_family = AF_INET6; 26991 tcp->tcp_ipversion = IPV6_VERSION; 26992 } 26993 sa = (struct sockaddr *)&addr; 26994 } 26995 26996 error = tcp_bind_check(connp, sa, len, cr, 26997 bind_to_req_port_only); 26998 if (error) 26999 return (error); 27000 /* Fall through and do the fanout insertion */ 27001 } 27002 27003 do_listen: 27004 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 27005 tcp->tcp_conn_req_max = backlog; 27006 if (tcp->tcp_conn_req_max) { 27007 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 27008 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 27009 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 27010 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 27011 /* 27012 * If this is a listener, do not reset the eager list 27013 * and other stuffs. Note that we don't check if the 27014 * existing eager list meets the new tcp_conn_req_max 27015 * requirement. 27016 */ 27017 if (tcp->tcp_state != TCPS_LISTEN) { 27018 tcp->tcp_state = TCPS_LISTEN; 27019 /* Initialize the chain. Don't need the eager_lock */ 27020 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 27021 tcp->tcp_eager_next_drop_q0 = tcp; 27022 tcp->tcp_eager_prev_drop_q0 = tcp; 27023 tcp->tcp_second_ctimer_threshold = 27024 tcps->tcps_ip_abort_linterval; 27025 } 27026 } 27027 27028 /* 27029 * We can call ip_bind directly, the processing continues 27030 * in tcp_post_ip_bind(). 27031 * 27032 * We need to make sure that the conn_recv is set to a non-null 27033 * value before we insert the conn into the classifier table. 27034 * This is to avoid a race with an incoming packet which does an 27035 * ipcl_classify(). 27036 */ 27037 connp->conn_recv = tcp_conn_request; 27038 if (tcp->tcp_family == AF_INET) { 27039 error = ip_proto_bind_laddr_v4(connp, NULL, 27040 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 27041 } else { 27042 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 27043 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 27044 } 27045 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 27046 } 27047 27048 void 27049 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 27050 { 27051 conn_t *connp = (conn_t *)proto_handle; 27052 tcp_t *tcp = connp->conn_tcp; 27053 tcp_stack_t *tcps = tcp->tcp_tcps; 27054 uint_t thwin; 27055 27056 ASSERT(connp->conn_upper_handle != NULL); 27057 27058 (void) squeue_synch_enter(connp->conn_sqp, connp, 0); 27059 27060 /* Flow control condition has been removed. */ 27061 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 27062 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 27063 << tcp->tcp_rcv_ws; 27064 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 27065 /* 27066 * Send back a window update immediately if TCP is above 27067 * ESTABLISHED state and the increase of the rcv window 27068 * that the other side knows is at least 1 MSS after flow 27069 * control is lifted. 27070 */ 27071 if (tcp->tcp_state >= TCPS_ESTABLISHED && 27072 (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss)) { 27073 tcp_xmit_ctl(NULL, tcp, 27074 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 27075 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 27076 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 27077 } 27078 27079 squeue_synch_exit(connp->conn_sqp, connp); 27080 } 27081 27082 /* ARGSUSED */ 27083 int 27084 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 27085 int mode, int32_t *rvalp, cred_t *cr) 27086 { 27087 conn_t *connp = (conn_t *)proto_handle; 27088 int error; 27089 27090 ASSERT(connp->conn_upper_handle != NULL); 27091 27092 /* All Solaris components should pass a cred for this operation. */ 27093 ASSERT(cr != NULL); 27094 27095 switch (cmd) { 27096 case ND_SET: 27097 case ND_GET: 27098 case TCP_IOC_DEFAULT_Q: 27099 case _SIOCSOCKFALLBACK: 27100 case TCP_IOC_ABORT_CONN: 27101 case TI_GETPEERNAME: 27102 case TI_GETMYNAME: 27103 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 27104 cmd)); 27105 error = EINVAL; 27106 break; 27107 default: 27108 /* 27109 * Pass on to IP using helper stream 27110 */ 27111 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 27112 cmd, arg, mode, cr, rvalp); 27113 break; 27114 } 27115 return (error); 27116 } 27117 27118 sock_downcalls_t sock_tcp_downcalls = { 27119 tcp_activate, 27120 tcp_accept, 27121 tcp_bind, 27122 tcp_listen, 27123 tcp_connect, 27124 tcp_getpeername, 27125 tcp_getsockname, 27126 tcp_getsockopt, 27127 tcp_setsockopt, 27128 tcp_sendmsg, 27129 NULL, 27130 NULL, 27131 NULL, 27132 tcp_shutdown, 27133 tcp_clr_flowctrl, 27134 tcp_ioctl, 27135 tcp_close, 27136 }; 27137