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); 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 *); 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. We 1844 * increment tcp_closemp_used to record any 1845 * attempt to reuse tcp_closemp while it is 1846 * still in use. 1847 */ 1848 1849 if (tcp->tcp_closemp.b_prev == NULL) 1850 tcp->tcp_closemp_used = 1; 1851 else 1852 tcp->tcp_closemp_used++; 1853 1854 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1855 mp = &tcp->tcp_closemp; 1856 squeue_fill(connp->conn_sqp, mp, 1857 tcp_timewait_output, connp, 1858 SQTAG_TCP_TIMEWAIT); 1859 } 1860 } else { 1861 mutex_enter(&connp->conn_lock); 1862 CONN_INC_REF_LOCKED(connp); 1863 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1864 mutex_exit(&connp->conn_lock); 1865 /* 1866 * We can reuse the closemp here since conn has 1867 * detached (otherwise we wouldn't even be in 1868 * time_wait list). tcp_closemp_used can safely 1869 * be changed without taking a lock as no other 1870 * thread can concurrently access it at this 1871 * point in the connection lifecycle. We 1872 * increment tcp_closemp_used to record any 1873 * attempt to reuse tcp_closemp while it is 1874 * still in use. 1875 */ 1876 1877 if (tcp->tcp_closemp.b_prev == NULL) 1878 tcp->tcp_closemp_used = 1; 1879 else 1880 tcp->tcp_closemp_used++; 1881 1882 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1883 mp = &tcp->tcp_closemp; 1884 squeue_fill(connp->conn_sqp, mp, 1885 tcp_timewait_output, connp, 0); 1886 } 1887 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1888 } 1889 1890 if (tcp_time_wait->tcp_free_list != NULL) 1891 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1892 1893 tcp_time_wait->tcp_time_wait_tid = 1894 timeout(tcp_time_wait_collector, sqp, TCP_TIME_WAIT_DELAY); 1895 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1896 } 1897 /* 1898 * Reply to a clients T_CONN_RES TPI message. This function 1899 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1900 * on the acceptor STREAM and processed in tcp_wput_accept(). 1901 * Read the block comment on top of tcp_conn_request(). 1902 */ 1903 static void 1904 tcp_accept(tcp_t *listener, mblk_t *mp) 1905 { 1906 tcp_t *acceptor; 1907 tcp_t *eager; 1908 tcp_t *tcp; 1909 struct T_conn_res *tcr; 1910 t_uscalar_t acceptor_id; 1911 t_scalar_t seqnum; 1912 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1913 mblk_t *ok_mp; 1914 mblk_t *mp1; 1915 tcp_stack_t *tcps = listener->tcp_tcps; 1916 1917 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1918 tcp_err_ack(listener, mp, TPROTO, 0); 1919 return; 1920 } 1921 tcr = (struct T_conn_res *)mp->b_rptr; 1922 1923 /* 1924 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1925 * read side queue of the streams device underneath us i.e. the 1926 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1927 * look it up in the queue_hash. Under LP64 it sends down the 1928 * minor_t of the accepting endpoint. 1929 * 1930 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1931 * fanout hash lock is held. 1932 * This prevents any thread from entering the acceptor queue from 1933 * below (since it has not been hard bound yet i.e. any inbound 1934 * packets will arrive on the listener or default tcp queue and 1935 * go through tcp_lookup). 1936 * The CONN_INC_REF will prevent the acceptor from closing. 1937 * 1938 * XXX It is still possible for a tli application to send down data 1939 * on the accepting stream while another thread calls t_accept. 1940 * This should not be a problem for well-behaved applications since 1941 * the T_OK_ACK is sent after the queue swapping is completed. 1942 * 1943 * If the accepting fd is the same as the listening fd, avoid 1944 * queue hash lookup since that will return an eager listener in a 1945 * already established state. 1946 */ 1947 acceptor_id = tcr->ACCEPTOR_id; 1948 mutex_enter(&listener->tcp_eager_lock); 1949 if (listener->tcp_acceptor_id == acceptor_id) { 1950 eager = listener->tcp_eager_next_q; 1951 /* only count how many T_CONN_INDs so don't count q0 */ 1952 if ((listener->tcp_conn_req_cnt_q != 1) || 1953 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1954 mutex_exit(&listener->tcp_eager_lock); 1955 tcp_err_ack(listener, mp, TBADF, 0); 1956 return; 1957 } 1958 if (listener->tcp_conn_req_cnt_q0 != 0) { 1959 /* Throw away all the eagers on q0. */ 1960 tcp_eager_cleanup(listener, 1); 1961 } 1962 if (listener->tcp_syn_defense) { 1963 listener->tcp_syn_defense = B_FALSE; 1964 if (listener->tcp_ip_addr_cache != NULL) { 1965 kmem_free(listener->tcp_ip_addr_cache, 1966 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1967 listener->tcp_ip_addr_cache = NULL; 1968 } 1969 } 1970 /* 1971 * Transfer tcp_conn_req_max to the eager so that when 1972 * a disconnect occurs we can revert the endpoint to the 1973 * listen state. 1974 */ 1975 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1976 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1977 /* 1978 * Get a reference on the acceptor just like the 1979 * tcp_acceptor_hash_lookup below. 1980 */ 1981 acceptor = listener; 1982 CONN_INC_REF(acceptor->tcp_connp); 1983 } else { 1984 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1985 if (acceptor == NULL) { 1986 if (listener->tcp_debug) { 1987 (void) strlog(TCP_MOD_ID, 0, 1, 1988 SL_ERROR|SL_TRACE, 1989 "tcp_accept: did not find acceptor 0x%x\n", 1990 acceptor_id); 1991 } 1992 mutex_exit(&listener->tcp_eager_lock); 1993 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 1994 return; 1995 } 1996 /* 1997 * Verify acceptor state. The acceptable states for an acceptor 1998 * include TCPS_IDLE and TCPS_BOUND. 1999 */ 2000 switch (acceptor->tcp_state) { 2001 case TCPS_IDLE: 2002 /* FALLTHRU */ 2003 case TCPS_BOUND: 2004 break; 2005 default: 2006 CONN_DEC_REF(acceptor->tcp_connp); 2007 mutex_exit(&listener->tcp_eager_lock); 2008 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2009 return; 2010 } 2011 } 2012 2013 /* The listener must be in TCPS_LISTEN */ 2014 if (listener->tcp_state != TCPS_LISTEN) { 2015 CONN_DEC_REF(acceptor->tcp_connp); 2016 mutex_exit(&listener->tcp_eager_lock); 2017 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2018 return; 2019 } 2020 2021 /* 2022 * Rendezvous with an eager connection request packet hanging off 2023 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2024 * tcp structure when the connection packet arrived in 2025 * tcp_conn_request(). 2026 */ 2027 seqnum = tcr->SEQ_number; 2028 eager = listener; 2029 do { 2030 eager = eager->tcp_eager_next_q; 2031 if (eager == NULL) { 2032 CONN_DEC_REF(acceptor->tcp_connp); 2033 mutex_exit(&listener->tcp_eager_lock); 2034 tcp_err_ack(listener, mp, TBADSEQ, 0); 2035 return; 2036 } 2037 } while (eager->tcp_conn_req_seqnum != seqnum); 2038 mutex_exit(&listener->tcp_eager_lock); 2039 2040 /* 2041 * At this point, both acceptor and listener have 2 ref 2042 * that they begin with. Acceptor has one additional ref 2043 * we placed in lookup while listener has 3 additional 2044 * ref for being behind the squeue (tcp_accept() is 2045 * done on listener's squeue); being in classifier hash; 2046 * and eager's ref on listener. 2047 */ 2048 ASSERT(listener->tcp_connp->conn_ref >= 5); 2049 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2050 2051 /* 2052 * The eager at this point is set in its own squeue and 2053 * could easily have been killed (tcp_accept_finish will 2054 * deal with that) because of a TH_RST so we can only 2055 * ASSERT for a single ref. 2056 */ 2057 ASSERT(eager->tcp_connp->conn_ref >= 1); 2058 2059 /* Pre allocate the stroptions mblk also */ 2060 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2061 if (opt_mp == NULL) { 2062 CONN_DEC_REF(acceptor->tcp_connp); 2063 CONN_DEC_REF(eager->tcp_connp); 2064 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2065 return; 2066 } 2067 DB_TYPE(opt_mp) = M_SETOPTS; 2068 opt_mp->b_wptr += sizeof (struct stroptions); 2069 2070 /* 2071 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2072 * from listener to acceptor. The message is chained on opt_mp 2073 * which will be sent onto eager's squeue. 2074 */ 2075 if (listener->tcp_bound_if != 0) { 2076 /* allocate optmgmt req */ 2077 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2078 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2079 sizeof (int)); 2080 if (mp1 != NULL) 2081 linkb(opt_mp, mp1); 2082 } 2083 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2084 uint_t on = 1; 2085 2086 /* allocate optmgmt req */ 2087 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2088 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2089 if (mp1 != NULL) 2090 linkb(opt_mp, mp1); 2091 } 2092 2093 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2094 if ((mp1 = copymsg(mp)) == NULL) { 2095 CONN_DEC_REF(acceptor->tcp_connp); 2096 CONN_DEC_REF(eager->tcp_connp); 2097 freemsg(opt_mp); 2098 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2099 return; 2100 } 2101 2102 tcr = (struct T_conn_res *)mp1->b_rptr; 2103 2104 /* 2105 * This is an expanded version of mi_tpi_ok_ack_alloc() 2106 * which allocates a larger mblk and appends the new 2107 * local address to the ok_ack. The address is copied by 2108 * soaccept() for getsockname(). 2109 */ 2110 { 2111 int extra; 2112 2113 extra = (eager->tcp_family == AF_INET) ? 2114 sizeof (sin_t) : sizeof (sin6_t); 2115 2116 /* 2117 * Try to re-use mp, if possible. Otherwise, allocate 2118 * an mblk and return it as ok_mp. In any case, mp 2119 * is no longer usable upon return. 2120 */ 2121 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2122 CONN_DEC_REF(acceptor->tcp_connp); 2123 CONN_DEC_REF(eager->tcp_connp); 2124 freemsg(opt_mp); 2125 /* Original mp has been freed by now, so use mp1 */ 2126 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2127 return; 2128 } 2129 2130 mp = NULL; /* We should never use mp after this point */ 2131 2132 switch (extra) { 2133 case sizeof (sin_t): { 2134 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2135 2136 ok_mp->b_wptr += extra; 2137 sin->sin_family = AF_INET; 2138 sin->sin_port = eager->tcp_lport; 2139 sin->sin_addr.s_addr = 2140 eager->tcp_ipha->ipha_src; 2141 break; 2142 } 2143 case sizeof (sin6_t): { 2144 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2145 2146 ok_mp->b_wptr += extra; 2147 sin6->sin6_family = AF_INET6; 2148 sin6->sin6_port = eager->tcp_lport; 2149 if (eager->tcp_ipversion == IPV4_VERSION) { 2150 sin6->sin6_flowinfo = 0; 2151 IN6_IPADDR_TO_V4MAPPED( 2152 eager->tcp_ipha->ipha_src, 2153 &sin6->sin6_addr); 2154 } else { 2155 ASSERT(eager->tcp_ip6h != NULL); 2156 sin6->sin6_flowinfo = 2157 eager->tcp_ip6h->ip6_vcf & 2158 ~IPV6_VERS_AND_FLOW_MASK; 2159 sin6->sin6_addr = 2160 eager->tcp_ip6h->ip6_src; 2161 } 2162 break; 2163 } 2164 default: 2165 break; 2166 } 2167 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2168 } 2169 2170 /* 2171 * If there are no options we know that the T_CONN_RES will 2172 * succeed. However, we can't send the T_OK_ACK upstream until 2173 * the tcp_accept_swap is done since it would be dangerous to 2174 * let the application start using the new fd prior to the swap. 2175 */ 2176 tcp_accept_swap(listener, acceptor, eager); 2177 2178 /* 2179 * tcp_accept_swap unlinks eager from listener but does not drop 2180 * the eager's reference on the listener. 2181 */ 2182 ASSERT(eager->tcp_listener == NULL); 2183 ASSERT(listener->tcp_connp->conn_ref >= 5); 2184 2185 /* 2186 * The eager is now associated with its own queue. Insert in 2187 * the hash so that the connection can be reused for a future 2188 * T_CONN_RES. 2189 */ 2190 tcp_acceptor_hash_insert(acceptor_id, eager); 2191 2192 /* 2193 * We now do the processing of options with T_CONN_RES. 2194 * We delay till now since we wanted to have queue to pass to 2195 * option processing routines that points back to the right 2196 * instance structure which does not happen until after 2197 * tcp_accept_swap(). 2198 * 2199 * Note: 2200 * The sanity of the logic here assumes that whatever options 2201 * are appropriate to inherit from listner=>eager are done 2202 * before this point, and whatever were to be overridden (or not) 2203 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2204 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2205 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2206 * This may not be true at this point in time but can be fixed 2207 * independently. This option processing code starts with 2208 * the instantiated acceptor instance and the final queue at 2209 * this point. 2210 */ 2211 2212 if (tcr->OPT_length != 0) { 2213 /* Options to process */ 2214 int t_error = 0; 2215 int sys_error = 0; 2216 int do_disconnect = 0; 2217 2218 if (tcp_conprim_opt_process(eager, mp1, 2219 &do_disconnect, &t_error, &sys_error) < 0) { 2220 eager->tcp_accept_error = 1; 2221 if (do_disconnect) { 2222 /* 2223 * An option failed which does not allow 2224 * connection to be accepted. 2225 * 2226 * We allow T_CONN_RES to succeed and 2227 * put a T_DISCON_IND on the eager queue. 2228 */ 2229 ASSERT(t_error == 0 && sys_error == 0); 2230 eager->tcp_send_discon_ind = 1; 2231 } else { 2232 ASSERT(t_error != 0); 2233 freemsg(ok_mp); 2234 /* 2235 * Original mp was either freed or set 2236 * to ok_mp above, so use mp1 instead. 2237 */ 2238 tcp_err_ack(listener, mp1, t_error, sys_error); 2239 goto finish; 2240 } 2241 } 2242 /* 2243 * Most likely success in setting options (except if 2244 * eager->tcp_send_discon_ind set). 2245 * mp1 option buffer represented by OPT_length/offset 2246 * potentially modified and contains results of setting 2247 * options at this point 2248 */ 2249 } 2250 2251 /* We no longer need mp1, since all options processing has passed */ 2252 freemsg(mp1); 2253 2254 putnext(listener->tcp_rq, ok_mp); 2255 2256 mutex_enter(&listener->tcp_eager_lock); 2257 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2258 tcp_t *tail; 2259 mblk_t *conn_ind; 2260 2261 /* 2262 * This path should not be executed if listener and 2263 * acceptor streams are the same. 2264 */ 2265 ASSERT(listener != acceptor); 2266 2267 tcp = listener->tcp_eager_prev_q0; 2268 /* 2269 * listener->tcp_eager_prev_q0 points to the TAIL of the 2270 * deferred T_conn_ind queue. We need to get to the head of 2271 * the queue in order to send up T_conn_ind the same order as 2272 * how the 3WHS is completed. 2273 */ 2274 while (tcp != listener) { 2275 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2276 break; 2277 else 2278 tcp = tcp->tcp_eager_prev_q0; 2279 } 2280 ASSERT(tcp != listener); 2281 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2282 ASSERT(conn_ind != NULL); 2283 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2284 2285 /* Move from q0 to q */ 2286 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2287 listener->tcp_conn_req_cnt_q0--; 2288 listener->tcp_conn_req_cnt_q++; 2289 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2290 tcp->tcp_eager_prev_q0; 2291 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2292 tcp->tcp_eager_next_q0; 2293 tcp->tcp_eager_prev_q0 = NULL; 2294 tcp->tcp_eager_next_q0 = NULL; 2295 tcp->tcp_conn_def_q0 = B_FALSE; 2296 2297 /* Make sure the tcp isn't in the list of droppables */ 2298 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2299 tcp->tcp_eager_prev_drop_q0 == NULL); 2300 2301 /* 2302 * Insert at end of the queue because sockfs sends 2303 * down T_CONN_RES in chronological order. Leaving 2304 * the older conn indications at front of the queue 2305 * helps reducing search time. 2306 */ 2307 tail = listener->tcp_eager_last_q; 2308 if (tail != NULL) 2309 tail->tcp_eager_next_q = tcp; 2310 else 2311 listener->tcp_eager_next_q = tcp; 2312 listener->tcp_eager_last_q = tcp; 2313 tcp->tcp_eager_next_q = NULL; 2314 mutex_exit(&listener->tcp_eager_lock); 2315 putnext(tcp->tcp_rq, conn_ind); 2316 } else { 2317 mutex_exit(&listener->tcp_eager_lock); 2318 } 2319 2320 /* 2321 * Done with the acceptor - free it 2322 * 2323 * Note: from this point on, no access to listener should be made 2324 * as listener can be equal to acceptor. 2325 */ 2326 finish: 2327 ASSERT(acceptor->tcp_detached); 2328 ASSERT(tcps->tcps_g_q != NULL); 2329 acceptor->tcp_rq = tcps->tcps_g_q; 2330 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2331 (void) tcp_clean_death(acceptor, 0, 2); 2332 CONN_DEC_REF(acceptor->tcp_connp); 2333 2334 /* 2335 * In case we already received a FIN we have to make tcp_rput send 2336 * the ordrel_ind. This will also send up a window update if the window 2337 * has opened up. 2338 * 2339 * In the normal case of a successful connection acceptance 2340 * we give the O_T_BIND_REQ to the read side put procedure as an 2341 * indication that this was just accepted. This tells tcp_rput to 2342 * pass up any data queued in tcp_rcv_list. 2343 * 2344 * In the fringe case where options sent with T_CONN_RES failed and 2345 * we required, we would be indicating a T_DISCON_IND to blow 2346 * away this connection. 2347 */ 2348 2349 /* 2350 * XXX: we currently have a problem if XTI application closes the 2351 * acceptor stream in between. This problem exists in on10-gate also 2352 * and is well know but nothing can be done short of major rewrite 2353 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2354 * eager same squeue as listener (we can distinguish non socket 2355 * listeners at the time of handling a SYN in tcp_conn_request) 2356 * and do most of the work that tcp_accept_finish does here itself 2357 * and then get behind the acceptor squeue to access the acceptor 2358 * queue. 2359 */ 2360 /* 2361 * We already have a ref on tcp so no need to do one before squeue_fill 2362 */ 2363 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2364 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2365 } 2366 2367 /* 2368 * Swap information between the eager and acceptor for a TLI/XTI client. 2369 * The sockfs accept is done on the acceptor stream and control goes 2370 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2371 * called. In either case, both the eager and listener are in their own 2372 * perimeter (squeue) and the code has to deal with potential race. 2373 * 2374 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2375 */ 2376 static void 2377 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2378 { 2379 conn_t *econnp, *aconnp; 2380 2381 ASSERT(eager->tcp_rq == listener->tcp_rq); 2382 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2383 ASSERT(!eager->tcp_hard_bound); 2384 ASSERT(!TCP_IS_SOCKET(acceptor)); 2385 ASSERT(!TCP_IS_SOCKET(eager)); 2386 ASSERT(!TCP_IS_SOCKET(listener)); 2387 2388 acceptor->tcp_detached = B_TRUE; 2389 /* 2390 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2391 * the acceptor id. 2392 */ 2393 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2394 2395 /* remove eager from listen list... */ 2396 mutex_enter(&listener->tcp_eager_lock); 2397 tcp_eager_unlink(eager); 2398 ASSERT(eager->tcp_eager_next_q == NULL && 2399 eager->tcp_eager_last_q == NULL); 2400 ASSERT(eager->tcp_eager_next_q0 == NULL && 2401 eager->tcp_eager_prev_q0 == NULL); 2402 mutex_exit(&listener->tcp_eager_lock); 2403 eager->tcp_rq = acceptor->tcp_rq; 2404 eager->tcp_wq = acceptor->tcp_wq; 2405 2406 econnp = eager->tcp_connp; 2407 aconnp = acceptor->tcp_connp; 2408 2409 eager->tcp_rq->q_ptr = econnp; 2410 eager->tcp_wq->q_ptr = econnp; 2411 2412 /* 2413 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2414 * which might be a different squeue from our peer TCP instance. 2415 * For TCP Fusion, the peer expects that whenever tcp_detached is 2416 * clear, our TCP queues point to the acceptor's queues. Thus, use 2417 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2418 * above reach global visibility prior to the clearing of tcp_detached. 2419 */ 2420 membar_producer(); 2421 eager->tcp_detached = B_FALSE; 2422 2423 ASSERT(eager->tcp_ack_tid == 0); 2424 2425 econnp->conn_dev = aconnp->conn_dev; 2426 if (eager->tcp_cred != NULL) 2427 crfree(eager->tcp_cred); 2428 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2429 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2430 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2431 2432 aconnp->conn_cred = NULL; 2433 2434 econnp->conn_zoneid = aconnp->conn_zoneid; 2435 econnp->conn_allzones = aconnp->conn_allzones; 2436 2437 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2438 aconnp->conn_mac_exempt = B_FALSE; 2439 2440 ASSERT(aconnp->conn_peercred == NULL); 2441 2442 /* Do the IPC initialization */ 2443 CONN_INC_REF(econnp); 2444 2445 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2446 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2447 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2448 econnp->conn_ulp = aconnp->conn_ulp; 2449 2450 /* Done with old IPC. Drop its ref on its connp */ 2451 CONN_DEC_REF(aconnp); 2452 } 2453 2454 2455 /* 2456 * Adapt to the information, such as rtt and rtt_sd, provided from the 2457 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2458 * 2459 * Checks for multicast and broadcast destination address. 2460 * Returns zero on failure; non-zero if ok. 2461 * 2462 * Note that the MSS calculation here is based on the info given in 2463 * the IRE. We do not do any calculation based on TCP options. They 2464 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2465 * knows which options to use. 2466 * 2467 * Note on how TCP gets its parameters for a connection. 2468 * 2469 * When a tcp_t structure is allocated, it gets all the default parameters. 2470 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2471 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2472 * default. But if there is an associated tcp_host_param, it will override 2473 * the metrics. 2474 * 2475 * An incoming SYN with a multicast or broadcast destination address, is dropped 2476 * in 1 of 2 places. 2477 * 2478 * 1. If the packet was received over the wire it is dropped in 2479 * ip_rput_process_broadcast() 2480 * 2481 * 2. If the packet was received through internal IP loopback, i.e. the packet 2482 * was generated and received on the same machine, it is dropped in 2483 * ip_wput_local() 2484 * 2485 * An incoming SYN with a multicast or broadcast source address is always 2486 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2487 * reject an attempt to connect to a broadcast or multicast (destination) 2488 * address. 2489 */ 2490 static int 2491 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2492 { 2493 tcp_hsp_t *hsp; 2494 ire_t *ire; 2495 ire_t *sire = NULL; 2496 iulp_t *ire_uinfo = NULL; 2497 uint32_t mss_max; 2498 uint32_t mss; 2499 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2500 conn_t *connp = tcp->tcp_connp; 2501 boolean_t ire_cacheable = B_FALSE; 2502 zoneid_t zoneid = connp->conn_zoneid; 2503 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2504 MATCH_IRE_SECATTR; 2505 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2506 ill_t *ill = NULL; 2507 boolean_t incoming = (ire_mp == NULL); 2508 tcp_stack_t *tcps = tcp->tcp_tcps; 2509 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2510 2511 ASSERT(connp->conn_ire_cache == NULL); 2512 2513 if (tcp->tcp_ipversion == IPV4_VERSION) { 2514 2515 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2516 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2517 return (0); 2518 } 2519 /* 2520 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2521 * for the destination with the nexthop as gateway. 2522 * ire_ctable_lookup() is used because this particular 2523 * ire, if it exists, will be marked private. 2524 * If that is not available, use the interface ire 2525 * for the nexthop. 2526 * 2527 * TSol: tcp_update_label will detect label mismatches based 2528 * only on the destination's label, but that would not 2529 * detect label mismatches based on the security attributes 2530 * of routes or next hop gateway. Hence we need to pass the 2531 * label to ire_ftable_lookup below in order to locate the 2532 * right prefix (and/or) ire cache. Similarly we also need 2533 * pass the label to the ire_cache_lookup below to locate 2534 * the right ire that also matches on the label. 2535 */ 2536 if (tcp->tcp_connp->conn_nexthop_set) { 2537 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2538 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2539 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2540 ipst); 2541 if (ire == NULL) { 2542 ire = ire_ftable_lookup( 2543 tcp->tcp_connp->conn_nexthop_v4, 2544 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2545 tsl, match_flags, ipst); 2546 if (ire == NULL) 2547 return (0); 2548 } else { 2549 ire_uinfo = &ire->ire_uinfo; 2550 } 2551 } else { 2552 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2553 zoneid, tsl, ipst); 2554 if (ire != NULL) { 2555 ire_cacheable = B_TRUE; 2556 ire_uinfo = (ire_mp != NULL) ? 2557 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2558 &ire->ire_uinfo; 2559 2560 } else { 2561 if (ire_mp == NULL) { 2562 ire = ire_ftable_lookup( 2563 tcp->tcp_connp->conn_rem, 2564 0, 0, 0, NULL, &sire, zoneid, 0, 2565 tsl, (MATCH_IRE_RECURSIVE | 2566 MATCH_IRE_DEFAULT), ipst); 2567 if (ire == NULL) 2568 return (0); 2569 ire_uinfo = (sire != NULL) ? 2570 &sire->ire_uinfo : 2571 &ire->ire_uinfo; 2572 } else { 2573 ire = (ire_t *)ire_mp->b_rptr; 2574 ire_uinfo = 2575 &((ire_t *) 2576 ire_mp->b_rptr)->ire_uinfo; 2577 } 2578 } 2579 } 2580 ASSERT(ire != NULL); 2581 2582 if ((ire->ire_src_addr == INADDR_ANY) || 2583 (ire->ire_type & IRE_BROADCAST)) { 2584 /* 2585 * ire->ire_mp is non null when ire_mp passed in is used 2586 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2587 */ 2588 if (ire->ire_mp == NULL) 2589 ire_refrele(ire); 2590 if (sire != NULL) 2591 ire_refrele(sire); 2592 return (0); 2593 } 2594 2595 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2596 ipaddr_t src_addr; 2597 2598 /* 2599 * ip_bind_connected() has stored the correct source 2600 * address in conn_src. 2601 */ 2602 src_addr = tcp->tcp_connp->conn_src; 2603 tcp->tcp_ipha->ipha_src = src_addr; 2604 /* 2605 * Copy of the src addr. in tcp_t is needed 2606 * for the lookup funcs. 2607 */ 2608 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2609 } 2610 /* 2611 * Set the fragment bit so that IP will tell us if the MTU 2612 * should change. IP tells us the latest setting of 2613 * ip_path_mtu_discovery through ire_frag_flag. 2614 */ 2615 if (ipst->ips_ip_path_mtu_discovery) { 2616 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2617 htons(IPH_DF); 2618 } 2619 /* 2620 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2621 * for IP_NEXTHOP. No cache ire has been found for the 2622 * destination and we are working with the nexthop's 2623 * interface ire. Since we need to forward all packets 2624 * to the nexthop first, we "blindly" set tcp_localnet 2625 * to false, eventhough the destination may also be 2626 * onlink. 2627 */ 2628 if (ire_uinfo == NULL) 2629 tcp->tcp_localnet = 0; 2630 else 2631 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2632 } else { 2633 /* 2634 * For incoming connection ire_mp = NULL 2635 * For outgoing connection ire_mp != NULL 2636 * Technically we should check conn_incoming_ill 2637 * when ire_mp is NULL and conn_outgoing_ill when 2638 * ire_mp is non-NULL. But this is performance 2639 * critical path and for IPV*_BOUND_IF, outgoing 2640 * and incoming ill are always set to the same value. 2641 */ 2642 ill_t *dst_ill = NULL; 2643 ipif_t *dst_ipif = NULL; 2644 2645 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2646 2647 if (connp->conn_outgoing_ill != NULL) { 2648 /* Outgoing or incoming path */ 2649 int err; 2650 2651 dst_ill = conn_get_held_ill(connp, 2652 &connp->conn_outgoing_ill, &err); 2653 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2654 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2655 return (0); 2656 } 2657 match_flags |= MATCH_IRE_ILL; 2658 dst_ipif = dst_ill->ill_ipif; 2659 } 2660 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2661 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2662 2663 if (ire != NULL) { 2664 ire_cacheable = B_TRUE; 2665 ire_uinfo = (ire_mp != NULL) ? 2666 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2667 &ire->ire_uinfo; 2668 } else { 2669 if (ire_mp == NULL) { 2670 ire = ire_ftable_lookup_v6( 2671 &tcp->tcp_connp->conn_remv6, 2672 0, 0, 0, dst_ipif, &sire, zoneid, 2673 0, tsl, match_flags, ipst); 2674 if (ire == NULL) { 2675 if (dst_ill != NULL) 2676 ill_refrele(dst_ill); 2677 return (0); 2678 } 2679 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2680 &ire->ire_uinfo; 2681 } else { 2682 ire = (ire_t *)ire_mp->b_rptr; 2683 ire_uinfo = 2684 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2685 } 2686 } 2687 if (dst_ill != NULL) 2688 ill_refrele(dst_ill); 2689 2690 ASSERT(ire != NULL); 2691 ASSERT(ire_uinfo != NULL); 2692 2693 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2694 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2695 /* 2696 * ire->ire_mp is non null when ire_mp passed in is used 2697 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2698 */ 2699 if (ire->ire_mp == NULL) 2700 ire_refrele(ire); 2701 if (sire != NULL) 2702 ire_refrele(sire); 2703 return (0); 2704 } 2705 2706 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2707 in6_addr_t src_addr; 2708 2709 /* 2710 * ip_bind_connected_v6() has stored the correct source 2711 * address per IPv6 addr. selection policy in 2712 * conn_src_v6. 2713 */ 2714 src_addr = tcp->tcp_connp->conn_srcv6; 2715 2716 tcp->tcp_ip6h->ip6_src = src_addr; 2717 /* 2718 * Copy of the src addr. in tcp_t is needed 2719 * for the lookup funcs. 2720 */ 2721 tcp->tcp_ip_src_v6 = src_addr; 2722 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2723 &connp->conn_srcv6)); 2724 } 2725 tcp->tcp_localnet = 2726 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2727 } 2728 2729 /* 2730 * This allows applications to fail quickly when connections are made 2731 * to dead hosts. Hosts can be labeled dead by adding a reject route 2732 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2733 */ 2734 if ((ire->ire_flags & RTF_REJECT) && 2735 (ire->ire_flags & RTF_PRIVATE)) 2736 goto error; 2737 2738 /* 2739 * Make use of the cached rtt and rtt_sd values to calculate the 2740 * initial RTO. Note that they are already initialized in 2741 * tcp_init_values(). 2742 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2743 * IP_NEXTHOP, but instead are using the interface ire for the 2744 * nexthop, then we do not use the ire_uinfo from that ire to 2745 * do any initializations. 2746 */ 2747 if (ire_uinfo != NULL) { 2748 if (ire_uinfo->iulp_rtt != 0) { 2749 clock_t rto; 2750 2751 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2752 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2753 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2754 tcps->tcps_rexmit_interval_extra + 2755 (tcp->tcp_rtt_sa >> 5); 2756 2757 if (rto > tcps->tcps_rexmit_interval_max) { 2758 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2759 } else if (rto < tcps->tcps_rexmit_interval_min) { 2760 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2761 } else { 2762 tcp->tcp_rto = rto; 2763 } 2764 } 2765 if (ire_uinfo->iulp_ssthresh != 0) 2766 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2767 else 2768 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2769 if (ire_uinfo->iulp_spipe > 0) { 2770 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2771 tcps->tcps_max_buf); 2772 if (tcps->tcps_snd_lowat_fraction != 0) 2773 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2774 tcps->tcps_snd_lowat_fraction; 2775 (void) tcp_maxpsz_set(tcp, B_TRUE); 2776 } 2777 /* 2778 * Note that up till now, acceptor always inherits receive 2779 * window from the listener. But if there is a metrics 2780 * associated with a host, we should use that instead of 2781 * inheriting it from listener. Thus we need to pass this 2782 * info back to the caller. 2783 */ 2784 if (ire_uinfo->iulp_rpipe > 0) { 2785 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2786 tcps->tcps_max_buf); 2787 } 2788 2789 if (ire_uinfo->iulp_rtomax > 0) { 2790 tcp->tcp_second_timer_threshold = 2791 ire_uinfo->iulp_rtomax; 2792 } 2793 2794 /* 2795 * Use the metric option settings, iulp_tstamp_ok and 2796 * iulp_wscale_ok, only for active open. What this means 2797 * is that if the other side uses timestamp or window 2798 * scale option, TCP will also use those options. That 2799 * is for passive open. If the application sets a 2800 * large window, window scale is enabled regardless of 2801 * the value in iulp_wscale_ok. This is the behavior 2802 * since 2.6. So we keep it. 2803 * The only case left in passive open processing is the 2804 * check for SACK. 2805 * For ECN, it should probably be like SACK. But the 2806 * current value is binary, so we treat it like the other 2807 * cases. The metric only controls active open.For passive 2808 * open, the ndd param, tcp_ecn_permitted, controls the 2809 * behavior. 2810 */ 2811 if (!tcp_detached) { 2812 /* 2813 * The if check means that the following can only 2814 * be turned on by the metrics only IRE, but not off. 2815 */ 2816 if (ire_uinfo->iulp_tstamp_ok) 2817 tcp->tcp_snd_ts_ok = B_TRUE; 2818 if (ire_uinfo->iulp_wscale_ok) 2819 tcp->tcp_snd_ws_ok = B_TRUE; 2820 if (ire_uinfo->iulp_sack == 2) 2821 tcp->tcp_snd_sack_ok = B_TRUE; 2822 if (ire_uinfo->iulp_ecn_ok) 2823 tcp->tcp_ecn_ok = B_TRUE; 2824 } else { 2825 /* 2826 * Passive open. 2827 * 2828 * As above, the if check means that SACK can only be 2829 * turned on by the metric only IRE. 2830 */ 2831 if (ire_uinfo->iulp_sack > 0) { 2832 tcp->tcp_snd_sack_ok = B_TRUE; 2833 } 2834 } 2835 } 2836 2837 2838 /* 2839 * XXX: Note that currently, ire_max_frag can be as small as 68 2840 * because of PMTUd. So tcp_mss may go to negative if combined 2841 * length of all those options exceeds 28 bytes. But because 2842 * of the tcp_mss_min check below, we may not have a problem if 2843 * tcp_mss_min is of a reasonable value. The default is 1 so 2844 * the negative problem still exists. And the check defeats PMTUd. 2845 * In fact, if PMTUd finds that the MSS should be smaller than 2846 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2847 * value. 2848 * 2849 * We do not deal with that now. All those problems related to 2850 * PMTUd will be fixed later. 2851 */ 2852 ASSERT(ire->ire_max_frag != 0); 2853 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2854 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2855 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2856 mss = MIN(mss, IPV6_MIN_MTU); 2857 } 2858 } 2859 2860 /* Sanity check for MSS value. */ 2861 if (tcp->tcp_ipversion == IPV4_VERSION) 2862 mss_max = tcps->tcps_mss_max_ipv4; 2863 else 2864 mss_max = tcps->tcps_mss_max_ipv6; 2865 2866 if (tcp->tcp_ipversion == IPV6_VERSION && 2867 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2868 /* 2869 * After receiving an ICMPv6 "packet too big" message with a 2870 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2871 * will insert a 8-byte fragment header in every packet; we 2872 * reduce the MSS by that amount here. 2873 */ 2874 mss -= sizeof (ip6_frag_t); 2875 } 2876 2877 if (tcp->tcp_ipsec_overhead == 0) 2878 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2879 2880 mss -= tcp->tcp_ipsec_overhead; 2881 2882 if (mss < tcps->tcps_mss_min) 2883 mss = tcps->tcps_mss_min; 2884 if (mss > mss_max) 2885 mss = mss_max; 2886 2887 /* Note that this is the maximum MSS, excluding all options. */ 2888 tcp->tcp_mss = mss; 2889 2890 /* 2891 * Initialize the ISS here now that we have the full connection ID. 2892 * The RFC 1948 method of initial sequence number generation requires 2893 * knowledge of the full connection ID before setting the ISS. 2894 */ 2895 2896 tcp_iss_init(tcp); 2897 2898 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2899 tcp->tcp_loopback = B_TRUE; 2900 2901 if (tcp->tcp_ipversion == IPV4_VERSION) { 2902 hsp = tcp_hsp_lookup(tcp->tcp_remote, tcps); 2903 } else { 2904 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6, tcps); 2905 } 2906 2907 if (hsp != NULL) { 2908 /* Only modify if we're going to make them bigger */ 2909 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2910 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2911 if (tcps->tcps_snd_lowat_fraction != 0) 2912 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2913 tcps->tcps_snd_lowat_fraction; 2914 } 2915 2916 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2917 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2918 } 2919 2920 /* Copy timestamp flag only for active open */ 2921 if (!tcp_detached) 2922 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2923 } 2924 2925 if (sire != NULL) 2926 IRE_REFRELE(sire); 2927 2928 /* 2929 * If we got an IRE_CACHE and an ILL, go through their properties; 2930 * otherwise, this is deferred until later when we have an IRE_CACHE. 2931 */ 2932 if (tcp->tcp_loopback || 2933 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2934 /* 2935 * For incoming, see if this tcp may be MDT-capable. For 2936 * outgoing, this process has been taken care of through 2937 * tcp_rput_other. 2938 */ 2939 tcp_ire_ill_check(tcp, ire, ill, incoming); 2940 tcp->tcp_ire_ill_check_done = B_TRUE; 2941 } 2942 2943 mutex_enter(&connp->conn_lock); 2944 /* 2945 * Make sure that conn is not marked incipient 2946 * for incoming connections. A blind 2947 * removal of incipient flag is cheaper than 2948 * check and removal. 2949 */ 2950 connp->conn_state_flags &= ~CONN_INCIPIENT; 2951 2952 /* 2953 * Must not cache forwarding table routes 2954 * or recache an IRE after the conn_t has 2955 * had conn_ire_cache cleared and is flagged 2956 * unusable, (see the CONN_CACHE_IRE() macro). 2957 */ 2958 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2959 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2960 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2961 connp->conn_ire_cache = ire; 2962 IRE_UNTRACE_REF(ire); 2963 rw_exit(&ire->ire_bucket->irb_lock); 2964 mutex_exit(&connp->conn_lock); 2965 return (1); 2966 } 2967 rw_exit(&ire->ire_bucket->irb_lock); 2968 } 2969 mutex_exit(&connp->conn_lock); 2970 2971 if (ire->ire_mp == NULL) 2972 ire_refrele(ire); 2973 return (1); 2974 2975 error: 2976 if (ire->ire_mp == NULL) 2977 ire_refrele(ire); 2978 if (sire != NULL) 2979 ire_refrele(sire); 2980 return (0); 2981 } 2982 2983 /* 2984 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 2985 * O_T_BIND_REQ/T_BIND_REQ message. 2986 */ 2987 static void 2988 tcp_bind(tcp_t *tcp, mblk_t *mp) 2989 { 2990 sin_t *sin; 2991 sin6_t *sin6; 2992 mblk_t *mp1; 2993 in_port_t requested_port; 2994 in_port_t allocated_port; 2995 struct T_bind_req *tbr; 2996 boolean_t bind_to_req_port_only; 2997 boolean_t backlog_update = B_FALSE; 2998 boolean_t user_specified; 2999 in6_addr_t v6addr; 3000 ipaddr_t v4addr; 3001 uint_t origipversion; 3002 int err; 3003 queue_t *q = tcp->tcp_wq; 3004 conn_t *connp; 3005 mlp_type_t addrtype, mlptype; 3006 zone_t *zone; 3007 cred_t *cr; 3008 in_port_t mlp_port; 3009 tcp_stack_t *tcps = tcp->tcp_tcps; 3010 3011 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3012 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3013 if (tcp->tcp_debug) { 3014 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3015 "tcp_bind: bad req, len %u", 3016 (uint_t)(mp->b_wptr - mp->b_rptr)); 3017 } 3018 tcp_err_ack(tcp, mp, TPROTO, 0); 3019 return; 3020 } 3021 /* Make sure the largest address fits */ 3022 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3023 if (mp1 == NULL) { 3024 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3025 return; 3026 } 3027 mp = mp1; 3028 tbr = (struct T_bind_req *)mp->b_rptr; 3029 if (tcp->tcp_state >= TCPS_BOUND) { 3030 if ((tcp->tcp_state == TCPS_BOUND || 3031 tcp->tcp_state == TCPS_LISTEN) && 3032 tcp->tcp_conn_req_max != tbr->CONIND_number && 3033 tbr->CONIND_number > 0) { 3034 /* 3035 * Handle listen() increasing CONIND_number. 3036 * This is more "liberal" then what the TPI spec 3037 * requires but is needed to avoid a t_unbind 3038 * when handling listen() since the port number 3039 * might be "stolen" between the unbind and bind. 3040 */ 3041 backlog_update = B_TRUE; 3042 goto do_bind; 3043 } 3044 if (tcp->tcp_debug) { 3045 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3046 "tcp_bind: bad state, %d", tcp->tcp_state); 3047 } 3048 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3049 return; 3050 } 3051 origipversion = tcp->tcp_ipversion; 3052 3053 switch (tbr->ADDR_length) { 3054 case 0: /* request for a generic port */ 3055 tbr->ADDR_offset = sizeof (struct T_bind_req); 3056 if (tcp->tcp_family == AF_INET) { 3057 tbr->ADDR_length = sizeof (sin_t); 3058 sin = (sin_t *)&tbr[1]; 3059 *sin = sin_null; 3060 sin->sin_family = AF_INET; 3061 mp->b_wptr = (uchar_t *)&sin[1]; 3062 tcp->tcp_ipversion = IPV4_VERSION; 3063 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3064 } else { 3065 ASSERT(tcp->tcp_family == AF_INET6); 3066 tbr->ADDR_length = sizeof (sin6_t); 3067 sin6 = (sin6_t *)&tbr[1]; 3068 *sin6 = sin6_null; 3069 sin6->sin6_family = AF_INET6; 3070 mp->b_wptr = (uchar_t *)&sin6[1]; 3071 tcp->tcp_ipversion = IPV6_VERSION; 3072 V6_SET_ZERO(v6addr); 3073 } 3074 requested_port = 0; 3075 break; 3076 3077 case sizeof (sin_t): /* Complete IPv4 address */ 3078 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3079 sizeof (sin_t)); 3080 if (sin == NULL || !OK_32PTR((char *)sin)) { 3081 if (tcp->tcp_debug) { 3082 (void) strlog(TCP_MOD_ID, 0, 1, 3083 SL_ERROR|SL_TRACE, 3084 "tcp_bind: bad address parameter, " 3085 "offset %d, len %d", 3086 tbr->ADDR_offset, tbr->ADDR_length); 3087 } 3088 tcp_err_ack(tcp, mp, TPROTO, 0); 3089 return; 3090 } 3091 /* 3092 * With sockets sockfs will accept bogus sin_family in 3093 * bind() and replace it with the family used in the socket 3094 * call. 3095 */ 3096 if (sin->sin_family != AF_INET || 3097 tcp->tcp_family != AF_INET) { 3098 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3099 return; 3100 } 3101 requested_port = ntohs(sin->sin_port); 3102 tcp->tcp_ipversion = IPV4_VERSION; 3103 v4addr = sin->sin_addr.s_addr; 3104 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3105 break; 3106 3107 case sizeof (sin6_t): /* Complete IPv6 address */ 3108 sin6 = (sin6_t *)mi_offset_param(mp, 3109 tbr->ADDR_offset, sizeof (sin6_t)); 3110 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3111 if (tcp->tcp_debug) { 3112 (void) strlog(TCP_MOD_ID, 0, 1, 3113 SL_ERROR|SL_TRACE, 3114 "tcp_bind: bad IPv6 address parameter, " 3115 "offset %d, len %d", tbr->ADDR_offset, 3116 tbr->ADDR_length); 3117 } 3118 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3119 return; 3120 } 3121 if (sin6->sin6_family != AF_INET6 || 3122 tcp->tcp_family != AF_INET6) { 3123 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3124 return; 3125 } 3126 requested_port = ntohs(sin6->sin6_port); 3127 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3128 IPV4_VERSION : IPV6_VERSION; 3129 v6addr = sin6->sin6_addr; 3130 break; 3131 3132 default: 3133 if (tcp->tcp_debug) { 3134 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3135 "tcp_bind: bad address length, %d", 3136 tbr->ADDR_length); 3137 } 3138 tcp_err_ack(tcp, mp, TBADADDR, 0); 3139 return; 3140 } 3141 tcp->tcp_bound_source_v6 = v6addr; 3142 3143 /* Check for change in ipversion */ 3144 if (origipversion != tcp->tcp_ipversion) { 3145 ASSERT(tcp->tcp_family == AF_INET6); 3146 err = tcp->tcp_ipversion == IPV6_VERSION ? 3147 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3148 if (err) { 3149 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3150 return; 3151 } 3152 } 3153 3154 /* 3155 * Initialize family specific fields. Copy of the src addr. 3156 * in tcp_t is needed for the lookup funcs. 3157 */ 3158 if (tcp->tcp_ipversion == IPV6_VERSION) { 3159 tcp->tcp_ip6h->ip6_src = v6addr; 3160 } else { 3161 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3162 } 3163 tcp->tcp_ip_src_v6 = v6addr; 3164 3165 /* 3166 * For O_T_BIND_REQ: 3167 * Verify that the target port/addr is available, or choose 3168 * another. 3169 * For T_BIND_REQ: 3170 * Verify that the target port/addr is available or fail. 3171 * In both cases when it succeeds the tcp is inserted in the 3172 * bind hash table. This ensures that the operation is atomic 3173 * under the lock on the hash bucket. 3174 */ 3175 bind_to_req_port_only = requested_port != 0 && 3176 tbr->PRIM_type != O_T_BIND_REQ; 3177 /* 3178 * Get a valid port (within the anonymous range and should not 3179 * be a privileged one) to use if the user has not given a port. 3180 * If multiple threads are here, they may all start with 3181 * with the same initial port. But, it should be fine as long as 3182 * tcp_bindi will ensure that no two threads will be assigned 3183 * the same port. 3184 * 3185 * NOTE: XXX If a privileged process asks for an anonymous port, we 3186 * still check for ports only in the range > tcp_smallest_non_priv_port, 3187 * unless TCP_ANONPRIVBIND option is set. 3188 */ 3189 mlptype = mlptSingle; 3190 mlp_port = requested_port; 3191 if (requested_port == 0) { 3192 requested_port = tcp->tcp_anon_priv_bind ? 3193 tcp_get_next_priv_port(tcp) : 3194 tcp_update_next_port(tcps->tcps_next_port_to_try, 3195 tcp, B_TRUE); 3196 if (requested_port == 0) { 3197 tcp_err_ack(tcp, mp, TNOADDR, 0); 3198 return; 3199 } 3200 user_specified = B_FALSE; 3201 3202 /* 3203 * If the user went through one of the RPC interfaces to create 3204 * this socket and RPC is MLP in this zone, then give him an 3205 * anonymous MLP. 3206 */ 3207 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3208 connp = tcp->tcp_connp; 3209 if (connp->conn_anon_mlp && is_system_labeled()) { 3210 zone = crgetzone(cr); 3211 addrtype = tsol_mlp_addr_type(zone->zone_id, 3212 IPV6_VERSION, &v6addr, 3213 tcps->tcps_netstack->netstack_ip); 3214 if (addrtype == mlptSingle) { 3215 tcp_err_ack(tcp, mp, TNOADDR, 0); 3216 return; 3217 } 3218 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3219 PMAPPORT, addrtype); 3220 mlp_port = PMAPPORT; 3221 } 3222 } else { 3223 int i; 3224 boolean_t priv = B_FALSE; 3225 3226 /* 3227 * If the requested_port is in the well-known privileged range, 3228 * verify that the stream was opened by a privileged user. 3229 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3230 * but instead the code relies on: 3231 * - the fact that the address of the array and its size never 3232 * changes 3233 * - the atomic assignment of the elements of the array 3234 */ 3235 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3236 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 3237 priv = B_TRUE; 3238 } else { 3239 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 3240 if (requested_port == 3241 tcps->tcps_g_epriv_ports[i]) { 3242 priv = B_TRUE; 3243 break; 3244 } 3245 } 3246 } 3247 if (priv) { 3248 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3249 if (tcp->tcp_debug) { 3250 (void) strlog(TCP_MOD_ID, 0, 1, 3251 SL_ERROR|SL_TRACE, 3252 "tcp_bind: no priv for port %d", 3253 requested_port); 3254 } 3255 tcp_err_ack(tcp, mp, TACCES, 0); 3256 return; 3257 } 3258 } 3259 user_specified = B_TRUE; 3260 3261 connp = tcp->tcp_connp; 3262 if (is_system_labeled()) { 3263 zone = crgetzone(cr); 3264 addrtype = tsol_mlp_addr_type(zone->zone_id, 3265 IPV6_VERSION, &v6addr, 3266 tcps->tcps_netstack->netstack_ip); 3267 if (addrtype == mlptSingle) { 3268 tcp_err_ack(tcp, mp, TNOADDR, 0); 3269 return; 3270 } 3271 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3272 requested_port, addrtype); 3273 } 3274 } 3275 3276 if (mlptype != mlptSingle) { 3277 if (secpolicy_net_bindmlp(cr) != 0) { 3278 if (tcp->tcp_debug) { 3279 (void) strlog(TCP_MOD_ID, 0, 1, 3280 SL_ERROR|SL_TRACE, 3281 "tcp_bind: no priv for multilevel port %d", 3282 requested_port); 3283 } 3284 tcp_err_ack(tcp, mp, TACCES, 0); 3285 return; 3286 } 3287 3288 /* 3289 * If we're specifically binding a shared IP address and the 3290 * port is MLP on shared addresses, then check to see if this 3291 * zone actually owns the MLP. Reject if not. 3292 */ 3293 if (mlptype == mlptShared && addrtype == mlptShared) { 3294 /* 3295 * No need to handle exclusive-stack zones since 3296 * ALL_ZONES only applies to the shared stack. 3297 */ 3298 zoneid_t mlpzone; 3299 3300 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3301 htons(mlp_port)); 3302 if (connp->conn_zoneid != mlpzone) { 3303 if (tcp->tcp_debug) { 3304 (void) strlog(TCP_MOD_ID, 0, 1, 3305 SL_ERROR|SL_TRACE, 3306 "tcp_bind: attempt to bind port " 3307 "%d on shared addr in zone %d " 3308 "(should be %d)", 3309 mlp_port, connp->conn_zoneid, 3310 mlpzone); 3311 } 3312 tcp_err_ack(tcp, mp, TACCES, 0); 3313 return; 3314 } 3315 } 3316 3317 if (!user_specified) { 3318 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3319 requested_port, B_TRUE); 3320 if (err != 0) { 3321 if (tcp->tcp_debug) { 3322 (void) strlog(TCP_MOD_ID, 0, 1, 3323 SL_ERROR|SL_TRACE, 3324 "tcp_bind: cannot establish anon " 3325 "MLP for port %d", 3326 requested_port); 3327 } 3328 tcp_err_ack(tcp, mp, TSYSERR, err); 3329 return; 3330 } 3331 connp->conn_anon_port = B_TRUE; 3332 } 3333 connp->conn_mlp_type = mlptype; 3334 } 3335 3336 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3337 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3338 3339 if (allocated_port == 0) { 3340 connp->conn_mlp_type = mlptSingle; 3341 if (connp->conn_anon_port) { 3342 connp->conn_anon_port = B_FALSE; 3343 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3344 requested_port, B_FALSE); 3345 } 3346 if (bind_to_req_port_only) { 3347 if (tcp->tcp_debug) { 3348 (void) strlog(TCP_MOD_ID, 0, 1, 3349 SL_ERROR|SL_TRACE, 3350 "tcp_bind: requested addr busy"); 3351 } 3352 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3353 } else { 3354 /* If we are out of ports, fail the bind. */ 3355 if (tcp->tcp_debug) { 3356 (void) strlog(TCP_MOD_ID, 0, 1, 3357 SL_ERROR|SL_TRACE, 3358 "tcp_bind: out of ports?"); 3359 } 3360 tcp_err_ack(tcp, mp, TNOADDR, 0); 3361 } 3362 return; 3363 } 3364 ASSERT(tcp->tcp_state == TCPS_BOUND); 3365 do_bind: 3366 if (!backlog_update) { 3367 if (tcp->tcp_family == AF_INET) 3368 sin->sin_port = htons(allocated_port); 3369 else 3370 sin6->sin6_port = htons(allocated_port); 3371 } 3372 if (tcp->tcp_family == AF_INET) { 3373 if (tbr->CONIND_number != 0) { 3374 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3375 sizeof (sin_t)); 3376 } else { 3377 /* Just verify the local IP address */ 3378 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3379 } 3380 } else { 3381 if (tbr->CONIND_number != 0) { 3382 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3383 sizeof (sin6_t)); 3384 } else { 3385 /* Just verify the local IP address */ 3386 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3387 IPV6_ADDR_LEN); 3388 } 3389 } 3390 if (mp1 == NULL) { 3391 if (connp->conn_anon_port) { 3392 connp->conn_anon_port = B_FALSE; 3393 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3394 requested_port, B_FALSE); 3395 } 3396 connp->conn_mlp_type = mlptSingle; 3397 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3398 return; 3399 } 3400 3401 tbr->PRIM_type = T_BIND_ACK; 3402 mp->b_datap->db_type = M_PCPROTO; 3403 3404 /* Chain in the reply mp for tcp_rput() */ 3405 mp1->b_cont = mp; 3406 mp = mp1; 3407 3408 tcp->tcp_conn_req_max = tbr->CONIND_number; 3409 if (tcp->tcp_conn_req_max) { 3410 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 3411 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 3412 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 3413 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 3414 /* 3415 * If this is a listener, do not reset the eager list 3416 * and other stuffs. Note that we don't check if the 3417 * existing eager list meets the new tcp_conn_req_max 3418 * requirement. 3419 */ 3420 if (tcp->tcp_state != TCPS_LISTEN) { 3421 tcp->tcp_state = TCPS_LISTEN; 3422 /* Initialize the chain. Don't need the eager_lock */ 3423 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3424 tcp->tcp_eager_next_drop_q0 = tcp; 3425 tcp->tcp_eager_prev_drop_q0 = tcp; 3426 tcp->tcp_second_ctimer_threshold = 3427 tcps->tcps_ip_abort_linterval; 3428 } 3429 } 3430 3431 /* 3432 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3433 * processing continues in tcp_rput_other(). 3434 */ 3435 if (tcp->tcp_family == AF_INET6) { 3436 ASSERT(tcp->tcp_connp->conn_af_isv6); 3437 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3438 } else { 3439 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3440 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3441 } 3442 /* 3443 * If the bind cannot complete immediately 3444 * IP will arrange to call tcp_rput_other 3445 * when the bind completes. 3446 */ 3447 if (mp != NULL) { 3448 tcp_rput_other(tcp, mp); 3449 } else { 3450 /* 3451 * Bind will be resumed later. Need to ensure 3452 * that conn doesn't disappear when that happens. 3453 * This will be decremented in ip_resume_tcp_bind(). 3454 */ 3455 CONN_INC_REF(tcp->tcp_connp); 3456 } 3457 } 3458 3459 3460 /* 3461 * If the "bind_to_req_port_only" parameter is set, if the requested port 3462 * number is available, return it, If not return 0 3463 * 3464 * If "bind_to_req_port_only" parameter is not set and 3465 * If the requested port number is available, return it. If not, return 3466 * the first anonymous port we happen across. If no anonymous ports are 3467 * available, return 0. addr is the requested local address, if any. 3468 * 3469 * In either case, when succeeding update the tcp_t to record the port number 3470 * and insert it in the bind hash table. 3471 * 3472 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3473 * without setting SO_REUSEADDR. This is needed so that they 3474 * can be viewed as two independent transport protocols. 3475 */ 3476 static in_port_t 3477 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3478 int reuseaddr, boolean_t quick_connect, 3479 boolean_t bind_to_req_port_only, boolean_t user_specified) 3480 { 3481 /* number of times we have run around the loop */ 3482 int count = 0; 3483 /* maximum number of times to run around the loop */ 3484 int loopmax; 3485 conn_t *connp = tcp->tcp_connp; 3486 zoneid_t zoneid = connp->conn_zoneid; 3487 tcp_stack_t *tcps = tcp->tcp_tcps; 3488 3489 /* 3490 * Lookup for free addresses is done in a loop and "loopmax" 3491 * influences how long we spin in the loop 3492 */ 3493 if (bind_to_req_port_only) { 3494 /* 3495 * If the requested port is busy, don't bother to look 3496 * for a new one. Setting loop maximum count to 1 has 3497 * that effect. 3498 */ 3499 loopmax = 1; 3500 } else { 3501 /* 3502 * If the requested port is busy, look for a free one 3503 * in the anonymous port range. 3504 * Set loopmax appropriately so that one does not look 3505 * forever in the case all of the anonymous ports are in use. 3506 */ 3507 if (tcp->tcp_anon_priv_bind) { 3508 /* 3509 * loopmax = 3510 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3511 */ 3512 loopmax = IPPORT_RESERVED - 3513 tcps->tcps_min_anonpriv_port; 3514 } else { 3515 loopmax = (tcps->tcps_largest_anon_port - 3516 tcps->tcps_smallest_anon_port + 1); 3517 } 3518 } 3519 do { 3520 uint16_t lport; 3521 tf_t *tbf; 3522 tcp_t *ltcp; 3523 conn_t *lconnp; 3524 3525 lport = htons(port); 3526 3527 /* 3528 * Ensure that the tcp_t is not currently in the bind hash. 3529 * Hold the lock on the hash bucket to ensure that 3530 * the duplicate check plus the insertion is an atomic 3531 * operation. 3532 * 3533 * This function does an inline lookup on the bind hash list 3534 * Make sure that we access only members of tcp_t 3535 * and that we don't look at tcp_tcp, since we are not 3536 * doing a CONN_INC_REF. 3537 */ 3538 tcp_bind_hash_remove(tcp); 3539 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3540 mutex_enter(&tbf->tf_lock); 3541 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3542 ltcp = ltcp->tcp_bind_hash) { 3543 boolean_t not_socket; 3544 boolean_t exclbind; 3545 3546 if (lport != ltcp->tcp_lport) 3547 continue; 3548 3549 lconnp = ltcp->tcp_connp; 3550 3551 /* 3552 * On a labeled system, we must treat bindings to ports 3553 * on shared IP addresses by sockets with MAC exemption 3554 * privilege as being in all zones, as there's 3555 * otherwise no way to identify the right receiver. 3556 */ 3557 if (!IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) && 3558 !lconnp->conn_mac_exempt && 3559 !connp->conn_mac_exempt) 3560 continue; 3561 3562 /* 3563 * If TCP_EXCLBIND is set for either the bound or 3564 * binding endpoint, the semantics of bind 3565 * is changed according to the following. 3566 * 3567 * spec = specified address (v4 or v6) 3568 * unspec = unspecified address (v4 or v6) 3569 * A = specified addresses are different for endpoints 3570 * 3571 * bound bind to allowed 3572 * ------------------------------------- 3573 * unspec unspec no 3574 * unspec spec no 3575 * spec unspec no 3576 * spec spec yes if A 3577 * 3578 * For labeled systems, SO_MAC_EXEMPT behaves the same 3579 * as TCP_EXCLBIND, except that zoneid is ignored. 3580 * 3581 * Note: 3582 * 3583 * 1. Because of TLI semantics, an endpoint can go 3584 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3585 * TCPS_BOUND, depending on whether it is originally 3586 * a listener or not. That is why we need to check 3587 * for states greater than or equal to TCPS_BOUND 3588 * here. 3589 * 3590 * 2. Ideally, we should only check for state equals 3591 * to TCPS_LISTEN. And the following check should be 3592 * added. 3593 * 3594 * if (ltcp->tcp_state == TCPS_LISTEN || 3595 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3596 * ... 3597 * } 3598 * 3599 * The semantics will be changed to this. If the 3600 * endpoint on the list is in state not equal to 3601 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3602 * set, let the bind succeed. 3603 * 3604 * Because of (1), we cannot do that for TLI 3605 * endpoints. But we can do that for socket endpoints. 3606 * If in future, we can change this going back 3607 * semantics, we can use the above check for TLI also. 3608 */ 3609 not_socket = !(TCP_IS_SOCKET(ltcp) && 3610 TCP_IS_SOCKET(tcp)); 3611 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3612 3613 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3614 (exclbind && (not_socket || 3615 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3616 if (V6_OR_V4_INADDR_ANY( 3617 ltcp->tcp_bound_source_v6) || 3618 V6_OR_V4_INADDR_ANY(*laddr) || 3619 IN6_ARE_ADDR_EQUAL(laddr, 3620 <cp->tcp_bound_source_v6)) { 3621 break; 3622 } 3623 continue; 3624 } 3625 3626 /* 3627 * Check ipversion to allow IPv4 and IPv6 sockets to 3628 * have disjoint port number spaces, if *_EXCLBIND 3629 * is not set and only if the application binds to a 3630 * specific port. We use the same autoassigned port 3631 * number space for IPv4 and IPv6 sockets. 3632 */ 3633 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3634 bind_to_req_port_only) 3635 continue; 3636 3637 /* 3638 * Ideally, we should make sure that the source 3639 * address, remote address, and remote port in the 3640 * four tuple for this tcp-connection is unique. 3641 * However, trying to find out the local source 3642 * address would require too much code duplication 3643 * with IP, since IP needs needs to have that code 3644 * to support userland TCP implementations. 3645 */ 3646 if (quick_connect && 3647 (ltcp->tcp_state > TCPS_LISTEN) && 3648 ((tcp->tcp_fport != ltcp->tcp_fport) || 3649 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3650 <cp->tcp_remote_v6))) 3651 continue; 3652 3653 if (!reuseaddr) { 3654 /* 3655 * No socket option SO_REUSEADDR. 3656 * If existing port is bound to 3657 * a non-wildcard IP address 3658 * and the requesting stream is 3659 * bound to a distinct 3660 * different IP addresses 3661 * (non-wildcard, also), keep 3662 * going. 3663 */ 3664 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3665 !V6_OR_V4_INADDR_ANY( 3666 ltcp->tcp_bound_source_v6) && 3667 !IN6_ARE_ADDR_EQUAL(laddr, 3668 <cp->tcp_bound_source_v6)) 3669 continue; 3670 if (ltcp->tcp_state >= TCPS_BOUND) { 3671 /* 3672 * This port is being used and 3673 * its state is >= TCPS_BOUND, 3674 * so we can't bind to it. 3675 */ 3676 break; 3677 } 3678 } else { 3679 /* 3680 * socket option SO_REUSEADDR is set on the 3681 * binding tcp_t. 3682 * 3683 * If two streams are bound to 3684 * same IP address or both addr 3685 * and bound source are wildcards 3686 * (INADDR_ANY), we want to stop 3687 * searching. 3688 * We have found a match of IP source 3689 * address and source port, which is 3690 * refused regardless of the 3691 * SO_REUSEADDR setting, so we break. 3692 */ 3693 if (IN6_ARE_ADDR_EQUAL(laddr, 3694 <cp->tcp_bound_source_v6) && 3695 (ltcp->tcp_state == TCPS_LISTEN || 3696 ltcp->tcp_state == TCPS_BOUND)) 3697 break; 3698 } 3699 } 3700 if (ltcp != NULL) { 3701 /* The port number is busy */ 3702 mutex_exit(&tbf->tf_lock); 3703 } else { 3704 /* 3705 * This port is ours. Insert in fanout and mark as 3706 * bound to prevent others from getting the port 3707 * number. 3708 */ 3709 tcp->tcp_state = TCPS_BOUND; 3710 tcp->tcp_lport = htons(port); 3711 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3712 3713 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3714 tcp->tcp_lport)] == tbf); 3715 tcp_bind_hash_insert(tbf, tcp, 1); 3716 3717 mutex_exit(&tbf->tf_lock); 3718 3719 /* 3720 * We don't want tcp_next_port_to_try to "inherit" 3721 * a port number supplied by the user in a bind. 3722 */ 3723 if (user_specified) 3724 return (port); 3725 3726 /* 3727 * This is the only place where tcp_next_port_to_try 3728 * is updated. After the update, it may or may not 3729 * be in the valid range. 3730 */ 3731 if (!tcp->tcp_anon_priv_bind) 3732 tcps->tcps_next_port_to_try = port + 1; 3733 return (port); 3734 } 3735 3736 if (tcp->tcp_anon_priv_bind) { 3737 port = tcp_get_next_priv_port(tcp); 3738 } else { 3739 if (count == 0 && user_specified) { 3740 /* 3741 * We may have to return an anonymous port. So 3742 * get one to start with. 3743 */ 3744 port = 3745 tcp_update_next_port( 3746 tcps->tcps_next_port_to_try, 3747 tcp, B_TRUE); 3748 user_specified = B_FALSE; 3749 } else { 3750 port = tcp_update_next_port(port + 1, tcp, 3751 B_FALSE); 3752 } 3753 } 3754 if (port == 0) 3755 break; 3756 3757 /* 3758 * Don't let this loop run forever in the case where 3759 * all of the anonymous ports are in use. 3760 */ 3761 } while (++count < loopmax); 3762 return (0); 3763 } 3764 3765 /* 3766 * tcp_clean_death / tcp_close_detached must not be called more than once 3767 * on a tcp. Thus every function that potentially calls tcp_clean_death 3768 * must check for the tcp state before calling tcp_clean_death. 3769 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3770 * tcp_timer_handler, all check for the tcp state. 3771 */ 3772 /* ARGSUSED */ 3773 void 3774 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3775 { 3776 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3777 3778 freemsg(mp); 3779 if (tcp->tcp_state > TCPS_BOUND) 3780 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, ETIMEDOUT, 5); 3781 } 3782 3783 /* 3784 * We are dying for some reason. Try to do it gracefully. (May be called 3785 * as writer.) 3786 * 3787 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3788 * done by a service procedure). 3789 * TBD - Should the return value distinguish between the tcp_t being 3790 * freed and it being reinitialized? 3791 */ 3792 static int 3793 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3794 { 3795 mblk_t *mp; 3796 queue_t *q; 3797 tcp_stack_t *tcps = tcp->tcp_tcps; 3798 3799 TCP_CLD_STAT(tag); 3800 3801 #if TCP_TAG_CLEAN_DEATH 3802 tcp->tcp_cleandeathtag = tag; 3803 #endif 3804 3805 if (tcp->tcp_fused) 3806 tcp_unfuse(tcp); 3807 3808 if (tcp->tcp_linger_tid != 0 && 3809 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3810 tcp_stop_lingering(tcp); 3811 } 3812 3813 ASSERT(tcp != NULL); 3814 ASSERT((tcp->tcp_family == AF_INET && 3815 tcp->tcp_ipversion == IPV4_VERSION) || 3816 (tcp->tcp_family == AF_INET6 && 3817 (tcp->tcp_ipversion == IPV4_VERSION || 3818 tcp->tcp_ipversion == IPV6_VERSION))); 3819 3820 if (TCP_IS_DETACHED(tcp)) { 3821 if (tcp->tcp_hard_binding) { 3822 /* 3823 * Its an eager that we are dealing with. We close the 3824 * eager but in case a conn_ind has already gone to the 3825 * listener, let tcp_accept_finish() send a discon_ind 3826 * to the listener and drop the last reference. If the 3827 * listener doesn't even know about the eager i.e. the 3828 * conn_ind hasn't gone up, blow away the eager and drop 3829 * the last reference as well. If the conn_ind has gone 3830 * up, state should be BOUND. tcp_accept_finish 3831 * will figure out that the connection has received a 3832 * RST and will send a DISCON_IND to the application. 3833 */ 3834 tcp_closei_local(tcp); 3835 if (!tcp->tcp_tconnind_started) { 3836 CONN_DEC_REF(tcp->tcp_connp); 3837 } else { 3838 tcp->tcp_state = TCPS_BOUND; 3839 } 3840 } else { 3841 tcp_close_detached(tcp); 3842 } 3843 return (0); 3844 } 3845 3846 TCP_STAT(tcps, tcp_clean_death_nondetached); 3847 3848 /* 3849 * If T_ORDREL_IND has not been sent yet (done when service routine 3850 * is run) postpone cleaning up the endpoint until service routine 3851 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3852 * client_errno since tcp_close uses the client_errno field. 3853 */ 3854 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3855 if (err != 0) 3856 tcp->tcp_client_errno = err; 3857 3858 tcp->tcp_deferred_clean_death = B_TRUE; 3859 return (-1); 3860 } 3861 3862 q = tcp->tcp_rq; 3863 3864 /* Trash all inbound data */ 3865 flushq(q, FLUSHALL); 3866 3867 /* 3868 * If we are at least part way open and there is error 3869 * (err==0 implies no error) 3870 * notify our client by a T_DISCON_IND. 3871 */ 3872 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3873 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3874 !TCP_IS_SOCKET(tcp)) { 3875 /* 3876 * Send M_FLUSH according to TPI. Because sockets will 3877 * (and must) ignore FLUSHR we do that only for TPI 3878 * endpoints and sockets in STREAMS mode. 3879 */ 3880 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3881 } 3882 if (tcp->tcp_debug) { 3883 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3884 "tcp_clean_death: discon err %d", err); 3885 } 3886 mp = mi_tpi_discon_ind(NULL, err, 0); 3887 if (mp != NULL) { 3888 putnext(q, mp); 3889 } else { 3890 if (tcp->tcp_debug) { 3891 (void) strlog(TCP_MOD_ID, 0, 1, 3892 SL_ERROR|SL_TRACE, 3893 "tcp_clean_death, sending M_ERROR"); 3894 } 3895 (void) putnextctl1(q, M_ERROR, EPROTO); 3896 } 3897 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3898 /* SYN_SENT or SYN_RCVD */ 3899 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3900 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3901 /* ESTABLISHED or CLOSE_WAIT */ 3902 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3903 } 3904 } 3905 3906 tcp_reinit(tcp); 3907 return (-1); 3908 } 3909 3910 /* 3911 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3912 * to expire, stop the wait and finish the close. 3913 */ 3914 static void 3915 tcp_stop_lingering(tcp_t *tcp) 3916 { 3917 clock_t delta = 0; 3918 tcp_stack_t *tcps = tcp->tcp_tcps; 3919 3920 tcp->tcp_linger_tid = 0; 3921 if (tcp->tcp_state > TCPS_LISTEN) { 3922 tcp_acceptor_hash_remove(tcp); 3923 mutex_enter(&tcp->tcp_non_sq_lock); 3924 if (tcp->tcp_flow_stopped) { 3925 tcp_clrqfull(tcp); 3926 } 3927 mutex_exit(&tcp->tcp_non_sq_lock); 3928 3929 if (tcp->tcp_timer_tid != 0) { 3930 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3931 tcp->tcp_timer_tid = 0; 3932 } 3933 /* 3934 * Need to cancel those timers which will not be used when 3935 * TCP is detached. This has to be done before the tcp_wq 3936 * is set to the global queue. 3937 */ 3938 tcp_timers_stop(tcp); 3939 3940 3941 tcp->tcp_detached = B_TRUE; 3942 ASSERT(tcps->tcps_g_q != NULL); 3943 tcp->tcp_rq = tcps->tcps_g_q; 3944 tcp->tcp_wq = WR(tcps->tcps_g_q); 3945 3946 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3947 tcp_time_wait_append(tcp); 3948 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3949 goto finish; 3950 } 3951 3952 /* 3953 * If delta is zero the timer event wasn't executed and was 3954 * successfully canceled. In this case we need to restart it 3955 * with the minimal delta possible. 3956 */ 3957 if (delta >= 0) { 3958 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3959 delta ? delta : 1); 3960 } 3961 } else { 3962 tcp_closei_local(tcp); 3963 CONN_DEC_REF(tcp->tcp_connp); 3964 } 3965 finish: 3966 /* Signal closing thread that it can complete close */ 3967 mutex_enter(&tcp->tcp_closelock); 3968 tcp->tcp_detached = B_TRUE; 3969 ASSERT(tcps->tcps_g_q != NULL); 3970 tcp->tcp_rq = tcps->tcps_g_q; 3971 tcp->tcp_wq = WR(tcps->tcps_g_q); 3972 tcp->tcp_closed = 1; 3973 cv_signal(&tcp->tcp_closecv); 3974 mutex_exit(&tcp->tcp_closelock); 3975 } 3976 3977 /* 3978 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3979 * expires. 3980 */ 3981 static void 3982 tcp_close_linger_timeout(void *arg) 3983 { 3984 conn_t *connp = (conn_t *)arg; 3985 tcp_t *tcp = connp->conn_tcp; 3986 3987 tcp->tcp_client_errno = ETIMEDOUT; 3988 tcp_stop_lingering(tcp); 3989 } 3990 3991 static int 3992 tcp_close(queue_t *q, int flags) 3993 { 3994 conn_t *connp = Q_TO_CONN(q); 3995 tcp_t *tcp = connp->conn_tcp; 3996 mblk_t *mp = &tcp->tcp_closemp; 3997 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3998 boolean_t linger_interrupted = B_FALSE; 3999 mblk_t *bp; 4000 4001 ASSERT(WR(q)->q_next == NULL); 4002 ASSERT(connp->conn_ref >= 2); 4003 ASSERT((connp->conn_flags & IPCL_TCPMOD) == 0); 4004 4005 /* 4006 * We are being closed as /dev/tcp or /dev/tcp6. 4007 * 4008 * Mark the conn as closing. ill_pending_mp_add will not 4009 * add any mp to the pending mp list, after this conn has 4010 * started closing. Same for sq_pending_mp_add 4011 */ 4012 mutex_enter(&connp->conn_lock); 4013 connp->conn_state_flags |= CONN_CLOSING; 4014 if (connp->conn_oper_pending_ill != NULL) 4015 conn_ioctl_cleanup_reqd = B_TRUE; 4016 CONN_INC_REF_LOCKED(connp); 4017 mutex_exit(&connp->conn_lock); 4018 tcp->tcp_closeflags = (uint8_t)flags; 4019 ASSERT(connp->conn_ref >= 3); 4020 4021 /* 4022 * tcp_closemp_used is used below without any protection of a lock 4023 * as we don't expect any one else to use it concurrently at this 4024 * point otherwise it would be a major defect, though we do 4025 * increment tcp_closemp_used to record any attempt to reuse 4026 * tcp_closemp while it is still in use. This would help debugging. 4027 */ 4028 4029 if (mp->b_prev == NULL) { 4030 tcp->tcp_closemp_used = 1; 4031 } else { 4032 tcp->tcp_closemp_used++; 4033 ASSERT(mp->b_prev == NULL); 4034 } 4035 4036 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4037 4038 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4039 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4040 4041 mutex_enter(&tcp->tcp_closelock); 4042 while (!tcp->tcp_closed) { 4043 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4044 /* 4045 * We got interrupted. Check if we are lingering, 4046 * if yes, post a message to stop and wait until 4047 * tcp_closed is set. If we aren't lingering, 4048 * just go back around. 4049 */ 4050 if (tcp->tcp_linger && 4051 tcp->tcp_lingertime > 0 && 4052 !linger_interrupted) { 4053 mutex_exit(&tcp->tcp_closelock); 4054 /* Entering squeue, bump ref count. */ 4055 CONN_INC_REF(connp); 4056 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4057 squeue_enter(connp->conn_sqp, bp, 4058 tcp_linger_interrupted, connp, 4059 SQTAG_IP_TCP_CLOSE); 4060 linger_interrupted = B_TRUE; 4061 mutex_enter(&tcp->tcp_closelock); 4062 } 4063 } 4064 } 4065 mutex_exit(&tcp->tcp_closelock); 4066 4067 /* 4068 * In the case of listener streams that have eagers in the q or q0 4069 * we wait for the eagers to drop their reference to us. tcp_rq and 4070 * tcp_wq of the eagers point to our queues. By waiting for the 4071 * refcnt to drop to 1, we are sure that the eagers have cleaned 4072 * up their queue pointers and also dropped their references to us. 4073 */ 4074 if (tcp->tcp_wait_for_eagers) { 4075 mutex_enter(&connp->conn_lock); 4076 while (connp->conn_ref != 1) { 4077 cv_wait(&connp->conn_cv, &connp->conn_lock); 4078 } 4079 mutex_exit(&connp->conn_lock); 4080 } 4081 /* 4082 * ioctl cleanup. The mp is queued in the 4083 * ill_pending_mp or in the sq_pending_mp. 4084 */ 4085 if (conn_ioctl_cleanup_reqd) 4086 conn_ioctl_cleanup(connp); 4087 4088 qprocsoff(q); 4089 inet_minor_free(ip_minor_arena, connp->conn_dev); 4090 4091 tcp->tcp_cpid = -1; 4092 4093 /* 4094 * Drop IP's reference on the conn. This is the last reference 4095 * on the connp if the state was less than established. If the 4096 * connection has gone into timewait state, then we will have 4097 * one ref for the TCP and one more ref (total of two) for the 4098 * classifier connected hash list (a timewait connections stays 4099 * in connected hash till closed). 4100 * 4101 * We can't assert the references because there might be other 4102 * transient reference places because of some walkers or queued 4103 * packets in squeue for the timewait state. 4104 */ 4105 CONN_DEC_REF(connp); 4106 q->q_ptr = WR(q)->q_ptr = NULL; 4107 return (0); 4108 } 4109 4110 static int 4111 tcpclose_accept(queue_t *q) 4112 { 4113 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4114 4115 /* 4116 * We had opened an acceptor STREAM for sockfs which is 4117 * now being closed due to some error. 4118 */ 4119 qprocsoff(q); 4120 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 4121 q->q_ptr = WR(q)->q_ptr = NULL; 4122 return (0); 4123 } 4124 4125 /* 4126 * Called by tcp_close() routine via squeue when lingering is 4127 * interrupted by a signal. 4128 */ 4129 4130 /* ARGSUSED */ 4131 static void 4132 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4133 { 4134 conn_t *connp = (conn_t *)arg; 4135 tcp_t *tcp = connp->conn_tcp; 4136 4137 freeb(mp); 4138 if (tcp->tcp_linger_tid != 0 && 4139 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4140 tcp_stop_lingering(tcp); 4141 tcp->tcp_client_errno = EINTR; 4142 } 4143 } 4144 4145 /* 4146 * Called by streams close routine via squeues when our client blows off her 4147 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4148 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4149 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4150 * acked. 4151 * 4152 * NOTE: tcp_close potentially returns error when lingering. 4153 * However, the stream head currently does not pass these errors 4154 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4155 * errors to the application (from tsleep()) and not errors 4156 * like ECONNRESET caused by receiving a reset packet. 4157 */ 4158 4159 /* ARGSUSED */ 4160 static void 4161 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4162 { 4163 char *msg; 4164 conn_t *connp = (conn_t *)arg; 4165 tcp_t *tcp = connp->conn_tcp; 4166 clock_t delta = 0; 4167 tcp_stack_t *tcps = tcp->tcp_tcps; 4168 4169 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4170 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4171 4172 /* Cancel any pending timeout */ 4173 if (tcp->tcp_ordrelid != 0) { 4174 if (tcp->tcp_timeout) { 4175 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4176 } 4177 tcp->tcp_ordrelid = 0; 4178 tcp->tcp_timeout = B_FALSE; 4179 } 4180 4181 mutex_enter(&tcp->tcp_eager_lock); 4182 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4183 /* Cleanup for listener */ 4184 tcp_eager_cleanup(tcp, 0); 4185 tcp->tcp_wait_for_eagers = 1; 4186 } 4187 mutex_exit(&tcp->tcp_eager_lock); 4188 4189 connp->conn_mdt_ok = B_FALSE; 4190 tcp->tcp_mdt = B_FALSE; 4191 4192 connp->conn_lso_ok = B_FALSE; 4193 tcp->tcp_lso = B_FALSE; 4194 4195 msg = NULL; 4196 switch (tcp->tcp_state) { 4197 case TCPS_CLOSED: 4198 case TCPS_IDLE: 4199 case TCPS_BOUND: 4200 case TCPS_LISTEN: 4201 break; 4202 case TCPS_SYN_SENT: 4203 msg = "tcp_close, during connect"; 4204 break; 4205 case TCPS_SYN_RCVD: 4206 /* 4207 * Close during the connect 3-way handshake 4208 * but here there may or may not be pending data 4209 * already on queue. Process almost same as in 4210 * the ESTABLISHED state. 4211 */ 4212 /* FALLTHRU */ 4213 default: 4214 if (tcp->tcp_fused) 4215 tcp_unfuse(tcp); 4216 4217 /* 4218 * If SO_LINGER has set a zero linger time, abort the 4219 * connection with a reset. 4220 */ 4221 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4222 msg = "tcp_close, zero lingertime"; 4223 break; 4224 } 4225 4226 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4227 /* 4228 * Abort connection if there is unread data queued. 4229 */ 4230 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4231 msg = "tcp_close, unread data"; 4232 break; 4233 } 4234 /* 4235 * tcp_hard_bound is now cleared thus all packets go through 4236 * tcp_lookup. This fact is used by tcp_detach below. 4237 * 4238 * We have done a qwait() above which could have possibly 4239 * drained more messages in turn causing transition to a 4240 * different state. Check whether we have to do the rest 4241 * of the processing or not. 4242 */ 4243 if (tcp->tcp_state <= TCPS_LISTEN) 4244 break; 4245 4246 /* 4247 * Transmit the FIN before detaching the tcp_t. 4248 * After tcp_detach returns this queue/perimeter 4249 * no longer owns the tcp_t thus others can modify it. 4250 */ 4251 (void) tcp_xmit_end(tcp); 4252 4253 /* 4254 * If lingering on close then wait until the fin is acked, 4255 * the SO_LINGER time passes, or a reset is sent/received. 4256 */ 4257 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4258 !(tcp->tcp_fin_acked) && 4259 tcp->tcp_state >= TCPS_ESTABLISHED) { 4260 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4261 tcp->tcp_client_errno = EWOULDBLOCK; 4262 } else if (tcp->tcp_client_errno == 0) { 4263 4264 ASSERT(tcp->tcp_linger_tid == 0); 4265 4266 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4267 tcp_close_linger_timeout, 4268 tcp->tcp_lingertime * hz); 4269 4270 /* tcp_close_linger_timeout will finish close */ 4271 if (tcp->tcp_linger_tid == 0) 4272 tcp->tcp_client_errno = ENOSR; 4273 else 4274 return; 4275 } 4276 4277 /* 4278 * Check if we need to detach or just close 4279 * the instance. 4280 */ 4281 if (tcp->tcp_state <= TCPS_LISTEN) 4282 break; 4283 } 4284 4285 /* 4286 * Make sure that no other thread will access the tcp_rq of 4287 * this instance (through lookups etc.) as tcp_rq will go 4288 * away shortly. 4289 */ 4290 tcp_acceptor_hash_remove(tcp); 4291 4292 mutex_enter(&tcp->tcp_non_sq_lock); 4293 if (tcp->tcp_flow_stopped) { 4294 tcp_clrqfull(tcp); 4295 } 4296 mutex_exit(&tcp->tcp_non_sq_lock); 4297 4298 if (tcp->tcp_timer_tid != 0) { 4299 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4300 tcp->tcp_timer_tid = 0; 4301 } 4302 /* 4303 * Need to cancel those timers which will not be used when 4304 * TCP is detached. This has to be done before the tcp_wq 4305 * is set to the global queue. 4306 */ 4307 tcp_timers_stop(tcp); 4308 4309 tcp->tcp_detached = B_TRUE; 4310 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4311 tcp_time_wait_append(tcp); 4312 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4313 ASSERT(connp->conn_ref >= 3); 4314 goto finish; 4315 } 4316 4317 /* 4318 * If delta is zero the timer event wasn't executed and was 4319 * successfully canceled. In this case we need to restart it 4320 * with the minimal delta possible. 4321 */ 4322 if (delta >= 0) 4323 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4324 delta ? delta : 1); 4325 4326 ASSERT(connp->conn_ref >= 3); 4327 goto finish; 4328 } 4329 4330 /* Detach did not complete. Still need to remove q from stream. */ 4331 if (msg) { 4332 if (tcp->tcp_state == TCPS_ESTABLISHED || 4333 tcp->tcp_state == TCPS_CLOSE_WAIT) 4334 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4335 if (tcp->tcp_state == TCPS_SYN_SENT || 4336 tcp->tcp_state == TCPS_SYN_RCVD) 4337 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4338 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4339 } 4340 4341 tcp_closei_local(tcp); 4342 CONN_DEC_REF(connp); 4343 ASSERT(connp->conn_ref >= 2); 4344 4345 finish: 4346 /* 4347 * Although packets are always processed on the correct 4348 * tcp's perimeter and access is serialized via squeue's, 4349 * IP still needs a queue when sending packets in time_wait 4350 * state so use WR(tcps_g_q) till ip_output() can be 4351 * changed to deal with just connp. For read side, we 4352 * could have set tcp_rq to NULL but there are some cases 4353 * in tcp_rput_data() from early days of this code which 4354 * do a putnext without checking if tcp is closed. Those 4355 * need to be identified before both tcp_rq and tcp_wq 4356 * can be set to NULL and tcps_g_q can disappear forever. 4357 */ 4358 mutex_enter(&tcp->tcp_closelock); 4359 /* 4360 * Don't change the queues in the case of a listener that has 4361 * eagers in its q or q0. It could surprise the eagers. 4362 * Instead wait for the eagers outside the squeue. 4363 */ 4364 if (!tcp->tcp_wait_for_eagers) { 4365 tcp->tcp_detached = B_TRUE; 4366 /* 4367 * When default queue is closing we set tcps_g_q to NULL 4368 * after the close is done. 4369 */ 4370 ASSERT(tcps->tcps_g_q != NULL); 4371 tcp->tcp_rq = tcps->tcps_g_q; 4372 tcp->tcp_wq = WR(tcps->tcps_g_q); 4373 } 4374 4375 /* Signal tcp_close() to finish closing. */ 4376 tcp->tcp_closed = 1; 4377 cv_signal(&tcp->tcp_closecv); 4378 mutex_exit(&tcp->tcp_closelock); 4379 } 4380 4381 4382 /* 4383 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4384 * Some stream heads get upset if they see these later on as anything but NULL. 4385 */ 4386 static void 4387 tcp_close_mpp(mblk_t **mpp) 4388 { 4389 mblk_t *mp; 4390 4391 if ((mp = *mpp) != NULL) { 4392 do { 4393 mp->b_next = NULL; 4394 mp->b_prev = NULL; 4395 } while ((mp = mp->b_cont) != NULL); 4396 4397 mp = *mpp; 4398 *mpp = NULL; 4399 freemsg(mp); 4400 } 4401 } 4402 4403 /* Do detached close. */ 4404 static void 4405 tcp_close_detached(tcp_t *tcp) 4406 { 4407 if (tcp->tcp_fused) 4408 tcp_unfuse(tcp); 4409 4410 /* 4411 * Clustering code serializes TCP disconnect callbacks and 4412 * cluster tcp list walks by blocking a TCP disconnect callback 4413 * if a cluster tcp list walk is in progress. This ensures 4414 * accurate accounting of TCPs in the cluster code even though 4415 * the TCP list walk itself is not atomic. 4416 */ 4417 tcp_closei_local(tcp); 4418 CONN_DEC_REF(tcp->tcp_connp); 4419 } 4420 4421 /* 4422 * Stop all TCP timers, and free the timer mblks if requested. 4423 */ 4424 void 4425 tcp_timers_stop(tcp_t *tcp) 4426 { 4427 if (tcp->tcp_timer_tid != 0) { 4428 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4429 tcp->tcp_timer_tid = 0; 4430 } 4431 if (tcp->tcp_ka_tid != 0) { 4432 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4433 tcp->tcp_ka_tid = 0; 4434 } 4435 if (tcp->tcp_ack_tid != 0) { 4436 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4437 tcp->tcp_ack_tid = 0; 4438 } 4439 if (tcp->tcp_push_tid != 0) { 4440 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4441 tcp->tcp_push_tid = 0; 4442 } 4443 } 4444 4445 /* 4446 * The tcp_t is going away. Remove it from all lists and set it 4447 * to TCPS_CLOSED. The freeing up of memory is deferred until 4448 * tcp_inactive. This is needed since a thread in tcp_rput might have 4449 * done a CONN_INC_REF on this structure before it was removed from the 4450 * hashes. 4451 */ 4452 static void 4453 tcp_closei_local(tcp_t *tcp) 4454 { 4455 ire_t *ire; 4456 conn_t *connp = tcp->tcp_connp; 4457 tcp_stack_t *tcps = tcp->tcp_tcps; 4458 4459 if (!TCP_IS_SOCKET(tcp)) 4460 tcp_acceptor_hash_remove(tcp); 4461 4462 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4463 tcp->tcp_ibsegs = 0; 4464 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4465 tcp->tcp_obsegs = 0; 4466 4467 /* 4468 * If we are an eager connection hanging off a listener that 4469 * hasn't formally accepted the connection yet, get off his 4470 * list and blow off any data that we have accumulated. 4471 */ 4472 if (tcp->tcp_listener != NULL) { 4473 tcp_t *listener = tcp->tcp_listener; 4474 mutex_enter(&listener->tcp_eager_lock); 4475 /* 4476 * tcp_tconnind_started == B_TRUE means that the 4477 * conn_ind has already gone to listener. At 4478 * this point, eager will be closed but we 4479 * leave it in listeners eager list so that 4480 * if listener decides to close without doing 4481 * accept, we can clean this up. In tcp_wput_accept 4482 * we take care of the case of accept on closed 4483 * eager. 4484 */ 4485 if (!tcp->tcp_tconnind_started) { 4486 tcp_eager_unlink(tcp); 4487 mutex_exit(&listener->tcp_eager_lock); 4488 /* 4489 * We don't want to have any pointers to the 4490 * listener queue, after we have released our 4491 * reference on the listener 4492 */ 4493 ASSERT(tcps->tcps_g_q != NULL); 4494 tcp->tcp_rq = tcps->tcps_g_q; 4495 tcp->tcp_wq = WR(tcps->tcps_g_q); 4496 CONN_DEC_REF(listener->tcp_connp); 4497 } else { 4498 mutex_exit(&listener->tcp_eager_lock); 4499 } 4500 } 4501 4502 /* Stop all the timers */ 4503 tcp_timers_stop(tcp); 4504 4505 if (tcp->tcp_state == TCPS_LISTEN) { 4506 if (tcp->tcp_ip_addr_cache) { 4507 kmem_free((void *)tcp->tcp_ip_addr_cache, 4508 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4509 tcp->tcp_ip_addr_cache = NULL; 4510 } 4511 } 4512 mutex_enter(&tcp->tcp_non_sq_lock); 4513 if (tcp->tcp_flow_stopped) 4514 tcp_clrqfull(tcp); 4515 mutex_exit(&tcp->tcp_non_sq_lock); 4516 4517 tcp_bind_hash_remove(tcp); 4518 /* 4519 * If the tcp_time_wait_collector (which runs outside the squeue) 4520 * is trying to remove this tcp from the time wait list, we will 4521 * block in tcp_time_wait_remove while trying to acquire the 4522 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4523 * requires the ipcl_hash_remove to be ordered after the 4524 * tcp_time_wait_remove for the refcnt checks to work correctly. 4525 */ 4526 if (tcp->tcp_state == TCPS_TIME_WAIT) 4527 (void) tcp_time_wait_remove(tcp, NULL); 4528 CL_INET_DISCONNECT(tcp); 4529 ipcl_hash_remove(connp); 4530 4531 /* 4532 * Delete the cached ire in conn_ire_cache and also mark 4533 * the conn as CONDEMNED 4534 */ 4535 mutex_enter(&connp->conn_lock); 4536 connp->conn_state_flags |= CONN_CONDEMNED; 4537 ire = connp->conn_ire_cache; 4538 connp->conn_ire_cache = NULL; 4539 mutex_exit(&connp->conn_lock); 4540 if (ire != NULL) 4541 IRE_REFRELE_NOTR(ire); 4542 4543 /* Need to cleanup any pending ioctls */ 4544 ASSERT(tcp->tcp_time_wait_next == NULL); 4545 ASSERT(tcp->tcp_time_wait_prev == NULL); 4546 ASSERT(tcp->tcp_time_wait_expire == 0); 4547 tcp->tcp_state = TCPS_CLOSED; 4548 4549 /* Release any SSL context */ 4550 if (tcp->tcp_kssl_ent != NULL) { 4551 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4552 tcp->tcp_kssl_ent = NULL; 4553 } 4554 if (tcp->tcp_kssl_ctx != NULL) { 4555 kssl_release_ctx(tcp->tcp_kssl_ctx); 4556 tcp->tcp_kssl_ctx = NULL; 4557 } 4558 tcp->tcp_kssl_pending = B_FALSE; 4559 4560 tcp_ipsec_cleanup(tcp); 4561 } 4562 4563 /* 4564 * tcp is dying (called from ipcl_conn_destroy and error cases). 4565 * Free the tcp_t in either case. 4566 */ 4567 void 4568 tcp_free(tcp_t *tcp) 4569 { 4570 mblk_t *mp; 4571 ip6_pkt_t *ipp; 4572 4573 ASSERT(tcp != NULL); 4574 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4575 4576 tcp->tcp_rq = NULL; 4577 tcp->tcp_wq = NULL; 4578 4579 tcp_close_mpp(&tcp->tcp_xmit_head); 4580 tcp_close_mpp(&tcp->tcp_reass_head); 4581 if (tcp->tcp_rcv_list != NULL) { 4582 /* Free b_next chain */ 4583 tcp_close_mpp(&tcp->tcp_rcv_list); 4584 } 4585 if ((mp = tcp->tcp_urp_mp) != NULL) { 4586 freemsg(mp); 4587 } 4588 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4589 freemsg(mp); 4590 } 4591 4592 if (tcp->tcp_fused_sigurg_mp != NULL) { 4593 freeb(tcp->tcp_fused_sigurg_mp); 4594 tcp->tcp_fused_sigurg_mp = NULL; 4595 } 4596 4597 if (tcp->tcp_sack_info != NULL) { 4598 if (tcp->tcp_notsack_list != NULL) { 4599 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4600 } 4601 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4602 } 4603 4604 if (tcp->tcp_hopopts != NULL) { 4605 mi_free(tcp->tcp_hopopts); 4606 tcp->tcp_hopopts = NULL; 4607 tcp->tcp_hopoptslen = 0; 4608 } 4609 ASSERT(tcp->tcp_hopoptslen == 0); 4610 if (tcp->tcp_dstopts != NULL) { 4611 mi_free(tcp->tcp_dstopts); 4612 tcp->tcp_dstopts = NULL; 4613 tcp->tcp_dstoptslen = 0; 4614 } 4615 ASSERT(tcp->tcp_dstoptslen == 0); 4616 if (tcp->tcp_rtdstopts != NULL) { 4617 mi_free(tcp->tcp_rtdstopts); 4618 tcp->tcp_rtdstopts = NULL; 4619 tcp->tcp_rtdstoptslen = 0; 4620 } 4621 ASSERT(tcp->tcp_rtdstoptslen == 0); 4622 if (tcp->tcp_rthdr != NULL) { 4623 mi_free(tcp->tcp_rthdr); 4624 tcp->tcp_rthdr = NULL; 4625 tcp->tcp_rthdrlen = 0; 4626 } 4627 ASSERT(tcp->tcp_rthdrlen == 0); 4628 4629 ipp = &tcp->tcp_sticky_ipp; 4630 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4631 IPPF_RTHDR)) 4632 ip6_pkt_free(ipp); 4633 4634 /* 4635 * Free memory associated with the tcp/ip header template. 4636 */ 4637 4638 if (tcp->tcp_iphc != NULL) 4639 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4640 4641 /* 4642 * Following is really a blowing away a union. 4643 * It happens to have exactly two members of identical size 4644 * the following code is enough. 4645 */ 4646 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4647 4648 if (tcp->tcp_tracebuf != NULL) { 4649 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4650 tcp->tcp_tracebuf = NULL; 4651 } 4652 } 4653 4654 4655 /* 4656 * Put a connection confirmation message upstream built from the 4657 * address information within 'iph' and 'tcph'. Report our success or failure. 4658 */ 4659 static boolean_t 4660 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4661 mblk_t **defermp) 4662 { 4663 sin_t sin; 4664 sin6_t sin6; 4665 mblk_t *mp; 4666 char *optp = NULL; 4667 int optlen = 0; 4668 cred_t *cr; 4669 4670 if (defermp != NULL) 4671 *defermp = NULL; 4672 4673 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4674 /* 4675 * Return in T_CONN_CON results of option negotiation through 4676 * the T_CONN_REQ. Note: If there is an real end-to-end option 4677 * negotiation, then what is received from remote end needs 4678 * to be taken into account but there is no such thing (yet?) 4679 * in our TCP/IP. 4680 * Note: We do not use mi_offset_param() here as 4681 * tcp_opts_conn_req contents do not directly come from 4682 * an application and are either generated in kernel or 4683 * from user input that was already verified. 4684 */ 4685 mp = tcp->tcp_conn.tcp_opts_conn_req; 4686 optp = (char *)(mp->b_rptr + 4687 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4688 optlen = (int) 4689 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4690 } 4691 4692 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4693 ipha_t *ipha = (ipha_t *)iphdr; 4694 4695 /* packet is IPv4 */ 4696 if (tcp->tcp_family == AF_INET) { 4697 sin = sin_null; 4698 sin.sin_addr.s_addr = ipha->ipha_src; 4699 sin.sin_port = *(uint16_t *)tcph->th_lport; 4700 sin.sin_family = AF_INET; 4701 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4702 (int)sizeof (sin_t), optp, optlen); 4703 } else { 4704 sin6 = sin6_null; 4705 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4706 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4707 sin6.sin6_family = AF_INET6; 4708 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4709 (int)sizeof (sin6_t), optp, optlen); 4710 4711 } 4712 } else { 4713 ip6_t *ip6h = (ip6_t *)iphdr; 4714 4715 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4716 ASSERT(tcp->tcp_family == AF_INET6); 4717 sin6 = sin6_null; 4718 sin6.sin6_addr = ip6h->ip6_src; 4719 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4720 sin6.sin6_family = AF_INET6; 4721 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4722 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4723 (int)sizeof (sin6_t), optp, optlen); 4724 } 4725 4726 if (!mp) 4727 return (B_FALSE); 4728 4729 if ((cr = DB_CRED(idmp)) != NULL) { 4730 mblk_setcred(mp, cr); 4731 DB_CPID(mp) = DB_CPID(idmp); 4732 } 4733 4734 if (defermp == NULL) 4735 putnext(tcp->tcp_rq, mp); 4736 else 4737 *defermp = mp; 4738 4739 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4740 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4741 return (B_TRUE); 4742 } 4743 4744 /* 4745 * Defense for the SYN attack - 4746 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4747 * one from the list of droppable eagers. This list is a subset of q0. 4748 * see comments before the definition of MAKE_DROPPABLE(). 4749 * 2. Don't drop a SYN request before its first timeout. This gives every 4750 * request at least til the first timeout to complete its 3-way handshake. 4751 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4752 * requests currently on the queue that has timed out. This will be used 4753 * as an indicator of whether an attack is under way, so that appropriate 4754 * actions can be taken. (It's incremented in tcp_timer() and decremented 4755 * either when eager goes into ESTABLISHED, or gets freed up.) 4756 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4757 * # of timeout drops back to <= q0len/32 => SYN alert off 4758 */ 4759 static boolean_t 4760 tcp_drop_q0(tcp_t *tcp) 4761 { 4762 tcp_t *eager; 4763 mblk_t *mp; 4764 tcp_stack_t *tcps = tcp->tcp_tcps; 4765 4766 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4767 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4768 4769 /* Pick oldest eager from the list of droppable eagers */ 4770 eager = tcp->tcp_eager_prev_drop_q0; 4771 4772 /* If list is empty. return B_FALSE */ 4773 if (eager == tcp) { 4774 return (B_FALSE); 4775 } 4776 4777 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4778 if ((mp = allocb(0, BPRI_HI)) == NULL) 4779 return (B_FALSE); 4780 4781 /* 4782 * Take this eager out from the list of droppable eagers since we are 4783 * going to drop it. 4784 */ 4785 MAKE_UNDROPPABLE(eager); 4786 4787 if (tcp->tcp_debug) { 4788 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4789 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4790 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4791 tcp->tcp_conn_req_cnt_q0, 4792 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4793 } 4794 4795 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4796 4797 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4798 CONN_INC_REF(eager->tcp_connp); 4799 4800 /* Mark the IRE created for this SYN request temporary */ 4801 tcp_ip_ire_mark_advice(eager); 4802 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4803 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4804 4805 return (B_TRUE); 4806 } 4807 4808 int 4809 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4810 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4811 { 4812 tcp_t *ltcp = lconnp->conn_tcp; 4813 tcp_t *tcp = connp->conn_tcp; 4814 mblk_t *tpi_mp; 4815 ipha_t *ipha; 4816 ip6_t *ip6h; 4817 sin6_t sin6; 4818 in6_addr_t v6dst; 4819 int err; 4820 int ifindex = 0; 4821 cred_t *cr; 4822 tcp_stack_t *tcps = tcp->tcp_tcps; 4823 4824 if (ipvers == IPV4_VERSION) { 4825 ipha = (ipha_t *)mp->b_rptr; 4826 4827 connp->conn_send = ip_output; 4828 connp->conn_recv = tcp_input; 4829 4830 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4831 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4832 4833 sin6 = sin6_null; 4834 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4835 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4836 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4837 sin6.sin6_family = AF_INET6; 4838 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4839 lconnp->conn_zoneid, tcps->tcps_netstack); 4840 if (tcp->tcp_recvdstaddr) { 4841 sin6_t sin6d; 4842 4843 sin6d = sin6_null; 4844 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4845 &sin6d.sin6_addr); 4846 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4847 sin6d.sin6_family = AF_INET; 4848 tpi_mp = mi_tpi_extconn_ind(NULL, 4849 (char *)&sin6d, sizeof (sin6_t), 4850 (char *)&tcp, 4851 (t_scalar_t)sizeof (intptr_t), 4852 (char *)&sin6d, sizeof (sin6_t), 4853 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4854 } else { 4855 tpi_mp = mi_tpi_conn_ind(NULL, 4856 (char *)&sin6, sizeof (sin6_t), 4857 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4858 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4859 } 4860 } else { 4861 ip6h = (ip6_t *)mp->b_rptr; 4862 4863 connp->conn_send = ip_output_v6; 4864 connp->conn_recv = tcp_input; 4865 4866 connp->conn_srcv6 = ip6h->ip6_dst; 4867 connp->conn_remv6 = ip6h->ip6_src; 4868 4869 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4870 ifindex = (int)DB_CKSUMSTUFF(mp); 4871 DB_CKSUMSTUFF(mp) = 0; 4872 4873 sin6 = sin6_null; 4874 sin6.sin6_addr = ip6h->ip6_src; 4875 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4876 sin6.sin6_family = AF_INET6; 4877 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4878 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4879 lconnp->conn_zoneid, tcps->tcps_netstack); 4880 4881 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4882 /* Pass up the scope_id of remote addr */ 4883 sin6.sin6_scope_id = ifindex; 4884 } else { 4885 sin6.sin6_scope_id = 0; 4886 } 4887 if (tcp->tcp_recvdstaddr) { 4888 sin6_t sin6d; 4889 4890 sin6d = sin6_null; 4891 sin6.sin6_addr = ip6h->ip6_dst; 4892 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4893 sin6d.sin6_family = AF_INET; 4894 tpi_mp = mi_tpi_extconn_ind(NULL, 4895 (char *)&sin6d, sizeof (sin6_t), 4896 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4897 (char *)&sin6d, sizeof (sin6_t), 4898 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4899 } else { 4900 tpi_mp = mi_tpi_conn_ind(NULL, 4901 (char *)&sin6, sizeof (sin6_t), 4902 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4903 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4904 } 4905 } 4906 4907 if (tpi_mp == NULL) 4908 return (ENOMEM); 4909 4910 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4911 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4912 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4913 connp->conn_fully_bound = B_FALSE; 4914 4915 if (tcps->tcps_trace) 4916 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4917 4918 /* Inherit information from the "parent" */ 4919 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4920 tcp->tcp_family = ltcp->tcp_family; 4921 tcp->tcp_wq = ltcp->tcp_wq; 4922 tcp->tcp_rq = ltcp->tcp_rq; 4923 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4924 tcp->tcp_detached = B_TRUE; 4925 if ((err = tcp_init_values(tcp)) != 0) { 4926 freemsg(tpi_mp); 4927 return (err); 4928 } 4929 4930 if (ipvers == IPV4_VERSION) { 4931 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4932 freemsg(tpi_mp); 4933 return (err); 4934 } 4935 ASSERT(tcp->tcp_ipha != NULL); 4936 } else { 4937 /* ifindex must be already set */ 4938 ASSERT(ifindex != 0); 4939 4940 if (ltcp->tcp_bound_if != 0) { 4941 /* 4942 * Set newtcp's bound_if equal to 4943 * listener's value. If ifindex is 4944 * not the same as ltcp->tcp_bound_if, 4945 * it must be a packet for the ipmp group 4946 * of interfaces 4947 */ 4948 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4949 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4950 tcp->tcp_bound_if = ifindex; 4951 } 4952 4953 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4954 tcp->tcp_recvifindex = 0; 4955 tcp->tcp_recvhops = 0xffffffffU; 4956 ASSERT(tcp->tcp_ip6h != NULL); 4957 } 4958 4959 tcp->tcp_lport = ltcp->tcp_lport; 4960 4961 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4962 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4963 /* 4964 * Listener had options of some sort; eager inherits. 4965 * Free up the eager template and allocate one 4966 * of the right size. 4967 */ 4968 if (tcp->tcp_hdr_grown) { 4969 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4970 } else { 4971 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4972 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4973 } 4974 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4975 KM_NOSLEEP); 4976 if (tcp->tcp_iphc == NULL) { 4977 tcp->tcp_iphc_len = 0; 4978 freemsg(tpi_mp); 4979 return (ENOMEM); 4980 } 4981 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4982 tcp->tcp_hdr_grown = B_TRUE; 4983 } 4984 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4985 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4986 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4987 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4988 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4989 4990 /* 4991 * Copy the IP+TCP header template from listener to eager 4992 */ 4993 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4994 if (tcp->tcp_ipversion == IPV6_VERSION) { 4995 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4996 IPPROTO_RAW) { 4997 tcp->tcp_ip6h = 4998 (ip6_t *)(tcp->tcp_iphc + 4999 sizeof (ip6i_t)); 5000 } else { 5001 tcp->tcp_ip6h = 5002 (ip6_t *)(tcp->tcp_iphc); 5003 } 5004 tcp->tcp_ipha = NULL; 5005 } else { 5006 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5007 tcp->tcp_ip6h = NULL; 5008 } 5009 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5010 tcp->tcp_ip_hdr_len); 5011 } else { 5012 /* 5013 * only valid case when ipversion of listener and 5014 * eager differ is when listener is IPv6 and 5015 * eager is IPv4. 5016 * Eager header template has been initialized to the 5017 * maximum v4 header sizes, which includes space for 5018 * TCP and IP options. 5019 */ 5020 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5021 (tcp->tcp_ipversion == IPV4_VERSION)); 5022 ASSERT(tcp->tcp_iphc_len >= 5023 TCP_MAX_COMBINED_HEADER_LENGTH); 5024 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5025 /* copy IP header fields individually */ 5026 tcp->tcp_ipha->ipha_ttl = 5027 ltcp->tcp_ip6h->ip6_hops; 5028 bcopy(ltcp->tcp_tcph->th_lport, 5029 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5030 } 5031 5032 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5033 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5034 sizeof (in_port_t)); 5035 5036 if (ltcp->tcp_lport == 0) { 5037 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5038 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5039 sizeof (in_port_t)); 5040 } 5041 5042 if (tcp->tcp_ipversion == IPV4_VERSION) { 5043 ASSERT(ipha != NULL); 5044 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5045 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5046 5047 /* Source routing option copyover (reverse it) */ 5048 if (tcps->tcps_rev_src_routes) 5049 tcp_opt_reverse(tcp, ipha); 5050 } else { 5051 ASSERT(ip6h != NULL); 5052 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5053 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5054 } 5055 5056 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5057 ASSERT(!tcp->tcp_tconnind_started); 5058 /* 5059 * If the SYN contains a credential, it's a loopback packet; attach 5060 * the credential to the TPI message. 5061 */ 5062 if ((cr = DB_CRED(idmp)) != NULL) { 5063 mblk_setcred(tpi_mp, cr); 5064 DB_CPID(tpi_mp) = DB_CPID(idmp); 5065 } 5066 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5067 5068 /* Inherit the listener's SSL protection state */ 5069 5070 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5071 kssl_hold_ent(tcp->tcp_kssl_ent); 5072 tcp->tcp_kssl_pending = B_TRUE; 5073 } 5074 5075 return (0); 5076 } 5077 5078 5079 int 5080 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5081 tcph_t *tcph, mblk_t *idmp) 5082 { 5083 tcp_t *ltcp = lconnp->conn_tcp; 5084 tcp_t *tcp = connp->conn_tcp; 5085 sin_t sin; 5086 mblk_t *tpi_mp = NULL; 5087 int err; 5088 cred_t *cr; 5089 tcp_stack_t *tcps = tcp->tcp_tcps; 5090 5091 sin = sin_null; 5092 sin.sin_addr.s_addr = ipha->ipha_src; 5093 sin.sin_port = *(uint16_t *)tcph->th_lport; 5094 sin.sin_family = AF_INET; 5095 if (ltcp->tcp_recvdstaddr) { 5096 sin_t sind; 5097 5098 sind = sin_null; 5099 sind.sin_addr.s_addr = ipha->ipha_dst; 5100 sind.sin_port = *(uint16_t *)tcph->th_fport; 5101 sind.sin_family = AF_INET; 5102 tpi_mp = mi_tpi_extconn_ind(NULL, 5103 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5104 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5105 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5106 } else { 5107 tpi_mp = mi_tpi_conn_ind(NULL, 5108 (char *)&sin, sizeof (sin_t), 5109 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5110 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5111 } 5112 5113 if (tpi_mp == NULL) { 5114 return (ENOMEM); 5115 } 5116 5117 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5118 connp->conn_send = ip_output; 5119 connp->conn_recv = tcp_input; 5120 connp->conn_fully_bound = B_FALSE; 5121 5122 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5123 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5124 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5125 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5126 5127 if (tcps->tcps_trace) { 5128 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 5129 } 5130 5131 /* Inherit information from the "parent" */ 5132 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5133 tcp->tcp_family = ltcp->tcp_family; 5134 tcp->tcp_wq = ltcp->tcp_wq; 5135 tcp->tcp_rq = ltcp->tcp_rq; 5136 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 5137 tcp->tcp_detached = B_TRUE; 5138 if ((err = tcp_init_values(tcp)) != 0) { 5139 freemsg(tpi_mp); 5140 return (err); 5141 } 5142 5143 /* 5144 * Let's make sure that eager tcp template has enough space to 5145 * copy IPv4 listener's tcp template. Since the conn_t structure is 5146 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5147 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5148 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5149 * extension headers or with ip6i_t struct). Note that bcopy() below 5150 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5151 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5152 */ 5153 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5154 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5155 5156 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5157 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5158 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5159 tcp->tcp_ttl = ltcp->tcp_ttl; 5160 tcp->tcp_tos = ltcp->tcp_tos; 5161 5162 /* Copy the IP+TCP header template from listener to eager */ 5163 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5164 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5165 tcp->tcp_ip6h = NULL; 5166 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5167 tcp->tcp_ip_hdr_len); 5168 5169 /* Initialize the IP addresses and Ports */ 5170 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5171 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5172 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5173 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5174 5175 /* Source routing option copyover (reverse it) */ 5176 if (tcps->tcps_rev_src_routes) 5177 tcp_opt_reverse(tcp, ipha); 5178 5179 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5180 ASSERT(!tcp->tcp_tconnind_started); 5181 5182 /* 5183 * If the SYN contains a credential, it's a loopback packet; attach 5184 * the credential to the TPI message. 5185 */ 5186 if ((cr = DB_CRED(idmp)) != NULL) { 5187 mblk_setcred(tpi_mp, cr); 5188 DB_CPID(tpi_mp) = DB_CPID(idmp); 5189 } 5190 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5191 5192 /* Inherit the listener's SSL protection state */ 5193 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5194 kssl_hold_ent(tcp->tcp_kssl_ent); 5195 tcp->tcp_kssl_pending = B_TRUE; 5196 } 5197 5198 return (0); 5199 } 5200 5201 /* 5202 * sets up conn for ipsec. 5203 * if the first mblk is M_CTL it is consumed and mpp is updated. 5204 * in case of error mpp is freed. 5205 */ 5206 conn_t * 5207 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5208 { 5209 conn_t *connp = tcp->tcp_connp; 5210 conn_t *econnp; 5211 squeue_t *new_sqp; 5212 mblk_t *first_mp = *mpp; 5213 mblk_t *mp = *mpp; 5214 boolean_t mctl_present = B_FALSE; 5215 uint_t ipvers; 5216 5217 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 5218 if (econnp == NULL) { 5219 freemsg(first_mp); 5220 return (NULL); 5221 } 5222 if (DB_TYPE(mp) == M_CTL) { 5223 if (mp->b_cont == NULL || 5224 mp->b_cont->b_datap->db_type != M_DATA) { 5225 freemsg(first_mp); 5226 return (NULL); 5227 } 5228 mp = mp->b_cont; 5229 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5230 freemsg(first_mp); 5231 return (NULL); 5232 } 5233 5234 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5235 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5236 mctl_present = B_TRUE; 5237 } else { 5238 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5239 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5240 } 5241 5242 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5243 DB_CKSUMSTART(mp) = 0; 5244 5245 ASSERT(OK_32PTR(mp->b_rptr)); 5246 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5247 if (ipvers == IPV4_VERSION) { 5248 uint16_t *up; 5249 uint32_t ports; 5250 ipha_t *ipha; 5251 5252 ipha = (ipha_t *)mp->b_rptr; 5253 up = (uint16_t *)((uchar_t *)ipha + 5254 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5255 ports = *(uint32_t *)up; 5256 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5257 ipha->ipha_dst, ipha->ipha_src, ports); 5258 } else { 5259 uint16_t *up; 5260 uint32_t ports; 5261 uint16_t ip_hdr_len; 5262 uint8_t *nexthdrp; 5263 ip6_t *ip6h; 5264 tcph_t *tcph; 5265 5266 ip6h = (ip6_t *)mp->b_rptr; 5267 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5268 ip_hdr_len = IPV6_HDR_LEN; 5269 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5270 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5271 CONN_DEC_REF(econnp); 5272 freemsg(first_mp); 5273 return (NULL); 5274 } 5275 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5276 up = (uint16_t *)tcph->th_lport; 5277 ports = *(uint32_t *)up; 5278 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5279 ip6h->ip6_dst, ip6h->ip6_src, ports); 5280 } 5281 5282 /* 5283 * The caller already ensured that there is a sqp present. 5284 */ 5285 econnp->conn_sqp = new_sqp; 5286 5287 if (connp->conn_policy != NULL) { 5288 ipsec_in_t *ii; 5289 ii = (ipsec_in_t *)(first_mp->b_rptr); 5290 ASSERT(ii->ipsec_in_policy == NULL); 5291 IPPH_REFHOLD(connp->conn_policy); 5292 ii->ipsec_in_policy = connp->conn_policy; 5293 5294 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5295 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5296 CONN_DEC_REF(econnp); 5297 freemsg(first_mp); 5298 return (NULL); 5299 } 5300 } 5301 5302 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5303 CONN_DEC_REF(econnp); 5304 freemsg(first_mp); 5305 return (NULL); 5306 } 5307 5308 /* 5309 * If we know we have some policy, pass the "IPSEC" 5310 * options size TCP uses this adjust the MSS. 5311 */ 5312 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5313 if (mctl_present) { 5314 freeb(first_mp); 5315 *mpp = mp; 5316 } 5317 5318 return (econnp); 5319 } 5320 5321 /* 5322 * tcp_get_conn/tcp_free_conn 5323 * 5324 * tcp_get_conn is used to get a clean tcp connection structure. 5325 * It tries to reuse the connections put on the freelist by the 5326 * time_wait_collector failing which it goes to kmem_cache. This 5327 * way has two benefits compared to just allocating from and 5328 * freeing to kmem_cache. 5329 * 1) The time_wait_collector can free (which includes the cleanup) 5330 * outside the squeue. So when the interrupt comes, we have a clean 5331 * connection sitting in the freelist. Obviously, this buys us 5332 * performance. 5333 * 5334 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5335 * has multiple disadvantages - tying up the squeue during alloc, and the 5336 * fact that IPSec policy initialization has to happen here which 5337 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5338 * But allocating the conn/tcp in IP land is also not the best since 5339 * we can't check the 'q' and 'q0' which are protected by squeue and 5340 * blindly allocate memory which might have to be freed here if we are 5341 * not allowed to accept the connection. By using the freelist and 5342 * putting the conn/tcp back in freelist, we don't pay a penalty for 5343 * allocating memory without checking 'q/q0' and freeing it if we can't 5344 * accept the connection. 5345 * 5346 * Care should be taken to put the conn back in the same squeue's freelist 5347 * from which it was allocated. Best results are obtained if conn is 5348 * allocated from listener's squeue and freed to the same. Time wait 5349 * collector will free up the freelist is the connection ends up sitting 5350 * there for too long. 5351 */ 5352 void * 5353 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5354 { 5355 tcp_t *tcp = NULL; 5356 conn_t *connp = NULL; 5357 squeue_t *sqp = (squeue_t *)arg; 5358 tcp_squeue_priv_t *tcp_time_wait; 5359 netstack_t *ns; 5360 5361 tcp_time_wait = 5362 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5363 5364 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5365 tcp = tcp_time_wait->tcp_free_list; 5366 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5367 if (tcp != NULL) { 5368 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5369 tcp_time_wait->tcp_free_list_cnt--; 5370 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5371 tcp->tcp_time_wait_next = NULL; 5372 connp = tcp->tcp_connp; 5373 connp->conn_flags |= IPCL_REUSED; 5374 5375 ASSERT(tcp->tcp_tcps == NULL); 5376 ASSERT(connp->conn_netstack == NULL); 5377 ns = tcps->tcps_netstack; 5378 netstack_hold(ns); 5379 connp->conn_netstack = ns; 5380 tcp->tcp_tcps = tcps; 5381 TCPS_REFHOLD(tcps); 5382 ipcl_globalhash_insert(connp); 5383 return ((void *)connp); 5384 } 5385 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5386 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5387 tcps->tcps_netstack)) == NULL) 5388 return (NULL); 5389 tcp = connp->conn_tcp; 5390 tcp->tcp_tcps = tcps; 5391 TCPS_REFHOLD(tcps); 5392 return ((void *)connp); 5393 } 5394 5395 /* 5396 * Update the cached label for the given tcp_t. This should be called once per 5397 * connection, and before any packets are sent or tcp_process_options is 5398 * invoked. Returns B_FALSE if the correct label could not be constructed. 5399 */ 5400 static boolean_t 5401 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5402 { 5403 conn_t *connp = tcp->tcp_connp; 5404 5405 if (tcp->tcp_ipversion == IPV4_VERSION) { 5406 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5407 int added; 5408 5409 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5410 connp->conn_mac_exempt, 5411 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5412 return (B_FALSE); 5413 5414 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5415 if (added == -1) 5416 return (B_FALSE); 5417 tcp->tcp_hdr_len += added; 5418 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5419 tcp->tcp_ip_hdr_len += added; 5420 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5421 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5422 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5423 tcp->tcp_hdr_len); 5424 if (added == -1) 5425 return (B_FALSE); 5426 tcp->tcp_hdr_len += added; 5427 tcp->tcp_tcph = (tcph_t *) 5428 ((uchar_t *)tcp->tcp_tcph + added); 5429 tcp->tcp_ip_hdr_len += added; 5430 } 5431 } else { 5432 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5433 5434 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5435 connp->conn_mac_exempt, 5436 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5437 return (B_FALSE); 5438 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5439 &tcp->tcp_label_len, optbuf) != 0) 5440 return (B_FALSE); 5441 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5442 return (B_FALSE); 5443 } 5444 5445 connp->conn_ulp_labeled = 1; 5446 5447 return (B_TRUE); 5448 } 5449 5450 /* BEGIN CSTYLED */ 5451 /* 5452 * 5453 * The sockfs ACCEPT path: 5454 * ======================= 5455 * 5456 * The eager is now established in its own perimeter as soon as SYN is 5457 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5458 * completes the accept processing on the acceptor STREAM. The sending 5459 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5460 * listener but a TLI/XTI listener completes the accept processing 5461 * on the listener perimeter. 5462 * 5463 * Common control flow for 3 way handshake: 5464 * ---------------------------------------- 5465 * 5466 * incoming SYN (listener perimeter) -> tcp_rput_data() 5467 * -> tcp_conn_request() 5468 * 5469 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5470 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5471 * 5472 * Sockfs ACCEPT Path: 5473 * ------------------- 5474 * 5475 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5476 * as STREAM entry point) 5477 * 5478 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5479 * 5480 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5481 * association (we are not behind eager's squeue but sockfs is protecting us 5482 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5483 * is changed to point at tcp_wput(). 5484 * 5485 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5486 * listener (done on listener's perimeter). 5487 * 5488 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5489 * accept. 5490 * 5491 * TLI/XTI client ACCEPT path: 5492 * --------------------------- 5493 * 5494 * soaccept() sends T_CONN_RES on the listener STREAM. 5495 * 5496 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5497 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5498 * 5499 * Locks: 5500 * ====== 5501 * 5502 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5503 * and listeners->tcp_eager_next_q. 5504 * 5505 * Referencing: 5506 * ============ 5507 * 5508 * 1) We start out in tcp_conn_request by eager placing a ref on 5509 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5510 * 5511 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5512 * doing so we place a ref on the eager. This ref is finally dropped at the 5513 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5514 * reference is dropped by the squeue framework. 5515 * 5516 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5517 * 5518 * The reference must be released by the same entity that added the reference 5519 * In the above scheme, the eager is the entity that adds and releases the 5520 * references. Note that tcp_accept_finish executes in the squeue of the eager 5521 * (albeit after it is attached to the acceptor stream). Though 1. executes 5522 * in the listener's squeue, the eager is nascent at this point and the 5523 * reference can be considered to have been added on behalf of the eager. 5524 * 5525 * Eager getting a Reset or listener closing: 5526 * ========================================== 5527 * 5528 * Once the listener and eager are linked, the listener never does the unlink. 5529 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5530 * a message on all eager perimeter. The eager then does the unlink, clears 5531 * any pointers to the listener's queue and drops the reference to the 5532 * listener. The listener waits in tcp_close outside the squeue until its 5533 * refcount has dropped to 1. This ensures that the listener has waited for 5534 * all eagers to clear their association with the listener. 5535 * 5536 * Similarly, if eager decides to go away, it can unlink itself and close. 5537 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5538 * the reference to eager is still valid because of the extra ref we put 5539 * in tcp_send_conn_ind. 5540 * 5541 * Listener can always locate the eager under the protection 5542 * of the listener->tcp_eager_lock, and then do a refhold 5543 * on the eager during the accept processing. 5544 * 5545 * The acceptor stream accesses the eager in the accept processing 5546 * based on the ref placed on eager before sending T_conn_ind. 5547 * The only entity that can negate this refhold is a listener close 5548 * which is mutually exclusive with an active acceptor stream. 5549 * 5550 * Eager's reference on the listener 5551 * =================================== 5552 * 5553 * If the accept happens (even on a closed eager) the eager drops its 5554 * reference on the listener at the start of tcp_accept_finish. If the 5555 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5556 * the reference is dropped in tcp_closei_local. If the listener closes, 5557 * the reference is dropped in tcp_eager_kill. In all cases the reference 5558 * is dropped while executing in the eager's context (squeue). 5559 */ 5560 /* END CSTYLED */ 5561 5562 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5563 5564 /* 5565 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5566 * tcp_rput_data will not see any SYN packets. 5567 */ 5568 /* ARGSUSED */ 5569 void 5570 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5571 { 5572 tcph_t *tcph; 5573 uint32_t seg_seq; 5574 tcp_t *eager; 5575 uint_t ipvers; 5576 ipha_t *ipha; 5577 ip6_t *ip6h; 5578 int err; 5579 conn_t *econnp = NULL; 5580 squeue_t *new_sqp; 5581 mblk_t *mp1; 5582 uint_t ip_hdr_len; 5583 conn_t *connp = (conn_t *)arg; 5584 tcp_t *tcp = connp->conn_tcp; 5585 ire_t *ire; 5586 cred_t *credp; 5587 tcp_stack_t *tcps = tcp->tcp_tcps; 5588 ip_stack_t *ipst; 5589 5590 if (tcp->tcp_state != TCPS_LISTEN) 5591 goto error2; 5592 5593 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5594 5595 mutex_enter(&tcp->tcp_eager_lock); 5596 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5597 mutex_exit(&tcp->tcp_eager_lock); 5598 TCP_STAT(tcps, tcp_listendrop); 5599 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5600 if (tcp->tcp_debug) { 5601 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5602 "tcp_conn_request: listen backlog (max=%d) " 5603 "overflow (%d pending) on %s", 5604 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5605 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5606 } 5607 goto error2; 5608 } 5609 5610 if (tcp->tcp_conn_req_cnt_q0 >= 5611 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5612 /* 5613 * Q0 is full. Drop a pending half-open req from the queue 5614 * to make room for the new SYN req. Also mark the time we 5615 * drop a SYN. 5616 * 5617 * A more aggressive defense against SYN attack will 5618 * be to set the "tcp_syn_defense" flag now. 5619 */ 5620 TCP_STAT(tcps, tcp_listendropq0); 5621 tcp->tcp_last_rcv_lbolt = lbolt64; 5622 if (!tcp_drop_q0(tcp)) { 5623 mutex_exit(&tcp->tcp_eager_lock); 5624 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5625 if (tcp->tcp_debug) { 5626 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5627 "tcp_conn_request: listen half-open queue " 5628 "(max=%d) full (%d pending) on %s", 5629 tcps->tcps_conn_req_max_q0, 5630 tcp->tcp_conn_req_cnt_q0, 5631 tcp_display(tcp, NULL, 5632 DISP_PORT_ONLY)); 5633 } 5634 goto error2; 5635 } 5636 } 5637 mutex_exit(&tcp->tcp_eager_lock); 5638 5639 /* 5640 * IP adds STRUIO_EAGER and ensures that the received packet is 5641 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5642 * link local address. If IPSec is enabled, db_struioflag has 5643 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5644 * otherwise an error case if neither of them is set. 5645 */ 5646 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5647 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5648 DB_CKSUMSTART(mp) = 0; 5649 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5650 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5651 if (econnp == NULL) 5652 goto error2; 5653 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5654 econnp->conn_sqp = new_sqp; 5655 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5656 /* 5657 * mp is updated in tcp_get_ipsec_conn(). 5658 */ 5659 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5660 if (econnp == NULL) { 5661 /* 5662 * mp freed by tcp_get_ipsec_conn. 5663 */ 5664 return; 5665 } 5666 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5667 } else { 5668 goto error2; 5669 } 5670 5671 ASSERT(DB_TYPE(mp) == M_DATA); 5672 5673 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5674 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5675 ASSERT(OK_32PTR(mp->b_rptr)); 5676 if (ipvers == IPV4_VERSION) { 5677 ipha = (ipha_t *)mp->b_rptr; 5678 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5679 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5680 } else { 5681 ip6h = (ip6_t *)mp->b_rptr; 5682 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5683 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5684 } 5685 5686 if (tcp->tcp_family == AF_INET) { 5687 ASSERT(ipvers == IPV4_VERSION); 5688 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5689 } else { 5690 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5691 } 5692 5693 if (err) 5694 goto error3; 5695 5696 eager = econnp->conn_tcp; 5697 5698 /* Inherit various TCP parameters from the listener */ 5699 eager->tcp_naglim = tcp->tcp_naglim; 5700 eager->tcp_first_timer_threshold = 5701 tcp->tcp_first_timer_threshold; 5702 eager->tcp_second_timer_threshold = 5703 tcp->tcp_second_timer_threshold; 5704 5705 eager->tcp_first_ctimer_threshold = 5706 tcp->tcp_first_ctimer_threshold; 5707 eager->tcp_second_ctimer_threshold = 5708 tcp->tcp_second_ctimer_threshold; 5709 5710 /* 5711 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5712 * If it does not, the eager's receive window will be set to the 5713 * listener's receive window later in this function. 5714 */ 5715 eager->tcp_rwnd = 0; 5716 5717 /* 5718 * Inherit listener's tcp_init_cwnd. Need to do this before 5719 * calling tcp_process_options() where tcp_mss_set() is called 5720 * to set the initial cwnd. 5721 */ 5722 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5723 5724 /* 5725 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5726 * zone id before the accept is completed in tcp_wput_accept(). 5727 */ 5728 econnp->conn_zoneid = connp->conn_zoneid; 5729 econnp->conn_allzones = connp->conn_allzones; 5730 5731 /* Copy nexthop information from listener to eager */ 5732 if (connp->conn_nexthop_set) { 5733 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5734 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5735 } 5736 5737 /* 5738 * TSOL: tsol_input_proc() needs the eager's cred before the 5739 * eager is accepted 5740 */ 5741 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5742 crhold(credp); 5743 5744 /* 5745 * If the caller has the process-wide flag set, then default to MAC 5746 * exempt mode. This allows read-down to unlabeled hosts. 5747 */ 5748 if (getpflags(NET_MAC_AWARE, credp) != 0) 5749 econnp->conn_mac_exempt = B_TRUE; 5750 5751 if (is_system_labeled()) { 5752 cred_t *cr; 5753 5754 if (connp->conn_mlp_type != mlptSingle) { 5755 cr = econnp->conn_peercred = DB_CRED(mp); 5756 if (cr != NULL) 5757 crhold(cr); 5758 else 5759 cr = econnp->conn_cred; 5760 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5761 econnp, cred_t *, cr) 5762 } else { 5763 cr = econnp->conn_cred; 5764 DTRACE_PROBE2(syn_accept, conn_t *, 5765 econnp, cred_t *, cr) 5766 } 5767 5768 if (!tcp_update_label(eager, cr)) { 5769 DTRACE_PROBE3( 5770 tx__ip__log__error__connrequest__tcp, 5771 char *, "eager connp(1) label on SYN mp(2) failed", 5772 conn_t *, econnp, mblk_t *, mp); 5773 goto error3; 5774 } 5775 } 5776 5777 eager->tcp_hard_binding = B_TRUE; 5778 5779 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5780 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5781 5782 CL_INET_CONNECT(eager); 5783 5784 /* 5785 * No need to check for multicast destination since ip will only pass 5786 * up multicasts to those that have expressed interest 5787 * TODO: what about rejecting broadcasts? 5788 * Also check that source is not a multicast or broadcast address. 5789 */ 5790 eager->tcp_state = TCPS_SYN_RCVD; 5791 5792 5793 /* 5794 * There should be no ire in the mp as we are being called after 5795 * receiving the SYN. 5796 */ 5797 ASSERT(tcp_ire_mp(mp) == NULL); 5798 5799 /* 5800 * Adapt our mss, ttl, ... according to information provided in IRE. 5801 */ 5802 5803 if (tcp_adapt_ire(eager, NULL) == 0) { 5804 /* Undo the bind_hash_insert */ 5805 tcp_bind_hash_remove(eager); 5806 goto error3; 5807 } 5808 5809 /* Process all TCP options. */ 5810 tcp_process_options(eager, tcph); 5811 5812 /* Is the other end ECN capable? */ 5813 if (tcps->tcps_ecn_permitted >= 1 && 5814 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5815 eager->tcp_ecn_ok = B_TRUE; 5816 } 5817 5818 /* 5819 * listener->tcp_rq->q_hiwat should be the default window size or a 5820 * window size changed via SO_RCVBUF option. First round up the 5821 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5822 * scale option value if needed. Call tcp_rwnd_set() to finish the 5823 * setting. 5824 * 5825 * Note if there is a rpipe metric associated with the remote host, 5826 * we should not inherit receive window size from listener. 5827 */ 5828 eager->tcp_rwnd = MSS_ROUNDUP( 5829 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5830 eager->tcp_rwnd), eager->tcp_mss); 5831 if (eager->tcp_snd_ws_ok) 5832 tcp_set_ws_value(eager); 5833 /* 5834 * Note that this is the only place tcp_rwnd_set() is called for 5835 * accepting a connection. We need to call it here instead of 5836 * after the 3-way handshake because we need to tell the other 5837 * side our rwnd in the SYN-ACK segment. 5838 */ 5839 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5840 5841 /* 5842 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5843 * via soaccept()->soinheritoptions() which essentially applies 5844 * all the listener options to the new STREAM. The options that we 5845 * need to take care of are: 5846 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5847 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5848 * SO_SNDBUF, SO_RCVBUF. 5849 * 5850 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5851 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5852 * tcp_maxpsz_set() gets called later from 5853 * tcp_accept_finish(), the option takes effect. 5854 * 5855 */ 5856 /* Set the TCP options */ 5857 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5858 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5859 eager->tcp_oobinline = tcp->tcp_oobinline; 5860 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5861 eager->tcp_broadcast = tcp->tcp_broadcast; 5862 eager->tcp_useloopback = tcp->tcp_useloopback; 5863 eager->tcp_dontroute = tcp->tcp_dontroute; 5864 eager->tcp_linger = tcp->tcp_linger; 5865 eager->tcp_lingertime = tcp->tcp_lingertime; 5866 if (tcp->tcp_ka_enabled) 5867 eager->tcp_ka_enabled = 1; 5868 5869 /* Set the IP options */ 5870 econnp->conn_broadcast = connp->conn_broadcast; 5871 econnp->conn_loopback = connp->conn_loopback; 5872 econnp->conn_dontroute = connp->conn_dontroute; 5873 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5874 5875 /* Put a ref on the listener for the eager. */ 5876 CONN_INC_REF(connp); 5877 mutex_enter(&tcp->tcp_eager_lock); 5878 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5879 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5880 tcp->tcp_eager_next_q0 = eager; 5881 eager->tcp_eager_prev_q0 = tcp; 5882 5883 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5884 eager->tcp_listener = tcp; 5885 eager->tcp_saved_listener = tcp; 5886 5887 /* 5888 * Tag this detached tcp vector for later retrieval 5889 * by our listener client in tcp_accept(). 5890 */ 5891 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5892 tcp->tcp_conn_req_cnt_q0++; 5893 if (++tcp->tcp_conn_req_seqnum == -1) { 5894 /* 5895 * -1 is "special" and defined in TPI as something 5896 * that should never be used in T_CONN_IND 5897 */ 5898 ++tcp->tcp_conn_req_seqnum; 5899 } 5900 mutex_exit(&tcp->tcp_eager_lock); 5901 5902 if (tcp->tcp_syn_defense) { 5903 /* Don't drop the SYN that comes from a good IP source */ 5904 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5905 if (addr_cache != NULL && eager->tcp_remote == 5906 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5907 eager->tcp_dontdrop = B_TRUE; 5908 } 5909 } 5910 5911 /* 5912 * We need to insert the eager in its own perimeter but as soon 5913 * as we do that, we expose the eager to the classifier and 5914 * should not touch any field outside the eager's perimeter. 5915 * So do all the work necessary before inserting the eager 5916 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5917 * will succeed but undo everything if it fails. 5918 */ 5919 seg_seq = ABE32_TO_U32(tcph->th_seq); 5920 eager->tcp_irs = seg_seq; 5921 eager->tcp_rack = seg_seq; 5922 eager->tcp_rnxt = seg_seq + 1; 5923 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5924 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5925 eager->tcp_state = TCPS_SYN_RCVD; 5926 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5927 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5928 if (mp1 == NULL) 5929 goto error1; 5930 DB_CPID(mp1) = tcp->tcp_cpid; 5931 eager->tcp_cpid = tcp->tcp_cpid; 5932 eager->tcp_open_time = lbolt64; 5933 5934 /* 5935 * We need to start the rto timer. In normal case, we start 5936 * the timer after sending the packet on the wire (or at 5937 * least believing that packet was sent by waiting for 5938 * CALL_IP_WPUT() to return). Since this is the first packet 5939 * being sent on the wire for the eager, our initial tcp_rto 5940 * is at least tcp_rexmit_interval_min which is a fairly 5941 * large value to allow the algorithm to adjust slowly to large 5942 * fluctuations of RTT during first few transmissions. 5943 * 5944 * Starting the timer first and then sending the packet in this 5945 * case shouldn't make much difference since tcp_rexmit_interval_min 5946 * is of the order of several 100ms and starting the timer 5947 * first and then sending the packet will result in difference 5948 * of few micro seconds. 5949 * 5950 * Without this optimization, we are forced to hold the fanout 5951 * lock across the ipcl_bind_insert() and sending the packet 5952 * so that we don't race against an incoming packet (maybe RST) 5953 * for this eager. 5954 * 5955 * It is necessary to acquire an extra reference on the eager 5956 * at this point and hold it until after tcp_send_data() to 5957 * ensure against an eager close race. 5958 */ 5959 5960 CONN_INC_REF(eager->tcp_connp); 5961 5962 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5963 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5964 5965 5966 /* 5967 * Insert the eager in its own perimeter now. We are ready to deal 5968 * with any packets on eager. 5969 */ 5970 if (eager->tcp_ipversion == IPV4_VERSION) { 5971 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5972 goto error; 5973 } 5974 } else { 5975 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5976 goto error; 5977 } 5978 } 5979 5980 /* mark conn as fully-bound */ 5981 econnp->conn_fully_bound = B_TRUE; 5982 5983 /* Send the SYN-ACK */ 5984 tcp_send_data(eager, eager->tcp_wq, mp1); 5985 CONN_DEC_REF(eager->tcp_connp); 5986 freemsg(mp); 5987 5988 return; 5989 error: 5990 (void) TCP_TIMER_CANCEL(eager, eager->tcp_timer_tid); 5991 CONN_DEC_REF(eager->tcp_connp); 5992 freemsg(mp1); 5993 error1: 5994 /* Undo what we did above */ 5995 mutex_enter(&tcp->tcp_eager_lock); 5996 tcp_eager_unlink(eager); 5997 mutex_exit(&tcp->tcp_eager_lock); 5998 /* Drop eager's reference on the listener */ 5999 CONN_DEC_REF(connp); 6000 6001 /* 6002 * Delete the cached ire in conn_ire_cache and also mark 6003 * the conn as CONDEMNED 6004 */ 6005 mutex_enter(&econnp->conn_lock); 6006 econnp->conn_state_flags |= CONN_CONDEMNED; 6007 ire = econnp->conn_ire_cache; 6008 econnp->conn_ire_cache = NULL; 6009 mutex_exit(&econnp->conn_lock); 6010 if (ire != NULL) 6011 IRE_REFRELE_NOTR(ire); 6012 6013 /* 6014 * tcp_accept_comm inserts the eager to the bind_hash 6015 * we need to remove it from the hash if ipcl_conn_insert 6016 * fails. 6017 */ 6018 tcp_bind_hash_remove(eager); 6019 /* Drop the eager ref placed in tcp_open_detached */ 6020 CONN_DEC_REF(econnp); 6021 6022 /* 6023 * If a connection already exists, send the mp to that connections so 6024 * that it can be appropriately dealt with. 6025 */ 6026 ipst = tcps->tcps_netstack->netstack_ip; 6027 6028 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 6029 if (!IPCL_IS_CONNECTED(econnp)) { 6030 /* 6031 * Something bad happened. ipcl_conn_insert() 6032 * failed because a connection already existed 6033 * in connected hash but we can't find it 6034 * anymore (someone blew it away). Just 6035 * free this message and hopefully remote 6036 * will retransmit at which time the SYN can be 6037 * treated as a new connection or dealth with 6038 * a TH_RST if a connection already exists. 6039 */ 6040 CONN_DEC_REF(econnp); 6041 freemsg(mp); 6042 } else { 6043 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6044 econnp, SQTAG_TCP_CONN_REQ); 6045 } 6046 } else { 6047 /* Nobody wants this packet */ 6048 freemsg(mp); 6049 } 6050 return; 6051 error2: 6052 freemsg(mp); 6053 return; 6054 error3: 6055 CONN_DEC_REF(econnp); 6056 freemsg(mp); 6057 } 6058 6059 /* 6060 * In an ideal case of vertical partition in NUMA architecture, its 6061 * beneficial to have the listener and all the incoming connections 6062 * tied to the same squeue. The other constraint is that incoming 6063 * connections should be tied to the squeue attached to interrupted 6064 * CPU for obvious locality reason so this leaves the listener to 6065 * be tied to the same squeue. Our only problem is that when listener 6066 * is binding, the CPU that will get interrupted by the NIC whose 6067 * IP address the listener is binding to is not even known. So 6068 * the code below allows us to change that binding at the time the 6069 * CPU is interrupted by virtue of incoming connection's squeue. 6070 * 6071 * This is usefull only in case of a listener bound to a specific IP 6072 * address. For other kind of listeners, they get bound the 6073 * very first time and there is no attempt to rebind them. 6074 */ 6075 void 6076 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6077 { 6078 conn_t *connp = (conn_t *)arg; 6079 squeue_t *sqp = (squeue_t *)arg2; 6080 squeue_t *new_sqp; 6081 uint32_t conn_flags; 6082 6083 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6084 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6085 } else { 6086 goto done; 6087 } 6088 6089 if (connp->conn_fanout == NULL) 6090 goto done; 6091 6092 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6093 mutex_enter(&connp->conn_fanout->connf_lock); 6094 mutex_enter(&connp->conn_lock); 6095 /* 6096 * No one from read or write side can access us now 6097 * except for already queued packets on this squeue. 6098 * But since we haven't changed the squeue yet, they 6099 * can't execute. If they are processed after we have 6100 * changed the squeue, they are sent back to the 6101 * correct squeue down below. 6102 * But a listner close can race with processing of 6103 * incoming SYN. If incoming SYN processing changes 6104 * the squeue then the listener close which is waiting 6105 * to enter the squeue would operate on the wrong 6106 * squeue. Hence we don't change the squeue here unless 6107 * the refcount is exactly the minimum refcount. The 6108 * minimum refcount of 4 is counted as - 1 each for 6109 * TCP and IP, 1 for being in the classifier hash, and 6110 * 1 for the mblk being processed. 6111 */ 6112 6113 if (connp->conn_ref != 4 || 6114 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6115 mutex_exit(&connp->conn_lock); 6116 mutex_exit(&connp->conn_fanout->connf_lock); 6117 goto done; 6118 } 6119 if (connp->conn_sqp != new_sqp) { 6120 while (connp->conn_sqp != new_sqp) 6121 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6122 } 6123 6124 do { 6125 conn_flags = connp->conn_flags; 6126 conn_flags |= IPCL_FULLY_BOUND; 6127 (void) cas32(&connp->conn_flags, connp->conn_flags, 6128 conn_flags); 6129 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6130 6131 mutex_exit(&connp->conn_fanout->connf_lock); 6132 mutex_exit(&connp->conn_lock); 6133 } 6134 6135 done: 6136 if (connp->conn_sqp != sqp) { 6137 CONN_INC_REF(connp); 6138 squeue_fill(connp->conn_sqp, mp, 6139 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6140 } else { 6141 tcp_conn_request(connp, mp, sqp); 6142 } 6143 } 6144 6145 /* 6146 * Successful connect request processing begins when our client passes 6147 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6148 * our T_OK_ACK reply message upstream. The control flow looks like this: 6149 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6150 * upstream <- tcp_rput() <- IP 6151 * After various error checks are completed, tcp_connect() lays 6152 * the target address and port into the composite header template, 6153 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6154 * request followed by an IRE request, and passes the three mblk message 6155 * down to IP looking like this: 6156 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6157 * Processing continues in tcp_rput() when we receive the following message: 6158 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6159 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6160 * to fire off the connection request, and then passes the T_OK_ACK mblk 6161 * upstream that we filled in below. There are, of course, numerous 6162 * error conditions along the way which truncate the processing described 6163 * above. 6164 */ 6165 static void 6166 tcp_connect(tcp_t *tcp, mblk_t *mp) 6167 { 6168 sin_t *sin; 6169 sin6_t *sin6; 6170 queue_t *q = tcp->tcp_wq; 6171 struct T_conn_req *tcr; 6172 ipaddr_t *dstaddrp; 6173 in_port_t dstport; 6174 uint_t srcid; 6175 6176 tcr = (struct T_conn_req *)mp->b_rptr; 6177 6178 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6179 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6180 tcp_err_ack(tcp, mp, TPROTO, 0); 6181 return; 6182 } 6183 6184 /* 6185 * Determine packet type based on type of address passed in 6186 * the request should contain an IPv4 or IPv6 address. 6187 * Make sure that address family matches the type of 6188 * family of the the address passed down 6189 */ 6190 switch (tcr->DEST_length) { 6191 default: 6192 tcp_err_ack(tcp, mp, TBADADDR, 0); 6193 return; 6194 6195 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6196 /* 6197 * XXX: The check for valid DEST_length was not there 6198 * in earlier releases and some buggy 6199 * TLI apps (e.g Sybase) got away with not feeding 6200 * in sin_zero part of address. 6201 * We allow that bug to keep those buggy apps humming. 6202 * Test suites require the check on DEST_length. 6203 * We construct a new mblk with valid DEST_length 6204 * free the original so the rest of the code does 6205 * not have to keep track of this special shorter 6206 * length address case. 6207 */ 6208 mblk_t *nmp; 6209 struct T_conn_req *ntcr; 6210 sin_t *nsin; 6211 6212 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6213 tcr->OPT_length, BPRI_HI); 6214 if (nmp == NULL) { 6215 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6216 return; 6217 } 6218 ntcr = (struct T_conn_req *)nmp->b_rptr; 6219 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6220 ntcr->PRIM_type = T_CONN_REQ; 6221 ntcr->DEST_length = sizeof (sin_t); 6222 ntcr->DEST_offset = sizeof (struct T_conn_req); 6223 6224 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6225 *nsin = sin_null; 6226 /* Get pointer to shorter address to copy from original mp */ 6227 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6228 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6229 if (sin == NULL || !OK_32PTR((char *)sin)) { 6230 freemsg(nmp); 6231 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6232 return; 6233 } 6234 nsin->sin_family = sin->sin_family; 6235 nsin->sin_port = sin->sin_port; 6236 nsin->sin_addr = sin->sin_addr; 6237 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6238 nmp->b_wptr = (uchar_t *)&nsin[1]; 6239 if (tcr->OPT_length != 0) { 6240 ntcr->OPT_length = tcr->OPT_length; 6241 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6242 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6243 (uchar_t *)ntcr + ntcr->OPT_offset, 6244 tcr->OPT_length); 6245 nmp->b_wptr += tcr->OPT_length; 6246 } 6247 freemsg(mp); /* original mp freed */ 6248 mp = nmp; /* re-initialize original variables */ 6249 tcr = ntcr; 6250 } 6251 /* FALLTHRU */ 6252 6253 case sizeof (sin_t): 6254 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6255 sizeof (sin_t)); 6256 if (sin == NULL || !OK_32PTR((char *)sin)) { 6257 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6258 return; 6259 } 6260 if (tcp->tcp_family != AF_INET || 6261 sin->sin_family != AF_INET) { 6262 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6263 return; 6264 } 6265 if (sin->sin_port == 0) { 6266 tcp_err_ack(tcp, mp, TBADADDR, 0); 6267 return; 6268 } 6269 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6270 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6271 return; 6272 } 6273 6274 break; 6275 6276 case sizeof (sin6_t): 6277 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6278 sizeof (sin6_t)); 6279 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6280 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6281 return; 6282 } 6283 if (tcp->tcp_family != AF_INET6 || 6284 sin6->sin6_family != AF_INET6) { 6285 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6286 return; 6287 } 6288 if (sin6->sin6_port == 0) { 6289 tcp_err_ack(tcp, mp, TBADADDR, 0); 6290 return; 6291 } 6292 break; 6293 } 6294 /* 6295 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6296 * should key on their sequence number and cut them loose. 6297 */ 6298 6299 /* 6300 * If options passed in, feed it for verification and handling 6301 */ 6302 if (tcr->OPT_length != 0) { 6303 mblk_t *ok_mp; 6304 mblk_t *discon_mp; 6305 mblk_t *conn_opts_mp; 6306 int t_error, sys_error, do_disconnect; 6307 6308 conn_opts_mp = NULL; 6309 6310 if (tcp_conprim_opt_process(tcp, mp, 6311 &do_disconnect, &t_error, &sys_error) < 0) { 6312 if (do_disconnect) { 6313 ASSERT(t_error == 0 && sys_error == 0); 6314 discon_mp = mi_tpi_discon_ind(NULL, 6315 ECONNREFUSED, 0); 6316 if (!discon_mp) { 6317 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6318 TSYSERR, ENOMEM); 6319 return; 6320 } 6321 ok_mp = mi_tpi_ok_ack_alloc(mp); 6322 if (!ok_mp) { 6323 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6324 TSYSERR, ENOMEM); 6325 return; 6326 } 6327 qreply(q, ok_mp); 6328 qreply(q, discon_mp); /* no flush! */ 6329 } else { 6330 ASSERT(t_error != 0); 6331 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6332 sys_error); 6333 } 6334 return; 6335 } 6336 /* 6337 * Success in setting options, the mp option buffer represented 6338 * by OPT_length/offset has been potentially modified and 6339 * contains results of option processing. We copy it in 6340 * another mp to save it for potentially influencing returning 6341 * it in T_CONN_CONN. 6342 */ 6343 if (tcr->OPT_length != 0) { /* there are resulting options */ 6344 conn_opts_mp = copyb(mp); 6345 if (!conn_opts_mp) { 6346 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6347 TSYSERR, ENOMEM); 6348 return; 6349 } 6350 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6351 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6352 /* 6353 * Note: 6354 * These resulting option negotiation can include any 6355 * end-to-end negotiation options but there no such 6356 * thing (yet?) in our TCP/IP. 6357 */ 6358 } 6359 } 6360 6361 /* 6362 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6363 * make sure that the template IP header in the tcp structure is an 6364 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6365 * need to this before we call tcp_bindi() so that the port lookup 6366 * code will look for ports in the correct port space (IPv4 and 6367 * IPv6 have separate port spaces). 6368 */ 6369 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6370 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6371 int err = 0; 6372 6373 err = tcp_header_init_ipv4(tcp); 6374 if (err != 0) { 6375 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6376 goto connect_failed; 6377 } 6378 if (tcp->tcp_lport != 0) 6379 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6380 } 6381 6382 switch (tcp->tcp_state) { 6383 case TCPS_IDLE: 6384 /* 6385 * We support quick connect, refer to comments in 6386 * tcp_connect_*() 6387 */ 6388 /* FALLTHRU */ 6389 case TCPS_BOUND: 6390 case TCPS_LISTEN: 6391 if (tcp->tcp_family == AF_INET6) { 6392 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6393 tcp_connect_ipv6(tcp, mp, 6394 &sin6->sin6_addr, 6395 sin6->sin6_port, sin6->sin6_flowinfo, 6396 sin6->__sin6_src_id, sin6->sin6_scope_id); 6397 return; 6398 } 6399 /* 6400 * Destination adress is mapped IPv6 address. 6401 * Source bound address should be unspecified or 6402 * IPv6 mapped address as well. 6403 */ 6404 if (!IN6_IS_ADDR_UNSPECIFIED( 6405 &tcp->tcp_bound_source_v6) && 6406 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6407 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6408 EADDRNOTAVAIL); 6409 break; 6410 } 6411 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6412 dstport = sin6->sin6_port; 6413 srcid = sin6->__sin6_src_id; 6414 } else { 6415 dstaddrp = &sin->sin_addr.s_addr; 6416 dstport = sin->sin_port; 6417 srcid = 0; 6418 } 6419 6420 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6421 return; 6422 default: 6423 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6424 break; 6425 } 6426 /* 6427 * Note: Code below is the "failure" case 6428 */ 6429 /* return error ack and blow away saved option results if any */ 6430 connect_failed: 6431 if (mp != NULL) 6432 putnext(tcp->tcp_rq, mp); 6433 else { 6434 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6435 TSYSERR, ENOMEM); 6436 } 6437 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6438 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6439 } 6440 6441 /* 6442 * Handle connect to IPv4 destinations, including connections for AF_INET6 6443 * sockets connecting to IPv4 mapped IPv6 destinations. 6444 */ 6445 static void 6446 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6447 uint_t srcid) 6448 { 6449 tcph_t *tcph; 6450 mblk_t *mp1; 6451 ipaddr_t dstaddr = *dstaddrp; 6452 int32_t oldstate; 6453 uint16_t lport; 6454 tcp_stack_t *tcps = tcp->tcp_tcps; 6455 6456 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6457 6458 /* Check for attempt to connect to INADDR_ANY */ 6459 if (dstaddr == INADDR_ANY) { 6460 /* 6461 * SunOS 4.x and 4.3 BSD allow an application 6462 * to connect a TCP socket to INADDR_ANY. 6463 * When they do this, the kernel picks the 6464 * address of one interface and uses it 6465 * instead. The kernel usually ends up 6466 * picking the address of the loopback 6467 * interface. This is an undocumented feature. 6468 * However, we provide the same thing here 6469 * in order to have source and binary 6470 * compatibility with SunOS 4.x. 6471 * Update the T_CONN_REQ (sin/sin6) since it is used to 6472 * generate the T_CONN_CON. 6473 */ 6474 dstaddr = htonl(INADDR_LOOPBACK); 6475 *dstaddrp = dstaddr; 6476 } 6477 6478 /* Handle __sin6_src_id if socket not bound to an IP address */ 6479 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6480 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6481 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6482 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6483 tcp->tcp_ipha->ipha_src); 6484 } 6485 6486 /* 6487 * Don't let an endpoint connect to itself. Note that 6488 * the test here does not catch the case where the 6489 * source IP addr was left unspecified by the user. In 6490 * this case, the source addr is set in tcp_adapt_ire() 6491 * using the reply to the T_BIND message that we send 6492 * down to IP here and the check is repeated in tcp_rput_other. 6493 */ 6494 if (dstaddr == tcp->tcp_ipha->ipha_src && 6495 dstport == tcp->tcp_lport) { 6496 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6497 goto failed; 6498 } 6499 6500 tcp->tcp_ipha->ipha_dst = dstaddr; 6501 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6502 6503 /* 6504 * Massage a source route if any putting the first hop 6505 * in iph_dst. Compute a starting value for the checksum which 6506 * takes into account that the original iph_dst should be 6507 * included in the checksum but that ip will include the 6508 * first hop in the source route in the tcp checksum. 6509 */ 6510 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6511 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6512 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6513 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6514 if ((int)tcp->tcp_sum < 0) 6515 tcp->tcp_sum--; 6516 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6517 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6518 (tcp->tcp_sum >> 16)); 6519 tcph = tcp->tcp_tcph; 6520 *(uint16_t *)tcph->th_fport = dstport; 6521 tcp->tcp_fport = dstport; 6522 6523 oldstate = tcp->tcp_state; 6524 /* 6525 * At this point the remote destination address and remote port fields 6526 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6527 * have to see which state tcp was in so we can take apropriate action. 6528 */ 6529 if (oldstate == TCPS_IDLE) { 6530 /* 6531 * We support a quick connect capability here, allowing 6532 * clients to transition directly from IDLE to SYN_SENT 6533 * tcp_bindi will pick an unused port, insert the connection 6534 * in the bind hash and transition to BOUND state. 6535 */ 6536 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6537 tcp, B_TRUE); 6538 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6539 B_FALSE, B_FALSE); 6540 if (lport == 0) { 6541 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6542 goto failed; 6543 } 6544 } 6545 tcp->tcp_state = TCPS_SYN_SENT; 6546 6547 /* 6548 * TODO: allow data with connect requests 6549 * by unlinking M_DATA trailers here and 6550 * linking them in behind the T_OK_ACK mblk. 6551 * The tcp_rput() bind ack handler would then 6552 * feed them to tcp_wput_data() rather than call 6553 * tcp_timer(). 6554 */ 6555 mp = mi_tpi_ok_ack_alloc(mp); 6556 if (!mp) { 6557 tcp->tcp_state = oldstate; 6558 goto failed; 6559 } 6560 if (tcp->tcp_family == AF_INET) { 6561 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6562 sizeof (ipa_conn_t)); 6563 } else { 6564 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6565 sizeof (ipa6_conn_t)); 6566 } 6567 if (mp1) { 6568 /* Hang onto the T_OK_ACK for later. */ 6569 linkb(mp1, mp); 6570 mblk_setcred(mp1, tcp->tcp_cred); 6571 if (tcp->tcp_family == AF_INET) 6572 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6573 else { 6574 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6575 &tcp->tcp_sticky_ipp); 6576 } 6577 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6578 tcp->tcp_active_open = 1; 6579 /* 6580 * If the bind cannot complete immediately 6581 * IP will arrange to call tcp_rput_other 6582 * when the bind completes. 6583 */ 6584 if (mp1 != NULL) 6585 tcp_rput_other(tcp, mp1); 6586 return; 6587 } 6588 /* Error case */ 6589 tcp->tcp_state = oldstate; 6590 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6591 6592 failed: 6593 /* return error ack and blow away saved option results if any */ 6594 if (mp != NULL) 6595 putnext(tcp->tcp_rq, mp); 6596 else { 6597 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6598 TSYSERR, ENOMEM); 6599 } 6600 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6601 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6602 6603 } 6604 6605 /* 6606 * Handle connect to IPv6 destinations. 6607 */ 6608 static void 6609 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6610 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6611 { 6612 tcph_t *tcph; 6613 mblk_t *mp1; 6614 ip6_rthdr_t *rth; 6615 int32_t oldstate; 6616 uint16_t lport; 6617 tcp_stack_t *tcps = tcp->tcp_tcps; 6618 6619 ASSERT(tcp->tcp_family == AF_INET6); 6620 6621 /* 6622 * If we're here, it means that the destination address is a native 6623 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6624 * reason why it might not be IPv6 is if the socket was bound to an 6625 * IPv4-mapped IPv6 address. 6626 */ 6627 if (tcp->tcp_ipversion != IPV6_VERSION) { 6628 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6629 goto failed; 6630 } 6631 6632 /* 6633 * Interpret a zero destination to mean loopback. 6634 * Update the T_CONN_REQ (sin/sin6) since it is used to 6635 * generate the T_CONN_CON. 6636 */ 6637 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6638 *dstaddrp = ipv6_loopback; 6639 } 6640 6641 /* Handle __sin6_src_id if socket not bound to an IP address */ 6642 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6643 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6644 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6645 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6646 } 6647 6648 /* 6649 * Take care of the scope_id now and add ip6i_t 6650 * if ip6i_t is not already allocated through TCP 6651 * sticky options. At this point tcp_ip6h does not 6652 * have dst info, thus use dstaddrp. 6653 */ 6654 if (scope_id != 0 && 6655 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6656 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6657 ip6i_t *ip6i; 6658 6659 ipp->ipp_ifindex = scope_id; 6660 ip6i = (ip6i_t *)tcp->tcp_iphc; 6661 6662 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6663 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6664 /* Already allocated */ 6665 ip6i->ip6i_flags |= IP6I_IFINDEX; 6666 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6667 ipp->ipp_fields |= IPPF_SCOPE_ID; 6668 } else { 6669 int reterr; 6670 6671 ipp->ipp_fields |= IPPF_SCOPE_ID; 6672 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6673 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6674 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6675 if (reterr != 0) 6676 goto failed; 6677 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6678 } 6679 } 6680 6681 /* 6682 * Don't let an endpoint connect to itself. Note that 6683 * the test here does not catch the case where the 6684 * source IP addr was left unspecified by the user. In 6685 * this case, the source addr is set in tcp_adapt_ire() 6686 * using the reply to the T_BIND message that we send 6687 * down to IP here and the check is repeated in tcp_rput_other. 6688 */ 6689 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6690 (dstport == tcp->tcp_lport)) { 6691 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6692 goto failed; 6693 } 6694 6695 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6696 tcp->tcp_remote_v6 = *dstaddrp; 6697 tcp->tcp_ip6h->ip6_vcf = 6698 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6699 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6700 6701 6702 /* 6703 * Massage a routing header (if present) putting the first hop 6704 * in ip6_dst. Compute a starting value for the checksum which 6705 * takes into account that the original ip6_dst should be 6706 * included in the checksum but that ip will include the 6707 * first hop in the source route in the tcp checksum. 6708 */ 6709 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6710 if (rth != NULL) { 6711 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6712 tcps->tcps_netstack); 6713 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6714 (tcp->tcp_sum >> 16)); 6715 } else { 6716 tcp->tcp_sum = 0; 6717 } 6718 6719 tcph = tcp->tcp_tcph; 6720 *(uint16_t *)tcph->th_fport = dstport; 6721 tcp->tcp_fport = dstport; 6722 6723 oldstate = tcp->tcp_state; 6724 /* 6725 * At this point the remote destination address and remote port fields 6726 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6727 * have to see which state tcp was in so we can take apropriate action. 6728 */ 6729 if (oldstate == TCPS_IDLE) { 6730 /* 6731 * We support a quick connect capability here, allowing 6732 * clients to transition directly from IDLE to SYN_SENT 6733 * tcp_bindi will pick an unused port, insert the connection 6734 * in the bind hash and transition to BOUND state. 6735 */ 6736 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6737 tcp, B_TRUE); 6738 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6739 B_FALSE, B_FALSE); 6740 if (lport == 0) { 6741 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6742 goto failed; 6743 } 6744 } 6745 tcp->tcp_state = TCPS_SYN_SENT; 6746 /* 6747 * TODO: allow data with connect requests 6748 * by unlinking M_DATA trailers here and 6749 * linking them in behind the T_OK_ACK mblk. 6750 * The tcp_rput() bind ack handler would then 6751 * feed them to tcp_wput_data() rather than call 6752 * tcp_timer(). 6753 */ 6754 mp = mi_tpi_ok_ack_alloc(mp); 6755 if (!mp) { 6756 tcp->tcp_state = oldstate; 6757 goto failed; 6758 } 6759 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6760 if (mp1) { 6761 /* Hang onto the T_OK_ACK for later. */ 6762 linkb(mp1, mp); 6763 mblk_setcred(mp1, tcp->tcp_cred); 6764 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6765 &tcp->tcp_sticky_ipp); 6766 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6767 tcp->tcp_active_open = 1; 6768 /* ip_bind_v6() may return ACK or ERROR */ 6769 if (mp1 != NULL) 6770 tcp_rput_other(tcp, mp1); 6771 return; 6772 } 6773 /* Error case */ 6774 tcp->tcp_state = oldstate; 6775 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6776 6777 failed: 6778 /* return error ack and blow away saved option results if any */ 6779 if (mp != NULL) 6780 putnext(tcp->tcp_rq, mp); 6781 else { 6782 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6783 TSYSERR, ENOMEM); 6784 } 6785 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6786 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6787 } 6788 6789 /* 6790 * We need a stream q for detached closing tcp connections 6791 * to use. Our client hereby indicates that this q is the 6792 * one to use. 6793 */ 6794 static void 6795 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6796 { 6797 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6798 queue_t *q = tcp->tcp_wq; 6799 tcp_stack_t *tcps = tcp->tcp_tcps; 6800 6801 #ifdef NS_DEBUG 6802 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6803 tcps->tcps_netstack->netstack_stackid); 6804 #endif 6805 mp->b_datap->db_type = M_IOCACK; 6806 iocp->ioc_count = 0; 6807 mutex_enter(&tcps->tcps_g_q_lock); 6808 if (tcps->tcps_g_q != NULL) { 6809 mutex_exit(&tcps->tcps_g_q_lock); 6810 iocp->ioc_error = EALREADY; 6811 } else { 6812 mblk_t *mp1; 6813 6814 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6815 if (mp1 == NULL) { 6816 mutex_exit(&tcps->tcps_g_q_lock); 6817 iocp->ioc_error = ENOMEM; 6818 } else { 6819 tcps->tcps_g_q = tcp->tcp_rq; 6820 mutex_exit(&tcps->tcps_g_q_lock); 6821 iocp->ioc_error = 0; 6822 iocp->ioc_rval = 0; 6823 /* 6824 * We are passing tcp_sticky_ipp as NULL 6825 * as it is not useful for tcp_default queue 6826 */ 6827 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6828 if (mp1 != NULL) 6829 tcp_rput_other(tcp, mp1); 6830 } 6831 } 6832 qreply(q, mp); 6833 } 6834 6835 /* 6836 * Our client hereby directs us to reject the connection request 6837 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6838 * of sending the appropriate RST, not an ICMP error. 6839 */ 6840 static void 6841 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6842 { 6843 tcp_t *ltcp = NULL; 6844 t_scalar_t seqnum; 6845 conn_t *connp; 6846 tcp_stack_t *tcps = tcp->tcp_tcps; 6847 6848 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6849 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6850 tcp_err_ack(tcp, mp, TPROTO, 0); 6851 return; 6852 } 6853 6854 /* 6855 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6856 * when the stream is in BOUND state. Do not send a reset, 6857 * since the destination IP address is not valid, and it can 6858 * be the initialized value of all zeros (broadcast address). 6859 * 6860 * If TCP has sent down a bind request to IP and has not 6861 * received the reply, reject the request. Otherwise, TCP 6862 * will be confused. 6863 */ 6864 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6865 if (tcp->tcp_debug) { 6866 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6867 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6868 } 6869 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6870 return; 6871 } 6872 6873 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6874 6875 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6876 6877 /* 6878 * According to TPI, for non-listeners, ignore seqnum 6879 * and disconnect. 6880 * Following interpretation of -1 seqnum is historical 6881 * and implied TPI ? (TPI only states that for T_CONN_IND, 6882 * a valid seqnum should not be -1). 6883 * 6884 * -1 means disconnect everything 6885 * regardless even on a listener. 6886 */ 6887 6888 int old_state = tcp->tcp_state; 6889 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6890 6891 /* 6892 * The connection can't be on the tcp_time_wait_head list 6893 * since it is not detached. 6894 */ 6895 ASSERT(tcp->tcp_time_wait_next == NULL); 6896 ASSERT(tcp->tcp_time_wait_prev == NULL); 6897 ASSERT(tcp->tcp_time_wait_expire == 0); 6898 ltcp = NULL; 6899 /* 6900 * If it used to be a listener, check to make sure no one else 6901 * has taken the port before switching back to LISTEN state. 6902 */ 6903 if (tcp->tcp_ipversion == IPV4_VERSION) { 6904 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6905 tcp->tcp_ipha->ipha_src, 6906 tcp->tcp_connp->conn_zoneid, ipst); 6907 if (connp != NULL) 6908 ltcp = connp->conn_tcp; 6909 } else { 6910 /* Allow tcp_bound_if listeners? */ 6911 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6912 &tcp->tcp_ip6h->ip6_src, 0, 6913 tcp->tcp_connp->conn_zoneid, ipst); 6914 if (connp != NULL) 6915 ltcp = connp->conn_tcp; 6916 } 6917 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6918 tcp->tcp_state = TCPS_LISTEN; 6919 } else if (old_state > TCPS_BOUND) { 6920 tcp->tcp_conn_req_max = 0; 6921 tcp->tcp_state = TCPS_BOUND; 6922 } 6923 if (ltcp != NULL) 6924 CONN_DEC_REF(ltcp->tcp_connp); 6925 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6926 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6927 } else if (old_state == TCPS_ESTABLISHED || 6928 old_state == TCPS_CLOSE_WAIT) { 6929 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6930 } 6931 6932 if (tcp->tcp_fused) 6933 tcp_unfuse(tcp); 6934 6935 mutex_enter(&tcp->tcp_eager_lock); 6936 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6937 (tcp->tcp_conn_req_cnt_q != 0)) { 6938 tcp_eager_cleanup(tcp, 0); 6939 } 6940 mutex_exit(&tcp->tcp_eager_lock); 6941 6942 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6943 tcp->tcp_rnxt, TH_RST | TH_ACK); 6944 6945 tcp_reinit(tcp); 6946 6947 if (old_state >= TCPS_ESTABLISHED) { 6948 /* Send M_FLUSH according to TPI */ 6949 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6950 } 6951 mp = mi_tpi_ok_ack_alloc(mp); 6952 if (mp) 6953 putnext(tcp->tcp_rq, mp); 6954 return; 6955 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6956 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6957 return; 6958 } 6959 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6960 /* Send M_FLUSH according to TPI */ 6961 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6962 } 6963 mp = mi_tpi_ok_ack_alloc(mp); 6964 if (mp) 6965 putnext(tcp->tcp_rq, mp); 6966 } 6967 6968 /* 6969 * Diagnostic routine used to return a string associated with the tcp state. 6970 * Note that if the caller does not supply a buffer, it will use an internal 6971 * static string. This means that if multiple threads call this function at 6972 * the same time, output can be corrupted... Note also that this function 6973 * does not check the size of the supplied buffer. The caller has to make 6974 * sure that it is big enough. 6975 */ 6976 static char * 6977 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6978 { 6979 char buf1[30]; 6980 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6981 char *buf; 6982 char *cp; 6983 in6_addr_t local, remote; 6984 char local_addrbuf[INET6_ADDRSTRLEN]; 6985 char remote_addrbuf[INET6_ADDRSTRLEN]; 6986 6987 if (sup_buf != NULL) 6988 buf = sup_buf; 6989 else 6990 buf = priv_buf; 6991 6992 if (tcp == NULL) 6993 return ("NULL_TCP"); 6994 switch (tcp->tcp_state) { 6995 case TCPS_CLOSED: 6996 cp = "TCP_CLOSED"; 6997 break; 6998 case TCPS_IDLE: 6999 cp = "TCP_IDLE"; 7000 break; 7001 case TCPS_BOUND: 7002 cp = "TCP_BOUND"; 7003 break; 7004 case TCPS_LISTEN: 7005 cp = "TCP_LISTEN"; 7006 break; 7007 case TCPS_SYN_SENT: 7008 cp = "TCP_SYN_SENT"; 7009 break; 7010 case TCPS_SYN_RCVD: 7011 cp = "TCP_SYN_RCVD"; 7012 break; 7013 case TCPS_ESTABLISHED: 7014 cp = "TCP_ESTABLISHED"; 7015 break; 7016 case TCPS_CLOSE_WAIT: 7017 cp = "TCP_CLOSE_WAIT"; 7018 break; 7019 case TCPS_FIN_WAIT_1: 7020 cp = "TCP_FIN_WAIT_1"; 7021 break; 7022 case TCPS_CLOSING: 7023 cp = "TCP_CLOSING"; 7024 break; 7025 case TCPS_LAST_ACK: 7026 cp = "TCP_LAST_ACK"; 7027 break; 7028 case TCPS_FIN_WAIT_2: 7029 cp = "TCP_FIN_WAIT_2"; 7030 break; 7031 case TCPS_TIME_WAIT: 7032 cp = "TCP_TIME_WAIT"; 7033 break; 7034 default: 7035 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7036 cp = buf1; 7037 break; 7038 } 7039 switch (format) { 7040 case DISP_ADDR_AND_PORT: 7041 if (tcp->tcp_ipversion == IPV4_VERSION) { 7042 /* 7043 * Note that we use the remote address in the tcp_b 7044 * structure. This means that it will print out 7045 * the real destination address, not the next hop's 7046 * address if source routing is used. 7047 */ 7048 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7049 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7050 7051 } else { 7052 local = tcp->tcp_ip_src_v6; 7053 remote = tcp->tcp_remote_v6; 7054 } 7055 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7056 sizeof (local_addrbuf)); 7057 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7058 sizeof (remote_addrbuf)); 7059 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7060 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7061 ntohs(tcp->tcp_fport), cp); 7062 break; 7063 case DISP_PORT_ONLY: 7064 default: 7065 (void) mi_sprintf(buf, "[%u, %u] %s", 7066 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7067 break; 7068 } 7069 7070 return (buf); 7071 } 7072 7073 /* 7074 * Called via squeue to get on to eager's perimeter to send a 7075 * TH_RST. The listener wants the eager to disappear either 7076 * by means of tcp_eager_blowoff() or tcp_eager_cleanup() 7077 * being called. 7078 */ 7079 /* ARGSUSED */ 7080 void 7081 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7082 { 7083 conn_t *econnp = (conn_t *)arg; 7084 tcp_t *eager = econnp->conn_tcp; 7085 tcp_t *listener = eager->tcp_listener; 7086 tcp_stack_t *tcps = eager->tcp_tcps; 7087 7088 /* 7089 * We could be called because listener is closing. Since 7090 * the eager is using listener's queue's, its not safe. 7091 * Better use the default queue just to send the TH_RST 7092 * out. 7093 */ 7094 ASSERT(tcps->tcps_g_q != NULL); 7095 eager->tcp_rq = tcps->tcps_g_q; 7096 eager->tcp_wq = WR(tcps->tcps_g_q); 7097 7098 if (eager->tcp_state > TCPS_LISTEN) { 7099 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7100 eager, eager->tcp_snxt, 0, TH_RST); 7101 } 7102 7103 /* We are here because listener wants this eager gone */ 7104 if (listener != NULL) { 7105 mutex_enter(&listener->tcp_eager_lock); 7106 tcp_eager_unlink(eager); 7107 if (eager->tcp_tconnind_started) { 7108 /* 7109 * The eager has sent a conn_ind up to the 7110 * listener but listener decides to close 7111 * instead. We need to drop the extra ref 7112 * placed on eager in tcp_rput_data() before 7113 * sending the conn_ind to listener. 7114 */ 7115 CONN_DEC_REF(econnp); 7116 } 7117 mutex_exit(&listener->tcp_eager_lock); 7118 CONN_DEC_REF(listener->tcp_connp); 7119 } 7120 7121 if (eager->tcp_state > TCPS_BOUND) 7122 tcp_close_detached(eager); 7123 } 7124 7125 /* 7126 * Reset any eager connection hanging off this listener marked 7127 * with 'seqnum' and then reclaim it's resources. 7128 */ 7129 static boolean_t 7130 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7131 { 7132 tcp_t *eager; 7133 mblk_t *mp; 7134 tcp_stack_t *tcps = listener->tcp_tcps; 7135 7136 TCP_STAT(tcps, tcp_eager_blowoff_calls); 7137 eager = listener; 7138 mutex_enter(&listener->tcp_eager_lock); 7139 do { 7140 eager = eager->tcp_eager_next_q; 7141 if (eager == NULL) { 7142 mutex_exit(&listener->tcp_eager_lock); 7143 return (B_FALSE); 7144 } 7145 } while (eager->tcp_conn_req_seqnum != seqnum); 7146 7147 if (eager->tcp_closemp_used > 0) { 7148 mutex_exit(&listener->tcp_eager_lock); 7149 return (B_TRUE); 7150 } 7151 eager->tcp_closemp_used = 1; 7152 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7153 CONN_INC_REF(eager->tcp_connp); 7154 mutex_exit(&listener->tcp_eager_lock); 7155 mp = &eager->tcp_closemp; 7156 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7157 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7158 return (B_TRUE); 7159 } 7160 7161 /* 7162 * Reset any eager connection hanging off this listener 7163 * and then reclaim it's resources. 7164 */ 7165 static void 7166 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7167 { 7168 tcp_t *eager; 7169 mblk_t *mp; 7170 tcp_stack_t *tcps = listener->tcp_tcps; 7171 7172 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7173 7174 if (!q0_only) { 7175 /* First cleanup q */ 7176 TCP_STAT(tcps, tcp_eager_blowoff_q); 7177 eager = listener->tcp_eager_next_q; 7178 while (eager != NULL) { 7179 if (eager->tcp_closemp_used == 0) { 7180 eager->tcp_closemp_used = 1; 7181 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7182 CONN_INC_REF(eager->tcp_connp); 7183 mp = &eager->tcp_closemp; 7184 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7185 tcp_eager_kill, eager->tcp_connp, 7186 SQTAG_TCP_EAGER_CLEANUP); 7187 } 7188 eager = eager->tcp_eager_next_q; 7189 } 7190 } 7191 /* Then cleanup q0 */ 7192 TCP_STAT(tcps, tcp_eager_blowoff_q0); 7193 eager = listener->tcp_eager_next_q0; 7194 while (eager != listener) { 7195 if (eager->tcp_closemp_used == 0) { 7196 eager->tcp_closemp_used = 1; 7197 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7198 CONN_INC_REF(eager->tcp_connp); 7199 mp = &eager->tcp_closemp; 7200 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7201 tcp_eager_kill, eager->tcp_connp, 7202 SQTAG_TCP_EAGER_CLEANUP_Q0); 7203 } 7204 eager = eager->tcp_eager_next_q0; 7205 } 7206 } 7207 7208 /* 7209 * If we are an eager connection hanging off a listener that hasn't 7210 * formally accepted the connection yet, get off his list and blow off 7211 * any data that we have accumulated. 7212 */ 7213 static void 7214 tcp_eager_unlink(tcp_t *tcp) 7215 { 7216 tcp_t *listener = tcp->tcp_listener; 7217 7218 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7219 ASSERT(listener != NULL); 7220 if (tcp->tcp_eager_next_q0 != NULL) { 7221 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7222 7223 /* Remove the eager tcp from q0 */ 7224 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7225 tcp->tcp_eager_prev_q0; 7226 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7227 tcp->tcp_eager_next_q0; 7228 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7229 listener->tcp_conn_req_cnt_q0--; 7230 7231 tcp->tcp_eager_next_q0 = NULL; 7232 tcp->tcp_eager_prev_q0 = NULL; 7233 7234 /* 7235 * Take the eager out, if it is in the list of droppable 7236 * eagers. 7237 */ 7238 MAKE_UNDROPPABLE(tcp); 7239 7240 if (tcp->tcp_syn_rcvd_timeout != 0) { 7241 /* we have timed out before */ 7242 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7243 listener->tcp_syn_rcvd_timeout--; 7244 } 7245 } else { 7246 tcp_t **tcpp = &listener->tcp_eager_next_q; 7247 tcp_t *prev = NULL; 7248 7249 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7250 if (tcpp[0] == tcp) { 7251 if (listener->tcp_eager_last_q == tcp) { 7252 /* 7253 * If we are unlinking the last 7254 * element on the list, adjust 7255 * tail pointer. Set tail pointer 7256 * to nil when list is empty. 7257 */ 7258 ASSERT(tcp->tcp_eager_next_q == NULL); 7259 if (listener->tcp_eager_last_q == 7260 listener->tcp_eager_next_q) { 7261 listener->tcp_eager_last_q = 7262 NULL; 7263 } else { 7264 /* 7265 * We won't get here if there 7266 * is only one eager in the 7267 * list. 7268 */ 7269 ASSERT(prev != NULL); 7270 listener->tcp_eager_last_q = 7271 prev; 7272 } 7273 } 7274 tcpp[0] = tcp->tcp_eager_next_q; 7275 tcp->tcp_eager_next_q = NULL; 7276 tcp->tcp_eager_last_q = NULL; 7277 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7278 listener->tcp_conn_req_cnt_q--; 7279 break; 7280 } 7281 prev = tcpp[0]; 7282 } 7283 } 7284 tcp->tcp_listener = NULL; 7285 } 7286 7287 /* Shorthand to generate and send TPI error acks to our client */ 7288 static void 7289 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7290 { 7291 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7292 putnext(tcp->tcp_rq, mp); 7293 } 7294 7295 /* Shorthand to generate and send TPI error acks to our client */ 7296 static void 7297 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7298 int t_error, int sys_error) 7299 { 7300 struct T_error_ack *teackp; 7301 7302 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7303 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7304 teackp = (struct T_error_ack *)mp->b_rptr; 7305 teackp->ERROR_prim = primitive; 7306 teackp->TLI_error = t_error; 7307 teackp->UNIX_error = sys_error; 7308 putnext(tcp->tcp_rq, mp); 7309 } 7310 } 7311 7312 /* 7313 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7314 * but instead the code relies on: 7315 * - the fact that the address of the array and its size never changes 7316 * - the atomic assignment of the elements of the array 7317 */ 7318 /* ARGSUSED */ 7319 static int 7320 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7321 { 7322 int i; 7323 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7324 7325 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7326 if (tcps->tcps_g_epriv_ports[i] != 0) 7327 (void) mi_mpprintf(mp, "%d ", 7328 tcps->tcps_g_epriv_ports[i]); 7329 } 7330 return (0); 7331 } 7332 7333 /* 7334 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7335 * threads from changing it at the same time. 7336 */ 7337 /* ARGSUSED */ 7338 static int 7339 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7340 cred_t *cr) 7341 { 7342 long new_value; 7343 int i; 7344 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7345 7346 /* 7347 * Fail the request if the new value does not lie within the 7348 * port number limits. 7349 */ 7350 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7351 new_value <= 0 || new_value >= 65536) { 7352 return (EINVAL); 7353 } 7354 7355 mutex_enter(&tcps->tcps_epriv_port_lock); 7356 /* Check if the value is already in the list */ 7357 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7358 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7359 mutex_exit(&tcps->tcps_epriv_port_lock); 7360 return (EEXIST); 7361 } 7362 } 7363 /* Find an empty slot */ 7364 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7365 if (tcps->tcps_g_epriv_ports[i] == 0) 7366 break; 7367 } 7368 if (i == tcps->tcps_g_num_epriv_ports) { 7369 mutex_exit(&tcps->tcps_epriv_port_lock); 7370 return (EOVERFLOW); 7371 } 7372 /* Set the new value */ 7373 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7374 mutex_exit(&tcps->tcps_epriv_port_lock); 7375 return (0); 7376 } 7377 7378 /* 7379 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7380 * threads from changing it at the same time. 7381 */ 7382 /* ARGSUSED */ 7383 static int 7384 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7385 cred_t *cr) 7386 { 7387 long new_value; 7388 int i; 7389 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7390 7391 /* 7392 * Fail the request if the new value does not lie within the 7393 * port number limits. 7394 */ 7395 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7396 new_value >= 65536) { 7397 return (EINVAL); 7398 } 7399 7400 mutex_enter(&tcps->tcps_epriv_port_lock); 7401 /* Check that the value is already in the list */ 7402 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7403 if (tcps->tcps_g_epriv_ports[i] == new_value) 7404 break; 7405 } 7406 if (i == tcps->tcps_g_num_epriv_ports) { 7407 mutex_exit(&tcps->tcps_epriv_port_lock); 7408 return (ESRCH); 7409 } 7410 /* Clear the value */ 7411 tcps->tcps_g_epriv_ports[i] = 0; 7412 mutex_exit(&tcps->tcps_epriv_port_lock); 7413 return (0); 7414 } 7415 7416 /* Return the TPI/TLI equivalent of our current tcp_state */ 7417 static int 7418 tcp_tpistate(tcp_t *tcp) 7419 { 7420 switch (tcp->tcp_state) { 7421 case TCPS_IDLE: 7422 return (TS_UNBND); 7423 case TCPS_LISTEN: 7424 /* 7425 * Return whether there are outstanding T_CONN_IND waiting 7426 * for the matching T_CONN_RES. Therefore don't count q0. 7427 */ 7428 if (tcp->tcp_conn_req_cnt_q > 0) 7429 return (TS_WRES_CIND); 7430 else 7431 return (TS_IDLE); 7432 case TCPS_BOUND: 7433 return (TS_IDLE); 7434 case TCPS_SYN_SENT: 7435 return (TS_WCON_CREQ); 7436 case TCPS_SYN_RCVD: 7437 /* 7438 * Note: assumption: this has to the active open SYN_RCVD. 7439 * The passive instance is detached in SYN_RCVD stage of 7440 * incoming connection processing so we cannot get request 7441 * for T_info_ack on it. 7442 */ 7443 return (TS_WACK_CRES); 7444 case TCPS_ESTABLISHED: 7445 return (TS_DATA_XFER); 7446 case TCPS_CLOSE_WAIT: 7447 return (TS_WREQ_ORDREL); 7448 case TCPS_FIN_WAIT_1: 7449 return (TS_WIND_ORDREL); 7450 case TCPS_FIN_WAIT_2: 7451 return (TS_WIND_ORDREL); 7452 7453 case TCPS_CLOSING: 7454 case TCPS_LAST_ACK: 7455 case TCPS_TIME_WAIT: 7456 case TCPS_CLOSED: 7457 /* 7458 * Following TS_WACK_DREQ7 is a rendition of "not 7459 * yet TS_IDLE" TPI state. There is no best match to any 7460 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7461 * choose a value chosen that will map to TLI/XTI level 7462 * state of TSTATECHNG (state is process of changing) which 7463 * captures what this dummy state represents. 7464 */ 7465 return (TS_WACK_DREQ7); 7466 default: 7467 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7468 tcp->tcp_state, tcp_display(tcp, NULL, 7469 DISP_PORT_ONLY)); 7470 return (TS_UNBND); 7471 } 7472 } 7473 7474 static void 7475 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7476 { 7477 tcp_stack_t *tcps = tcp->tcp_tcps; 7478 7479 if (tcp->tcp_family == AF_INET6) 7480 *tia = tcp_g_t_info_ack_v6; 7481 else 7482 *tia = tcp_g_t_info_ack; 7483 tia->CURRENT_state = tcp_tpistate(tcp); 7484 tia->OPT_size = tcp_max_optsize; 7485 if (tcp->tcp_mss == 0) { 7486 /* Not yet set - tcp_open does not set mss */ 7487 if (tcp->tcp_ipversion == IPV4_VERSION) 7488 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7489 else 7490 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7491 } else { 7492 tia->TIDU_size = tcp->tcp_mss; 7493 } 7494 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7495 } 7496 7497 /* 7498 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7499 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7500 * tcp_g_t_info_ack. The current state of the stream is copied from 7501 * tcp_state. 7502 */ 7503 static void 7504 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7505 { 7506 t_uscalar_t cap_bits1; 7507 struct T_capability_ack *tcap; 7508 7509 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7510 freemsg(mp); 7511 return; 7512 } 7513 7514 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7515 7516 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7517 mp->b_datap->db_type, T_CAPABILITY_ACK); 7518 if (mp == NULL) 7519 return; 7520 7521 tcap = (struct T_capability_ack *)mp->b_rptr; 7522 tcap->CAP_bits1 = 0; 7523 7524 if (cap_bits1 & TC1_INFO) { 7525 tcp_copy_info(&tcap->INFO_ack, tcp); 7526 tcap->CAP_bits1 |= TC1_INFO; 7527 } 7528 7529 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7530 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7531 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7532 } 7533 7534 putnext(tcp->tcp_rq, mp); 7535 } 7536 7537 /* 7538 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7539 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7540 * The current state of the stream is copied from tcp_state. 7541 */ 7542 static void 7543 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7544 { 7545 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7546 T_INFO_ACK); 7547 if (!mp) { 7548 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7549 return; 7550 } 7551 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7552 putnext(tcp->tcp_rq, mp); 7553 } 7554 7555 /* Respond to the TPI addr request */ 7556 static void 7557 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7558 { 7559 sin_t *sin; 7560 mblk_t *ackmp; 7561 struct T_addr_ack *taa; 7562 7563 /* Make it large enough for worst case */ 7564 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7565 2 * sizeof (sin6_t), 1); 7566 if (ackmp == NULL) { 7567 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7568 return; 7569 } 7570 7571 if (tcp->tcp_ipversion == IPV6_VERSION) { 7572 tcp_addr_req_ipv6(tcp, ackmp); 7573 return; 7574 } 7575 taa = (struct T_addr_ack *)ackmp->b_rptr; 7576 7577 bzero(taa, sizeof (struct T_addr_ack)); 7578 ackmp->b_wptr = (uchar_t *)&taa[1]; 7579 7580 taa->PRIM_type = T_ADDR_ACK; 7581 ackmp->b_datap->db_type = M_PCPROTO; 7582 7583 /* 7584 * Note: Following code assumes 32 bit alignment of basic 7585 * data structures like sin_t and struct T_addr_ack. 7586 */ 7587 if (tcp->tcp_state >= TCPS_BOUND) { 7588 /* 7589 * Fill in local address 7590 */ 7591 taa->LOCADDR_length = sizeof (sin_t); 7592 taa->LOCADDR_offset = sizeof (*taa); 7593 7594 sin = (sin_t *)&taa[1]; 7595 7596 /* Fill zeroes and then intialize non-zero fields */ 7597 *sin = sin_null; 7598 7599 sin->sin_family = AF_INET; 7600 7601 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7602 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7603 7604 ackmp->b_wptr = (uchar_t *)&sin[1]; 7605 7606 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7607 /* 7608 * Fill in Remote address 7609 */ 7610 taa->REMADDR_length = sizeof (sin_t); 7611 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7612 taa->LOCADDR_length); 7613 7614 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7615 *sin = sin_null; 7616 sin->sin_family = AF_INET; 7617 sin->sin_addr.s_addr = tcp->tcp_remote; 7618 sin->sin_port = tcp->tcp_fport; 7619 7620 ackmp->b_wptr = (uchar_t *)&sin[1]; 7621 } 7622 } 7623 putnext(tcp->tcp_rq, ackmp); 7624 } 7625 7626 /* Assumes that tcp_addr_req gets enough space and alignment */ 7627 static void 7628 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7629 { 7630 sin6_t *sin6; 7631 struct T_addr_ack *taa; 7632 7633 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7634 ASSERT(OK_32PTR(ackmp->b_rptr)); 7635 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7636 2 * sizeof (sin6_t)); 7637 7638 taa = (struct T_addr_ack *)ackmp->b_rptr; 7639 7640 bzero(taa, sizeof (struct T_addr_ack)); 7641 ackmp->b_wptr = (uchar_t *)&taa[1]; 7642 7643 taa->PRIM_type = T_ADDR_ACK; 7644 ackmp->b_datap->db_type = M_PCPROTO; 7645 7646 /* 7647 * Note: Following code assumes 32 bit alignment of basic 7648 * data structures like sin6_t and struct T_addr_ack. 7649 */ 7650 if (tcp->tcp_state >= TCPS_BOUND) { 7651 /* 7652 * Fill in local address 7653 */ 7654 taa->LOCADDR_length = sizeof (sin6_t); 7655 taa->LOCADDR_offset = sizeof (*taa); 7656 7657 sin6 = (sin6_t *)&taa[1]; 7658 *sin6 = sin6_null; 7659 7660 sin6->sin6_family = AF_INET6; 7661 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7662 sin6->sin6_port = tcp->tcp_lport; 7663 7664 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7665 7666 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7667 /* 7668 * Fill in Remote address 7669 */ 7670 taa->REMADDR_length = sizeof (sin6_t); 7671 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7672 taa->LOCADDR_length); 7673 7674 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7675 *sin6 = sin6_null; 7676 sin6->sin6_family = AF_INET6; 7677 sin6->sin6_flowinfo = 7678 tcp->tcp_ip6h->ip6_vcf & 7679 ~IPV6_VERS_AND_FLOW_MASK; 7680 sin6->sin6_addr = tcp->tcp_remote_v6; 7681 sin6->sin6_port = tcp->tcp_fport; 7682 7683 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7684 } 7685 } 7686 putnext(tcp->tcp_rq, ackmp); 7687 } 7688 7689 /* 7690 * Handle reinitialization of a tcp structure. 7691 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7692 */ 7693 static void 7694 tcp_reinit(tcp_t *tcp) 7695 { 7696 mblk_t *mp; 7697 int err; 7698 tcp_stack_t *tcps = tcp->tcp_tcps; 7699 7700 TCP_STAT(tcps, tcp_reinit_calls); 7701 7702 /* tcp_reinit should never be called for detached tcp_t's */ 7703 ASSERT(tcp->tcp_listener == NULL); 7704 ASSERT((tcp->tcp_family == AF_INET && 7705 tcp->tcp_ipversion == IPV4_VERSION) || 7706 (tcp->tcp_family == AF_INET6 && 7707 (tcp->tcp_ipversion == IPV4_VERSION || 7708 tcp->tcp_ipversion == IPV6_VERSION))); 7709 7710 /* Cancel outstanding timers */ 7711 tcp_timers_stop(tcp); 7712 7713 /* 7714 * Reset everything in the state vector, after updating global 7715 * MIB data from instance counters. 7716 */ 7717 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7718 tcp->tcp_ibsegs = 0; 7719 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7720 tcp->tcp_obsegs = 0; 7721 7722 tcp_close_mpp(&tcp->tcp_xmit_head); 7723 if (tcp->tcp_snd_zcopy_aware) 7724 tcp_zcopy_notify(tcp); 7725 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7726 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7727 mutex_enter(&tcp->tcp_non_sq_lock); 7728 if (tcp->tcp_flow_stopped && 7729 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7730 tcp_clrqfull(tcp); 7731 } 7732 mutex_exit(&tcp->tcp_non_sq_lock); 7733 tcp_close_mpp(&tcp->tcp_reass_head); 7734 tcp->tcp_reass_tail = NULL; 7735 if (tcp->tcp_rcv_list != NULL) { 7736 /* Free b_next chain */ 7737 tcp_close_mpp(&tcp->tcp_rcv_list); 7738 tcp->tcp_rcv_last_head = NULL; 7739 tcp->tcp_rcv_last_tail = NULL; 7740 tcp->tcp_rcv_cnt = 0; 7741 } 7742 tcp->tcp_rcv_last_tail = NULL; 7743 7744 if ((mp = tcp->tcp_urp_mp) != NULL) { 7745 freemsg(mp); 7746 tcp->tcp_urp_mp = NULL; 7747 } 7748 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7749 freemsg(mp); 7750 tcp->tcp_urp_mark_mp = NULL; 7751 } 7752 if (tcp->tcp_fused_sigurg_mp != NULL) { 7753 freeb(tcp->tcp_fused_sigurg_mp); 7754 tcp->tcp_fused_sigurg_mp = NULL; 7755 } 7756 7757 /* 7758 * Following is a union with two members which are 7759 * identical types and size so the following cleanup 7760 * is enough. 7761 */ 7762 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7763 7764 CL_INET_DISCONNECT(tcp); 7765 7766 /* 7767 * The connection can't be on the tcp_time_wait_head list 7768 * since it is not detached. 7769 */ 7770 ASSERT(tcp->tcp_time_wait_next == NULL); 7771 ASSERT(tcp->tcp_time_wait_prev == NULL); 7772 ASSERT(tcp->tcp_time_wait_expire == 0); 7773 7774 if (tcp->tcp_kssl_pending) { 7775 tcp->tcp_kssl_pending = B_FALSE; 7776 7777 /* Don't reset if the initialized by bind. */ 7778 if (tcp->tcp_kssl_ent != NULL) { 7779 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7780 KSSL_NO_PROXY); 7781 } 7782 } 7783 if (tcp->tcp_kssl_ctx != NULL) { 7784 kssl_release_ctx(tcp->tcp_kssl_ctx); 7785 tcp->tcp_kssl_ctx = NULL; 7786 } 7787 7788 /* 7789 * Reset/preserve other values 7790 */ 7791 tcp_reinit_values(tcp); 7792 ipcl_hash_remove(tcp->tcp_connp); 7793 conn_delete_ire(tcp->tcp_connp, NULL); 7794 tcp_ipsec_cleanup(tcp); 7795 7796 if (tcp->tcp_conn_req_max != 0) { 7797 /* 7798 * This is the case when a TLI program uses the same 7799 * transport end point to accept a connection. This 7800 * makes the TCP both a listener and acceptor. When 7801 * this connection is closed, we need to set the state 7802 * back to TCPS_LISTEN. Make sure that the eager list 7803 * is reinitialized. 7804 * 7805 * Note that this stream is still bound to the four 7806 * tuples of the previous connection in IP. If a new 7807 * SYN with different foreign address comes in, IP will 7808 * not find it and will send it to the global queue. In 7809 * the global queue, TCP will do a tcp_lookup_listener() 7810 * to find this stream. This works because this stream 7811 * is only removed from connected hash. 7812 * 7813 */ 7814 tcp->tcp_state = TCPS_LISTEN; 7815 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7816 tcp->tcp_eager_next_drop_q0 = tcp; 7817 tcp->tcp_eager_prev_drop_q0 = tcp; 7818 tcp->tcp_connp->conn_recv = tcp_conn_request; 7819 if (tcp->tcp_family == AF_INET6) { 7820 ASSERT(tcp->tcp_connp->conn_af_isv6); 7821 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7822 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7823 } else { 7824 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7825 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7826 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7827 } 7828 } else { 7829 tcp->tcp_state = TCPS_BOUND; 7830 } 7831 7832 /* 7833 * Initialize to default values 7834 * Can't fail since enough header template space already allocated 7835 * at open(). 7836 */ 7837 err = tcp_init_values(tcp); 7838 ASSERT(err == 0); 7839 /* Restore state in tcp_tcph */ 7840 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7841 if (tcp->tcp_ipversion == IPV4_VERSION) 7842 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7843 else 7844 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7845 /* 7846 * Copy of the src addr. in tcp_t is needed in tcp_t 7847 * since the lookup funcs can only lookup on tcp_t 7848 */ 7849 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7850 7851 ASSERT(tcp->tcp_ptpbhn != NULL); 7852 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7853 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7854 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7855 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7856 } 7857 7858 /* 7859 * Force values to zero that need be zero. 7860 * Do not touch values asociated with the BOUND or LISTEN state 7861 * since the connection will end up in that state after the reinit. 7862 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7863 * structure! 7864 */ 7865 static void 7866 tcp_reinit_values(tcp) 7867 tcp_t *tcp; 7868 { 7869 tcp_stack_t *tcps = tcp->tcp_tcps; 7870 7871 #ifndef lint 7872 #define DONTCARE(x) 7873 #define PRESERVE(x) 7874 #else 7875 #define DONTCARE(x) ((x) = (x)) 7876 #define PRESERVE(x) ((x) = (x)) 7877 #endif /* lint */ 7878 7879 PRESERVE(tcp->tcp_bind_hash); 7880 PRESERVE(tcp->tcp_ptpbhn); 7881 PRESERVE(tcp->tcp_acceptor_hash); 7882 PRESERVE(tcp->tcp_ptpahn); 7883 7884 /* Should be ASSERT NULL on these with new code! */ 7885 ASSERT(tcp->tcp_time_wait_next == NULL); 7886 ASSERT(tcp->tcp_time_wait_prev == NULL); 7887 ASSERT(tcp->tcp_time_wait_expire == 0); 7888 PRESERVE(tcp->tcp_state); 7889 PRESERVE(tcp->tcp_rq); 7890 PRESERVE(tcp->tcp_wq); 7891 7892 ASSERT(tcp->tcp_xmit_head == NULL); 7893 ASSERT(tcp->tcp_xmit_last == NULL); 7894 ASSERT(tcp->tcp_unsent == 0); 7895 ASSERT(tcp->tcp_xmit_tail == NULL); 7896 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7897 7898 tcp->tcp_snxt = 0; /* Displayed in mib */ 7899 tcp->tcp_suna = 0; /* Displayed in mib */ 7900 tcp->tcp_swnd = 0; 7901 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7902 7903 ASSERT(tcp->tcp_ibsegs == 0); 7904 ASSERT(tcp->tcp_obsegs == 0); 7905 7906 if (tcp->tcp_iphc != NULL) { 7907 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7908 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7909 } 7910 7911 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7912 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7913 DONTCARE(tcp->tcp_ipha); 7914 DONTCARE(tcp->tcp_ip6h); 7915 DONTCARE(tcp->tcp_ip_hdr_len); 7916 DONTCARE(tcp->tcp_tcph); 7917 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7918 tcp->tcp_valid_bits = 0; 7919 7920 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7921 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7922 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7923 tcp->tcp_last_rcv_lbolt = 0; 7924 7925 tcp->tcp_init_cwnd = 0; 7926 7927 tcp->tcp_urp_last_valid = 0; 7928 tcp->tcp_hard_binding = 0; 7929 tcp->tcp_hard_bound = 0; 7930 PRESERVE(tcp->tcp_cred); 7931 PRESERVE(tcp->tcp_cpid); 7932 PRESERVE(tcp->tcp_open_time); 7933 PRESERVE(tcp->tcp_exclbind); 7934 7935 tcp->tcp_fin_acked = 0; 7936 tcp->tcp_fin_rcvd = 0; 7937 tcp->tcp_fin_sent = 0; 7938 tcp->tcp_ordrel_done = 0; 7939 7940 tcp->tcp_debug = 0; 7941 tcp->tcp_dontroute = 0; 7942 tcp->tcp_broadcast = 0; 7943 7944 tcp->tcp_useloopback = 0; 7945 tcp->tcp_reuseaddr = 0; 7946 tcp->tcp_oobinline = 0; 7947 tcp->tcp_dgram_errind = 0; 7948 7949 tcp->tcp_detached = 0; 7950 tcp->tcp_bind_pending = 0; 7951 tcp->tcp_unbind_pending = 0; 7952 tcp->tcp_deferred_clean_death = 0; 7953 7954 tcp->tcp_snd_ws_ok = B_FALSE; 7955 tcp->tcp_snd_ts_ok = B_FALSE; 7956 tcp->tcp_linger = 0; 7957 tcp->tcp_ka_enabled = 0; 7958 tcp->tcp_zero_win_probe = 0; 7959 7960 tcp->tcp_loopback = 0; 7961 tcp->tcp_localnet = 0; 7962 tcp->tcp_syn_defense = 0; 7963 tcp->tcp_set_timer = 0; 7964 7965 tcp->tcp_active_open = 0; 7966 ASSERT(tcp->tcp_timeout == B_FALSE); 7967 tcp->tcp_rexmit = B_FALSE; 7968 tcp->tcp_xmit_zc_clean = B_FALSE; 7969 7970 tcp->tcp_snd_sack_ok = B_FALSE; 7971 PRESERVE(tcp->tcp_recvdstaddr); 7972 tcp->tcp_hwcksum = B_FALSE; 7973 7974 tcp->tcp_ire_ill_check_done = B_FALSE; 7975 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7976 7977 tcp->tcp_mdt = B_FALSE; 7978 tcp->tcp_mdt_hdr_head = 0; 7979 tcp->tcp_mdt_hdr_tail = 0; 7980 7981 tcp->tcp_conn_def_q0 = 0; 7982 tcp->tcp_ip_forward_progress = B_FALSE; 7983 tcp->tcp_anon_priv_bind = 0; 7984 tcp->tcp_ecn_ok = B_FALSE; 7985 7986 tcp->tcp_cwr = B_FALSE; 7987 tcp->tcp_ecn_echo_on = B_FALSE; 7988 7989 if (tcp->tcp_sack_info != NULL) { 7990 if (tcp->tcp_notsack_list != NULL) { 7991 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7992 } 7993 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7994 tcp->tcp_sack_info = NULL; 7995 } 7996 7997 tcp->tcp_rcv_ws = 0; 7998 tcp->tcp_snd_ws = 0; 7999 tcp->tcp_ts_recent = 0; 8000 tcp->tcp_rnxt = 0; /* Displayed in mib */ 8001 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 8002 tcp->tcp_if_mtu = 0; 8003 8004 ASSERT(tcp->tcp_reass_head == NULL); 8005 ASSERT(tcp->tcp_reass_tail == NULL); 8006 8007 tcp->tcp_cwnd_cnt = 0; 8008 8009 ASSERT(tcp->tcp_rcv_list == NULL); 8010 ASSERT(tcp->tcp_rcv_last_head == NULL); 8011 ASSERT(tcp->tcp_rcv_last_tail == NULL); 8012 ASSERT(tcp->tcp_rcv_cnt == 0); 8013 8014 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8015 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8016 tcp->tcp_csuna = 0; 8017 8018 tcp->tcp_rto = 0; /* Displayed in MIB */ 8019 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8020 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8021 tcp->tcp_rtt_update = 0; 8022 8023 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8024 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8025 8026 tcp->tcp_rack = 0; /* Displayed in mib */ 8027 tcp->tcp_rack_cnt = 0; 8028 tcp->tcp_rack_cur_max = 0; 8029 tcp->tcp_rack_abs_max = 0; 8030 8031 tcp->tcp_max_swnd = 0; 8032 8033 ASSERT(tcp->tcp_listener == NULL); 8034 8035 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8036 8037 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8038 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8039 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8040 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8041 8042 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8043 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8044 PRESERVE(tcp->tcp_conn_req_max); 8045 PRESERVE(tcp->tcp_conn_req_seqnum); 8046 8047 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8048 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8049 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8050 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8051 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8052 8053 tcp->tcp_lingertime = 0; 8054 8055 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8056 ASSERT(tcp->tcp_urp_mp == NULL); 8057 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8058 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8059 8060 ASSERT(tcp->tcp_eager_next_q == NULL); 8061 ASSERT(tcp->tcp_eager_last_q == NULL); 8062 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8063 tcp->tcp_eager_prev_q0 == NULL) || 8064 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8065 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8066 8067 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8068 tcp->tcp_eager_prev_drop_q0 == NULL) || 8069 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8070 8071 tcp->tcp_client_errno = 0; 8072 8073 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8074 8075 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8076 8077 PRESERVE(tcp->tcp_bound_source_v6); 8078 tcp->tcp_last_sent_len = 0; 8079 tcp->tcp_dupack_cnt = 0; 8080 8081 tcp->tcp_fport = 0; /* Displayed in MIB */ 8082 PRESERVE(tcp->tcp_lport); 8083 8084 PRESERVE(tcp->tcp_acceptor_lockp); 8085 8086 ASSERT(tcp->tcp_ordrelid == 0); 8087 PRESERVE(tcp->tcp_acceptor_id); 8088 DONTCARE(tcp->tcp_ipsec_overhead); 8089 8090 /* 8091 * If tcp_tracing flag is ON (i.e. We have a trace buffer 8092 * in tcp structure and now tracing), Re-initialize all 8093 * members of tcp_traceinfo. 8094 */ 8095 if (tcp->tcp_tracebuf != NULL) { 8096 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 8097 } 8098 8099 PRESERVE(tcp->tcp_family); 8100 if (tcp->tcp_family == AF_INET6) { 8101 tcp->tcp_ipversion = IPV6_VERSION; 8102 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8103 } else { 8104 tcp->tcp_ipversion = IPV4_VERSION; 8105 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8106 } 8107 8108 tcp->tcp_bound_if = 0; 8109 tcp->tcp_ipv6_recvancillary = 0; 8110 tcp->tcp_recvifindex = 0; 8111 tcp->tcp_recvhops = 0; 8112 tcp->tcp_closed = 0; 8113 tcp->tcp_cleandeathtag = 0; 8114 if (tcp->tcp_hopopts != NULL) { 8115 mi_free(tcp->tcp_hopopts); 8116 tcp->tcp_hopopts = NULL; 8117 tcp->tcp_hopoptslen = 0; 8118 } 8119 ASSERT(tcp->tcp_hopoptslen == 0); 8120 if (tcp->tcp_dstopts != NULL) { 8121 mi_free(tcp->tcp_dstopts); 8122 tcp->tcp_dstopts = NULL; 8123 tcp->tcp_dstoptslen = 0; 8124 } 8125 ASSERT(tcp->tcp_dstoptslen == 0); 8126 if (tcp->tcp_rtdstopts != NULL) { 8127 mi_free(tcp->tcp_rtdstopts); 8128 tcp->tcp_rtdstopts = NULL; 8129 tcp->tcp_rtdstoptslen = 0; 8130 } 8131 ASSERT(tcp->tcp_rtdstoptslen == 0); 8132 if (tcp->tcp_rthdr != NULL) { 8133 mi_free(tcp->tcp_rthdr); 8134 tcp->tcp_rthdr = NULL; 8135 tcp->tcp_rthdrlen = 0; 8136 } 8137 ASSERT(tcp->tcp_rthdrlen == 0); 8138 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8139 8140 /* Reset fusion-related fields */ 8141 tcp->tcp_fused = B_FALSE; 8142 tcp->tcp_unfusable = B_FALSE; 8143 tcp->tcp_fused_sigurg = B_FALSE; 8144 tcp->tcp_direct_sockfs = B_FALSE; 8145 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8146 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8147 tcp->tcp_loopback_peer = NULL; 8148 tcp->tcp_fuse_rcv_hiwater = 0; 8149 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8150 tcp->tcp_fuse_rcv_unread_cnt = 0; 8151 8152 tcp->tcp_lso = B_FALSE; 8153 8154 tcp->tcp_in_ack_unsent = 0; 8155 tcp->tcp_cork = B_FALSE; 8156 tcp->tcp_tconnind_started = B_FALSE; 8157 8158 PRESERVE(tcp->tcp_squeue_bytes); 8159 8160 ASSERT(tcp->tcp_kssl_ctx == NULL); 8161 ASSERT(!tcp->tcp_kssl_pending); 8162 PRESERVE(tcp->tcp_kssl_ent); 8163 8164 tcp->tcp_closemp_used = 0; 8165 8166 #ifdef DEBUG 8167 DONTCARE(tcp->tcmp_stk[0]); 8168 #endif 8169 8170 8171 #undef DONTCARE 8172 #undef PRESERVE 8173 } 8174 8175 /* 8176 * Allocate necessary resources and initialize state vector. 8177 * Guaranteed not to fail so that when an error is returned, 8178 * the caller doesn't need to do any additional cleanup. 8179 */ 8180 int 8181 tcp_init(tcp_t *tcp, queue_t *q) 8182 { 8183 int err; 8184 8185 tcp->tcp_rq = q; 8186 tcp->tcp_wq = WR(q); 8187 tcp->tcp_state = TCPS_IDLE; 8188 if ((err = tcp_init_values(tcp)) != 0) 8189 tcp_timers_stop(tcp); 8190 return (err); 8191 } 8192 8193 static int 8194 tcp_init_values(tcp_t *tcp) 8195 { 8196 int err; 8197 tcp_stack_t *tcps = tcp->tcp_tcps; 8198 8199 ASSERT((tcp->tcp_family == AF_INET && 8200 tcp->tcp_ipversion == IPV4_VERSION) || 8201 (tcp->tcp_family == AF_INET6 && 8202 (tcp->tcp_ipversion == IPV4_VERSION || 8203 tcp->tcp_ipversion == IPV6_VERSION))); 8204 8205 /* 8206 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8207 * will be close to tcp_rexmit_interval_initial. By doing this, we 8208 * allow the algorithm to adjust slowly to large fluctuations of RTT 8209 * during first few transmissions of a connection as seen in slow 8210 * links. 8211 */ 8212 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8213 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8214 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8215 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8216 tcps->tcps_conn_grace_period; 8217 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8218 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8219 tcp->tcp_timer_backoff = 0; 8220 tcp->tcp_ms_we_have_waited = 0; 8221 tcp->tcp_last_recv_time = lbolt; 8222 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8223 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8224 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8225 8226 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8227 8228 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8229 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8230 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8231 /* 8232 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8233 * passive open. 8234 */ 8235 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8236 8237 tcp->tcp_naglim = tcps->tcps_naglim_def; 8238 8239 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8240 8241 tcp->tcp_mdt_hdr_head = 0; 8242 tcp->tcp_mdt_hdr_tail = 0; 8243 8244 /* Reset fusion-related fields */ 8245 tcp->tcp_fused = B_FALSE; 8246 tcp->tcp_unfusable = B_FALSE; 8247 tcp->tcp_fused_sigurg = B_FALSE; 8248 tcp->tcp_direct_sockfs = B_FALSE; 8249 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8250 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8251 tcp->tcp_loopback_peer = NULL; 8252 tcp->tcp_fuse_rcv_hiwater = 0; 8253 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8254 tcp->tcp_fuse_rcv_unread_cnt = 0; 8255 8256 /* Initialize the header template */ 8257 if (tcp->tcp_ipversion == IPV4_VERSION) { 8258 err = tcp_header_init_ipv4(tcp); 8259 } else { 8260 err = tcp_header_init_ipv6(tcp); 8261 } 8262 if (err) 8263 return (err); 8264 8265 /* 8266 * Init the window scale to the max so tcp_rwnd_set() won't pare 8267 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8268 */ 8269 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8270 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8271 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8272 8273 tcp->tcp_cork = B_FALSE; 8274 /* 8275 * Init the tcp_debug option. This value determines whether TCP 8276 * calls strlog() to print out debug messages. Doing this 8277 * initialization here means that this value is not inherited thru 8278 * tcp_reinit(). 8279 */ 8280 tcp->tcp_debug = tcps->tcps_dbg; 8281 8282 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8283 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8284 8285 return (0); 8286 } 8287 8288 /* 8289 * Initialize the IPv4 header. Loses any record of any IP options. 8290 */ 8291 static int 8292 tcp_header_init_ipv4(tcp_t *tcp) 8293 { 8294 tcph_t *tcph; 8295 uint32_t sum; 8296 conn_t *connp; 8297 tcp_stack_t *tcps = tcp->tcp_tcps; 8298 8299 /* 8300 * This is a simple initialization. If there's 8301 * already a template, it should never be too small, 8302 * so reuse it. Otherwise, allocate space for the new one. 8303 */ 8304 if (tcp->tcp_iphc == NULL) { 8305 ASSERT(tcp->tcp_iphc_len == 0); 8306 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8307 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8308 if (tcp->tcp_iphc == NULL) { 8309 tcp->tcp_iphc_len = 0; 8310 return (ENOMEM); 8311 } 8312 } 8313 8314 /* options are gone; may need a new label */ 8315 connp = tcp->tcp_connp; 8316 connp->conn_mlp_type = mlptSingle; 8317 connp->conn_ulp_labeled = !is_system_labeled(); 8318 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8319 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8320 tcp->tcp_ip6h = NULL; 8321 tcp->tcp_ipversion = IPV4_VERSION; 8322 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8323 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8324 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8325 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8326 tcp->tcp_ipha->ipha_version_and_hdr_length 8327 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8328 tcp->tcp_ipha->ipha_ident = 0; 8329 8330 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8331 tcp->tcp_tos = 0; 8332 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8333 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8334 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8335 8336 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8337 tcp->tcp_tcph = tcph; 8338 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8339 /* 8340 * IP wants our header length in the checksum field to 8341 * allow it to perform a single pseudo-header+checksum 8342 * calculation on behalf of TCP. 8343 * Include the adjustment for a source route once IP_OPTIONS is set. 8344 */ 8345 sum = sizeof (tcph_t) + tcp->tcp_sum; 8346 sum = (sum >> 16) + (sum & 0xFFFF); 8347 U16_TO_ABE16(sum, tcph->th_sum); 8348 return (0); 8349 } 8350 8351 /* 8352 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8353 */ 8354 static int 8355 tcp_header_init_ipv6(tcp_t *tcp) 8356 { 8357 tcph_t *tcph; 8358 uint32_t sum; 8359 conn_t *connp; 8360 tcp_stack_t *tcps = tcp->tcp_tcps; 8361 8362 /* 8363 * This is a simple initialization. If there's 8364 * already a template, it should never be too small, 8365 * so reuse it. Otherwise, allocate space for the new one. 8366 * Ensure that there is enough space to "downgrade" the tcp_t 8367 * to an IPv4 tcp_t. This requires having space for a full load 8368 * of IPv4 options, as well as a full load of TCP options 8369 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8370 * than a v6 header and a TCP header with a full load of TCP options 8371 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8372 * We want to avoid reallocation in the "downgraded" case when 8373 * processing outbound IPv4 options. 8374 */ 8375 if (tcp->tcp_iphc == NULL) { 8376 ASSERT(tcp->tcp_iphc_len == 0); 8377 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8378 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8379 if (tcp->tcp_iphc == NULL) { 8380 tcp->tcp_iphc_len = 0; 8381 return (ENOMEM); 8382 } 8383 } 8384 8385 /* options are gone; may need a new label */ 8386 connp = tcp->tcp_connp; 8387 connp->conn_mlp_type = mlptSingle; 8388 connp->conn_ulp_labeled = !is_system_labeled(); 8389 8390 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8391 tcp->tcp_ipversion = IPV6_VERSION; 8392 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8393 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8394 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8395 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8396 tcp->tcp_ipha = NULL; 8397 8398 /* Initialize the header template */ 8399 8400 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8401 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8402 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8403 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8404 8405 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8406 tcp->tcp_tcph = tcph; 8407 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8408 /* 8409 * IP wants our header length in the checksum field to 8410 * allow it to perform a single psuedo-header+checksum 8411 * calculation on behalf of TCP. 8412 * Include the adjustment for a source route when IPV6_RTHDR is set. 8413 */ 8414 sum = sizeof (tcph_t) + tcp->tcp_sum; 8415 sum = (sum >> 16) + (sum & 0xFFFF); 8416 U16_TO_ABE16(sum, tcph->th_sum); 8417 return (0); 8418 } 8419 8420 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8421 #define ICMP_MIN_TCP_HDR 8 8422 8423 /* 8424 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8425 * passed up by IP. The message is always received on the correct tcp_t. 8426 * Assumes that IP has pulled up everything up to and including the ICMP header. 8427 */ 8428 void 8429 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8430 { 8431 icmph_t *icmph; 8432 ipha_t *ipha; 8433 int iph_hdr_length; 8434 tcph_t *tcph; 8435 boolean_t ipsec_mctl = B_FALSE; 8436 boolean_t secure; 8437 mblk_t *first_mp = mp; 8438 uint32_t new_mss; 8439 uint32_t ratio; 8440 size_t mp_size = MBLKL(mp); 8441 uint32_t seg_seq; 8442 tcp_stack_t *tcps = tcp->tcp_tcps; 8443 8444 /* Assume IP provides aligned packets - otherwise toss */ 8445 if (!OK_32PTR(mp->b_rptr)) { 8446 freemsg(mp); 8447 return; 8448 } 8449 8450 /* 8451 * Since ICMP errors are normal data marked with M_CTL when sent 8452 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8453 * packets starting with an ipsec_info_t, see ipsec_info.h. 8454 */ 8455 if ((mp_size == sizeof (ipsec_info_t)) && 8456 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8457 ASSERT(mp->b_cont != NULL); 8458 mp = mp->b_cont; 8459 /* IP should have done this */ 8460 ASSERT(OK_32PTR(mp->b_rptr)); 8461 mp_size = MBLKL(mp); 8462 ipsec_mctl = B_TRUE; 8463 } 8464 8465 /* 8466 * Verify that we have a complete outer IP header. If not, drop it. 8467 */ 8468 if (mp_size < sizeof (ipha_t)) { 8469 noticmpv4: 8470 freemsg(first_mp); 8471 return; 8472 } 8473 8474 ipha = (ipha_t *)mp->b_rptr; 8475 /* 8476 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8477 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8478 */ 8479 switch (IPH_HDR_VERSION(ipha)) { 8480 case IPV6_VERSION: 8481 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8482 return; 8483 case IPV4_VERSION: 8484 break; 8485 default: 8486 goto noticmpv4; 8487 } 8488 8489 /* Skip past the outer IP and ICMP headers */ 8490 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8491 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8492 /* 8493 * If we don't have the correct outer IP header length or if the ULP 8494 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8495 * send it upstream. 8496 */ 8497 if (iph_hdr_length < sizeof (ipha_t) || 8498 ipha->ipha_protocol != IPPROTO_ICMP || 8499 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8500 goto noticmpv4; 8501 } 8502 ipha = (ipha_t *)&icmph[1]; 8503 8504 /* Skip past the inner IP and find the ULP header */ 8505 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8506 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8507 /* 8508 * If we don't have the correct inner IP header length or if the ULP 8509 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8510 * bytes of TCP header, drop it. 8511 */ 8512 if (iph_hdr_length < sizeof (ipha_t) || 8513 ipha->ipha_protocol != IPPROTO_TCP || 8514 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8515 goto noticmpv4; 8516 } 8517 8518 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8519 if (ipsec_mctl) { 8520 secure = ipsec_in_is_secure(first_mp); 8521 } else { 8522 secure = B_FALSE; 8523 } 8524 if (secure) { 8525 /* 8526 * If we are willing to accept this in clear 8527 * we don't have to verify policy. 8528 */ 8529 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8530 if (!tcp_check_policy(tcp, first_mp, 8531 ipha, NULL, secure, ipsec_mctl)) { 8532 /* 8533 * tcp_check_policy called 8534 * ip_drop_packet() on failure. 8535 */ 8536 return; 8537 } 8538 } 8539 } 8540 } else if (ipsec_mctl) { 8541 /* 8542 * This is a hard_bound connection. IP has already 8543 * verified policy. We don't have to do it again. 8544 */ 8545 freeb(first_mp); 8546 first_mp = mp; 8547 ipsec_mctl = B_FALSE; 8548 } 8549 8550 seg_seq = ABE32_TO_U32(tcph->th_seq); 8551 /* 8552 * TCP SHOULD check that the TCP sequence number contained in 8553 * payload of the ICMP error message is within the range 8554 * SND.UNA <= SEG.SEQ < SND.NXT. 8555 */ 8556 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8557 /* 8558 * If the ICMP message is bogus, should we kill the 8559 * connection, or should we just drop the bogus ICMP 8560 * message? It would probably make more sense to just 8561 * drop the message so that if this one managed to get 8562 * in, the real connection should not suffer. 8563 */ 8564 goto noticmpv4; 8565 } 8566 8567 switch (icmph->icmph_type) { 8568 case ICMP_DEST_UNREACHABLE: 8569 switch (icmph->icmph_code) { 8570 case ICMP_FRAGMENTATION_NEEDED: 8571 /* 8572 * Reduce the MSS based on the new MTU. This will 8573 * eliminate any fragmentation locally. 8574 * N.B. There may well be some funny side-effects on 8575 * the local send policy and the remote receive policy. 8576 * Pending further research, we provide 8577 * tcp_ignore_path_mtu just in case this proves 8578 * disastrous somewhere. 8579 * 8580 * After updating the MSS, retransmit part of the 8581 * dropped segment using the new mss by calling 8582 * tcp_wput_data(). Need to adjust all those 8583 * params to make sure tcp_wput_data() work properly. 8584 */ 8585 if (tcps->tcps_ignore_path_mtu) 8586 break; 8587 8588 /* 8589 * Decrease the MSS by time stamp options 8590 * IP options and IPSEC options. tcp_hdr_len 8591 * includes time stamp option and IP option 8592 * length. 8593 */ 8594 8595 new_mss = ntohs(icmph->icmph_du_mtu) - 8596 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8597 8598 /* 8599 * Only update the MSS if the new one is 8600 * smaller than the previous one. This is 8601 * to avoid problems when getting multiple 8602 * ICMP errors for the same MTU. 8603 */ 8604 if (new_mss >= tcp->tcp_mss) 8605 break; 8606 8607 /* 8608 * Stop doing PMTU if new_mss is less than 68 8609 * or less than tcp_mss_min. 8610 * The value 68 comes from rfc 1191. 8611 */ 8612 if (new_mss < MAX(68, tcps->tcps_mss_min)) 8613 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8614 0; 8615 8616 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8617 ASSERT(ratio >= 1); 8618 tcp_mss_set(tcp, new_mss); 8619 8620 /* 8621 * Make sure we have something to 8622 * send. 8623 */ 8624 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8625 (tcp->tcp_xmit_head != NULL)) { 8626 /* 8627 * Shrink tcp_cwnd in 8628 * proportion to the old MSS/new MSS. 8629 */ 8630 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8631 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8632 (tcp->tcp_unsent == 0)) { 8633 tcp->tcp_rexmit_max = tcp->tcp_fss; 8634 } else { 8635 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8636 } 8637 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8638 tcp->tcp_rexmit = B_TRUE; 8639 tcp->tcp_dupack_cnt = 0; 8640 tcp->tcp_snd_burst = TCP_CWND_SS; 8641 tcp_ss_rexmit(tcp); 8642 } 8643 break; 8644 case ICMP_PORT_UNREACHABLE: 8645 case ICMP_PROTOCOL_UNREACHABLE: 8646 switch (tcp->tcp_state) { 8647 case TCPS_SYN_SENT: 8648 case TCPS_SYN_RCVD: 8649 /* 8650 * ICMP can snipe away incipient 8651 * TCP connections as long as 8652 * seq number is same as initial 8653 * send seq number. 8654 */ 8655 if (seg_seq == tcp->tcp_iss) { 8656 (void) tcp_clean_death(tcp, 8657 ECONNREFUSED, 6); 8658 } 8659 break; 8660 } 8661 break; 8662 case ICMP_HOST_UNREACHABLE: 8663 case ICMP_NET_UNREACHABLE: 8664 /* Record the error in case we finally time out. */ 8665 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8666 tcp->tcp_client_errno = EHOSTUNREACH; 8667 else 8668 tcp->tcp_client_errno = ENETUNREACH; 8669 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8670 if (tcp->tcp_listener != NULL && 8671 tcp->tcp_listener->tcp_syn_defense) { 8672 /* 8673 * Ditch the half-open connection if we 8674 * suspect a SYN attack is under way. 8675 */ 8676 tcp_ip_ire_mark_advice(tcp); 8677 (void) tcp_clean_death(tcp, 8678 tcp->tcp_client_errno, 7); 8679 } 8680 } 8681 break; 8682 default: 8683 break; 8684 } 8685 break; 8686 case ICMP_SOURCE_QUENCH: { 8687 /* 8688 * use a global boolean to control 8689 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8690 * The default is false. 8691 */ 8692 if (tcp_icmp_source_quench) { 8693 /* 8694 * Reduce the sending rate as if we got a 8695 * retransmit timeout 8696 */ 8697 uint32_t npkt; 8698 8699 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8700 tcp->tcp_mss; 8701 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8702 tcp->tcp_cwnd = tcp->tcp_mss; 8703 tcp->tcp_cwnd_cnt = 0; 8704 } 8705 break; 8706 } 8707 } 8708 freemsg(first_mp); 8709 } 8710 8711 /* 8712 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8713 * error messages passed up by IP. 8714 * Assumes that IP has pulled up all the extension headers as well 8715 * as the ICMPv6 header. 8716 */ 8717 static void 8718 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8719 { 8720 icmp6_t *icmp6; 8721 ip6_t *ip6h; 8722 uint16_t iph_hdr_length; 8723 tcpha_t *tcpha; 8724 uint8_t *nexthdrp; 8725 uint32_t new_mss; 8726 uint32_t ratio; 8727 boolean_t secure; 8728 mblk_t *first_mp = mp; 8729 size_t mp_size; 8730 uint32_t seg_seq; 8731 tcp_stack_t *tcps = tcp->tcp_tcps; 8732 8733 /* 8734 * The caller has determined if this is an IPSEC_IN packet and 8735 * set ipsec_mctl appropriately (see tcp_icmp_error). 8736 */ 8737 if (ipsec_mctl) 8738 mp = mp->b_cont; 8739 8740 mp_size = MBLKL(mp); 8741 8742 /* 8743 * Verify that we have a complete IP header. If not, send it upstream. 8744 */ 8745 if (mp_size < sizeof (ip6_t)) { 8746 noticmpv6: 8747 freemsg(first_mp); 8748 return; 8749 } 8750 8751 /* 8752 * Verify this is an ICMPV6 packet, else send it upstream. 8753 */ 8754 ip6h = (ip6_t *)mp->b_rptr; 8755 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8756 iph_hdr_length = IPV6_HDR_LEN; 8757 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8758 &nexthdrp) || 8759 *nexthdrp != IPPROTO_ICMPV6) { 8760 goto noticmpv6; 8761 } 8762 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8763 ip6h = (ip6_t *)&icmp6[1]; 8764 /* 8765 * Verify if we have a complete ICMP and inner IP header. 8766 */ 8767 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8768 goto noticmpv6; 8769 8770 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8771 goto noticmpv6; 8772 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8773 /* 8774 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8775 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8776 * packet. 8777 */ 8778 if ((*nexthdrp != IPPROTO_TCP) || 8779 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8780 goto noticmpv6; 8781 } 8782 8783 /* 8784 * ICMP errors come on the right queue or come on 8785 * listener/global queue for detached connections and 8786 * get switched to the right queue. If it comes on the 8787 * right queue, policy check has already been done by IP 8788 * and thus free the first_mp without verifying the policy. 8789 * If it has come for a non-hard bound connection, we need 8790 * to verify policy as IP may not have done it. 8791 */ 8792 if (!tcp->tcp_hard_bound) { 8793 if (ipsec_mctl) { 8794 secure = ipsec_in_is_secure(first_mp); 8795 } else { 8796 secure = B_FALSE; 8797 } 8798 if (secure) { 8799 /* 8800 * If we are willing to accept this in clear 8801 * we don't have to verify policy. 8802 */ 8803 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8804 if (!tcp_check_policy(tcp, first_mp, 8805 NULL, ip6h, secure, ipsec_mctl)) { 8806 /* 8807 * tcp_check_policy called 8808 * ip_drop_packet() on failure. 8809 */ 8810 return; 8811 } 8812 } 8813 } 8814 } else if (ipsec_mctl) { 8815 /* 8816 * This is a hard_bound connection. IP has already 8817 * verified policy. We don't have to do it again. 8818 */ 8819 freeb(first_mp); 8820 first_mp = mp; 8821 ipsec_mctl = B_FALSE; 8822 } 8823 8824 seg_seq = ntohl(tcpha->tha_seq); 8825 /* 8826 * TCP SHOULD check that the TCP sequence number contained in 8827 * payload of the ICMP error message is within the range 8828 * SND.UNA <= SEG.SEQ < SND.NXT. 8829 */ 8830 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8831 /* 8832 * If the ICMP message is bogus, should we kill the 8833 * connection, or should we just drop the bogus ICMP 8834 * message? It would probably make more sense to just 8835 * drop the message so that if this one managed to get 8836 * in, the real connection should not suffer. 8837 */ 8838 goto noticmpv6; 8839 } 8840 8841 switch (icmp6->icmp6_type) { 8842 case ICMP6_PACKET_TOO_BIG: 8843 /* 8844 * Reduce the MSS based on the new MTU. This will 8845 * eliminate any fragmentation locally. 8846 * N.B. There may well be some funny side-effects on 8847 * the local send policy and the remote receive policy. 8848 * Pending further research, we provide 8849 * tcp_ignore_path_mtu just in case this proves 8850 * disastrous somewhere. 8851 * 8852 * After updating the MSS, retransmit part of the 8853 * dropped segment using the new mss by calling 8854 * tcp_wput_data(). Need to adjust all those 8855 * params to make sure tcp_wput_data() work properly. 8856 */ 8857 if (tcps->tcps_ignore_path_mtu) 8858 break; 8859 8860 /* 8861 * Decrease the MSS by time stamp options 8862 * IP options and IPSEC options. tcp_hdr_len 8863 * includes time stamp option and IP option 8864 * length. 8865 */ 8866 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8867 tcp->tcp_ipsec_overhead; 8868 8869 /* 8870 * Only update the MSS if the new one is 8871 * smaller than the previous one. This is 8872 * to avoid problems when getting multiple 8873 * ICMP errors for the same MTU. 8874 */ 8875 if (new_mss >= tcp->tcp_mss) 8876 break; 8877 8878 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8879 ASSERT(ratio >= 1); 8880 tcp_mss_set(tcp, new_mss); 8881 8882 /* 8883 * Make sure we have something to 8884 * send. 8885 */ 8886 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8887 (tcp->tcp_xmit_head != NULL)) { 8888 /* 8889 * Shrink tcp_cwnd in 8890 * proportion to the old MSS/new MSS. 8891 */ 8892 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8893 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8894 (tcp->tcp_unsent == 0)) { 8895 tcp->tcp_rexmit_max = tcp->tcp_fss; 8896 } else { 8897 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8898 } 8899 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8900 tcp->tcp_rexmit = B_TRUE; 8901 tcp->tcp_dupack_cnt = 0; 8902 tcp->tcp_snd_burst = TCP_CWND_SS; 8903 tcp_ss_rexmit(tcp); 8904 } 8905 break; 8906 8907 case ICMP6_DST_UNREACH: 8908 switch (icmp6->icmp6_code) { 8909 case ICMP6_DST_UNREACH_NOPORT: 8910 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8911 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8912 (seg_seq == tcp->tcp_iss)) { 8913 (void) tcp_clean_death(tcp, 8914 ECONNREFUSED, 8); 8915 } 8916 break; 8917 8918 case ICMP6_DST_UNREACH_ADMIN: 8919 case ICMP6_DST_UNREACH_NOROUTE: 8920 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8921 case ICMP6_DST_UNREACH_ADDR: 8922 /* Record the error in case we finally time out. */ 8923 tcp->tcp_client_errno = EHOSTUNREACH; 8924 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8925 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8926 (seg_seq == tcp->tcp_iss)) { 8927 if (tcp->tcp_listener != NULL && 8928 tcp->tcp_listener->tcp_syn_defense) { 8929 /* 8930 * Ditch the half-open connection if we 8931 * suspect a SYN attack is under way. 8932 */ 8933 tcp_ip_ire_mark_advice(tcp); 8934 (void) tcp_clean_death(tcp, 8935 tcp->tcp_client_errno, 9); 8936 } 8937 } 8938 8939 8940 break; 8941 default: 8942 break; 8943 } 8944 break; 8945 8946 case ICMP6_PARAM_PROB: 8947 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8948 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8949 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8950 (uchar_t *)nexthdrp) { 8951 if (tcp->tcp_state == TCPS_SYN_SENT || 8952 tcp->tcp_state == TCPS_SYN_RCVD) { 8953 (void) tcp_clean_death(tcp, 8954 ECONNREFUSED, 10); 8955 } 8956 break; 8957 } 8958 break; 8959 8960 case ICMP6_TIME_EXCEEDED: 8961 default: 8962 break; 8963 } 8964 freemsg(first_mp); 8965 } 8966 8967 /* 8968 * IP recognizes seven kinds of bind requests: 8969 * 8970 * - A zero-length address binds only to the protocol number. 8971 * 8972 * - A 4-byte address is treated as a request to 8973 * validate that the address is a valid local IPv4 8974 * address, appropriate for an application to bind to. 8975 * IP does the verification, but does not make any note 8976 * of the address at this time. 8977 * 8978 * - A 16-byte address contains is treated as a request 8979 * to validate a local IPv6 address, as the 4-byte 8980 * address case above. 8981 * 8982 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 8983 * use it for the inbound fanout of packets. 8984 * 8985 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 8986 * use it for the inbound fanout of packets. 8987 * 8988 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 8989 * information consisting of local and remote addresses 8990 * and ports. In this case, the addresses are both 8991 * validated as appropriate for this operation, and, if 8992 * so, the information is retained for use in the 8993 * inbound fanout. 8994 * 8995 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 8996 * fanout information, like the 12-byte case above. 8997 * 8998 * IP will also fill in the IRE request mblk with information 8999 * regarding our peer. In all cases, we notify IP of our protocol 9000 * type by appending a single protocol byte to the bind request. 9001 */ 9002 static mblk_t * 9003 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 9004 { 9005 char *cp; 9006 mblk_t *mp; 9007 struct T_bind_req *tbr; 9008 ipa_conn_t *ac; 9009 ipa6_conn_t *ac6; 9010 sin_t *sin; 9011 sin6_t *sin6; 9012 9013 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9014 ASSERT((tcp->tcp_family == AF_INET && 9015 tcp->tcp_ipversion == IPV4_VERSION) || 9016 (tcp->tcp_family == AF_INET6 && 9017 (tcp->tcp_ipversion == IPV4_VERSION || 9018 tcp->tcp_ipversion == IPV6_VERSION))); 9019 9020 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9021 if (!mp) 9022 return (mp); 9023 mp->b_datap->db_type = M_PROTO; 9024 tbr = (struct T_bind_req *)mp->b_rptr; 9025 tbr->PRIM_type = bind_prim; 9026 tbr->ADDR_offset = sizeof (*tbr); 9027 tbr->CONIND_number = 0; 9028 tbr->ADDR_length = addr_length; 9029 cp = (char *)&tbr[1]; 9030 switch (addr_length) { 9031 case sizeof (ipa_conn_t): 9032 ASSERT(tcp->tcp_family == AF_INET); 9033 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9034 9035 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9036 if (mp->b_cont == NULL) { 9037 freemsg(mp); 9038 return (NULL); 9039 } 9040 mp->b_cont->b_wptr += sizeof (ire_t); 9041 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9042 9043 /* cp known to be 32 bit aligned */ 9044 ac = (ipa_conn_t *)cp; 9045 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9046 ac->ac_faddr = tcp->tcp_remote; 9047 ac->ac_fport = tcp->tcp_fport; 9048 ac->ac_lport = tcp->tcp_lport; 9049 tcp->tcp_hard_binding = 1; 9050 break; 9051 9052 case sizeof (ipa6_conn_t): 9053 ASSERT(tcp->tcp_family == AF_INET6); 9054 9055 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9056 if (mp->b_cont == NULL) { 9057 freemsg(mp); 9058 return (NULL); 9059 } 9060 mp->b_cont->b_wptr += sizeof (ire_t); 9061 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9062 9063 /* cp known to be 32 bit aligned */ 9064 ac6 = (ipa6_conn_t *)cp; 9065 if (tcp->tcp_ipversion == IPV4_VERSION) { 9066 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9067 &ac6->ac6_laddr); 9068 } else { 9069 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9070 } 9071 ac6->ac6_faddr = tcp->tcp_remote_v6; 9072 ac6->ac6_fport = tcp->tcp_fport; 9073 ac6->ac6_lport = tcp->tcp_lport; 9074 tcp->tcp_hard_binding = 1; 9075 break; 9076 9077 case sizeof (sin_t): 9078 /* 9079 * NOTE: IPV6_ADDR_LEN also has same size. 9080 * Use family to discriminate. 9081 */ 9082 if (tcp->tcp_family == AF_INET) { 9083 sin = (sin_t *)cp; 9084 9085 *sin = sin_null; 9086 sin->sin_family = AF_INET; 9087 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9088 sin->sin_port = tcp->tcp_lport; 9089 break; 9090 } else { 9091 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9092 } 9093 break; 9094 9095 case sizeof (sin6_t): 9096 ASSERT(tcp->tcp_family == AF_INET6); 9097 sin6 = (sin6_t *)cp; 9098 9099 *sin6 = sin6_null; 9100 sin6->sin6_family = AF_INET6; 9101 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9102 sin6->sin6_port = tcp->tcp_lport; 9103 break; 9104 9105 case IP_ADDR_LEN: 9106 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9107 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9108 break; 9109 9110 } 9111 /* Add protocol number to end */ 9112 cp[addr_length] = (char)IPPROTO_TCP; 9113 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9114 return (mp); 9115 } 9116 9117 /* 9118 * Notify IP that we are having trouble with this connection. IP should 9119 * blow the IRE away and start over. 9120 */ 9121 static void 9122 tcp_ip_notify(tcp_t *tcp) 9123 { 9124 struct iocblk *iocp; 9125 ipid_t *ipid; 9126 mblk_t *mp; 9127 9128 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9129 if (tcp->tcp_ipversion == IPV6_VERSION) 9130 return; 9131 9132 mp = mkiocb(IP_IOCTL); 9133 if (mp == NULL) 9134 return; 9135 9136 iocp = (struct iocblk *)mp->b_rptr; 9137 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9138 9139 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9140 if (!mp->b_cont) { 9141 freeb(mp); 9142 return; 9143 } 9144 9145 ipid = (ipid_t *)mp->b_cont->b_rptr; 9146 mp->b_cont->b_wptr += iocp->ioc_count; 9147 bzero(ipid, sizeof (*ipid)); 9148 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9149 ipid->ipid_ire_type = IRE_CACHE; 9150 ipid->ipid_addr_offset = sizeof (ipid_t); 9151 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9152 /* 9153 * Note: in the case of source routing we want to blow away the 9154 * route to the first source route hop. 9155 */ 9156 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9157 sizeof (tcp->tcp_ipha->ipha_dst)); 9158 9159 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9160 } 9161 9162 /* Unlink and return any mblk that looks like it contains an ire */ 9163 static mblk_t * 9164 tcp_ire_mp(mblk_t *mp) 9165 { 9166 mblk_t *prev_mp; 9167 9168 for (;;) { 9169 prev_mp = mp; 9170 mp = mp->b_cont; 9171 if (mp == NULL) 9172 break; 9173 switch (DB_TYPE(mp)) { 9174 case IRE_DB_TYPE: 9175 case IRE_DB_REQ_TYPE: 9176 if (prev_mp != NULL) 9177 prev_mp->b_cont = mp->b_cont; 9178 mp->b_cont = NULL; 9179 return (mp); 9180 default: 9181 break; 9182 } 9183 } 9184 return (mp); 9185 } 9186 9187 /* 9188 * Timer callback routine for keepalive probe. We do a fake resend of 9189 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9190 * check to see if we have heard anything from the other end for the last 9191 * RTO period. If we have, set the timer to expire for another 9192 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9193 * RTO << 1 and check again when it expires. Keep exponentially increasing 9194 * the timeout if we have not heard from the other side. If for more than 9195 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9196 * kill the connection unless the keepalive abort threshold is 0. In 9197 * that case, we will probe "forever." 9198 */ 9199 static void 9200 tcp_keepalive_killer(void *arg) 9201 { 9202 mblk_t *mp; 9203 conn_t *connp = (conn_t *)arg; 9204 tcp_t *tcp = connp->conn_tcp; 9205 int32_t firetime; 9206 int32_t idletime; 9207 int32_t ka_intrvl; 9208 tcp_stack_t *tcps = tcp->tcp_tcps; 9209 9210 tcp->tcp_ka_tid = 0; 9211 9212 if (tcp->tcp_fused) 9213 return; 9214 9215 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9216 ka_intrvl = tcp->tcp_ka_interval; 9217 9218 /* 9219 * Keepalive probe should only be sent if the application has not 9220 * done a close on the connection. 9221 */ 9222 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9223 return; 9224 } 9225 /* Timer fired too early, restart it. */ 9226 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9227 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9228 MSEC_TO_TICK(ka_intrvl)); 9229 return; 9230 } 9231 9232 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9233 /* 9234 * If we have not heard from the other side for a long 9235 * time, kill the connection unless the keepalive abort 9236 * threshold is 0. In that case, we will probe "forever." 9237 */ 9238 if (tcp->tcp_ka_abort_thres != 0 && 9239 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9240 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9241 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9242 tcp->tcp_client_errno : ETIMEDOUT, 11); 9243 return; 9244 } 9245 9246 if (tcp->tcp_snxt == tcp->tcp_suna && 9247 idletime >= ka_intrvl) { 9248 /* Fake resend of last ACKed byte. */ 9249 mblk_t *mp1 = allocb(1, BPRI_LO); 9250 9251 if (mp1 != NULL) { 9252 *mp1->b_wptr++ = '\0'; 9253 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9254 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9255 freeb(mp1); 9256 /* 9257 * if allocation failed, fall through to start the 9258 * timer back. 9259 */ 9260 if (mp != NULL) { 9261 TCP_RECORD_TRACE(tcp, mp, 9262 TCP_TRACE_SEND_PKT); 9263 tcp_send_data(tcp, tcp->tcp_wq, mp); 9264 BUMP_MIB(&tcps->tcps_mib, 9265 tcpTimKeepaliveProbe); 9266 if (tcp->tcp_ka_last_intrvl != 0) { 9267 int max; 9268 /* 9269 * We should probe again at least 9270 * in ka_intrvl, but not more than 9271 * tcp_rexmit_interval_max. 9272 */ 9273 max = tcps->tcps_rexmit_interval_max; 9274 firetime = MIN(ka_intrvl - 1, 9275 tcp->tcp_ka_last_intrvl << 1); 9276 if (firetime > max) 9277 firetime = max; 9278 } else { 9279 firetime = tcp->tcp_rto; 9280 } 9281 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9282 tcp_keepalive_killer, 9283 MSEC_TO_TICK(firetime)); 9284 tcp->tcp_ka_last_intrvl = firetime; 9285 return; 9286 } 9287 } 9288 } else { 9289 tcp->tcp_ka_last_intrvl = 0; 9290 } 9291 9292 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9293 if ((firetime = ka_intrvl - idletime) < 0) { 9294 firetime = ka_intrvl; 9295 } 9296 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9297 MSEC_TO_TICK(firetime)); 9298 } 9299 9300 int 9301 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9302 { 9303 queue_t *q = tcp->tcp_rq; 9304 int32_t mss = tcp->tcp_mss; 9305 int maxpsz; 9306 9307 if (TCP_IS_DETACHED(tcp)) 9308 return (mss); 9309 9310 if (tcp->tcp_fused) { 9311 maxpsz = tcp_fuse_maxpsz_set(tcp); 9312 mss = INFPSZ; 9313 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9314 /* 9315 * Set the sd_qn_maxpsz according to the socket send buffer 9316 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9317 * instruct the stream head to copyin user data into contiguous 9318 * kernel-allocated buffers without breaking it up into smaller 9319 * chunks. We round up the buffer size to the nearest SMSS. 9320 */ 9321 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9322 if (tcp->tcp_kssl_ctx == NULL) 9323 mss = INFPSZ; 9324 else 9325 mss = SSL3_MAX_RECORD_LEN; 9326 } else { 9327 /* 9328 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9329 * (and a multiple of the mss). This instructs the stream 9330 * head to break down larger than SMSS writes into SMSS- 9331 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9332 */ 9333 maxpsz = tcp->tcp_maxpsz * mss; 9334 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9335 maxpsz = tcp->tcp_xmit_hiwater/2; 9336 /* Round up to nearest mss */ 9337 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9338 } 9339 } 9340 (void) setmaxps(q, maxpsz); 9341 tcp->tcp_wq->q_maxpsz = maxpsz; 9342 9343 if (set_maxblk) 9344 (void) mi_set_sth_maxblk(q, mss); 9345 9346 return (mss); 9347 } 9348 9349 /* 9350 * Extract option values from a tcp header. We put any found values into the 9351 * tcpopt struct and return a bitmask saying which options were found. 9352 */ 9353 static int 9354 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9355 { 9356 uchar_t *endp; 9357 int len; 9358 uint32_t mss; 9359 uchar_t *up = (uchar_t *)tcph; 9360 int found = 0; 9361 int32_t sack_len; 9362 tcp_seq sack_begin, sack_end; 9363 tcp_t *tcp; 9364 9365 endp = up + TCP_HDR_LENGTH(tcph); 9366 up += TCP_MIN_HEADER_LENGTH; 9367 while (up < endp) { 9368 len = endp - up; 9369 switch (*up) { 9370 case TCPOPT_EOL: 9371 break; 9372 9373 case TCPOPT_NOP: 9374 up++; 9375 continue; 9376 9377 case TCPOPT_MAXSEG: 9378 if (len < TCPOPT_MAXSEG_LEN || 9379 up[1] != TCPOPT_MAXSEG_LEN) 9380 break; 9381 9382 mss = BE16_TO_U16(up+2); 9383 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9384 tcpopt->tcp_opt_mss = mss; 9385 found |= TCP_OPT_MSS_PRESENT; 9386 9387 up += TCPOPT_MAXSEG_LEN; 9388 continue; 9389 9390 case TCPOPT_WSCALE: 9391 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9392 break; 9393 9394 if (up[2] > TCP_MAX_WINSHIFT) 9395 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9396 else 9397 tcpopt->tcp_opt_wscale = up[2]; 9398 found |= TCP_OPT_WSCALE_PRESENT; 9399 9400 up += TCPOPT_WS_LEN; 9401 continue; 9402 9403 case TCPOPT_SACK_PERMITTED: 9404 if (len < TCPOPT_SACK_OK_LEN || 9405 up[1] != TCPOPT_SACK_OK_LEN) 9406 break; 9407 found |= TCP_OPT_SACK_OK_PRESENT; 9408 up += TCPOPT_SACK_OK_LEN; 9409 continue; 9410 9411 case TCPOPT_SACK: 9412 if (len <= 2 || up[1] <= 2 || len < up[1]) 9413 break; 9414 9415 /* If TCP is not interested in SACK blks... */ 9416 if ((tcp = tcpopt->tcp) == NULL) { 9417 up += up[1]; 9418 continue; 9419 } 9420 sack_len = up[1] - TCPOPT_HEADER_LEN; 9421 up += TCPOPT_HEADER_LEN; 9422 9423 /* 9424 * If the list is empty, allocate one and assume 9425 * nothing is sack'ed. 9426 */ 9427 ASSERT(tcp->tcp_sack_info != NULL); 9428 if (tcp->tcp_notsack_list == NULL) { 9429 tcp_notsack_update(&(tcp->tcp_notsack_list), 9430 tcp->tcp_suna, tcp->tcp_snxt, 9431 &(tcp->tcp_num_notsack_blk), 9432 &(tcp->tcp_cnt_notsack_list)); 9433 9434 /* 9435 * Make sure tcp_notsack_list is not NULL. 9436 * This happens when kmem_alloc(KM_NOSLEEP) 9437 * returns NULL. 9438 */ 9439 if (tcp->tcp_notsack_list == NULL) { 9440 up += sack_len; 9441 continue; 9442 } 9443 tcp->tcp_fack = tcp->tcp_suna; 9444 } 9445 9446 while (sack_len > 0) { 9447 if (up + 8 > endp) { 9448 up = endp; 9449 break; 9450 } 9451 sack_begin = BE32_TO_U32(up); 9452 up += 4; 9453 sack_end = BE32_TO_U32(up); 9454 up += 4; 9455 sack_len -= 8; 9456 /* 9457 * Bounds checking. Make sure the SACK 9458 * info is within tcp_suna and tcp_snxt. 9459 * If this SACK blk is out of bound, ignore 9460 * it but continue to parse the following 9461 * blks. 9462 */ 9463 if (SEQ_LEQ(sack_end, sack_begin) || 9464 SEQ_LT(sack_begin, tcp->tcp_suna) || 9465 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9466 continue; 9467 } 9468 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9469 sack_begin, sack_end, 9470 &(tcp->tcp_num_notsack_blk), 9471 &(tcp->tcp_cnt_notsack_list)); 9472 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9473 tcp->tcp_fack = sack_end; 9474 } 9475 } 9476 found |= TCP_OPT_SACK_PRESENT; 9477 continue; 9478 9479 case TCPOPT_TSTAMP: 9480 if (len < TCPOPT_TSTAMP_LEN || 9481 up[1] != TCPOPT_TSTAMP_LEN) 9482 break; 9483 9484 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9485 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9486 9487 found |= TCP_OPT_TSTAMP_PRESENT; 9488 9489 up += TCPOPT_TSTAMP_LEN; 9490 continue; 9491 9492 default: 9493 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9494 break; 9495 up += up[1]; 9496 continue; 9497 } 9498 break; 9499 } 9500 return (found); 9501 } 9502 9503 /* 9504 * Set the mss associated with a particular tcp based on its current value, 9505 * and a new one passed in. Observe minimums and maximums, and reset 9506 * other state variables that we want to view as multiples of mss. 9507 * 9508 * This function is called in various places mainly because 9509 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9510 * other side's SYN/SYN-ACK packet arrives. 9511 * 2) PMTUd may get us a new MSS. 9512 * 3) If the other side stops sending us timestamp option, we need to 9513 * increase the MSS size to use the extra bytes available. 9514 */ 9515 static void 9516 tcp_mss_set(tcp_t *tcp, uint32_t mss) 9517 { 9518 uint32_t mss_max; 9519 tcp_stack_t *tcps = tcp->tcp_tcps; 9520 9521 if (tcp->tcp_ipversion == IPV4_VERSION) 9522 mss_max = tcps->tcps_mss_max_ipv4; 9523 else 9524 mss_max = tcps->tcps_mss_max_ipv6; 9525 9526 if (mss < tcps->tcps_mss_min) 9527 mss = tcps->tcps_mss_min; 9528 if (mss > mss_max) 9529 mss = mss_max; 9530 /* 9531 * Unless naglim has been set by our client to 9532 * a non-mss value, force naglim to track mss. 9533 * This can help to aggregate small writes. 9534 */ 9535 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9536 tcp->tcp_naglim = mss; 9537 /* 9538 * TCP should be able to buffer at least 4 MSS data for obvious 9539 * performance reason. 9540 */ 9541 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9542 tcp->tcp_xmit_hiwater = mss << 2; 9543 9544 /* 9545 * Check if we need to apply the tcp_init_cwnd here. If 9546 * it is set and the MSS gets bigger (should not happen 9547 * normally), we need to adjust the resulting tcp_cwnd properly. 9548 * The new tcp_cwnd should not get bigger. 9549 */ 9550 if (tcp->tcp_init_cwnd == 0) { 9551 tcp->tcp_cwnd = MIN(tcps->tcps_slow_start_initial * mss, 9552 MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9553 } else { 9554 if (tcp->tcp_mss < mss) { 9555 tcp->tcp_cwnd = MAX(1, 9556 (tcp->tcp_init_cwnd * tcp->tcp_mss / mss)) * mss; 9557 } else { 9558 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9559 } 9560 } 9561 tcp->tcp_mss = mss; 9562 tcp->tcp_cwnd_cnt = 0; 9563 (void) tcp_maxpsz_set(tcp, B_TRUE); 9564 } 9565 9566 static int 9567 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9568 { 9569 tcp_t *tcp = NULL; 9570 conn_t *connp; 9571 int err; 9572 dev_t conn_dev; 9573 zoneid_t zoneid; 9574 tcp_stack_t *tcps = NULL; 9575 9576 if (q->q_ptr != NULL) 9577 return (0); 9578 9579 if (!(flag & SO_ACCEPTOR)) { 9580 /* 9581 * Special case for install: miniroot needs to be able to 9582 * access files via NFS as though it were always in the 9583 * global zone. 9584 */ 9585 if (credp == kcred && nfs_global_client_only != 0) { 9586 zoneid = GLOBAL_ZONEID; 9587 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9588 netstack_tcp; 9589 ASSERT(tcps != NULL); 9590 } else { 9591 netstack_t *ns; 9592 9593 ns = netstack_find_by_cred(credp); 9594 ASSERT(ns != NULL); 9595 tcps = ns->netstack_tcp; 9596 ASSERT(tcps != NULL); 9597 9598 /* 9599 * For exclusive stacks we set the zoneid to zero 9600 * to make TCP operate as if in the global zone. 9601 */ 9602 if (tcps->tcps_netstack->netstack_stackid != 9603 GLOBAL_NETSTACKID) 9604 zoneid = GLOBAL_ZONEID; 9605 else 9606 zoneid = crgetzoneid(credp); 9607 } 9608 /* 9609 * For stackid zero this is done from strplumb.c, but 9610 * non-zero stackids are handled here. 9611 */ 9612 if (tcps->tcps_g_q == NULL && 9613 tcps->tcps_netstack->netstack_stackid != 9614 GLOBAL_NETSTACKID) { 9615 tcp_g_q_setup(tcps); 9616 } 9617 } 9618 if (sflag == MODOPEN) { 9619 /* 9620 * This is a special case. The purpose of a modopen 9621 * is to allow just the T_SVR4_OPTMGMT_REQ to pass 9622 * through for MIB browsers. Everything else is failed. 9623 */ 9624 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9625 /* tcp_get_conn incremented refcnt */ 9626 netstack_rele(tcps->tcps_netstack); 9627 9628 if (connp == NULL) 9629 return (ENOMEM); 9630 9631 connp->conn_flags |= IPCL_TCPMOD; 9632 connp->conn_cred = credp; 9633 connp->conn_zoneid = zoneid; 9634 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9635 ASSERT(connp->conn_netstack->netstack_tcp == tcps); 9636 q->q_ptr = WR(q)->q_ptr = connp; 9637 crhold(credp); 9638 q->q_qinfo = &tcp_mod_rinit; 9639 WR(q)->q_qinfo = &tcp_mod_winit; 9640 qprocson(q); 9641 return (0); 9642 } 9643 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 9644 if (tcps != NULL) 9645 netstack_rele(tcps->tcps_netstack); 9646 return (EBUSY); 9647 } 9648 9649 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9650 9651 if (flag & SO_ACCEPTOR) { 9652 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9653 ASSERT(tcps == NULL); 9654 q->q_qinfo = &tcp_acceptor_rinit; 9655 q->q_ptr = (void *)conn_dev; 9656 WR(q)->q_qinfo = &tcp_acceptor_winit; 9657 WR(q)->q_ptr = (void *)conn_dev; 9658 qprocson(q); 9659 return (0); 9660 } 9661 9662 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9663 /* 9664 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9665 * so we drop it by one. 9666 */ 9667 netstack_rele(tcps->tcps_netstack); 9668 if (connp == NULL) { 9669 inet_minor_free(ip_minor_arena, conn_dev); 9670 q->q_ptr = NULL; 9671 return (ENOSR); 9672 } 9673 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9674 tcp = connp->conn_tcp; 9675 9676 q->q_ptr = WR(q)->q_ptr = connp; 9677 if (getmajor(*devp) == TCP6_MAJ) { 9678 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9679 connp->conn_send = ip_output_v6; 9680 connp->conn_af_isv6 = B_TRUE; 9681 connp->conn_pkt_isv6 = B_TRUE; 9682 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9683 tcp->tcp_ipversion = IPV6_VERSION; 9684 tcp->tcp_family = AF_INET6; 9685 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9686 } else { 9687 connp->conn_flags |= IPCL_TCP4; 9688 connp->conn_send = ip_output; 9689 connp->conn_af_isv6 = B_FALSE; 9690 connp->conn_pkt_isv6 = B_FALSE; 9691 tcp->tcp_ipversion = IPV4_VERSION; 9692 tcp->tcp_family = AF_INET; 9693 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9694 } 9695 9696 /* 9697 * TCP keeps a copy of cred for cache locality reasons but 9698 * we put a reference only once. If connp->conn_cred 9699 * becomes invalid, tcp_cred should also be set to NULL. 9700 */ 9701 tcp->tcp_cred = connp->conn_cred = credp; 9702 crhold(connp->conn_cred); 9703 tcp->tcp_cpid = curproc->p_pid; 9704 tcp->tcp_open_time = lbolt64; 9705 connp->conn_zoneid = zoneid; 9706 connp->conn_mlp_type = mlptSingle; 9707 connp->conn_ulp_labeled = !is_system_labeled(); 9708 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9709 ASSERT(tcp->tcp_tcps == tcps); 9710 9711 /* 9712 * If the caller has the process-wide flag set, then default to MAC 9713 * exempt mode. This allows read-down to unlabeled hosts. 9714 */ 9715 if (getpflags(NET_MAC_AWARE, credp) != 0) 9716 connp->conn_mac_exempt = B_TRUE; 9717 9718 connp->conn_dev = conn_dev; 9719 9720 ASSERT(q->q_qinfo == &tcp_rinit); 9721 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9722 9723 if (flag & SO_SOCKSTR) { 9724 /* 9725 * No need to insert a socket in tcp acceptor hash. 9726 * If it was a socket acceptor stream, we dealt with 9727 * it above. A socket listener can never accept a 9728 * connection and doesn't need acceptor_id. 9729 */ 9730 connp->conn_flags |= IPCL_SOCKET; 9731 tcp->tcp_issocket = 1; 9732 WR(q)->q_qinfo = &tcp_sock_winit; 9733 } else { 9734 #ifdef _ILP32 9735 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9736 #else 9737 tcp->tcp_acceptor_id = conn_dev; 9738 #endif /* _ILP32 */ 9739 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9740 } 9741 9742 if (tcps->tcps_trace) 9743 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9744 9745 err = tcp_init(tcp, q); 9746 if (err != 0) { 9747 inet_minor_free(ip_minor_arena, connp->conn_dev); 9748 tcp_acceptor_hash_remove(tcp); 9749 CONN_DEC_REF(connp); 9750 q->q_ptr = WR(q)->q_ptr = NULL; 9751 return (err); 9752 } 9753 9754 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9755 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9756 9757 /* Non-zero default values */ 9758 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9759 /* 9760 * Put the ref for TCP. Ref for IP was already put 9761 * by ipcl_conn_create. Also Make the conn_t globally 9762 * visible to walkers 9763 */ 9764 mutex_enter(&connp->conn_lock); 9765 CONN_INC_REF_LOCKED(connp); 9766 ASSERT(connp->conn_ref == 2); 9767 connp->conn_state_flags &= ~CONN_INCIPIENT; 9768 mutex_exit(&connp->conn_lock); 9769 9770 qprocson(q); 9771 return (0); 9772 } 9773 9774 /* 9775 * Some TCP options can be "set" by requesting them in the option 9776 * buffer. This is needed for XTI feature test though we do not 9777 * allow it in general. We interpret that this mechanism is more 9778 * applicable to OSI protocols and need not be allowed in general. 9779 * This routine filters out options for which it is not allowed (most) 9780 * and lets through those (few) for which it is. [ The XTI interface 9781 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9782 * ever implemented will have to be allowed here ]. 9783 */ 9784 static boolean_t 9785 tcp_allow_connopt_set(int level, int name) 9786 { 9787 9788 switch (level) { 9789 case IPPROTO_TCP: 9790 switch (name) { 9791 case TCP_NODELAY: 9792 return (B_TRUE); 9793 default: 9794 return (B_FALSE); 9795 } 9796 /*NOTREACHED*/ 9797 default: 9798 return (B_FALSE); 9799 } 9800 /*NOTREACHED*/ 9801 } 9802 9803 /* 9804 * This routine gets default values of certain options whose default 9805 * values are maintained by protocol specific code 9806 */ 9807 /* ARGSUSED */ 9808 int 9809 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9810 { 9811 int32_t *i1 = (int32_t *)ptr; 9812 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9813 9814 switch (level) { 9815 case IPPROTO_TCP: 9816 switch (name) { 9817 case TCP_NOTIFY_THRESHOLD: 9818 *i1 = tcps->tcps_ip_notify_interval; 9819 break; 9820 case TCP_ABORT_THRESHOLD: 9821 *i1 = tcps->tcps_ip_abort_interval; 9822 break; 9823 case TCP_CONN_NOTIFY_THRESHOLD: 9824 *i1 = tcps->tcps_ip_notify_cinterval; 9825 break; 9826 case TCP_CONN_ABORT_THRESHOLD: 9827 *i1 = tcps->tcps_ip_abort_cinterval; 9828 break; 9829 default: 9830 return (-1); 9831 } 9832 break; 9833 case IPPROTO_IP: 9834 switch (name) { 9835 case IP_TTL: 9836 *i1 = tcps->tcps_ipv4_ttl; 9837 break; 9838 default: 9839 return (-1); 9840 } 9841 break; 9842 case IPPROTO_IPV6: 9843 switch (name) { 9844 case IPV6_UNICAST_HOPS: 9845 *i1 = tcps->tcps_ipv6_hoplimit; 9846 break; 9847 default: 9848 return (-1); 9849 } 9850 break; 9851 default: 9852 return (-1); 9853 } 9854 return (sizeof (int)); 9855 } 9856 9857 9858 /* 9859 * TCP routine to get the values of options. 9860 */ 9861 int 9862 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9863 { 9864 int *i1 = (int *)ptr; 9865 conn_t *connp = Q_TO_CONN(q); 9866 tcp_t *tcp = connp->conn_tcp; 9867 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9868 9869 switch (level) { 9870 case SOL_SOCKET: 9871 switch (name) { 9872 case SO_LINGER: { 9873 struct linger *lgr = (struct linger *)ptr; 9874 9875 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9876 lgr->l_linger = tcp->tcp_lingertime; 9877 } 9878 return (sizeof (struct linger)); 9879 case SO_DEBUG: 9880 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9881 break; 9882 case SO_KEEPALIVE: 9883 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9884 break; 9885 case SO_DONTROUTE: 9886 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9887 break; 9888 case SO_USELOOPBACK: 9889 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9890 break; 9891 case SO_BROADCAST: 9892 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9893 break; 9894 case SO_REUSEADDR: 9895 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9896 break; 9897 case SO_OOBINLINE: 9898 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9899 break; 9900 case SO_DGRAM_ERRIND: 9901 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9902 break; 9903 case SO_TYPE: 9904 *i1 = SOCK_STREAM; 9905 break; 9906 case SO_SNDBUF: 9907 *i1 = tcp->tcp_xmit_hiwater; 9908 break; 9909 case SO_RCVBUF: 9910 *i1 = RD(q)->q_hiwat; 9911 break; 9912 case SO_SND_COPYAVOID: 9913 *i1 = tcp->tcp_snd_zcopy_on ? 9914 SO_SND_COPYAVOID : 0; 9915 break; 9916 case SO_ALLZONES: 9917 *i1 = connp->conn_allzones ? 1 : 0; 9918 break; 9919 case SO_ANON_MLP: 9920 *i1 = connp->conn_anon_mlp; 9921 break; 9922 case SO_MAC_EXEMPT: 9923 *i1 = connp->conn_mac_exempt; 9924 break; 9925 case SO_EXCLBIND: 9926 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9927 break; 9928 case SO_PROTOTYPE: 9929 *i1 = IPPROTO_TCP; 9930 break; 9931 case SO_DOMAIN: 9932 *i1 = tcp->tcp_family; 9933 break; 9934 default: 9935 return (-1); 9936 } 9937 break; 9938 case IPPROTO_TCP: 9939 switch (name) { 9940 case TCP_NODELAY: 9941 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9942 break; 9943 case TCP_MAXSEG: 9944 *i1 = tcp->tcp_mss; 9945 break; 9946 case TCP_NOTIFY_THRESHOLD: 9947 *i1 = (int)tcp->tcp_first_timer_threshold; 9948 break; 9949 case TCP_ABORT_THRESHOLD: 9950 *i1 = tcp->tcp_second_timer_threshold; 9951 break; 9952 case TCP_CONN_NOTIFY_THRESHOLD: 9953 *i1 = tcp->tcp_first_ctimer_threshold; 9954 break; 9955 case TCP_CONN_ABORT_THRESHOLD: 9956 *i1 = tcp->tcp_second_ctimer_threshold; 9957 break; 9958 case TCP_RECVDSTADDR: 9959 *i1 = tcp->tcp_recvdstaddr; 9960 break; 9961 case TCP_ANONPRIVBIND: 9962 *i1 = tcp->tcp_anon_priv_bind; 9963 break; 9964 case TCP_EXCLBIND: 9965 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9966 break; 9967 case TCP_INIT_CWND: 9968 *i1 = tcp->tcp_init_cwnd; 9969 break; 9970 case TCP_KEEPALIVE_THRESHOLD: 9971 *i1 = tcp->tcp_ka_interval; 9972 break; 9973 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9974 *i1 = tcp->tcp_ka_abort_thres; 9975 break; 9976 case TCP_CORK: 9977 *i1 = tcp->tcp_cork; 9978 break; 9979 default: 9980 return (-1); 9981 } 9982 break; 9983 case IPPROTO_IP: 9984 if (tcp->tcp_family != AF_INET) 9985 return (-1); 9986 switch (name) { 9987 case IP_OPTIONS: 9988 case T_IP_OPTIONS: { 9989 /* 9990 * This is compatible with BSD in that in only return 9991 * the reverse source route with the final destination 9992 * as the last entry. The first 4 bytes of the option 9993 * will contain the final destination. 9994 */ 9995 int opt_len; 9996 9997 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9998 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9999 ASSERT(opt_len >= 0); 10000 /* Caller ensures enough space */ 10001 if (opt_len > 0) { 10002 /* 10003 * TODO: Do we have to handle getsockopt on an 10004 * initiator as well? 10005 */ 10006 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 10007 } 10008 return (0); 10009 } 10010 case IP_TOS: 10011 case T_IP_TOS: 10012 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10013 break; 10014 case IP_TTL: 10015 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10016 break; 10017 case IP_NEXTHOP: 10018 /* Handled at IP level */ 10019 return (-EINVAL); 10020 default: 10021 return (-1); 10022 } 10023 break; 10024 case IPPROTO_IPV6: 10025 /* 10026 * IPPROTO_IPV6 options are only supported for sockets 10027 * that are using IPv6 on the wire. 10028 */ 10029 if (tcp->tcp_ipversion != IPV6_VERSION) { 10030 return (-1); 10031 } 10032 switch (name) { 10033 case IPV6_UNICAST_HOPS: 10034 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10035 break; /* goto sizeof (int) option return */ 10036 case IPV6_BOUND_IF: 10037 /* Zero if not set */ 10038 *i1 = tcp->tcp_bound_if; 10039 break; /* goto sizeof (int) option return */ 10040 case IPV6_RECVPKTINFO: 10041 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10042 *i1 = 1; 10043 else 10044 *i1 = 0; 10045 break; /* goto sizeof (int) option return */ 10046 case IPV6_RECVTCLASS: 10047 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10048 *i1 = 1; 10049 else 10050 *i1 = 0; 10051 break; /* goto sizeof (int) option return */ 10052 case IPV6_RECVHOPLIMIT: 10053 if (tcp->tcp_ipv6_recvancillary & 10054 TCP_IPV6_RECVHOPLIMIT) 10055 *i1 = 1; 10056 else 10057 *i1 = 0; 10058 break; /* goto sizeof (int) option return */ 10059 case IPV6_RECVHOPOPTS: 10060 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10061 *i1 = 1; 10062 else 10063 *i1 = 0; 10064 break; /* goto sizeof (int) option return */ 10065 case IPV6_RECVDSTOPTS: 10066 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10067 *i1 = 1; 10068 else 10069 *i1 = 0; 10070 break; /* goto sizeof (int) option return */ 10071 case _OLD_IPV6_RECVDSTOPTS: 10072 if (tcp->tcp_ipv6_recvancillary & 10073 TCP_OLD_IPV6_RECVDSTOPTS) 10074 *i1 = 1; 10075 else 10076 *i1 = 0; 10077 break; /* goto sizeof (int) option return */ 10078 case IPV6_RECVRTHDR: 10079 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10080 *i1 = 1; 10081 else 10082 *i1 = 0; 10083 break; /* goto sizeof (int) option return */ 10084 case IPV6_RECVRTHDRDSTOPTS: 10085 if (tcp->tcp_ipv6_recvancillary & 10086 TCP_IPV6_RECVRTDSTOPTS) 10087 *i1 = 1; 10088 else 10089 *i1 = 0; 10090 break; /* goto sizeof (int) option return */ 10091 case IPV6_PKTINFO: { 10092 /* XXX assumes that caller has room for max size! */ 10093 struct in6_pktinfo *pkti; 10094 10095 pkti = (struct in6_pktinfo *)ptr; 10096 if (ipp->ipp_fields & IPPF_IFINDEX) 10097 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10098 else 10099 pkti->ipi6_ifindex = 0; 10100 if (ipp->ipp_fields & IPPF_ADDR) 10101 pkti->ipi6_addr = ipp->ipp_addr; 10102 else 10103 pkti->ipi6_addr = ipv6_all_zeros; 10104 return (sizeof (struct in6_pktinfo)); 10105 } 10106 case IPV6_TCLASS: 10107 if (ipp->ipp_fields & IPPF_TCLASS) 10108 *i1 = ipp->ipp_tclass; 10109 else 10110 *i1 = IPV6_FLOW_TCLASS( 10111 IPV6_DEFAULT_VERS_AND_FLOW); 10112 break; /* goto sizeof (int) option return */ 10113 case IPV6_NEXTHOP: { 10114 sin6_t *sin6 = (sin6_t *)ptr; 10115 10116 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10117 return (0); 10118 *sin6 = sin6_null; 10119 sin6->sin6_family = AF_INET6; 10120 sin6->sin6_addr = ipp->ipp_nexthop; 10121 return (sizeof (sin6_t)); 10122 } 10123 case IPV6_HOPOPTS: 10124 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10125 return (0); 10126 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10127 return (0); 10128 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10129 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10130 if (tcp->tcp_label_len > 0) { 10131 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10132 ptr[1] = (ipp->ipp_hopoptslen - 10133 tcp->tcp_label_len + 7) / 8 - 1; 10134 } 10135 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10136 case IPV6_RTHDRDSTOPTS: 10137 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10138 return (0); 10139 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10140 return (ipp->ipp_rtdstoptslen); 10141 case IPV6_RTHDR: 10142 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10143 return (0); 10144 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10145 return (ipp->ipp_rthdrlen); 10146 case IPV6_DSTOPTS: 10147 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10148 return (0); 10149 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10150 return (ipp->ipp_dstoptslen); 10151 case IPV6_SRC_PREFERENCES: 10152 return (ip6_get_src_preferences(connp, 10153 (uint32_t *)ptr)); 10154 case IPV6_PATHMTU: { 10155 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10156 10157 if (tcp->tcp_state < TCPS_ESTABLISHED) 10158 return (-1); 10159 10160 return (ip_fill_mtuinfo(&connp->conn_remv6, 10161 connp->conn_fport, mtuinfo, 10162 connp->conn_netstack)); 10163 } 10164 default: 10165 return (-1); 10166 } 10167 break; 10168 default: 10169 return (-1); 10170 } 10171 return (sizeof (int)); 10172 } 10173 10174 /* 10175 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10176 * Parameters are assumed to be verified by the caller. 10177 */ 10178 /* ARGSUSED */ 10179 int 10180 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10181 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10182 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10183 { 10184 conn_t *connp = Q_TO_CONN(q); 10185 tcp_t *tcp = connp->conn_tcp; 10186 int *i1 = (int *)invalp; 10187 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10188 boolean_t checkonly; 10189 int reterr; 10190 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10191 10192 switch (optset_context) { 10193 case SETFN_OPTCOM_CHECKONLY: 10194 checkonly = B_TRUE; 10195 /* 10196 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10197 * inlen != 0 implies value supplied and 10198 * we have to "pretend" to set it. 10199 * inlen == 0 implies that there is no 10200 * value part in T_CHECK request and just validation 10201 * done elsewhere should be enough, we just return here. 10202 */ 10203 if (inlen == 0) { 10204 *outlenp = 0; 10205 return (0); 10206 } 10207 break; 10208 case SETFN_OPTCOM_NEGOTIATE: 10209 checkonly = B_FALSE; 10210 break; 10211 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10212 case SETFN_CONN_NEGOTIATE: 10213 checkonly = B_FALSE; 10214 /* 10215 * Negotiating local and "association-related" options 10216 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10217 * primitives is allowed by XTI, but we choose 10218 * to not implement this style negotiation for Internet 10219 * protocols (We interpret it is a must for OSI world but 10220 * optional for Internet protocols) for all options. 10221 * [ Will do only for the few options that enable test 10222 * suites that our XTI implementation of this feature 10223 * works for transports that do allow it ] 10224 */ 10225 if (!tcp_allow_connopt_set(level, name)) { 10226 *outlenp = 0; 10227 return (EINVAL); 10228 } 10229 break; 10230 default: 10231 /* 10232 * We should never get here 10233 */ 10234 *outlenp = 0; 10235 return (EINVAL); 10236 } 10237 10238 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10239 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10240 10241 /* 10242 * For TCP, we should have no ancillary data sent down 10243 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10244 * has to be zero. 10245 */ 10246 ASSERT(thisdg_attrs == NULL); 10247 10248 /* 10249 * For fixed length options, no sanity check 10250 * of passed in length is done. It is assumed *_optcom_req() 10251 * routines do the right thing. 10252 */ 10253 10254 switch (level) { 10255 case SOL_SOCKET: 10256 switch (name) { 10257 case SO_LINGER: { 10258 struct linger *lgr = (struct linger *)invalp; 10259 10260 if (!checkonly) { 10261 if (lgr->l_onoff) { 10262 tcp->tcp_linger = 1; 10263 tcp->tcp_lingertime = lgr->l_linger; 10264 } else { 10265 tcp->tcp_linger = 0; 10266 tcp->tcp_lingertime = 0; 10267 } 10268 /* struct copy */ 10269 *(struct linger *)outvalp = *lgr; 10270 } else { 10271 if (!lgr->l_onoff) { 10272 ((struct linger *)outvalp)->l_onoff = 0; 10273 ((struct linger *)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)); 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 * Nor can it reassemble urgent pointers, so discard 12925 * if it's not the next segment expected. 12926 * 12927 * Otherwise, collapse chain into one mblk (discard if 12928 * that fails). This makes sure the headers, retransmitted 12929 * data, and new data all are in the same mblk. 12930 */ 12931 ASSERT(mp != NULL); 12932 if (tcp->tcp_listener || !pullupmsg(mp, -1)) { 12933 freemsg(mp); 12934 return; 12935 } 12936 /* Update pointers into message */ 12937 iphdr = rptr = mp->b_rptr; 12938 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12939 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12940 /* 12941 * Since we can't handle any data with this urgent 12942 * pointer that is out of sequence, we expunge 12943 * the data. This allows us to still register 12944 * the urgent mark and generate the M_PCSIG, 12945 * which we can do. 12946 */ 12947 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12948 seg_len = 0; 12949 } 12950 } 12951 12952 switch (tcp->tcp_state) { 12953 case TCPS_SYN_SENT: 12954 if (flags & TH_ACK) { 12955 /* 12956 * Note that our stack cannot send data before a 12957 * connection is established, therefore the 12958 * following check is valid. Otherwise, it has 12959 * to be changed. 12960 */ 12961 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12962 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12963 freemsg(mp); 12964 if (flags & TH_RST) 12965 return; 12966 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12967 tcp, seg_ack, 0, TH_RST); 12968 return; 12969 } 12970 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12971 } 12972 if (flags & TH_RST) { 12973 freemsg(mp); 12974 if (flags & TH_ACK) 12975 (void) tcp_clean_death(tcp, 12976 ECONNREFUSED, 13); 12977 return; 12978 } 12979 if (!(flags & TH_SYN)) { 12980 freemsg(mp); 12981 return; 12982 } 12983 12984 /* Process all TCP options. */ 12985 tcp_process_options(tcp, tcph); 12986 /* 12987 * The following changes our rwnd to be a multiple of the 12988 * MIN(peer MSS, our MSS) for performance reason. 12989 */ 12990 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 12991 tcp->tcp_mss)); 12992 12993 /* Is the other end ECN capable? */ 12994 if (tcp->tcp_ecn_ok) { 12995 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12996 tcp->tcp_ecn_ok = B_FALSE; 12997 } 12998 } 12999 /* 13000 * Clear ECN flags because it may interfere with later 13001 * processing. 13002 */ 13003 flags &= ~(TH_ECE|TH_CWR); 13004 13005 tcp->tcp_irs = seg_seq; 13006 tcp->tcp_rack = seg_seq; 13007 tcp->tcp_rnxt = seg_seq + 1; 13008 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13009 if (!TCP_IS_DETACHED(tcp)) { 13010 /* Allocate room for SACK options if needed. */ 13011 if (tcp->tcp_snd_sack_ok) { 13012 (void) mi_set_sth_wroff(tcp->tcp_rq, 13013 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13014 (tcp->tcp_loopback ? 0 : 13015 tcps->tcps_wroff_xtra)); 13016 } else { 13017 (void) mi_set_sth_wroff(tcp->tcp_rq, 13018 tcp->tcp_hdr_len + 13019 (tcp->tcp_loopback ? 0 : 13020 tcps->tcps_wroff_xtra)); 13021 } 13022 } 13023 if (flags & TH_ACK) { 13024 /* 13025 * If we can't get the confirmation upstream, pretend 13026 * we didn't even see this one. 13027 * 13028 * XXX: how can we pretend we didn't see it if we 13029 * have updated rnxt et. al. 13030 * 13031 * For loopback we defer sending up the T_CONN_CON 13032 * until after some checks below. 13033 */ 13034 mp1 = NULL; 13035 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13036 tcp->tcp_loopback ? &mp1 : NULL)) { 13037 freemsg(mp); 13038 return; 13039 } 13040 /* SYN was acked - making progress */ 13041 if (tcp->tcp_ipversion == IPV6_VERSION) 13042 tcp->tcp_ip_forward_progress = B_TRUE; 13043 13044 /* One for the SYN */ 13045 tcp->tcp_suna = tcp->tcp_iss + 1; 13046 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13047 tcp->tcp_state = TCPS_ESTABLISHED; 13048 13049 /* 13050 * If SYN was retransmitted, need to reset all 13051 * retransmission info. This is because this 13052 * segment will be treated as a dup ACK. 13053 */ 13054 if (tcp->tcp_rexmit) { 13055 tcp->tcp_rexmit = B_FALSE; 13056 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13057 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13058 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13059 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13060 tcp->tcp_ms_we_have_waited = 0; 13061 13062 /* 13063 * Set tcp_cwnd back to 1 MSS, per 13064 * recommendation from 13065 * draft-floyd-incr-init-win-01.txt, 13066 * Increasing TCP's Initial Window. 13067 */ 13068 tcp->tcp_cwnd = tcp->tcp_mss; 13069 } 13070 13071 tcp->tcp_swl1 = seg_seq; 13072 tcp->tcp_swl2 = seg_ack; 13073 13074 new_swnd = BE16_TO_U16(tcph->th_win); 13075 tcp->tcp_swnd = new_swnd; 13076 if (new_swnd > tcp->tcp_max_swnd) 13077 tcp->tcp_max_swnd = new_swnd; 13078 13079 /* 13080 * Always send the three-way handshake ack immediately 13081 * in order to make the connection complete as soon as 13082 * possible on the accepting host. 13083 */ 13084 flags |= TH_ACK_NEEDED; 13085 13086 /* 13087 * Special case for loopback. At this point we have 13088 * received SYN-ACK from the remote endpoint. In 13089 * order to ensure that both endpoints reach the 13090 * fused state prior to any data exchange, the final 13091 * ACK needs to be sent before we indicate T_CONN_CON 13092 * to the module upstream. 13093 */ 13094 if (tcp->tcp_loopback) { 13095 mblk_t *ack_mp; 13096 13097 ASSERT(!tcp->tcp_unfusable); 13098 ASSERT(mp1 != NULL); 13099 /* 13100 * For loopback, we always get a pure SYN-ACK 13101 * and only need to send back the final ACK 13102 * with no data (this is because the other 13103 * tcp is ours and we don't do T/TCP). This 13104 * final ACK triggers the passive side to 13105 * perform fusion in ESTABLISHED state. 13106 */ 13107 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13108 if (tcp->tcp_ack_tid != 0) { 13109 (void) TCP_TIMER_CANCEL(tcp, 13110 tcp->tcp_ack_tid); 13111 tcp->tcp_ack_tid = 0; 13112 } 13113 TCP_RECORD_TRACE(tcp, ack_mp, 13114 TCP_TRACE_SEND_PKT); 13115 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13116 BUMP_LOCAL(tcp->tcp_obsegs); 13117 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13118 13119 /* Send up T_CONN_CON */ 13120 putnext(tcp->tcp_rq, mp1); 13121 13122 freemsg(mp); 13123 return; 13124 } 13125 /* 13126 * Forget fusion; we need to handle more 13127 * complex cases below. Send the deferred 13128 * T_CONN_CON message upstream and proceed 13129 * as usual. Mark this tcp as not capable 13130 * of fusion. 13131 */ 13132 TCP_STAT(tcps, tcp_fusion_unfusable); 13133 tcp->tcp_unfusable = B_TRUE; 13134 putnext(tcp->tcp_rq, mp1); 13135 } 13136 13137 /* 13138 * Check to see if there is data to be sent. If 13139 * yes, set the transmit flag. Then check to see 13140 * if received data processing needs to be done. 13141 * If not, go straight to xmit_check. This short 13142 * cut is OK as we don't support T/TCP. 13143 */ 13144 if (tcp->tcp_unsent) 13145 flags |= TH_XMIT_NEEDED; 13146 13147 if (seg_len == 0 && !(flags & TH_URG)) { 13148 freemsg(mp); 13149 goto xmit_check; 13150 } 13151 13152 flags &= ~TH_SYN; 13153 seg_seq++; 13154 break; 13155 } 13156 tcp->tcp_state = TCPS_SYN_RCVD; 13157 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13158 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13159 if (mp1) { 13160 DB_CPID(mp1) = tcp->tcp_cpid; 13161 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 13162 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13163 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13164 } 13165 freemsg(mp); 13166 return; 13167 case TCPS_SYN_RCVD: 13168 if (flags & TH_ACK) { 13169 /* 13170 * In this state, a SYN|ACK packet is either bogus 13171 * because the other side must be ACKing our SYN which 13172 * indicates it has seen the ACK for their SYN and 13173 * shouldn't retransmit it or we're crossing SYNs 13174 * on active open. 13175 */ 13176 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13177 freemsg(mp); 13178 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13179 tcp, seg_ack, 0, TH_RST); 13180 return; 13181 } 13182 /* 13183 * NOTE: RFC 793 pg. 72 says this should be 13184 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13185 * but that would mean we have an ack that ignored 13186 * our SYN. 13187 */ 13188 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13189 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13190 freemsg(mp); 13191 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13192 tcp, seg_ack, 0, TH_RST); 13193 return; 13194 } 13195 } 13196 break; 13197 case TCPS_LISTEN: 13198 /* 13199 * Only a TLI listener can come through this path when a 13200 * acceptor is going back to be a listener and a packet 13201 * for the acceptor hits the classifier. For a socket 13202 * listener, this can never happen because a listener 13203 * can never accept connection on itself and hence a 13204 * socket acceptor can not go back to being a listener. 13205 */ 13206 ASSERT(!TCP_IS_SOCKET(tcp)); 13207 /*FALLTHRU*/ 13208 case TCPS_CLOSED: 13209 case TCPS_BOUND: { 13210 conn_t *new_connp; 13211 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13212 13213 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13214 if (new_connp != NULL) { 13215 tcp_reinput(new_connp, mp, connp->conn_sqp); 13216 return; 13217 } 13218 /* We failed to classify. For now just drop the packet */ 13219 freemsg(mp); 13220 return; 13221 } 13222 case TCPS_IDLE: 13223 /* 13224 * Handle the case where the tcp_clean_death() has happened 13225 * on a connection (application hasn't closed yet) but a packet 13226 * was already queued on squeue before tcp_clean_death() 13227 * was processed. Calling tcp_clean_death() twice on same 13228 * connection can result in weird behaviour. 13229 */ 13230 freemsg(mp); 13231 return; 13232 default: 13233 break; 13234 } 13235 13236 /* 13237 * Already on the correct queue/perimeter. 13238 * If this is a detached connection and not an eager 13239 * connection hanging off a listener then new data 13240 * (past the FIN) will cause a reset. 13241 * We do a special check here where it 13242 * is out of the main line, rather than check 13243 * if we are detached every time we see new 13244 * data down below. 13245 */ 13246 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13247 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13248 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13249 TCP_RECORD_TRACE(tcp, 13250 mp, TCP_TRACE_RECV_PKT); 13251 13252 freemsg(mp); 13253 /* 13254 * This could be an SSL closure alert. We're detached so just 13255 * acknowledge it this last time. 13256 */ 13257 if (tcp->tcp_kssl_ctx != NULL) { 13258 kssl_release_ctx(tcp->tcp_kssl_ctx); 13259 tcp->tcp_kssl_ctx = NULL; 13260 13261 tcp->tcp_rnxt += seg_len; 13262 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13263 flags |= TH_ACK_NEEDED; 13264 goto ack_check; 13265 } 13266 13267 tcp_xmit_ctl("new data when detached", tcp, 13268 tcp->tcp_snxt, 0, TH_RST); 13269 (void) tcp_clean_death(tcp, EPROTO, 12); 13270 return; 13271 } 13272 13273 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13274 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13275 new_swnd = BE16_TO_U16(tcph->th_win) << 13276 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13277 mss = tcp->tcp_mss; 13278 13279 if (tcp->tcp_snd_ts_ok) { 13280 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13281 /* 13282 * This segment is not acceptable. 13283 * Drop it and send back an ACK. 13284 */ 13285 freemsg(mp); 13286 flags |= TH_ACK_NEEDED; 13287 goto ack_check; 13288 } 13289 } else if (tcp->tcp_snd_sack_ok) { 13290 ASSERT(tcp->tcp_sack_info != NULL); 13291 tcpopt.tcp = tcp; 13292 /* 13293 * SACK info in already updated in tcp_parse_options. Ignore 13294 * all other TCP options... 13295 */ 13296 (void) tcp_parse_options(tcph, &tcpopt); 13297 } 13298 try_again:; 13299 gap = seg_seq - tcp->tcp_rnxt; 13300 rgap = tcp->tcp_rwnd - (gap + seg_len); 13301 /* 13302 * gap is the amount of sequence space between what we expect to see 13303 * and what we got for seg_seq. A positive value for gap means 13304 * something got lost. A negative value means we got some old stuff. 13305 */ 13306 if (gap < 0) { 13307 /* Old stuff present. Is the SYN in there? */ 13308 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13309 (seg_len != 0)) { 13310 flags &= ~TH_SYN; 13311 seg_seq++; 13312 urp--; 13313 /* Recompute the gaps after noting the SYN. */ 13314 goto try_again; 13315 } 13316 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13317 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13318 (seg_len > -gap ? -gap : seg_len)); 13319 /* Remove the old stuff from seg_len. */ 13320 seg_len += gap; 13321 /* 13322 * Anything left? 13323 * Make sure to check for unack'd FIN when rest of data 13324 * has been previously ack'd. 13325 */ 13326 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13327 /* 13328 * Resets are only valid if they lie within our offered 13329 * window. If the RST bit is set, we just ignore this 13330 * segment. 13331 */ 13332 if (flags & TH_RST) { 13333 freemsg(mp); 13334 return; 13335 } 13336 13337 /* 13338 * The arriving of dup data packets indicate that we 13339 * may have postponed an ack for too long, or the other 13340 * side's RTT estimate is out of shape. Start acking 13341 * more often. 13342 */ 13343 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13344 tcp->tcp_rack_cnt >= 1 && 13345 tcp->tcp_rack_abs_max > 2) { 13346 tcp->tcp_rack_abs_max--; 13347 } 13348 tcp->tcp_rack_cur_max = 1; 13349 13350 /* 13351 * This segment is "unacceptable". None of its 13352 * sequence space lies within our advertized window. 13353 * 13354 * Adjust seg_len to the original value for tracing. 13355 */ 13356 seg_len -= gap; 13357 if (tcp->tcp_debug) { 13358 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13359 "tcp_rput: unacceptable, gap %d, rgap %d, " 13360 "flags 0x%x, seg_seq %u, seg_ack %u, " 13361 "seg_len %d, rnxt %u, snxt %u, %s", 13362 gap, rgap, flags, seg_seq, seg_ack, 13363 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13364 tcp_display(tcp, NULL, 13365 DISP_ADDR_AND_PORT)); 13366 } 13367 13368 /* 13369 * Arrange to send an ACK in response to the 13370 * unacceptable segment per RFC 793 page 69. There 13371 * is only one small difference between ours and the 13372 * acceptability test in the RFC - we accept ACK-only 13373 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13374 * will be generated. 13375 * 13376 * Note that we have to ACK an ACK-only packet at least 13377 * for stacks that send 0-length keep-alives with 13378 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13379 * section 4.2.3.6. As long as we don't ever generate 13380 * an unacceptable packet in response to an incoming 13381 * packet that is unacceptable, it should not cause 13382 * "ACK wars". 13383 */ 13384 flags |= TH_ACK_NEEDED; 13385 13386 /* 13387 * Continue processing this segment in order to use the 13388 * ACK information it contains, but skip all other 13389 * sequence-number processing. Processing the ACK 13390 * information is necessary in order to 13391 * re-synchronize connections that may have lost 13392 * synchronization. 13393 * 13394 * We clear seg_len and flag fields related to 13395 * sequence number processing as they are not 13396 * to be trusted for an unacceptable segment. 13397 */ 13398 seg_len = 0; 13399 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13400 goto process_ack; 13401 } 13402 13403 /* Fix seg_seq, and chew the gap off the front. */ 13404 seg_seq = tcp->tcp_rnxt; 13405 urp += gap; 13406 do { 13407 mblk_t *mp2; 13408 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13409 (uintptr_t)UINT_MAX); 13410 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13411 if (gap > 0) { 13412 mp->b_rptr = mp->b_wptr - gap; 13413 break; 13414 } 13415 mp2 = mp; 13416 mp = mp->b_cont; 13417 freeb(mp2); 13418 } while (gap < 0); 13419 /* 13420 * If the urgent data has already been acknowledged, we 13421 * should ignore TH_URG below 13422 */ 13423 if (urp < 0) 13424 flags &= ~TH_URG; 13425 } 13426 /* 13427 * rgap is the amount of stuff received out of window. A negative 13428 * value is the amount out of window. 13429 */ 13430 if (rgap < 0) { 13431 mblk_t *mp2; 13432 13433 if (tcp->tcp_rwnd == 0) { 13434 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13435 } else { 13436 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13437 UPDATE_MIB(&tcps->tcps_mib, 13438 tcpInDataPastWinBytes, -rgap); 13439 } 13440 13441 /* 13442 * seg_len does not include the FIN, so if more than 13443 * just the FIN is out of window, we act like we don't 13444 * see it. (If just the FIN is out of window, rgap 13445 * will be zero and we will go ahead and acknowledge 13446 * the FIN.) 13447 */ 13448 flags &= ~TH_FIN; 13449 13450 /* Fix seg_len and make sure there is something left. */ 13451 seg_len += rgap; 13452 if (seg_len <= 0) { 13453 /* 13454 * Resets are only valid if they lie within our offered 13455 * window. If the RST bit is set, we just ignore this 13456 * segment. 13457 */ 13458 if (flags & TH_RST) { 13459 freemsg(mp); 13460 return; 13461 } 13462 13463 /* Per RFC 793, we need to send back an ACK. */ 13464 flags |= TH_ACK_NEEDED; 13465 13466 /* 13467 * Send SIGURG as soon as possible i.e. even 13468 * if the TH_URG was delivered in a window probe 13469 * packet (which will be unacceptable). 13470 * 13471 * We generate a signal if none has been generated 13472 * for this connection or if this is a new urgent 13473 * byte. Also send a zero-length "unmarked" message 13474 * to inform SIOCATMARK that this is not the mark. 13475 * 13476 * tcp_urp_last_valid is cleared when the T_exdata_ind 13477 * is sent up. This plus the check for old data 13478 * (gap >= 0) handles the wraparound of the sequence 13479 * number space without having to always track the 13480 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13481 * this max in its rcv_up variable). 13482 * 13483 * This prevents duplicate SIGURGS due to a "late" 13484 * zero-window probe when the T_EXDATA_IND has already 13485 * been sent up. 13486 */ 13487 if ((flags & TH_URG) && 13488 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13489 tcp->tcp_urp_last))) { 13490 mp1 = allocb(0, BPRI_MED); 13491 if (mp1 == NULL) { 13492 freemsg(mp); 13493 return; 13494 } 13495 if (!TCP_IS_DETACHED(tcp) && 13496 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13497 SIGURG)) { 13498 /* Try again on the rexmit. */ 13499 freemsg(mp1); 13500 freemsg(mp); 13501 return; 13502 } 13503 /* 13504 * If the next byte would be the mark 13505 * then mark with MARKNEXT else mark 13506 * with NOTMARKNEXT. 13507 */ 13508 if (gap == 0 && urp == 0) 13509 mp1->b_flag |= MSGMARKNEXT; 13510 else 13511 mp1->b_flag |= MSGNOTMARKNEXT; 13512 freemsg(tcp->tcp_urp_mark_mp); 13513 tcp->tcp_urp_mark_mp = mp1; 13514 flags |= TH_SEND_URP_MARK; 13515 tcp->tcp_urp_last_valid = B_TRUE; 13516 tcp->tcp_urp_last = urp + seg_seq; 13517 } 13518 /* 13519 * If this is a zero window probe, continue to 13520 * process the ACK part. But we need to set seg_len 13521 * to 0 to avoid data processing. Otherwise just 13522 * drop the segment and send back an ACK. 13523 */ 13524 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13525 flags &= ~(TH_SYN | TH_URG); 13526 seg_len = 0; 13527 goto process_ack; 13528 } else { 13529 freemsg(mp); 13530 goto ack_check; 13531 } 13532 } 13533 /* Pitch out of window stuff off the end. */ 13534 rgap = seg_len; 13535 mp2 = mp; 13536 do { 13537 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13538 (uintptr_t)INT_MAX); 13539 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13540 if (rgap < 0) { 13541 mp2->b_wptr += rgap; 13542 if ((mp1 = mp2->b_cont) != NULL) { 13543 mp2->b_cont = NULL; 13544 freemsg(mp1); 13545 } 13546 break; 13547 } 13548 } while ((mp2 = mp2->b_cont) != NULL); 13549 } 13550 ok:; 13551 /* 13552 * TCP should check ECN info for segments inside the window only. 13553 * Therefore the check should be done here. 13554 */ 13555 if (tcp->tcp_ecn_ok) { 13556 if (flags & TH_CWR) { 13557 tcp->tcp_ecn_echo_on = B_FALSE; 13558 } 13559 /* 13560 * Note that both ECN_CE and CWR can be set in the 13561 * same segment. In this case, we once again turn 13562 * on ECN_ECHO. 13563 */ 13564 if (tcp->tcp_ipversion == IPV4_VERSION) { 13565 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13566 13567 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13568 tcp->tcp_ecn_echo_on = B_TRUE; 13569 } 13570 } else { 13571 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13572 13573 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13574 htonl(IPH_ECN_CE << 20)) { 13575 tcp->tcp_ecn_echo_on = B_TRUE; 13576 } 13577 } 13578 } 13579 13580 /* 13581 * Check whether we can update tcp_ts_recent. This test is 13582 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13583 * Extensions for High Performance: An Update", Internet Draft. 13584 */ 13585 if (tcp->tcp_snd_ts_ok && 13586 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13587 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13588 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13589 tcp->tcp_last_rcv_lbolt = lbolt64; 13590 } 13591 13592 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13593 /* 13594 * FIN in an out of order segment. We record this in 13595 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13596 * Clear the FIN so that any check on FIN flag will fail. 13597 * Remember that FIN also counts in the sequence number 13598 * space. So we need to ack out of order FIN only segments. 13599 */ 13600 if (flags & TH_FIN) { 13601 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13602 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13603 flags &= ~TH_FIN; 13604 flags |= TH_ACK_NEEDED; 13605 } 13606 if (seg_len > 0) { 13607 /* Fill in the SACK blk list. */ 13608 if (tcp->tcp_snd_sack_ok) { 13609 ASSERT(tcp->tcp_sack_info != NULL); 13610 tcp_sack_insert(tcp->tcp_sack_list, 13611 seg_seq, seg_seq + seg_len, 13612 &(tcp->tcp_num_sack_blk)); 13613 } 13614 13615 /* 13616 * Attempt reassembly and see if we have something 13617 * ready to go. 13618 */ 13619 mp = tcp_reass(tcp, mp, seg_seq); 13620 /* Always ack out of order packets */ 13621 flags |= TH_ACK_NEEDED | TH_PUSH; 13622 if (mp) { 13623 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13624 (uintptr_t)INT_MAX); 13625 seg_len = mp->b_cont ? msgdsize(mp) : 13626 (int)(mp->b_wptr - mp->b_rptr); 13627 seg_seq = tcp->tcp_rnxt; 13628 /* 13629 * A gap is filled and the seq num and len 13630 * of the gap match that of a previously 13631 * received FIN, put the FIN flag back in. 13632 */ 13633 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13634 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13635 flags |= TH_FIN; 13636 tcp->tcp_valid_bits &= 13637 ~TCP_OFO_FIN_VALID; 13638 } 13639 } else { 13640 /* 13641 * Keep going even with NULL mp. 13642 * There may be a useful ACK or something else 13643 * we don't want to miss. 13644 * 13645 * But TCP should not perform fast retransmit 13646 * because of the ack number. TCP uses 13647 * seg_len == 0 to determine if it is a pure 13648 * ACK. And this is not a pure ACK. 13649 */ 13650 seg_len = 0; 13651 ofo_seg = B_TRUE; 13652 } 13653 } 13654 } else if (seg_len > 0) { 13655 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13656 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13657 /* 13658 * If an out of order FIN was received before, and the seq 13659 * num and len of the new segment match that of the FIN, 13660 * put the FIN flag back in. 13661 */ 13662 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13663 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13664 flags |= TH_FIN; 13665 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13666 } 13667 } 13668 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13669 if (flags & TH_RST) { 13670 freemsg(mp); 13671 switch (tcp->tcp_state) { 13672 case TCPS_SYN_RCVD: 13673 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13674 break; 13675 case TCPS_ESTABLISHED: 13676 case TCPS_FIN_WAIT_1: 13677 case TCPS_FIN_WAIT_2: 13678 case TCPS_CLOSE_WAIT: 13679 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13680 break; 13681 case TCPS_CLOSING: 13682 case TCPS_LAST_ACK: 13683 (void) tcp_clean_death(tcp, 0, 16); 13684 break; 13685 default: 13686 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13687 (void) tcp_clean_death(tcp, ENXIO, 17); 13688 break; 13689 } 13690 return; 13691 } 13692 if (flags & TH_SYN) { 13693 /* 13694 * See RFC 793, Page 71 13695 * 13696 * The seq number must be in the window as it should 13697 * be "fixed" above. If it is outside window, it should 13698 * be already rejected. Note that we allow seg_seq to be 13699 * rnxt + rwnd because we want to accept 0 window probe. 13700 */ 13701 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13702 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13703 freemsg(mp); 13704 /* 13705 * If the ACK flag is not set, just use our snxt as the 13706 * seq number of the RST segment. 13707 */ 13708 if (!(flags & TH_ACK)) { 13709 seg_ack = tcp->tcp_snxt; 13710 } 13711 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13712 TH_RST|TH_ACK); 13713 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13714 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13715 return; 13716 } 13717 /* 13718 * urp could be -1 when the urp field in the packet is 0 13719 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13720 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13721 */ 13722 if (flags & TH_URG && urp >= 0) { 13723 if (!tcp->tcp_urp_last_valid || 13724 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13725 /* 13726 * If we haven't generated the signal yet for this 13727 * urgent pointer value, do it now. Also, send up a 13728 * zero-length M_DATA indicating whether or not this is 13729 * the mark. The latter is not needed when a 13730 * T_EXDATA_IND is sent up. However, if there are 13731 * allocation failures this code relies on the sender 13732 * retransmitting and the socket code for determining 13733 * the mark should not block waiting for the peer to 13734 * transmit. Thus, for simplicity we always send up the 13735 * mark indication. 13736 */ 13737 mp1 = allocb(0, BPRI_MED); 13738 if (mp1 == NULL) { 13739 freemsg(mp); 13740 return; 13741 } 13742 if (!TCP_IS_DETACHED(tcp) && 13743 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13744 /* Try again on the rexmit. */ 13745 freemsg(mp1); 13746 freemsg(mp); 13747 return; 13748 } 13749 /* 13750 * Mark with NOTMARKNEXT for now. 13751 * The code below will change this to MARKNEXT 13752 * if we are at the mark. 13753 * 13754 * If there are allocation failures (e.g. in dupmsg 13755 * below) the next time tcp_rput_data sees the urgent 13756 * segment it will send up the MSG*MARKNEXT message. 13757 */ 13758 mp1->b_flag |= MSGNOTMARKNEXT; 13759 freemsg(tcp->tcp_urp_mark_mp); 13760 tcp->tcp_urp_mark_mp = mp1; 13761 flags |= TH_SEND_URP_MARK; 13762 #ifdef DEBUG 13763 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13764 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13765 "last %x, %s", 13766 seg_seq, urp, tcp->tcp_urp_last, 13767 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13768 #endif /* DEBUG */ 13769 tcp->tcp_urp_last_valid = B_TRUE; 13770 tcp->tcp_urp_last = urp + seg_seq; 13771 } else if (tcp->tcp_urp_mark_mp != NULL) { 13772 /* 13773 * An allocation failure prevented the previous 13774 * tcp_rput_data from sending up the allocated 13775 * MSG*MARKNEXT message - send it up this time 13776 * around. 13777 */ 13778 flags |= TH_SEND_URP_MARK; 13779 } 13780 13781 /* 13782 * If the urgent byte is in this segment, make sure that it is 13783 * all by itself. This makes it much easier to deal with the 13784 * possibility of an allocation failure on the T_exdata_ind. 13785 * Note that seg_len is the number of bytes in the segment, and 13786 * urp is the offset into the segment of the urgent byte. 13787 * urp < seg_len means that the urgent byte is in this segment. 13788 */ 13789 if (urp < seg_len) { 13790 if (seg_len != 1) { 13791 uint32_t tmp_rnxt; 13792 /* 13793 * Break it up and feed it back in. 13794 * Re-attach the IP header. 13795 */ 13796 mp->b_rptr = iphdr; 13797 if (urp > 0) { 13798 /* 13799 * There is stuff before the urgent 13800 * byte. 13801 */ 13802 mp1 = dupmsg(mp); 13803 if (!mp1) { 13804 /* 13805 * Trim from urgent byte on. 13806 * The rest will come back. 13807 */ 13808 (void) adjmsg(mp, 13809 urp - seg_len); 13810 tcp_rput_data(connp, 13811 mp, NULL); 13812 return; 13813 } 13814 (void) adjmsg(mp1, urp - seg_len); 13815 /* Feed this piece back in. */ 13816 tmp_rnxt = tcp->tcp_rnxt; 13817 tcp_rput_data(connp, mp1, NULL); 13818 /* 13819 * If the data passed back in was not 13820 * processed (ie: bad ACK) sending 13821 * the remainder back in will cause a 13822 * loop. In this case, drop the 13823 * packet and let the sender try 13824 * sending a good packet. 13825 */ 13826 if (tmp_rnxt == tcp->tcp_rnxt) { 13827 freemsg(mp); 13828 return; 13829 } 13830 } 13831 if (urp != seg_len - 1) { 13832 uint32_t tmp_rnxt; 13833 /* 13834 * There is stuff after the urgent 13835 * byte. 13836 */ 13837 mp1 = dupmsg(mp); 13838 if (!mp1) { 13839 /* 13840 * Trim everything beyond the 13841 * urgent byte. The rest will 13842 * come back. 13843 */ 13844 (void) adjmsg(mp, 13845 urp + 1 - seg_len); 13846 tcp_rput_data(connp, 13847 mp, NULL); 13848 return; 13849 } 13850 (void) adjmsg(mp1, urp + 1 - seg_len); 13851 tmp_rnxt = tcp->tcp_rnxt; 13852 tcp_rput_data(connp, mp1, NULL); 13853 /* 13854 * If the data passed back in was not 13855 * processed (ie: bad ACK) sending 13856 * the remainder back in will cause a 13857 * loop. In this case, drop the 13858 * packet and let the sender try 13859 * sending a good packet. 13860 */ 13861 if (tmp_rnxt == tcp->tcp_rnxt) { 13862 freemsg(mp); 13863 return; 13864 } 13865 } 13866 tcp_rput_data(connp, mp, NULL); 13867 return; 13868 } 13869 /* 13870 * This segment contains only the urgent byte. We 13871 * have to allocate the T_exdata_ind, if we can. 13872 */ 13873 if (!tcp->tcp_urp_mp) { 13874 struct T_exdata_ind *tei; 13875 mp1 = allocb(sizeof (struct T_exdata_ind), 13876 BPRI_MED); 13877 if (!mp1) { 13878 /* 13879 * Sigh... It'll be back. 13880 * Generate any MSG*MARK message now. 13881 */ 13882 freemsg(mp); 13883 seg_len = 0; 13884 if (flags & TH_SEND_URP_MARK) { 13885 13886 13887 ASSERT(tcp->tcp_urp_mark_mp); 13888 tcp->tcp_urp_mark_mp->b_flag &= 13889 ~MSGNOTMARKNEXT; 13890 tcp->tcp_urp_mark_mp->b_flag |= 13891 MSGMARKNEXT; 13892 } 13893 goto ack_check; 13894 } 13895 mp1->b_datap->db_type = M_PROTO; 13896 tei = (struct T_exdata_ind *)mp1->b_rptr; 13897 tei->PRIM_type = T_EXDATA_IND; 13898 tei->MORE_flag = 0; 13899 mp1->b_wptr = (uchar_t *)&tei[1]; 13900 tcp->tcp_urp_mp = mp1; 13901 #ifdef DEBUG 13902 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13903 "tcp_rput: allocated exdata_ind %s", 13904 tcp_display(tcp, NULL, 13905 DISP_PORT_ONLY)); 13906 #endif /* DEBUG */ 13907 /* 13908 * There is no need to send a separate MSG*MARK 13909 * message since the T_EXDATA_IND will be sent 13910 * now. 13911 */ 13912 flags &= ~TH_SEND_URP_MARK; 13913 freemsg(tcp->tcp_urp_mark_mp); 13914 tcp->tcp_urp_mark_mp = NULL; 13915 } 13916 /* 13917 * Now we are all set. On the next putnext upstream, 13918 * tcp_urp_mp will be non-NULL and will get prepended 13919 * to what has to be this piece containing the urgent 13920 * byte. If for any reason we abort this segment below, 13921 * if it comes back, we will have this ready, or it 13922 * will get blown off in close. 13923 */ 13924 } else if (urp == seg_len) { 13925 /* 13926 * The urgent byte is the next byte after this sequence 13927 * number. If there is data it is marked with 13928 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13929 * since it is not needed. Otherwise, if the code 13930 * above just allocated a zero-length tcp_urp_mark_mp 13931 * message, that message is tagged with MSGMARKNEXT. 13932 * Sending up these MSGMARKNEXT messages makes 13933 * SIOCATMARK work correctly even though 13934 * the T_EXDATA_IND will not be sent up until the 13935 * urgent byte arrives. 13936 */ 13937 if (seg_len != 0) { 13938 flags |= TH_MARKNEXT_NEEDED; 13939 freemsg(tcp->tcp_urp_mark_mp); 13940 tcp->tcp_urp_mark_mp = NULL; 13941 flags &= ~TH_SEND_URP_MARK; 13942 } else if (tcp->tcp_urp_mark_mp != NULL) { 13943 flags |= TH_SEND_URP_MARK; 13944 tcp->tcp_urp_mark_mp->b_flag &= 13945 ~MSGNOTMARKNEXT; 13946 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13947 } 13948 #ifdef DEBUG 13949 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13950 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13951 seg_len, flags, 13952 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13953 #endif /* DEBUG */ 13954 } else { 13955 /* Data left until we hit mark */ 13956 #ifdef DEBUG 13957 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13958 "tcp_rput: URP %d bytes left, %s", 13959 urp - seg_len, tcp_display(tcp, NULL, 13960 DISP_PORT_ONLY)); 13961 #endif /* DEBUG */ 13962 } 13963 } 13964 13965 process_ack: 13966 if (!(flags & TH_ACK)) { 13967 freemsg(mp); 13968 goto xmit_check; 13969 } 13970 } 13971 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13972 13973 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13974 tcp->tcp_ip_forward_progress = B_TRUE; 13975 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13976 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13977 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13978 /* 3-way handshake complete - pass up the T_CONN_IND */ 13979 tcp_t *listener = tcp->tcp_listener; 13980 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13981 13982 tcp->tcp_tconnind_started = B_TRUE; 13983 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13984 /* 13985 * We are here means eager is fine but it can 13986 * get a TH_RST at any point between now and till 13987 * accept completes and disappear. We need to 13988 * ensure that reference to eager is valid after 13989 * we get out of eager's perimeter. So we do 13990 * an extra refhold. 13991 */ 13992 CONN_INC_REF(connp); 13993 13994 /* 13995 * The listener also exists because of the refhold 13996 * done in tcp_conn_request. Its possible that it 13997 * might have closed. We will check that once we 13998 * get inside listeners context. 13999 */ 14000 CONN_INC_REF(listener->tcp_connp); 14001 if (listener->tcp_connp->conn_sqp == 14002 connp->conn_sqp) { 14003 tcp_send_conn_ind(listener->tcp_connp, mp, 14004 listener->tcp_connp->conn_sqp); 14005 CONN_DEC_REF(listener->tcp_connp); 14006 } else if (!tcp->tcp_loopback) { 14007 squeue_fill(listener->tcp_connp->conn_sqp, mp, 14008 tcp_send_conn_ind, 14009 listener->tcp_connp, SQTAG_TCP_CONN_IND); 14010 } else { 14011 squeue_enter(listener->tcp_connp->conn_sqp, mp, 14012 tcp_send_conn_ind, listener->tcp_connp, 14013 SQTAG_TCP_CONN_IND); 14014 } 14015 } 14016 14017 if (tcp->tcp_active_open) { 14018 /* 14019 * We are seeing the final ack in the three way 14020 * hand shake of a active open'ed connection 14021 * so we must send up a T_CONN_CON 14022 */ 14023 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14024 freemsg(mp); 14025 return; 14026 } 14027 /* 14028 * Don't fuse the loopback endpoints for 14029 * simultaneous active opens. 14030 */ 14031 if (tcp->tcp_loopback) { 14032 TCP_STAT(tcps, tcp_fusion_unfusable); 14033 tcp->tcp_unfusable = B_TRUE; 14034 } 14035 } 14036 14037 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14038 bytes_acked--; 14039 /* SYN was acked - making progress */ 14040 if (tcp->tcp_ipversion == IPV6_VERSION) 14041 tcp->tcp_ip_forward_progress = B_TRUE; 14042 14043 /* 14044 * If SYN was retransmitted, need to reset all 14045 * retransmission info as this segment will be 14046 * treated as a dup ACK. 14047 */ 14048 if (tcp->tcp_rexmit) { 14049 tcp->tcp_rexmit = B_FALSE; 14050 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14051 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14052 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14053 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14054 tcp->tcp_ms_we_have_waited = 0; 14055 tcp->tcp_cwnd = mss; 14056 } 14057 14058 /* 14059 * We set the send window to zero here. 14060 * This is needed if there is data to be 14061 * processed already on the queue. 14062 * Later (at swnd_update label), the 14063 * "new_swnd > tcp_swnd" condition is satisfied 14064 * the XMIT_NEEDED flag is set in the current 14065 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14066 * called if there is already data on queue in 14067 * this state. 14068 */ 14069 tcp->tcp_swnd = 0; 14070 14071 if (new_swnd > tcp->tcp_max_swnd) 14072 tcp->tcp_max_swnd = new_swnd; 14073 tcp->tcp_swl1 = seg_seq; 14074 tcp->tcp_swl2 = seg_ack; 14075 tcp->tcp_state = TCPS_ESTABLISHED; 14076 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14077 14078 /* Fuse when both sides are in ESTABLISHED state */ 14079 if (tcp->tcp_loopback && do_tcp_fusion) 14080 tcp_fuse(tcp, iphdr, tcph); 14081 14082 } 14083 /* This code follows 4.4BSD-Lite2 mostly. */ 14084 if (bytes_acked < 0) 14085 goto est; 14086 14087 /* 14088 * If TCP is ECN capable and the congestion experience bit is 14089 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14090 * done once per window (or more loosely, per RTT). 14091 */ 14092 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14093 tcp->tcp_cwr = B_FALSE; 14094 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14095 if (!tcp->tcp_cwr) { 14096 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14097 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14098 tcp->tcp_cwnd = npkt * mss; 14099 /* 14100 * If the cwnd is 0, use the timer to clock out 14101 * new segments. This is required by the ECN spec. 14102 */ 14103 if (npkt == 0) { 14104 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14105 /* 14106 * This makes sure that when the ACK comes 14107 * back, we will increase tcp_cwnd by 1 MSS. 14108 */ 14109 tcp->tcp_cwnd_cnt = 0; 14110 } 14111 tcp->tcp_cwr = B_TRUE; 14112 /* 14113 * This marks the end of the current window of in 14114 * flight data. That is why we don't use 14115 * tcp_suna + tcp_swnd. Only data in flight can 14116 * provide ECN info. 14117 */ 14118 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14119 tcp->tcp_ecn_cwr_sent = B_FALSE; 14120 } 14121 } 14122 14123 mp1 = tcp->tcp_xmit_head; 14124 if (bytes_acked == 0) { 14125 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14126 int dupack_cnt; 14127 14128 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14129 /* 14130 * Fast retransmit. When we have seen exactly three 14131 * identical ACKs while we have unacked data 14132 * outstanding we take it as a hint that our peer 14133 * dropped something. 14134 * 14135 * If TCP is retransmitting, don't do fast retransmit. 14136 */ 14137 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14138 ! tcp->tcp_rexmit) { 14139 /* Do Limited Transmit */ 14140 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14141 tcps->tcps_dupack_fast_retransmit) { 14142 /* 14143 * RFC 3042 14144 * 14145 * What we need to do is temporarily 14146 * increase tcp_cwnd so that new 14147 * data can be sent if it is allowed 14148 * by the receive window (tcp_rwnd). 14149 * tcp_wput_data() will take care of 14150 * the rest. 14151 * 14152 * If the connection is SACK capable, 14153 * only do limited xmit when there 14154 * is SACK info. 14155 * 14156 * Note how tcp_cwnd is incremented. 14157 * The first dup ACK will increase 14158 * it by 1 MSS. The second dup ACK 14159 * will increase it by 2 MSS. This 14160 * means that only 1 new segment will 14161 * be sent for each dup ACK. 14162 */ 14163 if (tcp->tcp_unsent > 0 && 14164 (!tcp->tcp_snd_sack_ok || 14165 (tcp->tcp_snd_sack_ok && 14166 tcp->tcp_notsack_list != NULL))) { 14167 tcp->tcp_cwnd += mss << 14168 (tcp->tcp_dupack_cnt - 1); 14169 flags |= TH_LIMIT_XMIT; 14170 } 14171 } else if (dupack_cnt == 14172 tcps->tcps_dupack_fast_retransmit) { 14173 14174 /* 14175 * If we have reduced tcp_ssthresh 14176 * because of ECN, do not reduce it again 14177 * unless it is already one window of data 14178 * away. After one window of data, tcp_cwr 14179 * should then be cleared. Note that 14180 * for non ECN capable connection, tcp_cwr 14181 * should always be false. 14182 * 14183 * Adjust cwnd since the duplicate 14184 * ack indicates that a packet was 14185 * dropped (due to congestion.) 14186 */ 14187 if (!tcp->tcp_cwr) { 14188 npkt = ((tcp->tcp_snxt - 14189 tcp->tcp_suna) >> 1) / mss; 14190 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14191 mss; 14192 tcp->tcp_cwnd = (npkt + 14193 tcp->tcp_dupack_cnt) * mss; 14194 } 14195 if (tcp->tcp_ecn_ok) { 14196 tcp->tcp_cwr = B_TRUE; 14197 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14198 tcp->tcp_ecn_cwr_sent = B_FALSE; 14199 } 14200 14201 /* 14202 * We do Hoe's algorithm. Refer to her 14203 * paper "Improving the Start-up Behavior 14204 * of a Congestion Control Scheme for TCP," 14205 * appeared in SIGCOMM'96. 14206 * 14207 * Save highest seq no we have sent so far. 14208 * Be careful about the invisible FIN byte. 14209 */ 14210 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14211 (tcp->tcp_unsent == 0)) { 14212 tcp->tcp_rexmit_max = tcp->tcp_fss; 14213 } else { 14214 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14215 } 14216 14217 /* 14218 * Do not allow bursty traffic during. 14219 * fast recovery. Refer to Fall and Floyd's 14220 * paper "Simulation-based Comparisons of 14221 * Tahoe, Reno and SACK TCP" (in CCR?) 14222 * This is a best current practise. 14223 */ 14224 tcp->tcp_snd_burst = TCP_CWND_SS; 14225 14226 /* 14227 * For SACK: 14228 * Calculate tcp_pipe, which is the 14229 * estimated number of bytes in 14230 * network. 14231 * 14232 * tcp_fack is the highest sack'ed seq num 14233 * TCP has received. 14234 * 14235 * tcp_pipe is explained in the above quoted 14236 * Fall and Floyd's paper. tcp_fack is 14237 * explained in Mathis and Mahdavi's 14238 * "Forward Acknowledgment: Refining TCP 14239 * Congestion Control" in SIGCOMM '96. 14240 */ 14241 if (tcp->tcp_snd_sack_ok) { 14242 ASSERT(tcp->tcp_sack_info != NULL); 14243 if (tcp->tcp_notsack_list != NULL) { 14244 tcp->tcp_pipe = tcp->tcp_snxt - 14245 tcp->tcp_fack; 14246 tcp->tcp_sack_snxt = seg_ack; 14247 flags |= TH_NEED_SACK_REXMIT; 14248 } else { 14249 /* 14250 * Always initialize tcp_pipe 14251 * even though we don't have 14252 * any SACK info. If later 14253 * we get SACK info and 14254 * tcp_pipe is not initialized, 14255 * funny things will happen. 14256 */ 14257 tcp->tcp_pipe = 14258 tcp->tcp_cwnd_ssthresh; 14259 } 14260 } else { 14261 flags |= TH_REXMIT_NEEDED; 14262 } /* tcp_snd_sack_ok */ 14263 14264 } else { 14265 /* 14266 * Here we perform congestion 14267 * avoidance, but NOT slow start. 14268 * This is known as the Fast 14269 * Recovery Algorithm. 14270 */ 14271 if (tcp->tcp_snd_sack_ok && 14272 tcp->tcp_notsack_list != NULL) { 14273 flags |= TH_NEED_SACK_REXMIT; 14274 tcp->tcp_pipe -= mss; 14275 if (tcp->tcp_pipe < 0) 14276 tcp->tcp_pipe = 0; 14277 } else { 14278 /* 14279 * We know that one more packet has 14280 * left the pipe thus we can update 14281 * cwnd. 14282 */ 14283 cwnd = tcp->tcp_cwnd + mss; 14284 if (cwnd > tcp->tcp_cwnd_max) 14285 cwnd = tcp->tcp_cwnd_max; 14286 tcp->tcp_cwnd = cwnd; 14287 if (tcp->tcp_unsent > 0) 14288 flags |= TH_XMIT_NEEDED; 14289 } 14290 } 14291 } 14292 } else if (tcp->tcp_zero_win_probe) { 14293 /* 14294 * If the window has opened, need to arrange 14295 * to send additional data. 14296 */ 14297 if (new_swnd != 0) { 14298 /* tcp_suna != tcp_snxt */ 14299 /* Packet contains a window update */ 14300 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14301 tcp->tcp_zero_win_probe = 0; 14302 tcp->tcp_timer_backoff = 0; 14303 tcp->tcp_ms_we_have_waited = 0; 14304 14305 /* 14306 * Transmit starting with tcp_suna since 14307 * the one byte probe is not ack'ed. 14308 * If TCP has sent more than one identical 14309 * probe, tcp_rexmit will be set. That means 14310 * tcp_ss_rexmit() will send out the one 14311 * byte along with new data. Otherwise, 14312 * fake the retransmission. 14313 */ 14314 flags |= TH_XMIT_NEEDED; 14315 if (!tcp->tcp_rexmit) { 14316 tcp->tcp_rexmit = B_TRUE; 14317 tcp->tcp_dupack_cnt = 0; 14318 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14319 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14320 } 14321 } 14322 } 14323 goto swnd_update; 14324 } 14325 14326 /* 14327 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14328 * If the ACK value acks something that we have not yet sent, it might 14329 * be an old duplicate segment. Send an ACK to re-synchronize the 14330 * other side. 14331 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14332 * state is handled above, so we can always just drop the segment and 14333 * send an ACK here. 14334 * 14335 * Should we send ACKs in response to ACK only segments? 14336 */ 14337 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14338 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14339 /* drop the received segment */ 14340 freemsg(mp); 14341 14342 /* 14343 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14344 * greater than 0, check if the number of such 14345 * bogus ACks is greater than that count. If yes, 14346 * don't send back any ACK. This prevents TCP from 14347 * getting into an ACK storm if somehow an attacker 14348 * successfully spoofs an acceptable segment to our 14349 * peer. 14350 */ 14351 if (tcp_drop_ack_unsent_cnt > 0 && 14352 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14353 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14354 return; 14355 } 14356 mp = tcp_ack_mp(tcp); 14357 if (mp != NULL) { 14358 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 14359 BUMP_LOCAL(tcp->tcp_obsegs); 14360 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14361 tcp_send_data(tcp, tcp->tcp_wq, mp); 14362 } 14363 return; 14364 } 14365 14366 /* 14367 * TCP gets a new ACK, update the notsack'ed list to delete those 14368 * blocks that are covered by this ACK. 14369 */ 14370 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14371 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14372 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14373 } 14374 14375 /* 14376 * If we got an ACK after fast retransmit, check to see 14377 * if it is a partial ACK. If it is not and the congestion 14378 * window was inflated to account for the other side's 14379 * cached packets, retract it. If it is, do Hoe's algorithm. 14380 */ 14381 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14382 ASSERT(tcp->tcp_rexmit == B_FALSE); 14383 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14384 tcp->tcp_dupack_cnt = 0; 14385 /* 14386 * Restore the orig tcp_cwnd_ssthresh after 14387 * fast retransmit phase. 14388 */ 14389 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14390 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14391 } 14392 tcp->tcp_rexmit_max = seg_ack; 14393 tcp->tcp_cwnd_cnt = 0; 14394 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14395 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14396 14397 /* 14398 * Remove all notsack info to avoid confusion with 14399 * the next fast retrasnmit/recovery phase. 14400 */ 14401 if (tcp->tcp_snd_sack_ok && 14402 tcp->tcp_notsack_list != NULL) { 14403 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14404 } 14405 } else { 14406 if (tcp->tcp_snd_sack_ok && 14407 tcp->tcp_notsack_list != NULL) { 14408 flags |= TH_NEED_SACK_REXMIT; 14409 tcp->tcp_pipe -= mss; 14410 if (tcp->tcp_pipe < 0) 14411 tcp->tcp_pipe = 0; 14412 } else { 14413 /* 14414 * Hoe's algorithm: 14415 * 14416 * Retransmit the unack'ed segment and 14417 * restart fast recovery. Note that we 14418 * need to scale back tcp_cwnd to the 14419 * original value when we started fast 14420 * recovery. This is to prevent overly 14421 * aggressive behaviour in sending new 14422 * segments. 14423 */ 14424 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14425 tcps->tcps_dupack_fast_retransmit * mss; 14426 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14427 flags |= TH_REXMIT_NEEDED; 14428 } 14429 } 14430 } else { 14431 tcp->tcp_dupack_cnt = 0; 14432 if (tcp->tcp_rexmit) { 14433 /* 14434 * TCP is retranmitting. If the ACK ack's all 14435 * outstanding data, update tcp_rexmit_max and 14436 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14437 * to the correct value. 14438 * 14439 * Note that SEQ_LEQ() is used. This is to avoid 14440 * unnecessary fast retransmit caused by dup ACKs 14441 * received when TCP does slow start retransmission 14442 * after a time out. During this phase, TCP may 14443 * send out segments which are already received. 14444 * This causes dup ACKs to be sent back. 14445 */ 14446 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14447 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14448 tcp->tcp_rexmit_nxt = seg_ack; 14449 } 14450 if (seg_ack != tcp->tcp_rexmit_max) { 14451 flags |= TH_XMIT_NEEDED; 14452 } 14453 } else { 14454 tcp->tcp_rexmit = B_FALSE; 14455 tcp->tcp_xmit_zc_clean = B_FALSE; 14456 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14457 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14458 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14459 } 14460 tcp->tcp_ms_we_have_waited = 0; 14461 } 14462 } 14463 14464 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14465 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14466 tcp->tcp_suna = seg_ack; 14467 if (tcp->tcp_zero_win_probe != 0) { 14468 tcp->tcp_zero_win_probe = 0; 14469 tcp->tcp_timer_backoff = 0; 14470 } 14471 14472 /* 14473 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14474 * Note that it cannot be the SYN being ack'ed. The code flow 14475 * will not reach here. 14476 */ 14477 if (mp1 == NULL) { 14478 goto fin_acked; 14479 } 14480 14481 /* 14482 * Update the congestion window. 14483 * 14484 * If TCP is not ECN capable or TCP is ECN capable but the 14485 * congestion experience bit is not set, increase the tcp_cwnd as 14486 * usual. 14487 */ 14488 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14489 cwnd = tcp->tcp_cwnd; 14490 add = mss; 14491 14492 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14493 /* 14494 * This is to prevent an increase of less than 1 MSS of 14495 * tcp_cwnd. With partial increase, tcp_wput_data() 14496 * may send out tinygrams in order to preserve mblk 14497 * boundaries. 14498 * 14499 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14500 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14501 * increased by 1 MSS for every RTTs. 14502 */ 14503 if (tcp->tcp_cwnd_cnt <= 0) { 14504 tcp->tcp_cwnd_cnt = cwnd + add; 14505 } else { 14506 tcp->tcp_cwnd_cnt -= add; 14507 add = 0; 14508 } 14509 } 14510 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14511 } 14512 14513 /* See if the latest urgent data has been acknowledged */ 14514 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14515 SEQ_GT(seg_ack, tcp->tcp_urg)) 14516 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14517 14518 /* Can we update the RTT estimates? */ 14519 if (tcp->tcp_snd_ts_ok) { 14520 /* Ignore zero timestamp echo-reply. */ 14521 if (tcpopt.tcp_opt_ts_ecr != 0) { 14522 tcp_set_rto(tcp, (int32_t)lbolt - 14523 (int32_t)tcpopt.tcp_opt_ts_ecr); 14524 } 14525 14526 /* If needed, restart the timer. */ 14527 if (tcp->tcp_set_timer == 1) { 14528 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14529 tcp->tcp_set_timer = 0; 14530 } 14531 /* 14532 * Update tcp_csuna in case the other side stops sending 14533 * us timestamps. 14534 */ 14535 tcp->tcp_csuna = tcp->tcp_snxt; 14536 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14537 /* 14538 * An ACK sequence we haven't seen before, so get the RTT 14539 * and update the RTO. But first check if the timestamp is 14540 * valid to use. 14541 */ 14542 if ((mp1->b_next != NULL) && 14543 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14544 tcp_set_rto(tcp, (int32_t)lbolt - 14545 (int32_t)(intptr_t)mp1->b_prev); 14546 else 14547 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14548 14549 /* Remeber the last sequence to be ACKed */ 14550 tcp->tcp_csuna = seg_ack; 14551 if (tcp->tcp_set_timer == 1) { 14552 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14553 tcp->tcp_set_timer = 0; 14554 } 14555 } else { 14556 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14557 } 14558 14559 /* Eat acknowledged bytes off the xmit queue. */ 14560 for (;;) { 14561 mblk_t *mp2; 14562 uchar_t *wptr; 14563 14564 wptr = mp1->b_wptr; 14565 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14566 bytes_acked -= (int)(wptr - mp1->b_rptr); 14567 if (bytes_acked < 0) { 14568 mp1->b_rptr = wptr + bytes_acked; 14569 /* 14570 * Set a new timestamp if all the bytes timed by the 14571 * old timestamp have been ack'ed. 14572 */ 14573 if (SEQ_GT(seg_ack, 14574 (uint32_t)(uintptr_t)(mp1->b_next))) { 14575 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14576 mp1->b_next = NULL; 14577 } 14578 break; 14579 } 14580 mp1->b_next = NULL; 14581 mp1->b_prev = NULL; 14582 mp2 = mp1; 14583 mp1 = mp1->b_cont; 14584 14585 /* 14586 * This notification is required for some zero-copy 14587 * clients to maintain a copy semantic. After the data 14588 * is ack'ed, client is safe to modify or reuse the buffer. 14589 */ 14590 if (tcp->tcp_snd_zcopy_aware && 14591 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14592 tcp_zcopy_notify(tcp); 14593 freeb(mp2); 14594 if (bytes_acked == 0) { 14595 if (mp1 == NULL) { 14596 /* Everything is ack'ed, clear the tail. */ 14597 tcp->tcp_xmit_tail = NULL; 14598 /* 14599 * Cancel the timer unless we are still 14600 * waiting for an ACK for the FIN packet. 14601 */ 14602 if (tcp->tcp_timer_tid != 0 && 14603 tcp->tcp_snxt == tcp->tcp_suna) { 14604 (void) TCP_TIMER_CANCEL(tcp, 14605 tcp->tcp_timer_tid); 14606 tcp->tcp_timer_tid = 0; 14607 } 14608 goto pre_swnd_update; 14609 } 14610 if (mp2 != tcp->tcp_xmit_tail) 14611 break; 14612 tcp->tcp_xmit_tail = mp1; 14613 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14614 (uintptr_t)INT_MAX); 14615 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14616 mp1->b_rptr); 14617 break; 14618 } 14619 if (mp1 == NULL) { 14620 /* 14621 * More was acked but there is nothing more 14622 * outstanding. This means that the FIN was 14623 * just acked or that we're talking to a clown. 14624 */ 14625 fin_acked: 14626 ASSERT(tcp->tcp_fin_sent); 14627 tcp->tcp_xmit_tail = NULL; 14628 if (tcp->tcp_fin_sent) { 14629 /* FIN was acked - making progress */ 14630 if (tcp->tcp_ipversion == IPV6_VERSION && 14631 !tcp->tcp_fin_acked) 14632 tcp->tcp_ip_forward_progress = B_TRUE; 14633 tcp->tcp_fin_acked = B_TRUE; 14634 if (tcp->tcp_linger_tid != 0 && 14635 TCP_TIMER_CANCEL(tcp, 14636 tcp->tcp_linger_tid) >= 0) { 14637 tcp_stop_lingering(tcp); 14638 } 14639 } else { 14640 /* 14641 * We should never get here because 14642 * we have already checked that the 14643 * number of bytes ack'ed should be 14644 * smaller than or equal to what we 14645 * have sent so far (it is the 14646 * acceptability check of the ACK). 14647 * We can only get here if the send 14648 * queue is corrupted. 14649 * 14650 * Terminate the connection and 14651 * panic the system. It is better 14652 * for us to panic instead of 14653 * continuing to avoid other disaster. 14654 */ 14655 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14656 tcp->tcp_rnxt, TH_RST|TH_ACK); 14657 panic("Memory corruption " 14658 "detected for connection %s.", 14659 tcp_display(tcp, NULL, 14660 DISP_ADDR_AND_PORT)); 14661 /*NOTREACHED*/ 14662 } 14663 goto pre_swnd_update; 14664 } 14665 ASSERT(mp2 != tcp->tcp_xmit_tail); 14666 } 14667 if (tcp->tcp_unsent) { 14668 flags |= TH_XMIT_NEEDED; 14669 } 14670 pre_swnd_update: 14671 tcp->tcp_xmit_head = mp1; 14672 swnd_update: 14673 /* 14674 * The following check is different from most other implementations. 14675 * For bi-directional transfer, when segments are dropped, the 14676 * "normal" check will not accept a window update in those 14677 * retransmitted segemnts. Failing to do that, TCP may send out 14678 * segments which are outside receiver's window. As TCP accepts 14679 * the ack in those retransmitted segments, if the window update in 14680 * the same segment is not accepted, TCP will incorrectly calculates 14681 * that it can send more segments. This can create a deadlock 14682 * with the receiver if its window becomes zero. 14683 */ 14684 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14685 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14686 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14687 /* 14688 * The criteria for update is: 14689 * 14690 * 1. the segment acknowledges some data. Or 14691 * 2. the segment is new, i.e. it has a higher seq num. Or 14692 * 3. the segment is not old and the advertised window is 14693 * larger than the previous advertised window. 14694 */ 14695 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14696 flags |= TH_XMIT_NEEDED; 14697 tcp->tcp_swnd = new_swnd; 14698 if (new_swnd > tcp->tcp_max_swnd) 14699 tcp->tcp_max_swnd = new_swnd; 14700 tcp->tcp_swl1 = seg_seq; 14701 tcp->tcp_swl2 = seg_ack; 14702 } 14703 est: 14704 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14705 14706 switch (tcp->tcp_state) { 14707 case TCPS_FIN_WAIT_1: 14708 if (tcp->tcp_fin_acked) { 14709 tcp->tcp_state = TCPS_FIN_WAIT_2; 14710 /* 14711 * We implement the non-standard BSD/SunOS 14712 * FIN_WAIT_2 flushing algorithm. 14713 * If there is no user attached to this 14714 * TCP endpoint, then this TCP struct 14715 * could hang around forever in FIN_WAIT_2 14716 * state if the peer forgets to send us 14717 * a FIN. To prevent this, we wait only 14718 * 2*MSL (a convenient time value) for 14719 * the FIN to arrive. If it doesn't show up, 14720 * we flush the TCP endpoint. This algorithm, 14721 * though a violation of RFC-793, has worked 14722 * for over 10 years in BSD systems. 14723 * Note: SunOS 4.x waits 675 seconds before 14724 * flushing the FIN_WAIT_2 connection. 14725 */ 14726 TCP_TIMER_RESTART(tcp, 14727 tcps->tcps_fin_wait_2_flush_interval); 14728 } 14729 break; 14730 case TCPS_FIN_WAIT_2: 14731 break; /* Shutdown hook? */ 14732 case TCPS_LAST_ACK: 14733 freemsg(mp); 14734 if (tcp->tcp_fin_acked) { 14735 (void) tcp_clean_death(tcp, 0, 19); 14736 return; 14737 } 14738 goto xmit_check; 14739 case TCPS_CLOSING: 14740 if (tcp->tcp_fin_acked) { 14741 tcp->tcp_state = TCPS_TIME_WAIT; 14742 /* 14743 * Unconditionally clear the exclusive binding 14744 * bit so this TIME-WAIT connection won't 14745 * interfere with new ones. 14746 */ 14747 tcp->tcp_exclbind = 0; 14748 if (!TCP_IS_DETACHED(tcp)) { 14749 TCP_TIMER_RESTART(tcp, 14750 tcps->tcps_time_wait_interval); 14751 } else { 14752 tcp_time_wait_append(tcp); 14753 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14754 } 14755 } 14756 /*FALLTHRU*/ 14757 case TCPS_CLOSE_WAIT: 14758 freemsg(mp); 14759 goto xmit_check; 14760 default: 14761 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14762 break; 14763 } 14764 } 14765 if (flags & TH_FIN) { 14766 /* Make sure we ack the fin */ 14767 flags |= TH_ACK_NEEDED; 14768 if (!tcp->tcp_fin_rcvd) { 14769 tcp->tcp_fin_rcvd = B_TRUE; 14770 tcp->tcp_rnxt++; 14771 tcph = tcp->tcp_tcph; 14772 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14773 14774 /* 14775 * Generate the ordrel_ind at the end unless we 14776 * are an eager guy. 14777 * In the eager case tcp_rsrv will do this when run 14778 * after tcp_accept is done. 14779 */ 14780 if (tcp->tcp_listener == NULL && 14781 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14782 flags |= TH_ORDREL_NEEDED; 14783 switch (tcp->tcp_state) { 14784 case TCPS_SYN_RCVD: 14785 case TCPS_ESTABLISHED: 14786 tcp->tcp_state = TCPS_CLOSE_WAIT; 14787 /* Keepalive? */ 14788 break; 14789 case TCPS_FIN_WAIT_1: 14790 if (!tcp->tcp_fin_acked) { 14791 tcp->tcp_state = TCPS_CLOSING; 14792 break; 14793 } 14794 /* FALLTHRU */ 14795 case TCPS_FIN_WAIT_2: 14796 tcp->tcp_state = TCPS_TIME_WAIT; 14797 /* 14798 * Unconditionally clear the exclusive binding 14799 * bit so this TIME-WAIT connection won't 14800 * interfere with new ones. 14801 */ 14802 tcp->tcp_exclbind = 0; 14803 if (!TCP_IS_DETACHED(tcp)) { 14804 TCP_TIMER_RESTART(tcp, 14805 tcps->tcps_time_wait_interval); 14806 } else { 14807 tcp_time_wait_append(tcp); 14808 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14809 } 14810 if (seg_len) { 14811 /* 14812 * implies data piggybacked on FIN. 14813 * break to handle data. 14814 */ 14815 break; 14816 } 14817 freemsg(mp); 14818 goto ack_check; 14819 } 14820 } 14821 } 14822 if (mp == NULL) 14823 goto xmit_check; 14824 if (seg_len == 0) { 14825 freemsg(mp); 14826 goto xmit_check; 14827 } 14828 if (mp->b_rptr == mp->b_wptr) { 14829 /* 14830 * The header has been consumed, so we remove the 14831 * zero-length mblk here. 14832 */ 14833 mp1 = mp; 14834 mp = mp->b_cont; 14835 freeb(mp1); 14836 } 14837 tcph = tcp->tcp_tcph; 14838 tcp->tcp_rack_cnt++; 14839 { 14840 uint32_t cur_max; 14841 14842 cur_max = tcp->tcp_rack_cur_max; 14843 if (tcp->tcp_rack_cnt >= cur_max) { 14844 /* 14845 * We have more unacked data than we should - send 14846 * an ACK now. 14847 */ 14848 flags |= TH_ACK_NEEDED; 14849 cur_max++; 14850 if (cur_max > tcp->tcp_rack_abs_max) 14851 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14852 else 14853 tcp->tcp_rack_cur_max = cur_max; 14854 } else if (TCP_IS_DETACHED(tcp)) { 14855 /* We don't have an ACK timer for detached TCP. */ 14856 flags |= TH_ACK_NEEDED; 14857 } else if (seg_len < mss) { 14858 /* 14859 * If we get a segment that is less than an mss, and we 14860 * already have unacknowledged data, and the amount 14861 * unacknowledged is not a multiple of mss, then we 14862 * better generate an ACK now. Otherwise, this may be 14863 * the tail piece of a transaction, and we would rather 14864 * wait for the response. 14865 */ 14866 uint32_t udif; 14867 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14868 (uintptr_t)INT_MAX); 14869 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14870 if (udif && (udif % mss)) 14871 flags |= TH_ACK_NEEDED; 14872 else 14873 flags |= TH_ACK_TIMER_NEEDED; 14874 } else { 14875 /* Start delayed ack timer */ 14876 flags |= TH_ACK_TIMER_NEEDED; 14877 } 14878 } 14879 tcp->tcp_rnxt += seg_len; 14880 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14881 14882 /* Update SACK list */ 14883 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14884 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14885 &(tcp->tcp_num_sack_blk)); 14886 } 14887 14888 if (tcp->tcp_urp_mp) { 14889 tcp->tcp_urp_mp->b_cont = mp; 14890 mp = tcp->tcp_urp_mp; 14891 tcp->tcp_urp_mp = NULL; 14892 /* Ready for a new signal. */ 14893 tcp->tcp_urp_last_valid = B_FALSE; 14894 #ifdef DEBUG 14895 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14896 "tcp_rput: sending exdata_ind %s", 14897 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14898 #endif /* DEBUG */ 14899 } 14900 14901 /* 14902 * Check for ancillary data changes compared to last segment. 14903 */ 14904 if (tcp->tcp_ipv6_recvancillary != 0) { 14905 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14906 if (mp == NULL) 14907 return; 14908 } 14909 14910 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14911 /* 14912 * Side queue inbound data until the accept happens. 14913 * tcp_accept/tcp_rput drains this when the accept happens. 14914 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14915 * T_EXDATA_IND) it is queued on b_next. 14916 * XXX Make urgent data use this. Requires: 14917 * Removing tcp_listener check for TH_URG 14918 * Making M_PCPROTO and MARK messages skip the eager case 14919 */ 14920 14921 if (tcp->tcp_kssl_pending) { 14922 tcp_kssl_input(tcp, mp); 14923 } else { 14924 tcp_rcv_enqueue(tcp, mp, seg_len); 14925 } 14926 } else { 14927 if (mp->b_datap->db_type != M_DATA || 14928 (flags & TH_MARKNEXT_NEEDED)) { 14929 if (tcp->tcp_rcv_list != NULL) { 14930 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14931 } 14932 ASSERT(tcp->tcp_rcv_list == NULL || 14933 tcp->tcp_fused_sigurg); 14934 if (flags & TH_MARKNEXT_NEEDED) { 14935 #ifdef DEBUG 14936 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14937 "tcp_rput: sending MSGMARKNEXT %s", 14938 tcp_display(tcp, NULL, 14939 DISP_PORT_ONLY)); 14940 #endif /* DEBUG */ 14941 mp->b_flag |= MSGMARKNEXT; 14942 flags &= ~TH_MARKNEXT_NEEDED; 14943 } 14944 14945 /* Does this need SSL processing first? */ 14946 if ((tcp->tcp_kssl_ctx != NULL) && 14947 (DB_TYPE(mp) == M_DATA)) { 14948 tcp_kssl_input(tcp, mp); 14949 } else { 14950 putnext(tcp->tcp_rq, mp); 14951 if (!canputnext(tcp->tcp_rq)) 14952 tcp->tcp_rwnd -= seg_len; 14953 } 14954 } else if ((flags & (TH_PUSH|TH_FIN)) || 14955 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 14956 if (tcp->tcp_rcv_list != NULL) { 14957 /* 14958 * Enqueue the new segment first and then 14959 * call tcp_rcv_drain() to send all data 14960 * up. The other way to do this is to 14961 * send all queued data up and then call 14962 * putnext() to send the new segment up. 14963 * This way can remove the else part later 14964 * on. 14965 * 14966 * We don't this to avoid one more call to 14967 * canputnext() as tcp_rcv_drain() needs to 14968 * call canputnext(). 14969 */ 14970 tcp_rcv_enqueue(tcp, mp, seg_len); 14971 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14972 } else { 14973 /* Does this need SSL processing first? */ 14974 if ((tcp->tcp_kssl_ctx != NULL) && 14975 (DB_TYPE(mp) == M_DATA)) { 14976 tcp_kssl_input(tcp, mp); 14977 } else { 14978 putnext(tcp->tcp_rq, mp); 14979 if (!canputnext(tcp->tcp_rq)) 14980 tcp->tcp_rwnd -= seg_len; 14981 } 14982 } 14983 } else { 14984 /* 14985 * Enqueue all packets when processing an mblk 14986 * from the co queue and also enqueue normal packets. 14987 */ 14988 tcp_rcv_enqueue(tcp, mp, seg_len); 14989 } 14990 /* 14991 * Make sure the timer is running if we have data waiting 14992 * for a push bit. This provides resiliency against 14993 * implementations that do not correctly generate push bits. 14994 */ 14995 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 14996 /* 14997 * The connection may be closed at this point, so don't 14998 * do anything for a detached tcp. 14999 */ 15000 if (!TCP_IS_DETACHED(tcp)) 15001 tcp->tcp_push_tid = TCP_TIMER(tcp, 15002 tcp_push_timer, 15003 MSEC_TO_TICK(tcps->tcps_push_timer_interval)); 15004 } 15005 } 15006 xmit_check: 15007 /* Is there anything left to do? */ 15008 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15009 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15010 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15011 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15012 goto done; 15013 15014 /* Any transmit work to do and a non-zero window? */ 15015 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15016 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15017 if (flags & TH_REXMIT_NEEDED) { 15018 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15019 15020 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15021 if (snd_size > mss) 15022 snd_size = mss; 15023 if (snd_size > tcp->tcp_swnd) 15024 snd_size = tcp->tcp_swnd; 15025 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15026 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15027 B_TRUE); 15028 15029 if (mp1 != NULL) { 15030 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15031 tcp->tcp_csuna = tcp->tcp_snxt; 15032 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15033 UPDATE_MIB(&tcps->tcps_mib, 15034 tcpRetransBytes, snd_size); 15035 TCP_RECORD_TRACE(tcp, mp1, 15036 TCP_TRACE_SEND_PKT); 15037 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15038 } 15039 } 15040 if (flags & TH_NEED_SACK_REXMIT) { 15041 tcp_sack_rxmit(tcp, &flags); 15042 } 15043 /* 15044 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15045 * out new segment. Note that tcp_rexmit should not be 15046 * set, otherwise TH_LIMIT_XMIT should not be set. 15047 */ 15048 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15049 if (!tcp->tcp_rexmit) { 15050 tcp_wput_data(tcp, NULL, B_FALSE); 15051 } else { 15052 tcp_ss_rexmit(tcp); 15053 } 15054 } 15055 /* 15056 * Adjust tcp_cwnd back to normal value after sending 15057 * new data segments. 15058 */ 15059 if (flags & TH_LIMIT_XMIT) { 15060 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15061 /* 15062 * This will restart the timer. Restarting the 15063 * timer is used to avoid a timeout before the 15064 * limited transmitted segment's ACK gets back. 15065 */ 15066 if (tcp->tcp_xmit_head != NULL) 15067 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15068 } 15069 15070 /* Anything more to do? */ 15071 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15072 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15073 goto done; 15074 } 15075 ack_check: 15076 if (flags & TH_SEND_URP_MARK) { 15077 ASSERT(tcp->tcp_urp_mark_mp); 15078 /* 15079 * Send up any queued data and then send the mark message 15080 */ 15081 if (tcp->tcp_rcv_list != NULL) { 15082 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15083 } 15084 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15085 15086 mp1 = tcp->tcp_urp_mark_mp; 15087 tcp->tcp_urp_mark_mp = NULL; 15088 #ifdef DEBUG 15089 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15090 "tcp_rput: sending zero-length %s %s", 15091 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15092 "MSGNOTMARKNEXT"), 15093 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15094 #endif /* DEBUG */ 15095 putnext(tcp->tcp_rq, mp1); 15096 flags &= ~TH_SEND_URP_MARK; 15097 } 15098 if (flags & TH_ACK_NEEDED) { 15099 /* 15100 * Time to send an ack for some reason. 15101 */ 15102 mp1 = tcp_ack_mp(tcp); 15103 15104 if (mp1 != NULL) { 15105 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 15106 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15107 BUMP_LOCAL(tcp->tcp_obsegs); 15108 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15109 } 15110 if (tcp->tcp_ack_tid != 0) { 15111 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15112 tcp->tcp_ack_tid = 0; 15113 } 15114 } 15115 if (flags & TH_ACK_TIMER_NEEDED) { 15116 /* 15117 * Arrange for deferred ACK or push wait timeout. 15118 * Start timer if it is not already running. 15119 */ 15120 if (tcp->tcp_ack_tid == 0) { 15121 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15122 MSEC_TO_TICK(tcp->tcp_localnet ? 15123 (clock_t)tcps->tcps_local_dack_interval : 15124 (clock_t)tcps->tcps_deferred_ack_interval)); 15125 } 15126 } 15127 if (flags & TH_ORDREL_NEEDED) { 15128 /* 15129 * Send up the ordrel_ind unless we are an eager guy. 15130 * In the eager case tcp_rsrv will do this when run 15131 * after tcp_accept is done. 15132 */ 15133 ASSERT(tcp->tcp_listener == NULL); 15134 if (tcp->tcp_rcv_list != NULL) { 15135 /* 15136 * Push any mblk(s) enqueued from co processing. 15137 */ 15138 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15139 } 15140 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15141 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 15142 tcp->tcp_ordrel_done = B_TRUE; 15143 putnext(tcp->tcp_rq, mp1); 15144 if (tcp->tcp_deferred_clean_death) { 15145 /* 15146 * tcp_clean_death was deferred 15147 * for T_ORDREL_IND - do it now 15148 */ 15149 (void) tcp_clean_death(tcp, 15150 tcp->tcp_client_errno, 20); 15151 tcp->tcp_deferred_clean_death = B_FALSE; 15152 } 15153 } else { 15154 /* 15155 * Run the orderly release in the 15156 * service routine. 15157 */ 15158 qenable(tcp->tcp_rq); 15159 /* 15160 * Caveat(XXX): The machine may be so 15161 * overloaded that tcp_rsrv() is not scheduled 15162 * until after the endpoint has transitioned 15163 * to TCPS_TIME_WAIT 15164 * and tcp_time_wait_interval expires. Then 15165 * tcp_timer() will blow away state in tcp_t 15166 * and T_ORDREL_IND will never be delivered 15167 * upstream. Unlikely but potentially 15168 * a problem. 15169 */ 15170 } 15171 } 15172 done: 15173 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15174 } 15175 15176 /* 15177 * This function does PAWS protection check. Returns B_TRUE if the 15178 * segment passes the PAWS test, else returns B_FALSE. 15179 */ 15180 boolean_t 15181 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15182 { 15183 uint8_t flags; 15184 int options; 15185 uint8_t *up; 15186 15187 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15188 /* 15189 * If timestamp option is aligned nicely, get values inline, 15190 * otherwise call general routine to parse. Only do that 15191 * if timestamp is the only option. 15192 */ 15193 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15194 TCPOPT_REAL_TS_LEN && 15195 OK_32PTR((up = ((uint8_t *)tcph) + 15196 TCP_MIN_HEADER_LENGTH)) && 15197 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15198 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15199 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15200 15201 options = TCP_OPT_TSTAMP_PRESENT; 15202 } else { 15203 if (tcp->tcp_snd_sack_ok) { 15204 tcpoptp->tcp = tcp; 15205 } else { 15206 tcpoptp->tcp = NULL; 15207 } 15208 options = tcp_parse_options(tcph, tcpoptp); 15209 } 15210 15211 if (options & TCP_OPT_TSTAMP_PRESENT) { 15212 /* 15213 * Do PAWS per RFC 1323 section 4.2. Accept RST 15214 * regardless of the timestamp, page 18 RFC 1323.bis. 15215 */ 15216 if ((flags & TH_RST) == 0 && 15217 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15218 tcp->tcp_ts_recent)) { 15219 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15220 PAWS_TIMEOUT)) { 15221 /* This segment is not acceptable. */ 15222 return (B_FALSE); 15223 } else { 15224 /* 15225 * Connection has been idle for 15226 * too long. Reset the timestamp 15227 * and assume the segment is valid. 15228 */ 15229 tcp->tcp_ts_recent = 15230 tcpoptp->tcp_opt_ts_val; 15231 } 15232 } 15233 } else { 15234 /* 15235 * If we don't get a timestamp on every packet, we 15236 * figure we can't really trust 'em, so we stop sending 15237 * and parsing them. 15238 */ 15239 tcp->tcp_snd_ts_ok = B_FALSE; 15240 15241 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15242 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15243 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15244 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN); 15245 if (tcp->tcp_snd_sack_ok) { 15246 ASSERT(tcp->tcp_sack_info != NULL); 15247 tcp->tcp_max_sack_blk = 4; 15248 } 15249 } 15250 return (B_TRUE); 15251 } 15252 15253 /* 15254 * Attach ancillary data to a received TCP segments for the 15255 * ancillary pieces requested by the application that are 15256 * different than they were in the previous data segment. 15257 * 15258 * Save the "current" values once memory allocation is ok so that 15259 * when memory allocation fails we can just wait for the next data segment. 15260 */ 15261 static mblk_t * 15262 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15263 { 15264 struct T_optdata_ind *todi; 15265 int optlen; 15266 uchar_t *optptr; 15267 struct T_opthdr *toh; 15268 uint_t addflag; /* Which pieces to add */ 15269 mblk_t *mp1; 15270 15271 optlen = 0; 15272 addflag = 0; 15273 /* If app asked for pktinfo and the index has changed ... */ 15274 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15275 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15276 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15277 optlen += sizeof (struct T_opthdr) + 15278 sizeof (struct in6_pktinfo); 15279 addflag |= TCP_IPV6_RECVPKTINFO; 15280 } 15281 /* If app asked for hoplimit and it has changed ... */ 15282 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15283 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15284 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15285 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15286 addflag |= TCP_IPV6_RECVHOPLIMIT; 15287 } 15288 /* If app asked for tclass and it has changed ... */ 15289 if ((ipp->ipp_fields & IPPF_TCLASS) && 15290 ipp->ipp_tclass != tcp->tcp_recvtclass && 15291 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15292 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15293 addflag |= TCP_IPV6_RECVTCLASS; 15294 } 15295 /* 15296 * If app asked for hopbyhop headers and it has changed ... 15297 * For security labels, note that (1) security labels can't change on 15298 * a connected socket at all, (2) we're connected to at most one peer, 15299 * (3) if anything changes, then it must be some other extra option. 15300 */ 15301 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15302 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15303 (ipp->ipp_fields & IPPF_HOPOPTS), 15304 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15305 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15306 tcp->tcp_label_len; 15307 addflag |= TCP_IPV6_RECVHOPOPTS; 15308 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15309 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15310 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15311 return (mp); 15312 } 15313 /* If app asked for dst headers before routing headers ... */ 15314 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15315 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15316 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15317 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15318 optlen += sizeof (struct T_opthdr) + 15319 ipp->ipp_rtdstoptslen; 15320 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15321 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15322 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15323 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15324 return (mp); 15325 } 15326 /* If app asked for routing headers and it has changed ... */ 15327 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15328 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15329 (ipp->ipp_fields & IPPF_RTHDR), 15330 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15331 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15332 addflag |= TCP_IPV6_RECVRTHDR; 15333 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15334 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15335 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15336 return (mp); 15337 } 15338 /* If app asked for dest headers and it has changed ... */ 15339 if ((tcp->tcp_ipv6_recvancillary & 15340 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15341 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15342 (ipp->ipp_fields & IPPF_DSTOPTS), 15343 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15344 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15345 addflag |= TCP_IPV6_RECVDSTOPTS; 15346 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15347 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15348 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15349 return (mp); 15350 } 15351 15352 if (optlen == 0) { 15353 /* Nothing to add */ 15354 return (mp); 15355 } 15356 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15357 if (mp1 == NULL) { 15358 /* 15359 * Defer sending ancillary data until the next TCP segment 15360 * arrives. 15361 */ 15362 return (mp); 15363 } 15364 mp1->b_cont = mp; 15365 mp = mp1; 15366 mp->b_wptr += sizeof (*todi) + optlen; 15367 mp->b_datap->db_type = M_PROTO; 15368 todi = (struct T_optdata_ind *)mp->b_rptr; 15369 todi->PRIM_type = T_OPTDATA_IND; 15370 todi->DATA_flag = 1; /* MORE data */ 15371 todi->OPT_length = optlen; 15372 todi->OPT_offset = sizeof (*todi); 15373 optptr = (uchar_t *)&todi[1]; 15374 /* 15375 * If app asked for pktinfo and the index has changed ... 15376 * Note that the local address never changes for the connection. 15377 */ 15378 if (addflag & TCP_IPV6_RECVPKTINFO) { 15379 struct in6_pktinfo *pkti; 15380 15381 toh = (struct T_opthdr *)optptr; 15382 toh->level = IPPROTO_IPV6; 15383 toh->name = IPV6_PKTINFO; 15384 toh->len = sizeof (*toh) + sizeof (*pkti); 15385 toh->status = 0; 15386 optptr += sizeof (*toh); 15387 pkti = (struct in6_pktinfo *)optptr; 15388 if (tcp->tcp_ipversion == IPV6_VERSION) 15389 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15390 else 15391 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15392 &pkti->ipi6_addr); 15393 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15394 optptr += sizeof (*pkti); 15395 ASSERT(OK_32PTR(optptr)); 15396 /* Save as "last" value */ 15397 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15398 } 15399 /* If app asked for hoplimit and it has changed ... */ 15400 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15401 toh = (struct T_opthdr *)optptr; 15402 toh->level = IPPROTO_IPV6; 15403 toh->name = IPV6_HOPLIMIT; 15404 toh->len = sizeof (*toh) + sizeof (uint_t); 15405 toh->status = 0; 15406 optptr += sizeof (*toh); 15407 *(uint_t *)optptr = ipp->ipp_hoplimit; 15408 optptr += sizeof (uint_t); 15409 ASSERT(OK_32PTR(optptr)); 15410 /* Save as "last" value */ 15411 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15412 } 15413 /* If app asked for tclass and it has changed ... */ 15414 if (addflag & TCP_IPV6_RECVTCLASS) { 15415 toh = (struct T_opthdr *)optptr; 15416 toh->level = IPPROTO_IPV6; 15417 toh->name = IPV6_TCLASS; 15418 toh->len = sizeof (*toh) + sizeof (uint_t); 15419 toh->status = 0; 15420 optptr += sizeof (*toh); 15421 *(uint_t *)optptr = ipp->ipp_tclass; 15422 optptr += sizeof (uint_t); 15423 ASSERT(OK_32PTR(optptr)); 15424 /* Save as "last" value */ 15425 tcp->tcp_recvtclass = ipp->ipp_tclass; 15426 } 15427 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15428 toh = (struct T_opthdr *)optptr; 15429 toh->level = IPPROTO_IPV6; 15430 toh->name = IPV6_HOPOPTS; 15431 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15432 tcp->tcp_label_len; 15433 toh->status = 0; 15434 optptr += sizeof (*toh); 15435 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15436 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15437 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15438 ASSERT(OK_32PTR(optptr)); 15439 /* Save as last value */ 15440 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15441 (ipp->ipp_fields & IPPF_HOPOPTS), 15442 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15443 } 15444 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15445 toh = (struct T_opthdr *)optptr; 15446 toh->level = IPPROTO_IPV6; 15447 toh->name = IPV6_RTHDRDSTOPTS; 15448 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15449 toh->status = 0; 15450 optptr += sizeof (*toh); 15451 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15452 optptr += ipp->ipp_rtdstoptslen; 15453 ASSERT(OK_32PTR(optptr)); 15454 /* Save as last value */ 15455 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15456 &tcp->tcp_rtdstoptslen, 15457 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15458 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15459 } 15460 if (addflag & TCP_IPV6_RECVRTHDR) { 15461 toh = (struct T_opthdr *)optptr; 15462 toh->level = IPPROTO_IPV6; 15463 toh->name = IPV6_RTHDR; 15464 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15465 toh->status = 0; 15466 optptr += sizeof (*toh); 15467 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15468 optptr += ipp->ipp_rthdrlen; 15469 ASSERT(OK_32PTR(optptr)); 15470 /* Save as last value */ 15471 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15472 (ipp->ipp_fields & IPPF_RTHDR), 15473 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15474 } 15475 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15476 toh = (struct T_opthdr *)optptr; 15477 toh->level = IPPROTO_IPV6; 15478 toh->name = IPV6_DSTOPTS; 15479 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15480 toh->status = 0; 15481 optptr += sizeof (*toh); 15482 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15483 optptr += ipp->ipp_dstoptslen; 15484 ASSERT(OK_32PTR(optptr)); 15485 /* Save as last value */ 15486 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15487 (ipp->ipp_fields & IPPF_DSTOPTS), 15488 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15489 } 15490 ASSERT(optptr == mp->b_wptr); 15491 return (mp); 15492 } 15493 15494 15495 /* 15496 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15497 * or a "bad" IRE detected by tcp_adapt_ire. 15498 * We can't tell if the failure was due to the laddr or the faddr 15499 * thus we clear out all addresses and ports. 15500 */ 15501 static void 15502 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15503 { 15504 queue_t *q = tcp->tcp_rq; 15505 tcph_t *tcph; 15506 struct T_error_ack *tea; 15507 conn_t *connp = tcp->tcp_connp; 15508 15509 15510 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15511 15512 if (mp->b_cont) { 15513 freemsg(mp->b_cont); 15514 mp->b_cont = NULL; 15515 } 15516 tea = (struct T_error_ack *)mp->b_rptr; 15517 switch (tea->PRIM_type) { 15518 case T_BIND_ACK: 15519 /* 15520 * Need to unbind with classifier since we were just told that 15521 * our bind succeeded. 15522 */ 15523 tcp->tcp_hard_bound = B_FALSE; 15524 tcp->tcp_hard_binding = B_FALSE; 15525 15526 ipcl_hash_remove(connp); 15527 /* Reuse the mblk if possible */ 15528 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15529 sizeof (*tea)); 15530 mp->b_rptr = mp->b_datap->db_base; 15531 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15532 tea = (struct T_error_ack *)mp->b_rptr; 15533 tea->PRIM_type = T_ERROR_ACK; 15534 tea->TLI_error = TSYSERR; 15535 tea->UNIX_error = error; 15536 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15537 tea->ERROR_prim = T_CONN_REQ; 15538 } else { 15539 tea->ERROR_prim = O_T_BIND_REQ; 15540 } 15541 break; 15542 15543 case T_ERROR_ACK: 15544 if (tcp->tcp_state >= TCPS_SYN_SENT) 15545 tea->ERROR_prim = T_CONN_REQ; 15546 break; 15547 default: 15548 panic("tcp_bind_failed: unexpected TPI type"); 15549 /*NOTREACHED*/ 15550 } 15551 15552 tcp->tcp_state = TCPS_IDLE; 15553 if (tcp->tcp_ipversion == IPV4_VERSION) 15554 tcp->tcp_ipha->ipha_src = 0; 15555 else 15556 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15557 /* 15558 * Copy of the src addr. in tcp_t is needed since 15559 * the lookup funcs. can only look at tcp_t 15560 */ 15561 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15562 15563 tcph = tcp->tcp_tcph; 15564 tcph->th_lport[0] = 0; 15565 tcph->th_lport[1] = 0; 15566 tcp_bind_hash_remove(tcp); 15567 bzero(&connp->u_port, sizeof (connp->u_port)); 15568 /* blow away saved option results if any */ 15569 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15570 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15571 15572 conn_delete_ire(tcp->tcp_connp, NULL); 15573 putnext(q, mp); 15574 } 15575 15576 /* 15577 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15578 * messages. 15579 */ 15580 void 15581 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15582 { 15583 mblk_t *mp1; 15584 uchar_t *rptr = mp->b_rptr; 15585 queue_t *q = tcp->tcp_rq; 15586 struct T_error_ack *tea; 15587 uint32_t mss; 15588 mblk_t *syn_mp; 15589 mblk_t *mdti; 15590 mblk_t *lsoi; 15591 int retval; 15592 mblk_t *ire_mp; 15593 tcp_stack_t *tcps = tcp->tcp_tcps; 15594 15595 switch (mp->b_datap->db_type) { 15596 case M_PROTO: 15597 case M_PCPROTO: 15598 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15599 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15600 break; 15601 tea = (struct T_error_ack *)rptr; 15602 switch (tea->PRIM_type) { 15603 case T_BIND_ACK: 15604 /* 15605 * Adapt Multidata information, if any. The 15606 * following tcp_mdt_update routine will free 15607 * the message. 15608 */ 15609 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15610 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15611 b_rptr)->mdt_capab, B_TRUE); 15612 freemsg(mdti); 15613 } 15614 15615 /* 15616 * Check to update LSO information with tcp, and 15617 * tcp_lso_update routine will free the message. 15618 */ 15619 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 15620 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 15621 b_rptr)->lso_capab); 15622 freemsg(lsoi); 15623 } 15624 15625 /* Get the IRE, if we had requested for it */ 15626 ire_mp = tcp_ire_mp(mp); 15627 15628 if (tcp->tcp_hard_binding) { 15629 tcp->tcp_hard_binding = B_FALSE; 15630 tcp->tcp_hard_bound = B_TRUE; 15631 CL_INET_CONNECT(tcp); 15632 } else { 15633 if (ire_mp != NULL) 15634 freeb(ire_mp); 15635 goto after_syn_sent; 15636 } 15637 15638 retval = tcp_adapt_ire(tcp, ire_mp); 15639 if (ire_mp != NULL) 15640 freeb(ire_mp); 15641 if (retval == 0) { 15642 tcp_bind_failed(tcp, mp, 15643 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15644 ENETUNREACH : EADDRNOTAVAIL)); 15645 return; 15646 } 15647 /* 15648 * Don't let an endpoint connect to itself. 15649 * Also checked in tcp_connect() but that 15650 * check can't handle the case when the 15651 * local IP address is INADDR_ANY. 15652 */ 15653 if (tcp->tcp_ipversion == IPV4_VERSION) { 15654 if ((tcp->tcp_ipha->ipha_dst == 15655 tcp->tcp_ipha->ipha_src) && 15656 (BE16_EQL(tcp->tcp_tcph->th_lport, 15657 tcp->tcp_tcph->th_fport))) { 15658 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15659 return; 15660 } 15661 } else { 15662 if (IN6_ARE_ADDR_EQUAL( 15663 &tcp->tcp_ip6h->ip6_dst, 15664 &tcp->tcp_ip6h->ip6_src) && 15665 (BE16_EQL(tcp->tcp_tcph->th_lport, 15666 tcp->tcp_tcph->th_fport))) { 15667 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15668 return; 15669 } 15670 } 15671 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15672 /* 15673 * This should not be possible! Just for 15674 * defensive coding... 15675 */ 15676 if (tcp->tcp_state != TCPS_SYN_SENT) 15677 goto after_syn_sent; 15678 15679 if (is_system_labeled() && 15680 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 15681 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 15682 return; 15683 } 15684 15685 ASSERT(q == tcp->tcp_rq); 15686 /* 15687 * tcp_adapt_ire() does not adjust 15688 * for TCP/IP header length. 15689 */ 15690 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15691 15692 /* 15693 * Just make sure our rwnd is at 15694 * least tcp_recv_hiwat_mss * MSS 15695 * large, and round up to the nearest 15696 * MSS. 15697 * 15698 * We do the round up here because 15699 * we need to get the interface 15700 * MTU first before we can do the 15701 * round up. 15702 */ 15703 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15704 tcps->tcps_recv_hiwat_minmss * mss); 15705 q->q_hiwat = tcp->tcp_rwnd; 15706 tcp_set_ws_value(tcp); 15707 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15708 tcp->tcp_tcph->th_win); 15709 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 15710 tcp->tcp_snd_ws_ok = B_TRUE; 15711 15712 /* 15713 * Set tcp_snd_ts_ok to true 15714 * so that tcp_xmit_mp will 15715 * include the timestamp 15716 * option in the SYN segment. 15717 */ 15718 if (tcps->tcps_tstamp_always || 15719 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 15720 tcp->tcp_snd_ts_ok = B_TRUE; 15721 } 15722 15723 /* 15724 * tcp_snd_sack_ok can be set in 15725 * tcp_adapt_ire() if the sack metric 15726 * is set. So check it here also. 15727 */ 15728 if (tcps->tcps_sack_permitted == 2 || 15729 tcp->tcp_snd_sack_ok) { 15730 if (tcp->tcp_sack_info == NULL) { 15731 tcp->tcp_sack_info = 15732 kmem_cache_alloc(tcp_sack_info_cache, 15733 KM_SLEEP); 15734 } 15735 tcp->tcp_snd_sack_ok = B_TRUE; 15736 } 15737 15738 /* 15739 * Should we use ECN? Note that the current 15740 * default value (SunOS 5.9) of tcp_ecn_permitted 15741 * is 1. The reason for doing this is that there 15742 * are equipments out there that will drop ECN 15743 * enabled IP packets. Setting it to 1 avoids 15744 * compatibility problems. 15745 */ 15746 if (tcps->tcps_ecn_permitted == 2) 15747 tcp->tcp_ecn_ok = B_TRUE; 15748 15749 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15750 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15751 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15752 if (syn_mp) { 15753 cred_t *cr; 15754 pid_t pid; 15755 15756 /* 15757 * Obtain the credential from the 15758 * thread calling connect(); the credential 15759 * lives on in the second mblk which 15760 * originated from T_CONN_REQ and is echoed 15761 * with the T_BIND_ACK from ip. If none 15762 * can be found, default to the creator 15763 * of the socket. 15764 */ 15765 if (mp->b_cont == NULL || 15766 (cr = DB_CRED(mp->b_cont)) == NULL) { 15767 cr = tcp->tcp_cred; 15768 pid = tcp->tcp_cpid; 15769 } else { 15770 pid = DB_CPID(mp->b_cont); 15771 } 15772 15773 TCP_RECORD_TRACE(tcp, syn_mp, 15774 TCP_TRACE_SEND_PKT); 15775 mblk_setcred(syn_mp, cr); 15776 DB_CPID(syn_mp) = pid; 15777 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15778 } 15779 after_syn_sent: 15780 /* 15781 * A trailer mblk indicates a waiting client upstream. 15782 * We complete here the processing begun in 15783 * either tcp_bind() or tcp_connect() by passing 15784 * upstream the reply message they supplied. 15785 */ 15786 mp1 = mp; 15787 mp = mp->b_cont; 15788 freeb(mp1); 15789 if (mp) 15790 break; 15791 return; 15792 case T_ERROR_ACK: 15793 if (tcp->tcp_debug) { 15794 (void) strlog(TCP_MOD_ID, 0, 1, 15795 SL_TRACE|SL_ERROR, 15796 "tcp_rput_other: case T_ERROR_ACK, " 15797 "ERROR_prim == %d", 15798 tea->ERROR_prim); 15799 } 15800 switch (tea->ERROR_prim) { 15801 case O_T_BIND_REQ: 15802 case T_BIND_REQ: 15803 tcp_bind_failed(tcp, mp, 15804 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15805 ENETUNREACH : EADDRNOTAVAIL)); 15806 return; 15807 case T_UNBIND_REQ: 15808 tcp->tcp_hard_binding = B_FALSE; 15809 tcp->tcp_hard_bound = B_FALSE; 15810 if (mp->b_cont) { 15811 freemsg(mp->b_cont); 15812 mp->b_cont = NULL; 15813 } 15814 if (tcp->tcp_unbind_pending) 15815 tcp->tcp_unbind_pending = 0; 15816 else { 15817 /* From tcp_ip_unbind() - free */ 15818 freemsg(mp); 15819 return; 15820 } 15821 break; 15822 case T_SVR4_OPTMGMT_REQ: 15823 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15824 /* T_OPTMGMT_REQ generated by TCP */ 15825 printf("T_SVR4_OPTMGMT_REQ failed " 15826 "%d/%d - dropped (cnt %d)\n", 15827 tea->TLI_error, tea->UNIX_error, 15828 tcp->tcp_drop_opt_ack_cnt); 15829 freemsg(mp); 15830 tcp->tcp_drop_opt_ack_cnt--; 15831 return; 15832 } 15833 break; 15834 } 15835 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15836 tcp->tcp_drop_opt_ack_cnt > 0) { 15837 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15838 "- dropped (cnt %d)\n", 15839 tea->TLI_error, tea->UNIX_error, 15840 tcp->tcp_drop_opt_ack_cnt); 15841 freemsg(mp); 15842 tcp->tcp_drop_opt_ack_cnt--; 15843 return; 15844 } 15845 break; 15846 case T_OPTMGMT_ACK: 15847 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15848 /* T_OPTMGMT_REQ generated by TCP */ 15849 freemsg(mp); 15850 tcp->tcp_drop_opt_ack_cnt--; 15851 return; 15852 } 15853 break; 15854 default: 15855 break; 15856 } 15857 break; 15858 case M_FLUSH: 15859 if (*rptr & FLUSHR) 15860 flushq(q, FLUSHDATA); 15861 break; 15862 default: 15863 /* M_CTL will be directly sent to tcp_icmp_error() */ 15864 ASSERT(DB_TYPE(mp) != M_CTL); 15865 break; 15866 } 15867 /* 15868 * Make sure we set this bit before sending the ACK for 15869 * bind. Otherwise accept could possibly run and free 15870 * this tcp struct. 15871 */ 15872 putnext(q, mp); 15873 } 15874 15875 /* 15876 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15877 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15878 * tcp_rsrv() try again. 15879 */ 15880 static void 15881 tcp_ordrel_kick(void *arg) 15882 { 15883 conn_t *connp = (conn_t *)arg; 15884 tcp_t *tcp = connp->conn_tcp; 15885 15886 tcp->tcp_ordrelid = 0; 15887 tcp->tcp_timeout = B_FALSE; 15888 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15889 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15890 qenable(tcp->tcp_rq); 15891 } 15892 } 15893 15894 /* ARGSUSED */ 15895 static void 15896 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15897 { 15898 conn_t *connp = (conn_t *)arg; 15899 tcp_t *tcp = connp->conn_tcp; 15900 queue_t *q = tcp->tcp_rq; 15901 uint_t thwin; 15902 tcp_stack_t *tcps = tcp->tcp_tcps; 15903 15904 freeb(mp); 15905 15906 TCP_STAT(tcps, tcp_rsrv_calls); 15907 15908 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15909 return; 15910 } 15911 15912 if (tcp->tcp_fused) { 15913 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15914 15915 ASSERT(tcp->tcp_fused); 15916 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15917 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15918 ASSERT(!TCP_IS_DETACHED(tcp)); 15919 ASSERT(tcp->tcp_connp->conn_sqp == 15920 peer_tcp->tcp_connp->conn_sqp); 15921 15922 /* 15923 * Normally we would not get backenabled in synchronous 15924 * streams mode, but in case this happens, we need to plug 15925 * synchronous streams during our drain to prevent a race 15926 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15927 */ 15928 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15929 if (tcp->tcp_rcv_list != NULL) 15930 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15931 15932 if (peer_tcp > tcp) { 15933 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15934 mutex_enter(&tcp->tcp_non_sq_lock); 15935 } else { 15936 mutex_enter(&tcp->tcp_non_sq_lock); 15937 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15938 } 15939 15940 if (peer_tcp->tcp_flow_stopped && 15941 (TCP_UNSENT_BYTES(peer_tcp) <= 15942 peer_tcp->tcp_xmit_lowater)) { 15943 tcp_clrqfull(peer_tcp); 15944 } 15945 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15946 mutex_exit(&tcp->tcp_non_sq_lock); 15947 15948 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15949 TCP_STAT(tcps, tcp_fusion_backenabled); 15950 return; 15951 } 15952 15953 if (canputnext(q)) { 15954 tcp->tcp_rwnd = q->q_hiwat; 15955 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15956 << tcp->tcp_rcv_ws; 15957 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15958 /* 15959 * Send back a window update immediately if TCP is above 15960 * ESTABLISHED state and the increase of the rcv window 15961 * that the other side knows is at least 1 MSS after flow 15962 * control is lifted. 15963 */ 15964 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15965 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15966 tcp_xmit_ctl(NULL, tcp, 15967 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15968 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15969 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 15970 } 15971 } 15972 /* Handle a failure to allocate a T_ORDREL_IND here */ 15973 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15974 ASSERT(tcp->tcp_listener == NULL); 15975 if (tcp->tcp_rcv_list != NULL) { 15976 (void) tcp_rcv_drain(q, tcp); 15977 } 15978 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15979 mp = mi_tpi_ordrel_ind(); 15980 if (mp) { 15981 tcp->tcp_ordrel_done = B_TRUE; 15982 putnext(q, mp); 15983 if (tcp->tcp_deferred_clean_death) { 15984 /* 15985 * tcp_clean_death was deferred for 15986 * T_ORDREL_IND - do it now 15987 */ 15988 tcp->tcp_deferred_clean_death = B_FALSE; 15989 (void) tcp_clean_death(tcp, 15990 tcp->tcp_client_errno, 22); 15991 } 15992 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15993 /* 15994 * If there isn't already a timer running 15995 * start one. Use a 4 second 15996 * timer as a fallback since it can't fail. 15997 */ 15998 tcp->tcp_timeout = B_TRUE; 15999 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16000 MSEC_TO_TICK(4000)); 16001 } 16002 } 16003 } 16004 16005 /* 16006 * The read side service routine is called mostly when we get back-enabled as a 16007 * result of flow control relief. Since we don't actually queue anything in 16008 * TCP, we have no data to send out of here. What we do is clear the receive 16009 * window, and send out a window update. 16010 * This routine is also called to drive an orderly release message upstream 16011 * if the attempt in tcp_rput failed. 16012 */ 16013 static void 16014 tcp_rsrv(queue_t *q) 16015 { 16016 conn_t *connp = Q_TO_CONN(q); 16017 tcp_t *tcp = connp->conn_tcp; 16018 mblk_t *mp; 16019 tcp_stack_t *tcps = tcp->tcp_tcps; 16020 16021 /* No code does a putq on the read side */ 16022 ASSERT(q->q_first == NULL); 16023 16024 /* Nothing to do for the default queue */ 16025 if (q == tcps->tcps_g_q) { 16026 return; 16027 } 16028 16029 mp = allocb(0, BPRI_HI); 16030 if (mp == NULL) { 16031 /* 16032 * We are under memory pressure. Return for now and we 16033 * we will be called again later. 16034 */ 16035 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16036 /* 16037 * If there isn't already a timer running 16038 * start one. Use a 4 second 16039 * timer as a fallback since it can't fail. 16040 */ 16041 tcp->tcp_timeout = B_TRUE; 16042 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16043 MSEC_TO_TICK(4000)); 16044 } 16045 return; 16046 } 16047 CONN_INC_REF(connp); 16048 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16049 SQTAG_TCP_RSRV); 16050 } 16051 16052 /* 16053 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16054 * We do not allow the receive window to shrink. After setting rwnd, 16055 * set the flow control hiwat of the stream. 16056 * 16057 * This function is called in 2 cases: 16058 * 16059 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16060 * connection (passive open) and in tcp_rput_data() for active connect. 16061 * This is called after tcp_mss_set() when the desired MSS value is known. 16062 * This makes sure that our window size is a mutiple of the other side's 16063 * MSS. 16064 * 2) Handling SO_RCVBUF option. 16065 * 16066 * It is ASSUMED that the requested size is a multiple of the current MSS. 16067 * 16068 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16069 * user requests so. 16070 */ 16071 static int 16072 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16073 { 16074 uint32_t mss = tcp->tcp_mss; 16075 uint32_t old_max_rwnd; 16076 uint32_t max_transmittable_rwnd; 16077 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16078 tcp_stack_t *tcps = tcp->tcp_tcps; 16079 16080 if (tcp->tcp_fused) { 16081 size_t sth_hiwat; 16082 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16083 16084 ASSERT(peer_tcp != NULL); 16085 /* 16086 * Record the stream head's high water mark for 16087 * this endpoint; this is used for flow-control 16088 * purposes in tcp_fuse_output(). 16089 */ 16090 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16091 if (!tcp_detached) 16092 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16093 16094 /* 16095 * In the fusion case, the maxpsz stream head value of 16096 * our peer is set according to its send buffer size 16097 * and our receive buffer size; since the latter may 16098 * have changed we need to update the peer's maxpsz. 16099 */ 16100 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16101 return (rwnd); 16102 } 16103 16104 if (tcp_detached) 16105 old_max_rwnd = tcp->tcp_rwnd; 16106 else 16107 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16108 16109 /* 16110 * Insist on a receive window that is at least 16111 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16112 * funny TCP interactions of Nagle algorithm, SWS avoidance 16113 * and delayed acknowledgement. 16114 */ 16115 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16116 16117 /* 16118 * If window size info has already been exchanged, TCP should not 16119 * shrink the window. Shrinking window is doable if done carefully. 16120 * We may add that support later. But so far there is not a real 16121 * need to do that. 16122 */ 16123 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16124 /* MSS may have changed, do a round up again. */ 16125 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16126 } 16127 16128 /* 16129 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16130 * can be applied even before the window scale option is decided. 16131 */ 16132 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16133 if (rwnd > max_transmittable_rwnd) { 16134 rwnd = max_transmittable_rwnd - 16135 (max_transmittable_rwnd % mss); 16136 if (rwnd < mss) 16137 rwnd = max_transmittable_rwnd; 16138 /* 16139 * If we're over the limit we may have to back down tcp_rwnd. 16140 * The increment below won't work for us. So we set all three 16141 * here and the increment below will have no effect. 16142 */ 16143 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16144 } 16145 if (tcp->tcp_localnet) { 16146 tcp->tcp_rack_abs_max = 16147 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16148 } else { 16149 /* 16150 * For a remote host on a different subnet (through a router), 16151 * we ack every other packet to be conforming to RFC1122. 16152 * tcp_deferred_acks_max is default to 2. 16153 */ 16154 tcp->tcp_rack_abs_max = 16155 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16156 } 16157 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16158 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16159 else 16160 tcp->tcp_rack_cur_max = 0; 16161 /* 16162 * Increment the current rwnd by the amount the maximum grew (we 16163 * can not overwrite it since we might be in the middle of a 16164 * connection.) 16165 */ 16166 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16167 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16168 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16169 tcp->tcp_cwnd_max = rwnd; 16170 16171 if (tcp_detached) 16172 return (rwnd); 16173 /* 16174 * We set the maximum receive window into rq->q_hiwat. 16175 * This is not actually used for flow control. 16176 */ 16177 tcp->tcp_rq->q_hiwat = rwnd; 16178 /* 16179 * Set the Stream head high water mark. This doesn't have to be 16180 * here, since we are simply using default values, but we would 16181 * prefer to choose these values algorithmically, with a likely 16182 * relationship to rwnd. 16183 */ 16184 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16185 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16186 return (rwnd); 16187 } 16188 16189 /* 16190 * Return SNMP stuff in buffer in mpdata. 16191 */ 16192 int 16193 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16194 { 16195 mblk_t *mpdata; 16196 mblk_t *mp_conn_ctl = NULL; 16197 mblk_t *mp_conn_tail; 16198 mblk_t *mp_attr_ctl = NULL; 16199 mblk_t *mp_attr_tail; 16200 mblk_t *mp6_conn_ctl = NULL; 16201 mblk_t *mp6_conn_tail; 16202 mblk_t *mp6_attr_ctl = NULL; 16203 mblk_t *mp6_attr_tail; 16204 struct opthdr *optp; 16205 mib2_tcpConnEntry_t tce; 16206 mib2_tcp6ConnEntry_t tce6; 16207 mib2_transportMLPEntry_t mlp; 16208 connf_t *connfp; 16209 conn_t *connp; 16210 int i; 16211 boolean_t ispriv; 16212 zoneid_t zoneid; 16213 int v4_conn_idx; 16214 int v6_conn_idx; 16215 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16216 ip_stack_t *ipst; 16217 16218 if (mpctl == NULL || 16219 (mpdata = mpctl->b_cont) == NULL || 16220 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16221 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16222 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16223 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16224 freemsg(mp_conn_ctl); 16225 freemsg(mp_attr_ctl); 16226 freemsg(mp6_conn_ctl); 16227 freemsg(mp6_attr_ctl); 16228 return (0); 16229 } 16230 16231 /* build table of connections -- need count in fixed part */ 16232 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16233 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16234 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16235 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16236 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16237 16238 ispriv = 16239 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16240 zoneid = Q_TO_CONN(q)->conn_zoneid; 16241 16242 v4_conn_idx = v6_conn_idx = 0; 16243 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16244 16245 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16246 ipst = tcps->tcps_netstack->netstack_ip; 16247 16248 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16249 16250 connp = NULL; 16251 16252 while ((connp = 16253 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16254 tcp_t *tcp; 16255 boolean_t needattr; 16256 16257 if (connp->conn_zoneid != zoneid) 16258 continue; /* not in this zone */ 16259 16260 tcp = connp->conn_tcp; 16261 UPDATE_MIB(&tcps->tcps_mib, 16262 tcpHCInSegs, tcp->tcp_ibsegs); 16263 tcp->tcp_ibsegs = 0; 16264 UPDATE_MIB(&tcps->tcps_mib, 16265 tcpHCOutSegs, tcp->tcp_obsegs); 16266 tcp->tcp_obsegs = 0; 16267 16268 tce6.tcp6ConnState = tce.tcpConnState = 16269 tcp_snmp_state(tcp); 16270 if (tce.tcpConnState == MIB2_TCP_established || 16271 tce.tcpConnState == MIB2_TCP_closeWait) 16272 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16273 16274 needattr = B_FALSE; 16275 bzero(&mlp, sizeof (mlp)); 16276 if (connp->conn_mlp_type != mlptSingle) { 16277 if (connp->conn_mlp_type == mlptShared || 16278 connp->conn_mlp_type == mlptBoth) 16279 mlp.tme_flags |= MIB2_TMEF_SHARED; 16280 if (connp->conn_mlp_type == mlptPrivate || 16281 connp->conn_mlp_type == mlptBoth) 16282 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16283 needattr = B_TRUE; 16284 } 16285 if (connp->conn_peercred != NULL) { 16286 ts_label_t *tsl; 16287 16288 tsl = crgetlabel(connp->conn_peercred); 16289 mlp.tme_doi = label2doi(tsl); 16290 mlp.tme_label = *label2bslabel(tsl); 16291 needattr = B_TRUE; 16292 } 16293 16294 /* Create a message to report on IPv6 entries */ 16295 if (tcp->tcp_ipversion == IPV6_VERSION) { 16296 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16297 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16298 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16299 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16300 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16301 /* Don't want just anybody seeing these... */ 16302 if (ispriv) { 16303 tce6.tcp6ConnEntryInfo.ce_snxt = 16304 tcp->tcp_snxt; 16305 tce6.tcp6ConnEntryInfo.ce_suna = 16306 tcp->tcp_suna; 16307 tce6.tcp6ConnEntryInfo.ce_rnxt = 16308 tcp->tcp_rnxt; 16309 tce6.tcp6ConnEntryInfo.ce_rack = 16310 tcp->tcp_rack; 16311 } else { 16312 /* 16313 * Netstat, unfortunately, uses this to 16314 * get send/receive queue sizes. How to fix? 16315 * Why not compute the difference only? 16316 */ 16317 tce6.tcp6ConnEntryInfo.ce_snxt = 16318 tcp->tcp_snxt - tcp->tcp_suna; 16319 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16320 tce6.tcp6ConnEntryInfo.ce_rnxt = 16321 tcp->tcp_rnxt - tcp->tcp_rack; 16322 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16323 } 16324 16325 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16326 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16327 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16328 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16329 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16330 16331 tce6.tcp6ConnCreationProcess = 16332 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16333 tcp->tcp_cpid; 16334 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16335 16336 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16337 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16338 16339 mlp.tme_connidx = v6_conn_idx++; 16340 if (needattr) 16341 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16342 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16343 } 16344 /* 16345 * Create an IPv4 table entry for IPv4 entries and also 16346 * for IPv6 entries which are bound to in6addr_any 16347 * but don't have IPV6_V6ONLY set. 16348 * (i.e. anything an IPv4 peer could connect to) 16349 */ 16350 if (tcp->tcp_ipversion == IPV4_VERSION || 16351 (tcp->tcp_state <= TCPS_LISTEN && 16352 !tcp->tcp_connp->conn_ipv6_v6only && 16353 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16354 if (tcp->tcp_ipversion == IPV6_VERSION) { 16355 tce.tcpConnRemAddress = INADDR_ANY; 16356 tce.tcpConnLocalAddress = INADDR_ANY; 16357 } else { 16358 tce.tcpConnRemAddress = 16359 tcp->tcp_remote; 16360 tce.tcpConnLocalAddress = 16361 tcp->tcp_ip_src; 16362 } 16363 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16364 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16365 /* Don't want just anybody seeing these... */ 16366 if (ispriv) { 16367 tce.tcpConnEntryInfo.ce_snxt = 16368 tcp->tcp_snxt; 16369 tce.tcpConnEntryInfo.ce_suna = 16370 tcp->tcp_suna; 16371 tce.tcpConnEntryInfo.ce_rnxt = 16372 tcp->tcp_rnxt; 16373 tce.tcpConnEntryInfo.ce_rack = 16374 tcp->tcp_rack; 16375 } else { 16376 /* 16377 * Netstat, unfortunately, uses this to 16378 * get send/receive queue sizes. How 16379 * to fix? 16380 * Why not compute the difference only? 16381 */ 16382 tce.tcpConnEntryInfo.ce_snxt = 16383 tcp->tcp_snxt - tcp->tcp_suna; 16384 tce.tcpConnEntryInfo.ce_suna = 0; 16385 tce.tcpConnEntryInfo.ce_rnxt = 16386 tcp->tcp_rnxt - tcp->tcp_rack; 16387 tce.tcpConnEntryInfo.ce_rack = 0; 16388 } 16389 16390 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16391 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16392 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16393 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16394 tce.tcpConnEntryInfo.ce_state = 16395 tcp->tcp_state; 16396 16397 tce.tcpConnCreationProcess = 16398 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16399 tcp->tcp_cpid; 16400 tce.tcpConnCreationTime = tcp->tcp_open_time; 16401 16402 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16403 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16404 16405 mlp.tme_connidx = v4_conn_idx++; 16406 if (needattr) 16407 (void) snmp_append_data2( 16408 mp_attr_ctl->b_cont, 16409 &mp_attr_tail, (char *)&mlp, 16410 sizeof (mlp)); 16411 } 16412 } 16413 } 16414 16415 /* fixed length structure for IPv4 and IPv6 counters */ 16416 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16417 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16418 sizeof (mib2_tcp6ConnEntry_t)); 16419 /* synchronize 32- and 64-bit counters */ 16420 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16421 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16422 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16423 optp->level = MIB2_TCP; 16424 optp->name = 0; 16425 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16426 sizeof (tcps->tcps_mib)); 16427 optp->len = msgdsize(mpdata); 16428 qreply(q, mpctl); 16429 16430 /* table of connections... */ 16431 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16432 sizeof (struct T_optmgmt_ack)]; 16433 optp->level = MIB2_TCP; 16434 optp->name = MIB2_TCP_CONN; 16435 optp->len = msgdsize(mp_conn_ctl->b_cont); 16436 qreply(q, mp_conn_ctl); 16437 16438 /* table of MLP attributes... */ 16439 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16440 sizeof (struct T_optmgmt_ack)]; 16441 optp->level = MIB2_TCP; 16442 optp->name = EXPER_XPORT_MLP; 16443 optp->len = msgdsize(mp_attr_ctl->b_cont); 16444 if (optp->len == 0) 16445 freemsg(mp_attr_ctl); 16446 else 16447 qreply(q, mp_attr_ctl); 16448 16449 /* table of IPv6 connections... */ 16450 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16451 sizeof (struct T_optmgmt_ack)]; 16452 optp->level = MIB2_TCP6; 16453 optp->name = MIB2_TCP6_CONN; 16454 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16455 qreply(q, mp6_conn_ctl); 16456 16457 /* table of IPv6 MLP attributes... */ 16458 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16459 sizeof (struct T_optmgmt_ack)]; 16460 optp->level = MIB2_TCP6; 16461 optp->name = EXPER_XPORT_MLP; 16462 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16463 if (optp->len == 0) 16464 freemsg(mp6_attr_ctl); 16465 else 16466 qreply(q, mp6_attr_ctl); 16467 return (1); 16468 } 16469 16470 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16471 /* ARGSUSED */ 16472 int 16473 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16474 { 16475 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16476 16477 switch (level) { 16478 case MIB2_TCP: 16479 switch (name) { 16480 case 13: 16481 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16482 return (0); 16483 /* TODO: delete entry defined by tce */ 16484 return (1); 16485 default: 16486 return (0); 16487 } 16488 default: 16489 return (1); 16490 } 16491 } 16492 16493 /* Translate TCP state to MIB2 TCP state. */ 16494 static int 16495 tcp_snmp_state(tcp_t *tcp) 16496 { 16497 if (tcp == NULL) 16498 return (0); 16499 16500 switch (tcp->tcp_state) { 16501 case TCPS_CLOSED: 16502 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16503 case TCPS_BOUND: 16504 return (MIB2_TCP_closed); 16505 case TCPS_LISTEN: 16506 return (MIB2_TCP_listen); 16507 case TCPS_SYN_SENT: 16508 return (MIB2_TCP_synSent); 16509 case TCPS_SYN_RCVD: 16510 return (MIB2_TCP_synReceived); 16511 case TCPS_ESTABLISHED: 16512 return (MIB2_TCP_established); 16513 case TCPS_CLOSE_WAIT: 16514 return (MIB2_TCP_closeWait); 16515 case TCPS_FIN_WAIT_1: 16516 return (MIB2_TCP_finWait1); 16517 case TCPS_CLOSING: 16518 return (MIB2_TCP_closing); 16519 case TCPS_LAST_ACK: 16520 return (MIB2_TCP_lastAck); 16521 case TCPS_FIN_WAIT_2: 16522 return (MIB2_TCP_finWait2); 16523 case TCPS_TIME_WAIT: 16524 return (MIB2_TCP_timeWait); 16525 default: 16526 return (0); 16527 } 16528 } 16529 16530 static char tcp_report_header[] = 16531 "TCP " MI_COL_HDRPAD_STR 16532 "zone dest snxt suna " 16533 "swnd rnxt rack rwnd rto mss w sw rw t " 16534 "recent [lport,fport] state"; 16535 16536 /* 16537 * TCP status report triggered via the Named Dispatch mechanism. 16538 */ 16539 /* ARGSUSED */ 16540 static void 16541 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16542 cred_t *cr) 16543 { 16544 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16545 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16546 char cflag; 16547 in6_addr_t v6dst; 16548 char buf[80]; 16549 uint_t print_len, buf_len; 16550 16551 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16552 if (buf_len <= 0) 16553 return; 16554 16555 if (hashval >= 0) 16556 (void) sprintf(hash, "%03d ", hashval); 16557 else 16558 hash[0] = '\0'; 16559 16560 /* 16561 * Note that we use the remote address in the tcp_b structure. 16562 * This means that it will print out the real destination address, 16563 * not the next hop's address if source routing is used. This 16564 * avoid the confusion on the output because user may not 16565 * know that source routing is used for a connection. 16566 */ 16567 if (tcp->tcp_ipversion == IPV4_VERSION) { 16568 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16569 } else { 16570 v6dst = tcp->tcp_remote_v6; 16571 } 16572 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16573 /* 16574 * the ispriv checks are so that normal users cannot determine 16575 * sequence number information using NDD. 16576 */ 16577 16578 if (TCP_IS_DETACHED(tcp)) 16579 cflag = '*'; 16580 else 16581 cflag = ' '; 16582 print_len = snprintf((char *)mp->b_wptr, buf_len, 16583 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16584 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16585 hash, 16586 (void *)tcp, 16587 tcp->tcp_connp->conn_zoneid, 16588 addrbuf, 16589 (ispriv) ? tcp->tcp_snxt : 0, 16590 (ispriv) ? tcp->tcp_suna : 0, 16591 tcp->tcp_swnd, 16592 (ispriv) ? tcp->tcp_rnxt : 0, 16593 (ispriv) ? tcp->tcp_rack : 0, 16594 tcp->tcp_rwnd, 16595 tcp->tcp_rto, 16596 tcp->tcp_mss, 16597 tcp->tcp_snd_ws_ok, 16598 tcp->tcp_snd_ws, 16599 tcp->tcp_rcv_ws, 16600 tcp->tcp_snd_ts_ok, 16601 tcp->tcp_ts_recent, 16602 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16603 if (print_len < buf_len) { 16604 ((mblk_t *)mp)->b_wptr += print_len; 16605 } else { 16606 ((mblk_t *)mp)->b_wptr += buf_len; 16607 } 16608 } 16609 16610 /* 16611 * TCP status report (for listeners only) triggered via the Named Dispatch 16612 * mechanism. 16613 */ 16614 /* ARGSUSED */ 16615 static void 16616 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16617 { 16618 char addrbuf[INET6_ADDRSTRLEN]; 16619 in6_addr_t v6dst; 16620 uint_t print_len, buf_len; 16621 16622 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16623 if (buf_len <= 0) 16624 return; 16625 16626 if (tcp->tcp_ipversion == IPV4_VERSION) { 16627 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16628 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16629 } else { 16630 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16631 addrbuf, sizeof (addrbuf)); 16632 } 16633 print_len = snprintf((char *)mp->b_wptr, buf_len, 16634 "%03d " 16635 MI_COL_PTRFMT_STR 16636 "%d %s %05u %08u %d/%d/%d%c\n", 16637 hashval, (void *)tcp, 16638 tcp->tcp_connp->conn_zoneid, 16639 addrbuf, 16640 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16641 tcp->tcp_conn_req_seqnum, 16642 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16643 tcp->tcp_conn_req_max, 16644 tcp->tcp_syn_defense ? '*' : ' '); 16645 if (print_len < buf_len) { 16646 ((mblk_t *)mp)->b_wptr += print_len; 16647 } else { 16648 ((mblk_t *)mp)->b_wptr += buf_len; 16649 } 16650 } 16651 16652 /* TCP status report triggered via the Named Dispatch mechanism. */ 16653 /* ARGSUSED */ 16654 static int 16655 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16656 { 16657 tcp_t *tcp; 16658 int i; 16659 conn_t *connp; 16660 connf_t *connfp; 16661 zoneid_t zoneid; 16662 tcp_stack_t *tcps; 16663 ip_stack_t *ipst; 16664 16665 zoneid = Q_TO_CONN(q)->conn_zoneid; 16666 tcps = Q_TO_TCP(q)->tcp_tcps; 16667 16668 /* 16669 * Because of the ndd constraint, at most we can have 64K buffer 16670 * to put in all TCP info. So to be more efficient, just 16671 * allocate a 64K buffer here, assuming we need that large buffer. 16672 * This may be a problem as any user can read tcp_status. Therefore 16673 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16674 * This should be OK as normal users should not do this too often. 16675 */ 16676 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16677 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16678 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16679 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16680 return (0); 16681 } 16682 } 16683 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16684 /* The following may work even if we cannot get a large buf. */ 16685 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16686 return (0); 16687 } 16688 16689 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16690 16691 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16692 16693 ipst = tcps->tcps_netstack->netstack_ip; 16694 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16695 16696 connp = NULL; 16697 16698 while ((connp = 16699 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16700 tcp = connp->conn_tcp; 16701 if (zoneid != GLOBAL_ZONEID && 16702 zoneid != connp->conn_zoneid) 16703 continue; 16704 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16705 cr); 16706 } 16707 16708 } 16709 16710 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16711 return (0); 16712 } 16713 16714 /* TCP status report triggered via the Named Dispatch mechanism. */ 16715 /* ARGSUSED */ 16716 static int 16717 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16718 { 16719 tf_t *tbf; 16720 tcp_t *tcp; 16721 int i; 16722 zoneid_t zoneid; 16723 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16724 16725 zoneid = Q_TO_CONN(q)->conn_zoneid; 16726 16727 /* Refer to comments in tcp_status_report(). */ 16728 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16729 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16730 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16731 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16732 return (0); 16733 } 16734 } 16735 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16736 /* The following may work even if we cannot get a large buf. */ 16737 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16738 return (0); 16739 } 16740 16741 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16742 16743 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16744 tbf = &tcps->tcps_bind_fanout[i]; 16745 mutex_enter(&tbf->tf_lock); 16746 for (tcp = tbf->tf_tcp; tcp != NULL; 16747 tcp = tcp->tcp_bind_hash) { 16748 if (zoneid != GLOBAL_ZONEID && 16749 zoneid != tcp->tcp_connp->conn_zoneid) 16750 continue; 16751 CONN_INC_REF(tcp->tcp_connp); 16752 tcp_report_item(mp->b_cont, tcp, i, 16753 Q_TO_TCP(q), cr); 16754 CONN_DEC_REF(tcp->tcp_connp); 16755 } 16756 mutex_exit(&tbf->tf_lock); 16757 } 16758 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16759 return (0); 16760 } 16761 16762 /* TCP status report triggered via the Named Dispatch mechanism. */ 16763 /* ARGSUSED */ 16764 static int 16765 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16766 { 16767 connf_t *connfp; 16768 conn_t *connp; 16769 tcp_t *tcp; 16770 int i; 16771 zoneid_t zoneid; 16772 tcp_stack_t *tcps; 16773 ip_stack_t *ipst; 16774 16775 zoneid = Q_TO_CONN(q)->conn_zoneid; 16776 tcps = Q_TO_TCP(q)->tcp_tcps; 16777 16778 /* Refer to comments in tcp_status_report(). */ 16779 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16780 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16781 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16782 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16783 return (0); 16784 } 16785 } 16786 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16787 /* The following may work even if we cannot get a large buf. */ 16788 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16789 return (0); 16790 } 16791 16792 (void) mi_mpprintf(mp, 16793 " TCP " MI_COL_HDRPAD_STR 16794 "zone IP addr port seqnum backlog (q0/q/max)"); 16795 16796 ipst = tcps->tcps_netstack->netstack_ip; 16797 16798 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16799 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16800 connp = NULL; 16801 while ((connp = 16802 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16803 tcp = connp->conn_tcp; 16804 if (zoneid != GLOBAL_ZONEID && 16805 zoneid != connp->conn_zoneid) 16806 continue; 16807 tcp_report_listener(mp->b_cont, tcp, i); 16808 } 16809 } 16810 16811 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16812 return (0); 16813 } 16814 16815 /* TCP status report triggered via the Named Dispatch mechanism. */ 16816 /* ARGSUSED */ 16817 static int 16818 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16819 { 16820 connf_t *connfp; 16821 conn_t *connp; 16822 tcp_t *tcp; 16823 int i; 16824 zoneid_t zoneid; 16825 tcp_stack_t *tcps; 16826 ip_stack_t *ipst; 16827 16828 zoneid = Q_TO_CONN(q)->conn_zoneid; 16829 tcps = Q_TO_TCP(q)->tcp_tcps; 16830 ipst = tcps->tcps_netstack->netstack_ip; 16831 16832 /* Refer to comments in tcp_status_report(). */ 16833 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16834 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16835 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16836 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16837 return (0); 16838 } 16839 } 16840 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16841 /* The following may work even if we cannot get a large buf. */ 16842 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16843 return (0); 16844 } 16845 16846 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16847 ipst->ips_ipcl_conn_fanout_size); 16848 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16849 16850 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16851 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16852 connp = NULL; 16853 while ((connp = 16854 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16855 tcp = connp->conn_tcp; 16856 if (zoneid != GLOBAL_ZONEID && 16857 zoneid != connp->conn_zoneid) 16858 continue; 16859 tcp_report_item(mp->b_cont, tcp, i, 16860 Q_TO_TCP(q), cr); 16861 } 16862 } 16863 16864 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16865 return (0); 16866 } 16867 16868 /* TCP status report triggered via the Named Dispatch mechanism. */ 16869 /* ARGSUSED */ 16870 static int 16871 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16872 { 16873 tf_t *tf; 16874 tcp_t *tcp; 16875 int i; 16876 zoneid_t zoneid; 16877 tcp_stack_t *tcps; 16878 16879 zoneid = Q_TO_CONN(q)->conn_zoneid; 16880 tcps = Q_TO_TCP(q)->tcp_tcps; 16881 16882 /* Refer to comments in tcp_status_report(). */ 16883 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16884 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16885 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16886 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16887 return (0); 16888 } 16889 } 16890 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16891 /* The following may work even if we cannot get a large buf. */ 16892 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16893 return (0); 16894 } 16895 16896 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16897 16898 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16899 tf = &tcps->tcps_acceptor_fanout[i]; 16900 mutex_enter(&tf->tf_lock); 16901 for (tcp = tf->tf_tcp; tcp != NULL; 16902 tcp = tcp->tcp_acceptor_hash) { 16903 if (zoneid != GLOBAL_ZONEID && 16904 zoneid != tcp->tcp_connp->conn_zoneid) 16905 continue; 16906 tcp_report_item(mp->b_cont, tcp, i, 16907 Q_TO_TCP(q), cr); 16908 } 16909 mutex_exit(&tf->tf_lock); 16910 } 16911 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16912 return (0); 16913 } 16914 16915 /* 16916 * tcp_timer is the timer service routine. It handles the retransmission, 16917 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16918 * from the state of the tcp instance what kind of action needs to be done 16919 * at the time it is called. 16920 */ 16921 static void 16922 tcp_timer(void *arg) 16923 { 16924 mblk_t *mp; 16925 clock_t first_threshold; 16926 clock_t second_threshold; 16927 clock_t ms; 16928 uint32_t mss; 16929 conn_t *connp = (conn_t *)arg; 16930 tcp_t *tcp = connp->conn_tcp; 16931 tcp_stack_t *tcps = tcp->tcp_tcps; 16932 16933 tcp->tcp_timer_tid = 0; 16934 16935 if (tcp->tcp_fused) 16936 return; 16937 16938 first_threshold = tcp->tcp_first_timer_threshold; 16939 second_threshold = tcp->tcp_second_timer_threshold; 16940 switch (tcp->tcp_state) { 16941 case TCPS_IDLE: 16942 case TCPS_BOUND: 16943 case TCPS_LISTEN: 16944 return; 16945 case TCPS_SYN_RCVD: { 16946 tcp_t *listener = tcp->tcp_listener; 16947 16948 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16949 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16950 /* it's our first timeout */ 16951 tcp->tcp_syn_rcvd_timeout = 1; 16952 mutex_enter(&listener->tcp_eager_lock); 16953 listener->tcp_syn_rcvd_timeout++; 16954 if (!tcp->tcp_dontdrop && tcp->tcp_closemp_used == 0) { 16955 /* 16956 * Make this eager available for drop if we 16957 * need to drop one to accomodate a new 16958 * incoming SYN request. 16959 */ 16960 MAKE_DROPPABLE(listener, tcp); 16961 } 16962 if (!listener->tcp_syn_defense && 16963 (listener->tcp_syn_rcvd_timeout > 16964 (tcps->tcps_conn_req_max_q0 >> 2)) && 16965 (tcps->tcps_conn_req_max_q0 > 200)) { 16966 /* We may be under attack. Put on a defense. */ 16967 listener->tcp_syn_defense = B_TRUE; 16968 cmn_err(CE_WARN, "High TCP connect timeout " 16969 "rate! System (port %d) may be under a " 16970 "SYN flood attack!", 16971 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16972 16973 listener->tcp_ip_addr_cache = kmem_zalloc( 16974 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16975 KM_NOSLEEP); 16976 } 16977 mutex_exit(&listener->tcp_eager_lock); 16978 } else if (listener != NULL) { 16979 mutex_enter(&listener->tcp_eager_lock); 16980 tcp->tcp_syn_rcvd_timeout++; 16981 if (tcp->tcp_syn_rcvd_timeout > 1 && 16982 tcp->tcp_closemp_used == 0) { 16983 /* 16984 * This is our second timeout. Put the tcp in 16985 * the list of droppable eagers to allow it to 16986 * be dropped, if needed. We don't check 16987 * whether tcp_dontdrop is set or not to 16988 * protect ourselve from a SYN attack where a 16989 * remote host can spoof itself as one of the 16990 * good IP source and continue to hold 16991 * resources too long. 16992 */ 16993 MAKE_DROPPABLE(listener, tcp); 16994 } 16995 mutex_exit(&listener->tcp_eager_lock); 16996 } 16997 } 16998 /* FALLTHRU */ 16999 case TCPS_SYN_SENT: 17000 first_threshold = tcp->tcp_first_ctimer_threshold; 17001 second_threshold = tcp->tcp_second_ctimer_threshold; 17002 break; 17003 case TCPS_ESTABLISHED: 17004 case TCPS_FIN_WAIT_1: 17005 case TCPS_CLOSING: 17006 case TCPS_CLOSE_WAIT: 17007 case TCPS_LAST_ACK: 17008 /* If we have data to rexmit */ 17009 if (tcp->tcp_suna != tcp->tcp_snxt) { 17010 clock_t time_to_wait; 17011 17012 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17013 if (!tcp->tcp_xmit_head) 17014 break; 17015 time_to_wait = lbolt - 17016 (clock_t)tcp->tcp_xmit_head->b_prev; 17017 time_to_wait = tcp->tcp_rto - 17018 TICK_TO_MSEC(time_to_wait); 17019 /* 17020 * If the timer fires too early, 1 clock tick earlier, 17021 * restart the timer. 17022 */ 17023 if (time_to_wait > msec_per_tick) { 17024 TCP_STAT(tcps, tcp_timer_fire_early); 17025 TCP_TIMER_RESTART(tcp, time_to_wait); 17026 return; 17027 } 17028 /* 17029 * When we probe zero windows, we force the swnd open. 17030 * If our peer acks with a closed window swnd will be 17031 * set to zero by tcp_rput(). As long as we are 17032 * receiving acks tcp_rput will 17033 * reset 'tcp_ms_we_have_waited' so as not to trip the 17034 * first and second interval actions. NOTE: the timer 17035 * interval is allowed to continue its exponential 17036 * backoff. 17037 */ 17038 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17039 if (tcp->tcp_debug) { 17040 (void) strlog(TCP_MOD_ID, 0, 1, 17041 SL_TRACE, "tcp_timer: zero win"); 17042 } 17043 } else { 17044 /* 17045 * After retransmission, we need to do 17046 * slow start. Set the ssthresh to one 17047 * half of current effective window and 17048 * cwnd to one MSS. Also reset 17049 * tcp_cwnd_cnt. 17050 * 17051 * Note that if tcp_ssthresh is reduced because 17052 * of ECN, do not reduce it again unless it is 17053 * already one window of data away (tcp_cwr 17054 * should then be cleared) or this is a 17055 * timeout for a retransmitted segment. 17056 */ 17057 uint32_t npkt; 17058 17059 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17060 npkt = ((tcp->tcp_timer_backoff ? 17061 tcp->tcp_cwnd_ssthresh : 17062 tcp->tcp_snxt - 17063 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17064 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17065 tcp->tcp_mss; 17066 } 17067 tcp->tcp_cwnd = tcp->tcp_mss; 17068 tcp->tcp_cwnd_cnt = 0; 17069 if (tcp->tcp_ecn_ok) { 17070 tcp->tcp_cwr = B_TRUE; 17071 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17072 tcp->tcp_ecn_cwr_sent = B_FALSE; 17073 } 17074 } 17075 break; 17076 } 17077 /* 17078 * We have something to send yet we cannot send. The 17079 * reason can be: 17080 * 17081 * 1. Zero send window: we need to do zero window probe. 17082 * 2. Zero cwnd: because of ECN, we need to "clock out 17083 * segments. 17084 * 3. SWS avoidance: receiver may have shrunk window, 17085 * reset our knowledge. 17086 * 17087 * Note that condition 2 can happen with either 1 or 17088 * 3. But 1 and 3 are exclusive. 17089 */ 17090 if (tcp->tcp_unsent != 0) { 17091 if (tcp->tcp_cwnd == 0) { 17092 /* 17093 * Set tcp_cwnd to 1 MSS so that a 17094 * new segment can be sent out. We 17095 * are "clocking out" new data when 17096 * the network is really congested. 17097 */ 17098 ASSERT(tcp->tcp_ecn_ok); 17099 tcp->tcp_cwnd = tcp->tcp_mss; 17100 } 17101 if (tcp->tcp_swnd == 0) { 17102 /* Extend window for zero window probe */ 17103 tcp->tcp_swnd++; 17104 tcp->tcp_zero_win_probe = B_TRUE; 17105 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17106 } else { 17107 /* 17108 * Handle timeout from sender SWS avoidance. 17109 * Reset our knowledge of the max send window 17110 * since the receiver might have reduced its 17111 * receive buffer. Avoid setting tcp_max_swnd 17112 * to one since that will essentially disable 17113 * the SWS checks. 17114 * 17115 * Note that since we don't have a SWS 17116 * state variable, if the timeout is set 17117 * for ECN but not for SWS, this 17118 * code will also be executed. This is 17119 * fine as tcp_max_swnd is updated 17120 * constantly and it will not affect 17121 * anything. 17122 */ 17123 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17124 } 17125 tcp_wput_data(tcp, NULL, B_FALSE); 17126 return; 17127 } 17128 /* Is there a FIN that needs to be to re retransmitted? */ 17129 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17130 !tcp->tcp_fin_acked) 17131 break; 17132 /* Nothing to do, return without restarting timer. */ 17133 TCP_STAT(tcps, tcp_timer_fire_miss); 17134 return; 17135 case TCPS_FIN_WAIT_2: 17136 /* 17137 * User closed the TCP endpoint and peer ACK'ed our FIN. 17138 * We waited some time for for peer's FIN, but it hasn't 17139 * arrived. We flush the connection now to avoid 17140 * case where the peer has rebooted. 17141 */ 17142 if (TCP_IS_DETACHED(tcp)) { 17143 (void) tcp_clean_death(tcp, 0, 23); 17144 } else { 17145 TCP_TIMER_RESTART(tcp, 17146 tcps->tcps_fin_wait_2_flush_interval); 17147 } 17148 return; 17149 case TCPS_TIME_WAIT: 17150 (void) tcp_clean_death(tcp, 0, 24); 17151 return; 17152 default: 17153 if (tcp->tcp_debug) { 17154 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17155 "tcp_timer: strange state (%d) %s", 17156 tcp->tcp_state, tcp_display(tcp, NULL, 17157 DISP_PORT_ONLY)); 17158 } 17159 return; 17160 } 17161 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17162 /* 17163 * For zero window probe, we need to send indefinitely, 17164 * unless we have not heard from the other side for some 17165 * time... 17166 */ 17167 if ((tcp->tcp_zero_win_probe == 0) || 17168 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17169 second_threshold)) { 17170 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17171 /* 17172 * If TCP is in SYN_RCVD state, send back a 17173 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17174 * should be zero in TCPS_SYN_RCVD state. 17175 */ 17176 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17177 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17178 "in SYN_RCVD", 17179 tcp, tcp->tcp_snxt, 17180 tcp->tcp_rnxt, TH_RST | TH_ACK); 17181 } 17182 (void) tcp_clean_death(tcp, 17183 tcp->tcp_client_errno ? 17184 tcp->tcp_client_errno : ETIMEDOUT, 25); 17185 return; 17186 } else { 17187 /* 17188 * Set tcp_ms_we_have_waited to second_threshold 17189 * so that in next timeout, we will do the above 17190 * check (lbolt - tcp_last_recv_time). This is 17191 * also to avoid overflow. 17192 * 17193 * We don't need to decrement tcp_timer_backoff 17194 * to avoid overflow because it will be decremented 17195 * later if new timeout value is greater than 17196 * tcp_rexmit_interval_max. In the case when 17197 * tcp_rexmit_interval_max is greater than 17198 * second_threshold, it means that we will wait 17199 * longer than second_threshold to send the next 17200 * window probe. 17201 */ 17202 tcp->tcp_ms_we_have_waited = second_threshold; 17203 } 17204 } else if (ms > first_threshold) { 17205 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17206 tcp->tcp_xmit_head != NULL) { 17207 tcp->tcp_xmit_head = 17208 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17209 } 17210 /* 17211 * We have been retransmitting for too long... The RTT 17212 * we calculated is probably incorrect. Reinitialize it. 17213 * Need to compensate for 0 tcp_rtt_sa. Reset 17214 * tcp_rtt_update so that we won't accidentally cache a 17215 * bad value. But only do this if this is not a zero 17216 * window probe. 17217 */ 17218 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17219 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17220 (tcp->tcp_rtt_sa >> 5); 17221 tcp->tcp_rtt_sa = 0; 17222 tcp_ip_notify(tcp); 17223 tcp->tcp_rtt_update = 0; 17224 } 17225 } 17226 tcp->tcp_timer_backoff++; 17227 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17228 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17229 tcps->tcps_rexmit_interval_min) { 17230 /* 17231 * This means the original RTO is tcp_rexmit_interval_min. 17232 * So we will use tcp_rexmit_interval_min as the RTO value 17233 * and do the backoff. 17234 */ 17235 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17236 } else { 17237 ms <<= tcp->tcp_timer_backoff; 17238 } 17239 if (ms > tcps->tcps_rexmit_interval_max) { 17240 ms = tcps->tcps_rexmit_interval_max; 17241 /* 17242 * ms is at max, decrement tcp_timer_backoff to avoid 17243 * overflow. 17244 */ 17245 tcp->tcp_timer_backoff--; 17246 } 17247 tcp->tcp_ms_we_have_waited += ms; 17248 if (tcp->tcp_zero_win_probe == 0) { 17249 tcp->tcp_rto = ms; 17250 } 17251 TCP_TIMER_RESTART(tcp, ms); 17252 /* 17253 * This is after a timeout and tcp_rto is backed off. Set 17254 * tcp_set_timer to 1 so that next time RTO is updated, we will 17255 * restart the timer with a correct value. 17256 */ 17257 tcp->tcp_set_timer = 1; 17258 mss = tcp->tcp_snxt - tcp->tcp_suna; 17259 if (mss > tcp->tcp_mss) 17260 mss = tcp->tcp_mss; 17261 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17262 mss = tcp->tcp_swnd; 17263 17264 if ((mp = tcp->tcp_xmit_head) != NULL) 17265 mp->b_prev = (mblk_t *)lbolt; 17266 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17267 B_TRUE); 17268 17269 /* 17270 * When slow start after retransmission begins, start with 17271 * this seq no. tcp_rexmit_max marks the end of special slow 17272 * start phase. tcp_snd_burst controls how many segments 17273 * can be sent because of an ack. 17274 */ 17275 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17276 tcp->tcp_snd_burst = TCP_CWND_SS; 17277 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17278 (tcp->tcp_unsent == 0)) { 17279 tcp->tcp_rexmit_max = tcp->tcp_fss; 17280 } else { 17281 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17282 } 17283 tcp->tcp_rexmit = B_TRUE; 17284 tcp->tcp_dupack_cnt = 0; 17285 17286 /* 17287 * Remove all rexmit SACK blk to start from fresh. 17288 */ 17289 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17290 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17291 tcp->tcp_num_notsack_blk = 0; 17292 tcp->tcp_cnt_notsack_list = 0; 17293 } 17294 if (mp == NULL) { 17295 return; 17296 } 17297 /* Attach credentials to retransmitted initial SYNs. */ 17298 if (tcp->tcp_state == TCPS_SYN_SENT) { 17299 mblk_setcred(mp, tcp->tcp_cred); 17300 DB_CPID(mp) = tcp->tcp_cpid; 17301 } 17302 17303 tcp->tcp_csuna = tcp->tcp_snxt; 17304 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17305 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17306 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 17307 tcp_send_data(tcp, tcp->tcp_wq, mp); 17308 17309 } 17310 17311 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17312 static void 17313 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17314 { 17315 conn_t *connp; 17316 17317 switch (tcp->tcp_state) { 17318 case TCPS_BOUND: 17319 case TCPS_LISTEN: 17320 break; 17321 default: 17322 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17323 return; 17324 } 17325 17326 /* 17327 * Need to clean up all the eagers since after the unbind, segments 17328 * will no longer be delivered to this listener stream. 17329 */ 17330 mutex_enter(&tcp->tcp_eager_lock); 17331 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17332 tcp_eager_cleanup(tcp, 0); 17333 } 17334 mutex_exit(&tcp->tcp_eager_lock); 17335 17336 if (tcp->tcp_ipversion == IPV4_VERSION) { 17337 tcp->tcp_ipha->ipha_src = 0; 17338 } else { 17339 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17340 } 17341 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17342 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17343 tcp_bind_hash_remove(tcp); 17344 tcp->tcp_state = TCPS_IDLE; 17345 tcp->tcp_mdt = B_FALSE; 17346 /* Send M_FLUSH according to TPI */ 17347 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17348 connp = tcp->tcp_connp; 17349 connp->conn_mdt_ok = B_FALSE; 17350 ipcl_hash_remove(connp); 17351 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17352 mp = mi_tpi_ok_ack_alloc(mp); 17353 putnext(tcp->tcp_rq, mp); 17354 } 17355 17356 /* 17357 * Don't let port fall into the privileged range. 17358 * Since the extra privileged ports can be arbitrary we also 17359 * ensure that we exclude those from consideration. 17360 * tcp_g_epriv_ports is not sorted thus we loop over it until 17361 * there are no changes. 17362 * 17363 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17364 * but instead the code relies on: 17365 * - the fact that the address of the array and its size never changes 17366 * - the atomic assignment of the elements of the array 17367 * 17368 * Returns 0 if there are no more ports available. 17369 * 17370 * TS note: skip multilevel ports. 17371 */ 17372 static in_port_t 17373 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17374 { 17375 int i; 17376 boolean_t restart = B_FALSE; 17377 tcp_stack_t *tcps = tcp->tcp_tcps; 17378 17379 if (random && tcp_random_anon_port != 0) { 17380 (void) random_get_pseudo_bytes((uint8_t *)&port, 17381 sizeof (in_port_t)); 17382 /* 17383 * Unless changed by a sys admin, the smallest anon port 17384 * is 32768 and the largest anon port is 65535. It is 17385 * very likely (50%) for the random port to be smaller 17386 * than the smallest anon port. When that happens, 17387 * add port % (anon port range) to the smallest anon 17388 * port to get the random port. It should fall into the 17389 * valid anon port range. 17390 */ 17391 if (port < tcps->tcps_smallest_anon_port) { 17392 port = tcps->tcps_smallest_anon_port + 17393 port % (tcps->tcps_largest_anon_port - 17394 tcps->tcps_smallest_anon_port); 17395 } 17396 } 17397 17398 retry: 17399 if (port < tcps->tcps_smallest_anon_port) 17400 port = (in_port_t)tcps->tcps_smallest_anon_port; 17401 17402 if (port > tcps->tcps_largest_anon_port) { 17403 if (restart) 17404 return (0); 17405 restart = B_TRUE; 17406 port = (in_port_t)tcps->tcps_smallest_anon_port; 17407 } 17408 17409 if (port < tcps->tcps_smallest_nonpriv_port) 17410 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17411 17412 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17413 if (port == tcps->tcps_g_epriv_ports[i]) { 17414 port++; 17415 /* 17416 * Make sure whether the port is in the 17417 * valid range. 17418 */ 17419 goto retry; 17420 } 17421 } 17422 if (is_system_labeled() && 17423 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17424 IPPROTO_TCP, B_TRUE)) != 0) { 17425 port = i; 17426 goto retry; 17427 } 17428 return (port); 17429 } 17430 17431 /* 17432 * Return the next anonymous port in the privileged port range for 17433 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17434 * downwards. This is the same behavior as documented in the userland 17435 * library call rresvport(3N). 17436 * 17437 * TS note: skip multilevel ports. 17438 */ 17439 static in_port_t 17440 tcp_get_next_priv_port(const tcp_t *tcp) 17441 { 17442 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17443 in_port_t nextport; 17444 boolean_t restart = B_FALSE; 17445 tcp_stack_t *tcps = tcp->tcp_tcps; 17446 retry: 17447 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17448 next_priv_port >= IPPORT_RESERVED) { 17449 next_priv_port = IPPORT_RESERVED - 1; 17450 if (restart) 17451 return (0); 17452 restart = B_TRUE; 17453 } 17454 if (is_system_labeled() && 17455 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17456 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17457 next_priv_port = nextport; 17458 goto retry; 17459 } 17460 return (next_priv_port--); 17461 } 17462 17463 /* The write side r/w procedure. */ 17464 17465 #if CCS_STATS 17466 struct { 17467 struct { 17468 int64_t count, bytes; 17469 } tot, hit; 17470 } wrw_stats; 17471 #endif 17472 17473 /* 17474 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17475 * messages. 17476 */ 17477 /* ARGSUSED */ 17478 static void 17479 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17480 { 17481 conn_t *connp = (conn_t *)arg; 17482 tcp_t *tcp = connp->conn_tcp; 17483 queue_t *q = tcp->tcp_wq; 17484 17485 ASSERT(DB_TYPE(mp) != M_IOCTL); 17486 /* 17487 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17488 * Once the close starts, streamhead and sockfs will not let any data 17489 * packets come down (close ensures that there are no threads using the 17490 * queue and no new threads will come down) but since qprocsoff() 17491 * hasn't happened yet, a M_FLUSH or some non data message might 17492 * get reflected back (in response to our own FLUSHRW) and get 17493 * processed after tcp_close() is done. The conn would still be valid 17494 * because a ref would have added but we need to check the state 17495 * before actually processing the packet. 17496 */ 17497 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17498 freemsg(mp); 17499 return; 17500 } 17501 17502 switch (DB_TYPE(mp)) { 17503 case M_IOCDATA: 17504 tcp_wput_iocdata(tcp, mp); 17505 break; 17506 case M_FLUSH: 17507 tcp_wput_flush(tcp, mp); 17508 break; 17509 default: 17510 CALL_IP_WPUT(connp, q, mp); 17511 break; 17512 } 17513 } 17514 17515 /* 17516 * The TCP fast path write put procedure. 17517 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17518 */ 17519 /* ARGSUSED */ 17520 void 17521 tcp_output(void *arg, mblk_t *mp, void *arg2) 17522 { 17523 int len; 17524 int hdrlen; 17525 int plen; 17526 mblk_t *mp1; 17527 uchar_t *rptr; 17528 uint32_t snxt; 17529 tcph_t *tcph; 17530 struct datab *db; 17531 uint32_t suna; 17532 uint32_t mss; 17533 ipaddr_t *dst; 17534 ipaddr_t *src; 17535 uint32_t sum; 17536 int usable; 17537 conn_t *connp = (conn_t *)arg; 17538 tcp_t *tcp = connp->conn_tcp; 17539 uint32_t msize; 17540 tcp_stack_t *tcps = tcp->tcp_tcps; 17541 17542 /* 17543 * Try and ASSERT the minimum possible references on the 17544 * conn early enough. Since we are executing on write side, 17545 * the connection is obviously not detached and that means 17546 * there is a ref each for TCP and IP. Since we are behind 17547 * the squeue, the minimum references needed are 3. If the 17548 * conn is in classifier hash list, there should be an 17549 * extra ref for that (we check both the possibilities). 17550 */ 17551 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17552 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17553 17554 ASSERT(DB_TYPE(mp) == M_DATA); 17555 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17556 17557 mutex_enter(&tcp->tcp_non_sq_lock); 17558 tcp->tcp_squeue_bytes -= msize; 17559 mutex_exit(&tcp->tcp_non_sq_lock); 17560 17561 /* Bypass tcp protocol for fused tcp loopback */ 17562 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17563 return; 17564 17565 mss = tcp->tcp_mss; 17566 if (tcp->tcp_xmit_zc_clean) 17567 mp = tcp_zcopy_backoff(tcp, mp, 0); 17568 17569 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17570 len = (int)(mp->b_wptr - mp->b_rptr); 17571 17572 /* 17573 * Criteria for fast path: 17574 * 17575 * 1. no unsent data 17576 * 2. single mblk in request 17577 * 3. connection established 17578 * 4. data in mblk 17579 * 5. len <= mss 17580 * 6. no tcp_valid bits 17581 */ 17582 if ((tcp->tcp_unsent != 0) || 17583 (tcp->tcp_cork) || 17584 (mp->b_cont != NULL) || 17585 (tcp->tcp_state != TCPS_ESTABLISHED) || 17586 (len == 0) || 17587 (len > mss) || 17588 (tcp->tcp_valid_bits != 0)) { 17589 tcp_wput_data(tcp, mp, B_FALSE); 17590 return; 17591 } 17592 17593 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17594 ASSERT(tcp->tcp_fin_sent == 0); 17595 17596 /* queue new packet onto retransmission queue */ 17597 if (tcp->tcp_xmit_head == NULL) { 17598 tcp->tcp_xmit_head = mp; 17599 } else { 17600 tcp->tcp_xmit_last->b_cont = mp; 17601 } 17602 tcp->tcp_xmit_last = mp; 17603 tcp->tcp_xmit_tail = mp; 17604 17605 /* find out how much we can send */ 17606 /* BEGIN CSTYLED */ 17607 /* 17608 * un-acked usable 17609 * |--------------|-----------------| 17610 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17611 */ 17612 /* END CSTYLED */ 17613 17614 /* start sending from tcp_snxt */ 17615 snxt = tcp->tcp_snxt; 17616 17617 /* 17618 * Check to see if this connection has been idled for some 17619 * time and no ACK is expected. If it is, we need to slow 17620 * start again to get back the connection's "self-clock" as 17621 * described in VJ's paper. 17622 * 17623 * Refer to the comment in tcp_mss_set() for the calculation 17624 * of tcp_cwnd after idle. 17625 */ 17626 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17627 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17628 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17629 } 17630 17631 usable = tcp->tcp_swnd; /* tcp window size */ 17632 if (usable > tcp->tcp_cwnd) 17633 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17634 usable -= snxt; /* subtract stuff already sent */ 17635 suna = tcp->tcp_suna; 17636 usable += suna; 17637 /* usable can be < 0 if the congestion window is smaller */ 17638 if (len > usable) { 17639 /* Can't send complete M_DATA in one shot */ 17640 goto slow; 17641 } 17642 17643 mutex_enter(&tcp->tcp_non_sq_lock); 17644 if (tcp->tcp_flow_stopped && 17645 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17646 tcp_clrqfull(tcp); 17647 } 17648 mutex_exit(&tcp->tcp_non_sq_lock); 17649 17650 /* 17651 * determine if anything to send (Nagle). 17652 * 17653 * 1. len < tcp_mss (i.e. small) 17654 * 2. unacknowledged data present 17655 * 3. len < nagle limit 17656 * 4. last packet sent < nagle limit (previous packet sent) 17657 */ 17658 if ((len < mss) && (snxt != suna) && 17659 (len < (int)tcp->tcp_naglim) && 17660 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17661 /* 17662 * This was the first unsent packet and normally 17663 * mss < xmit_hiwater so there is no need to worry 17664 * about flow control. The next packet will go 17665 * through the flow control check in tcp_wput_data(). 17666 */ 17667 /* leftover work from above */ 17668 tcp->tcp_unsent = len; 17669 tcp->tcp_xmit_tail_unsent = len; 17670 17671 return; 17672 } 17673 17674 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17675 17676 if (snxt == suna) { 17677 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17678 } 17679 17680 /* we have always sent something */ 17681 tcp->tcp_rack_cnt = 0; 17682 17683 tcp->tcp_snxt = snxt + len; 17684 tcp->tcp_rack = tcp->tcp_rnxt; 17685 17686 if ((mp1 = dupb(mp)) == 0) 17687 goto no_memory; 17688 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17689 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17690 17691 /* adjust tcp header information */ 17692 tcph = tcp->tcp_tcph; 17693 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17694 17695 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17696 sum = (sum >> 16) + (sum & 0xFFFF); 17697 U16_TO_ABE16(sum, tcph->th_sum); 17698 17699 U32_TO_ABE32(snxt, tcph->th_seq); 17700 17701 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17702 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17703 BUMP_LOCAL(tcp->tcp_obsegs); 17704 17705 /* Update the latest receive window size in TCP header. */ 17706 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17707 tcph->th_win); 17708 17709 tcp->tcp_last_sent_len = (ushort_t)len; 17710 17711 plen = len + tcp->tcp_hdr_len; 17712 17713 if (tcp->tcp_ipversion == IPV4_VERSION) { 17714 tcp->tcp_ipha->ipha_length = htons(plen); 17715 } else { 17716 tcp->tcp_ip6h->ip6_plen = htons(plen - 17717 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17718 } 17719 17720 /* see if we need to allocate a mblk for the headers */ 17721 hdrlen = tcp->tcp_hdr_len; 17722 rptr = mp1->b_rptr - hdrlen; 17723 db = mp1->b_datap; 17724 if ((db->db_ref != 2) || rptr < db->db_base || 17725 (!OK_32PTR(rptr))) { 17726 /* NOTE: we assume allocb returns an OK_32PTR */ 17727 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17728 tcps->tcps_wroff_xtra, BPRI_MED); 17729 if (!mp) { 17730 freemsg(mp1); 17731 goto no_memory; 17732 } 17733 mp->b_cont = mp1; 17734 mp1 = mp; 17735 /* Leave room for Link Level header */ 17736 /* hdrlen = tcp->tcp_hdr_len; */ 17737 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17738 mp1->b_wptr = &rptr[hdrlen]; 17739 } 17740 mp1->b_rptr = rptr; 17741 17742 /* Fill in the timestamp option. */ 17743 if (tcp->tcp_snd_ts_ok) { 17744 U32_TO_BE32((uint32_t)lbolt, 17745 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17746 U32_TO_BE32(tcp->tcp_ts_recent, 17747 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17748 } else { 17749 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17750 } 17751 17752 /* copy header into outgoing packet */ 17753 dst = (ipaddr_t *)rptr; 17754 src = (ipaddr_t *)tcp->tcp_iphc; 17755 dst[0] = src[0]; 17756 dst[1] = src[1]; 17757 dst[2] = src[2]; 17758 dst[3] = src[3]; 17759 dst[4] = src[4]; 17760 dst[5] = src[5]; 17761 dst[6] = src[6]; 17762 dst[7] = src[7]; 17763 dst[8] = src[8]; 17764 dst[9] = src[9]; 17765 if (hdrlen -= 40) { 17766 hdrlen >>= 2; 17767 dst += 10; 17768 src += 10; 17769 do { 17770 *dst++ = *src++; 17771 } while (--hdrlen); 17772 } 17773 17774 /* 17775 * Set the ECN info in the TCP header. Note that this 17776 * is not the template header. 17777 */ 17778 if (tcp->tcp_ecn_ok) { 17779 SET_ECT(tcp, rptr); 17780 17781 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17782 if (tcp->tcp_ecn_echo_on) 17783 tcph->th_flags[0] |= TH_ECE; 17784 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17785 tcph->th_flags[0] |= TH_CWR; 17786 tcp->tcp_ecn_cwr_sent = B_TRUE; 17787 } 17788 } 17789 17790 if (tcp->tcp_ip_forward_progress) { 17791 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17792 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17793 tcp->tcp_ip_forward_progress = B_FALSE; 17794 } 17795 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17796 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17797 return; 17798 17799 /* 17800 * If we ran out of memory, we pretend to have sent the packet 17801 * and that it was lost on the wire. 17802 */ 17803 no_memory: 17804 return; 17805 17806 slow: 17807 /* leftover work from above */ 17808 tcp->tcp_unsent = len; 17809 tcp->tcp_xmit_tail_unsent = len; 17810 tcp_wput_data(tcp, NULL, B_FALSE); 17811 } 17812 17813 /* 17814 * The function called through squeue to get behind eager's perimeter to 17815 * finish the accept processing. 17816 */ 17817 /* ARGSUSED */ 17818 void 17819 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17820 { 17821 conn_t *connp = (conn_t *)arg; 17822 tcp_t *tcp = connp->conn_tcp; 17823 queue_t *q = tcp->tcp_rq; 17824 mblk_t *mp1; 17825 mblk_t *stropt_mp = mp; 17826 struct stroptions *stropt; 17827 uint_t thwin; 17828 tcp_stack_t *tcps = tcp->tcp_tcps; 17829 17830 /* 17831 * Drop the eager's ref on the listener, that was placed when 17832 * this eager began life in tcp_conn_request. 17833 */ 17834 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17835 17836 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17837 /* 17838 * Someone blewoff the eager before we could finish 17839 * the accept. 17840 * 17841 * The only reason eager exists it because we put in 17842 * a ref on it when conn ind went up. We need to send 17843 * a disconnect indication up while the last reference 17844 * on the eager will be dropped by the squeue when we 17845 * return. 17846 */ 17847 ASSERT(tcp->tcp_listener == NULL); 17848 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17849 struct T_discon_ind *tdi; 17850 17851 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17852 /* 17853 * Let us reuse the incoming mblk to avoid memory 17854 * allocation failure problems. We know that the 17855 * size of the incoming mblk i.e. stroptions is greater 17856 * than sizeof T_discon_ind. So the reallocb below 17857 * can't fail. 17858 */ 17859 freemsg(mp->b_cont); 17860 mp->b_cont = NULL; 17861 ASSERT(DB_REF(mp) == 1); 17862 mp = reallocb(mp, sizeof (struct T_discon_ind), 17863 B_FALSE); 17864 ASSERT(mp != NULL); 17865 DB_TYPE(mp) = M_PROTO; 17866 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17867 tdi = (struct T_discon_ind *)mp->b_rptr; 17868 if (tcp->tcp_issocket) { 17869 tdi->DISCON_reason = ECONNREFUSED; 17870 tdi->SEQ_number = 0; 17871 } else { 17872 tdi->DISCON_reason = ENOPROTOOPT; 17873 tdi->SEQ_number = 17874 tcp->tcp_conn_req_seqnum; 17875 } 17876 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17877 putnext(q, mp); 17878 } else { 17879 freemsg(mp); 17880 } 17881 if (tcp->tcp_hard_binding) { 17882 tcp->tcp_hard_binding = B_FALSE; 17883 tcp->tcp_hard_bound = B_TRUE; 17884 } 17885 tcp->tcp_detached = B_FALSE; 17886 return; 17887 } 17888 17889 mp1 = stropt_mp->b_cont; 17890 stropt_mp->b_cont = NULL; 17891 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17892 stropt = (struct stroptions *)stropt_mp->b_rptr; 17893 17894 while (mp1 != NULL) { 17895 mp = mp1; 17896 mp1 = mp1->b_cont; 17897 mp->b_cont = NULL; 17898 tcp->tcp_drop_opt_ack_cnt++; 17899 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17900 } 17901 mp = NULL; 17902 17903 /* 17904 * For a loopback connection with tcp_direct_sockfs on, note that 17905 * we don't have to protect tcp_rcv_list yet because synchronous 17906 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17907 * possibly race with us. 17908 */ 17909 17910 /* 17911 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17912 * properly. This is the first time we know of the acceptor' 17913 * queue. So we do it here. 17914 */ 17915 if (tcp->tcp_rcv_list == NULL) { 17916 /* 17917 * Recv queue is empty, tcp_rwnd should not have changed. 17918 * That means it should be equal to the listener's tcp_rwnd. 17919 */ 17920 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17921 } else { 17922 #ifdef DEBUG 17923 uint_t cnt = 0; 17924 17925 mp1 = tcp->tcp_rcv_list; 17926 while ((mp = mp1) != NULL) { 17927 mp1 = mp->b_next; 17928 cnt += msgdsize(mp); 17929 } 17930 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17931 #endif 17932 /* There is some data, add them back to get the max. */ 17933 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17934 } 17935 17936 stropt->so_flags = SO_HIWAT; 17937 stropt->so_hiwat = MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 17938 17939 stropt->so_flags |= SO_MAXBLK; 17940 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17941 17942 /* 17943 * This is the first time we run on the correct 17944 * queue after tcp_accept. So fix all the q parameters 17945 * here. 17946 */ 17947 /* Allocate room for SACK options if needed. */ 17948 stropt->so_flags |= SO_WROFF; 17949 if (tcp->tcp_fused) { 17950 ASSERT(tcp->tcp_loopback); 17951 ASSERT(tcp->tcp_loopback_peer != NULL); 17952 /* 17953 * For fused tcp loopback, set the stream head's write 17954 * offset value to zero since we won't be needing any room 17955 * for TCP/IP headers. This would also improve performance 17956 * since it would reduce the amount of work done by kmem. 17957 * Non-fused tcp loopback case is handled separately below. 17958 */ 17959 stropt->so_wroff = 0; 17960 /* 17961 * Record the stream head's high water mark for this endpoint; 17962 * this is used for flow-control purposes in tcp_fuse_output(). 17963 */ 17964 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 17965 /* 17966 * Update the peer's transmit parameters according to 17967 * our recently calculated high water mark value. 17968 */ 17969 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17970 } else if (tcp->tcp_snd_sack_ok) { 17971 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17972 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17973 } else { 17974 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17975 tcps->tcps_wroff_xtra); 17976 } 17977 17978 /* 17979 * If this is endpoint is handling SSL, then reserve extra 17980 * offset and space at the end. 17981 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17982 * overriding the previous setting. The extra cost of signing and 17983 * encrypting multiple MSS-size records (12 of them with Ethernet), 17984 * instead of a single contiguous one by the stream head 17985 * largely outweighs the statistical reduction of ACKs, when 17986 * applicable. The peer will also save on decyption and verification 17987 * costs. 17988 */ 17989 if (tcp->tcp_kssl_ctx != NULL) { 17990 stropt->so_wroff += SSL3_WROFFSET; 17991 17992 stropt->so_flags |= SO_TAIL; 17993 stropt->so_tail = SSL3_MAX_TAIL_LEN; 17994 17995 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 17996 } 17997 17998 /* Send the options up */ 17999 putnext(q, stropt_mp); 18000 18001 /* 18002 * Pass up any data and/or a fin that has been received. 18003 * 18004 * Adjust receive window in case it had decreased 18005 * (because there is data <=> tcp_rcv_list != NULL) 18006 * while the connection was detached. Note that 18007 * in case the eager was flow-controlled, w/o this 18008 * code, the rwnd may never open up again! 18009 */ 18010 if (tcp->tcp_rcv_list != NULL) { 18011 /* We drain directly in case of fused tcp loopback */ 18012 if (!tcp->tcp_fused && canputnext(q)) { 18013 tcp->tcp_rwnd = q->q_hiwat; 18014 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 18015 << tcp->tcp_rcv_ws; 18016 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 18017 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18018 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 18019 tcp_xmit_ctl(NULL, 18020 tcp, (tcp->tcp_swnd == 0) ? 18021 tcp->tcp_suna : tcp->tcp_snxt, 18022 tcp->tcp_rnxt, TH_ACK); 18023 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 18024 } 18025 18026 } 18027 (void) tcp_rcv_drain(q, tcp); 18028 18029 /* 18030 * For fused tcp loopback, back-enable peer endpoint 18031 * if it's currently flow-controlled. 18032 */ 18033 if (tcp->tcp_fused) { 18034 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18035 18036 ASSERT(peer_tcp != NULL); 18037 ASSERT(peer_tcp->tcp_fused); 18038 /* 18039 * In order to change the peer's tcp_flow_stopped, 18040 * we need to take locks for both end points. The 18041 * highest address is taken first. 18042 */ 18043 if (peer_tcp > tcp) { 18044 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18045 mutex_enter(&tcp->tcp_non_sq_lock); 18046 } else { 18047 mutex_enter(&tcp->tcp_non_sq_lock); 18048 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18049 } 18050 if (peer_tcp->tcp_flow_stopped) { 18051 tcp_clrqfull(peer_tcp); 18052 TCP_STAT(tcps, tcp_fusion_backenabled); 18053 } 18054 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18055 mutex_exit(&tcp->tcp_non_sq_lock); 18056 } 18057 } 18058 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18059 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18060 mp = mi_tpi_ordrel_ind(); 18061 if (mp) { 18062 tcp->tcp_ordrel_done = B_TRUE; 18063 putnext(q, mp); 18064 if (tcp->tcp_deferred_clean_death) { 18065 /* 18066 * tcp_clean_death was deferred 18067 * for T_ORDREL_IND - do it now 18068 */ 18069 (void) tcp_clean_death(tcp, 18070 tcp->tcp_client_errno, 21); 18071 tcp->tcp_deferred_clean_death = B_FALSE; 18072 } 18073 } else { 18074 /* 18075 * Run the orderly release in the 18076 * service routine. 18077 */ 18078 qenable(q); 18079 } 18080 } 18081 if (tcp->tcp_hard_binding) { 18082 tcp->tcp_hard_binding = B_FALSE; 18083 tcp->tcp_hard_bound = B_TRUE; 18084 } 18085 18086 tcp->tcp_detached = B_FALSE; 18087 18088 /* We can enable synchronous streams now */ 18089 if (tcp->tcp_fused) { 18090 tcp_fuse_syncstr_enable_pair(tcp); 18091 } 18092 18093 if (tcp->tcp_ka_enabled) { 18094 tcp->tcp_ka_last_intrvl = 0; 18095 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18096 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18097 } 18098 18099 /* 18100 * At this point, eager is fully established and will 18101 * have the following references - 18102 * 18103 * 2 references for connection to exist (1 for TCP and 1 for IP). 18104 * 1 reference for the squeue which will be dropped by the squeue as 18105 * soon as this function returns. 18106 * There will be 1 additonal reference for being in classifier 18107 * hash list provided something bad hasn't happened. 18108 */ 18109 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18110 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18111 } 18112 18113 /* 18114 * The function called through squeue to get behind listener's perimeter to 18115 * send a deffered conn_ind. 18116 */ 18117 /* ARGSUSED */ 18118 void 18119 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18120 { 18121 conn_t *connp = (conn_t *)arg; 18122 tcp_t *listener = connp->conn_tcp; 18123 18124 if (listener->tcp_state == TCPS_CLOSED || 18125 TCP_IS_DETACHED(listener)) { 18126 /* 18127 * If listener has closed, it would have caused a 18128 * a cleanup/blowoff to happen for the eager. 18129 */ 18130 tcp_t *tcp; 18131 struct T_conn_ind *conn_ind; 18132 18133 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18134 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18135 conn_ind->OPT_length); 18136 /* 18137 * We need to drop the ref on eager that was put 18138 * tcp_rput_data() before trying to send the conn_ind 18139 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18140 * and tcp_wput_accept() is sending this deferred conn_ind but 18141 * listener is closed so we drop the ref. 18142 */ 18143 CONN_DEC_REF(tcp->tcp_connp); 18144 freemsg(mp); 18145 return; 18146 } 18147 putnext(listener->tcp_rq, mp); 18148 } 18149 18150 18151 /* 18152 * This is the STREAMS entry point for T_CONN_RES coming down on 18153 * Acceptor STREAM when sockfs listener does accept processing. 18154 * Read the block comment on top of tcp_conn_request(). 18155 */ 18156 void 18157 tcp_wput_accept(queue_t *q, mblk_t *mp) 18158 { 18159 queue_t *rq = RD(q); 18160 struct T_conn_res *conn_res; 18161 tcp_t *eager; 18162 tcp_t *listener; 18163 struct T_ok_ack *ok; 18164 t_scalar_t PRIM_type; 18165 mblk_t *opt_mp; 18166 conn_t *econnp; 18167 18168 ASSERT(DB_TYPE(mp) == M_PROTO); 18169 18170 conn_res = (struct T_conn_res *)mp->b_rptr; 18171 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18172 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18173 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18174 if (mp != NULL) 18175 putnext(rq, mp); 18176 return; 18177 } 18178 switch (conn_res->PRIM_type) { 18179 case O_T_CONN_RES: 18180 case T_CONN_RES: 18181 /* 18182 * We pass up an err ack if allocb fails. This will 18183 * cause sockfs to issue a T_DISCON_REQ which will cause 18184 * tcp_eager_blowoff to be called. sockfs will then call 18185 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18186 * we need to do the allocb up here because we have to 18187 * make sure rq->q_qinfo->qi_qclose still points to the 18188 * correct function (tcpclose_accept) in case allocb 18189 * fails. 18190 */ 18191 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18192 if (opt_mp == NULL) { 18193 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18194 if (mp != NULL) 18195 putnext(rq, mp); 18196 return; 18197 } 18198 18199 bcopy(mp->b_rptr + conn_res->OPT_offset, 18200 &eager, conn_res->OPT_length); 18201 PRIM_type = conn_res->PRIM_type; 18202 mp->b_datap->db_type = M_PCPROTO; 18203 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18204 ok = (struct T_ok_ack *)mp->b_rptr; 18205 ok->PRIM_type = T_OK_ACK; 18206 ok->CORRECT_prim = PRIM_type; 18207 econnp = eager->tcp_connp; 18208 econnp->conn_dev = (dev_t)q->q_ptr; 18209 eager->tcp_rq = rq; 18210 eager->tcp_wq = q; 18211 rq->q_ptr = econnp; 18212 rq->q_qinfo = &tcp_rinit; 18213 q->q_ptr = econnp; 18214 q->q_qinfo = &tcp_winit; 18215 listener = eager->tcp_listener; 18216 eager->tcp_issocket = B_TRUE; 18217 18218 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18219 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18220 ASSERT(econnp->conn_netstack == 18221 listener->tcp_connp->conn_netstack); 18222 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18223 18224 /* Put the ref for IP */ 18225 CONN_INC_REF(econnp); 18226 18227 /* 18228 * We should have minimum of 3 references on the conn 18229 * at this point. One each for TCP and IP and one for 18230 * the T_conn_ind that was sent up when the 3-way handshake 18231 * completed. In the normal case we would also have another 18232 * reference (making a total of 4) for the conn being in the 18233 * classifier hash list. However the eager could have received 18234 * an RST subsequently and tcp_closei_local could have removed 18235 * the eager from the classifier hash list, hence we can't 18236 * assert that reference. 18237 */ 18238 ASSERT(econnp->conn_ref >= 3); 18239 18240 /* 18241 * Send the new local address also up to sockfs. There 18242 * should already be enough space in the mp that came 18243 * down from soaccept(). 18244 */ 18245 if (eager->tcp_family == AF_INET) { 18246 sin_t *sin; 18247 18248 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18249 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18250 sin = (sin_t *)mp->b_wptr; 18251 mp->b_wptr += sizeof (sin_t); 18252 sin->sin_family = AF_INET; 18253 sin->sin_port = eager->tcp_lport; 18254 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18255 } else { 18256 sin6_t *sin6; 18257 18258 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18259 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18260 sin6 = (sin6_t *)mp->b_wptr; 18261 mp->b_wptr += sizeof (sin6_t); 18262 sin6->sin6_family = AF_INET6; 18263 sin6->sin6_port = eager->tcp_lport; 18264 if (eager->tcp_ipversion == IPV4_VERSION) { 18265 sin6->sin6_flowinfo = 0; 18266 IN6_IPADDR_TO_V4MAPPED( 18267 eager->tcp_ipha->ipha_src, 18268 &sin6->sin6_addr); 18269 } else { 18270 ASSERT(eager->tcp_ip6h != NULL); 18271 sin6->sin6_flowinfo = 18272 eager->tcp_ip6h->ip6_vcf & 18273 ~IPV6_VERS_AND_FLOW_MASK; 18274 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18275 } 18276 sin6->sin6_scope_id = 0; 18277 sin6->__sin6_src_id = 0; 18278 } 18279 18280 putnext(rq, mp); 18281 18282 opt_mp->b_datap->db_type = M_SETOPTS; 18283 opt_mp->b_wptr += sizeof (struct stroptions); 18284 18285 /* 18286 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18287 * from listener to acceptor. The message is chained on the 18288 * bind_mp which tcp_rput_other will send down to IP. 18289 */ 18290 if (listener->tcp_bound_if != 0) { 18291 /* allocate optmgmt req */ 18292 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18293 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18294 sizeof (int)); 18295 if (mp != NULL) 18296 linkb(opt_mp, mp); 18297 } 18298 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18299 uint_t on = 1; 18300 18301 /* allocate optmgmt req */ 18302 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18303 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18304 if (mp != NULL) 18305 linkb(opt_mp, mp); 18306 } 18307 18308 18309 mutex_enter(&listener->tcp_eager_lock); 18310 18311 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18312 18313 tcp_t *tail; 18314 tcp_t *tcp; 18315 mblk_t *mp1; 18316 18317 tcp = listener->tcp_eager_prev_q0; 18318 /* 18319 * listener->tcp_eager_prev_q0 points to the TAIL of the 18320 * deferred T_conn_ind queue. We need to get to the head 18321 * of the queue in order to send up T_conn_ind the same 18322 * order as how the 3WHS is completed. 18323 */ 18324 while (tcp != listener) { 18325 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18326 !tcp->tcp_kssl_pending) 18327 break; 18328 else 18329 tcp = tcp->tcp_eager_prev_q0; 18330 } 18331 /* None of the pending eagers can be sent up now */ 18332 if (tcp == listener) 18333 goto no_more_eagers; 18334 18335 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18336 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18337 /* Move from q0 to q */ 18338 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18339 listener->tcp_conn_req_cnt_q0--; 18340 listener->tcp_conn_req_cnt_q++; 18341 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18342 tcp->tcp_eager_prev_q0; 18343 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18344 tcp->tcp_eager_next_q0; 18345 tcp->tcp_eager_prev_q0 = NULL; 18346 tcp->tcp_eager_next_q0 = NULL; 18347 tcp->tcp_conn_def_q0 = B_FALSE; 18348 18349 /* Make sure the tcp isn't in the list of droppables */ 18350 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18351 tcp->tcp_eager_prev_drop_q0 == NULL); 18352 18353 /* 18354 * Insert at end of the queue because sockfs sends 18355 * down T_CONN_RES in chronological order. Leaving 18356 * the older conn indications at front of the queue 18357 * helps reducing search time. 18358 */ 18359 tail = listener->tcp_eager_last_q; 18360 if (tail != NULL) { 18361 tail->tcp_eager_next_q = tcp; 18362 } else { 18363 listener->tcp_eager_next_q = tcp; 18364 } 18365 listener->tcp_eager_last_q = tcp; 18366 tcp->tcp_eager_next_q = NULL; 18367 18368 /* Need to get inside the listener perimeter */ 18369 CONN_INC_REF(listener->tcp_connp); 18370 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18371 tcp_send_pending, listener->tcp_connp, 18372 SQTAG_TCP_SEND_PENDING); 18373 } 18374 no_more_eagers: 18375 tcp_eager_unlink(eager); 18376 mutex_exit(&listener->tcp_eager_lock); 18377 18378 /* 18379 * At this point, the eager is detached from the listener 18380 * but we still have an extra refs on eager (apart from the 18381 * usual tcp references). The ref was placed in tcp_rput_data 18382 * before sending the conn_ind in tcp_send_conn_ind. 18383 * The ref will be dropped in tcp_accept_finish(). 18384 */ 18385 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18386 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18387 return; 18388 default: 18389 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18390 if (mp != NULL) 18391 putnext(rq, mp); 18392 return; 18393 } 18394 } 18395 18396 void 18397 tcp_wput(queue_t *q, mblk_t *mp) 18398 { 18399 conn_t *connp = Q_TO_CONN(q); 18400 tcp_t *tcp; 18401 void (*output_proc)(); 18402 t_scalar_t type; 18403 uchar_t *rptr; 18404 struct iocblk *iocp; 18405 uint32_t msize; 18406 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18407 18408 ASSERT(connp->conn_ref >= 2); 18409 18410 switch (DB_TYPE(mp)) { 18411 case M_DATA: 18412 tcp = connp->conn_tcp; 18413 ASSERT(tcp != NULL); 18414 18415 msize = msgdsize(mp); 18416 18417 mutex_enter(&tcp->tcp_non_sq_lock); 18418 tcp->tcp_squeue_bytes += msize; 18419 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18420 tcp_setqfull(tcp); 18421 } 18422 mutex_exit(&tcp->tcp_non_sq_lock); 18423 18424 CONN_INC_REF(connp); 18425 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18426 tcp_output, connp, SQTAG_TCP_OUTPUT); 18427 return; 18428 case M_PROTO: 18429 case M_PCPROTO: 18430 /* 18431 * if it is a snmp message, don't get behind the squeue 18432 */ 18433 tcp = connp->conn_tcp; 18434 rptr = mp->b_rptr; 18435 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18436 type = ((union T_primitives *)rptr)->type; 18437 } else { 18438 if (tcp->tcp_debug) { 18439 (void) strlog(TCP_MOD_ID, 0, 1, 18440 SL_ERROR|SL_TRACE, 18441 "tcp_wput_proto, dropping one..."); 18442 } 18443 freemsg(mp); 18444 return; 18445 } 18446 if (type == T_SVR4_OPTMGMT_REQ) { 18447 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18448 if (snmpcom_req(q, mp, tcp_snmp_set, tcp_snmp_get, 18449 cr)) { 18450 /* 18451 * This was a SNMP request 18452 */ 18453 return; 18454 } else { 18455 output_proc = tcp_wput_proto; 18456 } 18457 } else { 18458 output_proc = tcp_wput_proto; 18459 } 18460 break; 18461 case M_IOCTL: 18462 /* 18463 * Most ioctls can be processed right away without going via 18464 * squeues - process them right here. Those that do require 18465 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18466 * are processed by tcp_wput_ioctl(). 18467 */ 18468 iocp = (struct iocblk *)mp->b_rptr; 18469 tcp = connp->conn_tcp; 18470 18471 switch (iocp->ioc_cmd) { 18472 case TCP_IOC_ABORT_CONN: 18473 tcp_ioctl_abort_conn(q, mp); 18474 return; 18475 case TI_GETPEERNAME: 18476 if (tcp->tcp_state < TCPS_SYN_RCVD) { 18477 iocp->ioc_error = ENOTCONN; 18478 iocp->ioc_count = 0; 18479 mp->b_datap->db_type = M_IOCACK; 18480 qreply(q, mp); 18481 return; 18482 } 18483 /* FALLTHRU */ 18484 case TI_GETMYNAME: 18485 mi_copyin(q, mp, NULL, 18486 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18487 return; 18488 case ND_SET: 18489 /* nd_getset does the necessary checks */ 18490 case ND_GET: 18491 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18492 CALL_IP_WPUT(connp, q, mp); 18493 return; 18494 } 18495 qreply(q, mp); 18496 return; 18497 case TCP_IOC_DEFAULT_Q: 18498 /* 18499 * Wants to be the default wq. Check the credentials 18500 * first, the rest is executed via squeue. 18501 */ 18502 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18503 iocp->ioc_error = EPERM; 18504 iocp->ioc_count = 0; 18505 mp->b_datap->db_type = M_IOCACK; 18506 qreply(q, mp); 18507 return; 18508 } 18509 output_proc = tcp_wput_ioctl; 18510 break; 18511 default: 18512 output_proc = tcp_wput_ioctl; 18513 break; 18514 } 18515 break; 18516 default: 18517 output_proc = tcp_wput_nondata; 18518 break; 18519 } 18520 18521 CONN_INC_REF(connp); 18522 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18523 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18524 } 18525 18526 /* 18527 * Initial STREAMS write side put() procedure for sockets. It tries to 18528 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18529 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18530 * are handled by tcp_wput() as usual. 18531 * 18532 * All further messages will also be handled by tcp_wput() because we cannot 18533 * be sure that the above short cut is safe later. 18534 */ 18535 static void 18536 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18537 { 18538 conn_t *connp = Q_TO_CONN(wq); 18539 tcp_t *tcp = connp->conn_tcp; 18540 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18541 18542 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18543 wq->q_qinfo = &tcp_winit; 18544 18545 ASSERT(IPCL_IS_TCP(connp)); 18546 ASSERT(TCP_IS_SOCKET(tcp)); 18547 18548 if (DB_TYPE(mp) == M_PCPROTO && 18549 MBLKL(mp) == sizeof (struct T_capability_req) && 18550 car->PRIM_type == T_CAPABILITY_REQ) { 18551 tcp_capability_req(tcp, mp); 18552 return; 18553 } 18554 18555 tcp_wput(wq, mp); 18556 } 18557 18558 static boolean_t 18559 tcp_zcopy_check(tcp_t *tcp) 18560 { 18561 conn_t *connp = tcp->tcp_connp; 18562 ire_t *ire; 18563 boolean_t zc_enabled = B_FALSE; 18564 tcp_stack_t *tcps = tcp->tcp_tcps; 18565 18566 if (do_tcpzcopy == 2) 18567 zc_enabled = B_TRUE; 18568 else if (tcp->tcp_ipversion == IPV4_VERSION && 18569 IPCL_IS_CONNECTED(connp) && 18570 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18571 connp->conn_dontroute == 0 && 18572 !connp->conn_nexthop_set && 18573 connp->conn_xmit_if_ill == NULL && 18574 connp->conn_nofailover_ill == NULL && 18575 do_tcpzcopy == 1) { 18576 /* 18577 * the checks above closely resemble the fast path checks 18578 * in tcp_send_data(). 18579 */ 18580 mutex_enter(&connp->conn_lock); 18581 ire = connp->conn_ire_cache; 18582 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18583 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18584 IRE_REFHOLD(ire); 18585 if (ire->ire_stq != NULL) { 18586 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18587 18588 zc_enabled = ill && (ill->ill_capabilities & 18589 ILL_CAPAB_ZEROCOPY) && 18590 (ill->ill_zerocopy_capab-> 18591 ill_zerocopy_flags != 0); 18592 } 18593 IRE_REFRELE(ire); 18594 } 18595 mutex_exit(&connp->conn_lock); 18596 } 18597 tcp->tcp_snd_zcopy_on = zc_enabled; 18598 if (!TCP_IS_DETACHED(tcp)) { 18599 if (zc_enabled) { 18600 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 18601 TCP_STAT(tcps, tcp_zcopy_on); 18602 } else { 18603 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18604 TCP_STAT(tcps, tcp_zcopy_off); 18605 } 18606 } 18607 return (zc_enabled); 18608 } 18609 18610 static mblk_t * 18611 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18612 { 18613 tcp_stack_t *tcps = tcp->tcp_tcps; 18614 18615 if (do_tcpzcopy == 2) 18616 return (bp); 18617 else if (tcp->tcp_snd_zcopy_on) { 18618 tcp->tcp_snd_zcopy_on = B_FALSE; 18619 if (!TCP_IS_DETACHED(tcp)) { 18620 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18621 TCP_STAT(tcps, tcp_zcopy_disable); 18622 } 18623 } 18624 return (tcp_zcopy_backoff(tcp, bp, 0)); 18625 } 18626 18627 /* 18628 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18629 * the original desballoca'ed segmapped mblk. 18630 */ 18631 static mblk_t * 18632 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18633 { 18634 mblk_t *head, *tail, *nbp; 18635 tcp_stack_t *tcps = tcp->tcp_tcps; 18636 18637 if (IS_VMLOANED_MBLK(bp)) { 18638 TCP_STAT(tcps, tcp_zcopy_backoff); 18639 if ((head = copyb(bp)) == NULL) { 18640 /* fail to backoff; leave it for the next backoff */ 18641 tcp->tcp_xmit_zc_clean = B_FALSE; 18642 return (bp); 18643 } 18644 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18645 if (fix_xmitlist) 18646 tcp_zcopy_notify(tcp); 18647 else 18648 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18649 } 18650 nbp = bp->b_cont; 18651 if (fix_xmitlist) { 18652 head->b_prev = bp->b_prev; 18653 head->b_next = bp->b_next; 18654 if (tcp->tcp_xmit_tail == bp) 18655 tcp->tcp_xmit_tail = head; 18656 } 18657 bp->b_next = NULL; 18658 bp->b_prev = NULL; 18659 freeb(bp); 18660 } else { 18661 head = bp; 18662 nbp = bp->b_cont; 18663 } 18664 tail = head; 18665 while (nbp) { 18666 if (IS_VMLOANED_MBLK(nbp)) { 18667 TCP_STAT(tcps, tcp_zcopy_backoff); 18668 if ((tail->b_cont = copyb(nbp)) == NULL) { 18669 tcp->tcp_xmit_zc_clean = B_FALSE; 18670 tail->b_cont = nbp; 18671 return (head); 18672 } 18673 tail = tail->b_cont; 18674 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18675 if (fix_xmitlist) 18676 tcp_zcopy_notify(tcp); 18677 else 18678 tail->b_datap->db_struioflag |= 18679 STRUIO_ZCNOTIFY; 18680 } 18681 bp = nbp; 18682 nbp = nbp->b_cont; 18683 if (fix_xmitlist) { 18684 tail->b_prev = bp->b_prev; 18685 tail->b_next = bp->b_next; 18686 if (tcp->tcp_xmit_tail == bp) 18687 tcp->tcp_xmit_tail = tail; 18688 } 18689 bp->b_next = NULL; 18690 bp->b_prev = NULL; 18691 freeb(bp); 18692 } else { 18693 tail->b_cont = nbp; 18694 tail = nbp; 18695 nbp = nbp->b_cont; 18696 } 18697 } 18698 if (fix_xmitlist) { 18699 tcp->tcp_xmit_last = tail; 18700 tcp->tcp_xmit_zc_clean = B_TRUE; 18701 } 18702 return (head); 18703 } 18704 18705 static void 18706 tcp_zcopy_notify(tcp_t *tcp) 18707 { 18708 struct stdata *stp; 18709 18710 if (tcp->tcp_detached) 18711 return; 18712 stp = STREAM(tcp->tcp_rq); 18713 mutex_enter(&stp->sd_lock); 18714 stp->sd_flag |= STZCNOTIFY; 18715 cv_broadcast(&stp->sd_zcopy_wait); 18716 mutex_exit(&stp->sd_lock); 18717 } 18718 18719 static boolean_t 18720 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18721 { 18722 ire_t *ire; 18723 conn_t *connp = tcp->tcp_connp; 18724 tcp_stack_t *tcps = tcp->tcp_tcps; 18725 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18726 18727 mutex_enter(&connp->conn_lock); 18728 ire = connp->conn_ire_cache; 18729 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18730 18731 if ((ire != NULL) && 18732 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18733 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18734 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18735 IRE_REFHOLD(ire); 18736 mutex_exit(&connp->conn_lock); 18737 } else { 18738 boolean_t cached = B_FALSE; 18739 ts_label_t *tsl; 18740 18741 /* force a recheck later on */ 18742 tcp->tcp_ire_ill_check_done = B_FALSE; 18743 18744 TCP_DBGSTAT(tcps, tcp_ire_null1); 18745 connp->conn_ire_cache = NULL; 18746 mutex_exit(&connp->conn_lock); 18747 18748 if (ire != NULL) 18749 IRE_REFRELE_NOTR(ire); 18750 18751 tsl = crgetlabel(CONN_CRED(connp)); 18752 ire = (dst ? 18753 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18754 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18755 connp->conn_zoneid, tsl, ipst)); 18756 18757 if (ire == NULL) { 18758 TCP_STAT(tcps, tcp_ire_null); 18759 return (B_FALSE); 18760 } 18761 18762 IRE_REFHOLD_NOTR(ire); 18763 /* 18764 * Since we are inside the squeue, there cannot be another 18765 * thread in TCP trying to set the conn_ire_cache now. The 18766 * check for IRE_MARK_CONDEMNED ensures that an interface 18767 * unplumb thread has not yet started cleaning up the conns. 18768 * Hence we don't need to grab the conn lock. 18769 */ 18770 if (CONN_CACHE_IRE(connp)) { 18771 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18772 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18773 connp->conn_ire_cache = ire; 18774 cached = B_TRUE; 18775 } 18776 rw_exit(&ire->ire_bucket->irb_lock); 18777 } 18778 18779 /* 18780 * We can continue to use the ire but since it was 18781 * not cached, we should drop the extra reference. 18782 */ 18783 if (!cached) 18784 IRE_REFRELE_NOTR(ire); 18785 18786 /* 18787 * Rampart note: no need to select a new label here, since 18788 * labels are not allowed to change during the life of a TCP 18789 * connection. 18790 */ 18791 } 18792 18793 *irep = ire; 18794 18795 return (B_TRUE); 18796 } 18797 18798 /* 18799 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18800 * 18801 * 0 = success; 18802 * 1 = failed to find ire and ill. 18803 */ 18804 static boolean_t 18805 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18806 { 18807 ipha_t *ipha; 18808 ipaddr_t dst; 18809 ire_t *ire; 18810 ill_t *ill; 18811 conn_t *connp = tcp->tcp_connp; 18812 mblk_t *ire_fp_mp; 18813 tcp_stack_t *tcps = tcp->tcp_tcps; 18814 18815 if (mp != NULL) 18816 ipha = (ipha_t *)mp->b_rptr; 18817 else 18818 ipha = tcp->tcp_ipha; 18819 dst = ipha->ipha_dst; 18820 18821 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18822 return (B_FALSE); 18823 18824 if ((ire->ire_flags & RTF_MULTIRT) || 18825 (ire->ire_stq == NULL) || 18826 (ire->ire_nce == NULL) || 18827 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18828 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18829 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18830 TCP_STAT(tcps, tcp_ip_ire_send); 18831 IRE_REFRELE(ire); 18832 return (B_FALSE); 18833 } 18834 18835 ill = ire_to_ill(ire); 18836 if (connp->conn_outgoing_ill != NULL) { 18837 ill_t *conn_outgoing_ill = NULL; 18838 /* 18839 * Choose a good ill in the group to send the packets on. 18840 */ 18841 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18842 ill = ire_to_ill(ire); 18843 } 18844 ASSERT(ill != NULL); 18845 18846 if (!tcp->tcp_ire_ill_check_done) { 18847 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18848 tcp->tcp_ire_ill_check_done = B_TRUE; 18849 } 18850 18851 *irep = ire; 18852 *illp = ill; 18853 18854 return (B_TRUE); 18855 } 18856 18857 static void 18858 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18859 { 18860 ipha_t *ipha; 18861 ipaddr_t src; 18862 ipaddr_t dst; 18863 uint32_t cksum; 18864 ire_t *ire; 18865 uint16_t *up; 18866 ill_t *ill; 18867 conn_t *connp = tcp->tcp_connp; 18868 uint32_t hcksum_txflags = 0; 18869 mblk_t *ire_fp_mp; 18870 uint_t ire_fp_mp_len; 18871 tcp_stack_t *tcps = tcp->tcp_tcps; 18872 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18873 18874 ASSERT(DB_TYPE(mp) == M_DATA); 18875 18876 if (DB_CRED(mp) == NULL) 18877 mblk_setcred(mp, CONN_CRED(connp)); 18878 18879 ipha = (ipha_t *)mp->b_rptr; 18880 src = ipha->ipha_src; 18881 dst = ipha->ipha_dst; 18882 18883 /* 18884 * Drop off fast path for IPv6 and also if options are present or 18885 * we need to resolve a TS label. 18886 */ 18887 if (tcp->tcp_ipversion != IPV4_VERSION || 18888 !IPCL_IS_CONNECTED(connp) || 18889 !CONN_IS_LSO_MD_FASTPATH(connp) || 18890 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18891 !connp->conn_ulp_labeled || 18892 ipha->ipha_ident == IP_HDR_INCLUDED || 18893 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18894 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18895 if (tcp->tcp_snd_zcopy_aware) 18896 mp = tcp_zcopy_disable(tcp, mp); 18897 TCP_STAT(tcps, tcp_ip_send); 18898 CALL_IP_WPUT(connp, q, mp); 18899 return; 18900 } 18901 18902 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18903 if (tcp->tcp_snd_zcopy_aware) 18904 mp = tcp_zcopy_backoff(tcp, mp, 0); 18905 CALL_IP_WPUT(connp, q, mp); 18906 return; 18907 } 18908 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18909 ire_fp_mp_len = MBLKL(ire_fp_mp); 18910 18911 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18912 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18913 #ifndef _BIG_ENDIAN 18914 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18915 #endif 18916 18917 /* 18918 * Check to see if we need to re-enable LSO/MDT for this connection 18919 * because it was previously disabled due to changes in the ill; 18920 * note that by doing it here, this re-enabling only applies when 18921 * the packet is not dispatched through CALL_IP_WPUT(). 18922 * 18923 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18924 * case, since that's how we ended up here. For IPv6, we do the 18925 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18926 */ 18927 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18928 /* 18929 * Restore LSO for this connection, so that next time around 18930 * it is eligible to go through tcp_lsosend() path again. 18931 */ 18932 TCP_STAT(tcps, tcp_lso_enabled); 18933 tcp->tcp_lso = B_TRUE; 18934 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18935 "interface %s\n", (void *)connp, ill->ill_name)); 18936 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18937 /* 18938 * Restore MDT for this connection, so that next time around 18939 * it is eligible to go through tcp_multisend() path again. 18940 */ 18941 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18942 tcp->tcp_mdt = B_TRUE; 18943 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18944 "interface %s\n", (void *)connp, ill->ill_name)); 18945 } 18946 18947 if (tcp->tcp_snd_zcopy_aware) { 18948 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18949 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18950 mp = tcp_zcopy_disable(tcp, mp); 18951 /* 18952 * we shouldn't need to reset ipha as the mp containing 18953 * ipha should never be a zero-copy mp. 18954 */ 18955 } 18956 18957 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18958 ASSERT(ill->ill_hcksum_capab != NULL); 18959 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18960 } 18961 18962 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18963 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18964 18965 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18966 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18967 18968 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18969 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18970 18971 /* Software checksum? */ 18972 if (DB_CKSUMFLAGS(mp) == 0) { 18973 TCP_STAT(tcps, tcp_out_sw_cksum); 18974 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18975 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18976 } 18977 18978 ipha->ipha_fragment_offset_and_flags |= 18979 (uint32_t)htons(ire->ire_frag_flag); 18980 18981 /* Calculate IP header checksum if hardware isn't capable */ 18982 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18983 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18984 ((uint16_t *)ipha)[4]); 18985 } 18986 18987 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18988 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18989 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18990 18991 UPDATE_OB_PKT_COUNT(ire); 18992 ire->ire_last_used_time = lbolt; 18993 18994 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 18995 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 18996 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 18997 ntohs(ipha->ipha_length)); 18998 18999 if (ILL_DLS_CAPABLE(ill)) { 19000 /* 19001 * Send the packet directly to DLD, where it may be queued 19002 * depending on the availability of transmit resources at 19003 * the media layer. 19004 */ 19005 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 19006 } else { 19007 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 19008 DTRACE_PROBE4(ip4__physical__out__start, 19009 ill_t *, NULL, ill_t *, out_ill, 19010 ipha_t *, ipha, mblk_t *, mp); 19011 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19012 ipst->ips_ipv4firewall_physical_out, 19013 NULL, out_ill, ipha, mp, mp, ipst); 19014 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19015 if (mp != NULL) 19016 putnext(ire->ire_stq, mp); 19017 } 19018 IRE_REFRELE(ire); 19019 } 19020 19021 /* 19022 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19023 * if the receiver shrinks the window, i.e. moves the right window to the 19024 * left, the we should not send new data, but should retransmit normally the 19025 * old unacked data between suna and suna + swnd. We might has sent data 19026 * that is now outside the new window, pretend that we didn't send it. 19027 */ 19028 static void 19029 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19030 { 19031 uint32_t snxt = tcp->tcp_snxt; 19032 mblk_t *xmit_tail; 19033 int32_t offset; 19034 19035 ASSERT(shrunk_count > 0); 19036 19037 /* Pretend we didn't send the data outside the window */ 19038 snxt -= shrunk_count; 19039 19040 /* Get the mblk and the offset in it per the shrunk window */ 19041 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19042 19043 ASSERT(xmit_tail != NULL); 19044 19045 /* Reset all the values per the now shrunk window */ 19046 tcp->tcp_snxt = snxt; 19047 tcp->tcp_xmit_tail = xmit_tail; 19048 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19049 offset; 19050 tcp->tcp_unsent += shrunk_count; 19051 19052 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19053 /* 19054 * Make sure the timer is running so that we will probe a zero 19055 * window. 19056 */ 19057 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19058 } 19059 19060 19061 /* 19062 * The TCP normal data output path. 19063 * NOTE: the logic of the fast path is duplicated from this function. 19064 */ 19065 static void 19066 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19067 { 19068 int len; 19069 mblk_t *local_time; 19070 mblk_t *mp1; 19071 uint32_t snxt; 19072 int tail_unsent; 19073 int tcpstate; 19074 int usable = 0; 19075 mblk_t *xmit_tail; 19076 queue_t *q = tcp->tcp_wq; 19077 int32_t mss; 19078 int32_t num_sack_blk = 0; 19079 int32_t tcp_hdr_len; 19080 int32_t tcp_tcp_hdr_len; 19081 int mdt_thres; 19082 int rc; 19083 tcp_stack_t *tcps = tcp->tcp_tcps; 19084 ip_stack_t *ipst; 19085 19086 tcpstate = tcp->tcp_state; 19087 if (mp == NULL) { 19088 /* 19089 * tcp_wput_data() with NULL mp should only be called when 19090 * there is unsent data. 19091 */ 19092 ASSERT(tcp->tcp_unsent > 0); 19093 /* Really tacky... but we need this for detached closes. */ 19094 len = tcp->tcp_unsent; 19095 goto data_null; 19096 } 19097 19098 #if CCS_STATS 19099 wrw_stats.tot.count++; 19100 wrw_stats.tot.bytes += msgdsize(mp); 19101 #endif 19102 ASSERT(mp->b_datap->db_type == M_DATA); 19103 /* 19104 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19105 * or before a connection attempt has begun. 19106 */ 19107 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19108 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19109 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19110 #ifdef DEBUG 19111 cmn_err(CE_WARN, 19112 "tcp_wput_data: data after ordrel, %s", 19113 tcp_display(tcp, NULL, 19114 DISP_ADDR_AND_PORT)); 19115 #else 19116 if (tcp->tcp_debug) { 19117 (void) strlog(TCP_MOD_ID, 0, 1, 19118 SL_TRACE|SL_ERROR, 19119 "tcp_wput_data: data after ordrel, %s\n", 19120 tcp_display(tcp, NULL, 19121 DISP_ADDR_AND_PORT)); 19122 } 19123 #endif /* DEBUG */ 19124 } 19125 if (tcp->tcp_snd_zcopy_aware && 19126 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19127 tcp_zcopy_notify(tcp); 19128 freemsg(mp); 19129 mutex_enter(&tcp->tcp_non_sq_lock); 19130 if (tcp->tcp_flow_stopped && 19131 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19132 tcp_clrqfull(tcp); 19133 } 19134 mutex_exit(&tcp->tcp_non_sq_lock); 19135 return; 19136 } 19137 19138 /* Strip empties */ 19139 for (;;) { 19140 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19141 (uintptr_t)INT_MAX); 19142 len = (int)(mp->b_wptr - mp->b_rptr); 19143 if (len > 0) 19144 break; 19145 mp1 = mp; 19146 mp = mp->b_cont; 19147 freeb(mp1); 19148 if (!mp) { 19149 return; 19150 } 19151 } 19152 19153 /* If we are the first on the list ... */ 19154 if (tcp->tcp_xmit_head == NULL) { 19155 tcp->tcp_xmit_head = mp; 19156 tcp->tcp_xmit_tail = mp; 19157 tcp->tcp_xmit_tail_unsent = len; 19158 } else { 19159 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19160 struct datab *dp; 19161 19162 mp1 = tcp->tcp_xmit_last; 19163 if (len < tcp_tx_pull_len && 19164 (dp = mp1->b_datap)->db_ref == 1 && 19165 dp->db_lim - mp1->b_wptr >= len) { 19166 ASSERT(len > 0); 19167 ASSERT(!mp1->b_cont); 19168 if (len == 1) { 19169 *mp1->b_wptr++ = *mp->b_rptr; 19170 } else { 19171 bcopy(mp->b_rptr, mp1->b_wptr, len); 19172 mp1->b_wptr += len; 19173 } 19174 if (mp1 == tcp->tcp_xmit_tail) 19175 tcp->tcp_xmit_tail_unsent += len; 19176 mp1->b_cont = mp->b_cont; 19177 if (tcp->tcp_snd_zcopy_aware && 19178 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19179 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19180 freeb(mp); 19181 mp = mp1; 19182 } else { 19183 tcp->tcp_xmit_last->b_cont = mp; 19184 } 19185 len += tcp->tcp_unsent; 19186 } 19187 19188 /* Tack on however many more positive length mblks we have */ 19189 if ((mp1 = mp->b_cont) != NULL) { 19190 do { 19191 int tlen; 19192 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19193 (uintptr_t)INT_MAX); 19194 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19195 if (tlen <= 0) { 19196 mp->b_cont = mp1->b_cont; 19197 freeb(mp1); 19198 } else { 19199 len += tlen; 19200 mp = mp1; 19201 } 19202 } while ((mp1 = mp->b_cont) != NULL); 19203 } 19204 tcp->tcp_xmit_last = mp; 19205 tcp->tcp_unsent = len; 19206 19207 if (urgent) 19208 usable = 1; 19209 19210 data_null: 19211 snxt = tcp->tcp_snxt; 19212 xmit_tail = tcp->tcp_xmit_tail; 19213 tail_unsent = tcp->tcp_xmit_tail_unsent; 19214 19215 /* 19216 * Note that tcp_mss has been adjusted to take into account the 19217 * timestamp option if applicable. Because SACK options do not 19218 * appear in every TCP segments and they are of variable lengths, 19219 * they cannot be included in tcp_mss. Thus we need to calculate 19220 * the actual segment length when we need to send a segment which 19221 * includes SACK options. 19222 */ 19223 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19224 int32_t opt_len; 19225 19226 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19227 tcp->tcp_num_sack_blk); 19228 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19229 2 + TCPOPT_HEADER_LEN; 19230 mss = tcp->tcp_mss - opt_len; 19231 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19232 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19233 } else { 19234 mss = tcp->tcp_mss; 19235 tcp_hdr_len = tcp->tcp_hdr_len; 19236 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19237 } 19238 19239 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19240 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19241 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19242 } 19243 if (tcpstate == TCPS_SYN_RCVD) { 19244 /* 19245 * The three-way connection establishment handshake is not 19246 * complete yet. We want to queue the data for transmission 19247 * after entering ESTABLISHED state (RFC793). A jump to 19248 * "done" label effectively leaves data on the queue. 19249 */ 19250 goto done; 19251 } else { 19252 int usable_r; 19253 19254 /* 19255 * In the special case when cwnd is zero, which can only 19256 * happen if the connection is ECN capable, return now. 19257 * New segments is sent using tcp_timer(). The timer 19258 * is set in tcp_rput_data(). 19259 */ 19260 if (tcp->tcp_cwnd == 0) { 19261 /* 19262 * Note that tcp_cwnd is 0 before 3-way handshake is 19263 * finished. 19264 */ 19265 ASSERT(tcp->tcp_ecn_ok || 19266 tcp->tcp_state < TCPS_ESTABLISHED); 19267 return; 19268 } 19269 19270 /* NOTE: trouble if xmitting while SYN not acked? */ 19271 usable_r = snxt - tcp->tcp_suna; 19272 usable_r = tcp->tcp_swnd - usable_r; 19273 19274 /* 19275 * Check if the receiver has shrunk the window. If 19276 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19277 * cannot be set as there is unsent data, so FIN cannot 19278 * be sent out. Otherwise, we need to take into account 19279 * of FIN as it consumes an "invisible" sequence number. 19280 */ 19281 ASSERT(tcp->tcp_fin_sent == 0); 19282 if (usable_r < 0) { 19283 /* 19284 * The receiver has shrunk the window and we have sent 19285 * -usable_r date beyond the window, re-adjust. 19286 * 19287 * If TCP window scaling is enabled, there can be 19288 * round down error as the advertised receive window 19289 * is actually right shifted n bits. This means that 19290 * the lower n bits info is wiped out. It will look 19291 * like the window is shrunk. Do a check here to 19292 * see if the shrunk amount is actually within the 19293 * error in window calculation. If it is, just 19294 * return. Note that this check is inside the 19295 * shrunk window check. This makes sure that even 19296 * though tcp_process_shrunk_swnd() is not called, 19297 * we will stop further processing. 19298 */ 19299 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19300 tcp_process_shrunk_swnd(tcp, -usable_r); 19301 } 19302 return; 19303 } 19304 19305 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19306 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19307 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19308 19309 /* usable = MIN(usable, unsent) */ 19310 if (usable_r > len) 19311 usable_r = len; 19312 19313 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19314 if (usable_r > 0) { 19315 usable = usable_r; 19316 } else { 19317 /* Bypass all other unnecessary processing. */ 19318 goto done; 19319 } 19320 } 19321 19322 local_time = (mblk_t *)lbolt; 19323 19324 /* 19325 * "Our" Nagle Algorithm. This is not the same as in the old 19326 * BSD. This is more in line with the true intent of Nagle. 19327 * 19328 * The conditions are: 19329 * 1. The amount of unsent data (or amount of data which can be 19330 * sent, whichever is smaller) is less than Nagle limit. 19331 * 2. The last sent size is also less than Nagle limit. 19332 * 3. There is unack'ed data. 19333 * 4. Urgent pointer is not set. Send urgent data ignoring the 19334 * Nagle algorithm. This reduces the probability that urgent 19335 * bytes get "merged" together. 19336 * 5. The app has not closed the connection. This eliminates the 19337 * wait time of the receiving side waiting for the last piece of 19338 * (small) data. 19339 * 19340 * If all are satisified, exit without sending anything. Note 19341 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19342 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19343 * 4095). 19344 */ 19345 if (usable < (int)tcp->tcp_naglim && 19346 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19347 snxt != tcp->tcp_suna && 19348 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19349 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19350 goto done; 19351 } 19352 19353 if (tcp->tcp_cork) { 19354 /* 19355 * if the tcp->tcp_cork option is set, then we have to force 19356 * TCP not to send partial segment (smaller than MSS bytes). 19357 * We are calculating the usable now based on full mss and 19358 * will save the rest of remaining data for later. 19359 */ 19360 if (usable < mss) 19361 goto done; 19362 usable = (usable / mss) * mss; 19363 } 19364 19365 /* Update the latest receive window size in TCP header. */ 19366 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19367 tcp->tcp_tcph->th_win); 19368 19369 /* 19370 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19371 * 19372 * 1. Simple TCP/IP{v4,v6} (no options). 19373 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19374 * 3. If the TCP connection is in ESTABLISHED state. 19375 * 4. The TCP is not detached. 19376 * 19377 * If any of the above conditions have changed during the 19378 * connection, stop using LSO/MDT and restore the stream head 19379 * parameters accordingly. 19380 */ 19381 ipst = tcps->tcps_netstack->netstack_ip; 19382 19383 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19384 ((tcp->tcp_ipversion == IPV4_VERSION && 19385 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19386 (tcp->tcp_ipversion == IPV6_VERSION && 19387 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19388 tcp->tcp_state != TCPS_ESTABLISHED || 19389 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19390 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19391 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19392 if (tcp->tcp_lso) { 19393 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19394 tcp->tcp_lso = B_FALSE; 19395 } else { 19396 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19397 tcp->tcp_mdt = B_FALSE; 19398 } 19399 19400 /* Anything other than detached is considered pathological */ 19401 if (!TCP_IS_DETACHED(tcp)) { 19402 if (tcp->tcp_lso) 19403 TCP_STAT(tcps, tcp_lso_disabled); 19404 else 19405 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19406 (void) tcp_maxpsz_set(tcp, B_TRUE); 19407 } 19408 } 19409 19410 /* Use MDT if sendable amount is greater than the threshold */ 19411 if (tcp->tcp_mdt && 19412 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19413 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19414 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19415 (tcp->tcp_valid_bits == 0 || 19416 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19417 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19418 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19419 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19420 local_time, mdt_thres); 19421 } else { 19422 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19423 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19424 local_time, INT_MAX); 19425 } 19426 19427 /* Pretend that all we were trying to send really got sent */ 19428 if (rc < 0 && tail_unsent < 0) { 19429 do { 19430 xmit_tail = xmit_tail->b_cont; 19431 xmit_tail->b_prev = local_time; 19432 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19433 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19434 tail_unsent += (int)(xmit_tail->b_wptr - 19435 xmit_tail->b_rptr); 19436 } while (tail_unsent < 0); 19437 } 19438 done:; 19439 tcp->tcp_xmit_tail = xmit_tail; 19440 tcp->tcp_xmit_tail_unsent = tail_unsent; 19441 len = tcp->tcp_snxt - snxt; 19442 if (len) { 19443 /* 19444 * If new data was sent, need to update the notsack 19445 * list, which is, afterall, data blocks that have 19446 * not been sack'ed by the receiver. New data is 19447 * not sack'ed. 19448 */ 19449 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19450 /* len is a negative value. */ 19451 tcp->tcp_pipe -= len; 19452 tcp_notsack_update(&(tcp->tcp_notsack_list), 19453 tcp->tcp_snxt, snxt, 19454 &(tcp->tcp_num_notsack_blk), 19455 &(tcp->tcp_cnt_notsack_list)); 19456 } 19457 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19458 tcp->tcp_rack = tcp->tcp_rnxt; 19459 tcp->tcp_rack_cnt = 0; 19460 if ((snxt + len) == tcp->tcp_suna) { 19461 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19462 } 19463 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19464 /* 19465 * Didn't send anything. Make sure the timer is running 19466 * so that we will probe a zero window. 19467 */ 19468 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19469 } 19470 /* Note that len is the amount we just sent but with a negative sign */ 19471 tcp->tcp_unsent += len; 19472 mutex_enter(&tcp->tcp_non_sq_lock); 19473 if (tcp->tcp_flow_stopped) { 19474 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19475 tcp_clrqfull(tcp); 19476 } 19477 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19478 tcp_setqfull(tcp); 19479 } 19480 mutex_exit(&tcp->tcp_non_sq_lock); 19481 } 19482 19483 /* 19484 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19485 * outgoing TCP header with the template header, as well as other 19486 * options such as time-stamp, ECN and/or SACK. 19487 */ 19488 static void 19489 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19490 { 19491 tcph_t *tcp_tmpl, *tcp_h; 19492 uint32_t *dst, *src; 19493 int hdrlen; 19494 19495 ASSERT(OK_32PTR(rptr)); 19496 19497 /* Template header */ 19498 tcp_tmpl = tcp->tcp_tcph; 19499 19500 /* Header of outgoing packet */ 19501 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19502 19503 /* dst and src are opaque 32-bit fields, used for copying */ 19504 dst = (uint32_t *)rptr; 19505 src = (uint32_t *)tcp->tcp_iphc; 19506 hdrlen = tcp->tcp_hdr_len; 19507 19508 /* Fill time-stamp option if needed */ 19509 if (tcp->tcp_snd_ts_ok) { 19510 U32_TO_BE32((uint32_t)now, 19511 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19512 U32_TO_BE32(tcp->tcp_ts_recent, 19513 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19514 } else { 19515 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19516 } 19517 19518 /* 19519 * Copy the template header; is this really more efficient than 19520 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19521 * but perhaps not for other scenarios. 19522 */ 19523 dst[0] = src[0]; 19524 dst[1] = src[1]; 19525 dst[2] = src[2]; 19526 dst[3] = src[3]; 19527 dst[4] = src[4]; 19528 dst[5] = src[5]; 19529 dst[6] = src[6]; 19530 dst[7] = src[7]; 19531 dst[8] = src[8]; 19532 dst[9] = src[9]; 19533 if (hdrlen -= 40) { 19534 hdrlen >>= 2; 19535 dst += 10; 19536 src += 10; 19537 do { 19538 *dst++ = *src++; 19539 } while (--hdrlen); 19540 } 19541 19542 /* 19543 * Set the ECN info in the TCP header if it is not a zero 19544 * window probe. Zero window probe is only sent in 19545 * tcp_wput_data() and tcp_timer(). 19546 */ 19547 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19548 SET_ECT(tcp, rptr); 19549 19550 if (tcp->tcp_ecn_echo_on) 19551 tcp_h->th_flags[0] |= TH_ECE; 19552 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19553 tcp_h->th_flags[0] |= TH_CWR; 19554 tcp->tcp_ecn_cwr_sent = B_TRUE; 19555 } 19556 } 19557 19558 /* Fill in SACK options */ 19559 if (num_sack_blk > 0) { 19560 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19561 sack_blk_t *tmp; 19562 int32_t i; 19563 19564 wptr[0] = TCPOPT_NOP; 19565 wptr[1] = TCPOPT_NOP; 19566 wptr[2] = TCPOPT_SACK; 19567 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19568 sizeof (sack_blk_t); 19569 wptr += TCPOPT_REAL_SACK_LEN; 19570 19571 tmp = tcp->tcp_sack_list; 19572 for (i = 0; i < num_sack_blk; i++) { 19573 U32_TO_BE32(tmp[i].begin, wptr); 19574 wptr += sizeof (tcp_seq); 19575 U32_TO_BE32(tmp[i].end, wptr); 19576 wptr += sizeof (tcp_seq); 19577 } 19578 tcp_h->th_offset_and_rsrvd[0] += 19579 ((num_sack_blk * 2 + 1) << 4); 19580 } 19581 } 19582 19583 /* 19584 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19585 * the destination address and SAP attribute, and if necessary, the 19586 * hardware checksum offload attribute to a Multidata message. 19587 */ 19588 static int 19589 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19590 const uint32_t start, const uint32_t stuff, const uint32_t end, 19591 const uint32_t flags, tcp_stack_t *tcps) 19592 { 19593 /* Add global destination address & SAP attribute */ 19594 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19595 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19596 "destination address+SAP\n")); 19597 19598 if (dlmp != NULL) 19599 TCP_STAT(tcps, tcp_mdt_allocfail); 19600 return (-1); 19601 } 19602 19603 /* Add global hwcksum attribute */ 19604 if (hwcksum && 19605 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19606 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19607 "checksum attribute\n")); 19608 19609 TCP_STAT(tcps, tcp_mdt_allocfail); 19610 return (-1); 19611 } 19612 19613 return (0); 19614 } 19615 19616 /* 19617 * Smaller and private version of pdescinfo_t used specifically for TCP, 19618 * which allows for only two payload spans per packet. 19619 */ 19620 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19621 19622 /* 19623 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19624 * scheme, and returns one the following: 19625 * 19626 * -1 = failed allocation. 19627 * 0 = success; burst count reached, or usable send window is too small, 19628 * and that we'd rather wait until later before sending again. 19629 */ 19630 static int 19631 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19632 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19633 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19634 const int mdt_thres) 19635 { 19636 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19637 multidata_t *mmd; 19638 uint_t obsegs, obbytes, hdr_frag_sz; 19639 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19640 int num_burst_seg, max_pld; 19641 pdesc_t *pkt; 19642 tcp_pdescinfo_t tcp_pkt_info; 19643 pdescinfo_t *pkt_info; 19644 int pbuf_idx, pbuf_idx_nxt; 19645 int seg_len, len, spill, af; 19646 boolean_t add_buffer, zcopy, clusterwide; 19647 boolean_t buf_trunked = B_FALSE; 19648 boolean_t rconfirm = B_FALSE; 19649 boolean_t done = B_FALSE; 19650 uint32_t cksum; 19651 uint32_t hwcksum_flags; 19652 ire_t *ire = NULL; 19653 ill_t *ill; 19654 ipha_t *ipha; 19655 ip6_t *ip6h; 19656 ipaddr_t src, dst; 19657 ill_zerocopy_capab_t *zc_cap = NULL; 19658 uint16_t *up; 19659 int err; 19660 conn_t *connp; 19661 mblk_t *mp, *mp1, *fw_mp_head = NULL; 19662 uchar_t *pld_start; 19663 tcp_stack_t *tcps = tcp->tcp_tcps; 19664 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19665 19666 #ifdef _BIG_ENDIAN 19667 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19668 #else 19669 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19670 #endif 19671 19672 #define PREP_NEW_MULTIDATA() { \ 19673 mmd = NULL; \ 19674 md_mp = md_hbuf = NULL; \ 19675 cur_hdr_off = 0; \ 19676 max_pld = tcp->tcp_mdt_max_pld; \ 19677 pbuf_idx = pbuf_idx_nxt = -1; \ 19678 add_buffer = B_TRUE; \ 19679 zcopy = B_FALSE; \ 19680 } 19681 19682 #define PREP_NEW_PBUF() { \ 19683 md_pbuf = md_pbuf_nxt = NULL; \ 19684 pbuf_idx = pbuf_idx_nxt = -1; \ 19685 cur_pld_off = 0; \ 19686 first_snxt = *snxt; \ 19687 ASSERT(*tail_unsent > 0); \ 19688 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19689 } 19690 19691 ASSERT(mdt_thres >= mss); 19692 ASSERT(*usable > 0 && *usable > mdt_thres); 19693 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19694 ASSERT(!TCP_IS_DETACHED(tcp)); 19695 ASSERT(tcp->tcp_valid_bits == 0 || 19696 tcp->tcp_valid_bits == TCP_FSS_VALID); 19697 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19698 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19699 (tcp->tcp_ipversion == IPV6_VERSION && 19700 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19701 19702 connp = tcp->tcp_connp; 19703 ASSERT(connp != NULL); 19704 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19705 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19706 19707 /* 19708 * Note that tcp will only declare at most 2 payload spans per 19709 * packet, which is much lower than the maximum allowable number 19710 * of packet spans per Multidata. For this reason, we use the 19711 * privately declared and smaller descriptor info structure, in 19712 * order to save some stack space. 19713 */ 19714 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19715 19716 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19717 if (af == AF_INET) { 19718 dst = tcp->tcp_ipha->ipha_dst; 19719 src = tcp->tcp_ipha->ipha_src; 19720 ASSERT(!CLASSD(dst)); 19721 } 19722 ASSERT(af == AF_INET || 19723 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19724 19725 obsegs = obbytes = 0; 19726 num_burst_seg = tcp->tcp_snd_burst; 19727 md_mp_head = NULL; 19728 PREP_NEW_MULTIDATA(); 19729 19730 /* 19731 * Before we go on further, make sure there is an IRE that we can 19732 * use, and that the ILL supports MDT. Otherwise, there's no point 19733 * in proceeding any further, and we should just hand everything 19734 * off to the legacy path. 19735 */ 19736 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19737 goto legacy_send_no_md; 19738 19739 ASSERT(ire != NULL); 19740 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19741 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19742 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19743 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19744 /* 19745 * If we do support loopback for MDT (which requires modifications 19746 * to the receiving paths), the following assertions should go away, 19747 * and we would be sending the Multidata to loopback conn later on. 19748 */ 19749 ASSERT(!IRE_IS_LOCAL(ire)); 19750 ASSERT(ire->ire_stq != NULL); 19751 19752 ill = ire_to_ill(ire); 19753 ASSERT(ill != NULL); 19754 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19755 19756 if (!tcp->tcp_ire_ill_check_done) { 19757 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19758 tcp->tcp_ire_ill_check_done = B_TRUE; 19759 } 19760 19761 /* 19762 * If the underlying interface conditions have changed, or if the 19763 * new interface does not support MDT, go back to legacy path. 19764 */ 19765 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19766 /* don't go through this path anymore for this connection */ 19767 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19768 tcp->tcp_mdt = B_FALSE; 19769 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19770 "interface %s\n", (void *)connp, ill->ill_name)); 19771 /* IRE will be released prior to returning */ 19772 goto legacy_send_no_md; 19773 } 19774 19775 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19776 zc_cap = ill->ill_zerocopy_capab; 19777 19778 /* 19779 * Check if we can take tcp fast-path. Note that "incomplete" 19780 * ire's (where the link-layer for next hop is not resolved 19781 * or where the fast-path header in nce_fp_mp is not available 19782 * yet) are sent down the legacy (slow) path. 19783 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19784 */ 19785 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19786 /* IRE will be released prior to returning */ 19787 goto legacy_send_no_md; 19788 } 19789 19790 /* go to legacy path if interface doesn't support zerocopy */ 19791 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19792 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19793 /* IRE will be released prior to returning */ 19794 goto legacy_send_no_md; 19795 } 19796 19797 /* does the interface support hardware checksum offload? */ 19798 hwcksum_flags = 0; 19799 if (ILL_HCKSUM_CAPABLE(ill) && 19800 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19801 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19802 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19803 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19804 HCKSUM_IPHDRCKSUM) 19805 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19806 19807 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19808 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19809 hwcksum_flags |= HCK_FULLCKSUM; 19810 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19811 HCKSUM_INET_PARTIAL) 19812 hwcksum_flags |= HCK_PARTIALCKSUM; 19813 } 19814 19815 /* 19816 * Each header fragment consists of the leading extra space, 19817 * followed by the TCP/IP header, and the trailing extra space. 19818 * We make sure that each header fragment begins on a 32-bit 19819 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19820 * aligned in tcp_mdt_update). 19821 */ 19822 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19823 tcp->tcp_mdt_hdr_tail), 4); 19824 19825 /* are we starting from the beginning of data block? */ 19826 if (*tail_unsent == 0) { 19827 *xmit_tail = (*xmit_tail)->b_cont; 19828 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19829 *tail_unsent = (int)MBLKL(*xmit_tail); 19830 } 19831 19832 /* 19833 * Here we create one or more Multidata messages, each made up of 19834 * one header buffer and up to N payload buffers. This entire 19835 * operation is done within two loops: 19836 * 19837 * The outer loop mostly deals with creating the Multidata message, 19838 * as well as the header buffer that gets added to it. It also 19839 * links the Multidata messages together such that all of them can 19840 * be sent down to the lower layer in a single putnext call; this 19841 * linking behavior depends on the tcp_mdt_chain tunable. 19842 * 19843 * The inner loop takes an existing Multidata message, and adds 19844 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19845 * packetizes those buffers by filling up the corresponding header 19846 * buffer fragments with the proper IP and TCP headers, and by 19847 * describing the layout of each packet in the packet descriptors 19848 * that get added to the Multidata. 19849 */ 19850 do { 19851 /* 19852 * If usable send window is too small, or data blocks in 19853 * transmit list are smaller than our threshold (i.e. app 19854 * performs large writes followed by small ones), we hand 19855 * off the control over to the legacy path. Note that we'll 19856 * get back the control once it encounters a large block. 19857 */ 19858 if (*usable < mss || (*tail_unsent <= mdt_thres && 19859 (*xmit_tail)->b_cont != NULL && 19860 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19861 /* send down what we've got so far */ 19862 if (md_mp_head != NULL) { 19863 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19864 obsegs, obbytes, &rconfirm); 19865 } 19866 /* 19867 * Pass control over to tcp_send(), but tell it to 19868 * return to us once a large-size transmission is 19869 * possible. 19870 */ 19871 TCP_STAT(tcps, tcp_mdt_legacy_small); 19872 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19873 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19874 tail_unsent, xmit_tail, local_time, 19875 mdt_thres)) <= 0) { 19876 /* burst count reached, or alloc failed */ 19877 IRE_REFRELE(ire); 19878 return (err); 19879 } 19880 19881 /* tcp_send() may have sent everything, so check */ 19882 if (*usable <= 0) { 19883 IRE_REFRELE(ire); 19884 return (0); 19885 } 19886 19887 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19888 /* 19889 * We may have delivered the Multidata, so make sure 19890 * to re-initialize before the next round. 19891 */ 19892 md_mp_head = NULL; 19893 obsegs = obbytes = 0; 19894 num_burst_seg = tcp->tcp_snd_burst; 19895 PREP_NEW_MULTIDATA(); 19896 19897 /* are we starting from the beginning of data block? */ 19898 if (*tail_unsent == 0) { 19899 *xmit_tail = (*xmit_tail)->b_cont; 19900 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19901 (uintptr_t)INT_MAX); 19902 *tail_unsent = (int)MBLKL(*xmit_tail); 19903 } 19904 } 19905 19906 /* 19907 * max_pld limits the number of mblks in tcp's transmit 19908 * queue that can be added to a Multidata message. Once 19909 * this counter reaches zero, no more additional mblks 19910 * can be added to it. What happens afterwards depends 19911 * on whether or not we are set to chain the Multidata 19912 * messages. If we are to link them together, reset 19913 * max_pld to its original value (tcp_mdt_max_pld) and 19914 * prepare to create a new Multidata message which will 19915 * get linked to md_mp_head. Else, leave it alone and 19916 * let the inner loop break on its own. 19917 */ 19918 if (tcp_mdt_chain && max_pld == 0) 19919 PREP_NEW_MULTIDATA(); 19920 19921 /* adding a payload buffer; re-initialize values */ 19922 if (add_buffer) 19923 PREP_NEW_PBUF(); 19924 19925 /* 19926 * If we don't have a Multidata, either because we just 19927 * (re)entered this outer loop, or after we branched off 19928 * to tcp_send above, setup the Multidata and header 19929 * buffer to be used. 19930 */ 19931 if (md_mp == NULL) { 19932 int md_hbuflen; 19933 uint32_t start, stuff; 19934 19935 /* 19936 * Calculate Multidata header buffer size large enough 19937 * to hold all of the headers that can possibly be 19938 * sent at this moment. We'd rather over-estimate 19939 * the size than running out of space; this is okay 19940 * since this buffer is small anyway. 19941 */ 19942 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19943 19944 /* 19945 * Start and stuff offset for partial hardware 19946 * checksum offload; these are currently for IPv4. 19947 * For full checksum offload, they are set to zero. 19948 */ 19949 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19950 if (af == AF_INET) { 19951 start = IP_SIMPLE_HDR_LENGTH; 19952 stuff = IP_SIMPLE_HDR_LENGTH + 19953 TCP_CHECKSUM_OFFSET; 19954 } else { 19955 start = IPV6_HDR_LEN; 19956 stuff = IPV6_HDR_LEN + 19957 TCP_CHECKSUM_OFFSET; 19958 } 19959 } else { 19960 start = stuff = 0; 19961 } 19962 19963 /* 19964 * Create the header buffer, Multidata, as well as 19965 * any necessary attributes (destination address, 19966 * SAP and hardware checksum offload) that should 19967 * be associated with the Multidata message. 19968 */ 19969 ASSERT(cur_hdr_off == 0); 19970 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19971 ((md_hbuf->b_wptr += md_hbuflen), 19972 (mmd = mmd_alloc(md_hbuf, &md_mp, 19973 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19974 /* fastpath mblk */ 19975 ire->ire_nce->nce_res_mp, 19976 /* hardware checksum enabled */ 19977 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19978 /* hardware checksum offsets */ 19979 start, stuff, 0, 19980 /* hardware checksum flag */ 19981 hwcksum_flags, tcps) != 0)) { 19982 legacy_send: 19983 if (md_mp != NULL) { 19984 /* Unlink message from the chain */ 19985 if (md_mp_head != NULL) { 19986 err = (intptr_t)rmvb(md_mp_head, 19987 md_mp); 19988 /* 19989 * We can't assert that rmvb 19990 * did not return -1, since we 19991 * may get here before linkb 19992 * happens. We do, however, 19993 * check if we just removed the 19994 * only element in the list. 19995 */ 19996 if (err == 0) 19997 md_mp_head = NULL; 19998 } 19999 /* md_hbuf gets freed automatically */ 20000 TCP_STAT(tcps, tcp_mdt_discarded); 20001 freeb(md_mp); 20002 } else { 20003 /* Either allocb or mmd_alloc failed */ 20004 TCP_STAT(tcps, tcp_mdt_allocfail); 20005 if (md_hbuf != NULL) 20006 freeb(md_hbuf); 20007 } 20008 20009 /* send down what we've got so far */ 20010 if (md_mp_head != NULL) { 20011 tcp_multisend_data(tcp, ire, ill, 20012 md_mp_head, obsegs, obbytes, 20013 &rconfirm); 20014 } 20015 legacy_send_no_md: 20016 if (ire != NULL) 20017 IRE_REFRELE(ire); 20018 /* 20019 * Too bad; let the legacy path handle this. 20020 * We specify INT_MAX for the threshold, since 20021 * we gave up with the Multidata processings 20022 * and let the old path have it all. 20023 */ 20024 TCP_STAT(tcps, tcp_mdt_legacy_all); 20025 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20026 tcp_tcp_hdr_len, num_sack_blk, usable, 20027 snxt, tail_unsent, xmit_tail, local_time, 20028 INT_MAX)); 20029 } 20030 20031 /* link to any existing ones, if applicable */ 20032 TCP_STAT(tcps, tcp_mdt_allocd); 20033 if (md_mp_head == NULL) { 20034 md_mp_head = md_mp; 20035 } else if (tcp_mdt_chain) { 20036 TCP_STAT(tcps, tcp_mdt_linked); 20037 linkb(md_mp_head, md_mp); 20038 } 20039 } 20040 20041 ASSERT(md_mp_head != NULL); 20042 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20043 ASSERT(md_mp != NULL && mmd != NULL); 20044 ASSERT(md_hbuf != NULL); 20045 20046 /* 20047 * Packetize the transmittable portion of the data block; 20048 * each data block is essentially added to the Multidata 20049 * as a payload buffer. We also deal with adding more 20050 * than one payload buffers, which happens when the remaining 20051 * packetized portion of the current payload buffer is less 20052 * than MSS, while the next data block in transmit queue 20053 * has enough data to make up for one. This "spillover" 20054 * case essentially creates a split-packet, where portions 20055 * of the packet's payload fragments may span across two 20056 * virtually discontiguous address blocks. 20057 */ 20058 seg_len = mss; 20059 do { 20060 len = seg_len; 20061 20062 ASSERT(len > 0); 20063 ASSERT(max_pld >= 0); 20064 ASSERT(!add_buffer || cur_pld_off == 0); 20065 20066 /* 20067 * First time around for this payload buffer; note 20068 * in the case of a spillover, the following has 20069 * been done prior to adding the split-packet 20070 * descriptor to Multidata, and we don't want to 20071 * repeat the process. 20072 */ 20073 if (add_buffer) { 20074 ASSERT(mmd != NULL); 20075 ASSERT(md_pbuf == NULL); 20076 ASSERT(md_pbuf_nxt == NULL); 20077 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20078 20079 /* 20080 * Have we reached the limit? We'd get to 20081 * this case when we're not chaining the 20082 * Multidata messages together, and since 20083 * we're done, terminate this loop. 20084 */ 20085 if (max_pld == 0) 20086 break; /* done */ 20087 20088 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20089 TCP_STAT(tcps, tcp_mdt_allocfail); 20090 goto legacy_send; /* out_of_mem */ 20091 } 20092 20093 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20094 zc_cap != NULL) { 20095 if (!ip_md_zcopy_attr(mmd, NULL, 20096 zc_cap->ill_zerocopy_flags)) { 20097 freeb(md_pbuf); 20098 TCP_STAT(tcps, 20099 tcp_mdt_allocfail); 20100 /* out_of_mem */ 20101 goto legacy_send; 20102 } 20103 zcopy = B_TRUE; 20104 } 20105 20106 md_pbuf->b_rptr += base_pld_off; 20107 20108 /* 20109 * Add a payload buffer to the Multidata; this 20110 * operation must not fail, or otherwise our 20111 * logic in this routine is broken. There 20112 * is no memory allocation done by the 20113 * routine, so any returned failure simply 20114 * tells us that we've done something wrong. 20115 * 20116 * A failure tells us that either we're adding 20117 * the same payload buffer more than once, or 20118 * we're trying to add more buffers than 20119 * allowed (max_pld calculation is wrong). 20120 * None of the above cases should happen, and 20121 * we panic because either there's horrible 20122 * heap corruption, and/or programming mistake. 20123 */ 20124 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20125 if (pbuf_idx < 0) { 20126 cmn_err(CE_PANIC, "tcp_multisend: " 20127 "payload buffer logic error " 20128 "detected for tcp %p mmd %p " 20129 "pbuf %p (%d)\n", 20130 (void *)tcp, (void *)mmd, 20131 (void *)md_pbuf, pbuf_idx); 20132 } 20133 20134 ASSERT(max_pld > 0); 20135 --max_pld; 20136 add_buffer = B_FALSE; 20137 } 20138 20139 ASSERT(md_mp_head != NULL); 20140 ASSERT(md_pbuf != NULL); 20141 ASSERT(md_pbuf_nxt == NULL); 20142 ASSERT(pbuf_idx != -1); 20143 ASSERT(pbuf_idx_nxt == -1); 20144 ASSERT(*usable > 0); 20145 20146 /* 20147 * We spillover to the next payload buffer only 20148 * if all of the following is true: 20149 * 20150 * 1. There is not enough data on the current 20151 * payload buffer to make up `len', 20152 * 2. We are allowed to send `len', 20153 * 3. The next payload buffer length is large 20154 * enough to accomodate `spill'. 20155 */ 20156 if ((spill = len - *tail_unsent) > 0 && 20157 *usable >= len && 20158 MBLKL((*xmit_tail)->b_cont) >= spill && 20159 max_pld > 0) { 20160 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20161 if (md_pbuf_nxt == NULL) { 20162 TCP_STAT(tcps, tcp_mdt_allocfail); 20163 goto legacy_send; /* out_of_mem */ 20164 } 20165 20166 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20167 zc_cap != NULL) { 20168 if (!ip_md_zcopy_attr(mmd, NULL, 20169 zc_cap->ill_zerocopy_flags)) { 20170 freeb(md_pbuf_nxt); 20171 TCP_STAT(tcps, 20172 tcp_mdt_allocfail); 20173 /* out_of_mem */ 20174 goto legacy_send; 20175 } 20176 zcopy = B_TRUE; 20177 } 20178 20179 /* 20180 * See comments above on the first call to 20181 * mmd_addpldbuf for explanation on the panic. 20182 */ 20183 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20184 if (pbuf_idx_nxt < 0) { 20185 panic("tcp_multisend: " 20186 "next payload buffer logic error " 20187 "detected for tcp %p mmd %p " 20188 "pbuf %p (%d)\n", 20189 (void *)tcp, (void *)mmd, 20190 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20191 } 20192 20193 ASSERT(max_pld > 0); 20194 --max_pld; 20195 } else if (spill > 0) { 20196 /* 20197 * If there's a spillover, but the following 20198 * xmit_tail couldn't give us enough octets 20199 * to reach "len", then stop the current 20200 * Multidata creation and let the legacy 20201 * tcp_send() path take over. We don't want 20202 * to send the tiny segment as part of this 20203 * Multidata for performance reasons; instead, 20204 * we let the legacy path deal with grouping 20205 * it with the subsequent small mblks. 20206 */ 20207 if (*usable >= len && 20208 MBLKL((*xmit_tail)->b_cont) < spill) { 20209 max_pld = 0; 20210 break; /* done */ 20211 } 20212 20213 /* 20214 * We can't spillover, and we are near 20215 * the end of the current payload buffer, 20216 * so send what's left. 20217 */ 20218 ASSERT(*tail_unsent > 0); 20219 len = *tail_unsent; 20220 } 20221 20222 /* tail_unsent is negated if there is a spillover */ 20223 *tail_unsent -= len; 20224 *usable -= len; 20225 ASSERT(*usable >= 0); 20226 20227 if (*usable < mss) 20228 seg_len = *usable; 20229 /* 20230 * Sender SWS avoidance; see comments in tcp_send(); 20231 * everything else is the same, except that we only 20232 * do this here if there is no more data to be sent 20233 * following the current xmit_tail. We don't check 20234 * for 1-byte urgent data because we shouldn't get 20235 * here if TCP_URG_VALID is set. 20236 */ 20237 if (*usable > 0 && *usable < mss && 20238 ((md_pbuf_nxt == NULL && 20239 (*xmit_tail)->b_cont == NULL) || 20240 (md_pbuf_nxt != NULL && 20241 (*xmit_tail)->b_cont->b_cont == NULL)) && 20242 seg_len < (tcp->tcp_max_swnd >> 1) && 20243 (tcp->tcp_unsent - 20244 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20245 !tcp->tcp_zero_win_probe) { 20246 if ((*snxt + len) == tcp->tcp_snxt && 20247 (*snxt + len) == tcp->tcp_suna) { 20248 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20249 } 20250 done = B_TRUE; 20251 } 20252 20253 /* 20254 * Prime pump for IP's checksumming on our behalf; 20255 * include the adjustment for a source route if any. 20256 * Do this only for software/partial hardware checksum 20257 * offload, as this field gets zeroed out later for 20258 * the full hardware checksum offload case. 20259 */ 20260 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20261 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20262 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20263 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20264 } 20265 20266 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20267 *snxt += len; 20268 20269 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20270 /* 20271 * We set the PUSH bit only if TCP has no more buffered 20272 * data to be transmitted (or if sender SWS avoidance 20273 * takes place), as opposed to setting it for every 20274 * last packet in the burst. 20275 */ 20276 if (done || 20277 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20278 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20279 20280 /* 20281 * Set FIN bit if this is our last segment; snxt 20282 * already includes its length, and it will not 20283 * be adjusted after this point. 20284 */ 20285 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20286 *snxt == tcp->tcp_fss) { 20287 if (!tcp->tcp_fin_acked) { 20288 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20289 BUMP_MIB(&tcps->tcps_mib, 20290 tcpOutControl); 20291 } 20292 if (!tcp->tcp_fin_sent) { 20293 tcp->tcp_fin_sent = B_TRUE; 20294 /* 20295 * tcp state must be ESTABLISHED 20296 * in order for us to get here in 20297 * the first place. 20298 */ 20299 tcp->tcp_state = TCPS_FIN_WAIT_1; 20300 20301 /* 20302 * Upon returning from this routine, 20303 * tcp_wput_data() will set tcp_snxt 20304 * to be equal to snxt + tcp_fin_sent. 20305 * This is essentially the same as 20306 * setting it to tcp_fss + 1. 20307 */ 20308 } 20309 } 20310 20311 tcp->tcp_last_sent_len = (ushort_t)len; 20312 20313 len += tcp_hdr_len; 20314 if (tcp->tcp_ipversion == IPV4_VERSION) 20315 tcp->tcp_ipha->ipha_length = htons(len); 20316 else 20317 tcp->tcp_ip6h->ip6_plen = htons(len - 20318 ((char *)&tcp->tcp_ip6h[1] - 20319 tcp->tcp_iphc)); 20320 20321 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20322 20323 /* setup header fragment */ 20324 PDESC_HDR_ADD(pkt_info, 20325 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20326 tcp->tcp_mdt_hdr_head, /* head room */ 20327 tcp_hdr_len, /* len */ 20328 tcp->tcp_mdt_hdr_tail); /* tail room */ 20329 20330 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20331 hdr_frag_sz); 20332 ASSERT(MBLKIN(md_hbuf, 20333 (pkt_info->hdr_base - md_hbuf->b_rptr), 20334 PDESC_HDRSIZE(pkt_info))); 20335 20336 /* setup first payload fragment */ 20337 PDESC_PLD_INIT(pkt_info); 20338 PDESC_PLD_SPAN_ADD(pkt_info, 20339 pbuf_idx, /* index */ 20340 md_pbuf->b_rptr + cur_pld_off, /* start */ 20341 tcp->tcp_last_sent_len); /* len */ 20342 20343 /* create a split-packet in case of a spillover */ 20344 if (md_pbuf_nxt != NULL) { 20345 ASSERT(spill > 0); 20346 ASSERT(pbuf_idx_nxt > pbuf_idx); 20347 ASSERT(!add_buffer); 20348 20349 md_pbuf = md_pbuf_nxt; 20350 md_pbuf_nxt = NULL; 20351 pbuf_idx = pbuf_idx_nxt; 20352 pbuf_idx_nxt = -1; 20353 cur_pld_off = spill; 20354 20355 /* trim out first payload fragment */ 20356 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20357 20358 /* setup second payload fragment */ 20359 PDESC_PLD_SPAN_ADD(pkt_info, 20360 pbuf_idx, /* index */ 20361 md_pbuf->b_rptr, /* start */ 20362 spill); /* len */ 20363 20364 if ((*xmit_tail)->b_next == NULL) { 20365 /* 20366 * Store the lbolt used for RTT 20367 * estimation. We can only record one 20368 * timestamp per mblk so we do it when 20369 * we reach the end of the payload 20370 * buffer. Also we only take a new 20371 * timestamp sample when the previous 20372 * timed data from the same mblk has 20373 * been ack'ed. 20374 */ 20375 (*xmit_tail)->b_prev = local_time; 20376 (*xmit_tail)->b_next = 20377 (mblk_t *)(uintptr_t)first_snxt; 20378 } 20379 20380 first_snxt = *snxt - spill; 20381 20382 /* 20383 * Advance xmit_tail; usable could be 0 by 20384 * the time we got here, but we made sure 20385 * above that we would only spillover to 20386 * the next data block if usable includes 20387 * the spilled-over amount prior to the 20388 * subtraction. Therefore, we are sure 20389 * that xmit_tail->b_cont can't be NULL. 20390 */ 20391 ASSERT((*xmit_tail)->b_cont != NULL); 20392 *xmit_tail = (*xmit_tail)->b_cont; 20393 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20394 (uintptr_t)INT_MAX); 20395 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20396 } else { 20397 cur_pld_off += tcp->tcp_last_sent_len; 20398 } 20399 20400 /* 20401 * Fill in the header using the template header, and 20402 * add options such as time-stamp, ECN and/or SACK, 20403 * as needed. 20404 */ 20405 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20406 (clock_t)local_time, num_sack_blk); 20407 20408 /* take care of some IP header businesses */ 20409 if (af == AF_INET) { 20410 ipha = (ipha_t *)pkt_info->hdr_rptr; 20411 20412 ASSERT(OK_32PTR((uchar_t *)ipha)); 20413 ASSERT(PDESC_HDRL(pkt_info) >= 20414 IP_SIMPLE_HDR_LENGTH); 20415 ASSERT(ipha->ipha_version_and_hdr_length == 20416 IP_SIMPLE_HDR_VERSION); 20417 20418 /* 20419 * Assign ident value for current packet; see 20420 * related comments in ip_wput_ire() about the 20421 * contract private interface with clustering 20422 * group. 20423 */ 20424 clusterwide = B_FALSE; 20425 if (cl_inet_ipident != NULL) { 20426 ASSERT(cl_inet_isclusterwide != NULL); 20427 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20428 AF_INET, 20429 (uint8_t *)(uintptr_t)src)) { 20430 ipha->ipha_ident = 20431 (*cl_inet_ipident) 20432 (IPPROTO_IP, AF_INET, 20433 (uint8_t *)(uintptr_t)src, 20434 (uint8_t *)(uintptr_t)dst); 20435 clusterwide = B_TRUE; 20436 } 20437 } 20438 20439 if (!clusterwide) { 20440 ipha->ipha_ident = (uint16_t) 20441 atomic_add_32_nv( 20442 &ire->ire_ident, 1); 20443 } 20444 #ifndef _BIG_ENDIAN 20445 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20446 (ipha->ipha_ident >> 8); 20447 #endif 20448 } else { 20449 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20450 20451 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20452 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20453 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20454 ASSERT(PDESC_HDRL(pkt_info) >= 20455 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20456 TCP_CHECKSUM_SIZE)); 20457 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20458 20459 if (tcp->tcp_ip_forward_progress) { 20460 rconfirm = B_TRUE; 20461 tcp->tcp_ip_forward_progress = B_FALSE; 20462 } 20463 } 20464 20465 /* at least one payload span, and at most two */ 20466 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20467 20468 /* add the packet descriptor to Multidata */ 20469 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20470 KM_NOSLEEP)) == NULL) { 20471 /* 20472 * Any failure other than ENOMEM indicates 20473 * that we have passed in invalid pkt_info 20474 * or parameters to mmd_addpdesc, which must 20475 * not happen. 20476 * 20477 * EINVAL is a result of failure on boundary 20478 * checks against the pkt_info contents. It 20479 * should not happen, and we panic because 20480 * either there's horrible heap corruption, 20481 * and/or programming mistake. 20482 */ 20483 if (err != ENOMEM) { 20484 cmn_err(CE_PANIC, "tcp_multisend: " 20485 "pdesc logic error detected for " 20486 "tcp %p mmd %p pinfo %p (%d)\n", 20487 (void *)tcp, (void *)mmd, 20488 (void *)pkt_info, err); 20489 } 20490 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20491 goto legacy_send; /* out_of_mem */ 20492 } 20493 ASSERT(pkt != NULL); 20494 20495 /* calculate IP header and TCP checksums */ 20496 if (af == AF_INET) { 20497 /* calculate pseudo-header checksum */ 20498 cksum = (dst >> 16) + (dst & 0xFFFF) + 20499 (src >> 16) + (src & 0xFFFF); 20500 20501 /* offset for TCP header checksum */ 20502 up = IPH_TCPH_CHECKSUMP(ipha, 20503 IP_SIMPLE_HDR_LENGTH); 20504 } else { 20505 up = (uint16_t *)&ip6h->ip6_src; 20506 20507 /* calculate pseudo-header checksum */ 20508 cksum = up[0] + up[1] + up[2] + up[3] + 20509 up[4] + up[5] + up[6] + up[7] + 20510 up[8] + up[9] + up[10] + up[11] + 20511 up[12] + up[13] + up[14] + up[15]; 20512 20513 /* Fold the initial sum */ 20514 cksum = (cksum & 0xffff) + (cksum >> 16); 20515 20516 up = (uint16_t *)(((uchar_t *)ip6h) + 20517 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20518 } 20519 20520 if (hwcksum_flags & HCK_FULLCKSUM) { 20521 /* clear checksum field for hardware */ 20522 *up = 0; 20523 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20524 uint32_t sum; 20525 20526 /* pseudo-header checksumming */ 20527 sum = *up + cksum + IP_TCP_CSUM_COMP; 20528 sum = (sum & 0xFFFF) + (sum >> 16); 20529 *up = (sum & 0xFFFF) + (sum >> 16); 20530 } else { 20531 /* software checksumming */ 20532 TCP_STAT(tcps, tcp_out_sw_cksum); 20533 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20534 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20535 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20536 cksum + IP_TCP_CSUM_COMP); 20537 if (*up == 0) 20538 *up = 0xFFFF; 20539 } 20540 20541 /* IPv4 header checksum */ 20542 if (af == AF_INET) { 20543 ipha->ipha_fragment_offset_and_flags |= 20544 (uint32_t)htons(ire->ire_frag_flag); 20545 20546 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20547 ipha->ipha_hdr_checksum = 0; 20548 } else { 20549 IP_HDR_CKSUM(ipha, cksum, 20550 ((uint32_t *)ipha)[0], 20551 ((uint16_t *)ipha)[4]); 20552 } 20553 } 20554 20555 if (af == AF_INET && 20556 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20557 af == AF_INET6 && 20558 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20559 /* build header(IP/TCP) mblk for this segment */ 20560 if ((mp = dupb(md_hbuf)) == NULL) 20561 goto legacy_send; 20562 20563 mp->b_rptr = pkt_info->hdr_rptr; 20564 mp->b_wptr = pkt_info->hdr_wptr; 20565 20566 /* build payload mblk for this segment */ 20567 if ((mp1 = dupb(*xmit_tail)) == NULL) { 20568 freemsg(mp); 20569 goto legacy_send; 20570 } 20571 mp1->b_wptr = md_pbuf->b_rptr + cur_pld_off; 20572 mp1->b_rptr = mp1->b_wptr - 20573 tcp->tcp_last_sent_len; 20574 linkb(mp, mp1); 20575 20576 pld_start = mp1->b_rptr; 20577 20578 if (af == AF_INET) { 20579 DTRACE_PROBE4( 20580 ip4__physical__out__start, 20581 ill_t *, NULL, 20582 ill_t *, ill, 20583 ipha_t *, ipha, 20584 mblk_t *, mp); 20585 FW_HOOKS( 20586 ipst->ips_ip4_physical_out_event, 20587 ipst->ips_ipv4firewall_physical_out, 20588 NULL, ill, ipha, mp, mp, ipst); 20589 DTRACE_PROBE1( 20590 ip4__physical__out__end, 20591 mblk_t *, mp); 20592 } else { 20593 DTRACE_PROBE4( 20594 ip6__physical__out_start, 20595 ill_t *, NULL, 20596 ill_t *, ill, 20597 ip6_t *, ip6h, 20598 mblk_t *, mp); 20599 FW_HOOKS6( 20600 ipst->ips_ip6_physical_out_event, 20601 ipst->ips_ipv6firewall_physical_out, 20602 NULL, ill, ip6h, mp, mp, ipst); 20603 DTRACE_PROBE1( 20604 ip6__physical__out__end, 20605 mblk_t *, mp); 20606 } 20607 20608 if (buf_trunked && mp != NULL) { 20609 /* 20610 * Need to pass it to normal path. 20611 */ 20612 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20613 } else if (mp == NULL || 20614 mp->b_rptr != pkt_info->hdr_rptr || 20615 mp->b_wptr != pkt_info->hdr_wptr || 20616 (mp1 = mp->b_cont) == NULL || 20617 mp1->b_rptr != pld_start || 20618 mp1->b_wptr != pld_start + 20619 tcp->tcp_last_sent_len || 20620 mp1->b_cont != NULL) { 20621 /* 20622 * Need to pass all packets of this 20623 * buffer to normal path, either when 20624 * packet is blocked, or when boundary 20625 * of header buffer or payload buffer 20626 * has been changed by FW_HOOKS[6]. 20627 */ 20628 buf_trunked = B_TRUE; 20629 if (md_mp_head != NULL) { 20630 err = (intptr_t)rmvb(md_mp_head, 20631 md_mp); 20632 if (err == 0) 20633 md_mp_head = NULL; 20634 } 20635 20636 /* send down what we've got so far */ 20637 if (md_mp_head != NULL) { 20638 tcp_multisend_data(tcp, ire, 20639 ill, md_mp_head, obsegs, 20640 obbytes, &rconfirm); 20641 } 20642 md_mp_head = NULL; 20643 20644 if (mp != NULL) 20645 CALL_IP_WPUT(tcp->tcp_connp, 20646 q, mp); 20647 20648 mp1 = fw_mp_head; 20649 do { 20650 mp = mp1; 20651 mp1 = mp1->b_next; 20652 mp->b_next = NULL; 20653 mp->b_prev = NULL; 20654 CALL_IP_WPUT(tcp->tcp_connp, 20655 q, mp); 20656 } while (mp1 != NULL); 20657 20658 fw_mp_head = NULL; 20659 } else { 20660 if (fw_mp_head == NULL) 20661 fw_mp_head = mp; 20662 else 20663 fw_mp_head->b_prev->b_next = mp; 20664 fw_mp_head->b_prev = mp; 20665 } 20666 } 20667 20668 /* advance header offset */ 20669 cur_hdr_off += hdr_frag_sz; 20670 20671 obbytes += tcp->tcp_last_sent_len; 20672 ++obsegs; 20673 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20674 *tail_unsent > 0); 20675 20676 if ((*xmit_tail)->b_next == NULL) { 20677 /* 20678 * Store the lbolt used for RTT estimation. We can only 20679 * record one timestamp per mblk so we do it when we 20680 * reach the end of the payload buffer. Also we only 20681 * take a new timestamp sample when the previous timed 20682 * data from the same mblk has been ack'ed. 20683 */ 20684 (*xmit_tail)->b_prev = local_time; 20685 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20686 } 20687 20688 ASSERT(*tail_unsent >= 0); 20689 if (*tail_unsent > 0) { 20690 /* 20691 * We got here because we broke out of the above 20692 * loop due to of one of the following cases: 20693 * 20694 * 1. len < adjusted MSS (i.e. small), 20695 * 2. Sender SWS avoidance, 20696 * 3. max_pld is zero. 20697 * 20698 * We are done for this Multidata, so trim our 20699 * last payload buffer (if any) accordingly. 20700 */ 20701 if (md_pbuf != NULL) 20702 md_pbuf->b_wptr -= *tail_unsent; 20703 } else if (*usable > 0) { 20704 *xmit_tail = (*xmit_tail)->b_cont; 20705 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20706 (uintptr_t)INT_MAX); 20707 *tail_unsent = (int)MBLKL(*xmit_tail); 20708 add_buffer = B_TRUE; 20709 } 20710 20711 while (fw_mp_head) { 20712 mp = fw_mp_head; 20713 fw_mp_head = fw_mp_head->b_next; 20714 mp->b_prev = mp->b_next = NULL; 20715 freemsg(mp); 20716 } 20717 if (buf_trunked) { 20718 TCP_STAT(tcps, tcp_mdt_discarded); 20719 freeb(md_mp); 20720 buf_trunked = B_FALSE; 20721 } 20722 } while (!done && *usable > 0 && num_burst_seg > 0 && 20723 (tcp_mdt_chain || max_pld > 0)); 20724 20725 if (md_mp_head != NULL) { 20726 /* send everything down */ 20727 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20728 &rconfirm); 20729 } 20730 20731 #undef PREP_NEW_MULTIDATA 20732 #undef PREP_NEW_PBUF 20733 #undef IPVER 20734 20735 IRE_REFRELE(ire); 20736 return (0); 20737 } 20738 20739 /* 20740 * A wrapper function for sending one or more Multidata messages down to 20741 * the module below ip; this routine does not release the reference of the 20742 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20743 */ 20744 static void 20745 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20746 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20747 { 20748 uint64_t delta; 20749 nce_t *nce; 20750 tcp_stack_t *tcps = tcp->tcp_tcps; 20751 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20752 20753 ASSERT(ire != NULL && ill != NULL); 20754 ASSERT(ire->ire_stq != NULL); 20755 ASSERT(md_mp_head != NULL); 20756 ASSERT(rconfirm != NULL); 20757 20758 /* adjust MIBs and IRE timestamp */ 20759 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 20760 tcp->tcp_obsegs += obsegs; 20761 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20762 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20763 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20764 20765 if (tcp->tcp_ipversion == IPV4_VERSION) { 20766 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20767 } else { 20768 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20769 } 20770 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20771 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20772 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20773 20774 ire->ire_ob_pkt_count += obsegs; 20775 if (ire->ire_ipif != NULL) 20776 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20777 ire->ire_last_used_time = lbolt; 20778 20779 /* send it down */ 20780 putnext(ire->ire_stq, md_mp_head); 20781 20782 /* we're done for TCP/IPv4 */ 20783 if (tcp->tcp_ipversion == IPV4_VERSION) 20784 return; 20785 20786 nce = ire->ire_nce; 20787 20788 ASSERT(nce != NULL); 20789 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20790 ASSERT(nce->nce_state != ND_INCOMPLETE); 20791 20792 /* reachability confirmation? */ 20793 if (*rconfirm) { 20794 nce->nce_last = TICK_TO_MSEC(lbolt64); 20795 if (nce->nce_state != ND_REACHABLE) { 20796 mutex_enter(&nce->nce_lock); 20797 nce->nce_state = ND_REACHABLE; 20798 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20799 mutex_exit(&nce->nce_lock); 20800 (void) untimeout(nce->nce_timeout_id); 20801 if (ip_debug > 2) { 20802 /* ip1dbg */ 20803 pr_addr_dbg("tcp_multisend_data: state " 20804 "for %s changed to REACHABLE\n", 20805 AF_INET6, &ire->ire_addr_v6); 20806 } 20807 } 20808 /* reset transport reachability confirmation */ 20809 *rconfirm = B_FALSE; 20810 } 20811 20812 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20813 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20814 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20815 20816 if (delta > (uint64_t)ill->ill_reachable_time) { 20817 mutex_enter(&nce->nce_lock); 20818 switch (nce->nce_state) { 20819 case ND_REACHABLE: 20820 case ND_STALE: 20821 /* 20822 * ND_REACHABLE is identical to ND_STALE in this 20823 * specific case. If reachable time has expired for 20824 * this neighbor (delta is greater than reachable 20825 * time), conceptually, the neighbor cache is no 20826 * longer in REACHABLE state, but already in STALE 20827 * state. So the correct transition here is to 20828 * ND_DELAY. 20829 */ 20830 nce->nce_state = ND_DELAY; 20831 mutex_exit(&nce->nce_lock); 20832 NDP_RESTART_TIMER(nce, 20833 ipst->ips_delay_first_probe_time); 20834 if (ip_debug > 3) { 20835 /* ip2dbg */ 20836 pr_addr_dbg("tcp_multisend_data: state " 20837 "for %s changed to DELAY\n", 20838 AF_INET6, &ire->ire_addr_v6); 20839 } 20840 break; 20841 case ND_DELAY: 20842 case ND_PROBE: 20843 mutex_exit(&nce->nce_lock); 20844 /* Timers have already started */ 20845 break; 20846 case ND_UNREACHABLE: 20847 /* 20848 * ndp timer has detected that this nce is 20849 * unreachable and initiated deleting this nce 20850 * and all its associated IREs. This is a race 20851 * where we found the ire before it was deleted 20852 * and have just sent out a packet using this 20853 * unreachable nce. 20854 */ 20855 mutex_exit(&nce->nce_lock); 20856 break; 20857 default: 20858 ASSERT(0); 20859 } 20860 } 20861 } 20862 20863 /* 20864 * Derived from tcp_send_data(). 20865 */ 20866 static void 20867 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20868 int num_lso_seg) 20869 { 20870 ipha_t *ipha; 20871 mblk_t *ire_fp_mp; 20872 uint_t ire_fp_mp_len; 20873 uint32_t hcksum_txflags = 0; 20874 ipaddr_t src; 20875 ipaddr_t dst; 20876 uint32_t cksum; 20877 uint16_t *up; 20878 tcp_stack_t *tcps = tcp->tcp_tcps; 20879 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20880 20881 ASSERT(DB_TYPE(mp) == M_DATA); 20882 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20883 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20884 ASSERT(tcp->tcp_connp != NULL); 20885 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20886 20887 ipha = (ipha_t *)mp->b_rptr; 20888 src = ipha->ipha_src; 20889 dst = ipha->ipha_dst; 20890 20891 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20892 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20893 num_lso_seg); 20894 #ifndef _BIG_ENDIAN 20895 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20896 #endif 20897 if (tcp->tcp_snd_zcopy_aware) { 20898 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20899 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20900 mp = tcp_zcopy_disable(tcp, mp); 20901 } 20902 20903 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20904 ASSERT(ill->ill_hcksum_capab != NULL); 20905 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20906 } 20907 20908 /* 20909 * Since the TCP checksum should be recalculated by h/w, we can just 20910 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20911 * pseudo-header checksum for HCK_PARTIALCKSUM. 20912 * The partial pseudo-header excludes TCP length, that was calculated 20913 * in tcp_send(), so to zero *up before further processing. 20914 */ 20915 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20916 20917 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20918 *up = 0; 20919 20920 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20921 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20922 20923 /* 20924 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 20925 */ 20926 DB_LSOFLAGS(mp) |= HW_LSO; 20927 DB_LSOMSS(mp) = mss; 20928 20929 ipha->ipha_fragment_offset_and_flags |= 20930 (uint32_t)htons(ire->ire_frag_flag); 20931 20932 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20933 ire_fp_mp_len = MBLKL(ire_fp_mp); 20934 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20935 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20936 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20937 20938 UPDATE_OB_PKT_COUNT(ire); 20939 ire->ire_last_used_time = lbolt; 20940 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20941 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20942 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20943 ntohs(ipha->ipha_length)); 20944 20945 if (ILL_DLS_CAPABLE(ill)) { 20946 /* 20947 * Send the packet directly to DLD, where it may be queued 20948 * depending on the availability of transmit resources at 20949 * the media layer. 20950 */ 20951 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 20952 } else { 20953 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 20954 DTRACE_PROBE4(ip4__physical__out__start, 20955 ill_t *, NULL, ill_t *, out_ill, 20956 ipha_t *, ipha, mblk_t *, mp); 20957 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20958 ipst->ips_ipv4firewall_physical_out, 20959 NULL, out_ill, ipha, mp, mp, ipst); 20960 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20961 if (mp != NULL) 20962 putnext(ire->ire_stq, mp); 20963 } 20964 } 20965 20966 /* 20967 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20968 * scheme, and returns one of the following: 20969 * 20970 * -1 = failed allocation. 20971 * 0 = success; burst count reached, or usable send window is too small, 20972 * and that we'd rather wait until later before sending again. 20973 * 1 = success; we are called from tcp_multisend(), and both usable send 20974 * window and tail_unsent are greater than the MDT threshold, and thus 20975 * Multidata Transmit should be used instead. 20976 */ 20977 static int 20978 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20979 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20980 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20981 const int mdt_thres) 20982 { 20983 int num_burst_seg = tcp->tcp_snd_burst; 20984 ire_t *ire = NULL; 20985 ill_t *ill = NULL; 20986 mblk_t *ire_fp_mp = NULL; 20987 uint_t ire_fp_mp_len = 0; 20988 int num_lso_seg = 1; 20989 uint_t lso_usable; 20990 boolean_t do_lso_send = B_FALSE; 20991 tcp_stack_t *tcps = tcp->tcp_tcps; 20992 20993 /* 20994 * Check LSO capability before any further work. And the similar check 20995 * need to be done in for(;;) loop. 20996 * LSO will be deployed when therer is more than one mss of available 20997 * data and a burst transmission is allowed. 20998 */ 20999 if (tcp->tcp_lso && 21000 (tcp->tcp_valid_bits == 0 || 21001 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21002 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21003 /* 21004 * Try to find usable IRE/ILL and do basic check to the ILL. 21005 */ 21006 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21007 /* 21008 * Enable LSO with this transmission. 21009 * Since IRE has been hold in 21010 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21011 * should be called before return. 21012 */ 21013 do_lso_send = B_TRUE; 21014 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21015 ire_fp_mp_len = MBLKL(ire_fp_mp); 21016 /* Round up to multiple of 4 */ 21017 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21018 } else { 21019 do_lso_send = B_FALSE; 21020 ill = NULL; 21021 } 21022 } 21023 21024 for (;;) { 21025 struct datab *db; 21026 tcph_t *tcph; 21027 uint32_t sum; 21028 mblk_t *mp, *mp1; 21029 uchar_t *rptr; 21030 int len; 21031 21032 /* 21033 * If we're called by tcp_multisend(), and the amount of 21034 * sendable data as well as the size of current xmit_tail 21035 * is beyond the MDT threshold, return to the caller and 21036 * let the large data transmit be done using MDT. 21037 */ 21038 if (*usable > 0 && *usable > mdt_thres && 21039 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21040 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21041 ASSERT(tcp->tcp_mdt); 21042 return (1); /* success; do large send */ 21043 } 21044 21045 if (num_burst_seg == 0) 21046 break; /* success; burst count reached */ 21047 21048 /* 21049 * Calculate the maximum payload length we can send in *one* 21050 * time. 21051 */ 21052 if (do_lso_send) { 21053 /* 21054 * Check whether need to do LSO any more. 21055 */ 21056 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21057 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21058 lso_usable = MIN(lso_usable, 21059 num_burst_seg * mss); 21060 21061 num_lso_seg = lso_usable / mss; 21062 if (lso_usable % mss) { 21063 num_lso_seg++; 21064 tcp->tcp_last_sent_len = (ushort_t) 21065 (lso_usable % mss); 21066 } else { 21067 tcp->tcp_last_sent_len = (ushort_t)mss; 21068 } 21069 } else { 21070 do_lso_send = B_FALSE; 21071 num_lso_seg = 1; 21072 lso_usable = mss; 21073 } 21074 } 21075 21076 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21077 21078 /* 21079 * Adjust num_burst_seg here. 21080 */ 21081 num_burst_seg -= num_lso_seg; 21082 21083 len = mss; 21084 if (len > *usable) { 21085 ASSERT(do_lso_send == B_FALSE); 21086 21087 len = *usable; 21088 if (len <= 0) { 21089 /* Terminate the loop */ 21090 break; /* success; too small */ 21091 } 21092 /* 21093 * Sender silly-window avoidance. 21094 * Ignore this if we are going to send a 21095 * zero window probe out. 21096 * 21097 * TODO: force data into microscopic window? 21098 * ==> (!pushed || (unsent > usable)) 21099 */ 21100 if (len < (tcp->tcp_max_swnd >> 1) && 21101 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21102 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21103 len == 1) && (! tcp->tcp_zero_win_probe)) { 21104 /* 21105 * If the retransmit timer is not running 21106 * we start it so that we will retransmit 21107 * in the case when the the receiver has 21108 * decremented the window. 21109 */ 21110 if (*snxt == tcp->tcp_snxt && 21111 *snxt == tcp->tcp_suna) { 21112 /* 21113 * We are not supposed to send 21114 * anything. So let's wait a little 21115 * bit longer before breaking SWS 21116 * avoidance. 21117 * 21118 * What should the value be? 21119 * Suggestion: MAX(init rexmit time, 21120 * tcp->tcp_rto) 21121 */ 21122 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21123 } 21124 break; /* success; too small */ 21125 } 21126 } 21127 21128 tcph = tcp->tcp_tcph; 21129 21130 /* 21131 * The reason to adjust len here is that we need to set flags 21132 * and calculate checksum. 21133 */ 21134 if (do_lso_send) 21135 len = lso_usable; 21136 21137 *usable -= len; /* Approximate - can be adjusted later */ 21138 if (*usable > 0) 21139 tcph->th_flags[0] = TH_ACK; 21140 else 21141 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21142 21143 /* 21144 * Prime pump for IP's checksumming on our behalf 21145 * Include the adjustment for a source route if any. 21146 */ 21147 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21148 sum = (sum >> 16) + (sum & 0xFFFF); 21149 U16_TO_ABE16(sum, tcph->th_sum); 21150 21151 U32_TO_ABE32(*snxt, tcph->th_seq); 21152 21153 /* 21154 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21155 * set. For the case when TCP_FSS_VALID is the only valid 21156 * bit (normal active close), branch off only when we think 21157 * that the FIN flag needs to be set. Note for this case, 21158 * that (snxt + len) may not reflect the actual seg_len, 21159 * as len may be further reduced in tcp_xmit_mp(). If len 21160 * gets modified, we will end up here again. 21161 */ 21162 if (tcp->tcp_valid_bits != 0 && 21163 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21164 ((*snxt + len) == tcp->tcp_fss))) { 21165 uchar_t *prev_rptr; 21166 uint32_t prev_snxt = tcp->tcp_snxt; 21167 21168 if (*tail_unsent == 0) { 21169 ASSERT((*xmit_tail)->b_cont != NULL); 21170 *xmit_tail = (*xmit_tail)->b_cont; 21171 prev_rptr = (*xmit_tail)->b_rptr; 21172 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21173 (*xmit_tail)->b_rptr); 21174 } else { 21175 prev_rptr = (*xmit_tail)->b_rptr; 21176 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21177 *tail_unsent; 21178 } 21179 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21180 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21181 /* Restore tcp_snxt so we get amount sent right. */ 21182 tcp->tcp_snxt = prev_snxt; 21183 if (prev_rptr == (*xmit_tail)->b_rptr) { 21184 /* 21185 * If the previous timestamp is still in use, 21186 * don't stomp on it. 21187 */ 21188 if ((*xmit_tail)->b_next == NULL) { 21189 (*xmit_tail)->b_prev = local_time; 21190 (*xmit_tail)->b_next = 21191 (mblk_t *)(uintptr_t)(*snxt); 21192 } 21193 } else 21194 (*xmit_tail)->b_rptr = prev_rptr; 21195 21196 if (mp == NULL) { 21197 if (ire != NULL) 21198 IRE_REFRELE(ire); 21199 return (-1); 21200 } 21201 mp1 = mp->b_cont; 21202 21203 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21204 tcp->tcp_last_sent_len = (ushort_t)len; 21205 while (mp1->b_cont) { 21206 *xmit_tail = (*xmit_tail)->b_cont; 21207 (*xmit_tail)->b_prev = local_time; 21208 (*xmit_tail)->b_next = 21209 (mblk_t *)(uintptr_t)(*snxt); 21210 mp1 = mp1->b_cont; 21211 } 21212 *snxt += len; 21213 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21214 BUMP_LOCAL(tcp->tcp_obsegs); 21215 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21216 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21217 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21218 tcp_send_data(tcp, q, mp); 21219 continue; 21220 } 21221 21222 *snxt += len; /* Adjust later if we don't send all of len */ 21223 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21224 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21225 21226 if (*tail_unsent) { 21227 /* Are the bytes above us in flight? */ 21228 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21229 if (rptr != (*xmit_tail)->b_rptr) { 21230 *tail_unsent -= len; 21231 if (len <= mss) /* LSO is unusable */ 21232 tcp->tcp_last_sent_len = (ushort_t)len; 21233 len += tcp_hdr_len; 21234 if (tcp->tcp_ipversion == IPV4_VERSION) 21235 tcp->tcp_ipha->ipha_length = htons(len); 21236 else 21237 tcp->tcp_ip6h->ip6_plen = 21238 htons(len - 21239 ((char *)&tcp->tcp_ip6h[1] - 21240 tcp->tcp_iphc)); 21241 mp = dupb(*xmit_tail); 21242 if (mp == NULL) { 21243 if (ire != NULL) 21244 IRE_REFRELE(ire); 21245 return (-1); /* out_of_mem */ 21246 } 21247 mp->b_rptr = rptr; 21248 /* 21249 * If the old timestamp is no longer in use, 21250 * sample a new timestamp now. 21251 */ 21252 if ((*xmit_tail)->b_next == NULL) { 21253 (*xmit_tail)->b_prev = local_time; 21254 (*xmit_tail)->b_next = 21255 (mblk_t *)(uintptr_t)(*snxt-len); 21256 } 21257 goto must_alloc; 21258 } 21259 } else { 21260 *xmit_tail = (*xmit_tail)->b_cont; 21261 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21262 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21263 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21264 (*xmit_tail)->b_rptr); 21265 } 21266 21267 (*xmit_tail)->b_prev = local_time; 21268 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21269 21270 *tail_unsent -= len; 21271 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21272 tcp->tcp_last_sent_len = (ushort_t)len; 21273 21274 len += tcp_hdr_len; 21275 if (tcp->tcp_ipversion == IPV4_VERSION) 21276 tcp->tcp_ipha->ipha_length = htons(len); 21277 else 21278 tcp->tcp_ip6h->ip6_plen = htons(len - 21279 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21280 21281 mp = dupb(*xmit_tail); 21282 if (mp == NULL) { 21283 if (ire != NULL) 21284 IRE_REFRELE(ire); 21285 return (-1); /* out_of_mem */ 21286 } 21287 21288 len = tcp_hdr_len; 21289 /* 21290 * There are four reasons to allocate a new hdr mblk: 21291 * 1) The bytes above us are in use by another packet 21292 * 2) We don't have good alignment 21293 * 3) The mblk is being shared 21294 * 4) We don't have enough room for a header 21295 */ 21296 rptr = mp->b_rptr - len; 21297 if (!OK_32PTR(rptr) || 21298 ((db = mp->b_datap), db->db_ref != 2) || 21299 rptr < db->db_base + ire_fp_mp_len) { 21300 /* NOTE: we assume allocb returns an OK_32PTR */ 21301 21302 must_alloc:; 21303 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21304 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21305 if (mp1 == NULL) { 21306 freemsg(mp); 21307 if (ire != NULL) 21308 IRE_REFRELE(ire); 21309 return (-1); /* out_of_mem */ 21310 } 21311 mp1->b_cont = mp; 21312 mp = mp1; 21313 /* Leave room for Link Level header */ 21314 len = tcp_hdr_len; 21315 rptr = 21316 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21317 mp->b_wptr = &rptr[len]; 21318 } 21319 21320 /* 21321 * Fill in the header using the template header, and add 21322 * options such as time-stamp, ECN and/or SACK, as needed. 21323 */ 21324 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21325 21326 mp->b_rptr = rptr; 21327 21328 if (*tail_unsent) { 21329 int spill = *tail_unsent; 21330 21331 mp1 = mp->b_cont; 21332 if (mp1 == NULL) 21333 mp1 = mp; 21334 21335 /* 21336 * If we're a little short, tack on more mblks until 21337 * there is no more spillover. 21338 */ 21339 while (spill < 0) { 21340 mblk_t *nmp; 21341 int nmpsz; 21342 21343 nmp = (*xmit_tail)->b_cont; 21344 nmpsz = MBLKL(nmp); 21345 21346 /* 21347 * Excess data in mblk; can we split it? 21348 * If MDT is enabled for the connection, 21349 * keep on splitting as this is a transient 21350 * send path. 21351 */ 21352 if (!do_lso_send && !tcp->tcp_mdt && 21353 (spill + nmpsz > 0)) { 21354 /* 21355 * Don't split if stream head was 21356 * told to break up larger writes 21357 * into smaller ones. 21358 */ 21359 if (tcp->tcp_maxpsz > 0) 21360 break; 21361 21362 /* 21363 * Next mblk is less than SMSS/2 21364 * rounded up to nearest 64-byte; 21365 * let it get sent as part of the 21366 * next segment. 21367 */ 21368 if (tcp->tcp_localnet && 21369 !tcp->tcp_cork && 21370 (nmpsz < roundup((mss >> 1), 64))) 21371 break; 21372 } 21373 21374 *xmit_tail = nmp; 21375 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21376 /* Stash for rtt use later */ 21377 (*xmit_tail)->b_prev = local_time; 21378 (*xmit_tail)->b_next = 21379 (mblk_t *)(uintptr_t)(*snxt - len); 21380 mp1->b_cont = dupb(*xmit_tail); 21381 mp1 = mp1->b_cont; 21382 21383 spill += nmpsz; 21384 if (mp1 == NULL) { 21385 *tail_unsent = spill; 21386 freemsg(mp); 21387 if (ire != NULL) 21388 IRE_REFRELE(ire); 21389 return (-1); /* out_of_mem */ 21390 } 21391 } 21392 21393 /* Trim back any surplus on the last mblk */ 21394 if (spill >= 0) { 21395 mp1->b_wptr -= spill; 21396 *tail_unsent = spill; 21397 } else { 21398 /* 21399 * We did not send everything we could in 21400 * order to remain within the b_cont limit. 21401 */ 21402 *usable -= spill; 21403 *snxt += spill; 21404 tcp->tcp_last_sent_len += spill; 21405 UPDATE_MIB(&tcps->tcps_mib, 21406 tcpOutDataBytes, spill); 21407 /* 21408 * Adjust the checksum 21409 */ 21410 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21411 sum += spill; 21412 sum = (sum >> 16) + (sum & 0xFFFF); 21413 U16_TO_ABE16(sum, tcph->th_sum); 21414 if (tcp->tcp_ipversion == IPV4_VERSION) { 21415 sum = ntohs( 21416 ((ipha_t *)rptr)->ipha_length) + 21417 spill; 21418 ((ipha_t *)rptr)->ipha_length = 21419 htons(sum); 21420 } else { 21421 sum = ntohs( 21422 ((ip6_t *)rptr)->ip6_plen) + 21423 spill; 21424 ((ip6_t *)rptr)->ip6_plen = 21425 htons(sum); 21426 } 21427 *tail_unsent = 0; 21428 } 21429 } 21430 if (tcp->tcp_ip_forward_progress) { 21431 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21432 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21433 tcp->tcp_ip_forward_progress = B_FALSE; 21434 } 21435 21436 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21437 if (do_lso_send) { 21438 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21439 num_lso_seg); 21440 tcp->tcp_obsegs += num_lso_seg; 21441 21442 TCP_STAT(tcps, tcp_lso_times); 21443 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21444 } else { 21445 tcp_send_data(tcp, q, mp); 21446 BUMP_LOCAL(tcp->tcp_obsegs); 21447 } 21448 } 21449 21450 if (ire != NULL) 21451 IRE_REFRELE(ire); 21452 return (0); 21453 } 21454 21455 /* Unlink and return any mblk that looks like it contains a MDT info */ 21456 static mblk_t * 21457 tcp_mdt_info_mp(mblk_t *mp) 21458 { 21459 mblk_t *prev_mp; 21460 21461 for (;;) { 21462 prev_mp = mp; 21463 /* no more to process? */ 21464 if ((mp = mp->b_cont) == NULL) 21465 break; 21466 21467 switch (DB_TYPE(mp)) { 21468 case M_CTL: 21469 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21470 continue; 21471 ASSERT(prev_mp != NULL); 21472 prev_mp->b_cont = mp->b_cont; 21473 mp->b_cont = NULL; 21474 return (mp); 21475 default: 21476 break; 21477 } 21478 } 21479 return (mp); 21480 } 21481 21482 /* MDT info update routine, called when IP notifies us about MDT */ 21483 static void 21484 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21485 { 21486 boolean_t prev_state; 21487 tcp_stack_t *tcps = tcp->tcp_tcps; 21488 21489 /* 21490 * IP is telling us to abort MDT on this connection? We know 21491 * this because the capability is only turned off when IP 21492 * encounters some pathological cases, e.g. link-layer change 21493 * where the new driver doesn't support MDT, or in situation 21494 * where MDT usage on the link-layer has been switched off. 21495 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21496 * if the link-layer doesn't support MDT, and if it does, it 21497 * will indicate that the feature is to be turned on. 21498 */ 21499 prev_state = tcp->tcp_mdt; 21500 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21501 if (!tcp->tcp_mdt && !first) { 21502 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21503 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21504 (void *)tcp->tcp_connp)); 21505 } 21506 21507 /* 21508 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21509 * so disable MDT otherwise. The checks are done here 21510 * and in tcp_wput_data(). 21511 */ 21512 if (tcp->tcp_mdt && 21513 (tcp->tcp_ipversion == IPV4_VERSION && 21514 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21515 (tcp->tcp_ipversion == IPV6_VERSION && 21516 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21517 tcp->tcp_mdt = B_FALSE; 21518 21519 if (tcp->tcp_mdt) { 21520 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21521 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21522 "version (%d), expected version is %d", 21523 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21524 tcp->tcp_mdt = B_FALSE; 21525 return; 21526 } 21527 21528 /* 21529 * We need the driver to be able to handle at least three 21530 * spans per packet in order for tcp MDT to be utilized. 21531 * The first is for the header portion, while the rest are 21532 * needed to handle a packet that straddles across two 21533 * virtually non-contiguous buffers; a typical tcp packet 21534 * therefore consists of only two spans. Note that we take 21535 * a zero as "don't care". 21536 */ 21537 if (mdt_capab->ill_mdt_span_limit > 0 && 21538 mdt_capab->ill_mdt_span_limit < 3) { 21539 tcp->tcp_mdt = B_FALSE; 21540 return; 21541 } 21542 21543 /* a zero means driver wants default value */ 21544 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21545 tcps->tcps_mdt_max_pbufs); 21546 if (tcp->tcp_mdt_max_pld == 0) 21547 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21548 21549 /* ensure 32-bit alignment */ 21550 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21551 mdt_capab->ill_mdt_hdr_head), 4); 21552 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21553 mdt_capab->ill_mdt_hdr_tail), 4); 21554 21555 if (!first && !prev_state) { 21556 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21557 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21558 (void *)tcp->tcp_connp)); 21559 } 21560 } 21561 } 21562 21563 /* Unlink and return any mblk that looks like it contains a LSO info */ 21564 static mblk_t * 21565 tcp_lso_info_mp(mblk_t *mp) 21566 { 21567 mblk_t *prev_mp; 21568 21569 for (;;) { 21570 prev_mp = mp; 21571 /* no more to process? */ 21572 if ((mp = mp->b_cont) == NULL) 21573 break; 21574 21575 switch (DB_TYPE(mp)) { 21576 case M_CTL: 21577 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21578 continue; 21579 ASSERT(prev_mp != NULL); 21580 prev_mp->b_cont = mp->b_cont; 21581 mp->b_cont = NULL; 21582 return (mp); 21583 default: 21584 break; 21585 } 21586 } 21587 21588 return (mp); 21589 } 21590 21591 /* LSO info update routine, called when IP notifies us about LSO */ 21592 static void 21593 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21594 { 21595 tcp_stack_t *tcps = tcp->tcp_tcps; 21596 21597 /* 21598 * IP is telling us to abort LSO on this connection? We know 21599 * this because the capability is only turned off when IP 21600 * encounters some pathological cases, e.g. link-layer change 21601 * where the new NIC/driver doesn't support LSO, or in situation 21602 * where LSO usage on the link-layer has been switched off. 21603 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21604 * if the link-layer doesn't support LSO, and if it does, it 21605 * will indicate that the feature is to be turned on. 21606 */ 21607 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21608 TCP_STAT(tcps, tcp_lso_enabled); 21609 21610 /* 21611 * We currently only support LSO on simple TCP/IPv4, 21612 * so disable LSO otherwise. The checks are done here 21613 * and in tcp_wput_data(). 21614 */ 21615 if (tcp->tcp_lso && 21616 (tcp->tcp_ipversion == IPV4_VERSION && 21617 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21618 (tcp->tcp_ipversion == IPV6_VERSION)) { 21619 tcp->tcp_lso = B_FALSE; 21620 TCP_STAT(tcps, tcp_lso_disabled); 21621 } else { 21622 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21623 lso_capab->ill_lso_max); 21624 } 21625 } 21626 21627 static void 21628 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21629 { 21630 conn_t *connp = tcp->tcp_connp; 21631 tcp_stack_t *tcps = tcp->tcp_tcps; 21632 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21633 21634 ASSERT(ire != NULL); 21635 21636 /* 21637 * We may be in the fastpath here, and although we essentially do 21638 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21639 * we try to keep things as brief as possible. After all, these 21640 * are only best-effort checks, and we do more thorough ones prior 21641 * to calling tcp_send()/tcp_multisend(). 21642 */ 21643 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21644 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21645 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21646 !(ire->ire_flags & RTF_MULTIRT) && 21647 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21648 CONN_IS_LSO_MD_FASTPATH(connp)) { 21649 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21650 /* Cache the result */ 21651 connp->conn_lso_ok = B_TRUE; 21652 21653 ASSERT(ill->ill_lso_capab != NULL); 21654 if (!ill->ill_lso_capab->ill_lso_on) { 21655 ill->ill_lso_capab->ill_lso_on = 1; 21656 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21657 "LSO for interface %s\n", (void *)connp, 21658 ill->ill_name)); 21659 } 21660 tcp_lso_update(tcp, ill->ill_lso_capab); 21661 } else if (ipst->ips_ip_multidata_outbound && 21662 ILL_MDT_CAPABLE(ill)) { 21663 /* Cache the result */ 21664 connp->conn_mdt_ok = B_TRUE; 21665 21666 ASSERT(ill->ill_mdt_capab != NULL); 21667 if (!ill->ill_mdt_capab->ill_mdt_on) { 21668 ill->ill_mdt_capab->ill_mdt_on = 1; 21669 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21670 "MDT for interface %s\n", (void *)connp, 21671 ill->ill_name)); 21672 } 21673 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21674 } 21675 } 21676 21677 /* 21678 * The goal is to reduce the number of generated tcp segments by 21679 * setting the maxpsz multiplier to 0; this will have an affect on 21680 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21681 * into each packet, up to SMSS bytes. Doing this reduces the number 21682 * of outbound segments and incoming ACKs, thus allowing for better 21683 * network and system performance. In contrast the legacy behavior 21684 * may result in sending less than SMSS size, because the last mblk 21685 * for some packets may have more data than needed to make up SMSS, 21686 * and the legacy code refused to "split" it. 21687 * 21688 * We apply the new behavior on following situations: 21689 * 21690 * 1) Loopback connections, 21691 * 2) Connections in which the remote peer is not on local subnet, 21692 * 3) Local subnet connections over the bge interface (see below). 21693 * 21694 * Ideally, we would like this behavior to apply for interfaces other 21695 * than bge. However, doing so would negatively impact drivers which 21696 * perform dynamic mapping and unmapping of DMA resources, which are 21697 * increased by setting the maxpsz multiplier to 0 (more mblks per 21698 * packet will be generated by tcp). The bge driver does not suffer 21699 * from this, as it copies the mblks into pre-mapped buffers, and 21700 * therefore does not require more I/O resources than before. 21701 * 21702 * Otherwise, this behavior is present on all network interfaces when 21703 * the destination endpoint is non-local, since reducing the number 21704 * of packets in general is good for the network. 21705 * 21706 * TODO We need to remove this hard-coded conditional for bge once 21707 * a better "self-tuning" mechanism, or a way to comprehend 21708 * the driver transmit strategy is devised. Until the solution 21709 * is found and well understood, we live with this hack. 21710 */ 21711 if (!tcp_static_maxpsz && 21712 (tcp->tcp_loopback || !tcp->tcp_localnet || 21713 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21714 /* override the default value */ 21715 tcp->tcp_maxpsz = 0; 21716 21717 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21718 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21719 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21720 } 21721 21722 /* set the stream head parameters accordingly */ 21723 (void) tcp_maxpsz_set(tcp, B_TRUE); 21724 } 21725 21726 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21727 static void 21728 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21729 { 21730 uchar_t fval = *mp->b_rptr; 21731 mblk_t *tail; 21732 queue_t *q = tcp->tcp_wq; 21733 21734 /* TODO: How should flush interact with urgent data? */ 21735 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21736 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21737 /* 21738 * Flush only data that has not yet been put on the wire. If 21739 * we flush data that we have already transmitted, life, as we 21740 * know it, may come to an end. 21741 */ 21742 tail = tcp->tcp_xmit_tail; 21743 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21744 tcp->tcp_xmit_tail_unsent = 0; 21745 tcp->tcp_unsent = 0; 21746 if (tail->b_wptr != tail->b_rptr) 21747 tail = tail->b_cont; 21748 if (tail) { 21749 mblk_t **excess = &tcp->tcp_xmit_head; 21750 for (;;) { 21751 mblk_t *mp1 = *excess; 21752 if (mp1 == tail) 21753 break; 21754 tcp->tcp_xmit_tail = mp1; 21755 tcp->tcp_xmit_last = mp1; 21756 excess = &mp1->b_cont; 21757 } 21758 *excess = NULL; 21759 tcp_close_mpp(&tail); 21760 if (tcp->tcp_snd_zcopy_aware) 21761 tcp_zcopy_notify(tcp); 21762 } 21763 /* 21764 * We have no unsent data, so unsent must be less than 21765 * tcp_xmit_lowater, so re-enable flow. 21766 */ 21767 mutex_enter(&tcp->tcp_non_sq_lock); 21768 if (tcp->tcp_flow_stopped) { 21769 tcp_clrqfull(tcp); 21770 } 21771 mutex_exit(&tcp->tcp_non_sq_lock); 21772 } 21773 /* 21774 * TODO: you can't just flush these, you have to increase rwnd for one 21775 * thing. For another, how should urgent data interact? 21776 */ 21777 if (fval & FLUSHR) { 21778 *mp->b_rptr = fval & ~FLUSHW; 21779 /* XXX */ 21780 qreply(q, mp); 21781 return; 21782 } 21783 freemsg(mp); 21784 } 21785 21786 /* 21787 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21788 * messages. 21789 */ 21790 static void 21791 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21792 { 21793 mblk_t *mp1; 21794 STRUCT_HANDLE(strbuf, sb); 21795 uint16_t port; 21796 queue_t *q = tcp->tcp_wq; 21797 in6_addr_t v6addr; 21798 ipaddr_t v4addr; 21799 uint32_t flowinfo = 0; 21800 int addrlen; 21801 21802 /* Make sure it is one of ours. */ 21803 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21804 case TI_GETMYNAME: 21805 case TI_GETPEERNAME: 21806 break; 21807 default: 21808 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21809 return; 21810 } 21811 switch (mi_copy_state(q, mp, &mp1)) { 21812 case -1: 21813 return; 21814 case MI_COPY_CASE(MI_COPY_IN, 1): 21815 break; 21816 case MI_COPY_CASE(MI_COPY_OUT, 1): 21817 /* Copy out the strbuf. */ 21818 mi_copyout(q, mp); 21819 return; 21820 case MI_COPY_CASE(MI_COPY_OUT, 2): 21821 /* All done. */ 21822 mi_copy_done(q, mp, 0); 21823 return; 21824 default: 21825 mi_copy_done(q, mp, EPROTO); 21826 return; 21827 } 21828 /* Check alignment of the strbuf */ 21829 if (!OK_32PTR(mp1->b_rptr)) { 21830 mi_copy_done(q, mp, EINVAL); 21831 return; 21832 } 21833 21834 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 21835 (void *)mp1->b_rptr); 21836 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21837 21838 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21839 mi_copy_done(q, mp, EINVAL); 21840 return; 21841 } 21842 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21843 case TI_GETMYNAME: 21844 if (tcp->tcp_family == AF_INET) { 21845 if (tcp->tcp_ipversion == IPV4_VERSION) { 21846 v4addr = tcp->tcp_ipha->ipha_src; 21847 } else { 21848 /* can't return an address in this case */ 21849 v4addr = 0; 21850 } 21851 } else { 21852 /* tcp->tcp_family == AF_INET6 */ 21853 if (tcp->tcp_ipversion == IPV4_VERSION) { 21854 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 21855 &v6addr); 21856 } else { 21857 v6addr = tcp->tcp_ip6h->ip6_src; 21858 } 21859 } 21860 port = tcp->tcp_lport; 21861 break; 21862 case TI_GETPEERNAME: 21863 if (tcp->tcp_family == AF_INET) { 21864 if (tcp->tcp_ipversion == IPV4_VERSION) { 21865 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 21866 v4addr); 21867 } else { 21868 /* can't return an address in this case */ 21869 v4addr = 0; 21870 } 21871 } else { 21872 /* tcp->tcp_family == AF_INET6) */ 21873 v6addr = tcp->tcp_remote_v6; 21874 if (tcp->tcp_ipversion == IPV6_VERSION) { 21875 /* 21876 * No flowinfo if tcp->tcp_ipversion is v4. 21877 * 21878 * flowinfo was already initialized to zero 21879 * where it was declared above, so only 21880 * set it if ipversion is v6. 21881 */ 21882 flowinfo = tcp->tcp_ip6h->ip6_vcf & 21883 ~IPV6_VERS_AND_FLOW_MASK; 21884 } 21885 } 21886 port = tcp->tcp_fport; 21887 break; 21888 default: 21889 mi_copy_done(q, mp, EPROTO); 21890 return; 21891 } 21892 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21893 if (!mp1) 21894 return; 21895 21896 if (tcp->tcp_family == AF_INET) { 21897 sin_t *sin; 21898 21899 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 21900 sin = (sin_t *)mp1->b_rptr; 21901 mp1->b_wptr = (uchar_t *)&sin[1]; 21902 *sin = sin_null; 21903 sin->sin_family = AF_INET; 21904 sin->sin_addr.s_addr = v4addr; 21905 sin->sin_port = port; 21906 } else { 21907 /* tcp->tcp_family == AF_INET6 */ 21908 sin6_t *sin6; 21909 21910 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 21911 sin6 = (sin6_t *)mp1->b_rptr; 21912 mp1->b_wptr = (uchar_t *)&sin6[1]; 21913 *sin6 = sin6_null; 21914 sin6->sin6_family = AF_INET6; 21915 sin6->sin6_flowinfo = flowinfo; 21916 sin6->sin6_addr = v6addr; 21917 sin6->sin6_port = port; 21918 } 21919 /* Copy out the address */ 21920 mi_copyout(q, mp); 21921 } 21922 21923 /* 21924 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21925 * messages. 21926 */ 21927 /* ARGSUSED */ 21928 static void 21929 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21930 { 21931 conn_t *connp = (conn_t *)arg; 21932 tcp_t *tcp = connp->conn_tcp; 21933 queue_t *q = tcp->tcp_wq; 21934 struct iocblk *iocp; 21935 tcp_stack_t *tcps = tcp->tcp_tcps; 21936 21937 ASSERT(DB_TYPE(mp) == M_IOCTL); 21938 /* 21939 * Try and ASSERT the minimum possible references on the 21940 * conn early enough. Since we are executing on write side, 21941 * the connection is obviously not detached and that means 21942 * there is a ref each for TCP and IP. Since we are behind 21943 * the squeue, the minimum references needed are 3. If the 21944 * conn is in classifier hash list, there should be an 21945 * extra ref for that (we check both the possibilities). 21946 */ 21947 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21948 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21949 21950 iocp = (struct iocblk *)mp->b_rptr; 21951 switch (iocp->ioc_cmd) { 21952 case TCP_IOC_DEFAULT_Q: 21953 /* Wants to be the default wq. */ 21954 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21955 iocp->ioc_error = EPERM; 21956 iocp->ioc_count = 0; 21957 mp->b_datap->db_type = M_IOCACK; 21958 qreply(q, mp); 21959 return; 21960 } 21961 tcp_def_q_set(tcp, mp); 21962 return; 21963 case _SIOCSOCKFALLBACK: 21964 /* 21965 * Either sockmod is about to be popped and the socket 21966 * would now be treated as a plain stream, or a module 21967 * is about to be pushed so we could no longer use read- 21968 * side synchronous streams for fused loopback tcp. 21969 * Drain any queued data and disable direct sockfs 21970 * interface from now on. 21971 */ 21972 if (!tcp->tcp_issocket) { 21973 DB_TYPE(mp) = M_IOCNAK; 21974 iocp->ioc_error = EINVAL; 21975 } else { 21976 #ifdef _ILP32 21977 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 21978 #else 21979 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21980 #endif 21981 /* 21982 * Insert this socket into the acceptor hash. 21983 * We might need it for T_CONN_RES message 21984 */ 21985 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21986 21987 if (tcp->tcp_fused) { 21988 /* 21989 * This is a fused loopback tcp; disable 21990 * read-side synchronous streams interface 21991 * and drain any queued data. It is okay 21992 * to do this for non-synchronous streams 21993 * fused tcp as well. 21994 */ 21995 tcp_fuse_disable_pair(tcp, B_FALSE); 21996 } 21997 tcp->tcp_issocket = B_FALSE; 21998 TCP_STAT(tcps, tcp_sock_fallback); 21999 22000 DB_TYPE(mp) = M_IOCACK; 22001 iocp->ioc_error = 0; 22002 } 22003 iocp->ioc_count = 0; 22004 iocp->ioc_rval = 0; 22005 qreply(q, mp); 22006 return; 22007 } 22008 CALL_IP_WPUT(connp, q, mp); 22009 } 22010 22011 /* 22012 * This routine is called by tcp_wput() to handle all TPI requests. 22013 */ 22014 /* ARGSUSED */ 22015 static void 22016 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22017 { 22018 conn_t *connp = (conn_t *)arg; 22019 tcp_t *tcp = connp->conn_tcp; 22020 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22021 uchar_t *rptr; 22022 t_scalar_t type; 22023 int len; 22024 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22025 22026 /* 22027 * Try and ASSERT the minimum possible references on the 22028 * conn early enough. Since we are executing on write side, 22029 * the connection is obviously not detached and that means 22030 * there is a ref each for TCP and IP. Since we are behind 22031 * the squeue, the minimum references needed are 3. If the 22032 * conn is in classifier hash list, there should be an 22033 * extra ref for that (we check both the possibilities). 22034 */ 22035 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22036 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22037 22038 rptr = mp->b_rptr; 22039 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22040 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22041 type = ((union T_primitives *)rptr)->type; 22042 if (type == T_EXDATA_REQ) { 22043 uint32_t msize = msgdsize(mp->b_cont); 22044 22045 len = msize - 1; 22046 if (len < 0) { 22047 freemsg(mp); 22048 return; 22049 } 22050 /* 22051 * Try to force urgent data out on the wire. 22052 * Even if we have unsent data this will 22053 * at least send the urgent flag. 22054 * XXX does not handle more flag correctly. 22055 */ 22056 len += tcp->tcp_unsent; 22057 len += tcp->tcp_snxt; 22058 tcp->tcp_urg = len; 22059 tcp->tcp_valid_bits |= TCP_URG_VALID; 22060 22061 /* Bypass tcp protocol for fused tcp loopback */ 22062 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22063 return; 22064 } else if (type != T_DATA_REQ) { 22065 goto non_urgent_data; 22066 } 22067 /* TODO: options, flags, ... from user */ 22068 /* Set length to zero for reclamation below */ 22069 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22070 freeb(mp); 22071 return; 22072 } else { 22073 if (tcp->tcp_debug) { 22074 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22075 "tcp_wput_proto, dropping one..."); 22076 } 22077 freemsg(mp); 22078 return; 22079 } 22080 22081 non_urgent_data: 22082 22083 switch ((int)tprim->type) { 22084 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22085 /* 22086 * save the kssl_ent_t from the next block, and convert this 22087 * back to a normal bind_req. 22088 */ 22089 if (mp->b_cont != NULL) { 22090 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22091 22092 if (tcp->tcp_kssl_ent != NULL) { 22093 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22094 KSSL_NO_PROXY); 22095 tcp->tcp_kssl_ent = NULL; 22096 } 22097 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22098 sizeof (kssl_ent_t)); 22099 kssl_hold_ent(tcp->tcp_kssl_ent); 22100 freemsg(mp->b_cont); 22101 mp->b_cont = NULL; 22102 } 22103 tprim->type = T_BIND_REQ; 22104 22105 /* FALLTHROUGH */ 22106 case O_T_BIND_REQ: /* bind request */ 22107 case T_BIND_REQ: /* new semantics bind request */ 22108 tcp_bind(tcp, mp); 22109 break; 22110 case T_UNBIND_REQ: /* unbind request */ 22111 tcp_unbind(tcp, mp); 22112 break; 22113 case O_T_CONN_RES: /* old connection response XXX */ 22114 case T_CONN_RES: /* connection response */ 22115 tcp_accept(tcp, mp); 22116 break; 22117 case T_CONN_REQ: /* connection request */ 22118 tcp_connect(tcp, mp); 22119 break; 22120 case T_DISCON_REQ: /* disconnect request */ 22121 tcp_disconnect(tcp, mp); 22122 break; 22123 case T_CAPABILITY_REQ: 22124 tcp_capability_req(tcp, mp); /* capability request */ 22125 break; 22126 case T_INFO_REQ: /* information request */ 22127 tcp_info_req(tcp, mp); 22128 break; 22129 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22130 /* Only IP is allowed to return meaningful value */ 22131 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 22132 break; 22133 case T_OPTMGMT_REQ: 22134 /* 22135 * Note: no support for snmpcom_req() through new 22136 * T_OPTMGMT_REQ. See comments in ip.c 22137 */ 22138 /* Only IP is allowed to return meaningful value */ 22139 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 22140 break; 22141 22142 case T_UNITDATA_REQ: /* unitdata request */ 22143 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22144 break; 22145 case T_ORDREL_REQ: /* orderly release req */ 22146 freemsg(mp); 22147 22148 if (tcp->tcp_fused) 22149 tcp_unfuse(tcp); 22150 22151 if (tcp_xmit_end(tcp) != 0) { 22152 /* 22153 * We were crossing FINs and got a reset from 22154 * the other side. Just ignore it. 22155 */ 22156 if (tcp->tcp_debug) { 22157 (void) strlog(TCP_MOD_ID, 0, 1, 22158 SL_ERROR|SL_TRACE, 22159 "tcp_wput_proto, T_ORDREL_REQ out of " 22160 "state %s", 22161 tcp_display(tcp, NULL, 22162 DISP_ADDR_AND_PORT)); 22163 } 22164 } 22165 break; 22166 case T_ADDR_REQ: 22167 tcp_addr_req(tcp, mp); 22168 break; 22169 default: 22170 if (tcp->tcp_debug) { 22171 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22172 "tcp_wput_proto, bogus TPI msg, type %d", 22173 tprim->type); 22174 } 22175 /* 22176 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22177 * to recover. 22178 */ 22179 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22180 break; 22181 } 22182 } 22183 22184 /* 22185 * The TCP write service routine should never be called... 22186 */ 22187 /* ARGSUSED */ 22188 static void 22189 tcp_wsrv(queue_t *q) 22190 { 22191 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22192 22193 TCP_STAT(tcps, tcp_wsrv_called); 22194 } 22195 22196 /* Non overlapping byte exchanger */ 22197 static void 22198 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22199 { 22200 uchar_t uch; 22201 22202 while (len-- > 0) { 22203 uch = a[len]; 22204 a[len] = b[len]; 22205 b[len] = uch; 22206 } 22207 } 22208 22209 /* 22210 * Send out a control packet on the tcp connection specified. This routine 22211 * is typically called where we need a simple ACK or RST generated. 22212 */ 22213 static void 22214 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22215 { 22216 uchar_t *rptr; 22217 tcph_t *tcph; 22218 ipha_t *ipha = NULL; 22219 ip6_t *ip6h = NULL; 22220 uint32_t sum; 22221 int tcp_hdr_len; 22222 int tcp_ip_hdr_len; 22223 mblk_t *mp; 22224 tcp_stack_t *tcps = tcp->tcp_tcps; 22225 22226 /* 22227 * Save sum for use in source route later. 22228 */ 22229 ASSERT(tcp != NULL); 22230 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22231 tcp_hdr_len = tcp->tcp_hdr_len; 22232 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22233 22234 /* If a text string is passed in with the request, pass it to strlog. */ 22235 if (str != NULL && tcp->tcp_debug) { 22236 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22237 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22238 str, seq, ack, ctl); 22239 } 22240 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22241 BPRI_MED); 22242 if (mp == NULL) { 22243 return; 22244 } 22245 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22246 mp->b_rptr = rptr; 22247 mp->b_wptr = &rptr[tcp_hdr_len]; 22248 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22249 22250 if (tcp->tcp_ipversion == IPV4_VERSION) { 22251 ipha = (ipha_t *)rptr; 22252 ipha->ipha_length = htons(tcp_hdr_len); 22253 } else { 22254 ip6h = (ip6_t *)rptr; 22255 ASSERT(tcp != NULL); 22256 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22257 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22258 } 22259 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22260 tcph->th_flags[0] = (uint8_t)ctl; 22261 if (ctl & TH_RST) { 22262 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22263 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22264 /* 22265 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22266 */ 22267 if (tcp->tcp_snd_ts_ok && 22268 tcp->tcp_state > TCPS_SYN_SENT) { 22269 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22270 *(mp->b_wptr) = TCPOPT_EOL; 22271 if (tcp->tcp_ipversion == IPV4_VERSION) { 22272 ipha->ipha_length = htons(tcp_hdr_len - 22273 TCPOPT_REAL_TS_LEN); 22274 } else { 22275 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22276 TCPOPT_REAL_TS_LEN); 22277 } 22278 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22279 sum -= TCPOPT_REAL_TS_LEN; 22280 } 22281 } 22282 if (ctl & TH_ACK) { 22283 if (tcp->tcp_snd_ts_ok) { 22284 U32_TO_BE32(lbolt, 22285 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22286 U32_TO_BE32(tcp->tcp_ts_recent, 22287 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22288 } 22289 22290 /* Update the latest receive window size in TCP header. */ 22291 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22292 tcph->th_win); 22293 tcp->tcp_rack = ack; 22294 tcp->tcp_rack_cnt = 0; 22295 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22296 } 22297 BUMP_LOCAL(tcp->tcp_obsegs); 22298 U32_TO_BE32(seq, tcph->th_seq); 22299 U32_TO_BE32(ack, tcph->th_ack); 22300 /* 22301 * Include the adjustment for a source route if any. 22302 */ 22303 sum = (sum >> 16) + (sum & 0xFFFF); 22304 U16_TO_BE16(sum, tcph->th_sum); 22305 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22306 tcp_send_data(tcp, tcp->tcp_wq, mp); 22307 } 22308 22309 /* 22310 * If this routine returns B_TRUE, TCP can generate a RST in response 22311 * to a segment. If it returns B_FALSE, TCP should not respond. 22312 */ 22313 static boolean_t 22314 tcp_send_rst_chk(tcp_stack_t *tcps) 22315 { 22316 clock_t now; 22317 22318 /* 22319 * TCP needs to protect itself from generating too many RSTs. 22320 * This can be a DoS attack by sending us random segments 22321 * soliciting RSTs. 22322 * 22323 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22324 * in each 1 second interval. In this way, TCP still generate 22325 * RSTs in normal cases but when under attack, the impact is 22326 * limited. 22327 */ 22328 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22329 now = lbolt; 22330 /* lbolt can wrap around. */ 22331 if ((tcps->tcps_last_rst_intrvl > now) || 22332 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22333 1*SECONDS)) { 22334 tcps->tcps_last_rst_intrvl = now; 22335 tcps->tcps_rst_cnt = 1; 22336 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22337 return (B_FALSE); 22338 } 22339 } 22340 return (B_TRUE); 22341 } 22342 22343 /* 22344 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22345 */ 22346 static void 22347 tcp_ip_ire_mark_advice(tcp_t *tcp) 22348 { 22349 mblk_t *mp; 22350 ipic_t *ipic; 22351 22352 if (tcp->tcp_ipversion == IPV4_VERSION) { 22353 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22354 &ipic); 22355 } else { 22356 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22357 &ipic); 22358 } 22359 if (mp == NULL) 22360 return; 22361 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22362 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22363 } 22364 22365 /* 22366 * Return an IP advice ioctl mblk and set ipic to be the pointer 22367 * to the advice structure. 22368 */ 22369 static mblk_t * 22370 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22371 { 22372 struct iocblk *ioc; 22373 mblk_t *mp, *mp1; 22374 22375 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22376 if (mp == NULL) 22377 return (NULL); 22378 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22379 *ipic = (ipic_t *)mp->b_rptr; 22380 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22381 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22382 22383 bcopy(addr, *ipic + 1, addr_len); 22384 22385 (*ipic)->ipic_addr_length = addr_len; 22386 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22387 22388 mp1 = mkiocb(IP_IOCTL); 22389 if (mp1 == NULL) { 22390 freemsg(mp); 22391 return (NULL); 22392 } 22393 mp1->b_cont = mp; 22394 ioc = (struct iocblk *)mp1->b_rptr; 22395 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22396 22397 return (mp1); 22398 } 22399 22400 /* 22401 * Generate a reset based on an inbound packet for which there is no active 22402 * tcp state that we can find. 22403 * 22404 * IPSEC NOTE : Try to send the reply with the same protection as it came 22405 * in. We still have the ipsec_mp that the packet was attached to. Thus 22406 * the packet will go out at the same level of protection as it came in by 22407 * converting the IPSEC_IN to IPSEC_OUT. 22408 */ 22409 static void 22410 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22411 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22412 tcp_stack_t *tcps) 22413 { 22414 ipha_t *ipha = NULL; 22415 ip6_t *ip6h = NULL; 22416 ushort_t len; 22417 tcph_t *tcph; 22418 int i; 22419 mblk_t *ipsec_mp; 22420 boolean_t mctl_present; 22421 ipic_t *ipic; 22422 ipaddr_t v4addr; 22423 in6_addr_t v6addr; 22424 int addr_len; 22425 void *addr; 22426 queue_t *q = tcps->tcps_g_q; 22427 tcp_t *tcp; 22428 cred_t *cr; 22429 mblk_t *nmp; 22430 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22431 22432 if (tcps->tcps_g_q == NULL) { 22433 /* 22434 * For non-zero stackids the default queue isn't created 22435 * until the first open, thus there can be a need to send 22436 * a reset before then. But we can't do that, hence we just 22437 * drop the packet. Later during boot, when the default queue 22438 * has been setup, a retransmitted packet from the peer 22439 * will result in a reset. 22440 */ 22441 ASSERT(tcps->tcps_netstack->netstack_stackid != 22442 GLOBAL_NETSTACKID); 22443 freemsg(mp); 22444 return; 22445 } 22446 22447 tcp = Q_TO_TCP(q); 22448 22449 if (!tcp_send_rst_chk(tcps)) { 22450 tcps->tcps_rst_unsent++; 22451 freemsg(mp); 22452 return; 22453 } 22454 22455 if (mp->b_datap->db_type == M_CTL) { 22456 ipsec_mp = mp; 22457 mp = mp->b_cont; 22458 mctl_present = B_TRUE; 22459 } else { 22460 ipsec_mp = mp; 22461 mctl_present = B_FALSE; 22462 } 22463 22464 if (str && q && tcps->tcps_dbg) { 22465 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22466 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22467 "flags 0x%x", 22468 str, seq, ack, ctl); 22469 } 22470 if (mp->b_datap->db_ref != 1) { 22471 mblk_t *mp1 = copyb(mp); 22472 freemsg(mp); 22473 mp = mp1; 22474 if (!mp) { 22475 if (mctl_present) 22476 freeb(ipsec_mp); 22477 return; 22478 } else { 22479 if (mctl_present) { 22480 ipsec_mp->b_cont = mp; 22481 } else { 22482 ipsec_mp = mp; 22483 } 22484 } 22485 } else if (mp->b_cont) { 22486 freemsg(mp->b_cont); 22487 mp->b_cont = NULL; 22488 } 22489 /* 22490 * We skip reversing source route here. 22491 * (for now we replace all IP options with EOL) 22492 */ 22493 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22494 ipha = (ipha_t *)mp->b_rptr; 22495 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22496 mp->b_rptr[i] = IPOPT_EOL; 22497 /* 22498 * Make sure that src address isn't flagrantly invalid. 22499 * Not all broadcast address checking for the src address 22500 * is possible, since we don't know the netmask of the src 22501 * addr. No check for destination address is done, since 22502 * IP will not pass up a packet with a broadcast dest 22503 * address to TCP. Similar checks are done below for IPv6. 22504 */ 22505 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22506 CLASSD(ipha->ipha_src)) { 22507 freemsg(ipsec_mp); 22508 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22509 return; 22510 } 22511 } else { 22512 ip6h = (ip6_t *)mp->b_rptr; 22513 22514 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22515 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22516 freemsg(ipsec_mp); 22517 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22518 return; 22519 } 22520 22521 /* Remove any extension headers assuming partial overlay */ 22522 if (ip_hdr_len > IPV6_HDR_LEN) { 22523 uint8_t *to; 22524 22525 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22526 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22527 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22528 ip_hdr_len = IPV6_HDR_LEN; 22529 ip6h = (ip6_t *)mp->b_rptr; 22530 ip6h->ip6_nxt = IPPROTO_TCP; 22531 } 22532 } 22533 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22534 if (tcph->th_flags[0] & TH_RST) { 22535 freemsg(ipsec_mp); 22536 return; 22537 } 22538 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22539 len = ip_hdr_len + sizeof (tcph_t); 22540 mp->b_wptr = &mp->b_rptr[len]; 22541 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22542 ipha->ipha_length = htons(len); 22543 /* Swap addresses */ 22544 v4addr = ipha->ipha_src; 22545 ipha->ipha_src = ipha->ipha_dst; 22546 ipha->ipha_dst = v4addr; 22547 ipha->ipha_ident = 0; 22548 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22549 addr_len = IP_ADDR_LEN; 22550 addr = &v4addr; 22551 } else { 22552 /* No ip6i_t in this case */ 22553 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22554 /* Swap addresses */ 22555 v6addr = ip6h->ip6_src; 22556 ip6h->ip6_src = ip6h->ip6_dst; 22557 ip6h->ip6_dst = v6addr; 22558 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22559 addr_len = IPV6_ADDR_LEN; 22560 addr = &v6addr; 22561 } 22562 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22563 U32_TO_BE32(ack, tcph->th_ack); 22564 U32_TO_BE32(seq, tcph->th_seq); 22565 U16_TO_BE16(0, tcph->th_win); 22566 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22567 tcph->th_flags[0] = (uint8_t)ctl; 22568 if (ctl & TH_RST) { 22569 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22570 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22571 } 22572 22573 /* IP trusts us to set up labels when required. */ 22574 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 22575 crgetlabel(cr) != NULL) { 22576 int err, adjust; 22577 22578 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22579 err = tsol_check_label(cr, &mp, &adjust, 22580 tcp->tcp_connp->conn_mac_exempt, 22581 tcps->tcps_netstack->netstack_ip); 22582 else 22583 err = tsol_check_label_v6(cr, &mp, &adjust, 22584 tcp->tcp_connp->conn_mac_exempt, 22585 tcps->tcps_netstack->netstack_ip); 22586 if (mctl_present) 22587 ipsec_mp->b_cont = mp; 22588 else 22589 ipsec_mp = mp; 22590 if (err != 0) { 22591 freemsg(ipsec_mp); 22592 return; 22593 } 22594 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22595 ipha = (ipha_t *)mp->b_rptr; 22596 adjust += ntohs(ipha->ipha_length); 22597 ipha->ipha_length = htons(adjust); 22598 } else { 22599 ip6h = (ip6_t *)mp->b_rptr; 22600 } 22601 } 22602 22603 if (mctl_present) { 22604 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22605 22606 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22607 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22608 return; 22609 } 22610 } 22611 if (zoneid == ALL_ZONES) 22612 zoneid = GLOBAL_ZONEID; 22613 22614 /* Add the zoneid so ip_output routes it properly */ 22615 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22616 freemsg(ipsec_mp); 22617 return; 22618 } 22619 ipsec_mp = nmp; 22620 22621 /* 22622 * NOTE: one might consider tracing a TCP packet here, but 22623 * this function has no active TCP state and no tcp structure 22624 * that has a trace buffer. If we traced here, we would have 22625 * to keep a local trace buffer in tcp_record_trace(). 22626 * 22627 * TSol note: The mblk that contains the incoming packet was 22628 * reused by tcp_xmit_listener_reset, so it already contains 22629 * the right credentials and we don't need to call mblk_setcred. 22630 * Also the conn's cred is not right since it is associated 22631 * with tcps_g_q. 22632 */ 22633 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22634 22635 /* 22636 * Tell IP to mark the IRE used for this destination temporary. 22637 * This way, we can limit our exposure to DoS attack because IP 22638 * creates an IRE for each destination. If there are too many, 22639 * the time to do any routing lookup will be extremely long. And 22640 * the lookup can be in interrupt context. 22641 * 22642 * Note that in normal circumstances, this marking should not 22643 * affect anything. It would be nice if only 1 message is 22644 * needed to inform IP that the IRE created for this RST should 22645 * not be added to the cache table. But there is currently 22646 * not such communication mechanism between TCP and IP. So 22647 * the best we can do now is to send the advice ioctl to IP 22648 * to mark the IRE temporary. 22649 */ 22650 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22651 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22652 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22653 } 22654 } 22655 22656 /* 22657 * Initiate closedown sequence on an active connection. (May be called as 22658 * writer.) Return value zero for OK return, non-zero for error return. 22659 */ 22660 static int 22661 tcp_xmit_end(tcp_t *tcp) 22662 { 22663 ipic_t *ipic; 22664 mblk_t *mp; 22665 tcp_stack_t *tcps = tcp->tcp_tcps; 22666 22667 if (tcp->tcp_state < TCPS_SYN_RCVD || 22668 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22669 /* 22670 * Invalid state, only states TCPS_SYN_RCVD, 22671 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22672 */ 22673 return (-1); 22674 } 22675 22676 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22677 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22678 /* 22679 * If there is nothing more unsent, send the FIN now. 22680 * Otherwise, it will go out with the last segment. 22681 */ 22682 if (tcp->tcp_unsent == 0) { 22683 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22684 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22685 22686 if (mp) { 22687 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22688 tcp_send_data(tcp, tcp->tcp_wq, mp); 22689 } else { 22690 /* 22691 * Couldn't allocate msg. Pretend we got it out. 22692 * Wait for rexmit timeout. 22693 */ 22694 tcp->tcp_snxt = tcp->tcp_fss + 1; 22695 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22696 } 22697 22698 /* 22699 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22700 * changed. 22701 */ 22702 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22703 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22704 } 22705 } else { 22706 /* 22707 * If tcp->tcp_cork is set, then the data will not get sent, 22708 * so we have to check that and unset it first. 22709 */ 22710 if (tcp->tcp_cork) 22711 tcp->tcp_cork = B_FALSE; 22712 tcp_wput_data(tcp, NULL, B_FALSE); 22713 } 22714 22715 /* 22716 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22717 * is 0, don't update the cache. 22718 */ 22719 if (tcps->tcps_rtt_updates == 0 || 22720 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22721 return (0); 22722 22723 /* 22724 * NOTE: should not update if source routes i.e. if tcp_remote if 22725 * different from the destination. 22726 */ 22727 if (tcp->tcp_ipversion == IPV4_VERSION) { 22728 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22729 return (0); 22730 } 22731 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22732 &ipic); 22733 } else { 22734 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22735 &tcp->tcp_ip6h->ip6_dst))) { 22736 return (0); 22737 } 22738 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22739 &ipic); 22740 } 22741 22742 /* Record route attributes in the IRE for use by future connections. */ 22743 if (mp == NULL) 22744 return (0); 22745 22746 /* 22747 * We do not have a good algorithm to update ssthresh at this time. 22748 * So don't do any update. 22749 */ 22750 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22751 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22752 22753 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22754 return (0); 22755 } 22756 22757 /* 22758 * Generate a "no listener here" RST in response to an "unknown" segment. 22759 * Note that we are reusing the incoming mp to construct the outgoing 22760 * RST. 22761 */ 22762 void 22763 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22764 tcp_stack_t *tcps) 22765 { 22766 uchar_t *rptr; 22767 uint32_t seg_len; 22768 tcph_t *tcph; 22769 uint32_t seg_seq; 22770 uint32_t seg_ack; 22771 uint_t flags; 22772 mblk_t *ipsec_mp; 22773 ipha_t *ipha; 22774 ip6_t *ip6h; 22775 boolean_t mctl_present = B_FALSE; 22776 boolean_t check = B_TRUE; 22777 boolean_t policy_present; 22778 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22779 22780 TCP_STAT(tcps, tcp_no_listener); 22781 22782 ipsec_mp = mp; 22783 22784 if (mp->b_datap->db_type == M_CTL) { 22785 ipsec_in_t *ii; 22786 22787 mctl_present = B_TRUE; 22788 mp = mp->b_cont; 22789 22790 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22791 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22792 if (ii->ipsec_in_dont_check) { 22793 check = B_FALSE; 22794 if (!ii->ipsec_in_secure) { 22795 freeb(ipsec_mp); 22796 mctl_present = B_FALSE; 22797 ipsec_mp = mp; 22798 } 22799 } 22800 } 22801 22802 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22803 policy_present = ipss->ipsec_inbound_v4_policy_present; 22804 ipha = (ipha_t *)mp->b_rptr; 22805 ip6h = NULL; 22806 } else { 22807 policy_present = ipss->ipsec_inbound_v6_policy_present; 22808 ipha = NULL; 22809 ip6h = (ip6_t *)mp->b_rptr; 22810 } 22811 22812 if (check && policy_present) { 22813 /* 22814 * The conn_t parameter is NULL because we already know 22815 * nobody's home. 22816 */ 22817 ipsec_mp = ipsec_check_global_policy( 22818 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22819 tcps->tcps_netstack); 22820 if (ipsec_mp == NULL) 22821 return; 22822 } 22823 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22824 DTRACE_PROBE2( 22825 tx__ip__log__error__nolistener__tcp, 22826 char *, "Could not reply with RST to mp(1)", 22827 mblk_t *, mp); 22828 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22829 freemsg(ipsec_mp); 22830 return; 22831 } 22832 22833 rptr = mp->b_rptr; 22834 22835 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22836 seg_seq = BE32_TO_U32(tcph->th_seq); 22837 seg_ack = BE32_TO_U32(tcph->th_ack); 22838 flags = tcph->th_flags[0]; 22839 22840 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22841 if (flags & TH_RST) { 22842 freemsg(ipsec_mp); 22843 } else if (flags & TH_ACK) { 22844 tcp_xmit_early_reset("no tcp, reset", 22845 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps); 22846 } else { 22847 if (flags & TH_SYN) { 22848 seg_len++; 22849 } else { 22850 /* 22851 * Here we violate the RFC. Note that a normal 22852 * TCP will never send a segment without the ACK 22853 * flag, except for RST or SYN segment. This 22854 * segment is neither. Just drop it on the 22855 * floor. 22856 */ 22857 freemsg(ipsec_mp); 22858 tcps->tcps_rst_unsent++; 22859 return; 22860 } 22861 22862 tcp_xmit_early_reset("no tcp, reset/ack", 22863 ipsec_mp, 0, seg_seq + seg_len, 22864 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps); 22865 } 22866 } 22867 22868 /* 22869 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22870 * ip and tcp header ready to pass down to IP. If the mp passed in is 22871 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22872 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22873 * otherwise it will dup partial mblks.) 22874 * Otherwise, an appropriate ACK packet will be generated. This 22875 * routine is not usually called to send new data for the first time. It 22876 * is mostly called out of the timer for retransmits, and to generate ACKs. 22877 * 22878 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22879 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22880 * of the original mblk chain will be returned in *offset and *end_mp. 22881 */ 22882 mblk_t * 22883 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22884 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22885 boolean_t rexmit) 22886 { 22887 int data_length; 22888 int32_t off = 0; 22889 uint_t flags; 22890 mblk_t *mp1; 22891 mblk_t *mp2; 22892 uchar_t *rptr; 22893 tcph_t *tcph; 22894 int32_t num_sack_blk = 0; 22895 int32_t sack_opt_len = 0; 22896 tcp_stack_t *tcps = tcp->tcp_tcps; 22897 22898 /* Allocate for our maximum TCP header + link-level */ 22899 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22900 tcps->tcps_wroff_xtra, BPRI_MED); 22901 if (!mp1) 22902 return (NULL); 22903 data_length = 0; 22904 22905 /* 22906 * Note that tcp_mss has been adjusted to take into account the 22907 * timestamp option if applicable. Because SACK options do not 22908 * appear in every TCP segments and they are of variable lengths, 22909 * they cannot be included in tcp_mss. Thus we need to calculate 22910 * the actual segment length when we need to send a segment which 22911 * includes SACK options. 22912 */ 22913 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22914 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22915 tcp->tcp_num_sack_blk); 22916 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22917 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22918 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22919 max_to_send -= sack_opt_len; 22920 } 22921 22922 if (offset != NULL) { 22923 off = *offset; 22924 /* We use offset as an indicator that end_mp is not NULL. */ 22925 *end_mp = NULL; 22926 } 22927 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22928 /* This could be faster with cooperation from downstream */ 22929 if (mp2 != mp1 && !sendall && 22930 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22931 max_to_send) 22932 /* 22933 * Don't send the next mblk since the whole mblk 22934 * does not fit. 22935 */ 22936 break; 22937 mp2->b_cont = dupb(mp); 22938 mp2 = mp2->b_cont; 22939 if (!mp2) { 22940 freemsg(mp1); 22941 return (NULL); 22942 } 22943 mp2->b_rptr += off; 22944 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22945 (uintptr_t)INT_MAX); 22946 22947 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22948 if (data_length > max_to_send) { 22949 mp2->b_wptr -= data_length - max_to_send; 22950 data_length = max_to_send; 22951 off = mp2->b_wptr - mp->b_rptr; 22952 break; 22953 } else { 22954 off = 0; 22955 } 22956 } 22957 if (offset != NULL) { 22958 *offset = off; 22959 *end_mp = mp; 22960 } 22961 if (seg_len != NULL) { 22962 *seg_len = data_length; 22963 } 22964 22965 /* Update the latest receive window size in TCP header. */ 22966 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22967 tcp->tcp_tcph->th_win); 22968 22969 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22970 mp1->b_rptr = rptr; 22971 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22972 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22973 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22974 U32_TO_ABE32(seq, tcph->th_seq); 22975 22976 /* 22977 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22978 * that this function was called from tcp_wput_data. Thus, when called 22979 * to retransmit data the setting of the PUSH bit may appear some 22980 * what random in that it might get set when it should not. This 22981 * should not pose any performance issues. 22982 */ 22983 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22984 tcp->tcp_unsent == data_length)) { 22985 flags = TH_ACK | TH_PUSH; 22986 } else { 22987 flags = TH_ACK; 22988 } 22989 22990 if (tcp->tcp_ecn_ok) { 22991 if (tcp->tcp_ecn_echo_on) 22992 flags |= TH_ECE; 22993 22994 /* 22995 * Only set ECT bit and ECN_CWR if a segment contains new data. 22996 * There is no TCP flow control for non-data segments, and 22997 * only data segment is transmitted reliably. 22998 */ 22999 if (data_length > 0 && !rexmit) { 23000 SET_ECT(tcp, rptr); 23001 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23002 flags |= TH_CWR; 23003 tcp->tcp_ecn_cwr_sent = B_TRUE; 23004 } 23005 } 23006 } 23007 23008 if (tcp->tcp_valid_bits) { 23009 uint32_t u1; 23010 23011 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23012 seq == tcp->tcp_iss) { 23013 uchar_t *wptr; 23014 23015 /* 23016 * If TCP_ISS_VALID and the seq number is tcp_iss, 23017 * TCP can only be in SYN-SENT, SYN-RCVD or 23018 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23019 * our SYN is not ack'ed but the app closes this 23020 * TCP connection. 23021 */ 23022 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23023 tcp->tcp_state == TCPS_SYN_RCVD || 23024 tcp->tcp_state == TCPS_FIN_WAIT_1); 23025 23026 /* 23027 * Tack on the MSS option. It is always needed 23028 * for both active and passive open. 23029 * 23030 * MSS option value should be interface MTU - MIN 23031 * TCP/IP header according to RFC 793 as it means 23032 * the maximum segment size TCP can receive. But 23033 * to get around some broken middle boxes/end hosts 23034 * out there, we allow the option value to be the 23035 * same as the MSS option size on the peer side. 23036 * In this way, the other side will not send 23037 * anything larger than they can receive. 23038 * 23039 * Note that for SYN_SENT state, the ndd param 23040 * tcp_use_smss_as_mss_opt has no effect as we 23041 * don't know the peer's MSS option value. So 23042 * the only case we need to take care of is in 23043 * SYN_RCVD state, which is done later. 23044 */ 23045 wptr = mp1->b_wptr; 23046 wptr[0] = TCPOPT_MAXSEG; 23047 wptr[1] = TCPOPT_MAXSEG_LEN; 23048 wptr += 2; 23049 u1 = tcp->tcp_if_mtu - 23050 (tcp->tcp_ipversion == IPV4_VERSION ? 23051 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23052 TCP_MIN_HEADER_LENGTH; 23053 U16_TO_BE16(u1, wptr); 23054 mp1->b_wptr = wptr + 2; 23055 /* Update the offset to cover the additional word */ 23056 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23057 23058 /* 23059 * Note that the following way of filling in 23060 * TCP options are not optimal. Some NOPs can 23061 * be saved. But there is no need at this time 23062 * to optimize it. When it is needed, we will 23063 * do it. 23064 */ 23065 switch (tcp->tcp_state) { 23066 case TCPS_SYN_SENT: 23067 flags = TH_SYN; 23068 23069 if (tcp->tcp_snd_ts_ok) { 23070 uint32_t llbolt = (uint32_t)lbolt; 23071 23072 wptr = mp1->b_wptr; 23073 wptr[0] = TCPOPT_NOP; 23074 wptr[1] = TCPOPT_NOP; 23075 wptr[2] = TCPOPT_TSTAMP; 23076 wptr[3] = TCPOPT_TSTAMP_LEN; 23077 wptr += 4; 23078 U32_TO_BE32(llbolt, wptr); 23079 wptr += 4; 23080 ASSERT(tcp->tcp_ts_recent == 0); 23081 U32_TO_BE32(0L, wptr); 23082 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23083 tcph->th_offset_and_rsrvd[0] += 23084 (3 << 4); 23085 } 23086 23087 /* 23088 * Set up all the bits to tell other side 23089 * we are ECN capable. 23090 */ 23091 if (tcp->tcp_ecn_ok) { 23092 flags |= (TH_ECE | TH_CWR); 23093 } 23094 break; 23095 case TCPS_SYN_RCVD: 23096 flags |= TH_SYN; 23097 23098 /* 23099 * Reset the MSS option value to be SMSS 23100 * We should probably add back the bytes 23101 * for timestamp option and IPsec. We 23102 * don't do that as this is a workaround 23103 * for broken middle boxes/end hosts, it 23104 * is better for us to be more cautious. 23105 * They may not take these things into 23106 * account in their SMSS calculation. Thus 23107 * the peer's calculated SMSS may be smaller 23108 * than what it can be. This should be OK. 23109 */ 23110 if (tcps->tcps_use_smss_as_mss_opt) { 23111 u1 = tcp->tcp_mss; 23112 U16_TO_BE16(u1, wptr); 23113 } 23114 23115 /* 23116 * If the other side is ECN capable, reply 23117 * that we are also ECN capable. 23118 */ 23119 if (tcp->tcp_ecn_ok) 23120 flags |= TH_ECE; 23121 break; 23122 default: 23123 /* 23124 * The above ASSERT() makes sure that this 23125 * must be FIN-WAIT-1 state. Our SYN has 23126 * not been ack'ed so retransmit it. 23127 */ 23128 flags |= TH_SYN; 23129 break; 23130 } 23131 23132 if (tcp->tcp_snd_ws_ok) { 23133 wptr = mp1->b_wptr; 23134 wptr[0] = TCPOPT_NOP; 23135 wptr[1] = TCPOPT_WSCALE; 23136 wptr[2] = TCPOPT_WS_LEN; 23137 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23138 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23139 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23140 } 23141 23142 if (tcp->tcp_snd_sack_ok) { 23143 wptr = mp1->b_wptr; 23144 wptr[0] = TCPOPT_NOP; 23145 wptr[1] = TCPOPT_NOP; 23146 wptr[2] = TCPOPT_SACK_PERMITTED; 23147 wptr[3] = TCPOPT_SACK_OK_LEN; 23148 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23149 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23150 } 23151 23152 /* allocb() of adequate mblk assures space */ 23153 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23154 (uintptr_t)INT_MAX); 23155 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23156 /* 23157 * Get IP set to checksum on our behalf 23158 * Include the adjustment for a source route if any. 23159 */ 23160 u1 += tcp->tcp_sum; 23161 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23162 U16_TO_BE16(u1, tcph->th_sum); 23163 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23164 } 23165 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23166 (seq + data_length) == tcp->tcp_fss) { 23167 if (!tcp->tcp_fin_acked) { 23168 flags |= TH_FIN; 23169 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23170 } 23171 if (!tcp->tcp_fin_sent) { 23172 tcp->tcp_fin_sent = B_TRUE; 23173 switch (tcp->tcp_state) { 23174 case TCPS_SYN_RCVD: 23175 case TCPS_ESTABLISHED: 23176 tcp->tcp_state = TCPS_FIN_WAIT_1; 23177 break; 23178 case TCPS_CLOSE_WAIT: 23179 tcp->tcp_state = TCPS_LAST_ACK; 23180 break; 23181 } 23182 if (tcp->tcp_suna == tcp->tcp_snxt) 23183 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23184 tcp->tcp_snxt = tcp->tcp_fss + 1; 23185 } 23186 } 23187 /* 23188 * Note the trick here. u1 is unsigned. When tcp_urg 23189 * is smaller than seq, u1 will become a very huge value. 23190 * So the comparison will fail. Also note that tcp_urp 23191 * should be positive, see RFC 793 page 17. 23192 */ 23193 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23194 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23195 u1 < (uint32_t)(64 * 1024)) { 23196 flags |= TH_URG; 23197 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23198 U32_TO_ABE16(u1, tcph->th_urp); 23199 } 23200 } 23201 tcph->th_flags[0] = (uchar_t)flags; 23202 tcp->tcp_rack = tcp->tcp_rnxt; 23203 tcp->tcp_rack_cnt = 0; 23204 23205 if (tcp->tcp_snd_ts_ok) { 23206 if (tcp->tcp_state != TCPS_SYN_SENT) { 23207 uint32_t llbolt = (uint32_t)lbolt; 23208 23209 U32_TO_BE32(llbolt, 23210 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23211 U32_TO_BE32(tcp->tcp_ts_recent, 23212 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23213 } 23214 } 23215 23216 if (num_sack_blk > 0) { 23217 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23218 sack_blk_t *tmp; 23219 int32_t i; 23220 23221 wptr[0] = TCPOPT_NOP; 23222 wptr[1] = TCPOPT_NOP; 23223 wptr[2] = TCPOPT_SACK; 23224 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23225 sizeof (sack_blk_t); 23226 wptr += TCPOPT_REAL_SACK_LEN; 23227 23228 tmp = tcp->tcp_sack_list; 23229 for (i = 0; i < num_sack_blk; i++) { 23230 U32_TO_BE32(tmp[i].begin, wptr); 23231 wptr += sizeof (tcp_seq); 23232 U32_TO_BE32(tmp[i].end, wptr); 23233 wptr += sizeof (tcp_seq); 23234 } 23235 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23236 } 23237 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23238 data_length += (int)(mp1->b_wptr - rptr); 23239 if (tcp->tcp_ipversion == IPV4_VERSION) { 23240 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23241 } else { 23242 ip6_t *ip6 = (ip6_t *)(rptr + 23243 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23244 sizeof (ip6i_t) : 0)); 23245 23246 ip6->ip6_plen = htons(data_length - 23247 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23248 } 23249 23250 /* 23251 * Prime pump for IP 23252 * Include the adjustment for a source route if any. 23253 */ 23254 data_length -= tcp->tcp_ip_hdr_len; 23255 data_length += tcp->tcp_sum; 23256 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23257 U16_TO_ABE16(data_length, tcph->th_sum); 23258 if (tcp->tcp_ip_forward_progress) { 23259 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23260 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23261 tcp->tcp_ip_forward_progress = B_FALSE; 23262 } 23263 return (mp1); 23264 } 23265 23266 /* This function handles the push timeout. */ 23267 void 23268 tcp_push_timer(void *arg) 23269 { 23270 conn_t *connp = (conn_t *)arg; 23271 tcp_t *tcp = connp->conn_tcp; 23272 tcp_stack_t *tcps = tcp->tcp_tcps; 23273 23274 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23275 23276 ASSERT(tcp->tcp_listener == NULL); 23277 23278 /* 23279 * We need to plug synchronous streams during our drain to prevent 23280 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23281 */ 23282 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23283 tcp->tcp_push_tid = 0; 23284 if ((tcp->tcp_rcv_list != NULL) && 23285 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 23286 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23287 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23288 } 23289 23290 /* 23291 * This function handles delayed ACK timeout. 23292 */ 23293 static void 23294 tcp_ack_timer(void *arg) 23295 { 23296 conn_t *connp = (conn_t *)arg; 23297 tcp_t *tcp = connp->conn_tcp; 23298 mblk_t *mp; 23299 tcp_stack_t *tcps = tcp->tcp_tcps; 23300 23301 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23302 23303 tcp->tcp_ack_tid = 0; 23304 23305 if (tcp->tcp_fused) 23306 return; 23307 23308 /* 23309 * Do not send ACK if there is no outstanding unack'ed data. 23310 */ 23311 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23312 return; 23313 } 23314 23315 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23316 /* 23317 * Make sure we don't allow deferred ACKs to result in 23318 * timer-based ACKing. If we have held off an ACK 23319 * when there was more than an mss here, and the timer 23320 * goes off, we have to worry about the possibility 23321 * that the sender isn't doing slow-start, or is out 23322 * of step with us for some other reason. We fall 23323 * permanently back in the direction of 23324 * ACK-every-other-packet as suggested in RFC 1122. 23325 */ 23326 if (tcp->tcp_rack_abs_max > 2) 23327 tcp->tcp_rack_abs_max--; 23328 tcp->tcp_rack_cur_max = 2; 23329 } 23330 mp = tcp_ack_mp(tcp); 23331 23332 if (mp != NULL) { 23333 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 23334 BUMP_LOCAL(tcp->tcp_obsegs); 23335 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23336 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23337 tcp_send_data(tcp, tcp->tcp_wq, mp); 23338 } 23339 } 23340 23341 23342 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23343 static mblk_t * 23344 tcp_ack_mp(tcp_t *tcp) 23345 { 23346 uint32_t seq_no; 23347 tcp_stack_t *tcps = tcp->tcp_tcps; 23348 23349 /* 23350 * There are a few cases to be considered while setting the sequence no. 23351 * Essentially, we can come here while processing an unacceptable pkt 23352 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23353 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23354 * If we are here for a zero window probe, stick with suna. In all 23355 * other cases, we check if suna + swnd encompasses snxt and set 23356 * the sequence number to snxt, if so. If snxt falls outside the 23357 * window (the receiver probably shrunk its window), we will go with 23358 * suna + swnd, otherwise the sequence no will be unacceptable to the 23359 * receiver. 23360 */ 23361 if (tcp->tcp_zero_win_probe) { 23362 seq_no = tcp->tcp_suna; 23363 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23364 ASSERT(tcp->tcp_swnd == 0); 23365 seq_no = tcp->tcp_snxt; 23366 } else { 23367 seq_no = SEQ_GT(tcp->tcp_snxt, 23368 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23369 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23370 } 23371 23372 if (tcp->tcp_valid_bits) { 23373 /* 23374 * For the complex case where we have to send some 23375 * controls (FIN or SYN), let tcp_xmit_mp do it. 23376 */ 23377 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23378 NULL, B_FALSE)); 23379 } else { 23380 /* Generate a simple ACK */ 23381 int data_length; 23382 uchar_t *rptr; 23383 tcph_t *tcph; 23384 mblk_t *mp1; 23385 int32_t tcp_hdr_len; 23386 int32_t tcp_tcp_hdr_len; 23387 int32_t num_sack_blk = 0; 23388 int32_t sack_opt_len; 23389 23390 /* 23391 * Allocate space for TCP + IP headers 23392 * and link-level header 23393 */ 23394 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23395 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23396 tcp->tcp_num_sack_blk); 23397 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23398 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23399 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23400 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23401 } else { 23402 tcp_hdr_len = tcp->tcp_hdr_len; 23403 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23404 } 23405 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23406 if (!mp1) 23407 return (NULL); 23408 23409 /* Update the latest receive window size in TCP header. */ 23410 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23411 tcp->tcp_tcph->th_win); 23412 /* copy in prototype TCP + IP header */ 23413 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23414 mp1->b_rptr = rptr; 23415 mp1->b_wptr = rptr + tcp_hdr_len; 23416 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23417 23418 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23419 23420 /* Set the TCP sequence number. */ 23421 U32_TO_ABE32(seq_no, tcph->th_seq); 23422 23423 /* Set up the TCP flag field. */ 23424 tcph->th_flags[0] = (uchar_t)TH_ACK; 23425 if (tcp->tcp_ecn_echo_on) 23426 tcph->th_flags[0] |= TH_ECE; 23427 23428 tcp->tcp_rack = tcp->tcp_rnxt; 23429 tcp->tcp_rack_cnt = 0; 23430 23431 /* fill in timestamp option if in use */ 23432 if (tcp->tcp_snd_ts_ok) { 23433 uint32_t llbolt = (uint32_t)lbolt; 23434 23435 U32_TO_BE32(llbolt, 23436 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23437 U32_TO_BE32(tcp->tcp_ts_recent, 23438 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23439 } 23440 23441 /* Fill in SACK options */ 23442 if (num_sack_blk > 0) { 23443 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23444 sack_blk_t *tmp; 23445 int32_t i; 23446 23447 wptr[0] = TCPOPT_NOP; 23448 wptr[1] = TCPOPT_NOP; 23449 wptr[2] = TCPOPT_SACK; 23450 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23451 sizeof (sack_blk_t); 23452 wptr += TCPOPT_REAL_SACK_LEN; 23453 23454 tmp = tcp->tcp_sack_list; 23455 for (i = 0; i < num_sack_blk; i++) { 23456 U32_TO_BE32(tmp[i].begin, wptr); 23457 wptr += sizeof (tcp_seq); 23458 U32_TO_BE32(tmp[i].end, wptr); 23459 wptr += sizeof (tcp_seq); 23460 } 23461 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23462 << 4); 23463 } 23464 23465 if (tcp->tcp_ipversion == IPV4_VERSION) { 23466 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23467 } else { 23468 /* Check for ip6i_t header in sticky hdrs */ 23469 ip6_t *ip6 = (ip6_t *)(rptr + 23470 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23471 sizeof (ip6i_t) : 0)); 23472 23473 ip6->ip6_plen = htons(tcp_hdr_len - 23474 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23475 } 23476 23477 /* 23478 * Prime pump for checksum calculation in IP. Include the 23479 * adjustment for a source route if any. 23480 */ 23481 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23482 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23483 U16_TO_ABE16(data_length, tcph->th_sum); 23484 23485 if (tcp->tcp_ip_forward_progress) { 23486 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23487 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23488 tcp->tcp_ip_forward_progress = B_FALSE; 23489 } 23490 return (mp1); 23491 } 23492 } 23493 23494 /* 23495 * To create a temporary tcp structure for inserting into bind hash list. 23496 * The parameter is assumed to be in network byte order, ready for use. 23497 */ 23498 /* ARGSUSED */ 23499 static tcp_t * 23500 tcp_alloc_temp_tcp(in_port_t port, tcp_stack_t *tcps) 23501 { 23502 conn_t *connp; 23503 tcp_t *tcp; 23504 23505 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP, tcps->tcps_netstack); 23506 if (connp == NULL) 23507 return (NULL); 23508 23509 tcp = connp->conn_tcp; 23510 tcp->tcp_tcps = tcps; 23511 TCPS_REFHOLD(tcps); 23512 23513 /* 23514 * Only initialize the necessary info in those structures. Note 23515 * that since INADDR_ANY is all 0, we do not need to set 23516 * tcp_bound_source to INADDR_ANY here. 23517 */ 23518 tcp->tcp_state = TCPS_BOUND; 23519 tcp->tcp_lport = port; 23520 tcp->tcp_exclbind = 1; 23521 tcp->tcp_reserved_port = 1; 23522 23523 /* Just for place holding... */ 23524 tcp->tcp_ipversion = IPV4_VERSION; 23525 23526 return (tcp); 23527 } 23528 23529 /* 23530 * To remove a port range specified by lo_port and hi_port from the 23531 * reserved port ranges. This is one of the three public functions of 23532 * the reserved port interface. Note that a port range has to be removed 23533 * as a whole. Ports in a range cannot be removed individually. 23534 * 23535 * Params: 23536 * in_port_t lo_port: the beginning port of the reserved port range to 23537 * be deleted. 23538 * in_port_t hi_port: the ending port of the reserved port range to 23539 * be deleted. 23540 * 23541 * Return: 23542 * B_TRUE if the deletion is successful, B_FALSE otherwise. 23543 * 23544 * Assumes that nca is only for zoneid=0 23545 */ 23546 boolean_t 23547 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 23548 { 23549 int i, j; 23550 int size; 23551 tcp_t **temp_tcp_array; 23552 tcp_t *tcp; 23553 tcp_stack_t *tcps; 23554 23555 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23556 ASSERT(tcps != NULL); 23557 23558 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23559 23560 /* First make sure that the port ranage is indeed reserved. */ 23561 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23562 if (tcps->tcps_reserved_port[i].lo_port == lo_port) { 23563 hi_port = tcps->tcps_reserved_port[i].hi_port; 23564 temp_tcp_array = 23565 tcps->tcps_reserved_port[i].temp_tcp_array; 23566 break; 23567 } 23568 } 23569 if (i == tcps->tcps_reserved_port_array_size) { 23570 rw_exit(&tcps->tcps_reserved_port_lock); 23571 netstack_rele(tcps->tcps_netstack); 23572 return (B_FALSE); 23573 } 23574 23575 /* 23576 * Remove the range from the array. This simple loop is possible 23577 * because port ranges are inserted in ascending order. 23578 */ 23579 for (j = i; j < tcps->tcps_reserved_port_array_size - 1; j++) { 23580 tcps->tcps_reserved_port[j].lo_port = 23581 tcps->tcps_reserved_port[j+1].lo_port; 23582 tcps->tcps_reserved_port[j].hi_port = 23583 tcps->tcps_reserved_port[j+1].hi_port; 23584 tcps->tcps_reserved_port[j].temp_tcp_array = 23585 tcps->tcps_reserved_port[j+1].temp_tcp_array; 23586 } 23587 23588 /* Remove all the temporary tcp structures. */ 23589 size = hi_port - lo_port + 1; 23590 while (size > 0) { 23591 tcp = temp_tcp_array[size - 1]; 23592 ASSERT(tcp != NULL); 23593 tcp_bind_hash_remove(tcp); 23594 CONN_DEC_REF(tcp->tcp_connp); 23595 size--; 23596 } 23597 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 23598 tcps->tcps_reserved_port_array_size--; 23599 rw_exit(&tcps->tcps_reserved_port_lock); 23600 netstack_rele(tcps->tcps_netstack); 23601 return (B_TRUE); 23602 } 23603 23604 /* 23605 * Macro to remove temporary tcp structure from the bind hash list. The 23606 * first parameter is the list of tcp to be removed. The second parameter 23607 * is the number of tcps in the array. 23608 */ 23609 #define TCP_TMP_TCP_REMOVE(tcp_array, num, tcps) \ 23610 { \ 23611 while ((num) > 0) { \ 23612 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 23613 tf_t *tbf; \ 23614 tcp_t *tcpnext; \ 23615 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 23616 mutex_enter(&tbf->tf_lock); \ 23617 tcpnext = tcp->tcp_bind_hash; \ 23618 if (tcpnext) { \ 23619 tcpnext->tcp_ptpbhn = \ 23620 tcp->tcp_ptpbhn; \ 23621 } \ 23622 *tcp->tcp_ptpbhn = tcpnext; \ 23623 mutex_exit(&tbf->tf_lock); \ 23624 kmem_free(tcp, sizeof (tcp_t)); \ 23625 (tcp_array)[(num) - 1] = NULL; \ 23626 (num)--; \ 23627 } \ 23628 } 23629 23630 /* 23631 * The public interface for other modules to call to reserve a port range 23632 * in TCP. The caller passes in how large a port range it wants. TCP 23633 * will try to find a range and return it via lo_port and hi_port. This is 23634 * used by NCA's nca_conn_init. 23635 * NCA can only be used in the global zone so this only affects the global 23636 * zone's ports. 23637 * 23638 * Params: 23639 * int size: the size of the port range to be reserved. 23640 * in_port_t *lo_port (referenced): returns the beginning port of the 23641 * reserved port range added. 23642 * in_port_t *hi_port (referenced): returns the ending port of the 23643 * reserved port range added. 23644 * 23645 * Return: 23646 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 23647 * 23648 * Assumes that nca is only for zoneid=0 23649 */ 23650 boolean_t 23651 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 23652 { 23653 tcp_t *tcp; 23654 tcp_t *tmp_tcp; 23655 tcp_t **temp_tcp_array; 23656 tf_t *tbf; 23657 in_port_t net_port; 23658 in_port_t port; 23659 int32_t cur_size; 23660 int i, j; 23661 boolean_t used; 23662 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 23663 zoneid_t zoneid = GLOBAL_ZONEID; 23664 tcp_stack_t *tcps; 23665 23666 /* Sanity check. */ 23667 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 23668 return (B_FALSE); 23669 } 23670 23671 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23672 ASSERT(tcps != NULL); 23673 23674 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23675 if (tcps->tcps_reserved_port_array_size == 23676 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 23677 rw_exit(&tcps->tcps_reserved_port_lock); 23678 netstack_rele(tcps->tcps_netstack); 23679 return (B_FALSE); 23680 } 23681 23682 /* 23683 * Find the starting port to try. Since the port ranges are ordered 23684 * in the reserved port array, we can do a simple search here. 23685 */ 23686 *lo_port = TCP_SMALLEST_RESERVED_PORT; 23687 *hi_port = TCP_LARGEST_RESERVED_PORT; 23688 for (i = 0; i < tcps->tcps_reserved_port_array_size; 23689 *lo_port = tcps->tcps_reserved_port[i].hi_port + 1, i++) { 23690 if (tcps->tcps_reserved_port[i].lo_port - *lo_port >= size) { 23691 *hi_port = tcps->tcps_reserved_port[i].lo_port - 1; 23692 break; 23693 } 23694 } 23695 /* No available port range. */ 23696 if (i == tcps->tcps_reserved_port_array_size && 23697 *hi_port - *lo_port < size) { 23698 rw_exit(&tcps->tcps_reserved_port_lock); 23699 netstack_rele(tcps->tcps_netstack); 23700 return (B_FALSE); 23701 } 23702 23703 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 23704 if (temp_tcp_array == NULL) { 23705 rw_exit(&tcps->tcps_reserved_port_lock); 23706 netstack_rele(tcps->tcps_netstack); 23707 return (B_FALSE); 23708 } 23709 23710 /* Go thru the port range to see if some ports are already bound. */ 23711 for (port = *lo_port, cur_size = 0; 23712 cur_size < size && port <= *hi_port; 23713 cur_size++, port++) { 23714 used = B_FALSE; 23715 net_port = htons(port); 23716 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(net_port)]; 23717 mutex_enter(&tbf->tf_lock); 23718 for (tcp = tbf->tf_tcp; tcp != NULL; 23719 tcp = tcp->tcp_bind_hash) { 23720 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 23721 net_port == tcp->tcp_lport) { 23722 /* 23723 * A port is already bound. Search again 23724 * starting from port + 1. Release all 23725 * temporary tcps. 23726 */ 23727 mutex_exit(&tbf->tf_lock); 23728 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23729 tcps); 23730 *lo_port = port + 1; 23731 cur_size = -1; 23732 used = B_TRUE; 23733 break; 23734 } 23735 } 23736 if (!used) { 23737 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port, tcps)) == 23738 NULL) { 23739 /* 23740 * Allocation failure. Just fail the request. 23741 * Need to remove all those temporary tcp 23742 * structures. 23743 */ 23744 mutex_exit(&tbf->tf_lock); 23745 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23746 tcps); 23747 rw_exit(&tcps->tcps_reserved_port_lock); 23748 kmem_free(temp_tcp_array, 23749 (hi_port - lo_port + 1) * 23750 sizeof (tcp_t *)); 23751 netstack_rele(tcps->tcps_netstack); 23752 return (B_FALSE); 23753 } 23754 temp_tcp_array[cur_size] = tmp_tcp; 23755 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 23756 mutex_exit(&tbf->tf_lock); 23757 } 23758 } 23759 23760 /* 23761 * The current range is not large enough. We can actually do another 23762 * search if this search is done between 2 reserved port ranges. But 23763 * for first release, we just stop here and return saying that no port 23764 * range is available. 23765 */ 23766 if (cur_size < size) { 23767 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, tcps); 23768 rw_exit(&tcps->tcps_reserved_port_lock); 23769 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 23770 netstack_rele(tcps->tcps_netstack); 23771 return (B_FALSE); 23772 } 23773 *hi_port = port - 1; 23774 23775 /* 23776 * Insert range into array in ascending order. Since this function 23777 * must not be called often, we choose to use the simplest method. 23778 * The above array should not consume excessive stack space as 23779 * the size must be very small. If in future releases, we find 23780 * that we should provide more reserved port ranges, this function 23781 * has to be modified to be more efficient. 23782 */ 23783 if (tcps->tcps_reserved_port_array_size == 0) { 23784 tcps->tcps_reserved_port[0].lo_port = *lo_port; 23785 tcps->tcps_reserved_port[0].hi_port = *hi_port; 23786 tcps->tcps_reserved_port[0].temp_tcp_array = temp_tcp_array; 23787 } else { 23788 for (i = 0, j = 0; i < tcps->tcps_reserved_port_array_size; 23789 i++, j++) { 23790 if (*lo_port < tcps->tcps_reserved_port[i].lo_port && 23791 i == j) { 23792 tmp_ports[j].lo_port = *lo_port; 23793 tmp_ports[j].hi_port = *hi_port; 23794 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23795 j++; 23796 } 23797 tmp_ports[j].lo_port = 23798 tcps->tcps_reserved_port[i].lo_port; 23799 tmp_ports[j].hi_port = 23800 tcps->tcps_reserved_port[i].hi_port; 23801 tmp_ports[j].temp_tcp_array = 23802 tcps->tcps_reserved_port[i].temp_tcp_array; 23803 } 23804 if (j == i) { 23805 tmp_ports[j].lo_port = *lo_port; 23806 tmp_ports[j].hi_port = *hi_port; 23807 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23808 } 23809 bcopy(tmp_ports, tcps->tcps_reserved_port, sizeof (tmp_ports)); 23810 } 23811 tcps->tcps_reserved_port_array_size++; 23812 rw_exit(&tcps->tcps_reserved_port_lock); 23813 netstack_rele(tcps->tcps_netstack); 23814 return (B_TRUE); 23815 } 23816 23817 /* 23818 * Check to see if a port is in any reserved port range. 23819 * 23820 * Params: 23821 * in_port_t port: the port to be verified. 23822 * 23823 * Return: 23824 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 23825 */ 23826 boolean_t 23827 tcp_reserved_port_check(in_port_t port, tcp_stack_t *tcps) 23828 { 23829 int i; 23830 23831 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23832 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23833 if (port >= tcps->tcps_reserved_port[i].lo_port || 23834 port <= tcps->tcps_reserved_port[i].hi_port) { 23835 rw_exit(&tcps->tcps_reserved_port_lock); 23836 return (B_TRUE); 23837 } 23838 } 23839 rw_exit(&tcps->tcps_reserved_port_lock); 23840 return (B_FALSE); 23841 } 23842 23843 /* 23844 * To list all reserved port ranges. This is the function to handle 23845 * ndd tcp_reserved_port_list. 23846 */ 23847 /* ARGSUSED */ 23848 static int 23849 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23850 { 23851 int i; 23852 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23853 23854 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23855 if (tcps->tcps_reserved_port_array_size > 0) 23856 (void) mi_mpprintf(mp, "The following ports are reserved:"); 23857 else 23858 (void) mi_mpprintf(mp, "No port is reserved."); 23859 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23860 (void) mi_mpprintf(mp, "%d-%d", 23861 tcps->tcps_reserved_port[i].lo_port, 23862 tcps->tcps_reserved_port[i].hi_port); 23863 } 23864 rw_exit(&tcps->tcps_reserved_port_lock); 23865 return (0); 23866 } 23867 23868 /* 23869 * Hash list insertion routine for tcp_t structures. 23870 * Inserts entries with the ones bound to a specific IP address first 23871 * followed by those bound to INADDR_ANY. 23872 */ 23873 static void 23874 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23875 { 23876 tcp_t **tcpp; 23877 tcp_t *tcpnext; 23878 23879 if (tcp->tcp_ptpbhn != NULL) { 23880 ASSERT(!caller_holds_lock); 23881 tcp_bind_hash_remove(tcp); 23882 } 23883 tcpp = &tbf->tf_tcp; 23884 if (!caller_holds_lock) { 23885 mutex_enter(&tbf->tf_lock); 23886 } else { 23887 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23888 } 23889 tcpnext = tcpp[0]; 23890 if (tcpnext) { 23891 /* 23892 * If the new tcp bound to the INADDR_ANY address 23893 * and the first one in the list is not bound to 23894 * INADDR_ANY we skip all entries until we find the 23895 * first one bound to INADDR_ANY. 23896 * This makes sure that applications binding to a 23897 * specific address get preference over those binding to 23898 * INADDR_ANY. 23899 */ 23900 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23901 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23902 while ((tcpnext = tcpp[0]) != NULL && 23903 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23904 tcpp = &(tcpnext->tcp_bind_hash); 23905 if (tcpnext) 23906 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23907 } else 23908 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23909 } 23910 tcp->tcp_bind_hash = tcpnext; 23911 tcp->tcp_ptpbhn = tcpp; 23912 tcpp[0] = tcp; 23913 if (!caller_holds_lock) 23914 mutex_exit(&tbf->tf_lock); 23915 } 23916 23917 /* 23918 * Hash list removal routine for tcp_t structures. 23919 */ 23920 static void 23921 tcp_bind_hash_remove(tcp_t *tcp) 23922 { 23923 tcp_t *tcpnext; 23924 kmutex_t *lockp; 23925 tcp_stack_t *tcps = tcp->tcp_tcps; 23926 23927 if (tcp->tcp_ptpbhn == NULL) 23928 return; 23929 23930 /* 23931 * Extract the lock pointer in case there are concurrent 23932 * hash_remove's for this instance. 23933 */ 23934 ASSERT(tcp->tcp_lport != 0); 23935 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23936 23937 ASSERT(lockp != NULL); 23938 mutex_enter(lockp); 23939 if (tcp->tcp_ptpbhn) { 23940 tcpnext = tcp->tcp_bind_hash; 23941 if (tcpnext) { 23942 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23943 tcp->tcp_bind_hash = NULL; 23944 } 23945 *tcp->tcp_ptpbhn = tcpnext; 23946 tcp->tcp_ptpbhn = NULL; 23947 } 23948 mutex_exit(lockp); 23949 } 23950 23951 23952 /* 23953 * Hash list lookup routine for tcp_t structures. 23954 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23955 */ 23956 static tcp_t * 23957 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23958 { 23959 tf_t *tf; 23960 tcp_t *tcp; 23961 23962 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23963 mutex_enter(&tf->tf_lock); 23964 for (tcp = tf->tf_tcp; tcp != NULL; 23965 tcp = tcp->tcp_acceptor_hash) { 23966 if (tcp->tcp_acceptor_id == id) { 23967 CONN_INC_REF(tcp->tcp_connp); 23968 mutex_exit(&tf->tf_lock); 23969 return (tcp); 23970 } 23971 } 23972 mutex_exit(&tf->tf_lock); 23973 return (NULL); 23974 } 23975 23976 23977 /* 23978 * Hash list insertion routine for tcp_t structures. 23979 */ 23980 void 23981 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23982 { 23983 tf_t *tf; 23984 tcp_t **tcpp; 23985 tcp_t *tcpnext; 23986 tcp_stack_t *tcps = tcp->tcp_tcps; 23987 23988 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23989 23990 if (tcp->tcp_ptpahn != NULL) 23991 tcp_acceptor_hash_remove(tcp); 23992 tcpp = &tf->tf_tcp; 23993 mutex_enter(&tf->tf_lock); 23994 tcpnext = tcpp[0]; 23995 if (tcpnext) 23996 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23997 tcp->tcp_acceptor_hash = tcpnext; 23998 tcp->tcp_ptpahn = tcpp; 23999 tcpp[0] = tcp; 24000 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24001 mutex_exit(&tf->tf_lock); 24002 } 24003 24004 /* 24005 * Hash list removal routine for tcp_t structures. 24006 */ 24007 static void 24008 tcp_acceptor_hash_remove(tcp_t *tcp) 24009 { 24010 tcp_t *tcpnext; 24011 kmutex_t *lockp; 24012 24013 /* 24014 * Extract the lock pointer in case there are concurrent 24015 * hash_remove's for this instance. 24016 */ 24017 lockp = tcp->tcp_acceptor_lockp; 24018 24019 if (tcp->tcp_ptpahn == NULL) 24020 return; 24021 24022 ASSERT(lockp != NULL); 24023 mutex_enter(lockp); 24024 if (tcp->tcp_ptpahn) { 24025 tcpnext = tcp->tcp_acceptor_hash; 24026 if (tcpnext) { 24027 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24028 tcp->tcp_acceptor_hash = NULL; 24029 } 24030 *tcp->tcp_ptpahn = tcpnext; 24031 tcp->tcp_ptpahn = NULL; 24032 } 24033 mutex_exit(lockp); 24034 tcp->tcp_acceptor_lockp = NULL; 24035 } 24036 24037 /* ARGSUSED */ 24038 static int 24039 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 24040 { 24041 int error = 0; 24042 int retval; 24043 char *end; 24044 tcp_hsp_t *hsp; 24045 tcp_hsp_t *hspprev; 24046 ipaddr_t addr = 0; /* Address we're looking for */ 24047 in6_addr_t v6addr; /* Address we're looking for */ 24048 uint32_t hash; /* Hash of that address */ 24049 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24050 24051 /* 24052 * If the following variables are still zero after parsing the input 24053 * string, the user didn't specify them and we don't change them in 24054 * the HSP. 24055 */ 24056 24057 ipaddr_t mask = 0; /* Subnet mask */ 24058 in6_addr_t v6mask; 24059 long sendspace = 0; /* Send buffer size */ 24060 long recvspace = 0; /* Receive buffer size */ 24061 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 24062 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 24063 24064 rw_enter(&tcps->tcps_hsp_lock, RW_WRITER); 24065 24066 /* Parse and validate address */ 24067 if (af == AF_INET) { 24068 retval = inet_pton(af, value, &addr); 24069 if (retval == 1) 24070 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 24071 } else if (af == AF_INET6) { 24072 retval = inet_pton(af, value, &v6addr); 24073 } else { 24074 error = EINVAL; 24075 goto done; 24076 } 24077 if (retval == 0) { 24078 error = EINVAL; 24079 goto done; 24080 } 24081 24082 while ((*value) && *value != ' ') 24083 value++; 24084 24085 /* Parse individual keywords, set variables if found */ 24086 while (*value) { 24087 /* Skip leading blanks */ 24088 24089 while (*value == ' ' || *value == '\t') 24090 value++; 24091 24092 /* If at end of string, we're done */ 24093 24094 if (!*value) 24095 break; 24096 24097 /* We have a word, figure out what it is */ 24098 24099 if (strncmp("mask", value, 4) == 0) { 24100 value += 4; 24101 while (*value == ' ' || *value == '\t') 24102 value++; 24103 /* Parse subnet mask */ 24104 if (af == AF_INET) { 24105 retval = inet_pton(af, value, &mask); 24106 if (retval == 1) { 24107 V4MASK_TO_V6(mask, v6mask); 24108 } 24109 } else if (af == AF_INET6) { 24110 retval = inet_pton(af, value, &v6mask); 24111 } 24112 if (retval != 1) { 24113 error = EINVAL; 24114 goto done; 24115 } 24116 while ((*value) && *value != ' ') 24117 value++; 24118 } else if (strncmp("sendspace", value, 9) == 0) { 24119 value += 9; 24120 24121 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 24122 sendspace < TCP_XMIT_HIWATER || 24123 sendspace >= (1L<<30)) { 24124 error = EINVAL; 24125 goto done; 24126 } 24127 value = end; 24128 } else if (strncmp("recvspace", value, 9) == 0) { 24129 value += 9; 24130 24131 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 24132 recvspace < TCP_RECV_HIWATER || 24133 recvspace >= (1L<<30)) { 24134 error = EINVAL; 24135 goto done; 24136 } 24137 value = end; 24138 } else if (strncmp("timestamp", value, 9) == 0) { 24139 value += 9; 24140 24141 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 24142 timestamp < 0 || timestamp > 1) { 24143 error = EINVAL; 24144 goto done; 24145 } 24146 24147 /* 24148 * We increment timestamp so we know it's been set; 24149 * this is undone when we put it in the HSP 24150 */ 24151 timestamp++; 24152 value = end; 24153 } else if (strncmp("delete", value, 6) == 0) { 24154 value += 6; 24155 delete = B_TRUE; 24156 } else { 24157 error = EINVAL; 24158 goto done; 24159 } 24160 } 24161 24162 /* Hash address for lookup */ 24163 24164 hash = TCP_HSP_HASH(addr); 24165 24166 if (delete) { 24167 /* 24168 * Note that deletes don't return an error if the thing 24169 * we're trying to delete isn't there. 24170 */ 24171 if (tcps->tcps_hsp_hash == NULL) 24172 goto done; 24173 hsp = tcps->tcps_hsp_hash[hash]; 24174 24175 if (hsp) { 24176 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24177 &v6addr)) { 24178 tcps->tcps_hsp_hash[hash] = hsp->tcp_hsp_next; 24179 mi_free((char *)hsp); 24180 } else { 24181 hspprev = hsp; 24182 while ((hsp = hsp->tcp_hsp_next) != NULL) { 24183 if (IN6_ARE_ADDR_EQUAL( 24184 &hsp->tcp_hsp_addr_v6, &v6addr)) { 24185 hspprev->tcp_hsp_next = 24186 hsp->tcp_hsp_next; 24187 mi_free((char *)hsp); 24188 break; 24189 } 24190 hspprev = hsp; 24191 } 24192 } 24193 } 24194 } else { 24195 /* 24196 * We're adding/modifying an HSP. If we haven't already done 24197 * so, allocate the hash table. 24198 */ 24199 24200 if (!tcps->tcps_hsp_hash) { 24201 tcps->tcps_hsp_hash = (tcp_hsp_t **) 24202 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 24203 if (!tcps->tcps_hsp_hash) { 24204 error = EINVAL; 24205 goto done; 24206 } 24207 } 24208 24209 /* Get head of hash chain */ 24210 24211 hsp = tcps->tcps_hsp_hash[hash]; 24212 24213 /* Try to find pre-existing hsp on hash chain */ 24214 /* Doesn't handle CIDR prefixes. */ 24215 while (hsp) { 24216 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 24217 break; 24218 hsp = hsp->tcp_hsp_next; 24219 } 24220 24221 /* 24222 * If we didn't, create one with default values and put it 24223 * at head of hash chain 24224 */ 24225 24226 if (!hsp) { 24227 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 24228 if (!hsp) { 24229 error = EINVAL; 24230 goto done; 24231 } 24232 hsp->tcp_hsp_next = tcps->tcps_hsp_hash[hash]; 24233 tcps->tcps_hsp_hash[hash] = hsp; 24234 } 24235 24236 /* Set values that the user asked us to change */ 24237 24238 hsp->tcp_hsp_addr_v6 = v6addr; 24239 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 24240 hsp->tcp_hsp_vers = IPV4_VERSION; 24241 else 24242 hsp->tcp_hsp_vers = IPV6_VERSION; 24243 hsp->tcp_hsp_subnet_v6 = v6mask; 24244 if (sendspace > 0) 24245 hsp->tcp_hsp_sendspace = sendspace; 24246 if (recvspace > 0) 24247 hsp->tcp_hsp_recvspace = recvspace; 24248 if (timestamp > 0) 24249 hsp->tcp_hsp_tstamp = timestamp - 1; 24250 } 24251 24252 done: 24253 rw_exit(&tcps->tcps_hsp_lock); 24254 return (error); 24255 } 24256 24257 /* Set callback routine passed to nd_load by tcp_param_register. */ 24258 /* ARGSUSED */ 24259 static int 24260 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 24261 { 24262 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 24263 } 24264 /* ARGSUSED */ 24265 static int 24266 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24267 cred_t *cr) 24268 { 24269 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 24270 } 24271 24272 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 24273 /* ARGSUSED */ 24274 static int 24275 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 24276 { 24277 tcp_hsp_t *hsp; 24278 int i; 24279 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 24280 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24281 24282 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24283 (void) mi_mpprintf(mp, 24284 "Hash HSP " MI_COL_HDRPAD_STR 24285 "Address Subnet Mask Send Receive TStamp"); 24286 if (tcps->tcps_hsp_hash) { 24287 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 24288 hsp = tcps->tcps_hsp_hash[i]; 24289 while (hsp) { 24290 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 24291 (void) inet_ntop(AF_INET, 24292 &hsp->tcp_hsp_addr, 24293 addrbuf, sizeof (addrbuf)); 24294 (void) inet_ntop(AF_INET, 24295 &hsp->tcp_hsp_subnet, 24296 subnetbuf, sizeof (subnetbuf)); 24297 } else { 24298 (void) inet_ntop(AF_INET6, 24299 &hsp->tcp_hsp_addr_v6, 24300 addrbuf, sizeof (addrbuf)); 24301 (void) inet_ntop(AF_INET6, 24302 &hsp->tcp_hsp_subnet_v6, 24303 subnetbuf, sizeof (subnetbuf)); 24304 } 24305 (void) mi_mpprintf(mp, 24306 " %03d " MI_COL_PTRFMT_STR 24307 "%s %s %010d %010d %d", 24308 i, 24309 (void *)hsp, 24310 addrbuf, 24311 subnetbuf, 24312 hsp->tcp_hsp_sendspace, 24313 hsp->tcp_hsp_recvspace, 24314 hsp->tcp_hsp_tstamp); 24315 24316 hsp = hsp->tcp_hsp_next; 24317 } 24318 } 24319 } 24320 rw_exit(&tcps->tcps_hsp_lock); 24321 return (0); 24322 } 24323 24324 24325 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24326 24327 static ipaddr_t netmasks[] = { 24328 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24329 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24330 }; 24331 24332 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24333 24334 /* 24335 * XXX This routine should go away and instead we should use the metrics 24336 * associated with the routes to determine the default sndspace and rcvspace. 24337 */ 24338 static tcp_hsp_t * 24339 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24340 { 24341 tcp_hsp_t *hsp = NULL; 24342 24343 /* Quick check without acquiring the lock. */ 24344 if (tcps->tcps_hsp_hash == NULL) 24345 return (NULL); 24346 24347 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24348 24349 /* This routine finds the best-matching HSP for address addr. */ 24350 24351 if (tcps->tcps_hsp_hash) { 24352 int i; 24353 ipaddr_t srchaddr; 24354 tcp_hsp_t *hsp_net; 24355 24356 /* We do three passes: host, network, and subnet. */ 24357 24358 srchaddr = addr; 24359 24360 for (i = 1; i <= 3; i++) { 24361 /* Look for exact match on srchaddr */ 24362 24363 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24364 while (hsp) { 24365 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24366 hsp->tcp_hsp_addr == srchaddr) 24367 break; 24368 hsp = hsp->tcp_hsp_next; 24369 } 24370 ASSERT(hsp == NULL || 24371 hsp->tcp_hsp_vers == IPV4_VERSION); 24372 24373 /* 24374 * If this is the first pass: 24375 * If we found a match, great, return it. 24376 * If not, search for the network on the second pass. 24377 */ 24378 24379 if (i == 1) 24380 if (hsp) 24381 break; 24382 else 24383 { 24384 srchaddr = addr & netmask(addr); 24385 continue; 24386 } 24387 24388 /* 24389 * If this is the second pass: 24390 * If we found a match, but there's a subnet mask, 24391 * save the match but try again using the subnet 24392 * mask on the third pass. 24393 * Otherwise, return whatever we found. 24394 */ 24395 24396 if (i == 2) { 24397 if (hsp && hsp->tcp_hsp_subnet) { 24398 hsp_net = hsp; 24399 srchaddr = addr & hsp->tcp_hsp_subnet; 24400 continue; 24401 } else { 24402 break; 24403 } 24404 } 24405 24406 /* 24407 * This must be the third pass. If we didn't find 24408 * anything, return the saved network HSP instead. 24409 */ 24410 24411 if (!hsp) 24412 hsp = hsp_net; 24413 } 24414 } 24415 24416 rw_exit(&tcps->tcps_hsp_lock); 24417 return (hsp); 24418 } 24419 24420 /* 24421 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24422 * match lookup. 24423 */ 24424 static tcp_hsp_t * 24425 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24426 { 24427 tcp_hsp_t *hsp = NULL; 24428 24429 /* Quick check without acquiring the lock. */ 24430 if (tcps->tcps_hsp_hash == NULL) 24431 return (NULL); 24432 24433 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24434 24435 /* This routine finds the best-matching HSP for address addr. */ 24436 24437 if (tcps->tcps_hsp_hash) { 24438 int i; 24439 in6_addr_t v6srchaddr; 24440 tcp_hsp_t *hsp_net; 24441 24442 /* We do three passes: host, network, and subnet. */ 24443 24444 v6srchaddr = *v6addr; 24445 24446 for (i = 1; i <= 3; i++) { 24447 /* Look for exact match on srchaddr */ 24448 24449 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24450 V4_PART_OF_V6(v6srchaddr))]; 24451 while (hsp) { 24452 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24453 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24454 &v6srchaddr)) 24455 break; 24456 hsp = hsp->tcp_hsp_next; 24457 } 24458 24459 /* 24460 * If this is the first pass: 24461 * If we found a match, great, return it. 24462 * If not, search for the network on the second pass. 24463 */ 24464 24465 if (i == 1) 24466 if (hsp) 24467 break; 24468 else { 24469 /* Assume a 64 bit mask */ 24470 v6srchaddr.s6_addr32[0] = 24471 v6addr->s6_addr32[0]; 24472 v6srchaddr.s6_addr32[1] = 24473 v6addr->s6_addr32[1]; 24474 v6srchaddr.s6_addr32[2] = 0; 24475 v6srchaddr.s6_addr32[3] = 0; 24476 continue; 24477 } 24478 24479 /* 24480 * If this is the second pass: 24481 * If we found a match, but there's a subnet mask, 24482 * save the match but try again using the subnet 24483 * mask on the third pass. 24484 * Otherwise, return whatever we found. 24485 */ 24486 24487 if (i == 2) { 24488 ASSERT(hsp == NULL || 24489 hsp->tcp_hsp_vers == IPV6_VERSION); 24490 if (hsp && 24491 !IN6_IS_ADDR_UNSPECIFIED( 24492 &hsp->tcp_hsp_subnet_v6)) { 24493 hsp_net = hsp; 24494 V6_MASK_COPY(*v6addr, 24495 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24496 continue; 24497 } else { 24498 break; 24499 } 24500 } 24501 24502 /* 24503 * This must be the third pass. If we didn't find 24504 * anything, return the saved network HSP instead. 24505 */ 24506 24507 if (!hsp) 24508 hsp = hsp_net; 24509 } 24510 } 24511 24512 rw_exit(&tcps->tcps_hsp_lock); 24513 return (hsp); 24514 } 24515 24516 /* 24517 * Type three generator adapted from the random() function in 4.4 BSD: 24518 */ 24519 24520 /* 24521 * Copyright (c) 1983, 1993 24522 * The Regents of the University of California. All rights reserved. 24523 * 24524 * Redistribution and use in source and binary forms, with or without 24525 * modification, are permitted provided that the following conditions 24526 * are met: 24527 * 1. Redistributions of source code must retain the above copyright 24528 * notice, this list of conditions and the following disclaimer. 24529 * 2. Redistributions in binary form must reproduce the above copyright 24530 * notice, this list of conditions and the following disclaimer in the 24531 * documentation and/or other materials provided with the distribution. 24532 * 3. All advertising materials mentioning features or use of this software 24533 * must display the following acknowledgement: 24534 * This product includes software developed by the University of 24535 * California, Berkeley and its contributors. 24536 * 4. Neither the name of the University nor the names of its contributors 24537 * may be used to endorse or promote products derived from this software 24538 * without specific prior written permission. 24539 * 24540 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24541 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24542 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24543 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24544 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24545 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24546 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24547 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24548 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24549 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24550 * SUCH DAMAGE. 24551 */ 24552 24553 /* Type 3 -- x**31 + x**3 + 1 */ 24554 #define DEG_3 31 24555 #define SEP_3 3 24556 24557 24558 /* Protected by tcp_random_lock */ 24559 static int tcp_randtbl[DEG_3 + 1]; 24560 24561 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24562 static int *tcp_random_rptr = &tcp_randtbl[1]; 24563 24564 static int *tcp_random_state = &tcp_randtbl[1]; 24565 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24566 24567 kmutex_t tcp_random_lock; 24568 24569 void 24570 tcp_random_init(void) 24571 { 24572 int i; 24573 hrtime_t hrt; 24574 time_t wallclock; 24575 uint64_t result; 24576 24577 /* 24578 * Use high-res timer and current time for seed. Gethrtime() returns 24579 * a longlong, which may contain resolution down to nanoseconds. 24580 * The current time will either be a 32-bit or a 64-bit quantity. 24581 * XOR the two together in a 64-bit result variable. 24582 * Convert the result to a 32-bit value by multiplying the high-order 24583 * 32-bits by the low-order 32-bits. 24584 */ 24585 24586 hrt = gethrtime(); 24587 (void) drv_getparm(TIME, &wallclock); 24588 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24589 mutex_enter(&tcp_random_lock); 24590 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24591 (result & 0xffffffff); 24592 24593 for (i = 1; i < DEG_3; i++) 24594 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24595 + 12345; 24596 tcp_random_fptr = &tcp_random_state[SEP_3]; 24597 tcp_random_rptr = &tcp_random_state[0]; 24598 mutex_exit(&tcp_random_lock); 24599 for (i = 0; i < 10 * DEG_3; i++) 24600 (void) tcp_random(); 24601 } 24602 24603 /* 24604 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24605 * This range is selected to be approximately centered on TCP_ISS / 2, 24606 * and easy to compute. We get this value by generating a 32-bit random 24607 * number, selecting out the high-order 17 bits, and then adding one so 24608 * that we never return zero. 24609 */ 24610 int 24611 tcp_random(void) 24612 { 24613 int i; 24614 24615 mutex_enter(&tcp_random_lock); 24616 *tcp_random_fptr += *tcp_random_rptr; 24617 24618 /* 24619 * The high-order bits are more random than the low-order bits, 24620 * so we select out the high-order 17 bits and add one so that 24621 * we never return zero. 24622 */ 24623 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24624 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24625 tcp_random_fptr = tcp_random_state; 24626 ++tcp_random_rptr; 24627 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24628 tcp_random_rptr = tcp_random_state; 24629 24630 mutex_exit(&tcp_random_lock); 24631 return (i); 24632 } 24633 24634 /* 24635 * XXX This will go away when TPI is extended to send 24636 * info reqs to sockfs/timod ..... 24637 * Given a queue, set the max packet size for the write 24638 * side of the queue below stream head. This value is 24639 * cached on the stream head. 24640 * Returns 1 on success, 0 otherwise. 24641 */ 24642 static int 24643 setmaxps(queue_t *q, int maxpsz) 24644 { 24645 struct stdata *stp; 24646 queue_t *wq; 24647 stp = STREAM(q); 24648 24649 /* 24650 * At this point change of a queue parameter is not allowed 24651 * when a multiplexor is sitting on top. 24652 */ 24653 if (stp->sd_flag & STPLEX) 24654 return (0); 24655 24656 claimstr(stp->sd_wrq); 24657 wq = stp->sd_wrq->q_next; 24658 ASSERT(wq != NULL); 24659 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24660 releasestr(stp->sd_wrq); 24661 return (1); 24662 } 24663 24664 static int 24665 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24666 int *t_errorp, int *sys_errorp) 24667 { 24668 int error; 24669 int is_absreq_failure; 24670 t_scalar_t *opt_lenp; 24671 t_scalar_t opt_offset; 24672 int prim_type; 24673 struct T_conn_req *tcreqp; 24674 struct T_conn_res *tcresp; 24675 cred_t *cr; 24676 24677 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24678 24679 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24680 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24681 prim_type == T_CONN_RES); 24682 24683 switch (prim_type) { 24684 case T_CONN_REQ: 24685 tcreqp = (struct T_conn_req *)mp->b_rptr; 24686 opt_offset = tcreqp->OPT_offset; 24687 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24688 break; 24689 case O_T_CONN_RES: 24690 case T_CONN_RES: 24691 tcresp = (struct T_conn_res *)mp->b_rptr; 24692 opt_offset = tcresp->OPT_offset; 24693 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24694 break; 24695 } 24696 24697 *t_errorp = 0; 24698 *sys_errorp = 0; 24699 *do_disconnectp = 0; 24700 24701 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24702 opt_offset, cr, &tcp_opt_obj, 24703 NULL, &is_absreq_failure); 24704 24705 switch (error) { 24706 case 0: /* no error */ 24707 ASSERT(is_absreq_failure == 0); 24708 return (0); 24709 case ENOPROTOOPT: 24710 *t_errorp = TBADOPT; 24711 break; 24712 case EACCES: 24713 *t_errorp = TACCES; 24714 break; 24715 default: 24716 *t_errorp = TSYSERR; *sys_errorp = error; 24717 break; 24718 } 24719 if (is_absreq_failure != 0) { 24720 /* 24721 * The connection request should get the local ack 24722 * T_OK_ACK and then a T_DISCON_IND. 24723 */ 24724 *do_disconnectp = 1; 24725 } 24726 return (-1); 24727 } 24728 24729 /* 24730 * Split this function out so that if the secret changes, I'm okay. 24731 * 24732 * Initialize the tcp_iss_cookie and tcp_iss_key. 24733 */ 24734 24735 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24736 24737 static void 24738 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24739 { 24740 struct { 24741 int32_t current_time; 24742 uint32_t randnum; 24743 uint16_t pad; 24744 uint8_t ether[6]; 24745 uint8_t passwd[PASSWD_SIZE]; 24746 } tcp_iss_cookie; 24747 time_t t; 24748 24749 /* 24750 * Start with the current absolute time. 24751 */ 24752 (void) drv_getparm(TIME, &t); 24753 tcp_iss_cookie.current_time = t; 24754 24755 /* 24756 * XXX - Need a more random number per RFC 1750, not this crap. 24757 * OTOH, if what follows is pretty random, then I'm in better shape. 24758 */ 24759 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24760 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24761 24762 /* 24763 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24764 * as a good template. 24765 */ 24766 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24767 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24768 24769 /* 24770 * The pass-phrase. Normally this is supplied by user-called NDD. 24771 */ 24772 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24773 24774 /* 24775 * See 4010593 if this section becomes a problem again, 24776 * but the local ethernet address is useful here. 24777 */ 24778 (void) localetheraddr(NULL, 24779 (struct ether_addr *)&tcp_iss_cookie.ether); 24780 24781 /* 24782 * Hash 'em all together. The MD5Final is called per-connection. 24783 */ 24784 mutex_enter(&tcps->tcps_iss_key_lock); 24785 MD5Init(&tcps->tcps_iss_key); 24786 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24787 sizeof (tcp_iss_cookie)); 24788 mutex_exit(&tcps->tcps_iss_key_lock); 24789 } 24790 24791 /* 24792 * Set the RFC 1948 pass phrase 24793 */ 24794 /* ARGSUSED */ 24795 static int 24796 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24797 cred_t *cr) 24798 { 24799 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24800 24801 /* 24802 * Basically, value contains a new pass phrase. Pass it along! 24803 */ 24804 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24805 return (0); 24806 } 24807 24808 /* ARGSUSED */ 24809 static int 24810 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24811 { 24812 bzero(buf, sizeof (tcp_sack_info_t)); 24813 return (0); 24814 } 24815 24816 /* ARGSUSED */ 24817 static int 24818 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24819 { 24820 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24821 return (0); 24822 } 24823 24824 /* 24825 * Make sure we wait until the default queue is setup, yet allow 24826 * tcp_g_q_create() to open a TCP stream. 24827 * We need to allow tcp_g_q_create() do do an open 24828 * of tcp, hence we compare curhread. 24829 * All others have to wait until the tcps_g_q has been 24830 * setup. 24831 */ 24832 void 24833 tcp_g_q_setup(tcp_stack_t *tcps) 24834 { 24835 mutex_enter(&tcps->tcps_g_q_lock); 24836 if (tcps->tcps_g_q != NULL) { 24837 mutex_exit(&tcps->tcps_g_q_lock); 24838 return; 24839 } 24840 if (tcps->tcps_g_q_creator == NULL) { 24841 /* This thread will set it up */ 24842 tcps->tcps_g_q_creator = curthread; 24843 mutex_exit(&tcps->tcps_g_q_lock); 24844 tcp_g_q_create(tcps); 24845 mutex_enter(&tcps->tcps_g_q_lock); 24846 ASSERT(tcps->tcps_g_q_creator == curthread); 24847 tcps->tcps_g_q_creator = NULL; 24848 cv_signal(&tcps->tcps_g_q_cv); 24849 ASSERT(tcps->tcps_g_q != NULL); 24850 mutex_exit(&tcps->tcps_g_q_lock); 24851 return; 24852 } 24853 /* Everybody but the creator has to wait */ 24854 if (tcps->tcps_g_q_creator != curthread) { 24855 while (tcps->tcps_g_q == NULL) 24856 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24857 } 24858 mutex_exit(&tcps->tcps_g_q_lock); 24859 } 24860 24861 major_t IP_MAJ; 24862 #define IP "ip" 24863 24864 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24865 24866 /* 24867 * Create a default tcp queue here instead of in strplumb 24868 */ 24869 void 24870 tcp_g_q_create(tcp_stack_t *tcps) 24871 { 24872 int error; 24873 ldi_handle_t lh = NULL; 24874 ldi_ident_t li = NULL; 24875 int rval; 24876 cred_t *cr; 24877 24878 #ifdef NS_DEBUG 24879 (void) printf("tcp_g_q_create()\n"); 24880 #endif 24881 24882 ASSERT(tcps->tcps_g_q_creator == curthread); 24883 24884 error = ldi_ident_from_major(IP_MAJ, &li); 24885 if (error) { 24886 #ifdef DEBUG 24887 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24888 error); 24889 #endif 24890 return; 24891 } 24892 24893 cr = zone_get_kcred(netstackid_to_zoneid( 24894 tcps->tcps_netstack->netstack_stackid)); 24895 ASSERT(cr != NULL); 24896 /* 24897 * We set the tcp default queue to IPv6 because IPv4 falls 24898 * back to IPv6 when it can't find a client, but 24899 * IPv6 does not fall back to IPv4. 24900 */ 24901 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24902 if (error) { 24903 #ifdef DEBUG 24904 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24905 error); 24906 #endif 24907 goto out; 24908 } 24909 24910 /* 24911 * This ioctl causes the tcp framework to cache a pointer to 24912 * this stream, so we don't want to close the stream after 24913 * this operation. 24914 * Use the kernel credentials that are for the zone we're in. 24915 */ 24916 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24917 (intptr_t)0, FKIOCTL, cr, &rval); 24918 if (error) { 24919 #ifdef DEBUG 24920 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24921 "error %d\n", error); 24922 #endif 24923 goto out; 24924 } 24925 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24926 lh = NULL; 24927 out: 24928 /* Close layered handles */ 24929 if (li) 24930 ldi_ident_release(li); 24931 /* Keep cred around until _inactive needs it */ 24932 tcps->tcps_g_q_cr = cr; 24933 } 24934 24935 /* 24936 * We keep tcp_g_q set until all other tcp_t's in the zone 24937 * has gone away, and then when tcp_g_q_inactive() is called 24938 * we clear it. 24939 */ 24940 void 24941 tcp_g_q_destroy(tcp_stack_t *tcps) 24942 { 24943 #ifdef NS_DEBUG 24944 (void) printf("tcp_g_q_destroy()for stack %d\n", 24945 tcps->tcps_netstack->netstack_stackid); 24946 #endif 24947 24948 if (tcps->tcps_g_q == NULL) { 24949 return; /* Nothing to cleanup */ 24950 } 24951 /* 24952 * Drop reference corresponding to the default queue. 24953 * This reference was added from tcp_open when the default queue 24954 * was created, hence we compensate for this extra drop in 24955 * tcp_g_q_close. If the refcnt drops to zero here it means 24956 * the default queue was the last one to be open, in which 24957 * case, then tcp_g_q_inactive will be 24958 * called as a result of the refrele. 24959 */ 24960 TCPS_REFRELE(tcps); 24961 } 24962 24963 /* 24964 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24965 * Run by tcp_q_q_inactive using a taskq. 24966 */ 24967 static void 24968 tcp_g_q_close(void *arg) 24969 { 24970 tcp_stack_t *tcps = arg; 24971 int error; 24972 ldi_handle_t lh = NULL; 24973 ldi_ident_t li = NULL; 24974 cred_t *cr; 24975 24976 #ifdef NS_DEBUG 24977 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24978 tcps->tcps_netstack->netstack_stackid, 24979 tcps->tcps_netstack->netstack_refcnt); 24980 #endif 24981 lh = tcps->tcps_g_q_lh; 24982 if (lh == NULL) 24983 return; /* Nothing to cleanup */ 24984 24985 ASSERT(tcps->tcps_refcnt == 1); 24986 ASSERT(tcps->tcps_g_q != NULL); 24987 24988 error = ldi_ident_from_major(IP_MAJ, &li); 24989 if (error) { 24990 #ifdef DEBUG 24991 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24992 error); 24993 #endif 24994 return; 24995 } 24996 24997 cr = tcps->tcps_g_q_cr; 24998 tcps->tcps_g_q_cr = NULL; 24999 ASSERT(cr != NULL); 25000 25001 /* 25002 * Make sure we can break the recursion when tcp_close decrements 25003 * the reference count causing g_q_inactive to be called again. 25004 */ 25005 tcps->tcps_g_q_lh = NULL; 25006 25007 /* close the default queue */ 25008 (void) ldi_close(lh, FREAD|FWRITE, cr); 25009 /* 25010 * At this point in time tcps and the rest of netstack_t might 25011 * have been deleted. 25012 */ 25013 tcps = NULL; 25014 25015 /* Close layered handles */ 25016 ldi_ident_release(li); 25017 crfree(cr); 25018 } 25019 25020 /* 25021 * Called when last tcp_t drops reference count using TCPS_REFRELE. 25022 * 25023 * Have to ensure that the ldi routines are not used by an 25024 * interrupt thread by using a taskq. 25025 */ 25026 void 25027 tcp_g_q_inactive(tcp_stack_t *tcps) 25028 { 25029 if (tcps->tcps_g_q_lh == NULL) 25030 return; /* Nothing to cleanup */ 25031 25032 ASSERT(tcps->tcps_refcnt == 0); 25033 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 25034 25035 if (servicing_interrupt()) { 25036 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 25037 (void *) tcps, TQ_SLEEP); 25038 } else { 25039 tcp_g_q_close(tcps); 25040 } 25041 } 25042 25043 /* 25044 * Called by IP when IP is loaded into the kernel 25045 */ 25046 void 25047 tcp_ddi_g_init(void) 25048 { 25049 IP_MAJ = ddi_name_to_major(IP); 25050 25051 tcp_timercache = kmem_cache_create("tcp_timercache", 25052 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 25053 NULL, NULL, NULL, NULL, NULL, 0); 25054 25055 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 25056 sizeof (tcp_sack_info_t), 0, 25057 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 25058 25059 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 25060 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 25061 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 25062 25063 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 25064 25065 /* Initialize the random number generator */ 25066 tcp_random_init(); 25067 25068 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 25069 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 25070 25071 /* A single callback independently of how many netstacks we have */ 25072 ip_squeue_init(tcp_squeue_add); 25073 25074 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 25075 25076 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 25077 TASKQ_PREPOPULATE); 25078 25079 /* 25080 * We want to be informed each time a stack is created or 25081 * destroyed in the kernel, so we can maintain the 25082 * set of tcp_stack_t's. 25083 */ 25084 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 25085 tcp_stack_fini); 25086 } 25087 25088 25089 /* 25090 * Initialize the TCP stack instance. 25091 */ 25092 static void * 25093 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 25094 { 25095 tcp_stack_t *tcps; 25096 tcpparam_t *pa; 25097 int i; 25098 25099 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 25100 tcps->tcps_netstack = ns; 25101 25102 /* Initialize locks */ 25103 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 25104 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 25105 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 25106 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 25107 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 25108 rw_init(&tcps->tcps_reserved_port_lock, NULL, RW_DEFAULT, NULL); 25109 25110 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 25111 tcps->tcps_g_epriv_ports[0] = 2049; 25112 tcps->tcps_g_epriv_ports[1] = 4045; 25113 tcps->tcps_min_anonpriv_port = 512; 25114 25115 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 25116 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 25117 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 25118 TCP_FANOUT_SIZE, KM_SLEEP); 25119 tcps->tcps_reserved_port = kmem_zalloc(sizeof (tcp_rport_t) * 25120 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE, KM_SLEEP); 25121 25122 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25123 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 25124 MUTEX_DEFAULT, NULL); 25125 } 25126 25127 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25128 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 25129 MUTEX_DEFAULT, NULL); 25130 } 25131 25132 /* TCP's IPsec code calls the packet dropper. */ 25133 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 25134 25135 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 25136 tcps->tcps_params = pa; 25137 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25138 25139 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 25140 A_CNT(lcl_tcp_param_arr), tcps); 25141 25142 /* 25143 * Note: To really walk the device tree you need the devinfo 25144 * pointer to your device which is only available after probe/attach. 25145 * The following is safe only because it uses ddi_root_node() 25146 */ 25147 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 25148 tcp_opt_obj.odb_opt_arr_cnt); 25149 25150 /* 25151 * Initialize RFC 1948 secret values. This will probably be reset once 25152 * by the boot scripts. 25153 * 25154 * Use NULL name, as the name is caught by the new lockstats. 25155 * 25156 * Initialize with some random, non-guessable string, like the global 25157 * T_INFO_ACK. 25158 */ 25159 25160 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 25161 sizeof (tcp_g_t_info_ack), tcps); 25162 25163 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 25164 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 25165 25166 return (tcps); 25167 } 25168 25169 /* 25170 * Called when the IP module is about to be unloaded. 25171 */ 25172 void 25173 tcp_ddi_g_destroy(void) 25174 { 25175 tcp_g_kstat_fini(tcp_g_kstat); 25176 tcp_g_kstat = NULL; 25177 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 25178 25179 mutex_destroy(&tcp_random_lock); 25180 25181 kmem_cache_destroy(tcp_timercache); 25182 kmem_cache_destroy(tcp_sack_info_cache); 25183 kmem_cache_destroy(tcp_iphc_cache); 25184 25185 netstack_unregister(NS_TCP); 25186 taskq_destroy(tcp_taskq); 25187 } 25188 25189 /* 25190 * Shut down the TCP stack instance. 25191 */ 25192 /* ARGSUSED */ 25193 static void 25194 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25195 { 25196 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25197 25198 tcp_g_q_destroy(tcps); 25199 } 25200 25201 /* 25202 * Free the TCP stack instance. 25203 */ 25204 static void 25205 tcp_stack_fini(netstackid_t stackid, void *arg) 25206 { 25207 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25208 int i; 25209 25210 nd_free(&tcps->tcps_g_nd); 25211 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25212 tcps->tcps_params = NULL; 25213 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25214 tcps->tcps_wroff_xtra_param = NULL; 25215 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25216 tcps->tcps_mdt_head_param = NULL; 25217 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25218 tcps->tcps_mdt_tail_param = NULL; 25219 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25220 tcps->tcps_mdt_max_pbufs_param = NULL; 25221 25222 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25223 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25224 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25225 } 25226 25227 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25228 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25229 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25230 } 25231 25232 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25233 tcps->tcps_bind_fanout = NULL; 25234 25235 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25236 tcps->tcps_acceptor_fanout = NULL; 25237 25238 kmem_free(tcps->tcps_reserved_port, sizeof (tcp_rport_t) * 25239 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE); 25240 tcps->tcps_reserved_port = NULL; 25241 25242 mutex_destroy(&tcps->tcps_iss_key_lock); 25243 rw_destroy(&tcps->tcps_hsp_lock); 25244 mutex_destroy(&tcps->tcps_g_q_lock); 25245 cv_destroy(&tcps->tcps_g_q_cv); 25246 mutex_destroy(&tcps->tcps_epriv_port_lock); 25247 rw_destroy(&tcps->tcps_reserved_port_lock); 25248 25249 ip_drop_unregister(&tcps->tcps_dropper); 25250 25251 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25252 tcps->tcps_kstat = NULL; 25253 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25254 25255 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25256 tcps->tcps_mibkp = NULL; 25257 25258 kmem_free(tcps, sizeof (*tcps)); 25259 } 25260 25261 /* 25262 * Generate ISS, taking into account NDD changes may happen halfway through. 25263 * (If the iss is not zero, set it.) 25264 */ 25265 25266 static void 25267 tcp_iss_init(tcp_t *tcp) 25268 { 25269 MD5_CTX context; 25270 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25271 uint32_t answer[4]; 25272 tcp_stack_t *tcps = tcp->tcp_tcps; 25273 25274 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25275 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25276 switch (tcps->tcps_strong_iss) { 25277 case 2: 25278 mutex_enter(&tcps->tcps_iss_key_lock); 25279 context = tcps->tcps_iss_key; 25280 mutex_exit(&tcps->tcps_iss_key_lock); 25281 arg.ports = tcp->tcp_ports; 25282 if (tcp->tcp_ipversion == IPV4_VERSION) { 25283 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25284 &arg.src); 25285 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25286 &arg.dst); 25287 } else { 25288 arg.src = tcp->tcp_ip6h->ip6_src; 25289 arg.dst = tcp->tcp_ip6h->ip6_dst; 25290 } 25291 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25292 MD5Final((uchar_t *)answer, &context); 25293 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25294 /* 25295 * Now that we've hashed into a unique per-connection sequence 25296 * space, add a random increment per strong_iss == 1. So I 25297 * guess we'll have to... 25298 */ 25299 /* FALLTHRU */ 25300 case 1: 25301 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25302 break; 25303 default: 25304 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25305 break; 25306 } 25307 tcp->tcp_valid_bits = TCP_ISS_VALID; 25308 tcp->tcp_fss = tcp->tcp_iss - 1; 25309 tcp->tcp_suna = tcp->tcp_iss; 25310 tcp->tcp_snxt = tcp->tcp_iss + 1; 25311 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25312 tcp->tcp_csuna = tcp->tcp_snxt; 25313 } 25314 25315 /* 25316 * Exported routine for extracting active tcp connection status. 25317 * 25318 * This is used by the Solaris Cluster Networking software to 25319 * gather a list of connections that need to be forwarded to 25320 * specific nodes in the cluster when configuration changes occur. 25321 * 25322 * The callback is invoked for each tcp_t structure. Returning 25323 * non-zero from the callback routine terminates the search. 25324 */ 25325 int 25326 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25327 void *arg) 25328 { 25329 netstack_handle_t nh; 25330 netstack_t *ns; 25331 int ret = 0; 25332 25333 netstack_next_init(&nh); 25334 while ((ns = netstack_next(&nh)) != NULL) { 25335 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25336 ns->netstack_tcp); 25337 netstack_rele(ns); 25338 } 25339 netstack_next_fini(&nh); 25340 return (ret); 25341 } 25342 25343 static int 25344 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25345 tcp_stack_t *tcps) 25346 { 25347 tcp_t *tcp; 25348 cl_tcp_info_t cl_tcpi; 25349 connf_t *connfp; 25350 conn_t *connp; 25351 int i; 25352 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25353 25354 ASSERT(callback != NULL); 25355 25356 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25357 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25358 connp = NULL; 25359 25360 while ((connp = 25361 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25362 25363 tcp = connp->conn_tcp; 25364 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25365 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25366 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25367 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25368 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25369 /* 25370 * The macros tcp_laddr and tcp_faddr give the IPv4 25371 * addresses. They are copied implicitly below as 25372 * mapped addresses. 25373 */ 25374 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25375 if (tcp->tcp_ipversion == IPV4_VERSION) { 25376 cl_tcpi.cl_tcpi_faddr = 25377 tcp->tcp_ipha->ipha_dst; 25378 } else { 25379 cl_tcpi.cl_tcpi_faddr_v6 = 25380 tcp->tcp_ip6h->ip6_dst; 25381 } 25382 25383 /* 25384 * If the callback returns non-zero 25385 * we terminate the traversal. 25386 */ 25387 if ((*callback)(&cl_tcpi, arg) != 0) { 25388 CONN_DEC_REF(tcp->tcp_connp); 25389 return (1); 25390 } 25391 } 25392 } 25393 25394 return (0); 25395 } 25396 25397 /* 25398 * Macros used for accessing the different types of sockaddr 25399 * structures inside a tcp_ioc_abort_conn_t. 25400 */ 25401 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25402 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25403 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25404 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25405 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25406 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25407 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25408 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25409 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25410 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25411 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25412 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25413 25414 /* 25415 * Return the correct error code to mimic the behavior 25416 * of a connection reset. 25417 */ 25418 #define TCP_AC_GET_ERRCODE(state, err) { \ 25419 switch ((state)) { \ 25420 case TCPS_SYN_SENT: \ 25421 case TCPS_SYN_RCVD: \ 25422 (err) = ECONNREFUSED; \ 25423 break; \ 25424 case TCPS_ESTABLISHED: \ 25425 case TCPS_FIN_WAIT_1: \ 25426 case TCPS_FIN_WAIT_2: \ 25427 case TCPS_CLOSE_WAIT: \ 25428 (err) = ECONNRESET; \ 25429 break; \ 25430 case TCPS_CLOSING: \ 25431 case TCPS_LAST_ACK: \ 25432 case TCPS_TIME_WAIT: \ 25433 (err) = 0; \ 25434 break; \ 25435 default: \ 25436 (err) = ENXIO; \ 25437 } \ 25438 } 25439 25440 /* 25441 * Check if a tcp structure matches the info in acp. 25442 */ 25443 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25444 (((acp)->ac_local.ss_family == AF_INET) ? \ 25445 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25446 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25447 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25448 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25449 (TCP_AC_V4LPORT((acp)) == 0 || \ 25450 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25451 (TCP_AC_V4RPORT((acp)) == 0 || \ 25452 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25453 (acp)->ac_start <= (tcp)->tcp_state && \ 25454 (acp)->ac_end >= (tcp)->tcp_state) : \ 25455 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25456 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25457 &(tcp)->tcp_ip_src_v6)) && \ 25458 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25459 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25460 &(tcp)->tcp_remote_v6)) && \ 25461 (TCP_AC_V6LPORT((acp)) == 0 || \ 25462 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25463 (TCP_AC_V6RPORT((acp)) == 0 || \ 25464 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25465 (acp)->ac_start <= (tcp)->tcp_state && \ 25466 (acp)->ac_end >= (tcp)->tcp_state)) 25467 25468 #define TCP_AC_MATCH(acp, tcp) \ 25469 (((acp)->ac_zoneid == ALL_ZONES || \ 25470 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25471 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25472 25473 /* 25474 * Build a message containing a tcp_ioc_abort_conn_t structure 25475 * which is filled in with information from acp and tp. 25476 */ 25477 static mblk_t * 25478 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25479 { 25480 mblk_t *mp; 25481 tcp_ioc_abort_conn_t *tacp; 25482 25483 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25484 if (mp == NULL) 25485 return (NULL); 25486 25487 mp->b_datap->db_type = M_CTL; 25488 25489 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25490 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25491 sizeof (uint32_t)); 25492 25493 tacp->ac_start = acp->ac_start; 25494 tacp->ac_end = acp->ac_end; 25495 tacp->ac_zoneid = acp->ac_zoneid; 25496 25497 if (acp->ac_local.ss_family == AF_INET) { 25498 tacp->ac_local.ss_family = AF_INET; 25499 tacp->ac_remote.ss_family = AF_INET; 25500 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25501 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25502 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25503 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25504 } else { 25505 tacp->ac_local.ss_family = AF_INET6; 25506 tacp->ac_remote.ss_family = AF_INET6; 25507 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25508 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25509 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25510 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25511 } 25512 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25513 return (mp); 25514 } 25515 25516 /* 25517 * Print a tcp_ioc_abort_conn_t structure. 25518 */ 25519 static void 25520 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25521 { 25522 char lbuf[128]; 25523 char rbuf[128]; 25524 sa_family_t af; 25525 in_port_t lport, rport; 25526 ushort_t logflags; 25527 25528 af = acp->ac_local.ss_family; 25529 25530 if (af == AF_INET) { 25531 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25532 lbuf, 128); 25533 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25534 rbuf, 128); 25535 lport = ntohs(TCP_AC_V4LPORT(acp)); 25536 rport = ntohs(TCP_AC_V4RPORT(acp)); 25537 } else { 25538 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25539 lbuf, 128); 25540 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25541 rbuf, 128); 25542 lport = ntohs(TCP_AC_V6LPORT(acp)); 25543 rport = ntohs(TCP_AC_V6RPORT(acp)); 25544 } 25545 25546 logflags = SL_TRACE | SL_NOTE; 25547 /* 25548 * Don't print this message to the console if the operation was done 25549 * to a non-global zone. 25550 */ 25551 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25552 logflags |= SL_CONSOLE; 25553 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25554 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25555 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25556 acp->ac_start, acp->ac_end); 25557 } 25558 25559 /* 25560 * Called inside tcp_rput when a message built using 25561 * tcp_ioctl_abort_build_msg is put into a queue. 25562 * Note that when we get here there is no wildcard in acp any more. 25563 */ 25564 static void 25565 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25566 { 25567 tcp_ioc_abort_conn_t *acp; 25568 25569 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25570 if (tcp->tcp_state <= acp->ac_end) { 25571 /* 25572 * If we get here, we are already on the correct 25573 * squeue. This ioctl follows the following path 25574 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25575 * ->tcp_ioctl_abort->squeue_fill (if on a 25576 * different squeue) 25577 */ 25578 int errcode; 25579 25580 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25581 (void) tcp_clean_death(tcp, errcode, 26); 25582 } 25583 freemsg(mp); 25584 } 25585 25586 /* 25587 * Abort all matching connections on a hash chain. 25588 */ 25589 static int 25590 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25591 boolean_t exact, tcp_stack_t *tcps) 25592 { 25593 int nmatch, err = 0; 25594 tcp_t *tcp; 25595 MBLKP mp, last, listhead = NULL; 25596 conn_t *tconnp; 25597 connf_t *connfp; 25598 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25599 25600 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25601 25602 startover: 25603 nmatch = 0; 25604 25605 mutex_enter(&connfp->connf_lock); 25606 for (tconnp = connfp->connf_head; tconnp != NULL; 25607 tconnp = tconnp->conn_next) { 25608 tcp = tconnp->conn_tcp; 25609 if (TCP_AC_MATCH(acp, tcp)) { 25610 CONN_INC_REF(tcp->tcp_connp); 25611 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25612 if (mp == NULL) { 25613 err = ENOMEM; 25614 CONN_DEC_REF(tcp->tcp_connp); 25615 break; 25616 } 25617 mp->b_prev = (mblk_t *)tcp; 25618 25619 if (listhead == NULL) { 25620 listhead = mp; 25621 last = mp; 25622 } else { 25623 last->b_next = mp; 25624 last = mp; 25625 } 25626 nmatch++; 25627 if (exact) 25628 break; 25629 } 25630 25631 /* Avoid holding lock for too long. */ 25632 if (nmatch >= 500) 25633 break; 25634 } 25635 mutex_exit(&connfp->connf_lock); 25636 25637 /* Pass mp into the correct tcp */ 25638 while ((mp = listhead) != NULL) { 25639 listhead = listhead->b_next; 25640 tcp = (tcp_t *)mp->b_prev; 25641 mp->b_next = mp->b_prev = NULL; 25642 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 25643 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 25644 } 25645 25646 *count += nmatch; 25647 if (nmatch >= 500 && err == 0) 25648 goto startover; 25649 return (err); 25650 } 25651 25652 /* 25653 * Abort all connections that matches the attributes specified in acp. 25654 */ 25655 static int 25656 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25657 { 25658 sa_family_t af; 25659 uint32_t ports; 25660 uint16_t *pports; 25661 int err = 0, count = 0; 25662 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25663 int index = -1; 25664 ushort_t logflags; 25665 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25666 25667 af = acp->ac_local.ss_family; 25668 25669 if (af == AF_INET) { 25670 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25671 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25672 pports = (uint16_t *)&ports; 25673 pports[1] = TCP_AC_V4LPORT(acp); 25674 pports[0] = TCP_AC_V4RPORT(acp); 25675 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25676 } 25677 } else { 25678 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25679 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25680 pports = (uint16_t *)&ports; 25681 pports[1] = TCP_AC_V6LPORT(acp); 25682 pports[0] = TCP_AC_V6RPORT(acp); 25683 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25684 } 25685 } 25686 25687 /* 25688 * For cases where remote addr, local port, and remote port are non- 25689 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25690 */ 25691 if (index != -1) { 25692 err = tcp_ioctl_abort_bucket(acp, index, 25693 &count, exact, tcps); 25694 } else { 25695 /* 25696 * loop through all entries for wildcard case 25697 */ 25698 for (index = 0; 25699 index < ipst->ips_ipcl_conn_fanout_size; 25700 index++) { 25701 err = tcp_ioctl_abort_bucket(acp, index, 25702 &count, exact, tcps); 25703 if (err != 0) 25704 break; 25705 } 25706 } 25707 25708 logflags = SL_TRACE | SL_NOTE; 25709 /* 25710 * Don't print this message to the console if the operation was done 25711 * to a non-global zone. 25712 */ 25713 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25714 logflags |= SL_CONSOLE; 25715 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25716 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25717 if (err == 0 && count == 0) 25718 err = ENOENT; 25719 return (err); 25720 } 25721 25722 /* 25723 * Process the TCP_IOC_ABORT_CONN ioctl request. 25724 */ 25725 static void 25726 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25727 { 25728 int err; 25729 IOCP iocp; 25730 MBLKP mp1; 25731 sa_family_t laf, raf; 25732 tcp_ioc_abort_conn_t *acp; 25733 zone_t *zptr; 25734 conn_t *connp = Q_TO_CONN(q); 25735 zoneid_t zoneid = connp->conn_zoneid; 25736 tcp_t *tcp = connp->conn_tcp; 25737 tcp_stack_t *tcps = tcp->tcp_tcps; 25738 25739 iocp = (IOCP)mp->b_rptr; 25740 25741 if ((mp1 = mp->b_cont) == NULL || 25742 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25743 err = EINVAL; 25744 goto out; 25745 } 25746 25747 /* check permissions */ 25748 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25749 err = EPERM; 25750 goto out; 25751 } 25752 25753 if (mp1->b_cont != NULL) { 25754 freemsg(mp1->b_cont); 25755 mp1->b_cont = NULL; 25756 } 25757 25758 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25759 laf = acp->ac_local.ss_family; 25760 raf = acp->ac_remote.ss_family; 25761 25762 /* check that a zone with the supplied zoneid exists */ 25763 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25764 zptr = zone_find_by_id(zoneid); 25765 if (zptr != NULL) { 25766 zone_rele(zptr); 25767 } else { 25768 err = EINVAL; 25769 goto out; 25770 } 25771 } 25772 25773 /* 25774 * For exclusive stacks we set the zoneid to zero 25775 * to make TCP operate as if in the global zone. 25776 */ 25777 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25778 acp->ac_zoneid = GLOBAL_ZONEID; 25779 25780 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25781 acp->ac_start > acp->ac_end || laf != raf || 25782 (laf != AF_INET && laf != AF_INET6)) { 25783 err = EINVAL; 25784 goto out; 25785 } 25786 25787 tcp_ioctl_abort_dump(acp); 25788 err = tcp_ioctl_abort(acp, tcps); 25789 25790 out: 25791 if (mp1 != NULL) { 25792 freemsg(mp1); 25793 mp->b_cont = NULL; 25794 } 25795 25796 if (err != 0) 25797 miocnak(q, mp, 0, err); 25798 else 25799 miocack(q, mp, 0, 0); 25800 } 25801 25802 /* 25803 * tcp_time_wait_processing() handles processing of incoming packets when 25804 * the tcp is in the TIME_WAIT state. 25805 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25806 * on the time wait list. 25807 */ 25808 void 25809 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25810 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25811 { 25812 int32_t bytes_acked; 25813 int32_t gap; 25814 int32_t rgap; 25815 tcp_opt_t tcpopt; 25816 uint_t flags; 25817 uint32_t new_swnd = 0; 25818 conn_t *connp; 25819 tcp_stack_t *tcps = tcp->tcp_tcps; 25820 25821 BUMP_LOCAL(tcp->tcp_ibsegs); 25822 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 25823 25824 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25825 new_swnd = BE16_TO_U16(tcph->th_win) << 25826 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25827 if (tcp->tcp_snd_ts_ok) { 25828 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25829 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25830 tcp->tcp_rnxt, TH_ACK); 25831 goto done; 25832 } 25833 } 25834 gap = seg_seq - tcp->tcp_rnxt; 25835 rgap = tcp->tcp_rwnd - (gap + seg_len); 25836 if (gap < 0) { 25837 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25838 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25839 (seg_len > -gap ? -gap : seg_len)); 25840 seg_len += gap; 25841 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25842 if (flags & TH_RST) { 25843 goto done; 25844 } 25845 if ((flags & TH_FIN) && seg_len == -1) { 25846 /* 25847 * When TCP receives a duplicate FIN in 25848 * TIME_WAIT state, restart the 2 MSL timer. 25849 * See page 73 in RFC 793. Make sure this TCP 25850 * is already on the TIME_WAIT list. If not, 25851 * just restart the timer. 25852 */ 25853 if (TCP_IS_DETACHED(tcp)) { 25854 if (tcp_time_wait_remove(tcp, NULL) == 25855 B_TRUE) { 25856 tcp_time_wait_append(tcp); 25857 TCP_DBGSTAT(tcps, 25858 tcp_rput_time_wait); 25859 } 25860 } else { 25861 ASSERT(tcp != NULL); 25862 TCP_TIMER_RESTART(tcp, 25863 tcps->tcps_time_wait_interval); 25864 } 25865 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25866 tcp->tcp_rnxt, TH_ACK); 25867 goto done; 25868 } 25869 flags |= TH_ACK_NEEDED; 25870 seg_len = 0; 25871 goto process_ack; 25872 } 25873 25874 /* Fix seg_seq, and chew the gap off the front. */ 25875 seg_seq = tcp->tcp_rnxt; 25876 } 25877 25878 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25879 /* 25880 * Make sure that when we accept the connection, pick 25881 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25882 * old connection. 25883 * 25884 * The next ISS generated is equal to tcp_iss_incr_extra 25885 * + ISS_INCR/2 + other components depending on the 25886 * value of tcp_strong_iss. We pre-calculate the new 25887 * ISS here and compare with tcp_snxt to determine if 25888 * we need to make adjustment to tcp_iss_incr_extra. 25889 * 25890 * The above calculation is ugly and is a 25891 * waste of CPU cycles... 25892 */ 25893 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25894 int32_t adj; 25895 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25896 25897 switch (tcps->tcps_strong_iss) { 25898 case 2: { 25899 /* Add time and MD5 components. */ 25900 uint32_t answer[4]; 25901 struct { 25902 uint32_t ports; 25903 in6_addr_t src; 25904 in6_addr_t dst; 25905 } arg; 25906 MD5_CTX context; 25907 25908 mutex_enter(&tcps->tcps_iss_key_lock); 25909 context = tcps->tcps_iss_key; 25910 mutex_exit(&tcps->tcps_iss_key_lock); 25911 arg.ports = tcp->tcp_ports; 25912 /* We use MAPPED addresses in tcp_iss_init */ 25913 arg.src = tcp->tcp_ip_src_v6; 25914 if (tcp->tcp_ipversion == IPV4_VERSION) { 25915 IN6_IPADDR_TO_V4MAPPED( 25916 tcp->tcp_ipha->ipha_dst, 25917 &arg.dst); 25918 } else { 25919 arg.dst = 25920 tcp->tcp_ip6h->ip6_dst; 25921 } 25922 MD5Update(&context, (uchar_t *)&arg, 25923 sizeof (arg)); 25924 MD5Final((uchar_t *)answer, &context); 25925 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25926 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25927 break; 25928 } 25929 case 1: 25930 /* Add time component and min random (i.e. 1). */ 25931 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25932 break; 25933 default: 25934 /* Add only time component. */ 25935 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25936 break; 25937 } 25938 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25939 /* 25940 * New ISS not guaranteed to be ISS_INCR/2 25941 * ahead of the current tcp_snxt, so add the 25942 * difference to tcp_iss_incr_extra. 25943 */ 25944 tcps->tcps_iss_incr_extra += adj; 25945 } 25946 /* 25947 * If tcp_clean_death() can not perform the task now, 25948 * drop the SYN packet and let the other side re-xmit. 25949 * Otherwise pass the SYN packet back in, since the 25950 * old tcp state has been cleaned up or freed. 25951 */ 25952 if (tcp_clean_death(tcp, 0, 27) == -1) 25953 goto done; 25954 /* 25955 * We will come back to tcp_rput_data 25956 * on the global queue. Packets destined 25957 * for the global queue will be checked 25958 * with global policy. But the policy for 25959 * this packet has already been checked as 25960 * this was destined for the detached 25961 * connection. We need to bypass policy 25962 * check this time by attaching a dummy 25963 * ipsec_in with ipsec_in_dont_check set. 25964 */ 25965 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25966 if (connp != NULL) { 25967 TCP_STAT(tcps, tcp_time_wait_syn_success); 25968 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25969 return; 25970 } 25971 goto done; 25972 } 25973 25974 /* 25975 * rgap is the amount of stuff received out of window. A negative 25976 * value is the amount out of window. 25977 */ 25978 if (rgap < 0) { 25979 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25980 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25981 /* Fix seg_len and make sure there is something left. */ 25982 seg_len += rgap; 25983 if (seg_len <= 0) { 25984 if (flags & TH_RST) { 25985 goto done; 25986 } 25987 flags |= TH_ACK_NEEDED; 25988 seg_len = 0; 25989 goto process_ack; 25990 } 25991 } 25992 /* 25993 * Check whether we can update tcp_ts_recent. This test is 25994 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25995 * Extensions for High Performance: An Update", Internet Draft. 25996 */ 25997 if (tcp->tcp_snd_ts_ok && 25998 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25999 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 26000 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 26001 tcp->tcp_last_rcv_lbolt = lbolt64; 26002 } 26003 26004 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 26005 /* Always ack out of order packets */ 26006 flags |= TH_ACK_NEEDED; 26007 seg_len = 0; 26008 } else if (seg_len > 0) { 26009 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 26010 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 26011 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 26012 } 26013 if (flags & TH_RST) { 26014 (void) tcp_clean_death(tcp, 0, 28); 26015 goto done; 26016 } 26017 if (flags & TH_SYN) { 26018 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 26019 TH_RST|TH_ACK); 26020 /* 26021 * Do not delete the TCP structure if it is in 26022 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 26023 */ 26024 goto done; 26025 } 26026 process_ack: 26027 if (flags & TH_ACK) { 26028 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 26029 if (bytes_acked <= 0) { 26030 if (bytes_acked == 0 && seg_len == 0 && 26031 new_swnd == tcp->tcp_swnd) 26032 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 26033 } else { 26034 /* Acks something not sent */ 26035 flags |= TH_ACK_NEEDED; 26036 } 26037 } 26038 if (flags & TH_ACK_NEEDED) { 26039 /* 26040 * Time to send an ack for some reason. 26041 */ 26042 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 26043 tcp->tcp_rnxt, TH_ACK); 26044 } 26045 done: 26046 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26047 DB_CKSUMSTART(mp) = 0; 26048 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 26049 TCP_STAT(tcps, tcp_time_wait_syn_fail); 26050 } 26051 freemsg(mp); 26052 } 26053 26054 /* 26055 * Allocate a T_SVR4_OPTMGMT_REQ. 26056 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 26057 * that tcp_rput_other can drop the acks. 26058 */ 26059 static mblk_t * 26060 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 26061 { 26062 mblk_t *mp; 26063 struct T_optmgmt_req *tor; 26064 struct opthdr *oh; 26065 uint_t size; 26066 char *optptr; 26067 26068 size = sizeof (*tor) + sizeof (*oh) + optlen; 26069 mp = allocb(size, BPRI_MED); 26070 if (mp == NULL) 26071 return (NULL); 26072 26073 mp->b_wptr += size; 26074 mp->b_datap->db_type = M_PROTO; 26075 tor = (struct T_optmgmt_req *)mp->b_rptr; 26076 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 26077 tor->MGMT_flags = T_NEGOTIATE; 26078 tor->OPT_length = sizeof (*oh) + optlen; 26079 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 26080 26081 oh = (struct opthdr *)&tor[1]; 26082 oh->level = level; 26083 oh->name = cmd; 26084 oh->len = optlen; 26085 if (optlen != 0) { 26086 optptr = (char *)&oh[1]; 26087 bcopy(opt, optptr, optlen); 26088 } 26089 return (mp); 26090 } 26091 26092 /* 26093 * TCP Timers Implementation. 26094 */ 26095 timeout_id_t 26096 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 26097 { 26098 mblk_t *mp; 26099 tcp_timer_t *tcpt; 26100 tcp_t *tcp = connp->conn_tcp; 26101 tcp_stack_t *tcps = tcp->tcp_tcps; 26102 26103 ASSERT(connp->conn_sqp != NULL); 26104 26105 TCP_DBGSTAT(tcps, tcp_timeout_calls); 26106 26107 if (tcp->tcp_timercache == NULL) { 26108 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 26109 } else { 26110 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 26111 mp = tcp->tcp_timercache; 26112 tcp->tcp_timercache = mp->b_next; 26113 mp->b_next = NULL; 26114 ASSERT(mp->b_wptr == NULL); 26115 } 26116 26117 CONN_INC_REF(connp); 26118 tcpt = (tcp_timer_t *)mp->b_rptr; 26119 tcpt->connp = connp; 26120 tcpt->tcpt_proc = f; 26121 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 26122 return ((timeout_id_t)mp); 26123 } 26124 26125 static void 26126 tcp_timer_callback(void *arg) 26127 { 26128 mblk_t *mp = (mblk_t *)arg; 26129 tcp_timer_t *tcpt; 26130 conn_t *connp; 26131 26132 tcpt = (tcp_timer_t *)mp->b_rptr; 26133 connp = tcpt->connp; 26134 squeue_fill(connp->conn_sqp, mp, 26135 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 26136 } 26137 26138 static void 26139 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 26140 { 26141 tcp_timer_t *tcpt; 26142 conn_t *connp = (conn_t *)arg; 26143 tcp_t *tcp = connp->conn_tcp; 26144 26145 tcpt = (tcp_timer_t *)mp->b_rptr; 26146 ASSERT(connp == tcpt->connp); 26147 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 26148 26149 /* 26150 * If the TCP has reached the closed state, don't proceed any 26151 * further. This TCP logically does not exist on the system. 26152 * tcpt_proc could for example access queues, that have already 26153 * been qprocoff'ed off. Also see comments at the start of tcp_input 26154 */ 26155 if (tcp->tcp_state != TCPS_CLOSED) { 26156 (*tcpt->tcpt_proc)(connp); 26157 } else { 26158 tcp->tcp_timer_tid = 0; 26159 } 26160 tcp_timer_free(connp->conn_tcp, mp); 26161 } 26162 26163 /* 26164 * There is potential race with untimeout and the handler firing at the same 26165 * time. The mblock may be freed by the handler while we are trying to use 26166 * it. But since both should execute on the same squeue, this race should not 26167 * occur. 26168 */ 26169 clock_t 26170 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 26171 { 26172 mblk_t *mp = (mblk_t *)id; 26173 tcp_timer_t *tcpt; 26174 clock_t delta; 26175 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26176 26177 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 26178 26179 if (mp == NULL) 26180 return (-1); 26181 26182 tcpt = (tcp_timer_t *)mp->b_rptr; 26183 ASSERT(tcpt->connp == connp); 26184 26185 delta = untimeout(tcpt->tcpt_tid); 26186 26187 if (delta >= 0) { 26188 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 26189 tcp_timer_free(connp->conn_tcp, mp); 26190 CONN_DEC_REF(connp); 26191 } 26192 26193 return (delta); 26194 } 26195 26196 /* 26197 * Allocate space for the timer event. The allocation looks like mblk, but it is 26198 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26199 * 26200 * Dealing with failures: If we can't allocate from the timer cache we try 26201 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26202 * points to b_rptr. 26203 * If we can't allocate anything using allocb_tryhard(), we perform a last 26204 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26205 * save the actual allocation size in b_datap. 26206 */ 26207 mblk_t * 26208 tcp_timermp_alloc(int kmflags) 26209 { 26210 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26211 kmflags & ~KM_PANIC); 26212 26213 if (mp != NULL) { 26214 mp->b_next = mp->b_prev = NULL; 26215 mp->b_rptr = (uchar_t *)(&mp[1]); 26216 mp->b_wptr = NULL; 26217 mp->b_datap = NULL; 26218 mp->b_queue = NULL; 26219 mp->b_cont = NULL; 26220 } else if (kmflags & KM_PANIC) { 26221 /* 26222 * Failed to allocate memory for the timer. Try allocating from 26223 * dblock caches. 26224 */ 26225 /* ipclassifier calls this from a constructor - hence no tcps */ 26226 TCP_G_STAT(tcp_timermp_allocfail); 26227 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26228 if (mp == NULL) { 26229 size_t size = 0; 26230 /* 26231 * Memory is really low. Try tryhard allocation. 26232 * 26233 * ipclassifier calls this from a constructor - 26234 * hence no tcps 26235 */ 26236 TCP_G_STAT(tcp_timermp_allocdblfail); 26237 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26238 sizeof (tcp_timer_t), &size, kmflags); 26239 mp->b_rptr = (uchar_t *)(&mp[1]); 26240 mp->b_next = mp->b_prev = NULL; 26241 mp->b_wptr = (uchar_t *)-1; 26242 mp->b_datap = (dblk_t *)size; 26243 mp->b_queue = NULL; 26244 mp->b_cont = NULL; 26245 } 26246 ASSERT(mp->b_wptr != NULL); 26247 } 26248 /* ipclassifier calls this from a constructor - hence no tcps */ 26249 TCP_G_DBGSTAT(tcp_timermp_alloced); 26250 26251 return (mp); 26252 } 26253 26254 /* 26255 * Free per-tcp timer cache. 26256 * It can only contain entries from tcp_timercache. 26257 */ 26258 void 26259 tcp_timermp_free(tcp_t *tcp) 26260 { 26261 mblk_t *mp; 26262 26263 while ((mp = tcp->tcp_timercache) != NULL) { 26264 ASSERT(mp->b_wptr == NULL); 26265 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26266 kmem_cache_free(tcp_timercache, mp); 26267 } 26268 } 26269 26270 /* 26271 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26272 * events there already (currently at most two events are cached). 26273 * If the event is not allocated from the timer cache, free it right away. 26274 */ 26275 static void 26276 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26277 { 26278 mblk_t *mp1 = tcp->tcp_timercache; 26279 tcp_stack_t *tcps = tcp->tcp_tcps; 26280 26281 if (mp->b_wptr != NULL) { 26282 /* 26283 * This allocation is not from a timer cache, free it right 26284 * away. 26285 */ 26286 if (mp->b_wptr != (uchar_t *)-1) 26287 freeb(mp); 26288 else 26289 kmem_free(mp, (size_t)mp->b_datap); 26290 } else if (mp1 == NULL || mp1->b_next == NULL) { 26291 /* Cache this timer block for future allocations */ 26292 mp->b_rptr = (uchar_t *)(&mp[1]); 26293 mp->b_next = mp1; 26294 tcp->tcp_timercache = mp; 26295 } else { 26296 kmem_cache_free(tcp_timercache, mp); 26297 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26298 } 26299 } 26300 26301 /* 26302 * End of TCP Timers implementation. 26303 */ 26304 26305 /* 26306 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26307 * on the specified backing STREAMS q. Note, the caller may make the 26308 * decision to call based on the tcp_t.tcp_flow_stopped value which 26309 * when check outside the q's lock is only an advisory check ... 26310 */ 26311 26312 void 26313 tcp_setqfull(tcp_t *tcp) 26314 { 26315 queue_t *q = tcp->tcp_wq; 26316 tcp_stack_t *tcps = tcp->tcp_tcps; 26317 26318 if (!(q->q_flag & QFULL)) { 26319 mutex_enter(QLOCK(q)); 26320 if (!(q->q_flag & QFULL)) { 26321 /* still need to set QFULL */ 26322 q->q_flag |= QFULL; 26323 tcp->tcp_flow_stopped = B_TRUE; 26324 mutex_exit(QLOCK(q)); 26325 TCP_STAT(tcps, tcp_flwctl_on); 26326 } else { 26327 mutex_exit(QLOCK(q)); 26328 } 26329 } 26330 } 26331 26332 void 26333 tcp_clrqfull(tcp_t *tcp) 26334 { 26335 queue_t *q = tcp->tcp_wq; 26336 26337 if (q->q_flag & QFULL) { 26338 mutex_enter(QLOCK(q)); 26339 if (q->q_flag & QFULL) { 26340 q->q_flag &= ~QFULL; 26341 tcp->tcp_flow_stopped = B_FALSE; 26342 mutex_exit(QLOCK(q)); 26343 if (q->q_flag & QWANTW) 26344 qbackenable(q, 0); 26345 } else { 26346 mutex_exit(QLOCK(q)); 26347 } 26348 } 26349 } 26350 26351 26352 /* 26353 * kstats related to squeues i.e. not per IP instance 26354 */ 26355 static void * 26356 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26357 { 26358 kstat_t *ksp; 26359 26360 tcp_g_stat_t template = { 26361 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26362 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26363 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26364 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26365 }; 26366 26367 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26368 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26369 KSTAT_FLAG_VIRTUAL); 26370 26371 if (ksp == NULL) 26372 return (NULL); 26373 26374 bcopy(&template, tcp_g_statp, sizeof (template)); 26375 ksp->ks_data = (void *)tcp_g_statp; 26376 26377 kstat_install(ksp); 26378 return (ksp); 26379 } 26380 26381 static void 26382 tcp_g_kstat_fini(kstat_t *ksp) 26383 { 26384 if (ksp != NULL) { 26385 kstat_delete(ksp); 26386 } 26387 } 26388 26389 26390 static void * 26391 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26392 { 26393 kstat_t *ksp; 26394 26395 tcp_stat_t template = { 26396 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26397 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26398 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26399 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26400 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26401 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26402 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26403 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26404 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26405 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26406 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26407 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26408 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26409 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26410 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26411 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26412 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26413 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26414 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26415 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26416 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26417 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26418 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26419 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26420 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26421 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26422 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26423 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26424 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26425 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26426 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26427 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26428 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26429 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26430 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26431 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26432 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26433 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26434 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26435 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26436 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26437 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26438 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26439 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26440 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26441 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26442 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26443 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26444 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26445 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26446 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26447 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26448 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26449 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26450 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26451 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26452 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26453 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26454 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26455 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26456 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26457 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26458 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26459 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26460 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26461 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26462 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26463 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26464 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26465 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26466 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26467 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26468 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26469 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26470 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26471 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26472 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26473 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26474 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26475 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26476 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26477 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26478 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26479 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26480 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26481 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26482 }; 26483 26484 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26485 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26486 KSTAT_FLAG_VIRTUAL, stackid); 26487 26488 if (ksp == NULL) 26489 return (NULL); 26490 26491 bcopy(&template, tcps_statisticsp, sizeof (template)); 26492 ksp->ks_data = (void *)tcps_statisticsp; 26493 ksp->ks_private = (void *)(uintptr_t)stackid; 26494 26495 kstat_install(ksp); 26496 return (ksp); 26497 } 26498 26499 static void 26500 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26501 { 26502 if (ksp != NULL) { 26503 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26504 kstat_delete_netstack(ksp, stackid); 26505 } 26506 } 26507 26508 /* 26509 * TCP Kstats implementation 26510 */ 26511 static void * 26512 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26513 { 26514 kstat_t *ksp; 26515 26516 tcp_named_kstat_t template = { 26517 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26518 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26519 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26520 { "maxConn", KSTAT_DATA_INT32, 0 }, 26521 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26522 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26523 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26524 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26525 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26526 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26527 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26528 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26529 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26530 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26531 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26532 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26533 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26534 { "outAck", KSTAT_DATA_UINT32, 0 }, 26535 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26536 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26537 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26538 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26539 { "outControl", KSTAT_DATA_UINT32, 0 }, 26540 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26541 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26542 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26543 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26544 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26545 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26546 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26547 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26548 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26549 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26550 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26551 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26552 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26553 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26554 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26555 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26556 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26557 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26558 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26559 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26560 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26561 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26562 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26563 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26564 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26565 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26566 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26567 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26568 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26569 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26570 }; 26571 26572 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26573 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26574 26575 if (ksp == NULL) 26576 return (NULL); 26577 26578 template.rtoAlgorithm.value.ui32 = 4; 26579 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26580 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26581 template.maxConn.value.i32 = -1; 26582 26583 bcopy(&template, ksp->ks_data, sizeof (template)); 26584 ksp->ks_update = tcp_kstat_update; 26585 ksp->ks_private = (void *)(uintptr_t)stackid; 26586 26587 kstat_install(ksp); 26588 return (ksp); 26589 } 26590 26591 static void 26592 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26593 { 26594 if (ksp != NULL) { 26595 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26596 kstat_delete_netstack(ksp, stackid); 26597 } 26598 } 26599 26600 static int 26601 tcp_kstat_update(kstat_t *kp, int rw) 26602 { 26603 tcp_named_kstat_t *tcpkp; 26604 tcp_t *tcp; 26605 connf_t *connfp; 26606 conn_t *connp; 26607 int i; 26608 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26609 netstack_t *ns; 26610 tcp_stack_t *tcps; 26611 ip_stack_t *ipst; 26612 26613 if ((kp == NULL) || (kp->ks_data == NULL)) 26614 return (EIO); 26615 26616 if (rw == KSTAT_WRITE) 26617 return (EACCES); 26618 26619 ns = netstack_find_by_stackid(stackid); 26620 if (ns == NULL) 26621 return (-1); 26622 tcps = ns->netstack_tcp; 26623 if (tcps == NULL) { 26624 netstack_rele(ns); 26625 return (-1); 26626 } 26627 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26628 26629 tcpkp->currEstab.value.ui32 = 0; 26630 26631 ipst = ns->netstack_ip; 26632 26633 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26634 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26635 connp = NULL; 26636 while ((connp = 26637 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26638 tcp = connp->conn_tcp; 26639 switch (tcp_snmp_state(tcp)) { 26640 case MIB2_TCP_established: 26641 case MIB2_TCP_closeWait: 26642 tcpkp->currEstab.value.ui32++; 26643 break; 26644 } 26645 } 26646 } 26647 26648 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26649 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26650 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26651 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26652 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26653 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26654 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26655 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26656 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26657 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26658 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26659 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26660 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26661 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26662 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26663 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26664 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26665 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26666 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26667 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26668 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26669 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26670 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26671 tcpkp->inDataInorderSegs.value.ui32 = 26672 tcps->tcps_mib.tcpInDataInorderSegs; 26673 tcpkp->inDataInorderBytes.value.ui32 = 26674 tcps->tcps_mib.tcpInDataInorderBytes; 26675 tcpkp->inDataUnorderSegs.value.ui32 = 26676 tcps->tcps_mib.tcpInDataUnorderSegs; 26677 tcpkp->inDataUnorderBytes.value.ui32 = 26678 tcps->tcps_mib.tcpInDataUnorderBytes; 26679 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26680 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26681 tcpkp->inDataPartDupSegs.value.ui32 = 26682 tcps->tcps_mib.tcpInDataPartDupSegs; 26683 tcpkp->inDataPartDupBytes.value.ui32 = 26684 tcps->tcps_mib.tcpInDataPartDupBytes; 26685 tcpkp->inDataPastWinSegs.value.ui32 = 26686 tcps->tcps_mib.tcpInDataPastWinSegs; 26687 tcpkp->inDataPastWinBytes.value.ui32 = 26688 tcps->tcps_mib.tcpInDataPastWinBytes; 26689 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26690 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26691 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26692 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26693 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26694 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26695 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26696 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26697 tcpkp->timKeepaliveProbe.value.ui32 = 26698 tcps->tcps_mib.tcpTimKeepaliveProbe; 26699 tcpkp->timKeepaliveDrop.value.ui32 = 26700 tcps->tcps_mib.tcpTimKeepaliveDrop; 26701 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26702 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26703 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26704 tcpkp->outSackRetransSegs.value.ui32 = 26705 tcps->tcps_mib.tcpOutSackRetransSegs; 26706 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26707 26708 netstack_rele(ns); 26709 return (0); 26710 } 26711 26712 void 26713 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26714 { 26715 uint16_t hdr_len; 26716 ipha_t *ipha; 26717 uint8_t *nexthdrp; 26718 tcph_t *tcph; 26719 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26720 26721 /* Already has an eager */ 26722 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26723 TCP_STAT(tcps, tcp_reinput_syn); 26724 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 26725 connp, SQTAG_TCP_REINPUT_EAGER); 26726 return; 26727 } 26728 26729 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26730 case IPV4_VERSION: 26731 ipha = (ipha_t *)mp->b_rptr; 26732 hdr_len = IPH_HDR_LENGTH(ipha); 26733 break; 26734 case IPV6_VERSION: 26735 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26736 &hdr_len, &nexthdrp)) { 26737 CONN_DEC_REF(connp); 26738 freemsg(mp); 26739 return; 26740 } 26741 break; 26742 } 26743 26744 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26745 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26746 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26747 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26748 } 26749 26750 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 26751 SQTAG_TCP_REINPUT); 26752 } 26753 26754 static squeue_func_t 26755 tcp_squeue_switch(int val) 26756 { 26757 squeue_func_t rval = squeue_fill; 26758 26759 switch (val) { 26760 case 1: 26761 rval = squeue_enter_nodrain; 26762 break; 26763 case 2: 26764 rval = squeue_enter; 26765 break; 26766 default: 26767 break; 26768 } 26769 return (rval); 26770 } 26771 26772 /* 26773 * This is called once for each squeue - globally for all stack 26774 * instances. 26775 */ 26776 static void 26777 tcp_squeue_add(squeue_t *sqp) 26778 { 26779 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26780 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26781 26782 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26783 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 26784 sqp, TCP_TIME_WAIT_DELAY); 26785 if (tcp_free_list_max_cnt == 0) { 26786 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26787 max_ncpus : boot_max_ncpus); 26788 26789 /* 26790 * Limit number of entries to 1% of availble memory / tcp_ncpus 26791 */ 26792 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26793 (tcp_ncpus * sizeof (tcp_t) * 100); 26794 } 26795 tcp_time_wait->tcp_free_list_cnt = 0; 26796 } 26797