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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 const char tcp_version[] = "%Z%%M% %I% %E% SMI"; 30 31 32 #include <sys/types.h> 33 #include <sys/stream.h> 34 #include <sys/strsun.h> 35 #include <sys/strsubr.h> 36 #include <sys/stropts.h> 37 #include <sys/strlog.h> 38 #include <sys/strsun.h> 39 #define _SUN_TPI_VERSION 2 40 #include <sys/tihdr.h> 41 #include <sys/timod.h> 42 #include <sys/ddi.h> 43 #include <sys/sunddi.h> 44 #include <sys/suntpi.h> 45 #include <sys/xti_inet.h> 46 #include <sys/cmn_err.h> 47 #include <sys/debug.h> 48 #include <sys/sdt.h> 49 #include <sys/vtrace.h> 50 #include <sys/kmem.h> 51 #include <sys/ethernet.h> 52 #include <sys/cpuvar.h> 53 #include <sys/dlpi.h> 54 #include <sys/multidata.h> 55 #include <sys/multidata_impl.h> 56 #include <sys/pattr.h> 57 #include <sys/policy.h> 58 #include <sys/priv.h> 59 #include <sys/zone.h> 60 #include <sys/sunldi.h> 61 62 #include <sys/errno.h> 63 #include <sys/signal.h> 64 #include <sys/socket.h> 65 #include <sys/sockio.h> 66 #include <sys/isa_defs.h> 67 #include <sys/md5.h> 68 #include <sys/random.h> 69 #include <netinet/in.h> 70 #include <netinet/tcp.h> 71 #include <netinet/ip6.h> 72 #include <netinet/icmp6.h> 73 #include <net/if.h> 74 #include <net/route.h> 75 #include <inet/ipsec_impl.h> 76 77 #include <inet/common.h> 78 #include <inet/ip.h> 79 #include <inet/ip_impl.h> 80 #include <inet/ip6.h> 81 #include <inet/ip_ndp.h> 82 #include <inet/mi.h> 83 #include <inet/mib2.h> 84 #include <inet/nd.h> 85 #include <inet/optcom.h> 86 #include <inet/snmpcom.h> 87 #include <inet/kstatcom.h> 88 #include <inet/tcp.h> 89 #include <inet/tcp_impl.h> 90 #include <net/pfkeyv2.h> 91 #include <inet/ipsec_info.h> 92 #include <inet/ipdrop.h> 93 #include <inet/tcp_trace.h> 94 95 #include <inet/ipclassifier.h> 96 #include <inet/ip_ire.h> 97 #include <inet/ip_ftable.h> 98 #include <inet/ip_if.h> 99 #include <inet/ipp_common.h> 100 #include <inet/ip_netinfo.h> 101 #include <sys/squeue.h> 102 #include <inet/kssl/ksslapi.h> 103 #include <sys/tsol/label.h> 104 #include <sys/tsol/tnet.h> 105 #include <sys/sdt.h> 106 #include <rpc/pmap_prot.h> 107 108 /* 109 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 110 * 111 * (Read the detailed design doc in PSARC case directory) 112 * 113 * The entire tcp state is contained in tcp_t and conn_t structure 114 * which are allocated in tandem using ipcl_conn_create() and passing 115 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 116 * the references on the tcp_t. The tcp_t structure is never compressed 117 * and packets always land on the correct TCP perimeter from the time 118 * eager is created till the time tcp_t dies (as such the old mentat 119 * TCP global queue is not used for detached state and no IPSEC checking 120 * is required). The global queue is still allocated to send out resets 121 * for connection which have no listeners and IP directly calls 122 * tcp_xmit_listeners_reset() which does any policy check. 123 * 124 * Protection and Synchronisation mechanism: 125 * 126 * The tcp data structure does not use any kind of lock for protecting 127 * its state but instead uses 'squeues' for mutual exclusion from various 128 * read and write side threads. To access a tcp member, the thread should 129 * always be behind squeue (via squeue_enter, squeue_enter_nodrain, or 130 * squeue_fill). Since the squeues allow a direct function call, caller 131 * can pass any tcp function having prototype of edesc_t as argument 132 * (different from traditional STREAMs model where packets come in only 133 * designated entry points). The list of functions that can be directly 134 * called via squeue are listed before the usual function prototype. 135 * 136 * Referencing: 137 * 138 * TCP is MT-Hot and we use a reference based scheme to make sure that the 139 * tcp structure doesn't disappear when its needed. When the application 140 * creates an outgoing connection or accepts an incoming connection, we 141 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 142 * The IP reference is just a symbolic reference since ip_tcpclose() 143 * looks at tcp structure after tcp_close_output() returns which could 144 * have dropped the last TCP reference. So as long as the connection is 145 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 146 * conn_t. The classifier puts its own reference when the connection is 147 * inserted in listen or connected hash. Anytime a thread needs to enter 148 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 149 * on write side or by doing a classify on read side and then puts a 150 * reference on the conn before doing squeue_enter/tryenter/fill. For 151 * read side, the classifier itself puts the reference under fanout lock 152 * to make sure that tcp can't disappear before it gets processed. The 153 * squeue will drop this reference automatically so the called function 154 * doesn't have to do a DEC_REF. 155 * 156 * Opening a new connection: 157 * 158 * The outgoing connection open is pretty simple. tcp_open() does the 159 * work in creating the conn/tcp structure and initializing it. The 160 * squeue assignment is done based on the CPU the application 161 * is running on. So for outbound connections, processing is always done 162 * on application CPU which might be different from the incoming CPU 163 * being interrupted by the NIC. An optimal way would be to figure out 164 * the NIC <-> CPU binding at listen time, and assign the outgoing 165 * connection to the squeue attached to the CPU that will be interrupted 166 * for incoming packets (we know the NIC based on the bind IP address). 167 * This might seem like a problem if more data is going out but the 168 * fact is that in most cases the transmit is ACK driven transmit where 169 * the outgoing data normally sits on TCP's xmit queue waiting to be 170 * transmitted. 171 * 172 * Accepting a connection: 173 * 174 * This is a more interesting case because of various races involved in 175 * establishing a eager in its own perimeter. Read the meta comment on 176 * top of tcp_conn_request(). But briefly, the squeue is picked by 177 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 178 * 179 * Closing a connection: 180 * 181 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 182 * via squeue to do the close and mark the tcp as detached if the connection 183 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 184 * reference but tcp_close() drop IP's reference always. So if tcp was 185 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 186 * and 1 because it is in classifier's connected hash. This is the condition 187 * we use to determine that its OK to clean up the tcp outside of squeue 188 * when time wait expires (check the ref under fanout and conn_lock and 189 * if it is 2, remove it from fanout hash and kill it). 190 * 191 * Although close just drops the necessary references and marks the 192 * tcp_detached state, tcp_close needs to know the tcp_detached has been 193 * set (under squeue) before letting the STREAM go away (because a 194 * inbound packet might attempt to go up the STREAM while the close 195 * has happened and tcp_detached is not set). So a special lock and 196 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 197 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 198 * tcp_detached. 199 * 200 * Special provisions and fast paths: 201 * 202 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 203 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 204 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 205 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 206 * check to send packets directly to tcp_rput_data via squeue. Everyone 207 * else comes through tcp_input() on the read side. 208 * 209 * We also make special provisions for sockfs by marking tcp_issocket 210 * whenever we have only sockfs on top of TCP. This allows us to skip 211 * putting the tcp in acceptor hash since a sockfs listener can never 212 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 213 * since eager has already been allocated and the accept now happens 214 * on acceptor STREAM. There is a big blob of comment on top of 215 * tcp_conn_request explaining the new accept. When socket is POP'd, 216 * sockfs sends us an ioctl to mark the fact and we go back to old 217 * behaviour. Once tcp_issocket is unset, its never set for the 218 * life of that connection. 219 * 220 * IPsec notes : 221 * 222 * Since a packet is always executed on the correct TCP perimeter 223 * all IPsec processing is defered to IP including checking new 224 * connections and setting IPSEC policies for new connection. The 225 * only exception is tcp_xmit_listeners_reset() which is called 226 * directly from IP and needs to policy check to see if TH_RST 227 * can be sent out. 228 * 229 * PFHooks notes : 230 * 231 * For mdt case, one meta buffer contains multiple packets. Mblks for every 232 * packet are assembled and passed to the hooks. When packets are blocked, 233 * or boundary of any packet is changed, the mdt processing is stopped, and 234 * packets of the meta buffer are send to the IP path one by one. 235 */ 236 237 extern major_t TCP6_MAJ; 238 239 /* 240 * Values for squeue switch: 241 * 1: squeue_enter_nodrain 242 * 2: squeue_enter 243 * 3: squeue_fill 244 */ 245 int tcp_squeue_close = 2; /* Setable in /etc/system */ 246 int tcp_squeue_wput = 2; 247 248 squeue_func_t tcp_squeue_close_proc; 249 squeue_func_t tcp_squeue_wput_proc; 250 251 /* 252 * This controls how tiny a write must be before we try to copy it 253 * into the the mblk on the tail of the transmit queue. Not much 254 * speedup is observed for values larger than sixteen. Zero will 255 * disable the optimisation. 256 */ 257 int tcp_tx_pull_len = 16; 258 259 /* 260 * TCP Statistics. 261 * 262 * How TCP statistics work. 263 * 264 * There are two types of statistics invoked by two macros. 265 * 266 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 267 * supposed to be used in non MT-hot paths of the code. 268 * 269 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 270 * supposed to be used for DEBUG purposes and may be used on a hot path. 271 * 272 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 273 * (use "kstat tcp" to get them). 274 * 275 * There is also additional debugging facility that marks tcp_clean_death() 276 * instances and saves them in tcp_t structure. It is triggered by 277 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 278 * tcp_clean_death() calls that counts the number of times each tag was hit. It 279 * is triggered by TCP_CLD_COUNTERS define. 280 * 281 * How to add new counters. 282 * 283 * 1) Add a field in the tcp_stat structure describing your counter. 284 * 2) Add a line in the template in tcp_kstat2_init() with the name 285 * of the counter. 286 * 287 * IMPORTANT!! - make sure that both are in sync !! 288 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 289 * 290 * Please avoid using private counters which are not kstat-exported. 291 * 292 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 293 * in tcp_t structure. 294 * 295 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 296 */ 297 298 #ifndef TCP_DEBUG_COUNTER 299 #ifdef DEBUG 300 #define TCP_DEBUG_COUNTER 1 301 #else 302 #define TCP_DEBUG_COUNTER 0 303 #endif 304 #endif 305 306 #define TCP_CLD_COUNTERS 0 307 308 #define TCP_TAG_CLEAN_DEATH 1 309 #define TCP_MAX_CLEAN_DEATH_TAG 32 310 311 #ifdef lint 312 static int _lint_dummy_; 313 #endif 314 315 #if TCP_CLD_COUNTERS 316 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 317 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 318 #elif defined(lint) 319 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 320 #else 321 #define TCP_CLD_STAT(x) 322 #endif 323 324 #if TCP_DEBUG_COUNTER 325 #define TCP_DBGSTAT(tcps, x) \ 326 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 327 #define TCP_G_DBGSTAT(x) \ 328 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 329 #elif defined(lint) 330 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 331 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 332 #else 333 #define TCP_DBGSTAT(tcps, x) 334 #define TCP_G_DBGSTAT(x) 335 #endif 336 337 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 338 339 tcp_g_stat_t tcp_g_statistics; 340 kstat_t *tcp_g_kstat; 341 342 /* 343 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 344 * tcp write side. 345 */ 346 #define CALL_IP_WPUT(connp, q, mp) { \ 347 tcp_stack_t *tcps; \ 348 \ 349 tcps = connp->conn_netstack->netstack_tcp; \ 350 ASSERT(((q)->q_flag & QREADR) == 0); \ 351 TCP_DBGSTAT(tcps, tcp_ip_output); \ 352 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 353 } 354 355 /* Macros for timestamp comparisons */ 356 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 357 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 358 359 /* 360 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 361 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 362 * by adding three components: a time component which grows by 1 every 4096 363 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 364 * a per-connection component which grows by 125000 for every new connection; 365 * and an "extra" component that grows by a random amount centered 366 * approximately on 64000. This causes the the ISS generator to cycle every 367 * 4.89 hours if no TCP connections are made, and faster if connections are 368 * made. 369 * 370 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 371 * components: a time component which grows by 250000 every second; and 372 * a per-connection component which grows by 125000 for every new connections. 373 * 374 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 375 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 376 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 377 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 378 * password. 379 */ 380 #define ISS_INCR 250000 381 #define ISS_NSEC_SHT 12 382 383 static sin_t sin_null; /* Zero address for quick clears */ 384 static sin6_t sin6_null; /* Zero address for quick clears */ 385 386 /* 387 * This implementation follows the 4.3BSD interpretation of the urgent 388 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 389 * incompatible changes in protocols like telnet and rlogin. 390 */ 391 #define TCP_OLD_URP_INTERPRETATION 1 392 393 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 394 (TCP_IS_DETACHED(tcp) && \ 395 (!(tcp)->tcp_hard_binding)) 396 397 /* 398 * TCP reassembly macros. We hide starting and ending sequence numbers in 399 * b_next and b_prev of messages on the reassembly queue. The messages are 400 * chained using b_cont. These macros are used in tcp_reass() so we don't 401 * have to see the ugly casts and assignments. 402 */ 403 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 404 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 405 (mblk_t *)(uintptr_t)(u)) 406 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 407 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 408 (mblk_t *)(uintptr_t)(u)) 409 410 /* 411 * Implementation of TCP Timers. 412 * ============================= 413 * 414 * INTERFACE: 415 * 416 * There are two basic functions dealing with tcp timers: 417 * 418 * timeout_id_t tcp_timeout(connp, func, time) 419 * clock_t tcp_timeout_cancel(connp, timeout_id) 420 * TCP_TIMER_RESTART(tcp, intvl) 421 * 422 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 423 * after 'time' ticks passed. The function called by timeout() must adhere to 424 * the same restrictions as a driver soft interrupt handler - it must not sleep 425 * or call other functions that might sleep. The value returned is the opaque 426 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 427 * cancel the request. The call to tcp_timeout() may fail in which case it 428 * returns zero. This is different from the timeout(9F) function which never 429 * fails. 430 * 431 * The call-back function 'func' always receives 'connp' as its single 432 * argument. It is always executed in the squeue corresponding to the tcp 433 * structure. The tcp structure is guaranteed to be present at the time the 434 * call-back is called. 435 * 436 * NOTE: The call-back function 'func' is never called if tcp is in 437 * the TCPS_CLOSED state. 438 * 439 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 440 * request. locks acquired by the call-back routine should not be held across 441 * the call to tcp_timeout_cancel() or a deadlock may result. 442 * 443 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 444 * Otherwise, it returns an integer value greater than or equal to 0. In 445 * particular, if the call-back function is already placed on the squeue, it can 446 * not be canceled. 447 * 448 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 449 * within squeue context corresponding to the tcp instance. Since the 450 * call-back is also called via the same squeue, there are no race 451 * conditions described in untimeout(9F) manual page since all calls are 452 * strictly serialized. 453 * 454 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 455 * stored in tcp_timer_tid and starts a new one using 456 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 457 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 458 * field. 459 * 460 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 461 * call-back may still be called, so it is possible tcp_timer() will be 462 * called several times. This should not be a problem since tcp_timer() 463 * should always check the tcp instance state. 464 * 465 * 466 * IMPLEMENTATION: 467 * 468 * TCP timers are implemented using three-stage process. The call to 469 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 470 * when the timer expires. The tcp_timer_callback() arranges the call of the 471 * tcp_timer_handler() function via squeue corresponding to the tcp 472 * instance. The tcp_timer_handler() calls actual requested timeout call-back 473 * and passes tcp instance as an argument to it. Information is passed between 474 * stages using the tcp_timer_t structure which contains the connp pointer, the 475 * tcp call-back to call and the timeout id returned by the timeout(9F). 476 * 477 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 478 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 479 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 480 * returns the pointer to this mblk. 481 * 482 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 483 * looks like a normal mblk without actual dblk attached to it. 484 * 485 * To optimize performance each tcp instance holds a small cache of timer 486 * mblocks. In the current implementation it caches up to two timer mblocks per 487 * tcp instance. The cache is preserved over tcp frees and is only freed when 488 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 489 * timer processing happens on a corresponding squeue, the cache manipulation 490 * does not require any locks. Experiments show that majority of timer mblocks 491 * allocations are satisfied from the tcp cache and do not involve kmem calls. 492 * 493 * The tcp_timeout() places a refhold on the connp instance which guarantees 494 * that it will be present at the time the call-back function fires. The 495 * tcp_timer_handler() drops the reference after calling the call-back, so the 496 * call-back function does not need to manipulate the references explicitly. 497 */ 498 499 typedef struct tcp_timer_s { 500 conn_t *connp; 501 void (*tcpt_proc)(void *); 502 timeout_id_t tcpt_tid; 503 } tcp_timer_t; 504 505 static kmem_cache_t *tcp_timercache; 506 kmem_cache_t *tcp_sack_info_cache; 507 kmem_cache_t *tcp_iphc_cache; 508 509 /* 510 * For scalability, we must not run a timer for every TCP connection 511 * in TIME_WAIT state. To see why, consider (for time wait interval of 512 * 4 minutes): 513 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 514 * 515 * This list is ordered by time, so you need only delete from the head 516 * until you get to entries which aren't old enough to delete yet. 517 * The list consists of only the detached TIME_WAIT connections. 518 * 519 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 520 * becomes detached TIME_WAIT (either by changing the state and already 521 * being detached or the other way around). This means that the TIME_WAIT 522 * state can be extended (up to doubled) if the connection doesn't become 523 * detached for a long time. 524 * 525 * The list manipulations (including tcp_time_wait_next/prev) 526 * are protected by the tcp_time_wait_lock. The content of the 527 * detached TIME_WAIT connections is protected by the normal perimeters. 528 * 529 * This list is per squeue and squeues are shared across the tcp_stack_t's. 530 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 531 * and conn_netstack. 532 * The tcp_t's that are added to tcp_free_list are disassociated and 533 * have NULL tcp_tcps and conn_netstack pointers. 534 */ 535 typedef struct tcp_squeue_priv_s { 536 kmutex_t tcp_time_wait_lock; 537 timeout_id_t tcp_time_wait_tid; 538 tcp_t *tcp_time_wait_head; 539 tcp_t *tcp_time_wait_tail; 540 tcp_t *tcp_free_list; 541 uint_t tcp_free_list_cnt; 542 } tcp_squeue_priv_t; 543 544 /* 545 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 546 * Running it every 5 seconds seems to give the best results. 547 */ 548 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 549 550 /* 551 * To prevent memory hog, limit the number of entries in tcp_free_list 552 * to 1% of available memory / number of cpus 553 */ 554 uint_t tcp_free_list_max_cnt = 0; 555 556 #define TCP_XMIT_LOWATER 4096 557 #define TCP_XMIT_HIWATER 49152 558 #define TCP_RECV_LOWATER 2048 559 #define TCP_RECV_HIWATER 49152 560 561 /* 562 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 563 */ 564 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 565 566 #define TIDUSZ 4096 /* transport interface data unit size */ 567 568 /* 569 * Bind hash list size and has function. It has to be a power of 2 for 570 * hashing. 571 */ 572 #define TCP_BIND_FANOUT_SIZE 512 573 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 574 /* 575 * Size of listen and acceptor hash list. It has to be a power of 2 for 576 * hashing. 577 */ 578 #define TCP_FANOUT_SIZE 256 579 580 #ifdef _ILP32 581 #define TCP_ACCEPTOR_HASH(accid) \ 582 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 583 #else 584 #define TCP_ACCEPTOR_HASH(accid) \ 585 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 586 #endif /* _ILP32 */ 587 588 #define IP_ADDR_CACHE_SIZE 2048 589 #define IP_ADDR_CACHE_HASH(faddr) \ 590 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 591 592 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 593 #define TCP_HSP_HASH_SIZE 256 594 595 #define TCP_HSP_HASH(addr) \ 596 (((addr>>24) ^ (addr >>16) ^ \ 597 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 598 599 /* 600 * TCP options struct returned from tcp_parse_options. 601 */ 602 typedef struct tcp_opt_s { 603 uint32_t tcp_opt_mss; 604 uint32_t tcp_opt_wscale; 605 uint32_t tcp_opt_ts_val; 606 uint32_t tcp_opt_ts_ecr; 607 tcp_t *tcp; 608 } tcp_opt_t; 609 610 /* 611 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 612 */ 613 614 #ifdef _BIG_ENDIAN 615 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 616 (TCPOPT_TSTAMP << 8) | 10) 617 #else 618 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 619 (TCPOPT_NOP << 8) | TCPOPT_NOP) 620 #endif 621 622 /* 623 * Flags returned from tcp_parse_options. 624 */ 625 #define TCP_OPT_MSS_PRESENT 1 626 #define TCP_OPT_WSCALE_PRESENT 2 627 #define TCP_OPT_TSTAMP_PRESENT 4 628 #define TCP_OPT_SACK_OK_PRESENT 8 629 #define TCP_OPT_SACK_PRESENT 16 630 631 /* TCP option length */ 632 #define TCPOPT_NOP_LEN 1 633 #define TCPOPT_MAXSEG_LEN 4 634 #define TCPOPT_WS_LEN 3 635 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 636 #define TCPOPT_TSTAMP_LEN 10 637 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 638 #define TCPOPT_SACK_OK_LEN 2 639 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 640 #define TCPOPT_REAL_SACK_LEN 4 641 #define TCPOPT_MAX_SACK_LEN 36 642 #define TCPOPT_HEADER_LEN 2 643 644 /* TCP cwnd burst factor. */ 645 #define TCP_CWND_INFINITE 65535 646 #define TCP_CWND_SS 3 647 #define TCP_CWND_NORMAL 5 648 649 /* Maximum TCP initial cwin (start/restart). */ 650 #define TCP_MAX_INIT_CWND 8 651 652 /* 653 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 654 * either tcp_slow_start_initial or tcp_slow_start_after idle 655 * depending on the caller. If the upper layer has not used the 656 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 657 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 658 * If the upper layer has changed set the tcp_init_cwnd, just use 659 * it to calculate the tcp_cwnd. 660 */ 661 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 662 { \ 663 if ((tcp)->tcp_init_cwnd == 0) { \ 664 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 665 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 666 } else { \ 667 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 668 } \ 669 tcp->tcp_cwnd_cnt = 0; \ 670 } 671 672 /* TCP Timer control structure */ 673 typedef struct tcpt_s { 674 pfv_t tcpt_pfv; /* The routine we are to call */ 675 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 676 } tcpt_t; 677 678 /* Host Specific Parameter structure */ 679 typedef struct tcp_hsp { 680 struct tcp_hsp *tcp_hsp_next; 681 in6_addr_t tcp_hsp_addr_v6; 682 in6_addr_t tcp_hsp_subnet_v6; 683 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 684 int32_t tcp_hsp_sendspace; 685 int32_t tcp_hsp_recvspace; 686 int32_t tcp_hsp_tstamp; 687 } tcp_hsp_t; 688 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 689 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 690 691 /* 692 * Functions called directly via squeue having a prototype of edesc_t. 693 */ 694 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 695 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 696 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 697 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 698 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 699 void tcp_input(void *arg, mblk_t *mp, void *arg2); 700 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 701 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 702 void tcp_output(void *arg, mblk_t *mp, void *arg2); 703 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 704 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 705 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 706 707 708 /* Prototype for TCP functions */ 709 static void tcp_random_init(void); 710 int tcp_random(void); 711 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 712 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 713 tcp_t *eager); 714 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 715 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 716 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 717 boolean_t user_specified); 718 static void tcp_closei_local(tcp_t *tcp); 719 static void tcp_close_detached(tcp_t *tcp); 720 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 721 mblk_t *idmp, mblk_t **defermp); 722 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 723 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 724 in_port_t dstport, uint_t srcid); 725 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 726 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 727 uint32_t scope_id); 728 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 729 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 730 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 731 static char *tcp_display(tcp_t *tcp, char *, char); 732 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 733 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 734 static void tcp_eager_unlink(tcp_t *tcp); 735 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 736 int unixerr); 737 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 738 int tlierr, int unixerr); 739 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 740 cred_t *cr); 741 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 742 char *value, caddr_t cp, cred_t *cr); 743 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 744 char *value, caddr_t cp, cred_t *cr); 745 static int tcp_tpistate(tcp_t *tcp); 746 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 747 int caller_holds_lock); 748 static void tcp_bind_hash_remove(tcp_t *tcp); 749 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 750 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 751 static void tcp_acceptor_hash_remove(tcp_t *tcp); 752 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 753 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 754 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 755 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 756 void tcp_g_q_setup(tcp_stack_t *); 757 void tcp_g_q_create(tcp_stack_t *); 758 void tcp_g_q_destroy(tcp_stack_t *); 759 static int tcp_header_init_ipv4(tcp_t *tcp); 760 static int tcp_header_init_ipv6(tcp_t *tcp); 761 int tcp_init(tcp_t *tcp, queue_t *q); 762 static int tcp_init_values(tcp_t *tcp); 763 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 764 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 765 t_scalar_t addr_length); 766 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 767 static void tcp_ip_notify(tcp_t *tcp); 768 static mblk_t *tcp_ire_mp(mblk_t *mp); 769 static void tcp_iss_init(tcp_t *tcp); 770 static void tcp_keepalive_killer(void *arg); 771 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 772 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 773 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 774 int *do_disconnectp, int *t_errorp, int *sys_errorp); 775 static boolean_t tcp_allow_connopt_set(int level, int name); 776 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 777 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 778 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 779 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 780 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 781 mblk_t *mblk); 782 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 783 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 784 uchar_t *ptr, uint_t len); 785 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 786 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 787 tcp_stack_t *); 788 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 789 caddr_t cp, cred_t *cr); 790 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 791 caddr_t cp, cred_t *cr); 792 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 793 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 794 caddr_t cp, cred_t *cr); 795 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 796 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 797 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 798 static void tcp_reinit(tcp_t *tcp); 799 static void tcp_reinit_values(tcp_t *tcp); 800 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 801 tcp_t *thisstream, cred_t *cr); 802 803 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 804 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 805 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 806 static void tcp_ss_rexmit(tcp_t *tcp); 807 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 808 static void tcp_process_options(tcp_t *, tcph_t *); 809 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 810 static void tcp_rsrv(queue_t *q); 811 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 812 static int tcp_snmp_state(tcp_t *tcp); 813 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 814 cred_t *cr); 815 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 816 cred_t *cr); 817 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 818 cred_t *cr); 819 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 820 cred_t *cr); 821 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 822 cred_t *cr); 823 static int tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, 824 caddr_t cp, cred_t *cr); 825 static int tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, 826 caddr_t cp, cred_t *cr); 827 static int tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, 828 cred_t *cr); 829 static void tcp_timer(void *arg); 830 static void tcp_timer_callback(void *); 831 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 832 boolean_t random); 833 static in_port_t tcp_get_next_priv_port(const tcp_t *); 834 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 835 void tcp_wput_accept(queue_t *q, mblk_t *mp); 836 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 837 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 838 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 839 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 840 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 841 const int num_sack_blk, int *usable, uint_t *snxt, 842 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 843 const int mdt_thres); 844 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 845 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 846 const int num_sack_blk, int *usable, uint_t *snxt, 847 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 848 const int mdt_thres); 849 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 850 int num_sack_blk); 851 static void tcp_wsrv(queue_t *q); 852 static int tcp_xmit_end(tcp_t *tcp); 853 static void tcp_ack_timer(void *arg); 854 static mblk_t *tcp_ack_mp(tcp_t *tcp); 855 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 856 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 857 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 858 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 859 uint32_t ack, int ctl); 860 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 861 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 862 static int setmaxps(queue_t *q, int maxpsz); 863 static void tcp_set_rto(tcp_t *, time_t); 864 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 865 boolean_t, boolean_t); 866 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 867 boolean_t ipsec_mctl); 868 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 869 char *opt, int optlen); 870 static int tcp_build_hdrs(queue_t *, tcp_t *); 871 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 872 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 873 tcph_t *tcph); 874 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 875 boolean_t tcp_reserved_port_add(int, in_port_t *, in_port_t *); 876 boolean_t tcp_reserved_port_del(in_port_t, in_port_t); 877 boolean_t tcp_reserved_port_check(in_port_t, tcp_stack_t *); 878 static tcp_t *tcp_alloc_temp_tcp(in_port_t, tcp_stack_t *); 879 static int tcp_reserved_port_list(queue_t *, mblk_t *, caddr_t, cred_t *); 880 static mblk_t *tcp_mdt_info_mp(mblk_t *); 881 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 882 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 883 const boolean_t, const uint32_t, const uint32_t, 884 const uint32_t, const uint32_t, tcp_stack_t *); 885 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 886 const uint_t, const uint_t, boolean_t *); 887 static mblk_t *tcp_lso_info_mp(mblk_t *); 888 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 889 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 890 extern mblk_t *tcp_timermp_alloc(int); 891 extern void tcp_timermp_free(tcp_t *); 892 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 893 static void tcp_stop_lingering(tcp_t *tcp); 894 static void tcp_close_linger_timeout(void *arg); 895 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 896 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 897 static void tcp_stack_fini(netstackid_t stackid, void *arg); 898 static void *tcp_g_kstat_init(tcp_g_stat_t *); 899 static void tcp_g_kstat_fini(kstat_t *); 900 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 901 static void tcp_kstat_fini(netstackid_t, kstat_t *); 902 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 903 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 904 static int tcp_kstat_update(kstat_t *kp, int rw); 905 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 906 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 907 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 908 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 909 tcph_t *tcph, mblk_t *idmp); 910 static squeue_func_t tcp_squeue_switch(int); 911 912 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *); 913 static int tcp_close(queue_t *, int); 914 static int tcpclose_accept(queue_t *); 915 static int tcp_modclose(queue_t *); 916 static void tcp_wput_mod(queue_t *, mblk_t *); 917 918 static void tcp_squeue_add(squeue_t *); 919 static boolean_t tcp_zcopy_check(tcp_t *); 920 static void tcp_zcopy_notify(tcp_t *); 921 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 922 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 923 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 924 925 extern void tcp_kssl_input(tcp_t *, mblk_t *); 926 927 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 928 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 929 930 /* 931 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 932 * 933 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 934 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 935 * (defined in tcp.h) needs to be filled in and passed into the kernel 936 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 937 * structure contains the four-tuple of a TCP connection and a range of TCP 938 * states (specified by ac_start and ac_end). The use of wildcard addresses 939 * and ports is allowed. Connections with a matching four tuple and a state 940 * within the specified range will be aborted. The valid states for the 941 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 942 * inclusive. 943 * 944 * An application which has its connection aborted by this ioctl will receive 945 * an error that is dependent on the connection state at the time of the abort. 946 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 947 * though a RST packet has been received. If the connection state is equal to 948 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 949 * and all resources associated with the connection will be freed. 950 */ 951 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 952 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 953 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 954 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 955 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 956 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 957 boolean_t, tcp_stack_t *); 958 959 static struct module_info tcp_rinfo = { 960 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 961 }; 962 963 static struct module_info tcp_winfo = { 964 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 965 }; 966 967 /* 968 * Entry points for TCP as a module. It only allows SNMP requests 969 * to pass through. 970 */ 971 struct qinit tcp_mod_rinit = { 972 (pfi_t)putnext, NULL, tcp_open, ip_snmpmod_close, NULL, &tcp_rinfo, 973 }; 974 975 struct qinit tcp_mod_winit = { 976 (pfi_t)ip_snmpmod_wput, NULL, tcp_open, ip_snmpmod_close, NULL, 977 &tcp_rinfo 978 }; 979 980 /* 981 * Entry points for TCP as a device. The normal case which supports 982 * the TCP functionality. 983 */ 984 struct qinit tcp_rinit = { 985 NULL, (pfi_t)tcp_rsrv, tcp_open, tcp_close, NULL, &tcp_rinfo 986 }; 987 988 struct qinit tcp_winit = { 989 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 990 }; 991 992 /* Initial entry point for TCP in socket mode. */ 993 struct qinit tcp_sock_winit = { 994 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 995 }; 996 997 /* 998 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 999 * an accept. Avoid allocating data structures since eager has already 1000 * been created. 1001 */ 1002 struct qinit tcp_acceptor_rinit = { 1003 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1004 }; 1005 1006 struct qinit tcp_acceptor_winit = { 1007 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1008 }; 1009 1010 /* 1011 * Entry points for TCP loopback (read side only) 1012 */ 1013 struct qinit tcp_loopback_rinit = { 1014 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_open, tcp_close, (pfi_t)0, 1015 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1016 }; 1017 1018 struct streamtab tcpinfo = { 1019 &tcp_rinit, &tcp_winit 1020 }; 1021 1022 /* 1023 * Have to ensure that tcp_g_q_close is not done by an 1024 * interrupt thread. 1025 */ 1026 static taskq_t *tcp_taskq; 1027 1028 /* 1029 * TCP has a private interface for other kernel modules to reserve a 1030 * port range for them to use. Once reserved, TCP will not use any ports 1031 * in the range. This interface relies on the TCP_EXCLBIND feature. If 1032 * the semantics of TCP_EXCLBIND is changed, implementation of this interface 1033 * has to be verified. 1034 * 1035 * There can be TCP_RESERVED_PORTS_ARRAY_MAX_SIZE port ranges. Each port 1036 * range can cover at most TCP_RESERVED_PORTS_RANGE_MAX ports. A port 1037 * range is [port a, port b] inclusive. And each port range is between 1038 * TCP_LOWESET_RESERVED_PORT and TCP_LARGEST_RESERVED_PORT inclusive. 1039 * 1040 * Note that the default anonymous port range starts from 32768. There is 1041 * no port "collision" between that and the reserved port range. If there 1042 * is port collision (because the default smallest anonymous port is lowered 1043 * or some apps specifically bind to ports in the reserved port range), the 1044 * system may not be able to reserve a port range even there are enough 1045 * unbound ports as a reserved port range contains consecutive ports . 1046 */ 1047 #define TCP_RESERVED_PORTS_ARRAY_MAX_SIZE 5 1048 #define TCP_RESERVED_PORTS_RANGE_MAX 1000 1049 #define TCP_SMALLEST_RESERVED_PORT 10240 1050 #define TCP_LARGEST_RESERVED_PORT 20480 1051 1052 /* Structure to represent those reserved port ranges. */ 1053 typedef struct tcp_rport_s { 1054 in_port_t lo_port; 1055 in_port_t hi_port; 1056 tcp_t **temp_tcp_array; 1057 } tcp_rport_t; 1058 1059 /* Setable only in /etc/system. Move to ndd? */ 1060 boolean_t tcp_icmp_source_quench = B_FALSE; 1061 1062 /* 1063 * Following assumes TPI alignment requirements stay along 32 bit 1064 * boundaries 1065 */ 1066 #define ROUNDUP32(x) \ 1067 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1068 1069 /* Template for response to info request. */ 1070 static struct T_info_ack tcp_g_t_info_ack = { 1071 T_INFO_ACK, /* PRIM_type */ 1072 0, /* TSDU_size */ 1073 T_INFINITE, /* ETSDU_size */ 1074 T_INVALID, /* CDATA_size */ 1075 T_INVALID, /* DDATA_size */ 1076 sizeof (sin_t), /* ADDR_size */ 1077 0, /* OPT_size - not initialized here */ 1078 TIDUSZ, /* TIDU_size */ 1079 T_COTS_ORD, /* SERV_type */ 1080 TCPS_IDLE, /* CURRENT_state */ 1081 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1082 }; 1083 1084 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1085 T_INFO_ACK, /* PRIM_type */ 1086 0, /* TSDU_size */ 1087 T_INFINITE, /* ETSDU_size */ 1088 T_INVALID, /* CDATA_size */ 1089 T_INVALID, /* DDATA_size */ 1090 sizeof (sin6_t), /* ADDR_size */ 1091 0, /* OPT_size - not initialized here */ 1092 TIDUSZ, /* TIDU_size */ 1093 T_COTS_ORD, /* SERV_type */ 1094 TCPS_IDLE, /* CURRENT_state */ 1095 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1096 }; 1097 1098 #define MS 1L 1099 #define SECONDS (1000 * MS) 1100 #define MINUTES (60 * SECONDS) 1101 #define HOURS (60 * MINUTES) 1102 #define DAYS (24 * HOURS) 1103 1104 #define PARAM_MAX (~(uint32_t)0) 1105 1106 /* Max size IP datagram is 64k - 1 */ 1107 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1108 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1109 /* Max of the above */ 1110 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1111 1112 /* Largest TCP port number */ 1113 #define TCP_MAX_PORT (64 * 1024 - 1) 1114 1115 /* 1116 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1117 * layer header. It has to be a multiple of 4. 1118 */ 1119 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1120 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1121 1122 /* 1123 * All of these are alterable, within the min/max values given, at run time. 1124 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1125 * per the TCP spec. 1126 */ 1127 /* BEGIN CSTYLED */ 1128 static tcpparam_t lcl_tcp_param_arr[] = { 1129 /*min max value name */ 1130 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1131 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1132 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1133 { 1, 1024, 1, "tcp_conn_req_min" }, 1134 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1135 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1136 { 0, 10, 0, "tcp_debug" }, 1137 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1138 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1139 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1140 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1141 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1142 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1143 { 1, 255, 64, "tcp_ipv4_ttl"}, 1144 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1145 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1146 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1147 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1148 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1149 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1150 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1151 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1152 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1153 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1154 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1155 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1156 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1157 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1158 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1159 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1160 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1161 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1162 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1163 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1164 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1165 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1166 { 0, TCP_MSS_MAX, 64, "tcp_co_min"}, 1167 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1168 /* 1169 * Question: What default value should I set for tcp_strong_iss? 1170 */ 1171 { 0, 2, 1, "tcp_strong_iss"}, 1172 { 0, 65536, 20, "tcp_rtt_updates"}, 1173 { 0, 1, 1, "tcp_wscale_always"}, 1174 { 0, 1, 0, "tcp_tstamp_always"}, 1175 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1176 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1177 { 0, 16, 2, "tcp_deferred_acks_max"}, 1178 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1179 { 1, 4, 4, "tcp_slow_start_initial"}, 1180 { 10*MS, 50*MS, 20*MS, "tcp_co_timer_interval"}, 1181 { 0, 2, 2, "tcp_sack_permitted"}, 1182 { 0, 1, 0, "tcp_trace"}, 1183 { 0, 1, 1, "tcp_compression_enabled"}, 1184 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1185 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1186 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1187 { 0, 1, 0, "tcp_rev_src_routes"}, 1188 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1189 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1190 { 0, 16, 8, "tcp_local_dacks_max"}, 1191 { 0, 2, 1, "tcp_ecn_permitted"}, 1192 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1193 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1194 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1195 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1196 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1197 }; 1198 /* END CSTYLED */ 1199 1200 /* 1201 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1202 * each header fragment in the header buffer. Each parameter value has 1203 * to be a multiple of 4 (32-bit aligned). 1204 */ 1205 static tcpparam_t lcl_tcp_mdt_head_param = 1206 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1207 static tcpparam_t lcl_tcp_mdt_tail_param = 1208 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1209 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1210 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1211 1212 /* 1213 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1214 * the maximum number of payload buffers associated per Multidata. 1215 */ 1216 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1217 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1218 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1219 1220 /* Round up the value to the nearest mss. */ 1221 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1222 1223 /* 1224 * Set ECN capable transport (ECT) code point in IP header. 1225 * 1226 * Note that there are 2 ECT code points '01' and '10', which are called 1227 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1228 * point ECT(0) for TCP as described in RFC 2481. 1229 */ 1230 #define SET_ECT(tcp, iph) \ 1231 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1232 /* We need to clear the code point first. */ \ 1233 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1234 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1235 } else { \ 1236 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1237 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1238 } 1239 1240 /* 1241 * The format argument to pass to tcp_display(). 1242 * DISP_PORT_ONLY means that the returned string has only port info. 1243 * DISP_ADDR_AND_PORT means that the returned string also contains the 1244 * remote and local IP address. 1245 */ 1246 #define DISP_PORT_ONLY 1 1247 #define DISP_ADDR_AND_PORT 2 1248 1249 #define NDD_TOO_QUICK_MSG \ 1250 "ndd get info rate too high for non-privileged users, try again " \ 1251 "later.\n" 1252 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1253 1254 #define IS_VMLOANED_MBLK(mp) \ 1255 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1256 1257 1258 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1259 boolean_t tcp_mdt_chain = B_TRUE; 1260 1261 /* 1262 * MDT threshold in the form of effective send MSS multiplier; we take 1263 * the MDT path if the amount of unsent data exceeds the threshold value 1264 * (default threshold is 1*SMSS). 1265 */ 1266 uint_t tcp_mdt_smss_threshold = 1; 1267 1268 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1269 1270 /* 1271 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1272 * tunable settable via NDD. Otherwise, the per-connection behavior is 1273 * determined dynamically during tcp_adapt_ire(), which is the default. 1274 */ 1275 boolean_t tcp_static_maxpsz = B_FALSE; 1276 1277 /* Setable in /etc/system */ 1278 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1279 uint32_t tcp_random_anon_port = 1; 1280 1281 /* 1282 * To reach to an eager in Q0 which can be dropped due to an incoming 1283 * new SYN request when Q0 is full, a new doubly linked list is 1284 * introduced. This list allows to select an eager from Q0 in O(1) time. 1285 * This is needed to avoid spending too much time walking through the 1286 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1287 * this new list has to be a member of Q0. 1288 * This list is headed by listener's tcp_t. When the list is empty, 1289 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1290 * of listener's tcp_t point to listener's tcp_t itself. 1291 * 1292 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1293 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1294 * These macros do not affect the eager's membership to Q0. 1295 */ 1296 1297 1298 #define MAKE_DROPPABLE(listener, eager) \ 1299 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1300 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1301 = (eager); \ 1302 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1303 (eager)->tcp_eager_next_drop_q0 = \ 1304 (listener)->tcp_eager_next_drop_q0; \ 1305 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1306 } 1307 1308 #define MAKE_UNDROPPABLE(eager) \ 1309 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1310 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1311 = (eager)->tcp_eager_prev_drop_q0; \ 1312 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1313 = (eager)->tcp_eager_next_drop_q0; \ 1314 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1315 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1316 } 1317 1318 /* 1319 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1320 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1321 * data, TCP will not respond with an ACK. RFC 793 requires that 1322 * TCP responds with an ACK for such a bogus ACK. By not following 1323 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1324 * an attacker successfully spoofs an acceptable segment to our 1325 * peer; or when our peer is "confused." 1326 */ 1327 uint32_t tcp_drop_ack_unsent_cnt = 10; 1328 1329 /* 1330 * Hook functions to enable cluster networking 1331 * On non-clustered systems these vectors must always be NULL. 1332 */ 1333 1334 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1335 uint8_t *laddrp, in_port_t lport) = NULL; 1336 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1337 uint8_t *laddrp, in_port_t lport) = NULL; 1338 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1339 uint8_t *laddrp, in_port_t lport, 1340 uint8_t *faddrp, in_port_t fport) = NULL; 1341 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1342 uint8_t *laddrp, in_port_t lport, 1343 uint8_t *faddrp, in_port_t fport) = NULL; 1344 1345 /* 1346 * The following are defined in ip.c 1347 */ 1348 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1349 uint8_t *laddrp); 1350 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1351 uint8_t *laddrp, uint8_t *faddrp); 1352 1353 #define CL_INET_CONNECT(tcp) { \ 1354 if (cl_inet_connect != NULL) { \ 1355 /* \ 1356 * Running in cluster mode - register active connection \ 1357 * information \ 1358 */ \ 1359 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1360 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1361 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1362 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1363 (in_port_t)(tcp)->tcp_lport, \ 1364 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1365 (in_port_t)(tcp)->tcp_fport); \ 1366 } \ 1367 } else { \ 1368 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1369 &(tcp)->tcp_ip6h->ip6_src)) {\ 1370 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1371 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1372 (in_port_t)(tcp)->tcp_lport, \ 1373 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1374 (in_port_t)(tcp)->tcp_fport); \ 1375 } \ 1376 } \ 1377 } \ 1378 } 1379 1380 #define CL_INET_DISCONNECT(tcp) { \ 1381 if (cl_inet_disconnect != NULL) { \ 1382 /* \ 1383 * Running in cluster mode - deregister active \ 1384 * connection information \ 1385 */ \ 1386 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1387 if ((tcp)->tcp_ip_src != 0) { \ 1388 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1389 AF_INET, \ 1390 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1391 (in_port_t)(tcp)->tcp_lport, \ 1392 (uint8_t *) \ 1393 (&((tcp)->tcp_ipha->ipha_dst)),\ 1394 (in_port_t)(tcp)->tcp_fport); \ 1395 } \ 1396 } else { \ 1397 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1398 &(tcp)->tcp_ip_src_v6)) { \ 1399 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1400 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1401 (in_port_t)(tcp)->tcp_lport, \ 1402 (uint8_t *) \ 1403 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1404 (in_port_t)(tcp)->tcp_fport); \ 1405 } \ 1406 } \ 1407 } \ 1408 } 1409 1410 /* 1411 * Cluster networking hook for traversing current connection list. 1412 * This routine is used to extract the current list of live connections 1413 * which must continue to to be dispatched to this node. 1414 */ 1415 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1416 1417 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1418 void *arg, tcp_stack_t *tcps); 1419 1420 /* 1421 * Figure out the value of window scale opton. Note that the rwnd is 1422 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1423 * We cannot find the scale value and then do a round up of tcp_rwnd 1424 * because the scale value may not be correct after that. 1425 * 1426 * Set the compiler flag to make this function inline. 1427 */ 1428 static void 1429 tcp_set_ws_value(tcp_t *tcp) 1430 { 1431 int i; 1432 uint32_t rwnd = tcp->tcp_rwnd; 1433 1434 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1435 i++, rwnd >>= 1) 1436 ; 1437 tcp->tcp_rcv_ws = i; 1438 } 1439 1440 /* 1441 * Remove a connection from the list of detached TIME_WAIT connections. 1442 * It returns B_FALSE if it can't remove the connection from the list 1443 * as the connection has already been removed from the list due to an 1444 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1445 */ 1446 static boolean_t 1447 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1448 { 1449 boolean_t locked = B_FALSE; 1450 1451 if (tcp_time_wait == NULL) { 1452 tcp_time_wait = *((tcp_squeue_priv_t **) 1453 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1454 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1455 locked = B_TRUE; 1456 } else { 1457 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1458 } 1459 1460 if (tcp->tcp_time_wait_expire == 0) { 1461 ASSERT(tcp->tcp_time_wait_next == NULL); 1462 ASSERT(tcp->tcp_time_wait_prev == NULL); 1463 if (locked) 1464 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1465 return (B_FALSE); 1466 } 1467 ASSERT(TCP_IS_DETACHED(tcp)); 1468 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1469 1470 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1471 ASSERT(tcp->tcp_time_wait_prev == NULL); 1472 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1473 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1474 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1475 NULL; 1476 } else { 1477 tcp_time_wait->tcp_time_wait_tail = NULL; 1478 } 1479 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1480 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1481 ASSERT(tcp->tcp_time_wait_next == NULL); 1482 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1483 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1484 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1485 } else { 1486 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1487 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1488 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1489 tcp->tcp_time_wait_next; 1490 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1491 tcp->tcp_time_wait_prev; 1492 } 1493 tcp->tcp_time_wait_next = NULL; 1494 tcp->tcp_time_wait_prev = NULL; 1495 tcp->tcp_time_wait_expire = 0; 1496 1497 if (locked) 1498 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1499 return (B_TRUE); 1500 } 1501 1502 /* 1503 * Add a connection to the list of detached TIME_WAIT connections 1504 * and set its time to expire. 1505 */ 1506 static void 1507 tcp_time_wait_append(tcp_t *tcp) 1508 { 1509 tcp_stack_t *tcps = tcp->tcp_tcps; 1510 tcp_squeue_priv_t *tcp_time_wait = 1511 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1512 SQPRIVATE_TCP)); 1513 1514 tcp_timers_stop(tcp); 1515 1516 /* Freed above */ 1517 ASSERT(tcp->tcp_timer_tid == 0); 1518 ASSERT(tcp->tcp_ack_tid == 0); 1519 1520 /* must have happened at the time of detaching the tcp */ 1521 ASSERT(tcp->tcp_ptpahn == NULL); 1522 ASSERT(tcp->tcp_flow_stopped == 0); 1523 ASSERT(tcp->tcp_time_wait_next == NULL); 1524 ASSERT(tcp->tcp_time_wait_prev == NULL); 1525 ASSERT(tcp->tcp_time_wait_expire == NULL); 1526 ASSERT(tcp->tcp_listener == NULL); 1527 1528 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1529 /* 1530 * The value computed below in tcp->tcp_time_wait_expire may 1531 * appear negative or wrap around. That is ok since our 1532 * interest is only in the difference between the current lbolt 1533 * value and tcp->tcp_time_wait_expire. But the value should not 1534 * be zero, since it means the tcp is not in the TIME_WAIT list. 1535 * The corresponding comparison in tcp_time_wait_collector() uses 1536 * modular arithmetic. 1537 */ 1538 tcp->tcp_time_wait_expire += 1539 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1540 if (tcp->tcp_time_wait_expire == 0) 1541 tcp->tcp_time_wait_expire = 1; 1542 1543 ASSERT(TCP_IS_DETACHED(tcp)); 1544 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1545 ASSERT(tcp->tcp_time_wait_next == NULL); 1546 ASSERT(tcp->tcp_time_wait_prev == NULL); 1547 TCP_DBGSTAT(tcps, tcp_time_wait); 1548 1549 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1550 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1551 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1552 tcp_time_wait->tcp_time_wait_head = tcp; 1553 } else { 1554 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1555 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1556 TCPS_TIME_WAIT); 1557 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1558 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1559 } 1560 tcp_time_wait->tcp_time_wait_tail = tcp; 1561 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1562 } 1563 1564 /* ARGSUSED */ 1565 void 1566 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1567 { 1568 conn_t *connp = (conn_t *)arg; 1569 tcp_t *tcp = connp->conn_tcp; 1570 tcp_stack_t *tcps = tcp->tcp_tcps; 1571 1572 ASSERT(tcp != NULL); 1573 if (tcp->tcp_state == TCPS_CLOSED) { 1574 return; 1575 } 1576 1577 ASSERT((tcp->tcp_family == AF_INET && 1578 tcp->tcp_ipversion == IPV4_VERSION) || 1579 (tcp->tcp_family == AF_INET6 && 1580 (tcp->tcp_ipversion == IPV4_VERSION || 1581 tcp->tcp_ipversion == IPV6_VERSION))); 1582 ASSERT(!tcp->tcp_listener); 1583 1584 TCP_STAT(tcps, tcp_time_wait_reap); 1585 ASSERT(TCP_IS_DETACHED(tcp)); 1586 1587 /* 1588 * Because they have no upstream client to rebind or tcp_close() 1589 * them later, we axe the connection here and now. 1590 */ 1591 tcp_close_detached(tcp); 1592 } 1593 1594 /* 1595 * Remove cached/latched IPsec references. 1596 */ 1597 void 1598 tcp_ipsec_cleanup(tcp_t *tcp) 1599 { 1600 conn_t *connp = tcp->tcp_connp; 1601 1602 if (connp->conn_flags & IPCL_TCPCONN) { 1603 if (connp->conn_latch != NULL) { 1604 IPLATCH_REFRELE(connp->conn_latch, 1605 connp->conn_netstack); 1606 connp->conn_latch = NULL; 1607 } 1608 if (connp->conn_policy != NULL) { 1609 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1610 connp->conn_policy = NULL; 1611 } 1612 } 1613 } 1614 1615 /* 1616 * Cleaup before placing on free list. 1617 * Disassociate from the netstack/tcp_stack_t since the freelist 1618 * is per squeue and not per netstack. 1619 */ 1620 void 1621 tcp_cleanup(tcp_t *tcp) 1622 { 1623 mblk_t *mp; 1624 char *tcp_iphc; 1625 int tcp_iphc_len; 1626 int tcp_hdr_grown; 1627 tcp_sack_info_t *tcp_sack_info; 1628 conn_t *connp = tcp->tcp_connp; 1629 tcp_stack_t *tcps = tcp->tcp_tcps; 1630 netstack_t *ns = tcps->tcps_netstack; 1631 1632 tcp_bind_hash_remove(tcp); 1633 1634 /* Cleanup that which needs the netstack first */ 1635 tcp_ipsec_cleanup(tcp); 1636 1637 tcp_free(tcp); 1638 1639 /* Release any SSL context */ 1640 if (tcp->tcp_kssl_ent != NULL) { 1641 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1642 tcp->tcp_kssl_ent = NULL; 1643 } 1644 1645 if (tcp->tcp_kssl_ctx != NULL) { 1646 kssl_release_ctx(tcp->tcp_kssl_ctx); 1647 tcp->tcp_kssl_ctx = NULL; 1648 } 1649 tcp->tcp_kssl_pending = B_FALSE; 1650 1651 conn_delete_ire(connp, NULL); 1652 1653 /* 1654 * Since we will bzero the entire structure, we need to 1655 * remove it and reinsert it in global hash list. We 1656 * know the walkers can't get to this conn because we 1657 * had set CONDEMNED flag earlier and checked reference 1658 * under conn_lock so walker won't pick it and when we 1659 * go the ipcl_globalhash_remove() below, no walker 1660 * can get to it. 1661 */ 1662 ipcl_globalhash_remove(connp); 1663 1664 /* 1665 * Now it is safe to decrement the reference counts. 1666 * This might be the last reference on the netstack and TCPS 1667 * in which case it will cause the tcp_g_q_close and 1668 * the freeing of the IP Instance. 1669 */ 1670 connp->conn_netstack = NULL; 1671 netstack_rele(ns); 1672 ASSERT(tcps != NULL); 1673 tcp->tcp_tcps = NULL; 1674 TCPS_REFRELE(tcps); 1675 1676 /* Save some state */ 1677 mp = tcp->tcp_timercache; 1678 1679 tcp_sack_info = tcp->tcp_sack_info; 1680 tcp_iphc = tcp->tcp_iphc; 1681 tcp_iphc_len = tcp->tcp_iphc_len; 1682 tcp_hdr_grown = tcp->tcp_hdr_grown; 1683 1684 if (connp->conn_cred != NULL) 1685 crfree(connp->conn_cred); 1686 if (connp->conn_peercred != NULL) 1687 crfree(connp->conn_peercred); 1688 bzero(connp, sizeof (conn_t)); 1689 bzero(tcp, sizeof (tcp_t)); 1690 1691 /* restore the state */ 1692 tcp->tcp_timercache = mp; 1693 1694 tcp->tcp_sack_info = tcp_sack_info; 1695 tcp->tcp_iphc = tcp_iphc; 1696 tcp->tcp_iphc_len = tcp_iphc_len; 1697 tcp->tcp_hdr_grown = tcp_hdr_grown; 1698 1699 1700 tcp->tcp_connp = connp; 1701 1702 connp->conn_tcp = tcp; 1703 connp->conn_flags = IPCL_TCPCONN; 1704 connp->conn_state_flags = CONN_INCIPIENT; 1705 connp->conn_ulp = IPPROTO_TCP; 1706 connp->conn_ref = 1; 1707 } 1708 1709 /* 1710 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1711 * is done forwards from the head. 1712 * This walks all stack instances since 1713 * tcp_time_wait remains global across all stacks. 1714 */ 1715 /* ARGSUSED */ 1716 void 1717 tcp_time_wait_collector(void *arg) 1718 { 1719 tcp_t *tcp; 1720 clock_t now; 1721 mblk_t *mp; 1722 conn_t *connp; 1723 kmutex_t *lock; 1724 boolean_t removed; 1725 1726 squeue_t *sqp = (squeue_t *)arg; 1727 tcp_squeue_priv_t *tcp_time_wait = 1728 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1729 1730 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1731 tcp_time_wait->tcp_time_wait_tid = 0; 1732 1733 if (tcp_time_wait->tcp_free_list != NULL && 1734 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1735 TCP_G_STAT(tcp_freelist_cleanup); 1736 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1737 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1738 tcp->tcp_time_wait_next = NULL; 1739 tcp_time_wait->tcp_free_list_cnt--; 1740 ASSERT(tcp->tcp_tcps == NULL); 1741 CONN_DEC_REF(tcp->tcp_connp); 1742 } 1743 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1744 } 1745 1746 /* 1747 * In order to reap time waits reliably, we should use a 1748 * source of time that is not adjustable by the user -- hence 1749 * the call to ddi_get_lbolt(). 1750 */ 1751 now = ddi_get_lbolt(); 1752 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1753 /* 1754 * Compare times using modular arithmetic, since 1755 * lbolt can wrapover. 1756 */ 1757 if ((now - tcp->tcp_time_wait_expire) < 0) { 1758 break; 1759 } 1760 1761 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1762 ASSERT(removed); 1763 1764 connp = tcp->tcp_connp; 1765 ASSERT(connp->conn_fanout != NULL); 1766 lock = &connp->conn_fanout->connf_lock; 1767 /* 1768 * This is essentially a TW reclaim fast path optimization for 1769 * performance where the timewait collector checks under the 1770 * fanout lock (so that no one else can get access to the 1771 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1772 * the classifier hash list. If ref count is indeed 2, we can 1773 * just remove the conn under the fanout lock and avoid 1774 * cleaning up the conn under the squeue, provided that 1775 * clustering callbacks are not enabled. If clustering is 1776 * enabled, we need to make the clustering callback before 1777 * setting the CONDEMNED flag and after dropping all locks and 1778 * so we forego this optimization and fall back to the slow 1779 * path. Also please see the comments in tcp_closei_local 1780 * regarding the refcnt logic. 1781 * 1782 * Since we are holding the tcp_time_wait_lock, its better 1783 * not to block on the fanout_lock because other connections 1784 * can't add themselves to time_wait list. So we do a 1785 * tryenter instead of mutex_enter. 1786 */ 1787 if (mutex_tryenter(lock)) { 1788 mutex_enter(&connp->conn_lock); 1789 if ((connp->conn_ref == 2) && 1790 (cl_inet_disconnect == NULL)) { 1791 ipcl_hash_remove_locked(connp, 1792 connp->conn_fanout); 1793 /* 1794 * Set the CONDEMNED flag now itself so that 1795 * the refcnt cannot increase due to any 1796 * walker. But we have still not cleaned up 1797 * conn_ire_cache. This is still ok since 1798 * we are going to clean it up in tcp_cleanup 1799 * immediately and any interface unplumb 1800 * thread will wait till the ire is blown away 1801 */ 1802 connp->conn_state_flags |= CONN_CONDEMNED; 1803 mutex_exit(lock); 1804 mutex_exit(&connp->conn_lock); 1805 if (tcp_time_wait->tcp_free_list_cnt < 1806 tcp_free_list_max_cnt) { 1807 /* Add to head of tcp_free_list */ 1808 mutex_exit( 1809 &tcp_time_wait->tcp_time_wait_lock); 1810 tcp_cleanup(tcp); 1811 ASSERT(connp->conn_latch == NULL); 1812 ASSERT(connp->conn_policy == NULL); 1813 ASSERT(tcp->tcp_tcps == NULL); 1814 ASSERT(connp->conn_netstack == NULL); 1815 1816 mutex_enter( 1817 &tcp_time_wait->tcp_time_wait_lock); 1818 tcp->tcp_time_wait_next = 1819 tcp_time_wait->tcp_free_list; 1820 tcp_time_wait->tcp_free_list = tcp; 1821 tcp_time_wait->tcp_free_list_cnt++; 1822 continue; 1823 } else { 1824 /* Do not add to tcp_free_list */ 1825 mutex_exit( 1826 &tcp_time_wait->tcp_time_wait_lock); 1827 tcp_bind_hash_remove(tcp); 1828 conn_delete_ire(tcp->tcp_connp, NULL); 1829 tcp_ipsec_cleanup(tcp); 1830 CONN_DEC_REF(tcp->tcp_connp); 1831 } 1832 } else { 1833 CONN_INC_REF_LOCKED(connp); 1834 mutex_exit(lock); 1835 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1836 mutex_exit(&connp->conn_lock); 1837 /* 1838 * We can reuse the closemp here since conn has 1839 * detached (otherwise we wouldn't even be in 1840 * time_wait list). tcp_closemp_used can safely 1841 * be changed without taking a lock as no other 1842 * thread can concurrently access it at this 1843 * point in the connection lifecycle. 1844 */ 1845 1846 if (tcp->tcp_closemp.b_prev == NULL) 1847 tcp->tcp_closemp_used = B_TRUE; 1848 else 1849 cmn_err(CE_PANIC, 1850 "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_fill(connp->conn_sqp, mp, 1858 tcp_timewait_output, connp, 1859 SQTAG_TCP_TIMEWAIT); 1860 } 1861 } else { 1862 mutex_enter(&connp->conn_lock); 1863 CONN_INC_REF_LOCKED(connp); 1864 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1865 mutex_exit(&connp->conn_lock); 1866 /* 1867 * We can reuse the closemp here since conn has 1868 * detached (otherwise we wouldn't even be in 1869 * time_wait list). tcp_closemp_used can safely 1870 * be changed without taking a lock as no other 1871 * thread can concurrently access it at this 1872 * point in the connection lifecycle. 1873 */ 1874 1875 if (tcp->tcp_closemp.b_prev == NULL) 1876 tcp->tcp_closemp_used = B_TRUE; 1877 else 1878 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1879 "concurrent use of tcp_closemp: " 1880 "connp %p tcp %p\n", (void *)connp, 1881 (void *)tcp); 1882 1883 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1884 mp = &tcp->tcp_closemp; 1885 squeue_fill(connp->conn_sqp, mp, 1886 tcp_timewait_output, connp, 0); 1887 } 1888 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1889 } 1890 1891 if (tcp_time_wait->tcp_free_list != NULL) 1892 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1893 1894 tcp_time_wait->tcp_time_wait_tid = 1895 timeout(tcp_time_wait_collector, sqp, TCP_TIME_WAIT_DELAY); 1896 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1897 } 1898 /* 1899 * Reply to a clients T_CONN_RES TPI message. This function 1900 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1901 * on the acceptor STREAM and processed in tcp_wput_accept(). 1902 * Read the block comment on top of tcp_conn_request(). 1903 */ 1904 static void 1905 tcp_accept(tcp_t *listener, mblk_t *mp) 1906 { 1907 tcp_t *acceptor; 1908 tcp_t *eager; 1909 tcp_t *tcp; 1910 struct T_conn_res *tcr; 1911 t_uscalar_t acceptor_id; 1912 t_scalar_t seqnum; 1913 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1914 mblk_t *ok_mp; 1915 mblk_t *mp1; 1916 tcp_stack_t *tcps = listener->tcp_tcps; 1917 1918 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1919 tcp_err_ack(listener, mp, TPROTO, 0); 1920 return; 1921 } 1922 tcr = (struct T_conn_res *)mp->b_rptr; 1923 1924 /* 1925 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1926 * read side queue of the streams device underneath us i.e. the 1927 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1928 * look it up in the queue_hash. Under LP64 it sends down the 1929 * minor_t of the accepting endpoint. 1930 * 1931 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1932 * fanout hash lock is held. 1933 * This prevents any thread from entering the acceptor queue from 1934 * below (since it has not been hard bound yet i.e. any inbound 1935 * packets will arrive on the listener or default tcp queue and 1936 * go through tcp_lookup). 1937 * The CONN_INC_REF will prevent the acceptor from closing. 1938 * 1939 * XXX It is still possible for a tli application to send down data 1940 * on the accepting stream while another thread calls t_accept. 1941 * This should not be a problem for well-behaved applications since 1942 * the T_OK_ACK is sent after the queue swapping is completed. 1943 * 1944 * If the accepting fd is the same as the listening fd, avoid 1945 * queue hash lookup since that will return an eager listener in a 1946 * already established state. 1947 */ 1948 acceptor_id = tcr->ACCEPTOR_id; 1949 mutex_enter(&listener->tcp_eager_lock); 1950 if (listener->tcp_acceptor_id == acceptor_id) { 1951 eager = listener->tcp_eager_next_q; 1952 /* only count how many T_CONN_INDs so don't count q0 */ 1953 if ((listener->tcp_conn_req_cnt_q != 1) || 1954 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1955 mutex_exit(&listener->tcp_eager_lock); 1956 tcp_err_ack(listener, mp, TBADF, 0); 1957 return; 1958 } 1959 if (listener->tcp_conn_req_cnt_q0 != 0) { 1960 /* Throw away all the eagers on q0. */ 1961 tcp_eager_cleanup(listener, 1); 1962 } 1963 if (listener->tcp_syn_defense) { 1964 listener->tcp_syn_defense = B_FALSE; 1965 if (listener->tcp_ip_addr_cache != NULL) { 1966 kmem_free(listener->tcp_ip_addr_cache, 1967 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1968 listener->tcp_ip_addr_cache = NULL; 1969 } 1970 } 1971 /* 1972 * Transfer tcp_conn_req_max to the eager so that when 1973 * a disconnect occurs we can revert the endpoint to the 1974 * listen state. 1975 */ 1976 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1977 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1978 /* 1979 * Get a reference on the acceptor just like the 1980 * tcp_acceptor_hash_lookup below. 1981 */ 1982 acceptor = listener; 1983 CONN_INC_REF(acceptor->tcp_connp); 1984 } else { 1985 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1986 if (acceptor == NULL) { 1987 if (listener->tcp_debug) { 1988 (void) strlog(TCP_MOD_ID, 0, 1, 1989 SL_ERROR|SL_TRACE, 1990 "tcp_accept: did not find acceptor 0x%x\n", 1991 acceptor_id); 1992 } 1993 mutex_exit(&listener->tcp_eager_lock); 1994 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 1995 return; 1996 } 1997 /* 1998 * Verify acceptor state. The acceptable states for an acceptor 1999 * include TCPS_IDLE and TCPS_BOUND. 2000 */ 2001 switch (acceptor->tcp_state) { 2002 case TCPS_IDLE: 2003 /* FALLTHRU */ 2004 case TCPS_BOUND: 2005 break; 2006 default: 2007 CONN_DEC_REF(acceptor->tcp_connp); 2008 mutex_exit(&listener->tcp_eager_lock); 2009 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2010 return; 2011 } 2012 } 2013 2014 /* The listener must be in TCPS_LISTEN */ 2015 if (listener->tcp_state != TCPS_LISTEN) { 2016 CONN_DEC_REF(acceptor->tcp_connp); 2017 mutex_exit(&listener->tcp_eager_lock); 2018 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2019 return; 2020 } 2021 2022 /* 2023 * Rendezvous with an eager connection request packet hanging off 2024 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2025 * tcp structure when the connection packet arrived in 2026 * tcp_conn_request(). 2027 */ 2028 seqnum = tcr->SEQ_number; 2029 eager = listener; 2030 do { 2031 eager = eager->tcp_eager_next_q; 2032 if (eager == NULL) { 2033 CONN_DEC_REF(acceptor->tcp_connp); 2034 mutex_exit(&listener->tcp_eager_lock); 2035 tcp_err_ack(listener, mp, TBADSEQ, 0); 2036 return; 2037 } 2038 } while (eager->tcp_conn_req_seqnum != seqnum); 2039 mutex_exit(&listener->tcp_eager_lock); 2040 2041 /* 2042 * At this point, both acceptor and listener have 2 ref 2043 * that they begin with. Acceptor has one additional ref 2044 * we placed in lookup while listener has 3 additional 2045 * ref for being behind the squeue (tcp_accept() is 2046 * done on listener's squeue); being in classifier hash; 2047 * and eager's ref on listener. 2048 */ 2049 ASSERT(listener->tcp_connp->conn_ref >= 5); 2050 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2051 2052 /* 2053 * The eager at this point is set in its own squeue and 2054 * could easily have been killed (tcp_accept_finish will 2055 * deal with that) because of a TH_RST so we can only 2056 * ASSERT for a single ref. 2057 */ 2058 ASSERT(eager->tcp_connp->conn_ref >= 1); 2059 2060 /* Pre allocate the stroptions mblk also */ 2061 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2062 if (opt_mp == NULL) { 2063 CONN_DEC_REF(acceptor->tcp_connp); 2064 CONN_DEC_REF(eager->tcp_connp); 2065 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2066 return; 2067 } 2068 DB_TYPE(opt_mp) = M_SETOPTS; 2069 opt_mp->b_wptr += sizeof (struct stroptions); 2070 2071 /* 2072 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2073 * from listener to acceptor. The message is chained on opt_mp 2074 * which will be sent onto eager's squeue. 2075 */ 2076 if (listener->tcp_bound_if != 0) { 2077 /* allocate optmgmt req */ 2078 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2079 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2080 sizeof (int)); 2081 if (mp1 != NULL) 2082 linkb(opt_mp, mp1); 2083 } 2084 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2085 uint_t on = 1; 2086 2087 /* allocate optmgmt req */ 2088 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2089 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2090 if (mp1 != NULL) 2091 linkb(opt_mp, mp1); 2092 } 2093 2094 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2095 if ((mp1 = copymsg(mp)) == NULL) { 2096 CONN_DEC_REF(acceptor->tcp_connp); 2097 CONN_DEC_REF(eager->tcp_connp); 2098 freemsg(opt_mp); 2099 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2100 return; 2101 } 2102 2103 tcr = (struct T_conn_res *)mp1->b_rptr; 2104 2105 /* 2106 * This is an expanded version of mi_tpi_ok_ack_alloc() 2107 * which allocates a larger mblk and appends the new 2108 * local address to the ok_ack. The address is copied by 2109 * soaccept() for getsockname(). 2110 */ 2111 { 2112 int extra; 2113 2114 extra = (eager->tcp_family == AF_INET) ? 2115 sizeof (sin_t) : sizeof (sin6_t); 2116 2117 /* 2118 * Try to re-use mp, if possible. Otherwise, allocate 2119 * an mblk and return it as ok_mp. In any case, mp 2120 * is no longer usable upon return. 2121 */ 2122 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2123 CONN_DEC_REF(acceptor->tcp_connp); 2124 CONN_DEC_REF(eager->tcp_connp); 2125 freemsg(opt_mp); 2126 /* Original mp has been freed by now, so use mp1 */ 2127 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2128 return; 2129 } 2130 2131 mp = NULL; /* We should never use mp after this point */ 2132 2133 switch (extra) { 2134 case sizeof (sin_t): { 2135 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2136 2137 ok_mp->b_wptr += extra; 2138 sin->sin_family = AF_INET; 2139 sin->sin_port = eager->tcp_lport; 2140 sin->sin_addr.s_addr = 2141 eager->tcp_ipha->ipha_src; 2142 break; 2143 } 2144 case sizeof (sin6_t): { 2145 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2146 2147 ok_mp->b_wptr += extra; 2148 sin6->sin6_family = AF_INET6; 2149 sin6->sin6_port = eager->tcp_lport; 2150 if (eager->tcp_ipversion == IPV4_VERSION) { 2151 sin6->sin6_flowinfo = 0; 2152 IN6_IPADDR_TO_V4MAPPED( 2153 eager->tcp_ipha->ipha_src, 2154 &sin6->sin6_addr); 2155 } else { 2156 ASSERT(eager->tcp_ip6h != NULL); 2157 sin6->sin6_flowinfo = 2158 eager->tcp_ip6h->ip6_vcf & 2159 ~IPV6_VERS_AND_FLOW_MASK; 2160 sin6->sin6_addr = 2161 eager->tcp_ip6h->ip6_src; 2162 } 2163 sin6->sin6_scope_id = 0; 2164 sin6->__sin6_src_id = 0; 2165 break; 2166 } 2167 default: 2168 break; 2169 } 2170 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2171 } 2172 2173 /* 2174 * If there are no options we know that the T_CONN_RES will 2175 * succeed. However, we can't send the T_OK_ACK upstream until 2176 * the tcp_accept_swap is done since it would be dangerous to 2177 * let the application start using the new fd prior to the swap. 2178 */ 2179 tcp_accept_swap(listener, acceptor, eager); 2180 2181 /* 2182 * tcp_accept_swap unlinks eager from listener but does not drop 2183 * the eager's reference on the listener. 2184 */ 2185 ASSERT(eager->tcp_listener == NULL); 2186 ASSERT(listener->tcp_connp->conn_ref >= 5); 2187 2188 /* 2189 * The eager is now associated with its own queue. Insert in 2190 * the hash so that the connection can be reused for a future 2191 * T_CONN_RES. 2192 */ 2193 tcp_acceptor_hash_insert(acceptor_id, eager); 2194 2195 /* 2196 * We now do the processing of options with T_CONN_RES. 2197 * We delay till now since we wanted to have queue to pass to 2198 * option processing routines that points back to the right 2199 * instance structure which does not happen until after 2200 * tcp_accept_swap(). 2201 * 2202 * Note: 2203 * The sanity of the logic here assumes that whatever options 2204 * are appropriate to inherit from listner=>eager are done 2205 * before this point, and whatever were to be overridden (or not) 2206 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2207 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2208 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2209 * This may not be true at this point in time but can be fixed 2210 * independently. This option processing code starts with 2211 * the instantiated acceptor instance and the final queue at 2212 * this point. 2213 */ 2214 2215 if (tcr->OPT_length != 0) { 2216 /* Options to process */ 2217 int t_error = 0; 2218 int sys_error = 0; 2219 int do_disconnect = 0; 2220 2221 if (tcp_conprim_opt_process(eager, mp1, 2222 &do_disconnect, &t_error, &sys_error) < 0) { 2223 eager->tcp_accept_error = 1; 2224 if (do_disconnect) { 2225 /* 2226 * An option failed which does not allow 2227 * connection to be accepted. 2228 * 2229 * We allow T_CONN_RES to succeed and 2230 * put a T_DISCON_IND on the eager queue. 2231 */ 2232 ASSERT(t_error == 0 && sys_error == 0); 2233 eager->tcp_send_discon_ind = 1; 2234 } else { 2235 ASSERT(t_error != 0); 2236 freemsg(ok_mp); 2237 /* 2238 * Original mp was either freed or set 2239 * to ok_mp above, so use mp1 instead. 2240 */ 2241 tcp_err_ack(listener, mp1, t_error, sys_error); 2242 goto finish; 2243 } 2244 } 2245 /* 2246 * Most likely success in setting options (except if 2247 * eager->tcp_send_discon_ind set). 2248 * mp1 option buffer represented by OPT_length/offset 2249 * potentially modified and contains results of setting 2250 * options at this point 2251 */ 2252 } 2253 2254 /* We no longer need mp1, since all options processing has passed */ 2255 freemsg(mp1); 2256 2257 putnext(listener->tcp_rq, ok_mp); 2258 2259 mutex_enter(&listener->tcp_eager_lock); 2260 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2261 tcp_t *tail; 2262 mblk_t *conn_ind; 2263 2264 /* 2265 * This path should not be executed if listener and 2266 * acceptor streams are the same. 2267 */ 2268 ASSERT(listener != acceptor); 2269 2270 tcp = listener->tcp_eager_prev_q0; 2271 /* 2272 * listener->tcp_eager_prev_q0 points to the TAIL of the 2273 * deferred T_conn_ind queue. We need to get to the head of 2274 * the queue in order to send up T_conn_ind the same order as 2275 * how the 3WHS is completed. 2276 */ 2277 while (tcp != listener) { 2278 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2279 break; 2280 else 2281 tcp = tcp->tcp_eager_prev_q0; 2282 } 2283 ASSERT(tcp != listener); 2284 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2285 ASSERT(conn_ind != NULL); 2286 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2287 2288 /* Move from q0 to q */ 2289 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2290 listener->tcp_conn_req_cnt_q0--; 2291 listener->tcp_conn_req_cnt_q++; 2292 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2293 tcp->tcp_eager_prev_q0; 2294 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2295 tcp->tcp_eager_next_q0; 2296 tcp->tcp_eager_prev_q0 = NULL; 2297 tcp->tcp_eager_next_q0 = NULL; 2298 tcp->tcp_conn_def_q0 = B_FALSE; 2299 2300 /* Make sure the tcp isn't in the list of droppables */ 2301 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2302 tcp->tcp_eager_prev_drop_q0 == NULL); 2303 2304 /* 2305 * Insert at end of the queue because sockfs sends 2306 * down T_CONN_RES in chronological order. Leaving 2307 * the older conn indications at front of the queue 2308 * helps reducing search time. 2309 */ 2310 tail = listener->tcp_eager_last_q; 2311 if (tail != NULL) 2312 tail->tcp_eager_next_q = tcp; 2313 else 2314 listener->tcp_eager_next_q = tcp; 2315 listener->tcp_eager_last_q = tcp; 2316 tcp->tcp_eager_next_q = NULL; 2317 mutex_exit(&listener->tcp_eager_lock); 2318 putnext(tcp->tcp_rq, conn_ind); 2319 } else { 2320 mutex_exit(&listener->tcp_eager_lock); 2321 } 2322 2323 /* 2324 * Done with the acceptor - free it 2325 * 2326 * Note: from this point on, no access to listener should be made 2327 * as listener can be equal to acceptor. 2328 */ 2329 finish: 2330 ASSERT(acceptor->tcp_detached); 2331 ASSERT(tcps->tcps_g_q != NULL); 2332 acceptor->tcp_rq = tcps->tcps_g_q; 2333 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2334 (void) tcp_clean_death(acceptor, 0, 2); 2335 CONN_DEC_REF(acceptor->tcp_connp); 2336 2337 /* 2338 * In case we already received a FIN we have to make tcp_rput send 2339 * the ordrel_ind. This will also send up a window update if the window 2340 * has opened up. 2341 * 2342 * In the normal case of a successful connection acceptance 2343 * we give the O_T_BIND_REQ to the read side put procedure as an 2344 * indication that this was just accepted. This tells tcp_rput to 2345 * pass up any data queued in tcp_rcv_list. 2346 * 2347 * In the fringe case where options sent with T_CONN_RES failed and 2348 * we required, we would be indicating a T_DISCON_IND to blow 2349 * away this connection. 2350 */ 2351 2352 /* 2353 * XXX: we currently have a problem if XTI application closes the 2354 * acceptor stream in between. This problem exists in on10-gate also 2355 * and is well know but nothing can be done short of major rewrite 2356 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2357 * eager same squeue as listener (we can distinguish non socket 2358 * listeners at the time of handling a SYN in tcp_conn_request) 2359 * and do most of the work that tcp_accept_finish does here itself 2360 * and then get behind the acceptor squeue to access the acceptor 2361 * queue. 2362 */ 2363 /* 2364 * We already have a ref on tcp so no need to do one before squeue_fill 2365 */ 2366 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2367 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2368 } 2369 2370 /* 2371 * Swap information between the eager and acceptor for a TLI/XTI client. 2372 * The sockfs accept is done on the acceptor stream and control goes 2373 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2374 * called. In either case, both the eager and listener are in their own 2375 * perimeter (squeue) and the code has to deal with potential race. 2376 * 2377 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2378 */ 2379 static void 2380 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2381 { 2382 conn_t *econnp, *aconnp; 2383 2384 ASSERT(eager->tcp_rq == listener->tcp_rq); 2385 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2386 ASSERT(!eager->tcp_hard_bound); 2387 ASSERT(!TCP_IS_SOCKET(acceptor)); 2388 ASSERT(!TCP_IS_SOCKET(eager)); 2389 ASSERT(!TCP_IS_SOCKET(listener)); 2390 2391 acceptor->tcp_detached = B_TRUE; 2392 /* 2393 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2394 * the acceptor id. 2395 */ 2396 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2397 2398 /* remove eager from listen list... */ 2399 mutex_enter(&listener->tcp_eager_lock); 2400 tcp_eager_unlink(eager); 2401 ASSERT(eager->tcp_eager_next_q == NULL && 2402 eager->tcp_eager_last_q == NULL); 2403 ASSERT(eager->tcp_eager_next_q0 == NULL && 2404 eager->tcp_eager_prev_q0 == NULL); 2405 mutex_exit(&listener->tcp_eager_lock); 2406 eager->tcp_rq = acceptor->tcp_rq; 2407 eager->tcp_wq = acceptor->tcp_wq; 2408 2409 econnp = eager->tcp_connp; 2410 aconnp = acceptor->tcp_connp; 2411 2412 eager->tcp_rq->q_ptr = econnp; 2413 eager->tcp_wq->q_ptr = econnp; 2414 2415 /* 2416 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2417 * which might be a different squeue from our peer TCP instance. 2418 * For TCP Fusion, the peer expects that whenever tcp_detached is 2419 * clear, our TCP queues point to the acceptor's queues. Thus, use 2420 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2421 * above reach global visibility prior to the clearing of tcp_detached. 2422 */ 2423 membar_producer(); 2424 eager->tcp_detached = B_FALSE; 2425 2426 ASSERT(eager->tcp_ack_tid == 0); 2427 2428 econnp->conn_dev = aconnp->conn_dev; 2429 if (eager->tcp_cred != NULL) 2430 crfree(eager->tcp_cred); 2431 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2432 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2433 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2434 2435 aconnp->conn_cred = NULL; 2436 2437 econnp->conn_zoneid = aconnp->conn_zoneid; 2438 econnp->conn_allzones = aconnp->conn_allzones; 2439 2440 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2441 aconnp->conn_mac_exempt = B_FALSE; 2442 2443 ASSERT(aconnp->conn_peercred == NULL); 2444 2445 /* Do the IPC initialization */ 2446 CONN_INC_REF(econnp); 2447 2448 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2449 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2450 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2451 econnp->conn_ulp = aconnp->conn_ulp; 2452 2453 /* Done with old IPC. Drop its ref on its connp */ 2454 CONN_DEC_REF(aconnp); 2455 } 2456 2457 2458 /* 2459 * Adapt to the information, such as rtt and rtt_sd, provided from the 2460 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2461 * 2462 * Checks for multicast and broadcast destination address. 2463 * Returns zero on failure; non-zero if ok. 2464 * 2465 * Note that the MSS calculation here is based on the info given in 2466 * the IRE. We do not do any calculation based on TCP options. They 2467 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2468 * knows which options to use. 2469 * 2470 * Note on how TCP gets its parameters for a connection. 2471 * 2472 * When a tcp_t structure is allocated, it gets all the default parameters. 2473 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2474 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2475 * default. But if there is an associated tcp_host_param, it will override 2476 * the metrics. 2477 * 2478 * An incoming SYN with a multicast or broadcast destination address, is dropped 2479 * in 1 of 2 places. 2480 * 2481 * 1. If the packet was received over the wire it is dropped in 2482 * ip_rput_process_broadcast() 2483 * 2484 * 2. If the packet was received through internal IP loopback, i.e. the packet 2485 * was generated and received on the same machine, it is dropped in 2486 * ip_wput_local() 2487 * 2488 * An incoming SYN with a multicast or broadcast source address is always 2489 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2490 * reject an attempt to connect to a broadcast or multicast (destination) 2491 * address. 2492 */ 2493 static int 2494 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2495 { 2496 tcp_hsp_t *hsp; 2497 ire_t *ire; 2498 ire_t *sire = NULL; 2499 iulp_t *ire_uinfo = NULL; 2500 uint32_t mss_max; 2501 uint32_t mss; 2502 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2503 conn_t *connp = tcp->tcp_connp; 2504 boolean_t ire_cacheable = B_FALSE; 2505 zoneid_t zoneid = connp->conn_zoneid; 2506 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2507 MATCH_IRE_SECATTR; 2508 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2509 ill_t *ill = NULL; 2510 boolean_t incoming = (ire_mp == NULL); 2511 tcp_stack_t *tcps = tcp->tcp_tcps; 2512 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2513 2514 ASSERT(connp->conn_ire_cache == NULL); 2515 2516 if (tcp->tcp_ipversion == IPV4_VERSION) { 2517 2518 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2519 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2520 return (0); 2521 } 2522 /* 2523 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2524 * for the destination with the nexthop as gateway. 2525 * ire_ctable_lookup() is used because this particular 2526 * ire, if it exists, will be marked private. 2527 * If that is not available, use the interface ire 2528 * for the nexthop. 2529 * 2530 * TSol: tcp_update_label will detect label mismatches based 2531 * only on the destination's label, but that would not 2532 * detect label mismatches based on the security attributes 2533 * of routes or next hop gateway. Hence we need to pass the 2534 * label to ire_ftable_lookup below in order to locate the 2535 * right prefix (and/or) ire cache. Similarly we also need 2536 * pass the label to the ire_cache_lookup below to locate 2537 * the right ire that also matches on the label. 2538 */ 2539 if (tcp->tcp_connp->conn_nexthop_set) { 2540 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2541 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2542 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2543 ipst); 2544 if (ire == NULL) { 2545 ire = ire_ftable_lookup( 2546 tcp->tcp_connp->conn_nexthop_v4, 2547 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2548 tsl, match_flags, ipst); 2549 if (ire == NULL) 2550 return (0); 2551 } else { 2552 ire_uinfo = &ire->ire_uinfo; 2553 } 2554 } else { 2555 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2556 zoneid, tsl, ipst); 2557 if (ire != NULL) { 2558 ire_cacheable = B_TRUE; 2559 ire_uinfo = (ire_mp != NULL) ? 2560 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2561 &ire->ire_uinfo; 2562 2563 } else { 2564 if (ire_mp == NULL) { 2565 ire = ire_ftable_lookup( 2566 tcp->tcp_connp->conn_rem, 2567 0, 0, 0, NULL, &sire, zoneid, 0, 2568 tsl, (MATCH_IRE_RECURSIVE | 2569 MATCH_IRE_DEFAULT), ipst); 2570 if (ire == NULL) 2571 return (0); 2572 ire_uinfo = (sire != NULL) ? 2573 &sire->ire_uinfo : 2574 &ire->ire_uinfo; 2575 } else { 2576 ire = (ire_t *)ire_mp->b_rptr; 2577 ire_uinfo = 2578 &((ire_t *) 2579 ire_mp->b_rptr)->ire_uinfo; 2580 } 2581 } 2582 } 2583 ASSERT(ire != NULL); 2584 2585 if ((ire->ire_src_addr == INADDR_ANY) || 2586 (ire->ire_type & IRE_BROADCAST)) { 2587 /* 2588 * ire->ire_mp is non null when ire_mp passed in is used 2589 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2590 */ 2591 if (ire->ire_mp == NULL) 2592 ire_refrele(ire); 2593 if (sire != NULL) 2594 ire_refrele(sire); 2595 return (0); 2596 } 2597 2598 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2599 ipaddr_t src_addr; 2600 2601 /* 2602 * ip_bind_connected() has stored the correct source 2603 * address in conn_src. 2604 */ 2605 src_addr = tcp->tcp_connp->conn_src; 2606 tcp->tcp_ipha->ipha_src = src_addr; 2607 /* 2608 * Copy of the src addr. in tcp_t is needed 2609 * for the lookup funcs. 2610 */ 2611 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2612 } 2613 /* 2614 * Set the fragment bit so that IP will tell us if the MTU 2615 * should change. IP tells us the latest setting of 2616 * ip_path_mtu_discovery through ire_frag_flag. 2617 */ 2618 if (ipst->ips_ip_path_mtu_discovery) { 2619 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2620 htons(IPH_DF); 2621 } 2622 /* 2623 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2624 * for IP_NEXTHOP. No cache ire has been found for the 2625 * destination and we are working with the nexthop's 2626 * interface ire. Since we need to forward all packets 2627 * to the nexthop first, we "blindly" set tcp_localnet 2628 * to false, eventhough the destination may also be 2629 * onlink. 2630 */ 2631 if (ire_uinfo == NULL) 2632 tcp->tcp_localnet = 0; 2633 else 2634 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2635 } else { 2636 /* 2637 * For incoming connection ire_mp = NULL 2638 * For outgoing connection ire_mp != NULL 2639 * Technically we should check conn_incoming_ill 2640 * when ire_mp is NULL and conn_outgoing_ill when 2641 * ire_mp is non-NULL. But this is performance 2642 * critical path and for IPV*_BOUND_IF, outgoing 2643 * and incoming ill are always set to the same value. 2644 */ 2645 ill_t *dst_ill = NULL; 2646 ipif_t *dst_ipif = NULL; 2647 2648 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2649 2650 if (connp->conn_outgoing_ill != NULL) { 2651 /* Outgoing or incoming path */ 2652 int err; 2653 2654 dst_ill = conn_get_held_ill(connp, 2655 &connp->conn_outgoing_ill, &err); 2656 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2657 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2658 return (0); 2659 } 2660 match_flags |= MATCH_IRE_ILL; 2661 dst_ipif = dst_ill->ill_ipif; 2662 } 2663 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2664 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2665 2666 if (ire != NULL) { 2667 ire_cacheable = B_TRUE; 2668 ire_uinfo = (ire_mp != NULL) ? 2669 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2670 &ire->ire_uinfo; 2671 } else { 2672 if (ire_mp == NULL) { 2673 ire = ire_ftable_lookup_v6( 2674 &tcp->tcp_connp->conn_remv6, 2675 0, 0, 0, dst_ipif, &sire, zoneid, 2676 0, tsl, match_flags, ipst); 2677 if (ire == NULL) { 2678 if (dst_ill != NULL) 2679 ill_refrele(dst_ill); 2680 return (0); 2681 } 2682 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2683 &ire->ire_uinfo; 2684 } else { 2685 ire = (ire_t *)ire_mp->b_rptr; 2686 ire_uinfo = 2687 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2688 } 2689 } 2690 if (dst_ill != NULL) 2691 ill_refrele(dst_ill); 2692 2693 ASSERT(ire != NULL); 2694 ASSERT(ire_uinfo != NULL); 2695 2696 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2697 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2698 /* 2699 * ire->ire_mp is non null when ire_mp passed in is used 2700 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2701 */ 2702 if (ire->ire_mp == NULL) 2703 ire_refrele(ire); 2704 if (sire != NULL) 2705 ire_refrele(sire); 2706 return (0); 2707 } 2708 2709 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2710 in6_addr_t src_addr; 2711 2712 /* 2713 * ip_bind_connected_v6() has stored the correct source 2714 * address per IPv6 addr. selection policy in 2715 * conn_src_v6. 2716 */ 2717 src_addr = tcp->tcp_connp->conn_srcv6; 2718 2719 tcp->tcp_ip6h->ip6_src = src_addr; 2720 /* 2721 * Copy of the src addr. in tcp_t is needed 2722 * for the lookup funcs. 2723 */ 2724 tcp->tcp_ip_src_v6 = src_addr; 2725 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2726 &connp->conn_srcv6)); 2727 } 2728 tcp->tcp_localnet = 2729 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2730 } 2731 2732 /* 2733 * This allows applications to fail quickly when connections are made 2734 * to dead hosts. Hosts can be labeled dead by adding a reject route 2735 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2736 */ 2737 if ((ire->ire_flags & RTF_REJECT) && 2738 (ire->ire_flags & RTF_PRIVATE)) 2739 goto error; 2740 2741 /* 2742 * Make use of the cached rtt and rtt_sd values to calculate the 2743 * initial RTO. Note that they are already initialized in 2744 * tcp_init_values(). 2745 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2746 * IP_NEXTHOP, but instead are using the interface ire for the 2747 * nexthop, then we do not use the ire_uinfo from that ire to 2748 * do any initializations. 2749 */ 2750 if (ire_uinfo != NULL) { 2751 if (ire_uinfo->iulp_rtt != 0) { 2752 clock_t rto; 2753 2754 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2755 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2756 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2757 tcps->tcps_rexmit_interval_extra + 2758 (tcp->tcp_rtt_sa >> 5); 2759 2760 if (rto > tcps->tcps_rexmit_interval_max) { 2761 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2762 } else if (rto < tcps->tcps_rexmit_interval_min) { 2763 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2764 } else { 2765 tcp->tcp_rto = rto; 2766 } 2767 } 2768 if (ire_uinfo->iulp_ssthresh != 0) 2769 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2770 else 2771 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2772 if (ire_uinfo->iulp_spipe > 0) { 2773 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2774 tcps->tcps_max_buf); 2775 if (tcps->tcps_snd_lowat_fraction != 0) 2776 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2777 tcps->tcps_snd_lowat_fraction; 2778 (void) tcp_maxpsz_set(tcp, B_TRUE); 2779 } 2780 /* 2781 * Note that up till now, acceptor always inherits receive 2782 * window from the listener. But if there is a metrics 2783 * associated with a host, we should use that instead of 2784 * inheriting it from listener. Thus we need to pass this 2785 * info back to the caller. 2786 */ 2787 if (ire_uinfo->iulp_rpipe > 0) { 2788 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2789 tcps->tcps_max_buf); 2790 } 2791 2792 if (ire_uinfo->iulp_rtomax > 0) { 2793 tcp->tcp_second_timer_threshold = 2794 ire_uinfo->iulp_rtomax; 2795 } 2796 2797 /* 2798 * Use the metric option settings, iulp_tstamp_ok and 2799 * iulp_wscale_ok, only for active open. What this means 2800 * is that if the other side uses timestamp or window 2801 * scale option, TCP will also use those options. That 2802 * is for passive open. If the application sets a 2803 * large window, window scale is enabled regardless of 2804 * the value in iulp_wscale_ok. This is the behavior 2805 * since 2.6. So we keep it. 2806 * The only case left in passive open processing is the 2807 * check for SACK. 2808 * For ECN, it should probably be like SACK. But the 2809 * current value is binary, so we treat it like the other 2810 * cases. The metric only controls active open.For passive 2811 * open, the ndd param, tcp_ecn_permitted, controls the 2812 * behavior. 2813 */ 2814 if (!tcp_detached) { 2815 /* 2816 * The if check means that the following can only 2817 * be turned on by the metrics only IRE, but not off. 2818 */ 2819 if (ire_uinfo->iulp_tstamp_ok) 2820 tcp->tcp_snd_ts_ok = B_TRUE; 2821 if (ire_uinfo->iulp_wscale_ok) 2822 tcp->tcp_snd_ws_ok = B_TRUE; 2823 if (ire_uinfo->iulp_sack == 2) 2824 tcp->tcp_snd_sack_ok = B_TRUE; 2825 if (ire_uinfo->iulp_ecn_ok) 2826 tcp->tcp_ecn_ok = B_TRUE; 2827 } else { 2828 /* 2829 * Passive open. 2830 * 2831 * As above, the if check means that SACK can only be 2832 * turned on by the metric only IRE. 2833 */ 2834 if (ire_uinfo->iulp_sack > 0) { 2835 tcp->tcp_snd_sack_ok = B_TRUE; 2836 } 2837 } 2838 } 2839 2840 2841 /* 2842 * XXX: Note that currently, ire_max_frag can be as small as 68 2843 * because of PMTUd. So tcp_mss may go to negative if combined 2844 * length of all those options exceeds 28 bytes. But because 2845 * of the tcp_mss_min check below, we may not have a problem if 2846 * tcp_mss_min is of a reasonable value. The default is 1 so 2847 * the negative problem still exists. And the check defeats PMTUd. 2848 * In fact, if PMTUd finds that the MSS should be smaller than 2849 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2850 * value. 2851 * 2852 * We do not deal with that now. All those problems related to 2853 * PMTUd will be fixed later. 2854 */ 2855 ASSERT(ire->ire_max_frag != 0); 2856 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2857 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2858 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2859 mss = MIN(mss, IPV6_MIN_MTU); 2860 } 2861 } 2862 2863 /* Sanity check for MSS value. */ 2864 if (tcp->tcp_ipversion == IPV4_VERSION) 2865 mss_max = tcps->tcps_mss_max_ipv4; 2866 else 2867 mss_max = tcps->tcps_mss_max_ipv6; 2868 2869 if (tcp->tcp_ipversion == IPV6_VERSION && 2870 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2871 /* 2872 * After receiving an ICMPv6 "packet too big" message with a 2873 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2874 * will insert a 8-byte fragment header in every packet; we 2875 * reduce the MSS by that amount here. 2876 */ 2877 mss -= sizeof (ip6_frag_t); 2878 } 2879 2880 if (tcp->tcp_ipsec_overhead == 0) 2881 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2882 2883 mss -= tcp->tcp_ipsec_overhead; 2884 2885 if (mss < tcps->tcps_mss_min) 2886 mss = tcps->tcps_mss_min; 2887 if (mss > mss_max) 2888 mss = mss_max; 2889 2890 /* Note that this is the maximum MSS, excluding all options. */ 2891 tcp->tcp_mss = mss; 2892 2893 /* 2894 * Initialize the ISS here now that we have the full connection ID. 2895 * The RFC 1948 method of initial sequence number generation requires 2896 * knowledge of the full connection ID before setting the ISS. 2897 */ 2898 2899 tcp_iss_init(tcp); 2900 2901 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2902 tcp->tcp_loopback = B_TRUE; 2903 2904 if (tcp->tcp_ipversion == IPV4_VERSION) { 2905 hsp = tcp_hsp_lookup(tcp->tcp_remote, tcps); 2906 } else { 2907 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6, tcps); 2908 } 2909 2910 if (hsp != NULL) { 2911 /* Only modify if we're going to make them bigger */ 2912 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2913 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2914 if (tcps->tcps_snd_lowat_fraction != 0) 2915 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2916 tcps->tcps_snd_lowat_fraction; 2917 } 2918 2919 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2920 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2921 } 2922 2923 /* Copy timestamp flag only for active open */ 2924 if (!tcp_detached) 2925 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2926 } 2927 2928 if (sire != NULL) 2929 IRE_REFRELE(sire); 2930 2931 /* 2932 * If we got an IRE_CACHE and an ILL, go through their properties; 2933 * otherwise, this is deferred until later when we have an IRE_CACHE. 2934 */ 2935 if (tcp->tcp_loopback || 2936 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2937 /* 2938 * For incoming, see if this tcp may be MDT-capable. For 2939 * outgoing, this process has been taken care of through 2940 * tcp_rput_other. 2941 */ 2942 tcp_ire_ill_check(tcp, ire, ill, incoming); 2943 tcp->tcp_ire_ill_check_done = B_TRUE; 2944 } 2945 2946 mutex_enter(&connp->conn_lock); 2947 /* 2948 * Make sure that conn is not marked incipient 2949 * for incoming connections. A blind 2950 * removal of incipient flag is cheaper than 2951 * check and removal. 2952 */ 2953 connp->conn_state_flags &= ~CONN_INCIPIENT; 2954 2955 /* 2956 * Must not cache forwarding table routes 2957 * or recache an IRE after the conn_t has 2958 * had conn_ire_cache cleared and is flagged 2959 * unusable, (see the CONN_CACHE_IRE() macro). 2960 */ 2961 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2962 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2963 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2964 connp->conn_ire_cache = ire; 2965 IRE_UNTRACE_REF(ire); 2966 rw_exit(&ire->ire_bucket->irb_lock); 2967 mutex_exit(&connp->conn_lock); 2968 return (1); 2969 } 2970 rw_exit(&ire->ire_bucket->irb_lock); 2971 } 2972 mutex_exit(&connp->conn_lock); 2973 2974 if (ire->ire_mp == NULL) 2975 ire_refrele(ire); 2976 return (1); 2977 2978 error: 2979 if (ire->ire_mp == NULL) 2980 ire_refrele(ire); 2981 if (sire != NULL) 2982 ire_refrele(sire); 2983 return (0); 2984 } 2985 2986 /* 2987 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 2988 * O_T_BIND_REQ/T_BIND_REQ message. 2989 */ 2990 static void 2991 tcp_bind(tcp_t *tcp, mblk_t *mp) 2992 { 2993 sin_t *sin; 2994 sin6_t *sin6; 2995 mblk_t *mp1; 2996 in_port_t requested_port; 2997 in_port_t allocated_port; 2998 struct T_bind_req *tbr; 2999 boolean_t bind_to_req_port_only; 3000 boolean_t backlog_update = B_FALSE; 3001 boolean_t user_specified; 3002 in6_addr_t v6addr; 3003 ipaddr_t v4addr; 3004 uint_t origipversion; 3005 int err; 3006 queue_t *q = tcp->tcp_wq; 3007 conn_t *connp; 3008 mlp_type_t addrtype, mlptype; 3009 zone_t *zone; 3010 cred_t *cr; 3011 in_port_t mlp_port; 3012 tcp_stack_t *tcps = tcp->tcp_tcps; 3013 3014 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3015 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3016 if (tcp->tcp_debug) { 3017 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3018 "tcp_bind: bad req, len %u", 3019 (uint_t)(mp->b_wptr - mp->b_rptr)); 3020 } 3021 tcp_err_ack(tcp, mp, TPROTO, 0); 3022 return; 3023 } 3024 /* Make sure the largest address fits */ 3025 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3026 if (mp1 == NULL) { 3027 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3028 return; 3029 } 3030 mp = mp1; 3031 tbr = (struct T_bind_req *)mp->b_rptr; 3032 if (tcp->tcp_state >= TCPS_BOUND) { 3033 if ((tcp->tcp_state == TCPS_BOUND || 3034 tcp->tcp_state == TCPS_LISTEN) && 3035 tcp->tcp_conn_req_max != tbr->CONIND_number && 3036 tbr->CONIND_number > 0) { 3037 /* 3038 * Handle listen() increasing CONIND_number. 3039 * This is more "liberal" then what the TPI spec 3040 * requires but is needed to avoid a t_unbind 3041 * when handling listen() since the port number 3042 * might be "stolen" between the unbind and bind. 3043 */ 3044 backlog_update = B_TRUE; 3045 goto do_bind; 3046 } 3047 if (tcp->tcp_debug) { 3048 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3049 "tcp_bind: bad state, %d", tcp->tcp_state); 3050 } 3051 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3052 return; 3053 } 3054 origipversion = tcp->tcp_ipversion; 3055 3056 switch (tbr->ADDR_length) { 3057 case 0: /* request for a generic port */ 3058 tbr->ADDR_offset = sizeof (struct T_bind_req); 3059 if (tcp->tcp_family == AF_INET) { 3060 tbr->ADDR_length = sizeof (sin_t); 3061 sin = (sin_t *)&tbr[1]; 3062 *sin = sin_null; 3063 sin->sin_family = AF_INET; 3064 mp->b_wptr = (uchar_t *)&sin[1]; 3065 tcp->tcp_ipversion = IPV4_VERSION; 3066 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3067 } else { 3068 ASSERT(tcp->tcp_family == AF_INET6); 3069 tbr->ADDR_length = sizeof (sin6_t); 3070 sin6 = (sin6_t *)&tbr[1]; 3071 *sin6 = sin6_null; 3072 sin6->sin6_family = AF_INET6; 3073 mp->b_wptr = (uchar_t *)&sin6[1]; 3074 tcp->tcp_ipversion = IPV6_VERSION; 3075 V6_SET_ZERO(v6addr); 3076 } 3077 requested_port = 0; 3078 break; 3079 3080 case sizeof (sin_t): /* Complete IPv4 address */ 3081 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3082 sizeof (sin_t)); 3083 if (sin == NULL || !OK_32PTR((char *)sin)) { 3084 if (tcp->tcp_debug) { 3085 (void) strlog(TCP_MOD_ID, 0, 1, 3086 SL_ERROR|SL_TRACE, 3087 "tcp_bind: bad address parameter, " 3088 "offset %d, len %d", 3089 tbr->ADDR_offset, tbr->ADDR_length); 3090 } 3091 tcp_err_ack(tcp, mp, TPROTO, 0); 3092 return; 3093 } 3094 /* 3095 * With sockets sockfs will accept bogus sin_family in 3096 * bind() and replace it with the family used in the socket 3097 * call. 3098 */ 3099 if (sin->sin_family != AF_INET || 3100 tcp->tcp_family != AF_INET) { 3101 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3102 return; 3103 } 3104 requested_port = ntohs(sin->sin_port); 3105 tcp->tcp_ipversion = IPV4_VERSION; 3106 v4addr = sin->sin_addr.s_addr; 3107 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3108 break; 3109 3110 case sizeof (sin6_t): /* Complete IPv6 address */ 3111 sin6 = (sin6_t *)mi_offset_param(mp, 3112 tbr->ADDR_offset, sizeof (sin6_t)); 3113 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3114 if (tcp->tcp_debug) { 3115 (void) strlog(TCP_MOD_ID, 0, 1, 3116 SL_ERROR|SL_TRACE, 3117 "tcp_bind: bad IPv6 address parameter, " 3118 "offset %d, len %d", tbr->ADDR_offset, 3119 tbr->ADDR_length); 3120 } 3121 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3122 return; 3123 } 3124 if (sin6->sin6_family != AF_INET6 || 3125 tcp->tcp_family != AF_INET6) { 3126 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3127 return; 3128 } 3129 requested_port = ntohs(sin6->sin6_port); 3130 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3131 IPV4_VERSION : IPV6_VERSION; 3132 v6addr = sin6->sin6_addr; 3133 break; 3134 3135 default: 3136 if (tcp->tcp_debug) { 3137 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3138 "tcp_bind: bad address length, %d", 3139 tbr->ADDR_length); 3140 } 3141 tcp_err_ack(tcp, mp, TBADADDR, 0); 3142 return; 3143 } 3144 tcp->tcp_bound_source_v6 = v6addr; 3145 3146 /* Check for change in ipversion */ 3147 if (origipversion != tcp->tcp_ipversion) { 3148 ASSERT(tcp->tcp_family == AF_INET6); 3149 err = tcp->tcp_ipversion == IPV6_VERSION ? 3150 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3151 if (err) { 3152 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3153 return; 3154 } 3155 } 3156 3157 /* 3158 * Initialize family specific fields. Copy of the src addr. 3159 * in tcp_t is needed for the lookup funcs. 3160 */ 3161 if (tcp->tcp_ipversion == IPV6_VERSION) { 3162 tcp->tcp_ip6h->ip6_src = v6addr; 3163 } else { 3164 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3165 } 3166 tcp->tcp_ip_src_v6 = v6addr; 3167 3168 /* 3169 * For O_T_BIND_REQ: 3170 * Verify that the target port/addr is available, or choose 3171 * another. 3172 * For T_BIND_REQ: 3173 * Verify that the target port/addr is available or fail. 3174 * In both cases when it succeeds the tcp is inserted in the 3175 * bind hash table. This ensures that the operation is atomic 3176 * under the lock on the hash bucket. 3177 */ 3178 bind_to_req_port_only = requested_port != 0 && 3179 tbr->PRIM_type != O_T_BIND_REQ; 3180 /* 3181 * Get a valid port (within the anonymous range and should not 3182 * be a privileged one) to use if the user has not given a port. 3183 * If multiple threads are here, they may all start with 3184 * with the same initial port. But, it should be fine as long as 3185 * tcp_bindi will ensure that no two threads will be assigned 3186 * the same port. 3187 * 3188 * NOTE: XXX If a privileged process asks for an anonymous port, we 3189 * still check for ports only in the range > tcp_smallest_non_priv_port, 3190 * unless TCP_ANONPRIVBIND option is set. 3191 */ 3192 mlptype = mlptSingle; 3193 mlp_port = requested_port; 3194 if (requested_port == 0) { 3195 requested_port = tcp->tcp_anon_priv_bind ? 3196 tcp_get_next_priv_port(tcp) : 3197 tcp_update_next_port(tcps->tcps_next_port_to_try, 3198 tcp, B_TRUE); 3199 if (requested_port == 0) { 3200 tcp_err_ack(tcp, mp, TNOADDR, 0); 3201 return; 3202 } 3203 user_specified = B_FALSE; 3204 3205 /* 3206 * If the user went through one of the RPC interfaces to create 3207 * this socket and RPC is MLP in this zone, then give him an 3208 * anonymous MLP. 3209 */ 3210 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3211 connp = tcp->tcp_connp; 3212 if (connp->conn_anon_mlp && is_system_labeled()) { 3213 zone = crgetzone(cr); 3214 addrtype = tsol_mlp_addr_type(zone->zone_id, 3215 IPV6_VERSION, &v6addr, 3216 tcps->tcps_netstack->netstack_ip); 3217 if (addrtype == mlptSingle) { 3218 tcp_err_ack(tcp, mp, TNOADDR, 0); 3219 return; 3220 } 3221 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3222 PMAPPORT, addrtype); 3223 mlp_port = PMAPPORT; 3224 } 3225 } else { 3226 int i; 3227 boolean_t priv = B_FALSE; 3228 3229 /* 3230 * If the requested_port is in the well-known privileged range, 3231 * verify that the stream was opened by a privileged user. 3232 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3233 * but instead the code relies on: 3234 * - the fact that the address of the array and its size never 3235 * changes 3236 * - the atomic assignment of the elements of the array 3237 */ 3238 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3239 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 3240 priv = B_TRUE; 3241 } else { 3242 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 3243 if (requested_port == 3244 tcps->tcps_g_epriv_ports[i]) { 3245 priv = B_TRUE; 3246 break; 3247 } 3248 } 3249 } 3250 if (priv) { 3251 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3252 if (tcp->tcp_debug) { 3253 (void) strlog(TCP_MOD_ID, 0, 1, 3254 SL_ERROR|SL_TRACE, 3255 "tcp_bind: no priv for port %d", 3256 requested_port); 3257 } 3258 tcp_err_ack(tcp, mp, TACCES, 0); 3259 return; 3260 } 3261 } 3262 user_specified = B_TRUE; 3263 3264 connp = tcp->tcp_connp; 3265 if (is_system_labeled()) { 3266 zone = crgetzone(cr); 3267 addrtype = tsol_mlp_addr_type(zone->zone_id, 3268 IPV6_VERSION, &v6addr, 3269 tcps->tcps_netstack->netstack_ip); 3270 if (addrtype == mlptSingle) { 3271 tcp_err_ack(tcp, mp, TNOADDR, 0); 3272 return; 3273 } 3274 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3275 requested_port, addrtype); 3276 } 3277 } 3278 3279 if (mlptype != mlptSingle) { 3280 if (secpolicy_net_bindmlp(cr) != 0) { 3281 if (tcp->tcp_debug) { 3282 (void) strlog(TCP_MOD_ID, 0, 1, 3283 SL_ERROR|SL_TRACE, 3284 "tcp_bind: no priv for multilevel port %d", 3285 requested_port); 3286 } 3287 tcp_err_ack(tcp, mp, TACCES, 0); 3288 return; 3289 } 3290 3291 /* 3292 * If we're specifically binding a shared IP address and the 3293 * port is MLP on shared addresses, then check to see if this 3294 * zone actually owns the MLP. Reject if not. 3295 */ 3296 if (mlptype == mlptShared && addrtype == mlptShared) { 3297 /* 3298 * No need to handle exclusive-stack zones since 3299 * ALL_ZONES only applies to the shared stack. 3300 */ 3301 zoneid_t mlpzone; 3302 3303 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3304 htons(mlp_port)); 3305 if (connp->conn_zoneid != mlpzone) { 3306 if (tcp->tcp_debug) { 3307 (void) strlog(TCP_MOD_ID, 0, 1, 3308 SL_ERROR|SL_TRACE, 3309 "tcp_bind: attempt to bind port " 3310 "%d on shared addr in zone %d " 3311 "(should be %d)", 3312 mlp_port, connp->conn_zoneid, 3313 mlpzone); 3314 } 3315 tcp_err_ack(tcp, mp, TACCES, 0); 3316 return; 3317 } 3318 } 3319 3320 if (!user_specified) { 3321 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3322 requested_port, B_TRUE); 3323 if (err != 0) { 3324 if (tcp->tcp_debug) { 3325 (void) strlog(TCP_MOD_ID, 0, 1, 3326 SL_ERROR|SL_TRACE, 3327 "tcp_bind: cannot establish anon " 3328 "MLP for port %d", 3329 requested_port); 3330 } 3331 tcp_err_ack(tcp, mp, TSYSERR, err); 3332 return; 3333 } 3334 connp->conn_anon_port = B_TRUE; 3335 } 3336 connp->conn_mlp_type = mlptype; 3337 } 3338 3339 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3340 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3341 3342 if (allocated_port == 0) { 3343 connp->conn_mlp_type = mlptSingle; 3344 if (connp->conn_anon_port) { 3345 connp->conn_anon_port = B_FALSE; 3346 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3347 requested_port, B_FALSE); 3348 } 3349 if (bind_to_req_port_only) { 3350 if (tcp->tcp_debug) { 3351 (void) strlog(TCP_MOD_ID, 0, 1, 3352 SL_ERROR|SL_TRACE, 3353 "tcp_bind: requested addr busy"); 3354 } 3355 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3356 } else { 3357 /* If we are out of ports, fail the bind. */ 3358 if (tcp->tcp_debug) { 3359 (void) strlog(TCP_MOD_ID, 0, 1, 3360 SL_ERROR|SL_TRACE, 3361 "tcp_bind: out of ports?"); 3362 } 3363 tcp_err_ack(tcp, mp, TNOADDR, 0); 3364 } 3365 return; 3366 } 3367 ASSERT(tcp->tcp_state == TCPS_BOUND); 3368 do_bind: 3369 if (!backlog_update) { 3370 if (tcp->tcp_family == AF_INET) 3371 sin->sin_port = htons(allocated_port); 3372 else 3373 sin6->sin6_port = htons(allocated_port); 3374 } 3375 if (tcp->tcp_family == AF_INET) { 3376 if (tbr->CONIND_number != 0) { 3377 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3378 sizeof (sin_t)); 3379 } else { 3380 /* Just verify the local IP address */ 3381 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3382 } 3383 } else { 3384 if (tbr->CONIND_number != 0) { 3385 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3386 sizeof (sin6_t)); 3387 } else { 3388 /* Just verify the local IP address */ 3389 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3390 IPV6_ADDR_LEN); 3391 } 3392 } 3393 if (mp1 == NULL) { 3394 if (connp->conn_anon_port) { 3395 connp->conn_anon_port = B_FALSE; 3396 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3397 requested_port, B_FALSE); 3398 } 3399 connp->conn_mlp_type = mlptSingle; 3400 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3401 return; 3402 } 3403 3404 tbr->PRIM_type = T_BIND_ACK; 3405 mp->b_datap->db_type = M_PCPROTO; 3406 3407 /* Chain in the reply mp for tcp_rput() */ 3408 mp1->b_cont = mp; 3409 mp = mp1; 3410 3411 tcp->tcp_conn_req_max = tbr->CONIND_number; 3412 if (tcp->tcp_conn_req_max) { 3413 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 3414 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 3415 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 3416 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 3417 /* 3418 * If this is a listener, do not reset the eager list 3419 * and other stuffs. Note that we don't check if the 3420 * existing eager list meets the new tcp_conn_req_max 3421 * requirement. 3422 */ 3423 if (tcp->tcp_state != TCPS_LISTEN) { 3424 tcp->tcp_state = TCPS_LISTEN; 3425 /* Initialize the chain. Don't need the eager_lock */ 3426 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3427 tcp->tcp_eager_next_drop_q0 = tcp; 3428 tcp->tcp_eager_prev_drop_q0 = tcp; 3429 tcp->tcp_second_ctimer_threshold = 3430 tcps->tcps_ip_abort_linterval; 3431 } 3432 } 3433 3434 /* 3435 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3436 * processing continues in tcp_rput_other(). 3437 */ 3438 if (tcp->tcp_family == AF_INET6) { 3439 ASSERT(tcp->tcp_connp->conn_af_isv6); 3440 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3441 } else { 3442 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3443 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3444 } 3445 /* 3446 * If the bind cannot complete immediately 3447 * IP will arrange to call tcp_rput_other 3448 * when the bind completes. 3449 */ 3450 if (mp != NULL) { 3451 tcp_rput_other(tcp, mp); 3452 } else { 3453 /* 3454 * Bind will be resumed later. Need to ensure 3455 * that conn doesn't disappear when that happens. 3456 * This will be decremented in ip_resume_tcp_bind(). 3457 */ 3458 CONN_INC_REF(tcp->tcp_connp); 3459 } 3460 } 3461 3462 3463 /* 3464 * If the "bind_to_req_port_only" parameter is set, if the requested port 3465 * number is available, return it, If not return 0 3466 * 3467 * If "bind_to_req_port_only" parameter is not set and 3468 * If the requested port number is available, return it. If not, return 3469 * the first anonymous port we happen across. If no anonymous ports are 3470 * available, return 0. addr is the requested local address, if any. 3471 * 3472 * In either case, when succeeding update the tcp_t to record the port number 3473 * and insert it in the bind hash table. 3474 * 3475 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3476 * without setting SO_REUSEADDR. This is needed so that they 3477 * can be viewed as two independent transport protocols. 3478 */ 3479 static in_port_t 3480 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3481 int reuseaddr, boolean_t quick_connect, 3482 boolean_t bind_to_req_port_only, boolean_t user_specified) 3483 { 3484 /* number of times we have run around the loop */ 3485 int count = 0; 3486 /* maximum number of times to run around the loop */ 3487 int loopmax; 3488 conn_t *connp = tcp->tcp_connp; 3489 zoneid_t zoneid = connp->conn_zoneid; 3490 tcp_stack_t *tcps = tcp->tcp_tcps; 3491 3492 /* 3493 * Lookup for free addresses is done in a loop and "loopmax" 3494 * influences how long we spin in the loop 3495 */ 3496 if (bind_to_req_port_only) { 3497 /* 3498 * If the requested port is busy, don't bother to look 3499 * for a new one. Setting loop maximum count to 1 has 3500 * that effect. 3501 */ 3502 loopmax = 1; 3503 } else { 3504 /* 3505 * If the requested port is busy, look for a free one 3506 * in the anonymous port range. 3507 * Set loopmax appropriately so that one does not look 3508 * forever in the case all of the anonymous ports are in use. 3509 */ 3510 if (tcp->tcp_anon_priv_bind) { 3511 /* 3512 * loopmax = 3513 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3514 */ 3515 loopmax = IPPORT_RESERVED - 3516 tcps->tcps_min_anonpriv_port; 3517 } else { 3518 loopmax = (tcps->tcps_largest_anon_port - 3519 tcps->tcps_smallest_anon_port + 1); 3520 } 3521 } 3522 do { 3523 uint16_t lport; 3524 tf_t *tbf; 3525 tcp_t *ltcp; 3526 conn_t *lconnp; 3527 3528 lport = htons(port); 3529 3530 /* 3531 * Ensure that the tcp_t is not currently in the bind hash. 3532 * Hold the lock on the hash bucket to ensure that 3533 * the duplicate check plus the insertion is an atomic 3534 * operation. 3535 * 3536 * This function does an inline lookup on the bind hash list 3537 * Make sure that we access only members of tcp_t 3538 * and that we don't look at tcp_tcp, since we are not 3539 * doing a CONN_INC_REF. 3540 */ 3541 tcp_bind_hash_remove(tcp); 3542 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3543 mutex_enter(&tbf->tf_lock); 3544 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3545 ltcp = ltcp->tcp_bind_hash) { 3546 boolean_t not_socket; 3547 boolean_t exclbind; 3548 3549 if (lport != ltcp->tcp_lport) 3550 continue; 3551 3552 lconnp = ltcp->tcp_connp; 3553 3554 /* 3555 * On a labeled system, we must treat bindings to ports 3556 * on shared IP addresses by sockets with MAC exemption 3557 * privilege as being in all zones, as there's 3558 * otherwise no way to identify the right receiver. 3559 */ 3560 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3561 IPCL_ZONE_MATCH(connp, 3562 ltcp->tcp_connp->conn_zoneid)) && 3563 !lconnp->conn_mac_exempt && 3564 !connp->conn_mac_exempt) 3565 continue; 3566 3567 /* 3568 * If TCP_EXCLBIND is set for either the bound or 3569 * binding endpoint, the semantics of bind 3570 * is changed according to the following. 3571 * 3572 * spec = specified address (v4 or v6) 3573 * unspec = unspecified address (v4 or v6) 3574 * A = specified addresses are different for endpoints 3575 * 3576 * bound bind to allowed 3577 * ------------------------------------- 3578 * unspec unspec no 3579 * unspec spec no 3580 * spec unspec no 3581 * spec spec yes if A 3582 * 3583 * For labeled systems, SO_MAC_EXEMPT behaves the same 3584 * as TCP_EXCLBIND, except that zoneid is ignored. 3585 * 3586 * Note: 3587 * 3588 * 1. Because of TLI semantics, an endpoint can go 3589 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3590 * TCPS_BOUND, depending on whether it is originally 3591 * a listener or not. That is why we need to check 3592 * for states greater than or equal to TCPS_BOUND 3593 * here. 3594 * 3595 * 2. Ideally, we should only check for state equals 3596 * to TCPS_LISTEN. And the following check should be 3597 * added. 3598 * 3599 * if (ltcp->tcp_state == TCPS_LISTEN || 3600 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3601 * ... 3602 * } 3603 * 3604 * The semantics will be changed to this. If the 3605 * endpoint on the list is in state not equal to 3606 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3607 * set, let the bind succeed. 3608 * 3609 * Because of (1), we cannot do that for TLI 3610 * endpoints. But we can do that for socket endpoints. 3611 * If in future, we can change this going back 3612 * semantics, we can use the above check for TLI also. 3613 */ 3614 not_socket = !(TCP_IS_SOCKET(ltcp) && 3615 TCP_IS_SOCKET(tcp)); 3616 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3617 3618 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3619 (exclbind && (not_socket || 3620 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3621 if (V6_OR_V4_INADDR_ANY( 3622 ltcp->tcp_bound_source_v6) || 3623 V6_OR_V4_INADDR_ANY(*laddr) || 3624 IN6_ARE_ADDR_EQUAL(laddr, 3625 <cp->tcp_bound_source_v6)) { 3626 break; 3627 } 3628 continue; 3629 } 3630 3631 /* 3632 * Check ipversion to allow IPv4 and IPv6 sockets to 3633 * have disjoint port number spaces, if *_EXCLBIND 3634 * is not set and only if the application binds to a 3635 * specific port. We use the same autoassigned port 3636 * number space for IPv4 and IPv6 sockets. 3637 */ 3638 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3639 bind_to_req_port_only) 3640 continue; 3641 3642 /* 3643 * Ideally, we should make sure that the source 3644 * address, remote address, and remote port in the 3645 * four tuple for this tcp-connection is unique. 3646 * However, trying to find out the local source 3647 * address would require too much code duplication 3648 * with IP, since IP needs needs to have that code 3649 * to support userland TCP implementations. 3650 */ 3651 if (quick_connect && 3652 (ltcp->tcp_state > TCPS_LISTEN) && 3653 ((tcp->tcp_fport != ltcp->tcp_fport) || 3654 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3655 <cp->tcp_remote_v6))) 3656 continue; 3657 3658 if (!reuseaddr) { 3659 /* 3660 * No socket option SO_REUSEADDR. 3661 * If existing port is bound to 3662 * a non-wildcard IP address 3663 * and the requesting stream is 3664 * bound to a distinct 3665 * different IP addresses 3666 * (non-wildcard, also), keep 3667 * going. 3668 */ 3669 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3670 !V6_OR_V4_INADDR_ANY( 3671 ltcp->tcp_bound_source_v6) && 3672 !IN6_ARE_ADDR_EQUAL(laddr, 3673 <cp->tcp_bound_source_v6)) 3674 continue; 3675 if (ltcp->tcp_state >= TCPS_BOUND) { 3676 /* 3677 * This port is being used and 3678 * its state is >= TCPS_BOUND, 3679 * so we can't bind to it. 3680 */ 3681 break; 3682 } 3683 } else { 3684 /* 3685 * socket option SO_REUSEADDR is set on the 3686 * binding tcp_t. 3687 * 3688 * If two streams are bound to 3689 * same IP address or both addr 3690 * and bound source are wildcards 3691 * (INADDR_ANY), we want to stop 3692 * searching. 3693 * We have found a match of IP source 3694 * address and source port, which is 3695 * refused regardless of the 3696 * SO_REUSEADDR setting, so we break. 3697 */ 3698 if (IN6_ARE_ADDR_EQUAL(laddr, 3699 <cp->tcp_bound_source_v6) && 3700 (ltcp->tcp_state == TCPS_LISTEN || 3701 ltcp->tcp_state == TCPS_BOUND)) 3702 break; 3703 } 3704 } 3705 if (ltcp != NULL) { 3706 /* The port number is busy */ 3707 mutex_exit(&tbf->tf_lock); 3708 } else { 3709 /* 3710 * This port is ours. Insert in fanout and mark as 3711 * bound to prevent others from getting the port 3712 * number. 3713 */ 3714 tcp->tcp_state = TCPS_BOUND; 3715 tcp->tcp_lport = htons(port); 3716 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3717 3718 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3719 tcp->tcp_lport)] == tbf); 3720 tcp_bind_hash_insert(tbf, tcp, 1); 3721 3722 mutex_exit(&tbf->tf_lock); 3723 3724 /* 3725 * We don't want tcp_next_port_to_try to "inherit" 3726 * a port number supplied by the user in a bind. 3727 */ 3728 if (user_specified) 3729 return (port); 3730 3731 /* 3732 * This is the only place where tcp_next_port_to_try 3733 * is updated. After the update, it may or may not 3734 * be in the valid range. 3735 */ 3736 if (!tcp->tcp_anon_priv_bind) 3737 tcps->tcps_next_port_to_try = port + 1; 3738 return (port); 3739 } 3740 3741 if (tcp->tcp_anon_priv_bind) { 3742 port = tcp_get_next_priv_port(tcp); 3743 } else { 3744 if (count == 0 && user_specified) { 3745 /* 3746 * We may have to return an anonymous port. So 3747 * get one to start with. 3748 */ 3749 port = 3750 tcp_update_next_port( 3751 tcps->tcps_next_port_to_try, 3752 tcp, B_TRUE); 3753 user_specified = B_FALSE; 3754 } else { 3755 port = tcp_update_next_port(port + 1, tcp, 3756 B_FALSE); 3757 } 3758 } 3759 if (port == 0) 3760 break; 3761 3762 /* 3763 * Don't let this loop run forever in the case where 3764 * all of the anonymous ports are in use. 3765 */ 3766 } while (++count < loopmax); 3767 return (0); 3768 } 3769 3770 /* 3771 * tcp_clean_death / tcp_close_detached must not be called more than once 3772 * on a tcp. Thus every function that potentially calls tcp_clean_death 3773 * must check for the tcp state before calling tcp_clean_death. 3774 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3775 * tcp_timer_handler, all check for the tcp state. 3776 */ 3777 /* ARGSUSED */ 3778 void 3779 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3780 { 3781 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3782 3783 freemsg(mp); 3784 if (tcp->tcp_state > TCPS_BOUND) 3785 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3786 ETIMEDOUT, 5); 3787 } 3788 3789 /* 3790 * We are dying for some reason. Try to do it gracefully. (May be called 3791 * as writer.) 3792 * 3793 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3794 * done by a service procedure). 3795 * TBD - Should the return value distinguish between the tcp_t being 3796 * freed and it being reinitialized? 3797 */ 3798 static int 3799 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3800 { 3801 mblk_t *mp; 3802 queue_t *q; 3803 tcp_stack_t *tcps = tcp->tcp_tcps; 3804 3805 TCP_CLD_STAT(tag); 3806 3807 #if TCP_TAG_CLEAN_DEATH 3808 tcp->tcp_cleandeathtag = tag; 3809 #endif 3810 3811 if (tcp->tcp_fused) 3812 tcp_unfuse(tcp); 3813 3814 if (tcp->tcp_linger_tid != 0 && 3815 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3816 tcp_stop_lingering(tcp); 3817 } 3818 3819 ASSERT(tcp != NULL); 3820 ASSERT((tcp->tcp_family == AF_INET && 3821 tcp->tcp_ipversion == IPV4_VERSION) || 3822 (tcp->tcp_family == AF_INET6 && 3823 (tcp->tcp_ipversion == IPV4_VERSION || 3824 tcp->tcp_ipversion == IPV6_VERSION))); 3825 3826 if (TCP_IS_DETACHED(tcp)) { 3827 if (tcp->tcp_hard_binding) { 3828 /* 3829 * Its an eager that we are dealing with. We close the 3830 * eager but in case a conn_ind has already gone to the 3831 * listener, let tcp_accept_finish() send a discon_ind 3832 * to the listener and drop the last reference. If the 3833 * listener doesn't even know about the eager i.e. the 3834 * conn_ind hasn't gone up, blow away the eager and drop 3835 * the last reference as well. If the conn_ind has gone 3836 * up, state should be BOUND. tcp_accept_finish 3837 * will figure out that the connection has received a 3838 * RST and will send a DISCON_IND to the application. 3839 */ 3840 tcp_closei_local(tcp); 3841 if (!tcp->tcp_tconnind_started) { 3842 CONN_DEC_REF(tcp->tcp_connp); 3843 } else { 3844 tcp->tcp_state = TCPS_BOUND; 3845 } 3846 } else { 3847 tcp_close_detached(tcp); 3848 } 3849 return (0); 3850 } 3851 3852 TCP_STAT(tcps, tcp_clean_death_nondetached); 3853 3854 /* 3855 * If T_ORDREL_IND has not been sent yet (done when service routine 3856 * is run) postpone cleaning up the endpoint until service routine 3857 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3858 * client_errno since tcp_close uses the client_errno field. 3859 */ 3860 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3861 if (err != 0) 3862 tcp->tcp_client_errno = err; 3863 3864 tcp->tcp_deferred_clean_death = B_TRUE; 3865 return (-1); 3866 } 3867 3868 q = tcp->tcp_rq; 3869 3870 /* Trash all inbound data */ 3871 flushq(q, FLUSHALL); 3872 3873 /* 3874 * If we are at least part way open and there is error 3875 * (err==0 implies no error) 3876 * notify our client by a T_DISCON_IND. 3877 */ 3878 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3879 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3880 !TCP_IS_SOCKET(tcp)) { 3881 /* 3882 * Send M_FLUSH according to TPI. Because sockets will 3883 * (and must) ignore FLUSHR we do that only for TPI 3884 * endpoints and sockets in STREAMS mode. 3885 */ 3886 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3887 } 3888 if (tcp->tcp_debug) { 3889 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3890 "tcp_clean_death: discon err %d", err); 3891 } 3892 mp = mi_tpi_discon_ind(NULL, err, 0); 3893 if (mp != NULL) { 3894 putnext(q, mp); 3895 } else { 3896 if (tcp->tcp_debug) { 3897 (void) strlog(TCP_MOD_ID, 0, 1, 3898 SL_ERROR|SL_TRACE, 3899 "tcp_clean_death, sending M_ERROR"); 3900 } 3901 (void) putnextctl1(q, M_ERROR, EPROTO); 3902 } 3903 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3904 /* SYN_SENT or SYN_RCVD */ 3905 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3906 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3907 /* ESTABLISHED or CLOSE_WAIT */ 3908 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3909 } 3910 } 3911 3912 tcp_reinit(tcp); 3913 return (-1); 3914 } 3915 3916 /* 3917 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3918 * to expire, stop the wait and finish the close. 3919 */ 3920 static void 3921 tcp_stop_lingering(tcp_t *tcp) 3922 { 3923 clock_t delta = 0; 3924 tcp_stack_t *tcps = tcp->tcp_tcps; 3925 3926 tcp->tcp_linger_tid = 0; 3927 if (tcp->tcp_state > TCPS_LISTEN) { 3928 tcp_acceptor_hash_remove(tcp); 3929 mutex_enter(&tcp->tcp_non_sq_lock); 3930 if (tcp->tcp_flow_stopped) { 3931 tcp_clrqfull(tcp); 3932 } 3933 mutex_exit(&tcp->tcp_non_sq_lock); 3934 3935 if (tcp->tcp_timer_tid != 0) { 3936 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3937 tcp->tcp_timer_tid = 0; 3938 } 3939 /* 3940 * Need to cancel those timers which will not be used when 3941 * TCP is detached. This has to be done before the tcp_wq 3942 * is set to the global queue. 3943 */ 3944 tcp_timers_stop(tcp); 3945 3946 3947 tcp->tcp_detached = B_TRUE; 3948 ASSERT(tcps->tcps_g_q != NULL); 3949 tcp->tcp_rq = tcps->tcps_g_q; 3950 tcp->tcp_wq = WR(tcps->tcps_g_q); 3951 3952 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3953 tcp_time_wait_append(tcp); 3954 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3955 goto finish; 3956 } 3957 3958 /* 3959 * If delta is zero the timer event wasn't executed and was 3960 * successfully canceled. In this case we need to restart it 3961 * with the minimal delta possible. 3962 */ 3963 if (delta >= 0) { 3964 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3965 delta ? delta : 1); 3966 } 3967 } else { 3968 tcp_closei_local(tcp); 3969 CONN_DEC_REF(tcp->tcp_connp); 3970 } 3971 finish: 3972 /* Signal closing thread that it can complete close */ 3973 mutex_enter(&tcp->tcp_closelock); 3974 tcp->tcp_detached = B_TRUE; 3975 ASSERT(tcps->tcps_g_q != NULL); 3976 tcp->tcp_rq = tcps->tcps_g_q; 3977 tcp->tcp_wq = WR(tcps->tcps_g_q); 3978 tcp->tcp_closed = 1; 3979 cv_signal(&tcp->tcp_closecv); 3980 mutex_exit(&tcp->tcp_closelock); 3981 } 3982 3983 /* 3984 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3985 * expires. 3986 */ 3987 static void 3988 tcp_close_linger_timeout(void *arg) 3989 { 3990 conn_t *connp = (conn_t *)arg; 3991 tcp_t *tcp = connp->conn_tcp; 3992 3993 tcp->tcp_client_errno = ETIMEDOUT; 3994 tcp_stop_lingering(tcp); 3995 } 3996 3997 static int 3998 tcp_close(queue_t *q, int flags) 3999 { 4000 conn_t *connp = Q_TO_CONN(q); 4001 tcp_t *tcp = connp->conn_tcp; 4002 mblk_t *mp = &tcp->tcp_closemp; 4003 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 4004 boolean_t linger_interrupted = B_FALSE; 4005 mblk_t *bp; 4006 4007 ASSERT(WR(q)->q_next == NULL); 4008 ASSERT(connp->conn_ref >= 2); 4009 ASSERT((connp->conn_flags & IPCL_TCPMOD) == 0); 4010 4011 /* 4012 * We are being closed as /dev/tcp or /dev/tcp6. 4013 * 4014 * Mark the conn as closing. ill_pending_mp_add will not 4015 * add any mp to the pending mp list, after this conn has 4016 * started closing. Same for sq_pending_mp_add 4017 */ 4018 mutex_enter(&connp->conn_lock); 4019 connp->conn_state_flags |= CONN_CLOSING; 4020 if (connp->conn_oper_pending_ill != NULL) 4021 conn_ioctl_cleanup_reqd = B_TRUE; 4022 CONN_INC_REF_LOCKED(connp); 4023 mutex_exit(&connp->conn_lock); 4024 tcp->tcp_closeflags = (uint8_t)flags; 4025 ASSERT(connp->conn_ref >= 3); 4026 4027 /* 4028 * tcp_closemp_used is used below without any protection of a lock 4029 * as we don't expect any one else to use it concurrently at this 4030 * point otherwise it would be a major defect. 4031 */ 4032 4033 if (mp->b_prev == NULL) 4034 tcp->tcp_closemp_used = B_TRUE; 4035 else 4036 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 4037 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 4038 4039 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4040 4041 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4042 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4043 4044 mutex_enter(&tcp->tcp_closelock); 4045 while (!tcp->tcp_closed) { 4046 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4047 /* 4048 * We got interrupted. Check if we are lingering, 4049 * if yes, post a message to stop and wait until 4050 * tcp_closed is set. If we aren't lingering, 4051 * just go back around. 4052 */ 4053 if (tcp->tcp_linger && 4054 tcp->tcp_lingertime > 0 && 4055 !linger_interrupted) { 4056 mutex_exit(&tcp->tcp_closelock); 4057 /* Entering squeue, bump ref count. */ 4058 CONN_INC_REF(connp); 4059 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4060 squeue_enter(connp->conn_sqp, bp, 4061 tcp_linger_interrupted, connp, 4062 SQTAG_IP_TCP_CLOSE); 4063 linger_interrupted = B_TRUE; 4064 mutex_enter(&tcp->tcp_closelock); 4065 } 4066 } 4067 } 4068 mutex_exit(&tcp->tcp_closelock); 4069 4070 /* 4071 * In the case of listener streams that have eagers in the q or q0 4072 * we wait for the eagers to drop their reference to us. tcp_rq and 4073 * tcp_wq of the eagers point to our queues. By waiting for the 4074 * refcnt to drop to 1, we are sure that the eagers have cleaned 4075 * up their queue pointers and also dropped their references to us. 4076 */ 4077 if (tcp->tcp_wait_for_eagers) { 4078 mutex_enter(&connp->conn_lock); 4079 while (connp->conn_ref != 1) { 4080 cv_wait(&connp->conn_cv, &connp->conn_lock); 4081 } 4082 mutex_exit(&connp->conn_lock); 4083 } 4084 /* 4085 * ioctl cleanup. The mp is queued in the 4086 * ill_pending_mp or in the sq_pending_mp. 4087 */ 4088 if (conn_ioctl_cleanup_reqd) 4089 conn_ioctl_cleanup(connp); 4090 4091 qprocsoff(q); 4092 inet_minor_free(ip_minor_arena, connp->conn_dev); 4093 4094 tcp->tcp_cpid = -1; 4095 4096 /* 4097 * Drop IP's reference on the conn. This is the last reference 4098 * on the connp if the state was less than established. If the 4099 * connection has gone into timewait state, then we will have 4100 * one ref for the TCP and one more ref (total of two) for the 4101 * classifier connected hash list (a timewait connections stays 4102 * in connected hash till closed). 4103 * 4104 * We can't assert the references because there might be other 4105 * transient reference places because of some walkers or queued 4106 * packets in squeue for the timewait state. 4107 */ 4108 CONN_DEC_REF(connp); 4109 q->q_ptr = WR(q)->q_ptr = NULL; 4110 return (0); 4111 } 4112 4113 static int 4114 tcpclose_accept(queue_t *q) 4115 { 4116 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4117 4118 /* 4119 * We had opened an acceptor STREAM for sockfs which is 4120 * now being closed due to some error. 4121 */ 4122 qprocsoff(q); 4123 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 4124 q->q_ptr = WR(q)->q_ptr = NULL; 4125 return (0); 4126 } 4127 4128 /* 4129 * Called by tcp_close() routine via squeue when lingering is 4130 * interrupted by a signal. 4131 */ 4132 4133 /* ARGSUSED */ 4134 static void 4135 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4136 { 4137 conn_t *connp = (conn_t *)arg; 4138 tcp_t *tcp = connp->conn_tcp; 4139 4140 freeb(mp); 4141 if (tcp->tcp_linger_tid != 0 && 4142 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4143 tcp_stop_lingering(tcp); 4144 tcp->tcp_client_errno = EINTR; 4145 } 4146 } 4147 4148 /* 4149 * Called by streams close routine via squeues when our client blows off her 4150 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4151 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4152 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4153 * acked. 4154 * 4155 * NOTE: tcp_close potentially returns error when lingering. 4156 * However, the stream head currently does not pass these errors 4157 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4158 * errors to the application (from tsleep()) and not errors 4159 * like ECONNRESET caused by receiving a reset packet. 4160 */ 4161 4162 /* ARGSUSED */ 4163 static void 4164 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4165 { 4166 char *msg; 4167 conn_t *connp = (conn_t *)arg; 4168 tcp_t *tcp = connp->conn_tcp; 4169 clock_t delta = 0; 4170 tcp_stack_t *tcps = tcp->tcp_tcps; 4171 4172 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4173 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4174 4175 /* Cancel any pending timeout */ 4176 if (tcp->tcp_ordrelid != 0) { 4177 if (tcp->tcp_timeout) { 4178 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4179 } 4180 tcp->tcp_ordrelid = 0; 4181 tcp->tcp_timeout = B_FALSE; 4182 } 4183 4184 mutex_enter(&tcp->tcp_eager_lock); 4185 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4186 /* Cleanup for listener */ 4187 tcp_eager_cleanup(tcp, 0); 4188 tcp->tcp_wait_for_eagers = 1; 4189 } 4190 mutex_exit(&tcp->tcp_eager_lock); 4191 4192 connp->conn_mdt_ok = B_FALSE; 4193 tcp->tcp_mdt = B_FALSE; 4194 4195 connp->conn_lso_ok = B_FALSE; 4196 tcp->tcp_lso = B_FALSE; 4197 4198 msg = NULL; 4199 switch (tcp->tcp_state) { 4200 case TCPS_CLOSED: 4201 case TCPS_IDLE: 4202 case TCPS_BOUND: 4203 case TCPS_LISTEN: 4204 break; 4205 case TCPS_SYN_SENT: 4206 msg = "tcp_close, during connect"; 4207 break; 4208 case TCPS_SYN_RCVD: 4209 /* 4210 * Close during the connect 3-way handshake 4211 * but here there may or may not be pending data 4212 * already on queue. Process almost same as in 4213 * the ESTABLISHED state. 4214 */ 4215 /* FALLTHRU */ 4216 default: 4217 if (tcp->tcp_fused) 4218 tcp_unfuse(tcp); 4219 4220 /* 4221 * If SO_LINGER has set a zero linger time, abort the 4222 * connection with a reset. 4223 */ 4224 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4225 msg = "tcp_close, zero lingertime"; 4226 break; 4227 } 4228 4229 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4230 /* 4231 * Abort connection if there is unread data queued. 4232 */ 4233 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4234 msg = "tcp_close, unread data"; 4235 break; 4236 } 4237 /* 4238 * tcp_hard_bound is now cleared thus all packets go through 4239 * tcp_lookup. This fact is used by tcp_detach below. 4240 * 4241 * We have done a qwait() above which could have possibly 4242 * drained more messages in turn causing transition to a 4243 * different state. Check whether we have to do the rest 4244 * of the processing or not. 4245 */ 4246 if (tcp->tcp_state <= TCPS_LISTEN) 4247 break; 4248 4249 /* 4250 * Transmit the FIN before detaching the tcp_t. 4251 * After tcp_detach returns this queue/perimeter 4252 * no longer owns the tcp_t thus others can modify it. 4253 */ 4254 (void) tcp_xmit_end(tcp); 4255 4256 /* 4257 * If lingering on close then wait until the fin is acked, 4258 * the SO_LINGER time passes, or a reset is sent/received. 4259 */ 4260 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4261 !(tcp->tcp_fin_acked) && 4262 tcp->tcp_state >= TCPS_ESTABLISHED) { 4263 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4264 tcp->tcp_client_errno = EWOULDBLOCK; 4265 } else if (tcp->tcp_client_errno == 0) { 4266 4267 ASSERT(tcp->tcp_linger_tid == 0); 4268 4269 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4270 tcp_close_linger_timeout, 4271 tcp->tcp_lingertime * hz); 4272 4273 /* tcp_close_linger_timeout will finish close */ 4274 if (tcp->tcp_linger_tid == 0) 4275 tcp->tcp_client_errno = ENOSR; 4276 else 4277 return; 4278 } 4279 4280 /* 4281 * Check if we need to detach or just close 4282 * the instance. 4283 */ 4284 if (tcp->tcp_state <= TCPS_LISTEN) 4285 break; 4286 } 4287 4288 /* 4289 * Make sure that no other thread will access the tcp_rq of 4290 * this instance (through lookups etc.) as tcp_rq will go 4291 * away shortly. 4292 */ 4293 tcp_acceptor_hash_remove(tcp); 4294 4295 mutex_enter(&tcp->tcp_non_sq_lock); 4296 if (tcp->tcp_flow_stopped) { 4297 tcp_clrqfull(tcp); 4298 } 4299 mutex_exit(&tcp->tcp_non_sq_lock); 4300 4301 if (tcp->tcp_timer_tid != 0) { 4302 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4303 tcp->tcp_timer_tid = 0; 4304 } 4305 /* 4306 * Need to cancel those timers which will not be used when 4307 * TCP is detached. This has to be done before the tcp_wq 4308 * is set to the global queue. 4309 */ 4310 tcp_timers_stop(tcp); 4311 4312 tcp->tcp_detached = B_TRUE; 4313 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4314 tcp_time_wait_append(tcp); 4315 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4316 ASSERT(connp->conn_ref >= 3); 4317 goto finish; 4318 } 4319 4320 /* 4321 * If delta is zero the timer event wasn't executed and was 4322 * successfully canceled. In this case we need to restart it 4323 * with the minimal delta possible. 4324 */ 4325 if (delta >= 0) 4326 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4327 delta ? delta : 1); 4328 4329 ASSERT(connp->conn_ref >= 3); 4330 goto finish; 4331 } 4332 4333 /* Detach did not complete. Still need to remove q from stream. */ 4334 if (msg) { 4335 if (tcp->tcp_state == TCPS_ESTABLISHED || 4336 tcp->tcp_state == TCPS_CLOSE_WAIT) 4337 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4338 if (tcp->tcp_state == TCPS_SYN_SENT || 4339 tcp->tcp_state == TCPS_SYN_RCVD) 4340 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4341 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4342 } 4343 4344 tcp_closei_local(tcp); 4345 CONN_DEC_REF(connp); 4346 ASSERT(connp->conn_ref >= 2); 4347 4348 finish: 4349 /* 4350 * Although packets are always processed on the correct 4351 * tcp's perimeter and access is serialized via squeue's, 4352 * IP still needs a queue when sending packets in time_wait 4353 * state so use WR(tcps_g_q) till ip_output() can be 4354 * changed to deal with just connp. For read side, we 4355 * could have set tcp_rq to NULL but there are some cases 4356 * in tcp_rput_data() from early days of this code which 4357 * do a putnext without checking if tcp is closed. Those 4358 * need to be identified before both tcp_rq and tcp_wq 4359 * can be set to NULL and tcps_g_q can disappear forever. 4360 */ 4361 mutex_enter(&tcp->tcp_closelock); 4362 /* 4363 * Don't change the queues in the case of a listener that has 4364 * eagers in its q or q0. It could surprise the eagers. 4365 * Instead wait for the eagers outside the squeue. 4366 */ 4367 if (!tcp->tcp_wait_for_eagers) { 4368 tcp->tcp_detached = B_TRUE; 4369 /* 4370 * When default queue is closing we set tcps_g_q to NULL 4371 * after the close is done. 4372 */ 4373 ASSERT(tcps->tcps_g_q != NULL); 4374 tcp->tcp_rq = tcps->tcps_g_q; 4375 tcp->tcp_wq = WR(tcps->tcps_g_q); 4376 } 4377 4378 /* Signal tcp_close() to finish closing. */ 4379 tcp->tcp_closed = 1; 4380 cv_signal(&tcp->tcp_closecv); 4381 mutex_exit(&tcp->tcp_closelock); 4382 } 4383 4384 4385 /* 4386 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4387 * Some stream heads get upset if they see these later on as anything but NULL. 4388 */ 4389 static void 4390 tcp_close_mpp(mblk_t **mpp) 4391 { 4392 mblk_t *mp; 4393 4394 if ((mp = *mpp) != NULL) { 4395 do { 4396 mp->b_next = NULL; 4397 mp->b_prev = NULL; 4398 } while ((mp = mp->b_cont) != NULL); 4399 4400 mp = *mpp; 4401 *mpp = NULL; 4402 freemsg(mp); 4403 } 4404 } 4405 4406 /* Do detached close. */ 4407 static void 4408 tcp_close_detached(tcp_t *tcp) 4409 { 4410 if (tcp->tcp_fused) 4411 tcp_unfuse(tcp); 4412 4413 /* 4414 * Clustering code serializes TCP disconnect callbacks and 4415 * cluster tcp list walks by blocking a TCP disconnect callback 4416 * if a cluster tcp list walk is in progress. This ensures 4417 * accurate accounting of TCPs in the cluster code even though 4418 * the TCP list walk itself is not atomic. 4419 */ 4420 tcp_closei_local(tcp); 4421 CONN_DEC_REF(tcp->tcp_connp); 4422 } 4423 4424 /* 4425 * Stop all TCP timers, and free the timer mblks if requested. 4426 */ 4427 void 4428 tcp_timers_stop(tcp_t *tcp) 4429 { 4430 if (tcp->tcp_timer_tid != 0) { 4431 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4432 tcp->tcp_timer_tid = 0; 4433 } 4434 if (tcp->tcp_ka_tid != 0) { 4435 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4436 tcp->tcp_ka_tid = 0; 4437 } 4438 if (tcp->tcp_ack_tid != 0) { 4439 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4440 tcp->tcp_ack_tid = 0; 4441 } 4442 if (tcp->tcp_push_tid != 0) { 4443 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4444 tcp->tcp_push_tid = 0; 4445 } 4446 } 4447 4448 /* 4449 * The tcp_t is going away. Remove it from all lists and set it 4450 * to TCPS_CLOSED. The freeing up of memory is deferred until 4451 * tcp_inactive. This is needed since a thread in tcp_rput might have 4452 * done a CONN_INC_REF on this structure before it was removed from the 4453 * hashes. 4454 */ 4455 static void 4456 tcp_closei_local(tcp_t *tcp) 4457 { 4458 ire_t *ire; 4459 conn_t *connp = tcp->tcp_connp; 4460 tcp_stack_t *tcps = tcp->tcp_tcps; 4461 4462 if (!TCP_IS_SOCKET(tcp)) 4463 tcp_acceptor_hash_remove(tcp); 4464 4465 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4466 tcp->tcp_ibsegs = 0; 4467 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4468 tcp->tcp_obsegs = 0; 4469 4470 /* 4471 * If we are an eager connection hanging off a listener that 4472 * hasn't formally accepted the connection yet, get off his 4473 * list and blow off any data that we have accumulated. 4474 */ 4475 if (tcp->tcp_listener != NULL) { 4476 tcp_t *listener = tcp->tcp_listener; 4477 mutex_enter(&listener->tcp_eager_lock); 4478 /* 4479 * tcp_tconnind_started == B_TRUE means that the 4480 * conn_ind has already gone to listener. At 4481 * this point, eager will be closed but we 4482 * leave it in listeners eager list so that 4483 * if listener decides to close without doing 4484 * accept, we can clean this up. In tcp_wput_accept 4485 * we take care of the case of accept on closed 4486 * eager. 4487 */ 4488 if (!tcp->tcp_tconnind_started) { 4489 tcp_eager_unlink(tcp); 4490 mutex_exit(&listener->tcp_eager_lock); 4491 /* 4492 * We don't want to have any pointers to the 4493 * listener queue, after we have released our 4494 * reference on the listener 4495 */ 4496 ASSERT(tcps->tcps_g_q != NULL); 4497 tcp->tcp_rq = tcps->tcps_g_q; 4498 tcp->tcp_wq = WR(tcps->tcps_g_q); 4499 CONN_DEC_REF(listener->tcp_connp); 4500 } else { 4501 mutex_exit(&listener->tcp_eager_lock); 4502 } 4503 } 4504 4505 /* Stop all the timers */ 4506 tcp_timers_stop(tcp); 4507 4508 if (tcp->tcp_state == TCPS_LISTEN) { 4509 if (tcp->tcp_ip_addr_cache) { 4510 kmem_free((void *)tcp->tcp_ip_addr_cache, 4511 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4512 tcp->tcp_ip_addr_cache = NULL; 4513 } 4514 } 4515 mutex_enter(&tcp->tcp_non_sq_lock); 4516 if (tcp->tcp_flow_stopped) 4517 tcp_clrqfull(tcp); 4518 mutex_exit(&tcp->tcp_non_sq_lock); 4519 4520 tcp_bind_hash_remove(tcp); 4521 /* 4522 * If the tcp_time_wait_collector (which runs outside the squeue) 4523 * is trying to remove this tcp from the time wait list, we will 4524 * block in tcp_time_wait_remove while trying to acquire the 4525 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4526 * requires the ipcl_hash_remove to be ordered after the 4527 * tcp_time_wait_remove for the refcnt checks to work correctly. 4528 */ 4529 if (tcp->tcp_state == TCPS_TIME_WAIT) 4530 (void) tcp_time_wait_remove(tcp, NULL); 4531 CL_INET_DISCONNECT(tcp); 4532 ipcl_hash_remove(connp); 4533 4534 /* 4535 * Delete the cached ire in conn_ire_cache and also mark 4536 * the conn as CONDEMNED 4537 */ 4538 mutex_enter(&connp->conn_lock); 4539 connp->conn_state_flags |= CONN_CONDEMNED; 4540 ire = connp->conn_ire_cache; 4541 connp->conn_ire_cache = NULL; 4542 mutex_exit(&connp->conn_lock); 4543 if (ire != NULL) 4544 IRE_REFRELE_NOTR(ire); 4545 4546 /* Need to cleanup any pending ioctls */ 4547 ASSERT(tcp->tcp_time_wait_next == NULL); 4548 ASSERT(tcp->tcp_time_wait_prev == NULL); 4549 ASSERT(tcp->tcp_time_wait_expire == 0); 4550 tcp->tcp_state = TCPS_CLOSED; 4551 4552 /* Release any SSL context */ 4553 if (tcp->tcp_kssl_ent != NULL) { 4554 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4555 tcp->tcp_kssl_ent = NULL; 4556 } 4557 if (tcp->tcp_kssl_ctx != NULL) { 4558 kssl_release_ctx(tcp->tcp_kssl_ctx); 4559 tcp->tcp_kssl_ctx = NULL; 4560 } 4561 tcp->tcp_kssl_pending = B_FALSE; 4562 4563 tcp_ipsec_cleanup(tcp); 4564 } 4565 4566 /* 4567 * tcp is dying (called from ipcl_conn_destroy and error cases). 4568 * Free the tcp_t in either case. 4569 */ 4570 void 4571 tcp_free(tcp_t *tcp) 4572 { 4573 mblk_t *mp; 4574 ip6_pkt_t *ipp; 4575 4576 ASSERT(tcp != NULL); 4577 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4578 4579 tcp->tcp_rq = NULL; 4580 tcp->tcp_wq = NULL; 4581 4582 tcp_close_mpp(&tcp->tcp_xmit_head); 4583 tcp_close_mpp(&tcp->tcp_reass_head); 4584 if (tcp->tcp_rcv_list != NULL) { 4585 /* Free b_next chain */ 4586 tcp_close_mpp(&tcp->tcp_rcv_list); 4587 } 4588 if ((mp = tcp->tcp_urp_mp) != NULL) { 4589 freemsg(mp); 4590 } 4591 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4592 freemsg(mp); 4593 } 4594 4595 if (tcp->tcp_fused_sigurg_mp != NULL) { 4596 freeb(tcp->tcp_fused_sigurg_mp); 4597 tcp->tcp_fused_sigurg_mp = NULL; 4598 } 4599 4600 if (tcp->tcp_sack_info != NULL) { 4601 if (tcp->tcp_notsack_list != NULL) { 4602 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4603 } 4604 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4605 } 4606 4607 if (tcp->tcp_hopopts != NULL) { 4608 mi_free(tcp->tcp_hopopts); 4609 tcp->tcp_hopopts = NULL; 4610 tcp->tcp_hopoptslen = 0; 4611 } 4612 ASSERT(tcp->tcp_hopoptslen == 0); 4613 if (tcp->tcp_dstopts != NULL) { 4614 mi_free(tcp->tcp_dstopts); 4615 tcp->tcp_dstopts = NULL; 4616 tcp->tcp_dstoptslen = 0; 4617 } 4618 ASSERT(tcp->tcp_dstoptslen == 0); 4619 if (tcp->tcp_rtdstopts != NULL) { 4620 mi_free(tcp->tcp_rtdstopts); 4621 tcp->tcp_rtdstopts = NULL; 4622 tcp->tcp_rtdstoptslen = 0; 4623 } 4624 ASSERT(tcp->tcp_rtdstoptslen == 0); 4625 if (tcp->tcp_rthdr != NULL) { 4626 mi_free(tcp->tcp_rthdr); 4627 tcp->tcp_rthdr = NULL; 4628 tcp->tcp_rthdrlen = 0; 4629 } 4630 ASSERT(tcp->tcp_rthdrlen == 0); 4631 4632 ipp = &tcp->tcp_sticky_ipp; 4633 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4634 IPPF_RTHDR)) 4635 ip6_pkt_free(ipp); 4636 4637 /* 4638 * Free memory associated with the tcp/ip header template. 4639 */ 4640 4641 if (tcp->tcp_iphc != NULL) 4642 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4643 4644 /* 4645 * Following is really a blowing away a union. 4646 * It happens to have exactly two members of identical size 4647 * the following code is enough. 4648 */ 4649 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4650 4651 if (tcp->tcp_tracebuf != NULL) { 4652 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4653 tcp->tcp_tracebuf = NULL; 4654 } 4655 } 4656 4657 4658 /* 4659 * Put a connection confirmation message upstream built from the 4660 * address information within 'iph' and 'tcph'. Report our success or failure. 4661 */ 4662 static boolean_t 4663 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4664 mblk_t **defermp) 4665 { 4666 sin_t sin; 4667 sin6_t sin6; 4668 mblk_t *mp; 4669 char *optp = NULL; 4670 int optlen = 0; 4671 cred_t *cr; 4672 4673 if (defermp != NULL) 4674 *defermp = NULL; 4675 4676 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4677 /* 4678 * Return in T_CONN_CON results of option negotiation through 4679 * the T_CONN_REQ. Note: If there is an real end-to-end option 4680 * negotiation, then what is received from remote end needs 4681 * to be taken into account but there is no such thing (yet?) 4682 * in our TCP/IP. 4683 * Note: We do not use mi_offset_param() here as 4684 * tcp_opts_conn_req contents do not directly come from 4685 * an application and are either generated in kernel or 4686 * from user input that was already verified. 4687 */ 4688 mp = tcp->tcp_conn.tcp_opts_conn_req; 4689 optp = (char *)(mp->b_rptr + 4690 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4691 optlen = (int) 4692 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4693 } 4694 4695 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4696 ipha_t *ipha = (ipha_t *)iphdr; 4697 4698 /* packet is IPv4 */ 4699 if (tcp->tcp_family == AF_INET) { 4700 sin = sin_null; 4701 sin.sin_addr.s_addr = ipha->ipha_src; 4702 sin.sin_port = *(uint16_t *)tcph->th_lport; 4703 sin.sin_family = AF_INET; 4704 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4705 (int)sizeof (sin_t), optp, optlen); 4706 } else { 4707 sin6 = sin6_null; 4708 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4709 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4710 sin6.sin6_family = AF_INET6; 4711 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4712 (int)sizeof (sin6_t), optp, optlen); 4713 4714 } 4715 } else { 4716 ip6_t *ip6h = (ip6_t *)iphdr; 4717 4718 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4719 ASSERT(tcp->tcp_family == AF_INET6); 4720 sin6 = sin6_null; 4721 sin6.sin6_addr = ip6h->ip6_src; 4722 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4723 sin6.sin6_family = AF_INET6; 4724 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4725 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4726 (int)sizeof (sin6_t), optp, optlen); 4727 } 4728 4729 if (!mp) 4730 return (B_FALSE); 4731 4732 if ((cr = DB_CRED(idmp)) != NULL) { 4733 mblk_setcred(mp, cr); 4734 DB_CPID(mp) = DB_CPID(idmp); 4735 } 4736 4737 if (defermp == NULL) 4738 putnext(tcp->tcp_rq, mp); 4739 else 4740 *defermp = mp; 4741 4742 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4743 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4744 return (B_TRUE); 4745 } 4746 4747 /* 4748 * Defense for the SYN attack - 4749 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4750 * one from the list of droppable eagers. This list is a subset of q0. 4751 * see comments before the definition of MAKE_DROPPABLE(). 4752 * 2. Don't drop a SYN request before its first timeout. This gives every 4753 * request at least til the first timeout to complete its 3-way handshake. 4754 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4755 * requests currently on the queue that has timed out. This will be used 4756 * as an indicator of whether an attack is under way, so that appropriate 4757 * actions can be taken. (It's incremented in tcp_timer() and decremented 4758 * either when eager goes into ESTABLISHED, or gets freed up.) 4759 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4760 * # of timeout drops back to <= q0len/32 => SYN alert off 4761 */ 4762 static boolean_t 4763 tcp_drop_q0(tcp_t *tcp) 4764 { 4765 tcp_t *eager; 4766 mblk_t *mp; 4767 tcp_stack_t *tcps = tcp->tcp_tcps; 4768 4769 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4770 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4771 4772 /* Pick oldest eager from the list of droppable eagers */ 4773 eager = tcp->tcp_eager_prev_drop_q0; 4774 4775 /* If list is empty. return B_FALSE */ 4776 if (eager == tcp) { 4777 return (B_FALSE); 4778 } 4779 4780 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4781 if ((mp = allocb(0, BPRI_HI)) == NULL) 4782 return (B_FALSE); 4783 4784 /* 4785 * Take this eager out from the list of droppable eagers since we are 4786 * going to drop it. 4787 */ 4788 MAKE_UNDROPPABLE(eager); 4789 4790 if (tcp->tcp_debug) { 4791 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4792 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4793 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4794 tcp->tcp_conn_req_cnt_q0, 4795 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4796 } 4797 4798 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4799 4800 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4801 CONN_INC_REF(eager->tcp_connp); 4802 4803 /* Mark the IRE created for this SYN request temporary */ 4804 tcp_ip_ire_mark_advice(eager); 4805 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4806 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4807 4808 return (B_TRUE); 4809 } 4810 4811 int 4812 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4813 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4814 { 4815 tcp_t *ltcp = lconnp->conn_tcp; 4816 tcp_t *tcp = connp->conn_tcp; 4817 mblk_t *tpi_mp; 4818 ipha_t *ipha; 4819 ip6_t *ip6h; 4820 sin6_t sin6; 4821 in6_addr_t v6dst; 4822 int err; 4823 int ifindex = 0; 4824 cred_t *cr; 4825 tcp_stack_t *tcps = tcp->tcp_tcps; 4826 4827 if (ipvers == IPV4_VERSION) { 4828 ipha = (ipha_t *)mp->b_rptr; 4829 4830 connp->conn_send = ip_output; 4831 connp->conn_recv = tcp_input; 4832 4833 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4834 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4835 4836 sin6 = sin6_null; 4837 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4838 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4839 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4840 sin6.sin6_family = AF_INET6; 4841 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4842 lconnp->conn_zoneid, tcps->tcps_netstack); 4843 if (tcp->tcp_recvdstaddr) { 4844 sin6_t sin6d; 4845 4846 sin6d = sin6_null; 4847 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4848 &sin6d.sin6_addr); 4849 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4850 sin6d.sin6_family = AF_INET; 4851 tpi_mp = mi_tpi_extconn_ind(NULL, 4852 (char *)&sin6d, sizeof (sin6_t), 4853 (char *)&tcp, 4854 (t_scalar_t)sizeof (intptr_t), 4855 (char *)&sin6d, sizeof (sin6_t), 4856 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4857 } else { 4858 tpi_mp = mi_tpi_conn_ind(NULL, 4859 (char *)&sin6, sizeof (sin6_t), 4860 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4861 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4862 } 4863 } else { 4864 ip6h = (ip6_t *)mp->b_rptr; 4865 4866 connp->conn_send = ip_output_v6; 4867 connp->conn_recv = tcp_input; 4868 4869 connp->conn_srcv6 = ip6h->ip6_dst; 4870 connp->conn_remv6 = ip6h->ip6_src; 4871 4872 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4873 ifindex = (int)DB_CKSUMSTUFF(mp); 4874 DB_CKSUMSTUFF(mp) = 0; 4875 4876 sin6 = sin6_null; 4877 sin6.sin6_addr = ip6h->ip6_src; 4878 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4879 sin6.sin6_family = AF_INET6; 4880 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4881 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4882 lconnp->conn_zoneid, tcps->tcps_netstack); 4883 4884 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4885 /* Pass up the scope_id of remote addr */ 4886 sin6.sin6_scope_id = ifindex; 4887 } else { 4888 sin6.sin6_scope_id = 0; 4889 } 4890 if (tcp->tcp_recvdstaddr) { 4891 sin6_t sin6d; 4892 4893 sin6d = sin6_null; 4894 sin6.sin6_addr = ip6h->ip6_dst; 4895 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4896 sin6d.sin6_family = AF_INET; 4897 tpi_mp = mi_tpi_extconn_ind(NULL, 4898 (char *)&sin6d, sizeof (sin6_t), 4899 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4900 (char *)&sin6d, sizeof (sin6_t), 4901 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4902 } else { 4903 tpi_mp = mi_tpi_conn_ind(NULL, 4904 (char *)&sin6, sizeof (sin6_t), 4905 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4906 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4907 } 4908 } 4909 4910 if (tpi_mp == NULL) 4911 return (ENOMEM); 4912 4913 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4914 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4915 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4916 connp->conn_fully_bound = B_FALSE; 4917 4918 if (tcps->tcps_trace) 4919 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4920 4921 /* Inherit information from the "parent" */ 4922 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4923 tcp->tcp_family = ltcp->tcp_family; 4924 tcp->tcp_wq = ltcp->tcp_wq; 4925 tcp->tcp_rq = ltcp->tcp_rq; 4926 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4927 tcp->tcp_detached = B_TRUE; 4928 if ((err = tcp_init_values(tcp)) != 0) { 4929 freemsg(tpi_mp); 4930 return (err); 4931 } 4932 4933 if (ipvers == IPV4_VERSION) { 4934 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4935 freemsg(tpi_mp); 4936 return (err); 4937 } 4938 ASSERT(tcp->tcp_ipha != NULL); 4939 } else { 4940 /* ifindex must be already set */ 4941 ASSERT(ifindex != 0); 4942 4943 if (ltcp->tcp_bound_if != 0) { 4944 /* 4945 * Set newtcp's bound_if equal to 4946 * listener's value. If ifindex is 4947 * not the same as ltcp->tcp_bound_if, 4948 * it must be a packet for the ipmp group 4949 * of interfaces 4950 */ 4951 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4952 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4953 tcp->tcp_bound_if = ifindex; 4954 } 4955 4956 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4957 tcp->tcp_recvifindex = 0; 4958 tcp->tcp_recvhops = 0xffffffffU; 4959 ASSERT(tcp->tcp_ip6h != NULL); 4960 } 4961 4962 tcp->tcp_lport = ltcp->tcp_lport; 4963 4964 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4965 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4966 /* 4967 * Listener had options of some sort; eager inherits. 4968 * Free up the eager template and allocate one 4969 * of the right size. 4970 */ 4971 if (tcp->tcp_hdr_grown) { 4972 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4973 } else { 4974 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4975 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4976 } 4977 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4978 KM_NOSLEEP); 4979 if (tcp->tcp_iphc == NULL) { 4980 tcp->tcp_iphc_len = 0; 4981 freemsg(tpi_mp); 4982 return (ENOMEM); 4983 } 4984 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4985 tcp->tcp_hdr_grown = B_TRUE; 4986 } 4987 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4988 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4989 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4990 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4991 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4992 4993 /* 4994 * Copy the IP+TCP header template from listener to eager 4995 */ 4996 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4997 if (tcp->tcp_ipversion == IPV6_VERSION) { 4998 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4999 IPPROTO_RAW) { 5000 tcp->tcp_ip6h = 5001 (ip6_t *)(tcp->tcp_iphc + 5002 sizeof (ip6i_t)); 5003 } else { 5004 tcp->tcp_ip6h = 5005 (ip6_t *)(tcp->tcp_iphc); 5006 } 5007 tcp->tcp_ipha = NULL; 5008 } else { 5009 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5010 tcp->tcp_ip6h = NULL; 5011 } 5012 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5013 tcp->tcp_ip_hdr_len); 5014 } else { 5015 /* 5016 * only valid case when ipversion of listener and 5017 * eager differ is when listener is IPv6 and 5018 * eager is IPv4. 5019 * Eager header template has been initialized to the 5020 * maximum v4 header sizes, which includes space for 5021 * TCP and IP options. 5022 */ 5023 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5024 (tcp->tcp_ipversion == IPV4_VERSION)); 5025 ASSERT(tcp->tcp_iphc_len >= 5026 TCP_MAX_COMBINED_HEADER_LENGTH); 5027 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5028 /* copy IP header fields individually */ 5029 tcp->tcp_ipha->ipha_ttl = 5030 ltcp->tcp_ip6h->ip6_hops; 5031 bcopy(ltcp->tcp_tcph->th_lport, 5032 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5033 } 5034 5035 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5036 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5037 sizeof (in_port_t)); 5038 5039 if (ltcp->tcp_lport == 0) { 5040 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5041 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5042 sizeof (in_port_t)); 5043 } 5044 5045 if (tcp->tcp_ipversion == IPV4_VERSION) { 5046 ASSERT(ipha != NULL); 5047 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5048 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5049 5050 /* Source routing option copyover (reverse it) */ 5051 if (tcps->tcps_rev_src_routes) 5052 tcp_opt_reverse(tcp, ipha); 5053 } else { 5054 ASSERT(ip6h != NULL); 5055 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5056 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5057 } 5058 5059 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5060 ASSERT(!tcp->tcp_tconnind_started); 5061 /* 5062 * If the SYN contains a credential, it's a loopback packet; attach 5063 * the credential to the TPI message. 5064 */ 5065 if ((cr = DB_CRED(idmp)) != NULL) { 5066 mblk_setcred(tpi_mp, cr); 5067 DB_CPID(tpi_mp) = DB_CPID(idmp); 5068 } 5069 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5070 5071 /* Inherit the listener's SSL protection state */ 5072 5073 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5074 kssl_hold_ent(tcp->tcp_kssl_ent); 5075 tcp->tcp_kssl_pending = B_TRUE; 5076 } 5077 5078 return (0); 5079 } 5080 5081 5082 int 5083 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5084 tcph_t *tcph, mblk_t *idmp) 5085 { 5086 tcp_t *ltcp = lconnp->conn_tcp; 5087 tcp_t *tcp = connp->conn_tcp; 5088 sin_t sin; 5089 mblk_t *tpi_mp = NULL; 5090 int err; 5091 cred_t *cr; 5092 tcp_stack_t *tcps = tcp->tcp_tcps; 5093 5094 sin = sin_null; 5095 sin.sin_addr.s_addr = ipha->ipha_src; 5096 sin.sin_port = *(uint16_t *)tcph->th_lport; 5097 sin.sin_family = AF_INET; 5098 if (ltcp->tcp_recvdstaddr) { 5099 sin_t sind; 5100 5101 sind = sin_null; 5102 sind.sin_addr.s_addr = ipha->ipha_dst; 5103 sind.sin_port = *(uint16_t *)tcph->th_fport; 5104 sind.sin_family = AF_INET; 5105 tpi_mp = mi_tpi_extconn_ind(NULL, 5106 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5107 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5108 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5109 } else { 5110 tpi_mp = mi_tpi_conn_ind(NULL, 5111 (char *)&sin, sizeof (sin_t), 5112 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5113 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5114 } 5115 5116 if (tpi_mp == NULL) { 5117 return (ENOMEM); 5118 } 5119 5120 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5121 connp->conn_send = ip_output; 5122 connp->conn_recv = tcp_input; 5123 connp->conn_fully_bound = B_FALSE; 5124 5125 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5126 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5127 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5128 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5129 5130 if (tcps->tcps_trace) { 5131 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 5132 } 5133 5134 /* Inherit information from the "parent" */ 5135 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5136 tcp->tcp_family = ltcp->tcp_family; 5137 tcp->tcp_wq = ltcp->tcp_wq; 5138 tcp->tcp_rq = ltcp->tcp_rq; 5139 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 5140 tcp->tcp_detached = B_TRUE; 5141 if ((err = tcp_init_values(tcp)) != 0) { 5142 freemsg(tpi_mp); 5143 return (err); 5144 } 5145 5146 /* 5147 * Let's make sure that eager tcp template has enough space to 5148 * copy IPv4 listener's tcp template. Since the conn_t structure is 5149 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5150 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5151 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5152 * extension headers or with ip6i_t struct). Note that bcopy() below 5153 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5154 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5155 */ 5156 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5157 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5158 5159 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5160 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5161 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5162 tcp->tcp_ttl = ltcp->tcp_ttl; 5163 tcp->tcp_tos = ltcp->tcp_tos; 5164 5165 /* Copy the IP+TCP header template from listener to eager */ 5166 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5167 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5168 tcp->tcp_ip6h = NULL; 5169 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5170 tcp->tcp_ip_hdr_len); 5171 5172 /* Initialize the IP addresses and Ports */ 5173 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5174 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5175 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5176 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5177 5178 /* Source routing option copyover (reverse it) */ 5179 if (tcps->tcps_rev_src_routes) 5180 tcp_opt_reverse(tcp, ipha); 5181 5182 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5183 ASSERT(!tcp->tcp_tconnind_started); 5184 5185 /* 5186 * If the SYN contains a credential, it's a loopback packet; attach 5187 * the credential to the TPI message. 5188 */ 5189 if ((cr = DB_CRED(idmp)) != NULL) { 5190 mblk_setcred(tpi_mp, cr); 5191 DB_CPID(tpi_mp) = DB_CPID(idmp); 5192 } 5193 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5194 5195 /* Inherit the listener's SSL protection state */ 5196 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5197 kssl_hold_ent(tcp->tcp_kssl_ent); 5198 tcp->tcp_kssl_pending = B_TRUE; 5199 } 5200 5201 return (0); 5202 } 5203 5204 /* 5205 * sets up conn for ipsec. 5206 * if the first mblk is M_CTL it is consumed and mpp is updated. 5207 * in case of error mpp is freed. 5208 */ 5209 conn_t * 5210 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5211 { 5212 conn_t *connp = tcp->tcp_connp; 5213 conn_t *econnp; 5214 squeue_t *new_sqp; 5215 mblk_t *first_mp = *mpp; 5216 mblk_t *mp = *mpp; 5217 boolean_t mctl_present = B_FALSE; 5218 uint_t ipvers; 5219 5220 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 5221 if (econnp == NULL) { 5222 freemsg(first_mp); 5223 return (NULL); 5224 } 5225 if (DB_TYPE(mp) == M_CTL) { 5226 if (mp->b_cont == NULL || 5227 mp->b_cont->b_datap->db_type != M_DATA) { 5228 freemsg(first_mp); 5229 return (NULL); 5230 } 5231 mp = mp->b_cont; 5232 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5233 freemsg(first_mp); 5234 return (NULL); 5235 } 5236 5237 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5238 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5239 mctl_present = B_TRUE; 5240 } else { 5241 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5242 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5243 } 5244 5245 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5246 DB_CKSUMSTART(mp) = 0; 5247 5248 ASSERT(OK_32PTR(mp->b_rptr)); 5249 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5250 if (ipvers == IPV4_VERSION) { 5251 uint16_t *up; 5252 uint32_t ports; 5253 ipha_t *ipha; 5254 5255 ipha = (ipha_t *)mp->b_rptr; 5256 up = (uint16_t *)((uchar_t *)ipha + 5257 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5258 ports = *(uint32_t *)up; 5259 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5260 ipha->ipha_dst, ipha->ipha_src, ports); 5261 } else { 5262 uint16_t *up; 5263 uint32_t ports; 5264 uint16_t ip_hdr_len; 5265 uint8_t *nexthdrp; 5266 ip6_t *ip6h; 5267 tcph_t *tcph; 5268 5269 ip6h = (ip6_t *)mp->b_rptr; 5270 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5271 ip_hdr_len = IPV6_HDR_LEN; 5272 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5273 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5274 CONN_DEC_REF(econnp); 5275 freemsg(first_mp); 5276 return (NULL); 5277 } 5278 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5279 up = (uint16_t *)tcph->th_lport; 5280 ports = *(uint32_t *)up; 5281 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5282 ip6h->ip6_dst, ip6h->ip6_src, ports); 5283 } 5284 5285 /* 5286 * The caller already ensured that there is a sqp present. 5287 */ 5288 econnp->conn_sqp = new_sqp; 5289 5290 if (connp->conn_policy != NULL) { 5291 ipsec_in_t *ii; 5292 ii = (ipsec_in_t *)(first_mp->b_rptr); 5293 ASSERT(ii->ipsec_in_policy == NULL); 5294 IPPH_REFHOLD(connp->conn_policy); 5295 ii->ipsec_in_policy = connp->conn_policy; 5296 5297 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5298 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5299 CONN_DEC_REF(econnp); 5300 freemsg(first_mp); 5301 return (NULL); 5302 } 5303 } 5304 5305 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5306 CONN_DEC_REF(econnp); 5307 freemsg(first_mp); 5308 return (NULL); 5309 } 5310 5311 /* 5312 * If we know we have some policy, pass the "IPSEC" 5313 * options size TCP uses this adjust the MSS. 5314 */ 5315 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5316 if (mctl_present) { 5317 freeb(first_mp); 5318 *mpp = mp; 5319 } 5320 5321 return (econnp); 5322 } 5323 5324 /* 5325 * tcp_get_conn/tcp_free_conn 5326 * 5327 * tcp_get_conn is used to get a clean tcp connection structure. 5328 * It tries to reuse the connections put on the freelist by the 5329 * time_wait_collector failing which it goes to kmem_cache. This 5330 * way has two benefits compared to just allocating from and 5331 * freeing to kmem_cache. 5332 * 1) The time_wait_collector can free (which includes the cleanup) 5333 * outside the squeue. So when the interrupt comes, we have a clean 5334 * connection sitting in the freelist. Obviously, this buys us 5335 * performance. 5336 * 5337 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5338 * has multiple disadvantages - tying up the squeue during alloc, and the 5339 * fact that IPSec policy initialization has to happen here which 5340 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5341 * But allocating the conn/tcp in IP land is also not the best since 5342 * we can't check the 'q' and 'q0' which are protected by squeue and 5343 * blindly allocate memory which might have to be freed here if we are 5344 * not allowed to accept the connection. By using the freelist and 5345 * putting the conn/tcp back in freelist, we don't pay a penalty for 5346 * allocating memory without checking 'q/q0' and freeing it if we can't 5347 * accept the connection. 5348 * 5349 * Care should be taken to put the conn back in the same squeue's freelist 5350 * from which it was allocated. Best results are obtained if conn is 5351 * allocated from listener's squeue and freed to the same. Time wait 5352 * collector will free up the freelist is the connection ends up sitting 5353 * there for too long. 5354 */ 5355 void * 5356 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5357 { 5358 tcp_t *tcp = NULL; 5359 conn_t *connp = NULL; 5360 squeue_t *sqp = (squeue_t *)arg; 5361 tcp_squeue_priv_t *tcp_time_wait; 5362 netstack_t *ns; 5363 5364 tcp_time_wait = 5365 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5366 5367 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5368 tcp = tcp_time_wait->tcp_free_list; 5369 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5370 if (tcp != NULL) { 5371 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5372 tcp_time_wait->tcp_free_list_cnt--; 5373 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5374 tcp->tcp_time_wait_next = NULL; 5375 connp = tcp->tcp_connp; 5376 connp->conn_flags |= IPCL_REUSED; 5377 5378 ASSERT(tcp->tcp_tcps == NULL); 5379 ASSERT(connp->conn_netstack == NULL); 5380 ns = tcps->tcps_netstack; 5381 netstack_hold(ns); 5382 connp->conn_netstack = ns; 5383 tcp->tcp_tcps = tcps; 5384 TCPS_REFHOLD(tcps); 5385 ipcl_globalhash_insert(connp); 5386 return ((void *)connp); 5387 } 5388 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5389 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5390 tcps->tcps_netstack)) == NULL) 5391 return (NULL); 5392 tcp = connp->conn_tcp; 5393 tcp->tcp_tcps = tcps; 5394 TCPS_REFHOLD(tcps); 5395 return ((void *)connp); 5396 } 5397 5398 /* 5399 * Update the cached label for the given tcp_t. This should be called once per 5400 * connection, and before any packets are sent or tcp_process_options is 5401 * invoked. Returns B_FALSE if the correct label could not be constructed. 5402 */ 5403 static boolean_t 5404 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5405 { 5406 conn_t *connp = tcp->tcp_connp; 5407 5408 if (tcp->tcp_ipversion == IPV4_VERSION) { 5409 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5410 int added; 5411 5412 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5413 connp->conn_mac_exempt, 5414 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5415 return (B_FALSE); 5416 5417 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5418 if (added == -1) 5419 return (B_FALSE); 5420 tcp->tcp_hdr_len += added; 5421 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5422 tcp->tcp_ip_hdr_len += added; 5423 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5424 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5425 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5426 tcp->tcp_hdr_len); 5427 if (added == -1) 5428 return (B_FALSE); 5429 tcp->tcp_hdr_len += added; 5430 tcp->tcp_tcph = (tcph_t *) 5431 ((uchar_t *)tcp->tcp_tcph + added); 5432 tcp->tcp_ip_hdr_len += added; 5433 } 5434 } else { 5435 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5436 5437 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5438 connp->conn_mac_exempt, 5439 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5440 return (B_FALSE); 5441 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5442 &tcp->tcp_label_len, optbuf) != 0) 5443 return (B_FALSE); 5444 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5445 return (B_FALSE); 5446 } 5447 5448 connp->conn_ulp_labeled = 1; 5449 5450 return (B_TRUE); 5451 } 5452 5453 /* BEGIN CSTYLED */ 5454 /* 5455 * 5456 * The sockfs ACCEPT path: 5457 * ======================= 5458 * 5459 * The eager is now established in its own perimeter as soon as SYN is 5460 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5461 * completes the accept processing on the acceptor STREAM. The sending 5462 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5463 * listener but a TLI/XTI listener completes the accept processing 5464 * on the listener perimeter. 5465 * 5466 * Common control flow for 3 way handshake: 5467 * ---------------------------------------- 5468 * 5469 * incoming SYN (listener perimeter) -> tcp_rput_data() 5470 * -> tcp_conn_request() 5471 * 5472 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5473 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5474 * 5475 * Sockfs ACCEPT Path: 5476 * ------------------- 5477 * 5478 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5479 * as STREAM entry point) 5480 * 5481 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5482 * 5483 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5484 * association (we are not behind eager's squeue but sockfs is protecting us 5485 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5486 * is changed to point at tcp_wput(). 5487 * 5488 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5489 * listener (done on listener's perimeter). 5490 * 5491 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5492 * accept. 5493 * 5494 * TLI/XTI client ACCEPT path: 5495 * --------------------------- 5496 * 5497 * soaccept() sends T_CONN_RES on the listener STREAM. 5498 * 5499 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5500 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5501 * 5502 * Locks: 5503 * ====== 5504 * 5505 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5506 * and listeners->tcp_eager_next_q. 5507 * 5508 * Referencing: 5509 * ============ 5510 * 5511 * 1) We start out in tcp_conn_request by eager placing a ref on 5512 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5513 * 5514 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5515 * doing so we place a ref on the eager. This ref is finally dropped at the 5516 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5517 * reference is dropped by the squeue framework. 5518 * 5519 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5520 * 5521 * The reference must be released by the same entity that added the reference 5522 * In the above scheme, the eager is the entity that adds and releases the 5523 * references. Note that tcp_accept_finish executes in the squeue of the eager 5524 * (albeit after it is attached to the acceptor stream). Though 1. executes 5525 * in the listener's squeue, the eager is nascent at this point and the 5526 * reference can be considered to have been added on behalf of the eager. 5527 * 5528 * Eager getting a Reset or listener closing: 5529 * ========================================== 5530 * 5531 * Once the listener and eager are linked, the listener never does the unlink. 5532 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5533 * a message on all eager perimeter. The eager then does the unlink, clears 5534 * any pointers to the listener's queue and drops the reference to the 5535 * listener. The listener waits in tcp_close outside the squeue until its 5536 * refcount has dropped to 1. This ensures that the listener has waited for 5537 * all eagers to clear their association with the listener. 5538 * 5539 * Similarly, if eager decides to go away, it can unlink itself and close. 5540 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5541 * the reference to eager is still valid because of the extra ref we put 5542 * in tcp_send_conn_ind. 5543 * 5544 * Listener can always locate the eager under the protection 5545 * of the listener->tcp_eager_lock, and then do a refhold 5546 * on the eager during the accept processing. 5547 * 5548 * The acceptor stream accesses the eager in the accept processing 5549 * based on the ref placed on eager before sending T_conn_ind. 5550 * The only entity that can negate this refhold is a listener close 5551 * which is mutually exclusive with an active acceptor stream. 5552 * 5553 * Eager's reference on the listener 5554 * =================================== 5555 * 5556 * If the accept happens (even on a closed eager) the eager drops its 5557 * reference on the listener at the start of tcp_accept_finish. If the 5558 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5559 * the reference is dropped in tcp_closei_local. If the listener closes, 5560 * the reference is dropped in tcp_eager_kill. In all cases the reference 5561 * is dropped while executing in the eager's context (squeue). 5562 */ 5563 /* END CSTYLED */ 5564 5565 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5566 5567 /* 5568 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5569 * tcp_rput_data will not see any SYN packets. 5570 */ 5571 /* ARGSUSED */ 5572 void 5573 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5574 { 5575 tcph_t *tcph; 5576 uint32_t seg_seq; 5577 tcp_t *eager; 5578 uint_t ipvers; 5579 ipha_t *ipha; 5580 ip6_t *ip6h; 5581 int err; 5582 conn_t *econnp = NULL; 5583 squeue_t *new_sqp; 5584 mblk_t *mp1; 5585 uint_t ip_hdr_len; 5586 conn_t *connp = (conn_t *)arg; 5587 tcp_t *tcp = connp->conn_tcp; 5588 cred_t *credp; 5589 tcp_stack_t *tcps = tcp->tcp_tcps; 5590 ip_stack_t *ipst; 5591 5592 if (tcp->tcp_state != TCPS_LISTEN) 5593 goto error2; 5594 5595 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5596 5597 mutex_enter(&tcp->tcp_eager_lock); 5598 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5599 mutex_exit(&tcp->tcp_eager_lock); 5600 TCP_STAT(tcps, tcp_listendrop); 5601 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5602 if (tcp->tcp_debug) { 5603 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5604 "tcp_conn_request: listen backlog (max=%d) " 5605 "overflow (%d pending) on %s", 5606 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5607 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5608 } 5609 goto error2; 5610 } 5611 5612 if (tcp->tcp_conn_req_cnt_q0 >= 5613 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5614 /* 5615 * Q0 is full. Drop a pending half-open req from the queue 5616 * to make room for the new SYN req. Also mark the time we 5617 * drop a SYN. 5618 * 5619 * A more aggressive defense against SYN attack will 5620 * be to set the "tcp_syn_defense" flag now. 5621 */ 5622 TCP_STAT(tcps, tcp_listendropq0); 5623 tcp->tcp_last_rcv_lbolt = lbolt64; 5624 if (!tcp_drop_q0(tcp)) { 5625 mutex_exit(&tcp->tcp_eager_lock); 5626 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5627 if (tcp->tcp_debug) { 5628 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5629 "tcp_conn_request: listen half-open queue " 5630 "(max=%d) full (%d pending) on %s", 5631 tcps->tcps_conn_req_max_q0, 5632 tcp->tcp_conn_req_cnt_q0, 5633 tcp_display(tcp, NULL, 5634 DISP_PORT_ONLY)); 5635 } 5636 goto error2; 5637 } 5638 } 5639 mutex_exit(&tcp->tcp_eager_lock); 5640 5641 /* 5642 * IP adds STRUIO_EAGER and ensures that the received packet is 5643 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5644 * link local address. If IPSec is enabled, db_struioflag has 5645 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5646 * otherwise an error case if neither of them is set. 5647 */ 5648 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5649 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5650 DB_CKSUMSTART(mp) = 0; 5651 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5652 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5653 if (econnp == NULL) 5654 goto error2; 5655 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5656 econnp->conn_sqp = new_sqp; 5657 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5658 /* 5659 * mp is updated in tcp_get_ipsec_conn(). 5660 */ 5661 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5662 if (econnp == NULL) { 5663 /* 5664 * mp freed by tcp_get_ipsec_conn. 5665 */ 5666 return; 5667 } 5668 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5669 } else { 5670 goto error2; 5671 } 5672 5673 ASSERT(DB_TYPE(mp) == M_DATA); 5674 5675 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5676 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5677 ASSERT(OK_32PTR(mp->b_rptr)); 5678 if (ipvers == IPV4_VERSION) { 5679 ipha = (ipha_t *)mp->b_rptr; 5680 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5681 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5682 } else { 5683 ip6h = (ip6_t *)mp->b_rptr; 5684 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5685 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5686 } 5687 5688 if (tcp->tcp_family == AF_INET) { 5689 ASSERT(ipvers == IPV4_VERSION); 5690 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5691 } else { 5692 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5693 } 5694 5695 if (err) 5696 goto error3; 5697 5698 eager = econnp->conn_tcp; 5699 5700 /* Inherit various TCP parameters from the listener */ 5701 eager->tcp_naglim = tcp->tcp_naglim; 5702 eager->tcp_first_timer_threshold = 5703 tcp->tcp_first_timer_threshold; 5704 eager->tcp_second_timer_threshold = 5705 tcp->tcp_second_timer_threshold; 5706 5707 eager->tcp_first_ctimer_threshold = 5708 tcp->tcp_first_ctimer_threshold; 5709 eager->tcp_second_ctimer_threshold = 5710 tcp->tcp_second_ctimer_threshold; 5711 5712 /* 5713 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5714 * If it does not, the eager's receive window will be set to the 5715 * listener's receive window later in this function. 5716 */ 5717 eager->tcp_rwnd = 0; 5718 5719 /* 5720 * Inherit listener's tcp_init_cwnd. Need to do this before 5721 * calling tcp_process_options() where tcp_mss_set() is called 5722 * to set the initial cwnd. 5723 */ 5724 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5725 5726 /* 5727 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5728 * zone id before the accept is completed in tcp_wput_accept(). 5729 */ 5730 econnp->conn_zoneid = connp->conn_zoneid; 5731 econnp->conn_allzones = connp->conn_allzones; 5732 5733 /* Copy nexthop information from listener to eager */ 5734 if (connp->conn_nexthop_set) { 5735 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5736 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5737 } 5738 5739 /* 5740 * TSOL: tsol_input_proc() needs the eager's cred before the 5741 * eager is accepted 5742 */ 5743 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5744 crhold(credp); 5745 5746 /* 5747 * If the caller has the process-wide flag set, then default to MAC 5748 * exempt mode. This allows read-down to unlabeled hosts. 5749 */ 5750 if (getpflags(NET_MAC_AWARE, credp) != 0) 5751 econnp->conn_mac_exempt = B_TRUE; 5752 5753 if (is_system_labeled()) { 5754 cred_t *cr; 5755 5756 if (connp->conn_mlp_type != mlptSingle) { 5757 cr = econnp->conn_peercred = DB_CRED(mp); 5758 if (cr != NULL) 5759 crhold(cr); 5760 else 5761 cr = econnp->conn_cred; 5762 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5763 econnp, cred_t *, cr) 5764 } else { 5765 cr = econnp->conn_cred; 5766 DTRACE_PROBE2(syn_accept, conn_t *, 5767 econnp, cred_t *, cr) 5768 } 5769 5770 if (!tcp_update_label(eager, cr)) { 5771 DTRACE_PROBE3( 5772 tx__ip__log__error__connrequest__tcp, 5773 char *, "eager connp(1) label on SYN mp(2) failed", 5774 conn_t *, econnp, mblk_t *, mp); 5775 goto error3; 5776 } 5777 } 5778 5779 eager->tcp_hard_binding = B_TRUE; 5780 5781 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5782 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5783 5784 CL_INET_CONNECT(eager); 5785 5786 /* 5787 * No need to check for multicast destination since ip will only pass 5788 * up multicasts to those that have expressed interest 5789 * TODO: what about rejecting broadcasts? 5790 * Also check that source is not a multicast or broadcast address. 5791 */ 5792 eager->tcp_state = TCPS_SYN_RCVD; 5793 5794 5795 /* 5796 * There should be no ire in the mp as we are being called after 5797 * receiving the SYN. 5798 */ 5799 ASSERT(tcp_ire_mp(mp) == NULL); 5800 5801 /* 5802 * Adapt our mss, ttl, ... according to information provided in IRE. 5803 */ 5804 5805 if (tcp_adapt_ire(eager, NULL) == 0) { 5806 /* Undo the bind_hash_insert */ 5807 tcp_bind_hash_remove(eager); 5808 goto error3; 5809 } 5810 5811 /* Process all TCP options. */ 5812 tcp_process_options(eager, tcph); 5813 5814 /* Is the other end ECN capable? */ 5815 if (tcps->tcps_ecn_permitted >= 1 && 5816 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5817 eager->tcp_ecn_ok = B_TRUE; 5818 } 5819 5820 /* 5821 * listener->tcp_rq->q_hiwat should be the default window size or a 5822 * window size changed via SO_RCVBUF option. First round up the 5823 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5824 * scale option value if needed. Call tcp_rwnd_set() to finish the 5825 * setting. 5826 * 5827 * Note if there is a rpipe metric associated with the remote host, 5828 * we should not inherit receive window size from listener. 5829 */ 5830 eager->tcp_rwnd = MSS_ROUNDUP( 5831 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5832 eager->tcp_rwnd), eager->tcp_mss); 5833 if (eager->tcp_snd_ws_ok) 5834 tcp_set_ws_value(eager); 5835 /* 5836 * Note that this is the only place tcp_rwnd_set() is called for 5837 * accepting a connection. We need to call it here instead of 5838 * after the 3-way handshake because we need to tell the other 5839 * side our rwnd in the SYN-ACK segment. 5840 */ 5841 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5842 5843 /* 5844 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5845 * via soaccept()->soinheritoptions() which essentially applies 5846 * all the listener options to the new STREAM. The options that we 5847 * need to take care of are: 5848 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5849 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5850 * SO_SNDBUF, SO_RCVBUF. 5851 * 5852 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5853 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5854 * tcp_maxpsz_set() gets called later from 5855 * tcp_accept_finish(), the option takes effect. 5856 * 5857 */ 5858 /* Set the TCP options */ 5859 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5860 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5861 eager->tcp_oobinline = tcp->tcp_oobinline; 5862 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5863 eager->tcp_broadcast = tcp->tcp_broadcast; 5864 eager->tcp_useloopback = tcp->tcp_useloopback; 5865 eager->tcp_dontroute = tcp->tcp_dontroute; 5866 eager->tcp_linger = tcp->tcp_linger; 5867 eager->tcp_lingertime = tcp->tcp_lingertime; 5868 if (tcp->tcp_ka_enabled) 5869 eager->tcp_ka_enabled = 1; 5870 5871 /* Set the IP options */ 5872 econnp->conn_broadcast = connp->conn_broadcast; 5873 econnp->conn_loopback = connp->conn_loopback; 5874 econnp->conn_dontroute = connp->conn_dontroute; 5875 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5876 5877 /* Put a ref on the listener for the eager. */ 5878 CONN_INC_REF(connp); 5879 mutex_enter(&tcp->tcp_eager_lock); 5880 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5881 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5882 tcp->tcp_eager_next_q0 = eager; 5883 eager->tcp_eager_prev_q0 = tcp; 5884 5885 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5886 eager->tcp_listener = tcp; 5887 eager->tcp_saved_listener = tcp; 5888 5889 /* 5890 * Tag this detached tcp vector for later retrieval 5891 * by our listener client in tcp_accept(). 5892 */ 5893 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5894 tcp->tcp_conn_req_cnt_q0++; 5895 if (++tcp->tcp_conn_req_seqnum == -1) { 5896 /* 5897 * -1 is "special" and defined in TPI as something 5898 * that should never be used in T_CONN_IND 5899 */ 5900 ++tcp->tcp_conn_req_seqnum; 5901 } 5902 mutex_exit(&tcp->tcp_eager_lock); 5903 5904 if (tcp->tcp_syn_defense) { 5905 /* Don't drop the SYN that comes from a good IP source */ 5906 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5907 if (addr_cache != NULL && eager->tcp_remote == 5908 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5909 eager->tcp_dontdrop = B_TRUE; 5910 } 5911 } 5912 5913 /* 5914 * We need to insert the eager in its own perimeter but as soon 5915 * as we do that, we expose the eager to the classifier and 5916 * should not touch any field outside the eager's perimeter. 5917 * So do all the work necessary before inserting the eager 5918 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5919 * will succeed but undo everything if it fails. 5920 */ 5921 seg_seq = ABE32_TO_U32(tcph->th_seq); 5922 eager->tcp_irs = seg_seq; 5923 eager->tcp_rack = seg_seq; 5924 eager->tcp_rnxt = seg_seq + 1; 5925 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5926 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5927 eager->tcp_state = TCPS_SYN_RCVD; 5928 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5929 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5930 if (mp1 == NULL) { 5931 /* 5932 * Increment the ref count as we are going to 5933 * enqueueing an mp in squeue 5934 */ 5935 CONN_INC_REF(econnp); 5936 goto error; 5937 } 5938 DB_CPID(mp1) = tcp->tcp_cpid; 5939 eager->tcp_cpid = tcp->tcp_cpid; 5940 eager->tcp_open_time = lbolt64; 5941 5942 /* 5943 * We need to start the rto timer. In normal case, we start 5944 * the timer after sending the packet on the wire (or at 5945 * least believing that packet was sent by waiting for 5946 * CALL_IP_WPUT() to return). Since this is the first packet 5947 * being sent on the wire for the eager, our initial tcp_rto 5948 * is at least tcp_rexmit_interval_min which is a fairly 5949 * large value to allow the algorithm to adjust slowly to large 5950 * fluctuations of RTT during first few transmissions. 5951 * 5952 * Starting the timer first and then sending the packet in this 5953 * case shouldn't make much difference since tcp_rexmit_interval_min 5954 * is of the order of several 100ms and starting the timer 5955 * first and then sending the packet will result in difference 5956 * of few micro seconds. 5957 * 5958 * Without this optimization, we are forced to hold the fanout 5959 * lock across the ipcl_bind_insert() and sending the packet 5960 * so that we don't race against an incoming packet (maybe RST) 5961 * for this eager. 5962 * 5963 * It is necessary to acquire an extra reference on the eager 5964 * at this point and hold it until after tcp_send_data() to 5965 * ensure against an eager close race. 5966 */ 5967 5968 CONN_INC_REF(eager->tcp_connp); 5969 5970 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5971 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5972 5973 5974 /* 5975 * Insert the eager in its own perimeter now. We are ready to deal 5976 * with any packets on eager. 5977 */ 5978 if (eager->tcp_ipversion == IPV4_VERSION) { 5979 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5980 goto error; 5981 } 5982 } else { 5983 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5984 goto error; 5985 } 5986 } 5987 5988 /* mark conn as fully-bound */ 5989 econnp->conn_fully_bound = B_TRUE; 5990 5991 /* Send the SYN-ACK */ 5992 tcp_send_data(eager, eager->tcp_wq, mp1); 5993 CONN_DEC_REF(eager->tcp_connp); 5994 freemsg(mp); 5995 5996 return; 5997 error: 5998 freemsg(mp1); 5999 eager->tcp_closemp_used = B_TRUE; 6000 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6001 squeue_fill(econnp->conn_sqp, &eager->tcp_closemp, tcp_eager_kill, 6002 econnp, SQTAG_TCP_CONN_REQ_2); 6003 6004 /* 6005 * If a connection already exists, send the mp to that connections so 6006 * that it can be appropriately dealt with. 6007 */ 6008 ipst = tcps->tcps_netstack->netstack_ip; 6009 6010 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 6011 if (!IPCL_IS_CONNECTED(econnp)) { 6012 /* 6013 * Something bad happened. ipcl_conn_insert() 6014 * failed because a connection already existed 6015 * in connected hash but we can't find it 6016 * anymore (someone blew it away). Just 6017 * free this message and hopefully remote 6018 * will retransmit at which time the SYN can be 6019 * treated as a new connection or dealth with 6020 * a TH_RST if a connection already exists. 6021 */ 6022 CONN_DEC_REF(econnp); 6023 freemsg(mp); 6024 } else { 6025 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6026 econnp, SQTAG_TCP_CONN_REQ_1); 6027 } 6028 } else { 6029 /* Nobody wants this packet */ 6030 freemsg(mp); 6031 } 6032 return; 6033 error3: 6034 CONN_DEC_REF(econnp); 6035 error2: 6036 freemsg(mp); 6037 } 6038 6039 /* 6040 * In an ideal case of vertical partition in NUMA architecture, its 6041 * beneficial to have the listener and all the incoming connections 6042 * tied to the same squeue. The other constraint is that incoming 6043 * connections should be tied to the squeue attached to interrupted 6044 * CPU for obvious locality reason so this leaves the listener to 6045 * be tied to the same squeue. Our only problem is that when listener 6046 * is binding, the CPU that will get interrupted by the NIC whose 6047 * IP address the listener is binding to is not even known. So 6048 * the code below allows us to change that binding at the time the 6049 * CPU is interrupted by virtue of incoming connection's squeue. 6050 * 6051 * This is usefull only in case of a listener bound to a specific IP 6052 * address. For other kind of listeners, they get bound the 6053 * very first time and there is no attempt to rebind them. 6054 */ 6055 void 6056 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6057 { 6058 conn_t *connp = (conn_t *)arg; 6059 squeue_t *sqp = (squeue_t *)arg2; 6060 squeue_t *new_sqp; 6061 uint32_t conn_flags; 6062 6063 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6064 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6065 } else { 6066 goto done; 6067 } 6068 6069 if (connp->conn_fanout == NULL) 6070 goto done; 6071 6072 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6073 mutex_enter(&connp->conn_fanout->connf_lock); 6074 mutex_enter(&connp->conn_lock); 6075 /* 6076 * No one from read or write side can access us now 6077 * except for already queued packets on this squeue. 6078 * But since we haven't changed the squeue yet, they 6079 * can't execute. If they are processed after we have 6080 * changed the squeue, they are sent back to the 6081 * correct squeue down below. 6082 * But a listner close can race with processing of 6083 * incoming SYN. If incoming SYN processing changes 6084 * the squeue then the listener close which is waiting 6085 * to enter the squeue would operate on the wrong 6086 * squeue. Hence we don't change the squeue here unless 6087 * the refcount is exactly the minimum refcount. The 6088 * minimum refcount of 4 is counted as - 1 each for 6089 * TCP and IP, 1 for being in the classifier hash, and 6090 * 1 for the mblk being processed. 6091 */ 6092 6093 if (connp->conn_ref != 4 || 6094 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6095 mutex_exit(&connp->conn_lock); 6096 mutex_exit(&connp->conn_fanout->connf_lock); 6097 goto done; 6098 } 6099 if (connp->conn_sqp != new_sqp) { 6100 while (connp->conn_sqp != new_sqp) 6101 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6102 } 6103 6104 do { 6105 conn_flags = connp->conn_flags; 6106 conn_flags |= IPCL_FULLY_BOUND; 6107 (void) cas32(&connp->conn_flags, connp->conn_flags, 6108 conn_flags); 6109 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6110 6111 mutex_exit(&connp->conn_fanout->connf_lock); 6112 mutex_exit(&connp->conn_lock); 6113 } 6114 6115 done: 6116 if (connp->conn_sqp != sqp) { 6117 CONN_INC_REF(connp); 6118 squeue_fill(connp->conn_sqp, mp, 6119 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6120 } else { 6121 tcp_conn_request(connp, mp, sqp); 6122 } 6123 } 6124 6125 /* 6126 * Successful connect request processing begins when our client passes 6127 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6128 * our T_OK_ACK reply message upstream. The control flow looks like this: 6129 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6130 * upstream <- tcp_rput() <- IP 6131 * After various error checks are completed, tcp_connect() lays 6132 * the target address and port into the composite header template, 6133 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6134 * request followed by an IRE request, and passes the three mblk message 6135 * down to IP looking like this: 6136 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6137 * Processing continues in tcp_rput() when we receive the following message: 6138 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6139 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6140 * to fire off the connection request, and then passes the T_OK_ACK mblk 6141 * upstream that we filled in below. There are, of course, numerous 6142 * error conditions along the way which truncate the processing described 6143 * above. 6144 */ 6145 static void 6146 tcp_connect(tcp_t *tcp, mblk_t *mp) 6147 { 6148 sin_t *sin; 6149 sin6_t *sin6; 6150 queue_t *q = tcp->tcp_wq; 6151 struct T_conn_req *tcr; 6152 ipaddr_t *dstaddrp; 6153 in_port_t dstport; 6154 uint_t srcid; 6155 6156 tcr = (struct T_conn_req *)mp->b_rptr; 6157 6158 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6159 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6160 tcp_err_ack(tcp, mp, TPROTO, 0); 6161 return; 6162 } 6163 6164 /* 6165 * Determine packet type based on type of address passed in 6166 * the request should contain an IPv4 or IPv6 address. 6167 * Make sure that address family matches the type of 6168 * family of the the address passed down 6169 */ 6170 switch (tcr->DEST_length) { 6171 default: 6172 tcp_err_ack(tcp, mp, TBADADDR, 0); 6173 return; 6174 6175 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6176 /* 6177 * XXX: The check for valid DEST_length was not there 6178 * in earlier releases and some buggy 6179 * TLI apps (e.g Sybase) got away with not feeding 6180 * in sin_zero part of address. 6181 * We allow that bug to keep those buggy apps humming. 6182 * Test suites require the check on DEST_length. 6183 * We construct a new mblk with valid DEST_length 6184 * free the original so the rest of the code does 6185 * not have to keep track of this special shorter 6186 * length address case. 6187 */ 6188 mblk_t *nmp; 6189 struct T_conn_req *ntcr; 6190 sin_t *nsin; 6191 6192 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6193 tcr->OPT_length, BPRI_HI); 6194 if (nmp == NULL) { 6195 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6196 return; 6197 } 6198 ntcr = (struct T_conn_req *)nmp->b_rptr; 6199 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6200 ntcr->PRIM_type = T_CONN_REQ; 6201 ntcr->DEST_length = sizeof (sin_t); 6202 ntcr->DEST_offset = sizeof (struct T_conn_req); 6203 6204 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6205 *nsin = sin_null; 6206 /* Get pointer to shorter address to copy from original mp */ 6207 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6208 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6209 if (sin == NULL || !OK_32PTR((char *)sin)) { 6210 freemsg(nmp); 6211 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6212 return; 6213 } 6214 nsin->sin_family = sin->sin_family; 6215 nsin->sin_port = sin->sin_port; 6216 nsin->sin_addr = sin->sin_addr; 6217 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6218 nmp->b_wptr = (uchar_t *)&nsin[1]; 6219 if (tcr->OPT_length != 0) { 6220 ntcr->OPT_length = tcr->OPT_length; 6221 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6222 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6223 (uchar_t *)ntcr + ntcr->OPT_offset, 6224 tcr->OPT_length); 6225 nmp->b_wptr += tcr->OPT_length; 6226 } 6227 freemsg(mp); /* original mp freed */ 6228 mp = nmp; /* re-initialize original variables */ 6229 tcr = ntcr; 6230 } 6231 /* FALLTHRU */ 6232 6233 case sizeof (sin_t): 6234 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6235 sizeof (sin_t)); 6236 if (sin == NULL || !OK_32PTR((char *)sin)) { 6237 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6238 return; 6239 } 6240 if (tcp->tcp_family != AF_INET || 6241 sin->sin_family != AF_INET) { 6242 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6243 return; 6244 } 6245 if (sin->sin_port == 0) { 6246 tcp_err_ack(tcp, mp, TBADADDR, 0); 6247 return; 6248 } 6249 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6250 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6251 return; 6252 } 6253 6254 break; 6255 6256 case sizeof (sin6_t): 6257 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6258 sizeof (sin6_t)); 6259 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6260 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6261 return; 6262 } 6263 if (tcp->tcp_family != AF_INET6 || 6264 sin6->sin6_family != AF_INET6) { 6265 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6266 return; 6267 } 6268 if (sin6->sin6_port == 0) { 6269 tcp_err_ack(tcp, mp, TBADADDR, 0); 6270 return; 6271 } 6272 break; 6273 } 6274 /* 6275 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6276 * should key on their sequence number and cut them loose. 6277 */ 6278 6279 /* 6280 * If options passed in, feed it for verification and handling 6281 */ 6282 if (tcr->OPT_length != 0) { 6283 mblk_t *ok_mp; 6284 mblk_t *discon_mp; 6285 mblk_t *conn_opts_mp; 6286 int t_error, sys_error, do_disconnect; 6287 6288 conn_opts_mp = NULL; 6289 6290 if (tcp_conprim_opt_process(tcp, mp, 6291 &do_disconnect, &t_error, &sys_error) < 0) { 6292 if (do_disconnect) { 6293 ASSERT(t_error == 0 && sys_error == 0); 6294 discon_mp = mi_tpi_discon_ind(NULL, 6295 ECONNREFUSED, 0); 6296 if (!discon_mp) { 6297 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6298 TSYSERR, ENOMEM); 6299 return; 6300 } 6301 ok_mp = mi_tpi_ok_ack_alloc(mp); 6302 if (!ok_mp) { 6303 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6304 TSYSERR, ENOMEM); 6305 return; 6306 } 6307 qreply(q, ok_mp); 6308 qreply(q, discon_mp); /* no flush! */ 6309 } else { 6310 ASSERT(t_error != 0); 6311 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6312 sys_error); 6313 } 6314 return; 6315 } 6316 /* 6317 * Success in setting options, the mp option buffer represented 6318 * by OPT_length/offset has been potentially modified and 6319 * contains results of option processing. We copy it in 6320 * another mp to save it for potentially influencing returning 6321 * it in T_CONN_CONN. 6322 */ 6323 if (tcr->OPT_length != 0) { /* there are resulting options */ 6324 conn_opts_mp = copyb(mp); 6325 if (!conn_opts_mp) { 6326 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6327 TSYSERR, ENOMEM); 6328 return; 6329 } 6330 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6331 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6332 /* 6333 * Note: 6334 * These resulting option negotiation can include any 6335 * end-to-end negotiation options but there no such 6336 * thing (yet?) in our TCP/IP. 6337 */ 6338 } 6339 } 6340 6341 /* 6342 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6343 * make sure that the template IP header in the tcp structure is an 6344 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6345 * need to this before we call tcp_bindi() so that the port lookup 6346 * code will look for ports in the correct port space (IPv4 and 6347 * IPv6 have separate port spaces). 6348 */ 6349 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6350 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6351 int err = 0; 6352 6353 err = tcp_header_init_ipv4(tcp); 6354 if (err != 0) { 6355 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6356 goto connect_failed; 6357 } 6358 if (tcp->tcp_lport != 0) 6359 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6360 } 6361 6362 switch (tcp->tcp_state) { 6363 case TCPS_IDLE: 6364 /* 6365 * We support quick connect, refer to comments in 6366 * tcp_connect_*() 6367 */ 6368 /* FALLTHRU */ 6369 case TCPS_BOUND: 6370 case TCPS_LISTEN: 6371 if (tcp->tcp_family == AF_INET6) { 6372 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6373 tcp_connect_ipv6(tcp, mp, 6374 &sin6->sin6_addr, 6375 sin6->sin6_port, sin6->sin6_flowinfo, 6376 sin6->__sin6_src_id, sin6->sin6_scope_id); 6377 return; 6378 } 6379 /* 6380 * Destination adress is mapped IPv6 address. 6381 * Source bound address should be unspecified or 6382 * IPv6 mapped address as well. 6383 */ 6384 if (!IN6_IS_ADDR_UNSPECIFIED( 6385 &tcp->tcp_bound_source_v6) && 6386 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6387 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6388 EADDRNOTAVAIL); 6389 break; 6390 } 6391 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6392 dstport = sin6->sin6_port; 6393 srcid = sin6->__sin6_src_id; 6394 } else { 6395 dstaddrp = &sin->sin_addr.s_addr; 6396 dstport = sin->sin_port; 6397 srcid = 0; 6398 } 6399 6400 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6401 return; 6402 default: 6403 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6404 break; 6405 } 6406 /* 6407 * Note: Code below is the "failure" case 6408 */ 6409 /* return error ack and blow away saved option results if any */ 6410 connect_failed: 6411 if (mp != NULL) 6412 putnext(tcp->tcp_rq, mp); 6413 else { 6414 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6415 TSYSERR, ENOMEM); 6416 } 6417 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6418 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6419 } 6420 6421 /* 6422 * Handle connect to IPv4 destinations, including connections for AF_INET6 6423 * sockets connecting to IPv4 mapped IPv6 destinations. 6424 */ 6425 static void 6426 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6427 uint_t srcid) 6428 { 6429 tcph_t *tcph; 6430 mblk_t *mp1; 6431 ipaddr_t dstaddr = *dstaddrp; 6432 int32_t oldstate; 6433 uint16_t lport; 6434 tcp_stack_t *tcps = tcp->tcp_tcps; 6435 6436 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6437 6438 /* Check for attempt to connect to INADDR_ANY */ 6439 if (dstaddr == INADDR_ANY) { 6440 /* 6441 * SunOS 4.x and 4.3 BSD allow an application 6442 * to connect a TCP socket to INADDR_ANY. 6443 * When they do this, the kernel picks the 6444 * address of one interface and uses it 6445 * instead. The kernel usually ends up 6446 * picking the address of the loopback 6447 * interface. This is an undocumented feature. 6448 * However, we provide the same thing here 6449 * in order to have source and binary 6450 * compatibility with SunOS 4.x. 6451 * Update the T_CONN_REQ (sin/sin6) since it is used to 6452 * generate the T_CONN_CON. 6453 */ 6454 dstaddr = htonl(INADDR_LOOPBACK); 6455 *dstaddrp = dstaddr; 6456 } 6457 6458 /* Handle __sin6_src_id if socket not bound to an IP address */ 6459 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6460 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6461 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6462 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6463 tcp->tcp_ipha->ipha_src); 6464 } 6465 6466 /* 6467 * Don't let an endpoint connect to itself. Note that 6468 * the test here does not catch the case where the 6469 * source IP addr was left unspecified by the user. In 6470 * this case, the source addr is set in tcp_adapt_ire() 6471 * using the reply to the T_BIND message that we send 6472 * down to IP here and the check is repeated in tcp_rput_other. 6473 */ 6474 if (dstaddr == tcp->tcp_ipha->ipha_src && 6475 dstport == tcp->tcp_lport) { 6476 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6477 goto failed; 6478 } 6479 6480 tcp->tcp_ipha->ipha_dst = dstaddr; 6481 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6482 6483 /* 6484 * Massage a source route if any putting the first hop 6485 * in iph_dst. Compute a starting value for the checksum which 6486 * takes into account that the original iph_dst should be 6487 * included in the checksum but that ip will include the 6488 * first hop in the source route in the tcp checksum. 6489 */ 6490 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6491 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6492 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6493 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6494 if ((int)tcp->tcp_sum < 0) 6495 tcp->tcp_sum--; 6496 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6497 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6498 (tcp->tcp_sum >> 16)); 6499 tcph = tcp->tcp_tcph; 6500 *(uint16_t *)tcph->th_fport = dstport; 6501 tcp->tcp_fport = dstport; 6502 6503 oldstate = tcp->tcp_state; 6504 /* 6505 * At this point the remote destination address and remote port fields 6506 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6507 * have to see which state tcp was in so we can take apropriate action. 6508 */ 6509 if (oldstate == TCPS_IDLE) { 6510 /* 6511 * We support a quick connect capability here, allowing 6512 * clients to transition directly from IDLE to SYN_SENT 6513 * tcp_bindi will pick an unused port, insert the connection 6514 * in the bind hash and transition to BOUND state. 6515 */ 6516 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6517 tcp, B_TRUE); 6518 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6519 B_FALSE, B_FALSE); 6520 if (lport == 0) { 6521 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6522 goto failed; 6523 } 6524 } 6525 tcp->tcp_state = TCPS_SYN_SENT; 6526 6527 /* 6528 * TODO: allow data with connect requests 6529 * by unlinking M_DATA trailers here and 6530 * linking them in behind the T_OK_ACK mblk. 6531 * The tcp_rput() bind ack handler would then 6532 * feed them to tcp_wput_data() rather than call 6533 * tcp_timer(). 6534 */ 6535 mp = mi_tpi_ok_ack_alloc(mp); 6536 if (!mp) { 6537 tcp->tcp_state = oldstate; 6538 goto failed; 6539 } 6540 if (tcp->tcp_family == AF_INET) { 6541 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6542 sizeof (ipa_conn_t)); 6543 } else { 6544 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6545 sizeof (ipa6_conn_t)); 6546 } 6547 if (mp1) { 6548 /* Hang onto the T_OK_ACK for later. */ 6549 linkb(mp1, mp); 6550 mblk_setcred(mp1, tcp->tcp_cred); 6551 if (tcp->tcp_family == AF_INET) 6552 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6553 else { 6554 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6555 &tcp->tcp_sticky_ipp); 6556 } 6557 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6558 tcp->tcp_active_open = 1; 6559 /* 6560 * If the bind cannot complete immediately 6561 * IP will arrange to call tcp_rput_other 6562 * when the bind completes. 6563 */ 6564 if (mp1 != NULL) 6565 tcp_rput_other(tcp, mp1); 6566 return; 6567 } 6568 /* Error case */ 6569 tcp->tcp_state = oldstate; 6570 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6571 6572 failed: 6573 /* return error ack and blow away saved option results if any */ 6574 if (mp != NULL) 6575 putnext(tcp->tcp_rq, mp); 6576 else { 6577 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6578 TSYSERR, ENOMEM); 6579 } 6580 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6581 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6582 6583 } 6584 6585 /* 6586 * Handle connect to IPv6 destinations. 6587 */ 6588 static void 6589 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6590 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6591 { 6592 tcph_t *tcph; 6593 mblk_t *mp1; 6594 ip6_rthdr_t *rth; 6595 int32_t oldstate; 6596 uint16_t lport; 6597 tcp_stack_t *tcps = tcp->tcp_tcps; 6598 6599 ASSERT(tcp->tcp_family == AF_INET6); 6600 6601 /* 6602 * If we're here, it means that the destination address is a native 6603 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6604 * reason why it might not be IPv6 is if the socket was bound to an 6605 * IPv4-mapped IPv6 address. 6606 */ 6607 if (tcp->tcp_ipversion != IPV6_VERSION) { 6608 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6609 goto failed; 6610 } 6611 6612 /* 6613 * Interpret a zero destination to mean loopback. 6614 * Update the T_CONN_REQ (sin/sin6) since it is used to 6615 * generate the T_CONN_CON. 6616 */ 6617 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6618 *dstaddrp = ipv6_loopback; 6619 } 6620 6621 /* Handle __sin6_src_id if socket not bound to an IP address */ 6622 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6623 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6624 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6625 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6626 } 6627 6628 /* 6629 * Take care of the scope_id now and add ip6i_t 6630 * if ip6i_t is not already allocated through TCP 6631 * sticky options. At this point tcp_ip6h does not 6632 * have dst info, thus use dstaddrp. 6633 */ 6634 if (scope_id != 0 && 6635 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6636 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6637 ip6i_t *ip6i; 6638 6639 ipp->ipp_ifindex = scope_id; 6640 ip6i = (ip6i_t *)tcp->tcp_iphc; 6641 6642 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6643 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6644 /* Already allocated */ 6645 ip6i->ip6i_flags |= IP6I_IFINDEX; 6646 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6647 ipp->ipp_fields |= IPPF_SCOPE_ID; 6648 } else { 6649 int reterr; 6650 6651 ipp->ipp_fields |= IPPF_SCOPE_ID; 6652 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6653 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6654 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6655 if (reterr != 0) 6656 goto failed; 6657 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6658 } 6659 } 6660 6661 /* 6662 * Don't let an endpoint connect to itself. Note that 6663 * the test here does not catch the case where the 6664 * source IP addr was left unspecified by the user. In 6665 * this case, the source addr is set in tcp_adapt_ire() 6666 * using the reply to the T_BIND message that we send 6667 * down to IP here and the check is repeated in tcp_rput_other. 6668 */ 6669 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6670 (dstport == tcp->tcp_lport)) { 6671 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6672 goto failed; 6673 } 6674 6675 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6676 tcp->tcp_remote_v6 = *dstaddrp; 6677 tcp->tcp_ip6h->ip6_vcf = 6678 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6679 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6680 6681 6682 /* 6683 * Massage a routing header (if present) putting the first hop 6684 * in ip6_dst. Compute a starting value for the checksum which 6685 * takes into account that the original ip6_dst should be 6686 * included in the checksum but that ip will include the 6687 * first hop in the source route in the tcp checksum. 6688 */ 6689 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6690 if (rth != NULL) { 6691 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6692 tcps->tcps_netstack); 6693 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6694 (tcp->tcp_sum >> 16)); 6695 } else { 6696 tcp->tcp_sum = 0; 6697 } 6698 6699 tcph = tcp->tcp_tcph; 6700 *(uint16_t *)tcph->th_fport = dstport; 6701 tcp->tcp_fport = dstport; 6702 6703 oldstate = tcp->tcp_state; 6704 /* 6705 * At this point the remote destination address and remote port fields 6706 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6707 * have to see which state tcp was in so we can take apropriate action. 6708 */ 6709 if (oldstate == TCPS_IDLE) { 6710 /* 6711 * We support a quick connect capability here, allowing 6712 * clients to transition directly from IDLE to SYN_SENT 6713 * tcp_bindi will pick an unused port, insert the connection 6714 * in the bind hash and transition to BOUND state. 6715 */ 6716 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6717 tcp, B_TRUE); 6718 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6719 B_FALSE, B_FALSE); 6720 if (lport == 0) { 6721 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6722 goto failed; 6723 } 6724 } 6725 tcp->tcp_state = TCPS_SYN_SENT; 6726 /* 6727 * TODO: allow data with connect requests 6728 * by unlinking M_DATA trailers here and 6729 * linking them in behind the T_OK_ACK mblk. 6730 * The tcp_rput() bind ack handler would then 6731 * feed them to tcp_wput_data() rather than call 6732 * tcp_timer(). 6733 */ 6734 mp = mi_tpi_ok_ack_alloc(mp); 6735 if (!mp) { 6736 tcp->tcp_state = oldstate; 6737 goto failed; 6738 } 6739 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6740 if (mp1) { 6741 /* Hang onto the T_OK_ACK for later. */ 6742 linkb(mp1, mp); 6743 mblk_setcred(mp1, tcp->tcp_cred); 6744 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6745 &tcp->tcp_sticky_ipp); 6746 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6747 tcp->tcp_active_open = 1; 6748 /* ip_bind_v6() may return ACK or ERROR */ 6749 if (mp1 != NULL) 6750 tcp_rput_other(tcp, mp1); 6751 return; 6752 } 6753 /* Error case */ 6754 tcp->tcp_state = oldstate; 6755 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6756 6757 failed: 6758 /* return error ack and blow away saved option results if any */ 6759 if (mp != NULL) 6760 putnext(tcp->tcp_rq, mp); 6761 else { 6762 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6763 TSYSERR, ENOMEM); 6764 } 6765 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6766 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6767 } 6768 6769 /* 6770 * We need a stream q for detached closing tcp connections 6771 * to use. Our client hereby indicates that this q is the 6772 * one to use. 6773 */ 6774 static void 6775 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6776 { 6777 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6778 queue_t *q = tcp->tcp_wq; 6779 tcp_stack_t *tcps = tcp->tcp_tcps; 6780 6781 #ifdef NS_DEBUG 6782 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6783 tcps->tcps_netstack->netstack_stackid); 6784 #endif 6785 mp->b_datap->db_type = M_IOCACK; 6786 iocp->ioc_count = 0; 6787 mutex_enter(&tcps->tcps_g_q_lock); 6788 if (tcps->tcps_g_q != NULL) { 6789 mutex_exit(&tcps->tcps_g_q_lock); 6790 iocp->ioc_error = EALREADY; 6791 } else { 6792 mblk_t *mp1; 6793 6794 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6795 if (mp1 == NULL) { 6796 mutex_exit(&tcps->tcps_g_q_lock); 6797 iocp->ioc_error = ENOMEM; 6798 } else { 6799 tcps->tcps_g_q = tcp->tcp_rq; 6800 mutex_exit(&tcps->tcps_g_q_lock); 6801 iocp->ioc_error = 0; 6802 iocp->ioc_rval = 0; 6803 /* 6804 * We are passing tcp_sticky_ipp as NULL 6805 * as it is not useful for tcp_default queue 6806 */ 6807 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6808 if (mp1 != NULL) 6809 tcp_rput_other(tcp, mp1); 6810 } 6811 } 6812 qreply(q, mp); 6813 } 6814 6815 /* 6816 * Our client hereby directs us to reject the connection request 6817 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6818 * of sending the appropriate RST, not an ICMP error. 6819 */ 6820 static void 6821 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6822 { 6823 tcp_t *ltcp = NULL; 6824 t_scalar_t seqnum; 6825 conn_t *connp; 6826 tcp_stack_t *tcps = tcp->tcp_tcps; 6827 6828 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6829 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6830 tcp_err_ack(tcp, mp, TPROTO, 0); 6831 return; 6832 } 6833 6834 /* 6835 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6836 * when the stream is in BOUND state. Do not send a reset, 6837 * since the destination IP address is not valid, and it can 6838 * be the initialized value of all zeros (broadcast address). 6839 * 6840 * If TCP has sent down a bind request to IP and has not 6841 * received the reply, reject the request. Otherwise, TCP 6842 * will be confused. 6843 */ 6844 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6845 if (tcp->tcp_debug) { 6846 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6847 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6848 } 6849 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6850 return; 6851 } 6852 6853 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6854 6855 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6856 6857 /* 6858 * According to TPI, for non-listeners, ignore seqnum 6859 * and disconnect. 6860 * Following interpretation of -1 seqnum is historical 6861 * and implied TPI ? (TPI only states that for T_CONN_IND, 6862 * a valid seqnum should not be -1). 6863 * 6864 * -1 means disconnect everything 6865 * regardless even on a listener. 6866 */ 6867 6868 int old_state = tcp->tcp_state; 6869 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6870 6871 /* 6872 * The connection can't be on the tcp_time_wait_head list 6873 * since it is not detached. 6874 */ 6875 ASSERT(tcp->tcp_time_wait_next == NULL); 6876 ASSERT(tcp->tcp_time_wait_prev == NULL); 6877 ASSERT(tcp->tcp_time_wait_expire == 0); 6878 ltcp = NULL; 6879 /* 6880 * If it used to be a listener, check to make sure no one else 6881 * has taken the port before switching back to LISTEN state. 6882 */ 6883 if (tcp->tcp_ipversion == IPV4_VERSION) { 6884 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6885 tcp->tcp_ipha->ipha_src, 6886 tcp->tcp_connp->conn_zoneid, ipst); 6887 if (connp != NULL) 6888 ltcp = connp->conn_tcp; 6889 } else { 6890 /* Allow tcp_bound_if listeners? */ 6891 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6892 &tcp->tcp_ip6h->ip6_src, 0, 6893 tcp->tcp_connp->conn_zoneid, ipst); 6894 if (connp != NULL) 6895 ltcp = connp->conn_tcp; 6896 } 6897 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6898 tcp->tcp_state = TCPS_LISTEN; 6899 } else if (old_state > TCPS_BOUND) { 6900 tcp->tcp_conn_req_max = 0; 6901 tcp->tcp_state = TCPS_BOUND; 6902 } 6903 if (ltcp != NULL) 6904 CONN_DEC_REF(ltcp->tcp_connp); 6905 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6906 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6907 } else if (old_state == TCPS_ESTABLISHED || 6908 old_state == TCPS_CLOSE_WAIT) { 6909 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6910 } 6911 6912 if (tcp->tcp_fused) 6913 tcp_unfuse(tcp); 6914 6915 mutex_enter(&tcp->tcp_eager_lock); 6916 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6917 (tcp->tcp_conn_req_cnt_q != 0)) { 6918 tcp_eager_cleanup(tcp, 0); 6919 } 6920 mutex_exit(&tcp->tcp_eager_lock); 6921 6922 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6923 tcp->tcp_rnxt, TH_RST | TH_ACK); 6924 6925 tcp_reinit(tcp); 6926 6927 if (old_state >= TCPS_ESTABLISHED) { 6928 /* Send M_FLUSH according to TPI */ 6929 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6930 } 6931 mp = mi_tpi_ok_ack_alloc(mp); 6932 if (mp) 6933 putnext(tcp->tcp_rq, mp); 6934 return; 6935 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6936 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6937 return; 6938 } 6939 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6940 /* Send M_FLUSH according to TPI */ 6941 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6942 } 6943 mp = mi_tpi_ok_ack_alloc(mp); 6944 if (mp) 6945 putnext(tcp->tcp_rq, mp); 6946 } 6947 6948 /* 6949 * Diagnostic routine used to return a string associated with the tcp state. 6950 * Note that if the caller does not supply a buffer, it will use an internal 6951 * static string. This means that if multiple threads call this function at 6952 * the same time, output can be corrupted... Note also that this function 6953 * does not check the size of the supplied buffer. The caller has to make 6954 * sure that it is big enough. 6955 */ 6956 static char * 6957 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6958 { 6959 char buf1[30]; 6960 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6961 char *buf; 6962 char *cp; 6963 in6_addr_t local, remote; 6964 char local_addrbuf[INET6_ADDRSTRLEN]; 6965 char remote_addrbuf[INET6_ADDRSTRLEN]; 6966 6967 if (sup_buf != NULL) 6968 buf = sup_buf; 6969 else 6970 buf = priv_buf; 6971 6972 if (tcp == NULL) 6973 return ("NULL_TCP"); 6974 switch (tcp->tcp_state) { 6975 case TCPS_CLOSED: 6976 cp = "TCP_CLOSED"; 6977 break; 6978 case TCPS_IDLE: 6979 cp = "TCP_IDLE"; 6980 break; 6981 case TCPS_BOUND: 6982 cp = "TCP_BOUND"; 6983 break; 6984 case TCPS_LISTEN: 6985 cp = "TCP_LISTEN"; 6986 break; 6987 case TCPS_SYN_SENT: 6988 cp = "TCP_SYN_SENT"; 6989 break; 6990 case TCPS_SYN_RCVD: 6991 cp = "TCP_SYN_RCVD"; 6992 break; 6993 case TCPS_ESTABLISHED: 6994 cp = "TCP_ESTABLISHED"; 6995 break; 6996 case TCPS_CLOSE_WAIT: 6997 cp = "TCP_CLOSE_WAIT"; 6998 break; 6999 case TCPS_FIN_WAIT_1: 7000 cp = "TCP_FIN_WAIT_1"; 7001 break; 7002 case TCPS_CLOSING: 7003 cp = "TCP_CLOSING"; 7004 break; 7005 case TCPS_LAST_ACK: 7006 cp = "TCP_LAST_ACK"; 7007 break; 7008 case TCPS_FIN_WAIT_2: 7009 cp = "TCP_FIN_WAIT_2"; 7010 break; 7011 case TCPS_TIME_WAIT: 7012 cp = "TCP_TIME_WAIT"; 7013 break; 7014 default: 7015 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7016 cp = buf1; 7017 break; 7018 } 7019 switch (format) { 7020 case DISP_ADDR_AND_PORT: 7021 if (tcp->tcp_ipversion == IPV4_VERSION) { 7022 /* 7023 * Note that we use the remote address in the tcp_b 7024 * structure. This means that it will print out 7025 * the real destination address, not the next hop's 7026 * address if source routing is used. 7027 */ 7028 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7029 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7030 7031 } else { 7032 local = tcp->tcp_ip_src_v6; 7033 remote = tcp->tcp_remote_v6; 7034 } 7035 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7036 sizeof (local_addrbuf)); 7037 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7038 sizeof (remote_addrbuf)); 7039 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7040 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7041 ntohs(tcp->tcp_fport), cp); 7042 break; 7043 case DISP_PORT_ONLY: 7044 default: 7045 (void) mi_sprintf(buf, "[%u, %u] %s", 7046 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7047 break; 7048 } 7049 7050 return (buf); 7051 } 7052 7053 /* 7054 * Called via squeue to get on to eager's perimeter. It sends a 7055 * TH_RST if eager is in the fanout table. The listener wants the 7056 * eager to disappear either by means of tcp_eager_blowoff() or 7057 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 7058 * called (via squeue) if the eager cannot be inserted in the 7059 * fanout table in tcp_conn_request(). 7060 */ 7061 /* ARGSUSED */ 7062 void 7063 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7064 { 7065 conn_t *econnp = (conn_t *)arg; 7066 tcp_t *eager = econnp->conn_tcp; 7067 tcp_t *listener = eager->tcp_listener; 7068 tcp_stack_t *tcps = eager->tcp_tcps; 7069 7070 /* 7071 * We could be called because listener is closing. Since 7072 * the eager is using listener's queue's, its not safe. 7073 * Better use the default queue just to send the TH_RST 7074 * out. 7075 */ 7076 ASSERT(tcps->tcps_g_q != NULL); 7077 eager->tcp_rq = tcps->tcps_g_q; 7078 eager->tcp_wq = WR(tcps->tcps_g_q); 7079 7080 /* 7081 * An eager's conn_fanout will be NULL if it's a duplicate 7082 * for an existing 4-tuples in the conn fanout table. 7083 * We don't want to send an RST out in such case. 7084 */ 7085 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 7086 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7087 eager, eager->tcp_snxt, 0, TH_RST); 7088 } 7089 7090 /* We are here because listener wants this eager gone */ 7091 if (listener != NULL) { 7092 mutex_enter(&listener->tcp_eager_lock); 7093 tcp_eager_unlink(eager); 7094 if (eager->tcp_tconnind_started) { 7095 /* 7096 * The eager has sent a conn_ind up to the 7097 * listener but listener decides to close 7098 * instead. We need to drop the extra ref 7099 * placed on eager in tcp_rput_data() before 7100 * sending the conn_ind to listener. 7101 */ 7102 CONN_DEC_REF(econnp); 7103 } 7104 mutex_exit(&listener->tcp_eager_lock); 7105 CONN_DEC_REF(listener->tcp_connp); 7106 } 7107 7108 if (eager->tcp_state > TCPS_BOUND) 7109 tcp_close_detached(eager); 7110 } 7111 7112 /* 7113 * Reset any eager connection hanging off this listener marked 7114 * with 'seqnum' and then reclaim it's resources. 7115 */ 7116 static boolean_t 7117 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7118 { 7119 tcp_t *eager; 7120 mblk_t *mp; 7121 tcp_stack_t *tcps = listener->tcp_tcps; 7122 7123 TCP_STAT(tcps, tcp_eager_blowoff_calls); 7124 eager = listener; 7125 mutex_enter(&listener->tcp_eager_lock); 7126 do { 7127 eager = eager->tcp_eager_next_q; 7128 if (eager == NULL) { 7129 mutex_exit(&listener->tcp_eager_lock); 7130 return (B_FALSE); 7131 } 7132 } while (eager->tcp_conn_req_seqnum != seqnum); 7133 7134 if (eager->tcp_closemp_used) { 7135 mutex_exit(&listener->tcp_eager_lock); 7136 return (B_TRUE); 7137 } 7138 eager->tcp_closemp_used = B_TRUE; 7139 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7140 CONN_INC_REF(eager->tcp_connp); 7141 mutex_exit(&listener->tcp_eager_lock); 7142 mp = &eager->tcp_closemp; 7143 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7144 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7145 return (B_TRUE); 7146 } 7147 7148 /* 7149 * Reset any eager connection hanging off this listener 7150 * and then reclaim it's resources. 7151 */ 7152 static void 7153 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7154 { 7155 tcp_t *eager; 7156 mblk_t *mp; 7157 tcp_stack_t *tcps = listener->tcp_tcps; 7158 7159 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7160 7161 if (!q0_only) { 7162 /* First cleanup q */ 7163 TCP_STAT(tcps, tcp_eager_blowoff_q); 7164 eager = listener->tcp_eager_next_q; 7165 while (eager != NULL) { 7166 if (!eager->tcp_closemp_used) { 7167 eager->tcp_closemp_used = B_TRUE; 7168 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7169 CONN_INC_REF(eager->tcp_connp); 7170 mp = &eager->tcp_closemp; 7171 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7172 tcp_eager_kill, eager->tcp_connp, 7173 SQTAG_TCP_EAGER_CLEANUP); 7174 } 7175 eager = eager->tcp_eager_next_q; 7176 } 7177 } 7178 /* Then cleanup q0 */ 7179 TCP_STAT(tcps, tcp_eager_blowoff_q0); 7180 eager = listener->tcp_eager_next_q0; 7181 while (eager != listener) { 7182 if (!eager->tcp_closemp_used) { 7183 eager->tcp_closemp_used = B_TRUE; 7184 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7185 CONN_INC_REF(eager->tcp_connp); 7186 mp = &eager->tcp_closemp; 7187 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7188 tcp_eager_kill, eager->tcp_connp, 7189 SQTAG_TCP_EAGER_CLEANUP_Q0); 7190 } 7191 eager = eager->tcp_eager_next_q0; 7192 } 7193 } 7194 7195 /* 7196 * If we are an eager connection hanging off a listener that hasn't 7197 * formally accepted the connection yet, get off his list and blow off 7198 * any data that we have accumulated. 7199 */ 7200 static void 7201 tcp_eager_unlink(tcp_t *tcp) 7202 { 7203 tcp_t *listener = tcp->tcp_listener; 7204 7205 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7206 ASSERT(listener != NULL); 7207 if (tcp->tcp_eager_next_q0 != NULL) { 7208 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7209 7210 /* Remove the eager tcp from q0 */ 7211 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7212 tcp->tcp_eager_prev_q0; 7213 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7214 tcp->tcp_eager_next_q0; 7215 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7216 listener->tcp_conn_req_cnt_q0--; 7217 7218 tcp->tcp_eager_next_q0 = NULL; 7219 tcp->tcp_eager_prev_q0 = NULL; 7220 7221 /* 7222 * Take the eager out, if it is in the list of droppable 7223 * eagers. 7224 */ 7225 MAKE_UNDROPPABLE(tcp); 7226 7227 if (tcp->tcp_syn_rcvd_timeout != 0) { 7228 /* we have timed out before */ 7229 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7230 listener->tcp_syn_rcvd_timeout--; 7231 } 7232 } else { 7233 tcp_t **tcpp = &listener->tcp_eager_next_q; 7234 tcp_t *prev = NULL; 7235 7236 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7237 if (tcpp[0] == tcp) { 7238 if (listener->tcp_eager_last_q == tcp) { 7239 /* 7240 * If we are unlinking the last 7241 * element on the list, adjust 7242 * tail pointer. Set tail pointer 7243 * to nil when list is empty. 7244 */ 7245 ASSERT(tcp->tcp_eager_next_q == NULL); 7246 if (listener->tcp_eager_last_q == 7247 listener->tcp_eager_next_q) { 7248 listener->tcp_eager_last_q = 7249 NULL; 7250 } else { 7251 /* 7252 * We won't get here if there 7253 * is only one eager in the 7254 * list. 7255 */ 7256 ASSERT(prev != NULL); 7257 listener->tcp_eager_last_q = 7258 prev; 7259 } 7260 } 7261 tcpp[0] = tcp->tcp_eager_next_q; 7262 tcp->tcp_eager_next_q = NULL; 7263 tcp->tcp_eager_last_q = NULL; 7264 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7265 listener->tcp_conn_req_cnt_q--; 7266 break; 7267 } 7268 prev = tcpp[0]; 7269 } 7270 } 7271 tcp->tcp_listener = NULL; 7272 } 7273 7274 /* Shorthand to generate and send TPI error acks to our client */ 7275 static void 7276 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7277 { 7278 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7279 putnext(tcp->tcp_rq, mp); 7280 } 7281 7282 /* Shorthand to generate and send TPI error acks to our client */ 7283 static void 7284 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7285 int t_error, int sys_error) 7286 { 7287 struct T_error_ack *teackp; 7288 7289 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7290 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7291 teackp = (struct T_error_ack *)mp->b_rptr; 7292 teackp->ERROR_prim = primitive; 7293 teackp->TLI_error = t_error; 7294 teackp->UNIX_error = sys_error; 7295 putnext(tcp->tcp_rq, mp); 7296 } 7297 } 7298 7299 /* 7300 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7301 * but instead the code relies on: 7302 * - the fact that the address of the array and its size never changes 7303 * - the atomic assignment of the elements of the array 7304 */ 7305 /* ARGSUSED */ 7306 static int 7307 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7308 { 7309 int i; 7310 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7311 7312 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7313 if (tcps->tcps_g_epriv_ports[i] != 0) 7314 (void) mi_mpprintf(mp, "%d ", 7315 tcps->tcps_g_epriv_ports[i]); 7316 } 7317 return (0); 7318 } 7319 7320 /* 7321 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7322 * threads from changing it at the same time. 7323 */ 7324 /* ARGSUSED */ 7325 static int 7326 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7327 cred_t *cr) 7328 { 7329 long new_value; 7330 int i; 7331 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7332 7333 /* 7334 * Fail the request if the new value does not lie within the 7335 * port number limits. 7336 */ 7337 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7338 new_value <= 0 || new_value >= 65536) { 7339 return (EINVAL); 7340 } 7341 7342 mutex_enter(&tcps->tcps_epriv_port_lock); 7343 /* Check if the value is already in the list */ 7344 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7345 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7346 mutex_exit(&tcps->tcps_epriv_port_lock); 7347 return (EEXIST); 7348 } 7349 } 7350 /* Find an empty slot */ 7351 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7352 if (tcps->tcps_g_epriv_ports[i] == 0) 7353 break; 7354 } 7355 if (i == tcps->tcps_g_num_epriv_ports) { 7356 mutex_exit(&tcps->tcps_epriv_port_lock); 7357 return (EOVERFLOW); 7358 } 7359 /* Set the new value */ 7360 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7361 mutex_exit(&tcps->tcps_epriv_port_lock); 7362 return (0); 7363 } 7364 7365 /* 7366 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7367 * threads from changing it at the same time. 7368 */ 7369 /* ARGSUSED */ 7370 static int 7371 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7372 cred_t *cr) 7373 { 7374 long new_value; 7375 int i; 7376 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7377 7378 /* 7379 * Fail the request if the new value does not lie within the 7380 * port number limits. 7381 */ 7382 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7383 new_value >= 65536) { 7384 return (EINVAL); 7385 } 7386 7387 mutex_enter(&tcps->tcps_epriv_port_lock); 7388 /* Check that the value is already in the list */ 7389 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7390 if (tcps->tcps_g_epriv_ports[i] == new_value) 7391 break; 7392 } 7393 if (i == tcps->tcps_g_num_epriv_ports) { 7394 mutex_exit(&tcps->tcps_epriv_port_lock); 7395 return (ESRCH); 7396 } 7397 /* Clear the value */ 7398 tcps->tcps_g_epriv_ports[i] = 0; 7399 mutex_exit(&tcps->tcps_epriv_port_lock); 7400 return (0); 7401 } 7402 7403 /* Return the TPI/TLI equivalent of our current tcp_state */ 7404 static int 7405 tcp_tpistate(tcp_t *tcp) 7406 { 7407 switch (tcp->tcp_state) { 7408 case TCPS_IDLE: 7409 return (TS_UNBND); 7410 case TCPS_LISTEN: 7411 /* 7412 * Return whether there are outstanding T_CONN_IND waiting 7413 * for the matching T_CONN_RES. Therefore don't count q0. 7414 */ 7415 if (tcp->tcp_conn_req_cnt_q > 0) 7416 return (TS_WRES_CIND); 7417 else 7418 return (TS_IDLE); 7419 case TCPS_BOUND: 7420 return (TS_IDLE); 7421 case TCPS_SYN_SENT: 7422 return (TS_WCON_CREQ); 7423 case TCPS_SYN_RCVD: 7424 /* 7425 * Note: assumption: this has to the active open SYN_RCVD. 7426 * The passive instance is detached in SYN_RCVD stage of 7427 * incoming connection processing so we cannot get request 7428 * for T_info_ack on it. 7429 */ 7430 return (TS_WACK_CRES); 7431 case TCPS_ESTABLISHED: 7432 return (TS_DATA_XFER); 7433 case TCPS_CLOSE_WAIT: 7434 return (TS_WREQ_ORDREL); 7435 case TCPS_FIN_WAIT_1: 7436 return (TS_WIND_ORDREL); 7437 case TCPS_FIN_WAIT_2: 7438 return (TS_WIND_ORDREL); 7439 7440 case TCPS_CLOSING: 7441 case TCPS_LAST_ACK: 7442 case TCPS_TIME_WAIT: 7443 case TCPS_CLOSED: 7444 /* 7445 * Following TS_WACK_DREQ7 is a rendition of "not 7446 * yet TS_IDLE" TPI state. There is no best match to any 7447 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7448 * choose a value chosen that will map to TLI/XTI level 7449 * state of TSTATECHNG (state is process of changing) which 7450 * captures what this dummy state represents. 7451 */ 7452 return (TS_WACK_DREQ7); 7453 default: 7454 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7455 tcp->tcp_state, tcp_display(tcp, NULL, 7456 DISP_PORT_ONLY)); 7457 return (TS_UNBND); 7458 } 7459 } 7460 7461 static void 7462 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7463 { 7464 tcp_stack_t *tcps = tcp->tcp_tcps; 7465 7466 if (tcp->tcp_family == AF_INET6) 7467 *tia = tcp_g_t_info_ack_v6; 7468 else 7469 *tia = tcp_g_t_info_ack; 7470 tia->CURRENT_state = tcp_tpistate(tcp); 7471 tia->OPT_size = tcp_max_optsize; 7472 if (tcp->tcp_mss == 0) { 7473 /* Not yet set - tcp_open does not set mss */ 7474 if (tcp->tcp_ipversion == IPV4_VERSION) 7475 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7476 else 7477 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7478 } else { 7479 tia->TIDU_size = tcp->tcp_mss; 7480 } 7481 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7482 } 7483 7484 /* 7485 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7486 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7487 * tcp_g_t_info_ack. The current state of the stream is copied from 7488 * tcp_state. 7489 */ 7490 static void 7491 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7492 { 7493 t_uscalar_t cap_bits1; 7494 struct T_capability_ack *tcap; 7495 7496 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7497 freemsg(mp); 7498 return; 7499 } 7500 7501 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7502 7503 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7504 mp->b_datap->db_type, T_CAPABILITY_ACK); 7505 if (mp == NULL) 7506 return; 7507 7508 tcap = (struct T_capability_ack *)mp->b_rptr; 7509 tcap->CAP_bits1 = 0; 7510 7511 if (cap_bits1 & TC1_INFO) { 7512 tcp_copy_info(&tcap->INFO_ack, tcp); 7513 tcap->CAP_bits1 |= TC1_INFO; 7514 } 7515 7516 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7517 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7518 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7519 } 7520 7521 putnext(tcp->tcp_rq, mp); 7522 } 7523 7524 /* 7525 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7526 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7527 * The current state of the stream is copied from tcp_state. 7528 */ 7529 static void 7530 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7531 { 7532 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7533 T_INFO_ACK); 7534 if (!mp) { 7535 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7536 return; 7537 } 7538 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7539 putnext(tcp->tcp_rq, mp); 7540 } 7541 7542 /* Respond to the TPI addr request */ 7543 static void 7544 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7545 { 7546 sin_t *sin; 7547 mblk_t *ackmp; 7548 struct T_addr_ack *taa; 7549 7550 /* Make it large enough for worst case */ 7551 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7552 2 * sizeof (sin6_t), 1); 7553 if (ackmp == NULL) { 7554 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7555 return; 7556 } 7557 7558 if (tcp->tcp_ipversion == IPV6_VERSION) { 7559 tcp_addr_req_ipv6(tcp, ackmp); 7560 return; 7561 } 7562 taa = (struct T_addr_ack *)ackmp->b_rptr; 7563 7564 bzero(taa, sizeof (struct T_addr_ack)); 7565 ackmp->b_wptr = (uchar_t *)&taa[1]; 7566 7567 taa->PRIM_type = T_ADDR_ACK; 7568 ackmp->b_datap->db_type = M_PCPROTO; 7569 7570 /* 7571 * Note: Following code assumes 32 bit alignment of basic 7572 * data structures like sin_t and struct T_addr_ack. 7573 */ 7574 if (tcp->tcp_state >= TCPS_BOUND) { 7575 /* 7576 * Fill in local address 7577 */ 7578 taa->LOCADDR_length = sizeof (sin_t); 7579 taa->LOCADDR_offset = sizeof (*taa); 7580 7581 sin = (sin_t *)&taa[1]; 7582 7583 /* Fill zeroes and then intialize non-zero fields */ 7584 *sin = sin_null; 7585 7586 sin->sin_family = AF_INET; 7587 7588 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7589 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7590 7591 ackmp->b_wptr = (uchar_t *)&sin[1]; 7592 7593 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7594 /* 7595 * Fill in Remote address 7596 */ 7597 taa->REMADDR_length = sizeof (sin_t); 7598 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7599 taa->LOCADDR_length); 7600 7601 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7602 *sin = sin_null; 7603 sin->sin_family = AF_INET; 7604 sin->sin_addr.s_addr = tcp->tcp_remote; 7605 sin->sin_port = tcp->tcp_fport; 7606 7607 ackmp->b_wptr = (uchar_t *)&sin[1]; 7608 } 7609 } 7610 putnext(tcp->tcp_rq, ackmp); 7611 } 7612 7613 /* Assumes that tcp_addr_req gets enough space and alignment */ 7614 static void 7615 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7616 { 7617 sin6_t *sin6; 7618 struct T_addr_ack *taa; 7619 7620 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7621 ASSERT(OK_32PTR(ackmp->b_rptr)); 7622 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7623 2 * sizeof (sin6_t)); 7624 7625 taa = (struct T_addr_ack *)ackmp->b_rptr; 7626 7627 bzero(taa, sizeof (struct T_addr_ack)); 7628 ackmp->b_wptr = (uchar_t *)&taa[1]; 7629 7630 taa->PRIM_type = T_ADDR_ACK; 7631 ackmp->b_datap->db_type = M_PCPROTO; 7632 7633 /* 7634 * Note: Following code assumes 32 bit alignment of basic 7635 * data structures like sin6_t and struct T_addr_ack. 7636 */ 7637 if (tcp->tcp_state >= TCPS_BOUND) { 7638 /* 7639 * Fill in local address 7640 */ 7641 taa->LOCADDR_length = sizeof (sin6_t); 7642 taa->LOCADDR_offset = sizeof (*taa); 7643 7644 sin6 = (sin6_t *)&taa[1]; 7645 *sin6 = sin6_null; 7646 7647 sin6->sin6_family = AF_INET6; 7648 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7649 sin6->sin6_port = tcp->tcp_lport; 7650 7651 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7652 7653 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7654 /* 7655 * Fill in Remote address 7656 */ 7657 taa->REMADDR_length = sizeof (sin6_t); 7658 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7659 taa->LOCADDR_length); 7660 7661 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7662 *sin6 = sin6_null; 7663 sin6->sin6_family = AF_INET6; 7664 sin6->sin6_flowinfo = 7665 tcp->tcp_ip6h->ip6_vcf & 7666 ~IPV6_VERS_AND_FLOW_MASK; 7667 sin6->sin6_addr = tcp->tcp_remote_v6; 7668 sin6->sin6_port = tcp->tcp_fport; 7669 7670 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7671 } 7672 } 7673 putnext(tcp->tcp_rq, ackmp); 7674 } 7675 7676 /* 7677 * Handle reinitialization of a tcp structure. 7678 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7679 */ 7680 static void 7681 tcp_reinit(tcp_t *tcp) 7682 { 7683 mblk_t *mp; 7684 int err; 7685 tcp_stack_t *tcps = tcp->tcp_tcps; 7686 7687 TCP_STAT(tcps, tcp_reinit_calls); 7688 7689 /* tcp_reinit should never be called for detached tcp_t's */ 7690 ASSERT(tcp->tcp_listener == NULL); 7691 ASSERT((tcp->tcp_family == AF_INET && 7692 tcp->tcp_ipversion == IPV4_VERSION) || 7693 (tcp->tcp_family == AF_INET6 && 7694 (tcp->tcp_ipversion == IPV4_VERSION || 7695 tcp->tcp_ipversion == IPV6_VERSION))); 7696 7697 /* Cancel outstanding timers */ 7698 tcp_timers_stop(tcp); 7699 7700 /* 7701 * Reset everything in the state vector, after updating global 7702 * MIB data from instance counters. 7703 */ 7704 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7705 tcp->tcp_ibsegs = 0; 7706 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7707 tcp->tcp_obsegs = 0; 7708 7709 tcp_close_mpp(&tcp->tcp_xmit_head); 7710 if (tcp->tcp_snd_zcopy_aware) 7711 tcp_zcopy_notify(tcp); 7712 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7713 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7714 mutex_enter(&tcp->tcp_non_sq_lock); 7715 if (tcp->tcp_flow_stopped && 7716 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7717 tcp_clrqfull(tcp); 7718 } 7719 mutex_exit(&tcp->tcp_non_sq_lock); 7720 tcp_close_mpp(&tcp->tcp_reass_head); 7721 tcp->tcp_reass_tail = NULL; 7722 if (tcp->tcp_rcv_list != NULL) { 7723 /* Free b_next chain */ 7724 tcp_close_mpp(&tcp->tcp_rcv_list); 7725 tcp->tcp_rcv_last_head = NULL; 7726 tcp->tcp_rcv_last_tail = NULL; 7727 tcp->tcp_rcv_cnt = 0; 7728 } 7729 tcp->tcp_rcv_last_tail = NULL; 7730 7731 if ((mp = tcp->tcp_urp_mp) != NULL) { 7732 freemsg(mp); 7733 tcp->tcp_urp_mp = NULL; 7734 } 7735 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7736 freemsg(mp); 7737 tcp->tcp_urp_mark_mp = NULL; 7738 } 7739 if (tcp->tcp_fused_sigurg_mp != NULL) { 7740 freeb(tcp->tcp_fused_sigurg_mp); 7741 tcp->tcp_fused_sigurg_mp = NULL; 7742 } 7743 7744 /* 7745 * Following is a union with two members which are 7746 * identical types and size so the following cleanup 7747 * is enough. 7748 */ 7749 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7750 7751 CL_INET_DISCONNECT(tcp); 7752 7753 /* 7754 * The connection can't be on the tcp_time_wait_head list 7755 * since it is not detached. 7756 */ 7757 ASSERT(tcp->tcp_time_wait_next == NULL); 7758 ASSERT(tcp->tcp_time_wait_prev == NULL); 7759 ASSERT(tcp->tcp_time_wait_expire == 0); 7760 7761 if (tcp->tcp_kssl_pending) { 7762 tcp->tcp_kssl_pending = B_FALSE; 7763 7764 /* Don't reset if the initialized by bind. */ 7765 if (tcp->tcp_kssl_ent != NULL) { 7766 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7767 KSSL_NO_PROXY); 7768 } 7769 } 7770 if (tcp->tcp_kssl_ctx != NULL) { 7771 kssl_release_ctx(tcp->tcp_kssl_ctx); 7772 tcp->tcp_kssl_ctx = NULL; 7773 } 7774 7775 /* 7776 * Reset/preserve other values 7777 */ 7778 tcp_reinit_values(tcp); 7779 ipcl_hash_remove(tcp->tcp_connp); 7780 conn_delete_ire(tcp->tcp_connp, NULL); 7781 tcp_ipsec_cleanup(tcp); 7782 7783 if (tcp->tcp_conn_req_max != 0) { 7784 /* 7785 * This is the case when a TLI program uses the same 7786 * transport end point to accept a connection. This 7787 * makes the TCP both a listener and acceptor. When 7788 * this connection is closed, we need to set the state 7789 * back to TCPS_LISTEN. Make sure that the eager list 7790 * is reinitialized. 7791 * 7792 * Note that this stream is still bound to the four 7793 * tuples of the previous connection in IP. If a new 7794 * SYN with different foreign address comes in, IP will 7795 * not find it and will send it to the global queue. In 7796 * the global queue, TCP will do a tcp_lookup_listener() 7797 * to find this stream. This works because this stream 7798 * is only removed from connected hash. 7799 * 7800 */ 7801 tcp->tcp_state = TCPS_LISTEN; 7802 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7803 tcp->tcp_eager_next_drop_q0 = tcp; 7804 tcp->tcp_eager_prev_drop_q0 = tcp; 7805 tcp->tcp_connp->conn_recv = tcp_conn_request; 7806 if (tcp->tcp_family == AF_INET6) { 7807 ASSERT(tcp->tcp_connp->conn_af_isv6); 7808 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7809 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7810 } else { 7811 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7812 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7813 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7814 } 7815 } else { 7816 tcp->tcp_state = TCPS_BOUND; 7817 } 7818 7819 /* 7820 * Initialize to default values 7821 * Can't fail since enough header template space already allocated 7822 * at open(). 7823 */ 7824 err = tcp_init_values(tcp); 7825 ASSERT(err == 0); 7826 /* Restore state in tcp_tcph */ 7827 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7828 if (tcp->tcp_ipversion == IPV4_VERSION) 7829 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7830 else 7831 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7832 /* 7833 * Copy of the src addr. in tcp_t is needed in tcp_t 7834 * since the lookup funcs can only lookup on tcp_t 7835 */ 7836 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7837 7838 ASSERT(tcp->tcp_ptpbhn != NULL); 7839 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7840 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7841 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7842 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7843 } 7844 7845 /* 7846 * Force values to zero that need be zero. 7847 * Do not touch values asociated with the BOUND or LISTEN state 7848 * since the connection will end up in that state after the reinit. 7849 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7850 * structure! 7851 */ 7852 static void 7853 tcp_reinit_values(tcp) 7854 tcp_t *tcp; 7855 { 7856 tcp_stack_t *tcps = tcp->tcp_tcps; 7857 7858 #ifndef lint 7859 #define DONTCARE(x) 7860 #define PRESERVE(x) 7861 #else 7862 #define DONTCARE(x) ((x) = (x)) 7863 #define PRESERVE(x) ((x) = (x)) 7864 #endif /* lint */ 7865 7866 PRESERVE(tcp->tcp_bind_hash); 7867 PRESERVE(tcp->tcp_ptpbhn); 7868 PRESERVE(tcp->tcp_acceptor_hash); 7869 PRESERVE(tcp->tcp_ptpahn); 7870 7871 /* Should be ASSERT NULL on these with new code! */ 7872 ASSERT(tcp->tcp_time_wait_next == NULL); 7873 ASSERT(tcp->tcp_time_wait_prev == NULL); 7874 ASSERT(tcp->tcp_time_wait_expire == 0); 7875 PRESERVE(tcp->tcp_state); 7876 PRESERVE(tcp->tcp_rq); 7877 PRESERVE(tcp->tcp_wq); 7878 7879 ASSERT(tcp->tcp_xmit_head == NULL); 7880 ASSERT(tcp->tcp_xmit_last == NULL); 7881 ASSERT(tcp->tcp_unsent == 0); 7882 ASSERT(tcp->tcp_xmit_tail == NULL); 7883 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7884 7885 tcp->tcp_snxt = 0; /* Displayed in mib */ 7886 tcp->tcp_suna = 0; /* Displayed in mib */ 7887 tcp->tcp_swnd = 0; 7888 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7889 7890 ASSERT(tcp->tcp_ibsegs == 0); 7891 ASSERT(tcp->tcp_obsegs == 0); 7892 7893 if (tcp->tcp_iphc != NULL) { 7894 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7895 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7896 } 7897 7898 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7899 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7900 DONTCARE(tcp->tcp_ipha); 7901 DONTCARE(tcp->tcp_ip6h); 7902 DONTCARE(tcp->tcp_ip_hdr_len); 7903 DONTCARE(tcp->tcp_tcph); 7904 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7905 tcp->tcp_valid_bits = 0; 7906 7907 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7908 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7909 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7910 tcp->tcp_last_rcv_lbolt = 0; 7911 7912 tcp->tcp_init_cwnd = 0; 7913 7914 tcp->tcp_urp_last_valid = 0; 7915 tcp->tcp_hard_binding = 0; 7916 tcp->tcp_hard_bound = 0; 7917 PRESERVE(tcp->tcp_cred); 7918 PRESERVE(tcp->tcp_cpid); 7919 PRESERVE(tcp->tcp_open_time); 7920 PRESERVE(tcp->tcp_exclbind); 7921 7922 tcp->tcp_fin_acked = 0; 7923 tcp->tcp_fin_rcvd = 0; 7924 tcp->tcp_fin_sent = 0; 7925 tcp->tcp_ordrel_done = 0; 7926 7927 tcp->tcp_debug = 0; 7928 tcp->tcp_dontroute = 0; 7929 tcp->tcp_broadcast = 0; 7930 7931 tcp->tcp_useloopback = 0; 7932 tcp->tcp_reuseaddr = 0; 7933 tcp->tcp_oobinline = 0; 7934 tcp->tcp_dgram_errind = 0; 7935 7936 tcp->tcp_detached = 0; 7937 tcp->tcp_bind_pending = 0; 7938 tcp->tcp_unbind_pending = 0; 7939 tcp->tcp_deferred_clean_death = 0; 7940 7941 tcp->tcp_snd_ws_ok = B_FALSE; 7942 tcp->tcp_snd_ts_ok = B_FALSE; 7943 tcp->tcp_linger = 0; 7944 tcp->tcp_ka_enabled = 0; 7945 tcp->tcp_zero_win_probe = 0; 7946 7947 tcp->tcp_loopback = 0; 7948 tcp->tcp_localnet = 0; 7949 tcp->tcp_syn_defense = 0; 7950 tcp->tcp_set_timer = 0; 7951 7952 tcp->tcp_active_open = 0; 7953 ASSERT(tcp->tcp_timeout == B_FALSE); 7954 tcp->tcp_rexmit = B_FALSE; 7955 tcp->tcp_xmit_zc_clean = B_FALSE; 7956 7957 tcp->tcp_snd_sack_ok = B_FALSE; 7958 PRESERVE(tcp->tcp_recvdstaddr); 7959 tcp->tcp_hwcksum = B_FALSE; 7960 7961 tcp->tcp_ire_ill_check_done = B_FALSE; 7962 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7963 7964 tcp->tcp_mdt = B_FALSE; 7965 tcp->tcp_mdt_hdr_head = 0; 7966 tcp->tcp_mdt_hdr_tail = 0; 7967 7968 tcp->tcp_conn_def_q0 = 0; 7969 tcp->tcp_ip_forward_progress = B_FALSE; 7970 tcp->tcp_anon_priv_bind = 0; 7971 tcp->tcp_ecn_ok = B_FALSE; 7972 7973 tcp->tcp_cwr = B_FALSE; 7974 tcp->tcp_ecn_echo_on = B_FALSE; 7975 7976 if (tcp->tcp_sack_info != NULL) { 7977 if (tcp->tcp_notsack_list != NULL) { 7978 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7979 } 7980 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7981 tcp->tcp_sack_info = NULL; 7982 } 7983 7984 tcp->tcp_rcv_ws = 0; 7985 tcp->tcp_snd_ws = 0; 7986 tcp->tcp_ts_recent = 0; 7987 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7988 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7989 tcp->tcp_if_mtu = 0; 7990 7991 ASSERT(tcp->tcp_reass_head == NULL); 7992 ASSERT(tcp->tcp_reass_tail == NULL); 7993 7994 tcp->tcp_cwnd_cnt = 0; 7995 7996 ASSERT(tcp->tcp_rcv_list == NULL); 7997 ASSERT(tcp->tcp_rcv_last_head == NULL); 7998 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7999 ASSERT(tcp->tcp_rcv_cnt == 0); 8000 8001 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8002 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8003 tcp->tcp_csuna = 0; 8004 8005 tcp->tcp_rto = 0; /* Displayed in MIB */ 8006 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8007 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8008 tcp->tcp_rtt_update = 0; 8009 8010 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8011 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8012 8013 tcp->tcp_rack = 0; /* Displayed in mib */ 8014 tcp->tcp_rack_cnt = 0; 8015 tcp->tcp_rack_cur_max = 0; 8016 tcp->tcp_rack_abs_max = 0; 8017 8018 tcp->tcp_max_swnd = 0; 8019 8020 ASSERT(tcp->tcp_listener == NULL); 8021 8022 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8023 8024 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8025 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8026 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8027 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8028 8029 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8030 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8031 PRESERVE(tcp->tcp_conn_req_max); 8032 PRESERVE(tcp->tcp_conn_req_seqnum); 8033 8034 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8035 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8036 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8037 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8038 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8039 8040 tcp->tcp_lingertime = 0; 8041 8042 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8043 ASSERT(tcp->tcp_urp_mp == NULL); 8044 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8045 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8046 8047 ASSERT(tcp->tcp_eager_next_q == NULL); 8048 ASSERT(tcp->tcp_eager_last_q == NULL); 8049 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8050 tcp->tcp_eager_prev_q0 == NULL) || 8051 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8052 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8053 8054 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8055 tcp->tcp_eager_prev_drop_q0 == NULL) || 8056 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8057 8058 tcp->tcp_client_errno = 0; 8059 8060 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8061 8062 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8063 8064 PRESERVE(tcp->tcp_bound_source_v6); 8065 tcp->tcp_last_sent_len = 0; 8066 tcp->tcp_dupack_cnt = 0; 8067 8068 tcp->tcp_fport = 0; /* Displayed in MIB */ 8069 PRESERVE(tcp->tcp_lport); 8070 8071 PRESERVE(tcp->tcp_acceptor_lockp); 8072 8073 ASSERT(tcp->tcp_ordrelid == 0); 8074 PRESERVE(tcp->tcp_acceptor_id); 8075 DONTCARE(tcp->tcp_ipsec_overhead); 8076 8077 /* 8078 * If tcp_tracing flag is ON (i.e. We have a trace buffer 8079 * in tcp structure and now tracing), Re-initialize all 8080 * members of tcp_traceinfo. 8081 */ 8082 if (tcp->tcp_tracebuf != NULL) { 8083 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 8084 } 8085 8086 PRESERVE(tcp->tcp_family); 8087 if (tcp->tcp_family == AF_INET6) { 8088 tcp->tcp_ipversion = IPV6_VERSION; 8089 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8090 } else { 8091 tcp->tcp_ipversion = IPV4_VERSION; 8092 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8093 } 8094 8095 tcp->tcp_bound_if = 0; 8096 tcp->tcp_ipv6_recvancillary = 0; 8097 tcp->tcp_recvifindex = 0; 8098 tcp->tcp_recvhops = 0; 8099 tcp->tcp_closed = 0; 8100 tcp->tcp_cleandeathtag = 0; 8101 if (tcp->tcp_hopopts != NULL) { 8102 mi_free(tcp->tcp_hopopts); 8103 tcp->tcp_hopopts = NULL; 8104 tcp->tcp_hopoptslen = 0; 8105 } 8106 ASSERT(tcp->tcp_hopoptslen == 0); 8107 if (tcp->tcp_dstopts != NULL) { 8108 mi_free(tcp->tcp_dstopts); 8109 tcp->tcp_dstopts = NULL; 8110 tcp->tcp_dstoptslen = 0; 8111 } 8112 ASSERT(tcp->tcp_dstoptslen == 0); 8113 if (tcp->tcp_rtdstopts != NULL) { 8114 mi_free(tcp->tcp_rtdstopts); 8115 tcp->tcp_rtdstopts = NULL; 8116 tcp->tcp_rtdstoptslen = 0; 8117 } 8118 ASSERT(tcp->tcp_rtdstoptslen == 0); 8119 if (tcp->tcp_rthdr != NULL) { 8120 mi_free(tcp->tcp_rthdr); 8121 tcp->tcp_rthdr = NULL; 8122 tcp->tcp_rthdrlen = 0; 8123 } 8124 ASSERT(tcp->tcp_rthdrlen == 0); 8125 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8126 8127 /* Reset fusion-related fields */ 8128 tcp->tcp_fused = B_FALSE; 8129 tcp->tcp_unfusable = B_FALSE; 8130 tcp->tcp_fused_sigurg = B_FALSE; 8131 tcp->tcp_direct_sockfs = B_FALSE; 8132 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8133 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8134 tcp->tcp_loopback_peer = NULL; 8135 tcp->tcp_fuse_rcv_hiwater = 0; 8136 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8137 tcp->tcp_fuse_rcv_unread_cnt = 0; 8138 8139 tcp->tcp_lso = B_FALSE; 8140 8141 tcp->tcp_in_ack_unsent = 0; 8142 tcp->tcp_cork = B_FALSE; 8143 tcp->tcp_tconnind_started = B_FALSE; 8144 8145 PRESERVE(tcp->tcp_squeue_bytes); 8146 8147 ASSERT(tcp->tcp_kssl_ctx == NULL); 8148 ASSERT(!tcp->tcp_kssl_pending); 8149 PRESERVE(tcp->tcp_kssl_ent); 8150 8151 tcp->tcp_closemp_used = B_FALSE; 8152 8153 #ifdef DEBUG 8154 DONTCARE(tcp->tcmp_stk[0]); 8155 #endif 8156 8157 8158 #undef DONTCARE 8159 #undef PRESERVE 8160 } 8161 8162 /* 8163 * Allocate necessary resources and initialize state vector. 8164 * Guaranteed not to fail so that when an error is returned, 8165 * the caller doesn't need to do any additional cleanup. 8166 */ 8167 int 8168 tcp_init(tcp_t *tcp, queue_t *q) 8169 { 8170 int err; 8171 8172 tcp->tcp_rq = q; 8173 tcp->tcp_wq = WR(q); 8174 tcp->tcp_state = TCPS_IDLE; 8175 if ((err = tcp_init_values(tcp)) != 0) 8176 tcp_timers_stop(tcp); 8177 return (err); 8178 } 8179 8180 static int 8181 tcp_init_values(tcp_t *tcp) 8182 { 8183 int err; 8184 tcp_stack_t *tcps = tcp->tcp_tcps; 8185 8186 ASSERT((tcp->tcp_family == AF_INET && 8187 tcp->tcp_ipversion == IPV4_VERSION) || 8188 (tcp->tcp_family == AF_INET6 && 8189 (tcp->tcp_ipversion == IPV4_VERSION || 8190 tcp->tcp_ipversion == IPV6_VERSION))); 8191 8192 /* 8193 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8194 * will be close to tcp_rexmit_interval_initial. By doing this, we 8195 * allow the algorithm to adjust slowly to large fluctuations of RTT 8196 * during first few transmissions of a connection as seen in slow 8197 * links. 8198 */ 8199 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8200 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8201 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8202 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8203 tcps->tcps_conn_grace_period; 8204 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8205 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8206 tcp->tcp_timer_backoff = 0; 8207 tcp->tcp_ms_we_have_waited = 0; 8208 tcp->tcp_last_recv_time = lbolt; 8209 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8210 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8211 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8212 8213 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8214 8215 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8216 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8217 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8218 /* 8219 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8220 * passive open. 8221 */ 8222 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8223 8224 tcp->tcp_naglim = tcps->tcps_naglim_def; 8225 8226 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8227 8228 tcp->tcp_mdt_hdr_head = 0; 8229 tcp->tcp_mdt_hdr_tail = 0; 8230 8231 /* Reset fusion-related fields */ 8232 tcp->tcp_fused = B_FALSE; 8233 tcp->tcp_unfusable = B_FALSE; 8234 tcp->tcp_fused_sigurg = B_FALSE; 8235 tcp->tcp_direct_sockfs = B_FALSE; 8236 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8237 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8238 tcp->tcp_loopback_peer = NULL; 8239 tcp->tcp_fuse_rcv_hiwater = 0; 8240 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8241 tcp->tcp_fuse_rcv_unread_cnt = 0; 8242 8243 /* Initialize the header template */ 8244 if (tcp->tcp_ipversion == IPV4_VERSION) { 8245 err = tcp_header_init_ipv4(tcp); 8246 } else { 8247 err = tcp_header_init_ipv6(tcp); 8248 } 8249 if (err) 8250 return (err); 8251 8252 /* 8253 * Init the window scale to the max so tcp_rwnd_set() won't pare 8254 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8255 */ 8256 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8257 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8258 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8259 8260 tcp->tcp_cork = B_FALSE; 8261 /* 8262 * Init the tcp_debug option. This value determines whether TCP 8263 * calls strlog() to print out debug messages. Doing this 8264 * initialization here means that this value is not inherited thru 8265 * tcp_reinit(). 8266 */ 8267 tcp->tcp_debug = tcps->tcps_dbg; 8268 8269 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8270 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8271 8272 return (0); 8273 } 8274 8275 /* 8276 * Initialize the IPv4 header. Loses any record of any IP options. 8277 */ 8278 static int 8279 tcp_header_init_ipv4(tcp_t *tcp) 8280 { 8281 tcph_t *tcph; 8282 uint32_t sum; 8283 conn_t *connp; 8284 tcp_stack_t *tcps = tcp->tcp_tcps; 8285 8286 /* 8287 * This is a simple initialization. If there's 8288 * already a template, it should never be too small, 8289 * so reuse it. Otherwise, allocate space for the new one. 8290 */ 8291 if (tcp->tcp_iphc == NULL) { 8292 ASSERT(tcp->tcp_iphc_len == 0); 8293 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8294 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8295 if (tcp->tcp_iphc == NULL) { 8296 tcp->tcp_iphc_len = 0; 8297 return (ENOMEM); 8298 } 8299 } 8300 8301 /* options are gone; may need a new label */ 8302 connp = tcp->tcp_connp; 8303 connp->conn_mlp_type = mlptSingle; 8304 connp->conn_ulp_labeled = !is_system_labeled(); 8305 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8306 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8307 tcp->tcp_ip6h = NULL; 8308 tcp->tcp_ipversion = IPV4_VERSION; 8309 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8310 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8311 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8312 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8313 tcp->tcp_ipha->ipha_version_and_hdr_length 8314 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8315 tcp->tcp_ipha->ipha_ident = 0; 8316 8317 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8318 tcp->tcp_tos = 0; 8319 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8320 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8321 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8322 8323 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8324 tcp->tcp_tcph = tcph; 8325 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8326 /* 8327 * IP wants our header length in the checksum field to 8328 * allow it to perform a single pseudo-header+checksum 8329 * calculation on behalf of TCP. 8330 * Include the adjustment for a source route once IP_OPTIONS is set. 8331 */ 8332 sum = sizeof (tcph_t) + tcp->tcp_sum; 8333 sum = (sum >> 16) + (sum & 0xFFFF); 8334 U16_TO_ABE16(sum, tcph->th_sum); 8335 return (0); 8336 } 8337 8338 /* 8339 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8340 */ 8341 static int 8342 tcp_header_init_ipv6(tcp_t *tcp) 8343 { 8344 tcph_t *tcph; 8345 uint32_t sum; 8346 conn_t *connp; 8347 tcp_stack_t *tcps = tcp->tcp_tcps; 8348 8349 /* 8350 * This is a simple initialization. If there's 8351 * already a template, it should never be too small, 8352 * so reuse it. Otherwise, allocate space for the new one. 8353 * Ensure that there is enough space to "downgrade" the tcp_t 8354 * to an IPv4 tcp_t. This requires having space for a full load 8355 * of IPv4 options, as well as a full load of TCP options 8356 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8357 * than a v6 header and a TCP header with a full load of TCP options 8358 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8359 * We want to avoid reallocation in the "downgraded" case when 8360 * processing outbound IPv4 options. 8361 */ 8362 if (tcp->tcp_iphc == NULL) { 8363 ASSERT(tcp->tcp_iphc_len == 0); 8364 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8365 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8366 if (tcp->tcp_iphc == NULL) { 8367 tcp->tcp_iphc_len = 0; 8368 return (ENOMEM); 8369 } 8370 } 8371 8372 /* options are gone; may need a new label */ 8373 connp = tcp->tcp_connp; 8374 connp->conn_mlp_type = mlptSingle; 8375 connp->conn_ulp_labeled = !is_system_labeled(); 8376 8377 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8378 tcp->tcp_ipversion = IPV6_VERSION; 8379 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8380 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8381 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8382 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8383 tcp->tcp_ipha = NULL; 8384 8385 /* Initialize the header template */ 8386 8387 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8388 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8389 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8390 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8391 8392 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8393 tcp->tcp_tcph = tcph; 8394 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8395 /* 8396 * IP wants our header length in the checksum field to 8397 * allow it to perform a single psuedo-header+checksum 8398 * calculation on behalf of TCP. 8399 * Include the adjustment for a source route when IPV6_RTHDR is set. 8400 */ 8401 sum = sizeof (tcph_t) + tcp->tcp_sum; 8402 sum = (sum >> 16) + (sum & 0xFFFF); 8403 U16_TO_ABE16(sum, tcph->th_sum); 8404 return (0); 8405 } 8406 8407 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8408 #define ICMP_MIN_TCP_HDR 8 8409 8410 /* 8411 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8412 * passed up by IP. The message is always received on the correct tcp_t. 8413 * Assumes that IP has pulled up everything up to and including the ICMP header. 8414 */ 8415 void 8416 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8417 { 8418 icmph_t *icmph; 8419 ipha_t *ipha; 8420 int iph_hdr_length; 8421 tcph_t *tcph; 8422 boolean_t ipsec_mctl = B_FALSE; 8423 boolean_t secure; 8424 mblk_t *first_mp = mp; 8425 uint32_t new_mss; 8426 uint32_t ratio; 8427 size_t mp_size = MBLKL(mp); 8428 uint32_t seg_seq; 8429 tcp_stack_t *tcps = tcp->tcp_tcps; 8430 8431 /* Assume IP provides aligned packets - otherwise toss */ 8432 if (!OK_32PTR(mp->b_rptr)) { 8433 freemsg(mp); 8434 return; 8435 } 8436 8437 /* 8438 * Since ICMP errors are normal data marked with M_CTL when sent 8439 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8440 * packets starting with an ipsec_info_t, see ipsec_info.h. 8441 */ 8442 if ((mp_size == sizeof (ipsec_info_t)) && 8443 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8444 ASSERT(mp->b_cont != NULL); 8445 mp = mp->b_cont; 8446 /* IP should have done this */ 8447 ASSERT(OK_32PTR(mp->b_rptr)); 8448 mp_size = MBLKL(mp); 8449 ipsec_mctl = B_TRUE; 8450 } 8451 8452 /* 8453 * Verify that we have a complete outer IP header. If not, drop it. 8454 */ 8455 if (mp_size < sizeof (ipha_t)) { 8456 noticmpv4: 8457 freemsg(first_mp); 8458 return; 8459 } 8460 8461 ipha = (ipha_t *)mp->b_rptr; 8462 /* 8463 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8464 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8465 */ 8466 switch (IPH_HDR_VERSION(ipha)) { 8467 case IPV6_VERSION: 8468 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8469 return; 8470 case IPV4_VERSION: 8471 break; 8472 default: 8473 goto noticmpv4; 8474 } 8475 8476 /* Skip past the outer IP and ICMP headers */ 8477 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8478 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8479 /* 8480 * If we don't have the correct outer IP header length or if the ULP 8481 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8482 * send it upstream. 8483 */ 8484 if (iph_hdr_length < sizeof (ipha_t) || 8485 ipha->ipha_protocol != IPPROTO_ICMP || 8486 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8487 goto noticmpv4; 8488 } 8489 ipha = (ipha_t *)&icmph[1]; 8490 8491 /* Skip past the inner IP and find the ULP header */ 8492 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8493 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8494 /* 8495 * If we don't have the correct inner IP header length or if the ULP 8496 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8497 * bytes of TCP header, drop it. 8498 */ 8499 if (iph_hdr_length < sizeof (ipha_t) || 8500 ipha->ipha_protocol != IPPROTO_TCP || 8501 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8502 goto noticmpv4; 8503 } 8504 8505 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8506 if (ipsec_mctl) { 8507 secure = ipsec_in_is_secure(first_mp); 8508 } else { 8509 secure = B_FALSE; 8510 } 8511 if (secure) { 8512 /* 8513 * If we are willing to accept this in clear 8514 * we don't have to verify policy. 8515 */ 8516 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8517 if (!tcp_check_policy(tcp, first_mp, 8518 ipha, NULL, secure, ipsec_mctl)) { 8519 /* 8520 * tcp_check_policy called 8521 * ip_drop_packet() on failure. 8522 */ 8523 return; 8524 } 8525 } 8526 } 8527 } else if (ipsec_mctl) { 8528 /* 8529 * This is a hard_bound connection. IP has already 8530 * verified policy. We don't have to do it again. 8531 */ 8532 freeb(first_mp); 8533 first_mp = mp; 8534 ipsec_mctl = B_FALSE; 8535 } 8536 8537 seg_seq = ABE32_TO_U32(tcph->th_seq); 8538 /* 8539 * TCP SHOULD check that the TCP sequence number contained in 8540 * payload of the ICMP error message is within the range 8541 * SND.UNA <= SEG.SEQ < SND.NXT. 8542 */ 8543 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8544 /* 8545 * If the ICMP message is bogus, should we kill the 8546 * connection, or should we just drop the bogus ICMP 8547 * message? It would probably make more sense to just 8548 * drop the message so that if this one managed to get 8549 * in, the real connection should not suffer. 8550 */ 8551 goto noticmpv4; 8552 } 8553 8554 switch (icmph->icmph_type) { 8555 case ICMP_DEST_UNREACHABLE: 8556 switch (icmph->icmph_code) { 8557 case ICMP_FRAGMENTATION_NEEDED: 8558 /* 8559 * Reduce the MSS based on the new MTU. This will 8560 * eliminate any fragmentation locally. 8561 * N.B. There may well be some funny side-effects on 8562 * the local send policy and the remote receive policy. 8563 * Pending further research, we provide 8564 * tcp_ignore_path_mtu just in case this proves 8565 * disastrous somewhere. 8566 * 8567 * After updating the MSS, retransmit part of the 8568 * dropped segment using the new mss by calling 8569 * tcp_wput_data(). Need to adjust all those 8570 * params to make sure tcp_wput_data() work properly. 8571 */ 8572 if (tcps->tcps_ignore_path_mtu) 8573 break; 8574 8575 /* 8576 * Decrease the MSS by time stamp options 8577 * IP options and IPSEC options. tcp_hdr_len 8578 * includes time stamp option and IP option 8579 * length. 8580 */ 8581 8582 new_mss = ntohs(icmph->icmph_du_mtu) - 8583 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8584 8585 /* 8586 * Only update the MSS if the new one is 8587 * smaller than the previous one. This is 8588 * to avoid problems when getting multiple 8589 * ICMP errors for the same MTU. 8590 */ 8591 if (new_mss >= tcp->tcp_mss) 8592 break; 8593 8594 /* 8595 * Stop doing PMTU if new_mss is less than 68 8596 * or less than tcp_mss_min. 8597 * The value 68 comes from rfc 1191. 8598 */ 8599 if (new_mss < MAX(68, tcps->tcps_mss_min)) 8600 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8601 0; 8602 8603 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8604 ASSERT(ratio >= 1); 8605 tcp_mss_set(tcp, new_mss, B_TRUE); 8606 8607 /* 8608 * Make sure we have something to 8609 * send. 8610 */ 8611 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8612 (tcp->tcp_xmit_head != NULL)) { 8613 /* 8614 * Shrink tcp_cwnd in 8615 * proportion to the old MSS/new MSS. 8616 */ 8617 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8618 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8619 (tcp->tcp_unsent == 0)) { 8620 tcp->tcp_rexmit_max = tcp->tcp_fss; 8621 } else { 8622 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8623 } 8624 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8625 tcp->tcp_rexmit = B_TRUE; 8626 tcp->tcp_dupack_cnt = 0; 8627 tcp->tcp_snd_burst = TCP_CWND_SS; 8628 tcp_ss_rexmit(tcp); 8629 } 8630 break; 8631 case ICMP_PORT_UNREACHABLE: 8632 case ICMP_PROTOCOL_UNREACHABLE: 8633 switch (tcp->tcp_state) { 8634 case TCPS_SYN_SENT: 8635 case TCPS_SYN_RCVD: 8636 /* 8637 * ICMP can snipe away incipient 8638 * TCP connections as long as 8639 * seq number is same as initial 8640 * send seq number. 8641 */ 8642 if (seg_seq == tcp->tcp_iss) { 8643 (void) tcp_clean_death(tcp, 8644 ECONNREFUSED, 6); 8645 } 8646 break; 8647 } 8648 break; 8649 case ICMP_HOST_UNREACHABLE: 8650 case ICMP_NET_UNREACHABLE: 8651 /* Record the error in case we finally time out. */ 8652 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8653 tcp->tcp_client_errno = EHOSTUNREACH; 8654 else 8655 tcp->tcp_client_errno = ENETUNREACH; 8656 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8657 if (tcp->tcp_listener != NULL && 8658 tcp->tcp_listener->tcp_syn_defense) { 8659 /* 8660 * Ditch the half-open connection if we 8661 * suspect a SYN attack is under way. 8662 */ 8663 tcp_ip_ire_mark_advice(tcp); 8664 (void) tcp_clean_death(tcp, 8665 tcp->tcp_client_errno, 7); 8666 } 8667 } 8668 break; 8669 default: 8670 break; 8671 } 8672 break; 8673 case ICMP_SOURCE_QUENCH: { 8674 /* 8675 * use a global boolean to control 8676 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8677 * The default is false. 8678 */ 8679 if (tcp_icmp_source_quench) { 8680 /* 8681 * Reduce the sending rate as if we got a 8682 * retransmit timeout 8683 */ 8684 uint32_t npkt; 8685 8686 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8687 tcp->tcp_mss; 8688 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8689 tcp->tcp_cwnd = tcp->tcp_mss; 8690 tcp->tcp_cwnd_cnt = 0; 8691 } 8692 break; 8693 } 8694 } 8695 freemsg(first_mp); 8696 } 8697 8698 /* 8699 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8700 * error messages passed up by IP. 8701 * Assumes that IP has pulled up all the extension headers as well 8702 * as the ICMPv6 header. 8703 */ 8704 static void 8705 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8706 { 8707 icmp6_t *icmp6; 8708 ip6_t *ip6h; 8709 uint16_t iph_hdr_length; 8710 tcpha_t *tcpha; 8711 uint8_t *nexthdrp; 8712 uint32_t new_mss; 8713 uint32_t ratio; 8714 boolean_t secure; 8715 mblk_t *first_mp = mp; 8716 size_t mp_size; 8717 uint32_t seg_seq; 8718 tcp_stack_t *tcps = tcp->tcp_tcps; 8719 8720 /* 8721 * The caller has determined if this is an IPSEC_IN packet and 8722 * set ipsec_mctl appropriately (see tcp_icmp_error). 8723 */ 8724 if (ipsec_mctl) 8725 mp = mp->b_cont; 8726 8727 mp_size = MBLKL(mp); 8728 8729 /* 8730 * Verify that we have a complete IP header. If not, send it upstream. 8731 */ 8732 if (mp_size < sizeof (ip6_t)) { 8733 noticmpv6: 8734 freemsg(first_mp); 8735 return; 8736 } 8737 8738 /* 8739 * Verify this is an ICMPV6 packet, else send it upstream. 8740 */ 8741 ip6h = (ip6_t *)mp->b_rptr; 8742 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8743 iph_hdr_length = IPV6_HDR_LEN; 8744 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8745 &nexthdrp) || 8746 *nexthdrp != IPPROTO_ICMPV6) { 8747 goto noticmpv6; 8748 } 8749 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8750 ip6h = (ip6_t *)&icmp6[1]; 8751 /* 8752 * Verify if we have a complete ICMP and inner IP header. 8753 */ 8754 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8755 goto noticmpv6; 8756 8757 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8758 goto noticmpv6; 8759 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8760 /* 8761 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8762 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8763 * packet. 8764 */ 8765 if ((*nexthdrp != IPPROTO_TCP) || 8766 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8767 goto noticmpv6; 8768 } 8769 8770 /* 8771 * ICMP errors come on the right queue or come on 8772 * listener/global queue for detached connections and 8773 * get switched to the right queue. If it comes on the 8774 * right queue, policy check has already been done by IP 8775 * and thus free the first_mp without verifying the policy. 8776 * If it has come for a non-hard bound connection, we need 8777 * to verify policy as IP may not have done it. 8778 */ 8779 if (!tcp->tcp_hard_bound) { 8780 if (ipsec_mctl) { 8781 secure = ipsec_in_is_secure(first_mp); 8782 } else { 8783 secure = B_FALSE; 8784 } 8785 if (secure) { 8786 /* 8787 * If we are willing to accept this in clear 8788 * we don't have to verify policy. 8789 */ 8790 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8791 if (!tcp_check_policy(tcp, first_mp, 8792 NULL, ip6h, secure, ipsec_mctl)) { 8793 /* 8794 * tcp_check_policy called 8795 * ip_drop_packet() on failure. 8796 */ 8797 return; 8798 } 8799 } 8800 } 8801 } else if (ipsec_mctl) { 8802 /* 8803 * This is a hard_bound connection. IP has already 8804 * verified policy. We don't have to do it again. 8805 */ 8806 freeb(first_mp); 8807 first_mp = mp; 8808 ipsec_mctl = B_FALSE; 8809 } 8810 8811 seg_seq = ntohl(tcpha->tha_seq); 8812 /* 8813 * TCP SHOULD check that the TCP sequence number contained in 8814 * payload of the ICMP error message is within the range 8815 * SND.UNA <= SEG.SEQ < SND.NXT. 8816 */ 8817 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8818 /* 8819 * If the ICMP message is bogus, should we kill the 8820 * connection, or should we just drop the bogus ICMP 8821 * message? It would probably make more sense to just 8822 * drop the message so that if this one managed to get 8823 * in, the real connection should not suffer. 8824 */ 8825 goto noticmpv6; 8826 } 8827 8828 switch (icmp6->icmp6_type) { 8829 case ICMP6_PACKET_TOO_BIG: 8830 /* 8831 * Reduce the MSS based on the new MTU. This will 8832 * eliminate any fragmentation locally. 8833 * N.B. There may well be some funny side-effects on 8834 * the local send policy and the remote receive policy. 8835 * Pending further research, we provide 8836 * tcp_ignore_path_mtu just in case this proves 8837 * disastrous somewhere. 8838 * 8839 * After updating the MSS, retransmit part of the 8840 * dropped segment using the new mss by calling 8841 * tcp_wput_data(). Need to adjust all those 8842 * params to make sure tcp_wput_data() work properly. 8843 */ 8844 if (tcps->tcps_ignore_path_mtu) 8845 break; 8846 8847 /* 8848 * Decrease the MSS by time stamp options 8849 * IP options and IPSEC options. tcp_hdr_len 8850 * includes time stamp option and IP option 8851 * length. 8852 */ 8853 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8854 tcp->tcp_ipsec_overhead; 8855 8856 /* 8857 * Only update the MSS if the new one is 8858 * smaller than the previous one. This is 8859 * to avoid problems when getting multiple 8860 * ICMP errors for the same MTU. 8861 */ 8862 if (new_mss >= tcp->tcp_mss) 8863 break; 8864 8865 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8866 ASSERT(ratio >= 1); 8867 tcp_mss_set(tcp, new_mss, B_TRUE); 8868 8869 /* 8870 * Make sure we have something to 8871 * send. 8872 */ 8873 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8874 (tcp->tcp_xmit_head != NULL)) { 8875 /* 8876 * Shrink tcp_cwnd in 8877 * proportion to the old MSS/new MSS. 8878 */ 8879 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8880 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8881 (tcp->tcp_unsent == 0)) { 8882 tcp->tcp_rexmit_max = tcp->tcp_fss; 8883 } else { 8884 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8885 } 8886 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8887 tcp->tcp_rexmit = B_TRUE; 8888 tcp->tcp_dupack_cnt = 0; 8889 tcp->tcp_snd_burst = TCP_CWND_SS; 8890 tcp_ss_rexmit(tcp); 8891 } 8892 break; 8893 8894 case ICMP6_DST_UNREACH: 8895 switch (icmp6->icmp6_code) { 8896 case ICMP6_DST_UNREACH_NOPORT: 8897 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8898 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8899 (seg_seq == tcp->tcp_iss)) { 8900 (void) tcp_clean_death(tcp, 8901 ECONNREFUSED, 8); 8902 } 8903 break; 8904 8905 case ICMP6_DST_UNREACH_ADMIN: 8906 case ICMP6_DST_UNREACH_NOROUTE: 8907 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8908 case ICMP6_DST_UNREACH_ADDR: 8909 /* Record the error in case we finally time out. */ 8910 tcp->tcp_client_errno = EHOSTUNREACH; 8911 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8912 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8913 (seg_seq == tcp->tcp_iss)) { 8914 if (tcp->tcp_listener != NULL && 8915 tcp->tcp_listener->tcp_syn_defense) { 8916 /* 8917 * Ditch the half-open connection if we 8918 * suspect a SYN attack is under way. 8919 */ 8920 tcp_ip_ire_mark_advice(tcp); 8921 (void) tcp_clean_death(tcp, 8922 tcp->tcp_client_errno, 9); 8923 } 8924 } 8925 8926 8927 break; 8928 default: 8929 break; 8930 } 8931 break; 8932 8933 case ICMP6_PARAM_PROB: 8934 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8935 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8936 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8937 (uchar_t *)nexthdrp) { 8938 if (tcp->tcp_state == TCPS_SYN_SENT || 8939 tcp->tcp_state == TCPS_SYN_RCVD) { 8940 (void) tcp_clean_death(tcp, 8941 ECONNREFUSED, 10); 8942 } 8943 break; 8944 } 8945 break; 8946 8947 case ICMP6_TIME_EXCEEDED: 8948 default: 8949 break; 8950 } 8951 freemsg(first_mp); 8952 } 8953 8954 /* 8955 * IP recognizes seven kinds of bind requests: 8956 * 8957 * - A zero-length address binds only to the protocol number. 8958 * 8959 * - A 4-byte address is treated as a request to 8960 * validate that the address is a valid local IPv4 8961 * address, appropriate for an application to bind to. 8962 * IP does the verification, but does not make any note 8963 * of the address at this time. 8964 * 8965 * - A 16-byte address contains is treated as a request 8966 * to validate a local IPv6 address, as the 4-byte 8967 * address case above. 8968 * 8969 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 8970 * use it for the inbound fanout of packets. 8971 * 8972 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 8973 * use it for the inbound fanout of packets. 8974 * 8975 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 8976 * information consisting of local and remote addresses 8977 * and ports. In this case, the addresses are both 8978 * validated as appropriate for this operation, and, if 8979 * so, the information is retained for use in the 8980 * inbound fanout. 8981 * 8982 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 8983 * fanout information, like the 12-byte case above. 8984 * 8985 * IP will also fill in the IRE request mblk with information 8986 * regarding our peer. In all cases, we notify IP of our protocol 8987 * type by appending a single protocol byte to the bind request. 8988 */ 8989 static mblk_t * 8990 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 8991 { 8992 char *cp; 8993 mblk_t *mp; 8994 struct T_bind_req *tbr; 8995 ipa_conn_t *ac; 8996 ipa6_conn_t *ac6; 8997 sin_t *sin; 8998 sin6_t *sin6; 8999 9000 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9001 ASSERT((tcp->tcp_family == AF_INET && 9002 tcp->tcp_ipversion == IPV4_VERSION) || 9003 (tcp->tcp_family == AF_INET6 && 9004 (tcp->tcp_ipversion == IPV4_VERSION || 9005 tcp->tcp_ipversion == IPV6_VERSION))); 9006 9007 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9008 if (!mp) 9009 return (mp); 9010 mp->b_datap->db_type = M_PROTO; 9011 tbr = (struct T_bind_req *)mp->b_rptr; 9012 tbr->PRIM_type = bind_prim; 9013 tbr->ADDR_offset = sizeof (*tbr); 9014 tbr->CONIND_number = 0; 9015 tbr->ADDR_length = addr_length; 9016 cp = (char *)&tbr[1]; 9017 switch (addr_length) { 9018 case sizeof (ipa_conn_t): 9019 ASSERT(tcp->tcp_family == AF_INET); 9020 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9021 9022 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9023 if (mp->b_cont == NULL) { 9024 freemsg(mp); 9025 return (NULL); 9026 } 9027 mp->b_cont->b_wptr += sizeof (ire_t); 9028 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9029 9030 /* cp known to be 32 bit aligned */ 9031 ac = (ipa_conn_t *)cp; 9032 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9033 ac->ac_faddr = tcp->tcp_remote; 9034 ac->ac_fport = tcp->tcp_fport; 9035 ac->ac_lport = tcp->tcp_lport; 9036 tcp->tcp_hard_binding = 1; 9037 break; 9038 9039 case sizeof (ipa6_conn_t): 9040 ASSERT(tcp->tcp_family == AF_INET6); 9041 9042 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9043 if (mp->b_cont == NULL) { 9044 freemsg(mp); 9045 return (NULL); 9046 } 9047 mp->b_cont->b_wptr += sizeof (ire_t); 9048 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9049 9050 /* cp known to be 32 bit aligned */ 9051 ac6 = (ipa6_conn_t *)cp; 9052 if (tcp->tcp_ipversion == IPV4_VERSION) { 9053 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9054 &ac6->ac6_laddr); 9055 } else { 9056 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9057 } 9058 ac6->ac6_faddr = tcp->tcp_remote_v6; 9059 ac6->ac6_fport = tcp->tcp_fport; 9060 ac6->ac6_lport = tcp->tcp_lport; 9061 tcp->tcp_hard_binding = 1; 9062 break; 9063 9064 case sizeof (sin_t): 9065 /* 9066 * NOTE: IPV6_ADDR_LEN also has same size. 9067 * Use family to discriminate. 9068 */ 9069 if (tcp->tcp_family == AF_INET) { 9070 sin = (sin_t *)cp; 9071 9072 *sin = sin_null; 9073 sin->sin_family = AF_INET; 9074 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9075 sin->sin_port = tcp->tcp_lport; 9076 break; 9077 } else { 9078 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9079 } 9080 break; 9081 9082 case sizeof (sin6_t): 9083 ASSERT(tcp->tcp_family == AF_INET6); 9084 sin6 = (sin6_t *)cp; 9085 9086 *sin6 = sin6_null; 9087 sin6->sin6_family = AF_INET6; 9088 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9089 sin6->sin6_port = tcp->tcp_lport; 9090 break; 9091 9092 case IP_ADDR_LEN: 9093 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9094 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9095 break; 9096 9097 } 9098 /* Add protocol number to end */ 9099 cp[addr_length] = (char)IPPROTO_TCP; 9100 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9101 return (mp); 9102 } 9103 9104 /* 9105 * Notify IP that we are having trouble with this connection. IP should 9106 * blow the IRE away and start over. 9107 */ 9108 static void 9109 tcp_ip_notify(tcp_t *tcp) 9110 { 9111 struct iocblk *iocp; 9112 ipid_t *ipid; 9113 mblk_t *mp; 9114 9115 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9116 if (tcp->tcp_ipversion == IPV6_VERSION) 9117 return; 9118 9119 mp = mkiocb(IP_IOCTL); 9120 if (mp == NULL) 9121 return; 9122 9123 iocp = (struct iocblk *)mp->b_rptr; 9124 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9125 9126 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9127 if (!mp->b_cont) { 9128 freeb(mp); 9129 return; 9130 } 9131 9132 ipid = (ipid_t *)mp->b_cont->b_rptr; 9133 mp->b_cont->b_wptr += iocp->ioc_count; 9134 bzero(ipid, sizeof (*ipid)); 9135 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9136 ipid->ipid_ire_type = IRE_CACHE; 9137 ipid->ipid_addr_offset = sizeof (ipid_t); 9138 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9139 /* 9140 * Note: in the case of source routing we want to blow away the 9141 * route to the first source route hop. 9142 */ 9143 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9144 sizeof (tcp->tcp_ipha->ipha_dst)); 9145 9146 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9147 } 9148 9149 /* Unlink and return any mblk that looks like it contains an ire */ 9150 static mblk_t * 9151 tcp_ire_mp(mblk_t *mp) 9152 { 9153 mblk_t *prev_mp; 9154 9155 for (;;) { 9156 prev_mp = mp; 9157 mp = mp->b_cont; 9158 if (mp == NULL) 9159 break; 9160 switch (DB_TYPE(mp)) { 9161 case IRE_DB_TYPE: 9162 case IRE_DB_REQ_TYPE: 9163 if (prev_mp != NULL) 9164 prev_mp->b_cont = mp->b_cont; 9165 mp->b_cont = NULL; 9166 return (mp); 9167 default: 9168 break; 9169 } 9170 } 9171 return (mp); 9172 } 9173 9174 /* 9175 * Timer callback routine for keepalive probe. We do a fake resend of 9176 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9177 * check to see if we have heard anything from the other end for the last 9178 * RTO period. If we have, set the timer to expire for another 9179 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9180 * RTO << 1 and check again when it expires. Keep exponentially increasing 9181 * the timeout if we have not heard from the other side. If for more than 9182 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9183 * kill the connection unless the keepalive abort threshold is 0. In 9184 * that case, we will probe "forever." 9185 */ 9186 static void 9187 tcp_keepalive_killer(void *arg) 9188 { 9189 mblk_t *mp; 9190 conn_t *connp = (conn_t *)arg; 9191 tcp_t *tcp = connp->conn_tcp; 9192 int32_t firetime; 9193 int32_t idletime; 9194 int32_t ka_intrvl; 9195 tcp_stack_t *tcps = tcp->tcp_tcps; 9196 9197 tcp->tcp_ka_tid = 0; 9198 9199 if (tcp->tcp_fused) 9200 return; 9201 9202 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9203 ka_intrvl = tcp->tcp_ka_interval; 9204 9205 /* 9206 * Keepalive probe should only be sent if the application has not 9207 * done a close on the connection. 9208 */ 9209 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9210 return; 9211 } 9212 /* Timer fired too early, restart it. */ 9213 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9214 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9215 MSEC_TO_TICK(ka_intrvl)); 9216 return; 9217 } 9218 9219 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9220 /* 9221 * If we have not heard from the other side for a long 9222 * time, kill the connection unless the keepalive abort 9223 * threshold is 0. In that case, we will probe "forever." 9224 */ 9225 if (tcp->tcp_ka_abort_thres != 0 && 9226 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9227 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9228 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9229 tcp->tcp_client_errno : ETIMEDOUT, 11); 9230 return; 9231 } 9232 9233 if (tcp->tcp_snxt == tcp->tcp_suna && 9234 idletime >= ka_intrvl) { 9235 /* Fake resend of last ACKed byte. */ 9236 mblk_t *mp1 = allocb(1, BPRI_LO); 9237 9238 if (mp1 != NULL) { 9239 *mp1->b_wptr++ = '\0'; 9240 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9241 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9242 freeb(mp1); 9243 /* 9244 * if allocation failed, fall through to start the 9245 * timer back. 9246 */ 9247 if (mp != NULL) { 9248 TCP_RECORD_TRACE(tcp, mp, 9249 TCP_TRACE_SEND_PKT); 9250 tcp_send_data(tcp, tcp->tcp_wq, mp); 9251 BUMP_MIB(&tcps->tcps_mib, 9252 tcpTimKeepaliveProbe); 9253 if (tcp->tcp_ka_last_intrvl != 0) { 9254 int max; 9255 /* 9256 * We should probe again at least 9257 * in ka_intrvl, but not more than 9258 * tcp_rexmit_interval_max. 9259 */ 9260 max = tcps->tcps_rexmit_interval_max; 9261 firetime = MIN(ka_intrvl - 1, 9262 tcp->tcp_ka_last_intrvl << 1); 9263 if (firetime > max) 9264 firetime = max; 9265 } else { 9266 firetime = tcp->tcp_rto; 9267 } 9268 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9269 tcp_keepalive_killer, 9270 MSEC_TO_TICK(firetime)); 9271 tcp->tcp_ka_last_intrvl = firetime; 9272 return; 9273 } 9274 } 9275 } else { 9276 tcp->tcp_ka_last_intrvl = 0; 9277 } 9278 9279 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9280 if ((firetime = ka_intrvl - idletime) < 0) { 9281 firetime = ka_intrvl; 9282 } 9283 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9284 MSEC_TO_TICK(firetime)); 9285 } 9286 9287 int 9288 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9289 { 9290 queue_t *q = tcp->tcp_rq; 9291 int32_t mss = tcp->tcp_mss; 9292 int maxpsz; 9293 9294 if (TCP_IS_DETACHED(tcp)) 9295 return (mss); 9296 9297 if (tcp->tcp_fused) { 9298 maxpsz = tcp_fuse_maxpsz_set(tcp); 9299 mss = INFPSZ; 9300 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9301 /* 9302 * Set the sd_qn_maxpsz according to the socket send buffer 9303 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9304 * instruct the stream head to copyin user data into contiguous 9305 * kernel-allocated buffers without breaking it up into smaller 9306 * chunks. We round up the buffer size to the nearest SMSS. 9307 */ 9308 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9309 if (tcp->tcp_kssl_ctx == NULL) 9310 mss = INFPSZ; 9311 else 9312 mss = SSL3_MAX_RECORD_LEN; 9313 } else { 9314 /* 9315 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9316 * (and a multiple of the mss). This instructs the stream 9317 * head to break down larger than SMSS writes into SMSS- 9318 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9319 */ 9320 maxpsz = tcp->tcp_maxpsz * mss; 9321 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9322 maxpsz = tcp->tcp_xmit_hiwater/2; 9323 /* Round up to nearest mss */ 9324 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9325 } 9326 } 9327 (void) setmaxps(q, maxpsz); 9328 tcp->tcp_wq->q_maxpsz = maxpsz; 9329 9330 if (set_maxblk) 9331 (void) mi_set_sth_maxblk(q, mss); 9332 9333 return (mss); 9334 } 9335 9336 /* 9337 * Extract option values from a tcp header. We put any found values into the 9338 * tcpopt struct and return a bitmask saying which options were found. 9339 */ 9340 static int 9341 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9342 { 9343 uchar_t *endp; 9344 int len; 9345 uint32_t mss; 9346 uchar_t *up = (uchar_t *)tcph; 9347 int found = 0; 9348 int32_t sack_len; 9349 tcp_seq sack_begin, sack_end; 9350 tcp_t *tcp; 9351 9352 endp = up + TCP_HDR_LENGTH(tcph); 9353 up += TCP_MIN_HEADER_LENGTH; 9354 while (up < endp) { 9355 len = endp - up; 9356 switch (*up) { 9357 case TCPOPT_EOL: 9358 break; 9359 9360 case TCPOPT_NOP: 9361 up++; 9362 continue; 9363 9364 case TCPOPT_MAXSEG: 9365 if (len < TCPOPT_MAXSEG_LEN || 9366 up[1] != TCPOPT_MAXSEG_LEN) 9367 break; 9368 9369 mss = BE16_TO_U16(up+2); 9370 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9371 tcpopt->tcp_opt_mss = mss; 9372 found |= TCP_OPT_MSS_PRESENT; 9373 9374 up += TCPOPT_MAXSEG_LEN; 9375 continue; 9376 9377 case TCPOPT_WSCALE: 9378 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9379 break; 9380 9381 if (up[2] > TCP_MAX_WINSHIFT) 9382 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9383 else 9384 tcpopt->tcp_opt_wscale = up[2]; 9385 found |= TCP_OPT_WSCALE_PRESENT; 9386 9387 up += TCPOPT_WS_LEN; 9388 continue; 9389 9390 case TCPOPT_SACK_PERMITTED: 9391 if (len < TCPOPT_SACK_OK_LEN || 9392 up[1] != TCPOPT_SACK_OK_LEN) 9393 break; 9394 found |= TCP_OPT_SACK_OK_PRESENT; 9395 up += TCPOPT_SACK_OK_LEN; 9396 continue; 9397 9398 case TCPOPT_SACK: 9399 if (len <= 2 || up[1] <= 2 || len < up[1]) 9400 break; 9401 9402 /* If TCP is not interested in SACK blks... */ 9403 if ((tcp = tcpopt->tcp) == NULL) { 9404 up += up[1]; 9405 continue; 9406 } 9407 sack_len = up[1] - TCPOPT_HEADER_LEN; 9408 up += TCPOPT_HEADER_LEN; 9409 9410 /* 9411 * If the list is empty, allocate one and assume 9412 * nothing is sack'ed. 9413 */ 9414 ASSERT(tcp->tcp_sack_info != NULL); 9415 if (tcp->tcp_notsack_list == NULL) { 9416 tcp_notsack_update(&(tcp->tcp_notsack_list), 9417 tcp->tcp_suna, tcp->tcp_snxt, 9418 &(tcp->tcp_num_notsack_blk), 9419 &(tcp->tcp_cnt_notsack_list)); 9420 9421 /* 9422 * Make sure tcp_notsack_list is not NULL. 9423 * This happens when kmem_alloc(KM_NOSLEEP) 9424 * returns NULL. 9425 */ 9426 if (tcp->tcp_notsack_list == NULL) { 9427 up += sack_len; 9428 continue; 9429 } 9430 tcp->tcp_fack = tcp->tcp_suna; 9431 } 9432 9433 while (sack_len > 0) { 9434 if (up + 8 > endp) { 9435 up = endp; 9436 break; 9437 } 9438 sack_begin = BE32_TO_U32(up); 9439 up += 4; 9440 sack_end = BE32_TO_U32(up); 9441 up += 4; 9442 sack_len -= 8; 9443 /* 9444 * Bounds checking. Make sure the SACK 9445 * info is within tcp_suna and tcp_snxt. 9446 * If this SACK blk is out of bound, ignore 9447 * it but continue to parse the following 9448 * blks. 9449 */ 9450 if (SEQ_LEQ(sack_end, sack_begin) || 9451 SEQ_LT(sack_begin, tcp->tcp_suna) || 9452 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9453 continue; 9454 } 9455 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9456 sack_begin, sack_end, 9457 &(tcp->tcp_num_notsack_blk), 9458 &(tcp->tcp_cnt_notsack_list)); 9459 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9460 tcp->tcp_fack = sack_end; 9461 } 9462 } 9463 found |= TCP_OPT_SACK_PRESENT; 9464 continue; 9465 9466 case TCPOPT_TSTAMP: 9467 if (len < TCPOPT_TSTAMP_LEN || 9468 up[1] != TCPOPT_TSTAMP_LEN) 9469 break; 9470 9471 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9472 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9473 9474 found |= TCP_OPT_TSTAMP_PRESENT; 9475 9476 up += TCPOPT_TSTAMP_LEN; 9477 continue; 9478 9479 default: 9480 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9481 break; 9482 up += up[1]; 9483 continue; 9484 } 9485 break; 9486 } 9487 return (found); 9488 } 9489 9490 /* 9491 * Set the mss associated with a particular tcp based on its current value, 9492 * and a new one passed in. Observe minimums and maximums, and reset 9493 * other state variables that we want to view as multiples of mss. 9494 * 9495 * This function is called in various places mainly because 9496 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9497 * other side's SYN/SYN-ACK packet arrives. 9498 * 2) PMTUd may get us a new MSS. 9499 * 3) If the other side stops sending us timestamp option, we need to 9500 * increase the MSS size to use the extra bytes available. 9501 * 9502 * do_ss is used to control whether we will be doing slow start or 9503 * not if there is a change in the mss. Note that for some events like 9504 * tcp_paws_check() we allow the tcp_cwnd to adjust to the new mss but 9505 * do not perform a slow start specifically. 9506 */ 9507 static void 9508 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9509 { 9510 uint32_t mss_max; 9511 tcp_stack_t *tcps = tcp->tcp_tcps; 9512 9513 if (tcp->tcp_ipversion == IPV4_VERSION) 9514 mss_max = tcps->tcps_mss_max_ipv4; 9515 else 9516 mss_max = tcps->tcps_mss_max_ipv6; 9517 9518 if (mss < tcps->tcps_mss_min) 9519 mss = tcps->tcps_mss_min; 9520 if (mss > mss_max) 9521 mss = mss_max; 9522 /* 9523 * Unless naglim has been set by our client to 9524 * a non-mss value, force naglim to track mss. 9525 * This can help to aggregate small writes. 9526 */ 9527 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9528 tcp->tcp_naglim = mss; 9529 /* 9530 * TCP should be able to buffer at least 4 MSS data for obvious 9531 * performance reason. 9532 */ 9533 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9534 tcp->tcp_xmit_hiwater = mss << 2; 9535 9536 /* 9537 * Check if we need to apply the tcp_init_cwnd here. If 9538 * it is set and the MSS gets bigger (should not happen 9539 * normally), we need to adjust the resulting tcp_cwnd properly. 9540 * The new tcp_cwnd should not get bigger. 9541 */ 9542 /* 9543 * We need to avoid setting tcp_cwnd to its slow start value 9544 * unnecessarily. However we have to let the tcp_cwnd adjust 9545 * to the modified mss. 9546 */ 9547 if (tcp->tcp_init_cwnd == 0 && do_ss) { 9548 tcp->tcp_cwnd = MIN(tcps->tcps_slow_start_initial * 9549 mss, MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9550 } else { 9551 if (tcp->tcp_mss < mss) { 9552 tcp->tcp_cwnd = MAX(1, 9553 (tcp->tcp_init_cwnd * tcp->tcp_mss / 9554 mss)) * mss; 9555 } else { 9556 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9557 } 9558 } 9559 tcp->tcp_mss = mss; 9560 tcp->tcp_cwnd_cnt = 0; 9561 (void) tcp_maxpsz_set(tcp, B_TRUE); 9562 } 9563 9564 static int 9565 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9566 { 9567 tcp_t *tcp = NULL; 9568 conn_t *connp; 9569 int err; 9570 dev_t conn_dev; 9571 zoneid_t zoneid; 9572 tcp_stack_t *tcps = NULL; 9573 9574 if (q->q_ptr != NULL) 9575 return (0); 9576 9577 if (!(flag & SO_ACCEPTOR)) { 9578 /* 9579 * Special case for install: miniroot needs to be able to 9580 * access files via NFS as though it were always in the 9581 * global zone. 9582 */ 9583 if (credp == kcred && nfs_global_client_only != 0) { 9584 zoneid = GLOBAL_ZONEID; 9585 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9586 netstack_tcp; 9587 ASSERT(tcps != NULL); 9588 } else { 9589 netstack_t *ns; 9590 9591 ns = netstack_find_by_cred(credp); 9592 ASSERT(ns != NULL); 9593 tcps = ns->netstack_tcp; 9594 ASSERT(tcps != NULL); 9595 9596 /* 9597 * For exclusive stacks we set the zoneid to zero 9598 * to make TCP operate as if in the global zone. 9599 */ 9600 if (tcps->tcps_netstack->netstack_stackid != 9601 GLOBAL_NETSTACKID) 9602 zoneid = GLOBAL_ZONEID; 9603 else 9604 zoneid = crgetzoneid(credp); 9605 } 9606 /* 9607 * For stackid zero this is done from strplumb.c, but 9608 * non-zero stackids are handled here. 9609 */ 9610 if (tcps->tcps_g_q == NULL && 9611 tcps->tcps_netstack->netstack_stackid != 9612 GLOBAL_NETSTACKID) { 9613 tcp_g_q_setup(tcps); 9614 } 9615 } 9616 if (sflag == MODOPEN) { 9617 /* 9618 * This is a special case. The purpose of a modopen 9619 * is to allow just the T_SVR4_OPTMGMT_REQ to pass 9620 * through for MIB browsers. Everything else is failed. 9621 */ 9622 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9623 /* tcp_get_conn incremented refcnt */ 9624 netstack_rele(tcps->tcps_netstack); 9625 9626 if (connp == NULL) 9627 return (ENOMEM); 9628 9629 connp->conn_flags |= IPCL_TCPMOD; 9630 connp->conn_cred = credp; 9631 connp->conn_zoneid = zoneid; 9632 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9633 ASSERT(connp->conn_netstack->netstack_tcp == tcps); 9634 q->q_ptr = WR(q)->q_ptr = connp; 9635 crhold(credp); 9636 q->q_qinfo = &tcp_mod_rinit; 9637 WR(q)->q_qinfo = &tcp_mod_winit; 9638 qprocson(q); 9639 return (0); 9640 } 9641 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 9642 if (tcps != NULL) 9643 netstack_rele(tcps->tcps_netstack); 9644 return (EBUSY); 9645 } 9646 9647 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9648 9649 if (flag & SO_ACCEPTOR) { 9650 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9651 ASSERT(tcps == NULL); 9652 q->q_qinfo = &tcp_acceptor_rinit; 9653 q->q_ptr = (void *)conn_dev; 9654 WR(q)->q_qinfo = &tcp_acceptor_winit; 9655 WR(q)->q_ptr = (void *)conn_dev; 9656 qprocson(q); 9657 return (0); 9658 } 9659 9660 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9661 /* 9662 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9663 * so we drop it by one. 9664 */ 9665 netstack_rele(tcps->tcps_netstack); 9666 if (connp == NULL) { 9667 inet_minor_free(ip_minor_arena, conn_dev); 9668 q->q_ptr = NULL; 9669 return (ENOSR); 9670 } 9671 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9672 tcp = connp->conn_tcp; 9673 9674 q->q_ptr = WR(q)->q_ptr = connp; 9675 if (getmajor(*devp) == TCP6_MAJ) { 9676 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9677 connp->conn_send = ip_output_v6; 9678 connp->conn_af_isv6 = B_TRUE; 9679 connp->conn_pkt_isv6 = B_TRUE; 9680 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9681 tcp->tcp_ipversion = IPV6_VERSION; 9682 tcp->tcp_family = AF_INET6; 9683 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9684 } else { 9685 connp->conn_flags |= IPCL_TCP4; 9686 connp->conn_send = ip_output; 9687 connp->conn_af_isv6 = B_FALSE; 9688 connp->conn_pkt_isv6 = B_FALSE; 9689 tcp->tcp_ipversion = IPV4_VERSION; 9690 tcp->tcp_family = AF_INET; 9691 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9692 } 9693 9694 /* 9695 * TCP keeps a copy of cred for cache locality reasons but 9696 * we put a reference only once. If connp->conn_cred 9697 * becomes invalid, tcp_cred should also be set to NULL. 9698 */ 9699 tcp->tcp_cred = connp->conn_cred = credp; 9700 crhold(connp->conn_cred); 9701 tcp->tcp_cpid = curproc->p_pid; 9702 tcp->tcp_open_time = lbolt64; 9703 connp->conn_zoneid = zoneid; 9704 connp->conn_mlp_type = mlptSingle; 9705 connp->conn_ulp_labeled = !is_system_labeled(); 9706 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9707 ASSERT(tcp->tcp_tcps == tcps); 9708 9709 /* 9710 * If the caller has the process-wide flag set, then default to MAC 9711 * exempt mode. This allows read-down to unlabeled hosts. 9712 */ 9713 if (getpflags(NET_MAC_AWARE, credp) != 0) 9714 connp->conn_mac_exempt = B_TRUE; 9715 9716 connp->conn_dev = conn_dev; 9717 9718 ASSERT(q->q_qinfo == &tcp_rinit); 9719 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9720 9721 if (flag & SO_SOCKSTR) { 9722 /* 9723 * No need to insert a socket in tcp acceptor hash. 9724 * If it was a socket acceptor stream, we dealt with 9725 * it above. A socket listener can never accept a 9726 * connection and doesn't need acceptor_id. 9727 */ 9728 connp->conn_flags |= IPCL_SOCKET; 9729 tcp->tcp_issocket = 1; 9730 WR(q)->q_qinfo = &tcp_sock_winit; 9731 } else { 9732 #ifdef _ILP32 9733 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9734 #else 9735 tcp->tcp_acceptor_id = conn_dev; 9736 #endif /* _ILP32 */ 9737 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9738 } 9739 9740 if (tcps->tcps_trace) 9741 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9742 9743 err = tcp_init(tcp, q); 9744 if (err != 0) { 9745 inet_minor_free(ip_minor_arena, connp->conn_dev); 9746 tcp_acceptor_hash_remove(tcp); 9747 CONN_DEC_REF(connp); 9748 q->q_ptr = WR(q)->q_ptr = NULL; 9749 return (err); 9750 } 9751 9752 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9753 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9754 9755 /* Non-zero default values */ 9756 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9757 /* 9758 * Put the ref for TCP. Ref for IP was already put 9759 * by ipcl_conn_create. Also Make the conn_t globally 9760 * visible to walkers 9761 */ 9762 mutex_enter(&connp->conn_lock); 9763 CONN_INC_REF_LOCKED(connp); 9764 ASSERT(connp->conn_ref == 2); 9765 connp->conn_state_flags &= ~CONN_INCIPIENT; 9766 mutex_exit(&connp->conn_lock); 9767 9768 qprocson(q); 9769 return (0); 9770 } 9771 9772 /* 9773 * Some TCP options can be "set" by requesting them in the option 9774 * buffer. This is needed for XTI feature test though we do not 9775 * allow it in general. We interpret that this mechanism is more 9776 * applicable to OSI protocols and need not be allowed in general. 9777 * This routine filters out options for which it is not allowed (most) 9778 * and lets through those (few) for which it is. [ The XTI interface 9779 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9780 * ever implemented will have to be allowed here ]. 9781 */ 9782 static boolean_t 9783 tcp_allow_connopt_set(int level, int name) 9784 { 9785 9786 switch (level) { 9787 case IPPROTO_TCP: 9788 switch (name) { 9789 case TCP_NODELAY: 9790 return (B_TRUE); 9791 default: 9792 return (B_FALSE); 9793 } 9794 /*NOTREACHED*/ 9795 default: 9796 return (B_FALSE); 9797 } 9798 /*NOTREACHED*/ 9799 } 9800 9801 /* 9802 * This routine gets default values of certain options whose default 9803 * values are maintained by protocol specific code 9804 */ 9805 /* ARGSUSED */ 9806 int 9807 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9808 { 9809 int32_t *i1 = (int32_t *)ptr; 9810 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9811 9812 switch (level) { 9813 case IPPROTO_TCP: 9814 switch (name) { 9815 case TCP_NOTIFY_THRESHOLD: 9816 *i1 = tcps->tcps_ip_notify_interval; 9817 break; 9818 case TCP_ABORT_THRESHOLD: 9819 *i1 = tcps->tcps_ip_abort_interval; 9820 break; 9821 case TCP_CONN_NOTIFY_THRESHOLD: 9822 *i1 = tcps->tcps_ip_notify_cinterval; 9823 break; 9824 case TCP_CONN_ABORT_THRESHOLD: 9825 *i1 = tcps->tcps_ip_abort_cinterval; 9826 break; 9827 default: 9828 return (-1); 9829 } 9830 break; 9831 case IPPROTO_IP: 9832 switch (name) { 9833 case IP_TTL: 9834 *i1 = tcps->tcps_ipv4_ttl; 9835 break; 9836 default: 9837 return (-1); 9838 } 9839 break; 9840 case IPPROTO_IPV6: 9841 switch (name) { 9842 case IPV6_UNICAST_HOPS: 9843 *i1 = tcps->tcps_ipv6_hoplimit; 9844 break; 9845 default: 9846 return (-1); 9847 } 9848 break; 9849 default: 9850 return (-1); 9851 } 9852 return (sizeof (int)); 9853 } 9854 9855 9856 /* 9857 * TCP routine to get the values of options. 9858 */ 9859 int 9860 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9861 { 9862 int *i1 = (int *)ptr; 9863 conn_t *connp = Q_TO_CONN(q); 9864 tcp_t *tcp = connp->conn_tcp; 9865 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9866 9867 switch (level) { 9868 case SOL_SOCKET: 9869 switch (name) { 9870 case SO_LINGER: { 9871 struct linger *lgr = (struct linger *)ptr; 9872 9873 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9874 lgr->l_linger = tcp->tcp_lingertime; 9875 } 9876 return (sizeof (struct linger)); 9877 case SO_DEBUG: 9878 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9879 break; 9880 case SO_KEEPALIVE: 9881 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9882 break; 9883 case SO_DONTROUTE: 9884 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9885 break; 9886 case SO_USELOOPBACK: 9887 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9888 break; 9889 case SO_BROADCAST: 9890 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9891 break; 9892 case SO_REUSEADDR: 9893 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9894 break; 9895 case SO_OOBINLINE: 9896 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9897 break; 9898 case SO_DGRAM_ERRIND: 9899 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9900 break; 9901 case SO_TYPE: 9902 *i1 = SOCK_STREAM; 9903 break; 9904 case SO_SNDBUF: 9905 *i1 = tcp->tcp_xmit_hiwater; 9906 break; 9907 case SO_RCVBUF: 9908 *i1 = RD(q)->q_hiwat; 9909 break; 9910 case SO_SND_COPYAVOID: 9911 *i1 = tcp->tcp_snd_zcopy_on ? 9912 SO_SND_COPYAVOID : 0; 9913 break; 9914 case SO_ALLZONES: 9915 *i1 = connp->conn_allzones ? 1 : 0; 9916 break; 9917 case SO_ANON_MLP: 9918 *i1 = connp->conn_anon_mlp; 9919 break; 9920 case SO_MAC_EXEMPT: 9921 *i1 = connp->conn_mac_exempt; 9922 break; 9923 case SO_EXCLBIND: 9924 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9925 break; 9926 case SO_PROTOTYPE: 9927 *i1 = IPPROTO_TCP; 9928 break; 9929 case SO_DOMAIN: 9930 *i1 = tcp->tcp_family; 9931 break; 9932 default: 9933 return (-1); 9934 } 9935 break; 9936 case IPPROTO_TCP: 9937 switch (name) { 9938 case TCP_NODELAY: 9939 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9940 break; 9941 case TCP_MAXSEG: 9942 *i1 = tcp->tcp_mss; 9943 break; 9944 case TCP_NOTIFY_THRESHOLD: 9945 *i1 = (int)tcp->tcp_first_timer_threshold; 9946 break; 9947 case TCP_ABORT_THRESHOLD: 9948 *i1 = tcp->tcp_second_timer_threshold; 9949 break; 9950 case TCP_CONN_NOTIFY_THRESHOLD: 9951 *i1 = tcp->tcp_first_ctimer_threshold; 9952 break; 9953 case TCP_CONN_ABORT_THRESHOLD: 9954 *i1 = tcp->tcp_second_ctimer_threshold; 9955 break; 9956 case TCP_RECVDSTADDR: 9957 *i1 = tcp->tcp_recvdstaddr; 9958 break; 9959 case TCP_ANONPRIVBIND: 9960 *i1 = tcp->tcp_anon_priv_bind; 9961 break; 9962 case TCP_EXCLBIND: 9963 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9964 break; 9965 case TCP_INIT_CWND: 9966 *i1 = tcp->tcp_init_cwnd; 9967 break; 9968 case TCP_KEEPALIVE_THRESHOLD: 9969 *i1 = tcp->tcp_ka_interval; 9970 break; 9971 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9972 *i1 = tcp->tcp_ka_abort_thres; 9973 break; 9974 case TCP_CORK: 9975 *i1 = tcp->tcp_cork; 9976 break; 9977 default: 9978 return (-1); 9979 } 9980 break; 9981 case IPPROTO_IP: 9982 if (tcp->tcp_family != AF_INET) 9983 return (-1); 9984 switch (name) { 9985 case IP_OPTIONS: 9986 case T_IP_OPTIONS: { 9987 /* 9988 * This is compatible with BSD in that in only return 9989 * the reverse source route with the final destination 9990 * as the last entry. The first 4 bytes of the option 9991 * will contain the final destination. 9992 */ 9993 int opt_len; 9994 9995 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9996 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9997 ASSERT(opt_len >= 0); 9998 /* Caller ensures enough space */ 9999 if (opt_len > 0) { 10000 /* 10001 * TODO: Do we have to handle getsockopt on an 10002 * initiator as well? 10003 */ 10004 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 10005 } 10006 return (0); 10007 } 10008 case IP_TOS: 10009 case T_IP_TOS: 10010 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10011 break; 10012 case IP_TTL: 10013 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10014 break; 10015 case IP_NEXTHOP: 10016 /* Handled at IP level */ 10017 return (-EINVAL); 10018 default: 10019 return (-1); 10020 } 10021 break; 10022 case IPPROTO_IPV6: 10023 /* 10024 * IPPROTO_IPV6 options are only supported for sockets 10025 * that are using IPv6 on the wire. 10026 */ 10027 if (tcp->tcp_ipversion != IPV6_VERSION) { 10028 return (-1); 10029 } 10030 switch (name) { 10031 case IPV6_UNICAST_HOPS: 10032 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10033 break; /* goto sizeof (int) option return */ 10034 case IPV6_BOUND_IF: 10035 /* Zero if not set */ 10036 *i1 = tcp->tcp_bound_if; 10037 break; /* goto sizeof (int) option return */ 10038 case IPV6_RECVPKTINFO: 10039 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10040 *i1 = 1; 10041 else 10042 *i1 = 0; 10043 break; /* goto sizeof (int) option return */ 10044 case IPV6_RECVTCLASS: 10045 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10046 *i1 = 1; 10047 else 10048 *i1 = 0; 10049 break; /* goto sizeof (int) option return */ 10050 case IPV6_RECVHOPLIMIT: 10051 if (tcp->tcp_ipv6_recvancillary & 10052 TCP_IPV6_RECVHOPLIMIT) 10053 *i1 = 1; 10054 else 10055 *i1 = 0; 10056 break; /* goto sizeof (int) option return */ 10057 case IPV6_RECVHOPOPTS: 10058 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10059 *i1 = 1; 10060 else 10061 *i1 = 0; 10062 break; /* goto sizeof (int) option return */ 10063 case IPV6_RECVDSTOPTS: 10064 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10065 *i1 = 1; 10066 else 10067 *i1 = 0; 10068 break; /* goto sizeof (int) option return */ 10069 case _OLD_IPV6_RECVDSTOPTS: 10070 if (tcp->tcp_ipv6_recvancillary & 10071 TCP_OLD_IPV6_RECVDSTOPTS) 10072 *i1 = 1; 10073 else 10074 *i1 = 0; 10075 break; /* goto sizeof (int) option return */ 10076 case IPV6_RECVRTHDR: 10077 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10078 *i1 = 1; 10079 else 10080 *i1 = 0; 10081 break; /* goto sizeof (int) option return */ 10082 case IPV6_RECVRTHDRDSTOPTS: 10083 if (tcp->tcp_ipv6_recvancillary & 10084 TCP_IPV6_RECVRTDSTOPTS) 10085 *i1 = 1; 10086 else 10087 *i1 = 0; 10088 break; /* goto sizeof (int) option return */ 10089 case IPV6_PKTINFO: { 10090 /* XXX assumes that caller has room for max size! */ 10091 struct in6_pktinfo *pkti; 10092 10093 pkti = (struct in6_pktinfo *)ptr; 10094 if (ipp->ipp_fields & IPPF_IFINDEX) 10095 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10096 else 10097 pkti->ipi6_ifindex = 0; 10098 if (ipp->ipp_fields & IPPF_ADDR) 10099 pkti->ipi6_addr = ipp->ipp_addr; 10100 else 10101 pkti->ipi6_addr = ipv6_all_zeros; 10102 return (sizeof (struct in6_pktinfo)); 10103 } 10104 case IPV6_TCLASS: 10105 if (ipp->ipp_fields & IPPF_TCLASS) 10106 *i1 = ipp->ipp_tclass; 10107 else 10108 *i1 = IPV6_FLOW_TCLASS( 10109 IPV6_DEFAULT_VERS_AND_FLOW); 10110 break; /* goto sizeof (int) option return */ 10111 case IPV6_NEXTHOP: { 10112 sin6_t *sin6 = (sin6_t *)ptr; 10113 10114 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10115 return (0); 10116 *sin6 = sin6_null; 10117 sin6->sin6_family = AF_INET6; 10118 sin6->sin6_addr = ipp->ipp_nexthop; 10119 return (sizeof (sin6_t)); 10120 } 10121 case IPV6_HOPOPTS: 10122 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10123 return (0); 10124 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10125 return (0); 10126 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10127 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10128 if (tcp->tcp_label_len > 0) { 10129 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10130 ptr[1] = (ipp->ipp_hopoptslen - 10131 tcp->tcp_label_len + 7) / 8 - 1; 10132 } 10133 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10134 case IPV6_RTHDRDSTOPTS: 10135 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10136 return (0); 10137 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10138 return (ipp->ipp_rtdstoptslen); 10139 case IPV6_RTHDR: 10140 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10141 return (0); 10142 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10143 return (ipp->ipp_rthdrlen); 10144 case IPV6_DSTOPTS: 10145 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10146 return (0); 10147 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10148 return (ipp->ipp_dstoptslen); 10149 case IPV6_SRC_PREFERENCES: 10150 return (ip6_get_src_preferences(connp, 10151 (uint32_t *)ptr)); 10152 case IPV6_PATHMTU: { 10153 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10154 10155 if (tcp->tcp_state < TCPS_ESTABLISHED) 10156 return (-1); 10157 10158 return (ip_fill_mtuinfo(&connp->conn_remv6, 10159 connp->conn_fport, mtuinfo, 10160 connp->conn_netstack)); 10161 } 10162 default: 10163 return (-1); 10164 } 10165 break; 10166 default: 10167 return (-1); 10168 } 10169 return (sizeof (int)); 10170 } 10171 10172 /* 10173 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10174 * Parameters are assumed to be verified by the caller. 10175 */ 10176 /* ARGSUSED */ 10177 int 10178 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10179 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10180 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10181 { 10182 conn_t *connp = Q_TO_CONN(q); 10183 tcp_t *tcp = connp->conn_tcp; 10184 int *i1 = (int *)invalp; 10185 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10186 boolean_t checkonly; 10187 int reterr; 10188 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10189 10190 switch (optset_context) { 10191 case SETFN_OPTCOM_CHECKONLY: 10192 checkonly = B_TRUE; 10193 /* 10194 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10195 * inlen != 0 implies value supplied and 10196 * we have to "pretend" to set it. 10197 * inlen == 0 implies that there is no 10198 * value part in T_CHECK request and just validation 10199 * done elsewhere should be enough, we just return here. 10200 */ 10201 if (inlen == 0) { 10202 *outlenp = 0; 10203 return (0); 10204 } 10205 break; 10206 case SETFN_OPTCOM_NEGOTIATE: 10207 checkonly = B_FALSE; 10208 break; 10209 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10210 case SETFN_CONN_NEGOTIATE: 10211 checkonly = B_FALSE; 10212 /* 10213 * Negotiating local and "association-related" options 10214 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10215 * primitives is allowed by XTI, but we choose 10216 * to not implement this style negotiation for Internet 10217 * protocols (We interpret it is a must for OSI world but 10218 * optional for Internet protocols) for all options. 10219 * [ Will do only for the few options that enable test 10220 * suites that our XTI implementation of this feature 10221 * works for transports that do allow it ] 10222 */ 10223 if (!tcp_allow_connopt_set(level, name)) { 10224 *outlenp = 0; 10225 return (EINVAL); 10226 } 10227 break; 10228 default: 10229 /* 10230 * We should never get here 10231 */ 10232 *outlenp = 0; 10233 return (EINVAL); 10234 } 10235 10236 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10237 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10238 10239 /* 10240 * For TCP, we should have no ancillary data sent down 10241 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10242 * has to be zero. 10243 */ 10244 ASSERT(thisdg_attrs == NULL); 10245 10246 /* 10247 * For fixed length options, no sanity check 10248 * of passed in length is done. It is assumed *_optcom_req() 10249 * routines do the right thing. 10250 */ 10251 10252 switch (level) { 10253 case SOL_SOCKET: 10254 switch (name) { 10255 case SO_LINGER: { 10256 struct linger *lgr = (struct linger *)invalp; 10257 10258 if (!checkonly) { 10259 if (lgr->l_onoff) { 10260 tcp->tcp_linger = 1; 10261 tcp->tcp_lingertime = lgr->l_linger; 10262 } else { 10263 tcp->tcp_linger = 0; 10264 tcp->tcp_lingertime = 0; 10265 } 10266 /* struct copy */ 10267 *(struct linger *)outvalp = *lgr; 10268 } else { 10269 if (!lgr->l_onoff) { 10270 ((struct linger *) 10271 outvalp)->l_onoff = 0; 10272 ((struct linger *) 10273 outvalp)->l_linger = 0; 10274 } else { 10275 /* struct copy */ 10276 *(struct linger *)outvalp = *lgr; 10277 } 10278 } 10279 *outlenp = sizeof (struct linger); 10280 return (0); 10281 } 10282 case SO_DEBUG: 10283 if (!checkonly) 10284 tcp->tcp_debug = onoff; 10285 break; 10286 case SO_KEEPALIVE: 10287 if (checkonly) { 10288 /* T_CHECK case */ 10289 break; 10290 } 10291 10292 if (!onoff) { 10293 if (tcp->tcp_ka_enabled) { 10294 if (tcp->tcp_ka_tid != 0) { 10295 (void) TCP_TIMER_CANCEL(tcp, 10296 tcp->tcp_ka_tid); 10297 tcp->tcp_ka_tid = 0; 10298 } 10299 tcp->tcp_ka_enabled = 0; 10300 } 10301 break; 10302 } 10303 if (!tcp->tcp_ka_enabled) { 10304 /* Crank up the keepalive timer */ 10305 tcp->tcp_ka_last_intrvl = 0; 10306 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10307 tcp_keepalive_killer, 10308 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10309 tcp->tcp_ka_enabled = 1; 10310 } 10311 break; 10312 case SO_DONTROUTE: 10313 /* 10314 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10315 * only of interest to IP. We track them here only so 10316 * that we can report their current value. 10317 */ 10318 if (!checkonly) { 10319 tcp->tcp_dontroute = onoff; 10320 tcp->tcp_connp->conn_dontroute = onoff; 10321 } 10322 break; 10323 case SO_USELOOPBACK: 10324 if (!checkonly) { 10325 tcp->tcp_useloopback = onoff; 10326 tcp->tcp_connp->conn_loopback = onoff; 10327 } 10328 break; 10329 case SO_BROADCAST: 10330 if (!checkonly) { 10331 tcp->tcp_broadcast = onoff; 10332 tcp->tcp_connp->conn_broadcast = onoff; 10333 } 10334 break; 10335 case SO_REUSEADDR: 10336 if (!checkonly) { 10337 tcp->tcp_reuseaddr = onoff; 10338 tcp->tcp_connp->conn_reuseaddr = onoff; 10339 } 10340 break; 10341 case SO_OOBINLINE: 10342 if (!checkonly) 10343 tcp->tcp_oobinline = onoff; 10344 break; 10345 case SO_DGRAM_ERRIND: 10346 if (!checkonly) 10347 tcp->tcp_dgram_errind = onoff; 10348 break; 10349 case SO_SNDBUF: { 10350 if (*i1 > tcps->tcps_max_buf) { 10351 *outlenp = 0; 10352 return (ENOBUFS); 10353 } 10354 if (checkonly) 10355 break; 10356 10357 tcp->tcp_xmit_hiwater = *i1; 10358 if (tcps->tcps_snd_lowat_fraction != 0) 10359 tcp->tcp_xmit_lowater = 10360 tcp->tcp_xmit_hiwater / 10361 tcps->tcps_snd_lowat_fraction; 10362 (void) tcp_maxpsz_set(tcp, B_TRUE); 10363 /* 10364 * If we are flow-controlled, recheck the condition. 10365 * There are apps that increase SO_SNDBUF size when 10366 * flow-controlled (EWOULDBLOCK), and expect the flow 10367 * control condition to be lifted right away. 10368 */ 10369 mutex_enter(&tcp->tcp_non_sq_lock); 10370 if (tcp->tcp_flow_stopped && 10371 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10372 tcp_clrqfull(tcp); 10373 } 10374 mutex_exit(&tcp->tcp_non_sq_lock); 10375 break; 10376 } 10377 case SO_RCVBUF: 10378 if (*i1 > tcps->tcps_max_buf) { 10379 *outlenp = 0; 10380 return (ENOBUFS); 10381 } 10382 /* Silently ignore zero */ 10383 if (!checkonly && *i1 != 0) { 10384 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10385 (void) tcp_rwnd_set(tcp, *i1); 10386 } 10387 /* 10388 * XXX should we return the rwnd here 10389 * and tcp_opt_get ? 10390 */ 10391 break; 10392 case SO_SND_COPYAVOID: 10393 if (!checkonly) { 10394 /* we only allow enable at most once for now */ 10395 if (tcp->tcp_loopback || 10396 (!tcp->tcp_snd_zcopy_aware && 10397 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10398 *outlenp = 0; 10399 return (EOPNOTSUPP); 10400 } 10401 tcp->tcp_snd_zcopy_aware = 1; 10402 } 10403 break; 10404 case SO_ALLZONES: 10405 /* Handled at the IP level */ 10406 return (-EINVAL); 10407 case SO_ANON_MLP: 10408 if (!checkonly) { 10409 mutex_enter(&connp->conn_lock); 10410 connp->conn_anon_mlp = onoff; 10411 mutex_exit(&connp->conn_lock); 10412 } 10413 break; 10414 case SO_MAC_EXEMPT: 10415 if (secpolicy_net_mac_aware(cr) != 0 || 10416 IPCL_IS_BOUND(connp)) 10417 return (EACCES); 10418 if (!checkonly) { 10419 mutex_enter(&connp->conn_lock); 10420 connp->conn_mac_exempt = onoff; 10421 mutex_exit(&connp->conn_lock); 10422 } 10423 break; 10424 case SO_EXCLBIND: 10425 if (!checkonly) 10426 tcp->tcp_exclbind = onoff; 10427 break; 10428 default: 10429 *outlenp = 0; 10430 return (EINVAL); 10431 } 10432 break; 10433 case IPPROTO_TCP: 10434 switch (name) { 10435 case TCP_NODELAY: 10436 if (!checkonly) 10437 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10438 break; 10439 case TCP_NOTIFY_THRESHOLD: 10440 if (!checkonly) 10441 tcp->tcp_first_timer_threshold = *i1; 10442 break; 10443 case TCP_ABORT_THRESHOLD: 10444 if (!checkonly) 10445 tcp->tcp_second_timer_threshold = *i1; 10446 break; 10447 case TCP_CONN_NOTIFY_THRESHOLD: 10448 if (!checkonly) 10449 tcp->tcp_first_ctimer_threshold = *i1; 10450 break; 10451 case TCP_CONN_ABORT_THRESHOLD: 10452 if (!checkonly) 10453 tcp->tcp_second_ctimer_threshold = *i1; 10454 break; 10455 case TCP_RECVDSTADDR: 10456 if (tcp->tcp_state > TCPS_LISTEN) 10457 return (EOPNOTSUPP); 10458 if (!checkonly) 10459 tcp->tcp_recvdstaddr = onoff; 10460 break; 10461 case TCP_ANONPRIVBIND: 10462 if ((reterr = secpolicy_net_privaddr(cr, 0)) != 0) { 10463 *outlenp = 0; 10464 return (reterr); 10465 } 10466 if (!checkonly) { 10467 tcp->tcp_anon_priv_bind = onoff; 10468 } 10469 break; 10470 case TCP_EXCLBIND: 10471 if (!checkonly) 10472 tcp->tcp_exclbind = onoff; 10473 break; /* goto sizeof (int) option return */ 10474 case TCP_INIT_CWND: { 10475 uint32_t init_cwnd = *((uint32_t *)invalp); 10476 10477 if (checkonly) 10478 break; 10479 10480 /* 10481 * Only allow socket with network configuration 10482 * privilege to set the initial cwnd to be larger 10483 * than allowed by RFC 3390. 10484 */ 10485 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10486 tcp->tcp_init_cwnd = init_cwnd; 10487 break; 10488 } 10489 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10490 *outlenp = 0; 10491 return (reterr); 10492 } 10493 if (init_cwnd > TCP_MAX_INIT_CWND) { 10494 *outlenp = 0; 10495 return (EINVAL); 10496 } 10497 tcp->tcp_init_cwnd = init_cwnd; 10498 break; 10499 } 10500 case TCP_KEEPALIVE_THRESHOLD: 10501 if (checkonly) 10502 break; 10503 10504 if (*i1 < tcps->tcps_keepalive_interval_low || 10505 *i1 > tcps->tcps_keepalive_interval_high) { 10506 *outlenp = 0; 10507 return (EINVAL); 10508 } 10509 if (*i1 != tcp->tcp_ka_interval) { 10510 tcp->tcp_ka_interval = *i1; 10511 /* 10512 * Check if we need to restart the 10513 * keepalive timer. 10514 */ 10515 if (tcp->tcp_ka_tid != 0) { 10516 ASSERT(tcp->tcp_ka_enabled); 10517 (void) TCP_TIMER_CANCEL(tcp, 10518 tcp->tcp_ka_tid); 10519 tcp->tcp_ka_last_intrvl = 0; 10520 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10521 tcp_keepalive_killer, 10522 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10523 } 10524 } 10525 break; 10526 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10527 if (!checkonly) { 10528 if (*i1 < 10529 tcps->tcps_keepalive_abort_interval_low || 10530 *i1 > 10531 tcps->tcps_keepalive_abort_interval_high) { 10532 *outlenp = 0; 10533 return (EINVAL); 10534 } 10535 tcp->tcp_ka_abort_thres = *i1; 10536 } 10537 break; 10538 case TCP_CORK: 10539 if (!checkonly) { 10540 /* 10541 * if tcp->tcp_cork was set and is now 10542 * being unset, we have to make sure that 10543 * the remaining data gets sent out. Also 10544 * unset tcp->tcp_cork so that tcp_wput_data() 10545 * can send data even if it is less than mss 10546 */ 10547 if (tcp->tcp_cork && onoff == 0 && 10548 tcp->tcp_unsent > 0) { 10549 tcp->tcp_cork = B_FALSE; 10550 tcp_wput_data(tcp, NULL, B_FALSE); 10551 } 10552 tcp->tcp_cork = onoff; 10553 } 10554 break; 10555 default: 10556 *outlenp = 0; 10557 return (EINVAL); 10558 } 10559 break; 10560 case IPPROTO_IP: 10561 if (tcp->tcp_family != AF_INET) { 10562 *outlenp = 0; 10563 return (ENOPROTOOPT); 10564 } 10565 switch (name) { 10566 case IP_OPTIONS: 10567 case T_IP_OPTIONS: 10568 reterr = tcp_opt_set_header(tcp, checkonly, 10569 invalp, inlen); 10570 if (reterr) { 10571 *outlenp = 0; 10572 return (reterr); 10573 } 10574 /* OK return - copy input buffer into output buffer */ 10575 if (invalp != outvalp) { 10576 /* don't trust bcopy for identical src/dst */ 10577 bcopy(invalp, outvalp, inlen); 10578 } 10579 *outlenp = inlen; 10580 return (0); 10581 case IP_TOS: 10582 case T_IP_TOS: 10583 if (!checkonly) { 10584 tcp->tcp_ipha->ipha_type_of_service = 10585 (uchar_t)*i1; 10586 tcp->tcp_tos = (uchar_t)*i1; 10587 } 10588 break; 10589 case IP_TTL: 10590 if (!checkonly) { 10591 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10592 tcp->tcp_ttl = (uchar_t)*i1; 10593 } 10594 break; 10595 case IP_BOUND_IF: 10596 case IP_NEXTHOP: 10597 /* Handled at the IP level */ 10598 return (-EINVAL); 10599 case IP_SEC_OPT: 10600 /* 10601 * We should not allow policy setting after 10602 * we start listening for connections. 10603 */ 10604 if (tcp->tcp_state == TCPS_LISTEN) { 10605 return (EINVAL); 10606 } else { 10607 /* Handled at the IP level */ 10608 return (-EINVAL); 10609 } 10610 default: 10611 *outlenp = 0; 10612 return (EINVAL); 10613 } 10614 break; 10615 case IPPROTO_IPV6: { 10616 ip6_pkt_t *ipp; 10617 10618 /* 10619 * IPPROTO_IPV6 options are only supported for sockets 10620 * that are using IPv6 on the wire. 10621 */ 10622 if (tcp->tcp_ipversion != IPV6_VERSION) { 10623 *outlenp = 0; 10624 return (ENOPROTOOPT); 10625 } 10626 /* 10627 * Only sticky options; no ancillary data 10628 */ 10629 ASSERT(thisdg_attrs == NULL); 10630 ipp = &tcp->tcp_sticky_ipp; 10631 10632 switch (name) { 10633 case IPV6_UNICAST_HOPS: 10634 /* -1 means use default */ 10635 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10636 *outlenp = 0; 10637 return (EINVAL); 10638 } 10639 if (!checkonly) { 10640 if (*i1 == -1) { 10641 tcp->tcp_ip6h->ip6_hops = 10642 ipp->ipp_unicast_hops = 10643 (uint8_t)tcps->tcps_ipv6_hoplimit; 10644 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10645 /* Pass modified value to IP. */ 10646 *i1 = tcp->tcp_ip6h->ip6_hops; 10647 } else { 10648 tcp->tcp_ip6h->ip6_hops = 10649 ipp->ipp_unicast_hops = 10650 (uint8_t)*i1; 10651 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10652 } 10653 reterr = tcp_build_hdrs(q, tcp); 10654 if (reterr != 0) 10655 return (reterr); 10656 } 10657 break; 10658 case IPV6_BOUND_IF: 10659 if (!checkonly) { 10660 int error = 0; 10661 10662 tcp->tcp_bound_if = *i1; 10663 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10664 B_TRUE, checkonly, level, name, mblk); 10665 if (error != 0) { 10666 *outlenp = 0; 10667 return (error); 10668 } 10669 } 10670 break; 10671 /* 10672 * Set boolean switches for ancillary data delivery 10673 */ 10674 case IPV6_RECVPKTINFO: 10675 if (!checkonly) { 10676 if (onoff) 10677 tcp->tcp_ipv6_recvancillary |= 10678 TCP_IPV6_RECVPKTINFO; 10679 else 10680 tcp->tcp_ipv6_recvancillary &= 10681 ~TCP_IPV6_RECVPKTINFO; 10682 /* Force it to be sent up with the next msg */ 10683 tcp->tcp_recvifindex = 0; 10684 } 10685 break; 10686 case IPV6_RECVTCLASS: 10687 if (!checkonly) { 10688 if (onoff) 10689 tcp->tcp_ipv6_recvancillary |= 10690 TCP_IPV6_RECVTCLASS; 10691 else 10692 tcp->tcp_ipv6_recvancillary &= 10693 ~TCP_IPV6_RECVTCLASS; 10694 } 10695 break; 10696 case IPV6_RECVHOPLIMIT: 10697 if (!checkonly) { 10698 if (onoff) 10699 tcp->tcp_ipv6_recvancillary |= 10700 TCP_IPV6_RECVHOPLIMIT; 10701 else 10702 tcp->tcp_ipv6_recvancillary &= 10703 ~TCP_IPV6_RECVHOPLIMIT; 10704 /* Force it to be sent up with the next msg */ 10705 tcp->tcp_recvhops = 0xffffffffU; 10706 } 10707 break; 10708 case IPV6_RECVHOPOPTS: 10709 if (!checkonly) { 10710 if (onoff) 10711 tcp->tcp_ipv6_recvancillary |= 10712 TCP_IPV6_RECVHOPOPTS; 10713 else 10714 tcp->tcp_ipv6_recvancillary &= 10715 ~TCP_IPV6_RECVHOPOPTS; 10716 } 10717 break; 10718 case IPV6_RECVDSTOPTS: 10719 if (!checkonly) { 10720 if (onoff) 10721 tcp->tcp_ipv6_recvancillary |= 10722 TCP_IPV6_RECVDSTOPTS; 10723 else 10724 tcp->tcp_ipv6_recvancillary &= 10725 ~TCP_IPV6_RECVDSTOPTS; 10726 } 10727 break; 10728 case _OLD_IPV6_RECVDSTOPTS: 10729 if (!checkonly) { 10730 if (onoff) 10731 tcp->tcp_ipv6_recvancillary |= 10732 TCP_OLD_IPV6_RECVDSTOPTS; 10733 else 10734 tcp->tcp_ipv6_recvancillary &= 10735 ~TCP_OLD_IPV6_RECVDSTOPTS; 10736 } 10737 break; 10738 case IPV6_RECVRTHDR: 10739 if (!checkonly) { 10740 if (onoff) 10741 tcp->tcp_ipv6_recvancillary |= 10742 TCP_IPV6_RECVRTHDR; 10743 else 10744 tcp->tcp_ipv6_recvancillary &= 10745 ~TCP_IPV6_RECVRTHDR; 10746 } 10747 break; 10748 case IPV6_RECVRTHDRDSTOPTS: 10749 if (!checkonly) { 10750 if (onoff) 10751 tcp->tcp_ipv6_recvancillary |= 10752 TCP_IPV6_RECVRTDSTOPTS; 10753 else 10754 tcp->tcp_ipv6_recvancillary &= 10755 ~TCP_IPV6_RECVRTDSTOPTS; 10756 } 10757 break; 10758 case IPV6_PKTINFO: 10759 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10760 return (EINVAL); 10761 if (checkonly) 10762 break; 10763 10764 if (inlen == 0) { 10765 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10766 } else { 10767 struct in6_pktinfo *pkti; 10768 10769 pkti = (struct in6_pktinfo *)invalp; 10770 /* 10771 * RFC 3542 states that ipi6_addr must be 10772 * the unspecified address when setting the 10773 * IPV6_PKTINFO sticky socket option on a 10774 * TCP socket. 10775 */ 10776 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10777 return (EINVAL); 10778 /* 10779 * ip6_set_pktinfo() validates the source 10780 * address and interface index. 10781 */ 10782 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10783 pkti, mblk); 10784 if (reterr != 0) 10785 return (reterr); 10786 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10787 ipp->ipp_addr = pkti->ipi6_addr; 10788 if (ipp->ipp_ifindex != 0) 10789 ipp->ipp_fields |= IPPF_IFINDEX; 10790 else 10791 ipp->ipp_fields &= ~IPPF_IFINDEX; 10792 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10793 ipp->ipp_fields |= IPPF_ADDR; 10794 else 10795 ipp->ipp_fields &= ~IPPF_ADDR; 10796 } 10797 reterr = tcp_build_hdrs(q, tcp); 10798 if (reterr != 0) 10799 return (reterr); 10800 break; 10801 case IPV6_TCLASS: 10802 if (inlen != 0 && inlen != sizeof (int)) 10803 return (EINVAL); 10804 if (checkonly) 10805 break; 10806 10807 if (inlen == 0) { 10808 ipp->ipp_fields &= ~IPPF_TCLASS; 10809 } else { 10810 if (*i1 > 255 || *i1 < -1) 10811 return (EINVAL); 10812 if (*i1 == -1) { 10813 ipp->ipp_tclass = 0; 10814 *i1 = 0; 10815 } else { 10816 ipp->ipp_tclass = *i1; 10817 } 10818 ipp->ipp_fields |= IPPF_TCLASS; 10819 } 10820 reterr = tcp_build_hdrs(q, tcp); 10821 if (reterr != 0) 10822 return (reterr); 10823 break; 10824 case IPV6_NEXTHOP: 10825 /* 10826 * IP will verify that the nexthop is reachable 10827 * and fail for sticky options. 10828 */ 10829 if (inlen != 0 && inlen != sizeof (sin6_t)) 10830 return (EINVAL); 10831 if (checkonly) 10832 break; 10833 10834 if (inlen == 0) { 10835 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10836 } else { 10837 sin6_t *sin6 = (sin6_t *)invalp; 10838 10839 if (sin6->sin6_family != AF_INET6) 10840 return (EAFNOSUPPORT); 10841 if (IN6_IS_ADDR_V4MAPPED( 10842 &sin6->sin6_addr)) 10843 return (EADDRNOTAVAIL); 10844 ipp->ipp_nexthop = sin6->sin6_addr; 10845 if (!IN6_IS_ADDR_UNSPECIFIED( 10846 &ipp->ipp_nexthop)) 10847 ipp->ipp_fields |= IPPF_NEXTHOP; 10848 else 10849 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10850 } 10851 reterr = tcp_build_hdrs(q, tcp); 10852 if (reterr != 0) 10853 return (reterr); 10854 break; 10855 case IPV6_HOPOPTS: { 10856 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10857 10858 /* 10859 * Sanity checks - minimum size, size a multiple of 10860 * eight bytes, and matching size passed in. 10861 */ 10862 if (inlen != 0 && 10863 inlen != (8 * (hopts->ip6h_len + 1))) 10864 return (EINVAL); 10865 10866 if (checkonly) 10867 break; 10868 10869 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10870 (uchar_t **)&ipp->ipp_hopopts, 10871 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10872 if (reterr != 0) 10873 return (reterr); 10874 if (ipp->ipp_hopoptslen == 0) 10875 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10876 else 10877 ipp->ipp_fields |= IPPF_HOPOPTS; 10878 reterr = tcp_build_hdrs(q, tcp); 10879 if (reterr != 0) 10880 return (reterr); 10881 break; 10882 } 10883 case IPV6_RTHDRDSTOPTS: { 10884 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10885 10886 /* 10887 * Sanity checks - minimum size, size a multiple of 10888 * eight bytes, and matching size passed in. 10889 */ 10890 if (inlen != 0 && 10891 inlen != (8 * (dopts->ip6d_len + 1))) 10892 return (EINVAL); 10893 10894 if (checkonly) 10895 break; 10896 10897 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10898 (uchar_t **)&ipp->ipp_rtdstopts, 10899 &ipp->ipp_rtdstoptslen, 0); 10900 if (reterr != 0) 10901 return (reterr); 10902 if (ipp->ipp_rtdstoptslen == 0) 10903 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10904 else 10905 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10906 reterr = tcp_build_hdrs(q, tcp); 10907 if (reterr != 0) 10908 return (reterr); 10909 break; 10910 } 10911 case IPV6_DSTOPTS: { 10912 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10913 10914 /* 10915 * Sanity checks - minimum size, size a multiple of 10916 * eight bytes, and matching size passed in. 10917 */ 10918 if (inlen != 0 && 10919 inlen != (8 * (dopts->ip6d_len + 1))) 10920 return (EINVAL); 10921 10922 if (checkonly) 10923 break; 10924 10925 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10926 (uchar_t **)&ipp->ipp_dstopts, 10927 &ipp->ipp_dstoptslen, 0); 10928 if (reterr != 0) 10929 return (reterr); 10930 if (ipp->ipp_dstoptslen == 0) 10931 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10932 else 10933 ipp->ipp_fields |= IPPF_DSTOPTS; 10934 reterr = tcp_build_hdrs(q, tcp); 10935 if (reterr != 0) 10936 return (reterr); 10937 break; 10938 } 10939 case IPV6_RTHDR: { 10940 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10941 10942 /* 10943 * Sanity checks - minimum size, size a multiple of 10944 * eight bytes, and matching size passed in. 10945 */ 10946 if (inlen != 0 && 10947 inlen != (8 * (rt->ip6r_len + 1))) 10948 return (EINVAL); 10949 10950 if (checkonly) 10951 break; 10952 10953 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10954 (uchar_t **)&ipp->ipp_rthdr, 10955 &ipp->ipp_rthdrlen, 0); 10956 if (reterr != 0) 10957 return (reterr); 10958 if (ipp->ipp_rthdrlen == 0) 10959 ipp->ipp_fields &= ~IPPF_RTHDR; 10960 else 10961 ipp->ipp_fields |= IPPF_RTHDR; 10962 reterr = tcp_build_hdrs(q, tcp); 10963 if (reterr != 0) 10964 return (reterr); 10965 break; 10966 } 10967 case IPV6_V6ONLY: 10968 if (!checkonly) 10969 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10970 break; 10971 case IPV6_USE_MIN_MTU: 10972 if (inlen != sizeof (int)) 10973 return (EINVAL); 10974 10975 if (*i1 < -1 || *i1 > 1) 10976 return (EINVAL); 10977 10978 if (checkonly) 10979 break; 10980 10981 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10982 ipp->ipp_use_min_mtu = *i1; 10983 break; 10984 case IPV6_BOUND_PIF: 10985 /* Handled at the IP level */ 10986 return (-EINVAL); 10987 case IPV6_SEC_OPT: 10988 /* 10989 * We should not allow policy setting after 10990 * we start listening for connections. 10991 */ 10992 if (tcp->tcp_state == TCPS_LISTEN) { 10993 return (EINVAL); 10994 } else { 10995 /* Handled at the IP level */ 10996 return (-EINVAL); 10997 } 10998 case IPV6_SRC_PREFERENCES: 10999 if (inlen != sizeof (uint32_t)) 11000 return (EINVAL); 11001 reterr = ip6_set_src_preferences(tcp->tcp_connp, 11002 *(uint32_t *)invalp); 11003 if (reterr != 0) { 11004 *outlenp = 0; 11005 return (reterr); 11006 } 11007 break; 11008 default: 11009 *outlenp = 0; 11010 return (EINVAL); 11011 } 11012 break; 11013 } /* end IPPROTO_IPV6 */ 11014 default: 11015 *outlenp = 0; 11016 return (EINVAL); 11017 } 11018 /* 11019 * Common case of OK return with outval same as inval 11020 */ 11021 if (invalp != outvalp) { 11022 /* don't trust bcopy for identical src/dst */ 11023 (void) bcopy(invalp, outvalp, inlen); 11024 } 11025 *outlenp = inlen; 11026 return (0); 11027 } 11028 11029 /* 11030 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 11031 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 11032 * headers, and the maximum size tcp header (to avoid reallocation 11033 * on the fly for additional tcp options). 11034 * Returns failure if can't allocate memory. 11035 */ 11036 static int 11037 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 11038 { 11039 char *hdrs; 11040 uint_t hdrs_len; 11041 ip6i_t *ip6i; 11042 char buf[TCP_MAX_HDR_LENGTH]; 11043 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 11044 in6_addr_t src, dst; 11045 tcp_stack_t *tcps = tcp->tcp_tcps; 11046 11047 /* 11048 * save the existing tcp header and source/dest IP addresses 11049 */ 11050 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 11051 src = tcp->tcp_ip6h->ip6_src; 11052 dst = tcp->tcp_ip6h->ip6_dst; 11053 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 11054 ASSERT(hdrs_len != 0); 11055 if (hdrs_len > tcp->tcp_iphc_len) { 11056 /* Need to reallocate */ 11057 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 11058 if (hdrs == NULL) 11059 return (ENOMEM); 11060 if (tcp->tcp_iphc != NULL) { 11061 if (tcp->tcp_hdr_grown) { 11062 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 11063 } else { 11064 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 11065 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 11066 } 11067 tcp->tcp_iphc_len = 0; 11068 } 11069 ASSERT(tcp->tcp_iphc_len == 0); 11070 tcp->tcp_iphc = hdrs; 11071 tcp->tcp_iphc_len = hdrs_len; 11072 tcp->tcp_hdr_grown = B_TRUE; 11073 } 11074 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11075 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11076 11077 /* Set header fields not in ipp */ 11078 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11079 ip6i = (ip6i_t *)tcp->tcp_iphc; 11080 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11081 } else { 11082 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11083 } 11084 /* 11085 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11086 * 11087 * tcp->tcp_tcp_hdr_len doesn't change here. 11088 */ 11089 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11090 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11091 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11092 11093 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11094 11095 tcp->tcp_ip6h->ip6_src = src; 11096 tcp->tcp_ip6h->ip6_dst = dst; 11097 11098 /* 11099 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11100 * the default value for TCP. 11101 */ 11102 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11103 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 11104 11105 /* 11106 * If we're setting extension headers after a connection 11107 * has been established, and if we have a routing header 11108 * among the extension headers, call ip_massage_options_v6 to 11109 * manipulate the routing header/ip6_dst set the checksum 11110 * difference in the tcp header template. 11111 * (This happens in tcp_connect_ipv6 if the routing header 11112 * is set prior to the connect.) 11113 * Set the tcp_sum to zero first in case we've cleared a 11114 * routing header or don't have one at all. 11115 */ 11116 tcp->tcp_sum = 0; 11117 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11118 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11119 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11120 (uint8_t *)tcp->tcp_tcph); 11121 if (rth != NULL) { 11122 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11123 rth, tcps->tcps_netstack); 11124 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11125 (tcp->tcp_sum >> 16)); 11126 } 11127 } 11128 11129 /* Try to get everything in a single mblk */ 11130 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcps->tcps_wroff_xtra); 11131 return (0); 11132 } 11133 11134 /* 11135 * Transfer any source route option from ipha to buf/dst in reversed form. 11136 */ 11137 static int 11138 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11139 { 11140 ipoptp_t opts; 11141 uchar_t *opt; 11142 uint8_t optval; 11143 uint8_t optlen; 11144 uint32_t len = 0; 11145 11146 for (optval = ipoptp_first(&opts, ipha); 11147 optval != IPOPT_EOL; 11148 optval = ipoptp_next(&opts)) { 11149 opt = opts.ipoptp_cur; 11150 optlen = opts.ipoptp_len; 11151 switch (optval) { 11152 int off1, off2; 11153 case IPOPT_SSRR: 11154 case IPOPT_LSRR: 11155 11156 /* Reverse source route */ 11157 /* 11158 * First entry should be the next to last one in the 11159 * current source route (the last entry is our 11160 * address.) 11161 * The last entry should be the final destination. 11162 */ 11163 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11164 buf[IPOPT_OLEN] = (uint8_t)optlen; 11165 off1 = IPOPT_MINOFF_SR - 1; 11166 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11167 if (off2 < 0) { 11168 /* No entries in source route */ 11169 break; 11170 } 11171 bcopy(opt + off2, dst, IP_ADDR_LEN); 11172 /* 11173 * Note: use src since ipha has not had its src 11174 * and dst reversed (it is in the state it was 11175 * received. 11176 */ 11177 bcopy(&ipha->ipha_src, buf + off2, 11178 IP_ADDR_LEN); 11179 off2 -= IP_ADDR_LEN; 11180 11181 while (off2 > 0) { 11182 bcopy(opt + off2, buf + off1, 11183 IP_ADDR_LEN); 11184 off1 += IP_ADDR_LEN; 11185 off2 -= IP_ADDR_LEN; 11186 } 11187 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11188 buf += optlen; 11189 len += optlen; 11190 break; 11191 } 11192 } 11193 done: 11194 /* Pad the resulting options */ 11195 while (len & 0x3) { 11196 *buf++ = IPOPT_EOL; 11197 len++; 11198 } 11199 return (len); 11200 } 11201 11202 11203 /* 11204 * Extract and revert a source route from ipha (if any) 11205 * and then update the relevant fields in both tcp_t and the standard header. 11206 */ 11207 static void 11208 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11209 { 11210 char buf[TCP_MAX_HDR_LENGTH]; 11211 uint_t tcph_len; 11212 int len; 11213 11214 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11215 len = IPH_HDR_LENGTH(ipha); 11216 if (len == IP_SIMPLE_HDR_LENGTH) 11217 /* Nothing to do */ 11218 return; 11219 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11220 (len & 0x3)) 11221 return; 11222 11223 tcph_len = tcp->tcp_tcp_hdr_len; 11224 bcopy(tcp->tcp_tcph, buf, tcph_len); 11225 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11226 (tcp->tcp_ipha->ipha_dst & 0xffff); 11227 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11228 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11229 len += IP_SIMPLE_HDR_LENGTH; 11230 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11231 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11232 if ((int)tcp->tcp_sum < 0) 11233 tcp->tcp_sum--; 11234 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11235 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11236 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11237 bcopy(buf, tcp->tcp_tcph, tcph_len); 11238 tcp->tcp_ip_hdr_len = len; 11239 tcp->tcp_ipha->ipha_version_and_hdr_length = 11240 (IP_VERSION << 4) | (len >> 2); 11241 len += tcph_len; 11242 tcp->tcp_hdr_len = len; 11243 } 11244 11245 /* 11246 * Copy the standard header into its new location, 11247 * lay in the new options and then update the relevant 11248 * fields in both tcp_t and the standard header. 11249 */ 11250 static int 11251 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11252 { 11253 uint_t tcph_len; 11254 uint8_t *ip_optp; 11255 tcph_t *new_tcph; 11256 tcp_stack_t *tcps = tcp->tcp_tcps; 11257 11258 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11259 return (EINVAL); 11260 11261 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11262 return (EINVAL); 11263 11264 if (checkonly) { 11265 /* 11266 * do not really set, just pretend to - T_CHECK 11267 */ 11268 return (0); 11269 } 11270 11271 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11272 if (tcp->tcp_label_len > 0) { 11273 int padlen; 11274 uint8_t opt; 11275 11276 /* convert list termination to no-ops */ 11277 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11278 ip_optp += ip_optp[IPOPT_OLEN]; 11279 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11280 while (--padlen >= 0) 11281 *ip_optp++ = opt; 11282 } 11283 tcph_len = tcp->tcp_tcp_hdr_len; 11284 new_tcph = (tcph_t *)(ip_optp + len); 11285 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11286 tcp->tcp_tcph = new_tcph; 11287 bcopy(ptr, ip_optp, len); 11288 11289 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11290 11291 tcp->tcp_ip_hdr_len = len; 11292 tcp->tcp_ipha->ipha_version_and_hdr_length = 11293 (IP_VERSION << 4) | (len >> 2); 11294 tcp->tcp_hdr_len = len + tcph_len; 11295 if (!TCP_IS_DETACHED(tcp)) { 11296 /* Always allocate room for all options. */ 11297 (void) mi_set_sth_wroff(tcp->tcp_rq, 11298 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11299 } 11300 return (0); 11301 } 11302 11303 /* Get callback routine passed to nd_load by tcp_param_register */ 11304 /* ARGSUSED */ 11305 static int 11306 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11307 { 11308 tcpparam_t *tcppa = (tcpparam_t *)cp; 11309 11310 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11311 return (0); 11312 } 11313 11314 /* 11315 * Walk through the param array specified registering each element with the 11316 * named dispatch handler. 11317 */ 11318 static boolean_t 11319 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11320 { 11321 for (; cnt-- > 0; tcppa++) { 11322 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11323 if (!nd_load(ndp, tcppa->tcp_param_name, 11324 tcp_param_get, tcp_param_set, 11325 (caddr_t)tcppa)) { 11326 nd_free(ndp); 11327 return (B_FALSE); 11328 } 11329 } 11330 } 11331 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11332 KM_SLEEP); 11333 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11334 sizeof (tcpparam_t)); 11335 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11336 tcp_param_get, tcp_param_set_aligned, 11337 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11338 nd_free(ndp); 11339 return (B_FALSE); 11340 } 11341 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11342 KM_SLEEP); 11343 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11344 sizeof (tcpparam_t)); 11345 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11346 tcp_param_get, tcp_param_set_aligned, 11347 (caddr_t)tcps->tcps_mdt_head_param)) { 11348 nd_free(ndp); 11349 return (B_FALSE); 11350 } 11351 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11352 KM_SLEEP); 11353 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11354 sizeof (tcpparam_t)); 11355 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11356 tcp_param_get, tcp_param_set_aligned, 11357 (caddr_t)tcps->tcps_mdt_tail_param)) { 11358 nd_free(ndp); 11359 return (B_FALSE); 11360 } 11361 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11362 KM_SLEEP); 11363 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11364 sizeof (tcpparam_t)); 11365 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11366 tcp_param_get, tcp_param_set_aligned, 11367 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11368 nd_free(ndp); 11369 return (B_FALSE); 11370 } 11371 if (!nd_load(ndp, "tcp_extra_priv_ports", 11372 tcp_extra_priv_ports_get, NULL, NULL)) { 11373 nd_free(ndp); 11374 return (B_FALSE); 11375 } 11376 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11377 NULL, tcp_extra_priv_ports_add, NULL)) { 11378 nd_free(ndp); 11379 return (B_FALSE); 11380 } 11381 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11382 NULL, tcp_extra_priv_ports_del, NULL)) { 11383 nd_free(ndp); 11384 return (B_FALSE); 11385 } 11386 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11387 NULL)) { 11388 nd_free(ndp); 11389 return (B_FALSE); 11390 } 11391 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11392 NULL, NULL)) { 11393 nd_free(ndp); 11394 return (B_FALSE); 11395 } 11396 if (!nd_load(ndp, "tcp_listen_hash", 11397 tcp_listen_hash_report, NULL, NULL)) { 11398 nd_free(ndp); 11399 return (B_FALSE); 11400 } 11401 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11402 NULL, NULL)) { 11403 nd_free(ndp); 11404 return (B_FALSE); 11405 } 11406 if (!nd_load(ndp, "tcp_acceptor_hash", 11407 tcp_acceptor_hash_report, NULL, NULL)) { 11408 nd_free(ndp); 11409 return (B_FALSE); 11410 } 11411 if (!nd_load(ndp, "tcp_host_param", tcp_host_param_report, 11412 tcp_host_param_set, NULL)) { 11413 nd_free(ndp); 11414 return (B_FALSE); 11415 } 11416 if (!nd_load(ndp, "tcp_host_param_ipv6", 11417 tcp_host_param_report, tcp_host_param_set_ipv6, NULL)) { 11418 nd_free(ndp); 11419 return (B_FALSE); 11420 } 11421 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11422 tcp_1948_phrase_set, NULL)) { 11423 nd_free(ndp); 11424 return (B_FALSE); 11425 } 11426 if (!nd_load(ndp, "tcp_reserved_port_list", 11427 tcp_reserved_port_list, NULL, NULL)) { 11428 nd_free(ndp); 11429 return (B_FALSE); 11430 } 11431 /* 11432 * Dummy ndd variables - only to convey obsolescence information 11433 * through printing of their name (no get or set routines) 11434 * XXX Remove in future releases ? 11435 */ 11436 if (!nd_load(ndp, 11437 "tcp_close_wait_interval(obsoleted - " 11438 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11439 nd_free(ndp); 11440 return (B_FALSE); 11441 } 11442 return (B_TRUE); 11443 } 11444 11445 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11446 /* ARGSUSED */ 11447 static int 11448 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11449 cred_t *cr) 11450 { 11451 long new_value; 11452 tcpparam_t *tcppa = (tcpparam_t *)cp; 11453 11454 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11455 new_value < tcppa->tcp_param_min || 11456 new_value > tcppa->tcp_param_max) { 11457 return (EINVAL); 11458 } 11459 /* 11460 * Need to make sure new_value is a multiple of 4. If it is not, 11461 * round it up. For future 64 bit requirement, we actually make it 11462 * a multiple of 8. 11463 */ 11464 if (new_value & 0x7) { 11465 new_value = (new_value & ~0x7) + 0x8; 11466 } 11467 tcppa->tcp_param_val = new_value; 11468 return (0); 11469 } 11470 11471 /* Set callback routine passed to nd_load by tcp_param_register */ 11472 /* ARGSUSED */ 11473 static int 11474 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11475 { 11476 long new_value; 11477 tcpparam_t *tcppa = (tcpparam_t *)cp; 11478 11479 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11480 new_value < tcppa->tcp_param_min || 11481 new_value > tcppa->tcp_param_max) { 11482 return (EINVAL); 11483 } 11484 tcppa->tcp_param_val = new_value; 11485 return (0); 11486 } 11487 11488 /* 11489 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11490 * is filled, return as much as we can. The message passed in may be 11491 * multi-part, chained using b_cont. "start" is the starting sequence 11492 * number for this piece. 11493 */ 11494 static mblk_t * 11495 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11496 { 11497 uint32_t end; 11498 mblk_t *mp1; 11499 mblk_t *mp2; 11500 mblk_t *next_mp; 11501 uint32_t u1; 11502 tcp_stack_t *tcps = tcp->tcp_tcps; 11503 11504 /* Walk through all the new pieces. */ 11505 do { 11506 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11507 (uintptr_t)INT_MAX); 11508 end = start + (int)(mp->b_wptr - mp->b_rptr); 11509 next_mp = mp->b_cont; 11510 if (start == end) { 11511 /* Empty. Blast it. */ 11512 freeb(mp); 11513 continue; 11514 } 11515 mp->b_cont = NULL; 11516 TCP_REASS_SET_SEQ(mp, start); 11517 TCP_REASS_SET_END(mp, end); 11518 mp1 = tcp->tcp_reass_tail; 11519 if (!mp1) { 11520 tcp->tcp_reass_tail = mp; 11521 tcp->tcp_reass_head = mp; 11522 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11523 UPDATE_MIB(&tcps->tcps_mib, 11524 tcpInDataUnorderBytes, end - start); 11525 continue; 11526 } 11527 /* New stuff completely beyond tail? */ 11528 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11529 /* Link it on end. */ 11530 mp1->b_cont = mp; 11531 tcp->tcp_reass_tail = mp; 11532 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11533 UPDATE_MIB(&tcps->tcps_mib, 11534 tcpInDataUnorderBytes, end - start); 11535 continue; 11536 } 11537 mp1 = tcp->tcp_reass_head; 11538 u1 = TCP_REASS_SEQ(mp1); 11539 /* New stuff at the front? */ 11540 if (SEQ_LT(start, u1)) { 11541 /* Yes... Check for overlap. */ 11542 mp->b_cont = mp1; 11543 tcp->tcp_reass_head = mp; 11544 tcp_reass_elim_overlap(tcp, mp); 11545 continue; 11546 } 11547 /* 11548 * The new piece fits somewhere between the head and tail. 11549 * We find our slot, where mp1 precedes us and mp2 trails. 11550 */ 11551 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11552 u1 = TCP_REASS_SEQ(mp2); 11553 if (SEQ_LEQ(start, u1)) 11554 break; 11555 } 11556 /* Link ourselves in */ 11557 mp->b_cont = mp2; 11558 mp1->b_cont = mp; 11559 11560 /* Trim overlap with following mblk(s) first */ 11561 tcp_reass_elim_overlap(tcp, mp); 11562 11563 /* Trim overlap with preceding mblk */ 11564 tcp_reass_elim_overlap(tcp, mp1); 11565 11566 } while (start = end, mp = next_mp); 11567 mp1 = tcp->tcp_reass_head; 11568 /* Anything ready to go? */ 11569 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11570 return (NULL); 11571 /* Eat what we can off the queue */ 11572 for (;;) { 11573 mp = mp1->b_cont; 11574 end = TCP_REASS_END(mp1); 11575 TCP_REASS_SET_SEQ(mp1, 0); 11576 TCP_REASS_SET_END(mp1, 0); 11577 if (!mp) { 11578 tcp->tcp_reass_tail = NULL; 11579 break; 11580 } 11581 if (end != TCP_REASS_SEQ(mp)) { 11582 mp1->b_cont = NULL; 11583 break; 11584 } 11585 mp1 = mp; 11586 } 11587 mp1 = tcp->tcp_reass_head; 11588 tcp->tcp_reass_head = mp; 11589 return (mp1); 11590 } 11591 11592 /* Eliminate any overlap that mp may have over later mblks */ 11593 static void 11594 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11595 { 11596 uint32_t end; 11597 mblk_t *mp1; 11598 uint32_t u1; 11599 tcp_stack_t *tcps = tcp->tcp_tcps; 11600 11601 end = TCP_REASS_END(mp); 11602 while ((mp1 = mp->b_cont) != NULL) { 11603 u1 = TCP_REASS_SEQ(mp1); 11604 if (!SEQ_GT(end, u1)) 11605 break; 11606 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11607 mp->b_wptr -= end - u1; 11608 TCP_REASS_SET_END(mp, u1); 11609 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11610 UPDATE_MIB(&tcps->tcps_mib, 11611 tcpInDataPartDupBytes, end - u1); 11612 break; 11613 } 11614 mp->b_cont = mp1->b_cont; 11615 TCP_REASS_SET_SEQ(mp1, 0); 11616 TCP_REASS_SET_END(mp1, 0); 11617 freeb(mp1); 11618 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11619 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11620 } 11621 if (!mp1) 11622 tcp->tcp_reass_tail = mp; 11623 } 11624 11625 /* 11626 * Send up all messages queued on tcp_rcv_list. 11627 */ 11628 static uint_t 11629 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11630 { 11631 mblk_t *mp; 11632 uint_t ret = 0; 11633 uint_t thwin; 11634 #ifdef DEBUG 11635 uint_t cnt = 0; 11636 #endif 11637 tcp_stack_t *tcps = tcp->tcp_tcps; 11638 11639 /* Can't drain on an eager connection */ 11640 if (tcp->tcp_listener != NULL) 11641 return (ret); 11642 11643 /* 11644 * Handle two cases here: we are currently fused or we were 11645 * previously fused and have some urgent data to be delivered 11646 * upstream. The latter happens because we either ran out of 11647 * memory or were detached and therefore sending the SIGURG was 11648 * deferred until this point. In either case we pass control 11649 * over to tcp_fuse_rcv_drain() since it may need to complete 11650 * some work. 11651 */ 11652 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11653 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11654 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11655 &tcp->tcp_fused_sigurg_mp)) 11656 return (ret); 11657 } 11658 11659 while ((mp = tcp->tcp_rcv_list) != NULL) { 11660 tcp->tcp_rcv_list = mp->b_next; 11661 mp->b_next = NULL; 11662 #ifdef DEBUG 11663 cnt += msgdsize(mp); 11664 #endif 11665 /* Does this need SSL processing first? */ 11666 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11667 tcp_kssl_input(tcp, mp); 11668 continue; 11669 } 11670 putnext(q, mp); 11671 } 11672 ASSERT(cnt == tcp->tcp_rcv_cnt); 11673 tcp->tcp_rcv_last_head = NULL; 11674 tcp->tcp_rcv_last_tail = NULL; 11675 tcp->tcp_rcv_cnt = 0; 11676 11677 /* Learn the latest rwnd information that we sent to the other side. */ 11678 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11679 << tcp->tcp_rcv_ws; 11680 /* This is peer's calculated send window (our receive window). */ 11681 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11682 /* 11683 * Increase the receive window to max. But we need to do receiver 11684 * SWS avoidance. This means that we need to check the increase of 11685 * of receive window is at least 1 MSS. 11686 */ 11687 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11688 /* 11689 * If the window that the other side knows is less than max 11690 * deferred acks segments, send an update immediately. 11691 */ 11692 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11693 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11694 ret = TH_ACK_NEEDED; 11695 } 11696 tcp->tcp_rwnd = q->q_hiwat; 11697 } 11698 /* No need for the push timer now. */ 11699 if (tcp->tcp_push_tid != 0) { 11700 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11701 tcp->tcp_push_tid = 0; 11702 } 11703 return (ret); 11704 } 11705 11706 /* 11707 * Queue data on tcp_rcv_list which is a b_next chain. 11708 * tcp_rcv_last_head/tail is the last element of this chain. 11709 * Each element of the chain is a b_cont chain. 11710 * 11711 * M_DATA messages are added to the current element. 11712 * Other messages are added as new (b_next) elements. 11713 */ 11714 void 11715 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11716 { 11717 ASSERT(seg_len == msgdsize(mp)); 11718 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11719 11720 if (tcp->tcp_rcv_list == NULL) { 11721 ASSERT(tcp->tcp_rcv_last_head == NULL); 11722 tcp->tcp_rcv_list = mp; 11723 tcp->tcp_rcv_last_head = mp; 11724 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11725 tcp->tcp_rcv_last_tail->b_cont = mp; 11726 } else { 11727 tcp->tcp_rcv_last_head->b_next = mp; 11728 tcp->tcp_rcv_last_head = mp; 11729 } 11730 11731 while (mp->b_cont) 11732 mp = mp->b_cont; 11733 11734 tcp->tcp_rcv_last_tail = mp; 11735 tcp->tcp_rcv_cnt += seg_len; 11736 tcp->tcp_rwnd -= seg_len; 11737 } 11738 11739 /* 11740 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11741 * 11742 * This is the default entry function into TCP on the read side. TCP is 11743 * always entered via squeue i.e. using squeue's for mutual exclusion. 11744 * When classifier does a lookup to find the tcp, it also puts a reference 11745 * on the conn structure associated so the tcp is guaranteed to exist 11746 * when we come here. We still need to check the state because it might 11747 * as well has been closed. The squeue processing function i.e. squeue_enter, 11748 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 11749 * CONN_DEC_REF. 11750 * 11751 * Apart from the default entry point, IP also sends packets directly to 11752 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11753 * connections. 11754 */ 11755 void 11756 tcp_input(void *arg, mblk_t *mp, void *arg2) 11757 { 11758 conn_t *connp = (conn_t *)arg; 11759 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11760 11761 /* arg2 is the sqp */ 11762 ASSERT(arg2 != NULL); 11763 ASSERT(mp != NULL); 11764 11765 /* 11766 * Don't accept any input on a closed tcp as this TCP logically does 11767 * not exist on the system. Don't proceed further with this TCP. 11768 * For eg. this packet could trigger another close of this tcp 11769 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11770 * tcp_clean_death / tcp_closei_local must be called at most once 11771 * on a TCP. In this case we need to refeed the packet into the 11772 * classifier and figure out where the packet should go. Need to 11773 * preserve the recv_ill somehow. Until we figure that out, for 11774 * now just drop the packet if we can't classify the packet. 11775 */ 11776 if (tcp->tcp_state == TCPS_CLOSED || 11777 tcp->tcp_state == TCPS_BOUND) { 11778 conn_t *new_connp; 11779 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11780 11781 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11782 if (new_connp != NULL) { 11783 tcp_reinput(new_connp, mp, arg2); 11784 return; 11785 } 11786 /* We failed to classify. For now just drop the packet */ 11787 freemsg(mp); 11788 return; 11789 } 11790 11791 if (DB_TYPE(mp) == M_DATA) 11792 tcp_rput_data(connp, mp, arg2); 11793 else 11794 tcp_rput_common(tcp, mp); 11795 } 11796 11797 /* 11798 * The read side put procedure. 11799 * The packets passed up by ip are assume to be aligned according to 11800 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11801 */ 11802 static void 11803 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11804 { 11805 /* 11806 * tcp_rput_data() does not expect M_CTL except for the case 11807 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11808 * type. Need to make sure that any other M_CTLs don't make 11809 * it to tcp_rput_data since it is not expecting any and doesn't 11810 * check for it. 11811 */ 11812 if (DB_TYPE(mp) == M_CTL) { 11813 switch (*(uint32_t *)(mp->b_rptr)) { 11814 case TCP_IOC_ABORT_CONN: 11815 /* 11816 * Handle connection abort request. 11817 */ 11818 tcp_ioctl_abort_handler(tcp, mp); 11819 return; 11820 case IPSEC_IN: 11821 /* 11822 * Only secure icmp arrive in TCP and they 11823 * don't go through data path. 11824 */ 11825 tcp_icmp_error(tcp, mp); 11826 return; 11827 case IN_PKTINFO: 11828 /* 11829 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11830 * sockets that are receiving IPv4 traffic. tcp 11831 */ 11832 ASSERT(tcp->tcp_family == AF_INET6); 11833 ASSERT(tcp->tcp_ipv6_recvancillary & 11834 TCP_IPV6_RECVPKTINFO); 11835 tcp_rput_data(tcp->tcp_connp, mp, 11836 tcp->tcp_connp->conn_sqp); 11837 return; 11838 case MDT_IOC_INFO_UPDATE: 11839 /* 11840 * Handle Multidata information update; the 11841 * following routine will free the message. 11842 */ 11843 if (tcp->tcp_connp->conn_mdt_ok) { 11844 tcp_mdt_update(tcp, 11845 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11846 B_FALSE); 11847 } 11848 freemsg(mp); 11849 return; 11850 case LSO_IOC_INFO_UPDATE: 11851 /* 11852 * Handle LSO information update; the following 11853 * routine will free the message. 11854 */ 11855 if (tcp->tcp_connp->conn_lso_ok) { 11856 tcp_lso_update(tcp, 11857 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11858 } 11859 freemsg(mp); 11860 return; 11861 default: 11862 /* 11863 * tcp_icmp_err() will process the M_CTL packets. 11864 * Non-ICMP packets, if any, will be discarded in 11865 * tcp_icmp_err(). We will process the ICMP packet 11866 * even if we are TCP_IS_DETACHED_NONEAGER as the 11867 * incoming ICMP packet may result in changing 11868 * the tcp_mss, which we would need if we have 11869 * packets to retransmit. 11870 */ 11871 tcp_icmp_error(tcp, mp); 11872 return; 11873 } 11874 } 11875 11876 /* No point processing the message if tcp is already closed */ 11877 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11878 freemsg(mp); 11879 return; 11880 } 11881 11882 tcp_rput_other(tcp, mp); 11883 } 11884 11885 11886 /* The minimum of smoothed mean deviation in RTO calculation. */ 11887 #define TCP_SD_MIN 400 11888 11889 /* 11890 * Set RTO for this connection. The formula is from Jacobson and Karels' 11891 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11892 * are the same as those in Appendix A.2 of that paper. 11893 * 11894 * m = new measurement 11895 * sa = smoothed RTT average (8 * average estimates). 11896 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11897 */ 11898 static void 11899 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11900 { 11901 long m = TICK_TO_MSEC(rtt); 11902 clock_t sa = tcp->tcp_rtt_sa; 11903 clock_t sv = tcp->tcp_rtt_sd; 11904 clock_t rto; 11905 tcp_stack_t *tcps = tcp->tcp_tcps; 11906 11907 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11908 tcp->tcp_rtt_update++; 11909 11910 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11911 if (sa != 0) { 11912 /* 11913 * Update average estimator: 11914 * new rtt = 7/8 old rtt + 1/8 Error 11915 */ 11916 11917 /* m is now Error in estimate. */ 11918 m -= sa >> 3; 11919 if ((sa += m) <= 0) { 11920 /* 11921 * Don't allow the smoothed average to be negative. 11922 * We use 0 to denote reinitialization of the 11923 * variables. 11924 */ 11925 sa = 1; 11926 } 11927 11928 /* 11929 * Update deviation estimator: 11930 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11931 */ 11932 if (m < 0) 11933 m = -m; 11934 m -= sv >> 2; 11935 sv += m; 11936 } else { 11937 /* 11938 * This follows BSD's implementation. So the reinitialized 11939 * RTO is 3 * m. We cannot go less than 2 because if the 11940 * link is bandwidth dominated, doubling the window size 11941 * during slow start means doubling the RTT. We want to be 11942 * more conservative when we reinitialize our estimates. 3 11943 * is just a convenient number. 11944 */ 11945 sa = m << 3; 11946 sv = m << 1; 11947 } 11948 if (sv < TCP_SD_MIN) { 11949 /* 11950 * We do not know that if sa captures the delay ACK 11951 * effect as in a long train of segments, a receiver 11952 * does not delay its ACKs. So set the minimum of sv 11953 * to be TCP_SD_MIN, which is default to 400 ms, twice 11954 * of BSD DATO. That means the minimum of mean 11955 * deviation is 100 ms. 11956 * 11957 */ 11958 sv = TCP_SD_MIN; 11959 } 11960 tcp->tcp_rtt_sa = sa; 11961 tcp->tcp_rtt_sd = sv; 11962 /* 11963 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11964 * 11965 * Add tcp_rexmit_interval extra in case of extreme environment 11966 * where the algorithm fails to work. The default value of 11967 * tcp_rexmit_interval_extra should be 0. 11968 * 11969 * As we use a finer grained clock than BSD and update 11970 * RTO for every ACKs, add in another .25 of RTT to the 11971 * deviation of RTO to accomodate burstiness of 1/4 of 11972 * window size. 11973 */ 11974 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 11975 11976 if (rto > tcps->tcps_rexmit_interval_max) { 11977 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 11978 } else if (rto < tcps->tcps_rexmit_interval_min) { 11979 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 11980 } else { 11981 tcp->tcp_rto = rto; 11982 } 11983 11984 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11985 tcp->tcp_timer_backoff = 0; 11986 } 11987 11988 /* 11989 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11990 * send queue which starts at the given seq. no. 11991 * 11992 * Parameters: 11993 * tcp_t *tcp: the tcp instance pointer. 11994 * uint32_t seq: the starting seq. no of the requested segment. 11995 * int32_t *off: after the execution, *off will be the offset to 11996 * the returned mblk which points to the requested seq no. 11997 * It is the caller's responsibility to send in a non-null off. 11998 * 11999 * Return: 12000 * A mblk_t pointer pointing to the requested segment in send queue. 12001 */ 12002 static mblk_t * 12003 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12004 { 12005 int32_t cnt; 12006 mblk_t *mp; 12007 12008 /* Defensive coding. Make sure we don't send incorrect data. */ 12009 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12010 return (NULL); 12011 12012 cnt = seq - tcp->tcp_suna; 12013 mp = tcp->tcp_xmit_head; 12014 while (cnt > 0 && mp != NULL) { 12015 cnt -= mp->b_wptr - mp->b_rptr; 12016 if (cnt < 0) { 12017 cnt += mp->b_wptr - mp->b_rptr; 12018 break; 12019 } 12020 mp = mp->b_cont; 12021 } 12022 ASSERT(mp != NULL); 12023 *off = cnt; 12024 return (mp); 12025 } 12026 12027 /* 12028 * This function handles all retransmissions if SACK is enabled for this 12029 * connection. First it calculates how many segments can be retransmitted 12030 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12031 * segments. A segment is eligible if sack_cnt for that segment is greater 12032 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12033 * all eligible segments, it checks to see if TCP can send some new segments 12034 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12035 * 12036 * Parameters: 12037 * tcp_t *tcp: the tcp structure of the connection. 12038 * uint_t *flags: in return, appropriate value will be set for 12039 * tcp_rput_data(). 12040 */ 12041 static void 12042 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12043 { 12044 notsack_blk_t *notsack_blk; 12045 int32_t usable_swnd; 12046 int32_t mss; 12047 uint32_t seg_len; 12048 mblk_t *xmit_mp; 12049 tcp_stack_t *tcps = tcp->tcp_tcps; 12050 12051 ASSERT(tcp->tcp_sack_info != NULL); 12052 ASSERT(tcp->tcp_notsack_list != NULL); 12053 ASSERT(tcp->tcp_rexmit == B_FALSE); 12054 12055 /* Defensive coding in case there is a bug... */ 12056 if (tcp->tcp_notsack_list == NULL) { 12057 return; 12058 } 12059 notsack_blk = tcp->tcp_notsack_list; 12060 mss = tcp->tcp_mss; 12061 12062 /* 12063 * Limit the num of outstanding data in the network to be 12064 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12065 */ 12066 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12067 12068 /* At least retransmit 1 MSS of data. */ 12069 if (usable_swnd <= 0) { 12070 usable_swnd = mss; 12071 } 12072 12073 /* Make sure no new RTT samples will be taken. */ 12074 tcp->tcp_csuna = tcp->tcp_snxt; 12075 12076 notsack_blk = tcp->tcp_notsack_list; 12077 while (usable_swnd > 0) { 12078 mblk_t *snxt_mp, *tmp_mp; 12079 tcp_seq begin = tcp->tcp_sack_snxt; 12080 tcp_seq end; 12081 int32_t off; 12082 12083 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12084 if (SEQ_GT(notsack_blk->end, begin) && 12085 (notsack_blk->sack_cnt >= 12086 tcps->tcps_dupack_fast_retransmit)) { 12087 end = notsack_blk->end; 12088 if (SEQ_LT(begin, notsack_blk->begin)) { 12089 begin = notsack_blk->begin; 12090 } 12091 break; 12092 } 12093 } 12094 /* 12095 * All holes are filled. Manipulate tcp_cwnd to send more 12096 * if we can. Note that after the SACK recovery, tcp_cwnd is 12097 * set to tcp_cwnd_ssthresh. 12098 */ 12099 if (notsack_blk == NULL) { 12100 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12101 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12102 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12103 ASSERT(tcp->tcp_cwnd > 0); 12104 return; 12105 } else { 12106 usable_swnd = usable_swnd / mss; 12107 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12108 MAX(usable_swnd * mss, mss); 12109 *flags |= TH_XMIT_NEEDED; 12110 return; 12111 } 12112 } 12113 12114 /* 12115 * Note that we may send more than usable_swnd allows here 12116 * because of round off, but no more than 1 MSS of data. 12117 */ 12118 seg_len = end - begin; 12119 if (seg_len > mss) 12120 seg_len = mss; 12121 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12122 ASSERT(snxt_mp != NULL); 12123 /* This should not happen. Defensive coding again... */ 12124 if (snxt_mp == NULL) { 12125 return; 12126 } 12127 12128 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12129 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12130 if (xmit_mp == NULL) 12131 return; 12132 12133 usable_swnd -= seg_len; 12134 tcp->tcp_pipe += seg_len; 12135 tcp->tcp_sack_snxt = begin + seg_len; 12136 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 12137 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12138 12139 /* 12140 * Update the send timestamp to avoid false retransmission. 12141 */ 12142 snxt_mp->b_prev = (mblk_t *)lbolt; 12143 12144 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12145 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12146 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12147 /* 12148 * Update tcp_rexmit_max to extend this SACK recovery phase. 12149 * This happens when new data sent during fast recovery is 12150 * also lost. If TCP retransmits those new data, it needs 12151 * to extend SACK recover phase to avoid starting another 12152 * fast retransmit/recovery unnecessarily. 12153 */ 12154 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12155 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12156 } 12157 } 12158 } 12159 12160 /* 12161 * This function handles policy checking at TCP level for non-hard_bound/ 12162 * detached connections. 12163 */ 12164 static boolean_t 12165 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12166 boolean_t secure, boolean_t mctl_present) 12167 { 12168 ipsec_latch_t *ipl = NULL; 12169 ipsec_action_t *act = NULL; 12170 mblk_t *data_mp; 12171 ipsec_in_t *ii; 12172 const char *reason; 12173 kstat_named_t *counter; 12174 tcp_stack_t *tcps = tcp->tcp_tcps; 12175 ipsec_stack_t *ipss; 12176 ip_stack_t *ipst; 12177 12178 ASSERT(mctl_present || !secure); 12179 12180 ASSERT((ipha == NULL && ip6h != NULL) || 12181 (ip6h == NULL && ipha != NULL)); 12182 12183 /* 12184 * We don't necessarily have an ipsec_in_act action to verify 12185 * policy because of assymetrical policy where we have only 12186 * outbound policy and no inbound policy (possible with global 12187 * policy). 12188 */ 12189 if (!secure) { 12190 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12191 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12192 return (B_TRUE); 12193 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12194 "tcp_check_policy", ipha, ip6h, secure, 12195 tcps->tcps_netstack); 12196 ipss = tcps->tcps_netstack->netstack_ipsec; 12197 12198 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12199 DROPPER(ipss, ipds_tcp_clear), 12200 &tcps->tcps_dropper); 12201 return (B_FALSE); 12202 } 12203 12204 /* 12205 * We have a secure packet. 12206 */ 12207 if (act == NULL) { 12208 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12209 "tcp_check_policy", ipha, ip6h, secure, 12210 tcps->tcps_netstack); 12211 ipss = tcps->tcps_netstack->netstack_ipsec; 12212 12213 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12214 DROPPER(ipss, ipds_tcp_secure), 12215 &tcps->tcps_dropper); 12216 return (B_FALSE); 12217 } 12218 12219 /* 12220 * XXX This whole routine is currently incorrect. ipl should 12221 * be set to the latch pointer, but is currently not set, so 12222 * we initialize it to NULL to avoid picking up random garbage. 12223 */ 12224 if (ipl == NULL) 12225 return (B_TRUE); 12226 12227 data_mp = first_mp->b_cont; 12228 12229 ii = (ipsec_in_t *)first_mp->b_rptr; 12230 12231 ipst = tcps->tcps_netstack->netstack_ip; 12232 12233 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12234 &counter, tcp->tcp_connp)) { 12235 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12236 return (B_TRUE); 12237 } 12238 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12239 "tcp inbound policy mismatch: %s, packet dropped\n", 12240 reason); 12241 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12242 12243 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12244 &tcps->tcps_dropper); 12245 return (B_FALSE); 12246 } 12247 12248 /* 12249 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12250 * retransmission after a timeout. 12251 * 12252 * To limit the number of duplicate segments, we limit the number of segment 12253 * to be sent in one time to tcp_snd_burst, the burst variable. 12254 */ 12255 static void 12256 tcp_ss_rexmit(tcp_t *tcp) 12257 { 12258 uint32_t snxt; 12259 uint32_t smax; 12260 int32_t win; 12261 int32_t mss; 12262 int32_t off; 12263 int32_t burst = tcp->tcp_snd_burst; 12264 mblk_t *snxt_mp; 12265 tcp_stack_t *tcps = tcp->tcp_tcps; 12266 12267 /* 12268 * Note that tcp_rexmit can be set even though TCP has retransmitted 12269 * all unack'ed segments. 12270 */ 12271 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12272 smax = tcp->tcp_rexmit_max; 12273 snxt = tcp->tcp_rexmit_nxt; 12274 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12275 snxt = tcp->tcp_suna; 12276 } 12277 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12278 win -= snxt - tcp->tcp_suna; 12279 mss = tcp->tcp_mss; 12280 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12281 12282 while (SEQ_LT(snxt, smax) && (win > 0) && 12283 (burst > 0) && (snxt_mp != NULL)) { 12284 mblk_t *xmit_mp; 12285 mblk_t *old_snxt_mp = snxt_mp; 12286 uint32_t cnt = mss; 12287 12288 if (win < cnt) { 12289 cnt = win; 12290 } 12291 if (SEQ_GT(snxt + cnt, smax)) { 12292 cnt = smax - snxt; 12293 } 12294 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12295 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12296 if (xmit_mp == NULL) 12297 return; 12298 12299 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12300 12301 snxt += cnt; 12302 win -= cnt; 12303 /* 12304 * Update the send timestamp to avoid false 12305 * retransmission. 12306 */ 12307 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12308 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12309 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12310 12311 tcp->tcp_rexmit_nxt = snxt; 12312 burst--; 12313 } 12314 /* 12315 * If we have transmitted all we have at the time 12316 * we started the retranmission, we can leave 12317 * the rest of the job to tcp_wput_data(). But we 12318 * need to check the send window first. If the 12319 * win is not 0, go on with tcp_wput_data(). 12320 */ 12321 if (SEQ_LT(snxt, smax) || win == 0) { 12322 return; 12323 } 12324 } 12325 /* Only call tcp_wput_data() if there is data to be sent. */ 12326 if (tcp->tcp_unsent) { 12327 tcp_wput_data(tcp, NULL, B_FALSE); 12328 } 12329 } 12330 12331 /* 12332 * Process all TCP option in SYN segment. Note that this function should 12333 * be called after tcp_adapt_ire() is called so that the necessary info 12334 * from IRE is already set in the tcp structure. 12335 * 12336 * This function sets up the correct tcp_mss value according to the 12337 * MSS option value and our header size. It also sets up the window scale 12338 * and timestamp values, and initialize SACK info blocks. But it does not 12339 * change receive window size after setting the tcp_mss value. The caller 12340 * should do the appropriate change. 12341 */ 12342 void 12343 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12344 { 12345 int options; 12346 tcp_opt_t tcpopt; 12347 uint32_t mss_max; 12348 char *tmp_tcph; 12349 tcp_stack_t *tcps = tcp->tcp_tcps; 12350 12351 tcpopt.tcp = NULL; 12352 options = tcp_parse_options(tcph, &tcpopt); 12353 12354 /* 12355 * Process MSS option. Note that MSS option value does not account 12356 * for IP or TCP options. This means that it is equal to MTU - minimum 12357 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12358 * IPv6. 12359 */ 12360 if (!(options & TCP_OPT_MSS_PRESENT)) { 12361 if (tcp->tcp_ipversion == IPV4_VERSION) 12362 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12363 else 12364 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12365 } else { 12366 if (tcp->tcp_ipversion == IPV4_VERSION) 12367 mss_max = tcps->tcps_mss_max_ipv4; 12368 else 12369 mss_max = tcps->tcps_mss_max_ipv6; 12370 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12371 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12372 else if (tcpopt.tcp_opt_mss > mss_max) 12373 tcpopt.tcp_opt_mss = mss_max; 12374 } 12375 12376 /* Process Window Scale option. */ 12377 if (options & TCP_OPT_WSCALE_PRESENT) { 12378 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12379 tcp->tcp_snd_ws_ok = B_TRUE; 12380 } else { 12381 tcp->tcp_snd_ws = B_FALSE; 12382 tcp->tcp_snd_ws_ok = B_FALSE; 12383 tcp->tcp_rcv_ws = B_FALSE; 12384 } 12385 12386 /* Process Timestamp option. */ 12387 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12388 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12389 tmp_tcph = (char *)tcp->tcp_tcph; 12390 12391 tcp->tcp_snd_ts_ok = B_TRUE; 12392 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12393 tcp->tcp_last_rcv_lbolt = lbolt64; 12394 ASSERT(OK_32PTR(tmp_tcph)); 12395 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12396 12397 /* Fill in our template header with basic timestamp option. */ 12398 tmp_tcph += tcp->tcp_tcp_hdr_len; 12399 tmp_tcph[0] = TCPOPT_NOP; 12400 tmp_tcph[1] = TCPOPT_NOP; 12401 tmp_tcph[2] = TCPOPT_TSTAMP; 12402 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12403 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12404 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12405 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12406 } else { 12407 tcp->tcp_snd_ts_ok = B_FALSE; 12408 } 12409 12410 /* 12411 * Process SACK options. If SACK is enabled for this connection, 12412 * then allocate the SACK info structure. Note the following ways 12413 * when tcp_snd_sack_ok is set to true. 12414 * 12415 * For active connection: in tcp_adapt_ire() called in 12416 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12417 * is checked. 12418 * 12419 * For passive connection: in tcp_adapt_ire() called in 12420 * tcp_accept_comm(). 12421 * 12422 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12423 * That check makes sure that if we did not send a SACK OK option, 12424 * we will not enable SACK for this connection even though the other 12425 * side sends us SACK OK option. For active connection, the SACK 12426 * info structure has already been allocated. So we need to free 12427 * it if SACK is disabled. 12428 */ 12429 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12430 (tcp->tcp_snd_sack_ok || 12431 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12432 /* This should be true only in the passive case. */ 12433 if (tcp->tcp_sack_info == NULL) { 12434 ASSERT(TCP_IS_DETACHED(tcp)); 12435 tcp->tcp_sack_info = 12436 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12437 } 12438 if (tcp->tcp_sack_info == NULL) { 12439 tcp->tcp_snd_sack_ok = B_FALSE; 12440 } else { 12441 tcp->tcp_snd_sack_ok = B_TRUE; 12442 if (tcp->tcp_snd_ts_ok) { 12443 tcp->tcp_max_sack_blk = 3; 12444 } else { 12445 tcp->tcp_max_sack_blk = 4; 12446 } 12447 } 12448 } else { 12449 /* 12450 * Resetting tcp_snd_sack_ok to B_FALSE so that 12451 * no SACK info will be used for this 12452 * connection. This assumes that SACK usage 12453 * permission is negotiated. This may need 12454 * to be changed once this is clarified. 12455 */ 12456 if (tcp->tcp_sack_info != NULL) { 12457 ASSERT(tcp->tcp_notsack_list == NULL); 12458 kmem_cache_free(tcp_sack_info_cache, 12459 tcp->tcp_sack_info); 12460 tcp->tcp_sack_info = NULL; 12461 } 12462 tcp->tcp_snd_sack_ok = B_FALSE; 12463 } 12464 12465 /* 12466 * Now we know the exact TCP/IP header length, subtract 12467 * that from tcp_mss to get our side's MSS. 12468 */ 12469 tcp->tcp_mss -= tcp->tcp_hdr_len; 12470 /* 12471 * Here we assume that the other side's header size will be equal to 12472 * our header size. We calculate the real MSS accordingly. Need to 12473 * take into additional stuffs IPsec puts in. 12474 * 12475 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12476 */ 12477 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12478 ((tcp->tcp_ipversion == IPV4_VERSION ? 12479 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12480 12481 /* 12482 * Set MSS to the smaller one of both ends of the connection. 12483 * We should not have called tcp_mss_set() before, but our 12484 * side of the MSS should have been set to a proper value 12485 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12486 * STREAM head parameters properly. 12487 * 12488 * If we have a larger-than-16-bit window but the other side 12489 * didn't want to do window scale, tcp_rwnd_set() will take 12490 * care of that. 12491 */ 12492 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12493 } 12494 12495 /* 12496 * Sends the T_CONN_IND to the listener. The caller calls this 12497 * functions via squeue to get inside the listener's perimeter 12498 * once the 3 way hand shake is done a T_CONN_IND needs to be 12499 * sent. As an optimization, the caller can call this directly 12500 * if listener's perimeter is same as eager's. 12501 */ 12502 /* ARGSUSED */ 12503 void 12504 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12505 { 12506 conn_t *lconnp = (conn_t *)arg; 12507 tcp_t *listener = lconnp->conn_tcp; 12508 tcp_t *tcp; 12509 struct T_conn_ind *conn_ind; 12510 ipaddr_t *addr_cache; 12511 boolean_t need_send_conn_ind = B_FALSE; 12512 tcp_stack_t *tcps = listener->tcp_tcps; 12513 12514 /* retrieve the eager */ 12515 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12516 ASSERT(conn_ind->OPT_offset != 0 && 12517 conn_ind->OPT_length == sizeof (intptr_t)); 12518 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12519 conn_ind->OPT_length); 12520 12521 /* 12522 * TLI/XTI applications will get confused by 12523 * sending eager as an option since it violates 12524 * the option semantics. So remove the eager as 12525 * option since TLI/XTI app doesn't need it anyway. 12526 */ 12527 if (!TCP_IS_SOCKET(listener)) { 12528 conn_ind->OPT_length = 0; 12529 conn_ind->OPT_offset = 0; 12530 } 12531 if (listener->tcp_state == TCPS_CLOSED || 12532 TCP_IS_DETACHED(listener)) { 12533 /* 12534 * If listener has closed, it would have caused a 12535 * a cleanup/blowoff to happen for the eager. We 12536 * just need to return. 12537 */ 12538 freemsg(mp); 12539 return; 12540 } 12541 12542 12543 /* 12544 * if the conn_req_q is full defer passing up the 12545 * T_CONN_IND until space is availabe after t_accept() 12546 * processing 12547 */ 12548 mutex_enter(&listener->tcp_eager_lock); 12549 12550 /* 12551 * Take the eager out, if it is in the list of droppable eagers 12552 * as we are here because the 3W handshake is over. 12553 */ 12554 MAKE_UNDROPPABLE(tcp); 12555 12556 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12557 tcp_t *tail; 12558 12559 /* 12560 * The eager already has an extra ref put in tcp_rput_data 12561 * so that it stays till accept comes back even though it 12562 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12563 */ 12564 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12565 listener->tcp_conn_req_cnt_q0--; 12566 listener->tcp_conn_req_cnt_q++; 12567 12568 /* Move from SYN_RCVD to ESTABLISHED list */ 12569 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12570 tcp->tcp_eager_prev_q0; 12571 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12572 tcp->tcp_eager_next_q0; 12573 tcp->tcp_eager_prev_q0 = NULL; 12574 tcp->tcp_eager_next_q0 = NULL; 12575 12576 /* 12577 * Insert at end of the queue because sockfs 12578 * sends down T_CONN_RES in chronological 12579 * order. Leaving the older conn indications 12580 * at front of the queue helps reducing search 12581 * time. 12582 */ 12583 tail = listener->tcp_eager_last_q; 12584 if (tail != NULL) 12585 tail->tcp_eager_next_q = tcp; 12586 else 12587 listener->tcp_eager_next_q = tcp; 12588 listener->tcp_eager_last_q = tcp; 12589 tcp->tcp_eager_next_q = NULL; 12590 /* 12591 * Delay sending up the T_conn_ind until we are 12592 * done with the eager. Once we have have sent up 12593 * the T_conn_ind, the accept can potentially complete 12594 * any time and release the refhold we have on the eager. 12595 */ 12596 need_send_conn_ind = B_TRUE; 12597 } else { 12598 /* 12599 * Defer connection on q0 and set deferred 12600 * connection bit true 12601 */ 12602 tcp->tcp_conn_def_q0 = B_TRUE; 12603 12604 /* take tcp out of q0 ... */ 12605 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12606 tcp->tcp_eager_next_q0; 12607 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12608 tcp->tcp_eager_prev_q0; 12609 12610 /* ... and place it at the end of q0 */ 12611 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12612 tcp->tcp_eager_next_q0 = listener; 12613 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12614 listener->tcp_eager_prev_q0 = tcp; 12615 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12616 } 12617 12618 /* we have timed out before */ 12619 if (tcp->tcp_syn_rcvd_timeout != 0) { 12620 tcp->tcp_syn_rcvd_timeout = 0; 12621 listener->tcp_syn_rcvd_timeout--; 12622 if (listener->tcp_syn_defense && 12623 listener->tcp_syn_rcvd_timeout <= 12624 (tcps->tcps_conn_req_max_q0 >> 5) && 12625 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12626 listener->tcp_last_rcv_lbolt)) { 12627 /* 12628 * Turn off the defense mode if we 12629 * believe the SYN attack is over. 12630 */ 12631 listener->tcp_syn_defense = B_FALSE; 12632 if (listener->tcp_ip_addr_cache) { 12633 kmem_free((void *)listener->tcp_ip_addr_cache, 12634 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12635 listener->tcp_ip_addr_cache = NULL; 12636 } 12637 } 12638 } 12639 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12640 if (addr_cache != NULL) { 12641 /* 12642 * We have finished a 3-way handshake with this 12643 * remote host. This proves the IP addr is good. 12644 * Cache it! 12645 */ 12646 addr_cache[IP_ADDR_CACHE_HASH( 12647 tcp->tcp_remote)] = tcp->tcp_remote; 12648 } 12649 mutex_exit(&listener->tcp_eager_lock); 12650 if (need_send_conn_ind) 12651 putnext(listener->tcp_rq, mp); 12652 } 12653 12654 mblk_t * 12655 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12656 uint_t *ifindexp, ip6_pkt_t *ippp) 12657 { 12658 ip_pktinfo_t *pinfo; 12659 ip6_t *ip6h; 12660 uchar_t *rptr; 12661 mblk_t *first_mp = mp; 12662 boolean_t mctl_present = B_FALSE; 12663 uint_t ifindex = 0; 12664 ip6_pkt_t ipp; 12665 uint_t ipvers; 12666 uint_t ip_hdr_len; 12667 tcp_stack_t *tcps = tcp->tcp_tcps; 12668 12669 rptr = mp->b_rptr; 12670 ASSERT(OK_32PTR(rptr)); 12671 ASSERT(tcp != NULL); 12672 ipp.ipp_fields = 0; 12673 12674 switch DB_TYPE(mp) { 12675 case M_CTL: 12676 mp = mp->b_cont; 12677 if (mp == NULL) { 12678 freemsg(first_mp); 12679 return (NULL); 12680 } 12681 if (DB_TYPE(mp) != M_DATA) { 12682 freemsg(first_mp); 12683 return (NULL); 12684 } 12685 mctl_present = B_TRUE; 12686 break; 12687 case M_DATA: 12688 break; 12689 default: 12690 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12691 freemsg(mp); 12692 return (NULL); 12693 } 12694 ipvers = IPH_HDR_VERSION(rptr); 12695 if (ipvers == IPV4_VERSION) { 12696 if (tcp == NULL) { 12697 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12698 goto done; 12699 } 12700 12701 ipp.ipp_fields |= IPPF_HOPLIMIT; 12702 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12703 12704 /* 12705 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12706 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12707 */ 12708 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12709 mctl_present) { 12710 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12711 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12712 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12713 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12714 ipp.ipp_fields |= IPPF_IFINDEX; 12715 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12716 ifindex = pinfo->ip_pkt_ifindex; 12717 } 12718 freeb(first_mp); 12719 mctl_present = B_FALSE; 12720 } 12721 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12722 } else { 12723 ip6h = (ip6_t *)rptr; 12724 12725 ASSERT(ipvers == IPV6_VERSION); 12726 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12727 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12728 ipp.ipp_hoplimit = ip6h->ip6_hops; 12729 12730 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12731 uint8_t nexthdrp; 12732 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12733 12734 /* Look for ifindex information */ 12735 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12736 ip6i_t *ip6i = (ip6i_t *)ip6h; 12737 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12738 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12739 freemsg(first_mp); 12740 return (NULL); 12741 } 12742 12743 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12744 ASSERT(ip6i->ip6i_ifindex != 0); 12745 ipp.ipp_fields |= IPPF_IFINDEX; 12746 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12747 ifindex = ip6i->ip6i_ifindex; 12748 } 12749 rptr = (uchar_t *)&ip6i[1]; 12750 mp->b_rptr = rptr; 12751 if (rptr == mp->b_wptr) { 12752 mblk_t *mp1; 12753 mp1 = mp->b_cont; 12754 freeb(mp); 12755 mp = mp1; 12756 rptr = mp->b_rptr; 12757 } 12758 if (MBLKL(mp) < IPV6_HDR_LEN + 12759 sizeof (tcph_t)) { 12760 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12761 freemsg(first_mp); 12762 return (NULL); 12763 } 12764 ip6h = (ip6_t *)rptr; 12765 } 12766 12767 /* 12768 * Find any potentially interesting extension headers 12769 * as well as the length of the IPv6 + extension 12770 * headers. 12771 */ 12772 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12773 /* Verify if this is a TCP packet */ 12774 if (nexthdrp != IPPROTO_TCP) { 12775 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12776 freemsg(first_mp); 12777 return (NULL); 12778 } 12779 } else { 12780 ip_hdr_len = IPV6_HDR_LEN; 12781 } 12782 } 12783 12784 done: 12785 if (ipversp != NULL) 12786 *ipversp = ipvers; 12787 if (ip_hdr_lenp != NULL) 12788 *ip_hdr_lenp = ip_hdr_len; 12789 if (ippp != NULL) 12790 *ippp = ipp; 12791 if (ifindexp != NULL) 12792 *ifindexp = ifindex; 12793 if (mctl_present) { 12794 freeb(first_mp); 12795 } 12796 return (mp); 12797 } 12798 12799 /* 12800 * Handle M_DATA messages from IP. Its called directly from IP via 12801 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12802 * in this path. 12803 * 12804 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12805 * v4 and v6), we are called through tcp_input() and a M_CTL can 12806 * be present for options but tcp_find_pktinfo() deals with it. We 12807 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12808 * 12809 * The first argument is always the connp/tcp to which the mp belongs. 12810 * There are no exceptions to this rule. The caller has already put 12811 * a reference on this connp/tcp and once tcp_rput_data() returns, 12812 * the squeue will do the refrele. 12813 * 12814 * The TH_SYN for the listener directly go to tcp_conn_request via 12815 * squeue. 12816 * 12817 * sqp: NULL = recursive, sqp != NULL means called from squeue 12818 */ 12819 void 12820 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12821 { 12822 int32_t bytes_acked; 12823 int32_t gap; 12824 mblk_t *mp1; 12825 uint_t flags; 12826 uint32_t new_swnd = 0; 12827 uchar_t *iphdr; 12828 uchar_t *rptr; 12829 int32_t rgap; 12830 uint32_t seg_ack; 12831 int seg_len; 12832 uint_t ip_hdr_len; 12833 uint32_t seg_seq; 12834 tcph_t *tcph; 12835 int urp; 12836 tcp_opt_t tcpopt; 12837 uint_t ipvers; 12838 ip6_pkt_t ipp; 12839 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12840 uint32_t cwnd; 12841 uint32_t add; 12842 int npkt; 12843 int mss; 12844 conn_t *connp = (conn_t *)arg; 12845 squeue_t *sqp = (squeue_t *)arg2; 12846 tcp_t *tcp = connp->conn_tcp; 12847 tcp_stack_t *tcps = tcp->tcp_tcps; 12848 12849 /* 12850 * RST from fused tcp loopback peer should trigger an unfuse. 12851 */ 12852 if (tcp->tcp_fused) { 12853 TCP_STAT(tcps, tcp_fusion_aborted); 12854 tcp_unfuse(tcp); 12855 } 12856 12857 iphdr = mp->b_rptr; 12858 rptr = mp->b_rptr; 12859 ASSERT(OK_32PTR(rptr)); 12860 12861 /* 12862 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12863 * processing here. For rest call tcp_find_pktinfo to fill up the 12864 * necessary information. 12865 */ 12866 if (IPCL_IS_TCP4(connp)) { 12867 ipvers = IPV4_VERSION; 12868 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12869 } else { 12870 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12871 NULL, &ipp); 12872 if (mp == NULL) { 12873 TCP_STAT(tcps, tcp_rput_v6_error); 12874 return; 12875 } 12876 iphdr = mp->b_rptr; 12877 rptr = mp->b_rptr; 12878 } 12879 ASSERT(DB_TYPE(mp) == M_DATA); 12880 12881 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12882 seg_seq = ABE32_TO_U32(tcph->th_seq); 12883 seg_ack = ABE32_TO_U32(tcph->th_ack); 12884 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12885 seg_len = (int)(mp->b_wptr - rptr) - 12886 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12887 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12888 do { 12889 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12890 (uintptr_t)INT_MAX); 12891 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12892 } while ((mp1 = mp1->b_cont) != NULL && 12893 mp1->b_datap->db_type == M_DATA); 12894 } 12895 12896 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12897 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12898 seg_len, tcph); 12899 return; 12900 } 12901 12902 if (sqp != NULL) { 12903 /* 12904 * This is the correct place to update tcp_last_recv_time. Note 12905 * that it is also updated for tcp structure that belongs to 12906 * global and listener queues which do not really need updating. 12907 * But that should not cause any harm. And it is updated for 12908 * all kinds of incoming segments, not only for data segments. 12909 */ 12910 tcp->tcp_last_recv_time = lbolt; 12911 } 12912 12913 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12914 12915 BUMP_LOCAL(tcp->tcp_ibsegs); 12916 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 12917 12918 if ((flags & TH_URG) && sqp != NULL) { 12919 /* 12920 * TCP can't handle urgent pointers that arrive before 12921 * the connection has been accept()ed since it can't 12922 * buffer OOB data. Discard segment if this happens. 12923 * 12924 * We can't just rely on a non-null tcp_listener to indicate 12925 * that the accept() has completed since unlinking of the 12926 * eager and completion of the accept are not atomic. 12927 * tcp_detached, when it is not set (B_FALSE) indicates 12928 * that the accept() has completed. 12929 * 12930 * Nor can it reassemble urgent pointers, so discard 12931 * if it's not the next segment expected. 12932 * 12933 * Otherwise, collapse chain into one mblk (discard if 12934 * that fails). This makes sure the headers, retransmitted 12935 * data, and new data all are in the same mblk. 12936 */ 12937 ASSERT(mp != NULL); 12938 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 12939 freemsg(mp); 12940 return; 12941 } 12942 /* Update pointers into message */ 12943 iphdr = rptr = mp->b_rptr; 12944 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12945 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12946 /* 12947 * Since we can't handle any data with this urgent 12948 * pointer that is out of sequence, we expunge 12949 * the data. This allows us to still register 12950 * the urgent mark and generate the M_PCSIG, 12951 * which we can do. 12952 */ 12953 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12954 seg_len = 0; 12955 } 12956 } 12957 12958 switch (tcp->tcp_state) { 12959 case TCPS_SYN_SENT: 12960 if (flags & TH_ACK) { 12961 /* 12962 * Note that our stack cannot send data before a 12963 * connection is established, therefore the 12964 * following check is valid. Otherwise, it has 12965 * to be changed. 12966 */ 12967 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12968 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12969 freemsg(mp); 12970 if (flags & TH_RST) 12971 return; 12972 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12973 tcp, seg_ack, 0, TH_RST); 12974 return; 12975 } 12976 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12977 } 12978 if (flags & TH_RST) { 12979 freemsg(mp); 12980 if (flags & TH_ACK) 12981 (void) tcp_clean_death(tcp, 12982 ECONNREFUSED, 13); 12983 return; 12984 } 12985 if (!(flags & TH_SYN)) { 12986 freemsg(mp); 12987 return; 12988 } 12989 12990 /* Process all TCP options. */ 12991 tcp_process_options(tcp, tcph); 12992 /* 12993 * The following changes our rwnd to be a multiple of the 12994 * MIN(peer MSS, our MSS) for performance reason. 12995 */ 12996 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 12997 tcp->tcp_mss)); 12998 12999 /* Is the other end ECN capable? */ 13000 if (tcp->tcp_ecn_ok) { 13001 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13002 tcp->tcp_ecn_ok = B_FALSE; 13003 } 13004 } 13005 /* 13006 * Clear ECN flags because it may interfere with later 13007 * processing. 13008 */ 13009 flags &= ~(TH_ECE|TH_CWR); 13010 13011 tcp->tcp_irs = seg_seq; 13012 tcp->tcp_rack = seg_seq; 13013 tcp->tcp_rnxt = seg_seq + 1; 13014 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13015 if (!TCP_IS_DETACHED(tcp)) { 13016 /* Allocate room for SACK options if needed. */ 13017 if (tcp->tcp_snd_sack_ok) { 13018 (void) mi_set_sth_wroff(tcp->tcp_rq, 13019 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13020 (tcp->tcp_loopback ? 0 : 13021 tcps->tcps_wroff_xtra)); 13022 } else { 13023 (void) mi_set_sth_wroff(tcp->tcp_rq, 13024 tcp->tcp_hdr_len + 13025 (tcp->tcp_loopback ? 0 : 13026 tcps->tcps_wroff_xtra)); 13027 } 13028 } 13029 if (flags & TH_ACK) { 13030 /* 13031 * If we can't get the confirmation upstream, pretend 13032 * we didn't even see this one. 13033 * 13034 * XXX: how can we pretend we didn't see it if we 13035 * have updated rnxt et. al. 13036 * 13037 * For loopback we defer sending up the T_CONN_CON 13038 * until after some checks below. 13039 */ 13040 mp1 = NULL; 13041 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13042 tcp->tcp_loopback ? &mp1 : NULL)) { 13043 freemsg(mp); 13044 return; 13045 } 13046 /* SYN was acked - making progress */ 13047 if (tcp->tcp_ipversion == IPV6_VERSION) 13048 tcp->tcp_ip_forward_progress = B_TRUE; 13049 13050 /* One for the SYN */ 13051 tcp->tcp_suna = tcp->tcp_iss + 1; 13052 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13053 tcp->tcp_state = TCPS_ESTABLISHED; 13054 13055 /* 13056 * If SYN was retransmitted, need to reset all 13057 * retransmission info. This is because this 13058 * segment will be treated as a dup ACK. 13059 */ 13060 if (tcp->tcp_rexmit) { 13061 tcp->tcp_rexmit = B_FALSE; 13062 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13063 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13064 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13065 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13066 tcp->tcp_ms_we_have_waited = 0; 13067 13068 /* 13069 * Set tcp_cwnd back to 1 MSS, per 13070 * recommendation from 13071 * draft-floyd-incr-init-win-01.txt, 13072 * Increasing TCP's Initial Window. 13073 */ 13074 tcp->tcp_cwnd = tcp->tcp_mss; 13075 } 13076 13077 tcp->tcp_swl1 = seg_seq; 13078 tcp->tcp_swl2 = seg_ack; 13079 13080 new_swnd = BE16_TO_U16(tcph->th_win); 13081 tcp->tcp_swnd = new_swnd; 13082 if (new_swnd > tcp->tcp_max_swnd) 13083 tcp->tcp_max_swnd = new_swnd; 13084 13085 /* 13086 * Always send the three-way handshake ack immediately 13087 * in order to make the connection complete as soon as 13088 * possible on the accepting host. 13089 */ 13090 flags |= TH_ACK_NEEDED; 13091 13092 /* 13093 * Special case for loopback. At this point we have 13094 * received SYN-ACK from the remote endpoint. In 13095 * order to ensure that both endpoints reach the 13096 * fused state prior to any data exchange, the final 13097 * ACK needs to be sent before we indicate T_CONN_CON 13098 * to the module upstream. 13099 */ 13100 if (tcp->tcp_loopback) { 13101 mblk_t *ack_mp; 13102 13103 ASSERT(!tcp->tcp_unfusable); 13104 ASSERT(mp1 != NULL); 13105 /* 13106 * For loopback, we always get a pure SYN-ACK 13107 * and only need to send back the final ACK 13108 * with no data (this is because the other 13109 * tcp is ours and we don't do T/TCP). This 13110 * final ACK triggers the passive side to 13111 * perform fusion in ESTABLISHED state. 13112 */ 13113 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13114 if (tcp->tcp_ack_tid != 0) { 13115 (void) TCP_TIMER_CANCEL(tcp, 13116 tcp->tcp_ack_tid); 13117 tcp->tcp_ack_tid = 0; 13118 } 13119 TCP_RECORD_TRACE(tcp, ack_mp, 13120 TCP_TRACE_SEND_PKT); 13121 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13122 BUMP_LOCAL(tcp->tcp_obsegs); 13123 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13124 13125 /* Send up T_CONN_CON */ 13126 putnext(tcp->tcp_rq, mp1); 13127 13128 freemsg(mp); 13129 return; 13130 } 13131 /* 13132 * Forget fusion; we need to handle more 13133 * complex cases below. Send the deferred 13134 * T_CONN_CON message upstream and proceed 13135 * as usual. Mark this tcp as not capable 13136 * of fusion. 13137 */ 13138 TCP_STAT(tcps, tcp_fusion_unfusable); 13139 tcp->tcp_unfusable = B_TRUE; 13140 putnext(tcp->tcp_rq, mp1); 13141 } 13142 13143 /* 13144 * Check to see if there is data to be sent. If 13145 * yes, set the transmit flag. Then check to see 13146 * if received data processing needs to be done. 13147 * If not, go straight to xmit_check. This short 13148 * cut is OK as we don't support T/TCP. 13149 */ 13150 if (tcp->tcp_unsent) 13151 flags |= TH_XMIT_NEEDED; 13152 13153 if (seg_len == 0 && !(flags & TH_URG)) { 13154 freemsg(mp); 13155 goto xmit_check; 13156 } 13157 13158 flags &= ~TH_SYN; 13159 seg_seq++; 13160 break; 13161 } 13162 tcp->tcp_state = TCPS_SYN_RCVD; 13163 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13164 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13165 if (mp1) { 13166 DB_CPID(mp1) = tcp->tcp_cpid; 13167 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 13168 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13169 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13170 } 13171 freemsg(mp); 13172 return; 13173 case TCPS_SYN_RCVD: 13174 if (flags & TH_ACK) { 13175 /* 13176 * In this state, a SYN|ACK packet is either bogus 13177 * because the other side must be ACKing our SYN which 13178 * indicates it has seen the ACK for their SYN and 13179 * shouldn't retransmit it or we're crossing SYNs 13180 * on active open. 13181 */ 13182 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13183 freemsg(mp); 13184 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13185 tcp, seg_ack, 0, TH_RST); 13186 return; 13187 } 13188 /* 13189 * NOTE: RFC 793 pg. 72 says this should be 13190 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13191 * but that would mean we have an ack that ignored 13192 * our SYN. 13193 */ 13194 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13195 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13196 freemsg(mp); 13197 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13198 tcp, seg_ack, 0, TH_RST); 13199 return; 13200 } 13201 } 13202 break; 13203 case TCPS_LISTEN: 13204 /* 13205 * Only a TLI listener can come through this path when a 13206 * acceptor is going back to be a listener and a packet 13207 * for the acceptor hits the classifier. For a socket 13208 * listener, this can never happen because a listener 13209 * can never accept connection on itself and hence a 13210 * socket acceptor can not go back to being a listener. 13211 */ 13212 ASSERT(!TCP_IS_SOCKET(tcp)); 13213 /*FALLTHRU*/ 13214 case TCPS_CLOSED: 13215 case TCPS_BOUND: { 13216 conn_t *new_connp; 13217 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13218 13219 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13220 if (new_connp != NULL) { 13221 tcp_reinput(new_connp, mp, connp->conn_sqp); 13222 return; 13223 } 13224 /* We failed to classify. For now just drop the packet */ 13225 freemsg(mp); 13226 return; 13227 } 13228 case TCPS_IDLE: 13229 /* 13230 * Handle the case where the tcp_clean_death() has happened 13231 * on a connection (application hasn't closed yet) but a packet 13232 * was already queued on squeue before tcp_clean_death() 13233 * was processed. Calling tcp_clean_death() twice on same 13234 * connection can result in weird behaviour. 13235 */ 13236 freemsg(mp); 13237 return; 13238 default: 13239 break; 13240 } 13241 13242 /* 13243 * Already on the correct queue/perimeter. 13244 * If this is a detached connection and not an eager 13245 * connection hanging off a listener then new data 13246 * (past the FIN) will cause a reset. 13247 * We do a special check here where it 13248 * is out of the main line, rather than check 13249 * if we are detached every time we see new 13250 * data down below. 13251 */ 13252 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13253 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13254 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13255 TCP_RECORD_TRACE(tcp, 13256 mp, TCP_TRACE_RECV_PKT); 13257 13258 freemsg(mp); 13259 /* 13260 * This could be an SSL closure alert. We're detached so just 13261 * acknowledge it this last time. 13262 */ 13263 if (tcp->tcp_kssl_ctx != NULL) { 13264 kssl_release_ctx(tcp->tcp_kssl_ctx); 13265 tcp->tcp_kssl_ctx = NULL; 13266 13267 tcp->tcp_rnxt += seg_len; 13268 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13269 flags |= TH_ACK_NEEDED; 13270 goto ack_check; 13271 } 13272 13273 tcp_xmit_ctl("new data when detached", tcp, 13274 tcp->tcp_snxt, 0, TH_RST); 13275 (void) tcp_clean_death(tcp, EPROTO, 12); 13276 return; 13277 } 13278 13279 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13280 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13281 new_swnd = BE16_TO_U16(tcph->th_win) << 13282 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13283 13284 if (tcp->tcp_snd_ts_ok) { 13285 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13286 /* 13287 * This segment is not acceptable. 13288 * Drop it and send back an ACK. 13289 */ 13290 freemsg(mp); 13291 flags |= TH_ACK_NEEDED; 13292 goto ack_check; 13293 } 13294 } else if (tcp->tcp_snd_sack_ok) { 13295 ASSERT(tcp->tcp_sack_info != NULL); 13296 tcpopt.tcp = tcp; 13297 /* 13298 * SACK info in already updated in tcp_parse_options. Ignore 13299 * all other TCP options... 13300 */ 13301 (void) tcp_parse_options(tcph, &tcpopt); 13302 } 13303 try_again:; 13304 mss = tcp->tcp_mss; 13305 gap = seg_seq - tcp->tcp_rnxt; 13306 rgap = tcp->tcp_rwnd - (gap + seg_len); 13307 /* 13308 * gap is the amount of sequence space between what we expect to see 13309 * and what we got for seg_seq. A positive value for gap means 13310 * something got lost. A negative value means we got some old stuff. 13311 */ 13312 if (gap < 0) { 13313 /* Old stuff present. Is the SYN in there? */ 13314 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13315 (seg_len != 0)) { 13316 flags &= ~TH_SYN; 13317 seg_seq++; 13318 urp--; 13319 /* Recompute the gaps after noting the SYN. */ 13320 goto try_again; 13321 } 13322 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13323 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13324 (seg_len > -gap ? -gap : seg_len)); 13325 /* Remove the old stuff from seg_len. */ 13326 seg_len += gap; 13327 /* 13328 * Anything left? 13329 * Make sure to check for unack'd FIN when rest of data 13330 * has been previously ack'd. 13331 */ 13332 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13333 /* 13334 * Resets are only valid if they lie within our offered 13335 * window. If the RST bit is set, we just ignore this 13336 * segment. 13337 */ 13338 if (flags & TH_RST) { 13339 freemsg(mp); 13340 return; 13341 } 13342 13343 /* 13344 * The arriving of dup data packets indicate that we 13345 * may have postponed an ack for too long, or the other 13346 * side's RTT estimate is out of shape. Start acking 13347 * more often. 13348 */ 13349 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13350 tcp->tcp_rack_cnt >= 1 && 13351 tcp->tcp_rack_abs_max > 2) { 13352 tcp->tcp_rack_abs_max--; 13353 } 13354 tcp->tcp_rack_cur_max = 1; 13355 13356 /* 13357 * This segment is "unacceptable". None of its 13358 * sequence space lies within our advertized window. 13359 * 13360 * Adjust seg_len to the original value for tracing. 13361 */ 13362 seg_len -= gap; 13363 if (tcp->tcp_debug) { 13364 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13365 "tcp_rput: unacceptable, gap %d, rgap %d, " 13366 "flags 0x%x, seg_seq %u, seg_ack %u, " 13367 "seg_len %d, rnxt %u, snxt %u, %s", 13368 gap, rgap, flags, seg_seq, seg_ack, 13369 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13370 tcp_display(tcp, NULL, 13371 DISP_ADDR_AND_PORT)); 13372 } 13373 13374 /* 13375 * Arrange to send an ACK in response to the 13376 * unacceptable segment per RFC 793 page 69. There 13377 * is only one small difference between ours and the 13378 * acceptability test in the RFC - we accept ACK-only 13379 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13380 * will be generated. 13381 * 13382 * Note that we have to ACK an ACK-only packet at least 13383 * for stacks that send 0-length keep-alives with 13384 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13385 * section 4.2.3.6. As long as we don't ever generate 13386 * an unacceptable packet in response to an incoming 13387 * packet that is unacceptable, it should not cause 13388 * "ACK wars". 13389 */ 13390 flags |= TH_ACK_NEEDED; 13391 13392 /* 13393 * Continue processing this segment in order to use the 13394 * ACK information it contains, but skip all other 13395 * sequence-number processing. Processing the ACK 13396 * information is necessary in order to 13397 * re-synchronize connections that may have lost 13398 * synchronization. 13399 * 13400 * We clear seg_len and flag fields related to 13401 * sequence number processing as they are not 13402 * to be trusted for an unacceptable segment. 13403 */ 13404 seg_len = 0; 13405 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13406 goto process_ack; 13407 } 13408 13409 /* Fix seg_seq, and chew the gap off the front. */ 13410 seg_seq = tcp->tcp_rnxt; 13411 urp += gap; 13412 do { 13413 mblk_t *mp2; 13414 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13415 (uintptr_t)UINT_MAX); 13416 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13417 if (gap > 0) { 13418 mp->b_rptr = mp->b_wptr - gap; 13419 break; 13420 } 13421 mp2 = mp; 13422 mp = mp->b_cont; 13423 freeb(mp2); 13424 } while (gap < 0); 13425 /* 13426 * If the urgent data has already been acknowledged, we 13427 * should ignore TH_URG below 13428 */ 13429 if (urp < 0) 13430 flags &= ~TH_URG; 13431 } 13432 /* 13433 * rgap is the amount of stuff received out of window. A negative 13434 * value is the amount out of window. 13435 */ 13436 if (rgap < 0) { 13437 mblk_t *mp2; 13438 13439 if (tcp->tcp_rwnd == 0) { 13440 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13441 } else { 13442 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13443 UPDATE_MIB(&tcps->tcps_mib, 13444 tcpInDataPastWinBytes, -rgap); 13445 } 13446 13447 /* 13448 * seg_len does not include the FIN, so if more than 13449 * just the FIN is out of window, we act like we don't 13450 * see it. (If just the FIN is out of window, rgap 13451 * will be zero and we will go ahead and acknowledge 13452 * the FIN.) 13453 */ 13454 flags &= ~TH_FIN; 13455 13456 /* Fix seg_len and make sure there is something left. */ 13457 seg_len += rgap; 13458 if (seg_len <= 0) { 13459 /* 13460 * Resets are only valid if they lie within our offered 13461 * window. If the RST bit is set, we just ignore this 13462 * segment. 13463 */ 13464 if (flags & TH_RST) { 13465 freemsg(mp); 13466 return; 13467 } 13468 13469 /* Per RFC 793, we need to send back an ACK. */ 13470 flags |= TH_ACK_NEEDED; 13471 13472 /* 13473 * Send SIGURG as soon as possible i.e. even 13474 * if the TH_URG was delivered in a window probe 13475 * packet (which will be unacceptable). 13476 * 13477 * We generate a signal if none has been generated 13478 * for this connection or if this is a new urgent 13479 * byte. Also send a zero-length "unmarked" message 13480 * to inform SIOCATMARK that this is not the mark. 13481 * 13482 * tcp_urp_last_valid is cleared when the T_exdata_ind 13483 * is sent up. This plus the check for old data 13484 * (gap >= 0) handles the wraparound of the sequence 13485 * number space without having to always track the 13486 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13487 * this max in its rcv_up variable). 13488 * 13489 * This prevents duplicate SIGURGS due to a "late" 13490 * zero-window probe when the T_EXDATA_IND has already 13491 * been sent up. 13492 */ 13493 if ((flags & TH_URG) && 13494 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13495 tcp->tcp_urp_last))) { 13496 mp1 = allocb(0, BPRI_MED); 13497 if (mp1 == NULL) { 13498 freemsg(mp); 13499 return; 13500 } 13501 if (!TCP_IS_DETACHED(tcp) && 13502 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13503 SIGURG)) { 13504 /* Try again on the rexmit. */ 13505 freemsg(mp1); 13506 freemsg(mp); 13507 return; 13508 } 13509 /* 13510 * If the next byte would be the mark 13511 * then mark with MARKNEXT else mark 13512 * with NOTMARKNEXT. 13513 */ 13514 if (gap == 0 && urp == 0) 13515 mp1->b_flag |= MSGMARKNEXT; 13516 else 13517 mp1->b_flag |= MSGNOTMARKNEXT; 13518 freemsg(tcp->tcp_urp_mark_mp); 13519 tcp->tcp_urp_mark_mp = mp1; 13520 flags |= TH_SEND_URP_MARK; 13521 tcp->tcp_urp_last_valid = B_TRUE; 13522 tcp->tcp_urp_last = urp + seg_seq; 13523 } 13524 /* 13525 * If this is a zero window probe, continue to 13526 * process the ACK part. But we need to set seg_len 13527 * to 0 to avoid data processing. Otherwise just 13528 * drop the segment and send back an ACK. 13529 */ 13530 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13531 flags &= ~(TH_SYN | TH_URG); 13532 seg_len = 0; 13533 goto process_ack; 13534 } else { 13535 freemsg(mp); 13536 goto ack_check; 13537 } 13538 } 13539 /* Pitch out of window stuff off the end. */ 13540 rgap = seg_len; 13541 mp2 = mp; 13542 do { 13543 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13544 (uintptr_t)INT_MAX); 13545 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13546 if (rgap < 0) { 13547 mp2->b_wptr += rgap; 13548 if ((mp1 = mp2->b_cont) != NULL) { 13549 mp2->b_cont = NULL; 13550 freemsg(mp1); 13551 } 13552 break; 13553 } 13554 } while ((mp2 = mp2->b_cont) != NULL); 13555 } 13556 ok:; 13557 /* 13558 * TCP should check ECN info for segments inside the window only. 13559 * Therefore the check should be done here. 13560 */ 13561 if (tcp->tcp_ecn_ok) { 13562 if (flags & TH_CWR) { 13563 tcp->tcp_ecn_echo_on = B_FALSE; 13564 } 13565 /* 13566 * Note that both ECN_CE and CWR can be set in the 13567 * same segment. In this case, we once again turn 13568 * on ECN_ECHO. 13569 */ 13570 if (tcp->tcp_ipversion == IPV4_VERSION) { 13571 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13572 13573 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13574 tcp->tcp_ecn_echo_on = B_TRUE; 13575 } 13576 } else { 13577 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13578 13579 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13580 htonl(IPH_ECN_CE << 20)) { 13581 tcp->tcp_ecn_echo_on = B_TRUE; 13582 } 13583 } 13584 } 13585 13586 /* 13587 * Check whether we can update tcp_ts_recent. This test is 13588 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13589 * Extensions for High Performance: An Update", Internet Draft. 13590 */ 13591 if (tcp->tcp_snd_ts_ok && 13592 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13593 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13594 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13595 tcp->tcp_last_rcv_lbolt = lbolt64; 13596 } 13597 13598 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13599 /* 13600 * FIN in an out of order segment. We record this in 13601 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13602 * Clear the FIN so that any check on FIN flag will fail. 13603 * Remember that FIN also counts in the sequence number 13604 * space. So we need to ack out of order FIN only segments. 13605 */ 13606 if (flags & TH_FIN) { 13607 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13608 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13609 flags &= ~TH_FIN; 13610 flags |= TH_ACK_NEEDED; 13611 } 13612 if (seg_len > 0) { 13613 /* Fill in the SACK blk list. */ 13614 if (tcp->tcp_snd_sack_ok) { 13615 ASSERT(tcp->tcp_sack_info != NULL); 13616 tcp_sack_insert(tcp->tcp_sack_list, 13617 seg_seq, seg_seq + seg_len, 13618 &(tcp->tcp_num_sack_blk)); 13619 } 13620 13621 /* 13622 * Attempt reassembly and see if we have something 13623 * ready to go. 13624 */ 13625 mp = tcp_reass(tcp, mp, seg_seq); 13626 /* Always ack out of order packets */ 13627 flags |= TH_ACK_NEEDED | TH_PUSH; 13628 if (mp) { 13629 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13630 (uintptr_t)INT_MAX); 13631 seg_len = mp->b_cont ? msgdsize(mp) : 13632 (int)(mp->b_wptr - mp->b_rptr); 13633 seg_seq = tcp->tcp_rnxt; 13634 /* 13635 * A gap is filled and the seq num and len 13636 * of the gap match that of a previously 13637 * received FIN, put the FIN flag back in. 13638 */ 13639 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13640 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13641 flags |= TH_FIN; 13642 tcp->tcp_valid_bits &= 13643 ~TCP_OFO_FIN_VALID; 13644 } 13645 } else { 13646 /* 13647 * Keep going even with NULL mp. 13648 * There may be a useful ACK or something else 13649 * we don't want to miss. 13650 * 13651 * But TCP should not perform fast retransmit 13652 * because of the ack number. TCP uses 13653 * seg_len == 0 to determine if it is a pure 13654 * ACK. And this is not a pure ACK. 13655 */ 13656 seg_len = 0; 13657 ofo_seg = B_TRUE; 13658 } 13659 } 13660 } else if (seg_len > 0) { 13661 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13662 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13663 /* 13664 * If an out of order FIN was received before, and the seq 13665 * num and len of the new segment match that of the FIN, 13666 * put the FIN flag back in. 13667 */ 13668 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13669 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13670 flags |= TH_FIN; 13671 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13672 } 13673 } 13674 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13675 if (flags & TH_RST) { 13676 freemsg(mp); 13677 switch (tcp->tcp_state) { 13678 case TCPS_SYN_RCVD: 13679 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13680 break; 13681 case TCPS_ESTABLISHED: 13682 case TCPS_FIN_WAIT_1: 13683 case TCPS_FIN_WAIT_2: 13684 case TCPS_CLOSE_WAIT: 13685 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13686 break; 13687 case TCPS_CLOSING: 13688 case TCPS_LAST_ACK: 13689 (void) tcp_clean_death(tcp, 0, 16); 13690 break; 13691 default: 13692 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13693 (void) tcp_clean_death(tcp, ENXIO, 17); 13694 break; 13695 } 13696 return; 13697 } 13698 if (flags & TH_SYN) { 13699 /* 13700 * See RFC 793, Page 71 13701 * 13702 * The seq number must be in the window as it should 13703 * be "fixed" above. If it is outside window, it should 13704 * be already rejected. Note that we allow seg_seq to be 13705 * rnxt + rwnd because we want to accept 0 window probe. 13706 */ 13707 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13708 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13709 freemsg(mp); 13710 /* 13711 * If the ACK flag is not set, just use our snxt as the 13712 * seq number of the RST segment. 13713 */ 13714 if (!(flags & TH_ACK)) { 13715 seg_ack = tcp->tcp_snxt; 13716 } 13717 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13718 TH_RST|TH_ACK); 13719 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13720 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13721 return; 13722 } 13723 /* 13724 * urp could be -1 when the urp field in the packet is 0 13725 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13726 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13727 */ 13728 if (flags & TH_URG && urp >= 0) { 13729 if (!tcp->tcp_urp_last_valid || 13730 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13731 /* 13732 * If we haven't generated the signal yet for this 13733 * urgent pointer value, do it now. Also, send up a 13734 * zero-length M_DATA indicating whether or not this is 13735 * the mark. The latter is not needed when a 13736 * T_EXDATA_IND is sent up. However, if there are 13737 * allocation failures this code relies on the sender 13738 * retransmitting and the socket code for determining 13739 * the mark should not block waiting for the peer to 13740 * transmit. Thus, for simplicity we always send up the 13741 * mark indication. 13742 */ 13743 mp1 = allocb(0, BPRI_MED); 13744 if (mp1 == NULL) { 13745 freemsg(mp); 13746 return; 13747 } 13748 if (!TCP_IS_DETACHED(tcp) && 13749 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13750 /* Try again on the rexmit. */ 13751 freemsg(mp1); 13752 freemsg(mp); 13753 return; 13754 } 13755 /* 13756 * Mark with NOTMARKNEXT for now. 13757 * The code below will change this to MARKNEXT 13758 * if we are at the mark. 13759 * 13760 * If there are allocation failures (e.g. in dupmsg 13761 * below) the next time tcp_rput_data sees the urgent 13762 * segment it will send up the MSG*MARKNEXT message. 13763 */ 13764 mp1->b_flag |= MSGNOTMARKNEXT; 13765 freemsg(tcp->tcp_urp_mark_mp); 13766 tcp->tcp_urp_mark_mp = mp1; 13767 flags |= TH_SEND_URP_MARK; 13768 #ifdef DEBUG 13769 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13770 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13771 "last %x, %s", 13772 seg_seq, urp, tcp->tcp_urp_last, 13773 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13774 #endif /* DEBUG */ 13775 tcp->tcp_urp_last_valid = B_TRUE; 13776 tcp->tcp_urp_last = urp + seg_seq; 13777 } else if (tcp->tcp_urp_mark_mp != NULL) { 13778 /* 13779 * An allocation failure prevented the previous 13780 * tcp_rput_data from sending up the allocated 13781 * MSG*MARKNEXT message - send it up this time 13782 * around. 13783 */ 13784 flags |= TH_SEND_URP_MARK; 13785 } 13786 13787 /* 13788 * If the urgent byte is in this segment, make sure that it is 13789 * all by itself. This makes it much easier to deal with the 13790 * possibility of an allocation failure on the T_exdata_ind. 13791 * Note that seg_len is the number of bytes in the segment, and 13792 * urp is the offset into the segment of the urgent byte. 13793 * urp < seg_len means that the urgent byte is in this segment. 13794 */ 13795 if (urp < seg_len) { 13796 if (seg_len != 1) { 13797 uint32_t tmp_rnxt; 13798 /* 13799 * Break it up and feed it back in. 13800 * Re-attach the IP header. 13801 */ 13802 mp->b_rptr = iphdr; 13803 if (urp > 0) { 13804 /* 13805 * There is stuff before the urgent 13806 * byte. 13807 */ 13808 mp1 = dupmsg(mp); 13809 if (!mp1) { 13810 /* 13811 * Trim from urgent byte on. 13812 * The rest will come back. 13813 */ 13814 (void) adjmsg(mp, 13815 urp - seg_len); 13816 tcp_rput_data(connp, 13817 mp, NULL); 13818 return; 13819 } 13820 (void) adjmsg(mp1, urp - seg_len); 13821 /* Feed this piece back in. */ 13822 tmp_rnxt = tcp->tcp_rnxt; 13823 tcp_rput_data(connp, mp1, NULL); 13824 /* 13825 * If the data passed back in was not 13826 * processed (ie: bad ACK) sending 13827 * the remainder back in will cause a 13828 * loop. In this case, drop the 13829 * packet and let the sender try 13830 * sending a good packet. 13831 */ 13832 if (tmp_rnxt == tcp->tcp_rnxt) { 13833 freemsg(mp); 13834 return; 13835 } 13836 } 13837 if (urp != seg_len - 1) { 13838 uint32_t tmp_rnxt; 13839 /* 13840 * There is stuff after the urgent 13841 * byte. 13842 */ 13843 mp1 = dupmsg(mp); 13844 if (!mp1) { 13845 /* 13846 * Trim everything beyond the 13847 * urgent byte. The rest will 13848 * come back. 13849 */ 13850 (void) adjmsg(mp, 13851 urp + 1 - seg_len); 13852 tcp_rput_data(connp, 13853 mp, NULL); 13854 return; 13855 } 13856 (void) adjmsg(mp1, urp + 1 - seg_len); 13857 tmp_rnxt = tcp->tcp_rnxt; 13858 tcp_rput_data(connp, mp1, NULL); 13859 /* 13860 * If the data passed back in was not 13861 * processed (ie: bad ACK) sending 13862 * the remainder back in will cause a 13863 * loop. In this case, drop the 13864 * packet and let the sender try 13865 * sending a good packet. 13866 */ 13867 if (tmp_rnxt == tcp->tcp_rnxt) { 13868 freemsg(mp); 13869 return; 13870 } 13871 } 13872 tcp_rput_data(connp, mp, NULL); 13873 return; 13874 } 13875 /* 13876 * This segment contains only the urgent byte. We 13877 * have to allocate the T_exdata_ind, if we can. 13878 */ 13879 if (!tcp->tcp_urp_mp) { 13880 struct T_exdata_ind *tei; 13881 mp1 = allocb(sizeof (struct T_exdata_ind), 13882 BPRI_MED); 13883 if (!mp1) { 13884 /* 13885 * Sigh... It'll be back. 13886 * Generate any MSG*MARK message now. 13887 */ 13888 freemsg(mp); 13889 seg_len = 0; 13890 if (flags & TH_SEND_URP_MARK) { 13891 13892 13893 ASSERT(tcp->tcp_urp_mark_mp); 13894 tcp->tcp_urp_mark_mp->b_flag &= 13895 ~MSGNOTMARKNEXT; 13896 tcp->tcp_urp_mark_mp->b_flag |= 13897 MSGMARKNEXT; 13898 } 13899 goto ack_check; 13900 } 13901 mp1->b_datap->db_type = M_PROTO; 13902 tei = (struct T_exdata_ind *)mp1->b_rptr; 13903 tei->PRIM_type = T_EXDATA_IND; 13904 tei->MORE_flag = 0; 13905 mp1->b_wptr = (uchar_t *)&tei[1]; 13906 tcp->tcp_urp_mp = mp1; 13907 #ifdef DEBUG 13908 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13909 "tcp_rput: allocated exdata_ind %s", 13910 tcp_display(tcp, NULL, 13911 DISP_PORT_ONLY)); 13912 #endif /* DEBUG */ 13913 /* 13914 * There is no need to send a separate MSG*MARK 13915 * message since the T_EXDATA_IND will be sent 13916 * now. 13917 */ 13918 flags &= ~TH_SEND_URP_MARK; 13919 freemsg(tcp->tcp_urp_mark_mp); 13920 tcp->tcp_urp_mark_mp = NULL; 13921 } 13922 /* 13923 * Now we are all set. On the next putnext upstream, 13924 * tcp_urp_mp will be non-NULL and will get prepended 13925 * to what has to be this piece containing the urgent 13926 * byte. If for any reason we abort this segment below, 13927 * if it comes back, we will have this ready, or it 13928 * will get blown off in close. 13929 */ 13930 } else if (urp == seg_len) { 13931 /* 13932 * The urgent byte is the next byte after this sequence 13933 * number. If there is data it is marked with 13934 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13935 * since it is not needed. Otherwise, if the code 13936 * above just allocated a zero-length tcp_urp_mark_mp 13937 * message, that message is tagged with MSGMARKNEXT. 13938 * Sending up these MSGMARKNEXT messages makes 13939 * SIOCATMARK work correctly even though 13940 * the T_EXDATA_IND will not be sent up until the 13941 * urgent byte arrives. 13942 */ 13943 if (seg_len != 0) { 13944 flags |= TH_MARKNEXT_NEEDED; 13945 freemsg(tcp->tcp_urp_mark_mp); 13946 tcp->tcp_urp_mark_mp = NULL; 13947 flags &= ~TH_SEND_URP_MARK; 13948 } else if (tcp->tcp_urp_mark_mp != NULL) { 13949 flags |= TH_SEND_URP_MARK; 13950 tcp->tcp_urp_mark_mp->b_flag &= 13951 ~MSGNOTMARKNEXT; 13952 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13953 } 13954 #ifdef DEBUG 13955 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13956 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13957 seg_len, flags, 13958 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13959 #endif /* DEBUG */ 13960 } else { 13961 /* Data left until we hit mark */ 13962 #ifdef DEBUG 13963 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13964 "tcp_rput: URP %d bytes left, %s", 13965 urp - seg_len, tcp_display(tcp, NULL, 13966 DISP_PORT_ONLY)); 13967 #endif /* DEBUG */ 13968 } 13969 } 13970 13971 process_ack: 13972 if (!(flags & TH_ACK)) { 13973 freemsg(mp); 13974 goto xmit_check; 13975 } 13976 } 13977 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13978 13979 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13980 tcp->tcp_ip_forward_progress = B_TRUE; 13981 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13982 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13983 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13984 /* 3-way handshake complete - pass up the T_CONN_IND */ 13985 tcp_t *listener = tcp->tcp_listener; 13986 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13987 13988 tcp->tcp_tconnind_started = B_TRUE; 13989 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13990 /* 13991 * We are here means eager is fine but it can 13992 * get a TH_RST at any point between now and till 13993 * accept completes and disappear. We need to 13994 * ensure that reference to eager is valid after 13995 * we get out of eager's perimeter. So we do 13996 * an extra refhold. 13997 */ 13998 CONN_INC_REF(connp); 13999 14000 /* 14001 * The listener also exists because of the refhold 14002 * done in tcp_conn_request. Its possible that it 14003 * might have closed. We will check that once we 14004 * get inside listeners context. 14005 */ 14006 CONN_INC_REF(listener->tcp_connp); 14007 if (listener->tcp_connp->conn_sqp == 14008 connp->conn_sqp) { 14009 tcp_send_conn_ind(listener->tcp_connp, mp, 14010 listener->tcp_connp->conn_sqp); 14011 CONN_DEC_REF(listener->tcp_connp); 14012 } else if (!tcp->tcp_loopback) { 14013 squeue_fill(listener->tcp_connp->conn_sqp, mp, 14014 tcp_send_conn_ind, 14015 listener->tcp_connp, SQTAG_TCP_CONN_IND); 14016 } else { 14017 squeue_enter(listener->tcp_connp->conn_sqp, mp, 14018 tcp_send_conn_ind, listener->tcp_connp, 14019 SQTAG_TCP_CONN_IND); 14020 } 14021 } 14022 14023 if (tcp->tcp_active_open) { 14024 /* 14025 * We are seeing the final ack in the three way 14026 * hand shake of a active open'ed connection 14027 * so we must send up a T_CONN_CON 14028 */ 14029 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14030 freemsg(mp); 14031 return; 14032 } 14033 /* 14034 * Don't fuse the loopback endpoints for 14035 * simultaneous active opens. 14036 */ 14037 if (tcp->tcp_loopback) { 14038 TCP_STAT(tcps, tcp_fusion_unfusable); 14039 tcp->tcp_unfusable = B_TRUE; 14040 } 14041 } 14042 14043 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14044 bytes_acked--; 14045 /* SYN was acked - making progress */ 14046 if (tcp->tcp_ipversion == IPV6_VERSION) 14047 tcp->tcp_ip_forward_progress = B_TRUE; 14048 14049 /* 14050 * If SYN was retransmitted, need to reset all 14051 * retransmission info as this segment will be 14052 * treated as a dup ACK. 14053 */ 14054 if (tcp->tcp_rexmit) { 14055 tcp->tcp_rexmit = B_FALSE; 14056 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14057 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14058 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14059 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14060 tcp->tcp_ms_we_have_waited = 0; 14061 tcp->tcp_cwnd = mss; 14062 } 14063 14064 /* 14065 * We set the send window to zero here. 14066 * This is needed if there is data to be 14067 * processed already on the queue. 14068 * Later (at swnd_update label), the 14069 * "new_swnd > tcp_swnd" condition is satisfied 14070 * the XMIT_NEEDED flag is set in the current 14071 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14072 * called if there is already data on queue in 14073 * this state. 14074 */ 14075 tcp->tcp_swnd = 0; 14076 14077 if (new_swnd > tcp->tcp_max_swnd) 14078 tcp->tcp_max_swnd = new_swnd; 14079 tcp->tcp_swl1 = seg_seq; 14080 tcp->tcp_swl2 = seg_ack; 14081 tcp->tcp_state = TCPS_ESTABLISHED; 14082 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14083 14084 /* Fuse when both sides are in ESTABLISHED state */ 14085 if (tcp->tcp_loopback && do_tcp_fusion) 14086 tcp_fuse(tcp, iphdr, tcph); 14087 14088 } 14089 /* This code follows 4.4BSD-Lite2 mostly. */ 14090 if (bytes_acked < 0) 14091 goto est; 14092 14093 /* 14094 * If TCP is ECN capable and the congestion experience bit is 14095 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14096 * done once per window (or more loosely, per RTT). 14097 */ 14098 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14099 tcp->tcp_cwr = B_FALSE; 14100 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14101 if (!tcp->tcp_cwr) { 14102 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14103 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14104 tcp->tcp_cwnd = npkt * mss; 14105 /* 14106 * If the cwnd is 0, use the timer to clock out 14107 * new segments. This is required by the ECN spec. 14108 */ 14109 if (npkt == 0) { 14110 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14111 /* 14112 * This makes sure that when the ACK comes 14113 * back, we will increase tcp_cwnd by 1 MSS. 14114 */ 14115 tcp->tcp_cwnd_cnt = 0; 14116 } 14117 tcp->tcp_cwr = B_TRUE; 14118 /* 14119 * This marks the end of the current window of in 14120 * flight data. That is why we don't use 14121 * tcp_suna + tcp_swnd. Only data in flight can 14122 * provide ECN info. 14123 */ 14124 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14125 tcp->tcp_ecn_cwr_sent = B_FALSE; 14126 } 14127 } 14128 14129 mp1 = tcp->tcp_xmit_head; 14130 if (bytes_acked == 0) { 14131 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14132 int dupack_cnt; 14133 14134 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14135 /* 14136 * Fast retransmit. When we have seen exactly three 14137 * identical ACKs while we have unacked data 14138 * outstanding we take it as a hint that our peer 14139 * dropped something. 14140 * 14141 * If TCP is retransmitting, don't do fast retransmit. 14142 */ 14143 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14144 ! tcp->tcp_rexmit) { 14145 /* Do Limited Transmit */ 14146 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14147 tcps->tcps_dupack_fast_retransmit) { 14148 /* 14149 * RFC 3042 14150 * 14151 * What we need to do is temporarily 14152 * increase tcp_cwnd so that new 14153 * data can be sent if it is allowed 14154 * by the receive window (tcp_rwnd). 14155 * tcp_wput_data() will take care of 14156 * the rest. 14157 * 14158 * If the connection is SACK capable, 14159 * only do limited xmit when there 14160 * is SACK info. 14161 * 14162 * Note how tcp_cwnd is incremented. 14163 * The first dup ACK will increase 14164 * it by 1 MSS. The second dup ACK 14165 * will increase it by 2 MSS. This 14166 * means that only 1 new segment will 14167 * be sent for each dup ACK. 14168 */ 14169 if (tcp->tcp_unsent > 0 && 14170 (!tcp->tcp_snd_sack_ok || 14171 (tcp->tcp_snd_sack_ok && 14172 tcp->tcp_notsack_list != NULL))) { 14173 tcp->tcp_cwnd += mss << 14174 (tcp->tcp_dupack_cnt - 1); 14175 flags |= TH_LIMIT_XMIT; 14176 } 14177 } else if (dupack_cnt == 14178 tcps->tcps_dupack_fast_retransmit) { 14179 14180 /* 14181 * If we have reduced tcp_ssthresh 14182 * because of ECN, do not reduce it again 14183 * unless it is already one window of data 14184 * away. After one window of data, tcp_cwr 14185 * should then be cleared. Note that 14186 * for non ECN capable connection, tcp_cwr 14187 * should always be false. 14188 * 14189 * Adjust cwnd since the duplicate 14190 * ack indicates that a packet was 14191 * dropped (due to congestion.) 14192 */ 14193 if (!tcp->tcp_cwr) { 14194 npkt = ((tcp->tcp_snxt - 14195 tcp->tcp_suna) >> 1) / mss; 14196 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14197 mss; 14198 tcp->tcp_cwnd = (npkt + 14199 tcp->tcp_dupack_cnt) * mss; 14200 } 14201 if (tcp->tcp_ecn_ok) { 14202 tcp->tcp_cwr = B_TRUE; 14203 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14204 tcp->tcp_ecn_cwr_sent = B_FALSE; 14205 } 14206 14207 /* 14208 * We do Hoe's algorithm. Refer to her 14209 * paper "Improving the Start-up Behavior 14210 * of a Congestion Control Scheme for TCP," 14211 * appeared in SIGCOMM'96. 14212 * 14213 * Save highest seq no we have sent so far. 14214 * Be careful about the invisible FIN byte. 14215 */ 14216 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14217 (tcp->tcp_unsent == 0)) { 14218 tcp->tcp_rexmit_max = tcp->tcp_fss; 14219 } else { 14220 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14221 } 14222 14223 /* 14224 * Do not allow bursty traffic during. 14225 * fast recovery. Refer to Fall and Floyd's 14226 * paper "Simulation-based Comparisons of 14227 * Tahoe, Reno and SACK TCP" (in CCR?) 14228 * This is a best current practise. 14229 */ 14230 tcp->tcp_snd_burst = TCP_CWND_SS; 14231 14232 /* 14233 * For SACK: 14234 * Calculate tcp_pipe, which is the 14235 * estimated number of bytes in 14236 * network. 14237 * 14238 * tcp_fack is the highest sack'ed seq num 14239 * TCP has received. 14240 * 14241 * tcp_pipe is explained in the above quoted 14242 * Fall and Floyd's paper. tcp_fack is 14243 * explained in Mathis and Mahdavi's 14244 * "Forward Acknowledgment: Refining TCP 14245 * Congestion Control" in SIGCOMM '96. 14246 */ 14247 if (tcp->tcp_snd_sack_ok) { 14248 ASSERT(tcp->tcp_sack_info != NULL); 14249 if (tcp->tcp_notsack_list != NULL) { 14250 tcp->tcp_pipe = tcp->tcp_snxt - 14251 tcp->tcp_fack; 14252 tcp->tcp_sack_snxt = seg_ack; 14253 flags |= TH_NEED_SACK_REXMIT; 14254 } else { 14255 /* 14256 * Always initialize tcp_pipe 14257 * even though we don't have 14258 * any SACK info. If later 14259 * we get SACK info and 14260 * tcp_pipe is not initialized, 14261 * funny things will happen. 14262 */ 14263 tcp->tcp_pipe = 14264 tcp->tcp_cwnd_ssthresh; 14265 } 14266 } else { 14267 flags |= TH_REXMIT_NEEDED; 14268 } /* tcp_snd_sack_ok */ 14269 14270 } else { 14271 /* 14272 * Here we perform congestion 14273 * avoidance, but NOT slow start. 14274 * This is known as the Fast 14275 * Recovery Algorithm. 14276 */ 14277 if (tcp->tcp_snd_sack_ok && 14278 tcp->tcp_notsack_list != NULL) { 14279 flags |= TH_NEED_SACK_REXMIT; 14280 tcp->tcp_pipe -= mss; 14281 if (tcp->tcp_pipe < 0) 14282 tcp->tcp_pipe = 0; 14283 } else { 14284 /* 14285 * We know that one more packet has 14286 * left the pipe thus we can update 14287 * cwnd. 14288 */ 14289 cwnd = tcp->tcp_cwnd + mss; 14290 if (cwnd > tcp->tcp_cwnd_max) 14291 cwnd = tcp->tcp_cwnd_max; 14292 tcp->tcp_cwnd = cwnd; 14293 if (tcp->tcp_unsent > 0) 14294 flags |= TH_XMIT_NEEDED; 14295 } 14296 } 14297 } 14298 } else if (tcp->tcp_zero_win_probe) { 14299 /* 14300 * If the window has opened, need to arrange 14301 * to send additional data. 14302 */ 14303 if (new_swnd != 0) { 14304 /* tcp_suna != tcp_snxt */ 14305 /* Packet contains a window update */ 14306 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14307 tcp->tcp_zero_win_probe = 0; 14308 tcp->tcp_timer_backoff = 0; 14309 tcp->tcp_ms_we_have_waited = 0; 14310 14311 /* 14312 * Transmit starting with tcp_suna since 14313 * the one byte probe is not ack'ed. 14314 * If TCP has sent more than one identical 14315 * probe, tcp_rexmit will be set. That means 14316 * tcp_ss_rexmit() will send out the one 14317 * byte along with new data. Otherwise, 14318 * fake the retransmission. 14319 */ 14320 flags |= TH_XMIT_NEEDED; 14321 if (!tcp->tcp_rexmit) { 14322 tcp->tcp_rexmit = B_TRUE; 14323 tcp->tcp_dupack_cnt = 0; 14324 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14325 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14326 } 14327 } 14328 } 14329 goto swnd_update; 14330 } 14331 14332 /* 14333 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14334 * If the ACK value acks something that we have not yet sent, it might 14335 * be an old duplicate segment. Send an ACK to re-synchronize the 14336 * other side. 14337 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14338 * state is handled above, so we can always just drop the segment and 14339 * send an ACK here. 14340 * 14341 * Should we send ACKs in response to ACK only segments? 14342 */ 14343 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14344 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14345 /* drop the received segment */ 14346 freemsg(mp); 14347 14348 /* 14349 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14350 * greater than 0, check if the number of such 14351 * bogus ACks is greater than that count. If yes, 14352 * don't send back any ACK. This prevents TCP from 14353 * getting into an ACK storm if somehow an attacker 14354 * successfully spoofs an acceptable segment to our 14355 * peer. 14356 */ 14357 if (tcp_drop_ack_unsent_cnt > 0 && 14358 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14359 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14360 return; 14361 } 14362 mp = tcp_ack_mp(tcp); 14363 if (mp != NULL) { 14364 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 14365 BUMP_LOCAL(tcp->tcp_obsegs); 14366 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14367 tcp_send_data(tcp, tcp->tcp_wq, mp); 14368 } 14369 return; 14370 } 14371 14372 /* 14373 * TCP gets a new ACK, update the notsack'ed list to delete those 14374 * blocks that are covered by this ACK. 14375 */ 14376 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14377 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14378 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14379 } 14380 14381 /* 14382 * If we got an ACK after fast retransmit, check to see 14383 * if it is a partial ACK. If it is not and the congestion 14384 * window was inflated to account for the other side's 14385 * cached packets, retract it. If it is, do Hoe's algorithm. 14386 */ 14387 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14388 ASSERT(tcp->tcp_rexmit == B_FALSE); 14389 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14390 tcp->tcp_dupack_cnt = 0; 14391 /* 14392 * Restore the orig tcp_cwnd_ssthresh after 14393 * fast retransmit phase. 14394 */ 14395 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14396 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14397 } 14398 tcp->tcp_rexmit_max = seg_ack; 14399 tcp->tcp_cwnd_cnt = 0; 14400 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14401 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14402 14403 /* 14404 * Remove all notsack info to avoid confusion with 14405 * the next fast retrasnmit/recovery phase. 14406 */ 14407 if (tcp->tcp_snd_sack_ok && 14408 tcp->tcp_notsack_list != NULL) { 14409 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14410 } 14411 } else { 14412 if (tcp->tcp_snd_sack_ok && 14413 tcp->tcp_notsack_list != NULL) { 14414 flags |= TH_NEED_SACK_REXMIT; 14415 tcp->tcp_pipe -= mss; 14416 if (tcp->tcp_pipe < 0) 14417 tcp->tcp_pipe = 0; 14418 } else { 14419 /* 14420 * Hoe's algorithm: 14421 * 14422 * Retransmit the unack'ed segment and 14423 * restart fast recovery. Note that we 14424 * need to scale back tcp_cwnd to the 14425 * original value when we started fast 14426 * recovery. This is to prevent overly 14427 * aggressive behaviour in sending new 14428 * segments. 14429 */ 14430 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14431 tcps->tcps_dupack_fast_retransmit * mss; 14432 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14433 flags |= TH_REXMIT_NEEDED; 14434 } 14435 } 14436 } else { 14437 tcp->tcp_dupack_cnt = 0; 14438 if (tcp->tcp_rexmit) { 14439 /* 14440 * TCP is retranmitting. If the ACK ack's all 14441 * outstanding data, update tcp_rexmit_max and 14442 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14443 * to the correct value. 14444 * 14445 * Note that SEQ_LEQ() is used. This is to avoid 14446 * unnecessary fast retransmit caused by dup ACKs 14447 * received when TCP does slow start retransmission 14448 * after a time out. During this phase, TCP may 14449 * send out segments which are already received. 14450 * This causes dup ACKs to be sent back. 14451 */ 14452 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14453 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14454 tcp->tcp_rexmit_nxt = seg_ack; 14455 } 14456 if (seg_ack != tcp->tcp_rexmit_max) { 14457 flags |= TH_XMIT_NEEDED; 14458 } 14459 } else { 14460 tcp->tcp_rexmit = B_FALSE; 14461 tcp->tcp_xmit_zc_clean = B_FALSE; 14462 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14463 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14464 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14465 } 14466 tcp->tcp_ms_we_have_waited = 0; 14467 } 14468 } 14469 14470 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14471 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14472 tcp->tcp_suna = seg_ack; 14473 if (tcp->tcp_zero_win_probe != 0) { 14474 tcp->tcp_zero_win_probe = 0; 14475 tcp->tcp_timer_backoff = 0; 14476 } 14477 14478 /* 14479 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14480 * Note that it cannot be the SYN being ack'ed. The code flow 14481 * will not reach here. 14482 */ 14483 if (mp1 == NULL) { 14484 goto fin_acked; 14485 } 14486 14487 /* 14488 * Update the congestion window. 14489 * 14490 * If TCP is not ECN capable or TCP is ECN capable but the 14491 * congestion experience bit is not set, increase the tcp_cwnd as 14492 * usual. 14493 */ 14494 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14495 cwnd = tcp->tcp_cwnd; 14496 add = mss; 14497 14498 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14499 /* 14500 * This is to prevent an increase of less than 1 MSS of 14501 * tcp_cwnd. With partial increase, tcp_wput_data() 14502 * may send out tinygrams in order to preserve mblk 14503 * boundaries. 14504 * 14505 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14506 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14507 * increased by 1 MSS for every RTTs. 14508 */ 14509 if (tcp->tcp_cwnd_cnt <= 0) { 14510 tcp->tcp_cwnd_cnt = cwnd + add; 14511 } else { 14512 tcp->tcp_cwnd_cnt -= add; 14513 add = 0; 14514 } 14515 } 14516 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14517 } 14518 14519 /* See if the latest urgent data has been acknowledged */ 14520 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14521 SEQ_GT(seg_ack, tcp->tcp_urg)) 14522 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14523 14524 /* Can we update the RTT estimates? */ 14525 if (tcp->tcp_snd_ts_ok) { 14526 /* Ignore zero timestamp echo-reply. */ 14527 if (tcpopt.tcp_opt_ts_ecr != 0) { 14528 tcp_set_rto(tcp, (int32_t)lbolt - 14529 (int32_t)tcpopt.tcp_opt_ts_ecr); 14530 } 14531 14532 /* If needed, restart the timer. */ 14533 if (tcp->tcp_set_timer == 1) { 14534 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14535 tcp->tcp_set_timer = 0; 14536 } 14537 /* 14538 * Update tcp_csuna in case the other side stops sending 14539 * us timestamps. 14540 */ 14541 tcp->tcp_csuna = tcp->tcp_snxt; 14542 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14543 /* 14544 * An ACK sequence we haven't seen before, so get the RTT 14545 * and update the RTO. But first check if the timestamp is 14546 * valid to use. 14547 */ 14548 if ((mp1->b_next != NULL) && 14549 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14550 tcp_set_rto(tcp, (int32_t)lbolt - 14551 (int32_t)(intptr_t)mp1->b_prev); 14552 else 14553 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14554 14555 /* Remeber the last sequence to be ACKed */ 14556 tcp->tcp_csuna = seg_ack; 14557 if (tcp->tcp_set_timer == 1) { 14558 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14559 tcp->tcp_set_timer = 0; 14560 } 14561 } else { 14562 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14563 } 14564 14565 /* Eat acknowledged bytes off the xmit queue. */ 14566 for (;;) { 14567 mblk_t *mp2; 14568 uchar_t *wptr; 14569 14570 wptr = mp1->b_wptr; 14571 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14572 bytes_acked -= (int)(wptr - mp1->b_rptr); 14573 if (bytes_acked < 0) { 14574 mp1->b_rptr = wptr + bytes_acked; 14575 /* 14576 * Set a new timestamp if all the bytes timed by the 14577 * old timestamp have been ack'ed. 14578 */ 14579 if (SEQ_GT(seg_ack, 14580 (uint32_t)(uintptr_t)(mp1->b_next))) { 14581 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14582 mp1->b_next = NULL; 14583 } 14584 break; 14585 } 14586 mp1->b_next = NULL; 14587 mp1->b_prev = NULL; 14588 mp2 = mp1; 14589 mp1 = mp1->b_cont; 14590 14591 /* 14592 * This notification is required for some zero-copy 14593 * clients to maintain a copy semantic. After the data 14594 * is ack'ed, client is safe to modify or reuse the buffer. 14595 */ 14596 if (tcp->tcp_snd_zcopy_aware && 14597 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14598 tcp_zcopy_notify(tcp); 14599 freeb(mp2); 14600 if (bytes_acked == 0) { 14601 if (mp1 == NULL) { 14602 /* Everything is ack'ed, clear the tail. */ 14603 tcp->tcp_xmit_tail = NULL; 14604 /* 14605 * Cancel the timer unless we are still 14606 * waiting for an ACK for the FIN packet. 14607 */ 14608 if (tcp->tcp_timer_tid != 0 && 14609 tcp->tcp_snxt == tcp->tcp_suna) { 14610 (void) TCP_TIMER_CANCEL(tcp, 14611 tcp->tcp_timer_tid); 14612 tcp->tcp_timer_tid = 0; 14613 } 14614 goto pre_swnd_update; 14615 } 14616 if (mp2 != tcp->tcp_xmit_tail) 14617 break; 14618 tcp->tcp_xmit_tail = mp1; 14619 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14620 (uintptr_t)INT_MAX); 14621 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14622 mp1->b_rptr); 14623 break; 14624 } 14625 if (mp1 == NULL) { 14626 /* 14627 * More was acked but there is nothing more 14628 * outstanding. This means that the FIN was 14629 * just acked or that we're talking to a clown. 14630 */ 14631 fin_acked: 14632 ASSERT(tcp->tcp_fin_sent); 14633 tcp->tcp_xmit_tail = NULL; 14634 if (tcp->tcp_fin_sent) { 14635 /* FIN was acked - making progress */ 14636 if (tcp->tcp_ipversion == IPV6_VERSION && 14637 !tcp->tcp_fin_acked) 14638 tcp->tcp_ip_forward_progress = B_TRUE; 14639 tcp->tcp_fin_acked = B_TRUE; 14640 if (tcp->tcp_linger_tid != 0 && 14641 TCP_TIMER_CANCEL(tcp, 14642 tcp->tcp_linger_tid) >= 0) { 14643 tcp_stop_lingering(tcp); 14644 freemsg(mp); 14645 mp = NULL; 14646 } 14647 } else { 14648 /* 14649 * We should never get here because 14650 * we have already checked that the 14651 * number of bytes ack'ed should be 14652 * smaller than or equal to what we 14653 * have sent so far (it is the 14654 * acceptability check of the ACK). 14655 * We can only get here if the send 14656 * queue is corrupted. 14657 * 14658 * Terminate the connection and 14659 * panic the system. It is better 14660 * for us to panic instead of 14661 * continuing to avoid other disaster. 14662 */ 14663 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14664 tcp->tcp_rnxt, TH_RST|TH_ACK); 14665 panic("Memory corruption " 14666 "detected for connection %s.", 14667 tcp_display(tcp, NULL, 14668 DISP_ADDR_AND_PORT)); 14669 /*NOTREACHED*/ 14670 } 14671 goto pre_swnd_update; 14672 } 14673 ASSERT(mp2 != tcp->tcp_xmit_tail); 14674 } 14675 if (tcp->tcp_unsent) { 14676 flags |= TH_XMIT_NEEDED; 14677 } 14678 pre_swnd_update: 14679 tcp->tcp_xmit_head = mp1; 14680 swnd_update: 14681 /* 14682 * The following check is different from most other implementations. 14683 * For bi-directional transfer, when segments are dropped, the 14684 * "normal" check will not accept a window update in those 14685 * retransmitted segemnts. Failing to do that, TCP may send out 14686 * segments which are outside receiver's window. As TCP accepts 14687 * the ack in those retransmitted segments, if the window update in 14688 * the same segment is not accepted, TCP will incorrectly calculates 14689 * that it can send more segments. This can create a deadlock 14690 * with the receiver if its window becomes zero. 14691 */ 14692 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14693 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14694 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14695 /* 14696 * The criteria for update is: 14697 * 14698 * 1. the segment acknowledges some data. Or 14699 * 2. the segment is new, i.e. it has a higher seq num. Or 14700 * 3. the segment is not old and the advertised window is 14701 * larger than the previous advertised window. 14702 */ 14703 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14704 flags |= TH_XMIT_NEEDED; 14705 tcp->tcp_swnd = new_swnd; 14706 if (new_swnd > tcp->tcp_max_swnd) 14707 tcp->tcp_max_swnd = new_swnd; 14708 tcp->tcp_swl1 = seg_seq; 14709 tcp->tcp_swl2 = seg_ack; 14710 } 14711 est: 14712 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14713 14714 switch (tcp->tcp_state) { 14715 case TCPS_FIN_WAIT_1: 14716 if (tcp->tcp_fin_acked) { 14717 tcp->tcp_state = TCPS_FIN_WAIT_2; 14718 /* 14719 * We implement the non-standard BSD/SunOS 14720 * FIN_WAIT_2 flushing algorithm. 14721 * If there is no user attached to this 14722 * TCP endpoint, then this TCP struct 14723 * could hang around forever in FIN_WAIT_2 14724 * state if the peer forgets to send us 14725 * a FIN. To prevent this, we wait only 14726 * 2*MSL (a convenient time value) for 14727 * the FIN to arrive. If it doesn't show up, 14728 * we flush the TCP endpoint. This algorithm, 14729 * though a violation of RFC-793, has worked 14730 * for over 10 years in BSD systems. 14731 * Note: SunOS 4.x waits 675 seconds before 14732 * flushing the FIN_WAIT_2 connection. 14733 */ 14734 TCP_TIMER_RESTART(tcp, 14735 tcps->tcps_fin_wait_2_flush_interval); 14736 } 14737 break; 14738 case TCPS_FIN_WAIT_2: 14739 break; /* Shutdown hook? */ 14740 case TCPS_LAST_ACK: 14741 freemsg(mp); 14742 if (tcp->tcp_fin_acked) { 14743 (void) tcp_clean_death(tcp, 0, 19); 14744 return; 14745 } 14746 goto xmit_check; 14747 case TCPS_CLOSING: 14748 if (tcp->tcp_fin_acked) { 14749 tcp->tcp_state = TCPS_TIME_WAIT; 14750 /* 14751 * Unconditionally clear the exclusive binding 14752 * bit so this TIME-WAIT connection won't 14753 * interfere with new ones. 14754 */ 14755 tcp->tcp_exclbind = 0; 14756 if (!TCP_IS_DETACHED(tcp)) { 14757 TCP_TIMER_RESTART(tcp, 14758 tcps->tcps_time_wait_interval); 14759 } else { 14760 tcp_time_wait_append(tcp); 14761 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14762 } 14763 } 14764 /*FALLTHRU*/ 14765 case TCPS_CLOSE_WAIT: 14766 freemsg(mp); 14767 goto xmit_check; 14768 default: 14769 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14770 break; 14771 } 14772 } 14773 if (flags & TH_FIN) { 14774 /* Make sure we ack the fin */ 14775 flags |= TH_ACK_NEEDED; 14776 if (!tcp->tcp_fin_rcvd) { 14777 tcp->tcp_fin_rcvd = B_TRUE; 14778 tcp->tcp_rnxt++; 14779 tcph = tcp->tcp_tcph; 14780 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14781 14782 /* 14783 * Generate the ordrel_ind at the end unless we 14784 * are an eager guy. 14785 * In the eager case tcp_rsrv will do this when run 14786 * after tcp_accept is done. 14787 */ 14788 if (tcp->tcp_listener == NULL && 14789 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14790 flags |= TH_ORDREL_NEEDED; 14791 switch (tcp->tcp_state) { 14792 case TCPS_SYN_RCVD: 14793 case TCPS_ESTABLISHED: 14794 tcp->tcp_state = TCPS_CLOSE_WAIT; 14795 /* Keepalive? */ 14796 break; 14797 case TCPS_FIN_WAIT_1: 14798 if (!tcp->tcp_fin_acked) { 14799 tcp->tcp_state = TCPS_CLOSING; 14800 break; 14801 } 14802 /* FALLTHRU */ 14803 case TCPS_FIN_WAIT_2: 14804 tcp->tcp_state = TCPS_TIME_WAIT; 14805 /* 14806 * Unconditionally clear the exclusive binding 14807 * bit so this TIME-WAIT connection won't 14808 * interfere with new ones. 14809 */ 14810 tcp->tcp_exclbind = 0; 14811 if (!TCP_IS_DETACHED(tcp)) { 14812 TCP_TIMER_RESTART(tcp, 14813 tcps->tcps_time_wait_interval); 14814 } else { 14815 tcp_time_wait_append(tcp); 14816 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14817 } 14818 if (seg_len) { 14819 /* 14820 * implies data piggybacked on FIN. 14821 * break to handle data. 14822 */ 14823 break; 14824 } 14825 freemsg(mp); 14826 goto ack_check; 14827 } 14828 } 14829 } 14830 if (mp == NULL) 14831 goto xmit_check; 14832 if (seg_len == 0) { 14833 freemsg(mp); 14834 goto xmit_check; 14835 } 14836 if (mp->b_rptr == mp->b_wptr) { 14837 /* 14838 * The header has been consumed, so we remove the 14839 * zero-length mblk here. 14840 */ 14841 mp1 = mp; 14842 mp = mp->b_cont; 14843 freeb(mp1); 14844 } 14845 tcph = tcp->tcp_tcph; 14846 tcp->tcp_rack_cnt++; 14847 { 14848 uint32_t cur_max; 14849 14850 cur_max = tcp->tcp_rack_cur_max; 14851 if (tcp->tcp_rack_cnt >= cur_max) { 14852 /* 14853 * We have more unacked data than we should - send 14854 * an ACK now. 14855 */ 14856 flags |= TH_ACK_NEEDED; 14857 cur_max++; 14858 if (cur_max > tcp->tcp_rack_abs_max) 14859 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14860 else 14861 tcp->tcp_rack_cur_max = cur_max; 14862 } else if (TCP_IS_DETACHED(tcp)) { 14863 /* We don't have an ACK timer for detached TCP. */ 14864 flags |= TH_ACK_NEEDED; 14865 } else if (seg_len < mss) { 14866 /* 14867 * If we get a segment that is less than an mss, and we 14868 * already have unacknowledged data, and the amount 14869 * unacknowledged is not a multiple of mss, then we 14870 * better generate an ACK now. Otherwise, this may be 14871 * the tail piece of a transaction, and we would rather 14872 * wait for the response. 14873 */ 14874 uint32_t udif; 14875 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14876 (uintptr_t)INT_MAX); 14877 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14878 if (udif && (udif % mss)) 14879 flags |= TH_ACK_NEEDED; 14880 else 14881 flags |= TH_ACK_TIMER_NEEDED; 14882 } else { 14883 /* Start delayed ack timer */ 14884 flags |= TH_ACK_TIMER_NEEDED; 14885 } 14886 } 14887 tcp->tcp_rnxt += seg_len; 14888 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14889 14890 /* Update SACK list */ 14891 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14892 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14893 &(tcp->tcp_num_sack_blk)); 14894 } 14895 14896 if (tcp->tcp_urp_mp) { 14897 tcp->tcp_urp_mp->b_cont = mp; 14898 mp = tcp->tcp_urp_mp; 14899 tcp->tcp_urp_mp = NULL; 14900 /* Ready for a new signal. */ 14901 tcp->tcp_urp_last_valid = B_FALSE; 14902 #ifdef DEBUG 14903 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14904 "tcp_rput: sending exdata_ind %s", 14905 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14906 #endif /* DEBUG */ 14907 } 14908 14909 /* 14910 * Check for ancillary data changes compared to last segment. 14911 */ 14912 if (tcp->tcp_ipv6_recvancillary != 0) { 14913 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14914 if (mp == NULL) 14915 return; 14916 } 14917 14918 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14919 /* 14920 * Side queue inbound data until the accept happens. 14921 * tcp_accept/tcp_rput drains this when the accept happens. 14922 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14923 * T_EXDATA_IND) it is queued on b_next. 14924 * XXX Make urgent data use this. Requires: 14925 * Removing tcp_listener check for TH_URG 14926 * Making M_PCPROTO and MARK messages skip the eager case 14927 */ 14928 14929 if (tcp->tcp_kssl_pending) { 14930 tcp_kssl_input(tcp, mp); 14931 } else { 14932 tcp_rcv_enqueue(tcp, mp, seg_len); 14933 } 14934 } else { 14935 if (mp->b_datap->db_type != M_DATA || 14936 (flags & TH_MARKNEXT_NEEDED)) { 14937 if (tcp->tcp_rcv_list != NULL) { 14938 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14939 } 14940 ASSERT(tcp->tcp_rcv_list == NULL || 14941 tcp->tcp_fused_sigurg); 14942 if (flags & TH_MARKNEXT_NEEDED) { 14943 #ifdef DEBUG 14944 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14945 "tcp_rput: sending MSGMARKNEXT %s", 14946 tcp_display(tcp, NULL, 14947 DISP_PORT_ONLY)); 14948 #endif /* DEBUG */ 14949 mp->b_flag |= MSGMARKNEXT; 14950 flags &= ~TH_MARKNEXT_NEEDED; 14951 } 14952 14953 /* Does this need SSL processing first? */ 14954 if ((tcp->tcp_kssl_ctx != NULL) && 14955 (DB_TYPE(mp) == M_DATA)) { 14956 tcp_kssl_input(tcp, mp); 14957 } else { 14958 putnext(tcp->tcp_rq, mp); 14959 if (!canputnext(tcp->tcp_rq)) 14960 tcp->tcp_rwnd -= seg_len; 14961 } 14962 } else if ((flags & (TH_PUSH|TH_FIN)) || 14963 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 14964 if (tcp->tcp_rcv_list != NULL) { 14965 /* 14966 * Enqueue the new segment first and then 14967 * call tcp_rcv_drain() to send all data 14968 * up. The other way to do this is to 14969 * send all queued data up and then call 14970 * putnext() to send the new segment up. 14971 * This way can remove the else part later 14972 * on. 14973 * 14974 * We don't this to avoid one more call to 14975 * canputnext() as tcp_rcv_drain() needs to 14976 * call canputnext(). 14977 */ 14978 tcp_rcv_enqueue(tcp, mp, seg_len); 14979 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14980 } else { 14981 /* Does this need SSL processing first? */ 14982 if ((tcp->tcp_kssl_ctx != NULL) && 14983 (DB_TYPE(mp) == M_DATA)) { 14984 tcp_kssl_input(tcp, mp); 14985 } else { 14986 putnext(tcp->tcp_rq, mp); 14987 if (!canputnext(tcp->tcp_rq)) 14988 tcp->tcp_rwnd -= seg_len; 14989 } 14990 } 14991 } else { 14992 /* 14993 * Enqueue all packets when processing an mblk 14994 * from the co queue and also enqueue normal packets. 14995 * For packets which belong to SSL stream do SSL 14996 * processing first. 14997 */ 14998 if ((tcp->tcp_kssl_ctx != NULL) && 14999 (DB_TYPE(mp) == M_DATA)) { 15000 tcp_kssl_input(tcp, mp); 15001 } else { 15002 tcp_rcv_enqueue(tcp, mp, seg_len); 15003 } 15004 } 15005 /* 15006 * Make sure the timer is running if we have data waiting 15007 * for a push bit. This provides resiliency against 15008 * implementations that do not correctly generate push bits. 15009 */ 15010 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 15011 /* 15012 * The connection may be closed at this point, so don't 15013 * do anything for a detached tcp. 15014 */ 15015 if (!TCP_IS_DETACHED(tcp)) 15016 tcp->tcp_push_tid = TCP_TIMER(tcp, 15017 tcp_push_timer, 15018 MSEC_TO_TICK( 15019 tcps->tcps_push_timer_interval)); 15020 } 15021 } 15022 xmit_check: 15023 /* Is there anything left to do? */ 15024 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15025 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15026 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15027 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15028 goto done; 15029 15030 /* Any transmit work to do and a non-zero window? */ 15031 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15032 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15033 if (flags & TH_REXMIT_NEEDED) { 15034 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15035 15036 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15037 if (snd_size > mss) 15038 snd_size = mss; 15039 if (snd_size > tcp->tcp_swnd) 15040 snd_size = tcp->tcp_swnd; 15041 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15042 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15043 B_TRUE); 15044 15045 if (mp1 != NULL) { 15046 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15047 tcp->tcp_csuna = tcp->tcp_snxt; 15048 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15049 UPDATE_MIB(&tcps->tcps_mib, 15050 tcpRetransBytes, snd_size); 15051 TCP_RECORD_TRACE(tcp, mp1, 15052 TCP_TRACE_SEND_PKT); 15053 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15054 } 15055 } 15056 if (flags & TH_NEED_SACK_REXMIT) { 15057 tcp_sack_rxmit(tcp, &flags); 15058 } 15059 /* 15060 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15061 * out new segment. Note that tcp_rexmit should not be 15062 * set, otherwise TH_LIMIT_XMIT should not be set. 15063 */ 15064 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15065 if (!tcp->tcp_rexmit) { 15066 tcp_wput_data(tcp, NULL, B_FALSE); 15067 } else { 15068 tcp_ss_rexmit(tcp); 15069 } 15070 } 15071 /* 15072 * Adjust tcp_cwnd back to normal value after sending 15073 * new data segments. 15074 */ 15075 if (flags & TH_LIMIT_XMIT) { 15076 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15077 /* 15078 * This will restart the timer. Restarting the 15079 * timer is used to avoid a timeout before the 15080 * limited transmitted segment's ACK gets back. 15081 */ 15082 if (tcp->tcp_xmit_head != NULL) 15083 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15084 } 15085 15086 /* Anything more to do? */ 15087 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15088 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15089 goto done; 15090 } 15091 ack_check: 15092 if (flags & TH_SEND_URP_MARK) { 15093 ASSERT(tcp->tcp_urp_mark_mp); 15094 /* 15095 * Send up any queued data and then send the mark message 15096 */ 15097 if (tcp->tcp_rcv_list != NULL) { 15098 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15099 } 15100 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15101 15102 mp1 = tcp->tcp_urp_mark_mp; 15103 tcp->tcp_urp_mark_mp = NULL; 15104 #ifdef DEBUG 15105 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15106 "tcp_rput: sending zero-length %s %s", 15107 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15108 "MSGNOTMARKNEXT"), 15109 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15110 #endif /* DEBUG */ 15111 putnext(tcp->tcp_rq, mp1); 15112 flags &= ~TH_SEND_URP_MARK; 15113 } 15114 if (flags & TH_ACK_NEEDED) { 15115 /* 15116 * Time to send an ack for some reason. 15117 */ 15118 mp1 = tcp_ack_mp(tcp); 15119 15120 if (mp1 != NULL) { 15121 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 15122 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15123 BUMP_LOCAL(tcp->tcp_obsegs); 15124 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15125 } 15126 if (tcp->tcp_ack_tid != 0) { 15127 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15128 tcp->tcp_ack_tid = 0; 15129 } 15130 } 15131 if (flags & TH_ACK_TIMER_NEEDED) { 15132 /* 15133 * Arrange for deferred ACK or push wait timeout. 15134 * Start timer if it is not already running. 15135 */ 15136 if (tcp->tcp_ack_tid == 0) { 15137 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15138 MSEC_TO_TICK(tcp->tcp_localnet ? 15139 (clock_t)tcps->tcps_local_dack_interval : 15140 (clock_t)tcps->tcps_deferred_ack_interval)); 15141 } 15142 } 15143 if (flags & TH_ORDREL_NEEDED) { 15144 /* 15145 * Send up the ordrel_ind unless we are an eager guy. 15146 * In the eager case tcp_rsrv will do this when run 15147 * after tcp_accept is done. 15148 */ 15149 ASSERT(tcp->tcp_listener == NULL); 15150 if (tcp->tcp_rcv_list != NULL) { 15151 /* 15152 * Push any mblk(s) enqueued from co processing. 15153 */ 15154 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15155 } 15156 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15157 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 15158 tcp->tcp_ordrel_done = B_TRUE; 15159 putnext(tcp->tcp_rq, mp1); 15160 if (tcp->tcp_deferred_clean_death) { 15161 /* 15162 * tcp_clean_death was deferred 15163 * for T_ORDREL_IND - do it now 15164 */ 15165 (void) tcp_clean_death(tcp, 15166 tcp->tcp_client_errno, 20); 15167 tcp->tcp_deferred_clean_death = B_FALSE; 15168 } 15169 } else { 15170 /* 15171 * Run the orderly release in the 15172 * service routine. 15173 */ 15174 qenable(tcp->tcp_rq); 15175 /* 15176 * Caveat(XXX): The machine may be so 15177 * overloaded that tcp_rsrv() is not scheduled 15178 * until after the endpoint has transitioned 15179 * to TCPS_TIME_WAIT 15180 * and tcp_time_wait_interval expires. Then 15181 * tcp_timer() will blow away state in tcp_t 15182 * and T_ORDREL_IND will never be delivered 15183 * upstream. Unlikely but potentially 15184 * a problem. 15185 */ 15186 } 15187 } 15188 done: 15189 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15190 } 15191 15192 /* 15193 * This function does PAWS protection check. Returns B_TRUE if the 15194 * segment passes the PAWS test, else returns B_FALSE. 15195 */ 15196 boolean_t 15197 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15198 { 15199 uint8_t flags; 15200 int options; 15201 uint8_t *up; 15202 15203 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15204 /* 15205 * If timestamp option is aligned nicely, get values inline, 15206 * otherwise call general routine to parse. Only do that 15207 * if timestamp is the only option. 15208 */ 15209 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15210 TCPOPT_REAL_TS_LEN && 15211 OK_32PTR((up = ((uint8_t *)tcph) + 15212 TCP_MIN_HEADER_LENGTH)) && 15213 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15214 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15215 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15216 15217 options = TCP_OPT_TSTAMP_PRESENT; 15218 } else { 15219 if (tcp->tcp_snd_sack_ok) { 15220 tcpoptp->tcp = tcp; 15221 } else { 15222 tcpoptp->tcp = NULL; 15223 } 15224 options = tcp_parse_options(tcph, tcpoptp); 15225 } 15226 15227 if (options & TCP_OPT_TSTAMP_PRESENT) { 15228 /* 15229 * Do PAWS per RFC 1323 section 4.2. Accept RST 15230 * regardless of the timestamp, page 18 RFC 1323.bis. 15231 */ 15232 if ((flags & TH_RST) == 0 && 15233 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15234 tcp->tcp_ts_recent)) { 15235 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15236 PAWS_TIMEOUT)) { 15237 /* This segment is not acceptable. */ 15238 return (B_FALSE); 15239 } else { 15240 /* 15241 * Connection has been idle for 15242 * too long. Reset the timestamp 15243 * and assume the segment is valid. 15244 */ 15245 tcp->tcp_ts_recent = 15246 tcpoptp->tcp_opt_ts_val; 15247 } 15248 } 15249 } else { 15250 /* 15251 * If we don't get a timestamp on every packet, we 15252 * figure we can't really trust 'em, so we stop sending 15253 * and parsing them. 15254 */ 15255 tcp->tcp_snd_ts_ok = B_FALSE; 15256 15257 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15258 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15259 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15260 /* 15261 * Adjust the tcp_mss accordingly. We also need to 15262 * adjust tcp_cwnd here in accordance with the new mss. 15263 * But we avoid doing a slow start here so as to not 15264 * to lose on the transfer rate built up so far. 15265 */ 15266 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15267 if (tcp->tcp_snd_sack_ok) { 15268 ASSERT(tcp->tcp_sack_info != NULL); 15269 tcp->tcp_max_sack_blk = 4; 15270 } 15271 } 15272 return (B_TRUE); 15273 } 15274 15275 /* 15276 * Attach ancillary data to a received TCP segments for the 15277 * ancillary pieces requested by the application that are 15278 * different than they were in the previous data segment. 15279 * 15280 * Save the "current" values once memory allocation is ok so that 15281 * when memory allocation fails we can just wait for the next data segment. 15282 */ 15283 static mblk_t * 15284 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15285 { 15286 struct T_optdata_ind *todi; 15287 int optlen; 15288 uchar_t *optptr; 15289 struct T_opthdr *toh; 15290 uint_t addflag; /* Which pieces to add */ 15291 mblk_t *mp1; 15292 15293 optlen = 0; 15294 addflag = 0; 15295 /* If app asked for pktinfo and the index has changed ... */ 15296 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15297 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15298 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15299 optlen += sizeof (struct T_opthdr) + 15300 sizeof (struct in6_pktinfo); 15301 addflag |= TCP_IPV6_RECVPKTINFO; 15302 } 15303 /* If app asked for hoplimit and it has changed ... */ 15304 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15305 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15306 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15307 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15308 addflag |= TCP_IPV6_RECVHOPLIMIT; 15309 } 15310 /* If app asked for tclass and it has changed ... */ 15311 if ((ipp->ipp_fields & IPPF_TCLASS) && 15312 ipp->ipp_tclass != tcp->tcp_recvtclass && 15313 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15314 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15315 addflag |= TCP_IPV6_RECVTCLASS; 15316 } 15317 /* 15318 * If app asked for hopbyhop headers and it has changed ... 15319 * For security labels, note that (1) security labels can't change on 15320 * a connected socket at all, (2) we're connected to at most one peer, 15321 * (3) if anything changes, then it must be some other extra option. 15322 */ 15323 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15324 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15325 (ipp->ipp_fields & IPPF_HOPOPTS), 15326 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15327 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15328 tcp->tcp_label_len; 15329 addflag |= TCP_IPV6_RECVHOPOPTS; 15330 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15331 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15332 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15333 return (mp); 15334 } 15335 /* If app asked for dst headers before routing headers ... */ 15336 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15337 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15338 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15339 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15340 optlen += sizeof (struct T_opthdr) + 15341 ipp->ipp_rtdstoptslen; 15342 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15343 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15344 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15345 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15346 return (mp); 15347 } 15348 /* If app asked for routing headers and it has changed ... */ 15349 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15350 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15351 (ipp->ipp_fields & IPPF_RTHDR), 15352 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15353 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15354 addflag |= TCP_IPV6_RECVRTHDR; 15355 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15356 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15357 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15358 return (mp); 15359 } 15360 /* If app asked for dest headers and it has changed ... */ 15361 if ((tcp->tcp_ipv6_recvancillary & 15362 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15363 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15364 (ipp->ipp_fields & IPPF_DSTOPTS), 15365 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15366 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15367 addflag |= TCP_IPV6_RECVDSTOPTS; 15368 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15369 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15370 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15371 return (mp); 15372 } 15373 15374 if (optlen == 0) { 15375 /* Nothing to add */ 15376 return (mp); 15377 } 15378 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15379 if (mp1 == NULL) { 15380 /* 15381 * Defer sending ancillary data until the next TCP segment 15382 * arrives. 15383 */ 15384 return (mp); 15385 } 15386 mp1->b_cont = mp; 15387 mp = mp1; 15388 mp->b_wptr += sizeof (*todi) + optlen; 15389 mp->b_datap->db_type = M_PROTO; 15390 todi = (struct T_optdata_ind *)mp->b_rptr; 15391 todi->PRIM_type = T_OPTDATA_IND; 15392 todi->DATA_flag = 1; /* MORE data */ 15393 todi->OPT_length = optlen; 15394 todi->OPT_offset = sizeof (*todi); 15395 optptr = (uchar_t *)&todi[1]; 15396 /* 15397 * If app asked for pktinfo and the index has changed ... 15398 * Note that the local address never changes for the connection. 15399 */ 15400 if (addflag & TCP_IPV6_RECVPKTINFO) { 15401 struct in6_pktinfo *pkti; 15402 15403 toh = (struct T_opthdr *)optptr; 15404 toh->level = IPPROTO_IPV6; 15405 toh->name = IPV6_PKTINFO; 15406 toh->len = sizeof (*toh) + sizeof (*pkti); 15407 toh->status = 0; 15408 optptr += sizeof (*toh); 15409 pkti = (struct in6_pktinfo *)optptr; 15410 if (tcp->tcp_ipversion == IPV6_VERSION) 15411 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15412 else 15413 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15414 &pkti->ipi6_addr); 15415 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15416 optptr += sizeof (*pkti); 15417 ASSERT(OK_32PTR(optptr)); 15418 /* Save as "last" value */ 15419 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15420 } 15421 /* If app asked for hoplimit and it has changed ... */ 15422 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15423 toh = (struct T_opthdr *)optptr; 15424 toh->level = IPPROTO_IPV6; 15425 toh->name = IPV6_HOPLIMIT; 15426 toh->len = sizeof (*toh) + sizeof (uint_t); 15427 toh->status = 0; 15428 optptr += sizeof (*toh); 15429 *(uint_t *)optptr = ipp->ipp_hoplimit; 15430 optptr += sizeof (uint_t); 15431 ASSERT(OK_32PTR(optptr)); 15432 /* Save as "last" value */ 15433 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15434 } 15435 /* If app asked for tclass and it has changed ... */ 15436 if (addflag & TCP_IPV6_RECVTCLASS) { 15437 toh = (struct T_opthdr *)optptr; 15438 toh->level = IPPROTO_IPV6; 15439 toh->name = IPV6_TCLASS; 15440 toh->len = sizeof (*toh) + sizeof (uint_t); 15441 toh->status = 0; 15442 optptr += sizeof (*toh); 15443 *(uint_t *)optptr = ipp->ipp_tclass; 15444 optptr += sizeof (uint_t); 15445 ASSERT(OK_32PTR(optptr)); 15446 /* Save as "last" value */ 15447 tcp->tcp_recvtclass = ipp->ipp_tclass; 15448 } 15449 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15450 toh = (struct T_opthdr *)optptr; 15451 toh->level = IPPROTO_IPV6; 15452 toh->name = IPV6_HOPOPTS; 15453 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15454 tcp->tcp_label_len; 15455 toh->status = 0; 15456 optptr += sizeof (*toh); 15457 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15458 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15459 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15460 ASSERT(OK_32PTR(optptr)); 15461 /* Save as last value */ 15462 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15463 (ipp->ipp_fields & IPPF_HOPOPTS), 15464 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15465 } 15466 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15467 toh = (struct T_opthdr *)optptr; 15468 toh->level = IPPROTO_IPV6; 15469 toh->name = IPV6_RTHDRDSTOPTS; 15470 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15471 toh->status = 0; 15472 optptr += sizeof (*toh); 15473 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15474 optptr += ipp->ipp_rtdstoptslen; 15475 ASSERT(OK_32PTR(optptr)); 15476 /* Save as last value */ 15477 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15478 &tcp->tcp_rtdstoptslen, 15479 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15480 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15481 } 15482 if (addflag & TCP_IPV6_RECVRTHDR) { 15483 toh = (struct T_opthdr *)optptr; 15484 toh->level = IPPROTO_IPV6; 15485 toh->name = IPV6_RTHDR; 15486 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15487 toh->status = 0; 15488 optptr += sizeof (*toh); 15489 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15490 optptr += ipp->ipp_rthdrlen; 15491 ASSERT(OK_32PTR(optptr)); 15492 /* Save as last value */ 15493 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15494 (ipp->ipp_fields & IPPF_RTHDR), 15495 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15496 } 15497 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15498 toh = (struct T_opthdr *)optptr; 15499 toh->level = IPPROTO_IPV6; 15500 toh->name = IPV6_DSTOPTS; 15501 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15502 toh->status = 0; 15503 optptr += sizeof (*toh); 15504 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15505 optptr += ipp->ipp_dstoptslen; 15506 ASSERT(OK_32PTR(optptr)); 15507 /* Save as last value */ 15508 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15509 (ipp->ipp_fields & IPPF_DSTOPTS), 15510 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15511 } 15512 ASSERT(optptr == mp->b_wptr); 15513 return (mp); 15514 } 15515 15516 15517 /* 15518 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15519 * or a "bad" IRE detected by tcp_adapt_ire. 15520 * We can't tell if the failure was due to the laddr or the faddr 15521 * thus we clear out all addresses and ports. 15522 */ 15523 static void 15524 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15525 { 15526 queue_t *q = tcp->tcp_rq; 15527 tcph_t *tcph; 15528 struct T_error_ack *tea; 15529 conn_t *connp = tcp->tcp_connp; 15530 15531 15532 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15533 15534 if (mp->b_cont) { 15535 freemsg(mp->b_cont); 15536 mp->b_cont = NULL; 15537 } 15538 tea = (struct T_error_ack *)mp->b_rptr; 15539 switch (tea->PRIM_type) { 15540 case T_BIND_ACK: 15541 /* 15542 * Need to unbind with classifier since we were just told that 15543 * our bind succeeded. 15544 */ 15545 tcp->tcp_hard_bound = B_FALSE; 15546 tcp->tcp_hard_binding = B_FALSE; 15547 15548 ipcl_hash_remove(connp); 15549 /* Reuse the mblk if possible */ 15550 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15551 sizeof (*tea)); 15552 mp->b_rptr = mp->b_datap->db_base; 15553 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15554 tea = (struct T_error_ack *)mp->b_rptr; 15555 tea->PRIM_type = T_ERROR_ACK; 15556 tea->TLI_error = TSYSERR; 15557 tea->UNIX_error = error; 15558 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15559 tea->ERROR_prim = T_CONN_REQ; 15560 } else { 15561 tea->ERROR_prim = O_T_BIND_REQ; 15562 } 15563 break; 15564 15565 case T_ERROR_ACK: 15566 if (tcp->tcp_state >= TCPS_SYN_SENT) 15567 tea->ERROR_prim = T_CONN_REQ; 15568 break; 15569 default: 15570 panic("tcp_bind_failed: unexpected TPI type"); 15571 /*NOTREACHED*/ 15572 } 15573 15574 tcp->tcp_state = TCPS_IDLE; 15575 if (tcp->tcp_ipversion == IPV4_VERSION) 15576 tcp->tcp_ipha->ipha_src = 0; 15577 else 15578 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15579 /* 15580 * Copy of the src addr. in tcp_t is needed since 15581 * the lookup funcs. can only look at tcp_t 15582 */ 15583 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15584 15585 tcph = tcp->tcp_tcph; 15586 tcph->th_lport[0] = 0; 15587 tcph->th_lport[1] = 0; 15588 tcp_bind_hash_remove(tcp); 15589 bzero(&connp->u_port, sizeof (connp->u_port)); 15590 /* blow away saved option results if any */ 15591 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15592 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15593 15594 conn_delete_ire(tcp->tcp_connp, NULL); 15595 putnext(q, mp); 15596 } 15597 15598 /* 15599 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15600 * messages. 15601 */ 15602 void 15603 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15604 { 15605 mblk_t *mp1; 15606 uchar_t *rptr = mp->b_rptr; 15607 queue_t *q = tcp->tcp_rq; 15608 struct T_error_ack *tea; 15609 uint32_t mss; 15610 mblk_t *syn_mp; 15611 mblk_t *mdti; 15612 mblk_t *lsoi; 15613 int retval; 15614 mblk_t *ire_mp; 15615 tcp_stack_t *tcps = tcp->tcp_tcps; 15616 15617 switch (mp->b_datap->db_type) { 15618 case M_PROTO: 15619 case M_PCPROTO: 15620 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15621 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15622 break; 15623 tea = (struct T_error_ack *)rptr; 15624 switch (tea->PRIM_type) { 15625 case T_BIND_ACK: 15626 /* 15627 * Adapt Multidata information, if any. The 15628 * following tcp_mdt_update routine will free 15629 * the message. 15630 */ 15631 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15632 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15633 b_rptr)->mdt_capab, B_TRUE); 15634 freemsg(mdti); 15635 } 15636 15637 /* 15638 * Check to update LSO information with tcp, and 15639 * tcp_lso_update routine will free the message. 15640 */ 15641 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 15642 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 15643 b_rptr)->lso_capab); 15644 freemsg(lsoi); 15645 } 15646 15647 /* Get the IRE, if we had requested for it */ 15648 ire_mp = tcp_ire_mp(mp); 15649 15650 if (tcp->tcp_hard_binding) { 15651 tcp->tcp_hard_binding = B_FALSE; 15652 tcp->tcp_hard_bound = B_TRUE; 15653 CL_INET_CONNECT(tcp); 15654 } else { 15655 if (ire_mp != NULL) 15656 freeb(ire_mp); 15657 goto after_syn_sent; 15658 } 15659 15660 retval = tcp_adapt_ire(tcp, ire_mp); 15661 if (ire_mp != NULL) 15662 freeb(ire_mp); 15663 if (retval == 0) { 15664 tcp_bind_failed(tcp, mp, 15665 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15666 ENETUNREACH : EADDRNOTAVAIL)); 15667 return; 15668 } 15669 /* 15670 * Don't let an endpoint connect to itself. 15671 * Also checked in tcp_connect() but that 15672 * check can't handle the case when the 15673 * local IP address is INADDR_ANY. 15674 */ 15675 if (tcp->tcp_ipversion == IPV4_VERSION) { 15676 if ((tcp->tcp_ipha->ipha_dst == 15677 tcp->tcp_ipha->ipha_src) && 15678 (BE16_EQL(tcp->tcp_tcph->th_lport, 15679 tcp->tcp_tcph->th_fport))) { 15680 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15681 return; 15682 } 15683 } else { 15684 if (IN6_ARE_ADDR_EQUAL( 15685 &tcp->tcp_ip6h->ip6_dst, 15686 &tcp->tcp_ip6h->ip6_src) && 15687 (BE16_EQL(tcp->tcp_tcph->th_lport, 15688 tcp->tcp_tcph->th_fport))) { 15689 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15690 return; 15691 } 15692 } 15693 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15694 /* 15695 * This should not be possible! Just for 15696 * defensive coding... 15697 */ 15698 if (tcp->tcp_state != TCPS_SYN_SENT) 15699 goto after_syn_sent; 15700 15701 if (is_system_labeled() && 15702 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 15703 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 15704 return; 15705 } 15706 15707 ASSERT(q == tcp->tcp_rq); 15708 /* 15709 * tcp_adapt_ire() does not adjust 15710 * for TCP/IP header length. 15711 */ 15712 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15713 15714 /* 15715 * Just make sure our rwnd is at 15716 * least tcp_recv_hiwat_mss * MSS 15717 * large, and round up to the nearest 15718 * MSS. 15719 * 15720 * We do the round up here because 15721 * we need to get the interface 15722 * MTU first before we can do the 15723 * round up. 15724 */ 15725 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15726 tcps->tcps_recv_hiwat_minmss * mss); 15727 q->q_hiwat = tcp->tcp_rwnd; 15728 tcp_set_ws_value(tcp); 15729 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15730 tcp->tcp_tcph->th_win); 15731 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 15732 tcp->tcp_snd_ws_ok = B_TRUE; 15733 15734 /* 15735 * Set tcp_snd_ts_ok to true 15736 * so that tcp_xmit_mp will 15737 * include the timestamp 15738 * option in the SYN segment. 15739 */ 15740 if (tcps->tcps_tstamp_always || 15741 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 15742 tcp->tcp_snd_ts_ok = B_TRUE; 15743 } 15744 15745 /* 15746 * tcp_snd_sack_ok can be set in 15747 * tcp_adapt_ire() if the sack metric 15748 * is set. So check it here also. 15749 */ 15750 if (tcps->tcps_sack_permitted == 2 || 15751 tcp->tcp_snd_sack_ok) { 15752 if (tcp->tcp_sack_info == NULL) { 15753 tcp->tcp_sack_info = 15754 kmem_cache_alloc( 15755 tcp_sack_info_cache, 15756 KM_SLEEP); 15757 } 15758 tcp->tcp_snd_sack_ok = B_TRUE; 15759 } 15760 15761 /* 15762 * Should we use ECN? Note that the current 15763 * default value (SunOS 5.9) of tcp_ecn_permitted 15764 * is 1. The reason for doing this is that there 15765 * are equipments out there that will drop ECN 15766 * enabled IP packets. Setting it to 1 avoids 15767 * compatibility problems. 15768 */ 15769 if (tcps->tcps_ecn_permitted == 2) 15770 tcp->tcp_ecn_ok = B_TRUE; 15771 15772 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15773 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15774 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15775 if (syn_mp) { 15776 cred_t *cr; 15777 pid_t pid; 15778 15779 /* 15780 * Obtain the credential from the 15781 * thread calling connect(); the credential 15782 * lives on in the second mblk which 15783 * originated from T_CONN_REQ and is echoed 15784 * with the T_BIND_ACK from ip. If none 15785 * can be found, default to the creator 15786 * of the socket. 15787 */ 15788 if (mp->b_cont == NULL || 15789 (cr = DB_CRED(mp->b_cont)) == NULL) { 15790 cr = tcp->tcp_cred; 15791 pid = tcp->tcp_cpid; 15792 } else { 15793 pid = DB_CPID(mp->b_cont); 15794 } 15795 15796 TCP_RECORD_TRACE(tcp, syn_mp, 15797 TCP_TRACE_SEND_PKT); 15798 mblk_setcred(syn_mp, cr); 15799 DB_CPID(syn_mp) = pid; 15800 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15801 } 15802 after_syn_sent: 15803 /* 15804 * A trailer mblk indicates a waiting client upstream. 15805 * We complete here the processing begun in 15806 * either tcp_bind() or tcp_connect() by passing 15807 * upstream the reply message they supplied. 15808 */ 15809 mp1 = mp; 15810 mp = mp->b_cont; 15811 freeb(mp1); 15812 if (mp) 15813 break; 15814 return; 15815 case T_ERROR_ACK: 15816 if (tcp->tcp_debug) { 15817 (void) strlog(TCP_MOD_ID, 0, 1, 15818 SL_TRACE|SL_ERROR, 15819 "tcp_rput_other: case T_ERROR_ACK, " 15820 "ERROR_prim == %d", 15821 tea->ERROR_prim); 15822 } 15823 switch (tea->ERROR_prim) { 15824 case O_T_BIND_REQ: 15825 case T_BIND_REQ: 15826 tcp_bind_failed(tcp, mp, 15827 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15828 ENETUNREACH : EADDRNOTAVAIL)); 15829 return; 15830 case T_UNBIND_REQ: 15831 tcp->tcp_hard_binding = B_FALSE; 15832 tcp->tcp_hard_bound = B_FALSE; 15833 if (mp->b_cont) { 15834 freemsg(mp->b_cont); 15835 mp->b_cont = NULL; 15836 } 15837 if (tcp->tcp_unbind_pending) 15838 tcp->tcp_unbind_pending = 0; 15839 else { 15840 /* From tcp_ip_unbind() - free */ 15841 freemsg(mp); 15842 return; 15843 } 15844 break; 15845 case T_SVR4_OPTMGMT_REQ: 15846 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15847 /* T_OPTMGMT_REQ generated by TCP */ 15848 printf("T_SVR4_OPTMGMT_REQ failed " 15849 "%d/%d - dropped (cnt %d)\n", 15850 tea->TLI_error, tea->UNIX_error, 15851 tcp->tcp_drop_opt_ack_cnt); 15852 freemsg(mp); 15853 tcp->tcp_drop_opt_ack_cnt--; 15854 return; 15855 } 15856 break; 15857 } 15858 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15859 tcp->tcp_drop_opt_ack_cnt > 0) { 15860 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15861 "- dropped (cnt %d)\n", 15862 tea->TLI_error, tea->UNIX_error, 15863 tcp->tcp_drop_opt_ack_cnt); 15864 freemsg(mp); 15865 tcp->tcp_drop_opt_ack_cnt--; 15866 return; 15867 } 15868 break; 15869 case T_OPTMGMT_ACK: 15870 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15871 /* T_OPTMGMT_REQ generated by TCP */ 15872 freemsg(mp); 15873 tcp->tcp_drop_opt_ack_cnt--; 15874 return; 15875 } 15876 break; 15877 default: 15878 break; 15879 } 15880 break; 15881 case M_FLUSH: 15882 if (*rptr & FLUSHR) 15883 flushq(q, FLUSHDATA); 15884 break; 15885 default: 15886 /* M_CTL will be directly sent to tcp_icmp_error() */ 15887 ASSERT(DB_TYPE(mp) != M_CTL); 15888 break; 15889 } 15890 /* 15891 * Make sure we set this bit before sending the ACK for 15892 * bind. Otherwise accept could possibly run and free 15893 * this tcp struct. 15894 */ 15895 putnext(q, mp); 15896 } 15897 15898 /* 15899 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15900 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15901 * tcp_rsrv() try again. 15902 */ 15903 static void 15904 tcp_ordrel_kick(void *arg) 15905 { 15906 conn_t *connp = (conn_t *)arg; 15907 tcp_t *tcp = connp->conn_tcp; 15908 15909 tcp->tcp_ordrelid = 0; 15910 tcp->tcp_timeout = B_FALSE; 15911 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15912 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15913 qenable(tcp->tcp_rq); 15914 } 15915 } 15916 15917 /* ARGSUSED */ 15918 static void 15919 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15920 { 15921 conn_t *connp = (conn_t *)arg; 15922 tcp_t *tcp = connp->conn_tcp; 15923 queue_t *q = tcp->tcp_rq; 15924 uint_t thwin; 15925 tcp_stack_t *tcps = tcp->tcp_tcps; 15926 15927 freeb(mp); 15928 15929 TCP_STAT(tcps, tcp_rsrv_calls); 15930 15931 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15932 return; 15933 } 15934 15935 if (tcp->tcp_fused) { 15936 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15937 15938 ASSERT(tcp->tcp_fused); 15939 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15940 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15941 ASSERT(!TCP_IS_DETACHED(tcp)); 15942 ASSERT(tcp->tcp_connp->conn_sqp == 15943 peer_tcp->tcp_connp->conn_sqp); 15944 15945 /* 15946 * Normally we would not get backenabled in synchronous 15947 * streams mode, but in case this happens, we need to plug 15948 * synchronous streams during our drain to prevent a race 15949 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15950 */ 15951 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15952 if (tcp->tcp_rcv_list != NULL) 15953 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15954 15955 if (peer_tcp > tcp) { 15956 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15957 mutex_enter(&tcp->tcp_non_sq_lock); 15958 } else { 15959 mutex_enter(&tcp->tcp_non_sq_lock); 15960 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15961 } 15962 15963 if (peer_tcp->tcp_flow_stopped && 15964 (TCP_UNSENT_BYTES(peer_tcp) <= 15965 peer_tcp->tcp_xmit_lowater)) { 15966 tcp_clrqfull(peer_tcp); 15967 } 15968 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15969 mutex_exit(&tcp->tcp_non_sq_lock); 15970 15971 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15972 TCP_STAT(tcps, tcp_fusion_backenabled); 15973 return; 15974 } 15975 15976 if (canputnext(q)) { 15977 tcp->tcp_rwnd = q->q_hiwat; 15978 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15979 << tcp->tcp_rcv_ws; 15980 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15981 /* 15982 * Send back a window update immediately if TCP is above 15983 * ESTABLISHED state and the increase of the rcv window 15984 * that the other side knows is at least 1 MSS after flow 15985 * control is lifted. 15986 */ 15987 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15988 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15989 tcp_xmit_ctl(NULL, tcp, 15990 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15991 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15992 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 15993 } 15994 } 15995 /* Handle a failure to allocate a T_ORDREL_IND here */ 15996 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15997 ASSERT(tcp->tcp_listener == NULL); 15998 if (tcp->tcp_rcv_list != NULL) { 15999 (void) tcp_rcv_drain(q, tcp); 16000 } 16001 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 16002 mp = mi_tpi_ordrel_ind(); 16003 if (mp) { 16004 tcp->tcp_ordrel_done = B_TRUE; 16005 putnext(q, mp); 16006 if (tcp->tcp_deferred_clean_death) { 16007 /* 16008 * tcp_clean_death was deferred for 16009 * T_ORDREL_IND - do it now 16010 */ 16011 tcp->tcp_deferred_clean_death = B_FALSE; 16012 (void) tcp_clean_death(tcp, 16013 tcp->tcp_client_errno, 22); 16014 } 16015 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16016 /* 16017 * If there isn't already a timer running 16018 * start one. Use a 4 second 16019 * timer as a fallback since it can't fail. 16020 */ 16021 tcp->tcp_timeout = B_TRUE; 16022 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16023 MSEC_TO_TICK(4000)); 16024 } 16025 } 16026 } 16027 16028 /* 16029 * The read side service routine is called mostly when we get back-enabled as a 16030 * result of flow control relief. Since we don't actually queue anything in 16031 * TCP, we have no data to send out of here. What we do is clear the receive 16032 * window, and send out a window update. 16033 * This routine is also called to drive an orderly release message upstream 16034 * if the attempt in tcp_rput failed. 16035 */ 16036 static void 16037 tcp_rsrv(queue_t *q) 16038 { 16039 conn_t *connp = Q_TO_CONN(q); 16040 tcp_t *tcp = connp->conn_tcp; 16041 mblk_t *mp; 16042 tcp_stack_t *tcps = tcp->tcp_tcps; 16043 16044 /* No code does a putq on the read side */ 16045 ASSERT(q->q_first == NULL); 16046 16047 /* Nothing to do for the default queue */ 16048 if (q == tcps->tcps_g_q) { 16049 return; 16050 } 16051 16052 mp = allocb(0, BPRI_HI); 16053 if (mp == NULL) { 16054 /* 16055 * We are under memory pressure. Return for now and we 16056 * we will be called again later. 16057 */ 16058 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16059 /* 16060 * If there isn't already a timer running 16061 * start one. Use a 4 second 16062 * timer as a fallback since it can't fail. 16063 */ 16064 tcp->tcp_timeout = B_TRUE; 16065 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16066 MSEC_TO_TICK(4000)); 16067 } 16068 return; 16069 } 16070 CONN_INC_REF(connp); 16071 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16072 SQTAG_TCP_RSRV); 16073 } 16074 16075 /* 16076 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16077 * We do not allow the receive window to shrink. After setting rwnd, 16078 * set the flow control hiwat of the stream. 16079 * 16080 * This function is called in 2 cases: 16081 * 16082 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16083 * connection (passive open) and in tcp_rput_data() for active connect. 16084 * This is called after tcp_mss_set() when the desired MSS value is known. 16085 * This makes sure that our window size is a mutiple of the other side's 16086 * MSS. 16087 * 2) Handling SO_RCVBUF option. 16088 * 16089 * It is ASSUMED that the requested size is a multiple of the current MSS. 16090 * 16091 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16092 * user requests so. 16093 */ 16094 static int 16095 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16096 { 16097 uint32_t mss = tcp->tcp_mss; 16098 uint32_t old_max_rwnd; 16099 uint32_t max_transmittable_rwnd; 16100 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16101 tcp_stack_t *tcps = tcp->tcp_tcps; 16102 16103 if (tcp->tcp_fused) { 16104 size_t sth_hiwat; 16105 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16106 16107 ASSERT(peer_tcp != NULL); 16108 /* 16109 * Record the stream head's high water mark for 16110 * this endpoint; this is used for flow-control 16111 * purposes in tcp_fuse_output(). 16112 */ 16113 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16114 if (!tcp_detached) 16115 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16116 16117 /* 16118 * In the fusion case, the maxpsz stream head value of 16119 * our peer is set according to its send buffer size 16120 * and our receive buffer size; since the latter may 16121 * have changed we need to update the peer's maxpsz. 16122 */ 16123 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16124 return (rwnd); 16125 } 16126 16127 if (tcp_detached) 16128 old_max_rwnd = tcp->tcp_rwnd; 16129 else 16130 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16131 16132 /* 16133 * Insist on a receive window that is at least 16134 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16135 * funny TCP interactions of Nagle algorithm, SWS avoidance 16136 * and delayed acknowledgement. 16137 */ 16138 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16139 16140 /* 16141 * If window size info has already been exchanged, TCP should not 16142 * shrink the window. Shrinking window is doable if done carefully. 16143 * We may add that support later. But so far there is not a real 16144 * need to do that. 16145 */ 16146 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16147 /* MSS may have changed, do a round up again. */ 16148 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16149 } 16150 16151 /* 16152 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16153 * can be applied even before the window scale option is decided. 16154 */ 16155 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16156 if (rwnd > max_transmittable_rwnd) { 16157 rwnd = max_transmittable_rwnd - 16158 (max_transmittable_rwnd % mss); 16159 if (rwnd < mss) 16160 rwnd = max_transmittable_rwnd; 16161 /* 16162 * If we're over the limit we may have to back down tcp_rwnd. 16163 * The increment below won't work for us. So we set all three 16164 * here and the increment below will have no effect. 16165 */ 16166 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16167 } 16168 if (tcp->tcp_localnet) { 16169 tcp->tcp_rack_abs_max = 16170 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16171 } else { 16172 /* 16173 * For a remote host on a different subnet (through a router), 16174 * we ack every other packet to be conforming to RFC1122. 16175 * tcp_deferred_acks_max is default to 2. 16176 */ 16177 tcp->tcp_rack_abs_max = 16178 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16179 } 16180 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16181 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16182 else 16183 tcp->tcp_rack_cur_max = 0; 16184 /* 16185 * Increment the current rwnd by the amount the maximum grew (we 16186 * can not overwrite it since we might be in the middle of a 16187 * connection.) 16188 */ 16189 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16190 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16191 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16192 tcp->tcp_cwnd_max = rwnd; 16193 16194 if (tcp_detached) 16195 return (rwnd); 16196 /* 16197 * We set the maximum receive window into rq->q_hiwat. 16198 * This is not actually used for flow control. 16199 */ 16200 tcp->tcp_rq->q_hiwat = rwnd; 16201 /* 16202 * Set the Stream head high water mark. This doesn't have to be 16203 * here, since we are simply using default values, but we would 16204 * prefer to choose these values algorithmically, with a likely 16205 * relationship to rwnd. 16206 */ 16207 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16208 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16209 return (rwnd); 16210 } 16211 16212 /* 16213 * Return SNMP stuff in buffer in mpdata. 16214 */ 16215 int 16216 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16217 { 16218 mblk_t *mpdata; 16219 mblk_t *mp_conn_ctl = NULL; 16220 mblk_t *mp_conn_tail; 16221 mblk_t *mp_attr_ctl = NULL; 16222 mblk_t *mp_attr_tail; 16223 mblk_t *mp6_conn_ctl = NULL; 16224 mblk_t *mp6_conn_tail; 16225 mblk_t *mp6_attr_ctl = NULL; 16226 mblk_t *mp6_attr_tail; 16227 struct opthdr *optp; 16228 mib2_tcpConnEntry_t tce; 16229 mib2_tcp6ConnEntry_t tce6; 16230 mib2_transportMLPEntry_t mlp; 16231 connf_t *connfp; 16232 conn_t *connp; 16233 int i; 16234 boolean_t ispriv; 16235 zoneid_t zoneid; 16236 int v4_conn_idx; 16237 int v6_conn_idx; 16238 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16239 ip_stack_t *ipst; 16240 16241 if (mpctl == NULL || 16242 (mpdata = mpctl->b_cont) == NULL || 16243 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16244 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16245 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16246 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16247 freemsg(mp_conn_ctl); 16248 freemsg(mp_attr_ctl); 16249 freemsg(mp6_conn_ctl); 16250 freemsg(mp6_attr_ctl); 16251 return (0); 16252 } 16253 16254 /* build table of connections -- need count in fixed part */ 16255 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16256 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16257 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16258 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16259 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16260 16261 ispriv = 16262 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16263 zoneid = Q_TO_CONN(q)->conn_zoneid; 16264 16265 v4_conn_idx = v6_conn_idx = 0; 16266 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16267 16268 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16269 ipst = tcps->tcps_netstack->netstack_ip; 16270 16271 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16272 16273 connp = NULL; 16274 16275 while ((connp = 16276 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16277 tcp_t *tcp; 16278 boolean_t needattr; 16279 16280 if (connp->conn_zoneid != zoneid) 16281 continue; /* not in this zone */ 16282 16283 tcp = connp->conn_tcp; 16284 UPDATE_MIB(&tcps->tcps_mib, 16285 tcpHCInSegs, tcp->tcp_ibsegs); 16286 tcp->tcp_ibsegs = 0; 16287 UPDATE_MIB(&tcps->tcps_mib, 16288 tcpHCOutSegs, tcp->tcp_obsegs); 16289 tcp->tcp_obsegs = 0; 16290 16291 tce6.tcp6ConnState = tce.tcpConnState = 16292 tcp_snmp_state(tcp); 16293 if (tce.tcpConnState == MIB2_TCP_established || 16294 tce.tcpConnState == MIB2_TCP_closeWait) 16295 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16296 16297 needattr = B_FALSE; 16298 bzero(&mlp, sizeof (mlp)); 16299 if (connp->conn_mlp_type != mlptSingle) { 16300 if (connp->conn_mlp_type == mlptShared || 16301 connp->conn_mlp_type == mlptBoth) 16302 mlp.tme_flags |= MIB2_TMEF_SHARED; 16303 if (connp->conn_mlp_type == mlptPrivate || 16304 connp->conn_mlp_type == mlptBoth) 16305 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16306 needattr = B_TRUE; 16307 } 16308 if (connp->conn_peercred != NULL) { 16309 ts_label_t *tsl; 16310 16311 tsl = crgetlabel(connp->conn_peercred); 16312 mlp.tme_doi = label2doi(tsl); 16313 mlp.tme_label = *label2bslabel(tsl); 16314 needattr = B_TRUE; 16315 } 16316 16317 /* Create a message to report on IPv6 entries */ 16318 if (tcp->tcp_ipversion == IPV6_VERSION) { 16319 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16320 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16321 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16322 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16323 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16324 /* Don't want just anybody seeing these... */ 16325 if (ispriv) { 16326 tce6.tcp6ConnEntryInfo.ce_snxt = 16327 tcp->tcp_snxt; 16328 tce6.tcp6ConnEntryInfo.ce_suna = 16329 tcp->tcp_suna; 16330 tce6.tcp6ConnEntryInfo.ce_rnxt = 16331 tcp->tcp_rnxt; 16332 tce6.tcp6ConnEntryInfo.ce_rack = 16333 tcp->tcp_rack; 16334 } else { 16335 /* 16336 * Netstat, unfortunately, uses this to 16337 * get send/receive queue sizes. How to fix? 16338 * Why not compute the difference only? 16339 */ 16340 tce6.tcp6ConnEntryInfo.ce_snxt = 16341 tcp->tcp_snxt - tcp->tcp_suna; 16342 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16343 tce6.tcp6ConnEntryInfo.ce_rnxt = 16344 tcp->tcp_rnxt - tcp->tcp_rack; 16345 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16346 } 16347 16348 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16349 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16350 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16351 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16352 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16353 16354 tce6.tcp6ConnCreationProcess = 16355 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16356 tcp->tcp_cpid; 16357 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16358 16359 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16360 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16361 16362 mlp.tme_connidx = v6_conn_idx++; 16363 if (needattr) 16364 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16365 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16366 } 16367 /* 16368 * Create an IPv4 table entry for IPv4 entries and also 16369 * for IPv6 entries which are bound to in6addr_any 16370 * but don't have IPV6_V6ONLY set. 16371 * (i.e. anything an IPv4 peer could connect to) 16372 */ 16373 if (tcp->tcp_ipversion == IPV4_VERSION || 16374 (tcp->tcp_state <= TCPS_LISTEN && 16375 !tcp->tcp_connp->conn_ipv6_v6only && 16376 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16377 if (tcp->tcp_ipversion == IPV6_VERSION) { 16378 tce.tcpConnRemAddress = INADDR_ANY; 16379 tce.tcpConnLocalAddress = INADDR_ANY; 16380 } else { 16381 tce.tcpConnRemAddress = 16382 tcp->tcp_remote; 16383 tce.tcpConnLocalAddress = 16384 tcp->tcp_ip_src; 16385 } 16386 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16387 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16388 /* Don't want just anybody seeing these... */ 16389 if (ispriv) { 16390 tce.tcpConnEntryInfo.ce_snxt = 16391 tcp->tcp_snxt; 16392 tce.tcpConnEntryInfo.ce_suna = 16393 tcp->tcp_suna; 16394 tce.tcpConnEntryInfo.ce_rnxt = 16395 tcp->tcp_rnxt; 16396 tce.tcpConnEntryInfo.ce_rack = 16397 tcp->tcp_rack; 16398 } else { 16399 /* 16400 * Netstat, unfortunately, uses this to 16401 * get send/receive queue sizes. How 16402 * to fix? 16403 * Why not compute the difference only? 16404 */ 16405 tce.tcpConnEntryInfo.ce_snxt = 16406 tcp->tcp_snxt - tcp->tcp_suna; 16407 tce.tcpConnEntryInfo.ce_suna = 0; 16408 tce.tcpConnEntryInfo.ce_rnxt = 16409 tcp->tcp_rnxt - tcp->tcp_rack; 16410 tce.tcpConnEntryInfo.ce_rack = 0; 16411 } 16412 16413 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16414 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16415 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16416 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16417 tce.tcpConnEntryInfo.ce_state = 16418 tcp->tcp_state; 16419 16420 tce.tcpConnCreationProcess = 16421 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16422 tcp->tcp_cpid; 16423 tce.tcpConnCreationTime = tcp->tcp_open_time; 16424 16425 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16426 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16427 16428 mlp.tme_connidx = v4_conn_idx++; 16429 if (needattr) 16430 (void) snmp_append_data2( 16431 mp_attr_ctl->b_cont, 16432 &mp_attr_tail, (char *)&mlp, 16433 sizeof (mlp)); 16434 } 16435 } 16436 } 16437 16438 /* fixed length structure for IPv4 and IPv6 counters */ 16439 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16440 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16441 sizeof (mib2_tcp6ConnEntry_t)); 16442 /* synchronize 32- and 64-bit counters */ 16443 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16444 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16445 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16446 optp->level = MIB2_TCP; 16447 optp->name = 0; 16448 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16449 sizeof (tcps->tcps_mib)); 16450 optp->len = msgdsize(mpdata); 16451 qreply(q, mpctl); 16452 16453 /* table of connections... */ 16454 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16455 sizeof (struct T_optmgmt_ack)]; 16456 optp->level = MIB2_TCP; 16457 optp->name = MIB2_TCP_CONN; 16458 optp->len = msgdsize(mp_conn_ctl->b_cont); 16459 qreply(q, mp_conn_ctl); 16460 16461 /* table of MLP attributes... */ 16462 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16463 sizeof (struct T_optmgmt_ack)]; 16464 optp->level = MIB2_TCP; 16465 optp->name = EXPER_XPORT_MLP; 16466 optp->len = msgdsize(mp_attr_ctl->b_cont); 16467 if (optp->len == 0) 16468 freemsg(mp_attr_ctl); 16469 else 16470 qreply(q, mp_attr_ctl); 16471 16472 /* table of IPv6 connections... */ 16473 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16474 sizeof (struct T_optmgmt_ack)]; 16475 optp->level = MIB2_TCP6; 16476 optp->name = MIB2_TCP6_CONN; 16477 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16478 qreply(q, mp6_conn_ctl); 16479 16480 /* table of IPv6 MLP attributes... */ 16481 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16482 sizeof (struct T_optmgmt_ack)]; 16483 optp->level = MIB2_TCP6; 16484 optp->name = EXPER_XPORT_MLP; 16485 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16486 if (optp->len == 0) 16487 freemsg(mp6_attr_ctl); 16488 else 16489 qreply(q, mp6_attr_ctl); 16490 return (1); 16491 } 16492 16493 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16494 /* ARGSUSED */ 16495 int 16496 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16497 { 16498 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16499 16500 switch (level) { 16501 case MIB2_TCP: 16502 switch (name) { 16503 case 13: 16504 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16505 return (0); 16506 /* TODO: delete entry defined by tce */ 16507 return (1); 16508 default: 16509 return (0); 16510 } 16511 default: 16512 return (1); 16513 } 16514 } 16515 16516 /* Translate TCP state to MIB2 TCP state. */ 16517 static int 16518 tcp_snmp_state(tcp_t *tcp) 16519 { 16520 if (tcp == NULL) 16521 return (0); 16522 16523 switch (tcp->tcp_state) { 16524 case TCPS_CLOSED: 16525 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16526 case TCPS_BOUND: 16527 return (MIB2_TCP_closed); 16528 case TCPS_LISTEN: 16529 return (MIB2_TCP_listen); 16530 case TCPS_SYN_SENT: 16531 return (MIB2_TCP_synSent); 16532 case TCPS_SYN_RCVD: 16533 return (MIB2_TCP_synReceived); 16534 case TCPS_ESTABLISHED: 16535 return (MIB2_TCP_established); 16536 case TCPS_CLOSE_WAIT: 16537 return (MIB2_TCP_closeWait); 16538 case TCPS_FIN_WAIT_1: 16539 return (MIB2_TCP_finWait1); 16540 case TCPS_CLOSING: 16541 return (MIB2_TCP_closing); 16542 case TCPS_LAST_ACK: 16543 return (MIB2_TCP_lastAck); 16544 case TCPS_FIN_WAIT_2: 16545 return (MIB2_TCP_finWait2); 16546 case TCPS_TIME_WAIT: 16547 return (MIB2_TCP_timeWait); 16548 default: 16549 return (0); 16550 } 16551 } 16552 16553 static char tcp_report_header[] = 16554 "TCP " MI_COL_HDRPAD_STR 16555 "zone dest snxt suna " 16556 "swnd rnxt rack rwnd rto mss w sw rw t " 16557 "recent [lport,fport] state"; 16558 16559 /* 16560 * TCP status report triggered via the Named Dispatch mechanism. 16561 */ 16562 /* ARGSUSED */ 16563 static void 16564 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16565 cred_t *cr) 16566 { 16567 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16568 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16569 char cflag; 16570 in6_addr_t v6dst; 16571 char buf[80]; 16572 uint_t print_len, buf_len; 16573 16574 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16575 if (buf_len <= 0) 16576 return; 16577 16578 if (hashval >= 0) 16579 (void) sprintf(hash, "%03d ", hashval); 16580 else 16581 hash[0] = '\0'; 16582 16583 /* 16584 * Note that we use the remote address in the tcp_b structure. 16585 * This means that it will print out the real destination address, 16586 * not the next hop's address if source routing is used. This 16587 * avoid the confusion on the output because user may not 16588 * know that source routing is used for a connection. 16589 */ 16590 if (tcp->tcp_ipversion == IPV4_VERSION) { 16591 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16592 } else { 16593 v6dst = tcp->tcp_remote_v6; 16594 } 16595 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16596 /* 16597 * the ispriv checks are so that normal users cannot determine 16598 * sequence number information using NDD. 16599 */ 16600 16601 if (TCP_IS_DETACHED(tcp)) 16602 cflag = '*'; 16603 else 16604 cflag = ' '; 16605 print_len = snprintf((char *)mp->b_wptr, buf_len, 16606 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16607 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16608 hash, 16609 (void *)tcp, 16610 tcp->tcp_connp->conn_zoneid, 16611 addrbuf, 16612 (ispriv) ? tcp->tcp_snxt : 0, 16613 (ispriv) ? tcp->tcp_suna : 0, 16614 tcp->tcp_swnd, 16615 (ispriv) ? tcp->tcp_rnxt : 0, 16616 (ispriv) ? tcp->tcp_rack : 0, 16617 tcp->tcp_rwnd, 16618 tcp->tcp_rto, 16619 tcp->tcp_mss, 16620 tcp->tcp_snd_ws_ok, 16621 tcp->tcp_snd_ws, 16622 tcp->tcp_rcv_ws, 16623 tcp->tcp_snd_ts_ok, 16624 tcp->tcp_ts_recent, 16625 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16626 if (print_len < buf_len) { 16627 ((mblk_t *)mp)->b_wptr += print_len; 16628 } else { 16629 ((mblk_t *)mp)->b_wptr += buf_len; 16630 } 16631 } 16632 16633 /* 16634 * TCP status report (for listeners only) triggered via the Named Dispatch 16635 * mechanism. 16636 */ 16637 /* ARGSUSED */ 16638 static void 16639 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16640 { 16641 char addrbuf[INET6_ADDRSTRLEN]; 16642 in6_addr_t v6dst; 16643 uint_t print_len, buf_len; 16644 16645 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16646 if (buf_len <= 0) 16647 return; 16648 16649 if (tcp->tcp_ipversion == IPV4_VERSION) { 16650 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16651 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16652 } else { 16653 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16654 addrbuf, sizeof (addrbuf)); 16655 } 16656 print_len = snprintf((char *)mp->b_wptr, buf_len, 16657 "%03d " 16658 MI_COL_PTRFMT_STR 16659 "%d %s %05u %08u %d/%d/%d%c\n", 16660 hashval, (void *)tcp, 16661 tcp->tcp_connp->conn_zoneid, 16662 addrbuf, 16663 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16664 tcp->tcp_conn_req_seqnum, 16665 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16666 tcp->tcp_conn_req_max, 16667 tcp->tcp_syn_defense ? '*' : ' '); 16668 if (print_len < buf_len) { 16669 ((mblk_t *)mp)->b_wptr += print_len; 16670 } else { 16671 ((mblk_t *)mp)->b_wptr += buf_len; 16672 } 16673 } 16674 16675 /* TCP status report triggered via the Named Dispatch mechanism. */ 16676 /* ARGSUSED */ 16677 static int 16678 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16679 { 16680 tcp_t *tcp; 16681 int i; 16682 conn_t *connp; 16683 connf_t *connfp; 16684 zoneid_t zoneid; 16685 tcp_stack_t *tcps; 16686 ip_stack_t *ipst; 16687 16688 zoneid = Q_TO_CONN(q)->conn_zoneid; 16689 tcps = Q_TO_TCP(q)->tcp_tcps; 16690 16691 /* 16692 * Because of the ndd constraint, at most we can have 64K buffer 16693 * to put in all TCP info. So to be more efficient, just 16694 * allocate a 64K buffer here, assuming we need that large buffer. 16695 * This may be a problem as any user can read tcp_status. Therefore 16696 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16697 * This should be OK as normal users should not do this too often. 16698 */ 16699 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16700 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16701 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16702 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16703 return (0); 16704 } 16705 } 16706 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16707 /* The following may work even if we cannot get a large buf. */ 16708 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16709 return (0); 16710 } 16711 16712 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16713 16714 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16715 16716 ipst = tcps->tcps_netstack->netstack_ip; 16717 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16718 16719 connp = NULL; 16720 16721 while ((connp = 16722 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16723 tcp = connp->conn_tcp; 16724 if (zoneid != GLOBAL_ZONEID && 16725 zoneid != connp->conn_zoneid) 16726 continue; 16727 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16728 cr); 16729 } 16730 16731 } 16732 16733 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16734 return (0); 16735 } 16736 16737 /* TCP status report triggered via the Named Dispatch mechanism. */ 16738 /* ARGSUSED */ 16739 static int 16740 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16741 { 16742 tf_t *tbf; 16743 tcp_t *tcp; 16744 int i; 16745 zoneid_t zoneid; 16746 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16747 16748 zoneid = Q_TO_CONN(q)->conn_zoneid; 16749 16750 /* Refer to comments in tcp_status_report(). */ 16751 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16752 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16753 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16754 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16755 return (0); 16756 } 16757 } 16758 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16759 /* The following may work even if we cannot get a large buf. */ 16760 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16761 return (0); 16762 } 16763 16764 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16765 16766 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16767 tbf = &tcps->tcps_bind_fanout[i]; 16768 mutex_enter(&tbf->tf_lock); 16769 for (tcp = tbf->tf_tcp; tcp != NULL; 16770 tcp = tcp->tcp_bind_hash) { 16771 if (zoneid != GLOBAL_ZONEID && 16772 zoneid != tcp->tcp_connp->conn_zoneid) 16773 continue; 16774 CONN_INC_REF(tcp->tcp_connp); 16775 tcp_report_item(mp->b_cont, tcp, i, 16776 Q_TO_TCP(q), cr); 16777 CONN_DEC_REF(tcp->tcp_connp); 16778 } 16779 mutex_exit(&tbf->tf_lock); 16780 } 16781 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16782 return (0); 16783 } 16784 16785 /* TCP status report triggered via the Named Dispatch mechanism. */ 16786 /* ARGSUSED */ 16787 static int 16788 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16789 { 16790 connf_t *connfp; 16791 conn_t *connp; 16792 tcp_t *tcp; 16793 int i; 16794 zoneid_t zoneid; 16795 tcp_stack_t *tcps; 16796 ip_stack_t *ipst; 16797 16798 zoneid = Q_TO_CONN(q)->conn_zoneid; 16799 tcps = Q_TO_TCP(q)->tcp_tcps; 16800 16801 /* Refer to comments in tcp_status_report(). */ 16802 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16803 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16804 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16805 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16806 return (0); 16807 } 16808 } 16809 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16810 /* The following may work even if we cannot get a large buf. */ 16811 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16812 return (0); 16813 } 16814 16815 (void) mi_mpprintf(mp, 16816 " TCP " MI_COL_HDRPAD_STR 16817 "zone IP addr port seqnum backlog (q0/q/max)"); 16818 16819 ipst = tcps->tcps_netstack->netstack_ip; 16820 16821 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16822 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16823 connp = NULL; 16824 while ((connp = 16825 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16826 tcp = connp->conn_tcp; 16827 if (zoneid != GLOBAL_ZONEID && 16828 zoneid != connp->conn_zoneid) 16829 continue; 16830 tcp_report_listener(mp->b_cont, tcp, i); 16831 } 16832 } 16833 16834 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16835 return (0); 16836 } 16837 16838 /* TCP status report triggered via the Named Dispatch mechanism. */ 16839 /* ARGSUSED */ 16840 static int 16841 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16842 { 16843 connf_t *connfp; 16844 conn_t *connp; 16845 tcp_t *tcp; 16846 int i; 16847 zoneid_t zoneid; 16848 tcp_stack_t *tcps; 16849 ip_stack_t *ipst; 16850 16851 zoneid = Q_TO_CONN(q)->conn_zoneid; 16852 tcps = Q_TO_TCP(q)->tcp_tcps; 16853 ipst = tcps->tcps_netstack->netstack_ip; 16854 16855 /* Refer to comments in tcp_status_report(). */ 16856 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16857 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16858 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16859 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16860 return (0); 16861 } 16862 } 16863 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16864 /* The following may work even if we cannot get a large buf. */ 16865 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16866 return (0); 16867 } 16868 16869 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16870 ipst->ips_ipcl_conn_fanout_size); 16871 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16872 16873 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16874 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16875 connp = NULL; 16876 while ((connp = 16877 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16878 tcp = connp->conn_tcp; 16879 if (zoneid != GLOBAL_ZONEID && 16880 zoneid != connp->conn_zoneid) 16881 continue; 16882 tcp_report_item(mp->b_cont, tcp, i, 16883 Q_TO_TCP(q), cr); 16884 } 16885 } 16886 16887 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16888 return (0); 16889 } 16890 16891 /* TCP status report triggered via the Named Dispatch mechanism. */ 16892 /* ARGSUSED */ 16893 static int 16894 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16895 { 16896 tf_t *tf; 16897 tcp_t *tcp; 16898 int i; 16899 zoneid_t zoneid; 16900 tcp_stack_t *tcps; 16901 16902 zoneid = Q_TO_CONN(q)->conn_zoneid; 16903 tcps = Q_TO_TCP(q)->tcp_tcps; 16904 16905 /* Refer to comments in tcp_status_report(). */ 16906 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16907 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16908 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16909 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16910 return (0); 16911 } 16912 } 16913 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16914 /* The following may work even if we cannot get a large buf. */ 16915 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16916 return (0); 16917 } 16918 16919 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16920 16921 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16922 tf = &tcps->tcps_acceptor_fanout[i]; 16923 mutex_enter(&tf->tf_lock); 16924 for (tcp = tf->tf_tcp; tcp != NULL; 16925 tcp = tcp->tcp_acceptor_hash) { 16926 if (zoneid != GLOBAL_ZONEID && 16927 zoneid != tcp->tcp_connp->conn_zoneid) 16928 continue; 16929 tcp_report_item(mp->b_cont, tcp, i, 16930 Q_TO_TCP(q), cr); 16931 } 16932 mutex_exit(&tf->tf_lock); 16933 } 16934 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16935 return (0); 16936 } 16937 16938 /* 16939 * tcp_timer is the timer service routine. It handles the retransmission, 16940 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16941 * from the state of the tcp instance what kind of action needs to be done 16942 * at the time it is called. 16943 */ 16944 static void 16945 tcp_timer(void *arg) 16946 { 16947 mblk_t *mp; 16948 clock_t first_threshold; 16949 clock_t second_threshold; 16950 clock_t ms; 16951 uint32_t mss; 16952 conn_t *connp = (conn_t *)arg; 16953 tcp_t *tcp = connp->conn_tcp; 16954 tcp_stack_t *tcps = tcp->tcp_tcps; 16955 16956 tcp->tcp_timer_tid = 0; 16957 16958 if (tcp->tcp_fused) 16959 return; 16960 16961 first_threshold = tcp->tcp_first_timer_threshold; 16962 second_threshold = tcp->tcp_second_timer_threshold; 16963 switch (tcp->tcp_state) { 16964 case TCPS_IDLE: 16965 case TCPS_BOUND: 16966 case TCPS_LISTEN: 16967 return; 16968 case TCPS_SYN_RCVD: { 16969 tcp_t *listener = tcp->tcp_listener; 16970 16971 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16972 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16973 /* it's our first timeout */ 16974 tcp->tcp_syn_rcvd_timeout = 1; 16975 mutex_enter(&listener->tcp_eager_lock); 16976 listener->tcp_syn_rcvd_timeout++; 16977 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16978 /* 16979 * Make this eager available for drop if we 16980 * need to drop one to accomodate a new 16981 * incoming SYN request. 16982 */ 16983 MAKE_DROPPABLE(listener, tcp); 16984 } 16985 if (!listener->tcp_syn_defense && 16986 (listener->tcp_syn_rcvd_timeout > 16987 (tcps->tcps_conn_req_max_q0 >> 2)) && 16988 (tcps->tcps_conn_req_max_q0 > 200)) { 16989 /* We may be under attack. Put on a defense. */ 16990 listener->tcp_syn_defense = B_TRUE; 16991 cmn_err(CE_WARN, "High TCP connect timeout " 16992 "rate! System (port %d) may be under a " 16993 "SYN flood attack!", 16994 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16995 16996 listener->tcp_ip_addr_cache = kmem_zalloc( 16997 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16998 KM_NOSLEEP); 16999 } 17000 mutex_exit(&listener->tcp_eager_lock); 17001 } else if (listener != NULL) { 17002 mutex_enter(&listener->tcp_eager_lock); 17003 tcp->tcp_syn_rcvd_timeout++; 17004 if (tcp->tcp_syn_rcvd_timeout > 1 && 17005 !tcp->tcp_closemp_used) { 17006 /* 17007 * This is our second timeout. Put the tcp in 17008 * the list of droppable eagers to allow it to 17009 * be dropped, if needed. We don't check 17010 * whether tcp_dontdrop is set or not to 17011 * protect ourselve from a SYN attack where a 17012 * remote host can spoof itself as one of the 17013 * good IP source and continue to hold 17014 * resources too long. 17015 */ 17016 MAKE_DROPPABLE(listener, tcp); 17017 } 17018 mutex_exit(&listener->tcp_eager_lock); 17019 } 17020 } 17021 /* FALLTHRU */ 17022 case TCPS_SYN_SENT: 17023 first_threshold = tcp->tcp_first_ctimer_threshold; 17024 second_threshold = tcp->tcp_second_ctimer_threshold; 17025 break; 17026 case TCPS_ESTABLISHED: 17027 case TCPS_FIN_WAIT_1: 17028 case TCPS_CLOSING: 17029 case TCPS_CLOSE_WAIT: 17030 case TCPS_LAST_ACK: 17031 /* If we have data to rexmit */ 17032 if (tcp->tcp_suna != tcp->tcp_snxt) { 17033 clock_t time_to_wait; 17034 17035 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17036 if (!tcp->tcp_xmit_head) 17037 break; 17038 time_to_wait = lbolt - 17039 (clock_t)tcp->tcp_xmit_head->b_prev; 17040 time_to_wait = tcp->tcp_rto - 17041 TICK_TO_MSEC(time_to_wait); 17042 /* 17043 * If the timer fires too early, 1 clock tick earlier, 17044 * restart the timer. 17045 */ 17046 if (time_to_wait > msec_per_tick) { 17047 TCP_STAT(tcps, tcp_timer_fire_early); 17048 TCP_TIMER_RESTART(tcp, time_to_wait); 17049 return; 17050 } 17051 /* 17052 * When we probe zero windows, we force the swnd open. 17053 * If our peer acks with a closed window swnd will be 17054 * set to zero by tcp_rput(). As long as we are 17055 * receiving acks tcp_rput will 17056 * reset 'tcp_ms_we_have_waited' so as not to trip the 17057 * first and second interval actions. NOTE: the timer 17058 * interval is allowed to continue its exponential 17059 * backoff. 17060 */ 17061 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17062 if (tcp->tcp_debug) { 17063 (void) strlog(TCP_MOD_ID, 0, 1, 17064 SL_TRACE, "tcp_timer: zero win"); 17065 } 17066 } else { 17067 /* 17068 * After retransmission, we need to do 17069 * slow start. Set the ssthresh to one 17070 * half of current effective window and 17071 * cwnd to one MSS. Also reset 17072 * tcp_cwnd_cnt. 17073 * 17074 * Note that if tcp_ssthresh is reduced because 17075 * of ECN, do not reduce it again unless it is 17076 * already one window of data away (tcp_cwr 17077 * should then be cleared) or this is a 17078 * timeout for a retransmitted segment. 17079 */ 17080 uint32_t npkt; 17081 17082 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17083 npkt = ((tcp->tcp_timer_backoff ? 17084 tcp->tcp_cwnd_ssthresh : 17085 tcp->tcp_snxt - 17086 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17087 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17088 tcp->tcp_mss; 17089 } 17090 tcp->tcp_cwnd = tcp->tcp_mss; 17091 tcp->tcp_cwnd_cnt = 0; 17092 if (tcp->tcp_ecn_ok) { 17093 tcp->tcp_cwr = B_TRUE; 17094 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17095 tcp->tcp_ecn_cwr_sent = B_FALSE; 17096 } 17097 } 17098 break; 17099 } 17100 /* 17101 * We have something to send yet we cannot send. The 17102 * reason can be: 17103 * 17104 * 1. Zero send window: we need to do zero window probe. 17105 * 2. Zero cwnd: because of ECN, we need to "clock out 17106 * segments. 17107 * 3. SWS avoidance: receiver may have shrunk window, 17108 * reset our knowledge. 17109 * 17110 * Note that condition 2 can happen with either 1 or 17111 * 3. But 1 and 3 are exclusive. 17112 */ 17113 if (tcp->tcp_unsent != 0) { 17114 if (tcp->tcp_cwnd == 0) { 17115 /* 17116 * Set tcp_cwnd to 1 MSS so that a 17117 * new segment can be sent out. We 17118 * are "clocking out" new data when 17119 * the network is really congested. 17120 */ 17121 ASSERT(tcp->tcp_ecn_ok); 17122 tcp->tcp_cwnd = tcp->tcp_mss; 17123 } 17124 if (tcp->tcp_swnd == 0) { 17125 /* Extend window for zero window probe */ 17126 tcp->tcp_swnd++; 17127 tcp->tcp_zero_win_probe = B_TRUE; 17128 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17129 } else { 17130 /* 17131 * Handle timeout from sender SWS avoidance. 17132 * Reset our knowledge of the max send window 17133 * since the receiver might have reduced its 17134 * receive buffer. Avoid setting tcp_max_swnd 17135 * to one since that will essentially disable 17136 * the SWS checks. 17137 * 17138 * Note that since we don't have a SWS 17139 * state variable, if the timeout is set 17140 * for ECN but not for SWS, this 17141 * code will also be executed. This is 17142 * fine as tcp_max_swnd is updated 17143 * constantly and it will not affect 17144 * anything. 17145 */ 17146 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17147 } 17148 tcp_wput_data(tcp, NULL, B_FALSE); 17149 return; 17150 } 17151 /* Is there a FIN that needs to be to re retransmitted? */ 17152 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17153 !tcp->tcp_fin_acked) 17154 break; 17155 /* Nothing to do, return without restarting timer. */ 17156 TCP_STAT(tcps, tcp_timer_fire_miss); 17157 return; 17158 case TCPS_FIN_WAIT_2: 17159 /* 17160 * User closed the TCP endpoint and peer ACK'ed our FIN. 17161 * We waited some time for for peer's FIN, but it hasn't 17162 * arrived. We flush the connection now to avoid 17163 * case where the peer has rebooted. 17164 */ 17165 if (TCP_IS_DETACHED(tcp)) { 17166 (void) tcp_clean_death(tcp, 0, 23); 17167 } else { 17168 TCP_TIMER_RESTART(tcp, 17169 tcps->tcps_fin_wait_2_flush_interval); 17170 } 17171 return; 17172 case TCPS_TIME_WAIT: 17173 (void) tcp_clean_death(tcp, 0, 24); 17174 return; 17175 default: 17176 if (tcp->tcp_debug) { 17177 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17178 "tcp_timer: strange state (%d) %s", 17179 tcp->tcp_state, tcp_display(tcp, NULL, 17180 DISP_PORT_ONLY)); 17181 } 17182 return; 17183 } 17184 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17185 /* 17186 * For zero window probe, we need to send indefinitely, 17187 * unless we have not heard from the other side for some 17188 * time... 17189 */ 17190 if ((tcp->tcp_zero_win_probe == 0) || 17191 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17192 second_threshold)) { 17193 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17194 /* 17195 * If TCP is in SYN_RCVD state, send back a 17196 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17197 * should be zero in TCPS_SYN_RCVD state. 17198 */ 17199 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17200 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17201 "in SYN_RCVD", 17202 tcp, tcp->tcp_snxt, 17203 tcp->tcp_rnxt, TH_RST | TH_ACK); 17204 } 17205 (void) tcp_clean_death(tcp, 17206 tcp->tcp_client_errno ? 17207 tcp->tcp_client_errno : ETIMEDOUT, 25); 17208 return; 17209 } else { 17210 /* 17211 * Set tcp_ms_we_have_waited to second_threshold 17212 * so that in next timeout, we will do the above 17213 * check (lbolt - tcp_last_recv_time). This is 17214 * also to avoid overflow. 17215 * 17216 * We don't need to decrement tcp_timer_backoff 17217 * to avoid overflow because it will be decremented 17218 * later if new timeout value is greater than 17219 * tcp_rexmit_interval_max. In the case when 17220 * tcp_rexmit_interval_max is greater than 17221 * second_threshold, it means that we will wait 17222 * longer than second_threshold to send the next 17223 * window probe. 17224 */ 17225 tcp->tcp_ms_we_have_waited = second_threshold; 17226 } 17227 } else if (ms > first_threshold) { 17228 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17229 tcp->tcp_xmit_head != NULL) { 17230 tcp->tcp_xmit_head = 17231 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17232 } 17233 /* 17234 * We have been retransmitting for too long... The RTT 17235 * we calculated is probably incorrect. Reinitialize it. 17236 * Need to compensate for 0 tcp_rtt_sa. Reset 17237 * tcp_rtt_update so that we won't accidentally cache a 17238 * bad value. But only do this if this is not a zero 17239 * window probe. 17240 */ 17241 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17242 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17243 (tcp->tcp_rtt_sa >> 5); 17244 tcp->tcp_rtt_sa = 0; 17245 tcp_ip_notify(tcp); 17246 tcp->tcp_rtt_update = 0; 17247 } 17248 } 17249 tcp->tcp_timer_backoff++; 17250 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17251 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17252 tcps->tcps_rexmit_interval_min) { 17253 /* 17254 * This means the original RTO is tcp_rexmit_interval_min. 17255 * So we will use tcp_rexmit_interval_min as the RTO value 17256 * and do the backoff. 17257 */ 17258 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17259 } else { 17260 ms <<= tcp->tcp_timer_backoff; 17261 } 17262 if (ms > tcps->tcps_rexmit_interval_max) { 17263 ms = tcps->tcps_rexmit_interval_max; 17264 /* 17265 * ms is at max, decrement tcp_timer_backoff to avoid 17266 * overflow. 17267 */ 17268 tcp->tcp_timer_backoff--; 17269 } 17270 tcp->tcp_ms_we_have_waited += ms; 17271 if (tcp->tcp_zero_win_probe == 0) { 17272 tcp->tcp_rto = ms; 17273 } 17274 TCP_TIMER_RESTART(tcp, ms); 17275 /* 17276 * This is after a timeout and tcp_rto is backed off. Set 17277 * tcp_set_timer to 1 so that next time RTO is updated, we will 17278 * restart the timer with a correct value. 17279 */ 17280 tcp->tcp_set_timer = 1; 17281 mss = tcp->tcp_snxt - tcp->tcp_suna; 17282 if (mss > tcp->tcp_mss) 17283 mss = tcp->tcp_mss; 17284 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17285 mss = tcp->tcp_swnd; 17286 17287 if ((mp = tcp->tcp_xmit_head) != NULL) 17288 mp->b_prev = (mblk_t *)lbolt; 17289 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17290 B_TRUE); 17291 17292 /* 17293 * When slow start after retransmission begins, start with 17294 * this seq no. tcp_rexmit_max marks the end of special slow 17295 * start phase. tcp_snd_burst controls how many segments 17296 * can be sent because of an ack. 17297 */ 17298 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17299 tcp->tcp_snd_burst = TCP_CWND_SS; 17300 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17301 (tcp->tcp_unsent == 0)) { 17302 tcp->tcp_rexmit_max = tcp->tcp_fss; 17303 } else { 17304 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17305 } 17306 tcp->tcp_rexmit = B_TRUE; 17307 tcp->tcp_dupack_cnt = 0; 17308 17309 /* 17310 * Remove all rexmit SACK blk to start from fresh. 17311 */ 17312 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17313 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17314 tcp->tcp_num_notsack_blk = 0; 17315 tcp->tcp_cnt_notsack_list = 0; 17316 } 17317 if (mp == NULL) { 17318 return; 17319 } 17320 /* Attach credentials to retransmitted initial SYNs. */ 17321 if (tcp->tcp_state == TCPS_SYN_SENT) { 17322 mblk_setcred(mp, tcp->tcp_cred); 17323 DB_CPID(mp) = tcp->tcp_cpid; 17324 } 17325 17326 tcp->tcp_csuna = tcp->tcp_snxt; 17327 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17328 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17329 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 17330 tcp_send_data(tcp, tcp->tcp_wq, mp); 17331 17332 } 17333 17334 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17335 static void 17336 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17337 { 17338 conn_t *connp; 17339 17340 switch (tcp->tcp_state) { 17341 case TCPS_BOUND: 17342 case TCPS_LISTEN: 17343 break; 17344 default: 17345 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17346 return; 17347 } 17348 17349 /* 17350 * Need to clean up all the eagers since after the unbind, segments 17351 * will no longer be delivered to this listener stream. 17352 */ 17353 mutex_enter(&tcp->tcp_eager_lock); 17354 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17355 tcp_eager_cleanup(tcp, 0); 17356 } 17357 mutex_exit(&tcp->tcp_eager_lock); 17358 17359 if (tcp->tcp_ipversion == IPV4_VERSION) { 17360 tcp->tcp_ipha->ipha_src = 0; 17361 } else { 17362 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17363 } 17364 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17365 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17366 tcp_bind_hash_remove(tcp); 17367 tcp->tcp_state = TCPS_IDLE; 17368 tcp->tcp_mdt = B_FALSE; 17369 /* Send M_FLUSH according to TPI */ 17370 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17371 connp = tcp->tcp_connp; 17372 connp->conn_mdt_ok = B_FALSE; 17373 ipcl_hash_remove(connp); 17374 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17375 mp = mi_tpi_ok_ack_alloc(mp); 17376 putnext(tcp->tcp_rq, mp); 17377 } 17378 17379 /* 17380 * Don't let port fall into the privileged range. 17381 * Since the extra privileged ports can be arbitrary we also 17382 * ensure that we exclude those from consideration. 17383 * tcp_g_epriv_ports is not sorted thus we loop over it until 17384 * there are no changes. 17385 * 17386 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17387 * but instead the code relies on: 17388 * - the fact that the address of the array and its size never changes 17389 * - the atomic assignment of the elements of the array 17390 * 17391 * Returns 0 if there are no more ports available. 17392 * 17393 * TS note: skip multilevel ports. 17394 */ 17395 static in_port_t 17396 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17397 { 17398 int i; 17399 boolean_t restart = B_FALSE; 17400 tcp_stack_t *tcps = tcp->tcp_tcps; 17401 17402 if (random && tcp_random_anon_port != 0) { 17403 (void) random_get_pseudo_bytes((uint8_t *)&port, 17404 sizeof (in_port_t)); 17405 /* 17406 * Unless changed by a sys admin, the smallest anon port 17407 * is 32768 and the largest anon port is 65535. It is 17408 * very likely (50%) for the random port to be smaller 17409 * than the smallest anon port. When that happens, 17410 * add port % (anon port range) to the smallest anon 17411 * port to get the random port. It should fall into the 17412 * valid anon port range. 17413 */ 17414 if (port < tcps->tcps_smallest_anon_port) { 17415 port = tcps->tcps_smallest_anon_port + 17416 port % (tcps->tcps_largest_anon_port - 17417 tcps->tcps_smallest_anon_port); 17418 } 17419 } 17420 17421 retry: 17422 if (port < tcps->tcps_smallest_anon_port) 17423 port = (in_port_t)tcps->tcps_smallest_anon_port; 17424 17425 if (port > tcps->tcps_largest_anon_port) { 17426 if (restart) 17427 return (0); 17428 restart = B_TRUE; 17429 port = (in_port_t)tcps->tcps_smallest_anon_port; 17430 } 17431 17432 if (port < tcps->tcps_smallest_nonpriv_port) 17433 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17434 17435 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17436 if (port == tcps->tcps_g_epriv_ports[i]) { 17437 port++; 17438 /* 17439 * Make sure whether the port is in the 17440 * valid range. 17441 */ 17442 goto retry; 17443 } 17444 } 17445 if (is_system_labeled() && 17446 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17447 IPPROTO_TCP, B_TRUE)) != 0) { 17448 port = i; 17449 goto retry; 17450 } 17451 return (port); 17452 } 17453 17454 /* 17455 * Return the next anonymous port in the privileged port range for 17456 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17457 * downwards. This is the same behavior as documented in the userland 17458 * library call rresvport(3N). 17459 * 17460 * TS note: skip multilevel ports. 17461 */ 17462 static in_port_t 17463 tcp_get_next_priv_port(const tcp_t *tcp) 17464 { 17465 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17466 in_port_t nextport; 17467 boolean_t restart = B_FALSE; 17468 tcp_stack_t *tcps = tcp->tcp_tcps; 17469 retry: 17470 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17471 next_priv_port >= IPPORT_RESERVED) { 17472 next_priv_port = IPPORT_RESERVED - 1; 17473 if (restart) 17474 return (0); 17475 restart = B_TRUE; 17476 } 17477 if (is_system_labeled() && 17478 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17479 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17480 next_priv_port = nextport; 17481 goto retry; 17482 } 17483 return (next_priv_port--); 17484 } 17485 17486 /* The write side r/w procedure. */ 17487 17488 #if CCS_STATS 17489 struct { 17490 struct { 17491 int64_t count, bytes; 17492 } tot, hit; 17493 } wrw_stats; 17494 #endif 17495 17496 /* 17497 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17498 * messages. 17499 */ 17500 /* ARGSUSED */ 17501 static void 17502 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17503 { 17504 conn_t *connp = (conn_t *)arg; 17505 tcp_t *tcp = connp->conn_tcp; 17506 queue_t *q = tcp->tcp_wq; 17507 17508 ASSERT(DB_TYPE(mp) != M_IOCTL); 17509 /* 17510 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17511 * Once the close starts, streamhead and sockfs will not let any data 17512 * packets come down (close ensures that there are no threads using the 17513 * queue and no new threads will come down) but since qprocsoff() 17514 * hasn't happened yet, a M_FLUSH or some non data message might 17515 * get reflected back (in response to our own FLUSHRW) and get 17516 * processed after tcp_close() is done. The conn would still be valid 17517 * because a ref would have added but we need to check the state 17518 * before actually processing the packet. 17519 */ 17520 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17521 freemsg(mp); 17522 return; 17523 } 17524 17525 switch (DB_TYPE(mp)) { 17526 case M_IOCDATA: 17527 tcp_wput_iocdata(tcp, mp); 17528 break; 17529 case M_FLUSH: 17530 tcp_wput_flush(tcp, mp); 17531 break; 17532 default: 17533 CALL_IP_WPUT(connp, q, mp); 17534 break; 17535 } 17536 } 17537 17538 /* 17539 * The TCP fast path write put procedure. 17540 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17541 */ 17542 /* ARGSUSED */ 17543 void 17544 tcp_output(void *arg, mblk_t *mp, void *arg2) 17545 { 17546 int len; 17547 int hdrlen; 17548 int plen; 17549 mblk_t *mp1; 17550 uchar_t *rptr; 17551 uint32_t snxt; 17552 tcph_t *tcph; 17553 struct datab *db; 17554 uint32_t suna; 17555 uint32_t mss; 17556 ipaddr_t *dst; 17557 ipaddr_t *src; 17558 uint32_t sum; 17559 int usable; 17560 conn_t *connp = (conn_t *)arg; 17561 tcp_t *tcp = connp->conn_tcp; 17562 uint32_t msize; 17563 tcp_stack_t *tcps = tcp->tcp_tcps; 17564 17565 /* 17566 * Try and ASSERT the minimum possible references on the 17567 * conn early enough. Since we are executing on write side, 17568 * the connection is obviously not detached and that means 17569 * there is a ref each for TCP and IP. Since we are behind 17570 * the squeue, the minimum references needed are 3. If the 17571 * conn is in classifier hash list, there should be an 17572 * extra ref for that (we check both the possibilities). 17573 */ 17574 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17575 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17576 17577 ASSERT(DB_TYPE(mp) == M_DATA); 17578 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17579 17580 mutex_enter(&tcp->tcp_non_sq_lock); 17581 tcp->tcp_squeue_bytes -= msize; 17582 mutex_exit(&tcp->tcp_non_sq_lock); 17583 17584 /* Bypass tcp protocol for fused tcp loopback */ 17585 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17586 return; 17587 17588 mss = tcp->tcp_mss; 17589 if (tcp->tcp_xmit_zc_clean) 17590 mp = tcp_zcopy_backoff(tcp, mp, 0); 17591 17592 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17593 len = (int)(mp->b_wptr - mp->b_rptr); 17594 17595 /* 17596 * Criteria for fast path: 17597 * 17598 * 1. no unsent data 17599 * 2. single mblk in request 17600 * 3. connection established 17601 * 4. data in mblk 17602 * 5. len <= mss 17603 * 6. no tcp_valid bits 17604 */ 17605 if ((tcp->tcp_unsent != 0) || 17606 (tcp->tcp_cork) || 17607 (mp->b_cont != NULL) || 17608 (tcp->tcp_state != TCPS_ESTABLISHED) || 17609 (len == 0) || 17610 (len > mss) || 17611 (tcp->tcp_valid_bits != 0)) { 17612 tcp_wput_data(tcp, mp, B_FALSE); 17613 return; 17614 } 17615 17616 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17617 ASSERT(tcp->tcp_fin_sent == 0); 17618 17619 /* queue new packet onto retransmission queue */ 17620 if (tcp->tcp_xmit_head == NULL) { 17621 tcp->tcp_xmit_head = mp; 17622 } else { 17623 tcp->tcp_xmit_last->b_cont = mp; 17624 } 17625 tcp->tcp_xmit_last = mp; 17626 tcp->tcp_xmit_tail = mp; 17627 17628 /* find out how much we can send */ 17629 /* BEGIN CSTYLED */ 17630 /* 17631 * un-acked usable 17632 * |--------------|-----------------| 17633 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17634 */ 17635 /* END CSTYLED */ 17636 17637 /* start sending from tcp_snxt */ 17638 snxt = tcp->tcp_snxt; 17639 17640 /* 17641 * Check to see if this connection has been idled for some 17642 * time and no ACK is expected. If it is, we need to slow 17643 * start again to get back the connection's "self-clock" as 17644 * described in VJ's paper. 17645 * 17646 * Refer to the comment in tcp_mss_set() for the calculation 17647 * of tcp_cwnd after idle. 17648 */ 17649 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17650 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17651 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17652 } 17653 17654 usable = tcp->tcp_swnd; /* tcp window size */ 17655 if (usable > tcp->tcp_cwnd) 17656 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17657 usable -= snxt; /* subtract stuff already sent */ 17658 suna = tcp->tcp_suna; 17659 usable += suna; 17660 /* usable can be < 0 if the congestion window is smaller */ 17661 if (len > usable) { 17662 /* Can't send complete M_DATA in one shot */ 17663 goto slow; 17664 } 17665 17666 mutex_enter(&tcp->tcp_non_sq_lock); 17667 if (tcp->tcp_flow_stopped && 17668 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17669 tcp_clrqfull(tcp); 17670 } 17671 mutex_exit(&tcp->tcp_non_sq_lock); 17672 17673 /* 17674 * determine if anything to send (Nagle). 17675 * 17676 * 1. len < tcp_mss (i.e. small) 17677 * 2. unacknowledged data present 17678 * 3. len < nagle limit 17679 * 4. last packet sent < nagle limit (previous packet sent) 17680 */ 17681 if ((len < mss) && (snxt != suna) && 17682 (len < (int)tcp->tcp_naglim) && 17683 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17684 /* 17685 * This was the first unsent packet and normally 17686 * mss < xmit_hiwater so there is no need to worry 17687 * about flow control. The next packet will go 17688 * through the flow control check in tcp_wput_data(). 17689 */ 17690 /* leftover work from above */ 17691 tcp->tcp_unsent = len; 17692 tcp->tcp_xmit_tail_unsent = len; 17693 17694 return; 17695 } 17696 17697 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17698 17699 if (snxt == suna) { 17700 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17701 } 17702 17703 /* we have always sent something */ 17704 tcp->tcp_rack_cnt = 0; 17705 17706 tcp->tcp_snxt = snxt + len; 17707 tcp->tcp_rack = tcp->tcp_rnxt; 17708 17709 if ((mp1 = dupb(mp)) == 0) 17710 goto no_memory; 17711 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17712 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17713 17714 /* adjust tcp header information */ 17715 tcph = tcp->tcp_tcph; 17716 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17717 17718 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17719 sum = (sum >> 16) + (sum & 0xFFFF); 17720 U16_TO_ABE16(sum, tcph->th_sum); 17721 17722 U32_TO_ABE32(snxt, tcph->th_seq); 17723 17724 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17725 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17726 BUMP_LOCAL(tcp->tcp_obsegs); 17727 17728 /* Update the latest receive window size in TCP header. */ 17729 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17730 tcph->th_win); 17731 17732 tcp->tcp_last_sent_len = (ushort_t)len; 17733 17734 plen = len + tcp->tcp_hdr_len; 17735 17736 if (tcp->tcp_ipversion == IPV4_VERSION) { 17737 tcp->tcp_ipha->ipha_length = htons(plen); 17738 } else { 17739 tcp->tcp_ip6h->ip6_plen = htons(plen - 17740 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17741 } 17742 17743 /* see if we need to allocate a mblk for the headers */ 17744 hdrlen = tcp->tcp_hdr_len; 17745 rptr = mp1->b_rptr - hdrlen; 17746 db = mp1->b_datap; 17747 if ((db->db_ref != 2) || rptr < db->db_base || 17748 (!OK_32PTR(rptr))) { 17749 /* NOTE: we assume allocb returns an OK_32PTR */ 17750 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17751 tcps->tcps_wroff_xtra, BPRI_MED); 17752 if (!mp) { 17753 freemsg(mp1); 17754 goto no_memory; 17755 } 17756 mp->b_cont = mp1; 17757 mp1 = mp; 17758 /* Leave room for Link Level header */ 17759 /* hdrlen = tcp->tcp_hdr_len; */ 17760 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17761 mp1->b_wptr = &rptr[hdrlen]; 17762 } 17763 mp1->b_rptr = rptr; 17764 17765 /* Fill in the timestamp option. */ 17766 if (tcp->tcp_snd_ts_ok) { 17767 U32_TO_BE32((uint32_t)lbolt, 17768 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17769 U32_TO_BE32(tcp->tcp_ts_recent, 17770 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17771 } else { 17772 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17773 } 17774 17775 /* copy header into outgoing packet */ 17776 dst = (ipaddr_t *)rptr; 17777 src = (ipaddr_t *)tcp->tcp_iphc; 17778 dst[0] = src[0]; 17779 dst[1] = src[1]; 17780 dst[2] = src[2]; 17781 dst[3] = src[3]; 17782 dst[4] = src[4]; 17783 dst[5] = src[5]; 17784 dst[6] = src[6]; 17785 dst[7] = src[7]; 17786 dst[8] = src[8]; 17787 dst[9] = src[9]; 17788 if (hdrlen -= 40) { 17789 hdrlen >>= 2; 17790 dst += 10; 17791 src += 10; 17792 do { 17793 *dst++ = *src++; 17794 } while (--hdrlen); 17795 } 17796 17797 /* 17798 * Set the ECN info in the TCP header. Note that this 17799 * is not the template header. 17800 */ 17801 if (tcp->tcp_ecn_ok) { 17802 SET_ECT(tcp, rptr); 17803 17804 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17805 if (tcp->tcp_ecn_echo_on) 17806 tcph->th_flags[0] |= TH_ECE; 17807 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17808 tcph->th_flags[0] |= TH_CWR; 17809 tcp->tcp_ecn_cwr_sent = B_TRUE; 17810 } 17811 } 17812 17813 if (tcp->tcp_ip_forward_progress) { 17814 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17815 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17816 tcp->tcp_ip_forward_progress = B_FALSE; 17817 } 17818 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17819 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17820 return; 17821 17822 /* 17823 * If we ran out of memory, we pretend to have sent the packet 17824 * and that it was lost on the wire. 17825 */ 17826 no_memory: 17827 return; 17828 17829 slow: 17830 /* leftover work from above */ 17831 tcp->tcp_unsent = len; 17832 tcp->tcp_xmit_tail_unsent = len; 17833 tcp_wput_data(tcp, NULL, B_FALSE); 17834 } 17835 17836 /* 17837 * The function called through squeue to get behind eager's perimeter to 17838 * finish the accept processing. 17839 */ 17840 /* ARGSUSED */ 17841 void 17842 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17843 { 17844 conn_t *connp = (conn_t *)arg; 17845 tcp_t *tcp = connp->conn_tcp; 17846 queue_t *q = tcp->tcp_rq; 17847 mblk_t *mp1; 17848 mblk_t *stropt_mp = mp; 17849 struct stroptions *stropt; 17850 uint_t thwin; 17851 tcp_stack_t *tcps = tcp->tcp_tcps; 17852 17853 /* 17854 * Drop the eager's ref on the listener, that was placed when 17855 * this eager began life in tcp_conn_request. 17856 */ 17857 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17858 17859 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17860 /* 17861 * Someone blewoff the eager before we could finish 17862 * the accept. 17863 * 17864 * The only reason eager exists it because we put in 17865 * a ref on it when conn ind went up. We need to send 17866 * a disconnect indication up while the last reference 17867 * on the eager will be dropped by the squeue when we 17868 * return. 17869 */ 17870 ASSERT(tcp->tcp_listener == NULL); 17871 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17872 struct T_discon_ind *tdi; 17873 17874 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17875 /* 17876 * Let us reuse the incoming mblk to avoid memory 17877 * allocation failure problems. We know that the 17878 * size of the incoming mblk i.e. stroptions is greater 17879 * than sizeof T_discon_ind. So the reallocb below 17880 * can't fail. 17881 */ 17882 freemsg(mp->b_cont); 17883 mp->b_cont = NULL; 17884 ASSERT(DB_REF(mp) == 1); 17885 mp = reallocb(mp, sizeof (struct T_discon_ind), 17886 B_FALSE); 17887 ASSERT(mp != NULL); 17888 DB_TYPE(mp) = M_PROTO; 17889 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17890 tdi = (struct T_discon_ind *)mp->b_rptr; 17891 if (tcp->tcp_issocket) { 17892 tdi->DISCON_reason = ECONNREFUSED; 17893 tdi->SEQ_number = 0; 17894 } else { 17895 tdi->DISCON_reason = ENOPROTOOPT; 17896 tdi->SEQ_number = 17897 tcp->tcp_conn_req_seqnum; 17898 } 17899 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17900 putnext(q, mp); 17901 } else { 17902 freemsg(mp); 17903 } 17904 if (tcp->tcp_hard_binding) { 17905 tcp->tcp_hard_binding = B_FALSE; 17906 tcp->tcp_hard_bound = B_TRUE; 17907 } 17908 tcp->tcp_detached = B_FALSE; 17909 return; 17910 } 17911 17912 mp1 = stropt_mp->b_cont; 17913 stropt_mp->b_cont = NULL; 17914 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17915 stropt = (struct stroptions *)stropt_mp->b_rptr; 17916 17917 while (mp1 != NULL) { 17918 mp = mp1; 17919 mp1 = mp1->b_cont; 17920 mp->b_cont = NULL; 17921 tcp->tcp_drop_opt_ack_cnt++; 17922 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17923 } 17924 mp = NULL; 17925 17926 /* 17927 * For a loopback connection with tcp_direct_sockfs on, note that 17928 * we don't have to protect tcp_rcv_list yet because synchronous 17929 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17930 * possibly race with us. 17931 */ 17932 17933 /* 17934 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17935 * properly. This is the first time we know of the acceptor' 17936 * queue. So we do it here. 17937 */ 17938 if (tcp->tcp_rcv_list == NULL) { 17939 /* 17940 * Recv queue is empty, tcp_rwnd should not have changed. 17941 * That means it should be equal to the listener's tcp_rwnd. 17942 */ 17943 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17944 } else { 17945 #ifdef DEBUG 17946 uint_t cnt = 0; 17947 17948 mp1 = tcp->tcp_rcv_list; 17949 while ((mp = mp1) != NULL) { 17950 mp1 = mp->b_next; 17951 cnt += msgdsize(mp); 17952 } 17953 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17954 #endif 17955 /* There is some data, add them back to get the max. */ 17956 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17957 } 17958 17959 stropt->so_flags = SO_HIWAT; 17960 stropt->so_hiwat = MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 17961 17962 stropt->so_flags |= SO_MAXBLK; 17963 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17964 17965 /* 17966 * This is the first time we run on the correct 17967 * queue after tcp_accept. So fix all the q parameters 17968 * here. 17969 */ 17970 /* Allocate room for SACK options if needed. */ 17971 stropt->so_flags |= SO_WROFF; 17972 if (tcp->tcp_fused) { 17973 ASSERT(tcp->tcp_loopback); 17974 ASSERT(tcp->tcp_loopback_peer != NULL); 17975 /* 17976 * For fused tcp loopback, set the stream head's write 17977 * offset value to zero since we won't be needing any room 17978 * for TCP/IP headers. This would also improve performance 17979 * since it would reduce the amount of work done by kmem. 17980 * Non-fused tcp loopback case is handled separately below. 17981 */ 17982 stropt->so_wroff = 0; 17983 /* 17984 * Record the stream head's high water mark for this endpoint; 17985 * this is used for flow-control purposes in tcp_fuse_output(). 17986 */ 17987 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 17988 /* 17989 * Update the peer's transmit parameters according to 17990 * our recently calculated high water mark value. 17991 */ 17992 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17993 } else if (tcp->tcp_snd_sack_ok) { 17994 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17995 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17996 } else { 17997 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17998 tcps->tcps_wroff_xtra); 17999 } 18000 18001 /* 18002 * If this is endpoint is handling SSL, then reserve extra 18003 * offset and space at the end. 18004 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18005 * overriding the previous setting. The extra cost of signing and 18006 * encrypting multiple MSS-size records (12 of them with Ethernet), 18007 * instead of a single contiguous one by the stream head 18008 * largely outweighs the statistical reduction of ACKs, when 18009 * applicable. The peer will also save on decyption and verification 18010 * costs. 18011 */ 18012 if (tcp->tcp_kssl_ctx != NULL) { 18013 stropt->so_wroff += SSL3_WROFFSET; 18014 18015 stropt->so_flags |= SO_TAIL; 18016 stropt->so_tail = SSL3_MAX_TAIL_LEN; 18017 18018 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 18019 } 18020 18021 /* Send the options up */ 18022 putnext(q, stropt_mp); 18023 18024 /* 18025 * Pass up any data and/or a fin that has been received. 18026 * 18027 * Adjust receive window in case it had decreased 18028 * (because there is data <=> tcp_rcv_list != NULL) 18029 * while the connection was detached. Note that 18030 * in case the eager was flow-controlled, w/o this 18031 * code, the rwnd may never open up again! 18032 */ 18033 if (tcp->tcp_rcv_list != NULL) { 18034 /* We drain directly in case of fused tcp loopback */ 18035 if (!tcp->tcp_fused && canputnext(q)) { 18036 tcp->tcp_rwnd = q->q_hiwat; 18037 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 18038 << tcp->tcp_rcv_ws; 18039 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 18040 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18041 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 18042 tcp_xmit_ctl(NULL, 18043 tcp, (tcp->tcp_swnd == 0) ? 18044 tcp->tcp_suna : tcp->tcp_snxt, 18045 tcp->tcp_rnxt, TH_ACK); 18046 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 18047 } 18048 18049 } 18050 (void) tcp_rcv_drain(q, tcp); 18051 18052 /* 18053 * For fused tcp loopback, back-enable peer endpoint 18054 * if it's currently flow-controlled. 18055 */ 18056 if (tcp->tcp_fused) { 18057 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18058 18059 ASSERT(peer_tcp != NULL); 18060 ASSERT(peer_tcp->tcp_fused); 18061 /* 18062 * In order to change the peer's tcp_flow_stopped, 18063 * we need to take locks for both end points. The 18064 * highest address is taken first. 18065 */ 18066 if (peer_tcp > tcp) { 18067 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18068 mutex_enter(&tcp->tcp_non_sq_lock); 18069 } else { 18070 mutex_enter(&tcp->tcp_non_sq_lock); 18071 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18072 } 18073 if (peer_tcp->tcp_flow_stopped) { 18074 tcp_clrqfull(peer_tcp); 18075 TCP_STAT(tcps, tcp_fusion_backenabled); 18076 } 18077 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18078 mutex_exit(&tcp->tcp_non_sq_lock); 18079 } 18080 } 18081 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18082 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18083 mp = mi_tpi_ordrel_ind(); 18084 if (mp) { 18085 tcp->tcp_ordrel_done = B_TRUE; 18086 putnext(q, mp); 18087 if (tcp->tcp_deferred_clean_death) { 18088 /* 18089 * tcp_clean_death was deferred 18090 * for T_ORDREL_IND - do it now 18091 */ 18092 (void) tcp_clean_death(tcp, 18093 tcp->tcp_client_errno, 21); 18094 tcp->tcp_deferred_clean_death = B_FALSE; 18095 } 18096 } else { 18097 /* 18098 * Run the orderly release in the 18099 * service routine. 18100 */ 18101 qenable(q); 18102 } 18103 } 18104 if (tcp->tcp_hard_binding) { 18105 tcp->tcp_hard_binding = B_FALSE; 18106 tcp->tcp_hard_bound = B_TRUE; 18107 } 18108 18109 tcp->tcp_detached = B_FALSE; 18110 18111 /* We can enable synchronous streams now */ 18112 if (tcp->tcp_fused) { 18113 tcp_fuse_syncstr_enable_pair(tcp); 18114 } 18115 18116 if (tcp->tcp_ka_enabled) { 18117 tcp->tcp_ka_last_intrvl = 0; 18118 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18119 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18120 } 18121 18122 /* 18123 * At this point, eager is fully established and will 18124 * have the following references - 18125 * 18126 * 2 references for connection to exist (1 for TCP and 1 for IP). 18127 * 1 reference for the squeue which will be dropped by the squeue as 18128 * soon as this function returns. 18129 * There will be 1 additonal reference for being in classifier 18130 * hash list provided something bad hasn't happened. 18131 */ 18132 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18133 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18134 } 18135 18136 /* 18137 * The function called through squeue to get behind listener's perimeter to 18138 * send a deffered conn_ind. 18139 */ 18140 /* ARGSUSED */ 18141 void 18142 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18143 { 18144 conn_t *connp = (conn_t *)arg; 18145 tcp_t *listener = connp->conn_tcp; 18146 18147 if (listener->tcp_state == TCPS_CLOSED || 18148 TCP_IS_DETACHED(listener)) { 18149 /* 18150 * If listener has closed, it would have caused a 18151 * a cleanup/blowoff to happen for the eager. 18152 */ 18153 tcp_t *tcp; 18154 struct T_conn_ind *conn_ind; 18155 18156 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18157 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18158 conn_ind->OPT_length); 18159 /* 18160 * We need to drop the ref on eager that was put 18161 * tcp_rput_data() before trying to send the conn_ind 18162 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18163 * and tcp_wput_accept() is sending this deferred conn_ind but 18164 * listener is closed so we drop the ref. 18165 */ 18166 CONN_DEC_REF(tcp->tcp_connp); 18167 freemsg(mp); 18168 return; 18169 } 18170 putnext(listener->tcp_rq, mp); 18171 } 18172 18173 18174 /* 18175 * This is the STREAMS entry point for T_CONN_RES coming down on 18176 * Acceptor STREAM when sockfs listener does accept processing. 18177 * Read the block comment on top of tcp_conn_request(). 18178 */ 18179 void 18180 tcp_wput_accept(queue_t *q, mblk_t *mp) 18181 { 18182 queue_t *rq = RD(q); 18183 struct T_conn_res *conn_res; 18184 tcp_t *eager; 18185 tcp_t *listener; 18186 struct T_ok_ack *ok; 18187 t_scalar_t PRIM_type; 18188 mblk_t *opt_mp; 18189 conn_t *econnp; 18190 18191 ASSERT(DB_TYPE(mp) == M_PROTO); 18192 18193 conn_res = (struct T_conn_res *)mp->b_rptr; 18194 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18195 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18196 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18197 if (mp != NULL) 18198 putnext(rq, mp); 18199 return; 18200 } 18201 switch (conn_res->PRIM_type) { 18202 case O_T_CONN_RES: 18203 case T_CONN_RES: 18204 /* 18205 * We pass up an err ack if allocb fails. This will 18206 * cause sockfs to issue a T_DISCON_REQ which will cause 18207 * tcp_eager_blowoff to be called. sockfs will then call 18208 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18209 * we need to do the allocb up here because we have to 18210 * make sure rq->q_qinfo->qi_qclose still points to the 18211 * correct function (tcpclose_accept) in case allocb 18212 * fails. 18213 */ 18214 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18215 if (opt_mp == NULL) { 18216 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18217 if (mp != NULL) 18218 putnext(rq, mp); 18219 return; 18220 } 18221 18222 bcopy(mp->b_rptr + conn_res->OPT_offset, 18223 &eager, conn_res->OPT_length); 18224 PRIM_type = conn_res->PRIM_type; 18225 mp->b_datap->db_type = M_PCPROTO; 18226 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18227 ok = (struct T_ok_ack *)mp->b_rptr; 18228 ok->PRIM_type = T_OK_ACK; 18229 ok->CORRECT_prim = PRIM_type; 18230 econnp = eager->tcp_connp; 18231 econnp->conn_dev = (dev_t)q->q_ptr; 18232 eager->tcp_rq = rq; 18233 eager->tcp_wq = q; 18234 rq->q_ptr = econnp; 18235 rq->q_qinfo = &tcp_rinit; 18236 q->q_ptr = econnp; 18237 q->q_qinfo = &tcp_winit; 18238 listener = eager->tcp_listener; 18239 eager->tcp_issocket = B_TRUE; 18240 18241 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18242 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18243 ASSERT(econnp->conn_netstack == 18244 listener->tcp_connp->conn_netstack); 18245 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18246 18247 /* Put the ref for IP */ 18248 CONN_INC_REF(econnp); 18249 18250 /* 18251 * We should have minimum of 3 references on the conn 18252 * at this point. One each for TCP and IP and one for 18253 * the T_conn_ind that was sent up when the 3-way handshake 18254 * completed. In the normal case we would also have another 18255 * reference (making a total of 4) for the conn being in the 18256 * classifier hash list. However the eager could have received 18257 * an RST subsequently and tcp_closei_local could have removed 18258 * the eager from the classifier hash list, hence we can't 18259 * assert that reference. 18260 */ 18261 ASSERT(econnp->conn_ref >= 3); 18262 18263 /* 18264 * Send the new local address also up to sockfs. There 18265 * should already be enough space in the mp that came 18266 * down from soaccept(). 18267 */ 18268 if (eager->tcp_family == AF_INET) { 18269 sin_t *sin; 18270 18271 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18272 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18273 sin = (sin_t *)mp->b_wptr; 18274 mp->b_wptr += sizeof (sin_t); 18275 sin->sin_family = AF_INET; 18276 sin->sin_port = eager->tcp_lport; 18277 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18278 } else { 18279 sin6_t *sin6; 18280 18281 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18282 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18283 sin6 = (sin6_t *)mp->b_wptr; 18284 mp->b_wptr += sizeof (sin6_t); 18285 sin6->sin6_family = AF_INET6; 18286 sin6->sin6_port = eager->tcp_lport; 18287 if (eager->tcp_ipversion == IPV4_VERSION) { 18288 sin6->sin6_flowinfo = 0; 18289 IN6_IPADDR_TO_V4MAPPED( 18290 eager->tcp_ipha->ipha_src, 18291 &sin6->sin6_addr); 18292 } else { 18293 ASSERT(eager->tcp_ip6h != NULL); 18294 sin6->sin6_flowinfo = 18295 eager->tcp_ip6h->ip6_vcf & 18296 ~IPV6_VERS_AND_FLOW_MASK; 18297 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18298 } 18299 sin6->sin6_scope_id = 0; 18300 sin6->__sin6_src_id = 0; 18301 } 18302 18303 putnext(rq, mp); 18304 18305 opt_mp->b_datap->db_type = M_SETOPTS; 18306 opt_mp->b_wptr += sizeof (struct stroptions); 18307 18308 /* 18309 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18310 * from listener to acceptor. The message is chained on the 18311 * bind_mp which tcp_rput_other will send down to IP. 18312 */ 18313 if (listener->tcp_bound_if != 0) { 18314 /* allocate optmgmt req */ 18315 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18316 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18317 sizeof (int)); 18318 if (mp != NULL) 18319 linkb(opt_mp, mp); 18320 } 18321 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18322 uint_t on = 1; 18323 18324 /* allocate optmgmt req */ 18325 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18326 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18327 if (mp != NULL) 18328 linkb(opt_mp, mp); 18329 } 18330 18331 18332 mutex_enter(&listener->tcp_eager_lock); 18333 18334 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18335 18336 tcp_t *tail; 18337 tcp_t *tcp; 18338 mblk_t *mp1; 18339 18340 tcp = listener->tcp_eager_prev_q0; 18341 /* 18342 * listener->tcp_eager_prev_q0 points to the TAIL of the 18343 * deferred T_conn_ind queue. We need to get to the head 18344 * of the queue in order to send up T_conn_ind the same 18345 * order as how the 3WHS is completed. 18346 */ 18347 while (tcp != listener) { 18348 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18349 !tcp->tcp_kssl_pending) 18350 break; 18351 else 18352 tcp = tcp->tcp_eager_prev_q0; 18353 } 18354 /* None of the pending eagers can be sent up now */ 18355 if (tcp == listener) 18356 goto no_more_eagers; 18357 18358 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18359 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18360 /* Move from q0 to q */ 18361 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18362 listener->tcp_conn_req_cnt_q0--; 18363 listener->tcp_conn_req_cnt_q++; 18364 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18365 tcp->tcp_eager_prev_q0; 18366 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18367 tcp->tcp_eager_next_q0; 18368 tcp->tcp_eager_prev_q0 = NULL; 18369 tcp->tcp_eager_next_q0 = NULL; 18370 tcp->tcp_conn_def_q0 = B_FALSE; 18371 18372 /* Make sure the tcp isn't in the list of droppables */ 18373 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18374 tcp->tcp_eager_prev_drop_q0 == NULL); 18375 18376 /* 18377 * Insert at end of the queue because sockfs sends 18378 * down T_CONN_RES in chronological order. Leaving 18379 * the older conn indications at front of the queue 18380 * helps reducing search time. 18381 */ 18382 tail = listener->tcp_eager_last_q; 18383 if (tail != NULL) { 18384 tail->tcp_eager_next_q = tcp; 18385 } else { 18386 listener->tcp_eager_next_q = tcp; 18387 } 18388 listener->tcp_eager_last_q = tcp; 18389 tcp->tcp_eager_next_q = NULL; 18390 18391 /* Need to get inside the listener perimeter */ 18392 CONN_INC_REF(listener->tcp_connp); 18393 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18394 tcp_send_pending, listener->tcp_connp, 18395 SQTAG_TCP_SEND_PENDING); 18396 } 18397 no_more_eagers: 18398 tcp_eager_unlink(eager); 18399 mutex_exit(&listener->tcp_eager_lock); 18400 18401 /* 18402 * At this point, the eager is detached from the listener 18403 * but we still have an extra refs on eager (apart from the 18404 * usual tcp references). The ref was placed in tcp_rput_data 18405 * before sending the conn_ind in tcp_send_conn_ind. 18406 * The ref will be dropped in tcp_accept_finish(). 18407 */ 18408 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18409 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18410 return; 18411 default: 18412 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18413 if (mp != NULL) 18414 putnext(rq, mp); 18415 return; 18416 } 18417 } 18418 18419 void 18420 tcp_wput(queue_t *q, mblk_t *mp) 18421 { 18422 conn_t *connp = Q_TO_CONN(q); 18423 tcp_t *tcp; 18424 void (*output_proc)(); 18425 t_scalar_t type; 18426 uchar_t *rptr; 18427 struct iocblk *iocp; 18428 uint32_t msize; 18429 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18430 18431 ASSERT(connp->conn_ref >= 2); 18432 18433 switch (DB_TYPE(mp)) { 18434 case M_DATA: 18435 tcp = connp->conn_tcp; 18436 ASSERT(tcp != NULL); 18437 18438 msize = msgdsize(mp); 18439 18440 mutex_enter(&tcp->tcp_non_sq_lock); 18441 tcp->tcp_squeue_bytes += msize; 18442 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18443 tcp_setqfull(tcp); 18444 } 18445 mutex_exit(&tcp->tcp_non_sq_lock); 18446 18447 CONN_INC_REF(connp); 18448 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18449 tcp_output, connp, SQTAG_TCP_OUTPUT); 18450 return; 18451 case M_PROTO: 18452 case M_PCPROTO: 18453 /* 18454 * if it is a snmp message, don't get behind the squeue 18455 */ 18456 tcp = connp->conn_tcp; 18457 rptr = mp->b_rptr; 18458 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18459 type = ((union T_primitives *)rptr)->type; 18460 } else { 18461 if (tcp->tcp_debug) { 18462 (void) strlog(TCP_MOD_ID, 0, 1, 18463 SL_ERROR|SL_TRACE, 18464 "tcp_wput_proto, dropping one..."); 18465 } 18466 freemsg(mp); 18467 return; 18468 } 18469 if (type == T_SVR4_OPTMGMT_REQ) { 18470 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18471 if (snmpcom_req(q, mp, tcp_snmp_set, tcp_snmp_get, 18472 cr)) { 18473 /* 18474 * This was a SNMP request 18475 */ 18476 return; 18477 } else { 18478 output_proc = tcp_wput_proto; 18479 } 18480 } else { 18481 output_proc = tcp_wput_proto; 18482 } 18483 break; 18484 case M_IOCTL: 18485 /* 18486 * Most ioctls can be processed right away without going via 18487 * squeues - process them right here. Those that do require 18488 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18489 * are processed by tcp_wput_ioctl(). 18490 */ 18491 iocp = (struct iocblk *)mp->b_rptr; 18492 tcp = connp->conn_tcp; 18493 18494 switch (iocp->ioc_cmd) { 18495 case TCP_IOC_ABORT_CONN: 18496 tcp_ioctl_abort_conn(q, mp); 18497 return; 18498 case TI_GETPEERNAME: 18499 if (tcp->tcp_state < TCPS_SYN_RCVD) { 18500 iocp->ioc_error = ENOTCONN; 18501 iocp->ioc_count = 0; 18502 mp->b_datap->db_type = M_IOCACK; 18503 qreply(q, mp); 18504 return; 18505 } 18506 /* FALLTHRU */ 18507 case TI_GETMYNAME: 18508 mi_copyin(q, mp, NULL, 18509 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18510 return; 18511 case ND_SET: 18512 /* nd_getset does the necessary checks */ 18513 case ND_GET: 18514 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18515 CALL_IP_WPUT(connp, q, mp); 18516 return; 18517 } 18518 qreply(q, mp); 18519 return; 18520 case TCP_IOC_DEFAULT_Q: 18521 /* 18522 * Wants to be the default wq. Check the credentials 18523 * first, the rest is executed via squeue. 18524 */ 18525 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18526 iocp->ioc_error = EPERM; 18527 iocp->ioc_count = 0; 18528 mp->b_datap->db_type = M_IOCACK; 18529 qreply(q, mp); 18530 return; 18531 } 18532 output_proc = tcp_wput_ioctl; 18533 break; 18534 default: 18535 output_proc = tcp_wput_ioctl; 18536 break; 18537 } 18538 break; 18539 default: 18540 output_proc = tcp_wput_nondata; 18541 break; 18542 } 18543 18544 CONN_INC_REF(connp); 18545 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18546 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18547 } 18548 18549 /* 18550 * Initial STREAMS write side put() procedure for sockets. It tries to 18551 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18552 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18553 * are handled by tcp_wput() as usual. 18554 * 18555 * All further messages will also be handled by tcp_wput() because we cannot 18556 * be sure that the above short cut is safe later. 18557 */ 18558 static void 18559 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18560 { 18561 conn_t *connp = Q_TO_CONN(wq); 18562 tcp_t *tcp = connp->conn_tcp; 18563 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18564 18565 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18566 wq->q_qinfo = &tcp_winit; 18567 18568 ASSERT(IPCL_IS_TCP(connp)); 18569 ASSERT(TCP_IS_SOCKET(tcp)); 18570 18571 if (DB_TYPE(mp) == M_PCPROTO && 18572 MBLKL(mp) == sizeof (struct T_capability_req) && 18573 car->PRIM_type == T_CAPABILITY_REQ) { 18574 tcp_capability_req(tcp, mp); 18575 return; 18576 } 18577 18578 tcp_wput(wq, mp); 18579 } 18580 18581 static boolean_t 18582 tcp_zcopy_check(tcp_t *tcp) 18583 { 18584 conn_t *connp = tcp->tcp_connp; 18585 ire_t *ire; 18586 boolean_t zc_enabled = B_FALSE; 18587 tcp_stack_t *tcps = tcp->tcp_tcps; 18588 18589 if (do_tcpzcopy == 2) 18590 zc_enabled = B_TRUE; 18591 else if (tcp->tcp_ipversion == IPV4_VERSION && 18592 IPCL_IS_CONNECTED(connp) && 18593 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18594 connp->conn_dontroute == 0 && 18595 !connp->conn_nexthop_set && 18596 connp->conn_xmit_if_ill == NULL && 18597 connp->conn_nofailover_ill == NULL && 18598 do_tcpzcopy == 1) { 18599 /* 18600 * the checks above closely resemble the fast path checks 18601 * in tcp_send_data(). 18602 */ 18603 mutex_enter(&connp->conn_lock); 18604 ire = connp->conn_ire_cache; 18605 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18606 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18607 IRE_REFHOLD(ire); 18608 if (ire->ire_stq != NULL) { 18609 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18610 18611 zc_enabled = ill && (ill->ill_capabilities & 18612 ILL_CAPAB_ZEROCOPY) && 18613 (ill->ill_zerocopy_capab-> 18614 ill_zerocopy_flags != 0); 18615 } 18616 IRE_REFRELE(ire); 18617 } 18618 mutex_exit(&connp->conn_lock); 18619 } 18620 tcp->tcp_snd_zcopy_on = zc_enabled; 18621 if (!TCP_IS_DETACHED(tcp)) { 18622 if (zc_enabled) { 18623 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 18624 TCP_STAT(tcps, tcp_zcopy_on); 18625 } else { 18626 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18627 TCP_STAT(tcps, tcp_zcopy_off); 18628 } 18629 } 18630 return (zc_enabled); 18631 } 18632 18633 static mblk_t * 18634 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18635 { 18636 tcp_stack_t *tcps = tcp->tcp_tcps; 18637 18638 if (do_tcpzcopy == 2) 18639 return (bp); 18640 else if (tcp->tcp_snd_zcopy_on) { 18641 tcp->tcp_snd_zcopy_on = B_FALSE; 18642 if (!TCP_IS_DETACHED(tcp)) { 18643 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18644 TCP_STAT(tcps, tcp_zcopy_disable); 18645 } 18646 } 18647 return (tcp_zcopy_backoff(tcp, bp, 0)); 18648 } 18649 18650 /* 18651 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18652 * the original desballoca'ed segmapped mblk. 18653 */ 18654 static mblk_t * 18655 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18656 { 18657 mblk_t *head, *tail, *nbp; 18658 tcp_stack_t *tcps = tcp->tcp_tcps; 18659 18660 if (IS_VMLOANED_MBLK(bp)) { 18661 TCP_STAT(tcps, tcp_zcopy_backoff); 18662 if ((head = copyb(bp)) == NULL) { 18663 /* fail to backoff; leave it for the next backoff */ 18664 tcp->tcp_xmit_zc_clean = B_FALSE; 18665 return (bp); 18666 } 18667 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18668 if (fix_xmitlist) 18669 tcp_zcopy_notify(tcp); 18670 else 18671 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18672 } 18673 nbp = bp->b_cont; 18674 if (fix_xmitlist) { 18675 head->b_prev = bp->b_prev; 18676 head->b_next = bp->b_next; 18677 if (tcp->tcp_xmit_tail == bp) 18678 tcp->tcp_xmit_tail = head; 18679 } 18680 bp->b_next = NULL; 18681 bp->b_prev = NULL; 18682 freeb(bp); 18683 } else { 18684 head = bp; 18685 nbp = bp->b_cont; 18686 } 18687 tail = head; 18688 while (nbp) { 18689 if (IS_VMLOANED_MBLK(nbp)) { 18690 TCP_STAT(tcps, tcp_zcopy_backoff); 18691 if ((tail->b_cont = copyb(nbp)) == NULL) { 18692 tcp->tcp_xmit_zc_clean = B_FALSE; 18693 tail->b_cont = nbp; 18694 return (head); 18695 } 18696 tail = tail->b_cont; 18697 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18698 if (fix_xmitlist) 18699 tcp_zcopy_notify(tcp); 18700 else 18701 tail->b_datap->db_struioflag |= 18702 STRUIO_ZCNOTIFY; 18703 } 18704 bp = nbp; 18705 nbp = nbp->b_cont; 18706 if (fix_xmitlist) { 18707 tail->b_prev = bp->b_prev; 18708 tail->b_next = bp->b_next; 18709 if (tcp->tcp_xmit_tail == bp) 18710 tcp->tcp_xmit_tail = tail; 18711 } 18712 bp->b_next = NULL; 18713 bp->b_prev = NULL; 18714 freeb(bp); 18715 } else { 18716 tail->b_cont = nbp; 18717 tail = nbp; 18718 nbp = nbp->b_cont; 18719 } 18720 } 18721 if (fix_xmitlist) { 18722 tcp->tcp_xmit_last = tail; 18723 tcp->tcp_xmit_zc_clean = B_TRUE; 18724 } 18725 return (head); 18726 } 18727 18728 static void 18729 tcp_zcopy_notify(tcp_t *tcp) 18730 { 18731 struct stdata *stp; 18732 18733 if (tcp->tcp_detached) 18734 return; 18735 stp = STREAM(tcp->tcp_rq); 18736 mutex_enter(&stp->sd_lock); 18737 stp->sd_flag |= STZCNOTIFY; 18738 cv_broadcast(&stp->sd_zcopy_wait); 18739 mutex_exit(&stp->sd_lock); 18740 } 18741 18742 static boolean_t 18743 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18744 { 18745 ire_t *ire; 18746 conn_t *connp = tcp->tcp_connp; 18747 tcp_stack_t *tcps = tcp->tcp_tcps; 18748 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18749 18750 mutex_enter(&connp->conn_lock); 18751 ire = connp->conn_ire_cache; 18752 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18753 18754 if ((ire != NULL) && 18755 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18756 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18757 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18758 IRE_REFHOLD(ire); 18759 mutex_exit(&connp->conn_lock); 18760 } else { 18761 boolean_t cached = B_FALSE; 18762 ts_label_t *tsl; 18763 18764 /* force a recheck later on */ 18765 tcp->tcp_ire_ill_check_done = B_FALSE; 18766 18767 TCP_DBGSTAT(tcps, tcp_ire_null1); 18768 connp->conn_ire_cache = NULL; 18769 mutex_exit(&connp->conn_lock); 18770 18771 if (ire != NULL) 18772 IRE_REFRELE_NOTR(ire); 18773 18774 tsl = crgetlabel(CONN_CRED(connp)); 18775 ire = (dst ? 18776 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18777 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18778 connp->conn_zoneid, tsl, ipst)); 18779 18780 if (ire == NULL) { 18781 TCP_STAT(tcps, tcp_ire_null); 18782 return (B_FALSE); 18783 } 18784 18785 IRE_REFHOLD_NOTR(ire); 18786 /* 18787 * Since we are inside the squeue, there cannot be another 18788 * thread in TCP trying to set the conn_ire_cache now. The 18789 * check for IRE_MARK_CONDEMNED ensures that an interface 18790 * unplumb thread has not yet started cleaning up the conns. 18791 * Hence we don't need to grab the conn lock. 18792 */ 18793 if (CONN_CACHE_IRE(connp)) { 18794 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18795 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18796 TCP_CHECK_IREINFO(tcp, ire); 18797 connp->conn_ire_cache = ire; 18798 cached = B_TRUE; 18799 } 18800 rw_exit(&ire->ire_bucket->irb_lock); 18801 } 18802 18803 /* 18804 * We can continue to use the ire but since it was 18805 * not cached, we should drop the extra reference. 18806 */ 18807 if (!cached) 18808 IRE_REFRELE_NOTR(ire); 18809 18810 /* 18811 * Rampart note: no need to select a new label here, since 18812 * labels are not allowed to change during the life of a TCP 18813 * connection. 18814 */ 18815 } 18816 18817 *irep = ire; 18818 18819 return (B_TRUE); 18820 } 18821 18822 /* 18823 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18824 * 18825 * 0 = success; 18826 * 1 = failed to find ire and ill. 18827 */ 18828 static boolean_t 18829 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18830 { 18831 ipha_t *ipha; 18832 ipaddr_t dst; 18833 ire_t *ire; 18834 ill_t *ill; 18835 conn_t *connp = tcp->tcp_connp; 18836 mblk_t *ire_fp_mp; 18837 tcp_stack_t *tcps = tcp->tcp_tcps; 18838 18839 if (mp != NULL) 18840 ipha = (ipha_t *)mp->b_rptr; 18841 else 18842 ipha = tcp->tcp_ipha; 18843 dst = ipha->ipha_dst; 18844 18845 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18846 return (B_FALSE); 18847 18848 if ((ire->ire_flags & RTF_MULTIRT) || 18849 (ire->ire_stq == NULL) || 18850 (ire->ire_nce == NULL) || 18851 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18852 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18853 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18854 TCP_STAT(tcps, tcp_ip_ire_send); 18855 IRE_REFRELE(ire); 18856 return (B_FALSE); 18857 } 18858 18859 ill = ire_to_ill(ire); 18860 if (connp->conn_outgoing_ill != NULL) { 18861 ill_t *conn_outgoing_ill = NULL; 18862 /* 18863 * Choose a good ill in the group to send the packets on. 18864 */ 18865 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18866 ill = ire_to_ill(ire); 18867 } 18868 ASSERT(ill != NULL); 18869 18870 if (!tcp->tcp_ire_ill_check_done) { 18871 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18872 tcp->tcp_ire_ill_check_done = B_TRUE; 18873 } 18874 18875 *irep = ire; 18876 *illp = ill; 18877 18878 return (B_TRUE); 18879 } 18880 18881 static void 18882 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18883 { 18884 ipha_t *ipha; 18885 ipaddr_t src; 18886 ipaddr_t dst; 18887 uint32_t cksum; 18888 ire_t *ire; 18889 uint16_t *up; 18890 ill_t *ill; 18891 conn_t *connp = tcp->tcp_connp; 18892 uint32_t hcksum_txflags = 0; 18893 mblk_t *ire_fp_mp; 18894 uint_t ire_fp_mp_len; 18895 tcp_stack_t *tcps = tcp->tcp_tcps; 18896 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18897 18898 ASSERT(DB_TYPE(mp) == M_DATA); 18899 18900 if (DB_CRED(mp) == NULL) 18901 mblk_setcred(mp, CONN_CRED(connp)); 18902 18903 ipha = (ipha_t *)mp->b_rptr; 18904 src = ipha->ipha_src; 18905 dst = ipha->ipha_dst; 18906 18907 /* 18908 * Drop off fast path for IPv6 and also if options are present or 18909 * we need to resolve a TS label. 18910 */ 18911 if (tcp->tcp_ipversion != IPV4_VERSION || 18912 !IPCL_IS_CONNECTED(connp) || 18913 !CONN_IS_LSO_MD_FASTPATH(connp) || 18914 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18915 !connp->conn_ulp_labeled || 18916 ipha->ipha_ident == IP_HDR_INCLUDED || 18917 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18918 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18919 if (tcp->tcp_snd_zcopy_aware) 18920 mp = tcp_zcopy_disable(tcp, mp); 18921 TCP_STAT(tcps, tcp_ip_send); 18922 CALL_IP_WPUT(connp, q, mp); 18923 return; 18924 } 18925 18926 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18927 if (tcp->tcp_snd_zcopy_aware) 18928 mp = tcp_zcopy_backoff(tcp, mp, 0); 18929 CALL_IP_WPUT(connp, q, mp); 18930 return; 18931 } 18932 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18933 ire_fp_mp_len = MBLKL(ire_fp_mp); 18934 18935 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18936 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18937 #ifndef _BIG_ENDIAN 18938 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18939 #endif 18940 18941 /* 18942 * Check to see if we need to re-enable LSO/MDT for this connection 18943 * because it was previously disabled due to changes in the ill; 18944 * note that by doing it here, this re-enabling only applies when 18945 * the packet is not dispatched through CALL_IP_WPUT(). 18946 * 18947 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18948 * case, since that's how we ended up here. For IPv6, we do the 18949 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18950 */ 18951 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18952 /* 18953 * Restore LSO for this connection, so that next time around 18954 * it is eligible to go through tcp_lsosend() path again. 18955 */ 18956 TCP_STAT(tcps, tcp_lso_enabled); 18957 tcp->tcp_lso = B_TRUE; 18958 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18959 "interface %s\n", (void *)connp, ill->ill_name)); 18960 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18961 /* 18962 * Restore MDT for this connection, so that next time around 18963 * it is eligible to go through tcp_multisend() path again. 18964 */ 18965 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18966 tcp->tcp_mdt = B_TRUE; 18967 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18968 "interface %s\n", (void *)connp, ill->ill_name)); 18969 } 18970 18971 if (tcp->tcp_snd_zcopy_aware) { 18972 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18973 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18974 mp = tcp_zcopy_disable(tcp, mp); 18975 /* 18976 * we shouldn't need to reset ipha as the mp containing 18977 * ipha should never be a zero-copy mp. 18978 */ 18979 } 18980 18981 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18982 ASSERT(ill->ill_hcksum_capab != NULL); 18983 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18984 } 18985 18986 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18987 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18988 18989 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18990 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18991 18992 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18993 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18994 18995 /* Software checksum? */ 18996 if (DB_CKSUMFLAGS(mp) == 0) { 18997 TCP_STAT(tcps, tcp_out_sw_cksum); 18998 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18999 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19000 } 19001 19002 ipha->ipha_fragment_offset_and_flags |= 19003 (uint32_t)htons(ire->ire_frag_flag); 19004 19005 /* Calculate IP header checksum if hardware isn't capable */ 19006 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19007 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19008 ((uint16_t *)ipha)[4]); 19009 } 19010 19011 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19012 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19013 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19014 19015 UPDATE_OB_PKT_COUNT(ire); 19016 ire->ire_last_used_time = lbolt; 19017 19018 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19019 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19020 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19021 ntohs(ipha->ipha_length)); 19022 19023 if (ILL_DLS_CAPABLE(ill)) { 19024 /* 19025 * Send the packet directly to DLD, where it may be queued 19026 * depending on the availability of transmit resources at 19027 * the media layer. 19028 */ 19029 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 19030 } else { 19031 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 19032 DTRACE_PROBE4(ip4__physical__out__start, 19033 ill_t *, NULL, ill_t *, out_ill, 19034 ipha_t *, ipha, mblk_t *, mp); 19035 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19036 ipst->ips_ipv4firewall_physical_out, 19037 NULL, out_ill, ipha, mp, mp, ipst); 19038 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19039 if (mp != NULL) 19040 putnext(ire->ire_stq, mp); 19041 } 19042 IRE_REFRELE(ire); 19043 } 19044 19045 /* 19046 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19047 * if the receiver shrinks the window, i.e. moves the right window to the 19048 * left, the we should not send new data, but should retransmit normally the 19049 * old unacked data between suna and suna + swnd. We might has sent data 19050 * that is now outside the new window, pretend that we didn't send it. 19051 */ 19052 static void 19053 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19054 { 19055 uint32_t snxt = tcp->tcp_snxt; 19056 mblk_t *xmit_tail; 19057 int32_t offset; 19058 19059 ASSERT(shrunk_count > 0); 19060 19061 /* Pretend we didn't send the data outside the window */ 19062 snxt -= shrunk_count; 19063 19064 /* Get the mblk and the offset in it per the shrunk window */ 19065 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19066 19067 ASSERT(xmit_tail != NULL); 19068 19069 /* Reset all the values per the now shrunk window */ 19070 tcp->tcp_snxt = snxt; 19071 tcp->tcp_xmit_tail = xmit_tail; 19072 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19073 offset; 19074 tcp->tcp_unsent += shrunk_count; 19075 19076 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19077 /* 19078 * Make sure the timer is running so that we will probe a zero 19079 * window. 19080 */ 19081 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19082 } 19083 19084 19085 /* 19086 * The TCP normal data output path. 19087 * NOTE: the logic of the fast path is duplicated from this function. 19088 */ 19089 static void 19090 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19091 { 19092 int len; 19093 mblk_t *local_time; 19094 mblk_t *mp1; 19095 uint32_t snxt; 19096 int tail_unsent; 19097 int tcpstate; 19098 int usable = 0; 19099 mblk_t *xmit_tail; 19100 queue_t *q = tcp->tcp_wq; 19101 int32_t mss; 19102 int32_t num_sack_blk = 0; 19103 int32_t tcp_hdr_len; 19104 int32_t tcp_tcp_hdr_len; 19105 int mdt_thres; 19106 int rc; 19107 tcp_stack_t *tcps = tcp->tcp_tcps; 19108 ip_stack_t *ipst; 19109 19110 tcpstate = tcp->tcp_state; 19111 if (mp == NULL) { 19112 /* 19113 * tcp_wput_data() with NULL mp should only be called when 19114 * there is unsent data. 19115 */ 19116 ASSERT(tcp->tcp_unsent > 0); 19117 /* Really tacky... but we need this for detached closes. */ 19118 len = tcp->tcp_unsent; 19119 goto data_null; 19120 } 19121 19122 #if CCS_STATS 19123 wrw_stats.tot.count++; 19124 wrw_stats.tot.bytes += msgdsize(mp); 19125 #endif 19126 ASSERT(mp->b_datap->db_type == M_DATA); 19127 /* 19128 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19129 * or before a connection attempt has begun. 19130 */ 19131 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19132 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19133 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19134 #ifdef DEBUG 19135 cmn_err(CE_WARN, 19136 "tcp_wput_data: data after ordrel, %s", 19137 tcp_display(tcp, NULL, 19138 DISP_ADDR_AND_PORT)); 19139 #else 19140 if (tcp->tcp_debug) { 19141 (void) strlog(TCP_MOD_ID, 0, 1, 19142 SL_TRACE|SL_ERROR, 19143 "tcp_wput_data: data after ordrel, %s\n", 19144 tcp_display(tcp, NULL, 19145 DISP_ADDR_AND_PORT)); 19146 } 19147 #endif /* DEBUG */ 19148 } 19149 if (tcp->tcp_snd_zcopy_aware && 19150 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19151 tcp_zcopy_notify(tcp); 19152 freemsg(mp); 19153 mutex_enter(&tcp->tcp_non_sq_lock); 19154 if (tcp->tcp_flow_stopped && 19155 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19156 tcp_clrqfull(tcp); 19157 } 19158 mutex_exit(&tcp->tcp_non_sq_lock); 19159 return; 19160 } 19161 19162 /* Strip empties */ 19163 for (;;) { 19164 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19165 (uintptr_t)INT_MAX); 19166 len = (int)(mp->b_wptr - mp->b_rptr); 19167 if (len > 0) 19168 break; 19169 mp1 = mp; 19170 mp = mp->b_cont; 19171 freeb(mp1); 19172 if (!mp) { 19173 return; 19174 } 19175 } 19176 19177 /* If we are the first on the list ... */ 19178 if (tcp->tcp_xmit_head == NULL) { 19179 tcp->tcp_xmit_head = mp; 19180 tcp->tcp_xmit_tail = mp; 19181 tcp->tcp_xmit_tail_unsent = len; 19182 } else { 19183 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19184 struct datab *dp; 19185 19186 mp1 = tcp->tcp_xmit_last; 19187 if (len < tcp_tx_pull_len && 19188 (dp = mp1->b_datap)->db_ref == 1 && 19189 dp->db_lim - mp1->b_wptr >= len) { 19190 ASSERT(len > 0); 19191 ASSERT(!mp1->b_cont); 19192 if (len == 1) { 19193 *mp1->b_wptr++ = *mp->b_rptr; 19194 } else { 19195 bcopy(mp->b_rptr, mp1->b_wptr, len); 19196 mp1->b_wptr += len; 19197 } 19198 if (mp1 == tcp->tcp_xmit_tail) 19199 tcp->tcp_xmit_tail_unsent += len; 19200 mp1->b_cont = mp->b_cont; 19201 if (tcp->tcp_snd_zcopy_aware && 19202 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19203 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19204 freeb(mp); 19205 mp = mp1; 19206 } else { 19207 tcp->tcp_xmit_last->b_cont = mp; 19208 } 19209 len += tcp->tcp_unsent; 19210 } 19211 19212 /* Tack on however many more positive length mblks we have */ 19213 if ((mp1 = mp->b_cont) != NULL) { 19214 do { 19215 int tlen; 19216 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19217 (uintptr_t)INT_MAX); 19218 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19219 if (tlen <= 0) { 19220 mp->b_cont = mp1->b_cont; 19221 freeb(mp1); 19222 } else { 19223 len += tlen; 19224 mp = mp1; 19225 } 19226 } while ((mp1 = mp->b_cont) != NULL); 19227 } 19228 tcp->tcp_xmit_last = mp; 19229 tcp->tcp_unsent = len; 19230 19231 if (urgent) 19232 usable = 1; 19233 19234 data_null: 19235 snxt = tcp->tcp_snxt; 19236 xmit_tail = tcp->tcp_xmit_tail; 19237 tail_unsent = tcp->tcp_xmit_tail_unsent; 19238 19239 /* 19240 * Note that tcp_mss has been adjusted to take into account the 19241 * timestamp option if applicable. Because SACK options do not 19242 * appear in every TCP segments and they are of variable lengths, 19243 * they cannot be included in tcp_mss. Thus we need to calculate 19244 * the actual segment length when we need to send a segment which 19245 * includes SACK options. 19246 */ 19247 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19248 int32_t opt_len; 19249 19250 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19251 tcp->tcp_num_sack_blk); 19252 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19253 2 + TCPOPT_HEADER_LEN; 19254 mss = tcp->tcp_mss - opt_len; 19255 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19256 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19257 } else { 19258 mss = tcp->tcp_mss; 19259 tcp_hdr_len = tcp->tcp_hdr_len; 19260 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19261 } 19262 19263 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19264 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19265 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19266 } 19267 if (tcpstate == TCPS_SYN_RCVD) { 19268 /* 19269 * The three-way connection establishment handshake is not 19270 * complete yet. We want to queue the data for transmission 19271 * after entering ESTABLISHED state (RFC793). A jump to 19272 * "done" label effectively leaves data on the queue. 19273 */ 19274 goto done; 19275 } else { 19276 int usable_r; 19277 19278 /* 19279 * In the special case when cwnd is zero, which can only 19280 * happen if the connection is ECN capable, return now. 19281 * New segments is sent using tcp_timer(). The timer 19282 * is set in tcp_rput_data(). 19283 */ 19284 if (tcp->tcp_cwnd == 0) { 19285 /* 19286 * Note that tcp_cwnd is 0 before 3-way handshake is 19287 * finished. 19288 */ 19289 ASSERT(tcp->tcp_ecn_ok || 19290 tcp->tcp_state < TCPS_ESTABLISHED); 19291 return; 19292 } 19293 19294 /* NOTE: trouble if xmitting while SYN not acked? */ 19295 usable_r = snxt - tcp->tcp_suna; 19296 usable_r = tcp->tcp_swnd - usable_r; 19297 19298 /* 19299 * Check if the receiver has shrunk the window. If 19300 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19301 * cannot be set as there is unsent data, so FIN cannot 19302 * be sent out. Otherwise, we need to take into account 19303 * of FIN as it consumes an "invisible" sequence number. 19304 */ 19305 ASSERT(tcp->tcp_fin_sent == 0); 19306 if (usable_r < 0) { 19307 /* 19308 * The receiver has shrunk the window and we have sent 19309 * -usable_r date beyond the window, re-adjust. 19310 * 19311 * If TCP window scaling is enabled, there can be 19312 * round down error as the advertised receive window 19313 * is actually right shifted n bits. This means that 19314 * the lower n bits info is wiped out. It will look 19315 * like the window is shrunk. Do a check here to 19316 * see if the shrunk amount is actually within the 19317 * error in window calculation. If it is, just 19318 * return. Note that this check is inside the 19319 * shrunk window check. This makes sure that even 19320 * though tcp_process_shrunk_swnd() is not called, 19321 * we will stop further processing. 19322 */ 19323 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19324 tcp_process_shrunk_swnd(tcp, -usable_r); 19325 } 19326 return; 19327 } 19328 19329 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19330 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19331 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19332 19333 /* usable = MIN(usable, unsent) */ 19334 if (usable_r > len) 19335 usable_r = len; 19336 19337 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19338 if (usable_r > 0) { 19339 usable = usable_r; 19340 } else { 19341 /* Bypass all other unnecessary processing. */ 19342 goto done; 19343 } 19344 } 19345 19346 local_time = (mblk_t *)lbolt; 19347 19348 /* 19349 * "Our" Nagle Algorithm. This is not the same as in the old 19350 * BSD. This is more in line with the true intent of Nagle. 19351 * 19352 * The conditions are: 19353 * 1. The amount of unsent data (or amount of data which can be 19354 * sent, whichever is smaller) is less than Nagle limit. 19355 * 2. The last sent size is also less than Nagle limit. 19356 * 3. There is unack'ed data. 19357 * 4. Urgent pointer is not set. Send urgent data ignoring the 19358 * Nagle algorithm. This reduces the probability that urgent 19359 * bytes get "merged" together. 19360 * 5. The app has not closed the connection. This eliminates the 19361 * wait time of the receiving side waiting for the last piece of 19362 * (small) data. 19363 * 19364 * If all are satisified, exit without sending anything. Note 19365 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19366 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19367 * 4095). 19368 */ 19369 if (usable < (int)tcp->tcp_naglim && 19370 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19371 snxt != tcp->tcp_suna && 19372 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19373 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19374 goto done; 19375 } 19376 19377 if (tcp->tcp_cork) { 19378 /* 19379 * if the tcp->tcp_cork option is set, then we have to force 19380 * TCP not to send partial segment (smaller than MSS bytes). 19381 * We are calculating the usable now based on full mss and 19382 * will save the rest of remaining data for later. 19383 */ 19384 if (usable < mss) 19385 goto done; 19386 usable = (usable / mss) * mss; 19387 } 19388 19389 /* Update the latest receive window size in TCP header. */ 19390 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19391 tcp->tcp_tcph->th_win); 19392 19393 /* 19394 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19395 * 19396 * 1. Simple TCP/IP{v4,v6} (no options). 19397 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19398 * 3. If the TCP connection is in ESTABLISHED state. 19399 * 4. The TCP is not detached. 19400 * 19401 * If any of the above conditions have changed during the 19402 * connection, stop using LSO/MDT and restore the stream head 19403 * parameters accordingly. 19404 */ 19405 ipst = tcps->tcps_netstack->netstack_ip; 19406 19407 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19408 ((tcp->tcp_ipversion == IPV4_VERSION && 19409 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19410 (tcp->tcp_ipversion == IPV6_VERSION && 19411 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19412 tcp->tcp_state != TCPS_ESTABLISHED || 19413 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19414 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19415 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19416 if (tcp->tcp_lso) { 19417 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19418 tcp->tcp_lso = B_FALSE; 19419 } else { 19420 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19421 tcp->tcp_mdt = B_FALSE; 19422 } 19423 19424 /* Anything other than detached is considered pathological */ 19425 if (!TCP_IS_DETACHED(tcp)) { 19426 if (tcp->tcp_lso) 19427 TCP_STAT(tcps, tcp_lso_disabled); 19428 else 19429 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19430 (void) tcp_maxpsz_set(tcp, B_TRUE); 19431 } 19432 } 19433 19434 /* Use MDT if sendable amount is greater than the threshold */ 19435 if (tcp->tcp_mdt && 19436 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19437 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19438 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19439 (tcp->tcp_valid_bits == 0 || 19440 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19441 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19442 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19443 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19444 local_time, mdt_thres); 19445 } else { 19446 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19447 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19448 local_time, INT_MAX); 19449 } 19450 19451 /* Pretend that all we were trying to send really got sent */ 19452 if (rc < 0 && tail_unsent < 0) { 19453 do { 19454 xmit_tail = xmit_tail->b_cont; 19455 xmit_tail->b_prev = local_time; 19456 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19457 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19458 tail_unsent += (int)(xmit_tail->b_wptr - 19459 xmit_tail->b_rptr); 19460 } while (tail_unsent < 0); 19461 } 19462 done:; 19463 tcp->tcp_xmit_tail = xmit_tail; 19464 tcp->tcp_xmit_tail_unsent = tail_unsent; 19465 len = tcp->tcp_snxt - snxt; 19466 if (len) { 19467 /* 19468 * If new data was sent, need to update the notsack 19469 * list, which is, afterall, data blocks that have 19470 * not been sack'ed by the receiver. New data is 19471 * not sack'ed. 19472 */ 19473 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19474 /* len is a negative value. */ 19475 tcp->tcp_pipe -= len; 19476 tcp_notsack_update(&(tcp->tcp_notsack_list), 19477 tcp->tcp_snxt, snxt, 19478 &(tcp->tcp_num_notsack_blk), 19479 &(tcp->tcp_cnt_notsack_list)); 19480 } 19481 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19482 tcp->tcp_rack = tcp->tcp_rnxt; 19483 tcp->tcp_rack_cnt = 0; 19484 if ((snxt + len) == tcp->tcp_suna) { 19485 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19486 } 19487 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19488 /* 19489 * Didn't send anything. Make sure the timer is running 19490 * so that we will probe a zero window. 19491 */ 19492 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19493 } 19494 /* Note that len is the amount we just sent but with a negative sign */ 19495 tcp->tcp_unsent += len; 19496 mutex_enter(&tcp->tcp_non_sq_lock); 19497 if (tcp->tcp_flow_stopped) { 19498 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19499 tcp_clrqfull(tcp); 19500 } 19501 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19502 tcp_setqfull(tcp); 19503 } 19504 mutex_exit(&tcp->tcp_non_sq_lock); 19505 } 19506 19507 /* 19508 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19509 * outgoing TCP header with the template header, as well as other 19510 * options such as time-stamp, ECN and/or SACK. 19511 */ 19512 static void 19513 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19514 { 19515 tcph_t *tcp_tmpl, *tcp_h; 19516 uint32_t *dst, *src; 19517 int hdrlen; 19518 19519 ASSERT(OK_32PTR(rptr)); 19520 19521 /* Template header */ 19522 tcp_tmpl = tcp->tcp_tcph; 19523 19524 /* Header of outgoing packet */ 19525 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19526 19527 /* dst and src are opaque 32-bit fields, used for copying */ 19528 dst = (uint32_t *)rptr; 19529 src = (uint32_t *)tcp->tcp_iphc; 19530 hdrlen = tcp->tcp_hdr_len; 19531 19532 /* Fill time-stamp option if needed */ 19533 if (tcp->tcp_snd_ts_ok) { 19534 U32_TO_BE32((uint32_t)now, 19535 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19536 U32_TO_BE32(tcp->tcp_ts_recent, 19537 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19538 } else { 19539 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19540 } 19541 19542 /* 19543 * Copy the template header; is this really more efficient than 19544 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19545 * but perhaps not for other scenarios. 19546 */ 19547 dst[0] = src[0]; 19548 dst[1] = src[1]; 19549 dst[2] = src[2]; 19550 dst[3] = src[3]; 19551 dst[4] = src[4]; 19552 dst[5] = src[5]; 19553 dst[6] = src[6]; 19554 dst[7] = src[7]; 19555 dst[8] = src[8]; 19556 dst[9] = src[9]; 19557 if (hdrlen -= 40) { 19558 hdrlen >>= 2; 19559 dst += 10; 19560 src += 10; 19561 do { 19562 *dst++ = *src++; 19563 } while (--hdrlen); 19564 } 19565 19566 /* 19567 * Set the ECN info in the TCP header if it is not a zero 19568 * window probe. Zero window probe is only sent in 19569 * tcp_wput_data() and tcp_timer(). 19570 */ 19571 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19572 SET_ECT(tcp, rptr); 19573 19574 if (tcp->tcp_ecn_echo_on) 19575 tcp_h->th_flags[0] |= TH_ECE; 19576 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19577 tcp_h->th_flags[0] |= TH_CWR; 19578 tcp->tcp_ecn_cwr_sent = B_TRUE; 19579 } 19580 } 19581 19582 /* Fill in SACK options */ 19583 if (num_sack_blk > 0) { 19584 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19585 sack_blk_t *tmp; 19586 int32_t i; 19587 19588 wptr[0] = TCPOPT_NOP; 19589 wptr[1] = TCPOPT_NOP; 19590 wptr[2] = TCPOPT_SACK; 19591 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19592 sizeof (sack_blk_t); 19593 wptr += TCPOPT_REAL_SACK_LEN; 19594 19595 tmp = tcp->tcp_sack_list; 19596 for (i = 0; i < num_sack_blk; i++) { 19597 U32_TO_BE32(tmp[i].begin, wptr); 19598 wptr += sizeof (tcp_seq); 19599 U32_TO_BE32(tmp[i].end, wptr); 19600 wptr += sizeof (tcp_seq); 19601 } 19602 tcp_h->th_offset_and_rsrvd[0] += 19603 ((num_sack_blk * 2 + 1) << 4); 19604 } 19605 } 19606 19607 /* 19608 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19609 * the destination address and SAP attribute, and if necessary, the 19610 * hardware checksum offload attribute to a Multidata message. 19611 */ 19612 static int 19613 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19614 const uint32_t start, const uint32_t stuff, const uint32_t end, 19615 const uint32_t flags, tcp_stack_t *tcps) 19616 { 19617 /* Add global destination address & SAP attribute */ 19618 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19619 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19620 "destination address+SAP\n")); 19621 19622 if (dlmp != NULL) 19623 TCP_STAT(tcps, tcp_mdt_allocfail); 19624 return (-1); 19625 } 19626 19627 /* Add global hwcksum attribute */ 19628 if (hwcksum && 19629 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19630 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19631 "checksum attribute\n")); 19632 19633 TCP_STAT(tcps, tcp_mdt_allocfail); 19634 return (-1); 19635 } 19636 19637 return (0); 19638 } 19639 19640 /* 19641 * Smaller and private version of pdescinfo_t used specifically for TCP, 19642 * which allows for only two payload spans per packet. 19643 */ 19644 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19645 19646 /* 19647 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19648 * scheme, and returns one the following: 19649 * 19650 * -1 = failed allocation. 19651 * 0 = success; burst count reached, or usable send window is too small, 19652 * and that we'd rather wait until later before sending again. 19653 */ 19654 static int 19655 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19656 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19657 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19658 const int mdt_thres) 19659 { 19660 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19661 multidata_t *mmd; 19662 uint_t obsegs, obbytes, hdr_frag_sz; 19663 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19664 int num_burst_seg, max_pld; 19665 pdesc_t *pkt; 19666 tcp_pdescinfo_t tcp_pkt_info; 19667 pdescinfo_t *pkt_info; 19668 int pbuf_idx, pbuf_idx_nxt; 19669 int seg_len, len, spill, af; 19670 boolean_t add_buffer, zcopy, clusterwide; 19671 boolean_t buf_trunked = B_FALSE; 19672 boolean_t rconfirm = B_FALSE; 19673 boolean_t done = B_FALSE; 19674 uint32_t cksum; 19675 uint32_t hwcksum_flags; 19676 ire_t *ire = NULL; 19677 ill_t *ill; 19678 ipha_t *ipha; 19679 ip6_t *ip6h; 19680 ipaddr_t src, dst; 19681 ill_zerocopy_capab_t *zc_cap = NULL; 19682 uint16_t *up; 19683 int err; 19684 conn_t *connp; 19685 mblk_t *mp, *mp1, *fw_mp_head = NULL; 19686 uchar_t *pld_start; 19687 tcp_stack_t *tcps = tcp->tcp_tcps; 19688 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19689 19690 #ifdef _BIG_ENDIAN 19691 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19692 #else 19693 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19694 #endif 19695 19696 #define PREP_NEW_MULTIDATA() { \ 19697 mmd = NULL; \ 19698 md_mp = md_hbuf = NULL; \ 19699 cur_hdr_off = 0; \ 19700 max_pld = tcp->tcp_mdt_max_pld; \ 19701 pbuf_idx = pbuf_idx_nxt = -1; \ 19702 add_buffer = B_TRUE; \ 19703 zcopy = B_FALSE; \ 19704 } 19705 19706 #define PREP_NEW_PBUF() { \ 19707 md_pbuf = md_pbuf_nxt = NULL; \ 19708 pbuf_idx = pbuf_idx_nxt = -1; \ 19709 cur_pld_off = 0; \ 19710 first_snxt = *snxt; \ 19711 ASSERT(*tail_unsent > 0); \ 19712 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19713 } 19714 19715 ASSERT(mdt_thres >= mss); 19716 ASSERT(*usable > 0 && *usable > mdt_thres); 19717 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19718 ASSERT(!TCP_IS_DETACHED(tcp)); 19719 ASSERT(tcp->tcp_valid_bits == 0 || 19720 tcp->tcp_valid_bits == TCP_FSS_VALID); 19721 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19722 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19723 (tcp->tcp_ipversion == IPV6_VERSION && 19724 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19725 19726 connp = tcp->tcp_connp; 19727 ASSERT(connp != NULL); 19728 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19729 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19730 19731 /* 19732 * Note that tcp will only declare at most 2 payload spans per 19733 * packet, which is much lower than the maximum allowable number 19734 * of packet spans per Multidata. For this reason, we use the 19735 * privately declared and smaller descriptor info structure, in 19736 * order to save some stack space. 19737 */ 19738 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19739 19740 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19741 if (af == AF_INET) { 19742 dst = tcp->tcp_ipha->ipha_dst; 19743 src = tcp->tcp_ipha->ipha_src; 19744 ASSERT(!CLASSD(dst)); 19745 } 19746 ASSERT(af == AF_INET || 19747 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19748 19749 obsegs = obbytes = 0; 19750 num_burst_seg = tcp->tcp_snd_burst; 19751 md_mp_head = NULL; 19752 PREP_NEW_MULTIDATA(); 19753 19754 /* 19755 * Before we go on further, make sure there is an IRE that we can 19756 * use, and that the ILL supports MDT. Otherwise, there's no point 19757 * in proceeding any further, and we should just hand everything 19758 * off to the legacy path. 19759 */ 19760 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19761 goto legacy_send_no_md; 19762 19763 ASSERT(ire != NULL); 19764 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19765 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19766 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19767 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19768 /* 19769 * If we do support loopback for MDT (which requires modifications 19770 * to the receiving paths), the following assertions should go away, 19771 * and we would be sending the Multidata to loopback conn later on. 19772 */ 19773 ASSERT(!IRE_IS_LOCAL(ire)); 19774 ASSERT(ire->ire_stq != NULL); 19775 19776 ill = ire_to_ill(ire); 19777 ASSERT(ill != NULL); 19778 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19779 19780 if (!tcp->tcp_ire_ill_check_done) { 19781 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19782 tcp->tcp_ire_ill_check_done = B_TRUE; 19783 } 19784 19785 /* 19786 * If the underlying interface conditions have changed, or if the 19787 * new interface does not support MDT, go back to legacy path. 19788 */ 19789 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19790 /* don't go through this path anymore for this connection */ 19791 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19792 tcp->tcp_mdt = B_FALSE; 19793 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19794 "interface %s\n", (void *)connp, ill->ill_name)); 19795 /* IRE will be released prior to returning */ 19796 goto legacy_send_no_md; 19797 } 19798 19799 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19800 zc_cap = ill->ill_zerocopy_capab; 19801 19802 /* 19803 * Check if we can take tcp fast-path. Note that "incomplete" 19804 * ire's (where the link-layer for next hop is not resolved 19805 * or where the fast-path header in nce_fp_mp is not available 19806 * yet) are sent down the legacy (slow) path. 19807 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19808 */ 19809 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19810 /* IRE will be released prior to returning */ 19811 goto legacy_send_no_md; 19812 } 19813 19814 /* go to legacy path if interface doesn't support zerocopy */ 19815 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19816 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19817 /* IRE will be released prior to returning */ 19818 goto legacy_send_no_md; 19819 } 19820 19821 /* does the interface support hardware checksum offload? */ 19822 hwcksum_flags = 0; 19823 if (ILL_HCKSUM_CAPABLE(ill) && 19824 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19825 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19826 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19827 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19828 HCKSUM_IPHDRCKSUM) 19829 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19830 19831 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19832 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19833 hwcksum_flags |= HCK_FULLCKSUM; 19834 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19835 HCKSUM_INET_PARTIAL) 19836 hwcksum_flags |= HCK_PARTIALCKSUM; 19837 } 19838 19839 /* 19840 * Each header fragment consists of the leading extra space, 19841 * followed by the TCP/IP header, and the trailing extra space. 19842 * We make sure that each header fragment begins on a 32-bit 19843 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19844 * aligned in tcp_mdt_update). 19845 */ 19846 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19847 tcp->tcp_mdt_hdr_tail), 4); 19848 19849 /* are we starting from the beginning of data block? */ 19850 if (*tail_unsent == 0) { 19851 *xmit_tail = (*xmit_tail)->b_cont; 19852 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19853 *tail_unsent = (int)MBLKL(*xmit_tail); 19854 } 19855 19856 /* 19857 * Here we create one or more Multidata messages, each made up of 19858 * one header buffer and up to N payload buffers. This entire 19859 * operation is done within two loops: 19860 * 19861 * The outer loop mostly deals with creating the Multidata message, 19862 * as well as the header buffer that gets added to it. It also 19863 * links the Multidata messages together such that all of them can 19864 * be sent down to the lower layer in a single putnext call; this 19865 * linking behavior depends on the tcp_mdt_chain tunable. 19866 * 19867 * The inner loop takes an existing Multidata message, and adds 19868 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19869 * packetizes those buffers by filling up the corresponding header 19870 * buffer fragments with the proper IP and TCP headers, and by 19871 * describing the layout of each packet in the packet descriptors 19872 * that get added to the Multidata. 19873 */ 19874 do { 19875 /* 19876 * If usable send window is too small, or data blocks in 19877 * transmit list are smaller than our threshold (i.e. app 19878 * performs large writes followed by small ones), we hand 19879 * off the control over to the legacy path. Note that we'll 19880 * get back the control once it encounters a large block. 19881 */ 19882 if (*usable < mss || (*tail_unsent <= mdt_thres && 19883 (*xmit_tail)->b_cont != NULL && 19884 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19885 /* send down what we've got so far */ 19886 if (md_mp_head != NULL) { 19887 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19888 obsegs, obbytes, &rconfirm); 19889 } 19890 /* 19891 * Pass control over to tcp_send(), but tell it to 19892 * return to us once a large-size transmission is 19893 * possible. 19894 */ 19895 TCP_STAT(tcps, tcp_mdt_legacy_small); 19896 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19897 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19898 tail_unsent, xmit_tail, local_time, 19899 mdt_thres)) <= 0) { 19900 /* burst count reached, or alloc failed */ 19901 IRE_REFRELE(ire); 19902 return (err); 19903 } 19904 19905 /* tcp_send() may have sent everything, so check */ 19906 if (*usable <= 0) { 19907 IRE_REFRELE(ire); 19908 return (0); 19909 } 19910 19911 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19912 /* 19913 * We may have delivered the Multidata, so make sure 19914 * to re-initialize before the next round. 19915 */ 19916 md_mp_head = NULL; 19917 obsegs = obbytes = 0; 19918 num_burst_seg = tcp->tcp_snd_burst; 19919 PREP_NEW_MULTIDATA(); 19920 19921 /* are we starting from the beginning of data block? */ 19922 if (*tail_unsent == 0) { 19923 *xmit_tail = (*xmit_tail)->b_cont; 19924 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19925 (uintptr_t)INT_MAX); 19926 *tail_unsent = (int)MBLKL(*xmit_tail); 19927 } 19928 } 19929 19930 /* 19931 * max_pld limits the number of mblks in tcp's transmit 19932 * queue that can be added to a Multidata message. Once 19933 * this counter reaches zero, no more additional mblks 19934 * can be added to it. What happens afterwards depends 19935 * on whether or not we are set to chain the Multidata 19936 * messages. If we are to link them together, reset 19937 * max_pld to its original value (tcp_mdt_max_pld) and 19938 * prepare to create a new Multidata message which will 19939 * get linked to md_mp_head. Else, leave it alone and 19940 * let the inner loop break on its own. 19941 */ 19942 if (tcp_mdt_chain && max_pld == 0) 19943 PREP_NEW_MULTIDATA(); 19944 19945 /* adding a payload buffer; re-initialize values */ 19946 if (add_buffer) 19947 PREP_NEW_PBUF(); 19948 19949 /* 19950 * If we don't have a Multidata, either because we just 19951 * (re)entered this outer loop, or after we branched off 19952 * to tcp_send above, setup the Multidata and header 19953 * buffer to be used. 19954 */ 19955 if (md_mp == NULL) { 19956 int md_hbuflen; 19957 uint32_t start, stuff; 19958 19959 /* 19960 * Calculate Multidata header buffer size large enough 19961 * to hold all of the headers that can possibly be 19962 * sent at this moment. We'd rather over-estimate 19963 * the size than running out of space; this is okay 19964 * since this buffer is small anyway. 19965 */ 19966 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19967 19968 /* 19969 * Start and stuff offset for partial hardware 19970 * checksum offload; these are currently for IPv4. 19971 * For full checksum offload, they are set to zero. 19972 */ 19973 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19974 if (af == AF_INET) { 19975 start = IP_SIMPLE_HDR_LENGTH; 19976 stuff = IP_SIMPLE_HDR_LENGTH + 19977 TCP_CHECKSUM_OFFSET; 19978 } else { 19979 start = IPV6_HDR_LEN; 19980 stuff = IPV6_HDR_LEN + 19981 TCP_CHECKSUM_OFFSET; 19982 } 19983 } else { 19984 start = stuff = 0; 19985 } 19986 19987 /* 19988 * Create the header buffer, Multidata, as well as 19989 * any necessary attributes (destination address, 19990 * SAP and hardware checksum offload) that should 19991 * be associated with the Multidata message. 19992 */ 19993 ASSERT(cur_hdr_off == 0); 19994 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19995 ((md_hbuf->b_wptr += md_hbuflen), 19996 (mmd = mmd_alloc(md_hbuf, &md_mp, 19997 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19998 /* fastpath mblk */ 19999 ire->ire_nce->nce_res_mp, 20000 /* hardware checksum enabled */ 20001 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20002 /* hardware checksum offsets */ 20003 start, stuff, 0, 20004 /* hardware checksum flag */ 20005 hwcksum_flags, tcps) != 0)) { 20006 legacy_send: 20007 if (md_mp != NULL) { 20008 /* Unlink message from the chain */ 20009 if (md_mp_head != NULL) { 20010 err = (intptr_t)rmvb(md_mp_head, 20011 md_mp); 20012 /* 20013 * We can't assert that rmvb 20014 * did not return -1, since we 20015 * may get here before linkb 20016 * happens. We do, however, 20017 * check if we just removed the 20018 * only element in the list. 20019 */ 20020 if (err == 0) 20021 md_mp_head = NULL; 20022 } 20023 /* md_hbuf gets freed automatically */ 20024 TCP_STAT(tcps, tcp_mdt_discarded); 20025 freeb(md_mp); 20026 } else { 20027 /* Either allocb or mmd_alloc failed */ 20028 TCP_STAT(tcps, tcp_mdt_allocfail); 20029 if (md_hbuf != NULL) 20030 freeb(md_hbuf); 20031 } 20032 20033 /* send down what we've got so far */ 20034 if (md_mp_head != NULL) { 20035 tcp_multisend_data(tcp, ire, ill, 20036 md_mp_head, obsegs, obbytes, 20037 &rconfirm); 20038 } 20039 legacy_send_no_md: 20040 if (ire != NULL) 20041 IRE_REFRELE(ire); 20042 /* 20043 * Too bad; let the legacy path handle this. 20044 * We specify INT_MAX for the threshold, since 20045 * we gave up with the Multidata processings 20046 * and let the old path have it all. 20047 */ 20048 TCP_STAT(tcps, tcp_mdt_legacy_all); 20049 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20050 tcp_tcp_hdr_len, num_sack_blk, usable, 20051 snxt, tail_unsent, xmit_tail, local_time, 20052 INT_MAX)); 20053 } 20054 20055 /* link to any existing ones, if applicable */ 20056 TCP_STAT(tcps, tcp_mdt_allocd); 20057 if (md_mp_head == NULL) { 20058 md_mp_head = md_mp; 20059 } else if (tcp_mdt_chain) { 20060 TCP_STAT(tcps, tcp_mdt_linked); 20061 linkb(md_mp_head, md_mp); 20062 } 20063 } 20064 20065 ASSERT(md_mp_head != NULL); 20066 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20067 ASSERT(md_mp != NULL && mmd != NULL); 20068 ASSERT(md_hbuf != NULL); 20069 20070 /* 20071 * Packetize the transmittable portion of the data block; 20072 * each data block is essentially added to the Multidata 20073 * as a payload buffer. We also deal with adding more 20074 * than one payload buffers, which happens when the remaining 20075 * packetized portion of the current payload buffer is less 20076 * than MSS, while the next data block in transmit queue 20077 * has enough data to make up for one. This "spillover" 20078 * case essentially creates a split-packet, where portions 20079 * of the packet's payload fragments may span across two 20080 * virtually discontiguous address blocks. 20081 */ 20082 seg_len = mss; 20083 do { 20084 len = seg_len; 20085 20086 ASSERT(len > 0); 20087 ASSERT(max_pld >= 0); 20088 ASSERT(!add_buffer || cur_pld_off == 0); 20089 20090 /* 20091 * First time around for this payload buffer; note 20092 * in the case of a spillover, the following has 20093 * been done prior to adding the split-packet 20094 * descriptor to Multidata, and we don't want to 20095 * repeat the process. 20096 */ 20097 if (add_buffer) { 20098 ASSERT(mmd != NULL); 20099 ASSERT(md_pbuf == NULL); 20100 ASSERT(md_pbuf_nxt == NULL); 20101 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20102 20103 /* 20104 * Have we reached the limit? We'd get to 20105 * this case when we're not chaining the 20106 * Multidata messages together, and since 20107 * we're done, terminate this loop. 20108 */ 20109 if (max_pld == 0) 20110 break; /* done */ 20111 20112 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20113 TCP_STAT(tcps, tcp_mdt_allocfail); 20114 goto legacy_send; /* out_of_mem */ 20115 } 20116 20117 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20118 zc_cap != NULL) { 20119 if (!ip_md_zcopy_attr(mmd, NULL, 20120 zc_cap->ill_zerocopy_flags)) { 20121 freeb(md_pbuf); 20122 TCP_STAT(tcps, 20123 tcp_mdt_allocfail); 20124 /* out_of_mem */ 20125 goto legacy_send; 20126 } 20127 zcopy = B_TRUE; 20128 } 20129 20130 md_pbuf->b_rptr += base_pld_off; 20131 20132 /* 20133 * Add a payload buffer to the Multidata; this 20134 * operation must not fail, or otherwise our 20135 * logic in this routine is broken. There 20136 * is no memory allocation done by the 20137 * routine, so any returned failure simply 20138 * tells us that we've done something wrong. 20139 * 20140 * A failure tells us that either we're adding 20141 * the same payload buffer more than once, or 20142 * we're trying to add more buffers than 20143 * allowed (max_pld calculation is wrong). 20144 * None of the above cases should happen, and 20145 * we panic because either there's horrible 20146 * heap corruption, and/or programming mistake. 20147 */ 20148 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20149 if (pbuf_idx < 0) { 20150 cmn_err(CE_PANIC, "tcp_multisend: " 20151 "payload buffer logic error " 20152 "detected for tcp %p mmd %p " 20153 "pbuf %p (%d)\n", 20154 (void *)tcp, (void *)mmd, 20155 (void *)md_pbuf, pbuf_idx); 20156 } 20157 20158 ASSERT(max_pld > 0); 20159 --max_pld; 20160 add_buffer = B_FALSE; 20161 } 20162 20163 ASSERT(md_mp_head != NULL); 20164 ASSERT(md_pbuf != NULL); 20165 ASSERT(md_pbuf_nxt == NULL); 20166 ASSERT(pbuf_idx != -1); 20167 ASSERT(pbuf_idx_nxt == -1); 20168 ASSERT(*usable > 0); 20169 20170 /* 20171 * We spillover to the next payload buffer only 20172 * if all of the following is true: 20173 * 20174 * 1. There is not enough data on the current 20175 * payload buffer to make up `len', 20176 * 2. We are allowed to send `len', 20177 * 3. The next payload buffer length is large 20178 * enough to accomodate `spill'. 20179 */ 20180 if ((spill = len - *tail_unsent) > 0 && 20181 *usable >= len && 20182 MBLKL((*xmit_tail)->b_cont) >= spill && 20183 max_pld > 0) { 20184 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20185 if (md_pbuf_nxt == NULL) { 20186 TCP_STAT(tcps, tcp_mdt_allocfail); 20187 goto legacy_send; /* out_of_mem */ 20188 } 20189 20190 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20191 zc_cap != NULL) { 20192 if (!ip_md_zcopy_attr(mmd, NULL, 20193 zc_cap->ill_zerocopy_flags)) { 20194 freeb(md_pbuf_nxt); 20195 TCP_STAT(tcps, 20196 tcp_mdt_allocfail); 20197 /* out_of_mem */ 20198 goto legacy_send; 20199 } 20200 zcopy = B_TRUE; 20201 } 20202 20203 /* 20204 * See comments above on the first call to 20205 * mmd_addpldbuf for explanation on the panic. 20206 */ 20207 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20208 if (pbuf_idx_nxt < 0) { 20209 panic("tcp_multisend: " 20210 "next payload buffer logic error " 20211 "detected for tcp %p mmd %p " 20212 "pbuf %p (%d)\n", 20213 (void *)tcp, (void *)mmd, 20214 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20215 } 20216 20217 ASSERT(max_pld > 0); 20218 --max_pld; 20219 } else if (spill > 0) { 20220 /* 20221 * If there's a spillover, but the following 20222 * xmit_tail couldn't give us enough octets 20223 * to reach "len", then stop the current 20224 * Multidata creation and let the legacy 20225 * tcp_send() path take over. We don't want 20226 * to send the tiny segment as part of this 20227 * Multidata for performance reasons; instead, 20228 * we let the legacy path deal with grouping 20229 * it with the subsequent small mblks. 20230 */ 20231 if (*usable >= len && 20232 MBLKL((*xmit_tail)->b_cont) < spill) { 20233 max_pld = 0; 20234 break; /* done */ 20235 } 20236 20237 /* 20238 * We can't spillover, and we are near 20239 * the end of the current payload buffer, 20240 * so send what's left. 20241 */ 20242 ASSERT(*tail_unsent > 0); 20243 len = *tail_unsent; 20244 } 20245 20246 /* tail_unsent is negated if there is a spillover */ 20247 *tail_unsent -= len; 20248 *usable -= len; 20249 ASSERT(*usable >= 0); 20250 20251 if (*usable < mss) 20252 seg_len = *usable; 20253 /* 20254 * Sender SWS avoidance; see comments in tcp_send(); 20255 * everything else is the same, except that we only 20256 * do this here if there is no more data to be sent 20257 * following the current xmit_tail. We don't check 20258 * for 1-byte urgent data because we shouldn't get 20259 * here if TCP_URG_VALID is set. 20260 */ 20261 if (*usable > 0 && *usable < mss && 20262 ((md_pbuf_nxt == NULL && 20263 (*xmit_tail)->b_cont == NULL) || 20264 (md_pbuf_nxt != NULL && 20265 (*xmit_tail)->b_cont->b_cont == NULL)) && 20266 seg_len < (tcp->tcp_max_swnd >> 1) && 20267 (tcp->tcp_unsent - 20268 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20269 !tcp->tcp_zero_win_probe) { 20270 if ((*snxt + len) == tcp->tcp_snxt && 20271 (*snxt + len) == tcp->tcp_suna) { 20272 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20273 } 20274 done = B_TRUE; 20275 } 20276 20277 /* 20278 * Prime pump for IP's checksumming on our behalf; 20279 * include the adjustment for a source route if any. 20280 * Do this only for software/partial hardware checksum 20281 * offload, as this field gets zeroed out later for 20282 * the full hardware checksum offload case. 20283 */ 20284 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20285 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20286 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20287 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20288 } 20289 20290 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20291 *snxt += len; 20292 20293 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20294 /* 20295 * We set the PUSH bit only if TCP has no more buffered 20296 * data to be transmitted (or if sender SWS avoidance 20297 * takes place), as opposed to setting it for every 20298 * last packet in the burst. 20299 */ 20300 if (done || 20301 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20302 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20303 20304 /* 20305 * Set FIN bit if this is our last segment; snxt 20306 * already includes its length, and it will not 20307 * be adjusted after this point. 20308 */ 20309 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20310 *snxt == tcp->tcp_fss) { 20311 if (!tcp->tcp_fin_acked) { 20312 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20313 BUMP_MIB(&tcps->tcps_mib, 20314 tcpOutControl); 20315 } 20316 if (!tcp->tcp_fin_sent) { 20317 tcp->tcp_fin_sent = B_TRUE; 20318 /* 20319 * tcp state must be ESTABLISHED 20320 * in order for us to get here in 20321 * the first place. 20322 */ 20323 tcp->tcp_state = TCPS_FIN_WAIT_1; 20324 20325 /* 20326 * Upon returning from this routine, 20327 * tcp_wput_data() will set tcp_snxt 20328 * to be equal to snxt + tcp_fin_sent. 20329 * This is essentially the same as 20330 * setting it to tcp_fss + 1. 20331 */ 20332 } 20333 } 20334 20335 tcp->tcp_last_sent_len = (ushort_t)len; 20336 20337 len += tcp_hdr_len; 20338 if (tcp->tcp_ipversion == IPV4_VERSION) 20339 tcp->tcp_ipha->ipha_length = htons(len); 20340 else 20341 tcp->tcp_ip6h->ip6_plen = htons(len - 20342 ((char *)&tcp->tcp_ip6h[1] - 20343 tcp->tcp_iphc)); 20344 20345 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20346 20347 /* setup header fragment */ 20348 PDESC_HDR_ADD(pkt_info, 20349 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20350 tcp->tcp_mdt_hdr_head, /* head room */ 20351 tcp_hdr_len, /* len */ 20352 tcp->tcp_mdt_hdr_tail); /* tail room */ 20353 20354 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20355 hdr_frag_sz); 20356 ASSERT(MBLKIN(md_hbuf, 20357 (pkt_info->hdr_base - md_hbuf->b_rptr), 20358 PDESC_HDRSIZE(pkt_info))); 20359 20360 /* setup first payload fragment */ 20361 PDESC_PLD_INIT(pkt_info); 20362 PDESC_PLD_SPAN_ADD(pkt_info, 20363 pbuf_idx, /* index */ 20364 md_pbuf->b_rptr + cur_pld_off, /* start */ 20365 tcp->tcp_last_sent_len); /* len */ 20366 20367 /* create a split-packet in case of a spillover */ 20368 if (md_pbuf_nxt != NULL) { 20369 ASSERT(spill > 0); 20370 ASSERT(pbuf_idx_nxt > pbuf_idx); 20371 ASSERT(!add_buffer); 20372 20373 md_pbuf = md_pbuf_nxt; 20374 md_pbuf_nxt = NULL; 20375 pbuf_idx = pbuf_idx_nxt; 20376 pbuf_idx_nxt = -1; 20377 cur_pld_off = spill; 20378 20379 /* trim out first payload fragment */ 20380 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20381 20382 /* setup second payload fragment */ 20383 PDESC_PLD_SPAN_ADD(pkt_info, 20384 pbuf_idx, /* index */ 20385 md_pbuf->b_rptr, /* start */ 20386 spill); /* len */ 20387 20388 if ((*xmit_tail)->b_next == NULL) { 20389 /* 20390 * Store the lbolt used for RTT 20391 * estimation. We can only record one 20392 * timestamp per mblk so we do it when 20393 * we reach the end of the payload 20394 * buffer. Also we only take a new 20395 * timestamp sample when the previous 20396 * timed data from the same mblk has 20397 * been ack'ed. 20398 */ 20399 (*xmit_tail)->b_prev = local_time; 20400 (*xmit_tail)->b_next = 20401 (mblk_t *)(uintptr_t)first_snxt; 20402 } 20403 20404 first_snxt = *snxt - spill; 20405 20406 /* 20407 * Advance xmit_tail; usable could be 0 by 20408 * the time we got here, but we made sure 20409 * above that we would only spillover to 20410 * the next data block if usable includes 20411 * the spilled-over amount prior to the 20412 * subtraction. Therefore, we are sure 20413 * that xmit_tail->b_cont can't be NULL. 20414 */ 20415 ASSERT((*xmit_tail)->b_cont != NULL); 20416 *xmit_tail = (*xmit_tail)->b_cont; 20417 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20418 (uintptr_t)INT_MAX); 20419 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20420 } else { 20421 cur_pld_off += tcp->tcp_last_sent_len; 20422 } 20423 20424 /* 20425 * Fill in the header using the template header, and 20426 * add options such as time-stamp, ECN and/or SACK, 20427 * as needed. 20428 */ 20429 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20430 (clock_t)local_time, num_sack_blk); 20431 20432 /* take care of some IP header businesses */ 20433 if (af == AF_INET) { 20434 ipha = (ipha_t *)pkt_info->hdr_rptr; 20435 20436 ASSERT(OK_32PTR((uchar_t *)ipha)); 20437 ASSERT(PDESC_HDRL(pkt_info) >= 20438 IP_SIMPLE_HDR_LENGTH); 20439 ASSERT(ipha->ipha_version_and_hdr_length == 20440 IP_SIMPLE_HDR_VERSION); 20441 20442 /* 20443 * Assign ident value for current packet; see 20444 * related comments in ip_wput_ire() about the 20445 * contract private interface with clustering 20446 * group. 20447 */ 20448 clusterwide = B_FALSE; 20449 if (cl_inet_ipident != NULL) { 20450 ASSERT(cl_inet_isclusterwide != NULL); 20451 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20452 AF_INET, 20453 (uint8_t *)(uintptr_t)src)) { 20454 ipha->ipha_ident = 20455 (*cl_inet_ipident) 20456 (IPPROTO_IP, AF_INET, 20457 (uint8_t *)(uintptr_t)src, 20458 (uint8_t *)(uintptr_t)dst); 20459 clusterwide = B_TRUE; 20460 } 20461 } 20462 20463 if (!clusterwide) { 20464 ipha->ipha_ident = (uint16_t) 20465 atomic_add_32_nv( 20466 &ire->ire_ident, 1); 20467 } 20468 #ifndef _BIG_ENDIAN 20469 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20470 (ipha->ipha_ident >> 8); 20471 #endif 20472 } else { 20473 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20474 20475 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20476 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20477 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20478 ASSERT(PDESC_HDRL(pkt_info) >= 20479 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20480 TCP_CHECKSUM_SIZE)); 20481 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20482 20483 if (tcp->tcp_ip_forward_progress) { 20484 rconfirm = B_TRUE; 20485 tcp->tcp_ip_forward_progress = B_FALSE; 20486 } 20487 } 20488 20489 /* at least one payload span, and at most two */ 20490 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20491 20492 /* add the packet descriptor to Multidata */ 20493 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20494 KM_NOSLEEP)) == NULL) { 20495 /* 20496 * Any failure other than ENOMEM indicates 20497 * that we have passed in invalid pkt_info 20498 * or parameters to mmd_addpdesc, which must 20499 * not happen. 20500 * 20501 * EINVAL is a result of failure on boundary 20502 * checks against the pkt_info contents. It 20503 * should not happen, and we panic because 20504 * either there's horrible heap corruption, 20505 * and/or programming mistake. 20506 */ 20507 if (err != ENOMEM) { 20508 cmn_err(CE_PANIC, "tcp_multisend: " 20509 "pdesc logic error detected for " 20510 "tcp %p mmd %p pinfo %p (%d)\n", 20511 (void *)tcp, (void *)mmd, 20512 (void *)pkt_info, err); 20513 } 20514 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20515 goto legacy_send; /* out_of_mem */ 20516 } 20517 ASSERT(pkt != NULL); 20518 20519 /* calculate IP header and TCP checksums */ 20520 if (af == AF_INET) { 20521 /* calculate pseudo-header checksum */ 20522 cksum = (dst >> 16) + (dst & 0xFFFF) + 20523 (src >> 16) + (src & 0xFFFF); 20524 20525 /* offset for TCP header checksum */ 20526 up = IPH_TCPH_CHECKSUMP(ipha, 20527 IP_SIMPLE_HDR_LENGTH); 20528 } else { 20529 up = (uint16_t *)&ip6h->ip6_src; 20530 20531 /* calculate pseudo-header checksum */ 20532 cksum = up[0] + up[1] + up[2] + up[3] + 20533 up[4] + up[5] + up[6] + up[7] + 20534 up[8] + up[9] + up[10] + up[11] + 20535 up[12] + up[13] + up[14] + up[15]; 20536 20537 /* Fold the initial sum */ 20538 cksum = (cksum & 0xffff) + (cksum >> 16); 20539 20540 up = (uint16_t *)(((uchar_t *)ip6h) + 20541 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20542 } 20543 20544 if (hwcksum_flags & HCK_FULLCKSUM) { 20545 /* clear checksum field for hardware */ 20546 *up = 0; 20547 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20548 uint32_t sum; 20549 20550 /* pseudo-header checksumming */ 20551 sum = *up + cksum + IP_TCP_CSUM_COMP; 20552 sum = (sum & 0xFFFF) + (sum >> 16); 20553 *up = (sum & 0xFFFF) + (sum >> 16); 20554 } else { 20555 /* software checksumming */ 20556 TCP_STAT(tcps, tcp_out_sw_cksum); 20557 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20558 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20559 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20560 cksum + IP_TCP_CSUM_COMP); 20561 if (*up == 0) 20562 *up = 0xFFFF; 20563 } 20564 20565 /* IPv4 header checksum */ 20566 if (af == AF_INET) { 20567 ipha->ipha_fragment_offset_and_flags |= 20568 (uint32_t)htons(ire->ire_frag_flag); 20569 20570 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20571 ipha->ipha_hdr_checksum = 0; 20572 } else { 20573 IP_HDR_CKSUM(ipha, cksum, 20574 ((uint32_t *)ipha)[0], 20575 ((uint16_t *)ipha)[4]); 20576 } 20577 } 20578 20579 if (af == AF_INET && 20580 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20581 af == AF_INET6 && 20582 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20583 /* build header(IP/TCP) mblk for this segment */ 20584 if ((mp = dupb(md_hbuf)) == NULL) 20585 goto legacy_send; 20586 20587 mp->b_rptr = pkt_info->hdr_rptr; 20588 mp->b_wptr = pkt_info->hdr_wptr; 20589 20590 /* build payload mblk for this segment */ 20591 if ((mp1 = dupb(*xmit_tail)) == NULL) { 20592 freemsg(mp); 20593 goto legacy_send; 20594 } 20595 mp1->b_wptr = md_pbuf->b_rptr + cur_pld_off; 20596 mp1->b_rptr = mp1->b_wptr - 20597 tcp->tcp_last_sent_len; 20598 linkb(mp, mp1); 20599 20600 pld_start = mp1->b_rptr; 20601 20602 if (af == AF_INET) { 20603 DTRACE_PROBE4( 20604 ip4__physical__out__start, 20605 ill_t *, NULL, 20606 ill_t *, ill, 20607 ipha_t *, ipha, 20608 mblk_t *, mp); 20609 FW_HOOKS( 20610 ipst->ips_ip4_physical_out_event, 20611 ipst->ips_ipv4firewall_physical_out, 20612 NULL, ill, ipha, mp, mp, ipst); 20613 DTRACE_PROBE1( 20614 ip4__physical__out__end, 20615 mblk_t *, mp); 20616 } else { 20617 DTRACE_PROBE4( 20618 ip6__physical__out_start, 20619 ill_t *, NULL, 20620 ill_t *, ill, 20621 ip6_t *, ip6h, 20622 mblk_t *, mp); 20623 FW_HOOKS6( 20624 ipst->ips_ip6_physical_out_event, 20625 ipst->ips_ipv6firewall_physical_out, 20626 NULL, ill, ip6h, mp, mp, ipst); 20627 DTRACE_PROBE1( 20628 ip6__physical__out__end, 20629 mblk_t *, mp); 20630 } 20631 20632 if (buf_trunked && mp != NULL) { 20633 /* 20634 * Need to pass it to normal path. 20635 */ 20636 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20637 } else if (mp == NULL || 20638 mp->b_rptr != pkt_info->hdr_rptr || 20639 mp->b_wptr != pkt_info->hdr_wptr || 20640 (mp1 = mp->b_cont) == NULL || 20641 mp1->b_rptr != pld_start || 20642 mp1->b_wptr != pld_start + 20643 tcp->tcp_last_sent_len || 20644 mp1->b_cont != NULL) { 20645 /* 20646 * Need to pass all packets of this 20647 * buffer to normal path, either when 20648 * packet is blocked, or when boundary 20649 * of header buffer or payload buffer 20650 * has been changed by FW_HOOKS[6]. 20651 */ 20652 buf_trunked = B_TRUE; 20653 if (md_mp_head != NULL) { 20654 err = (intptr_t)rmvb(md_mp_head, 20655 md_mp); 20656 if (err == 0) 20657 md_mp_head = NULL; 20658 } 20659 20660 /* send down what we've got so far */ 20661 if (md_mp_head != NULL) { 20662 tcp_multisend_data(tcp, ire, 20663 ill, md_mp_head, obsegs, 20664 obbytes, &rconfirm); 20665 } 20666 md_mp_head = NULL; 20667 20668 if (mp != NULL) 20669 CALL_IP_WPUT(tcp->tcp_connp, 20670 q, mp); 20671 20672 mp1 = fw_mp_head; 20673 do { 20674 mp = mp1; 20675 mp1 = mp1->b_next; 20676 mp->b_next = NULL; 20677 mp->b_prev = NULL; 20678 CALL_IP_WPUT(tcp->tcp_connp, 20679 q, mp); 20680 } while (mp1 != NULL); 20681 20682 fw_mp_head = NULL; 20683 } else { 20684 if (fw_mp_head == NULL) 20685 fw_mp_head = mp; 20686 else 20687 fw_mp_head->b_prev->b_next = mp; 20688 fw_mp_head->b_prev = mp; 20689 } 20690 } 20691 20692 /* advance header offset */ 20693 cur_hdr_off += hdr_frag_sz; 20694 20695 obbytes += tcp->tcp_last_sent_len; 20696 ++obsegs; 20697 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20698 *tail_unsent > 0); 20699 20700 if ((*xmit_tail)->b_next == NULL) { 20701 /* 20702 * Store the lbolt used for RTT estimation. We can only 20703 * record one timestamp per mblk so we do it when we 20704 * reach the end of the payload buffer. Also we only 20705 * take a new timestamp sample when the previous timed 20706 * data from the same mblk has been ack'ed. 20707 */ 20708 (*xmit_tail)->b_prev = local_time; 20709 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20710 } 20711 20712 ASSERT(*tail_unsent >= 0); 20713 if (*tail_unsent > 0) { 20714 /* 20715 * We got here because we broke out of the above 20716 * loop due to of one of the following cases: 20717 * 20718 * 1. len < adjusted MSS (i.e. small), 20719 * 2. Sender SWS avoidance, 20720 * 3. max_pld is zero. 20721 * 20722 * We are done for this Multidata, so trim our 20723 * last payload buffer (if any) accordingly. 20724 */ 20725 if (md_pbuf != NULL) 20726 md_pbuf->b_wptr -= *tail_unsent; 20727 } else if (*usable > 0) { 20728 *xmit_tail = (*xmit_tail)->b_cont; 20729 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20730 (uintptr_t)INT_MAX); 20731 *tail_unsent = (int)MBLKL(*xmit_tail); 20732 add_buffer = B_TRUE; 20733 } 20734 20735 while (fw_mp_head) { 20736 mp = fw_mp_head; 20737 fw_mp_head = fw_mp_head->b_next; 20738 mp->b_prev = mp->b_next = NULL; 20739 freemsg(mp); 20740 } 20741 if (buf_trunked) { 20742 TCP_STAT(tcps, tcp_mdt_discarded); 20743 freeb(md_mp); 20744 buf_trunked = B_FALSE; 20745 } 20746 } while (!done && *usable > 0 && num_burst_seg > 0 && 20747 (tcp_mdt_chain || max_pld > 0)); 20748 20749 if (md_mp_head != NULL) { 20750 /* send everything down */ 20751 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20752 &rconfirm); 20753 } 20754 20755 #undef PREP_NEW_MULTIDATA 20756 #undef PREP_NEW_PBUF 20757 #undef IPVER 20758 20759 IRE_REFRELE(ire); 20760 return (0); 20761 } 20762 20763 /* 20764 * A wrapper function for sending one or more Multidata messages down to 20765 * the module below ip; this routine does not release the reference of the 20766 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20767 */ 20768 static void 20769 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20770 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20771 { 20772 uint64_t delta; 20773 nce_t *nce; 20774 tcp_stack_t *tcps = tcp->tcp_tcps; 20775 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20776 20777 ASSERT(ire != NULL && ill != NULL); 20778 ASSERT(ire->ire_stq != NULL); 20779 ASSERT(md_mp_head != NULL); 20780 ASSERT(rconfirm != NULL); 20781 20782 /* adjust MIBs and IRE timestamp */ 20783 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 20784 tcp->tcp_obsegs += obsegs; 20785 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20786 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20787 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20788 20789 if (tcp->tcp_ipversion == IPV4_VERSION) { 20790 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20791 } else { 20792 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20793 } 20794 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20795 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20796 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20797 20798 ire->ire_ob_pkt_count += obsegs; 20799 if (ire->ire_ipif != NULL) 20800 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20801 ire->ire_last_used_time = lbolt; 20802 20803 /* send it down */ 20804 putnext(ire->ire_stq, md_mp_head); 20805 20806 /* we're done for TCP/IPv4 */ 20807 if (tcp->tcp_ipversion == IPV4_VERSION) 20808 return; 20809 20810 nce = ire->ire_nce; 20811 20812 ASSERT(nce != NULL); 20813 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20814 ASSERT(nce->nce_state != ND_INCOMPLETE); 20815 20816 /* reachability confirmation? */ 20817 if (*rconfirm) { 20818 nce->nce_last = TICK_TO_MSEC(lbolt64); 20819 if (nce->nce_state != ND_REACHABLE) { 20820 mutex_enter(&nce->nce_lock); 20821 nce->nce_state = ND_REACHABLE; 20822 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20823 mutex_exit(&nce->nce_lock); 20824 (void) untimeout(nce->nce_timeout_id); 20825 if (ip_debug > 2) { 20826 /* ip1dbg */ 20827 pr_addr_dbg("tcp_multisend_data: state " 20828 "for %s changed to REACHABLE\n", 20829 AF_INET6, &ire->ire_addr_v6); 20830 } 20831 } 20832 /* reset transport reachability confirmation */ 20833 *rconfirm = B_FALSE; 20834 } 20835 20836 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20837 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20838 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20839 20840 if (delta > (uint64_t)ill->ill_reachable_time) { 20841 mutex_enter(&nce->nce_lock); 20842 switch (nce->nce_state) { 20843 case ND_REACHABLE: 20844 case ND_STALE: 20845 /* 20846 * ND_REACHABLE is identical to ND_STALE in this 20847 * specific case. If reachable time has expired for 20848 * this neighbor (delta is greater than reachable 20849 * time), conceptually, the neighbor cache is no 20850 * longer in REACHABLE state, but already in STALE 20851 * state. So the correct transition here is to 20852 * ND_DELAY. 20853 */ 20854 nce->nce_state = ND_DELAY; 20855 mutex_exit(&nce->nce_lock); 20856 NDP_RESTART_TIMER(nce, 20857 ipst->ips_delay_first_probe_time); 20858 if (ip_debug > 3) { 20859 /* ip2dbg */ 20860 pr_addr_dbg("tcp_multisend_data: state " 20861 "for %s changed to DELAY\n", 20862 AF_INET6, &ire->ire_addr_v6); 20863 } 20864 break; 20865 case ND_DELAY: 20866 case ND_PROBE: 20867 mutex_exit(&nce->nce_lock); 20868 /* Timers have already started */ 20869 break; 20870 case ND_UNREACHABLE: 20871 /* 20872 * ndp timer has detected that this nce is 20873 * unreachable and initiated deleting this nce 20874 * and all its associated IREs. This is a race 20875 * where we found the ire before it was deleted 20876 * and have just sent out a packet using this 20877 * unreachable nce. 20878 */ 20879 mutex_exit(&nce->nce_lock); 20880 break; 20881 default: 20882 ASSERT(0); 20883 } 20884 } 20885 } 20886 20887 /* 20888 * Derived from tcp_send_data(). 20889 */ 20890 static void 20891 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20892 int num_lso_seg) 20893 { 20894 ipha_t *ipha; 20895 mblk_t *ire_fp_mp; 20896 uint_t ire_fp_mp_len; 20897 uint32_t hcksum_txflags = 0; 20898 ipaddr_t src; 20899 ipaddr_t dst; 20900 uint32_t cksum; 20901 uint16_t *up; 20902 tcp_stack_t *tcps = tcp->tcp_tcps; 20903 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20904 20905 ASSERT(DB_TYPE(mp) == M_DATA); 20906 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20907 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20908 ASSERT(tcp->tcp_connp != NULL); 20909 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20910 20911 ipha = (ipha_t *)mp->b_rptr; 20912 src = ipha->ipha_src; 20913 dst = ipha->ipha_dst; 20914 20915 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20916 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20917 num_lso_seg); 20918 #ifndef _BIG_ENDIAN 20919 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20920 #endif 20921 if (tcp->tcp_snd_zcopy_aware) { 20922 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20923 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20924 mp = tcp_zcopy_disable(tcp, mp); 20925 } 20926 20927 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20928 ASSERT(ill->ill_hcksum_capab != NULL); 20929 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20930 } 20931 20932 /* 20933 * Since the TCP checksum should be recalculated by h/w, we can just 20934 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20935 * pseudo-header checksum for HCK_PARTIALCKSUM. 20936 * The partial pseudo-header excludes TCP length, that was calculated 20937 * in tcp_send(), so to zero *up before further processing. 20938 */ 20939 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20940 20941 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20942 *up = 0; 20943 20944 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20945 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20946 20947 /* 20948 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 20949 */ 20950 DB_LSOFLAGS(mp) |= HW_LSO; 20951 DB_LSOMSS(mp) = mss; 20952 20953 ipha->ipha_fragment_offset_and_flags |= 20954 (uint32_t)htons(ire->ire_frag_flag); 20955 20956 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20957 ire_fp_mp_len = MBLKL(ire_fp_mp); 20958 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20959 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20960 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20961 20962 UPDATE_OB_PKT_COUNT(ire); 20963 ire->ire_last_used_time = lbolt; 20964 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20965 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20966 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20967 ntohs(ipha->ipha_length)); 20968 20969 if (ILL_DLS_CAPABLE(ill)) { 20970 /* 20971 * Send the packet directly to DLD, where it may be queued 20972 * depending on the availability of transmit resources at 20973 * the media layer. 20974 */ 20975 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 20976 } else { 20977 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 20978 DTRACE_PROBE4(ip4__physical__out__start, 20979 ill_t *, NULL, ill_t *, out_ill, 20980 ipha_t *, ipha, mblk_t *, mp); 20981 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20982 ipst->ips_ipv4firewall_physical_out, 20983 NULL, out_ill, ipha, mp, mp, ipst); 20984 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20985 if (mp != NULL) 20986 putnext(ire->ire_stq, mp); 20987 } 20988 } 20989 20990 /* 20991 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20992 * scheme, and returns one of the following: 20993 * 20994 * -1 = failed allocation. 20995 * 0 = success; burst count reached, or usable send window is too small, 20996 * and that we'd rather wait until later before sending again. 20997 * 1 = success; we are called from tcp_multisend(), and both usable send 20998 * window and tail_unsent are greater than the MDT threshold, and thus 20999 * Multidata Transmit should be used instead. 21000 */ 21001 static int 21002 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21003 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21004 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21005 const int mdt_thres) 21006 { 21007 int num_burst_seg = tcp->tcp_snd_burst; 21008 ire_t *ire = NULL; 21009 ill_t *ill = NULL; 21010 mblk_t *ire_fp_mp = NULL; 21011 uint_t ire_fp_mp_len = 0; 21012 int num_lso_seg = 1; 21013 uint_t lso_usable; 21014 boolean_t do_lso_send = B_FALSE; 21015 tcp_stack_t *tcps = tcp->tcp_tcps; 21016 21017 /* 21018 * Check LSO capability before any further work. And the similar check 21019 * need to be done in for(;;) loop. 21020 * LSO will be deployed when therer is more than one mss of available 21021 * data and a burst transmission is allowed. 21022 */ 21023 if (tcp->tcp_lso && 21024 (tcp->tcp_valid_bits == 0 || 21025 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21026 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21027 /* 21028 * Try to find usable IRE/ILL and do basic check to the ILL. 21029 */ 21030 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21031 /* 21032 * Enable LSO with this transmission. 21033 * Since IRE has been hold in 21034 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21035 * should be called before return. 21036 */ 21037 do_lso_send = B_TRUE; 21038 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21039 ire_fp_mp_len = MBLKL(ire_fp_mp); 21040 /* Round up to multiple of 4 */ 21041 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21042 } else { 21043 do_lso_send = B_FALSE; 21044 ill = NULL; 21045 } 21046 } 21047 21048 for (;;) { 21049 struct datab *db; 21050 tcph_t *tcph; 21051 uint32_t sum; 21052 mblk_t *mp, *mp1; 21053 uchar_t *rptr; 21054 int len; 21055 21056 /* 21057 * If we're called by tcp_multisend(), and the amount of 21058 * sendable data as well as the size of current xmit_tail 21059 * is beyond the MDT threshold, return to the caller and 21060 * let the large data transmit be done using MDT. 21061 */ 21062 if (*usable > 0 && *usable > mdt_thres && 21063 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21064 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21065 ASSERT(tcp->tcp_mdt); 21066 return (1); /* success; do large send */ 21067 } 21068 21069 if (num_burst_seg == 0) 21070 break; /* success; burst count reached */ 21071 21072 /* 21073 * Calculate the maximum payload length we can send in *one* 21074 * time. 21075 */ 21076 if (do_lso_send) { 21077 /* 21078 * Check whether need to do LSO any more. 21079 */ 21080 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21081 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21082 lso_usable = MIN(lso_usable, 21083 num_burst_seg * mss); 21084 21085 num_lso_seg = lso_usable / mss; 21086 if (lso_usable % mss) { 21087 num_lso_seg++; 21088 tcp->tcp_last_sent_len = (ushort_t) 21089 (lso_usable % mss); 21090 } else { 21091 tcp->tcp_last_sent_len = (ushort_t)mss; 21092 } 21093 } else { 21094 do_lso_send = B_FALSE; 21095 num_lso_seg = 1; 21096 lso_usable = mss; 21097 } 21098 } 21099 21100 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21101 21102 /* 21103 * Adjust num_burst_seg here. 21104 */ 21105 num_burst_seg -= num_lso_seg; 21106 21107 len = mss; 21108 if (len > *usable) { 21109 ASSERT(do_lso_send == B_FALSE); 21110 21111 len = *usable; 21112 if (len <= 0) { 21113 /* Terminate the loop */ 21114 break; /* success; too small */ 21115 } 21116 /* 21117 * Sender silly-window avoidance. 21118 * Ignore this if we are going to send a 21119 * zero window probe out. 21120 * 21121 * TODO: force data into microscopic window? 21122 * ==> (!pushed || (unsent > usable)) 21123 */ 21124 if (len < (tcp->tcp_max_swnd >> 1) && 21125 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21126 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21127 len == 1) && (! tcp->tcp_zero_win_probe)) { 21128 /* 21129 * If the retransmit timer is not running 21130 * we start it so that we will retransmit 21131 * in the case when the the receiver has 21132 * decremented the window. 21133 */ 21134 if (*snxt == tcp->tcp_snxt && 21135 *snxt == tcp->tcp_suna) { 21136 /* 21137 * We are not supposed to send 21138 * anything. So let's wait a little 21139 * bit longer before breaking SWS 21140 * avoidance. 21141 * 21142 * What should the value be? 21143 * Suggestion: MAX(init rexmit time, 21144 * tcp->tcp_rto) 21145 */ 21146 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21147 } 21148 break; /* success; too small */ 21149 } 21150 } 21151 21152 tcph = tcp->tcp_tcph; 21153 21154 /* 21155 * The reason to adjust len here is that we need to set flags 21156 * and calculate checksum. 21157 */ 21158 if (do_lso_send) 21159 len = lso_usable; 21160 21161 *usable -= len; /* Approximate - can be adjusted later */ 21162 if (*usable > 0) 21163 tcph->th_flags[0] = TH_ACK; 21164 else 21165 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21166 21167 /* 21168 * Prime pump for IP's checksumming on our behalf 21169 * Include the adjustment for a source route if any. 21170 */ 21171 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21172 sum = (sum >> 16) + (sum & 0xFFFF); 21173 U16_TO_ABE16(sum, tcph->th_sum); 21174 21175 U32_TO_ABE32(*snxt, tcph->th_seq); 21176 21177 /* 21178 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21179 * set. For the case when TCP_FSS_VALID is the only valid 21180 * bit (normal active close), branch off only when we think 21181 * that the FIN flag needs to be set. Note for this case, 21182 * that (snxt + len) may not reflect the actual seg_len, 21183 * as len may be further reduced in tcp_xmit_mp(). If len 21184 * gets modified, we will end up here again. 21185 */ 21186 if (tcp->tcp_valid_bits != 0 && 21187 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21188 ((*snxt + len) == tcp->tcp_fss))) { 21189 uchar_t *prev_rptr; 21190 uint32_t prev_snxt = tcp->tcp_snxt; 21191 21192 if (*tail_unsent == 0) { 21193 ASSERT((*xmit_tail)->b_cont != NULL); 21194 *xmit_tail = (*xmit_tail)->b_cont; 21195 prev_rptr = (*xmit_tail)->b_rptr; 21196 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21197 (*xmit_tail)->b_rptr); 21198 } else { 21199 prev_rptr = (*xmit_tail)->b_rptr; 21200 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21201 *tail_unsent; 21202 } 21203 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21204 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21205 /* Restore tcp_snxt so we get amount sent right. */ 21206 tcp->tcp_snxt = prev_snxt; 21207 if (prev_rptr == (*xmit_tail)->b_rptr) { 21208 /* 21209 * If the previous timestamp is still in use, 21210 * don't stomp on it. 21211 */ 21212 if ((*xmit_tail)->b_next == NULL) { 21213 (*xmit_tail)->b_prev = local_time; 21214 (*xmit_tail)->b_next = 21215 (mblk_t *)(uintptr_t)(*snxt); 21216 } 21217 } else 21218 (*xmit_tail)->b_rptr = prev_rptr; 21219 21220 if (mp == NULL) { 21221 if (ire != NULL) 21222 IRE_REFRELE(ire); 21223 return (-1); 21224 } 21225 mp1 = mp->b_cont; 21226 21227 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21228 tcp->tcp_last_sent_len = (ushort_t)len; 21229 while (mp1->b_cont) { 21230 *xmit_tail = (*xmit_tail)->b_cont; 21231 (*xmit_tail)->b_prev = local_time; 21232 (*xmit_tail)->b_next = 21233 (mblk_t *)(uintptr_t)(*snxt); 21234 mp1 = mp1->b_cont; 21235 } 21236 *snxt += len; 21237 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21238 BUMP_LOCAL(tcp->tcp_obsegs); 21239 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21240 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21241 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21242 tcp_send_data(tcp, q, mp); 21243 continue; 21244 } 21245 21246 *snxt += len; /* Adjust later if we don't send all of len */ 21247 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21248 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21249 21250 if (*tail_unsent) { 21251 /* Are the bytes above us in flight? */ 21252 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21253 if (rptr != (*xmit_tail)->b_rptr) { 21254 *tail_unsent -= len; 21255 if (len <= mss) /* LSO is unusable */ 21256 tcp->tcp_last_sent_len = (ushort_t)len; 21257 len += tcp_hdr_len; 21258 if (tcp->tcp_ipversion == IPV4_VERSION) 21259 tcp->tcp_ipha->ipha_length = htons(len); 21260 else 21261 tcp->tcp_ip6h->ip6_plen = 21262 htons(len - 21263 ((char *)&tcp->tcp_ip6h[1] - 21264 tcp->tcp_iphc)); 21265 mp = dupb(*xmit_tail); 21266 if (mp == NULL) { 21267 if (ire != NULL) 21268 IRE_REFRELE(ire); 21269 return (-1); /* out_of_mem */ 21270 } 21271 mp->b_rptr = rptr; 21272 /* 21273 * If the old timestamp is no longer in use, 21274 * sample a new timestamp now. 21275 */ 21276 if ((*xmit_tail)->b_next == NULL) { 21277 (*xmit_tail)->b_prev = local_time; 21278 (*xmit_tail)->b_next = 21279 (mblk_t *)(uintptr_t)(*snxt-len); 21280 } 21281 goto must_alloc; 21282 } 21283 } else { 21284 *xmit_tail = (*xmit_tail)->b_cont; 21285 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21286 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21287 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21288 (*xmit_tail)->b_rptr); 21289 } 21290 21291 (*xmit_tail)->b_prev = local_time; 21292 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21293 21294 *tail_unsent -= len; 21295 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21296 tcp->tcp_last_sent_len = (ushort_t)len; 21297 21298 len += tcp_hdr_len; 21299 if (tcp->tcp_ipversion == IPV4_VERSION) 21300 tcp->tcp_ipha->ipha_length = htons(len); 21301 else 21302 tcp->tcp_ip6h->ip6_plen = htons(len - 21303 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21304 21305 mp = dupb(*xmit_tail); 21306 if (mp == NULL) { 21307 if (ire != NULL) 21308 IRE_REFRELE(ire); 21309 return (-1); /* out_of_mem */ 21310 } 21311 21312 len = tcp_hdr_len; 21313 /* 21314 * There are four reasons to allocate a new hdr mblk: 21315 * 1) The bytes above us are in use by another packet 21316 * 2) We don't have good alignment 21317 * 3) The mblk is being shared 21318 * 4) We don't have enough room for a header 21319 */ 21320 rptr = mp->b_rptr - len; 21321 if (!OK_32PTR(rptr) || 21322 ((db = mp->b_datap), db->db_ref != 2) || 21323 rptr < db->db_base + ire_fp_mp_len) { 21324 /* NOTE: we assume allocb returns an OK_32PTR */ 21325 21326 must_alloc:; 21327 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21328 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21329 if (mp1 == NULL) { 21330 freemsg(mp); 21331 if (ire != NULL) 21332 IRE_REFRELE(ire); 21333 return (-1); /* out_of_mem */ 21334 } 21335 mp1->b_cont = mp; 21336 mp = mp1; 21337 /* Leave room for Link Level header */ 21338 len = tcp_hdr_len; 21339 rptr = 21340 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21341 mp->b_wptr = &rptr[len]; 21342 } 21343 21344 /* 21345 * Fill in the header using the template header, and add 21346 * options such as time-stamp, ECN and/or SACK, as needed. 21347 */ 21348 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21349 21350 mp->b_rptr = rptr; 21351 21352 if (*tail_unsent) { 21353 int spill = *tail_unsent; 21354 21355 mp1 = mp->b_cont; 21356 if (mp1 == NULL) 21357 mp1 = mp; 21358 21359 /* 21360 * If we're a little short, tack on more mblks until 21361 * there is no more spillover. 21362 */ 21363 while (spill < 0) { 21364 mblk_t *nmp; 21365 int nmpsz; 21366 21367 nmp = (*xmit_tail)->b_cont; 21368 nmpsz = MBLKL(nmp); 21369 21370 /* 21371 * Excess data in mblk; can we split it? 21372 * If MDT is enabled for the connection, 21373 * keep on splitting as this is a transient 21374 * send path. 21375 */ 21376 if (!do_lso_send && !tcp->tcp_mdt && 21377 (spill + nmpsz > 0)) { 21378 /* 21379 * Don't split if stream head was 21380 * told to break up larger writes 21381 * into smaller ones. 21382 */ 21383 if (tcp->tcp_maxpsz > 0) 21384 break; 21385 21386 /* 21387 * Next mblk is less than SMSS/2 21388 * rounded up to nearest 64-byte; 21389 * let it get sent as part of the 21390 * next segment. 21391 */ 21392 if (tcp->tcp_localnet && 21393 !tcp->tcp_cork && 21394 (nmpsz < roundup((mss >> 1), 64))) 21395 break; 21396 } 21397 21398 *xmit_tail = nmp; 21399 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21400 /* Stash for rtt use later */ 21401 (*xmit_tail)->b_prev = local_time; 21402 (*xmit_tail)->b_next = 21403 (mblk_t *)(uintptr_t)(*snxt - len); 21404 mp1->b_cont = dupb(*xmit_tail); 21405 mp1 = mp1->b_cont; 21406 21407 spill += nmpsz; 21408 if (mp1 == NULL) { 21409 *tail_unsent = spill; 21410 freemsg(mp); 21411 if (ire != NULL) 21412 IRE_REFRELE(ire); 21413 return (-1); /* out_of_mem */ 21414 } 21415 } 21416 21417 /* Trim back any surplus on the last mblk */ 21418 if (spill >= 0) { 21419 mp1->b_wptr -= spill; 21420 *tail_unsent = spill; 21421 } else { 21422 /* 21423 * We did not send everything we could in 21424 * order to remain within the b_cont limit. 21425 */ 21426 *usable -= spill; 21427 *snxt += spill; 21428 tcp->tcp_last_sent_len += spill; 21429 UPDATE_MIB(&tcps->tcps_mib, 21430 tcpOutDataBytes, spill); 21431 /* 21432 * Adjust the checksum 21433 */ 21434 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21435 sum += spill; 21436 sum = (sum >> 16) + (sum & 0xFFFF); 21437 U16_TO_ABE16(sum, tcph->th_sum); 21438 if (tcp->tcp_ipversion == IPV4_VERSION) { 21439 sum = ntohs( 21440 ((ipha_t *)rptr)->ipha_length) + 21441 spill; 21442 ((ipha_t *)rptr)->ipha_length = 21443 htons(sum); 21444 } else { 21445 sum = ntohs( 21446 ((ip6_t *)rptr)->ip6_plen) + 21447 spill; 21448 ((ip6_t *)rptr)->ip6_plen = 21449 htons(sum); 21450 } 21451 *tail_unsent = 0; 21452 } 21453 } 21454 if (tcp->tcp_ip_forward_progress) { 21455 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21456 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21457 tcp->tcp_ip_forward_progress = B_FALSE; 21458 } 21459 21460 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21461 if (do_lso_send) { 21462 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21463 num_lso_seg); 21464 tcp->tcp_obsegs += num_lso_seg; 21465 21466 TCP_STAT(tcps, tcp_lso_times); 21467 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21468 } else { 21469 tcp_send_data(tcp, q, mp); 21470 BUMP_LOCAL(tcp->tcp_obsegs); 21471 } 21472 } 21473 21474 if (ire != NULL) 21475 IRE_REFRELE(ire); 21476 return (0); 21477 } 21478 21479 /* Unlink and return any mblk that looks like it contains a MDT info */ 21480 static mblk_t * 21481 tcp_mdt_info_mp(mblk_t *mp) 21482 { 21483 mblk_t *prev_mp; 21484 21485 for (;;) { 21486 prev_mp = mp; 21487 /* no more to process? */ 21488 if ((mp = mp->b_cont) == NULL) 21489 break; 21490 21491 switch (DB_TYPE(mp)) { 21492 case M_CTL: 21493 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21494 continue; 21495 ASSERT(prev_mp != NULL); 21496 prev_mp->b_cont = mp->b_cont; 21497 mp->b_cont = NULL; 21498 return (mp); 21499 default: 21500 break; 21501 } 21502 } 21503 return (mp); 21504 } 21505 21506 /* MDT info update routine, called when IP notifies us about MDT */ 21507 static void 21508 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21509 { 21510 boolean_t prev_state; 21511 tcp_stack_t *tcps = tcp->tcp_tcps; 21512 21513 /* 21514 * IP is telling us to abort MDT on this connection? We know 21515 * this because the capability is only turned off when IP 21516 * encounters some pathological cases, e.g. link-layer change 21517 * where the new driver doesn't support MDT, or in situation 21518 * where MDT usage on the link-layer has been switched off. 21519 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21520 * if the link-layer doesn't support MDT, and if it does, it 21521 * will indicate that the feature is to be turned on. 21522 */ 21523 prev_state = tcp->tcp_mdt; 21524 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21525 if (!tcp->tcp_mdt && !first) { 21526 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21527 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21528 (void *)tcp->tcp_connp)); 21529 } 21530 21531 /* 21532 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21533 * so disable MDT otherwise. The checks are done here 21534 * and in tcp_wput_data(). 21535 */ 21536 if (tcp->tcp_mdt && 21537 (tcp->tcp_ipversion == IPV4_VERSION && 21538 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21539 (tcp->tcp_ipversion == IPV6_VERSION && 21540 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21541 tcp->tcp_mdt = B_FALSE; 21542 21543 if (tcp->tcp_mdt) { 21544 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21545 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21546 "version (%d), expected version is %d", 21547 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21548 tcp->tcp_mdt = B_FALSE; 21549 return; 21550 } 21551 21552 /* 21553 * We need the driver to be able to handle at least three 21554 * spans per packet in order for tcp MDT to be utilized. 21555 * The first is for the header portion, while the rest are 21556 * needed to handle a packet that straddles across two 21557 * virtually non-contiguous buffers; a typical tcp packet 21558 * therefore consists of only two spans. Note that we take 21559 * a zero as "don't care". 21560 */ 21561 if (mdt_capab->ill_mdt_span_limit > 0 && 21562 mdt_capab->ill_mdt_span_limit < 3) { 21563 tcp->tcp_mdt = B_FALSE; 21564 return; 21565 } 21566 21567 /* a zero means driver wants default value */ 21568 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21569 tcps->tcps_mdt_max_pbufs); 21570 if (tcp->tcp_mdt_max_pld == 0) 21571 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21572 21573 /* ensure 32-bit alignment */ 21574 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21575 mdt_capab->ill_mdt_hdr_head), 4); 21576 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21577 mdt_capab->ill_mdt_hdr_tail), 4); 21578 21579 if (!first && !prev_state) { 21580 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21581 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21582 (void *)tcp->tcp_connp)); 21583 } 21584 } 21585 } 21586 21587 /* Unlink and return any mblk that looks like it contains a LSO info */ 21588 static mblk_t * 21589 tcp_lso_info_mp(mblk_t *mp) 21590 { 21591 mblk_t *prev_mp; 21592 21593 for (;;) { 21594 prev_mp = mp; 21595 /* no more to process? */ 21596 if ((mp = mp->b_cont) == NULL) 21597 break; 21598 21599 switch (DB_TYPE(mp)) { 21600 case M_CTL: 21601 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21602 continue; 21603 ASSERT(prev_mp != NULL); 21604 prev_mp->b_cont = mp->b_cont; 21605 mp->b_cont = NULL; 21606 return (mp); 21607 default: 21608 break; 21609 } 21610 } 21611 21612 return (mp); 21613 } 21614 21615 /* LSO info update routine, called when IP notifies us about LSO */ 21616 static void 21617 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21618 { 21619 tcp_stack_t *tcps = tcp->tcp_tcps; 21620 21621 /* 21622 * IP is telling us to abort LSO on this connection? We know 21623 * this because the capability is only turned off when IP 21624 * encounters some pathological cases, e.g. link-layer change 21625 * where the new NIC/driver doesn't support LSO, or in situation 21626 * where LSO usage on the link-layer has been switched off. 21627 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21628 * if the link-layer doesn't support LSO, and if it does, it 21629 * will indicate that the feature is to be turned on. 21630 */ 21631 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21632 TCP_STAT(tcps, tcp_lso_enabled); 21633 21634 /* 21635 * We currently only support LSO on simple TCP/IPv4, 21636 * so disable LSO otherwise. The checks are done here 21637 * and in tcp_wput_data(). 21638 */ 21639 if (tcp->tcp_lso && 21640 (tcp->tcp_ipversion == IPV4_VERSION && 21641 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21642 (tcp->tcp_ipversion == IPV6_VERSION)) { 21643 tcp->tcp_lso = B_FALSE; 21644 TCP_STAT(tcps, tcp_lso_disabled); 21645 } else { 21646 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21647 lso_capab->ill_lso_max); 21648 } 21649 } 21650 21651 static void 21652 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21653 { 21654 conn_t *connp = tcp->tcp_connp; 21655 tcp_stack_t *tcps = tcp->tcp_tcps; 21656 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21657 21658 ASSERT(ire != NULL); 21659 21660 /* 21661 * We may be in the fastpath here, and although we essentially do 21662 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21663 * we try to keep things as brief as possible. After all, these 21664 * are only best-effort checks, and we do more thorough ones prior 21665 * to calling tcp_send()/tcp_multisend(). 21666 */ 21667 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21668 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21669 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21670 !(ire->ire_flags & RTF_MULTIRT) && 21671 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21672 CONN_IS_LSO_MD_FASTPATH(connp)) { 21673 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21674 /* Cache the result */ 21675 connp->conn_lso_ok = B_TRUE; 21676 21677 ASSERT(ill->ill_lso_capab != NULL); 21678 if (!ill->ill_lso_capab->ill_lso_on) { 21679 ill->ill_lso_capab->ill_lso_on = 1; 21680 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21681 "LSO for interface %s\n", (void *)connp, 21682 ill->ill_name)); 21683 } 21684 tcp_lso_update(tcp, ill->ill_lso_capab); 21685 } else if (ipst->ips_ip_multidata_outbound && 21686 ILL_MDT_CAPABLE(ill)) { 21687 /* Cache the result */ 21688 connp->conn_mdt_ok = B_TRUE; 21689 21690 ASSERT(ill->ill_mdt_capab != NULL); 21691 if (!ill->ill_mdt_capab->ill_mdt_on) { 21692 ill->ill_mdt_capab->ill_mdt_on = 1; 21693 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21694 "MDT for interface %s\n", (void *)connp, 21695 ill->ill_name)); 21696 } 21697 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21698 } 21699 } 21700 21701 /* 21702 * The goal is to reduce the number of generated tcp segments by 21703 * setting the maxpsz multiplier to 0; this will have an affect on 21704 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21705 * into each packet, up to SMSS bytes. Doing this reduces the number 21706 * of outbound segments and incoming ACKs, thus allowing for better 21707 * network and system performance. In contrast the legacy behavior 21708 * may result in sending less than SMSS size, because the last mblk 21709 * for some packets may have more data than needed to make up SMSS, 21710 * and the legacy code refused to "split" it. 21711 * 21712 * We apply the new behavior on following situations: 21713 * 21714 * 1) Loopback connections, 21715 * 2) Connections in which the remote peer is not on local subnet, 21716 * 3) Local subnet connections over the bge interface (see below). 21717 * 21718 * Ideally, we would like this behavior to apply for interfaces other 21719 * than bge. However, doing so would negatively impact drivers which 21720 * perform dynamic mapping and unmapping of DMA resources, which are 21721 * increased by setting the maxpsz multiplier to 0 (more mblks per 21722 * packet will be generated by tcp). The bge driver does not suffer 21723 * from this, as it copies the mblks into pre-mapped buffers, and 21724 * therefore does not require more I/O resources than before. 21725 * 21726 * Otherwise, this behavior is present on all network interfaces when 21727 * the destination endpoint is non-local, since reducing the number 21728 * of packets in general is good for the network. 21729 * 21730 * TODO We need to remove this hard-coded conditional for bge once 21731 * a better "self-tuning" mechanism, or a way to comprehend 21732 * the driver transmit strategy is devised. Until the solution 21733 * is found and well understood, we live with this hack. 21734 */ 21735 if (!tcp_static_maxpsz && 21736 (tcp->tcp_loopback || !tcp->tcp_localnet || 21737 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21738 /* override the default value */ 21739 tcp->tcp_maxpsz = 0; 21740 21741 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21742 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21743 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21744 } 21745 21746 /* set the stream head parameters accordingly */ 21747 (void) tcp_maxpsz_set(tcp, B_TRUE); 21748 } 21749 21750 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21751 static void 21752 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21753 { 21754 uchar_t fval = *mp->b_rptr; 21755 mblk_t *tail; 21756 queue_t *q = tcp->tcp_wq; 21757 21758 /* TODO: How should flush interact with urgent data? */ 21759 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21760 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21761 /* 21762 * Flush only data that has not yet been put on the wire. If 21763 * we flush data that we have already transmitted, life, as we 21764 * know it, may come to an end. 21765 */ 21766 tail = tcp->tcp_xmit_tail; 21767 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21768 tcp->tcp_xmit_tail_unsent = 0; 21769 tcp->tcp_unsent = 0; 21770 if (tail->b_wptr != tail->b_rptr) 21771 tail = tail->b_cont; 21772 if (tail) { 21773 mblk_t **excess = &tcp->tcp_xmit_head; 21774 for (;;) { 21775 mblk_t *mp1 = *excess; 21776 if (mp1 == tail) 21777 break; 21778 tcp->tcp_xmit_tail = mp1; 21779 tcp->tcp_xmit_last = mp1; 21780 excess = &mp1->b_cont; 21781 } 21782 *excess = NULL; 21783 tcp_close_mpp(&tail); 21784 if (tcp->tcp_snd_zcopy_aware) 21785 tcp_zcopy_notify(tcp); 21786 } 21787 /* 21788 * We have no unsent data, so unsent must be less than 21789 * tcp_xmit_lowater, so re-enable flow. 21790 */ 21791 mutex_enter(&tcp->tcp_non_sq_lock); 21792 if (tcp->tcp_flow_stopped) { 21793 tcp_clrqfull(tcp); 21794 } 21795 mutex_exit(&tcp->tcp_non_sq_lock); 21796 } 21797 /* 21798 * TODO: you can't just flush these, you have to increase rwnd for one 21799 * thing. For another, how should urgent data interact? 21800 */ 21801 if (fval & FLUSHR) { 21802 *mp->b_rptr = fval & ~FLUSHW; 21803 /* XXX */ 21804 qreply(q, mp); 21805 return; 21806 } 21807 freemsg(mp); 21808 } 21809 21810 /* 21811 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21812 * messages. 21813 */ 21814 static void 21815 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21816 { 21817 mblk_t *mp1; 21818 STRUCT_HANDLE(strbuf, sb); 21819 uint16_t port; 21820 queue_t *q = tcp->tcp_wq; 21821 in6_addr_t v6addr; 21822 ipaddr_t v4addr; 21823 uint32_t flowinfo = 0; 21824 int addrlen; 21825 21826 /* Make sure it is one of ours. */ 21827 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21828 case TI_GETMYNAME: 21829 case TI_GETPEERNAME: 21830 break; 21831 default: 21832 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21833 return; 21834 } 21835 switch (mi_copy_state(q, mp, &mp1)) { 21836 case -1: 21837 return; 21838 case MI_COPY_CASE(MI_COPY_IN, 1): 21839 break; 21840 case MI_COPY_CASE(MI_COPY_OUT, 1): 21841 /* Copy out the strbuf. */ 21842 mi_copyout(q, mp); 21843 return; 21844 case MI_COPY_CASE(MI_COPY_OUT, 2): 21845 /* All done. */ 21846 mi_copy_done(q, mp, 0); 21847 return; 21848 default: 21849 mi_copy_done(q, mp, EPROTO); 21850 return; 21851 } 21852 /* Check alignment of the strbuf */ 21853 if (!OK_32PTR(mp1->b_rptr)) { 21854 mi_copy_done(q, mp, EINVAL); 21855 return; 21856 } 21857 21858 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 21859 (void *)mp1->b_rptr); 21860 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21861 21862 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21863 mi_copy_done(q, mp, EINVAL); 21864 return; 21865 } 21866 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21867 case TI_GETMYNAME: 21868 if (tcp->tcp_family == AF_INET) { 21869 if (tcp->tcp_ipversion == IPV4_VERSION) { 21870 v4addr = tcp->tcp_ipha->ipha_src; 21871 } else { 21872 /* can't return an address in this case */ 21873 v4addr = 0; 21874 } 21875 } else { 21876 /* tcp->tcp_family == AF_INET6 */ 21877 if (tcp->tcp_ipversion == IPV4_VERSION) { 21878 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 21879 &v6addr); 21880 } else { 21881 v6addr = tcp->tcp_ip6h->ip6_src; 21882 } 21883 } 21884 port = tcp->tcp_lport; 21885 break; 21886 case TI_GETPEERNAME: 21887 if (tcp->tcp_family == AF_INET) { 21888 if (tcp->tcp_ipversion == IPV4_VERSION) { 21889 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 21890 v4addr); 21891 } else { 21892 /* can't return an address in this case */ 21893 v4addr = 0; 21894 } 21895 } else { 21896 /* tcp->tcp_family == AF_INET6) */ 21897 v6addr = tcp->tcp_remote_v6; 21898 if (tcp->tcp_ipversion == IPV6_VERSION) { 21899 /* 21900 * No flowinfo if tcp->tcp_ipversion is v4. 21901 * 21902 * flowinfo was already initialized to zero 21903 * where it was declared above, so only 21904 * set it if ipversion is v6. 21905 */ 21906 flowinfo = tcp->tcp_ip6h->ip6_vcf & 21907 ~IPV6_VERS_AND_FLOW_MASK; 21908 } 21909 } 21910 port = tcp->tcp_fport; 21911 break; 21912 default: 21913 mi_copy_done(q, mp, EPROTO); 21914 return; 21915 } 21916 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21917 if (!mp1) 21918 return; 21919 21920 if (tcp->tcp_family == AF_INET) { 21921 sin_t *sin; 21922 21923 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 21924 sin = (sin_t *)mp1->b_rptr; 21925 mp1->b_wptr = (uchar_t *)&sin[1]; 21926 *sin = sin_null; 21927 sin->sin_family = AF_INET; 21928 sin->sin_addr.s_addr = v4addr; 21929 sin->sin_port = port; 21930 } else { 21931 /* tcp->tcp_family == AF_INET6 */ 21932 sin6_t *sin6; 21933 21934 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 21935 sin6 = (sin6_t *)mp1->b_rptr; 21936 mp1->b_wptr = (uchar_t *)&sin6[1]; 21937 *sin6 = sin6_null; 21938 sin6->sin6_family = AF_INET6; 21939 sin6->sin6_flowinfo = flowinfo; 21940 sin6->sin6_addr = v6addr; 21941 sin6->sin6_port = port; 21942 } 21943 /* Copy out the address */ 21944 mi_copyout(q, mp); 21945 } 21946 21947 /* 21948 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21949 * messages. 21950 */ 21951 /* ARGSUSED */ 21952 static void 21953 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21954 { 21955 conn_t *connp = (conn_t *)arg; 21956 tcp_t *tcp = connp->conn_tcp; 21957 queue_t *q = tcp->tcp_wq; 21958 struct iocblk *iocp; 21959 tcp_stack_t *tcps = tcp->tcp_tcps; 21960 21961 ASSERT(DB_TYPE(mp) == M_IOCTL); 21962 /* 21963 * Try and ASSERT the minimum possible references on the 21964 * conn early enough. Since we are executing on write side, 21965 * the connection is obviously not detached and that means 21966 * there is a ref each for TCP and IP. Since we are behind 21967 * the squeue, the minimum references needed are 3. If the 21968 * conn is in classifier hash list, there should be an 21969 * extra ref for that (we check both the possibilities). 21970 */ 21971 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21972 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21973 21974 iocp = (struct iocblk *)mp->b_rptr; 21975 switch (iocp->ioc_cmd) { 21976 case TCP_IOC_DEFAULT_Q: 21977 /* Wants to be the default wq. */ 21978 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21979 iocp->ioc_error = EPERM; 21980 iocp->ioc_count = 0; 21981 mp->b_datap->db_type = M_IOCACK; 21982 qreply(q, mp); 21983 return; 21984 } 21985 tcp_def_q_set(tcp, mp); 21986 return; 21987 case _SIOCSOCKFALLBACK: 21988 /* 21989 * Either sockmod is about to be popped and the socket 21990 * would now be treated as a plain stream, or a module 21991 * is about to be pushed so we could no longer use read- 21992 * side synchronous streams for fused loopback tcp. 21993 * Drain any queued data and disable direct sockfs 21994 * interface from now on. 21995 */ 21996 if (!tcp->tcp_issocket) { 21997 DB_TYPE(mp) = M_IOCNAK; 21998 iocp->ioc_error = EINVAL; 21999 } else { 22000 #ifdef _ILP32 22001 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 22002 #else 22003 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22004 #endif 22005 /* 22006 * Insert this socket into the acceptor hash. 22007 * We might need it for T_CONN_RES message 22008 */ 22009 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22010 22011 if (tcp->tcp_fused) { 22012 /* 22013 * This is a fused loopback tcp; disable 22014 * read-side synchronous streams interface 22015 * and drain any queued data. It is okay 22016 * to do this for non-synchronous streams 22017 * fused tcp as well. 22018 */ 22019 tcp_fuse_disable_pair(tcp, B_FALSE); 22020 } 22021 tcp->tcp_issocket = B_FALSE; 22022 TCP_STAT(tcps, tcp_sock_fallback); 22023 22024 DB_TYPE(mp) = M_IOCACK; 22025 iocp->ioc_error = 0; 22026 } 22027 iocp->ioc_count = 0; 22028 iocp->ioc_rval = 0; 22029 qreply(q, mp); 22030 return; 22031 } 22032 CALL_IP_WPUT(connp, q, mp); 22033 } 22034 22035 /* 22036 * This routine is called by tcp_wput() to handle all TPI requests. 22037 */ 22038 /* ARGSUSED */ 22039 static void 22040 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22041 { 22042 conn_t *connp = (conn_t *)arg; 22043 tcp_t *tcp = connp->conn_tcp; 22044 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22045 uchar_t *rptr; 22046 t_scalar_t type; 22047 int len; 22048 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22049 22050 /* 22051 * Try and ASSERT the minimum possible references on the 22052 * conn early enough. Since we are executing on write side, 22053 * the connection is obviously not detached and that means 22054 * there is a ref each for TCP and IP. Since we are behind 22055 * the squeue, the minimum references needed are 3. If the 22056 * conn is in classifier hash list, there should be an 22057 * extra ref for that (we check both the possibilities). 22058 */ 22059 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22060 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22061 22062 rptr = mp->b_rptr; 22063 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22064 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22065 type = ((union T_primitives *)rptr)->type; 22066 if (type == T_EXDATA_REQ) { 22067 uint32_t msize = msgdsize(mp->b_cont); 22068 22069 len = msize - 1; 22070 if (len < 0) { 22071 freemsg(mp); 22072 return; 22073 } 22074 /* 22075 * Try to force urgent data out on the wire. 22076 * Even if we have unsent data this will 22077 * at least send the urgent flag. 22078 * XXX does not handle more flag correctly. 22079 */ 22080 len += tcp->tcp_unsent; 22081 len += tcp->tcp_snxt; 22082 tcp->tcp_urg = len; 22083 tcp->tcp_valid_bits |= TCP_URG_VALID; 22084 22085 /* Bypass tcp protocol for fused tcp loopback */ 22086 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22087 return; 22088 } else if (type != T_DATA_REQ) { 22089 goto non_urgent_data; 22090 } 22091 /* TODO: options, flags, ... from user */ 22092 /* Set length to zero for reclamation below */ 22093 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22094 freeb(mp); 22095 return; 22096 } else { 22097 if (tcp->tcp_debug) { 22098 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22099 "tcp_wput_proto, dropping one..."); 22100 } 22101 freemsg(mp); 22102 return; 22103 } 22104 22105 non_urgent_data: 22106 22107 switch ((int)tprim->type) { 22108 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22109 /* 22110 * save the kssl_ent_t from the next block, and convert this 22111 * back to a normal bind_req. 22112 */ 22113 if (mp->b_cont != NULL) { 22114 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22115 22116 if (tcp->tcp_kssl_ent != NULL) { 22117 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22118 KSSL_NO_PROXY); 22119 tcp->tcp_kssl_ent = NULL; 22120 } 22121 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22122 sizeof (kssl_ent_t)); 22123 kssl_hold_ent(tcp->tcp_kssl_ent); 22124 freemsg(mp->b_cont); 22125 mp->b_cont = NULL; 22126 } 22127 tprim->type = T_BIND_REQ; 22128 22129 /* FALLTHROUGH */ 22130 case O_T_BIND_REQ: /* bind request */ 22131 case T_BIND_REQ: /* new semantics bind request */ 22132 tcp_bind(tcp, mp); 22133 break; 22134 case T_UNBIND_REQ: /* unbind request */ 22135 tcp_unbind(tcp, mp); 22136 break; 22137 case O_T_CONN_RES: /* old connection response XXX */ 22138 case T_CONN_RES: /* connection response */ 22139 tcp_accept(tcp, mp); 22140 break; 22141 case T_CONN_REQ: /* connection request */ 22142 tcp_connect(tcp, mp); 22143 break; 22144 case T_DISCON_REQ: /* disconnect request */ 22145 tcp_disconnect(tcp, mp); 22146 break; 22147 case T_CAPABILITY_REQ: 22148 tcp_capability_req(tcp, mp); /* capability request */ 22149 break; 22150 case T_INFO_REQ: /* information request */ 22151 tcp_info_req(tcp, mp); 22152 break; 22153 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22154 /* Only IP is allowed to return meaningful value */ 22155 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 22156 break; 22157 case T_OPTMGMT_REQ: 22158 /* 22159 * Note: no support for snmpcom_req() through new 22160 * T_OPTMGMT_REQ. See comments in ip.c 22161 */ 22162 /* Only IP is allowed to return meaningful value */ 22163 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 22164 break; 22165 22166 case T_UNITDATA_REQ: /* unitdata request */ 22167 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22168 break; 22169 case T_ORDREL_REQ: /* orderly release req */ 22170 freemsg(mp); 22171 22172 if (tcp->tcp_fused) 22173 tcp_unfuse(tcp); 22174 22175 if (tcp_xmit_end(tcp) != 0) { 22176 /* 22177 * We were crossing FINs and got a reset from 22178 * the other side. Just ignore it. 22179 */ 22180 if (tcp->tcp_debug) { 22181 (void) strlog(TCP_MOD_ID, 0, 1, 22182 SL_ERROR|SL_TRACE, 22183 "tcp_wput_proto, T_ORDREL_REQ out of " 22184 "state %s", 22185 tcp_display(tcp, NULL, 22186 DISP_ADDR_AND_PORT)); 22187 } 22188 } 22189 break; 22190 case T_ADDR_REQ: 22191 tcp_addr_req(tcp, mp); 22192 break; 22193 default: 22194 if (tcp->tcp_debug) { 22195 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22196 "tcp_wput_proto, bogus TPI msg, type %d", 22197 tprim->type); 22198 } 22199 /* 22200 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22201 * to recover. 22202 */ 22203 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22204 break; 22205 } 22206 } 22207 22208 /* 22209 * The TCP write service routine should never be called... 22210 */ 22211 /* ARGSUSED */ 22212 static void 22213 tcp_wsrv(queue_t *q) 22214 { 22215 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22216 22217 TCP_STAT(tcps, tcp_wsrv_called); 22218 } 22219 22220 /* Non overlapping byte exchanger */ 22221 static void 22222 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22223 { 22224 uchar_t uch; 22225 22226 while (len-- > 0) { 22227 uch = a[len]; 22228 a[len] = b[len]; 22229 b[len] = uch; 22230 } 22231 } 22232 22233 /* 22234 * Send out a control packet on the tcp connection specified. This routine 22235 * is typically called where we need a simple ACK or RST generated. 22236 */ 22237 static void 22238 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22239 { 22240 uchar_t *rptr; 22241 tcph_t *tcph; 22242 ipha_t *ipha = NULL; 22243 ip6_t *ip6h = NULL; 22244 uint32_t sum; 22245 int tcp_hdr_len; 22246 int tcp_ip_hdr_len; 22247 mblk_t *mp; 22248 tcp_stack_t *tcps = tcp->tcp_tcps; 22249 22250 /* 22251 * Save sum for use in source route later. 22252 */ 22253 ASSERT(tcp != NULL); 22254 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22255 tcp_hdr_len = tcp->tcp_hdr_len; 22256 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22257 22258 /* If a text string is passed in with the request, pass it to strlog. */ 22259 if (str != NULL && tcp->tcp_debug) { 22260 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22261 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22262 str, seq, ack, ctl); 22263 } 22264 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22265 BPRI_MED); 22266 if (mp == NULL) { 22267 return; 22268 } 22269 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22270 mp->b_rptr = rptr; 22271 mp->b_wptr = &rptr[tcp_hdr_len]; 22272 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22273 22274 if (tcp->tcp_ipversion == IPV4_VERSION) { 22275 ipha = (ipha_t *)rptr; 22276 ipha->ipha_length = htons(tcp_hdr_len); 22277 } else { 22278 ip6h = (ip6_t *)rptr; 22279 ASSERT(tcp != NULL); 22280 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22281 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22282 } 22283 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22284 tcph->th_flags[0] = (uint8_t)ctl; 22285 if (ctl & TH_RST) { 22286 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22287 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22288 /* 22289 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22290 */ 22291 if (tcp->tcp_snd_ts_ok && 22292 tcp->tcp_state > TCPS_SYN_SENT) { 22293 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22294 *(mp->b_wptr) = TCPOPT_EOL; 22295 if (tcp->tcp_ipversion == IPV4_VERSION) { 22296 ipha->ipha_length = htons(tcp_hdr_len - 22297 TCPOPT_REAL_TS_LEN); 22298 } else { 22299 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22300 TCPOPT_REAL_TS_LEN); 22301 } 22302 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22303 sum -= TCPOPT_REAL_TS_LEN; 22304 } 22305 } 22306 if (ctl & TH_ACK) { 22307 if (tcp->tcp_snd_ts_ok) { 22308 U32_TO_BE32(lbolt, 22309 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22310 U32_TO_BE32(tcp->tcp_ts_recent, 22311 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22312 } 22313 22314 /* Update the latest receive window size in TCP header. */ 22315 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22316 tcph->th_win); 22317 tcp->tcp_rack = ack; 22318 tcp->tcp_rack_cnt = 0; 22319 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22320 } 22321 BUMP_LOCAL(tcp->tcp_obsegs); 22322 U32_TO_BE32(seq, tcph->th_seq); 22323 U32_TO_BE32(ack, tcph->th_ack); 22324 /* 22325 * Include the adjustment for a source route if any. 22326 */ 22327 sum = (sum >> 16) + (sum & 0xFFFF); 22328 U16_TO_BE16(sum, tcph->th_sum); 22329 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22330 tcp_send_data(tcp, tcp->tcp_wq, mp); 22331 } 22332 22333 /* 22334 * If this routine returns B_TRUE, TCP can generate a RST in response 22335 * to a segment. If it returns B_FALSE, TCP should not respond. 22336 */ 22337 static boolean_t 22338 tcp_send_rst_chk(tcp_stack_t *tcps) 22339 { 22340 clock_t now; 22341 22342 /* 22343 * TCP needs to protect itself from generating too many RSTs. 22344 * This can be a DoS attack by sending us random segments 22345 * soliciting RSTs. 22346 * 22347 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22348 * in each 1 second interval. In this way, TCP still generate 22349 * RSTs in normal cases but when under attack, the impact is 22350 * limited. 22351 */ 22352 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22353 now = lbolt; 22354 /* lbolt can wrap around. */ 22355 if ((tcps->tcps_last_rst_intrvl > now) || 22356 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22357 1*SECONDS)) { 22358 tcps->tcps_last_rst_intrvl = now; 22359 tcps->tcps_rst_cnt = 1; 22360 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22361 return (B_FALSE); 22362 } 22363 } 22364 return (B_TRUE); 22365 } 22366 22367 /* 22368 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22369 */ 22370 static void 22371 tcp_ip_ire_mark_advice(tcp_t *tcp) 22372 { 22373 mblk_t *mp; 22374 ipic_t *ipic; 22375 22376 if (tcp->tcp_ipversion == IPV4_VERSION) { 22377 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22378 &ipic); 22379 } else { 22380 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22381 &ipic); 22382 } 22383 if (mp == NULL) 22384 return; 22385 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22386 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22387 } 22388 22389 /* 22390 * Return an IP advice ioctl mblk and set ipic to be the pointer 22391 * to the advice structure. 22392 */ 22393 static mblk_t * 22394 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22395 { 22396 struct iocblk *ioc; 22397 mblk_t *mp, *mp1; 22398 22399 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22400 if (mp == NULL) 22401 return (NULL); 22402 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22403 *ipic = (ipic_t *)mp->b_rptr; 22404 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22405 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22406 22407 bcopy(addr, *ipic + 1, addr_len); 22408 22409 (*ipic)->ipic_addr_length = addr_len; 22410 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22411 22412 mp1 = mkiocb(IP_IOCTL); 22413 if (mp1 == NULL) { 22414 freemsg(mp); 22415 return (NULL); 22416 } 22417 mp1->b_cont = mp; 22418 ioc = (struct iocblk *)mp1->b_rptr; 22419 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22420 22421 return (mp1); 22422 } 22423 22424 /* 22425 * Generate a reset based on an inbound packet, connp is set by caller 22426 * when RST is in response to an unexpected inbound packet for which 22427 * there is active tcp state in the system. 22428 * 22429 * IPSEC NOTE : Try to send the reply with the same protection as it came 22430 * in. We still have the ipsec_mp that the packet was attached to. Thus 22431 * the packet will go out at the same level of protection as it came in by 22432 * converting the IPSEC_IN to IPSEC_OUT. 22433 */ 22434 static void 22435 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22436 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22437 tcp_stack_t *tcps, conn_t *connp) 22438 { 22439 ipha_t *ipha = NULL; 22440 ip6_t *ip6h = NULL; 22441 ushort_t len; 22442 tcph_t *tcph; 22443 int i; 22444 mblk_t *ipsec_mp; 22445 boolean_t mctl_present; 22446 ipic_t *ipic; 22447 ipaddr_t v4addr; 22448 in6_addr_t v6addr; 22449 int addr_len; 22450 void *addr; 22451 queue_t *q = tcps->tcps_g_q; 22452 tcp_t *tcp; 22453 cred_t *cr; 22454 mblk_t *nmp; 22455 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22456 22457 if (tcps->tcps_g_q == NULL) { 22458 /* 22459 * For non-zero stackids the default queue isn't created 22460 * until the first open, thus there can be a need to send 22461 * a reset before then. But we can't do that, hence we just 22462 * drop the packet. Later during boot, when the default queue 22463 * has been setup, a retransmitted packet from the peer 22464 * will result in a reset. 22465 */ 22466 ASSERT(tcps->tcps_netstack->netstack_stackid != 22467 GLOBAL_NETSTACKID); 22468 freemsg(mp); 22469 return; 22470 } 22471 22472 if (connp != NULL) 22473 tcp = connp->conn_tcp; 22474 else 22475 tcp = Q_TO_TCP(q); 22476 22477 if (!tcp_send_rst_chk(tcps)) { 22478 tcps->tcps_rst_unsent++; 22479 freemsg(mp); 22480 return; 22481 } 22482 22483 if (mp->b_datap->db_type == M_CTL) { 22484 ipsec_mp = mp; 22485 mp = mp->b_cont; 22486 mctl_present = B_TRUE; 22487 } else { 22488 ipsec_mp = mp; 22489 mctl_present = B_FALSE; 22490 } 22491 22492 if (str && q && tcps->tcps_dbg) { 22493 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22494 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22495 "flags 0x%x", 22496 str, seq, ack, ctl); 22497 } 22498 if (mp->b_datap->db_ref != 1) { 22499 mblk_t *mp1 = copyb(mp); 22500 freemsg(mp); 22501 mp = mp1; 22502 if (!mp) { 22503 if (mctl_present) 22504 freeb(ipsec_mp); 22505 return; 22506 } else { 22507 if (mctl_present) { 22508 ipsec_mp->b_cont = mp; 22509 } else { 22510 ipsec_mp = mp; 22511 } 22512 } 22513 } else if (mp->b_cont) { 22514 freemsg(mp->b_cont); 22515 mp->b_cont = NULL; 22516 } 22517 /* 22518 * We skip reversing source route here. 22519 * (for now we replace all IP options with EOL) 22520 */ 22521 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22522 ipha = (ipha_t *)mp->b_rptr; 22523 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22524 mp->b_rptr[i] = IPOPT_EOL; 22525 /* 22526 * Make sure that src address isn't flagrantly invalid. 22527 * Not all broadcast address checking for the src address 22528 * is possible, since we don't know the netmask of the src 22529 * addr. No check for destination address is done, since 22530 * IP will not pass up a packet with a broadcast dest 22531 * address to TCP. Similar checks are done below for IPv6. 22532 */ 22533 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22534 CLASSD(ipha->ipha_src)) { 22535 freemsg(ipsec_mp); 22536 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22537 return; 22538 } 22539 } else { 22540 ip6h = (ip6_t *)mp->b_rptr; 22541 22542 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22543 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22544 freemsg(ipsec_mp); 22545 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22546 return; 22547 } 22548 22549 /* Remove any extension headers assuming partial overlay */ 22550 if (ip_hdr_len > IPV6_HDR_LEN) { 22551 uint8_t *to; 22552 22553 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22554 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22555 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22556 ip_hdr_len = IPV6_HDR_LEN; 22557 ip6h = (ip6_t *)mp->b_rptr; 22558 ip6h->ip6_nxt = IPPROTO_TCP; 22559 } 22560 } 22561 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22562 if (tcph->th_flags[0] & TH_RST) { 22563 freemsg(ipsec_mp); 22564 return; 22565 } 22566 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22567 len = ip_hdr_len + sizeof (tcph_t); 22568 mp->b_wptr = &mp->b_rptr[len]; 22569 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22570 ipha->ipha_length = htons(len); 22571 /* Swap addresses */ 22572 v4addr = ipha->ipha_src; 22573 ipha->ipha_src = ipha->ipha_dst; 22574 ipha->ipha_dst = v4addr; 22575 ipha->ipha_ident = 0; 22576 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22577 addr_len = IP_ADDR_LEN; 22578 addr = &v4addr; 22579 } else { 22580 /* No ip6i_t in this case */ 22581 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22582 /* Swap addresses */ 22583 v6addr = ip6h->ip6_src; 22584 ip6h->ip6_src = ip6h->ip6_dst; 22585 ip6h->ip6_dst = v6addr; 22586 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22587 addr_len = IPV6_ADDR_LEN; 22588 addr = &v6addr; 22589 } 22590 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22591 U32_TO_BE32(ack, tcph->th_ack); 22592 U32_TO_BE32(seq, tcph->th_seq); 22593 U16_TO_BE16(0, tcph->th_win); 22594 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22595 tcph->th_flags[0] = (uint8_t)ctl; 22596 if (ctl & TH_RST) { 22597 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22598 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22599 } 22600 22601 /* IP trusts us to set up labels when required. */ 22602 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 22603 crgetlabel(cr) != NULL) { 22604 int err, adjust; 22605 22606 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22607 err = tsol_check_label(cr, &mp, &adjust, 22608 tcp->tcp_connp->conn_mac_exempt, 22609 tcps->tcps_netstack->netstack_ip); 22610 else 22611 err = tsol_check_label_v6(cr, &mp, &adjust, 22612 tcp->tcp_connp->conn_mac_exempt, 22613 tcps->tcps_netstack->netstack_ip); 22614 if (mctl_present) 22615 ipsec_mp->b_cont = mp; 22616 else 22617 ipsec_mp = mp; 22618 if (err != 0) { 22619 freemsg(ipsec_mp); 22620 return; 22621 } 22622 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22623 ipha = (ipha_t *)mp->b_rptr; 22624 adjust += ntohs(ipha->ipha_length); 22625 ipha->ipha_length = htons(adjust); 22626 } else { 22627 ip6h = (ip6_t *)mp->b_rptr; 22628 } 22629 } 22630 22631 if (mctl_present) { 22632 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22633 22634 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22635 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22636 return; 22637 } 22638 } 22639 if (zoneid == ALL_ZONES) 22640 zoneid = GLOBAL_ZONEID; 22641 22642 /* Add the zoneid so ip_output routes it properly */ 22643 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22644 freemsg(ipsec_mp); 22645 return; 22646 } 22647 ipsec_mp = nmp; 22648 22649 /* 22650 * NOTE: one might consider tracing a TCP packet here, but 22651 * this function has no active TCP state and no tcp structure 22652 * that has a trace buffer. If we traced here, we would have 22653 * to keep a local trace buffer in tcp_record_trace(). 22654 * 22655 * TSol note: The mblk that contains the incoming packet was 22656 * reused by tcp_xmit_listener_reset, so it already contains 22657 * the right credentials and we don't need to call mblk_setcred. 22658 * Also the conn's cred is not right since it is associated 22659 * with tcps_g_q. 22660 */ 22661 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22662 22663 /* 22664 * Tell IP to mark the IRE used for this destination temporary. 22665 * This way, we can limit our exposure to DoS attack because IP 22666 * creates an IRE for each destination. If there are too many, 22667 * the time to do any routing lookup will be extremely long. And 22668 * the lookup can be in interrupt context. 22669 * 22670 * Note that in normal circumstances, this marking should not 22671 * affect anything. It would be nice if only 1 message is 22672 * needed to inform IP that the IRE created for this RST should 22673 * not be added to the cache table. But there is currently 22674 * not such communication mechanism between TCP and IP. So 22675 * the best we can do now is to send the advice ioctl to IP 22676 * to mark the IRE temporary. 22677 */ 22678 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22679 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22680 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22681 } 22682 } 22683 22684 /* 22685 * Initiate closedown sequence on an active connection. (May be called as 22686 * writer.) Return value zero for OK return, non-zero for error return. 22687 */ 22688 static int 22689 tcp_xmit_end(tcp_t *tcp) 22690 { 22691 ipic_t *ipic; 22692 mblk_t *mp; 22693 tcp_stack_t *tcps = tcp->tcp_tcps; 22694 22695 if (tcp->tcp_state < TCPS_SYN_RCVD || 22696 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22697 /* 22698 * Invalid state, only states TCPS_SYN_RCVD, 22699 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22700 */ 22701 return (-1); 22702 } 22703 22704 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22705 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22706 /* 22707 * If there is nothing more unsent, send the FIN now. 22708 * Otherwise, it will go out with the last segment. 22709 */ 22710 if (tcp->tcp_unsent == 0) { 22711 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22712 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22713 22714 if (mp) { 22715 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22716 tcp_send_data(tcp, tcp->tcp_wq, mp); 22717 } else { 22718 /* 22719 * Couldn't allocate msg. Pretend we got it out. 22720 * Wait for rexmit timeout. 22721 */ 22722 tcp->tcp_snxt = tcp->tcp_fss + 1; 22723 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22724 } 22725 22726 /* 22727 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22728 * changed. 22729 */ 22730 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22731 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22732 } 22733 } else { 22734 /* 22735 * If tcp->tcp_cork is set, then the data will not get sent, 22736 * so we have to check that and unset it first. 22737 */ 22738 if (tcp->tcp_cork) 22739 tcp->tcp_cork = B_FALSE; 22740 tcp_wput_data(tcp, NULL, B_FALSE); 22741 } 22742 22743 /* 22744 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22745 * is 0, don't update the cache. 22746 */ 22747 if (tcps->tcps_rtt_updates == 0 || 22748 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22749 return (0); 22750 22751 /* 22752 * NOTE: should not update if source routes i.e. if tcp_remote if 22753 * different from the destination. 22754 */ 22755 if (tcp->tcp_ipversion == IPV4_VERSION) { 22756 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22757 return (0); 22758 } 22759 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22760 &ipic); 22761 } else { 22762 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22763 &tcp->tcp_ip6h->ip6_dst))) { 22764 return (0); 22765 } 22766 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22767 &ipic); 22768 } 22769 22770 /* Record route attributes in the IRE for use by future connections. */ 22771 if (mp == NULL) 22772 return (0); 22773 22774 /* 22775 * We do not have a good algorithm to update ssthresh at this time. 22776 * So don't do any update. 22777 */ 22778 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22779 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22780 22781 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22782 return (0); 22783 } 22784 22785 /* 22786 * Generate a "no listener here" RST in response to an "unknown" segment. 22787 * connp is set by caller when RST is in response to an unexpected 22788 * inbound packet for which there is active tcp state in the system. 22789 * Note that we are reusing the incoming mp to construct the outgoing RST. 22790 */ 22791 void 22792 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22793 tcp_stack_t *tcps, conn_t *connp) 22794 { 22795 uchar_t *rptr; 22796 uint32_t seg_len; 22797 tcph_t *tcph; 22798 uint32_t seg_seq; 22799 uint32_t seg_ack; 22800 uint_t flags; 22801 mblk_t *ipsec_mp; 22802 ipha_t *ipha; 22803 ip6_t *ip6h; 22804 boolean_t mctl_present = B_FALSE; 22805 boolean_t check = B_TRUE; 22806 boolean_t policy_present; 22807 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22808 22809 TCP_STAT(tcps, tcp_no_listener); 22810 22811 ipsec_mp = mp; 22812 22813 if (mp->b_datap->db_type == M_CTL) { 22814 ipsec_in_t *ii; 22815 22816 mctl_present = B_TRUE; 22817 mp = mp->b_cont; 22818 22819 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22820 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22821 if (ii->ipsec_in_dont_check) { 22822 check = B_FALSE; 22823 if (!ii->ipsec_in_secure) { 22824 freeb(ipsec_mp); 22825 mctl_present = B_FALSE; 22826 ipsec_mp = mp; 22827 } 22828 } 22829 } 22830 22831 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22832 policy_present = ipss->ipsec_inbound_v4_policy_present; 22833 ipha = (ipha_t *)mp->b_rptr; 22834 ip6h = NULL; 22835 } else { 22836 policy_present = ipss->ipsec_inbound_v6_policy_present; 22837 ipha = NULL; 22838 ip6h = (ip6_t *)mp->b_rptr; 22839 } 22840 22841 if (check && policy_present) { 22842 /* 22843 * The conn_t parameter is NULL because we already know 22844 * nobody's home. 22845 */ 22846 ipsec_mp = ipsec_check_global_policy( 22847 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22848 tcps->tcps_netstack); 22849 if (ipsec_mp == NULL) 22850 return; 22851 } 22852 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22853 DTRACE_PROBE2( 22854 tx__ip__log__error__nolistener__tcp, 22855 char *, "Could not reply with RST to mp(1)", 22856 mblk_t *, mp); 22857 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22858 freemsg(ipsec_mp); 22859 return; 22860 } 22861 22862 rptr = mp->b_rptr; 22863 22864 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22865 seg_seq = BE32_TO_U32(tcph->th_seq); 22866 seg_ack = BE32_TO_U32(tcph->th_ack); 22867 flags = tcph->th_flags[0]; 22868 22869 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22870 if (flags & TH_RST) { 22871 freemsg(ipsec_mp); 22872 } else if (flags & TH_ACK) { 22873 tcp_xmit_early_reset("no tcp, reset", 22874 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 22875 connp); 22876 } else { 22877 if (flags & TH_SYN) { 22878 seg_len++; 22879 } else { 22880 /* 22881 * Here we violate the RFC. Note that a normal 22882 * TCP will never send a segment without the ACK 22883 * flag, except for RST or SYN segment. This 22884 * segment is neither. Just drop it on the 22885 * floor. 22886 */ 22887 freemsg(ipsec_mp); 22888 tcps->tcps_rst_unsent++; 22889 return; 22890 } 22891 22892 tcp_xmit_early_reset("no tcp, reset/ack", 22893 ipsec_mp, 0, seg_seq + seg_len, 22894 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 22895 } 22896 } 22897 22898 /* 22899 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22900 * ip and tcp header ready to pass down to IP. If the mp passed in is 22901 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22902 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22903 * otherwise it will dup partial mblks.) 22904 * Otherwise, an appropriate ACK packet will be generated. This 22905 * routine is not usually called to send new data for the first time. It 22906 * is mostly called out of the timer for retransmits, and to generate ACKs. 22907 * 22908 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22909 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22910 * of the original mblk chain will be returned in *offset and *end_mp. 22911 */ 22912 mblk_t * 22913 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22914 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22915 boolean_t rexmit) 22916 { 22917 int data_length; 22918 int32_t off = 0; 22919 uint_t flags; 22920 mblk_t *mp1; 22921 mblk_t *mp2; 22922 uchar_t *rptr; 22923 tcph_t *tcph; 22924 int32_t num_sack_blk = 0; 22925 int32_t sack_opt_len = 0; 22926 tcp_stack_t *tcps = tcp->tcp_tcps; 22927 22928 /* Allocate for our maximum TCP header + link-level */ 22929 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22930 tcps->tcps_wroff_xtra, BPRI_MED); 22931 if (!mp1) 22932 return (NULL); 22933 data_length = 0; 22934 22935 /* 22936 * Note that tcp_mss has been adjusted to take into account the 22937 * timestamp option if applicable. Because SACK options do not 22938 * appear in every TCP segments and they are of variable lengths, 22939 * they cannot be included in tcp_mss. Thus we need to calculate 22940 * the actual segment length when we need to send a segment which 22941 * includes SACK options. 22942 */ 22943 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22944 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22945 tcp->tcp_num_sack_blk); 22946 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22947 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22948 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22949 max_to_send -= sack_opt_len; 22950 } 22951 22952 if (offset != NULL) { 22953 off = *offset; 22954 /* We use offset as an indicator that end_mp is not NULL. */ 22955 *end_mp = NULL; 22956 } 22957 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22958 /* This could be faster with cooperation from downstream */ 22959 if (mp2 != mp1 && !sendall && 22960 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22961 max_to_send) 22962 /* 22963 * Don't send the next mblk since the whole mblk 22964 * does not fit. 22965 */ 22966 break; 22967 mp2->b_cont = dupb(mp); 22968 mp2 = mp2->b_cont; 22969 if (!mp2) { 22970 freemsg(mp1); 22971 return (NULL); 22972 } 22973 mp2->b_rptr += off; 22974 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22975 (uintptr_t)INT_MAX); 22976 22977 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22978 if (data_length > max_to_send) { 22979 mp2->b_wptr -= data_length - max_to_send; 22980 data_length = max_to_send; 22981 off = mp2->b_wptr - mp->b_rptr; 22982 break; 22983 } else { 22984 off = 0; 22985 } 22986 } 22987 if (offset != NULL) { 22988 *offset = off; 22989 *end_mp = mp; 22990 } 22991 if (seg_len != NULL) { 22992 *seg_len = data_length; 22993 } 22994 22995 /* Update the latest receive window size in TCP header. */ 22996 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22997 tcp->tcp_tcph->th_win); 22998 22999 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23000 mp1->b_rptr = rptr; 23001 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23002 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23003 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23004 U32_TO_ABE32(seq, tcph->th_seq); 23005 23006 /* 23007 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23008 * that this function was called from tcp_wput_data. Thus, when called 23009 * to retransmit data the setting of the PUSH bit may appear some 23010 * what random in that it might get set when it should not. This 23011 * should not pose any performance issues. 23012 */ 23013 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23014 tcp->tcp_unsent == data_length)) { 23015 flags = TH_ACK | TH_PUSH; 23016 } else { 23017 flags = TH_ACK; 23018 } 23019 23020 if (tcp->tcp_ecn_ok) { 23021 if (tcp->tcp_ecn_echo_on) 23022 flags |= TH_ECE; 23023 23024 /* 23025 * Only set ECT bit and ECN_CWR if a segment contains new data. 23026 * There is no TCP flow control for non-data segments, and 23027 * only data segment is transmitted reliably. 23028 */ 23029 if (data_length > 0 && !rexmit) { 23030 SET_ECT(tcp, rptr); 23031 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23032 flags |= TH_CWR; 23033 tcp->tcp_ecn_cwr_sent = B_TRUE; 23034 } 23035 } 23036 } 23037 23038 if (tcp->tcp_valid_bits) { 23039 uint32_t u1; 23040 23041 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23042 seq == tcp->tcp_iss) { 23043 uchar_t *wptr; 23044 23045 /* 23046 * If TCP_ISS_VALID and the seq number is tcp_iss, 23047 * TCP can only be in SYN-SENT, SYN-RCVD or 23048 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23049 * our SYN is not ack'ed but the app closes this 23050 * TCP connection. 23051 */ 23052 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23053 tcp->tcp_state == TCPS_SYN_RCVD || 23054 tcp->tcp_state == TCPS_FIN_WAIT_1); 23055 23056 /* 23057 * Tack on the MSS option. It is always needed 23058 * for both active and passive open. 23059 * 23060 * MSS option value should be interface MTU - MIN 23061 * TCP/IP header according to RFC 793 as it means 23062 * the maximum segment size TCP can receive. But 23063 * to get around some broken middle boxes/end hosts 23064 * out there, we allow the option value to be the 23065 * same as the MSS option size on the peer side. 23066 * In this way, the other side will not send 23067 * anything larger than they can receive. 23068 * 23069 * Note that for SYN_SENT state, the ndd param 23070 * tcp_use_smss_as_mss_opt has no effect as we 23071 * don't know the peer's MSS option value. So 23072 * the only case we need to take care of is in 23073 * SYN_RCVD state, which is done later. 23074 */ 23075 wptr = mp1->b_wptr; 23076 wptr[0] = TCPOPT_MAXSEG; 23077 wptr[1] = TCPOPT_MAXSEG_LEN; 23078 wptr += 2; 23079 u1 = tcp->tcp_if_mtu - 23080 (tcp->tcp_ipversion == IPV4_VERSION ? 23081 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23082 TCP_MIN_HEADER_LENGTH; 23083 U16_TO_BE16(u1, wptr); 23084 mp1->b_wptr = wptr + 2; 23085 /* Update the offset to cover the additional word */ 23086 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23087 23088 /* 23089 * Note that the following way of filling in 23090 * TCP options are not optimal. Some NOPs can 23091 * be saved. But there is no need at this time 23092 * to optimize it. When it is needed, we will 23093 * do it. 23094 */ 23095 switch (tcp->tcp_state) { 23096 case TCPS_SYN_SENT: 23097 flags = TH_SYN; 23098 23099 if (tcp->tcp_snd_ts_ok) { 23100 uint32_t llbolt = (uint32_t)lbolt; 23101 23102 wptr = mp1->b_wptr; 23103 wptr[0] = TCPOPT_NOP; 23104 wptr[1] = TCPOPT_NOP; 23105 wptr[2] = TCPOPT_TSTAMP; 23106 wptr[3] = TCPOPT_TSTAMP_LEN; 23107 wptr += 4; 23108 U32_TO_BE32(llbolt, wptr); 23109 wptr += 4; 23110 ASSERT(tcp->tcp_ts_recent == 0); 23111 U32_TO_BE32(0L, wptr); 23112 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23113 tcph->th_offset_and_rsrvd[0] += 23114 (3 << 4); 23115 } 23116 23117 /* 23118 * Set up all the bits to tell other side 23119 * we are ECN capable. 23120 */ 23121 if (tcp->tcp_ecn_ok) { 23122 flags |= (TH_ECE | TH_CWR); 23123 } 23124 break; 23125 case TCPS_SYN_RCVD: 23126 flags |= TH_SYN; 23127 23128 /* 23129 * Reset the MSS option value to be SMSS 23130 * We should probably add back the bytes 23131 * for timestamp option and IPsec. We 23132 * don't do that as this is a workaround 23133 * for broken middle boxes/end hosts, it 23134 * is better for us to be more cautious. 23135 * They may not take these things into 23136 * account in their SMSS calculation. Thus 23137 * the peer's calculated SMSS may be smaller 23138 * than what it can be. This should be OK. 23139 */ 23140 if (tcps->tcps_use_smss_as_mss_opt) { 23141 u1 = tcp->tcp_mss; 23142 U16_TO_BE16(u1, wptr); 23143 } 23144 23145 /* 23146 * If the other side is ECN capable, reply 23147 * that we are also ECN capable. 23148 */ 23149 if (tcp->tcp_ecn_ok) 23150 flags |= TH_ECE; 23151 break; 23152 default: 23153 /* 23154 * The above ASSERT() makes sure that this 23155 * must be FIN-WAIT-1 state. Our SYN has 23156 * not been ack'ed so retransmit it. 23157 */ 23158 flags |= TH_SYN; 23159 break; 23160 } 23161 23162 if (tcp->tcp_snd_ws_ok) { 23163 wptr = mp1->b_wptr; 23164 wptr[0] = TCPOPT_NOP; 23165 wptr[1] = TCPOPT_WSCALE; 23166 wptr[2] = TCPOPT_WS_LEN; 23167 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23168 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23169 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23170 } 23171 23172 if (tcp->tcp_snd_sack_ok) { 23173 wptr = mp1->b_wptr; 23174 wptr[0] = TCPOPT_NOP; 23175 wptr[1] = TCPOPT_NOP; 23176 wptr[2] = TCPOPT_SACK_PERMITTED; 23177 wptr[3] = TCPOPT_SACK_OK_LEN; 23178 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23179 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23180 } 23181 23182 /* allocb() of adequate mblk assures space */ 23183 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23184 (uintptr_t)INT_MAX); 23185 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23186 /* 23187 * Get IP set to checksum on our behalf 23188 * Include the adjustment for a source route if any. 23189 */ 23190 u1 += tcp->tcp_sum; 23191 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23192 U16_TO_BE16(u1, tcph->th_sum); 23193 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23194 } 23195 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23196 (seq + data_length) == tcp->tcp_fss) { 23197 if (!tcp->tcp_fin_acked) { 23198 flags |= TH_FIN; 23199 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23200 } 23201 if (!tcp->tcp_fin_sent) { 23202 tcp->tcp_fin_sent = B_TRUE; 23203 switch (tcp->tcp_state) { 23204 case TCPS_SYN_RCVD: 23205 case TCPS_ESTABLISHED: 23206 tcp->tcp_state = TCPS_FIN_WAIT_1; 23207 break; 23208 case TCPS_CLOSE_WAIT: 23209 tcp->tcp_state = TCPS_LAST_ACK; 23210 break; 23211 } 23212 if (tcp->tcp_suna == tcp->tcp_snxt) 23213 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23214 tcp->tcp_snxt = tcp->tcp_fss + 1; 23215 } 23216 } 23217 /* 23218 * Note the trick here. u1 is unsigned. When tcp_urg 23219 * is smaller than seq, u1 will become a very huge value. 23220 * So the comparison will fail. Also note that tcp_urp 23221 * should be positive, see RFC 793 page 17. 23222 */ 23223 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23224 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23225 u1 < (uint32_t)(64 * 1024)) { 23226 flags |= TH_URG; 23227 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23228 U32_TO_ABE16(u1, tcph->th_urp); 23229 } 23230 } 23231 tcph->th_flags[0] = (uchar_t)flags; 23232 tcp->tcp_rack = tcp->tcp_rnxt; 23233 tcp->tcp_rack_cnt = 0; 23234 23235 if (tcp->tcp_snd_ts_ok) { 23236 if (tcp->tcp_state != TCPS_SYN_SENT) { 23237 uint32_t llbolt = (uint32_t)lbolt; 23238 23239 U32_TO_BE32(llbolt, 23240 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23241 U32_TO_BE32(tcp->tcp_ts_recent, 23242 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23243 } 23244 } 23245 23246 if (num_sack_blk > 0) { 23247 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23248 sack_blk_t *tmp; 23249 int32_t i; 23250 23251 wptr[0] = TCPOPT_NOP; 23252 wptr[1] = TCPOPT_NOP; 23253 wptr[2] = TCPOPT_SACK; 23254 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23255 sizeof (sack_blk_t); 23256 wptr += TCPOPT_REAL_SACK_LEN; 23257 23258 tmp = tcp->tcp_sack_list; 23259 for (i = 0; i < num_sack_blk; i++) { 23260 U32_TO_BE32(tmp[i].begin, wptr); 23261 wptr += sizeof (tcp_seq); 23262 U32_TO_BE32(tmp[i].end, wptr); 23263 wptr += sizeof (tcp_seq); 23264 } 23265 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23266 } 23267 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23268 data_length += (int)(mp1->b_wptr - rptr); 23269 if (tcp->tcp_ipversion == IPV4_VERSION) { 23270 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23271 } else { 23272 ip6_t *ip6 = (ip6_t *)(rptr + 23273 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23274 sizeof (ip6i_t) : 0)); 23275 23276 ip6->ip6_plen = htons(data_length - 23277 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23278 } 23279 23280 /* 23281 * Prime pump for IP 23282 * Include the adjustment for a source route if any. 23283 */ 23284 data_length -= tcp->tcp_ip_hdr_len; 23285 data_length += tcp->tcp_sum; 23286 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23287 U16_TO_ABE16(data_length, tcph->th_sum); 23288 if (tcp->tcp_ip_forward_progress) { 23289 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23290 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23291 tcp->tcp_ip_forward_progress = B_FALSE; 23292 } 23293 return (mp1); 23294 } 23295 23296 /* This function handles the push timeout. */ 23297 void 23298 tcp_push_timer(void *arg) 23299 { 23300 conn_t *connp = (conn_t *)arg; 23301 tcp_t *tcp = connp->conn_tcp; 23302 tcp_stack_t *tcps = tcp->tcp_tcps; 23303 23304 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23305 23306 ASSERT(tcp->tcp_listener == NULL); 23307 23308 /* 23309 * We need to plug synchronous streams during our drain to prevent 23310 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23311 */ 23312 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23313 tcp->tcp_push_tid = 0; 23314 if ((tcp->tcp_rcv_list != NULL) && 23315 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 23316 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23317 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23318 } 23319 23320 /* 23321 * This function handles delayed ACK timeout. 23322 */ 23323 static void 23324 tcp_ack_timer(void *arg) 23325 { 23326 conn_t *connp = (conn_t *)arg; 23327 tcp_t *tcp = connp->conn_tcp; 23328 mblk_t *mp; 23329 tcp_stack_t *tcps = tcp->tcp_tcps; 23330 23331 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23332 23333 tcp->tcp_ack_tid = 0; 23334 23335 if (tcp->tcp_fused) 23336 return; 23337 23338 /* 23339 * Do not send ACK if there is no outstanding unack'ed data. 23340 */ 23341 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23342 return; 23343 } 23344 23345 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23346 /* 23347 * Make sure we don't allow deferred ACKs to result in 23348 * timer-based ACKing. If we have held off an ACK 23349 * when there was more than an mss here, and the timer 23350 * goes off, we have to worry about the possibility 23351 * that the sender isn't doing slow-start, or is out 23352 * of step with us for some other reason. We fall 23353 * permanently back in the direction of 23354 * ACK-every-other-packet as suggested in RFC 1122. 23355 */ 23356 if (tcp->tcp_rack_abs_max > 2) 23357 tcp->tcp_rack_abs_max--; 23358 tcp->tcp_rack_cur_max = 2; 23359 } 23360 mp = tcp_ack_mp(tcp); 23361 23362 if (mp != NULL) { 23363 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 23364 BUMP_LOCAL(tcp->tcp_obsegs); 23365 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23366 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23367 tcp_send_data(tcp, tcp->tcp_wq, mp); 23368 } 23369 } 23370 23371 23372 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23373 static mblk_t * 23374 tcp_ack_mp(tcp_t *tcp) 23375 { 23376 uint32_t seq_no; 23377 tcp_stack_t *tcps = tcp->tcp_tcps; 23378 23379 /* 23380 * There are a few cases to be considered while setting the sequence no. 23381 * Essentially, we can come here while processing an unacceptable pkt 23382 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23383 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23384 * If we are here for a zero window probe, stick with suna. In all 23385 * other cases, we check if suna + swnd encompasses snxt and set 23386 * the sequence number to snxt, if so. If snxt falls outside the 23387 * window (the receiver probably shrunk its window), we will go with 23388 * suna + swnd, otherwise the sequence no will be unacceptable to the 23389 * receiver. 23390 */ 23391 if (tcp->tcp_zero_win_probe) { 23392 seq_no = tcp->tcp_suna; 23393 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23394 ASSERT(tcp->tcp_swnd == 0); 23395 seq_no = tcp->tcp_snxt; 23396 } else { 23397 seq_no = SEQ_GT(tcp->tcp_snxt, 23398 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23399 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23400 } 23401 23402 if (tcp->tcp_valid_bits) { 23403 /* 23404 * For the complex case where we have to send some 23405 * controls (FIN or SYN), let tcp_xmit_mp do it. 23406 */ 23407 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23408 NULL, B_FALSE)); 23409 } else { 23410 /* Generate a simple ACK */ 23411 int data_length; 23412 uchar_t *rptr; 23413 tcph_t *tcph; 23414 mblk_t *mp1; 23415 int32_t tcp_hdr_len; 23416 int32_t tcp_tcp_hdr_len; 23417 int32_t num_sack_blk = 0; 23418 int32_t sack_opt_len; 23419 23420 /* 23421 * Allocate space for TCP + IP headers 23422 * and link-level header 23423 */ 23424 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23425 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23426 tcp->tcp_num_sack_blk); 23427 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23428 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23429 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23430 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23431 } else { 23432 tcp_hdr_len = tcp->tcp_hdr_len; 23433 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23434 } 23435 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23436 if (!mp1) 23437 return (NULL); 23438 23439 /* Update the latest receive window size in TCP header. */ 23440 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23441 tcp->tcp_tcph->th_win); 23442 /* copy in prototype TCP + IP header */ 23443 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23444 mp1->b_rptr = rptr; 23445 mp1->b_wptr = rptr + tcp_hdr_len; 23446 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23447 23448 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23449 23450 /* Set the TCP sequence number. */ 23451 U32_TO_ABE32(seq_no, tcph->th_seq); 23452 23453 /* Set up the TCP flag field. */ 23454 tcph->th_flags[0] = (uchar_t)TH_ACK; 23455 if (tcp->tcp_ecn_echo_on) 23456 tcph->th_flags[0] |= TH_ECE; 23457 23458 tcp->tcp_rack = tcp->tcp_rnxt; 23459 tcp->tcp_rack_cnt = 0; 23460 23461 /* fill in timestamp option if in use */ 23462 if (tcp->tcp_snd_ts_ok) { 23463 uint32_t llbolt = (uint32_t)lbolt; 23464 23465 U32_TO_BE32(llbolt, 23466 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23467 U32_TO_BE32(tcp->tcp_ts_recent, 23468 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23469 } 23470 23471 /* Fill in SACK options */ 23472 if (num_sack_blk > 0) { 23473 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23474 sack_blk_t *tmp; 23475 int32_t i; 23476 23477 wptr[0] = TCPOPT_NOP; 23478 wptr[1] = TCPOPT_NOP; 23479 wptr[2] = TCPOPT_SACK; 23480 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23481 sizeof (sack_blk_t); 23482 wptr += TCPOPT_REAL_SACK_LEN; 23483 23484 tmp = tcp->tcp_sack_list; 23485 for (i = 0; i < num_sack_blk; i++) { 23486 U32_TO_BE32(tmp[i].begin, wptr); 23487 wptr += sizeof (tcp_seq); 23488 U32_TO_BE32(tmp[i].end, wptr); 23489 wptr += sizeof (tcp_seq); 23490 } 23491 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23492 << 4); 23493 } 23494 23495 if (tcp->tcp_ipversion == IPV4_VERSION) { 23496 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23497 } else { 23498 /* Check for ip6i_t header in sticky hdrs */ 23499 ip6_t *ip6 = (ip6_t *)(rptr + 23500 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23501 sizeof (ip6i_t) : 0)); 23502 23503 ip6->ip6_plen = htons(tcp_hdr_len - 23504 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23505 } 23506 23507 /* 23508 * Prime pump for checksum calculation in IP. Include the 23509 * adjustment for a source route if any. 23510 */ 23511 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23512 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23513 U16_TO_ABE16(data_length, tcph->th_sum); 23514 23515 if (tcp->tcp_ip_forward_progress) { 23516 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23517 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23518 tcp->tcp_ip_forward_progress = B_FALSE; 23519 } 23520 return (mp1); 23521 } 23522 } 23523 23524 /* 23525 * To create a temporary tcp structure for inserting into bind hash list. 23526 * The parameter is assumed to be in network byte order, ready for use. 23527 */ 23528 /* ARGSUSED */ 23529 static tcp_t * 23530 tcp_alloc_temp_tcp(in_port_t port, tcp_stack_t *tcps) 23531 { 23532 conn_t *connp; 23533 tcp_t *tcp; 23534 23535 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP, tcps->tcps_netstack); 23536 if (connp == NULL) 23537 return (NULL); 23538 23539 tcp = connp->conn_tcp; 23540 tcp->tcp_tcps = tcps; 23541 TCPS_REFHOLD(tcps); 23542 23543 /* 23544 * Only initialize the necessary info in those structures. Note 23545 * that since INADDR_ANY is all 0, we do not need to set 23546 * tcp_bound_source to INADDR_ANY here. 23547 */ 23548 tcp->tcp_state = TCPS_BOUND; 23549 tcp->tcp_lport = port; 23550 tcp->tcp_exclbind = 1; 23551 tcp->tcp_reserved_port = 1; 23552 23553 /* Just for place holding... */ 23554 tcp->tcp_ipversion = IPV4_VERSION; 23555 23556 return (tcp); 23557 } 23558 23559 /* 23560 * To remove a port range specified by lo_port and hi_port from the 23561 * reserved port ranges. This is one of the three public functions of 23562 * the reserved port interface. Note that a port range has to be removed 23563 * as a whole. Ports in a range cannot be removed individually. 23564 * 23565 * Params: 23566 * in_port_t lo_port: the beginning port of the reserved port range to 23567 * be deleted. 23568 * in_port_t hi_port: the ending port of the reserved port range to 23569 * be deleted. 23570 * 23571 * Return: 23572 * B_TRUE if the deletion is successful, B_FALSE otherwise. 23573 * 23574 * Assumes that nca is only for zoneid=0 23575 */ 23576 boolean_t 23577 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 23578 { 23579 int i, j; 23580 int size; 23581 tcp_t **temp_tcp_array; 23582 tcp_t *tcp; 23583 tcp_stack_t *tcps; 23584 23585 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23586 ASSERT(tcps != NULL); 23587 23588 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23589 23590 /* First make sure that the port ranage is indeed reserved. */ 23591 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23592 if (tcps->tcps_reserved_port[i].lo_port == lo_port) { 23593 hi_port = tcps->tcps_reserved_port[i].hi_port; 23594 temp_tcp_array = 23595 tcps->tcps_reserved_port[i].temp_tcp_array; 23596 break; 23597 } 23598 } 23599 if (i == tcps->tcps_reserved_port_array_size) { 23600 rw_exit(&tcps->tcps_reserved_port_lock); 23601 netstack_rele(tcps->tcps_netstack); 23602 return (B_FALSE); 23603 } 23604 23605 /* 23606 * Remove the range from the array. This simple loop is possible 23607 * because port ranges are inserted in ascending order. 23608 */ 23609 for (j = i; j < tcps->tcps_reserved_port_array_size - 1; j++) { 23610 tcps->tcps_reserved_port[j].lo_port = 23611 tcps->tcps_reserved_port[j+1].lo_port; 23612 tcps->tcps_reserved_port[j].hi_port = 23613 tcps->tcps_reserved_port[j+1].hi_port; 23614 tcps->tcps_reserved_port[j].temp_tcp_array = 23615 tcps->tcps_reserved_port[j+1].temp_tcp_array; 23616 } 23617 23618 /* Remove all the temporary tcp structures. */ 23619 size = hi_port - lo_port + 1; 23620 while (size > 0) { 23621 tcp = temp_tcp_array[size - 1]; 23622 ASSERT(tcp != NULL); 23623 tcp_bind_hash_remove(tcp); 23624 CONN_DEC_REF(tcp->tcp_connp); 23625 size--; 23626 } 23627 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 23628 tcps->tcps_reserved_port_array_size--; 23629 rw_exit(&tcps->tcps_reserved_port_lock); 23630 netstack_rele(tcps->tcps_netstack); 23631 return (B_TRUE); 23632 } 23633 23634 /* 23635 * Macro to remove temporary tcp structure from the bind hash list. The 23636 * first parameter is the list of tcp to be removed. The second parameter 23637 * is the number of tcps in the array. 23638 */ 23639 #define TCP_TMP_TCP_REMOVE(tcp_array, num, tcps) \ 23640 { \ 23641 while ((num) > 0) { \ 23642 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 23643 tf_t *tbf; \ 23644 tcp_t *tcpnext; \ 23645 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 23646 mutex_enter(&tbf->tf_lock); \ 23647 tcpnext = tcp->tcp_bind_hash; \ 23648 if (tcpnext) { \ 23649 tcpnext->tcp_ptpbhn = \ 23650 tcp->tcp_ptpbhn; \ 23651 } \ 23652 *tcp->tcp_ptpbhn = tcpnext; \ 23653 mutex_exit(&tbf->tf_lock); \ 23654 kmem_free(tcp, sizeof (tcp_t)); \ 23655 (tcp_array)[(num) - 1] = NULL; \ 23656 (num)--; \ 23657 } \ 23658 } 23659 23660 /* 23661 * The public interface for other modules to call to reserve a port range 23662 * in TCP. The caller passes in how large a port range it wants. TCP 23663 * will try to find a range and return it via lo_port and hi_port. This is 23664 * used by NCA's nca_conn_init. 23665 * NCA can only be used in the global zone so this only affects the global 23666 * zone's ports. 23667 * 23668 * Params: 23669 * int size: the size of the port range to be reserved. 23670 * in_port_t *lo_port (referenced): returns the beginning port of the 23671 * reserved port range added. 23672 * in_port_t *hi_port (referenced): returns the ending port of the 23673 * reserved port range added. 23674 * 23675 * Return: 23676 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 23677 * 23678 * Assumes that nca is only for zoneid=0 23679 */ 23680 boolean_t 23681 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 23682 { 23683 tcp_t *tcp; 23684 tcp_t *tmp_tcp; 23685 tcp_t **temp_tcp_array; 23686 tf_t *tbf; 23687 in_port_t net_port; 23688 in_port_t port; 23689 int32_t cur_size; 23690 int i, j; 23691 boolean_t used; 23692 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 23693 zoneid_t zoneid = GLOBAL_ZONEID; 23694 tcp_stack_t *tcps; 23695 23696 /* Sanity check. */ 23697 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 23698 return (B_FALSE); 23699 } 23700 23701 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23702 ASSERT(tcps != NULL); 23703 23704 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23705 if (tcps->tcps_reserved_port_array_size == 23706 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 23707 rw_exit(&tcps->tcps_reserved_port_lock); 23708 netstack_rele(tcps->tcps_netstack); 23709 return (B_FALSE); 23710 } 23711 23712 /* 23713 * Find the starting port to try. Since the port ranges are ordered 23714 * in the reserved port array, we can do a simple search here. 23715 */ 23716 *lo_port = TCP_SMALLEST_RESERVED_PORT; 23717 *hi_port = TCP_LARGEST_RESERVED_PORT; 23718 for (i = 0; i < tcps->tcps_reserved_port_array_size; 23719 *lo_port = tcps->tcps_reserved_port[i].hi_port + 1, i++) { 23720 if (tcps->tcps_reserved_port[i].lo_port - *lo_port >= size) { 23721 *hi_port = tcps->tcps_reserved_port[i].lo_port - 1; 23722 break; 23723 } 23724 } 23725 /* No available port range. */ 23726 if (i == tcps->tcps_reserved_port_array_size && 23727 *hi_port - *lo_port < size) { 23728 rw_exit(&tcps->tcps_reserved_port_lock); 23729 netstack_rele(tcps->tcps_netstack); 23730 return (B_FALSE); 23731 } 23732 23733 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 23734 if (temp_tcp_array == NULL) { 23735 rw_exit(&tcps->tcps_reserved_port_lock); 23736 netstack_rele(tcps->tcps_netstack); 23737 return (B_FALSE); 23738 } 23739 23740 /* Go thru the port range to see if some ports are already bound. */ 23741 for (port = *lo_port, cur_size = 0; 23742 cur_size < size && port <= *hi_port; 23743 cur_size++, port++) { 23744 used = B_FALSE; 23745 net_port = htons(port); 23746 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(net_port)]; 23747 mutex_enter(&tbf->tf_lock); 23748 for (tcp = tbf->tf_tcp; tcp != NULL; 23749 tcp = tcp->tcp_bind_hash) { 23750 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 23751 net_port == tcp->tcp_lport) { 23752 /* 23753 * A port is already bound. Search again 23754 * starting from port + 1. Release all 23755 * temporary tcps. 23756 */ 23757 mutex_exit(&tbf->tf_lock); 23758 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23759 tcps); 23760 *lo_port = port + 1; 23761 cur_size = -1; 23762 used = B_TRUE; 23763 break; 23764 } 23765 } 23766 if (!used) { 23767 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port, tcps)) == 23768 NULL) { 23769 /* 23770 * Allocation failure. Just fail the request. 23771 * Need to remove all those temporary tcp 23772 * structures. 23773 */ 23774 mutex_exit(&tbf->tf_lock); 23775 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23776 tcps); 23777 rw_exit(&tcps->tcps_reserved_port_lock); 23778 kmem_free(temp_tcp_array, 23779 (hi_port - lo_port + 1) * 23780 sizeof (tcp_t *)); 23781 netstack_rele(tcps->tcps_netstack); 23782 return (B_FALSE); 23783 } 23784 temp_tcp_array[cur_size] = tmp_tcp; 23785 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 23786 mutex_exit(&tbf->tf_lock); 23787 } 23788 } 23789 23790 /* 23791 * The current range is not large enough. We can actually do another 23792 * search if this search is done between 2 reserved port ranges. But 23793 * for first release, we just stop here and return saying that no port 23794 * range is available. 23795 */ 23796 if (cur_size < size) { 23797 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, tcps); 23798 rw_exit(&tcps->tcps_reserved_port_lock); 23799 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 23800 netstack_rele(tcps->tcps_netstack); 23801 return (B_FALSE); 23802 } 23803 *hi_port = port - 1; 23804 23805 /* 23806 * Insert range into array in ascending order. Since this function 23807 * must not be called often, we choose to use the simplest method. 23808 * The above array should not consume excessive stack space as 23809 * the size must be very small. If in future releases, we find 23810 * that we should provide more reserved port ranges, this function 23811 * has to be modified to be more efficient. 23812 */ 23813 if (tcps->tcps_reserved_port_array_size == 0) { 23814 tcps->tcps_reserved_port[0].lo_port = *lo_port; 23815 tcps->tcps_reserved_port[0].hi_port = *hi_port; 23816 tcps->tcps_reserved_port[0].temp_tcp_array = temp_tcp_array; 23817 } else { 23818 for (i = 0, j = 0; i < tcps->tcps_reserved_port_array_size; 23819 i++, j++) { 23820 if (*lo_port < tcps->tcps_reserved_port[i].lo_port && 23821 i == j) { 23822 tmp_ports[j].lo_port = *lo_port; 23823 tmp_ports[j].hi_port = *hi_port; 23824 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23825 j++; 23826 } 23827 tmp_ports[j].lo_port = 23828 tcps->tcps_reserved_port[i].lo_port; 23829 tmp_ports[j].hi_port = 23830 tcps->tcps_reserved_port[i].hi_port; 23831 tmp_ports[j].temp_tcp_array = 23832 tcps->tcps_reserved_port[i].temp_tcp_array; 23833 } 23834 if (j == i) { 23835 tmp_ports[j].lo_port = *lo_port; 23836 tmp_ports[j].hi_port = *hi_port; 23837 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23838 } 23839 bcopy(tmp_ports, tcps->tcps_reserved_port, sizeof (tmp_ports)); 23840 } 23841 tcps->tcps_reserved_port_array_size++; 23842 rw_exit(&tcps->tcps_reserved_port_lock); 23843 netstack_rele(tcps->tcps_netstack); 23844 return (B_TRUE); 23845 } 23846 23847 /* 23848 * Check to see if a port is in any reserved port range. 23849 * 23850 * Params: 23851 * in_port_t port: the port to be verified. 23852 * 23853 * Return: 23854 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 23855 */ 23856 boolean_t 23857 tcp_reserved_port_check(in_port_t port, tcp_stack_t *tcps) 23858 { 23859 int i; 23860 23861 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23862 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23863 if (port >= tcps->tcps_reserved_port[i].lo_port || 23864 port <= tcps->tcps_reserved_port[i].hi_port) { 23865 rw_exit(&tcps->tcps_reserved_port_lock); 23866 return (B_TRUE); 23867 } 23868 } 23869 rw_exit(&tcps->tcps_reserved_port_lock); 23870 return (B_FALSE); 23871 } 23872 23873 /* 23874 * To list all reserved port ranges. This is the function to handle 23875 * ndd tcp_reserved_port_list. 23876 */ 23877 /* ARGSUSED */ 23878 static int 23879 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23880 { 23881 int i; 23882 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23883 23884 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23885 if (tcps->tcps_reserved_port_array_size > 0) 23886 (void) mi_mpprintf(mp, "The following ports are reserved:"); 23887 else 23888 (void) mi_mpprintf(mp, "No port is reserved."); 23889 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23890 (void) mi_mpprintf(mp, "%d-%d", 23891 tcps->tcps_reserved_port[i].lo_port, 23892 tcps->tcps_reserved_port[i].hi_port); 23893 } 23894 rw_exit(&tcps->tcps_reserved_port_lock); 23895 return (0); 23896 } 23897 23898 /* 23899 * Hash list insertion routine for tcp_t structures. 23900 * Inserts entries with the ones bound to a specific IP address first 23901 * followed by those bound to INADDR_ANY. 23902 */ 23903 static void 23904 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23905 { 23906 tcp_t **tcpp; 23907 tcp_t *tcpnext; 23908 23909 if (tcp->tcp_ptpbhn != NULL) { 23910 ASSERT(!caller_holds_lock); 23911 tcp_bind_hash_remove(tcp); 23912 } 23913 tcpp = &tbf->tf_tcp; 23914 if (!caller_holds_lock) { 23915 mutex_enter(&tbf->tf_lock); 23916 } else { 23917 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23918 } 23919 tcpnext = tcpp[0]; 23920 if (tcpnext) { 23921 /* 23922 * If the new tcp bound to the INADDR_ANY address 23923 * and the first one in the list is not bound to 23924 * INADDR_ANY we skip all entries until we find the 23925 * first one bound to INADDR_ANY. 23926 * This makes sure that applications binding to a 23927 * specific address get preference over those binding to 23928 * INADDR_ANY. 23929 */ 23930 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23931 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23932 while ((tcpnext = tcpp[0]) != NULL && 23933 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23934 tcpp = &(tcpnext->tcp_bind_hash); 23935 if (tcpnext) 23936 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23937 } else 23938 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23939 } 23940 tcp->tcp_bind_hash = tcpnext; 23941 tcp->tcp_ptpbhn = tcpp; 23942 tcpp[0] = tcp; 23943 if (!caller_holds_lock) 23944 mutex_exit(&tbf->tf_lock); 23945 } 23946 23947 /* 23948 * Hash list removal routine for tcp_t structures. 23949 */ 23950 static void 23951 tcp_bind_hash_remove(tcp_t *tcp) 23952 { 23953 tcp_t *tcpnext; 23954 kmutex_t *lockp; 23955 tcp_stack_t *tcps = tcp->tcp_tcps; 23956 23957 if (tcp->tcp_ptpbhn == NULL) 23958 return; 23959 23960 /* 23961 * Extract the lock pointer in case there are concurrent 23962 * hash_remove's for this instance. 23963 */ 23964 ASSERT(tcp->tcp_lport != 0); 23965 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23966 23967 ASSERT(lockp != NULL); 23968 mutex_enter(lockp); 23969 if (tcp->tcp_ptpbhn) { 23970 tcpnext = tcp->tcp_bind_hash; 23971 if (tcpnext) { 23972 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23973 tcp->tcp_bind_hash = NULL; 23974 } 23975 *tcp->tcp_ptpbhn = tcpnext; 23976 tcp->tcp_ptpbhn = NULL; 23977 } 23978 mutex_exit(lockp); 23979 } 23980 23981 23982 /* 23983 * Hash list lookup routine for tcp_t structures. 23984 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23985 */ 23986 static tcp_t * 23987 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23988 { 23989 tf_t *tf; 23990 tcp_t *tcp; 23991 23992 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23993 mutex_enter(&tf->tf_lock); 23994 for (tcp = tf->tf_tcp; tcp != NULL; 23995 tcp = tcp->tcp_acceptor_hash) { 23996 if (tcp->tcp_acceptor_id == id) { 23997 CONN_INC_REF(tcp->tcp_connp); 23998 mutex_exit(&tf->tf_lock); 23999 return (tcp); 24000 } 24001 } 24002 mutex_exit(&tf->tf_lock); 24003 return (NULL); 24004 } 24005 24006 24007 /* 24008 * Hash list insertion routine for tcp_t structures. 24009 */ 24010 void 24011 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24012 { 24013 tf_t *tf; 24014 tcp_t **tcpp; 24015 tcp_t *tcpnext; 24016 tcp_stack_t *tcps = tcp->tcp_tcps; 24017 24018 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24019 24020 if (tcp->tcp_ptpahn != NULL) 24021 tcp_acceptor_hash_remove(tcp); 24022 tcpp = &tf->tf_tcp; 24023 mutex_enter(&tf->tf_lock); 24024 tcpnext = tcpp[0]; 24025 if (tcpnext) 24026 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24027 tcp->tcp_acceptor_hash = tcpnext; 24028 tcp->tcp_ptpahn = tcpp; 24029 tcpp[0] = tcp; 24030 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24031 mutex_exit(&tf->tf_lock); 24032 } 24033 24034 /* 24035 * Hash list removal routine for tcp_t structures. 24036 */ 24037 static void 24038 tcp_acceptor_hash_remove(tcp_t *tcp) 24039 { 24040 tcp_t *tcpnext; 24041 kmutex_t *lockp; 24042 24043 /* 24044 * Extract the lock pointer in case there are concurrent 24045 * hash_remove's for this instance. 24046 */ 24047 lockp = tcp->tcp_acceptor_lockp; 24048 24049 if (tcp->tcp_ptpahn == NULL) 24050 return; 24051 24052 ASSERT(lockp != NULL); 24053 mutex_enter(lockp); 24054 if (tcp->tcp_ptpahn) { 24055 tcpnext = tcp->tcp_acceptor_hash; 24056 if (tcpnext) { 24057 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24058 tcp->tcp_acceptor_hash = NULL; 24059 } 24060 *tcp->tcp_ptpahn = tcpnext; 24061 tcp->tcp_ptpahn = NULL; 24062 } 24063 mutex_exit(lockp); 24064 tcp->tcp_acceptor_lockp = NULL; 24065 } 24066 24067 /* ARGSUSED */ 24068 static int 24069 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 24070 { 24071 int error = 0; 24072 int retval; 24073 char *end; 24074 tcp_hsp_t *hsp; 24075 tcp_hsp_t *hspprev; 24076 ipaddr_t addr = 0; /* Address we're looking for */ 24077 in6_addr_t v6addr; /* Address we're looking for */ 24078 uint32_t hash; /* Hash of that address */ 24079 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24080 24081 /* 24082 * If the following variables are still zero after parsing the input 24083 * string, the user didn't specify them and we don't change them in 24084 * the HSP. 24085 */ 24086 24087 ipaddr_t mask = 0; /* Subnet mask */ 24088 in6_addr_t v6mask; 24089 long sendspace = 0; /* Send buffer size */ 24090 long recvspace = 0; /* Receive buffer size */ 24091 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 24092 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 24093 24094 rw_enter(&tcps->tcps_hsp_lock, RW_WRITER); 24095 24096 /* Parse and validate address */ 24097 if (af == AF_INET) { 24098 retval = inet_pton(af, value, &addr); 24099 if (retval == 1) 24100 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 24101 } else if (af == AF_INET6) { 24102 retval = inet_pton(af, value, &v6addr); 24103 } else { 24104 error = EINVAL; 24105 goto done; 24106 } 24107 if (retval == 0) { 24108 error = EINVAL; 24109 goto done; 24110 } 24111 24112 while ((*value) && *value != ' ') 24113 value++; 24114 24115 /* Parse individual keywords, set variables if found */ 24116 while (*value) { 24117 /* Skip leading blanks */ 24118 24119 while (*value == ' ' || *value == '\t') 24120 value++; 24121 24122 /* If at end of string, we're done */ 24123 24124 if (!*value) 24125 break; 24126 24127 /* We have a word, figure out what it is */ 24128 24129 if (strncmp("mask", value, 4) == 0) { 24130 value += 4; 24131 while (*value == ' ' || *value == '\t') 24132 value++; 24133 /* Parse subnet mask */ 24134 if (af == AF_INET) { 24135 retval = inet_pton(af, value, &mask); 24136 if (retval == 1) { 24137 V4MASK_TO_V6(mask, v6mask); 24138 } 24139 } else if (af == AF_INET6) { 24140 retval = inet_pton(af, value, &v6mask); 24141 } 24142 if (retval != 1) { 24143 error = EINVAL; 24144 goto done; 24145 } 24146 while ((*value) && *value != ' ') 24147 value++; 24148 } else if (strncmp("sendspace", value, 9) == 0) { 24149 value += 9; 24150 24151 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 24152 sendspace < TCP_XMIT_HIWATER || 24153 sendspace >= (1L<<30)) { 24154 error = EINVAL; 24155 goto done; 24156 } 24157 value = end; 24158 } else if (strncmp("recvspace", value, 9) == 0) { 24159 value += 9; 24160 24161 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 24162 recvspace < TCP_RECV_HIWATER || 24163 recvspace >= (1L<<30)) { 24164 error = EINVAL; 24165 goto done; 24166 } 24167 value = end; 24168 } else if (strncmp("timestamp", value, 9) == 0) { 24169 value += 9; 24170 24171 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 24172 timestamp < 0 || timestamp > 1) { 24173 error = EINVAL; 24174 goto done; 24175 } 24176 24177 /* 24178 * We increment timestamp so we know it's been set; 24179 * this is undone when we put it in the HSP 24180 */ 24181 timestamp++; 24182 value = end; 24183 } else if (strncmp("delete", value, 6) == 0) { 24184 value += 6; 24185 delete = B_TRUE; 24186 } else { 24187 error = EINVAL; 24188 goto done; 24189 } 24190 } 24191 24192 /* Hash address for lookup */ 24193 24194 hash = TCP_HSP_HASH(addr); 24195 24196 if (delete) { 24197 /* 24198 * Note that deletes don't return an error if the thing 24199 * we're trying to delete isn't there. 24200 */ 24201 if (tcps->tcps_hsp_hash == NULL) 24202 goto done; 24203 hsp = tcps->tcps_hsp_hash[hash]; 24204 24205 if (hsp) { 24206 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24207 &v6addr)) { 24208 tcps->tcps_hsp_hash[hash] = hsp->tcp_hsp_next; 24209 mi_free((char *)hsp); 24210 } else { 24211 hspprev = hsp; 24212 while ((hsp = hsp->tcp_hsp_next) != NULL) { 24213 if (IN6_ARE_ADDR_EQUAL( 24214 &hsp->tcp_hsp_addr_v6, &v6addr)) { 24215 hspprev->tcp_hsp_next = 24216 hsp->tcp_hsp_next; 24217 mi_free((char *)hsp); 24218 break; 24219 } 24220 hspprev = hsp; 24221 } 24222 } 24223 } 24224 } else { 24225 /* 24226 * We're adding/modifying an HSP. If we haven't already done 24227 * so, allocate the hash table. 24228 */ 24229 24230 if (!tcps->tcps_hsp_hash) { 24231 tcps->tcps_hsp_hash = (tcp_hsp_t **) 24232 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 24233 if (!tcps->tcps_hsp_hash) { 24234 error = EINVAL; 24235 goto done; 24236 } 24237 } 24238 24239 /* Get head of hash chain */ 24240 24241 hsp = tcps->tcps_hsp_hash[hash]; 24242 24243 /* Try to find pre-existing hsp on hash chain */ 24244 /* Doesn't handle CIDR prefixes. */ 24245 while (hsp) { 24246 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 24247 break; 24248 hsp = hsp->tcp_hsp_next; 24249 } 24250 24251 /* 24252 * If we didn't, create one with default values and put it 24253 * at head of hash chain 24254 */ 24255 24256 if (!hsp) { 24257 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 24258 if (!hsp) { 24259 error = EINVAL; 24260 goto done; 24261 } 24262 hsp->tcp_hsp_next = tcps->tcps_hsp_hash[hash]; 24263 tcps->tcps_hsp_hash[hash] = hsp; 24264 } 24265 24266 /* Set values that the user asked us to change */ 24267 24268 hsp->tcp_hsp_addr_v6 = v6addr; 24269 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 24270 hsp->tcp_hsp_vers = IPV4_VERSION; 24271 else 24272 hsp->tcp_hsp_vers = IPV6_VERSION; 24273 hsp->tcp_hsp_subnet_v6 = v6mask; 24274 if (sendspace > 0) 24275 hsp->tcp_hsp_sendspace = sendspace; 24276 if (recvspace > 0) 24277 hsp->tcp_hsp_recvspace = recvspace; 24278 if (timestamp > 0) 24279 hsp->tcp_hsp_tstamp = timestamp - 1; 24280 } 24281 24282 done: 24283 rw_exit(&tcps->tcps_hsp_lock); 24284 return (error); 24285 } 24286 24287 /* Set callback routine passed to nd_load by tcp_param_register. */ 24288 /* ARGSUSED */ 24289 static int 24290 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 24291 { 24292 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 24293 } 24294 /* ARGSUSED */ 24295 static int 24296 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24297 cred_t *cr) 24298 { 24299 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 24300 } 24301 24302 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 24303 /* ARGSUSED */ 24304 static int 24305 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 24306 { 24307 tcp_hsp_t *hsp; 24308 int i; 24309 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 24310 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24311 24312 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24313 (void) mi_mpprintf(mp, 24314 "Hash HSP " MI_COL_HDRPAD_STR 24315 "Address Subnet Mask Send Receive TStamp"); 24316 if (tcps->tcps_hsp_hash) { 24317 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 24318 hsp = tcps->tcps_hsp_hash[i]; 24319 while (hsp) { 24320 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 24321 (void) inet_ntop(AF_INET, 24322 &hsp->tcp_hsp_addr, 24323 addrbuf, sizeof (addrbuf)); 24324 (void) inet_ntop(AF_INET, 24325 &hsp->tcp_hsp_subnet, 24326 subnetbuf, sizeof (subnetbuf)); 24327 } else { 24328 (void) inet_ntop(AF_INET6, 24329 &hsp->tcp_hsp_addr_v6, 24330 addrbuf, sizeof (addrbuf)); 24331 (void) inet_ntop(AF_INET6, 24332 &hsp->tcp_hsp_subnet_v6, 24333 subnetbuf, sizeof (subnetbuf)); 24334 } 24335 (void) mi_mpprintf(mp, 24336 " %03d " MI_COL_PTRFMT_STR 24337 "%s %s %010d %010d %d", 24338 i, 24339 (void *)hsp, 24340 addrbuf, 24341 subnetbuf, 24342 hsp->tcp_hsp_sendspace, 24343 hsp->tcp_hsp_recvspace, 24344 hsp->tcp_hsp_tstamp); 24345 24346 hsp = hsp->tcp_hsp_next; 24347 } 24348 } 24349 } 24350 rw_exit(&tcps->tcps_hsp_lock); 24351 return (0); 24352 } 24353 24354 24355 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24356 24357 static ipaddr_t netmasks[] = { 24358 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24359 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24360 }; 24361 24362 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24363 24364 /* 24365 * XXX This routine should go away and instead we should use the metrics 24366 * associated with the routes to determine the default sndspace and rcvspace. 24367 */ 24368 static tcp_hsp_t * 24369 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24370 { 24371 tcp_hsp_t *hsp = NULL; 24372 24373 /* Quick check without acquiring the lock. */ 24374 if (tcps->tcps_hsp_hash == NULL) 24375 return (NULL); 24376 24377 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24378 24379 /* This routine finds the best-matching HSP for address addr. */ 24380 24381 if (tcps->tcps_hsp_hash) { 24382 int i; 24383 ipaddr_t srchaddr; 24384 tcp_hsp_t *hsp_net; 24385 24386 /* We do three passes: host, network, and subnet. */ 24387 24388 srchaddr = addr; 24389 24390 for (i = 1; i <= 3; i++) { 24391 /* Look for exact match on srchaddr */ 24392 24393 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24394 while (hsp) { 24395 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24396 hsp->tcp_hsp_addr == srchaddr) 24397 break; 24398 hsp = hsp->tcp_hsp_next; 24399 } 24400 ASSERT(hsp == NULL || 24401 hsp->tcp_hsp_vers == IPV4_VERSION); 24402 24403 /* 24404 * If this is the first pass: 24405 * If we found a match, great, return it. 24406 * If not, search for the network on the second pass. 24407 */ 24408 24409 if (i == 1) 24410 if (hsp) 24411 break; 24412 else 24413 { 24414 srchaddr = addr & netmask(addr); 24415 continue; 24416 } 24417 24418 /* 24419 * If this is the second pass: 24420 * If we found a match, but there's a subnet mask, 24421 * save the match but try again using the subnet 24422 * mask on the third pass. 24423 * Otherwise, return whatever we found. 24424 */ 24425 24426 if (i == 2) { 24427 if (hsp && hsp->tcp_hsp_subnet) { 24428 hsp_net = hsp; 24429 srchaddr = addr & hsp->tcp_hsp_subnet; 24430 continue; 24431 } else { 24432 break; 24433 } 24434 } 24435 24436 /* 24437 * This must be the third pass. If we didn't find 24438 * anything, return the saved network HSP instead. 24439 */ 24440 24441 if (!hsp) 24442 hsp = hsp_net; 24443 } 24444 } 24445 24446 rw_exit(&tcps->tcps_hsp_lock); 24447 return (hsp); 24448 } 24449 24450 /* 24451 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24452 * match lookup. 24453 */ 24454 static tcp_hsp_t * 24455 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24456 { 24457 tcp_hsp_t *hsp = NULL; 24458 24459 /* Quick check without acquiring the lock. */ 24460 if (tcps->tcps_hsp_hash == NULL) 24461 return (NULL); 24462 24463 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24464 24465 /* This routine finds the best-matching HSP for address addr. */ 24466 24467 if (tcps->tcps_hsp_hash) { 24468 int i; 24469 in6_addr_t v6srchaddr; 24470 tcp_hsp_t *hsp_net; 24471 24472 /* We do three passes: host, network, and subnet. */ 24473 24474 v6srchaddr = *v6addr; 24475 24476 for (i = 1; i <= 3; i++) { 24477 /* Look for exact match on srchaddr */ 24478 24479 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24480 V4_PART_OF_V6(v6srchaddr))]; 24481 while (hsp) { 24482 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24483 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24484 &v6srchaddr)) 24485 break; 24486 hsp = hsp->tcp_hsp_next; 24487 } 24488 24489 /* 24490 * If this is the first pass: 24491 * If we found a match, great, return it. 24492 * If not, search for the network on the second pass. 24493 */ 24494 24495 if (i == 1) 24496 if (hsp) 24497 break; 24498 else { 24499 /* Assume a 64 bit mask */ 24500 v6srchaddr.s6_addr32[0] = 24501 v6addr->s6_addr32[0]; 24502 v6srchaddr.s6_addr32[1] = 24503 v6addr->s6_addr32[1]; 24504 v6srchaddr.s6_addr32[2] = 0; 24505 v6srchaddr.s6_addr32[3] = 0; 24506 continue; 24507 } 24508 24509 /* 24510 * If this is the second pass: 24511 * If we found a match, but there's a subnet mask, 24512 * save the match but try again using the subnet 24513 * mask on the third pass. 24514 * Otherwise, return whatever we found. 24515 */ 24516 24517 if (i == 2) { 24518 ASSERT(hsp == NULL || 24519 hsp->tcp_hsp_vers == IPV6_VERSION); 24520 if (hsp && 24521 !IN6_IS_ADDR_UNSPECIFIED( 24522 &hsp->tcp_hsp_subnet_v6)) { 24523 hsp_net = hsp; 24524 V6_MASK_COPY(*v6addr, 24525 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24526 continue; 24527 } else { 24528 break; 24529 } 24530 } 24531 24532 /* 24533 * This must be the third pass. If we didn't find 24534 * anything, return the saved network HSP instead. 24535 */ 24536 24537 if (!hsp) 24538 hsp = hsp_net; 24539 } 24540 } 24541 24542 rw_exit(&tcps->tcps_hsp_lock); 24543 return (hsp); 24544 } 24545 24546 /* 24547 * Type three generator adapted from the random() function in 4.4 BSD: 24548 */ 24549 24550 /* 24551 * Copyright (c) 1983, 1993 24552 * The Regents of the University of California. All rights reserved. 24553 * 24554 * Redistribution and use in source and binary forms, with or without 24555 * modification, are permitted provided that the following conditions 24556 * are met: 24557 * 1. Redistributions of source code must retain the above copyright 24558 * notice, this list of conditions and the following disclaimer. 24559 * 2. Redistributions in binary form must reproduce the above copyright 24560 * notice, this list of conditions and the following disclaimer in the 24561 * documentation and/or other materials provided with the distribution. 24562 * 3. All advertising materials mentioning features or use of this software 24563 * must display the following acknowledgement: 24564 * This product includes software developed by the University of 24565 * California, Berkeley and its contributors. 24566 * 4. Neither the name of the University nor the names of its contributors 24567 * may be used to endorse or promote products derived from this software 24568 * without specific prior written permission. 24569 * 24570 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24571 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24572 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24573 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24574 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24575 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24576 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24577 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24578 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24579 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24580 * SUCH DAMAGE. 24581 */ 24582 24583 /* Type 3 -- x**31 + x**3 + 1 */ 24584 #define DEG_3 31 24585 #define SEP_3 3 24586 24587 24588 /* Protected by tcp_random_lock */ 24589 static int tcp_randtbl[DEG_3 + 1]; 24590 24591 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24592 static int *tcp_random_rptr = &tcp_randtbl[1]; 24593 24594 static int *tcp_random_state = &tcp_randtbl[1]; 24595 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24596 24597 kmutex_t tcp_random_lock; 24598 24599 void 24600 tcp_random_init(void) 24601 { 24602 int i; 24603 hrtime_t hrt; 24604 time_t wallclock; 24605 uint64_t result; 24606 24607 /* 24608 * Use high-res timer and current time for seed. Gethrtime() returns 24609 * a longlong, which may contain resolution down to nanoseconds. 24610 * The current time will either be a 32-bit or a 64-bit quantity. 24611 * XOR the two together in a 64-bit result variable. 24612 * Convert the result to a 32-bit value by multiplying the high-order 24613 * 32-bits by the low-order 32-bits. 24614 */ 24615 24616 hrt = gethrtime(); 24617 (void) drv_getparm(TIME, &wallclock); 24618 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24619 mutex_enter(&tcp_random_lock); 24620 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24621 (result & 0xffffffff); 24622 24623 for (i = 1; i < DEG_3; i++) 24624 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24625 + 12345; 24626 tcp_random_fptr = &tcp_random_state[SEP_3]; 24627 tcp_random_rptr = &tcp_random_state[0]; 24628 mutex_exit(&tcp_random_lock); 24629 for (i = 0; i < 10 * DEG_3; i++) 24630 (void) tcp_random(); 24631 } 24632 24633 /* 24634 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24635 * This range is selected to be approximately centered on TCP_ISS / 2, 24636 * and easy to compute. We get this value by generating a 32-bit random 24637 * number, selecting out the high-order 17 bits, and then adding one so 24638 * that we never return zero. 24639 */ 24640 int 24641 tcp_random(void) 24642 { 24643 int i; 24644 24645 mutex_enter(&tcp_random_lock); 24646 *tcp_random_fptr += *tcp_random_rptr; 24647 24648 /* 24649 * The high-order bits are more random than the low-order bits, 24650 * so we select out the high-order 17 bits and add one so that 24651 * we never return zero. 24652 */ 24653 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24654 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24655 tcp_random_fptr = tcp_random_state; 24656 ++tcp_random_rptr; 24657 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24658 tcp_random_rptr = tcp_random_state; 24659 24660 mutex_exit(&tcp_random_lock); 24661 return (i); 24662 } 24663 24664 /* 24665 * XXX This will go away when TPI is extended to send 24666 * info reqs to sockfs/timod ..... 24667 * Given a queue, set the max packet size for the write 24668 * side of the queue below stream head. This value is 24669 * cached on the stream head. 24670 * Returns 1 on success, 0 otherwise. 24671 */ 24672 static int 24673 setmaxps(queue_t *q, int maxpsz) 24674 { 24675 struct stdata *stp; 24676 queue_t *wq; 24677 stp = STREAM(q); 24678 24679 /* 24680 * At this point change of a queue parameter is not allowed 24681 * when a multiplexor is sitting on top. 24682 */ 24683 if (stp->sd_flag & STPLEX) 24684 return (0); 24685 24686 claimstr(stp->sd_wrq); 24687 wq = stp->sd_wrq->q_next; 24688 ASSERT(wq != NULL); 24689 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24690 releasestr(stp->sd_wrq); 24691 return (1); 24692 } 24693 24694 static int 24695 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24696 int *t_errorp, int *sys_errorp) 24697 { 24698 int error; 24699 int is_absreq_failure; 24700 t_scalar_t *opt_lenp; 24701 t_scalar_t opt_offset; 24702 int prim_type; 24703 struct T_conn_req *tcreqp; 24704 struct T_conn_res *tcresp; 24705 cred_t *cr; 24706 24707 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24708 24709 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24710 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24711 prim_type == T_CONN_RES); 24712 24713 switch (prim_type) { 24714 case T_CONN_REQ: 24715 tcreqp = (struct T_conn_req *)mp->b_rptr; 24716 opt_offset = tcreqp->OPT_offset; 24717 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24718 break; 24719 case O_T_CONN_RES: 24720 case T_CONN_RES: 24721 tcresp = (struct T_conn_res *)mp->b_rptr; 24722 opt_offset = tcresp->OPT_offset; 24723 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24724 break; 24725 } 24726 24727 *t_errorp = 0; 24728 *sys_errorp = 0; 24729 *do_disconnectp = 0; 24730 24731 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24732 opt_offset, cr, &tcp_opt_obj, 24733 NULL, &is_absreq_failure); 24734 24735 switch (error) { 24736 case 0: /* no error */ 24737 ASSERT(is_absreq_failure == 0); 24738 return (0); 24739 case ENOPROTOOPT: 24740 *t_errorp = TBADOPT; 24741 break; 24742 case EACCES: 24743 *t_errorp = TACCES; 24744 break; 24745 default: 24746 *t_errorp = TSYSERR; *sys_errorp = error; 24747 break; 24748 } 24749 if (is_absreq_failure != 0) { 24750 /* 24751 * The connection request should get the local ack 24752 * T_OK_ACK and then a T_DISCON_IND. 24753 */ 24754 *do_disconnectp = 1; 24755 } 24756 return (-1); 24757 } 24758 24759 /* 24760 * Split this function out so that if the secret changes, I'm okay. 24761 * 24762 * Initialize the tcp_iss_cookie and tcp_iss_key. 24763 */ 24764 24765 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24766 24767 static void 24768 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24769 { 24770 struct { 24771 int32_t current_time; 24772 uint32_t randnum; 24773 uint16_t pad; 24774 uint8_t ether[6]; 24775 uint8_t passwd[PASSWD_SIZE]; 24776 } tcp_iss_cookie; 24777 time_t t; 24778 24779 /* 24780 * Start with the current absolute time. 24781 */ 24782 (void) drv_getparm(TIME, &t); 24783 tcp_iss_cookie.current_time = t; 24784 24785 /* 24786 * XXX - Need a more random number per RFC 1750, not this crap. 24787 * OTOH, if what follows is pretty random, then I'm in better shape. 24788 */ 24789 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24790 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24791 24792 /* 24793 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24794 * as a good template. 24795 */ 24796 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24797 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24798 24799 /* 24800 * The pass-phrase. Normally this is supplied by user-called NDD. 24801 */ 24802 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24803 24804 /* 24805 * See 4010593 if this section becomes a problem again, 24806 * but the local ethernet address is useful here. 24807 */ 24808 (void) localetheraddr(NULL, 24809 (struct ether_addr *)&tcp_iss_cookie.ether); 24810 24811 /* 24812 * Hash 'em all together. The MD5Final is called per-connection. 24813 */ 24814 mutex_enter(&tcps->tcps_iss_key_lock); 24815 MD5Init(&tcps->tcps_iss_key); 24816 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24817 sizeof (tcp_iss_cookie)); 24818 mutex_exit(&tcps->tcps_iss_key_lock); 24819 } 24820 24821 /* 24822 * Set the RFC 1948 pass phrase 24823 */ 24824 /* ARGSUSED */ 24825 static int 24826 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24827 cred_t *cr) 24828 { 24829 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24830 24831 /* 24832 * Basically, value contains a new pass phrase. Pass it along! 24833 */ 24834 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24835 return (0); 24836 } 24837 24838 /* ARGSUSED */ 24839 static int 24840 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24841 { 24842 bzero(buf, sizeof (tcp_sack_info_t)); 24843 return (0); 24844 } 24845 24846 /* ARGSUSED */ 24847 static int 24848 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24849 { 24850 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24851 return (0); 24852 } 24853 24854 /* 24855 * Make sure we wait until the default queue is setup, yet allow 24856 * tcp_g_q_create() to open a TCP stream. 24857 * We need to allow tcp_g_q_create() do do an open 24858 * of tcp, hence we compare curhread. 24859 * All others have to wait until the tcps_g_q has been 24860 * setup. 24861 */ 24862 void 24863 tcp_g_q_setup(tcp_stack_t *tcps) 24864 { 24865 mutex_enter(&tcps->tcps_g_q_lock); 24866 if (tcps->tcps_g_q != NULL) { 24867 mutex_exit(&tcps->tcps_g_q_lock); 24868 return; 24869 } 24870 if (tcps->tcps_g_q_creator == NULL) { 24871 /* This thread will set it up */ 24872 tcps->tcps_g_q_creator = curthread; 24873 mutex_exit(&tcps->tcps_g_q_lock); 24874 tcp_g_q_create(tcps); 24875 mutex_enter(&tcps->tcps_g_q_lock); 24876 ASSERT(tcps->tcps_g_q_creator == curthread); 24877 tcps->tcps_g_q_creator = NULL; 24878 cv_signal(&tcps->tcps_g_q_cv); 24879 ASSERT(tcps->tcps_g_q != NULL); 24880 mutex_exit(&tcps->tcps_g_q_lock); 24881 return; 24882 } 24883 /* Everybody but the creator has to wait */ 24884 if (tcps->tcps_g_q_creator != curthread) { 24885 while (tcps->tcps_g_q == NULL) 24886 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24887 } 24888 mutex_exit(&tcps->tcps_g_q_lock); 24889 } 24890 24891 major_t IP_MAJ; 24892 #define IP "ip" 24893 24894 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24895 24896 /* 24897 * Create a default tcp queue here instead of in strplumb 24898 */ 24899 void 24900 tcp_g_q_create(tcp_stack_t *tcps) 24901 { 24902 int error; 24903 ldi_handle_t lh = NULL; 24904 ldi_ident_t li = NULL; 24905 int rval; 24906 cred_t *cr; 24907 24908 #ifdef NS_DEBUG 24909 (void) printf("tcp_g_q_create()\n"); 24910 #endif 24911 24912 ASSERT(tcps->tcps_g_q_creator == curthread); 24913 24914 error = ldi_ident_from_major(IP_MAJ, &li); 24915 if (error) { 24916 #ifdef DEBUG 24917 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24918 error); 24919 #endif 24920 return; 24921 } 24922 24923 cr = zone_get_kcred(netstackid_to_zoneid( 24924 tcps->tcps_netstack->netstack_stackid)); 24925 ASSERT(cr != NULL); 24926 /* 24927 * We set the tcp default queue to IPv6 because IPv4 falls 24928 * back to IPv6 when it can't find a client, but 24929 * IPv6 does not fall back to IPv4. 24930 */ 24931 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24932 if (error) { 24933 #ifdef DEBUG 24934 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24935 error); 24936 #endif 24937 goto out; 24938 } 24939 24940 /* 24941 * This ioctl causes the tcp framework to cache a pointer to 24942 * this stream, so we don't want to close the stream after 24943 * this operation. 24944 * Use the kernel credentials that are for the zone we're in. 24945 */ 24946 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24947 (intptr_t)0, FKIOCTL, cr, &rval); 24948 if (error) { 24949 #ifdef DEBUG 24950 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24951 "error %d\n", error); 24952 #endif 24953 goto out; 24954 } 24955 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24956 lh = NULL; 24957 out: 24958 /* Close layered handles */ 24959 if (li) 24960 ldi_ident_release(li); 24961 /* Keep cred around until _inactive needs it */ 24962 tcps->tcps_g_q_cr = cr; 24963 } 24964 24965 /* 24966 * We keep tcp_g_q set until all other tcp_t's in the zone 24967 * has gone away, and then when tcp_g_q_inactive() is called 24968 * we clear it. 24969 */ 24970 void 24971 tcp_g_q_destroy(tcp_stack_t *tcps) 24972 { 24973 #ifdef NS_DEBUG 24974 (void) printf("tcp_g_q_destroy()for stack %d\n", 24975 tcps->tcps_netstack->netstack_stackid); 24976 #endif 24977 24978 if (tcps->tcps_g_q == NULL) { 24979 return; /* Nothing to cleanup */ 24980 } 24981 /* 24982 * Drop reference corresponding to the default queue. 24983 * This reference was added from tcp_open when the default queue 24984 * was created, hence we compensate for this extra drop in 24985 * tcp_g_q_close. If the refcnt drops to zero here it means 24986 * the default queue was the last one to be open, in which 24987 * case, then tcp_g_q_inactive will be 24988 * called as a result of the refrele. 24989 */ 24990 TCPS_REFRELE(tcps); 24991 } 24992 24993 /* 24994 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24995 * Run by tcp_q_q_inactive using a taskq. 24996 */ 24997 static void 24998 tcp_g_q_close(void *arg) 24999 { 25000 tcp_stack_t *tcps = arg; 25001 int error; 25002 ldi_handle_t lh = NULL; 25003 ldi_ident_t li = NULL; 25004 cred_t *cr; 25005 25006 #ifdef NS_DEBUG 25007 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 25008 tcps->tcps_netstack->netstack_stackid, 25009 tcps->tcps_netstack->netstack_refcnt); 25010 #endif 25011 lh = tcps->tcps_g_q_lh; 25012 if (lh == NULL) 25013 return; /* Nothing to cleanup */ 25014 25015 ASSERT(tcps->tcps_refcnt == 1); 25016 ASSERT(tcps->tcps_g_q != NULL); 25017 25018 error = ldi_ident_from_major(IP_MAJ, &li); 25019 if (error) { 25020 #ifdef DEBUG 25021 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 25022 error); 25023 #endif 25024 return; 25025 } 25026 25027 cr = tcps->tcps_g_q_cr; 25028 tcps->tcps_g_q_cr = NULL; 25029 ASSERT(cr != NULL); 25030 25031 /* 25032 * Make sure we can break the recursion when tcp_close decrements 25033 * the reference count causing g_q_inactive to be called again. 25034 */ 25035 tcps->tcps_g_q_lh = NULL; 25036 25037 /* close the default queue */ 25038 (void) ldi_close(lh, FREAD|FWRITE, cr); 25039 /* 25040 * At this point in time tcps and the rest of netstack_t might 25041 * have been deleted. 25042 */ 25043 tcps = NULL; 25044 25045 /* Close layered handles */ 25046 ldi_ident_release(li); 25047 crfree(cr); 25048 } 25049 25050 /* 25051 * Called when last tcp_t drops reference count using TCPS_REFRELE. 25052 * 25053 * Have to ensure that the ldi routines are not used by an 25054 * interrupt thread by using a taskq. 25055 */ 25056 void 25057 tcp_g_q_inactive(tcp_stack_t *tcps) 25058 { 25059 if (tcps->tcps_g_q_lh == NULL) 25060 return; /* Nothing to cleanup */ 25061 25062 ASSERT(tcps->tcps_refcnt == 0); 25063 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 25064 25065 if (servicing_interrupt()) { 25066 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 25067 (void *) tcps, TQ_SLEEP); 25068 } else { 25069 tcp_g_q_close(tcps); 25070 } 25071 } 25072 25073 /* 25074 * Called by IP when IP is loaded into the kernel 25075 */ 25076 void 25077 tcp_ddi_g_init(void) 25078 { 25079 IP_MAJ = ddi_name_to_major(IP); 25080 25081 tcp_timercache = kmem_cache_create("tcp_timercache", 25082 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 25083 NULL, NULL, NULL, NULL, NULL, 0); 25084 25085 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 25086 sizeof (tcp_sack_info_t), 0, 25087 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 25088 25089 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 25090 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 25091 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 25092 25093 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 25094 25095 /* Initialize the random number generator */ 25096 tcp_random_init(); 25097 25098 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 25099 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 25100 25101 /* A single callback independently of how many netstacks we have */ 25102 ip_squeue_init(tcp_squeue_add); 25103 25104 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 25105 25106 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 25107 TASKQ_PREPOPULATE); 25108 25109 /* 25110 * We want to be informed each time a stack is created or 25111 * destroyed in the kernel, so we can maintain the 25112 * set of tcp_stack_t's. 25113 */ 25114 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 25115 tcp_stack_fini); 25116 } 25117 25118 25119 /* 25120 * Initialize the TCP stack instance. 25121 */ 25122 static void * 25123 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 25124 { 25125 tcp_stack_t *tcps; 25126 tcpparam_t *pa; 25127 int i; 25128 25129 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 25130 tcps->tcps_netstack = ns; 25131 25132 /* Initialize locks */ 25133 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 25134 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 25135 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 25136 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 25137 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 25138 rw_init(&tcps->tcps_reserved_port_lock, NULL, RW_DEFAULT, NULL); 25139 25140 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 25141 tcps->tcps_g_epriv_ports[0] = 2049; 25142 tcps->tcps_g_epriv_ports[1] = 4045; 25143 tcps->tcps_min_anonpriv_port = 512; 25144 25145 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 25146 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 25147 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 25148 TCP_FANOUT_SIZE, KM_SLEEP); 25149 tcps->tcps_reserved_port = kmem_zalloc(sizeof (tcp_rport_t) * 25150 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE, KM_SLEEP); 25151 25152 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25153 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 25154 MUTEX_DEFAULT, NULL); 25155 } 25156 25157 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25158 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 25159 MUTEX_DEFAULT, NULL); 25160 } 25161 25162 /* TCP's IPsec code calls the packet dropper. */ 25163 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 25164 25165 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 25166 tcps->tcps_params = pa; 25167 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25168 25169 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 25170 A_CNT(lcl_tcp_param_arr), tcps); 25171 25172 /* 25173 * Note: To really walk the device tree you need the devinfo 25174 * pointer to your device which is only available after probe/attach. 25175 * The following is safe only because it uses ddi_root_node() 25176 */ 25177 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 25178 tcp_opt_obj.odb_opt_arr_cnt); 25179 25180 /* 25181 * Initialize RFC 1948 secret values. This will probably be reset once 25182 * by the boot scripts. 25183 * 25184 * Use NULL name, as the name is caught by the new lockstats. 25185 * 25186 * Initialize with some random, non-guessable string, like the global 25187 * T_INFO_ACK. 25188 */ 25189 25190 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 25191 sizeof (tcp_g_t_info_ack), tcps); 25192 25193 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 25194 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 25195 25196 return (tcps); 25197 } 25198 25199 /* 25200 * Called when the IP module is about to be unloaded. 25201 */ 25202 void 25203 tcp_ddi_g_destroy(void) 25204 { 25205 tcp_g_kstat_fini(tcp_g_kstat); 25206 tcp_g_kstat = NULL; 25207 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 25208 25209 mutex_destroy(&tcp_random_lock); 25210 25211 kmem_cache_destroy(tcp_timercache); 25212 kmem_cache_destroy(tcp_sack_info_cache); 25213 kmem_cache_destroy(tcp_iphc_cache); 25214 25215 netstack_unregister(NS_TCP); 25216 taskq_destroy(tcp_taskq); 25217 } 25218 25219 /* 25220 * Shut down the TCP stack instance. 25221 */ 25222 /* ARGSUSED */ 25223 static void 25224 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25225 { 25226 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25227 25228 tcp_g_q_destroy(tcps); 25229 } 25230 25231 /* 25232 * Free the TCP stack instance. 25233 */ 25234 static void 25235 tcp_stack_fini(netstackid_t stackid, void *arg) 25236 { 25237 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25238 int i; 25239 25240 nd_free(&tcps->tcps_g_nd); 25241 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25242 tcps->tcps_params = NULL; 25243 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25244 tcps->tcps_wroff_xtra_param = NULL; 25245 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25246 tcps->tcps_mdt_head_param = NULL; 25247 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25248 tcps->tcps_mdt_tail_param = NULL; 25249 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25250 tcps->tcps_mdt_max_pbufs_param = NULL; 25251 25252 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25253 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25254 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25255 } 25256 25257 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25258 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25259 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25260 } 25261 25262 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25263 tcps->tcps_bind_fanout = NULL; 25264 25265 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25266 tcps->tcps_acceptor_fanout = NULL; 25267 25268 kmem_free(tcps->tcps_reserved_port, sizeof (tcp_rport_t) * 25269 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE); 25270 tcps->tcps_reserved_port = NULL; 25271 25272 mutex_destroy(&tcps->tcps_iss_key_lock); 25273 rw_destroy(&tcps->tcps_hsp_lock); 25274 mutex_destroy(&tcps->tcps_g_q_lock); 25275 cv_destroy(&tcps->tcps_g_q_cv); 25276 mutex_destroy(&tcps->tcps_epriv_port_lock); 25277 rw_destroy(&tcps->tcps_reserved_port_lock); 25278 25279 ip_drop_unregister(&tcps->tcps_dropper); 25280 25281 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25282 tcps->tcps_kstat = NULL; 25283 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25284 25285 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25286 tcps->tcps_mibkp = NULL; 25287 25288 kmem_free(tcps, sizeof (*tcps)); 25289 } 25290 25291 /* 25292 * Generate ISS, taking into account NDD changes may happen halfway through. 25293 * (If the iss is not zero, set it.) 25294 */ 25295 25296 static void 25297 tcp_iss_init(tcp_t *tcp) 25298 { 25299 MD5_CTX context; 25300 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25301 uint32_t answer[4]; 25302 tcp_stack_t *tcps = tcp->tcp_tcps; 25303 25304 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25305 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25306 switch (tcps->tcps_strong_iss) { 25307 case 2: 25308 mutex_enter(&tcps->tcps_iss_key_lock); 25309 context = tcps->tcps_iss_key; 25310 mutex_exit(&tcps->tcps_iss_key_lock); 25311 arg.ports = tcp->tcp_ports; 25312 if (tcp->tcp_ipversion == IPV4_VERSION) { 25313 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25314 &arg.src); 25315 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25316 &arg.dst); 25317 } else { 25318 arg.src = tcp->tcp_ip6h->ip6_src; 25319 arg.dst = tcp->tcp_ip6h->ip6_dst; 25320 } 25321 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25322 MD5Final((uchar_t *)answer, &context); 25323 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25324 /* 25325 * Now that we've hashed into a unique per-connection sequence 25326 * space, add a random increment per strong_iss == 1. So I 25327 * guess we'll have to... 25328 */ 25329 /* FALLTHRU */ 25330 case 1: 25331 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25332 break; 25333 default: 25334 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25335 break; 25336 } 25337 tcp->tcp_valid_bits = TCP_ISS_VALID; 25338 tcp->tcp_fss = tcp->tcp_iss - 1; 25339 tcp->tcp_suna = tcp->tcp_iss; 25340 tcp->tcp_snxt = tcp->tcp_iss + 1; 25341 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25342 tcp->tcp_csuna = tcp->tcp_snxt; 25343 } 25344 25345 /* 25346 * Exported routine for extracting active tcp connection status. 25347 * 25348 * This is used by the Solaris Cluster Networking software to 25349 * gather a list of connections that need to be forwarded to 25350 * specific nodes in the cluster when configuration changes occur. 25351 * 25352 * The callback is invoked for each tcp_t structure. Returning 25353 * non-zero from the callback routine terminates the search. 25354 */ 25355 int 25356 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25357 void *arg) 25358 { 25359 netstack_handle_t nh; 25360 netstack_t *ns; 25361 int ret = 0; 25362 25363 netstack_next_init(&nh); 25364 while ((ns = netstack_next(&nh)) != NULL) { 25365 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25366 ns->netstack_tcp); 25367 netstack_rele(ns); 25368 } 25369 netstack_next_fini(&nh); 25370 return (ret); 25371 } 25372 25373 static int 25374 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25375 tcp_stack_t *tcps) 25376 { 25377 tcp_t *tcp; 25378 cl_tcp_info_t cl_tcpi; 25379 connf_t *connfp; 25380 conn_t *connp; 25381 int i; 25382 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25383 25384 ASSERT(callback != NULL); 25385 25386 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25387 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25388 connp = NULL; 25389 25390 while ((connp = 25391 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25392 25393 tcp = connp->conn_tcp; 25394 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25395 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25396 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25397 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25398 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25399 /* 25400 * The macros tcp_laddr and tcp_faddr give the IPv4 25401 * addresses. They are copied implicitly below as 25402 * mapped addresses. 25403 */ 25404 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25405 if (tcp->tcp_ipversion == IPV4_VERSION) { 25406 cl_tcpi.cl_tcpi_faddr = 25407 tcp->tcp_ipha->ipha_dst; 25408 } else { 25409 cl_tcpi.cl_tcpi_faddr_v6 = 25410 tcp->tcp_ip6h->ip6_dst; 25411 } 25412 25413 /* 25414 * If the callback returns non-zero 25415 * we terminate the traversal. 25416 */ 25417 if ((*callback)(&cl_tcpi, arg) != 0) { 25418 CONN_DEC_REF(tcp->tcp_connp); 25419 return (1); 25420 } 25421 } 25422 } 25423 25424 return (0); 25425 } 25426 25427 /* 25428 * Macros used for accessing the different types of sockaddr 25429 * structures inside a tcp_ioc_abort_conn_t. 25430 */ 25431 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25432 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25433 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25434 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25435 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25436 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25437 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25438 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25439 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25440 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25441 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25442 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25443 25444 /* 25445 * Return the correct error code to mimic the behavior 25446 * of a connection reset. 25447 */ 25448 #define TCP_AC_GET_ERRCODE(state, err) { \ 25449 switch ((state)) { \ 25450 case TCPS_SYN_SENT: \ 25451 case TCPS_SYN_RCVD: \ 25452 (err) = ECONNREFUSED; \ 25453 break; \ 25454 case TCPS_ESTABLISHED: \ 25455 case TCPS_FIN_WAIT_1: \ 25456 case TCPS_FIN_WAIT_2: \ 25457 case TCPS_CLOSE_WAIT: \ 25458 (err) = ECONNRESET; \ 25459 break; \ 25460 case TCPS_CLOSING: \ 25461 case TCPS_LAST_ACK: \ 25462 case TCPS_TIME_WAIT: \ 25463 (err) = 0; \ 25464 break; \ 25465 default: \ 25466 (err) = ENXIO; \ 25467 } \ 25468 } 25469 25470 /* 25471 * Check if a tcp structure matches the info in acp. 25472 */ 25473 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25474 (((acp)->ac_local.ss_family == AF_INET) ? \ 25475 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25476 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25477 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25478 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25479 (TCP_AC_V4LPORT((acp)) == 0 || \ 25480 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25481 (TCP_AC_V4RPORT((acp)) == 0 || \ 25482 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25483 (acp)->ac_start <= (tcp)->tcp_state && \ 25484 (acp)->ac_end >= (tcp)->tcp_state) : \ 25485 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25486 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25487 &(tcp)->tcp_ip_src_v6)) && \ 25488 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25489 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25490 &(tcp)->tcp_remote_v6)) && \ 25491 (TCP_AC_V6LPORT((acp)) == 0 || \ 25492 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25493 (TCP_AC_V6RPORT((acp)) == 0 || \ 25494 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25495 (acp)->ac_start <= (tcp)->tcp_state && \ 25496 (acp)->ac_end >= (tcp)->tcp_state)) 25497 25498 #define TCP_AC_MATCH(acp, tcp) \ 25499 (((acp)->ac_zoneid == ALL_ZONES || \ 25500 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25501 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25502 25503 /* 25504 * Build a message containing a tcp_ioc_abort_conn_t structure 25505 * which is filled in with information from acp and tp. 25506 */ 25507 static mblk_t * 25508 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25509 { 25510 mblk_t *mp; 25511 tcp_ioc_abort_conn_t *tacp; 25512 25513 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25514 if (mp == NULL) 25515 return (NULL); 25516 25517 mp->b_datap->db_type = M_CTL; 25518 25519 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25520 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25521 sizeof (uint32_t)); 25522 25523 tacp->ac_start = acp->ac_start; 25524 tacp->ac_end = acp->ac_end; 25525 tacp->ac_zoneid = acp->ac_zoneid; 25526 25527 if (acp->ac_local.ss_family == AF_INET) { 25528 tacp->ac_local.ss_family = AF_INET; 25529 tacp->ac_remote.ss_family = AF_INET; 25530 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25531 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25532 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25533 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25534 } else { 25535 tacp->ac_local.ss_family = AF_INET6; 25536 tacp->ac_remote.ss_family = AF_INET6; 25537 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25538 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25539 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25540 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25541 } 25542 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25543 return (mp); 25544 } 25545 25546 /* 25547 * Print a tcp_ioc_abort_conn_t structure. 25548 */ 25549 static void 25550 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25551 { 25552 char lbuf[128]; 25553 char rbuf[128]; 25554 sa_family_t af; 25555 in_port_t lport, rport; 25556 ushort_t logflags; 25557 25558 af = acp->ac_local.ss_family; 25559 25560 if (af == AF_INET) { 25561 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25562 lbuf, 128); 25563 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25564 rbuf, 128); 25565 lport = ntohs(TCP_AC_V4LPORT(acp)); 25566 rport = ntohs(TCP_AC_V4RPORT(acp)); 25567 } else { 25568 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25569 lbuf, 128); 25570 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25571 rbuf, 128); 25572 lport = ntohs(TCP_AC_V6LPORT(acp)); 25573 rport = ntohs(TCP_AC_V6RPORT(acp)); 25574 } 25575 25576 logflags = SL_TRACE | SL_NOTE; 25577 /* 25578 * Don't print this message to the console if the operation was done 25579 * to a non-global zone. 25580 */ 25581 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25582 logflags |= SL_CONSOLE; 25583 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25584 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25585 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25586 acp->ac_start, acp->ac_end); 25587 } 25588 25589 /* 25590 * Called inside tcp_rput when a message built using 25591 * tcp_ioctl_abort_build_msg is put into a queue. 25592 * Note that when we get here there is no wildcard in acp any more. 25593 */ 25594 static void 25595 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25596 { 25597 tcp_ioc_abort_conn_t *acp; 25598 25599 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25600 if (tcp->tcp_state <= acp->ac_end) { 25601 /* 25602 * If we get here, we are already on the correct 25603 * squeue. This ioctl follows the following path 25604 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25605 * ->tcp_ioctl_abort->squeue_fill (if on a 25606 * different squeue) 25607 */ 25608 int errcode; 25609 25610 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25611 (void) tcp_clean_death(tcp, errcode, 26); 25612 } 25613 freemsg(mp); 25614 } 25615 25616 /* 25617 * Abort all matching connections on a hash chain. 25618 */ 25619 static int 25620 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25621 boolean_t exact, tcp_stack_t *tcps) 25622 { 25623 int nmatch, err = 0; 25624 tcp_t *tcp; 25625 MBLKP mp, last, listhead = NULL; 25626 conn_t *tconnp; 25627 connf_t *connfp; 25628 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25629 25630 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25631 25632 startover: 25633 nmatch = 0; 25634 25635 mutex_enter(&connfp->connf_lock); 25636 for (tconnp = connfp->connf_head; tconnp != NULL; 25637 tconnp = tconnp->conn_next) { 25638 tcp = tconnp->conn_tcp; 25639 if (TCP_AC_MATCH(acp, tcp)) { 25640 CONN_INC_REF(tcp->tcp_connp); 25641 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25642 if (mp == NULL) { 25643 err = ENOMEM; 25644 CONN_DEC_REF(tcp->tcp_connp); 25645 break; 25646 } 25647 mp->b_prev = (mblk_t *)tcp; 25648 25649 if (listhead == NULL) { 25650 listhead = mp; 25651 last = mp; 25652 } else { 25653 last->b_next = mp; 25654 last = mp; 25655 } 25656 nmatch++; 25657 if (exact) 25658 break; 25659 } 25660 25661 /* Avoid holding lock for too long. */ 25662 if (nmatch >= 500) 25663 break; 25664 } 25665 mutex_exit(&connfp->connf_lock); 25666 25667 /* Pass mp into the correct tcp */ 25668 while ((mp = listhead) != NULL) { 25669 listhead = listhead->b_next; 25670 tcp = (tcp_t *)mp->b_prev; 25671 mp->b_next = mp->b_prev = NULL; 25672 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 25673 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 25674 } 25675 25676 *count += nmatch; 25677 if (nmatch >= 500 && err == 0) 25678 goto startover; 25679 return (err); 25680 } 25681 25682 /* 25683 * Abort all connections that matches the attributes specified in acp. 25684 */ 25685 static int 25686 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25687 { 25688 sa_family_t af; 25689 uint32_t ports; 25690 uint16_t *pports; 25691 int err = 0, count = 0; 25692 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25693 int index = -1; 25694 ushort_t logflags; 25695 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25696 25697 af = acp->ac_local.ss_family; 25698 25699 if (af == AF_INET) { 25700 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25701 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25702 pports = (uint16_t *)&ports; 25703 pports[1] = TCP_AC_V4LPORT(acp); 25704 pports[0] = TCP_AC_V4RPORT(acp); 25705 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25706 } 25707 } else { 25708 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25709 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25710 pports = (uint16_t *)&ports; 25711 pports[1] = TCP_AC_V6LPORT(acp); 25712 pports[0] = TCP_AC_V6RPORT(acp); 25713 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25714 } 25715 } 25716 25717 /* 25718 * For cases where remote addr, local port, and remote port are non- 25719 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25720 */ 25721 if (index != -1) { 25722 err = tcp_ioctl_abort_bucket(acp, index, 25723 &count, exact, tcps); 25724 } else { 25725 /* 25726 * loop through all entries for wildcard case 25727 */ 25728 for (index = 0; 25729 index < ipst->ips_ipcl_conn_fanout_size; 25730 index++) { 25731 err = tcp_ioctl_abort_bucket(acp, index, 25732 &count, exact, tcps); 25733 if (err != 0) 25734 break; 25735 } 25736 } 25737 25738 logflags = SL_TRACE | SL_NOTE; 25739 /* 25740 * Don't print this message to the console if the operation was done 25741 * to a non-global zone. 25742 */ 25743 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25744 logflags |= SL_CONSOLE; 25745 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25746 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25747 if (err == 0 && count == 0) 25748 err = ENOENT; 25749 return (err); 25750 } 25751 25752 /* 25753 * Process the TCP_IOC_ABORT_CONN ioctl request. 25754 */ 25755 static void 25756 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25757 { 25758 int err; 25759 IOCP iocp; 25760 MBLKP mp1; 25761 sa_family_t laf, raf; 25762 tcp_ioc_abort_conn_t *acp; 25763 zone_t *zptr; 25764 conn_t *connp = Q_TO_CONN(q); 25765 zoneid_t zoneid = connp->conn_zoneid; 25766 tcp_t *tcp = connp->conn_tcp; 25767 tcp_stack_t *tcps = tcp->tcp_tcps; 25768 25769 iocp = (IOCP)mp->b_rptr; 25770 25771 if ((mp1 = mp->b_cont) == NULL || 25772 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25773 err = EINVAL; 25774 goto out; 25775 } 25776 25777 /* check permissions */ 25778 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25779 err = EPERM; 25780 goto out; 25781 } 25782 25783 if (mp1->b_cont != NULL) { 25784 freemsg(mp1->b_cont); 25785 mp1->b_cont = NULL; 25786 } 25787 25788 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25789 laf = acp->ac_local.ss_family; 25790 raf = acp->ac_remote.ss_family; 25791 25792 /* check that a zone with the supplied zoneid exists */ 25793 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25794 zptr = zone_find_by_id(zoneid); 25795 if (zptr != NULL) { 25796 zone_rele(zptr); 25797 } else { 25798 err = EINVAL; 25799 goto out; 25800 } 25801 } 25802 25803 /* 25804 * For exclusive stacks we set the zoneid to zero 25805 * to make TCP operate as if in the global zone. 25806 */ 25807 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25808 acp->ac_zoneid = GLOBAL_ZONEID; 25809 25810 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25811 acp->ac_start > acp->ac_end || laf != raf || 25812 (laf != AF_INET && laf != AF_INET6)) { 25813 err = EINVAL; 25814 goto out; 25815 } 25816 25817 tcp_ioctl_abort_dump(acp); 25818 err = tcp_ioctl_abort(acp, tcps); 25819 25820 out: 25821 if (mp1 != NULL) { 25822 freemsg(mp1); 25823 mp->b_cont = NULL; 25824 } 25825 25826 if (err != 0) 25827 miocnak(q, mp, 0, err); 25828 else 25829 miocack(q, mp, 0, 0); 25830 } 25831 25832 /* 25833 * tcp_time_wait_processing() handles processing of incoming packets when 25834 * the tcp is in the TIME_WAIT state. 25835 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25836 * on the time wait list. 25837 */ 25838 void 25839 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25840 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25841 { 25842 int32_t bytes_acked; 25843 int32_t gap; 25844 int32_t rgap; 25845 tcp_opt_t tcpopt; 25846 uint_t flags; 25847 uint32_t new_swnd = 0; 25848 conn_t *connp; 25849 tcp_stack_t *tcps = tcp->tcp_tcps; 25850 25851 BUMP_LOCAL(tcp->tcp_ibsegs); 25852 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 25853 25854 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25855 new_swnd = BE16_TO_U16(tcph->th_win) << 25856 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25857 if (tcp->tcp_snd_ts_ok) { 25858 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25859 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25860 tcp->tcp_rnxt, TH_ACK); 25861 goto done; 25862 } 25863 } 25864 gap = seg_seq - tcp->tcp_rnxt; 25865 rgap = tcp->tcp_rwnd - (gap + seg_len); 25866 if (gap < 0) { 25867 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25868 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25869 (seg_len > -gap ? -gap : seg_len)); 25870 seg_len += gap; 25871 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25872 if (flags & TH_RST) { 25873 goto done; 25874 } 25875 if ((flags & TH_FIN) && seg_len == -1) { 25876 /* 25877 * When TCP receives a duplicate FIN in 25878 * TIME_WAIT state, restart the 2 MSL timer. 25879 * See page 73 in RFC 793. Make sure this TCP 25880 * is already on the TIME_WAIT list. If not, 25881 * just restart the timer. 25882 */ 25883 if (TCP_IS_DETACHED(tcp)) { 25884 if (tcp_time_wait_remove(tcp, NULL) == 25885 B_TRUE) { 25886 tcp_time_wait_append(tcp); 25887 TCP_DBGSTAT(tcps, 25888 tcp_rput_time_wait); 25889 } 25890 } else { 25891 ASSERT(tcp != NULL); 25892 TCP_TIMER_RESTART(tcp, 25893 tcps->tcps_time_wait_interval); 25894 } 25895 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25896 tcp->tcp_rnxt, TH_ACK); 25897 goto done; 25898 } 25899 flags |= TH_ACK_NEEDED; 25900 seg_len = 0; 25901 goto process_ack; 25902 } 25903 25904 /* Fix seg_seq, and chew the gap off the front. */ 25905 seg_seq = tcp->tcp_rnxt; 25906 } 25907 25908 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25909 /* 25910 * Make sure that when we accept the connection, pick 25911 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25912 * old connection. 25913 * 25914 * The next ISS generated is equal to tcp_iss_incr_extra 25915 * + ISS_INCR/2 + other components depending on the 25916 * value of tcp_strong_iss. We pre-calculate the new 25917 * ISS here and compare with tcp_snxt to determine if 25918 * we need to make adjustment to tcp_iss_incr_extra. 25919 * 25920 * The above calculation is ugly and is a 25921 * waste of CPU cycles... 25922 */ 25923 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25924 int32_t adj; 25925 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25926 25927 switch (tcps->tcps_strong_iss) { 25928 case 2: { 25929 /* Add time and MD5 components. */ 25930 uint32_t answer[4]; 25931 struct { 25932 uint32_t ports; 25933 in6_addr_t src; 25934 in6_addr_t dst; 25935 } arg; 25936 MD5_CTX context; 25937 25938 mutex_enter(&tcps->tcps_iss_key_lock); 25939 context = tcps->tcps_iss_key; 25940 mutex_exit(&tcps->tcps_iss_key_lock); 25941 arg.ports = tcp->tcp_ports; 25942 /* We use MAPPED addresses in tcp_iss_init */ 25943 arg.src = tcp->tcp_ip_src_v6; 25944 if (tcp->tcp_ipversion == IPV4_VERSION) { 25945 IN6_IPADDR_TO_V4MAPPED( 25946 tcp->tcp_ipha->ipha_dst, 25947 &arg.dst); 25948 } else { 25949 arg.dst = 25950 tcp->tcp_ip6h->ip6_dst; 25951 } 25952 MD5Update(&context, (uchar_t *)&arg, 25953 sizeof (arg)); 25954 MD5Final((uchar_t *)answer, &context); 25955 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25956 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25957 break; 25958 } 25959 case 1: 25960 /* Add time component and min random (i.e. 1). */ 25961 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25962 break; 25963 default: 25964 /* Add only time component. */ 25965 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25966 break; 25967 } 25968 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25969 /* 25970 * New ISS not guaranteed to be ISS_INCR/2 25971 * ahead of the current tcp_snxt, so add the 25972 * difference to tcp_iss_incr_extra. 25973 */ 25974 tcps->tcps_iss_incr_extra += adj; 25975 } 25976 /* 25977 * If tcp_clean_death() can not perform the task now, 25978 * drop the SYN packet and let the other side re-xmit. 25979 * Otherwise pass the SYN packet back in, since the 25980 * old tcp state has been cleaned up or freed. 25981 */ 25982 if (tcp_clean_death(tcp, 0, 27) == -1) 25983 goto done; 25984 /* 25985 * We will come back to tcp_rput_data 25986 * on the global queue. Packets destined 25987 * for the global queue will be checked 25988 * with global policy. But the policy for 25989 * this packet has already been checked as 25990 * this was destined for the detached 25991 * connection. We need to bypass policy 25992 * check this time by attaching a dummy 25993 * ipsec_in with ipsec_in_dont_check set. 25994 */ 25995 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25996 if (connp != NULL) { 25997 TCP_STAT(tcps, tcp_time_wait_syn_success); 25998 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25999 return; 26000 } 26001 goto done; 26002 } 26003 26004 /* 26005 * rgap is the amount of stuff received out of window. A negative 26006 * value is the amount out of window. 26007 */ 26008 if (rgap < 0) { 26009 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 26010 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 26011 /* Fix seg_len and make sure there is something left. */ 26012 seg_len += rgap; 26013 if (seg_len <= 0) { 26014 if (flags & TH_RST) { 26015 goto done; 26016 } 26017 flags |= TH_ACK_NEEDED; 26018 seg_len = 0; 26019 goto process_ack; 26020 } 26021 } 26022 /* 26023 * Check whether we can update tcp_ts_recent. This test is 26024 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 26025 * Extensions for High Performance: An Update", Internet Draft. 26026 */ 26027 if (tcp->tcp_snd_ts_ok && 26028 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 26029 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 26030 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 26031 tcp->tcp_last_rcv_lbolt = lbolt64; 26032 } 26033 26034 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 26035 /* Always ack out of order packets */ 26036 flags |= TH_ACK_NEEDED; 26037 seg_len = 0; 26038 } else if (seg_len > 0) { 26039 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 26040 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 26041 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 26042 } 26043 if (flags & TH_RST) { 26044 (void) tcp_clean_death(tcp, 0, 28); 26045 goto done; 26046 } 26047 if (flags & TH_SYN) { 26048 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 26049 TH_RST|TH_ACK); 26050 /* 26051 * Do not delete the TCP structure if it is in 26052 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 26053 */ 26054 goto done; 26055 } 26056 process_ack: 26057 if (flags & TH_ACK) { 26058 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 26059 if (bytes_acked <= 0) { 26060 if (bytes_acked == 0 && seg_len == 0 && 26061 new_swnd == tcp->tcp_swnd) 26062 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 26063 } else { 26064 /* Acks something not sent */ 26065 flags |= TH_ACK_NEEDED; 26066 } 26067 } 26068 if (flags & TH_ACK_NEEDED) { 26069 /* 26070 * Time to send an ack for some reason. 26071 */ 26072 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 26073 tcp->tcp_rnxt, TH_ACK); 26074 } 26075 done: 26076 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26077 DB_CKSUMSTART(mp) = 0; 26078 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 26079 TCP_STAT(tcps, tcp_time_wait_syn_fail); 26080 } 26081 freemsg(mp); 26082 } 26083 26084 /* 26085 * Allocate a T_SVR4_OPTMGMT_REQ. 26086 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 26087 * that tcp_rput_other can drop the acks. 26088 */ 26089 static mblk_t * 26090 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 26091 { 26092 mblk_t *mp; 26093 struct T_optmgmt_req *tor; 26094 struct opthdr *oh; 26095 uint_t size; 26096 char *optptr; 26097 26098 size = sizeof (*tor) + sizeof (*oh) + optlen; 26099 mp = allocb(size, BPRI_MED); 26100 if (mp == NULL) 26101 return (NULL); 26102 26103 mp->b_wptr += size; 26104 mp->b_datap->db_type = M_PROTO; 26105 tor = (struct T_optmgmt_req *)mp->b_rptr; 26106 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 26107 tor->MGMT_flags = T_NEGOTIATE; 26108 tor->OPT_length = sizeof (*oh) + optlen; 26109 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 26110 26111 oh = (struct opthdr *)&tor[1]; 26112 oh->level = level; 26113 oh->name = cmd; 26114 oh->len = optlen; 26115 if (optlen != 0) { 26116 optptr = (char *)&oh[1]; 26117 bcopy(opt, optptr, optlen); 26118 } 26119 return (mp); 26120 } 26121 26122 /* 26123 * TCP Timers Implementation. 26124 */ 26125 timeout_id_t 26126 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 26127 { 26128 mblk_t *mp; 26129 tcp_timer_t *tcpt; 26130 tcp_t *tcp = connp->conn_tcp; 26131 tcp_stack_t *tcps = tcp->tcp_tcps; 26132 26133 ASSERT(connp->conn_sqp != NULL); 26134 26135 TCP_DBGSTAT(tcps, tcp_timeout_calls); 26136 26137 if (tcp->tcp_timercache == NULL) { 26138 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 26139 } else { 26140 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 26141 mp = tcp->tcp_timercache; 26142 tcp->tcp_timercache = mp->b_next; 26143 mp->b_next = NULL; 26144 ASSERT(mp->b_wptr == NULL); 26145 } 26146 26147 CONN_INC_REF(connp); 26148 tcpt = (tcp_timer_t *)mp->b_rptr; 26149 tcpt->connp = connp; 26150 tcpt->tcpt_proc = f; 26151 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 26152 return ((timeout_id_t)mp); 26153 } 26154 26155 static void 26156 tcp_timer_callback(void *arg) 26157 { 26158 mblk_t *mp = (mblk_t *)arg; 26159 tcp_timer_t *tcpt; 26160 conn_t *connp; 26161 26162 tcpt = (tcp_timer_t *)mp->b_rptr; 26163 connp = tcpt->connp; 26164 squeue_fill(connp->conn_sqp, mp, 26165 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 26166 } 26167 26168 static void 26169 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 26170 { 26171 tcp_timer_t *tcpt; 26172 conn_t *connp = (conn_t *)arg; 26173 tcp_t *tcp = connp->conn_tcp; 26174 26175 tcpt = (tcp_timer_t *)mp->b_rptr; 26176 ASSERT(connp == tcpt->connp); 26177 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 26178 26179 /* 26180 * If the TCP has reached the closed state, don't proceed any 26181 * further. This TCP logically does not exist on the system. 26182 * tcpt_proc could for example access queues, that have already 26183 * been qprocoff'ed off. Also see comments at the start of tcp_input 26184 */ 26185 if (tcp->tcp_state != TCPS_CLOSED) { 26186 (*tcpt->tcpt_proc)(connp); 26187 } else { 26188 tcp->tcp_timer_tid = 0; 26189 } 26190 tcp_timer_free(connp->conn_tcp, mp); 26191 } 26192 26193 /* 26194 * There is potential race with untimeout and the handler firing at the same 26195 * time. The mblock may be freed by the handler while we are trying to use 26196 * it. But since both should execute on the same squeue, this race should not 26197 * occur. 26198 */ 26199 clock_t 26200 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 26201 { 26202 mblk_t *mp = (mblk_t *)id; 26203 tcp_timer_t *tcpt; 26204 clock_t delta; 26205 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26206 26207 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 26208 26209 if (mp == NULL) 26210 return (-1); 26211 26212 tcpt = (tcp_timer_t *)mp->b_rptr; 26213 ASSERT(tcpt->connp == connp); 26214 26215 delta = untimeout(tcpt->tcpt_tid); 26216 26217 if (delta >= 0) { 26218 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 26219 tcp_timer_free(connp->conn_tcp, mp); 26220 CONN_DEC_REF(connp); 26221 } 26222 26223 return (delta); 26224 } 26225 26226 /* 26227 * Allocate space for the timer event. The allocation looks like mblk, but it is 26228 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26229 * 26230 * Dealing with failures: If we can't allocate from the timer cache we try 26231 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26232 * points to b_rptr. 26233 * If we can't allocate anything using allocb_tryhard(), we perform a last 26234 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26235 * save the actual allocation size in b_datap. 26236 */ 26237 mblk_t * 26238 tcp_timermp_alloc(int kmflags) 26239 { 26240 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26241 kmflags & ~KM_PANIC); 26242 26243 if (mp != NULL) { 26244 mp->b_next = mp->b_prev = NULL; 26245 mp->b_rptr = (uchar_t *)(&mp[1]); 26246 mp->b_wptr = NULL; 26247 mp->b_datap = NULL; 26248 mp->b_queue = NULL; 26249 mp->b_cont = NULL; 26250 } else if (kmflags & KM_PANIC) { 26251 /* 26252 * Failed to allocate memory for the timer. Try allocating from 26253 * dblock caches. 26254 */ 26255 /* ipclassifier calls this from a constructor - hence no tcps */ 26256 TCP_G_STAT(tcp_timermp_allocfail); 26257 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26258 if (mp == NULL) { 26259 size_t size = 0; 26260 /* 26261 * Memory is really low. Try tryhard allocation. 26262 * 26263 * ipclassifier calls this from a constructor - 26264 * hence no tcps 26265 */ 26266 TCP_G_STAT(tcp_timermp_allocdblfail); 26267 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26268 sizeof (tcp_timer_t), &size, kmflags); 26269 mp->b_rptr = (uchar_t *)(&mp[1]); 26270 mp->b_next = mp->b_prev = NULL; 26271 mp->b_wptr = (uchar_t *)-1; 26272 mp->b_datap = (dblk_t *)size; 26273 mp->b_queue = NULL; 26274 mp->b_cont = NULL; 26275 } 26276 ASSERT(mp->b_wptr != NULL); 26277 } 26278 /* ipclassifier calls this from a constructor - hence no tcps */ 26279 TCP_G_DBGSTAT(tcp_timermp_alloced); 26280 26281 return (mp); 26282 } 26283 26284 /* 26285 * Free per-tcp timer cache. 26286 * It can only contain entries from tcp_timercache. 26287 */ 26288 void 26289 tcp_timermp_free(tcp_t *tcp) 26290 { 26291 mblk_t *mp; 26292 26293 while ((mp = tcp->tcp_timercache) != NULL) { 26294 ASSERT(mp->b_wptr == NULL); 26295 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26296 kmem_cache_free(tcp_timercache, mp); 26297 } 26298 } 26299 26300 /* 26301 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26302 * events there already (currently at most two events are cached). 26303 * If the event is not allocated from the timer cache, free it right away. 26304 */ 26305 static void 26306 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26307 { 26308 mblk_t *mp1 = tcp->tcp_timercache; 26309 tcp_stack_t *tcps = tcp->tcp_tcps; 26310 26311 if (mp->b_wptr != NULL) { 26312 /* 26313 * This allocation is not from a timer cache, free it right 26314 * away. 26315 */ 26316 if (mp->b_wptr != (uchar_t *)-1) 26317 freeb(mp); 26318 else 26319 kmem_free(mp, (size_t)mp->b_datap); 26320 } else if (mp1 == NULL || mp1->b_next == NULL) { 26321 /* Cache this timer block for future allocations */ 26322 mp->b_rptr = (uchar_t *)(&mp[1]); 26323 mp->b_next = mp1; 26324 tcp->tcp_timercache = mp; 26325 } else { 26326 kmem_cache_free(tcp_timercache, mp); 26327 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26328 } 26329 } 26330 26331 /* 26332 * End of TCP Timers implementation. 26333 */ 26334 26335 /* 26336 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26337 * on the specified backing STREAMS q. Note, the caller may make the 26338 * decision to call based on the tcp_t.tcp_flow_stopped value which 26339 * when check outside the q's lock is only an advisory check ... 26340 */ 26341 26342 void 26343 tcp_setqfull(tcp_t *tcp) 26344 { 26345 queue_t *q = tcp->tcp_wq; 26346 tcp_stack_t *tcps = tcp->tcp_tcps; 26347 26348 if (!(q->q_flag & QFULL)) { 26349 mutex_enter(QLOCK(q)); 26350 if (!(q->q_flag & QFULL)) { 26351 /* still need to set QFULL */ 26352 q->q_flag |= QFULL; 26353 tcp->tcp_flow_stopped = B_TRUE; 26354 mutex_exit(QLOCK(q)); 26355 TCP_STAT(tcps, tcp_flwctl_on); 26356 } else { 26357 mutex_exit(QLOCK(q)); 26358 } 26359 } 26360 } 26361 26362 void 26363 tcp_clrqfull(tcp_t *tcp) 26364 { 26365 queue_t *q = tcp->tcp_wq; 26366 26367 if (q->q_flag & QFULL) { 26368 mutex_enter(QLOCK(q)); 26369 if (q->q_flag & QFULL) { 26370 q->q_flag &= ~QFULL; 26371 tcp->tcp_flow_stopped = B_FALSE; 26372 mutex_exit(QLOCK(q)); 26373 if (q->q_flag & QWANTW) 26374 qbackenable(q, 0); 26375 } else { 26376 mutex_exit(QLOCK(q)); 26377 } 26378 } 26379 } 26380 26381 26382 /* 26383 * kstats related to squeues i.e. not per IP instance 26384 */ 26385 static void * 26386 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26387 { 26388 kstat_t *ksp; 26389 26390 tcp_g_stat_t template = { 26391 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26392 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26393 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26394 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26395 }; 26396 26397 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26398 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26399 KSTAT_FLAG_VIRTUAL); 26400 26401 if (ksp == NULL) 26402 return (NULL); 26403 26404 bcopy(&template, tcp_g_statp, sizeof (template)); 26405 ksp->ks_data = (void *)tcp_g_statp; 26406 26407 kstat_install(ksp); 26408 return (ksp); 26409 } 26410 26411 static void 26412 tcp_g_kstat_fini(kstat_t *ksp) 26413 { 26414 if (ksp != NULL) { 26415 kstat_delete(ksp); 26416 } 26417 } 26418 26419 26420 static void * 26421 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26422 { 26423 kstat_t *ksp; 26424 26425 tcp_stat_t template = { 26426 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26427 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26428 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26429 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26430 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26431 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26432 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26433 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26434 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26435 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26436 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26437 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26438 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26439 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26440 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26441 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26442 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26443 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26444 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26445 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26446 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26447 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26448 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26449 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26450 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26451 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26452 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26453 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26454 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26455 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26456 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26457 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26458 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26459 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26460 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26461 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26462 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26463 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26464 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26465 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26466 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26467 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26468 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26469 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26470 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26471 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26472 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26473 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26474 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26475 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26476 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26477 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26478 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26479 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26480 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26481 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26482 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26483 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26484 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26485 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26486 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26487 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26488 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26489 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26490 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26491 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26492 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26493 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26494 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26495 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26496 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26497 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26498 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26499 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26500 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26501 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26502 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26503 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26504 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26505 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26506 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26507 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26508 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26509 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26510 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26511 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26512 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26513 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26514 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26515 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26516 }; 26517 26518 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26519 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26520 KSTAT_FLAG_VIRTUAL, stackid); 26521 26522 if (ksp == NULL) 26523 return (NULL); 26524 26525 bcopy(&template, tcps_statisticsp, sizeof (template)); 26526 ksp->ks_data = (void *)tcps_statisticsp; 26527 ksp->ks_private = (void *)(uintptr_t)stackid; 26528 26529 kstat_install(ksp); 26530 return (ksp); 26531 } 26532 26533 static void 26534 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26535 { 26536 if (ksp != NULL) { 26537 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26538 kstat_delete_netstack(ksp, stackid); 26539 } 26540 } 26541 26542 /* 26543 * TCP Kstats implementation 26544 */ 26545 static void * 26546 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26547 { 26548 kstat_t *ksp; 26549 26550 tcp_named_kstat_t template = { 26551 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26552 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26553 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26554 { "maxConn", KSTAT_DATA_INT32, 0 }, 26555 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26556 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26557 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26558 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26559 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26560 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26561 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26562 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26563 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26564 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26565 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26566 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26567 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26568 { "outAck", KSTAT_DATA_UINT32, 0 }, 26569 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26570 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26571 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26572 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26573 { "outControl", KSTAT_DATA_UINT32, 0 }, 26574 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26575 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26576 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26577 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26578 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26579 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26580 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26581 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26582 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26583 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26584 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26585 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26586 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26587 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26588 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26589 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26590 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26591 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26592 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26593 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26594 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26595 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26596 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26597 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26598 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26599 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26600 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26601 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26602 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26603 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26604 }; 26605 26606 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26607 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26608 26609 if (ksp == NULL) 26610 return (NULL); 26611 26612 template.rtoAlgorithm.value.ui32 = 4; 26613 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26614 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26615 template.maxConn.value.i32 = -1; 26616 26617 bcopy(&template, ksp->ks_data, sizeof (template)); 26618 ksp->ks_update = tcp_kstat_update; 26619 ksp->ks_private = (void *)(uintptr_t)stackid; 26620 26621 kstat_install(ksp); 26622 return (ksp); 26623 } 26624 26625 static void 26626 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26627 { 26628 if (ksp != NULL) { 26629 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26630 kstat_delete_netstack(ksp, stackid); 26631 } 26632 } 26633 26634 static int 26635 tcp_kstat_update(kstat_t *kp, int rw) 26636 { 26637 tcp_named_kstat_t *tcpkp; 26638 tcp_t *tcp; 26639 connf_t *connfp; 26640 conn_t *connp; 26641 int i; 26642 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26643 netstack_t *ns; 26644 tcp_stack_t *tcps; 26645 ip_stack_t *ipst; 26646 26647 if ((kp == NULL) || (kp->ks_data == NULL)) 26648 return (EIO); 26649 26650 if (rw == KSTAT_WRITE) 26651 return (EACCES); 26652 26653 ns = netstack_find_by_stackid(stackid); 26654 if (ns == NULL) 26655 return (-1); 26656 tcps = ns->netstack_tcp; 26657 if (tcps == NULL) { 26658 netstack_rele(ns); 26659 return (-1); 26660 } 26661 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26662 26663 tcpkp->currEstab.value.ui32 = 0; 26664 26665 ipst = ns->netstack_ip; 26666 26667 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26668 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26669 connp = NULL; 26670 while ((connp = 26671 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26672 tcp = connp->conn_tcp; 26673 switch (tcp_snmp_state(tcp)) { 26674 case MIB2_TCP_established: 26675 case MIB2_TCP_closeWait: 26676 tcpkp->currEstab.value.ui32++; 26677 break; 26678 } 26679 } 26680 } 26681 26682 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26683 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26684 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26685 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26686 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26687 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26688 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26689 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26690 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26691 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26692 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26693 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26694 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26695 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26696 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26697 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26698 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26699 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26700 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26701 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26702 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26703 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26704 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26705 tcpkp->inDataInorderSegs.value.ui32 = 26706 tcps->tcps_mib.tcpInDataInorderSegs; 26707 tcpkp->inDataInorderBytes.value.ui32 = 26708 tcps->tcps_mib.tcpInDataInorderBytes; 26709 tcpkp->inDataUnorderSegs.value.ui32 = 26710 tcps->tcps_mib.tcpInDataUnorderSegs; 26711 tcpkp->inDataUnorderBytes.value.ui32 = 26712 tcps->tcps_mib.tcpInDataUnorderBytes; 26713 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26714 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26715 tcpkp->inDataPartDupSegs.value.ui32 = 26716 tcps->tcps_mib.tcpInDataPartDupSegs; 26717 tcpkp->inDataPartDupBytes.value.ui32 = 26718 tcps->tcps_mib.tcpInDataPartDupBytes; 26719 tcpkp->inDataPastWinSegs.value.ui32 = 26720 tcps->tcps_mib.tcpInDataPastWinSegs; 26721 tcpkp->inDataPastWinBytes.value.ui32 = 26722 tcps->tcps_mib.tcpInDataPastWinBytes; 26723 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26724 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26725 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26726 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26727 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26728 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26729 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26730 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26731 tcpkp->timKeepaliveProbe.value.ui32 = 26732 tcps->tcps_mib.tcpTimKeepaliveProbe; 26733 tcpkp->timKeepaliveDrop.value.ui32 = 26734 tcps->tcps_mib.tcpTimKeepaliveDrop; 26735 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26736 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26737 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26738 tcpkp->outSackRetransSegs.value.ui32 = 26739 tcps->tcps_mib.tcpOutSackRetransSegs; 26740 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26741 26742 netstack_rele(ns); 26743 return (0); 26744 } 26745 26746 void 26747 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26748 { 26749 uint16_t hdr_len; 26750 ipha_t *ipha; 26751 uint8_t *nexthdrp; 26752 tcph_t *tcph; 26753 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26754 26755 /* Already has an eager */ 26756 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26757 TCP_STAT(tcps, tcp_reinput_syn); 26758 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 26759 connp, SQTAG_TCP_REINPUT_EAGER); 26760 return; 26761 } 26762 26763 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26764 case IPV4_VERSION: 26765 ipha = (ipha_t *)mp->b_rptr; 26766 hdr_len = IPH_HDR_LENGTH(ipha); 26767 break; 26768 case IPV6_VERSION: 26769 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26770 &hdr_len, &nexthdrp)) { 26771 CONN_DEC_REF(connp); 26772 freemsg(mp); 26773 return; 26774 } 26775 break; 26776 } 26777 26778 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26779 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26780 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26781 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26782 } 26783 26784 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 26785 SQTAG_TCP_REINPUT); 26786 } 26787 26788 static squeue_func_t 26789 tcp_squeue_switch(int val) 26790 { 26791 squeue_func_t rval = squeue_fill; 26792 26793 switch (val) { 26794 case 1: 26795 rval = squeue_enter_nodrain; 26796 break; 26797 case 2: 26798 rval = squeue_enter; 26799 break; 26800 default: 26801 break; 26802 } 26803 return (rval); 26804 } 26805 26806 /* 26807 * This is called once for each squeue - globally for all stack 26808 * instances. 26809 */ 26810 static void 26811 tcp_squeue_add(squeue_t *sqp) 26812 { 26813 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26814 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26815 26816 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26817 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 26818 sqp, TCP_TIME_WAIT_DELAY); 26819 if (tcp_free_list_max_cnt == 0) { 26820 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26821 max_ncpus : boot_max_ncpus); 26822 26823 /* 26824 * Limit number of entries to 1% of availble memory / tcp_ncpus 26825 */ 26826 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26827 (tcp_ncpus * sizeof (tcp_t) * 100); 26828 } 26829 tcp_time_wait->tcp_free_list_cnt = 0; 26830 } 26831