1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #include <sys/types.h> 29 #include <sys/stream.h> 30 #include <sys/strsun.h> 31 #include <sys/strsubr.h> 32 #include <sys/stropts.h> 33 #include <sys/strlog.h> 34 #define _SUN_TPI_VERSION 2 35 #include <sys/tihdr.h> 36 #include <sys/timod.h> 37 #include <sys/ddi.h> 38 #include <sys/sunddi.h> 39 #include <sys/suntpi.h> 40 #include <sys/xti_inet.h> 41 #include <sys/cmn_err.h> 42 #include <sys/debug.h> 43 #include <sys/sdt.h> 44 #include <sys/vtrace.h> 45 #include <sys/kmem.h> 46 #include <sys/ethernet.h> 47 #include <sys/cpuvar.h> 48 #include <sys/dlpi.h> 49 #include <sys/multidata.h> 50 #include <sys/multidata_impl.h> 51 #include <sys/pattr.h> 52 #include <sys/policy.h> 53 #include <sys/priv.h> 54 #include <sys/zone.h> 55 #include <sys/sunldi.h> 56 57 #include <sys/errno.h> 58 #include <sys/signal.h> 59 #include <sys/socket.h> 60 #include <sys/socketvar.h> 61 #include <sys/sockio.h> 62 #include <sys/isa_defs.h> 63 #include <sys/md5.h> 64 #include <sys/random.h> 65 #include <sys/uio.h> 66 #include <sys/systm.h> 67 #include <netinet/in.h> 68 #include <netinet/tcp.h> 69 #include <netinet/ip6.h> 70 #include <netinet/icmp6.h> 71 #include <net/if.h> 72 #include <net/route.h> 73 #include <inet/ipsec_impl.h> 74 75 #include <inet/common.h> 76 #include <inet/ip.h> 77 #include <inet/ip_impl.h> 78 #include <inet/ip6.h> 79 #include <inet/ip_ndp.h> 80 #include <inet/proto_set.h> 81 #include <inet/mib2.h> 82 #include <inet/nd.h> 83 #include <inet/optcom.h> 84 #include <inet/snmpcom.h> 85 #include <inet/kstatcom.h> 86 #include <inet/tcp.h> 87 #include <inet/tcp_impl.h> 88 #include <inet/udp_impl.h> 89 #include <net/pfkeyv2.h> 90 #include <inet/ipsec_info.h> 91 #include <inet/ipdrop.h> 92 93 #include <inet/ipclassifier.h> 94 #include <inet/ip_ire.h> 95 #include <inet/ip_ftable.h> 96 #include <inet/ip_if.h> 97 #include <inet/ipp_common.h> 98 #include <inet/ip_netinfo.h> 99 #include <sys/squeue_impl.h> 100 #include <sys/squeue.h> 101 #include <inet/kssl/ksslapi.h> 102 #include <sys/tsol/label.h> 103 #include <sys/tsol/tnet.h> 104 #include <rpc/pmap_prot.h> 105 #include <sys/callo.h> 106 107 /* 108 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 109 * 110 * (Read the detailed design doc in PSARC case directory) 111 * 112 * The entire tcp state is contained in tcp_t and conn_t structure 113 * which are allocated in tandem using ipcl_conn_create() and passing 114 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 115 * the references on the tcp_t. The tcp_t structure is never compressed 116 * and packets always land on the correct TCP perimeter from the time 117 * eager is created till the time tcp_t dies (as such the old mentat 118 * TCP global queue is not used for detached state and no IPSEC checking 119 * is required). The global queue is still allocated to send out resets 120 * for connection which have no listeners and IP directly calls 121 * tcp_xmit_listeners_reset() which does any policy check. 122 * 123 * Protection and Synchronisation mechanism: 124 * 125 * The tcp data structure does not use any kind of lock for protecting 126 * its state but instead uses 'squeues' for mutual exclusion from various 127 * read and write side threads. To access a tcp member, the thread should 128 * always be behind squeue (via squeue_enter with flags as SQ_FILL, SQ_PROCESS, 129 * or SQ_NODRAIN). Since the squeues allow a direct function call, caller 130 * can pass any tcp function having prototype of edesc_t as argument 131 * (different from traditional STREAMs model where packets come in only 132 * designated entry points). The list of functions that can be directly 133 * called via squeue are listed before the usual function prototype. 134 * 135 * Referencing: 136 * 137 * TCP is MT-Hot and we use a reference based scheme to make sure that the 138 * tcp structure doesn't disappear when its needed. When the application 139 * creates an outgoing connection or accepts an incoming connection, we 140 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 141 * The IP reference is just a symbolic reference since ip_tcpclose() 142 * looks at tcp structure after tcp_close_output() returns which could 143 * have dropped the last TCP reference. So as long as the connection is 144 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 145 * conn_t. The classifier puts its own reference when the connection is 146 * inserted in listen or connected hash. Anytime a thread needs to enter 147 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 148 * on write side or by doing a classify on read side and then puts a 149 * reference on the conn before doing squeue_enter/tryenter/fill. For 150 * read side, the classifier itself puts the reference under fanout lock 151 * to make sure that tcp can't disappear before it gets processed. The 152 * squeue will drop this reference automatically so the called function 153 * doesn't have to do a DEC_REF. 154 * 155 * Opening a new connection: 156 * 157 * The outgoing connection open is pretty simple. tcp_open() does the 158 * work in creating the conn/tcp structure and initializing it. The 159 * squeue assignment is done based on the CPU the application 160 * is running on. So for outbound connections, processing is always done 161 * on application CPU which might be different from the incoming CPU 162 * being interrupted by the NIC. An optimal way would be to figure out 163 * the NIC <-> CPU binding at listen time, and assign the outgoing 164 * connection to the squeue attached to the CPU that will be interrupted 165 * for incoming packets (we know the NIC based on the bind IP address). 166 * This might seem like a problem if more data is going out but the 167 * fact is that in most cases the transmit is ACK driven transmit where 168 * the outgoing data normally sits on TCP's xmit queue waiting to be 169 * transmitted. 170 * 171 * Accepting a connection: 172 * 173 * This is a more interesting case because of various races involved in 174 * establishing a eager in its own perimeter. Read the meta comment on 175 * top of tcp_conn_request(). But briefly, the squeue is picked by 176 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 177 * 178 * Closing a connection: 179 * 180 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 181 * via squeue to do the close and mark the tcp as detached if the connection 182 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 183 * reference but tcp_close() drop IP's reference always. So if tcp was 184 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 185 * and 1 because it is in classifier's connected hash. This is the condition 186 * we use to determine that its OK to clean up the tcp outside of squeue 187 * when time wait expires (check the ref under fanout and conn_lock and 188 * if it is 2, remove it from fanout hash and kill it). 189 * 190 * Although close just drops the necessary references and marks the 191 * tcp_detached state, tcp_close needs to know the tcp_detached has been 192 * set (under squeue) before letting the STREAM go away (because a 193 * inbound packet might attempt to go up the STREAM while the close 194 * has happened and tcp_detached is not set). So a special lock and 195 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 196 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 197 * tcp_detached. 198 * 199 * Special provisions and fast paths: 200 * 201 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 202 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 203 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 204 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 205 * check to send packets directly to tcp_rput_data via squeue. Everyone 206 * else comes through tcp_input() on the read side. 207 * 208 * We also make special provisions for sockfs by marking tcp_issocket 209 * whenever we have only sockfs on top of TCP. This allows us to skip 210 * putting the tcp in acceptor hash since a sockfs listener can never 211 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 212 * since eager has already been allocated and the accept now happens 213 * on acceptor STREAM. There is a big blob of comment on top of 214 * tcp_conn_request explaining the new accept. When socket is POP'd, 215 * sockfs sends us an ioctl to mark the fact and we go back to old 216 * behaviour. Once tcp_issocket is unset, its never set for the 217 * life of that connection. 218 * 219 * IPsec notes : 220 * 221 * Since a packet is always executed on the correct TCP perimeter 222 * all IPsec processing is defered to IP including checking new 223 * connections and setting IPSEC policies for new connection. The 224 * only exception is tcp_xmit_listeners_reset() which is called 225 * directly from IP and needs to policy check to see if TH_RST 226 * can be sent out. 227 * 228 * PFHooks notes : 229 * 230 * For mdt case, one meta buffer contains multiple packets. Mblks for every 231 * packet are assembled and passed to the hooks. When packets are blocked, 232 * or boundary of any packet is changed, the mdt processing is stopped, and 233 * packets of the meta buffer are send to the IP path one by one. 234 */ 235 236 /* 237 * Values for squeue switch: 238 * 1: SQ_NODRAIN 239 * 2: SQ_PROCESS 240 * 3: SQ_FILL 241 */ 242 int tcp_squeue_wput = 2; /* /etc/systems */ 243 int tcp_squeue_flag; 244 245 /* 246 * This controls how tiny a write must be before we try to copy it 247 * into the the mblk on the tail of the transmit queue. Not much 248 * speedup is observed for values larger than sixteen. Zero will 249 * disable the optimisation. 250 */ 251 int tcp_tx_pull_len = 16; 252 253 /* 254 * TCP Statistics. 255 * 256 * How TCP statistics work. 257 * 258 * There are two types of statistics invoked by two macros. 259 * 260 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 261 * supposed to be used in non MT-hot paths of the code. 262 * 263 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 264 * supposed to be used for DEBUG purposes and may be used on a hot path. 265 * 266 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 267 * (use "kstat tcp" to get them). 268 * 269 * There is also additional debugging facility that marks tcp_clean_death() 270 * instances and saves them in tcp_t structure. It is triggered by 271 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 272 * tcp_clean_death() calls that counts the number of times each tag was hit. It 273 * is triggered by TCP_CLD_COUNTERS define. 274 * 275 * How to add new counters. 276 * 277 * 1) Add a field in the tcp_stat structure describing your counter. 278 * 2) Add a line in the template in tcp_kstat2_init() with the name 279 * of the counter. 280 * 281 * IMPORTANT!! - make sure that both are in sync !! 282 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 283 * 284 * Please avoid using private counters which are not kstat-exported. 285 * 286 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 287 * in tcp_t structure. 288 * 289 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 290 */ 291 292 #ifndef TCP_DEBUG_COUNTER 293 #ifdef DEBUG 294 #define TCP_DEBUG_COUNTER 1 295 #else 296 #define TCP_DEBUG_COUNTER 0 297 #endif 298 #endif 299 300 #define TCP_CLD_COUNTERS 0 301 302 #define TCP_TAG_CLEAN_DEATH 1 303 #define TCP_MAX_CLEAN_DEATH_TAG 32 304 305 #ifdef lint 306 static int _lint_dummy_; 307 #endif 308 309 #if TCP_CLD_COUNTERS 310 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 311 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 312 #elif defined(lint) 313 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 314 #else 315 #define TCP_CLD_STAT(x) 316 #endif 317 318 #if TCP_DEBUG_COUNTER 319 #define TCP_DBGSTAT(tcps, x) \ 320 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 321 #define TCP_G_DBGSTAT(x) \ 322 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 323 #elif defined(lint) 324 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 325 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 326 #else 327 #define TCP_DBGSTAT(tcps, x) 328 #define TCP_G_DBGSTAT(x) 329 #endif 330 331 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 332 333 tcp_g_stat_t tcp_g_statistics; 334 kstat_t *tcp_g_kstat; 335 336 /* 337 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 338 * tcp write side. 339 */ 340 #define CALL_IP_WPUT(connp, q, mp) { \ 341 ASSERT(((q)->q_flag & QREADR) == 0); \ 342 TCP_DBGSTAT(connp->conn_netstack->netstack_tcp, tcp_ip_output); \ 343 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 344 } 345 346 /* Macros for timestamp comparisons */ 347 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 348 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 349 350 /* 351 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 352 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 353 * by adding three components: a time component which grows by 1 every 4096 354 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 355 * a per-connection component which grows by 125000 for every new connection; 356 * and an "extra" component that grows by a random amount centered 357 * approximately on 64000. This causes the the ISS generator to cycle every 358 * 4.89 hours if no TCP connections are made, and faster if connections are 359 * made. 360 * 361 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 362 * components: a time component which grows by 250000 every second; and 363 * a per-connection component which grows by 125000 for every new connections. 364 * 365 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 366 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 367 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 368 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 369 * password. 370 */ 371 #define ISS_INCR 250000 372 #define ISS_NSEC_SHT 12 373 374 static sin_t sin_null; /* Zero address for quick clears */ 375 static sin6_t sin6_null; /* Zero address for quick clears */ 376 377 /* 378 * This implementation follows the 4.3BSD interpretation of the urgent 379 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 380 * incompatible changes in protocols like telnet and rlogin. 381 */ 382 #define TCP_OLD_URP_INTERPRETATION 1 383 384 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 385 (TCP_IS_DETACHED(tcp) && \ 386 (!(tcp)->tcp_hard_binding)) 387 388 /* 389 * TCP reassembly macros. We hide starting and ending sequence numbers in 390 * b_next and b_prev of messages on the reassembly queue. The messages are 391 * chained using b_cont. These macros are used in tcp_reass() so we don't 392 * have to see the ugly casts and assignments. 393 */ 394 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 395 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 396 (mblk_t *)(uintptr_t)(u)) 397 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 398 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 399 (mblk_t *)(uintptr_t)(u)) 400 401 /* 402 * Implementation of TCP Timers. 403 * ============================= 404 * 405 * INTERFACE: 406 * 407 * There are two basic functions dealing with tcp timers: 408 * 409 * timeout_id_t tcp_timeout(connp, func, time) 410 * clock_t tcp_timeout_cancel(connp, timeout_id) 411 * TCP_TIMER_RESTART(tcp, intvl) 412 * 413 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 414 * after 'time' ticks passed. The function called by timeout() must adhere to 415 * the same restrictions as a driver soft interrupt handler - it must not sleep 416 * or call other functions that might sleep. The value returned is the opaque 417 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 418 * cancel the request. The call to tcp_timeout() may fail in which case it 419 * returns zero. This is different from the timeout(9F) function which never 420 * fails. 421 * 422 * The call-back function 'func' always receives 'connp' as its single 423 * argument. It is always executed in the squeue corresponding to the tcp 424 * structure. The tcp structure is guaranteed to be present at the time the 425 * call-back is called. 426 * 427 * NOTE: The call-back function 'func' is never called if tcp is in 428 * the TCPS_CLOSED state. 429 * 430 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 431 * request. locks acquired by the call-back routine should not be held across 432 * the call to tcp_timeout_cancel() or a deadlock may result. 433 * 434 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 435 * Otherwise, it returns an integer value greater than or equal to 0. In 436 * particular, if the call-back function is already placed on the squeue, it can 437 * not be canceled. 438 * 439 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 440 * within squeue context corresponding to the tcp instance. Since the 441 * call-back is also called via the same squeue, there are no race 442 * conditions described in untimeout(9F) manual page since all calls are 443 * strictly serialized. 444 * 445 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 446 * stored in tcp_timer_tid and starts a new one using 447 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 448 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 449 * field. 450 * 451 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 452 * call-back may still be called, so it is possible tcp_timer() will be 453 * called several times. This should not be a problem since tcp_timer() 454 * should always check the tcp instance state. 455 * 456 * 457 * IMPLEMENTATION: 458 * 459 * TCP timers are implemented using three-stage process. The call to 460 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 461 * when the timer expires. The tcp_timer_callback() arranges the call of the 462 * tcp_timer_handler() function via squeue corresponding to the tcp 463 * instance. The tcp_timer_handler() calls actual requested timeout call-back 464 * and passes tcp instance as an argument to it. Information is passed between 465 * stages using the tcp_timer_t structure which contains the connp pointer, the 466 * tcp call-back to call and the timeout id returned by the timeout(9F). 467 * 468 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 469 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 470 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 471 * returns the pointer to this mblk. 472 * 473 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 474 * looks like a normal mblk without actual dblk attached to it. 475 * 476 * To optimize performance each tcp instance holds a small cache of timer 477 * mblocks. In the current implementation it caches up to two timer mblocks per 478 * tcp instance. The cache is preserved over tcp frees and is only freed when 479 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 480 * timer processing happens on a corresponding squeue, the cache manipulation 481 * does not require any locks. Experiments show that majority of timer mblocks 482 * allocations are satisfied from the tcp cache and do not involve kmem calls. 483 * 484 * The tcp_timeout() places a refhold on the connp instance which guarantees 485 * that it will be present at the time the call-back function fires. The 486 * tcp_timer_handler() drops the reference after calling the call-back, so the 487 * call-back function does not need to manipulate the references explicitly. 488 */ 489 490 typedef struct tcp_timer_s { 491 conn_t *connp; 492 void (*tcpt_proc)(void *); 493 callout_id_t tcpt_tid; 494 } tcp_timer_t; 495 496 static kmem_cache_t *tcp_timercache; 497 kmem_cache_t *tcp_sack_info_cache; 498 kmem_cache_t *tcp_iphc_cache; 499 500 /* 501 * For scalability, we must not run a timer for every TCP connection 502 * in TIME_WAIT state. To see why, consider (for time wait interval of 503 * 4 minutes): 504 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 505 * 506 * This list is ordered by time, so you need only delete from the head 507 * until you get to entries which aren't old enough to delete yet. 508 * The list consists of only the detached TIME_WAIT connections. 509 * 510 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 511 * becomes detached TIME_WAIT (either by changing the state and already 512 * being detached or the other way around). This means that the TIME_WAIT 513 * state can be extended (up to doubled) if the connection doesn't become 514 * detached for a long time. 515 * 516 * The list manipulations (including tcp_time_wait_next/prev) 517 * are protected by the tcp_time_wait_lock. The content of the 518 * detached TIME_WAIT connections is protected by the normal perimeters. 519 * 520 * This list is per squeue and squeues are shared across the tcp_stack_t's. 521 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 522 * and conn_netstack. 523 * The tcp_t's that are added to tcp_free_list are disassociated and 524 * have NULL tcp_tcps and conn_netstack pointers. 525 */ 526 typedef struct tcp_squeue_priv_s { 527 kmutex_t tcp_time_wait_lock; 528 callout_id_t tcp_time_wait_tid; 529 tcp_t *tcp_time_wait_head; 530 tcp_t *tcp_time_wait_tail; 531 tcp_t *tcp_free_list; 532 uint_t tcp_free_list_cnt; 533 } tcp_squeue_priv_t; 534 535 /* 536 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 537 * Running it every 5 seconds seems to give the best results. 538 */ 539 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 540 541 /* 542 * To prevent memory hog, limit the number of entries in tcp_free_list 543 * to 1% of available memory / number of cpus 544 */ 545 uint_t tcp_free_list_max_cnt = 0; 546 547 #define TCP_XMIT_LOWATER 4096 548 #define TCP_XMIT_HIWATER 49152 549 #define TCP_RECV_LOWATER 2048 550 #define TCP_RECV_HIWATER 49152 551 552 /* 553 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 554 */ 555 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 556 557 #define TIDUSZ 4096 /* transport interface data unit size */ 558 559 /* 560 * Bind hash list size and has function. It has to be a power of 2 for 561 * hashing. 562 */ 563 #define TCP_BIND_FANOUT_SIZE 512 564 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 565 /* 566 * Size of listen and acceptor hash list. It has to be a power of 2 for 567 * hashing. 568 */ 569 #define TCP_FANOUT_SIZE 256 570 571 #ifdef _ILP32 572 #define TCP_ACCEPTOR_HASH(accid) \ 573 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 574 #else 575 #define TCP_ACCEPTOR_HASH(accid) \ 576 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 577 #endif /* _ILP32 */ 578 579 #define IP_ADDR_CACHE_SIZE 2048 580 #define IP_ADDR_CACHE_HASH(faddr) \ 581 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 582 583 /* 584 * TCP options struct returned from tcp_parse_options. 585 */ 586 typedef struct tcp_opt_s { 587 uint32_t tcp_opt_mss; 588 uint32_t tcp_opt_wscale; 589 uint32_t tcp_opt_ts_val; 590 uint32_t tcp_opt_ts_ecr; 591 tcp_t *tcp; 592 } tcp_opt_t; 593 594 /* 595 * TCP option struct passing information b/w lisenter and eager. 596 */ 597 struct tcp_options { 598 uint_t to_flags; 599 ssize_t to_boundif; /* IPV6_BOUND_IF */ 600 }; 601 602 #define TCPOPT_BOUNDIF 0x00000001 /* set IPV6_BOUND_IF */ 603 #define TCPOPT_RECVPKTINFO 0x00000002 /* set IPV6_RECVPKTINFO */ 604 605 /* 606 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 607 */ 608 609 #ifdef _BIG_ENDIAN 610 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 611 (TCPOPT_TSTAMP << 8) | 10) 612 #else 613 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 614 (TCPOPT_NOP << 8) | TCPOPT_NOP) 615 #endif 616 617 /* 618 * Flags returned from tcp_parse_options. 619 */ 620 #define TCP_OPT_MSS_PRESENT 1 621 #define TCP_OPT_WSCALE_PRESENT 2 622 #define TCP_OPT_TSTAMP_PRESENT 4 623 #define TCP_OPT_SACK_OK_PRESENT 8 624 #define TCP_OPT_SACK_PRESENT 16 625 626 /* TCP option length */ 627 #define TCPOPT_NOP_LEN 1 628 #define TCPOPT_MAXSEG_LEN 4 629 #define TCPOPT_WS_LEN 3 630 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 631 #define TCPOPT_TSTAMP_LEN 10 632 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 633 #define TCPOPT_SACK_OK_LEN 2 634 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 635 #define TCPOPT_REAL_SACK_LEN 4 636 #define TCPOPT_MAX_SACK_LEN 36 637 #define TCPOPT_HEADER_LEN 2 638 639 /* TCP cwnd burst factor. */ 640 #define TCP_CWND_INFINITE 65535 641 #define TCP_CWND_SS 3 642 #define TCP_CWND_NORMAL 5 643 644 /* Maximum TCP initial cwin (start/restart). */ 645 #define TCP_MAX_INIT_CWND 8 646 647 /* 648 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 649 * either tcp_slow_start_initial or tcp_slow_start_after idle 650 * depending on the caller. If the upper layer has not used the 651 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 652 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 653 * If the upper layer has changed set the tcp_init_cwnd, just use 654 * it to calculate the tcp_cwnd. 655 */ 656 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 657 { \ 658 if ((tcp)->tcp_init_cwnd == 0) { \ 659 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 660 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 661 } else { \ 662 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 663 } \ 664 tcp->tcp_cwnd_cnt = 0; \ 665 } 666 667 /* TCP Timer control structure */ 668 typedef struct tcpt_s { 669 pfv_t tcpt_pfv; /* The routine we are to call */ 670 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 671 } tcpt_t; 672 673 /* 674 * Functions called directly via squeue having a prototype of edesc_t. 675 */ 676 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 677 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 678 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 679 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 680 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 681 void tcp_input(void *arg, mblk_t *mp, void *arg2); 682 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 683 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 684 void tcp_output(void *arg, mblk_t *mp, void *arg2); 685 void tcp_output_urgent(void *arg, mblk_t *mp, void *arg2); 686 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 687 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 688 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 689 690 691 /* Prototype for TCP functions */ 692 static void tcp_random_init(void); 693 int tcp_random(void); 694 static void tcp_tli_accept(tcp_t *tcp, mblk_t *mp); 695 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 696 tcp_t *eager); 697 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 698 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 699 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 700 boolean_t user_specified); 701 static void tcp_closei_local(tcp_t *tcp); 702 static void tcp_close_detached(tcp_t *tcp); 703 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 704 mblk_t *idmp, mblk_t **defermp); 705 static void tcp_tpi_connect(tcp_t *tcp, mblk_t *mp); 706 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, 707 in_port_t dstport, uint_t srcid, cred_t *cr, pid_t pid); 708 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, 709 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 710 uint32_t scope_id, cred_t *cr, pid_t pid); 711 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 712 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 713 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 714 static char *tcp_display(tcp_t *tcp, char *, char); 715 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 716 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 717 static void tcp_eager_unlink(tcp_t *tcp); 718 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 719 int unixerr); 720 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 721 int tlierr, int unixerr); 722 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 723 cred_t *cr); 724 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 725 char *value, caddr_t cp, cred_t *cr); 726 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 727 char *value, caddr_t cp, cred_t *cr); 728 static int tcp_tpistate(tcp_t *tcp); 729 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 730 int caller_holds_lock); 731 static void tcp_bind_hash_remove(tcp_t *tcp); 732 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 733 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 734 static void tcp_acceptor_hash_remove(tcp_t *tcp); 735 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 736 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 737 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 738 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 739 void tcp_g_q_setup(tcp_stack_t *); 740 void tcp_g_q_create(tcp_stack_t *); 741 void tcp_g_q_destroy(tcp_stack_t *); 742 static int tcp_header_init_ipv4(tcp_t *tcp); 743 static int tcp_header_init_ipv6(tcp_t *tcp); 744 int tcp_init(tcp_t *tcp, queue_t *q); 745 static int tcp_init_values(tcp_t *tcp); 746 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 747 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 748 static void tcp_ip_notify(tcp_t *tcp); 749 static mblk_t *tcp_ire_mp(mblk_t **mpp); 750 static void tcp_iss_init(tcp_t *tcp); 751 static void tcp_keepalive_killer(void *arg); 752 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 753 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 754 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 755 int *do_disconnectp, int *t_errorp, int *sys_errorp); 756 static boolean_t tcp_allow_connopt_set(int level, int name); 757 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 758 int tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 759 int tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, 760 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 761 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 762 mblk_t *mblk); 763 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 764 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 765 uchar_t *ptr, uint_t len); 766 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 767 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 768 tcp_stack_t *); 769 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 770 caddr_t cp, cred_t *cr); 771 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 772 caddr_t cp, cred_t *cr); 773 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 774 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 775 caddr_t cp, cred_t *cr); 776 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 777 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 778 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 779 static void tcp_reinit(tcp_t *tcp); 780 static void tcp_reinit_values(tcp_t *tcp); 781 782 static uint_t tcp_rwnd_reopen(tcp_t *tcp); 783 static uint_t tcp_rcv_drain(tcp_t *tcp); 784 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 785 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 786 static void tcp_ss_rexmit(tcp_t *tcp); 787 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 788 static void tcp_process_options(tcp_t *, tcph_t *); 789 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 790 static void tcp_rsrv(queue_t *q); 791 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 792 static int tcp_snmp_state(tcp_t *tcp); 793 static void tcp_timer(void *arg); 794 static void tcp_timer_callback(void *); 795 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 796 boolean_t random); 797 static in_port_t tcp_get_next_priv_port(const tcp_t *); 798 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 799 static void tcp_wput_fallback(queue_t *q, mblk_t *mp); 800 void tcp_tpi_accept(queue_t *q, mblk_t *mp); 801 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 802 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 803 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 804 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 805 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 806 const int num_sack_blk, int *usable, uint_t *snxt, 807 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 808 const int mdt_thres); 809 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 810 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 811 const int num_sack_blk, int *usable, uint_t *snxt, 812 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 813 const int mdt_thres); 814 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 815 int num_sack_blk); 816 static void tcp_wsrv(queue_t *q); 817 static int tcp_xmit_end(tcp_t *tcp); 818 static void tcp_ack_timer(void *arg); 819 static mblk_t *tcp_ack_mp(tcp_t *tcp); 820 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 821 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 822 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 823 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 824 uint32_t ack, int ctl); 825 static int setmaxps(queue_t *q, int maxpsz); 826 static void tcp_set_rto(tcp_t *, time_t); 827 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 828 boolean_t, boolean_t); 829 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 830 boolean_t ipsec_mctl); 831 static int tcp_build_hdrs(tcp_t *); 832 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 833 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 834 tcph_t *tcph); 835 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 836 static mblk_t *tcp_mdt_info_mp(mblk_t *); 837 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 838 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 839 const boolean_t, const uint32_t, const uint32_t, 840 const uint32_t, const uint32_t, tcp_stack_t *); 841 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 842 const uint_t, const uint_t, boolean_t *); 843 static mblk_t *tcp_lso_info_mp(mblk_t *); 844 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 845 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 846 extern mblk_t *tcp_timermp_alloc(int); 847 extern void tcp_timermp_free(tcp_t *); 848 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 849 static void tcp_stop_lingering(tcp_t *tcp); 850 static void tcp_close_linger_timeout(void *arg); 851 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 852 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 853 static void tcp_stack_fini(netstackid_t stackid, void *arg); 854 static void *tcp_g_kstat_init(tcp_g_stat_t *); 855 static void tcp_g_kstat_fini(kstat_t *); 856 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 857 static void tcp_kstat_fini(netstackid_t, kstat_t *); 858 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 859 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 860 static int tcp_kstat_update(kstat_t *kp, int rw); 861 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 862 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 863 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 864 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 865 tcph_t *tcph, mblk_t *idmp); 866 static int tcp_squeue_switch(int); 867 868 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 869 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 870 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 871 static int tcp_tpi_close(queue_t *, int); 872 static int tcp_tpi_close_accept(queue_t *); 873 874 static void tcp_squeue_add(squeue_t *); 875 static boolean_t tcp_zcopy_check(tcp_t *); 876 static void tcp_zcopy_notify(tcp_t *); 877 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 878 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 879 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 880 881 extern void tcp_kssl_input(tcp_t *, mblk_t *); 882 883 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 884 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 885 886 static int tcp_accept(sock_lower_handle_t, sock_lower_handle_t, 887 sock_upper_handle_t, cred_t *); 888 static int tcp_listen(sock_lower_handle_t, int, cred_t *); 889 static int tcp_post_ip_bind(tcp_t *, mblk_t *, int, cred_t *, pid_t); 890 static int tcp_do_listen(conn_t *, struct sockaddr *, socklen_t, int, cred_t *, 891 boolean_t); 892 static int tcp_do_connect(conn_t *, const struct sockaddr *, socklen_t, 893 cred_t *, pid_t); 894 static int tcp_do_bind(conn_t *, struct sockaddr *, socklen_t, cred_t *, 895 boolean_t); 896 static int tcp_do_unbind(conn_t *); 897 static int tcp_bind_check(conn_t *, struct sockaddr *, socklen_t, cred_t *, 898 boolean_t); 899 900 static void tcp_ulp_newconn(conn_t *, conn_t *, mblk_t *); 901 902 /* 903 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 904 * 905 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 906 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 907 * (defined in tcp.h) needs to be filled in and passed into the kernel 908 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 909 * structure contains the four-tuple of a TCP connection and a range of TCP 910 * states (specified by ac_start and ac_end). The use of wildcard addresses 911 * and ports is allowed. Connections with a matching four tuple and a state 912 * within the specified range will be aborted. The valid states for the 913 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 914 * inclusive. 915 * 916 * An application which has its connection aborted by this ioctl will receive 917 * an error that is dependent on the connection state at the time of the abort. 918 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 919 * though a RST packet has been received. If the connection state is equal to 920 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 921 * and all resources associated with the connection will be freed. 922 */ 923 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 924 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 925 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 926 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 927 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 928 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 929 boolean_t, tcp_stack_t *); 930 931 static struct module_info tcp_rinfo = { 932 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 933 }; 934 935 static struct module_info tcp_winfo = { 936 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 937 }; 938 939 /* 940 * Entry points for TCP as a device. The normal case which supports 941 * the TCP functionality. 942 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 943 */ 944 struct qinit tcp_rinitv4 = { 945 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo 946 }; 947 948 struct qinit tcp_rinitv6 = { 949 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo 950 }; 951 952 struct qinit tcp_winit = { 953 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 954 }; 955 956 /* Initial entry point for TCP in socket mode. */ 957 struct qinit tcp_sock_winit = { 958 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 959 }; 960 961 /* TCP entry point during fallback */ 962 struct qinit tcp_fallback_sock_winit = { 963 (pfi_t)tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo 964 }; 965 966 /* 967 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 968 * an accept. Avoid allocating data structures since eager has already 969 * been created. 970 */ 971 struct qinit tcp_acceptor_rinit = { 972 NULL, (pfi_t)tcp_rsrv, NULL, tcp_tpi_close_accept, NULL, &tcp_winfo 973 }; 974 975 struct qinit tcp_acceptor_winit = { 976 (pfi_t)tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo 977 }; 978 979 /* 980 * Entry points for TCP loopback (read side only) 981 * The open routine is only used for reopens, thus no need to 982 * have a separate one for tcp_openv6. 983 */ 984 struct qinit tcp_loopback_rinit = { 985 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, (pfi_t)0, 986 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 987 }; 988 989 /* For AF_INET aka /dev/tcp */ 990 struct streamtab tcpinfov4 = { 991 &tcp_rinitv4, &tcp_winit 992 }; 993 994 /* For AF_INET6 aka /dev/tcp6 */ 995 struct streamtab tcpinfov6 = { 996 &tcp_rinitv6, &tcp_winit 997 }; 998 999 sock_downcalls_t sock_tcp_downcalls; 1000 1001 /* 1002 * Have to ensure that tcp_g_q_close is not done by an 1003 * interrupt thread. 1004 */ 1005 static taskq_t *tcp_taskq; 1006 1007 /* Setable only in /etc/system. Move to ndd? */ 1008 boolean_t tcp_icmp_source_quench = B_FALSE; 1009 1010 /* 1011 * Following assumes TPI alignment requirements stay along 32 bit 1012 * boundaries 1013 */ 1014 #define ROUNDUP32(x) \ 1015 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1016 1017 /* Template for response to info request. */ 1018 static struct T_info_ack tcp_g_t_info_ack = { 1019 T_INFO_ACK, /* PRIM_type */ 1020 0, /* TSDU_size */ 1021 T_INFINITE, /* ETSDU_size */ 1022 T_INVALID, /* CDATA_size */ 1023 T_INVALID, /* DDATA_size */ 1024 sizeof (sin_t), /* ADDR_size */ 1025 0, /* OPT_size - not initialized here */ 1026 TIDUSZ, /* TIDU_size */ 1027 T_COTS_ORD, /* SERV_type */ 1028 TCPS_IDLE, /* CURRENT_state */ 1029 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1030 }; 1031 1032 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1033 T_INFO_ACK, /* PRIM_type */ 1034 0, /* TSDU_size */ 1035 T_INFINITE, /* ETSDU_size */ 1036 T_INVALID, /* CDATA_size */ 1037 T_INVALID, /* DDATA_size */ 1038 sizeof (sin6_t), /* ADDR_size */ 1039 0, /* OPT_size - not initialized here */ 1040 TIDUSZ, /* TIDU_size */ 1041 T_COTS_ORD, /* SERV_type */ 1042 TCPS_IDLE, /* CURRENT_state */ 1043 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1044 }; 1045 1046 #define MS 1L 1047 #define SECONDS (1000 * MS) 1048 #define MINUTES (60 * SECONDS) 1049 #define HOURS (60 * MINUTES) 1050 #define DAYS (24 * HOURS) 1051 1052 #define PARAM_MAX (~(uint32_t)0) 1053 1054 /* Max size IP datagram is 64k - 1 */ 1055 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1056 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1057 /* Max of the above */ 1058 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1059 1060 /* Largest TCP port number */ 1061 #define TCP_MAX_PORT (64 * 1024 - 1) 1062 1063 /* 1064 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1065 * layer header. It has to be a multiple of 4. 1066 */ 1067 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1068 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1069 1070 /* 1071 * All of these are alterable, within the min/max values given, at run time. 1072 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1073 * per the TCP spec. 1074 */ 1075 /* BEGIN CSTYLED */ 1076 static tcpparam_t lcl_tcp_param_arr[] = { 1077 /*min max value name */ 1078 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1079 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1080 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1081 { 1, 1024, 1, "tcp_conn_req_min" }, 1082 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1083 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1084 { 0, 10, 0, "tcp_debug" }, 1085 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1086 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1087 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1088 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1089 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1090 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1091 { 1, 255, 64, "tcp_ipv4_ttl"}, 1092 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1093 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1094 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1095 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1096 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1097 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1098 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1099 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1100 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1101 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1102 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1103 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1104 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1105 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1106 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1107 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1108 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1109 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1110 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1111 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1112 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1113 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1114 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1115 /* 1116 * Question: What default value should I set for tcp_strong_iss? 1117 */ 1118 { 0, 2, 1, "tcp_strong_iss"}, 1119 { 0, 65536, 20, "tcp_rtt_updates"}, 1120 { 0, 1, 1, "tcp_wscale_always"}, 1121 { 0, 1, 0, "tcp_tstamp_always"}, 1122 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1123 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1124 { 0, 16, 2, "tcp_deferred_acks_max"}, 1125 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1126 { 1, 4, 4, "tcp_slow_start_initial"}, 1127 { 0, 2, 2, "tcp_sack_permitted"}, 1128 { 0, 1, 1, "tcp_compression_enabled"}, 1129 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1130 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1131 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1132 { 0, 1, 0, "tcp_rev_src_routes"}, 1133 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1134 { 0, 16, 8, "tcp_local_dacks_max"}, 1135 { 0, 2, 1, "tcp_ecn_permitted"}, 1136 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1137 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1138 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1139 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1140 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1141 }; 1142 /* END CSTYLED */ 1143 1144 /* 1145 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1146 * each header fragment in the header buffer. Each parameter value has 1147 * to be a multiple of 4 (32-bit aligned). 1148 */ 1149 static tcpparam_t lcl_tcp_mdt_head_param = 1150 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1151 static tcpparam_t lcl_tcp_mdt_tail_param = 1152 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1153 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1154 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1155 1156 /* 1157 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1158 * the maximum number of payload buffers associated per Multidata. 1159 */ 1160 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1161 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1162 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1163 1164 /* Round up the value to the nearest mss. */ 1165 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1166 1167 /* 1168 * Set ECN capable transport (ECT) code point in IP header. 1169 * 1170 * Note that there are 2 ECT code points '01' and '10', which are called 1171 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1172 * point ECT(0) for TCP as described in RFC 2481. 1173 */ 1174 #define SET_ECT(tcp, iph) \ 1175 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1176 /* We need to clear the code point first. */ \ 1177 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1178 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1179 } else { \ 1180 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1181 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1182 } 1183 1184 /* 1185 * The format argument to pass to tcp_display(). 1186 * DISP_PORT_ONLY means that the returned string has only port info. 1187 * DISP_ADDR_AND_PORT means that the returned string also contains the 1188 * remote and local IP address. 1189 */ 1190 #define DISP_PORT_ONLY 1 1191 #define DISP_ADDR_AND_PORT 2 1192 1193 #define IS_VMLOANED_MBLK(mp) \ 1194 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1195 1196 1197 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1198 boolean_t tcp_mdt_chain = B_TRUE; 1199 1200 /* 1201 * MDT threshold in the form of effective send MSS multiplier; we take 1202 * the MDT path if the amount of unsent data exceeds the threshold value 1203 * (default threshold is 1*SMSS). 1204 */ 1205 uint_t tcp_mdt_smss_threshold = 1; 1206 1207 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1208 1209 /* 1210 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1211 * tunable settable via NDD. Otherwise, the per-connection behavior is 1212 * determined dynamically during tcp_adapt_ire(), which is the default. 1213 */ 1214 boolean_t tcp_static_maxpsz = B_FALSE; 1215 1216 /* Setable in /etc/system */ 1217 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1218 uint32_t tcp_random_anon_port = 1; 1219 1220 /* 1221 * To reach to an eager in Q0 which can be dropped due to an incoming 1222 * new SYN request when Q0 is full, a new doubly linked list is 1223 * introduced. This list allows to select an eager from Q0 in O(1) time. 1224 * This is needed to avoid spending too much time walking through the 1225 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1226 * this new list has to be a member of Q0. 1227 * This list is headed by listener's tcp_t. When the list is empty, 1228 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1229 * of listener's tcp_t point to listener's tcp_t itself. 1230 * 1231 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1232 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1233 * These macros do not affect the eager's membership to Q0. 1234 */ 1235 1236 1237 #define MAKE_DROPPABLE(listener, eager) \ 1238 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1239 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1240 = (eager); \ 1241 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1242 (eager)->tcp_eager_next_drop_q0 = \ 1243 (listener)->tcp_eager_next_drop_q0; \ 1244 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1245 } 1246 1247 #define MAKE_UNDROPPABLE(eager) \ 1248 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1249 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1250 = (eager)->tcp_eager_prev_drop_q0; \ 1251 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1252 = (eager)->tcp_eager_next_drop_q0; \ 1253 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1254 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1255 } 1256 1257 /* 1258 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1259 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1260 * data, TCP will not respond with an ACK. RFC 793 requires that 1261 * TCP responds with an ACK for such a bogus ACK. By not following 1262 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1263 * an attacker successfully spoofs an acceptable segment to our 1264 * peer; or when our peer is "confused." 1265 */ 1266 uint32_t tcp_drop_ack_unsent_cnt = 10; 1267 1268 /* 1269 * Hook functions to enable cluster networking 1270 * On non-clustered systems these vectors must always be NULL. 1271 */ 1272 1273 void (*cl_inet_listen)(netstackid_t stack_id, uint8_t protocol, 1274 sa_family_t addr_family, uint8_t *laddrp, 1275 in_port_t lport, void *args) = NULL; 1276 void (*cl_inet_unlisten)(netstackid_t stack_id, uint8_t protocol, 1277 sa_family_t addr_family, uint8_t *laddrp, 1278 in_port_t lport, void *args) = NULL; 1279 1280 int (*cl_inet_connect2)(netstackid_t stack_id, uint8_t protocol, 1281 boolean_t is_outgoing, 1282 sa_family_t addr_family, 1283 uint8_t *laddrp, in_port_t lport, 1284 uint8_t *faddrp, in_port_t fport, 1285 void *args) = NULL; 1286 1287 void (*cl_inet_disconnect)(netstackid_t stack_id, uint8_t protocol, 1288 sa_family_t addr_family, uint8_t *laddrp, 1289 in_port_t lport, uint8_t *faddrp, 1290 in_port_t fport, void *args) = NULL; 1291 1292 /* 1293 * The following are defined in ip.c 1294 */ 1295 extern int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol, 1296 sa_family_t addr_family, uint8_t *laddrp, 1297 void *args); 1298 extern uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol, 1299 sa_family_t addr_family, uint8_t *laddrp, 1300 uint8_t *faddrp, void *args); 1301 1302 1303 /* 1304 * int CL_INET_CONNECT(conn_t *cp, tcp_t *tcp, boolean_t is_outgoing, int err) 1305 */ 1306 #define CL_INET_CONNECT(connp, tcp, is_outgoing, err) { \ 1307 (err) = 0; \ 1308 if (cl_inet_connect2 != NULL) { \ 1309 /* \ 1310 * Running in cluster mode - register active connection \ 1311 * information \ 1312 */ \ 1313 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1314 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1315 (err) = (*cl_inet_connect2)( \ 1316 (connp)->conn_netstack->netstack_stackid,\ 1317 IPPROTO_TCP, is_outgoing, AF_INET, \ 1318 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1319 (in_port_t)(tcp)->tcp_lport, \ 1320 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1321 (in_port_t)(tcp)->tcp_fport, NULL); \ 1322 } \ 1323 } else { \ 1324 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1325 &(tcp)->tcp_ip6h->ip6_src)) { \ 1326 (err) = (*cl_inet_connect2)( \ 1327 (connp)->conn_netstack->netstack_stackid,\ 1328 IPPROTO_TCP, is_outgoing, AF_INET6, \ 1329 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1330 (in_port_t)(tcp)->tcp_lport, \ 1331 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1332 (in_port_t)(tcp)->tcp_fport, NULL); \ 1333 } \ 1334 } \ 1335 } \ 1336 } 1337 1338 #define CL_INET_DISCONNECT(connp, tcp) { \ 1339 if (cl_inet_disconnect != NULL) { \ 1340 /* \ 1341 * Running in cluster mode - deregister active \ 1342 * connection information \ 1343 */ \ 1344 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1345 if ((tcp)->tcp_ip_src != 0) { \ 1346 (*cl_inet_disconnect)( \ 1347 (connp)->conn_netstack->netstack_stackid,\ 1348 IPPROTO_TCP, AF_INET, \ 1349 (uint8_t *)(&((tcp)->tcp_ip_src)), \ 1350 (in_port_t)(tcp)->tcp_lport, \ 1351 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1352 (in_port_t)(tcp)->tcp_fport, NULL); \ 1353 } \ 1354 } else { \ 1355 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1356 &(tcp)->tcp_ip_src_v6)) { \ 1357 (*cl_inet_disconnect)( \ 1358 (connp)->conn_netstack->netstack_stackid,\ 1359 IPPROTO_TCP, AF_INET6, \ 1360 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1361 (in_port_t)(tcp)->tcp_lport, \ 1362 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1363 (in_port_t)(tcp)->tcp_fport, NULL); \ 1364 } \ 1365 } \ 1366 } \ 1367 } 1368 1369 /* 1370 * Cluster networking hook for traversing current connection list. 1371 * This routine is used to extract the current list of live connections 1372 * which must continue to to be dispatched to this node. 1373 */ 1374 int cl_tcp_walk_list(netstackid_t stack_id, 1375 int (*callback)(cl_tcp_info_t *, void *), void *arg); 1376 1377 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1378 void *arg, tcp_stack_t *tcps); 1379 1380 #define DTRACE_IP_FASTPATH(mp, iph, ill, ipha, ip6h) \ 1381 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, \ 1382 iph, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, \ 1383 ip6_t *, ip6h, int, 0); 1384 1385 /* 1386 * Figure out the value of window scale opton. Note that the rwnd is 1387 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1388 * We cannot find the scale value and then do a round up of tcp_rwnd 1389 * because the scale value may not be correct after that. 1390 * 1391 * Set the compiler flag to make this function inline. 1392 */ 1393 static void 1394 tcp_set_ws_value(tcp_t *tcp) 1395 { 1396 int i; 1397 uint32_t rwnd = tcp->tcp_rwnd; 1398 1399 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1400 i++, rwnd >>= 1) 1401 ; 1402 tcp->tcp_rcv_ws = i; 1403 } 1404 1405 /* 1406 * Remove a connection from the list of detached TIME_WAIT connections. 1407 * It returns B_FALSE if it can't remove the connection from the list 1408 * as the connection has already been removed from the list due to an 1409 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1410 */ 1411 static boolean_t 1412 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1413 { 1414 boolean_t locked = B_FALSE; 1415 1416 if (tcp_time_wait == NULL) { 1417 tcp_time_wait = *((tcp_squeue_priv_t **) 1418 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1419 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1420 locked = B_TRUE; 1421 } else { 1422 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1423 } 1424 1425 if (tcp->tcp_time_wait_expire == 0) { 1426 ASSERT(tcp->tcp_time_wait_next == NULL); 1427 ASSERT(tcp->tcp_time_wait_prev == NULL); 1428 if (locked) 1429 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1430 return (B_FALSE); 1431 } 1432 ASSERT(TCP_IS_DETACHED(tcp)); 1433 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1434 1435 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1436 ASSERT(tcp->tcp_time_wait_prev == NULL); 1437 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1438 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1439 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1440 NULL; 1441 } else { 1442 tcp_time_wait->tcp_time_wait_tail = NULL; 1443 } 1444 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1445 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1446 ASSERT(tcp->tcp_time_wait_next == NULL); 1447 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1448 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1449 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1450 } else { 1451 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1452 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1453 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1454 tcp->tcp_time_wait_next; 1455 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1456 tcp->tcp_time_wait_prev; 1457 } 1458 tcp->tcp_time_wait_next = NULL; 1459 tcp->tcp_time_wait_prev = NULL; 1460 tcp->tcp_time_wait_expire = 0; 1461 1462 if (locked) 1463 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1464 return (B_TRUE); 1465 } 1466 1467 /* 1468 * Add a connection to the list of detached TIME_WAIT connections 1469 * and set its time to expire. 1470 */ 1471 static void 1472 tcp_time_wait_append(tcp_t *tcp) 1473 { 1474 tcp_stack_t *tcps = tcp->tcp_tcps; 1475 tcp_squeue_priv_t *tcp_time_wait = 1476 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1477 SQPRIVATE_TCP)); 1478 1479 tcp_timers_stop(tcp); 1480 1481 /* Freed above */ 1482 ASSERT(tcp->tcp_timer_tid == 0); 1483 ASSERT(tcp->tcp_ack_tid == 0); 1484 1485 /* must have happened at the time of detaching the tcp */ 1486 ASSERT(tcp->tcp_ptpahn == NULL); 1487 ASSERT(tcp->tcp_flow_stopped == 0); 1488 ASSERT(tcp->tcp_time_wait_next == NULL); 1489 ASSERT(tcp->tcp_time_wait_prev == NULL); 1490 ASSERT(tcp->tcp_time_wait_expire == NULL); 1491 ASSERT(tcp->tcp_listener == NULL); 1492 1493 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1494 /* 1495 * The value computed below in tcp->tcp_time_wait_expire may 1496 * appear negative or wrap around. That is ok since our 1497 * interest is only in the difference between the current lbolt 1498 * value and tcp->tcp_time_wait_expire. But the value should not 1499 * be zero, since it means the tcp is not in the TIME_WAIT list. 1500 * The corresponding comparison in tcp_time_wait_collector() uses 1501 * modular arithmetic. 1502 */ 1503 tcp->tcp_time_wait_expire += 1504 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1505 if (tcp->tcp_time_wait_expire == 0) 1506 tcp->tcp_time_wait_expire = 1; 1507 1508 ASSERT(TCP_IS_DETACHED(tcp)); 1509 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1510 ASSERT(tcp->tcp_time_wait_next == NULL); 1511 ASSERT(tcp->tcp_time_wait_prev == NULL); 1512 TCP_DBGSTAT(tcps, tcp_time_wait); 1513 1514 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1515 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1516 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1517 tcp_time_wait->tcp_time_wait_head = tcp; 1518 } else { 1519 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1520 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1521 TCPS_TIME_WAIT); 1522 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1523 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1524 } 1525 tcp_time_wait->tcp_time_wait_tail = tcp; 1526 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1527 } 1528 1529 /* ARGSUSED */ 1530 void 1531 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1532 { 1533 conn_t *connp = (conn_t *)arg; 1534 tcp_t *tcp = connp->conn_tcp; 1535 tcp_stack_t *tcps = tcp->tcp_tcps; 1536 1537 ASSERT(tcp != NULL); 1538 if (tcp->tcp_state == TCPS_CLOSED) { 1539 return; 1540 } 1541 1542 ASSERT((tcp->tcp_family == AF_INET && 1543 tcp->tcp_ipversion == IPV4_VERSION) || 1544 (tcp->tcp_family == AF_INET6 && 1545 (tcp->tcp_ipversion == IPV4_VERSION || 1546 tcp->tcp_ipversion == IPV6_VERSION))); 1547 ASSERT(!tcp->tcp_listener); 1548 1549 TCP_STAT(tcps, tcp_time_wait_reap); 1550 ASSERT(TCP_IS_DETACHED(tcp)); 1551 1552 /* 1553 * Because they have no upstream client to rebind or tcp_close() 1554 * them later, we axe the connection here and now. 1555 */ 1556 tcp_close_detached(tcp); 1557 } 1558 1559 /* 1560 * Remove cached/latched IPsec references. 1561 */ 1562 void 1563 tcp_ipsec_cleanup(tcp_t *tcp) 1564 { 1565 conn_t *connp = tcp->tcp_connp; 1566 1567 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1568 1569 if (connp->conn_latch != NULL) { 1570 IPLATCH_REFRELE(connp->conn_latch, 1571 connp->conn_netstack); 1572 connp->conn_latch = NULL; 1573 } 1574 if (connp->conn_policy != NULL) { 1575 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1576 connp->conn_policy = NULL; 1577 } 1578 } 1579 1580 /* 1581 * Cleaup before placing on free list. 1582 * Disassociate from the netstack/tcp_stack_t since the freelist 1583 * is per squeue and not per netstack. 1584 */ 1585 void 1586 tcp_cleanup(tcp_t *tcp) 1587 { 1588 mblk_t *mp; 1589 char *tcp_iphc; 1590 int tcp_iphc_len; 1591 int tcp_hdr_grown; 1592 tcp_sack_info_t *tcp_sack_info; 1593 conn_t *connp = tcp->tcp_connp; 1594 tcp_stack_t *tcps = tcp->tcp_tcps; 1595 netstack_t *ns = tcps->tcps_netstack; 1596 mblk_t *tcp_rsrv_mp; 1597 1598 tcp_bind_hash_remove(tcp); 1599 1600 /* Cleanup that which needs the netstack first */ 1601 tcp_ipsec_cleanup(tcp); 1602 1603 tcp_free(tcp); 1604 1605 /* Release any SSL context */ 1606 if (tcp->tcp_kssl_ent != NULL) { 1607 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1608 tcp->tcp_kssl_ent = NULL; 1609 } 1610 1611 if (tcp->tcp_kssl_ctx != NULL) { 1612 kssl_release_ctx(tcp->tcp_kssl_ctx); 1613 tcp->tcp_kssl_ctx = NULL; 1614 } 1615 tcp->tcp_kssl_pending = B_FALSE; 1616 1617 conn_delete_ire(connp, NULL); 1618 1619 /* 1620 * Since we will bzero the entire structure, we need to 1621 * remove it and reinsert it in global hash list. We 1622 * know the walkers can't get to this conn because we 1623 * had set CONDEMNED flag earlier and checked reference 1624 * under conn_lock so walker won't pick it and when we 1625 * go the ipcl_globalhash_remove() below, no walker 1626 * can get to it. 1627 */ 1628 ipcl_globalhash_remove(connp); 1629 1630 /* 1631 * Now it is safe to decrement the reference counts. 1632 * This might be the last reference on the netstack and TCPS 1633 * in which case it will cause the tcp_g_q_close and 1634 * the freeing of the IP Instance. 1635 */ 1636 connp->conn_netstack = NULL; 1637 netstack_rele(ns); 1638 ASSERT(tcps != NULL); 1639 tcp->tcp_tcps = NULL; 1640 TCPS_REFRELE(tcps); 1641 1642 /* Save some state */ 1643 mp = tcp->tcp_timercache; 1644 1645 tcp_sack_info = tcp->tcp_sack_info; 1646 tcp_iphc = tcp->tcp_iphc; 1647 tcp_iphc_len = tcp->tcp_iphc_len; 1648 tcp_hdr_grown = tcp->tcp_hdr_grown; 1649 tcp_rsrv_mp = tcp->tcp_rsrv_mp; 1650 1651 if (connp->conn_cred != NULL) { 1652 crfree(connp->conn_cred); 1653 connp->conn_cred = NULL; 1654 } 1655 if (connp->conn_peercred != NULL) { 1656 crfree(connp->conn_peercred); 1657 connp->conn_peercred = NULL; 1658 } 1659 ipcl_conn_cleanup(connp); 1660 connp->conn_flags = IPCL_TCPCONN; 1661 bzero(tcp, sizeof (tcp_t)); 1662 1663 /* restore the state */ 1664 tcp->tcp_timercache = mp; 1665 1666 tcp->tcp_sack_info = tcp_sack_info; 1667 tcp->tcp_iphc = tcp_iphc; 1668 tcp->tcp_iphc_len = tcp_iphc_len; 1669 tcp->tcp_hdr_grown = tcp_hdr_grown; 1670 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 1671 1672 tcp->tcp_connp = connp; 1673 1674 ASSERT(connp->conn_tcp == tcp); 1675 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1676 connp->conn_state_flags = CONN_INCIPIENT; 1677 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1678 ASSERT(connp->conn_ref == 1); 1679 } 1680 1681 /* 1682 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1683 * is done forwards from the head. 1684 * This walks all stack instances since 1685 * tcp_time_wait remains global across all stacks. 1686 */ 1687 /* ARGSUSED */ 1688 void 1689 tcp_time_wait_collector(void *arg) 1690 { 1691 tcp_t *tcp; 1692 clock_t now; 1693 mblk_t *mp; 1694 conn_t *connp; 1695 kmutex_t *lock; 1696 boolean_t removed; 1697 1698 squeue_t *sqp = (squeue_t *)arg; 1699 tcp_squeue_priv_t *tcp_time_wait = 1700 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1701 1702 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1703 tcp_time_wait->tcp_time_wait_tid = 0; 1704 1705 if (tcp_time_wait->tcp_free_list != NULL && 1706 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1707 TCP_G_STAT(tcp_freelist_cleanup); 1708 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1709 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1710 tcp->tcp_time_wait_next = NULL; 1711 tcp_time_wait->tcp_free_list_cnt--; 1712 ASSERT(tcp->tcp_tcps == NULL); 1713 CONN_DEC_REF(tcp->tcp_connp); 1714 } 1715 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1716 } 1717 1718 /* 1719 * In order to reap time waits reliably, we should use a 1720 * source of time that is not adjustable by the user -- hence 1721 * the call to ddi_get_lbolt(). 1722 */ 1723 now = ddi_get_lbolt(); 1724 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1725 /* 1726 * Compare times using modular arithmetic, since 1727 * lbolt can wrapover. 1728 */ 1729 if ((now - tcp->tcp_time_wait_expire) < 0) { 1730 break; 1731 } 1732 1733 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1734 ASSERT(removed); 1735 1736 connp = tcp->tcp_connp; 1737 ASSERT(connp->conn_fanout != NULL); 1738 lock = &connp->conn_fanout->connf_lock; 1739 /* 1740 * This is essentially a TW reclaim fast path optimization for 1741 * performance where the timewait collector checks under the 1742 * fanout lock (so that no one else can get access to the 1743 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1744 * the classifier hash list. If ref count is indeed 2, we can 1745 * just remove the conn under the fanout lock and avoid 1746 * cleaning up the conn under the squeue, provided that 1747 * clustering callbacks are not enabled. If clustering is 1748 * enabled, we need to make the clustering callback before 1749 * setting the CONDEMNED flag and after dropping all locks and 1750 * so we forego this optimization and fall back to the slow 1751 * path. Also please see the comments in tcp_closei_local 1752 * regarding the refcnt logic. 1753 * 1754 * Since we are holding the tcp_time_wait_lock, its better 1755 * not to block on the fanout_lock because other connections 1756 * can't add themselves to time_wait list. So we do a 1757 * tryenter instead of mutex_enter. 1758 */ 1759 if (mutex_tryenter(lock)) { 1760 mutex_enter(&connp->conn_lock); 1761 if ((connp->conn_ref == 2) && 1762 (cl_inet_disconnect == NULL)) { 1763 ipcl_hash_remove_locked(connp, 1764 connp->conn_fanout); 1765 /* 1766 * Set the CONDEMNED flag now itself so that 1767 * the refcnt cannot increase due to any 1768 * walker. But we have still not cleaned up 1769 * conn_ire_cache. This is still ok since 1770 * we are going to clean it up in tcp_cleanup 1771 * immediately and any interface unplumb 1772 * thread will wait till the ire is blown away 1773 */ 1774 connp->conn_state_flags |= CONN_CONDEMNED; 1775 mutex_exit(lock); 1776 mutex_exit(&connp->conn_lock); 1777 if (tcp_time_wait->tcp_free_list_cnt < 1778 tcp_free_list_max_cnt) { 1779 /* Add to head of tcp_free_list */ 1780 mutex_exit( 1781 &tcp_time_wait->tcp_time_wait_lock); 1782 tcp_cleanup(tcp); 1783 ASSERT(connp->conn_latch == NULL); 1784 ASSERT(connp->conn_policy == NULL); 1785 ASSERT(tcp->tcp_tcps == NULL); 1786 ASSERT(connp->conn_netstack == NULL); 1787 1788 mutex_enter( 1789 &tcp_time_wait->tcp_time_wait_lock); 1790 tcp->tcp_time_wait_next = 1791 tcp_time_wait->tcp_free_list; 1792 tcp_time_wait->tcp_free_list = tcp; 1793 tcp_time_wait->tcp_free_list_cnt++; 1794 continue; 1795 } else { 1796 /* Do not add to tcp_free_list */ 1797 mutex_exit( 1798 &tcp_time_wait->tcp_time_wait_lock); 1799 tcp_bind_hash_remove(tcp); 1800 conn_delete_ire(tcp->tcp_connp, NULL); 1801 tcp_ipsec_cleanup(tcp); 1802 CONN_DEC_REF(tcp->tcp_connp); 1803 } 1804 } else { 1805 CONN_INC_REF_LOCKED(connp); 1806 mutex_exit(lock); 1807 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1808 mutex_exit(&connp->conn_lock); 1809 /* 1810 * We can reuse the closemp here since conn has 1811 * detached (otherwise we wouldn't even be in 1812 * time_wait list). tcp_closemp_used can safely 1813 * be changed without taking a lock as no other 1814 * thread can concurrently access it at this 1815 * point in the connection lifecycle. 1816 */ 1817 1818 if (tcp->tcp_closemp.b_prev == NULL) 1819 tcp->tcp_closemp_used = B_TRUE; 1820 else 1821 cmn_err(CE_PANIC, 1822 "tcp_timewait_collector: " 1823 "concurrent use of tcp_closemp: " 1824 "connp %p tcp %p\n", (void *)connp, 1825 (void *)tcp); 1826 1827 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1828 mp = &tcp->tcp_closemp; 1829 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1830 tcp_timewait_output, connp, 1831 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1832 } 1833 } else { 1834 mutex_enter(&connp->conn_lock); 1835 CONN_INC_REF_LOCKED(connp); 1836 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1837 mutex_exit(&connp->conn_lock); 1838 /* 1839 * We can reuse the closemp here since conn has 1840 * detached (otherwise we wouldn't even be in 1841 * time_wait list). tcp_closemp_used can safely 1842 * be changed without taking a lock as no other 1843 * thread can concurrently access it at this 1844 * point in the connection lifecycle. 1845 */ 1846 1847 if (tcp->tcp_closemp.b_prev == NULL) 1848 tcp->tcp_closemp_used = B_TRUE; 1849 else 1850 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1851 "concurrent use of tcp_closemp: " 1852 "connp %p tcp %p\n", (void *)connp, 1853 (void *)tcp); 1854 1855 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1856 mp = &tcp->tcp_closemp; 1857 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1858 tcp_timewait_output, connp, 1859 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1860 } 1861 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1862 } 1863 1864 if (tcp_time_wait->tcp_free_list != NULL) 1865 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1866 1867 tcp_time_wait->tcp_time_wait_tid = 1868 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 1869 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 1870 CALLOUT_FLAG_ROUNDUP); 1871 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1872 } 1873 1874 /* 1875 * Reply to a clients T_CONN_RES TPI message. This function 1876 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1877 * on the acceptor STREAM and processed in tcp_wput_accept(). 1878 * Read the block comment on top of tcp_conn_request(). 1879 */ 1880 static void 1881 tcp_tli_accept(tcp_t *listener, mblk_t *mp) 1882 { 1883 tcp_t *acceptor; 1884 tcp_t *eager; 1885 tcp_t *tcp; 1886 struct T_conn_res *tcr; 1887 t_uscalar_t acceptor_id; 1888 t_scalar_t seqnum; 1889 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1890 struct tcp_options *tcpopt; 1891 mblk_t *ok_mp; 1892 mblk_t *mp1; 1893 tcp_stack_t *tcps = listener->tcp_tcps; 1894 1895 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1896 tcp_err_ack(listener, mp, TPROTO, 0); 1897 return; 1898 } 1899 tcr = (struct T_conn_res *)mp->b_rptr; 1900 1901 /* 1902 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1903 * read side queue of the streams device underneath us i.e. the 1904 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1905 * look it up in the queue_hash. Under LP64 it sends down the 1906 * minor_t of the accepting endpoint. 1907 * 1908 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1909 * fanout hash lock is held. 1910 * This prevents any thread from entering the acceptor queue from 1911 * below (since it has not been hard bound yet i.e. any inbound 1912 * packets will arrive on the listener or default tcp queue and 1913 * go through tcp_lookup). 1914 * The CONN_INC_REF will prevent the acceptor from closing. 1915 * 1916 * XXX It is still possible for a tli application to send down data 1917 * on the accepting stream while another thread calls t_accept. 1918 * This should not be a problem for well-behaved applications since 1919 * the T_OK_ACK is sent after the queue swapping is completed. 1920 * 1921 * If the accepting fd is the same as the listening fd, avoid 1922 * queue hash lookup since that will return an eager listener in a 1923 * already established state. 1924 */ 1925 acceptor_id = tcr->ACCEPTOR_id; 1926 mutex_enter(&listener->tcp_eager_lock); 1927 if (listener->tcp_acceptor_id == acceptor_id) { 1928 eager = listener->tcp_eager_next_q; 1929 /* only count how many T_CONN_INDs so don't count q0 */ 1930 if ((listener->tcp_conn_req_cnt_q != 1) || 1931 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1932 mutex_exit(&listener->tcp_eager_lock); 1933 tcp_err_ack(listener, mp, TBADF, 0); 1934 return; 1935 } 1936 if (listener->tcp_conn_req_cnt_q0 != 0) { 1937 /* Throw away all the eagers on q0. */ 1938 tcp_eager_cleanup(listener, 1); 1939 } 1940 if (listener->tcp_syn_defense) { 1941 listener->tcp_syn_defense = B_FALSE; 1942 if (listener->tcp_ip_addr_cache != NULL) { 1943 kmem_free(listener->tcp_ip_addr_cache, 1944 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1945 listener->tcp_ip_addr_cache = NULL; 1946 } 1947 } 1948 /* 1949 * Transfer tcp_conn_req_max to the eager so that when 1950 * a disconnect occurs we can revert the endpoint to the 1951 * listen state. 1952 */ 1953 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1954 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1955 /* 1956 * Get a reference on the acceptor just like the 1957 * tcp_acceptor_hash_lookup below. 1958 */ 1959 acceptor = listener; 1960 CONN_INC_REF(acceptor->tcp_connp); 1961 } else { 1962 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1963 if (acceptor == NULL) { 1964 if (listener->tcp_debug) { 1965 (void) strlog(TCP_MOD_ID, 0, 1, 1966 SL_ERROR|SL_TRACE, 1967 "tcp_accept: did not find acceptor 0x%x\n", 1968 acceptor_id); 1969 } 1970 mutex_exit(&listener->tcp_eager_lock); 1971 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 1972 return; 1973 } 1974 /* 1975 * Verify acceptor state. The acceptable states for an acceptor 1976 * include TCPS_IDLE and TCPS_BOUND. 1977 */ 1978 switch (acceptor->tcp_state) { 1979 case TCPS_IDLE: 1980 /* FALLTHRU */ 1981 case TCPS_BOUND: 1982 break; 1983 default: 1984 CONN_DEC_REF(acceptor->tcp_connp); 1985 mutex_exit(&listener->tcp_eager_lock); 1986 tcp_err_ack(listener, mp, TOUTSTATE, 0); 1987 return; 1988 } 1989 } 1990 1991 /* The listener must be in TCPS_LISTEN */ 1992 if (listener->tcp_state != TCPS_LISTEN) { 1993 CONN_DEC_REF(acceptor->tcp_connp); 1994 mutex_exit(&listener->tcp_eager_lock); 1995 tcp_err_ack(listener, mp, TOUTSTATE, 0); 1996 return; 1997 } 1998 1999 /* 2000 * Rendezvous with an eager connection request packet hanging off 2001 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2002 * tcp structure when the connection packet arrived in 2003 * tcp_conn_request(). 2004 */ 2005 seqnum = tcr->SEQ_number; 2006 eager = listener; 2007 do { 2008 eager = eager->tcp_eager_next_q; 2009 if (eager == NULL) { 2010 CONN_DEC_REF(acceptor->tcp_connp); 2011 mutex_exit(&listener->tcp_eager_lock); 2012 tcp_err_ack(listener, mp, TBADSEQ, 0); 2013 return; 2014 } 2015 } while (eager->tcp_conn_req_seqnum != seqnum); 2016 mutex_exit(&listener->tcp_eager_lock); 2017 2018 /* 2019 * At this point, both acceptor and listener have 2 ref 2020 * that they begin with. Acceptor has one additional ref 2021 * we placed in lookup while listener has 3 additional 2022 * ref for being behind the squeue (tcp_accept() is 2023 * done on listener's squeue); being in classifier hash; 2024 * and eager's ref on listener. 2025 */ 2026 ASSERT(listener->tcp_connp->conn_ref >= 5); 2027 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2028 2029 /* 2030 * The eager at this point is set in its own squeue and 2031 * could easily have been killed (tcp_accept_finish will 2032 * deal with that) because of a TH_RST so we can only 2033 * ASSERT for a single ref. 2034 */ 2035 ASSERT(eager->tcp_connp->conn_ref >= 1); 2036 2037 /* Pre allocate the stroptions mblk also */ 2038 opt_mp = allocb(MAX(sizeof (struct tcp_options), 2039 sizeof (struct T_conn_res)), BPRI_HI); 2040 if (opt_mp == NULL) { 2041 CONN_DEC_REF(acceptor->tcp_connp); 2042 CONN_DEC_REF(eager->tcp_connp); 2043 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2044 return; 2045 } 2046 DB_TYPE(opt_mp) = M_SETOPTS; 2047 opt_mp->b_wptr += sizeof (struct tcp_options); 2048 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 2049 tcpopt->to_flags = 0; 2050 2051 /* 2052 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2053 * from listener to acceptor. 2054 */ 2055 if (listener->tcp_bound_if != 0) { 2056 tcpopt->to_flags |= TCPOPT_BOUNDIF; 2057 tcpopt->to_boundif = listener->tcp_bound_if; 2058 } 2059 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2060 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 2061 } 2062 2063 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2064 if ((mp1 = copymsg(mp)) == NULL) { 2065 CONN_DEC_REF(acceptor->tcp_connp); 2066 CONN_DEC_REF(eager->tcp_connp); 2067 freemsg(opt_mp); 2068 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2069 return; 2070 } 2071 2072 tcr = (struct T_conn_res *)mp1->b_rptr; 2073 2074 /* 2075 * This is an expanded version of mi_tpi_ok_ack_alloc() 2076 * which allocates a larger mblk and appends the new 2077 * local address to the ok_ack. The address is copied by 2078 * soaccept() for getsockname(). 2079 */ 2080 { 2081 int extra; 2082 2083 extra = (eager->tcp_family == AF_INET) ? 2084 sizeof (sin_t) : sizeof (sin6_t); 2085 2086 /* 2087 * Try to re-use mp, if possible. Otherwise, allocate 2088 * an mblk and return it as ok_mp. In any case, mp 2089 * is no longer usable upon return. 2090 */ 2091 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2092 CONN_DEC_REF(acceptor->tcp_connp); 2093 CONN_DEC_REF(eager->tcp_connp); 2094 freemsg(opt_mp); 2095 /* Original mp has been freed by now, so use mp1 */ 2096 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2097 return; 2098 } 2099 2100 mp = NULL; /* We should never use mp after this point */ 2101 2102 switch (extra) { 2103 case sizeof (sin_t): { 2104 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2105 2106 ok_mp->b_wptr += extra; 2107 sin->sin_family = AF_INET; 2108 sin->sin_port = eager->tcp_lport; 2109 sin->sin_addr.s_addr = 2110 eager->tcp_ipha->ipha_src; 2111 break; 2112 } 2113 case sizeof (sin6_t): { 2114 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2115 2116 ok_mp->b_wptr += extra; 2117 sin6->sin6_family = AF_INET6; 2118 sin6->sin6_port = eager->tcp_lport; 2119 if (eager->tcp_ipversion == IPV4_VERSION) { 2120 sin6->sin6_flowinfo = 0; 2121 IN6_IPADDR_TO_V4MAPPED( 2122 eager->tcp_ipha->ipha_src, 2123 &sin6->sin6_addr); 2124 } else { 2125 ASSERT(eager->tcp_ip6h != NULL); 2126 sin6->sin6_flowinfo = 2127 eager->tcp_ip6h->ip6_vcf & 2128 ~IPV6_VERS_AND_FLOW_MASK; 2129 sin6->sin6_addr = 2130 eager->tcp_ip6h->ip6_src; 2131 } 2132 sin6->sin6_scope_id = 0; 2133 sin6->__sin6_src_id = 0; 2134 break; 2135 } 2136 default: 2137 break; 2138 } 2139 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2140 } 2141 2142 /* 2143 * If there are no options we know that the T_CONN_RES will 2144 * succeed. However, we can't send the T_OK_ACK upstream until 2145 * the tcp_accept_swap is done since it would be dangerous to 2146 * let the application start using the new fd prior to the swap. 2147 */ 2148 tcp_accept_swap(listener, acceptor, eager); 2149 2150 /* 2151 * tcp_accept_swap unlinks eager from listener but does not drop 2152 * the eager's reference on the listener. 2153 */ 2154 ASSERT(eager->tcp_listener == NULL); 2155 ASSERT(listener->tcp_connp->conn_ref >= 5); 2156 2157 /* 2158 * The eager is now associated with its own queue. Insert in 2159 * the hash so that the connection can be reused for a future 2160 * T_CONN_RES. 2161 */ 2162 tcp_acceptor_hash_insert(acceptor_id, eager); 2163 2164 /* 2165 * We now do the processing of options with T_CONN_RES. 2166 * We delay till now since we wanted to have queue to pass to 2167 * option processing routines that points back to the right 2168 * instance structure which does not happen until after 2169 * tcp_accept_swap(). 2170 * 2171 * Note: 2172 * The sanity of the logic here assumes that whatever options 2173 * are appropriate to inherit from listner=>eager are done 2174 * before this point, and whatever were to be overridden (or not) 2175 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2176 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2177 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2178 * This may not be true at this point in time but can be fixed 2179 * independently. This option processing code starts with 2180 * the instantiated acceptor instance and the final queue at 2181 * this point. 2182 */ 2183 2184 if (tcr->OPT_length != 0) { 2185 /* Options to process */ 2186 int t_error = 0; 2187 int sys_error = 0; 2188 int do_disconnect = 0; 2189 2190 if (tcp_conprim_opt_process(eager, mp1, 2191 &do_disconnect, &t_error, &sys_error) < 0) { 2192 eager->tcp_accept_error = 1; 2193 if (do_disconnect) { 2194 /* 2195 * An option failed which does not allow 2196 * connection to be accepted. 2197 * 2198 * We allow T_CONN_RES to succeed and 2199 * put a T_DISCON_IND on the eager queue. 2200 */ 2201 ASSERT(t_error == 0 && sys_error == 0); 2202 eager->tcp_send_discon_ind = 1; 2203 } else { 2204 ASSERT(t_error != 0); 2205 freemsg(ok_mp); 2206 /* 2207 * Original mp was either freed or set 2208 * to ok_mp above, so use mp1 instead. 2209 */ 2210 tcp_err_ack(listener, mp1, t_error, sys_error); 2211 goto finish; 2212 } 2213 } 2214 /* 2215 * Most likely success in setting options (except if 2216 * eager->tcp_send_discon_ind set). 2217 * mp1 option buffer represented by OPT_length/offset 2218 * potentially modified and contains results of setting 2219 * options at this point 2220 */ 2221 } 2222 2223 /* We no longer need mp1, since all options processing has passed */ 2224 freemsg(mp1); 2225 2226 putnext(listener->tcp_rq, ok_mp); 2227 2228 mutex_enter(&listener->tcp_eager_lock); 2229 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2230 tcp_t *tail; 2231 mblk_t *conn_ind; 2232 2233 /* 2234 * This path should not be executed if listener and 2235 * acceptor streams are the same. 2236 */ 2237 ASSERT(listener != acceptor); 2238 2239 tcp = listener->tcp_eager_prev_q0; 2240 /* 2241 * listener->tcp_eager_prev_q0 points to the TAIL of the 2242 * deferred T_conn_ind queue. We need to get to the head of 2243 * the queue in order to send up T_conn_ind the same order as 2244 * how the 3WHS is completed. 2245 */ 2246 while (tcp != listener) { 2247 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2248 break; 2249 else 2250 tcp = tcp->tcp_eager_prev_q0; 2251 } 2252 ASSERT(tcp != listener); 2253 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2254 ASSERT(conn_ind != NULL); 2255 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2256 2257 /* Move from q0 to q */ 2258 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2259 listener->tcp_conn_req_cnt_q0--; 2260 listener->tcp_conn_req_cnt_q++; 2261 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2262 tcp->tcp_eager_prev_q0; 2263 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2264 tcp->tcp_eager_next_q0; 2265 tcp->tcp_eager_prev_q0 = NULL; 2266 tcp->tcp_eager_next_q0 = NULL; 2267 tcp->tcp_conn_def_q0 = B_FALSE; 2268 2269 /* Make sure the tcp isn't in the list of droppables */ 2270 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2271 tcp->tcp_eager_prev_drop_q0 == NULL); 2272 2273 /* 2274 * Insert at end of the queue because sockfs sends 2275 * down T_CONN_RES in chronological order. Leaving 2276 * the older conn indications at front of the queue 2277 * helps reducing search time. 2278 */ 2279 tail = listener->tcp_eager_last_q; 2280 if (tail != NULL) 2281 tail->tcp_eager_next_q = tcp; 2282 else 2283 listener->tcp_eager_next_q = tcp; 2284 listener->tcp_eager_last_q = tcp; 2285 tcp->tcp_eager_next_q = NULL; 2286 mutex_exit(&listener->tcp_eager_lock); 2287 putnext(tcp->tcp_rq, conn_ind); 2288 } else { 2289 mutex_exit(&listener->tcp_eager_lock); 2290 } 2291 2292 /* 2293 * Done with the acceptor - free it 2294 * 2295 * Note: from this point on, no access to listener should be made 2296 * as listener can be equal to acceptor. 2297 */ 2298 finish: 2299 ASSERT(acceptor->tcp_detached); 2300 ASSERT(tcps->tcps_g_q != NULL); 2301 ASSERT(!IPCL_IS_NONSTR(acceptor->tcp_connp)); 2302 acceptor->tcp_rq = tcps->tcps_g_q; 2303 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2304 (void) tcp_clean_death(acceptor, 0, 2); 2305 CONN_DEC_REF(acceptor->tcp_connp); 2306 2307 /* 2308 * In case we already received a FIN we have to make tcp_rput send 2309 * the ordrel_ind. This will also send up a window update if the window 2310 * has opened up. 2311 * 2312 * In the normal case of a successful connection acceptance 2313 * we give the O_T_BIND_REQ to the read side put procedure as an 2314 * indication that this was just accepted. This tells tcp_rput to 2315 * pass up any data queued in tcp_rcv_list. 2316 * 2317 * In the fringe case where options sent with T_CONN_RES failed and 2318 * we required, we would be indicating a T_DISCON_IND to blow 2319 * away this connection. 2320 */ 2321 2322 /* 2323 * XXX: we currently have a problem if XTI application closes the 2324 * acceptor stream in between. This problem exists in on10-gate also 2325 * and is well know but nothing can be done short of major rewrite 2326 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2327 * eager same squeue as listener (we can distinguish non socket 2328 * listeners at the time of handling a SYN in tcp_conn_request) 2329 * and do most of the work that tcp_accept_finish does here itself 2330 * and then get behind the acceptor squeue to access the acceptor 2331 * queue. 2332 */ 2333 /* 2334 * We already have a ref on tcp so no need to do one before squeue_enter 2335 */ 2336 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, opt_mp, tcp_accept_finish, 2337 eager->tcp_connp, SQ_FILL, SQTAG_TCP_ACCEPT_FINISH); 2338 } 2339 2340 /* 2341 * Swap information between the eager and acceptor for a TLI/XTI client. 2342 * The sockfs accept is done on the acceptor stream and control goes 2343 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2344 * called. In either case, both the eager and listener are in their own 2345 * perimeter (squeue) and the code has to deal with potential race. 2346 * 2347 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2348 */ 2349 static void 2350 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2351 { 2352 conn_t *econnp, *aconnp; 2353 2354 ASSERT(eager->tcp_rq == listener->tcp_rq); 2355 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2356 ASSERT(!eager->tcp_hard_bound); 2357 ASSERT(!TCP_IS_SOCKET(acceptor)); 2358 ASSERT(!TCP_IS_SOCKET(eager)); 2359 ASSERT(!TCP_IS_SOCKET(listener)); 2360 2361 acceptor->tcp_detached = B_TRUE; 2362 /* 2363 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2364 * the acceptor id. 2365 */ 2366 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2367 2368 /* remove eager from listen list... */ 2369 mutex_enter(&listener->tcp_eager_lock); 2370 tcp_eager_unlink(eager); 2371 ASSERT(eager->tcp_eager_next_q == NULL && 2372 eager->tcp_eager_last_q == NULL); 2373 ASSERT(eager->tcp_eager_next_q0 == NULL && 2374 eager->tcp_eager_prev_q0 == NULL); 2375 mutex_exit(&listener->tcp_eager_lock); 2376 eager->tcp_rq = acceptor->tcp_rq; 2377 eager->tcp_wq = acceptor->tcp_wq; 2378 2379 econnp = eager->tcp_connp; 2380 aconnp = acceptor->tcp_connp; 2381 2382 eager->tcp_rq->q_ptr = econnp; 2383 eager->tcp_wq->q_ptr = econnp; 2384 2385 /* 2386 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2387 * which might be a different squeue from our peer TCP instance. 2388 * For TCP Fusion, the peer expects that whenever tcp_detached is 2389 * clear, our TCP queues point to the acceptor's queues. Thus, use 2390 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2391 * above reach global visibility prior to the clearing of tcp_detached. 2392 */ 2393 membar_producer(); 2394 eager->tcp_detached = B_FALSE; 2395 2396 ASSERT(eager->tcp_ack_tid == 0); 2397 2398 econnp->conn_dev = aconnp->conn_dev; 2399 econnp->conn_minor_arena = aconnp->conn_minor_arena; 2400 ASSERT(econnp->conn_minor_arena != NULL); 2401 if (eager->tcp_cred != NULL) 2402 crfree(eager->tcp_cred); 2403 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2404 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2405 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2406 2407 aconnp->conn_cred = NULL; 2408 2409 econnp->conn_zoneid = aconnp->conn_zoneid; 2410 econnp->conn_allzones = aconnp->conn_allzones; 2411 2412 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2413 aconnp->conn_mac_exempt = B_FALSE; 2414 2415 ASSERT(aconnp->conn_peercred == NULL); 2416 2417 /* Do the IPC initialization */ 2418 CONN_INC_REF(econnp); 2419 2420 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2421 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2422 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2423 2424 /* Done with old IPC. Drop its ref on its connp */ 2425 CONN_DEC_REF(aconnp); 2426 } 2427 2428 2429 /* 2430 * Adapt to the information, such as rtt and rtt_sd, provided from the 2431 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2432 * 2433 * Checks for multicast and broadcast destination address. 2434 * Returns zero on failure; non-zero if ok. 2435 * 2436 * Note that the MSS calculation here is based on the info given in 2437 * the IRE. We do not do any calculation based on TCP options. They 2438 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2439 * knows which options to use. 2440 * 2441 * Note on how TCP gets its parameters for a connection. 2442 * 2443 * When a tcp_t structure is allocated, it gets all the default parameters. 2444 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2445 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2446 * default. 2447 * 2448 * An incoming SYN with a multicast or broadcast destination address, is dropped 2449 * in 1 of 2 places. 2450 * 2451 * 1. If the packet was received over the wire it is dropped in 2452 * ip_rput_process_broadcast() 2453 * 2454 * 2. If the packet was received through internal IP loopback, i.e. the packet 2455 * was generated and received on the same machine, it is dropped in 2456 * ip_wput_local() 2457 * 2458 * An incoming SYN with a multicast or broadcast source address is always 2459 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2460 * reject an attempt to connect to a broadcast or multicast (destination) 2461 * address. 2462 */ 2463 static int 2464 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2465 { 2466 ire_t *ire; 2467 ire_t *sire = NULL; 2468 iulp_t *ire_uinfo = NULL; 2469 uint32_t mss_max; 2470 uint32_t mss; 2471 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2472 conn_t *connp = tcp->tcp_connp; 2473 boolean_t ire_cacheable = B_FALSE; 2474 zoneid_t zoneid = connp->conn_zoneid; 2475 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2476 MATCH_IRE_SECATTR; 2477 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2478 ill_t *ill = NULL; 2479 boolean_t incoming = (ire_mp == NULL); 2480 tcp_stack_t *tcps = tcp->tcp_tcps; 2481 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2482 2483 ASSERT(connp->conn_ire_cache == NULL); 2484 2485 if (tcp->tcp_ipversion == IPV4_VERSION) { 2486 2487 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2488 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2489 return (0); 2490 } 2491 /* 2492 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2493 * for the destination with the nexthop as gateway. 2494 * ire_ctable_lookup() is used because this particular 2495 * ire, if it exists, will be marked private. 2496 * If that is not available, use the interface ire 2497 * for the nexthop. 2498 * 2499 * TSol: tcp_update_label will detect label mismatches based 2500 * only on the destination's label, but that would not 2501 * detect label mismatches based on the security attributes 2502 * of routes or next hop gateway. Hence we need to pass the 2503 * label to ire_ftable_lookup below in order to locate the 2504 * right prefix (and/or) ire cache. Similarly we also need 2505 * pass the label to the ire_cache_lookup below to locate 2506 * the right ire that also matches on the label. 2507 */ 2508 if (tcp->tcp_connp->conn_nexthop_set) { 2509 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2510 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2511 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2512 ipst); 2513 if (ire == NULL) { 2514 ire = ire_ftable_lookup( 2515 tcp->tcp_connp->conn_nexthop_v4, 2516 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2517 tsl, match_flags, ipst); 2518 if (ire == NULL) 2519 return (0); 2520 } else { 2521 ire_uinfo = &ire->ire_uinfo; 2522 } 2523 } else { 2524 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2525 zoneid, tsl, ipst); 2526 if (ire != NULL) { 2527 ire_cacheable = B_TRUE; 2528 ire_uinfo = (ire_mp != NULL) ? 2529 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2530 &ire->ire_uinfo; 2531 2532 } else { 2533 if (ire_mp == NULL) { 2534 ire = ire_ftable_lookup( 2535 tcp->tcp_connp->conn_rem, 2536 0, 0, 0, NULL, &sire, zoneid, 0, 2537 tsl, (MATCH_IRE_RECURSIVE | 2538 MATCH_IRE_DEFAULT), ipst); 2539 if (ire == NULL) 2540 return (0); 2541 ire_uinfo = (sire != NULL) ? 2542 &sire->ire_uinfo : 2543 &ire->ire_uinfo; 2544 } else { 2545 ire = (ire_t *)ire_mp->b_rptr; 2546 ire_uinfo = 2547 &((ire_t *) 2548 ire_mp->b_rptr)->ire_uinfo; 2549 } 2550 } 2551 } 2552 ASSERT(ire != NULL); 2553 2554 if ((ire->ire_src_addr == INADDR_ANY) || 2555 (ire->ire_type & IRE_BROADCAST)) { 2556 /* 2557 * ire->ire_mp is non null when ire_mp passed in is used 2558 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2559 */ 2560 if (ire->ire_mp == NULL) 2561 ire_refrele(ire); 2562 if (sire != NULL) 2563 ire_refrele(sire); 2564 return (0); 2565 } 2566 2567 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2568 ipaddr_t src_addr; 2569 2570 /* 2571 * ip_bind_connected() has stored the correct source 2572 * address in conn_src. 2573 */ 2574 src_addr = tcp->tcp_connp->conn_src; 2575 tcp->tcp_ipha->ipha_src = src_addr; 2576 /* 2577 * Copy of the src addr. in tcp_t is needed 2578 * for the lookup funcs. 2579 */ 2580 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2581 } 2582 /* 2583 * Set the fragment bit so that IP will tell us if the MTU 2584 * should change. IP tells us the latest setting of 2585 * ip_path_mtu_discovery through ire_frag_flag. 2586 */ 2587 if (ipst->ips_ip_path_mtu_discovery) { 2588 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2589 htons(IPH_DF); 2590 } 2591 /* 2592 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2593 * for IP_NEXTHOP. No cache ire has been found for the 2594 * destination and we are working with the nexthop's 2595 * interface ire. Since we need to forward all packets 2596 * to the nexthop first, we "blindly" set tcp_localnet 2597 * to false, eventhough the destination may also be 2598 * onlink. 2599 */ 2600 if (ire_uinfo == NULL) 2601 tcp->tcp_localnet = 0; 2602 else 2603 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2604 } else { 2605 /* 2606 * For incoming connection ire_mp = NULL 2607 * For outgoing connection ire_mp != NULL 2608 * Technically we should check conn_incoming_ill 2609 * when ire_mp is NULL and conn_outgoing_ill when 2610 * ire_mp is non-NULL. But this is performance 2611 * critical path and for IPV*_BOUND_IF, outgoing 2612 * and incoming ill are always set to the same value. 2613 */ 2614 ill_t *dst_ill = NULL; 2615 ipif_t *dst_ipif = NULL; 2616 2617 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2618 2619 if (connp->conn_outgoing_ill != NULL) { 2620 /* Outgoing or incoming path */ 2621 int err; 2622 2623 dst_ill = conn_get_held_ill(connp, 2624 &connp->conn_outgoing_ill, &err); 2625 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2626 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2627 return (0); 2628 } 2629 match_flags |= MATCH_IRE_ILL; 2630 dst_ipif = dst_ill->ill_ipif; 2631 } 2632 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2633 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2634 2635 if (ire != NULL) { 2636 ire_cacheable = B_TRUE; 2637 ire_uinfo = (ire_mp != NULL) ? 2638 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2639 &ire->ire_uinfo; 2640 } else { 2641 if (ire_mp == NULL) { 2642 ire = ire_ftable_lookup_v6( 2643 &tcp->tcp_connp->conn_remv6, 2644 0, 0, 0, dst_ipif, &sire, zoneid, 2645 0, tsl, match_flags, ipst); 2646 if (ire == NULL) { 2647 if (dst_ill != NULL) 2648 ill_refrele(dst_ill); 2649 return (0); 2650 } 2651 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2652 &ire->ire_uinfo; 2653 } else { 2654 ire = (ire_t *)ire_mp->b_rptr; 2655 ire_uinfo = 2656 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2657 } 2658 } 2659 if (dst_ill != NULL) 2660 ill_refrele(dst_ill); 2661 2662 ASSERT(ire != NULL); 2663 ASSERT(ire_uinfo != NULL); 2664 2665 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2666 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2667 /* 2668 * ire->ire_mp is non null when ire_mp passed in is used 2669 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2670 */ 2671 if (ire->ire_mp == NULL) 2672 ire_refrele(ire); 2673 if (sire != NULL) 2674 ire_refrele(sire); 2675 return (0); 2676 } 2677 2678 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2679 in6_addr_t src_addr; 2680 2681 /* 2682 * ip_bind_connected_v6() has stored the correct source 2683 * address per IPv6 addr. selection policy in 2684 * conn_src_v6. 2685 */ 2686 src_addr = tcp->tcp_connp->conn_srcv6; 2687 2688 tcp->tcp_ip6h->ip6_src = src_addr; 2689 /* 2690 * Copy of the src addr. in tcp_t is needed 2691 * for the lookup funcs. 2692 */ 2693 tcp->tcp_ip_src_v6 = src_addr; 2694 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2695 &connp->conn_srcv6)); 2696 } 2697 tcp->tcp_localnet = 2698 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2699 } 2700 2701 /* 2702 * This allows applications to fail quickly when connections are made 2703 * to dead hosts. Hosts can be labeled dead by adding a reject route 2704 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2705 */ 2706 if ((ire->ire_flags & RTF_REJECT) && 2707 (ire->ire_flags & RTF_PRIVATE)) 2708 goto error; 2709 2710 /* 2711 * Make use of the cached rtt and rtt_sd values to calculate the 2712 * initial RTO. Note that they are already initialized in 2713 * tcp_init_values(). 2714 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2715 * IP_NEXTHOP, but instead are using the interface ire for the 2716 * nexthop, then we do not use the ire_uinfo from that ire to 2717 * do any initializations. 2718 */ 2719 if (ire_uinfo != NULL) { 2720 if (ire_uinfo->iulp_rtt != 0) { 2721 clock_t rto; 2722 2723 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2724 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2725 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2726 tcps->tcps_rexmit_interval_extra + 2727 (tcp->tcp_rtt_sa >> 5); 2728 2729 if (rto > tcps->tcps_rexmit_interval_max) { 2730 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2731 } else if (rto < tcps->tcps_rexmit_interval_min) { 2732 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2733 } else { 2734 tcp->tcp_rto = rto; 2735 } 2736 } 2737 if (ire_uinfo->iulp_ssthresh != 0) 2738 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2739 else 2740 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2741 if (ire_uinfo->iulp_spipe > 0) { 2742 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2743 tcps->tcps_max_buf); 2744 if (tcps->tcps_snd_lowat_fraction != 0) 2745 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2746 tcps->tcps_snd_lowat_fraction; 2747 (void) tcp_maxpsz_set(tcp, B_TRUE); 2748 } 2749 /* 2750 * Note that up till now, acceptor always inherits receive 2751 * window from the listener. But if there is a metrics 2752 * associated with a host, we should use that instead of 2753 * inheriting it from listener. Thus we need to pass this 2754 * info back to the caller. 2755 */ 2756 if (ire_uinfo->iulp_rpipe > 0) { 2757 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2758 tcps->tcps_max_buf); 2759 } 2760 2761 if (ire_uinfo->iulp_rtomax > 0) { 2762 tcp->tcp_second_timer_threshold = 2763 ire_uinfo->iulp_rtomax; 2764 } 2765 2766 /* 2767 * Use the metric option settings, iulp_tstamp_ok and 2768 * iulp_wscale_ok, only for active open. What this means 2769 * is that if the other side uses timestamp or window 2770 * scale option, TCP will also use those options. That 2771 * is for passive open. If the application sets a 2772 * large window, window scale is enabled regardless of 2773 * the value in iulp_wscale_ok. This is the behavior 2774 * since 2.6. So we keep it. 2775 * The only case left in passive open processing is the 2776 * check for SACK. 2777 * For ECN, it should probably be like SACK. But the 2778 * current value is binary, so we treat it like the other 2779 * cases. The metric only controls active open.For passive 2780 * open, the ndd param, tcp_ecn_permitted, controls the 2781 * behavior. 2782 */ 2783 if (!tcp_detached) { 2784 /* 2785 * The if check means that the following can only 2786 * be turned on by the metrics only IRE, but not off. 2787 */ 2788 if (ire_uinfo->iulp_tstamp_ok) 2789 tcp->tcp_snd_ts_ok = B_TRUE; 2790 if (ire_uinfo->iulp_wscale_ok) 2791 tcp->tcp_snd_ws_ok = B_TRUE; 2792 if (ire_uinfo->iulp_sack == 2) 2793 tcp->tcp_snd_sack_ok = B_TRUE; 2794 if (ire_uinfo->iulp_ecn_ok) 2795 tcp->tcp_ecn_ok = B_TRUE; 2796 } else { 2797 /* 2798 * Passive open. 2799 * 2800 * As above, the if check means that SACK can only be 2801 * turned on by the metric only IRE. 2802 */ 2803 if (ire_uinfo->iulp_sack > 0) { 2804 tcp->tcp_snd_sack_ok = B_TRUE; 2805 } 2806 } 2807 } 2808 2809 2810 /* 2811 * XXX: Note that currently, ire_max_frag can be as small as 68 2812 * because of PMTUd. So tcp_mss may go to negative if combined 2813 * length of all those options exceeds 28 bytes. But because 2814 * of the tcp_mss_min check below, we may not have a problem if 2815 * tcp_mss_min is of a reasonable value. The default is 1 so 2816 * the negative problem still exists. And the check defeats PMTUd. 2817 * In fact, if PMTUd finds that the MSS should be smaller than 2818 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2819 * value. 2820 * 2821 * We do not deal with that now. All those problems related to 2822 * PMTUd will be fixed later. 2823 */ 2824 ASSERT(ire->ire_max_frag != 0); 2825 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2826 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2827 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2828 mss = MIN(mss, IPV6_MIN_MTU); 2829 } 2830 } 2831 2832 /* Sanity check for MSS value. */ 2833 if (tcp->tcp_ipversion == IPV4_VERSION) 2834 mss_max = tcps->tcps_mss_max_ipv4; 2835 else 2836 mss_max = tcps->tcps_mss_max_ipv6; 2837 2838 if (tcp->tcp_ipversion == IPV6_VERSION && 2839 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2840 /* 2841 * After receiving an ICMPv6 "packet too big" message with a 2842 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2843 * will insert a 8-byte fragment header in every packet; we 2844 * reduce the MSS by that amount here. 2845 */ 2846 mss -= sizeof (ip6_frag_t); 2847 } 2848 2849 if (tcp->tcp_ipsec_overhead == 0) 2850 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2851 2852 mss -= tcp->tcp_ipsec_overhead; 2853 2854 if (mss < tcps->tcps_mss_min) 2855 mss = tcps->tcps_mss_min; 2856 if (mss > mss_max) 2857 mss = mss_max; 2858 2859 /* Note that this is the maximum MSS, excluding all options. */ 2860 tcp->tcp_mss = mss; 2861 2862 /* 2863 * Initialize the ISS here now that we have the full connection ID. 2864 * The RFC 1948 method of initial sequence number generation requires 2865 * knowledge of the full connection ID before setting the ISS. 2866 */ 2867 2868 tcp_iss_init(tcp); 2869 2870 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2871 tcp->tcp_loopback = B_TRUE; 2872 2873 if (sire != NULL) 2874 IRE_REFRELE(sire); 2875 2876 /* 2877 * If we got an IRE_CACHE and an ILL, go through their properties; 2878 * otherwise, this is deferred until later when we have an IRE_CACHE. 2879 */ 2880 if (tcp->tcp_loopback || 2881 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2882 /* 2883 * For incoming, see if this tcp may be MDT-capable. For 2884 * outgoing, this process has been taken care of through 2885 * tcp_rput_other. 2886 */ 2887 tcp_ire_ill_check(tcp, ire, ill, incoming); 2888 tcp->tcp_ire_ill_check_done = B_TRUE; 2889 } 2890 2891 mutex_enter(&connp->conn_lock); 2892 /* 2893 * Make sure that conn is not marked incipient 2894 * for incoming connections. A blind 2895 * removal of incipient flag is cheaper than 2896 * check and removal. 2897 */ 2898 connp->conn_state_flags &= ~CONN_INCIPIENT; 2899 2900 /* 2901 * Must not cache forwarding table routes 2902 * or recache an IRE after the conn_t has 2903 * had conn_ire_cache cleared and is flagged 2904 * unusable, (see the CONN_CACHE_IRE() macro). 2905 */ 2906 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2907 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2908 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2909 connp->conn_ire_cache = ire; 2910 IRE_UNTRACE_REF(ire); 2911 rw_exit(&ire->ire_bucket->irb_lock); 2912 mutex_exit(&connp->conn_lock); 2913 return (1); 2914 } 2915 rw_exit(&ire->ire_bucket->irb_lock); 2916 } 2917 mutex_exit(&connp->conn_lock); 2918 2919 if (ire->ire_mp == NULL) 2920 ire_refrele(ire); 2921 return (1); 2922 2923 error: 2924 if (ire->ire_mp == NULL) 2925 ire_refrele(ire); 2926 if (sire != NULL) 2927 ire_refrele(sire); 2928 return (0); 2929 } 2930 2931 static void 2932 tcp_tpi_bind(tcp_t *tcp, mblk_t *mp) 2933 { 2934 int error; 2935 conn_t *connp = tcp->tcp_connp; 2936 struct sockaddr *sa; 2937 mblk_t *mp1; 2938 struct T_bind_req *tbr; 2939 int backlog; 2940 socklen_t len; 2941 sin_t *sin; 2942 sin6_t *sin6; 2943 cred_t *cr; 2944 2945 /* 2946 * All Solaris components should pass a db_credp 2947 * for this TPI message, hence we ASSERT. 2948 * But in case there is some other M_PROTO that looks 2949 * like a TPI message sent by some other kernel 2950 * component, we check and return an error. 2951 */ 2952 cr = msg_getcred(mp, NULL); 2953 ASSERT(cr != NULL); 2954 if (cr == NULL) { 2955 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 2956 return; 2957 } 2958 2959 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 2960 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 2961 if (tcp->tcp_debug) { 2962 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 2963 "tcp_tpi_bind: bad req, len %u", 2964 (uint_t)(mp->b_wptr - mp->b_rptr)); 2965 } 2966 tcp_err_ack(tcp, mp, TPROTO, 0); 2967 return; 2968 } 2969 /* Make sure the largest address fits */ 2970 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 2971 if (mp1 == NULL) { 2972 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 2973 return; 2974 } 2975 mp = mp1; 2976 tbr = (struct T_bind_req *)mp->b_rptr; 2977 2978 backlog = tbr->CONIND_number; 2979 len = tbr->ADDR_length; 2980 2981 switch (len) { 2982 case 0: /* request for a generic port */ 2983 tbr->ADDR_offset = sizeof (struct T_bind_req); 2984 if (tcp->tcp_family == AF_INET) { 2985 tbr->ADDR_length = sizeof (sin_t); 2986 sin = (sin_t *)&tbr[1]; 2987 *sin = sin_null; 2988 sin->sin_family = AF_INET; 2989 sa = (struct sockaddr *)sin; 2990 len = sizeof (sin_t); 2991 mp->b_wptr = (uchar_t *)&sin[1]; 2992 } else { 2993 ASSERT(tcp->tcp_family == AF_INET6); 2994 tbr->ADDR_length = sizeof (sin6_t); 2995 sin6 = (sin6_t *)&tbr[1]; 2996 *sin6 = sin6_null; 2997 sin6->sin6_family = AF_INET6; 2998 sa = (struct sockaddr *)sin6; 2999 len = sizeof (sin6_t); 3000 mp->b_wptr = (uchar_t *)&sin6[1]; 3001 } 3002 break; 3003 3004 case sizeof (sin_t): /* Complete IPv4 address */ 3005 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 3006 sizeof (sin_t)); 3007 break; 3008 3009 case sizeof (sin6_t): /* Complete IPv6 address */ 3010 sa = (struct sockaddr *)mi_offset_param(mp, 3011 tbr->ADDR_offset, sizeof (sin6_t)); 3012 break; 3013 3014 default: 3015 if (tcp->tcp_debug) { 3016 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3017 "tcp_tpi_bind: bad address length, %d", 3018 tbr->ADDR_length); 3019 } 3020 tcp_err_ack(tcp, mp, TBADADDR, 0); 3021 return; 3022 } 3023 3024 if (backlog > 0) { 3025 error = tcp_do_listen(connp, sa, len, backlog, DB_CRED(mp), 3026 tbr->PRIM_type != O_T_BIND_REQ); 3027 } else { 3028 error = tcp_do_bind(connp, sa, len, DB_CRED(mp), 3029 tbr->PRIM_type != O_T_BIND_REQ); 3030 } 3031 done: 3032 if (error > 0) { 3033 tcp_err_ack(tcp, mp, TSYSERR, error); 3034 } else if (error < 0) { 3035 tcp_err_ack(tcp, mp, -error, 0); 3036 } else { 3037 /* 3038 * Update port information as sockfs/tpi needs it for checking 3039 */ 3040 if (tcp->tcp_family == AF_INET) { 3041 sin = (sin_t *)sa; 3042 sin->sin_port = tcp->tcp_lport; 3043 } else { 3044 sin6 = (sin6_t *)sa; 3045 sin6->sin6_port = tcp->tcp_lport; 3046 } 3047 mp->b_datap->db_type = M_PCPROTO; 3048 tbr->PRIM_type = T_BIND_ACK; 3049 putnext(tcp->tcp_rq, mp); 3050 } 3051 } 3052 3053 /* 3054 * If the "bind_to_req_port_only" parameter is set, if the requested port 3055 * number is available, return it, If not return 0 3056 * 3057 * If "bind_to_req_port_only" parameter is not set and 3058 * If the requested port number is available, return it. If not, return 3059 * the first anonymous port we happen across. If no anonymous ports are 3060 * available, return 0. addr is the requested local address, if any. 3061 * 3062 * In either case, when succeeding update the tcp_t to record the port number 3063 * and insert it in the bind hash table. 3064 * 3065 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3066 * without setting SO_REUSEADDR. This is needed so that they 3067 * can be viewed as two independent transport protocols. 3068 */ 3069 static in_port_t 3070 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3071 int reuseaddr, boolean_t quick_connect, 3072 boolean_t bind_to_req_port_only, boolean_t user_specified) 3073 { 3074 /* number of times we have run around the loop */ 3075 int count = 0; 3076 /* maximum number of times to run around the loop */ 3077 int loopmax; 3078 conn_t *connp = tcp->tcp_connp; 3079 zoneid_t zoneid = connp->conn_zoneid; 3080 tcp_stack_t *tcps = tcp->tcp_tcps; 3081 3082 /* 3083 * Lookup for free addresses is done in a loop and "loopmax" 3084 * influences how long we spin in the loop 3085 */ 3086 if (bind_to_req_port_only) { 3087 /* 3088 * If the requested port is busy, don't bother to look 3089 * for a new one. Setting loop maximum count to 1 has 3090 * that effect. 3091 */ 3092 loopmax = 1; 3093 } else { 3094 /* 3095 * If the requested port is busy, look for a free one 3096 * in the anonymous port range. 3097 * Set loopmax appropriately so that one does not look 3098 * forever in the case all of the anonymous ports are in use. 3099 */ 3100 if (tcp->tcp_anon_priv_bind) { 3101 /* 3102 * loopmax = 3103 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3104 */ 3105 loopmax = IPPORT_RESERVED - 3106 tcps->tcps_min_anonpriv_port; 3107 } else { 3108 loopmax = (tcps->tcps_largest_anon_port - 3109 tcps->tcps_smallest_anon_port + 1); 3110 } 3111 } 3112 do { 3113 uint16_t lport; 3114 tf_t *tbf; 3115 tcp_t *ltcp; 3116 conn_t *lconnp; 3117 3118 lport = htons(port); 3119 3120 /* 3121 * Ensure that the tcp_t is not currently in the bind hash. 3122 * Hold the lock on the hash bucket to ensure that 3123 * the duplicate check plus the insertion is an atomic 3124 * operation. 3125 * 3126 * This function does an inline lookup on the bind hash list 3127 * Make sure that we access only members of tcp_t 3128 * and that we don't look at tcp_tcp, since we are not 3129 * doing a CONN_INC_REF. 3130 */ 3131 tcp_bind_hash_remove(tcp); 3132 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3133 mutex_enter(&tbf->tf_lock); 3134 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3135 ltcp = ltcp->tcp_bind_hash) { 3136 if (lport == ltcp->tcp_lport) 3137 break; 3138 } 3139 3140 for (; ltcp != NULL; ltcp = ltcp->tcp_bind_hash_port) { 3141 boolean_t not_socket; 3142 boolean_t exclbind; 3143 3144 lconnp = ltcp->tcp_connp; 3145 3146 /* 3147 * On a labeled system, we must treat bindings to ports 3148 * on shared IP addresses by sockets with MAC exemption 3149 * privilege as being in all zones, as there's 3150 * otherwise no way to identify the right receiver. 3151 */ 3152 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3153 IPCL_ZONE_MATCH(connp, 3154 ltcp->tcp_connp->conn_zoneid)) && 3155 !lconnp->conn_mac_exempt && 3156 !connp->conn_mac_exempt) 3157 continue; 3158 3159 /* 3160 * If TCP_EXCLBIND is set for either the bound or 3161 * binding endpoint, the semantics of bind 3162 * is changed according to the following. 3163 * 3164 * spec = specified address (v4 or v6) 3165 * unspec = unspecified address (v4 or v6) 3166 * A = specified addresses are different for endpoints 3167 * 3168 * bound bind to allowed 3169 * ------------------------------------- 3170 * unspec unspec no 3171 * unspec spec no 3172 * spec unspec no 3173 * spec spec yes if A 3174 * 3175 * For labeled systems, SO_MAC_EXEMPT behaves the same 3176 * as TCP_EXCLBIND, except that zoneid is ignored. 3177 * 3178 * Note: 3179 * 3180 * 1. Because of TLI semantics, an endpoint can go 3181 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3182 * TCPS_BOUND, depending on whether it is originally 3183 * a listener or not. That is why we need to check 3184 * for states greater than or equal to TCPS_BOUND 3185 * here. 3186 * 3187 * 2. Ideally, we should only check for state equals 3188 * to TCPS_LISTEN. And the following check should be 3189 * added. 3190 * 3191 * if (ltcp->tcp_state == TCPS_LISTEN || 3192 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3193 * ... 3194 * } 3195 * 3196 * The semantics will be changed to this. If the 3197 * endpoint on the list is in state not equal to 3198 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3199 * set, let the bind succeed. 3200 * 3201 * Because of (1), we cannot do that for TLI 3202 * endpoints. But we can do that for socket endpoints. 3203 * If in future, we can change this going back 3204 * semantics, we can use the above check for TLI also. 3205 */ 3206 not_socket = !(TCP_IS_SOCKET(ltcp) && 3207 TCP_IS_SOCKET(tcp)); 3208 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3209 3210 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3211 (exclbind && (not_socket || 3212 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3213 if (V6_OR_V4_INADDR_ANY( 3214 ltcp->tcp_bound_source_v6) || 3215 V6_OR_V4_INADDR_ANY(*laddr) || 3216 IN6_ARE_ADDR_EQUAL(laddr, 3217 <cp->tcp_bound_source_v6)) { 3218 break; 3219 } 3220 continue; 3221 } 3222 3223 /* 3224 * Check ipversion to allow IPv4 and IPv6 sockets to 3225 * have disjoint port number spaces, if *_EXCLBIND 3226 * is not set and only if the application binds to a 3227 * specific port. We use the same autoassigned port 3228 * number space for IPv4 and IPv6 sockets. 3229 */ 3230 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3231 bind_to_req_port_only) 3232 continue; 3233 3234 /* 3235 * Ideally, we should make sure that the source 3236 * address, remote address, and remote port in the 3237 * four tuple for this tcp-connection is unique. 3238 * However, trying to find out the local source 3239 * address would require too much code duplication 3240 * with IP, since IP needs needs to have that code 3241 * to support userland TCP implementations. 3242 */ 3243 if (quick_connect && 3244 (ltcp->tcp_state > TCPS_LISTEN) && 3245 ((tcp->tcp_fport != ltcp->tcp_fport) || 3246 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3247 <cp->tcp_remote_v6))) 3248 continue; 3249 3250 if (!reuseaddr) { 3251 /* 3252 * No socket option SO_REUSEADDR. 3253 * If existing port is bound to 3254 * a non-wildcard IP address 3255 * and the requesting stream is 3256 * bound to a distinct 3257 * different IP addresses 3258 * (non-wildcard, also), keep 3259 * going. 3260 */ 3261 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3262 !V6_OR_V4_INADDR_ANY( 3263 ltcp->tcp_bound_source_v6) && 3264 !IN6_ARE_ADDR_EQUAL(laddr, 3265 <cp->tcp_bound_source_v6)) 3266 continue; 3267 if (ltcp->tcp_state >= TCPS_BOUND) { 3268 /* 3269 * This port is being used and 3270 * its state is >= TCPS_BOUND, 3271 * so we can't bind to it. 3272 */ 3273 break; 3274 } 3275 } else { 3276 /* 3277 * socket option SO_REUSEADDR is set on the 3278 * binding tcp_t. 3279 * 3280 * If two streams are bound to 3281 * same IP address or both addr 3282 * and bound source are wildcards 3283 * (INADDR_ANY), we want to stop 3284 * searching. 3285 * We have found a match of IP source 3286 * address and source port, which is 3287 * refused regardless of the 3288 * SO_REUSEADDR setting, so we break. 3289 */ 3290 if (IN6_ARE_ADDR_EQUAL(laddr, 3291 <cp->tcp_bound_source_v6) && 3292 (ltcp->tcp_state == TCPS_LISTEN || 3293 ltcp->tcp_state == TCPS_BOUND)) 3294 break; 3295 } 3296 } 3297 if (ltcp != NULL) { 3298 /* The port number is busy */ 3299 mutex_exit(&tbf->tf_lock); 3300 } else { 3301 /* 3302 * This port is ours. Insert in fanout and mark as 3303 * bound to prevent others from getting the port 3304 * number. 3305 */ 3306 tcp->tcp_state = TCPS_BOUND; 3307 tcp->tcp_lport = htons(port); 3308 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3309 3310 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3311 tcp->tcp_lport)] == tbf); 3312 tcp_bind_hash_insert(tbf, tcp, 1); 3313 3314 mutex_exit(&tbf->tf_lock); 3315 3316 /* 3317 * We don't want tcp_next_port_to_try to "inherit" 3318 * a port number supplied by the user in a bind. 3319 */ 3320 if (user_specified) 3321 return (port); 3322 3323 /* 3324 * This is the only place where tcp_next_port_to_try 3325 * is updated. After the update, it may or may not 3326 * be in the valid range. 3327 */ 3328 if (!tcp->tcp_anon_priv_bind) 3329 tcps->tcps_next_port_to_try = port + 1; 3330 return (port); 3331 } 3332 3333 if (tcp->tcp_anon_priv_bind) { 3334 port = tcp_get_next_priv_port(tcp); 3335 } else { 3336 if (count == 0 && user_specified) { 3337 /* 3338 * We may have to return an anonymous port. So 3339 * get one to start with. 3340 */ 3341 port = 3342 tcp_update_next_port( 3343 tcps->tcps_next_port_to_try, 3344 tcp, B_TRUE); 3345 user_specified = B_FALSE; 3346 } else { 3347 port = tcp_update_next_port(port + 1, tcp, 3348 B_FALSE); 3349 } 3350 } 3351 if (port == 0) 3352 break; 3353 3354 /* 3355 * Don't let this loop run forever in the case where 3356 * all of the anonymous ports are in use. 3357 */ 3358 } while (++count < loopmax); 3359 return (0); 3360 } 3361 3362 /* 3363 * tcp_clean_death / tcp_close_detached must not be called more than once 3364 * on a tcp. Thus every function that potentially calls tcp_clean_death 3365 * must check for the tcp state before calling tcp_clean_death. 3366 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3367 * tcp_timer_handler, all check for the tcp state. 3368 */ 3369 /* ARGSUSED */ 3370 void 3371 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3372 { 3373 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3374 3375 freemsg(mp); 3376 if (tcp->tcp_state > TCPS_BOUND) 3377 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3378 ETIMEDOUT, 5); 3379 } 3380 3381 /* 3382 * We are dying for some reason. Try to do it gracefully. (May be called 3383 * as writer.) 3384 * 3385 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3386 * done by a service procedure). 3387 * TBD - Should the return value distinguish between the tcp_t being 3388 * freed and it being reinitialized? 3389 */ 3390 static int 3391 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3392 { 3393 mblk_t *mp; 3394 queue_t *q; 3395 conn_t *connp = tcp->tcp_connp; 3396 tcp_stack_t *tcps = tcp->tcp_tcps; 3397 3398 TCP_CLD_STAT(tag); 3399 3400 #if TCP_TAG_CLEAN_DEATH 3401 tcp->tcp_cleandeathtag = tag; 3402 #endif 3403 3404 if (tcp->tcp_fused) 3405 tcp_unfuse(tcp); 3406 3407 if (tcp->tcp_linger_tid != 0 && 3408 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3409 tcp_stop_lingering(tcp); 3410 } 3411 3412 ASSERT(tcp != NULL); 3413 ASSERT((tcp->tcp_family == AF_INET && 3414 tcp->tcp_ipversion == IPV4_VERSION) || 3415 (tcp->tcp_family == AF_INET6 && 3416 (tcp->tcp_ipversion == IPV4_VERSION || 3417 tcp->tcp_ipversion == IPV6_VERSION))); 3418 3419 if (TCP_IS_DETACHED(tcp)) { 3420 if (tcp->tcp_hard_binding) { 3421 /* 3422 * Its an eager that we are dealing with. We close the 3423 * eager but in case a conn_ind has already gone to the 3424 * listener, let tcp_accept_finish() send a discon_ind 3425 * to the listener and drop the last reference. If the 3426 * listener doesn't even know about the eager i.e. the 3427 * conn_ind hasn't gone up, blow away the eager and drop 3428 * the last reference as well. If the conn_ind has gone 3429 * up, state should be BOUND. tcp_accept_finish 3430 * will figure out that the connection has received a 3431 * RST and will send a DISCON_IND to the application. 3432 */ 3433 tcp_closei_local(tcp); 3434 if (!tcp->tcp_tconnind_started) { 3435 CONN_DEC_REF(connp); 3436 } else { 3437 tcp->tcp_state = TCPS_BOUND; 3438 } 3439 } else { 3440 tcp_close_detached(tcp); 3441 } 3442 return (0); 3443 } 3444 3445 TCP_STAT(tcps, tcp_clean_death_nondetached); 3446 3447 q = tcp->tcp_rq; 3448 3449 /* Trash all inbound data */ 3450 if (!IPCL_IS_NONSTR(connp)) { 3451 ASSERT(q != NULL); 3452 flushq(q, FLUSHALL); 3453 } 3454 3455 /* 3456 * If we are at least part way open and there is error 3457 * (err==0 implies no error) 3458 * notify our client by a T_DISCON_IND. 3459 */ 3460 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3461 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3462 !TCP_IS_SOCKET(tcp)) { 3463 /* 3464 * Send M_FLUSH according to TPI. Because sockets will 3465 * (and must) ignore FLUSHR we do that only for TPI 3466 * endpoints and sockets in STREAMS mode. 3467 */ 3468 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3469 } 3470 if (tcp->tcp_debug) { 3471 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3472 "tcp_clean_death: discon err %d", err); 3473 } 3474 if (IPCL_IS_NONSTR(connp)) { 3475 /* Direct socket, use upcall */ 3476 (*connp->conn_upcalls->su_disconnected)( 3477 connp->conn_upper_handle, tcp->tcp_connid, err); 3478 } else { 3479 mp = mi_tpi_discon_ind(NULL, err, 0); 3480 if (mp != NULL) { 3481 putnext(q, mp); 3482 } else { 3483 if (tcp->tcp_debug) { 3484 (void) strlog(TCP_MOD_ID, 0, 1, 3485 SL_ERROR|SL_TRACE, 3486 "tcp_clean_death, sending M_ERROR"); 3487 } 3488 (void) putnextctl1(q, M_ERROR, EPROTO); 3489 } 3490 } 3491 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3492 /* SYN_SENT or SYN_RCVD */ 3493 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3494 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3495 /* ESTABLISHED or CLOSE_WAIT */ 3496 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3497 } 3498 } 3499 3500 tcp_reinit(tcp); 3501 if (IPCL_IS_NONSTR(connp)) 3502 (void) tcp_do_unbind(connp); 3503 3504 return (-1); 3505 } 3506 3507 /* 3508 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3509 * to expire, stop the wait and finish the close. 3510 */ 3511 static void 3512 tcp_stop_lingering(tcp_t *tcp) 3513 { 3514 clock_t delta = 0; 3515 tcp_stack_t *tcps = tcp->tcp_tcps; 3516 3517 tcp->tcp_linger_tid = 0; 3518 if (tcp->tcp_state > TCPS_LISTEN) { 3519 tcp_acceptor_hash_remove(tcp); 3520 mutex_enter(&tcp->tcp_non_sq_lock); 3521 if (tcp->tcp_flow_stopped) { 3522 tcp_clrqfull(tcp); 3523 } 3524 mutex_exit(&tcp->tcp_non_sq_lock); 3525 3526 if (tcp->tcp_timer_tid != 0) { 3527 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3528 tcp->tcp_timer_tid = 0; 3529 } 3530 /* 3531 * Need to cancel those timers which will not be used when 3532 * TCP is detached. This has to be done before the tcp_wq 3533 * is set to the global queue. 3534 */ 3535 tcp_timers_stop(tcp); 3536 3537 tcp->tcp_detached = B_TRUE; 3538 ASSERT(tcps->tcps_g_q != NULL); 3539 tcp->tcp_rq = tcps->tcps_g_q; 3540 tcp->tcp_wq = WR(tcps->tcps_g_q); 3541 3542 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3543 tcp_time_wait_append(tcp); 3544 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3545 goto finish; 3546 } 3547 3548 /* 3549 * If delta is zero the timer event wasn't executed and was 3550 * successfully canceled. In this case we need to restart it 3551 * with the minimal delta possible. 3552 */ 3553 if (delta >= 0) { 3554 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3555 delta ? delta : 1); 3556 } 3557 } else { 3558 tcp_closei_local(tcp); 3559 CONN_DEC_REF(tcp->tcp_connp); 3560 } 3561 finish: 3562 /* Signal closing thread that it can complete close */ 3563 mutex_enter(&tcp->tcp_closelock); 3564 tcp->tcp_detached = B_TRUE; 3565 ASSERT(tcps->tcps_g_q != NULL); 3566 3567 tcp->tcp_rq = tcps->tcps_g_q; 3568 tcp->tcp_wq = WR(tcps->tcps_g_q); 3569 3570 tcp->tcp_closed = 1; 3571 cv_signal(&tcp->tcp_closecv); 3572 mutex_exit(&tcp->tcp_closelock); 3573 } 3574 3575 /* 3576 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3577 * expires. 3578 */ 3579 static void 3580 tcp_close_linger_timeout(void *arg) 3581 { 3582 conn_t *connp = (conn_t *)arg; 3583 tcp_t *tcp = connp->conn_tcp; 3584 3585 tcp->tcp_client_errno = ETIMEDOUT; 3586 tcp_stop_lingering(tcp); 3587 } 3588 3589 static void 3590 tcp_close_common(conn_t *connp, int flags) 3591 { 3592 tcp_t *tcp = connp->conn_tcp; 3593 mblk_t *mp = &tcp->tcp_closemp; 3594 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3595 mblk_t *bp; 3596 3597 ASSERT(connp->conn_ref >= 2); 3598 3599 /* 3600 * Mark the conn as closing. ill_pending_mp_add will not 3601 * add any mp to the pending mp list, after this conn has 3602 * started closing. Same for sq_pending_mp_add 3603 */ 3604 mutex_enter(&connp->conn_lock); 3605 connp->conn_state_flags |= CONN_CLOSING; 3606 if (connp->conn_oper_pending_ill != NULL) 3607 conn_ioctl_cleanup_reqd = B_TRUE; 3608 CONN_INC_REF_LOCKED(connp); 3609 mutex_exit(&connp->conn_lock); 3610 tcp->tcp_closeflags = (uint8_t)flags; 3611 ASSERT(connp->conn_ref >= 3); 3612 3613 /* 3614 * tcp_closemp_used is used below without any protection of a lock 3615 * as we don't expect any one else to use it concurrently at this 3616 * point otherwise it would be a major defect. 3617 */ 3618 3619 if (mp->b_prev == NULL) 3620 tcp->tcp_closemp_used = B_TRUE; 3621 else 3622 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 3623 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 3624 3625 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 3626 3627 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp, 3628 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3629 3630 mutex_enter(&tcp->tcp_closelock); 3631 while (!tcp->tcp_closed) { 3632 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 3633 /* 3634 * The cv_wait_sig() was interrupted. We now do the 3635 * following: 3636 * 3637 * 1) If the endpoint was lingering, we allow this 3638 * to be interrupted by cancelling the linger timeout 3639 * and closing normally. 3640 * 3641 * 2) Revert to calling cv_wait() 3642 * 3643 * We revert to using cv_wait() to avoid an 3644 * infinite loop which can occur if the calling 3645 * thread is higher priority than the squeue worker 3646 * thread and is bound to the same cpu. 3647 */ 3648 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 3649 mutex_exit(&tcp->tcp_closelock); 3650 /* Entering squeue, bump ref count. */ 3651 CONN_INC_REF(connp); 3652 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 3653 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, 3654 tcp_linger_interrupted, connp, 3655 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3656 mutex_enter(&tcp->tcp_closelock); 3657 } 3658 break; 3659 } 3660 } 3661 while (!tcp->tcp_closed) 3662 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3663 mutex_exit(&tcp->tcp_closelock); 3664 3665 /* 3666 * In the case of listener streams that have eagers in the q or q0 3667 * we wait for the eagers to drop their reference to us. tcp_rq and 3668 * tcp_wq of the eagers point to our queues. By waiting for the 3669 * refcnt to drop to 1, we are sure that the eagers have cleaned 3670 * up their queue pointers and also dropped their references to us. 3671 */ 3672 if (tcp->tcp_wait_for_eagers) { 3673 mutex_enter(&connp->conn_lock); 3674 while (connp->conn_ref != 1) { 3675 cv_wait(&connp->conn_cv, &connp->conn_lock); 3676 } 3677 mutex_exit(&connp->conn_lock); 3678 } 3679 /* 3680 * ioctl cleanup. The mp is queued in the 3681 * ill_pending_mp or in the sq_pending_mp. 3682 */ 3683 if (conn_ioctl_cleanup_reqd) 3684 conn_ioctl_cleanup(connp); 3685 3686 tcp->tcp_cpid = -1; 3687 } 3688 3689 static int 3690 tcp_tpi_close(queue_t *q, int flags) 3691 { 3692 conn_t *connp; 3693 3694 ASSERT(WR(q)->q_next == NULL); 3695 3696 if (flags & SO_FALLBACK) { 3697 /* 3698 * stream is being closed while in fallback 3699 * simply free the resources that were allocated 3700 */ 3701 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr)); 3702 qprocsoff(q); 3703 goto done; 3704 } 3705 3706 connp = Q_TO_CONN(q); 3707 /* 3708 * We are being closed as /dev/tcp or /dev/tcp6. 3709 */ 3710 tcp_close_common(connp, flags); 3711 3712 qprocsoff(q); 3713 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 3714 3715 /* 3716 * Drop IP's reference on the conn. This is the last reference 3717 * on the connp if the state was less than established. If the 3718 * connection has gone into timewait state, then we will have 3719 * one ref for the TCP and one more ref (total of two) for the 3720 * classifier connected hash list (a timewait connections stays 3721 * in connected hash till closed). 3722 * 3723 * We can't assert the references because there might be other 3724 * transient reference places because of some walkers or queued 3725 * packets in squeue for the timewait state. 3726 */ 3727 CONN_DEC_REF(connp); 3728 done: 3729 q->q_ptr = WR(q)->q_ptr = NULL; 3730 return (0); 3731 } 3732 3733 static int 3734 tcp_tpi_close_accept(queue_t *q) 3735 { 3736 vmem_t *minor_arena; 3737 dev_t conn_dev; 3738 3739 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 3740 3741 /* 3742 * We had opened an acceptor STREAM for sockfs which is 3743 * now being closed due to some error. 3744 */ 3745 qprocsoff(q); 3746 3747 minor_arena = (vmem_t *)WR(q)->q_ptr; 3748 conn_dev = (dev_t)RD(q)->q_ptr; 3749 ASSERT(minor_arena != NULL); 3750 ASSERT(conn_dev != 0); 3751 inet_minor_free(minor_arena, conn_dev); 3752 q->q_ptr = WR(q)->q_ptr = NULL; 3753 return (0); 3754 } 3755 3756 /* 3757 * Called by tcp_close() routine via squeue when lingering is 3758 * interrupted by a signal. 3759 */ 3760 3761 /* ARGSUSED */ 3762 static void 3763 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 3764 { 3765 conn_t *connp = (conn_t *)arg; 3766 tcp_t *tcp = connp->conn_tcp; 3767 3768 freeb(mp); 3769 if (tcp->tcp_linger_tid != 0 && 3770 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3771 tcp_stop_lingering(tcp); 3772 tcp->tcp_client_errno = EINTR; 3773 } 3774 } 3775 3776 /* 3777 * Called by streams close routine via squeues when our client blows off her 3778 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 3779 * connection politely" When SO_LINGER is set (with a non-zero linger time and 3780 * it is not a nonblocking socket) then this routine sleeps until the FIN is 3781 * acked. 3782 * 3783 * NOTE: tcp_close potentially returns error when lingering. 3784 * However, the stream head currently does not pass these errors 3785 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 3786 * errors to the application (from tsleep()) and not errors 3787 * like ECONNRESET caused by receiving a reset packet. 3788 */ 3789 3790 /* ARGSUSED */ 3791 static void 3792 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 3793 { 3794 char *msg; 3795 conn_t *connp = (conn_t *)arg; 3796 tcp_t *tcp = connp->conn_tcp; 3797 clock_t delta = 0; 3798 tcp_stack_t *tcps = tcp->tcp_tcps; 3799 3800 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 3801 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 3802 3803 mutex_enter(&tcp->tcp_eager_lock); 3804 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 3805 /* Cleanup for listener */ 3806 tcp_eager_cleanup(tcp, 0); 3807 tcp->tcp_wait_for_eagers = 1; 3808 } 3809 mutex_exit(&tcp->tcp_eager_lock); 3810 3811 connp->conn_mdt_ok = B_FALSE; 3812 tcp->tcp_mdt = B_FALSE; 3813 3814 connp->conn_lso_ok = B_FALSE; 3815 tcp->tcp_lso = B_FALSE; 3816 3817 msg = NULL; 3818 switch (tcp->tcp_state) { 3819 case TCPS_CLOSED: 3820 case TCPS_IDLE: 3821 case TCPS_BOUND: 3822 case TCPS_LISTEN: 3823 break; 3824 case TCPS_SYN_SENT: 3825 msg = "tcp_close, during connect"; 3826 break; 3827 case TCPS_SYN_RCVD: 3828 /* 3829 * Close during the connect 3-way handshake 3830 * but here there may or may not be pending data 3831 * already on queue. Process almost same as in 3832 * the ESTABLISHED state. 3833 */ 3834 /* FALLTHRU */ 3835 default: 3836 if (tcp->tcp_fused) 3837 tcp_unfuse(tcp); 3838 3839 /* 3840 * If SO_LINGER has set a zero linger time, abort the 3841 * connection with a reset. 3842 */ 3843 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 3844 msg = "tcp_close, zero lingertime"; 3845 break; 3846 } 3847 3848 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 3849 /* 3850 * Abort connection if there is unread data queued. 3851 */ 3852 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 3853 msg = "tcp_close, unread data"; 3854 break; 3855 } 3856 /* 3857 * tcp_hard_bound is now cleared thus all packets go through 3858 * tcp_lookup. This fact is used by tcp_detach below. 3859 * 3860 * We have done a qwait() above which could have possibly 3861 * drained more messages in turn causing transition to a 3862 * different state. Check whether we have to do the rest 3863 * of the processing or not. 3864 */ 3865 if (tcp->tcp_state <= TCPS_LISTEN) 3866 break; 3867 3868 /* 3869 * Transmit the FIN before detaching the tcp_t. 3870 * After tcp_detach returns this queue/perimeter 3871 * no longer owns the tcp_t thus others can modify it. 3872 */ 3873 (void) tcp_xmit_end(tcp); 3874 3875 /* 3876 * If lingering on close then wait until the fin is acked, 3877 * the SO_LINGER time passes, or a reset is sent/received. 3878 */ 3879 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 3880 !(tcp->tcp_fin_acked) && 3881 tcp->tcp_state >= TCPS_ESTABLISHED) { 3882 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 3883 tcp->tcp_client_errno = EWOULDBLOCK; 3884 } else if (tcp->tcp_client_errno == 0) { 3885 3886 ASSERT(tcp->tcp_linger_tid == 0); 3887 3888 tcp->tcp_linger_tid = TCP_TIMER(tcp, 3889 tcp_close_linger_timeout, 3890 tcp->tcp_lingertime * hz); 3891 3892 /* tcp_close_linger_timeout will finish close */ 3893 if (tcp->tcp_linger_tid == 0) 3894 tcp->tcp_client_errno = ENOSR; 3895 else 3896 return; 3897 } 3898 3899 /* 3900 * Check if we need to detach or just close 3901 * the instance. 3902 */ 3903 if (tcp->tcp_state <= TCPS_LISTEN) 3904 break; 3905 } 3906 3907 /* 3908 * Make sure that no other thread will access the tcp_rq of 3909 * this instance (through lookups etc.) as tcp_rq will go 3910 * away shortly. 3911 */ 3912 tcp_acceptor_hash_remove(tcp); 3913 3914 mutex_enter(&tcp->tcp_non_sq_lock); 3915 if (tcp->tcp_flow_stopped) { 3916 tcp_clrqfull(tcp); 3917 } 3918 mutex_exit(&tcp->tcp_non_sq_lock); 3919 3920 if (tcp->tcp_timer_tid != 0) { 3921 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3922 tcp->tcp_timer_tid = 0; 3923 } 3924 /* 3925 * Need to cancel those timers which will not be used when 3926 * TCP is detached. This has to be done before the tcp_wq 3927 * is set to the global queue. 3928 */ 3929 tcp_timers_stop(tcp); 3930 3931 tcp->tcp_detached = B_TRUE; 3932 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3933 tcp_time_wait_append(tcp); 3934 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3935 ASSERT(connp->conn_ref >= 3); 3936 goto finish; 3937 } 3938 3939 /* 3940 * If delta is zero the timer event wasn't executed and was 3941 * successfully canceled. In this case we need to restart it 3942 * with the minimal delta possible. 3943 */ 3944 if (delta >= 0) 3945 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3946 delta ? delta : 1); 3947 3948 ASSERT(connp->conn_ref >= 3); 3949 goto finish; 3950 } 3951 3952 /* Detach did not complete. Still need to remove q from stream. */ 3953 if (msg) { 3954 if (tcp->tcp_state == TCPS_ESTABLISHED || 3955 tcp->tcp_state == TCPS_CLOSE_WAIT) 3956 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3957 if (tcp->tcp_state == TCPS_SYN_SENT || 3958 tcp->tcp_state == TCPS_SYN_RCVD) 3959 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3960 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 3961 } 3962 3963 tcp_closei_local(tcp); 3964 CONN_DEC_REF(connp); 3965 ASSERT(connp->conn_ref >= 2); 3966 3967 finish: 3968 /* 3969 * Although packets are always processed on the correct 3970 * tcp's perimeter and access is serialized via squeue's, 3971 * IP still needs a queue when sending packets in time_wait 3972 * state so use WR(tcps_g_q) till ip_output() can be 3973 * changed to deal with just connp. For read side, we 3974 * could have set tcp_rq to NULL but there are some cases 3975 * in tcp_rput_data() from early days of this code which 3976 * do a putnext without checking if tcp is closed. Those 3977 * need to be identified before both tcp_rq and tcp_wq 3978 * can be set to NULL and tcps_g_q can disappear forever. 3979 */ 3980 mutex_enter(&tcp->tcp_closelock); 3981 /* 3982 * Don't change the queues in the case of a listener that has 3983 * eagers in its q or q0. It could surprise the eagers. 3984 * Instead wait for the eagers outside the squeue. 3985 */ 3986 if (!tcp->tcp_wait_for_eagers) { 3987 tcp->tcp_detached = B_TRUE; 3988 /* 3989 * When default queue is closing we set tcps_g_q to NULL 3990 * after the close is done. 3991 */ 3992 ASSERT(tcps->tcps_g_q != NULL); 3993 tcp->tcp_rq = tcps->tcps_g_q; 3994 tcp->tcp_wq = WR(tcps->tcps_g_q); 3995 } 3996 3997 /* Signal tcp_close() to finish closing. */ 3998 tcp->tcp_closed = 1; 3999 cv_signal(&tcp->tcp_closecv); 4000 mutex_exit(&tcp->tcp_closelock); 4001 } 4002 4003 /* 4004 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4005 * Some stream heads get upset if they see these later on as anything but NULL. 4006 */ 4007 static void 4008 tcp_close_mpp(mblk_t **mpp) 4009 { 4010 mblk_t *mp; 4011 4012 if ((mp = *mpp) != NULL) { 4013 do { 4014 mp->b_next = NULL; 4015 mp->b_prev = NULL; 4016 } while ((mp = mp->b_cont) != NULL); 4017 4018 mp = *mpp; 4019 *mpp = NULL; 4020 freemsg(mp); 4021 } 4022 } 4023 4024 /* Do detached close. */ 4025 static void 4026 tcp_close_detached(tcp_t *tcp) 4027 { 4028 if (tcp->tcp_fused) 4029 tcp_unfuse(tcp); 4030 4031 /* 4032 * Clustering code serializes TCP disconnect callbacks and 4033 * cluster tcp list walks by blocking a TCP disconnect callback 4034 * if a cluster tcp list walk is in progress. This ensures 4035 * accurate accounting of TCPs in the cluster code even though 4036 * the TCP list walk itself is not atomic. 4037 */ 4038 tcp_closei_local(tcp); 4039 CONN_DEC_REF(tcp->tcp_connp); 4040 } 4041 4042 /* 4043 * Stop all TCP timers, and free the timer mblks if requested. 4044 */ 4045 void 4046 tcp_timers_stop(tcp_t *tcp) 4047 { 4048 if (tcp->tcp_timer_tid != 0) { 4049 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4050 tcp->tcp_timer_tid = 0; 4051 } 4052 if (tcp->tcp_ka_tid != 0) { 4053 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4054 tcp->tcp_ka_tid = 0; 4055 } 4056 if (tcp->tcp_ack_tid != 0) { 4057 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4058 tcp->tcp_ack_tid = 0; 4059 } 4060 if (tcp->tcp_push_tid != 0) { 4061 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4062 tcp->tcp_push_tid = 0; 4063 } 4064 } 4065 4066 /* 4067 * The tcp_t is going away. Remove it from all lists and set it 4068 * to TCPS_CLOSED. The freeing up of memory is deferred until 4069 * tcp_inactive. This is needed since a thread in tcp_rput might have 4070 * done a CONN_INC_REF on this structure before it was removed from the 4071 * hashes. 4072 */ 4073 static void 4074 tcp_closei_local(tcp_t *tcp) 4075 { 4076 ire_t *ire; 4077 conn_t *connp = tcp->tcp_connp; 4078 tcp_stack_t *tcps = tcp->tcp_tcps; 4079 4080 if (!TCP_IS_SOCKET(tcp)) 4081 tcp_acceptor_hash_remove(tcp); 4082 4083 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4084 tcp->tcp_ibsegs = 0; 4085 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4086 tcp->tcp_obsegs = 0; 4087 4088 /* 4089 * If we are an eager connection hanging off a listener that 4090 * hasn't formally accepted the connection yet, get off his 4091 * list and blow off any data that we have accumulated. 4092 */ 4093 if (tcp->tcp_listener != NULL) { 4094 tcp_t *listener = tcp->tcp_listener; 4095 mutex_enter(&listener->tcp_eager_lock); 4096 /* 4097 * tcp_tconnind_started == B_TRUE means that the 4098 * conn_ind has already gone to listener. At 4099 * this point, eager will be closed but we 4100 * leave it in listeners eager list so that 4101 * if listener decides to close without doing 4102 * accept, we can clean this up. In tcp_wput_accept 4103 * we take care of the case of accept on closed 4104 * eager. 4105 */ 4106 if (!tcp->tcp_tconnind_started) { 4107 tcp_eager_unlink(tcp); 4108 mutex_exit(&listener->tcp_eager_lock); 4109 /* 4110 * We don't want to have any pointers to the 4111 * listener queue, after we have released our 4112 * reference on the listener 4113 */ 4114 ASSERT(tcps->tcps_g_q != NULL); 4115 tcp->tcp_rq = tcps->tcps_g_q; 4116 tcp->tcp_wq = WR(tcps->tcps_g_q); 4117 CONN_DEC_REF(listener->tcp_connp); 4118 } else { 4119 mutex_exit(&listener->tcp_eager_lock); 4120 } 4121 } 4122 4123 /* Stop all the timers */ 4124 tcp_timers_stop(tcp); 4125 4126 if (tcp->tcp_state == TCPS_LISTEN) { 4127 if (tcp->tcp_ip_addr_cache) { 4128 kmem_free((void *)tcp->tcp_ip_addr_cache, 4129 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4130 tcp->tcp_ip_addr_cache = NULL; 4131 } 4132 } 4133 mutex_enter(&tcp->tcp_non_sq_lock); 4134 if (tcp->tcp_flow_stopped) 4135 tcp_clrqfull(tcp); 4136 mutex_exit(&tcp->tcp_non_sq_lock); 4137 4138 tcp_bind_hash_remove(tcp); 4139 /* 4140 * If the tcp_time_wait_collector (which runs outside the squeue) 4141 * is trying to remove this tcp from the time wait list, we will 4142 * block in tcp_time_wait_remove while trying to acquire the 4143 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4144 * requires the ipcl_hash_remove to be ordered after the 4145 * tcp_time_wait_remove for the refcnt checks to work correctly. 4146 */ 4147 if (tcp->tcp_state == TCPS_TIME_WAIT) 4148 (void) tcp_time_wait_remove(tcp, NULL); 4149 CL_INET_DISCONNECT(connp, tcp); 4150 ipcl_hash_remove(connp); 4151 4152 /* 4153 * Delete the cached ire in conn_ire_cache and also mark 4154 * the conn as CONDEMNED 4155 */ 4156 mutex_enter(&connp->conn_lock); 4157 connp->conn_state_flags |= CONN_CONDEMNED; 4158 ire = connp->conn_ire_cache; 4159 connp->conn_ire_cache = NULL; 4160 mutex_exit(&connp->conn_lock); 4161 if (ire != NULL) 4162 IRE_REFRELE_NOTR(ire); 4163 4164 /* Need to cleanup any pending ioctls */ 4165 ASSERT(tcp->tcp_time_wait_next == NULL); 4166 ASSERT(tcp->tcp_time_wait_prev == NULL); 4167 ASSERT(tcp->tcp_time_wait_expire == 0); 4168 tcp->tcp_state = TCPS_CLOSED; 4169 4170 /* Release any SSL context */ 4171 if (tcp->tcp_kssl_ent != NULL) { 4172 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4173 tcp->tcp_kssl_ent = NULL; 4174 } 4175 if (tcp->tcp_kssl_ctx != NULL) { 4176 kssl_release_ctx(tcp->tcp_kssl_ctx); 4177 tcp->tcp_kssl_ctx = NULL; 4178 } 4179 tcp->tcp_kssl_pending = B_FALSE; 4180 4181 tcp_ipsec_cleanup(tcp); 4182 } 4183 4184 /* 4185 * tcp is dying (called from ipcl_conn_destroy and error cases). 4186 * Free the tcp_t in either case. 4187 */ 4188 void 4189 tcp_free(tcp_t *tcp) 4190 { 4191 mblk_t *mp; 4192 ip6_pkt_t *ipp; 4193 4194 ASSERT(tcp != NULL); 4195 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4196 4197 tcp->tcp_rq = NULL; 4198 tcp->tcp_wq = NULL; 4199 4200 tcp_close_mpp(&tcp->tcp_xmit_head); 4201 tcp_close_mpp(&tcp->tcp_reass_head); 4202 if (tcp->tcp_rcv_list != NULL) { 4203 /* Free b_next chain */ 4204 tcp_close_mpp(&tcp->tcp_rcv_list); 4205 } 4206 if ((mp = tcp->tcp_urp_mp) != NULL) { 4207 freemsg(mp); 4208 } 4209 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4210 freemsg(mp); 4211 } 4212 4213 if (tcp->tcp_fused_sigurg_mp != NULL) { 4214 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4215 freeb(tcp->tcp_fused_sigurg_mp); 4216 tcp->tcp_fused_sigurg_mp = NULL; 4217 } 4218 4219 if (tcp->tcp_ordrel_mp != NULL) { 4220 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4221 freeb(tcp->tcp_ordrel_mp); 4222 tcp->tcp_ordrel_mp = NULL; 4223 } 4224 4225 if (tcp->tcp_sack_info != NULL) { 4226 if (tcp->tcp_notsack_list != NULL) { 4227 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4228 } 4229 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4230 } 4231 4232 if (tcp->tcp_hopopts != NULL) { 4233 mi_free(tcp->tcp_hopopts); 4234 tcp->tcp_hopopts = NULL; 4235 tcp->tcp_hopoptslen = 0; 4236 } 4237 ASSERT(tcp->tcp_hopoptslen == 0); 4238 if (tcp->tcp_dstopts != NULL) { 4239 mi_free(tcp->tcp_dstopts); 4240 tcp->tcp_dstopts = NULL; 4241 tcp->tcp_dstoptslen = 0; 4242 } 4243 ASSERT(tcp->tcp_dstoptslen == 0); 4244 if (tcp->tcp_rtdstopts != NULL) { 4245 mi_free(tcp->tcp_rtdstopts); 4246 tcp->tcp_rtdstopts = NULL; 4247 tcp->tcp_rtdstoptslen = 0; 4248 } 4249 ASSERT(tcp->tcp_rtdstoptslen == 0); 4250 if (tcp->tcp_rthdr != NULL) { 4251 mi_free(tcp->tcp_rthdr); 4252 tcp->tcp_rthdr = NULL; 4253 tcp->tcp_rthdrlen = 0; 4254 } 4255 ASSERT(tcp->tcp_rthdrlen == 0); 4256 4257 ipp = &tcp->tcp_sticky_ipp; 4258 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4259 IPPF_RTHDR)) 4260 ip6_pkt_free(ipp); 4261 4262 /* 4263 * Free memory associated with the tcp/ip header template. 4264 */ 4265 4266 if (tcp->tcp_iphc != NULL) 4267 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4268 4269 /* 4270 * Following is really a blowing away a union. 4271 * It happens to have exactly two members of identical size 4272 * the following code is enough. 4273 */ 4274 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4275 } 4276 4277 4278 /* 4279 * Put a connection confirmation message upstream built from the 4280 * address information within 'iph' and 'tcph'. Report our success or failure. 4281 */ 4282 static boolean_t 4283 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4284 mblk_t **defermp) 4285 { 4286 sin_t sin; 4287 sin6_t sin6; 4288 mblk_t *mp; 4289 char *optp = NULL; 4290 int optlen = 0; 4291 4292 if (defermp != NULL) 4293 *defermp = NULL; 4294 4295 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4296 /* 4297 * Return in T_CONN_CON results of option negotiation through 4298 * the T_CONN_REQ. Note: If there is an real end-to-end option 4299 * negotiation, then what is received from remote end needs 4300 * to be taken into account but there is no such thing (yet?) 4301 * in our TCP/IP. 4302 * Note: We do not use mi_offset_param() here as 4303 * tcp_opts_conn_req contents do not directly come from 4304 * an application and are either generated in kernel or 4305 * from user input that was already verified. 4306 */ 4307 mp = tcp->tcp_conn.tcp_opts_conn_req; 4308 optp = (char *)(mp->b_rptr + 4309 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4310 optlen = (int) 4311 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4312 } 4313 4314 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4315 ipha_t *ipha = (ipha_t *)iphdr; 4316 4317 /* packet is IPv4 */ 4318 if (tcp->tcp_family == AF_INET) { 4319 sin = sin_null; 4320 sin.sin_addr.s_addr = ipha->ipha_src; 4321 sin.sin_port = *(uint16_t *)tcph->th_lport; 4322 sin.sin_family = AF_INET; 4323 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4324 (int)sizeof (sin_t), optp, optlen); 4325 } else { 4326 sin6 = sin6_null; 4327 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4328 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4329 sin6.sin6_family = AF_INET6; 4330 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4331 (int)sizeof (sin6_t), optp, optlen); 4332 4333 } 4334 } else { 4335 ip6_t *ip6h = (ip6_t *)iphdr; 4336 4337 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4338 ASSERT(tcp->tcp_family == AF_INET6); 4339 sin6 = sin6_null; 4340 sin6.sin6_addr = ip6h->ip6_src; 4341 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4342 sin6.sin6_family = AF_INET6; 4343 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4344 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4345 (int)sizeof (sin6_t), optp, optlen); 4346 } 4347 4348 if (!mp) 4349 return (B_FALSE); 4350 4351 mblk_copycred(mp, idmp); 4352 4353 if (defermp == NULL) { 4354 conn_t *connp = tcp->tcp_connp; 4355 if (IPCL_IS_NONSTR(connp)) { 4356 cred_t *cr; 4357 pid_t cpid; 4358 4359 cr = msg_getcred(mp, &cpid); 4360 (*connp->conn_upcalls->su_connected) 4361 (connp->conn_upper_handle, tcp->tcp_connid, cr, 4362 cpid); 4363 freemsg(mp); 4364 } else { 4365 putnext(tcp->tcp_rq, mp); 4366 } 4367 } else { 4368 *defermp = mp; 4369 } 4370 4371 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4372 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4373 return (B_TRUE); 4374 } 4375 4376 /* 4377 * Defense for the SYN attack - 4378 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4379 * one from the list of droppable eagers. This list is a subset of q0. 4380 * see comments before the definition of MAKE_DROPPABLE(). 4381 * 2. Don't drop a SYN request before its first timeout. This gives every 4382 * request at least til the first timeout to complete its 3-way handshake. 4383 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4384 * requests currently on the queue that has timed out. This will be used 4385 * as an indicator of whether an attack is under way, so that appropriate 4386 * actions can be taken. (It's incremented in tcp_timer() and decremented 4387 * either when eager goes into ESTABLISHED, or gets freed up.) 4388 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4389 * # of timeout drops back to <= q0len/32 => SYN alert off 4390 */ 4391 static boolean_t 4392 tcp_drop_q0(tcp_t *tcp) 4393 { 4394 tcp_t *eager; 4395 mblk_t *mp; 4396 tcp_stack_t *tcps = tcp->tcp_tcps; 4397 4398 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4399 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4400 4401 /* Pick oldest eager from the list of droppable eagers */ 4402 eager = tcp->tcp_eager_prev_drop_q0; 4403 4404 /* If list is empty. return B_FALSE */ 4405 if (eager == tcp) { 4406 return (B_FALSE); 4407 } 4408 4409 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4410 if ((mp = allocb(0, BPRI_HI)) == NULL) 4411 return (B_FALSE); 4412 4413 /* 4414 * Take this eager out from the list of droppable eagers since we are 4415 * going to drop it. 4416 */ 4417 MAKE_UNDROPPABLE(eager); 4418 4419 if (tcp->tcp_debug) { 4420 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4421 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4422 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4423 tcp->tcp_conn_req_cnt_q0, 4424 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4425 } 4426 4427 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4428 4429 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4430 CONN_INC_REF(eager->tcp_connp); 4431 4432 /* Mark the IRE created for this SYN request temporary */ 4433 tcp_ip_ire_mark_advice(eager); 4434 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 4435 tcp_clean_death_wrapper, eager->tcp_connp, 4436 SQ_FILL, SQTAG_TCP_DROP_Q0); 4437 4438 return (B_TRUE); 4439 } 4440 4441 int 4442 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4443 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4444 { 4445 tcp_t *ltcp = lconnp->conn_tcp; 4446 tcp_t *tcp = connp->conn_tcp; 4447 mblk_t *tpi_mp; 4448 ipha_t *ipha; 4449 ip6_t *ip6h; 4450 sin6_t sin6; 4451 in6_addr_t v6dst; 4452 int err; 4453 int ifindex = 0; 4454 tcp_stack_t *tcps = tcp->tcp_tcps; 4455 4456 if (ipvers == IPV4_VERSION) { 4457 ipha = (ipha_t *)mp->b_rptr; 4458 4459 connp->conn_send = ip_output; 4460 connp->conn_recv = tcp_input; 4461 4462 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4463 &connp->conn_bound_source_v6); 4464 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4465 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4466 4467 sin6 = sin6_null; 4468 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4469 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4470 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4471 sin6.sin6_family = AF_INET6; 4472 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4473 lconnp->conn_zoneid, tcps->tcps_netstack); 4474 if (tcp->tcp_recvdstaddr) { 4475 sin6_t sin6d; 4476 4477 sin6d = sin6_null; 4478 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4479 &sin6d.sin6_addr); 4480 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4481 sin6d.sin6_family = AF_INET; 4482 tpi_mp = mi_tpi_extconn_ind(NULL, 4483 (char *)&sin6d, sizeof (sin6_t), 4484 (char *)&tcp, 4485 (t_scalar_t)sizeof (intptr_t), 4486 (char *)&sin6d, sizeof (sin6_t), 4487 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4488 } else { 4489 tpi_mp = mi_tpi_conn_ind(NULL, 4490 (char *)&sin6, sizeof (sin6_t), 4491 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4492 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4493 } 4494 } else { 4495 ip6h = (ip6_t *)mp->b_rptr; 4496 4497 connp->conn_send = ip_output_v6; 4498 connp->conn_recv = tcp_input; 4499 4500 connp->conn_bound_source_v6 = ip6h->ip6_dst; 4501 connp->conn_srcv6 = ip6h->ip6_dst; 4502 connp->conn_remv6 = ip6h->ip6_src; 4503 4504 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4505 ifindex = (int)DB_CKSUMSTUFF(mp); 4506 DB_CKSUMSTUFF(mp) = 0; 4507 4508 sin6 = sin6_null; 4509 sin6.sin6_addr = ip6h->ip6_src; 4510 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4511 sin6.sin6_family = AF_INET6; 4512 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4513 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4514 lconnp->conn_zoneid, tcps->tcps_netstack); 4515 4516 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4517 /* Pass up the scope_id of remote addr */ 4518 sin6.sin6_scope_id = ifindex; 4519 } else { 4520 sin6.sin6_scope_id = 0; 4521 } 4522 if (tcp->tcp_recvdstaddr) { 4523 sin6_t sin6d; 4524 4525 sin6d = sin6_null; 4526 sin6.sin6_addr = ip6h->ip6_dst; 4527 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4528 sin6d.sin6_family = AF_INET; 4529 tpi_mp = mi_tpi_extconn_ind(NULL, 4530 (char *)&sin6d, sizeof (sin6_t), 4531 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4532 (char *)&sin6d, sizeof (sin6_t), 4533 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4534 } else { 4535 tpi_mp = mi_tpi_conn_ind(NULL, 4536 (char *)&sin6, sizeof (sin6_t), 4537 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4538 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4539 } 4540 } 4541 4542 if (tpi_mp == NULL) 4543 return (ENOMEM); 4544 4545 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4546 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4547 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4548 connp->conn_fully_bound = B_FALSE; 4549 4550 /* Inherit information from the "parent" */ 4551 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4552 tcp->tcp_family = ltcp->tcp_family; 4553 4554 tcp->tcp_wq = ltcp->tcp_wq; 4555 tcp->tcp_rq = ltcp->tcp_rq; 4556 4557 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4558 tcp->tcp_detached = B_TRUE; 4559 SOCK_CONNID_INIT(tcp->tcp_connid); 4560 if ((err = tcp_init_values(tcp)) != 0) { 4561 freemsg(tpi_mp); 4562 return (err); 4563 } 4564 4565 if (ipvers == IPV4_VERSION) { 4566 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4567 freemsg(tpi_mp); 4568 return (err); 4569 } 4570 ASSERT(tcp->tcp_ipha != NULL); 4571 } else { 4572 /* ifindex must be already set */ 4573 ASSERT(ifindex != 0); 4574 4575 if (ltcp->tcp_bound_if != 0) 4576 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4577 else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 4578 tcp->tcp_bound_if = ifindex; 4579 4580 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4581 tcp->tcp_recvifindex = 0; 4582 tcp->tcp_recvhops = 0xffffffffU; 4583 ASSERT(tcp->tcp_ip6h != NULL); 4584 } 4585 4586 tcp->tcp_lport = ltcp->tcp_lport; 4587 4588 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4589 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4590 /* 4591 * Listener had options of some sort; eager inherits. 4592 * Free up the eager template and allocate one 4593 * of the right size. 4594 */ 4595 if (tcp->tcp_hdr_grown) { 4596 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4597 } else { 4598 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4599 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4600 } 4601 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4602 KM_NOSLEEP); 4603 if (tcp->tcp_iphc == NULL) { 4604 tcp->tcp_iphc_len = 0; 4605 freemsg(tpi_mp); 4606 return (ENOMEM); 4607 } 4608 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4609 tcp->tcp_hdr_grown = B_TRUE; 4610 } 4611 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4612 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4613 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4614 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4615 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4616 4617 /* 4618 * Copy the IP+TCP header template from listener to eager 4619 */ 4620 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4621 if (tcp->tcp_ipversion == IPV6_VERSION) { 4622 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4623 IPPROTO_RAW) { 4624 tcp->tcp_ip6h = 4625 (ip6_t *)(tcp->tcp_iphc + 4626 sizeof (ip6i_t)); 4627 } else { 4628 tcp->tcp_ip6h = 4629 (ip6_t *)(tcp->tcp_iphc); 4630 } 4631 tcp->tcp_ipha = NULL; 4632 } else { 4633 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4634 tcp->tcp_ip6h = NULL; 4635 } 4636 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4637 tcp->tcp_ip_hdr_len); 4638 } else { 4639 /* 4640 * only valid case when ipversion of listener and 4641 * eager differ is when listener is IPv6 and 4642 * eager is IPv4. 4643 * Eager header template has been initialized to the 4644 * maximum v4 header sizes, which includes space for 4645 * TCP and IP options. 4646 */ 4647 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4648 (tcp->tcp_ipversion == IPV4_VERSION)); 4649 ASSERT(tcp->tcp_iphc_len >= 4650 TCP_MAX_COMBINED_HEADER_LENGTH); 4651 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4652 /* copy IP header fields individually */ 4653 tcp->tcp_ipha->ipha_ttl = 4654 ltcp->tcp_ip6h->ip6_hops; 4655 bcopy(ltcp->tcp_tcph->th_lport, 4656 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4657 } 4658 4659 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4660 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4661 sizeof (in_port_t)); 4662 4663 if (ltcp->tcp_lport == 0) { 4664 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4665 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4666 sizeof (in_port_t)); 4667 } 4668 4669 if (tcp->tcp_ipversion == IPV4_VERSION) { 4670 ASSERT(ipha != NULL); 4671 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4672 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4673 4674 /* Source routing option copyover (reverse it) */ 4675 if (tcps->tcps_rev_src_routes) 4676 tcp_opt_reverse(tcp, ipha); 4677 } else { 4678 ASSERT(ip6h != NULL); 4679 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4680 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4681 } 4682 4683 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4684 ASSERT(!tcp->tcp_tconnind_started); 4685 /* 4686 * If the SYN contains a credential, it's a loopback packet; attach 4687 * the credential to the TPI message. 4688 */ 4689 mblk_copycred(tpi_mp, idmp); 4690 4691 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4692 4693 /* Inherit the listener's SSL protection state */ 4694 4695 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4696 kssl_hold_ent(tcp->tcp_kssl_ent); 4697 tcp->tcp_kssl_pending = B_TRUE; 4698 } 4699 4700 /* Inherit the listener's non-STREAMS flag */ 4701 if (IPCL_IS_NONSTR(lconnp)) { 4702 connp->conn_flags |= IPCL_NONSTR; 4703 } 4704 4705 return (0); 4706 } 4707 4708 4709 int 4710 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4711 tcph_t *tcph, mblk_t *idmp) 4712 { 4713 tcp_t *ltcp = lconnp->conn_tcp; 4714 tcp_t *tcp = connp->conn_tcp; 4715 sin_t sin; 4716 mblk_t *tpi_mp = NULL; 4717 int err; 4718 tcp_stack_t *tcps = tcp->tcp_tcps; 4719 4720 sin = sin_null; 4721 sin.sin_addr.s_addr = ipha->ipha_src; 4722 sin.sin_port = *(uint16_t *)tcph->th_lport; 4723 sin.sin_family = AF_INET; 4724 if (ltcp->tcp_recvdstaddr) { 4725 sin_t sind; 4726 4727 sind = sin_null; 4728 sind.sin_addr.s_addr = ipha->ipha_dst; 4729 sind.sin_port = *(uint16_t *)tcph->th_fport; 4730 sind.sin_family = AF_INET; 4731 tpi_mp = mi_tpi_extconn_ind(NULL, 4732 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4733 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4734 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4735 } else { 4736 tpi_mp = mi_tpi_conn_ind(NULL, 4737 (char *)&sin, sizeof (sin_t), 4738 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4739 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4740 } 4741 4742 if (tpi_mp == NULL) { 4743 return (ENOMEM); 4744 } 4745 4746 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4747 connp->conn_send = ip_output; 4748 connp->conn_recv = tcp_input; 4749 connp->conn_fully_bound = B_FALSE; 4750 4751 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_bound_source_v6); 4752 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4753 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4754 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4755 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4756 4757 /* Inherit information from the "parent" */ 4758 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4759 tcp->tcp_family = ltcp->tcp_family; 4760 tcp->tcp_wq = ltcp->tcp_wq; 4761 tcp->tcp_rq = ltcp->tcp_rq; 4762 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 4763 tcp->tcp_detached = B_TRUE; 4764 SOCK_CONNID_INIT(tcp->tcp_connid); 4765 if ((err = tcp_init_values(tcp)) != 0) { 4766 freemsg(tpi_mp); 4767 return (err); 4768 } 4769 4770 /* 4771 * Let's make sure that eager tcp template has enough space to 4772 * copy IPv4 listener's tcp template. Since the conn_t structure is 4773 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4774 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4775 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4776 * extension headers or with ip6i_t struct). Note that bcopy() below 4777 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4778 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4779 */ 4780 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 4781 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 4782 4783 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4784 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4785 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4786 tcp->tcp_ttl = ltcp->tcp_ttl; 4787 tcp->tcp_tos = ltcp->tcp_tos; 4788 4789 /* Copy the IP+TCP header template from listener to eager */ 4790 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4791 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4792 tcp->tcp_ip6h = NULL; 4793 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4794 tcp->tcp_ip_hdr_len); 4795 4796 /* Initialize the IP addresses and Ports */ 4797 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4798 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4799 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4800 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 4801 4802 /* Source routing option copyover (reverse it) */ 4803 if (tcps->tcps_rev_src_routes) 4804 tcp_opt_reverse(tcp, ipha); 4805 4806 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4807 ASSERT(!tcp->tcp_tconnind_started); 4808 4809 /* 4810 * If the SYN contains a credential, it's a loopback packet; attach 4811 * the credential to the TPI message. 4812 */ 4813 mblk_copycred(tpi_mp, idmp); 4814 4815 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4816 4817 /* Inherit the listener's SSL protection state */ 4818 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4819 kssl_hold_ent(tcp->tcp_kssl_ent); 4820 tcp->tcp_kssl_pending = B_TRUE; 4821 } 4822 4823 /* Inherit the listener's non-STREAMS flag */ 4824 if (IPCL_IS_NONSTR(lconnp)) { 4825 connp->conn_flags |= IPCL_NONSTR; 4826 } 4827 4828 return (0); 4829 } 4830 4831 /* 4832 * sets up conn for ipsec. 4833 * if the first mblk is M_CTL it is consumed and mpp is updated. 4834 * in case of error mpp is freed. 4835 */ 4836 conn_t * 4837 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 4838 { 4839 conn_t *connp = tcp->tcp_connp; 4840 conn_t *econnp; 4841 squeue_t *new_sqp; 4842 mblk_t *first_mp = *mpp; 4843 mblk_t *mp = *mpp; 4844 boolean_t mctl_present = B_FALSE; 4845 uint_t ipvers; 4846 4847 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 4848 if (econnp == NULL) { 4849 freemsg(first_mp); 4850 return (NULL); 4851 } 4852 if (DB_TYPE(mp) == M_CTL) { 4853 if (mp->b_cont == NULL || 4854 mp->b_cont->b_datap->db_type != M_DATA) { 4855 freemsg(first_mp); 4856 return (NULL); 4857 } 4858 mp = mp->b_cont; 4859 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4860 freemsg(first_mp); 4861 return (NULL); 4862 } 4863 4864 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4865 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4866 mctl_present = B_TRUE; 4867 } else { 4868 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4869 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4870 } 4871 4872 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 4873 DB_CKSUMSTART(mp) = 0; 4874 4875 ASSERT(OK_32PTR(mp->b_rptr)); 4876 ipvers = IPH_HDR_VERSION(mp->b_rptr); 4877 if (ipvers == IPV4_VERSION) { 4878 uint16_t *up; 4879 uint32_t ports; 4880 ipha_t *ipha; 4881 4882 ipha = (ipha_t *)mp->b_rptr; 4883 up = (uint16_t *)((uchar_t *)ipha + 4884 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 4885 ports = *(uint32_t *)up; 4886 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 4887 ipha->ipha_dst, ipha->ipha_src, ports); 4888 } else { 4889 uint16_t *up; 4890 uint32_t ports; 4891 uint16_t ip_hdr_len; 4892 uint8_t *nexthdrp; 4893 ip6_t *ip6h; 4894 tcph_t *tcph; 4895 4896 ip6h = (ip6_t *)mp->b_rptr; 4897 if (ip6h->ip6_nxt == IPPROTO_TCP) { 4898 ip_hdr_len = IPV6_HDR_LEN; 4899 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 4900 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 4901 CONN_DEC_REF(econnp); 4902 freemsg(first_mp); 4903 return (NULL); 4904 } 4905 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 4906 up = (uint16_t *)tcph->th_lport; 4907 ports = *(uint32_t *)up; 4908 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 4909 ip6h->ip6_dst, ip6h->ip6_src, ports); 4910 } 4911 4912 /* 4913 * The caller already ensured that there is a sqp present. 4914 */ 4915 econnp->conn_sqp = new_sqp; 4916 econnp->conn_initial_sqp = new_sqp; 4917 4918 if (connp->conn_policy != NULL) { 4919 ipsec_in_t *ii; 4920 ii = (ipsec_in_t *)(first_mp->b_rptr); 4921 ASSERT(ii->ipsec_in_policy == NULL); 4922 IPPH_REFHOLD(connp->conn_policy); 4923 ii->ipsec_in_policy = connp->conn_policy; 4924 4925 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 4926 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 4927 CONN_DEC_REF(econnp); 4928 freemsg(first_mp); 4929 return (NULL); 4930 } 4931 } 4932 4933 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 4934 CONN_DEC_REF(econnp); 4935 freemsg(first_mp); 4936 return (NULL); 4937 } 4938 4939 /* 4940 * If we know we have some policy, pass the "IPSEC" 4941 * options size TCP uses this adjust the MSS. 4942 */ 4943 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 4944 if (mctl_present) { 4945 freeb(first_mp); 4946 *mpp = mp; 4947 } 4948 4949 return (econnp); 4950 } 4951 4952 /* 4953 * tcp_get_conn/tcp_free_conn 4954 * 4955 * tcp_get_conn is used to get a clean tcp connection structure. 4956 * It tries to reuse the connections put on the freelist by the 4957 * time_wait_collector failing which it goes to kmem_cache. This 4958 * way has two benefits compared to just allocating from and 4959 * freeing to kmem_cache. 4960 * 1) The time_wait_collector can free (which includes the cleanup) 4961 * outside the squeue. So when the interrupt comes, we have a clean 4962 * connection sitting in the freelist. Obviously, this buys us 4963 * performance. 4964 * 4965 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 4966 * has multiple disadvantages - tying up the squeue during alloc, and the 4967 * fact that IPSec policy initialization has to happen here which 4968 * requires us sending a M_CTL and checking for it i.e. real ugliness. 4969 * But allocating the conn/tcp in IP land is also not the best since 4970 * we can't check the 'q' and 'q0' which are protected by squeue and 4971 * blindly allocate memory which might have to be freed here if we are 4972 * not allowed to accept the connection. By using the freelist and 4973 * putting the conn/tcp back in freelist, we don't pay a penalty for 4974 * allocating memory without checking 'q/q0' and freeing it if we can't 4975 * accept the connection. 4976 * 4977 * Care should be taken to put the conn back in the same squeue's freelist 4978 * from which it was allocated. Best results are obtained if conn is 4979 * allocated from listener's squeue and freed to the same. Time wait 4980 * collector will free up the freelist is the connection ends up sitting 4981 * there for too long. 4982 */ 4983 void * 4984 tcp_get_conn(void *arg, tcp_stack_t *tcps) 4985 { 4986 tcp_t *tcp = NULL; 4987 conn_t *connp = NULL; 4988 squeue_t *sqp = (squeue_t *)arg; 4989 tcp_squeue_priv_t *tcp_time_wait; 4990 netstack_t *ns; 4991 mblk_t *tcp_rsrv_mp = NULL; 4992 4993 tcp_time_wait = 4994 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 4995 4996 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 4997 tcp = tcp_time_wait->tcp_free_list; 4998 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 4999 if (tcp != NULL) { 5000 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5001 tcp_time_wait->tcp_free_list_cnt--; 5002 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5003 tcp->tcp_time_wait_next = NULL; 5004 connp = tcp->tcp_connp; 5005 connp->conn_flags |= IPCL_REUSED; 5006 5007 ASSERT(tcp->tcp_tcps == NULL); 5008 ASSERT(connp->conn_netstack == NULL); 5009 ASSERT(tcp->tcp_rsrv_mp != NULL); 5010 ns = tcps->tcps_netstack; 5011 netstack_hold(ns); 5012 connp->conn_netstack = ns; 5013 tcp->tcp_tcps = tcps; 5014 TCPS_REFHOLD(tcps); 5015 ipcl_globalhash_insert(connp); 5016 return ((void *)connp); 5017 } 5018 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5019 /* 5020 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed until 5021 * this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5022 */ 5023 tcp_rsrv_mp = allocb(0, BPRI_HI); 5024 if (tcp_rsrv_mp == NULL) 5025 return (NULL); 5026 5027 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5028 tcps->tcps_netstack)) == NULL) { 5029 freeb(tcp_rsrv_mp); 5030 return (NULL); 5031 } 5032 5033 tcp = connp->conn_tcp; 5034 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 5035 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5036 5037 tcp->tcp_tcps = tcps; 5038 TCPS_REFHOLD(tcps); 5039 5040 return ((void *)connp); 5041 } 5042 5043 /* 5044 * Update the cached label for the given tcp_t. This should be called once per 5045 * connection, and before any packets are sent or tcp_process_options is 5046 * invoked. Returns B_FALSE if the correct label could not be constructed. 5047 */ 5048 static boolean_t 5049 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5050 { 5051 conn_t *connp = tcp->tcp_connp; 5052 5053 if (tcp->tcp_ipversion == IPV4_VERSION) { 5054 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5055 int added; 5056 5057 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5058 connp->conn_mac_exempt, 5059 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5060 return (B_FALSE); 5061 5062 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5063 if (added == -1) 5064 return (B_FALSE); 5065 tcp->tcp_hdr_len += added; 5066 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5067 tcp->tcp_ip_hdr_len += added; 5068 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5069 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5070 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5071 tcp->tcp_hdr_len); 5072 if (added == -1) 5073 return (B_FALSE); 5074 tcp->tcp_hdr_len += added; 5075 tcp->tcp_tcph = (tcph_t *) 5076 ((uchar_t *)tcp->tcp_tcph + added); 5077 tcp->tcp_ip_hdr_len += added; 5078 } 5079 } else { 5080 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5081 5082 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5083 connp->conn_mac_exempt, 5084 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5085 return (B_FALSE); 5086 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5087 &tcp->tcp_label_len, optbuf) != 0) 5088 return (B_FALSE); 5089 if (tcp_build_hdrs(tcp) != 0) 5090 return (B_FALSE); 5091 } 5092 5093 connp->conn_ulp_labeled = 1; 5094 5095 return (B_TRUE); 5096 } 5097 5098 /* BEGIN CSTYLED */ 5099 /* 5100 * 5101 * The sockfs ACCEPT path: 5102 * ======================= 5103 * 5104 * The eager is now established in its own perimeter as soon as SYN is 5105 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5106 * completes the accept processing on the acceptor STREAM. The sending 5107 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5108 * listener but a TLI/XTI listener completes the accept processing 5109 * on the listener perimeter. 5110 * 5111 * Common control flow for 3 way handshake: 5112 * ---------------------------------------- 5113 * 5114 * incoming SYN (listener perimeter) -> tcp_rput_data() 5115 * -> tcp_conn_request() 5116 * 5117 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5118 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5119 * 5120 * Sockfs ACCEPT Path: 5121 * ------------------- 5122 * 5123 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5124 * as STREAM entry point) 5125 * 5126 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5127 * 5128 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5129 * association (we are not behind eager's squeue but sockfs is protecting us 5130 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5131 * is changed to point at tcp_wput(). 5132 * 5133 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5134 * listener (done on listener's perimeter). 5135 * 5136 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5137 * accept. 5138 * 5139 * TLI/XTI client ACCEPT path: 5140 * --------------------------- 5141 * 5142 * soaccept() sends T_CONN_RES on the listener STREAM. 5143 * 5144 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5145 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5146 * 5147 * Locks: 5148 * ====== 5149 * 5150 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5151 * and listeners->tcp_eager_next_q. 5152 * 5153 * Referencing: 5154 * ============ 5155 * 5156 * 1) We start out in tcp_conn_request by eager placing a ref on 5157 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5158 * 5159 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5160 * doing so we place a ref on the eager. This ref is finally dropped at the 5161 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5162 * reference is dropped by the squeue framework. 5163 * 5164 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5165 * 5166 * The reference must be released by the same entity that added the reference 5167 * In the above scheme, the eager is the entity that adds and releases the 5168 * references. Note that tcp_accept_finish executes in the squeue of the eager 5169 * (albeit after it is attached to the acceptor stream). Though 1. executes 5170 * in the listener's squeue, the eager is nascent at this point and the 5171 * reference can be considered to have been added on behalf of the eager. 5172 * 5173 * Eager getting a Reset or listener closing: 5174 * ========================================== 5175 * 5176 * Once the listener and eager are linked, the listener never does the unlink. 5177 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5178 * a message on all eager perimeter. The eager then does the unlink, clears 5179 * any pointers to the listener's queue and drops the reference to the 5180 * listener. The listener waits in tcp_close outside the squeue until its 5181 * refcount has dropped to 1. This ensures that the listener has waited for 5182 * all eagers to clear their association with the listener. 5183 * 5184 * Similarly, if eager decides to go away, it can unlink itself and close. 5185 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5186 * the reference to eager is still valid because of the extra ref we put 5187 * in tcp_send_conn_ind. 5188 * 5189 * Listener can always locate the eager under the protection 5190 * of the listener->tcp_eager_lock, and then do a refhold 5191 * on the eager during the accept processing. 5192 * 5193 * The acceptor stream accesses the eager in the accept processing 5194 * based on the ref placed on eager before sending T_conn_ind. 5195 * The only entity that can negate this refhold is a listener close 5196 * which is mutually exclusive with an active acceptor stream. 5197 * 5198 * Eager's reference on the listener 5199 * =================================== 5200 * 5201 * If the accept happens (even on a closed eager) the eager drops its 5202 * reference on the listener at the start of tcp_accept_finish. If the 5203 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5204 * the reference is dropped in tcp_closei_local. If the listener closes, 5205 * the reference is dropped in tcp_eager_kill. In all cases the reference 5206 * is dropped while executing in the eager's context (squeue). 5207 */ 5208 /* END CSTYLED */ 5209 5210 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5211 5212 /* 5213 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5214 * tcp_rput_data will not see any SYN packets. 5215 */ 5216 /* ARGSUSED */ 5217 void 5218 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5219 { 5220 tcph_t *tcph; 5221 uint32_t seg_seq; 5222 tcp_t *eager; 5223 uint_t ipvers; 5224 ipha_t *ipha; 5225 ip6_t *ip6h; 5226 int err; 5227 conn_t *econnp = NULL; 5228 squeue_t *new_sqp; 5229 mblk_t *mp1; 5230 uint_t ip_hdr_len; 5231 conn_t *connp = (conn_t *)arg; 5232 tcp_t *tcp = connp->conn_tcp; 5233 cred_t *credp; 5234 tcp_stack_t *tcps = tcp->tcp_tcps; 5235 ip_stack_t *ipst; 5236 5237 if (tcp->tcp_state != TCPS_LISTEN) 5238 goto error2; 5239 5240 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5241 5242 mutex_enter(&tcp->tcp_eager_lock); 5243 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5244 mutex_exit(&tcp->tcp_eager_lock); 5245 TCP_STAT(tcps, tcp_listendrop); 5246 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5247 if (tcp->tcp_debug) { 5248 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5249 "tcp_conn_request: listen backlog (max=%d) " 5250 "overflow (%d pending) on %s", 5251 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5252 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5253 } 5254 goto error2; 5255 } 5256 5257 if (tcp->tcp_conn_req_cnt_q0 >= 5258 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5259 /* 5260 * Q0 is full. Drop a pending half-open req from the queue 5261 * to make room for the new SYN req. Also mark the time we 5262 * drop a SYN. 5263 * 5264 * A more aggressive defense against SYN attack will 5265 * be to set the "tcp_syn_defense" flag now. 5266 */ 5267 TCP_STAT(tcps, tcp_listendropq0); 5268 tcp->tcp_last_rcv_lbolt = lbolt64; 5269 if (!tcp_drop_q0(tcp)) { 5270 mutex_exit(&tcp->tcp_eager_lock); 5271 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5272 if (tcp->tcp_debug) { 5273 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5274 "tcp_conn_request: listen half-open queue " 5275 "(max=%d) full (%d pending) on %s", 5276 tcps->tcps_conn_req_max_q0, 5277 tcp->tcp_conn_req_cnt_q0, 5278 tcp_display(tcp, NULL, 5279 DISP_PORT_ONLY)); 5280 } 5281 goto error2; 5282 } 5283 } 5284 mutex_exit(&tcp->tcp_eager_lock); 5285 5286 /* 5287 * IP adds STRUIO_EAGER and ensures that the received packet is 5288 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5289 * link local address. If IPSec is enabled, db_struioflag has 5290 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5291 * otherwise an error case if neither of them is set. 5292 */ 5293 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5294 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5295 DB_CKSUMSTART(mp) = 0; 5296 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5297 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5298 if (econnp == NULL) 5299 goto error2; 5300 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5301 econnp->conn_sqp = new_sqp; 5302 econnp->conn_initial_sqp = new_sqp; 5303 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5304 /* 5305 * mp is updated in tcp_get_ipsec_conn(). 5306 */ 5307 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5308 if (econnp == NULL) { 5309 /* 5310 * mp freed by tcp_get_ipsec_conn. 5311 */ 5312 return; 5313 } 5314 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5315 } else { 5316 goto error2; 5317 } 5318 5319 ASSERT(DB_TYPE(mp) == M_DATA); 5320 5321 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5322 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5323 ASSERT(OK_32PTR(mp->b_rptr)); 5324 if (ipvers == IPV4_VERSION) { 5325 ipha = (ipha_t *)mp->b_rptr; 5326 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5327 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5328 } else { 5329 ip6h = (ip6_t *)mp->b_rptr; 5330 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5331 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5332 } 5333 5334 if (tcp->tcp_family == AF_INET) { 5335 ASSERT(ipvers == IPV4_VERSION); 5336 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5337 } else { 5338 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5339 } 5340 5341 if (err) 5342 goto error3; 5343 5344 eager = econnp->conn_tcp; 5345 ASSERT(eager->tcp_ordrel_mp == NULL); 5346 5347 if (!IPCL_IS_NONSTR(econnp)) { 5348 /* 5349 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that 5350 * at close time, we will always have that to send up. 5351 * Otherwise, we need to do special handling in case the 5352 * allocation fails at that time. 5353 */ 5354 if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5355 goto error3; 5356 } 5357 /* Inherit various TCP parameters from the listener */ 5358 eager->tcp_naglim = tcp->tcp_naglim; 5359 eager->tcp_first_timer_threshold = tcp->tcp_first_timer_threshold; 5360 eager->tcp_second_timer_threshold = tcp->tcp_second_timer_threshold; 5361 5362 eager->tcp_first_ctimer_threshold = tcp->tcp_first_ctimer_threshold; 5363 eager->tcp_second_ctimer_threshold = tcp->tcp_second_ctimer_threshold; 5364 5365 /* 5366 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5367 * If it does not, the eager's receive window will be set to the 5368 * listener's receive window later in this function. 5369 */ 5370 eager->tcp_rwnd = 0; 5371 5372 /* 5373 * Inherit listener's tcp_init_cwnd. Need to do this before 5374 * calling tcp_process_options() where tcp_mss_set() is called 5375 * to set the initial cwnd. 5376 */ 5377 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5378 5379 /* 5380 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5381 * zone id before the accept is completed in tcp_wput_accept(). 5382 */ 5383 econnp->conn_zoneid = connp->conn_zoneid; 5384 econnp->conn_allzones = connp->conn_allzones; 5385 5386 /* Copy nexthop information from listener to eager */ 5387 if (connp->conn_nexthop_set) { 5388 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5389 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5390 } 5391 5392 /* 5393 * TSOL: tsol_input_proc() needs the eager's cred before the 5394 * eager is accepted 5395 */ 5396 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5397 crhold(credp); 5398 5399 /* 5400 * If the caller has the process-wide flag set, then default to MAC 5401 * exempt mode. This allows read-down to unlabeled hosts. 5402 */ 5403 if (getpflags(NET_MAC_AWARE, credp) != 0) 5404 econnp->conn_mac_exempt = B_TRUE; 5405 5406 if (is_system_labeled()) { 5407 cred_t *cr; 5408 5409 if (connp->conn_mlp_type != mlptSingle) { 5410 cr = econnp->conn_peercred = msg_getcred(mp, NULL); 5411 if (cr != NULL) 5412 crhold(cr); 5413 else 5414 cr = econnp->conn_cred; 5415 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5416 econnp, cred_t *, cr) 5417 } else { 5418 cr = econnp->conn_cred; 5419 DTRACE_PROBE2(syn_accept, conn_t *, 5420 econnp, cred_t *, cr) 5421 } 5422 5423 if (!tcp_update_label(eager, cr)) { 5424 DTRACE_PROBE3( 5425 tx__ip__log__error__connrequest__tcp, 5426 char *, "eager connp(1) label on SYN mp(2) failed", 5427 conn_t *, econnp, mblk_t *, mp); 5428 goto error3; 5429 } 5430 } 5431 5432 eager->tcp_hard_binding = B_TRUE; 5433 5434 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5435 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5436 5437 CL_INET_CONNECT(connp, eager, B_FALSE, err); 5438 if (err != 0) { 5439 tcp_bind_hash_remove(eager); 5440 goto error3; 5441 } 5442 5443 /* 5444 * No need to check for multicast destination since ip will only pass 5445 * up multicasts to those that have expressed interest 5446 * TODO: what about rejecting broadcasts? 5447 * Also check that source is not a multicast or broadcast address. 5448 */ 5449 eager->tcp_state = TCPS_SYN_RCVD; 5450 5451 5452 /* 5453 * There should be no ire in the mp as we are being called after 5454 * receiving the SYN. 5455 */ 5456 ASSERT(tcp_ire_mp(&mp) == NULL); 5457 5458 /* 5459 * Adapt our mss, ttl, ... according to information provided in IRE. 5460 */ 5461 5462 if (tcp_adapt_ire(eager, NULL) == 0) { 5463 /* Undo the bind_hash_insert */ 5464 tcp_bind_hash_remove(eager); 5465 goto error3; 5466 } 5467 5468 /* Process all TCP options. */ 5469 tcp_process_options(eager, tcph); 5470 5471 /* Is the other end ECN capable? */ 5472 if (tcps->tcps_ecn_permitted >= 1 && 5473 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5474 eager->tcp_ecn_ok = B_TRUE; 5475 } 5476 5477 /* 5478 * listener->tcp_rq->q_hiwat should be the default window size or a 5479 * window size changed via SO_RCVBUF option. First round up the 5480 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5481 * scale option value if needed. Call tcp_rwnd_set() to finish the 5482 * setting. 5483 * 5484 * Note if there is a rpipe metric associated with the remote host, 5485 * we should not inherit receive window size from listener. 5486 */ 5487 eager->tcp_rwnd = MSS_ROUNDUP( 5488 (eager->tcp_rwnd == 0 ? tcp->tcp_recv_hiwater: 5489 eager->tcp_rwnd), eager->tcp_mss); 5490 if (eager->tcp_snd_ws_ok) 5491 tcp_set_ws_value(eager); 5492 /* 5493 * Note that this is the only place tcp_rwnd_set() is called for 5494 * accepting a connection. We need to call it here instead of 5495 * after the 3-way handshake because we need to tell the other 5496 * side our rwnd in the SYN-ACK segment. 5497 */ 5498 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5499 5500 /* 5501 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5502 * via soaccept()->soinheritoptions() which essentially applies 5503 * all the listener options to the new STREAM. The options that we 5504 * need to take care of are: 5505 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5506 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5507 * SO_SNDBUF, SO_RCVBUF. 5508 * 5509 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5510 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5511 * tcp_maxpsz_set() gets called later from 5512 * tcp_accept_finish(), the option takes effect. 5513 * 5514 */ 5515 /* Set the TCP options */ 5516 eager->tcp_recv_hiwater = tcp->tcp_recv_hiwater; 5517 eager->tcp_recv_lowater = tcp->tcp_recv_lowater; 5518 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5519 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5520 eager->tcp_oobinline = tcp->tcp_oobinline; 5521 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5522 eager->tcp_broadcast = tcp->tcp_broadcast; 5523 eager->tcp_useloopback = tcp->tcp_useloopback; 5524 eager->tcp_dontroute = tcp->tcp_dontroute; 5525 eager->tcp_debug = tcp->tcp_debug; 5526 eager->tcp_linger = tcp->tcp_linger; 5527 eager->tcp_lingertime = tcp->tcp_lingertime; 5528 if (tcp->tcp_ka_enabled) 5529 eager->tcp_ka_enabled = 1; 5530 5531 /* Set the IP options */ 5532 econnp->conn_broadcast = connp->conn_broadcast; 5533 econnp->conn_loopback = connp->conn_loopback; 5534 econnp->conn_dontroute = connp->conn_dontroute; 5535 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5536 5537 /* Put a ref on the listener for the eager. */ 5538 CONN_INC_REF(connp); 5539 mutex_enter(&tcp->tcp_eager_lock); 5540 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5541 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5542 tcp->tcp_eager_next_q0 = eager; 5543 eager->tcp_eager_prev_q0 = tcp; 5544 5545 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5546 eager->tcp_listener = tcp; 5547 eager->tcp_saved_listener = tcp; 5548 5549 /* 5550 * Tag this detached tcp vector for later retrieval 5551 * by our listener client in tcp_accept(). 5552 */ 5553 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5554 tcp->tcp_conn_req_cnt_q0++; 5555 if (++tcp->tcp_conn_req_seqnum == -1) { 5556 /* 5557 * -1 is "special" and defined in TPI as something 5558 * that should never be used in T_CONN_IND 5559 */ 5560 ++tcp->tcp_conn_req_seqnum; 5561 } 5562 mutex_exit(&tcp->tcp_eager_lock); 5563 5564 if (tcp->tcp_syn_defense) { 5565 /* Don't drop the SYN that comes from a good IP source */ 5566 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5567 if (addr_cache != NULL && eager->tcp_remote == 5568 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5569 eager->tcp_dontdrop = B_TRUE; 5570 } 5571 } 5572 5573 /* 5574 * We need to insert the eager in its own perimeter but as soon 5575 * as we do that, we expose the eager to the classifier and 5576 * should not touch any field outside the eager's perimeter. 5577 * So do all the work necessary before inserting the eager 5578 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5579 * will succeed but undo everything if it fails. 5580 */ 5581 seg_seq = ABE32_TO_U32(tcph->th_seq); 5582 eager->tcp_irs = seg_seq; 5583 eager->tcp_rack = seg_seq; 5584 eager->tcp_rnxt = seg_seq + 1; 5585 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5586 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5587 eager->tcp_state = TCPS_SYN_RCVD; 5588 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5589 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5590 if (mp1 == NULL) { 5591 /* 5592 * Increment the ref count as we are going to 5593 * enqueueing an mp in squeue 5594 */ 5595 CONN_INC_REF(econnp); 5596 goto error; 5597 } 5598 5599 /* 5600 * Note that in theory this should use the current pid 5601 * so that getpeerucred on the client returns the actual listener 5602 * that does accept. But accept() hasn't been called yet. We could use 5603 * the pid of the process that did bind/listen on the server. 5604 * However, with common usage like inetd() the bind/listen can be done 5605 * by a different process than the accept(). 5606 * Hence we do the simple thing of using the open pid here. 5607 * Note that db_credp is set later in tcp_send_data(). 5608 */ 5609 mblk_setcred(mp1, credp, tcp->tcp_cpid); 5610 eager->tcp_cpid = tcp->tcp_cpid; 5611 eager->tcp_open_time = lbolt64; 5612 5613 /* 5614 * We need to start the rto timer. In normal case, we start 5615 * the timer after sending the packet on the wire (or at 5616 * least believing that packet was sent by waiting for 5617 * CALL_IP_WPUT() to return). Since this is the first packet 5618 * being sent on the wire for the eager, our initial tcp_rto 5619 * is at least tcp_rexmit_interval_min which is a fairly 5620 * large value to allow the algorithm to adjust slowly to large 5621 * fluctuations of RTT during first few transmissions. 5622 * 5623 * Starting the timer first and then sending the packet in this 5624 * case shouldn't make much difference since tcp_rexmit_interval_min 5625 * is of the order of several 100ms and starting the timer 5626 * first and then sending the packet will result in difference 5627 * of few micro seconds. 5628 * 5629 * Without this optimization, we are forced to hold the fanout 5630 * lock across the ipcl_bind_insert() and sending the packet 5631 * so that we don't race against an incoming packet (maybe RST) 5632 * for this eager. 5633 * 5634 * It is necessary to acquire an extra reference on the eager 5635 * at this point and hold it until after tcp_send_data() to 5636 * ensure against an eager close race. 5637 */ 5638 5639 CONN_INC_REF(eager->tcp_connp); 5640 5641 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5642 5643 /* 5644 * Insert the eager in its own perimeter now. We are ready to deal 5645 * with any packets on eager. 5646 */ 5647 if (eager->tcp_ipversion == IPV4_VERSION) { 5648 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5649 goto error; 5650 } 5651 } else { 5652 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5653 goto error; 5654 } 5655 } 5656 5657 /* mark conn as fully-bound */ 5658 econnp->conn_fully_bound = B_TRUE; 5659 5660 /* Send the SYN-ACK */ 5661 tcp_send_data(eager, eager->tcp_wq, mp1); 5662 CONN_DEC_REF(eager->tcp_connp); 5663 freemsg(mp); 5664 5665 return; 5666 error: 5667 freemsg(mp1); 5668 eager->tcp_closemp_used = B_TRUE; 5669 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 5670 mp1 = &eager->tcp_closemp; 5671 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill, 5672 econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_2); 5673 5674 /* 5675 * If a connection already exists, send the mp to that connections so 5676 * that it can be appropriately dealt with. 5677 */ 5678 ipst = tcps->tcps_netstack->netstack_ip; 5679 5680 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 5681 if (!IPCL_IS_CONNECTED(econnp)) { 5682 /* 5683 * Something bad happened. ipcl_conn_insert() 5684 * failed because a connection already existed 5685 * in connected hash but we can't find it 5686 * anymore (someone blew it away). Just 5687 * free this message and hopefully remote 5688 * will retransmit at which time the SYN can be 5689 * treated as a new connection or dealth with 5690 * a TH_RST if a connection already exists. 5691 */ 5692 CONN_DEC_REF(econnp); 5693 freemsg(mp); 5694 } else { 5695 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, 5696 tcp_input, econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_1); 5697 } 5698 } else { 5699 /* Nobody wants this packet */ 5700 freemsg(mp); 5701 } 5702 return; 5703 error3: 5704 CONN_DEC_REF(econnp); 5705 error2: 5706 freemsg(mp); 5707 } 5708 5709 /* 5710 * In an ideal case of vertical partition in NUMA architecture, its 5711 * beneficial to have the listener and all the incoming connections 5712 * tied to the same squeue. The other constraint is that incoming 5713 * connections should be tied to the squeue attached to interrupted 5714 * CPU for obvious locality reason so this leaves the listener to 5715 * be tied to the same squeue. Our only problem is that when listener 5716 * is binding, the CPU that will get interrupted by the NIC whose 5717 * IP address the listener is binding to is not even known. So 5718 * the code below allows us to change that binding at the time the 5719 * CPU is interrupted by virtue of incoming connection's squeue. 5720 * 5721 * This is usefull only in case of a listener bound to a specific IP 5722 * address. For other kind of listeners, they get bound the 5723 * very first time and there is no attempt to rebind them. 5724 */ 5725 void 5726 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5727 { 5728 conn_t *connp = (conn_t *)arg; 5729 squeue_t *sqp = (squeue_t *)arg2; 5730 squeue_t *new_sqp; 5731 uint32_t conn_flags; 5732 5733 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5734 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5735 } else { 5736 goto done; 5737 } 5738 5739 if (connp->conn_fanout == NULL) 5740 goto done; 5741 5742 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5743 mutex_enter(&connp->conn_fanout->connf_lock); 5744 mutex_enter(&connp->conn_lock); 5745 /* 5746 * No one from read or write side can access us now 5747 * except for already queued packets on this squeue. 5748 * But since we haven't changed the squeue yet, they 5749 * can't execute. If they are processed after we have 5750 * changed the squeue, they are sent back to the 5751 * correct squeue down below. 5752 * But a listner close can race with processing of 5753 * incoming SYN. If incoming SYN processing changes 5754 * the squeue then the listener close which is waiting 5755 * to enter the squeue would operate on the wrong 5756 * squeue. Hence we don't change the squeue here unless 5757 * the refcount is exactly the minimum refcount. The 5758 * minimum refcount of 4 is counted as - 1 each for 5759 * TCP and IP, 1 for being in the classifier hash, and 5760 * 1 for the mblk being processed. 5761 */ 5762 5763 if (connp->conn_ref != 4 || 5764 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 5765 mutex_exit(&connp->conn_lock); 5766 mutex_exit(&connp->conn_fanout->connf_lock); 5767 goto done; 5768 } 5769 if (connp->conn_sqp != new_sqp) { 5770 while (connp->conn_sqp != new_sqp) 5771 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5772 } 5773 5774 do { 5775 conn_flags = connp->conn_flags; 5776 conn_flags |= IPCL_FULLY_BOUND; 5777 (void) cas32(&connp->conn_flags, connp->conn_flags, 5778 conn_flags); 5779 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5780 5781 mutex_exit(&connp->conn_fanout->connf_lock); 5782 mutex_exit(&connp->conn_lock); 5783 } 5784 5785 done: 5786 if (connp->conn_sqp != sqp) { 5787 CONN_INC_REF(connp); 5788 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 5789 SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND); 5790 } else { 5791 tcp_conn_request(connp, mp, sqp); 5792 } 5793 } 5794 5795 /* 5796 * Successful connect request processing begins when our client passes 5797 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5798 * our T_OK_ACK reply message upstream. The control flow looks like this: 5799 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_tpi_connect() -> IP 5800 * upstream <- tcp_rput() <- IP 5801 * After various error checks are completed, tcp_tpi_connect() lays 5802 * the target address and port into the composite header template, 5803 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5804 * request followed by an IRE request, and passes the three mblk message 5805 * down to IP looking like this: 5806 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5807 * Processing continues in tcp_rput() when we receive the following message: 5808 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5809 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5810 * to fire off the connection request, and then passes the T_OK_ACK mblk 5811 * upstream that we filled in below. There are, of course, numerous 5812 * error conditions along the way which truncate the processing described 5813 * above. 5814 */ 5815 static void 5816 tcp_tpi_connect(tcp_t *tcp, mblk_t *mp) 5817 { 5818 sin_t *sin; 5819 queue_t *q = tcp->tcp_wq; 5820 struct T_conn_req *tcr; 5821 struct sockaddr *sa; 5822 socklen_t len; 5823 int error; 5824 cred_t *cr; 5825 pid_t cpid; 5826 5827 /* 5828 * All Solaris components should pass a db_credp 5829 * for this TPI message, hence we ASSERT. 5830 * But in case there is some other M_PROTO that looks 5831 * like a TPI message sent by some other kernel 5832 * component, we check and return an error. 5833 */ 5834 cr = msg_getcred(mp, &cpid); 5835 ASSERT(cr != NULL); 5836 if (cr == NULL) { 5837 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5838 return; 5839 } 5840 5841 tcr = (struct T_conn_req *)mp->b_rptr; 5842 5843 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5844 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5845 tcp_err_ack(tcp, mp, TPROTO, 0); 5846 return; 5847 } 5848 5849 /* 5850 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5851 * will always have that to send up. Otherwise, we need to do 5852 * special handling in case the allocation fails at that time. 5853 * If the end point is TPI, the tcp_t can be reused and the 5854 * tcp_ordrel_mp may be allocated already. 5855 */ 5856 if (tcp->tcp_ordrel_mp == NULL) { 5857 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 5858 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5859 return; 5860 } 5861 } 5862 5863 /* 5864 * Determine packet type based on type of address passed in 5865 * the request should contain an IPv4 or IPv6 address. 5866 * Make sure that address family matches the type of 5867 * family of the the address passed down 5868 */ 5869 switch (tcr->DEST_length) { 5870 default: 5871 tcp_err_ack(tcp, mp, TBADADDR, 0); 5872 return; 5873 5874 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5875 /* 5876 * XXX: The check for valid DEST_length was not there 5877 * in earlier releases and some buggy 5878 * TLI apps (e.g Sybase) got away with not feeding 5879 * in sin_zero part of address. 5880 * We allow that bug to keep those buggy apps humming. 5881 * Test suites require the check on DEST_length. 5882 * We construct a new mblk with valid DEST_length 5883 * free the original so the rest of the code does 5884 * not have to keep track of this special shorter 5885 * length address case. 5886 */ 5887 mblk_t *nmp; 5888 struct T_conn_req *ntcr; 5889 sin_t *nsin; 5890 5891 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 5892 tcr->OPT_length, BPRI_HI); 5893 if (nmp == NULL) { 5894 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5895 return; 5896 } 5897 ntcr = (struct T_conn_req *)nmp->b_rptr; 5898 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 5899 ntcr->PRIM_type = T_CONN_REQ; 5900 ntcr->DEST_length = sizeof (sin_t); 5901 ntcr->DEST_offset = sizeof (struct T_conn_req); 5902 5903 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 5904 *nsin = sin_null; 5905 /* Get pointer to shorter address to copy from original mp */ 5906 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 5907 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 5908 if (sin == NULL || !OK_32PTR((char *)sin)) { 5909 freemsg(nmp); 5910 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5911 return; 5912 } 5913 nsin->sin_family = sin->sin_family; 5914 nsin->sin_port = sin->sin_port; 5915 nsin->sin_addr = sin->sin_addr; 5916 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 5917 nmp->b_wptr = (uchar_t *)&nsin[1]; 5918 if (tcr->OPT_length != 0) { 5919 ntcr->OPT_length = tcr->OPT_length; 5920 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 5921 bcopy((uchar_t *)tcr + tcr->OPT_offset, 5922 (uchar_t *)ntcr + ntcr->OPT_offset, 5923 tcr->OPT_length); 5924 nmp->b_wptr += tcr->OPT_length; 5925 } 5926 freemsg(mp); /* original mp freed */ 5927 mp = nmp; /* re-initialize original variables */ 5928 tcr = ntcr; 5929 } 5930 /* FALLTHRU */ 5931 5932 case sizeof (sin_t): 5933 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 5934 sizeof (sin_t)); 5935 len = sizeof (sin_t); 5936 break; 5937 5938 case sizeof (sin6_t): 5939 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 5940 sizeof (sin6_t)); 5941 len = sizeof (sin6_t); 5942 break; 5943 } 5944 5945 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 5946 if (error != 0) { 5947 tcp_err_ack(tcp, mp, TSYSERR, error); 5948 return; 5949 } 5950 5951 /* 5952 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 5953 * should key on their sequence number and cut them loose. 5954 */ 5955 5956 /* 5957 * If options passed in, feed it for verification and handling 5958 */ 5959 if (tcr->OPT_length != 0) { 5960 mblk_t *ok_mp; 5961 mblk_t *discon_mp; 5962 mblk_t *conn_opts_mp; 5963 int t_error, sys_error, do_disconnect; 5964 5965 conn_opts_mp = NULL; 5966 5967 if (tcp_conprim_opt_process(tcp, mp, 5968 &do_disconnect, &t_error, &sys_error) < 0) { 5969 if (do_disconnect) { 5970 ASSERT(t_error == 0 && sys_error == 0); 5971 discon_mp = mi_tpi_discon_ind(NULL, 5972 ECONNREFUSED, 0); 5973 if (!discon_mp) { 5974 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 5975 TSYSERR, ENOMEM); 5976 return; 5977 } 5978 ok_mp = mi_tpi_ok_ack_alloc(mp); 5979 if (!ok_mp) { 5980 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 5981 TSYSERR, ENOMEM); 5982 return; 5983 } 5984 qreply(q, ok_mp); 5985 qreply(q, discon_mp); /* no flush! */ 5986 } else { 5987 ASSERT(t_error != 0); 5988 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 5989 sys_error); 5990 } 5991 return; 5992 } 5993 /* 5994 * Success in setting options, the mp option buffer represented 5995 * by OPT_length/offset has been potentially modified and 5996 * contains results of option processing. We copy it in 5997 * another mp to save it for potentially influencing returning 5998 * it in T_CONN_CONN. 5999 */ 6000 if (tcr->OPT_length != 0) { /* there are resulting options */ 6001 conn_opts_mp = copyb(mp); 6002 if (!conn_opts_mp) { 6003 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6004 TSYSERR, ENOMEM); 6005 return; 6006 } 6007 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6008 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6009 /* 6010 * Note: 6011 * These resulting option negotiation can include any 6012 * end-to-end negotiation options but there no such 6013 * thing (yet?) in our TCP/IP. 6014 */ 6015 } 6016 } 6017 6018 /* call the non-TPI version */ 6019 error = tcp_do_connect(tcp->tcp_connp, sa, len, cr, cpid); 6020 if (error < 0) { 6021 mp = mi_tpi_err_ack_alloc(mp, -error, 0); 6022 } else if (error > 0) { 6023 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 6024 } else { 6025 mp = mi_tpi_ok_ack_alloc(mp); 6026 } 6027 6028 /* 6029 * Note: Code below is the "failure" case 6030 */ 6031 /* return error ack and blow away saved option results if any */ 6032 connect_failed: 6033 if (mp != NULL) 6034 putnext(tcp->tcp_rq, mp); 6035 else { 6036 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6037 TSYSERR, ENOMEM); 6038 } 6039 } 6040 6041 /* 6042 * Handle connect to IPv4 destinations, including connections for AF_INET6 6043 * sockets connecting to IPv4 mapped IPv6 destinations. 6044 */ 6045 static int 6046 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport, 6047 uint_t srcid, cred_t *cr, pid_t pid) 6048 { 6049 tcph_t *tcph; 6050 mblk_t *mp; 6051 ipaddr_t dstaddr = *dstaddrp; 6052 int32_t oldstate; 6053 uint16_t lport; 6054 int error = 0; 6055 tcp_stack_t *tcps = tcp->tcp_tcps; 6056 6057 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6058 6059 /* Check for attempt to connect to INADDR_ANY */ 6060 if (dstaddr == INADDR_ANY) { 6061 /* 6062 * SunOS 4.x and 4.3 BSD allow an application 6063 * to connect a TCP socket to INADDR_ANY. 6064 * When they do this, the kernel picks the 6065 * address of one interface and uses it 6066 * instead. The kernel usually ends up 6067 * picking the address of the loopback 6068 * interface. This is an undocumented feature. 6069 * However, we provide the same thing here 6070 * in order to have source and binary 6071 * compatibility with SunOS 4.x. 6072 * Update the T_CONN_REQ (sin/sin6) since it is used to 6073 * generate the T_CONN_CON. 6074 */ 6075 dstaddr = htonl(INADDR_LOOPBACK); 6076 *dstaddrp = dstaddr; 6077 } 6078 6079 /* Handle __sin6_src_id if socket not bound to an IP address */ 6080 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6081 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6082 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6083 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6084 tcp->tcp_ipha->ipha_src); 6085 } 6086 6087 /* 6088 * Don't let an endpoint connect to itself. Note that 6089 * the test here does not catch the case where the 6090 * source IP addr was left unspecified by the user. In 6091 * this case, the source addr is set in tcp_adapt_ire() 6092 * using the reply to the T_BIND message that we send 6093 * down to IP here and the check is repeated in tcp_rput_other. 6094 */ 6095 if (dstaddr == tcp->tcp_ipha->ipha_src && 6096 dstport == tcp->tcp_lport) { 6097 error = -TBADADDR; 6098 goto failed; 6099 } 6100 6101 tcp->tcp_ipha->ipha_dst = dstaddr; 6102 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6103 6104 /* 6105 * Massage a source route if any putting the first hop 6106 * in iph_dst. Compute a starting value for the checksum which 6107 * takes into account that the original iph_dst should be 6108 * included in the checksum but that ip will include the 6109 * first hop in the source route in the tcp checksum. 6110 */ 6111 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6112 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6113 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6114 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6115 if ((int)tcp->tcp_sum < 0) 6116 tcp->tcp_sum--; 6117 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6118 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6119 (tcp->tcp_sum >> 16)); 6120 tcph = tcp->tcp_tcph; 6121 *(uint16_t *)tcph->th_fport = dstport; 6122 tcp->tcp_fport = dstport; 6123 6124 oldstate = tcp->tcp_state; 6125 /* 6126 * At this point the remote destination address and remote port fields 6127 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6128 * have to see which state tcp was in so we can take apropriate action. 6129 */ 6130 if (oldstate == TCPS_IDLE) { 6131 /* 6132 * We support a quick connect capability here, allowing 6133 * clients to transition directly from IDLE to SYN_SENT 6134 * tcp_bindi will pick an unused port, insert the connection 6135 * in the bind hash and transition to BOUND state. 6136 */ 6137 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6138 tcp, B_TRUE); 6139 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6140 B_FALSE, B_FALSE); 6141 if (lport == 0) { 6142 error = -TNOADDR; 6143 goto failed; 6144 } 6145 } 6146 tcp->tcp_state = TCPS_SYN_SENT; 6147 6148 mp = allocb(sizeof (ire_t), BPRI_HI); 6149 if (mp == NULL) { 6150 tcp->tcp_state = oldstate; 6151 error = ENOMEM; 6152 goto failed; 6153 } 6154 6155 mp->b_wptr += sizeof (ire_t); 6156 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6157 tcp->tcp_hard_binding = 1; 6158 6159 /* 6160 * We need to make sure that the conn_recv is set to a non-null 6161 * value before we insert the conn_t into the classifier table. 6162 * This is to avoid a race with an incoming packet which does 6163 * an ipcl_classify(). 6164 */ 6165 tcp->tcp_connp->conn_recv = tcp_input; 6166 6167 if (tcp->tcp_family == AF_INET) { 6168 error = ip_proto_bind_connected_v4(tcp->tcp_connp, &mp, 6169 IPPROTO_TCP, &tcp->tcp_ipha->ipha_src, tcp->tcp_lport, 6170 tcp->tcp_remote, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6171 } else { 6172 in6_addr_t v6src; 6173 if (tcp->tcp_ipversion == IPV4_VERSION) { 6174 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6175 } else { 6176 v6src = tcp->tcp_ip6h->ip6_src; 6177 } 6178 error = ip_proto_bind_connected_v6(tcp->tcp_connp, &mp, 6179 IPPROTO_TCP, &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6180 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6181 } 6182 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6183 tcp->tcp_active_open = 1; 6184 6185 6186 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6187 failed: 6188 /* return error ack and blow away saved option results if any */ 6189 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6190 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6191 return (error); 6192 } 6193 6194 /* 6195 * Handle connect to IPv6 destinations. 6196 */ 6197 static int 6198 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport, 6199 uint32_t flowinfo, uint_t srcid, uint32_t scope_id, cred_t *cr, pid_t pid) 6200 { 6201 tcph_t *tcph; 6202 mblk_t *mp; 6203 ip6_rthdr_t *rth; 6204 int32_t oldstate; 6205 uint16_t lport; 6206 tcp_stack_t *tcps = tcp->tcp_tcps; 6207 int error = 0; 6208 conn_t *connp = tcp->tcp_connp; 6209 6210 ASSERT(tcp->tcp_family == AF_INET6); 6211 6212 /* 6213 * If we're here, it means that the destination address is a native 6214 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6215 * reason why it might not be IPv6 is if the socket was bound to an 6216 * IPv4-mapped IPv6 address. 6217 */ 6218 if (tcp->tcp_ipversion != IPV6_VERSION) { 6219 return (-TBADADDR); 6220 } 6221 6222 /* 6223 * Interpret a zero destination to mean loopback. 6224 * Update the T_CONN_REQ (sin/sin6) since it is used to 6225 * generate the T_CONN_CON. 6226 */ 6227 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6228 *dstaddrp = ipv6_loopback; 6229 } 6230 6231 /* Handle __sin6_src_id if socket not bound to an IP address */ 6232 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6233 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6234 connp->conn_zoneid, tcps->tcps_netstack); 6235 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6236 } 6237 6238 /* 6239 * Take care of the scope_id now and add ip6i_t 6240 * if ip6i_t is not already allocated through TCP 6241 * sticky options. At this point tcp_ip6h does not 6242 * have dst info, thus use dstaddrp. 6243 */ 6244 if (scope_id != 0 && 6245 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6246 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6247 ip6i_t *ip6i; 6248 6249 ipp->ipp_ifindex = scope_id; 6250 ip6i = (ip6i_t *)tcp->tcp_iphc; 6251 6252 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6253 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6254 /* Already allocated */ 6255 ip6i->ip6i_flags |= IP6I_IFINDEX; 6256 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6257 ipp->ipp_fields |= IPPF_SCOPE_ID; 6258 } else { 6259 int reterr; 6260 6261 ipp->ipp_fields |= IPPF_SCOPE_ID; 6262 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6263 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6264 reterr = tcp_build_hdrs(tcp); 6265 if (reterr != 0) 6266 goto failed; 6267 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6268 } 6269 } 6270 6271 /* 6272 * Don't let an endpoint connect to itself. Note that 6273 * the test here does not catch the case where the 6274 * source IP addr was left unspecified by the user. In 6275 * this case, the source addr is set in tcp_adapt_ire() 6276 * using the reply to the T_BIND message that we send 6277 * down to IP here and the check is repeated in tcp_rput_other. 6278 */ 6279 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6280 (dstport == tcp->tcp_lport)) { 6281 error = -TBADADDR; 6282 goto failed; 6283 } 6284 6285 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6286 tcp->tcp_remote_v6 = *dstaddrp; 6287 tcp->tcp_ip6h->ip6_vcf = 6288 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6289 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6290 6291 /* 6292 * Massage a routing header (if present) putting the first hop 6293 * in ip6_dst. Compute a starting value for the checksum which 6294 * takes into account that the original ip6_dst should be 6295 * included in the checksum but that ip will include the 6296 * first hop in the source route in the tcp checksum. 6297 */ 6298 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6299 if (rth != NULL) { 6300 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6301 tcps->tcps_netstack); 6302 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6303 (tcp->tcp_sum >> 16)); 6304 } else { 6305 tcp->tcp_sum = 0; 6306 } 6307 6308 tcph = tcp->tcp_tcph; 6309 *(uint16_t *)tcph->th_fport = dstport; 6310 tcp->tcp_fport = dstport; 6311 6312 oldstate = tcp->tcp_state; 6313 /* 6314 * At this point the remote destination address and remote port fields 6315 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6316 * have to see which state tcp was in so we can take apropriate action. 6317 */ 6318 if (oldstate == TCPS_IDLE) { 6319 /* 6320 * We support a quick connect capability here, allowing 6321 * clients to transition directly from IDLE to SYN_SENT 6322 * tcp_bindi will pick an unused port, insert the connection 6323 * in the bind hash and transition to BOUND state. 6324 */ 6325 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6326 tcp, B_TRUE); 6327 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6328 B_FALSE, B_FALSE); 6329 if (lport == 0) { 6330 error = -TNOADDR; 6331 goto failed; 6332 } 6333 } 6334 tcp->tcp_state = TCPS_SYN_SENT; 6335 6336 mp = allocb(sizeof (ire_t), BPRI_HI); 6337 if (mp != NULL) { 6338 in6_addr_t v6src; 6339 6340 mp->b_wptr += sizeof (ire_t); 6341 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6342 6343 tcp->tcp_hard_binding = 1; 6344 6345 /* 6346 * We need to make sure that the conn_recv is set to a non-null 6347 * value before we insert the conn_t into the classifier table. 6348 * This is to avoid a race with an incoming packet which does 6349 * an ipcl_classify(). 6350 */ 6351 tcp->tcp_connp->conn_recv = tcp_input; 6352 6353 if (tcp->tcp_ipversion == IPV4_VERSION) { 6354 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6355 } else { 6356 v6src = tcp->tcp_ip6h->ip6_src; 6357 } 6358 error = ip_proto_bind_connected_v6(connp, &mp, IPPROTO_TCP, 6359 &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6360 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6361 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6362 tcp->tcp_active_open = 1; 6363 6364 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6365 } 6366 /* Error case */ 6367 tcp->tcp_state = oldstate; 6368 error = ENOMEM; 6369 6370 failed: 6371 /* return error ack and blow away saved option results if any */ 6372 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6373 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6374 return (error); 6375 } 6376 6377 /* 6378 * We need a stream q for detached closing tcp connections 6379 * to use. Our client hereby indicates that this q is the 6380 * one to use. 6381 */ 6382 static void 6383 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6384 { 6385 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6386 queue_t *q = tcp->tcp_wq; 6387 tcp_stack_t *tcps = tcp->tcp_tcps; 6388 6389 #ifdef NS_DEBUG 6390 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6391 tcps->tcps_netstack->netstack_stackid); 6392 #endif 6393 mp->b_datap->db_type = M_IOCACK; 6394 iocp->ioc_count = 0; 6395 mutex_enter(&tcps->tcps_g_q_lock); 6396 if (tcps->tcps_g_q != NULL) { 6397 mutex_exit(&tcps->tcps_g_q_lock); 6398 iocp->ioc_error = EALREADY; 6399 } else { 6400 int error = 0; 6401 conn_t *connp = tcp->tcp_connp; 6402 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6403 6404 tcps->tcps_g_q = tcp->tcp_rq; 6405 mutex_exit(&tcps->tcps_g_q_lock); 6406 iocp->ioc_error = 0; 6407 iocp->ioc_rval = 0; 6408 /* 6409 * We are passing tcp_sticky_ipp as NULL 6410 * as it is not useful for tcp_default queue 6411 * 6412 * Set conn_recv just in case. 6413 */ 6414 tcp->tcp_connp->conn_recv = tcp_conn_request; 6415 6416 ASSERT(connp->conn_af_isv6); 6417 connp->conn_ulp = IPPROTO_TCP; 6418 6419 if (ipst->ips_ipcl_proto_fanout_v6[IPPROTO_TCP].connf_head != 6420 NULL || connp->conn_mac_exempt) { 6421 error = -TBADADDR; 6422 } else { 6423 connp->conn_srcv6 = ipv6_all_zeros; 6424 ipcl_proto_insert_v6(connp, IPPROTO_TCP); 6425 } 6426 6427 (void) tcp_post_ip_bind(tcp, NULL, error, NULL, 0); 6428 } 6429 qreply(q, mp); 6430 } 6431 6432 static int 6433 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum) 6434 { 6435 tcp_t *ltcp = NULL; 6436 conn_t *connp; 6437 tcp_stack_t *tcps = tcp->tcp_tcps; 6438 6439 /* 6440 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6441 * when the stream is in BOUND state. Do not send a reset, 6442 * since the destination IP address is not valid, and it can 6443 * be the initialized value of all zeros (broadcast address). 6444 * 6445 * XXX There won't be any pending bind request to IP. 6446 */ 6447 if (tcp->tcp_state <= TCPS_BOUND) { 6448 if (tcp->tcp_debug) { 6449 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6450 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6451 } 6452 return (TOUTSTATE); 6453 } 6454 6455 6456 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6457 6458 /* 6459 * According to TPI, for non-listeners, ignore seqnum 6460 * and disconnect. 6461 * Following interpretation of -1 seqnum is historical 6462 * and implied TPI ? (TPI only states that for T_CONN_IND, 6463 * a valid seqnum should not be -1). 6464 * 6465 * -1 means disconnect everything 6466 * regardless even on a listener. 6467 */ 6468 6469 int old_state = tcp->tcp_state; 6470 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6471 6472 /* 6473 * The connection can't be on the tcp_time_wait_head list 6474 * since it is not detached. 6475 */ 6476 ASSERT(tcp->tcp_time_wait_next == NULL); 6477 ASSERT(tcp->tcp_time_wait_prev == NULL); 6478 ASSERT(tcp->tcp_time_wait_expire == 0); 6479 ltcp = NULL; 6480 /* 6481 * If it used to be a listener, check to make sure no one else 6482 * has taken the port before switching back to LISTEN state. 6483 */ 6484 if (tcp->tcp_ipversion == IPV4_VERSION) { 6485 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6486 tcp->tcp_ipha->ipha_src, 6487 tcp->tcp_connp->conn_zoneid, ipst); 6488 if (connp != NULL) 6489 ltcp = connp->conn_tcp; 6490 } else { 6491 /* Allow tcp_bound_if listeners? */ 6492 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6493 &tcp->tcp_ip6h->ip6_src, 0, 6494 tcp->tcp_connp->conn_zoneid, ipst); 6495 if (connp != NULL) 6496 ltcp = connp->conn_tcp; 6497 } 6498 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6499 tcp->tcp_state = TCPS_LISTEN; 6500 } else if (old_state > TCPS_BOUND) { 6501 tcp->tcp_conn_req_max = 0; 6502 tcp->tcp_state = TCPS_BOUND; 6503 } 6504 if (ltcp != NULL) 6505 CONN_DEC_REF(ltcp->tcp_connp); 6506 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6507 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6508 } else if (old_state == TCPS_ESTABLISHED || 6509 old_state == TCPS_CLOSE_WAIT) { 6510 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6511 } 6512 6513 if (tcp->tcp_fused) 6514 tcp_unfuse(tcp); 6515 6516 mutex_enter(&tcp->tcp_eager_lock); 6517 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6518 (tcp->tcp_conn_req_cnt_q != 0)) { 6519 tcp_eager_cleanup(tcp, 0); 6520 } 6521 mutex_exit(&tcp->tcp_eager_lock); 6522 6523 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6524 tcp->tcp_rnxt, TH_RST | TH_ACK); 6525 6526 tcp_reinit(tcp); 6527 6528 return (0); 6529 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6530 return (TBADSEQ); 6531 } 6532 return (0); 6533 } 6534 6535 /* 6536 * Our client hereby directs us to reject the connection request 6537 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6538 * of sending the appropriate RST, not an ICMP error. 6539 */ 6540 static void 6541 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6542 { 6543 t_scalar_t seqnum; 6544 int error; 6545 6546 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6547 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6548 tcp_err_ack(tcp, mp, TPROTO, 0); 6549 return; 6550 } 6551 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6552 error = tcp_disconnect_common(tcp, seqnum); 6553 if (error != 0) 6554 tcp_err_ack(tcp, mp, error, 0); 6555 else { 6556 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6557 /* Send M_FLUSH according to TPI */ 6558 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6559 } 6560 mp = mi_tpi_ok_ack_alloc(mp); 6561 if (mp) 6562 putnext(tcp->tcp_rq, mp); 6563 } 6564 } 6565 6566 /* 6567 * Diagnostic routine used to return a string associated with the tcp state. 6568 * Note that if the caller does not supply a buffer, it will use an internal 6569 * static string. This means that if multiple threads call this function at 6570 * the same time, output can be corrupted... Note also that this function 6571 * does not check the size of the supplied buffer. The caller has to make 6572 * sure that it is big enough. 6573 */ 6574 static char * 6575 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6576 { 6577 char buf1[30]; 6578 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6579 char *buf; 6580 char *cp; 6581 in6_addr_t local, remote; 6582 char local_addrbuf[INET6_ADDRSTRLEN]; 6583 char remote_addrbuf[INET6_ADDRSTRLEN]; 6584 6585 if (sup_buf != NULL) 6586 buf = sup_buf; 6587 else 6588 buf = priv_buf; 6589 6590 if (tcp == NULL) 6591 return ("NULL_TCP"); 6592 switch (tcp->tcp_state) { 6593 case TCPS_CLOSED: 6594 cp = "TCP_CLOSED"; 6595 break; 6596 case TCPS_IDLE: 6597 cp = "TCP_IDLE"; 6598 break; 6599 case TCPS_BOUND: 6600 cp = "TCP_BOUND"; 6601 break; 6602 case TCPS_LISTEN: 6603 cp = "TCP_LISTEN"; 6604 break; 6605 case TCPS_SYN_SENT: 6606 cp = "TCP_SYN_SENT"; 6607 break; 6608 case TCPS_SYN_RCVD: 6609 cp = "TCP_SYN_RCVD"; 6610 break; 6611 case TCPS_ESTABLISHED: 6612 cp = "TCP_ESTABLISHED"; 6613 break; 6614 case TCPS_CLOSE_WAIT: 6615 cp = "TCP_CLOSE_WAIT"; 6616 break; 6617 case TCPS_FIN_WAIT_1: 6618 cp = "TCP_FIN_WAIT_1"; 6619 break; 6620 case TCPS_CLOSING: 6621 cp = "TCP_CLOSING"; 6622 break; 6623 case TCPS_LAST_ACK: 6624 cp = "TCP_LAST_ACK"; 6625 break; 6626 case TCPS_FIN_WAIT_2: 6627 cp = "TCP_FIN_WAIT_2"; 6628 break; 6629 case TCPS_TIME_WAIT: 6630 cp = "TCP_TIME_WAIT"; 6631 break; 6632 default: 6633 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6634 cp = buf1; 6635 break; 6636 } 6637 switch (format) { 6638 case DISP_ADDR_AND_PORT: 6639 if (tcp->tcp_ipversion == IPV4_VERSION) { 6640 /* 6641 * Note that we use the remote address in the tcp_b 6642 * structure. This means that it will print out 6643 * the real destination address, not the next hop's 6644 * address if source routing is used. 6645 */ 6646 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6647 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6648 6649 } else { 6650 local = tcp->tcp_ip_src_v6; 6651 remote = tcp->tcp_remote_v6; 6652 } 6653 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6654 sizeof (local_addrbuf)); 6655 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6656 sizeof (remote_addrbuf)); 6657 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6658 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6659 ntohs(tcp->tcp_fport), cp); 6660 break; 6661 case DISP_PORT_ONLY: 6662 default: 6663 (void) mi_sprintf(buf, "[%u, %u] %s", 6664 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6665 break; 6666 } 6667 6668 return (buf); 6669 } 6670 6671 /* 6672 * Called via squeue to get on to eager's perimeter. It sends a 6673 * TH_RST if eager is in the fanout table. The listener wants the 6674 * eager to disappear either by means of tcp_eager_blowoff() or 6675 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 6676 * called (via squeue) if the eager cannot be inserted in the 6677 * fanout table in tcp_conn_request(). 6678 */ 6679 /* ARGSUSED */ 6680 void 6681 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6682 { 6683 conn_t *econnp = (conn_t *)arg; 6684 tcp_t *eager = econnp->conn_tcp; 6685 tcp_t *listener = eager->tcp_listener; 6686 tcp_stack_t *tcps = eager->tcp_tcps; 6687 6688 /* 6689 * We could be called because listener is closing. Since 6690 * the eager is using listener's queue's, its not safe. 6691 * Better use the default queue just to send the TH_RST 6692 * out. 6693 */ 6694 ASSERT(tcps->tcps_g_q != NULL); 6695 eager->tcp_rq = tcps->tcps_g_q; 6696 eager->tcp_wq = WR(tcps->tcps_g_q); 6697 6698 /* 6699 * An eager's conn_fanout will be NULL if it's a duplicate 6700 * for an existing 4-tuples in the conn fanout table. 6701 * We don't want to send an RST out in such case. 6702 */ 6703 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 6704 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6705 eager, eager->tcp_snxt, 0, TH_RST); 6706 } 6707 6708 /* We are here because listener wants this eager gone */ 6709 if (listener != NULL) { 6710 mutex_enter(&listener->tcp_eager_lock); 6711 tcp_eager_unlink(eager); 6712 if (eager->tcp_tconnind_started) { 6713 /* 6714 * The eager has sent a conn_ind up to the 6715 * listener but listener decides to close 6716 * instead. We need to drop the extra ref 6717 * placed on eager in tcp_rput_data() before 6718 * sending the conn_ind to listener. 6719 */ 6720 CONN_DEC_REF(econnp); 6721 } 6722 mutex_exit(&listener->tcp_eager_lock); 6723 CONN_DEC_REF(listener->tcp_connp); 6724 } 6725 6726 if (eager->tcp_state > TCPS_BOUND) 6727 tcp_close_detached(eager); 6728 } 6729 6730 /* 6731 * Reset any eager connection hanging off this listener marked 6732 * with 'seqnum' and then reclaim it's resources. 6733 */ 6734 static boolean_t 6735 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6736 { 6737 tcp_t *eager; 6738 mblk_t *mp; 6739 tcp_stack_t *tcps = listener->tcp_tcps; 6740 6741 TCP_STAT(tcps, tcp_eager_blowoff_calls); 6742 eager = listener; 6743 mutex_enter(&listener->tcp_eager_lock); 6744 do { 6745 eager = eager->tcp_eager_next_q; 6746 if (eager == NULL) { 6747 mutex_exit(&listener->tcp_eager_lock); 6748 return (B_FALSE); 6749 } 6750 } while (eager->tcp_conn_req_seqnum != seqnum); 6751 6752 if (eager->tcp_closemp_used) { 6753 mutex_exit(&listener->tcp_eager_lock); 6754 return (B_TRUE); 6755 } 6756 eager->tcp_closemp_used = B_TRUE; 6757 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6758 CONN_INC_REF(eager->tcp_connp); 6759 mutex_exit(&listener->tcp_eager_lock); 6760 mp = &eager->tcp_closemp; 6761 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6762 eager->tcp_connp, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF); 6763 return (B_TRUE); 6764 } 6765 6766 /* 6767 * Reset any eager connection hanging off this listener 6768 * and then reclaim it's resources. 6769 */ 6770 static void 6771 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6772 { 6773 tcp_t *eager; 6774 mblk_t *mp; 6775 tcp_stack_t *tcps = listener->tcp_tcps; 6776 6777 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6778 6779 if (!q0_only) { 6780 /* First cleanup q */ 6781 TCP_STAT(tcps, tcp_eager_blowoff_q); 6782 eager = listener->tcp_eager_next_q; 6783 while (eager != NULL) { 6784 if (!eager->tcp_closemp_used) { 6785 eager->tcp_closemp_used = B_TRUE; 6786 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6787 CONN_INC_REF(eager->tcp_connp); 6788 mp = &eager->tcp_closemp; 6789 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6790 tcp_eager_kill, eager->tcp_connp, 6791 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP); 6792 } 6793 eager = eager->tcp_eager_next_q; 6794 } 6795 } 6796 /* Then cleanup q0 */ 6797 TCP_STAT(tcps, tcp_eager_blowoff_q0); 6798 eager = listener->tcp_eager_next_q0; 6799 while (eager != listener) { 6800 if (!eager->tcp_closemp_used) { 6801 eager->tcp_closemp_used = B_TRUE; 6802 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6803 CONN_INC_REF(eager->tcp_connp); 6804 mp = &eager->tcp_closemp; 6805 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6806 tcp_eager_kill, eager->tcp_connp, SQ_FILL, 6807 SQTAG_TCP_EAGER_CLEANUP_Q0); 6808 } 6809 eager = eager->tcp_eager_next_q0; 6810 } 6811 } 6812 6813 /* 6814 * If we are an eager connection hanging off a listener that hasn't 6815 * formally accepted the connection yet, get off his list and blow off 6816 * any data that we have accumulated. 6817 */ 6818 static void 6819 tcp_eager_unlink(tcp_t *tcp) 6820 { 6821 tcp_t *listener = tcp->tcp_listener; 6822 6823 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6824 ASSERT(listener != NULL); 6825 if (tcp->tcp_eager_next_q0 != NULL) { 6826 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6827 6828 /* Remove the eager tcp from q0 */ 6829 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6830 tcp->tcp_eager_prev_q0; 6831 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6832 tcp->tcp_eager_next_q0; 6833 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6834 listener->tcp_conn_req_cnt_q0--; 6835 6836 tcp->tcp_eager_next_q0 = NULL; 6837 tcp->tcp_eager_prev_q0 = NULL; 6838 6839 /* 6840 * Take the eager out, if it is in the list of droppable 6841 * eagers. 6842 */ 6843 MAKE_UNDROPPABLE(tcp); 6844 6845 if (tcp->tcp_syn_rcvd_timeout != 0) { 6846 /* we have timed out before */ 6847 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6848 listener->tcp_syn_rcvd_timeout--; 6849 } 6850 } else { 6851 tcp_t **tcpp = &listener->tcp_eager_next_q; 6852 tcp_t *prev = NULL; 6853 6854 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6855 if (tcpp[0] == tcp) { 6856 if (listener->tcp_eager_last_q == tcp) { 6857 /* 6858 * If we are unlinking the last 6859 * element on the list, adjust 6860 * tail pointer. Set tail pointer 6861 * to nil when list is empty. 6862 */ 6863 ASSERT(tcp->tcp_eager_next_q == NULL); 6864 if (listener->tcp_eager_last_q == 6865 listener->tcp_eager_next_q) { 6866 listener->tcp_eager_last_q = 6867 NULL; 6868 } else { 6869 /* 6870 * We won't get here if there 6871 * is only one eager in the 6872 * list. 6873 */ 6874 ASSERT(prev != NULL); 6875 listener->tcp_eager_last_q = 6876 prev; 6877 } 6878 } 6879 tcpp[0] = tcp->tcp_eager_next_q; 6880 tcp->tcp_eager_next_q = NULL; 6881 tcp->tcp_eager_last_q = NULL; 6882 ASSERT(listener->tcp_conn_req_cnt_q > 0); 6883 listener->tcp_conn_req_cnt_q--; 6884 break; 6885 } 6886 prev = tcpp[0]; 6887 } 6888 } 6889 tcp->tcp_listener = NULL; 6890 } 6891 6892 /* Shorthand to generate and send TPI error acks to our client */ 6893 static void 6894 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 6895 { 6896 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 6897 putnext(tcp->tcp_rq, mp); 6898 } 6899 6900 /* Shorthand to generate and send TPI error acks to our client */ 6901 static void 6902 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 6903 int t_error, int sys_error) 6904 { 6905 struct T_error_ack *teackp; 6906 6907 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 6908 M_PCPROTO, T_ERROR_ACK)) != NULL) { 6909 teackp = (struct T_error_ack *)mp->b_rptr; 6910 teackp->ERROR_prim = primitive; 6911 teackp->TLI_error = t_error; 6912 teackp->UNIX_error = sys_error; 6913 putnext(tcp->tcp_rq, mp); 6914 } 6915 } 6916 6917 /* 6918 * Note: No locks are held when inspecting tcp_g_*epriv_ports 6919 * but instead the code relies on: 6920 * - the fact that the address of the array and its size never changes 6921 * - the atomic assignment of the elements of the array 6922 */ 6923 /* ARGSUSED */ 6924 static int 6925 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 6926 { 6927 int i; 6928 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 6929 6930 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 6931 if (tcps->tcps_g_epriv_ports[i] != 0) 6932 (void) mi_mpprintf(mp, "%d ", 6933 tcps->tcps_g_epriv_ports[i]); 6934 } 6935 return (0); 6936 } 6937 6938 /* 6939 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 6940 * threads from changing it at the same time. 6941 */ 6942 /* ARGSUSED */ 6943 static int 6944 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 6945 cred_t *cr) 6946 { 6947 long new_value; 6948 int i; 6949 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 6950 6951 /* 6952 * Fail the request if the new value does not lie within the 6953 * port number limits. 6954 */ 6955 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 6956 new_value <= 0 || new_value >= 65536) { 6957 return (EINVAL); 6958 } 6959 6960 mutex_enter(&tcps->tcps_epriv_port_lock); 6961 /* Check if the value is already in the list */ 6962 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 6963 if (new_value == tcps->tcps_g_epriv_ports[i]) { 6964 mutex_exit(&tcps->tcps_epriv_port_lock); 6965 return (EEXIST); 6966 } 6967 } 6968 /* Find an empty slot */ 6969 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 6970 if (tcps->tcps_g_epriv_ports[i] == 0) 6971 break; 6972 } 6973 if (i == tcps->tcps_g_num_epriv_ports) { 6974 mutex_exit(&tcps->tcps_epriv_port_lock); 6975 return (EOVERFLOW); 6976 } 6977 /* Set the new value */ 6978 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 6979 mutex_exit(&tcps->tcps_epriv_port_lock); 6980 return (0); 6981 } 6982 6983 /* 6984 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 6985 * threads from changing it at the same time. 6986 */ 6987 /* ARGSUSED */ 6988 static int 6989 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 6990 cred_t *cr) 6991 { 6992 long new_value; 6993 int i; 6994 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 6995 6996 /* 6997 * Fail the request if the new value does not lie within the 6998 * port number limits. 6999 */ 7000 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7001 new_value >= 65536) { 7002 return (EINVAL); 7003 } 7004 7005 mutex_enter(&tcps->tcps_epriv_port_lock); 7006 /* Check that the value is already in the list */ 7007 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7008 if (tcps->tcps_g_epriv_ports[i] == new_value) 7009 break; 7010 } 7011 if (i == tcps->tcps_g_num_epriv_ports) { 7012 mutex_exit(&tcps->tcps_epriv_port_lock); 7013 return (ESRCH); 7014 } 7015 /* Clear the value */ 7016 tcps->tcps_g_epriv_ports[i] = 0; 7017 mutex_exit(&tcps->tcps_epriv_port_lock); 7018 return (0); 7019 } 7020 7021 /* Return the TPI/TLI equivalent of our current tcp_state */ 7022 static int 7023 tcp_tpistate(tcp_t *tcp) 7024 { 7025 switch (tcp->tcp_state) { 7026 case TCPS_IDLE: 7027 return (TS_UNBND); 7028 case TCPS_LISTEN: 7029 /* 7030 * Return whether there are outstanding T_CONN_IND waiting 7031 * for the matching T_CONN_RES. Therefore don't count q0. 7032 */ 7033 if (tcp->tcp_conn_req_cnt_q > 0) 7034 return (TS_WRES_CIND); 7035 else 7036 return (TS_IDLE); 7037 case TCPS_BOUND: 7038 return (TS_IDLE); 7039 case TCPS_SYN_SENT: 7040 return (TS_WCON_CREQ); 7041 case TCPS_SYN_RCVD: 7042 /* 7043 * Note: assumption: this has to the active open SYN_RCVD. 7044 * The passive instance is detached in SYN_RCVD stage of 7045 * incoming connection processing so we cannot get request 7046 * for T_info_ack on it. 7047 */ 7048 return (TS_WACK_CRES); 7049 case TCPS_ESTABLISHED: 7050 return (TS_DATA_XFER); 7051 case TCPS_CLOSE_WAIT: 7052 return (TS_WREQ_ORDREL); 7053 case TCPS_FIN_WAIT_1: 7054 return (TS_WIND_ORDREL); 7055 case TCPS_FIN_WAIT_2: 7056 return (TS_WIND_ORDREL); 7057 7058 case TCPS_CLOSING: 7059 case TCPS_LAST_ACK: 7060 case TCPS_TIME_WAIT: 7061 case TCPS_CLOSED: 7062 /* 7063 * Following TS_WACK_DREQ7 is a rendition of "not 7064 * yet TS_IDLE" TPI state. There is no best match to any 7065 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7066 * choose a value chosen that will map to TLI/XTI level 7067 * state of TSTATECHNG (state is process of changing) which 7068 * captures what this dummy state represents. 7069 */ 7070 return (TS_WACK_DREQ7); 7071 default: 7072 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7073 tcp->tcp_state, tcp_display(tcp, NULL, 7074 DISP_PORT_ONLY)); 7075 return (TS_UNBND); 7076 } 7077 } 7078 7079 static void 7080 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7081 { 7082 tcp_stack_t *tcps = tcp->tcp_tcps; 7083 7084 if (tcp->tcp_family == AF_INET6) 7085 *tia = tcp_g_t_info_ack_v6; 7086 else 7087 *tia = tcp_g_t_info_ack; 7088 tia->CURRENT_state = tcp_tpistate(tcp); 7089 tia->OPT_size = tcp_max_optsize; 7090 if (tcp->tcp_mss == 0) { 7091 /* Not yet set - tcp_open does not set mss */ 7092 if (tcp->tcp_ipversion == IPV4_VERSION) 7093 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7094 else 7095 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7096 } else { 7097 tia->TIDU_size = tcp->tcp_mss; 7098 } 7099 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7100 } 7101 7102 static void 7103 tcp_do_capability_ack(tcp_t *tcp, struct T_capability_ack *tcap, 7104 t_uscalar_t cap_bits1) 7105 { 7106 tcap->CAP_bits1 = 0; 7107 7108 if (cap_bits1 & TC1_INFO) { 7109 tcp_copy_info(&tcap->INFO_ack, tcp); 7110 tcap->CAP_bits1 |= TC1_INFO; 7111 } 7112 7113 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7114 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7115 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7116 } 7117 7118 } 7119 7120 /* 7121 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7122 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7123 * tcp_g_t_info_ack. The current state of the stream is copied from 7124 * tcp_state. 7125 */ 7126 static void 7127 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7128 { 7129 t_uscalar_t cap_bits1; 7130 struct T_capability_ack *tcap; 7131 7132 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7133 freemsg(mp); 7134 return; 7135 } 7136 7137 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7138 7139 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7140 mp->b_datap->db_type, T_CAPABILITY_ACK); 7141 if (mp == NULL) 7142 return; 7143 7144 tcap = (struct T_capability_ack *)mp->b_rptr; 7145 tcp_do_capability_ack(tcp, tcap, cap_bits1); 7146 7147 putnext(tcp->tcp_rq, mp); 7148 } 7149 7150 /* 7151 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7152 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7153 * The current state of the stream is copied from tcp_state. 7154 */ 7155 static void 7156 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7157 { 7158 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7159 T_INFO_ACK); 7160 if (!mp) { 7161 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7162 return; 7163 } 7164 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7165 putnext(tcp->tcp_rq, mp); 7166 } 7167 7168 /* Respond to the TPI addr request */ 7169 static void 7170 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7171 { 7172 sin_t *sin; 7173 mblk_t *ackmp; 7174 struct T_addr_ack *taa; 7175 7176 /* Make it large enough for worst case */ 7177 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7178 2 * sizeof (sin6_t), 1); 7179 if (ackmp == NULL) { 7180 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7181 return; 7182 } 7183 7184 if (tcp->tcp_ipversion == IPV6_VERSION) { 7185 tcp_addr_req_ipv6(tcp, ackmp); 7186 return; 7187 } 7188 taa = (struct T_addr_ack *)ackmp->b_rptr; 7189 7190 bzero(taa, sizeof (struct T_addr_ack)); 7191 ackmp->b_wptr = (uchar_t *)&taa[1]; 7192 7193 taa->PRIM_type = T_ADDR_ACK; 7194 ackmp->b_datap->db_type = M_PCPROTO; 7195 7196 /* 7197 * Note: Following code assumes 32 bit alignment of basic 7198 * data structures like sin_t and struct T_addr_ack. 7199 */ 7200 if (tcp->tcp_state >= TCPS_BOUND) { 7201 /* 7202 * Fill in local address 7203 */ 7204 taa->LOCADDR_length = sizeof (sin_t); 7205 taa->LOCADDR_offset = sizeof (*taa); 7206 7207 sin = (sin_t *)&taa[1]; 7208 7209 /* Fill zeroes and then intialize non-zero fields */ 7210 *sin = sin_null; 7211 7212 sin->sin_family = AF_INET; 7213 7214 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7215 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7216 7217 ackmp->b_wptr = (uchar_t *)&sin[1]; 7218 7219 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7220 /* 7221 * Fill in Remote address 7222 */ 7223 taa->REMADDR_length = sizeof (sin_t); 7224 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7225 taa->LOCADDR_length); 7226 7227 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7228 *sin = sin_null; 7229 sin->sin_family = AF_INET; 7230 sin->sin_addr.s_addr = tcp->tcp_remote; 7231 sin->sin_port = tcp->tcp_fport; 7232 7233 ackmp->b_wptr = (uchar_t *)&sin[1]; 7234 } 7235 } 7236 putnext(tcp->tcp_rq, ackmp); 7237 } 7238 7239 /* Assumes that tcp_addr_req gets enough space and alignment */ 7240 static void 7241 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7242 { 7243 sin6_t *sin6; 7244 struct T_addr_ack *taa; 7245 7246 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7247 ASSERT(OK_32PTR(ackmp->b_rptr)); 7248 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7249 2 * sizeof (sin6_t)); 7250 7251 taa = (struct T_addr_ack *)ackmp->b_rptr; 7252 7253 bzero(taa, sizeof (struct T_addr_ack)); 7254 ackmp->b_wptr = (uchar_t *)&taa[1]; 7255 7256 taa->PRIM_type = T_ADDR_ACK; 7257 ackmp->b_datap->db_type = M_PCPROTO; 7258 7259 /* 7260 * Note: Following code assumes 32 bit alignment of basic 7261 * data structures like sin6_t and struct T_addr_ack. 7262 */ 7263 if (tcp->tcp_state >= TCPS_BOUND) { 7264 /* 7265 * Fill in local address 7266 */ 7267 taa->LOCADDR_length = sizeof (sin6_t); 7268 taa->LOCADDR_offset = sizeof (*taa); 7269 7270 sin6 = (sin6_t *)&taa[1]; 7271 *sin6 = sin6_null; 7272 7273 sin6->sin6_family = AF_INET6; 7274 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7275 sin6->sin6_port = tcp->tcp_lport; 7276 7277 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7278 7279 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7280 /* 7281 * Fill in Remote address 7282 */ 7283 taa->REMADDR_length = sizeof (sin6_t); 7284 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7285 taa->LOCADDR_length); 7286 7287 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7288 *sin6 = sin6_null; 7289 sin6->sin6_family = AF_INET6; 7290 sin6->sin6_flowinfo = 7291 tcp->tcp_ip6h->ip6_vcf & 7292 ~IPV6_VERS_AND_FLOW_MASK; 7293 sin6->sin6_addr = tcp->tcp_remote_v6; 7294 sin6->sin6_port = tcp->tcp_fport; 7295 7296 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7297 } 7298 } 7299 putnext(tcp->tcp_rq, ackmp); 7300 } 7301 7302 /* 7303 * Handle reinitialization of a tcp structure. 7304 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7305 */ 7306 static void 7307 tcp_reinit(tcp_t *tcp) 7308 { 7309 mblk_t *mp; 7310 int err; 7311 tcp_stack_t *tcps = tcp->tcp_tcps; 7312 7313 TCP_STAT(tcps, tcp_reinit_calls); 7314 7315 /* tcp_reinit should never be called for detached tcp_t's */ 7316 ASSERT(tcp->tcp_listener == NULL); 7317 ASSERT((tcp->tcp_family == AF_INET && 7318 tcp->tcp_ipversion == IPV4_VERSION) || 7319 (tcp->tcp_family == AF_INET6 && 7320 (tcp->tcp_ipversion == IPV4_VERSION || 7321 tcp->tcp_ipversion == IPV6_VERSION))); 7322 7323 /* Cancel outstanding timers */ 7324 tcp_timers_stop(tcp); 7325 7326 /* 7327 * Reset everything in the state vector, after updating global 7328 * MIB data from instance counters. 7329 */ 7330 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7331 tcp->tcp_ibsegs = 0; 7332 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7333 tcp->tcp_obsegs = 0; 7334 7335 tcp_close_mpp(&tcp->tcp_xmit_head); 7336 if (tcp->tcp_snd_zcopy_aware) 7337 tcp_zcopy_notify(tcp); 7338 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7339 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7340 mutex_enter(&tcp->tcp_non_sq_lock); 7341 if (tcp->tcp_flow_stopped && 7342 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7343 tcp_clrqfull(tcp); 7344 } 7345 mutex_exit(&tcp->tcp_non_sq_lock); 7346 tcp_close_mpp(&tcp->tcp_reass_head); 7347 tcp->tcp_reass_tail = NULL; 7348 if (tcp->tcp_rcv_list != NULL) { 7349 /* Free b_next chain */ 7350 tcp_close_mpp(&tcp->tcp_rcv_list); 7351 tcp->tcp_rcv_last_head = NULL; 7352 tcp->tcp_rcv_last_tail = NULL; 7353 tcp->tcp_rcv_cnt = 0; 7354 } 7355 tcp->tcp_rcv_last_tail = NULL; 7356 7357 if ((mp = tcp->tcp_urp_mp) != NULL) { 7358 freemsg(mp); 7359 tcp->tcp_urp_mp = NULL; 7360 } 7361 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7362 freemsg(mp); 7363 tcp->tcp_urp_mark_mp = NULL; 7364 } 7365 if (tcp->tcp_fused_sigurg_mp != NULL) { 7366 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7367 freeb(tcp->tcp_fused_sigurg_mp); 7368 tcp->tcp_fused_sigurg_mp = NULL; 7369 } 7370 if (tcp->tcp_ordrel_mp != NULL) { 7371 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7372 freeb(tcp->tcp_ordrel_mp); 7373 tcp->tcp_ordrel_mp = NULL; 7374 } 7375 7376 /* 7377 * Following is a union with two members which are 7378 * identical types and size so the following cleanup 7379 * is enough. 7380 */ 7381 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7382 7383 CL_INET_DISCONNECT(tcp->tcp_connp, tcp); 7384 7385 /* 7386 * The connection can't be on the tcp_time_wait_head list 7387 * since it is not detached. 7388 */ 7389 ASSERT(tcp->tcp_time_wait_next == NULL); 7390 ASSERT(tcp->tcp_time_wait_prev == NULL); 7391 ASSERT(tcp->tcp_time_wait_expire == 0); 7392 7393 if (tcp->tcp_kssl_pending) { 7394 tcp->tcp_kssl_pending = B_FALSE; 7395 7396 /* Don't reset if the initialized by bind. */ 7397 if (tcp->tcp_kssl_ent != NULL) { 7398 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7399 KSSL_NO_PROXY); 7400 } 7401 } 7402 if (tcp->tcp_kssl_ctx != NULL) { 7403 kssl_release_ctx(tcp->tcp_kssl_ctx); 7404 tcp->tcp_kssl_ctx = NULL; 7405 } 7406 7407 /* 7408 * Reset/preserve other values 7409 */ 7410 tcp_reinit_values(tcp); 7411 ipcl_hash_remove(tcp->tcp_connp); 7412 conn_delete_ire(tcp->tcp_connp, NULL); 7413 tcp_ipsec_cleanup(tcp); 7414 7415 if (tcp->tcp_conn_req_max != 0) { 7416 /* 7417 * This is the case when a TLI program uses the same 7418 * transport end point to accept a connection. This 7419 * makes the TCP both a listener and acceptor. When 7420 * this connection is closed, we need to set the state 7421 * back to TCPS_LISTEN. Make sure that the eager list 7422 * is reinitialized. 7423 * 7424 * Note that this stream is still bound to the four 7425 * tuples of the previous connection in IP. If a new 7426 * SYN with different foreign address comes in, IP will 7427 * not find it and will send it to the global queue. In 7428 * the global queue, TCP will do a tcp_lookup_listener() 7429 * to find this stream. This works because this stream 7430 * is only removed from connected hash. 7431 * 7432 */ 7433 tcp->tcp_state = TCPS_LISTEN; 7434 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7435 tcp->tcp_eager_next_drop_q0 = tcp; 7436 tcp->tcp_eager_prev_drop_q0 = tcp; 7437 tcp->tcp_connp->conn_recv = tcp_conn_request; 7438 if (tcp->tcp_family == AF_INET6) { 7439 ASSERT(tcp->tcp_connp->conn_af_isv6); 7440 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7441 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7442 } else { 7443 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7444 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7445 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7446 } 7447 } else { 7448 tcp->tcp_state = TCPS_BOUND; 7449 } 7450 7451 /* 7452 * Initialize to default values 7453 * Can't fail since enough header template space already allocated 7454 * at open(). 7455 */ 7456 err = tcp_init_values(tcp); 7457 ASSERT(err == 0); 7458 /* Restore state in tcp_tcph */ 7459 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7460 if (tcp->tcp_ipversion == IPV4_VERSION) 7461 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7462 else 7463 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7464 /* 7465 * Copy of the src addr. in tcp_t is needed in tcp_t 7466 * since the lookup funcs can only lookup on tcp_t 7467 */ 7468 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7469 7470 ASSERT(tcp->tcp_ptpbhn != NULL); 7471 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 7472 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7473 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 7474 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 7475 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7476 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7477 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7478 } 7479 7480 /* 7481 * Force values to zero that need be zero. 7482 * Do not touch values asociated with the BOUND or LISTEN state 7483 * since the connection will end up in that state after the reinit. 7484 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7485 * structure! 7486 */ 7487 static void 7488 tcp_reinit_values(tcp) 7489 tcp_t *tcp; 7490 { 7491 tcp_stack_t *tcps = tcp->tcp_tcps; 7492 7493 #ifndef lint 7494 #define DONTCARE(x) 7495 #define PRESERVE(x) 7496 #else 7497 #define DONTCARE(x) ((x) = (x)) 7498 #define PRESERVE(x) ((x) = (x)) 7499 #endif /* lint */ 7500 7501 PRESERVE(tcp->tcp_bind_hash_port); 7502 PRESERVE(tcp->tcp_bind_hash); 7503 PRESERVE(tcp->tcp_ptpbhn); 7504 PRESERVE(tcp->tcp_acceptor_hash); 7505 PRESERVE(tcp->tcp_ptpahn); 7506 7507 /* Should be ASSERT NULL on these with new code! */ 7508 ASSERT(tcp->tcp_time_wait_next == NULL); 7509 ASSERT(tcp->tcp_time_wait_prev == NULL); 7510 ASSERT(tcp->tcp_time_wait_expire == 0); 7511 PRESERVE(tcp->tcp_state); 7512 PRESERVE(tcp->tcp_rq); 7513 PRESERVE(tcp->tcp_wq); 7514 7515 ASSERT(tcp->tcp_xmit_head == NULL); 7516 ASSERT(tcp->tcp_xmit_last == NULL); 7517 ASSERT(tcp->tcp_unsent == 0); 7518 ASSERT(tcp->tcp_xmit_tail == NULL); 7519 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7520 7521 tcp->tcp_snxt = 0; /* Displayed in mib */ 7522 tcp->tcp_suna = 0; /* Displayed in mib */ 7523 tcp->tcp_swnd = 0; 7524 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7525 7526 ASSERT(tcp->tcp_ibsegs == 0); 7527 ASSERT(tcp->tcp_obsegs == 0); 7528 7529 if (tcp->tcp_iphc != NULL) { 7530 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7531 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7532 } 7533 7534 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7535 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7536 DONTCARE(tcp->tcp_ipha); 7537 DONTCARE(tcp->tcp_ip6h); 7538 DONTCARE(tcp->tcp_ip_hdr_len); 7539 DONTCARE(tcp->tcp_tcph); 7540 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7541 tcp->tcp_valid_bits = 0; 7542 7543 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7544 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7545 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7546 tcp->tcp_last_rcv_lbolt = 0; 7547 7548 tcp->tcp_init_cwnd = 0; 7549 7550 tcp->tcp_urp_last_valid = 0; 7551 tcp->tcp_hard_binding = 0; 7552 tcp->tcp_hard_bound = 0; 7553 PRESERVE(tcp->tcp_cred); 7554 PRESERVE(tcp->tcp_cpid); 7555 PRESERVE(tcp->tcp_open_time); 7556 PRESERVE(tcp->tcp_exclbind); 7557 7558 tcp->tcp_fin_acked = 0; 7559 tcp->tcp_fin_rcvd = 0; 7560 tcp->tcp_fin_sent = 0; 7561 tcp->tcp_ordrel_done = 0; 7562 7563 tcp->tcp_debug = 0; 7564 tcp->tcp_dontroute = 0; 7565 tcp->tcp_broadcast = 0; 7566 7567 tcp->tcp_useloopback = 0; 7568 tcp->tcp_reuseaddr = 0; 7569 tcp->tcp_oobinline = 0; 7570 tcp->tcp_dgram_errind = 0; 7571 7572 tcp->tcp_detached = 0; 7573 tcp->tcp_bind_pending = 0; 7574 tcp->tcp_unbind_pending = 0; 7575 7576 tcp->tcp_snd_ws_ok = B_FALSE; 7577 tcp->tcp_snd_ts_ok = B_FALSE; 7578 tcp->tcp_linger = 0; 7579 tcp->tcp_ka_enabled = 0; 7580 tcp->tcp_zero_win_probe = 0; 7581 7582 tcp->tcp_loopback = 0; 7583 tcp->tcp_refuse = 0; 7584 tcp->tcp_localnet = 0; 7585 tcp->tcp_syn_defense = 0; 7586 tcp->tcp_set_timer = 0; 7587 7588 tcp->tcp_active_open = 0; 7589 tcp->tcp_rexmit = B_FALSE; 7590 tcp->tcp_xmit_zc_clean = B_FALSE; 7591 7592 tcp->tcp_snd_sack_ok = B_FALSE; 7593 PRESERVE(tcp->tcp_recvdstaddr); 7594 tcp->tcp_hwcksum = B_FALSE; 7595 7596 tcp->tcp_ire_ill_check_done = B_FALSE; 7597 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7598 7599 tcp->tcp_mdt = B_FALSE; 7600 tcp->tcp_mdt_hdr_head = 0; 7601 tcp->tcp_mdt_hdr_tail = 0; 7602 7603 tcp->tcp_conn_def_q0 = 0; 7604 tcp->tcp_ip_forward_progress = B_FALSE; 7605 tcp->tcp_anon_priv_bind = 0; 7606 tcp->tcp_ecn_ok = B_FALSE; 7607 7608 tcp->tcp_cwr = B_FALSE; 7609 tcp->tcp_ecn_echo_on = B_FALSE; 7610 7611 if (tcp->tcp_sack_info != NULL) { 7612 if (tcp->tcp_notsack_list != NULL) { 7613 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7614 } 7615 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7616 tcp->tcp_sack_info = NULL; 7617 } 7618 7619 tcp->tcp_rcv_ws = 0; 7620 tcp->tcp_snd_ws = 0; 7621 tcp->tcp_ts_recent = 0; 7622 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7623 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7624 tcp->tcp_if_mtu = 0; 7625 7626 ASSERT(tcp->tcp_reass_head == NULL); 7627 ASSERT(tcp->tcp_reass_tail == NULL); 7628 7629 tcp->tcp_cwnd_cnt = 0; 7630 7631 ASSERT(tcp->tcp_rcv_list == NULL); 7632 ASSERT(tcp->tcp_rcv_last_head == NULL); 7633 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7634 ASSERT(tcp->tcp_rcv_cnt == 0); 7635 7636 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7637 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7638 tcp->tcp_csuna = 0; 7639 7640 tcp->tcp_rto = 0; /* Displayed in MIB */ 7641 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7642 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7643 tcp->tcp_rtt_update = 0; 7644 7645 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7646 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7647 7648 tcp->tcp_rack = 0; /* Displayed in mib */ 7649 tcp->tcp_rack_cnt = 0; 7650 tcp->tcp_rack_cur_max = 0; 7651 tcp->tcp_rack_abs_max = 0; 7652 7653 tcp->tcp_max_swnd = 0; 7654 7655 ASSERT(tcp->tcp_listener == NULL); 7656 7657 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7658 7659 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7660 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7661 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7662 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7663 7664 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7665 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7666 PRESERVE(tcp->tcp_conn_req_max); 7667 PRESERVE(tcp->tcp_conn_req_seqnum); 7668 7669 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7670 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7671 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7672 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7673 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7674 7675 tcp->tcp_lingertime = 0; 7676 7677 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7678 ASSERT(tcp->tcp_urp_mp == NULL); 7679 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7680 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7681 7682 ASSERT(tcp->tcp_eager_next_q == NULL); 7683 ASSERT(tcp->tcp_eager_last_q == NULL); 7684 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7685 tcp->tcp_eager_prev_q0 == NULL) || 7686 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7687 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7688 7689 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 7690 tcp->tcp_eager_prev_drop_q0 == NULL) || 7691 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 7692 7693 tcp->tcp_client_errno = 0; 7694 7695 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7696 7697 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7698 7699 PRESERVE(tcp->tcp_bound_source_v6); 7700 tcp->tcp_last_sent_len = 0; 7701 tcp->tcp_dupack_cnt = 0; 7702 7703 tcp->tcp_fport = 0; /* Displayed in MIB */ 7704 PRESERVE(tcp->tcp_lport); 7705 7706 PRESERVE(tcp->tcp_acceptor_lockp); 7707 7708 ASSERT(tcp->tcp_ordrel_mp == NULL); 7709 PRESERVE(tcp->tcp_acceptor_id); 7710 DONTCARE(tcp->tcp_ipsec_overhead); 7711 7712 PRESERVE(tcp->tcp_family); 7713 if (tcp->tcp_family == AF_INET6) { 7714 tcp->tcp_ipversion = IPV6_VERSION; 7715 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 7716 } else { 7717 tcp->tcp_ipversion = IPV4_VERSION; 7718 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 7719 } 7720 7721 tcp->tcp_bound_if = 0; 7722 tcp->tcp_ipv6_recvancillary = 0; 7723 tcp->tcp_recvifindex = 0; 7724 tcp->tcp_recvhops = 0; 7725 tcp->tcp_closed = 0; 7726 tcp->tcp_cleandeathtag = 0; 7727 if (tcp->tcp_hopopts != NULL) { 7728 mi_free(tcp->tcp_hopopts); 7729 tcp->tcp_hopopts = NULL; 7730 tcp->tcp_hopoptslen = 0; 7731 } 7732 ASSERT(tcp->tcp_hopoptslen == 0); 7733 if (tcp->tcp_dstopts != NULL) { 7734 mi_free(tcp->tcp_dstopts); 7735 tcp->tcp_dstopts = NULL; 7736 tcp->tcp_dstoptslen = 0; 7737 } 7738 ASSERT(tcp->tcp_dstoptslen == 0); 7739 if (tcp->tcp_rtdstopts != NULL) { 7740 mi_free(tcp->tcp_rtdstopts); 7741 tcp->tcp_rtdstopts = NULL; 7742 tcp->tcp_rtdstoptslen = 0; 7743 } 7744 ASSERT(tcp->tcp_rtdstoptslen == 0); 7745 if (tcp->tcp_rthdr != NULL) { 7746 mi_free(tcp->tcp_rthdr); 7747 tcp->tcp_rthdr = NULL; 7748 tcp->tcp_rthdrlen = 0; 7749 } 7750 ASSERT(tcp->tcp_rthdrlen == 0); 7751 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7752 7753 /* Reset fusion-related fields */ 7754 tcp->tcp_fused = B_FALSE; 7755 tcp->tcp_unfusable = B_FALSE; 7756 tcp->tcp_fused_sigurg = B_FALSE; 7757 tcp->tcp_direct_sockfs = B_FALSE; 7758 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7759 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7760 tcp->tcp_loopback_peer = NULL; 7761 tcp->tcp_fuse_rcv_hiwater = 0; 7762 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7763 tcp->tcp_fuse_rcv_unread_cnt = 0; 7764 7765 tcp->tcp_lso = B_FALSE; 7766 7767 tcp->tcp_in_ack_unsent = 0; 7768 tcp->tcp_cork = B_FALSE; 7769 tcp->tcp_tconnind_started = B_FALSE; 7770 7771 PRESERVE(tcp->tcp_squeue_bytes); 7772 7773 ASSERT(tcp->tcp_kssl_ctx == NULL); 7774 ASSERT(!tcp->tcp_kssl_pending); 7775 PRESERVE(tcp->tcp_kssl_ent); 7776 7777 tcp->tcp_closemp_used = B_FALSE; 7778 7779 PRESERVE(tcp->tcp_rsrv_mp); 7780 PRESERVE(tcp->tcp_rsrv_mp_lock); 7781 7782 #ifdef DEBUG 7783 DONTCARE(tcp->tcmp_stk[0]); 7784 #endif 7785 7786 PRESERVE(tcp->tcp_connid); 7787 7788 7789 #undef DONTCARE 7790 #undef PRESERVE 7791 } 7792 7793 /* 7794 * Allocate necessary resources and initialize state vector. 7795 * Guaranteed not to fail so that when an error is returned, 7796 * the caller doesn't need to do any additional cleanup. 7797 */ 7798 int 7799 tcp_init(tcp_t *tcp, queue_t *q) 7800 { 7801 int err; 7802 7803 tcp->tcp_rq = q; 7804 tcp->tcp_wq = WR(q); 7805 tcp->tcp_state = TCPS_IDLE; 7806 if ((err = tcp_init_values(tcp)) != 0) 7807 tcp_timers_stop(tcp); 7808 return (err); 7809 } 7810 7811 static int 7812 tcp_init_values(tcp_t *tcp) 7813 { 7814 int err; 7815 tcp_stack_t *tcps = tcp->tcp_tcps; 7816 7817 ASSERT((tcp->tcp_family == AF_INET && 7818 tcp->tcp_ipversion == IPV4_VERSION) || 7819 (tcp->tcp_family == AF_INET6 && 7820 (tcp->tcp_ipversion == IPV4_VERSION || 7821 tcp->tcp_ipversion == IPV6_VERSION))); 7822 7823 /* 7824 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7825 * will be close to tcp_rexmit_interval_initial. By doing this, we 7826 * allow the algorithm to adjust slowly to large fluctuations of RTT 7827 * during first few transmissions of a connection as seen in slow 7828 * links. 7829 */ 7830 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 7831 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 7832 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7833 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7834 tcps->tcps_conn_grace_period; 7835 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 7836 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 7837 tcp->tcp_timer_backoff = 0; 7838 tcp->tcp_ms_we_have_waited = 0; 7839 tcp->tcp_last_recv_time = lbolt; 7840 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 7841 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7842 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7843 7844 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 7845 7846 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 7847 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 7848 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 7849 /* 7850 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7851 * passive open. 7852 */ 7853 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 7854 7855 tcp->tcp_naglim = tcps->tcps_naglim_def; 7856 7857 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7858 7859 tcp->tcp_mdt_hdr_head = 0; 7860 tcp->tcp_mdt_hdr_tail = 0; 7861 7862 /* Reset fusion-related fields */ 7863 tcp->tcp_fused = B_FALSE; 7864 tcp->tcp_unfusable = B_FALSE; 7865 tcp->tcp_fused_sigurg = B_FALSE; 7866 tcp->tcp_direct_sockfs = B_FALSE; 7867 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7868 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7869 tcp->tcp_loopback_peer = NULL; 7870 tcp->tcp_fuse_rcv_hiwater = 0; 7871 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7872 tcp->tcp_fuse_rcv_unread_cnt = 0; 7873 7874 /* Initialize the header template */ 7875 if (tcp->tcp_ipversion == IPV4_VERSION) { 7876 err = tcp_header_init_ipv4(tcp); 7877 } else { 7878 err = tcp_header_init_ipv6(tcp); 7879 } 7880 if (err) 7881 return (err); 7882 7883 /* 7884 * Init the window scale to the max so tcp_rwnd_set() won't pare 7885 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 7886 */ 7887 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 7888 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 7889 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 7890 7891 tcp->tcp_cork = B_FALSE; 7892 /* 7893 * Init the tcp_debug option. This value determines whether TCP 7894 * calls strlog() to print out debug messages. Doing this 7895 * initialization here means that this value is not inherited thru 7896 * tcp_reinit(). 7897 */ 7898 tcp->tcp_debug = tcps->tcps_dbg; 7899 7900 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 7901 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 7902 7903 return (0); 7904 } 7905 7906 /* 7907 * Initialize the IPv4 header. Loses any record of any IP options. 7908 */ 7909 static int 7910 tcp_header_init_ipv4(tcp_t *tcp) 7911 { 7912 tcph_t *tcph; 7913 uint32_t sum; 7914 conn_t *connp; 7915 tcp_stack_t *tcps = tcp->tcp_tcps; 7916 7917 /* 7918 * This is a simple initialization. If there's 7919 * already a template, it should never be too small, 7920 * so reuse it. Otherwise, allocate space for the new one. 7921 */ 7922 if (tcp->tcp_iphc == NULL) { 7923 ASSERT(tcp->tcp_iphc_len == 0); 7924 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 7925 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 7926 if (tcp->tcp_iphc == NULL) { 7927 tcp->tcp_iphc_len = 0; 7928 return (ENOMEM); 7929 } 7930 } 7931 7932 /* options are gone; may need a new label */ 7933 connp = tcp->tcp_connp; 7934 connp->conn_mlp_type = mlptSingle; 7935 connp->conn_ulp_labeled = !is_system_labeled(); 7936 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7937 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 7938 tcp->tcp_ip6h = NULL; 7939 tcp->tcp_ipversion = IPV4_VERSION; 7940 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 7941 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 7942 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 7943 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 7944 tcp->tcp_ipha->ipha_version_and_hdr_length 7945 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 7946 tcp->tcp_ipha->ipha_ident = 0; 7947 7948 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 7949 tcp->tcp_tos = 0; 7950 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 7951 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 7952 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 7953 7954 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 7955 tcp->tcp_tcph = tcph; 7956 tcph->th_offset_and_rsrvd[0] = (5 << 4); 7957 /* 7958 * IP wants our header length in the checksum field to 7959 * allow it to perform a single pseudo-header+checksum 7960 * calculation on behalf of TCP. 7961 * Include the adjustment for a source route once IP_OPTIONS is set. 7962 */ 7963 sum = sizeof (tcph_t) + tcp->tcp_sum; 7964 sum = (sum >> 16) + (sum & 0xFFFF); 7965 U16_TO_ABE16(sum, tcph->th_sum); 7966 return (0); 7967 } 7968 7969 /* 7970 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 7971 */ 7972 static int 7973 tcp_header_init_ipv6(tcp_t *tcp) 7974 { 7975 tcph_t *tcph; 7976 uint32_t sum; 7977 conn_t *connp; 7978 tcp_stack_t *tcps = tcp->tcp_tcps; 7979 7980 /* 7981 * This is a simple initialization. If there's 7982 * already a template, it should never be too small, 7983 * so reuse it. Otherwise, allocate space for the new one. 7984 * Ensure that there is enough space to "downgrade" the tcp_t 7985 * to an IPv4 tcp_t. This requires having space for a full load 7986 * of IPv4 options, as well as a full load of TCP options 7987 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 7988 * than a v6 header and a TCP header with a full load of TCP options 7989 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 7990 * We want to avoid reallocation in the "downgraded" case when 7991 * processing outbound IPv4 options. 7992 */ 7993 if (tcp->tcp_iphc == NULL) { 7994 ASSERT(tcp->tcp_iphc_len == 0); 7995 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 7996 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 7997 if (tcp->tcp_iphc == NULL) { 7998 tcp->tcp_iphc_len = 0; 7999 return (ENOMEM); 8000 } 8001 } 8002 8003 /* options are gone; may need a new label */ 8004 connp = tcp->tcp_connp; 8005 connp->conn_mlp_type = mlptSingle; 8006 connp->conn_ulp_labeled = !is_system_labeled(); 8007 8008 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8009 tcp->tcp_ipversion = IPV6_VERSION; 8010 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8011 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8012 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8013 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8014 tcp->tcp_ipha = NULL; 8015 8016 /* Initialize the header template */ 8017 8018 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8019 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8020 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8021 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8022 8023 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8024 tcp->tcp_tcph = tcph; 8025 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8026 /* 8027 * IP wants our header length in the checksum field to 8028 * allow it to perform a single psuedo-header+checksum 8029 * calculation on behalf of TCP. 8030 * Include the adjustment for a source route when IPV6_RTHDR is set. 8031 */ 8032 sum = sizeof (tcph_t) + tcp->tcp_sum; 8033 sum = (sum >> 16) + (sum & 0xFFFF); 8034 U16_TO_ABE16(sum, tcph->th_sum); 8035 return (0); 8036 } 8037 8038 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8039 #define ICMP_MIN_TCP_HDR 8 8040 8041 /* 8042 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8043 * passed up by IP. The message is always received on the correct tcp_t. 8044 * Assumes that IP has pulled up everything up to and including the ICMP header. 8045 */ 8046 void 8047 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8048 { 8049 icmph_t *icmph; 8050 ipha_t *ipha; 8051 int iph_hdr_length; 8052 tcph_t *tcph; 8053 boolean_t ipsec_mctl = B_FALSE; 8054 boolean_t secure; 8055 mblk_t *first_mp = mp; 8056 int32_t new_mss; 8057 uint32_t ratio; 8058 size_t mp_size = MBLKL(mp); 8059 uint32_t seg_seq; 8060 tcp_stack_t *tcps = tcp->tcp_tcps; 8061 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8062 8063 /* Assume IP provides aligned packets - otherwise toss */ 8064 if (!OK_32PTR(mp->b_rptr)) { 8065 freemsg(mp); 8066 return; 8067 } 8068 8069 /* 8070 * Since ICMP errors are normal data marked with M_CTL when sent 8071 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8072 * packets starting with an ipsec_info_t, see ipsec_info.h. 8073 */ 8074 if ((mp_size == sizeof (ipsec_info_t)) && 8075 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8076 ASSERT(mp->b_cont != NULL); 8077 mp = mp->b_cont; 8078 /* IP should have done this */ 8079 ASSERT(OK_32PTR(mp->b_rptr)); 8080 mp_size = MBLKL(mp); 8081 ipsec_mctl = B_TRUE; 8082 } 8083 8084 /* 8085 * Verify that we have a complete outer IP header. If not, drop it. 8086 */ 8087 if (mp_size < sizeof (ipha_t)) { 8088 noticmpv4: 8089 freemsg(first_mp); 8090 return; 8091 } 8092 8093 ipha = (ipha_t *)mp->b_rptr; 8094 /* 8095 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8096 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8097 */ 8098 switch (IPH_HDR_VERSION(ipha)) { 8099 case IPV6_VERSION: 8100 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8101 return; 8102 case IPV4_VERSION: 8103 break; 8104 default: 8105 goto noticmpv4; 8106 } 8107 8108 /* Skip past the outer IP and ICMP headers */ 8109 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8110 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8111 /* 8112 * If we don't have the correct outer IP header length or if the ULP 8113 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8114 * send it upstream. 8115 */ 8116 if (iph_hdr_length < sizeof (ipha_t) || 8117 ipha->ipha_protocol != IPPROTO_ICMP || 8118 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8119 goto noticmpv4; 8120 } 8121 ipha = (ipha_t *)&icmph[1]; 8122 8123 /* Skip past the inner IP and find the ULP header */ 8124 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8125 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8126 /* 8127 * If we don't have the correct inner IP header length or if the ULP 8128 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8129 * bytes of TCP header, drop it. 8130 */ 8131 if (iph_hdr_length < sizeof (ipha_t) || 8132 ipha->ipha_protocol != IPPROTO_TCP || 8133 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8134 goto noticmpv4; 8135 } 8136 8137 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8138 if (ipsec_mctl) { 8139 secure = ipsec_in_is_secure(first_mp); 8140 } else { 8141 secure = B_FALSE; 8142 } 8143 if (secure) { 8144 /* 8145 * If we are willing to accept this in clear 8146 * we don't have to verify policy. 8147 */ 8148 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8149 if (!tcp_check_policy(tcp, first_mp, 8150 ipha, NULL, secure, ipsec_mctl)) { 8151 /* 8152 * tcp_check_policy called 8153 * ip_drop_packet() on failure. 8154 */ 8155 return; 8156 } 8157 } 8158 } 8159 } else if (ipsec_mctl) { 8160 /* 8161 * This is a hard_bound connection. IP has already 8162 * verified policy. We don't have to do it again. 8163 */ 8164 freeb(first_mp); 8165 first_mp = mp; 8166 ipsec_mctl = B_FALSE; 8167 } 8168 8169 seg_seq = ABE32_TO_U32(tcph->th_seq); 8170 /* 8171 * TCP SHOULD check that the TCP sequence number contained in 8172 * payload of the ICMP error message is within the range 8173 * SND.UNA <= SEG.SEQ < SND.NXT. 8174 */ 8175 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8176 /* 8177 * The ICMP message is bogus, just drop it. But if this is 8178 * an ICMP too big message, IP has already changed 8179 * the ire_max_frag to the bogus value. We need to change 8180 * it back. 8181 */ 8182 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8183 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8184 conn_t *connp = tcp->tcp_connp; 8185 ire_t *ire; 8186 int flag; 8187 8188 if (tcp->tcp_ipversion == IPV4_VERSION) { 8189 flag = tcp->tcp_ipha-> 8190 ipha_fragment_offset_and_flags; 8191 } else { 8192 flag = 0; 8193 } 8194 mutex_enter(&connp->conn_lock); 8195 if ((ire = connp->conn_ire_cache) != NULL) { 8196 mutex_enter(&ire->ire_lock); 8197 mutex_exit(&connp->conn_lock); 8198 ire->ire_max_frag = tcp->tcp_if_mtu; 8199 ire->ire_frag_flag |= flag; 8200 mutex_exit(&ire->ire_lock); 8201 } else { 8202 mutex_exit(&connp->conn_lock); 8203 } 8204 } 8205 goto noticmpv4; 8206 } 8207 8208 switch (icmph->icmph_type) { 8209 case ICMP_DEST_UNREACHABLE: 8210 switch (icmph->icmph_code) { 8211 case ICMP_FRAGMENTATION_NEEDED: 8212 /* 8213 * Reduce the MSS based on the new MTU. This will 8214 * eliminate any fragmentation locally. 8215 * N.B. There may well be some funny side-effects on 8216 * the local send policy and the remote receive policy. 8217 * Pending further research, we provide 8218 * tcp_ignore_path_mtu just in case this proves 8219 * disastrous somewhere. 8220 * 8221 * After updating the MSS, retransmit part of the 8222 * dropped segment using the new mss by calling 8223 * tcp_wput_data(). Need to adjust all those 8224 * params to make sure tcp_wput_data() work properly. 8225 */ 8226 if (tcps->tcps_ignore_path_mtu || 8227 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8228 break; 8229 8230 /* 8231 * Decrease the MSS by time stamp options 8232 * IP options and IPSEC options. tcp_hdr_len 8233 * includes time stamp option and IP option 8234 * length. Note that new_mss may be negative 8235 * if tcp_ipsec_overhead is large and the 8236 * icmph_du_mtu is the minimum value, which is 68. 8237 */ 8238 new_mss = ntohs(icmph->icmph_du_mtu) - 8239 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8240 8241 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8242 new_mss); 8243 8244 /* 8245 * Only update the MSS if the new one is 8246 * smaller than the previous one. This is 8247 * to avoid problems when getting multiple 8248 * ICMP errors for the same MTU. 8249 */ 8250 if (new_mss >= tcp->tcp_mss) 8251 break; 8252 8253 /* 8254 * Note that we are using the template header's DF 8255 * bit in the fast path sending. So we need to compare 8256 * the new mss with both tcps_mss_min and ip_pmtu_min. 8257 * And stop doing IPv4 PMTUd if new_mss is less than 8258 * MAX(tcps_mss_min, ip_pmtu_min). 8259 */ 8260 if (new_mss < tcps->tcps_mss_min || 8261 new_mss < ipst->ips_ip_pmtu_min) { 8262 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8263 0; 8264 } 8265 8266 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8267 ASSERT(ratio >= 1); 8268 tcp_mss_set(tcp, new_mss, B_TRUE); 8269 8270 /* 8271 * Make sure we have something to 8272 * send. 8273 */ 8274 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8275 (tcp->tcp_xmit_head != NULL)) { 8276 /* 8277 * Shrink tcp_cwnd in 8278 * proportion to the old MSS/new MSS. 8279 */ 8280 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8281 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8282 (tcp->tcp_unsent == 0)) { 8283 tcp->tcp_rexmit_max = tcp->tcp_fss; 8284 } else { 8285 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8286 } 8287 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8288 tcp->tcp_rexmit = B_TRUE; 8289 tcp->tcp_dupack_cnt = 0; 8290 tcp->tcp_snd_burst = TCP_CWND_SS; 8291 tcp_ss_rexmit(tcp); 8292 } 8293 break; 8294 case ICMP_PORT_UNREACHABLE: 8295 case ICMP_PROTOCOL_UNREACHABLE: 8296 switch (tcp->tcp_state) { 8297 case TCPS_SYN_SENT: 8298 case TCPS_SYN_RCVD: 8299 /* 8300 * ICMP can snipe away incipient 8301 * TCP connections as long as 8302 * seq number is same as initial 8303 * send seq number. 8304 */ 8305 if (seg_seq == tcp->tcp_iss) { 8306 (void) tcp_clean_death(tcp, 8307 ECONNREFUSED, 6); 8308 } 8309 break; 8310 } 8311 break; 8312 case ICMP_HOST_UNREACHABLE: 8313 case ICMP_NET_UNREACHABLE: 8314 /* Record the error in case we finally time out. */ 8315 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8316 tcp->tcp_client_errno = EHOSTUNREACH; 8317 else 8318 tcp->tcp_client_errno = ENETUNREACH; 8319 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8320 if (tcp->tcp_listener != NULL && 8321 tcp->tcp_listener->tcp_syn_defense) { 8322 /* 8323 * Ditch the half-open connection if we 8324 * suspect a SYN attack is under way. 8325 */ 8326 tcp_ip_ire_mark_advice(tcp); 8327 (void) tcp_clean_death(tcp, 8328 tcp->tcp_client_errno, 7); 8329 } 8330 } 8331 break; 8332 default: 8333 break; 8334 } 8335 break; 8336 case ICMP_SOURCE_QUENCH: { 8337 /* 8338 * use a global boolean to control 8339 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8340 * The default is false. 8341 */ 8342 if (tcp_icmp_source_quench) { 8343 /* 8344 * Reduce the sending rate as if we got a 8345 * retransmit timeout 8346 */ 8347 uint32_t npkt; 8348 8349 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8350 tcp->tcp_mss; 8351 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8352 tcp->tcp_cwnd = tcp->tcp_mss; 8353 tcp->tcp_cwnd_cnt = 0; 8354 } 8355 break; 8356 } 8357 } 8358 freemsg(first_mp); 8359 } 8360 8361 /* 8362 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8363 * error messages passed up by IP. 8364 * Assumes that IP has pulled up all the extension headers as well 8365 * as the ICMPv6 header. 8366 */ 8367 static void 8368 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8369 { 8370 icmp6_t *icmp6; 8371 ip6_t *ip6h; 8372 uint16_t iph_hdr_length; 8373 tcpha_t *tcpha; 8374 uint8_t *nexthdrp; 8375 uint32_t new_mss; 8376 uint32_t ratio; 8377 boolean_t secure; 8378 mblk_t *first_mp = mp; 8379 size_t mp_size; 8380 uint32_t seg_seq; 8381 tcp_stack_t *tcps = tcp->tcp_tcps; 8382 8383 /* 8384 * The caller has determined if this is an IPSEC_IN packet and 8385 * set ipsec_mctl appropriately (see tcp_icmp_error). 8386 */ 8387 if (ipsec_mctl) 8388 mp = mp->b_cont; 8389 8390 mp_size = MBLKL(mp); 8391 8392 /* 8393 * Verify that we have a complete IP header. If not, send it upstream. 8394 */ 8395 if (mp_size < sizeof (ip6_t)) { 8396 noticmpv6: 8397 freemsg(first_mp); 8398 return; 8399 } 8400 8401 /* 8402 * Verify this is an ICMPV6 packet, else send it upstream. 8403 */ 8404 ip6h = (ip6_t *)mp->b_rptr; 8405 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8406 iph_hdr_length = IPV6_HDR_LEN; 8407 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8408 &nexthdrp) || 8409 *nexthdrp != IPPROTO_ICMPV6) { 8410 goto noticmpv6; 8411 } 8412 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8413 ip6h = (ip6_t *)&icmp6[1]; 8414 /* 8415 * Verify if we have a complete ICMP and inner IP header. 8416 */ 8417 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8418 goto noticmpv6; 8419 8420 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8421 goto noticmpv6; 8422 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8423 /* 8424 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8425 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8426 * packet. 8427 */ 8428 if ((*nexthdrp != IPPROTO_TCP) || 8429 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8430 goto noticmpv6; 8431 } 8432 8433 /* 8434 * ICMP errors come on the right queue or come on 8435 * listener/global queue for detached connections and 8436 * get switched to the right queue. If it comes on the 8437 * right queue, policy check has already been done by IP 8438 * and thus free the first_mp without verifying the policy. 8439 * If it has come for a non-hard bound connection, we need 8440 * to verify policy as IP may not have done it. 8441 */ 8442 if (!tcp->tcp_hard_bound) { 8443 if (ipsec_mctl) { 8444 secure = ipsec_in_is_secure(first_mp); 8445 } else { 8446 secure = B_FALSE; 8447 } 8448 if (secure) { 8449 /* 8450 * If we are willing to accept this in clear 8451 * we don't have to verify policy. 8452 */ 8453 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8454 if (!tcp_check_policy(tcp, first_mp, 8455 NULL, ip6h, secure, ipsec_mctl)) { 8456 /* 8457 * tcp_check_policy called 8458 * ip_drop_packet() on failure. 8459 */ 8460 return; 8461 } 8462 } 8463 } 8464 } else if (ipsec_mctl) { 8465 /* 8466 * This is a hard_bound connection. IP has already 8467 * verified policy. We don't have to do it again. 8468 */ 8469 freeb(first_mp); 8470 first_mp = mp; 8471 ipsec_mctl = B_FALSE; 8472 } 8473 8474 seg_seq = ntohl(tcpha->tha_seq); 8475 /* 8476 * TCP SHOULD check that the TCP sequence number contained in 8477 * payload of the ICMP error message is within the range 8478 * SND.UNA <= SEG.SEQ < SND.NXT. 8479 */ 8480 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8481 /* 8482 * If the ICMP message is bogus, should we kill the 8483 * connection, or should we just drop the bogus ICMP 8484 * message? It would probably make more sense to just 8485 * drop the message so that if this one managed to get 8486 * in, the real connection should not suffer. 8487 */ 8488 goto noticmpv6; 8489 } 8490 8491 switch (icmp6->icmp6_type) { 8492 case ICMP6_PACKET_TOO_BIG: 8493 /* 8494 * Reduce the MSS based on the new MTU. This will 8495 * eliminate any fragmentation locally. 8496 * N.B. There may well be some funny side-effects on 8497 * the local send policy and the remote receive policy. 8498 * Pending further research, we provide 8499 * tcp_ignore_path_mtu just in case this proves 8500 * disastrous somewhere. 8501 * 8502 * After updating the MSS, retransmit part of the 8503 * dropped segment using the new mss by calling 8504 * tcp_wput_data(). Need to adjust all those 8505 * params to make sure tcp_wput_data() work properly. 8506 */ 8507 if (tcps->tcps_ignore_path_mtu) 8508 break; 8509 8510 /* 8511 * Decrease the MSS by time stamp options 8512 * IP options and IPSEC options. tcp_hdr_len 8513 * includes time stamp option and IP option 8514 * length. 8515 */ 8516 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8517 tcp->tcp_ipsec_overhead; 8518 8519 /* 8520 * Only update the MSS if the new one is 8521 * smaller than the previous one. This is 8522 * to avoid problems when getting multiple 8523 * ICMP errors for the same MTU. 8524 */ 8525 if (new_mss >= tcp->tcp_mss) 8526 break; 8527 8528 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8529 ASSERT(ratio >= 1); 8530 tcp_mss_set(tcp, new_mss, B_TRUE); 8531 8532 /* 8533 * Make sure we have something to 8534 * send. 8535 */ 8536 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8537 (tcp->tcp_xmit_head != NULL)) { 8538 /* 8539 * Shrink tcp_cwnd in 8540 * proportion to the old MSS/new MSS. 8541 */ 8542 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8543 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8544 (tcp->tcp_unsent == 0)) { 8545 tcp->tcp_rexmit_max = tcp->tcp_fss; 8546 } else { 8547 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8548 } 8549 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8550 tcp->tcp_rexmit = B_TRUE; 8551 tcp->tcp_dupack_cnt = 0; 8552 tcp->tcp_snd_burst = TCP_CWND_SS; 8553 tcp_ss_rexmit(tcp); 8554 } 8555 break; 8556 8557 case ICMP6_DST_UNREACH: 8558 switch (icmp6->icmp6_code) { 8559 case ICMP6_DST_UNREACH_NOPORT: 8560 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8561 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8562 (seg_seq == tcp->tcp_iss)) { 8563 (void) tcp_clean_death(tcp, 8564 ECONNREFUSED, 8); 8565 } 8566 break; 8567 8568 case ICMP6_DST_UNREACH_ADMIN: 8569 case ICMP6_DST_UNREACH_NOROUTE: 8570 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8571 case ICMP6_DST_UNREACH_ADDR: 8572 /* Record the error in case we finally time out. */ 8573 tcp->tcp_client_errno = EHOSTUNREACH; 8574 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8575 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8576 (seg_seq == tcp->tcp_iss)) { 8577 if (tcp->tcp_listener != NULL && 8578 tcp->tcp_listener->tcp_syn_defense) { 8579 /* 8580 * Ditch the half-open connection if we 8581 * suspect a SYN attack is under way. 8582 */ 8583 tcp_ip_ire_mark_advice(tcp); 8584 (void) tcp_clean_death(tcp, 8585 tcp->tcp_client_errno, 9); 8586 } 8587 } 8588 8589 8590 break; 8591 default: 8592 break; 8593 } 8594 break; 8595 8596 case ICMP6_PARAM_PROB: 8597 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8598 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8599 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8600 (uchar_t *)nexthdrp) { 8601 if (tcp->tcp_state == TCPS_SYN_SENT || 8602 tcp->tcp_state == TCPS_SYN_RCVD) { 8603 (void) tcp_clean_death(tcp, 8604 ECONNREFUSED, 10); 8605 } 8606 break; 8607 } 8608 break; 8609 8610 case ICMP6_TIME_EXCEEDED: 8611 default: 8612 break; 8613 } 8614 freemsg(first_mp); 8615 } 8616 8617 /* 8618 * Notify IP that we are having trouble with this connection. IP should 8619 * blow the IRE away and start over. 8620 */ 8621 static void 8622 tcp_ip_notify(tcp_t *tcp) 8623 { 8624 struct iocblk *iocp; 8625 ipid_t *ipid; 8626 mblk_t *mp; 8627 8628 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8629 if (tcp->tcp_ipversion == IPV6_VERSION) 8630 return; 8631 8632 mp = mkiocb(IP_IOCTL); 8633 if (mp == NULL) 8634 return; 8635 8636 iocp = (struct iocblk *)mp->b_rptr; 8637 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8638 8639 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8640 if (!mp->b_cont) { 8641 freeb(mp); 8642 return; 8643 } 8644 8645 ipid = (ipid_t *)mp->b_cont->b_rptr; 8646 mp->b_cont->b_wptr += iocp->ioc_count; 8647 bzero(ipid, sizeof (*ipid)); 8648 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8649 ipid->ipid_ire_type = IRE_CACHE; 8650 ipid->ipid_addr_offset = sizeof (ipid_t); 8651 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8652 /* 8653 * Note: in the case of source routing we want to blow away the 8654 * route to the first source route hop. 8655 */ 8656 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8657 sizeof (tcp->tcp_ipha->ipha_dst)); 8658 8659 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8660 } 8661 8662 /* Unlink and return any mblk that looks like it contains an ire */ 8663 static mblk_t * 8664 tcp_ire_mp(mblk_t **mpp) 8665 { 8666 mblk_t *mp = *mpp; 8667 mblk_t *prev_mp = NULL; 8668 8669 for (;;) { 8670 switch (DB_TYPE(mp)) { 8671 case IRE_DB_TYPE: 8672 case IRE_DB_REQ_TYPE: 8673 if (mp == *mpp) { 8674 *mpp = mp->b_cont; 8675 } else { 8676 prev_mp->b_cont = mp->b_cont; 8677 } 8678 mp->b_cont = NULL; 8679 return (mp); 8680 default: 8681 break; 8682 } 8683 prev_mp = mp; 8684 mp = mp->b_cont; 8685 if (mp == NULL) 8686 break; 8687 } 8688 return (mp); 8689 } 8690 8691 /* 8692 * Timer callback routine for keepalive probe. We do a fake resend of 8693 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8694 * check to see if we have heard anything from the other end for the last 8695 * RTO period. If we have, set the timer to expire for another 8696 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8697 * RTO << 1 and check again when it expires. Keep exponentially increasing 8698 * the timeout if we have not heard from the other side. If for more than 8699 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8700 * kill the connection unless the keepalive abort threshold is 0. In 8701 * that case, we will probe "forever." 8702 */ 8703 static void 8704 tcp_keepalive_killer(void *arg) 8705 { 8706 mblk_t *mp; 8707 conn_t *connp = (conn_t *)arg; 8708 tcp_t *tcp = connp->conn_tcp; 8709 int32_t firetime; 8710 int32_t idletime; 8711 int32_t ka_intrvl; 8712 tcp_stack_t *tcps = tcp->tcp_tcps; 8713 8714 tcp->tcp_ka_tid = 0; 8715 8716 if (tcp->tcp_fused) 8717 return; 8718 8719 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 8720 ka_intrvl = tcp->tcp_ka_interval; 8721 8722 /* 8723 * Keepalive probe should only be sent if the application has not 8724 * done a close on the connection. 8725 */ 8726 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8727 return; 8728 } 8729 /* Timer fired too early, restart it. */ 8730 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8731 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8732 MSEC_TO_TICK(ka_intrvl)); 8733 return; 8734 } 8735 8736 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8737 /* 8738 * If we have not heard from the other side for a long 8739 * time, kill the connection unless the keepalive abort 8740 * threshold is 0. In that case, we will probe "forever." 8741 */ 8742 if (tcp->tcp_ka_abort_thres != 0 && 8743 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8744 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 8745 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8746 tcp->tcp_client_errno : ETIMEDOUT, 11); 8747 return; 8748 } 8749 8750 if (tcp->tcp_snxt == tcp->tcp_suna && 8751 idletime >= ka_intrvl) { 8752 /* Fake resend of last ACKed byte. */ 8753 mblk_t *mp1 = allocb(1, BPRI_LO); 8754 8755 if (mp1 != NULL) { 8756 *mp1->b_wptr++ = '\0'; 8757 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8758 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8759 freeb(mp1); 8760 /* 8761 * if allocation failed, fall through to start the 8762 * timer back. 8763 */ 8764 if (mp != NULL) { 8765 tcp_send_data(tcp, tcp->tcp_wq, mp); 8766 BUMP_MIB(&tcps->tcps_mib, 8767 tcpTimKeepaliveProbe); 8768 if (tcp->tcp_ka_last_intrvl != 0) { 8769 int max; 8770 /* 8771 * We should probe again at least 8772 * in ka_intrvl, but not more than 8773 * tcp_rexmit_interval_max. 8774 */ 8775 max = tcps->tcps_rexmit_interval_max; 8776 firetime = MIN(ka_intrvl - 1, 8777 tcp->tcp_ka_last_intrvl << 1); 8778 if (firetime > max) 8779 firetime = max; 8780 } else { 8781 firetime = tcp->tcp_rto; 8782 } 8783 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8784 tcp_keepalive_killer, 8785 MSEC_TO_TICK(firetime)); 8786 tcp->tcp_ka_last_intrvl = firetime; 8787 return; 8788 } 8789 } 8790 } else { 8791 tcp->tcp_ka_last_intrvl = 0; 8792 } 8793 8794 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8795 if ((firetime = ka_intrvl - idletime) < 0) { 8796 firetime = ka_intrvl; 8797 } 8798 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8799 MSEC_TO_TICK(firetime)); 8800 } 8801 8802 int 8803 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8804 { 8805 queue_t *q = tcp->tcp_rq; 8806 int32_t mss = tcp->tcp_mss; 8807 int maxpsz; 8808 conn_t *connp = tcp->tcp_connp; 8809 8810 if (TCP_IS_DETACHED(tcp)) 8811 return (mss); 8812 if (tcp->tcp_fused) { 8813 maxpsz = tcp_fuse_maxpsz_set(tcp); 8814 mss = INFPSZ; 8815 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 8816 /* 8817 * Set the sd_qn_maxpsz according to the socket send buffer 8818 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8819 * instruct the stream head to copyin user data into contiguous 8820 * kernel-allocated buffers without breaking it up into smaller 8821 * chunks. We round up the buffer size to the nearest SMSS. 8822 */ 8823 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8824 if (tcp->tcp_kssl_ctx == NULL) 8825 mss = INFPSZ; 8826 else 8827 mss = SSL3_MAX_RECORD_LEN; 8828 } else { 8829 /* 8830 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8831 * (and a multiple of the mss). This instructs the stream 8832 * head to break down larger than SMSS writes into SMSS- 8833 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8834 */ 8835 /* XXX tune this with ndd tcp_maxpsz_multiplier */ 8836 maxpsz = tcp->tcp_maxpsz * mss; 8837 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8838 maxpsz = tcp->tcp_xmit_hiwater/2; 8839 /* Round up to nearest mss */ 8840 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8841 } 8842 } 8843 8844 (void) proto_set_maxpsz(q, connp, maxpsz); 8845 if (!(IPCL_IS_NONSTR(connp))) { 8846 /* XXX do it in set_maxpsz()? */ 8847 tcp->tcp_wq->q_maxpsz = maxpsz; 8848 } 8849 8850 if (set_maxblk) 8851 (void) proto_set_tx_maxblk(q, connp, mss); 8852 return (mss); 8853 } 8854 8855 /* 8856 * Extract option values from a tcp header. We put any found values into the 8857 * tcpopt struct and return a bitmask saying which options were found. 8858 */ 8859 static int 8860 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8861 { 8862 uchar_t *endp; 8863 int len; 8864 uint32_t mss; 8865 uchar_t *up = (uchar_t *)tcph; 8866 int found = 0; 8867 int32_t sack_len; 8868 tcp_seq sack_begin, sack_end; 8869 tcp_t *tcp; 8870 8871 endp = up + TCP_HDR_LENGTH(tcph); 8872 up += TCP_MIN_HEADER_LENGTH; 8873 while (up < endp) { 8874 len = endp - up; 8875 switch (*up) { 8876 case TCPOPT_EOL: 8877 break; 8878 8879 case TCPOPT_NOP: 8880 up++; 8881 continue; 8882 8883 case TCPOPT_MAXSEG: 8884 if (len < TCPOPT_MAXSEG_LEN || 8885 up[1] != TCPOPT_MAXSEG_LEN) 8886 break; 8887 8888 mss = BE16_TO_U16(up+2); 8889 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 8890 tcpopt->tcp_opt_mss = mss; 8891 found |= TCP_OPT_MSS_PRESENT; 8892 8893 up += TCPOPT_MAXSEG_LEN; 8894 continue; 8895 8896 case TCPOPT_WSCALE: 8897 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 8898 break; 8899 8900 if (up[2] > TCP_MAX_WINSHIFT) 8901 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 8902 else 8903 tcpopt->tcp_opt_wscale = up[2]; 8904 found |= TCP_OPT_WSCALE_PRESENT; 8905 8906 up += TCPOPT_WS_LEN; 8907 continue; 8908 8909 case TCPOPT_SACK_PERMITTED: 8910 if (len < TCPOPT_SACK_OK_LEN || 8911 up[1] != TCPOPT_SACK_OK_LEN) 8912 break; 8913 found |= TCP_OPT_SACK_OK_PRESENT; 8914 up += TCPOPT_SACK_OK_LEN; 8915 continue; 8916 8917 case TCPOPT_SACK: 8918 if (len <= 2 || up[1] <= 2 || len < up[1]) 8919 break; 8920 8921 /* If TCP is not interested in SACK blks... */ 8922 if ((tcp = tcpopt->tcp) == NULL) { 8923 up += up[1]; 8924 continue; 8925 } 8926 sack_len = up[1] - TCPOPT_HEADER_LEN; 8927 up += TCPOPT_HEADER_LEN; 8928 8929 /* 8930 * If the list is empty, allocate one and assume 8931 * nothing is sack'ed. 8932 */ 8933 ASSERT(tcp->tcp_sack_info != NULL); 8934 if (tcp->tcp_notsack_list == NULL) { 8935 tcp_notsack_update(&(tcp->tcp_notsack_list), 8936 tcp->tcp_suna, tcp->tcp_snxt, 8937 &(tcp->tcp_num_notsack_blk), 8938 &(tcp->tcp_cnt_notsack_list)); 8939 8940 /* 8941 * Make sure tcp_notsack_list is not NULL. 8942 * This happens when kmem_alloc(KM_NOSLEEP) 8943 * returns NULL. 8944 */ 8945 if (tcp->tcp_notsack_list == NULL) { 8946 up += sack_len; 8947 continue; 8948 } 8949 tcp->tcp_fack = tcp->tcp_suna; 8950 } 8951 8952 while (sack_len > 0) { 8953 if (up + 8 > endp) { 8954 up = endp; 8955 break; 8956 } 8957 sack_begin = BE32_TO_U32(up); 8958 up += 4; 8959 sack_end = BE32_TO_U32(up); 8960 up += 4; 8961 sack_len -= 8; 8962 /* 8963 * Bounds checking. Make sure the SACK 8964 * info is within tcp_suna and tcp_snxt. 8965 * If this SACK blk is out of bound, ignore 8966 * it but continue to parse the following 8967 * blks. 8968 */ 8969 if (SEQ_LEQ(sack_end, sack_begin) || 8970 SEQ_LT(sack_begin, tcp->tcp_suna) || 8971 SEQ_GT(sack_end, tcp->tcp_snxt)) { 8972 continue; 8973 } 8974 tcp_notsack_insert(&(tcp->tcp_notsack_list), 8975 sack_begin, sack_end, 8976 &(tcp->tcp_num_notsack_blk), 8977 &(tcp->tcp_cnt_notsack_list)); 8978 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 8979 tcp->tcp_fack = sack_end; 8980 } 8981 } 8982 found |= TCP_OPT_SACK_PRESENT; 8983 continue; 8984 8985 case TCPOPT_TSTAMP: 8986 if (len < TCPOPT_TSTAMP_LEN || 8987 up[1] != TCPOPT_TSTAMP_LEN) 8988 break; 8989 8990 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 8991 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 8992 8993 found |= TCP_OPT_TSTAMP_PRESENT; 8994 8995 up += TCPOPT_TSTAMP_LEN; 8996 continue; 8997 8998 default: 8999 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9000 break; 9001 up += up[1]; 9002 continue; 9003 } 9004 break; 9005 } 9006 return (found); 9007 } 9008 9009 /* 9010 * Set the mss associated with a particular tcp based on its current value, 9011 * and a new one passed in. Observe minimums and maximums, and reset 9012 * other state variables that we want to view as multiples of mss. 9013 * 9014 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9015 * highwater marks etc. need to be initialized or adjusted. 9016 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9017 * packet arrives. 9018 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9019 * ICMP6_PACKET_TOO_BIG arrives. 9020 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9021 * to increase the MSS to use the extra bytes available. 9022 * 9023 * Callers except tcp_paws_check() ensure that they only reduce mss. 9024 */ 9025 static void 9026 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9027 { 9028 uint32_t mss_max; 9029 tcp_stack_t *tcps = tcp->tcp_tcps; 9030 9031 if (tcp->tcp_ipversion == IPV4_VERSION) 9032 mss_max = tcps->tcps_mss_max_ipv4; 9033 else 9034 mss_max = tcps->tcps_mss_max_ipv6; 9035 9036 if (mss < tcps->tcps_mss_min) 9037 mss = tcps->tcps_mss_min; 9038 if (mss > mss_max) 9039 mss = mss_max; 9040 /* 9041 * Unless naglim has been set by our client to 9042 * a non-mss value, force naglim to track mss. 9043 * This can help to aggregate small writes. 9044 */ 9045 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9046 tcp->tcp_naglim = mss; 9047 /* 9048 * TCP should be able to buffer at least 4 MSS data for obvious 9049 * performance reason. 9050 */ 9051 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9052 tcp->tcp_xmit_hiwater = mss << 2; 9053 9054 if (do_ss) { 9055 /* 9056 * Either the tcp_cwnd is as yet uninitialized, or mss is 9057 * changing due to a reduction in MTU, presumably as a 9058 * result of a new path component, reset cwnd to its 9059 * "initial" value, as a multiple of the new mss. 9060 */ 9061 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9062 } else { 9063 /* 9064 * Called by tcp_paws_check(), the mss increased 9065 * marginally to allow use of space previously taken 9066 * by the timestamp option. It would be inappropriate 9067 * to apply slow start or tcp_init_cwnd values to 9068 * tcp_cwnd, simply adjust to a multiple of the new mss. 9069 */ 9070 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9071 tcp->tcp_cwnd_cnt = 0; 9072 } 9073 tcp->tcp_mss = mss; 9074 (void) tcp_maxpsz_set(tcp, B_TRUE); 9075 } 9076 9077 /* For /dev/tcp aka AF_INET open */ 9078 static int 9079 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9080 { 9081 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9082 } 9083 9084 /* For /dev/tcp6 aka AF_INET6 open */ 9085 static int 9086 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9087 { 9088 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9089 } 9090 9091 static conn_t * 9092 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9093 boolean_t issocket, int *errorp) 9094 { 9095 tcp_t *tcp = NULL; 9096 conn_t *connp; 9097 int err; 9098 zoneid_t zoneid; 9099 tcp_stack_t *tcps; 9100 squeue_t *sqp; 9101 9102 ASSERT(errorp != NULL); 9103 /* 9104 * Find the proper zoneid and netstack. 9105 */ 9106 /* 9107 * Special case for install: miniroot needs to be able to 9108 * access files via NFS as though it were always in the 9109 * global zone. 9110 */ 9111 if (credp == kcred && nfs_global_client_only != 0) { 9112 zoneid = GLOBAL_ZONEID; 9113 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9114 netstack_tcp; 9115 ASSERT(tcps != NULL); 9116 } else { 9117 netstack_t *ns; 9118 9119 ns = netstack_find_by_cred(credp); 9120 ASSERT(ns != NULL); 9121 tcps = ns->netstack_tcp; 9122 ASSERT(tcps != NULL); 9123 9124 /* 9125 * For exclusive stacks we set the zoneid to zero 9126 * to make TCP operate as if in the global zone. 9127 */ 9128 if (tcps->tcps_netstack->netstack_stackid != 9129 GLOBAL_NETSTACKID) 9130 zoneid = GLOBAL_ZONEID; 9131 else 9132 zoneid = crgetzoneid(credp); 9133 } 9134 /* 9135 * For stackid zero this is done from strplumb.c, but 9136 * non-zero stackids are handled here. 9137 */ 9138 if (tcps->tcps_g_q == NULL && 9139 tcps->tcps_netstack->netstack_stackid != 9140 GLOBAL_NETSTACKID) { 9141 tcp_g_q_setup(tcps); 9142 } 9143 9144 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9145 connp = (conn_t *)tcp_get_conn(sqp, tcps); 9146 /* 9147 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9148 * so we drop it by one. 9149 */ 9150 netstack_rele(tcps->tcps_netstack); 9151 if (connp == NULL) { 9152 *errorp = ENOSR; 9153 return (NULL); 9154 } 9155 connp->conn_sqp = sqp; 9156 connp->conn_initial_sqp = connp->conn_sqp; 9157 tcp = connp->conn_tcp; 9158 9159 if (isv6) { 9160 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9161 connp->conn_send = ip_output_v6; 9162 connp->conn_af_isv6 = B_TRUE; 9163 connp->conn_pkt_isv6 = B_TRUE; 9164 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9165 tcp->tcp_ipversion = IPV6_VERSION; 9166 tcp->tcp_family = AF_INET6; 9167 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9168 } else { 9169 connp->conn_flags |= IPCL_TCP4; 9170 connp->conn_send = ip_output; 9171 connp->conn_af_isv6 = B_FALSE; 9172 connp->conn_pkt_isv6 = B_FALSE; 9173 tcp->tcp_ipversion = IPV4_VERSION; 9174 tcp->tcp_family = AF_INET; 9175 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9176 } 9177 9178 /* 9179 * TCP keeps a copy of cred for cache locality reasons but 9180 * we put a reference only once. If connp->conn_cred 9181 * becomes invalid, tcp_cred should also be set to NULL. 9182 */ 9183 tcp->tcp_cred = connp->conn_cred = credp; 9184 crhold(connp->conn_cred); 9185 tcp->tcp_cpid = curproc->p_pid; 9186 tcp->tcp_open_time = lbolt64; 9187 connp->conn_zoneid = zoneid; 9188 connp->conn_mlp_type = mlptSingle; 9189 connp->conn_ulp_labeled = !is_system_labeled(); 9190 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9191 ASSERT(tcp->tcp_tcps == tcps); 9192 9193 /* 9194 * If the caller has the process-wide flag set, then default to MAC 9195 * exempt mode. This allows read-down to unlabeled hosts. 9196 */ 9197 if (getpflags(NET_MAC_AWARE, credp) != 0) 9198 connp->conn_mac_exempt = B_TRUE; 9199 9200 connp->conn_dev = NULL; 9201 if (issocket) { 9202 connp->conn_flags |= IPCL_SOCKET; 9203 tcp->tcp_issocket = 1; 9204 } 9205 9206 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9207 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9208 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9209 9210 /* Non-zero default values */ 9211 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9212 9213 if (q == NULL) { 9214 /* 9215 * Create a helper stream for non-STREAMS socket. 9216 */ 9217 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9218 if (err != 0) { 9219 ip1dbg(("tcp_create_common: create of IP helper stream " 9220 "failed\n")); 9221 CONN_DEC_REF(connp); 9222 *errorp = err; 9223 return (NULL); 9224 } 9225 q = connp->conn_rq; 9226 } else { 9227 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9228 } 9229 9230 SOCK_CONNID_INIT(tcp->tcp_connid); 9231 err = tcp_init(tcp, q); 9232 if (err != 0) { 9233 CONN_DEC_REF(connp); 9234 *errorp = err; 9235 return (NULL); 9236 } 9237 9238 return (connp); 9239 } 9240 9241 static int 9242 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9243 boolean_t isv6) 9244 { 9245 tcp_t *tcp = NULL; 9246 conn_t *connp = NULL; 9247 int err; 9248 vmem_t *minor_arena = NULL; 9249 dev_t conn_dev; 9250 boolean_t issocket; 9251 9252 if (q->q_ptr != NULL) 9253 return (0); 9254 9255 if (sflag == MODOPEN) 9256 return (EINVAL); 9257 9258 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9259 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9260 minor_arena = ip_minor_arena_la; 9261 } else { 9262 /* 9263 * Either minor numbers in the large arena were exhausted 9264 * or a non socket application is doing the open. 9265 * Try to allocate from the small arena. 9266 */ 9267 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9268 return (EBUSY); 9269 } 9270 minor_arena = ip_minor_arena_sa; 9271 } 9272 9273 ASSERT(minor_arena != NULL); 9274 9275 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9276 9277 if (flag & SO_FALLBACK) { 9278 /* 9279 * Non streams socket needs a stream to fallback to 9280 */ 9281 RD(q)->q_ptr = (void *)conn_dev; 9282 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9283 WR(q)->q_ptr = (void *)minor_arena; 9284 qprocson(q); 9285 return (0); 9286 } else if (flag & SO_ACCEPTOR) { 9287 q->q_qinfo = &tcp_acceptor_rinit; 9288 /* 9289 * the conn_dev and minor_arena will be subsequently used by 9290 * tcp_wput_accept() and tcp_tpi_close_accept() to figure out 9291 * the minor device number for this connection from the q_ptr. 9292 */ 9293 RD(q)->q_ptr = (void *)conn_dev; 9294 WR(q)->q_qinfo = &tcp_acceptor_winit; 9295 WR(q)->q_ptr = (void *)minor_arena; 9296 qprocson(q); 9297 return (0); 9298 } 9299 9300 issocket = flag & SO_SOCKSTR; 9301 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9302 9303 if (connp == NULL) { 9304 inet_minor_free(minor_arena, conn_dev); 9305 q->q_ptr = WR(q)->q_ptr = NULL; 9306 return (err); 9307 } 9308 9309 q->q_ptr = WR(q)->q_ptr = connp; 9310 9311 connp->conn_dev = conn_dev; 9312 connp->conn_minor_arena = minor_arena; 9313 9314 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9315 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9316 9317 tcp = connp->conn_tcp; 9318 9319 if (issocket) { 9320 WR(q)->q_qinfo = &tcp_sock_winit; 9321 } else { 9322 #ifdef _ILP32 9323 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9324 #else 9325 tcp->tcp_acceptor_id = conn_dev; 9326 #endif /* _ILP32 */ 9327 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9328 } 9329 9330 /* 9331 * Put the ref for TCP. Ref for IP was already put 9332 * by ipcl_conn_create. Also Make the conn_t globally 9333 * visible to walkers 9334 */ 9335 mutex_enter(&connp->conn_lock); 9336 CONN_INC_REF_LOCKED(connp); 9337 ASSERT(connp->conn_ref == 2); 9338 connp->conn_state_flags &= ~CONN_INCIPIENT; 9339 mutex_exit(&connp->conn_lock); 9340 9341 qprocson(q); 9342 return (0); 9343 } 9344 9345 /* 9346 * Some TCP options can be "set" by requesting them in the option 9347 * buffer. This is needed for XTI feature test though we do not 9348 * allow it in general. We interpret that this mechanism is more 9349 * applicable to OSI protocols and need not be allowed in general. 9350 * This routine filters out options for which it is not allowed (most) 9351 * and lets through those (few) for which it is. [ The XTI interface 9352 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9353 * ever implemented will have to be allowed here ]. 9354 */ 9355 static boolean_t 9356 tcp_allow_connopt_set(int level, int name) 9357 { 9358 9359 switch (level) { 9360 case IPPROTO_TCP: 9361 switch (name) { 9362 case TCP_NODELAY: 9363 return (B_TRUE); 9364 default: 9365 return (B_FALSE); 9366 } 9367 /*NOTREACHED*/ 9368 default: 9369 return (B_FALSE); 9370 } 9371 /*NOTREACHED*/ 9372 } 9373 9374 /* 9375 * this routine gets default values of certain options whose default 9376 * values are maintained by protocol specific code 9377 */ 9378 /* ARGSUSED */ 9379 int 9380 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9381 { 9382 int32_t *i1 = (int32_t *)ptr; 9383 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9384 9385 switch (level) { 9386 case IPPROTO_TCP: 9387 switch (name) { 9388 case TCP_NOTIFY_THRESHOLD: 9389 *i1 = tcps->tcps_ip_notify_interval; 9390 break; 9391 case TCP_ABORT_THRESHOLD: 9392 *i1 = tcps->tcps_ip_abort_interval; 9393 break; 9394 case TCP_CONN_NOTIFY_THRESHOLD: 9395 *i1 = tcps->tcps_ip_notify_cinterval; 9396 break; 9397 case TCP_CONN_ABORT_THRESHOLD: 9398 *i1 = tcps->tcps_ip_abort_cinterval; 9399 break; 9400 default: 9401 return (-1); 9402 } 9403 break; 9404 case IPPROTO_IP: 9405 switch (name) { 9406 case IP_TTL: 9407 *i1 = tcps->tcps_ipv4_ttl; 9408 break; 9409 default: 9410 return (-1); 9411 } 9412 break; 9413 case IPPROTO_IPV6: 9414 switch (name) { 9415 case IPV6_UNICAST_HOPS: 9416 *i1 = tcps->tcps_ipv6_hoplimit; 9417 break; 9418 default: 9419 return (-1); 9420 } 9421 break; 9422 default: 9423 return (-1); 9424 } 9425 return (sizeof (int)); 9426 } 9427 9428 static int 9429 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9430 { 9431 int *i1 = (int *)ptr; 9432 tcp_t *tcp = connp->conn_tcp; 9433 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9434 9435 switch (level) { 9436 case SOL_SOCKET: 9437 switch (name) { 9438 case SO_LINGER: { 9439 struct linger *lgr = (struct linger *)ptr; 9440 9441 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9442 lgr->l_linger = tcp->tcp_lingertime; 9443 } 9444 return (sizeof (struct linger)); 9445 case SO_DEBUG: 9446 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9447 break; 9448 case SO_KEEPALIVE: 9449 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9450 break; 9451 case SO_DONTROUTE: 9452 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9453 break; 9454 case SO_USELOOPBACK: 9455 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9456 break; 9457 case SO_BROADCAST: 9458 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9459 break; 9460 case SO_REUSEADDR: 9461 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9462 break; 9463 case SO_OOBINLINE: 9464 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9465 break; 9466 case SO_DGRAM_ERRIND: 9467 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9468 break; 9469 case SO_TYPE: 9470 *i1 = SOCK_STREAM; 9471 break; 9472 case SO_SNDBUF: 9473 *i1 = tcp->tcp_xmit_hiwater; 9474 break; 9475 case SO_RCVBUF: 9476 *i1 = tcp->tcp_recv_hiwater; 9477 break; 9478 case SO_SND_COPYAVOID: 9479 *i1 = tcp->tcp_snd_zcopy_on ? 9480 SO_SND_COPYAVOID : 0; 9481 break; 9482 case SO_ALLZONES: 9483 *i1 = connp->conn_allzones ? 1 : 0; 9484 break; 9485 case SO_ANON_MLP: 9486 *i1 = connp->conn_anon_mlp; 9487 break; 9488 case SO_MAC_EXEMPT: 9489 *i1 = connp->conn_mac_exempt; 9490 break; 9491 case SO_EXCLBIND: 9492 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9493 break; 9494 case SO_PROTOTYPE: 9495 *i1 = IPPROTO_TCP; 9496 break; 9497 case SO_DOMAIN: 9498 *i1 = tcp->tcp_family; 9499 break; 9500 case SO_ACCEPTCONN: 9501 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9502 default: 9503 return (-1); 9504 } 9505 break; 9506 case IPPROTO_TCP: 9507 switch (name) { 9508 case TCP_NODELAY: 9509 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9510 break; 9511 case TCP_MAXSEG: 9512 *i1 = tcp->tcp_mss; 9513 break; 9514 case TCP_NOTIFY_THRESHOLD: 9515 *i1 = (int)tcp->tcp_first_timer_threshold; 9516 break; 9517 case TCP_ABORT_THRESHOLD: 9518 *i1 = tcp->tcp_second_timer_threshold; 9519 break; 9520 case TCP_CONN_NOTIFY_THRESHOLD: 9521 *i1 = tcp->tcp_first_ctimer_threshold; 9522 break; 9523 case TCP_CONN_ABORT_THRESHOLD: 9524 *i1 = tcp->tcp_second_ctimer_threshold; 9525 break; 9526 case TCP_RECVDSTADDR: 9527 *i1 = tcp->tcp_recvdstaddr; 9528 break; 9529 case TCP_ANONPRIVBIND: 9530 *i1 = tcp->tcp_anon_priv_bind; 9531 break; 9532 case TCP_EXCLBIND: 9533 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9534 break; 9535 case TCP_INIT_CWND: 9536 *i1 = tcp->tcp_init_cwnd; 9537 break; 9538 case TCP_KEEPALIVE_THRESHOLD: 9539 *i1 = tcp->tcp_ka_interval; 9540 break; 9541 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9542 *i1 = tcp->tcp_ka_abort_thres; 9543 break; 9544 case TCP_CORK: 9545 *i1 = tcp->tcp_cork; 9546 break; 9547 default: 9548 return (-1); 9549 } 9550 break; 9551 case IPPROTO_IP: 9552 if (tcp->tcp_family != AF_INET) 9553 return (-1); 9554 switch (name) { 9555 case IP_OPTIONS: 9556 case T_IP_OPTIONS: { 9557 /* 9558 * This is compatible with BSD in that in only return 9559 * the reverse source route with the final destination 9560 * as the last entry. The first 4 bytes of the option 9561 * will contain the final destination. 9562 */ 9563 int opt_len; 9564 9565 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9566 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9567 ASSERT(opt_len >= 0); 9568 /* Caller ensures enough space */ 9569 if (opt_len > 0) { 9570 /* 9571 * TODO: Do we have to handle getsockopt on an 9572 * initiator as well? 9573 */ 9574 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9575 } 9576 return (0); 9577 } 9578 case IP_TOS: 9579 case T_IP_TOS: 9580 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9581 break; 9582 case IP_TTL: 9583 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9584 break; 9585 case IP_NEXTHOP: 9586 /* Handled at IP level */ 9587 return (-EINVAL); 9588 default: 9589 return (-1); 9590 } 9591 break; 9592 case IPPROTO_IPV6: 9593 /* 9594 * IPPROTO_IPV6 options are only supported for sockets 9595 * that are using IPv6 on the wire. 9596 */ 9597 if (tcp->tcp_ipversion != IPV6_VERSION) { 9598 return (-1); 9599 } 9600 switch (name) { 9601 case IPV6_UNICAST_HOPS: 9602 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9603 break; /* goto sizeof (int) option return */ 9604 case IPV6_BOUND_IF: 9605 /* Zero if not set */ 9606 *i1 = tcp->tcp_bound_if; 9607 break; /* goto sizeof (int) option return */ 9608 case IPV6_RECVPKTINFO: 9609 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9610 *i1 = 1; 9611 else 9612 *i1 = 0; 9613 break; /* goto sizeof (int) option return */ 9614 case IPV6_RECVTCLASS: 9615 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9616 *i1 = 1; 9617 else 9618 *i1 = 0; 9619 break; /* goto sizeof (int) option return */ 9620 case IPV6_RECVHOPLIMIT: 9621 if (tcp->tcp_ipv6_recvancillary & 9622 TCP_IPV6_RECVHOPLIMIT) 9623 *i1 = 1; 9624 else 9625 *i1 = 0; 9626 break; /* goto sizeof (int) option return */ 9627 case IPV6_RECVHOPOPTS: 9628 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9629 *i1 = 1; 9630 else 9631 *i1 = 0; 9632 break; /* goto sizeof (int) option return */ 9633 case IPV6_RECVDSTOPTS: 9634 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9635 *i1 = 1; 9636 else 9637 *i1 = 0; 9638 break; /* goto sizeof (int) option return */ 9639 case _OLD_IPV6_RECVDSTOPTS: 9640 if (tcp->tcp_ipv6_recvancillary & 9641 TCP_OLD_IPV6_RECVDSTOPTS) 9642 *i1 = 1; 9643 else 9644 *i1 = 0; 9645 break; /* goto sizeof (int) option return */ 9646 case IPV6_RECVRTHDR: 9647 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9648 *i1 = 1; 9649 else 9650 *i1 = 0; 9651 break; /* goto sizeof (int) option return */ 9652 case IPV6_RECVRTHDRDSTOPTS: 9653 if (tcp->tcp_ipv6_recvancillary & 9654 TCP_IPV6_RECVRTDSTOPTS) 9655 *i1 = 1; 9656 else 9657 *i1 = 0; 9658 break; /* goto sizeof (int) option return */ 9659 case IPV6_PKTINFO: { 9660 /* XXX assumes that caller has room for max size! */ 9661 struct in6_pktinfo *pkti; 9662 9663 pkti = (struct in6_pktinfo *)ptr; 9664 if (ipp->ipp_fields & IPPF_IFINDEX) 9665 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9666 else 9667 pkti->ipi6_ifindex = 0; 9668 if (ipp->ipp_fields & IPPF_ADDR) 9669 pkti->ipi6_addr = ipp->ipp_addr; 9670 else 9671 pkti->ipi6_addr = ipv6_all_zeros; 9672 return (sizeof (struct in6_pktinfo)); 9673 } 9674 case IPV6_TCLASS: 9675 if (ipp->ipp_fields & IPPF_TCLASS) 9676 *i1 = ipp->ipp_tclass; 9677 else 9678 *i1 = IPV6_FLOW_TCLASS( 9679 IPV6_DEFAULT_VERS_AND_FLOW); 9680 break; /* goto sizeof (int) option return */ 9681 case IPV6_NEXTHOP: { 9682 sin6_t *sin6 = (sin6_t *)ptr; 9683 9684 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9685 return (0); 9686 *sin6 = sin6_null; 9687 sin6->sin6_family = AF_INET6; 9688 sin6->sin6_addr = ipp->ipp_nexthop; 9689 return (sizeof (sin6_t)); 9690 } 9691 case IPV6_HOPOPTS: 9692 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9693 return (0); 9694 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9695 return (0); 9696 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9697 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9698 if (tcp->tcp_label_len > 0) { 9699 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9700 ptr[1] = (ipp->ipp_hopoptslen - 9701 tcp->tcp_label_len + 7) / 8 - 1; 9702 } 9703 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9704 case IPV6_RTHDRDSTOPTS: 9705 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9706 return (0); 9707 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9708 return (ipp->ipp_rtdstoptslen); 9709 case IPV6_RTHDR: 9710 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9711 return (0); 9712 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9713 return (ipp->ipp_rthdrlen); 9714 case IPV6_DSTOPTS: 9715 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9716 return (0); 9717 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9718 return (ipp->ipp_dstoptslen); 9719 case IPV6_SRC_PREFERENCES: 9720 return (ip6_get_src_preferences(connp, 9721 (uint32_t *)ptr)); 9722 case IPV6_PATHMTU: { 9723 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9724 9725 if (tcp->tcp_state < TCPS_ESTABLISHED) 9726 return (-1); 9727 9728 return (ip_fill_mtuinfo(&connp->conn_remv6, 9729 connp->conn_fport, mtuinfo, 9730 connp->conn_netstack)); 9731 } 9732 default: 9733 return (-1); 9734 } 9735 break; 9736 default: 9737 return (-1); 9738 } 9739 return (sizeof (int)); 9740 } 9741 9742 /* 9743 * TCP routine to get the values of options. 9744 */ 9745 int 9746 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9747 { 9748 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9749 } 9750 9751 /* returns UNIX error, the optlen is a value-result arg */ 9752 int 9753 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9754 void *optvalp, socklen_t *optlen, cred_t *cr) 9755 { 9756 conn_t *connp = (conn_t *)proto_handle; 9757 squeue_t *sqp = connp->conn_sqp; 9758 int error; 9759 t_uscalar_t max_optbuf_len; 9760 void *optvalp_buf; 9761 int len; 9762 9763 ASSERT(connp->conn_upper_handle != NULL); 9764 9765 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9766 tcp_opt_obj.odb_opt_des_arr, 9767 tcp_opt_obj.odb_opt_arr_cnt, 9768 tcp_opt_obj.odb_topmost_tpiprovider, 9769 B_FALSE, B_TRUE, cr); 9770 if (error != 0) { 9771 if (error < 0) { 9772 error = proto_tlitosyserr(-error); 9773 } 9774 return (error); 9775 } 9776 9777 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9778 9779 error = squeue_synch_enter(sqp, connp, NULL); 9780 if (error == ENOMEM) { 9781 return (ENOMEM); 9782 } 9783 9784 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9785 squeue_synch_exit(sqp, connp); 9786 9787 if (len < 0) { 9788 /* 9789 * Pass on to IP 9790 */ 9791 kmem_free(optvalp_buf, max_optbuf_len); 9792 return (ip_get_options(connp, level, option_name, 9793 optvalp, optlen, cr)); 9794 } else { 9795 /* 9796 * update optlen and copy option value 9797 */ 9798 t_uscalar_t size = MIN(len, *optlen); 9799 bcopy(optvalp_buf, optvalp, size); 9800 bcopy(&size, optlen, sizeof (size)); 9801 9802 kmem_free(optvalp_buf, max_optbuf_len); 9803 return (0); 9804 } 9805 } 9806 9807 /* 9808 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9809 * Parameters are assumed to be verified by the caller. 9810 */ 9811 /* ARGSUSED */ 9812 int 9813 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9814 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9815 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9816 { 9817 tcp_t *tcp = connp->conn_tcp; 9818 int *i1 = (int *)invalp; 9819 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9820 boolean_t checkonly; 9821 int reterr; 9822 tcp_stack_t *tcps = tcp->tcp_tcps; 9823 9824 switch (optset_context) { 9825 case SETFN_OPTCOM_CHECKONLY: 9826 checkonly = B_TRUE; 9827 /* 9828 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9829 * inlen != 0 implies value supplied and 9830 * we have to "pretend" to set it. 9831 * inlen == 0 implies that there is no 9832 * value part in T_CHECK request and just validation 9833 * done elsewhere should be enough, we just return here. 9834 */ 9835 if (inlen == 0) { 9836 *outlenp = 0; 9837 return (0); 9838 } 9839 break; 9840 case SETFN_OPTCOM_NEGOTIATE: 9841 checkonly = B_FALSE; 9842 break; 9843 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9844 case SETFN_CONN_NEGOTIATE: 9845 checkonly = B_FALSE; 9846 /* 9847 * Negotiating local and "association-related" options 9848 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9849 * primitives is allowed by XTI, but we choose 9850 * to not implement this style negotiation for Internet 9851 * protocols (We interpret it is a must for OSI world but 9852 * optional for Internet protocols) for all options. 9853 * [ Will do only for the few options that enable test 9854 * suites that our XTI implementation of this feature 9855 * works for transports that do allow it ] 9856 */ 9857 if (!tcp_allow_connopt_set(level, name)) { 9858 *outlenp = 0; 9859 return (EINVAL); 9860 } 9861 break; 9862 default: 9863 /* 9864 * We should never get here 9865 */ 9866 *outlenp = 0; 9867 return (EINVAL); 9868 } 9869 9870 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9871 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9872 9873 /* 9874 * For TCP, we should have no ancillary data sent down 9875 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9876 * has to be zero. 9877 */ 9878 ASSERT(thisdg_attrs == NULL); 9879 9880 /* 9881 * For fixed length options, no sanity check 9882 * of passed in length is done. It is assumed *_optcom_req() 9883 * routines do the right thing. 9884 */ 9885 switch (level) { 9886 case SOL_SOCKET: 9887 switch (name) { 9888 case SO_LINGER: { 9889 struct linger *lgr = (struct linger *)invalp; 9890 9891 if (!checkonly) { 9892 if (lgr->l_onoff) { 9893 tcp->tcp_linger = 1; 9894 tcp->tcp_lingertime = lgr->l_linger; 9895 } else { 9896 tcp->tcp_linger = 0; 9897 tcp->tcp_lingertime = 0; 9898 } 9899 /* struct copy */ 9900 *(struct linger *)outvalp = *lgr; 9901 } else { 9902 if (!lgr->l_onoff) { 9903 ((struct linger *) 9904 outvalp)->l_onoff = 0; 9905 ((struct linger *) 9906 outvalp)->l_linger = 0; 9907 } else { 9908 /* struct copy */ 9909 *(struct linger *)outvalp = *lgr; 9910 } 9911 } 9912 *outlenp = sizeof (struct linger); 9913 return (0); 9914 } 9915 case SO_DEBUG: 9916 if (!checkonly) 9917 tcp->tcp_debug = onoff; 9918 break; 9919 case SO_KEEPALIVE: 9920 if (checkonly) { 9921 /* check only case */ 9922 break; 9923 } 9924 9925 if (!onoff) { 9926 if (tcp->tcp_ka_enabled) { 9927 if (tcp->tcp_ka_tid != 0) { 9928 (void) TCP_TIMER_CANCEL(tcp, 9929 tcp->tcp_ka_tid); 9930 tcp->tcp_ka_tid = 0; 9931 } 9932 tcp->tcp_ka_enabled = 0; 9933 } 9934 break; 9935 } 9936 if (!tcp->tcp_ka_enabled) { 9937 /* Crank up the keepalive timer */ 9938 tcp->tcp_ka_last_intrvl = 0; 9939 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9940 tcp_keepalive_killer, 9941 MSEC_TO_TICK(tcp->tcp_ka_interval)); 9942 tcp->tcp_ka_enabled = 1; 9943 } 9944 break; 9945 case SO_DONTROUTE: 9946 /* 9947 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 9948 * only of interest to IP. We track them here only so 9949 * that we can report their current value. 9950 */ 9951 if (!checkonly) { 9952 tcp->tcp_dontroute = onoff; 9953 tcp->tcp_connp->conn_dontroute = onoff; 9954 } 9955 break; 9956 case SO_USELOOPBACK: 9957 if (!checkonly) { 9958 tcp->tcp_useloopback = onoff; 9959 tcp->tcp_connp->conn_loopback = onoff; 9960 } 9961 break; 9962 case SO_BROADCAST: 9963 if (!checkonly) { 9964 tcp->tcp_broadcast = onoff; 9965 tcp->tcp_connp->conn_broadcast = onoff; 9966 } 9967 break; 9968 case SO_REUSEADDR: 9969 if (!checkonly) { 9970 tcp->tcp_reuseaddr = onoff; 9971 tcp->tcp_connp->conn_reuseaddr = onoff; 9972 } 9973 break; 9974 case SO_OOBINLINE: 9975 if (!checkonly) { 9976 tcp->tcp_oobinline = onoff; 9977 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 9978 proto_set_rx_oob_opt(connp, onoff); 9979 } 9980 break; 9981 case SO_DGRAM_ERRIND: 9982 if (!checkonly) 9983 tcp->tcp_dgram_errind = onoff; 9984 break; 9985 case SO_SNDBUF: { 9986 if (*i1 > tcps->tcps_max_buf) { 9987 *outlenp = 0; 9988 return (ENOBUFS); 9989 } 9990 if (checkonly) 9991 break; 9992 9993 tcp->tcp_xmit_hiwater = *i1; 9994 if (tcps->tcps_snd_lowat_fraction != 0) 9995 tcp->tcp_xmit_lowater = 9996 tcp->tcp_xmit_hiwater / 9997 tcps->tcps_snd_lowat_fraction; 9998 (void) tcp_maxpsz_set(tcp, B_TRUE); 9999 /* 10000 * If we are flow-controlled, recheck the condition. 10001 * There are apps that increase SO_SNDBUF size when 10002 * flow-controlled (EWOULDBLOCK), and expect the flow 10003 * control condition to be lifted right away. 10004 */ 10005 mutex_enter(&tcp->tcp_non_sq_lock); 10006 if (tcp->tcp_flow_stopped && 10007 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10008 tcp_clrqfull(tcp); 10009 } 10010 mutex_exit(&tcp->tcp_non_sq_lock); 10011 break; 10012 } 10013 case SO_RCVBUF: 10014 if (*i1 > tcps->tcps_max_buf) { 10015 *outlenp = 0; 10016 return (ENOBUFS); 10017 } 10018 /* Silently ignore zero */ 10019 if (!checkonly && *i1 != 0) { 10020 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10021 (void) tcp_rwnd_set(tcp, *i1); 10022 } 10023 /* 10024 * XXX should we return the rwnd here 10025 * and tcp_opt_get ? 10026 */ 10027 break; 10028 case SO_SND_COPYAVOID: 10029 if (!checkonly) { 10030 /* we only allow enable at most once for now */ 10031 if (tcp->tcp_loopback || 10032 (tcp->tcp_kssl_ctx != NULL) || 10033 (!tcp->tcp_snd_zcopy_aware && 10034 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10035 *outlenp = 0; 10036 return (EOPNOTSUPP); 10037 } 10038 tcp->tcp_snd_zcopy_aware = 1; 10039 } 10040 break; 10041 case SO_RCVTIMEO: 10042 case SO_SNDTIMEO: 10043 /* 10044 * Pass these two options in order for third part 10045 * protocol usage. Here just return directly. 10046 */ 10047 return (0); 10048 case SO_ALLZONES: 10049 /* Pass option along to IP level for handling */ 10050 return (-EINVAL); 10051 case SO_ANON_MLP: 10052 /* Pass option along to IP level for handling */ 10053 return (-EINVAL); 10054 case SO_MAC_EXEMPT: 10055 /* Pass option along to IP level for handling */ 10056 return (-EINVAL); 10057 case SO_EXCLBIND: 10058 if (!checkonly) 10059 tcp->tcp_exclbind = onoff; 10060 break; 10061 default: 10062 *outlenp = 0; 10063 return (EINVAL); 10064 } 10065 break; 10066 case IPPROTO_TCP: 10067 switch (name) { 10068 case TCP_NODELAY: 10069 if (!checkonly) 10070 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10071 break; 10072 case TCP_NOTIFY_THRESHOLD: 10073 if (!checkonly) 10074 tcp->tcp_first_timer_threshold = *i1; 10075 break; 10076 case TCP_ABORT_THRESHOLD: 10077 if (!checkonly) 10078 tcp->tcp_second_timer_threshold = *i1; 10079 break; 10080 case TCP_CONN_NOTIFY_THRESHOLD: 10081 if (!checkonly) 10082 tcp->tcp_first_ctimer_threshold = *i1; 10083 break; 10084 case TCP_CONN_ABORT_THRESHOLD: 10085 if (!checkonly) 10086 tcp->tcp_second_ctimer_threshold = *i1; 10087 break; 10088 case TCP_RECVDSTADDR: 10089 if (tcp->tcp_state > TCPS_LISTEN) 10090 return (EOPNOTSUPP); 10091 if (!checkonly) 10092 tcp->tcp_recvdstaddr = onoff; 10093 break; 10094 case TCP_ANONPRIVBIND: 10095 if ((reterr = secpolicy_net_privaddr(cr, 0, 10096 IPPROTO_TCP)) != 0) { 10097 *outlenp = 0; 10098 return (reterr); 10099 } 10100 if (!checkonly) { 10101 tcp->tcp_anon_priv_bind = onoff; 10102 } 10103 break; 10104 case TCP_EXCLBIND: 10105 if (!checkonly) 10106 tcp->tcp_exclbind = onoff; 10107 break; /* goto sizeof (int) option return */ 10108 case TCP_INIT_CWND: { 10109 uint32_t init_cwnd = *((uint32_t *)invalp); 10110 10111 if (checkonly) 10112 break; 10113 10114 /* 10115 * Only allow socket with network configuration 10116 * privilege to set the initial cwnd to be larger 10117 * than allowed by RFC 3390. 10118 */ 10119 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10120 tcp->tcp_init_cwnd = init_cwnd; 10121 break; 10122 } 10123 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10124 *outlenp = 0; 10125 return (reterr); 10126 } 10127 if (init_cwnd > TCP_MAX_INIT_CWND) { 10128 *outlenp = 0; 10129 return (EINVAL); 10130 } 10131 tcp->tcp_init_cwnd = init_cwnd; 10132 break; 10133 } 10134 case TCP_KEEPALIVE_THRESHOLD: 10135 if (checkonly) 10136 break; 10137 10138 if (*i1 < tcps->tcps_keepalive_interval_low || 10139 *i1 > tcps->tcps_keepalive_interval_high) { 10140 *outlenp = 0; 10141 return (EINVAL); 10142 } 10143 if (*i1 != tcp->tcp_ka_interval) { 10144 tcp->tcp_ka_interval = *i1; 10145 /* 10146 * Check if we need to restart the 10147 * keepalive timer. 10148 */ 10149 if (tcp->tcp_ka_tid != 0) { 10150 ASSERT(tcp->tcp_ka_enabled); 10151 (void) TCP_TIMER_CANCEL(tcp, 10152 tcp->tcp_ka_tid); 10153 tcp->tcp_ka_last_intrvl = 0; 10154 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10155 tcp_keepalive_killer, 10156 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10157 } 10158 } 10159 break; 10160 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10161 if (!checkonly) { 10162 if (*i1 < 10163 tcps->tcps_keepalive_abort_interval_low || 10164 *i1 > 10165 tcps->tcps_keepalive_abort_interval_high) { 10166 *outlenp = 0; 10167 return (EINVAL); 10168 } 10169 tcp->tcp_ka_abort_thres = *i1; 10170 } 10171 break; 10172 case TCP_CORK: 10173 if (!checkonly) { 10174 /* 10175 * if tcp->tcp_cork was set and is now 10176 * being unset, we have to make sure that 10177 * the remaining data gets sent out. Also 10178 * unset tcp->tcp_cork so that tcp_wput_data() 10179 * can send data even if it is less than mss 10180 */ 10181 if (tcp->tcp_cork && onoff == 0 && 10182 tcp->tcp_unsent > 0) { 10183 tcp->tcp_cork = B_FALSE; 10184 tcp_wput_data(tcp, NULL, B_FALSE); 10185 } 10186 tcp->tcp_cork = onoff; 10187 } 10188 break; 10189 default: 10190 *outlenp = 0; 10191 return (EINVAL); 10192 } 10193 break; 10194 case IPPROTO_IP: 10195 if (tcp->tcp_family != AF_INET) { 10196 *outlenp = 0; 10197 return (ENOPROTOOPT); 10198 } 10199 switch (name) { 10200 case IP_OPTIONS: 10201 case T_IP_OPTIONS: 10202 reterr = tcp_opt_set_header(tcp, checkonly, 10203 invalp, inlen); 10204 if (reterr) { 10205 *outlenp = 0; 10206 return (reterr); 10207 } 10208 /* OK return - copy input buffer into output buffer */ 10209 if (invalp != outvalp) { 10210 /* don't trust bcopy for identical src/dst */ 10211 bcopy(invalp, outvalp, inlen); 10212 } 10213 *outlenp = inlen; 10214 return (0); 10215 case IP_TOS: 10216 case T_IP_TOS: 10217 if (!checkonly) { 10218 tcp->tcp_ipha->ipha_type_of_service = 10219 (uchar_t)*i1; 10220 tcp->tcp_tos = (uchar_t)*i1; 10221 } 10222 break; 10223 case IP_TTL: 10224 if (!checkonly) { 10225 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10226 tcp->tcp_ttl = (uchar_t)*i1; 10227 } 10228 break; 10229 case IP_BOUND_IF: 10230 case IP_NEXTHOP: 10231 /* Handled at the IP level */ 10232 return (-EINVAL); 10233 case IP_SEC_OPT: 10234 /* 10235 * We should not allow policy setting after 10236 * we start listening for connections. 10237 */ 10238 if (tcp->tcp_state == TCPS_LISTEN) { 10239 return (EINVAL); 10240 } else { 10241 /* Handled at the IP level */ 10242 return (-EINVAL); 10243 } 10244 default: 10245 *outlenp = 0; 10246 return (EINVAL); 10247 } 10248 break; 10249 case IPPROTO_IPV6: { 10250 ip6_pkt_t *ipp; 10251 10252 /* 10253 * IPPROTO_IPV6 options are only supported for sockets 10254 * that are using IPv6 on the wire. 10255 */ 10256 if (tcp->tcp_ipversion != IPV6_VERSION) { 10257 *outlenp = 0; 10258 return (ENOPROTOOPT); 10259 } 10260 /* 10261 * Only sticky options; no ancillary data 10262 */ 10263 ipp = &tcp->tcp_sticky_ipp; 10264 10265 switch (name) { 10266 case IPV6_UNICAST_HOPS: 10267 /* -1 means use default */ 10268 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10269 *outlenp = 0; 10270 return (EINVAL); 10271 } 10272 if (!checkonly) { 10273 if (*i1 == -1) { 10274 tcp->tcp_ip6h->ip6_hops = 10275 ipp->ipp_unicast_hops = 10276 (uint8_t)tcps->tcps_ipv6_hoplimit; 10277 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10278 /* Pass modified value to IP. */ 10279 *i1 = tcp->tcp_ip6h->ip6_hops; 10280 } else { 10281 tcp->tcp_ip6h->ip6_hops = 10282 ipp->ipp_unicast_hops = 10283 (uint8_t)*i1; 10284 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10285 } 10286 reterr = tcp_build_hdrs(tcp); 10287 if (reterr != 0) 10288 return (reterr); 10289 } 10290 break; 10291 case IPV6_BOUND_IF: 10292 if (!checkonly) { 10293 tcp->tcp_bound_if = *i1; 10294 PASS_OPT_TO_IP(connp); 10295 } 10296 break; 10297 /* 10298 * Set boolean switches for ancillary data delivery 10299 */ 10300 case IPV6_RECVPKTINFO: 10301 if (!checkonly) { 10302 if (onoff) 10303 tcp->tcp_ipv6_recvancillary |= 10304 TCP_IPV6_RECVPKTINFO; 10305 else 10306 tcp->tcp_ipv6_recvancillary &= 10307 ~TCP_IPV6_RECVPKTINFO; 10308 /* Force it to be sent up with the next msg */ 10309 tcp->tcp_recvifindex = 0; 10310 PASS_OPT_TO_IP(connp); 10311 } 10312 break; 10313 case IPV6_RECVTCLASS: 10314 if (!checkonly) { 10315 if (onoff) 10316 tcp->tcp_ipv6_recvancillary |= 10317 TCP_IPV6_RECVTCLASS; 10318 else 10319 tcp->tcp_ipv6_recvancillary &= 10320 ~TCP_IPV6_RECVTCLASS; 10321 PASS_OPT_TO_IP(connp); 10322 } 10323 break; 10324 case IPV6_RECVHOPLIMIT: 10325 if (!checkonly) { 10326 if (onoff) 10327 tcp->tcp_ipv6_recvancillary |= 10328 TCP_IPV6_RECVHOPLIMIT; 10329 else 10330 tcp->tcp_ipv6_recvancillary &= 10331 ~TCP_IPV6_RECVHOPLIMIT; 10332 /* Force it to be sent up with the next msg */ 10333 tcp->tcp_recvhops = 0xffffffffU; 10334 PASS_OPT_TO_IP(connp); 10335 } 10336 break; 10337 case IPV6_RECVHOPOPTS: 10338 if (!checkonly) { 10339 if (onoff) 10340 tcp->tcp_ipv6_recvancillary |= 10341 TCP_IPV6_RECVHOPOPTS; 10342 else 10343 tcp->tcp_ipv6_recvancillary &= 10344 ~TCP_IPV6_RECVHOPOPTS; 10345 PASS_OPT_TO_IP(connp); 10346 } 10347 break; 10348 case IPV6_RECVDSTOPTS: 10349 if (!checkonly) { 10350 if (onoff) 10351 tcp->tcp_ipv6_recvancillary |= 10352 TCP_IPV6_RECVDSTOPTS; 10353 else 10354 tcp->tcp_ipv6_recvancillary &= 10355 ~TCP_IPV6_RECVDSTOPTS; 10356 PASS_OPT_TO_IP(connp); 10357 } 10358 break; 10359 case _OLD_IPV6_RECVDSTOPTS: 10360 if (!checkonly) { 10361 if (onoff) 10362 tcp->tcp_ipv6_recvancillary |= 10363 TCP_OLD_IPV6_RECVDSTOPTS; 10364 else 10365 tcp->tcp_ipv6_recvancillary &= 10366 ~TCP_OLD_IPV6_RECVDSTOPTS; 10367 } 10368 break; 10369 case IPV6_RECVRTHDR: 10370 if (!checkonly) { 10371 if (onoff) 10372 tcp->tcp_ipv6_recvancillary |= 10373 TCP_IPV6_RECVRTHDR; 10374 else 10375 tcp->tcp_ipv6_recvancillary &= 10376 ~TCP_IPV6_RECVRTHDR; 10377 PASS_OPT_TO_IP(connp); 10378 } 10379 break; 10380 case IPV6_RECVRTHDRDSTOPTS: 10381 if (!checkonly) { 10382 if (onoff) 10383 tcp->tcp_ipv6_recvancillary |= 10384 TCP_IPV6_RECVRTDSTOPTS; 10385 else 10386 tcp->tcp_ipv6_recvancillary &= 10387 ~TCP_IPV6_RECVRTDSTOPTS; 10388 PASS_OPT_TO_IP(connp); 10389 } 10390 break; 10391 case IPV6_PKTINFO: 10392 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10393 return (EINVAL); 10394 if (checkonly) 10395 break; 10396 10397 if (inlen == 0) { 10398 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10399 } else { 10400 struct in6_pktinfo *pkti; 10401 10402 pkti = (struct in6_pktinfo *)invalp; 10403 /* 10404 * RFC 3542 states that ipi6_addr must be 10405 * the unspecified address when setting the 10406 * IPV6_PKTINFO sticky socket option on a 10407 * TCP socket. 10408 */ 10409 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10410 return (EINVAL); 10411 /* 10412 * IP will validate the source address and 10413 * interface index. 10414 */ 10415 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 10416 reterr = ip_set_options(tcp->tcp_connp, 10417 level, name, invalp, inlen, cr); 10418 } else { 10419 reterr = ip6_set_pktinfo(cr, 10420 tcp->tcp_connp, pkti); 10421 } 10422 if (reterr != 0) 10423 return (reterr); 10424 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10425 ipp->ipp_addr = pkti->ipi6_addr; 10426 if (ipp->ipp_ifindex != 0) 10427 ipp->ipp_fields |= IPPF_IFINDEX; 10428 else 10429 ipp->ipp_fields &= ~IPPF_IFINDEX; 10430 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10431 ipp->ipp_fields |= IPPF_ADDR; 10432 else 10433 ipp->ipp_fields &= ~IPPF_ADDR; 10434 } 10435 reterr = tcp_build_hdrs(tcp); 10436 if (reterr != 0) 10437 return (reterr); 10438 break; 10439 case IPV6_TCLASS: 10440 if (inlen != 0 && inlen != sizeof (int)) 10441 return (EINVAL); 10442 if (checkonly) 10443 break; 10444 10445 if (inlen == 0) { 10446 ipp->ipp_fields &= ~IPPF_TCLASS; 10447 } else { 10448 if (*i1 > 255 || *i1 < -1) 10449 return (EINVAL); 10450 if (*i1 == -1) { 10451 ipp->ipp_tclass = 0; 10452 *i1 = 0; 10453 } else { 10454 ipp->ipp_tclass = *i1; 10455 } 10456 ipp->ipp_fields |= IPPF_TCLASS; 10457 } 10458 reterr = tcp_build_hdrs(tcp); 10459 if (reterr != 0) 10460 return (reterr); 10461 break; 10462 case IPV6_NEXTHOP: 10463 /* 10464 * IP will verify that the nexthop is reachable 10465 * and fail for sticky options. 10466 */ 10467 if (inlen != 0 && inlen != sizeof (sin6_t)) 10468 return (EINVAL); 10469 if (checkonly) 10470 break; 10471 10472 if (inlen == 0) { 10473 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10474 } else { 10475 sin6_t *sin6 = (sin6_t *)invalp; 10476 10477 if (sin6->sin6_family != AF_INET6) 10478 return (EAFNOSUPPORT); 10479 if (IN6_IS_ADDR_V4MAPPED( 10480 &sin6->sin6_addr)) 10481 return (EADDRNOTAVAIL); 10482 ipp->ipp_nexthop = sin6->sin6_addr; 10483 if (!IN6_IS_ADDR_UNSPECIFIED( 10484 &ipp->ipp_nexthop)) 10485 ipp->ipp_fields |= IPPF_NEXTHOP; 10486 else 10487 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10488 } 10489 reterr = tcp_build_hdrs(tcp); 10490 if (reterr != 0) 10491 return (reterr); 10492 PASS_OPT_TO_IP(connp); 10493 break; 10494 case IPV6_HOPOPTS: { 10495 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10496 10497 /* 10498 * Sanity checks - minimum size, size a multiple of 10499 * eight bytes, and matching size passed in. 10500 */ 10501 if (inlen != 0 && 10502 inlen != (8 * (hopts->ip6h_len + 1))) 10503 return (EINVAL); 10504 10505 if (checkonly) 10506 break; 10507 10508 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10509 (uchar_t **)&ipp->ipp_hopopts, 10510 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10511 if (reterr != 0) 10512 return (reterr); 10513 if (ipp->ipp_hopoptslen == 0) 10514 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10515 else 10516 ipp->ipp_fields |= IPPF_HOPOPTS; 10517 reterr = tcp_build_hdrs(tcp); 10518 if (reterr != 0) 10519 return (reterr); 10520 break; 10521 } 10522 case IPV6_RTHDRDSTOPTS: { 10523 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10524 10525 /* 10526 * Sanity checks - minimum size, size a multiple of 10527 * eight bytes, and matching size passed in. 10528 */ 10529 if (inlen != 0 && 10530 inlen != (8 * (dopts->ip6d_len + 1))) 10531 return (EINVAL); 10532 10533 if (checkonly) 10534 break; 10535 10536 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10537 (uchar_t **)&ipp->ipp_rtdstopts, 10538 &ipp->ipp_rtdstoptslen, 0); 10539 if (reterr != 0) 10540 return (reterr); 10541 if (ipp->ipp_rtdstoptslen == 0) 10542 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10543 else 10544 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10545 reterr = tcp_build_hdrs(tcp); 10546 if (reterr != 0) 10547 return (reterr); 10548 break; 10549 } 10550 case IPV6_DSTOPTS: { 10551 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10552 10553 /* 10554 * Sanity checks - minimum size, size a multiple of 10555 * eight bytes, and matching size passed in. 10556 */ 10557 if (inlen != 0 && 10558 inlen != (8 * (dopts->ip6d_len + 1))) 10559 return (EINVAL); 10560 10561 if (checkonly) 10562 break; 10563 10564 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10565 (uchar_t **)&ipp->ipp_dstopts, 10566 &ipp->ipp_dstoptslen, 0); 10567 if (reterr != 0) 10568 return (reterr); 10569 if (ipp->ipp_dstoptslen == 0) 10570 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10571 else 10572 ipp->ipp_fields |= IPPF_DSTOPTS; 10573 reterr = tcp_build_hdrs(tcp); 10574 if (reterr != 0) 10575 return (reterr); 10576 break; 10577 } 10578 case IPV6_RTHDR: { 10579 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10580 10581 /* 10582 * Sanity checks - minimum size, size a multiple of 10583 * eight bytes, and matching size passed in. 10584 */ 10585 if (inlen != 0 && 10586 inlen != (8 * (rt->ip6r_len + 1))) 10587 return (EINVAL); 10588 10589 if (checkonly) 10590 break; 10591 10592 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10593 (uchar_t **)&ipp->ipp_rthdr, 10594 &ipp->ipp_rthdrlen, 0); 10595 if (reterr != 0) 10596 return (reterr); 10597 if (ipp->ipp_rthdrlen == 0) 10598 ipp->ipp_fields &= ~IPPF_RTHDR; 10599 else 10600 ipp->ipp_fields |= IPPF_RTHDR; 10601 reterr = tcp_build_hdrs(tcp); 10602 if (reterr != 0) 10603 return (reterr); 10604 break; 10605 } 10606 case IPV6_V6ONLY: 10607 if (!checkonly) { 10608 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10609 } 10610 break; 10611 case IPV6_USE_MIN_MTU: 10612 if (inlen != sizeof (int)) 10613 return (EINVAL); 10614 10615 if (*i1 < -1 || *i1 > 1) 10616 return (EINVAL); 10617 10618 if (checkonly) 10619 break; 10620 10621 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10622 ipp->ipp_use_min_mtu = *i1; 10623 break; 10624 case IPV6_SEC_OPT: 10625 /* 10626 * We should not allow policy setting after 10627 * we start listening for connections. 10628 */ 10629 if (tcp->tcp_state == TCPS_LISTEN) { 10630 return (EINVAL); 10631 } else { 10632 /* Handled at the IP level */ 10633 return (-EINVAL); 10634 } 10635 case IPV6_SRC_PREFERENCES: 10636 if (inlen != sizeof (uint32_t)) 10637 return (EINVAL); 10638 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10639 *(uint32_t *)invalp); 10640 if (reterr != 0) { 10641 *outlenp = 0; 10642 return (reterr); 10643 } 10644 break; 10645 default: 10646 *outlenp = 0; 10647 return (EINVAL); 10648 } 10649 break; 10650 } /* end IPPROTO_IPV6 */ 10651 default: 10652 *outlenp = 0; 10653 return (EINVAL); 10654 } 10655 /* 10656 * Common case of OK return with outval same as inval 10657 */ 10658 if (invalp != outvalp) { 10659 /* don't trust bcopy for identical src/dst */ 10660 (void) bcopy(invalp, outvalp, inlen); 10661 } 10662 *outlenp = inlen; 10663 return (0); 10664 } 10665 10666 /* ARGSUSED */ 10667 int 10668 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10669 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10670 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10671 { 10672 conn_t *connp = Q_TO_CONN(q); 10673 10674 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10675 outlenp, outvalp, thisdg_attrs, cr, mblk)); 10676 } 10677 10678 int 10679 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10680 const void *optvalp, socklen_t optlen, cred_t *cr) 10681 { 10682 conn_t *connp = (conn_t *)proto_handle; 10683 squeue_t *sqp = connp->conn_sqp; 10684 int error; 10685 10686 ASSERT(connp->conn_upper_handle != NULL); 10687 /* 10688 * Entering the squeue synchronously can result in a context switch, 10689 * which can cause a rather sever performance degradation. So we try to 10690 * handle whatever options we can without entering the squeue. 10691 */ 10692 if (level == IPPROTO_TCP) { 10693 switch (option_name) { 10694 case TCP_NODELAY: 10695 if (optlen != sizeof (int32_t)) 10696 return (EINVAL); 10697 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10698 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10699 connp->conn_tcp->tcp_mss; 10700 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10701 return (0); 10702 default: 10703 break; 10704 } 10705 } 10706 10707 error = squeue_synch_enter(sqp, connp, NULL); 10708 if (error == ENOMEM) { 10709 return (ENOMEM); 10710 } 10711 10712 error = proto_opt_check(level, option_name, optlen, NULL, 10713 tcp_opt_obj.odb_opt_des_arr, 10714 tcp_opt_obj.odb_opt_arr_cnt, 10715 tcp_opt_obj.odb_topmost_tpiprovider, 10716 B_TRUE, B_FALSE, cr); 10717 10718 if (error != 0) { 10719 if (error < 0) { 10720 error = proto_tlitosyserr(-error); 10721 } 10722 squeue_synch_exit(sqp, connp); 10723 return (error); 10724 } 10725 10726 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10727 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10728 NULL, cr, NULL); 10729 squeue_synch_exit(sqp, connp); 10730 10731 if (error < 0) { 10732 /* 10733 * Pass on to ip 10734 */ 10735 error = ip_set_options(connp, level, option_name, optvalp, 10736 optlen, cr); 10737 } 10738 return (error); 10739 } 10740 10741 /* 10742 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10743 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10744 * headers, and the maximum size tcp header (to avoid reallocation 10745 * on the fly for additional tcp options). 10746 * Returns failure if can't allocate memory. 10747 */ 10748 static int 10749 tcp_build_hdrs(tcp_t *tcp) 10750 { 10751 char *hdrs; 10752 uint_t hdrs_len; 10753 ip6i_t *ip6i; 10754 char buf[TCP_MAX_HDR_LENGTH]; 10755 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10756 in6_addr_t src, dst; 10757 tcp_stack_t *tcps = tcp->tcp_tcps; 10758 conn_t *connp = tcp->tcp_connp; 10759 10760 /* 10761 * save the existing tcp header and source/dest IP addresses 10762 */ 10763 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10764 src = tcp->tcp_ip6h->ip6_src; 10765 dst = tcp->tcp_ip6h->ip6_dst; 10766 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10767 ASSERT(hdrs_len != 0); 10768 if (hdrs_len > tcp->tcp_iphc_len) { 10769 /* Need to reallocate */ 10770 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10771 if (hdrs == NULL) 10772 return (ENOMEM); 10773 if (tcp->tcp_iphc != NULL) { 10774 if (tcp->tcp_hdr_grown) { 10775 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10776 } else { 10777 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10778 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10779 } 10780 tcp->tcp_iphc_len = 0; 10781 } 10782 ASSERT(tcp->tcp_iphc_len == 0); 10783 tcp->tcp_iphc = hdrs; 10784 tcp->tcp_iphc_len = hdrs_len; 10785 tcp->tcp_hdr_grown = B_TRUE; 10786 } 10787 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10788 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10789 10790 /* Set header fields not in ipp */ 10791 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10792 ip6i = (ip6i_t *)tcp->tcp_iphc; 10793 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10794 } else { 10795 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10796 } 10797 /* 10798 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10799 * 10800 * tcp->tcp_tcp_hdr_len doesn't change here. 10801 */ 10802 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10803 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10804 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10805 10806 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10807 10808 tcp->tcp_ip6h->ip6_src = src; 10809 tcp->tcp_ip6h->ip6_dst = dst; 10810 10811 /* 10812 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10813 * the default value for TCP. 10814 */ 10815 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10816 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10817 10818 /* 10819 * If we're setting extension headers after a connection 10820 * has been established, and if we have a routing header 10821 * among the extension headers, call ip_massage_options_v6 to 10822 * manipulate the routing header/ip6_dst set the checksum 10823 * difference in the tcp header template. 10824 * (This happens in tcp_connect_ipv6 if the routing header 10825 * is set prior to the connect.) 10826 * Set the tcp_sum to zero first in case we've cleared a 10827 * routing header or don't have one at all. 10828 */ 10829 tcp->tcp_sum = 0; 10830 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10831 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10832 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10833 (uint8_t *)tcp->tcp_tcph); 10834 if (rth != NULL) { 10835 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10836 rth, tcps->tcps_netstack); 10837 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10838 (tcp->tcp_sum >> 16)); 10839 } 10840 } 10841 10842 /* Try to get everything in a single mblk */ 10843 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10844 hdrs_len + tcps->tcps_wroff_xtra); 10845 return (0); 10846 } 10847 10848 /* 10849 * Transfer any source route option from ipha to buf/dst in reversed form. 10850 */ 10851 static int 10852 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10853 { 10854 ipoptp_t opts; 10855 uchar_t *opt; 10856 uint8_t optval; 10857 uint8_t optlen; 10858 uint32_t len = 0; 10859 10860 for (optval = ipoptp_first(&opts, ipha); 10861 optval != IPOPT_EOL; 10862 optval = ipoptp_next(&opts)) { 10863 opt = opts.ipoptp_cur; 10864 optlen = opts.ipoptp_len; 10865 switch (optval) { 10866 int off1, off2; 10867 case IPOPT_SSRR: 10868 case IPOPT_LSRR: 10869 10870 /* Reverse source route */ 10871 /* 10872 * First entry should be the next to last one in the 10873 * current source route (the last entry is our 10874 * address.) 10875 * The last entry should be the final destination. 10876 */ 10877 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10878 buf[IPOPT_OLEN] = (uint8_t)optlen; 10879 off1 = IPOPT_MINOFF_SR - 1; 10880 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10881 if (off2 < 0) { 10882 /* No entries in source route */ 10883 break; 10884 } 10885 bcopy(opt + off2, dst, IP_ADDR_LEN); 10886 /* 10887 * Note: use src since ipha has not had its src 10888 * and dst reversed (it is in the state it was 10889 * received. 10890 */ 10891 bcopy(&ipha->ipha_src, buf + off2, 10892 IP_ADDR_LEN); 10893 off2 -= IP_ADDR_LEN; 10894 10895 while (off2 > 0) { 10896 bcopy(opt + off2, buf + off1, 10897 IP_ADDR_LEN); 10898 off1 += IP_ADDR_LEN; 10899 off2 -= IP_ADDR_LEN; 10900 } 10901 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 10902 buf += optlen; 10903 len += optlen; 10904 break; 10905 } 10906 } 10907 done: 10908 /* Pad the resulting options */ 10909 while (len & 0x3) { 10910 *buf++ = IPOPT_EOL; 10911 len++; 10912 } 10913 return (len); 10914 } 10915 10916 10917 /* 10918 * Extract and revert a source route from ipha (if any) 10919 * and then update the relevant fields in both tcp_t and the standard header. 10920 */ 10921 static void 10922 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 10923 { 10924 char buf[TCP_MAX_HDR_LENGTH]; 10925 uint_t tcph_len; 10926 int len; 10927 10928 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 10929 len = IPH_HDR_LENGTH(ipha); 10930 if (len == IP_SIMPLE_HDR_LENGTH) 10931 /* Nothing to do */ 10932 return; 10933 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 10934 (len & 0x3)) 10935 return; 10936 10937 tcph_len = tcp->tcp_tcp_hdr_len; 10938 bcopy(tcp->tcp_tcph, buf, tcph_len); 10939 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 10940 (tcp->tcp_ipha->ipha_dst & 0xffff); 10941 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 10942 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 10943 len += IP_SIMPLE_HDR_LENGTH; 10944 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 10945 (tcp->tcp_ipha->ipha_dst & 0xffff)); 10946 if ((int)tcp->tcp_sum < 0) 10947 tcp->tcp_sum--; 10948 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 10949 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 10950 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 10951 bcopy(buf, tcp->tcp_tcph, tcph_len); 10952 tcp->tcp_ip_hdr_len = len; 10953 tcp->tcp_ipha->ipha_version_and_hdr_length = 10954 (IP_VERSION << 4) | (len >> 2); 10955 len += tcph_len; 10956 tcp->tcp_hdr_len = len; 10957 } 10958 10959 /* 10960 * Copy the standard header into its new location, 10961 * lay in the new options and then update the relevant 10962 * fields in both tcp_t and the standard header. 10963 */ 10964 static int 10965 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 10966 { 10967 uint_t tcph_len; 10968 uint8_t *ip_optp; 10969 tcph_t *new_tcph; 10970 tcp_stack_t *tcps = tcp->tcp_tcps; 10971 conn_t *connp = tcp->tcp_connp; 10972 10973 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 10974 return (EINVAL); 10975 10976 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 10977 return (EINVAL); 10978 10979 if (checkonly) { 10980 /* 10981 * do not really set, just pretend to - T_CHECK 10982 */ 10983 return (0); 10984 } 10985 10986 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 10987 if (tcp->tcp_label_len > 0) { 10988 int padlen; 10989 uint8_t opt; 10990 10991 /* convert list termination to no-ops */ 10992 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 10993 ip_optp += ip_optp[IPOPT_OLEN]; 10994 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 10995 while (--padlen >= 0) 10996 *ip_optp++ = opt; 10997 } 10998 tcph_len = tcp->tcp_tcp_hdr_len; 10999 new_tcph = (tcph_t *)(ip_optp + len); 11000 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11001 tcp->tcp_tcph = new_tcph; 11002 bcopy(ptr, ip_optp, len); 11003 11004 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11005 11006 tcp->tcp_ip_hdr_len = len; 11007 tcp->tcp_ipha->ipha_version_and_hdr_length = 11008 (IP_VERSION << 4) | (len >> 2); 11009 tcp->tcp_hdr_len = len + tcph_len; 11010 if (!TCP_IS_DETACHED(tcp)) { 11011 /* Always allocate room for all options. */ 11012 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11013 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11014 } 11015 return (0); 11016 } 11017 11018 /* Get callback routine passed to nd_load by tcp_param_register */ 11019 /* ARGSUSED */ 11020 static int 11021 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11022 { 11023 tcpparam_t *tcppa = (tcpparam_t *)cp; 11024 11025 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11026 return (0); 11027 } 11028 11029 /* 11030 * Walk through the param array specified registering each element with the 11031 * named dispatch handler. 11032 */ 11033 static boolean_t 11034 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11035 { 11036 for (; cnt-- > 0; tcppa++) { 11037 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11038 if (!nd_load(ndp, tcppa->tcp_param_name, 11039 tcp_param_get, tcp_param_set, 11040 (caddr_t)tcppa)) { 11041 nd_free(ndp); 11042 return (B_FALSE); 11043 } 11044 } 11045 } 11046 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11047 KM_SLEEP); 11048 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11049 sizeof (tcpparam_t)); 11050 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11051 tcp_param_get, tcp_param_set_aligned, 11052 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11053 nd_free(ndp); 11054 return (B_FALSE); 11055 } 11056 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11057 KM_SLEEP); 11058 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11059 sizeof (tcpparam_t)); 11060 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11061 tcp_param_get, tcp_param_set_aligned, 11062 (caddr_t)tcps->tcps_mdt_head_param)) { 11063 nd_free(ndp); 11064 return (B_FALSE); 11065 } 11066 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11067 KM_SLEEP); 11068 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11069 sizeof (tcpparam_t)); 11070 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11071 tcp_param_get, tcp_param_set_aligned, 11072 (caddr_t)tcps->tcps_mdt_tail_param)) { 11073 nd_free(ndp); 11074 return (B_FALSE); 11075 } 11076 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11077 KM_SLEEP); 11078 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11079 sizeof (tcpparam_t)); 11080 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11081 tcp_param_get, tcp_param_set_aligned, 11082 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11083 nd_free(ndp); 11084 return (B_FALSE); 11085 } 11086 if (!nd_load(ndp, "tcp_extra_priv_ports", 11087 tcp_extra_priv_ports_get, NULL, NULL)) { 11088 nd_free(ndp); 11089 return (B_FALSE); 11090 } 11091 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11092 NULL, tcp_extra_priv_ports_add, NULL)) { 11093 nd_free(ndp); 11094 return (B_FALSE); 11095 } 11096 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11097 NULL, tcp_extra_priv_ports_del, NULL)) { 11098 nd_free(ndp); 11099 return (B_FALSE); 11100 } 11101 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11102 tcp_1948_phrase_set, NULL)) { 11103 nd_free(ndp); 11104 return (B_FALSE); 11105 } 11106 /* 11107 * Dummy ndd variables - only to convey obsolescence information 11108 * through printing of their name (no get or set routines) 11109 * XXX Remove in future releases ? 11110 */ 11111 if (!nd_load(ndp, 11112 "tcp_close_wait_interval(obsoleted - " 11113 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11114 nd_free(ndp); 11115 return (B_FALSE); 11116 } 11117 return (B_TRUE); 11118 } 11119 11120 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11121 /* ARGSUSED */ 11122 static int 11123 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11124 cred_t *cr) 11125 { 11126 long new_value; 11127 tcpparam_t *tcppa = (tcpparam_t *)cp; 11128 11129 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11130 new_value < tcppa->tcp_param_min || 11131 new_value > tcppa->tcp_param_max) { 11132 return (EINVAL); 11133 } 11134 /* 11135 * Need to make sure new_value is a multiple of 4. If it is not, 11136 * round it up. For future 64 bit requirement, we actually make it 11137 * a multiple of 8. 11138 */ 11139 if (new_value & 0x7) { 11140 new_value = (new_value & ~0x7) + 0x8; 11141 } 11142 tcppa->tcp_param_val = new_value; 11143 return (0); 11144 } 11145 11146 /* Set callback routine passed to nd_load by tcp_param_register */ 11147 /* ARGSUSED */ 11148 static int 11149 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11150 { 11151 long new_value; 11152 tcpparam_t *tcppa = (tcpparam_t *)cp; 11153 11154 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11155 new_value < tcppa->tcp_param_min || 11156 new_value > tcppa->tcp_param_max) { 11157 return (EINVAL); 11158 } 11159 tcppa->tcp_param_val = new_value; 11160 return (0); 11161 } 11162 11163 /* 11164 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11165 * is filled, return as much as we can. The message passed in may be 11166 * multi-part, chained using b_cont. "start" is the starting sequence 11167 * number for this piece. 11168 */ 11169 static mblk_t * 11170 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11171 { 11172 uint32_t end; 11173 mblk_t *mp1; 11174 mblk_t *mp2; 11175 mblk_t *next_mp; 11176 uint32_t u1; 11177 tcp_stack_t *tcps = tcp->tcp_tcps; 11178 11179 /* Walk through all the new pieces. */ 11180 do { 11181 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11182 (uintptr_t)INT_MAX); 11183 end = start + (int)(mp->b_wptr - mp->b_rptr); 11184 next_mp = mp->b_cont; 11185 if (start == end) { 11186 /* Empty. Blast it. */ 11187 freeb(mp); 11188 continue; 11189 } 11190 mp->b_cont = NULL; 11191 TCP_REASS_SET_SEQ(mp, start); 11192 TCP_REASS_SET_END(mp, end); 11193 mp1 = tcp->tcp_reass_tail; 11194 if (!mp1) { 11195 tcp->tcp_reass_tail = mp; 11196 tcp->tcp_reass_head = mp; 11197 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11198 UPDATE_MIB(&tcps->tcps_mib, 11199 tcpInDataUnorderBytes, end - start); 11200 continue; 11201 } 11202 /* New stuff completely beyond tail? */ 11203 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11204 /* Link it on end. */ 11205 mp1->b_cont = mp; 11206 tcp->tcp_reass_tail = mp; 11207 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11208 UPDATE_MIB(&tcps->tcps_mib, 11209 tcpInDataUnorderBytes, end - start); 11210 continue; 11211 } 11212 mp1 = tcp->tcp_reass_head; 11213 u1 = TCP_REASS_SEQ(mp1); 11214 /* New stuff at the front? */ 11215 if (SEQ_LT(start, u1)) { 11216 /* Yes... Check for overlap. */ 11217 mp->b_cont = mp1; 11218 tcp->tcp_reass_head = mp; 11219 tcp_reass_elim_overlap(tcp, mp); 11220 continue; 11221 } 11222 /* 11223 * The new piece fits somewhere between the head and tail. 11224 * We find our slot, where mp1 precedes us and mp2 trails. 11225 */ 11226 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11227 u1 = TCP_REASS_SEQ(mp2); 11228 if (SEQ_LEQ(start, u1)) 11229 break; 11230 } 11231 /* Link ourselves in */ 11232 mp->b_cont = mp2; 11233 mp1->b_cont = mp; 11234 11235 /* Trim overlap with following mblk(s) first */ 11236 tcp_reass_elim_overlap(tcp, mp); 11237 11238 /* Trim overlap with preceding mblk */ 11239 tcp_reass_elim_overlap(tcp, mp1); 11240 11241 } while (start = end, mp = next_mp); 11242 mp1 = tcp->tcp_reass_head; 11243 /* Anything ready to go? */ 11244 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11245 return (NULL); 11246 /* Eat what we can off the queue */ 11247 for (;;) { 11248 mp = mp1->b_cont; 11249 end = TCP_REASS_END(mp1); 11250 TCP_REASS_SET_SEQ(mp1, 0); 11251 TCP_REASS_SET_END(mp1, 0); 11252 if (!mp) { 11253 tcp->tcp_reass_tail = NULL; 11254 break; 11255 } 11256 if (end != TCP_REASS_SEQ(mp)) { 11257 mp1->b_cont = NULL; 11258 break; 11259 } 11260 mp1 = mp; 11261 } 11262 mp1 = tcp->tcp_reass_head; 11263 tcp->tcp_reass_head = mp; 11264 return (mp1); 11265 } 11266 11267 /* Eliminate any overlap that mp may have over later mblks */ 11268 static void 11269 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11270 { 11271 uint32_t end; 11272 mblk_t *mp1; 11273 uint32_t u1; 11274 tcp_stack_t *tcps = tcp->tcp_tcps; 11275 11276 end = TCP_REASS_END(mp); 11277 while ((mp1 = mp->b_cont) != NULL) { 11278 u1 = TCP_REASS_SEQ(mp1); 11279 if (!SEQ_GT(end, u1)) 11280 break; 11281 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11282 mp->b_wptr -= end - u1; 11283 TCP_REASS_SET_END(mp, u1); 11284 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11285 UPDATE_MIB(&tcps->tcps_mib, 11286 tcpInDataPartDupBytes, end - u1); 11287 break; 11288 } 11289 mp->b_cont = mp1->b_cont; 11290 TCP_REASS_SET_SEQ(mp1, 0); 11291 TCP_REASS_SET_END(mp1, 0); 11292 freeb(mp1); 11293 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11294 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11295 } 11296 if (!mp1) 11297 tcp->tcp_reass_tail = mp; 11298 } 11299 11300 static uint_t 11301 tcp_rwnd_reopen(tcp_t *tcp) 11302 { 11303 uint_t ret = 0; 11304 uint_t thwin; 11305 11306 /* Learn the latest rwnd information that we sent to the other side. */ 11307 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11308 << tcp->tcp_rcv_ws; 11309 /* This is peer's calculated send window (our receive window). */ 11310 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11311 /* 11312 * Increase the receive window to max. But we need to do receiver 11313 * SWS avoidance. This means that we need to check the increase of 11314 * of receive window is at least 1 MSS. 11315 */ 11316 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11317 /* 11318 * If the window that the other side knows is less than max 11319 * deferred acks segments, send an update immediately. 11320 */ 11321 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11322 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11323 ret = TH_ACK_NEEDED; 11324 } 11325 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11326 } 11327 return (ret); 11328 } 11329 11330 /* 11331 * Send up all messages queued on tcp_rcv_list. 11332 */ 11333 static uint_t 11334 tcp_rcv_drain(tcp_t *tcp) 11335 { 11336 mblk_t *mp; 11337 uint_t ret = 0; 11338 #ifdef DEBUG 11339 uint_t cnt = 0; 11340 #endif 11341 queue_t *q = tcp->tcp_rq; 11342 11343 /* Can't drain on an eager connection */ 11344 if (tcp->tcp_listener != NULL) 11345 return (ret); 11346 11347 /* Can't be a non-STREAMS connection */ 11348 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 11349 11350 /* No need for the push timer now. */ 11351 if (tcp->tcp_push_tid != 0) { 11352 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11353 tcp->tcp_push_tid = 0; 11354 } 11355 11356 /* 11357 * Handle two cases here: we are currently fused or we were 11358 * previously fused and have some urgent data to be delivered 11359 * upstream. The latter happens because we either ran out of 11360 * memory or were detached and therefore sending the SIGURG was 11361 * deferred until this point. In either case we pass control 11362 * over to tcp_fuse_rcv_drain() since it may need to complete 11363 * some work. 11364 */ 11365 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11366 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11367 tcp->tcp_fused_sigurg_mp != NULL); 11368 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11369 &tcp->tcp_fused_sigurg_mp)) 11370 return (ret); 11371 } 11372 11373 while ((mp = tcp->tcp_rcv_list) != NULL) { 11374 tcp->tcp_rcv_list = mp->b_next; 11375 mp->b_next = NULL; 11376 #ifdef DEBUG 11377 cnt += msgdsize(mp); 11378 #endif 11379 /* Does this need SSL processing first? */ 11380 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11381 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11382 mblk_t *, mp); 11383 tcp_kssl_input(tcp, mp); 11384 continue; 11385 } 11386 putnext(q, mp); 11387 } 11388 #ifdef DEBUG 11389 ASSERT(cnt == tcp->tcp_rcv_cnt); 11390 #endif 11391 tcp->tcp_rcv_last_head = NULL; 11392 tcp->tcp_rcv_last_tail = NULL; 11393 tcp->tcp_rcv_cnt = 0; 11394 11395 if (canputnext(q)) 11396 return (tcp_rwnd_reopen(tcp)); 11397 11398 return (ret); 11399 } 11400 11401 /* 11402 * Queue data on tcp_rcv_list which is a b_next chain. 11403 * tcp_rcv_last_head/tail is the last element of this chain. 11404 * Each element of the chain is a b_cont chain. 11405 * 11406 * M_DATA messages are added to the current element. 11407 * Other messages are added as new (b_next) elements. 11408 */ 11409 void 11410 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11411 { 11412 ASSERT(seg_len == msgdsize(mp)); 11413 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11414 11415 if (tcp->tcp_rcv_list == NULL) { 11416 ASSERT(tcp->tcp_rcv_last_head == NULL); 11417 tcp->tcp_rcv_list = mp; 11418 tcp->tcp_rcv_last_head = mp; 11419 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11420 tcp->tcp_rcv_last_tail->b_cont = mp; 11421 } else { 11422 tcp->tcp_rcv_last_head->b_next = mp; 11423 tcp->tcp_rcv_last_head = mp; 11424 } 11425 11426 while (mp->b_cont) 11427 mp = mp->b_cont; 11428 11429 tcp->tcp_rcv_last_tail = mp; 11430 tcp->tcp_rcv_cnt += seg_len; 11431 tcp->tcp_rwnd -= seg_len; 11432 } 11433 11434 /* 11435 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11436 * 11437 * This is the default entry function into TCP on the read side. TCP is 11438 * always entered via squeue i.e. using squeue's for mutual exclusion. 11439 * When classifier does a lookup to find the tcp, it also puts a reference 11440 * on the conn structure associated so the tcp is guaranteed to exist 11441 * when we come here. We still need to check the state because it might 11442 * as well has been closed. The squeue processing function i.e. squeue_enter, 11443 * is responsible for doing the CONN_DEC_REF. 11444 * 11445 * Apart from the default entry point, IP also sends packets directly to 11446 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11447 * connections. 11448 */ 11449 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11450 void 11451 tcp_input(void *arg, mblk_t *mp, void *arg2) 11452 { 11453 conn_t *connp = (conn_t *)arg; 11454 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11455 11456 /* arg2 is the sqp */ 11457 ASSERT(arg2 != NULL); 11458 ASSERT(mp != NULL); 11459 11460 /* 11461 * Don't accept any input on a closed tcp as this TCP logically does 11462 * not exist on the system. Don't proceed further with this TCP. 11463 * For eg. this packet could trigger another close of this tcp 11464 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11465 * tcp_clean_death / tcp_closei_local must be called at most once 11466 * on a TCP. In this case we need to refeed the packet into the 11467 * classifier and figure out where the packet should go. Need to 11468 * preserve the recv_ill somehow. Until we figure that out, for 11469 * now just drop the packet if we can't classify the packet. 11470 */ 11471 if (tcp->tcp_state == TCPS_CLOSED || 11472 tcp->tcp_state == TCPS_BOUND) { 11473 conn_t *new_connp; 11474 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11475 11476 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11477 if (new_connp != NULL) { 11478 tcp_reinput(new_connp, mp, arg2); 11479 return; 11480 } 11481 /* We failed to classify. For now just drop the packet */ 11482 freemsg(mp); 11483 return; 11484 } 11485 11486 if (DB_TYPE(mp) != M_DATA) { 11487 tcp_rput_common(tcp, mp); 11488 return; 11489 } 11490 11491 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11492 squeue_t *final_sqp; 11493 11494 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11495 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11496 DB_CKSUMSTART(mp) = 0; 11497 if (tcp->tcp_state == TCPS_SYN_SENT && 11498 connp->conn_final_sqp == NULL && 11499 tcp_outbound_squeue_switch) { 11500 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11501 connp->conn_final_sqp = final_sqp; 11502 if (connp->conn_final_sqp != connp->conn_sqp) { 11503 CONN_INC_REF(connp); 11504 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11505 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11506 tcp_rput_data, connp, ip_squeue_flag, 11507 SQTAG_CONNECT_FINISH); 11508 return; 11509 } 11510 } 11511 } 11512 tcp_rput_data(connp, mp, arg2); 11513 } 11514 11515 /* 11516 * The read side put procedure. 11517 * The packets passed up by ip are assume to be aligned according to 11518 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11519 */ 11520 static void 11521 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11522 { 11523 /* 11524 * tcp_rput_data() does not expect M_CTL except for the case 11525 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11526 * type. Need to make sure that any other M_CTLs don't make 11527 * it to tcp_rput_data since it is not expecting any and doesn't 11528 * check for it. 11529 */ 11530 if (DB_TYPE(mp) == M_CTL) { 11531 switch (*(uint32_t *)(mp->b_rptr)) { 11532 case TCP_IOC_ABORT_CONN: 11533 /* 11534 * Handle connection abort request. 11535 */ 11536 tcp_ioctl_abort_handler(tcp, mp); 11537 return; 11538 case IPSEC_IN: 11539 /* 11540 * Only secure icmp arrive in TCP and they 11541 * don't go through data path. 11542 */ 11543 tcp_icmp_error(tcp, mp); 11544 return; 11545 case IN_PKTINFO: 11546 /* 11547 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11548 * sockets that are receiving IPv4 traffic. tcp 11549 */ 11550 ASSERT(tcp->tcp_family == AF_INET6); 11551 ASSERT(tcp->tcp_ipv6_recvancillary & 11552 TCP_IPV6_RECVPKTINFO); 11553 tcp_rput_data(tcp->tcp_connp, mp, 11554 tcp->tcp_connp->conn_sqp); 11555 return; 11556 case MDT_IOC_INFO_UPDATE: 11557 /* 11558 * Handle Multidata information update; the 11559 * following routine will free the message. 11560 */ 11561 if (tcp->tcp_connp->conn_mdt_ok) { 11562 tcp_mdt_update(tcp, 11563 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11564 B_FALSE); 11565 } 11566 freemsg(mp); 11567 return; 11568 case LSO_IOC_INFO_UPDATE: 11569 /* 11570 * Handle LSO information update; the following 11571 * routine will free the message. 11572 */ 11573 if (tcp->tcp_connp->conn_lso_ok) { 11574 tcp_lso_update(tcp, 11575 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11576 } 11577 freemsg(mp); 11578 return; 11579 default: 11580 /* 11581 * tcp_icmp_err() will process the M_CTL packets. 11582 * Non-ICMP packets, if any, will be discarded in 11583 * tcp_icmp_err(). We will process the ICMP packet 11584 * even if we are TCP_IS_DETACHED_NONEAGER as the 11585 * incoming ICMP packet may result in changing 11586 * the tcp_mss, which we would need if we have 11587 * packets to retransmit. 11588 */ 11589 tcp_icmp_error(tcp, mp); 11590 return; 11591 } 11592 } 11593 11594 /* No point processing the message if tcp is already closed */ 11595 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11596 freemsg(mp); 11597 return; 11598 } 11599 11600 tcp_rput_other(tcp, mp); 11601 } 11602 11603 11604 /* The minimum of smoothed mean deviation in RTO calculation. */ 11605 #define TCP_SD_MIN 400 11606 11607 /* 11608 * Set RTO for this connection. The formula is from Jacobson and Karels' 11609 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11610 * are the same as those in Appendix A.2 of that paper. 11611 * 11612 * m = new measurement 11613 * sa = smoothed RTT average (8 * average estimates). 11614 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11615 */ 11616 static void 11617 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11618 { 11619 long m = TICK_TO_MSEC(rtt); 11620 clock_t sa = tcp->tcp_rtt_sa; 11621 clock_t sv = tcp->tcp_rtt_sd; 11622 clock_t rto; 11623 tcp_stack_t *tcps = tcp->tcp_tcps; 11624 11625 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11626 tcp->tcp_rtt_update++; 11627 11628 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11629 if (sa != 0) { 11630 /* 11631 * Update average estimator: 11632 * new rtt = 7/8 old rtt + 1/8 Error 11633 */ 11634 11635 /* m is now Error in estimate. */ 11636 m -= sa >> 3; 11637 if ((sa += m) <= 0) { 11638 /* 11639 * Don't allow the smoothed average to be negative. 11640 * We use 0 to denote reinitialization of the 11641 * variables. 11642 */ 11643 sa = 1; 11644 } 11645 11646 /* 11647 * Update deviation estimator: 11648 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11649 */ 11650 if (m < 0) 11651 m = -m; 11652 m -= sv >> 2; 11653 sv += m; 11654 } else { 11655 /* 11656 * This follows BSD's implementation. So the reinitialized 11657 * RTO is 3 * m. We cannot go less than 2 because if the 11658 * link is bandwidth dominated, doubling the window size 11659 * during slow start means doubling the RTT. We want to be 11660 * more conservative when we reinitialize our estimates. 3 11661 * is just a convenient number. 11662 */ 11663 sa = m << 3; 11664 sv = m << 1; 11665 } 11666 if (sv < TCP_SD_MIN) { 11667 /* 11668 * We do not know that if sa captures the delay ACK 11669 * effect as in a long train of segments, a receiver 11670 * does not delay its ACKs. So set the minimum of sv 11671 * to be TCP_SD_MIN, which is default to 400 ms, twice 11672 * of BSD DATO. That means the minimum of mean 11673 * deviation is 100 ms. 11674 * 11675 */ 11676 sv = TCP_SD_MIN; 11677 } 11678 tcp->tcp_rtt_sa = sa; 11679 tcp->tcp_rtt_sd = sv; 11680 /* 11681 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11682 * 11683 * Add tcp_rexmit_interval extra in case of extreme environment 11684 * where the algorithm fails to work. The default value of 11685 * tcp_rexmit_interval_extra should be 0. 11686 * 11687 * As we use a finer grained clock than BSD and update 11688 * RTO for every ACKs, add in another .25 of RTT to the 11689 * deviation of RTO to accomodate burstiness of 1/4 of 11690 * window size. 11691 */ 11692 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 11693 11694 if (rto > tcps->tcps_rexmit_interval_max) { 11695 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 11696 } else if (rto < tcps->tcps_rexmit_interval_min) { 11697 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 11698 } else { 11699 tcp->tcp_rto = rto; 11700 } 11701 11702 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11703 tcp->tcp_timer_backoff = 0; 11704 } 11705 11706 /* 11707 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11708 * send queue which starts at the given seq. no. 11709 * 11710 * Parameters: 11711 * tcp_t *tcp: the tcp instance pointer. 11712 * uint32_t seq: the starting seq. no of the requested segment. 11713 * int32_t *off: after the execution, *off will be the offset to 11714 * the returned mblk which points to the requested seq no. 11715 * It is the caller's responsibility to send in a non-null off. 11716 * 11717 * Return: 11718 * A mblk_t pointer pointing to the requested segment in send queue. 11719 */ 11720 static mblk_t * 11721 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11722 { 11723 int32_t cnt; 11724 mblk_t *mp; 11725 11726 /* Defensive coding. Make sure we don't send incorrect data. */ 11727 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 11728 return (NULL); 11729 11730 cnt = seq - tcp->tcp_suna; 11731 mp = tcp->tcp_xmit_head; 11732 while (cnt > 0 && mp != NULL) { 11733 cnt -= mp->b_wptr - mp->b_rptr; 11734 if (cnt < 0) { 11735 cnt += mp->b_wptr - mp->b_rptr; 11736 break; 11737 } 11738 mp = mp->b_cont; 11739 } 11740 ASSERT(mp != NULL); 11741 *off = cnt; 11742 return (mp); 11743 } 11744 11745 /* 11746 * This function handles all retransmissions if SACK is enabled for this 11747 * connection. First it calculates how many segments can be retransmitted 11748 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11749 * segments. A segment is eligible if sack_cnt for that segment is greater 11750 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11751 * all eligible segments, it checks to see if TCP can send some new segments 11752 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11753 * 11754 * Parameters: 11755 * tcp_t *tcp: the tcp structure of the connection. 11756 * uint_t *flags: in return, appropriate value will be set for 11757 * tcp_rput_data(). 11758 */ 11759 static void 11760 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11761 { 11762 notsack_blk_t *notsack_blk; 11763 int32_t usable_swnd; 11764 int32_t mss; 11765 uint32_t seg_len; 11766 mblk_t *xmit_mp; 11767 tcp_stack_t *tcps = tcp->tcp_tcps; 11768 11769 ASSERT(tcp->tcp_sack_info != NULL); 11770 ASSERT(tcp->tcp_notsack_list != NULL); 11771 ASSERT(tcp->tcp_rexmit == B_FALSE); 11772 11773 /* Defensive coding in case there is a bug... */ 11774 if (tcp->tcp_notsack_list == NULL) { 11775 return; 11776 } 11777 notsack_blk = tcp->tcp_notsack_list; 11778 mss = tcp->tcp_mss; 11779 11780 /* 11781 * Limit the num of outstanding data in the network to be 11782 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11783 */ 11784 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11785 11786 /* At least retransmit 1 MSS of data. */ 11787 if (usable_swnd <= 0) { 11788 usable_swnd = mss; 11789 } 11790 11791 /* Make sure no new RTT samples will be taken. */ 11792 tcp->tcp_csuna = tcp->tcp_snxt; 11793 11794 notsack_blk = tcp->tcp_notsack_list; 11795 while (usable_swnd > 0) { 11796 mblk_t *snxt_mp, *tmp_mp; 11797 tcp_seq begin = tcp->tcp_sack_snxt; 11798 tcp_seq end; 11799 int32_t off; 11800 11801 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11802 if (SEQ_GT(notsack_blk->end, begin) && 11803 (notsack_blk->sack_cnt >= 11804 tcps->tcps_dupack_fast_retransmit)) { 11805 end = notsack_blk->end; 11806 if (SEQ_LT(begin, notsack_blk->begin)) { 11807 begin = notsack_blk->begin; 11808 } 11809 break; 11810 } 11811 } 11812 /* 11813 * All holes are filled. Manipulate tcp_cwnd to send more 11814 * if we can. Note that after the SACK recovery, tcp_cwnd is 11815 * set to tcp_cwnd_ssthresh. 11816 */ 11817 if (notsack_blk == NULL) { 11818 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11819 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11820 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11821 ASSERT(tcp->tcp_cwnd > 0); 11822 return; 11823 } else { 11824 usable_swnd = usable_swnd / mss; 11825 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 11826 MAX(usable_swnd * mss, mss); 11827 *flags |= TH_XMIT_NEEDED; 11828 return; 11829 } 11830 } 11831 11832 /* 11833 * Note that we may send more than usable_swnd allows here 11834 * because of round off, but no more than 1 MSS of data. 11835 */ 11836 seg_len = end - begin; 11837 if (seg_len > mss) 11838 seg_len = mss; 11839 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 11840 ASSERT(snxt_mp != NULL); 11841 /* This should not happen. Defensive coding again... */ 11842 if (snxt_mp == NULL) { 11843 return; 11844 } 11845 11846 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 11847 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 11848 if (xmit_mp == NULL) 11849 return; 11850 11851 usable_swnd -= seg_len; 11852 tcp->tcp_pipe += seg_len; 11853 tcp->tcp_sack_snxt = begin + seg_len; 11854 11855 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11856 11857 /* 11858 * Update the send timestamp to avoid false retransmission. 11859 */ 11860 snxt_mp->b_prev = (mblk_t *)lbolt; 11861 11862 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 11863 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 11864 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 11865 /* 11866 * Update tcp_rexmit_max to extend this SACK recovery phase. 11867 * This happens when new data sent during fast recovery is 11868 * also lost. If TCP retransmits those new data, it needs 11869 * to extend SACK recover phase to avoid starting another 11870 * fast retransmit/recovery unnecessarily. 11871 */ 11872 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 11873 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 11874 } 11875 } 11876 } 11877 11878 /* 11879 * This function handles policy checking at TCP level for non-hard_bound/ 11880 * detached connections. 11881 */ 11882 static boolean_t 11883 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 11884 boolean_t secure, boolean_t mctl_present) 11885 { 11886 ipsec_latch_t *ipl = NULL; 11887 ipsec_action_t *act = NULL; 11888 mblk_t *data_mp; 11889 ipsec_in_t *ii; 11890 const char *reason; 11891 kstat_named_t *counter; 11892 tcp_stack_t *tcps = tcp->tcp_tcps; 11893 ipsec_stack_t *ipss; 11894 ip_stack_t *ipst; 11895 11896 ASSERT(mctl_present || !secure); 11897 11898 ASSERT((ipha == NULL && ip6h != NULL) || 11899 (ip6h == NULL && ipha != NULL)); 11900 11901 /* 11902 * We don't necessarily have an ipsec_in_act action to verify 11903 * policy because of assymetrical policy where we have only 11904 * outbound policy and no inbound policy (possible with global 11905 * policy). 11906 */ 11907 if (!secure) { 11908 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 11909 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 11910 return (B_TRUE); 11911 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 11912 "tcp_check_policy", ipha, ip6h, secure, 11913 tcps->tcps_netstack); 11914 ipss = tcps->tcps_netstack->netstack_ipsec; 11915 11916 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11917 DROPPER(ipss, ipds_tcp_clear), 11918 &tcps->tcps_dropper); 11919 return (B_FALSE); 11920 } 11921 11922 /* 11923 * We have a secure packet. 11924 */ 11925 if (act == NULL) { 11926 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 11927 "tcp_check_policy", ipha, ip6h, secure, 11928 tcps->tcps_netstack); 11929 ipss = tcps->tcps_netstack->netstack_ipsec; 11930 11931 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11932 DROPPER(ipss, ipds_tcp_secure), 11933 &tcps->tcps_dropper); 11934 return (B_FALSE); 11935 } 11936 11937 /* 11938 * XXX This whole routine is currently incorrect. ipl should 11939 * be set to the latch pointer, but is currently not set, so 11940 * we initialize it to NULL to avoid picking up random garbage. 11941 */ 11942 if (ipl == NULL) 11943 return (B_TRUE); 11944 11945 data_mp = first_mp->b_cont; 11946 11947 ii = (ipsec_in_t *)first_mp->b_rptr; 11948 11949 ipst = tcps->tcps_netstack->netstack_ip; 11950 11951 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 11952 &counter, tcp->tcp_connp)) { 11953 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 11954 return (B_TRUE); 11955 } 11956 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 11957 "tcp inbound policy mismatch: %s, packet dropped\n", 11958 reason); 11959 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 11960 11961 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 11962 &tcps->tcps_dropper); 11963 return (B_FALSE); 11964 } 11965 11966 /* 11967 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 11968 * retransmission after a timeout. 11969 * 11970 * To limit the number of duplicate segments, we limit the number of segment 11971 * to be sent in one time to tcp_snd_burst, the burst variable. 11972 */ 11973 static void 11974 tcp_ss_rexmit(tcp_t *tcp) 11975 { 11976 uint32_t snxt; 11977 uint32_t smax; 11978 int32_t win; 11979 int32_t mss; 11980 int32_t off; 11981 int32_t burst = tcp->tcp_snd_burst; 11982 mblk_t *snxt_mp; 11983 tcp_stack_t *tcps = tcp->tcp_tcps; 11984 11985 /* 11986 * Note that tcp_rexmit can be set even though TCP has retransmitted 11987 * all unack'ed segments. 11988 */ 11989 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 11990 smax = tcp->tcp_rexmit_max; 11991 snxt = tcp->tcp_rexmit_nxt; 11992 if (SEQ_LT(snxt, tcp->tcp_suna)) { 11993 snxt = tcp->tcp_suna; 11994 } 11995 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 11996 win -= snxt - tcp->tcp_suna; 11997 mss = tcp->tcp_mss; 11998 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 11999 12000 while (SEQ_LT(snxt, smax) && (win > 0) && 12001 (burst > 0) && (snxt_mp != NULL)) { 12002 mblk_t *xmit_mp; 12003 mblk_t *old_snxt_mp = snxt_mp; 12004 uint32_t cnt = mss; 12005 12006 if (win < cnt) { 12007 cnt = win; 12008 } 12009 if (SEQ_GT(snxt + cnt, smax)) { 12010 cnt = smax - snxt; 12011 } 12012 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12013 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12014 if (xmit_mp == NULL) 12015 return; 12016 12017 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12018 12019 snxt += cnt; 12020 win -= cnt; 12021 /* 12022 * Update the send timestamp to avoid false 12023 * retransmission. 12024 */ 12025 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12026 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12027 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12028 12029 tcp->tcp_rexmit_nxt = snxt; 12030 burst--; 12031 } 12032 /* 12033 * If we have transmitted all we have at the time 12034 * we started the retranmission, we can leave 12035 * the rest of the job to tcp_wput_data(). But we 12036 * need to check the send window first. If the 12037 * win is not 0, go on with tcp_wput_data(). 12038 */ 12039 if (SEQ_LT(snxt, smax) || win == 0) { 12040 return; 12041 } 12042 } 12043 /* Only call tcp_wput_data() if there is data to be sent. */ 12044 if (tcp->tcp_unsent) { 12045 tcp_wput_data(tcp, NULL, B_FALSE); 12046 } 12047 } 12048 12049 /* 12050 * Process all TCP option in SYN segment. Note that this function should 12051 * be called after tcp_adapt_ire() is called so that the necessary info 12052 * from IRE is already set in the tcp structure. 12053 * 12054 * This function sets up the correct tcp_mss value according to the 12055 * MSS option value and our header size. It also sets up the window scale 12056 * and timestamp values, and initialize SACK info blocks. But it does not 12057 * change receive window size after setting the tcp_mss value. The caller 12058 * should do the appropriate change. 12059 */ 12060 void 12061 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12062 { 12063 int options; 12064 tcp_opt_t tcpopt; 12065 uint32_t mss_max; 12066 char *tmp_tcph; 12067 tcp_stack_t *tcps = tcp->tcp_tcps; 12068 12069 tcpopt.tcp = NULL; 12070 options = tcp_parse_options(tcph, &tcpopt); 12071 12072 /* 12073 * Process MSS option. Note that MSS option value does not account 12074 * for IP or TCP options. This means that it is equal to MTU - minimum 12075 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12076 * IPv6. 12077 */ 12078 if (!(options & TCP_OPT_MSS_PRESENT)) { 12079 if (tcp->tcp_ipversion == IPV4_VERSION) 12080 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12081 else 12082 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12083 } else { 12084 if (tcp->tcp_ipversion == IPV4_VERSION) 12085 mss_max = tcps->tcps_mss_max_ipv4; 12086 else 12087 mss_max = tcps->tcps_mss_max_ipv6; 12088 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12089 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12090 else if (tcpopt.tcp_opt_mss > mss_max) 12091 tcpopt.tcp_opt_mss = mss_max; 12092 } 12093 12094 /* Process Window Scale option. */ 12095 if (options & TCP_OPT_WSCALE_PRESENT) { 12096 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12097 tcp->tcp_snd_ws_ok = B_TRUE; 12098 } else { 12099 tcp->tcp_snd_ws = B_FALSE; 12100 tcp->tcp_snd_ws_ok = B_FALSE; 12101 tcp->tcp_rcv_ws = B_FALSE; 12102 } 12103 12104 /* Process Timestamp option. */ 12105 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12106 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12107 tmp_tcph = (char *)tcp->tcp_tcph; 12108 12109 tcp->tcp_snd_ts_ok = B_TRUE; 12110 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12111 tcp->tcp_last_rcv_lbolt = lbolt64; 12112 ASSERT(OK_32PTR(tmp_tcph)); 12113 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12114 12115 /* Fill in our template header with basic timestamp option. */ 12116 tmp_tcph += tcp->tcp_tcp_hdr_len; 12117 tmp_tcph[0] = TCPOPT_NOP; 12118 tmp_tcph[1] = TCPOPT_NOP; 12119 tmp_tcph[2] = TCPOPT_TSTAMP; 12120 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12121 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12122 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12123 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12124 } else { 12125 tcp->tcp_snd_ts_ok = B_FALSE; 12126 } 12127 12128 /* 12129 * Process SACK options. If SACK is enabled for this connection, 12130 * then allocate the SACK info structure. Note the following ways 12131 * when tcp_snd_sack_ok is set to true. 12132 * 12133 * For active connection: in tcp_adapt_ire() called in 12134 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12135 * is checked. 12136 * 12137 * For passive connection: in tcp_adapt_ire() called in 12138 * tcp_accept_comm(). 12139 * 12140 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12141 * That check makes sure that if we did not send a SACK OK option, 12142 * we will not enable SACK for this connection even though the other 12143 * side sends us SACK OK option. For active connection, the SACK 12144 * info structure has already been allocated. So we need to free 12145 * it if SACK is disabled. 12146 */ 12147 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12148 (tcp->tcp_snd_sack_ok || 12149 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12150 /* This should be true only in the passive case. */ 12151 if (tcp->tcp_sack_info == NULL) { 12152 ASSERT(TCP_IS_DETACHED(tcp)); 12153 tcp->tcp_sack_info = 12154 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12155 } 12156 if (tcp->tcp_sack_info == NULL) { 12157 tcp->tcp_snd_sack_ok = B_FALSE; 12158 } else { 12159 tcp->tcp_snd_sack_ok = B_TRUE; 12160 if (tcp->tcp_snd_ts_ok) { 12161 tcp->tcp_max_sack_blk = 3; 12162 } else { 12163 tcp->tcp_max_sack_blk = 4; 12164 } 12165 } 12166 } else { 12167 /* 12168 * Resetting tcp_snd_sack_ok to B_FALSE so that 12169 * no SACK info will be used for this 12170 * connection. This assumes that SACK usage 12171 * permission is negotiated. This may need 12172 * to be changed once this is clarified. 12173 */ 12174 if (tcp->tcp_sack_info != NULL) { 12175 ASSERT(tcp->tcp_notsack_list == NULL); 12176 kmem_cache_free(tcp_sack_info_cache, 12177 tcp->tcp_sack_info); 12178 tcp->tcp_sack_info = NULL; 12179 } 12180 tcp->tcp_snd_sack_ok = B_FALSE; 12181 } 12182 12183 /* 12184 * Now we know the exact TCP/IP header length, subtract 12185 * that from tcp_mss to get our side's MSS. 12186 */ 12187 tcp->tcp_mss -= tcp->tcp_hdr_len; 12188 /* 12189 * Here we assume that the other side's header size will be equal to 12190 * our header size. We calculate the real MSS accordingly. Need to 12191 * take into additional stuffs IPsec puts in. 12192 * 12193 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12194 */ 12195 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12196 ((tcp->tcp_ipversion == IPV4_VERSION ? 12197 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12198 12199 /* 12200 * Set MSS to the smaller one of both ends of the connection. 12201 * We should not have called tcp_mss_set() before, but our 12202 * side of the MSS should have been set to a proper value 12203 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12204 * STREAM head parameters properly. 12205 * 12206 * If we have a larger-than-16-bit window but the other side 12207 * didn't want to do window scale, tcp_rwnd_set() will take 12208 * care of that. 12209 */ 12210 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12211 } 12212 12213 /* 12214 * Sends the T_CONN_IND to the listener. The caller calls this 12215 * functions via squeue to get inside the listener's perimeter 12216 * once the 3 way hand shake is done a T_CONN_IND needs to be 12217 * sent. As an optimization, the caller can call this directly 12218 * if listener's perimeter is same as eager's. 12219 */ 12220 /* ARGSUSED */ 12221 void 12222 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12223 { 12224 conn_t *lconnp = (conn_t *)arg; 12225 tcp_t *listener = lconnp->conn_tcp; 12226 tcp_t *tcp; 12227 struct T_conn_ind *conn_ind; 12228 ipaddr_t *addr_cache; 12229 boolean_t need_send_conn_ind = B_FALSE; 12230 tcp_stack_t *tcps = listener->tcp_tcps; 12231 12232 /* retrieve the eager */ 12233 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12234 ASSERT(conn_ind->OPT_offset != 0 && 12235 conn_ind->OPT_length == sizeof (intptr_t)); 12236 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12237 conn_ind->OPT_length); 12238 12239 /* 12240 * TLI/XTI applications will get confused by 12241 * sending eager as an option since it violates 12242 * the option semantics. So remove the eager as 12243 * option since TLI/XTI app doesn't need it anyway. 12244 */ 12245 if (!TCP_IS_SOCKET(listener)) { 12246 conn_ind->OPT_length = 0; 12247 conn_ind->OPT_offset = 0; 12248 } 12249 if (listener->tcp_state == TCPS_CLOSED || 12250 TCP_IS_DETACHED(listener)) { 12251 /* 12252 * If listener has closed, it would have caused a 12253 * a cleanup/blowoff to happen for the eager. We 12254 * just need to return. 12255 */ 12256 freemsg(mp); 12257 return; 12258 } 12259 12260 12261 /* 12262 * if the conn_req_q is full defer passing up the 12263 * T_CONN_IND until space is availabe after t_accept() 12264 * processing 12265 */ 12266 mutex_enter(&listener->tcp_eager_lock); 12267 12268 /* 12269 * Take the eager out, if it is in the list of droppable eagers 12270 * as we are here because the 3W handshake is over. 12271 */ 12272 MAKE_UNDROPPABLE(tcp); 12273 12274 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12275 tcp_t *tail; 12276 12277 /* 12278 * The eager already has an extra ref put in tcp_rput_data 12279 * so that it stays till accept comes back even though it 12280 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12281 */ 12282 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12283 listener->tcp_conn_req_cnt_q0--; 12284 listener->tcp_conn_req_cnt_q++; 12285 12286 /* Move from SYN_RCVD to ESTABLISHED list */ 12287 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12288 tcp->tcp_eager_prev_q0; 12289 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12290 tcp->tcp_eager_next_q0; 12291 tcp->tcp_eager_prev_q0 = NULL; 12292 tcp->tcp_eager_next_q0 = NULL; 12293 12294 /* 12295 * Insert at end of the queue because sockfs 12296 * sends down T_CONN_RES in chronological 12297 * order. Leaving the older conn indications 12298 * at front of the queue helps reducing search 12299 * time. 12300 */ 12301 tail = listener->tcp_eager_last_q; 12302 if (tail != NULL) 12303 tail->tcp_eager_next_q = tcp; 12304 else 12305 listener->tcp_eager_next_q = tcp; 12306 listener->tcp_eager_last_q = tcp; 12307 tcp->tcp_eager_next_q = NULL; 12308 /* 12309 * Delay sending up the T_conn_ind until we are 12310 * done with the eager. Once we have have sent up 12311 * the T_conn_ind, the accept can potentially complete 12312 * any time and release the refhold we have on the eager. 12313 */ 12314 need_send_conn_ind = B_TRUE; 12315 } else { 12316 /* 12317 * Defer connection on q0 and set deferred 12318 * connection bit true 12319 */ 12320 tcp->tcp_conn_def_q0 = B_TRUE; 12321 12322 /* take tcp out of q0 ... */ 12323 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12324 tcp->tcp_eager_next_q0; 12325 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12326 tcp->tcp_eager_prev_q0; 12327 12328 /* ... and place it at the end of q0 */ 12329 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12330 tcp->tcp_eager_next_q0 = listener; 12331 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12332 listener->tcp_eager_prev_q0 = tcp; 12333 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12334 } 12335 12336 /* we have timed out before */ 12337 if (tcp->tcp_syn_rcvd_timeout != 0) { 12338 tcp->tcp_syn_rcvd_timeout = 0; 12339 listener->tcp_syn_rcvd_timeout--; 12340 if (listener->tcp_syn_defense && 12341 listener->tcp_syn_rcvd_timeout <= 12342 (tcps->tcps_conn_req_max_q0 >> 5) && 12343 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12344 listener->tcp_last_rcv_lbolt)) { 12345 /* 12346 * Turn off the defense mode if we 12347 * believe the SYN attack is over. 12348 */ 12349 listener->tcp_syn_defense = B_FALSE; 12350 if (listener->tcp_ip_addr_cache) { 12351 kmem_free((void *)listener->tcp_ip_addr_cache, 12352 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12353 listener->tcp_ip_addr_cache = NULL; 12354 } 12355 } 12356 } 12357 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12358 if (addr_cache != NULL) { 12359 /* 12360 * We have finished a 3-way handshake with this 12361 * remote host. This proves the IP addr is good. 12362 * Cache it! 12363 */ 12364 addr_cache[IP_ADDR_CACHE_HASH( 12365 tcp->tcp_remote)] = tcp->tcp_remote; 12366 } 12367 mutex_exit(&listener->tcp_eager_lock); 12368 if (need_send_conn_ind) 12369 tcp_ulp_newconn(lconnp, tcp->tcp_connp, mp); 12370 } 12371 12372 /* 12373 * Send the newconn notification to ulp. The eager is blown off if the 12374 * notification fails. 12375 */ 12376 static void 12377 tcp_ulp_newconn(conn_t *lconnp, conn_t *econnp, mblk_t *mp) 12378 { 12379 if (IPCL_IS_NONSTR(lconnp)) { 12380 cred_t *cr; 12381 pid_t cpid; 12382 12383 cr = msg_getcred(mp, &cpid); 12384 12385 ASSERT(econnp->conn_tcp->tcp_listener == lconnp->conn_tcp); 12386 ASSERT(econnp->conn_tcp->tcp_saved_listener == 12387 lconnp->conn_tcp); 12388 12389 /* Keep the message around in case of a fallback to TPI */ 12390 econnp->conn_tcp->tcp_conn.tcp_eager_conn_ind = mp; 12391 12392 /* 12393 * Notify the ULP about the newconn. It is guaranteed that no 12394 * tcp_accept() call will be made for the eager if the 12395 * notification fails, so it's safe to blow it off in that 12396 * case. 12397 * 12398 * The upper handle will be assigned when tcp_accept() is 12399 * called. 12400 */ 12401 if ((*lconnp->conn_upcalls->su_newconn) 12402 (lconnp->conn_upper_handle, 12403 (sock_lower_handle_t)econnp, 12404 &sock_tcp_downcalls, cr, cpid, 12405 &econnp->conn_upcalls) == NULL) { 12406 /* Failed to allocate a socket */ 12407 BUMP_MIB(&lconnp->conn_tcp->tcp_tcps->tcps_mib, 12408 tcpEstabResets); 12409 (void) tcp_eager_blowoff(lconnp->conn_tcp, 12410 econnp->conn_tcp->tcp_conn_req_seqnum); 12411 } 12412 } else { 12413 putnext(lconnp->conn_tcp->tcp_rq, mp); 12414 } 12415 } 12416 12417 mblk_t * 12418 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12419 uint_t *ifindexp, ip6_pkt_t *ippp) 12420 { 12421 ip_pktinfo_t *pinfo; 12422 ip6_t *ip6h; 12423 uchar_t *rptr; 12424 mblk_t *first_mp = mp; 12425 boolean_t mctl_present = B_FALSE; 12426 uint_t ifindex = 0; 12427 ip6_pkt_t ipp; 12428 uint_t ipvers; 12429 uint_t ip_hdr_len; 12430 tcp_stack_t *tcps = tcp->tcp_tcps; 12431 12432 rptr = mp->b_rptr; 12433 ASSERT(OK_32PTR(rptr)); 12434 ASSERT(tcp != NULL); 12435 ipp.ipp_fields = 0; 12436 12437 switch DB_TYPE(mp) { 12438 case M_CTL: 12439 mp = mp->b_cont; 12440 if (mp == NULL) { 12441 freemsg(first_mp); 12442 return (NULL); 12443 } 12444 if (DB_TYPE(mp) != M_DATA) { 12445 freemsg(first_mp); 12446 return (NULL); 12447 } 12448 mctl_present = B_TRUE; 12449 break; 12450 case M_DATA: 12451 break; 12452 default: 12453 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12454 freemsg(mp); 12455 return (NULL); 12456 } 12457 ipvers = IPH_HDR_VERSION(rptr); 12458 if (ipvers == IPV4_VERSION) { 12459 if (tcp == NULL) { 12460 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12461 goto done; 12462 } 12463 12464 ipp.ipp_fields |= IPPF_HOPLIMIT; 12465 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12466 12467 /* 12468 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12469 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12470 */ 12471 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12472 mctl_present) { 12473 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12474 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12475 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12476 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12477 ipp.ipp_fields |= IPPF_IFINDEX; 12478 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12479 ifindex = pinfo->ip_pkt_ifindex; 12480 } 12481 freeb(first_mp); 12482 mctl_present = B_FALSE; 12483 } 12484 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12485 } else { 12486 ip6h = (ip6_t *)rptr; 12487 12488 ASSERT(ipvers == IPV6_VERSION); 12489 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12490 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12491 ipp.ipp_hoplimit = ip6h->ip6_hops; 12492 12493 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12494 uint8_t nexthdrp; 12495 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12496 12497 /* Look for ifindex information */ 12498 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12499 ip6i_t *ip6i = (ip6i_t *)ip6h; 12500 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12501 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12502 freemsg(first_mp); 12503 return (NULL); 12504 } 12505 12506 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12507 ASSERT(ip6i->ip6i_ifindex != 0); 12508 ipp.ipp_fields |= IPPF_IFINDEX; 12509 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12510 ifindex = ip6i->ip6i_ifindex; 12511 } 12512 rptr = (uchar_t *)&ip6i[1]; 12513 mp->b_rptr = rptr; 12514 if (rptr == mp->b_wptr) { 12515 mblk_t *mp1; 12516 mp1 = mp->b_cont; 12517 freeb(mp); 12518 mp = mp1; 12519 rptr = mp->b_rptr; 12520 } 12521 if (MBLKL(mp) < IPV6_HDR_LEN + 12522 sizeof (tcph_t)) { 12523 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12524 freemsg(first_mp); 12525 return (NULL); 12526 } 12527 ip6h = (ip6_t *)rptr; 12528 } 12529 12530 /* 12531 * Find any potentially interesting extension headers 12532 * as well as the length of the IPv6 + extension 12533 * headers. 12534 */ 12535 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12536 /* Verify if this is a TCP packet */ 12537 if (nexthdrp != IPPROTO_TCP) { 12538 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12539 freemsg(first_mp); 12540 return (NULL); 12541 } 12542 } else { 12543 ip_hdr_len = IPV6_HDR_LEN; 12544 } 12545 } 12546 12547 done: 12548 if (ipversp != NULL) 12549 *ipversp = ipvers; 12550 if (ip_hdr_lenp != NULL) 12551 *ip_hdr_lenp = ip_hdr_len; 12552 if (ippp != NULL) 12553 *ippp = ipp; 12554 if (ifindexp != NULL) 12555 *ifindexp = ifindex; 12556 if (mctl_present) { 12557 freeb(first_mp); 12558 } 12559 return (mp); 12560 } 12561 12562 /* 12563 * Handle M_DATA messages from IP. Its called directly from IP via 12564 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12565 * in this path. 12566 * 12567 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12568 * v4 and v6), we are called through tcp_input() and a M_CTL can 12569 * be present for options but tcp_find_pktinfo() deals with it. We 12570 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12571 * 12572 * The first argument is always the connp/tcp to which the mp belongs. 12573 * There are no exceptions to this rule. The caller has already put 12574 * a reference on this connp/tcp and once tcp_rput_data() returns, 12575 * the squeue will do the refrele. 12576 * 12577 * The TH_SYN for the listener directly go to tcp_conn_request via 12578 * squeue. 12579 * 12580 * sqp: NULL = recursive, sqp != NULL means called from squeue 12581 */ 12582 void 12583 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12584 { 12585 int32_t bytes_acked; 12586 int32_t gap; 12587 mblk_t *mp1; 12588 uint_t flags; 12589 uint32_t new_swnd = 0; 12590 uchar_t *iphdr; 12591 uchar_t *rptr; 12592 int32_t rgap; 12593 uint32_t seg_ack; 12594 int seg_len; 12595 uint_t ip_hdr_len; 12596 uint32_t seg_seq; 12597 tcph_t *tcph; 12598 int urp; 12599 tcp_opt_t tcpopt; 12600 uint_t ipvers; 12601 ip6_pkt_t ipp; 12602 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12603 uint32_t cwnd; 12604 uint32_t add; 12605 int npkt; 12606 int mss; 12607 conn_t *connp = (conn_t *)arg; 12608 squeue_t *sqp = (squeue_t *)arg2; 12609 tcp_t *tcp = connp->conn_tcp; 12610 tcp_stack_t *tcps = tcp->tcp_tcps; 12611 12612 /* 12613 * RST from fused tcp loopback peer should trigger an unfuse. 12614 */ 12615 if (tcp->tcp_fused) { 12616 TCP_STAT(tcps, tcp_fusion_aborted); 12617 tcp_unfuse(tcp); 12618 } 12619 12620 iphdr = mp->b_rptr; 12621 rptr = mp->b_rptr; 12622 ASSERT(OK_32PTR(rptr)); 12623 12624 /* 12625 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12626 * processing here. For rest call tcp_find_pktinfo to fill up the 12627 * necessary information. 12628 */ 12629 if (IPCL_IS_TCP4(connp)) { 12630 ipvers = IPV4_VERSION; 12631 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12632 } else { 12633 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12634 NULL, &ipp); 12635 if (mp == NULL) { 12636 TCP_STAT(tcps, tcp_rput_v6_error); 12637 return; 12638 } 12639 iphdr = mp->b_rptr; 12640 rptr = mp->b_rptr; 12641 } 12642 ASSERT(DB_TYPE(mp) == M_DATA); 12643 ASSERT(mp->b_next == NULL); 12644 12645 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12646 seg_seq = ABE32_TO_U32(tcph->th_seq); 12647 seg_ack = ABE32_TO_U32(tcph->th_ack); 12648 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12649 seg_len = (int)(mp->b_wptr - rptr) - 12650 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12651 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12652 do { 12653 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12654 (uintptr_t)INT_MAX); 12655 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12656 } while ((mp1 = mp1->b_cont) != NULL && 12657 mp1->b_datap->db_type == M_DATA); 12658 } 12659 12660 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12661 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12662 seg_len, tcph); 12663 return; 12664 } 12665 12666 if (sqp != NULL) { 12667 /* 12668 * This is the correct place to update tcp_last_recv_time. Note 12669 * that it is also updated for tcp structure that belongs to 12670 * global and listener queues which do not really need updating. 12671 * But that should not cause any harm. And it is updated for 12672 * all kinds of incoming segments, not only for data segments. 12673 */ 12674 tcp->tcp_last_recv_time = lbolt; 12675 } 12676 12677 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12678 12679 BUMP_LOCAL(tcp->tcp_ibsegs); 12680 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 12681 12682 if ((flags & TH_URG) && sqp != NULL) { 12683 /* 12684 * TCP can't handle urgent pointers that arrive before 12685 * the connection has been accept()ed since it can't 12686 * buffer OOB data. Discard segment if this happens. 12687 * 12688 * We can't just rely on a non-null tcp_listener to indicate 12689 * that the accept() has completed since unlinking of the 12690 * eager and completion of the accept are not atomic. 12691 * tcp_detached, when it is not set (B_FALSE) indicates 12692 * that the accept() has completed. 12693 * 12694 * Nor can it reassemble urgent pointers, so discard 12695 * if it's not the next segment expected. 12696 * 12697 * Otherwise, collapse chain into one mblk (discard if 12698 * that fails). This makes sure the headers, retransmitted 12699 * data, and new data all are in the same mblk. 12700 */ 12701 ASSERT(mp != NULL); 12702 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 12703 freemsg(mp); 12704 return; 12705 } 12706 /* Update pointers into message */ 12707 iphdr = rptr = mp->b_rptr; 12708 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12709 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12710 /* 12711 * Since we can't handle any data with this urgent 12712 * pointer that is out of sequence, we expunge 12713 * the data. This allows us to still register 12714 * the urgent mark and generate the M_PCSIG, 12715 * which we can do. 12716 */ 12717 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12718 seg_len = 0; 12719 } 12720 } 12721 12722 switch (tcp->tcp_state) { 12723 case TCPS_SYN_SENT: 12724 if (flags & TH_ACK) { 12725 /* 12726 * Note that our stack cannot send data before a 12727 * connection is established, therefore the 12728 * following check is valid. Otherwise, it has 12729 * to be changed. 12730 */ 12731 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12732 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12733 freemsg(mp); 12734 if (flags & TH_RST) 12735 return; 12736 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12737 tcp, seg_ack, 0, TH_RST); 12738 return; 12739 } 12740 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12741 } 12742 if (flags & TH_RST) { 12743 freemsg(mp); 12744 if (flags & TH_ACK) 12745 (void) tcp_clean_death(tcp, 12746 ECONNREFUSED, 13); 12747 return; 12748 } 12749 if (!(flags & TH_SYN)) { 12750 freemsg(mp); 12751 return; 12752 } 12753 12754 /* Process all TCP options. */ 12755 tcp_process_options(tcp, tcph); 12756 /* 12757 * The following changes our rwnd to be a multiple of the 12758 * MIN(peer MSS, our MSS) for performance reason. 12759 */ 12760 (void) tcp_rwnd_set(tcp, 12761 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 12762 12763 /* Is the other end ECN capable? */ 12764 if (tcp->tcp_ecn_ok) { 12765 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12766 tcp->tcp_ecn_ok = B_FALSE; 12767 } 12768 } 12769 /* 12770 * Clear ECN flags because it may interfere with later 12771 * processing. 12772 */ 12773 flags &= ~(TH_ECE|TH_CWR); 12774 12775 tcp->tcp_irs = seg_seq; 12776 tcp->tcp_rack = seg_seq; 12777 tcp->tcp_rnxt = seg_seq + 1; 12778 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12779 if (!TCP_IS_DETACHED(tcp)) { 12780 /* Allocate room for SACK options if needed. */ 12781 if (tcp->tcp_snd_sack_ok) { 12782 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12783 tcp->tcp_hdr_len + 12784 TCPOPT_MAX_SACK_LEN + 12785 (tcp->tcp_loopback ? 0 : 12786 tcps->tcps_wroff_xtra)); 12787 } else { 12788 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12789 tcp->tcp_hdr_len + 12790 (tcp->tcp_loopback ? 0 : 12791 tcps->tcps_wroff_xtra)); 12792 } 12793 } 12794 if (flags & TH_ACK) { 12795 /* 12796 * If we can't get the confirmation upstream, pretend 12797 * we didn't even see this one. 12798 * 12799 * XXX: how can we pretend we didn't see it if we 12800 * have updated rnxt et. al. 12801 * 12802 * For loopback we defer sending up the T_CONN_CON 12803 * until after some checks below. 12804 */ 12805 mp1 = NULL; 12806 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12807 tcp->tcp_loopback ? &mp1 : NULL)) { 12808 freemsg(mp); 12809 return; 12810 } 12811 /* SYN was acked - making progress */ 12812 if (tcp->tcp_ipversion == IPV6_VERSION) 12813 tcp->tcp_ip_forward_progress = B_TRUE; 12814 12815 /* One for the SYN */ 12816 tcp->tcp_suna = tcp->tcp_iss + 1; 12817 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12818 tcp->tcp_state = TCPS_ESTABLISHED; 12819 12820 /* 12821 * If SYN was retransmitted, need to reset all 12822 * retransmission info. This is because this 12823 * segment will be treated as a dup ACK. 12824 */ 12825 if (tcp->tcp_rexmit) { 12826 tcp->tcp_rexmit = B_FALSE; 12827 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12828 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12829 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12830 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12831 tcp->tcp_ms_we_have_waited = 0; 12832 12833 /* 12834 * Set tcp_cwnd back to 1 MSS, per 12835 * recommendation from 12836 * draft-floyd-incr-init-win-01.txt, 12837 * Increasing TCP's Initial Window. 12838 */ 12839 tcp->tcp_cwnd = tcp->tcp_mss; 12840 } 12841 12842 tcp->tcp_swl1 = seg_seq; 12843 tcp->tcp_swl2 = seg_ack; 12844 12845 new_swnd = BE16_TO_U16(tcph->th_win); 12846 tcp->tcp_swnd = new_swnd; 12847 if (new_swnd > tcp->tcp_max_swnd) 12848 tcp->tcp_max_swnd = new_swnd; 12849 12850 /* 12851 * Always send the three-way handshake ack immediately 12852 * in order to make the connection complete as soon as 12853 * possible on the accepting host. 12854 */ 12855 flags |= TH_ACK_NEEDED; 12856 12857 /* 12858 * Special case for loopback. At this point we have 12859 * received SYN-ACK from the remote endpoint. In 12860 * order to ensure that both endpoints reach the 12861 * fused state prior to any data exchange, the final 12862 * ACK needs to be sent before we indicate T_CONN_CON 12863 * to the module upstream. 12864 */ 12865 if (tcp->tcp_loopback) { 12866 mblk_t *ack_mp; 12867 12868 ASSERT(!tcp->tcp_unfusable); 12869 ASSERT(mp1 != NULL); 12870 /* 12871 * For loopback, we always get a pure SYN-ACK 12872 * and only need to send back the final ACK 12873 * with no data (this is because the other 12874 * tcp is ours and we don't do T/TCP). This 12875 * final ACK triggers the passive side to 12876 * perform fusion in ESTABLISHED state. 12877 */ 12878 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 12879 if (tcp->tcp_ack_tid != 0) { 12880 (void) TCP_TIMER_CANCEL(tcp, 12881 tcp->tcp_ack_tid); 12882 tcp->tcp_ack_tid = 0; 12883 } 12884 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 12885 BUMP_LOCAL(tcp->tcp_obsegs); 12886 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 12887 12888 if (!IPCL_IS_NONSTR(connp)) { 12889 /* Send up T_CONN_CON */ 12890 putnext(tcp->tcp_rq, mp1); 12891 } else { 12892 cred_t *cr; 12893 pid_t cpid; 12894 12895 cr = msg_getcred(mp1, &cpid); 12896 (*connp->conn_upcalls-> 12897 su_connected) 12898 (connp->conn_upper_handle, 12899 tcp->tcp_connid, cr, cpid); 12900 freemsg(mp1); 12901 } 12902 12903 freemsg(mp); 12904 return; 12905 } 12906 /* 12907 * Forget fusion; we need to handle more 12908 * complex cases below. Send the deferred 12909 * T_CONN_CON message upstream and proceed 12910 * as usual. Mark this tcp as not capable 12911 * of fusion. 12912 */ 12913 TCP_STAT(tcps, tcp_fusion_unfusable); 12914 tcp->tcp_unfusable = B_TRUE; 12915 if (!IPCL_IS_NONSTR(connp)) { 12916 putnext(tcp->tcp_rq, mp1); 12917 } else { 12918 cred_t *cr; 12919 pid_t cpid; 12920 12921 cr = msg_getcred(mp1, &cpid); 12922 (*connp->conn_upcalls->su_connected) 12923 (connp->conn_upper_handle, 12924 tcp->tcp_connid, cr, cpid); 12925 freemsg(mp1); 12926 } 12927 } 12928 12929 /* 12930 * Check to see if there is data to be sent. If 12931 * yes, set the transmit flag. Then check to see 12932 * if received data processing needs to be done. 12933 * If not, go straight to xmit_check. This short 12934 * cut is OK as we don't support T/TCP. 12935 */ 12936 if (tcp->tcp_unsent) 12937 flags |= TH_XMIT_NEEDED; 12938 12939 if (seg_len == 0 && !(flags & TH_URG)) { 12940 freemsg(mp); 12941 goto xmit_check; 12942 } 12943 12944 flags &= ~TH_SYN; 12945 seg_seq++; 12946 break; 12947 } 12948 tcp->tcp_state = TCPS_SYN_RCVD; 12949 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 12950 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 12951 if (mp1) { 12952 /* 12953 * See comment in tcp_conn_request() for why we use 12954 * the open() time pid here. 12955 */ 12956 DB_CPID(mp1) = tcp->tcp_cpid; 12957 tcp_send_data(tcp, tcp->tcp_wq, mp1); 12958 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 12959 } 12960 freemsg(mp); 12961 return; 12962 case TCPS_SYN_RCVD: 12963 if (flags & TH_ACK) { 12964 /* 12965 * In this state, a SYN|ACK packet is either bogus 12966 * because the other side must be ACKing our SYN which 12967 * indicates it has seen the ACK for their SYN and 12968 * shouldn't retransmit it or we're crossing SYNs 12969 * on active open. 12970 */ 12971 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 12972 freemsg(mp); 12973 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 12974 tcp, seg_ack, 0, TH_RST); 12975 return; 12976 } 12977 /* 12978 * NOTE: RFC 793 pg. 72 says this should be 12979 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 12980 * but that would mean we have an ack that ignored 12981 * our SYN. 12982 */ 12983 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 12984 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12985 freemsg(mp); 12986 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 12987 tcp, seg_ack, 0, TH_RST); 12988 return; 12989 } 12990 } 12991 break; 12992 case TCPS_LISTEN: 12993 /* 12994 * Only a TLI listener can come through this path when a 12995 * acceptor is going back to be a listener and a packet 12996 * for the acceptor hits the classifier. For a socket 12997 * listener, this can never happen because a listener 12998 * can never accept connection on itself and hence a 12999 * socket acceptor can not go back to being a listener. 13000 */ 13001 ASSERT(!TCP_IS_SOCKET(tcp)); 13002 /*FALLTHRU*/ 13003 case TCPS_CLOSED: 13004 case TCPS_BOUND: { 13005 conn_t *new_connp; 13006 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13007 13008 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13009 if (new_connp != NULL) { 13010 tcp_reinput(new_connp, mp, connp->conn_sqp); 13011 return; 13012 } 13013 /* We failed to classify. For now just drop the packet */ 13014 freemsg(mp); 13015 return; 13016 } 13017 case TCPS_IDLE: 13018 /* 13019 * Handle the case where the tcp_clean_death() has happened 13020 * on a connection (application hasn't closed yet) but a packet 13021 * was already queued on squeue before tcp_clean_death() 13022 * was processed. Calling tcp_clean_death() twice on same 13023 * connection can result in weird behaviour. 13024 */ 13025 freemsg(mp); 13026 return; 13027 default: 13028 break; 13029 } 13030 13031 /* 13032 * Already on the correct queue/perimeter. 13033 * If this is a detached connection and not an eager 13034 * connection hanging off a listener then new data 13035 * (past the FIN) will cause a reset. 13036 * We do a special check here where it 13037 * is out of the main line, rather than check 13038 * if we are detached every time we see new 13039 * data down below. 13040 */ 13041 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13042 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13043 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13044 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13045 13046 freemsg(mp); 13047 /* 13048 * This could be an SSL closure alert. We're detached so just 13049 * acknowledge it this last time. 13050 */ 13051 if (tcp->tcp_kssl_ctx != NULL) { 13052 kssl_release_ctx(tcp->tcp_kssl_ctx); 13053 tcp->tcp_kssl_ctx = NULL; 13054 13055 tcp->tcp_rnxt += seg_len; 13056 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13057 flags |= TH_ACK_NEEDED; 13058 goto ack_check; 13059 } 13060 13061 tcp_xmit_ctl("new data when detached", tcp, 13062 tcp->tcp_snxt, 0, TH_RST); 13063 (void) tcp_clean_death(tcp, EPROTO, 12); 13064 return; 13065 } 13066 13067 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13068 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13069 new_swnd = BE16_TO_U16(tcph->th_win) << 13070 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13071 13072 if (tcp->tcp_snd_ts_ok) { 13073 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13074 /* 13075 * This segment is not acceptable. 13076 * Drop it and send back an ACK. 13077 */ 13078 freemsg(mp); 13079 flags |= TH_ACK_NEEDED; 13080 goto ack_check; 13081 } 13082 } else if (tcp->tcp_snd_sack_ok) { 13083 ASSERT(tcp->tcp_sack_info != NULL); 13084 tcpopt.tcp = tcp; 13085 /* 13086 * SACK info in already updated in tcp_parse_options. Ignore 13087 * all other TCP options... 13088 */ 13089 (void) tcp_parse_options(tcph, &tcpopt); 13090 } 13091 try_again:; 13092 mss = tcp->tcp_mss; 13093 gap = seg_seq - tcp->tcp_rnxt; 13094 rgap = tcp->tcp_rwnd - (gap + seg_len); 13095 /* 13096 * gap is the amount of sequence space between what we expect to see 13097 * and what we got for seg_seq. A positive value for gap means 13098 * something got lost. A negative value means we got some old stuff. 13099 */ 13100 if (gap < 0) { 13101 /* Old stuff present. Is the SYN in there? */ 13102 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13103 (seg_len != 0)) { 13104 flags &= ~TH_SYN; 13105 seg_seq++; 13106 urp--; 13107 /* Recompute the gaps after noting the SYN. */ 13108 goto try_again; 13109 } 13110 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13111 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13112 (seg_len > -gap ? -gap : seg_len)); 13113 /* Remove the old stuff from seg_len. */ 13114 seg_len += gap; 13115 /* 13116 * Anything left? 13117 * Make sure to check for unack'd FIN when rest of data 13118 * has been previously ack'd. 13119 */ 13120 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13121 /* 13122 * Resets are only valid if they lie within our offered 13123 * window. If the RST bit is set, we just ignore this 13124 * segment. 13125 */ 13126 if (flags & TH_RST) { 13127 freemsg(mp); 13128 return; 13129 } 13130 13131 /* 13132 * The arriving of dup data packets indicate that we 13133 * may have postponed an ack for too long, or the other 13134 * side's RTT estimate is out of shape. Start acking 13135 * more often. 13136 */ 13137 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13138 tcp->tcp_rack_cnt >= 1 && 13139 tcp->tcp_rack_abs_max > 2) { 13140 tcp->tcp_rack_abs_max--; 13141 } 13142 tcp->tcp_rack_cur_max = 1; 13143 13144 /* 13145 * This segment is "unacceptable". None of its 13146 * sequence space lies within our advertized window. 13147 * 13148 * Adjust seg_len to the original value for tracing. 13149 */ 13150 seg_len -= gap; 13151 if (tcp->tcp_debug) { 13152 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13153 "tcp_rput: unacceptable, gap %d, rgap %d, " 13154 "flags 0x%x, seg_seq %u, seg_ack %u, " 13155 "seg_len %d, rnxt %u, snxt %u, %s", 13156 gap, rgap, flags, seg_seq, seg_ack, 13157 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13158 tcp_display(tcp, NULL, 13159 DISP_ADDR_AND_PORT)); 13160 } 13161 13162 /* 13163 * Arrange to send an ACK in response to the 13164 * unacceptable segment per RFC 793 page 69. There 13165 * is only one small difference between ours and the 13166 * acceptability test in the RFC - we accept ACK-only 13167 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13168 * will be generated. 13169 * 13170 * Note that we have to ACK an ACK-only packet at least 13171 * for stacks that send 0-length keep-alives with 13172 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13173 * section 4.2.3.6. As long as we don't ever generate 13174 * an unacceptable packet in response to an incoming 13175 * packet that is unacceptable, it should not cause 13176 * "ACK wars". 13177 */ 13178 flags |= TH_ACK_NEEDED; 13179 13180 /* 13181 * Continue processing this segment in order to use the 13182 * ACK information it contains, but skip all other 13183 * sequence-number processing. Processing the ACK 13184 * information is necessary in order to 13185 * re-synchronize connections that may have lost 13186 * synchronization. 13187 * 13188 * We clear seg_len and flag fields related to 13189 * sequence number processing as they are not 13190 * to be trusted for an unacceptable segment. 13191 */ 13192 seg_len = 0; 13193 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13194 goto process_ack; 13195 } 13196 13197 /* Fix seg_seq, and chew the gap off the front. */ 13198 seg_seq = tcp->tcp_rnxt; 13199 urp += gap; 13200 do { 13201 mblk_t *mp2; 13202 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13203 (uintptr_t)UINT_MAX); 13204 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13205 if (gap > 0) { 13206 mp->b_rptr = mp->b_wptr - gap; 13207 break; 13208 } 13209 mp2 = mp; 13210 mp = mp->b_cont; 13211 freeb(mp2); 13212 } while (gap < 0); 13213 /* 13214 * If the urgent data has already been acknowledged, we 13215 * should ignore TH_URG below 13216 */ 13217 if (urp < 0) 13218 flags &= ~TH_URG; 13219 } 13220 /* 13221 * rgap is the amount of stuff received out of window. A negative 13222 * value is the amount out of window. 13223 */ 13224 if (rgap < 0) { 13225 mblk_t *mp2; 13226 13227 if (tcp->tcp_rwnd == 0) { 13228 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13229 } else { 13230 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13231 UPDATE_MIB(&tcps->tcps_mib, 13232 tcpInDataPastWinBytes, -rgap); 13233 } 13234 13235 /* 13236 * seg_len does not include the FIN, so if more than 13237 * just the FIN is out of window, we act like we don't 13238 * see it. (If just the FIN is out of window, rgap 13239 * will be zero and we will go ahead and acknowledge 13240 * the FIN.) 13241 */ 13242 flags &= ~TH_FIN; 13243 13244 /* Fix seg_len and make sure there is something left. */ 13245 seg_len += rgap; 13246 if (seg_len <= 0) { 13247 /* 13248 * Resets are only valid if they lie within our offered 13249 * window. If the RST bit is set, we just ignore this 13250 * segment. 13251 */ 13252 if (flags & TH_RST) { 13253 freemsg(mp); 13254 return; 13255 } 13256 13257 /* Per RFC 793, we need to send back an ACK. */ 13258 flags |= TH_ACK_NEEDED; 13259 13260 /* 13261 * Send SIGURG as soon as possible i.e. even 13262 * if the TH_URG was delivered in a window probe 13263 * packet (which will be unacceptable). 13264 * 13265 * We generate a signal if none has been generated 13266 * for this connection or if this is a new urgent 13267 * byte. Also send a zero-length "unmarked" message 13268 * to inform SIOCATMARK that this is not the mark. 13269 * 13270 * tcp_urp_last_valid is cleared when the T_exdata_ind 13271 * is sent up. This plus the check for old data 13272 * (gap >= 0) handles the wraparound of the sequence 13273 * number space without having to always track the 13274 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13275 * this max in its rcv_up variable). 13276 * 13277 * This prevents duplicate SIGURGS due to a "late" 13278 * zero-window probe when the T_EXDATA_IND has already 13279 * been sent up. 13280 */ 13281 if ((flags & TH_URG) && 13282 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13283 tcp->tcp_urp_last))) { 13284 if (IPCL_IS_NONSTR(connp)) { 13285 if (!TCP_IS_DETACHED(tcp)) { 13286 (*connp->conn_upcalls-> 13287 su_signal_oob) 13288 (connp->conn_upper_handle, 13289 urp); 13290 } 13291 } else { 13292 mp1 = allocb(0, BPRI_MED); 13293 if (mp1 == NULL) { 13294 freemsg(mp); 13295 return; 13296 } 13297 if (!TCP_IS_DETACHED(tcp) && 13298 !putnextctl1(tcp->tcp_rq, 13299 M_PCSIG, SIGURG)) { 13300 /* Try again on the rexmit. */ 13301 freemsg(mp1); 13302 freemsg(mp); 13303 return; 13304 } 13305 /* 13306 * If the next byte would be the mark 13307 * then mark with MARKNEXT else mark 13308 * with NOTMARKNEXT. 13309 */ 13310 if (gap == 0 && urp == 0) 13311 mp1->b_flag |= MSGMARKNEXT; 13312 else 13313 mp1->b_flag |= MSGNOTMARKNEXT; 13314 freemsg(tcp->tcp_urp_mark_mp); 13315 tcp->tcp_urp_mark_mp = mp1; 13316 flags |= TH_SEND_URP_MARK; 13317 } 13318 tcp->tcp_urp_last_valid = B_TRUE; 13319 tcp->tcp_urp_last = urp + seg_seq; 13320 } 13321 /* 13322 * If this is a zero window probe, continue to 13323 * process the ACK part. But we need to set seg_len 13324 * to 0 to avoid data processing. Otherwise just 13325 * drop the segment and send back an ACK. 13326 */ 13327 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13328 flags &= ~(TH_SYN | TH_URG); 13329 seg_len = 0; 13330 goto process_ack; 13331 } else { 13332 freemsg(mp); 13333 goto ack_check; 13334 } 13335 } 13336 /* Pitch out of window stuff off the end. */ 13337 rgap = seg_len; 13338 mp2 = mp; 13339 do { 13340 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13341 (uintptr_t)INT_MAX); 13342 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13343 if (rgap < 0) { 13344 mp2->b_wptr += rgap; 13345 if ((mp1 = mp2->b_cont) != NULL) { 13346 mp2->b_cont = NULL; 13347 freemsg(mp1); 13348 } 13349 break; 13350 } 13351 } while ((mp2 = mp2->b_cont) != NULL); 13352 } 13353 ok:; 13354 /* 13355 * TCP should check ECN info for segments inside the window only. 13356 * Therefore the check should be done here. 13357 */ 13358 if (tcp->tcp_ecn_ok) { 13359 if (flags & TH_CWR) { 13360 tcp->tcp_ecn_echo_on = B_FALSE; 13361 } 13362 /* 13363 * Note that both ECN_CE and CWR can be set in the 13364 * same segment. In this case, we once again turn 13365 * on ECN_ECHO. 13366 */ 13367 if (tcp->tcp_ipversion == IPV4_VERSION) { 13368 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13369 13370 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13371 tcp->tcp_ecn_echo_on = B_TRUE; 13372 } 13373 } else { 13374 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13375 13376 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13377 htonl(IPH_ECN_CE << 20)) { 13378 tcp->tcp_ecn_echo_on = B_TRUE; 13379 } 13380 } 13381 } 13382 13383 /* 13384 * Check whether we can update tcp_ts_recent. This test is 13385 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13386 * Extensions for High Performance: An Update", Internet Draft. 13387 */ 13388 if (tcp->tcp_snd_ts_ok && 13389 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13390 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13391 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13392 tcp->tcp_last_rcv_lbolt = lbolt64; 13393 } 13394 13395 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13396 /* 13397 * FIN in an out of order segment. We record this in 13398 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13399 * Clear the FIN so that any check on FIN flag will fail. 13400 * Remember that FIN also counts in the sequence number 13401 * space. So we need to ack out of order FIN only segments. 13402 */ 13403 if (flags & TH_FIN) { 13404 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13405 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13406 flags &= ~TH_FIN; 13407 flags |= TH_ACK_NEEDED; 13408 } 13409 if (seg_len > 0) { 13410 /* Fill in the SACK blk list. */ 13411 if (tcp->tcp_snd_sack_ok) { 13412 ASSERT(tcp->tcp_sack_info != NULL); 13413 tcp_sack_insert(tcp->tcp_sack_list, 13414 seg_seq, seg_seq + seg_len, 13415 &(tcp->tcp_num_sack_blk)); 13416 } 13417 13418 /* 13419 * Attempt reassembly and see if we have something 13420 * ready to go. 13421 */ 13422 mp = tcp_reass(tcp, mp, seg_seq); 13423 /* Always ack out of order packets */ 13424 flags |= TH_ACK_NEEDED | TH_PUSH; 13425 if (mp) { 13426 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13427 (uintptr_t)INT_MAX); 13428 seg_len = mp->b_cont ? msgdsize(mp) : 13429 (int)(mp->b_wptr - mp->b_rptr); 13430 seg_seq = tcp->tcp_rnxt; 13431 /* 13432 * A gap is filled and the seq num and len 13433 * of the gap match that of a previously 13434 * received FIN, put the FIN flag back in. 13435 */ 13436 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13437 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13438 flags |= TH_FIN; 13439 tcp->tcp_valid_bits &= 13440 ~TCP_OFO_FIN_VALID; 13441 } 13442 } else { 13443 /* 13444 * Keep going even with NULL mp. 13445 * There may be a useful ACK or something else 13446 * we don't want to miss. 13447 * 13448 * But TCP should not perform fast retransmit 13449 * because of the ack number. TCP uses 13450 * seg_len == 0 to determine if it is a pure 13451 * ACK. And this is not a pure ACK. 13452 */ 13453 seg_len = 0; 13454 ofo_seg = B_TRUE; 13455 } 13456 } 13457 } else if (seg_len > 0) { 13458 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13459 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13460 /* 13461 * If an out of order FIN was received before, and the seq 13462 * num and len of the new segment match that of the FIN, 13463 * put the FIN flag back in. 13464 */ 13465 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13466 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13467 flags |= TH_FIN; 13468 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13469 } 13470 } 13471 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13472 if (flags & TH_RST) { 13473 freemsg(mp); 13474 switch (tcp->tcp_state) { 13475 case TCPS_SYN_RCVD: 13476 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13477 break; 13478 case TCPS_ESTABLISHED: 13479 case TCPS_FIN_WAIT_1: 13480 case TCPS_FIN_WAIT_2: 13481 case TCPS_CLOSE_WAIT: 13482 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13483 break; 13484 case TCPS_CLOSING: 13485 case TCPS_LAST_ACK: 13486 (void) tcp_clean_death(tcp, 0, 16); 13487 break; 13488 default: 13489 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13490 (void) tcp_clean_death(tcp, ENXIO, 17); 13491 break; 13492 } 13493 return; 13494 } 13495 if (flags & TH_SYN) { 13496 /* 13497 * See RFC 793, Page 71 13498 * 13499 * The seq number must be in the window as it should 13500 * be "fixed" above. If it is outside window, it should 13501 * be already rejected. Note that we allow seg_seq to be 13502 * rnxt + rwnd because we want to accept 0 window probe. 13503 */ 13504 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13505 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13506 freemsg(mp); 13507 /* 13508 * If the ACK flag is not set, just use our snxt as the 13509 * seq number of the RST segment. 13510 */ 13511 if (!(flags & TH_ACK)) { 13512 seg_ack = tcp->tcp_snxt; 13513 } 13514 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13515 TH_RST|TH_ACK); 13516 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13517 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13518 return; 13519 } 13520 /* 13521 * urp could be -1 when the urp field in the packet is 0 13522 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13523 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13524 */ 13525 if (flags & TH_URG && urp >= 0) { 13526 if (!tcp->tcp_urp_last_valid || 13527 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13528 if (IPCL_IS_NONSTR(connp)) { 13529 if (!TCP_IS_DETACHED(tcp)) { 13530 (*connp->conn_upcalls->su_signal_oob) 13531 (connp->conn_upper_handle, urp); 13532 } 13533 } else { 13534 /* 13535 * If we haven't generated the signal yet for 13536 * this urgent pointer value, do it now. Also, 13537 * send up a zero-length M_DATA indicating 13538 * whether or not this is the mark. The latter 13539 * is not needed when a T_EXDATA_IND is sent up. 13540 * However, if there are allocation failures 13541 * this code relies on the sender retransmitting 13542 * and the socket code for determining the mark 13543 * should not block waiting for the peer to 13544 * transmit. Thus, for simplicity we always 13545 * send up the mark indication. 13546 */ 13547 mp1 = allocb(0, BPRI_MED); 13548 if (mp1 == NULL) { 13549 freemsg(mp); 13550 return; 13551 } 13552 if (!TCP_IS_DETACHED(tcp) && 13553 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13554 SIGURG)) { 13555 /* Try again on the rexmit. */ 13556 freemsg(mp1); 13557 freemsg(mp); 13558 return; 13559 } 13560 /* 13561 * Mark with NOTMARKNEXT for now. 13562 * The code below will change this to MARKNEXT 13563 * if we are at the mark. 13564 * 13565 * If there are allocation failures (e.g. in 13566 * dupmsg below) the next time tcp_rput_data 13567 * sees the urgent segment it will send up the 13568 * MSGMARKNEXT message. 13569 */ 13570 mp1->b_flag |= MSGNOTMARKNEXT; 13571 freemsg(tcp->tcp_urp_mark_mp); 13572 tcp->tcp_urp_mark_mp = mp1; 13573 flags |= TH_SEND_URP_MARK; 13574 #ifdef DEBUG 13575 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13576 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13577 "last %x, %s", 13578 seg_seq, urp, tcp->tcp_urp_last, 13579 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13580 #endif /* DEBUG */ 13581 } 13582 tcp->tcp_urp_last_valid = B_TRUE; 13583 tcp->tcp_urp_last = urp + seg_seq; 13584 } else if (tcp->tcp_urp_mark_mp != NULL) { 13585 /* 13586 * An allocation failure prevented the previous 13587 * tcp_rput_data from sending up the allocated 13588 * MSG*MARKNEXT message - send it up this time 13589 * around. 13590 */ 13591 flags |= TH_SEND_URP_MARK; 13592 } 13593 13594 /* 13595 * If the urgent byte is in this segment, make sure that it is 13596 * all by itself. This makes it much easier to deal with the 13597 * possibility of an allocation failure on the T_exdata_ind. 13598 * Note that seg_len is the number of bytes in the segment, and 13599 * urp is the offset into the segment of the urgent byte. 13600 * urp < seg_len means that the urgent byte is in this segment. 13601 */ 13602 if (urp < seg_len) { 13603 if (seg_len != 1) { 13604 uint32_t tmp_rnxt; 13605 /* 13606 * Break it up and feed it back in. 13607 * Re-attach the IP header. 13608 */ 13609 mp->b_rptr = iphdr; 13610 if (urp > 0) { 13611 /* 13612 * There is stuff before the urgent 13613 * byte. 13614 */ 13615 mp1 = dupmsg(mp); 13616 if (!mp1) { 13617 /* 13618 * Trim from urgent byte on. 13619 * The rest will come back. 13620 */ 13621 (void) adjmsg(mp, 13622 urp - seg_len); 13623 tcp_rput_data(connp, 13624 mp, NULL); 13625 return; 13626 } 13627 (void) adjmsg(mp1, urp - seg_len); 13628 /* Feed this piece back in. */ 13629 tmp_rnxt = tcp->tcp_rnxt; 13630 tcp_rput_data(connp, mp1, NULL); 13631 /* 13632 * If the data passed back in was not 13633 * processed (ie: bad ACK) sending 13634 * the remainder back in will cause a 13635 * loop. In this case, drop the 13636 * packet and let the sender try 13637 * sending a good packet. 13638 */ 13639 if (tmp_rnxt == tcp->tcp_rnxt) { 13640 freemsg(mp); 13641 return; 13642 } 13643 } 13644 if (urp != seg_len - 1) { 13645 uint32_t tmp_rnxt; 13646 /* 13647 * There is stuff after the urgent 13648 * byte. 13649 */ 13650 mp1 = dupmsg(mp); 13651 if (!mp1) { 13652 /* 13653 * Trim everything beyond the 13654 * urgent byte. The rest will 13655 * come back. 13656 */ 13657 (void) adjmsg(mp, 13658 urp + 1 - seg_len); 13659 tcp_rput_data(connp, 13660 mp, NULL); 13661 return; 13662 } 13663 (void) adjmsg(mp1, urp + 1 - seg_len); 13664 tmp_rnxt = tcp->tcp_rnxt; 13665 tcp_rput_data(connp, mp1, NULL); 13666 /* 13667 * If the data passed back in was not 13668 * processed (ie: bad ACK) sending 13669 * the remainder back in will cause a 13670 * loop. In this case, drop the 13671 * packet and let the sender try 13672 * sending a good packet. 13673 */ 13674 if (tmp_rnxt == tcp->tcp_rnxt) { 13675 freemsg(mp); 13676 return; 13677 } 13678 } 13679 tcp_rput_data(connp, mp, NULL); 13680 return; 13681 } 13682 /* 13683 * This segment contains only the urgent byte. We 13684 * have to allocate the T_exdata_ind, if we can. 13685 */ 13686 if (IPCL_IS_NONSTR(connp)) { 13687 int error; 13688 13689 (*connp->conn_upcalls->su_recv) 13690 (connp->conn_upper_handle, mp, seg_len, 13691 MSG_OOB, &error, NULL); 13692 /* 13693 * We should never be in middle of a 13694 * fallback, the squeue guarantees that. 13695 */ 13696 ASSERT(error != EOPNOTSUPP); 13697 mp = NULL; 13698 goto update_ack; 13699 } else if (!tcp->tcp_urp_mp) { 13700 struct T_exdata_ind *tei; 13701 mp1 = allocb(sizeof (struct T_exdata_ind), 13702 BPRI_MED); 13703 if (!mp1) { 13704 /* 13705 * Sigh... It'll be back. 13706 * Generate any MSG*MARK message now. 13707 */ 13708 freemsg(mp); 13709 seg_len = 0; 13710 if (flags & TH_SEND_URP_MARK) { 13711 13712 13713 ASSERT(tcp->tcp_urp_mark_mp); 13714 tcp->tcp_urp_mark_mp->b_flag &= 13715 ~MSGNOTMARKNEXT; 13716 tcp->tcp_urp_mark_mp->b_flag |= 13717 MSGMARKNEXT; 13718 } 13719 goto ack_check; 13720 } 13721 mp1->b_datap->db_type = M_PROTO; 13722 tei = (struct T_exdata_ind *)mp1->b_rptr; 13723 tei->PRIM_type = T_EXDATA_IND; 13724 tei->MORE_flag = 0; 13725 mp1->b_wptr = (uchar_t *)&tei[1]; 13726 tcp->tcp_urp_mp = mp1; 13727 #ifdef DEBUG 13728 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13729 "tcp_rput: allocated exdata_ind %s", 13730 tcp_display(tcp, NULL, 13731 DISP_PORT_ONLY)); 13732 #endif /* DEBUG */ 13733 /* 13734 * There is no need to send a separate MSG*MARK 13735 * message since the T_EXDATA_IND will be sent 13736 * now. 13737 */ 13738 flags &= ~TH_SEND_URP_MARK; 13739 freemsg(tcp->tcp_urp_mark_mp); 13740 tcp->tcp_urp_mark_mp = NULL; 13741 } 13742 /* 13743 * Now we are all set. On the next putnext upstream, 13744 * tcp_urp_mp will be non-NULL and will get prepended 13745 * to what has to be this piece containing the urgent 13746 * byte. If for any reason we abort this segment below, 13747 * if it comes back, we will have this ready, or it 13748 * will get blown off in close. 13749 */ 13750 } else if (urp == seg_len) { 13751 /* 13752 * The urgent byte is the next byte after this sequence 13753 * number. If there is data it is marked with 13754 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13755 * since it is not needed. Otherwise, if the code 13756 * above just allocated a zero-length tcp_urp_mark_mp 13757 * message, that message is tagged with MSGMARKNEXT. 13758 * Sending up these MSGMARKNEXT messages makes 13759 * SIOCATMARK work correctly even though 13760 * the T_EXDATA_IND will not be sent up until the 13761 * urgent byte arrives. 13762 */ 13763 if (seg_len != 0) { 13764 flags |= TH_MARKNEXT_NEEDED; 13765 freemsg(tcp->tcp_urp_mark_mp); 13766 tcp->tcp_urp_mark_mp = NULL; 13767 flags &= ~TH_SEND_URP_MARK; 13768 } else if (tcp->tcp_urp_mark_mp != NULL) { 13769 flags |= TH_SEND_URP_MARK; 13770 tcp->tcp_urp_mark_mp->b_flag &= 13771 ~MSGNOTMARKNEXT; 13772 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13773 } 13774 #ifdef DEBUG 13775 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13776 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13777 seg_len, flags, 13778 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13779 #endif /* DEBUG */ 13780 } 13781 #ifdef DEBUG 13782 else { 13783 /* Data left until we hit mark */ 13784 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13785 "tcp_rput: URP %d bytes left, %s", 13786 urp - seg_len, tcp_display(tcp, NULL, 13787 DISP_PORT_ONLY)); 13788 } 13789 #endif /* DEBUG */ 13790 } 13791 13792 process_ack: 13793 if (!(flags & TH_ACK)) { 13794 freemsg(mp); 13795 goto xmit_check; 13796 } 13797 } 13798 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13799 13800 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13801 tcp->tcp_ip_forward_progress = B_TRUE; 13802 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13803 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13804 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13805 /* 3-way handshake complete - pass up the T_CONN_IND */ 13806 tcp_t *listener = tcp->tcp_listener; 13807 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13808 13809 tcp->tcp_tconnind_started = B_TRUE; 13810 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13811 /* 13812 * We are here means eager is fine but it can 13813 * get a TH_RST at any point between now and till 13814 * accept completes and disappear. We need to 13815 * ensure that reference to eager is valid after 13816 * we get out of eager's perimeter. So we do 13817 * an extra refhold. 13818 */ 13819 CONN_INC_REF(connp); 13820 13821 /* 13822 * The listener also exists because of the refhold 13823 * done in tcp_conn_request. Its possible that it 13824 * might have closed. We will check that once we 13825 * get inside listeners context. 13826 */ 13827 CONN_INC_REF(listener->tcp_connp); 13828 if (listener->tcp_connp->conn_sqp == 13829 connp->conn_sqp) { 13830 /* 13831 * We optimize by not calling an SQUEUE_ENTER 13832 * on the listener since we know that the 13833 * listener and eager squeues are the same. 13834 * We are able to make this check safely only 13835 * because neither the eager nor the listener 13836 * can change its squeue. Only an active connect 13837 * can change its squeue 13838 */ 13839 tcp_send_conn_ind(listener->tcp_connp, mp, 13840 listener->tcp_connp->conn_sqp); 13841 CONN_DEC_REF(listener->tcp_connp); 13842 } else if (!tcp->tcp_loopback) { 13843 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13844 mp, tcp_send_conn_ind, 13845 listener->tcp_connp, SQ_FILL, 13846 SQTAG_TCP_CONN_IND); 13847 } else { 13848 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13849 mp, tcp_send_conn_ind, 13850 listener->tcp_connp, SQ_PROCESS, 13851 SQTAG_TCP_CONN_IND); 13852 } 13853 } 13854 13855 if (tcp->tcp_active_open) { 13856 /* 13857 * We are seeing the final ack in the three way 13858 * hand shake of a active open'ed connection 13859 * so we must send up a T_CONN_CON 13860 */ 13861 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13862 freemsg(mp); 13863 return; 13864 } 13865 /* 13866 * Don't fuse the loopback endpoints for 13867 * simultaneous active opens. 13868 */ 13869 if (tcp->tcp_loopback) { 13870 TCP_STAT(tcps, tcp_fusion_unfusable); 13871 tcp->tcp_unfusable = B_TRUE; 13872 } 13873 } 13874 13875 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 13876 bytes_acked--; 13877 /* SYN was acked - making progress */ 13878 if (tcp->tcp_ipversion == IPV6_VERSION) 13879 tcp->tcp_ip_forward_progress = B_TRUE; 13880 13881 /* 13882 * If SYN was retransmitted, need to reset all 13883 * retransmission info as this segment will be 13884 * treated as a dup ACK. 13885 */ 13886 if (tcp->tcp_rexmit) { 13887 tcp->tcp_rexmit = B_FALSE; 13888 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13889 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13890 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13891 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13892 tcp->tcp_ms_we_have_waited = 0; 13893 tcp->tcp_cwnd = mss; 13894 } 13895 13896 /* 13897 * We set the send window to zero here. 13898 * This is needed if there is data to be 13899 * processed already on the queue. 13900 * Later (at swnd_update label), the 13901 * "new_swnd > tcp_swnd" condition is satisfied 13902 * the XMIT_NEEDED flag is set in the current 13903 * (SYN_RCVD) state. This ensures tcp_wput_data() is 13904 * called if there is already data on queue in 13905 * this state. 13906 */ 13907 tcp->tcp_swnd = 0; 13908 13909 if (new_swnd > tcp->tcp_max_swnd) 13910 tcp->tcp_max_swnd = new_swnd; 13911 tcp->tcp_swl1 = seg_seq; 13912 tcp->tcp_swl2 = seg_ack; 13913 tcp->tcp_state = TCPS_ESTABLISHED; 13914 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13915 13916 /* Fuse when both sides are in ESTABLISHED state */ 13917 if (tcp->tcp_loopback && do_tcp_fusion) 13918 tcp_fuse(tcp, iphdr, tcph); 13919 13920 } 13921 /* This code follows 4.4BSD-Lite2 mostly. */ 13922 if (bytes_acked < 0) 13923 goto est; 13924 13925 /* 13926 * If TCP is ECN capable and the congestion experience bit is 13927 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 13928 * done once per window (or more loosely, per RTT). 13929 */ 13930 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 13931 tcp->tcp_cwr = B_FALSE; 13932 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 13933 if (!tcp->tcp_cwr) { 13934 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 13935 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 13936 tcp->tcp_cwnd = npkt * mss; 13937 /* 13938 * If the cwnd is 0, use the timer to clock out 13939 * new segments. This is required by the ECN spec. 13940 */ 13941 if (npkt == 0) { 13942 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13943 /* 13944 * This makes sure that when the ACK comes 13945 * back, we will increase tcp_cwnd by 1 MSS. 13946 */ 13947 tcp->tcp_cwnd_cnt = 0; 13948 } 13949 tcp->tcp_cwr = B_TRUE; 13950 /* 13951 * This marks the end of the current window of in 13952 * flight data. That is why we don't use 13953 * tcp_suna + tcp_swnd. Only data in flight can 13954 * provide ECN info. 13955 */ 13956 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13957 tcp->tcp_ecn_cwr_sent = B_FALSE; 13958 } 13959 } 13960 13961 mp1 = tcp->tcp_xmit_head; 13962 if (bytes_acked == 0) { 13963 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 13964 int dupack_cnt; 13965 13966 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 13967 /* 13968 * Fast retransmit. When we have seen exactly three 13969 * identical ACKs while we have unacked data 13970 * outstanding we take it as a hint that our peer 13971 * dropped something. 13972 * 13973 * If TCP is retransmitting, don't do fast retransmit. 13974 */ 13975 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 13976 ! tcp->tcp_rexmit) { 13977 /* Do Limited Transmit */ 13978 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 13979 tcps->tcps_dupack_fast_retransmit) { 13980 /* 13981 * RFC 3042 13982 * 13983 * What we need to do is temporarily 13984 * increase tcp_cwnd so that new 13985 * data can be sent if it is allowed 13986 * by the receive window (tcp_rwnd). 13987 * tcp_wput_data() will take care of 13988 * the rest. 13989 * 13990 * If the connection is SACK capable, 13991 * only do limited xmit when there 13992 * is SACK info. 13993 * 13994 * Note how tcp_cwnd is incremented. 13995 * The first dup ACK will increase 13996 * it by 1 MSS. The second dup ACK 13997 * will increase it by 2 MSS. This 13998 * means that only 1 new segment will 13999 * be sent for each dup ACK. 14000 */ 14001 if (tcp->tcp_unsent > 0 && 14002 (!tcp->tcp_snd_sack_ok || 14003 (tcp->tcp_snd_sack_ok && 14004 tcp->tcp_notsack_list != NULL))) { 14005 tcp->tcp_cwnd += mss << 14006 (tcp->tcp_dupack_cnt - 1); 14007 flags |= TH_LIMIT_XMIT; 14008 } 14009 } else if (dupack_cnt == 14010 tcps->tcps_dupack_fast_retransmit) { 14011 14012 /* 14013 * If we have reduced tcp_ssthresh 14014 * because of ECN, do not reduce it again 14015 * unless it is already one window of data 14016 * away. After one window of data, tcp_cwr 14017 * should then be cleared. Note that 14018 * for non ECN capable connection, tcp_cwr 14019 * should always be false. 14020 * 14021 * Adjust cwnd since the duplicate 14022 * ack indicates that a packet was 14023 * dropped (due to congestion.) 14024 */ 14025 if (!tcp->tcp_cwr) { 14026 npkt = ((tcp->tcp_snxt - 14027 tcp->tcp_suna) >> 1) / mss; 14028 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14029 mss; 14030 tcp->tcp_cwnd = (npkt + 14031 tcp->tcp_dupack_cnt) * mss; 14032 } 14033 if (tcp->tcp_ecn_ok) { 14034 tcp->tcp_cwr = B_TRUE; 14035 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14036 tcp->tcp_ecn_cwr_sent = B_FALSE; 14037 } 14038 14039 /* 14040 * We do Hoe's algorithm. Refer to her 14041 * paper "Improving the Start-up Behavior 14042 * of a Congestion Control Scheme for TCP," 14043 * appeared in SIGCOMM'96. 14044 * 14045 * Save highest seq no we have sent so far. 14046 * Be careful about the invisible FIN byte. 14047 */ 14048 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14049 (tcp->tcp_unsent == 0)) { 14050 tcp->tcp_rexmit_max = tcp->tcp_fss; 14051 } else { 14052 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14053 } 14054 14055 /* 14056 * Do not allow bursty traffic during. 14057 * fast recovery. Refer to Fall and Floyd's 14058 * paper "Simulation-based Comparisons of 14059 * Tahoe, Reno and SACK TCP" (in CCR?) 14060 * This is a best current practise. 14061 */ 14062 tcp->tcp_snd_burst = TCP_CWND_SS; 14063 14064 /* 14065 * For SACK: 14066 * Calculate tcp_pipe, which is the 14067 * estimated number of bytes in 14068 * network. 14069 * 14070 * tcp_fack is the highest sack'ed seq num 14071 * TCP has received. 14072 * 14073 * tcp_pipe is explained in the above quoted 14074 * Fall and Floyd's paper. tcp_fack is 14075 * explained in Mathis and Mahdavi's 14076 * "Forward Acknowledgment: Refining TCP 14077 * Congestion Control" in SIGCOMM '96. 14078 */ 14079 if (tcp->tcp_snd_sack_ok) { 14080 ASSERT(tcp->tcp_sack_info != NULL); 14081 if (tcp->tcp_notsack_list != NULL) { 14082 tcp->tcp_pipe = tcp->tcp_snxt - 14083 tcp->tcp_fack; 14084 tcp->tcp_sack_snxt = seg_ack; 14085 flags |= TH_NEED_SACK_REXMIT; 14086 } else { 14087 /* 14088 * Always initialize tcp_pipe 14089 * even though we don't have 14090 * any SACK info. If later 14091 * we get SACK info and 14092 * tcp_pipe is not initialized, 14093 * funny things will happen. 14094 */ 14095 tcp->tcp_pipe = 14096 tcp->tcp_cwnd_ssthresh; 14097 } 14098 } else { 14099 flags |= TH_REXMIT_NEEDED; 14100 } /* tcp_snd_sack_ok */ 14101 14102 } else { 14103 /* 14104 * Here we perform congestion 14105 * avoidance, but NOT slow start. 14106 * This is known as the Fast 14107 * Recovery Algorithm. 14108 */ 14109 if (tcp->tcp_snd_sack_ok && 14110 tcp->tcp_notsack_list != NULL) { 14111 flags |= TH_NEED_SACK_REXMIT; 14112 tcp->tcp_pipe -= mss; 14113 if (tcp->tcp_pipe < 0) 14114 tcp->tcp_pipe = 0; 14115 } else { 14116 /* 14117 * We know that one more packet has 14118 * left the pipe thus we can update 14119 * cwnd. 14120 */ 14121 cwnd = tcp->tcp_cwnd + mss; 14122 if (cwnd > tcp->tcp_cwnd_max) 14123 cwnd = tcp->tcp_cwnd_max; 14124 tcp->tcp_cwnd = cwnd; 14125 if (tcp->tcp_unsent > 0) 14126 flags |= TH_XMIT_NEEDED; 14127 } 14128 } 14129 } 14130 } else if (tcp->tcp_zero_win_probe) { 14131 /* 14132 * If the window has opened, need to arrange 14133 * to send additional data. 14134 */ 14135 if (new_swnd != 0) { 14136 /* tcp_suna != tcp_snxt */ 14137 /* Packet contains a window update */ 14138 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14139 tcp->tcp_zero_win_probe = 0; 14140 tcp->tcp_timer_backoff = 0; 14141 tcp->tcp_ms_we_have_waited = 0; 14142 14143 /* 14144 * Transmit starting with tcp_suna since 14145 * the one byte probe is not ack'ed. 14146 * If TCP has sent more than one identical 14147 * probe, tcp_rexmit will be set. That means 14148 * tcp_ss_rexmit() will send out the one 14149 * byte along with new data. Otherwise, 14150 * fake the retransmission. 14151 */ 14152 flags |= TH_XMIT_NEEDED; 14153 if (!tcp->tcp_rexmit) { 14154 tcp->tcp_rexmit = B_TRUE; 14155 tcp->tcp_dupack_cnt = 0; 14156 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14157 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14158 } 14159 } 14160 } 14161 goto swnd_update; 14162 } 14163 14164 /* 14165 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14166 * If the ACK value acks something that we have not yet sent, it might 14167 * be an old duplicate segment. Send an ACK to re-synchronize the 14168 * other side. 14169 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14170 * state is handled above, so we can always just drop the segment and 14171 * send an ACK here. 14172 * 14173 * Should we send ACKs in response to ACK only segments? 14174 */ 14175 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14176 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14177 /* drop the received segment */ 14178 freemsg(mp); 14179 14180 /* 14181 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14182 * greater than 0, check if the number of such 14183 * bogus ACks is greater than that count. If yes, 14184 * don't send back any ACK. This prevents TCP from 14185 * getting into an ACK storm if somehow an attacker 14186 * successfully spoofs an acceptable segment to our 14187 * peer. 14188 */ 14189 if (tcp_drop_ack_unsent_cnt > 0 && 14190 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14191 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14192 return; 14193 } 14194 mp = tcp_ack_mp(tcp); 14195 if (mp != NULL) { 14196 BUMP_LOCAL(tcp->tcp_obsegs); 14197 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14198 tcp_send_data(tcp, tcp->tcp_wq, mp); 14199 } 14200 return; 14201 } 14202 14203 /* 14204 * TCP gets a new ACK, update the notsack'ed list to delete those 14205 * blocks that are covered by this ACK. 14206 */ 14207 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14208 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14209 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14210 } 14211 14212 /* 14213 * If we got an ACK after fast retransmit, check to see 14214 * if it is a partial ACK. If it is not and the congestion 14215 * window was inflated to account for the other side's 14216 * cached packets, retract it. If it is, do Hoe's algorithm. 14217 */ 14218 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14219 ASSERT(tcp->tcp_rexmit == B_FALSE); 14220 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14221 tcp->tcp_dupack_cnt = 0; 14222 /* 14223 * Restore the orig tcp_cwnd_ssthresh after 14224 * fast retransmit phase. 14225 */ 14226 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14227 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14228 } 14229 tcp->tcp_rexmit_max = seg_ack; 14230 tcp->tcp_cwnd_cnt = 0; 14231 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14232 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14233 14234 /* 14235 * Remove all notsack info to avoid confusion with 14236 * the next fast retrasnmit/recovery phase. 14237 */ 14238 if (tcp->tcp_snd_sack_ok && 14239 tcp->tcp_notsack_list != NULL) { 14240 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14241 } 14242 } else { 14243 if (tcp->tcp_snd_sack_ok && 14244 tcp->tcp_notsack_list != NULL) { 14245 flags |= TH_NEED_SACK_REXMIT; 14246 tcp->tcp_pipe -= mss; 14247 if (tcp->tcp_pipe < 0) 14248 tcp->tcp_pipe = 0; 14249 } else { 14250 /* 14251 * Hoe's algorithm: 14252 * 14253 * Retransmit the unack'ed segment and 14254 * restart fast recovery. Note that we 14255 * need to scale back tcp_cwnd to the 14256 * original value when we started fast 14257 * recovery. This is to prevent overly 14258 * aggressive behaviour in sending new 14259 * segments. 14260 */ 14261 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14262 tcps->tcps_dupack_fast_retransmit * mss; 14263 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14264 flags |= TH_REXMIT_NEEDED; 14265 } 14266 } 14267 } else { 14268 tcp->tcp_dupack_cnt = 0; 14269 if (tcp->tcp_rexmit) { 14270 /* 14271 * TCP is retranmitting. If the ACK ack's all 14272 * outstanding data, update tcp_rexmit_max and 14273 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14274 * to the correct value. 14275 * 14276 * Note that SEQ_LEQ() is used. This is to avoid 14277 * unnecessary fast retransmit caused by dup ACKs 14278 * received when TCP does slow start retransmission 14279 * after a time out. During this phase, TCP may 14280 * send out segments which are already received. 14281 * This causes dup ACKs to be sent back. 14282 */ 14283 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14284 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14285 tcp->tcp_rexmit_nxt = seg_ack; 14286 } 14287 if (seg_ack != tcp->tcp_rexmit_max) { 14288 flags |= TH_XMIT_NEEDED; 14289 } 14290 } else { 14291 tcp->tcp_rexmit = B_FALSE; 14292 tcp->tcp_xmit_zc_clean = B_FALSE; 14293 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14294 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14295 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14296 } 14297 tcp->tcp_ms_we_have_waited = 0; 14298 } 14299 } 14300 14301 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14302 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14303 tcp->tcp_suna = seg_ack; 14304 if (tcp->tcp_zero_win_probe != 0) { 14305 tcp->tcp_zero_win_probe = 0; 14306 tcp->tcp_timer_backoff = 0; 14307 } 14308 14309 /* 14310 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14311 * Note that it cannot be the SYN being ack'ed. The code flow 14312 * will not reach here. 14313 */ 14314 if (mp1 == NULL) { 14315 goto fin_acked; 14316 } 14317 14318 /* 14319 * Update the congestion window. 14320 * 14321 * If TCP is not ECN capable or TCP is ECN capable but the 14322 * congestion experience bit is not set, increase the tcp_cwnd as 14323 * usual. 14324 */ 14325 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14326 cwnd = tcp->tcp_cwnd; 14327 add = mss; 14328 14329 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14330 /* 14331 * This is to prevent an increase of less than 1 MSS of 14332 * tcp_cwnd. With partial increase, tcp_wput_data() 14333 * may send out tinygrams in order to preserve mblk 14334 * boundaries. 14335 * 14336 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14337 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14338 * increased by 1 MSS for every RTTs. 14339 */ 14340 if (tcp->tcp_cwnd_cnt <= 0) { 14341 tcp->tcp_cwnd_cnt = cwnd + add; 14342 } else { 14343 tcp->tcp_cwnd_cnt -= add; 14344 add = 0; 14345 } 14346 } 14347 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14348 } 14349 14350 /* See if the latest urgent data has been acknowledged */ 14351 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14352 SEQ_GT(seg_ack, tcp->tcp_urg)) 14353 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14354 14355 /* Can we update the RTT estimates? */ 14356 if (tcp->tcp_snd_ts_ok) { 14357 /* Ignore zero timestamp echo-reply. */ 14358 if (tcpopt.tcp_opt_ts_ecr != 0) { 14359 tcp_set_rto(tcp, (int32_t)lbolt - 14360 (int32_t)tcpopt.tcp_opt_ts_ecr); 14361 } 14362 14363 /* If needed, restart the timer. */ 14364 if (tcp->tcp_set_timer == 1) { 14365 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14366 tcp->tcp_set_timer = 0; 14367 } 14368 /* 14369 * Update tcp_csuna in case the other side stops sending 14370 * us timestamps. 14371 */ 14372 tcp->tcp_csuna = tcp->tcp_snxt; 14373 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14374 /* 14375 * An ACK sequence we haven't seen before, so get the RTT 14376 * and update the RTO. But first check if the timestamp is 14377 * valid to use. 14378 */ 14379 if ((mp1->b_next != NULL) && 14380 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14381 tcp_set_rto(tcp, (int32_t)lbolt - 14382 (int32_t)(intptr_t)mp1->b_prev); 14383 else 14384 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14385 14386 /* Remeber the last sequence to be ACKed */ 14387 tcp->tcp_csuna = seg_ack; 14388 if (tcp->tcp_set_timer == 1) { 14389 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14390 tcp->tcp_set_timer = 0; 14391 } 14392 } else { 14393 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14394 } 14395 14396 /* Eat acknowledged bytes off the xmit queue. */ 14397 for (;;) { 14398 mblk_t *mp2; 14399 uchar_t *wptr; 14400 14401 wptr = mp1->b_wptr; 14402 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14403 bytes_acked -= (int)(wptr - mp1->b_rptr); 14404 if (bytes_acked < 0) { 14405 mp1->b_rptr = wptr + bytes_acked; 14406 /* 14407 * Set a new timestamp if all the bytes timed by the 14408 * old timestamp have been ack'ed. 14409 */ 14410 if (SEQ_GT(seg_ack, 14411 (uint32_t)(uintptr_t)(mp1->b_next))) { 14412 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14413 mp1->b_next = NULL; 14414 } 14415 break; 14416 } 14417 mp1->b_next = NULL; 14418 mp1->b_prev = NULL; 14419 mp2 = mp1; 14420 mp1 = mp1->b_cont; 14421 14422 /* 14423 * This notification is required for some zero-copy 14424 * clients to maintain a copy semantic. After the data 14425 * is ack'ed, client is safe to modify or reuse the buffer. 14426 */ 14427 if (tcp->tcp_snd_zcopy_aware && 14428 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14429 tcp_zcopy_notify(tcp); 14430 freeb(mp2); 14431 if (bytes_acked == 0) { 14432 if (mp1 == NULL) { 14433 /* Everything is ack'ed, clear the tail. */ 14434 tcp->tcp_xmit_tail = NULL; 14435 /* 14436 * Cancel the timer unless we are still 14437 * waiting for an ACK for the FIN packet. 14438 */ 14439 if (tcp->tcp_timer_tid != 0 && 14440 tcp->tcp_snxt == tcp->tcp_suna) { 14441 (void) TCP_TIMER_CANCEL(tcp, 14442 tcp->tcp_timer_tid); 14443 tcp->tcp_timer_tid = 0; 14444 } 14445 goto pre_swnd_update; 14446 } 14447 if (mp2 != tcp->tcp_xmit_tail) 14448 break; 14449 tcp->tcp_xmit_tail = mp1; 14450 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14451 (uintptr_t)INT_MAX); 14452 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14453 mp1->b_rptr); 14454 break; 14455 } 14456 if (mp1 == NULL) { 14457 /* 14458 * More was acked but there is nothing more 14459 * outstanding. This means that the FIN was 14460 * just acked or that we're talking to a clown. 14461 */ 14462 fin_acked: 14463 ASSERT(tcp->tcp_fin_sent); 14464 tcp->tcp_xmit_tail = NULL; 14465 if (tcp->tcp_fin_sent) { 14466 /* FIN was acked - making progress */ 14467 if (tcp->tcp_ipversion == IPV6_VERSION && 14468 !tcp->tcp_fin_acked) 14469 tcp->tcp_ip_forward_progress = B_TRUE; 14470 tcp->tcp_fin_acked = B_TRUE; 14471 if (tcp->tcp_linger_tid != 0 && 14472 TCP_TIMER_CANCEL(tcp, 14473 tcp->tcp_linger_tid) >= 0) { 14474 tcp_stop_lingering(tcp); 14475 freemsg(mp); 14476 mp = NULL; 14477 } 14478 } else { 14479 /* 14480 * We should never get here because 14481 * we have already checked that the 14482 * number of bytes ack'ed should be 14483 * smaller than or equal to what we 14484 * have sent so far (it is the 14485 * acceptability check of the ACK). 14486 * We can only get here if the send 14487 * queue is corrupted. 14488 * 14489 * Terminate the connection and 14490 * panic the system. It is better 14491 * for us to panic instead of 14492 * continuing to avoid other disaster. 14493 */ 14494 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14495 tcp->tcp_rnxt, TH_RST|TH_ACK); 14496 panic("Memory corruption " 14497 "detected for connection %s.", 14498 tcp_display(tcp, NULL, 14499 DISP_ADDR_AND_PORT)); 14500 /*NOTREACHED*/ 14501 } 14502 goto pre_swnd_update; 14503 } 14504 ASSERT(mp2 != tcp->tcp_xmit_tail); 14505 } 14506 if (tcp->tcp_unsent) { 14507 flags |= TH_XMIT_NEEDED; 14508 } 14509 pre_swnd_update: 14510 tcp->tcp_xmit_head = mp1; 14511 swnd_update: 14512 /* 14513 * The following check is different from most other implementations. 14514 * For bi-directional transfer, when segments are dropped, the 14515 * "normal" check will not accept a window update in those 14516 * retransmitted segemnts. Failing to do that, TCP may send out 14517 * segments which are outside receiver's window. As TCP accepts 14518 * the ack in those retransmitted segments, if the window update in 14519 * the same segment is not accepted, TCP will incorrectly calculates 14520 * that it can send more segments. This can create a deadlock 14521 * with the receiver if its window becomes zero. 14522 */ 14523 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14524 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14525 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14526 /* 14527 * The criteria for update is: 14528 * 14529 * 1. the segment acknowledges some data. Or 14530 * 2. the segment is new, i.e. it has a higher seq num. Or 14531 * 3. the segment is not old and the advertised window is 14532 * larger than the previous advertised window. 14533 */ 14534 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14535 flags |= TH_XMIT_NEEDED; 14536 tcp->tcp_swnd = new_swnd; 14537 if (new_swnd > tcp->tcp_max_swnd) 14538 tcp->tcp_max_swnd = new_swnd; 14539 tcp->tcp_swl1 = seg_seq; 14540 tcp->tcp_swl2 = seg_ack; 14541 } 14542 est: 14543 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14544 14545 switch (tcp->tcp_state) { 14546 case TCPS_FIN_WAIT_1: 14547 if (tcp->tcp_fin_acked) { 14548 tcp->tcp_state = TCPS_FIN_WAIT_2; 14549 /* 14550 * We implement the non-standard BSD/SunOS 14551 * FIN_WAIT_2 flushing algorithm. 14552 * If there is no user attached to this 14553 * TCP endpoint, then this TCP struct 14554 * could hang around forever in FIN_WAIT_2 14555 * state if the peer forgets to send us 14556 * a FIN. To prevent this, we wait only 14557 * 2*MSL (a convenient time value) for 14558 * the FIN to arrive. If it doesn't show up, 14559 * we flush the TCP endpoint. This algorithm, 14560 * though a violation of RFC-793, has worked 14561 * for over 10 years in BSD systems. 14562 * Note: SunOS 4.x waits 675 seconds before 14563 * flushing the FIN_WAIT_2 connection. 14564 */ 14565 TCP_TIMER_RESTART(tcp, 14566 tcps->tcps_fin_wait_2_flush_interval); 14567 } 14568 break; 14569 case TCPS_FIN_WAIT_2: 14570 break; /* Shutdown hook? */ 14571 case TCPS_LAST_ACK: 14572 freemsg(mp); 14573 if (tcp->tcp_fin_acked) { 14574 (void) tcp_clean_death(tcp, 0, 19); 14575 return; 14576 } 14577 goto xmit_check; 14578 case TCPS_CLOSING: 14579 if (tcp->tcp_fin_acked) { 14580 tcp->tcp_state = TCPS_TIME_WAIT; 14581 /* 14582 * Unconditionally clear the exclusive binding 14583 * bit so this TIME-WAIT connection won't 14584 * interfere with new ones. 14585 */ 14586 tcp->tcp_exclbind = 0; 14587 if (!TCP_IS_DETACHED(tcp)) { 14588 TCP_TIMER_RESTART(tcp, 14589 tcps->tcps_time_wait_interval); 14590 } else { 14591 tcp_time_wait_append(tcp); 14592 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14593 } 14594 } 14595 /*FALLTHRU*/ 14596 case TCPS_CLOSE_WAIT: 14597 freemsg(mp); 14598 goto xmit_check; 14599 default: 14600 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14601 break; 14602 } 14603 } 14604 if (flags & TH_FIN) { 14605 /* Make sure we ack the fin */ 14606 flags |= TH_ACK_NEEDED; 14607 if (!tcp->tcp_fin_rcvd) { 14608 tcp->tcp_fin_rcvd = B_TRUE; 14609 tcp->tcp_rnxt++; 14610 tcph = tcp->tcp_tcph; 14611 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14612 14613 /* 14614 * Generate the ordrel_ind at the end unless we 14615 * are an eager guy. 14616 * In the eager case tcp_rsrv will do this when run 14617 * after tcp_accept is done. 14618 */ 14619 if (tcp->tcp_listener == NULL && 14620 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14621 flags |= TH_ORDREL_NEEDED; 14622 switch (tcp->tcp_state) { 14623 case TCPS_SYN_RCVD: 14624 case TCPS_ESTABLISHED: 14625 tcp->tcp_state = TCPS_CLOSE_WAIT; 14626 /* Keepalive? */ 14627 break; 14628 case TCPS_FIN_WAIT_1: 14629 if (!tcp->tcp_fin_acked) { 14630 tcp->tcp_state = TCPS_CLOSING; 14631 break; 14632 } 14633 /* FALLTHRU */ 14634 case TCPS_FIN_WAIT_2: 14635 tcp->tcp_state = TCPS_TIME_WAIT; 14636 /* 14637 * Unconditionally clear the exclusive binding 14638 * bit so this TIME-WAIT connection won't 14639 * interfere with new ones. 14640 */ 14641 tcp->tcp_exclbind = 0; 14642 if (!TCP_IS_DETACHED(tcp)) { 14643 TCP_TIMER_RESTART(tcp, 14644 tcps->tcps_time_wait_interval); 14645 } else { 14646 tcp_time_wait_append(tcp); 14647 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14648 } 14649 if (seg_len) { 14650 /* 14651 * implies data piggybacked on FIN. 14652 * break to handle data. 14653 */ 14654 break; 14655 } 14656 freemsg(mp); 14657 goto ack_check; 14658 } 14659 } 14660 } 14661 if (mp == NULL) 14662 goto xmit_check; 14663 if (seg_len == 0) { 14664 freemsg(mp); 14665 goto xmit_check; 14666 } 14667 if (mp->b_rptr == mp->b_wptr) { 14668 /* 14669 * The header has been consumed, so we remove the 14670 * zero-length mblk here. 14671 */ 14672 mp1 = mp; 14673 mp = mp->b_cont; 14674 freeb(mp1); 14675 } 14676 update_ack: 14677 tcph = tcp->tcp_tcph; 14678 tcp->tcp_rack_cnt++; 14679 { 14680 uint32_t cur_max; 14681 14682 cur_max = tcp->tcp_rack_cur_max; 14683 if (tcp->tcp_rack_cnt >= cur_max) { 14684 /* 14685 * We have more unacked data than we should - send 14686 * an ACK now. 14687 */ 14688 flags |= TH_ACK_NEEDED; 14689 cur_max++; 14690 if (cur_max > tcp->tcp_rack_abs_max) 14691 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14692 else 14693 tcp->tcp_rack_cur_max = cur_max; 14694 } else if (TCP_IS_DETACHED(tcp)) { 14695 /* We don't have an ACK timer for detached TCP. */ 14696 flags |= TH_ACK_NEEDED; 14697 } else if (seg_len < mss) { 14698 /* 14699 * If we get a segment that is less than an mss, and we 14700 * already have unacknowledged data, and the amount 14701 * unacknowledged is not a multiple of mss, then we 14702 * better generate an ACK now. Otherwise, this may be 14703 * the tail piece of a transaction, and we would rather 14704 * wait for the response. 14705 */ 14706 uint32_t udif; 14707 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14708 (uintptr_t)INT_MAX); 14709 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14710 if (udif && (udif % mss)) 14711 flags |= TH_ACK_NEEDED; 14712 else 14713 flags |= TH_ACK_TIMER_NEEDED; 14714 } else { 14715 /* Start delayed ack timer */ 14716 flags |= TH_ACK_TIMER_NEEDED; 14717 } 14718 } 14719 tcp->tcp_rnxt += seg_len; 14720 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14721 14722 if (mp == NULL) 14723 goto xmit_check; 14724 14725 /* Update SACK list */ 14726 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14727 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14728 &(tcp->tcp_num_sack_blk)); 14729 } 14730 14731 if (tcp->tcp_urp_mp) { 14732 tcp->tcp_urp_mp->b_cont = mp; 14733 mp = tcp->tcp_urp_mp; 14734 tcp->tcp_urp_mp = NULL; 14735 /* Ready for a new signal. */ 14736 tcp->tcp_urp_last_valid = B_FALSE; 14737 #ifdef DEBUG 14738 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14739 "tcp_rput: sending exdata_ind %s", 14740 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14741 #endif /* DEBUG */ 14742 } 14743 14744 /* 14745 * Check for ancillary data changes compared to last segment. 14746 */ 14747 if (tcp->tcp_ipv6_recvancillary != 0) { 14748 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14749 ASSERT(mp != NULL); 14750 } 14751 14752 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14753 /* 14754 * Side queue inbound data until the accept happens. 14755 * tcp_accept/tcp_rput drains this when the accept happens. 14756 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14757 * T_EXDATA_IND) it is queued on b_next. 14758 * XXX Make urgent data use this. Requires: 14759 * Removing tcp_listener check for TH_URG 14760 * Making M_PCPROTO and MARK messages skip the eager case 14761 */ 14762 14763 if (tcp->tcp_kssl_pending) { 14764 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 14765 mblk_t *, mp); 14766 tcp_kssl_input(tcp, mp); 14767 } else { 14768 tcp_rcv_enqueue(tcp, mp, seg_len); 14769 } 14770 } else { 14771 if (mp->b_datap->db_type != M_DATA || 14772 (flags & TH_MARKNEXT_NEEDED)) { 14773 if (IPCL_IS_NONSTR(connp)) { 14774 int error; 14775 14776 if ((*connp->conn_upcalls->su_recv) 14777 (connp->conn_upper_handle, mp, 14778 seg_len, 0, &error, NULL) <= 0) { 14779 /* 14780 * We should never be in middle of a 14781 * fallback, the squeue guarantees that. 14782 */ 14783 ASSERT(error != EOPNOTSUPP); 14784 if (error == ENOSPC) 14785 tcp->tcp_rwnd -= seg_len; 14786 } 14787 } else if (tcp->tcp_rcv_list != NULL) { 14788 flags |= tcp_rcv_drain(tcp); 14789 } 14790 ASSERT(tcp->tcp_rcv_list == NULL || 14791 tcp->tcp_fused_sigurg); 14792 14793 if (flags & TH_MARKNEXT_NEEDED) { 14794 #ifdef DEBUG 14795 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14796 "tcp_rput: sending MSGMARKNEXT %s", 14797 tcp_display(tcp, NULL, 14798 DISP_PORT_ONLY)); 14799 #endif /* DEBUG */ 14800 mp->b_flag |= MSGMARKNEXT; 14801 flags &= ~TH_MARKNEXT_NEEDED; 14802 } 14803 14804 /* Does this need SSL processing first? */ 14805 if ((tcp->tcp_kssl_ctx != NULL) && 14806 (DB_TYPE(mp) == M_DATA)) { 14807 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 14808 mblk_t *, mp); 14809 tcp_kssl_input(tcp, mp); 14810 } else if (!IPCL_IS_NONSTR(connp)) { 14811 /* Already handled non-STREAMS case. */ 14812 putnext(tcp->tcp_rq, mp); 14813 if (!canputnext(tcp->tcp_rq)) 14814 tcp->tcp_rwnd -= seg_len; 14815 } 14816 } else if ((tcp->tcp_kssl_ctx != NULL) && 14817 (DB_TYPE(mp) == M_DATA)) { 14818 /* Does this need SSL processing first? */ 14819 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 14820 tcp_kssl_input(tcp, mp); 14821 } else if (IPCL_IS_NONSTR(connp)) { 14822 /* Non-STREAMS socket */ 14823 boolean_t push = flags & (TH_PUSH|TH_FIN); 14824 int error; 14825 14826 if ((*connp->conn_upcalls->su_recv)( 14827 connp->conn_upper_handle, 14828 mp, seg_len, 0, &error, &push) <= 0) { 14829 /* 14830 * We should never be in middle of a 14831 * fallback, the squeue guarantees that. 14832 */ 14833 ASSERT(error != EOPNOTSUPP); 14834 if (error == ENOSPC) 14835 tcp->tcp_rwnd -= seg_len; 14836 } else if (push) { 14837 /* 14838 * PUSH bit set and sockfs is not 14839 * flow controlled 14840 */ 14841 flags |= tcp_rwnd_reopen(tcp); 14842 } 14843 } else if ((flags & (TH_PUSH|TH_FIN)) || 14844 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 14845 if (tcp->tcp_rcv_list != NULL) { 14846 /* 14847 * Enqueue the new segment first and then 14848 * call tcp_rcv_drain() to send all data 14849 * up. The other way to do this is to 14850 * send all queued data up and then call 14851 * putnext() to send the new segment up. 14852 * This way can remove the else part later 14853 * on. 14854 * 14855 * We don't do this to avoid one more call to 14856 * canputnext() as tcp_rcv_drain() needs to 14857 * call canputnext(). 14858 */ 14859 tcp_rcv_enqueue(tcp, mp, seg_len); 14860 flags |= tcp_rcv_drain(tcp); 14861 } else { 14862 putnext(tcp->tcp_rq, mp); 14863 if (!canputnext(tcp->tcp_rq)) 14864 tcp->tcp_rwnd -= seg_len; 14865 } 14866 } else { 14867 /* 14868 * Enqueue all packets when processing an mblk 14869 * from the co queue and also enqueue normal packets. 14870 */ 14871 tcp_rcv_enqueue(tcp, mp, seg_len); 14872 } 14873 /* 14874 * Make sure the timer is running if we have data waiting 14875 * for a push bit. This provides resiliency against 14876 * implementations that do not correctly generate push bits. 14877 */ 14878 if (!IPCL_IS_NONSTR(connp) && tcp->tcp_rcv_list != NULL && 14879 tcp->tcp_push_tid == 0) { 14880 /* 14881 * The connection may be closed at this point, so don't 14882 * do anything for a detached tcp. 14883 */ 14884 if (!TCP_IS_DETACHED(tcp)) 14885 tcp->tcp_push_tid = TCP_TIMER(tcp, 14886 tcp_push_timer, 14887 MSEC_TO_TICK( 14888 tcps->tcps_push_timer_interval)); 14889 } 14890 } 14891 14892 xmit_check: 14893 /* Is there anything left to do? */ 14894 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14895 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 14896 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 14897 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14898 goto done; 14899 14900 /* Any transmit work to do and a non-zero window? */ 14901 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 14902 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 14903 if (flags & TH_REXMIT_NEEDED) { 14904 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 14905 14906 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 14907 if (snd_size > mss) 14908 snd_size = mss; 14909 if (snd_size > tcp->tcp_swnd) 14910 snd_size = tcp->tcp_swnd; 14911 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 14912 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 14913 B_TRUE); 14914 14915 if (mp1 != NULL) { 14916 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14917 tcp->tcp_csuna = tcp->tcp_snxt; 14918 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 14919 UPDATE_MIB(&tcps->tcps_mib, 14920 tcpRetransBytes, snd_size); 14921 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14922 } 14923 } 14924 if (flags & TH_NEED_SACK_REXMIT) { 14925 tcp_sack_rxmit(tcp, &flags); 14926 } 14927 /* 14928 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 14929 * out new segment. Note that tcp_rexmit should not be 14930 * set, otherwise TH_LIMIT_XMIT should not be set. 14931 */ 14932 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 14933 if (!tcp->tcp_rexmit) { 14934 tcp_wput_data(tcp, NULL, B_FALSE); 14935 } else { 14936 tcp_ss_rexmit(tcp); 14937 } 14938 } 14939 /* 14940 * Adjust tcp_cwnd back to normal value after sending 14941 * new data segments. 14942 */ 14943 if (flags & TH_LIMIT_XMIT) { 14944 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 14945 /* 14946 * This will restart the timer. Restarting the 14947 * timer is used to avoid a timeout before the 14948 * limited transmitted segment's ACK gets back. 14949 */ 14950 if (tcp->tcp_xmit_head != NULL) 14951 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14952 } 14953 14954 /* Anything more to do? */ 14955 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 14956 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14957 goto done; 14958 } 14959 ack_check: 14960 if (flags & TH_SEND_URP_MARK) { 14961 ASSERT(tcp->tcp_urp_mark_mp); 14962 ASSERT(!IPCL_IS_NONSTR(connp)); 14963 /* 14964 * Send up any queued data and then send the mark message 14965 */ 14966 if (tcp->tcp_rcv_list != NULL) { 14967 flags |= tcp_rcv_drain(tcp); 14968 14969 } 14970 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14971 mp1 = tcp->tcp_urp_mark_mp; 14972 tcp->tcp_urp_mark_mp = NULL; 14973 putnext(tcp->tcp_rq, mp1); 14974 #ifdef DEBUG 14975 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14976 "tcp_rput: sending zero-length %s %s", 14977 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 14978 "MSGNOTMARKNEXT"), 14979 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14980 #endif /* DEBUG */ 14981 flags &= ~TH_SEND_URP_MARK; 14982 } 14983 if (flags & TH_ACK_NEEDED) { 14984 /* 14985 * Time to send an ack for some reason. 14986 */ 14987 mp1 = tcp_ack_mp(tcp); 14988 14989 if (mp1 != NULL) { 14990 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14991 BUMP_LOCAL(tcp->tcp_obsegs); 14992 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14993 } 14994 if (tcp->tcp_ack_tid != 0) { 14995 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 14996 tcp->tcp_ack_tid = 0; 14997 } 14998 } 14999 if (flags & TH_ACK_TIMER_NEEDED) { 15000 /* 15001 * Arrange for deferred ACK or push wait timeout. 15002 * Start timer if it is not already running. 15003 */ 15004 if (tcp->tcp_ack_tid == 0) { 15005 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15006 MSEC_TO_TICK(tcp->tcp_localnet ? 15007 (clock_t)tcps->tcps_local_dack_interval : 15008 (clock_t)tcps->tcps_deferred_ack_interval)); 15009 } 15010 } 15011 if (flags & TH_ORDREL_NEEDED) { 15012 /* 15013 * Send up the ordrel_ind unless we are an eager guy. 15014 * In the eager case tcp_rsrv will do this when run 15015 * after tcp_accept is done. 15016 */ 15017 ASSERT(tcp->tcp_listener == NULL); 15018 15019 if (IPCL_IS_NONSTR(connp)) { 15020 ASSERT(tcp->tcp_ordrel_mp == NULL); 15021 tcp->tcp_ordrel_done = B_TRUE; 15022 (*connp->conn_upcalls->su_opctl) 15023 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15024 goto done; 15025 } 15026 15027 if (tcp->tcp_rcv_list != NULL) { 15028 /* 15029 * Push any mblk(s) enqueued from co processing. 15030 */ 15031 flags |= tcp_rcv_drain(tcp); 15032 } 15033 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15034 15035 mp1 = tcp->tcp_ordrel_mp; 15036 tcp->tcp_ordrel_mp = NULL; 15037 tcp->tcp_ordrel_done = B_TRUE; 15038 putnext(tcp->tcp_rq, mp1); 15039 } 15040 done: 15041 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15042 } 15043 15044 /* 15045 * This function does PAWS protection check. Returns B_TRUE if the 15046 * segment passes the PAWS test, else returns B_FALSE. 15047 */ 15048 boolean_t 15049 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15050 { 15051 uint8_t flags; 15052 int options; 15053 uint8_t *up; 15054 15055 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15056 /* 15057 * If timestamp option is aligned nicely, get values inline, 15058 * otherwise call general routine to parse. Only do that 15059 * if timestamp is the only option. 15060 */ 15061 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15062 TCPOPT_REAL_TS_LEN && 15063 OK_32PTR((up = ((uint8_t *)tcph) + 15064 TCP_MIN_HEADER_LENGTH)) && 15065 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15066 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15067 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15068 15069 options = TCP_OPT_TSTAMP_PRESENT; 15070 } else { 15071 if (tcp->tcp_snd_sack_ok) { 15072 tcpoptp->tcp = tcp; 15073 } else { 15074 tcpoptp->tcp = NULL; 15075 } 15076 options = tcp_parse_options(tcph, tcpoptp); 15077 } 15078 15079 if (options & TCP_OPT_TSTAMP_PRESENT) { 15080 /* 15081 * Do PAWS per RFC 1323 section 4.2. Accept RST 15082 * regardless of the timestamp, page 18 RFC 1323.bis. 15083 */ 15084 if ((flags & TH_RST) == 0 && 15085 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15086 tcp->tcp_ts_recent)) { 15087 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15088 PAWS_TIMEOUT)) { 15089 /* This segment is not acceptable. */ 15090 return (B_FALSE); 15091 } else { 15092 /* 15093 * Connection has been idle for 15094 * too long. Reset the timestamp 15095 * and assume the segment is valid. 15096 */ 15097 tcp->tcp_ts_recent = 15098 tcpoptp->tcp_opt_ts_val; 15099 } 15100 } 15101 } else { 15102 /* 15103 * If we don't get a timestamp on every packet, we 15104 * figure we can't really trust 'em, so we stop sending 15105 * and parsing them. 15106 */ 15107 tcp->tcp_snd_ts_ok = B_FALSE; 15108 15109 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15110 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15111 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15112 /* 15113 * Adjust the tcp_mss accordingly. We also need to 15114 * adjust tcp_cwnd here in accordance with the new mss. 15115 * But we avoid doing a slow start here so as to not 15116 * to lose on the transfer rate built up so far. 15117 */ 15118 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15119 if (tcp->tcp_snd_sack_ok) { 15120 ASSERT(tcp->tcp_sack_info != NULL); 15121 tcp->tcp_max_sack_blk = 4; 15122 } 15123 } 15124 return (B_TRUE); 15125 } 15126 15127 /* 15128 * Attach ancillary data to a received TCP segments for the 15129 * ancillary pieces requested by the application that are 15130 * different than they were in the previous data segment. 15131 * 15132 * Save the "current" values once memory allocation is ok so that 15133 * when memory allocation fails we can just wait for the next data segment. 15134 */ 15135 static mblk_t * 15136 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15137 { 15138 struct T_optdata_ind *todi; 15139 int optlen; 15140 uchar_t *optptr; 15141 struct T_opthdr *toh; 15142 uint_t addflag; /* Which pieces to add */ 15143 mblk_t *mp1; 15144 15145 optlen = 0; 15146 addflag = 0; 15147 /* If app asked for pktinfo and the index has changed ... */ 15148 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15149 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15150 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15151 optlen += sizeof (struct T_opthdr) + 15152 sizeof (struct in6_pktinfo); 15153 addflag |= TCP_IPV6_RECVPKTINFO; 15154 } 15155 /* If app asked for hoplimit and it has changed ... */ 15156 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15157 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15158 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15159 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15160 addflag |= TCP_IPV6_RECVHOPLIMIT; 15161 } 15162 /* If app asked for tclass and it has changed ... */ 15163 if ((ipp->ipp_fields & IPPF_TCLASS) && 15164 ipp->ipp_tclass != tcp->tcp_recvtclass && 15165 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15166 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15167 addflag |= TCP_IPV6_RECVTCLASS; 15168 } 15169 /* 15170 * If app asked for hopbyhop headers and it has changed ... 15171 * For security labels, note that (1) security labels can't change on 15172 * a connected socket at all, (2) we're connected to at most one peer, 15173 * (3) if anything changes, then it must be some other extra option. 15174 */ 15175 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15176 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15177 (ipp->ipp_fields & IPPF_HOPOPTS), 15178 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15179 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15180 tcp->tcp_label_len; 15181 addflag |= TCP_IPV6_RECVHOPOPTS; 15182 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15183 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15184 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15185 return (mp); 15186 } 15187 /* If app asked for dst headers before routing headers ... */ 15188 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15189 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15190 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15191 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15192 optlen += sizeof (struct T_opthdr) + 15193 ipp->ipp_rtdstoptslen; 15194 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15195 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15196 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15197 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15198 return (mp); 15199 } 15200 /* If app asked for routing headers and it has changed ... */ 15201 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15202 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15203 (ipp->ipp_fields & IPPF_RTHDR), 15204 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15205 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15206 addflag |= TCP_IPV6_RECVRTHDR; 15207 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15208 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15209 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15210 return (mp); 15211 } 15212 /* If app asked for dest headers and it has changed ... */ 15213 if ((tcp->tcp_ipv6_recvancillary & 15214 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15215 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15216 (ipp->ipp_fields & IPPF_DSTOPTS), 15217 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15218 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15219 addflag |= TCP_IPV6_RECVDSTOPTS; 15220 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15221 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15222 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15223 return (mp); 15224 } 15225 15226 if (optlen == 0) { 15227 /* Nothing to add */ 15228 return (mp); 15229 } 15230 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15231 if (mp1 == NULL) { 15232 /* 15233 * Defer sending ancillary data until the next TCP segment 15234 * arrives. 15235 */ 15236 return (mp); 15237 } 15238 mp1->b_cont = mp; 15239 mp = mp1; 15240 mp->b_wptr += sizeof (*todi) + optlen; 15241 mp->b_datap->db_type = M_PROTO; 15242 todi = (struct T_optdata_ind *)mp->b_rptr; 15243 todi->PRIM_type = T_OPTDATA_IND; 15244 todi->DATA_flag = 1; /* MORE data */ 15245 todi->OPT_length = optlen; 15246 todi->OPT_offset = sizeof (*todi); 15247 optptr = (uchar_t *)&todi[1]; 15248 /* 15249 * If app asked for pktinfo and the index has changed ... 15250 * Note that the local address never changes for the connection. 15251 */ 15252 if (addflag & TCP_IPV6_RECVPKTINFO) { 15253 struct in6_pktinfo *pkti; 15254 15255 toh = (struct T_opthdr *)optptr; 15256 toh->level = IPPROTO_IPV6; 15257 toh->name = IPV6_PKTINFO; 15258 toh->len = sizeof (*toh) + sizeof (*pkti); 15259 toh->status = 0; 15260 optptr += sizeof (*toh); 15261 pkti = (struct in6_pktinfo *)optptr; 15262 if (tcp->tcp_ipversion == IPV6_VERSION) 15263 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15264 else 15265 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15266 &pkti->ipi6_addr); 15267 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15268 optptr += sizeof (*pkti); 15269 ASSERT(OK_32PTR(optptr)); 15270 /* Save as "last" value */ 15271 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15272 } 15273 /* If app asked for hoplimit and it has changed ... */ 15274 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15275 toh = (struct T_opthdr *)optptr; 15276 toh->level = IPPROTO_IPV6; 15277 toh->name = IPV6_HOPLIMIT; 15278 toh->len = sizeof (*toh) + sizeof (uint_t); 15279 toh->status = 0; 15280 optptr += sizeof (*toh); 15281 *(uint_t *)optptr = ipp->ipp_hoplimit; 15282 optptr += sizeof (uint_t); 15283 ASSERT(OK_32PTR(optptr)); 15284 /* Save as "last" value */ 15285 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15286 } 15287 /* If app asked for tclass and it has changed ... */ 15288 if (addflag & TCP_IPV6_RECVTCLASS) { 15289 toh = (struct T_opthdr *)optptr; 15290 toh->level = IPPROTO_IPV6; 15291 toh->name = IPV6_TCLASS; 15292 toh->len = sizeof (*toh) + sizeof (uint_t); 15293 toh->status = 0; 15294 optptr += sizeof (*toh); 15295 *(uint_t *)optptr = ipp->ipp_tclass; 15296 optptr += sizeof (uint_t); 15297 ASSERT(OK_32PTR(optptr)); 15298 /* Save as "last" value */ 15299 tcp->tcp_recvtclass = ipp->ipp_tclass; 15300 } 15301 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15302 toh = (struct T_opthdr *)optptr; 15303 toh->level = IPPROTO_IPV6; 15304 toh->name = IPV6_HOPOPTS; 15305 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15306 tcp->tcp_label_len; 15307 toh->status = 0; 15308 optptr += sizeof (*toh); 15309 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15310 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15311 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15312 ASSERT(OK_32PTR(optptr)); 15313 /* Save as last value */ 15314 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15315 (ipp->ipp_fields & IPPF_HOPOPTS), 15316 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15317 } 15318 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15319 toh = (struct T_opthdr *)optptr; 15320 toh->level = IPPROTO_IPV6; 15321 toh->name = IPV6_RTHDRDSTOPTS; 15322 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15323 toh->status = 0; 15324 optptr += sizeof (*toh); 15325 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15326 optptr += ipp->ipp_rtdstoptslen; 15327 ASSERT(OK_32PTR(optptr)); 15328 /* Save as last value */ 15329 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15330 &tcp->tcp_rtdstoptslen, 15331 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15332 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15333 } 15334 if (addflag & TCP_IPV6_RECVRTHDR) { 15335 toh = (struct T_opthdr *)optptr; 15336 toh->level = IPPROTO_IPV6; 15337 toh->name = IPV6_RTHDR; 15338 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15339 toh->status = 0; 15340 optptr += sizeof (*toh); 15341 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15342 optptr += ipp->ipp_rthdrlen; 15343 ASSERT(OK_32PTR(optptr)); 15344 /* Save as last value */ 15345 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15346 (ipp->ipp_fields & IPPF_RTHDR), 15347 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15348 } 15349 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15350 toh = (struct T_opthdr *)optptr; 15351 toh->level = IPPROTO_IPV6; 15352 toh->name = IPV6_DSTOPTS; 15353 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15354 toh->status = 0; 15355 optptr += sizeof (*toh); 15356 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15357 optptr += ipp->ipp_dstoptslen; 15358 ASSERT(OK_32PTR(optptr)); 15359 /* Save as last value */ 15360 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15361 (ipp->ipp_fields & IPPF_DSTOPTS), 15362 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15363 } 15364 ASSERT(optptr == mp->b_wptr); 15365 return (mp); 15366 } 15367 15368 /* 15369 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15370 * messages. 15371 */ 15372 void 15373 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15374 { 15375 uchar_t *rptr = mp->b_rptr; 15376 queue_t *q = tcp->tcp_rq; 15377 struct T_error_ack *tea; 15378 15379 switch (mp->b_datap->db_type) { 15380 case M_PROTO: 15381 case M_PCPROTO: 15382 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15383 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15384 break; 15385 tea = (struct T_error_ack *)rptr; 15386 ASSERT(tea->PRIM_type != T_BIND_ACK); 15387 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15388 tea->ERROR_prim != T_BIND_REQ); 15389 switch (tea->PRIM_type) { 15390 case T_ERROR_ACK: 15391 if (tcp->tcp_debug) { 15392 (void) strlog(TCP_MOD_ID, 0, 1, 15393 SL_TRACE|SL_ERROR, 15394 "tcp_rput_other: case T_ERROR_ACK, " 15395 "ERROR_prim == %d", 15396 tea->ERROR_prim); 15397 } 15398 switch (tea->ERROR_prim) { 15399 case T_SVR4_OPTMGMT_REQ: 15400 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15401 /* T_OPTMGMT_REQ generated by TCP */ 15402 printf("T_SVR4_OPTMGMT_REQ failed " 15403 "%d/%d - dropped (cnt %d)\n", 15404 tea->TLI_error, tea->UNIX_error, 15405 tcp->tcp_drop_opt_ack_cnt); 15406 freemsg(mp); 15407 tcp->tcp_drop_opt_ack_cnt--; 15408 return; 15409 } 15410 break; 15411 } 15412 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15413 tcp->tcp_drop_opt_ack_cnt > 0) { 15414 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15415 "- dropped (cnt %d)\n", 15416 tea->TLI_error, tea->UNIX_error, 15417 tcp->tcp_drop_opt_ack_cnt); 15418 freemsg(mp); 15419 tcp->tcp_drop_opt_ack_cnt--; 15420 return; 15421 } 15422 break; 15423 case T_OPTMGMT_ACK: 15424 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15425 /* T_OPTMGMT_REQ generated by TCP */ 15426 freemsg(mp); 15427 tcp->tcp_drop_opt_ack_cnt--; 15428 return; 15429 } 15430 break; 15431 default: 15432 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15433 break; 15434 } 15435 break; 15436 case M_FLUSH: 15437 if (*rptr & FLUSHR) 15438 flushq(q, FLUSHDATA); 15439 break; 15440 default: 15441 /* M_CTL will be directly sent to tcp_icmp_error() */ 15442 ASSERT(DB_TYPE(mp) != M_CTL); 15443 break; 15444 } 15445 /* 15446 * Make sure we set this bit before sending the ACK for 15447 * bind. Otherwise accept could possibly run and free 15448 * this tcp struct. 15449 */ 15450 ASSERT(q != NULL); 15451 putnext(q, mp); 15452 } 15453 15454 /* ARGSUSED */ 15455 static void 15456 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15457 { 15458 conn_t *connp = (conn_t *)arg; 15459 tcp_t *tcp = connp->conn_tcp; 15460 queue_t *q = tcp->tcp_rq; 15461 tcp_stack_t *tcps = tcp->tcp_tcps; 15462 15463 ASSERT(!IPCL_IS_NONSTR(connp)); 15464 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15465 tcp->tcp_rsrv_mp = mp; 15466 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15467 15468 TCP_STAT(tcps, tcp_rsrv_calls); 15469 15470 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15471 return; 15472 } 15473 15474 if (tcp->tcp_fused) { 15475 tcp_fuse_backenable(tcp); 15476 return; 15477 } 15478 15479 if (canputnext(q)) { 15480 /* Not flow-controlled, open rwnd */ 15481 tcp->tcp_rwnd = q->q_hiwat; 15482 15483 /* 15484 * Send back a window update immediately if TCP is above 15485 * ESTABLISHED state and the increase of the rcv window 15486 * that the other side knows is at least 1 MSS after flow 15487 * control is lifted. 15488 */ 15489 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15490 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 15491 tcp_xmit_ctl(NULL, tcp, 15492 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15493 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15494 } 15495 } 15496 } 15497 15498 /* 15499 * The read side service routine is called mostly when we get back-enabled as a 15500 * result of flow control relief. Since we don't actually queue anything in 15501 * TCP, we have no data to send out of here. What we do is clear the receive 15502 * window, and send out a window update. 15503 */ 15504 static void 15505 tcp_rsrv(queue_t *q) 15506 { 15507 conn_t *connp = Q_TO_CONN(q); 15508 tcp_t *tcp = connp->conn_tcp; 15509 mblk_t *mp; 15510 tcp_stack_t *tcps = tcp->tcp_tcps; 15511 15512 /* No code does a putq on the read side */ 15513 ASSERT(q->q_first == NULL); 15514 15515 /* Nothing to do for the default queue */ 15516 if (q == tcps->tcps_g_q) { 15517 return; 15518 } 15519 15520 /* 15521 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 15522 * been run. So just return. 15523 */ 15524 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15525 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 15526 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15527 return; 15528 } 15529 tcp->tcp_rsrv_mp = NULL; 15530 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15531 15532 CONN_INC_REF(connp); 15533 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15534 SQ_PROCESS, SQTAG_TCP_RSRV); 15535 } 15536 15537 /* 15538 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15539 * We do not allow the receive window to shrink. After setting rwnd, 15540 * set the flow control hiwat of the stream. 15541 * 15542 * This function is called in 2 cases: 15543 * 15544 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15545 * connection (passive open) and in tcp_rput_data() for active connect. 15546 * This is called after tcp_mss_set() when the desired MSS value is known. 15547 * This makes sure that our window size is a mutiple of the other side's 15548 * MSS. 15549 * 2) Handling SO_RCVBUF option. 15550 * 15551 * It is ASSUMED that the requested size is a multiple of the current MSS. 15552 * 15553 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15554 * user requests so. 15555 */ 15556 static int 15557 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15558 { 15559 uint32_t mss = tcp->tcp_mss; 15560 uint32_t old_max_rwnd; 15561 uint32_t max_transmittable_rwnd; 15562 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15563 tcp_stack_t *tcps = tcp->tcp_tcps; 15564 15565 if (tcp->tcp_fused) { 15566 size_t sth_hiwat; 15567 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15568 15569 ASSERT(peer_tcp != NULL); 15570 /* 15571 * Record the stream head's high water mark for 15572 * this endpoint; this is used for flow-control 15573 * purposes in tcp_fuse_output(). 15574 */ 15575 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15576 if (!tcp_detached) { 15577 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15578 sth_hiwat); 15579 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 15580 conn_t *connp = tcp->tcp_connp; 15581 struct sock_proto_props sopp; 15582 15583 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 15584 sopp.sopp_rcvthresh = sth_hiwat >> 3; 15585 15586 (*connp->conn_upcalls->su_set_proto_props) 15587 (connp->conn_upper_handle, &sopp); 15588 } 15589 } 15590 15591 /* 15592 * In the fusion case, the maxpsz stream head value of 15593 * our peer is set according to its send buffer size 15594 * and our receive buffer size; since the latter may 15595 * have changed we need to update the peer's maxpsz. 15596 */ 15597 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15598 return (rwnd); 15599 } 15600 15601 if (tcp_detached) { 15602 old_max_rwnd = tcp->tcp_rwnd; 15603 } else { 15604 old_max_rwnd = tcp->tcp_recv_hiwater; 15605 } 15606 15607 /* 15608 * Insist on a receive window that is at least 15609 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15610 * funny TCP interactions of Nagle algorithm, SWS avoidance 15611 * and delayed acknowledgement. 15612 */ 15613 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 15614 15615 /* 15616 * If window size info has already been exchanged, TCP should not 15617 * shrink the window. Shrinking window is doable if done carefully. 15618 * We may add that support later. But so far there is not a real 15619 * need to do that. 15620 */ 15621 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15622 /* MSS may have changed, do a round up again. */ 15623 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15624 } 15625 15626 /* 15627 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15628 * can be applied even before the window scale option is decided. 15629 */ 15630 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15631 if (rwnd > max_transmittable_rwnd) { 15632 rwnd = max_transmittable_rwnd - 15633 (max_transmittable_rwnd % mss); 15634 if (rwnd < mss) 15635 rwnd = max_transmittable_rwnd; 15636 /* 15637 * If we're over the limit we may have to back down tcp_rwnd. 15638 * The increment below won't work for us. So we set all three 15639 * here and the increment below will have no effect. 15640 */ 15641 tcp->tcp_rwnd = old_max_rwnd = rwnd; 15642 } 15643 if (tcp->tcp_localnet) { 15644 tcp->tcp_rack_abs_max = 15645 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 15646 } else { 15647 /* 15648 * For a remote host on a different subnet (through a router), 15649 * we ack every other packet to be conforming to RFC1122. 15650 * tcp_deferred_acks_max is default to 2. 15651 */ 15652 tcp->tcp_rack_abs_max = 15653 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 15654 } 15655 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 15656 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15657 else 15658 tcp->tcp_rack_cur_max = 0; 15659 /* 15660 * Increment the current rwnd by the amount the maximum grew (we 15661 * can not overwrite it since we might be in the middle of a 15662 * connection.) 15663 */ 15664 tcp->tcp_rwnd += rwnd - old_max_rwnd; 15665 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 15666 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 15667 tcp->tcp_cwnd_max = rwnd; 15668 15669 if (tcp_detached) 15670 return (rwnd); 15671 /* 15672 * We set the maximum receive window into rq->q_hiwat if it is 15673 * a STREAMS socket. 15674 * This is not actually used for flow control. 15675 */ 15676 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 15677 tcp->tcp_rq->q_hiwat = rwnd; 15678 tcp->tcp_recv_hiwater = rwnd; 15679 /* 15680 * Set the STREAM head high water mark. This doesn't have to be 15681 * here, since we are simply using default values, but we would 15682 * prefer to choose these values algorithmically, with a likely 15683 * relationship to rwnd. 15684 */ 15685 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15686 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 15687 return (rwnd); 15688 } 15689 15690 /* 15691 * Return SNMP stuff in buffer in mpdata. 15692 */ 15693 mblk_t * 15694 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 15695 { 15696 mblk_t *mpdata; 15697 mblk_t *mp_conn_ctl = NULL; 15698 mblk_t *mp_conn_tail; 15699 mblk_t *mp_attr_ctl = NULL; 15700 mblk_t *mp_attr_tail; 15701 mblk_t *mp6_conn_ctl = NULL; 15702 mblk_t *mp6_conn_tail; 15703 mblk_t *mp6_attr_ctl = NULL; 15704 mblk_t *mp6_attr_tail; 15705 struct opthdr *optp; 15706 mib2_tcpConnEntry_t tce; 15707 mib2_tcp6ConnEntry_t tce6; 15708 mib2_transportMLPEntry_t mlp; 15709 connf_t *connfp; 15710 int i; 15711 boolean_t ispriv; 15712 zoneid_t zoneid; 15713 int v4_conn_idx; 15714 int v6_conn_idx; 15715 conn_t *connp = Q_TO_CONN(q); 15716 tcp_stack_t *tcps; 15717 ip_stack_t *ipst; 15718 mblk_t *mp2ctl; 15719 15720 /* 15721 * make a copy of the original message 15722 */ 15723 mp2ctl = copymsg(mpctl); 15724 15725 if (mpctl == NULL || 15726 (mpdata = mpctl->b_cont) == NULL || 15727 (mp_conn_ctl = copymsg(mpctl)) == NULL || 15728 (mp_attr_ctl = copymsg(mpctl)) == NULL || 15729 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 15730 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 15731 freemsg(mp_conn_ctl); 15732 freemsg(mp_attr_ctl); 15733 freemsg(mp6_conn_ctl); 15734 freemsg(mp6_attr_ctl); 15735 freemsg(mpctl); 15736 freemsg(mp2ctl); 15737 return (NULL); 15738 } 15739 15740 ipst = connp->conn_netstack->netstack_ip; 15741 tcps = connp->conn_netstack->netstack_tcp; 15742 15743 /* build table of connections -- need count in fixed part */ 15744 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 15745 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 15746 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 15747 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 15748 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 15749 15750 ispriv = 15751 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 15752 zoneid = Q_TO_CONN(q)->conn_zoneid; 15753 15754 v4_conn_idx = v6_conn_idx = 0; 15755 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 15756 15757 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 15758 ipst = tcps->tcps_netstack->netstack_ip; 15759 15760 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 15761 15762 connp = NULL; 15763 15764 while ((connp = 15765 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 15766 tcp_t *tcp; 15767 boolean_t needattr; 15768 15769 if (connp->conn_zoneid != zoneid) 15770 continue; /* not in this zone */ 15771 15772 tcp = connp->conn_tcp; 15773 UPDATE_MIB(&tcps->tcps_mib, 15774 tcpHCInSegs, tcp->tcp_ibsegs); 15775 tcp->tcp_ibsegs = 0; 15776 UPDATE_MIB(&tcps->tcps_mib, 15777 tcpHCOutSegs, tcp->tcp_obsegs); 15778 tcp->tcp_obsegs = 0; 15779 15780 tce6.tcp6ConnState = tce.tcpConnState = 15781 tcp_snmp_state(tcp); 15782 if (tce.tcpConnState == MIB2_TCP_established || 15783 tce.tcpConnState == MIB2_TCP_closeWait) 15784 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 15785 15786 needattr = B_FALSE; 15787 bzero(&mlp, sizeof (mlp)); 15788 if (connp->conn_mlp_type != mlptSingle) { 15789 if (connp->conn_mlp_type == mlptShared || 15790 connp->conn_mlp_type == mlptBoth) 15791 mlp.tme_flags |= MIB2_TMEF_SHARED; 15792 if (connp->conn_mlp_type == mlptPrivate || 15793 connp->conn_mlp_type == mlptBoth) 15794 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 15795 needattr = B_TRUE; 15796 } 15797 if (connp->conn_peercred != NULL) { 15798 ts_label_t *tsl; 15799 15800 tsl = crgetlabel(connp->conn_peercred); 15801 mlp.tme_doi = label2doi(tsl); 15802 mlp.tme_label = *label2bslabel(tsl); 15803 needattr = B_TRUE; 15804 } 15805 15806 /* Create a message to report on IPv6 entries */ 15807 if (tcp->tcp_ipversion == IPV6_VERSION) { 15808 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 15809 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 15810 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 15811 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 15812 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 15813 /* Don't want just anybody seeing these... */ 15814 if (ispriv) { 15815 tce6.tcp6ConnEntryInfo.ce_snxt = 15816 tcp->tcp_snxt; 15817 tce6.tcp6ConnEntryInfo.ce_suna = 15818 tcp->tcp_suna; 15819 tce6.tcp6ConnEntryInfo.ce_rnxt = 15820 tcp->tcp_rnxt; 15821 tce6.tcp6ConnEntryInfo.ce_rack = 15822 tcp->tcp_rack; 15823 } else { 15824 /* 15825 * Netstat, unfortunately, uses this to 15826 * get send/receive queue sizes. How to fix? 15827 * Why not compute the difference only? 15828 */ 15829 tce6.tcp6ConnEntryInfo.ce_snxt = 15830 tcp->tcp_snxt - tcp->tcp_suna; 15831 tce6.tcp6ConnEntryInfo.ce_suna = 0; 15832 tce6.tcp6ConnEntryInfo.ce_rnxt = 15833 tcp->tcp_rnxt - tcp->tcp_rack; 15834 tce6.tcp6ConnEntryInfo.ce_rack = 0; 15835 } 15836 15837 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15838 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15839 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 15840 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 15841 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 15842 15843 tce6.tcp6ConnCreationProcess = 15844 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 15845 tcp->tcp_cpid; 15846 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 15847 15848 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 15849 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 15850 15851 mlp.tme_connidx = v6_conn_idx++; 15852 if (needattr) 15853 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 15854 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 15855 } 15856 /* 15857 * Create an IPv4 table entry for IPv4 entries and also 15858 * for IPv6 entries which are bound to in6addr_any 15859 * but don't have IPV6_V6ONLY set. 15860 * (i.e. anything an IPv4 peer could connect to) 15861 */ 15862 if (tcp->tcp_ipversion == IPV4_VERSION || 15863 (tcp->tcp_state <= TCPS_LISTEN && 15864 !tcp->tcp_connp->conn_ipv6_v6only && 15865 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 15866 if (tcp->tcp_ipversion == IPV6_VERSION) { 15867 tce.tcpConnRemAddress = INADDR_ANY; 15868 tce.tcpConnLocalAddress = INADDR_ANY; 15869 } else { 15870 tce.tcpConnRemAddress = 15871 tcp->tcp_remote; 15872 tce.tcpConnLocalAddress = 15873 tcp->tcp_ip_src; 15874 } 15875 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 15876 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 15877 /* Don't want just anybody seeing these... */ 15878 if (ispriv) { 15879 tce.tcpConnEntryInfo.ce_snxt = 15880 tcp->tcp_snxt; 15881 tce.tcpConnEntryInfo.ce_suna = 15882 tcp->tcp_suna; 15883 tce.tcpConnEntryInfo.ce_rnxt = 15884 tcp->tcp_rnxt; 15885 tce.tcpConnEntryInfo.ce_rack = 15886 tcp->tcp_rack; 15887 } else { 15888 /* 15889 * Netstat, unfortunately, uses this to 15890 * get send/receive queue sizes. How 15891 * to fix? 15892 * Why not compute the difference only? 15893 */ 15894 tce.tcpConnEntryInfo.ce_snxt = 15895 tcp->tcp_snxt - tcp->tcp_suna; 15896 tce.tcpConnEntryInfo.ce_suna = 0; 15897 tce.tcpConnEntryInfo.ce_rnxt = 15898 tcp->tcp_rnxt - tcp->tcp_rack; 15899 tce.tcpConnEntryInfo.ce_rack = 0; 15900 } 15901 15902 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15903 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15904 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 15905 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 15906 tce.tcpConnEntryInfo.ce_state = 15907 tcp->tcp_state; 15908 15909 tce.tcpConnCreationProcess = 15910 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 15911 tcp->tcp_cpid; 15912 tce.tcpConnCreationTime = tcp->tcp_open_time; 15913 15914 (void) snmp_append_data2(mp_conn_ctl->b_cont, 15915 &mp_conn_tail, (char *)&tce, sizeof (tce)); 15916 15917 mlp.tme_connidx = v4_conn_idx++; 15918 if (needattr) 15919 (void) snmp_append_data2( 15920 mp_attr_ctl->b_cont, 15921 &mp_attr_tail, (char *)&mlp, 15922 sizeof (mlp)); 15923 } 15924 } 15925 } 15926 15927 /* fixed length structure for IPv4 and IPv6 counters */ 15928 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 15929 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 15930 sizeof (mib2_tcp6ConnEntry_t)); 15931 /* synchronize 32- and 64-bit counters */ 15932 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 15933 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 15934 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 15935 optp->level = MIB2_TCP; 15936 optp->name = 0; 15937 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 15938 sizeof (tcps->tcps_mib)); 15939 optp->len = msgdsize(mpdata); 15940 qreply(q, mpctl); 15941 15942 /* table of connections... */ 15943 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 15944 sizeof (struct T_optmgmt_ack)]; 15945 optp->level = MIB2_TCP; 15946 optp->name = MIB2_TCP_CONN; 15947 optp->len = msgdsize(mp_conn_ctl->b_cont); 15948 qreply(q, mp_conn_ctl); 15949 15950 /* table of MLP attributes... */ 15951 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 15952 sizeof (struct T_optmgmt_ack)]; 15953 optp->level = MIB2_TCP; 15954 optp->name = EXPER_XPORT_MLP; 15955 optp->len = msgdsize(mp_attr_ctl->b_cont); 15956 if (optp->len == 0) 15957 freemsg(mp_attr_ctl); 15958 else 15959 qreply(q, mp_attr_ctl); 15960 15961 /* table of IPv6 connections... */ 15962 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 15963 sizeof (struct T_optmgmt_ack)]; 15964 optp->level = MIB2_TCP6; 15965 optp->name = MIB2_TCP6_CONN; 15966 optp->len = msgdsize(mp6_conn_ctl->b_cont); 15967 qreply(q, mp6_conn_ctl); 15968 15969 /* table of IPv6 MLP attributes... */ 15970 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 15971 sizeof (struct T_optmgmt_ack)]; 15972 optp->level = MIB2_TCP6; 15973 optp->name = EXPER_XPORT_MLP; 15974 optp->len = msgdsize(mp6_attr_ctl->b_cont); 15975 if (optp->len == 0) 15976 freemsg(mp6_attr_ctl); 15977 else 15978 qreply(q, mp6_attr_ctl); 15979 return (mp2ctl); 15980 } 15981 15982 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 15983 /* ARGSUSED */ 15984 int 15985 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 15986 { 15987 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 15988 15989 switch (level) { 15990 case MIB2_TCP: 15991 switch (name) { 15992 case 13: 15993 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 15994 return (0); 15995 /* TODO: delete entry defined by tce */ 15996 return (1); 15997 default: 15998 return (0); 15999 } 16000 default: 16001 return (1); 16002 } 16003 } 16004 16005 /* Translate TCP state to MIB2 TCP state. */ 16006 static int 16007 tcp_snmp_state(tcp_t *tcp) 16008 { 16009 if (tcp == NULL) 16010 return (0); 16011 16012 switch (tcp->tcp_state) { 16013 case TCPS_CLOSED: 16014 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16015 case TCPS_BOUND: 16016 return (MIB2_TCP_closed); 16017 case TCPS_LISTEN: 16018 return (MIB2_TCP_listen); 16019 case TCPS_SYN_SENT: 16020 return (MIB2_TCP_synSent); 16021 case TCPS_SYN_RCVD: 16022 return (MIB2_TCP_synReceived); 16023 case TCPS_ESTABLISHED: 16024 return (MIB2_TCP_established); 16025 case TCPS_CLOSE_WAIT: 16026 return (MIB2_TCP_closeWait); 16027 case TCPS_FIN_WAIT_1: 16028 return (MIB2_TCP_finWait1); 16029 case TCPS_CLOSING: 16030 return (MIB2_TCP_closing); 16031 case TCPS_LAST_ACK: 16032 return (MIB2_TCP_lastAck); 16033 case TCPS_FIN_WAIT_2: 16034 return (MIB2_TCP_finWait2); 16035 case TCPS_TIME_WAIT: 16036 return (MIB2_TCP_timeWait); 16037 default: 16038 return (0); 16039 } 16040 } 16041 16042 /* 16043 * tcp_timer is the timer service routine. It handles the retransmission, 16044 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16045 * from the state of the tcp instance what kind of action needs to be done 16046 * at the time it is called. 16047 */ 16048 static void 16049 tcp_timer(void *arg) 16050 { 16051 mblk_t *mp; 16052 clock_t first_threshold; 16053 clock_t second_threshold; 16054 clock_t ms; 16055 uint32_t mss; 16056 conn_t *connp = (conn_t *)arg; 16057 tcp_t *tcp = connp->conn_tcp; 16058 tcp_stack_t *tcps = tcp->tcp_tcps; 16059 16060 tcp->tcp_timer_tid = 0; 16061 16062 if (tcp->tcp_fused) 16063 return; 16064 16065 first_threshold = tcp->tcp_first_timer_threshold; 16066 second_threshold = tcp->tcp_second_timer_threshold; 16067 switch (tcp->tcp_state) { 16068 case TCPS_IDLE: 16069 case TCPS_BOUND: 16070 case TCPS_LISTEN: 16071 return; 16072 case TCPS_SYN_RCVD: { 16073 tcp_t *listener = tcp->tcp_listener; 16074 16075 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16076 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16077 /* it's our first timeout */ 16078 tcp->tcp_syn_rcvd_timeout = 1; 16079 mutex_enter(&listener->tcp_eager_lock); 16080 listener->tcp_syn_rcvd_timeout++; 16081 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16082 /* 16083 * Make this eager available for drop if we 16084 * need to drop one to accomodate a new 16085 * incoming SYN request. 16086 */ 16087 MAKE_DROPPABLE(listener, tcp); 16088 } 16089 if (!listener->tcp_syn_defense && 16090 (listener->tcp_syn_rcvd_timeout > 16091 (tcps->tcps_conn_req_max_q0 >> 2)) && 16092 (tcps->tcps_conn_req_max_q0 > 200)) { 16093 /* We may be under attack. Put on a defense. */ 16094 listener->tcp_syn_defense = B_TRUE; 16095 cmn_err(CE_WARN, "High TCP connect timeout " 16096 "rate! System (port %d) may be under a " 16097 "SYN flood attack!", 16098 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16099 16100 listener->tcp_ip_addr_cache = kmem_zalloc( 16101 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16102 KM_NOSLEEP); 16103 } 16104 mutex_exit(&listener->tcp_eager_lock); 16105 } else if (listener != NULL) { 16106 mutex_enter(&listener->tcp_eager_lock); 16107 tcp->tcp_syn_rcvd_timeout++; 16108 if (tcp->tcp_syn_rcvd_timeout > 1 && 16109 !tcp->tcp_closemp_used) { 16110 /* 16111 * This is our second timeout. Put the tcp in 16112 * the list of droppable eagers to allow it to 16113 * be dropped, if needed. We don't check 16114 * whether tcp_dontdrop is set or not to 16115 * protect ourselve from a SYN attack where a 16116 * remote host can spoof itself as one of the 16117 * good IP source and continue to hold 16118 * resources too long. 16119 */ 16120 MAKE_DROPPABLE(listener, tcp); 16121 } 16122 mutex_exit(&listener->tcp_eager_lock); 16123 } 16124 } 16125 /* FALLTHRU */ 16126 case TCPS_SYN_SENT: 16127 first_threshold = tcp->tcp_first_ctimer_threshold; 16128 second_threshold = tcp->tcp_second_ctimer_threshold; 16129 break; 16130 case TCPS_ESTABLISHED: 16131 case TCPS_FIN_WAIT_1: 16132 case TCPS_CLOSING: 16133 case TCPS_CLOSE_WAIT: 16134 case TCPS_LAST_ACK: 16135 /* If we have data to rexmit */ 16136 if (tcp->tcp_suna != tcp->tcp_snxt) { 16137 clock_t time_to_wait; 16138 16139 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 16140 if (!tcp->tcp_xmit_head) 16141 break; 16142 time_to_wait = lbolt - 16143 (clock_t)tcp->tcp_xmit_head->b_prev; 16144 time_to_wait = tcp->tcp_rto - 16145 TICK_TO_MSEC(time_to_wait); 16146 /* 16147 * If the timer fires too early, 1 clock tick earlier, 16148 * restart the timer. 16149 */ 16150 if (time_to_wait > msec_per_tick) { 16151 TCP_STAT(tcps, tcp_timer_fire_early); 16152 TCP_TIMER_RESTART(tcp, time_to_wait); 16153 return; 16154 } 16155 /* 16156 * When we probe zero windows, we force the swnd open. 16157 * If our peer acks with a closed window swnd will be 16158 * set to zero by tcp_rput(). As long as we are 16159 * receiving acks tcp_rput will 16160 * reset 'tcp_ms_we_have_waited' so as not to trip the 16161 * first and second interval actions. NOTE: the timer 16162 * interval is allowed to continue its exponential 16163 * backoff. 16164 */ 16165 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16166 if (tcp->tcp_debug) { 16167 (void) strlog(TCP_MOD_ID, 0, 1, 16168 SL_TRACE, "tcp_timer: zero win"); 16169 } 16170 } else { 16171 /* 16172 * After retransmission, we need to do 16173 * slow start. Set the ssthresh to one 16174 * half of current effective window and 16175 * cwnd to one MSS. Also reset 16176 * tcp_cwnd_cnt. 16177 * 16178 * Note that if tcp_ssthresh is reduced because 16179 * of ECN, do not reduce it again unless it is 16180 * already one window of data away (tcp_cwr 16181 * should then be cleared) or this is a 16182 * timeout for a retransmitted segment. 16183 */ 16184 uint32_t npkt; 16185 16186 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16187 npkt = ((tcp->tcp_timer_backoff ? 16188 tcp->tcp_cwnd_ssthresh : 16189 tcp->tcp_snxt - 16190 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16191 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16192 tcp->tcp_mss; 16193 } 16194 tcp->tcp_cwnd = tcp->tcp_mss; 16195 tcp->tcp_cwnd_cnt = 0; 16196 if (tcp->tcp_ecn_ok) { 16197 tcp->tcp_cwr = B_TRUE; 16198 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16199 tcp->tcp_ecn_cwr_sent = B_FALSE; 16200 } 16201 } 16202 break; 16203 } 16204 /* 16205 * We have something to send yet we cannot send. The 16206 * reason can be: 16207 * 16208 * 1. Zero send window: we need to do zero window probe. 16209 * 2. Zero cwnd: because of ECN, we need to "clock out 16210 * segments. 16211 * 3. SWS avoidance: receiver may have shrunk window, 16212 * reset our knowledge. 16213 * 16214 * Note that condition 2 can happen with either 1 or 16215 * 3. But 1 and 3 are exclusive. 16216 */ 16217 if (tcp->tcp_unsent != 0) { 16218 if (tcp->tcp_cwnd == 0) { 16219 /* 16220 * Set tcp_cwnd to 1 MSS so that a 16221 * new segment can be sent out. We 16222 * are "clocking out" new data when 16223 * the network is really congested. 16224 */ 16225 ASSERT(tcp->tcp_ecn_ok); 16226 tcp->tcp_cwnd = tcp->tcp_mss; 16227 } 16228 if (tcp->tcp_swnd == 0) { 16229 /* Extend window for zero window probe */ 16230 tcp->tcp_swnd++; 16231 tcp->tcp_zero_win_probe = B_TRUE; 16232 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 16233 } else { 16234 /* 16235 * Handle timeout from sender SWS avoidance. 16236 * Reset our knowledge of the max send window 16237 * since the receiver might have reduced its 16238 * receive buffer. Avoid setting tcp_max_swnd 16239 * to one since that will essentially disable 16240 * the SWS checks. 16241 * 16242 * Note that since we don't have a SWS 16243 * state variable, if the timeout is set 16244 * for ECN but not for SWS, this 16245 * code will also be executed. This is 16246 * fine as tcp_max_swnd is updated 16247 * constantly and it will not affect 16248 * anything. 16249 */ 16250 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16251 } 16252 tcp_wput_data(tcp, NULL, B_FALSE); 16253 return; 16254 } 16255 /* Is there a FIN that needs to be to re retransmitted? */ 16256 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16257 !tcp->tcp_fin_acked) 16258 break; 16259 /* Nothing to do, return without restarting timer. */ 16260 TCP_STAT(tcps, tcp_timer_fire_miss); 16261 return; 16262 case TCPS_FIN_WAIT_2: 16263 /* 16264 * User closed the TCP endpoint and peer ACK'ed our FIN. 16265 * We waited some time for for peer's FIN, but it hasn't 16266 * arrived. We flush the connection now to avoid 16267 * case where the peer has rebooted. 16268 */ 16269 if (TCP_IS_DETACHED(tcp)) { 16270 (void) tcp_clean_death(tcp, 0, 23); 16271 } else { 16272 TCP_TIMER_RESTART(tcp, 16273 tcps->tcps_fin_wait_2_flush_interval); 16274 } 16275 return; 16276 case TCPS_TIME_WAIT: 16277 (void) tcp_clean_death(tcp, 0, 24); 16278 return; 16279 default: 16280 if (tcp->tcp_debug) { 16281 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16282 "tcp_timer: strange state (%d) %s", 16283 tcp->tcp_state, tcp_display(tcp, NULL, 16284 DISP_PORT_ONLY)); 16285 } 16286 return; 16287 } 16288 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16289 /* 16290 * For zero window probe, we need to send indefinitely, 16291 * unless we have not heard from the other side for some 16292 * time... 16293 */ 16294 if ((tcp->tcp_zero_win_probe == 0) || 16295 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16296 second_threshold)) { 16297 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 16298 /* 16299 * If TCP is in SYN_RCVD state, send back a 16300 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16301 * should be zero in TCPS_SYN_RCVD state. 16302 */ 16303 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16304 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16305 "in SYN_RCVD", 16306 tcp, tcp->tcp_snxt, 16307 tcp->tcp_rnxt, TH_RST | TH_ACK); 16308 } 16309 (void) tcp_clean_death(tcp, 16310 tcp->tcp_client_errno ? 16311 tcp->tcp_client_errno : ETIMEDOUT, 25); 16312 return; 16313 } else { 16314 /* 16315 * Set tcp_ms_we_have_waited to second_threshold 16316 * so that in next timeout, we will do the above 16317 * check (lbolt - tcp_last_recv_time). This is 16318 * also to avoid overflow. 16319 * 16320 * We don't need to decrement tcp_timer_backoff 16321 * to avoid overflow because it will be decremented 16322 * later if new timeout value is greater than 16323 * tcp_rexmit_interval_max. In the case when 16324 * tcp_rexmit_interval_max is greater than 16325 * second_threshold, it means that we will wait 16326 * longer than second_threshold to send the next 16327 * window probe. 16328 */ 16329 tcp->tcp_ms_we_have_waited = second_threshold; 16330 } 16331 } else if (ms > first_threshold) { 16332 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16333 tcp->tcp_xmit_head != NULL) { 16334 tcp->tcp_xmit_head = 16335 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16336 } 16337 /* 16338 * We have been retransmitting for too long... The RTT 16339 * we calculated is probably incorrect. Reinitialize it. 16340 * Need to compensate for 0 tcp_rtt_sa. Reset 16341 * tcp_rtt_update so that we won't accidentally cache a 16342 * bad value. But only do this if this is not a zero 16343 * window probe. 16344 */ 16345 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 16346 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 16347 (tcp->tcp_rtt_sa >> 5); 16348 tcp->tcp_rtt_sa = 0; 16349 tcp_ip_notify(tcp); 16350 tcp->tcp_rtt_update = 0; 16351 } 16352 } 16353 tcp->tcp_timer_backoff++; 16354 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 16355 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 16356 tcps->tcps_rexmit_interval_min) { 16357 /* 16358 * This means the original RTO is tcp_rexmit_interval_min. 16359 * So we will use tcp_rexmit_interval_min as the RTO value 16360 * and do the backoff. 16361 */ 16362 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 16363 } else { 16364 ms <<= tcp->tcp_timer_backoff; 16365 } 16366 if (ms > tcps->tcps_rexmit_interval_max) { 16367 ms = tcps->tcps_rexmit_interval_max; 16368 /* 16369 * ms is at max, decrement tcp_timer_backoff to avoid 16370 * overflow. 16371 */ 16372 tcp->tcp_timer_backoff--; 16373 } 16374 tcp->tcp_ms_we_have_waited += ms; 16375 if (tcp->tcp_zero_win_probe == 0) { 16376 tcp->tcp_rto = ms; 16377 } 16378 TCP_TIMER_RESTART(tcp, ms); 16379 /* 16380 * This is after a timeout and tcp_rto is backed off. Set 16381 * tcp_set_timer to 1 so that next time RTO is updated, we will 16382 * restart the timer with a correct value. 16383 */ 16384 tcp->tcp_set_timer = 1; 16385 mss = tcp->tcp_snxt - tcp->tcp_suna; 16386 if (mss > tcp->tcp_mss) 16387 mss = tcp->tcp_mss; 16388 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 16389 mss = tcp->tcp_swnd; 16390 16391 if ((mp = tcp->tcp_xmit_head) != NULL) 16392 mp->b_prev = (mblk_t *)lbolt; 16393 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 16394 B_TRUE); 16395 16396 /* 16397 * When slow start after retransmission begins, start with 16398 * this seq no. tcp_rexmit_max marks the end of special slow 16399 * start phase. tcp_snd_burst controls how many segments 16400 * can be sent because of an ack. 16401 */ 16402 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 16403 tcp->tcp_snd_burst = TCP_CWND_SS; 16404 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16405 (tcp->tcp_unsent == 0)) { 16406 tcp->tcp_rexmit_max = tcp->tcp_fss; 16407 } else { 16408 tcp->tcp_rexmit_max = tcp->tcp_snxt; 16409 } 16410 tcp->tcp_rexmit = B_TRUE; 16411 tcp->tcp_dupack_cnt = 0; 16412 16413 /* 16414 * Remove all rexmit SACK blk to start from fresh. 16415 */ 16416 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 16417 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 16418 tcp->tcp_num_notsack_blk = 0; 16419 tcp->tcp_cnt_notsack_list = 0; 16420 } 16421 if (mp == NULL) { 16422 return; 16423 } 16424 /* 16425 * Attach credentials to retransmitted initial SYNs. 16426 * In theory we should use the credentials from the connect() 16427 * call to ensure that getpeerucred() on the peer will be correct. 16428 * But we assume that SYN's are not dropped for loopback connections. 16429 */ 16430 if (tcp->tcp_state == TCPS_SYN_SENT) { 16431 mblk_setcred(mp, tcp->tcp_cred, tcp->tcp_cpid); 16432 } 16433 16434 tcp->tcp_csuna = tcp->tcp_snxt; 16435 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 16436 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 16437 tcp_send_data(tcp, tcp->tcp_wq, mp); 16438 16439 } 16440 16441 static int 16442 tcp_do_unbind(conn_t *connp) 16443 { 16444 tcp_t *tcp = connp->conn_tcp; 16445 int error = 0; 16446 16447 switch (tcp->tcp_state) { 16448 case TCPS_BOUND: 16449 case TCPS_LISTEN: 16450 break; 16451 default: 16452 return (-TOUTSTATE); 16453 } 16454 16455 /* 16456 * Need to clean up all the eagers since after the unbind, segments 16457 * will no longer be delivered to this listener stream. 16458 */ 16459 mutex_enter(&tcp->tcp_eager_lock); 16460 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 16461 tcp_eager_cleanup(tcp, 0); 16462 } 16463 mutex_exit(&tcp->tcp_eager_lock); 16464 16465 if (tcp->tcp_ipversion == IPV4_VERSION) { 16466 tcp->tcp_ipha->ipha_src = 0; 16467 } else { 16468 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16469 } 16470 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16471 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 16472 tcp_bind_hash_remove(tcp); 16473 tcp->tcp_state = TCPS_IDLE; 16474 tcp->tcp_mdt = B_FALSE; 16475 16476 connp = tcp->tcp_connp; 16477 connp->conn_mdt_ok = B_FALSE; 16478 ipcl_hash_remove(connp); 16479 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 16480 16481 return (error); 16482 } 16483 16484 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 16485 static void 16486 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 16487 { 16488 int error = tcp_do_unbind(tcp->tcp_connp); 16489 16490 if (error > 0) { 16491 tcp_err_ack(tcp, mp, TSYSERR, error); 16492 } else if (error < 0) { 16493 tcp_err_ack(tcp, mp, -error, 0); 16494 } else { 16495 /* Send M_FLUSH according to TPI */ 16496 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 16497 16498 mp = mi_tpi_ok_ack_alloc(mp); 16499 putnext(tcp->tcp_rq, mp); 16500 } 16501 } 16502 16503 /* 16504 * Don't let port fall into the privileged range. 16505 * Since the extra privileged ports can be arbitrary we also 16506 * ensure that we exclude those from consideration. 16507 * tcp_g_epriv_ports is not sorted thus we loop over it until 16508 * there are no changes. 16509 * 16510 * Note: No locks are held when inspecting tcp_g_*epriv_ports 16511 * but instead the code relies on: 16512 * - the fact that the address of the array and its size never changes 16513 * - the atomic assignment of the elements of the array 16514 * 16515 * Returns 0 if there are no more ports available. 16516 * 16517 * TS note: skip multilevel ports. 16518 */ 16519 static in_port_t 16520 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 16521 { 16522 int i; 16523 boolean_t restart = B_FALSE; 16524 tcp_stack_t *tcps = tcp->tcp_tcps; 16525 16526 if (random && tcp_random_anon_port != 0) { 16527 (void) random_get_pseudo_bytes((uint8_t *)&port, 16528 sizeof (in_port_t)); 16529 /* 16530 * Unless changed by a sys admin, the smallest anon port 16531 * is 32768 and the largest anon port is 65535. It is 16532 * very likely (50%) for the random port to be smaller 16533 * than the smallest anon port. When that happens, 16534 * add port % (anon port range) to the smallest anon 16535 * port to get the random port. It should fall into the 16536 * valid anon port range. 16537 */ 16538 if (port < tcps->tcps_smallest_anon_port) { 16539 port = tcps->tcps_smallest_anon_port + 16540 port % (tcps->tcps_largest_anon_port - 16541 tcps->tcps_smallest_anon_port); 16542 } 16543 } 16544 16545 retry: 16546 if (port < tcps->tcps_smallest_anon_port) 16547 port = (in_port_t)tcps->tcps_smallest_anon_port; 16548 16549 if (port > tcps->tcps_largest_anon_port) { 16550 if (restart) 16551 return (0); 16552 restart = B_TRUE; 16553 port = (in_port_t)tcps->tcps_smallest_anon_port; 16554 } 16555 16556 if (port < tcps->tcps_smallest_nonpriv_port) 16557 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 16558 16559 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 16560 if (port == tcps->tcps_g_epriv_ports[i]) { 16561 port++; 16562 /* 16563 * Make sure whether the port is in the 16564 * valid range. 16565 */ 16566 goto retry; 16567 } 16568 } 16569 if (is_system_labeled() && 16570 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 16571 IPPROTO_TCP, B_TRUE)) != 0) { 16572 port = i; 16573 goto retry; 16574 } 16575 return (port); 16576 } 16577 16578 /* 16579 * Return the next anonymous port in the privileged port range for 16580 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 16581 * downwards. This is the same behavior as documented in the userland 16582 * library call rresvport(3N). 16583 * 16584 * TS note: skip multilevel ports. 16585 */ 16586 static in_port_t 16587 tcp_get_next_priv_port(const tcp_t *tcp) 16588 { 16589 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 16590 in_port_t nextport; 16591 boolean_t restart = B_FALSE; 16592 tcp_stack_t *tcps = tcp->tcp_tcps; 16593 retry: 16594 if (next_priv_port < tcps->tcps_min_anonpriv_port || 16595 next_priv_port >= IPPORT_RESERVED) { 16596 next_priv_port = IPPORT_RESERVED - 1; 16597 if (restart) 16598 return (0); 16599 restart = B_TRUE; 16600 } 16601 if (is_system_labeled() && 16602 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 16603 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 16604 next_priv_port = nextport; 16605 goto retry; 16606 } 16607 return (next_priv_port--); 16608 } 16609 16610 /* The write side r/w procedure. */ 16611 16612 #if CCS_STATS 16613 struct { 16614 struct { 16615 int64_t count, bytes; 16616 } tot, hit; 16617 } wrw_stats; 16618 #endif 16619 16620 /* 16621 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 16622 * messages. 16623 */ 16624 /* ARGSUSED */ 16625 static void 16626 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 16627 { 16628 conn_t *connp = (conn_t *)arg; 16629 tcp_t *tcp = connp->conn_tcp; 16630 queue_t *q = tcp->tcp_wq; 16631 16632 ASSERT(DB_TYPE(mp) != M_IOCTL); 16633 /* 16634 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 16635 * Once the close starts, streamhead and sockfs will not let any data 16636 * packets come down (close ensures that there are no threads using the 16637 * queue and no new threads will come down) but since qprocsoff() 16638 * hasn't happened yet, a M_FLUSH or some non data message might 16639 * get reflected back (in response to our own FLUSHRW) and get 16640 * processed after tcp_close() is done. The conn would still be valid 16641 * because a ref would have added but we need to check the state 16642 * before actually processing the packet. 16643 */ 16644 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 16645 freemsg(mp); 16646 return; 16647 } 16648 16649 switch (DB_TYPE(mp)) { 16650 case M_IOCDATA: 16651 tcp_wput_iocdata(tcp, mp); 16652 break; 16653 case M_FLUSH: 16654 tcp_wput_flush(tcp, mp); 16655 break; 16656 default: 16657 CALL_IP_WPUT(connp, q, mp); 16658 break; 16659 } 16660 } 16661 16662 /* 16663 * The TCP fast path write put procedure. 16664 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 16665 */ 16666 /* ARGSUSED */ 16667 void 16668 tcp_output(void *arg, mblk_t *mp, void *arg2) 16669 { 16670 int len; 16671 int hdrlen; 16672 int plen; 16673 mblk_t *mp1; 16674 uchar_t *rptr; 16675 uint32_t snxt; 16676 tcph_t *tcph; 16677 struct datab *db; 16678 uint32_t suna; 16679 uint32_t mss; 16680 ipaddr_t *dst; 16681 ipaddr_t *src; 16682 uint32_t sum; 16683 int usable; 16684 conn_t *connp = (conn_t *)arg; 16685 tcp_t *tcp = connp->conn_tcp; 16686 uint32_t msize; 16687 tcp_stack_t *tcps = tcp->tcp_tcps; 16688 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 16689 16690 /* 16691 * Try and ASSERT the minimum possible references on the 16692 * conn early enough. Since we are executing on write side, 16693 * the connection is obviously not detached and that means 16694 * there is a ref each for TCP and IP. Since we are behind 16695 * the squeue, the minimum references needed are 3. If the 16696 * conn is in classifier hash list, there should be an 16697 * extra ref for that (we check both the possibilities). 16698 */ 16699 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 16700 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 16701 16702 ASSERT(DB_TYPE(mp) == M_DATA); 16703 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 16704 16705 mutex_enter(&tcp->tcp_non_sq_lock); 16706 tcp->tcp_squeue_bytes -= msize; 16707 mutex_exit(&tcp->tcp_non_sq_lock); 16708 16709 /* Check to see if this connection wants to be re-fused. */ 16710 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 16711 if (tcp->tcp_ipversion == IPV4_VERSION) { 16712 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 16713 &tcp->tcp_saved_tcph); 16714 } else { 16715 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 16716 &tcp->tcp_saved_tcph); 16717 } 16718 } 16719 /* Bypass tcp protocol for fused tcp loopback */ 16720 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 16721 return; 16722 16723 mss = tcp->tcp_mss; 16724 if (tcp->tcp_xmit_zc_clean) 16725 mp = tcp_zcopy_backoff(tcp, mp, 0); 16726 16727 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 16728 len = (int)(mp->b_wptr - mp->b_rptr); 16729 16730 /* 16731 * Criteria for fast path: 16732 * 16733 * 1. no unsent data 16734 * 2. single mblk in request 16735 * 3. connection established 16736 * 4. data in mblk 16737 * 5. len <= mss 16738 * 6. no tcp_valid bits 16739 */ 16740 if ((tcp->tcp_unsent != 0) || 16741 (tcp->tcp_cork) || 16742 (mp->b_cont != NULL) || 16743 (tcp->tcp_state != TCPS_ESTABLISHED) || 16744 (len == 0) || 16745 (len > mss) || 16746 (tcp->tcp_valid_bits != 0)) { 16747 tcp_wput_data(tcp, mp, B_FALSE); 16748 return; 16749 } 16750 16751 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 16752 ASSERT(tcp->tcp_fin_sent == 0); 16753 16754 /* queue new packet onto retransmission queue */ 16755 if (tcp->tcp_xmit_head == NULL) { 16756 tcp->tcp_xmit_head = mp; 16757 } else { 16758 tcp->tcp_xmit_last->b_cont = mp; 16759 } 16760 tcp->tcp_xmit_last = mp; 16761 tcp->tcp_xmit_tail = mp; 16762 16763 /* find out how much we can send */ 16764 /* BEGIN CSTYLED */ 16765 /* 16766 * un-acked usable 16767 * |--------------|-----------------| 16768 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 16769 */ 16770 /* END CSTYLED */ 16771 16772 /* start sending from tcp_snxt */ 16773 snxt = tcp->tcp_snxt; 16774 16775 /* 16776 * Check to see if this connection has been idled for some 16777 * time and no ACK is expected. If it is, we need to slow 16778 * start again to get back the connection's "self-clock" as 16779 * described in VJ's paper. 16780 * 16781 * Refer to the comment in tcp_mss_set() for the calculation 16782 * of tcp_cwnd after idle. 16783 */ 16784 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 16785 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 16786 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 16787 } 16788 16789 usable = tcp->tcp_swnd; /* tcp window size */ 16790 if (usable > tcp->tcp_cwnd) 16791 usable = tcp->tcp_cwnd; /* congestion window smaller */ 16792 usable -= snxt; /* subtract stuff already sent */ 16793 suna = tcp->tcp_suna; 16794 usable += suna; 16795 /* usable can be < 0 if the congestion window is smaller */ 16796 if (len > usable) { 16797 /* Can't send complete M_DATA in one shot */ 16798 goto slow; 16799 } 16800 16801 mutex_enter(&tcp->tcp_non_sq_lock); 16802 if (tcp->tcp_flow_stopped && 16803 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 16804 tcp_clrqfull(tcp); 16805 } 16806 mutex_exit(&tcp->tcp_non_sq_lock); 16807 16808 /* 16809 * determine if anything to send (Nagle). 16810 * 16811 * 1. len < tcp_mss (i.e. small) 16812 * 2. unacknowledged data present 16813 * 3. len < nagle limit 16814 * 4. last packet sent < nagle limit (previous packet sent) 16815 */ 16816 if ((len < mss) && (snxt != suna) && 16817 (len < (int)tcp->tcp_naglim) && 16818 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 16819 /* 16820 * This was the first unsent packet and normally 16821 * mss < xmit_hiwater so there is no need to worry 16822 * about flow control. The next packet will go 16823 * through the flow control check in tcp_wput_data(). 16824 */ 16825 /* leftover work from above */ 16826 tcp->tcp_unsent = len; 16827 tcp->tcp_xmit_tail_unsent = len; 16828 16829 return; 16830 } 16831 16832 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 16833 16834 if (snxt == suna) { 16835 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 16836 } 16837 16838 /* we have always sent something */ 16839 tcp->tcp_rack_cnt = 0; 16840 16841 tcp->tcp_snxt = snxt + len; 16842 tcp->tcp_rack = tcp->tcp_rnxt; 16843 16844 if ((mp1 = dupb(mp)) == 0) 16845 goto no_memory; 16846 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 16847 mp->b_next = (mblk_t *)(uintptr_t)snxt; 16848 16849 /* adjust tcp header information */ 16850 tcph = tcp->tcp_tcph; 16851 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 16852 16853 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 16854 sum = (sum >> 16) + (sum & 0xFFFF); 16855 U16_TO_ABE16(sum, tcph->th_sum); 16856 16857 U32_TO_ABE32(snxt, tcph->th_seq); 16858 16859 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 16860 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 16861 BUMP_LOCAL(tcp->tcp_obsegs); 16862 16863 /* Update the latest receive window size in TCP header. */ 16864 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 16865 tcph->th_win); 16866 16867 tcp->tcp_last_sent_len = (ushort_t)len; 16868 16869 plen = len + tcp->tcp_hdr_len; 16870 16871 if (tcp->tcp_ipversion == IPV4_VERSION) { 16872 tcp->tcp_ipha->ipha_length = htons(plen); 16873 } else { 16874 tcp->tcp_ip6h->ip6_plen = htons(plen - 16875 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 16876 } 16877 16878 /* see if we need to allocate a mblk for the headers */ 16879 hdrlen = tcp->tcp_hdr_len; 16880 rptr = mp1->b_rptr - hdrlen; 16881 db = mp1->b_datap; 16882 if ((db->db_ref != 2) || rptr < db->db_base || 16883 (!OK_32PTR(rptr))) { 16884 /* NOTE: we assume allocb returns an OK_32PTR */ 16885 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 16886 tcps->tcps_wroff_xtra, BPRI_MED); 16887 if (!mp) { 16888 freemsg(mp1); 16889 goto no_memory; 16890 } 16891 mp->b_cont = mp1; 16892 mp1 = mp; 16893 /* Leave room for Link Level header */ 16894 /* hdrlen = tcp->tcp_hdr_len; */ 16895 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 16896 mp1->b_wptr = &rptr[hdrlen]; 16897 } 16898 mp1->b_rptr = rptr; 16899 16900 /* Fill in the timestamp option. */ 16901 if (tcp->tcp_snd_ts_ok) { 16902 U32_TO_BE32((uint32_t)lbolt, 16903 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 16904 U32_TO_BE32(tcp->tcp_ts_recent, 16905 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 16906 } else { 16907 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 16908 } 16909 16910 /* copy header into outgoing packet */ 16911 dst = (ipaddr_t *)rptr; 16912 src = (ipaddr_t *)tcp->tcp_iphc; 16913 dst[0] = src[0]; 16914 dst[1] = src[1]; 16915 dst[2] = src[2]; 16916 dst[3] = src[3]; 16917 dst[4] = src[4]; 16918 dst[5] = src[5]; 16919 dst[6] = src[6]; 16920 dst[7] = src[7]; 16921 dst[8] = src[8]; 16922 dst[9] = src[9]; 16923 if (hdrlen -= 40) { 16924 hdrlen >>= 2; 16925 dst += 10; 16926 src += 10; 16927 do { 16928 *dst++ = *src++; 16929 } while (--hdrlen); 16930 } 16931 16932 /* 16933 * Set the ECN info in the TCP header. Note that this 16934 * is not the template header. 16935 */ 16936 if (tcp->tcp_ecn_ok) { 16937 SET_ECT(tcp, rptr); 16938 16939 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 16940 if (tcp->tcp_ecn_echo_on) 16941 tcph->th_flags[0] |= TH_ECE; 16942 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 16943 tcph->th_flags[0] |= TH_CWR; 16944 tcp->tcp_ecn_cwr_sent = B_TRUE; 16945 } 16946 } 16947 16948 if (tcp->tcp_ip_forward_progress) { 16949 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 16950 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 16951 tcp->tcp_ip_forward_progress = B_FALSE; 16952 } 16953 tcp_send_data(tcp, tcp->tcp_wq, mp1); 16954 return; 16955 16956 /* 16957 * If we ran out of memory, we pretend to have sent the packet 16958 * and that it was lost on the wire. 16959 */ 16960 no_memory: 16961 return; 16962 16963 slow: 16964 /* leftover work from above */ 16965 tcp->tcp_unsent = len; 16966 tcp->tcp_xmit_tail_unsent = len; 16967 tcp_wput_data(tcp, NULL, B_FALSE); 16968 } 16969 16970 /* ARGSUSED */ 16971 void 16972 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 16973 { 16974 conn_t *connp = (conn_t *)arg; 16975 tcp_t *tcp = connp->conn_tcp; 16976 queue_t *q = tcp->tcp_rq; 16977 struct tcp_options *tcpopt; 16978 tcp_stack_t *tcps = tcp->tcp_tcps; 16979 16980 /* socket options */ 16981 uint_t sopp_flags; 16982 ssize_t sopp_rxhiwat; 16983 ssize_t sopp_maxblk; 16984 ushort_t sopp_wroff; 16985 ushort_t sopp_tail; 16986 ushort_t sopp_copyopt; 16987 16988 tcpopt = (struct tcp_options *)mp->b_rptr; 16989 16990 /* 16991 * Drop the eager's ref on the listener, that was placed when 16992 * this eager began life in tcp_conn_request. 16993 */ 16994 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 16995 if (IPCL_IS_NONSTR(connp)) { 16996 /* Safe to free conn_ind message */ 16997 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 16998 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 16999 } 17000 17001 tcp->tcp_detached = B_FALSE; 17002 17003 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17004 /* 17005 * Someone blewoff the eager before we could finish 17006 * the accept. 17007 * 17008 * The only reason eager exists it because we put in 17009 * a ref on it when conn ind went up. We need to send 17010 * a disconnect indication up while the last reference 17011 * on the eager will be dropped by the squeue when we 17012 * return. 17013 */ 17014 ASSERT(tcp->tcp_listener == NULL); 17015 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17016 if (IPCL_IS_NONSTR(connp)) { 17017 ASSERT(tcp->tcp_issocket); 17018 (*connp->conn_upcalls->su_disconnected)( 17019 connp->conn_upper_handle, tcp->tcp_connid, 17020 ECONNREFUSED); 17021 freemsg(mp); 17022 } else { 17023 struct T_discon_ind *tdi; 17024 17025 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17026 /* 17027 * Let us reuse the incoming mblk to avoid 17028 * memory allocation failure problems. We know 17029 * that the size of the incoming mblk i.e. 17030 * stroptions is greater than sizeof 17031 * T_discon_ind. So the reallocb below can't 17032 * fail. 17033 */ 17034 freemsg(mp->b_cont); 17035 mp->b_cont = NULL; 17036 ASSERT(DB_REF(mp) == 1); 17037 mp = reallocb(mp, sizeof (struct T_discon_ind), 17038 B_FALSE); 17039 ASSERT(mp != NULL); 17040 DB_TYPE(mp) = M_PROTO; 17041 ((union T_primitives *)mp->b_rptr)->type = 17042 T_DISCON_IND; 17043 tdi = (struct T_discon_ind *)mp->b_rptr; 17044 if (tcp->tcp_issocket) { 17045 tdi->DISCON_reason = ECONNREFUSED; 17046 tdi->SEQ_number = 0; 17047 } else { 17048 tdi->DISCON_reason = ENOPROTOOPT; 17049 tdi->SEQ_number = 17050 tcp->tcp_conn_req_seqnum; 17051 } 17052 mp->b_wptr = mp->b_rptr + 17053 sizeof (struct T_discon_ind); 17054 putnext(q, mp); 17055 return; 17056 } 17057 } 17058 if (tcp->tcp_hard_binding) { 17059 tcp->tcp_hard_binding = B_FALSE; 17060 tcp->tcp_hard_bound = B_TRUE; 17061 } 17062 return; 17063 } 17064 17065 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17066 int boundif = tcpopt->to_boundif; 17067 uint_t len = sizeof (int); 17068 17069 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17070 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17071 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17072 } 17073 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17074 uint_t on = 1; 17075 uint_t len = sizeof (uint_t); 17076 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17077 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17078 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17079 } 17080 17081 /* 17082 * For a loopback connection with tcp_direct_sockfs on, note that 17083 * we don't have to protect tcp_rcv_list yet because synchronous 17084 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17085 * possibly race with us. 17086 */ 17087 17088 /* 17089 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17090 * properly. This is the first time we know of the acceptor' 17091 * queue. So we do it here. 17092 * 17093 * XXX 17094 */ 17095 if (tcp->tcp_rcv_list == NULL) { 17096 /* 17097 * Recv queue is empty, tcp_rwnd should not have changed. 17098 * That means it should be equal to the listener's tcp_rwnd. 17099 */ 17100 if (!IPCL_IS_NONSTR(connp)) 17101 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17102 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 17103 } else { 17104 #ifdef DEBUG 17105 mblk_t *tmp; 17106 mblk_t *mp1; 17107 uint_t cnt = 0; 17108 17109 mp1 = tcp->tcp_rcv_list; 17110 while ((tmp = mp1) != NULL) { 17111 mp1 = tmp->b_next; 17112 cnt += msgdsize(tmp); 17113 } 17114 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17115 #endif 17116 /* There is some data, add them back to get the max. */ 17117 if (!IPCL_IS_NONSTR(connp)) 17118 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17119 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17120 } 17121 /* 17122 * This is the first time we run on the correct 17123 * queue after tcp_accept. So fix all the q parameters 17124 * here. 17125 */ 17126 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 17127 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17128 17129 /* 17130 * Record the stream head's high water mark for this endpoint; 17131 * this is used for flow-control purposes. 17132 */ 17133 sopp_rxhiwat = tcp->tcp_fused ? 17134 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 17135 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 17136 17137 /* 17138 * Determine what write offset value to use depending on SACK and 17139 * whether the endpoint is fused or not. 17140 */ 17141 if (tcp->tcp_fused) { 17142 ASSERT(tcp->tcp_loopback); 17143 ASSERT(tcp->tcp_loopback_peer != NULL); 17144 /* 17145 * For fused tcp loopback, set the stream head's write 17146 * offset value to zero since we won't be needing any room 17147 * for TCP/IP headers. This would also improve performance 17148 * since it would reduce the amount of work done by kmem. 17149 * Non-fused tcp loopback case is handled separately below. 17150 */ 17151 sopp_wroff = 0; 17152 /* 17153 * Update the peer's transmit parameters according to 17154 * our recently calculated high water mark value. 17155 */ 17156 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17157 } else if (tcp->tcp_snd_sack_ok) { 17158 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17159 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17160 } else { 17161 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17162 tcps->tcps_wroff_xtra); 17163 } 17164 17165 /* 17166 * If this is endpoint is handling SSL, then reserve extra 17167 * offset and space at the end. 17168 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17169 * overriding the previous setting. The extra cost of signing and 17170 * encrypting multiple MSS-size records (12 of them with Ethernet), 17171 * instead of a single contiguous one by the stream head 17172 * largely outweighs the statistical reduction of ACKs, when 17173 * applicable. The peer will also save on decryption and verification 17174 * costs. 17175 */ 17176 if (tcp->tcp_kssl_ctx != NULL) { 17177 sopp_wroff += SSL3_WROFFSET; 17178 17179 sopp_flags |= SOCKOPT_TAIL; 17180 sopp_tail = SSL3_MAX_TAIL_LEN; 17181 17182 sopp_flags |= SOCKOPT_ZCOPY; 17183 sopp_copyopt = ZCVMUNSAFE; 17184 17185 sopp_maxblk = SSL3_MAX_RECORD_LEN; 17186 } 17187 17188 /* Send the options up */ 17189 if (IPCL_IS_NONSTR(connp)) { 17190 struct sock_proto_props sopp; 17191 17192 sopp.sopp_flags = sopp_flags; 17193 sopp.sopp_wroff = sopp_wroff; 17194 sopp.sopp_maxblk = sopp_maxblk; 17195 sopp.sopp_rxhiwat = sopp_rxhiwat; 17196 if (sopp_flags & SOCKOPT_TAIL) { 17197 ASSERT(tcp->tcp_kssl_ctx != NULL); 17198 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 17199 sopp.sopp_tail = sopp_tail; 17200 sopp.sopp_zcopyflag = sopp_copyopt; 17201 } 17202 (*connp->conn_upcalls->su_set_proto_props) 17203 (connp->conn_upper_handle, &sopp); 17204 } else { 17205 struct stroptions *stropt; 17206 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17207 if (stropt_mp == NULL) { 17208 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 17209 return; 17210 } 17211 DB_TYPE(stropt_mp) = M_SETOPTS; 17212 stropt = (struct stroptions *)stropt_mp->b_rptr; 17213 stropt_mp->b_wptr += sizeof (struct stroptions); 17214 stropt->so_flags = SO_HIWAT | SO_WROFF | SO_MAXBLK; 17215 stropt->so_hiwat = sopp_rxhiwat; 17216 stropt->so_wroff = sopp_wroff; 17217 stropt->so_maxblk = sopp_maxblk; 17218 17219 if (sopp_flags & SOCKOPT_TAIL) { 17220 ASSERT(tcp->tcp_kssl_ctx != NULL); 17221 17222 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 17223 stropt->so_tail = sopp_tail; 17224 stropt->so_copyopt = sopp_copyopt; 17225 } 17226 17227 /* Send the options up */ 17228 putnext(q, stropt_mp); 17229 } 17230 17231 freemsg(mp); 17232 /* 17233 * Pass up any data and/or a fin that has been received. 17234 * 17235 * Adjust receive window in case it had decreased 17236 * (because there is data <=> tcp_rcv_list != NULL) 17237 * while the connection was detached. Note that 17238 * in case the eager was flow-controlled, w/o this 17239 * code, the rwnd may never open up again! 17240 */ 17241 if (tcp->tcp_rcv_list != NULL) { 17242 if (IPCL_IS_NONSTR(connp)) { 17243 mblk_t *mp; 17244 int space_left; 17245 int error; 17246 boolean_t push = B_TRUE; 17247 17248 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 17249 (connp->conn_upper_handle, NULL, 0, 0, &error, 17250 &push) >= 0) { 17251 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 17252 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17253 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17254 tcp_xmit_ctl(NULL, 17255 tcp, (tcp->tcp_swnd == 0) ? 17256 tcp->tcp_suna : tcp->tcp_snxt, 17257 tcp->tcp_rnxt, TH_ACK); 17258 } 17259 } 17260 while ((mp = tcp->tcp_rcv_list) != NULL) { 17261 push = B_TRUE; 17262 tcp->tcp_rcv_list = mp->b_next; 17263 mp->b_next = NULL; 17264 space_left = (*connp->conn_upcalls->su_recv) 17265 (connp->conn_upper_handle, mp, msgdsize(mp), 17266 0, &error, &push); 17267 if (space_left < 0) { 17268 /* 17269 * We should never be in middle of a 17270 * fallback, the squeue guarantees that. 17271 */ 17272 ASSERT(error != EOPNOTSUPP); 17273 } 17274 } 17275 tcp->tcp_rcv_last_head = NULL; 17276 tcp->tcp_rcv_last_tail = NULL; 17277 tcp->tcp_rcv_cnt = 0; 17278 } else { 17279 /* We drain directly in case of fused tcp loopback */ 17280 17281 if (!tcp->tcp_fused && canputnext(q)) { 17282 tcp->tcp_rwnd = q->q_hiwat; 17283 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17284 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17285 tcp_xmit_ctl(NULL, 17286 tcp, (tcp->tcp_swnd == 0) ? 17287 tcp->tcp_suna : tcp->tcp_snxt, 17288 tcp->tcp_rnxt, TH_ACK); 17289 } 17290 } 17291 17292 (void) tcp_rcv_drain(tcp); 17293 } 17294 17295 /* 17296 * For fused tcp loopback, back-enable peer endpoint 17297 * if it's currently flow-controlled. 17298 */ 17299 if (tcp->tcp_fused) { 17300 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17301 17302 ASSERT(peer_tcp != NULL); 17303 ASSERT(peer_tcp->tcp_fused); 17304 /* 17305 * In order to change the peer's tcp_flow_stopped, 17306 * we need to take locks for both end points. The 17307 * highest address is taken first. 17308 */ 17309 if (peer_tcp > tcp) { 17310 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17311 mutex_enter(&tcp->tcp_non_sq_lock); 17312 } else { 17313 mutex_enter(&tcp->tcp_non_sq_lock); 17314 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17315 } 17316 if (peer_tcp->tcp_flow_stopped) { 17317 tcp_clrqfull(peer_tcp); 17318 TCP_STAT(tcps, tcp_fusion_backenabled); 17319 } 17320 mutex_exit(&peer_tcp->tcp_non_sq_lock); 17321 mutex_exit(&tcp->tcp_non_sq_lock); 17322 } 17323 } 17324 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17325 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17326 tcp->tcp_ordrel_done = B_TRUE; 17327 if (IPCL_IS_NONSTR(connp)) { 17328 ASSERT(tcp->tcp_ordrel_mp == NULL); 17329 (*connp->conn_upcalls->su_opctl)( 17330 connp->conn_upper_handle, 17331 SOCK_OPCTL_SHUT_RECV, 0); 17332 } else { 17333 mp = tcp->tcp_ordrel_mp; 17334 tcp->tcp_ordrel_mp = NULL; 17335 putnext(q, mp); 17336 } 17337 } 17338 if (tcp->tcp_hard_binding) { 17339 tcp->tcp_hard_binding = B_FALSE; 17340 tcp->tcp_hard_bound = B_TRUE; 17341 } 17342 17343 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 17344 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 17345 tcp->tcp_loopback_peer != NULL && 17346 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 17347 tcp_fuse_syncstr_enable_pair(tcp); 17348 } 17349 17350 if (tcp->tcp_ka_enabled) { 17351 tcp->tcp_ka_last_intrvl = 0; 17352 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17353 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17354 } 17355 17356 /* 17357 * At this point, eager is fully established and will 17358 * have the following references - 17359 * 17360 * 2 references for connection to exist (1 for TCP and 1 for IP). 17361 * 1 reference for the squeue which will be dropped by the squeue as 17362 * soon as this function returns. 17363 * There will be 1 additonal reference for being in classifier 17364 * hash list provided something bad hasn't happened. 17365 */ 17366 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17367 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17368 } 17369 17370 /* 17371 * The function called through squeue to get behind listener's perimeter to 17372 * send a deffered conn_ind. 17373 */ 17374 /* ARGSUSED */ 17375 void 17376 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17377 { 17378 conn_t *connp = (conn_t *)arg; 17379 tcp_t *listener = connp->conn_tcp; 17380 struct T_conn_ind *conn_ind; 17381 tcp_t *tcp; 17382 17383 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17384 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17385 conn_ind->OPT_length); 17386 17387 if (listener->tcp_state == TCPS_CLOSED || 17388 TCP_IS_DETACHED(listener)) { 17389 /* 17390 * If listener has closed, it would have caused a 17391 * a cleanup/blowoff to happen for the eager. 17392 * 17393 * We need to drop the ref on eager that was put 17394 * tcp_rput_data() before trying to send the conn_ind 17395 * to listener. The conn_ind was deferred in tcp_send_conn_ind 17396 * and tcp_wput_accept() is sending this deferred conn_ind but 17397 * listener is closed so we drop the ref. 17398 */ 17399 CONN_DEC_REF(tcp->tcp_connp); 17400 freemsg(mp); 17401 return; 17402 } 17403 17404 tcp_ulp_newconn(connp, tcp->tcp_connp, mp); 17405 } 17406 17407 /* ARGSUSED */ 17408 static int 17409 tcp_accept_common(conn_t *lconnp, conn_t *econnp, cred_t *cr) 17410 { 17411 tcp_t *listener, *eager; 17412 mblk_t *opt_mp; 17413 struct tcp_options *tcpopt; 17414 17415 listener = lconnp->conn_tcp; 17416 ASSERT(listener->tcp_state == TCPS_LISTEN); 17417 eager = econnp->conn_tcp; 17418 ASSERT(eager->tcp_listener != NULL); 17419 17420 ASSERT(eager->tcp_rq != NULL); 17421 17422 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 17423 if (opt_mp == NULL) { 17424 return (-TPROTO); 17425 } 17426 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 17427 eager->tcp_issocket = B_TRUE; 17428 17429 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17430 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 17431 ASSERT(econnp->conn_netstack == 17432 listener->tcp_connp->conn_netstack); 17433 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 17434 17435 /* Put the ref for IP */ 17436 CONN_INC_REF(econnp); 17437 17438 /* 17439 * We should have minimum of 3 references on the conn 17440 * at this point. One each for TCP and IP and one for 17441 * the T_conn_ind that was sent up when the 3-way handshake 17442 * completed. In the normal case we would also have another 17443 * reference (making a total of 4) for the conn being in the 17444 * classifier hash list. However the eager could have received 17445 * an RST subsequently and tcp_closei_local could have removed 17446 * the eager from the classifier hash list, hence we can't 17447 * assert that reference. 17448 */ 17449 ASSERT(econnp->conn_ref >= 3); 17450 17451 opt_mp->b_datap->db_type = M_SETOPTS; 17452 opt_mp->b_wptr += sizeof (struct tcp_options); 17453 17454 /* 17455 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 17456 * from listener to acceptor. 17457 */ 17458 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 17459 tcpopt->to_flags = 0; 17460 17461 if (listener->tcp_bound_if != 0) { 17462 tcpopt->to_flags |= TCPOPT_BOUNDIF; 17463 tcpopt->to_boundif = listener->tcp_bound_if; 17464 } 17465 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 17466 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 17467 } 17468 17469 mutex_enter(&listener->tcp_eager_lock); 17470 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 17471 17472 tcp_t *tail; 17473 tcp_t *tcp; 17474 mblk_t *mp1; 17475 17476 tcp = listener->tcp_eager_prev_q0; 17477 /* 17478 * listener->tcp_eager_prev_q0 points to the TAIL of the 17479 * deferred T_conn_ind queue. We need to get to the head 17480 * of the queue in order to send up T_conn_ind the same 17481 * order as how the 3WHS is completed. 17482 */ 17483 while (tcp != listener) { 17484 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 17485 !tcp->tcp_kssl_pending) 17486 break; 17487 else 17488 tcp = tcp->tcp_eager_prev_q0; 17489 } 17490 /* None of the pending eagers can be sent up now */ 17491 if (tcp == listener) 17492 goto no_more_eagers; 17493 17494 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 17495 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17496 /* Move from q0 to q */ 17497 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 17498 listener->tcp_conn_req_cnt_q0--; 17499 listener->tcp_conn_req_cnt_q++; 17500 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 17501 tcp->tcp_eager_prev_q0; 17502 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 17503 tcp->tcp_eager_next_q0; 17504 tcp->tcp_eager_prev_q0 = NULL; 17505 tcp->tcp_eager_next_q0 = NULL; 17506 tcp->tcp_conn_def_q0 = B_FALSE; 17507 17508 /* Make sure the tcp isn't in the list of droppables */ 17509 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 17510 tcp->tcp_eager_prev_drop_q0 == NULL); 17511 17512 /* 17513 * Insert at end of the queue because sockfs sends 17514 * down T_CONN_RES in chronological order. Leaving 17515 * the older conn indications at front of the queue 17516 * helps reducing search time. 17517 */ 17518 tail = listener->tcp_eager_last_q; 17519 if (tail != NULL) { 17520 tail->tcp_eager_next_q = tcp; 17521 } else { 17522 listener->tcp_eager_next_q = tcp; 17523 } 17524 listener->tcp_eager_last_q = tcp; 17525 tcp->tcp_eager_next_q = NULL; 17526 17527 /* Need to get inside the listener perimeter */ 17528 CONN_INC_REF(listener->tcp_connp); 17529 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 17530 tcp_send_pending, listener->tcp_connp, SQ_FILL, 17531 SQTAG_TCP_SEND_PENDING); 17532 } 17533 no_more_eagers: 17534 tcp_eager_unlink(eager); 17535 mutex_exit(&listener->tcp_eager_lock); 17536 17537 /* 17538 * At this point, the eager is detached from the listener 17539 * but we still have an extra refs on eager (apart from the 17540 * usual tcp references). The ref was placed in tcp_rput_data 17541 * before sending the conn_ind in tcp_send_conn_ind. 17542 * The ref will be dropped in tcp_accept_finish(). 17543 */ 17544 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 17545 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 17546 return (0); 17547 } 17548 17549 int 17550 tcp_accept(sock_lower_handle_t lproto_handle, 17551 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 17552 cred_t *cr) 17553 { 17554 conn_t *lconnp, *econnp; 17555 tcp_t *listener, *eager; 17556 tcp_stack_t *tcps; 17557 17558 lconnp = (conn_t *)lproto_handle; 17559 listener = lconnp->conn_tcp; 17560 ASSERT(listener->tcp_state == TCPS_LISTEN); 17561 econnp = (conn_t *)eproto_handle; 17562 eager = econnp->conn_tcp; 17563 ASSERT(eager->tcp_listener != NULL); 17564 tcps = eager->tcp_tcps; 17565 17566 /* 17567 * It is OK to manipulate these fields outside the eager's squeue 17568 * because they will not start being used until tcp_accept_finish 17569 * has been called. 17570 */ 17571 ASSERT(lconnp->conn_upper_handle != NULL); 17572 ASSERT(econnp->conn_upper_handle == NULL); 17573 econnp->conn_upper_handle = sock_handle; 17574 econnp->conn_upcalls = lconnp->conn_upcalls; 17575 ASSERT(IPCL_IS_NONSTR(econnp)); 17576 /* 17577 * Create helper stream if it is a non-TPI TCP connection. 17578 */ 17579 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 17580 ip1dbg(("tcp_accept: create of IP helper stream" 17581 " failed\n")); 17582 return (EPROTO); 17583 } 17584 eager->tcp_rq = econnp->conn_rq; 17585 eager->tcp_wq = econnp->conn_wq; 17586 17587 ASSERT(eager->tcp_rq != NULL); 17588 17589 return (tcp_accept_common(lconnp, econnp, cr)); 17590 } 17591 17592 17593 /* 17594 * This is the STREAMS entry point for T_CONN_RES coming down on 17595 * Acceptor STREAM when sockfs listener does accept processing. 17596 * Read the block comment on top of tcp_conn_request(). 17597 */ 17598 void 17599 tcp_tpi_accept(queue_t *q, mblk_t *mp) 17600 { 17601 queue_t *rq = RD(q); 17602 struct T_conn_res *conn_res; 17603 tcp_t *eager; 17604 tcp_t *listener; 17605 struct T_ok_ack *ok; 17606 t_scalar_t PRIM_type; 17607 conn_t *econnp; 17608 cred_t *cr; 17609 17610 ASSERT(DB_TYPE(mp) == M_PROTO); 17611 17612 /* 17613 * All Solaris components should pass a db_credp 17614 * for this TPI message, hence we ASSERT. 17615 * But in case there is some other M_PROTO that looks 17616 * like a TPI message sent by some other kernel 17617 * component, we check and return an error. 17618 */ 17619 cr = msg_getcred(mp, NULL); 17620 ASSERT(cr != NULL); 17621 if (cr == NULL) { 17622 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 17623 if (mp != NULL) 17624 putnext(rq, mp); 17625 return; 17626 } 17627 conn_res = (struct T_conn_res *)mp->b_rptr; 17628 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17629 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17630 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17631 if (mp != NULL) 17632 putnext(rq, mp); 17633 return; 17634 } 17635 switch (conn_res->PRIM_type) { 17636 case O_T_CONN_RES: 17637 case T_CONN_RES: 17638 /* 17639 * We pass up an err ack if allocb fails. This will 17640 * cause sockfs to issue a T_DISCON_REQ which will cause 17641 * tcp_eager_blowoff to be called. sockfs will then call 17642 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17643 * we need to do the allocb up here because we have to 17644 * make sure rq->q_qinfo->qi_qclose still points to the 17645 * correct function (tcp_tpi_close_accept) in case allocb 17646 * fails. 17647 */ 17648 bcopy(mp->b_rptr + conn_res->OPT_offset, 17649 &eager, conn_res->OPT_length); 17650 PRIM_type = conn_res->PRIM_type; 17651 mp->b_datap->db_type = M_PCPROTO; 17652 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 17653 ok = (struct T_ok_ack *)mp->b_rptr; 17654 ok->PRIM_type = T_OK_ACK; 17655 ok->CORRECT_prim = PRIM_type; 17656 econnp = eager->tcp_connp; 17657 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 17658 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 17659 eager->tcp_rq = rq; 17660 eager->tcp_wq = q; 17661 rq->q_ptr = econnp; 17662 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 17663 q->q_ptr = econnp; 17664 q->q_qinfo = &tcp_winit; 17665 listener = eager->tcp_listener; 17666 17667 if (tcp_accept_common(listener->tcp_connp, 17668 econnp, cr) < 0) { 17669 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17670 if (mp != NULL) 17671 putnext(rq, mp); 17672 return; 17673 } 17674 17675 /* 17676 * Send the new local address also up to sockfs. There 17677 * should already be enough space in the mp that came 17678 * down from soaccept(). 17679 */ 17680 if (eager->tcp_family == AF_INET) { 17681 sin_t *sin; 17682 17683 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17684 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 17685 sin = (sin_t *)mp->b_wptr; 17686 mp->b_wptr += sizeof (sin_t); 17687 sin->sin_family = AF_INET; 17688 sin->sin_port = eager->tcp_lport; 17689 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 17690 } else { 17691 sin6_t *sin6; 17692 17693 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17694 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 17695 sin6 = (sin6_t *)mp->b_wptr; 17696 mp->b_wptr += sizeof (sin6_t); 17697 sin6->sin6_family = AF_INET6; 17698 sin6->sin6_port = eager->tcp_lport; 17699 if (eager->tcp_ipversion == IPV4_VERSION) { 17700 sin6->sin6_flowinfo = 0; 17701 IN6_IPADDR_TO_V4MAPPED( 17702 eager->tcp_ipha->ipha_src, 17703 &sin6->sin6_addr); 17704 } else { 17705 ASSERT(eager->tcp_ip6h != NULL); 17706 sin6->sin6_flowinfo = 17707 eager->tcp_ip6h->ip6_vcf & 17708 ~IPV6_VERS_AND_FLOW_MASK; 17709 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 17710 } 17711 sin6->sin6_scope_id = 0; 17712 sin6->__sin6_src_id = 0; 17713 } 17714 17715 putnext(rq, mp); 17716 return; 17717 default: 17718 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 17719 if (mp != NULL) 17720 putnext(rq, mp); 17721 return; 17722 } 17723 } 17724 17725 static int 17726 tcp_do_getsockname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17727 { 17728 sin_t *sin = (sin_t *)sa; 17729 sin6_t *sin6 = (sin6_t *)sa; 17730 17731 switch (tcp->tcp_family) { 17732 case AF_INET: 17733 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17734 17735 if (*salenp < sizeof (sin_t)) 17736 return (EINVAL); 17737 17738 *sin = sin_null; 17739 sin->sin_family = AF_INET; 17740 if (tcp->tcp_state >= TCPS_BOUND) { 17741 sin->sin_port = tcp->tcp_lport; 17742 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 17743 } 17744 *salenp = sizeof (sin_t); 17745 break; 17746 17747 case AF_INET6: 17748 if (*salenp < sizeof (sin6_t)) 17749 return (EINVAL); 17750 17751 *sin6 = sin6_null; 17752 sin6->sin6_family = AF_INET6; 17753 if (tcp->tcp_state >= TCPS_BOUND) { 17754 sin6->sin6_port = tcp->tcp_lport; 17755 if (tcp->tcp_ipversion == IPV4_VERSION) { 17756 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 17757 &sin6->sin6_addr); 17758 } else { 17759 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 17760 } 17761 } 17762 *salenp = sizeof (sin6_t); 17763 break; 17764 } 17765 17766 return (0); 17767 } 17768 17769 static int 17770 tcp_do_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17771 { 17772 sin_t *sin = (sin_t *)sa; 17773 sin6_t *sin6 = (sin6_t *)sa; 17774 17775 if (tcp->tcp_state < TCPS_SYN_RCVD) 17776 return (ENOTCONN); 17777 17778 switch (tcp->tcp_family) { 17779 case AF_INET: 17780 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17781 17782 if (*salenp < sizeof (sin_t)) 17783 return (EINVAL); 17784 17785 *sin = sin_null; 17786 sin->sin_family = AF_INET; 17787 sin->sin_port = tcp->tcp_fport; 17788 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 17789 sin->sin_addr.s_addr); 17790 *salenp = sizeof (sin_t); 17791 break; 17792 17793 case AF_INET6: 17794 if (*salenp < sizeof (sin6_t)) 17795 return (EINVAL); 17796 17797 *sin6 = sin6_null; 17798 sin6->sin6_family = AF_INET6; 17799 sin6->sin6_port = tcp->tcp_fport; 17800 sin6->sin6_addr = tcp->tcp_remote_v6; 17801 if (tcp->tcp_ipversion == IPV6_VERSION) { 17802 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 17803 ~IPV6_VERS_AND_FLOW_MASK; 17804 } 17805 *salenp = sizeof (sin6_t); 17806 break; 17807 } 17808 17809 return (0); 17810 } 17811 17812 /* 17813 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 17814 */ 17815 static void 17816 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 17817 { 17818 void *data; 17819 mblk_t *datamp = mp->b_cont; 17820 tcp_t *tcp = Q_TO_TCP(q); 17821 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 17822 17823 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 17824 cmdp->cb_error = EPROTO; 17825 qreply(q, mp); 17826 return; 17827 } 17828 17829 data = datamp->b_rptr; 17830 17831 switch (cmdp->cb_cmd) { 17832 case TI_GETPEERNAME: 17833 cmdp->cb_error = tcp_do_getpeername(tcp, data, &cmdp->cb_len); 17834 break; 17835 case TI_GETMYNAME: 17836 cmdp->cb_error = tcp_do_getsockname(tcp, data, &cmdp->cb_len); 17837 break; 17838 default: 17839 cmdp->cb_error = EINVAL; 17840 break; 17841 } 17842 17843 qreply(q, mp); 17844 } 17845 17846 void 17847 tcp_wput(queue_t *q, mblk_t *mp) 17848 { 17849 conn_t *connp = Q_TO_CONN(q); 17850 tcp_t *tcp; 17851 void (*output_proc)(); 17852 t_scalar_t type; 17853 uchar_t *rptr; 17854 struct iocblk *iocp; 17855 size_t size; 17856 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 17857 17858 ASSERT(connp->conn_ref >= 2); 17859 17860 switch (DB_TYPE(mp)) { 17861 case M_DATA: 17862 tcp = connp->conn_tcp; 17863 ASSERT(tcp != NULL); 17864 17865 size = msgdsize(mp); 17866 17867 mutex_enter(&tcp->tcp_non_sq_lock); 17868 tcp->tcp_squeue_bytes += size; 17869 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 17870 tcp_setqfull(tcp); 17871 } 17872 mutex_exit(&tcp->tcp_non_sq_lock); 17873 17874 CONN_INC_REF(connp); 17875 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 17876 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 17877 return; 17878 17879 case M_CMD: 17880 tcp_wput_cmdblk(q, mp); 17881 return; 17882 17883 case M_PROTO: 17884 case M_PCPROTO: 17885 /* 17886 * if it is a snmp message, don't get behind the squeue 17887 */ 17888 tcp = connp->conn_tcp; 17889 rptr = mp->b_rptr; 17890 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 17891 type = ((union T_primitives *)rptr)->type; 17892 } else { 17893 if (tcp->tcp_debug) { 17894 (void) strlog(TCP_MOD_ID, 0, 1, 17895 SL_ERROR|SL_TRACE, 17896 "tcp_wput_proto, dropping one..."); 17897 } 17898 freemsg(mp); 17899 return; 17900 } 17901 if (type == T_SVR4_OPTMGMT_REQ) { 17902 /* 17903 * All Solaris components should pass a db_credp 17904 * for this TPI message, hence we ASSERT. 17905 * But in case there is some other M_PROTO that looks 17906 * like a TPI message sent by some other kernel 17907 * component, we check and return an error. 17908 */ 17909 cred_t *cr = msg_getcred(mp, NULL); 17910 17911 ASSERT(cr != NULL); 17912 if (cr == NULL) { 17913 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 17914 return; 17915 } 17916 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 17917 cr)) { 17918 /* 17919 * This was a SNMP request 17920 */ 17921 return; 17922 } else { 17923 output_proc = tcp_wput_proto; 17924 } 17925 } else { 17926 output_proc = tcp_wput_proto; 17927 } 17928 break; 17929 case M_IOCTL: 17930 /* 17931 * Most ioctls can be processed right away without going via 17932 * squeues - process them right here. Those that do require 17933 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 17934 * are processed by tcp_wput_ioctl(). 17935 */ 17936 iocp = (struct iocblk *)mp->b_rptr; 17937 tcp = connp->conn_tcp; 17938 17939 switch (iocp->ioc_cmd) { 17940 case TCP_IOC_ABORT_CONN: 17941 tcp_ioctl_abort_conn(q, mp); 17942 return; 17943 case TI_GETPEERNAME: 17944 case TI_GETMYNAME: 17945 mi_copyin(q, mp, NULL, 17946 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 17947 return; 17948 case ND_SET: 17949 /* nd_getset does the necessary checks */ 17950 case ND_GET: 17951 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 17952 CALL_IP_WPUT(connp, q, mp); 17953 return; 17954 } 17955 qreply(q, mp); 17956 return; 17957 case TCP_IOC_DEFAULT_Q: 17958 /* 17959 * Wants to be the default wq. Check the credentials 17960 * first, the rest is executed via squeue. 17961 */ 17962 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 17963 iocp->ioc_error = EPERM; 17964 iocp->ioc_count = 0; 17965 mp->b_datap->db_type = M_IOCACK; 17966 qreply(q, mp); 17967 return; 17968 } 17969 output_proc = tcp_wput_ioctl; 17970 break; 17971 default: 17972 output_proc = tcp_wput_ioctl; 17973 break; 17974 } 17975 break; 17976 default: 17977 output_proc = tcp_wput_nondata; 17978 break; 17979 } 17980 17981 CONN_INC_REF(connp); 17982 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 17983 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 17984 } 17985 17986 /* 17987 * Initial STREAMS write side put() procedure for sockets. It tries to 17988 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 17989 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 17990 * are handled by tcp_wput() as usual. 17991 * 17992 * All further messages will also be handled by tcp_wput() because we cannot 17993 * be sure that the above short cut is safe later. 17994 */ 17995 static void 17996 tcp_wput_sock(queue_t *wq, mblk_t *mp) 17997 { 17998 conn_t *connp = Q_TO_CONN(wq); 17999 tcp_t *tcp = connp->conn_tcp; 18000 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18001 18002 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18003 wq->q_qinfo = &tcp_winit; 18004 18005 ASSERT(IPCL_IS_TCP(connp)); 18006 ASSERT(TCP_IS_SOCKET(tcp)); 18007 18008 if (DB_TYPE(mp) == M_PCPROTO && 18009 MBLKL(mp) == sizeof (struct T_capability_req) && 18010 car->PRIM_type == T_CAPABILITY_REQ) { 18011 tcp_capability_req(tcp, mp); 18012 return; 18013 } 18014 18015 tcp_wput(wq, mp); 18016 } 18017 18018 /* ARGSUSED */ 18019 static void 18020 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18021 { 18022 #ifdef DEBUG 18023 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18024 #endif 18025 freemsg(mp); 18026 } 18027 18028 static boolean_t 18029 tcp_zcopy_check(tcp_t *tcp) 18030 { 18031 conn_t *connp = tcp->tcp_connp; 18032 ire_t *ire; 18033 boolean_t zc_enabled = B_FALSE; 18034 tcp_stack_t *tcps = tcp->tcp_tcps; 18035 18036 if (do_tcpzcopy == 2) 18037 zc_enabled = B_TRUE; 18038 else if (tcp->tcp_ipversion == IPV4_VERSION && 18039 IPCL_IS_CONNECTED(connp) && 18040 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18041 connp->conn_dontroute == 0 && 18042 !connp->conn_nexthop_set && 18043 connp->conn_outgoing_ill == NULL && 18044 do_tcpzcopy == 1) { 18045 /* 18046 * the checks above closely resemble the fast path checks 18047 * in tcp_send_data(). 18048 */ 18049 mutex_enter(&connp->conn_lock); 18050 ire = connp->conn_ire_cache; 18051 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18052 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18053 IRE_REFHOLD(ire); 18054 if (ire->ire_stq != NULL) { 18055 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18056 18057 zc_enabled = ill && (ill->ill_capabilities & 18058 ILL_CAPAB_ZEROCOPY) && 18059 (ill->ill_zerocopy_capab-> 18060 ill_zerocopy_flags != 0); 18061 } 18062 IRE_REFRELE(ire); 18063 } 18064 mutex_exit(&connp->conn_lock); 18065 } 18066 tcp->tcp_snd_zcopy_on = zc_enabled; 18067 if (!TCP_IS_DETACHED(tcp)) { 18068 if (zc_enabled) { 18069 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18070 ZCVMSAFE); 18071 TCP_STAT(tcps, tcp_zcopy_on); 18072 } else { 18073 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18074 ZCVMUNSAFE); 18075 TCP_STAT(tcps, tcp_zcopy_off); 18076 } 18077 } 18078 return (zc_enabled); 18079 } 18080 18081 static mblk_t * 18082 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18083 { 18084 tcp_stack_t *tcps = tcp->tcp_tcps; 18085 18086 if (do_tcpzcopy == 2) 18087 return (bp); 18088 else if (tcp->tcp_snd_zcopy_on) { 18089 tcp->tcp_snd_zcopy_on = B_FALSE; 18090 if (!TCP_IS_DETACHED(tcp)) { 18091 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 18092 ZCVMUNSAFE); 18093 TCP_STAT(tcps, tcp_zcopy_disable); 18094 } 18095 } 18096 return (tcp_zcopy_backoff(tcp, bp, 0)); 18097 } 18098 18099 /* 18100 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18101 * the original desballoca'ed segmapped mblk. 18102 */ 18103 static mblk_t * 18104 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18105 { 18106 mblk_t *head, *tail, *nbp; 18107 tcp_stack_t *tcps = tcp->tcp_tcps; 18108 18109 if (IS_VMLOANED_MBLK(bp)) { 18110 TCP_STAT(tcps, tcp_zcopy_backoff); 18111 if ((head = copyb(bp)) == NULL) { 18112 /* fail to backoff; leave it for the next backoff */ 18113 tcp->tcp_xmit_zc_clean = B_FALSE; 18114 return (bp); 18115 } 18116 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18117 if (fix_xmitlist) 18118 tcp_zcopy_notify(tcp); 18119 else 18120 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18121 } 18122 nbp = bp->b_cont; 18123 if (fix_xmitlist) { 18124 head->b_prev = bp->b_prev; 18125 head->b_next = bp->b_next; 18126 if (tcp->tcp_xmit_tail == bp) 18127 tcp->tcp_xmit_tail = head; 18128 } 18129 bp->b_next = NULL; 18130 bp->b_prev = NULL; 18131 freeb(bp); 18132 } else { 18133 head = bp; 18134 nbp = bp->b_cont; 18135 } 18136 tail = head; 18137 while (nbp) { 18138 if (IS_VMLOANED_MBLK(nbp)) { 18139 TCP_STAT(tcps, tcp_zcopy_backoff); 18140 if ((tail->b_cont = copyb(nbp)) == NULL) { 18141 tcp->tcp_xmit_zc_clean = B_FALSE; 18142 tail->b_cont = nbp; 18143 return (head); 18144 } 18145 tail = tail->b_cont; 18146 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18147 if (fix_xmitlist) 18148 tcp_zcopy_notify(tcp); 18149 else 18150 tail->b_datap->db_struioflag |= 18151 STRUIO_ZCNOTIFY; 18152 } 18153 bp = nbp; 18154 nbp = nbp->b_cont; 18155 if (fix_xmitlist) { 18156 tail->b_prev = bp->b_prev; 18157 tail->b_next = bp->b_next; 18158 if (tcp->tcp_xmit_tail == bp) 18159 tcp->tcp_xmit_tail = tail; 18160 } 18161 bp->b_next = NULL; 18162 bp->b_prev = NULL; 18163 freeb(bp); 18164 } else { 18165 tail->b_cont = nbp; 18166 tail = nbp; 18167 nbp = nbp->b_cont; 18168 } 18169 } 18170 if (fix_xmitlist) { 18171 tcp->tcp_xmit_last = tail; 18172 tcp->tcp_xmit_zc_clean = B_TRUE; 18173 } 18174 return (head); 18175 } 18176 18177 static void 18178 tcp_zcopy_notify(tcp_t *tcp) 18179 { 18180 struct stdata *stp; 18181 conn_t *connp; 18182 18183 if (tcp->tcp_detached) 18184 return; 18185 connp = tcp->tcp_connp; 18186 if (IPCL_IS_NONSTR(connp)) { 18187 (*connp->conn_upcalls->su_zcopy_notify) 18188 (connp->conn_upper_handle); 18189 return; 18190 } 18191 stp = STREAM(tcp->tcp_rq); 18192 mutex_enter(&stp->sd_lock); 18193 stp->sd_flag |= STZCNOTIFY; 18194 cv_broadcast(&stp->sd_zcopy_wait); 18195 mutex_exit(&stp->sd_lock); 18196 } 18197 18198 static boolean_t 18199 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18200 { 18201 ire_t *ire; 18202 conn_t *connp = tcp->tcp_connp; 18203 tcp_stack_t *tcps = tcp->tcp_tcps; 18204 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18205 18206 mutex_enter(&connp->conn_lock); 18207 ire = connp->conn_ire_cache; 18208 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18209 18210 if ((ire != NULL) && 18211 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18212 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18213 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18214 IRE_REFHOLD(ire); 18215 mutex_exit(&connp->conn_lock); 18216 } else { 18217 boolean_t cached = B_FALSE; 18218 ts_label_t *tsl; 18219 18220 /* force a recheck later on */ 18221 tcp->tcp_ire_ill_check_done = B_FALSE; 18222 18223 TCP_DBGSTAT(tcps, tcp_ire_null1); 18224 connp->conn_ire_cache = NULL; 18225 mutex_exit(&connp->conn_lock); 18226 18227 if (ire != NULL) 18228 IRE_REFRELE_NOTR(ire); 18229 18230 tsl = crgetlabel(CONN_CRED(connp)); 18231 ire = (dst ? 18232 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18233 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18234 connp->conn_zoneid, tsl, ipst)); 18235 18236 if (ire == NULL) { 18237 TCP_STAT(tcps, tcp_ire_null); 18238 return (B_FALSE); 18239 } 18240 18241 IRE_REFHOLD_NOTR(ire); 18242 18243 mutex_enter(&connp->conn_lock); 18244 if (CONN_CACHE_IRE(connp)) { 18245 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18246 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18247 TCP_CHECK_IREINFO(tcp, ire); 18248 connp->conn_ire_cache = ire; 18249 cached = B_TRUE; 18250 } 18251 rw_exit(&ire->ire_bucket->irb_lock); 18252 } 18253 mutex_exit(&connp->conn_lock); 18254 18255 /* 18256 * We can continue to use the ire but since it was 18257 * not cached, we should drop the extra reference. 18258 */ 18259 if (!cached) 18260 IRE_REFRELE_NOTR(ire); 18261 18262 /* 18263 * Rampart note: no need to select a new label here, since 18264 * labels are not allowed to change during the life of a TCP 18265 * connection. 18266 */ 18267 } 18268 18269 *irep = ire; 18270 18271 return (B_TRUE); 18272 } 18273 18274 /* 18275 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18276 * 18277 * 0 = success; 18278 * 1 = failed to find ire and ill. 18279 */ 18280 static boolean_t 18281 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18282 { 18283 ipha_t *ipha; 18284 ipaddr_t dst; 18285 ire_t *ire; 18286 ill_t *ill; 18287 mblk_t *ire_fp_mp; 18288 tcp_stack_t *tcps = tcp->tcp_tcps; 18289 18290 if (mp != NULL) 18291 ipha = (ipha_t *)mp->b_rptr; 18292 else 18293 ipha = tcp->tcp_ipha; 18294 dst = ipha->ipha_dst; 18295 18296 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18297 return (B_FALSE); 18298 18299 if ((ire->ire_flags & RTF_MULTIRT) || 18300 (ire->ire_stq == NULL) || 18301 (ire->ire_nce == NULL) || 18302 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18303 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18304 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18305 TCP_STAT(tcps, tcp_ip_ire_send); 18306 IRE_REFRELE(ire); 18307 return (B_FALSE); 18308 } 18309 18310 ill = ire_to_ill(ire); 18311 ASSERT(ill != NULL); 18312 18313 if (!tcp->tcp_ire_ill_check_done) { 18314 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18315 tcp->tcp_ire_ill_check_done = B_TRUE; 18316 } 18317 18318 *irep = ire; 18319 *illp = ill; 18320 18321 return (B_TRUE); 18322 } 18323 18324 static void 18325 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18326 { 18327 ipha_t *ipha; 18328 ipaddr_t src; 18329 ipaddr_t dst; 18330 uint32_t cksum; 18331 ire_t *ire; 18332 uint16_t *up; 18333 ill_t *ill; 18334 conn_t *connp = tcp->tcp_connp; 18335 uint32_t hcksum_txflags = 0; 18336 mblk_t *ire_fp_mp; 18337 uint_t ire_fp_mp_len; 18338 tcp_stack_t *tcps = tcp->tcp_tcps; 18339 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18340 cred_t *cr; 18341 pid_t cpid; 18342 18343 ASSERT(DB_TYPE(mp) == M_DATA); 18344 18345 /* 18346 * Here we need to handle the overloading of the cred_t for 18347 * both getpeerucred and TX. 18348 * If this is a SYN then the caller already set db_credp so 18349 * that getpeerucred will work. But if TX is in use we might have 18350 * a conn_peercred which is different, and we need to use that cred 18351 * to make TX use the correct label and label dependent route. 18352 */ 18353 if (is_system_labeled()) { 18354 cr = msg_getcred(mp, &cpid); 18355 if (cr == NULL || connp->conn_peercred != NULL) 18356 mblk_setcred(mp, CONN_CRED(connp), cpid); 18357 } 18358 18359 ipha = (ipha_t *)mp->b_rptr; 18360 src = ipha->ipha_src; 18361 dst = ipha->ipha_dst; 18362 18363 ASSERT(q != NULL); 18364 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 18365 18366 /* 18367 * Drop off fast path for IPv6 and also if options are present or 18368 * we need to resolve a TS label. 18369 */ 18370 if (tcp->tcp_ipversion != IPV4_VERSION || 18371 !IPCL_IS_CONNECTED(connp) || 18372 !CONN_IS_LSO_MD_FASTPATH(connp) || 18373 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18374 !connp->conn_ulp_labeled || 18375 ipha->ipha_ident == IP_HDR_INCLUDED || 18376 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18377 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18378 if (tcp->tcp_snd_zcopy_aware) 18379 mp = tcp_zcopy_disable(tcp, mp); 18380 TCP_STAT(tcps, tcp_ip_send); 18381 CALL_IP_WPUT(connp, q, mp); 18382 return; 18383 } 18384 18385 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18386 if (tcp->tcp_snd_zcopy_aware) 18387 mp = tcp_zcopy_backoff(tcp, mp, 0); 18388 CALL_IP_WPUT(connp, q, mp); 18389 return; 18390 } 18391 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18392 ire_fp_mp_len = MBLKL(ire_fp_mp); 18393 18394 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18395 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18396 #ifndef _BIG_ENDIAN 18397 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18398 #endif 18399 18400 /* 18401 * Check to see if we need to re-enable LSO/MDT for this connection 18402 * because it was previously disabled due to changes in the ill; 18403 * note that by doing it here, this re-enabling only applies when 18404 * the packet is not dispatched through CALL_IP_WPUT(). 18405 * 18406 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18407 * case, since that's how we ended up here. For IPv6, we do the 18408 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18409 */ 18410 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18411 /* 18412 * Restore LSO for this connection, so that next time around 18413 * it is eligible to go through tcp_lsosend() path again. 18414 */ 18415 TCP_STAT(tcps, tcp_lso_enabled); 18416 tcp->tcp_lso = B_TRUE; 18417 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18418 "interface %s\n", (void *)connp, ill->ill_name)); 18419 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18420 /* 18421 * Restore MDT for this connection, so that next time around 18422 * it is eligible to go through tcp_multisend() path again. 18423 */ 18424 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18425 tcp->tcp_mdt = B_TRUE; 18426 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18427 "interface %s\n", (void *)connp, ill->ill_name)); 18428 } 18429 18430 if (tcp->tcp_snd_zcopy_aware) { 18431 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18432 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18433 mp = tcp_zcopy_disable(tcp, mp); 18434 /* 18435 * we shouldn't need to reset ipha as the mp containing 18436 * ipha should never be a zero-copy mp. 18437 */ 18438 } 18439 18440 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18441 ASSERT(ill->ill_hcksum_capab != NULL); 18442 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18443 } 18444 18445 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18446 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18447 18448 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18449 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18450 18451 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18452 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18453 18454 /* Software checksum? */ 18455 if (DB_CKSUMFLAGS(mp) == 0) { 18456 TCP_STAT(tcps, tcp_out_sw_cksum); 18457 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18458 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18459 } 18460 18461 /* Calculate IP header checksum if hardware isn't capable */ 18462 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18463 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18464 ((uint16_t *)ipha)[4]); 18465 } 18466 18467 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18468 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18469 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18470 18471 UPDATE_OB_PKT_COUNT(ire); 18472 ire->ire_last_used_time = lbolt; 18473 18474 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 18475 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 18476 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 18477 ntohs(ipha->ipha_length)); 18478 18479 DTRACE_PROBE4(ip4__physical__out__start, 18480 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 18481 FW_HOOKS(ipst->ips_ip4_physical_out_event, 18482 ipst->ips_ipv4firewall_physical_out, 18483 NULL, ill, ipha, mp, mp, 0, ipst); 18484 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 18485 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 18486 18487 if (mp != NULL) { 18488 if (ipst->ips_ipobs_enabled) { 18489 zoneid_t szone; 18490 18491 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 18492 ipst, ALL_ZONES); 18493 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 18494 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 18495 } 18496 18497 ILL_SEND_TX(ill, ire, connp, mp, 0, NULL); 18498 } 18499 18500 IRE_REFRELE(ire); 18501 } 18502 18503 /* 18504 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18505 * if the receiver shrinks the window, i.e. moves the right window to the 18506 * left, the we should not send new data, but should retransmit normally the 18507 * old unacked data between suna and suna + swnd. We might has sent data 18508 * that is now outside the new window, pretend that we didn't send it. 18509 */ 18510 static void 18511 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18512 { 18513 uint32_t snxt = tcp->tcp_snxt; 18514 mblk_t *xmit_tail; 18515 int32_t offset; 18516 18517 ASSERT(shrunk_count > 0); 18518 18519 /* Pretend we didn't send the data outside the window */ 18520 snxt -= shrunk_count; 18521 18522 /* Get the mblk and the offset in it per the shrunk window */ 18523 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 18524 18525 ASSERT(xmit_tail != NULL); 18526 18527 /* Reset all the values per the now shrunk window */ 18528 tcp->tcp_snxt = snxt; 18529 tcp->tcp_xmit_tail = xmit_tail; 18530 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 18531 offset; 18532 tcp->tcp_unsent += shrunk_count; 18533 18534 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18535 /* 18536 * Make sure the timer is running so that we will probe a zero 18537 * window. 18538 */ 18539 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18540 } 18541 18542 18543 /* 18544 * The TCP normal data output path. 18545 * NOTE: the logic of the fast path is duplicated from this function. 18546 */ 18547 static void 18548 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18549 { 18550 int len; 18551 mblk_t *local_time; 18552 mblk_t *mp1; 18553 uint32_t snxt; 18554 int tail_unsent; 18555 int tcpstate; 18556 int usable = 0; 18557 mblk_t *xmit_tail; 18558 queue_t *q = tcp->tcp_wq; 18559 int32_t mss; 18560 int32_t num_sack_blk = 0; 18561 int32_t tcp_hdr_len; 18562 int32_t tcp_tcp_hdr_len; 18563 int mdt_thres; 18564 int rc; 18565 tcp_stack_t *tcps = tcp->tcp_tcps; 18566 ip_stack_t *ipst; 18567 18568 tcpstate = tcp->tcp_state; 18569 if (mp == NULL) { 18570 /* 18571 * tcp_wput_data() with NULL mp should only be called when 18572 * there is unsent data. 18573 */ 18574 ASSERT(tcp->tcp_unsent > 0); 18575 /* Really tacky... but we need this for detached closes. */ 18576 len = tcp->tcp_unsent; 18577 goto data_null; 18578 } 18579 18580 #if CCS_STATS 18581 wrw_stats.tot.count++; 18582 wrw_stats.tot.bytes += msgdsize(mp); 18583 #endif 18584 ASSERT(mp->b_datap->db_type == M_DATA); 18585 /* 18586 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18587 * or before a connection attempt has begun. 18588 */ 18589 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18590 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18591 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18592 #ifdef DEBUG 18593 cmn_err(CE_WARN, 18594 "tcp_wput_data: data after ordrel, %s", 18595 tcp_display(tcp, NULL, 18596 DISP_ADDR_AND_PORT)); 18597 #else 18598 if (tcp->tcp_debug) { 18599 (void) strlog(TCP_MOD_ID, 0, 1, 18600 SL_TRACE|SL_ERROR, 18601 "tcp_wput_data: data after ordrel, %s\n", 18602 tcp_display(tcp, NULL, 18603 DISP_ADDR_AND_PORT)); 18604 } 18605 #endif /* DEBUG */ 18606 } 18607 if (tcp->tcp_snd_zcopy_aware && 18608 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18609 tcp_zcopy_notify(tcp); 18610 freemsg(mp); 18611 mutex_enter(&tcp->tcp_non_sq_lock); 18612 if (tcp->tcp_flow_stopped && 18613 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18614 tcp_clrqfull(tcp); 18615 } 18616 mutex_exit(&tcp->tcp_non_sq_lock); 18617 return; 18618 } 18619 18620 /* Strip empties */ 18621 for (;;) { 18622 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18623 (uintptr_t)INT_MAX); 18624 len = (int)(mp->b_wptr - mp->b_rptr); 18625 if (len > 0) 18626 break; 18627 mp1 = mp; 18628 mp = mp->b_cont; 18629 freeb(mp1); 18630 if (!mp) { 18631 return; 18632 } 18633 } 18634 18635 /* If we are the first on the list ... */ 18636 if (tcp->tcp_xmit_head == NULL) { 18637 tcp->tcp_xmit_head = mp; 18638 tcp->tcp_xmit_tail = mp; 18639 tcp->tcp_xmit_tail_unsent = len; 18640 } else { 18641 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18642 struct datab *dp; 18643 18644 mp1 = tcp->tcp_xmit_last; 18645 if (len < tcp_tx_pull_len && 18646 (dp = mp1->b_datap)->db_ref == 1 && 18647 dp->db_lim - mp1->b_wptr >= len) { 18648 ASSERT(len > 0); 18649 ASSERT(!mp1->b_cont); 18650 if (len == 1) { 18651 *mp1->b_wptr++ = *mp->b_rptr; 18652 } else { 18653 bcopy(mp->b_rptr, mp1->b_wptr, len); 18654 mp1->b_wptr += len; 18655 } 18656 if (mp1 == tcp->tcp_xmit_tail) 18657 tcp->tcp_xmit_tail_unsent += len; 18658 mp1->b_cont = mp->b_cont; 18659 if (tcp->tcp_snd_zcopy_aware && 18660 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18661 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18662 freeb(mp); 18663 mp = mp1; 18664 } else { 18665 tcp->tcp_xmit_last->b_cont = mp; 18666 } 18667 len += tcp->tcp_unsent; 18668 } 18669 18670 /* Tack on however many more positive length mblks we have */ 18671 if ((mp1 = mp->b_cont) != NULL) { 18672 do { 18673 int tlen; 18674 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18675 (uintptr_t)INT_MAX); 18676 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18677 if (tlen <= 0) { 18678 mp->b_cont = mp1->b_cont; 18679 freeb(mp1); 18680 } else { 18681 len += tlen; 18682 mp = mp1; 18683 } 18684 } while ((mp1 = mp->b_cont) != NULL); 18685 } 18686 tcp->tcp_xmit_last = mp; 18687 tcp->tcp_unsent = len; 18688 18689 if (urgent) 18690 usable = 1; 18691 18692 data_null: 18693 snxt = tcp->tcp_snxt; 18694 xmit_tail = tcp->tcp_xmit_tail; 18695 tail_unsent = tcp->tcp_xmit_tail_unsent; 18696 18697 /* 18698 * Note that tcp_mss has been adjusted to take into account the 18699 * timestamp option if applicable. Because SACK options do not 18700 * appear in every TCP segments and they are of variable lengths, 18701 * they cannot be included in tcp_mss. Thus we need to calculate 18702 * the actual segment length when we need to send a segment which 18703 * includes SACK options. 18704 */ 18705 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 18706 int32_t opt_len; 18707 18708 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 18709 tcp->tcp_num_sack_blk); 18710 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 18711 2 + TCPOPT_HEADER_LEN; 18712 mss = tcp->tcp_mss - opt_len; 18713 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 18714 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 18715 } else { 18716 mss = tcp->tcp_mss; 18717 tcp_hdr_len = tcp->tcp_hdr_len; 18718 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 18719 } 18720 18721 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18722 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18723 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 18724 } 18725 if (tcpstate == TCPS_SYN_RCVD) { 18726 /* 18727 * The three-way connection establishment handshake is not 18728 * complete yet. We want to queue the data for transmission 18729 * after entering ESTABLISHED state (RFC793). A jump to 18730 * "done" label effectively leaves data on the queue. 18731 */ 18732 goto done; 18733 } else { 18734 int usable_r; 18735 18736 /* 18737 * In the special case when cwnd is zero, which can only 18738 * happen if the connection is ECN capable, return now. 18739 * New segments is sent using tcp_timer(). The timer 18740 * is set in tcp_rput_data(). 18741 */ 18742 if (tcp->tcp_cwnd == 0) { 18743 /* 18744 * Note that tcp_cwnd is 0 before 3-way handshake is 18745 * finished. 18746 */ 18747 ASSERT(tcp->tcp_ecn_ok || 18748 tcp->tcp_state < TCPS_ESTABLISHED); 18749 return; 18750 } 18751 18752 /* NOTE: trouble if xmitting while SYN not acked? */ 18753 usable_r = snxt - tcp->tcp_suna; 18754 usable_r = tcp->tcp_swnd - usable_r; 18755 18756 /* 18757 * Check if the receiver has shrunk the window. If 18758 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 18759 * cannot be set as there is unsent data, so FIN cannot 18760 * be sent out. Otherwise, we need to take into account 18761 * of FIN as it consumes an "invisible" sequence number. 18762 */ 18763 ASSERT(tcp->tcp_fin_sent == 0); 18764 if (usable_r < 0) { 18765 /* 18766 * The receiver has shrunk the window and we have sent 18767 * -usable_r date beyond the window, re-adjust. 18768 * 18769 * If TCP window scaling is enabled, there can be 18770 * round down error as the advertised receive window 18771 * is actually right shifted n bits. This means that 18772 * the lower n bits info is wiped out. It will look 18773 * like the window is shrunk. Do a check here to 18774 * see if the shrunk amount is actually within the 18775 * error in window calculation. If it is, just 18776 * return. Note that this check is inside the 18777 * shrunk window check. This makes sure that even 18778 * though tcp_process_shrunk_swnd() is not called, 18779 * we will stop further processing. 18780 */ 18781 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 18782 tcp_process_shrunk_swnd(tcp, -usable_r); 18783 } 18784 return; 18785 } 18786 18787 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 18788 if (tcp->tcp_swnd > tcp->tcp_cwnd) 18789 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 18790 18791 /* usable = MIN(usable, unsent) */ 18792 if (usable_r > len) 18793 usable_r = len; 18794 18795 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 18796 if (usable_r > 0) { 18797 usable = usable_r; 18798 } else { 18799 /* Bypass all other unnecessary processing. */ 18800 goto done; 18801 } 18802 } 18803 18804 local_time = (mblk_t *)lbolt; 18805 18806 /* 18807 * "Our" Nagle Algorithm. This is not the same as in the old 18808 * BSD. This is more in line with the true intent of Nagle. 18809 * 18810 * The conditions are: 18811 * 1. The amount of unsent data (or amount of data which can be 18812 * sent, whichever is smaller) is less than Nagle limit. 18813 * 2. The last sent size is also less than Nagle limit. 18814 * 3. There is unack'ed data. 18815 * 4. Urgent pointer is not set. Send urgent data ignoring the 18816 * Nagle algorithm. This reduces the probability that urgent 18817 * bytes get "merged" together. 18818 * 5. The app has not closed the connection. This eliminates the 18819 * wait time of the receiving side waiting for the last piece of 18820 * (small) data. 18821 * 18822 * If all are satisified, exit without sending anything. Note 18823 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 18824 * the smaller of 1 MSS and global tcp_naglim_def (default to be 18825 * 4095). 18826 */ 18827 if (usable < (int)tcp->tcp_naglim && 18828 tcp->tcp_naglim > tcp->tcp_last_sent_len && 18829 snxt != tcp->tcp_suna && 18830 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 18831 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 18832 goto done; 18833 } 18834 18835 if (tcp->tcp_cork) { 18836 /* 18837 * if the tcp->tcp_cork option is set, then we have to force 18838 * TCP not to send partial segment (smaller than MSS bytes). 18839 * We are calculating the usable now based on full mss and 18840 * will save the rest of remaining data for later. 18841 */ 18842 if (usable < mss) 18843 goto done; 18844 usable = (usable / mss) * mss; 18845 } 18846 18847 /* Update the latest receive window size in TCP header. */ 18848 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18849 tcp->tcp_tcph->th_win); 18850 18851 /* 18852 * Determine if it's worthwhile to attempt LSO or MDT, based on: 18853 * 18854 * 1. Simple TCP/IP{v4,v6} (no options). 18855 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 18856 * 3. If the TCP connection is in ESTABLISHED state. 18857 * 4. The TCP is not detached. 18858 * 18859 * If any of the above conditions have changed during the 18860 * connection, stop using LSO/MDT and restore the stream head 18861 * parameters accordingly. 18862 */ 18863 ipst = tcps->tcps_netstack->netstack_ip; 18864 18865 if ((tcp->tcp_lso || tcp->tcp_mdt) && 18866 ((tcp->tcp_ipversion == IPV4_VERSION && 18867 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 18868 (tcp->tcp_ipversion == IPV6_VERSION && 18869 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 18870 tcp->tcp_state != TCPS_ESTABLISHED || 18871 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 18872 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 18873 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 18874 if (tcp->tcp_lso) { 18875 tcp->tcp_connp->conn_lso_ok = B_FALSE; 18876 tcp->tcp_lso = B_FALSE; 18877 } else { 18878 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 18879 tcp->tcp_mdt = B_FALSE; 18880 } 18881 18882 /* Anything other than detached is considered pathological */ 18883 if (!TCP_IS_DETACHED(tcp)) { 18884 if (tcp->tcp_lso) 18885 TCP_STAT(tcps, tcp_lso_disabled); 18886 else 18887 TCP_STAT(tcps, tcp_mdt_conn_halted1); 18888 (void) tcp_maxpsz_set(tcp, B_TRUE); 18889 } 18890 } 18891 18892 /* Use MDT if sendable amount is greater than the threshold */ 18893 if (tcp->tcp_mdt && 18894 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 18895 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 18896 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 18897 (tcp->tcp_valid_bits == 0 || 18898 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 18899 ASSERT(tcp->tcp_connp->conn_mdt_ok); 18900 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18901 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18902 local_time, mdt_thres); 18903 } else { 18904 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18905 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18906 local_time, INT_MAX); 18907 } 18908 18909 /* Pretend that all we were trying to send really got sent */ 18910 if (rc < 0 && tail_unsent < 0) { 18911 do { 18912 xmit_tail = xmit_tail->b_cont; 18913 xmit_tail->b_prev = local_time; 18914 ASSERT((uintptr_t)(xmit_tail->b_wptr - 18915 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 18916 tail_unsent += (int)(xmit_tail->b_wptr - 18917 xmit_tail->b_rptr); 18918 } while (tail_unsent < 0); 18919 } 18920 done:; 18921 tcp->tcp_xmit_tail = xmit_tail; 18922 tcp->tcp_xmit_tail_unsent = tail_unsent; 18923 len = tcp->tcp_snxt - snxt; 18924 if (len) { 18925 /* 18926 * If new data was sent, need to update the notsack 18927 * list, which is, afterall, data blocks that have 18928 * not been sack'ed by the receiver. New data is 18929 * not sack'ed. 18930 */ 18931 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 18932 /* len is a negative value. */ 18933 tcp->tcp_pipe -= len; 18934 tcp_notsack_update(&(tcp->tcp_notsack_list), 18935 tcp->tcp_snxt, snxt, 18936 &(tcp->tcp_num_notsack_blk), 18937 &(tcp->tcp_cnt_notsack_list)); 18938 } 18939 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 18940 tcp->tcp_rack = tcp->tcp_rnxt; 18941 tcp->tcp_rack_cnt = 0; 18942 if ((snxt + len) == tcp->tcp_suna) { 18943 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18944 } 18945 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 18946 /* 18947 * Didn't send anything. Make sure the timer is running 18948 * so that we will probe a zero window. 18949 */ 18950 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18951 } 18952 /* Note that len is the amount we just sent but with a negative sign */ 18953 tcp->tcp_unsent += len; 18954 mutex_enter(&tcp->tcp_non_sq_lock); 18955 if (tcp->tcp_flow_stopped) { 18956 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18957 tcp_clrqfull(tcp); 18958 } 18959 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 18960 tcp_setqfull(tcp); 18961 } 18962 mutex_exit(&tcp->tcp_non_sq_lock); 18963 } 18964 18965 /* 18966 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 18967 * outgoing TCP header with the template header, as well as other 18968 * options such as time-stamp, ECN and/or SACK. 18969 */ 18970 static void 18971 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 18972 { 18973 tcph_t *tcp_tmpl, *tcp_h; 18974 uint32_t *dst, *src; 18975 int hdrlen; 18976 18977 ASSERT(OK_32PTR(rptr)); 18978 18979 /* Template header */ 18980 tcp_tmpl = tcp->tcp_tcph; 18981 18982 /* Header of outgoing packet */ 18983 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 18984 18985 /* dst and src are opaque 32-bit fields, used for copying */ 18986 dst = (uint32_t *)rptr; 18987 src = (uint32_t *)tcp->tcp_iphc; 18988 hdrlen = tcp->tcp_hdr_len; 18989 18990 /* Fill time-stamp option if needed */ 18991 if (tcp->tcp_snd_ts_ok) { 18992 U32_TO_BE32((uint32_t)now, 18993 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 18994 U32_TO_BE32(tcp->tcp_ts_recent, 18995 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 18996 } else { 18997 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 18998 } 18999 19000 /* 19001 * Copy the template header; is this really more efficient than 19002 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19003 * but perhaps not for other scenarios. 19004 */ 19005 dst[0] = src[0]; 19006 dst[1] = src[1]; 19007 dst[2] = src[2]; 19008 dst[3] = src[3]; 19009 dst[4] = src[4]; 19010 dst[5] = src[5]; 19011 dst[6] = src[6]; 19012 dst[7] = src[7]; 19013 dst[8] = src[8]; 19014 dst[9] = src[9]; 19015 if (hdrlen -= 40) { 19016 hdrlen >>= 2; 19017 dst += 10; 19018 src += 10; 19019 do { 19020 *dst++ = *src++; 19021 } while (--hdrlen); 19022 } 19023 19024 /* 19025 * Set the ECN info in the TCP header if it is not a zero 19026 * window probe. Zero window probe is only sent in 19027 * tcp_wput_data() and tcp_timer(). 19028 */ 19029 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19030 SET_ECT(tcp, rptr); 19031 19032 if (tcp->tcp_ecn_echo_on) 19033 tcp_h->th_flags[0] |= TH_ECE; 19034 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19035 tcp_h->th_flags[0] |= TH_CWR; 19036 tcp->tcp_ecn_cwr_sent = B_TRUE; 19037 } 19038 } 19039 19040 /* Fill in SACK options */ 19041 if (num_sack_blk > 0) { 19042 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19043 sack_blk_t *tmp; 19044 int32_t i; 19045 19046 wptr[0] = TCPOPT_NOP; 19047 wptr[1] = TCPOPT_NOP; 19048 wptr[2] = TCPOPT_SACK; 19049 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19050 sizeof (sack_blk_t); 19051 wptr += TCPOPT_REAL_SACK_LEN; 19052 19053 tmp = tcp->tcp_sack_list; 19054 for (i = 0; i < num_sack_blk; i++) { 19055 U32_TO_BE32(tmp[i].begin, wptr); 19056 wptr += sizeof (tcp_seq); 19057 U32_TO_BE32(tmp[i].end, wptr); 19058 wptr += sizeof (tcp_seq); 19059 } 19060 tcp_h->th_offset_and_rsrvd[0] += 19061 ((num_sack_blk * 2 + 1) << 4); 19062 } 19063 } 19064 19065 /* 19066 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19067 * the destination address and SAP attribute, and if necessary, the 19068 * hardware checksum offload attribute to a Multidata message. 19069 */ 19070 static int 19071 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19072 const uint32_t start, const uint32_t stuff, const uint32_t end, 19073 const uint32_t flags, tcp_stack_t *tcps) 19074 { 19075 /* Add global destination address & SAP attribute */ 19076 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19077 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19078 "destination address+SAP\n")); 19079 19080 if (dlmp != NULL) 19081 TCP_STAT(tcps, tcp_mdt_allocfail); 19082 return (-1); 19083 } 19084 19085 /* Add global hwcksum attribute */ 19086 if (hwcksum && 19087 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19088 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19089 "checksum attribute\n")); 19090 19091 TCP_STAT(tcps, tcp_mdt_allocfail); 19092 return (-1); 19093 } 19094 19095 return (0); 19096 } 19097 19098 /* 19099 * Smaller and private version of pdescinfo_t used specifically for TCP, 19100 * which allows for only two payload spans per packet. 19101 */ 19102 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19103 19104 /* 19105 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19106 * scheme, and returns one the following: 19107 * 19108 * -1 = failed allocation. 19109 * 0 = success; burst count reached, or usable send window is too small, 19110 * and that we'd rather wait until later before sending again. 19111 */ 19112 static int 19113 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19114 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19115 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19116 const int mdt_thres) 19117 { 19118 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19119 multidata_t *mmd; 19120 uint_t obsegs, obbytes, hdr_frag_sz; 19121 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19122 int num_burst_seg, max_pld; 19123 pdesc_t *pkt; 19124 tcp_pdescinfo_t tcp_pkt_info; 19125 pdescinfo_t *pkt_info; 19126 int pbuf_idx, pbuf_idx_nxt; 19127 int seg_len, len, spill, af; 19128 boolean_t add_buffer, zcopy, clusterwide; 19129 boolean_t rconfirm = B_FALSE; 19130 boolean_t done = B_FALSE; 19131 uint32_t cksum; 19132 uint32_t hwcksum_flags; 19133 ire_t *ire = NULL; 19134 ill_t *ill; 19135 ipha_t *ipha; 19136 ip6_t *ip6h; 19137 ipaddr_t src, dst; 19138 ill_zerocopy_capab_t *zc_cap = NULL; 19139 uint16_t *up; 19140 int err; 19141 conn_t *connp; 19142 tcp_stack_t *tcps = tcp->tcp_tcps; 19143 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19144 int usable_mmd, tail_unsent_mmd; 19145 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 19146 mblk_t *xmit_tail_mmd; 19147 netstackid_t stack_id; 19148 19149 #ifdef _BIG_ENDIAN 19150 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19151 #else 19152 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19153 #endif 19154 19155 #define PREP_NEW_MULTIDATA() { \ 19156 mmd = NULL; \ 19157 md_mp = md_hbuf = NULL; \ 19158 cur_hdr_off = 0; \ 19159 max_pld = tcp->tcp_mdt_max_pld; \ 19160 pbuf_idx = pbuf_idx_nxt = -1; \ 19161 add_buffer = B_TRUE; \ 19162 zcopy = B_FALSE; \ 19163 } 19164 19165 #define PREP_NEW_PBUF() { \ 19166 md_pbuf = md_pbuf_nxt = NULL; \ 19167 pbuf_idx = pbuf_idx_nxt = -1; \ 19168 cur_pld_off = 0; \ 19169 first_snxt = *snxt; \ 19170 ASSERT(*tail_unsent > 0); \ 19171 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19172 } 19173 19174 ASSERT(mdt_thres >= mss); 19175 ASSERT(*usable > 0 && *usable > mdt_thres); 19176 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19177 ASSERT(!TCP_IS_DETACHED(tcp)); 19178 ASSERT(tcp->tcp_valid_bits == 0 || 19179 tcp->tcp_valid_bits == TCP_FSS_VALID); 19180 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19181 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19182 (tcp->tcp_ipversion == IPV6_VERSION && 19183 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19184 19185 connp = tcp->tcp_connp; 19186 ASSERT(connp != NULL); 19187 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19188 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19189 19190 stack_id = connp->conn_netstack->netstack_stackid; 19191 19192 usable_mmd = tail_unsent_mmd = 0; 19193 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 19194 xmit_tail_mmd = NULL; 19195 /* 19196 * Note that tcp will only declare at most 2 payload spans per 19197 * packet, which is much lower than the maximum allowable number 19198 * of packet spans per Multidata. For this reason, we use the 19199 * privately declared and smaller descriptor info structure, in 19200 * order to save some stack space. 19201 */ 19202 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19203 19204 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19205 if (af == AF_INET) { 19206 dst = tcp->tcp_ipha->ipha_dst; 19207 src = tcp->tcp_ipha->ipha_src; 19208 ASSERT(!CLASSD(dst)); 19209 } 19210 ASSERT(af == AF_INET || 19211 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19212 19213 obsegs = obbytes = 0; 19214 num_burst_seg = tcp->tcp_snd_burst; 19215 md_mp_head = NULL; 19216 PREP_NEW_MULTIDATA(); 19217 19218 /* 19219 * Before we go on further, make sure there is an IRE that we can 19220 * use, and that the ILL supports MDT. Otherwise, there's no point 19221 * in proceeding any further, and we should just hand everything 19222 * off to the legacy path. 19223 */ 19224 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19225 goto legacy_send_no_md; 19226 19227 ASSERT(ire != NULL); 19228 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19229 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19230 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19231 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19232 /* 19233 * If we do support loopback for MDT (which requires modifications 19234 * to the receiving paths), the following assertions should go away, 19235 * and we would be sending the Multidata to loopback conn later on. 19236 */ 19237 ASSERT(!IRE_IS_LOCAL(ire)); 19238 ASSERT(ire->ire_stq != NULL); 19239 19240 ill = ire_to_ill(ire); 19241 ASSERT(ill != NULL); 19242 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19243 19244 if (!tcp->tcp_ire_ill_check_done) { 19245 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19246 tcp->tcp_ire_ill_check_done = B_TRUE; 19247 } 19248 19249 /* 19250 * If the underlying interface conditions have changed, or if the 19251 * new interface does not support MDT, go back to legacy path. 19252 */ 19253 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19254 /* don't go through this path anymore for this connection */ 19255 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19256 tcp->tcp_mdt = B_FALSE; 19257 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19258 "interface %s\n", (void *)connp, ill->ill_name)); 19259 /* IRE will be released prior to returning */ 19260 goto legacy_send_no_md; 19261 } 19262 19263 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19264 zc_cap = ill->ill_zerocopy_capab; 19265 19266 /* 19267 * Check if we can take tcp fast-path. Note that "incomplete" 19268 * ire's (where the link-layer for next hop is not resolved 19269 * or where the fast-path header in nce_fp_mp is not available 19270 * yet) are sent down the legacy (slow) path. 19271 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19272 */ 19273 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19274 /* IRE will be released prior to returning */ 19275 goto legacy_send_no_md; 19276 } 19277 19278 /* go to legacy path if interface doesn't support zerocopy */ 19279 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19280 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19281 /* IRE will be released prior to returning */ 19282 goto legacy_send_no_md; 19283 } 19284 19285 /* does the interface support hardware checksum offload? */ 19286 hwcksum_flags = 0; 19287 if (ILL_HCKSUM_CAPABLE(ill) && 19288 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19289 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19290 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19291 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19292 HCKSUM_IPHDRCKSUM) 19293 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19294 19295 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19296 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19297 hwcksum_flags |= HCK_FULLCKSUM; 19298 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19299 HCKSUM_INET_PARTIAL) 19300 hwcksum_flags |= HCK_PARTIALCKSUM; 19301 } 19302 19303 /* 19304 * Each header fragment consists of the leading extra space, 19305 * followed by the TCP/IP header, and the trailing extra space. 19306 * We make sure that each header fragment begins on a 32-bit 19307 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19308 * aligned in tcp_mdt_update). 19309 */ 19310 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19311 tcp->tcp_mdt_hdr_tail), 4); 19312 19313 /* are we starting from the beginning of data block? */ 19314 if (*tail_unsent == 0) { 19315 *xmit_tail = (*xmit_tail)->b_cont; 19316 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19317 *tail_unsent = (int)MBLKL(*xmit_tail); 19318 } 19319 19320 /* 19321 * Here we create one or more Multidata messages, each made up of 19322 * one header buffer and up to N payload buffers. This entire 19323 * operation is done within two loops: 19324 * 19325 * The outer loop mostly deals with creating the Multidata message, 19326 * as well as the header buffer that gets added to it. It also 19327 * links the Multidata messages together such that all of them can 19328 * be sent down to the lower layer in a single putnext call; this 19329 * linking behavior depends on the tcp_mdt_chain tunable. 19330 * 19331 * The inner loop takes an existing Multidata message, and adds 19332 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19333 * packetizes those buffers by filling up the corresponding header 19334 * buffer fragments with the proper IP and TCP headers, and by 19335 * describing the layout of each packet in the packet descriptors 19336 * that get added to the Multidata. 19337 */ 19338 do { 19339 /* 19340 * If usable send window is too small, or data blocks in 19341 * transmit list are smaller than our threshold (i.e. app 19342 * performs large writes followed by small ones), we hand 19343 * off the control over to the legacy path. Note that we'll 19344 * get back the control once it encounters a large block. 19345 */ 19346 if (*usable < mss || (*tail_unsent <= mdt_thres && 19347 (*xmit_tail)->b_cont != NULL && 19348 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19349 /* send down what we've got so far */ 19350 if (md_mp_head != NULL) { 19351 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19352 obsegs, obbytes, &rconfirm); 19353 } 19354 /* 19355 * Pass control over to tcp_send(), but tell it to 19356 * return to us once a large-size transmission is 19357 * possible. 19358 */ 19359 TCP_STAT(tcps, tcp_mdt_legacy_small); 19360 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19361 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19362 tail_unsent, xmit_tail, local_time, 19363 mdt_thres)) <= 0) { 19364 /* burst count reached, or alloc failed */ 19365 IRE_REFRELE(ire); 19366 return (err); 19367 } 19368 19369 /* tcp_send() may have sent everything, so check */ 19370 if (*usable <= 0) { 19371 IRE_REFRELE(ire); 19372 return (0); 19373 } 19374 19375 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19376 /* 19377 * We may have delivered the Multidata, so make sure 19378 * to re-initialize before the next round. 19379 */ 19380 md_mp_head = NULL; 19381 obsegs = obbytes = 0; 19382 num_burst_seg = tcp->tcp_snd_burst; 19383 PREP_NEW_MULTIDATA(); 19384 19385 /* are we starting from the beginning of data block? */ 19386 if (*tail_unsent == 0) { 19387 *xmit_tail = (*xmit_tail)->b_cont; 19388 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19389 (uintptr_t)INT_MAX); 19390 *tail_unsent = (int)MBLKL(*xmit_tail); 19391 } 19392 } 19393 /* 19394 * Record current values for parameters we may need to pass 19395 * to tcp_send() or tcp_multisend_data(). We checkpoint at 19396 * each iteration of the outer loop (each multidata message 19397 * creation). If we have a failure in the inner loop, we send 19398 * any complete multidata messages we have before reverting 19399 * to using the traditional non-md path. 19400 */ 19401 snxt_mmd = *snxt; 19402 usable_mmd = *usable; 19403 xmit_tail_mmd = *xmit_tail; 19404 tail_unsent_mmd = *tail_unsent; 19405 obsegs_mmd = obsegs; 19406 obbytes_mmd = obbytes; 19407 19408 /* 19409 * max_pld limits the number of mblks in tcp's transmit 19410 * queue that can be added to a Multidata message. Once 19411 * this counter reaches zero, no more additional mblks 19412 * can be added to it. What happens afterwards depends 19413 * on whether or not we are set to chain the Multidata 19414 * messages. If we are to link them together, reset 19415 * max_pld to its original value (tcp_mdt_max_pld) and 19416 * prepare to create a new Multidata message which will 19417 * get linked to md_mp_head. Else, leave it alone and 19418 * let the inner loop break on its own. 19419 */ 19420 if (tcp_mdt_chain && max_pld == 0) 19421 PREP_NEW_MULTIDATA(); 19422 19423 /* adding a payload buffer; re-initialize values */ 19424 if (add_buffer) 19425 PREP_NEW_PBUF(); 19426 19427 /* 19428 * If we don't have a Multidata, either because we just 19429 * (re)entered this outer loop, or after we branched off 19430 * to tcp_send above, setup the Multidata and header 19431 * buffer to be used. 19432 */ 19433 if (md_mp == NULL) { 19434 int md_hbuflen; 19435 uint32_t start, stuff; 19436 19437 /* 19438 * Calculate Multidata header buffer size large enough 19439 * to hold all of the headers that can possibly be 19440 * sent at this moment. We'd rather over-estimate 19441 * the size than running out of space; this is okay 19442 * since this buffer is small anyway. 19443 */ 19444 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19445 19446 /* 19447 * Start and stuff offset for partial hardware 19448 * checksum offload; these are currently for IPv4. 19449 * For full checksum offload, they are set to zero. 19450 */ 19451 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19452 if (af == AF_INET) { 19453 start = IP_SIMPLE_HDR_LENGTH; 19454 stuff = IP_SIMPLE_HDR_LENGTH + 19455 TCP_CHECKSUM_OFFSET; 19456 } else { 19457 start = IPV6_HDR_LEN; 19458 stuff = IPV6_HDR_LEN + 19459 TCP_CHECKSUM_OFFSET; 19460 } 19461 } else { 19462 start = stuff = 0; 19463 } 19464 19465 /* 19466 * Create the header buffer, Multidata, as well as 19467 * any necessary attributes (destination address, 19468 * SAP and hardware checksum offload) that should 19469 * be associated with the Multidata message. 19470 */ 19471 ASSERT(cur_hdr_off == 0); 19472 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19473 ((md_hbuf->b_wptr += md_hbuflen), 19474 (mmd = mmd_alloc(md_hbuf, &md_mp, 19475 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19476 /* fastpath mblk */ 19477 ire->ire_nce->nce_res_mp, 19478 /* hardware checksum enabled */ 19479 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19480 /* hardware checksum offsets */ 19481 start, stuff, 0, 19482 /* hardware checksum flag */ 19483 hwcksum_flags, tcps) != 0)) { 19484 legacy_send: 19485 /* 19486 * We arrive here from a failure within the 19487 * inner (packetizer) loop or we fail one of 19488 * the conditionals above. We restore the 19489 * previously checkpointed values for: 19490 * xmit_tail 19491 * usable 19492 * tail_unsent 19493 * snxt 19494 * obbytes 19495 * obsegs 19496 * We should then be able to dispatch any 19497 * complete multidata before reverting to the 19498 * traditional path with consistent parameters 19499 * (the inner loop updates these as it 19500 * iterates). 19501 */ 19502 *xmit_tail = xmit_tail_mmd; 19503 *usable = usable_mmd; 19504 *tail_unsent = tail_unsent_mmd; 19505 *snxt = snxt_mmd; 19506 obbytes = obbytes_mmd; 19507 obsegs = obsegs_mmd; 19508 if (md_mp != NULL) { 19509 /* Unlink message from the chain */ 19510 if (md_mp_head != NULL) { 19511 err = (intptr_t)rmvb(md_mp_head, 19512 md_mp); 19513 /* 19514 * We can't assert that rmvb 19515 * did not return -1, since we 19516 * may get here before linkb 19517 * happens. We do, however, 19518 * check if we just removed the 19519 * only element in the list. 19520 */ 19521 if (err == 0) 19522 md_mp_head = NULL; 19523 } 19524 /* md_hbuf gets freed automatically */ 19525 TCP_STAT(tcps, tcp_mdt_discarded); 19526 freeb(md_mp); 19527 } else { 19528 /* Either allocb or mmd_alloc failed */ 19529 TCP_STAT(tcps, tcp_mdt_allocfail); 19530 if (md_hbuf != NULL) 19531 freeb(md_hbuf); 19532 } 19533 19534 /* send down what we've got so far */ 19535 if (md_mp_head != NULL) { 19536 tcp_multisend_data(tcp, ire, ill, 19537 md_mp_head, obsegs, obbytes, 19538 &rconfirm); 19539 } 19540 legacy_send_no_md: 19541 if (ire != NULL) 19542 IRE_REFRELE(ire); 19543 /* 19544 * Too bad; let the legacy path handle this. 19545 * We specify INT_MAX for the threshold, since 19546 * we gave up with the Multidata processings 19547 * and let the old path have it all. 19548 */ 19549 TCP_STAT(tcps, tcp_mdt_legacy_all); 19550 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19551 tcp_tcp_hdr_len, num_sack_blk, usable, 19552 snxt, tail_unsent, xmit_tail, local_time, 19553 INT_MAX)); 19554 } 19555 19556 /* link to any existing ones, if applicable */ 19557 TCP_STAT(tcps, tcp_mdt_allocd); 19558 if (md_mp_head == NULL) { 19559 md_mp_head = md_mp; 19560 } else if (tcp_mdt_chain) { 19561 TCP_STAT(tcps, tcp_mdt_linked); 19562 linkb(md_mp_head, md_mp); 19563 } 19564 } 19565 19566 ASSERT(md_mp_head != NULL); 19567 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19568 ASSERT(md_mp != NULL && mmd != NULL); 19569 ASSERT(md_hbuf != NULL); 19570 19571 /* 19572 * Packetize the transmittable portion of the data block; 19573 * each data block is essentially added to the Multidata 19574 * as a payload buffer. We also deal with adding more 19575 * than one payload buffers, which happens when the remaining 19576 * packetized portion of the current payload buffer is less 19577 * than MSS, while the next data block in transmit queue 19578 * has enough data to make up for one. This "spillover" 19579 * case essentially creates a split-packet, where portions 19580 * of the packet's payload fragments may span across two 19581 * virtually discontiguous address blocks. 19582 */ 19583 seg_len = mss; 19584 do { 19585 len = seg_len; 19586 19587 /* one must remain NULL for DTRACE_IP_FASTPATH */ 19588 ipha = NULL; 19589 ip6h = NULL; 19590 19591 ASSERT(len > 0); 19592 ASSERT(max_pld >= 0); 19593 ASSERT(!add_buffer || cur_pld_off == 0); 19594 19595 /* 19596 * First time around for this payload buffer; note 19597 * in the case of a spillover, the following has 19598 * been done prior to adding the split-packet 19599 * descriptor to Multidata, and we don't want to 19600 * repeat the process. 19601 */ 19602 if (add_buffer) { 19603 ASSERT(mmd != NULL); 19604 ASSERT(md_pbuf == NULL); 19605 ASSERT(md_pbuf_nxt == NULL); 19606 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19607 19608 /* 19609 * Have we reached the limit? We'd get to 19610 * this case when we're not chaining the 19611 * Multidata messages together, and since 19612 * we're done, terminate this loop. 19613 */ 19614 if (max_pld == 0) 19615 break; /* done */ 19616 19617 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19618 TCP_STAT(tcps, tcp_mdt_allocfail); 19619 goto legacy_send; /* out_of_mem */ 19620 } 19621 19622 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19623 zc_cap != NULL) { 19624 if (!ip_md_zcopy_attr(mmd, NULL, 19625 zc_cap->ill_zerocopy_flags)) { 19626 freeb(md_pbuf); 19627 TCP_STAT(tcps, 19628 tcp_mdt_allocfail); 19629 /* out_of_mem */ 19630 goto legacy_send; 19631 } 19632 zcopy = B_TRUE; 19633 } 19634 19635 md_pbuf->b_rptr += base_pld_off; 19636 19637 /* 19638 * Add a payload buffer to the Multidata; this 19639 * operation must not fail, or otherwise our 19640 * logic in this routine is broken. There 19641 * is no memory allocation done by the 19642 * routine, so any returned failure simply 19643 * tells us that we've done something wrong. 19644 * 19645 * A failure tells us that either we're adding 19646 * the same payload buffer more than once, or 19647 * we're trying to add more buffers than 19648 * allowed (max_pld calculation is wrong). 19649 * None of the above cases should happen, and 19650 * we panic because either there's horrible 19651 * heap corruption, and/or programming mistake. 19652 */ 19653 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19654 if (pbuf_idx < 0) { 19655 cmn_err(CE_PANIC, "tcp_multisend: " 19656 "payload buffer logic error " 19657 "detected for tcp %p mmd %p " 19658 "pbuf %p (%d)\n", 19659 (void *)tcp, (void *)mmd, 19660 (void *)md_pbuf, pbuf_idx); 19661 } 19662 19663 ASSERT(max_pld > 0); 19664 --max_pld; 19665 add_buffer = B_FALSE; 19666 } 19667 19668 ASSERT(md_mp_head != NULL); 19669 ASSERT(md_pbuf != NULL); 19670 ASSERT(md_pbuf_nxt == NULL); 19671 ASSERT(pbuf_idx != -1); 19672 ASSERT(pbuf_idx_nxt == -1); 19673 ASSERT(*usable > 0); 19674 19675 /* 19676 * We spillover to the next payload buffer only 19677 * if all of the following is true: 19678 * 19679 * 1. There is not enough data on the current 19680 * payload buffer to make up `len', 19681 * 2. We are allowed to send `len', 19682 * 3. The next payload buffer length is large 19683 * enough to accomodate `spill'. 19684 */ 19685 if ((spill = len - *tail_unsent) > 0 && 19686 *usable >= len && 19687 MBLKL((*xmit_tail)->b_cont) >= spill && 19688 max_pld > 0) { 19689 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19690 if (md_pbuf_nxt == NULL) { 19691 TCP_STAT(tcps, tcp_mdt_allocfail); 19692 goto legacy_send; /* out_of_mem */ 19693 } 19694 19695 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19696 zc_cap != NULL) { 19697 if (!ip_md_zcopy_attr(mmd, NULL, 19698 zc_cap->ill_zerocopy_flags)) { 19699 freeb(md_pbuf_nxt); 19700 TCP_STAT(tcps, 19701 tcp_mdt_allocfail); 19702 /* out_of_mem */ 19703 goto legacy_send; 19704 } 19705 zcopy = B_TRUE; 19706 } 19707 19708 /* 19709 * See comments above on the first call to 19710 * mmd_addpldbuf for explanation on the panic. 19711 */ 19712 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19713 if (pbuf_idx_nxt < 0) { 19714 panic("tcp_multisend: " 19715 "next payload buffer logic error " 19716 "detected for tcp %p mmd %p " 19717 "pbuf %p (%d)\n", 19718 (void *)tcp, (void *)mmd, 19719 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19720 } 19721 19722 ASSERT(max_pld > 0); 19723 --max_pld; 19724 } else if (spill > 0) { 19725 /* 19726 * If there's a spillover, but the following 19727 * xmit_tail couldn't give us enough octets 19728 * to reach "len", then stop the current 19729 * Multidata creation and let the legacy 19730 * tcp_send() path take over. We don't want 19731 * to send the tiny segment as part of this 19732 * Multidata for performance reasons; instead, 19733 * we let the legacy path deal with grouping 19734 * it with the subsequent small mblks. 19735 */ 19736 if (*usable >= len && 19737 MBLKL((*xmit_tail)->b_cont) < spill) { 19738 max_pld = 0; 19739 break; /* done */ 19740 } 19741 19742 /* 19743 * We can't spillover, and we are near 19744 * the end of the current payload buffer, 19745 * so send what's left. 19746 */ 19747 ASSERT(*tail_unsent > 0); 19748 len = *tail_unsent; 19749 } 19750 19751 /* tail_unsent is negated if there is a spillover */ 19752 *tail_unsent -= len; 19753 *usable -= len; 19754 ASSERT(*usable >= 0); 19755 19756 if (*usable < mss) 19757 seg_len = *usable; 19758 /* 19759 * Sender SWS avoidance; see comments in tcp_send(); 19760 * everything else is the same, except that we only 19761 * do this here if there is no more data to be sent 19762 * following the current xmit_tail. We don't check 19763 * for 1-byte urgent data because we shouldn't get 19764 * here if TCP_URG_VALID is set. 19765 */ 19766 if (*usable > 0 && *usable < mss && 19767 ((md_pbuf_nxt == NULL && 19768 (*xmit_tail)->b_cont == NULL) || 19769 (md_pbuf_nxt != NULL && 19770 (*xmit_tail)->b_cont->b_cont == NULL)) && 19771 seg_len < (tcp->tcp_max_swnd >> 1) && 19772 (tcp->tcp_unsent - 19773 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 19774 !tcp->tcp_zero_win_probe) { 19775 if ((*snxt + len) == tcp->tcp_snxt && 19776 (*snxt + len) == tcp->tcp_suna) { 19777 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19778 } 19779 done = B_TRUE; 19780 } 19781 19782 /* 19783 * Prime pump for IP's checksumming on our behalf; 19784 * include the adjustment for a source route if any. 19785 * Do this only for software/partial hardware checksum 19786 * offload, as this field gets zeroed out later for 19787 * the full hardware checksum offload case. 19788 */ 19789 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 19790 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19791 cksum = (cksum >> 16) + (cksum & 0xFFFF); 19792 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 19793 } 19794 19795 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 19796 *snxt += len; 19797 19798 tcp->tcp_tcph->th_flags[0] = TH_ACK; 19799 /* 19800 * We set the PUSH bit only if TCP has no more buffered 19801 * data to be transmitted (or if sender SWS avoidance 19802 * takes place), as opposed to setting it for every 19803 * last packet in the burst. 19804 */ 19805 if (done || 19806 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 19807 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 19808 19809 /* 19810 * Set FIN bit if this is our last segment; snxt 19811 * already includes its length, and it will not 19812 * be adjusted after this point. 19813 */ 19814 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 19815 *snxt == tcp->tcp_fss) { 19816 if (!tcp->tcp_fin_acked) { 19817 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 19818 BUMP_MIB(&tcps->tcps_mib, 19819 tcpOutControl); 19820 } 19821 if (!tcp->tcp_fin_sent) { 19822 tcp->tcp_fin_sent = B_TRUE; 19823 /* 19824 * tcp state must be ESTABLISHED 19825 * in order for us to get here in 19826 * the first place. 19827 */ 19828 tcp->tcp_state = TCPS_FIN_WAIT_1; 19829 19830 /* 19831 * Upon returning from this routine, 19832 * tcp_wput_data() will set tcp_snxt 19833 * to be equal to snxt + tcp_fin_sent. 19834 * This is essentially the same as 19835 * setting it to tcp_fss + 1. 19836 */ 19837 } 19838 } 19839 19840 tcp->tcp_last_sent_len = (ushort_t)len; 19841 19842 len += tcp_hdr_len; 19843 if (tcp->tcp_ipversion == IPV4_VERSION) 19844 tcp->tcp_ipha->ipha_length = htons(len); 19845 else 19846 tcp->tcp_ip6h->ip6_plen = htons(len - 19847 ((char *)&tcp->tcp_ip6h[1] - 19848 tcp->tcp_iphc)); 19849 19850 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 19851 19852 /* setup header fragment */ 19853 PDESC_HDR_ADD(pkt_info, 19854 md_hbuf->b_rptr + cur_hdr_off, /* base */ 19855 tcp->tcp_mdt_hdr_head, /* head room */ 19856 tcp_hdr_len, /* len */ 19857 tcp->tcp_mdt_hdr_tail); /* tail room */ 19858 19859 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 19860 hdr_frag_sz); 19861 ASSERT(MBLKIN(md_hbuf, 19862 (pkt_info->hdr_base - md_hbuf->b_rptr), 19863 PDESC_HDRSIZE(pkt_info))); 19864 19865 /* setup first payload fragment */ 19866 PDESC_PLD_INIT(pkt_info); 19867 PDESC_PLD_SPAN_ADD(pkt_info, 19868 pbuf_idx, /* index */ 19869 md_pbuf->b_rptr + cur_pld_off, /* start */ 19870 tcp->tcp_last_sent_len); /* len */ 19871 19872 /* create a split-packet in case of a spillover */ 19873 if (md_pbuf_nxt != NULL) { 19874 ASSERT(spill > 0); 19875 ASSERT(pbuf_idx_nxt > pbuf_idx); 19876 ASSERT(!add_buffer); 19877 19878 md_pbuf = md_pbuf_nxt; 19879 md_pbuf_nxt = NULL; 19880 pbuf_idx = pbuf_idx_nxt; 19881 pbuf_idx_nxt = -1; 19882 cur_pld_off = spill; 19883 19884 /* trim out first payload fragment */ 19885 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 19886 19887 /* setup second payload fragment */ 19888 PDESC_PLD_SPAN_ADD(pkt_info, 19889 pbuf_idx, /* index */ 19890 md_pbuf->b_rptr, /* start */ 19891 spill); /* len */ 19892 19893 if ((*xmit_tail)->b_next == NULL) { 19894 /* 19895 * Store the lbolt used for RTT 19896 * estimation. We can only record one 19897 * timestamp per mblk so we do it when 19898 * we reach the end of the payload 19899 * buffer. Also we only take a new 19900 * timestamp sample when the previous 19901 * timed data from the same mblk has 19902 * been ack'ed. 19903 */ 19904 (*xmit_tail)->b_prev = local_time; 19905 (*xmit_tail)->b_next = 19906 (mblk_t *)(uintptr_t)first_snxt; 19907 } 19908 19909 first_snxt = *snxt - spill; 19910 19911 /* 19912 * Advance xmit_tail; usable could be 0 by 19913 * the time we got here, but we made sure 19914 * above that we would only spillover to 19915 * the next data block if usable includes 19916 * the spilled-over amount prior to the 19917 * subtraction. Therefore, we are sure 19918 * that xmit_tail->b_cont can't be NULL. 19919 */ 19920 ASSERT((*xmit_tail)->b_cont != NULL); 19921 *xmit_tail = (*xmit_tail)->b_cont; 19922 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19923 (uintptr_t)INT_MAX); 19924 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 19925 } else { 19926 cur_pld_off += tcp->tcp_last_sent_len; 19927 } 19928 19929 /* 19930 * Fill in the header using the template header, and 19931 * add options such as time-stamp, ECN and/or SACK, 19932 * as needed. 19933 */ 19934 tcp_fill_header(tcp, pkt_info->hdr_rptr, 19935 (clock_t)local_time, num_sack_blk); 19936 19937 /* take care of some IP header businesses */ 19938 if (af == AF_INET) { 19939 ipha = (ipha_t *)pkt_info->hdr_rptr; 19940 19941 ASSERT(OK_32PTR((uchar_t *)ipha)); 19942 ASSERT(PDESC_HDRL(pkt_info) >= 19943 IP_SIMPLE_HDR_LENGTH); 19944 ASSERT(ipha->ipha_version_and_hdr_length == 19945 IP_SIMPLE_HDR_VERSION); 19946 19947 /* 19948 * Assign ident value for current packet; see 19949 * related comments in ip_wput_ire() about the 19950 * contract private interface with clustering 19951 * group. 19952 */ 19953 clusterwide = B_FALSE; 19954 if (cl_inet_ipident != NULL) { 19955 ASSERT(cl_inet_isclusterwide != NULL); 19956 if ((*cl_inet_isclusterwide)(stack_id, 19957 IPPROTO_IP, AF_INET, 19958 (uint8_t *)(uintptr_t)src, NULL)) { 19959 ipha->ipha_ident = 19960 (*cl_inet_ipident)(stack_id, 19961 IPPROTO_IP, AF_INET, 19962 (uint8_t *)(uintptr_t)src, 19963 (uint8_t *)(uintptr_t)dst, 19964 NULL); 19965 clusterwide = B_TRUE; 19966 } 19967 } 19968 19969 if (!clusterwide) { 19970 ipha->ipha_ident = (uint16_t) 19971 atomic_add_32_nv( 19972 &ire->ire_ident, 1); 19973 } 19974 #ifndef _BIG_ENDIAN 19975 ipha->ipha_ident = (ipha->ipha_ident << 8) | 19976 (ipha->ipha_ident >> 8); 19977 #endif 19978 } else { 19979 ip6h = (ip6_t *)pkt_info->hdr_rptr; 19980 19981 ASSERT(OK_32PTR((uchar_t *)ip6h)); 19982 ASSERT(IPVER(ip6h) == IPV6_VERSION); 19983 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 19984 ASSERT(PDESC_HDRL(pkt_info) >= 19985 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 19986 TCP_CHECKSUM_SIZE)); 19987 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 19988 19989 if (tcp->tcp_ip_forward_progress) { 19990 rconfirm = B_TRUE; 19991 tcp->tcp_ip_forward_progress = B_FALSE; 19992 } 19993 } 19994 19995 /* at least one payload span, and at most two */ 19996 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 19997 19998 /* add the packet descriptor to Multidata */ 19999 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20000 KM_NOSLEEP)) == NULL) { 20001 /* 20002 * Any failure other than ENOMEM indicates 20003 * that we have passed in invalid pkt_info 20004 * or parameters to mmd_addpdesc, which must 20005 * not happen. 20006 * 20007 * EINVAL is a result of failure on boundary 20008 * checks against the pkt_info contents. It 20009 * should not happen, and we panic because 20010 * either there's horrible heap corruption, 20011 * and/or programming mistake. 20012 */ 20013 if (err != ENOMEM) { 20014 cmn_err(CE_PANIC, "tcp_multisend: " 20015 "pdesc logic error detected for " 20016 "tcp %p mmd %p pinfo %p (%d)\n", 20017 (void *)tcp, (void *)mmd, 20018 (void *)pkt_info, err); 20019 } 20020 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20021 goto legacy_send; /* out_of_mem */ 20022 } 20023 ASSERT(pkt != NULL); 20024 20025 /* calculate IP header and TCP checksums */ 20026 if (af == AF_INET) { 20027 /* calculate pseudo-header checksum */ 20028 cksum = (dst >> 16) + (dst & 0xFFFF) + 20029 (src >> 16) + (src & 0xFFFF); 20030 20031 /* offset for TCP header checksum */ 20032 up = IPH_TCPH_CHECKSUMP(ipha, 20033 IP_SIMPLE_HDR_LENGTH); 20034 } else { 20035 up = (uint16_t *)&ip6h->ip6_src; 20036 20037 /* calculate pseudo-header checksum */ 20038 cksum = up[0] + up[1] + up[2] + up[3] + 20039 up[4] + up[5] + up[6] + up[7] + 20040 up[8] + up[9] + up[10] + up[11] + 20041 up[12] + up[13] + up[14] + up[15]; 20042 20043 /* Fold the initial sum */ 20044 cksum = (cksum & 0xffff) + (cksum >> 16); 20045 20046 up = (uint16_t *)(((uchar_t *)ip6h) + 20047 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20048 } 20049 20050 if (hwcksum_flags & HCK_FULLCKSUM) { 20051 /* clear checksum field for hardware */ 20052 *up = 0; 20053 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20054 uint32_t sum; 20055 20056 /* pseudo-header checksumming */ 20057 sum = *up + cksum + IP_TCP_CSUM_COMP; 20058 sum = (sum & 0xFFFF) + (sum >> 16); 20059 *up = (sum & 0xFFFF) + (sum >> 16); 20060 } else { 20061 /* software checksumming */ 20062 TCP_STAT(tcps, tcp_out_sw_cksum); 20063 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20064 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20065 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20066 cksum + IP_TCP_CSUM_COMP); 20067 if (*up == 0) 20068 *up = 0xFFFF; 20069 } 20070 20071 /* IPv4 header checksum */ 20072 if (af == AF_INET) { 20073 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20074 ipha->ipha_hdr_checksum = 0; 20075 } else { 20076 IP_HDR_CKSUM(ipha, cksum, 20077 ((uint32_t *)ipha)[0], 20078 ((uint16_t *)ipha)[4]); 20079 } 20080 } 20081 20082 if (af == AF_INET && 20083 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20084 af == AF_INET6 && 20085 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20086 mblk_t *mp, *mp1; 20087 uchar_t *hdr_rptr, *hdr_wptr; 20088 uchar_t *pld_rptr, *pld_wptr; 20089 20090 /* 20091 * We reconstruct a pseudo packet for the hooks 20092 * framework using mmd_transform_link(). 20093 * If it is a split packet we pullup the 20094 * payload. FW_HOOKS expects a pkt comprising 20095 * of two mblks: a header and the payload. 20096 */ 20097 if ((mp = mmd_transform_link(pkt)) == NULL) { 20098 TCP_STAT(tcps, tcp_mdt_allocfail); 20099 goto legacy_send; 20100 } 20101 20102 if (pkt_info->pld_cnt > 1) { 20103 /* split payload, more than one pld */ 20104 if ((mp1 = msgpullup(mp->b_cont, -1)) == 20105 NULL) { 20106 freemsg(mp); 20107 TCP_STAT(tcps, 20108 tcp_mdt_allocfail); 20109 goto legacy_send; 20110 } 20111 freemsg(mp->b_cont); 20112 mp->b_cont = mp1; 20113 } else { 20114 mp1 = mp->b_cont; 20115 } 20116 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 20117 20118 /* 20119 * Remember the message offsets. This is so we 20120 * can detect changes when we return from the 20121 * FW_HOOKS callbacks. 20122 */ 20123 hdr_rptr = mp->b_rptr; 20124 hdr_wptr = mp->b_wptr; 20125 pld_rptr = mp->b_cont->b_rptr; 20126 pld_wptr = mp->b_cont->b_wptr; 20127 20128 if (af == AF_INET) { 20129 DTRACE_PROBE4( 20130 ip4__physical__out__start, 20131 ill_t *, NULL, 20132 ill_t *, ill, 20133 ipha_t *, ipha, 20134 mblk_t *, mp); 20135 FW_HOOKS( 20136 ipst->ips_ip4_physical_out_event, 20137 ipst->ips_ipv4firewall_physical_out, 20138 NULL, ill, ipha, mp, mp, 0, ipst); 20139 DTRACE_PROBE1( 20140 ip4__physical__out__end, 20141 mblk_t *, mp); 20142 } else { 20143 DTRACE_PROBE4( 20144 ip6__physical__out_start, 20145 ill_t *, NULL, 20146 ill_t *, ill, 20147 ip6_t *, ip6h, 20148 mblk_t *, mp); 20149 FW_HOOKS6( 20150 ipst->ips_ip6_physical_out_event, 20151 ipst->ips_ipv6firewall_physical_out, 20152 NULL, ill, ip6h, mp, mp, 0, ipst); 20153 DTRACE_PROBE1( 20154 ip6__physical__out__end, 20155 mblk_t *, mp); 20156 } 20157 20158 if (mp == NULL || 20159 (mp1 = mp->b_cont) == NULL || 20160 mp->b_rptr != hdr_rptr || 20161 mp->b_wptr != hdr_wptr || 20162 mp1->b_rptr != pld_rptr || 20163 mp1->b_wptr != pld_wptr || 20164 mp1->b_cont != NULL) { 20165 /* 20166 * We abandon multidata processing and 20167 * return to the normal path, either 20168 * when a packet is blocked, or when 20169 * the boundaries of header buffer or 20170 * payload buffer have been changed by 20171 * FW_HOOKS[6]. 20172 */ 20173 if (mp != NULL) 20174 freemsg(mp); 20175 goto legacy_send; 20176 } 20177 /* Finished with the pseudo packet */ 20178 freemsg(mp); 20179 } 20180 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 20181 ill, ipha, ip6h); 20182 /* advance header offset */ 20183 cur_hdr_off += hdr_frag_sz; 20184 20185 obbytes += tcp->tcp_last_sent_len; 20186 ++obsegs; 20187 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20188 *tail_unsent > 0); 20189 20190 if ((*xmit_tail)->b_next == NULL) { 20191 /* 20192 * Store the lbolt used for RTT estimation. We can only 20193 * record one timestamp per mblk so we do it when we 20194 * reach the end of the payload buffer. Also we only 20195 * take a new timestamp sample when the previous timed 20196 * data from the same mblk has been ack'ed. 20197 */ 20198 (*xmit_tail)->b_prev = local_time; 20199 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20200 } 20201 20202 ASSERT(*tail_unsent >= 0); 20203 if (*tail_unsent > 0) { 20204 /* 20205 * We got here because we broke out of the above 20206 * loop due to of one of the following cases: 20207 * 20208 * 1. len < adjusted MSS (i.e. small), 20209 * 2. Sender SWS avoidance, 20210 * 3. max_pld is zero. 20211 * 20212 * We are done for this Multidata, so trim our 20213 * last payload buffer (if any) accordingly. 20214 */ 20215 if (md_pbuf != NULL) 20216 md_pbuf->b_wptr -= *tail_unsent; 20217 } else if (*usable > 0) { 20218 *xmit_tail = (*xmit_tail)->b_cont; 20219 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20220 (uintptr_t)INT_MAX); 20221 *tail_unsent = (int)MBLKL(*xmit_tail); 20222 add_buffer = B_TRUE; 20223 } 20224 } while (!done && *usable > 0 && num_burst_seg > 0 && 20225 (tcp_mdt_chain || max_pld > 0)); 20226 20227 if (md_mp_head != NULL) { 20228 /* send everything down */ 20229 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20230 &rconfirm); 20231 } 20232 20233 #undef PREP_NEW_MULTIDATA 20234 #undef PREP_NEW_PBUF 20235 #undef IPVER 20236 20237 IRE_REFRELE(ire); 20238 return (0); 20239 } 20240 20241 /* 20242 * A wrapper function for sending one or more Multidata messages down to 20243 * the module below ip; this routine does not release the reference of the 20244 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20245 */ 20246 static void 20247 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20248 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20249 { 20250 uint64_t delta; 20251 nce_t *nce; 20252 tcp_stack_t *tcps = tcp->tcp_tcps; 20253 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20254 20255 ASSERT(ire != NULL && ill != NULL); 20256 ASSERT(ire->ire_stq != NULL); 20257 ASSERT(md_mp_head != NULL); 20258 ASSERT(rconfirm != NULL); 20259 20260 /* adjust MIBs and IRE timestamp */ 20261 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 20262 tcp->tcp_obsegs += obsegs; 20263 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20264 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20265 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20266 20267 if (tcp->tcp_ipversion == IPV4_VERSION) { 20268 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20269 } else { 20270 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20271 } 20272 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20273 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20274 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20275 20276 ire->ire_ob_pkt_count += obsegs; 20277 if (ire->ire_ipif != NULL) 20278 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20279 ire->ire_last_used_time = lbolt; 20280 20281 if (ipst->ips_ipobs_enabled) { 20282 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 20283 pdesc_t *dl_pkt; 20284 pdescinfo_t pinfo; 20285 mblk_t *nmp; 20286 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 20287 20288 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 20289 (dl_pkt != NULL); 20290 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 20291 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 20292 continue; 20293 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 20294 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 20295 freemsg(nmp); 20296 } 20297 } 20298 20299 /* send it down */ 20300 putnext(ire->ire_stq, md_mp_head); 20301 20302 /* we're done for TCP/IPv4 */ 20303 if (tcp->tcp_ipversion == IPV4_VERSION) 20304 return; 20305 20306 nce = ire->ire_nce; 20307 20308 ASSERT(nce != NULL); 20309 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20310 ASSERT(nce->nce_state != ND_INCOMPLETE); 20311 20312 /* reachability confirmation? */ 20313 if (*rconfirm) { 20314 nce->nce_last = TICK_TO_MSEC(lbolt64); 20315 if (nce->nce_state != ND_REACHABLE) { 20316 mutex_enter(&nce->nce_lock); 20317 nce->nce_state = ND_REACHABLE; 20318 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20319 mutex_exit(&nce->nce_lock); 20320 (void) untimeout(nce->nce_timeout_id); 20321 if (ip_debug > 2) { 20322 /* ip1dbg */ 20323 pr_addr_dbg("tcp_multisend_data: state " 20324 "for %s changed to REACHABLE\n", 20325 AF_INET6, &ire->ire_addr_v6); 20326 } 20327 } 20328 /* reset transport reachability confirmation */ 20329 *rconfirm = B_FALSE; 20330 } 20331 20332 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20333 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20334 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20335 20336 if (delta > (uint64_t)ill->ill_reachable_time) { 20337 mutex_enter(&nce->nce_lock); 20338 switch (nce->nce_state) { 20339 case ND_REACHABLE: 20340 case ND_STALE: 20341 /* 20342 * ND_REACHABLE is identical to ND_STALE in this 20343 * specific case. If reachable time has expired for 20344 * this neighbor (delta is greater than reachable 20345 * time), conceptually, the neighbor cache is no 20346 * longer in REACHABLE state, but already in STALE 20347 * state. So the correct transition here is to 20348 * ND_DELAY. 20349 */ 20350 nce->nce_state = ND_DELAY; 20351 mutex_exit(&nce->nce_lock); 20352 NDP_RESTART_TIMER(nce, 20353 ipst->ips_delay_first_probe_time); 20354 if (ip_debug > 3) { 20355 /* ip2dbg */ 20356 pr_addr_dbg("tcp_multisend_data: state " 20357 "for %s changed to DELAY\n", 20358 AF_INET6, &ire->ire_addr_v6); 20359 } 20360 break; 20361 case ND_DELAY: 20362 case ND_PROBE: 20363 mutex_exit(&nce->nce_lock); 20364 /* Timers have already started */ 20365 break; 20366 case ND_UNREACHABLE: 20367 /* 20368 * ndp timer has detected that this nce is 20369 * unreachable and initiated deleting this nce 20370 * and all its associated IREs. This is a race 20371 * where we found the ire before it was deleted 20372 * and have just sent out a packet using this 20373 * unreachable nce. 20374 */ 20375 mutex_exit(&nce->nce_lock); 20376 break; 20377 default: 20378 ASSERT(0); 20379 } 20380 } 20381 } 20382 20383 /* 20384 * Derived from tcp_send_data(). 20385 */ 20386 static void 20387 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20388 int num_lso_seg) 20389 { 20390 ipha_t *ipha; 20391 mblk_t *ire_fp_mp; 20392 uint_t ire_fp_mp_len; 20393 uint32_t hcksum_txflags = 0; 20394 ipaddr_t src; 20395 ipaddr_t dst; 20396 uint32_t cksum; 20397 uint16_t *up; 20398 tcp_stack_t *tcps = tcp->tcp_tcps; 20399 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20400 20401 ASSERT(DB_TYPE(mp) == M_DATA); 20402 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20403 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20404 ASSERT(tcp->tcp_connp != NULL); 20405 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20406 20407 ipha = (ipha_t *)mp->b_rptr; 20408 src = ipha->ipha_src; 20409 dst = ipha->ipha_dst; 20410 20411 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 20412 20413 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20414 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20415 num_lso_seg); 20416 #ifndef _BIG_ENDIAN 20417 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20418 #endif 20419 if (tcp->tcp_snd_zcopy_aware) { 20420 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20421 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20422 mp = tcp_zcopy_disable(tcp, mp); 20423 } 20424 20425 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20426 ASSERT(ill->ill_hcksum_capab != NULL); 20427 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20428 } 20429 20430 /* 20431 * Since the TCP checksum should be recalculated by h/w, we can just 20432 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20433 * pseudo-header checksum for HCK_PARTIALCKSUM. 20434 * The partial pseudo-header excludes TCP length, that was calculated 20435 * in tcp_send(), so to zero *up before further processing. 20436 */ 20437 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20438 20439 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20440 *up = 0; 20441 20442 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20443 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20444 20445 /* 20446 * Append LSO flags and mss to the mp. 20447 */ 20448 lso_info_set(mp, mss, HW_LSO); 20449 20450 ipha->ipha_fragment_offset_and_flags |= 20451 (uint32_t)htons(ire->ire_frag_flag); 20452 20453 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20454 ire_fp_mp_len = MBLKL(ire_fp_mp); 20455 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20456 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20457 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20458 20459 UPDATE_OB_PKT_COUNT(ire); 20460 ire->ire_last_used_time = lbolt; 20461 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20462 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20463 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20464 ntohs(ipha->ipha_length)); 20465 20466 DTRACE_PROBE4(ip4__physical__out__start, 20467 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 20468 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20469 ipst->ips_ipv4firewall_physical_out, NULL, 20470 ill, ipha, mp, mp, 0, ipst); 20471 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20472 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 20473 20474 if (mp != NULL) { 20475 if (ipst->ips_ipobs_enabled) { 20476 zoneid_t szone; 20477 20478 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 20479 ipst, ALL_ZONES); 20480 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 20481 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 20482 } 20483 20484 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0, NULL); 20485 } 20486 } 20487 20488 /* 20489 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20490 * scheme, and returns one of the following: 20491 * 20492 * -1 = failed allocation. 20493 * 0 = success; burst count reached, or usable send window is too small, 20494 * and that we'd rather wait until later before sending again. 20495 * 1 = success; we are called from tcp_multisend(), and both usable send 20496 * window and tail_unsent are greater than the MDT threshold, and thus 20497 * Multidata Transmit should be used instead. 20498 */ 20499 static int 20500 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20501 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20502 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20503 const int mdt_thres) 20504 { 20505 int num_burst_seg = tcp->tcp_snd_burst; 20506 ire_t *ire = NULL; 20507 ill_t *ill = NULL; 20508 mblk_t *ire_fp_mp = NULL; 20509 uint_t ire_fp_mp_len = 0; 20510 int num_lso_seg = 1; 20511 uint_t lso_usable; 20512 boolean_t do_lso_send = B_FALSE; 20513 tcp_stack_t *tcps = tcp->tcp_tcps; 20514 20515 /* 20516 * Check LSO capability before any further work. And the similar check 20517 * need to be done in for(;;) loop. 20518 * LSO will be deployed when therer is more than one mss of available 20519 * data and a burst transmission is allowed. 20520 */ 20521 if (tcp->tcp_lso && 20522 (tcp->tcp_valid_bits == 0 || 20523 tcp->tcp_valid_bits == TCP_FSS_VALID) && 20524 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20525 /* 20526 * Try to find usable IRE/ILL and do basic check to the ILL. 20527 * Double check LSO usability before going further, since the 20528 * underlying interface could have been changed. In case of any 20529 * change of LSO capability, set tcp_ire_ill_check_done to 20530 * B_FALSE to force to check the ILL with the next send. 20531 */ 20532 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill) && 20533 tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 20534 /* 20535 * Enable LSO with this transmission. 20536 * Since IRE has been hold in tcp_send_find_ire_ill(), 20537 * IRE_REFRELE(ire) should be called before return. 20538 */ 20539 do_lso_send = B_TRUE; 20540 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20541 ire_fp_mp_len = MBLKL(ire_fp_mp); 20542 /* Round up to multiple of 4 */ 20543 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 20544 } else { 20545 tcp->tcp_lso = B_FALSE; 20546 tcp->tcp_ire_ill_check_done = B_FALSE; 20547 do_lso_send = B_FALSE; 20548 ill = NULL; 20549 } 20550 } 20551 20552 for (;;) { 20553 struct datab *db; 20554 tcph_t *tcph; 20555 uint32_t sum; 20556 mblk_t *mp, *mp1; 20557 uchar_t *rptr; 20558 int len; 20559 20560 /* 20561 * If we're called by tcp_multisend(), and the amount of 20562 * sendable data as well as the size of current xmit_tail 20563 * is beyond the MDT threshold, return to the caller and 20564 * let the large data transmit be done using MDT. 20565 */ 20566 if (*usable > 0 && *usable > mdt_thres && 20567 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 20568 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 20569 ASSERT(tcp->tcp_mdt); 20570 return (1); /* success; do large send */ 20571 } 20572 20573 if (num_burst_seg == 0) 20574 break; /* success; burst count reached */ 20575 20576 /* 20577 * Calculate the maximum payload length we can send in *one* 20578 * time. 20579 */ 20580 if (do_lso_send) { 20581 /* 20582 * Check whether need to do LSO any more. 20583 */ 20584 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20585 lso_usable = MIN(tcp->tcp_lso_max, *usable); 20586 lso_usable = MIN(lso_usable, 20587 num_burst_seg * mss); 20588 20589 num_lso_seg = lso_usable / mss; 20590 if (lso_usable % mss) { 20591 num_lso_seg++; 20592 tcp->tcp_last_sent_len = (ushort_t) 20593 (lso_usable % mss); 20594 } else { 20595 tcp->tcp_last_sent_len = (ushort_t)mss; 20596 } 20597 } else { 20598 do_lso_send = B_FALSE; 20599 num_lso_seg = 1; 20600 lso_usable = mss; 20601 } 20602 } 20603 20604 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 20605 20606 /* 20607 * Adjust num_burst_seg here. 20608 */ 20609 num_burst_seg -= num_lso_seg; 20610 20611 len = mss; 20612 if (len > *usable) { 20613 ASSERT(do_lso_send == B_FALSE); 20614 20615 len = *usable; 20616 if (len <= 0) { 20617 /* Terminate the loop */ 20618 break; /* success; too small */ 20619 } 20620 /* 20621 * Sender silly-window avoidance. 20622 * Ignore this if we are going to send a 20623 * zero window probe out. 20624 * 20625 * TODO: force data into microscopic window? 20626 * ==> (!pushed || (unsent > usable)) 20627 */ 20628 if (len < (tcp->tcp_max_swnd >> 1) && 20629 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 20630 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 20631 len == 1) && (! tcp->tcp_zero_win_probe)) { 20632 /* 20633 * If the retransmit timer is not running 20634 * we start it so that we will retransmit 20635 * in the case when the the receiver has 20636 * decremented the window. 20637 */ 20638 if (*snxt == tcp->tcp_snxt && 20639 *snxt == tcp->tcp_suna) { 20640 /* 20641 * We are not supposed to send 20642 * anything. So let's wait a little 20643 * bit longer before breaking SWS 20644 * avoidance. 20645 * 20646 * What should the value be? 20647 * Suggestion: MAX(init rexmit time, 20648 * tcp->tcp_rto) 20649 */ 20650 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20651 } 20652 break; /* success; too small */ 20653 } 20654 } 20655 20656 tcph = tcp->tcp_tcph; 20657 20658 /* 20659 * The reason to adjust len here is that we need to set flags 20660 * and calculate checksum. 20661 */ 20662 if (do_lso_send) 20663 len = lso_usable; 20664 20665 *usable -= len; /* Approximate - can be adjusted later */ 20666 if (*usable > 0) 20667 tcph->th_flags[0] = TH_ACK; 20668 else 20669 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 20670 20671 /* 20672 * Prime pump for IP's checksumming on our behalf 20673 * Include the adjustment for a source route if any. 20674 */ 20675 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20676 sum = (sum >> 16) + (sum & 0xFFFF); 20677 U16_TO_ABE16(sum, tcph->th_sum); 20678 20679 U32_TO_ABE32(*snxt, tcph->th_seq); 20680 20681 /* 20682 * Branch off to tcp_xmit_mp() if any of the VALID bits is 20683 * set. For the case when TCP_FSS_VALID is the only valid 20684 * bit (normal active close), branch off only when we think 20685 * that the FIN flag needs to be set. Note for this case, 20686 * that (snxt + len) may not reflect the actual seg_len, 20687 * as len may be further reduced in tcp_xmit_mp(). If len 20688 * gets modified, we will end up here again. 20689 */ 20690 if (tcp->tcp_valid_bits != 0 && 20691 (tcp->tcp_valid_bits != TCP_FSS_VALID || 20692 ((*snxt + len) == tcp->tcp_fss))) { 20693 uchar_t *prev_rptr; 20694 uint32_t prev_snxt = tcp->tcp_snxt; 20695 20696 if (*tail_unsent == 0) { 20697 ASSERT((*xmit_tail)->b_cont != NULL); 20698 *xmit_tail = (*xmit_tail)->b_cont; 20699 prev_rptr = (*xmit_tail)->b_rptr; 20700 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20701 (*xmit_tail)->b_rptr); 20702 } else { 20703 prev_rptr = (*xmit_tail)->b_rptr; 20704 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 20705 *tail_unsent; 20706 } 20707 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 20708 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20709 /* Restore tcp_snxt so we get amount sent right. */ 20710 tcp->tcp_snxt = prev_snxt; 20711 if (prev_rptr == (*xmit_tail)->b_rptr) { 20712 /* 20713 * If the previous timestamp is still in use, 20714 * don't stomp on it. 20715 */ 20716 if ((*xmit_tail)->b_next == NULL) { 20717 (*xmit_tail)->b_prev = local_time; 20718 (*xmit_tail)->b_next = 20719 (mblk_t *)(uintptr_t)(*snxt); 20720 } 20721 } else 20722 (*xmit_tail)->b_rptr = prev_rptr; 20723 20724 if (mp == NULL) { 20725 if (ire != NULL) 20726 IRE_REFRELE(ire); 20727 return (-1); 20728 } 20729 mp1 = mp->b_cont; 20730 20731 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20732 tcp->tcp_last_sent_len = (ushort_t)len; 20733 while (mp1->b_cont) { 20734 *xmit_tail = (*xmit_tail)->b_cont; 20735 (*xmit_tail)->b_prev = local_time; 20736 (*xmit_tail)->b_next = 20737 (mblk_t *)(uintptr_t)(*snxt); 20738 mp1 = mp1->b_cont; 20739 } 20740 *snxt += len; 20741 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20742 BUMP_LOCAL(tcp->tcp_obsegs); 20743 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20744 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20745 tcp_send_data(tcp, q, mp); 20746 continue; 20747 } 20748 20749 *snxt += len; /* Adjust later if we don't send all of len */ 20750 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20751 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20752 20753 if (*tail_unsent) { 20754 /* Are the bytes above us in flight? */ 20755 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20756 if (rptr != (*xmit_tail)->b_rptr) { 20757 *tail_unsent -= len; 20758 if (len <= mss) /* LSO is unusable */ 20759 tcp->tcp_last_sent_len = (ushort_t)len; 20760 len += tcp_hdr_len; 20761 if (tcp->tcp_ipversion == IPV4_VERSION) 20762 tcp->tcp_ipha->ipha_length = htons(len); 20763 else 20764 tcp->tcp_ip6h->ip6_plen = 20765 htons(len - 20766 ((char *)&tcp->tcp_ip6h[1] - 20767 tcp->tcp_iphc)); 20768 mp = dupb(*xmit_tail); 20769 if (mp == NULL) { 20770 if (ire != NULL) 20771 IRE_REFRELE(ire); 20772 return (-1); /* out_of_mem */ 20773 } 20774 mp->b_rptr = rptr; 20775 /* 20776 * If the old timestamp is no longer in use, 20777 * sample a new timestamp now. 20778 */ 20779 if ((*xmit_tail)->b_next == NULL) { 20780 (*xmit_tail)->b_prev = local_time; 20781 (*xmit_tail)->b_next = 20782 (mblk_t *)(uintptr_t)(*snxt-len); 20783 } 20784 goto must_alloc; 20785 } 20786 } else { 20787 *xmit_tail = (*xmit_tail)->b_cont; 20788 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 20789 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 20790 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20791 (*xmit_tail)->b_rptr); 20792 } 20793 20794 (*xmit_tail)->b_prev = local_time; 20795 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 20796 20797 *tail_unsent -= len; 20798 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20799 tcp->tcp_last_sent_len = (ushort_t)len; 20800 20801 len += tcp_hdr_len; 20802 if (tcp->tcp_ipversion == IPV4_VERSION) 20803 tcp->tcp_ipha->ipha_length = htons(len); 20804 else 20805 tcp->tcp_ip6h->ip6_plen = htons(len - 20806 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20807 20808 mp = dupb(*xmit_tail); 20809 if (mp == NULL) { 20810 if (ire != NULL) 20811 IRE_REFRELE(ire); 20812 return (-1); /* out_of_mem */ 20813 } 20814 20815 len = tcp_hdr_len; 20816 /* 20817 * There are four reasons to allocate a new hdr mblk: 20818 * 1) The bytes above us are in use by another packet 20819 * 2) We don't have good alignment 20820 * 3) The mblk is being shared 20821 * 4) We don't have enough room for a header 20822 */ 20823 rptr = mp->b_rptr - len; 20824 if (!OK_32PTR(rptr) || 20825 ((db = mp->b_datap), db->db_ref != 2) || 20826 rptr < db->db_base + ire_fp_mp_len) { 20827 /* NOTE: we assume allocb returns an OK_32PTR */ 20828 20829 must_alloc:; 20830 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 20831 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 20832 if (mp1 == NULL) { 20833 freemsg(mp); 20834 if (ire != NULL) 20835 IRE_REFRELE(ire); 20836 return (-1); /* out_of_mem */ 20837 } 20838 mp1->b_cont = mp; 20839 mp = mp1; 20840 /* Leave room for Link Level header */ 20841 len = tcp_hdr_len; 20842 rptr = 20843 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 20844 mp->b_wptr = &rptr[len]; 20845 } 20846 20847 /* 20848 * Fill in the header using the template header, and add 20849 * options such as time-stamp, ECN and/or SACK, as needed. 20850 */ 20851 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 20852 20853 mp->b_rptr = rptr; 20854 20855 if (*tail_unsent) { 20856 int spill = *tail_unsent; 20857 20858 mp1 = mp->b_cont; 20859 if (mp1 == NULL) 20860 mp1 = mp; 20861 20862 /* 20863 * If we're a little short, tack on more mblks until 20864 * there is no more spillover. 20865 */ 20866 while (spill < 0) { 20867 mblk_t *nmp; 20868 int nmpsz; 20869 20870 nmp = (*xmit_tail)->b_cont; 20871 nmpsz = MBLKL(nmp); 20872 20873 /* 20874 * Excess data in mblk; can we split it? 20875 * If MDT is enabled for the connection, 20876 * keep on splitting as this is a transient 20877 * send path. 20878 */ 20879 if (!do_lso_send && !tcp->tcp_mdt && 20880 (spill + nmpsz > 0)) { 20881 /* 20882 * Don't split if stream head was 20883 * told to break up larger writes 20884 * into smaller ones. 20885 */ 20886 if (tcp->tcp_maxpsz > 0) 20887 break; 20888 20889 /* 20890 * Next mblk is less than SMSS/2 20891 * rounded up to nearest 64-byte; 20892 * let it get sent as part of the 20893 * next segment. 20894 */ 20895 if (tcp->tcp_localnet && 20896 !tcp->tcp_cork && 20897 (nmpsz < roundup((mss >> 1), 64))) 20898 break; 20899 } 20900 20901 *xmit_tail = nmp; 20902 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 20903 /* Stash for rtt use later */ 20904 (*xmit_tail)->b_prev = local_time; 20905 (*xmit_tail)->b_next = 20906 (mblk_t *)(uintptr_t)(*snxt - len); 20907 mp1->b_cont = dupb(*xmit_tail); 20908 mp1 = mp1->b_cont; 20909 20910 spill += nmpsz; 20911 if (mp1 == NULL) { 20912 *tail_unsent = spill; 20913 freemsg(mp); 20914 if (ire != NULL) 20915 IRE_REFRELE(ire); 20916 return (-1); /* out_of_mem */ 20917 } 20918 } 20919 20920 /* Trim back any surplus on the last mblk */ 20921 if (spill >= 0) { 20922 mp1->b_wptr -= spill; 20923 *tail_unsent = spill; 20924 } else { 20925 /* 20926 * We did not send everything we could in 20927 * order to remain within the b_cont limit. 20928 */ 20929 *usable -= spill; 20930 *snxt += spill; 20931 tcp->tcp_last_sent_len += spill; 20932 UPDATE_MIB(&tcps->tcps_mib, 20933 tcpOutDataBytes, spill); 20934 /* 20935 * Adjust the checksum 20936 */ 20937 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 20938 sum += spill; 20939 sum = (sum >> 16) + (sum & 0xFFFF); 20940 U16_TO_ABE16(sum, tcph->th_sum); 20941 if (tcp->tcp_ipversion == IPV4_VERSION) { 20942 sum = ntohs( 20943 ((ipha_t *)rptr)->ipha_length) + 20944 spill; 20945 ((ipha_t *)rptr)->ipha_length = 20946 htons(sum); 20947 } else { 20948 sum = ntohs( 20949 ((ip6_t *)rptr)->ip6_plen) + 20950 spill; 20951 ((ip6_t *)rptr)->ip6_plen = 20952 htons(sum); 20953 } 20954 *tail_unsent = 0; 20955 } 20956 } 20957 if (tcp->tcp_ip_forward_progress) { 20958 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20959 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 20960 tcp->tcp_ip_forward_progress = B_FALSE; 20961 } 20962 20963 if (do_lso_send) { 20964 tcp_lsosend_data(tcp, mp, ire, ill, mss, 20965 num_lso_seg); 20966 tcp->tcp_obsegs += num_lso_seg; 20967 20968 TCP_STAT(tcps, tcp_lso_times); 20969 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 20970 } else { 20971 tcp_send_data(tcp, q, mp); 20972 BUMP_LOCAL(tcp->tcp_obsegs); 20973 } 20974 } 20975 20976 if (ire != NULL) 20977 IRE_REFRELE(ire); 20978 return (0); 20979 } 20980 20981 /* Unlink and return any mblk that looks like it contains a MDT info */ 20982 static mblk_t * 20983 tcp_mdt_info_mp(mblk_t *mp) 20984 { 20985 mblk_t *prev_mp; 20986 20987 for (;;) { 20988 prev_mp = mp; 20989 /* no more to process? */ 20990 if ((mp = mp->b_cont) == NULL) 20991 break; 20992 20993 switch (DB_TYPE(mp)) { 20994 case M_CTL: 20995 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 20996 continue; 20997 ASSERT(prev_mp != NULL); 20998 prev_mp->b_cont = mp->b_cont; 20999 mp->b_cont = NULL; 21000 return (mp); 21001 default: 21002 break; 21003 } 21004 } 21005 return (mp); 21006 } 21007 21008 /* MDT info update routine, called when IP notifies us about MDT */ 21009 static void 21010 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21011 { 21012 boolean_t prev_state; 21013 tcp_stack_t *tcps = tcp->tcp_tcps; 21014 21015 /* 21016 * IP is telling us to abort MDT on this connection? We know 21017 * this because the capability is only turned off when IP 21018 * encounters some pathological cases, e.g. link-layer change 21019 * where the new driver doesn't support MDT, or in situation 21020 * where MDT usage on the link-layer has been switched off. 21021 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21022 * if the link-layer doesn't support MDT, and if it does, it 21023 * will indicate that the feature is to be turned on. 21024 */ 21025 prev_state = tcp->tcp_mdt; 21026 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21027 if (!tcp->tcp_mdt && !first) { 21028 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21029 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21030 (void *)tcp->tcp_connp)); 21031 } 21032 21033 /* 21034 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21035 * so disable MDT otherwise. The checks are done here 21036 * and in tcp_wput_data(). 21037 */ 21038 if (tcp->tcp_mdt && 21039 (tcp->tcp_ipversion == IPV4_VERSION && 21040 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21041 (tcp->tcp_ipversion == IPV6_VERSION && 21042 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21043 tcp->tcp_mdt = B_FALSE; 21044 21045 if (tcp->tcp_mdt) { 21046 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21047 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21048 "version (%d), expected version is %d", 21049 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21050 tcp->tcp_mdt = B_FALSE; 21051 return; 21052 } 21053 21054 /* 21055 * We need the driver to be able to handle at least three 21056 * spans per packet in order for tcp MDT to be utilized. 21057 * The first is for the header portion, while the rest are 21058 * needed to handle a packet that straddles across two 21059 * virtually non-contiguous buffers; a typical tcp packet 21060 * therefore consists of only two spans. Note that we take 21061 * a zero as "don't care". 21062 */ 21063 if (mdt_capab->ill_mdt_span_limit > 0 && 21064 mdt_capab->ill_mdt_span_limit < 3) { 21065 tcp->tcp_mdt = B_FALSE; 21066 return; 21067 } 21068 21069 /* a zero means driver wants default value */ 21070 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21071 tcps->tcps_mdt_max_pbufs); 21072 if (tcp->tcp_mdt_max_pld == 0) 21073 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21074 21075 /* ensure 32-bit alignment */ 21076 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21077 mdt_capab->ill_mdt_hdr_head), 4); 21078 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21079 mdt_capab->ill_mdt_hdr_tail), 4); 21080 21081 if (!first && !prev_state) { 21082 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21083 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21084 (void *)tcp->tcp_connp)); 21085 } 21086 } 21087 } 21088 21089 /* Unlink and return any mblk that looks like it contains a LSO info */ 21090 static mblk_t * 21091 tcp_lso_info_mp(mblk_t *mp) 21092 { 21093 mblk_t *prev_mp; 21094 21095 for (;;) { 21096 prev_mp = mp; 21097 /* no more to process? */ 21098 if ((mp = mp->b_cont) == NULL) 21099 break; 21100 21101 switch (DB_TYPE(mp)) { 21102 case M_CTL: 21103 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21104 continue; 21105 ASSERT(prev_mp != NULL); 21106 prev_mp->b_cont = mp->b_cont; 21107 mp->b_cont = NULL; 21108 return (mp); 21109 default: 21110 break; 21111 } 21112 } 21113 21114 return (mp); 21115 } 21116 21117 /* LSO info update routine, called when IP notifies us about LSO */ 21118 static void 21119 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21120 { 21121 tcp_stack_t *tcps = tcp->tcp_tcps; 21122 21123 /* 21124 * IP is telling us to abort LSO on this connection? We know 21125 * this because the capability is only turned off when IP 21126 * encounters some pathological cases, e.g. link-layer change 21127 * where the new NIC/driver doesn't support LSO, or in situation 21128 * where LSO usage on the link-layer has been switched off. 21129 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21130 * if the link-layer doesn't support LSO, and if it does, it 21131 * will indicate that the feature is to be turned on. 21132 */ 21133 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21134 TCP_STAT(tcps, tcp_lso_enabled); 21135 21136 /* 21137 * We currently only support LSO on simple TCP/IPv4, 21138 * so disable LSO otherwise. The checks are done here 21139 * and in tcp_wput_data(). 21140 */ 21141 if (tcp->tcp_lso && 21142 (tcp->tcp_ipversion == IPV4_VERSION && 21143 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21144 (tcp->tcp_ipversion == IPV6_VERSION)) { 21145 tcp->tcp_lso = B_FALSE; 21146 TCP_STAT(tcps, tcp_lso_disabled); 21147 } else { 21148 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21149 lso_capab->ill_lso_max); 21150 } 21151 } 21152 21153 static void 21154 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21155 { 21156 conn_t *connp = tcp->tcp_connp; 21157 tcp_stack_t *tcps = tcp->tcp_tcps; 21158 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21159 21160 ASSERT(ire != NULL); 21161 21162 /* 21163 * We may be in the fastpath here, and although we essentially do 21164 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21165 * we try to keep things as brief as possible. After all, these 21166 * are only best-effort checks, and we do more thorough ones prior 21167 * to calling tcp_send()/tcp_multisend(). 21168 */ 21169 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21170 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21171 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21172 !(ire->ire_flags & RTF_MULTIRT) && 21173 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21174 CONN_IS_LSO_MD_FASTPATH(connp)) { 21175 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21176 /* Cache the result */ 21177 connp->conn_lso_ok = B_TRUE; 21178 21179 ASSERT(ill->ill_lso_capab != NULL); 21180 if (!ill->ill_lso_capab->ill_lso_on) { 21181 ill->ill_lso_capab->ill_lso_on = 1; 21182 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21183 "LSO for interface %s\n", (void *)connp, 21184 ill->ill_name)); 21185 } 21186 tcp_lso_update(tcp, ill->ill_lso_capab); 21187 } else if (ipst->ips_ip_multidata_outbound && 21188 ILL_MDT_CAPABLE(ill)) { 21189 /* Cache the result */ 21190 connp->conn_mdt_ok = B_TRUE; 21191 21192 ASSERT(ill->ill_mdt_capab != NULL); 21193 if (!ill->ill_mdt_capab->ill_mdt_on) { 21194 ill->ill_mdt_capab->ill_mdt_on = 1; 21195 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21196 "MDT for interface %s\n", (void *)connp, 21197 ill->ill_name)); 21198 } 21199 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21200 } 21201 } 21202 21203 /* 21204 * The goal is to reduce the number of generated tcp segments by 21205 * setting the maxpsz multiplier to 0; this will have an affect on 21206 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21207 * into each packet, up to SMSS bytes. Doing this reduces the number 21208 * of outbound segments and incoming ACKs, thus allowing for better 21209 * network and system performance. In contrast the legacy behavior 21210 * may result in sending less than SMSS size, because the last mblk 21211 * for some packets may have more data than needed to make up SMSS, 21212 * and the legacy code refused to "split" it. 21213 * 21214 * We apply the new behavior on following situations: 21215 * 21216 * 1) Loopback connections, 21217 * 2) Connections in which the remote peer is not on local subnet, 21218 * 3) Local subnet connections over the bge interface (see below). 21219 * 21220 * Ideally, we would like this behavior to apply for interfaces other 21221 * than bge. However, doing so would negatively impact drivers which 21222 * perform dynamic mapping and unmapping of DMA resources, which are 21223 * increased by setting the maxpsz multiplier to 0 (more mblks per 21224 * packet will be generated by tcp). The bge driver does not suffer 21225 * from this, as it copies the mblks into pre-mapped buffers, and 21226 * therefore does not require more I/O resources than before. 21227 * 21228 * Otherwise, this behavior is present on all network interfaces when 21229 * the destination endpoint is non-local, since reducing the number 21230 * of packets in general is good for the network. 21231 * 21232 * TODO We need to remove this hard-coded conditional for bge once 21233 * a better "self-tuning" mechanism, or a way to comprehend 21234 * the driver transmit strategy is devised. Until the solution 21235 * is found and well understood, we live with this hack. 21236 */ 21237 if (!tcp_static_maxpsz && 21238 (tcp->tcp_loopback || !tcp->tcp_localnet || 21239 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21240 /* override the default value */ 21241 tcp->tcp_maxpsz = 0; 21242 21243 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21244 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21245 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21246 } 21247 21248 /* set the stream head parameters accordingly */ 21249 (void) tcp_maxpsz_set(tcp, B_TRUE); 21250 } 21251 21252 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21253 static void 21254 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21255 { 21256 uchar_t fval = *mp->b_rptr; 21257 mblk_t *tail; 21258 queue_t *q = tcp->tcp_wq; 21259 21260 /* TODO: How should flush interact with urgent data? */ 21261 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21262 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21263 /* 21264 * Flush only data that has not yet been put on the wire. If 21265 * we flush data that we have already transmitted, life, as we 21266 * know it, may come to an end. 21267 */ 21268 tail = tcp->tcp_xmit_tail; 21269 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21270 tcp->tcp_xmit_tail_unsent = 0; 21271 tcp->tcp_unsent = 0; 21272 if (tail->b_wptr != tail->b_rptr) 21273 tail = tail->b_cont; 21274 if (tail) { 21275 mblk_t **excess = &tcp->tcp_xmit_head; 21276 for (;;) { 21277 mblk_t *mp1 = *excess; 21278 if (mp1 == tail) 21279 break; 21280 tcp->tcp_xmit_tail = mp1; 21281 tcp->tcp_xmit_last = mp1; 21282 excess = &mp1->b_cont; 21283 } 21284 *excess = NULL; 21285 tcp_close_mpp(&tail); 21286 if (tcp->tcp_snd_zcopy_aware) 21287 tcp_zcopy_notify(tcp); 21288 } 21289 /* 21290 * We have no unsent data, so unsent must be less than 21291 * tcp_xmit_lowater, so re-enable flow. 21292 */ 21293 mutex_enter(&tcp->tcp_non_sq_lock); 21294 if (tcp->tcp_flow_stopped) { 21295 tcp_clrqfull(tcp); 21296 } 21297 mutex_exit(&tcp->tcp_non_sq_lock); 21298 } 21299 /* 21300 * TODO: you can't just flush these, you have to increase rwnd for one 21301 * thing. For another, how should urgent data interact? 21302 */ 21303 if (fval & FLUSHR) { 21304 *mp->b_rptr = fval & ~FLUSHW; 21305 /* XXX */ 21306 qreply(q, mp); 21307 return; 21308 } 21309 freemsg(mp); 21310 } 21311 21312 /* 21313 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21314 * messages. 21315 */ 21316 static void 21317 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21318 { 21319 mblk_t *mp1; 21320 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 21321 STRUCT_HANDLE(strbuf, sb); 21322 queue_t *q = tcp->tcp_wq; 21323 int error; 21324 uint_t addrlen; 21325 21326 /* Make sure it is one of ours. */ 21327 switch (iocp->ioc_cmd) { 21328 case TI_GETMYNAME: 21329 case TI_GETPEERNAME: 21330 break; 21331 default: 21332 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21333 return; 21334 } 21335 switch (mi_copy_state(q, mp, &mp1)) { 21336 case -1: 21337 return; 21338 case MI_COPY_CASE(MI_COPY_IN, 1): 21339 break; 21340 case MI_COPY_CASE(MI_COPY_OUT, 1): 21341 /* Copy out the strbuf. */ 21342 mi_copyout(q, mp); 21343 return; 21344 case MI_COPY_CASE(MI_COPY_OUT, 2): 21345 /* All done. */ 21346 mi_copy_done(q, mp, 0); 21347 return; 21348 default: 21349 mi_copy_done(q, mp, EPROTO); 21350 return; 21351 } 21352 /* Check alignment of the strbuf */ 21353 if (!OK_32PTR(mp1->b_rptr)) { 21354 mi_copy_done(q, mp, EINVAL); 21355 return; 21356 } 21357 21358 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 21359 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21360 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21361 mi_copy_done(q, mp, EINVAL); 21362 return; 21363 } 21364 21365 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21366 if (mp1 == NULL) 21367 return; 21368 21369 switch (iocp->ioc_cmd) { 21370 case TI_GETMYNAME: 21371 error = tcp_do_getsockname(tcp, (void *)mp1->b_rptr, &addrlen); 21372 break; 21373 case TI_GETPEERNAME: 21374 error = tcp_do_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 21375 break; 21376 } 21377 21378 if (error != 0) { 21379 mi_copy_done(q, mp, error); 21380 } else { 21381 mp1->b_wptr += addrlen; 21382 STRUCT_FSET(sb, len, addrlen); 21383 21384 /* Copy out the address */ 21385 mi_copyout(q, mp); 21386 } 21387 } 21388 21389 static void 21390 tcp_disable_direct_sockfs(tcp_t *tcp) 21391 { 21392 #ifdef _ILP32 21393 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 21394 #else 21395 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21396 #endif 21397 /* 21398 * Insert this socket into the acceptor hash. 21399 * We might need it for T_CONN_RES message 21400 */ 21401 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21402 21403 if (tcp->tcp_fused) { 21404 /* 21405 * This is a fused loopback tcp; disable 21406 * read-side synchronous streams interface 21407 * and drain any queued data. It is okay 21408 * to do this for non-synchronous streams 21409 * fused tcp as well. 21410 */ 21411 tcp_fuse_disable_pair(tcp, B_FALSE); 21412 } 21413 tcp->tcp_issocket = B_FALSE; 21414 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 21415 } 21416 21417 /* 21418 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21419 * messages. 21420 */ 21421 /* ARGSUSED */ 21422 static void 21423 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21424 { 21425 conn_t *connp = (conn_t *)arg; 21426 tcp_t *tcp = connp->conn_tcp; 21427 queue_t *q = tcp->tcp_wq; 21428 struct iocblk *iocp; 21429 21430 ASSERT(DB_TYPE(mp) == M_IOCTL); 21431 /* 21432 * Try and ASSERT the minimum possible references on the 21433 * conn early enough. Since we are executing on write side, 21434 * the connection is obviously not detached and that means 21435 * there is a ref each for TCP and IP. Since we are behind 21436 * the squeue, the minimum references needed are 3. If the 21437 * conn is in classifier hash list, there should be an 21438 * extra ref for that (we check both the possibilities). 21439 */ 21440 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21441 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21442 21443 iocp = (struct iocblk *)mp->b_rptr; 21444 switch (iocp->ioc_cmd) { 21445 case TCP_IOC_DEFAULT_Q: 21446 /* Wants to be the default wq. */ 21447 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21448 iocp->ioc_error = EPERM; 21449 iocp->ioc_count = 0; 21450 mp->b_datap->db_type = M_IOCACK; 21451 qreply(q, mp); 21452 return; 21453 } 21454 tcp_def_q_set(tcp, mp); 21455 return; 21456 case _SIOCSOCKFALLBACK: 21457 /* 21458 * Either sockmod is about to be popped and the socket 21459 * would now be treated as a plain stream, or a module 21460 * is about to be pushed so we could no longer use read- 21461 * side synchronous streams for fused loopback tcp. 21462 * Drain any queued data and disable direct sockfs 21463 * interface from now on. 21464 */ 21465 if (!tcp->tcp_issocket) { 21466 DB_TYPE(mp) = M_IOCNAK; 21467 iocp->ioc_error = EINVAL; 21468 } else { 21469 tcp_disable_direct_sockfs(tcp); 21470 DB_TYPE(mp) = M_IOCACK; 21471 iocp->ioc_error = 0; 21472 } 21473 iocp->ioc_count = 0; 21474 iocp->ioc_rval = 0; 21475 qreply(q, mp); 21476 return; 21477 } 21478 CALL_IP_WPUT(connp, q, mp); 21479 } 21480 21481 /* 21482 * This routine is called by tcp_wput() to handle all TPI requests. 21483 */ 21484 /* ARGSUSED */ 21485 static void 21486 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21487 { 21488 conn_t *connp = (conn_t *)arg; 21489 tcp_t *tcp = connp->conn_tcp; 21490 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21491 uchar_t *rptr; 21492 t_scalar_t type; 21493 cred_t *cr; 21494 21495 /* 21496 * Try and ASSERT the minimum possible references on the 21497 * conn early enough. Since we are executing on write side, 21498 * the connection is obviously not detached and that means 21499 * there is a ref each for TCP and IP. Since we are behind 21500 * the squeue, the minimum references needed are 3. If the 21501 * conn is in classifier hash list, there should be an 21502 * extra ref for that (we check both the possibilities). 21503 */ 21504 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21505 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21506 21507 rptr = mp->b_rptr; 21508 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21509 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 21510 type = ((union T_primitives *)rptr)->type; 21511 if (type == T_EXDATA_REQ) { 21512 tcp_output_urgent(connp, mp->b_cont, arg2); 21513 freeb(mp); 21514 } else if (type != T_DATA_REQ) { 21515 goto non_urgent_data; 21516 } else { 21517 /* TODO: options, flags, ... from user */ 21518 /* Set length to zero for reclamation below */ 21519 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 21520 freeb(mp); 21521 } 21522 return; 21523 } else { 21524 if (tcp->tcp_debug) { 21525 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21526 "tcp_wput_proto, dropping one..."); 21527 } 21528 freemsg(mp); 21529 return; 21530 } 21531 21532 non_urgent_data: 21533 21534 switch ((int)tprim->type) { 21535 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 21536 /* 21537 * save the kssl_ent_t from the next block, and convert this 21538 * back to a normal bind_req. 21539 */ 21540 if (mp->b_cont != NULL) { 21541 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 21542 21543 if (tcp->tcp_kssl_ent != NULL) { 21544 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 21545 KSSL_NO_PROXY); 21546 tcp->tcp_kssl_ent = NULL; 21547 } 21548 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 21549 sizeof (kssl_ent_t)); 21550 kssl_hold_ent(tcp->tcp_kssl_ent); 21551 freemsg(mp->b_cont); 21552 mp->b_cont = NULL; 21553 } 21554 tprim->type = T_BIND_REQ; 21555 21556 /* FALLTHROUGH */ 21557 case O_T_BIND_REQ: /* bind request */ 21558 case T_BIND_REQ: /* new semantics bind request */ 21559 tcp_tpi_bind(tcp, mp); 21560 break; 21561 case T_UNBIND_REQ: /* unbind request */ 21562 tcp_tpi_unbind(tcp, mp); 21563 break; 21564 case O_T_CONN_RES: /* old connection response XXX */ 21565 case T_CONN_RES: /* connection response */ 21566 tcp_tli_accept(tcp, mp); 21567 break; 21568 case T_CONN_REQ: /* connection request */ 21569 tcp_tpi_connect(tcp, mp); 21570 break; 21571 case T_DISCON_REQ: /* disconnect request */ 21572 tcp_disconnect(tcp, mp); 21573 break; 21574 case T_CAPABILITY_REQ: 21575 tcp_capability_req(tcp, mp); /* capability request */ 21576 break; 21577 case T_INFO_REQ: /* information request */ 21578 tcp_info_req(tcp, mp); 21579 break; 21580 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 21581 case T_OPTMGMT_REQ: 21582 /* 21583 * Note: no support for snmpcom_req() through new 21584 * T_OPTMGMT_REQ. See comments in ip.c 21585 */ 21586 21587 /* 21588 * All Solaris components should pass a db_credp 21589 * for this TPI message, hence we ASSERT. 21590 * But in case there is some other M_PROTO that looks 21591 * like a TPI message sent by some other kernel 21592 * component, we check and return an error. 21593 */ 21594 cr = msg_getcred(mp, NULL); 21595 ASSERT(cr != NULL); 21596 if (cr == NULL) { 21597 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 21598 return; 21599 } 21600 /* 21601 * If EINPROGRESS is returned, the request has been queued 21602 * for subsequent processing by ip_restart_optmgmt(), which 21603 * will do the CONN_DEC_REF(). 21604 */ 21605 CONN_INC_REF(connp); 21606 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) { 21607 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21608 B_TRUE) != EINPROGRESS) { 21609 CONN_DEC_REF(connp); 21610 } 21611 } else { 21612 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21613 B_TRUE) != EINPROGRESS) { 21614 CONN_DEC_REF(connp); 21615 } 21616 } 21617 break; 21618 21619 case T_UNITDATA_REQ: /* unitdata request */ 21620 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21621 break; 21622 case T_ORDREL_REQ: /* orderly release req */ 21623 freemsg(mp); 21624 21625 if (tcp->tcp_fused) 21626 tcp_unfuse(tcp); 21627 21628 if (tcp_xmit_end(tcp) != 0) { 21629 /* 21630 * We were crossing FINs and got a reset from 21631 * the other side. Just ignore it. 21632 */ 21633 if (tcp->tcp_debug) { 21634 (void) strlog(TCP_MOD_ID, 0, 1, 21635 SL_ERROR|SL_TRACE, 21636 "tcp_wput_proto, T_ORDREL_REQ out of " 21637 "state %s", 21638 tcp_display(tcp, NULL, 21639 DISP_ADDR_AND_PORT)); 21640 } 21641 } 21642 break; 21643 case T_ADDR_REQ: 21644 tcp_addr_req(tcp, mp); 21645 break; 21646 default: 21647 if (tcp->tcp_debug) { 21648 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21649 "tcp_wput_proto, bogus TPI msg, type %d", 21650 tprim->type); 21651 } 21652 /* 21653 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 21654 * to recover. 21655 */ 21656 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21657 break; 21658 } 21659 } 21660 21661 /* 21662 * The TCP write service routine should never be called... 21663 */ 21664 /* ARGSUSED */ 21665 static void 21666 tcp_wsrv(queue_t *q) 21667 { 21668 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 21669 21670 TCP_STAT(tcps, tcp_wsrv_called); 21671 } 21672 21673 /* Non overlapping byte exchanger */ 21674 static void 21675 tcp_xchg(uchar_t *a, uchar_t *b, int len) 21676 { 21677 uchar_t uch; 21678 21679 while (len-- > 0) { 21680 uch = a[len]; 21681 a[len] = b[len]; 21682 b[len] = uch; 21683 } 21684 } 21685 21686 /* 21687 * Send out a control packet on the tcp connection specified. This routine 21688 * is typically called where we need a simple ACK or RST generated. 21689 */ 21690 static void 21691 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 21692 { 21693 uchar_t *rptr; 21694 tcph_t *tcph; 21695 ipha_t *ipha = NULL; 21696 ip6_t *ip6h = NULL; 21697 uint32_t sum; 21698 int tcp_hdr_len; 21699 int tcp_ip_hdr_len; 21700 mblk_t *mp; 21701 tcp_stack_t *tcps = tcp->tcp_tcps; 21702 21703 /* 21704 * Save sum for use in source route later. 21705 */ 21706 ASSERT(tcp != NULL); 21707 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 21708 tcp_hdr_len = tcp->tcp_hdr_len; 21709 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 21710 21711 /* If a text string is passed in with the request, pass it to strlog. */ 21712 if (str != NULL && tcp->tcp_debug) { 21713 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21714 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 21715 str, seq, ack, ctl); 21716 } 21717 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 21718 BPRI_MED); 21719 if (mp == NULL) { 21720 return; 21721 } 21722 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 21723 mp->b_rptr = rptr; 21724 mp->b_wptr = &rptr[tcp_hdr_len]; 21725 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 21726 21727 if (tcp->tcp_ipversion == IPV4_VERSION) { 21728 ipha = (ipha_t *)rptr; 21729 ipha->ipha_length = htons(tcp_hdr_len); 21730 } else { 21731 ip6h = (ip6_t *)rptr; 21732 ASSERT(tcp != NULL); 21733 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 21734 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21735 } 21736 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 21737 tcph->th_flags[0] = (uint8_t)ctl; 21738 if (ctl & TH_RST) { 21739 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 21740 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 21741 /* 21742 * Don't send TSopt w/ TH_RST packets per RFC 1323. 21743 */ 21744 if (tcp->tcp_snd_ts_ok && 21745 tcp->tcp_state > TCPS_SYN_SENT) { 21746 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 21747 *(mp->b_wptr) = TCPOPT_EOL; 21748 if (tcp->tcp_ipversion == IPV4_VERSION) { 21749 ipha->ipha_length = htons(tcp_hdr_len - 21750 TCPOPT_REAL_TS_LEN); 21751 } else { 21752 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 21753 TCPOPT_REAL_TS_LEN); 21754 } 21755 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 21756 sum -= TCPOPT_REAL_TS_LEN; 21757 } 21758 } 21759 if (ctl & TH_ACK) { 21760 if (tcp->tcp_snd_ts_ok) { 21761 U32_TO_BE32(lbolt, 21762 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 21763 U32_TO_BE32(tcp->tcp_ts_recent, 21764 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 21765 } 21766 21767 /* Update the latest receive window size in TCP header. */ 21768 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21769 tcph->th_win); 21770 tcp->tcp_rack = ack; 21771 tcp->tcp_rack_cnt = 0; 21772 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 21773 } 21774 BUMP_LOCAL(tcp->tcp_obsegs); 21775 U32_TO_BE32(seq, tcph->th_seq); 21776 U32_TO_BE32(ack, tcph->th_ack); 21777 /* 21778 * Include the adjustment for a source route if any. 21779 */ 21780 sum = (sum >> 16) + (sum & 0xFFFF); 21781 U16_TO_BE16(sum, tcph->th_sum); 21782 tcp_send_data(tcp, tcp->tcp_wq, mp); 21783 } 21784 21785 /* 21786 * If this routine returns B_TRUE, TCP can generate a RST in response 21787 * to a segment. If it returns B_FALSE, TCP should not respond. 21788 */ 21789 static boolean_t 21790 tcp_send_rst_chk(tcp_stack_t *tcps) 21791 { 21792 clock_t now; 21793 21794 /* 21795 * TCP needs to protect itself from generating too many RSTs. 21796 * This can be a DoS attack by sending us random segments 21797 * soliciting RSTs. 21798 * 21799 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 21800 * in each 1 second interval. In this way, TCP still generate 21801 * RSTs in normal cases but when under attack, the impact is 21802 * limited. 21803 */ 21804 if (tcps->tcps_rst_sent_rate_enabled != 0) { 21805 now = lbolt; 21806 /* lbolt can wrap around. */ 21807 if ((tcps->tcps_last_rst_intrvl > now) || 21808 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 21809 1*SECONDS)) { 21810 tcps->tcps_last_rst_intrvl = now; 21811 tcps->tcps_rst_cnt = 1; 21812 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 21813 return (B_FALSE); 21814 } 21815 } 21816 return (B_TRUE); 21817 } 21818 21819 /* 21820 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 21821 */ 21822 static void 21823 tcp_ip_ire_mark_advice(tcp_t *tcp) 21824 { 21825 mblk_t *mp; 21826 ipic_t *ipic; 21827 21828 if (tcp->tcp_ipversion == IPV4_VERSION) { 21829 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21830 &ipic); 21831 } else { 21832 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21833 &ipic); 21834 } 21835 if (mp == NULL) 21836 return; 21837 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21838 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21839 } 21840 21841 /* 21842 * Return an IP advice ioctl mblk and set ipic to be the pointer 21843 * to the advice structure. 21844 */ 21845 static mblk_t * 21846 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 21847 { 21848 struct iocblk *ioc; 21849 mblk_t *mp, *mp1; 21850 21851 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 21852 if (mp == NULL) 21853 return (NULL); 21854 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 21855 *ipic = (ipic_t *)mp->b_rptr; 21856 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 21857 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 21858 21859 bcopy(addr, *ipic + 1, addr_len); 21860 21861 (*ipic)->ipic_addr_length = addr_len; 21862 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 21863 21864 mp1 = mkiocb(IP_IOCTL); 21865 if (mp1 == NULL) { 21866 freemsg(mp); 21867 return (NULL); 21868 } 21869 mp1->b_cont = mp; 21870 ioc = (struct iocblk *)mp1->b_rptr; 21871 ioc->ioc_count = sizeof (ipic_t) + addr_len; 21872 21873 return (mp1); 21874 } 21875 21876 /* 21877 * Generate a reset based on an inbound packet, connp is set by caller 21878 * when RST is in response to an unexpected inbound packet for which 21879 * there is active tcp state in the system. 21880 * 21881 * IPSEC NOTE : Try to send the reply with the same protection as it came 21882 * in. We still have the ipsec_mp that the packet was attached to. Thus 21883 * the packet will go out at the same level of protection as it came in by 21884 * converting the IPSEC_IN to IPSEC_OUT. 21885 */ 21886 static void 21887 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 21888 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 21889 tcp_stack_t *tcps, conn_t *connp) 21890 { 21891 ipha_t *ipha = NULL; 21892 ip6_t *ip6h = NULL; 21893 ushort_t len; 21894 tcph_t *tcph; 21895 int i; 21896 mblk_t *ipsec_mp; 21897 boolean_t mctl_present; 21898 ipic_t *ipic; 21899 ipaddr_t v4addr; 21900 in6_addr_t v6addr; 21901 int addr_len; 21902 void *addr; 21903 queue_t *q = tcps->tcps_g_q; 21904 tcp_t *tcp; 21905 cred_t *cr; 21906 mblk_t *nmp; 21907 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21908 21909 if (tcps->tcps_g_q == NULL) { 21910 /* 21911 * For non-zero stackids the default queue isn't created 21912 * until the first open, thus there can be a need to send 21913 * a reset before then. But we can't do that, hence we just 21914 * drop the packet. Later during boot, when the default queue 21915 * has been setup, a retransmitted packet from the peer 21916 * will result in a reset. 21917 */ 21918 ASSERT(tcps->tcps_netstack->netstack_stackid != 21919 GLOBAL_NETSTACKID); 21920 freemsg(mp); 21921 return; 21922 } 21923 21924 if (connp != NULL) 21925 tcp = connp->conn_tcp; 21926 else 21927 tcp = Q_TO_TCP(q); 21928 21929 if (!tcp_send_rst_chk(tcps)) { 21930 tcps->tcps_rst_unsent++; 21931 freemsg(mp); 21932 return; 21933 } 21934 21935 if (mp->b_datap->db_type == M_CTL) { 21936 ipsec_mp = mp; 21937 mp = mp->b_cont; 21938 mctl_present = B_TRUE; 21939 } else { 21940 ipsec_mp = mp; 21941 mctl_present = B_FALSE; 21942 } 21943 21944 if (str && q && tcps->tcps_dbg) { 21945 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21946 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 21947 "flags 0x%x", 21948 str, seq, ack, ctl); 21949 } 21950 if (mp->b_datap->db_ref != 1) { 21951 mblk_t *mp1 = copyb(mp); 21952 freemsg(mp); 21953 mp = mp1; 21954 if (!mp) { 21955 if (mctl_present) 21956 freeb(ipsec_mp); 21957 return; 21958 } else { 21959 if (mctl_present) { 21960 ipsec_mp->b_cont = mp; 21961 } else { 21962 ipsec_mp = mp; 21963 } 21964 } 21965 } else if (mp->b_cont) { 21966 freemsg(mp->b_cont); 21967 mp->b_cont = NULL; 21968 } 21969 /* 21970 * We skip reversing source route here. 21971 * (for now we replace all IP options with EOL) 21972 */ 21973 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21974 ipha = (ipha_t *)mp->b_rptr; 21975 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 21976 mp->b_rptr[i] = IPOPT_EOL; 21977 /* 21978 * Make sure that src address isn't flagrantly invalid. 21979 * Not all broadcast address checking for the src address 21980 * is possible, since we don't know the netmask of the src 21981 * addr. No check for destination address is done, since 21982 * IP will not pass up a packet with a broadcast dest 21983 * address to TCP. Similar checks are done below for IPv6. 21984 */ 21985 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 21986 CLASSD(ipha->ipha_src)) { 21987 freemsg(ipsec_mp); 21988 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 21989 return; 21990 } 21991 } else { 21992 ip6h = (ip6_t *)mp->b_rptr; 21993 21994 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 21995 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 21996 freemsg(ipsec_mp); 21997 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 21998 return; 21999 } 22000 22001 /* Remove any extension headers assuming partial overlay */ 22002 if (ip_hdr_len > IPV6_HDR_LEN) { 22003 uint8_t *to; 22004 22005 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22006 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22007 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22008 ip_hdr_len = IPV6_HDR_LEN; 22009 ip6h = (ip6_t *)mp->b_rptr; 22010 ip6h->ip6_nxt = IPPROTO_TCP; 22011 } 22012 } 22013 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22014 if (tcph->th_flags[0] & TH_RST) { 22015 freemsg(ipsec_mp); 22016 return; 22017 } 22018 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22019 len = ip_hdr_len + sizeof (tcph_t); 22020 mp->b_wptr = &mp->b_rptr[len]; 22021 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22022 ipha->ipha_length = htons(len); 22023 /* Swap addresses */ 22024 v4addr = ipha->ipha_src; 22025 ipha->ipha_src = ipha->ipha_dst; 22026 ipha->ipha_dst = v4addr; 22027 ipha->ipha_ident = 0; 22028 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22029 addr_len = IP_ADDR_LEN; 22030 addr = &v4addr; 22031 } else { 22032 /* No ip6i_t in this case */ 22033 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22034 /* Swap addresses */ 22035 v6addr = ip6h->ip6_src; 22036 ip6h->ip6_src = ip6h->ip6_dst; 22037 ip6h->ip6_dst = v6addr; 22038 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22039 addr_len = IPV6_ADDR_LEN; 22040 addr = &v6addr; 22041 } 22042 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22043 U32_TO_BE32(ack, tcph->th_ack); 22044 U32_TO_BE32(seq, tcph->th_seq); 22045 U16_TO_BE16(0, tcph->th_win); 22046 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22047 tcph->th_flags[0] = (uint8_t)ctl; 22048 if (ctl & TH_RST) { 22049 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22050 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22051 } 22052 22053 /* IP trusts us to set up labels when required. */ 22054 if (is_system_labeled() && (cr = msg_getcred(mp, NULL)) != NULL && 22055 crgetlabel(cr) != NULL) { 22056 int err; 22057 22058 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22059 err = tsol_check_label(cr, &mp, 22060 tcp->tcp_connp->conn_mac_exempt, 22061 tcps->tcps_netstack->netstack_ip); 22062 else 22063 err = tsol_check_label_v6(cr, &mp, 22064 tcp->tcp_connp->conn_mac_exempt, 22065 tcps->tcps_netstack->netstack_ip); 22066 if (mctl_present) 22067 ipsec_mp->b_cont = mp; 22068 else 22069 ipsec_mp = mp; 22070 if (err != 0) { 22071 freemsg(ipsec_mp); 22072 return; 22073 } 22074 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22075 ipha = (ipha_t *)mp->b_rptr; 22076 } else { 22077 ip6h = (ip6_t *)mp->b_rptr; 22078 } 22079 } 22080 22081 if (mctl_present) { 22082 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22083 22084 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22085 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22086 return; 22087 } 22088 } 22089 if (zoneid == ALL_ZONES) 22090 zoneid = GLOBAL_ZONEID; 22091 22092 /* Add the zoneid so ip_output routes it properly */ 22093 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22094 freemsg(ipsec_mp); 22095 return; 22096 } 22097 ipsec_mp = nmp; 22098 22099 /* 22100 * NOTE: one might consider tracing a TCP packet here, but 22101 * this function has no active TCP state and no tcp structure 22102 * that has a trace buffer. If we traced here, we would have 22103 * to keep a local trace buffer in tcp_record_trace(). 22104 * 22105 * TSol note: The mblk that contains the incoming packet was 22106 * reused by tcp_xmit_listener_reset, so it already contains 22107 * the right credentials and we don't need to call mblk_setcred. 22108 * Also the conn's cred is not right since it is associated 22109 * with tcps_g_q. 22110 */ 22111 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22112 22113 /* 22114 * Tell IP to mark the IRE used for this destination temporary. 22115 * This way, we can limit our exposure to DoS attack because IP 22116 * creates an IRE for each destination. If there are too many, 22117 * the time to do any routing lookup will be extremely long. And 22118 * the lookup can be in interrupt context. 22119 * 22120 * Note that in normal circumstances, this marking should not 22121 * affect anything. It would be nice if only 1 message is 22122 * needed to inform IP that the IRE created for this RST should 22123 * not be added to the cache table. But there is currently 22124 * not such communication mechanism between TCP and IP. So 22125 * the best we can do now is to send the advice ioctl to IP 22126 * to mark the IRE temporary. 22127 */ 22128 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22129 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22130 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22131 } 22132 } 22133 22134 /* 22135 * Initiate closedown sequence on an active connection. (May be called as 22136 * writer.) Return value zero for OK return, non-zero for error return. 22137 */ 22138 static int 22139 tcp_xmit_end(tcp_t *tcp) 22140 { 22141 ipic_t *ipic; 22142 mblk_t *mp; 22143 tcp_stack_t *tcps = tcp->tcp_tcps; 22144 22145 if (tcp->tcp_state < TCPS_SYN_RCVD || 22146 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22147 /* 22148 * Invalid state, only states TCPS_SYN_RCVD, 22149 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22150 */ 22151 return (-1); 22152 } 22153 22154 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22155 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22156 /* 22157 * If there is nothing more unsent, send the FIN now. 22158 * Otherwise, it will go out with the last segment. 22159 */ 22160 if (tcp->tcp_unsent == 0) { 22161 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22162 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22163 22164 if (mp) { 22165 tcp_send_data(tcp, tcp->tcp_wq, mp); 22166 } else { 22167 /* 22168 * Couldn't allocate msg. Pretend we got it out. 22169 * Wait for rexmit timeout. 22170 */ 22171 tcp->tcp_snxt = tcp->tcp_fss + 1; 22172 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22173 } 22174 22175 /* 22176 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22177 * changed. 22178 */ 22179 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22180 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22181 } 22182 } else { 22183 /* 22184 * If tcp->tcp_cork is set, then the data will not get sent, 22185 * so we have to check that and unset it first. 22186 */ 22187 if (tcp->tcp_cork) 22188 tcp->tcp_cork = B_FALSE; 22189 tcp_wput_data(tcp, NULL, B_FALSE); 22190 } 22191 22192 /* 22193 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22194 * is 0, don't update the cache. 22195 */ 22196 if (tcps->tcps_rtt_updates == 0 || 22197 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22198 return (0); 22199 22200 /* 22201 * NOTE: should not update if source routes i.e. if tcp_remote if 22202 * different from the destination. 22203 */ 22204 if (tcp->tcp_ipversion == IPV4_VERSION) { 22205 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22206 return (0); 22207 } 22208 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22209 &ipic); 22210 } else { 22211 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22212 &tcp->tcp_ip6h->ip6_dst))) { 22213 return (0); 22214 } 22215 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22216 &ipic); 22217 } 22218 22219 /* Record route attributes in the IRE for use by future connections. */ 22220 if (mp == NULL) 22221 return (0); 22222 22223 /* 22224 * We do not have a good algorithm to update ssthresh at this time. 22225 * So don't do any update. 22226 */ 22227 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22228 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22229 22230 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22231 22232 return (0); 22233 } 22234 22235 /* ARGSUSED */ 22236 void 22237 tcp_xmit_reset(void *arg, mblk_t *mp, void *arg2) 22238 { 22239 conn_t *connp = (conn_t *)arg; 22240 mblk_t *mp1; 22241 tcp_t *tcp = connp->conn_tcp; 22242 tcp_xmit_reset_event_t *eventp; 22243 22244 ASSERT(mp->b_datap->db_type == M_PROTO && 22245 MBLKL(mp) == sizeof (tcp_xmit_reset_event_t)); 22246 22247 if (tcp->tcp_state != TCPS_LISTEN) { 22248 freemsg(mp); 22249 return; 22250 } 22251 22252 mp1 = mp->b_cont; 22253 mp->b_cont = NULL; 22254 eventp = (tcp_xmit_reset_event_t *)mp->b_rptr; 22255 ASSERT(eventp->tcp_xre_tcps->tcps_netstack == 22256 connp->conn_netstack); 22257 22258 tcp_xmit_listeners_reset(mp1, eventp->tcp_xre_iphdrlen, 22259 eventp->tcp_xre_zoneid, eventp->tcp_xre_tcps, connp); 22260 freemsg(mp); 22261 } 22262 22263 /* 22264 * Generate a "no listener here" RST in response to an "unknown" segment. 22265 * connp is set by caller when RST is in response to an unexpected 22266 * inbound packet for which there is active tcp state in the system. 22267 * Note that we are reusing the incoming mp to construct the outgoing RST. 22268 */ 22269 void 22270 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22271 tcp_stack_t *tcps, conn_t *connp) 22272 { 22273 uchar_t *rptr; 22274 uint32_t seg_len; 22275 tcph_t *tcph; 22276 uint32_t seg_seq; 22277 uint32_t seg_ack; 22278 uint_t flags; 22279 mblk_t *ipsec_mp; 22280 ipha_t *ipha; 22281 ip6_t *ip6h; 22282 boolean_t mctl_present = B_FALSE; 22283 boolean_t check = B_TRUE; 22284 boolean_t policy_present; 22285 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22286 22287 TCP_STAT(tcps, tcp_no_listener); 22288 22289 ipsec_mp = mp; 22290 22291 if (mp->b_datap->db_type == M_CTL) { 22292 ipsec_in_t *ii; 22293 22294 mctl_present = B_TRUE; 22295 mp = mp->b_cont; 22296 22297 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22298 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22299 if (ii->ipsec_in_dont_check) { 22300 check = B_FALSE; 22301 if (!ii->ipsec_in_secure) { 22302 freeb(ipsec_mp); 22303 mctl_present = B_FALSE; 22304 ipsec_mp = mp; 22305 } 22306 } 22307 } 22308 22309 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22310 policy_present = ipss->ipsec_inbound_v4_policy_present; 22311 ipha = (ipha_t *)mp->b_rptr; 22312 ip6h = NULL; 22313 } else { 22314 policy_present = ipss->ipsec_inbound_v6_policy_present; 22315 ipha = NULL; 22316 ip6h = (ip6_t *)mp->b_rptr; 22317 } 22318 22319 if (check && policy_present) { 22320 /* 22321 * The conn_t parameter is NULL because we already know 22322 * nobody's home. 22323 */ 22324 ipsec_mp = ipsec_check_global_policy( 22325 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22326 tcps->tcps_netstack); 22327 if (ipsec_mp == NULL) 22328 return; 22329 } 22330 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22331 DTRACE_PROBE2( 22332 tx__ip__log__error__nolistener__tcp, 22333 char *, "Could not reply with RST to mp(1)", 22334 mblk_t *, mp); 22335 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22336 freemsg(ipsec_mp); 22337 return; 22338 } 22339 22340 rptr = mp->b_rptr; 22341 22342 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22343 seg_seq = BE32_TO_U32(tcph->th_seq); 22344 seg_ack = BE32_TO_U32(tcph->th_ack); 22345 flags = tcph->th_flags[0]; 22346 22347 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22348 if (flags & TH_RST) { 22349 freemsg(ipsec_mp); 22350 } else if (flags & TH_ACK) { 22351 tcp_xmit_early_reset("no tcp, reset", 22352 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 22353 connp); 22354 } else { 22355 if (flags & TH_SYN) { 22356 seg_len++; 22357 } else { 22358 /* 22359 * Here we violate the RFC. Note that a normal 22360 * TCP will never send a segment without the ACK 22361 * flag, except for RST or SYN segment. This 22362 * segment is neither. Just drop it on the 22363 * floor. 22364 */ 22365 freemsg(ipsec_mp); 22366 tcps->tcps_rst_unsent++; 22367 return; 22368 } 22369 22370 tcp_xmit_early_reset("no tcp, reset/ack", 22371 ipsec_mp, 0, seg_seq + seg_len, 22372 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 22373 } 22374 } 22375 22376 /* 22377 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22378 * ip and tcp header ready to pass down to IP. If the mp passed in is 22379 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22380 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22381 * otherwise it will dup partial mblks.) 22382 * Otherwise, an appropriate ACK packet will be generated. This 22383 * routine is not usually called to send new data for the first time. It 22384 * is mostly called out of the timer for retransmits, and to generate ACKs. 22385 * 22386 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22387 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22388 * of the original mblk chain will be returned in *offset and *end_mp. 22389 */ 22390 mblk_t * 22391 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22392 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22393 boolean_t rexmit) 22394 { 22395 int data_length; 22396 int32_t off = 0; 22397 uint_t flags; 22398 mblk_t *mp1; 22399 mblk_t *mp2; 22400 uchar_t *rptr; 22401 tcph_t *tcph; 22402 int32_t num_sack_blk = 0; 22403 int32_t sack_opt_len = 0; 22404 tcp_stack_t *tcps = tcp->tcp_tcps; 22405 22406 /* Allocate for our maximum TCP header + link-level */ 22407 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22408 tcps->tcps_wroff_xtra, BPRI_MED); 22409 if (!mp1) 22410 return (NULL); 22411 data_length = 0; 22412 22413 /* 22414 * Note that tcp_mss has been adjusted to take into account the 22415 * timestamp option if applicable. Because SACK options do not 22416 * appear in every TCP segments and they are of variable lengths, 22417 * they cannot be included in tcp_mss. Thus we need to calculate 22418 * the actual segment length when we need to send a segment which 22419 * includes SACK options. 22420 */ 22421 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22422 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22423 tcp->tcp_num_sack_blk); 22424 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22425 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22426 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22427 max_to_send -= sack_opt_len; 22428 } 22429 22430 if (offset != NULL) { 22431 off = *offset; 22432 /* We use offset as an indicator that end_mp is not NULL. */ 22433 *end_mp = NULL; 22434 } 22435 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22436 /* This could be faster with cooperation from downstream */ 22437 if (mp2 != mp1 && !sendall && 22438 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22439 max_to_send) 22440 /* 22441 * Don't send the next mblk since the whole mblk 22442 * does not fit. 22443 */ 22444 break; 22445 mp2->b_cont = dupb(mp); 22446 mp2 = mp2->b_cont; 22447 if (!mp2) { 22448 freemsg(mp1); 22449 return (NULL); 22450 } 22451 mp2->b_rptr += off; 22452 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22453 (uintptr_t)INT_MAX); 22454 22455 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22456 if (data_length > max_to_send) { 22457 mp2->b_wptr -= data_length - max_to_send; 22458 data_length = max_to_send; 22459 off = mp2->b_wptr - mp->b_rptr; 22460 break; 22461 } else { 22462 off = 0; 22463 } 22464 } 22465 if (offset != NULL) { 22466 *offset = off; 22467 *end_mp = mp; 22468 } 22469 if (seg_len != NULL) { 22470 *seg_len = data_length; 22471 } 22472 22473 /* Update the latest receive window size in TCP header. */ 22474 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22475 tcp->tcp_tcph->th_win); 22476 22477 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22478 mp1->b_rptr = rptr; 22479 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22480 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22481 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22482 U32_TO_ABE32(seq, tcph->th_seq); 22483 22484 /* 22485 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22486 * that this function was called from tcp_wput_data. Thus, when called 22487 * to retransmit data the setting of the PUSH bit may appear some 22488 * what random in that it might get set when it should not. This 22489 * should not pose any performance issues. 22490 */ 22491 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22492 tcp->tcp_unsent == data_length)) { 22493 flags = TH_ACK | TH_PUSH; 22494 } else { 22495 flags = TH_ACK; 22496 } 22497 22498 if (tcp->tcp_ecn_ok) { 22499 if (tcp->tcp_ecn_echo_on) 22500 flags |= TH_ECE; 22501 22502 /* 22503 * Only set ECT bit and ECN_CWR if a segment contains new data. 22504 * There is no TCP flow control for non-data segments, and 22505 * only data segment is transmitted reliably. 22506 */ 22507 if (data_length > 0 && !rexmit) { 22508 SET_ECT(tcp, rptr); 22509 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 22510 flags |= TH_CWR; 22511 tcp->tcp_ecn_cwr_sent = B_TRUE; 22512 } 22513 } 22514 } 22515 22516 if (tcp->tcp_valid_bits) { 22517 uint32_t u1; 22518 22519 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 22520 seq == tcp->tcp_iss) { 22521 uchar_t *wptr; 22522 22523 /* 22524 * If TCP_ISS_VALID and the seq number is tcp_iss, 22525 * TCP can only be in SYN-SENT, SYN-RCVD or 22526 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 22527 * our SYN is not ack'ed but the app closes this 22528 * TCP connection. 22529 */ 22530 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 22531 tcp->tcp_state == TCPS_SYN_RCVD || 22532 tcp->tcp_state == TCPS_FIN_WAIT_1); 22533 22534 /* 22535 * Tack on the MSS option. It is always needed 22536 * for both active and passive open. 22537 * 22538 * MSS option value should be interface MTU - MIN 22539 * TCP/IP header according to RFC 793 as it means 22540 * the maximum segment size TCP can receive. But 22541 * to get around some broken middle boxes/end hosts 22542 * out there, we allow the option value to be the 22543 * same as the MSS option size on the peer side. 22544 * In this way, the other side will not send 22545 * anything larger than they can receive. 22546 * 22547 * Note that for SYN_SENT state, the ndd param 22548 * tcp_use_smss_as_mss_opt has no effect as we 22549 * don't know the peer's MSS option value. So 22550 * the only case we need to take care of is in 22551 * SYN_RCVD state, which is done later. 22552 */ 22553 wptr = mp1->b_wptr; 22554 wptr[0] = TCPOPT_MAXSEG; 22555 wptr[1] = TCPOPT_MAXSEG_LEN; 22556 wptr += 2; 22557 u1 = tcp->tcp_if_mtu - 22558 (tcp->tcp_ipversion == IPV4_VERSION ? 22559 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 22560 TCP_MIN_HEADER_LENGTH; 22561 U16_TO_BE16(u1, wptr); 22562 mp1->b_wptr = wptr + 2; 22563 /* Update the offset to cover the additional word */ 22564 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22565 22566 /* 22567 * Note that the following way of filling in 22568 * TCP options are not optimal. Some NOPs can 22569 * be saved. But there is no need at this time 22570 * to optimize it. When it is needed, we will 22571 * do it. 22572 */ 22573 switch (tcp->tcp_state) { 22574 case TCPS_SYN_SENT: 22575 flags = TH_SYN; 22576 22577 if (tcp->tcp_snd_ts_ok) { 22578 uint32_t llbolt = (uint32_t)lbolt; 22579 22580 wptr = mp1->b_wptr; 22581 wptr[0] = TCPOPT_NOP; 22582 wptr[1] = TCPOPT_NOP; 22583 wptr[2] = TCPOPT_TSTAMP; 22584 wptr[3] = TCPOPT_TSTAMP_LEN; 22585 wptr += 4; 22586 U32_TO_BE32(llbolt, wptr); 22587 wptr += 4; 22588 ASSERT(tcp->tcp_ts_recent == 0); 22589 U32_TO_BE32(0L, wptr); 22590 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 22591 tcph->th_offset_and_rsrvd[0] += 22592 (3 << 4); 22593 } 22594 22595 /* 22596 * Set up all the bits to tell other side 22597 * we are ECN capable. 22598 */ 22599 if (tcp->tcp_ecn_ok) { 22600 flags |= (TH_ECE | TH_CWR); 22601 } 22602 break; 22603 case TCPS_SYN_RCVD: 22604 flags |= TH_SYN; 22605 22606 /* 22607 * Reset the MSS option value to be SMSS 22608 * We should probably add back the bytes 22609 * for timestamp option and IPsec. We 22610 * don't do that as this is a workaround 22611 * for broken middle boxes/end hosts, it 22612 * is better for us to be more cautious. 22613 * They may not take these things into 22614 * account in their SMSS calculation. Thus 22615 * the peer's calculated SMSS may be smaller 22616 * than what it can be. This should be OK. 22617 */ 22618 if (tcps->tcps_use_smss_as_mss_opt) { 22619 u1 = tcp->tcp_mss; 22620 U16_TO_BE16(u1, wptr); 22621 } 22622 22623 /* 22624 * If the other side is ECN capable, reply 22625 * that we are also ECN capable. 22626 */ 22627 if (tcp->tcp_ecn_ok) 22628 flags |= TH_ECE; 22629 break; 22630 default: 22631 /* 22632 * The above ASSERT() makes sure that this 22633 * must be FIN-WAIT-1 state. Our SYN has 22634 * not been ack'ed so retransmit it. 22635 */ 22636 flags |= TH_SYN; 22637 break; 22638 } 22639 22640 if (tcp->tcp_snd_ws_ok) { 22641 wptr = mp1->b_wptr; 22642 wptr[0] = TCPOPT_NOP; 22643 wptr[1] = TCPOPT_WSCALE; 22644 wptr[2] = TCPOPT_WS_LEN; 22645 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 22646 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 22647 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22648 } 22649 22650 if (tcp->tcp_snd_sack_ok) { 22651 wptr = mp1->b_wptr; 22652 wptr[0] = TCPOPT_NOP; 22653 wptr[1] = TCPOPT_NOP; 22654 wptr[2] = TCPOPT_SACK_PERMITTED; 22655 wptr[3] = TCPOPT_SACK_OK_LEN; 22656 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 22657 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22658 } 22659 22660 /* allocb() of adequate mblk assures space */ 22661 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 22662 (uintptr_t)INT_MAX); 22663 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 22664 /* 22665 * Get IP set to checksum on our behalf 22666 * Include the adjustment for a source route if any. 22667 */ 22668 u1 += tcp->tcp_sum; 22669 u1 = (u1 >> 16) + (u1 & 0xFFFF); 22670 U16_TO_BE16(u1, tcph->th_sum); 22671 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22672 } 22673 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 22674 (seq + data_length) == tcp->tcp_fss) { 22675 if (!tcp->tcp_fin_acked) { 22676 flags |= TH_FIN; 22677 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22678 } 22679 if (!tcp->tcp_fin_sent) { 22680 tcp->tcp_fin_sent = B_TRUE; 22681 switch (tcp->tcp_state) { 22682 case TCPS_SYN_RCVD: 22683 case TCPS_ESTABLISHED: 22684 tcp->tcp_state = TCPS_FIN_WAIT_1; 22685 break; 22686 case TCPS_CLOSE_WAIT: 22687 tcp->tcp_state = TCPS_LAST_ACK; 22688 break; 22689 } 22690 if (tcp->tcp_suna == tcp->tcp_snxt) 22691 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22692 tcp->tcp_snxt = tcp->tcp_fss + 1; 22693 } 22694 } 22695 /* 22696 * Note the trick here. u1 is unsigned. When tcp_urg 22697 * is smaller than seq, u1 will become a very huge value. 22698 * So the comparison will fail. Also note that tcp_urp 22699 * should be positive, see RFC 793 page 17. 22700 */ 22701 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 22702 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 22703 u1 < (uint32_t)(64 * 1024)) { 22704 flags |= TH_URG; 22705 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 22706 U32_TO_ABE16(u1, tcph->th_urp); 22707 } 22708 } 22709 tcph->th_flags[0] = (uchar_t)flags; 22710 tcp->tcp_rack = tcp->tcp_rnxt; 22711 tcp->tcp_rack_cnt = 0; 22712 22713 if (tcp->tcp_snd_ts_ok) { 22714 if (tcp->tcp_state != TCPS_SYN_SENT) { 22715 uint32_t llbolt = (uint32_t)lbolt; 22716 22717 U32_TO_BE32(llbolt, 22718 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22719 U32_TO_BE32(tcp->tcp_ts_recent, 22720 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22721 } 22722 } 22723 22724 if (num_sack_blk > 0) { 22725 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22726 sack_blk_t *tmp; 22727 int32_t i; 22728 22729 wptr[0] = TCPOPT_NOP; 22730 wptr[1] = TCPOPT_NOP; 22731 wptr[2] = TCPOPT_SACK; 22732 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22733 sizeof (sack_blk_t); 22734 wptr += TCPOPT_REAL_SACK_LEN; 22735 22736 tmp = tcp->tcp_sack_list; 22737 for (i = 0; i < num_sack_blk; i++) { 22738 U32_TO_BE32(tmp[i].begin, wptr); 22739 wptr += sizeof (tcp_seq); 22740 U32_TO_BE32(tmp[i].end, wptr); 22741 wptr += sizeof (tcp_seq); 22742 } 22743 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 22744 } 22745 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22746 data_length += (int)(mp1->b_wptr - rptr); 22747 if (tcp->tcp_ipversion == IPV4_VERSION) { 22748 ((ipha_t *)rptr)->ipha_length = htons(data_length); 22749 } else { 22750 ip6_t *ip6 = (ip6_t *)(rptr + 22751 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22752 sizeof (ip6i_t) : 0)); 22753 22754 ip6->ip6_plen = htons(data_length - 22755 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22756 } 22757 22758 /* 22759 * Prime pump for IP 22760 * Include the adjustment for a source route if any. 22761 */ 22762 data_length -= tcp->tcp_ip_hdr_len; 22763 data_length += tcp->tcp_sum; 22764 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22765 U16_TO_ABE16(data_length, tcph->th_sum); 22766 if (tcp->tcp_ip_forward_progress) { 22767 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22768 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22769 tcp->tcp_ip_forward_progress = B_FALSE; 22770 } 22771 return (mp1); 22772 } 22773 22774 /* This function handles the push timeout. */ 22775 void 22776 tcp_push_timer(void *arg) 22777 { 22778 conn_t *connp = (conn_t *)arg; 22779 tcp_t *tcp = connp->conn_tcp; 22780 22781 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 22782 22783 ASSERT(tcp->tcp_listener == NULL); 22784 22785 ASSERT(!IPCL_IS_NONSTR(connp)); 22786 22787 /* 22788 * We need to plug synchronous streams during our drain to prevent 22789 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 22790 */ 22791 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 22792 tcp->tcp_push_tid = 0; 22793 22794 if (tcp->tcp_rcv_list != NULL && 22795 tcp_rcv_drain(tcp) == TH_ACK_NEEDED) 22796 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 22797 22798 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 22799 } 22800 22801 /* 22802 * This function handles delayed ACK timeout. 22803 */ 22804 static void 22805 tcp_ack_timer(void *arg) 22806 { 22807 conn_t *connp = (conn_t *)arg; 22808 tcp_t *tcp = connp->conn_tcp; 22809 mblk_t *mp; 22810 tcp_stack_t *tcps = tcp->tcp_tcps; 22811 22812 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 22813 22814 tcp->tcp_ack_tid = 0; 22815 22816 if (tcp->tcp_fused) 22817 return; 22818 22819 /* 22820 * Do not send ACK if there is no outstanding unack'ed data. 22821 */ 22822 if (tcp->tcp_rnxt == tcp->tcp_rack) { 22823 return; 22824 } 22825 22826 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 22827 /* 22828 * Make sure we don't allow deferred ACKs to result in 22829 * timer-based ACKing. If we have held off an ACK 22830 * when there was more than an mss here, and the timer 22831 * goes off, we have to worry about the possibility 22832 * that the sender isn't doing slow-start, or is out 22833 * of step with us for some other reason. We fall 22834 * permanently back in the direction of 22835 * ACK-every-other-packet as suggested in RFC 1122. 22836 */ 22837 if (tcp->tcp_rack_abs_max > 2) 22838 tcp->tcp_rack_abs_max--; 22839 tcp->tcp_rack_cur_max = 2; 22840 } 22841 mp = tcp_ack_mp(tcp); 22842 22843 if (mp != NULL) { 22844 BUMP_LOCAL(tcp->tcp_obsegs); 22845 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22846 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 22847 tcp_send_data(tcp, tcp->tcp_wq, mp); 22848 } 22849 } 22850 22851 22852 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 22853 static mblk_t * 22854 tcp_ack_mp(tcp_t *tcp) 22855 { 22856 uint32_t seq_no; 22857 tcp_stack_t *tcps = tcp->tcp_tcps; 22858 22859 /* 22860 * There are a few cases to be considered while setting the sequence no. 22861 * Essentially, we can come here while processing an unacceptable pkt 22862 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 22863 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 22864 * If we are here for a zero window probe, stick with suna. In all 22865 * other cases, we check if suna + swnd encompasses snxt and set 22866 * the sequence number to snxt, if so. If snxt falls outside the 22867 * window (the receiver probably shrunk its window), we will go with 22868 * suna + swnd, otherwise the sequence no will be unacceptable to the 22869 * receiver. 22870 */ 22871 if (tcp->tcp_zero_win_probe) { 22872 seq_no = tcp->tcp_suna; 22873 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 22874 ASSERT(tcp->tcp_swnd == 0); 22875 seq_no = tcp->tcp_snxt; 22876 } else { 22877 seq_no = SEQ_GT(tcp->tcp_snxt, 22878 (tcp->tcp_suna + tcp->tcp_swnd)) ? 22879 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 22880 } 22881 22882 if (tcp->tcp_valid_bits) { 22883 /* 22884 * For the complex case where we have to send some 22885 * controls (FIN or SYN), let tcp_xmit_mp do it. 22886 */ 22887 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 22888 NULL, B_FALSE)); 22889 } else { 22890 /* Generate a simple ACK */ 22891 int data_length; 22892 uchar_t *rptr; 22893 tcph_t *tcph; 22894 mblk_t *mp1; 22895 int32_t tcp_hdr_len; 22896 int32_t tcp_tcp_hdr_len; 22897 int32_t num_sack_blk = 0; 22898 int32_t sack_opt_len; 22899 22900 /* 22901 * Allocate space for TCP + IP headers 22902 * and link-level header 22903 */ 22904 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22905 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22906 tcp->tcp_num_sack_blk); 22907 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22908 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22909 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 22910 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 22911 } else { 22912 tcp_hdr_len = tcp->tcp_hdr_len; 22913 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 22914 } 22915 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 22916 if (!mp1) 22917 return (NULL); 22918 22919 /* Update the latest receive window size in TCP header. */ 22920 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22921 tcp->tcp_tcph->th_win); 22922 /* copy in prototype TCP + IP header */ 22923 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22924 mp1->b_rptr = rptr; 22925 mp1->b_wptr = rptr + tcp_hdr_len; 22926 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22927 22928 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22929 22930 /* Set the TCP sequence number. */ 22931 U32_TO_ABE32(seq_no, tcph->th_seq); 22932 22933 /* Set up the TCP flag field. */ 22934 tcph->th_flags[0] = (uchar_t)TH_ACK; 22935 if (tcp->tcp_ecn_echo_on) 22936 tcph->th_flags[0] |= TH_ECE; 22937 22938 tcp->tcp_rack = tcp->tcp_rnxt; 22939 tcp->tcp_rack_cnt = 0; 22940 22941 /* fill in timestamp option if in use */ 22942 if (tcp->tcp_snd_ts_ok) { 22943 uint32_t llbolt = (uint32_t)lbolt; 22944 22945 U32_TO_BE32(llbolt, 22946 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22947 U32_TO_BE32(tcp->tcp_ts_recent, 22948 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22949 } 22950 22951 /* Fill in SACK options */ 22952 if (num_sack_blk > 0) { 22953 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22954 sack_blk_t *tmp; 22955 int32_t i; 22956 22957 wptr[0] = TCPOPT_NOP; 22958 wptr[1] = TCPOPT_NOP; 22959 wptr[2] = TCPOPT_SACK; 22960 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22961 sizeof (sack_blk_t); 22962 wptr += TCPOPT_REAL_SACK_LEN; 22963 22964 tmp = tcp->tcp_sack_list; 22965 for (i = 0; i < num_sack_blk; i++) { 22966 U32_TO_BE32(tmp[i].begin, wptr); 22967 wptr += sizeof (tcp_seq); 22968 U32_TO_BE32(tmp[i].end, wptr); 22969 wptr += sizeof (tcp_seq); 22970 } 22971 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 22972 << 4); 22973 } 22974 22975 if (tcp->tcp_ipversion == IPV4_VERSION) { 22976 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 22977 } else { 22978 /* Check for ip6i_t header in sticky hdrs */ 22979 ip6_t *ip6 = (ip6_t *)(rptr + 22980 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22981 sizeof (ip6i_t) : 0)); 22982 22983 ip6->ip6_plen = htons(tcp_hdr_len - 22984 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22985 } 22986 22987 /* 22988 * Prime pump for checksum calculation in IP. Include the 22989 * adjustment for a source route if any. 22990 */ 22991 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 22992 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22993 U16_TO_ABE16(data_length, tcph->th_sum); 22994 22995 if (tcp->tcp_ip_forward_progress) { 22996 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22997 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22998 tcp->tcp_ip_forward_progress = B_FALSE; 22999 } 23000 return (mp1); 23001 } 23002 } 23003 23004 /* 23005 * Hash list insertion routine for tcp_t structures. Each hash bucket 23006 * contains a list of tcp_t entries, and each entry is bound to a unique 23007 * port. If there are multiple tcp_t's that are bound to the same port, then 23008 * one of them will be linked into the hash bucket list, and the rest will 23009 * hang off of that one entry. For each port, entries bound to a specific IP 23010 * address will be inserted before those those bound to INADDR_ANY. 23011 */ 23012 static void 23013 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23014 { 23015 tcp_t **tcpp; 23016 tcp_t *tcpnext; 23017 tcp_t *tcphash; 23018 23019 if (tcp->tcp_ptpbhn != NULL) { 23020 ASSERT(!caller_holds_lock); 23021 tcp_bind_hash_remove(tcp); 23022 } 23023 tcpp = &tbf->tf_tcp; 23024 if (!caller_holds_lock) { 23025 mutex_enter(&tbf->tf_lock); 23026 } else { 23027 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23028 } 23029 tcphash = tcpp[0]; 23030 tcpnext = NULL; 23031 if (tcphash != NULL) { 23032 /* Look for an entry using the same port */ 23033 while ((tcphash = tcpp[0]) != NULL && 23034 tcp->tcp_lport != tcphash->tcp_lport) 23035 tcpp = &(tcphash->tcp_bind_hash); 23036 23037 /* The port was not found, just add to the end */ 23038 if (tcphash == NULL) 23039 goto insert; 23040 23041 /* 23042 * OK, there already exists an entry bound to the 23043 * same port. 23044 * 23045 * If the new tcp bound to the INADDR_ANY address 23046 * and the first one in the list is not bound to 23047 * INADDR_ANY we skip all entries until we find the 23048 * first one bound to INADDR_ANY. 23049 * This makes sure that applications binding to a 23050 * specific address get preference over those binding to 23051 * INADDR_ANY. 23052 */ 23053 tcpnext = tcphash; 23054 tcphash = NULL; 23055 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23056 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23057 while ((tcpnext = tcpp[0]) != NULL && 23058 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23059 tcpp = &(tcpnext->tcp_bind_hash_port); 23060 23061 if (tcpnext) { 23062 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23063 tcphash = tcpnext->tcp_bind_hash; 23064 if (tcphash != NULL) { 23065 tcphash->tcp_ptpbhn = 23066 &(tcp->tcp_bind_hash); 23067 tcpnext->tcp_bind_hash = NULL; 23068 } 23069 } 23070 } else { 23071 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23072 tcphash = tcpnext->tcp_bind_hash; 23073 if (tcphash != NULL) { 23074 tcphash->tcp_ptpbhn = 23075 &(tcp->tcp_bind_hash); 23076 tcpnext->tcp_bind_hash = NULL; 23077 } 23078 } 23079 } 23080 insert: 23081 tcp->tcp_bind_hash_port = tcpnext; 23082 tcp->tcp_bind_hash = tcphash; 23083 tcp->tcp_ptpbhn = tcpp; 23084 tcpp[0] = tcp; 23085 if (!caller_holds_lock) 23086 mutex_exit(&tbf->tf_lock); 23087 } 23088 23089 /* 23090 * Hash list removal routine for tcp_t structures. 23091 */ 23092 static void 23093 tcp_bind_hash_remove(tcp_t *tcp) 23094 { 23095 tcp_t *tcpnext; 23096 kmutex_t *lockp; 23097 tcp_stack_t *tcps = tcp->tcp_tcps; 23098 23099 if (tcp->tcp_ptpbhn == NULL) 23100 return; 23101 23102 /* 23103 * Extract the lock pointer in case there are concurrent 23104 * hash_remove's for this instance. 23105 */ 23106 ASSERT(tcp->tcp_lport != 0); 23107 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23108 23109 ASSERT(lockp != NULL); 23110 mutex_enter(lockp); 23111 if (tcp->tcp_ptpbhn) { 23112 tcpnext = tcp->tcp_bind_hash_port; 23113 if (tcpnext != NULL) { 23114 tcp->tcp_bind_hash_port = NULL; 23115 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23116 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 23117 if (tcpnext->tcp_bind_hash != NULL) { 23118 tcpnext->tcp_bind_hash->tcp_ptpbhn = 23119 &(tcpnext->tcp_bind_hash); 23120 tcp->tcp_bind_hash = NULL; 23121 } 23122 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 23123 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23124 tcp->tcp_bind_hash = NULL; 23125 } 23126 *tcp->tcp_ptpbhn = tcpnext; 23127 tcp->tcp_ptpbhn = NULL; 23128 } 23129 mutex_exit(lockp); 23130 } 23131 23132 23133 /* 23134 * Hash list lookup routine for tcp_t structures. 23135 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23136 */ 23137 static tcp_t * 23138 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23139 { 23140 tf_t *tf; 23141 tcp_t *tcp; 23142 23143 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23144 mutex_enter(&tf->tf_lock); 23145 for (tcp = tf->tf_tcp; tcp != NULL; 23146 tcp = tcp->tcp_acceptor_hash) { 23147 if (tcp->tcp_acceptor_id == id) { 23148 CONN_INC_REF(tcp->tcp_connp); 23149 mutex_exit(&tf->tf_lock); 23150 return (tcp); 23151 } 23152 } 23153 mutex_exit(&tf->tf_lock); 23154 return (NULL); 23155 } 23156 23157 23158 /* 23159 * Hash list insertion routine for tcp_t structures. 23160 */ 23161 void 23162 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23163 { 23164 tf_t *tf; 23165 tcp_t **tcpp; 23166 tcp_t *tcpnext; 23167 tcp_stack_t *tcps = tcp->tcp_tcps; 23168 23169 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23170 23171 if (tcp->tcp_ptpahn != NULL) 23172 tcp_acceptor_hash_remove(tcp); 23173 tcpp = &tf->tf_tcp; 23174 mutex_enter(&tf->tf_lock); 23175 tcpnext = tcpp[0]; 23176 if (tcpnext) 23177 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23178 tcp->tcp_acceptor_hash = tcpnext; 23179 tcp->tcp_ptpahn = tcpp; 23180 tcpp[0] = tcp; 23181 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23182 mutex_exit(&tf->tf_lock); 23183 } 23184 23185 /* 23186 * Hash list removal routine for tcp_t structures. 23187 */ 23188 static void 23189 tcp_acceptor_hash_remove(tcp_t *tcp) 23190 { 23191 tcp_t *tcpnext; 23192 kmutex_t *lockp; 23193 23194 /* 23195 * Extract the lock pointer in case there are concurrent 23196 * hash_remove's for this instance. 23197 */ 23198 lockp = tcp->tcp_acceptor_lockp; 23199 23200 if (tcp->tcp_ptpahn == NULL) 23201 return; 23202 23203 ASSERT(lockp != NULL); 23204 mutex_enter(lockp); 23205 if (tcp->tcp_ptpahn) { 23206 tcpnext = tcp->tcp_acceptor_hash; 23207 if (tcpnext) { 23208 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 23209 tcp->tcp_acceptor_hash = NULL; 23210 } 23211 *tcp->tcp_ptpahn = tcpnext; 23212 tcp->tcp_ptpahn = NULL; 23213 } 23214 mutex_exit(lockp); 23215 tcp->tcp_acceptor_lockp = NULL; 23216 } 23217 23218 /* 23219 * Type three generator adapted from the random() function in 4.4 BSD: 23220 */ 23221 23222 /* 23223 * Copyright (c) 1983, 1993 23224 * The Regents of the University of California. All rights reserved. 23225 * 23226 * Redistribution and use in source and binary forms, with or without 23227 * modification, are permitted provided that the following conditions 23228 * are met: 23229 * 1. Redistributions of source code must retain the above copyright 23230 * notice, this list of conditions and the following disclaimer. 23231 * 2. Redistributions in binary form must reproduce the above copyright 23232 * notice, this list of conditions and the following disclaimer in the 23233 * documentation and/or other materials provided with the distribution. 23234 * 3. All advertising materials mentioning features or use of this software 23235 * must display the following acknowledgement: 23236 * This product includes software developed by the University of 23237 * California, Berkeley and its contributors. 23238 * 4. Neither the name of the University nor the names of its contributors 23239 * may be used to endorse or promote products derived from this software 23240 * without specific prior written permission. 23241 * 23242 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23243 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23244 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23245 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23246 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23247 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23248 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23249 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23250 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23251 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23252 * SUCH DAMAGE. 23253 */ 23254 23255 /* Type 3 -- x**31 + x**3 + 1 */ 23256 #define DEG_3 31 23257 #define SEP_3 3 23258 23259 23260 /* Protected by tcp_random_lock */ 23261 static int tcp_randtbl[DEG_3 + 1]; 23262 23263 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23264 static int *tcp_random_rptr = &tcp_randtbl[1]; 23265 23266 static int *tcp_random_state = &tcp_randtbl[1]; 23267 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23268 23269 kmutex_t tcp_random_lock; 23270 23271 void 23272 tcp_random_init(void) 23273 { 23274 int i; 23275 hrtime_t hrt; 23276 time_t wallclock; 23277 uint64_t result; 23278 23279 /* 23280 * Use high-res timer and current time for seed. Gethrtime() returns 23281 * a longlong, which may contain resolution down to nanoseconds. 23282 * The current time will either be a 32-bit or a 64-bit quantity. 23283 * XOR the two together in a 64-bit result variable. 23284 * Convert the result to a 32-bit value by multiplying the high-order 23285 * 32-bits by the low-order 32-bits. 23286 */ 23287 23288 hrt = gethrtime(); 23289 (void) drv_getparm(TIME, &wallclock); 23290 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23291 mutex_enter(&tcp_random_lock); 23292 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23293 (result & 0xffffffff); 23294 23295 for (i = 1; i < DEG_3; i++) 23296 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23297 + 12345; 23298 tcp_random_fptr = &tcp_random_state[SEP_3]; 23299 tcp_random_rptr = &tcp_random_state[0]; 23300 mutex_exit(&tcp_random_lock); 23301 for (i = 0; i < 10 * DEG_3; i++) 23302 (void) tcp_random(); 23303 } 23304 23305 /* 23306 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23307 * This range is selected to be approximately centered on TCP_ISS / 2, 23308 * and easy to compute. We get this value by generating a 32-bit random 23309 * number, selecting out the high-order 17 bits, and then adding one so 23310 * that we never return zero. 23311 */ 23312 int 23313 tcp_random(void) 23314 { 23315 int i; 23316 23317 mutex_enter(&tcp_random_lock); 23318 *tcp_random_fptr += *tcp_random_rptr; 23319 23320 /* 23321 * The high-order bits are more random than the low-order bits, 23322 * so we select out the high-order 17 bits and add one so that 23323 * we never return zero. 23324 */ 23325 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23326 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23327 tcp_random_fptr = tcp_random_state; 23328 ++tcp_random_rptr; 23329 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23330 tcp_random_rptr = tcp_random_state; 23331 23332 mutex_exit(&tcp_random_lock); 23333 return (i); 23334 } 23335 23336 static int 23337 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 23338 int *t_errorp, int *sys_errorp) 23339 { 23340 int error; 23341 int is_absreq_failure; 23342 t_scalar_t *opt_lenp; 23343 t_scalar_t opt_offset; 23344 int prim_type; 23345 struct T_conn_req *tcreqp; 23346 struct T_conn_res *tcresp; 23347 cred_t *cr; 23348 23349 /* 23350 * All Solaris components should pass a db_credp 23351 * for this TPI message, hence we ASSERT. 23352 * But in case there is some other M_PROTO that looks 23353 * like a TPI message sent by some other kernel 23354 * component, we check and return an error. 23355 */ 23356 cr = msg_getcred(mp, NULL); 23357 ASSERT(cr != NULL); 23358 if (cr == NULL) 23359 return (-1); 23360 23361 prim_type = ((union T_primitives *)mp->b_rptr)->type; 23362 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 23363 prim_type == T_CONN_RES); 23364 23365 switch (prim_type) { 23366 case T_CONN_REQ: 23367 tcreqp = (struct T_conn_req *)mp->b_rptr; 23368 opt_offset = tcreqp->OPT_offset; 23369 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 23370 break; 23371 case O_T_CONN_RES: 23372 case T_CONN_RES: 23373 tcresp = (struct T_conn_res *)mp->b_rptr; 23374 opt_offset = tcresp->OPT_offset; 23375 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 23376 break; 23377 } 23378 23379 *t_errorp = 0; 23380 *sys_errorp = 0; 23381 *do_disconnectp = 0; 23382 23383 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 23384 opt_offset, cr, &tcp_opt_obj, 23385 NULL, &is_absreq_failure); 23386 23387 switch (error) { 23388 case 0: /* no error */ 23389 ASSERT(is_absreq_failure == 0); 23390 return (0); 23391 case ENOPROTOOPT: 23392 *t_errorp = TBADOPT; 23393 break; 23394 case EACCES: 23395 *t_errorp = TACCES; 23396 break; 23397 default: 23398 *t_errorp = TSYSERR; *sys_errorp = error; 23399 break; 23400 } 23401 if (is_absreq_failure != 0) { 23402 /* 23403 * The connection request should get the local ack 23404 * T_OK_ACK and then a T_DISCON_IND. 23405 */ 23406 *do_disconnectp = 1; 23407 } 23408 return (-1); 23409 } 23410 23411 /* 23412 * Split this function out so that if the secret changes, I'm okay. 23413 * 23414 * Initialize the tcp_iss_cookie and tcp_iss_key. 23415 */ 23416 23417 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 23418 23419 static void 23420 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 23421 { 23422 struct { 23423 int32_t current_time; 23424 uint32_t randnum; 23425 uint16_t pad; 23426 uint8_t ether[6]; 23427 uint8_t passwd[PASSWD_SIZE]; 23428 } tcp_iss_cookie; 23429 time_t t; 23430 23431 /* 23432 * Start with the current absolute time. 23433 */ 23434 (void) drv_getparm(TIME, &t); 23435 tcp_iss_cookie.current_time = t; 23436 23437 /* 23438 * XXX - Need a more random number per RFC 1750, not this crap. 23439 * OTOH, if what follows is pretty random, then I'm in better shape. 23440 */ 23441 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 23442 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 23443 23444 /* 23445 * The cpu_type_info is pretty non-random. Ugggh. It does serve 23446 * as a good template. 23447 */ 23448 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 23449 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 23450 23451 /* 23452 * The pass-phrase. Normally this is supplied by user-called NDD. 23453 */ 23454 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 23455 23456 /* 23457 * See 4010593 if this section becomes a problem again, 23458 * but the local ethernet address is useful here. 23459 */ 23460 (void) localetheraddr(NULL, 23461 (struct ether_addr *)&tcp_iss_cookie.ether); 23462 23463 /* 23464 * Hash 'em all together. The MD5Final is called per-connection. 23465 */ 23466 mutex_enter(&tcps->tcps_iss_key_lock); 23467 MD5Init(&tcps->tcps_iss_key); 23468 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 23469 sizeof (tcp_iss_cookie)); 23470 mutex_exit(&tcps->tcps_iss_key_lock); 23471 } 23472 23473 /* 23474 * Set the RFC 1948 pass phrase 23475 */ 23476 /* ARGSUSED */ 23477 static int 23478 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23479 cred_t *cr) 23480 { 23481 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23482 23483 /* 23484 * Basically, value contains a new pass phrase. Pass it along! 23485 */ 23486 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 23487 return (0); 23488 } 23489 23490 /* ARGSUSED */ 23491 static int 23492 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 23493 { 23494 bzero(buf, sizeof (tcp_sack_info_t)); 23495 return (0); 23496 } 23497 23498 /* ARGSUSED */ 23499 static int 23500 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 23501 { 23502 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 23503 return (0); 23504 } 23505 23506 /* 23507 * Make sure we wait until the default queue is setup, yet allow 23508 * tcp_g_q_create() to open a TCP stream. 23509 * We need to allow tcp_g_q_create() do do an open 23510 * of tcp, hence we compare curhread. 23511 * All others have to wait until the tcps_g_q has been 23512 * setup. 23513 */ 23514 void 23515 tcp_g_q_setup(tcp_stack_t *tcps) 23516 { 23517 mutex_enter(&tcps->tcps_g_q_lock); 23518 if (tcps->tcps_g_q != NULL) { 23519 mutex_exit(&tcps->tcps_g_q_lock); 23520 return; 23521 } 23522 if (tcps->tcps_g_q_creator == NULL) { 23523 /* This thread will set it up */ 23524 tcps->tcps_g_q_creator = curthread; 23525 mutex_exit(&tcps->tcps_g_q_lock); 23526 tcp_g_q_create(tcps); 23527 mutex_enter(&tcps->tcps_g_q_lock); 23528 ASSERT(tcps->tcps_g_q_creator == curthread); 23529 tcps->tcps_g_q_creator = NULL; 23530 cv_signal(&tcps->tcps_g_q_cv); 23531 ASSERT(tcps->tcps_g_q != NULL); 23532 mutex_exit(&tcps->tcps_g_q_lock); 23533 return; 23534 } 23535 /* Everybody but the creator has to wait */ 23536 if (tcps->tcps_g_q_creator != curthread) { 23537 while (tcps->tcps_g_q == NULL) 23538 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 23539 } 23540 mutex_exit(&tcps->tcps_g_q_lock); 23541 } 23542 23543 #define IP "ip" 23544 23545 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 23546 23547 /* 23548 * Create a default tcp queue here instead of in strplumb 23549 */ 23550 void 23551 tcp_g_q_create(tcp_stack_t *tcps) 23552 { 23553 int error; 23554 ldi_handle_t lh = NULL; 23555 ldi_ident_t li = NULL; 23556 int rval; 23557 cred_t *cr; 23558 major_t IP_MAJ; 23559 23560 #ifdef NS_DEBUG 23561 (void) printf("tcp_g_q_create()\n"); 23562 #endif 23563 23564 IP_MAJ = ddi_name_to_major(IP); 23565 23566 ASSERT(tcps->tcps_g_q_creator == curthread); 23567 23568 error = ldi_ident_from_major(IP_MAJ, &li); 23569 if (error) { 23570 #ifdef DEBUG 23571 printf("tcp_g_q_create: lyr ident get failed error %d\n", 23572 error); 23573 #endif 23574 return; 23575 } 23576 23577 cr = zone_get_kcred(netstackid_to_zoneid( 23578 tcps->tcps_netstack->netstack_stackid)); 23579 ASSERT(cr != NULL); 23580 /* 23581 * We set the tcp default queue to IPv6 because IPv4 falls 23582 * back to IPv6 when it can't find a client, but 23583 * IPv6 does not fall back to IPv4. 23584 */ 23585 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 23586 if (error) { 23587 #ifdef DEBUG 23588 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 23589 error); 23590 #endif 23591 goto out; 23592 } 23593 23594 /* 23595 * This ioctl causes the tcp framework to cache a pointer to 23596 * this stream, so we don't want to close the stream after 23597 * this operation. 23598 * Use the kernel credentials that are for the zone we're in. 23599 */ 23600 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 23601 (intptr_t)0, FKIOCTL, cr, &rval); 23602 if (error) { 23603 #ifdef DEBUG 23604 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 23605 "error %d\n", error); 23606 #endif 23607 goto out; 23608 } 23609 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 23610 lh = NULL; 23611 out: 23612 /* Close layered handles */ 23613 if (li) 23614 ldi_ident_release(li); 23615 /* Keep cred around until _inactive needs it */ 23616 tcps->tcps_g_q_cr = cr; 23617 } 23618 23619 /* 23620 * We keep tcp_g_q set until all other tcp_t's in the zone 23621 * has gone away, and then when tcp_g_q_inactive() is called 23622 * we clear it. 23623 */ 23624 void 23625 tcp_g_q_destroy(tcp_stack_t *tcps) 23626 { 23627 #ifdef NS_DEBUG 23628 (void) printf("tcp_g_q_destroy()for stack %d\n", 23629 tcps->tcps_netstack->netstack_stackid); 23630 #endif 23631 23632 if (tcps->tcps_g_q == NULL) { 23633 return; /* Nothing to cleanup */ 23634 } 23635 /* 23636 * Drop reference corresponding to the default queue. 23637 * This reference was added from tcp_open when the default queue 23638 * was created, hence we compensate for this extra drop in 23639 * tcp_g_q_close. If the refcnt drops to zero here it means 23640 * the default queue was the last one to be open, in which 23641 * case, then tcp_g_q_inactive will be 23642 * called as a result of the refrele. 23643 */ 23644 TCPS_REFRELE(tcps); 23645 } 23646 23647 /* 23648 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23649 * Run by tcp_q_q_inactive using a taskq. 23650 */ 23651 static void 23652 tcp_g_q_close(void *arg) 23653 { 23654 tcp_stack_t *tcps = arg; 23655 int error; 23656 ldi_handle_t lh = NULL; 23657 ldi_ident_t li = NULL; 23658 cred_t *cr; 23659 major_t IP_MAJ; 23660 23661 IP_MAJ = ddi_name_to_major(IP); 23662 23663 #ifdef NS_DEBUG 23664 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 23665 tcps->tcps_netstack->netstack_stackid, 23666 tcps->tcps_netstack->netstack_refcnt); 23667 #endif 23668 lh = tcps->tcps_g_q_lh; 23669 if (lh == NULL) 23670 return; /* Nothing to cleanup */ 23671 23672 ASSERT(tcps->tcps_refcnt == 1); 23673 ASSERT(tcps->tcps_g_q != NULL); 23674 23675 error = ldi_ident_from_major(IP_MAJ, &li); 23676 if (error) { 23677 #ifdef DEBUG 23678 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 23679 error); 23680 #endif 23681 return; 23682 } 23683 23684 cr = tcps->tcps_g_q_cr; 23685 tcps->tcps_g_q_cr = NULL; 23686 ASSERT(cr != NULL); 23687 23688 /* 23689 * Make sure we can break the recursion when tcp_close decrements 23690 * the reference count causing g_q_inactive to be called again. 23691 */ 23692 tcps->tcps_g_q_lh = NULL; 23693 23694 /* close the default queue */ 23695 (void) ldi_close(lh, FREAD|FWRITE, cr); 23696 /* 23697 * At this point in time tcps and the rest of netstack_t might 23698 * have been deleted. 23699 */ 23700 tcps = NULL; 23701 23702 /* Close layered handles */ 23703 ldi_ident_release(li); 23704 crfree(cr); 23705 } 23706 23707 /* 23708 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23709 * 23710 * Have to ensure that the ldi routines are not used by an 23711 * interrupt thread by using a taskq. 23712 */ 23713 void 23714 tcp_g_q_inactive(tcp_stack_t *tcps) 23715 { 23716 if (tcps->tcps_g_q_lh == NULL) 23717 return; /* Nothing to cleanup */ 23718 23719 ASSERT(tcps->tcps_refcnt == 0); 23720 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 23721 23722 if (servicing_interrupt()) { 23723 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 23724 (void *) tcps, TQ_SLEEP); 23725 } else { 23726 tcp_g_q_close(tcps); 23727 } 23728 } 23729 23730 /* 23731 * Called by IP when IP is loaded into the kernel 23732 */ 23733 void 23734 tcp_ddi_g_init(void) 23735 { 23736 tcp_timercache = kmem_cache_create("tcp_timercache", 23737 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 23738 NULL, NULL, NULL, NULL, NULL, 0); 23739 23740 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 23741 sizeof (tcp_sack_info_t), 0, 23742 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 23743 23744 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 23745 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 23746 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 23747 23748 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 23749 23750 /* Initialize the random number generator */ 23751 tcp_random_init(); 23752 23753 /* A single callback independently of how many netstacks we have */ 23754 ip_squeue_init(tcp_squeue_add); 23755 23756 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 23757 23758 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 23759 TASKQ_PREPOPULATE); 23760 23761 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 23762 23763 /* 23764 * We want to be informed each time a stack is created or 23765 * destroyed in the kernel, so we can maintain the 23766 * set of tcp_stack_t's. 23767 */ 23768 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 23769 tcp_stack_fini); 23770 } 23771 23772 23773 #define INET_NAME "ip" 23774 23775 /* 23776 * Initialize the TCP stack instance. 23777 */ 23778 static void * 23779 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 23780 { 23781 tcp_stack_t *tcps; 23782 tcpparam_t *pa; 23783 int i; 23784 int error = 0; 23785 major_t major; 23786 23787 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 23788 tcps->tcps_netstack = ns; 23789 23790 /* Initialize locks */ 23791 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 23792 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 23793 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 23794 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 23795 23796 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 23797 tcps->tcps_g_epriv_ports[0] = 2049; 23798 tcps->tcps_g_epriv_ports[1] = 4045; 23799 tcps->tcps_min_anonpriv_port = 512; 23800 23801 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 23802 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 23803 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 23804 TCP_FANOUT_SIZE, KM_SLEEP); 23805 23806 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 23807 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 23808 MUTEX_DEFAULT, NULL); 23809 } 23810 23811 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 23812 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 23813 MUTEX_DEFAULT, NULL); 23814 } 23815 23816 /* TCP's IPsec code calls the packet dropper. */ 23817 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 23818 23819 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 23820 tcps->tcps_params = pa; 23821 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 23822 23823 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 23824 A_CNT(lcl_tcp_param_arr), tcps); 23825 23826 /* 23827 * Note: To really walk the device tree you need the devinfo 23828 * pointer to your device which is only available after probe/attach. 23829 * The following is safe only because it uses ddi_root_node() 23830 */ 23831 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 23832 tcp_opt_obj.odb_opt_arr_cnt); 23833 23834 /* 23835 * Initialize RFC 1948 secret values. This will probably be reset once 23836 * by the boot scripts. 23837 * 23838 * Use NULL name, as the name is caught by the new lockstats. 23839 * 23840 * Initialize with some random, non-guessable string, like the global 23841 * T_INFO_ACK. 23842 */ 23843 23844 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 23845 sizeof (tcp_g_t_info_ack), tcps); 23846 23847 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 23848 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 23849 23850 major = mod_name_to_major(INET_NAME); 23851 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 23852 ASSERT(error == 0); 23853 return (tcps); 23854 } 23855 23856 /* 23857 * Called when the IP module is about to be unloaded. 23858 */ 23859 void 23860 tcp_ddi_g_destroy(void) 23861 { 23862 tcp_g_kstat_fini(tcp_g_kstat); 23863 tcp_g_kstat = NULL; 23864 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 23865 23866 mutex_destroy(&tcp_random_lock); 23867 23868 kmem_cache_destroy(tcp_timercache); 23869 kmem_cache_destroy(tcp_sack_info_cache); 23870 kmem_cache_destroy(tcp_iphc_cache); 23871 23872 netstack_unregister(NS_TCP); 23873 taskq_destroy(tcp_taskq); 23874 } 23875 23876 /* 23877 * Shut down the TCP stack instance. 23878 */ 23879 /* ARGSUSED */ 23880 static void 23881 tcp_stack_shutdown(netstackid_t stackid, void *arg) 23882 { 23883 tcp_stack_t *tcps = (tcp_stack_t *)arg; 23884 23885 tcp_g_q_destroy(tcps); 23886 } 23887 23888 /* 23889 * Free the TCP stack instance. 23890 */ 23891 static void 23892 tcp_stack_fini(netstackid_t stackid, void *arg) 23893 { 23894 tcp_stack_t *tcps = (tcp_stack_t *)arg; 23895 int i; 23896 23897 nd_free(&tcps->tcps_g_nd); 23898 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 23899 tcps->tcps_params = NULL; 23900 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 23901 tcps->tcps_wroff_xtra_param = NULL; 23902 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 23903 tcps->tcps_mdt_head_param = NULL; 23904 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 23905 tcps->tcps_mdt_tail_param = NULL; 23906 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 23907 tcps->tcps_mdt_max_pbufs_param = NULL; 23908 23909 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 23910 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 23911 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 23912 } 23913 23914 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 23915 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 23916 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 23917 } 23918 23919 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 23920 tcps->tcps_bind_fanout = NULL; 23921 23922 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 23923 tcps->tcps_acceptor_fanout = NULL; 23924 23925 mutex_destroy(&tcps->tcps_iss_key_lock); 23926 mutex_destroy(&tcps->tcps_g_q_lock); 23927 cv_destroy(&tcps->tcps_g_q_cv); 23928 mutex_destroy(&tcps->tcps_epriv_port_lock); 23929 23930 ip_drop_unregister(&tcps->tcps_dropper); 23931 23932 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 23933 tcps->tcps_kstat = NULL; 23934 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 23935 23936 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 23937 tcps->tcps_mibkp = NULL; 23938 23939 ldi_ident_release(tcps->tcps_ldi_ident); 23940 kmem_free(tcps, sizeof (*tcps)); 23941 } 23942 23943 /* 23944 * Generate ISS, taking into account NDD changes may happen halfway through. 23945 * (If the iss is not zero, set it.) 23946 */ 23947 23948 static void 23949 tcp_iss_init(tcp_t *tcp) 23950 { 23951 MD5_CTX context; 23952 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 23953 uint32_t answer[4]; 23954 tcp_stack_t *tcps = tcp->tcp_tcps; 23955 23956 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 23957 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 23958 switch (tcps->tcps_strong_iss) { 23959 case 2: 23960 mutex_enter(&tcps->tcps_iss_key_lock); 23961 context = tcps->tcps_iss_key; 23962 mutex_exit(&tcps->tcps_iss_key_lock); 23963 arg.ports = tcp->tcp_ports; 23964 if (tcp->tcp_ipversion == IPV4_VERSION) { 23965 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 23966 &arg.src); 23967 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 23968 &arg.dst); 23969 } else { 23970 arg.src = tcp->tcp_ip6h->ip6_src; 23971 arg.dst = tcp->tcp_ip6h->ip6_dst; 23972 } 23973 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 23974 MD5Final((uchar_t *)answer, &context); 23975 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 23976 /* 23977 * Now that we've hashed into a unique per-connection sequence 23978 * space, add a random increment per strong_iss == 1. So I 23979 * guess we'll have to... 23980 */ 23981 /* FALLTHRU */ 23982 case 1: 23983 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 23984 break; 23985 default: 23986 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 23987 break; 23988 } 23989 tcp->tcp_valid_bits = TCP_ISS_VALID; 23990 tcp->tcp_fss = tcp->tcp_iss - 1; 23991 tcp->tcp_suna = tcp->tcp_iss; 23992 tcp->tcp_snxt = tcp->tcp_iss + 1; 23993 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23994 tcp->tcp_csuna = tcp->tcp_snxt; 23995 } 23996 23997 /* 23998 * Exported routine for extracting active tcp connection status. 23999 * 24000 * This is used by the Solaris Cluster Networking software to 24001 * gather a list of connections that need to be forwarded to 24002 * specific nodes in the cluster when configuration changes occur. 24003 * 24004 * The callback is invoked for each tcp_t structure from all netstacks, 24005 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 24006 * from the netstack with the specified stack_id. Returning 24007 * non-zero from the callback routine terminates the search. 24008 */ 24009 int 24010 cl_tcp_walk_list(netstackid_t stack_id, 24011 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 24012 { 24013 netstack_handle_t nh; 24014 netstack_t *ns; 24015 int ret = 0; 24016 24017 if (stack_id >= 0) { 24018 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 24019 return (EINVAL); 24020 24021 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24022 ns->netstack_tcp); 24023 netstack_rele(ns); 24024 return (ret); 24025 } 24026 24027 netstack_next_init(&nh); 24028 while ((ns = netstack_next(&nh)) != NULL) { 24029 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24030 ns->netstack_tcp); 24031 netstack_rele(ns); 24032 } 24033 netstack_next_fini(&nh); 24034 return (ret); 24035 } 24036 24037 static int 24038 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 24039 tcp_stack_t *tcps) 24040 { 24041 tcp_t *tcp; 24042 cl_tcp_info_t cl_tcpi; 24043 connf_t *connfp; 24044 conn_t *connp; 24045 int i; 24046 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24047 24048 ASSERT(callback != NULL); 24049 24050 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24051 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 24052 connp = NULL; 24053 24054 while ((connp = 24055 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24056 24057 tcp = connp->conn_tcp; 24058 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 24059 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 24060 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 24061 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 24062 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 24063 /* 24064 * The macros tcp_laddr and tcp_faddr give the IPv4 24065 * addresses. They are copied implicitly below as 24066 * mapped addresses. 24067 */ 24068 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 24069 if (tcp->tcp_ipversion == IPV4_VERSION) { 24070 cl_tcpi.cl_tcpi_faddr = 24071 tcp->tcp_ipha->ipha_dst; 24072 } else { 24073 cl_tcpi.cl_tcpi_faddr_v6 = 24074 tcp->tcp_ip6h->ip6_dst; 24075 } 24076 24077 /* 24078 * If the callback returns non-zero 24079 * we terminate the traversal. 24080 */ 24081 if ((*callback)(&cl_tcpi, arg) != 0) { 24082 CONN_DEC_REF(tcp->tcp_connp); 24083 return (1); 24084 } 24085 } 24086 } 24087 24088 return (0); 24089 } 24090 24091 /* 24092 * Macros used for accessing the different types of sockaddr 24093 * structures inside a tcp_ioc_abort_conn_t. 24094 */ 24095 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 24096 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 24097 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 24098 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 24099 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 24100 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 24101 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 24102 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 24103 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 24104 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 24105 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 24106 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 24107 24108 /* 24109 * Return the correct error code to mimic the behavior 24110 * of a connection reset. 24111 */ 24112 #define TCP_AC_GET_ERRCODE(state, err) { \ 24113 switch ((state)) { \ 24114 case TCPS_SYN_SENT: \ 24115 case TCPS_SYN_RCVD: \ 24116 (err) = ECONNREFUSED; \ 24117 break; \ 24118 case TCPS_ESTABLISHED: \ 24119 case TCPS_FIN_WAIT_1: \ 24120 case TCPS_FIN_WAIT_2: \ 24121 case TCPS_CLOSE_WAIT: \ 24122 (err) = ECONNRESET; \ 24123 break; \ 24124 case TCPS_CLOSING: \ 24125 case TCPS_LAST_ACK: \ 24126 case TCPS_TIME_WAIT: \ 24127 (err) = 0; \ 24128 break; \ 24129 default: \ 24130 (err) = ENXIO; \ 24131 } \ 24132 } 24133 24134 /* 24135 * Check if a tcp structure matches the info in acp. 24136 */ 24137 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 24138 (((acp)->ac_local.ss_family == AF_INET) ? \ 24139 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 24140 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 24141 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 24142 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 24143 (TCP_AC_V4LPORT((acp)) == 0 || \ 24144 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 24145 (TCP_AC_V4RPORT((acp)) == 0 || \ 24146 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24147 (acp)->ac_start <= (tcp)->tcp_state && \ 24148 (acp)->ac_end >= (tcp)->tcp_state) : \ 24149 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24150 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24151 &(tcp)->tcp_ip_src_v6)) && \ 24152 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24153 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24154 &(tcp)->tcp_remote_v6)) && \ 24155 (TCP_AC_V6LPORT((acp)) == 0 || \ 24156 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24157 (TCP_AC_V6RPORT((acp)) == 0 || \ 24158 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24159 (acp)->ac_start <= (tcp)->tcp_state && \ 24160 (acp)->ac_end >= (tcp)->tcp_state)) 24161 24162 #define TCP_AC_MATCH(acp, tcp) \ 24163 (((acp)->ac_zoneid == ALL_ZONES || \ 24164 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24165 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24166 24167 /* 24168 * Build a message containing a tcp_ioc_abort_conn_t structure 24169 * which is filled in with information from acp and tp. 24170 */ 24171 static mblk_t * 24172 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24173 { 24174 mblk_t *mp; 24175 tcp_ioc_abort_conn_t *tacp; 24176 24177 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24178 if (mp == NULL) 24179 return (NULL); 24180 24181 mp->b_datap->db_type = M_CTL; 24182 24183 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24184 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24185 sizeof (uint32_t)); 24186 24187 tacp->ac_start = acp->ac_start; 24188 tacp->ac_end = acp->ac_end; 24189 tacp->ac_zoneid = acp->ac_zoneid; 24190 24191 if (acp->ac_local.ss_family == AF_INET) { 24192 tacp->ac_local.ss_family = AF_INET; 24193 tacp->ac_remote.ss_family = AF_INET; 24194 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24195 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24196 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24197 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24198 } else { 24199 tacp->ac_local.ss_family = AF_INET6; 24200 tacp->ac_remote.ss_family = AF_INET6; 24201 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24202 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24203 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24204 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24205 } 24206 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24207 return (mp); 24208 } 24209 24210 /* 24211 * Print a tcp_ioc_abort_conn_t structure. 24212 */ 24213 static void 24214 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24215 { 24216 char lbuf[128]; 24217 char rbuf[128]; 24218 sa_family_t af; 24219 in_port_t lport, rport; 24220 ushort_t logflags; 24221 24222 af = acp->ac_local.ss_family; 24223 24224 if (af == AF_INET) { 24225 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24226 lbuf, 128); 24227 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24228 rbuf, 128); 24229 lport = ntohs(TCP_AC_V4LPORT(acp)); 24230 rport = ntohs(TCP_AC_V4RPORT(acp)); 24231 } else { 24232 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24233 lbuf, 128); 24234 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24235 rbuf, 128); 24236 lport = ntohs(TCP_AC_V6LPORT(acp)); 24237 rport = ntohs(TCP_AC_V6RPORT(acp)); 24238 } 24239 24240 logflags = SL_TRACE | SL_NOTE; 24241 /* 24242 * Don't print this message to the console if the operation was done 24243 * to a non-global zone. 24244 */ 24245 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24246 logflags |= SL_CONSOLE; 24247 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24248 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24249 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24250 acp->ac_start, acp->ac_end); 24251 } 24252 24253 /* 24254 * Called inside tcp_rput when a message built using 24255 * tcp_ioctl_abort_build_msg is put into a queue. 24256 * Note that when we get here there is no wildcard in acp any more. 24257 */ 24258 static void 24259 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24260 { 24261 tcp_ioc_abort_conn_t *acp; 24262 24263 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24264 if (tcp->tcp_state <= acp->ac_end) { 24265 /* 24266 * If we get here, we are already on the correct 24267 * squeue. This ioctl follows the following path 24268 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24269 * ->tcp_ioctl_abort->squeue_enter (if on a 24270 * different squeue) 24271 */ 24272 int errcode; 24273 24274 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24275 (void) tcp_clean_death(tcp, errcode, 26); 24276 } 24277 freemsg(mp); 24278 } 24279 24280 /* 24281 * Abort all matching connections on a hash chain. 24282 */ 24283 static int 24284 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24285 boolean_t exact, tcp_stack_t *tcps) 24286 { 24287 int nmatch, err = 0; 24288 tcp_t *tcp; 24289 MBLKP mp, last, listhead = NULL; 24290 conn_t *tconnp; 24291 connf_t *connfp; 24292 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24293 24294 connfp = &ipst->ips_ipcl_conn_fanout[index]; 24295 24296 startover: 24297 nmatch = 0; 24298 24299 mutex_enter(&connfp->connf_lock); 24300 for (tconnp = connfp->connf_head; tconnp != NULL; 24301 tconnp = tconnp->conn_next) { 24302 tcp = tconnp->conn_tcp; 24303 if (TCP_AC_MATCH(acp, tcp)) { 24304 CONN_INC_REF(tcp->tcp_connp); 24305 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24306 if (mp == NULL) { 24307 err = ENOMEM; 24308 CONN_DEC_REF(tcp->tcp_connp); 24309 break; 24310 } 24311 mp->b_prev = (mblk_t *)tcp; 24312 24313 if (listhead == NULL) { 24314 listhead = mp; 24315 last = mp; 24316 } else { 24317 last->b_next = mp; 24318 last = mp; 24319 } 24320 nmatch++; 24321 if (exact) 24322 break; 24323 } 24324 24325 /* Avoid holding lock for too long. */ 24326 if (nmatch >= 500) 24327 break; 24328 } 24329 mutex_exit(&connfp->connf_lock); 24330 24331 /* Pass mp into the correct tcp */ 24332 while ((mp = listhead) != NULL) { 24333 listhead = listhead->b_next; 24334 tcp = (tcp_t *)mp->b_prev; 24335 mp->b_next = mp->b_prev = NULL; 24336 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 24337 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 24338 } 24339 24340 *count += nmatch; 24341 if (nmatch >= 500 && err == 0) 24342 goto startover; 24343 return (err); 24344 } 24345 24346 /* 24347 * Abort all connections that matches the attributes specified in acp. 24348 */ 24349 static int 24350 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 24351 { 24352 sa_family_t af; 24353 uint32_t ports; 24354 uint16_t *pports; 24355 int err = 0, count = 0; 24356 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24357 int index = -1; 24358 ushort_t logflags; 24359 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24360 24361 af = acp->ac_local.ss_family; 24362 24363 if (af == AF_INET) { 24364 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24365 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24366 pports = (uint16_t *)&ports; 24367 pports[1] = TCP_AC_V4LPORT(acp); 24368 pports[0] = TCP_AC_V4RPORT(acp); 24369 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24370 } 24371 } else { 24372 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 24373 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 24374 pports = (uint16_t *)&ports; 24375 pports[1] = TCP_AC_V6LPORT(acp); 24376 pports[0] = TCP_AC_V6RPORT(acp); 24377 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 24378 } 24379 } 24380 24381 /* 24382 * For cases where remote addr, local port, and remote port are non- 24383 * wildcards, tcp_ioctl_abort_bucket will only be called once. 24384 */ 24385 if (index != -1) { 24386 err = tcp_ioctl_abort_bucket(acp, index, 24387 &count, exact, tcps); 24388 } else { 24389 /* 24390 * loop through all entries for wildcard case 24391 */ 24392 for (index = 0; 24393 index < ipst->ips_ipcl_conn_fanout_size; 24394 index++) { 24395 err = tcp_ioctl_abort_bucket(acp, index, 24396 &count, exact, tcps); 24397 if (err != 0) 24398 break; 24399 } 24400 } 24401 24402 logflags = SL_TRACE | SL_NOTE; 24403 /* 24404 * Don't print this message to the console if the operation was done 24405 * to a non-global zone. 24406 */ 24407 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24408 logflags |= SL_CONSOLE; 24409 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 24410 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 24411 if (err == 0 && count == 0) 24412 err = ENOENT; 24413 return (err); 24414 } 24415 24416 /* 24417 * Process the TCP_IOC_ABORT_CONN ioctl request. 24418 */ 24419 static void 24420 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 24421 { 24422 int err; 24423 IOCP iocp; 24424 MBLKP mp1; 24425 sa_family_t laf, raf; 24426 tcp_ioc_abort_conn_t *acp; 24427 zone_t *zptr; 24428 conn_t *connp = Q_TO_CONN(q); 24429 zoneid_t zoneid = connp->conn_zoneid; 24430 tcp_t *tcp = connp->conn_tcp; 24431 tcp_stack_t *tcps = tcp->tcp_tcps; 24432 24433 iocp = (IOCP)mp->b_rptr; 24434 24435 if ((mp1 = mp->b_cont) == NULL || 24436 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 24437 err = EINVAL; 24438 goto out; 24439 } 24440 24441 /* check permissions */ 24442 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 24443 err = EPERM; 24444 goto out; 24445 } 24446 24447 if (mp1->b_cont != NULL) { 24448 freemsg(mp1->b_cont); 24449 mp1->b_cont = NULL; 24450 } 24451 24452 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 24453 laf = acp->ac_local.ss_family; 24454 raf = acp->ac_remote.ss_family; 24455 24456 /* check that a zone with the supplied zoneid exists */ 24457 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 24458 zptr = zone_find_by_id(zoneid); 24459 if (zptr != NULL) { 24460 zone_rele(zptr); 24461 } else { 24462 err = EINVAL; 24463 goto out; 24464 } 24465 } 24466 24467 /* 24468 * For exclusive stacks we set the zoneid to zero 24469 * to make TCP operate as if in the global zone. 24470 */ 24471 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 24472 acp->ac_zoneid = GLOBAL_ZONEID; 24473 24474 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24475 acp->ac_start > acp->ac_end || laf != raf || 24476 (laf != AF_INET && laf != AF_INET6)) { 24477 err = EINVAL; 24478 goto out; 24479 } 24480 24481 tcp_ioctl_abort_dump(acp); 24482 err = tcp_ioctl_abort(acp, tcps); 24483 24484 out: 24485 if (mp1 != NULL) { 24486 freemsg(mp1); 24487 mp->b_cont = NULL; 24488 } 24489 24490 if (err != 0) 24491 miocnak(q, mp, 0, err); 24492 else 24493 miocack(q, mp, 0, 0); 24494 } 24495 24496 /* 24497 * tcp_time_wait_processing() handles processing of incoming packets when 24498 * the tcp is in the TIME_WAIT state. 24499 * A TIME_WAIT tcp that has an associated open TCP stream is never put 24500 * on the time wait list. 24501 */ 24502 void 24503 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 24504 uint32_t seg_ack, int seg_len, tcph_t *tcph) 24505 { 24506 int32_t bytes_acked; 24507 int32_t gap; 24508 int32_t rgap; 24509 tcp_opt_t tcpopt; 24510 uint_t flags; 24511 uint32_t new_swnd = 0; 24512 conn_t *connp; 24513 tcp_stack_t *tcps = tcp->tcp_tcps; 24514 24515 BUMP_LOCAL(tcp->tcp_ibsegs); 24516 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 24517 24518 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 24519 new_swnd = BE16_TO_U16(tcph->th_win) << 24520 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 24521 if (tcp->tcp_snd_ts_ok) { 24522 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 24523 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24524 tcp->tcp_rnxt, TH_ACK); 24525 goto done; 24526 } 24527 } 24528 gap = seg_seq - tcp->tcp_rnxt; 24529 rgap = tcp->tcp_rwnd - (gap + seg_len); 24530 if (gap < 0) { 24531 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 24532 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 24533 (seg_len > -gap ? -gap : seg_len)); 24534 seg_len += gap; 24535 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 24536 if (flags & TH_RST) { 24537 goto done; 24538 } 24539 if ((flags & TH_FIN) && seg_len == -1) { 24540 /* 24541 * When TCP receives a duplicate FIN in 24542 * TIME_WAIT state, restart the 2 MSL timer. 24543 * See page 73 in RFC 793. Make sure this TCP 24544 * is already on the TIME_WAIT list. If not, 24545 * just restart the timer. 24546 */ 24547 if (TCP_IS_DETACHED(tcp)) { 24548 if (tcp_time_wait_remove(tcp, NULL) == 24549 B_TRUE) { 24550 tcp_time_wait_append(tcp); 24551 TCP_DBGSTAT(tcps, 24552 tcp_rput_time_wait); 24553 } 24554 } else { 24555 ASSERT(tcp != NULL); 24556 TCP_TIMER_RESTART(tcp, 24557 tcps->tcps_time_wait_interval); 24558 } 24559 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24560 tcp->tcp_rnxt, TH_ACK); 24561 goto done; 24562 } 24563 flags |= TH_ACK_NEEDED; 24564 seg_len = 0; 24565 goto process_ack; 24566 } 24567 24568 /* Fix seg_seq, and chew the gap off the front. */ 24569 seg_seq = tcp->tcp_rnxt; 24570 } 24571 24572 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 24573 /* 24574 * Make sure that when we accept the connection, pick 24575 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 24576 * old connection. 24577 * 24578 * The next ISS generated is equal to tcp_iss_incr_extra 24579 * + ISS_INCR/2 + other components depending on the 24580 * value of tcp_strong_iss. We pre-calculate the new 24581 * ISS here and compare with tcp_snxt to determine if 24582 * we need to make adjustment to tcp_iss_incr_extra. 24583 * 24584 * The above calculation is ugly and is a 24585 * waste of CPU cycles... 24586 */ 24587 uint32_t new_iss = tcps->tcps_iss_incr_extra; 24588 int32_t adj; 24589 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24590 24591 switch (tcps->tcps_strong_iss) { 24592 case 2: { 24593 /* Add time and MD5 components. */ 24594 uint32_t answer[4]; 24595 struct { 24596 uint32_t ports; 24597 in6_addr_t src; 24598 in6_addr_t dst; 24599 } arg; 24600 MD5_CTX context; 24601 24602 mutex_enter(&tcps->tcps_iss_key_lock); 24603 context = tcps->tcps_iss_key; 24604 mutex_exit(&tcps->tcps_iss_key_lock); 24605 arg.ports = tcp->tcp_ports; 24606 /* We use MAPPED addresses in tcp_iss_init */ 24607 arg.src = tcp->tcp_ip_src_v6; 24608 if (tcp->tcp_ipversion == IPV4_VERSION) { 24609 IN6_IPADDR_TO_V4MAPPED( 24610 tcp->tcp_ipha->ipha_dst, 24611 &arg.dst); 24612 } else { 24613 arg.dst = 24614 tcp->tcp_ip6h->ip6_dst; 24615 } 24616 MD5Update(&context, (uchar_t *)&arg, 24617 sizeof (arg)); 24618 MD5Final((uchar_t *)answer, &context); 24619 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 24620 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 24621 break; 24622 } 24623 case 1: 24624 /* Add time component and min random (i.e. 1). */ 24625 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 24626 break; 24627 default: 24628 /* Add only time component. */ 24629 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24630 break; 24631 } 24632 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 24633 /* 24634 * New ISS not guaranteed to be ISS_INCR/2 24635 * ahead of the current tcp_snxt, so add the 24636 * difference to tcp_iss_incr_extra. 24637 */ 24638 tcps->tcps_iss_incr_extra += adj; 24639 } 24640 /* 24641 * If tcp_clean_death() can not perform the task now, 24642 * drop the SYN packet and let the other side re-xmit. 24643 * Otherwise pass the SYN packet back in, since the 24644 * old tcp state has been cleaned up or freed. 24645 */ 24646 if (tcp_clean_death(tcp, 0, 27) == -1) 24647 goto done; 24648 /* 24649 * We will come back to tcp_rput_data 24650 * on the global queue. Packets destined 24651 * for the global queue will be checked 24652 * with global policy. But the policy for 24653 * this packet has already been checked as 24654 * this was destined for the detached 24655 * connection. We need to bypass policy 24656 * check this time by attaching a dummy 24657 * ipsec_in with ipsec_in_dont_check set. 24658 */ 24659 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 24660 if (connp != NULL) { 24661 TCP_STAT(tcps, tcp_time_wait_syn_success); 24662 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 24663 return; 24664 } 24665 goto done; 24666 } 24667 24668 /* 24669 * rgap is the amount of stuff received out of window. A negative 24670 * value is the amount out of window. 24671 */ 24672 if (rgap < 0) { 24673 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 24674 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 24675 /* Fix seg_len and make sure there is something left. */ 24676 seg_len += rgap; 24677 if (seg_len <= 0) { 24678 if (flags & TH_RST) { 24679 goto done; 24680 } 24681 flags |= TH_ACK_NEEDED; 24682 seg_len = 0; 24683 goto process_ack; 24684 } 24685 } 24686 /* 24687 * Check whether we can update tcp_ts_recent. This test is 24688 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 24689 * Extensions for High Performance: An Update", Internet Draft. 24690 */ 24691 if (tcp->tcp_snd_ts_ok && 24692 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 24693 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 24694 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 24695 tcp->tcp_last_rcv_lbolt = lbolt64; 24696 } 24697 24698 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 24699 /* Always ack out of order packets */ 24700 flags |= TH_ACK_NEEDED; 24701 seg_len = 0; 24702 } else if (seg_len > 0) { 24703 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 24704 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 24705 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 24706 } 24707 if (flags & TH_RST) { 24708 (void) tcp_clean_death(tcp, 0, 28); 24709 goto done; 24710 } 24711 if (flags & TH_SYN) { 24712 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 24713 TH_RST|TH_ACK); 24714 /* 24715 * Do not delete the TCP structure if it is in 24716 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 24717 */ 24718 goto done; 24719 } 24720 process_ack: 24721 if (flags & TH_ACK) { 24722 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 24723 if (bytes_acked <= 0) { 24724 if (bytes_acked == 0 && seg_len == 0 && 24725 new_swnd == tcp->tcp_swnd) 24726 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 24727 } else { 24728 /* Acks something not sent */ 24729 flags |= TH_ACK_NEEDED; 24730 } 24731 } 24732 if (flags & TH_ACK_NEEDED) { 24733 /* 24734 * Time to send an ack for some reason. 24735 */ 24736 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24737 tcp->tcp_rnxt, TH_ACK); 24738 } 24739 done: 24740 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 24741 DB_CKSUMSTART(mp) = 0; 24742 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 24743 TCP_STAT(tcps, tcp_time_wait_syn_fail); 24744 } 24745 freemsg(mp); 24746 } 24747 24748 /* 24749 * TCP Timers Implementation. 24750 */ 24751 timeout_id_t 24752 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 24753 { 24754 mblk_t *mp; 24755 tcp_timer_t *tcpt; 24756 tcp_t *tcp = connp->conn_tcp; 24757 24758 ASSERT(connp->conn_sqp != NULL); 24759 24760 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 24761 24762 if (tcp->tcp_timercache == NULL) { 24763 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 24764 } else { 24765 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 24766 mp = tcp->tcp_timercache; 24767 tcp->tcp_timercache = mp->b_next; 24768 mp->b_next = NULL; 24769 ASSERT(mp->b_wptr == NULL); 24770 } 24771 24772 CONN_INC_REF(connp); 24773 tcpt = (tcp_timer_t *)mp->b_rptr; 24774 tcpt->connp = connp; 24775 tcpt->tcpt_proc = f; 24776 /* 24777 * TCP timers are normal timeouts. Plus, they do not require more than 24778 * a 10 millisecond resolution. By choosing a coarser resolution and by 24779 * rounding up the expiration to the next resolution boundary, we can 24780 * batch timers in the callout subsystem to make TCP timers more 24781 * efficient. The roundup also protects short timers from expiring too 24782 * early before they have a chance to be cancelled. 24783 */ 24784 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 24785 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 24786 24787 return ((timeout_id_t)mp); 24788 } 24789 24790 static void 24791 tcp_timer_callback(void *arg) 24792 { 24793 mblk_t *mp = (mblk_t *)arg; 24794 tcp_timer_t *tcpt; 24795 conn_t *connp; 24796 24797 tcpt = (tcp_timer_t *)mp->b_rptr; 24798 connp = tcpt->connp; 24799 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 24800 SQ_FILL, SQTAG_TCP_TIMER); 24801 } 24802 24803 static void 24804 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 24805 { 24806 tcp_timer_t *tcpt; 24807 conn_t *connp = (conn_t *)arg; 24808 tcp_t *tcp = connp->conn_tcp; 24809 24810 tcpt = (tcp_timer_t *)mp->b_rptr; 24811 ASSERT(connp == tcpt->connp); 24812 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 24813 24814 /* 24815 * If the TCP has reached the closed state, don't proceed any 24816 * further. This TCP logically does not exist on the system. 24817 * tcpt_proc could for example access queues, that have already 24818 * been qprocoff'ed off. Also see comments at the start of tcp_input 24819 */ 24820 if (tcp->tcp_state != TCPS_CLOSED) { 24821 (*tcpt->tcpt_proc)(connp); 24822 } else { 24823 tcp->tcp_timer_tid = 0; 24824 } 24825 tcp_timer_free(connp->conn_tcp, mp); 24826 } 24827 24828 /* 24829 * There is potential race with untimeout and the handler firing at the same 24830 * time. The mblock may be freed by the handler while we are trying to use 24831 * it. But since both should execute on the same squeue, this race should not 24832 * occur. 24833 */ 24834 clock_t 24835 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 24836 { 24837 mblk_t *mp = (mblk_t *)id; 24838 tcp_timer_t *tcpt; 24839 clock_t delta; 24840 24841 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 24842 24843 if (mp == NULL) 24844 return (-1); 24845 24846 tcpt = (tcp_timer_t *)mp->b_rptr; 24847 ASSERT(tcpt->connp == connp); 24848 24849 delta = untimeout_default(tcpt->tcpt_tid, 0); 24850 24851 if (delta >= 0) { 24852 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 24853 tcp_timer_free(connp->conn_tcp, mp); 24854 CONN_DEC_REF(connp); 24855 } 24856 24857 return (delta); 24858 } 24859 24860 /* 24861 * Allocate space for the timer event. The allocation looks like mblk, but it is 24862 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 24863 * 24864 * Dealing with failures: If we can't allocate from the timer cache we try 24865 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 24866 * points to b_rptr. 24867 * If we can't allocate anything using allocb_tryhard(), we perform a last 24868 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 24869 * save the actual allocation size in b_datap. 24870 */ 24871 mblk_t * 24872 tcp_timermp_alloc(int kmflags) 24873 { 24874 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 24875 kmflags & ~KM_PANIC); 24876 24877 if (mp != NULL) { 24878 mp->b_next = mp->b_prev = NULL; 24879 mp->b_rptr = (uchar_t *)(&mp[1]); 24880 mp->b_wptr = NULL; 24881 mp->b_datap = NULL; 24882 mp->b_queue = NULL; 24883 mp->b_cont = NULL; 24884 } else if (kmflags & KM_PANIC) { 24885 /* 24886 * Failed to allocate memory for the timer. Try allocating from 24887 * dblock caches. 24888 */ 24889 /* ipclassifier calls this from a constructor - hence no tcps */ 24890 TCP_G_STAT(tcp_timermp_allocfail); 24891 mp = allocb_tryhard(sizeof (tcp_timer_t)); 24892 if (mp == NULL) { 24893 size_t size = 0; 24894 /* 24895 * Memory is really low. Try tryhard allocation. 24896 * 24897 * ipclassifier calls this from a constructor - 24898 * hence no tcps 24899 */ 24900 TCP_G_STAT(tcp_timermp_allocdblfail); 24901 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 24902 sizeof (tcp_timer_t), &size, kmflags); 24903 mp->b_rptr = (uchar_t *)(&mp[1]); 24904 mp->b_next = mp->b_prev = NULL; 24905 mp->b_wptr = (uchar_t *)-1; 24906 mp->b_datap = (dblk_t *)size; 24907 mp->b_queue = NULL; 24908 mp->b_cont = NULL; 24909 } 24910 ASSERT(mp->b_wptr != NULL); 24911 } 24912 /* ipclassifier calls this from a constructor - hence no tcps */ 24913 TCP_G_DBGSTAT(tcp_timermp_alloced); 24914 24915 return (mp); 24916 } 24917 24918 /* 24919 * Free per-tcp timer cache. 24920 * It can only contain entries from tcp_timercache. 24921 */ 24922 void 24923 tcp_timermp_free(tcp_t *tcp) 24924 { 24925 mblk_t *mp; 24926 24927 while ((mp = tcp->tcp_timercache) != NULL) { 24928 ASSERT(mp->b_wptr == NULL); 24929 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 24930 kmem_cache_free(tcp_timercache, mp); 24931 } 24932 } 24933 24934 /* 24935 * Free timer event. Put it on the per-tcp timer cache if there is not too many 24936 * events there already (currently at most two events are cached). 24937 * If the event is not allocated from the timer cache, free it right away. 24938 */ 24939 static void 24940 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 24941 { 24942 mblk_t *mp1 = tcp->tcp_timercache; 24943 24944 if (mp->b_wptr != NULL) { 24945 /* 24946 * This allocation is not from a timer cache, free it right 24947 * away. 24948 */ 24949 if (mp->b_wptr != (uchar_t *)-1) 24950 freeb(mp); 24951 else 24952 kmem_free(mp, (size_t)mp->b_datap); 24953 } else if (mp1 == NULL || mp1->b_next == NULL) { 24954 /* Cache this timer block for future allocations */ 24955 mp->b_rptr = (uchar_t *)(&mp[1]); 24956 mp->b_next = mp1; 24957 tcp->tcp_timercache = mp; 24958 } else { 24959 kmem_cache_free(tcp_timercache, mp); 24960 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 24961 } 24962 } 24963 24964 /* 24965 * End of TCP Timers implementation. 24966 */ 24967 24968 /* 24969 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 24970 * on the specified backing STREAMS q. Note, the caller may make the 24971 * decision to call based on the tcp_t.tcp_flow_stopped value which 24972 * when check outside the q's lock is only an advisory check ... 24973 */ 24974 void 24975 tcp_setqfull(tcp_t *tcp) 24976 { 24977 tcp_stack_t *tcps = tcp->tcp_tcps; 24978 conn_t *connp = tcp->tcp_connp; 24979 24980 if (tcp->tcp_closed) 24981 return; 24982 24983 if (IPCL_IS_NONSTR(connp)) { 24984 (*connp->conn_upcalls->su_txq_full) 24985 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 24986 tcp->tcp_flow_stopped = B_TRUE; 24987 } else { 24988 queue_t *q = tcp->tcp_wq; 24989 24990 if (!(q->q_flag & QFULL)) { 24991 mutex_enter(QLOCK(q)); 24992 if (!(q->q_flag & QFULL)) { 24993 /* still need to set QFULL */ 24994 q->q_flag |= QFULL; 24995 tcp->tcp_flow_stopped = B_TRUE; 24996 mutex_exit(QLOCK(q)); 24997 TCP_STAT(tcps, tcp_flwctl_on); 24998 } else { 24999 mutex_exit(QLOCK(q)); 25000 } 25001 } 25002 } 25003 } 25004 25005 void 25006 tcp_clrqfull(tcp_t *tcp) 25007 { 25008 conn_t *connp = tcp->tcp_connp; 25009 25010 if (tcp->tcp_closed) 25011 return; 25012 25013 if (IPCL_IS_NONSTR(connp)) { 25014 (*connp->conn_upcalls->su_txq_full) 25015 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 25016 tcp->tcp_flow_stopped = B_FALSE; 25017 } else { 25018 queue_t *q = tcp->tcp_wq; 25019 25020 if (q->q_flag & QFULL) { 25021 mutex_enter(QLOCK(q)); 25022 if (q->q_flag & QFULL) { 25023 q->q_flag &= ~QFULL; 25024 tcp->tcp_flow_stopped = B_FALSE; 25025 mutex_exit(QLOCK(q)); 25026 if (q->q_flag & QWANTW) 25027 qbackenable(q, 0); 25028 } else { 25029 mutex_exit(QLOCK(q)); 25030 } 25031 } 25032 } 25033 } 25034 25035 /* 25036 * kstats related to squeues i.e. not per IP instance 25037 */ 25038 static void * 25039 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 25040 { 25041 kstat_t *ksp; 25042 25043 tcp_g_stat_t template = { 25044 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 25045 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 25046 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 25047 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 25048 }; 25049 25050 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 25051 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25052 KSTAT_FLAG_VIRTUAL); 25053 25054 if (ksp == NULL) 25055 return (NULL); 25056 25057 bcopy(&template, tcp_g_statp, sizeof (template)); 25058 ksp->ks_data = (void *)tcp_g_statp; 25059 25060 kstat_install(ksp); 25061 return (ksp); 25062 } 25063 25064 static void 25065 tcp_g_kstat_fini(kstat_t *ksp) 25066 { 25067 if (ksp != NULL) { 25068 kstat_delete(ksp); 25069 } 25070 } 25071 25072 25073 static void * 25074 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 25075 { 25076 kstat_t *ksp; 25077 25078 tcp_stat_t template = { 25079 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 25080 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 25081 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 25082 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 25083 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 25084 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 25085 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 25086 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 25087 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 25088 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 25089 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 25090 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 25091 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 25092 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 25093 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 25094 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 25095 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 25096 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 25097 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 25098 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 25099 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 25100 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 25101 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 25102 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 25103 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 25104 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 25105 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 25106 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 25107 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 25108 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 25109 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 25110 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 25111 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 25112 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 25113 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 25114 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 25115 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 25116 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 25117 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 25118 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 25119 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 25120 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 25121 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 25122 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 25123 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 25124 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 25125 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 25126 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 25127 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 25128 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 25129 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 25130 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 25131 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 25132 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 25133 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 25134 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 25135 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 25136 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 25137 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 25138 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 25139 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 25140 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 25141 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 25142 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 25143 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 25144 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 25145 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 25146 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 25147 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 25148 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 25149 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 25150 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 25151 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 25152 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 25153 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 25154 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 25155 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 25156 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 25157 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 25158 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 25159 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 25160 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 25161 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 25162 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 25163 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 25164 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 25165 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 25166 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 25167 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 25168 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 25169 }; 25170 25171 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 25172 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25173 KSTAT_FLAG_VIRTUAL, stackid); 25174 25175 if (ksp == NULL) 25176 return (NULL); 25177 25178 bcopy(&template, tcps_statisticsp, sizeof (template)); 25179 ksp->ks_data = (void *)tcps_statisticsp; 25180 ksp->ks_private = (void *)(uintptr_t)stackid; 25181 25182 kstat_install(ksp); 25183 return (ksp); 25184 } 25185 25186 static void 25187 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 25188 { 25189 if (ksp != NULL) { 25190 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25191 kstat_delete_netstack(ksp, stackid); 25192 } 25193 } 25194 25195 /* 25196 * TCP Kstats implementation 25197 */ 25198 static void * 25199 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 25200 { 25201 kstat_t *ksp; 25202 25203 tcp_named_kstat_t template = { 25204 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 25205 { "rtoMin", KSTAT_DATA_INT32, 0 }, 25206 { "rtoMax", KSTAT_DATA_INT32, 0 }, 25207 { "maxConn", KSTAT_DATA_INT32, 0 }, 25208 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 25209 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 25210 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 25211 { "estabResets", KSTAT_DATA_UINT32, 0 }, 25212 { "currEstab", KSTAT_DATA_UINT32, 0 }, 25213 { "inSegs", KSTAT_DATA_UINT64, 0 }, 25214 { "outSegs", KSTAT_DATA_UINT64, 0 }, 25215 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 25216 { "connTableSize", KSTAT_DATA_INT32, 0 }, 25217 { "outRsts", KSTAT_DATA_UINT32, 0 }, 25218 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 25219 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 25220 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 25221 { "outAck", KSTAT_DATA_UINT32, 0 }, 25222 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 25223 { "outUrg", KSTAT_DATA_UINT32, 0 }, 25224 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 25225 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 25226 { "outControl", KSTAT_DATA_UINT32, 0 }, 25227 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 25228 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 25229 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 25230 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 25231 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 25232 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 25233 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 25234 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 25235 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 25236 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 25237 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 25238 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 25239 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 25240 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 25241 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 25242 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 25243 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 25244 { "inClosed", KSTAT_DATA_UINT32, 0 }, 25245 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 25246 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 25247 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 25248 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 25249 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 25250 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 25251 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 25252 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 25253 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 25254 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 25255 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 25256 { "connTableSize6", KSTAT_DATA_INT32, 0 } 25257 }; 25258 25259 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 25260 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 25261 25262 if (ksp == NULL) 25263 return (NULL); 25264 25265 template.rtoAlgorithm.value.ui32 = 4; 25266 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 25267 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 25268 template.maxConn.value.i32 = -1; 25269 25270 bcopy(&template, ksp->ks_data, sizeof (template)); 25271 ksp->ks_update = tcp_kstat_update; 25272 ksp->ks_private = (void *)(uintptr_t)stackid; 25273 25274 kstat_install(ksp); 25275 return (ksp); 25276 } 25277 25278 static void 25279 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 25280 { 25281 if (ksp != NULL) { 25282 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25283 kstat_delete_netstack(ksp, stackid); 25284 } 25285 } 25286 25287 static int 25288 tcp_kstat_update(kstat_t *kp, int rw) 25289 { 25290 tcp_named_kstat_t *tcpkp; 25291 tcp_t *tcp; 25292 connf_t *connfp; 25293 conn_t *connp; 25294 int i; 25295 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 25296 netstack_t *ns; 25297 tcp_stack_t *tcps; 25298 ip_stack_t *ipst; 25299 25300 if ((kp == NULL) || (kp->ks_data == NULL)) 25301 return (EIO); 25302 25303 if (rw == KSTAT_WRITE) 25304 return (EACCES); 25305 25306 ns = netstack_find_by_stackid(stackid); 25307 if (ns == NULL) 25308 return (-1); 25309 tcps = ns->netstack_tcp; 25310 if (tcps == NULL) { 25311 netstack_rele(ns); 25312 return (-1); 25313 } 25314 25315 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 25316 25317 tcpkp->currEstab.value.ui32 = 0; 25318 25319 ipst = ns->netstack_ip; 25320 25321 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25322 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25323 connp = NULL; 25324 while ((connp = 25325 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25326 tcp = connp->conn_tcp; 25327 switch (tcp_snmp_state(tcp)) { 25328 case MIB2_TCP_established: 25329 case MIB2_TCP_closeWait: 25330 tcpkp->currEstab.value.ui32++; 25331 break; 25332 } 25333 } 25334 } 25335 25336 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 25337 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 25338 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 25339 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 25340 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 25341 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 25342 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 25343 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 25344 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 25345 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 25346 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 25347 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 25348 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 25349 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 25350 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 25351 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 25352 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 25353 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 25354 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 25355 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 25356 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 25357 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 25358 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 25359 tcpkp->inDataInorderSegs.value.ui32 = 25360 tcps->tcps_mib.tcpInDataInorderSegs; 25361 tcpkp->inDataInorderBytes.value.ui32 = 25362 tcps->tcps_mib.tcpInDataInorderBytes; 25363 tcpkp->inDataUnorderSegs.value.ui32 = 25364 tcps->tcps_mib.tcpInDataUnorderSegs; 25365 tcpkp->inDataUnorderBytes.value.ui32 = 25366 tcps->tcps_mib.tcpInDataUnorderBytes; 25367 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 25368 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 25369 tcpkp->inDataPartDupSegs.value.ui32 = 25370 tcps->tcps_mib.tcpInDataPartDupSegs; 25371 tcpkp->inDataPartDupBytes.value.ui32 = 25372 tcps->tcps_mib.tcpInDataPartDupBytes; 25373 tcpkp->inDataPastWinSegs.value.ui32 = 25374 tcps->tcps_mib.tcpInDataPastWinSegs; 25375 tcpkp->inDataPastWinBytes.value.ui32 = 25376 tcps->tcps_mib.tcpInDataPastWinBytes; 25377 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 25378 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 25379 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 25380 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 25381 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 25382 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 25383 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 25384 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 25385 tcpkp->timKeepaliveProbe.value.ui32 = 25386 tcps->tcps_mib.tcpTimKeepaliveProbe; 25387 tcpkp->timKeepaliveDrop.value.ui32 = 25388 tcps->tcps_mib.tcpTimKeepaliveDrop; 25389 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 25390 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 25391 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 25392 tcpkp->outSackRetransSegs.value.ui32 = 25393 tcps->tcps_mib.tcpOutSackRetransSegs; 25394 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 25395 25396 netstack_rele(ns); 25397 return (0); 25398 } 25399 25400 void 25401 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25402 { 25403 uint16_t hdr_len; 25404 ipha_t *ipha; 25405 uint8_t *nexthdrp; 25406 tcph_t *tcph; 25407 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 25408 25409 /* Already has an eager */ 25410 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25411 TCP_STAT(tcps, tcp_reinput_syn); 25412 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25413 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 25414 return; 25415 } 25416 25417 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25418 case IPV4_VERSION: 25419 ipha = (ipha_t *)mp->b_rptr; 25420 hdr_len = IPH_HDR_LENGTH(ipha); 25421 break; 25422 case IPV6_VERSION: 25423 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25424 &hdr_len, &nexthdrp)) { 25425 CONN_DEC_REF(connp); 25426 freemsg(mp); 25427 return; 25428 } 25429 break; 25430 } 25431 25432 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25433 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25434 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25435 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25436 } 25437 25438 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25439 SQ_FILL, SQTAG_TCP_REINPUT); 25440 } 25441 25442 static int 25443 tcp_squeue_switch(int val) 25444 { 25445 int rval = SQ_FILL; 25446 25447 switch (val) { 25448 case 1: 25449 rval = SQ_NODRAIN; 25450 break; 25451 case 2: 25452 rval = SQ_PROCESS; 25453 break; 25454 default: 25455 break; 25456 } 25457 return (rval); 25458 } 25459 25460 /* 25461 * This is called once for each squeue - globally for all stack 25462 * instances. 25463 */ 25464 static void 25465 tcp_squeue_add(squeue_t *sqp) 25466 { 25467 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25468 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25469 25470 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25471 tcp_time_wait->tcp_time_wait_tid = 25472 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 25473 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 25474 CALLOUT_FLAG_ROUNDUP); 25475 if (tcp_free_list_max_cnt == 0) { 25476 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25477 max_ncpus : boot_max_ncpus); 25478 25479 /* 25480 * Limit number of entries to 1% of availble memory / tcp_ncpus 25481 */ 25482 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25483 (tcp_ncpus * sizeof (tcp_t) * 100); 25484 } 25485 tcp_time_wait->tcp_free_list_cnt = 0; 25486 } 25487 25488 static int 25489 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 25490 { 25491 mblk_t *ire_mp = NULL; 25492 mblk_t *syn_mp; 25493 mblk_t *mdti; 25494 mblk_t *lsoi; 25495 int retval; 25496 tcph_t *tcph; 25497 uint32_t mss; 25498 queue_t *q = tcp->tcp_rq; 25499 conn_t *connp = tcp->tcp_connp; 25500 tcp_stack_t *tcps = tcp->tcp_tcps; 25501 25502 if (error == 0) { 25503 /* 25504 * Adapt Multidata information, if any. The 25505 * following tcp_mdt_update routine will free 25506 * the message. 25507 */ 25508 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 25509 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 25510 b_rptr)->mdt_capab, B_TRUE); 25511 freemsg(mdti); 25512 } 25513 25514 /* 25515 * Check to update LSO information with tcp, and 25516 * tcp_lso_update routine will free the message. 25517 */ 25518 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 25519 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 25520 b_rptr)->lso_capab); 25521 freemsg(lsoi); 25522 } 25523 25524 /* Get the IRE, if we had requested for it */ 25525 if (mp != NULL) 25526 ire_mp = tcp_ire_mp(&mp); 25527 25528 if (tcp->tcp_hard_binding) { 25529 tcp->tcp_hard_binding = B_FALSE; 25530 tcp->tcp_hard_bound = B_TRUE; 25531 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 25532 if (retval != 0) { 25533 error = EADDRINUSE; 25534 goto bind_failed; 25535 } 25536 } else { 25537 if (ire_mp != NULL) 25538 freeb(ire_mp); 25539 goto after_syn_sent; 25540 } 25541 25542 retval = tcp_adapt_ire(tcp, ire_mp); 25543 if (ire_mp != NULL) 25544 freeb(ire_mp); 25545 if (retval == 0) { 25546 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 25547 ENETUNREACH : EADDRNOTAVAIL); 25548 goto ipcl_rm; 25549 } 25550 /* 25551 * Don't let an endpoint connect to itself. 25552 * Also checked in tcp_connect() but that 25553 * check can't handle the case when the 25554 * local IP address is INADDR_ANY. 25555 */ 25556 if (tcp->tcp_ipversion == IPV4_VERSION) { 25557 if ((tcp->tcp_ipha->ipha_dst == 25558 tcp->tcp_ipha->ipha_src) && 25559 (BE16_EQL(tcp->tcp_tcph->th_lport, 25560 tcp->tcp_tcph->th_fport))) { 25561 error = EADDRNOTAVAIL; 25562 goto ipcl_rm; 25563 } 25564 } else { 25565 if (IN6_ARE_ADDR_EQUAL( 25566 &tcp->tcp_ip6h->ip6_dst, 25567 &tcp->tcp_ip6h->ip6_src) && 25568 (BE16_EQL(tcp->tcp_tcph->th_lport, 25569 tcp->tcp_tcph->th_fport))) { 25570 error = EADDRNOTAVAIL; 25571 goto ipcl_rm; 25572 } 25573 } 25574 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 25575 /* 25576 * This should not be possible! Just for 25577 * defensive coding... 25578 */ 25579 if (tcp->tcp_state != TCPS_SYN_SENT) 25580 goto after_syn_sent; 25581 25582 if (is_system_labeled() && 25583 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 25584 error = EHOSTUNREACH; 25585 goto ipcl_rm; 25586 } 25587 25588 /* 25589 * tcp_adapt_ire() does not adjust 25590 * for TCP/IP header length. 25591 */ 25592 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 25593 25594 /* 25595 * Just make sure our rwnd is at 25596 * least tcp_recv_hiwat_mss * MSS 25597 * large, and round up to the nearest 25598 * MSS. 25599 * 25600 * We do the round up here because 25601 * we need to get the interface 25602 * MTU first before we can do the 25603 * round up. 25604 */ 25605 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 25606 tcps->tcps_recv_hiwat_minmss * mss); 25607 if (!IPCL_IS_NONSTR(connp)) 25608 q->q_hiwat = tcp->tcp_rwnd; 25609 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 25610 tcp_set_ws_value(tcp); 25611 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 25612 tcp->tcp_tcph->th_win); 25613 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 25614 tcp->tcp_snd_ws_ok = B_TRUE; 25615 25616 /* 25617 * Set tcp_snd_ts_ok to true 25618 * so that tcp_xmit_mp will 25619 * include the timestamp 25620 * option in the SYN segment. 25621 */ 25622 if (tcps->tcps_tstamp_always || 25623 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 25624 tcp->tcp_snd_ts_ok = B_TRUE; 25625 } 25626 25627 /* 25628 * tcp_snd_sack_ok can be set in 25629 * tcp_adapt_ire() if the sack metric 25630 * is set. So check it here also. 25631 */ 25632 if (tcps->tcps_sack_permitted == 2 || 25633 tcp->tcp_snd_sack_ok) { 25634 if (tcp->tcp_sack_info == NULL) { 25635 tcp->tcp_sack_info = 25636 kmem_cache_alloc(tcp_sack_info_cache, 25637 KM_SLEEP); 25638 } 25639 tcp->tcp_snd_sack_ok = B_TRUE; 25640 } 25641 25642 /* 25643 * Should we use ECN? Note that the current 25644 * default value (SunOS 5.9) of tcp_ecn_permitted 25645 * is 1. The reason for doing this is that there 25646 * are equipments out there that will drop ECN 25647 * enabled IP packets. Setting it to 1 avoids 25648 * compatibility problems. 25649 */ 25650 if (tcps->tcps_ecn_permitted == 2) 25651 tcp->tcp_ecn_ok = B_TRUE; 25652 25653 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 25654 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 25655 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 25656 if (syn_mp) { 25657 if (cr == NULL) { 25658 cr = tcp->tcp_cred; 25659 pid = tcp->tcp_cpid; 25660 } 25661 mblk_setcred(syn_mp, cr, pid); 25662 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 25663 } 25664 after_syn_sent: 25665 if (mp != NULL) { 25666 ASSERT(mp->b_cont == NULL); 25667 freeb(mp); 25668 } 25669 return (error); 25670 } else { 25671 /* error */ 25672 if (tcp->tcp_debug) { 25673 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 25674 "tcp_post_ip_bind: error == %d", error); 25675 } 25676 if (mp != NULL) { 25677 freeb(mp); 25678 } 25679 } 25680 25681 ipcl_rm: 25682 /* 25683 * Need to unbind with classifier since we were just 25684 * told that our bind succeeded. a.k.a error == 0 at the entry. 25685 */ 25686 tcp->tcp_hard_bound = B_FALSE; 25687 tcp->tcp_hard_binding = B_FALSE; 25688 25689 ipcl_hash_remove(connp); 25690 25691 bind_failed: 25692 tcp->tcp_state = TCPS_IDLE; 25693 if (tcp->tcp_ipversion == IPV4_VERSION) 25694 tcp->tcp_ipha->ipha_src = 0; 25695 else 25696 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 25697 /* 25698 * Copy of the src addr. in tcp_t is needed since 25699 * the lookup funcs. can only look at tcp_t 25700 */ 25701 V6_SET_ZERO(tcp->tcp_ip_src_v6); 25702 25703 tcph = tcp->tcp_tcph; 25704 tcph->th_lport[0] = 0; 25705 tcph->th_lport[1] = 0; 25706 tcp_bind_hash_remove(tcp); 25707 bzero(&connp->u_port, sizeof (connp->u_port)); 25708 /* blow away saved option results if any */ 25709 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 25710 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 25711 25712 conn_delete_ire(tcp->tcp_connp, NULL); 25713 25714 return (error); 25715 } 25716 25717 static int 25718 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 25719 boolean_t bind_to_req_port_only, cred_t *cr) 25720 { 25721 in_port_t mlp_port; 25722 mlp_type_t addrtype, mlptype; 25723 boolean_t user_specified; 25724 in_port_t allocated_port; 25725 in_port_t requested_port = *requested_port_ptr; 25726 conn_t *connp; 25727 zone_t *zone; 25728 tcp_stack_t *tcps = tcp->tcp_tcps; 25729 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 25730 25731 /* 25732 * XXX It's up to the caller to specify bind_to_req_port_only or not. 25733 */ 25734 if (cr == NULL) 25735 cr = tcp->tcp_cred; 25736 /* 25737 * Get a valid port (within the anonymous range and should not 25738 * be a privileged one) to use if the user has not given a port. 25739 * If multiple threads are here, they may all start with 25740 * with the same initial port. But, it should be fine as long as 25741 * tcp_bindi will ensure that no two threads will be assigned 25742 * the same port. 25743 * 25744 * NOTE: XXX If a privileged process asks for an anonymous port, we 25745 * still check for ports only in the range > tcp_smallest_non_priv_port, 25746 * unless TCP_ANONPRIVBIND option is set. 25747 */ 25748 mlptype = mlptSingle; 25749 mlp_port = requested_port; 25750 if (requested_port == 0) { 25751 requested_port = tcp->tcp_anon_priv_bind ? 25752 tcp_get_next_priv_port(tcp) : 25753 tcp_update_next_port(tcps->tcps_next_port_to_try, 25754 tcp, B_TRUE); 25755 if (requested_port == 0) { 25756 return (-TNOADDR); 25757 } 25758 user_specified = B_FALSE; 25759 25760 /* 25761 * If the user went through one of the RPC interfaces to create 25762 * this socket and RPC is MLP in this zone, then give him an 25763 * anonymous MLP. 25764 */ 25765 connp = tcp->tcp_connp; 25766 if (connp->conn_anon_mlp && is_system_labeled()) { 25767 zone = crgetzone(cr); 25768 addrtype = tsol_mlp_addr_type(zone->zone_id, 25769 IPV6_VERSION, &v6addr, 25770 tcps->tcps_netstack->netstack_ip); 25771 if (addrtype == mlptSingle) { 25772 return (-TNOADDR); 25773 } 25774 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 25775 PMAPPORT, addrtype); 25776 mlp_port = PMAPPORT; 25777 } 25778 } else { 25779 int i; 25780 boolean_t priv = B_FALSE; 25781 25782 /* 25783 * If the requested_port is in the well-known privileged range, 25784 * verify that the stream was opened by a privileged user. 25785 * Note: No locks are held when inspecting tcp_g_*epriv_ports 25786 * but instead the code relies on: 25787 * - the fact that the address of the array and its size never 25788 * changes 25789 * - the atomic assignment of the elements of the array 25790 */ 25791 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 25792 priv = B_TRUE; 25793 } else { 25794 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 25795 if (requested_port == 25796 tcps->tcps_g_epriv_ports[i]) { 25797 priv = B_TRUE; 25798 break; 25799 } 25800 } 25801 } 25802 if (priv) { 25803 if (secpolicy_net_privaddr(cr, requested_port, 25804 IPPROTO_TCP) != 0) { 25805 if (tcp->tcp_debug) { 25806 (void) strlog(TCP_MOD_ID, 0, 1, 25807 SL_ERROR|SL_TRACE, 25808 "tcp_bind: no priv for port %d", 25809 requested_port); 25810 } 25811 return (-TACCES); 25812 } 25813 } 25814 user_specified = B_TRUE; 25815 25816 connp = tcp->tcp_connp; 25817 if (is_system_labeled()) { 25818 zone = crgetzone(cr); 25819 addrtype = tsol_mlp_addr_type(zone->zone_id, 25820 IPV6_VERSION, &v6addr, 25821 tcps->tcps_netstack->netstack_ip); 25822 if (addrtype == mlptSingle) { 25823 return (-TNOADDR); 25824 } 25825 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 25826 requested_port, addrtype); 25827 } 25828 } 25829 25830 if (mlptype != mlptSingle) { 25831 if (secpolicy_net_bindmlp(cr) != 0) { 25832 if (tcp->tcp_debug) { 25833 (void) strlog(TCP_MOD_ID, 0, 1, 25834 SL_ERROR|SL_TRACE, 25835 "tcp_bind: no priv for multilevel port %d", 25836 requested_port); 25837 } 25838 return (-TACCES); 25839 } 25840 25841 /* 25842 * If we're specifically binding a shared IP address and the 25843 * port is MLP on shared addresses, then check to see if this 25844 * zone actually owns the MLP. Reject if not. 25845 */ 25846 if (mlptype == mlptShared && addrtype == mlptShared) { 25847 /* 25848 * No need to handle exclusive-stack zones since 25849 * ALL_ZONES only applies to the shared stack. 25850 */ 25851 zoneid_t mlpzone; 25852 25853 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 25854 htons(mlp_port)); 25855 if (connp->conn_zoneid != mlpzone) { 25856 if (tcp->tcp_debug) { 25857 (void) strlog(TCP_MOD_ID, 0, 1, 25858 SL_ERROR|SL_TRACE, 25859 "tcp_bind: attempt to bind port " 25860 "%d on shared addr in zone %d " 25861 "(should be %d)", 25862 mlp_port, connp->conn_zoneid, 25863 mlpzone); 25864 } 25865 return (-TACCES); 25866 } 25867 } 25868 25869 if (!user_specified) { 25870 int err; 25871 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 25872 requested_port, B_TRUE); 25873 if (err != 0) { 25874 if (tcp->tcp_debug) { 25875 (void) strlog(TCP_MOD_ID, 0, 1, 25876 SL_ERROR|SL_TRACE, 25877 "tcp_bind: cannot establish anon " 25878 "MLP for port %d", 25879 requested_port); 25880 } 25881 return (err); 25882 } 25883 connp->conn_anon_port = B_TRUE; 25884 } 25885 connp->conn_mlp_type = mlptype; 25886 } 25887 25888 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 25889 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 25890 25891 if (allocated_port == 0) { 25892 connp->conn_mlp_type = mlptSingle; 25893 if (connp->conn_anon_port) { 25894 connp->conn_anon_port = B_FALSE; 25895 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 25896 requested_port, B_FALSE); 25897 } 25898 if (bind_to_req_port_only) { 25899 if (tcp->tcp_debug) { 25900 (void) strlog(TCP_MOD_ID, 0, 1, 25901 SL_ERROR|SL_TRACE, 25902 "tcp_bind: requested addr busy"); 25903 } 25904 return (-TADDRBUSY); 25905 } else { 25906 /* If we are out of ports, fail the bind. */ 25907 if (tcp->tcp_debug) { 25908 (void) strlog(TCP_MOD_ID, 0, 1, 25909 SL_ERROR|SL_TRACE, 25910 "tcp_bind: out of ports?"); 25911 } 25912 return (-TNOADDR); 25913 } 25914 } 25915 25916 /* Pass the allocated port back */ 25917 *requested_port_ptr = allocated_port; 25918 return (0); 25919 } 25920 25921 static int 25922 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 25923 boolean_t bind_to_req_port_only) 25924 { 25925 tcp_t *tcp = connp->conn_tcp; 25926 sin_t *sin; 25927 sin6_t *sin6; 25928 in_port_t requested_port; 25929 ipaddr_t v4addr; 25930 in6_addr_t v6addr; 25931 uint_t origipversion; 25932 int error = 0; 25933 25934 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 25935 25936 if (tcp->tcp_state == TCPS_BOUND) { 25937 return (0); 25938 } else if (tcp->tcp_state > TCPS_BOUND) { 25939 if (tcp->tcp_debug) { 25940 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 25941 "tcp_bind: bad state, %d", tcp->tcp_state); 25942 } 25943 return (-TOUTSTATE); 25944 } 25945 origipversion = tcp->tcp_ipversion; 25946 25947 ASSERT(sa != NULL && len != 0); 25948 25949 if (!OK_32PTR((char *)sa)) { 25950 if (tcp->tcp_debug) { 25951 (void) strlog(TCP_MOD_ID, 0, 1, 25952 SL_ERROR|SL_TRACE, 25953 "tcp_bind: bad address parameter, " 25954 "address %p, len %d", 25955 (void *)sa, len); 25956 } 25957 return (-TPROTO); 25958 } 25959 25960 switch (len) { 25961 case sizeof (sin_t): /* Complete IPv4 address */ 25962 sin = (sin_t *)sa; 25963 /* 25964 * With sockets sockfs will accept bogus sin_family in 25965 * bind() and replace it with the family used in the socket 25966 * call. 25967 */ 25968 if (sin->sin_family != AF_INET || 25969 tcp->tcp_family != AF_INET) { 25970 return (EAFNOSUPPORT); 25971 } 25972 requested_port = ntohs(sin->sin_port); 25973 tcp->tcp_ipversion = IPV4_VERSION; 25974 v4addr = sin->sin_addr.s_addr; 25975 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 25976 break; 25977 25978 case sizeof (sin6_t): /* Complete IPv6 address */ 25979 sin6 = (sin6_t *)sa; 25980 if (sin6->sin6_family != AF_INET6 || 25981 tcp->tcp_family != AF_INET6) { 25982 return (EAFNOSUPPORT); 25983 } 25984 requested_port = ntohs(sin6->sin6_port); 25985 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 25986 IPV4_VERSION : IPV6_VERSION; 25987 v6addr = sin6->sin6_addr; 25988 break; 25989 25990 default: 25991 if (tcp->tcp_debug) { 25992 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 25993 "tcp_bind: bad address length, %d", len); 25994 } 25995 return (EAFNOSUPPORT); 25996 /* return (-TBADADDR); */ 25997 } 25998 25999 tcp->tcp_bound_source_v6 = v6addr; 26000 26001 /* Check for change in ipversion */ 26002 if (origipversion != tcp->tcp_ipversion) { 26003 ASSERT(tcp->tcp_family == AF_INET6); 26004 error = tcp->tcp_ipversion == IPV6_VERSION ? 26005 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 26006 if (error) { 26007 return (ENOMEM); 26008 } 26009 } 26010 26011 /* 26012 * Initialize family specific fields. Copy of the src addr. 26013 * in tcp_t is needed for the lookup funcs. 26014 */ 26015 if (tcp->tcp_ipversion == IPV6_VERSION) { 26016 tcp->tcp_ip6h->ip6_src = v6addr; 26017 } else { 26018 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 26019 } 26020 tcp->tcp_ip_src_v6 = v6addr; 26021 26022 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 26023 26024 error = tcp_bind_select_lport(tcp, &requested_port, 26025 bind_to_req_port_only, cr); 26026 26027 return (error); 26028 } 26029 26030 /* 26031 * Return unix error is tli error is TSYSERR, otherwise return a negative 26032 * tli error. 26033 */ 26034 int 26035 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26036 boolean_t bind_to_req_port_only) 26037 { 26038 int error; 26039 tcp_t *tcp = connp->conn_tcp; 26040 26041 if (tcp->tcp_state >= TCPS_BOUND) { 26042 if (tcp->tcp_debug) { 26043 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26044 "tcp_bind: bad state, %d", tcp->tcp_state); 26045 } 26046 return (-TOUTSTATE); 26047 } 26048 26049 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 26050 if (error != 0) 26051 return (error); 26052 26053 ASSERT(tcp->tcp_state == TCPS_BOUND); 26054 26055 tcp->tcp_conn_req_max = 0; 26056 26057 if (tcp->tcp_family == AF_INET6) { 26058 ASSERT(tcp->tcp_connp->conn_af_isv6); 26059 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 26060 &tcp->tcp_bound_source_v6, 0, B_FALSE); 26061 } else { 26062 ASSERT(!tcp->tcp_connp->conn_af_isv6); 26063 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 26064 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 26065 } 26066 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 26067 } 26068 26069 int 26070 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 26071 socklen_t len, cred_t *cr) 26072 { 26073 int error; 26074 conn_t *connp = (conn_t *)proto_handle; 26075 squeue_t *sqp = connp->conn_sqp; 26076 26077 /* All Solaris components should pass a cred for this operation. */ 26078 ASSERT(cr != NULL); 26079 26080 ASSERT(sqp != NULL); 26081 ASSERT(connp->conn_upper_handle != NULL); 26082 26083 error = squeue_synch_enter(sqp, connp, NULL); 26084 if (error != 0) { 26085 /* failed to enter */ 26086 return (ENOSR); 26087 } 26088 26089 /* binding to a NULL address really means unbind */ 26090 if (sa == NULL) { 26091 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 26092 error = tcp_do_unbind(connp); 26093 else 26094 error = EINVAL; 26095 } else { 26096 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 26097 } 26098 26099 squeue_synch_exit(sqp, connp); 26100 26101 if (error < 0) { 26102 if (error == -TOUTSTATE) 26103 error = EINVAL; 26104 else 26105 error = proto_tlitosyserr(-error); 26106 } 26107 26108 return (error); 26109 } 26110 26111 /* 26112 * If the return value from this function is positive, it's a UNIX error. 26113 * Otherwise, if it's negative, then the absolute value is a TLI error. 26114 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 26115 */ 26116 int 26117 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 26118 cred_t *cr, pid_t pid) 26119 { 26120 tcp_t *tcp = connp->conn_tcp; 26121 sin_t *sin = (sin_t *)sa; 26122 sin6_t *sin6 = (sin6_t *)sa; 26123 ipaddr_t *dstaddrp; 26124 in_port_t dstport; 26125 uint_t srcid; 26126 int error = 0; 26127 26128 switch (len) { 26129 default: 26130 /* 26131 * Should never happen 26132 */ 26133 return (EINVAL); 26134 26135 case sizeof (sin_t): 26136 sin = (sin_t *)sa; 26137 if (sin->sin_port == 0) { 26138 return (-TBADADDR); 26139 } 26140 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 26141 return (EAFNOSUPPORT); 26142 } 26143 break; 26144 26145 case sizeof (sin6_t): 26146 sin6 = (sin6_t *)sa; 26147 if (sin6->sin6_port == 0) { 26148 return (-TBADADDR); 26149 } 26150 break; 26151 } 26152 /* 26153 * If we're connecting to an IPv4-mapped IPv6 address, we need to 26154 * make sure that the template IP header in the tcp structure is an 26155 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 26156 * need to this before we call tcp_bindi() so that the port lookup 26157 * code will look for ports in the correct port space (IPv4 and 26158 * IPv6 have separate port spaces). 26159 */ 26160 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 26161 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26162 int err = 0; 26163 26164 err = tcp_header_init_ipv4(tcp); 26165 if (err != 0) { 26166 error = ENOMEM; 26167 goto connect_failed; 26168 } 26169 if (tcp->tcp_lport != 0) 26170 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 26171 } 26172 26173 switch (tcp->tcp_state) { 26174 case TCPS_LISTEN: 26175 /* 26176 * Listening sockets are not allowed to issue connect(). 26177 */ 26178 if (IPCL_IS_NONSTR(connp)) 26179 return (EOPNOTSUPP); 26180 /* FALLTHRU */ 26181 case TCPS_IDLE: 26182 /* 26183 * We support quick connect, refer to comments in 26184 * tcp_connect_*() 26185 */ 26186 /* FALLTHRU */ 26187 case TCPS_BOUND: 26188 /* 26189 * We must bump the generation before the operation start. 26190 * This is done to ensure that any upcall made later on sends 26191 * up the right generation to the socket. 26192 */ 26193 SOCK_CONNID_BUMP(tcp->tcp_connid); 26194 26195 if (tcp->tcp_family == AF_INET6) { 26196 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26197 return (tcp_connect_ipv6(tcp, 26198 &sin6->sin6_addr, 26199 sin6->sin6_port, sin6->sin6_flowinfo, 26200 sin6->__sin6_src_id, sin6->sin6_scope_id, 26201 cr, pid)); 26202 } 26203 /* 26204 * Destination adress is mapped IPv6 address. 26205 * Source bound address should be unspecified or 26206 * IPv6 mapped address as well. 26207 */ 26208 if (!IN6_IS_ADDR_UNSPECIFIED( 26209 &tcp->tcp_bound_source_v6) && 26210 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 26211 return (EADDRNOTAVAIL); 26212 } 26213 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 26214 dstport = sin6->sin6_port; 26215 srcid = sin6->__sin6_src_id; 26216 } else { 26217 dstaddrp = &sin->sin_addr.s_addr; 26218 dstport = sin->sin_port; 26219 srcid = 0; 26220 } 26221 26222 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 26223 pid); 26224 break; 26225 default: 26226 return (-TOUTSTATE); 26227 } 26228 /* 26229 * Note: Code below is the "failure" case 26230 */ 26231 connect_failed: 26232 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26233 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26234 return (error); 26235 } 26236 26237 int 26238 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 26239 socklen_t len, sock_connid_t *id, cred_t *cr) 26240 { 26241 conn_t *connp = (conn_t *)proto_handle; 26242 tcp_t *tcp = connp->conn_tcp; 26243 squeue_t *sqp = connp->conn_sqp; 26244 int error; 26245 26246 ASSERT(connp->conn_upper_handle != NULL); 26247 26248 /* All Solaris components should pass a cred for this operation. */ 26249 ASSERT(cr != NULL); 26250 26251 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 26252 if (error != 0) { 26253 return (error); 26254 } 26255 26256 error = squeue_synch_enter(sqp, connp, NULL); 26257 if (error != 0) { 26258 /* failed to enter */ 26259 return (ENOSR); 26260 } 26261 26262 /* 26263 * TCP supports quick connect, so no need to do an implicit bind 26264 */ 26265 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 26266 if (error == 0) { 26267 *id = connp->conn_tcp->tcp_connid; 26268 } else if (error < 0) { 26269 if (error == -TOUTSTATE) { 26270 switch (connp->conn_tcp->tcp_state) { 26271 case TCPS_SYN_SENT: 26272 error = EALREADY; 26273 break; 26274 case TCPS_ESTABLISHED: 26275 error = EISCONN; 26276 break; 26277 case TCPS_LISTEN: 26278 error = EOPNOTSUPP; 26279 break; 26280 default: 26281 error = EINVAL; 26282 break; 26283 } 26284 } else { 26285 error = proto_tlitosyserr(-error); 26286 } 26287 } 26288 done: 26289 squeue_synch_exit(sqp, connp); 26290 26291 return ((error == 0) ? EINPROGRESS : error); 26292 } 26293 26294 /* ARGSUSED */ 26295 sock_lower_handle_t 26296 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 26297 uint_t *smodep, int *errorp, int flags, cred_t *credp) 26298 { 26299 conn_t *connp; 26300 boolean_t isv6 = family == AF_INET6; 26301 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 26302 (proto != 0 && proto != IPPROTO_TCP)) { 26303 *errorp = EPROTONOSUPPORT; 26304 return (NULL); 26305 } 26306 26307 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 26308 if (connp == NULL) { 26309 return (NULL); 26310 } 26311 26312 /* 26313 * Put the ref for TCP. Ref for IP was already put 26314 * by ipcl_conn_create. Also Make the conn_t globally 26315 * visible to walkers 26316 */ 26317 mutex_enter(&connp->conn_lock); 26318 CONN_INC_REF_LOCKED(connp); 26319 ASSERT(connp->conn_ref == 2); 26320 connp->conn_state_flags &= ~CONN_INCIPIENT; 26321 26322 connp->conn_flags |= IPCL_NONSTR; 26323 mutex_exit(&connp->conn_lock); 26324 26325 ASSERT(errorp != NULL); 26326 *errorp = 0; 26327 *sock_downcalls = &sock_tcp_downcalls; 26328 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 26329 SM_SENDFILESUPP; 26330 26331 return ((sock_lower_handle_t)connp); 26332 } 26333 26334 /* ARGSUSED */ 26335 void 26336 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 26337 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 26338 { 26339 conn_t *connp = (conn_t *)proto_handle; 26340 struct sock_proto_props sopp; 26341 26342 ASSERT(connp->conn_upper_handle == NULL); 26343 26344 /* All Solaris components should pass a cred for this operation. */ 26345 ASSERT(cr != NULL); 26346 26347 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 26348 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 26349 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 26350 26351 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 26352 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 26353 sopp.sopp_maxpsz = INFPSZ; 26354 sopp.sopp_maxblk = INFPSZ; 26355 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 26356 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 26357 sopp.sopp_maxaddrlen = sizeof (sin6_t); 26358 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 26359 tcp_rinfo.mi_minpsz; 26360 26361 connp->conn_upcalls = sock_upcalls; 26362 connp->conn_upper_handle = sock_handle; 26363 26364 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 26365 } 26366 26367 /* ARGSUSED */ 26368 int 26369 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 26370 { 26371 conn_t *connp = (conn_t *)proto_handle; 26372 26373 ASSERT(connp->conn_upper_handle != NULL); 26374 26375 /* All Solaris components should pass a cred for this operation. */ 26376 ASSERT(cr != NULL); 26377 26378 tcp_close_common(connp, flags); 26379 26380 ip_free_helper_stream(connp); 26381 26382 /* 26383 * Drop IP's reference on the conn. This is the last reference 26384 * on the connp if the state was less than established. If the 26385 * connection has gone into timewait state, then we will have 26386 * one ref for the TCP and one more ref (total of two) for the 26387 * classifier connected hash list (a timewait connections stays 26388 * in connected hash till closed). 26389 * 26390 * We can't assert the references because there might be other 26391 * transient reference places because of some walkers or queued 26392 * packets in squeue for the timewait state. 26393 */ 26394 CONN_DEC_REF(connp); 26395 return (0); 26396 } 26397 26398 /* ARGSUSED */ 26399 int 26400 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 26401 cred_t *cr) 26402 { 26403 tcp_t *tcp; 26404 uint32_t msize; 26405 conn_t *connp = (conn_t *)proto_handle; 26406 int32_t tcpstate; 26407 26408 /* All Solaris components should pass a cred for this operation. */ 26409 ASSERT(cr != NULL); 26410 26411 ASSERT(connp->conn_ref >= 2); 26412 ASSERT(connp->conn_upper_handle != NULL); 26413 26414 if (msg->msg_controllen != 0) { 26415 return (EOPNOTSUPP); 26416 26417 } 26418 switch (DB_TYPE(mp)) { 26419 case M_DATA: 26420 tcp = connp->conn_tcp; 26421 ASSERT(tcp != NULL); 26422 26423 tcpstate = tcp->tcp_state; 26424 if (tcpstate < TCPS_ESTABLISHED) { 26425 freemsg(mp); 26426 return (ENOTCONN); 26427 } else if (tcpstate > TCPS_CLOSE_WAIT) { 26428 freemsg(mp); 26429 return (EPIPE); 26430 } 26431 26432 msize = msgdsize(mp); 26433 26434 mutex_enter(&tcp->tcp_non_sq_lock); 26435 tcp->tcp_squeue_bytes += msize; 26436 /* 26437 * Squeue Flow Control 26438 */ 26439 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 26440 tcp_setqfull(tcp); 26441 } 26442 mutex_exit(&tcp->tcp_non_sq_lock); 26443 26444 /* 26445 * The application may pass in an address in the msghdr, but 26446 * we ignore the address on connection-oriented sockets. 26447 * Just like BSD this code does not generate an error for 26448 * TCP (a CONNREQUIRED socket) when sending to an address 26449 * passed in with sendto/sendmsg. Instead the data is 26450 * delivered on the connection as if no address had been 26451 * supplied. 26452 */ 26453 CONN_INC_REF(connp); 26454 26455 if (msg != NULL && msg->msg_flags & MSG_OOB) { 26456 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 26457 tcp_output_urgent, connp, tcp_squeue_flag, 26458 SQTAG_TCP_OUTPUT); 26459 } else { 26460 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 26461 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 26462 } 26463 26464 return (0); 26465 26466 default: 26467 ASSERT(0); 26468 } 26469 26470 freemsg(mp); 26471 return (0); 26472 } 26473 26474 /* ARGSUSED */ 26475 void 26476 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 26477 { 26478 int len; 26479 uint32_t msize; 26480 conn_t *connp = (conn_t *)arg; 26481 tcp_t *tcp = connp->conn_tcp; 26482 26483 msize = msgdsize(mp); 26484 26485 len = msize - 1; 26486 if (len < 0) { 26487 freemsg(mp); 26488 return; 26489 } 26490 26491 /* 26492 * Try to force urgent data out on the wire. 26493 * Even if we have unsent data this will 26494 * at least send the urgent flag. 26495 * XXX does not handle more flag correctly. 26496 */ 26497 len += tcp->tcp_unsent; 26498 len += tcp->tcp_snxt; 26499 tcp->tcp_urg = len; 26500 tcp->tcp_valid_bits |= TCP_URG_VALID; 26501 26502 /* Bypass tcp protocol for fused tcp loopback */ 26503 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 26504 return; 26505 tcp_wput_data(tcp, mp, B_TRUE); 26506 } 26507 26508 /* ARGSUSED */ 26509 int 26510 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26511 socklen_t *addrlenp, cred_t *cr) 26512 { 26513 conn_t *connp = (conn_t *)proto_handle; 26514 tcp_t *tcp = connp->conn_tcp; 26515 26516 ASSERT(connp->conn_upper_handle != NULL); 26517 /* All Solaris components should pass a cred for this operation. */ 26518 ASSERT(cr != NULL); 26519 26520 ASSERT(tcp != NULL); 26521 26522 return (tcp_do_getpeername(tcp, addr, addrlenp)); 26523 } 26524 26525 /* ARGSUSED */ 26526 int 26527 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26528 socklen_t *addrlenp, cred_t *cr) 26529 { 26530 conn_t *connp = (conn_t *)proto_handle; 26531 tcp_t *tcp = connp->conn_tcp; 26532 26533 /* All Solaris components should pass a cred for this operation. */ 26534 ASSERT(cr != NULL); 26535 26536 ASSERT(connp->conn_upper_handle != NULL); 26537 26538 return (tcp_do_getsockname(tcp, addr, addrlenp)); 26539 } 26540 26541 /* 26542 * tcp_fallback 26543 * 26544 * A direct socket is falling back to using STREAMS. The queue 26545 * that is being passed down was created using tcp_open() with 26546 * the SO_FALLBACK flag set. As a result, the queue is not 26547 * associated with a conn, and the q_ptrs instead contain the 26548 * dev and minor area that should be used. 26549 * 26550 * The 'direct_sockfs' flag indicates whether the FireEngine 26551 * optimizations should be used. The common case would be that 26552 * optimizations are enabled, and they might be subsequently 26553 * disabled using the _SIOCSOCKFALLBACK ioctl. 26554 */ 26555 26556 /* 26557 * An active connection is falling back to TPI. Gather all the information 26558 * required by the STREAM head and TPI sonode and send it up. 26559 */ 26560 void 26561 tcp_fallback_noneager(tcp_t *tcp, mblk_t *stropt_mp, queue_t *q, 26562 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26563 { 26564 conn_t *connp = tcp->tcp_connp; 26565 struct stroptions *stropt; 26566 struct T_capability_ack tca; 26567 struct sockaddr_in6 laddr, faddr; 26568 socklen_t laddrlen, faddrlen; 26569 short opts; 26570 int error; 26571 mblk_t *mp; 26572 26573 connp->conn_dev = (dev_t)RD(q)->q_ptr; 26574 connp->conn_minor_arena = WR(q)->q_ptr; 26575 26576 RD(q)->q_ptr = WR(q)->q_ptr = connp; 26577 26578 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 26579 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 26580 26581 WR(q)->q_qinfo = &tcp_sock_winit; 26582 26583 if (!direct_sockfs) 26584 tcp_disable_direct_sockfs(tcp); 26585 26586 /* 26587 * free the helper stream 26588 */ 26589 ip_free_helper_stream(connp); 26590 26591 /* 26592 * Notify the STREAM head about options 26593 */ 26594 DB_TYPE(stropt_mp) = M_SETOPTS; 26595 stropt = (struct stroptions *)stropt_mp->b_rptr; 26596 stropt_mp->b_wptr += sizeof (struct stroptions); 26597 stropt->so_flags = SO_HIWAT | SO_WROFF | SO_MAXBLK; 26598 26599 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 26600 tcp->tcp_tcps->tcps_wroff_xtra); 26601 if (tcp->tcp_snd_sack_ok) 26602 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 26603 stropt->so_hiwat = tcp->tcp_fused ? 26604 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 26605 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 26606 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 26607 26608 putnext(RD(q), stropt_mp); 26609 26610 /* 26611 * Collect the information needed to sync with the sonode 26612 */ 26613 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 26614 26615 laddrlen = faddrlen = sizeof (sin6_t); 26616 (void) tcp_do_getsockname(tcp, (struct sockaddr *)&laddr, &laddrlen); 26617 error = tcp_do_getpeername(tcp, (struct sockaddr *)&faddr, &faddrlen); 26618 if (error != 0) 26619 faddrlen = 0; 26620 26621 opts = 0; 26622 if (tcp->tcp_oobinline) 26623 opts |= SO_OOBINLINE; 26624 if (tcp->tcp_dontroute) 26625 opts |= SO_DONTROUTE; 26626 26627 /* 26628 * Notify the socket that the protocol is now quiescent, 26629 * and it's therefore safe move data from the socket 26630 * to the stream head. 26631 */ 26632 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 26633 (struct sockaddr *)&laddr, laddrlen, 26634 (struct sockaddr *)&faddr, faddrlen, opts); 26635 26636 while ((mp = tcp->tcp_rcv_list) != NULL) { 26637 tcp->tcp_rcv_list = mp->b_next; 26638 mp->b_next = NULL; 26639 putnext(q, mp); 26640 } 26641 tcp->tcp_rcv_last_head = NULL; 26642 tcp->tcp_rcv_last_tail = NULL; 26643 tcp->tcp_rcv_cnt = 0; 26644 } 26645 26646 /* 26647 * An eager is falling back to TPI. All we have to do is send 26648 * up a T_CONN_IND. 26649 */ 26650 void 26651 tcp_fallback_eager(tcp_t *eager, boolean_t direct_sockfs) 26652 { 26653 tcp_t *listener = eager->tcp_listener; 26654 mblk_t *mp = eager->tcp_conn.tcp_eager_conn_ind; 26655 26656 ASSERT(listener != NULL); 26657 ASSERT(mp != NULL); 26658 26659 eager->tcp_conn.tcp_eager_conn_ind = NULL; 26660 26661 /* 26662 * TLI/XTI applications will get confused by 26663 * sending eager as an option since it violates 26664 * the option semantics. So remove the eager as 26665 * option since TLI/XTI app doesn't need it anyway. 26666 */ 26667 if (!direct_sockfs) { 26668 struct T_conn_ind *conn_ind; 26669 26670 conn_ind = (struct T_conn_ind *)mp->b_rptr; 26671 conn_ind->OPT_length = 0; 26672 conn_ind->OPT_offset = 0; 26673 } 26674 26675 /* 26676 * Sockfs guarantees that the listener will not be closed 26677 * during fallback. So we can safely use the listener's queue. 26678 */ 26679 putnext(listener->tcp_rq, mp); 26680 } 26681 26682 int 26683 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 26684 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26685 { 26686 tcp_t *tcp; 26687 conn_t *connp = (conn_t *)proto_handle; 26688 int error; 26689 mblk_t *stropt_mp; 26690 mblk_t *ordrel_mp; 26691 mblk_t *fused_sigurp_mp; 26692 26693 tcp = connp->conn_tcp; 26694 26695 stropt_mp = allocb_wait(sizeof (struct stroptions), BPRI_HI, STR_NOSIG, 26696 NULL); 26697 26698 /* Pre-allocate the T_ordrel_ind mblk. */ 26699 ASSERT(tcp->tcp_ordrel_mp == NULL); 26700 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 26701 STR_NOSIG, NULL); 26702 ordrel_mp->b_datap->db_type = M_PROTO; 26703 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 26704 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 26705 26706 /* Pre-allocate the M_PCSIG used by fusion */ 26707 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 26708 26709 /* 26710 * Enter the squeue so that no new packets can come in 26711 */ 26712 error = squeue_synch_enter(connp->conn_sqp, connp, NULL); 26713 if (error != 0) { 26714 /* failed to enter, free all the pre-allocated messages. */ 26715 freeb(stropt_mp); 26716 freeb(ordrel_mp); 26717 freeb(fused_sigurp_mp); 26718 /* 26719 * We cannot process the eager, so at least send out a 26720 * RST so the peer can reconnect. 26721 */ 26722 if (tcp->tcp_listener != NULL) { 26723 (void) tcp_eager_blowoff(tcp->tcp_listener, 26724 tcp->tcp_conn_req_seqnum); 26725 } 26726 return (ENOMEM); 26727 } 26728 26729 /* 26730 * No longer a direct socket 26731 */ 26732 connp->conn_flags &= ~IPCL_NONSTR; 26733 26734 tcp->tcp_ordrel_mp = ordrel_mp; 26735 26736 if (tcp->tcp_fused) { 26737 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 26738 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 26739 } else { 26740 freeb(fused_sigurp_mp); 26741 } 26742 26743 if (tcp->tcp_listener != NULL) { 26744 /* The eager will deal with opts when accept() is called */ 26745 freeb(stropt_mp); 26746 tcp_fallback_eager(tcp, direct_sockfs); 26747 } else { 26748 tcp_fallback_noneager(tcp, stropt_mp, q, direct_sockfs, 26749 quiesced_cb); 26750 } 26751 26752 /* 26753 * There should be atleast two ref's (IP + TCP) 26754 */ 26755 ASSERT(connp->conn_ref >= 2); 26756 squeue_synch_exit(connp->conn_sqp, connp); 26757 26758 return (0); 26759 } 26760 26761 /* ARGSUSED */ 26762 static void 26763 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 26764 { 26765 conn_t *connp = (conn_t *)arg; 26766 tcp_t *tcp = connp->conn_tcp; 26767 26768 freemsg(mp); 26769 26770 if (tcp->tcp_fused) 26771 tcp_unfuse(tcp); 26772 26773 if (tcp_xmit_end(tcp) != 0) { 26774 /* 26775 * We were crossing FINs and got a reset from 26776 * the other side. Just ignore it. 26777 */ 26778 if (tcp->tcp_debug) { 26779 (void) strlog(TCP_MOD_ID, 0, 1, 26780 SL_ERROR|SL_TRACE, 26781 "tcp_shutdown_output() out of state %s", 26782 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 26783 } 26784 } 26785 } 26786 26787 /* ARGSUSED */ 26788 int 26789 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 26790 { 26791 conn_t *connp = (conn_t *)proto_handle; 26792 tcp_t *tcp = connp->conn_tcp; 26793 26794 ASSERT(connp->conn_upper_handle != NULL); 26795 26796 /* All Solaris components should pass a cred for this operation. */ 26797 ASSERT(cr != NULL); 26798 26799 /* 26800 * X/Open requires that we check the connected state. 26801 */ 26802 if (tcp->tcp_state < TCPS_SYN_SENT) 26803 return (ENOTCONN); 26804 26805 /* shutdown the send side */ 26806 if (how != SHUT_RD) { 26807 mblk_t *bp; 26808 26809 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 26810 CONN_INC_REF(connp); 26811 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 26812 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 26813 26814 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26815 SOCK_OPCTL_SHUT_SEND, 0); 26816 } 26817 26818 /* shutdown the recv side */ 26819 if (how != SHUT_WR) 26820 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26821 SOCK_OPCTL_SHUT_RECV, 0); 26822 26823 return (0); 26824 } 26825 26826 /* 26827 * SOP_LISTEN() calls into tcp_listen(). 26828 */ 26829 /* ARGSUSED */ 26830 int 26831 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 26832 { 26833 conn_t *connp = (conn_t *)proto_handle; 26834 int error; 26835 squeue_t *sqp = connp->conn_sqp; 26836 26837 ASSERT(connp->conn_upper_handle != NULL); 26838 26839 /* All Solaris components should pass a cred for this operation. */ 26840 ASSERT(cr != NULL); 26841 26842 error = squeue_synch_enter(sqp, connp, NULL); 26843 if (error != 0) { 26844 /* failed to enter */ 26845 return (ENOBUFS); 26846 } 26847 26848 error = tcp_do_listen(connp, NULL, 0, backlog, cr, FALSE); 26849 if (error == 0) { 26850 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26851 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 26852 } else if (error < 0) { 26853 if (error == -TOUTSTATE) 26854 error = EINVAL; 26855 else 26856 error = proto_tlitosyserr(-error); 26857 } 26858 squeue_synch_exit(sqp, connp); 26859 return (error); 26860 } 26861 26862 static int 26863 tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len, 26864 int backlog, cred_t *cr, boolean_t bind_to_req_port_only) 26865 { 26866 tcp_t *tcp = connp->conn_tcp; 26867 int error = 0; 26868 tcp_stack_t *tcps = tcp->tcp_tcps; 26869 26870 /* All Solaris components should pass a cred for this operation. */ 26871 ASSERT(cr != NULL); 26872 26873 if (tcp->tcp_state >= TCPS_BOUND) { 26874 if ((tcp->tcp_state == TCPS_BOUND || 26875 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 26876 /* 26877 * Handle listen() increasing backlog. 26878 * This is more "liberal" then what the TPI spec 26879 * requires but is needed to avoid a t_unbind 26880 * when handling listen() since the port number 26881 * might be "stolen" between the unbind and bind. 26882 */ 26883 goto do_listen; 26884 } 26885 if (tcp->tcp_debug) { 26886 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26887 "tcp_listen: bad state, %d", tcp->tcp_state); 26888 } 26889 return (-TOUTSTATE); 26890 } else { 26891 if (sa == NULL) { 26892 sin6_t addr; 26893 sin_t *sin; 26894 sin6_t *sin6; 26895 26896 ASSERT(IPCL_IS_NONSTR(connp)); 26897 26898 /* Do an implicit bind: Request for a generic port. */ 26899 if (tcp->tcp_family == AF_INET) { 26900 len = sizeof (sin_t); 26901 sin = (sin_t *)&addr; 26902 *sin = sin_null; 26903 sin->sin_family = AF_INET; 26904 tcp->tcp_ipversion = IPV4_VERSION; 26905 } else { 26906 ASSERT(tcp->tcp_family == AF_INET6); 26907 len = sizeof (sin6_t); 26908 sin6 = (sin6_t *)&addr; 26909 *sin6 = sin6_null; 26910 sin6->sin6_family = AF_INET6; 26911 tcp->tcp_ipversion = IPV6_VERSION; 26912 } 26913 sa = (struct sockaddr *)&addr; 26914 } 26915 26916 error = tcp_bind_check(connp, sa, len, cr, 26917 bind_to_req_port_only); 26918 if (error) 26919 return (error); 26920 /* Fall through and do the fanout insertion */ 26921 } 26922 26923 do_listen: 26924 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 26925 tcp->tcp_conn_req_max = backlog; 26926 if (tcp->tcp_conn_req_max) { 26927 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 26928 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 26929 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 26930 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 26931 /* 26932 * If this is a listener, do not reset the eager list 26933 * and other stuffs. Note that we don't check if the 26934 * existing eager list meets the new tcp_conn_req_max 26935 * requirement. 26936 */ 26937 if (tcp->tcp_state != TCPS_LISTEN) { 26938 tcp->tcp_state = TCPS_LISTEN; 26939 /* Initialize the chain. Don't need the eager_lock */ 26940 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 26941 tcp->tcp_eager_next_drop_q0 = tcp; 26942 tcp->tcp_eager_prev_drop_q0 = tcp; 26943 tcp->tcp_second_ctimer_threshold = 26944 tcps->tcps_ip_abort_linterval; 26945 } 26946 } 26947 26948 /* 26949 * We can call ip_bind directly, the processing continues 26950 * in tcp_post_ip_bind(). 26951 * 26952 * We need to make sure that the conn_recv is set to a non-null 26953 * value before we insert the conn into the classifier table. 26954 * This is to avoid a race with an incoming packet which does an 26955 * ipcl_classify(). 26956 */ 26957 connp->conn_recv = tcp_conn_request; 26958 if (tcp->tcp_family == AF_INET) { 26959 error = ip_proto_bind_laddr_v4(connp, NULL, 26960 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 26961 } else { 26962 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 26963 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 26964 } 26965 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 26966 } 26967 26968 void 26969 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 26970 { 26971 conn_t *connp = (conn_t *)proto_handle; 26972 tcp_t *tcp = connp->conn_tcp; 26973 mblk_t *mp; 26974 int error; 26975 26976 ASSERT(connp->conn_upper_handle != NULL); 26977 26978 /* 26979 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_clr_flowctrl() 26980 * is currently running. 26981 */ 26982 mutex_enter(&tcp->tcp_rsrv_mp_lock); 26983 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 26984 mutex_exit(&tcp->tcp_rsrv_mp_lock); 26985 return; 26986 } 26987 tcp->tcp_rsrv_mp = NULL; 26988 mutex_exit(&tcp->tcp_rsrv_mp_lock); 26989 26990 error = squeue_synch_enter(connp->conn_sqp, connp, mp); 26991 ASSERT(error == 0); 26992 26993 mutex_enter(&tcp->tcp_rsrv_mp_lock); 26994 tcp->tcp_rsrv_mp = mp; 26995 mutex_exit(&tcp->tcp_rsrv_mp_lock); 26996 26997 if (tcp->tcp_fused) { 26998 tcp_fuse_backenable(tcp); 26999 } else { 27000 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 27001 /* 27002 * Send back a window update immediately if TCP is above 27003 * ESTABLISHED state and the increase of the rcv window 27004 * that the other side knows is at least 1 MSS after flow 27005 * control is lifted. 27006 */ 27007 if (tcp->tcp_state >= TCPS_ESTABLISHED && 27008 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 27009 tcp_xmit_ctl(NULL, tcp, 27010 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 27011 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 27012 } 27013 } 27014 27015 squeue_synch_exit(connp->conn_sqp, connp); 27016 } 27017 27018 /* ARGSUSED */ 27019 int 27020 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 27021 int mode, int32_t *rvalp, cred_t *cr) 27022 { 27023 conn_t *connp = (conn_t *)proto_handle; 27024 int error; 27025 27026 ASSERT(connp->conn_upper_handle != NULL); 27027 27028 /* All Solaris components should pass a cred for this operation. */ 27029 ASSERT(cr != NULL); 27030 27031 switch (cmd) { 27032 case ND_SET: 27033 case ND_GET: 27034 case TCP_IOC_DEFAULT_Q: 27035 case _SIOCSOCKFALLBACK: 27036 case TCP_IOC_ABORT_CONN: 27037 case TI_GETPEERNAME: 27038 case TI_GETMYNAME: 27039 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 27040 cmd)); 27041 error = EINVAL; 27042 break; 27043 default: 27044 /* 27045 * Pass on to IP using helper stream 27046 */ 27047 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 27048 cmd, arg, mode, cr, rvalp); 27049 break; 27050 } 27051 return (error); 27052 } 27053 27054 sock_downcalls_t sock_tcp_downcalls = { 27055 tcp_activate, 27056 tcp_accept, 27057 tcp_bind, 27058 tcp_listen, 27059 tcp_connect, 27060 tcp_getpeername, 27061 tcp_getsockname, 27062 tcp_getsockopt, 27063 tcp_setsockopt, 27064 tcp_sendmsg, 27065 NULL, 27066 NULL, 27067 NULL, 27068 tcp_shutdown, 27069 tcp_clr_flowctrl, 27070 tcp_ioctl, 27071 tcp_close, 27072 }; 27073