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 /* 238 * Values for squeue switch: 239 * 1: squeue_enter_nodrain 240 * 2: squeue_enter 241 * 3: squeue_fill 242 */ 243 int tcp_squeue_close = 2; /* Setable in /etc/system */ 244 int tcp_squeue_wput = 2; 245 246 squeue_func_t tcp_squeue_close_proc; 247 squeue_func_t tcp_squeue_wput_proc; 248 249 /* 250 * This controls how tiny a write must be before we try to copy it 251 * into the the mblk on the tail of the transmit queue. Not much 252 * speedup is observed for values larger than sixteen. Zero will 253 * disable the optimisation. 254 */ 255 int tcp_tx_pull_len = 16; 256 257 /* 258 * TCP Statistics. 259 * 260 * How TCP statistics work. 261 * 262 * There are two types of statistics invoked by two macros. 263 * 264 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 265 * supposed to be used in non MT-hot paths of the code. 266 * 267 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 268 * supposed to be used for DEBUG purposes and may be used on a hot path. 269 * 270 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 271 * (use "kstat tcp" to get them). 272 * 273 * There is also additional debugging facility that marks tcp_clean_death() 274 * instances and saves them in tcp_t structure. It is triggered by 275 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 276 * tcp_clean_death() calls that counts the number of times each tag was hit. It 277 * is triggered by TCP_CLD_COUNTERS define. 278 * 279 * How to add new counters. 280 * 281 * 1) Add a field in the tcp_stat structure describing your counter. 282 * 2) Add a line in the template in tcp_kstat2_init() with the name 283 * of the counter. 284 * 285 * IMPORTANT!! - make sure that both are in sync !! 286 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 287 * 288 * Please avoid using private counters which are not kstat-exported. 289 * 290 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 291 * in tcp_t structure. 292 * 293 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 294 */ 295 296 #ifndef TCP_DEBUG_COUNTER 297 #ifdef DEBUG 298 #define TCP_DEBUG_COUNTER 1 299 #else 300 #define TCP_DEBUG_COUNTER 0 301 #endif 302 #endif 303 304 #define TCP_CLD_COUNTERS 0 305 306 #define TCP_TAG_CLEAN_DEATH 1 307 #define TCP_MAX_CLEAN_DEATH_TAG 32 308 309 #ifdef lint 310 static int _lint_dummy_; 311 #endif 312 313 #if TCP_CLD_COUNTERS 314 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 315 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 316 #elif defined(lint) 317 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 318 #else 319 #define TCP_CLD_STAT(x) 320 #endif 321 322 #if TCP_DEBUG_COUNTER 323 #define TCP_DBGSTAT(tcps, x) \ 324 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 325 #define TCP_G_DBGSTAT(x) \ 326 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 327 #elif defined(lint) 328 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 329 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 330 #else 331 #define TCP_DBGSTAT(tcps, x) 332 #define TCP_G_DBGSTAT(x) 333 #endif 334 335 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 336 337 tcp_g_stat_t tcp_g_statistics; 338 kstat_t *tcp_g_kstat; 339 340 /* 341 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 342 * tcp write side. 343 */ 344 #define CALL_IP_WPUT(connp, q, mp) { \ 345 tcp_stack_t *tcps; \ 346 \ 347 tcps = connp->conn_netstack->netstack_tcp; \ 348 ASSERT(((q)->q_flag & QREADR) == 0); \ 349 TCP_DBGSTAT(tcps, tcp_ip_output); \ 350 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 351 } 352 353 /* Macros for timestamp comparisons */ 354 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 355 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 356 357 /* 358 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 359 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 360 * by adding three components: a time component which grows by 1 every 4096 361 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 362 * a per-connection component which grows by 125000 for every new connection; 363 * and an "extra" component that grows by a random amount centered 364 * approximately on 64000. This causes the the ISS generator to cycle every 365 * 4.89 hours if no TCP connections are made, and faster if connections are 366 * made. 367 * 368 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 369 * components: a time component which grows by 250000 every second; and 370 * a per-connection component which grows by 125000 for every new connections. 371 * 372 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 373 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 374 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 375 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 376 * password. 377 */ 378 #define ISS_INCR 250000 379 #define ISS_NSEC_SHT 12 380 381 static sin_t sin_null; /* Zero address for quick clears */ 382 static sin6_t sin6_null; /* Zero address for quick clears */ 383 384 /* 385 * This implementation follows the 4.3BSD interpretation of the urgent 386 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 387 * incompatible changes in protocols like telnet and rlogin. 388 */ 389 #define TCP_OLD_URP_INTERPRETATION 1 390 391 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 392 (TCP_IS_DETACHED(tcp) && \ 393 (!(tcp)->tcp_hard_binding)) 394 395 /* 396 * TCP reassembly macros. We hide starting and ending sequence numbers in 397 * b_next and b_prev of messages on the reassembly queue. The messages are 398 * chained using b_cont. These macros are used in tcp_reass() so we don't 399 * have to see the ugly casts and assignments. 400 */ 401 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 402 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 403 (mblk_t *)(uintptr_t)(u)) 404 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 405 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 406 (mblk_t *)(uintptr_t)(u)) 407 408 /* 409 * Implementation of TCP Timers. 410 * ============================= 411 * 412 * INTERFACE: 413 * 414 * There are two basic functions dealing with tcp timers: 415 * 416 * timeout_id_t tcp_timeout(connp, func, time) 417 * clock_t tcp_timeout_cancel(connp, timeout_id) 418 * TCP_TIMER_RESTART(tcp, intvl) 419 * 420 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 421 * after 'time' ticks passed. The function called by timeout() must adhere to 422 * the same restrictions as a driver soft interrupt handler - it must not sleep 423 * or call other functions that might sleep. The value returned is the opaque 424 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 425 * cancel the request. The call to tcp_timeout() may fail in which case it 426 * returns zero. This is different from the timeout(9F) function which never 427 * fails. 428 * 429 * The call-back function 'func' always receives 'connp' as its single 430 * argument. It is always executed in the squeue corresponding to the tcp 431 * structure. The tcp structure is guaranteed to be present at the time the 432 * call-back is called. 433 * 434 * NOTE: The call-back function 'func' is never called if tcp is in 435 * the TCPS_CLOSED state. 436 * 437 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 438 * request. locks acquired by the call-back routine should not be held across 439 * the call to tcp_timeout_cancel() or a deadlock may result. 440 * 441 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 442 * Otherwise, it returns an integer value greater than or equal to 0. In 443 * particular, if the call-back function is already placed on the squeue, it can 444 * not be canceled. 445 * 446 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 447 * within squeue context corresponding to the tcp instance. Since the 448 * call-back is also called via the same squeue, there are no race 449 * conditions described in untimeout(9F) manual page since all calls are 450 * strictly serialized. 451 * 452 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 453 * stored in tcp_timer_tid and starts a new one using 454 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 455 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 456 * field. 457 * 458 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 459 * call-back may still be called, so it is possible tcp_timer() will be 460 * called several times. This should not be a problem since tcp_timer() 461 * should always check the tcp instance state. 462 * 463 * 464 * IMPLEMENTATION: 465 * 466 * TCP timers are implemented using three-stage process. The call to 467 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 468 * when the timer expires. The tcp_timer_callback() arranges the call of the 469 * tcp_timer_handler() function via squeue corresponding to the tcp 470 * instance. The tcp_timer_handler() calls actual requested timeout call-back 471 * and passes tcp instance as an argument to it. Information is passed between 472 * stages using the tcp_timer_t structure which contains the connp pointer, the 473 * tcp call-back to call and the timeout id returned by the timeout(9F). 474 * 475 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 476 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 477 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 478 * returns the pointer to this mblk. 479 * 480 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 481 * looks like a normal mblk without actual dblk attached to it. 482 * 483 * To optimize performance each tcp instance holds a small cache of timer 484 * mblocks. In the current implementation it caches up to two timer mblocks per 485 * tcp instance. The cache is preserved over tcp frees and is only freed when 486 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 487 * timer processing happens on a corresponding squeue, the cache manipulation 488 * does not require any locks. Experiments show that majority of timer mblocks 489 * allocations are satisfied from the tcp cache and do not involve kmem calls. 490 * 491 * The tcp_timeout() places a refhold on the connp instance which guarantees 492 * that it will be present at the time the call-back function fires. The 493 * tcp_timer_handler() drops the reference after calling the call-back, so the 494 * call-back function does not need to manipulate the references explicitly. 495 */ 496 497 typedef struct tcp_timer_s { 498 conn_t *connp; 499 void (*tcpt_proc)(void *); 500 timeout_id_t tcpt_tid; 501 } tcp_timer_t; 502 503 static kmem_cache_t *tcp_timercache; 504 kmem_cache_t *tcp_sack_info_cache; 505 kmem_cache_t *tcp_iphc_cache; 506 507 /* 508 * For scalability, we must not run a timer for every TCP connection 509 * in TIME_WAIT state. To see why, consider (for time wait interval of 510 * 4 minutes): 511 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 512 * 513 * This list is ordered by time, so you need only delete from the head 514 * until you get to entries which aren't old enough to delete yet. 515 * The list consists of only the detached TIME_WAIT connections. 516 * 517 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 518 * becomes detached TIME_WAIT (either by changing the state and already 519 * being detached or the other way around). This means that the TIME_WAIT 520 * state can be extended (up to doubled) if the connection doesn't become 521 * detached for a long time. 522 * 523 * The list manipulations (including tcp_time_wait_next/prev) 524 * are protected by the tcp_time_wait_lock. The content of the 525 * detached TIME_WAIT connections is protected by the normal perimeters. 526 * 527 * This list is per squeue and squeues are shared across the tcp_stack_t's. 528 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 529 * and conn_netstack. 530 * The tcp_t's that are added to tcp_free_list are disassociated and 531 * have NULL tcp_tcps and conn_netstack pointers. 532 */ 533 typedef struct tcp_squeue_priv_s { 534 kmutex_t tcp_time_wait_lock; 535 timeout_id_t tcp_time_wait_tid; 536 tcp_t *tcp_time_wait_head; 537 tcp_t *tcp_time_wait_tail; 538 tcp_t *tcp_free_list; 539 uint_t tcp_free_list_cnt; 540 } tcp_squeue_priv_t; 541 542 /* 543 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 544 * Running it every 5 seconds seems to give the best results. 545 */ 546 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 547 548 /* 549 * To prevent memory hog, limit the number of entries in tcp_free_list 550 * to 1% of available memory / number of cpus 551 */ 552 uint_t tcp_free_list_max_cnt = 0; 553 554 #define TCP_XMIT_LOWATER 4096 555 #define TCP_XMIT_HIWATER 49152 556 #define TCP_RECV_LOWATER 2048 557 #define TCP_RECV_HIWATER 49152 558 559 /* 560 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 561 */ 562 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 563 564 #define TIDUSZ 4096 /* transport interface data unit size */ 565 566 /* 567 * Bind hash list size and has function. It has to be a power of 2 for 568 * hashing. 569 */ 570 #define TCP_BIND_FANOUT_SIZE 512 571 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 572 /* 573 * Size of listen and acceptor hash list. It has to be a power of 2 for 574 * hashing. 575 */ 576 #define TCP_FANOUT_SIZE 256 577 578 #ifdef _ILP32 579 #define TCP_ACCEPTOR_HASH(accid) \ 580 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 581 #else 582 #define TCP_ACCEPTOR_HASH(accid) \ 583 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 584 #endif /* _ILP32 */ 585 586 #define IP_ADDR_CACHE_SIZE 2048 587 #define IP_ADDR_CACHE_HASH(faddr) \ 588 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 589 590 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 591 #define TCP_HSP_HASH_SIZE 256 592 593 #define TCP_HSP_HASH(addr) \ 594 (((addr>>24) ^ (addr >>16) ^ \ 595 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 596 597 /* 598 * TCP options struct returned from tcp_parse_options. 599 */ 600 typedef struct tcp_opt_s { 601 uint32_t tcp_opt_mss; 602 uint32_t tcp_opt_wscale; 603 uint32_t tcp_opt_ts_val; 604 uint32_t tcp_opt_ts_ecr; 605 tcp_t *tcp; 606 } tcp_opt_t; 607 608 /* 609 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 610 */ 611 612 #ifdef _BIG_ENDIAN 613 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 614 (TCPOPT_TSTAMP << 8) | 10) 615 #else 616 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 617 (TCPOPT_NOP << 8) | TCPOPT_NOP) 618 #endif 619 620 /* 621 * Flags returned from tcp_parse_options. 622 */ 623 #define TCP_OPT_MSS_PRESENT 1 624 #define TCP_OPT_WSCALE_PRESENT 2 625 #define TCP_OPT_TSTAMP_PRESENT 4 626 #define TCP_OPT_SACK_OK_PRESENT 8 627 #define TCP_OPT_SACK_PRESENT 16 628 629 /* TCP option length */ 630 #define TCPOPT_NOP_LEN 1 631 #define TCPOPT_MAXSEG_LEN 4 632 #define TCPOPT_WS_LEN 3 633 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 634 #define TCPOPT_TSTAMP_LEN 10 635 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 636 #define TCPOPT_SACK_OK_LEN 2 637 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 638 #define TCPOPT_REAL_SACK_LEN 4 639 #define TCPOPT_MAX_SACK_LEN 36 640 #define TCPOPT_HEADER_LEN 2 641 642 /* TCP cwnd burst factor. */ 643 #define TCP_CWND_INFINITE 65535 644 #define TCP_CWND_SS 3 645 #define TCP_CWND_NORMAL 5 646 647 /* Maximum TCP initial cwin (start/restart). */ 648 #define TCP_MAX_INIT_CWND 8 649 650 /* 651 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 652 * either tcp_slow_start_initial or tcp_slow_start_after idle 653 * depending on the caller. If the upper layer has not used the 654 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 655 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 656 * If the upper layer has changed set the tcp_init_cwnd, just use 657 * it to calculate the tcp_cwnd. 658 */ 659 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 660 { \ 661 if ((tcp)->tcp_init_cwnd == 0) { \ 662 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 663 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 664 } else { \ 665 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 666 } \ 667 tcp->tcp_cwnd_cnt = 0; \ 668 } 669 670 /* TCP Timer control structure */ 671 typedef struct tcpt_s { 672 pfv_t tcpt_pfv; /* The routine we are to call */ 673 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 674 } tcpt_t; 675 676 /* Host Specific Parameter structure */ 677 typedef struct tcp_hsp { 678 struct tcp_hsp *tcp_hsp_next; 679 in6_addr_t tcp_hsp_addr_v6; 680 in6_addr_t tcp_hsp_subnet_v6; 681 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 682 int32_t tcp_hsp_sendspace; 683 int32_t tcp_hsp_recvspace; 684 int32_t tcp_hsp_tstamp; 685 } tcp_hsp_t; 686 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 687 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 688 689 /* 690 * Functions called directly via squeue having a prototype of edesc_t. 691 */ 692 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 693 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 694 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 695 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 696 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 697 void tcp_input(void *arg, mblk_t *mp, void *arg2); 698 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 699 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 700 void tcp_output(void *arg, mblk_t *mp, void *arg2); 701 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 702 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 703 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 704 705 706 /* Prototype for TCP functions */ 707 static void tcp_random_init(void); 708 int tcp_random(void); 709 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 710 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 711 tcp_t *eager); 712 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 713 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 714 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 715 boolean_t user_specified); 716 static void tcp_closei_local(tcp_t *tcp); 717 static void tcp_close_detached(tcp_t *tcp); 718 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 719 mblk_t *idmp, mblk_t **defermp); 720 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 721 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 722 in_port_t dstport, uint_t srcid); 723 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 724 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 725 uint32_t scope_id); 726 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 727 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 728 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 729 static char *tcp_display(tcp_t *tcp, char *, char); 730 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 731 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 732 static void tcp_eager_unlink(tcp_t *tcp); 733 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 734 int unixerr); 735 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 736 int tlierr, int unixerr); 737 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 738 cred_t *cr); 739 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 740 char *value, caddr_t cp, cred_t *cr); 741 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 742 char *value, caddr_t cp, cred_t *cr); 743 static int tcp_tpistate(tcp_t *tcp); 744 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 745 int caller_holds_lock); 746 static void tcp_bind_hash_remove(tcp_t *tcp); 747 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 748 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 749 static void tcp_acceptor_hash_remove(tcp_t *tcp); 750 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 751 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 752 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 753 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 754 void tcp_g_q_setup(tcp_stack_t *); 755 void tcp_g_q_create(tcp_stack_t *); 756 void tcp_g_q_destroy(tcp_stack_t *); 757 static int tcp_header_init_ipv4(tcp_t *tcp); 758 static int tcp_header_init_ipv6(tcp_t *tcp); 759 int tcp_init(tcp_t *tcp, queue_t *q); 760 static int tcp_init_values(tcp_t *tcp); 761 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 762 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 763 t_scalar_t addr_length); 764 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 765 static void tcp_ip_notify(tcp_t *tcp); 766 static mblk_t *tcp_ire_mp(mblk_t *mp); 767 static void tcp_iss_init(tcp_t *tcp); 768 static void tcp_keepalive_killer(void *arg); 769 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 770 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 771 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 772 int *do_disconnectp, int *t_errorp, int *sys_errorp); 773 static boolean_t tcp_allow_connopt_set(int level, int name); 774 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 775 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 776 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 777 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 778 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 779 mblk_t *mblk); 780 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 781 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 782 uchar_t *ptr, uint_t len); 783 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 784 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 785 tcp_stack_t *); 786 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 787 caddr_t cp, cred_t *cr); 788 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 789 caddr_t cp, cred_t *cr); 790 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 791 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 792 caddr_t cp, cred_t *cr); 793 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 794 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 795 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 796 static void tcp_reinit(tcp_t *tcp); 797 static void tcp_reinit_values(tcp_t *tcp); 798 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 799 tcp_t *thisstream, cred_t *cr); 800 801 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 802 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 803 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 804 static void tcp_ss_rexmit(tcp_t *tcp); 805 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 806 static void tcp_process_options(tcp_t *, tcph_t *); 807 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 808 static void tcp_rsrv(queue_t *q); 809 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 810 static int tcp_snmp_state(tcp_t *tcp); 811 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 812 cred_t *cr); 813 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 814 cred_t *cr); 815 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 816 cred_t *cr); 817 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 818 cred_t *cr); 819 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 820 cred_t *cr); 821 static int tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, 822 caddr_t cp, cred_t *cr); 823 static int tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, 824 caddr_t cp, cred_t *cr); 825 static int tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, 826 cred_t *cr); 827 static void tcp_timer(void *arg); 828 static void tcp_timer_callback(void *); 829 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 830 boolean_t random); 831 static in_port_t tcp_get_next_priv_port(const tcp_t *); 832 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 833 void tcp_wput_accept(queue_t *q, mblk_t *mp); 834 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 835 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 836 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 837 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 838 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 839 const int num_sack_blk, int *usable, uint_t *snxt, 840 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 841 const int mdt_thres); 842 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 843 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 844 const int num_sack_blk, int *usable, uint_t *snxt, 845 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 846 const int mdt_thres); 847 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 848 int num_sack_blk); 849 static void tcp_wsrv(queue_t *q); 850 static int tcp_xmit_end(tcp_t *tcp); 851 static void tcp_ack_timer(void *arg); 852 static mblk_t *tcp_ack_mp(tcp_t *tcp); 853 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 854 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 855 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 856 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 857 uint32_t ack, int ctl); 858 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 859 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 860 static int setmaxps(queue_t *q, int maxpsz); 861 static void tcp_set_rto(tcp_t *, time_t); 862 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 863 boolean_t, boolean_t); 864 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 865 boolean_t ipsec_mctl); 866 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 867 char *opt, int optlen); 868 static int tcp_build_hdrs(queue_t *, tcp_t *); 869 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 870 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 871 tcph_t *tcph); 872 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 873 boolean_t tcp_reserved_port_add(int, in_port_t *, in_port_t *); 874 boolean_t tcp_reserved_port_del(in_port_t, in_port_t); 875 boolean_t tcp_reserved_port_check(in_port_t, tcp_stack_t *); 876 static tcp_t *tcp_alloc_temp_tcp(in_port_t, tcp_stack_t *); 877 static int tcp_reserved_port_list(queue_t *, mblk_t *, caddr_t, cred_t *); 878 static mblk_t *tcp_mdt_info_mp(mblk_t *); 879 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 880 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 881 const boolean_t, const uint32_t, const uint32_t, 882 const uint32_t, const uint32_t, tcp_stack_t *); 883 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 884 const uint_t, const uint_t, boolean_t *); 885 static mblk_t *tcp_lso_info_mp(mblk_t *); 886 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 887 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 888 extern mblk_t *tcp_timermp_alloc(int); 889 extern void tcp_timermp_free(tcp_t *); 890 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 891 static void tcp_stop_lingering(tcp_t *tcp); 892 static void tcp_close_linger_timeout(void *arg); 893 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 894 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 895 static void tcp_stack_fini(netstackid_t stackid, void *arg); 896 static void *tcp_g_kstat_init(tcp_g_stat_t *); 897 static void tcp_g_kstat_fini(kstat_t *); 898 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 899 static void tcp_kstat_fini(netstackid_t, kstat_t *); 900 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 901 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 902 static int tcp_kstat_update(kstat_t *kp, int rw); 903 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 904 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 905 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 906 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 907 tcph_t *tcph, mblk_t *idmp); 908 static squeue_func_t tcp_squeue_switch(int); 909 910 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 911 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 912 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 913 static int tcp_close(queue_t *, int); 914 static int tcpclose_accept(queue_t *); 915 916 static void tcp_squeue_add(squeue_t *); 917 static boolean_t tcp_zcopy_check(tcp_t *); 918 static void tcp_zcopy_notify(tcp_t *); 919 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 920 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 921 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 922 923 extern void tcp_kssl_input(tcp_t *, mblk_t *); 924 925 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 926 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 927 928 /* 929 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 930 * 931 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 932 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 933 * (defined in tcp.h) needs to be filled in and passed into the kernel 934 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 935 * structure contains the four-tuple of a TCP connection and a range of TCP 936 * states (specified by ac_start and ac_end). The use of wildcard addresses 937 * and ports is allowed. Connections with a matching four tuple and a state 938 * within the specified range will be aborted. The valid states for the 939 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 940 * inclusive. 941 * 942 * An application which has its connection aborted by this ioctl will receive 943 * an error that is dependent on the connection state at the time of the abort. 944 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 945 * though a RST packet has been received. If the connection state is equal to 946 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 947 * and all resources associated with the connection will be freed. 948 */ 949 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 950 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 951 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 952 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 953 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 954 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 955 boolean_t, tcp_stack_t *); 956 957 static struct module_info tcp_rinfo = { 958 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 959 }; 960 961 static struct module_info tcp_winfo = { 962 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 963 }; 964 965 /* 966 * Entry points for TCP as a device. The normal case which supports 967 * the TCP functionality. 968 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 969 */ 970 struct qinit tcp_rinitv4 = { 971 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_close, NULL, &tcp_rinfo 972 }; 973 974 struct qinit tcp_rinitv6 = { 975 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_close, NULL, &tcp_rinfo 976 }; 977 978 struct qinit tcp_winit = { 979 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 980 }; 981 982 /* Initial entry point for TCP in socket mode. */ 983 struct qinit tcp_sock_winit = { 984 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 985 }; 986 987 /* 988 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 989 * an accept. Avoid allocating data structures since eager has already 990 * been created. 991 */ 992 struct qinit tcp_acceptor_rinit = { 993 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 994 }; 995 996 struct qinit tcp_acceptor_winit = { 997 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 998 }; 999 1000 /* 1001 * Entry points for TCP loopback (read side only) 1002 * The open routine is only used for reopens, thus no need to 1003 * have a separate one for tcp_openv6. 1004 */ 1005 struct qinit tcp_loopback_rinit = { 1006 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_openv4, tcp_close, (pfi_t)0, 1007 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1008 }; 1009 1010 /* For AF_INET aka /dev/tcp */ 1011 struct streamtab tcpinfov4 = { 1012 &tcp_rinitv4, &tcp_winit 1013 }; 1014 1015 /* For AF_INET6 aka /dev/tcp6 */ 1016 struct streamtab tcpinfov6 = { 1017 &tcp_rinitv6, &tcp_winit 1018 }; 1019 1020 /* 1021 * Have to ensure that tcp_g_q_close is not done by an 1022 * interrupt thread. 1023 */ 1024 static taskq_t *tcp_taskq; 1025 1026 /* 1027 * TCP has a private interface for other kernel modules to reserve a 1028 * port range for them to use. Once reserved, TCP will not use any ports 1029 * in the range. This interface relies on the TCP_EXCLBIND feature. If 1030 * the semantics of TCP_EXCLBIND is changed, implementation of this interface 1031 * has to be verified. 1032 * 1033 * There can be TCP_RESERVED_PORTS_ARRAY_MAX_SIZE port ranges. Each port 1034 * range can cover at most TCP_RESERVED_PORTS_RANGE_MAX ports. A port 1035 * range is [port a, port b] inclusive. And each port range is between 1036 * TCP_LOWESET_RESERVED_PORT and TCP_LARGEST_RESERVED_PORT inclusive. 1037 * 1038 * Note that the default anonymous port range starts from 32768. There is 1039 * no port "collision" between that and the reserved port range. If there 1040 * is port collision (because the default smallest anonymous port is lowered 1041 * or some apps specifically bind to ports in the reserved port range), the 1042 * system may not be able to reserve a port range even there are enough 1043 * unbound ports as a reserved port range contains consecutive ports . 1044 */ 1045 #define TCP_RESERVED_PORTS_ARRAY_MAX_SIZE 5 1046 #define TCP_RESERVED_PORTS_RANGE_MAX 1000 1047 #define TCP_SMALLEST_RESERVED_PORT 10240 1048 #define TCP_LARGEST_RESERVED_PORT 20480 1049 1050 /* Structure to represent those reserved port ranges. */ 1051 typedef struct tcp_rport_s { 1052 in_port_t lo_port; 1053 in_port_t hi_port; 1054 tcp_t **temp_tcp_array; 1055 } tcp_rport_t; 1056 1057 /* Setable only in /etc/system. Move to ndd? */ 1058 boolean_t tcp_icmp_source_quench = B_FALSE; 1059 1060 /* 1061 * Following assumes TPI alignment requirements stay along 32 bit 1062 * boundaries 1063 */ 1064 #define ROUNDUP32(x) \ 1065 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1066 1067 /* Template for response to info request. */ 1068 static struct T_info_ack tcp_g_t_info_ack = { 1069 T_INFO_ACK, /* PRIM_type */ 1070 0, /* TSDU_size */ 1071 T_INFINITE, /* ETSDU_size */ 1072 T_INVALID, /* CDATA_size */ 1073 T_INVALID, /* DDATA_size */ 1074 sizeof (sin_t), /* ADDR_size */ 1075 0, /* OPT_size - not initialized here */ 1076 TIDUSZ, /* TIDU_size */ 1077 T_COTS_ORD, /* SERV_type */ 1078 TCPS_IDLE, /* CURRENT_state */ 1079 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1080 }; 1081 1082 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1083 T_INFO_ACK, /* PRIM_type */ 1084 0, /* TSDU_size */ 1085 T_INFINITE, /* ETSDU_size */ 1086 T_INVALID, /* CDATA_size */ 1087 T_INVALID, /* DDATA_size */ 1088 sizeof (sin6_t), /* ADDR_size */ 1089 0, /* OPT_size - not initialized here */ 1090 TIDUSZ, /* TIDU_size */ 1091 T_COTS_ORD, /* SERV_type */ 1092 TCPS_IDLE, /* CURRENT_state */ 1093 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1094 }; 1095 1096 #define MS 1L 1097 #define SECONDS (1000 * MS) 1098 #define MINUTES (60 * SECONDS) 1099 #define HOURS (60 * MINUTES) 1100 #define DAYS (24 * HOURS) 1101 1102 #define PARAM_MAX (~(uint32_t)0) 1103 1104 /* Max size IP datagram is 64k - 1 */ 1105 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1106 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1107 /* Max of the above */ 1108 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1109 1110 /* Largest TCP port number */ 1111 #define TCP_MAX_PORT (64 * 1024 - 1) 1112 1113 /* 1114 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1115 * layer header. It has to be a multiple of 4. 1116 */ 1117 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1118 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1119 1120 /* 1121 * All of these are alterable, within the min/max values given, at run time. 1122 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1123 * per the TCP spec. 1124 */ 1125 /* BEGIN CSTYLED */ 1126 static tcpparam_t lcl_tcp_param_arr[] = { 1127 /*min max value name */ 1128 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1129 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1130 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1131 { 1, 1024, 1, "tcp_conn_req_min" }, 1132 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1133 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1134 { 0, 10, 0, "tcp_debug" }, 1135 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1136 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1137 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1138 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1139 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1140 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1141 { 1, 255, 64, "tcp_ipv4_ttl"}, 1142 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1143 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1144 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1145 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1146 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1147 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1148 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1149 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1150 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1151 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1152 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1153 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1154 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1155 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1156 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1157 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1158 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1159 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1160 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1161 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1162 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1163 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1164 { 0, TCP_MSS_MAX, 64, "tcp_co_min"}, 1165 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1166 /* 1167 * Question: What default value should I set for tcp_strong_iss? 1168 */ 1169 { 0, 2, 1, "tcp_strong_iss"}, 1170 { 0, 65536, 20, "tcp_rtt_updates"}, 1171 { 0, 1, 1, "tcp_wscale_always"}, 1172 { 0, 1, 0, "tcp_tstamp_always"}, 1173 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1174 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1175 { 0, 16, 2, "tcp_deferred_acks_max"}, 1176 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1177 { 1, 4, 4, "tcp_slow_start_initial"}, 1178 { 10*MS, 50*MS, 20*MS, "tcp_co_timer_interval"}, 1179 { 0, 2, 2, "tcp_sack_permitted"}, 1180 { 0, 1, 0, "tcp_trace"}, 1181 { 0, 1, 1, "tcp_compression_enabled"}, 1182 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1183 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1184 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1185 { 0, 1, 0, "tcp_rev_src_routes"}, 1186 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1187 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1188 { 0, 16, 8, "tcp_local_dacks_max"}, 1189 { 0, 2, 1, "tcp_ecn_permitted"}, 1190 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1191 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1192 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1193 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1194 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1195 }; 1196 /* END CSTYLED */ 1197 1198 /* 1199 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1200 * each header fragment in the header buffer. Each parameter value has 1201 * to be a multiple of 4 (32-bit aligned). 1202 */ 1203 static tcpparam_t lcl_tcp_mdt_head_param = 1204 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1205 static tcpparam_t lcl_tcp_mdt_tail_param = 1206 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1207 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1208 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1209 1210 /* 1211 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1212 * the maximum number of payload buffers associated per Multidata. 1213 */ 1214 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1215 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1216 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1217 1218 /* Round up the value to the nearest mss. */ 1219 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1220 1221 /* 1222 * Set ECN capable transport (ECT) code point in IP header. 1223 * 1224 * Note that there are 2 ECT code points '01' and '10', which are called 1225 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1226 * point ECT(0) for TCP as described in RFC 2481. 1227 */ 1228 #define SET_ECT(tcp, iph) \ 1229 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1230 /* We need to clear the code point first. */ \ 1231 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1232 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1233 } else { \ 1234 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1235 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1236 } 1237 1238 /* 1239 * The format argument to pass to tcp_display(). 1240 * DISP_PORT_ONLY means that the returned string has only port info. 1241 * DISP_ADDR_AND_PORT means that the returned string also contains the 1242 * remote and local IP address. 1243 */ 1244 #define DISP_PORT_ONLY 1 1245 #define DISP_ADDR_AND_PORT 2 1246 1247 #define NDD_TOO_QUICK_MSG \ 1248 "ndd get info rate too high for non-privileged users, try again " \ 1249 "later.\n" 1250 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1251 1252 #define IS_VMLOANED_MBLK(mp) \ 1253 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1254 1255 1256 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1257 boolean_t tcp_mdt_chain = B_TRUE; 1258 1259 /* 1260 * MDT threshold in the form of effective send MSS multiplier; we take 1261 * the MDT path if the amount of unsent data exceeds the threshold value 1262 * (default threshold is 1*SMSS). 1263 */ 1264 uint_t tcp_mdt_smss_threshold = 1; 1265 1266 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1267 1268 /* 1269 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1270 * tunable settable via NDD. Otherwise, the per-connection behavior is 1271 * determined dynamically during tcp_adapt_ire(), which is the default. 1272 */ 1273 boolean_t tcp_static_maxpsz = B_FALSE; 1274 1275 /* Setable in /etc/system */ 1276 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1277 uint32_t tcp_random_anon_port = 1; 1278 1279 /* 1280 * To reach to an eager in Q0 which can be dropped due to an incoming 1281 * new SYN request when Q0 is full, a new doubly linked list is 1282 * introduced. This list allows to select an eager from Q0 in O(1) time. 1283 * This is needed to avoid spending too much time walking through the 1284 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1285 * this new list has to be a member of Q0. 1286 * This list is headed by listener's tcp_t. When the list is empty, 1287 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1288 * of listener's tcp_t point to listener's tcp_t itself. 1289 * 1290 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1291 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1292 * These macros do not affect the eager's membership to Q0. 1293 */ 1294 1295 1296 #define MAKE_DROPPABLE(listener, eager) \ 1297 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1298 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1299 = (eager); \ 1300 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1301 (eager)->tcp_eager_next_drop_q0 = \ 1302 (listener)->tcp_eager_next_drop_q0; \ 1303 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1304 } 1305 1306 #define MAKE_UNDROPPABLE(eager) \ 1307 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1308 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1309 = (eager)->tcp_eager_prev_drop_q0; \ 1310 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1311 = (eager)->tcp_eager_next_drop_q0; \ 1312 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1313 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1314 } 1315 1316 /* 1317 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1318 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1319 * data, TCP will not respond with an ACK. RFC 793 requires that 1320 * TCP responds with an ACK for such a bogus ACK. By not following 1321 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1322 * an attacker successfully spoofs an acceptable segment to our 1323 * peer; or when our peer is "confused." 1324 */ 1325 uint32_t tcp_drop_ack_unsent_cnt = 10; 1326 1327 /* 1328 * Hook functions to enable cluster networking 1329 * On non-clustered systems these vectors must always be NULL. 1330 */ 1331 1332 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1333 uint8_t *laddrp, in_port_t lport) = NULL; 1334 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1335 uint8_t *laddrp, in_port_t lport) = NULL; 1336 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1337 uint8_t *laddrp, in_port_t lport, 1338 uint8_t *faddrp, in_port_t fport) = NULL; 1339 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1340 uint8_t *laddrp, in_port_t lport, 1341 uint8_t *faddrp, in_port_t fport) = NULL; 1342 1343 /* 1344 * The following are defined in ip.c 1345 */ 1346 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1347 uint8_t *laddrp); 1348 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1349 uint8_t *laddrp, uint8_t *faddrp); 1350 1351 #define CL_INET_CONNECT(tcp) { \ 1352 if (cl_inet_connect != NULL) { \ 1353 /* \ 1354 * Running in cluster mode - register active connection \ 1355 * information \ 1356 */ \ 1357 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1358 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1359 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1360 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1361 (in_port_t)(tcp)->tcp_lport, \ 1362 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1363 (in_port_t)(tcp)->tcp_fport); \ 1364 } \ 1365 } else { \ 1366 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1367 &(tcp)->tcp_ip6h->ip6_src)) {\ 1368 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1369 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1370 (in_port_t)(tcp)->tcp_lport, \ 1371 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1372 (in_port_t)(tcp)->tcp_fport); \ 1373 } \ 1374 } \ 1375 } \ 1376 } 1377 1378 #define CL_INET_DISCONNECT(tcp) { \ 1379 if (cl_inet_disconnect != NULL) { \ 1380 /* \ 1381 * Running in cluster mode - deregister active \ 1382 * connection information \ 1383 */ \ 1384 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1385 if ((tcp)->tcp_ip_src != 0) { \ 1386 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1387 AF_INET, \ 1388 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1389 (in_port_t)(tcp)->tcp_lport, \ 1390 (uint8_t *) \ 1391 (&((tcp)->tcp_ipha->ipha_dst)),\ 1392 (in_port_t)(tcp)->tcp_fport); \ 1393 } \ 1394 } else { \ 1395 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1396 &(tcp)->tcp_ip_src_v6)) { \ 1397 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1398 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1399 (in_port_t)(tcp)->tcp_lport, \ 1400 (uint8_t *) \ 1401 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1402 (in_port_t)(tcp)->tcp_fport); \ 1403 } \ 1404 } \ 1405 } \ 1406 } 1407 1408 /* 1409 * Cluster networking hook for traversing current connection list. 1410 * This routine is used to extract the current list of live connections 1411 * which must continue to to be dispatched to this node. 1412 */ 1413 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1414 1415 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1416 void *arg, tcp_stack_t *tcps); 1417 1418 /* 1419 * Figure out the value of window scale opton. Note that the rwnd is 1420 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1421 * We cannot find the scale value and then do a round up of tcp_rwnd 1422 * because the scale value may not be correct after that. 1423 * 1424 * Set the compiler flag to make this function inline. 1425 */ 1426 static void 1427 tcp_set_ws_value(tcp_t *tcp) 1428 { 1429 int i; 1430 uint32_t rwnd = tcp->tcp_rwnd; 1431 1432 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1433 i++, rwnd >>= 1) 1434 ; 1435 tcp->tcp_rcv_ws = i; 1436 } 1437 1438 /* 1439 * Remove a connection from the list of detached TIME_WAIT connections. 1440 * It returns B_FALSE if it can't remove the connection from the list 1441 * as the connection has already been removed from the list due to an 1442 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1443 */ 1444 static boolean_t 1445 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1446 { 1447 boolean_t locked = B_FALSE; 1448 1449 if (tcp_time_wait == NULL) { 1450 tcp_time_wait = *((tcp_squeue_priv_t **) 1451 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1452 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1453 locked = B_TRUE; 1454 } else { 1455 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1456 } 1457 1458 if (tcp->tcp_time_wait_expire == 0) { 1459 ASSERT(tcp->tcp_time_wait_next == NULL); 1460 ASSERT(tcp->tcp_time_wait_prev == NULL); 1461 if (locked) 1462 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1463 return (B_FALSE); 1464 } 1465 ASSERT(TCP_IS_DETACHED(tcp)); 1466 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1467 1468 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1469 ASSERT(tcp->tcp_time_wait_prev == NULL); 1470 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1471 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1472 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1473 NULL; 1474 } else { 1475 tcp_time_wait->tcp_time_wait_tail = NULL; 1476 } 1477 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1478 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1479 ASSERT(tcp->tcp_time_wait_next == NULL); 1480 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1481 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1482 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1483 } else { 1484 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1485 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1486 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1487 tcp->tcp_time_wait_next; 1488 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1489 tcp->tcp_time_wait_prev; 1490 } 1491 tcp->tcp_time_wait_next = NULL; 1492 tcp->tcp_time_wait_prev = NULL; 1493 tcp->tcp_time_wait_expire = 0; 1494 1495 if (locked) 1496 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1497 return (B_TRUE); 1498 } 1499 1500 /* 1501 * Add a connection to the list of detached TIME_WAIT connections 1502 * and set its time to expire. 1503 */ 1504 static void 1505 tcp_time_wait_append(tcp_t *tcp) 1506 { 1507 tcp_stack_t *tcps = tcp->tcp_tcps; 1508 tcp_squeue_priv_t *tcp_time_wait = 1509 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1510 SQPRIVATE_TCP)); 1511 1512 tcp_timers_stop(tcp); 1513 1514 /* Freed above */ 1515 ASSERT(tcp->tcp_timer_tid == 0); 1516 ASSERT(tcp->tcp_ack_tid == 0); 1517 1518 /* must have happened at the time of detaching the tcp */ 1519 ASSERT(tcp->tcp_ptpahn == NULL); 1520 ASSERT(tcp->tcp_flow_stopped == 0); 1521 ASSERT(tcp->tcp_time_wait_next == NULL); 1522 ASSERT(tcp->tcp_time_wait_prev == NULL); 1523 ASSERT(tcp->tcp_time_wait_expire == NULL); 1524 ASSERT(tcp->tcp_listener == NULL); 1525 1526 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1527 /* 1528 * The value computed below in tcp->tcp_time_wait_expire may 1529 * appear negative or wrap around. That is ok since our 1530 * interest is only in the difference between the current lbolt 1531 * value and tcp->tcp_time_wait_expire. But the value should not 1532 * be zero, since it means the tcp is not in the TIME_WAIT list. 1533 * The corresponding comparison in tcp_time_wait_collector() uses 1534 * modular arithmetic. 1535 */ 1536 tcp->tcp_time_wait_expire += 1537 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1538 if (tcp->tcp_time_wait_expire == 0) 1539 tcp->tcp_time_wait_expire = 1; 1540 1541 ASSERT(TCP_IS_DETACHED(tcp)); 1542 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1543 ASSERT(tcp->tcp_time_wait_next == NULL); 1544 ASSERT(tcp->tcp_time_wait_prev == NULL); 1545 TCP_DBGSTAT(tcps, tcp_time_wait); 1546 1547 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1548 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1549 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1550 tcp_time_wait->tcp_time_wait_head = tcp; 1551 } else { 1552 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1553 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1554 TCPS_TIME_WAIT); 1555 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1556 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1557 } 1558 tcp_time_wait->tcp_time_wait_tail = tcp; 1559 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1560 } 1561 1562 /* ARGSUSED */ 1563 void 1564 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1565 { 1566 conn_t *connp = (conn_t *)arg; 1567 tcp_t *tcp = connp->conn_tcp; 1568 tcp_stack_t *tcps = tcp->tcp_tcps; 1569 1570 ASSERT(tcp != NULL); 1571 if (tcp->tcp_state == TCPS_CLOSED) { 1572 return; 1573 } 1574 1575 ASSERT((tcp->tcp_family == AF_INET && 1576 tcp->tcp_ipversion == IPV4_VERSION) || 1577 (tcp->tcp_family == AF_INET6 && 1578 (tcp->tcp_ipversion == IPV4_VERSION || 1579 tcp->tcp_ipversion == IPV6_VERSION))); 1580 ASSERT(!tcp->tcp_listener); 1581 1582 TCP_STAT(tcps, tcp_time_wait_reap); 1583 ASSERT(TCP_IS_DETACHED(tcp)); 1584 1585 /* 1586 * Because they have no upstream client to rebind or tcp_close() 1587 * them later, we axe the connection here and now. 1588 */ 1589 tcp_close_detached(tcp); 1590 } 1591 1592 /* 1593 * Remove cached/latched IPsec references. 1594 */ 1595 void 1596 tcp_ipsec_cleanup(tcp_t *tcp) 1597 { 1598 conn_t *connp = tcp->tcp_connp; 1599 1600 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1601 1602 if (connp->conn_latch != NULL) { 1603 IPLATCH_REFRELE(connp->conn_latch, 1604 connp->conn_netstack); 1605 connp->conn_latch = NULL; 1606 } 1607 if (connp->conn_policy != NULL) { 1608 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1609 connp->conn_policy = NULL; 1610 } 1611 } 1612 1613 /* 1614 * Cleaup before placing on free list. 1615 * Disassociate from the netstack/tcp_stack_t since the freelist 1616 * is per squeue and not per netstack. 1617 */ 1618 void 1619 tcp_cleanup(tcp_t *tcp) 1620 { 1621 mblk_t *mp; 1622 char *tcp_iphc; 1623 int tcp_iphc_len; 1624 int tcp_hdr_grown; 1625 tcp_sack_info_t *tcp_sack_info; 1626 conn_t *connp = tcp->tcp_connp; 1627 tcp_stack_t *tcps = tcp->tcp_tcps; 1628 netstack_t *ns = tcps->tcps_netstack; 1629 1630 tcp_bind_hash_remove(tcp); 1631 1632 /* Cleanup that which needs the netstack first */ 1633 tcp_ipsec_cleanup(tcp); 1634 1635 tcp_free(tcp); 1636 1637 /* Release any SSL context */ 1638 if (tcp->tcp_kssl_ent != NULL) { 1639 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1640 tcp->tcp_kssl_ent = NULL; 1641 } 1642 1643 if (tcp->tcp_kssl_ctx != NULL) { 1644 kssl_release_ctx(tcp->tcp_kssl_ctx); 1645 tcp->tcp_kssl_ctx = NULL; 1646 } 1647 tcp->tcp_kssl_pending = B_FALSE; 1648 1649 conn_delete_ire(connp, NULL); 1650 1651 /* 1652 * Since we will bzero the entire structure, we need to 1653 * remove it and reinsert it in global hash list. We 1654 * know the walkers can't get to this conn because we 1655 * had set CONDEMNED flag earlier and checked reference 1656 * under conn_lock so walker won't pick it and when we 1657 * go the ipcl_globalhash_remove() below, no walker 1658 * can get to it. 1659 */ 1660 ipcl_globalhash_remove(connp); 1661 1662 /* 1663 * Now it is safe to decrement the reference counts. 1664 * This might be the last reference on the netstack and TCPS 1665 * in which case it will cause the tcp_g_q_close and 1666 * the freeing of the IP Instance. 1667 */ 1668 connp->conn_netstack = NULL; 1669 netstack_rele(ns); 1670 ASSERT(tcps != NULL); 1671 tcp->tcp_tcps = NULL; 1672 TCPS_REFRELE(tcps); 1673 1674 /* Save some state */ 1675 mp = tcp->tcp_timercache; 1676 1677 tcp_sack_info = tcp->tcp_sack_info; 1678 tcp_iphc = tcp->tcp_iphc; 1679 tcp_iphc_len = tcp->tcp_iphc_len; 1680 tcp_hdr_grown = tcp->tcp_hdr_grown; 1681 1682 if (connp->conn_cred != NULL) { 1683 crfree(connp->conn_cred); 1684 connp->conn_cred = NULL; 1685 } 1686 if (connp->conn_peercred != NULL) { 1687 crfree(connp->conn_peercred); 1688 connp->conn_peercred = NULL; 1689 } 1690 ipcl_conn_cleanup(connp); 1691 connp->conn_flags = IPCL_TCPCONN; 1692 bzero(tcp, sizeof (tcp_t)); 1693 1694 /* restore the state */ 1695 tcp->tcp_timercache = mp; 1696 1697 tcp->tcp_sack_info = tcp_sack_info; 1698 tcp->tcp_iphc = tcp_iphc; 1699 tcp->tcp_iphc_len = tcp_iphc_len; 1700 tcp->tcp_hdr_grown = tcp_hdr_grown; 1701 1702 tcp->tcp_connp = connp; 1703 1704 ASSERT(connp->conn_tcp == tcp); 1705 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1706 connp->conn_state_flags = CONN_INCIPIENT; 1707 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1708 ASSERT(connp->conn_ref == 1); 1709 } 1710 1711 /* 1712 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1713 * is done forwards from the head. 1714 * This walks all stack instances since 1715 * tcp_time_wait remains global across all stacks. 1716 */ 1717 /* ARGSUSED */ 1718 void 1719 tcp_time_wait_collector(void *arg) 1720 { 1721 tcp_t *tcp; 1722 clock_t now; 1723 mblk_t *mp; 1724 conn_t *connp; 1725 kmutex_t *lock; 1726 boolean_t removed; 1727 1728 squeue_t *sqp = (squeue_t *)arg; 1729 tcp_squeue_priv_t *tcp_time_wait = 1730 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1731 1732 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1733 tcp_time_wait->tcp_time_wait_tid = 0; 1734 1735 if (tcp_time_wait->tcp_free_list != NULL && 1736 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1737 TCP_G_STAT(tcp_freelist_cleanup); 1738 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1739 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1740 tcp->tcp_time_wait_next = NULL; 1741 tcp_time_wait->tcp_free_list_cnt--; 1742 ASSERT(tcp->tcp_tcps == NULL); 1743 CONN_DEC_REF(tcp->tcp_connp); 1744 } 1745 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1746 } 1747 1748 /* 1749 * In order to reap time waits reliably, we should use a 1750 * source of time that is not adjustable by the user -- hence 1751 * the call to ddi_get_lbolt(). 1752 */ 1753 now = ddi_get_lbolt(); 1754 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1755 /* 1756 * Compare times using modular arithmetic, since 1757 * lbolt can wrapover. 1758 */ 1759 if ((now - tcp->tcp_time_wait_expire) < 0) { 1760 break; 1761 } 1762 1763 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1764 ASSERT(removed); 1765 1766 connp = tcp->tcp_connp; 1767 ASSERT(connp->conn_fanout != NULL); 1768 lock = &connp->conn_fanout->connf_lock; 1769 /* 1770 * This is essentially a TW reclaim fast path optimization for 1771 * performance where the timewait collector checks under the 1772 * fanout lock (so that no one else can get access to the 1773 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1774 * the classifier hash list. If ref count is indeed 2, we can 1775 * just remove the conn under the fanout lock and avoid 1776 * cleaning up the conn under the squeue, provided that 1777 * clustering callbacks are not enabled. If clustering is 1778 * enabled, we need to make the clustering callback before 1779 * setting the CONDEMNED flag and after dropping all locks and 1780 * so we forego this optimization and fall back to the slow 1781 * path. Also please see the comments in tcp_closei_local 1782 * regarding the refcnt logic. 1783 * 1784 * Since we are holding the tcp_time_wait_lock, its better 1785 * not to block on the fanout_lock because other connections 1786 * can't add themselves to time_wait list. So we do a 1787 * tryenter instead of mutex_enter. 1788 */ 1789 if (mutex_tryenter(lock)) { 1790 mutex_enter(&connp->conn_lock); 1791 if ((connp->conn_ref == 2) && 1792 (cl_inet_disconnect == NULL)) { 1793 ipcl_hash_remove_locked(connp, 1794 connp->conn_fanout); 1795 /* 1796 * Set the CONDEMNED flag now itself so that 1797 * the refcnt cannot increase due to any 1798 * walker. But we have still not cleaned up 1799 * conn_ire_cache. This is still ok since 1800 * we are going to clean it up in tcp_cleanup 1801 * immediately and any interface unplumb 1802 * thread will wait till the ire is blown away 1803 */ 1804 connp->conn_state_flags |= CONN_CONDEMNED; 1805 mutex_exit(lock); 1806 mutex_exit(&connp->conn_lock); 1807 if (tcp_time_wait->tcp_free_list_cnt < 1808 tcp_free_list_max_cnt) { 1809 /* Add to head of tcp_free_list */ 1810 mutex_exit( 1811 &tcp_time_wait->tcp_time_wait_lock); 1812 tcp_cleanup(tcp); 1813 ASSERT(connp->conn_latch == NULL); 1814 ASSERT(connp->conn_policy == NULL); 1815 ASSERT(tcp->tcp_tcps == NULL); 1816 ASSERT(connp->conn_netstack == NULL); 1817 1818 mutex_enter( 1819 &tcp_time_wait->tcp_time_wait_lock); 1820 tcp->tcp_time_wait_next = 1821 tcp_time_wait->tcp_free_list; 1822 tcp_time_wait->tcp_free_list = tcp; 1823 tcp_time_wait->tcp_free_list_cnt++; 1824 continue; 1825 } else { 1826 /* Do not add to tcp_free_list */ 1827 mutex_exit( 1828 &tcp_time_wait->tcp_time_wait_lock); 1829 tcp_bind_hash_remove(tcp); 1830 conn_delete_ire(tcp->tcp_connp, NULL); 1831 tcp_ipsec_cleanup(tcp); 1832 CONN_DEC_REF(tcp->tcp_connp); 1833 } 1834 } else { 1835 CONN_INC_REF_LOCKED(connp); 1836 mutex_exit(lock); 1837 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1838 mutex_exit(&connp->conn_lock); 1839 /* 1840 * We can reuse the closemp here since conn has 1841 * detached (otherwise we wouldn't even be in 1842 * time_wait list). tcp_closemp_used can safely 1843 * be changed without taking a lock as no other 1844 * thread can concurrently access it at this 1845 * point in the connection lifecycle. 1846 */ 1847 1848 if (tcp->tcp_closemp.b_prev == NULL) 1849 tcp->tcp_closemp_used = B_TRUE; 1850 else 1851 cmn_err(CE_PANIC, 1852 "tcp_timewait_collector: " 1853 "concurrent use of tcp_closemp: " 1854 "connp %p tcp %p\n", (void *)connp, 1855 (void *)tcp); 1856 1857 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1858 mp = &tcp->tcp_closemp; 1859 squeue_fill(connp->conn_sqp, mp, 1860 tcp_timewait_output, connp, 1861 SQTAG_TCP_TIMEWAIT); 1862 } 1863 } else { 1864 mutex_enter(&connp->conn_lock); 1865 CONN_INC_REF_LOCKED(connp); 1866 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1867 mutex_exit(&connp->conn_lock); 1868 /* 1869 * We can reuse the closemp here since conn has 1870 * detached (otherwise we wouldn't even be in 1871 * time_wait list). tcp_closemp_used can safely 1872 * be changed without taking a lock as no other 1873 * thread can concurrently access it at this 1874 * point in the connection lifecycle. 1875 */ 1876 1877 if (tcp->tcp_closemp.b_prev == NULL) 1878 tcp->tcp_closemp_used = B_TRUE; 1879 else 1880 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1881 "concurrent use of tcp_closemp: " 1882 "connp %p tcp %p\n", (void *)connp, 1883 (void *)tcp); 1884 1885 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1886 mp = &tcp->tcp_closemp; 1887 squeue_fill(connp->conn_sqp, mp, 1888 tcp_timewait_output, connp, 0); 1889 } 1890 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1891 } 1892 1893 if (tcp_time_wait->tcp_free_list != NULL) 1894 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1895 1896 tcp_time_wait->tcp_time_wait_tid = 1897 timeout(tcp_time_wait_collector, sqp, TCP_TIME_WAIT_DELAY); 1898 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1899 } 1900 /* 1901 * Reply to a clients T_CONN_RES TPI message. This function 1902 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1903 * on the acceptor STREAM and processed in tcp_wput_accept(). 1904 * Read the block comment on top of tcp_conn_request(). 1905 */ 1906 static void 1907 tcp_accept(tcp_t *listener, mblk_t *mp) 1908 { 1909 tcp_t *acceptor; 1910 tcp_t *eager; 1911 tcp_t *tcp; 1912 struct T_conn_res *tcr; 1913 t_uscalar_t acceptor_id; 1914 t_scalar_t seqnum; 1915 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1916 mblk_t *ok_mp; 1917 mblk_t *mp1; 1918 tcp_stack_t *tcps = listener->tcp_tcps; 1919 1920 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1921 tcp_err_ack(listener, mp, TPROTO, 0); 1922 return; 1923 } 1924 tcr = (struct T_conn_res *)mp->b_rptr; 1925 1926 /* 1927 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1928 * read side queue of the streams device underneath us i.e. the 1929 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1930 * look it up in the queue_hash. Under LP64 it sends down the 1931 * minor_t of the accepting endpoint. 1932 * 1933 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1934 * fanout hash lock is held. 1935 * This prevents any thread from entering the acceptor queue from 1936 * below (since it has not been hard bound yet i.e. any inbound 1937 * packets will arrive on the listener or default tcp queue and 1938 * go through tcp_lookup). 1939 * The CONN_INC_REF will prevent the acceptor from closing. 1940 * 1941 * XXX It is still possible for a tli application to send down data 1942 * on the accepting stream while another thread calls t_accept. 1943 * This should not be a problem for well-behaved applications since 1944 * the T_OK_ACK is sent after the queue swapping is completed. 1945 * 1946 * If the accepting fd is the same as the listening fd, avoid 1947 * queue hash lookup since that will return an eager listener in a 1948 * already established state. 1949 */ 1950 acceptor_id = tcr->ACCEPTOR_id; 1951 mutex_enter(&listener->tcp_eager_lock); 1952 if (listener->tcp_acceptor_id == acceptor_id) { 1953 eager = listener->tcp_eager_next_q; 1954 /* only count how many T_CONN_INDs so don't count q0 */ 1955 if ((listener->tcp_conn_req_cnt_q != 1) || 1956 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1957 mutex_exit(&listener->tcp_eager_lock); 1958 tcp_err_ack(listener, mp, TBADF, 0); 1959 return; 1960 } 1961 if (listener->tcp_conn_req_cnt_q0 != 0) { 1962 /* Throw away all the eagers on q0. */ 1963 tcp_eager_cleanup(listener, 1); 1964 } 1965 if (listener->tcp_syn_defense) { 1966 listener->tcp_syn_defense = B_FALSE; 1967 if (listener->tcp_ip_addr_cache != NULL) { 1968 kmem_free(listener->tcp_ip_addr_cache, 1969 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1970 listener->tcp_ip_addr_cache = NULL; 1971 } 1972 } 1973 /* 1974 * Transfer tcp_conn_req_max to the eager so that when 1975 * a disconnect occurs we can revert the endpoint to the 1976 * listen state. 1977 */ 1978 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1979 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1980 /* 1981 * Get a reference on the acceptor just like the 1982 * tcp_acceptor_hash_lookup below. 1983 */ 1984 acceptor = listener; 1985 CONN_INC_REF(acceptor->tcp_connp); 1986 } else { 1987 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1988 if (acceptor == NULL) { 1989 if (listener->tcp_debug) { 1990 (void) strlog(TCP_MOD_ID, 0, 1, 1991 SL_ERROR|SL_TRACE, 1992 "tcp_accept: did not find acceptor 0x%x\n", 1993 acceptor_id); 1994 } 1995 mutex_exit(&listener->tcp_eager_lock); 1996 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 1997 return; 1998 } 1999 /* 2000 * Verify acceptor state. The acceptable states for an acceptor 2001 * include TCPS_IDLE and TCPS_BOUND. 2002 */ 2003 switch (acceptor->tcp_state) { 2004 case TCPS_IDLE: 2005 /* FALLTHRU */ 2006 case TCPS_BOUND: 2007 break; 2008 default: 2009 CONN_DEC_REF(acceptor->tcp_connp); 2010 mutex_exit(&listener->tcp_eager_lock); 2011 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2012 return; 2013 } 2014 } 2015 2016 /* The listener must be in TCPS_LISTEN */ 2017 if (listener->tcp_state != TCPS_LISTEN) { 2018 CONN_DEC_REF(acceptor->tcp_connp); 2019 mutex_exit(&listener->tcp_eager_lock); 2020 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2021 return; 2022 } 2023 2024 /* 2025 * Rendezvous with an eager connection request packet hanging off 2026 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2027 * tcp structure when the connection packet arrived in 2028 * tcp_conn_request(). 2029 */ 2030 seqnum = tcr->SEQ_number; 2031 eager = listener; 2032 do { 2033 eager = eager->tcp_eager_next_q; 2034 if (eager == NULL) { 2035 CONN_DEC_REF(acceptor->tcp_connp); 2036 mutex_exit(&listener->tcp_eager_lock); 2037 tcp_err_ack(listener, mp, TBADSEQ, 0); 2038 return; 2039 } 2040 } while (eager->tcp_conn_req_seqnum != seqnum); 2041 mutex_exit(&listener->tcp_eager_lock); 2042 2043 /* 2044 * At this point, both acceptor and listener have 2 ref 2045 * that they begin with. Acceptor has one additional ref 2046 * we placed in lookup while listener has 3 additional 2047 * ref for being behind the squeue (tcp_accept() is 2048 * done on listener's squeue); being in classifier hash; 2049 * and eager's ref on listener. 2050 */ 2051 ASSERT(listener->tcp_connp->conn_ref >= 5); 2052 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2053 2054 /* 2055 * The eager at this point is set in its own squeue and 2056 * could easily have been killed (tcp_accept_finish will 2057 * deal with that) because of a TH_RST so we can only 2058 * ASSERT for a single ref. 2059 */ 2060 ASSERT(eager->tcp_connp->conn_ref >= 1); 2061 2062 /* Pre allocate the stroptions mblk also */ 2063 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2064 if (opt_mp == NULL) { 2065 CONN_DEC_REF(acceptor->tcp_connp); 2066 CONN_DEC_REF(eager->tcp_connp); 2067 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2068 return; 2069 } 2070 DB_TYPE(opt_mp) = M_SETOPTS; 2071 opt_mp->b_wptr += sizeof (struct stroptions); 2072 2073 /* 2074 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2075 * from listener to acceptor. The message is chained on opt_mp 2076 * which will be sent onto eager's squeue. 2077 */ 2078 if (listener->tcp_bound_if != 0) { 2079 /* allocate optmgmt req */ 2080 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2081 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2082 sizeof (int)); 2083 if (mp1 != NULL) 2084 linkb(opt_mp, mp1); 2085 } 2086 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2087 uint_t on = 1; 2088 2089 /* allocate optmgmt req */ 2090 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2091 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2092 if (mp1 != NULL) 2093 linkb(opt_mp, mp1); 2094 } 2095 2096 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2097 if ((mp1 = copymsg(mp)) == NULL) { 2098 CONN_DEC_REF(acceptor->tcp_connp); 2099 CONN_DEC_REF(eager->tcp_connp); 2100 freemsg(opt_mp); 2101 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2102 return; 2103 } 2104 2105 tcr = (struct T_conn_res *)mp1->b_rptr; 2106 2107 /* 2108 * This is an expanded version of mi_tpi_ok_ack_alloc() 2109 * which allocates a larger mblk and appends the new 2110 * local address to the ok_ack. The address is copied by 2111 * soaccept() for getsockname(). 2112 */ 2113 { 2114 int extra; 2115 2116 extra = (eager->tcp_family == AF_INET) ? 2117 sizeof (sin_t) : sizeof (sin6_t); 2118 2119 /* 2120 * Try to re-use mp, if possible. Otherwise, allocate 2121 * an mblk and return it as ok_mp. In any case, mp 2122 * is no longer usable upon return. 2123 */ 2124 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2125 CONN_DEC_REF(acceptor->tcp_connp); 2126 CONN_DEC_REF(eager->tcp_connp); 2127 freemsg(opt_mp); 2128 /* Original mp has been freed by now, so use mp1 */ 2129 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2130 return; 2131 } 2132 2133 mp = NULL; /* We should never use mp after this point */ 2134 2135 switch (extra) { 2136 case sizeof (sin_t): { 2137 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2138 2139 ok_mp->b_wptr += extra; 2140 sin->sin_family = AF_INET; 2141 sin->sin_port = eager->tcp_lport; 2142 sin->sin_addr.s_addr = 2143 eager->tcp_ipha->ipha_src; 2144 break; 2145 } 2146 case sizeof (sin6_t): { 2147 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2148 2149 ok_mp->b_wptr += extra; 2150 sin6->sin6_family = AF_INET6; 2151 sin6->sin6_port = eager->tcp_lport; 2152 if (eager->tcp_ipversion == IPV4_VERSION) { 2153 sin6->sin6_flowinfo = 0; 2154 IN6_IPADDR_TO_V4MAPPED( 2155 eager->tcp_ipha->ipha_src, 2156 &sin6->sin6_addr); 2157 } else { 2158 ASSERT(eager->tcp_ip6h != NULL); 2159 sin6->sin6_flowinfo = 2160 eager->tcp_ip6h->ip6_vcf & 2161 ~IPV6_VERS_AND_FLOW_MASK; 2162 sin6->sin6_addr = 2163 eager->tcp_ip6h->ip6_src; 2164 } 2165 sin6->sin6_scope_id = 0; 2166 sin6->__sin6_src_id = 0; 2167 break; 2168 } 2169 default: 2170 break; 2171 } 2172 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2173 } 2174 2175 /* 2176 * If there are no options we know that the T_CONN_RES will 2177 * succeed. However, we can't send the T_OK_ACK upstream until 2178 * the tcp_accept_swap is done since it would be dangerous to 2179 * let the application start using the new fd prior to the swap. 2180 */ 2181 tcp_accept_swap(listener, acceptor, eager); 2182 2183 /* 2184 * tcp_accept_swap unlinks eager from listener but does not drop 2185 * the eager's reference on the listener. 2186 */ 2187 ASSERT(eager->tcp_listener == NULL); 2188 ASSERT(listener->tcp_connp->conn_ref >= 5); 2189 2190 /* 2191 * The eager is now associated with its own queue. Insert in 2192 * the hash so that the connection can be reused for a future 2193 * T_CONN_RES. 2194 */ 2195 tcp_acceptor_hash_insert(acceptor_id, eager); 2196 2197 /* 2198 * We now do the processing of options with T_CONN_RES. 2199 * We delay till now since we wanted to have queue to pass to 2200 * option processing routines that points back to the right 2201 * instance structure which does not happen until after 2202 * tcp_accept_swap(). 2203 * 2204 * Note: 2205 * The sanity of the logic here assumes that whatever options 2206 * are appropriate to inherit from listner=>eager are done 2207 * before this point, and whatever were to be overridden (or not) 2208 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2209 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2210 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2211 * This may not be true at this point in time but can be fixed 2212 * independently. This option processing code starts with 2213 * the instantiated acceptor instance and the final queue at 2214 * this point. 2215 */ 2216 2217 if (tcr->OPT_length != 0) { 2218 /* Options to process */ 2219 int t_error = 0; 2220 int sys_error = 0; 2221 int do_disconnect = 0; 2222 2223 if (tcp_conprim_opt_process(eager, mp1, 2224 &do_disconnect, &t_error, &sys_error) < 0) { 2225 eager->tcp_accept_error = 1; 2226 if (do_disconnect) { 2227 /* 2228 * An option failed which does not allow 2229 * connection to be accepted. 2230 * 2231 * We allow T_CONN_RES to succeed and 2232 * put a T_DISCON_IND on the eager queue. 2233 */ 2234 ASSERT(t_error == 0 && sys_error == 0); 2235 eager->tcp_send_discon_ind = 1; 2236 } else { 2237 ASSERT(t_error != 0); 2238 freemsg(ok_mp); 2239 /* 2240 * Original mp was either freed or set 2241 * to ok_mp above, so use mp1 instead. 2242 */ 2243 tcp_err_ack(listener, mp1, t_error, sys_error); 2244 goto finish; 2245 } 2246 } 2247 /* 2248 * Most likely success in setting options (except if 2249 * eager->tcp_send_discon_ind set). 2250 * mp1 option buffer represented by OPT_length/offset 2251 * potentially modified and contains results of setting 2252 * options at this point 2253 */ 2254 } 2255 2256 /* We no longer need mp1, since all options processing has passed */ 2257 freemsg(mp1); 2258 2259 putnext(listener->tcp_rq, ok_mp); 2260 2261 mutex_enter(&listener->tcp_eager_lock); 2262 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2263 tcp_t *tail; 2264 mblk_t *conn_ind; 2265 2266 /* 2267 * This path should not be executed if listener and 2268 * acceptor streams are the same. 2269 */ 2270 ASSERT(listener != acceptor); 2271 2272 tcp = listener->tcp_eager_prev_q0; 2273 /* 2274 * listener->tcp_eager_prev_q0 points to the TAIL of the 2275 * deferred T_conn_ind queue. We need to get to the head of 2276 * the queue in order to send up T_conn_ind the same order as 2277 * how the 3WHS is completed. 2278 */ 2279 while (tcp != listener) { 2280 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2281 break; 2282 else 2283 tcp = tcp->tcp_eager_prev_q0; 2284 } 2285 ASSERT(tcp != listener); 2286 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2287 ASSERT(conn_ind != NULL); 2288 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2289 2290 /* Move from q0 to q */ 2291 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2292 listener->tcp_conn_req_cnt_q0--; 2293 listener->tcp_conn_req_cnt_q++; 2294 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2295 tcp->tcp_eager_prev_q0; 2296 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2297 tcp->tcp_eager_next_q0; 2298 tcp->tcp_eager_prev_q0 = NULL; 2299 tcp->tcp_eager_next_q0 = NULL; 2300 tcp->tcp_conn_def_q0 = B_FALSE; 2301 2302 /* Make sure the tcp isn't in the list of droppables */ 2303 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2304 tcp->tcp_eager_prev_drop_q0 == NULL); 2305 2306 /* 2307 * Insert at end of the queue because sockfs sends 2308 * down T_CONN_RES in chronological order. Leaving 2309 * the older conn indications at front of the queue 2310 * helps reducing search time. 2311 */ 2312 tail = listener->tcp_eager_last_q; 2313 if (tail != NULL) 2314 tail->tcp_eager_next_q = tcp; 2315 else 2316 listener->tcp_eager_next_q = tcp; 2317 listener->tcp_eager_last_q = tcp; 2318 tcp->tcp_eager_next_q = NULL; 2319 mutex_exit(&listener->tcp_eager_lock); 2320 putnext(tcp->tcp_rq, conn_ind); 2321 } else { 2322 mutex_exit(&listener->tcp_eager_lock); 2323 } 2324 2325 /* 2326 * Done with the acceptor - free it 2327 * 2328 * Note: from this point on, no access to listener should be made 2329 * as listener can be equal to acceptor. 2330 */ 2331 finish: 2332 ASSERT(acceptor->tcp_detached); 2333 ASSERT(tcps->tcps_g_q != NULL); 2334 acceptor->tcp_rq = tcps->tcps_g_q; 2335 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2336 (void) tcp_clean_death(acceptor, 0, 2); 2337 CONN_DEC_REF(acceptor->tcp_connp); 2338 2339 /* 2340 * In case we already received a FIN we have to make tcp_rput send 2341 * the ordrel_ind. This will also send up a window update if the window 2342 * has opened up. 2343 * 2344 * In the normal case of a successful connection acceptance 2345 * we give the O_T_BIND_REQ to the read side put procedure as an 2346 * indication that this was just accepted. This tells tcp_rput to 2347 * pass up any data queued in tcp_rcv_list. 2348 * 2349 * In the fringe case where options sent with T_CONN_RES failed and 2350 * we required, we would be indicating a T_DISCON_IND to blow 2351 * away this connection. 2352 */ 2353 2354 /* 2355 * XXX: we currently have a problem if XTI application closes the 2356 * acceptor stream in between. This problem exists in on10-gate also 2357 * and is well know but nothing can be done short of major rewrite 2358 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2359 * eager same squeue as listener (we can distinguish non socket 2360 * listeners at the time of handling a SYN in tcp_conn_request) 2361 * and do most of the work that tcp_accept_finish does here itself 2362 * and then get behind the acceptor squeue to access the acceptor 2363 * queue. 2364 */ 2365 /* 2366 * We already have a ref on tcp so no need to do one before squeue_fill 2367 */ 2368 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2369 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2370 } 2371 2372 /* 2373 * Swap information between the eager and acceptor for a TLI/XTI client. 2374 * The sockfs accept is done on the acceptor stream and control goes 2375 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2376 * called. In either case, both the eager and listener are in their own 2377 * perimeter (squeue) and the code has to deal with potential race. 2378 * 2379 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2380 */ 2381 static void 2382 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2383 { 2384 conn_t *econnp, *aconnp; 2385 2386 ASSERT(eager->tcp_rq == listener->tcp_rq); 2387 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2388 ASSERT(!eager->tcp_hard_bound); 2389 ASSERT(!TCP_IS_SOCKET(acceptor)); 2390 ASSERT(!TCP_IS_SOCKET(eager)); 2391 ASSERT(!TCP_IS_SOCKET(listener)); 2392 2393 acceptor->tcp_detached = B_TRUE; 2394 /* 2395 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2396 * the acceptor id. 2397 */ 2398 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2399 2400 /* remove eager from listen list... */ 2401 mutex_enter(&listener->tcp_eager_lock); 2402 tcp_eager_unlink(eager); 2403 ASSERT(eager->tcp_eager_next_q == NULL && 2404 eager->tcp_eager_last_q == NULL); 2405 ASSERT(eager->tcp_eager_next_q0 == NULL && 2406 eager->tcp_eager_prev_q0 == NULL); 2407 mutex_exit(&listener->tcp_eager_lock); 2408 eager->tcp_rq = acceptor->tcp_rq; 2409 eager->tcp_wq = acceptor->tcp_wq; 2410 2411 econnp = eager->tcp_connp; 2412 aconnp = acceptor->tcp_connp; 2413 2414 eager->tcp_rq->q_ptr = econnp; 2415 eager->tcp_wq->q_ptr = econnp; 2416 2417 /* 2418 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2419 * which might be a different squeue from our peer TCP instance. 2420 * For TCP Fusion, the peer expects that whenever tcp_detached is 2421 * clear, our TCP queues point to the acceptor's queues. Thus, use 2422 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2423 * above reach global visibility prior to the clearing of tcp_detached. 2424 */ 2425 membar_producer(); 2426 eager->tcp_detached = B_FALSE; 2427 2428 ASSERT(eager->tcp_ack_tid == 0); 2429 2430 econnp->conn_dev = aconnp->conn_dev; 2431 if (eager->tcp_cred != NULL) 2432 crfree(eager->tcp_cred); 2433 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2434 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2435 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2436 2437 aconnp->conn_cred = NULL; 2438 2439 econnp->conn_zoneid = aconnp->conn_zoneid; 2440 econnp->conn_allzones = aconnp->conn_allzones; 2441 2442 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2443 aconnp->conn_mac_exempt = B_FALSE; 2444 2445 ASSERT(aconnp->conn_peercred == NULL); 2446 2447 /* Do the IPC initialization */ 2448 CONN_INC_REF(econnp); 2449 2450 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2451 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2452 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2453 2454 /* Done with old IPC. Drop its ref on its connp */ 2455 CONN_DEC_REF(aconnp); 2456 } 2457 2458 2459 /* 2460 * Adapt to the information, such as rtt and rtt_sd, provided from the 2461 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2462 * 2463 * Checks for multicast and broadcast destination address. 2464 * Returns zero on failure; non-zero if ok. 2465 * 2466 * Note that the MSS calculation here is based on the info given in 2467 * the IRE. We do not do any calculation based on TCP options. They 2468 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2469 * knows which options to use. 2470 * 2471 * Note on how TCP gets its parameters for a connection. 2472 * 2473 * When a tcp_t structure is allocated, it gets all the default parameters. 2474 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2475 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2476 * default. But if there is an associated tcp_host_param, it will override 2477 * the metrics. 2478 * 2479 * An incoming SYN with a multicast or broadcast destination address, is dropped 2480 * in 1 of 2 places. 2481 * 2482 * 1. If the packet was received over the wire it is dropped in 2483 * ip_rput_process_broadcast() 2484 * 2485 * 2. If the packet was received through internal IP loopback, i.e. the packet 2486 * was generated and received on the same machine, it is dropped in 2487 * ip_wput_local() 2488 * 2489 * An incoming SYN with a multicast or broadcast source address is always 2490 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2491 * reject an attempt to connect to a broadcast or multicast (destination) 2492 * address. 2493 */ 2494 static int 2495 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2496 { 2497 tcp_hsp_t *hsp; 2498 ire_t *ire; 2499 ire_t *sire = NULL; 2500 iulp_t *ire_uinfo = NULL; 2501 uint32_t mss_max; 2502 uint32_t mss; 2503 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2504 conn_t *connp = tcp->tcp_connp; 2505 boolean_t ire_cacheable = B_FALSE; 2506 zoneid_t zoneid = connp->conn_zoneid; 2507 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2508 MATCH_IRE_SECATTR; 2509 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2510 ill_t *ill = NULL; 2511 boolean_t incoming = (ire_mp == NULL); 2512 tcp_stack_t *tcps = tcp->tcp_tcps; 2513 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2514 2515 ASSERT(connp->conn_ire_cache == NULL); 2516 2517 if (tcp->tcp_ipversion == IPV4_VERSION) { 2518 2519 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2520 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2521 return (0); 2522 } 2523 /* 2524 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2525 * for the destination with the nexthop as gateway. 2526 * ire_ctable_lookup() is used because this particular 2527 * ire, if it exists, will be marked private. 2528 * If that is not available, use the interface ire 2529 * for the nexthop. 2530 * 2531 * TSol: tcp_update_label will detect label mismatches based 2532 * only on the destination's label, but that would not 2533 * detect label mismatches based on the security attributes 2534 * of routes or next hop gateway. Hence we need to pass the 2535 * label to ire_ftable_lookup below in order to locate the 2536 * right prefix (and/or) ire cache. Similarly we also need 2537 * pass the label to the ire_cache_lookup below to locate 2538 * the right ire that also matches on the label. 2539 */ 2540 if (tcp->tcp_connp->conn_nexthop_set) { 2541 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2542 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2543 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2544 ipst); 2545 if (ire == NULL) { 2546 ire = ire_ftable_lookup( 2547 tcp->tcp_connp->conn_nexthop_v4, 2548 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2549 tsl, match_flags, ipst); 2550 if (ire == NULL) 2551 return (0); 2552 } else { 2553 ire_uinfo = &ire->ire_uinfo; 2554 } 2555 } else { 2556 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2557 zoneid, tsl, ipst); 2558 if (ire != NULL) { 2559 ire_cacheable = B_TRUE; 2560 ire_uinfo = (ire_mp != NULL) ? 2561 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2562 &ire->ire_uinfo; 2563 2564 } else { 2565 if (ire_mp == NULL) { 2566 ire = ire_ftable_lookup( 2567 tcp->tcp_connp->conn_rem, 2568 0, 0, 0, NULL, &sire, zoneid, 0, 2569 tsl, (MATCH_IRE_RECURSIVE | 2570 MATCH_IRE_DEFAULT), ipst); 2571 if (ire == NULL) 2572 return (0); 2573 ire_uinfo = (sire != NULL) ? 2574 &sire->ire_uinfo : 2575 &ire->ire_uinfo; 2576 } else { 2577 ire = (ire_t *)ire_mp->b_rptr; 2578 ire_uinfo = 2579 &((ire_t *) 2580 ire_mp->b_rptr)->ire_uinfo; 2581 } 2582 } 2583 } 2584 ASSERT(ire != NULL); 2585 2586 if ((ire->ire_src_addr == INADDR_ANY) || 2587 (ire->ire_type & IRE_BROADCAST)) { 2588 /* 2589 * ire->ire_mp is non null when ire_mp passed in is used 2590 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2591 */ 2592 if (ire->ire_mp == NULL) 2593 ire_refrele(ire); 2594 if (sire != NULL) 2595 ire_refrele(sire); 2596 return (0); 2597 } 2598 2599 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2600 ipaddr_t src_addr; 2601 2602 /* 2603 * ip_bind_connected() has stored the correct source 2604 * address in conn_src. 2605 */ 2606 src_addr = tcp->tcp_connp->conn_src; 2607 tcp->tcp_ipha->ipha_src = src_addr; 2608 /* 2609 * Copy of the src addr. in tcp_t is needed 2610 * for the lookup funcs. 2611 */ 2612 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2613 } 2614 /* 2615 * Set the fragment bit so that IP will tell us if the MTU 2616 * should change. IP tells us the latest setting of 2617 * ip_path_mtu_discovery through ire_frag_flag. 2618 */ 2619 if (ipst->ips_ip_path_mtu_discovery) { 2620 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2621 htons(IPH_DF); 2622 } 2623 /* 2624 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2625 * for IP_NEXTHOP. No cache ire has been found for the 2626 * destination and we are working with the nexthop's 2627 * interface ire. Since we need to forward all packets 2628 * to the nexthop first, we "blindly" set tcp_localnet 2629 * to false, eventhough the destination may also be 2630 * onlink. 2631 */ 2632 if (ire_uinfo == NULL) 2633 tcp->tcp_localnet = 0; 2634 else 2635 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2636 } else { 2637 /* 2638 * For incoming connection ire_mp = NULL 2639 * For outgoing connection ire_mp != NULL 2640 * Technically we should check conn_incoming_ill 2641 * when ire_mp is NULL and conn_outgoing_ill when 2642 * ire_mp is non-NULL. But this is performance 2643 * critical path and for IPV*_BOUND_IF, outgoing 2644 * and incoming ill are always set to the same value. 2645 */ 2646 ill_t *dst_ill = NULL; 2647 ipif_t *dst_ipif = NULL; 2648 2649 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2650 2651 if (connp->conn_outgoing_ill != NULL) { 2652 /* Outgoing or incoming path */ 2653 int err; 2654 2655 dst_ill = conn_get_held_ill(connp, 2656 &connp->conn_outgoing_ill, &err); 2657 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2658 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2659 return (0); 2660 } 2661 match_flags |= MATCH_IRE_ILL; 2662 dst_ipif = dst_ill->ill_ipif; 2663 } 2664 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2665 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2666 2667 if (ire != NULL) { 2668 ire_cacheable = B_TRUE; 2669 ire_uinfo = (ire_mp != NULL) ? 2670 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2671 &ire->ire_uinfo; 2672 } else { 2673 if (ire_mp == NULL) { 2674 ire = ire_ftable_lookup_v6( 2675 &tcp->tcp_connp->conn_remv6, 2676 0, 0, 0, dst_ipif, &sire, zoneid, 2677 0, tsl, match_flags, ipst); 2678 if (ire == NULL) { 2679 if (dst_ill != NULL) 2680 ill_refrele(dst_ill); 2681 return (0); 2682 } 2683 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2684 &ire->ire_uinfo; 2685 } else { 2686 ire = (ire_t *)ire_mp->b_rptr; 2687 ire_uinfo = 2688 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2689 } 2690 } 2691 if (dst_ill != NULL) 2692 ill_refrele(dst_ill); 2693 2694 ASSERT(ire != NULL); 2695 ASSERT(ire_uinfo != NULL); 2696 2697 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2698 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2699 /* 2700 * ire->ire_mp is non null when ire_mp passed in is used 2701 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2702 */ 2703 if (ire->ire_mp == NULL) 2704 ire_refrele(ire); 2705 if (sire != NULL) 2706 ire_refrele(sire); 2707 return (0); 2708 } 2709 2710 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2711 in6_addr_t src_addr; 2712 2713 /* 2714 * ip_bind_connected_v6() has stored the correct source 2715 * address per IPv6 addr. selection policy in 2716 * conn_src_v6. 2717 */ 2718 src_addr = tcp->tcp_connp->conn_srcv6; 2719 2720 tcp->tcp_ip6h->ip6_src = src_addr; 2721 /* 2722 * Copy of the src addr. in tcp_t is needed 2723 * for the lookup funcs. 2724 */ 2725 tcp->tcp_ip_src_v6 = src_addr; 2726 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2727 &connp->conn_srcv6)); 2728 } 2729 tcp->tcp_localnet = 2730 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2731 } 2732 2733 /* 2734 * This allows applications to fail quickly when connections are made 2735 * to dead hosts. Hosts can be labeled dead by adding a reject route 2736 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2737 */ 2738 if ((ire->ire_flags & RTF_REJECT) && 2739 (ire->ire_flags & RTF_PRIVATE)) 2740 goto error; 2741 2742 /* 2743 * Make use of the cached rtt and rtt_sd values to calculate the 2744 * initial RTO. Note that they are already initialized in 2745 * tcp_init_values(). 2746 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2747 * IP_NEXTHOP, but instead are using the interface ire for the 2748 * nexthop, then we do not use the ire_uinfo from that ire to 2749 * do any initializations. 2750 */ 2751 if (ire_uinfo != NULL) { 2752 if (ire_uinfo->iulp_rtt != 0) { 2753 clock_t rto; 2754 2755 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2756 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2757 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2758 tcps->tcps_rexmit_interval_extra + 2759 (tcp->tcp_rtt_sa >> 5); 2760 2761 if (rto > tcps->tcps_rexmit_interval_max) { 2762 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2763 } else if (rto < tcps->tcps_rexmit_interval_min) { 2764 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2765 } else { 2766 tcp->tcp_rto = rto; 2767 } 2768 } 2769 if (ire_uinfo->iulp_ssthresh != 0) 2770 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2771 else 2772 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2773 if (ire_uinfo->iulp_spipe > 0) { 2774 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2775 tcps->tcps_max_buf); 2776 if (tcps->tcps_snd_lowat_fraction != 0) 2777 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2778 tcps->tcps_snd_lowat_fraction; 2779 (void) tcp_maxpsz_set(tcp, B_TRUE); 2780 } 2781 /* 2782 * Note that up till now, acceptor always inherits receive 2783 * window from the listener. But if there is a metrics 2784 * associated with a host, we should use that instead of 2785 * inheriting it from listener. Thus we need to pass this 2786 * info back to the caller. 2787 */ 2788 if (ire_uinfo->iulp_rpipe > 0) { 2789 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2790 tcps->tcps_max_buf); 2791 } 2792 2793 if (ire_uinfo->iulp_rtomax > 0) { 2794 tcp->tcp_second_timer_threshold = 2795 ire_uinfo->iulp_rtomax; 2796 } 2797 2798 /* 2799 * Use the metric option settings, iulp_tstamp_ok and 2800 * iulp_wscale_ok, only for active open. What this means 2801 * is that if the other side uses timestamp or window 2802 * scale option, TCP will also use those options. That 2803 * is for passive open. If the application sets a 2804 * large window, window scale is enabled regardless of 2805 * the value in iulp_wscale_ok. This is the behavior 2806 * since 2.6. So we keep it. 2807 * The only case left in passive open processing is the 2808 * check for SACK. 2809 * For ECN, it should probably be like SACK. But the 2810 * current value is binary, so we treat it like the other 2811 * cases. The metric only controls active open.For passive 2812 * open, the ndd param, tcp_ecn_permitted, controls the 2813 * behavior. 2814 */ 2815 if (!tcp_detached) { 2816 /* 2817 * The if check means that the following can only 2818 * be turned on by the metrics only IRE, but not off. 2819 */ 2820 if (ire_uinfo->iulp_tstamp_ok) 2821 tcp->tcp_snd_ts_ok = B_TRUE; 2822 if (ire_uinfo->iulp_wscale_ok) 2823 tcp->tcp_snd_ws_ok = B_TRUE; 2824 if (ire_uinfo->iulp_sack == 2) 2825 tcp->tcp_snd_sack_ok = B_TRUE; 2826 if (ire_uinfo->iulp_ecn_ok) 2827 tcp->tcp_ecn_ok = B_TRUE; 2828 } else { 2829 /* 2830 * Passive open. 2831 * 2832 * As above, the if check means that SACK can only be 2833 * turned on by the metric only IRE. 2834 */ 2835 if (ire_uinfo->iulp_sack > 0) { 2836 tcp->tcp_snd_sack_ok = B_TRUE; 2837 } 2838 } 2839 } 2840 2841 2842 /* 2843 * XXX: Note that currently, ire_max_frag can be as small as 68 2844 * because of PMTUd. So tcp_mss may go to negative if combined 2845 * length of all those options exceeds 28 bytes. But because 2846 * of the tcp_mss_min check below, we may not have a problem if 2847 * tcp_mss_min is of a reasonable value. The default is 1 so 2848 * the negative problem still exists. And the check defeats PMTUd. 2849 * In fact, if PMTUd finds that the MSS should be smaller than 2850 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2851 * value. 2852 * 2853 * We do not deal with that now. All those problems related to 2854 * PMTUd will be fixed later. 2855 */ 2856 ASSERT(ire->ire_max_frag != 0); 2857 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2858 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2859 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2860 mss = MIN(mss, IPV6_MIN_MTU); 2861 } 2862 } 2863 2864 /* Sanity check for MSS value. */ 2865 if (tcp->tcp_ipversion == IPV4_VERSION) 2866 mss_max = tcps->tcps_mss_max_ipv4; 2867 else 2868 mss_max = tcps->tcps_mss_max_ipv6; 2869 2870 if (tcp->tcp_ipversion == IPV6_VERSION && 2871 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2872 /* 2873 * After receiving an ICMPv6 "packet too big" message with a 2874 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2875 * will insert a 8-byte fragment header in every packet; we 2876 * reduce the MSS by that amount here. 2877 */ 2878 mss -= sizeof (ip6_frag_t); 2879 } 2880 2881 if (tcp->tcp_ipsec_overhead == 0) 2882 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2883 2884 mss -= tcp->tcp_ipsec_overhead; 2885 2886 if (mss < tcps->tcps_mss_min) 2887 mss = tcps->tcps_mss_min; 2888 if (mss > mss_max) 2889 mss = mss_max; 2890 2891 /* Note that this is the maximum MSS, excluding all options. */ 2892 tcp->tcp_mss = mss; 2893 2894 /* 2895 * Initialize the ISS here now that we have the full connection ID. 2896 * The RFC 1948 method of initial sequence number generation requires 2897 * knowledge of the full connection ID before setting the ISS. 2898 */ 2899 2900 tcp_iss_init(tcp); 2901 2902 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2903 tcp->tcp_loopback = B_TRUE; 2904 2905 if (tcp->tcp_ipversion == IPV4_VERSION) { 2906 hsp = tcp_hsp_lookup(tcp->tcp_remote, tcps); 2907 } else { 2908 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6, tcps); 2909 } 2910 2911 if (hsp != NULL) { 2912 /* Only modify if we're going to make them bigger */ 2913 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2914 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2915 if (tcps->tcps_snd_lowat_fraction != 0) 2916 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2917 tcps->tcps_snd_lowat_fraction; 2918 } 2919 2920 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2921 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2922 } 2923 2924 /* Copy timestamp flag only for active open */ 2925 if (!tcp_detached) 2926 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2927 } 2928 2929 if (sire != NULL) 2930 IRE_REFRELE(sire); 2931 2932 /* 2933 * If we got an IRE_CACHE and an ILL, go through their properties; 2934 * otherwise, this is deferred until later when we have an IRE_CACHE. 2935 */ 2936 if (tcp->tcp_loopback || 2937 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2938 /* 2939 * For incoming, see if this tcp may be MDT-capable. For 2940 * outgoing, this process has been taken care of through 2941 * tcp_rput_other. 2942 */ 2943 tcp_ire_ill_check(tcp, ire, ill, incoming); 2944 tcp->tcp_ire_ill_check_done = B_TRUE; 2945 } 2946 2947 mutex_enter(&connp->conn_lock); 2948 /* 2949 * Make sure that conn is not marked incipient 2950 * for incoming connections. A blind 2951 * removal of incipient flag is cheaper than 2952 * check and removal. 2953 */ 2954 connp->conn_state_flags &= ~CONN_INCIPIENT; 2955 2956 /* 2957 * Must not cache forwarding table routes 2958 * or recache an IRE after the conn_t has 2959 * had conn_ire_cache cleared and is flagged 2960 * unusable, (see the CONN_CACHE_IRE() macro). 2961 */ 2962 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2963 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2964 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2965 connp->conn_ire_cache = ire; 2966 IRE_UNTRACE_REF(ire); 2967 rw_exit(&ire->ire_bucket->irb_lock); 2968 mutex_exit(&connp->conn_lock); 2969 return (1); 2970 } 2971 rw_exit(&ire->ire_bucket->irb_lock); 2972 } 2973 mutex_exit(&connp->conn_lock); 2974 2975 if (ire->ire_mp == NULL) 2976 ire_refrele(ire); 2977 return (1); 2978 2979 error: 2980 if (ire->ire_mp == NULL) 2981 ire_refrele(ire); 2982 if (sire != NULL) 2983 ire_refrele(sire); 2984 return (0); 2985 } 2986 2987 /* 2988 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 2989 * O_T_BIND_REQ/T_BIND_REQ message. 2990 */ 2991 static void 2992 tcp_bind(tcp_t *tcp, mblk_t *mp) 2993 { 2994 sin_t *sin; 2995 sin6_t *sin6; 2996 mblk_t *mp1; 2997 in_port_t requested_port; 2998 in_port_t allocated_port; 2999 struct T_bind_req *tbr; 3000 boolean_t bind_to_req_port_only; 3001 boolean_t backlog_update = B_FALSE; 3002 boolean_t user_specified; 3003 in6_addr_t v6addr; 3004 ipaddr_t v4addr; 3005 uint_t origipversion; 3006 int err; 3007 queue_t *q = tcp->tcp_wq; 3008 conn_t *connp = tcp->tcp_connp; 3009 mlp_type_t addrtype, mlptype; 3010 zone_t *zone; 3011 cred_t *cr; 3012 in_port_t mlp_port; 3013 tcp_stack_t *tcps = tcp->tcp_tcps; 3014 3015 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3016 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3017 if (tcp->tcp_debug) { 3018 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3019 "tcp_bind: bad req, len %u", 3020 (uint_t)(mp->b_wptr - mp->b_rptr)); 3021 } 3022 tcp_err_ack(tcp, mp, TPROTO, 0); 3023 return; 3024 } 3025 /* Make sure the largest address fits */ 3026 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3027 if (mp1 == NULL) { 3028 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3029 return; 3030 } 3031 mp = mp1; 3032 tbr = (struct T_bind_req *)mp->b_rptr; 3033 if (tcp->tcp_state >= TCPS_BOUND) { 3034 if ((tcp->tcp_state == TCPS_BOUND || 3035 tcp->tcp_state == TCPS_LISTEN) && 3036 tcp->tcp_conn_req_max != tbr->CONIND_number && 3037 tbr->CONIND_number > 0) { 3038 /* 3039 * Handle listen() increasing CONIND_number. 3040 * This is more "liberal" then what the TPI spec 3041 * requires but is needed to avoid a t_unbind 3042 * when handling listen() since the port number 3043 * might be "stolen" between the unbind and bind. 3044 */ 3045 backlog_update = B_TRUE; 3046 goto do_bind; 3047 } 3048 if (tcp->tcp_debug) { 3049 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3050 "tcp_bind: bad state, %d", tcp->tcp_state); 3051 } 3052 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3053 return; 3054 } 3055 origipversion = tcp->tcp_ipversion; 3056 3057 switch (tbr->ADDR_length) { 3058 case 0: /* request for a generic port */ 3059 tbr->ADDR_offset = sizeof (struct T_bind_req); 3060 if (tcp->tcp_family == AF_INET) { 3061 tbr->ADDR_length = sizeof (sin_t); 3062 sin = (sin_t *)&tbr[1]; 3063 *sin = sin_null; 3064 sin->sin_family = AF_INET; 3065 mp->b_wptr = (uchar_t *)&sin[1]; 3066 tcp->tcp_ipversion = IPV4_VERSION; 3067 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3068 } else { 3069 ASSERT(tcp->tcp_family == AF_INET6); 3070 tbr->ADDR_length = sizeof (sin6_t); 3071 sin6 = (sin6_t *)&tbr[1]; 3072 *sin6 = sin6_null; 3073 sin6->sin6_family = AF_INET6; 3074 mp->b_wptr = (uchar_t *)&sin6[1]; 3075 tcp->tcp_ipversion = IPV6_VERSION; 3076 V6_SET_ZERO(v6addr); 3077 } 3078 requested_port = 0; 3079 break; 3080 3081 case sizeof (sin_t): /* Complete IPv4 address */ 3082 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3083 sizeof (sin_t)); 3084 if (sin == NULL || !OK_32PTR((char *)sin)) { 3085 if (tcp->tcp_debug) { 3086 (void) strlog(TCP_MOD_ID, 0, 1, 3087 SL_ERROR|SL_TRACE, 3088 "tcp_bind: bad address parameter, " 3089 "offset %d, len %d", 3090 tbr->ADDR_offset, tbr->ADDR_length); 3091 } 3092 tcp_err_ack(tcp, mp, TPROTO, 0); 3093 return; 3094 } 3095 /* 3096 * With sockets sockfs will accept bogus sin_family in 3097 * bind() and replace it with the family used in the socket 3098 * call. 3099 */ 3100 if (sin->sin_family != AF_INET || 3101 tcp->tcp_family != AF_INET) { 3102 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3103 return; 3104 } 3105 requested_port = ntohs(sin->sin_port); 3106 tcp->tcp_ipversion = IPV4_VERSION; 3107 v4addr = sin->sin_addr.s_addr; 3108 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3109 break; 3110 3111 case sizeof (sin6_t): /* Complete IPv6 address */ 3112 sin6 = (sin6_t *)mi_offset_param(mp, 3113 tbr->ADDR_offset, sizeof (sin6_t)); 3114 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3115 if (tcp->tcp_debug) { 3116 (void) strlog(TCP_MOD_ID, 0, 1, 3117 SL_ERROR|SL_TRACE, 3118 "tcp_bind: bad IPv6 address parameter, " 3119 "offset %d, len %d", tbr->ADDR_offset, 3120 tbr->ADDR_length); 3121 } 3122 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3123 return; 3124 } 3125 if (sin6->sin6_family != AF_INET6 || 3126 tcp->tcp_family != AF_INET6) { 3127 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3128 return; 3129 } 3130 requested_port = ntohs(sin6->sin6_port); 3131 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3132 IPV4_VERSION : IPV6_VERSION; 3133 v6addr = sin6->sin6_addr; 3134 break; 3135 3136 default: 3137 if (tcp->tcp_debug) { 3138 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3139 "tcp_bind: bad address length, %d", 3140 tbr->ADDR_length); 3141 } 3142 tcp_err_ack(tcp, mp, TBADADDR, 0); 3143 return; 3144 } 3145 tcp->tcp_bound_source_v6 = v6addr; 3146 3147 /* Check for change in ipversion */ 3148 if (origipversion != tcp->tcp_ipversion) { 3149 ASSERT(tcp->tcp_family == AF_INET6); 3150 err = tcp->tcp_ipversion == IPV6_VERSION ? 3151 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3152 if (err) { 3153 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3154 return; 3155 } 3156 } 3157 3158 /* 3159 * Initialize family specific fields. Copy of the src addr. 3160 * in tcp_t is needed for the lookup funcs. 3161 */ 3162 if (tcp->tcp_ipversion == IPV6_VERSION) { 3163 tcp->tcp_ip6h->ip6_src = v6addr; 3164 } else { 3165 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3166 } 3167 tcp->tcp_ip_src_v6 = v6addr; 3168 3169 /* 3170 * For O_T_BIND_REQ: 3171 * Verify that the target port/addr is available, or choose 3172 * another. 3173 * For T_BIND_REQ: 3174 * Verify that the target port/addr is available or fail. 3175 * In both cases when it succeeds the tcp is inserted in the 3176 * bind hash table. This ensures that the operation is atomic 3177 * under the lock on the hash bucket. 3178 */ 3179 bind_to_req_port_only = requested_port != 0 && 3180 tbr->PRIM_type != O_T_BIND_REQ; 3181 /* 3182 * Get a valid port (within the anonymous range and should not 3183 * be a privileged one) to use if the user has not given a port. 3184 * If multiple threads are here, they may all start with 3185 * with the same initial port. But, it should be fine as long as 3186 * tcp_bindi will ensure that no two threads will be assigned 3187 * the same port. 3188 * 3189 * NOTE: XXX If a privileged process asks for an anonymous port, we 3190 * still check for ports only in the range > tcp_smallest_non_priv_port, 3191 * unless TCP_ANONPRIVBIND option is set. 3192 */ 3193 mlptype = mlptSingle; 3194 mlp_port = requested_port; 3195 if (requested_port == 0) { 3196 requested_port = tcp->tcp_anon_priv_bind ? 3197 tcp_get_next_priv_port(tcp) : 3198 tcp_update_next_port(tcps->tcps_next_port_to_try, 3199 tcp, B_TRUE); 3200 if (requested_port == 0) { 3201 tcp_err_ack(tcp, mp, TNOADDR, 0); 3202 return; 3203 } 3204 user_specified = B_FALSE; 3205 3206 /* 3207 * If the user went through one of the RPC interfaces to create 3208 * this socket and RPC is MLP in this zone, then give him an 3209 * anonymous MLP. 3210 */ 3211 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3212 if (connp->conn_anon_mlp && is_system_labeled()) { 3213 zone = crgetzone(cr); 3214 addrtype = tsol_mlp_addr_type(zone->zone_id, 3215 IPV6_VERSION, &v6addr, 3216 tcps->tcps_netstack->netstack_ip); 3217 if (addrtype == mlptSingle) { 3218 tcp_err_ack(tcp, mp, TNOADDR, 0); 3219 return; 3220 } 3221 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3222 PMAPPORT, addrtype); 3223 mlp_port = PMAPPORT; 3224 } 3225 } else { 3226 int i; 3227 boolean_t priv = B_FALSE; 3228 3229 /* 3230 * If the requested_port is in the well-known privileged range, 3231 * verify that the stream was opened by a privileged user. 3232 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3233 * but instead the code relies on: 3234 * - the fact that the address of the array and its size never 3235 * changes 3236 * - the atomic assignment of the elements of the array 3237 */ 3238 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3239 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 3240 priv = B_TRUE; 3241 } else { 3242 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 3243 if (requested_port == 3244 tcps->tcps_g_epriv_ports[i]) { 3245 priv = B_TRUE; 3246 break; 3247 } 3248 } 3249 } 3250 if (priv) { 3251 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3252 if (tcp->tcp_debug) { 3253 (void) strlog(TCP_MOD_ID, 0, 1, 3254 SL_ERROR|SL_TRACE, 3255 "tcp_bind: no priv for port %d", 3256 requested_port); 3257 } 3258 tcp_err_ack(tcp, mp, TACCES, 0); 3259 return; 3260 } 3261 } 3262 user_specified = B_TRUE; 3263 3264 if (is_system_labeled()) { 3265 zone = crgetzone(cr); 3266 addrtype = tsol_mlp_addr_type(zone->zone_id, 3267 IPV6_VERSION, &v6addr, 3268 tcps->tcps_netstack->netstack_ip); 3269 if (addrtype == mlptSingle) { 3270 tcp_err_ack(tcp, mp, TNOADDR, 0); 3271 return; 3272 } 3273 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3274 requested_port, addrtype); 3275 } 3276 } 3277 3278 if (mlptype != mlptSingle) { 3279 if (secpolicy_net_bindmlp(cr) != 0) { 3280 if (tcp->tcp_debug) { 3281 (void) strlog(TCP_MOD_ID, 0, 1, 3282 SL_ERROR|SL_TRACE, 3283 "tcp_bind: no priv for multilevel port %d", 3284 requested_port); 3285 } 3286 tcp_err_ack(tcp, mp, TACCES, 0); 3287 return; 3288 } 3289 3290 /* 3291 * If we're specifically binding a shared IP address and the 3292 * port is MLP on shared addresses, then check to see if this 3293 * zone actually owns the MLP. Reject if not. 3294 */ 3295 if (mlptype == mlptShared && addrtype == mlptShared) { 3296 /* 3297 * No need to handle exclusive-stack zones since 3298 * ALL_ZONES only applies to the shared stack. 3299 */ 3300 zoneid_t mlpzone; 3301 3302 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3303 htons(mlp_port)); 3304 if (connp->conn_zoneid != mlpzone) { 3305 if (tcp->tcp_debug) { 3306 (void) strlog(TCP_MOD_ID, 0, 1, 3307 SL_ERROR|SL_TRACE, 3308 "tcp_bind: attempt to bind port " 3309 "%d on shared addr in zone %d " 3310 "(should be %d)", 3311 mlp_port, connp->conn_zoneid, 3312 mlpzone); 3313 } 3314 tcp_err_ack(tcp, mp, TACCES, 0); 3315 return; 3316 } 3317 } 3318 3319 if (!user_specified) { 3320 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3321 requested_port, B_TRUE); 3322 if (err != 0) { 3323 if (tcp->tcp_debug) { 3324 (void) strlog(TCP_MOD_ID, 0, 1, 3325 SL_ERROR|SL_TRACE, 3326 "tcp_bind: cannot establish anon " 3327 "MLP for port %d", 3328 requested_port); 3329 } 3330 tcp_err_ack(tcp, mp, TSYSERR, err); 3331 return; 3332 } 3333 connp->conn_anon_port = B_TRUE; 3334 } 3335 connp->conn_mlp_type = mlptype; 3336 } 3337 3338 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3339 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3340 3341 if (allocated_port == 0) { 3342 connp->conn_mlp_type = mlptSingle; 3343 if (connp->conn_anon_port) { 3344 connp->conn_anon_port = B_FALSE; 3345 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3346 requested_port, B_FALSE); 3347 } 3348 if (bind_to_req_port_only) { 3349 if (tcp->tcp_debug) { 3350 (void) strlog(TCP_MOD_ID, 0, 1, 3351 SL_ERROR|SL_TRACE, 3352 "tcp_bind: requested addr busy"); 3353 } 3354 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3355 } else { 3356 /* If we are out of ports, fail the bind. */ 3357 if (tcp->tcp_debug) { 3358 (void) strlog(TCP_MOD_ID, 0, 1, 3359 SL_ERROR|SL_TRACE, 3360 "tcp_bind: out of ports?"); 3361 } 3362 tcp_err_ack(tcp, mp, TNOADDR, 0); 3363 } 3364 return; 3365 } 3366 ASSERT(tcp->tcp_state == TCPS_BOUND); 3367 do_bind: 3368 if (!backlog_update) { 3369 if (tcp->tcp_family == AF_INET) 3370 sin->sin_port = htons(allocated_port); 3371 else 3372 sin6->sin6_port = htons(allocated_port); 3373 } 3374 if (tcp->tcp_family == AF_INET) { 3375 if (tbr->CONIND_number != 0) { 3376 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3377 sizeof (sin_t)); 3378 } else { 3379 /* Just verify the local IP address */ 3380 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3381 } 3382 } else { 3383 if (tbr->CONIND_number != 0) { 3384 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3385 sizeof (sin6_t)); 3386 } else { 3387 /* Just verify the local IP address */ 3388 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3389 IPV6_ADDR_LEN); 3390 } 3391 } 3392 if (mp1 == NULL) { 3393 if (connp->conn_anon_port) { 3394 connp->conn_anon_port = B_FALSE; 3395 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3396 requested_port, B_FALSE); 3397 } 3398 connp->conn_mlp_type = mlptSingle; 3399 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3400 return; 3401 } 3402 3403 tbr->PRIM_type = T_BIND_ACK; 3404 mp->b_datap->db_type = M_PCPROTO; 3405 3406 /* Chain in the reply mp for tcp_rput() */ 3407 mp1->b_cont = mp; 3408 mp = mp1; 3409 3410 tcp->tcp_conn_req_max = tbr->CONIND_number; 3411 if (tcp->tcp_conn_req_max) { 3412 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 3413 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 3414 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 3415 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 3416 /* 3417 * If this is a listener, do not reset the eager list 3418 * and other stuffs. Note that we don't check if the 3419 * existing eager list meets the new tcp_conn_req_max 3420 * requirement. 3421 */ 3422 if (tcp->tcp_state != TCPS_LISTEN) { 3423 tcp->tcp_state = TCPS_LISTEN; 3424 /* Initialize the chain. Don't need the eager_lock */ 3425 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3426 tcp->tcp_eager_next_drop_q0 = tcp; 3427 tcp->tcp_eager_prev_drop_q0 = tcp; 3428 tcp->tcp_second_ctimer_threshold = 3429 tcps->tcps_ip_abort_linterval; 3430 } 3431 } 3432 3433 /* 3434 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3435 * processing continues in tcp_rput_other(). 3436 * 3437 * We need to make sure that the conn_recv is set to a non-null 3438 * value before we insert the conn into the classifier table. 3439 * This is to avoid a race with an incoming packet which does an 3440 * ipcl_classify(). 3441 */ 3442 connp->conn_recv = tcp_conn_request; 3443 if (tcp->tcp_family == AF_INET6) { 3444 ASSERT(tcp->tcp_connp->conn_af_isv6); 3445 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3446 } else { 3447 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3448 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3449 } 3450 /* 3451 * If the bind cannot complete immediately 3452 * IP will arrange to call tcp_rput_other 3453 * when the bind completes. 3454 */ 3455 if (mp != NULL) { 3456 tcp_rput_other(tcp, mp); 3457 } else { 3458 /* 3459 * Bind will be resumed later. Need to ensure 3460 * that conn doesn't disappear when that happens. 3461 * This will be decremented in ip_resume_tcp_bind(). 3462 */ 3463 CONN_INC_REF(tcp->tcp_connp); 3464 } 3465 } 3466 3467 3468 /* 3469 * If the "bind_to_req_port_only" parameter is set, if the requested port 3470 * number is available, return it, If not return 0 3471 * 3472 * If "bind_to_req_port_only" parameter is not set and 3473 * If the requested port number is available, return it. If not, return 3474 * the first anonymous port we happen across. If no anonymous ports are 3475 * available, return 0. addr is the requested local address, if any. 3476 * 3477 * In either case, when succeeding update the tcp_t to record the port number 3478 * and insert it in the bind hash table. 3479 * 3480 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3481 * without setting SO_REUSEADDR. This is needed so that they 3482 * can be viewed as two independent transport protocols. 3483 */ 3484 static in_port_t 3485 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3486 int reuseaddr, boolean_t quick_connect, 3487 boolean_t bind_to_req_port_only, boolean_t user_specified) 3488 { 3489 /* number of times we have run around the loop */ 3490 int count = 0; 3491 /* maximum number of times to run around the loop */ 3492 int loopmax; 3493 conn_t *connp = tcp->tcp_connp; 3494 zoneid_t zoneid = connp->conn_zoneid; 3495 tcp_stack_t *tcps = tcp->tcp_tcps; 3496 3497 /* 3498 * Lookup for free addresses is done in a loop and "loopmax" 3499 * influences how long we spin in the loop 3500 */ 3501 if (bind_to_req_port_only) { 3502 /* 3503 * If the requested port is busy, don't bother to look 3504 * for a new one. Setting loop maximum count to 1 has 3505 * that effect. 3506 */ 3507 loopmax = 1; 3508 } else { 3509 /* 3510 * If the requested port is busy, look for a free one 3511 * in the anonymous port range. 3512 * Set loopmax appropriately so that one does not look 3513 * forever in the case all of the anonymous ports are in use. 3514 */ 3515 if (tcp->tcp_anon_priv_bind) { 3516 /* 3517 * loopmax = 3518 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3519 */ 3520 loopmax = IPPORT_RESERVED - 3521 tcps->tcps_min_anonpriv_port; 3522 } else { 3523 loopmax = (tcps->tcps_largest_anon_port - 3524 tcps->tcps_smallest_anon_port + 1); 3525 } 3526 } 3527 do { 3528 uint16_t lport; 3529 tf_t *tbf; 3530 tcp_t *ltcp; 3531 conn_t *lconnp; 3532 3533 lport = htons(port); 3534 3535 /* 3536 * Ensure that the tcp_t is not currently in the bind hash. 3537 * Hold the lock on the hash bucket to ensure that 3538 * the duplicate check plus the insertion is an atomic 3539 * operation. 3540 * 3541 * This function does an inline lookup on the bind hash list 3542 * Make sure that we access only members of tcp_t 3543 * and that we don't look at tcp_tcp, since we are not 3544 * doing a CONN_INC_REF. 3545 */ 3546 tcp_bind_hash_remove(tcp); 3547 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3548 mutex_enter(&tbf->tf_lock); 3549 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3550 ltcp = ltcp->tcp_bind_hash) { 3551 boolean_t not_socket; 3552 boolean_t exclbind; 3553 3554 if (lport != ltcp->tcp_lport) 3555 continue; 3556 3557 lconnp = ltcp->tcp_connp; 3558 3559 /* 3560 * On a labeled system, we must treat bindings to ports 3561 * on shared IP addresses by sockets with MAC exemption 3562 * privilege as being in all zones, as there's 3563 * otherwise no way to identify the right receiver. 3564 */ 3565 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3566 IPCL_ZONE_MATCH(connp, 3567 ltcp->tcp_connp->conn_zoneid)) && 3568 !lconnp->conn_mac_exempt && 3569 !connp->conn_mac_exempt) 3570 continue; 3571 3572 /* 3573 * If TCP_EXCLBIND is set for either the bound or 3574 * binding endpoint, the semantics of bind 3575 * is changed according to the following. 3576 * 3577 * spec = specified address (v4 or v6) 3578 * unspec = unspecified address (v4 or v6) 3579 * A = specified addresses are different for endpoints 3580 * 3581 * bound bind to allowed 3582 * ------------------------------------- 3583 * unspec unspec no 3584 * unspec spec no 3585 * spec unspec no 3586 * spec spec yes if A 3587 * 3588 * For labeled systems, SO_MAC_EXEMPT behaves the same 3589 * as TCP_EXCLBIND, except that zoneid is ignored. 3590 * 3591 * Note: 3592 * 3593 * 1. Because of TLI semantics, an endpoint can go 3594 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3595 * TCPS_BOUND, depending on whether it is originally 3596 * a listener or not. That is why we need to check 3597 * for states greater than or equal to TCPS_BOUND 3598 * here. 3599 * 3600 * 2. Ideally, we should only check for state equals 3601 * to TCPS_LISTEN. And the following check should be 3602 * added. 3603 * 3604 * if (ltcp->tcp_state == TCPS_LISTEN || 3605 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3606 * ... 3607 * } 3608 * 3609 * The semantics will be changed to this. If the 3610 * endpoint on the list is in state not equal to 3611 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3612 * set, let the bind succeed. 3613 * 3614 * Because of (1), we cannot do that for TLI 3615 * endpoints. But we can do that for socket endpoints. 3616 * If in future, we can change this going back 3617 * semantics, we can use the above check for TLI also. 3618 */ 3619 not_socket = !(TCP_IS_SOCKET(ltcp) && 3620 TCP_IS_SOCKET(tcp)); 3621 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3622 3623 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3624 (exclbind && (not_socket || 3625 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3626 if (V6_OR_V4_INADDR_ANY( 3627 ltcp->tcp_bound_source_v6) || 3628 V6_OR_V4_INADDR_ANY(*laddr) || 3629 IN6_ARE_ADDR_EQUAL(laddr, 3630 <cp->tcp_bound_source_v6)) { 3631 break; 3632 } 3633 continue; 3634 } 3635 3636 /* 3637 * Check ipversion to allow IPv4 and IPv6 sockets to 3638 * have disjoint port number spaces, if *_EXCLBIND 3639 * is not set and only if the application binds to a 3640 * specific port. We use the same autoassigned port 3641 * number space for IPv4 and IPv6 sockets. 3642 */ 3643 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3644 bind_to_req_port_only) 3645 continue; 3646 3647 /* 3648 * Ideally, we should make sure that the source 3649 * address, remote address, and remote port in the 3650 * four tuple for this tcp-connection is unique. 3651 * However, trying to find out the local source 3652 * address would require too much code duplication 3653 * with IP, since IP needs needs to have that code 3654 * to support userland TCP implementations. 3655 */ 3656 if (quick_connect && 3657 (ltcp->tcp_state > TCPS_LISTEN) && 3658 ((tcp->tcp_fport != ltcp->tcp_fport) || 3659 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3660 <cp->tcp_remote_v6))) 3661 continue; 3662 3663 if (!reuseaddr) { 3664 /* 3665 * No socket option SO_REUSEADDR. 3666 * If existing port is bound to 3667 * a non-wildcard IP address 3668 * and the requesting stream is 3669 * bound to a distinct 3670 * different IP addresses 3671 * (non-wildcard, also), keep 3672 * going. 3673 */ 3674 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3675 !V6_OR_V4_INADDR_ANY( 3676 ltcp->tcp_bound_source_v6) && 3677 !IN6_ARE_ADDR_EQUAL(laddr, 3678 <cp->tcp_bound_source_v6)) 3679 continue; 3680 if (ltcp->tcp_state >= TCPS_BOUND) { 3681 /* 3682 * This port is being used and 3683 * its state is >= TCPS_BOUND, 3684 * so we can't bind to it. 3685 */ 3686 break; 3687 } 3688 } else { 3689 /* 3690 * socket option SO_REUSEADDR is set on the 3691 * binding tcp_t. 3692 * 3693 * If two streams are bound to 3694 * same IP address or both addr 3695 * and bound source are wildcards 3696 * (INADDR_ANY), we want to stop 3697 * searching. 3698 * We have found a match of IP source 3699 * address and source port, which is 3700 * refused regardless of the 3701 * SO_REUSEADDR setting, so we break. 3702 */ 3703 if (IN6_ARE_ADDR_EQUAL(laddr, 3704 <cp->tcp_bound_source_v6) && 3705 (ltcp->tcp_state == TCPS_LISTEN || 3706 ltcp->tcp_state == TCPS_BOUND)) 3707 break; 3708 } 3709 } 3710 if (ltcp != NULL) { 3711 /* The port number is busy */ 3712 mutex_exit(&tbf->tf_lock); 3713 } else { 3714 /* 3715 * This port is ours. Insert in fanout and mark as 3716 * bound to prevent others from getting the port 3717 * number. 3718 */ 3719 tcp->tcp_state = TCPS_BOUND; 3720 tcp->tcp_lport = htons(port); 3721 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3722 3723 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3724 tcp->tcp_lport)] == tbf); 3725 tcp_bind_hash_insert(tbf, tcp, 1); 3726 3727 mutex_exit(&tbf->tf_lock); 3728 3729 /* 3730 * We don't want tcp_next_port_to_try to "inherit" 3731 * a port number supplied by the user in a bind. 3732 */ 3733 if (user_specified) 3734 return (port); 3735 3736 /* 3737 * This is the only place where tcp_next_port_to_try 3738 * is updated. After the update, it may or may not 3739 * be in the valid range. 3740 */ 3741 if (!tcp->tcp_anon_priv_bind) 3742 tcps->tcps_next_port_to_try = port + 1; 3743 return (port); 3744 } 3745 3746 if (tcp->tcp_anon_priv_bind) { 3747 port = tcp_get_next_priv_port(tcp); 3748 } else { 3749 if (count == 0 && user_specified) { 3750 /* 3751 * We may have to return an anonymous port. So 3752 * get one to start with. 3753 */ 3754 port = 3755 tcp_update_next_port( 3756 tcps->tcps_next_port_to_try, 3757 tcp, B_TRUE); 3758 user_specified = B_FALSE; 3759 } else { 3760 port = tcp_update_next_port(port + 1, tcp, 3761 B_FALSE); 3762 } 3763 } 3764 if (port == 0) 3765 break; 3766 3767 /* 3768 * Don't let this loop run forever in the case where 3769 * all of the anonymous ports are in use. 3770 */ 3771 } while (++count < loopmax); 3772 return (0); 3773 } 3774 3775 /* 3776 * tcp_clean_death / tcp_close_detached must not be called more than once 3777 * on a tcp. Thus every function that potentially calls tcp_clean_death 3778 * must check for the tcp state before calling tcp_clean_death. 3779 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3780 * tcp_timer_handler, all check for the tcp state. 3781 */ 3782 /* ARGSUSED */ 3783 void 3784 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3785 { 3786 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3787 3788 freemsg(mp); 3789 if (tcp->tcp_state > TCPS_BOUND) 3790 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3791 ETIMEDOUT, 5); 3792 } 3793 3794 /* 3795 * We are dying for some reason. Try to do it gracefully. (May be called 3796 * as writer.) 3797 * 3798 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3799 * done by a service procedure). 3800 * TBD - Should the return value distinguish between the tcp_t being 3801 * freed and it being reinitialized? 3802 */ 3803 static int 3804 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3805 { 3806 mblk_t *mp; 3807 queue_t *q; 3808 tcp_stack_t *tcps = tcp->tcp_tcps; 3809 3810 TCP_CLD_STAT(tag); 3811 3812 #if TCP_TAG_CLEAN_DEATH 3813 tcp->tcp_cleandeathtag = tag; 3814 #endif 3815 3816 if (tcp->tcp_fused) 3817 tcp_unfuse(tcp); 3818 3819 if (tcp->tcp_linger_tid != 0 && 3820 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3821 tcp_stop_lingering(tcp); 3822 } 3823 3824 ASSERT(tcp != NULL); 3825 ASSERT((tcp->tcp_family == AF_INET && 3826 tcp->tcp_ipversion == IPV4_VERSION) || 3827 (tcp->tcp_family == AF_INET6 && 3828 (tcp->tcp_ipversion == IPV4_VERSION || 3829 tcp->tcp_ipversion == IPV6_VERSION))); 3830 3831 if (TCP_IS_DETACHED(tcp)) { 3832 if (tcp->tcp_hard_binding) { 3833 /* 3834 * Its an eager that we are dealing with. We close the 3835 * eager but in case a conn_ind has already gone to the 3836 * listener, let tcp_accept_finish() send a discon_ind 3837 * to the listener and drop the last reference. If the 3838 * listener doesn't even know about the eager i.e. the 3839 * conn_ind hasn't gone up, blow away the eager and drop 3840 * the last reference as well. If the conn_ind has gone 3841 * up, state should be BOUND. tcp_accept_finish 3842 * will figure out that the connection has received a 3843 * RST and will send a DISCON_IND to the application. 3844 */ 3845 tcp_closei_local(tcp); 3846 if (!tcp->tcp_tconnind_started) { 3847 CONN_DEC_REF(tcp->tcp_connp); 3848 } else { 3849 tcp->tcp_state = TCPS_BOUND; 3850 } 3851 } else { 3852 tcp_close_detached(tcp); 3853 } 3854 return (0); 3855 } 3856 3857 TCP_STAT(tcps, tcp_clean_death_nondetached); 3858 3859 /* 3860 * If T_ORDREL_IND has not been sent yet (done when service routine 3861 * is run) postpone cleaning up the endpoint until service routine 3862 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3863 * client_errno since tcp_close uses the client_errno field. 3864 */ 3865 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3866 if (err != 0) 3867 tcp->tcp_client_errno = err; 3868 3869 tcp->tcp_deferred_clean_death = B_TRUE; 3870 return (-1); 3871 } 3872 3873 q = tcp->tcp_rq; 3874 3875 /* Trash all inbound data */ 3876 flushq(q, FLUSHALL); 3877 3878 /* 3879 * If we are at least part way open and there is error 3880 * (err==0 implies no error) 3881 * notify our client by a T_DISCON_IND. 3882 */ 3883 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3884 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3885 !TCP_IS_SOCKET(tcp)) { 3886 /* 3887 * Send M_FLUSH according to TPI. Because sockets will 3888 * (and must) ignore FLUSHR we do that only for TPI 3889 * endpoints and sockets in STREAMS mode. 3890 */ 3891 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3892 } 3893 if (tcp->tcp_debug) { 3894 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3895 "tcp_clean_death: discon err %d", err); 3896 } 3897 mp = mi_tpi_discon_ind(NULL, err, 0); 3898 if (mp != NULL) { 3899 putnext(q, mp); 3900 } else { 3901 if (tcp->tcp_debug) { 3902 (void) strlog(TCP_MOD_ID, 0, 1, 3903 SL_ERROR|SL_TRACE, 3904 "tcp_clean_death, sending M_ERROR"); 3905 } 3906 (void) putnextctl1(q, M_ERROR, EPROTO); 3907 } 3908 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3909 /* SYN_SENT or SYN_RCVD */ 3910 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3911 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3912 /* ESTABLISHED or CLOSE_WAIT */ 3913 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3914 } 3915 } 3916 3917 tcp_reinit(tcp); 3918 return (-1); 3919 } 3920 3921 /* 3922 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3923 * to expire, stop the wait and finish the close. 3924 */ 3925 static void 3926 tcp_stop_lingering(tcp_t *tcp) 3927 { 3928 clock_t delta = 0; 3929 tcp_stack_t *tcps = tcp->tcp_tcps; 3930 3931 tcp->tcp_linger_tid = 0; 3932 if (tcp->tcp_state > TCPS_LISTEN) { 3933 tcp_acceptor_hash_remove(tcp); 3934 mutex_enter(&tcp->tcp_non_sq_lock); 3935 if (tcp->tcp_flow_stopped) { 3936 tcp_clrqfull(tcp); 3937 } 3938 mutex_exit(&tcp->tcp_non_sq_lock); 3939 3940 if (tcp->tcp_timer_tid != 0) { 3941 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3942 tcp->tcp_timer_tid = 0; 3943 } 3944 /* 3945 * Need to cancel those timers which will not be used when 3946 * TCP is detached. This has to be done before the tcp_wq 3947 * is set to the global queue. 3948 */ 3949 tcp_timers_stop(tcp); 3950 3951 3952 tcp->tcp_detached = B_TRUE; 3953 ASSERT(tcps->tcps_g_q != NULL); 3954 tcp->tcp_rq = tcps->tcps_g_q; 3955 tcp->tcp_wq = WR(tcps->tcps_g_q); 3956 3957 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3958 tcp_time_wait_append(tcp); 3959 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3960 goto finish; 3961 } 3962 3963 /* 3964 * If delta is zero the timer event wasn't executed and was 3965 * successfully canceled. In this case we need to restart it 3966 * with the minimal delta possible. 3967 */ 3968 if (delta >= 0) { 3969 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3970 delta ? delta : 1); 3971 } 3972 } else { 3973 tcp_closei_local(tcp); 3974 CONN_DEC_REF(tcp->tcp_connp); 3975 } 3976 finish: 3977 /* Signal closing thread that it can complete close */ 3978 mutex_enter(&tcp->tcp_closelock); 3979 tcp->tcp_detached = B_TRUE; 3980 ASSERT(tcps->tcps_g_q != NULL); 3981 tcp->tcp_rq = tcps->tcps_g_q; 3982 tcp->tcp_wq = WR(tcps->tcps_g_q); 3983 tcp->tcp_closed = 1; 3984 cv_signal(&tcp->tcp_closecv); 3985 mutex_exit(&tcp->tcp_closelock); 3986 } 3987 3988 /* 3989 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3990 * expires. 3991 */ 3992 static void 3993 tcp_close_linger_timeout(void *arg) 3994 { 3995 conn_t *connp = (conn_t *)arg; 3996 tcp_t *tcp = connp->conn_tcp; 3997 3998 tcp->tcp_client_errno = ETIMEDOUT; 3999 tcp_stop_lingering(tcp); 4000 } 4001 4002 static int 4003 tcp_close(queue_t *q, int flags) 4004 { 4005 conn_t *connp = Q_TO_CONN(q); 4006 tcp_t *tcp = connp->conn_tcp; 4007 mblk_t *mp = &tcp->tcp_closemp; 4008 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 4009 mblk_t *bp; 4010 4011 ASSERT(WR(q)->q_next == NULL); 4012 ASSERT(connp->conn_ref >= 2); 4013 4014 /* 4015 * We are being closed as /dev/tcp or /dev/tcp6. 4016 * 4017 * Mark the conn as closing. ill_pending_mp_add will not 4018 * add any mp to the pending mp list, after this conn has 4019 * started closing. Same for sq_pending_mp_add 4020 */ 4021 mutex_enter(&connp->conn_lock); 4022 connp->conn_state_flags |= CONN_CLOSING; 4023 if (connp->conn_oper_pending_ill != NULL) 4024 conn_ioctl_cleanup_reqd = B_TRUE; 4025 CONN_INC_REF_LOCKED(connp); 4026 mutex_exit(&connp->conn_lock); 4027 tcp->tcp_closeflags = (uint8_t)flags; 4028 ASSERT(connp->conn_ref >= 3); 4029 4030 /* 4031 * tcp_closemp_used is used below without any protection of a lock 4032 * as we don't expect any one else to use it concurrently at this 4033 * point otherwise it would be a major defect. 4034 */ 4035 4036 if (mp->b_prev == NULL) 4037 tcp->tcp_closemp_used = B_TRUE; 4038 else 4039 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 4040 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 4041 4042 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4043 4044 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4045 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4046 4047 mutex_enter(&tcp->tcp_closelock); 4048 while (!tcp->tcp_closed) { 4049 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4050 /* 4051 * The cv_wait_sig() was interrupted. We now do the 4052 * following: 4053 * 4054 * 1) If the endpoint was lingering, we allow this 4055 * to be interrupted by cancelling the linger timeout 4056 * and closing normally. 4057 * 4058 * 2) Revert to calling cv_wait() 4059 * 4060 * We revert to using cv_wait() to avoid an 4061 * infinite loop which can occur if the calling 4062 * thread is higher priority than the squeue worker 4063 * thread and is bound to the same cpu. 4064 */ 4065 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 4066 mutex_exit(&tcp->tcp_closelock); 4067 /* Entering squeue, bump ref count. */ 4068 CONN_INC_REF(connp); 4069 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4070 squeue_enter(connp->conn_sqp, bp, 4071 tcp_linger_interrupted, connp, 4072 SQTAG_IP_TCP_CLOSE); 4073 mutex_enter(&tcp->tcp_closelock); 4074 } 4075 break; 4076 } 4077 } 4078 while (!tcp->tcp_closed) 4079 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 4080 mutex_exit(&tcp->tcp_closelock); 4081 4082 /* 4083 * In the case of listener streams that have eagers in the q or q0 4084 * we wait for the eagers to drop their reference to us. tcp_rq and 4085 * tcp_wq of the eagers point to our queues. By waiting for the 4086 * refcnt to drop to 1, we are sure that the eagers have cleaned 4087 * up their queue pointers and also dropped their references to us. 4088 */ 4089 if (tcp->tcp_wait_for_eagers) { 4090 mutex_enter(&connp->conn_lock); 4091 while (connp->conn_ref != 1) { 4092 cv_wait(&connp->conn_cv, &connp->conn_lock); 4093 } 4094 mutex_exit(&connp->conn_lock); 4095 } 4096 /* 4097 * ioctl cleanup. The mp is queued in the 4098 * ill_pending_mp or in the sq_pending_mp. 4099 */ 4100 if (conn_ioctl_cleanup_reqd) 4101 conn_ioctl_cleanup(connp); 4102 4103 qprocsoff(q); 4104 inet_minor_free(ip_minor_arena, connp->conn_dev); 4105 4106 tcp->tcp_cpid = -1; 4107 4108 /* 4109 * Drop IP's reference on the conn. This is the last reference 4110 * on the connp if the state was less than established. If the 4111 * connection has gone into timewait state, then we will have 4112 * one ref for the TCP and one more ref (total of two) for the 4113 * classifier connected hash list (a timewait connections stays 4114 * in connected hash till closed). 4115 * 4116 * We can't assert the references because there might be other 4117 * transient reference places because of some walkers or queued 4118 * packets in squeue for the timewait state. 4119 */ 4120 CONN_DEC_REF(connp); 4121 q->q_ptr = WR(q)->q_ptr = NULL; 4122 return (0); 4123 } 4124 4125 static int 4126 tcpclose_accept(queue_t *q) 4127 { 4128 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4129 4130 /* 4131 * We had opened an acceptor STREAM for sockfs which is 4132 * now being closed due to some error. 4133 */ 4134 qprocsoff(q); 4135 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 4136 q->q_ptr = WR(q)->q_ptr = NULL; 4137 return (0); 4138 } 4139 4140 /* 4141 * Called by tcp_close() routine via squeue when lingering is 4142 * interrupted by a signal. 4143 */ 4144 4145 /* ARGSUSED */ 4146 static void 4147 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4148 { 4149 conn_t *connp = (conn_t *)arg; 4150 tcp_t *tcp = connp->conn_tcp; 4151 4152 freeb(mp); 4153 if (tcp->tcp_linger_tid != 0 && 4154 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4155 tcp_stop_lingering(tcp); 4156 tcp->tcp_client_errno = EINTR; 4157 } 4158 } 4159 4160 /* 4161 * Called by streams close routine via squeues when our client blows off her 4162 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4163 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4164 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4165 * acked. 4166 * 4167 * NOTE: tcp_close potentially returns error when lingering. 4168 * However, the stream head currently does not pass these errors 4169 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4170 * errors to the application (from tsleep()) and not errors 4171 * like ECONNRESET caused by receiving a reset packet. 4172 */ 4173 4174 /* ARGSUSED */ 4175 static void 4176 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4177 { 4178 char *msg; 4179 conn_t *connp = (conn_t *)arg; 4180 tcp_t *tcp = connp->conn_tcp; 4181 clock_t delta = 0; 4182 tcp_stack_t *tcps = tcp->tcp_tcps; 4183 4184 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4185 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4186 4187 /* Cancel any pending timeout */ 4188 if (tcp->tcp_ordrelid != 0) { 4189 if (tcp->tcp_timeout) { 4190 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4191 } 4192 tcp->tcp_ordrelid = 0; 4193 tcp->tcp_timeout = B_FALSE; 4194 } 4195 4196 mutex_enter(&tcp->tcp_eager_lock); 4197 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4198 /* Cleanup for listener */ 4199 tcp_eager_cleanup(tcp, 0); 4200 tcp->tcp_wait_for_eagers = 1; 4201 } 4202 mutex_exit(&tcp->tcp_eager_lock); 4203 4204 connp->conn_mdt_ok = B_FALSE; 4205 tcp->tcp_mdt = B_FALSE; 4206 4207 connp->conn_lso_ok = B_FALSE; 4208 tcp->tcp_lso = B_FALSE; 4209 4210 msg = NULL; 4211 switch (tcp->tcp_state) { 4212 case TCPS_CLOSED: 4213 case TCPS_IDLE: 4214 case TCPS_BOUND: 4215 case TCPS_LISTEN: 4216 break; 4217 case TCPS_SYN_SENT: 4218 msg = "tcp_close, during connect"; 4219 break; 4220 case TCPS_SYN_RCVD: 4221 /* 4222 * Close during the connect 3-way handshake 4223 * but here there may or may not be pending data 4224 * already on queue. Process almost same as in 4225 * the ESTABLISHED state. 4226 */ 4227 /* FALLTHRU */ 4228 default: 4229 if (tcp->tcp_fused) 4230 tcp_unfuse(tcp); 4231 4232 /* 4233 * If SO_LINGER has set a zero linger time, abort the 4234 * connection with a reset. 4235 */ 4236 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4237 msg = "tcp_close, zero lingertime"; 4238 break; 4239 } 4240 4241 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4242 /* 4243 * Abort connection if there is unread data queued. 4244 */ 4245 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4246 msg = "tcp_close, unread data"; 4247 break; 4248 } 4249 /* 4250 * tcp_hard_bound is now cleared thus all packets go through 4251 * tcp_lookup. This fact is used by tcp_detach below. 4252 * 4253 * We have done a qwait() above which could have possibly 4254 * drained more messages in turn causing transition to a 4255 * different state. Check whether we have to do the rest 4256 * of the processing or not. 4257 */ 4258 if (tcp->tcp_state <= TCPS_LISTEN) 4259 break; 4260 4261 /* 4262 * Transmit the FIN before detaching the tcp_t. 4263 * After tcp_detach returns this queue/perimeter 4264 * no longer owns the tcp_t thus others can modify it. 4265 */ 4266 (void) tcp_xmit_end(tcp); 4267 4268 /* 4269 * If lingering on close then wait until the fin is acked, 4270 * the SO_LINGER time passes, or a reset is sent/received. 4271 */ 4272 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4273 !(tcp->tcp_fin_acked) && 4274 tcp->tcp_state >= TCPS_ESTABLISHED) { 4275 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4276 tcp->tcp_client_errno = EWOULDBLOCK; 4277 } else if (tcp->tcp_client_errno == 0) { 4278 4279 ASSERT(tcp->tcp_linger_tid == 0); 4280 4281 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4282 tcp_close_linger_timeout, 4283 tcp->tcp_lingertime * hz); 4284 4285 /* tcp_close_linger_timeout will finish close */ 4286 if (tcp->tcp_linger_tid == 0) 4287 tcp->tcp_client_errno = ENOSR; 4288 else 4289 return; 4290 } 4291 4292 /* 4293 * Check if we need to detach or just close 4294 * the instance. 4295 */ 4296 if (tcp->tcp_state <= TCPS_LISTEN) 4297 break; 4298 } 4299 4300 /* 4301 * Make sure that no other thread will access the tcp_rq of 4302 * this instance (through lookups etc.) as tcp_rq will go 4303 * away shortly. 4304 */ 4305 tcp_acceptor_hash_remove(tcp); 4306 4307 mutex_enter(&tcp->tcp_non_sq_lock); 4308 if (tcp->tcp_flow_stopped) { 4309 tcp_clrqfull(tcp); 4310 } 4311 mutex_exit(&tcp->tcp_non_sq_lock); 4312 4313 if (tcp->tcp_timer_tid != 0) { 4314 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4315 tcp->tcp_timer_tid = 0; 4316 } 4317 /* 4318 * Need to cancel those timers which will not be used when 4319 * TCP is detached. This has to be done before the tcp_wq 4320 * is set to the global queue. 4321 */ 4322 tcp_timers_stop(tcp); 4323 4324 tcp->tcp_detached = B_TRUE; 4325 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4326 tcp_time_wait_append(tcp); 4327 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4328 ASSERT(connp->conn_ref >= 3); 4329 goto finish; 4330 } 4331 4332 /* 4333 * If delta is zero the timer event wasn't executed and was 4334 * successfully canceled. In this case we need to restart it 4335 * with the minimal delta possible. 4336 */ 4337 if (delta >= 0) 4338 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4339 delta ? delta : 1); 4340 4341 ASSERT(connp->conn_ref >= 3); 4342 goto finish; 4343 } 4344 4345 /* Detach did not complete. Still need to remove q from stream. */ 4346 if (msg) { 4347 if (tcp->tcp_state == TCPS_ESTABLISHED || 4348 tcp->tcp_state == TCPS_CLOSE_WAIT) 4349 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4350 if (tcp->tcp_state == TCPS_SYN_SENT || 4351 tcp->tcp_state == TCPS_SYN_RCVD) 4352 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4353 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4354 } 4355 4356 tcp_closei_local(tcp); 4357 CONN_DEC_REF(connp); 4358 ASSERT(connp->conn_ref >= 2); 4359 4360 finish: 4361 /* 4362 * Although packets are always processed on the correct 4363 * tcp's perimeter and access is serialized via squeue's, 4364 * IP still needs a queue when sending packets in time_wait 4365 * state so use WR(tcps_g_q) till ip_output() can be 4366 * changed to deal with just connp. For read side, we 4367 * could have set tcp_rq to NULL but there are some cases 4368 * in tcp_rput_data() from early days of this code which 4369 * do a putnext without checking if tcp is closed. Those 4370 * need to be identified before both tcp_rq and tcp_wq 4371 * can be set to NULL and tcps_g_q can disappear forever. 4372 */ 4373 mutex_enter(&tcp->tcp_closelock); 4374 /* 4375 * Don't change the queues in the case of a listener that has 4376 * eagers in its q or q0. It could surprise the eagers. 4377 * Instead wait for the eagers outside the squeue. 4378 */ 4379 if (!tcp->tcp_wait_for_eagers) { 4380 tcp->tcp_detached = B_TRUE; 4381 /* 4382 * When default queue is closing we set tcps_g_q to NULL 4383 * after the close is done. 4384 */ 4385 ASSERT(tcps->tcps_g_q != NULL); 4386 tcp->tcp_rq = tcps->tcps_g_q; 4387 tcp->tcp_wq = WR(tcps->tcps_g_q); 4388 } 4389 4390 /* Signal tcp_close() to finish closing. */ 4391 tcp->tcp_closed = 1; 4392 cv_signal(&tcp->tcp_closecv); 4393 mutex_exit(&tcp->tcp_closelock); 4394 } 4395 4396 4397 /* 4398 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4399 * Some stream heads get upset if they see these later on as anything but NULL. 4400 */ 4401 static void 4402 tcp_close_mpp(mblk_t **mpp) 4403 { 4404 mblk_t *mp; 4405 4406 if ((mp = *mpp) != NULL) { 4407 do { 4408 mp->b_next = NULL; 4409 mp->b_prev = NULL; 4410 } while ((mp = mp->b_cont) != NULL); 4411 4412 mp = *mpp; 4413 *mpp = NULL; 4414 freemsg(mp); 4415 } 4416 } 4417 4418 /* Do detached close. */ 4419 static void 4420 tcp_close_detached(tcp_t *tcp) 4421 { 4422 if (tcp->tcp_fused) 4423 tcp_unfuse(tcp); 4424 4425 /* 4426 * Clustering code serializes TCP disconnect callbacks and 4427 * cluster tcp list walks by blocking a TCP disconnect callback 4428 * if a cluster tcp list walk is in progress. This ensures 4429 * accurate accounting of TCPs in the cluster code even though 4430 * the TCP list walk itself is not atomic. 4431 */ 4432 tcp_closei_local(tcp); 4433 CONN_DEC_REF(tcp->tcp_connp); 4434 } 4435 4436 /* 4437 * Stop all TCP timers, and free the timer mblks if requested. 4438 */ 4439 void 4440 tcp_timers_stop(tcp_t *tcp) 4441 { 4442 if (tcp->tcp_timer_tid != 0) { 4443 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4444 tcp->tcp_timer_tid = 0; 4445 } 4446 if (tcp->tcp_ka_tid != 0) { 4447 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4448 tcp->tcp_ka_tid = 0; 4449 } 4450 if (tcp->tcp_ack_tid != 0) { 4451 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4452 tcp->tcp_ack_tid = 0; 4453 } 4454 if (tcp->tcp_push_tid != 0) { 4455 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4456 tcp->tcp_push_tid = 0; 4457 } 4458 } 4459 4460 /* 4461 * The tcp_t is going away. Remove it from all lists and set it 4462 * to TCPS_CLOSED. The freeing up of memory is deferred until 4463 * tcp_inactive. This is needed since a thread in tcp_rput might have 4464 * done a CONN_INC_REF on this structure before it was removed from the 4465 * hashes. 4466 */ 4467 static void 4468 tcp_closei_local(tcp_t *tcp) 4469 { 4470 ire_t *ire; 4471 conn_t *connp = tcp->tcp_connp; 4472 tcp_stack_t *tcps = tcp->tcp_tcps; 4473 4474 if (!TCP_IS_SOCKET(tcp)) 4475 tcp_acceptor_hash_remove(tcp); 4476 4477 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4478 tcp->tcp_ibsegs = 0; 4479 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4480 tcp->tcp_obsegs = 0; 4481 4482 /* 4483 * If we are an eager connection hanging off a listener that 4484 * hasn't formally accepted the connection yet, get off his 4485 * list and blow off any data that we have accumulated. 4486 */ 4487 if (tcp->tcp_listener != NULL) { 4488 tcp_t *listener = tcp->tcp_listener; 4489 mutex_enter(&listener->tcp_eager_lock); 4490 /* 4491 * tcp_tconnind_started == B_TRUE means that the 4492 * conn_ind has already gone to listener. At 4493 * this point, eager will be closed but we 4494 * leave it in listeners eager list so that 4495 * if listener decides to close without doing 4496 * accept, we can clean this up. In tcp_wput_accept 4497 * we take care of the case of accept on closed 4498 * eager. 4499 */ 4500 if (!tcp->tcp_tconnind_started) { 4501 tcp_eager_unlink(tcp); 4502 mutex_exit(&listener->tcp_eager_lock); 4503 /* 4504 * We don't want to have any pointers to the 4505 * listener queue, after we have released our 4506 * reference on the listener 4507 */ 4508 ASSERT(tcps->tcps_g_q != NULL); 4509 tcp->tcp_rq = tcps->tcps_g_q; 4510 tcp->tcp_wq = WR(tcps->tcps_g_q); 4511 CONN_DEC_REF(listener->tcp_connp); 4512 } else { 4513 mutex_exit(&listener->tcp_eager_lock); 4514 } 4515 } 4516 4517 /* Stop all the timers */ 4518 tcp_timers_stop(tcp); 4519 4520 if (tcp->tcp_state == TCPS_LISTEN) { 4521 if (tcp->tcp_ip_addr_cache) { 4522 kmem_free((void *)tcp->tcp_ip_addr_cache, 4523 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4524 tcp->tcp_ip_addr_cache = NULL; 4525 } 4526 } 4527 mutex_enter(&tcp->tcp_non_sq_lock); 4528 if (tcp->tcp_flow_stopped) 4529 tcp_clrqfull(tcp); 4530 mutex_exit(&tcp->tcp_non_sq_lock); 4531 4532 tcp_bind_hash_remove(tcp); 4533 /* 4534 * If the tcp_time_wait_collector (which runs outside the squeue) 4535 * is trying to remove this tcp from the time wait list, we will 4536 * block in tcp_time_wait_remove while trying to acquire the 4537 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4538 * requires the ipcl_hash_remove to be ordered after the 4539 * tcp_time_wait_remove for the refcnt checks to work correctly. 4540 */ 4541 if (tcp->tcp_state == TCPS_TIME_WAIT) 4542 (void) tcp_time_wait_remove(tcp, NULL); 4543 CL_INET_DISCONNECT(tcp); 4544 ipcl_hash_remove(connp); 4545 4546 /* 4547 * Delete the cached ire in conn_ire_cache and also mark 4548 * the conn as CONDEMNED 4549 */ 4550 mutex_enter(&connp->conn_lock); 4551 connp->conn_state_flags |= CONN_CONDEMNED; 4552 ire = connp->conn_ire_cache; 4553 connp->conn_ire_cache = NULL; 4554 mutex_exit(&connp->conn_lock); 4555 if (ire != NULL) 4556 IRE_REFRELE_NOTR(ire); 4557 4558 /* Need to cleanup any pending ioctls */ 4559 ASSERT(tcp->tcp_time_wait_next == NULL); 4560 ASSERT(tcp->tcp_time_wait_prev == NULL); 4561 ASSERT(tcp->tcp_time_wait_expire == 0); 4562 tcp->tcp_state = TCPS_CLOSED; 4563 4564 /* Release any SSL context */ 4565 if (tcp->tcp_kssl_ent != NULL) { 4566 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4567 tcp->tcp_kssl_ent = NULL; 4568 } 4569 if (tcp->tcp_kssl_ctx != NULL) { 4570 kssl_release_ctx(tcp->tcp_kssl_ctx); 4571 tcp->tcp_kssl_ctx = NULL; 4572 } 4573 tcp->tcp_kssl_pending = B_FALSE; 4574 4575 tcp_ipsec_cleanup(tcp); 4576 } 4577 4578 /* 4579 * tcp is dying (called from ipcl_conn_destroy and error cases). 4580 * Free the tcp_t in either case. 4581 */ 4582 void 4583 tcp_free(tcp_t *tcp) 4584 { 4585 mblk_t *mp; 4586 ip6_pkt_t *ipp; 4587 4588 ASSERT(tcp != NULL); 4589 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4590 4591 tcp->tcp_rq = NULL; 4592 tcp->tcp_wq = NULL; 4593 4594 tcp_close_mpp(&tcp->tcp_xmit_head); 4595 tcp_close_mpp(&tcp->tcp_reass_head); 4596 if (tcp->tcp_rcv_list != NULL) { 4597 /* Free b_next chain */ 4598 tcp_close_mpp(&tcp->tcp_rcv_list); 4599 } 4600 if ((mp = tcp->tcp_urp_mp) != NULL) { 4601 freemsg(mp); 4602 } 4603 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4604 freemsg(mp); 4605 } 4606 4607 if (tcp->tcp_fused_sigurg_mp != NULL) { 4608 freeb(tcp->tcp_fused_sigurg_mp); 4609 tcp->tcp_fused_sigurg_mp = NULL; 4610 } 4611 4612 if (tcp->tcp_sack_info != NULL) { 4613 if (tcp->tcp_notsack_list != NULL) { 4614 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4615 } 4616 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4617 } 4618 4619 if (tcp->tcp_hopopts != NULL) { 4620 mi_free(tcp->tcp_hopopts); 4621 tcp->tcp_hopopts = NULL; 4622 tcp->tcp_hopoptslen = 0; 4623 } 4624 ASSERT(tcp->tcp_hopoptslen == 0); 4625 if (tcp->tcp_dstopts != NULL) { 4626 mi_free(tcp->tcp_dstopts); 4627 tcp->tcp_dstopts = NULL; 4628 tcp->tcp_dstoptslen = 0; 4629 } 4630 ASSERT(tcp->tcp_dstoptslen == 0); 4631 if (tcp->tcp_rtdstopts != NULL) { 4632 mi_free(tcp->tcp_rtdstopts); 4633 tcp->tcp_rtdstopts = NULL; 4634 tcp->tcp_rtdstoptslen = 0; 4635 } 4636 ASSERT(tcp->tcp_rtdstoptslen == 0); 4637 if (tcp->tcp_rthdr != NULL) { 4638 mi_free(tcp->tcp_rthdr); 4639 tcp->tcp_rthdr = NULL; 4640 tcp->tcp_rthdrlen = 0; 4641 } 4642 ASSERT(tcp->tcp_rthdrlen == 0); 4643 4644 ipp = &tcp->tcp_sticky_ipp; 4645 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4646 IPPF_RTHDR)) 4647 ip6_pkt_free(ipp); 4648 4649 /* 4650 * Free memory associated with the tcp/ip header template. 4651 */ 4652 4653 if (tcp->tcp_iphc != NULL) 4654 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4655 4656 /* 4657 * Following is really a blowing away a union. 4658 * It happens to have exactly two members of identical size 4659 * the following code is enough. 4660 */ 4661 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4662 4663 if (tcp->tcp_tracebuf != NULL) { 4664 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4665 tcp->tcp_tracebuf = NULL; 4666 } 4667 } 4668 4669 4670 /* 4671 * Put a connection confirmation message upstream built from the 4672 * address information within 'iph' and 'tcph'. Report our success or failure. 4673 */ 4674 static boolean_t 4675 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4676 mblk_t **defermp) 4677 { 4678 sin_t sin; 4679 sin6_t sin6; 4680 mblk_t *mp; 4681 char *optp = NULL; 4682 int optlen = 0; 4683 cred_t *cr; 4684 4685 if (defermp != NULL) 4686 *defermp = NULL; 4687 4688 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4689 /* 4690 * Return in T_CONN_CON results of option negotiation through 4691 * the T_CONN_REQ. Note: If there is an real end-to-end option 4692 * negotiation, then what is received from remote end needs 4693 * to be taken into account but there is no such thing (yet?) 4694 * in our TCP/IP. 4695 * Note: We do not use mi_offset_param() here as 4696 * tcp_opts_conn_req contents do not directly come from 4697 * an application and are either generated in kernel or 4698 * from user input that was already verified. 4699 */ 4700 mp = tcp->tcp_conn.tcp_opts_conn_req; 4701 optp = (char *)(mp->b_rptr + 4702 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4703 optlen = (int) 4704 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4705 } 4706 4707 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4708 ipha_t *ipha = (ipha_t *)iphdr; 4709 4710 /* packet is IPv4 */ 4711 if (tcp->tcp_family == AF_INET) { 4712 sin = sin_null; 4713 sin.sin_addr.s_addr = ipha->ipha_src; 4714 sin.sin_port = *(uint16_t *)tcph->th_lport; 4715 sin.sin_family = AF_INET; 4716 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4717 (int)sizeof (sin_t), optp, optlen); 4718 } else { 4719 sin6 = sin6_null; 4720 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4721 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4722 sin6.sin6_family = AF_INET6; 4723 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4724 (int)sizeof (sin6_t), optp, optlen); 4725 4726 } 4727 } else { 4728 ip6_t *ip6h = (ip6_t *)iphdr; 4729 4730 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4731 ASSERT(tcp->tcp_family == AF_INET6); 4732 sin6 = sin6_null; 4733 sin6.sin6_addr = ip6h->ip6_src; 4734 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4735 sin6.sin6_family = AF_INET6; 4736 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4737 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4738 (int)sizeof (sin6_t), optp, optlen); 4739 } 4740 4741 if (!mp) 4742 return (B_FALSE); 4743 4744 if ((cr = DB_CRED(idmp)) != NULL) { 4745 mblk_setcred(mp, cr); 4746 DB_CPID(mp) = DB_CPID(idmp); 4747 } 4748 4749 if (defermp == NULL) 4750 putnext(tcp->tcp_rq, mp); 4751 else 4752 *defermp = mp; 4753 4754 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4755 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4756 return (B_TRUE); 4757 } 4758 4759 /* 4760 * Defense for the SYN attack - 4761 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4762 * one from the list of droppable eagers. This list is a subset of q0. 4763 * see comments before the definition of MAKE_DROPPABLE(). 4764 * 2. Don't drop a SYN request before its first timeout. This gives every 4765 * request at least til the first timeout to complete its 3-way handshake. 4766 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4767 * requests currently on the queue that has timed out. This will be used 4768 * as an indicator of whether an attack is under way, so that appropriate 4769 * actions can be taken. (It's incremented in tcp_timer() and decremented 4770 * either when eager goes into ESTABLISHED, or gets freed up.) 4771 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4772 * # of timeout drops back to <= q0len/32 => SYN alert off 4773 */ 4774 static boolean_t 4775 tcp_drop_q0(tcp_t *tcp) 4776 { 4777 tcp_t *eager; 4778 mblk_t *mp; 4779 tcp_stack_t *tcps = tcp->tcp_tcps; 4780 4781 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4782 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4783 4784 /* Pick oldest eager from the list of droppable eagers */ 4785 eager = tcp->tcp_eager_prev_drop_q0; 4786 4787 /* If list is empty. return B_FALSE */ 4788 if (eager == tcp) { 4789 return (B_FALSE); 4790 } 4791 4792 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4793 if ((mp = allocb(0, BPRI_HI)) == NULL) 4794 return (B_FALSE); 4795 4796 /* 4797 * Take this eager out from the list of droppable eagers since we are 4798 * going to drop it. 4799 */ 4800 MAKE_UNDROPPABLE(eager); 4801 4802 if (tcp->tcp_debug) { 4803 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4804 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4805 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4806 tcp->tcp_conn_req_cnt_q0, 4807 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4808 } 4809 4810 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4811 4812 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4813 CONN_INC_REF(eager->tcp_connp); 4814 4815 /* Mark the IRE created for this SYN request temporary */ 4816 tcp_ip_ire_mark_advice(eager); 4817 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4818 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4819 4820 return (B_TRUE); 4821 } 4822 4823 int 4824 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4825 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4826 { 4827 tcp_t *ltcp = lconnp->conn_tcp; 4828 tcp_t *tcp = connp->conn_tcp; 4829 mblk_t *tpi_mp; 4830 ipha_t *ipha; 4831 ip6_t *ip6h; 4832 sin6_t sin6; 4833 in6_addr_t v6dst; 4834 int err; 4835 int ifindex = 0; 4836 cred_t *cr; 4837 tcp_stack_t *tcps = tcp->tcp_tcps; 4838 4839 if (ipvers == IPV4_VERSION) { 4840 ipha = (ipha_t *)mp->b_rptr; 4841 4842 connp->conn_send = ip_output; 4843 connp->conn_recv = tcp_input; 4844 4845 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4846 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4847 4848 sin6 = sin6_null; 4849 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4850 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4851 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4852 sin6.sin6_family = AF_INET6; 4853 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4854 lconnp->conn_zoneid, tcps->tcps_netstack); 4855 if (tcp->tcp_recvdstaddr) { 4856 sin6_t sin6d; 4857 4858 sin6d = sin6_null; 4859 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4860 &sin6d.sin6_addr); 4861 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4862 sin6d.sin6_family = AF_INET; 4863 tpi_mp = mi_tpi_extconn_ind(NULL, 4864 (char *)&sin6d, sizeof (sin6_t), 4865 (char *)&tcp, 4866 (t_scalar_t)sizeof (intptr_t), 4867 (char *)&sin6d, sizeof (sin6_t), 4868 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4869 } else { 4870 tpi_mp = mi_tpi_conn_ind(NULL, 4871 (char *)&sin6, sizeof (sin6_t), 4872 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4873 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4874 } 4875 } else { 4876 ip6h = (ip6_t *)mp->b_rptr; 4877 4878 connp->conn_send = ip_output_v6; 4879 connp->conn_recv = tcp_input; 4880 4881 connp->conn_srcv6 = ip6h->ip6_dst; 4882 connp->conn_remv6 = ip6h->ip6_src; 4883 4884 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4885 ifindex = (int)DB_CKSUMSTUFF(mp); 4886 DB_CKSUMSTUFF(mp) = 0; 4887 4888 sin6 = sin6_null; 4889 sin6.sin6_addr = ip6h->ip6_src; 4890 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4891 sin6.sin6_family = AF_INET6; 4892 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4893 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4894 lconnp->conn_zoneid, tcps->tcps_netstack); 4895 4896 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4897 /* Pass up the scope_id of remote addr */ 4898 sin6.sin6_scope_id = ifindex; 4899 } else { 4900 sin6.sin6_scope_id = 0; 4901 } 4902 if (tcp->tcp_recvdstaddr) { 4903 sin6_t sin6d; 4904 4905 sin6d = sin6_null; 4906 sin6.sin6_addr = ip6h->ip6_dst; 4907 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4908 sin6d.sin6_family = AF_INET; 4909 tpi_mp = mi_tpi_extconn_ind(NULL, 4910 (char *)&sin6d, sizeof (sin6_t), 4911 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4912 (char *)&sin6d, sizeof (sin6_t), 4913 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4914 } else { 4915 tpi_mp = mi_tpi_conn_ind(NULL, 4916 (char *)&sin6, sizeof (sin6_t), 4917 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4918 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4919 } 4920 } 4921 4922 if (tpi_mp == NULL) 4923 return (ENOMEM); 4924 4925 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4926 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4927 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4928 connp->conn_fully_bound = B_FALSE; 4929 4930 if (tcps->tcps_trace) 4931 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4932 4933 /* Inherit information from the "parent" */ 4934 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4935 tcp->tcp_family = ltcp->tcp_family; 4936 tcp->tcp_wq = ltcp->tcp_wq; 4937 tcp->tcp_rq = ltcp->tcp_rq; 4938 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4939 tcp->tcp_detached = B_TRUE; 4940 if ((err = tcp_init_values(tcp)) != 0) { 4941 freemsg(tpi_mp); 4942 return (err); 4943 } 4944 4945 if (ipvers == IPV4_VERSION) { 4946 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4947 freemsg(tpi_mp); 4948 return (err); 4949 } 4950 ASSERT(tcp->tcp_ipha != NULL); 4951 } else { 4952 /* ifindex must be already set */ 4953 ASSERT(ifindex != 0); 4954 4955 if (ltcp->tcp_bound_if != 0) { 4956 /* 4957 * Set newtcp's bound_if equal to 4958 * listener's value. If ifindex is 4959 * not the same as ltcp->tcp_bound_if, 4960 * it must be a packet for the ipmp group 4961 * of interfaces 4962 */ 4963 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4964 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4965 tcp->tcp_bound_if = ifindex; 4966 } 4967 4968 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4969 tcp->tcp_recvifindex = 0; 4970 tcp->tcp_recvhops = 0xffffffffU; 4971 ASSERT(tcp->tcp_ip6h != NULL); 4972 } 4973 4974 tcp->tcp_lport = ltcp->tcp_lport; 4975 4976 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4977 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4978 /* 4979 * Listener had options of some sort; eager inherits. 4980 * Free up the eager template and allocate one 4981 * of the right size. 4982 */ 4983 if (tcp->tcp_hdr_grown) { 4984 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4985 } else { 4986 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4987 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4988 } 4989 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4990 KM_NOSLEEP); 4991 if (tcp->tcp_iphc == NULL) { 4992 tcp->tcp_iphc_len = 0; 4993 freemsg(tpi_mp); 4994 return (ENOMEM); 4995 } 4996 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4997 tcp->tcp_hdr_grown = B_TRUE; 4998 } 4999 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5000 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5001 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5002 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 5003 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 5004 5005 /* 5006 * Copy the IP+TCP header template from listener to eager 5007 */ 5008 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5009 if (tcp->tcp_ipversion == IPV6_VERSION) { 5010 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 5011 IPPROTO_RAW) { 5012 tcp->tcp_ip6h = 5013 (ip6_t *)(tcp->tcp_iphc + 5014 sizeof (ip6i_t)); 5015 } else { 5016 tcp->tcp_ip6h = 5017 (ip6_t *)(tcp->tcp_iphc); 5018 } 5019 tcp->tcp_ipha = NULL; 5020 } else { 5021 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5022 tcp->tcp_ip6h = NULL; 5023 } 5024 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5025 tcp->tcp_ip_hdr_len); 5026 } else { 5027 /* 5028 * only valid case when ipversion of listener and 5029 * eager differ is when listener is IPv6 and 5030 * eager is IPv4. 5031 * Eager header template has been initialized to the 5032 * maximum v4 header sizes, which includes space for 5033 * TCP and IP options. 5034 */ 5035 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5036 (tcp->tcp_ipversion == IPV4_VERSION)); 5037 ASSERT(tcp->tcp_iphc_len >= 5038 TCP_MAX_COMBINED_HEADER_LENGTH); 5039 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5040 /* copy IP header fields individually */ 5041 tcp->tcp_ipha->ipha_ttl = 5042 ltcp->tcp_ip6h->ip6_hops; 5043 bcopy(ltcp->tcp_tcph->th_lport, 5044 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5045 } 5046 5047 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5048 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5049 sizeof (in_port_t)); 5050 5051 if (ltcp->tcp_lport == 0) { 5052 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5053 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5054 sizeof (in_port_t)); 5055 } 5056 5057 if (tcp->tcp_ipversion == IPV4_VERSION) { 5058 ASSERT(ipha != NULL); 5059 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5060 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5061 5062 /* Source routing option copyover (reverse it) */ 5063 if (tcps->tcps_rev_src_routes) 5064 tcp_opt_reverse(tcp, ipha); 5065 } else { 5066 ASSERT(ip6h != NULL); 5067 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5068 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5069 } 5070 5071 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5072 ASSERT(!tcp->tcp_tconnind_started); 5073 /* 5074 * If the SYN contains a credential, it's a loopback packet; attach 5075 * the credential to the TPI message. 5076 */ 5077 if ((cr = DB_CRED(idmp)) != NULL) { 5078 mblk_setcred(tpi_mp, cr); 5079 DB_CPID(tpi_mp) = DB_CPID(idmp); 5080 } 5081 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5082 5083 /* Inherit the listener's SSL protection state */ 5084 5085 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5086 kssl_hold_ent(tcp->tcp_kssl_ent); 5087 tcp->tcp_kssl_pending = B_TRUE; 5088 } 5089 5090 return (0); 5091 } 5092 5093 5094 int 5095 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5096 tcph_t *tcph, mblk_t *idmp) 5097 { 5098 tcp_t *ltcp = lconnp->conn_tcp; 5099 tcp_t *tcp = connp->conn_tcp; 5100 sin_t sin; 5101 mblk_t *tpi_mp = NULL; 5102 int err; 5103 cred_t *cr; 5104 tcp_stack_t *tcps = tcp->tcp_tcps; 5105 5106 sin = sin_null; 5107 sin.sin_addr.s_addr = ipha->ipha_src; 5108 sin.sin_port = *(uint16_t *)tcph->th_lport; 5109 sin.sin_family = AF_INET; 5110 if (ltcp->tcp_recvdstaddr) { 5111 sin_t sind; 5112 5113 sind = sin_null; 5114 sind.sin_addr.s_addr = ipha->ipha_dst; 5115 sind.sin_port = *(uint16_t *)tcph->th_fport; 5116 sind.sin_family = AF_INET; 5117 tpi_mp = mi_tpi_extconn_ind(NULL, 5118 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5119 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5120 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5121 } else { 5122 tpi_mp = mi_tpi_conn_ind(NULL, 5123 (char *)&sin, sizeof (sin_t), 5124 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5125 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5126 } 5127 5128 if (tpi_mp == NULL) { 5129 return (ENOMEM); 5130 } 5131 5132 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5133 connp->conn_send = ip_output; 5134 connp->conn_recv = tcp_input; 5135 connp->conn_fully_bound = B_FALSE; 5136 5137 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5138 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5139 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5140 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5141 5142 if (tcps->tcps_trace) { 5143 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 5144 } 5145 5146 /* Inherit information from the "parent" */ 5147 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5148 tcp->tcp_family = ltcp->tcp_family; 5149 tcp->tcp_wq = ltcp->tcp_wq; 5150 tcp->tcp_rq = ltcp->tcp_rq; 5151 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 5152 tcp->tcp_detached = B_TRUE; 5153 if ((err = tcp_init_values(tcp)) != 0) { 5154 freemsg(tpi_mp); 5155 return (err); 5156 } 5157 5158 /* 5159 * Let's make sure that eager tcp template has enough space to 5160 * copy IPv4 listener's tcp template. Since the conn_t structure is 5161 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5162 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5163 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5164 * extension headers or with ip6i_t struct). Note that bcopy() below 5165 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5166 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5167 */ 5168 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5169 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5170 5171 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5172 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5173 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5174 tcp->tcp_ttl = ltcp->tcp_ttl; 5175 tcp->tcp_tos = ltcp->tcp_tos; 5176 5177 /* Copy the IP+TCP header template from listener to eager */ 5178 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5179 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5180 tcp->tcp_ip6h = NULL; 5181 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5182 tcp->tcp_ip_hdr_len); 5183 5184 /* Initialize the IP addresses and Ports */ 5185 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5186 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5187 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5188 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5189 5190 /* Source routing option copyover (reverse it) */ 5191 if (tcps->tcps_rev_src_routes) 5192 tcp_opt_reverse(tcp, ipha); 5193 5194 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5195 ASSERT(!tcp->tcp_tconnind_started); 5196 5197 /* 5198 * If the SYN contains a credential, it's a loopback packet; attach 5199 * the credential to the TPI message. 5200 */ 5201 if ((cr = DB_CRED(idmp)) != NULL) { 5202 mblk_setcred(tpi_mp, cr); 5203 DB_CPID(tpi_mp) = DB_CPID(idmp); 5204 } 5205 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5206 5207 /* Inherit the listener's SSL protection state */ 5208 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5209 kssl_hold_ent(tcp->tcp_kssl_ent); 5210 tcp->tcp_kssl_pending = B_TRUE; 5211 } 5212 5213 return (0); 5214 } 5215 5216 /* 5217 * sets up conn for ipsec. 5218 * if the first mblk is M_CTL it is consumed and mpp is updated. 5219 * in case of error mpp is freed. 5220 */ 5221 conn_t * 5222 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5223 { 5224 conn_t *connp = tcp->tcp_connp; 5225 conn_t *econnp; 5226 squeue_t *new_sqp; 5227 mblk_t *first_mp = *mpp; 5228 mblk_t *mp = *mpp; 5229 boolean_t mctl_present = B_FALSE; 5230 uint_t ipvers; 5231 5232 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 5233 if (econnp == NULL) { 5234 freemsg(first_mp); 5235 return (NULL); 5236 } 5237 if (DB_TYPE(mp) == M_CTL) { 5238 if (mp->b_cont == NULL || 5239 mp->b_cont->b_datap->db_type != M_DATA) { 5240 freemsg(first_mp); 5241 return (NULL); 5242 } 5243 mp = mp->b_cont; 5244 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5245 freemsg(first_mp); 5246 return (NULL); 5247 } 5248 5249 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5250 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5251 mctl_present = B_TRUE; 5252 } else { 5253 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5254 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5255 } 5256 5257 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5258 DB_CKSUMSTART(mp) = 0; 5259 5260 ASSERT(OK_32PTR(mp->b_rptr)); 5261 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5262 if (ipvers == IPV4_VERSION) { 5263 uint16_t *up; 5264 uint32_t ports; 5265 ipha_t *ipha; 5266 5267 ipha = (ipha_t *)mp->b_rptr; 5268 up = (uint16_t *)((uchar_t *)ipha + 5269 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5270 ports = *(uint32_t *)up; 5271 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5272 ipha->ipha_dst, ipha->ipha_src, ports); 5273 } else { 5274 uint16_t *up; 5275 uint32_t ports; 5276 uint16_t ip_hdr_len; 5277 uint8_t *nexthdrp; 5278 ip6_t *ip6h; 5279 tcph_t *tcph; 5280 5281 ip6h = (ip6_t *)mp->b_rptr; 5282 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5283 ip_hdr_len = IPV6_HDR_LEN; 5284 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5285 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5286 CONN_DEC_REF(econnp); 5287 freemsg(first_mp); 5288 return (NULL); 5289 } 5290 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5291 up = (uint16_t *)tcph->th_lport; 5292 ports = *(uint32_t *)up; 5293 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5294 ip6h->ip6_dst, ip6h->ip6_src, ports); 5295 } 5296 5297 /* 5298 * The caller already ensured that there is a sqp present. 5299 */ 5300 econnp->conn_sqp = new_sqp; 5301 5302 if (connp->conn_policy != NULL) { 5303 ipsec_in_t *ii; 5304 ii = (ipsec_in_t *)(first_mp->b_rptr); 5305 ASSERT(ii->ipsec_in_policy == NULL); 5306 IPPH_REFHOLD(connp->conn_policy); 5307 ii->ipsec_in_policy = connp->conn_policy; 5308 5309 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5310 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5311 CONN_DEC_REF(econnp); 5312 freemsg(first_mp); 5313 return (NULL); 5314 } 5315 } 5316 5317 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5318 CONN_DEC_REF(econnp); 5319 freemsg(first_mp); 5320 return (NULL); 5321 } 5322 5323 /* 5324 * If we know we have some policy, pass the "IPSEC" 5325 * options size TCP uses this adjust the MSS. 5326 */ 5327 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5328 if (mctl_present) { 5329 freeb(first_mp); 5330 *mpp = mp; 5331 } 5332 5333 return (econnp); 5334 } 5335 5336 /* 5337 * tcp_get_conn/tcp_free_conn 5338 * 5339 * tcp_get_conn is used to get a clean tcp connection structure. 5340 * It tries to reuse the connections put on the freelist by the 5341 * time_wait_collector failing which it goes to kmem_cache. This 5342 * way has two benefits compared to just allocating from and 5343 * freeing to kmem_cache. 5344 * 1) The time_wait_collector can free (which includes the cleanup) 5345 * outside the squeue. So when the interrupt comes, we have a clean 5346 * connection sitting in the freelist. Obviously, this buys us 5347 * performance. 5348 * 5349 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5350 * has multiple disadvantages - tying up the squeue during alloc, and the 5351 * fact that IPSec policy initialization has to happen here which 5352 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5353 * But allocating the conn/tcp in IP land is also not the best since 5354 * we can't check the 'q' and 'q0' which are protected by squeue and 5355 * blindly allocate memory which might have to be freed here if we are 5356 * not allowed to accept the connection. By using the freelist and 5357 * putting the conn/tcp back in freelist, we don't pay a penalty for 5358 * allocating memory without checking 'q/q0' and freeing it if we can't 5359 * accept the connection. 5360 * 5361 * Care should be taken to put the conn back in the same squeue's freelist 5362 * from which it was allocated. Best results are obtained if conn is 5363 * allocated from listener's squeue and freed to the same. Time wait 5364 * collector will free up the freelist is the connection ends up sitting 5365 * there for too long. 5366 */ 5367 void * 5368 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5369 { 5370 tcp_t *tcp = NULL; 5371 conn_t *connp = NULL; 5372 squeue_t *sqp = (squeue_t *)arg; 5373 tcp_squeue_priv_t *tcp_time_wait; 5374 netstack_t *ns; 5375 5376 tcp_time_wait = 5377 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5378 5379 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5380 tcp = tcp_time_wait->tcp_free_list; 5381 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5382 if (tcp != NULL) { 5383 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5384 tcp_time_wait->tcp_free_list_cnt--; 5385 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5386 tcp->tcp_time_wait_next = NULL; 5387 connp = tcp->tcp_connp; 5388 connp->conn_flags |= IPCL_REUSED; 5389 5390 ASSERT(tcp->tcp_tcps == NULL); 5391 ASSERT(connp->conn_netstack == NULL); 5392 ns = tcps->tcps_netstack; 5393 netstack_hold(ns); 5394 connp->conn_netstack = ns; 5395 tcp->tcp_tcps = tcps; 5396 TCPS_REFHOLD(tcps); 5397 ipcl_globalhash_insert(connp); 5398 return ((void *)connp); 5399 } 5400 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5401 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5402 tcps->tcps_netstack)) == NULL) 5403 return (NULL); 5404 tcp = connp->conn_tcp; 5405 tcp->tcp_tcps = tcps; 5406 TCPS_REFHOLD(tcps); 5407 return ((void *)connp); 5408 } 5409 5410 /* 5411 * Update the cached label for the given tcp_t. This should be called once per 5412 * connection, and before any packets are sent or tcp_process_options is 5413 * invoked. Returns B_FALSE if the correct label could not be constructed. 5414 */ 5415 static boolean_t 5416 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5417 { 5418 conn_t *connp = tcp->tcp_connp; 5419 5420 if (tcp->tcp_ipversion == IPV4_VERSION) { 5421 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5422 int added; 5423 5424 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5425 connp->conn_mac_exempt, 5426 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5427 return (B_FALSE); 5428 5429 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5430 if (added == -1) 5431 return (B_FALSE); 5432 tcp->tcp_hdr_len += added; 5433 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5434 tcp->tcp_ip_hdr_len += added; 5435 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5436 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5437 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5438 tcp->tcp_hdr_len); 5439 if (added == -1) 5440 return (B_FALSE); 5441 tcp->tcp_hdr_len += added; 5442 tcp->tcp_tcph = (tcph_t *) 5443 ((uchar_t *)tcp->tcp_tcph + added); 5444 tcp->tcp_ip_hdr_len += added; 5445 } 5446 } else { 5447 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5448 5449 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5450 connp->conn_mac_exempt, 5451 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5452 return (B_FALSE); 5453 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5454 &tcp->tcp_label_len, optbuf) != 0) 5455 return (B_FALSE); 5456 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5457 return (B_FALSE); 5458 } 5459 5460 connp->conn_ulp_labeled = 1; 5461 5462 return (B_TRUE); 5463 } 5464 5465 /* BEGIN CSTYLED */ 5466 /* 5467 * 5468 * The sockfs ACCEPT path: 5469 * ======================= 5470 * 5471 * The eager is now established in its own perimeter as soon as SYN is 5472 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5473 * completes the accept processing on the acceptor STREAM. The sending 5474 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5475 * listener but a TLI/XTI listener completes the accept processing 5476 * on the listener perimeter. 5477 * 5478 * Common control flow for 3 way handshake: 5479 * ---------------------------------------- 5480 * 5481 * incoming SYN (listener perimeter) -> tcp_rput_data() 5482 * -> tcp_conn_request() 5483 * 5484 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5485 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5486 * 5487 * Sockfs ACCEPT Path: 5488 * ------------------- 5489 * 5490 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5491 * as STREAM entry point) 5492 * 5493 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5494 * 5495 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5496 * association (we are not behind eager's squeue but sockfs is protecting us 5497 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5498 * is changed to point at tcp_wput(). 5499 * 5500 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5501 * listener (done on listener's perimeter). 5502 * 5503 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5504 * accept. 5505 * 5506 * TLI/XTI client ACCEPT path: 5507 * --------------------------- 5508 * 5509 * soaccept() sends T_CONN_RES on the listener STREAM. 5510 * 5511 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5512 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5513 * 5514 * Locks: 5515 * ====== 5516 * 5517 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5518 * and listeners->tcp_eager_next_q. 5519 * 5520 * Referencing: 5521 * ============ 5522 * 5523 * 1) We start out in tcp_conn_request by eager placing a ref on 5524 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5525 * 5526 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5527 * doing so we place a ref on the eager. This ref is finally dropped at the 5528 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5529 * reference is dropped by the squeue framework. 5530 * 5531 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5532 * 5533 * The reference must be released by the same entity that added the reference 5534 * In the above scheme, the eager is the entity that adds and releases the 5535 * references. Note that tcp_accept_finish executes in the squeue of the eager 5536 * (albeit after it is attached to the acceptor stream). Though 1. executes 5537 * in the listener's squeue, the eager is nascent at this point and the 5538 * reference can be considered to have been added on behalf of the eager. 5539 * 5540 * Eager getting a Reset or listener closing: 5541 * ========================================== 5542 * 5543 * Once the listener and eager are linked, the listener never does the unlink. 5544 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5545 * a message on all eager perimeter. The eager then does the unlink, clears 5546 * any pointers to the listener's queue and drops the reference to the 5547 * listener. The listener waits in tcp_close outside the squeue until its 5548 * refcount has dropped to 1. This ensures that the listener has waited for 5549 * all eagers to clear their association with the listener. 5550 * 5551 * Similarly, if eager decides to go away, it can unlink itself and close. 5552 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5553 * the reference to eager is still valid because of the extra ref we put 5554 * in tcp_send_conn_ind. 5555 * 5556 * Listener can always locate the eager under the protection 5557 * of the listener->tcp_eager_lock, and then do a refhold 5558 * on the eager during the accept processing. 5559 * 5560 * The acceptor stream accesses the eager in the accept processing 5561 * based on the ref placed on eager before sending T_conn_ind. 5562 * The only entity that can negate this refhold is a listener close 5563 * which is mutually exclusive with an active acceptor stream. 5564 * 5565 * Eager's reference on the listener 5566 * =================================== 5567 * 5568 * If the accept happens (even on a closed eager) the eager drops its 5569 * reference on the listener at the start of tcp_accept_finish. If the 5570 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5571 * the reference is dropped in tcp_closei_local. If the listener closes, 5572 * the reference is dropped in tcp_eager_kill. In all cases the reference 5573 * is dropped while executing in the eager's context (squeue). 5574 */ 5575 /* END CSTYLED */ 5576 5577 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5578 5579 /* 5580 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5581 * tcp_rput_data will not see any SYN packets. 5582 */ 5583 /* ARGSUSED */ 5584 void 5585 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5586 { 5587 tcph_t *tcph; 5588 uint32_t seg_seq; 5589 tcp_t *eager; 5590 uint_t ipvers; 5591 ipha_t *ipha; 5592 ip6_t *ip6h; 5593 int err; 5594 conn_t *econnp = NULL; 5595 squeue_t *new_sqp; 5596 mblk_t *mp1; 5597 uint_t ip_hdr_len; 5598 conn_t *connp = (conn_t *)arg; 5599 tcp_t *tcp = connp->conn_tcp; 5600 cred_t *credp; 5601 tcp_stack_t *tcps = tcp->tcp_tcps; 5602 ip_stack_t *ipst; 5603 5604 if (tcp->tcp_state != TCPS_LISTEN) 5605 goto error2; 5606 5607 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5608 5609 mutex_enter(&tcp->tcp_eager_lock); 5610 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5611 mutex_exit(&tcp->tcp_eager_lock); 5612 TCP_STAT(tcps, tcp_listendrop); 5613 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5614 if (tcp->tcp_debug) { 5615 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5616 "tcp_conn_request: listen backlog (max=%d) " 5617 "overflow (%d pending) on %s", 5618 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5619 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5620 } 5621 goto error2; 5622 } 5623 5624 if (tcp->tcp_conn_req_cnt_q0 >= 5625 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5626 /* 5627 * Q0 is full. Drop a pending half-open req from the queue 5628 * to make room for the new SYN req. Also mark the time we 5629 * drop a SYN. 5630 * 5631 * A more aggressive defense against SYN attack will 5632 * be to set the "tcp_syn_defense" flag now. 5633 */ 5634 TCP_STAT(tcps, tcp_listendropq0); 5635 tcp->tcp_last_rcv_lbolt = lbolt64; 5636 if (!tcp_drop_q0(tcp)) { 5637 mutex_exit(&tcp->tcp_eager_lock); 5638 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5639 if (tcp->tcp_debug) { 5640 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5641 "tcp_conn_request: listen half-open queue " 5642 "(max=%d) full (%d pending) on %s", 5643 tcps->tcps_conn_req_max_q0, 5644 tcp->tcp_conn_req_cnt_q0, 5645 tcp_display(tcp, NULL, 5646 DISP_PORT_ONLY)); 5647 } 5648 goto error2; 5649 } 5650 } 5651 mutex_exit(&tcp->tcp_eager_lock); 5652 5653 /* 5654 * IP adds STRUIO_EAGER and ensures that the received packet is 5655 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5656 * link local address. If IPSec is enabled, db_struioflag has 5657 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5658 * otherwise an error case if neither of them is set. 5659 */ 5660 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5661 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5662 DB_CKSUMSTART(mp) = 0; 5663 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5664 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5665 if (econnp == NULL) 5666 goto error2; 5667 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5668 econnp->conn_sqp = new_sqp; 5669 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5670 /* 5671 * mp is updated in tcp_get_ipsec_conn(). 5672 */ 5673 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5674 if (econnp == NULL) { 5675 /* 5676 * mp freed by tcp_get_ipsec_conn. 5677 */ 5678 return; 5679 } 5680 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5681 } else { 5682 goto error2; 5683 } 5684 5685 ASSERT(DB_TYPE(mp) == M_DATA); 5686 5687 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5688 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5689 ASSERT(OK_32PTR(mp->b_rptr)); 5690 if (ipvers == IPV4_VERSION) { 5691 ipha = (ipha_t *)mp->b_rptr; 5692 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5693 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5694 } else { 5695 ip6h = (ip6_t *)mp->b_rptr; 5696 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5697 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5698 } 5699 5700 if (tcp->tcp_family == AF_INET) { 5701 ASSERT(ipvers == IPV4_VERSION); 5702 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5703 } else { 5704 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5705 } 5706 5707 if (err) 5708 goto error3; 5709 5710 eager = econnp->conn_tcp; 5711 5712 /* Inherit various TCP parameters from the listener */ 5713 eager->tcp_naglim = tcp->tcp_naglim; 5714 eager->tcp_first_timer_threshold = 5715 tcp->tcp_first_timer_threshold; 5716 eager->tcp_second_timer_threshold = 5717 tcp->tcp_second_timer_threshold; 5718 5719 eager->tcp_first_ctimer_threshold = 5720 tcp->tcp_first_ctimer_threshold; 5721 eager->tcp_second_ctimer_threshold = 5722 tcp->tcp_second_ctimer_threshold; 5723 5724 /* 5725 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5726 * If it does not, the eager's receive window will be set to the 5727 * listener's receive window later in this function. 5728 */ 5729 eager->tcp_rwnd = 0; 5730 5731 /* 5732 * Inherit listener's tcp_init_cwnd. Need to do this before 5733 * calling tcp_process_options() where tcp_mss_set() is called 5734 * to set the initial cwnd. 5735 */ 5736 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5737 5738 /* 5739 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5740 * zone id before the accept is completed in tcp_wput_accept(). 5741 */ 5742 econnp->conn_zoneid = connp->conn_zoneid; 5743 econnp->conn_allzones = connp->conn_allzones; 5744 5745 /* Copy nexthop information from listener to eager */ 5746 if (connp->conn_nexthop_set) { 5747 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5748 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5749 } 5750 5751 /* 5752 * TSOL: tsol_input_proc() needs the eager's cred before the 5753 * eager is accepted 5754 */ 5755 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5756 crhold(credp); 5757 5758 /* 5759 * If the caller has the process-wide flag set, then default to MAC 5760 * exempt mode. This allows read-down to unlabeled hosts. 5761 */ 5762 if (getpflags(NET_MAC_AWARE, credp) != 0) 5763 econnp->conn_mac_exempt = B_TRUE; 5764 5765 if (is_system_labeled()) { 5766 cred_t *cr; 5767 5768 if (connp->conn_mlp_type != mlptSingle) { 5769 cr = econnp->conn_peercred = DB_CRED(mp); 5770 if (cr != NULL) 5771 crhold(cr); 5772 else 5773 cr = econnp->conn_cred; 5774 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5775 econnp, cred_t *, cr) 5776 } else { 5777 cr = econnp->conn_cred; 5778 DTRACE_PROBE2(syn_accept, conn_t *, 5779 econnp, cred_t *, cr) 5780 } 5781 5782 if (!tcp_update_label(eager, cr)) { 5783 DTRACE_PROBE3( 5784 tx__ip__log__error__connrequest__tcp, 5785 char *, "eager connp(1) label on SYN mp(2) failed", 5786 conn_t *, econnp, mblk_t *, mp); 5787 goto error3; 5788 } 5789 } 5790 5791 eager->tcp_hard_binding = B_TRUE; 5792 5793 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5794 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5795 5796 CL_INET_CONNECT(eager); 5797 5798 /* 5799 * No need to check for multicast destination since ip will only pass 5800 * up multicasts to those that have expressed interest 5801 * TODO: what about rejecting broadcasts? 5802 * Also check that source is not a multicast or broadcast address. 5803 */ 5804 eager->tcp_state = TCPS_SYN_RCVD; 5805 5806 5807 /* 5808 * There should be no ire in the mp as we are being called after 5809 * receiving the SYN. 5810 */ 5811 ASSERT(tcp_ire_mp(mp) == NULL); 5812 5813 /* 5814 * Adapt our mss, ttl, ... according to information provided in IRE. 5815 */ 5816 5817 if (tcp_adapt_ire(eager, NULL) == 0) { 5818 /* Undo the bind_hash_insert */ 5819 tcp_bind_hash_remove(eager); 5820 goto error3; 5821 } 5822 5823 /* Process all TCP options. */ 5824 tcp_process_options(eager, tcph); 5825 5826 /* Is the other end ECN capable? */ 5827 if (tcps->tcps_ecn_permitted >= 1 && 5828 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5829 eager->tcp_ecn_ok = B_TRUE; 5830 } 5831 5832 /* 5833 * listener->tcp_rq->q_hiwat should be the default window size or a 5834 * window size changed via SO_RCVBUF option. First round up the 5835 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5836 * scale option value if needed. Call tcp_rwnd_set() to finish the 5837 * setting. 5838 * 5839 * Note if there is a rpipe metric associated with the remote host, 5840 * we should not inherit receive window size from listener. 5841 */ 5842 eager->tcp_rwnd = MSS_ROUNDUP( 5843 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5844 eager->tcp_rwnd), eager->tcp_mss); 5845 if (eager->tcp_snd_ws_ok) 5846 tcp_set_ws_value(eager); 5847 /* 5848 * Note that this is the only place tcp_rwnd_set() is called for 5849 * accepting a connection. We need to call it here instead of 5850 * after the 3-way handshake because we need to tell the other 5851 * side our rwnd in the SYN-ACK segment. 5852 */ 5853 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5854 5855 /* 5856 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5857 * via soaccept()->soinheritoptions() which essentially applies 5858 * all the listener options to the new STREAM. The options that we 5859 * need to take care of are: 5860 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5861 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5862 * SO_SNDBUF, SO_RCVBUF. 5863 * 5864 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5865 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5866 * tcp_maxpsz_set() gets called later from 5867 * tcp_accept_finish(), the option takes effect. 5868 * 5869 */ 5870 /* Set the TCP options */ 5871 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5872 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5873 eager->tcp_oobinline = tcp->tcp_oobinline; 5874 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5875 eager->tcp_broadcast = tcp->tcp_broadcast; 5876 eager->tcp_useloopback = tcp->tcp_useloopback; 5877 eager->tcp_dontroute = tcp->tcp_dontroute; 5878 eager->tcp_linger = tcp->tcp_linger; 5879 eager->tcp_lingertime = tcp->tcp_lingertime; 5880 if (tcp->tcp_ka_enabled) 5881 eager->tcp_ka_enabled = 1; 5882 5883 /* Set the IP options */ 5884 econnp->conn_broadcast = connp->conn_broadcast; 5885 econnp->conn_loopback = connp->conn_loopback; 5886 econnp->conn_dontroute = connp->conn_dontroute; 5887 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5888 5889 /* Put a ref on the listener for the eager. */ 5890 CONN_INC_REF(connp); 5891 mutex_enter(&tcp->tcp_eager_lock); 5892 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5893 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5894 tcp->tcp_eager_next_q0 = eager; 5895 eager->tcp_eager_prev_q0 = tcp; 5896 5897 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5898 eager->tcp_listener = tcp; 5899 eager->tcp_saved_listener = tcp; 5900 5901 /* 5902 * Tag this detached tcp vector for later retrieval 5903 * by our listener client in tcp_accept(). 5904 */ 5905 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5906 tcp->tcp_conn_req_cnt_q0++; 5907 if (++tcp->tcp_conn_req_seqnum == -1) { 5908 /* 5909 * -1 is "special" and defined in TPI as something 5910 * that should never be used in T_CONN_IND 5911 */ 5912 ++tcp->tcp_conn_req_seqnum; 5913 } 5914 mutex_exit(&tcp->tcp_eager_lock); 5915 5916 if (tcp->tcp_syn_defense) { 5917 /* Don't drop the SYN that comes from a good IP source */ 5918 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5919 if (addr_cache != NULL && eager->tcp_remote == 5920 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5921 eager->tcp_dontdrop = B_TRUE; 5922 } 5923 } 5924 5925 /* 5926 * We need to insert the eager in its own perimeter but as soon 5927 * as we do that, we expose the eager to the classifier and 5928 * should not touch any field outside the eager's perimeter. 5929 * So do all the work necessary before inserting the eager 5930 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5931 * will succeed but undo everything if it fails. 5932 */ 5933 seg_seq = ABE32_TO_U32(tcph->th_seq); 5934 eager->tcp_irs = seg_seq; 5935 eager->tcp_rack = seg_seq; 5936 eager->tcp_rnxt = seg_seq + 1; 5937 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5938 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5939 eager->tcp_state = TCPS_SYN_RCVD; 5940 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5941 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5942 if (mp1 == NULL) { 5943 /* 5944 * Increment the ref count as we are going to 5945 * enqueueing an mp in squeue 5946 */ 5947 CONN_INC_REF(econnp); 5948 goto error; 5949 } 5950 DB_CPID(mp1) = tcp->tcp_cpid; 5951 eager->tcp_cpid = tcp->tcp_cpid; 5952 eager->tcp_open_time = lbolt64; 5953 5954 /* 5955 * We need to start the rto timer. In normal case, we start 5956 * the timer after sending the packet on the wire (or at 5957 * least believing that packet was sent by waiting for 5958 * CALL_IP_WPUT() to return). Since this is the first packet 5959 * being sent on the wire for the eager, our initial tcp_rto 5960 * is at least tcp_rexmit_interval_min which is a fairly 5961 * large value to allow the algorithm to adjust slowly to large 5962 * fluctuations of RTT during first few transmissions. 5963 * 5964 * Starting the timer first and then sending the packet in this 5965 * case shouldn't make much difference since tcp_rexmit_interval_min 5966 * is of the order of several 100ms and starting the timer 5967 * first and then sending the packet will result in difference 5968 * of few micro seconds. 5969 * 5970 * Without this optimization, we are forced to hold the fanout 5971 * lock across the ipcl_bind_insert() and sending the packet 5972 * so that we don't race against an incoming packet (maybe RST) 5973 * for this eager. 5974 * 5975 * It is necessary to acquire an extra reference on the eager 5976 * at this point and hold it until after tcp_send_data() to 5977 * ensure against an eager close race. 5978 */ 5979 5980 CONN_INC_REF(eager->tcp_connp); 5981 5982 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5983 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5984 5985 5986 /* 5987 * Insert the eager in its own perimeter now. We are ready to deal 5988 * with any packets on eager. 5989 */ 5990 if (eager->tcp_ipversion == IPV4_VERSION) { 5991 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5992 goto error; 5993 } 5994 } else { 5995 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5996 goto error; 5997 } 5998 } 5999 6000 /* mark conn as fully-bound */ 6001 econnp->conn_fully_bound = B_TRUE; 6002 6003 /* Send the SYN-ACK */ 6004 tcp_send_data(eager, eager->tcp_wq, mp1); 6005 CONN_DEC_REF(eager->tcp_connp); 6006 freemsg(mp); 6007 6008 return; 6009 error: 6010 freemsg(mp1); 6011 eager->tcp_closemp_used = B_TRUE; 6012 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6013 squeue_fill(econnp->conn_sqp, &eager->tcp_closemp, tcp_eager_kill, 6014 econnp, SQTAG_TCP_CONN_REQ_2); 6015 6016 /* 6017 * If a connection already exists, send the mp to that connections so 6018 * that it can be appropriately dealt with. 6019 */ 6020 ipst = tcps->tcps_netstack->netstack_ip; 6021 6022 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 6023 if (!IPCL_IS_CONNECTED(econnp)) { 6024 /* 6025 * Something bad happened. ipcl_conn_insert() 6026 * failed because a connection already existed 6027 * in connected hash but we can't find it 6028 * anymore (someone blew it away). Just 6029 * free this message and hopefully remote 6030 * will retransmit at which time the SYN can be 6031 * treated as a new connection or dealth with 6032 * a TH_RST if a connection already exists. 6033 */ 6034 CONN_DEC_REF(econnp); 6035 freemsg(mp); 6036 } else { 6037 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6038 econnp, SQTAG_TCP_CONN_REQ_1); 6039 } 6040 } else { 6041 /* Nobody wants this packet */ 6042 freemsg(mp); 6043 } 6044 return; 6045 error3: 6046 CONN_DEC_REF(econnp); 6047 error2: 6048 freemsg(mp); 6049 } 6050 6051 /* 6052 * In an ideal case of vertical partition in NUMA architecture, its 6053 * beneficial to have the listener and all the incoming connections 6054 * tied to the same squeue. The other constraint is that incoming 6055 * connections should be tied to the squeue attached to interrupted 6056 * CPU for obvious locality reason so this leaves the listener to 6057 * be tied to the same squeue. Our only problem is that when listener 6058 * is binding, the CPU that will get interrupted by the NIC whose 6059 * IP address the listener is binding to is not even known. So 6060 * the code below allows us to change that binding at the time the 6061 * CPU is interrupted by virtue of incoming connection's squeue. 6062 * 6063 * This is usefull only in case of a listener bound to a specific IP 6064 * address. For other kind of listeners, they get bound the 6065 * very first time and there is no attempt to rebind them. 6066 */ 6067 void 6068 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6069 { 6070 conn_t *connp = (conn_t *)arg; 6071 squeue_t *sqp = (squeue_t *)arg2; 6072 squeue_t *new_sqp; 6073 uint32_t conn_flags; 6074 6075 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6076 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6077 } else { 6078 goto done; 6079 } 6080 6081 if (connp->conn_fanout == NULL) 6082 goto done; 6083 6084 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6085 mutex_enter(&connp->conn_fanout->connf_lock); 6086 mutex_enter(&connp->conn_lock); 6087 /* 6088 * No one from read or write side can access us now 6089 * except for already queued packets on this squeue. 6090 * But since we haven't changed the squeue yet, they 6091 * can't execute. If they are processed after we have 6092 * changed the squeue, they are sent back to the 6093 * correct squeue down below. 6094 * But a listner close can race with processing of 6095 * incoming SYN. If incoming SYN processing changes 6096 * the squeue then the listener close which is waiting 6097 * to enter the squeue would operate on the wrong 6098 * squeue. Hence we don't change the squeue here unless 6099 * the refcount is exactly the minimum refcount. The 6100 * minimum refcount of 4 is counted as - 1 each for 6101 * TCP and IP, 1 for being in the classifier hash, and 6102 * 1 for the mblk being processed. 6103 */ 6104 6105 if (connp->conn_ref != 4 || 6106 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6107 mutex_exit(&connp->conn_lock); 6108 mutex_exit(&connp->conn_fanout->connf_lock); 6109 goto done; 6110 } 6111 if (connp->conn_sqp != new_sqp) { 6112 while (connp->conn_sqp != new_sqp) 6113 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6114 } 6115 6116 do { 6117 conn_flags = connp->conn_flags; 6118 conn_flags |= IPCL_FULLY_BOUND; 6119 (void) cas32(&connp->conn_flags, connp->conn_flags, 6120 conn_flags); 6121 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6122 6123 mutex_exit(&connp->conn_fanout->connf_lock); 6124 mutex_exit(&connp->conn_lock); 6125 } 6126 6127 done: 6128 if (connp->conn_sqp != sqp) { 6129 CONN_INC_REF(connp); 6130 squeue_fill(connp->conn_sqp, mp, 6131 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6132 } else { 6133 tcp_conn_request(connp, mp, sqp); 6134 } 6135 } 6136 6137 /* 6138 * Successful connect request processing begins when our client passes 6139 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6140 * our T_OK_ACK reply message upstream. The control flow looks like this: 6141 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6142 * upstream <- tcp_rput() <- IP 6143 * After various error checks are completed, tcp_connect() lays 6144 * the target address and port into the composite header template, 6145 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6146 * request followed by an IRE request, and passes the three mblk message 6147 * down to IP looking like this: 6148 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6149 * Processing continues in tcp_rput() when we receive the following message: 6150 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6151 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6152 * to fire off the connection request, and then passes the T_OK_ACK mblk 6153 * upstream that we filled in below. There are, of course, numerous 6154 * error conditions along the way which truncate the processing described 6155 * above. 6156 */ 6157 static void 6158 tcp_connect(tcp_t *tcp, mblk_t *mp) 6159 { 6160 sin_t *sin; 6161 sin6_t *sin6; 6162 queue_t *q = tcp->tcp_wq; 6163 struct T_conn_req *tcr; 6164 ipaddr_t *dstaddrp; 6165 in_port_t dstport; 6166 uint_t srcid; 6167 6168 tcr = (struct T_conn_req *)mp->b_rptr; 6169 6170 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6171 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6172 tcp_err_ack(tcp, mp, TPROTO, 0); 6173 return; 6174 } 6175 6176 /* 6177 * Determine packet type based on type of address passed in 6178 * the request should contain an IPv4 or IPv6 address. 6179 * Make sure that address family matches the type of 6180 * family of the the address passed down 6181 */ 6182 switch (tcr->DEST_length) { 6183 default: 6184 tcp_err_ack(tcp, mp, TBADADDR, 0); 6185 return; 6186 6187 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6188 /* 6189 * XXX: The check for valid DEST_length was not there 6190 * in earlier releases and some buggy 6191 * TLI apps (e.g Sybase) got away with not feeding 6192 * in sin_zero part of address. 6193 * We allow that bug to keep those buggy apps humming. 6194 * Test suites require the check on DEST_length. 6195 * We construct a new mblk with valid DEST_length 6196 * free the original so the rest of the code does 6197 * not have to keep track of this special shorter 6198 * length address case. 6199 */ 6200 mblk_t *nmp; 6201 struct T_conn_req *ntcr; 6202 sin_t *nsin; 6203 6204 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6205 tcr->OPT_length, BPRI_HI); 6206 if (nmp == NULL) { 6207 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6208 return; 6209 } 6210 ntcr = (struct T_conn_req *)nmp->b_rptr; 6211 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6212 ntcr->PRIM_type = T_CONN_REQ; 6213 ntcr->DEST_length = sizeof (sin_t); 6214 ntcr->DEST_offset = sizeof (struct T_conn_req); 6215 6216 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6217 *nsin = sin_null; 6218 /* Get pointer to shorter address to copy from original mp */ 6219 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6220 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6221 if (sin == NULL || !OK_32PTR((char *)sin)) { 6222 freemsg(nmp); 6223 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6224 return; 6225 } 6226 nsin->sin_family = sin->sin_family; 6227 nsin->sin_port = sin->sin_port; 6228 nsin->sin_addr = sin->sin_addr; 6229 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6230 nmp->b_wptr = (uchar_t *)&nsin[1]; 6231 if (tcr->OPT_length != 0) { 6232 ntcr->OPT_length = tcr->OPT_length; 6233 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6234 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6235 (uchar_t *)ntcr + ntcr->OPT_offset, 6236 tcr->OPT_length); 6237 nmp->b_wptr += tcr->OPT_length; 6238 } 6239 freemsg(mp); /* original mp freed */ 6240 mp = nmp; /* re-initialize original variables */ 6241 tcr = ntcr; 6242 } 6243 /* FALLTHRU */ 6244 6245 case sizeof (sin_t): 6246 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6247 sizeof (sin_t)); 6248 if (sin == NULL || !OK_32PTR((char *)sin)) { 6249 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6250 return; 6251 } 6252 if (tcp->tcp_family != AF_INET || 6253 sin->sin_family != AF_INET) { 6254 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6255 return; 6256 } 6257 if (sin->sin_port == 0) { 6258 tcp_err_ack(tcp, mp, TBADADDR, 0); 6259 return; 6260 } 6261 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6262 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6263 return; 6264 } 6265 6266 break; 6267 6268 case sizeof (sin6_t): 6269 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6270 sizeof (sin6_t)); 6271 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6272 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6273 return; 6274 } 6275 if (tcp->tcp_family != AF_INET6 || 6276 sin6->sin6_family != AF_INET6) { 6277 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6278 return; 6279 } 6280 if (sin6->sin6_port == 0) { 6281 tcp_err_ack(tcp, mp, TBADADDR, 0); 6282 return; 6283 } 6284 break; 6285 } 6286 /* 6287 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6288 * should key on their sequence number and cut them loose. 6289 */ 6290 6291 /* 6292 * If options passed in, feed it for verification and handling 6293 */ 6294 if (tcr->OPT_length != 0) { 6295 mblk_t *ok_mp; 6296 mblk_t *discon_mp; 6297 mblk_t *conn_opts_mp; 6298 int t_error, sys_error, do_disconnect; 6299 6300 conn_opts_mp = NULL; 6301 6302 if (tcp_conprim_opt_process(tcp, mp, 6303 &do_disconnect, &t_error, &sys_error) < 0) { 6304 if (do_disconnect) { 6305 ASSERT(t_error == 0 && sys_error == 0); 6306 discon_mp = mi_tpi_discon_ind(NULL, 6307 ECONNREFUSED, 0); 6308 if (!discon_mp) { 6309 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6310 TSYSERR, ENOMEM); 6311 return; 6312 } 6313 ok_mp = mi_tpi_ok_ack_alloc(mp); 6314 if (!ok_mp) { 6315 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6316 TSYSERR, ENOMEM); 6317 return; 6318 } 6319 qreply(q, ok_mp); 6320 qreply(q, discon_mp); /* no flush! */ 6321 } else { 6322 ASSERT(t_error != 0); 6323 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6324 sys_error); 6325 } 6326 return; 6327 } 6328 /* 6329 * Success in setting options, the mp option buffer represented 6330 * by OPT_length/offset has been potentially modified and 6331 * contains results of option processing. We copy it in 6332 * another mp to save it for potentially influencing returning 6333 * it in T_CONN_CONN. 6334 */ 6335 if (tcr->OPT_length != 0) { /* there are resulting options */ 6336 conn_opts_mp = copyb(mp); 6337 if (!conn_opts_mp) { 6338 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6339 TSYSERR, ENOMEM); 6340 return; 6341 } 6342 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6343 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6344 /* 6345 * Note: 6346 * These resulting option negotiation can include any 6347 * end-to-end negotiation options but there no such 6348 * thing (yet?) in our TCP/IP. 6349 */ 6350 } 6351 } 6352 6353 /* 6354 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6355 * make sure that the template IP header in the tcp structure is an 6356 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6357 * need to this before we call tcp_bindi() so that the port lookup 6358 * code will look for ports in the correct port space (IPv4 and 6359 * IPv6 have separate port spaces). 6360 */ 6361 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6362 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6363 int err = 0; 6364 6365 err = tcp_header_init_ipv4(tcp); 6366 if (err != 0) { 6367 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6368 goto connect_failed; 6369 } 6370 if (tcp->tcp_lport != 0) 6371 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6372 } 6373 6374 switch (tcp->tcp_state) { 6375 case TCPS_IDLE: 6376 /* 6377 * We support quick connect, refer to comments in 6378 * tcp_connect_*() 6379 */ 6380 /* FALLTHRU */ 6381 case TCPS_BOUND: 6382 case TCPS_LISTEN: 6383 if (tcp->tcp_family == AF_INET6) { 6384 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6385 tcp_connect_ipv6(tcp, mp, 6386 &sin6->sin6_addr, 6387 sin6->sin6_port, sin6->sin6_flowinfo, 6388 sin6->__sin6_src_id, sin6->sin6_scope_id); 6389 return; 6390 } 6391 /* 6392 * Destination adress is mapped IPv6 address. 6393 * Source bound address should be unspecified or 6394 * IPv6 mapped address as well. 6395 */ 6396 if (!IN6_IS_ADDR_UNSPECIFIED( 6397 &tcp->tcp_bound_source_v6) && 6398 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6399 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6400 EADDRNOTAVAIL); 6401 break; 6402 } 6403 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6404 dstport = sin6->sin6_port; 6405 srcid = sin6->__sin6_src_id; 6406 } else { 6407 dstaddrp = &sin->sin_addr.s_addr; 6408 dstport = sin->sin_port; 6409 srcid = 0; 6410 } 6411 6412 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6413 return; 6414 default: 6415 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6416 break; 6417 } 6418 /* 6419 * Note: Code below is the "failure" case 6420 */ 6421 /* return error ack and blow away saved option results if any */ 6422 connect_failed: 6423 if (mp != NULL) 6424 putnext(tcp->tcp_rq, mp); 6425 else { 6426 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6427 TSYSERR, ENOMEM); 6428 } 6429 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6430 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6431 } 6432 6433 /* 6434 * Handle connect to IPv4 destinations, including connections for AF_INET6 6435 * sockets connecting to IPv4 mapped IPv6 destinations. 6436 */ 6437 static void 6438 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6439 uint_t srcid) 6440 { 6441 tcph_t *tcph; 6442 mblk_t *mp1; 6443 ipaddr_t dstaddr = *dstaddrp; 6444 int32_t oldstate; 6445 uint16_t lport; 6446 tcp_stack_t *tcps = tcp->tcp_tcps; 6447 6448 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6449 6450 /* Check for attempt to connect to INADDR_ANY */ 6451 if (dstaddr == INADDR_ANY) { 6452 /* 6453 * SunOS 4.x and 4.3 BSD allow an application 6454 * to connect a TCP socket to INADDR_ANY. 6455 * When they do this, the kernel picks the 6456 * address of one interface and uses it 6457 * instead. The kernel usually ends up 6458 * picking the address of the loopback 6459 * interface. This is an undocumented feature. 6460 * However, we provide the same thing here 6461 * in order to have source and binary 6462 * compatibility with SunOS 4.x. 6463 * Update the T_CONN_REQ (sin/sin6) since it is used to 6464 * generate the T_CONN_CON. 6465 */ 6466 dstaddr = htonl(INADDR_LOOPBACK); 6467 *dstaddrp = dstaddr; 6468 } 6469 6470 /* Handle __sin6_src_id if socket not bound to an IP address */ 6471 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6472 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6473 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6474 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6475 tcp->tcp_ipha->ipha_src); 6476 } 6477 6478 /* 6479 * Don't let an endpoint connect to itself. Note that 6480 * the test here does not catch the case where the 6481 * source IP addr was left unspecified by the user. In 6482 * this case, the source addr is set in tcp_adapt_ire() 6483 * using the reply to the T_BIND message that we send 6484 * down to IP here and the check is repeated in tcp_rput_other. 6485 */ 6486 if (dstaddr == tcp->tcp_ipha->ipha_src && 6487 dstport == tcp->tcp_lport) { 6488 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6489 goto failed; 6490 } 6491 6492 tcp->tcp_ipha->ipha_dst = dstaddr; 6493 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6494 6495 /* 6496 * Massage a source route if any putting the first hop 6497 * in iph_dst. Compute a starting value for the checksum which 6498 * takes into account that the original iph_dst should be 6499 * included in the checksum but that ip will include the 6500 * first hop in the source route in the tcp checksum. 6501 */ 6502 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6503 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6504 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6505 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6506 if ((int)tcp->tcp_sum < 0) 6507 tcp->tcp_sum--; 6508 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6509 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6510 (tcp->tcp_sum >> 16)); 6511 tcph = tcp->tcp_tcph; 6512 *(uint16_t *)tcph->th_fport = dstport; 6513 tcp->tcp_fport = dstport; 6514 6515 oldstate = tcp->tcp_state; 6516 /* 6517 * At this point the remote destination address and remote port fields 6518 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6519 * have to see which state tcp was in so we can take apropriate action. 6520 */ 6521 if (oldstate == TCPS_IDLE) { 6522 /* 6523 * We support a quick connect capability here, allowing 6524 * clients to transition directly from IDLE to SYN_SENT 6525 * tcp_bindi will pick an unused port, insert the connection 6526 * in the bind hash and transition to BOUND state. 6527 */ 6528 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6529 tcp, B_TRUE); 6530 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6531 B_FALSE, B_FALSE); 6532 if (lport == 0) { 6533 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6534 goto failed; 6535 } 6536 } 6537 tcp->tcp_state = TCPS_SYN_SENT; 6538 6539 /* 6540 * TODO: allow data with connect requests 6541 * by unlinking M_DATA trailers here and 6542 * linking them in behind the T_OK_ACK mblk. 6543 * The tcp_rput() bind ack handler would then 6544 * feed them to tcp_wput_data() rather than call 6545 * tcp_timer(). 6546 */ 6547 mp = mi_tpi_ok_ack_alloc(mp); 6548 if (!mp) { 6549 tcp->tcp_state = oldstate; 6550 goto failed; 6551 } 6552 if (tcp->tcp_family == AF_INET) { 6553 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6554 sizeof (ipa_conn_t)); 6555 } else { 6556 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6557 sizeof (ipa6_conn_t)); 6558 } 6559 if (mp1) { 6560 /* 6561 * We need to make sure that the conn_recv is set to a non-null 6562 * value before we insert the conn_t into the classifier table. 6563 * This is to avoid a race with an incoming packet which does 6564 * an ipcl_classify(). 6565 */ 6566 tcp->tcp_connp->conn_recv = tcp_input; 6567 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 /* 6762 * We need to make sure that the conn_recv is set to a non-null 6763 * value before we insert the conn_t into the classifier table. 6764 * This is to avoid a race with an incoming packet which does 6765 * an ipcl_classify(). 6766 */ 6767 tcp->tcp_connp->conn_recv = tcp_input; 6768 6769 /* Hang onto the T_OK_ACK for later. */ 6770 linkb(mp1, mp); 6771 mblk_setcred(mp1, tcp->tcp_cred); 6772 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6773 &tcp->tcp_sticky_ipp); 6774 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6775 tcp->tcp_active_open = 1; 6776 /* ip_bind_v6() may return ACK or ERROR */ 6777 if (mp1 != NULL) 6778 tcp_rput_other(tcp, mp1); 6779 return; 6780 } 6781 /* Error case */ 6782 tcp->tcp_state = oldstate; 6783 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6784 6785 failed: 6786 /* return error ack and blow away saved option results if any */ 6787 if (mp != NULL) 6788 putnext(tcp->tcp_rq, mp); 6789 else { 6790 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6791 TSYSERR, ENOMEM); 6792 } 6793 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6794 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6795 } 6796 6797 /* 6798 * We need a stream q for detached closing tcp connections 6799 * to use. Our client hereby indicates that this q is the 6800 * one to use. 6801 */ 6802 static void 6803 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6804 { 6805 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6806 queue_t *q = tcp->tcp_wq; 6807 tcp_stack_t *tcps = tcp->tcp_tcps; 6808 6809 #ifdef NS_DEBUG 6810 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6811 tcps->tcps_netstack->netstack_stackid); 6812 #endif 6813 mp->b_datap->db_type = M_IOCACK; 6814 iocp->ioc_count = 0; 6815 mutex_enter(&tcps->tcps_g_q_lock); 6816 if (tcps->tcps_g_q != NULL) { 6817 mutex_exit(&tcps->tcps_g_q_lock); 6818 iocp->ioc_error = EALREADY; 6819 } else { 6820 mblk_t *mp1; 6821 6822 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6823 if (mp1 == NULL) { 6824 mutex_exit(&tcps->tcps_g_q_lock); 6825 iocp->ioc_error = ENOMEM; 6826 } else { 6827 tcps->tcps_g_q = tcp->tcp_rq; 6828 mutex_exit(&tcps->tcps_g_q_lock); 6829 iocp->ioc_error = 0; 6830 iocp->ioc_rval = 0; 6831 /* 6832 * We are passing tcp_sticky_ipp as NULL 6833 * as it is not useful for tcp_default queue 6834 * 6835 * Set conn_recv just in case. 6836 */ 6837 tcp->tcp_connp->conn_recv = tcp_conn_request; 6838 6839 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6840 if (mp1 != NULL) 6841 tcp_rput_other(tcp, mp1); 6842 } 6843 } 6844 qreply(q, mp); 6845 } 6846 6847 /* 6848 * Our client hereby directs us to reject the connection request 6849 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6850 * of sending the appropriate RST, not an ICMP error. 6851 */ 6852 static void 6853 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6854 { 6855 tcp_t *ltcp = NULL; 6856 t_scalar_t seqnum; 6857 conn_t *connp; 6858 tcp_stack_t *tcps = tcp->tcp_tcps; 6859 6860 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6861 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6862 tcp_err_ack(tcp, mp, TPROTO, 0); 6863 return; 6864 } 6865 6866 /* 6867 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6868 * when the stream is in BOUND state. Do not send a reset, 6869 * since the destination IP address is not valid, and it can 6870 * be the initialized value of all zeros (broadcast address). 6871 * 6872 * If TCP has sent down a bind request to IP and has not 6873 * received the reply, reject the request. Otherwise, TCP 6874 * will be confused. 6875 */ 6876 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6877 if (tcp->tcp_debug) { 6878 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6879 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6880 } 6881 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6882 return; 6883 } 6884 6885 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6886 6887 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6888 6889 /* 6890 * According to TPI, for non-listeners, ignore seqnum 6891 * and disconnect. 6892 * Following interpretation of -1 seqnum is historical 6893 * and implied TPI ? (TPI only states that for T_CONN_IND, 6894 * a valid seqnum should not be -1). 6895 * 6896 * -1 means disconnect everything 6897 * regardless even on a listener. 6898 */ 6899 6900 int old_state = tcp->tcp_state; 6901 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6902 6903 /* 6904 * The connection can't be on the tcp_time_wait_head list 6905 * since it is not detached. 6906 */ 6907 ASSERT(tcp->tcp_time_wait_next == NULL); 6908 ASSERT(tcp->tcp_time_wait_prev == NULL); 6909 ASSERT(tcp->tcp_time_wait_expire == 0); 6910 ltcp = NULL; 6911 /* 6912 * If it used to be a listener, check to make sure no one else 6913 * has taken the port before switching back to LISTEN state. 6914 */ 6915 if (tcp->tcp_ipversion == IPV4_VERSION) { 6916 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6917 tcp->tcp_ipha->ipha_src, 6918 tcp->tcp_connp->conn_zoneid, ipst); 6919 if (connp != NULL) 6920 ltcp = connp->conn_tcp; 6921 } else { 6922 /* Allow tcp_bound_if listeners? */ 6923 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6924 &tcp->tcp_ip6h->ip6_src, 0, 6925 tcp->tcp_connp->conn_zoneid, ipst); 6926 if (connp != NULL) 6927 ltcp = connp->conn_tcp; 6928 } 6929 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6930 tcp->tcp_state = TCPS_LISTEN; 6931 } else if (old_state > TCPS_BOUND) { 6932 tcp->tcp_conn_req_max = 0; 6933 tcp->tcp_state = TCPS_BOUND; 6934 } 6935 if (ltcp != NULL) 6936 CONN_DEC_REF(ltcp->tcp_connp); 6937 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6938 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6939 } else if (old_state == TCPS_ESTABLISHED || 6940 old_state == TCPS_CLOSE_WAIT) { 6941 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6942 } 6943 6944 if (tcp->tcp_fused) 6945 tcp_unfuse(tcp); 6946 6947 mutex_enter(&tcp->tcp_eager_lock); 6948 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6949 (tcp->tcp_conn_req_cnt_q != 0)) { 6950 tcp_eager_cleanup(tcp, 0); 6951 } 6952 mutex_exit(&tcp->tcp_eager_lock); 6953 6954 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6955 tcp->tcp_rnxt, TH_RST | TH_ACK); 6956 6957 tcp_reinit(tcp); 6958 6959 if (old_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 return; 6967 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6968 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6969 return; 6970 } 6971 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6972 /* Send M_FLUSH according to TPI */ 6973 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6974 } 6975 mp = mi_tpi_ok_ack_alloc(mp); 6976 if (mp) 6977 putnext(tcp->tcp_rq, mp); 6978 } 6979 6980 /* 6981 * Diagnostic routine used to return a string associated with the tcp state. 6982 * Note that if the caller does not supply a buffer, it will use an internal 6983 * static string. This means that if multiple threads call this function at 6984 * the same time, output can be corrupted... Note also that this function 6985 * does not check the size of the supplied buffer. The caller has to make 6986 * sure that it is big enough. 6987 */ 6988 static char * 6989 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6990 { 6991 char buf1[30]; 6992 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6993 char *buf; 6994 char *cp; 6995 in6_addr_t local, remote; 6996 char local_addrbuf[INET6_ADDRSTRLEN]; 6997 char remote_addrbuf[INET6_ADDRSTRLEN]; 6998 6999 if (sup_buf != NULL) 7000 buf = sup_buf; 7001 else 7002 buf = priv_buf; 7003 7004 if (tcp == NULL) 7005 return ("NULL_TCP"); 7006 switch (tcp->tcp_state) { 7007 case TCPS_CLOSED: 7008 cp = "TCP_CLOSED"; 7009 break; 7010 case TCPS_IDLE: 7011 cp = "TCP_IDLE"; 7012 break; 7013 case TCPS_BOUND: 7014 cp = "TCP_BOUND"; 7015 break; 7016 case TCPS_LISTEN: 7017 cp = "TCP_LISTEN"; 7018 break; 7019 case TCPS_SYN_SENT: 7020 cp = "TCP_SYN_SENT"; 7021 break; 7022 case TCPS_SYN_RCVD: 7023 cp = "TCP_SYN_RCVD"; 7024 break; 7025 case TCPS_ESTABLISHED: 7026 cp = "TCP_ESTABLISHED"; 7027 break; 7028 case TCPS_CLOSE_WAIT: 7029 cp = "TCP_CLOSE_WAIT"; 7030 break; 7031 case TCPS_FIN_WAIT_1: 7032 cp = "TCP_FIN_WAIT_1"; 7033 break; 7034 case TCPS_CLOSING: 7035 cp = "TCP_CLOSING"; 7036 break; 7037 case TCPS_LAST_ACK: 7038 cp = "TCP_LAST_ACK"; 7039 break; 7040 case TCPS_FIN_WAIT_2: 7041 cp = "TCP_FIN_WAIT_2"; 7042 break; 7043 case TCPS_TIME_WAIT: 7044 cp = "TCP_TIME_WAIT"; 7045 break; 7046 default: 7047 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7048 cp = buf1; 7049 break; 7050 } 7051 switch (format) { 7052 case DISP_ADDR_AND_PORT: 7053 if (tcp->tcp_ipversion == IPV4_VERSION) { 7054 /* 7055 * Note that we use the remote address in the tcp_b 7056 * structure. This means that it will print out 7057 * the real destination address, not the next hop's 7058 * address if source routing is used. 7059 */ 7060 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7061 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7062 7063 } else { 7064 local = tcp->tcp_ip_src_v6; 7065 remote = tcp->tcp_remote_v6; 7066 } 7067 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7068 sizeof (local_addrbuf)); 7069 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7070 sizeof (remote_addrbuf)); 7071 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7072 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7073 ntohs(tcp->tcp_fport), cp); 7074 break; 7075 case DISP_PORT_ONLY: 7076 default: 7077 (void) mi_sprintf(buf, "[%u, %u] %s", 7078 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7079 break; 7080 } 7081 7082 return (buf); 7083 } 7084 7085 /* 7086 * Called via squeue to get on to eager's perimeter. It sends a 7087 * TH_RST if eager is in the fanout table. The listener wants the 7088 * eager to disappear either by means of tcp_eager_blowoff() or 7089 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 7090 * called (via squeue) if the eager cannot be inserted in the 7091 * fanout table in tcp_conn_request(). 7092 */ 7093 /* ARGSUSED */ 7094 void 7095 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7096 { 7097 conn_t *econnp = (conn_t *)arg; 7098 tcp_t *eager = econnp->conn_tcp; 7099 tcp_t *listener = eager->tcp_listener; 7100 tcp_stack_t *tcps = eager->tcp_tcps; 7101 7102 /* 7103 * We could be called because listener is closing. Since 7104 * the eager is using listener's queue's, its not safe. 7105 * Better use the default queue just to send the TH_RST 7106 * out. 7107 */ 7108 ASSERT(tcps->tcps_g_q != NULL); 7109 eager->tcp_rq = tcps->tcps_g_q; 7110 eager->tcp_wq = WR(tcps->tcps_g_q); 7111 7112 /* 7113 * An eager's conn_fanout will be NULL if it's a duplicate 7114 * for an existing 4-tuples in the conn fanout table. 7115 * We don't want to send an RST out in such case. 7116 */ 7117 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 7118 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7119 eager, eager->tcp_snxt, 0, TH_RST); 7120 } 7121 7122 /* We are here because listener wants this eager gone */ 7123 if (listener != NULL) { 7124 mutex_enter(&listener->tcp_eager_lock); 7125 tcp_eager_unlink(eager); 7126 if (eager->tcp_tconnind_started) { 7127 /* 7128 * The eager has sent a conn_ind up to the 7129 * listener but listener decides to close 7130 * instead. We need to drop the extra ref 7131 * placed on eager in tcp_rput_data() before 7132 * sending the conn_ind to listener. 7133 */ 7134 CONN_DEC_REF(econnp); 7135 } 7136 mutex_exit(&listener->tcp_eager_lock); 7137 CONN_DEC_REF(listener->tcp_connp); 7138 } 7139 7140 if (eager->tcp_state > TCPS_BOUND) 7141 tcp_close_detached(eager); 7142 } 7143 7144 /* 7145 * Reset any eager connection hanging off this listener marked 7146 * with 'seqnum' and then reclaim it's resources. 7147 */ 7148 static boolean_t 7149 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7150 { 7151 tcp_t *eager; 7152 mblk_t *mp; 7153 tcp_stack_t *tcps = listener->tcp_tcps; 7154 7155 TCP_STAT(tcps, tcp_eager_blowoff_calls); 7156 eager = listener; 7157 mutex_enter(&listener->tcp_eager_lock); 7158 do { 7159 eager = eager->tcp_eager_next_q; 7160 if (eager == NULL) { 7161 mutex_exit(&listener->tcp_eager_lock); 7162 return (B_FALSE); 7163 } 7164 } while (eager->tcp_conn_req_seqnum != seqnum); 7165 7166 if (eager->tcp_closemp_used) { 7167 mutex_exit(&listener->tcp_eager_lock); 7168 return (B_TRUE); 7169 } 7170 eager->tcp_closemp_used = B_TRUE; 7171 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7172 CONN_INC_REF(eager->tcp_connp); 7173 mutex_exit(&listener->tcp_eager_lock); 7174 mp = &eager->tcp_closemp; 7175 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7176 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7177 return (B_TRUE); 7178 } 7179 7180 /* 7181 * Reset any eager connection hanging off this listener 7182 * and then reclaim it's resources. 7183 */ 7184 static void 7185 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7186 { 7187 tcp_t *eager; 7188 mblk_t *mp; 7189 tcp_stack_t *tcps = listener->tcp_tcps; 7190 7191 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7192 7193 if (!q0_only) { 7194 /* First cleanup q */ 7195 TCP_STAT(tcps, tcp_eager_blowoff_q); 7196 eager = listener->tcp_eager_next_q; 7197 while (eager != NULL) { 7198 if (!eager->tcp_closemp_used) { 7199 eager->tcp_closemp_used = B_TRUE; 7200 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7201 CONN_INC_REF(eager->tcp_connp); 7202 mp = &eager->tcp_closemp; 7203 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7204 tcp_eager_kill, eager->tcp_connp, 7205 SQTAG_TCP_EAGER_CLEANUP); 7206 } 7207 eager = eager->tcp_eager_next_q; 7208 } 7209 } 7210 /* Then cleanup q0 */ 7211 TCP_STAT(tcps, tcp_eager_blowoff_q0); 7212 eager = listener->tcp_eager_next_q0; 7213 while (eager != listener) { 7214 if (!eager->tcp_closemp_used) { 7215 eager->tcp_closemp_used = B_TRUE; 7216 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7217 CONN_INC_REF(eager->tcp_connp); 7218 mp = &eager->tcp_closemp; 7219 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7220 tcp_eager_kill, eager->tcp_connp, 7221 SQTAG_TCP_EAGER_CLEANUP_Q0); 7222 } 7223 eager = eager->tcp_eager_next_q0; 7224 } 7225 } 7226 7227 /* 7228 * If we are an eager connection hanging off a listener that hasn't 7229 * formally accepted the connection yet, get off his list and blow off 7230 * any data that we have accumulated. 7231 */ 7232 static void 7233 tcp_eager_unlink(tcp_t *tcp) 7234 { 7235 tcp_t *listener = tcp->tcp_listener; 7236 7237 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7238 ASSERT(listener != NULL); 7239 if (tcp->tcp_eager_next_q0 != NULL) { 7240 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7241 7242 /* Remove the eager tcp from q0 */ 7243 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7244 tcp->tcp_eager_prev_q0; 7245 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7246 tcp->tcp_eager_next_q0; 7247 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7248 listener->tcp_conn_req_cnt_q0--; 7249 7250 tcp->tcp_eager_next_q0 = NULL; 7251 tcp->tcp_eager_prev_q0 = NULL; 7252 7253 /* 7254 * Take the eager out, if it is in the list of droppable 7255 * eagers. 7256 */ 7257 MAKE_UNDROPPABLE(tcp); 7258 7259 if (tcp->tcp_syn_rcvd_timeout != 0) { 7260 /* we have timed out before */ 7261 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7262 listener->tcp_syn_rcvd_timeout--; 7263 } 7264 } else { 7265 tcp_t **tcpp = &listener->tcp_eager_next_q; 7266 tcp_t *prev = NULL; 7267 7268 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7269 if (tcpp[0] == tcp) { 7270 if (listener->tcp_eager_last_q == tcp) { 7271 /* 7272 * If we are unlinking the last 7273 * element on the list, adjust 7274 * tail pointer. Set tail pointer 7275 * to nil when list is empty. 7276 */ 7277 ASSERT(tcp->tcp_eager_next_q == NULL); 7278 if (listener->tcp_eager_last_q == 7279 listener->tcp_eager_next_q) { 7280 listener->tcp_eager_last_q = 7281 NULL; 7282 } else { 7283 /* 7284 * We won't get here if there 7285 * is only one eager in the 7286 * list. 7287 */ 7288 ASSERT(prev != NULL); 7289 listener->tcp_eager_last_q = 7290 prev; 7291 } 7292 } 7293 tcpp[0] = tcp->tcp_eager_next_q; 7294 tcp->tcp_eager_next_q = NULL; 7295 tcp->tcp_eager_last_q = NULL; 7296 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7297 listener->tcp_conn_req_cnt_q--; 7298 break; 7299 } 7300 prev = tcpp[0]; 7301 } 7302 } 7303 tcp->tcp_listener = NULL; 7304 } 7305 7306 /* Shorthand to generate and send TPI error acks to our client */ 7307 static void 7308 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7309 { 7310 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7311 putnext(tcp->tcp_rq, mp); 7312 } 7313 7314 /* Shorthand to generate and send TPI error acks to our client */ 7315 static void 7316 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7317 int t_error, int sys_error) 7318 { 7319 struct T_error_ack *teackp; 7320 7321 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7322 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7323 teackp = (struct T_error_ack *)mp->b_rptr; 7324 teackp->ERROR_prim = primitive; 7325 teackp->TLI_error = t_error; 7326 teackp->UNIX_error = sys_error; 7327 putnext(tcp->tcp_rq, mp); 7328 } 7329 } 7330 7331 /* 7332 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7333 * but instead the code relies on: 7334 * - the fact that the address of the array and its size never changes 7335 * - the atomic assignment of the elements of the array 7336 */ 7337 /* ARGSUSED */ 7338 static int 7339 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7340 { 7341 int i; 7342 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7343 7344 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7345 if (tcps->tcps_g_epriv_ports[i] != 0) 7346 (void) mi_mpprintf(mp, "%d ", 7347 tcps->tcps_g_epriv_ports[i]); 7348 } 7349 return (0); 7350 } 7351 7352 /* 7353 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7354 * threads from changing it at the same time. 7355 */ 7356 /* ARGSUSED */ 7357 static int 7358 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7359 cred_t *cr) 7360 { 7361 long new_value; 7362 int i; 7363 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7364 7365 /* 7366 * Fail the request if the new value does not lie within the 7367 * port number limits. 7368 */ 7369 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7370 new_value <= 0 || new_value >= 65536) { 7371 return (EINVAL); 7372 } 7373 7374 mutex_enter(&tcps->tcps_epriv_port_lock); 7375 /* Check if the value is already in the list */ 7376 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7377 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7378 mutex_exit(&tcps->tcps_epriv_port_lock); 7379 return (EEXIST); 7380 } 7381 } 7382 /* Find an empty slot */ 7383 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7384 if (tcps->tcps_g_epriv_ports[i] == 0) 7385 break; 7386 } 7387 if (i == tcps->tcps_g_num_epriv_ports) { 7388 mutex_exit(&tcps->tcps_epriv_port_lock); 7389 return (EOVERFLOW); 7390 } 7391 /* Set the new value */ 7392 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7393 mutex_exit(&tcps->tcps_epriv_port_lock); 7394 return (0); 7395 } 7396 7397 /* 7398 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7399 * threads from changing it at the same time. 7400 */ 7401 /* ARGSUSED */ 7402 static int 7403 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7404 cred_t *cr) 7405 { 7406 long new_value; 7407 int i; 7408 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7409 7410 /* 7411 * Fail the request if the new value does not lie within the 7412 * port number limits. 7413 */ 7414 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7415 new_value >= 65536) { 7416 return (EINVAL); 7417 } 7418 7419 mutex_enter(&tcps->tcps_epriv_port_lock); 7420 /* Check that the value is already in the list */ 7421 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7422 if (tcps->tcps_g_epriv_ports[i] == new_value) 7423 break; 7424 } 7425 if (i == tcps->tcps_g_num_epriv_ports) { 7426 mutex_exit(&tcps->tcps_epriv_port_lock); 7427 return (ESRCH); 7428 } 7429 /* Clear the value */ 7430 tcps->tcps_g_epriv_ports[i] = 0; 7431 mutex_exit(&tcps->tcps_epriv_port_lock); 7432 return (0); 7433 } 7434 7435 /* Return the TPI/TLI equivalent of our current tcp_state */ 7436 static int 7437 tcp_tpistate(tcp_t *tcp) 7438 { 7439 switch (tcp->tcp_state) { 7440 case TCPS_IDLE: 7441 return (TS_UNBND); 7442 case TCPS_LISTEN: 7443 /* 7444 * Return whether there are outstanding T_CONN_IND waiting 7445 * for the matching T_CONN_RES. Therefore don't count q0. 7446 */ 7447 if (tcp->tcp_conn_req_cnt_q > 0) 7448 return (TS_WRES_CIND); 7449 else 7450 return (TS_IDLE); 7451 case TCPS_BOUND: 7452 return (TS_IDLE); 7453 case TCPS_SYN_SENT: 7454 return (TS_WCON_CREQ); 7455 case TCPS_SYN_RCVD: 7456 /* 7457 * Note: assumption: this has to the active open SYN_RCVD. 7458 * The passive instance is detached in SYN_RCVD stage of 7459 * incoming connection processing so we cannot get request 7460 * for T_info_ack on it. 7461 */ 7462 return (TS_WACK_CRES); 7463 case TCPS_ESTABLISHED: 7464 return (TS_DATA_XFER); 7465 case TCPS_CLOSE_WAIT: 7466 return (TS_WREQ_ORDREL); 7467 case TCPS_FIN_WAIT_1: 7468 return (TS_WIND_ORDREL); 7469 case TCPS_FIN_WAIT_2: 7470 return (TS_WIND_ORDREL); 7471 7472 case TCPS_CLOSING: 7473 case TCPS_LAST_ACK: 7474 case TCPS_TIME_WAIT: 7475 case TCPS_CLOSED: 7476 /* 7477 * Following TS_WACK_DREQ7 is a rendition of "not 7478 * yet TS_IDLE" TPI state. There is no best match to any 7479 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7480 * choose a value chosen that will map to TLI/XTI level 7481 * state of TSTATECHNG (state is process of changing) which 7482 * captures what this dummy state represents. 7483 */ 7484 return (TS_WACK_DREQ7); 7485 default: 7486 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7487 tcp->tcp_state, tcp_display(tcp, NULL, 7488 DISP_PORT_ONLY)); 7489 return (TS_UNBND); 7490 } 7491 } 7492 7493 static void 7494 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7495 { 7496 tcp_stack_t *tcps = tcp->tcp_tcps; 7497 7498 if (tcp->tcp_family == AF_INET6) 7499 *tia = tcp_g_t_info_ack_v6; 7500 else 7501 *tia = tcp_g_t_info_ack; 7502 tia->CURRENT_state = tcp_tpistate(tcp); 7503 tia->OPT_size = tcp_max_optsize; 7504 if (tcp->tcp_mss == 0) { 7505 /* Not yet set - tcp_open does not set mss */ 7506 if (tcp->tcp_ipversion == IPV4_VERSION) 7507 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7508 else 7509 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7510 } else { 7511 tia->TIDU_size = tcp->tcp_mss; 7512 } 7513 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7514 } 7515 7516 /* 7517 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7518 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7519 * tcp_g_t_info_ack. The current state of the stream is copied from 7520 * tcp_state. 7521 */ 7522 static void 7523 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7524 { 7525 t_uscalar_t cap_bits1; 7526 struct T_capability_ack *tcap; 7527 7528 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7529 freemsg(mp); 7530 return; 7531 } 7532 7533 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7534 7535 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7536 mp->b_datap->db_type, T_CAPABILITY_ACK); 7537 if (mp == NULL) 7538 return; 7539 7540 tcap = (struct T_capability_ack *)mp->b_rptr; 7541 tcap->CAP_bits1 = 0; 7542 7543 if (cap_bits1 & TC1_INFO) { 7544 tcp_copy_info(&tcap->INFO_ack, tcp); 7545 tcap->CAP_bits1 |= TC1_INFO; 7546 } 7547 7548 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7549 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7550 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7551 } 7552 7553 putnext(tcp->tcp_rq, mp); 7554 } 7555 7556 /* 7557 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7558 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7559 * The current state of the stream is copied from tcp_state. 7560 */ 7561 static void 7562 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7563 { 7564 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7565 T_INFO_ACK); 7566 if (!mp) { 7567 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7568 return; 7569 } 7570 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7571 putnext(tcp->tcp_rq, mp); 7572 } 7573 7574 /* Respond to the TPI addr request */ 7575 static void 7576 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7577 { 7578 sin_t *sin; 7579 mblk_t *ackmp; 7580 struct T_addr_ack *taa; 7581 7582 /* Make it large enough for worst case */ 7583 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7584 2 * sizeof (sin6_t), 1); 7585 if (ackmp == NULL) { 7586 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7587 return; 7588 } 7589 7590 if (tcp->tcp_ipversion == IPV6_VERSION) { 7591 tcp_addr_req_ipv6(tcp, ackmp); 7592 return; 7593 } 7594 taa = (struct T_addr_ack *)ackmp->b_rptr; 7595 7596 bzero(taa, sizeof (struct T_addr_ack)); 7597 ackmp->b_wptr = (uchar_t *)&taa[1]; 7598 7599 taa->PRIM_type = T_ADDR_ACK; 7600 ackmp->b_datap->db_type = M_PCPROTO; 7601 7602 /* 7603 * Note: Following code assumes 32 bit alignment of basic 7604 * data structures like sin_t and struct T_addr_ack. 7605 */ 7606 if (tcp->tcp_state >= TCPS_BOUND) { 7607 /* 7608 * Fill in local address 7609 */ 7610 taa->LOCADDR_length = sizeof (sin_t); 7611 taa->LOCADDR_offset = sizeof (*taa); 7612 7613 sin = (sin_t *)&taa[1]; 7614 7615 /* Fill zeroes and then intialize non-zero fields */ 7616 *sin = sin_null; 7617 7618 sin->sin_family = AF_INET; 7619 7620 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7621 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7622 7623 ackmp->b_wptr = (uchar_t *)&sin[1]; 7624 7625 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7626 /* 7627 * Fill in Remote address 7628 */ 7629 taa->REMADDR_length = sizeof (sin_t); 7630 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7631 taa->LOCADDR_length); 7632 7633 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7634 *sin = sin_null; 7635 sin->sin_family = AF_INET; 7636 sin->sin_addr.s_addr = tcp->tcp_remote; 7637 sin->sin_port = tcp->tcp_fport; 7638 7639 ackmp->b_wptr = (uchar_t *)&sin[1]; 7640 } 7641 } 7642 putnext(tcp->tcp_rq, ackmp); 7643 } 7644 7645 /* Assumes that tcp_addr_req gets enough space and alignment */ 7646 static void 7647 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7648 { 7649 sin6_t *sin6; 7650 struct T_addr_ack *taa; 7651 7652 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7653 ASSERT(OK_32PTR(ackmp->b_rptr)); 7654 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7655 2 * sizeof (sin6_t)); 7656 7657 taa = (struct T_addr_ack *)ackmp->b_rptr; 7658 7659 bzero(taa, sizeof (struct T_addr_ack)); 7660 ackmp->b_wptr = (uchar_t *)&taa[1]; 7661 7662 taa->PRIM_type = T_ADDR_ACK; 7663 ackmp->b_datap->db_type = M_PCPROTO; 7664 7665 /* 7666 * Note: Following code assumes 32 bit alignment of basic 7667 * data structures like sin6_t and struct T_addr_ack. 7668 */ 7669 if (tcp->tcp_state >= TCPS_BOUND) { 7670 /* 7671 * Fill in local address 7672 */ 7673 taa->LOCADDR_length = sizeof (sin6_t); 7674 taa->LOCADDR_offset = sizeof (*taa); 7675 7676 sin6 = (sin6_t *)&taa[1]; 7677 *sin6 = sin6_null; 7678 7679 sin6->sin6_family = AF_INET6; 7680 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7681 sin6->sin6_port = tcp->tcp_lport; 7682 7683 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7684 7685 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7686 /* 7687 * Fill in Remote address 7688 */ 7689 taa->REMADDR_length = sizeof (sin6_t); 7690 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7691 taa->LOCADDR_length); 7692 7693 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7694 *sin6 = sin6_null; 7695 sin6->sin6_family = AF_INET6; 7696 sin6->sin6_flowinfo = 7697 tcp->tcp_ip6h->ip6_vcf & 7698 ~IPV6_VERS_AND_FLOW_MASK; 7699 sin6->sin6_addr = tcp->tcp_remote_v6; 7700 sin6->sin6_port = tcp->tcp_fport; 7701 7702 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7703 } 7704 } 7705 putnext(tcp->tcp_rq, ackmp); 7706 } 7707 7708 /* 7709 * Handle reinitialization of a tcp structure. 7710 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7711 */ 7712 static void 7713 tcp_reinit(tcp_t *tcp) 7714 { 7715 mblk_t *mp; 7716 int err; 7717 tcp_stack_t *tcps = tcp->tcp_tcps; 7718 7719 TCP_STAT(tcps, tcp_reinit_calls); 7720 7721 /* tcp_reinit should never be called for detached tcp_t's */ 7722 ASSERT(tcp->tcp_listener == NULL); 7723 ASSERT((tcp->tcp_family == AF_INET && 7724 tcp->tcp_ipversion == IPV4_VERSION) || 7725 (tcp->tcp_family == AF_INET6 && 7726 (tcp->tcp_ipversion == IPV4_VERSION || 7727 tcp->tcp_ipversion == IPV6_VERSION))); 7728 7729 /* Cancel outstanding timers */ 7730 tcp_timers_stop(tcp); 7731 7732 /* 7733 * Reset everything in the state vector, after updating global 7734 * MIB data from instance counters. 7735 */ 7736 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7737 tcp->tcp_ibsegs = 0; 7738 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7739 tcp->tcp_obsegs = 0; 7740 7741 tcp_close_mpp(&tcp->tcp_xmit_head); 7742 if (tcp->tcp_snd_zcopy_aware) 7743 tcp_zcopy_notify(tcp); 7744 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7745 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7746 mutex_enter(&tcp->tcp_non_sq_lock); 7747 if (tcp->tcp_flow_stopped && 7748 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7749 tcp_clrqfull(tcp); 7750 } 7751 mutex_exit(&tcp->tcp_non_sq_lock); 7752 tcp_close_mpp(&tcp->tcp_reass_head); 7753 tcp->tcp_reass_tail = NULL; 7754 if (tcp->tcp_rcv_list != NULL) { 7755 /* Free b_next chain */ 7756 tcp_close_mpp(&tcp->tcp_rcv_list); 7757 tcp->tcp_rcv_last_head = NULL; 7758 tcp->tcp_rcv_last_tail = NULL; 7759 tcp->tcp_rcv_cnt = 0; 7760 } 7761 tcp->tcp_rcv_last_tail = NULL; 7762 7763 if ((mp = tcp->tcp_urp_mp) != NULL) { 7764 freemsg(mp); 7765 tcp->tcp_urp_mp = NULL; 7766 } 7767 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7768 freemsg(mp); 7769 tcp->tcp_urp_mark_mp = NULL; 7770 } 7771 if (tcp->tcp_fused_sigurg_mp != NULL) { 7772 freeb(tcp->tcp_fused_sigurg_mp); 7773 tcp->tcp_fused_sigurg_mp = NULL; 7774 } 7775 7776 /* 7777 * Following is a union with two members which are 7778 * identical types and size so the following cleanup 7779 * is enough. 7780 */ 7781 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7782 7783 CL_INET_DISCONNECT(tcp); 7784 7785 /* 7786 * The connection can't be on the tcp_time_wait_head list 7787 * since it is not detached. 7788 */ 7789 ASSERT(tcp->tcp_time_wait_next == NULL); 7790 ASSERT(tcp->tcp_time_wait_prev == NULL); 7791 ASSERT(tcp->tcp_time_wait_expire == 0); 7792 7793 if (tcp->tcp_kssl_pending) { 7794 tcp->tcp_kssl_pending = B_FALSE; 7795 7796 /* Don't reset if the initialized by bind. */ 7797 if (tcp->tcp_kssl_ent != NULL) { 7798 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7799 KSSL_NO_PROXY); 7800 } 7801 } 7802 if (tcp->tcp_kssl_ctx != NULL) { 7803 kssl_release_ctx(tcp->tcp_kssl_ctx); 7804 tcp->tcp_kssl_ctx = NULL; 7805 } 7806 7807 /* 7808 * Reset/preserve other values 7809 */ 7810 tcp_reinit_values(tcp); 7811 ipcl_hash_remove(tcp->tcp_connp); 7812 conn_delete_ire(tcp->tcp_connp, NULL); 7813 tcp_ipsec_cleanup(tcp); 7814 7815 if (tcp->tcp_conn_req_max != 0) { 7816 /* 7817 * This is the case when a TLI program uses the same 7818 * transport end point to accept a connection. This 7819 * makes the TCP both a listener and acceptor. When 7820 * this connection is closed, we need to set the state 7821 * back to TCPS_LISTEN. Make sure that the eager list 7822 * is reinitialized. 7823 * 7824 * Note that this stream is still bound to the four 7825 * tuples of the previous connection in IP. If a new 7826 * SYN with different foreign address comes in, IP will 7827 * not find it and will send it to the global queue. In 7828 * the global queue, TCP will do a tcp_lookup_listener() 7829 * to find this stream. This works because this stream 7830 * is only removed from connected hash. 7831 * 7832 */ 7833 tcp->tcp_state = TCPS_LISTEN; 7834 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7835 tcp->tcp_eager_next_drop_q0 = tcp; 7836 tcp->tcp_eager_prev_drop_q0 = tcp; 7837 tcp->tcp_connp->conn_recv = tcp_conn_request; 7838 if (tcp->tcp_family == AF_INET6) { 7839 ASSERT(tcp->tcp_connp->conn_af_isv6); 7840 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7841 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7842 } else { 7843 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7844 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7845 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7846 } 7847 } else { 7848 tcp->tcp_state = TCPS_BOUND; 7849 } 7850 7851 /* 7852 * Initialize to default values 7853 * Can't fail since enough header template space already allocated 7854 * at open(). 7855 */ 7856 err = tcp_init_values(tcp); 7857 ASSERT(err == 0); 7858 /* Restore state in tcp_tcph */ 7859 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7860 if (tcp->tcp_ipversion == IPV4_VERSION) 7861 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7862 else 7863 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7864 /* 7865 * Copy of the src addr. in tcp_t is needed in tcp_t 7866 * since the lookup funcs can only lookup on tcp_t 7867 */ 7868 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7869 7870 ASSERT(tcp->tcp_ptpbhn != NULL); 7871 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7872 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7873 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7874 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7875 } 7876 7877 /* 7878 * Force values to zero that need be zero. 7879 * Do not touch values asociated with the BOUND or LISTEN state 7880 * since the connection will end up in that state after the reinit. 7881 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7882 * structure! 7883 */ 7884 static void 7885 tcp_reinit_values(tcp) 7886 tcp_t *tcp; 7887 { 7888 tcp_stack_t *tcps = tcp->tcp_tcps; 7889 7890 #ifndef lint 7891 #define DONTCARE(x) 7892 #define PRESERVE(x) 7893 #else 7894 #define DONTCARE(x) ((x) = (x)) 7895 #define PRESERVE(x) ((x) = (x)) 7896 #endif /* lint */ 7897 7898 PRESERVE(tcp->tcp_bind_hash); 7899 PRESERVE(tcp->tcp_ptpbhn); 7900 PRESERVE(tcp->tcp_acceptor_hash); 7901 PRESERVE(tcp->tcp_ptpahn); 7902 7903 /* Should be ASSERT NULL on these with new code! */ 7904 ASSERT(tcp->tcp_time_wait_next == NULL); 7905 ASSERT(tcp->tcp_time_wait_prev == NULL); 7906 ASSERT(tcp->tcp_time_wait_expire == 0); 7907 PRESERVE(tcp->tcp_state); 7908 PRESERVE(tcp->tcp_rq); 7909 PRESERVE(tcp->tcp_wq); 7910 7911 ASSERT(tcp->tcp_xmit_head == NULL); 7912 ASSERT(tcp->tcp_xmit_last == NULL); 7913 ASSERT(tcp->tcp_unsent == 0); 7914 ASSERT(tcp->tcp_xmit_tail == NULL); 7915 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7916 7917 tcp->tcp_snxt = 0; /* Displayed in mib */ 7918 tcp->tcp_suna = 0; /* Displayed in mib */ 7919 tcp->tcp_swnd = 0; 7920 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7921 7922 ASSERT(tcp->tcp_ibsegs == 0); 7923 ASSERT(tcp->tcp_obsegs == 0); 7924 7925 if (tcp->tcp_iphc != NULL) { 7926 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7927 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7928 } 7929 7930 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7931 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7932 DONTCARE(tcp->tcp_ipha); 7933 DONTCARE(tcp->tcp_ip6h); 7934 DONTCARE(tcp->tcp_ip_hdr_len); 7935 DONTCARE(tcp->tcp_tcph); 7936 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7937 tcp->tcp_valid_bits = 0; 7938 7939 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7940 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7941 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7942 tcp->tcp_last_rcv_lbolt = 0; 7943 7944 tcp->tcp_init_cwnd = 0; 7945 7946 tcp->tcp_urp_last_valid = 0; 7947 tcp->tcp_hard_binding = 0; 7948 tcp->tcp_hard_bound = 0; 7949 PRESERVE(tcp->tcp_cred); 7950 PRESERVE(tcp->tcp_cpid); 7951 PRESERVE(tcp->tcp_open_time); 7952 PRESERVE(tcp->tcp_exclbind); 7953 7954 tcp->tcp_fin_acked = 0; 7955 tcp->tcp_fin_rcvd = 0; 7956 tcp->tcp_fin_sent = 0; 7957 tcp->tcp_ordrel_done = 0; 7958 7959 tcp->tcp_debug = 0; 7960 tcp->tcp_dontroute = 0; 7961 tcp->tcp_broadcast = 0; 7962 7963 tcp->tcp_useloopback = 0; 7964 tcp->tcp_reuseaddr = 0; 7965 tcp->tcp_oobinline = 0; 7966 tcp->tcp_dgram_errind = 0; 7967 7968 tcp->tcp_detached = 0; 7969 tcp->tcp_bind_pending = 0; 7970 tcp->tcp_unbind_pending = 0; 7971 tcp->tcp_deferred_clean_death = 0; 7972 7973 tcp->tcp_snd_ws_ok = B_FALSE; 7974 tcp->tcp_snd_ts_ok = B_FALSE; 7975 tcp->tcp_linger = 0; 7976 tcp->tcp_ka_enabled = 0; 7977 tcp->tcp_zero_win_probe = 0; 7978 7979 tcp->tcp_loopback = 0; 7980 tcp->tcp_localnet = 0; 7981 tcp->tcp_syn_defense = 0; 7982 tcp->tcp_set_timer = 0; 7983 7984 tcp->tcp_active_open = 0; 7985 ASSERT(tcp->tcp_timeout == B_FALSE); 7986 tcp->tcp_rexmit = B_FALSE; 7987 tcp->tcp_xmit_zc_clean = B_FALSE; 7988 7989 tcp->tcp_snd_sack_ok = B_FALSE; 7990 PRESERVE(tcp->tcp_recvdstaddr); 7991 tcp->tcp_hwcksum = B_FALSE; 7992 7993 tcp->tcp_ire_ill_check_done = B_FALSE; 7994 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7995 7996 tcp->tcp_mdt = B_FALSE; 7997 tcp->tcp_mdt_hdr_head = 0; 7998 tcp->tcp_mdt_hdr_tail = 0; 7999 8000 tcp->tcp_conn_def_q0 = 0; 8001 tcp->tcp_ip_forward_progress = B_FALSE; 8002 tcp->tcp_anon_priv_bind = 0; 8003 tcp->tcp_ecn_ok = B_FALSE; 8004 8005 tcp->tcp_cwr = B_FALSE; 8006 tcp->tcp_ecn_echo_on = B_FALSE; 8007 8008 if (tcp->tcp_sack_info != NULL) { 8009 if (tcp->tcp_notsack_list != NULL) { 8010 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 8011 } 8012 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 8013 tcp->tcp_sack_info = NULL; 8014 } 8015 8016 tcp->tcp_rcv_ws = 0; 8017 tcp->tcp_snd_ws = 0; 8018 tcp->tcp_ts_recent = 0; 8019 tcp->tcp_rnxt = 0; /* Displayed in mib */ 8020 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 8021 tcp->tcp_if_mtu = 0; 8022 8023 ASSERT(tcp->tcp_reass_head == NULL); 8024 ASSERT(tcp->tcp_reass_tail == NULL); 8025 8026 tcp->tcp_cwnd_cnt = 0; 8027 8028 ASSERT(tcp->tcp_rcv_list == NULL); 8029 ASSERT(tcp->tcp_rcv_last_head == NULL); 8030 ASSERT(tcp->tcp_rcv_last_tail == NULL); 8031 ASSERT(tcp->tcp_rcv_cnt == 0); 8032 8033 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8034 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8035 tcp->tcp_csuna = 0; 8036 8037 tcp->tcp_rto = 0; /* Displayed in MIB */ 8038 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8039 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8040 tcp->tcp_rtt_update = 0; 8041 8042 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8043 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8044 8045 tcp->tcp_rack = 0; /* Displayed in mib */ 8046 tcp->tcp_rack_cnt = 0; 8047 tcp->tcp_rack_cur_max = 0; 8048 tcp->tcp_rack_abs_max = 0; 8049 8050 tcp->tcp_max_swnd = 0; 8051 8052 ASSERT(tcp->tcp_listener == NULL); 8053 8054 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8055 8056 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8057 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8058 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8059 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8060 8061 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8062 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8063 PRESERVE(tcp->tcp_conn_req_max); 8064 PRESERVE(tcp->tcp_conn_req_seqnum); 8065 8066 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8067 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8068 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8069 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8070 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8071 8072 tcp->tcp_lingertime = 0; 8073 8074 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8075 ASSERT(tcp->tcp_urp_mp == NULL); 8076 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8077 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8078 8079 ASSERT(tcp->tcp_eager_next_q == NULL); 8080 ASSERT(tcp->tcp_eager_last_q == NULL); 8081 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8082 tcp->tcp_eager_prev_q0 == NULL) || 8083 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8084 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8085 8086 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8087 tcp->tcp_eager_prev_drop_q0 == NULL) || 8088 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8089 8090 tcp->tcp_client_errno = 0; 8091 8092 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8093 8094 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8095 8096 PRESERVE(tcp->tcp_bound_source_v6); 8097 tcp->tcp_last_sent_len = 0; 8098 tcp->tcp_dupack_cnt = 0; 8099 8100 tcp->tcp_fport = 0; /* Displayed in MIB */ 8101 PRESERVE(tcp->tcp_lport); 8102 8103 PRESERVE(tcp->tcp_acceptor_lockp); 8104 8105 ASSERT(tcp->tcp_ordrelid == 0); 8106 PRESERVE(tcp->tcp_acceptor_id); 8107 DONTCARE(tcp->tcp_ipsec_overhead); 8108 8109 /* 8110 * If tcp_tracing flag is ON (i.e. We have a trace buffer 8111 * in tcp structure and now tracing), Re-initialize all 8112 * members of tcp_traceinfo. 8113 */ 8114 if (tcp->tcp_tracebuf != NULL) { 8115 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 8116 } 8117 8118 PRESERVE(tcp->tcp_family); 8119 if (tcp->tcp_family == AF_INET6) { 8120 tcp->tcp_ipversion = IPV6_VERSION; 8121 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8122 } else { 8123 tcp->tcp_ipversion = IPV4_VERSION; 8124 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8125 } 8126 8127 tcp->tcp_bound_if = 0; 8128 tcp->tcp_ipv6_recvancillary = 0; 8129 tcp->tcp_recvifindex = 0; 8130 tcp->tcp_recvhops = 0; 8131 tcp->tcp_closed = 0; 8132 tcp->tcp_cleandeathtag = 0; 8133 if (tcp->tcp_hopopts != NULL) { 8134 mi_free(tcp->tcp_hopopts); 8135 tcp->tcp_hopopts = NULL; 8136 tcp->tcp_hopoptslen = 0; 8137 } 8138 ASSERT(tcp->tcp_hopoptslen == 0); 8139 if (tcp->tcp_dstopts != NULL) { 8140 mi_free(tcp->tcp_dstopts); 8141 tcp->tcp_dstopts = NULL; 8142 tcp->tcp_dstoptslen = 0; 8143 } 8144 ASSERT(tcp->tcp_dstoptslen == 0); 8145 if (tcp->tcp_rtdstopts != NULL) { 8146 mi_free(tcp->tcp_rtdstopts); 8147 tcp->tcp_rtdstopts = NULL; 8148 tcp->tcp_rtdstoptslen = 0; 8149 } 8150 ASSERT(tcp->tcp_rtdstoptslen == 0); 8151 if (tcp->tcp_rthdr != NULL) { 8152 mi_free(tcp->tcp_rthdr); 8153 tcp->tcp_rthdr = NULL; 8154 tcp->tcp_rthdrlen = 0; 8155 } 8156 ASSERT(tcp->tcp_rthdrlen == 0); 8157 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8158 8159 /* Reset fusion-related fields */ 8160 tcp->tcp_fused = B_FALSE; 8161 tcp->tcp_unfusable = B_FALSE; 8162 tcp->tcp_fused_sigurg = B_FALSE; 8163 tcp->tcp_direct_sockfs = B_FALSE; 8164 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8165 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8166 tcp->tcp_loopback_peer = NULL; 8167 tcp->tcp_fuse_rcv_hiwater = 0; 8168 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8169 tcp->tcp_fuse_rcv_unread_cnt = 0; 8170 8171 tcp->tcp_lso = B_FALSE; 8172 8173 tcp->tcp_in_ack_unsent = 0; 8174 tcp->tcp_cork = B_FALSE; 8175 tcp->tcp_tconnind_started = B_FALSE; 8176 8177 PRESERVE(tcp->tcp_squeue_bytes); 8178 8179 ASSERT(tcp->tcp_kssl_ctx == NULL); 8180 ASSERT(!tcp->tcp_kssl_pending); 8181 PRESERVE(tcp->tcp_kssl_ent); 8182 8183 tcp->tcp_closemp_used = B_FALSE; 8184 8185 #ifdef DEBUG 8186 DONTCARE(tcp->tcmp_stk[0]); 8187 #endif 8188 8189 8190 #undef DONTCARE 8191 #undef PRESERVE 8192 } 8193 8194 /* 8195 * Allocate necessary resources and initialize state vector. 8196 * Guaranteed not to fail so that when an error is returned, 8197 * the caller doesn't need to do any additional cleanup. 8198 */ 8199 int 8200 tcp_init(tcp_t *tcp, queue_t *q) 8201 { 8202 int err; 8203 8204 tcp->tcp_rq = q; 8205 tcp->tcp_wq = WR(q); 8206 tcp->tcp_state = TCPS_IDLE; 8207 if ((err = tcp_init_values(tcp)) != 0) 8208 tcp_timers_stop(tcp); 8209 return (err); 8210 } 8211 8212 static int 8213 tcp_init_values(tcp_t *tcp) 8214 { 8215 int err; 8216 tcp_stack_t *tcps = tcp->tcp_tcps; 8217 8218 ASSERT((tcp->tcp_family == AF_INET && 8219 tcp->tcp_ipversion == IPV4_VERSION) || 8220 (tcp->tcp_family == AF_INET6 && 8221 (tcp->tcp_ipversion == IPV4_VERSION || 8222 tcp->tcp_ipversion == IPV6_VERSION))); 8223 8224 /* 8225 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8226 * will be close to tcp_rexmit_interval_initial. By doing this, we 8227 * allow the algorithm to adjust slowly to large fluctuations of RTT 8228 * during first few transmissions of a connection as seen in slow 8229 * links. 8230 */ 8231 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8232 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8233 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8234 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8235 tcps->tcps_conn_grace_period; 8236 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8237 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8238 tcp->tcp_timer_backoff = 0; 8239 tcp->tcp_ms_we_have_waited = 0; 8240 tcp->tcp_last_recv_time = lbolt; 8241 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8242 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8243 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8244 8245 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8246 8247 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8248 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8249 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8250 /* 8251 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8252 * passive open. 8253 */ 8254 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8255 8256 tcp->tcp_naglim = tcps->tcps_naglim_def; 8257 8258 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8259 8260 tcp->tcp_mdt_hdr_head = 0; 8261 tcp->tcp_mdt_hdr_tail = 0; 8262 8263 /* Reset fusion-related fields */ 8264 tcp->tcp_fused = B_FALSE; 8265 tcp->tcp_unfusable = B_FALSE; 8266 tcp->tcp_fused_sigurg = B_FALSE; 8267 tcp->tcp_direct_sockfs = B_FALSE; 8268 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8269 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8270 tcp->tcp_loopback_peer = NULL; 8271 tcp->tcp_fuse_rcv_hiwater = 0; 8272 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8273 tcp->tcp_fuse_rcv_unread_cnt = 0; 8274 8275 /* Initialize the header template */ 8276 if (tcp->tcp_ipversion == IPV4_VERSION) { 8277 err = tcp_header_init_ipv4(tcp); 8278 } else { 8279 err = tcp_header_init_ipv6(tcp); 8280 } 8281 if (err) 8282 return (err); 8283 8284 /* 8285 * Init the window scale to the max so tcp_rwnd_set() won't pare 8286 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8287 */ 8288 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8289 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8290 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8291 8292 tcp->tcp_cork = B_FALSE; 8293 /* 8294 * Init the tcp_debug option. This value determines whether TCP 8295 * calls strlog() to print out debug messages. Doing this 8296 * initialization here means that this value is not inherited thru 8297 * tcp_reinit(). 8298 */ 8299 tcp->tcp_debug = tcps->tcps_dbg; 8300 8301 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8302 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8303 8304 return (0); 8305 } 8306 8307 /* 8308 * Initialize the IPv4 header. Loses any record of any IP options. 8309 */ 8310 static int 8311 tcp_header_init_ipv4(tcp_t *tcp) 8312 { 8313 tcph_t *tcph; 8314 uint32_t sum; 8315 conn_t *connp; 8316 tcp_stack_t *tcps = tcp->tcp_tcps; 8317 8318 /* 8319 * This is a simple initialization. If there's 8320 * already a template, it should never be too small, 8321 * so reuse it. Otherwise, allocate space for the new one. 8322 */ 8323 if (tcp->tcp_iphc == NULL) { 8324 ASSERT(tcp->tcp_iphc_len == 0); 8325 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8326 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8327 if (tcp->tcp_iphc == NULL) { 8328 tcp->tcp_iphc_len = 0; 8329 return (ENOMEM); 8330 } 8331 } 8332 8333 /* options are gone; may need a new label */ 8334 connp = tcp->tcp_connp; 8335 connp->conn_mlp_type = mlptSingle; 8336 connp->conn_ulp_labeled = !is_system_labeled(); 8337 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8338 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8339 tcp->tcp_ip6h = NULL; 8340 tcp->tcp_ipversion = IPV4_VERSION; 8341 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8342 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8343 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8344 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8345 tcp->tcp_ipha->ipha_version_and_hdr_length 8346 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8347 tcp->tcp_ipha->ipha_ident = 0; 8348 8349 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8350 tcp->tcp_tos = 0; 8351 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8352 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8353 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8354 8355 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8356 tcp->tcp_tcph = tcph; 8357 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8358 /* 8359 * IP wants our header length in the checksum field to 8360 * allow it to perform a single pseudo-header+checksum 8361 * calculation on behalf of TCP. 8362 * Include the adjustment for a source route once IP_OPTIONS is set. 8363 */ 8364 sum = sizeof (tcph_t) + tcp->tcp_sum; 8365 sum = (sum >> 16) + (sum & 0xFFFF); 8366 U16_TO_ABE16(sum, tcph->th_sum); 8367 return (0); 8368 } 8369 8370 /* 8371 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8372 */ 8373 static int 8374 tcp_header_init_ipv6(tcp_t *tcp) 8375 { 8376 tcph_t *tcph; 8377 uint32_t sum; 8378 conn_t *connp; 8379 tcp_stack_t *tcps = tcp->tcp_tcps; 8380 8381 /* 8382 * This is a simple initialization. If there's 8383 * already a template, it should never be too small, 8384 * so reuse it. Otherwise, allocate space for the new one. 8385 * Ensure that there is enough space to "downgrade" the tcp_t 8386 * to an IPv4 tcp_t. This requires having space for a full load 8387 * of IPv4 options, as well as a full load of TCP options 8388 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8389 * than a v6 header and a TCP header with a full load of TCP options 8390 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8391 * We want to avoid reallocation in the "downgraded" case when 8392 * processing outbound IPv4 options. 8393 */ 8394 if (tcp->tcp_iphc == NULL) { 8395 ASSERT(tcp->tcp_iphc_len == 0); 8396 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8397 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8398 if (tcp->tcp_iphc == NULL) { 8399 tcp->tcp_iphc_len = 0; 8400 return (ENOMEM); 8401 } 8402 } 8403 8404 /* options are gone; may need a new label */ 8405 connp = tcp->tcp_connp; 8406 connp->conn_mlp_type = mlptSingle; 8407 connp->conn_ulp_labeled = !is_system_labeled(); 8408 8409 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8410 tcp->tcp_ipversion = IPV6_VERSION; 8411 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8412 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8413 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8414 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8415 tcp->tcp_ipha = NULL; 8416 8417 /* Initialize the header template */ 8418 8419 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8420 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8421 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8422 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8423 8424 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8425 tcp->tcp_tcph = tcph; 8426 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8427 /* 8428 * IP wants our header length in the checksum field to 8429 * allow it to perform a single psuedo-header+checksum 8430 * calculation on behalf of TCP. 8431 * Include the adjustment for a source route when IPV6_RTHDR is set. 8432 */ 8433 sum = sizeof (tcph_t) + tcp->tcp_sum; 8434 sum = (sum >> 16) + (sum & 0xFFFF); 8435 U16_TO_ABE16(sum, tcph->th_sum); 8436 return (0); 8437 } 8438 8439 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8440 #define ICMP_MIN_TCP_HDR 8 8441 8442 /* 8443 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8444 * passed up by IP. The message is always received on the correct tcp_t. 8445 * Assumes that IP has pulled up everything up to and including the ICMP header. 8446 */ 8447 void 8448 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8449 { 8450 icmph_t *icmph; 8451 ipha_t *ipha; 8452 int iph_hdr_length; 8453 tcph_t *tcph; 8454 boolean_t ipsec_mctl = B_FALSE; 8455 boolean_t secure; 8456 mblk_t *first_mp = mp; 8457 uint32_t new_mss; 8458 uint32_t ratio; 8459 size_t mp_size = MBLKL(mp); 8460 uint32_t seg_seq; 8461 tcp_stack_t *tcps = tcp->tcp_tcps; 8462 8463 /* Assume IP provides aligned packets - otherwise toss */ 8464 if (!OK_32PTR(mp->b_rptr)) { 8465 freemsg(mp); 8466 return; 8467 } 8468 8469 /* 8470 * Since ICMP errors are normal data marked with M_CTL when sent 8471 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8472 * packets starting with an ipsec_info_t, see ipsec_info.h. 8473 */ 8474 if ((mp_size == sizeof (ipsec_info_t)) && 8475 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8476 ASSERT(mp->b_cont != NULL); 8477 mp = mp->b_cont; 8478 /* IP should have done this */ 8479 ASSERT(OK_32PTR(mp->b_rptr)); 8480 mp_size = MBLKL(mp); 8481 ipsec_mctl = B_TRUE; 8482 } 8483 8484 /* 8485 * Verify that we have a complete outer IP header. If not, drop it. 8486 */ 8487 if (mp_size < sizeof (ipha_t)) { 8488 noticmpv4: 8489 freemsg(first_mp); 8490 return; 8491 } 8492 8493 ipha = (ipha_t *)mp->b_rptr; 8494 /* 8495 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8496 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8497 */ 8498 switch (IPH_HDR_VERSION(ipha)) { 8499 case IPV6_VERSION: 8500 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8501 return; 8502 case IPV4_VERSION: 8503 break; 8504 default: 8505 goto noticmpv4; 8506 } 8507 8508 /* Skip past the outer IP and ICMP headers */ 8509 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8510 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8511 /* 8512 * If we don't have the correct outer IP header length or if the ULP 8513 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8514 * send it upstream. 8515 */ 8516 if (iph_hdr_length < sizeof (ipha_t) || 8517 ipha->ipha_protocol != IPPROTO_ICMP || 8518 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8519 goto noticmpv4; 8520 } 8521 ipha = (ipha_t *)&icmph[1]; 8522 8523 /* Skip past the inner IP and find the ULP header */ 8524 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8525 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8526 /* 8527 * If we don't have the correct inner IP header length or if the ULP 8528 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8529 * bytes of TCP header, drop it. 8530 */ 8531 if (iph_hdr_length < sizeof (ipha_t) || 8532 ipha->ipha_protocol != IPPROTO_TCP || 8533 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8534 goto noticmpv4; 8535 } 8536 8537 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8538 if (ipsec_mctl) { 8539 secure = ipsec_in_is_secure(first_mp); 8540 } else { 8541 secure = B_FALSE; 8542 } 8543 if (secure) { 8544 /* 8545 * If we are willing to accept this in clear 8546 * we don't have to verify policy. 8547 */ 8548 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8549 if (!tcp_check_policy(tcp, first_mp, 8550 ipha, NULL, secure, ipsec_mctl)) { 8551 /* 8552 * tcp_check_policy called 8553 * ip_drop_packet() on failure. 8554 */ 8555 return; 8556 } 8557 } 8558 } 8559 } else if (ipsec_mctl) { 8560 /* 8561 * This is a hard_bound connection. IP has already 8562 * verified policy. We don't have to do it again. 8563 */ 8564 freeb(first_mp); 8565 first_mp = mp; 8566 ipsec_mctl = B_FALSE; 8567 } 8568 8569 seg_seq = ABE32_TO_U32(tcph->th_seq); 8570 /* 8571 * TCP SHOULD check that the TCP sequence number contained in 8572 * payload of the ICMP error message is within the range 8573 * SND.UNA <= SEG.SEQ < SND.NXT. 8574 */ 8575 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8576 /* 8577 * If the ICMP message is bogus, should we kill the 8578 * connection, or should we just drop the bogus ICMP 8579 * message? It would probably make more sense to just 8580 * drop the message so that if this one managed to get 8581 * in, the real connection should not suffer. 8582 */ 8583 goto noticmpv4; 8584 } 8585 8586 switch (icmph->icmph_type) { 8587 case ICMP_DEST_UNREACHABLE: 8588 switch (icmph->icmph_code) { 8589 case ICMP_FRAGMENTATION_NEEDED: 8590 /* 8591 * Reduce the MSS based on the new MTU. This will 8592 * eliminate any fragmentation locally. 8593 * N.B. There may well be some funny side-effects on 8594 * the local send policy and the remote receive policy. 8595 * Pending further research, we provide 8596 * tcp_ignore_path_mtu just in case this proves 8597 * disastrous somewhere. 8598 * 8599 * After updating the MSS, retransmit part of the 8600 * dropped segment using the new mss by calling 8601 * tcp_wput_data(). Need to adjust all those 8602 * params to make sure tcp_wput_data() work properly. 8603 */ 8604 if (tcps->tcps_ignore_path_mtu) 8605 break; 8606 8607 /* 8608 * Decrease the MSS by time stamp options 8609 * IP options and IPSEC options. tcp_hdr_len 8610 * includes time stamp option and IP option 8611 * length. 8612 */ 8613 8614 new_mss = ntohs(icmph->icmph_du_mtu) - 8615 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8616 8617 /* 8618 * Only update the MSS if the new one is 8619 * smaller than the previous one. This is 8620 * to avoid problems when getting multiple 8621 * ICMP errors for the same MTU. 8622 */ 8623 if (new_mss >= tcp->tcp_mss) 8624 break; 8625 8626 /* 8627 * Stop doing PMTU if new_mss is less than 68 8628 * or less than tcp_mss_min. 8629 * The value 68 comes from rfc 1191. 8630 */ 8631 if (new_mss < MAX(68, tcps->tcps_mss_min)) 8632 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8633 0; 8634 8635 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8636 ASSERT(ratio >= 1); 8637 tcp_mss_set(tcp, new_mss, B_TRUE); 8638 8639 /* 8640 * Make sure we have something to 8641 * send. 8642 */ 8643 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8644 (tcp->tcp_xmit_head != NULL)) { 8645 /* 8646 * Shrink tcp_cwnd in 8647 * proportion to the old MSS/new MSS. 8648 */ 8649 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8650 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8651 (tcp->tcp_unsent == 0)) { 8652 tcp->tcp_rexmit_max = tcp->tcp_fss; 8653 } else { 8654 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8655 } 8656 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8657 tcp->tcp_rexmit = B_TRUE; 8658 tcp->tcp_dupack_cnt = 0; 8659 tcp->tcp_snd_burst = TCP_CWND_SS; 8660 tcp_ss_rexmit(tcp); 8661 } 8662 break; 8663 case ICMP_PORT_UNREACHABLE: 8664 case ICMP_PROTOCOL_UNREACHABLE: 8665 switch (tcp->tcp_state) { 8666 case TCPS_SYN_SENT: 8667 case TCPS_SYN_RCVD: 8668 /* 8669 * ICMP can snipe away incipient 8670 * TCP connections as long as 8671 * seq number is same as initial 8672 * send seq number. 8673 */ 8674 if (seg_seq == tcp->tcp_iss) { 8675 (void) tcp_clean_death(tcp, 8676 ECONNREFUSED, 6); 8677 } 8678 break; 8679 } 8680 break; 8681 case ICMP_HOST_UNREACHABLE: 8682 case ICMP_NET_UNREACHABLE: 8683 /* Record the error in case we finally time out. */ 8684 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8685 tcp->tcp_client_errno = EHOSTUNREACH; 8686 else 8687 tcp->tcp_client_errno = ENETUNREACH; 8688 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8689 if (tcp->tcp_listener != NULL && 8690 tcp->tcp_listener->tcp_syn_defense) { 8691 /* 8692 * Ditch the half-open connection if we 8693 * suspect a SYN attack is under way. 8694 */ 8695 tcp_ip_ire_mark_advice(tcp); 8696 (void) tcp_clean_death(tcp, 8697 tcp->tcp_client_errno, 7); 8698 } 8699 } 8700 break; 8701 default: 8702 break; 8703 } 8704 break; 8705 case ICMP_SOURCE_QUENCH: { 8706 /* 8707 * use a global boolean to control 8708 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8709 * The default is false. 8710 */ 8711 if (tcp_icmp_source_quench) { 8712 /* 8713 * Reduce the sending rate as if we got a 8714 * retransmit timeout 8715 */ 8716 uint32_t npkt; 8717 8718 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8719 tcp->tcp_mss; 8720 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8721 tcp->tcp_cwnd = tcp->tcp_mss; 8722 tcp->tcp_cwnd_cnt = 0; 8723 } 8724 break; 8725 } 8726 } 8727 freemsg(first_mp); 8728 } 8729 8730 /* 8731 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8732 * error messages passed up by IP. 8733 * Assumes that IP has pulled up all the extension headers as well 8734 * as the ICMPv6 header. 8735 */ 8736 static void 8737 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8738 { 8739 icmp6_t *icmp6; 8740 ip6_t *ip6h; 8741 uint16_t iph_hdr_length; 8742 tcpha_t *tcpha; 8743 uint8_t *nexthdrp; 8744 uint32_t new_mss; 8745 uint32_t ratio; 8746 boolean_t secure; 8747 mblk_t *first_mp = mp; 8748 size_t mp_size; 8749 uint32_t seg_seq; 8750 tcp_stack_t *tcps = tcp->tcp_tcps; 8751 8752 /* 8753 * The caller has determined if this is an IPSEC_IN packet and 8754 * set ipsec_mctl appropriately (see tcp_icmp_error). 8755 */ 8756 if (ipsec_mctl) 8757 mp = mp->b_cont; 8758 8759 mp_size = MBLKL(mp); 8760 8761 /* 8762 * Verify that we have a complete IP header. If not, send it upstream. 8763 */ 8764 if (mp_size < sizeof (ip6_t)) { 8765 noticmpv6: 8766 freemsg(first_mp); 8767 return; 8768 } 8769 8770 /* 8771 * Verify this is an ICMPV6 packet, else send it upstream. 8772 */ 8773 ip6h = (ip6_t *)mp->b_rptr; 8774 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8775 iph_hdr_length = IPV6_HDR_LEN; 8776 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8777 &nexthdrp) || 8778 *nexthdrp != IPPROTO_ICMPV6) { 8779 goto noticmpv6; 8780 } 8781 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8782 ip6h = (ip6_t *)&icmp6[1]; 8783 /* 8784 * Verify if we have a complete ICMP and inner IP header. 8785 */ 8786 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8787 goto noticmpv6; 8788 8789 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8790 goto noticmpv6; 8791 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8792 /* 8793 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8794 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8795 * packet. 8796 */ 8797 if ((*nexthdrp != IPPROTO_TCP) || 8798 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8799 goto noticmpv6; 8800 } 8801 8802 /* 8803 * ICMP errors come on the right queue or come on 8804 * listener/global queue for detached connections and 8805 * get switched to the right queue. If it comes on the 8806 * right queue, policy check has already been done by IP 8807 * and thus free the first_mp without verifying the policy. 8808 * If it has come for a non-hard bound connection, we need 8809 * to verify policy as IP may not have done it. 8810 */ 8811 if (!tcp->tcp_hard_bound) { 8812 if (ipsec_mctl) { 8813 secure = ipsec_in_is_secure(first_mp); 8814 } else { 8815 secure = B_FALSE; 8816 } 8817 if (secure) { 8818 /* 8819 * If we are willing to accept this in clear 8820 * we don't have to verify policy. 8821 */ 8822 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8823 if (!tcp_check_policy(tcp, first_mp, 8824 NULL, ip6h, secure, ipsec_mctl)) { 8825 /* 8826 * tcp_check_policy called 8827 * ip_drop_packet() on failure. 8828 */ 8829 return; 8830 } 8831 } 8832 } 8833 } else if (ipsec_mctl) { 8834 /* 8835 * This is a hard_bound connection. IP has already 8836 * verified policy. We don't have to do it again. 8837 */ 8838 freeb(first_mp); 8839 first_mp = mp; 8840 ipsec_mctl = B_FALSE; 8841 } 8842 8843 seg_seq = ntohl(tcpha->tha_seq); 8844 /* 8845 * TCP SHOULD check that the TCP sequence number contained in 8846 * payload of the ICMP error message is within the range 8847 * SND.UNA <= SEG.SEQ < SND.NXT. 8848 */ 8849 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8850 /* 8851 * If the ICMP message is bogus, should we kill the 8852 * connection, or should we just drop the bogus ICMP 8853 * message? It would probably make more sense to just 8854 * drop the message so that if this one managed to get 8855 * in, the real connection should not suffer. 8856 */ 8857 goto noticmpv6; 8858 } 8859 8860 switch (icmp6->icmp6_type) { 8861 case ICMP6_PACKET_TOO_BIG: 8862 /* 8863 * Reduce the MSS based on the new MTU. This will 8864 * eliminate any fragmentation locally. 8865 * N.B. There may well be some funny side-effects on 8866 * the local send policy and the remote receive policy. 8867 * Pending further research, we provide 8868 * tcp_ignore_path_mtu just in case this proves 8869 * disastrous somewhere. 8870 * 8871 * After updating the MSS, retransmit part of the 8872 * dropped segment using the new mss by calling 8873 * tcp_wput_data(). Need to adjust all those 8874 * params to make sure tcp_wput_data() work properly. 8875 */ 8876 if (tcps->tcps_ignore_path_mtu) 8877 break; 8878 8879 /* 8880 * Decrease the MSS by time stamp options 8881 * IP options and IPSEC options. tcp_hdr_len 8882 * includes time stamp option and IP option 8883 * length. 8884 */ 8885 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8886 tcp->tcp_ipsec_overhead; 8887 8888 /* 8889 * Only update the MSS if the new one is 8890 * smaller than the previous one. This is 8891 * to avoid problems when getting multiple 8892 * ICMP errors for the same MTU. 8893 */ 8894 if (new_mss >= tcp->tcp_mss) 8895 break; 8896 8897 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8898 ASSERT(ratio >= 1); 8899 tcp_mss_set(tcp, new_mss, B_TRUE); 8900 8901 /* 8902 * Make sure we have something to 8903 * send. 8904 */ 8905 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8906 (tcp->tcp_xmit_head != NULL)) { 8907 /* 8908 * Shrink tcp_cwnd in 8909 * proportion to the old MSS/new MSS. 8910 */ 8911 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8912 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8913 (tcp->tcp_unsent == 0)) { 8914 tcp->tcp_rexmit_max = tcp->tcp_fss; 8915 } else { 8916 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8917 } 8918 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8919 tcp->tcp_rexmit = B_TRUE; 8920 tcp->tcp_dupack_cnt = 0; 8921 tcp->tcp_snd_burst = TCP_CWND_SS; 8922 tcp_ss_rexmit(tcp); 8923 } 8924 break; 8925 8926 case ICMP6_DST_UNREACH: 8927 switch (icmp6->icmp6_code) { 8928 case ICMP6_DST_UNREACH_NOPORT: 8929 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8930 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8931 (seg_seq == tcp->tcp_iss)) { 8932 (void) tcp_clean_death(tcp, 8933 ECONNREFUSED, 8); 8934 } 8935 break; 8936 8937 case ICMP6_DST_UNREACH_ADMIN: 8938 case ICMP6_DST_UNREACH_NOROUTE: 8939 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8940 case ICMP6_DST_UNREACH_ADDR: 8941 /* Record the error in case we finally time out. */ 8942 tcp->tcp_client_errno = EHOSTUNREACH; 8943 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8944 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8945 (seg_seq == tcp->tcp_iss)) { 8946 if (tcp->tcp_listener != NULL && 8947 tcp->tcp_listener->tcp_syn_defense) { 8948 /* 8949 * Ditch the half-open connection if we 8950 * suspect a SYN attack is under way. 8951 */ 8952 tcp_ip_ire_mark_advice(tcp); 8953 (void) tcp_clean_death(tcp, 8954 tcp->tcp_client_errno, 9); 8955 } 8956 } 8957 8958 8959 break; 8960 default: 8961 break; 8962 } 8963 break; 8964 8965 case ICMP6_PARAM_PROB: 8966 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8967 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8968 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8969 (uchar_t *)nexthdrp) { 8970 if (tcp->tcp_state == TCPS_SYN_SENT || 8971 tcp->tcp_state == TCPS_SYN_RCVD) { 8972 (void) tcp_clean_death(tcp, 8973 ECONNREFUSED, 10); 8974 } 8975 break; 8976 } 8977 break; 8978 8979 case ICMP6_TIME_EXCEEDED: 8980 default: 8981 break; 8982 } 8983 freemsg(first_mp); 8984 } 8985 8986 /* 8987 * IP recognizes seven kinds of bind requests: 8988 * 8989 * - A zero-length address binds only to the protocol number. 8990 * 8991 * - A 4-byte address is treated as a request to 8992 * validate that the address is a valid local IPv4 8993 * address, appropriate for an application to bind to. 8994 * IP does the verification, but does not make any note 8995 * of the address at this time. 8996 * 8997 * - A 16-byte address contains is treated as a request 8998 * to validate a local IPv6 address, as the 4-byte 8999 * address case above. 9000 * 9001 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 9002 * use it for the inbound fanout of packets. 9003 * 9004 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 9005 * use it for the inbound fanout of packets. 9006 * 9007 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 9008 * information consisting of local and remote addresses 9009 * and ports. In this case, the addresses are both 9010 * validated as appropriate for this operation, and, if 9011 * so, the information is retained for use in the 9012 * inbound fanout. 9013 * 9014 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 9015 * fanout information, like the 12-byte case above. 9016 * 9017 * IP will also fill in the IRE request mblk with information 9018 * regarding our peer. In all cases, we notify IP of our protocol 9019 * type by appending a single protocol byte to the bind request. 9020 */ 9021 static mblk_t * 9022 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 9023 { 9024 char *cp; 9025 mblk_t *mp; 9026 struct T_bind_req *tbr; 9027 ipa_conn_t *ac; 9028 ipa6_conn_t *ac6; 9029 sin_t *sin; 9030 sin6_t *sin6; 9031 9032 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9033 ASSERT((tcp->tcp_family == AF_INET && 9034 tcp->tcp_ipversion == IPV4_VERSION) || 9035 (tcp->tcp_family == AF_INET6 && 9036 (tcp->tcp_ipversion == IPV4_VERSION || 9037 tcp->tcp_ipversion == IPV6_VERSION))); 9038 9039 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9040 if (!mp) 9041 return (mp); 9042 mp->b_datap->db_type = M_PROTO; 9043 tbr = (struct T_bind_req *)mp->b_rptr; 9044 tbr->PRIM_type = bind_prim; 9045 tbr->ADDR_offset = sizeof (*tbr); 9046 tbr->CONIND_number = 0; 9047 tbr->ADDR_length = addr_length; 9048 cp = (char *)&tbr[1]; 9049 switch (addr_length) { 9050 case sizeof (ipa_conn_t): 9051 ASSERT(tcp->tcp_family == AF_INET); 9052 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9053 9054 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9055 if (mp->b_cont == NULL) { 9056 freemsg(mp); 9057 return (NULL); 9058 } 9059 mp->b_cont->b_wptr += sizeof (ire_t); 9060 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9061 9062 /* cp known to be 32 bit aligned */ 9063 ac = (ipa_conn_t *)cp; 9064 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9065 ac->ac_faddr = tcp->tcp_remote; 9066 ac->ac_fport = tcp->tcp_fport; 9067 ac->ac_lport = tcp->tcp_lport; 9068 tcp->tcp_hard_binding = 1; 9069 break; 9070 9071 case sizeof (ipa6_conn_t): 9072 ASSERT(tcp->tcp_family == AF_INET6); 9073 9074 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9075 if (mp->b_cont == NULL) { 9076 freemsg(mp); 9077 return (NULL); 9078 } 9079 mp->b_cont->b_wptr += sizeof (ire_t); 9080 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9081 9082 /* cp known to be 32 bit aligned */ 9083 ac6 = (ipa6_conn_t *)cp; 9084 if (tcp->tcp_ipversion == IPV4_VERSION) { 9085 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9086 &ac6->ac6_laddr); 9087 } else { 9088 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9089 } 9090 ac6->ac6_faddr = tcp->tcp_remote_v6; 9091 ac6->ac6_fport = tcp->tcp_fport; 9092 ac6->ac6_lport = tcp->tcp_lport; 9093 tcp->tcp_hard_binding = 1; 9094 break; 9095 9096 case sizeof (sin_t): 9097 /* 9098 * NOTE: IPV6_ADDR_LEN also has same size. 9099 * Use family to discriminate. 9100 */ 9101 if (tcp->tcp_family == AF_INET) { 9102 sin = (sin_t *)cp; 9103 9104 *sin = sin_null; 9105 sin->sin_family = AF_INET; 9106 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9107 sin->sin_port = tcp->tcp_lport; 9108 break; 9109 } else { 9110 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9111 } 9112 break; 9113 9114 case sizeof (sin6_t): 9115 ASSERT(tcp->tcp_family == AF_INET6); 9116 sin6 = (sin6_t *)cp; 9117 9118 *sin6 = sin6_null; 9119 sin6->sin6_family = AF_INET6; 9120 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9121 sin6->sin6_port = tcp->tcp_lport; 9122 break; 9123 9124 case IP_ADDR_LEN: 9125 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9126 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9127 break; 9128 9129 } 9130 /* Add protocol number to end */ 9131 cp[addr_length] = (char)IPPROTO_TCP; 9132 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9133 return (mp); 9134 } 9135 9136 /* 9137 * Notify IP that we are having trouble with this connection. IP should 9138 * blow the IRE away and start over. 9139 */ 9140 static void 9141 tcp_ip_notify(tcp_t *tcp) 9142 { 9143 struct iocblk *iocp; 9144 ipid_t *ipid; 9145 mblk_t *mp; 9146 9147 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9148 if (tcp->tcp_ipversion == IPV6_VERSION) 9149 return; 9150 9151 mp = mkiocb(IP_IOCTL); 9152 if (mp == NULL) 9153 return; 9154 9155 iocp = (struct iocblk *)mp->b_rptr; 9156 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9157 9158 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9159 if (!mp->b_cont) { 9160 freeb(mp); 9161 return; 9162 } 9163 9164 ipid = (ipid_t *)mp->b_cont->b_rptr; 9165 mp->b_cont->b_wptr += iocp->ioc_count; 9166 bzero(ipid, sizeof (*ipid)); 9167 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9168 ipid->ipid_ire_type = IRE_CACHE; 9169 ipid->ipid_addr_offset = sizeof (ipid_t); 9170 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9171 /* 9172 * Note: in the case of source routing we want to blow away the 9173 * route to the first source route hop. 9174 */ 9175 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9176 sizeof (tcp->tcp_ipha->ipha_dst)); 9177 9178 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9179 } 9180 9181 /* Unlink and return any mblk that looks like it contains an ire */ 9182 static mblk_t * 9183 tcp_ire_mp(mblk_t *mp) 9184 { 9185 mblk_t *prev_mp; 9186 9187 for (;;) { 9188 prev_mp = mp; 9189 mp = mp->b_cont; 9190 if (mp == NULL) 9191 break; 9192 switch (DB_TYPE(mp)) { 9193 case IRE_DB_TYPE: 9194 case IRE_DB_REQ_TYPE: 9195 if (prev_mp != NULL) 9196 prev_mp->b_cont = mp->b_cont; 9197 mp->b_cont = NULL; 9198 return (mp); 9199 default: 9200 break; 9201 } 9202 } 9203 return (mp); 9204 } 9205 9206 /* 9207 * Timer callback routine for keepalive probe. We do a fake resend of 9208 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9209 * check to see if we have heard anything from the other end for the last 9210 * RTO period. If we have, set the timer to expire for another 9211 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9212 * RTO << 1 and check again when it expires. Keep exponentially increasing 9213 * the timeout if we have not heard from the other side. If for more than 9214 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9215 * kill the connection unless the keepalive abort threshold is 0. In 9216 * that case, we will probe "forever." 9217 */ 9218 static void 9219 tcp_keepalive_killer(void *arg) 9220 { 9221 mblk_t *mp; 9222 conn_t *connp = (conn_t *)arg; 9223 tcp_t *tcp = connp->conn_tcp; 9224 int32_t firetime; 9225 int32_t idletime; 9226 int32_t ka_intrvl; 9227 tcp_stack_t *tcps = tcp->tcp_tcps; 9228 9229 tcp->tcp_ka_tid = 0; 9230 9231 if (tcp->tcp_fused) 9232 return; 9233 9234 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9235 ka_intrvl = tcp->tcp_ka_interval; 9236 9237 /* 9238 * Keepalive probe should only be sent if the application has not 9239 * done a close on the connection. 9240 */ 9241 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9242 return; 9243 } 9244 /* Timer fired too early, restart it. */ 9245 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9246 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9247 MSEC_TO_TICK(ka_intrvl)); 9248 return; 9249 } 9250 9251 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9252 /* 9253 * If we have not heard from the other side for a long 9254 * time, kill the connection unless the keepalive abort 9255 * threshold is 0. In that case, we will probe "forever." 9256 */ 9257 if (tcp->tcp_ka_abort_thres != 0 && 9258 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9259 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9260 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9261 tcp->tcp_client_errno : ETIMEDOUT, 11); 9262 return; 9263 } 9264 9265 if (tcp->tcp_snxt == tcp->tcp_suna && 9266 idletime >= ka_intrvl) { 9267 /* Fake resend of last ACKed byte. */ 9268 mblk_t *mp1 = allocb(1, BPRI_LO); 9269 9270 if (mp1 != NULL) { 9271 *mp1->b_wptr++ = '\0'; 9272 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9273 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9274 freeb(mp1); 9275 /* 9276 * if allocation failed, fall through to start the 9277 * timer back. 9278 */ 9279 if (mp != NULL) { 9280 TCP_RECORD_TRACE(tcp, mp, 9281 TCP_TRACE_SEND_PKT); 9282 tcp_send_data(tcp, tcp->tcp_wq, mp); 9283 BUMP_MIB(&tcps->tcps_mib, 9284 tcpTimKeepaliveProbe); 9285 if (tcp->tcp_ka_last_intrvl != 0) { 9286 int max; 9287 /* 9288 * We should probe again at least 9289 * in ka_intrvl, but not more than 9290 * tcp_rexmit_interval_max. 9291 */ 9292 max = tcps->tcps_rexmit_interval_max; 9293 firetime = MIN(ka_intrvl - 1, 9294 tcp->tcp_ka_last_intrvl << 1); 9295 if (firetime > max) 9296 firetime = max; 9297 } else { 9298 firetime = tcp->tcp_rto; 9299 } 9300 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9301 tcp_keepalive_killer, 9302 MSEC_TO_TICK(firetime)); 9303 tcp->tcp_ka_last_intrvl = firetime; 9304 return; 9305 } 9306 } 9307 } else { 9308 tcp->tcp_ka_last_intrvl = 0; 9309 } 9310 9311 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9312 if ((firetime = ka_intrvl - idletime) < 0) { 9313 firetime = ka_intrvl; 9314 } 9315 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9316 MSEC_TO_TICK(firetime)); 9317 } 9318 9319 int 9320 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9321 { 9322 queue_t *q = tcp->tcp_rq; 9323 int32_t mss = tcp->tcp_mss; 9324 int maxpsz; 9325 9326 if (TCP_IS_DETACHED(tcp)) 9327 return (mss); 9328 9329 if (tcp->tcp_fused) { 9330 maxpsz = tcp_fuse_maxpsz_set(tcp); 9331 mss = INFPSZ; 9332 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9333 /* 9334 * Set the sd_qn_maxpsz according to the socket send buffer 9335 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9336 * instruct the stream head to copyin user data into contiguous 9337 * kernel-allocated buffers without breaking it up into smaller 9338 * chunks. We round up the buffer size to the nearest SMSS. 9339 */ 9340 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9341 if (tcp->tcp_kssl_ctx == NULL) 9342 mss = INFPSZ; 9343 else 9344 mss = SSL3_MAX_RECORD_LEN; 9345 } else { 9346 /* 9347 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9348 * (and a multiple of the mss). This instructs the stream 9349 * head to break down larger than SMSS writes into SMSS- 9350 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9351 */ 9352 maxpsz = tcp->tcp_maxpsz * mss; 9353 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9354 maxpsz = tcp->tcp_xmit_hiwater/2; 9355 /* Round up to nearest mss */ 9356 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9357 } 9358 } 9359 (void) setmaxps(q, maxpsz); 9360 tcp->tcp_wq->q_maxpsz = maxpsz; 9361 9362 if (set_maxblk) 9363 (void) mi_set_sth_maxblk(q, mss); 9364 9365 return (mss); 9366 } 9367 9368 /* 9369 * Extract option values from a tcp header. We put any found values into the 9370 * tcpopt struct and return a bitmask saying which options were found. 9371 */ 9372 static int 9373 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9374 { 9375 uchar_t *endp; 9376 int len; 9377 uint32_t mss; 9378 uchar_t *up = (uchar_t *)tcph; 9379 int found = 0; 9380 int32_t sack_len; 9381 tcp_seq sack_begin, sack_end; 9382 tcp_t *tcp; 9383 9384 endp = up + TCP_HDR_LENGTH(tcph); 9385 up += TCP_MIN_HEADER_LENGTH; 9386 while (up < endp) { 9387 len = endp - up; 9388 switch (*up) { 9389 case TCPOPT_EOL: 9390 break; 9391 9392 case TCPOPT_NOP: 9393 up++; 9394 continue; 9395 9396 case TCPOPT_MAXSEG: 9397 if (len < TCPOPT_MAXSEG_LEN || 9398 up[1] != TCPOPT_MAXSEG_LEN) 9399 break; 9400 9401 mss = BE16_TO_U16(up+2); 9402 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9403 tcpopt->tcp_opt_mss = mss; 9404 found |= TCP_OPT_MSS_PRESENT; 9405 9406 up += TCPOPT_MAXSEG_LEN; 9407 continue; 9408 9409 case TCPOPT_WSCALE: 9410 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9411 break; 9412 9413 if (up[2] > TCP_MAX_WINSHIFT) 9414 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9415 else 9416 tcpopt->tcp_opt_wscale = up[2]; 9417 found |= TCP_OPT_WSCALE_PRESENT; 9418 9419 up += TCPOPT_WS_LEN; 9420 continue; 9421 9422 case TCPOPT_SACK_PERMITTED: 9423 if (len < TCPOPT_SACK_OK_LEN || 9424 up[1] != TCPOPT_SACK_OK_LEN) 9425 break; 9426 found |= TCP_OPT_SACK_OK_PRESENT; 9427 up += TCPOPT_SACK_OK_LEN; 9428 continue; 9429 9430 case TCPOPT_SACK: 9431 if (len <= 2 || up[1] <= 2 || len < up[1]) 9432 break; 9433 9434 /* If TCP is not interested in SACK blks... */ 9435 if ((tcp = tcpopt->tcp) == NULL) { 9436 up += up[1]; 9437 continue; 9438 } 9439 sack_len = up[1] - TCPOPT_HEADER_LEN; 9440 up += TCPOPT_HEADER_LEN; 9441 9442 /* 9443 * If the list is empty, allocate one and assume 9444 * nothing is sack'ed. 9445 */ 9446 ASSERT(tcp->tcp_sack_info != NULL); 9447 if (tcp->tcp_notsack_list == NULL) { 9448 tcp_notsack_update(&(tcp->tcp_notsack_list), 9449 tcp->tcp_suna, tcp->tcp_snxt, 9450 &(tcp->tcp_num_notsack_blk), 9451 &(tcp->tcp_cnt_notsack_list)); 9452 9453 /* 9454 * Make sure tcp_notsack_list is not NULL. 9455 * This happens when kmem_alloc(KM_NOSLEEP) 9456 * returns NULL. 9457 */ 9458 if (tcp->tcp_notsack_list == NULL) { 9459 up += sack_len; 9460 continue; 9461 } 9462 tcp->tcp_fack = tcp->tcp_suna; 9463 } 9464 9465 while (sack_len > 0) { 9466 if (up + 8 > endp) { 9467 up = endp; 9468 break; 9469 } 9470 sack_begin = BE32_TO_U32(up); 9471 up += 4; 9472 sack_end = BE32_TO_U32(up); 9473 up += 4; 9474 sack_len -= 8; 9475 /* 9476 * Bounds checking. Make sure the SACK 9477 * info is within tcp_suna and tcp_snxt. 9478 * If this SACK blk is out of bound, ignore 9479 * it but continue to parse the following 9480 * blks. 9481 */ 9482 if (SEQ_LEQ(sack_end, sack_begin) || 9483 SEQ_LT(sack_begin, tcp->tcp_suna) || 9484 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9485 continue; 9486 } 9487 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9488 sack_begin, sack_end, 9489 &(tcp->tcp_num_notsack_blk), 9490 &(tcp->tcp_cnt_notsack_list)); 9491 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9492 tcp->tcp_fack = sack_end; 9493 } 9494 } 9495 found |= TCP_OPT_SACK_PRESENT; 9496 continue; 9497 9498 case TCPOPT_TSTAMP: 9499 if (len < TCPOPT_TSTAMP_LEN || 9500 up[1] != TCPOPT_TSTAMP_LEN) 9501 break; 9502 9503 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9504 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9505 9506 found |= TCP_OPT_TSTAMP_PRESENT; 9507 9508 up += TCPOPT_TSTAMP_LEN; 9509 continue; 9510 9511 default: 9512 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9513 break; 9514 up += up[1]; 9515 continue; 9516 } 9517 break; 9518 } 9519 return (found); 9520 } 9521 9522 /* 9523 * Set the mss associated with a particular tcp based on its current value, 9524 * and a new one passed in. Observe minimums and maximums, and reset 9525 * other state variables that we want to view as multiples of mss. 9526 * 9527 * This function is called in various places mainly because 9528 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9529 * other side's SYN/SYN-ACK packet arrives. 9530 * 2) PMTUd may get us a new MSS. 9531 * 3) If the other side stops sending us timestamp option, we need to 9532 * increase the MSS size to use the extra bytes available. 9533 * 9534 * do_ss is used to control whether we will be doing slow start or 9535 * not if there is a change in the mss. Note that for some events like 9536 * tcp_paws_check() we allow the tcp_cwnd to adjust to the new mss but 9537 * do not perform a slow start specifically. 9538 */ 9539 static void 9540 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9541 { 9542 uint32_t mss_max; 9543 tcp_stack_t *tcps = tcp->tcp_tcps; 9544 9545 if (tcp->tcp_ipversion == IPV4_VERSION) 9546 mss_max = tcps->tcps_mss_max_ipv4; 9547 else 9548 mss_max = tcps->tcps_mss_max_ipv6; 9549 9550 if (mss < tcps->tcps_mss_min) 9551 mss = tcps->tcps_mss_min; 9552 if (mss > mss_max) 9553 mss = mss_max; 9554 /* 9555 * Unless naglim has been set by our client to 9556 * a non-mss value, force naglim to track mss. 9557 * This can help to aggregate small writes. 9558 */ 9559 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9560 tcp->tcp_naglim = mss; 9561 /* 9562 * TCP should be able to buffer at least 4 MSS data for obvious 9563 * performance reason. 9564 */ 9565 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9566 tcp->tcp_xmit_hiwater = mss << 2; 9567 9568 /* 9569 * Check if we need to apply the tcp_init_cwnd here. If 9570 * it is set and the MSS gets bigger (should not happen 9571 * normally), we need to adjust the resulting tcp_cwnd properly. 9572 * The new tcp_cwnd should not get bigger. 9573 */ 9574 /* 9575 * We need to avoid setting tcp_cwnd to its slow start value 9576 * unnecessarily. However we have to let the tcp_cwnd adjust 9577 * to the modified mss. 9578 */ 9579 if (tcp->tcp_init_cwnd == 0 && do_ss) { 9580 tcp->tcp_cwnd = MIN(tcps->tcps_slow_start_initial * 9581 mss, MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9582 } else { 9583 if (tcp->tcp_mss < mss) { 9584 tcp->tcp_cwnd = MAX(1, 9585 (tcp->tcp_init_cwnd * tcp->tcp_mss / 9586 mss)) * mss; 9587 } else { 9588 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9589 } 9590 } 9591 tcp->tcp_mss = mss; 9592 tcp->tcp_cwnd_cnt = 0; 9593 (void) tcp_maxpsz_set(tcp, B_TRUE); 9594 } 9595 9596 /* For /dev/tcp aka AF_INET open */ 9597 static int 9598 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9599 { 9600 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9601 } 9602 9603 /* For /dev/tcp6 aka AF_INET6 open */ 9604 static int 9605 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9606 { 9607 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9608 } 9609 9610 static int 9611 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9612 boolean_t isv6) 9613 { 9614 tcp_t *tcp = NULL; 9615 conn_t *connp; 9616 int err; 9617 dev_t conn_dev; 9618 zoneid_t zoneid; 9619 tcp_stack_t *tcps = NULL; 9620 9621 if (q->q_ptr != NULL) 9622 return (0); 9623 9624 if (sflag == MODOPEN) 9625 return (EINVAL); 9626 9627 if (!(flag & SO_ACCEPTOR)) { 9628 /* 9629 * Special case for install: miniroot needs to be able to 9630 * access files via NFS as though it were always in the 9631 * global zone. 9632 */ 9633 if (credp == kcred && nfs_global_client_only != 0) { 9634 zoneid = GLOBAL_ZONEID; 9635 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9636 netstack_tcp; 9637 ASSERT(tcps != NULL); 9638 } else { 9639 netstack_t *ns; 9640 9641 ns = netstack_find_by_cred(credp); 9642 ASSERT(ns != NULL); 9643 tcps = ns->netstack_tcp; 9644 ASSERT(tcps != NULL); 9645 9646 /* 9647 * For exclusive stacks we set the zoneid to zero 9648 * to make TCP operate as if in the global zone. 9649 */ 9650 if (tcps->tcps_netstack->netstack_stackid != 9651 GLOBAL_NETSTACKID) 9652 zoneid = GLOBAL_ZONEID; 9653 else 9654 zoneid = crgetzoneid(credp); 9655 } 9656 /* 9657 * For stackid zero this is done from strplumb.c, but 9658 * non-zero stackids are handled here. 9659 */ 9660 if (tcps->tcps_g_q == NULL && 9661 tcps->tcps_netstack->netstack_stackid != 9662 GLOBAL_NETSTACKID) { 9663 tcp_g_q_setup(tcps); 9664 } 9665 } 9666 9667 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 9668 if (tcps != NULL) 9669 netstack_rele(tcps->tcps_netstack); 9670 return (EBUSY); 9671 } 9672 9673 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9674 9675 if (flag & SO_ACCEPTOR) { 9676 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9677 ASSERT(tcps == NULL); 9678 q->q_qinfo = &tcp_acceptor_rinit; 9679 q->q_ptr = (void *)conn_dev; 9680 WR(q)->q_qinfo = &tcp_acceptor_winit; 9681 WR(q)->q_ptr = (void *)conn_dev; 9682 qprocson(q); 9683 return (0); 9684 } 9685 9686 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9687 /* 9688 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9689 * so we drop it by one. 9690 */ 9691 netstack_rele(tcps->tcps_netstack); 9692 if (connp == NULL) { 9693 inet_minor_free(ip_minor_arena, conn_dev); 9694 q->q_ptr = NULL; 9695 return (ENOSR); 9696 } 9697 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9698 tcp = connp->conn_tcp; 9699 9700 q->q_ptr = WR(q)->q_ptr = connp; 9701 if (isv6) { 9702 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9703 connp->conn_send = ip_output_v6; 9704 connp->conn_af_isv6 = B_TRUE; 9705 connp->conn_pkt_isv6 = B_TRUE; 9706 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9707 tcp->tcp_ipversion = IPV6_VERSION; 9708 tcp->tcp_family = AF_INET6; 9709 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9710 } else { 9711 connp->conn_flags |= IPCL_TCP4; 9712 connp->conn_send = ip_output; 9713 connp->conn_af_isv6 = B_FALSE; 9714 connp->conn_pkt_isv6 = B_FALSE; 9715 tcp->tcp_ipversion = IPV4_VERSION; 9716 tcp->tcp_family = AF_INET; 9717 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9718 } 9719 9720 /* 9721 * TCP keeps a copy of cred for cache locality reasons but 9722 * we put a reference only once. If connp->conn_cred 9723 * becomes invalid, tcp_cred should also be set to NULL. 9724 */ 9725 tcp->tcp_cred = connp->conn_cred = credp; 9726 crhold(connp->conn_cred); 9727 tcp->tcp_cpid = curproc->p_pid; 9728 tcp->tcp_open_time = lbolt64; 9729 connp->conn_zoneid = zoneid; 9730 connp->conn_mlp_type = mlptSingle; 9731 connp->conn_ulp_labeled = !is_system_labeled(); 9732 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9733 ASSERT(tcp->tcp_tcps == tcps); 9734 9735 /* 9736 * If the caller has the process-wide flag set, then default to MAC 9737 * exempt mode. This allows read-down to unlabeled hosts. 9738 */ 9739 if (getpflags(NET_MAC_AWARE, credp) != 0) 9740 connp->conn_mac_exempt = B_TRUE; 9741 9742 connp->conn_dev = conn_dev; 9743 9744 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9745 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9746 9747 if (flag & SO_SOCKSTR) { 9748 /* 9749 * No need to insert a socket in tcp acceptor hash. 9750 * If it was a socket acceptor stream, we dealt with 9751 * it above. A socket listener can never accept a 9752 * connection and doesn't need acceptor_id. 9753 */ 9754 connp->conn_flags |= IPCL_SOCKET; 9755 tcp->tcp_issocket = 1; 9756 WR(q)->q_qinfo = &tcp_sock_winit; 9757 } else { 9758 #ifdef _ILP32 9759 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9760 #else 9761 tcp->tcp_acceptor_id = conn_dev; 9762 #endif /* _ILP32 */ 9763 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9764 } 9765 9766 if (tcps->tcps_trace) 9767 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9768 9769 err = tcp_init(tcp, q); 9770 if (err != 0) { 9771 inet_minor_free(ip_minor_arena, connp->conn_dev); 9772 tcp_acceptor_hash_remove(tcp); 9773 CONN_DEC_REF(connp); 9774 q->q_ptr = WR(q)->q_ptr = NULL; 9775 return (err); 9776 } 9777 9778 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9779 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9780 9781 /* Non-zero default values */ 9782 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9783 /* 9784 * Put the ref for TCP. Ref for IP was already put 9785 * by ipcl_conn_create. Also Make the conn_t globally 9786 * visible to walkers 9787 */ 9788 mutex_enter(&connp->conn_lock); 9789 CONN_INC_REF_LOCKED(connp); 9790 ASSERT(connp->conn_ref == 2); 9791 connp->conn_state_flags &= ~CONN_INCIPIENT; 9792 mutex_exit(&connp->conn_lock); 9793 9794 qprocson(q); 9795 return (0); 9796 } 9797 9798 /* 9799 * Some TCP options can be "set" by requesting them in the option 9800 * buffer. This is needed for XTI feature test though we do not 9801 * allow it in general. We interpret that this mechanism is more 9802 * applicable to OSI protocols and need not be allowed in general. 9803 * This routine filters out options for which it is not allowed (most) 9804 * and lets through those (few) for which it is. [ The XTI interface 9805 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9806 * ever implemented will have to be allowed here ]. 9807 */ 9808 static boolean_t 9809 tcp_allow_connopt_set(int level, int name) 9810 { 9811 9812 switch (level) { 9813 case IPPROTO_TCP: 9814 switch (name) { 9815 case TCP_NODELAY: 9816 return (B_TRUE); 9817 default: 9818 return (B_FALSE); 9819 } 9820 /*NOTREACHED*/ 9821 default: 9822 return (B_FALSE); 9823 } 9824 /*NOTREACHED*/ 9825 } 9826 9827 /* 9828 * This routine gets default values of certain options whose default 9829 * values are maintained by protocol specific code 9830 */ 9831 /* ARGSUSED */ 9832 int 9833 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9834 { 9835 int32_t *i1 = (int32_t *)ptr; 9836 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9837 9838 switch (level) { 9839 case IPPROTO_TCP: 9840 switch (name) { 9841 case TCP_NOTIFY_THRESHOLD: 9842 *i1 = tcps->tcps_ip_notify_interval; 9843 break; 9844 case TCP_ABORT_THRESHOLD: 9845 *i1 = tcps->tcps_ip_abort_interval; 9846 break; 9847 case TCP_CONN_NOTIFY_THRESHOLD: 9848 *i1 = tcps->tcps_ip_notify_cinterval; 9849 break; 9850 case TCP_CONN_ABORT_THRESHOLD: 9851 *i1 = tcps->tcps_ip_abort_cinterval; 9852 break; 9853 default: 9854 return (-1); 9855 } 9856 break; 9857 case IPPROTO_IP: 9858 switch (name) { 9859 case IP_TTL: 9860 *i1 = tcps->tcps_ipv4_ttl; 9861 break; 9862 default: 9863 return (-1); 9864 } 9865 break; 9866 case IPPROTO_IPV6: 9867 switch (name) { 9868 case IPV6_UNICAST_HOPS: 9869 *i1 = tcps->tcps_ipv6_hoplimit; 9870 break; 9871 default: 9872 return (-1); 9873 } 9874 break; 9875 default: 9876 return (-1); 9877 } 9878 return (sizeof (int)); 9879 } 9880 9881 9882 /* 9883 * TCP routine to get the values of options. 9884 */ 9885 int 9886 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9887 { 9888 int *i1 = (int *)ptr; 9889 conn_t *connp = Q_TO_CONN(q); 9890 tcp_t *tcp = connp->conn_tcp; 9891 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9892 9893 switch (level) { 9894 case SOL_SOCKET: 9895 switch (name) { 9896 case SO_LINGER: { 9897 struct linger *lgr = (struct linger *)ptr; 9898 9899 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9900 lgr->l_linger = tcp->tcp_lingertime; 9901 } 9902 return (sizeof (struct linger)); 9903 case SO_DEBUG: 9904 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9905 break; 9906 case SO_KEEPALIVE: 9907 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9908 break; 9909 case SO_DONTROUTE: 9910 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9911 break; 9912 case SO_USELOOPBACK: 9913 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9914 break; 9915 case SO_BROADCAST: 9916 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9917 break; 9918 case SO_REUSEADDR: 9919 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9920 break; 9921 case SO_OOBINLINE: 9922 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9923 break; 9924 case SO_DGRAM_ERRIND: 9925 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9926 break; 9927 case SO_TYPE: 9928 *i1 = SOCK_STREAM; 9929 break; 9930 case SO_SNDBUF: 9931 *i1 = tcp->tcp_xmit_hiwater; 9932 break; 9933 case SO_RCVBUF: 9934 *i1 = RD(q)->q_hiwat; 9935 break; 9936 case SO_SND_COPYAVOID: 9937 *i1 = tcp->tcp_snd_zcopy_on ? 9938 SO_SND_COPYAVOID : 0; 9939 break; 9940 case SO_ALLZONES: 9941 *i1 = connp->conn_allzones ? 1 : 0; 9942 break; 9943 case SO_ANON_MLP: 9944 *i1 = connp->conn_anon_mlp; 9945 break; 9946 case SO_MAC_EXEMPT: 9947 *i1 = connp->conn_mac_exempt; 9948 break; 9949 case SO_EXCLBIND: 9950 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9951 break; 9952 case SO_PROTOTYPE: 9953 *i1 = IPPROTO_TCP; 9954 break; 9955 case SO_DOMAIN: 9956 *i1 = tcp->tcp_family; 9957 break; 9958 default: 9959 return (-1); 9960 } 9961 break; 9962 case IPPROTO_TCP: 9963 switch (name) { 9964 case TCP_NODELAY: 9965 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9966 break; 9967 case TCP_MAXSEG: 9968 *i1 = tcp->tcp_mss; 9969 break; 9970 case TCP_NOTIFY_THRESHOLD: 9971 *i1 = (int)tcp->tcp_first_timer_threshold; 9972 break; 9973 case TCP_ABORT_THRESHOLD: 9974 *i1 = tcp->tcp_second_timer_threshold; 9975 break; 9976 case TCP_CONN_NOTIFY_THRESHOLD: 9977 *i1 = tcp->tcp_first_ctimer_threshold; 9978 break; 9979 case TCP_CONN_ABORT_THRESHOLD: 9980 *i1 = tcp->tcp_second_ctimer_threshold; 9981 break; 9982 case TCP_RECVDSTADDR: 9983 *i1 = tcp->tcp_recvdstaddr; 9984 break; 9985 case TCP_ANONPRIVBIND: 9986 *i1 = tcp->tcp_anon_priv_bind; 9987 break; 9988 case TCP_EXCLBIND: 9989 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9990 break; 9991 case TCP_INIT_CWND: 9992 *i1 = tcp->tcp_init_cwnd; 9993 break; 9994 case TCP_KEEPALIVE_THRESHOLD: 9995 *i1 = tcp->tcp_ka_interval; 9996 break; 9997 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9998 *i1 = tcp->tcp_ka_abort_thres; 9999 break; 10000 case TCP_CORK: 10001 *i1 = tcp->tcp_cork; 10002 break; 10003 default: 10004 return (-1); 10005 } 10006 break; 10007 case IPPROTO_IP: 10008 if (tcp->tcp_family != AF_INET) 10009 return (-1); 10010 switch (name) { 10011 case IP_OPTIONS: 10012 case T_IP_OPTIONS: { 10013 /* 10014 * This is compatible with BSD in that in only return 10015 * the reverse source route with the final destination 10016 * as the last entry. The first 4 bytes of the option 10017 * will contain the final destination. 10018 */ 10019 int opt_len; 10020 10021 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 10022 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 10023 ASSERT(opt_len >= 0); 10024 /* Caller ensures enough space */ 10025 if (opt_len > 0) { 10026 /* 10027 * TODO: Do we have to handle getsockopt on an 10028 * initiator as well? 10029 */ 10030 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 10031 } 10032 return (0); 10033 } 10034 case IP_TOS: 10035 case T_IP_TOS: 10036 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10037 break; 10038 case IP_TTL: 10039 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10040 break; 10041 case IP_NEXTHOP: 10042 /* Handled at IP level */ 10043 return (-EINVAL); 10044 default: 10045 return (-1); 10046 } 10047 break; 10048 case IPPROTO_IPV6: 10049 /* 10050 * IPPROTO_IPV6 options are only supported for sockets 10051 * that are using IPv6 on the wire. 10052 */ 10053 if (tcp->tcp_ipversion != IPV6_VERSION) { 10054 return (-1); 10055 } 10056 switch (name) { 10057 case IPV6_UNICAST_HOPS: 10058 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10059 break; /* goto sizeof (int) option return */ 10060 case IPV6_BOUND_IF: 10061 /* Zero if not set */ 10062 *i1 = tcp->tcp_bound_if; 10063 break; /* goto sizeof (int) option return */ 10064 case IPV6_RECVPKTINFO: 10065 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10066 *i1 = 1; 10067 else 10068 *i1 = 0; 10069 break; /* goto sizeof (int) option return */ 10070 case IPV6_RECVTCLASS: 10071 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10072 *i1 = 1; 10073 else 10074 *i1 = 0; 10075 break; /* goto sizeof (int) option return */ 10076 case IPV6_RECVHOPLIMIT: 10077 if (tcp->tcp_ipv6_recvancillary & 10078 TCP_IPV6_RECVHOPLIMIT) 10079 *i1 = 1; 10080 else 10081 *i1 = 0; 10082 break; /* goto sizeof (int) option return */ 10083 case IPV6_RECVHOPOPTS: 10084 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10085 *i1 = 1; 10086 else 10087 *i1 = 0; 10088 break; /* goto sizeof (int) option return */ 10089 case IPV6_RECVDSTOPTS: 10090 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10091 *i1 = 1; 10092 else 10093 *i1 = 0; 10094 break; /* goto sizeof (int) option return */ 10095 case _OLD_IPV6_RECVDSTOPTS: 10096 if (tcp->tcp_ipv6_recvancillary & 10097 TCP_OLD_IPV6_RECVDSTOPTS) 10098 *i1 = 1; 10099 else 10100 *i1 = 0; 10101 break; /* goto sizeof (int) option return */ 10102 case IPV6_RECVRTHDR: 10103 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10104 *i1 = 1; 10105 else 10106 *i1 = 0; 10107 break; /* goto sizeof (int) option return */ 10108 case IPV6_RECVRTHDRDSTOPTS: 10109 if (tcp->tcp_ipv6_recvancillary & 10110 TCP_IPV6_RECVRTDSTOPTS) 10111 *i1 = 1; 10112 else 10113 *i1 = 0; 10114 break; /* goto sizeof (int) option return */ 10115 case IPV6_PKTINFO: { 10116 /* XXX assumes that caller has room for max size! */ 10117 struct in6_pktinfo *pkti; 10118 10119 pkti = (struct in6_pktinfo *)ptr; 10120 if (ipp->ipp_fields & IPPF_IFINDEX) 10121 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10122 else 10123 pkti->ipi6_ifindex = 0; 10124 if (ipp->ipp_fields & IPPF_ADDR) 10125 pkti->ipi6_addr = ipp->ipp_addr; 10126 else 10127 pkti->ipi6_addr = ipv6_all_zeros; 10128 return (sizeof (struct in6_pktinfo)); 10129 } 10130 case IPV6_TCLASS: 10131 if (ipp->ipp_fields & IPPF_TCLASS) 10132 *i1 = ipp->ipp_tclass; 10133 else 10134 *i1 = IPV6_FLOW_TCLASS( 10135 IPV6_DEFAULT_VERS_AND_FLOW); 10136 break; /* goto sizeof (int) option return */ 10137 case IPV6_NEXTHOP: { 10138 sin6_t *sin6 = (sin6_t *)ptr; 10139 10140 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10141 return (0); 10142 *sin6 = sin6_null; 10143 sin6->sin6_family = AF_INET6; 10144 sin6->sin6_addr = ipp->ipp_nexthop; 10145 return (sizeof (sin6_t)); 10146 } 10147 case IPV6_HOPOPTS: 10148 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10149 return (0); 10150 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10151 return (0); 10152 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10153 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10154 if (tcp->tcp_label_len > 0) { 10155 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10156 ptr[1] = (ipp->ipp_hopoptslen - 10157 tcp->tcp_label_len + 7) / 8 - 1; 10158 } 10159 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10160 case IPV6_RTHDRDSTOPTS: 10161 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10162 return (0); 10163 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10164 return (ipp->ipp_rtdstoptslen); 10165 case IPV6_RTHDR: 10166 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10167 return (0); 10168 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10169 return (ipp->ipp_rthdrlen); 10170 case IPV6_DSTOPTS: 10171 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10172 return (0); 10173 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10174 return (ipp->ipp_dstoptslen); 10175 case IPV6_SRC_PREFERENCES: 10176 return (ip6_get_src_preferences(connp, 10177 (uint32_t *)ptr)); 10178 case IPV6_PATHMTU: { 10179 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10180 10181 if (tcp->tcp_state < TCPS_ESTABLISHED) 10182 return (-1); 10183 10184 return (ip_fill_mtuinfo(&connp->conn_remv6, 10185 connp->conn_fport, mtuinfo, 10186 connp->conn_netstack)); 10187 } 10188 default: 10189 return (-1); 10190 } 10191 break; 10192 default: 10193 return (-1); 10194 } 10195 return (sizeof (int)); 10196 } 10197 10198 /* 10199 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10200 * Parameters are assumed to be verified by the caller. 10201 */ 10202 /* ARGSUSED */ 10203 int 10204 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10205 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10206 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10207 { 10208 conn_t *connp = Q_TO_CONN(q); 10209 tcp_t *tcp = connp->conn_tcp; 10210 int *i1 = (int *)invalp; 10211 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10212 boolean_t checkonly; 10213 int reterr; 10214 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10215 10216 switch (optset_context) { 10217 case SETFN_OPTCOM_CHECKONLY: 10218 checkonly = B_TRUE; 10219 /* 10220 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10221 * inlen != 0 implies value supplied and 10222 * we have to "pretend" to set it. 10223 * inlen == 0 implies that there is no 10224 * value part in T_CHECK request and just validation 10225 * done elsewhere should be enough, we just return here. 10226 */ 10227 if (inlen == 0) { 10228 *outlenp = 0; 10229 return (0); 10230 } 10231 break; 10232 case SETFN_OPTCOM_NEGOTIATE: 10233 checkonly = B_FALSE; 10234 break; 10235 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10236 case SETFN_CONN_NEGOTIATE: 10237 checkonly = B_FALSE; 10238 /* 10239 * Negotiating local and "association-related" options 10240 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10241 * primitives is allowed by XTI, but we choose 10242 * to not implement this style negotiation for Internet 10243 * protocols (We interpret it is a must for OSI world but 10244 * optional for Internet protocols) for all options. 10245 * [ Will do only for the few options that enable test 10246 * suites that our XTI implementation of this feature 10247 * works for transports that do allow it ] 10248 */ 10249 if (!tcp_allow_connopt_set(level, name)) { 10250 *outlenp = 0; 10251 return (EINVAL); 10252 } 10253 break; 10254 default: 10255 /* 10256 * We should never get here 10257 */ 10258 *outlenp = 0; 10259 return (EINVAL); 10260 } 10261 10262 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10263 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10264 10265 /* 10266 * For TCP, we should have no ancillary data sent down 10267 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10268 * has to be zero. 10269 */ 10270 ASSERT(thisdg_attrs == NULL); 10271 10272 /* 10273 * For fixed length options, no sanity check 10274 * of passed in length is done. It is assumed *_optcom_req() 10275 * routines do the right thing. 10276 */ 10277 10278 switch (level) { 10279 case SOL_SOCKET: 10280 switch (name) { 10281 case SO_LINGER: { 10282 struct linger *lgr = (struct linger *)invalp; 10283 10284 if (!checkonly) { 10285 if (lgr->l_onoff) { 10286 tcp->tcp_linger = 1; 10287 tcp->tcp_lingertime = lgr->l_linger; 10288 } else { 10289 tcp->tcp_linger = 0; 10290 tcp->tcp_lingertime = 0; 10291 } 10292 /* struct copy */ 10293 *(struct linger *)outvalp = *lgr; 10294 } else { 10295 if (!lgr->l_onoff) { 10296 ((struct linger *) 10297 outvalp)->l_onoff = 0; 10298 ((struct linger *) 10299 outvalp)->l_linger = 0; 10300 } else { 10301 /* struct copy */ 10302 *(struct linger *)outvalp = *lgr; 10303 } 10304 } 10305 *outlenp = sizeof (struct linger); 10306 return (0); 10307 } 10308 case SO_DEBUG: 10309 if (!checkonly) 10310 tcp->tcp_debug = onoff; 10311 break; 10312 case SO_KEEPALIVE: 10313 if (checkonly) { 10314 /* T_CHECK case */ 10315 break; 10316 } 10317 10318 if (!onoff) { 10319 if (tcp->tcp_ka_enabled) { 10320 if (tcp->tcp_ka_tid != 0) { 10321 (void) TCP_TIMER_CANCEL(tcp, 10322 tcp->tcp_ka_tid); 10323 tcp->tcp_ka_tid = 0; 10324 } 10325 tcp->tcp_ka_enabled = 0; 10326 } 10327 break; 10328 } 10329 if (!tcp->tcp_ka_enabled) { 10330 /* Crank up the keepalive timer */ 10331 tcp->tcp_ka_last_intrvl = 0; 10332 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10333 tcp_keepalive_killer, 10334 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10335 tcp->tcp_ka_enabled = 1; 10336 } 10337 break; 10338 case SO_DONTROUTE: 10339 /* 10340 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10341 * only of interest to IP. We track them here only so 10342 * that we can report their current value. 10343 */ 10344 if (!checkonly) { 10345 tcp->tcp_dontroute = onoff; 10346 tcp->tcp_connp->conn_dontroute = onoff; 10347 } 10348 break; 10349 case SO_USELOOPBACK: 10350 if (!checkonly) { 10351 tcp->tcp_useloopback = onoff; 10352 tcp->tcp_connp->conn_loopback = onoff; 10353 } 10354 break; 10355 case SO_BROADCAST: 10356 if (!checkonly) { 10357 tcp->tcp_broadcast = onoff; 10358 tcp->tcp_connp->conn_broadcast = onoff; 10359 } 10360 break; 10361 case SO_REUSEADDR: 10362 if (!checkonly) { 10363 tcp->tcp_reuseaddr = onoff; 10364 tcp->tcp_connp->conn_reuseaddr = onoff; 10365 } 10366 break; 10367 case SO_OOBINLINE: 10368 if (!checkonly) 10369 tcp->tcp_oobinline = onoff; 10370 break; 10371 case SO_DGRAM_ERRIND: 10372 if (!checkonly) 10373 tcp->tcp_dgram_errind = onoff; 10374 break; 10375 case SO_SNDBUF: { 10376 if (*i1 > tcps->tcps_max_buf) { 10377 *outlenp = 0; 10378 return (ENOBUFS); 10379 } 10380 if (checkonly) 10381 break; 10382 10383 tcp->tcp_xmit_hiwater = *i1; 10384 if (tcps->tcps_snd_lowat_fraction != 0) 10385 tcp->tcp_xmit_lowater = 10386 tcp->tcp_xmit_hiwater / 10387 tcps->tcps_snd_lowat_fraction; 10388 (void) tcp_maxpsz_set(tcp, B_TRUE); 10389 /* 10390 * If we are flow-controlled, recheck the condition. 10391 * There are apps that increase SO_SNDBUF size when 10392 * flow-controlled (EWOULDBLOCK), and expect the flow 10393 * control condition to be lifted right away. 10394 */ 10395 mutex_enter(&tcp->tcp_non_sq_lock); 10396 if (tcp->tcp_flow_stopped && 10397 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10398 tcp_clrqfull(tcp); 10399 } 10400 mutex_exit(&tcp->tcp_non_sq_lock); 10401 break; 10402 } 10403 case SO_RCVBUF: 10404 if (*i1 > tcps->tcps_max_buf) { 10405 *outlenp = 0; 10406 return (ENOBUFS); 10407 } 10408 /* Silently ignore zero */ 10409 if (!checkonly && *i1 != 0) { 10410 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10411 (void) tcp_rwnd_set(tcp, *i1); 10412 } 10413 /* 10414 * XXX should we return the rwnd here 10415 * and tcp_opt_get ? 10416 */ 10417 break; 10418 case SO_SND_COPYAVOID: 10419 if (!checkonly) { 10420 /* we only allow enable at most once for now */ 10421 if (tcp->tcp_loopback || 10422 (!tcp->tcp_snd_zcopy_aware && 10423 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10424 *outlenp = 0; 10425 return (EOPNOTSUPP); 10426 } 10427 tcp->tcp_snd_zcopy_aware = 1; 10428 } 10429 break; 10430 case SO_ALLZONES: 10431 /* Handled at the IP level */ 10432 return (-EINVAL); 10433 case SO_ANON_MLP: 10434 if (!checkonly) { 10435 mutex_enter(&connp->conn_lock); 10436 connp->conn_anon_mlp = onoff; 10437 mutex_exit(&connp->conn_lock); 10438 } 10439 break; 10440 case SO_MAC_EXEMPT: 10441 if (secpolicy_net_mac_aware(cr) != 0 || 10442 IPCL_IS_BOUND(connp)) 10443 return (EACCES); 10444 if (!checkonly) { 10445 mutex_enter(&connp->conn_lock); 10446 connp->conn_mac_exempt = onoff; 10447 mutex_exit(&connp->conn_lock); 10448 } 10449 break; 10450 case SO_EXCLBIND: 10451 if (!checkonly) 10452 tcp->tcp_exclbind = onoff; 10453 break; 10454 default: 10455 *outlenp = 0; 10456 return (EINVAL); 10457 } 10458 break; 10459 case IPPROTO_TCP: 10460 switch (name) { 10461 case TCP_NODELAY: 10462 if (!checkonly) 10463 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10464 break; 10465 case TCP_NOTIFY_THRESHOLD: 10466 if (!checkonly) 10467 tcp->tcp_first_timer_threshold = *i1; 10468 break; 10469 case TCP_ABORT_THRESHOLD: 10470 if (!checkonly) 10471 tcp->tcp_second_timer_threshold = *i1; 10472 break; 10473 case TCP_CONN_NOTIFY_THRESHOLD: 10474 if (!checkonly) 10475 tcp->tcp_first_ctimer_threshold = *i1; 10476 break; 10477 case TCP_CONN_ABORT_THRESHOLD: 10478 if (!checkonly) 10479 tcp->tcp_second_ctimer_threshold = *i1; 10480 break; 10481 case TCP_RECVDSTADDR: 10482 if (tcp->tcp_state > TCPS_LISTEN) 10483 return (EOPNOTSUPP); 10484 if (!checkonly) 10485 tcp->tcp_recvdstaddr = onoff; 10486 break; 10487 case TCP_ANONPRIVBIND: 10488 if ((reterr = secpolicy_net_privaddr(cr, 0)) != 0) { 10489 *outlenp = 0; 10490 return (reterr); 10491 } 10492 if (!checkonly) { 10493 tcp->tcp_anon_priv_bind = onoff; 10494 } 10495 break; 10496 case TCP_EXCLBIND: 10497 if (!checkonly) 10498 tcp->tcp_exclbind = onoff; 10499 break; /* goto sizeof (int) option return */ 10500 case TCP_INIT_CWND: { 10501 uint32_t init_cwnd = *((uint32_t *)invalp); 10502 10503 if (checkonly) 10504 break; 10505 10506 /* 10507 * Only allow socket with network configuration 10508 * privilege to set the initial cwnd to be larger 10509 * than allowed by RFC 3390. 10510 */ 10511 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10512 tcp->tcp_init_cwnd = init_cwnd; 10513 break; 10514 } 10515 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10516 *outlenp = 0; 10517 return (reterr); 10518 } 10519 if (init_cwnd > TCP_MAX_INIT_CWND) { 10520 *outlenp = 0; 10521 return (EINVAL); 10522 } 10523 tcp->tcp_init_cwnd = init_cwnd; 10524 break; 10525 } 10526 case TCP_KEEPALIVE_THRESHOLD: 10527 if (checkonly) 10528 break; 10529 10530 if (*i1 < tcps->tcps_keepalive_interval_low || 10531 *i1 > tcps->tcps_keepalive_interval_high) { 10532 *outlenp = 0; 10533 return (EINVAL); 10534 } 10535 if (*i1 != tcp->tcp_ka_interval) { 10536 tcp->tcp_ka_interval = *i1; 10537 /* 10538 * Check if we need to restart the 10539 * keepalive timer. 10540 */ 10541 if (tcp->tcp_ka_tid != 0) { 10542 ASSERT(tcp->tcp_ka_enabled); 10543 (void) TCP_TIMER_CANCEL(tcp, 10544 tcp->tcp_ka_tid); 10545 tcp->tcp_ka_last_intrvl = 0; 10546 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10547 tcp_keepalive_killer, 10548 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10549 } 10550 } 10551 break; 10552 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10553 if (!checkonly) { 10554 if (*i1 < 10555 tcps->tcps_keepalive_abort_interval_low || 10556 *i1 > 10557 tcps->tcps_keepalive_abort_interval_high) { 10558 *outlenp = 0; 10559 return (EINVAL); 10560 } 10561 tcp->tcp_ka_abort_thres = *i1; 10562 } 10563 break; 10564 case TCP_CORK: 10565 if (!checkonly) { 10566 /* 10567 * if tcp->tcp_cork was set and is now 10568 * being unset, we have to make sure that 10569 * the remaining data gets sent out. Also 10570 * unset tcp->tcp_cork so that tcp_wput_data() 10571 * can send data even if it is less than mss 10572 */ 10573 if (tcp->tcp_cork && onoff == 0 && 10574 tcp->tcp_unsent > 0) { 10575 tcp->tcp_cork = B_FALSE; 10576 tcp_wput_data(tcp, NULL, B_FALSE); 10577 } 10578 tcp->tcp_cork = onoff; 10579 } 10580 break; 10581 default: 10582 *outlenp = 0; 10583 return (EINVAL); 10584 } 10585 break; 10586 case IPPROTO_IP: 10587 if (tcp->tcp_family != AF_INET) { 10588 *outlenp = 0; 10589 return (ENOPROTOOPT); 10590 } 10591 switch (name) { 10592 case IP_OPTIONS: 10593 case T_IP_OPTIONS: 10594 reterr = tcp_opt_set_header(tcp, checkonly, 10595 invalp, inlen); 10596 if (reterr) { 10597 *outlenp = 0; 10598 return (reterr); 10599 } 10600 /* OK return - copy input buffer into output buffer */ 10601 if (invalp != outvalp) { 10602 /* don't trust bcopy for identical src/dst */ 10603 bcopy(invalp, outvalp, inlen); 10604 } 10605 *outlenp = inlen; 10606 return (0); 10607 case IP_TOS: 10608 case T_IP_TOS: 10609 if (!checkonly) { 10610 tcp->tcp_ipha->ipha_type_of_service = 10611 (uchar_t)*i1; 10612 tcp->tcp_tos = (uchar_t)*i1; 10613 } 10614 break; 10615 case IP_TTL: 10616 if (!checkonly) { 10617 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10618 tcp->tcp_ttl = (uchar_t)*i1; 10619 } 10620 break; 10621 case IP_BOUND_IF: 10622 case IP_NEXTHOP: 10623 /* Handled at the IP level */ 10624 return (-EINVAL); 10625 case IP_SEC_OPT: 10626 /* 10627 * We should not allow policy setting after 10628 * we start listening for connections. 10629 */ 10630 if (tcp->tcp_state == TCPS_LISTEN) { 10631 return (EINVAL); 10632 } else { 10633 /* Handled at the IP level */ 10634 return (-EINVAL); 10635 } 10636 default: 10637 *outlenp = 0; 10638 return (EINVAL); 10639 } 10640 break; 10641 case IPPROTO_IPV6: { 10642 ip6_pkt_t *ipp; 10643 10644 /* 10645 * IPPROTO_IPV6 options are only supported for sockets 10646 * that are using IPv6 on the wire. 10647 */ 10648 if (tcp->tcp_ipversion != IPV6_VERSION) { 10649 *outlenp = 0; 10650 return (ENOPROTOOPT); 10651 } 10652 /* 10653 * Only sticky options; no ancillary data 10654 */ 10655 ASSERT(thisdg_attrs == NULL); 10656 ipp = &tcp->tcp_sticky_ipp; 10657 10658 switch (name) { 10659 case IPV6_UNICAST_HOPS: 10660 /* -1 means use default */ 10661 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10662 *outlenp = 0; 10663 return (EINVAL); 10664 } 10665 if (!checkonly) { 10666 if (*i1 == -1) { 10667 tcp->tcp_ip6h->ip6_hops = 10668 ipp->ipp_unicast_hops = 10669 (uint8_t)tcps->tcps_ipv6_hoplimit; 10670 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10671 /* Pass modified value to IP. */ 10672 *i1 = tcp->tcp_ip6h->ip6_hops; 10673 } else { 10674 tcp->tcp_ip6h->ip6_hops = 10675 ipp->ipp_unicast_hops = 10676 (uint8_t)*i1; 10677 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10678 } 10679 reterr = tcp_build_hdrs(q, tcp); 10680 if (reterr != 0) 10681 return (reterr); 10682 } 10683 break; 10684 case IPV6_BOUND_IF: 10685 if (!checkonly) { 10686 int error = 0; 10687 10688 tcp->tcp_bound_if = *i1; 10689 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10690 B_TRUE, checkonly, level, name, mblk); 10691 if (error != 0) { 10692 *outlenp = 0; 10693 return (error); 10694 } 10695 } 10696 break; 10697 /* 10698 * Set boolean switches for ancillary data delivery 10699 */ 10700 case IPV6_RECVPKTINFO: 10701 if (!checkonly) { 10702 if (onoff) 10703 tcp->tcp_ipv6_recvancillary |= 10704 TCP_IPV6_RECVPKTINFO; 10705 else 10706 tcp->tcp_ipv6_recvancillary &= 10707 ~TCP_IPV6_RECVPKTINFO; 10708 /* Force it to be sent up with the next msg */ 10709 tcp->tcp_recvifindex = 0; 10710 } 10711 break; 10712 case IPV6_RECVTCLASS: 10713 if (!checkonly) { 10714 if (onoff) 10715 tcp->tcp_ipv6_recvancillary |= 10716 TCP_IPV6_RECVTCLASS; 10717 else 10718 tcp->tcp_ipv6_recvancillary &= 10719 ~TCP_IPV6_RECVTCLASS; 10720 } 10721 break; 10722 case IPV6_RECVHOPLIMIT: 10723 if (!checkonly) { 10724 if (onoff) 10725 tcp->tcp_ipv6_recvancillary |= 10726 TCP_IPV6_RECVHOPLIMIT; 10727 else 10728 tcp->tcp_ipv6_recvancillary &= 10729 ~TCP_IPV6_RECVHOPLIMIT; 10730 /* Force it to be sent up with the next msg */ 10731 tcp->tcp_recvhops = 0xffffffffU; 10732 } 10733 break; 10734 case IPV6_RECVHOPOPTS: 10735 if (!checkonly) { 10736 if (onoff) 10737 tcp->tcp_ipv6_recvancillary |= 10738 TCP_IPV6_RECVHOPOPTS; 10739 else 10740 tcp->tcp_ipv6_recvancillary &= 10741 ~TCP_IPV6_RECVHOPOPTS; 10742 } 10743 break; 10744 case IPV6_RECVDSTOPTS: 10745 if (!checkonly) { 10746 if (onoff) 10747 tcp->tcp_ipv6_recvancillary |= 10748 TCP_IPV6_RECVDSTOPTS; 10749 else 10750 tcp->tcp_ipv6_recvancillary &= 10751 ~TCP_IPV6_RECVDSTOPTS; 10752 } 10753 break; 10754 case _OLD_IPV6_RECVDSTOPTS: 10755 if (!checkonly) { 10756 if (onoff) 10757 tcp->tcp_ipv6_recvancillary |= 10758 TCP_OLD_IPV6_RECVDSTOPTS; 10759 else 10760 tcp->tcp_ipv6_recvancillary &= 10761 ~TCP_OLD_IPV6_RECVDSTOPTS; 10762 } 10763 break; 10764 case IPV6_RECVRTHDR: 10765 if (!checkonly) { 10766 if (onoff) 10767 tcp->tcp_ipv6_recvancillary |= 10768 TCP_IPV6_RECVRTHDR; 10769 else 10770 tcp->tcp_ipv6_recvancillary &= 10771 ~TCP_IPV6_RECVRTHDR; 10772 } 10773 break; 10774 case IPV6_RECVRTHDRDSTOPTS: 10775 if (!checkonly) { 10776 if (onoff) 10777 tcp->tcp_ipv6_recvancillary |= 10778 TCP_IPV6_RECVRTDSTOPTS; 10779 else 10780 tcp->tcp_ipv6_recvancillary &= 10781 ~TCP_IPV6_RECVRTDSTOPTS; 10782 } 10783 break; 10784 case IPV6_PKTINFO: 10785 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10786 return (EINVAL); 10787 if (checkonly) 10788 break; 10789 10790 if (inlen == 0) { 10791 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10792 } else { 10793 struct in6_pktinfo *pkti; 10794 10795 pkti = (struct in6_pktinfo *)invalp; 10796 /* 10797 * RFC 3542 states that ipi6_addr must be 10798 * the unspecified address when setting the 10799 * IPV6_PKTINFO sticky socket option on a 10800 * TCP socket. 10801 */ 10802 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10803 return (EINVAL); 10804 /* 10805 * ip6_set_pktinfo() validates the source 10806 * address and interface index. 10807 */ 10808 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10809 pkti, mblk); 10810 if (reterr != 0) 10811 return (reterr); 10812 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10813 ipp->ipp_addr = pkti->ipi6_addr; 10814 if (ipp->ipp_ifindex != 0) 10815 ipp->ipp_fields |= IPPF_IFINDEX; 10816 else 10817 ipp->ipp_fields &= ~IPPF_IFINDEX; 10818 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10819 ipp->ipp_fields |= IPPF_ADDR; 10820 else 10821 ipp->ipp_fields &= ~IPPF_ADDR; 10822 } 10823 reterr = tcp_build_hdrs(q, tcp); 10824 if (reterr != 0) 10825 return (reterr); 10826 break; 10827 case IPV6_TCLASS: 10828 if (inlen != 0 && inlen != sizeof (int)) 10829 return (EINVAL); 10830 if (checkonly) 10831 break; 10832 10833 if (inlen == 0) { 10834 ipp->ipp_fields &= ~IPPF_TCLASS; 10835 } else { 10836 if (*i1 > 255 || *i1 < -1) 10837 return (EINVAL); 10838 if (*i1 == -1) { 10839 ipp->ipp_tclass = 0; 10840 *i1 = 0; 10841 } else { 10842 ipp->ipp_tclass = *i1; 10843 } 10844 ipp->ipp_fields |= IPPF_TCLASS; 10845 } 10846 reterr = tcp_build_hdrs(q, tcp); 10847 if (reterr != 0) 10848 return (reterr); 10849 break; 10850 case IPV6_NEXTHOP: 10851 /* 10852 * IP will verify that the nexthop is reachable 10853 * and fail for sticky options. 10854 */ 10855 if (inlen != 0 && inlen != sizeof (sin6_t)) 10856 return (EINVAL); 10857 if (checkonly) 10858 break; 10859 10860 if (inlen == 0) { 10861 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10862 } else { 10863 sin6_t *sin6 = (sin6_t *)invalp; 10864 10865 if (sin6->sin6_family != AF_INET6) 10866 return (EAFNOSUPPORT); 10867 if (IN6_IS_ADDR_V4MAPPED( 10868 &sin6->sin6_addr)) 10869 return (EADDRNOTAVAIL); 10870 ipp->ipp_nexthop = sin6->sin6_addr; 10871 if (!IN6_IS_ADDR_UNSPECIFIED( 10872 &ipp->ipp_nexthop)) 10873 ipp->ipp_fields |= IPPF_NEXTHOP; 10874 else 10875 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10876 } 10877 reterr = tcp_build_hdrs(q, tcp); 10878 if (reterr != 0) 10879 return (reterr); 10880 break; 10881 case IPV6_HOPOPTS: { 10882 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10883 10884 /* 10885 * Sanity checks - minimum size, size a multiple of 10886 * eight bytes, and matching size passed in. 10887 */ 10888 if (inlen != 0 && 10889 inlen != (8 * (hopts->ip6h_len + 1))) 10890 return (EINVAL); 10891 10892 if (checkonly) 10893 break; 10894 10895 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10896 (uchar_t **)&ipp->ipp_hopopts, 10897 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10898 if (reterr != 0) 10899 return (reterr); 10900 if (ipp->ipp_hopoptslen == 0) 10901 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10902 else 10903 ipp->ipp_fields |= IPPF_HOPOPTS; 10904 reterr = tcp_build_hdrs(q, tcp); 10905 if (reterr != 0) 10906 return (reterr); 10907 break; 10908 } 10909 case IPV6_RTHDRDSTOPTS: { 10910 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10911 10912 /* 10913 * Sanity checks - minimum size, size a multiple of 10914 * eight bytes, and matching size passed in. 10915 */ 10916 if (inlen != 0 && 10917 inlen != (8 * (dopts->ip6d_len + 1))) 10918 return (EINVAL); 10919 10920 if (checkonly) 10921 break; 10922 10923 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10924 (uchar_t **)&ipp->ipp_rtdstopts, 10925 &ipp->ipp_rtdstoptslen, 0); 10926 if (reterr != 0) 10927 return (reterr); 10928 if (ipp->ipp_rtdstoptslen == 0) 10929 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10930 else 10931 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10932 reterr = tcp_build_hdrs(q, tcp); 10933 if (reterr != 0) 10934 return (reterr); 10935 break; 10936 } 10937 case IPV6_DSTOPTS: { 10938 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10939 10940 /* 10941 * Sanity checks - minimum size, size a multiple of 10942 * eight bytes, and matching size passed in. 10943 */ 10944 if (inlen != 0 && 10945 inlen != (8 * (dopts->ip6d_len + 1))) 10946 return (EINVAL); 10947 10948 if (checkonly) 10949 break; 10950 10951 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10952 (uchar_t **)&ipp->ipp_dstopts, 10953 &ipp->ipp_dstoptslen, 0); 10954 if (reterr != 0) 10955 return (reterr); 10956 if (ipp->ipp_dstoptslen == 0) 10957 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10958 else 10959 ipp->ipp_fields |= IPPF_DSTOPTS; 10960 reterr = tcp_build_hdrs(q, tcp); 10961 if (reterr != 0) 10962 return (reterr); 10963 break; 10964 } 10965 case IPV6_RTHDR: { 10966 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10967 10968 /* 10969 * Sanity checks - minimum size, size a multiple of 10970 * eight bytes, and matching size passed in. 10971 */ 10972 if (inlen != 0 && 10973 inlen != (8 * (rt->ip6r_len + 1))) 10974 return (EINVAL); 10975 10976 if (checkonly) 10977 break; 10978 10979 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10980 (uchar_t **)&ipp->ipp_rthdr, 10981 &ipp->ipp_rthdrlen, 0); 10982 if (reterr != 0) 10983 return (reterr); 10984 if (ipp->ipp_rthdrlen == 0) 10985 ipp->ipp_fields &= ~IPPF_RTHDR; 10986 else 10987 ipp->ipp_fields |= IPPF_RTHDR; 10988 reterr = tcp_build_hdrs(q, tcp); 10989 if (reterr != 0) 10990 return (reterr); 10991 break; 10992 } 10993 case IPV6_V6ONLY: 10994 if (!checkonly) 10995 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10996 break; 10997 case IPV6_USE_MIN_MTU: 10998 if (inlen != sizeof (int)) 10999 return (EINVAL); 11000 11001 if (*i1 < -1 || *i1 > 1) 11002 return (EINVAL); 11003 11004 if (checkonly) 11005 break; 11006 11007 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 11008 ipp->ipp_use_min_mtu = *i1; 11009 break; 11010 case IPV6_BOUND_PIF: 11011 /* Handled at the IP level */ 11012 return (-EINVAL); 11013 case IPV6_SEC_OPT: 11014 /* 11015 * We should not allow policy setting after 11016 * we start listening for connections. 11017 */ 11018 if (tcp->tcp_state == TCPS_LISTEN) { 11019 return (EINVAL); 11020 } else { 11021 /* Handled at the IP level */ 11022 return (-EINVAL); 11023 } 11024 case IPV6_SRC_PREFERENCES: 11025 if (inlen != sizeof (uint32_t)) 11026 return (EINVAL); 11027 reterr = ip6_set_src_preferences(tcp->tcp_connp, 11028 *(uint32_t *)invalp); 11029 if (reterr != 0) { 11030 *outlenp = 0; 11031 return (reterr); 11032 } 11033 break; 11034 default: 11035 *outlenp = 0; 11036 return (EINVAL); 11037 } 11038 break; 11039 } /* end IPPROTO_IPV6 */ 11040 default: 11041 *outlenp = 0; 11042 return (EINVAL); 11043 } 11044 /* 11045 * Common case of OK return with outval same as inval 11046 */ 11047 if (invalp != outvalp) { 11048 /* don't trust bcopy for identical src/dst */ 11049 (void) bcopy(invalp, outvalp, inlen); 11050 } 11051 *outlenp = inlen; 11052 return (0); 11053 } 11054 11055 /* 11056 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 11057 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 11058 * headers, and the maximum size tcp header (to avoid reallocation 11059 * on the fly for additional tcp options). 11060 * Returns failure if can't allocate memory. 11061 */ 11062 static int 11063 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 11064 { 11065 char *hdrs; 11066 uint_t hdrs_len; 11067 ip6i_t *ip6i; 11068 char buf[TCP_MAX_HDR_LENGTH]; 11069 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 11070 in6_addr_t src, dst; 11071 tcp_stack_t *tcps = tcp->tcp_tcps; 11072 11073 /* 11074 * save the existing tcp header and source/dest IP addresses 11075 */ 11076 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 11077 src = tcp->tcp_ip6h->ip6_src; 11078 dst = tcp->tcp_ip6h->ip6_dst; 11079 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 11080 ASSERT(hdrs_len != 0); 11081 if (hdrs_len > tcp->tcp_iphc_len) { 11082 /* Need to reallocate */ 11083 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 11084 if (hdrs == NULL) 11085 return (ENOMEM); 11086 if (tcp->tcp_iphc != NULL) { 11087 if (tcp->tcp_hdr_grown) { 11088 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 11089 } else { 11090 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 11091 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 11092 } 11093 tcp->tcp_iphc_len = 0; 11094 } 11095 ASSERT(tcp->tcp_iphc_len == 0); 11096 tcp->tcp_iphc = hdrs; 11097 tcp->tcp_iphc_len = hdrs_len; 11098 tcp->tcp_hdr_grown = B_TRUE; 11099 } 11100 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11101 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11102 11103 /* Set header fields not in ipp */ 11104 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11105 ip6i = (ip6i_t *)tcp->tcp_iphc; 11106 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11107 } else { 11108 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11109 } 11110 /* 11111 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11112 * 11113 * tcp->tcp_tcp_hdr_len doesn't change here. 11114 */ 11115 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11116 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11117 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11118 11119 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11120 11121 tcp->tcp_ip6h->ip6_src = src; 11122 tcp->tcp_ip6h->ip6_dst = dst; 11123 11124 /* 11125 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11126 * the default value for TCP. 11127 */ 11128 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11129 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 11130 11131 /* 11132 * If we're setting extension headers after a connection 11133 * has been established, and if we have a routing header 11134 * among the extension headers, call ip_massage_options_v6 to 11135 * manipulate the routing header/ip6_dst set the checksum 11136 * difference in the tcp header template. 11137 * (This happens in tcp_connect_ipv6 if the routing header 11138 * is set prior to the connect.) 11139 * Set the tcp_sum to zero first in case we've cleared a 11140 * routing header or don't have one at all. 11141 */ 11142 tcp->tcp_sum = 0; 11143 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11144 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11145 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11146 (uint8_t *)tcp->tcp_tcph); 11147 if (rth != NULL) { 11148 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11149 rth, tcps->tcps_netstack); 11150 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11151 (tcp->tcp_sum >> 16)); 11152 } 11153 } 11154 11155 /* Try to get everything in a single mblk */ 11156 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcps->tcps_wroff_xtra); 11157 return (0); 11158 } 11159 11160 /* 11161 * Transfer any source route option from ipha to buf/dst in reversed form. 11162 */ 11163 static int 11164 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11165 { 11166 ipoptp_t opts; 11167 uchar_t *opt; 11168 uint8_t optval; 11169 uint8_t optlen; 11170 uint32_t len = 0; 11171 11172 for (optval = ipoptp_first(&opts, ipha); 11173 optval != IPOPT_EOL; 11174 optval = ipoptp_next(&opts)) { 11175 opt = opts.ipoptp_cur; 11176 optlen = opts.ipoptp_len; 11177 switch (optval) { 11178 int off1, off2; 11179 case IPOPT_SSRR: 11180 case IPOPT_LSRR: 11181 11182 /* Reverse source route */ 11183 /* 11184 * First entry should be the next to last one in the 11185 * current source route (the last entry is our 11186 * address.) 11187 * The last entry should be the final destination. 11188 */ 11189 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11190 buf[IPOPT_OLEN] = (uint8_t)optlen; 11191 off1 = IPOPT_MINOFF_SR - 1; 11192 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11193 if (off2 < 0) { 11194 /* No entries in source route */ 11195 break; 11196 } 11197 bcopy(opt + off2, dst, IP_ADDR_LEN); 11198 /* 11199 * Note: use src since ipha has not had its src 11200 * and dst reversed (it is in the state it was 11201 * received. 11202 */ 11203 bcopy(&ipha->ipha_src, buf + off2, 11204 IP_ADDR_LEN); 11205 off2 -= IP_ADDR_LEN; 11206 11207 while (off2 > 0) { 11208 bcopy(opt + off2, buf + off1, 11209 IP_ADDR_LEN); 11210 off1 += IP_ADDR_LEN; 11211 off2 -= IP_ADDR_LEN; 11212 } 11213 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11214 buf += optlen; 11215 len += optlen; 11216 break; 11217 } 11218 } 11219 done: 11220 /* Pad the resulting options */ 11221 while (len & 0x3) { 11222 *buf++ = IPOPT_EOL; 11223 len++; 11224 } 11225 return (len); 11226 } 11227 11228 11229 /* 11230 * Extract and revert a source route from ipha (if any) 11231 * and then update the relevant fields in both tcp_t and the standard header. 11232 */ 11233 static void 11234 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11235 { 11236 char buf[TCP_MAX_HDR_LENGTH]; 11237 uint_t tcph_len; 11238 int len; 11239 11240 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11241 len = IPH_HDR_LENGTH(ipha); 11242 if (len == IP_SIMPLE_HDR_LENGTH) 11243 /* Nothing to do */ 11244 return; 11245 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11246 (len & 0x3)) 11247 return; 11248 11249 tcph_len = tcp->tcp_tcp_hdr_len; 11250 bcopy(tcp->tcp_tcph, buf, tcph_len); 11251 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11252 (tcp->tcp_ipha->ipha_dst & 0xffff); 11253 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11254 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11255 len += IP_SIMPLE_HDR_LENGTH; 11256 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11257 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11258 if ((int)tcp->tcp_sum < 0) 11259 tcp->tcp_sum--; 11260 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11261 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11262 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11263 bcopy(buf, tcp->tcp_tcph, tcph_len); 11264 tcp->tcp_ip_hdr_len = len; 11265 tcp->tcp_ipha->ipha_version_and_hdr_length = 11266 (IP_VERSION << 4) | (len >> 2); 11267 len += tcph_len; 11268 tcp->tcp_hdr_len = len; 11269 } 11270 11271 /* 11272 * Copy the standard header into its new location, 11273 * lay in the new options and then update the relevant 11274 * fields in both tcp_t and the standard header. 11275 */ 11276 static int 11277 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11278 { 11279 uint_t tcph_len; 11280 uint8_t *ip_optp; 11281 tcph_t *new_tcph; 11282 tcp_stack_t *tcps = tcp->tcp_tcps; 11283 11284 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11285 return (EINVAL); 11286 11287 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11288 return (EINVAL); 11289 11290 if (checkonly) { 11291 /* 11292 * do not really set, just pretend to - T_CHECK 11293 */ 11294 return (0); 11295 } 11296 11297 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11298 if (tcp->tcp_label_len > 0) { 11299 int padlen; 11300 uint8_t opt; 11301 11302 /* convert list termination to no-ops */ 11303 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11304 ip_optp += ip_optp[IPOPT_OLEN]; 11305 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11306 while (--padlen >= 0) 11307 *ip_optp++ = opt; 11308 } 11309 tcph_len = tcp->tcp_tcp_hdr_len; 11310 new_tcph = (tcph_t *)(ip_optp + len); 11311 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11312 tcp->tcp_tcph = new_tcph; 11313 bcopy(ptr, ip_optp, len); 11314 11315 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11316 11317 tcp->tcp_ip_hdr_len = len; 11318 tcp->tcp_ipha->ipha_version_and_hdr_length = 11319 (IP_VERSION << 4) | (len >> 2); 11320 tcp->tcp_hdr_len = len + tcph_len; 11321 if (!TCP_IS_DETACHED(tcp)) { 11322 /* Always allocate room for all options. */ 11323 (void) mi_set_sth_wroff(tcp->tcp_rq, 11324 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11325 } 11326 return (0); 11327 } 11328 11329 /* Get callback routine passed to nd_load by tcp_param_register */ 11330 /* ARGSUSED */ 11331 static int 11332 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11333 { 11334 tcpparam_t *tcppa = (tcpparam_t *)cp; 11335 11336 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11337 return (0); 11338 } 11339 11340 /* 11341 * Walk through the param array specified registering each element with the 11342 * named dispatch handler. 11343 */ 11344 static boolean_t 11345 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11346 { 11347 for (; cnt-- > 0; tcppa++) { 11348 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11349 if (!nd_load(ndp, tcppa->tcp_param_name, 11350 tcp_param_get, tcp_param_set, 11351 (caddr_t)tcppa)) { 11352 nd_free(ndp); 11353 return (B_FALSE); 11354 } 11355 } 11356 } 11357 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11358 KM_SLEEP); 11359 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11360 sizeof (tcpparam_t)); 11361 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11362 tcp_param_get, tcp_param_set_aligned, 11363 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11364 nd_free(ndp); 11365 return (B_FALSE); 11366 } 11367 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11368 KM_SLEEP); 11369 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11370 sizeof (tcpparam_t)); 11371 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11372 tcp_param_get, tcp_param_set_aligned, 11373 (caddr_t)tcps->tcps_mdt_head_param)) { 11374 nd_free(ndp); 11375 return (B_FALSE); 11376 } 11377 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11378 KM_SLEEP); 11379 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11380 sizeof (tcpparam_t)); 11381 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11382 tcp_param_get, tcp_param_set_aligned, 11383 (caddr_t)tcps->tcps_mdt_tail_param)) { 11384 nd_free(ndp); 11385 return (B_FALSE); 11386 } 11387 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11388 KM_SLEEP); 11389 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11390 sizeof (tcpparam_t)); 11391 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11392 tcp_param_get, tcp_param_set_aligned, 11393 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11394 nd_free(ndp); 11395 return (B_FALSE); 11396 } 11397 if (!nd_load(ndp, "tcp_extra_priv_ports", 11398 tcp_extra_priv_ports_get, NULL, NULL)) { 11399 nd_free(ndp); 11400 return (B_FALSE); 11401 } 11402 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11403 NULL, tcp_extra_priv_ports_add, NULL)) { 11404 nd_free(ndp); 11405 return (B_FALSE); 11406 } 11407 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11408 NULL, tcp_extra_priv_ports_del, NULL)) { 11409 nd_free(ndp); 11410 return (B_FALSE); 11411 } 11412 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11413 NULL)) { 11414 nd_free(ndp); 11415 return (B_FALSE); 11416 } 11417 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11418 NULL, NULL)) { 11419 nd_free(ndp); 11420 return (B_FALSE); 11421 } 11422 if (!nd_load(ndp, "tcp_listen_hash", 11423 tcp_listen_hash_report, NULL, NULL)) { 11424 nd_free(ndp); 11425 return (B_FALSE); 11426 } 11427 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11428 NULL, NULL)) { 11429 nd_free(ndp); 11430 return (B_FALSE); 11431 } 11432 if (!nd_load(ndp, "tcp_acceptor_hash", 11433 tcp_acceptor_hash_report, NULL, NULL)) { 11434 nd_free(ndp); 11435 return (B_FALSE); 11436 } 11437 if (!nd_load(ndp, "tcp_host_param", tcp_host_param_report, 11438 tcp_host_param_set, NULL)) { 11439 nd_free(ndp); 11440 return (B_FALSE); 11441 } 11442 if (!nd_load(ndp, "tcp_host_param_ipv6", 11443 tcp_host_param_report, tcp_host_param_set_ipv6, NULL)) { 11444 nd_free(ndp); 11445 return (B_FALSE); 11446 } 11447 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11448 tcp_1948_phrase_set, NULL)) { 11449 nd_free(ndp); 11450 return (B_FALSE); 11451 } 11452 if (!nd_load(ndp, "tcp_reserved_port_list", 11453 tcp_reserved_port_list, NULL, NULL)) { 11454 nd_free(ndp); 11455 return (B_FALSE); 11456 } 11457 /* 11458 * Dummy ndd variables - only to convey obsolescence information 11459 * through printing of their name (no get or set routines) 11460 * XXX Remove in future releases ? 11461 */ 11462 if (!nd_load(ndp, 11463 "tcp_close_wait_interval(obsoleted - " 11464 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11465 nd_free(ndp); 11466 return (B_FALSE); 11467 } 11468 return (B_TRUE); 11469 } 11470 11471 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11472 /* ARGSUSED */ 11473 static int 11474 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11475 cred_t *cr) 11476 { 11477 long new_value; 11478 tcpparam_t *tcppa = (tcpparam_t *)cp; 11479 11480 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11481 new_value < tcppa->tcp_param_min || 11482 new_value > tcppa->tcp_param_max) { 11483 return (EINVAL); 11484 } 11485 /* 11486 * Need to make sure new_value is a multiple of 4. If it is not, 11487 * round it up. For future 64 bit requirement, we actually make it 11488 * a multiple of 8. 11489 */ 11490 if (new_value & 0x7) { 11491 new_value = (new_value & ~0x7) + 0x8; 11492 } 11493 tcppa->tcp_param_val = new_value; 11494 return (0); 11495 } 11496 11497 /* Set callback routine passed to nd_load by tcp_param_register */ 11498 /* ARGSUSED */ 11499 static int 11500 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11501 { 11502 long new_value; 11503 tcpparam_t *tcppa = (tcpparam_t *)cp; 11504 11505 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11506 new_value < tcppa->tcp_param_min || 11507 new_value > tcppa->tcp_param_max) { 11508 return (EINVAL); 11509 } 11510 tcppa->tcp_param_val = new_value; 11511 return (0); 11512 } 11513 11514 /* 11515 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11516 * is filled, return as much as we can. The message passed in may be 11517 * multi-part, chained using b_cont. "start" is the starting sequence 11518 * number for this piece. 11519 */ 11520 static mblk_t * 11521 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11522 { 11523 uint32_t end; 11524 mblk_t *mp1; 11525 mblk_t *mp2; 11526 mblk_t *next_mp; 11527 uint32_t u1; 11528 tcp_stack_t *tcps = tcp->tcp_tcps; 11529 11530 /* Walk through all the new pieces. */ 11531 do { 11532 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11533 (uintptr_t)INT_MAX); 11534 end = start + (int)(mp->b_wptr - mp->b_rptr); 11535 next_mp = mp->b_cont; 11536 if (start == end) { 11537 /* Empty. Blast it. */ 11538 freeb(mp); 11539 continue; 11540 } 11541 mp->b_cont = NULL; 11542 TCP_REASS_SET_SEQ(mp, start); 11543 TCP_REASS_SET_END(mp, end); 11544 mp1 = tcp->tcp_reass_tail; 11545 if (!mp1) { 11546 tcp->tcp_reass_tail = mp; 11547 tcp->tcp_reass_head = mp; 11548 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11549 UPDATE_MIB(&tcps->tcps_mib, 11550 tcpInDataUnorderBytes, end - start); 11551 continue; 11552 } 11553 /* New stuff completely beyond tail? */ 11554 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11555 /* Link it on end. */ 11556 mp1->b_cont = mp; 11557 tcp->tcp_reass_tail = mp; 11558 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11559 UPDATE_MIB(&tcps->tcps_mib, 11560 tcpInDataUnorderBytes, end - start); 11561 continue; 11562 } 11563 mp1 = tcp->tcp_reass_head; 11564 u1 = TCP_REASS_SEQ(mp1); 11565 /* New stuff at the front? */ 11566 if (SEQ_LT(start, u1)) { 11567 /* Yes... Check for overlap. */ 11568 mp->b_cont = mp1; 11569 tcp->tcp_reass_head = mp; 11570 tcp_reass_elim_overlap(tcp, mp); 11571 continue; 11572 } 11573 /* 11574 * The new piece fits somewhere between the head and tail. 11575 * We find our slot, where mp1 precedes us and mp2 trails. 11576 */ 11577 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11578 u1 = TCP_REASS_SEQ(mp2); 11579 if (SEQ_LEQ(start, u1)) 11580 break; 11581 } 11582 /* Link ourselves in */ 11583 mp->b_cont = mp2; 11584 mp1->b_cont = mp; 11585 11586 /* Trim overlap with following mblk(s) first */ 11587 tcp_reass_elim_overlap(tcp, mp); 11588 11589 /* Trim overlap with preceding mblk */ 11590 tcp_reass_elim_overlap(tcp, mp1); 11591 11592 } while (start = end, mp = next_mp); 11593 mp1 = tcp->tcp_reass_head; 11594 /* Anything ready to go? */ 11595 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11596 return (NULL); 11597 /* Eat what we can off the queue */ 11598 for (;;) { 11599 mp = mp1->b_cont; 11600 end = TCP_REASS_END(mp1); 11601 TCP_REASS_SET_SEQ(mp1, 0); 11602 TCP_REASS_SET_END(mp1, 0); 11603 if (!mp) { 11604 tcp->tcp_reass_tail = NULL; 11605 break; 11606 } 11607 if (end != TCP_REASS_SEQ(mp)) { 11608 mp1->b_cont = NULL; 11609 break; 11610 } 11611 mp1 = mp; 11612 } 11613 mp1 = tcp->tcp_reass_head; 11614 tcp->tcp_reass_head = mp; 11615 return (mp1); 11616 } 11617 11618 /* Eliminate any overlap that mp may have over later mblks */ 11619 static void 11620 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11621 { 11622 uint32_t end; 11623 mblk_t *mp1; 11624 uint32_t u1; 11625 tcp_stack_t *tcps = tcp->tcp_tcps; 11626 11627 end = TCP_REASS_END(mp); 11628 while ((mp1 = mp->b_cont) != NULL) { 11629 u1 = TCP_REASS_SEQ(mp1); 11630 if (!SEQ_GT(end, u1)) 11631 break; 11632 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11633 mp->b_wptr -= end - u1; 11634 TCP_REASS_SET_END(mp, u1); 11635 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11636 UPDATE_MIB(&tcps->tcps_mib, 11637 tcpInDataPartDupBytes, end - u1); 11638 break; 11639 } 11640 mp->b_cont = mp1->b_cont; 11641 TCP_REASS_SET_SEQ(mp1, 0); 11642 TCP_REASS_SET_END(mp1, 0); 11643 freeb(mp1); 11644 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11645 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11646 } 11647 if (!mp1) 11648 tcp->tcp_reass_tail = mp; 11649 } 11650 11651 /* 11652 * Send up all messages queued on tcp_rcv_list. 11653 */ 11654 static uint_t 11655 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11656 { 11657 mblk_t *mp; 11658 uint_t ret = 0; 11659 uint_t thwin; 11660 #ifdef DEBUG 11661 uint_t cnt = 0; 11662 #endif 11663 tcp_stack_t *tcps = tcp->tcp_tcps; 11664 11665 /* Can't drain on an eager connection */ 11666 if (tcp->tcp_listener != NULL) 11667 return (ret); 11668 11669 /* 11670 * Handle two cases here: we are currently fused or we were 11671 * previously fused and have some urgent data to be delivered 11672 * upstream. The latter happens because we either ran out of 11673 * memory or were detached and therefore sending the SIGURG was 11674 * deferred until this point. In either case we pass control 11675 * over to tcp_fuse_rcv_drain() since it may need to complete 11676 * some work. 11677 */ 11678 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11679 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11680 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11681 &tcp->tcp_fused_sigurg_mp)) 11682 return (ret); 11683 } 11684 11685 while ((mp = tcp->tcp_rcv_list) != NULL) { 11686 tcp->tcp_rcv_list = mp->b_next; 11687 mp->b_next = NULL; 11688 #ifdef DEBUG 11689 cnt += msgdsize(mp); 11690 #endif 11691 /* Does this need SSL processing first? */ 11692 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11693 tcp_kssl_input(tcp, mp); 11694 continue; 11695 } 11696 putnext(q, mp); 11697 } 11698 ASSERT(cnt == tcp->tcp_rcv_cnt); 11699 tcp->tcp_rcv_last_head = NULL; 11700 tcp->tcp_rcv_last_tail = NULL; 11701 tcp->tcp_rcv_cnt = 0; 11702 11703 /* Learn the latest rwnd information that we sent to the other side. */ 11704 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11705 << tcp->tcp_rcv_ws; 11706 /* This is peer's calculated send window (our receive window). */ 11707 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11708 /* 11709 * Increase the receive window to max. But we need to do receiver 11710 * SWS avoidance. This means that we need to check the increase of 11711 * of receive window is at least 1 MSS. 11712 */ 11713 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11714 /* 11715 * If the window that the other side knows is less than max 11716 * deferred acks segments, send an update immediately. 11717 */ 11718 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11719 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11720 ret = TH_ACK_NEEDED; 11721 } 11722 tcp->tcp_rwnd = q->q_hiwat; 11723 } 11724 /* No need for the push timer now. */ 11725 if (tcp->tcp_push_tid != 0) { 11726 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11727 tcp->tcp_push_tid = 0; 11728 } 11729 return (ret); 11730 } 11731 11732 /* 11733 * Queue data on tcp_rcv_list which is a b_next chain. 11734 * tcp_rcv_last_head/tail is the last element of this chain. 11735 * Each element of the chain is a b_cont chain. 11736 * 11737 * M_DATA messages are added to the current element. 11738 * Other messages are added as new (b_next) elements. 11739 */ 11740 void 11741 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11742 { 11743 ASSERT(seg_len == msgdsize(mp)); 11744 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11745 11746 if (tcp->tcp_rcv_list == NULL) { 11747 ASSERT(tcp->tcp_rcv_last_head == NULL); 11748 tcp->tcp_rcv_list = mp; 11749 tcp->tcp_rcv_last_head = mp; 11750 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11751 tcp->tcp_rcv_last_tail->b_cont = mp; 11752 } else { 11753 tcp->tcp_rcv_last_head->b_next = mp; 11754 tcp->tcp_rcv_last_head = mp; 11755 } 11756 11757 while (mp->b_cont) 11758 mp = mp->b_cont; 11759 11760 tcp->tcp_rcv_last_tail = mp; 11761 tcp->tcp_rcv_cnt += seg_len; 11762 tcp->tcp_rwnd -= seg_len; 11763 } 11764 11765 /* 11766 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11767 * 11768 * This is the default entry function into TCP on the read side. TCP is 11769 * always entered via squeue i.e. using squeue's for mutual exclusion. 11770 * When classifier does a lookup to find the tcp, it also puts a reference 11771 * on the conn structure associated so the tcp is guaranteed to exist 11772 * when we come here. We still need to check the state because it might 11773 * as well has been closed. The squeue processing function i.e. squeue_enter, 11774 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 11775 * CONN_DEC_REF. 11776 * 11777 * Apart from the default entry point, IP also sends packets directly to 11778 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11779 * connections. 11780 */ 11781 void 11782 tcp_input(void *arg, mblk_t *mp, void *arg2) 11783 { 11784 conn_t *connp = (conn_t *)arg; 11785 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11786 11787 /* arg2 is the sqp */ 11788 ASSERT(arg2 != NULL); 11789 ASSERT(mp != NULL); 11790 11791 /* 11792 * Don't accept any input on a closed tcp as this TCP logically does 11793 * not exist on the system. Don't proceed further with this TCP. 11794 * For eg. this packet could trigger another close of this tcp 11795 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11796 * tcp_clean_death / tcp_closei_local must be called at most once 11797 * on a TCP. In this case we need to refeed the packet into the 11798 * classifier and figure out where the packet should go. Need to 11799 * preserve the recv_ill somehow. Until we figure that out, for 11800 * now just drop the packet if we can't classify the packet. 11801 */ 11802 if (tcp->tcp_state == TCPS_CLOSED || 11803 tcp->tcp_state == TCPS_BOUND) { 11804 conn_t *new_connp; 11805 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11806 11807 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11808 if (new_connp != NULL) { 11809 tcp_reinput(new_connp, mp, arg2); 11810 return; 11811 } 11812 /* We failed to classify. For now just drop the packet */ 11813 freemsg(mp); 11814 return; 11815 } 11816 11817 if (DB_TYPE(mp) == M_DATA) 11818 tcp_rput_data(connp, mp, arg2); 11819 else 11820 tcp_rput_common(tcp, mp); 11821 } 11822 11823 /* 11824 * The read side put procedure. 11825 * The packets passed up by ip are assume to be aligned according to 11826 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11827 */ 11828 static void 11829 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11830 { 11831 /* 11832 * tcp_rput_data() does not expect M_CTL except for the case 11833 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11834 * type. Need to make sure that any other M_CTLs don't make 11835 * it to tcp_rput_data since it is not expecting any and doesn't 11836 * check for it. 11837 */ 11838 if (DB_TYPE(mp) == M_CTL) { 11839 switch (*(uint32_t *)(mp->b_rptr)) { 11840 case TCP_IOC_ABORT_CONN: 11841 /* 11842 * Handle connection abort request. 11843 */ 11844 tcp_ioctl_abort_handler(tcp, mp); 11845 return; 11846 case IPSEC_IN: 11847 /* 11848 * Only secure icmp arrive in TCP and they 11849 * don't go through data path. 11850 */ 11851 tcp_icmp_error(tcp, mp); 11852 return; 11853 case IN_PKTINFO: 11854 /* 11855 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11856 * sockets that are receiving IPv4 traffic. tcp 11857 */ 11858 ASSERT(tcp->tcp_family == AF_INET6); 11859 ASSERT(tcp->tcp_ipv6_recvancillary & 11860 TCP_IPV6_RECVPKTINFO); 11861 tcp_rput_data(tcp->tcp_connp, mp, 11862 tcp->tcp_connp->conn_sqp); 11863 return; 11864 case MDT_IOC_INFO_UPDATE: 11865 /* 11866 * Handle Multidata information update; the 11867 * following routine will free the message. 11868 */ 11869 if (tcp->tcp_connp->conn_mdt_ok) { 11870 tcp_mdt_update(tcp, 11871 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11872 B_FALSE); 11873 } 11874 freemsg(mp); 11875 return; 11876 case LSO_IOC_INFO_UPDATE: 11877 /* 11878 * Handle LSO information update; the following 11879 * routine will free the message. 11880 */ 11881 if (tcp->tcp_connp->conn_lso_ok) { 11882 tcp_lso_update(tcp, 11883 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11884 } 11885 freemsg(mp); 11886 return; 11887 default: 11888 /* 11889 * tcp_icmp_err() will process the M_CTL packets. 11890 * Non-ICMP packets, if any, will be discarded in 11891 * tcp_icmp_err(). We will process the ICMP packet 11892 * even if we are TCP_IS_DETACHED_NONEAGER as the 11893 * incoming ICMP packet may result in changing 11894 * the tcp_mss, which we would need if we have 11895 * packets to retransmit. 11896 */ 11897 tcp_icmp_error(tcp, mp); 11898 return; 11899 } 11900 } 11901 11902 /* No point processing the message if tcp is already closed */ 11903 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11904 freemsg(mp); 11905 return; 11906 } 11907 11908 tcp_rput_other(tcp, mp); 11909 } 11910 11911 11912 /* The minimum of smoothed mean deviation in RTO calculation. */ 11913 #define TCP_SD_MIN 400 11914 11915 /* 11916 * Set RTO for this connection. The formula is from Jacobson and Karels' 11917 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11918 * are the same as those in Appendix A.2 of that paper. 11919 * 11920 * m = new measurement 11921 * sa = smoothed RTT average (8 * average estimates). 11922 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11923 */ 11924 static void 11925 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11926 { 11927 long m = TICK_TO_MSEC(rtt); 11928 clock_t sa = tcp->tcp_rtt_sa; 11929 clock_t sv = tcp->tcp_rtt_sd; 11930 clock_t rto; 11931 tcp_stack_t *tcps = tcp->tcp_tcps; 11932 11933 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11934 tcp->tcp_rtt_update++; 11935 11936 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11937 if (sa != 0) { 11938 /* 11939 * Update average estimator: 11940 * new rtt = 7/8 old rtt + 1/8 Error 11941 */ 11942 11943 /* m is now Error in estimate. */ 11944 m -= sa >> 3; 11945 if ((sa += m) <= 0) { 11946 /* 11947 * Don't allow the smoothed average to be negative. 11948 * We use 0 to denote reinitialization of the 11949 * variables. 11950 */ 11951 sa = 1; 11952 } 11953 11954 /* 11955 * Update deviation estimator: 11956 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11957 */ 11958 if (m < 0) 11959 m = -m; 11960 m -= sv >> 2; 11961 sv += m; 11962 } else { 11963 /* 11964 * This follows BSD's implementation. So the reinitialized 11965 * RTO is 3 * m. We cannot go less than 2 because if the 11966 * link is bandwidth dominated, doubling the window size 11967 * during slow start means doubling the RTT. We want to be 11968 * more conservative when we reinitialize our estimates. 3 11969 * is just a convenient number. 11970 */ 11971 sa = m << 3; 11972 sv = m << 1; 11973 } 11974 if (sv < TCP_SD_MIN) { 11975 /* 11976 * We do not know that if sa captures the delay ACK 11977 * effect as in a long train of segments, a receiver 11978 * does not delay its ACKs. So set the minimum of sv 11979 * to be TCP_SD_MIN, which is default to 400 ms, twice 11980 * of BSD DATO. That means the minimum of mean 11981 * deviation is 100 ms. 11982 * 11983 */ 11984 sv = TCP_SD_MIN; 11985 } 11986 tcp->tcp_rtt_sa = sa; 11987 tcp->tcp_rtt_sd = sv; 11988 /* 11989 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11990 * 11991 * Add tcp_rexmit_interval extra in case of extreme environment 11992 * where the algorithm fails to work. The default value of 11993 * tcp_rexmit_interval_extra should be 0. 11994 * 11995 * As we use a finer grained clock than BSD and update 11996 * RTO for every ACKs, add in another .25 of RTT to the 11997 * deviation of RTO to accomodate burstiness of 1/4 of 11998 * window size. 11999 */ 12000 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12001 12002 if (rto > tcps->tcps_rexmit_interval_max) { 12003 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12004 } else if (rto < tcps->tcps_rexmit_interval_min) { 12005 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12006 } else { 12007 tcp->tcp_rto = rto; 12008 } 12009 12010 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12011 tcp->tcp_timer_backoff = 0; 12012 } 12013 12014 /* 12015 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12016 * send queue which starts at the given seq. no. 12017 * 12018 * Parameters: 12019 * tcp_t *tcp: the tcp instance pointer. 12020 * uint32_t seq: the starting seq. no of the requested segment. 12021 * int32_t *off: after the execution, *off will be the offset to 12022 * the returned mblk which points to the requested seq no. 12023 * It is the caller's responsibility to send in a non-null off. 12024 * 12025 * Return: 12026 * A mblk_t pointer pointing to the requested segment in send queue. 12027 */ 12028 static mblk_t * 12029 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12030 { 12031 int32_t cnt; 12032 mblk_t *mp; 12033 12034 /* Defensive coding. Make sure we don't send incorrect data. */ 12035 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12036 return (NULL); 12037 12038 cnt = seq - tcp->tcp_suna; 12039 mp = tcp->tcp_xmit_head; 12040 while (cnt > 0 && mp != NULL) { 12041 cnt -= mp->b_wptr - mp->b_rptr; 12042 if (cnt < 0) { 12043 cnt += mp->b_wptr - mp->b_rptr; 12044 break; 12045 } 12046 mp = mp->b_cont; 12047 } 12048 ASSERT(mp != NULL); 12049 *off = cnt; 12050 return (mp); 12051 } 12052 12053 /* 12054 * This function handles all retransmissions if SACK is enabled for this 12055 * connection. First it calculates how many segments can be retransmitted 12056 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12057 * segments. A segment is eligible if sack_cnt for that segment is greater 12058 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12059 * all eligible segments, it checks to see if TCP can send some new segments 12060 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12061 * 12062 * Parameters: 12063 * tcp_t *tcp: the tcp structure of the connection. 12064 * uint_t *flags: in return, appropriate value will be set for 12065 * tcp_rput_data(). 12066 */ 12067 static void 12068 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12069 { 12070 notsack_blk_t *notsack_blk; 12071 int32_t usable_swnd; 12072 int32_t mss; 12073 uint32_t seg_len; 12074 mblk_t *xmit_mp; 12075 tcp_stack_t *tcps = tcp->tcp_tcps; 12076 12077 ASSERT(tcp->tcp_sack_info != NULL); 12078 ASSERT(tcp->tcp_notsack_list != NULL); 12079 ASSERT(tcp->tcp_rexmit == B_FALSE); 12080 12081 /* Defensive coding in case there is a bug... */ 12082 if (tcp->tcp_notsack_list == NULL) { 12083 return; 12084 } 12085 notsack_blk = tcp->tcp_notsack_list; 12086 mss = tcp->tcp_mss; 12087 12088 /* 12089 * Limit the num of outstanding data in the network to be 12090 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12091 */ 12092 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12093 12094 /* At least retransmit 1 MSS of data. */ 12095 if (usable_swnd <= 0) { 12096 usable_swnd = mss; 12097 } 12098 12099 /* Make sure no new RTT samples will be taken. */ 12100 tcp->tcp_csuna = tcp->tcp_snxt; 12101 12102 notsack_blk = tcp->tcp_notsack_list; 12103 while (usable_swnd > 0) { 12104 mblk_t *snxt_mp, *tmp_mp; 12105 tcp_seq begin = tcp->tcp_sack_snxt; 12106 tcp_seq end; 12107 int32_t off; 12108 12109 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12110 if (SEQ_GT(notsack_blk->end, begin) && 12111 (notsack_blk->sack_cnt >= 12112 tcps->tcps_dupack_fast_retransmit)) { 12113 end = notsack_blk->end; 12114 if (SEQ_LT(begin, notsack_blk->begin)) { 12115 begin = notsack_blk->begin; 12116 } 12117 break; 12118 } 12119 } 12120 /* 12121 * All holes are filled. Manipulate tcp_cwnd to send more 12122 * if we can. Note that after the SACK recovery, tcp_cwnd is 12123 * set to tcp_cwnd_ssthresh. 12124 */ 12125 if (notsack_blk == NULL) { 12126 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12127 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12128 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12129 ASSERT(tcp->tcp_cwnd > 0); 12130 return; 12131 } else { 12132 usable_swnd = usable_swnd / mss; 12133 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12134 MAX(usable_swnd * mss, mss); 12135 *flags |= TH_XMIT_NEEDED; 12136 return; 12137 } 12138 } 12139 12140 /* 12141 * Note that we may send more than usable_swnd allows here 12142 * because of round off, but no more than 1 MSS of data. 12143 */ 12144 seg_len = end - begin; 12145 if (seg_len > mss) 12146 seg_len = mss; 12147 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12148 ASSERT(snxt_mp != NULL); 12149 /* This should not happen. Defensive coding again... */ 12150 if (snxt_mp == NULL) { 12151 return; 12152 } 12153 12154 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12155 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12156 if (xmit_mp == NULL) 12157 return; 12158 12159 usable_swnd -= seg_len; 12160 tcp->tcp_pipe += seg_len; 12161 tcp->tcp_sack_snxt = begin + seg_len; 12162 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 12163 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12164 12165 /* 12166 * Update the send timestamp to avoid false retransmission. 12167 */ 12168 snxt_mp->b_prev = (mblk_t *)lbolt; 12169 12170 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12171 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12172 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12173 /* 12174 * Update tcp_rexmit_max to extend this SACK recovery phase. 12175 * This happens when new data sent during fast recovery is 12176 * also lost. If TCP retransmits those new data, it needs 12177 * to extend SACK recover phase to avoid starting another 12178 * fast retransmit/recovery unnecessarily. 12179 */ 12180 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12181 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12182 } 12183 } 12184 } 12185 12186 /* 12187 * This function handles policy checking at TCP level for non-hard_bound/ 12188 * detached connections. 12189 */ 12190 static boolean_t 12191 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12192 boolean_t secure, boolean_t mctl_present) 12193 { 12194 ipsec_latch_t *ipl = NULL; 12195 ipsec_action_t *act = NULL; 12196 mblk_t *data_mp; 12197 ipsec_in_t *ii; 12198 const char *reason; 12199 kstat_named_t *counter; 12200 tcp_stack_t *tcps = tcp->tcp_tcps; 12201 ipsec_stack_t *ipss; 12202 ip_stack_t *ipst; 12203 12204 ASSERT(mctl_present || !secure); 12205 12206 ASSERT((ipha == NULL && ip6h != NULL) || 12207 (ip6h == NULL && ipha != NULL)); 12208 12209 /* 12210 * We don't necessarily have an ipsec_in_act action to verify 12211 * policy because of assymetrical policy where we have only 12212 * outbound policy and no inbound policy (possible with global 12213 * policy). 12214 */ 12215 if (!secure) { 12216 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12217 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12218 return (B_TRUE); 12219 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12220 "tcp_check_policy", ipha, ip6h, secure, 12221 tcps->tcps_netstack); 12222 ipss = tcps->tcps_netstack->netstack_ipsec; 12223 12224 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12225 DROPPER(ipss, ipds_tcp_clear), 12226 &tcps->tcps_dropper); 12227 return (B_FALSE); 12228 } 12229 12230 /* 12231 * We have a secure packet. 12232 */ 12233 if (act == NULL) { 12234 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12235 "tcp_check_policy", ipha, ip6h, secure, 12236 tcps->tcps_netstack); 12237 ipss = tcps->tcps_netstack->netstack_ipsec; 12238 12239 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12240 DROPPER(ipss, ipds_tcp_secure), 12241 &tcps->tcps_dropper); 12242 return (B_FALSE); 12243 } 12244 12245 /* 12246 * XXX This whole routine is currently incorrect. ipl should 12247 * be set to the latch pointer, but is currently not set, so 12248 * we initialize it to NULL to avoid picking up random garbage. 12249 */ 12250 if (ipl == NULL) 12251 return (B_TRUE); 12252 12253 data_mp = first_mp->b_cont; 12254 12255 ii = (ipsec_in_t *)first_mp->b_rptr; 12256 12257 ipst = tcps->tcps_netstack->netstack_ip; 12258 12259 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12260 &counter, tcp->tcp_connp)) { 12261 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12262 return (B_TRUE); 12263 } 12264 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12265 "tcp inbound policy mismatch: %s, packet dropped\n", 12266 reason); 12267 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12268 12269 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12270 &tcps->tcps_dropper); 12271 return (B_FALSE); 12272 } 12273 12274 /* 12275 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12276 * retransmission after a timeout. 12277 * 12278 * To limit the number of duplicate segments, we limit the number of segment 12279 * to be sent in one time to tcp_snd_burst, the burst variable. 12280 */ 12281 static void 12282 tcp_ss_rexmit(tcp_t *tcp) 12283 { 12284 uint32_t snxt; 12285 uint32_t smax; 12286 int32_t win; 12287 int32_t mss; 12288 int32_t off; 12289 int32_t burst = tcp->tcp_snd_burst; 12290 mblk_t *snxt_mp; 12291 tcp_stack_t *tcps = tcp->tcp_tcps; 12292 12293 /* 12294 * Note that tcp_rexmit can be set even though TCP has retransmitted 12295 * all unack'ed segments. 12296 */ 12297 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12298 smax = tcp->tcp_rexmit_max; 12299 snxt = tcp->tcp_rexmit_nxt; 12300 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12301 snxt = tcp->tcp_suna; 12302 } 12303 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12304 win -= snxt - tcp->tcp_suna; 12305 mss = tcp->tcp_mss; 12306 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12307 12308 while (SEQ_LT(snxt, smax) && (win > 0) && 12309 (burst > 0) && (snxt_mp != NULL)) { 12310 mblk_t *xmit_mp; 12311 mblk_t *old_snxt_mp = snxt_mp; 12312 uint32_t cnt = mss; 12313 12314 if (win < cnt) { 12315 cnt = win; 12316 } 12317 if (SEQ_GT(snxt + cnt, smax)) { 12318 cnt = smax - snxt; 12319 } 12320 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12321 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12322 if (xmit_mp == NULL) 12323 return; 12324 12325 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12326 12327 snxt += cnt; 12328 win -= cnt; 12329 /* 12330 * Update the send timestamp to avoid false 12331 * retransmission. 12332 */ 12333 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12334 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12335 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12336 12337 tcp->tcp_rexmit_nxt = snxt; 12338 burst--; 12339 } 12340 /* 12341 * If we have transmitted all we have at the time 12342 * we started the retranmission, we can leave 12343 * the rest of the job to tcp_wput_data(). But we 12344 * need to check the send window first. If the 12345 * win is not 0, go on with tcp_wput_data(). 12346 */ 12347 if (SEQ_LT(snxt, smax) || win == 0) { 12348 return; 12349 } 12350 } 12351 /* Only call tcp_wput_data() if there is data to be sent. */ 12352 if (tcp->tcp_unsent) { 12353 tcp_wput_data(tcp, NULL, B_FALSE); 12354 } 12355 } 12356 12357 /* 12358 * Process all TCP option in SYN segment. Note that this function should 12359 * be called after tcp_adapt_ire() is called so that the necessary info 12360 * from IRE is already set in the tcp structure. 12361 * 12362 * This function sets up the correct tcp_mss value according to the 12363 * MSS option value and our header size. It also sets up the window scale 12364 * and timestamp values, and initialize SACK info blocks. But it does not 12365 * change receive window size after setting the tcp_mss value. The caller 12366 * should do the appropriate change. 12367 */ 12368 void 12369 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12370 { 12371 int options; 12372 tcp_opt_t tcpopt; 12373 uint32_t mss_max; 12374 char *tmp_tcph; 12375 tcp_stack_t *tcps = tcp->tcp_tcps; 12376 12377 tcpopt.tcp = NULL; 12378 options = tcp_parse_options(tcph, &tcpopt); 12379 12380 /* 12381 * Process MSS option. Note that MSS option value does not account 12382 * for IP or TCP options. This means that it is equal to MTU - minimum 12383 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12384 * IPv6. 12385 */ 12386 if (!(options & TCP_OPT_MSS_PRESENT)) { 12387 if (tcp->tcp_ipversion == IPV4_VERSION) 12388 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12389 else 12390 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12391 } else { 12392 if (tcp->tcp_ipversion == IPV4_VERSION) 12393 mss_max = tcps->tcps_mss_max_ipv4; 12394 else 12395 mss_max = tcps->tcps_mss_max_ipv6; 12396 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12397 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12398 else if (tcpopt.tcp_opt_mss > mss_max) 12399 tcpopt.tcp_opt_mss = mss_max; 12400 } 12401 12402 /* Process Window Scale option. */ 12403 if (options & TCP_OPT_WSCALE_PRESENT) { 12404 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12405 tcp->tcp_snd_ws_ok = B_TRUE; 12406 } else { 12407 tcp->tcp_snd_ws = B_FALSE; 12408 tcp->tcp_snd_ws_ok = B_FALSE; 12409 tcp->tcp_rcv_ws = B_FALSE; 12410 } 12411 12412 /* Process Timestamp option. */ 12413 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12414 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12415 tmp_tcph = (char *)tcp->tcp_tcph; 12416 12417 tcp->tcp_snd_ts_ok = B_TRUE; 12418 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12419 tcp->tcp_last_rcv_lbolt = lbolt64; 12420 ASSERT(OK_32PTR(tmp_tcph)); 12421 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12422 12423 /* Fill in our template header with basic timestamp option. */ 12424 tmp_tcph += tcp->tcp_tcp_hdr_len; 12425 tmp_tcph[0] = TCPOPT_NOP; 12426 tmp_tcph[1] = TCPOPT_NOP; 12427 tmp_tcph[2] = TCPOPT_TSTAMP; 12428 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12429 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12430 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12431 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12432 } else { 12433 tcp->tcp_snd_ts_ok = B_FALSE; 12434 } 12435 12436 /* 12437 * Process SACK options. If SACK is enabled for this connection, 12438 * then allocate the SACK info structure. Note the following ways 12439 * when tcp_snd_sack_ok is set to true. 12440 * 12441 * For active connection: in tcp_adapt_ire() called in 12442 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12443 * is checked. 12444 * 12445 * For passive connection: in tcp_adapt_ire() called in 12446 * tcp_accept_comm(). 12447 * 12448 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12449 * That check makes sure that if we did not send a SACK OK option, 12450 * we will not enable SACK for this connection even though the other 12451 * side sends us SACK OK option. For active connection, the SACK 12452 * info structure has already been allocated. So we need to free 12453 * it if SACK is disabled. 12454 */ 12455 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12456 (tcp->tcp_snd_sack_ok || 12457 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12458 /* This should be true only in the passive case. */ 12459 if (tcp->tcp_sack_info == NULL) { 12460 ASSERT(TCP_IS_DETACHED(tcp)); 12461 tcp->tcp_sack_info = 12462 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12463 } 12464 if (tcp->tcp_sack_info == NULL) { 12465 tcp->tcp_snd_sack_ok = B_FALSE; 12466 } else { 12467 tcp->tcp_snd_sack_ok = B_TRUE; 12468 if (tcp->tcp_snd_ts_ok) { 12469 tcp->tcp_max_sack_blk = 3; 12470 } else { 12471 tcp->tcp_max_sack_blk = 4; 12472 } 12473 } 12474 } else { 12475 /* 12476 * Resetting tcp_snd_sack_ok to B_FALSE so that 12477 * no SACK info will be used for this 12478 * connection. This assumes that SACK usage 12479 * permission is negotiated. This may need 12480 * to be changed once this is clarified. 12481 */ 12482 if (tcp->tcp_sack_info != NULL) { 12483 ASSERT(tcp->tcp_notsack_list == NULL); 12484 kmem_cache_free(tcp_sack_info_cache, 12485 tcp->tcp_sack_info); 12486 tcp->tcp_sack_info = NULL; 12487 } 12488 tcp->tcp_snd_sack_ok = B_FALSE; 12489 } 12490 12491 /* 12492 * Now we know the exact TCP/IP header length, subtract 12493 * that from tcp_mss to get our side's MSS. 12494 */ 12495 tcp->tcp_mss -= tcp->tcp_hdr_len; 12496 /* 12497 * Here we assume that the other side's header size will be equal to 12498 * our header size. We calculate the real MSS accordingly. Need to 12499 * take into additional stuffs IPsec puts in. 12500 * 12501 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12502 */ 12503 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12504 ((tcp->tcp_ipversion == IPV4_VERSION ? 12505 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12506 12507 /* 12508 * Set MSS to the smaller one of both ends of the connection. 12509 * We should not have called tcp_mss_set() before, but our 12510 * side of the MSS should have been set to a proper value 12511 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12512 * STREAM head parameters properly. 12513 * 12514 * If we have a larger-than-16-bit window but the other side 12515 * didn't want to do window scale, tcp_rwnd_set() will take 12516 * care of that. 12517 */ 12518 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12519 } 12520 12521 /* 12522 * Sends the T_CONN_IND to the listener. The caller calls this 12523 * functions via squeue to get inside the listener's perimeter 12524 * once the 3 way hand shake is done a T_CONN_IND needs to be 12525 * sent. As an optimization, the caller can call this directly 12526 * if listener's perimeter is same as eager's. 12527 */ 12528 /* ARGSUSED */ 12529 void 12530 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12531 { 12532 conn_t *lconnp = (conn_t *)arg; 12533 tcp_t *listener = lconnp->conn_tcp; 12534 tcp_t *tcp; 12535 struct T_conn_ind *conn_ind; 12536 ipaddr_t *addr_cache; 12537 boolean_t need_send_conn_ind = B_FALSE; 12538 tcp_stack_t *tcps = listener->tcp_tcps; 12539 12540 /* retrieve the eager */ 12541 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12542 ASSERT(conn_ind->OPT_offset != 0 && 12543 conn_ind->OPT_length == sizeof (intptr_t)); 12544 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12545 conn_ind->OPT_length); 12546 12547 /* 12548 * TLI/XTI applications will get confused by 12549 * sending eager as an option since it violates 12550 * the option semantics. So remove the eager as 12551 * option since TLI/XTI app doesn't need it anyway. 12552 */ 12553 if (!TCP_IS_SOCKET(listener)) { 12554 conn_ind->OPT_length = 0; 12555 conn_ind->OPT_offset = 0; 12556 } 12557 if (listener->tcp_state == TCPS_CLOSED || 12558 TCP_IS_DETACHED(listener)) { 12559 /* 12560 * If listener has closed, it would have caused a 12561 * a cleanup/blowoff to happen for the eager. We 12562 * just need to return. 12563 */ 12564 freemsg(mp); 12565 return; 12566 } 12567 12568 12569 /* 12570 * if the conn_req_q is full defer passing up the 12571 * T_CONN_IND until space is availabe after t_accept() 12572 * processing 12573 */ 12574 mutex_enter(&listener->tcp_eager_lock); 12575 12576 /* 12577 * Take the eager out, if it is in the list of droppable eagers 12578 * as we are here because the 3W handshake is over. 12579 */ 12580 MAKE_UNDROPPABLE(tcp); 12581 12582 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12583 tcp_t *tail; 12584 12585 /* 12586 * The eager already has an extra ref put in tcp_rput_data 12587 * so that it stays till accept comes back even though it 12588 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12589 */ 12590 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12591 listener->tcp_conn_req_cnt_q0--; 12592 listener->tcp_conn_req_cnt_q++; 12593 12594 /* Move from SYN_RCVD to ESTABLISHED list */ 12595 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12596 tcp->tcp_eager_prev_q0; 12597 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12598 tcp->tcp_eager_next_q0; 12599 tcp->tcp_eager_prev_q0 = NULL; 12600 tcp->tcp_eager_next_q0 = NULL; 12601 12602 /* 12603 * Insert at end of the queue because sockfs 12604 * sends down T_CONN_RES in chronological 12605 * order. Leaving the older conn indications 12606 * at front of the queue helps reducing search 12607 * time. 12608 */ 12609 tail = listener->tcp_eager_last_q; 12610 if (tail != NULL) 12611 tail->tcp_eager_next_q = tcp; 12612 else 12613 listener->tcp_eager_next_q = tcp; 12614 listener->tcp_eager_last_q = tcp; 12615 tcp->tcp_eager_next_q = NULL; 12616 /* 12617 * Delay sending up the T_conn_ind until we are 12618 * done with the eager. Once we have have sent up 12619 * the T_conn_ind, the accept can potentially complete 12620 * any time and release the refhold we have on the eager. 12621 */ 12622 need_send_conn_ind = B_TRUE; 12623 } else { 12624 /* 12625 * Defer connection on q0 and set deferred 12626 * connection bit true 12627 */ 12628 tcp->tcp_conn_def_q0 = B_TRUE; 12629 12630 /* take tcp out of q0 ... */ 12631 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12632 tcp->tcp_eager_next_q0; 12633 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12634 tcp->tcp_eager_prev_q0; 12635 12636 /* ... and place it at the end of q0 */ 12637 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12638 tcp->tcp_eager_next_q0 = listener; 12639 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12640 listener->tcp_eager_prev_q0 = tcp; 12641 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12642 } 12643 12644 /* we have timed out before */ 12645 if (tcp->tcp_syn_rcvd_timeout != 0) { 12646 tcp->tcp_syn_rcvd_timeout = 0; 12647 listener->tcp_syn_rcvd_timeout--; 12648 if (listener->tcp_syn_defense && 12649 listener->tcp_syn_rcvd_timeout <= 12650 (tcps->tcps_conn_req_max_q0 >> 5) && 12651 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12652 listener->tcp_last_rcv_lbolt)) { 12653 /* 12654 * Turn off the defense mode if we 12655 * believe the SYN attack is over. 12656 */ 12657 listener->tcp_syn_defense = B_FALSE; 12658 if (listener->tcp_ip_addr_cache) { 12659 kmem_free((void *)listener->tcp_ip_addr_cache, 12660 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12661 listener->tcp_ip_addr_cache = NULL; 12662 } 12663 } 12664 } 12665 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12666 if (addr_cache != NULL) { 12667 /* 12668 * We have finished a 3-way handshake with this 12669 * remote host. This proves the IP addr is good. 12670 * Cache it! 12671 */ 12672 addr_cache[IP_ADDR_CACHE_HASH( 12673 tcp->tcp_remote)] = tcp->tcp_remote; 12674 } 12675 mutex_exit(&listener->tcp_eager_lock); 12676 if (need_send_conn_ind) 12677 putnext(listener->tcp_rq, mp); 12678 } 12679 12680 mblk_t * 12681 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12682 uint_t *ifindexp, ip6_pkt_t *ippp) 12683 { 12684 ip_pktinfo_t *pinfo; 12685 ip6_t *ip6h; 12686 uchar_t *rptr; 12687 mblk_t *first_mp = mp; 12688 boolean_t mctl_present = B_FALSE; 12689 uint_t ifindex = 0; 12690 ip6_pkt_t ipp; 12691 uint_t ipvers; 12692 uint_t ip_hdr_len; 12693 tcp_stack_t *tcps = tcp->tcp_tcps; 12694 12695 rptr = mp->b_rptr; 12696 ASSERT(OK_32PTR(rptr)); 12697 ASSERT(tcp != NULL); 12698 ipp.ipp_fields = 0; 12699 12700 switch DB_TYPE(mp) { 12701 case M_CTL: 12702 mp = mp->b_cont; 12703 if (mp == NULL) { 12704 freemsg(first_mp); 12705 return (NULL); 12706 } 12707 if (DB_TYPE(mp) != M_DATA) { 12708 freemsg(first_mp); 12709 return (NULL); 12710 } 12711 mctl_present = B_TRUE; 12712 break; 12713 case M_DATA: 12714 break; 12715 default: 12716 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12717 freemsg(mp); 12718 return (NULL); 12719 } 12720 ipvers = IPH_HDR_VERSION(rptr); 12721 if (ipvers == IPV4_VERSION) { 12722 if (tcp == NULL) { 12723 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12724 goto done; 12725 } 12726 12727 ipp.ipp_fields |= IPPF_HOPLIMIT; 12728 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12729 12730 /* 12731 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12732 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12733 */ 12734 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12735 mctl_present) { 12736 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12737 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12738 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12739 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12740 ipp.ipp_fields |= IPPF_IFINDEX; 12741 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12742 ifindex = pinfo->ip_pkt_ifindex; 12743 } 12744 freeb(first_mp); 12745 mctl_present = B_FALSE; 12746 } 12747 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12748 } else { 12749 ip6h = (ip6_t *)rptr; 12750 12751 ASSERT(ipvers == IPV6_VERSION); 12752 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12753 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12754 ipp.ipp_hoplimit = ip6h->ip6_hops; 12755 12756 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12757 uint8_t nexthdrp; 12758 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12759 12760 /* Look for ifindex information */ 12761 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12762 ip6i_t *ip6i = (ip6i_t *)ip6h; 12763 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12764 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12765 freemsg(first_mp); 12766 return (NULL); 12767 } 12768 12769 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12770 ASSERT(ip6i->ip6i_ifindex != 0); 12771 ipp.ipp_fields |= IPPF_IFINDEX; 12772 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12773 ifindex = ip6i->ip6i_ifindex; 12774 } 12775 rptr = (uchar_t *)&ip6i[1]; 12776 mp->b_rptr = rptr; 12777 if (rptr == mp->b_wptr) { 12778 mblk_t *mp1; 12779 mp1 = mp->b_cont; 12780 freeb(mp); 12781 mp = mp1; 12782 rptr = mp->b_rptr; 12783 } 12784 if (MBLKL(mp) < IPV6_HDR_LEN + 12785 sizeof (tcph_t)) { 12786 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12787 freemsg(first_mp); 12788 return (NULL); 12789 } 12790 ip6h = (ip6_t *)rptr; 12791 } 12792 12793 /* 12794 * Find any potentially interesting extension headers 12795 * as well as the length of the IPv6 + extension 12796 * headers. 12797 */ 12798 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12799 /* Verify if this is a TCP packet */ 12800 if (nexthdrp != IPPROTO_TCP) { 12801 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12802 freemsg(first_mp); 12803 return (NULL); 12804 } 12805 } else { 12806 ip_hdr_len = IPV6_HDR_LEN; 12807 } 12808 } 12809 12810 done: 12811 if (ipversp != NULL) 12812 *ipversp = ipvers; 12813 if (ip_hdr_lenp != NULL) 12814 *ip_hdr_lenp = ip_hdr_len; 12815 if (ippp != NULL) 12816 *ippp = ipp; 12817 if (ifindexp != NULL) 12818 *ifindexp = ifindex; 12819 if (mctl_present) { 12820 freeb(first_mp); 12821 } 12822 return (mp); 12823 } 12824 12825 /* 12826 * Handle M_DATA messages from IP. Its called directly from IP via 12827 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12828 * in this path. 12829 * 12830 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12831 * v4 and v6), we are called through tcp_input() and a M_CTL can 12832 * be present for options but tcp_find_pktinfo() deals with it. We 12833 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12834 * 12835 * The first argument is always the connp/tcp to which the mp belongs. 12836 * There are no exceptions to this rule. The caller has already put 12837 * a reference on this connp/tcp and once tcp_rput_data() returns, 12838 * the squeue will do the refrele. 12839 * 12840 * The TH_SYN for the listener directly go to tcp_conn_request via 12841 * squeue. 12842 * 12843 * sqp: NULL = recursive, sqp != NULL means called from squeue 12844 */ 12845 void 12846 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12847 { 12848 int32_t bytes_acked; 12849 int32_t gap; 12850 mblk_t *mp1; 12851 uint_t flags; 12852 uint32_t new_swnd = 0; 12853 uchar_t *iphdr; 12854 uchar_t *rptr; 12855 int32_t rgap; 12856 uint32_t seg_ack; 12857 int seg_len; 12858 uint_t ip_hdr_len; 12859 uint32_t seg_seq; 12860 tcph_t *tcph; 12861 int urp; 12862 tcp_opt_t tcpopt; 12863 uint_t ipvers; 12864 ip6_pkt_t ipp; 12865 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12866 uint32_t cwnd; 12867 uint32_t add; 12868 int npkt; 12869 int mss; 12870 conn_t *connp = (conn_t *)arg; 12871 squeue_t *sqp = (squeue_t *)arg2; 12872 tcp_t *tcp = connp->conn_tcp; 12873 tcp_stack_t *tcps = tcp->tcp_tcps; 12874 12875 /* 12876 * RST from fused tcp loopback peer should trigger an unfuse. 12877 */ 12878 if (tcp->tcp_fused) { 12879 TCP_STAT(tcps, tcp_fusion_aborted); 12880 tcp_unfuse(tcp); 12881 } 12882 12883 iphdr = mp->b_rptr; 12884 rptr = mp->b_rptr; 12885 ASSERT(OK_32PTR(rptr)); 12886 12887 /* 12888 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12889 * processing here. For rest call tcp_find_pktinfo to fill up the 12890 * necessary information. 12891 */ 12892 if (IPCL_IS_TCP4(connp)) { 12893 ipvers = IPV4_VERSION; 12894 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12895 } else { 12896 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12897 NULL, &ipp); 12898 if (mp == NULL) { 12899 TCP_STAT(tcps, tcp_rput_v6_error); 12900 return; 12901 } 12902 iphdr = mp->b_rptr; 12903 rptr = mp->b_rptr; 12904 } 12905 ASSERT(DB_TYPE(mp) == M_DATA); 12906 12907 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12908 seg_seq = ABE32_TO_U32(tcph->th_seq); 12909 seg_ack = ABE32_TO_U32(tcph->th_ack); 12910 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12911 seg_len = (int)(mp->b_wptr - rptr) - 12912 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12913 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12914 do { 12915 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12916 (uintptr_t)INT_MAX); 12917 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12918 } while ((mp1 = mp1->b_cont) != NULL && 12919 mp1->b_datap->db_type == M_DATA); 12920 } 12921 12922 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12923 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12924 seg_len, tcph); 12925 return; 12926 } 12927 12928 if (sqp != NULL) { 12929 /* 12930 * This is the correct place to update tcp_last_recv_time. Note 12931 * that it is also updated for tcp structure that belongs to 12932 * global and listener queues which do not really need updating. 12933 * But that should not cause any harm. And it is updated for 12934 * all kinds of incoming segments, not only for data segments. 12935 */ 12936 tcp->tcp_last_recv_time = lbolt; 12937 } 12938 12939 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12940 12941 BUMP_LOCAL(tcp->tcp_ibsegs); 12942 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 12943 12944 if ((flags & TH_URG) && sqp != NULL) { 12945 /* 12946 * TCP can't handle urgent pointers that arrive before 12947 * the connection has been accept()ed since it can't 12948 * buffer OOB data. Discard segment if this happens. 12949 * 12950 * We can't just rely on a non-null tcp_listener to indicate 12951 * that the accept() has completed since unlinking of the 12952 * eager and completion of the accept are not atomic. 12953 * tcp_detached, when it is not set (B_FALSE) indicates 12954 * that the accept() has completed. 12955 * 12956 * Nor can it reassemble urgent pointers, so discard 12957 * if it's not the next segment expected. 12958 * 12959 * Otherwise, collapse chain into one mblk (discard if 12960 * that fails). This makes sure the headers, retransmitted 12961 * data, and new data all are in the same mblk. 12962 */ 12963 ASSERT(mp != NULL); 12964 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 12965 freemsg(mp); 12966 return; 12967 } 12968 /* Update pointers into message */ 12969 iphdr = rptr = mp->b_rptr; 12970 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12971 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12972 /* 12973 * Since we can't handle any data with this urgent 12974 * pointer that is out of sequence, we expunge 12975 * the data. This allows us to still register 12976 * the urgent mark and generate the M_PCSIG, 12977 * which we can do. 12978 */ 12979 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12980 seg_len = 0; 12981 } 12982 } 12983 12984 switch (tcp->tcp_state) { 12985 case TCPS_SYN_SENT: 12986 if (flags & TH_ACK) { 12987 /* 12988 * Note that our stack cannot send data before a 12989 * connection is established, therefore the 12990 * following check is valid. Otherwise, it has 12991 * to be changed. 12992 */ 12993 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12994 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12995 freemsg(mp); 12996 if (flags & TH_RST) 12997 return; 12998 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12999 tcp, seg_ack, 0, TH_RST); 13000 return; 13001 } 13002 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13003 } 13004 if (flags & TH_RST) { 13005 freemsg(mp); 13006 if (flags & TH_ACK) 13007 (void) tcp_clean_death(tcp, 13008 ECONNREFUSED, 13); 13009 return; 13010 } 13011 if (!(flags & TH_SYN)) { 13012 freemsg(mp); 13013 return; 13014 } 13015 13016 /* Process all TCP options. */ 13017 tcp_process_options(tcp, tcph); 13018 /* 13019 * The following changes our rwnd to be a multiple of the 13020 * MIN(peer MSS, our MSS) for performance reason. 13021 */ 13022 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 13023 tcp->tcp_mss)); 13024 13025 /* Is the other end ECN capable? */ 13026 if (tcp->tcp_ecn_ok) { 13027 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13028 tcp->tcp_ecn_ok = B_FALSE; 13029 } 13030 } 13031 /* 13032 * Clear ECN flags because it may interfere with later 13033 * processing. 13034 */ 13035 flags &= ~(TH_ECE|TH_CWR); 13036 13037 tcp->tcp_irs = seg_seq; 13038 tcp->tcp_rack = seg_seq; 13039 tcp->tcp_rnxt = seg_seq + 1; 13040 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13041 if (!TCP_IS_DETACHED(tcp)) { 13042 /* Allocate room for SACK options if needed. */ 13043 if (tcp->tcp_snd_sack_ok) { 13044 (void) mi_set_sth_wroff(tcp->tcp_rq, 13045 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13046 (tcp->tcp_loopback ? 0 : 13047 tcps->tcps_wroff_xtra)); 13048 } else { 13049 (void) mi_set_sth_wroff(tcp->tcp_rq, 13050 tcp->tcp_hdr_len + 13051 (tcp->tcp_loopback ? 0 : 13052 tcps->tcps_wroff_xtra)); 13053 } 13054 } 13055 if (flags & TH_ACK) { 13056 /* 13057 * If we can't get the confirmation upstream, pretend 13058 * we didn't even see this one. 13059 * 13060 * XXX: how can we pretend we didn't see it if we 13061 * have updated rnxt et. al. 13062 * 13063 * For loopback we defer sending up the T_CONN_CON 13064 * until after some checks below. 13065 */ 13066 mp1 = NULL; 13067 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13068 tcp->tcp_loopback ? &mp1 : NULL)) { 13069 freemsg(mp); 13070 return; 13071 } 13072 /* SYN was acked - making progress */ 13073 if (tcp->tcp_ipversion == IPV6_VERSION) 13074 tcp->tcp_ip_forward_progress = B_TRUE; 13075 13076 /* One for the SYN */ 13077 tcp->tcp_suna = tcp->tcp_iss + 1; 13078 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13079 tcp->tcp_state = TCPS_ESTABLISHED; 13080 13081 /* 13082 * If SYN was retransmitted, need to reset all 13083 * retransmission info. This is because this 13084 * segment will be treated as a dup ACK. 13085 */ 13086 if (tcp->tcp_rexmit) { 13087 tcp->tcp_rexmit = B_FALSE; 13088 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13089 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13090 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13091 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13092 tcp->tcp_ms_we_have_waited = 0; 13093 13094 /* 13095 * Set tcp_cwnd back to 1 MSS, per 13096 * recommendation from 13097 * draft-floyd-incr-init-win-01.txt, 13098 * Increasing TCP's Initial Window. 13099 */ 13100 tcp->tcp_cwnd = tcp->tcp_mss; 13101 } 13102 13103 tcp->tcp_swl1 = seg_seq; 13104 tcp->tcp_swl2 = seg_ack; 13105 13106 new_swnd = BE16_TO_U16(tcph->th_win); 13107 tcp->tcp_swnd = new_swnd; 13108 if (new_swnd > tcp->tcp_max_swnd) 13109 tcp->tcp_max_swnd = new_swnd; 13110 13111 /* 13112 * Always send the three-way handshake ack immediately 13113 * in order to make the connection complete as soon as 13114 * possible on the accepting host. 13115 */ 13116 flags |= TH_ACK_NEEDED; 13117 13118 /* 13119 * Special case for loopback. At this point we have 13120 * received SYN-ACK from the remote endpoint. In 13121 * order to ensure that both endpoints reach the 13122 * fused state prior to any data exchange, the final 13123 * ACK needs to be sent before we indicate T_CONN_CON 13124 * to the module upstream. 13125 */ 13126 if (tcp->tcp_loopback) { 13127 mblk_t *ack_mp; 13128 13129 ASSERT(!tcp->tcp_unfusable); 13130 ASSERT(mp1 != NULL); 13131 /* 13132 * For loopback, we always get a pure SYN-ACK 13133 * and only need to send back the final ACK 13134 * with no data (this is because the other 13135 * tcp is ours and we don't do T/TCP). This 13136 * final ACK triggers the passive side to 13137 * perform fusion in ESTABLISHED state. 13138 */ 13139 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13140 if (tcp->tcp_ack_tid != 0) { 13141 (void) TCP_TIMER_CANCEL(tcp, 13142 tcp->tcp_ack_tid); 13143 tcp->tcp_ack_tid = 0; 13144 } 13145 TCP_RECORD_TRACE(tcp, ack_mp, 13146 TCP_TRACE_SEND_PKT); 13147 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13148 BUMP_LOCAL(tcp->tcp_obsegs); 13149 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13150 13151 /* Send up T_CONN_CON */ 13152 putnext(tcp->tcp_rq, mp1); 13153 13154 freemsg(mp); 13155 return; 13156 } 13157 /* 13158 * Forget fusion; we need to handle more 13159 * complex cases below. Send the deferred 13160 * T_CONN_CON message upstream and proceed 13161 * as usual. Mark this tcp as not capable 13162 * of fusion. 13163 */ 13164 TCP_STAT(tcps, tcp_fusion_unfusable); 13165 tcp->tcp_unfusable = B_TRUE; 13166 putnext(tcp->tcp_rq, mp1); 13167 } 13168 13169 /* 13170 * Check to see if there is data to be sent. If 13171 * yes, set the transmit flag. Then check to see 13172 * if received data processing needs to be done. 13173 * If not, go straight to xmit_check. This short 13174 * cut is OK as we don't support T/TCP. 13175 */ 13176 if (tcp->tcp_unsent) 13177 flags |= TH_XMIT_NEEDED; 13178 13179 if (seg_len == 0 && !(flags & TH_URG)) { 13180 freemsg(mp); 13181 goto xmit_check; 13182 } 13183 13184 flags &= ~TH_SYN; 13185 seg_seq++; 13186 break; 13187 } 13188 tcp->tcp_state = TCPS_SYN_RCVD; 13189 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13190 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13191 if (mp1) { 13192 DB_CPID(mp1) = tcp->tcp_cpid; 13193 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 13194 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13195 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13196 } 13197 freemsg(mp); 13198 return; 13199 case TCPS_SYN_RCVD: 13200 if (flags & TH_ACK) { 13201 /* 13202 * In this state, a SYN|ACK packet is either bogus 13203 * because the other side must be ACKing our SYN which 13204 * indicates it has seen the ACK for their SYN and 13205 * shouldn't retransmit it or we're crossing SYNs 13206 * on active open. 13207 */ 13208 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13209 freemsg(mp); 13210 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13211 tcp, seg_ack, 0, TH_RST); 13212 return; 13213 } 13214 /* 13215 * NOTE: RFC 793 pg. 72 says this should be 13216 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13217 * but that would mean we have an ack that ignored 13218 * our SYN. 13219 */ 13220 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13221 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13222 freemsg(mp); 13223 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13224 tcp, seg_ack, 0, TH_RST); 13225 return; 13226 } 13227 } 13228 break; 13229 case TCPS_LISTEN: 13230 /* 13231 * Only a TLI listener can come through this path when a 13232 * acceptor is going back to be a listener and a packet 13233 * for the acceptor hits the classifier. For a socket 13234 * listener, this can never happen because a listener 13235 * can never accept connection on itself and hence a 13236 * socket acceptor can not go back to being a listener. 13237 */ 13238 ASSERT(!TCP_IS_SOCKET(tcp)); 13239 /*FALLTHRU*/ 13240 case TCPS_CLOSED: 13241 case TCPS_BOUND: { 13242 conn_t *new_connp; 13243 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13244 13245 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13246 if (new_connp != NULL) { 13247 tcp_reinput(new_connp, mp, connp->conn_sqp); 13248 return; 13249 } 13250 /* We failed to classify. For now just drop the packet */ 13251 freemsg(mp); 13252 return; 13253 } 13254 case TCPS_IDLE: 13255 /* 13256 * Handle the case where the tcp_clean_death() has happened 13257 * on a connection (application hasn't closed yet) but a packet 13258 * was already queued on squeue before tcp_clean_death() 13259 * was processed. Calling tcp_clean_death() twice on same 13260 * connection can result in weird behaviour. 13261 */ 13262 freemsg(mp); 13263 return; 13264 default: 13265 break; 13266 } 13267 13268 /* 13269 * Already on the correct queue/perimeter. 13270 * If this is a detached connection and not an eager 13271 * connection hanging off a listener then new data 13272 * (past the FIN) will cause a reset. 13273 * We do a special check here where it 13274 * is out of the main line, rather than check 13275 * if we are detached every time we see new 13276 * data down below. 13277 */ 13278 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13279 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13280 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13281 TCP_RECORD_TRACE(tcp, 13282 mp, TCP_TRACE_RECV_PKT); 13283 13284 freemsg(mp); 13285 /* 13286 * This could be an SSL closure alert. We're detached so just 13287 * acknowledge it this last time. 13288 */ 13289 if (tcp->tcp_kssl_ctx != NULL) { 13290 kssl_release_ctx(tcp->tcp_kssl_ctx); 13291 tcp->tcp_kssl_ctx = NULL; 13292 13293 tcp->tcp_rnxt += seg_len; 13294 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13295 flags |= TH_ACK_NEEDED; 13296 goto ack_check; 13297 } 13298 13299 tcp_xmit_ctl("new data when detached", tcp, 13300 tcp->tcp_snxt, 0, TH_RST); 13301 (void) tcp_clean_death(tcp, EPROTO, 12); 13302 return; 13303 } 13304 13305 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13306 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13307 new_swnd = BE16_TO_U16(tcph->th_win) << 13308 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13309 13310 if (tcp->tcp_snd_ts_ok) { 13311 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13312 /* 13313 * This segment is not acceptable. 13314 * Drop it and send back an ACK. 13315 */ 13316 freemsg(mp); 13317 flags |= TH_ACK_NEEDED; 13318 goto ack_check; 13319 } 13320 } else if (tcp->tcp_snd_sack_ok) { 13321 ASSERT(tcp->tcp_sack_info != NULL); 13322 tcpopt.tcp = tcp; 13323 /* 13324 * SACK info in already updated in tcp_parse_options. Ignore 13325 * all other TCP options... 13326 */ 13327 (void) tcp_parse_options(tcph, &tcpopt); 13328 } 13329 try_again:; 13330 mss = tcp->tcp_mss; 13331 gap = seg_seq - tcp->tcp_rnxt; 13332 rgap = tcp->tcp_rwnd - (gap + seg_len); 13333 /* 13334 * gap is the amount of sequence space between what we expect to see 13335 * and what we got for seg_seq. A positive value for gap means 13336 * something got lost. A negative value means we got some old stuff. 13337 */ 13338 if (gap < 0) { 13339 /* Old stuff present. Is the SYN in there? */ 13340 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13341 (seg_len != 0)) { 13342 flags &= ~TH_SYN; 13343 seg_seq++; 13344 urp--; 13345 /* Recompute the gaps after noting the SYN. */ 13346 goto try_again; 13347 } 13348 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13349 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13350 (seg_len > -gap ? -gap : seg_len)); 13351 /* Remove the old stuff from seg_len. */ 13352 seg_len += gap; 13353 /* 13354 * Anything left? 13355 * Make sure to check for unack'd FIN when rest of data 13356 * has been previously ack'd. 13357 */ 13358 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13359 /* 13360 * Resets are only valid if they lie within our offered 13361 * window. If the RST bit is set, we just ignore this 13362 * segment. 13363 */ 13364 if (flags & TH_RST) { 13365 freemsg(mp); 13366 return; 13367 } 13368 13369 /* 13370 * The arriving of dup data packets indicate that we 13371 * may have postponed an ack for too long, or the other 13372 * side's RTT estimate is out of shape. Start acking 13373 * more often. 13374 */ 13375 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13376 tcp->tcp_rack_cnt >= 1 && 13377 tcp->tcp_rack_abs_max > 2) { 13378 tcp->tcp_rack_abs_max--; 13379 } 13380 tcp->tcp_rack_cur_max = 1; 13381 13382 /* 13383 * This segment is "unacceptable". None of its 13384 * sequence space lies within our advertized window. 13385 * 13386 * Adjust seg_len to the original value for tracing. 13387 */ 13388 seg_len -= gap; 13389 if (tcp->tcp_debug) { 13390 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13391 "tcp_rput: unacceptable, gap %d, rgap %d, " 13392 "flags 0x%x, seg_seq %u, seg_ack %u, " 13393 "seg_len %d, rnxt %u, snxt %u, %s", 13394 gap, rgap, flags, seg_seq, seg_ack, 13395 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13396 tcp_display(tcp, NULL, 13397 DISP_ADDR_AND_PORT)); 13398 } 13399 13400 /* 13401 * Arrange to send an ACK in response to the 13402 * unacceptable segment per RFC 793 page 69. There 13403 * is only one small difference between ours and the 13404 * acceptability test in the RFC - we accept ACK-only 13405 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13406 * will be generated. 13407 * 13408 * Note that we have to ACK an ACK-only packet at least 13409 * for stacks that send 0-length keep-alives with 13410 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13411 * section 4.2.3.6. As long as we don't ever generate 13412 * an unacceptable packet in response to an incoming 13413 * packet that is unacceptable, it should not cause 13414 * "ACK wars". 13415 */ 13416 flags |= TH_ACK_NEEDED; 13417 13418 /* 13419 * Continue processing this segment in order to use the 13420 * ACK information it contains, but skip all other 13421 * sequence-number processing. Processing the ACK 13422 * information is necessary in order to 13423 * re-synchronize connections that may have lost 13424 * synchronization. 13425 * 13426 * We clear seg_len and flag fields related to 13427 * sequence number processing as they are not 13428 * to be trusted for an unacceptable segment. 13429 */ 13430 seg_len = 0; 13431 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13432 goto process_ack; 13433 } 13434 13435 /* Fix seg_seq, and chew the gap off the front. */ 13436 seg_seq = tcp->tcp_rnxt; 13437 urp += gap; 13438 do { 13439 mblk_t *mp2; 13440 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13441 (uintptr_t)UINT_MAX); 13442 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13443 if (gap > 0) { 13444 mp->b_rptr = mp->b_wptr - gap; 13445 break; 13446 } 13447 mp2 = mp; 13448 mp = mp->b_cont; 13449 freeb(mp2); 13450 } while (gap < 0); 13451 /* 13452 * If the urgent data has already been acknowledged, we 13453 * should ignore TH_URG below 13454 */ 13455 if (urp < 0) 13456 flags &= ~TH_URG; 13457 } 13458 /* 13459 * rgap is the amount of stuff received out of window. A negative 13460 * value is the amount out of window. 13461 */ 13462 if (rgap < 0) { 13463 mblk_t *mp2; 13464 13465 if (tcp->tcp_rwnd == 0) { 13466 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13467 } else { 13468 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13469 UPDATE_MIB(&tcps->tcps_mib, 13470 tcpInDataPastWinBytes, -rgap); 13471 } 13472 13473 /* 13474 * seg_len does not include the FIN, so if more than 13475 * just the FIN is out of window, we act like we don't 13476 * see it. (If just the FIN is out of window, rgap 13477 * will be zero and we will go ahead and acknowledge 13478 * the FIN.) 13479 */ 13480 flags &= ~TH_FIN; 13481 13482 /* Fix seg_len and make sure there is something left. */ 13483 seg_len += rgap; 13484 if (seg_len <= 0) { 13485 /* 13486 * Resets are only valid if they lie within our offered 13487 * window. If the RST bit is set, we just ignore this 13488 * segment. 13489 */ 13490 if (flags & TH_RST) { 13491 freemsg(mp); 13492 return; 13493 } 13494 13495 /* Per RFC 793, we need to send back an ACK. */ 13496 flags |= TH_ACK_NEEDED; 13497 13498 /* 13499 * Send SIGURG as soon as possible i.e. even 13500 * if the TH_URG was delivered in a window probe 13501 * packet (which will be unacceptable). 13502 * 13503 * We generate a signal if none has been generated 13504 * for this connection or if this is a new urgent 13505 * byte. Also send a zero-length "unmarked" message 13506 * to inform SIOCATMARK that this is not the mark. 13507 * 13508 * tcp_urp_last_valid is cleared when the T_exdata_ind 13509 * is sent up. This plus the check for old data 13510 * (gap >= 0) handles the wraparound of the sequence 13511 * number space without having to always track the 13512 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13513 * this max in its rcv_up variable). 13514 * 13515 * This prevents duplicate SIGURGS due to a "late" 13516 * zero-window probe when the T_EXDATA_IND has already 13517 * been sent up. 13518 */ 13519 if ((flags & TH_URG) && 13520 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13521 tcp->tcp_urp_last))) { 13522 mp1 = allocb(0, BPRI_MED); 13523 if (mp1 == NULL) { 13524 freemsg(mp); 13525 return; 13526 } 13527 if (!TCP_IS_DETACHED(tcp) && 13528 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13529 SIGURG)) { 13530 /* Try again on the rexmit. */ 13531 freemsg(mp1); 13532 freemsg(mp); 13533 return; 13534 } 13535 /* 13536 * If the next byte would be the mark 13537 * then mark with MARKNEXT else mark 13538 * with NOTMARKNEXT. 13539 */ 13540 if (gap == 0 && urp == 0) 13541 mp1->b_flag |= MSGMARKNEXT; 13542 else 13543 mp1->b_flag |= MSGNOTMARKNEXT; 13544 freemsg(tcp->tcp_urp_mark_mp); 13545 tcp->tcp_urp_mark_mp = mp1; 13546 flags |= TH_SEND_URP_MARK; 13547 tcp->tcp_urp_last_valid = B_TRUE; 13548 tcp->tcp_urp_last = urp + seg_seq; 13549 } 13550 /* 13551 * If this is a zero window probe, continue to 13552 * process the ACK part. But we need to set seg_len 13553 * to 0 to avoid data processing. Otherwise just 13554 * drop the segment and send back an ACK. 13555 */ 13556 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13557 flags &= ~(TH_SYN | TH_URG); 13558 seg_len = 0; 13559 goto process_ack; 13560 } else { 13561 freemsg(mp); 13562 goto ack_check; 13563 } 13564 } 13565 /* Pitch out of window stuff off the end. */ 13566 rgap = seg_len; 13567 mp2 = mp; 13568 do { 13569 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13570 (uintptr_t)INT_MAX); 13571 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13572 if (rgap < 0) { 13573 mp2->b_wptr += rgap; 13574 if ((mp1 = mp2->b_cont) != NULL) { 13575 mp2->b_cont = NULL; 13576 freemsg(mp1); 13577 } 13578 break; 13579 } 13580 } while ((mp2 = mp2->b_cont) != NULL); 13581 } 13582 ok:; 13583 /* 13584 * TCP should check ECN info for segments inside the window only. 13585 * Therefore the check should be done here. 13586 */ 13587 if (tcp->tcp_ecn_ok) { 13588 if (flags & TH_CWR) { 13589 tcp->tcp_ecn_echo_on = B_FALSE; 13590 } 13591 /* 13592 * Note that both ECN_CE and CWR can be set in the 13593 * same segment. In this case, we once again turn 13594 * on ECN_ECHO. 13595 */ 13596 if (tcp->tcp_ipversion == IPV4_VERSION) { 13597 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13598 13599 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13600 tcp->tcp_ecn_echo_on = B_TRUE; 13601 } 13602 } else { 13603 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13604 13605 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13606 htonl(IPH_ECN_CE << 20)) { 13607 tcp->tcp_ecn_echo_on = B_TRUE; 13608 } 13609 } 13610 } 13611 13612 /* 13613 * Check whether we can update tcp_ts_recent. This test is 13614 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13615 * Extensions for High Performance: An Update", Internet Draft. 13616 */ 13617 if (tcp->tcp_snd_ts_ok && 13618 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13619 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13620 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13621 tcp->tcp_last_rcv_lbolt = lbolt64; 13622 } 13623 13624 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13625 /* 13626 * FIN in an out of order segment. We record this in 13627 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13628 * Clear the FIN so that any check on FIN flag will fail. 13629 * Remember that FIN also counts in the sequence number 13630 * space. So we need to ack out of order FIN only segments. 13631 */ 13632 if (flags & TH_FIN) { 13633 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13634 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13635 flags &= ~TH_FIN; 13636 flags |= TH_ACK_NEEDED; 13637 } 13638 if (seg_len > 0) { 13639 /* Fill in the SACK blk list. */ 13640 if (tcp->tcp_snd_sack_ok) { 13641 ASSERT(tcp->tcp_sack_info != NULL); 13642 tcp_sack_insert(tcp->tcp_sack_list, 13643 seg_seq, seg_seq + seg_len, 13644 &(tcp->tcp_num_sack_blk)); 13645 } 13646 13647 /* 13648 * Attempt reassembly and see if we have something 13649 * ready to go. 13650 */ 13651 mp = tcp_reass(tcp, mp, seg_seq); 13652 /* Always ack out of order packets */ 13653 flags |= TH_ACK_NEEDED | TH_PUSH; 13654 if (mp) { 13655 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13656 (uintptr_t)INT_MAX); 13657 seg_len = mp->b_cont ? msgdsize(mp) : 13658 (int)(mp->b_wptr - mp->b_rptr); 13659 seg_seq = tcp->tcp_rnxt; 13660 /* 13661 * A gap is filled and the seq num and len 13662 * of the gap match that of a previously 13663 * received FIN, put the FIN flag back in. 13664 */ 13665 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13666 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13667 flags |= TH_FIN; 13668 tcp->tcp_valid_bits &= 13669 ~TCP_OFO_FIN_VALID; 13670 } 13671 } else { 13672 /* 13673 * Keep going even with NULL mp. 13674 * There may be a useful ACK or something else 13675 * we don't want to miss. 13676 * 13677 * But TCP should not perform fast retransmit 13678 * because of the ack number. TCP uses 13679 * seg_len == 0 to determine if it is a pure 13680 * ACK. And this is not a pure ACK. 13681 */ 13682 seg_len = 0; 13683 ofo_seg = B_TRUE; 13684 } 13685 } 13686 } else if (seg_len > 0) { 13687 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13688 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13689 /* 13690 * If an out of order FIN was received before, and the seq 13691 * num and len of the new segment match that of the FIN, 13692 * put the FIN flag back in. 13693 */ 13694 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13695 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13696 flags |= TH_FIN; 13697 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13698 } 13699 } 13700 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13701 if (flags & TH_RST) { 13702 freemsg(mp); 13703 switch (tcp->tcp_state) { 13704 case TCPS_SYN_RCVD: 13705 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13706 break; 13707 case TCPS_ESTABLISHED: 13708 case TCPS_FIN_WAIT_1: 13709 case TCPS_FIN_WAIT_2: 13710 case TCPS_CLOSE_WAIT: 13711 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13712 break; 13713 case TCPS_CLOSING: 13714 case TCPS_LAST_ACK: 13715 (void) tcp_clean_death(tcp, 0, 16); 13716 break; 13717 default: 13718 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13719 (void) tcp_clean_death(tcp, ENXIO, 17); 13720 break; 13721 } 13722 return; 13723 } 13724 if (flags & TH_SYN) { 13725 /* 13726 * See RFC 793, Page 71 13727 * 13728 * The seq number must be in the window as it should 13729 * be "fixed" above. If it is outside window, it should 13730 * be already rejected. Note that we allow seg_seq to be 13731 * rnxt + rwnd because we want to accept 0 window probe. 13732 */ 13733 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13734 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13735 freemsg(mp); 13736 /* 13737 * If the ACK flag is not set, just use our snxt as the 13738 * seq number of the RST segment. 13739 */ 13740 if (!(flags & TH_ACK)) { 13741 seg_ack = tcp->tcp_snxt; 13742 } 13743 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13744 TH_RST|TH_ACK); 13745 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13746 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13747 return; 13748 } 13749 /* 13750 * urp could be -1 when the urp field in the packet is 0 13751 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13752 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13753 */ 13754 if (flags & TH_URG && urp >= 0) { 13755 if (!tcp->tcp_urp_last_valid || 13756 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13757 /* 13758 * If we haven't generated the signal yet for this 13759 * urgent pointer value, do it now. Also, send up a 13760 * zero-length M_DATA indicating whether or not this is 13761 * the mark. The latter is not needed when a 13762 * T_EXDATA_IND is sent up. However, if there are 13763 * allocation failures this code relies on the sender 13764 * retransmitting and the socket code for determining 13765 * the mark should not block waiting for the peer to 13766 * transmit. Thus, for simplicity we always send up the 13767 * mark indication. 13768 */ 13769 mp1 = allocb(0, BPRI_MED); 13770 if (mp1 == NULL) { 13771 freemsg(mp); 13772 return; 13773 } 13774 if (!TCP_IS_DETACHED(tcp) && 13775 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13776 /* Try again on the rexmit. */ 13777 freemsg(mp1); 13778 freemsg(mp); 13779 return; 13780 } 13781 /* 13782 * Mark with NOTMARKNEXT for now. 13783 * The code below will change this to MARKNEXT 13784 * if we are at the mark. 13785 * 13786 * If there are allocation failures (e.g. in dupmsg 13787 * below) the next time tcp_rput_data sees the urgent 13788 * segment it will send up the MSG*MARKNEXT message. 13789 */ 13790 mp1->b_flag |= MSGNOTMARKNEXT; 13791 freemsg(tcp->tcp_urp_mark_mp); 13792 tcp->tcp_urp_mark_mp = mp1; 13793 flags |= TH_SEND_URP_MARK; 13794 #ifdef DEBUG 13795 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13796 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13797 "last %x, %s", 13798 seg_seq, urp, tcp->tcp_urp_last, 13799 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13800 #endif /* DEBUG */ 13801 tcp->tcp_urp_last_valid = B_TRUE; 13802 tcp->tcp_urp_last = urp + seg_seq; 13803 } else if (tcp->tcp_urp_mark_mp != NULL) { 13804 /* 13805 * An allocation failure prevented the previous 13806 * tcp_rput_data from sending up the allocated 13807 * MSG*MARKNEXT message - send it up this time 13808 * around. 13809 */ 13810 flags |= TH_SEND_URP_MARK; 13811 } 13812 13813 /* 13814 * If the urgent byte is in this segment, make sure that it is 13815 * all by itself. This makes it much easier to deal with the 13816 * possibility of an allocation failure on the T_exdata_ind. 13817 * Note that seg_len is the number of bytes in the segment, and 13818 * urp is the offset into the segment of the urgent byte. 13819 * urp < seg_len means that the urgent byte is in this segment. 13820 */ 13821 if (urp < seg_len) { 13822 if (seg_len != 1) { 13823 uint32_t tmp_rnxt; 13824 /* 13825 * Break it up and feed it back in. 13826 * Re-attach the IP header. 13827 */ 13828 mp->b_rptr = iphdr; 13829 if (urp > 0) { 13830 /* 13831 * There is stuff before the urgent 13832 * byte. 13833 */ 13834 mp1 = dupmsg(mp); 13835 if (!mp1) { 13836 /* 13837 * Trim from urgent byte on. 13838 * The rest will come back. 13839 */ 13840 (void) adjmsg(mp, 13841 urp - seg_len); 13842 tcp_rput_data(connp, 13843 mp, NULL); 13844 return; 13845 } 13846 (void) adjmsg(mp1, urp - seg_len); 13847 /* Feed this piece back in. */ 13848 tmp_rnxt = tcp->tcp_rnxt; 13849 tcp_rput_data(connp, mp1, NULL); 13850 /* 13851 * If the data passed back in was not 13852 * processed (ie: bad ACK) sending 13853 * the remainder back in will cause a 13854 * loop. In this case, drop the 13855 * packet and let the sender try 13856 * sending a good packet. 13857 */ 13858 if (tmp_rnxt == tcp->tcp_rnxt) { 13859 freemsg(mp); 13860 return; 13861 } 13862 } 13863 if (urp != seg_len - 1) { 13864 uint32_t tmp_rnxt; 13865 /* 13866 * There is stuff after the urgent 13867 * byte. 13868 */ 13869 mp1 = dupmsg(mp); 13870 if (!mp1) { 13871 /* 13872 * Trim everything beyond the 13873 * urgent byte. The rest will 13874 * come back. 13875 */ 13876 (void) adjmsg(mp, 13877 urp + 1 - seg_len); 13878 tcp_rput_data(connp, 13879 mp, NULL); 13880 return; 13881 } 13882 (void) adjmsg(mp1, urp + 1 - seg_len); 13883 tmp_rnxt = tcp->tcp_rnxt; 13884 tcp_rput_data(connp, mp1, NULL); 13885 /* 13886 * If the data passed back in was not 13887 * processed (ie: bad ACK) sending 13888 * the remainder back in will cause a 13889 * loop. In this case, drop the 13890 * packet and let the sender try 13891 * sending a good packet. 13892 */ 13893 if (tmp_rnxt == tcp->tcp_rnxt) { 13894 freemsg(mp); 13895 return; 13896 } 13897 } 13898 tcp_rput_data(connp, mp, NULL); 13899 return; 13900 } 13901 /* 13902 * This segment contains only the urgent byte. We 13903 * have to allocate the T_exdata_ind, if we can. 13904 */ 13905 if (!tcp->tcp_urp_mp) { 13906 struct T_exdata_ind *tei; 13907 mp1 = allocb(sizeof (struct T_exdata_ind), 13908 BPRI_MED); 13909 if (!mp1) { 13910 /* 13911 * Sigh... It'll be back. 13912 * Generate any MSG*MARK message now. 13913 */ 13914 freemsg(mp); 13915 seg_len = 0; 13916 if (flags & TH_SEND_URP_MARK) { 13917 13918 13919 ASSERT(tcp->tcp_urp_mark_mp); 13920 tcp->tcp_urp_mark_mp->b_flag &= 13921 ~MSGNOTMARKNEXT; 13922 tcp->tcp_urp_mark_mp->b_flag |= 13923 MSGMARKNEXT; 13924 } 13925 goto ack_check; 13926 } 13927 mp1->b_datap->db_type = M_PROTO; 13928 tei = (struct T_exdata_ind *)mp1->b_rptr; 13929 tei->PRIM_type = T_EXDATA_IND; 13930 tei->MORE_flag = 0; 13931 mp1->b_wptr = (uchar_t *)&tei[1]; 13932 tcp->tcp_urp_mp = mp1; 13933 #ifdef DEBUG 13934 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13935 "tcp_rput: allocated exdata_ind %s", 13936 tcp_display(tcp, NULL, 13937 DISP_PORT_ONLY)); 13938 #endif /* DEBUG */ 13939 /* 13940 * There is no need to send a separate MSG*MARK 13941 * message since the T_EXDATA_IND will be sent 13942 * now. 13943 */ 13944 flags &= ~TH_SEND_URP_MARK; 13945 freemsg(tcp->tcp_urp_mark_mp); 13946 tcp->tcp_urp_mark_mp = NULL; 13947 } 13948 /* 13949 * Now we are all set. On the next putnext upstream, 13950 * tcp_urp_mp will be non-NULL and will get prepended 13951 * to what has to be this piece containing the urgent 13952 * byte. If for any reason we abort this segment below, 13953 * if it comes back, we will have this ready, or it 13954 * will get blown off in close. 13955 */ 13956 } else if (urp == seg_len) { 13957 /* 13958 * The urgent byte is the next byte after this sequence 13959 * number. If there is data it is marked with 13960 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13961 * since it is not needed. Otherwise, if the code 13962 * above just allocated a zero-length tcp_urp_mark_mp 13963 * message, that message is tagged with MSGMARKNEXT. 13964 * Sending up these MSGMARKNEXT messages makes 13965 * SIOCATMARK work correctly even though 13966 * the T_EXDATA_IND will not be sent up until the 13967 * urgent byte arrives. 13968 */ 13969 if (seg_len != 0) { 13970 flags |= TH_MARKNEXT_NEEDED; 13971 freemsg(tcp->tcp_urp_mark_mp); 13972 tcp->tcp_urp_mark_mp = NULL; 13973 flags &= ~TH_SEND_URP_MARK; 13974 } else if (tcp->tcp_urp_mark_mp != NULL) { 13975 flags |= TH_SEND_URP_MARK; 13976 tcp->tcp_urp_mark_mp->b_flag &= 13977 ~MSGNOTMARKNEXT; 13978 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13979 } 13980 #ifdef DEBUG 13981 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13982 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13983 seg_len, flags, 13984 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13985 #endif /* DEBUG */ 13986 } else { 13987 /* Data left until we hit mark */ 13988 #ifdef DEBUG 13989 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13990 "tcp_rput: URP %d bytes left, %s", 13991 urp - seg_len, tcp_display(tcp, NULL, 13992 DISP_PORT_ONLY)); 13993 #endif /* DEBUG */ 13994 } 13995 } 13996 13997 process_ack: 13998 if (!(flags & TH_ACK)) { 13999 freemsg(mp); 14000 goto xmit_check; 14001 } 14002 } 14003 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14004 14005 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14006 tcp->tcp_ip_forward_progress = B_TRUE; 14007 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14008 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14009 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14010 /* 3-way handshake complete - pass up the T_CONN_IND */ 14011 tcp_t *listener = tcp->tcp_listener; 14012 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14013 14014 tcp->tcp_tconnind_started = B_TRUE; 14015 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14016 /* 14017 * We are here means eager is fine but it can 14018 * get a TH_RST at any point between now and till 14019 * accept completes and disappear. We need to 14020 * ensure that reference to eager is valid after 14021 * we get out of eager's perimeter. So we do 14022 * an extra refhold. 14023 */ 14024 CONN_INC_REF(connp); 14025 14026 /* 14027 * The listener also exists because of the refhold 14028 * done in tcp_conn_request. Its possible that it 14029 * might have closed. We will check that once we 14030 * get inside listeners context. 14031 */ 14032 CONN_INC_REF(listener->tcp_connp); 14033 if (listener->tcp_connp->conn_sqp == 14034 connp->conn_sqp) { 14035 tcp_send_conn_ind(listener->tcp_connp, mp, 14036 listener->tcp_connp->conn_sqp); 14037 CONN_DEC_REF(listener->tcp_connp); 14038 } else if (!tcp->tcp_loopback) { 14039 squeue_fill(listener->tcp_connp->conn_sqp, mp, 14040 tcp_send_conn_ind, 14041 listener->tcp_connp, SQTAG_TCP_CONN_IND); 14042 } else { 14043 squeue_enter(listener->tcp_connp->conn_sqp, mp, 14044 tcp_send_conn_ind, listener->tcp_connp, 14045 SQTAG_TCP_CONN_IND); 14046 } 14047 } 14048 14049 if (tcp->tcp_active_open) { 14050 /* 14051 * We are seeing the final ack in the three way 14052 * hand shake of a active open'ed connection 14053 * so we must send up a T_CONN_CON 14054 */ 14055 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14056 freemsg(mp); 14057 return; 14058 } 14059 /* 14060 * Don't fuse the loopback endpoints for 14061 * simultaneous active opens. 14062 */ 14063 if (tcp->tcp_loopback) { 14064 TCP_STAT(tcps, tcp_fusion_unfusable); 14065 tcp->tcp_unfusable = B_TRUE; 14066 } 14067 } 14068 14069 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14070 bytes_acked--; 14071 /* SYN was acked - making progress */ 14072 if (tcp->tcp_ipversion == IPV6_VERSION) 14073 tcp->tcp_ip_forward_progress = B_TRUE; 14074 14075 /* 14076 * If SYN was retransmitted, need to reset all 14077 * retransmission info as this segment will be 14078 * treated as a dup ACK. 14079 */ 14080 if (tcp->tcp_rexmit) { 14081 tcp->tcp_rexmit = B_FALSE; 14082 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14083 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14084 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14085 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14086 tcp->tcp_ms_we_have_waited = 0; 14087 tcp->tcp_cwnd = mss; 14088 } 14089 14090 /* 14091 * We set the send window to zero here. 14092 * This is needed if there is data to be 14093 * processed already on the queue. 14094 * Later (at swnd_update label), the 14095 * "new_swnd > tcp_swnd" condition is satisfied 14096 * the XMIT_NEEDED flag is set in the current 14097 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14098 * called if there is already data on queue in 14099 * this state. 14100 */ 14101 tcp->tcp_swnd = 0; 14102 14103 if (new_swnd > tcp->tcp_max_swnd) 14104 tcp->tcp_max_swnd = new_swnd; 14105 tcp->tcp_swl1 = seg_seq; 14106 tcp->tcp_swl2 = seg_ack; 14107 tcp->tcp_state = TCPS_ESTABLISHED; 14108 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14109 14110 /* Fuse when both sides are in ESTABLISHED state */ 14111 if (tcp->tcp_loopback && do_tcp_fusion) 14112 tcp_fuse(tcp, iphdr, tcph); 14113 14114 } 14115 /* This code follows 4.4BSD-Lite2 mostly. */ 14116 if (bytes_acked < 0) 14117 goto est; 14118 14119 /* 14120 * If TCP is ECN capable and the congestion experience bit is 14121 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14122 * done once per window (or more loosely, per RTT). 14123 */ 14124 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14125 tcp->tcp_cwr = B_FALSE; 14126 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14127 if (!tcp->tcp_cwr) { 14128 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14129 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14130 tcp->tcp_cwnd = npkt * mss; 14131 /* 14132 * If the cwnd is 0, use the timer to clock out 14133 * new segments. This is required by the ECN spec. 14134 */ 14135 if (npkt == 0) { 14136 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14137 /* 14138 * This makes sure that when the ACK comes 14139 * back, we will increase tcp_cwnd by 1 MSS. 14140 */ 14141 tcp->tcp_cwnd_cnt = 0; 14142 } 14143 tcp->tcp_cwr = B_TRUE; 14144 /* 14145 * This marks the end of the current window of in 14146 * flight data. That is why we don't use 14147 * tcp_suna + tcp_swnd. Only data in flight can 14148 * provide ECN info. 14149 */ 14150 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14151 tcp->tcp_ecn_cwr_sent = B_FALSE; 14152 } 14153 } 14154 14155 mp1 = tcp->tcp_xmit_head; 14156 if (bytes_acked == 0) { 14157 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14158 int dupack_cnt; 14159 14160 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14161 /* 14162 * Fast retransmit. When we have seen exactly three 14163 * identical ACKs while we have unacked data 14164 * outstanding we take it as a hint that our peer 14165 * dropped something. 14166 * 14167 * If TCP is retransmitting, don't do fast retransmit. 14168 */ 14169 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14170 ! tcp->tcp_rexmit) { 14171 /* Do Limited Transmit */ 14172 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14173 tcps->tcps_dupack_fast_retransmit) { 14174 /* 14175 * RFC 3042 14176 * 14177 * What we need to do is temporarily 14178 * increase tcp_cwnd so that new 14179 * data can be sent if it is allowed 14180 * by the receive window (tcp_rwnd). 14181 * tcp_wput_data() will take care of 14182 * the rest. 14183 * 14184 * If the connection is SACK capable, 14185 * only do limited xmit when there 14186 * is SACK info. 14187 * 14188 * Note how tcp_cwnd is incremented. 14189 * The first dup ACK will increase 14190 * it by 1 MSS. The second dup ACK 14191 * will increase it by 2 MSS. This 14192 * means that only 1 new segment will 14193 * be sent for each dup ACK. 14194 */ 14195 if (tcp->tcp_unsent > 0 && 14196 (!tcp->tcp_snd_sack_ok || 14197 (tcp->tcp_snd_sack_ok && 14198 tcp->tcp_notsack_list != NULL))) { 14199 tcp->tcp_cwnd += mss << 14200 (tcp->tcp_dupack_cnt - 1); 14201 flags |= TH_LIMIT_XMIT; 14202 } 14203 } else if (dupack_cnt == 14204 tcps->tcps_dupack_fast_retransmit) { 14205 14206 /* 14207 * If we have reduced tcp_ssthresh 14208 * because of ECN, do not reduce it again 14209 * unless it is already one window of data 14210 * away. After one window of data, tcp_cwr 14211 * should then be cleared. Note that 14212 * for non ECN capable connection, tcp_cwr 14213 * should always be false. 14214 * 14215 * Adjust cwnd since the duplicate 14216 * ack indicates that a packet was 14217 * dropped (due to congestion.) 14218 */ 14219 if (!tcp->tcp_cwr) { 14220 npkt = ((tcp->tcp_snxt - 14221 tcp->tcp_suna) >> 1) / mss; 14222 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14223 mss; 14224 tcp->tcp_cwnd = (npkt + 14225 tcp->tcp_dupack_cnt) * mss; 14226 } 14227 if (tcp->tcp_ecn_ok) { 14228 tcp->tcp_cwr = B_TRUE; 14229 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14230 tcp->tcp_ecn_cwr_sent = B_FALSE; 14231 } 14232 14233 /* 14234 * We do Hoe's algorithm. Refer to her 14235 * paper "Improving the Start-up Behavior 14236 * of a Congestion Control Scheme for TCP," 14237 * appeared in SIGCOMM'96. 14238 * 14239 * Save highest seq no we have sent so far. 14240 * Be careful about the invisible FIN byte. 14241 */ 14242 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14243 (tcp->tcp_unsent == 0)) { 14244 tcp->tcp_rexmit_max = tcp->tcp_fss; 14245 } else { 14246 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14247 } 14248 14249 /* 14250 * Do not allow bursty traffic during. 14251 * fast recovery. Refer to Fall and Floyd's 14252 * paper "Simulation-based Comparisons of 14253 * Tahoe, Reno and SACK TCP" (in CCR?) 14254 * This is a best current practise. 14255 */ 14256 tcp->tcp_snd_burst = TCP_CWND_SS; 14257 14258 /* 14259 * For SACK: 14260 * Calculate tcp_pipe, which is the 14261 * estimated number of bytes in 14262 * network. 14263 * 14264 * tcp_fack is the highest sack'ed seq num 14265 * TCP has received. 14266 * 14267 * tcp_pipe is explained in the above quoted 14268 * Fall and Floyd's paper. tcp_fack is 14269 * explained in Mathis and Mahdavi's 14270 * "Forward Acknowledgment: Refining TCP 14271 * Congestion Control" in SIGCOMM '96. 14272 */ 14273 if (tcp->tcp_snd_sack_ok) { 14274 ASSERT(tcp->tcp_sack_info != NULL); 14275 if (tcp->tcp_notsack_list != NULL) { 14276 tcp->tcp_pipe = tcp->tcp_snxt - 14277 tcp->tcp_fack; 14278 tcp->tcp_sack_snxt = seg_ack; 14279 flags |= TH_NEED_SACK_REXMIT; 14280 } else { 14281 /* 14282 * Always initialize tcp_pipe 14283 * even though we don't have 14284 * any SACK info. If later 14285 * we get SACK info and 14286 * tcp_pipe is not initialized, 14287 * funny things will happen. 14288 */ 14289 tcp->tcp_pipe = 14290 tcp->tcp_cwnd_ssthresh; 14291 } 14292 } else { 14293 flags |= TH_REXMIT_NEEDED; 14294 } /* tcp_snd_sack_ok */ 14295 14296 } else { 14297 /* 14298 * Here we perform congestion 14299 * avoidance, but NOT slow start. 14300 * This is known as the Fast 14301 * Recovery Algorithm. 14302 */ 14303 if (tcp->tcp_snd_sack_ok && 14304 tcp->tcp_notsack_list != NULL) { 14305 flags |= TH_NEED_SACK_REXMIT; 14306 tcp->tcp_pipe -= mss; 14307 if (tcp->tcp_pipe < 0) 14308 tcp->tcp_pipe = 0; 14309 } else { 14310 /* 14311 * We know that one more packet has 14312 * left the pipe thus we can update 14313 * cwnd. 14314 */ 14315 cwnd = tcp->tcp_cwnd + mss; 14316 if (cwnd > tcp->tcp_cwnd_max) 14317 cwnd = tcp->tcp_cwnd_max; 14318 tcp->tcp_cwnd = cwnd; 14319 if (tcp->tcp_unsent > 0) 14320 flags |= TH_XMIT_NEEDED; 14321 } 14322 } 14323 } 14324 } else if (tcp->tcp_zero_win_probe) { 14325 /* 14326 * If the window has opened, need to arrange 14327 * to send additional data. 14328 */ 14329 if (new_swnd != 0) { 14330 /* tcp_suna != tcp_snxt */ 14331 /* Packet contains a window update */ 14332 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14333 tcp->tcp_zero_win_probe = 0; 14334 tcp->tcp_timer_backoff = 0; 14335 tcp->tcp_ms_we_have_waited = 0; 14336 14337 /* 14338 * Transmit starting with tcp_suna since 14339 * the one byte probe is not ack'ed. 14340 * If TCP has sent more than one identical 14341 * probe, tcp_rexmit will be set. That means 14342 * tcp_ss_rexmit() will send out the one 14343 * byte along with new data. Otherwise, 14344 * fake the retransmission. 14345 */ 14346 flags |= TH_XMIT_NEEDED; 14347 if (!tcp->tcp_rexmit) { 14348 tcp->tcp_rexmit = B_TRUE; 14349 tcp->tcp_dupack_cnt = 0; 14350 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14351 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14352 } 14353 } 14354 } 14355 goto swnd_update; 14356 } 14357 14358 /* 14359 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14360 * If the ACK value acks something that we have not yet sent, it might 14361 * be an old duplicate segment. Send an ACK to re-synchronize the 14362 * other side. 14363 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14364 * state is handled above, so we can always just drop the segment and 14365 * send an ACK here. 14366 * 14367 * Should we send ACKs in response to ACK only segments? 14368 */ 14369 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14370 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14371 /* drop the received segment */ 14372 freemsg(mp); 14373 14374 /* 14375 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14376 * greater than 0, check if the number of such 14377 * bogus ACks is greater than that count. If yes, 14378 * don't send back any ACK. This prevents TCP from 14379 * getting into an ACK storm if somehow an attacker 14380 * successfully spoofs an acceptable segment to our 14381 * peer. 14382 */ 14383 if (tcp_drop_ack_unsent_cnt > 0 && 14384 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14385 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14386 return; 14387 } 14388 mp = tcp_ack_mp(tcp); 14389 if (mp != NULL) { 14390 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 14391 BUMP_LOCAL(tcp->tcp_obsegs); 14392 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14393 tcp_send_data(tcp, tcp->tcp_wq, mp); 14394 } 14395 return; 14396 } 14397 14398 /* 14399 * TCP gets a new ACK, update the notsack'ed list to delete those 14400 * blocks that are covered by this ACK. 14401 */ 14402 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14403 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14404 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14405 } 14406 14407 /* 14408 * If we got an ACK after fast retransmit, check to see 14409 * if it is a partial ACK. If it is not and the congestion 14410 * window was inflated to account for the other side's 14411 * cached packets, retract it. If it is, do Hoe's algorithm. 14412 */ 14413 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14414 ASSERT(tcp->tcp_rexmit == B_FALSE); 14415 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14416 tcp->tcp_dupack_cnt = 0; 14417 /* 14418 * Restore the orig tcp_cwnd_ssthresh after 14419 * fast retransmit phase. 14420 */ 14421 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14422 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14423 } 14424 tcp->tcp_rexmit_max = seg_ack; 14425 tcp->tcp_cwnd_cnt = 0; 14426 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14427 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14428 14429 /* 14430 * Remove all notsack info to avoid confusion with 14431 * the next fast retrasnmit/recovery phase. 14432 */ 14433 if (tcp->tcp_snd_sack_ok && 14434 tcp->tcp_notsack_list != NULL) { 14435 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14436 } 14437 } else { 14438 if (tcp->tcp_snd_sack_ok && 14439 tcp->tcp_notsack_list != NULL) { 14440 flags |= TH_NEED_SACK_REXMIT; 14441 tcp->tcp_pipe -= mss; 14442 if (tcp->tcp_pipe < 0) 14443 tcp->tcp_pipe = 0; 14444 } else { 14445 /* 14446 * Hoe's algorithm: 14447 * 14448 * Retransmit the unack'ed segment and 14449 * restart fast recovery. Note that we 14450 * need to scale back tcp_cwnd to the 14451 * original value when we started fast 14452 * recovery. This is to prevent overly 14453 * aggressive behaviour in sending new 14454 * segments. 14455 */ 14456 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14457 tcps->tcps_dupack_fast_retransmit * mss; 14458 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14459 flags |= TH_REXMIT_NEEDED; 14460 } 14461 } 14462 } else { 14463 tcp->tcp_dupack_cnt = 0; 14464 if (tcp->tcp_rexmit) { 14465 /* 14466 * TCP is retranmitting. If the ACK ack's all 14467 * outstanding data, update tcp_rexmit_max and 14468 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14469 * to the correct value. 14470 * 14471 * Note that SEQ_LEQ() is used. This is to avoid 14472 * unnecessary fast retransmit caused by dup ACKs 14473 * received when TCP does slow start retransmission 14474 * after a time out. During this phase, TCP may 14475 * send out segments which are already received. 14476 * This causes dup ACKs to be sent back. 14477 */ 14478 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14479 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14480 tcp->tcp_rexmit_nxt = seg_ack; 14481 } 14482 if (seg_ack != tcp->tcp_rexmit_max) { 14483 flags |= TH_XMIT_NEEDED; 14484 } 14485 } else { 14486 tcp->tcp_rexmit = B_FALSE; 14487 tcp->tcp_xmit_zc_clean = B_FALSE; 14488 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14489 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14490 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14491 } 14492 tcp->tcp_ms_we_have_waited = 0; 14493 } 14494 } 14495 14496 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14497 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14498 tcp->tcp_suna = seg_ack; 14499 if (tcp->tcp_zero_win_probe != 0) { 14500 tcp->tcp_zero_win_probe = 0; 14501 tcp->tcp_timer_backoff = 0; 14502 } 14503 14504 /* 14505 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14506 * Note that it cannot be the SYN being ack'ed. The code flow 14507 * will not reach here. 14508 */ 14509 if (mp1 == NULL) { 14510 goto fin_acked; 14511 } 14512 14513 /* 14514 * Update the congestion window. 14515 * 14516 * If TCP is not ECN capable or TCP is ECN capable but the 14517 * congestion experience bit is not set, increase the tcp_cwnd as 14518 * usual. 14519 */ 14520 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14521 cwnd = tcp->tcp_cwnd; 14522 add = mss; 14523 14524 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14525 /* 14526 * This is to prevent an increase of less than 1 MSS of 14527 * tcp_cwnd. With partial increase, tcp_wput_data() 14528 * may send out tinygrams in order to preserve mblk 14529 * boundaries. 14530 * 14531 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14532 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14533 * increased by 1 MSS for every RTTs. 14534 */ 14535 if (tcp->tcp_cwnd_cnt <= 0) { 14536 tcp->tcp_cwnd_cnt = cwnd + add; 14537 } else { 14538 tcp->tcp_cwnd_cnt -= add; 14539 add = 0; 14540 } 14541 } 14542 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14543 } 14544 14545 /* See if the latest urgent data has been acknowledged */ 14546 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14547 SEQ_GT(seg_ack, tcp->tcp_urg)) 14548 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14549 14550 /* Can we update the RTT estimates? */ 14551 if (tcp->tcp_snd_ts_ok) { 14552 /* Ignore zero timestamp echo-reply. */ 14553 if (tcpopt.tcp_opt_ts_ecr != 0) { 14554 tcp_set_rto(tcp, (int32_t)lbolt - 14555 (int32_t)tcpopt.tcp_opt_ts_ecr); 14556 } 14557 14558 /* If needed, restart the timer. */ 14559 if (tcp->tcp_set_timer == 1) { 14560 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14561 tcp->tcp_set_timer = 0; 14562 } 14563 /* 14564 * Update tcp_csuna in case the other side stops sending 14565 * us timestamps. 14566 */ 14567 tcp->tcp_csuna = tcp->tcp_snxt; 14568 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14569 /* 14570 * An ACK sequence we haven't seen before, so get the RTT 14571 * and update the RTO. But first check if the timestamp is 14572 * valid to use. 14573 */ 14574 if ((mp1->b_next != NULL) && 14575 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14576 tcp_set_rto(tcp, (int32_t)lbolt - 14577 (int32_t)(intptr_t)mp1->b_prev); 14578 else 14579 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14580 14581 /* Remeber the last sequence to be ACKed */ 14582 tcp->tcp_csuna = seg_ack; 14583 if (tcp->tcp_set_timer == 1) { 14584 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14585 tcp->tcp_set_timer = 0; 14586 } 14587 } else { 14588 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14589 } 14590 14591 /* Eat acknowledged bytes off the xmit queue. */ 14592 for (;;) { 14593 mblk_t *mp2; 14594 uchar_t *wptr; 14595 14596 wptr = mp1->b_wptr; 14597 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14598 bytes_acked -= (int)(wptr - mp1->b_rptr); 14599 if (bytes_acked < 0) { 14600 mp1->b_rptr = wptr + bytes_acked; 14601 /* 14602 * Set a new timestamp if all the bytes timed by the 14603 * old timestamp have been ack'ed. 14604 */ 14605 if (SEQ_GT(seg_ack, 14606 (uint32_t)(uintptr_t)(mp1->b_next))) { 14607 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14608 mp1->b_next = NULL; 14609 } 14610 break; 14611 } 14612 mp1->b_next = NULL; 14613 mp1->b_prev = NULL; 14614 mp2 = mp1; 14615 mp1 = mp1->b_cont; 14616 14617 /* 14618 * This notification is required for some zero-copy 14619 * clients to maintain a copy semantic. After the data 14620 * is ack'ed, client is safe to modify or reuse the buffer. 14621 */ 14622 if (tcp->tcp_snd_zcopy_aware && 14623 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14624 tcp_zcopy_notify(tcp); 14625 freeb(mp2); 14626 if (bytes_acked == 0) { 14627 if (mp1 == NULL) { 14628 /* Everything is ack'ed, clear the tail. */ 14629 tcp->tcp_xmit_tail = NULL; 14630 /* 14631 * Cancel the timer unless we are still 14632 * waiting for an ACK for the FIN packet. 14633 */ 14634 if (tcp->tcp_timer_tid != 0 && 14635 tcp->tcp_snxt == tcp->tcp_suna) { 14636 (void) TCP_TIMER_CANCEL(tcp, 14637 tcp->tcp_timer_tid); 14638 tcp->tcp_timer_tid = 0; 14639 } 14640 goto pre_swnd_update; 14641 } 14642 if (mp2 != tcp->tcp_xmit_tail) 14643 break; 14644 tcp->tcp_xmit_tail = mp1; 14645 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14646 (uintptr_t)INT_MAX); 14647 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14648 mp1->b_rptr); 14649 break; 14650 } 14651 if (mp1 == NULL) { 14652 /* 14653 * More was acked but there is nothing more 14654 * outstanding. This means that the FIN was 14655 * just acked or that we're talking to a clown. 14656 */ 14657 fin_acked: 14658 ASSERT(tcp->tcp_fin_sent); 14659 tcp->tcp_xmit_tail = NULL; 14660 if (tcp->tcp_fin_sent) { 14661 /* FIN was acked - making progress */ 14662 if (tcp->tcp_ipversion == IPV6_VERSION && 14663 !tcp->tcp_fin_acked) 14664 tcp->tcp_ip_forward_progress = B_TRUE; 14665 tcp->tcp_fin_acked = B_TRUE; 14666 if (tcp->tcp_linger_tid != 0 && 14667 TCP_TIMER_CANCEL(tcp, 14668 tcp->tcp_linger_tid) >= 0) { 14669 tcp_stop_lingering(tcp); 14670 freemsg(mp); 14671 mp = NULL; 14672 } 14673 } else { 14674 /* 14675 * We should never get here because 14676 * we have already checked that the 14677 * number of bytes ack'ed should be 14678 * smaller than or equal to what we 14679 * have sent so far (it is the 14680 * acceptability check of the ACK). 14681 * We can only get here if the send 14682 * queue is corrupted. 14683 * 14684 * Terminate the connection and 14685 * panic the system. It is better 14686 * for us to panic instead of 14687 * continuing to avoid other disaster. 14688 */ 14689 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14690 tcp->tcp_rnxt, TH_RST|TH_ACK); 14691 panic("Memory corruption " 14692 "detected for connection %s.", 14693 tcp_display(tcp, NULL, 14694 DISP_ADDR_AND_PORT)); 14695 /*NOTREACHED*/ 14696 } 14697 goto pre_swnd_update; 14698 } 14699 ASSERT(mp2 != tcp->tcp_xmit_tail); 14700 } 14701 if (tcp->tcp_unsent) { 14702 flags |= TH_XMIT_NEEDED; 14703 } 14704 pre_swnd_update: 14705 tcp->tcp_xmit_head = mp1; 14706 swnd_update: 14707 /* 14708 * The following check is different from most other implementations. 14709 * For bi-directional transfer, when segments are dropped, the 14710 * "normal" check will not accept a window update in those 14711 * retransmitted segemnts. Failing to do that, TCP may send out 14712 * segments which are outside receiver's window. As TCP accepts 14713 * the ack in those retransmitted segments, if the window update in 14714 * the same segment is not accepted, TCP will incorrectly calculates 14715 * that it can send more segments. This can create a deadlock 14716 * with the receiver if its window becomes zero. 14717 */ 14718 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14719 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14720 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14721 /* 14722 * The criteria for update is: 14723 * 14724 * 1. the segment acknowledges some data. Or 14725 * 2. the segment is new, i.e. it has a higher seq num. Or 14726 * 3. the segment is not old and the advertised window is 14727 * larger than the previous advertised window. 14728 */ 14729 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14730 flags |= TH_XMIT_NEEDED; 14731 tcp->tcp_swnd = new_swnd; 14732 if (new_swnd > tcp->tcp_max_swnd) 14733 tcp->tcp_max_swnd = new_swnd; 14734 tcp->tcp_swl1 = seg_seq; 14735 tcp->tcp_swl2 = seg_ack; 14736 } 14737 est: 14738 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14739 14740 switch (tcp->tcp_state) { 14741 case TCPS_FIN_WAIT_1: 14742 if (tcp->tcp_fin_acked) { 14743 tcp->tcp_state = TCPS_FIN_WAIT_2; 14744 /* 14745 * We implement the non-standard BSD/SunOS 14746 * FIN_WAIT_2 flushing algorithm. 14747 * If there is no user attached to this 14748 * TCP endpoint, then this TCP struct 14749 * could hang around forever in FIN_WAIT_2 14750 * state if the peer forgets to send us 14751 * a FIN. To prevent this, we wait only 14752 * 2*MSL (a convenient time value) for 14753 * the FIN to arrive. If it doesn't show up, 14754 * we flush the TCP endpoint. This algorithm, 14755 * though a violation of RFC-793, has worked 14756 * for over 10 years in BSD systems. 14757 * Note: SunOS 4.x waits 675 seconds before 14758 * flushing the FIN_WAIT_2 connection. 14759 */ 14760 TCP_TIMER_RESTART(tcp, 14761 tcps->tcps_fin_wait_2_flush_interval); 14762 } 14763 break; 14764 case TCPS_FIN_WAIT_2: 14765 break; /* Shutdown hook? */ 14766 case TCPS_LAST_ACK: 14767 freemsg(mp); 14768 if (tcp->tcp_fin_acked) { 14769 (void) tcp_clean_death(tcp, 0, 19); 14770 return; 14771 } 14772 goto xmit_check; 14773 case TCPS_CLOSING: 14774 if (tcp->tcp_fin_acked) { 14775 tcp->tcp_state = TCPS_TIME_WAIT; 14776 /* 14777 * Unconditionally clear the exclusive binding 14778 * bit so this TIME-WAIT connection won't 14779 * interfere with new ones. 14780 */ 14781 tcp->tcp_exclbind = 0; 14782 if (!TCP_IS_DETACHED(tcp)) { 14783 TCP_TIMER_RESTART(tcp, 14784 tcps->tcps_time_wait_interval); 14785 } else { 14786 tcp_time_wait_append(tcp); 14787 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14788 } 14789 } 14790 /*FALLTHRU*/ 14791 case TCPS_CLOSE_WAIT: 14792 freemsg(mp); 14793 goto xmit_check; 14794 default: 14795 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14796 break; 14797 } 14798 } 14799 if (flags & TH_FIN) { 14800 /* Make sure we ack the fin */ 14801 flags |= TH_ACK_NEEDED; 14802 if (!tcp->tcp_fin_rcvd) { 14803 tcp->tcp_fin_rcvd = B_TRUE; 14804 tcp->tcp_rnxt++; 14805 tcph = tcp->tcp_tcph; 14806 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14807 14808 /* 14809 * Generate the ordrel_ind at the end unless we 14810 * are an eager guy. 14811 * In the eager case tcp_rsrv will do this when run 14812 * after tcp_accept is done. 14813 */ 14814 if (tcp->tcp_listener == NULL && 14815 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14816 flags |= TH_ORDREL_NEEDED; 14817 switch (tcp->tcp_state) { 14818 case TCPS_SYN_RCVD: 14819 case TCPS_ESTABLISHED: 14820 tcp->tcp_state = TCPS_CLOSE_WAIT; 14821 /* Keepalive? */ 14822 break; 14823 case TCPS_FIN_WAIT_1: 14824 if (!tcp->tcp_fin_acked) { 14825 tcp->tcp_state = TCPS_CLOSING; 14826 break; 14827 } 14828 /* FALLTHRU */ 14829 case TCPS_FIN_WAIT_2: 14830 tcp->tcp_state = TCPS_TIME_WAIT; 14831 /* 14832 * Unconditionally clear the exclusive binding 14833 * bit so this TIME-WAIT connection won't 14834 * interfere with new ones. 14835 */ 14836 tcp->tcp_exclbind = 0; 14837 if (!TCP_IS_DETACHED(tcp)) { 14838 TCP_TIMER_RESTART(tcp, 14839 tcps->tcps_time_wait_interval); 14840 } else { 14841 tcp_time_wait_append(tcp); 14842 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14843 } 14844 if (seg_len) { 14845 /* 14846 * implies data piggybacked on FIN. 14847 * break to handle data. 14848 */ 14849 break; 14850 } 14851 freemsg(mp); 14852 goto ack_check; 14853 } 14854 } 14855 } 14856 if (mp == NULL) 14857 goto xmit_check; 14858 if (seg_len == 0) { 14859 freemsg(mp); 14860 goto xmit_check; 14861 } 14862 if (mp->b_rptr == mp->b_wptr) { 14863 /* 14864 * The header has been consumed, so we remove the 14865 * zero-length mblk here. 14866 */ 14867 mp1 = mp; 14868 mp = mp->b_cont; 14869 freeb(mp1); 14870 } 14871 tcph = tcp->tcp_tcph; 14872 tcp->tcp_rack_cnt++; 14873 { 14874 uint32_t cur_max; 14875 14876 cur_max = tcp->tcp_rack_cur_max; 14877 if (tcp->tcp_rack_cnt >= cur_max) { 14878 /* 14879 * We have more unacked data than we should - send 14880 * an ACK now. 14881 */ 14882 flags |= TH_ACK_NEEDED; 14883 cur_max++; 14884 if (cur_max > tcp->tcp_rack_abs_max) 14885 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14886 else 14887 tcp->tcp_rack_cur_max = cur_max; 14888 } else if (TCP_IS_DETACHED(tcp)) { 14889 /* We don't have an ACK timer for detached TCP. */ 14890 flags |= TH_ACK_NEEDED; 14891 } else if (seg_len < mss) { 14892 /* 14893 * If we get a segment that is less than an mss, and we 14894 * already have unacknowledged data, and the amount 14895 * unacknowledged is not a multiple of mss, then we 14896 * better generate an ACK now. Otherwise, this may be 14897 * the tail piece of a transaction, and we would rather 14898 * wait for the response. 14899 */ 14900 uint32_t udif; 14901 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14902 (uintptr_t)INT_MAX); 14903 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14904 if (udif && (udif % mss)) 14905 flags |= TH_ACK_NEEDED; 14906 else 14907 flags |= TH_ACK_TIMER_NEEDED; 14908 } else { 14909 /* Start delayed ack timer */ 14910 flags |= TH_ACK_TIMER_NEEDED; 14911 } 14912 } 14913 tcp->tcp_rnxt += seg_len; 14914 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14915 14916 /* Update SACK list */ 14917 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14918 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14919 &(tcp->tcp_num_sack_blk)); 14920 } 14921 14922 if (tcp->tcp_urp_mp) { 14923 tcp->tcp_urp_mp->b_cont = mp; 14924 mp = tcp->tcp_urp_mp; 14925 tcp->tcp_urp_mp = NULL; 14926 /* Ready for a new signal. */ 14927 tcp->tcp_urp_last_valid = B_FALSE; 14928 #ifdef DEBUG 14929 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14930 "tcp_rput: sending exdata_ind %s", 14931 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14932 #endif /* DEBUG */ 14933 } 14934 14935 /* 14936 * Check for ancillary data changes compared to last segment. 14937 */ 14938 if (tcp->tcp_ipv6_recvancillary != 0) { 14939 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14940 if (mp == NULL) 14941 return; 14942 } 14943 14944 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14945 /* 14946 * Side queue inbound data until the accept happens. 14947 * tcp_accept/tcp_rput drains this when the accept happens. 14948 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14949 * T_EXDATA_IND) it is queued on b_next. 14950 * XXX Make urgent data use this. Requires: 14951 * Removing tcp_listener check for TH_URG 14952 * Making M_PCPROTO and MARK messages skip the eager case 14953 */ 14954 14955 if (tcp->tcp_kssl_pending) { 14956 tcp_kssl_input(tcp, mp); 14957 } else { 14958 tcp_rcv_enqueue(tcp, mp, seg_len); 14959 } 14960 } else { 14961 if (mp->b_datap->db_type != M_DATA || 14962 (flags & TH_MARKNEXT_NEEDED)) { 14963 if (tcp->tcp_rcv_list != NULL) { 14964 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14965 } 14966 ASSERT(tcp->tcp_rcv_list == NULL || 14967 tcp->tcp_fused_sigurg); 14968 if (flags & TH_MARKNEXT_NEEDED) { 14969 #ifdef DEBUG 14970 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14971 "tcp_rput: sending MSGMARKNEXT %s", 14972 tcp_display(tcp, NULL, 14973 DISP_PORT_ONLY)); 14974 #endif /* DEBUG */ 14975 mp->b_flag |= MSGMARKNEXT; 14976 flags &= ~TH_MARKNEXT_NEEDED; 14977 } 14978 14979 /* Does this need SSL processing first? */ 14980 if ((tcp->tcp_kssl_ctx != NULL) && 14981 (DB_TYPE(mp) == M_DATA)) { 14982 tcp_kssl_input(tcp, mp); 14983 } else { 14984 putnext(tcp->tcp_rq, mp); 14985 if (!canputnext(tcp->tcp_rq)) 14986 tcp->tcp_rwnd -= seg_len; 14987 } 14988 } else if ((flags & (TH_PUSH|TH_FIN)) || 14989 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 14990 if (tcp->tcp_rcv_list != NULL) { 14991 /* 14992 * Enqueue the new segment first and then 14993 * call tcp_rcv_drain() to send all data 14994 * up. The other way to do this is to 14995 * send all queued data up and then call 14996 * putnext() to send the new segment up. 14997 * This way can remove the else part later 14998 * on. 14999 * 15000 * We don't this to avoid one more call to 15001 * canputnext() as tcp_rcv_drain() needs to 15002 * call canputnext(). 15003 */ 15004 tcp_rcv_enqueue(tcp, mp, seg_len); 15005 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15006 } else { 15007 /* Does this need SSL processing first? */ 15008 if ((tcp->tcp_kssl_ctx != NULL) && 15009 (DB_TYPE(mp) == M_DATA)) { 15010 tcp_kssl_input(tcp, mp); 15011 } else { 15012 putnext(tcp->tcp_rq, mp); 15013 if (!canputnext(tcp->tcp_rq)) 15014 tcp->tcp_rwnd -= seg_len; 15015 } 15016 } 15017 } else { 15018 /* 15019 * Enqueue all packets when processing an mblk 15020 * from the co queue and also enqueue normal packets. 15021 * For packets which belong to SSL stream do SSL 15022 * processing first. 15023 */ 15024 if ((tcp->tcp_kssl_ctx != NULL) && 15025 (DB_TYPE(mp) == M_DATA)) { 15026 tcp_kssl_input(tcp, mp); 15027 } else { 15028 tcp_rcv_enqueue(tcp, mp, seg_len); 15029 } 15030 } 15031 /* 15032 * Make sure the timer is running if we have data waiting 15033 * for a push bit. This provides resiliency against 15034 * implementations that do not correctly generate push bits. 15035 */ 15036 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 15037 /* 15038 * The connection may be closed at this point, so don't 15039 * do anything for a detached tcp. 15040 */ 15041 if (!TCP_IS_DETACHED(tcp)) 15042 tcp->tcp_push_tid = TCP_TIMER(tcp, 15043 tcp_push_timer, 15044 MSEC_TO_TICK( 15045 tcps->tcps_push_timer_interval)); 15046 } 15047 } 15048 xmit_check: 15049 /* Is there anything left to do? */ 15050 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15051 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15052 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15053 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15054 goto done; 15055 15056 /* Any transmit work to do and a non-zero window? */ 15057 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15058 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15059 if (flags & TH_REXMIT_NEEDED) { 15060 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15061 15062 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15063 if (snd_size > mss) 15064 snd_size = mss; 15065 if (snd_size > tcp->tcp_swnd) 15066 snd_size = tcp->tcp_swnd; 15067 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15068 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15069 B_TRUE); 15070 15071 if (mp1 != NULL) { 15072 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15073 tcp->tcp_csuna = tcp->tcp_snxt; 15074 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15075 UPDATE_MIB(&tcps->tcps_mib, 15076 tcpRetransBytes, snd_size); 15077 TCP_RECORD_TRACE(tcp, mp1, 15078 TCP_TRACE_SEND_PKT); 15079 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15080 } 15081 } 15082 if (flags & TH_NEED_SACK_REXMIT) { 15083 tcp_sack_rxmit(tcp, &flags); 15084 } 15085 /* 15086 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15087 * out new segment. Note that tcp_rexmit should not be 15088 * set, otherwise TH_LIMIT_XMIT should not be set. 15089 */ 15090 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15091 if (!tcp->tcp_rexmit) { 15092 tcp_wput_data(tcp, NULL, B_FALSE); 15093 } else { 15094 tcp_ss_rexmit(tcp); 15095 } 15096 } 15097 /* 15098 * Adjust tcp_cwnd back to normal value after sending 15099 * new data segments. 15100 */ 15101 if (flags & TH_LIMIT_XMIT) { 15102 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15103 /* 15104 * This will restart the timer. Restarting the 15105 * timer is used to avoid a timeout before the 15106 * limited transmitted segment's ACK gets back. 15107 */ 15108 if (tcp->tcp_xmit_head != NULL) 15109 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15110 } 15111 15112 /* Anything more to do? */ 15113 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15114 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15115 goto done; 15116 } 15117 ack_check: 15118 if (flags & TH_SEND_URP_MARK) { 15119 ASSERT(tcp->tcp_urp_mark_mp); 15120 /* 15121 * Send up any queued data and then send the mark message 15122 */ 15123 if (tcp->tcp_rcv_list != NULL) { 15124 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15125 } 15126 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15127 15128 mp1 = tcp->tcp_urp_mark_mp; 15129 tcp->tcp_urp_mark_mp = NULL; 15130 #ifdef DEBUG 15131 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15132 "tcp_rput: sending zero-length %s %s", 15133 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15134 "MSGNOTMARKNEXT"), 15135 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15136 #endif /* DEBUG */ 15137 putnext(tcp->tcp_rq, mp1); 15138 flags &= ~TH_SEND_URP_MARK; 15139 } 15140 if (flags & TH_ACK_NEEDED) { 15141 /* 15142 * Time to send an ack for some reason. 15143 */ 15144 mp1 = tcp_ack_mp(tcp); 15145 15146 if (mp1 != NULL) { 15147 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 15148 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15149 BUMP_LOCAL(tcp->tcp_obsegs); 15150 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15151 } 15152 if (tcp->tcp_ack_tid != 0) { 15153 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15154 tcp->tcp_ack_tid = 0; 15155 } 15156 } 15157 if (flags & TH_ACK_TIMER_NEEDED) { 15158 /* 15159 * Arrange for deferred ACK or push wait timeout. 15160 * Start timer if it is not already running. 15161 */ 15162 if (tcp->tcp_ack_tid == 0) { 15163 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15164 MSEC_TO_TICK(tcp->tcp_localnet ? 15165 (clock_t)tcps->tcps_local_dack_interval : 15166 (clock_t)tcps->tcps_deferred_ack_interval)); 15167 } 15168 } 15169 if (flags & TH_ORDREL_NEEDED) { 15170 /* 15171 * Send up the ordrel_ind unless we are an eager guy. 15172 * In the eager case tcp_rsrv will do this when run 15173 * after tcp_accept is done. 15174 */ 15175 ASSERT(tcp->tcp_listener == NULL); 15176 if (tcp->tcp_rcv_list != NULL) { 15177 /* 15178 * Push any mblk(s) enqueued from co processing. 15179 */ 15180 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15181 } 15182 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15183 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 15184 tcp->tcp_ordrel_done = B_TRUE; 15185 putnext(tcp->tcp_rq, mp1); 15186 if (tcp->tcp_deferred_clean_death) { 15187 /* 15188 * tcp_clean_death was deferred 15189 * for T_ORDREL_IND - do it now 15190 */ 15191 (void) tcp_clean_death(tcp, 15192 tcp->tcp_client_errno, 20); 15193 tcp->tcp_deferred_clean_death = B_FALSE; 15194 } 15195 } else { 15196 /* 15197 * Run the orderly release in the 15198 * service routine. 15199 */ 15200 qenable(tcp->tcp_rq); 15201 /* 15202 * Caveat(XXX): The machine may be so 15203 * overloaded that tcp_rsrv() is not scheduled 15204 * until after the endpoint has transitioned 15205 * to TCPS_TIME_WAIT 15206 * and tcp_time_wait_interval expires. Then 15207 * tcp_timer() will blow away state in tcp_t 15208 * and T_ORDREL_IND will never be delivered 15209 * upstream. Unlikely but potentially 15210 * a problem. 15211 */ 15212 } 15213 } 15214 done: 15215 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15216 } 15217 15218 /* 15219 * This function does PAWS protection check. Returns B_TRUE if the 15220 * segment passes the PAWS test, else returns B_FALSE. 15221 */ 15222 boolean_t 15223 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15224 { 15225 uint8_t flags; 15226 int options; 15227 uint8_t *up; 15228 15229 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15230 /* 15231 * If timestamp option is aligned nicely, get values inline, 15232 * otherwise call general routine to parse. Only do that 15233 * if timestamp is the only option. 15234 */ 15235 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15236 TCPOPT_REAL_TS_LEN && 15237 OK_32PTR((up = ((uint8_t *)tcph) + 15238 TCP_MIN_HEADER_LENGTH)) && 15239 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15240 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15241 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15242 15243 options = TCP_OPT_TSTAMP_PRESENT; 15244 } else { 15245 if (tcp->tcp_snd_sack_ok) { 15246 tcpoptp->tcp = tcp; 15247 } else { 15248 tcpoptp->tcp = NULL; 15249 } 15250 options = tcp_parse_options(tcph, tcpoptp); 15251 } 15252 15253 if (options & TCP_OPT_TSTAMP_PRESENT) { 15254 /* 15255 * Do PAWS per RFC 1323 section 4.2. Accept RST 15256 * regardless of the timestamp, page 18 RFC 1323.bis. 15257 */ 15258 if ((flags & TH_RST) == 0 && 15259 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15260 tcp->tcp_ts_recent)) { 15261 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15262 PAWS_TIMEOUT)) { 15263 /* This segment is not acceptable. */ 15264 return (B_FALSE); 15265 } else { 15266 /* 15267 * Connection has been idle for 15268 * too long. Reset the timestamp 15269 * and assume the segment is valid. 15270 */ 15271 tcp->tcp_ts_recent = 15272 tcpoptp->tcp_opt_ts_val; 15273 } 15274 } 15275 } else { 15276 /* 15277 * If we don't get a timestamp on every packet, we 15278 * figure we can't really trust 'em, so we stop sending 15279 * and parsing them. 15280 */ 15281 tcp->tcp_snd_ts_ok = B_FALSE; 15282 15283 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15284 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15285 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15286 /* 15287 * Adjust the tcp_mss accordingly. We also need to 15288 * adjust tcp_cwnd here in accordance with the new mss. 15289 * But we avoid doing a slow start here so as to not 15290 * to lose on the transfer rate built up so far. 15291 */ 15292 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15293 if (tcp->tcp_snd_sack_ok) { 15294 ASSERT(tcp->tcp_sack_info != NULL); 15295 tcp->tcp_max_sack_blk = 4; 15296 } 15297 } 15298 return (B_TRUE); 15299 } 15300 15301 /* 15302 * Attach ancillary data to a received TCP segments for the 15303 * ancillary pieces requested by the application that are 15304 * different than they were in the previous data segment. 15305 * 15306 * Save the "current" values once memory allocation is ok so that 15307 * when memory allocation fails we can just wait for the next data segment. 15308 */ 15309 static mblk_t * 15310 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15311 { 15312 struct T_optdata_ind *todi; 15313 int optlen; 15314 uchar_t *optptr; 15315 struct T_opthdr *toh; 15316 uint_t addflag; /* Which pieces to add */ 15317 mblk_t *mp1; 15318 15319 optlen = 0; 15320 addflag = 0; 15321 /* If app asked for pktinfo and the index has changed ... */ 15322 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15323 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15324 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15325 optlen += sizeof (struct T_opthdr) + 15326 sizeof (struct in6_pktinfo); 15327 addflag |= TCP_IPV6_RECVPKTINFO; 15328 } 15329 /* If app asked for hoplimit and it has changed ... */ 15330 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15331 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15332 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15333 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15334 addflag |= TCP_IPV6_RECVHOPLIMIT; 15335 } 15336 /* If app asked for tclass and it has changed ... */ 15337 if ((ipp->ipp_fields & IPPF_TCLASS) && 15338 ipp->ipp_tclass != tcp->tcp_recvtclass && 15339 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15340 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15341 addflag |= TCP_IPV6_RECVTCLASS; 15342 } 15343 /* 15344 * If app asked for hopbyhop headers and it has changed ... 15345 * For security labels, note that (1) security labels can't change on 15346 * a connected socket at all, (2) we're connected to at most one peer, 15347 * (3) if anything changes, then it must be some other extra option. 15348 */ 15349 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15350 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15351 (ipp->ipp_fields & IPPF_HOPOPTS), 15352 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15353 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15354 tcp->tcp_label_len; 15355 addflag |= TCP_IPV6_RECVHOPOPTS; 15356 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15357 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15358 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15359 return (mp); 15360 } 15361 /* If app asked for dst headers before routing headers ... */ 15362 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15363 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15364 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15365 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15366 optlen += sizeof (struct T_opthdr) + 15367 ipp->ipp_rtdstoptslen; 15368 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15369 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15370 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15371 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15372 return (mp); 15373 } 15374 /* If app asked for routing headers and it has changed ... */ 15375 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15376 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15377 (ipp->ipp_fields & IPPF_RTHDR), 15378 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15379 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15380 addflag |= TCP_IPV6_RECVRTHDR; 15381 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15382 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15383 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15384 return (mp); 15385 } 15386 /* If app asked for dest headers and it has changed ... */ 15387 if ((tcp->tcp_ipv6_recvancillary & 15388 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15389 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15390 (ipp->ipp_fields & IPPF_DSTOPTS), 15391 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15392 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15393 addflag |= TCP_IPV6_RECVDSTOPTS; 15394 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15395 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15396 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15397 return (mp); 15398 } 15399 15400 if (optlen == 0) { 15401 /* Nothing to add */ 15402 return (mp); 15403 } 15404 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15405 if (mp1 == NULL) { 15406 /* 15407 * Defer sending ancillary data until the next TCP segment 15408 * arrives. 15409 */ 15410 return (mp); 15411 } 15412 mp1->b_cont = mp; 15413 mp = mp1; 15414 mp->b_wptr += sizeof (*todi) + optlen; 15415 mp->b_datap->db_type = M_PROTO; 15416 todi = (struct T_optdata_ind *)mp->b_rptr; 15417 todi->PRIM_type = T_OPTDATA_IND; 15418 todi->DATA_flag = 1; /* MORE data */ 15419 todi->OPT_length = optlen; 15420 todi->OPT_offset = sizeof (*todi); 15421 optptr = (uchar_t *)&todi[1]; 15422 /* 15423 * If app asked for pktinfo and the index has changed ... 15424 * Note that the local address never changes for the connection. 15425 */ 15426 if (addflag & TCP_IPV6_RECVPKTINFO) { 15427 struct in6_pktinfo *pkti; 15428 15429 toh = (struct T_opthdr *)optptr; 15430 toh->level = IPPROTO_IPV6; 15431 toh->name = IPV6_PKTINFO; 15432 toh->len = sizeof (*toh) + sizeof (*pkti); 15433 toh->status = 0; 15434 optptr += sizeof (*toh); 15435 pkti = (struct in6_pktinfo *)optptr; 15436 if (tcp->tcp_ipversion == IPV6_VERSION) 15437 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15438 else 15439 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15440 &pkti->ipi6_addr); 15441 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15442 optptr += sizeof (*pkti); 15443 ASSERT(OK_32PTR(optptr)); 15444 /* Save as "last" value */ 15445 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15446 } 15447 /* If app asked for hoplimit and it has changed ... */ 15448 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15449 toh = (struct T_opthdr *)optptr; 15450 toh->level = IPPROTO_IPV6; 15451 toh->name = IPV6_HOPLIMIT; 15452 toh->len = sizeof (*toh) + sizeof (uint_t); 15453 toh->status = 0; 15454 optptr += sizeof (*toh); 15455 *(uint_t *)optptr = ipp->ipp_hoplimit; 15456 optptr += sizeof (uint_t); 15457 ASSERT(OK_32PTR(optptr)); 15458 /* Save as "last" value */ 15459 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15460 } 15461 /* If app asked for tclass and it has changed ... */ 15462 if (addflag & TCP_IPV6_RECVTCLASS) { 15463 toh = (struct T_opthdr *)optptr; 15464 toh->level = IPPROTO_IPV6; 15465 toh->name = IPV6_TCLASS; 15466 toh->len = sizeof (*toh) + sizeof (uint_t); 15467 toh->status = 0; 15468 optptr += sizeof (*toh); 15469 *(uint_t *)optptr = ipp->ipp_tclass; 15470 optptr += sizeof (uint_t); 15471 ASSERT(OK_32PTR(optptr)); 15472 /* Save as "last" value */ 15473 tcp->tcp_recvtclass = ipp->ipp_tclass; 15474 } 15475 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15476 toh = (struct T_opthdr *)optptr; 15477 toh->level = IPPROTO_IPV6; 15478 toh->name = IPV6_HOPOPTS; 15479 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15480 tcp->tcp_label_len; 15481 toh->status = 0; 15482 optptr += sizeof (*toh); 15483 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15484 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15485 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15486 ASSERT(OK_32PTR(optptr)); 15487 /* Save as last value */ 15488 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15489 (ipp->ipp_fields & IPPF_HOPOPTS), 15490 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15491 } 15492 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15493 toh = (struct T_opthdr *)optptr; 15494 toh->level = IPPROTO_IPV6; 15495 toh->name = IPV6_RTHDRDSTOPTS; 15496 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15497 toh->status = 0; 15498 optptr += sizeof (*toh); 15499 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15500 optptr += ipp->ipp_rtdstoptslen; 15501 ASSERT(OK_32PTR(optptr)); 15502 /* Save as last value */ 15503 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15504 &tcp->tcp_rtdstoptslen, 15505 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15506 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15507 } 15508 if (addflag & TCP_IPV6_RECVRTHDR) { 15509 toh = (struct T_opthdr *)optptr; 15510 toh->level = IPPROTO_IPV6; 15511 toh->name = IPV6_RTHDR; 15512 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15513 toh->status = 0; 15514 optptr += sizeof (*toh); 15515 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15516 optptr += ipp->ipp_rthdrlen; 15517 ASSERT(OK_32PTR(optptr)); 15518 /* Save as last value */ 15519 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15520 (ipp->ipp_fields & IPPF_RTHDR), 15521 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15522 } 15523 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15524 toh = (struct T_opthdr *)optptr; 15525 toh->level = IPPROTO_IPV6; 15526 toh->name = IPV6_DSTOPTS; 15527 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15528 toh->status = 0; 15529 optptr += sizeof (*toh); 15530 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15531 optptr += ipp->ipp_dstoptslen; 15532 ASSERT(OK_32PTR(optptr)); 15533 /* Save as last value */ 15534 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15535 (ipp->ipp_fields & IPPF_DSTOPTS), 15536 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15537 } 15538 ASSERT(optptr == mp->b_wptr); 15539 return (mp); 15540 } 15541 15542 15543 /* 15544 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15545 * or a "bad" IRE detected by tcp_adapt_ire. 15546 * We can't tell if the failure was due to the laddr or the faddr 15547 * thus we clear out all addresses and ports. 15548 */ 15549 static void 15550 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15551 { 15552 queue_t *q = tcp->tcp_rq; 15553 tcph_t *tcph; 15554 struct T_error_ack *tea; 15555 conn_t *connp = tcp->tcp_connp; 15556 15557 15558 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15559 15560 if (mp->b_cont) { 15561 freemsg(mp->b_cont); 15562 mp->b_cont = NULL; 15563 } 15564 tea = (struct T_error_ack *)mp->b_rptr; 15565 switch (tea->PRIM_type) { 15566 case T_BIND_ACK: 15567 /* 15568 * Need to unbind with classifier since we were just told that 15569 * our bind succeeded. 15570 */ 15571 tcp->tcp_hard_bound = B_FALSE; 15572 tcp->tcp_hard_binding = B_FALSE; 15573 15574 ipcl_hash_remove(connp); 15575 /* Reuse the mblk if possible */ 15576 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15577 sizeof (*tea)); 15578 mp->b_rptr = mp->b_datap->db_base; 15579 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15580 tea = (struct T_error_ack *)mp->b_rptr; 15581 tea->PRIM_type = T_ERROR_ACK; 15582 tea->TLI_error = TSYSERR; 15583 tea->UNIX_error = error; 15584 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15585 tea->ERROR_prim = T_CONN_REQ; 15586 } else { 15587 tea->ERROR_prim = O_T_BIND_REQ; 15588 } 15589 break; 15590 15591 case T_ERROR_ACK: 15592 if (tcp->tcp_state >= TCPS_SYN_SENT) 15593 tea->ERROR_prim = T_CONN_REQ; 15594 break; 15595 default: 15596 panic("tcp_bind_failed: unexpected TPI type"); 15597 /*NOTREACHED*/ 15598 } 15599 15600 tcp->tcp_state = TCPS_IDLE; 15601 if (tcp->tcp_ipversion == IPV4_VERSION) 15602 tcp->tcp_ipha->ipha_src = 0; 15603 else 15604 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15605 /* 15606 * Copy of the src addr. in tcp_t is needed since 15607 * the lookup funcs. can only look at tcp_t 15608 */ 15609 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15610 15611 tcph = tcp->tcp_tcph; 15612 tcph->th_lport[0] = 0; 15613 tcph->th_lport[1] = 0; 15614 tcp_bind_hash_remove(tcp); 15615 bzero(&connp->u_port, sizeof (connp->u_port)); 15616 /* blow away saved option results if any */ 15617 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15618 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15619 15620 conn_delete_ire(tcp->tcp_connp, NULL); 15621 putnext(q, mp); 15622 } 15623 15624 /* 15625 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15626 * messages. 15627 */ 15628 void 15629 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15630 { 15631 mblk_t *mp1; 15632 uchar_t *rptr = mp->b_rptr; 15633 queue_t *q = tcp->tcp_rq; 15634 struct T_error_ack *tea; 15635 uint32_t mss; 15636 mblk_t *syn_mp; 15637 mblk_t *mdti; 15638 mblk_t *lsoi; 15639 int retval; 15640 mblk_t *ire_mp; 15641 tcp_stack_t *tcps = tcp->tcp_tcps; 15642 15643 switch (mp->b_datap->db_type) { 15644 case M_PROTO: 15645 case M_PCPROTO: 15646 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15647 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15648 break; 15649 tea = (struct T_error_ack *)rptr; 15650 switch (tea->PRIM_type) { 15651 case T_BIND_ACK: 15652 /* 15653 * Adapt Multidata information, if any. The 15654 * following tcp_mdt_update routine will free 15655 * the message. 15656 */ 15657 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15658 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15659 b_rptr)->mdt_capab, B_TRUE); 15660 freemsg(mdti); 15661 } 15662 15663 /* 15664 * Check to update LSO information with tcp, and 15665 * tcp_lso_update routine will free the message. 15666 */ 15667 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 15668 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 15669 b_rptr)->lso_capab); 15670 freemsg(lsoi); 15671 } 15672 15673 /* Get the IRE, if we had requested for it */ 15674 ire_mp = tcp_ire_mp(mp); 15675 15676 if (tcp->tcp_hard_binding) { 15677 tcp->tcp_hard_binding = B_FALSE; 15678 tcp->tcp_hard_bound = B_TRUE; 15679 CL_INET_CONNECT(tcp); 15680 } else { 15681 if (ire_mp != NULL) 15682 freeb(ire_mp); 15683 goto after_syn_sent; 15684 } 15685 15686 retval = tcp_adapt_ire(tcp, ire_mp); 15687 if (ire_mp != NULL) 15688 freeb(ire_mp); 15689 if (retval == 0) { 15690 tcp_bind_failed(tcp, mp, 15691 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15692 ENETUNREACH : EADDRNOTAVAIL)); 15693 return; 15694 } 15695 /* 15696 * Don't let an endpoint connect to itself. 15697 * Also checked in tcp_connect() but that 15698 * check can't handle the case when the 15699 * local IP address is INADDR_ANY. 15700 */ 15701 if (tcp->tcp_ipversion == IPV4_VERSION) { 15702 if ((tcp->tcp_ipha->ipha_dst == 15703 tcp->tcp_ipha->ipha_src) && 15704 (BE16_EQL(tcp->tcp_tcph->th_lport, 15705 tcp->tcp_tcph->th_fport))) { 15706 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15707 return; 15708 } 15709 } else { 15710 if (IN6_ARE_ADDR_EQUAL( 15711 &tcp->tcp_ip6h->ip6_dst, 15712 &tcp->tcp_ip6h->ip6_src) && 15713 (BE16_EQL(tcp->tcp_tcph->th_lport, 15714 tcp->tcp_tcph->th_fport))) { 15715 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15716 return; 15717 } 15718 } 15719 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15720 /* 15721 * This should not be possible! Just for 15722 * defensive coding... 15723 */ 15724 if (tcp->tcp_state != TCPS_SYN_SENT) 15725 goto after_syn_sent; 15726 15727 if (is_system_labeled() && 15728 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 15729 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 15730 return; 15731 } 15732 15733 ASSERT(q == tcp->tcp_rq); 15734 /* 15735 * tcp_adapt_ire() does not adjust 15736 * for TCP/IP header length. 15737 */ 15738 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15739 15740 /* 15741 * Just make sure our rwnd is at 15742 * least tcp_recv_hiwat_mss * MSS 15743 * large, and round up to the nearest 15744 * MSS. 15745 * 15746 * We do the round up here because 15747 * we need to get the interface 15748 * MTU first before we can do the 15749 * round up. 15750 */ 15751 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15752 tcps->tcps_recv_hiwat_minmss * mss); 15753 q->q_hiwat = tcp->tcp_rwnd; 15754 tcp_set_ws_value(tcp); 15755 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15756 tcp->tcp_tcph->th_win); 15757 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 15758 tcp->tcp_snd_ws_ok = B_TRUE; 15759 15760 /* 15761 * Set tcp_snd_ts_ok to true 15762 * so that tcp_xmit_mp will 15763 * include the timestamp 15764 * option in the SYN segment. 15765 */ 15766 if (tcps->tcps_tstamp_always || 15767 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 15768 tcp->tcp_snd_ts_ok = B_TRUE; 15769 } 15770 15771 /* 15772 * tcp_snd_sack_ok can be set in 15773 * tcp_adapt_ire() if the sack metric 15774 * is set. So check it here also. 15775 */ 15776 if (tcps->tcps_sack_permitted == 2 || 15777 tcp->tcp_snd_sack_ok) { 15778 if (tcp->tcp_sack_info == NULL) { 15779 tcp->tcp_sack_info = 15780 kmem_cache_alloc( 15781 tcp_sack_info_cache, 15782 KM_SLEEP); 15783 } 15784 tcp->tcp_snd_sack_ok = B_TRUE; 15785 } 15786 15787 /* 15788 * Should we use ECN? Note that the current 15789 * default value (SunOS 5.9) of tcp_ecn_permitted 15790 * is 1. The reason for doing this is that there 15791 * are equipments out there that will drop ECN 15792 * enabled IP packets. Setting it to 1 avoids 15793 * compatibility problems. 15794 */ 15795 if (tcps->tcps_ecn_permitted == 2) 15796 tcp->tcp_ecn_ok = B_TRUE; 15797 15798 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15799 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15800 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15801 if (syn_mp) { 15802 cred_t *cr; 15803 pid_t pid; 15804 15805 /* 15806 * Obtain the credential from the 15807 * thread calling connect(); the credential 15808 * lives on in the second mblk which 15809 * originated from T_CONN_REQ and is echoed 15810 * with the T_BIND_ACK from ip. If none 15811 * can be found, default to the creator 15812 * of the socket. 15813 */ 15814 if (mp->b_cont == NULL || 15815 (cr = DB_CRED(mp->b_cont)) == NULL) { 15816 cr = tcp->tcp_cred; 15817 pid = tcp->tcp_cpid; 15818 } else { 15819 pid = DB_CPID(mp->b_cont); 15820 } 15821 15822 TCP_RECORD_TRACE(tcp, syn_mp, 15823 TCP_TRACE_SEND_PKT); 15824 mblk_setcred(syn_mp, cr); 15825 DB_CPID(syn_mp) = pid; 15826 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15827 } 15828 after_syn_sent: 15829 /* 15830 * A trailer mblk indicates a waiting client upstream. 15831 * We complete here the processing begun in 15832 * either tcp_bind() or tcp_connect() by passing 15833 * upstream the reply message they supplied. 15834 */ 15835 mp1 = mp; 15836 mp = mp->b_cont; 15837 freeb(mp1); 15838 if (mp) 15839 break; 15840 return; 15841 case T_ERROR_ACK: 15842 if (tcp->tcp_debug) { 15843 (void) strlog(TCP_MOD_ID, 0, 1, 15844 SL_TRACE|SL_ERROR, 15845 "tcp_rput_other: case T_ERROR_ACK, " 15846 "ERROR_prim == %d", 15847 tea->ERROR_prim); 15848 } 15849 switch (tea->ERROR_prim) { 15850 case O_T_BIND_REQ: 15851 case T_BIND_REQ: 15852 tcp_bind_failed(tcp, mp, 15853 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15854 ENETUNREACH : EADDRNOTAVAIL)); 15855 return; 15856 case T_UNBIND_REQ: 15857 tcp->tcp_hard_binding = B_FALSE; 15858 tcp->tcp_hard_bound = B_FALSE; 15859 if (mp->b_cont) { 15860 freemsg(mp->b_cont); 15861 mp->b_cont = NULL; 15862 } 15863 if (tcp->tcp_unbind_pending) 15864 tcp->tcp_unbind_pending = 0; 15865 else { 15866 /* From tcp_ip_unbind() - free */ 15867 freemsg(mp); 15868 return; 15869 } 15870 break; 15871 case T_SVR4_OPTMGMT_REQ: 15872 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15873 /* T_OPTMGMT_REQ generated by TCP */ 15874 printf("T_SVR4_OPTMGMT_REQ failed " 15875 "%d/%d - dropped (cnt %d)\n", 15876 tea->TLI_error, tea->UNIX_error, 15877 tcp->tcp_drop_opt_ack_cnt); 15878 freemsg(mp); 15879 tcp->tcp_drop_opt_ack_cnt--; 15880 return; 15881 } 15882 break; 15883 } 15884 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15885 tcp->tcp_drop_opt_ack_cnt > 0) { 15886 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15887 "- dropped (cnt %d)\n", 15888 tea->TLI_error, tea->UNIX_error, 15889 tcp->tcp_drop_opt_ack_cnt); 15890 freemsg(mp); 15891 tcp->tcp_drop_opt_ack_cnt--; 15892 return; 15893 } 15894 break; 15895 case T_OPTMGMT_ACK: 15896 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15897 /* T_OPTMGMT_REQ generated by TCP */ 15898 freemsg(mp); 15899 tcp->tcp_drop_opt_ack_cnt--; 15900 return; 15901 } 15902 break; 15903 default: 15904 break; 15905 } 15906 break; 15907 case M_FLUSH: 15908 if (*rptr & FLUSHR) 15909 flushq(q, FLUSHDATA); 15910 break; 15911 default: 15912 /* M_CTL will be directly sent to tcp_icmp_error() */ 15913 ASSERT(DB_TYPE(mp) != M_CTL); 15914 break; 15915 } 15916 /* 15917 * Make sure we set this bit before sending the ACK for 15918 * bind. Otherwise accept could possibly run and free 15919 * this tcp struct. 15920 */ 15921 putnext(q, mp); 15922 } 15923 15924 /* 15925 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15926 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15927 * tcp_rsrv() try again. 15928 */ 15929 static void 15930 tcp_ordrel_kick(void *arg) 15931 { 15932 conn_t *connp = (conn_t *)arg; 15933 tcp_t *tcp = connp->conn_tcp; 15934 15935 tcp->tcp_ordrelid = 0; 15936 tcp->tcp_timeout = B_FALSE; 15937 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15938 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15939 qenable(tcp->tcp_rq); 15940 } 15941 } 15942 15943 /* ARGSUSED */ 15944 static void 15945 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15946 { 15947 conn_t *connp = (conn_t *)arg; 15948 tcp_t *tcp = connp->conn_tcp; 15949 queue_t *q = tcp->tcp_rq; 15950 uint_t thwin; 15951 tcp_stack_t *tcps = tcp->tcp_tcps; 15952 15953 freeb(mp); 15954 15955 TCP_STAT(tcps, tcp_rsrv_calls); 15956 15957 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15958 return; 15959 } 15960 15961 if (tcp->tcp_fused) { 15962 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15963 15964 ASSERT(tcp->tcp_fused); 15965 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15966 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15967 ASSERT(!TCP_IS_DETACHED(tcp)); 15968 ASSERT(tcp->tcp_connp->conn_sqp == 15969 peer_tcp->tcp_connp->conn_sqp); 15970 15971 /* 15972 * Normally we would not get backenabled in synchronous 15973 * streams mode, but in case this happens, we need to plug 15974 * synchronous streams during our drain to prevent a race 15975 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15976 */ 15977 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15978 if (tcp->tcp_rcv_list != NULL) 15979 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15980 15981 if (peer_tcp > tcp) { 15982 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15983 mutex_enter(&tcp->tcp_non_sq_lock); 15984 } else { 15985 mutex_enter(&tcp->tcp_non_sq_lock); 15986 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15987 } 15988 15989 if (peer_tcp->tcp_flow_stopped && 15990 (TCP_UNSENT_BYTES(peer_tcp) <= 15991 peer_tcp->tcp_xmit_lowater)) { 15992 tcp_clrqfull(peer_tcp); 15993 } 15994 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15995 mutex_exit(&tcp->tcp_non_sq_lock); 15996 15997 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15998 TCP_STAT(tcps, tcp_fusion_backenabled); 15999 return; 16000 } 16001 16002 if (canputnext(q)) { 16003 tcp->tcp_rwnd = q->q_hiwat; 16004 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 16005 << tcp->tcp_rcv_ws; 16006 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 16007 /* 16008 * Send back a window update immediately if TCP is above 16009 * ESTABLISHED state and the increase of the rcv window 16010 * that the other side knows is at least 1 MSS after flow 16011 * control is lifted. 16012 */ 16013 if (tcp->tcp_state >= TCPS_ESTABLISHED && 16014 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 16015 tcp_xmit_ctl(NULL, tcp, 16016 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 16017 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 16018 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 16019 } 16020 } 16021 /* Handle a failure to allocate a T_ORDREL_IND here */ 16022 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 16023 ASSERT(tcp->tcp_listener == NULL); 16024 if (tcp->tcp_rcv_list != NULL) { 16025 (void) tcp_rcv_drain(q, tcp); 16026 } 16027 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 16028 mp = mi_tpi_ordrel_ind(); 16029 if (mp) { 16030 tcp->tcp_ordrel_done = B_TRUE; 16031 putnext(q, mp); 16032 if (tcp->tcp_deferred_clean_death) { 16033 /* 16034 * tcp_clean_death was deferred for 16035 * T_ORDREL_IND - do it now 16036 */ 16037 tcp->tcp_deferred_clean_death = B_FALSE; 16038 (void) tcp_clean_death(tcp, 16039 tcp->tcp_client_errno, 22); 16040 } 16041 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16042 /* 16043 * If there isn't already a timer running 16044 * start one. Use a 4 second 16045 * timer as a fallback since it can't fail. 16046 */ 16047 tcp->tcp_timeout = B_TRUE; 16048 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16049 MSEC_TO_TICK(4000)); 16050 } 16051 } 16052 } 16053 16054 /* 16055 * The read side service routine is called mostly when we get back-enabled as a 16056 * result of flow control relief. Since we don't actually queue anything in 16057 * TCP, we have no data to send out of here. What we do is clear the receive 16058 * window, and send out a window update. 16059 * This routine is also called to drive an orderly release message upstream 16060 * if the attempt in tcp_rput failed. 16061 */ 16062 static void 16063 tcp_rsrv(queue_t *q) 16064 { 16065 conn_t *connp = Q_TO_CONN(q); 16066 tcp_t *tcp = connp->conn_tcp; 16067 mblk_t *mp; 16068 tcp_stack_t *tcps = tcp->tcp_tcps; 16069 16070 /* No code does a putq on the read side */ 16071 ASSERT(q->q_first == NULL); 16072 16073 /* Nothing to do for the default queue */ 16074 if (q == tcps->tcps_g_q) { 16075 return; 16076 } 16077 16078 mp = allocb(0, BPRI_HI); 16079 if (mp == NULL) { 16080 /* 16081 * We are under memory pressure. Return for now and we 16082 * we will be called again later. 16083 */ 16084 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16085 /* 16086 * If there isn't already a timer running 16087 * start one. Use a 4 second 16088 * timer as a fallback since it can't fail. 16089 */ 16090 tcp->tcp_timeout = B_TRUE; 16091 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16092 MSEC_TO_TICK(4000)); 16093 } 16094 return; 16095 } 16096 CONN_INC_REF(connp); 16097 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16098 SQTAG_TCP_RSRV); 16099 } 16100 16101 /* 16102 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16103 * We do not allow the receive window to shrink. After setting rwnd, 16104 * set the flow control hiwat of the stream. 16105 * 16106 * This function is called in 2 cases: 16107 * 16108 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16109 * connection (passive open) and in tcp_rput_data() for active connect. 16110 * This is called after tcp_mss_set() when the desired MSS value is known. 16111 * This makes sure that our window size is a mutiple of the other side's 16112 * MSS. 16113 * 2) Handling SO_RCVBUF option. 16114 * 16115 * It is ASSUMED that the requested size is a multiple of the current MSS. 16116 * 16117 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16118 * user requests so. 16119 */ 16120 static int 16121 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16122 { 16123 uint32_t mss = tcp->tcp_mss; 16124 uint32_t old_max_rwnd; 16125 uint32_t max_transmittable_rwnd; 16126 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16127 tcp_stack_t *tcps = tcp->tcp_tcps; 16128 16129 if (tcp->tcp_fused) { 16130 size_t sth_hiwat; 16131 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16132 16133 ASSERT(peer_tcp != NULL); 16134 /* 16135 * Record the stream head's high water mark for 16136 * this endpoint; this is used for flow-control 16137 * purposes in tcp_fuse_output(). 16138 */ 16139 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16140 if (!tcp_detached) 16141 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16142 16143 /* 16144 * In the fusion case, the maxpsz stream head value of 16145 * our peer is set according to its send buffer size 16146 * and our receive buffer size; since the latter may 16147 * have changed we need to update the peer's maxpsz. 16148 */ 16149 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16150 return (rwnd); 16151 } 16152 16153 if (tcp_detached) 16154 old_max_rwnd = tcp->tcp_rwnd; 16155 else 16156 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16157 16158 /* 16159 * Insist on a receive window that is at least 16160 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16161 * funny TCP interactions of Nagle algorithm, SWS avoidance 16162 * and delayed acknowledgement. 16163 */ 16164 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16165 16166 /* 16167 * If window size info has already been exchanged, TCP should not 16168 * shrink the window. Shrinking window is doable if done carefully. 16169 * We may add that support later. But so far there is not a real 16170 * need to do that. 16171 */ 16172 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16173 /* MSS may have changed, do a round up again. */ 16174 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16175 } 16176 16177 /* 16178 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16179 * can be applied even before the window scale option is decided. 16180 */ 16181 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16182 if (rwnd > max_transmittable_rwnd) { 16183 rwnd = max_transmittable_rwnd - 16184 (max_transmittable_rwnd % mss); 16185 if (rwnd < mss) 16186 rwnd = max_transmittable_rwnd; 16187 /* 16188 * If we're over the limit we may have to back down tcp_rwnd. 16189 * The increment below won't work for us. So we set all three 16190 * here and the increment below will have no effect. 16191 */ 16192 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16193 } 16194 if (tcp->tcp_localnet) { 16195 tcp->tcp_rack_abs_max = 16196 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16197 } else { 16198 /* 16199 * For a remote host on a different subnet (through a router), 16200 * we ack every other packet to be conforming to RFC1122. 16201 * tcp_deferred_acks_max is default to 2. 16202 */ 16203 tcp->tcp_rack_abs_max = 16204 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16205 } 16206 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16207 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16208 else 16209 tcp->tcp_rack_cur_max = 0; 16210 /* 16211 * Increment the current rwnd by the amount the maximum grew (we 16212 * can not overwrite it since we might be in the middle of a 16213 * connection.) 16214 */ 16215 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16216 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16217 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16218 tcp->tcp_cwnd_max = rwnd; 16219 16220 if (tcp_detached) 16221 return (rwnd); 16222 /* 16223 * We set the maximum receive window into rq->q_hiwat. 16224 * This is not actually used for flow control. 16225 */ 16226 tcp->tcp_rq->q_hiwat = rwnd; 16227 /* 16228 * Set the Stream head high water mark. This doesn't have to be 16229 * here, since we are simply using default values, but we would 16230 * prefer to choose these values algorithmically, with a likely 16231 * relationship to rwnd. 16232 */ 16233 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16234 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16235 return (rwnd); 16236 } 16237 16238 /* 16239 * Return SNMP stuff in buffer in mpdata. 16240 */ 16241 mblk_t * 16242 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16243 { 16244 mblk_t *mpdata; 16245 mblk_t *mp_conn_ctl = NULL; 16246 mblk_t *mp_conn_tail; 16247 mblk_t *mp_attr_ctl = NULL; 16248 mblk_t *mp_attr_tail; 16249 mblk_t *mp6_conn_ctl = NULL; 16250 mblk_t *mp6_conn_tail; 16251 mblk_t *mp6_attr_ctl = NULL; 16252 mblk_t *mp6_attr_tail; 16253 struct opthdr *optp; 16254 mib2_tcpConnEntry_t tce; 16255 mib2_tcp6ConnEntry_t tce6; 16256 mib2_transportMLPEntry_t mlp; 16257 connf_t *connfp; 16258 int i; 16259 boolean_t ispriv; 16260 zoneid_t zoneid; 16261 int v4_conn_idx; 16262 int v6_conn_idx; 16263 conn_t *connp = Q_TO_CONN(q); 16264 tcp_stack_t *tcps; 16265 ip_stack_t *ipst; 16266 mblk_t *mp2ctl; 16267 16268 /* 16269 * make a copy of the original message 16270 */ 16271 mp2ctl = copymsg(mpctl); 16272 16273 if (mpctl == NULL || 16274 (mpdata = mpctl->b_cont) == NULL || 16275 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16276 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16277 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16278 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16279 freemsg(mp_conn_ctl); 16280 freemsg(mp_attr_ctl); 16281 freemsg(mp6_conn_ctl); 16282 freemsg(mp6_attr_ctl); 16283 freemsg(mpctl); 16284 freemsg(mp2ctl); 16285 return (NULL); 16286 } 16287 16288 ipst = connp->conn_netstack->netstack_ip; 16289 tcps = connp->conn_netstack->netstack_tcp; 16290 16291 /* build table of connections -- need count in fixed part */ 16292 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16293 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16294 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16295 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16296 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16297 16298 ispriv = 16299 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16300 zoneid = Q_TO_CONN(q)->conn_zoneid; 16301 16302 v4_conn_idx = v6_conn_idx = 0; 16303 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16304 16305 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16306 ipst = tcps->tcps_netstack->netstack_ip; 16307 16308 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16309 16310 connp = NULL; 16311 16312 while ((connp = 16313 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16314 tcp_t *tcp; 16315 boolean_t needattr; 16316 16317 if (connp->conn_zoneid != zoneid) 16318 continue; /* not in this zone */ 16319 16320 tcp = connp->conn_tcp; 16321 UPDATE_MIB(&tcps->tcps_mib, 16322 tcpHCInSegs, tcp->tcp_ibsegs); 16323 tcp->tcp_ibsegs = 0; 16324 UPDATE_MIB(&tcps->tcps_mib, 16325 tcpHCOutSegs, tcp->tcp_obsegs); 16326 tcp->tcp_obsegs = 0; 16327 16328 tce6.tcp6ConnState = tce.tcpConnState = 16329 tcp_snmp_state(tcp); 16330 if (tce.tcpConnState == MIB2_TCP_established || 16331 tce.tcpConnState == MIB2_TCP_closeWait) 16332 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16333 16334 needattr = B_FALSE; 16335 bzero(&mlp, sizeof (mlp)); 16336 if (connp->conn_mlp_type != mlptSingle) { 16337 if (connp->conn_mlp_type == mlptShared || 16338 connp->conn_mlp_type == mlptBoth) 16339 mlp.tme_flags |= MIB2_TMEF_SHARED; 16340 if (connp->conn_mlp_type == mlptPrivate || 16341 connp->conn_mlp_type == mlptBoth) 16342 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16343 needattr = B_TRUE; 16344 } 16345 if (connp->conn_peercred != NULL) { 16346 ts_label_t *tsl; 16347 16348 tsl = crgetlabel(connp->conn_peercred); 16349 mlp.tme_doi = label2doi(tsl); 16350 mlp.tme_label = *label2bslabel(tsl); 16351 needattr = B_TRUE; 16352 } 16353 16354 /* Create a message to report on IPv6 entries */ 16355 if (tcp->tcp_ipversion == IPV6_VERSION) { 16356 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16357 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16358 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16359 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16360 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16361 /* Don't want just anybody seeing these... */ 16362 if (ispriv) { 16363 tce6.tcp6ConnEntryInfo.ce_snxt = 16364 tcp->tcp_snxt; 16365 tce6.tcp6ConnEntryInfo.ce_suna = 16366 tcp->tcp_suna; 16367 tce6.tcp6ConnEntryInfo.ce_rnxt = 16368 tcp->tcp_rnxt; 16369 tce6.tcp6ConnEntryInfo.ce_rack = 16370 tcp->tcp_rack; 16371 } else { 16372 /* 16373 * Netstat, unfortunately, uses this to 16374 * get send/receive queue sizes. How to fix? 16375 * Why not compute the difference only? 16376 */ 16377 tce6.tcp6ConnEntryInfo.ce_snxt = 16378 tcp->tcp_snxt - tcp->tcp_suna; 16379 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16380 tce6.tcp6ConnEntryInfo.ce_rnxt = 16381 tcp->tcp_rnxt - tcp->tcp_rack; 16382 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16383 } 16384 16385 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16386 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16387 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16388 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16389 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16390 16391 tce6.tcp6ConnCreationProcess = 16392 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16393 tcp->tcp_cpid; 16394 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16395 16396 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16397 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16398 16399 mlp.tme_connidx = v6_conn_idx++; 16400 if (needattr) 16401 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16402 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16403 } 16404 /* 16405 * Create an IPv4 table entry for IPv4 entries and also 16406 * for IPv6 entries which are bound to in6addr_any 16407 * but don't have IPV6_V6ONLY set. 16408 * (i.e. anything an IPv4 peer could connect to) 16409 */ 16410 if (tcp->tcp_ipversion == IPV4_VERSION || 16411 (tcp->tcp_state <= TCPS_LISTEN && 16412 !tcp->tcp_connp->conn_ipv6_v6only && 16413 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16414 if (tcp->tcp_ipversion == IPV6_VERSION) { 16415 tce.tcpConnRemAddress = INADDR_ANY; 16416 tce.tcpConnLocalAddress = INADDR_ANY; 16417 } else { 16418 tce.tcpConnRemAddress = 16419 tcp->tcp_remote; 16420 tce.tcpConnLocalAddress = 16421 tcp->tcp_ip_src; 16422 } 16423 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16424 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16425 /* Don't want just anybody seeing these... */ 16426 if (ispriv) { 16427 tce.tcpConnEntryInfo.ce_snxt = 16428 tcp->tcp_snxt; 16429 tce.tcpConnEntryInfo.ce_suna = 16430 tcp->tcp_suna; 16431 tce.tcpConnEntryInfo.ce_rnxt = 16432 tcp->tcp_rnxt; 16433 tce.tcpConnEntryInfo.ce_rack = 16434 tcp->tcp_rack; 16435 } else { 16436 /* 16437 * Netstat, unfortunately, uses this to 16438 * get send/receive queue sizes. How 16439 * to fix? 16440 * Why not compute the difference only? 16441 */ 16442 tce.tcpConnEntryInfo.ce_snxt = 16443 tcp->tcp_snxt - tcp->tcp_suna; 16444 tce.tcpConnEntryInfo.ce_suna = 0; 16445 tce.tcpConnEntryInfo.ce_rnxt = 16446 tcp->tcp_rnxt - tcp->tcp_rack; 16447 tce.tcpConnEntryInfo.ce_rack = 0; 16448 } 16449 16450 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16451 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16452 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16453 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16454 tce.tcpConnEntryInfo.ce_state = 16455 tcp->tcp_state; 16456 16457 tce.tcpConnCreationProcess = 16458 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16459 tcp->tcp_cpid; 16460 tce.tcpConnCreationTime = tcp->tcp_open_time; 16461 16462 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16463 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16464 16465 mlp.tme_connidx = v4_conn_idx++; 16466 if (needattr) 16467 (void) snmp_append_data2( 16468 mp_attr_ctl->b_cont, 16469 &mp_attr_tail, (char *)&mlp, 16470 sizeof (mlp)); 16471 } 16472 } 16473 } 16474 16475 /* fixed length structure for IPv4 and IPv6 counters */ 16476 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16477 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16478 sizeof (mib2_tcp6ConnEntry_t)); 16479 /* synchronize 32- and 64-bit counters */ 16480 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16481 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16482 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16483 optp->level = MIB2_TCP; 16484 optp->name = 0; 16485 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16486 sizeof (tcps->tcps_mib)); 16487 optp->len = msgdsize(mpdata); 16488 qreply(q, mpctl); 16489 16490 /* table of connections... */ 16491 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16492 sizeof (struct T_optmgmt_ack)]; 16493 optp->level = MIB2_TCP; 16494 optp->name = MIB2_TCP_CONN; 16495 optp->len = msgdsize(mp_conn_ctl->b_cont); 16496 qreply(q, mp_conn_ctl); 16497 16498 /* table of MLP attributes... */ 16499 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16500 sizeof (struct T_optmgmt_ack)]; 16501 optp->level = MIB2_TCP; 16502 optp->name = EXPER_XPORT_MLP; 16503 optp->len = msgdsize(mp_attr_ctl->b_cont); 16504 if (optp->len == 0) 16505 freemsg(mp_attr_ctl); 16506 else 16507 qreply(q, mp_attr_ctl); 16508 16509 /* table of IPv6 connections... */ 16510 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16511 sizeof (struct T_optmgmt_ack)]; 16512 optp->level = MIB2_TCP6; 16513 optp->name = MIB2_TCP6_CONN; 16514 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16515 qreply(q, mp6_conn_ctl); 16516 16517 /* table of IPv6 MLP attributes... */ 16518 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16519 sizeof (struct T_optmgmt_ack)]; 16520 optp->level = MIB2_TCP6; 16521 optp->name = EXPER_XPORT_MLP; 16522 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16523 if (optp->len == 0) 16524 freemsg(mp6_attr_ctl); 16525 else 16526 qreply(q, mp6_attr_ctl); 16527 return (mp2ctl); 16528 } 16529 16530 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16531 /* ARGSUSED */ 16532 int 16533 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16534 { 16535 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16536 16537 switch (level) { 16538 case MIB2_TCP: 16539 switch (name) { 16540 case 13: 16541 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16542 return (0); 16543 /* TODO: delete entry defined by tce */ 16544 return (1); 16545 default: 16546 return (0); 16547 } 16548 default: 16549 return (1); 16550 } 16551 } 16552 16553 /* Translate TCP state to MIB2 TCP state. */ 16554 static int 16555 tcp_snmp_state(tcp_t *tcp) 16556 { 16557 if (tcp == NULL) 16558 return (0); 16559 16560 switch (tcp->tcp_state) { 16561 case TCPS_CLOSED: 16562 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16563 case TCPS_BOUND: 16564 return (MIB2_TCP_closed); 16565 case TCPS_LISTEN: 16566 return (MIB2_TCP_listen); 16567 case TCPS_SYN_SENT: 16568 return (MIB2_TCP_synSent); 16569 case TCPS_SYN_RCVD: 16570 return (MIB2_TCP_synReceived); 16571 case TCPS_ESTABLISHED: 16572 return (MIB2_TCP_established); 16573 case TCPS_CLOSE_WAIT: 16574 return (MIB2_TCP_closeWait); 16575 case TCPS_FIN_WAIT_1: 16576 return (MIB2_TCP_finWait1); 16577 case TCPS_CLOSING: 16578 return (MIB2_TCP_closing); 16579 case TCPS_LAST_ACK: 16580 return (MIB2_TCP_lastAck); 16581 case TCPS_FIN_WAIT_2: 16582 return (MIB2_TCP_finWait2); 16583 case TCPS_TIME_WAIT: 16584 return (MIB2_TCP_timeWait); 16585 default: 16586 return (0); 16587 } 16588 } 16589 16590 static char tcp_report_header[] = 16591 "TCP " MI_COL_HDRPAD_STR 16592 "zone dest snxt suna " 16593 "swnd rnxt rack rwnd rto mss w sw rw t " 16594 "recent [lport,fport] state"; 16595 16596 /* 16597 * TCP status report triggered via the Named Dispatch mechanism. 16598 */ 16599 /* ARGSUSED */ 16600 static void 16601 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16602 cred_t *cr) 16603 { 16604 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16605 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16606 char cflag; 16607 in6_addr_t v6dst; 16608 char buf[80]; 16609 uint_t print_len, buf_len; 16610 16611 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16612 if (buf_len <= 0) 16613 return; 16614 16615 if (hashval >= 0) 16616 (void) sprintf(hash, "%03d ", hashval); 16617 else 16618 hash[0] = '\0'; 16619 16620 /* 16621 * Note that we use the remote address in the tcp_b structure. 16622 * This means that it will print out the real destination address, 16623 * not the next hop's address if source routing is used. This 16624 * avoid the confusion on the output because user may not 16625 * know that source routing is used for a connection. 16626 */ 16627 if (tcp->tcp_ipversion == IPV4_VERSION) { 16628 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16629 } else { 16630 v6dst = tcp->tcp_remote_v6; 16631 } 16632 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16633 /* 16634 * the ispriv checks are so that normal users cannot determine 16635 * sequence number information using NDD. 16636 */ 16637 16638 if (TCP_IS_DETACHED(tcp)) 16639 cflag = '*'; 16640 else 16641 cflag = ' '; 16642 print_len = snprintf((char *)mp->b_wptr, buf_len, 16643 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16644 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16645 hash, 16646 (void *)tcp, 16647 tcp->tcp_connp->conn_zoneid, 16648 addrbuf, 16649 (ispriv) ? tcp->tcp_snxt : 0, 16650 (ispriv) ? tcp->tcp_suna : 0, 16651 tcp->tcp_swnd, 16652 (ispriv) ? tcp->tcp_rnxt : 0, 16653 (ispriv) ? tcp->tcp_rack : 0, 16654 tcp->tcp_rwnd, 16655 tcp->tcp_rto, 16656 tcp->tcp_mss, 16657 tcp->tcp_snd_ws_ok, 16658 tcp->tcp_snd_ws, 16659 tcp->tcp_rcv_ws, 16660 tcp->tcp_snd_ts_ok, 16661 tcp->tcp_ts_recent, 16662 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16663 if (print_len < buf_len) { 16664 ((mblk_t *)mp)->b_wptr += print_len; 16665 } else { 16666 ((mblk_t *)mp)->b_wptr += buf_len; 16667 } 16668 } 16669 16670 /* 16671 * TCP status report (for listeners only) triggered via the Named Dispatch 16672 * mechanism. 16673 */ 16674 /* ARGSUSED */ 16675 static void 16676 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16677 { 16678 char addrbuf[INET6_ADDRSTRLEN]; 16679 in6_addr_t v6dst; 16680 uint_t print_len, buf_len; 16681 16682 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16683 if (buf_len <= 0) 16684 return; 16685 16686 if (tcp->tcp_ipversion == IPV4_VERSION) { 16687 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16688 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16689 } else { 16690 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16691 addrbuf, sizeof (addrbuf)); 16692 } 16693 print_len = snprintf((char *)mp->b_wptr, buf_len, 16694 "%03d " 16695 MI_COL_PTRFMT_STR 16696 "%d %s %05u %08u %d/%d/%d%c\n", 16697 hashval, (void *)tcp, 16698 tcp->tcp_connp->conn_zoneid, 16699 addrbuf, 16700 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16701 tcp->tcp_conn_req_seqnum, 16702 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16703 tcp->tcp_conn_req_max, 16704 tcp->tcp_syn_defense ? '*' : ' '); 16705 if (print_len < buf_len) { 16706 ((mblk_t *)mp)->b_wptr += print_len; 16707 } else { 16708 ((mblk_t *)mp)->b_wptr += buf_len; 16709 } 16710 } 16711 16712 /* TCP status report triggered via the Named Dispatch mechanism. */ 16713 /* ARGSUSED */ 16714 static int 16715 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16716 { 16717 tcp_t *tcp; 16718 int i; 16719 conn_t *connp; 16720 connf_t *connfp; 16721 zoneid_t zoneid; 16722 tcp_stack_t *tcps; 16723 ip_stack_t *ipst; 16724 16725 zoneid = Q_TO_CONN(q)->conn_zoneid; 16726 tcps = Q_TO_TCP(q)->tcp_tcps; 16727 16728 /* 16729 * Because of the ndd constraint, at most we can have 64K buffer 16730 * to put in all TCP info. So to be more efficient, just 16731 * allocate a 64K buffer here, assuming we need that large buffer. 16732 * This may be a problem as any user can read tcp_status. Therefore 16733 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16734 * This should be OK as normal users should not do this too often. 16735 */ 16736 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16737 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16738 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16739 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16740 return (0); 16741 } 16742 } 16743 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16744 /* The following may work even if we cannot get a large buf. */ 16745 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16746 return (0); 16747 } 16748 16749 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16750 16751 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16752 16753 ipst = tcps->tcps_netstack->netstack_ip; 16754 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16755 16756 connp = NULL; 16757 16758 while ((connp = 16759 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16760 tcp = connp->conn_tcp; 16761 if (zoneid != GLOBAL_ZONEID && 16762 zoneid != connp->conn_zoneid) 16763 continue; 16764 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16765 cr); 16766 } 16767 16768 } 16769 16770 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16771 return (0); 16772 } 16773 16774 /* TCP status report triggered via the Named Dispatch mechanism. */ 16775 /* ARGSUSED */ 16776 static int 16777 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16778 { 16779 tf_t *tbf; 16780 tcp_t *tcp; 16781 int i; 16782 zoneid_t zoneid; 16783 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16784 16785 zoneid = Q_TO_CONN(q)->conn_zoneid; 16786 16787 /* Refer to comments in tcp_status_report(). */ 16788 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16789 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16790 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16791 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16792 return (0); 16793 } 16794 } 16795 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16796 /* The following may work even if we cannot get a large buf. */ 16797 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16798 return (0); 16799 } 16800 16801 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16802 16803 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16804 tbf = &tcps->tcps_bind_fanout[i]; 16805 mutex_enter(&tbf->tf_lock); 16806 for (tcp = tbf->tf_tcp; tcp != NULL; 16807 tcp = tcp->tcp_bind_hash) { 16808 if (zoneid != GLOBAL_ZONEID && 16809 zoneid != tcp->tcp_connp->conn_zoneid) 16810 continue; 16811 CONN_INC_REF(tcp->tcp_connp); 16812 tcp_report_item(mp->b_cont, tcp, i, 16813 Q_TO_TCP(q), cr); 16814 CONN_DEC_REF(tcp->tcp_connp); 16815 } 16816 mutex_exit(&tbf->tf_lock); 16817 } 16818 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16819 return (0); 16820 } 16821 16822 /* TCP status report triggered via the Named Dispatch mechanism. */ 16823 /* ARGSUSED */ 16824 static int 16825 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16826 { 16827 connf_t *connfp; 16828 conn_t *connp; 16829 tcp_t *tcp; 16830 int i; 16831 zoneid_t zoneid; 16832 tcp_stack_t *tcps; 16833 ip_stack_t *ipst; 16834 16835 zoneid = Q_TO_CONN(q)->conn_zoneid; 16836 tcps = Q_TO_TCP(q)->tcp_tcps; 16837 16838 /* Refer to comments in tcp_status_report(). */ 16839 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16840 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16841 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16842 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16843 return (0); 16844 } 16845 } 16846 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16847 /* The following may work even if we cannot get a large buf. */ 16848 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16849 return (0); 16850 } 16851 16852 (void) mi_mpprintf(mp, 16853 " TCP " MI_COL_HDRPAD_STR 16854 "zone IP addr port seqnum backlog (q0/q/max)"); 16855 16856 ipst = tcps->tcps_netstack->netstack_ip; 16857 16858 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16859 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16860 connp = NULL; 16861 while ((connp = 16862 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16863 tcp = connp->conn_tcp; 16864 if (zoneid != GLOBAL_ZONEID && 16865 zoneid != connp->conn_zoneid) 16866 continue; 16867 tcp_report_listener(mp->b_cont, tcp, i); 16868 } 16869 } 16870 16871 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16872 return (0); 16873 } 16874 16875 /* TCP status report triggered via the Named Dispatch mechanism. */ 16876 /* ARGSUSED */ 16877 static int 16878 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16879 { 16880 connf_t *connfp; 16881 conn_t *connp; 16882 tcp_t *tcp; 16883 int i; 16884 zoneid_t zoneid; 16885 tcp_stack_t *tcps; 16886 ip_stack_t *ipst; 16887 16888 zoneid = Q_TO_CONN(q)->conn_zoneid; 16889 tcps = Q_TO_TCP(q)->tcp_tcps; 16890 ipst = tcps->tcps_netstack->netstack_ip; 16891 16892 /* Refer to comments in tcp_status_report(). */ 16893 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16894 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16895 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16896 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16897 return (0); 16898 } 16899 } 16900 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16901 /* The following may work even if we cannot get a large buf. */ 16902 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16903 return (0); 16904 } 16905 16906 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16907 ipst->ips_ipcl_conn_fanout_size); 16908 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16909 16910 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16911 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16912 connp = NULL; 16913 while ((connp = 16914 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16915 tcp = connp->conn_tcp; 16916 if (zoneid != GLOBAL_ZONEID && 16917 zoneid != connp->conn_zoneid) 16918 continue; 16919 tcp_report_item(mp->b_cont, tcp, i, 16920 Q_TO_TCP(q), cr); 16921 } 16922 } 16923 16924 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16925 return (0); 16926 } 16927 16928 /* TCP status report triggered via the Named Dispatch mechanism. */ 16929 /* ARGSUSED */ 16930 static int 16931 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16932 { 16933 tf_t *tf; 16934 tcp_t *tcp; 16935 int i; 16936 zoneid_t zoneid; 16937 tcp_stack_t *tcps; 16938 16939 zoneid = Q_TO_CONN(q)->conn_zoneid; 16940 tcps = Q_TO_TCP(q)->tcp_tcps; 16941 16942 /* Refer to comments in tcp_status_report(). */ 16943 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16944 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16945 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16946 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16947 return (0); 16948 } 16949 } 16950 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16951 /* The following may work even if we cannot get a large buf. */ 16952 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16953 return (0); 16954 } 16955 16956 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16957 16958 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16959 tf = &tcps->tcps_acceptor_fanout[i]; 16960 mutex_enter(&tf->tf_lock); 16961 for (tcp = tf->tf_tcp; tcp != NULL; 16962 tcp = tcp->tcp_acceptor_hash) { 16963 if (zoneid != GLOBAL_ZONEID && 16964 zoneid != tcp->tcp_connp->conn_zoneid) 16965 continue; 16966 tcp_report_item(mp->b_cont, tcp, i, 16967 Q_TO_TCP(q), cr); 16968 } 16969 mutex_exit(&tf->tf_lock); 16970 } 16971 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16972 return (0); 16973 } 16974 16975 /* 16976 * tcp_timer is the timer service routine. It handles the retransmission, 16977 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16978 * from the state of the tcp instance what kind of action needs to be done 16979 * at the time it is called. 16980 */ 16981 static void 16982 tcp_timer(void *arg) 16983 { 16984 mblk_t *mp; 16985 clock_t first_threshold; 16986 clock_t second_threshold; 16987 clock_t ms; 16988 uint32_t mss; 16989 conn_t *connp = (conn_t *)arg; 16990 tcp_t *tcp = connp->conn_tcp; 16991 tcp_stack_t *tcps = tcp->tcp_tcps; 16992 16993 tcp->tcp_timer_tid = 0; 16994 16995 if (tcp->tcp_fused) 16996 return; 16997 16998 first_threshold = tcp->tcp_first_timer_threshold; 16999 second_threshold = tcp->tcp_second_timer_threshold; 17000 switch (tcp->tcp_state) { 17001 case TCPS_IDLE: 17002 case TCPS_BOUND: 17003 case TCPS_LISTEN: 17004 return; 17005 case TCPS_SYN_RCVD: { 17006 tcp_t *listener = tcp->tcp_listener; 17007 17008 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 17009 ASSERT(tcp->tcp_rq == listener->tcp_rq); 17010 /* it's our first timeout */ 17011 tcp->tcp_syn_rcvd_timeout = 1; 17012 mutex_enter(&listener->tcp_eager_lock); 17013 listener->tcp_syn_rcvd_timeout++; 17014 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 17015 /* 17016 * Make this eager available for drop if we 17017 * need to drop one to accomodate a new 17018 * incoming SYN request. 17019 */ 17020 MAKE_DROPPABLE(listener, tcp); 17021 } 17022 if (!listener->tcp_syn_defense && 17023 (listener->tcp_syn_rcvd_timeout > 17024 (tcps->tcps_conn_req_max_q0 >> 2)) && 17025 (tcps->tcps_conn_req_max_q0 > 200)) { 17026 /* We may be under attack. Put on a defense. */ 17027 listener->tcp_syn_defense = B_TRUE; 17028 cmn_err(CE_WARN, "High TCP connect timeout " 17029 "rate! System (port %d) may be under a " 17030 "SYN flood attack!", 17031 BE16_TO_U16(listener->tcp_tcph->th_lport)); 17032 17033 listener->tcp_ip_addr_cache = kmem_zalloc( 17034 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 17035 KM_NOSLEEP); 17036 } 17037 mutex_exit(&listener->tcp_eager_lock); 17038 } else if (listener != NULL) { 17039 mutex_enter(&listener->tcp_eager_lock); 17040 tcp->tcp_syn_rcvd_timeout++; 17041 if (tcp->tcp_syn_rcvd_timeout > 1 && 17042 !tcp->tcp_closemp_used) { 17043 /* 17044 * This is our second timeout. Put the tcp in 17045 * the list of droppable eagers to allow it to 17046 * be dropped, if needed. We don't check 17047 * whether tcp_dontdrop is set or not to 17048 * protect ourselve from a SYN attack where a 17049 * remote host can spoof itself as one of the 17050 * good IP source and continue to hold 17051 * resources too long. 17052 */ 17053 MAKE_DROPPABLE(listener, tcp); 17054 } 17055 mutex_exit(&listener->tcp_eager_lock); 17056 } 17057 } 17058 /* FALLTHRU */ 17059 case TCPS_SYN_SENT: 17060 first_threshold = tcp->tcp_first_ctimer_threshold; 17061 second_threshold = tcp->tcp_second_ctimer_threshold; 17062 break; 17063 case TCPS_ESTABLISHED: 17064 case TCPS_FIN_WAIT_1: 17065 case TCPS_CLOSING: 17066 case TCPS_CLOSE_WAIT: 17067 case TCPS_LAST_ACK: 17068 /* If we have data to rexmit */ 17069 if (tcp->tcp_suna != tcp->tcp_snxt) { 17070 clock_t time_to_wait; 17071 17072 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17073 if (!tcp->tcp_xmit_head) 17074 break; 17075 time_to_wait = lbolt - 17076 (clock_t)tcp->tcp_xmit_head->b_prev; 17077 time_to_wait = tcp->tcp_rto - 17078 TICK_TO_MSEC(time_to_wait); 17079 /* 17080 * If the timer fires too early, 1 clock tick earlier, 17081 * restart the timer. 17082 */ 17083 if (time_to_wait > msec_per_tick) { 17084 TCP_STAT(tcps, tcp_timer_fire_early); 17085 TCP_TIMER_RESTART(tcp, time_to_wait); 17086 return; 17087 } 17088 /* 17089 * When we probe zero windows, we force the swnd open. 17090 * If our peer acks with a closed window swnd will be 17091 * set to zero by tcp_rput(). As long as we are 17092 * receiving acks tcp_rput will 17093 * reset 'tcp_ms_we_have_waited' so as not to trip the 17094 * first and second interval actions. NOTE: the timer 17095 * interval is allowed to continue its exponential 17096 * backoff. 17097 */ 17098 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17099 if (tcp->tcp_debug) { 17100 (void) strlog(TCP_MOD_ID, 0, 1, 17101 SL_TRACE, "tcp_timer: zero win"); 17102 } 17103 } else { 17104 /* 17105 * After retransmission, we need to do 17106 * slow start. Set the ssthresh to one 17107 * half of current effective window and 17108 * cwnd to one MSS. Also reset 17109 * tcp_cwnd_cnt. 17110 * 17111 * Note that if tcp_ssthresh is reduced because 17112 * of ECN, do not reduce it again unless it is 17113 * already one window of data away (tcp_cwr 17114 * should then be cleared) or this is a 17115 * timeout for a retransmitted segment. 17116 */ 17117 uint32_t npkt; 17118 17119 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17120 npkt = ((tcp->tcp_timer_backoff ? 17121 tcp->tcp_cwnd_ssthresh : 17122 tcp->tcp_snxt - 17123 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17124 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17125 tcp->tcp_mss; 17126 } 17127 tcp->tcp_cwnd = tcp->tcp_mss; 17128 tcp->tcp_cwnd_cnt = 0; 17129 if (tcp->tcp_ecn_ok) { 17130 tcp->tcp_cwr = B_TRUE; 17131 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17132 tcp->tcp_ecn_cwr_sent = B_FALSE; 17133 } 17134 } 17135 break; 17136 } 17137 /* 17138 * We have something to send yet we cannot send. The 17139 * reason can be: 17140 * 17141 * 1. Zero send window: we need to do zero window probe. 17142 * 2. Zero cwnd: because of ECN, we need to "clock out 17143 * segments. 17144 * 3. SWS avoidance: receiver may have shrunk window, 17145 * reset our knowledge. 17146 * 17147 * Note that condition 2 can happen with either 1 or 17148 * 3. But 1 and 3 are exclusive. 17149 */ 17150 if (tcp->tcp_unsent != 0) { 17151 if (tcp->tcp_cwnd == 0) { 17152 /* 17153 * Set tcp_cwnd to 1 MSS so that a 17154 * new segment can be sent out. We 17155 * are "clocking out" new data when 17156 * the network is really congested. 17157 */ 17158 ASSERT(tcp->tcp_ecn_ok); 17159 tcp->tcp_cwnd = tcp->tcp_mss; 17160 } 17161 if (tcp->tcp_swnd == 0) { 17162 /* Extend window for zero window probe */ 17163 tcp->tcp_swnd++; 17164 tcp->tcp_zero_win_probe = B_TRUE; 17165 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17166 } else { 17167 /* 17168 * Handle timeout from sender SWS avoidance. 17169 * Reset our knowledge of the max send window 17170 * since the receiver might have reduced its 17171 * receive buffer. Avoid setting tcp_max_swnd 17172 * to one since that will essentially disable 17173 * the SWS checks. 17174 * 17175 * Note that since we don't have a SWS 17176 * state variable, if the timeout is set 17177 * for ECN but not for SWS, this 17178 * code will also be executed. This is 17179 * fine as tcp_max_swnd is updated 17180 * constantly and it will not affect 17181 * anything. 17182 */ 17183 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17184 } 17185 tcp_wput_data(tcp, NULL, B_FALSE); 17186 return; 17187 } 17188 /* Is there a FIN that needs to be to re retransmitted? */ 17189 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17190 !tcp->tcp_fin_acked) 17191 break; 17192 /* Nothing to do, return without restarting timer. */ 17193 TCP_STAT(tcps, tcp_timer_fire_miss); 17194 return; 17195 case TCPS_FIN_WAIT_2: 17196 /* 17197 * User closed the TCP endpoint and peer ACK'ed our FIN. 17198 * We waited some time for for peer's FIN, but it hasn't 17199 * arrived. We flush the connection now to avoid 17200 * case where the peer has rebooted. 17201 */ 17202 if (TCP_IS_DETACHED(tcp)) { 17203 (void) tcp_clean_death(tcp, 0, 23); 17204 } else { 17205 TCP_TIMER_RESTART(tcp, 17206 tcps->tcps_fin_wait_2_flush_interval); 17207 } 17208 return; 17209 case TCPS_TIME_WAIT: 17210 (void) tcp_clean_death(tcp, 0, 24); 17211 return; 17212 default: 17213 if (tcp->tcp_debug) { 17214 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17215 "tcp_timer: strange state (%d) %s", 17216 tcp->tcp_state, tcp_display(tcp, NULL, 17217 DISP_PORT_ONLY)); 17218 } 17219 return; 17220 } 17221 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17222 /* 17223 * For zero window probe, we need to send indefinitely, 17224 * unless we have not heard from the other side for some 17225 * time... 17226 */ 17227 if ((tcp->tcp_zero_win_probe == 0) || 17228 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17229 second_threshold)) { 17230 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17231 /* 17232 * If TCP is in SYN_RCVD state, send back a 17233 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17234 * should be zero in TCPS_SYN_RCVD state. 17235 */ 17236 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17237 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17238 "in SYN_RCVD", 17239 tcp, tcp->tcp_snxt, 17240 tcp->tcp_rnxt, TH_RST | TH_ACK); 17241 } 17242 (void) tcp_clean_death(tcp, 17243 tcp->tcp_client_errno ? 17244 tcp->tcp_client_errno : ETIMEDOUT, 25); 17245 return; 17246 } else { 17247 /* 17248 * Set tcp_ms_we_have_waited to second_threshold 17249 * so that in next timeout, we will do the above 17250 * check (lbolt - tcp_last_recv_time). This is 17251 * also to avoid overflow. 17252 * 17253 * We don't need to decrement tcp_timer_backoff 17254 * to avoid overflow because it will be decremented 17255 * later if new timeout value is greater than 17256 * tcp_rexmit_interval_max. In the case when 17257 * tcp_rexmit_interval_max is greater than 17258 * second_threshold, it means that we will wait 17259 * longer than second_threshold to send the next 17260 * window probe. 17261 */ 17262 tcp->tcp_ms_we_have_waited = second_threshold; 17263 } 17264 } else if (ms > first_threshold) { 17265 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17266 tcp->tcp_xmit_head != NULL) { 17267 tcp->tcp_xmit_head = 17268 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17269 } 17270 /* 17271 * We have been retransmitting for too long... The RTT 17272 * we calculated is probably incorrect. Reinitialize it. 17273 * Need to compensate for 0 tcp_rtt_sa. Reset 17274 * tcp_rtt_update so that we won't accidentally cache a 17275 * bad value. But only do this if this is not a zero 17276 * window probe. 17277 */ 17278 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17279 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17280 (tcp->tcp_rtt_sa >> 5); 17281 tcp->tcp_rtt_sa = 0; 17282 tcp_ip_notify(tcp); 17283 tcp->tcp_rtt_update = 0; 17284 } 17285 } 17286 tcp->tcp_timer_backoff++; 17287 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17288 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17289 tcps->tcps_rexmit_interval_min) { 17290 /* 17291 * This means the original RTO is tcp_rexmit_interval_min. 17292 * So we will use tcp_rexmit_interval_min as the RTO value 17293 * and do the backoff. 17294 */ 17295 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17296 } else { 17297 ms <<= tcp->tcp_timer_backoff; 17298 } 17299 if (ms > tcps->tcps_rexmit_interval_max) { 17300 ms = tcps->tcps_rexmit_interval_max; 17301 /* 17302 * ms is at max, decrement tcp_timer_backoff to avoid 17303 * overflow. 17304 */ 17305 tcp->tcp_timer_backoff--; 17306 } 17307 tcp->tcp_ms_we_have_waited += ms; 17308 if (tcp->tcp_zero_win_probe == 0) { 17309 tcp->tcp_rto = ms; 17310 } 17311 TCP_TIMER_RESTART(tcp, ms); 17312 /* 17313 * This is after a timeout and tcp_rto is backed off. Set 17314 * tcp_set_timer to 1 so that next time RTO is updated, we will 17315 * restart the timer with a correct value. 17316 */ 17317 tcp->tcp_set_timer = 1; 17318 mss = tcp->tcp_snxt - tcp->tcp_suna; 17319 if (mss > tcp->tcp_mss) 17320 mss = tcp->tcp_mss; 17321 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17322 mss = tcp->tcp_swnd; 17323 17324 if ((mp = tcp->tcp_xmit_head) != NULL) 17325 mp->b_prev = (mblk_t *)lbolt; 17326 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17327 B_TRUE); 17328 17329 /* 17330 * When slow start after retransmission begins, start with 17331 * this seq no. tcp_rexmit_max marks the end of special slow 17332 * start phase. tcp_snd_burst controls how many segments 17333 * can be sent because of an ack. 17334 */ 17335 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17336 tcp->tcp_snd_burst = TCP_CWND_SS; 17337 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17338 (tcp->tcp_unsent == 0)) { 17339 tcp->tcp_rexmit_max = tcp->tcp_fss; 17340 } else { 17341 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17342 } 17343 tcp->tcp_rexmit = B_TRUE; 17344 tcp->tcp_dupack_cnt = 0; 17345 17346 /* 17347 * Remove all rexmit SACK blk to start from fresh. 17348 */ 17349 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17350 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17351 tcp->tcp_num_notsack_blk = 0; 17352 tcp->tcp_cnt_notsack_list = 0; 17353 } 17354 if (mp == NULL) { 17355 return; 17356 } 17357 /* Attach credentials to retransmitted initial SYNs. */ 17358 if (tcp->tcp_state == TCPS_SYN_SENT) { 17359 mblk_setcred(mp, tcp->tcp_cred); 17360 DB_CPID(mp) = tcp->tcp_cpid; 17361 } 17362 17363 tcp->tcp_csuna = tcp->tcp_snxt; 17364 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17365 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17366 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 17367 tcp_send_data(tcp, tcp->tcp_wq, mp); 17368 17369 } 17370 17371 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17372 static void 17373 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17374 { 17375 conn_t *connp; 17376 17377 switch (tcp->tcp_state) { 17378 case TCPS_BOUND: 17379 case TCPS_LISTEN: 17380 break; 17381 default: 17382 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17383 return; 17384 } 17385 17386 /* 17387 * Need to clean up all the eagers since after the unbind, segments 17388 * will no longer be delivered to this listener stream. 17389 */ 17390 mutex_enter(&tcp->tcp_eager_lock); 17391 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17392 tcp_eager_cleanup(tcp, 0); 17393 } 17394 mutex_exit(&tcp->tcp_eager_lock); 17395 17396 if (tcp->tcp_ipversion == IPV4_VERSION) { 17397 tcp->tcp_ipha->ipha_src = 0; 17398 } else { 17399 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17400 } 17401 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17402 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17403 tcp_bind_hash_remove(tcp); 17404 tcp->tcp_state = TCPS_IDLE; 17405 tcp->tcp_mdt = B_FALSE; 17406 /* Send M_FLUSH according to TPI */ 17407 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17408 connp = tcp->tcp_connp; 17409 connp->conn_mdt_ok = B_FALSE; 17410 ipcl_hash_remove(connp); 17411 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17412 mp = mi_tpi_ok_ack_alloc(mp); 17413 putnext(tcp->tcp_rq, mp); 17414 } 17415 17416 /* 17417 * Don't let port fall into the privileged range. 17418 * Since the extra privileged ports can be arbitrary we also 17419 * ensure that we exclude those from consideration. 17420 * tcp_g_epriv_ports is not sorted thus we loop over it until 17421 * there are no changes. 17422 * 17423 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17424 * but instead the code relies on: 17425 * - the fact that the address of the array and its size never changes 17426 * - the atomic assignment of the elements of the array 17427 * 17428 * Returns 0 if there are no more ports available. 17429 * 17430 * TS note: skip multilevel ports. 17431 */ 17432 static in_port_t 17433 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17434 { 17435 int i; 17436 boolean_t restart = B_FALSE; 17437 tcp_stack_t *tcps = tcp->tcp_tcps; 17438 17439 if (random && tcp_random_anon_port != 0) { 17440 (void) random_get_pseudo_bytes((uint8_t *)&port, 17441 sizeof (in_port_t)); 17442 /* 17443 * Unless changed by a sys admin, the smallest anon port 17444 * is 32768 and the largest anon port is 65535. It is 17445 * very likely (50%) for the random port to be smaller 17446 * than the smallest anon port. When that happens, 17447 * add port % (anon port range) to the smallest anon 17448 * port to get the random port. It should fall into the 17449 * valid anon port range. 17450 */ 17451 if (port < tcps->tcps_smallest_anon_port) { 17452 port = tcps->tcps_smallest_anon_port + 17453 port % (tcps->tcps_largest_anon_port - 17454 tcps->tcps_smallest_anon_port); 17455 } 17456 } 17457 17458 retry: 17459 if (port < tcps->tcps_smallest_anon_port) 17460 port = (in_port_t)tcps->tcps_smallest_anon_port; 17461 17462 if (port > tcps->tcps_largest_anon_port) { 17463 if (restart) 17464 return (0); 17465 restart = B_TRUE; 17466 port = (in_port_t)tcps->tcps_smallest_anon_port; 17467 } 17468 17469 if (port < tcps->tcps_smallest_nonpriv_port) 17470 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17471 17472 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17473 if (port == tcps->tcps_g_epriv_ports[i]) { 17474 port++; 17475 /* 17476 * Make sure whether the port is in the 17477 * valid range. 17478 */ 17479 goto retry; 17480 } 17481 } 17482 if (is_system_labeled() && 17483 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17484 IPPROTO_TCP, B_TRUE)) != 0) { 17485 port = i; 17486 goto retry; 17487 } 17488 return (port); 17489 } 17490 17491 /* 17492 * Return the next anonymous port in the privileged port range for 17493 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17494 * downwards. This is the same behavior as documented in the userland 17495 * library call rresvport(3N). 17496 * 17497 * TS note: skip multilevel ports. 17498 */ 17499 static in_port_t 17500 tcp_get_next_priv_port(const tcp_t *tcp) 17501 { 17502 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17503 in_port_t nextport; 17504 boolean_t restart = B_FALSE; 17505 tcp_stack_t *tcps = tcp->tcp_tcps; 17506 retry: 17507 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17508 next_priv_port >= IPPORT_RESERVED) { 17509 next_priv_port = IPPORT_RESERVED - 1; 17510 if (restart) 17511 return (0); 17512 restart = B_TRUE; 17513 } 17514 if (is_system_labeled() && 17515 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17516 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17517 next_priv_port = nextport; 17518 goto retry; 17519 } 17520 return (next_priv_port--); 17521 } 17522 17523 /* The write side r/w procedure. */ 17524 17525 #if CCS_STATS 17526 struct { 17527 struct { 17528 int64_t count, bytes; 17529 } tot, hit; 17530 } wrw_stats; 17531 #endif 17532 17533 /* 17534 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17535 * messages. 17536 */ 17537 /* ARGSUSED */ 17538 static void 17539 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17540 { 17541 conn_t *connp = (conn_t *)arg; 17542 tcp_t *tcp = connp->conn_tcp; 17543 queue_t *q = tcp->tcp_wq; 17544 17545 ASSERT(DB_TYPE(mp) != M_IOCTL); 17546 /* 17547 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17548 * Once the close starts, streamhead and sockfs will not let any data 17549 * packets come down (close ensures that there are no threads using the 17550 * queue and no new threads will come down) but since qprocsoff() 17551 * hasn't happened yet, a M_FLUSH or some non data message might 17552 * get reflected back (in response to our own FLUSHRW) and get 17553 * processed after tcp_close() is done. The conn would still be valid 17554 * because a ref would have added but we need to check the state 17555 * before actually processing the packet. 17556 */ 17557 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17558 freemsg(mp); 17559 return; 17560 } 17561 17562 switch (DB_TYPE(mp)) { 17563 case M_IOCDATA: 17564 tcp_wput_iocdata(tcp, mp); 17565 break; 17566 case M_FLUSH: 17567 tcp_wput_flush(tcp, mp); 17568 break; 17569 default: 17570 CALL_IP_WPUT(connp, q, mp); 17571 break; 17572 } 17573 } 17574 17575 /* 17576 * The TCP fast path write put procedure. 17577 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17578 */ 17579 /* ARGSUSED */ 17580 void 17581 tcp_output(void *arg, mblk_t *mp, void *arg2) 17582 { 17583 int len; 17584 int hdrlen; 17585 int plen; 17586 mblk_t *mp1; 17587 uchar_t *rptr; 17588 uint32_t snxt; 17589 tcph_t *tcph; 17590 struct datab *db; 17591 uint32_t suna; 17592 uint32_t mss; 17593 ipaddr_t *dst; 17594 ipaddr_t *src; 17595 uint32_t sum; 17596 int usable; 17597 conn_t *connp = (conn_t *)arg; 17598 tcp_t *tcp = connp->conn_tcp; 17599 uint32_t msize; 17600 tcp_stack_t *tcps = tcp->tcp_tcps; 17601 17602 /* 17603 * Try and ASSERT the minimum possible references on the 17604 * conn early enough. Since we are executing on write side, 17605 * the connection is obviously not detached and that means 17606 * there is a ref each for TCP and IP. Since we are behind 17607 * the squeue, the minimum references needed are 3. If the 17608 * conn is in classifier hash list, there should be an 17609 * extra ref for that (we check both the possibilities). 17610 */ 17611 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17612 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17613 17614 ASSERT(DB_TYPE(mp) == M_DATA); 17615 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17616 17617 mutex_enter(&tcp->tcp_non_sq_lock); 17618 tcp->tcp_squeue_bytes -= msize; 17619 mutex_exit(&tcp->tcp_non_sq_lock); 17620 17621 /* Bypass tcp protocol for fused tcp loopback */ 17622 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17623 return; 17624 17625 mss = tcp->tcp_mss; 17626 if (tcp->tcp_xmit_zc_clean) 17627 mp = tcp_zcopy_backoff(tcp, mp, 0); 17628 17629 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17630 len = (int)(mp->b_wptr - mp->b_rptr); 17631 17632 /* 17633 * Criteria for fast path: 17634 * 17635 * 1. no unsent data 17636 * 2. single mblk in request 17637 * 3. connection established 17638 * 4. data in mblk 17639 * 5. len <= mss 17640 * 6. no tcp_valid bits 17641 */ 17642 if ((tcp->tcp_unsent != 0) || 17643 (tcp->tcp_cork) || 17644 (mp->b_cont != NULL) || 17645 (tcp->tcp_state != TCPS_ESTABLISHED) || 17646 (len == 0) || 17647 (len > mss) || 17648 (tcp->tcp_valid_bits != 0)) { 17649 tcp_wput_data(tcp, mp, B_FALSE); 17650 return; 17651 } 17652 17653 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17654 ASSERT(tcp->tcp_fin_sent == 0); 17655 17656 /* queue new packet onto retransmission queue */ 17657 if (tcp->tcp_xmit_head == NULL) { 17658 tcp->tcp_xmit_head = mp; 17659 } else { 17660 tcp->tcp_xmit_last->b_cont = mp; 17661 } 17662 tcp->tcp_xmit_last = mp; 17663 tcp->tcp_xmit_tail = mp; 17664 17665 /* find out how much we can send */ 17666 /* BEGIN CSTYLED */ 17667 /* 17668 * un-acked usable 17669 * |--------------|-----------------| 17670 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17671 */ 17672 /* END CSTYLED */ 17673 17674 /* start sending from tcp_snxt */ 17675 snxt = tcp->tcp_snxt; 17676 17677 /* 17678 * Check to see if this connection has been idled for some 17679 * time and no ACK is expected. If it is, we need to slow 17680 * start again to get back the connection's "self-clock" as 17681 * described in VJ's paper. 17682 * 17683 * Refer to the comment in tcp_mss_set() for the calculation 17684 * of tcp_cwnd after idle. 17685 */ 17686 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17687 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17688 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17689 } 17690 17691 usable = tcp->tcp_swnd; /* tcp window size */ 17692 if (usable > tcp->tcp_cwnd) 17693 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17694 usable -= snxt; /* subtract stuff already sent */ 17695 suna = tcp->tcp_suna; 17696 usable += suna; 17697 /* usable can be < 0 if the congestion window is smaller */ 17698 if (len > usable) { 17699 /* Can't send complete M_DATA in one shot */ 17700 goto slow; 17701 } 17702 17703 mutex_enter(&tcp->tcp_non_sq_lock); 17704 if (tcp->tcp_flow_stopped && 17705 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17706 tcp_clrqfull(tcp); 17707 } 17708 mutex_exit(&tcp->tcp_non_sq_lock); 17709 17710 /* 17711 * determine if anything to send (Nagle). 17712 * 17713 * 1. len < tcp_mss (i.e. small) 17714 * 2. unacknowledged data present 17715 * 3. len < nagle limit 17716 * 4. last packet sent < nagle limit (previous packet sent) 17717 */ 17718 if ((len < mss) && (snxt != suna) && 17719 (len < (int)tcp->tcp_naglim) && 17720 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17721 /* 17722 * This was the first unsent packet and normally 17723 * mss < xmit_hiwater so there is no need to worry 17724 * about flow control. The next packet will go 17725 * through the flow control check in tcp_wput_data(). 17726 */ 17727 /* leftover work from above */ 17728 tcp->tcp_unsent = len; 17729 tcp->tcp_xmit_tail_unsent = len; 17730 17731 return; 17732 } 17733 17734 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17735 17736 if (snxt == suna) { 17737 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17738 } 17739 17740 /* we have always sent something */ 17741 tcp->tcp_rack_cnt = 0; 17742 17743 tcp->tcp_snxt = snxt + len; 17744 tcp->tcp_rack = tcp->tcp_rnxt; 17745 17746 if ((mp1 = dupb(mp)) == 0) 17747 goto no_memory; 17748 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17749 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17750 17751 /* adjust tcp header information */ 17752 tcph = tcp->tcp_tcph; 17753 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17754 17755 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17756 sum = (sum >> 16) + (sum & 0xFFFF); 17757 U16_TO_ABE16(sum, tcph->th_sum); 17758 17759 U32_TO_ABE32(snxt, tcph->th_seq); 17760 17761 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17762 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17763 BUMP_LOCAL(tcp->tcp_obsegs); 17764 17765 /* Update the latest receive window size in TCP header. */ 17766 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17767 tcph->th_win); 17768 17769 tcp->tcp_last_sent_len = (ushort_t)len; 17770 17771 plen = len + tcp->tcp_hdr_len; 17772 17773 if (tcp->tcp_ipversion == IPV4_VERSION) { 17774 tcp->tcp_ipha->ipha_length = htons(plen); 17775 } else { 17776 tcp->tcp_ip6h->ip6_plen = htons(plen - 17777 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17778 } 17779 17780 /* see if we need to allocate a mblk for the headers */ 17781 hdrlen = tcp->tcp_hdr_len; 17782 rptr = mp1->b_rptr - hdrlen; 17783 db = mp1->b_datap; 17784 if ((db->db_ref != 2) || rptr < db->db_base || 17785 (!OK_32PTR(rptr))) { 17786 /* NOTE: we assume allocb returns an OK_32PTR */ 17787 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17788 tcps->tcps_wroff_xtra, BPRI_MED); 17789 if (!mp) { 17790 freemsg(mp1); 17791 goto no_memory; 17792 } 17793 mp->b_cont = mp1; 17794 mp1 = mp; 17795 /* Leave room for Link Level header */ 17796 /* hdrlen = tcp->tcp_hdr_len; */ 17797 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17798 mp1->b_wptr = &rptr[hdrlen]; 17799 } 17800 mp1->b_rptr = rptr; 17801 17802 /* Fill in the timestamp option. */ 17803 if (tcp->tcp_snd_ts_ok) { 17804 U32_TO_BE32((uint32_t)lbolt, 17805 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17806 U32_TO_BE32(tcp->tcp_ts_recent, 17807 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17808 } else { 17809 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17810 } 17811 17812 /* copy header into outgoing packet */ 17813 dst = (ipaddr_t *)rptr; 17814 src = (ipaddr_t *)tcp->tcp_iphc; 17815 dst[0] = src[0]; 17816 dst[1] = src[1]; 17817 dst[2] = src[2]; 17818 dst[3] = src[3]; 17819 dst[4] = src[4]; 17820 dst[5] = src[5]; 17821 dst[6] = src[6]; 17822 dst[7] = src[7]; 17823 dst[8] = src[8]; 17824 dst[9] = src[9]; 17825 if (hdrlen -= 40) { 17826 hdrlen >>= 2; 17827 dst += 10; 17828 src += 10; 17829 do { 17830 *dst++ = *src++; 17831 } while (--hdrlen); 17832 } 17833 17834 /* 17835 * Set the ECN info in the TCP header. Note that this 17836 * is not the template header. 17837 */ 17838 if (tcp->tcp_ecn_ok) { 17839 SET_ECT(tcp, rptr); 17840 17841 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17842 if (tcp->tcp_ecn_echo_on) 17843 tcph->th_flags[0] |= TH_ECE; 17844 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17845 tcph->th_flags[0] |= TH_CWR; 17846 tcp->tcp_ecn_cwr_sent = B_TRUE; 17847 } 17848 } 17849 17850 if (tcp->tcp_ip_forward_progress) { 17851 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17852 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17853 tcp->tcp_ip_forward_progress = B_FALSE; 17854 } 17855 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17856 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17857 return; 17858 17859 /* 17860 * If we ran out of memory, we pretend to have sent the packet 17861 * and that it was lost on the wire. 17862 */ 17863 no_memory: 17864 return; 17865 17866 slow: 17867 /* leftover work from above */ 17868 tcp->tcp_unsent = len; 17869 tcp->tcp_xmit_tail_unsent = len; 17870 tcp_wput_data(tcp, NULL, B_FALSE); 17871 } 17872 17873 /* 17874 * The function called through squeue to get behind eager's perimeter to 17875 * finish the accept processing. 17876 */ 17877 /* ARGSUSED */ 17878 void 17879 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17880 { 17881 conn_t *connp = (conn_t *)arg; 17882 tcp_t *tcp = connp->conn_tcp; 17883 queue_t *q = tcp->tcp_rq; 17884 mblk_t *mp1; 17885 mblk_t *stropt_mp = mp; 17886 struct stroptions *stropt; 17887 uint_t thwin; 17888 tcp_stack_t *tcps = tcp->tcp_tcps; 17889 17890 /* 17891 * Drop the eager's ref on the listener, that was placed when 17892 * this eager began life in tcp_conn_request. 17893 */ 17894 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17895 17896 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17897 /* 17898 * Someone blewoff the eager before we could finish 17899 * the accept. 17900 * 17901 * The only reason eager exists it because we put in 17902 * a ref on it when conn ind went up. We need to send 17903 * a disconnect indication up while the last reference 17904 * on the eager will be dropped by the squeue when we 17905 * return. 17906 */ 17907 ASSERT(tcp->tcp_listener == NULL); 17908 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17909 struct T_discon_ind *tdi; 17910 17911 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17912 /* 17913 * Let us reuse the incoming mblk to avoid memory 17914 * allocation failure problems. We know that the 17915 * size of the incoming mblk i.e. stroptions is greater 17916 * than sizeof T_discon_ind. So the reallocb below 17917 * can't fail. 17918 */ 17919 freemsg(mp->b_cont); 17920 mp->b_cont = NULL; 17921 ASSERT(DB_REF(mp) == 1); 17922 mp = reallocb(mp, sizeof (struct T_discon_ind), 17923 B_FALSE); 17924 ASSERT(mp != NULL); 17925 DB_TYPE(mp) = M_PROTO; 17926 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17927 tdi = (struct T_discon_ind *)mp->b_rptr; 17928 if (tcp->tcp_issocket) { 17929 tdi->DISCON_reason = ECONNREFUSED; 17930 tdi->SEQ_number = 0; 17931 } else { 17932 tdi->DISCON_reason = ENOPROTOOPT; 17933 tdi->SEQ_number = 17934 tcp->tcp_conn_req_seqnum; 17935 } 17936 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17937 putnext(q, mp); 17938 } else { 17939 freemsg(mp); 17940 } 17941 if (tcp->tcp_hard_binding) { 17942 tcp->tcp_hard_binding = B_FALSE; 17943 tcp->tcp_hard_bound = B_TRUE; 17944 } 17945 tcp->tcp_detached = B_FALSE; 17946 return; 17947 } 17948 17949 mp1 = stropt_mp->b_cont; 17950 stropt_mp->b_cont = NULL; 17951 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17952 stropt = (struct stroptions *)stropt_mp->b_rptr; 17953 17954 while (mp1 != NULL) { 17955 mp = mp1; 17956 mp1 = mp1->b_cont; 17957 mp->b_cont = NULL; 17958 tcp->tcp_drop_opt_ack_cnt++; 17959 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17960 } 17961 mp = NULL; 17962 17963 /* 17964 * For a loopback connection with tcp_direct_sockfs on, note that 17965 * we don't have to protect tcp_rcv_list yet because synchronous 17966 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17967 * possibly race with us. 17968 */ 17969 17970 /* 17971 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17972 * properly. This is the first time we know of the acceptor' 17973 * queue. So we do it here. 17974 */ 17975 if (tcp->tcp_rcv_list == NULL) { 17976 /* 17977 * Recv queue is empty, tcp_rwnd should not have changed. 17978 * That means it should be equal to the listener's tcp_rwnd. 17979 */ 17980 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17981 } else { 17982 #ifdef DEBUG 17983 uint_t cnt = 0; 17984 17985 mp1 = tcp->tcp_rcv_list; 17986 while ((mp = mp1) != NULL) { 17987 mp1 = mp->b_next; 17988 cnt += msgdsize(mp); 17989 } 17990 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17991 #endif 17992 /* There is some data, add them back to get the max. */ 17993 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17994 } 17995 17996 stropt->so_flags = SO_HIWAT; 17997 stropt->so_hiwat = MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 17998 17999 stropt->so_flags |= SO_MAXBLK; 18000 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 18001 18002 /* 18003 * This is the first time we run on the correct 18004 * queue after tcp_accept. So fix all the q parameters 18005 * here. 18006 */ 18007 /* Allocate room for SACK options if needed. */ 18008 stropt->so_flags |= SO_WROFF; 18009 if (tcp->tcp_fused) { 18010 ASSERT(tcp->tcp_loopback); 18011 ASSERT(tcp->tcp_loopback_peer != NULL); 18012 /* 18013 * For fused tcp loopback, set the stream head's write 18014 * offset value to zero since we won't be needing any room 18015 * for TCP/IP headers. This would also improve performance 18016 * since it would reduce the amount of work done by kmem. 18017 * Non-fused tcp loopback case is handled separately below. 18018 */ 18019 stropt->so_wroff = 0; 18020 /* 18021 * Record the stream head's high water mark for this endpoint; 18022 * this is used for flow-control purposes in tcp_fuse_output(). 18023 */ 18024 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 18025 /* 18026 * Update the peer's transmit parameters according to 18027 * our recently calculated high water mark value. 18028 */ 18029 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 18030 } else if (tcp->tcp_snd_sack_ok) { 18031 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 18032 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 18033 } else { 18034 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 18035 tcps->tcps_wroff_xtra); 18036 } 18037 18038 /* 18039 * If this is endpoint is handling SSL, then reserve extra 18040 * offset and space at the end. 18041 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18042 * overriding the previous setting. The extra cost of signing and 18043 * encrypting multiple MSS-size records (12 of them with Ethernet), 18044 * instead of a single contiguous one by the stream head 18045 * largely outweighs the statistical reduction of ACKs, when 18046 * applicable. The peer will also save on decyption and verification 18047 * costs. 18048 */ 18049 if (tcp->tcp_kssl_ctx != NULL) { 18050 stropt->so_wroff += SSL3_WROFFSET; 18051 18052 stropt->so_flags |= SO_TAIL; 18053 stropt->so_tail = SSL3_MAX_TAIL_LEN; 18054 18055 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 18056 } 18057 18058 /* Send the options up */ 18059 putnext(q, stropt_mp); 18060 18061 /* 18062 * Pass up any data and/or a fin that has been received. 18063 * 18064 * Adjust receive window in case it had decreased 18065 * (because there is data <=> tcp_rcv_list != NULL) 18066 * while the connection was detached. Note that 18067 * in case the eager was flow-controlled, w/o this 18068 * code, the rwnd may never open up again! 18069 */ 18070 if (tcp->tcp_rcv_list != NULL) { 18071 /* We drain directly in case of fused tcp loopback */ 18072 if (!tcp->tcp_fused && canputnext(q)) { 18073 tcp->tcp_rwnd = q->q_hiwat; 18074 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 18075 << tcp->tcp_rcv_ws; 18076 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 18077 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18078 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 18079 tcp_xmit_ctl(NULL, 18080 tcp, (tcp->tcp_swnd == 0) ? 18081 tcp->tcp_suna : tcp->tcp_snxt, 18082 tcp->tcp_rnxt, TH_ACK); 18083 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 18084 } 18085 18086 } 18087 (void) tcp_rcv_drain(q, tcp); 18088 18089 /* 18090 * For fused tcp loopback, back-enable peer endpoint 18091 * if it's currently flow-controlled. 18092 */ 18093 if (tcp->tcp_fused) { 18094 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18095 18096 ASSERT(peer_tcp != NULL); 18097 ASSERT(peer_tcp->tcp_fused); 18098 /* 18099 * In order to change the peer's tcp_flow_stopped, 18100 * we need to take locks for both end points. The 18101 * highest address is taken first. 18102 */ 18103 if (peer_tcp > tcp) { 18104 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18105 mutex_enter(&tcp->tcp_non_sq_lock); 18106 } else { 18107 mutex_enter(&tcp->tcp_non_sq_lock); 18108 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18109 } 18110 if (peer_tcp->tcp_flow_stopped) { 18111 tcp_clrqfull(peer_tcp); 18112 TCP_STAT(tcps, tcp_fusion_backenabled); 18113 } 18114 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18115 mutex_exit(&tcp->tcp_non_sq_lock); 18116 } 18117 } 18118 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18119 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18120 mp = mi_tpi_ordrel_ind(); 18121 if (mp) { 18122 tcp->tcp_ordrel_done = B_TRUE; 18123 putnext(q, mp); 18124 if (tcp->tcp_deferred_clean_death) { 18125 /* 18126 * tcp_clean_death was deferred 18127 * for T_ORDREL_IND - do it now 18128 */ 18129 (void) tcp_clean_death(tcp, 18130 tcp->tcp_client_errno, 21); 18131 tcp->tcp_deferred_clean_death = B_FALSE; 18132 } 18133 } else { 18134 /* 18135 * Run the orderly release in the 18136 * service routine. 18137 */ 18138 qenable(q); 18139 } 18140 } 18141 if (tcp->tcp_hard_binding) { 18142 tcp->tcp_hard_binding = B_FALSE; 18143 tcp->tcp_hard_bound = B_TRUE; 18144 } 18145 18146 tcp->tcp_detached = B_FALSE; 18147 18148 /* We can enable synchronous streams now */ 18149 if (tcp->tcp_fused) { 18150 tcp_fuse_syncstr_enable_pair(tcp); 18151 } 18152 18153 if (tcp->tcp_ka_enabled) { 18154 tcp->tcp_ka_last_intrvl = 0; 18155 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18156 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18157 } 18158 18159 /* 18160 * At this point, eager is fully established and will 18161 * have the following references - 18162 * 18163 * 2 references for connection to exist (1 for TCP and 1 for IP). 18164 * 1 reference for the squeue which will be dropped by the squeue as 18165 * soon as this function returns. 18166 * There will be 1 additonal reference for being in classifier 18167 * hash list provided something bad hasn't happened. 18168 */ 18169 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18170 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18171 } 18172 18173 /* 18174 * The function called through squeue to get behind listener's perimeter to 18175 * send a deffered conn_ind. 18176 */ 18177 /* ARGSUSED */ 18178 void 18179 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18180 { 18181 conn_t *connp = (conn_t *)arg; 18182 tcp_t *listener = connp->conn_tcp; 18183 18184 if (listener->tcp_state == TCPS_CLOSED || 18185 TCP_IS_DETACHED(listener)) { 18186 /* 18187 * If listener has closed, it would have caused a 18188 * a cleanup/blowoff to happen for the eager. 18189 */ 18190 tcp_t *tcp; 18191 struct T_conn_ind *conn_ind; 18192 18193 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18194 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18195 conn_ind->OPT_length); 18196 /* 18197 * We need to drop the ref on eager that was put 18198 * tcp_rput_data() before trying to send the conn_ind 18199 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18200 * and tcp_wput_accept() is sending this deferred conn_ind but 18201 * listener is closed so we drop the ref. 18202 */ 18203 CONN_DEC_REF(tcp->tcp_connp); 18204 freemsg(mp); 18205 return; 18206 } 18207 putnext(listener->tcp_rq, mp); 18208 } 18209 18210 18211 /* 18212 * This is the STREAMS entry point for T_CONN_RES coming down on 18213 * Acceptor STREAM when sockfs listener does accept processing. 18214 * Read the block comment on top of tcp_conn_request(). 18215 */ 18216 void 18217 tcp_wput_accept(queue_t *q, mblk_t *mp) 18218 { 18219 queue_t *rq = RD(q); 18220 struct T_conn_res *conn_res; 18221 tcp_t *eager; 18222 tcp_t *listener; 18223 struct T_ok_ack *ok; 18224 t_scalar_t PRIM_type; 18225 mblk_t *opt_mp; 18226 conn_t *econnp; 18227 18228 ASSERT(DB_TYPE(mp) == M_PROTO); 18229 18230 conn_res = (struct T_conn_res *)mp->b_rptr; 18231 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18232 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18233 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18234 if (mp != NULL) 18235 putnext(rq, mp); 18236 return; 18237 } 18238 switch (conn_res->PRIM_type) { 18239 case O_T_CONN_RES: 18240 case T_CONN_RES: 18241 /* 18242 * We pass up an err ack if allocb fails. This will 18243 * cause sockfs to issue a T_DISCON_REQ which will cause 18244 * tcp_eager_blowoff to be called. sockfs will then call 18245 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18246 * we need to do the allocb up here because we have to 18247 * make sure rq->q_qinfo->qi_qclose still points to the 18248 * correct function (tcpclose_accept) in case allocb 18249 * fails. 18250 */ 18251 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18252 if (opt_mp == NULL) { 18253 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18254 if (mp != NULL) 18255 putnext(rq, mp); 18256 return; 18257 } 18258 18259 bcopy(mp->b_rptr + conn_res->OPT_offset, 18260 &eager, conn_res->OPT_length); 18261 PRIM_type = conn_res->PRIM_type; 18262 mp->b_datap->db_type = M_PCPROTO; 18263 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18264 ok = (struct T_ok_ack *)mp->b_rptr; 18265 ok->PRIM_type = T_OK_ACK; 18266 ok->CORRECT_prim = PRIM_type; 18267 econnp = eager->tcp_connp; 18268 econnp->conn_dev = (dev_t)q->q_ptr; 18269 eager->tcp_rq = rq; 18270 eager->tcp_wq = q; 18271 rq->q_ptr = econnp; 18272 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18273 q->q_ptr = econnp; 18274 q->q_qinfo = &tcp_winit; 18275 listener = eager->tcp_listener; 18276 eager->tcp_issocket = B_TRUE; 18277 18278 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18279 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18280 ASSERT(econnp->conn_netstack == 18281 listener->tcp_connp->conn_netstack); 18282 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18283 18284 /* Put the ref for IP */ 18285 CONN_INC_REF(econnp); 18286 18287 /* 18288 * We should have minimum of 3 references on the conn 18289 * at this point. One each for TCP and IP and one for 18290 * the T_conn_ind that was sent up when the 3-way handshake 18291 * completed. In the normal case we would also have another 18292 * reference (making a total of 4) for the conn being in the 18293 * classifier hash list. However the eager could have received 18294 * an RST subsequently and tcp_closei_local could have removed 18295 * the eager from the classifier hash list, hence we can't 18296 * assert that reference. 18297 */ 18298 ASSERT(econnp->conn_ref >= 3); 18299 18300 /* 18301 * Send the new local address also up to sockfs. There 18302 * should already be enough space in the mp that came 18303 * down from soaccept(). 18304 */ 18305 if (eager->tcp_family == AF_INET) { 18306 sin_t *sin; 18307 18308 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18309 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18310 sin = (sin_t *)mp->b_wptr; 18311 mp->b_wptr += sizeof (sin_t); 18312 sin->sin_family = AF_INET; 18313 sin->sin_port = eager->tcp_lport; 18314 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18315 } else { 18316 sin6_t *sin6; 18317 18318 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18319 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18320 sin6 = (sin6_t *)mp->b_wptr; 18321 mp->b_wptr += sizeof (sin6_t); 18322 sin6->sin6_family = AF_INET6; 18323 sin6->sin6_port = eager->tcp_lport; 18324 if (eager->tcp_ipversion == IPV4_VERSION) { 18325 sin6->sin6_flowinfo = 0; 18326 IN6_IPADDR_TO_V4MAPPED( 18327 eager->tcp_ipha->ipha_src, 18328 &sin6->sin6_addr); 18329 } else { 18330 ASSERT(eager->tcp_ip6h != NULL); 18331 sin6->sin6_flowinfo = 18332 eager->tcp_ip6h->ip6_vcf & 18333 ~IPV6_VERS_AND_FLOW_MASK; 18334 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18335 } 18336 sin6->sin6_scope_id = 0; 18337 sin6->__sin6_src_id = 0; 18338 } 18339 18340 putnext(rq, mp); 18341 18342 opt_mp->b_datap->db_type = M_SETOPTS; 18343 opt_mp->b_wptr += sizeof (struct stroptions); 18344 18345 /* 18346 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18347 * from listener to acceptor. The message is chained on the 18348 * bind_mp which tcp_rput_other will send down to IP. 18349 */ 18350 if (listener->tcp_bound_if != 0) { 18351 /* allocate optmgmt req */ 18352 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18353 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18354 sizeof (int)); 18355 if (mp != NULL) 18356 linkb(opt_mp, mp); 18357 } 18358 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18359 uint_t on = 1; 18360 18361 /* allocate optmgmt req */ 18362 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18363 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18364 if (mp != NULL) 18365 linkb(opt_mp, mp); 18366 } 18367 18368 18369 mutex_enter(&listener->tcp_eager_lock); 18370 18371 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18372 18373 tcp_t *tail; 18374 tcp_t *tcp; 18375 mblk_t *mp1; 18376 18377 tcp = listener->tcp_eager_prev_q0; 18378 /* 18379 * listener->tcp_eager_prev_q0 points to the TAIL of the 18380 * deferred T_conn_ind queue. We need to get to the head 18381 * of the queue in order to send up T_conn_ind the same 18382 * order as how the 3WHS is completed. 18383 */ 18384 while (tcp != listener) { 18385 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18386 !tcp->tcp_kssl_pending) 18387 break; 18388 else 18389 tcp = tcp->tcp_eager_prev_q0; 18390 } 18391 /* None of the pending eagers can be sent up now */ 18392 if (tcp == listener) 18393 goto no_more_eagers; 18394 18395 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18396 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18397 /* Move from q0 to q */ 18398 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18399 listener->tcp_conn_req_cnt_q0--; 18400 listener->tcp_conn_req_cnt_q++; 18401 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18402 tcp->tcp_eager_prev_q0; 18403 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18404 tcp->tcp_eager_next_q0; 18405 tcp->tcp_eager_prev_q0 = NULL; 18406 tcp->tcp_eager_next_q0 = NULL; 18407 tcp->tcp_conn_def_q0 = B_FALSE; 18408 18409 /* Make sure the tcp isn't in the list of droppables */ 18410 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18411 tcp->tcp_eager_prev_drop_q0 == NULL); 18412 18413 /* 18414 * Insert at end of the queue because sockfs sends 18415 * down T_CONN_RES in chronological order. Leaving 18416 * the older conn indications at front of the queue 18417 * helps reducing search time. 18418 */ 18419 tail = listener->tcp_eager_last_q; 18420 if (tail != NULL) { 18421 tail->tcp_eager_next_q = tcp; 18422 } else { 18423 listener->tcp_eager_next_q = tcp; 18424 } 18425 listener->tcp_eager_last_q = tcp; 18426 tcp->tcp_eager_next_q = NULL; 18427 18428 /* Need to get inside the listener perimeter */ 18429 CONN_INC_REF(listener->tcp_connp); 18430 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18431 tcp_send_pending, listener->tcp_connp, 18432 SQTAG_TCP_SEND_PENDING); 18433 } 18434 no_more_eagers: 18435 tcp_eager_unlink(eager); 18436 mutex_exit(&listener->tcp_eager_lock); 18437 18438 /* 18439 * At this point, the eager is detached from the listener 18440 * but we still have an extra refs on eager (apart from the 18441 * usual tcp references). The ref was placed in tcp_rput_data 18442 * before sending the conn_ind in tcp_send_conn_ind. 18443 * The ref will be dropped in tcp_accept_finish(). 18444 */ 18445 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18446 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18447 return; 18448 default: 18449 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18450 if (mp != NULL) 18451 putnext(rq, mp); 18452 return; 18453 } 18454 } 18455 18456 void 18457 tcp_wput(queue_t *q, mblk_t *mp) 18458 { 18459 conn_t *connp = Q_TO_CONN(q); 18460 tcp_t *tcp; 18461 void (*output_proc)(); 18462 t_scalar_t type; 18463 uchar_t *rptr; 18464 struct iocblk *iocp; 18465 uint32_t msize; 18466 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18467 18468 ASSERT(connp->conn_ref >= 2); 18469 18470 switch (DB_TYPE(mp)) { 18471 case M_DATA: 18472 tcp = connp->conn_tcp; 18473 ASSERT(tcp != NULL); 18474 18475 msize = msgdsize(mp); 18476 18477 mutex_enter(&tcp->tcp_non_sq_lock); 18478 tcp->tcp_squeue_bytes += msize; 18479 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18480 tcp_setqfull(tcp); 18481 } 18482 mutex_exit(&tcp->tcp_non_sq_lock); 18483 18484 CONN_INC_REF(connp); 18485 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18486 tcp_output, connp, SQTAG_TCP_OUTPUT); 18487 return; 18488 case M_PROTO: 18489 case M_PCPROTO: 18490 /* 18491 * if it is a snmp message, don't get behind the squeue 18492 */ 18493 tcp = connp->conn_tcp; 18494 rptr = mp->b_rptr; 18495 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18496 type = ((union T_primitives *)rptr)->type; 18497 } else { 18498 if (tcp->tcp_debug) { 18499 (void) strlog(TCP_MOD_ID, 0, 1, 18500 SL_ERROR|SL_TRACE, 18501 "tcp_wput_proto, dropping one..."); 18502 } 18503 freemsg(mp); 18504 return; 18505 } 18506 if (type == T_SVR4_OPTMGMT_REQ) { 18507 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18508 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18509 cr)) { 18510 /* 18511 * This was a SNMP request 18512 */ 18513 return; 18514 } else { 18515 output_proc = tcp_wput_proto; 18516 } 18517 } else { 18518 output_proc = tcp_wput_proto; 18519 } 18520 break; 18521 case M_IOCTL: 18522 /* 18523 * Most ioctls can be processed right away without going via 18524 * squeues - process them right here. Those that do require 18525 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18526 * are processed by tcp_wput_ioctl(). 18527 */ 18528 iocp = (struct iocblk *)mp->b_rptr; 18529 tcp = connp->conn_tcp; 18530 18531 switch (iocp->ioc_cmd) { 18532 case TCP_IOC_ABORT_CONN: 18533 tcp_ioctl_abort_conn(q, mp); 18534 return; 18535 case TI_GETPEERNAME: 18536 if (tcp->tcp_state < TCPS_SYN_RCVD) { 18537 iocp->ioc_error = ENOTCONN; 18538 iocp->ioc_count = 0; 18539 mp->b_datap->db_type = M_IOCACK; 18540 qreply(q, mp); 18541 return; 18542 } 18543 /* FALLTHRU */ 18544 case TI_GETMYNAME: 18545 mi_copyin(q, mp, NULL, 18546 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18547 return; 18548 case ND_SET: 18549 /* nd_getset does the necessary checks */ 18550 case ND_GET: 18551 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18552 CALL_IP_WPUT(connp, q, mp); 18553 return; 18554 } 18555 qreply(q, mp); 18556 return; 18557 case TCP_IOC_DEFAULT_Q: 18558 /* 18559 * Wants to be the default wq. Check the credentials 18560 * first, the rest is executed via squeue. 18561 */ 18562 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18563 iocp->ioc_error = EPERM; 18564 iocp->ioc_count = 0; 18565 mp->b_datap->db_type = M_IOCACK; 18566 qreply(q, mp); 18567 return; 18568 } 18569 output_proc = tcp_wput_ioctl; 18570 break; 18571 default: 18572 output_proc = tcp_wput_ioctl; 18573 break; 18574 } 18575 break; 18576 default: 18577 output_proc = tcp_wput_nondata; 18578 break; 18579 } 18580 18581 CONN_INC_REF(connp); 18582 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18583 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18584 } 18585 18586 /* 18587 * Initial STREAMS write side put() procedure for sockets. It tries to 18588 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18589 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18590 * are handled by tcp_wput() as usual. 18591 * 18592 * All further messages will also be handled by tcp_wput() because we cannot 18593 * be sure that the above short cut is safe later. 18594 */ 18595 static void 18596 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18597 { 18598 conn_t *connp = Q_TO_CONN(wq); 18599 tcp_t *tcp = connp->conn_tcp; 18600 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18601 18602 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18603 wq->q_qinfo = &tcp_winit; 18604 18605 ASSERT(IPCL_IS_TCP(connp)); 18606 ASSERT(TCP_IS_SOCKET(tcp)); 18607 18608 if (DB_TYPE(mp) == M_PCPROTO && 18609 MBLKL(mp) == sizeof (struct T_capability_req) && 18610 car->PRIM_type == T_CAPABILITY_REQ) { 18611 tcp_capability_req(tcp, mp); 18612 return; 18613 } 18614 18615 tcp_wput(wq, mp); 18616 } 18617 18618 static boolean_t 18619 tcp_zcopy_check(tcp_t *tcp) 18620 { 18621 conn_t *connp = tcp->tcp_connp; 18622 ire_t *ire; 18623 boolean_t zc_enabled = B_FALSE; 18624 tcp_stack_t *tcps = tcp->tcp_tcps; 18625 18626 if (do_tcpzcopy == 2) 18627 zc_enabled = B_TRUE; 18628 else if (tcp->tcp_ipversion == IPV4_VERSION && 18629 IPCL_IS_CONNECTED(connp) && 18630 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18631 connp->conn_dontroute == 0 && 18632 !connp->conn_nexthop_set && 18633 connp->conn_outgoing_ill == NULL && 18634 connp->conn_nofailover_ill == NULL && 18635 do_tcpzcopy == 1) { 18636 /* 18637 * the checks above closely resemble the fast path checks 18638 * in tcp_send_data(). 18639 */ 18640 mutex_enter(&connp->conn_lock); 18641 ire = connp->conn_ire_cache; 18642 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18643 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18644 IRE_REFHOLD(ire); 18645 if (ire->ire_stq != NULL) { 18646 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18647 18648 zc_enabled = ill && (ill->ill_capabilities & 18649 ILL_CAPAB_ZEROCOPY) && 18650 (ill->ill_zerocopy_capab-> 18651 ill_zerocopy_flags != 0); 18652 } 18653 IRE_REFRELE(ire); 18654 } 18655 mutex_exit(&connp->conn_lock); 18656 } 18657 tcp->tcp_snd_zcopy_on = zc_enabled; 18658 if (!TCP_IS_DETACHED(tcp)) { 18659 if (zc_enabled) { 18660 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 18661 TCP_STAT(tcps, tcp_zcopy_on); 18662 } else { 18663 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18664 TCP_STAT(tcps, tcp_zcopy_off); 18665 } 18666 } 18667 return (zc_enabled); 18668 } 18669 18670 static mblk_t * 18671 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18672 { 18673 tcp_stack_t *tcps = tcp->tcp_tcps; 18674 18675 if (do_tcpzcopy == 2) 18676 return (bp); 18677 else if (tcp->tcp_snd_zcopy_on) { 18678 tcp->tcp_snd_zcopy_on = B_FALSE; 18679 if (!TCP_IS_DETACHED(tcp)) { 18680 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18681 TCP_STAT(tcps, tcp_zcopy_disable); 18682 } 18683 } 18684 return (tcp_zcopy_backoff(tcp, bp, 0)); 18685 } 18686 18687 /* 18688 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18689 * the original desballoca'ed segmapped mblk. 18690 */ 18691 static mblk_t * 18692 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18693 { 18694 mblk_t *head, *tail, *nbp; 18695 tcp_stack_t *tcps = tcp->tcp_tcps; 18696 18697 if (IS_VMLOANED_MBLK(bp)) { 18698 TCP_STAT(tcps, tcp_zcopy_backoff); 18699 if ((head = copyb(bp)) == NULL) { 18700 /* fail to backoff; leave it for the next backoff */ 18701 tcp->tcp_xmit_zc_clean = B_FALSE; 18702 return (bp); 18703 } 18704 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18705 if (fix_xmitlist) 18706 tcp_zcopy_notify(tcp); 18707 else 18708 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18709 } 18710 nbp = bp->b_cont; 18711 if (fix_xmitlist) { 18712 head->b_prev = bp->b_prev; 18713 head->b_next = bp->b_next; 18714 if (tcp->tcp_xmit_tail == bp) 18715 tcp->tcp_xmit_tail = head; 18716 } 18717 bp->b_next = NULL; 18718 bp->b_prev = NULL; 18719 freeb(bp); 18720 } else { 18721 head = bp; 18722 nbp = bp->b_cont; 18723 } 18724 tail = head; 18725 while (nbp) { 18726 if (IS_VMLOANED_MBLK(nbp)) { 18727 TCP_STAT(tcps, tcp_zcopy_backoff); 18728 if ((tail->b_cont = copyb(nbp)) == NULL) { 18729 tcp->tcp_xmit_zc_clean = B_FALSE; 18730 tail->b_cont = nbp; 18731 return (head); 18732 } 18733 tail = tail->b_cont; 18734 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18735 if (fix_xmitlist) 18736 tcp_zcopy_notify(tcp); 18737 else 18738 tail->b_datap->db_struioflag |= 18739 STRUIO_ZCNOTIFY; 18740 } 18741 bp = nbp; 18742 nbp = nbp->b_cont; 18743 if (fix_xmitlist) { 18744 tail->b_prev = bp->b_prev; 18745 tail->b_next = bp->b_next; 18746 if (tcp->tcp_xmit_tail == bp) 18747 tcp->tcp_xmit_tail = tail; 18748 } 18749 bp->b_next = NULL; 18750 bp->b_prev = NULL; 18751 freeb(bp); 18752 } else { 18753 tail->b_cont = nbp; 18754 tail = nbp; 18755 nbp = nbp->b_cont; 18756 } 18757 } 18758 if (fix_xmitlist) { 18759 tcp->tcp_xmit_last = tail; 18760 tcp->tcp_xmit_zc_clean = B_TRUE; 18761 } 18762 return (head); 18763 } 18764 18765 static void 18766 tcp_zcopy_notify(tcp_t *tcp) 18767 { 18768 struct stdata *stp; 18769 18770 if (tcp->tcp_detached) 18771 return; 18772 stp = STREAM(tcp->tcp_rq); 18773 mutex_enter(&stp->sd_lock); 18774 stp->sd_flag |= STZCNOTIFY; 18775 cv_broadcast(&stp->sd_zcopy_wait); 18776 mutex_exit(&stp->sd_lock); 18777 } 18778 18779 static boolean_t 18780 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18781 { 18782 ire_t *ire; 18783 conn_t *connp = tcp->tcp_connp; 18784 tcp_stack_t *tcps = tcp->tcp_tcps; 18785 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18786 18787 mutex_enter(&connp->conn_lock); 18788 ire = connp->conn_ire_cache; 18789 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18790 18791 if ((ire != NULL) && 18792 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18793 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18794 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18795 IRE_REFHOLD(ire); 18796 mutex_exit(&connp->conn_lock); 18797 } else { 18798 boolean_t cached = B_FALSE; 18799 ts_label_t *tsl; 18800 18801 /* force a recheck later on */ 18802 tcp->tcp_ire_ill_check_done = B_FALSE; 18803 18804 TCP_DBGSTAT(tcps, tcp_ire_null1); 18805 connp->conn_ire_cache = NULL; 18806 mutex_exit(&connp->conn_lock); 18807 18808 if (ire != NULL) 18809 IRE_REFRELE_NOTR(ire); 18810 18811 tsl = crgetlabel(CONN_CRED(connp)); 18812 ire = (dst ? 18813 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18814 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18815 connp->conn_zoneid, tsl, ipst)); 18816 18817 if (ire == NULL) { 18818 TCP_STAT(tcps, tcp_ire_null); 18819 return (B_FALSE); 18820 } 18821 18822 IRE_REFHOLD_NOTR(ire); 18823 /* 18824 * Since we are inside the squeue, there cannot be another 18825 * thread in TCP trying to set the conn_ire_cache now. The 18826 * check for IRE_MARK_CONDEMNED ensures that an interface 18827 * unplumb thread has not yet started cleaning up the conns. 18828 * Hence we don't need to grab the conn lock. 18829 */ 18830 if (CONN_CACHE_IRE(connp)) { 18831 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18832 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18833 TCP_CHECK_IREINFO(tcp, ire); 18834 connp->conn_ire_cache = ire; 18835 cached = B_TRUE; 18836 } 18837 rw_exit(&ire->ire_bucket->irb_lock); 18838 } 18839 18840 /* 18841 * We can continue to use the ire but since it was 18842 * not cached, we should drop the extra reference. 18843 */ 18844 if (!cached) 18845 IRE_REFRELE_NOTR(ire); 18846 18847 /* 18848 * Rampart note: no need to select a new label here, since 18849 * labels are not allowed to change during the life of a TCP 18850 * connection. 18851 */ 18852 } 18853 18854 *irep = ire; 18855 18856 return (B_TRUE); 18857 } 18858 18859 /* 18860 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18861 * 18862 * 0 = success; 18863 * 1 = failed to find ire and ill. 18864 */ 18865 static boolean_t 18866 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18867 { 18868 ipha_t *ipha; 18869 ipaddr_t dst; 18870 ire_t *ire; 18871 ill_t *ill; 18872 conn_t *connp = tcp->tcp_connp; 18873 mblk_t *ire_fp_mp; 18874 tcp_stack_t *tcps = tcp->tcp_tcps; 18875 18876 if (mp != NULL) 18877 ipha = (ipha_t *)mp->b_rptr; 18878 else 18879 ipha = tcp->tcp_ipha; 18880 dst = ipha->ipha_dst; 18881 18882 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18883 return (B_FALSE); 18884 18885 if ((ire->ire_flags & RTF_MULTIRT) || 18886 (ire->ire_stq == NULL) || 18887 (ire->ire_nce == NULL) || 18888 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18889 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18890 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18891 TCP_STAT(tcps, tcp_ip_ire_send); 18892 IRE_REFRELE(ire); 18893 return (B_FALSE); 18894 } 18895 18896 ill = ire_to_ill(ire); 18897 if (connp->conn_outgoing_ill != NULL) { 18898 ill_t *conn_outgoing_ill = NULL; 18899 /* 18900 * Choose a good ill in the group to send the packets on. 18901 */ 18902 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18903 ill = ire_to_ill(ire); 18904 } 18905 ASSERT(ill != NULL); 18906 18907 if (!tcp->tcp_ire_ill_check_done) { 18908 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18909 tcp->tcp_ire_ill_check_done = B_TRUE; 18910 } 18911 18912 *irep = ire; 18913 *illp = ill; 18914 18915 return (B_TRUE); 18916 } 18917 18918 static void 18919 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18920 { 18921 ipha_t *ipha; 18922 ipaddr_t src; 18923 ipaddr_t dst; 18924 uint32_t cksum; 18925 ire_t *ire; 18926 uint16_t *up; 18927 ill_t *ill; 18928 conn_t *connp = tcp->tcp_connp; 18929 uint32_t hcksum_txflags = 0; 18930 mblk_t *ire_fp_mp; 18931 uint_t ire_fp_mp_len; 18932 tcp_stack_t *tcps = tcp->tcp_tcps; 18933 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18934 18935 ASSERT(DB_TYPE(mp) == M_DATA); 18936 18937 if (DB_CRED(mp) == NULL) 18938 mblk_setcred(mp, CONN_CRED(connp)); 18939 18940 ipha = (ipha_t *)mp->b_rptr; 18941 src = ipha->ipha_src; 18942 dst = ipha->ipha_dst; 18943 18944 /* 18945 * Drop off fast path for IPv6 and also if options are present or 18946 * we need to resolve a TS label. 18947 */ 18948 if (tcp->tcp_ipversion != IPV4_VERSION || 18949 !IPCL_IS_CONNECTED(connp) || 18950 !CONN_IS_LSO_MD_FASTPATH(connp) || 18951 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18952 !connp->conn_ulp_labeled || 18953 ipha->ipha_ident == IP_HDR_INCLUDED || 18954 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18955 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18956 if (tcp->tcp_snd_zcopy_aware) 18957 mp = tcp_zcopy_disable(tcp, mp); 18958 TCP_STAT(tcps, tcp_ip_send); 18959 CALL_IP_WPUT(connp, q, mp); 18960 return; 18961 } 18962 18963 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18964 if (tcp->tcp_snd_zcopy_aware) 18965 mp = tcp_zcopy_backoff(tcp, mp, 0); 18966 CALL_IP_WPUT(connp, q, mp); 18967 return; 18968 } 18969 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18970 ire_fp_mp_len = MBLKL(ire_fp_mp); 18971 18972 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18973 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18974 #ifndef _BIG_ENDIAN 18975 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18976 #endif 18977 18978 /* 18979 * Check to see if we need to re-enable LSO/MDT for this connection 18980 * because it was previously disabled due to changes in the ill; 18981 * note that by doing it here, this re-enabling only applies when 18982 * the packet is not dispatched through CALL_IP_WPUT(). 18983 * 18984 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18985 * case, since that's how we ended up here. For IPv6, we do the 18986 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18987 */ 18988 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18989 /* 18990 * Restore LSO for this connection, so that next time around 18991 * it is eligible to go through tcp_lsosend() path again. 18992 */ 18993 TCP_STAT(tcps, tcp_lso_enabled); 18994 tcp->tcp_lso = B_TRUE; 18995 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18996 "interface %s\n", (void *)connp, ill->ill_name)); 18997 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18998 /* 18999 * Restore MDT for this connection, so that next time around 19000 * it is eligible to go through tcp_multisend() path again. 19001 */ 19002 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19003 tcp->tcp_mdt = B_TRUE; 19004 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19005 "interface %s\n", (void *)connp, ill->ill_name)); 19006 } 19007 19008 if (tcp->tcp_snd_zcopy_aware) { 19009 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19010 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19011 mp = tcp_zcopy_disable(tcp, mp); 19012 /* 19013 * we shouldn't need to reset ipha as the mp containing 19014 * ipha should never be a zero-copy mp. 19015 */ 19016 } 19017 19018 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19019 ASSERT(ill->ill_hcksum_capab != NULL); 19020 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19021 } 19022 19023 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19024 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19025 19026 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19027 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19028 19029 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19030 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19031 19032 /* Software checksum? */ 19033 if (DB_CKSUMFLAGS(mp) == 0) { 19034 TCP_STAT(tcps, tcp_out_sw_cksum); 19035 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19036 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19037 } 19038 19039 ipha->ipha_fragment_offset_and_flags |= 19040 (uint32_t)htons(ire->ire_frag_flag); 19041 19042 /* Calculate IP header checksum if hardware isn't capable */ 19043 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19044 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19045 ((uint16_t *)ipha)[4]); 19046 } 19047 19048 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19049 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19050 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19051 19052 UPDATE_OB_PKT_COUNT(ire); 19053 ire->ire_last_used_time = lbolt; 19054 19055 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19056 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19057 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19058 ntohs(ipha->ipha_length)); 19059 19060 if (ILL_DLS_CAPABLE(ill)) { 19061 /* 19062 * Send the packet directly to DLD, where it may be queued 19063 * depending on the availability of transmit resources at 19064 * the media layer. 19065 */ 19066 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 19067 } else { 19068 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 19069 DTRACE_PROBE4(ip4__physical__out__start, 19070 ill_t *, NULL, ill_t *, out_ill, 19071 ipha_t *, ipha, mblk_t *, mp); 19072 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19073 ipst->ips_ipv4firewall_physical_out, 19074 NULL, out_ill, ipha, mp, mp, ipst); 19075 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19076 if (mp != NULL) 19077 putnext(ire->ire_stq, mp); 19078 } 19079 IRE_REFRELE(ire); 19080 } 19081 19082 /* 19083 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19084 * if the receiver shrinks the window, i.e. moves the right window to the 19085 * left, the we should not send new data, but should retransmit normally the 19086 * old unacked data between suna and suna + swnd. We might has sent data 19087 * that is now outside the new window, pretend that we didn't send it. 19088 */ 19089 static void 19090 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19091 { 19092 uint32_t snxt = tcp->tcp_snxt; 19093 mblk_t *xmit_tail; 19094 int32_t offset; 19095 19096 ASSERT(shrunk_count > 0); 19097 19098 /* Pretend we didn't send the data outside the window */ 19099 snxt -= shrunk_count; 19100 19101 /* Get the mblk and the offset in it per the shrunk window */ 19102 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19103 19104 ASSERT(xmit_tail != NULL); 19105 19106 /* Reset all the values per the now shrunk window */ 19107 tcp->tcp_snxt = snxt; 19108 tcp->tcp_xmit_tail = xmit_tail; 19109 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19110 offset; 19111 tcp->tcp_unsent += shrunk_count; 19112 19113 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19114 /* 19115 * Make sure the timer is running so that we will probe a zero 19116 * window. 19117 */ 19118 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19119 } 19120 19121 19122 /* 19123 * The TCP normal data output path. 19124 * NOTE: the logic of the fast path is duplicated from this function. 19125 */ 19126 static void 19127 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19128 { 19129 int len; 19130 mblk_t *local_time; 19131 mblk_t *mp1; 19132 uint32_t snxt; 19133 int tail_unsent; 19134 int tcpstate; 19135 int usable = 0; 19136 mblk_t *xmit_tail; 19137 queue_t *q = tcp->tcp_wq; 19138 int32_t mss; 19139 int32_t num_sack_blk = 0; 19140 int32_t tcp_hdr_len; 19141 int32_t tcp_tcp_hdr_len; 19142 int mdt_thres; 19143 int rc; 19144 tcp_stack_t *tcps = tcp->tcp_tcps; 19145 ip_stack_t *ipst; 19146 19147 tcpstate = tcp->tcp_state; 19148 if (mp == NULL) { 19149 /* 19150 * tcp_wput_data() with NULL mp should only be called when 19151 * there is unsent data. 19152 */ 19153 ASSERT(tcp->tcp_unsent > 0); 19154 /* Really tacky... but we need this for detached closes. */ 19155 len = tcp->tcp_unsent; 19156 goto data_null; 19157 } 19158 19159 #if CCS_STATS 19160 wrw_stats.tot.count++; 19161 wrw_stats.tot.bytes += msgdsize(mp); 19162 #endif 19163 ASSERT(mp->b_datap->db_type == M_DATA); 19164 /* 19165 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19166 * or before a connection attempt has begun. 19167 */ 19168 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19169 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19170 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19171 #ifdef DEBUG 19172 cmn_err(CE_WARN, 19173 "tcp_wput_data: data after ordrel, %s", 19174 tcp_display(tcp, NULL, 19175 DISP_ADDR_AND_PORT)); 19176 #else 19177 if (tcp->tcp_debug) { 19178 (void) strlog(TCP_MOD_ID, 0, 1, 19179 SL_TRACE|SL_ERROR, 19180 "tcp_wput_data: data after ordrel, %s\n", 19181 tcp_display(tcp, NULL, 19182 DISP_ADDR_AND_PORT)); 19183 } 19184 #endif /* DEBUG */ 19185 } 19186 if (tcp->tcp_snd_zcopy_aware && 19187 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19188 tcp_zcopy_notify(tcp); 19189 freemsg(mp); 19190 mutex_enter(&tcp->tcp_non_sq_lock); 19191 if (tcp->tcp_flow_stopped && 19192 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19193 tcp_clrqfull(tcp); 19194 } 19195 mutex_exit(&tcp->tcp_non_sq_lock); 19196 return; 19197 } 19198 19199 /* Strip empties */ 19200 for (;;) { 19201 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19202 (uintptr_t)INT_MAX); 19203 len = (int)(mp->b_wptr - mp->b_rptr); 19204 if (len > 0) 19205 break; 19206 mp1 = mp; 19207 mp = mp->b_cont; 19208 freeb(mp1); 19209 if (!mp) { 19210 return; 19211 } 19212 } 19213 19214 /* If we are the first on the list ... */ 19215 if (tcp->tcp_xmit_head == NULL) { 19216 tcp->tcp_xmit_head = mp; 19217 tcp->tcp_xmit_tail = mp; 19218 tcp->tcp_xmit_tail_unsent = len; 19219 } else { 19220 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19221 struct datab *dp; 19222 19223 mp1 = tcp->tcp_xmit_last; 19224 if (len < tcp_tx_pull_len && 19225 (dp = mp1->b_datap)->db_ref == 1 && 19226 dp->db_lim - mp1->b_wptr >= len) { 19227 ASSERT(len > 0); 19228 ASSERT(!mp1->b_cont); 19229 if (len == 1) { 19230 *mp1->b_wptr++ = *mp->b_rptr; 19231 } else { 19232 bcopy(mp->b_rptr, mp1->b_wptr, len); 19233 mp1->b_wptr += len; 19234 } 19235 if (mp1 == tcp->tcp_xmit_tail) 19236 tcp->tcp_xmit_tail_unsent += len; 19237 mp1->b_cont = mp->b_cont; 19238 if (tcp->tcp_snd_zcopy_aware && 19239 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19240 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19241 freeb(mp); 19242 mp = mp1; 19243 } else { 19244 tcp->tcp_xmit_last->b_cont = mp; 19245 } 19246 len += tcp->tcp_unsent; 19247 } 19248 19249 /* Tack on however many more positive length mblks we have */ 19250 if ((mp1 = mp->b_cont) != NULL) { 19251 do { 19252 int tlen; 19253 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19254 (uintptr_t)INT_MAX); 19255 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19256 if (tlen <= 0) { 19257 mp->b_cont = mp1->b_cont; 19258 freeb(mp1); 19259 } else { 19260 len += tlen; 19261 mp = mp1; 19262 } 19263 } while ((mp1 = mp->b_cont) != NULL); 19264 } 19265 tcp->tcp_xmit_last = mp; 19266 tcp->tcp_unsent = len; 19267 19268 if (urgent) 19269 usable = 1; 19270 19271 data_null: 19272 snxt = tcp->tcp_snxt; 19273 xmit_tail = tcp->tcp_xmit_tail; 19274 tail_unsent = tcp->tcp_xmit_tail_unsent; 19275 19276 /* 19277 * Note that tcp_mss has been adjusted to take into account the 19278 * timestamp option if applicable. Because SACK options do not 19279 * appear in every TCP segments and they are of variable lengths, 19280 * they cannot be included in tcp_mss. Thus we need to calculate 19281 * the actual segment length when we need to send a segment which 19282 * includes SACK options. 19283 */ 19284 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19285 int32_t opt_len; 19286 19287 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19288 tcp->tcp_num_sack_blk); 19289 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19290 2 + TCPOPT_HEADER_LEN; 19291 mss = tcp->tcp_mss - opt_len; 19292 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19293 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19294 } else { 19295 mss = tcp->tcp_mss; 19296 tcp_hdr_len = tcp->tcp_hdr_len; 19297 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19298 } 19299 19300 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19301 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19302 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19303 } 19304 if (tcpstate == TCPS_SYN_RCVD) { 19305 /* 19306 * The three-way connection establishment handshake is not 19307 * complete yet. We want to queue the data for transmission 19308 * after entering ESTABLISHED state (RFC793). A jump to 19309 * "done" label effectively leaves data on the queue. 19310 */ 19311 goto done; 19312 } else { 19313 int usable_r; 19314 19315 /* 19316 * In the special case when cwnd is zero, which can only 19317 * happen if the connection is ECN capable, return now. 19318 * New segments is sent using tcp_timer(). The timer 19319 * is set in tcp_rput_data(). 19320 */ 19321 if (tcp->tcp_cwnd == 0) { 19322 /* 19323 * Note that tcp_cwnd is 0 before 3-way handshake is 19324 * finished. 19325 */ 19326 ASSERT(tcp->tcp_ecn_ok || 19327 tcp->tcp_state < TCPS_ESTABLISHED); 19328 return; 19329 } 19330 19331 /* NOTE: trouble if xmitting while SYN not acked? */ 19332 usable_r = snxt - tcp->tcp_suna; 19333 usable_r = tcp->tcp_swnd - usable_r; 19334 19335 /* 19336 * Check if the receiver has shrunk the window. If 19337 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19338 * cannot be set as there is unsent data, so FIN cannot 19339 * be sent out. Otherwise, we need to take into account 19340 * of FIN as it consumes an "invisible" sequence number. 19341 */ 19342 ASSERT(tcp->tcp_fin_sent == 0); 19343 if (usable_r < 0) { 19344 /* 19345 * The receiver has shrunk the window and we have sent 19346 * -usable_r date beyond the window, re-adjust. 19347 * 19348 * If TCP window scaling is enabled, there can be 19349 * round down error as the advertised receive window 19350 * is actually right shifted n bits. This means that 19351 * the lower n bits info is wiped out. It will look 19352 * like the window is shrunk. Do a check here to 19353 * see if the shrunk amount is actually within the 19354 * error in window calculation. If it is, just 19355 * return. Note that this check is inside the 19356 * shrunk window check. This makes sure that even 19357 * though tcp_process_shrunk_swnd() is not called, 19358 * we will stop further processing. 19359 */ 19360 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19361 tcp_process_shrunk_swnd(tcp, -usable_r); 19362 } 19363 return; 19364 } 19365 19366 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19367 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19368 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19369 19370 /* usable = MIN(usable, unsent) */ 19371 if (usable_r > len) 19372 usable_r = len; 19373 19374 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19375 if (usable_r > 0) { 19376 usable = usable_r; 19377 } else { 19378 /* Bypass all other unnecessary processing. */ 19379 goto done; 19380 } 19381 } 19382 19383 local_time = (mblk_t *)lbolt; 19384 19385 /* 19386 * "Our" Nagle Algorithm. This is not the same as in the old 19387 * BSD. This is more in line with the true intent of Nagle. 19388 * 19389 * The conditions are: 19390 * 1. The amount of unsent data (or amount of data which can be 19391 * sent, whichever is smaller) is less than Nagle limit. 19392 * 2. The last sent size is also less than Nagle limit. 19393 * 3. There is unack'ed data. 19394 * 4. Urgent pointer is not set. Send urgent data ignoring the 19395 * Nagle algorithm. This reduces the probability that urgent 19396 * bytes get "merged" together. 19397 * 5. The app has not closed the connection. This eliminates the 19398 * wait time of the receiving side waiting for the last piece of 19399 * (small) data. 19400 * 19401 * If all are satisified, exit without sending anything. Note 19402 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19403 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19404 * 4095). 19405 */ 19406 if (usable < (int)tcp->tcp_naglim && 19407 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19408 snxt != tcp->tcp_suna && 19409 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19410 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19411 goto done; 19412 } 19413 19414 if (tcp->tcp_cork) { 19415 /* 19416 * if the tcp->tcp_cork option is set, then we have to force 19417 * TCP not to send partial segment (smaller than MSS bytes). 19418 * We are calculating the usable now based on full mss and 19419 * will save the rest of remaining data for later. 19420 */ 19421 if (usable < mss) 19422 goto done; 19423 usable = (usable / mss) * mss; 19424 } 19425 19426 /* Update the latest receive window size in TCP header. */ 19427 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19428 tcp->tcp_tcph->th_win); 19429 19430 /* 19431 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19432 * 19433 * 1. Simple TCP/IP{v4,v6} (no options). 19434 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19435 * 3. If the TCP connection is in ESTABLISHED state. 19436 * 4. The TCP is not detached. 19437 * 19438 * If any of the above conditions have changed during the 19439 * connection, stop using LSO/MDT and restore the stream head 19440 * parameters accordingly. 19441 */ 19442 ipst = tcps->tcps_netstack->netstack_ip; 19443 19444 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19445 ((tcp->tcp_ipversion == IPV4_VERSION && 19446 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19447 (tcp->tcp_ipversion == IPV6_VERSION && 19448 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19449 tcp->tcp_state != TCPS_ESTABLISHED || 19450 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19451 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19452 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19453 if (tcp->tcp_lso) { 19454 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19455 tcp->tcp_lso = B_FALSE; 19456 } else { 19457 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19458 tcp->tcp_mdt = B_FALSE; 19459 } 19460 19461 /* Anything other than detached is considered pathological */ 19462 if (!TCP_IS_DETACHED(tcp)) { 19463 if (tcp->tcp_lso) 19464 TCP_STAT(tcps, tcp_lso_disabled); 19465 else 19466 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19467 (void) tcp_maxpsz_set(tcp, B_TRUE); 19468 } 19469 } 19470 19471 /* Use MDT if sendable amount is greater than the threshold */ 19472 if (tcp->tcp_mdt && 19473 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19474 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19475 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19476 (tcp->tcp_valid_bits == 0 || 19477 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19478 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19479 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19480 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19481 local_time, mdt_thres); 19482 } else { 19483 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19484 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19485 local_time, INT_MAX); 19486 } 19487 19488 /* Pretend that all we were trying to send really got sent */ 19489 if (rc < 0 && tail_unsent < 0) { 19490 do { 19491 xmit_tail = xmit_tail->b_cont; 19492 xmit_tail->b_prev = local_time; 19493 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19494 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19495 tail_unsent += (int)(xmit_tail->b_wptr - 19496 xmit_tail->b_rptr); 19497 } while (tail_unsent < 0); 19498 } 19499 done:; 19500 tcp->tcp_xmit_tail = xmit_tail; 19501 tcp->tcp_xmit_tail_unsent = tail_unsent; 19502 len = tcp->tcp_snxt - snxt; 19503 if (len) { 19504 /* 19505 * If new data was sent, need to update the notsack 19506 * list, which is, afterall, data blocks that have 19507 * not been sack'ed by the receiver. New data is 19508 * not sack'ed. 19509 */ 19510 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19511 /* len is a negative value. */ 19512 tcp->tcp_pipe -= len; 19513 tcp_notsack_update(&(tcp->tcp_notsack_list), 19514 tcp->tcp_snxt, snxt, 19515 &(tcp->tcp_num_notsack_blk), 19516 &(tcp->tcp_cnt_notsack_list)); 19517 } 19518 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19519 tcp->tcp_rack = tcp->tcp_rnxt; 19520 tcp->tcp_rack_cnt = 0; 19521 if ((snxt + len) == tcp->tcp_suna) { 19522 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19523 } 19524 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19525 /* 19526 * Didn't send anything. Make sure the timer is running 19527 * so that we will probe a zero window. 19528 */ 19529 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19530 } 19531 /* Note that len is the amount we just sent but with a negative sign */ 19532 tcp->tcp_unsent += len; 19533 mutex_enter(&tcp->tcp_non_sq_lock); 19534 if (tcp->tcp_flow_stopped) { 19535 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19536 tcp_clrqfull(tcp); 19537 } 19538 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19539 tcp_setqfull(tcp); 19540 } 19541 mutex_exit(&tcp->tcp_non_sq_lock); 19542 } 19543 19544 /* 19545 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19546 * outgoing TCP header with the template header, as well as other 19547 * options such as time-stamp, ECN and/or SACK. 19548 */ 19549 static void 19550 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19551 { 19552 tcph_t *tcp_tmpl, *tcp_h; 19553 uint32_t *dst, *src; 19554 int hdrlen; 19555 19556 ASSERT(OK_32PTR(rptr)); 19557 19558 /* Template header */ 19559 tcp_tmpl = tcp->tcp_tcph; 19560 19561 /* Header of outgoing packet */ 19562 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19563 19564 /* dst and src are opaque 32-bit fields, used for copying */ 19565 dst = (uint32_t *)rptr; 19566 src = (uint32_t *)tcp->tcp_iphc; 19567 hdrlen = tcp->tcp_hdr_len; 19568 19569 /* Fill time-stamp option if needed */ 19570 if (tcp->tcp_snd_ts_ok) { 19571 U32_TO_BE32((uint32_t)now, 19572 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19573 U32_TO_BE32(tcp->tcp_ts_recent, 19574 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19575 } else { 19576 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19577 } 19578 19579 /* 19580 * Copy the template header; is this really more efficient than 19581 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19582 * but perhaps not for other scenarios. 19583 */ 19584 dst[0] = src[0]; 19585 dst[1] = src[1]; 19586 dst[2] = src[2]; 19587 dst[3] = src[3]; 19588 dst[4] = src[4]; 19589 dst[5] = src[5]; 19590 dst[6] = src[6]; 19591 dst[7] = src[7]; 19592 dst[8] = src[8]; 19593 dst[9] = src[9]; 19594 if (hdrlen -= 40) { 19595 hdrlen >>= 2; 19596 dst += 10; 19597 src += 10; 19598 do { 19599 *dst++ = *src++; 19600 } while (--hdrlen); 19601 } 19602 19603 /* 19604 * Set the ECN info in the TCP header if it is not a zero 19605 * window probe. Zero window probe is only sent in 19606 * tcp_wput_data() and tcp_timer(). 19607 */ 19608 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19609 SET_ECT(tcp, rptr); 19610 19611 if (tcp->tcp_ecn_echo_on) 19612 tcp_h->th_flags[0] |= TH_ECE; 19613 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19614 tcp_h->th_flags[0] |= TH_CWR; 19615 tcp->tcp_ecn_cwr_sent = B_TRUE; 19616 } 19617 } 19618 19619 /* Fill in SACK options */ 19620 if (num_sack_blk > 0) { 19621 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19622 sack_blk_t *tmp; 19623 int32_t i; 19624 19625 wptr[0] = TCPOPT_NOP; 19626 wptr[1] = TCPOPT_NOP; 19627 wptr[2] = TCPOPT_SACK; 19628 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19629 sizeof (sack_blk_t); 19630 wptr += TCPOPT_REAL_SACK_LEN; 19631 19632 tmp = tcp->tcp_sack_list; 19633 for (i = 0; i < num_sack_blk; i++) { 19634 U32_TO_BE32(tmp[i].begin, wptr); 19635 wptr += sizeof (tcp_seq); 19636 U32_TO_BE32(tmp[i].end, wptr); 19637 wptr += sizeof (tcp_seq); 19638 } 19639 tcp_h->th_offset_and_rsrvd[0] += 19640 ((num_sack_blk * 2 + 1) << 4); 19641 } 19642 } 19643 19644 /* 19645 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19646 * the destination address and SAP attribute, and if necessary, the 19647 * hardware checksum offload attribute to a Multidata message. 19648 */ 19649 static int 19650 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19651 const uint32_t start, const uint32_t stuff, const uint32_t end, 19652 const uint32_t flags, tcp_stack_t *tcps) 19653 { 19654 /* Add global destination address & SAP attribute */ 19655 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19656 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19657 "destination address+SAP\n")); 19658 19659 if (dlmp != NULL) 19660 TCP_STAT(tcps, tcp_mdt_allocfail); 19661 return (-1); 19662 } 19663 19664 /* Add global hwcksum attribute */ 19665 if (hwcksum && 19666 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19667 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19668 "checksum attribute\n")); 19669 19670 TCP_STAT(tcps, tcp_mdt_allocfail); 19671 return (-1); 19672 } 19673 19674 return (0); 19675 } 19676 19677 /* 19678 * Smaller and private version of pdescinfo_t used specifically for TCP, 19679 * which allows for only two payload spans per packet. 19680 */ 19681 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19682 19683 /* 19684 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19685 * scheme, and returns one the following: 19686 * 19687 * -1 = failed allocation. 19688 * 0 = success; burst count reached, or usable send window is too small, 19689 * and that we'd rather wait until later before sending again. 19690 */ 19691 static int 19692 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19693 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19694 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19695 const int mdt_thres) 19696 { 19697 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19698 multidata_t *mmd; 19699 uint_t obsegs, obbytes, hdr_frag_sz; 19700 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19701 int num_burst_seg, max_pld; 19702 pdesc_t *pkt; 19703 tcp_pdescinfo_t tcp_pkt_info; 19704 pdescinfo_t *pkt_info; 19705 int pbuf_idx, pbuf_idx_nxt; 19706 int seg_len, len, spill, af; 19707 boolean_t add_buffer, zcopy, clusterwide; 19708 boolean_t buf_trunked = B_FALSE; 19709 boolean_t rconfirm = B_FALSE; 19710 boolean_t done = B_FALSE; 19711 uint32_t cksum; 19712 uint32_t hwcksum_flags; 19713 ire_t *ire = NULL; 19714 ill_t *ill; 19715 ipha_t *ipha; 19716 ip6_t *ip6h; 19717 ipaddr_t src, dst; 19718 ill_zerocopy_capab_t *zc_cap = NULL; 19719 uint16_t *up; 19720 int err; 19721 conn_t *connp; 19722 mblk_t *mp, *mp1, *fw_mp_head = NULL; 19723 uchar_t *pld_start; 19724 tcp_stack_t *tcps = tcp->tcp_tcps; 19725 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19726 19727 #ifdef _BIG_ENDIAN 19728 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19729 #else 19730 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19731 #endif 19732 19733 #define PREP_NEW_MULTIDATA() { \ 19734 mmd = NULL; \ 19735 md_mp = md_hbuf = NULL; \ 19736 cur_hdr_off = 0; \ 19737 max_pld = tcp->tcp_mdt_max_pld; \ 19738 pbuf_idx = pbuf_idx_nxt = -1; \ 19739 add_buffer = B_TRUE; \ 19740 zcopy = B_FALSE; \ 19741 } 19742 19743 #define PREP_NEW_PBUF() { \ 19744 md_pbuf = md_pbuf_nxt = NULL; \ 19745 pbuf_idx = pbuf_idx_nxt = -1; \ 19746 cur_pld_off = 0; \ 19747 first_snxt = *snxt; \ 19748 ASSERT(*tail_unsent > 0); \ 19749 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19750 } 19751 19752 ASSERT(mdt_thres >= mss); 19753 ASSERT(*usable > 0 && *usable > mdt_thres); 19754 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19755 ASSERT(!TCP_IS_DETACHED(tcp)); 19756 ASSERT(tcp->tcp_valid_bits == 0 || 19757 tcp->tcp_valid_bits == TCP_FSS_VALID); 19758 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19759 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19760 (tcp->tcp_ipversion == IPV6_VERSION && 19761 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19762 19763 connp = tcp->tcp_connp; 19764 ASSERT(connp != NULL); 19765 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19766 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19767 19768 /* 19769 * Note that tcp will only declare at most 2 payload spans per 19770 * packet, which is much lower than the maximum allowable number 19771 * of packet spans per Multidata. For this reason, we use the 19772 * privately declared and smaller descriptor info structure, in 19773 * order to save some stack space. 19774 */ 19775 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19776 19777 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19778 if (af == AF_INET) { 19779 dst = tcp->tcp_ipha->ipha_dst; 19780 src = tcp->tcp_ipha->ipha_src; 19781 ASSERT(!CLASSD(dst)); 19782 } 19783 ASSERT(af == AF_INET || 19784 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19785 19786 obsegs = obbytes = 0; 19787 num_burst_seg = tcp->tcp_snd_burst; 19788 md_mp_head = NULL; 19789 PREP_NEW_MULTIDATA(); 19790 19791 /* 19792 * Before we go on further, make sure there is an IRE that we can 19793 * use, and that the ILL supports MDT. Otherwise, there's no point 19794 * in proceeding any further, and we should just hand everything 19795 * off to the legacy path. 19796 */ 19797 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19798 goto legacy_send_no_md; 19799 19800 ASSERT(ire != NULL); 19801 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19802 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19803 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19804 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19805 /* 19806 * If we do support loopback for MDT (which requires modifications 19807 * to the receiving paths), the following assertions should go away, 19808 * and we would be sending the Multidata to loopback conn later on. 19809 */ 19810 ASSERT(!IRE_IS_LOCAL(ire)); 19811 ASSERT(ire->ire_stq != NULL); 19812 19813 ill = ire_to_ill(ire); 19814 ASSERT(ill != NULL); 19815 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19816 19817 if (!tcp->tcp_ire_ill_check_done) { 19818 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19819 tcp->tcp_ire_ill_check_done = B_TRUE; 19820 } 19821 19822 /* 19823 * If the underlying interface conditions have changed, or if the 19824 * new interface does not support MDT, go back to legacy path. 19825 */ 19826 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19827 /* don't go through this path anymore for this connection */ 19828 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19829 tcp->tcp_mdt = B_FALSE; 19830 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19831 "interface %s\n", (void *)connp, ill->ill_name)); 19832 /* IRE will be released prior to returning */ 19833 goto legacy_send_no_md; 19834 } 19835 19836 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19837 zc_cap = ill->ill_zerocopy_capab; 19838 19839 /* 19840 * Check if we can take tcp fast-path. Note that "incomplete" 19841 * ire's (where the link-layer for next hop is not resolved 19842 * or where the fast-path header in nce_fp_mp is not available 19843 * yet) are sent down the legacy (slow) path. 19844 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19845 */ 19846 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19847 /* IRE will be released prior to returning */ 19848 goto legacy_send_no_md; 19849 } 19850 19851 /* go to legacy path if interface doesn't support zerocopy */ 19852 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19853 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19854 /* IRE will be released prior to returning */ 19855 goto legacy_send_no_md; 19856 } 19857 19858 /* does the interface support hardware checksum offload? */ 19859 hwcksum_flags = 0; 19860 if (ILL_HCKSUM_CAPABLE(ill) && 19861 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19862 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19863 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19864 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19865 HCKSUM_IPHDRCKSUM) 19866 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19867 19868 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19869 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19870 hwcksum_flags |= HCK_FULLCKSUM; 19871 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19872 HCKSUM_INET_PARTIAL) 19873 hwcksum_flags |= HCK_PARTIALCKSUM; 19874 } 19875 19876 /* 19877 * Each header fragment consists of the leading extra space, 19878 * followed by the TCP/IP header, and the trailing extra space. 19879 * We make sure that each header fragment begins on a 32-bit 19880 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19881 * aligned in tcp_mdt_update). 19882 */ 19883 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19884 tcp->tcp_mdt_hdr_tail), 4); 19885 19886 /* are we starting from the beginning of data block? */ 19887 if (*tail_unsent == 0) { 19888 *xmit_tail = (*xmit_tail)->b_cont; 19889 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19890 *tail_unsent = (int)MBLKL(*xmit_tail); 19891 } 19892 19893 /* 19894 * Here we create one or more Multidata messages, each made up of 19895 * one header buffer and up to N payload buffers. This entire 19896 * operation is done within two loops: 19897 * 19898 * The outer loop mostly deals with creating the Multidata message, 19899 * as well as the header buffer that gets added to it. It also 19900 * links the Multidata messages together such that all of them can 19901 * be sent down to the lower layer in a single putnext call; this 19902 * linking behavior depends on the tcp_mdt_chain tunable. 19903 * 19904 * The inner loop takes an existing Multidata message, and adds 19905 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19906 * packetizes those buffers by filling up the corresponding header 19907 * buffer fragments with the proper IP and TCP headers, and by 19908 * describing the layout of each packet in the packet descriptors 19909 * that get added to the Multidata. 19910 */ 19911 do { 19912 /* 19913 * If usable send window is too small, or data blocks in 19914 * transmit list are smaller than our threshold (i.e. app 19915 * performs large writes followed by small ones), we hand 19916 * off the control over to the legacy path. Note that we'll 19917 * get back the control once it encounters a large block. 19918 */ 19919 if (*usable < mss || (*tail_unsent <= mdt_thres && 19920 (*xmit_tail)->b_cont != NULL && 19921 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19922 /* send down what we've got so far */ 19923 if (md_mp_head != NULL) { 19924 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19925 obsegs, obbytes, &rconfirm); 19926 } 19927 /* 19928 * Pass control over to tcp_send(), but tell it to 19929 * return to us once a large-size transmission is 19930 * possible. 19931 */ 19932 TCP_STAT(tcps, tcp_mdt_legacy_small); 19933 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19934 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19935 tail_unsent, xmit_tail, local_time, 19936 mdt_thres)) <= 0) { 19937 /* burst count reached, or alloc failed */ 19938 IRE_REFRELE(ire); 19939 return (err); 19940 } 19941 19942 /* tcp_send() may have sent everything, so check */ 19943 if (*usable <= 0) { 19944 IRE_REFRELE(ire); 19945 return (0); 19946 } 19947 19948 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19949 /* 19950 * We may have delivered the Multidata, so make sure 19951 * to re-initialize before the next round. 19952 */ 19953 md_mp_head = NULL; 19954 obsegs = obbytes = 0; 19955 num_burst_seg = tcp->tcp_snd_burst; 19956 PREP_NEW_MULTIDATA(); 19957 19958 /* are we starting from the beginning of data block? */ 19959 if (*tail_unsent == 0) { 19960 *xmit_tail = (*xmit_tail)->b_cont; 19961 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19962 (uintptr_t)INT_MAX); 19963 *tail_unsent = (int)MBLKL(*xmit_tail); 19964 } 19965 } 19966 19967 /* 19968 * max_pld limits the number of mblks in tcp's transmit 19969 * queue that can be added to a Multidata message. Once 19970 * this counter reaches zero, no more additional mblks 19971 * can be added to it. What happens afterwards depends 19972 * on whether or not we are set to chain the Multidata 19973 * messages. If we are to link them together, reset 19974 * max_pld to its original value (tcp_mdt_max_pld) and 19975 * prepare to create a new Multidata message which will 19976 * get linked to md_mp_head. Else, leave it alone and 19977 * let the inner loop break on its own. 19978 */ 19979 if (tcp_mdt_chain && max_pld == 0) 19980 PREP_NEW_MULTIDATA(); 19981 19982 /* adding a payload buffer; re-initialize values */ 19983 if (add_buffer) 19984 PREP_NEW_PBUF(); 19985 19986 /* 19987 * If we don't have a Multidata, either because we just 19988 * (re)entered this outer loop, or after we branched off 19989 * to tcp_send above, setup the Multidata and header 19990 * buffer to be used. 19991 */ 19992 if (md_mp == NULL) { 19993 int md_hbuflen; 19994 uint32_t start, stuff; 19995 19996 /* 19997 * Calculate Multidata header buffer size large enough 19998 * to hold all of the headers that can possibly be 19999 * sent at this moment. We'd rather over-estimate 20000 * the size than running out of space; this is okay 20001 * since this buffer is small anyway. 20002 */ 20003 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20004 20005 /* 20006 * Start and stuff offset for partial hardware 20007 * checksum offload; these are currently for IPv4. 20008 * For full checksum offload, they are set to zero. 20009 */ 20010 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20011 if (af == AF_INET) { 20012 start = IP_SIMPLE_HDR_LENGTH; 20013 stuff = IP_SIMPLE_HDR_LENGTH + 20014 TCP_CHECKSUM_OFFSET; 20015 } else { 20016 start = IPV6_HDR_LEN; 20017 stuff = IPV6_HDR_LEN + 20018 TCP_CHECKSUM_OFFSET; 20019 } 20020 } else { 20021 start = stuff = 0; 20022 } 20023 20024 /* 20025 * Create the header buffer, Multidata, as well as 20026 * any necessary attributes (destination address, 20027 * SAP and hardware checksum offload) that should 20028 * be associated with the Multidata message. 20029 */ 20030 ASSERT(cur_hdr_off == 0); 20031 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20032 ((md_hbuf->b_wptr += md_hbuflen), 20033 (mmd = mmd_alloc(md_hbuf, &md_mp, 20034 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20035 /* fastpath mblk */ 20036 ire->ire_nce->nce_res_mp, 20037 /* hardware checksum enabled */ 20038 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20039 /* hardware checksum offsets */ 20040 start, stuff, 0, 20041 /* hardware checksum flag */ 20042 hwcksum_flags, tcps) != 0)) { 20043 legacy_send: 20044 if (md_mp != NULL) { 20045 /* Unlink message from the chain */ 20046 if (md_mp_head != NULL) { 20047 err = (intptr_t)rmvb(md_mp_head, 20048 md_mp); 20049 /* 20050 * We can't assert that rmvb 20051 * did not return -1, since we 20052 * may get here before linkb 20053 * happens. We do, however, 20054 * check if we just removed the 20055 * only element in the list. 20056 */ 20057 if (err == 0) 20058 md_mp_head = NULL; 20059 } 20060 /* md_hbuf gets freed automatically */ 20061 TCP_STAT(tcps, tcp_mdt_discarded); 20062 freeb(md_mp); 20063 } else { 20064 /* Either allocb or mmd_alloc failed */ 20065 TCP_STAT(tcps, tcp_mdt_allocfail); 20066 if (md_hbuf != NULL) 20067 freeb(md_hbuf); 20068 } 20069 20070 /* send down what we've got so far */ 20071 if (md_mp_head != NULL) { 20072 tcp_multisend_data(tcp, ire, ill, 20073 md_mp_head, obsegs, obbytes, 20074 &rconfirm); 20075 } 20076 legacy_send_no_md: 20077 if (ire != NULL) 20078 IRE_REFRELE(ire); 20079 /* 20080 * Too bad; let the legacy path handle this. 20081 * We specify INT_MAX for the threshold, since 20082 * we gave up with the Multidata processings 20083 * and let the old path have it all. 20084 */ 20085 TCP_STAT(tcps, tcp_mdt_legacy_all); 20086 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20087 tcp_tcp_hdr_len, num_sack_blk, usable, 20088 snxt, tail_unsent, xmit_tail, local_time, 20089 INT_MAX)); 20090 } 20091 20092 /* link to any existing ones, if applicable */ 20093 TCP_STAT(tcps, tcp_mdt_allocd); 20094 if (md_mp_head == NULL) { 20095 md_mp_head = md_mp; 20096 } else if (tcp_mdt_chain) { 20097 TCP_STAT(tcps, tcp_mdt_linked); 20098 linkb(md_mp_head, md_mp); 20099 } 20100 } 20101 20102 ASSERT(md_mp_head != NULL); 20103 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20104 ASSERT(md_mp != NULL && mmd != NULL); 20105 ASSERT(md_hbuf != NULL); 20106 20107 /* 20108 * Packetize the transmittable portion of the data block; 20109 * each data block is essentially added to the Multidata 20110 * as a payload buffer. We also deal with adding more 20111 * than one payload buffers, which happens when the remaining 20112 * packetized portion of the current payload buffer is less 20113 * than MSS, while the next data block in transmit queue 20114 * has enough data to make up for one. This "spillover" 20115 * case essentially creates a split-packet, where portions 20116 * of the packet's payload fragments may span across two 20117 * virtually discontiguous address blocks. 20118 */ 20119 seg_len = mss; 20120 do { 20121 len = seg_len; 20122 20123 ASSERT(len > 0); 20124 ASSERT(max_pld >= 0); 20125 ASSERT(!add_buffer || cur_pld_off == 0); 20126 20127 /* 20128 * First time around for this payload buffer; note 20129 * in the case of a spillover, the following has 20130 * been done prior to adding the split-packet 20131 * descriptor to Multidata, and we don't want to 20132 * repeat the process. 20133 */ 20134 if (add_buffer) { 20135 ASSERT(mmd != NULL); 20136 ASSERT(md_pbuf == NULL); 20137 ASSERT(md_pbuf_nxt == NULL); 20138 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20139 20140 /* 20141 * Have we reached the limit? We'd get to 20142 * this case when we're not chaining the 20143 * Multidata messages together, and since 20144 * we're done, terminate this loop. 20145 */ 20146 if (max_pld == 0) 20147 break; /* done */ 20148 20149 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20150 TCP_STAT(tcps, tcp_mdt_allocfail); 20151 goto legacy_send; /* out_of_mem */ 20152 } 20153 20154 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20155 zc_cap != NULL) { 20156 if (!ip_md_zcopy_attr(mmd, NULL, 20157 zc_cap->ill_zerocopy_flags)) { 20158 freeb(md_pbuf); 20159 TCP_STAT(tcps, 20160 tcp_mdt_allocfail); 20161 /* out_of_mem */ 20162 goto legacy_send; 20163 } 20164 zcopy = B_TRUE; 20165 } 20166 20167 md_pbuf->b_rptr += base_pld_off; 20168 20169 /* 20170 * Add a payload buffer to the Multidata; this 20171 * operation must not fail, or otherwise our 20172 * logic in this routine is broken. There 20173 * is no memory allocation done by the 20174 * routine, so any returned failure simply 20175 * tells us that we've done something wrong. 20176 * 20177 * A failure tells us that either we're adding 20178 * the same payload buffer more than once, or 20179 * we're trying to add more buffers than 20180 * allowed (max_pld calculation is wrong). 20181 * None of the above cases should happen, and 20182 * we panic because either there's horrible 20183 * heap corruption, and/or programming mistake. 20184 */ 20185 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20186 if (pbuf_idx < 0) { 20187 cmn_err(CE_PANIC, "tcp_multisend: " 20188 "payload buffer logic error " 20189 "detected for tcp %p mmd %p " 20190 "pbuf %p (%d)\n", 20191 (void *)tcp, (void *)mmd, 20192 (void *)md_pbuf, pbuf_idx); 20193 } 20194 20195 ASSERT(max_pld > 0); 20196 --max_pld; 20197 add_buffer = B_FALSE; 20198 } 20199 20200 ASSERT(md_mp_head != NULL); 20201 ASSERT(md_pbuf != NULL); 20202 ASSERT(md_pbuf_nxt == NULL); 20203 ASSERT(pbuf_idx != -1); 20204 ASSERT(pbuf_idx_nxt == -1); 20205 ASSERT(*usable > 0); 20206 20207 /* 20208 * We spillover to the next payload buffer only 20209 * if all of the following is true: 20210 * 20211 * 1. There is not enough data on the current 20212 * payload buffer to make up `len', 20213 * 2. We are allowed to send `len', 20214 * 3. The next payload buffer length is large 20215 * enough to accomodate `spill'. 20216 */ 20217 if ((spill = len - *tail_unsent) > 0 && 20218 *usable >= len && 20219 MBLKL((*xmit_tail)->b_cont) >= spill && 20220 max_pld > 0) { 20221 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20222 if (md_pbuf_nxt == NULL) { 20223 TCP_STAT(tcps, tcp_mdt_allocfail); 20224 goto legacy_send; /* out_of_mem */ 20225 } 20226 20227 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20228 zc_cap != NULL) { 20229 if (!ip_md_zcopy_attr(mmd, NULL, 20230 zc_cap->ill_zerocopy_flags)) { 20231 freeb(md_pbuf_nxt); 20232 TCP_STAT(tcps, 20233 tcp_mdt_allocfail); 20234 /* out_of_mem */ 20235 goto legacy_send; 20236 } 20237 zcopy = B_TRUE; 20238 } 20239 20240 /* 20241 * See comments above on the first call to 20242 * mmd_addpldbuf for explanation on the panic. 20243 */ 20244 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20245 if (pbuf_idx_nxt < 0) { 20246 panic("tcp_multisend: " 20247 "next payload buffer logic error " 20248 "detected for tcp %p mmd %p " 20249 "pbuf %p (%d)\n", 20250 (void *)tcp, (void *)mmd, 20251 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20252 } 20253 20254 ASSERT(max_pld > 0); 20255 --max_pld; 20256 } else if (spill > 0) { 20257 /* 20258 * If there's a spillover, but the following 20259 * xmit_tail couldn't give us enough octets 20260 * to reach "len", then stop the current 20261 * Multidata creation and let the legacy 20262 * tcp_send() path take over. We don't want 20263 * to send the tiny segment as part of this 20264 * Multidata for performance reasons; instead, 20265 * we let the legacy path deal with grouping 20266 * it with the subsequent small mblks. 20267 */ 20268 if (*usable >= len && 20269 MBLKL((*xmit_tail)->b_cont) < spill) { 20270 max_pld = 0; 20271 break; /* done */ 20272 } 20273 20274 /* 20275 * We can't spillover, and we are near 20276 * the end of the current payload buffer, 20277 * so send what's left. 20278 */ 20279 ASSERT(*tail_unsent > 0); 20280 len = *tail_unsent; 20281 } 20282 20283 /* tail_unsent is negated if there is a spillover */ 20284 *tail_unsent -= len; 20285 *usable -= len; 20286 ASSERT(*usable >= 0); 20287 20288 if (*usable < mss) 20289 seg_len = *usable; 20290 /* 20291 * Sender SWS avoidance; see comments in tcp_send(); 20292 * everything else is the same, except that we only 20293 * do this here if there is no more data to be sent 20294 * following the current xmit_tail. We don't check 20295 * for 1-byte urgent data because we shouldn't get 20296 * here if TCP_URG_VALID is set. 20297 */ 20298 if (*usable > 0 && *usable < mss && 20299 ((md_pbuf_nxt == NULL && 20300 (*xmit_tail)->b_cont == NULL) || 20301 (md_pbuf_nxt != NULL && 20302 (*xmit_tail)->b_cont->b_cont == NULL)) && 20303 seg_len < (tcp->tcp_max_swnd >> 1) && 20304 (tcp->tcp_unsent - 20305 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20306 !tcp->tcp_zero_win_probe) { 20307 if ((*snxt + len) == tcp->tcp_snxt && 20308 (*snxt + len) == tcp->tcp_suna) { 20309 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20310 } 20311 done = B_TRUE; 20312 } 20313 20314 /* 20315 * Prime pump for IP's checksumming on our behalf; 20316 * include the adjustment for a source route if any. 20317 * Do this only for software/partial hardware checksum 20318 * offload, as this field gets zeroed out later for 20319 * the full hardware checksum offload case. 20320 */ 20321 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20322 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20323 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20324 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20325 } 20326 20327 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20328 *snxt += len; 20329 20330 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20331 /* 20332 * We set the PUSH bit only if TCP has no more buffered 20333 * data to be transmitted (or if sender SWS avoidance 20334 * takes place), as opposed to setting it for every 20335 * last packet in the burst. 20336 */ 20337 if (done || 20338 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20339 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20340 20341 /* 20342 * Set FIN bit if this is our last segment; snxt 20343 * already includes its length, and it will not 20344 * be adjusted after this point. 20345 */ 20346 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20347 *snxt == tcp->tcp_fss) { 20348 if (!tcp->tcp_fin_acked) { 20349 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20350 BUMP_MIB(&tcps->tcps_mib, 20351 tcpOutControl); 20352 } 20353 if (!tcp->tcp_fin_sent) { 20354 tcp->tcp_fin_sent = B_TRUE; 20355 /* 20356 * tcp state must be ESTABLISHED 20357 * in order for us to get here in 20358 * the first place. 20359 */ 20360 tcp->tcp_state = TCPS_FIN_WAIT_1; 20361 20362 /* 20363 * Upon returning from this routine, 20364 * tcp_wput_data() will set tcp_snxt 20365 * to be equal to snxt + tcp_fin_sent. 20366 * This is essentially the same as 20367 * setting it to tcp_fss + 1. 20368 */ 20369 } 20370 } 20371 20372 tcp->tcp_last_sent_len = (ushort_t)len; 20373 20374 len += tcp_hdr_len; 20375 if (tcp->tcp_ipversion == IPV4_VERSION) 20376 tcp->tcp_ipha->ipha_length = htons(len); 20377 else 20378 tcp->tcp_ip6h->ip6_plen = htons(len - 20379 ((char *)&tcp->tcp_ip6h[1] - 20380 tcp->tcp_iphc)); 20381 20382 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20383 20384 /* setup header fragment */ 20385 PDESC_HDR_ADD(pkt_info, 20386 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20387 tcp->tcp_mdt_hdr_head, /* head room */ 20388 tcp_hdr_len, /* len */ 20389 tcp->tcp_mdt_hdr_tail); /* tail room */ 20390 20391 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20392 hdr_frag_sz); 20393 ASSERT(MBLKIN(md_hbuf, 20394 (pkt_info->hdr_base - md_hbuf->b_rptr), 20395 PDESC_HDRSIZE(pkt_info))); 20396 20397 /* setup first payload fragment */ 20398 PDESC_PLD_INIT(pkt_info); 20399 PDESC_PLD_SPAN_ADD(pkt_info, 20400 pbuf_idx, /* index */ 20401 md_pbuf->b_rptr + cur_pld_off, /* start */ 20402 tcp->tcp_last_sent_len); /* len */ 20403 20404 /* create a split-packet in case of a spillover */ 20405 if (md_pbuf_nxt != NULL) { 20406 ASSERT(spill > 0); 20407 ASSERT(pbuf_idx_nxt > pbuf_idx); 20408 ASSERT(!add_buffer); 20409 20410 md_pbuf = md_pbuf_nxt; 20411 md_pbuf_nxt = NULL; 20412 pbuf_idx = pbuf_idx_nxt; 20413 pbuf_idx_nxt = -1; 20414 cur_pld_off = spill; 20415 20416 /* trim out first payload fragment */ 20417 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20418 20419 /* setup second payload fragment */ 20420 PDESC_PLD_SPAN_ADD(pkt_info, 20421 pbuf_idx, /* index */ 20422 md_pbuf->b_rptr, /* start */ 20423 spill); /* len */ 20424 20425 if ((*xmit_tail)->b_next == NULL) { 20426 /* 20427 * Store the lbolt used for RTT 20428 * estimation. We can only record one 20429 * timestamp per mblk so we do it when 20430 * we reach the end of the payload 20431 * buffer. Also we only take a new 20432 * timestamp sample when the previous 20433 * timed data from the same mblk has 20434 * been ack'ed. 20435 */ 20436 (*xmit_tail)->b_prev = local_time; 20437 (*xmit_tail)->b_next = 20438 (mblk_t *)(uintptr_t)first_snxt; 20439 } 20440 20441 first_snxt = *snxt - spill; 20442 20443 /* 20444 * Advance xmit_tail; usable could be 0 by 20445 * the time we got here, but we made sure 20446 * above that we would only spillover to 20447 * the next data block if usable includes 20448 * the spilled-over amount prior to the 20449 * subtraction. Therefore, we are sure 20450 * that xmit_tail->b_cont can't be NULL. 20451 */ 20452 ASSERT((*xmit_tail)->b_cont != NULL); 20453 *xmit_tail = (*xmit_tail)->b_cont; 20454 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20455 (uintptr_t)INT_MAX); 20456 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20457 } else { 20458 cur_pld_off += tcp->tcp_last_sent_len; 20459 } 20460 20461 /* 20462 * Fill in the header using the template header, and 20463 * add options such as time-stamp, ECN and/or SACK, 20464 * as needed. 20465 */ 20466 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20467 (clock_t)local_time, num_sack_blk); 20468 20469 /* take care of some IP header businesses */ 20470 if (af == AF_INET) { 20471 ipha = (ipha_t *)pkt_info->hdr_rptr; 20472 20473 ASSERT(OK_32PTR((uchar_t *)ipha)); 20474 ASSERT(PDESC_HDRL(pkt_info) >= 20475 IP_SIMPLE_HDR_LENGTH); 20476 ASSERT(ipha->ipha_version_and_hdr_length == 20477 IP_SIMPLE_HDR_VERSION); 20478 20479 /* 20480 * Assign ident value for current packet; see 20481 * related comments in ip_wput_ire() about the 20482 * contract private interface with clustering 20483 * group. 20484 */ 20485 clusterwide = B_FALSE; 20486 if (cl_inet_ipident != NULL) { 20487 ASSERT(cl_inet_isclusterwide != NULL); 20488 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20489 AF_INET, 20490 (uint8_t *)(uintptr_t)src)) { 20491 ipha->ipha_ident = 20492 (*cl_inet_ipident) 20493 (IPPROTO_IP, AF_INET, 20494 (uint8_t *)(uintptr_t)src, 20495 (uint8_t *)(uintptr_t)dst); 20496 clusterwide = B_TRUE; 20497 } 20498 } 20499 20500 if (!clusterwide) { 20501 ipha->ipha_ident = (uint16_t) 20502 atomic_add_32_nv( 20503 &ire->ire_ident, 1); 20504 } 20505 #ifndef _BIG_ENDIAN 20506 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20507 (ipha->ipha_ident >> 8); 20508 #endif 20509 } else { 20510 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20511 20512 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20513 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20514 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20515 ASSERT(PDESC_HDRL(pkt_info) >= 20516 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20517 TCP_CHECKSUM_SIZE)); 20518 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20519 20520 if (tcp->tcp_ip_forward_progress) { 20521 rconfirm = B_TRUE; 20522 tcp->tcp_ip_forward_progress = B_FALSE; 20523 } 20524 } 20525 20526 /* at least one payload span, and at most two */ 20527 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20528 20529 /* add the packet descriptor to Multidata */ 20530 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20531 KM_NOSLEEP)) == NULL) { 20532 /* 20533 * Any failure other than ENOMEM indicates 20534 * that we have passed in invalid pkt_info 20535 * or parameters to mmd_addpdesc, which must 20536 * not happen. 20537 * 20538 * EINVAL is a result of failure on boundary 20539 * checks against the pkt_info contents. It 20540 * should not happen, and we panic because 20541 * either there's horrible heap corruption, 20542 * and/or programming mistake. 20543 */ 20544 if (err != ENOMEM) { 20545 cmn_err(CE_PANIC, "tcp_multisend: " 20546 "pdesc logic error detected for " 20547 "tcp %p mmd %p pinfo %p (%d)\n", 20548 (void *)tcp, (void *)mmd, 20549 (void *)pkt_info, err); 20550 } 20551 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20552 goto legacy_send; /* out_of_mem */ 20553 } 20554 ASSERT(pkt != NULL); 20555 20556 /* calculate IP header and TCP checksums */ 20557 if (af == AF_INET) { 20558 /* calculate pseudo-header checksum */ 20559 cksum = (dst >> 16) + (dst & 0xFFFF) + 20560 (src >> 16) + (src & 0xFFFF); 20561 20562 /* offset for TCP header checksum */ 20563 up = IPH_TCPH_CHECKSUMP(ipha, 20564 IP_SIMPLE_HDR_LENGTH); 20565 } else { 20566 up = (uint16_t *)&ip6h->ip6_src; 20567 20568 /* calculate pseudo-header checksum */ 20569 cksum = up[0] + up[1] + up[2] + up[3] + 20570 up[4] + up[5] + up[6] + up[7] + 20571 up[8] + up[9] + up[10] + up[11] + 20572 up[12] + up[13] + up[14] + up[15]; 20573 20574 /* Fold the initial sum */ 20575 cksum = (cksum & 0xffff) + (cksum >> 16); 20576 20577 up = (uint16_t *)(((uchar_t *)ip6h) + 20578 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20579 } 20580 20581 if (hwcksum_flags & HCK_FULLCKSUM) { 20582 /* clear checksum field for hardware */ 20583 *up = 0; 20584 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20585 uint32_t sum; 20586 20587 /* pseudo-header checksumming */ 20588 sum = *up + cksum + IP_TCP_CSUM_COMP; 20589 sum = (sum & 0xFFFF) + (sum >> 16); 20590 *up = (sum & 0xFFFF) + (sum >> 16); 20591 } else { 20592 /* software checksumming */ 20593 TCP_STAT(tcps, tcp_out_sw_cksum); 20594 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20595 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20596 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20597 cksum + IP_TCP_CSUM_COMP); 20598 if (*up == 0) 20599 *up = 0xFFFF; 20600 } 20601 20602 /* IPv4 header checksum */ 20603 if (af == AF_INET) { 20604 ipha->ipha_fragment_offset_and_flags |= 20605 (uint32_t)htons(ire->ire_frag_flag); 20606 20607 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20608 ipha->ipha_hdr_checksum = 0; 20609 } else { 20610 IP_HDR_CKSUM(ipha, cksum, 20611 ((uint32_t *)ipha)[0], 20612 ((uint16_t *)ipha)[4]); 20613 } 20614 } 20615 20616 if (af == AF_INET && 20617 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20618 af == AF_INET6 && 20619 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20620 /* build header(IP/TCP) mblk for this segment */ 20621 if ((mp = dupb(md_hbuf)) == NULL) 20622 goto legacy_send; 20623 20624 mp->b_rptr = pkt_info->hdr_rptr; 20625 mp->b_wptr = pkt_info->hdr_wptr; 20626 20627 /* build payload mblk for this segment */ 20628 if ((mp1 = dupb(*xmit_tail)) == NULL) { 20629 freemsg(mp); 20630 goto legacy_send; 20631 } 20632 mp1->b_wptr = md_pbuf->b_rptr + cur_pld_off; 20633 mp1->b_rptr = mp1->b_wptr - 20634 tcp->tcp_last_sent_len; 20635 linkb(mp, mp1); 20636 20637 pld_start = mp1->b_rptr; 20638 20639 if (af == AF_INET) { 20640 DTRACE_PROBE4( 20641 ip4__physical__out__start, 20642 ill_t *, NULL, 20643 ill_t *, ill, 20644 ipha_t *, ipha, 20645 mblk_t *, mp); 20646 FW_HOOKS( 20647 ipst->ips_ip4_physical_out_event, 20648 ipst->ips_ipv4firewall_physical_out, 20649 NULL, ill, ipha, mp, mp, ipst); 20650 DTRACE_PROBE1( 20651 ip4__physical__out__end, 20652 mblk_t *, mp); 20653 } else { 20654 DTRACE_PROBE4( 20655 ip6__physical__out_start, 20656 ill_t *, NULL, 20657 ill_t *, ill, 20658 ip6_t *, ip6h, 20659 mblk_t *, mp); 20660 FW_HOOKS6( 20661 ipst->ips_ip6_physical_out_event, 20662 ipst->ips_ipv6firewall_physical_out, 20663 NULL, ill, ip6h, mp, mp, ipst); 20664 DTRACE_PROBE1( 20665 ip6__physical__out__end, 20666 mblk_t *, mp); 20667 } 20668 20669 if (buf_trunked && mp != NULL) { 20670 /* 20671 * Need to pass it to normal path. 20672 */ 20673 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20674 } else if (mp == NULL || 20675 mp->b_rptr != pkt_info->hdr_rptr || 20676 mp->b_wptr != pkt_info->hdr_wptr || 20677 (mp1 = mp->b_cont) == NULL || 20678 mp1->b_rptr != pld_start || 20679 mp1->b_wptr != pld_start + 20680 tcp->tcp_last_sent_len || 20681 mp1->b_cont != NULL) { 20682 /* 20683 * Need to pass all packets of this 20684 * buffer to normal path, either when 20685 * packet is blocked, or when boundary 20686 * of header buffer or payload buffer 20687 * has been changed by FW_HOOKS[6]. 20688 */ 20689 buf_trunked = B_TRUE; 20690 if (md_mp_head != NULL) { 20691 err = (intptr_t)rmvb(md_mp_head, 20692 md_mp); 20693 if (err == 0) 20694 md_mp_head = NULL; 20695 } 20696 20697 /* send down what we've got so far */ 20698 if (md_mp_head != NULL) { 20699 tcp_multisend_data(tcp, ire, 20700 ill, md_mp_head, obsegs, 20701 obbytes, &rconfirm); 20702 } 20703 md_mp_head = NULL; 20704 20705 if (mp != NULL) 20706 CALL_IP_WPUT(tcp->tcp_connp, 20707 q, mp); 20708 20709 mp1 = fw_mp_head; 20710 do { 20711 mp = mp1; 20712 mp1 = mp1->b_next; 20713 mp->b_next = NULL; 20714 mp->b_prev = NULL; 20715 CALL_IP_WPUT(tcp->tcp_connp, 20716 q, mp); 20717 } while (mp1 != NULL); 20718 20719 fw_mp_head = NULL; 20720 } else { 20721 if (fw_mp_head == NULL) 20722 fw_mp_head = mp; 20723 else 20724 fw_mp_head->b_prev->b_next = mp; 20725 fw_mp_head->b_prev = mp; 20726 } 20727 } 20728 20729 /* advance header offset */ 20730 cur_hdr_off += hdr_frag_sz; 20731 20732 obbytes += tcp->tcp_last_sent_len; 20733 ++obsegs; 20734 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20735 *tail_unsent > 0); 20736 20737 if ((*xmit_tail)->b_next == NULL) { 20738 /* 20739 * Store the lbolt used for RTT estimation. We can only 20740 * record one timestamp per mblk so we do it when we 20741 * reach the end of the payload buffer. Also we only 20742 * take a new timestamp sample when the previous timed 20743 * data from the same mblk has been ack'ed. 20744 */ 20745 (*xmit_tail)->b_prev = local_time; 20746 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20747 } 20748 20749 ASSERT(*tail_unsent >= 0); 20750 if (*tail_unsent > 0) { 20751 /* 20752 * We got here because we broke out of the above 20753 * loop due to of one of the following cases: 20754 * 20755 * 1. len < adjusted MSS (i.e. small), 20756 * 2. Sender SWS avoidance, 20757 * 3. max_pld is zero. 20758 * 20759 * We are done for this Multidata, so trim our 20760 * last payload buffer (if any) accordingly. 20761 */ 20762 if (md_pbuf != NULL) 20763 md_pbuf->b_wptr -= *tail_unsent; 20764 } else if (*usable > 0) { 20765 *xmit_tail = (*xmit_tail)->b_cont; 20766 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20767 (uintptr_t)INT_MAX); 20768 *tail_unsent = (int)MBLKL(*xmit_tail); 20769 add_buffer = B_TRUE; 20770 } 20771 20772 while (fw_mp_head) { 20773 mp = fw_mp_head; 20774 fw_mp_head = fw_mp_head->b_next; 20775 mp->b_prev = mp->b_next = NULL; 20776 freemsg(mp); 20777 } 20778 if (buf_trunked) { 20779 TCP_STAT(tcps, tcp_mdt_discarded); 20780 freeb(md_mp); 20781 buf_trunked = B_FALSE; 20782 } 20783 } while (!done && *usable > 0 && num_burst_seg > 0 && 20784 (tcp_mdt_chain || max_pld > 0)); 20785 20786 if (md_mp_head != NULL) { 20787 /* send everything down */ 20788 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20789 &rconfirm); 20790 } 20791 20792 #undef PREP_NEW_MULTIDATA 20793 #undef PREP_NEW_PBUF 20794 #undef IPVER 20795 20796 IRE_REFRELE(ire); 20797 return (0); 20798 } 20799 20800 /* 20801 * A wrapper function for sending one or more Multidata messages down to 20802 * the module below ip; this routine does not release the reference of the 20803 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20804 */ 20805 static void 20806 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20807 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20808 { 20809 uint64_t delta; 20810 nce_t *nce; 20811 tcp_stack_t *tcps = tcp->tcp_tcps; 20812 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20813 20814 ASSERT(ire != NULL && ill != NULL); 20815 ASSERT(ire->ire_stq != NULL); 20816 ASSERT(md_mp_head != NULL); 20817 ASSERT(rconfirm != NULL); 20818 20819 /* adjust MIBs and IRE timestamp */ 20820 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 20821 tcp->tcp_obsegs += obsegs; 20822 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20823 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20824 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20825 20826 if (tcp->tcp_ipversion == IPV4_VERSION) { 20827 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20828 } else { 20829 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20830 } 20831 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20832 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20833 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20834 20835 ire->ire_ob_pkt_count += obsegs; 20836 if (ire->ire_ipif != NULL) 20837 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20838 ire->ire_last_used_time = lbolt; 20839 20840 /* send it down */ 20841 putnext(ire->ire_stq, md_mp_head); 20842 20843 /* we're done for TCP/IPv4 */ 20844 if (tcp->tcp_ipversion == IPV4_VERSION) 20845 return; 20846 20847 nce = ire->ire_nce; 20848 20849 ASSERT(nce != NULL); 20850 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20851 ASSERT(nce->nce_state != ND_INCOMPLETE); 20852 20853 /* reachability confirmation? */ 20854 if (*rconfirm) { 20855 nce->nce_last = TICK_TO_MSEC(lbolt64); 20856 if (nce->nce_state != ND_REACHABLE) { 20857 mutex_enter(&nce->nce_lock); 20858 nce->nce_state = ND_REACHABLE; 20859 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20860 mutex_exit(&nce->nce_lock); 20861 (void) untimeout(nce->nce_timeout_id); 20862 if (ip_debug > 2) { 20863 /* ip1dbg */ 20864 pr_addr_dbg("tcp_multisend_data: state " 20865 "for %s changed to REACHABLE\n", 20866 AF_INET6, &ire->ire_addr_v6); 20867 } 20868 } 20869 /* reset transport reachability confirmation */ 20870 *rconfirm = B_FALSE; 20871 } 20872 20873 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20874 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20875 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20876 20877 if (delta > (uint64_t)ill->ill_reachable_time) { 20878 mutex_enter(&nce->nce_lock); 20879 switch (nce->nce_state) { 20880 case ND_REACHABLE: 20881 case ND_STALE: 20882 /* 20883 * ND_REACHABLE is identical to ND_STALE in this 20884 * specific case. If reachable time has expired for 20885 * this neighbor (delta is greater than reachable 20886 * time), conceptually, the neighbor cache is no 20887 * longer in REACHABLE state, but already in STALE 20888 * state. So the correct transition here is to 20889 * ND_DELAY. 20890 */ 20891 nce->nce_state = ND_DELAY; 20892 mutex_exit(&nce->nce_lock); 20893 NDP_RESTART_TIMER(nce, 20894 ipst->ips_delay_first_probe_time); 20895 if (ip_debug > 3) { 20896 /* ip2dbg */ 20897 pr_addr_dbg("tcp_multisend_data: state " 20898 "for %s changed to DELAY\n", 20899 AF_INET6, &ire->ire_addr_v6); 20900 } 20901 break; 20902 case ND_DELAY: 20903 case ND_PROBE: 20904 mutex_exit(&nce->nce_lock); 20905 /* Timers have already started */ 20906 break; 20907 case ND_UNREACHABLE: 20908 /* 20909 * ndp timer has detected that this nce is 20910 * unreachable and initiated deleting this nce 20911 * and all its associated IREs. This is a race 20912 * where we found the ire before it was deleted 20913 * and have just sent out a packet using this 20914 * unreachable nce. 20915 */ 20916 mutex_exit(&nce->nce_lock); 20917 break; 20918 default: 20919 ASSERT(0); 20920 } 20921 } 20922 } 20923 20924 /* 20925 * Derived from tcp_send_data(). 20926 */ 20927 static void 20928 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20929 int num_lso_seg) 20930 { 20931 ipha_t *ipha; 20932 mblk_t *ire_fp_mp; 20933 uint_t ire_fp_mp_len; 20934 uint32_t hcksum_txflags = 0; 20935 ipaddr_t src; 20936 ipaddr_t dst; 20937 uint32_t cksum; 20938 uint16_t *up; 20939 tcp_stack_t *tcps = tcp->tcp_tcps; 20940 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20941 20942 ASSERT(DB_TYPE(mp) == M_DATA); 20943 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20944 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20945 ASSERT(tcp->tcp_connp != NULL); 20946 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20947 20948 ipha = (ipha_t *)mp->b_rptr; 20949 src = ipha->ipha_src; 20950 dst = ipha->ipha_dst; 20951 20952 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20953 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20954 num_lso_seg); 20955 #ifndef _BIG_ENDIAN 20956 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20957 #endif 20958 if (tcp->tcp_snd_zcopy_aware) { 20959 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20960 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20961 mp = tcp_zcopy_disable(tcp, mp); 20962 } 20963 20964 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20965 ASSERT(ill->ill_hcksum_capab != NULL); 20966 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20967 } 20968 20969 /* 20970 * Since the TCP checksum should be recalculated by h/w, we can just 20971 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20972 * pseudo-header checksum for HCK_PARTIALCKSUM. 20973 * The partial pseudo-header excludes TCP length, that was calculated 20974 * in tcp_send(), so to zero *up before further processing. 20975 */ 20976 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20977 20978 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20979 *up = 0; 20980 20981 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20982 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20983 20984 /* 20985 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 20986 */ 20987 DB_LSOFLAGS(mp) |= HW_LSO; 20988 DB_LSOMSS(mp) = mss; 20989 20990 ipha->ipha_fragment_offset_and_flags |= 20991 (uint32_t)htons(ire->ire_frag_flag); 20992 20993 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20994 ire_fp_mp_len = MBLKL(ire_fp_mp); 20995 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20996 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20997 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20998 20999 UPDATE_OB_PKT_COUNT(ire); 21000 ire->ire_last_used_time = lbolt; 21001 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21002 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21003 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21004 ntohs(ipha->ipha_length)); 21005 21006 if (ILL_DLS_CAPABLE(ill)) { 21007 /* 21008 * Send the packet directly to DLD, where it may be queued 21009 * depending on the availability of transmit resources at 21010 * the media layer. 21011 */ 21012 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 21013 } else { 21014 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 21015 DTRACE_PROBE4(ip4__physical__out__start, 21016 ill_t *, NULL, ill_t *, out_ill, 21017 ipha_t *, ipha, mblk_t *, mp); 21018 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21019 ipst->ips_ipv4firewall_physical_out, 21020 NULL, out_ill, ipha, mp, mp, ipst); 21021 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21022 if (mp != NULL) 21023 putnext(ire->ire_stq, mp); 21024 } 21025 } 21026 21027 /* 21028 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21029 * scheme, and returns one of the following: 21030 * 21031 * -1 = failed allocation. 21032 * 0 = success; burst count reached, or usable send window is too small, 21033 * and that we'd rather wait until later before sending again. 21034 * 1 = success; we are called from tcp_multisend(), and both usable send 21035 * window and tail_unsent are greater than the MDT threshold, and thus 21036 * Multidata Transmit should be used instead. 21037 */ 21038 static int 21039 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21040 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21041 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21042 const int mdt_thres) 21043 { 21044 int num_burst_seg = tcp->tcp_snd_burst; 21045 ire_t *ire = NULL; 21046 ill_t *ill = NULL; 21047 mblk_t *ire_fp_mp = NULL; 21048 uint_t ire_fp_mp_len = 0; 21049 int num_lso_seg = 1; 21050 uint_t lso_usable; 21051 boolean_t do_lso_send = B_FALSE; 21052 tcp_stack_t *tcps = tcp->tcp_tcps; 21053 21054 /* 21055 * Check LSO capability before any further work. And the similar check 21056 * need to be done in for(;;) loop. 21057 * LSO will be deployed when therer is more than one mss of available 21058 * data and a burst transmission is allowed. 21059 */ 21060 if (tcp->tcp_lso && 21061 (tcp->tcp_valid_bits == 0 || 21062 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21063 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21064 /* 21065 * Try to find usable IRE/ILL and do basic check to the ILL. 21066 */ 21067 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21068 /* 21069 * Enable LSO with this transmission. 21070 * Since IRE has been hold in 21071 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21072 * should be called before return. 21073 */ 21074 do_lso_send = B_TRUE; 21075 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21076 ire_fp_mp_len = MBLKL(ire_fp_mp); 21077 /* Round up to multiple of 4 */ 21078 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21079 } else { 21080 do_lso_send = B_FALSE; 21081 ill = NULL; 21082 } 21083 } 21084 21085 for (;;) { 21086 struct datab *db; 21087 tcph_t *tcph; 21088 uint32_t sum; 21089 mblk_t *mp, *mp1; 21090 uchar_t *rptr; 21091 int len; 21092 21093 /* 21094 * If we're called by tcp_multisend(), and the amount of 21095 * sendable data as well as the size of current xmit_tail 21096 * is beyond the MDT threshold, return to the caller and 21097 * let the large data transmit be done using MDT. 21098 */ 21099 if (*usable > 0 && *usable > mdt_thres && 21100 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21101 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21102 ASSERT(tcp->tcp_mdt); 21103 return (1); /* success; do large send */ 21104 } 21105 21106 if (num_burst_seg == 0) 21107 break; /* success; burst count reached */ 21108 21109 /* 21110 * Calculate the maximum payload length we can send in *one* 21111 * time. 21112 */ 21113 if (do_lso_send) { 21114 /* 21115 * Check whether need to do LSO any more. 21116 */ 21117 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21118 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21119 lso_usable = MIN(lso_usable, 21120 num_burst_seg * mss); 21121 21122 num_lso_seg = lso_usable / mss; 21123 if (lso_usable % mss) { 21124 num_lso_seg++; 21125 tcp->tcp_last_sent_len = (ushort_t) 21126 (lso_usable % mss); 21127 } else { 21128 tcp->tcp_last_sent_len = (ushort_t)mss; 21129 } 21130 } else { 21131 do_lso_send = B_FALSE; 21132 num_lso_seg = 1; 21133 lso_usable = mss; 21134 } 21135 } 21136 21137 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21138 21139 /* 21140 * Adjust num_burst_seg here. 21141 */ 21142 num_burst_seg -= num_lso_seg; 21143 21144 len = mss; 21145 if (len > *usable) { 21146 ASSERT(do_lso_send == B_FALSE); 21147 21148 len = *usable; 21149 if (len <= 0) { 21150 /* Terminate the loop */ 21151 break; /* success; too small */ 21152 } 21153 /* 21154 * Sender silly-window avoidance. 21155 * Ignore this if we are going to send a 21156 * zero window probe out. 21157 * 21158 * TODO: force data into microscopic window? 21159 * ==> (!pushed || (unsent > usable)) 21160 */ 21161 if (len < (tcp->tcp_max_swnd >> 1) && 21162 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21163 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21164 len == 1) && (! tcp->tcp_zero_win_probe)) { 21165 /* 21166 * If the retransmit timer is not running 21167 * we start it so that we will retransmit 21168 * in the case when the the receiver has 21169 * decremented the window. 21170 */ 21171 if (*snxt == tcp->tcp_snxt && 21172 *snxt == tcp->tcp_suna) { 21173 /* 21174 * We are not supposed to send 21175 * anything. So let's wait a little 21176 * bit longer before breaking SWS 21177 * avoidance. 21178 * 21179 * What should the value be? 21180 * Suggestion: MAX(init rexmit time, 21181 * tcp->tcp_rto) 21182 */ 21183 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21184 } 21185 break; /* success; too small */ 21186 } 21187 } 21188 21189 tcph = tcp->tcp_tcph; 21190 21191 /* 21192 * The reason to adjust len here is that we need to set flags 21193 * and calculate checksum. 21194 */ 21195 if (do_lso_send) 21196 len = lso_usable; 21197 21198 *usable -= len; /* Approximate - can be adjusted later */ 21199 if (*usable > 0) 21200 tcph->th_flags[0] = TH_ACK; 21201 else 21202 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21203 21204 /* 21205 * Prime pump for IP's checksumming on our behalf 21206 * Include the adjustment for a source route if any. 21207 */ 21208 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21209 sum = (sum >> 16) + (sum & 0xFFFF); 21210 U16_TO_ABE16(sum, tcph->th_sum); 21211 21212 U32_TO_ABE32(*snxt, tcph->th_seq); 21213 21214 /* 21215 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21216 * set. For the case when TCP_FSS_VALID is the only valid 21217 * bit (normal active close), branch off only when we think 21218 * that the FIN flag needs to be set. Note for this case, 21219 * that (snxt + len) may not reflect the actual seg_len, 21220 * as len may be further reduced in tcp_xmit_mp(). If len 21221 * gets modified, we will end up here again. 21222 */ 21223 if (tcp->tcp_valid_bits != 0 && 21224 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21225 ((*snxt + len) == tcp->tcp_fss))) { 21226 uchar_t *prev_rptr; 21227 uint32_t prev_snxt = tcp->tcp_snxt; 21228 21229 if (*tail_unsent == 0) { 21230 ASSERT((*xmit_tail)->b_cont != NULL); 21231 *xmit_tail = (*xmit_tail)->b_cont; 21232 prev_rptr = (*xmit_tail)->b_rptr; 21233 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21234 (*xmit_tail)->b_rptr); 21235 } else { 21236 prev_rptr = (*xmit_tail)->b_rptr; 21237 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21238 *tail_unsent; 21239 } 21240 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21241 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21242 /* Restore tcp_snxt so we get amount sent right. */ 21243 tcp->tcp_snxt = prev_snxt; 21244 if (prev_rptr == (*xmit_tail)->b_rptr) { 21245 /* 21246 * If the previous timestamp is still in use, 21247 * don't stomp on it. 21248 */ 21249 if ((*xmit_tail)->b_next == NULL) { 21250 (*xmit_tail)->b_prev = local_time; 21251 (*xmit_tail)->b_next = 21252 (mblk_t *)(uintptr_t)(*snxt); 21253 } 21254 } else 21255 (*xmit_tail)->b_rptr = prev_rptr; 21256 21257 if (mp == NULL) { 21258 if (ire != NULL) 21259 IRE_REFRELE(ire); 21260 return (-1); 21261 } 21262 mp1 = mp->b_cont; 21263 21264 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21265 tcp->tcp_last_sent_len = (ushort_t)len; 21266 while (mp1->b_cont) { 21267 *xmit_tail = (*xmit_tail)->b_cont; 21268 (*xmit_tail)->b_prev = local_time; 21269 (*xmit_tail)->b_next = 21270 (mblk_t *)(uintptr_t)(*snxt); 21271 mp1 = mp1->b_cont; 21272 } 21273 *snxt += len; 21274 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21275 BUMP_LOCAL(tcp->tcp_obsegs); 21276 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21277 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21278 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21279 tcp_send_data(tcp, q, mp); 21280 continue; 21281 } 21282 21283 *snxt += len; /* Adjust later if we don't send all of len */ 21284 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21285 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21286 21287 if (*tail_unsent) { 21288 /* Are the bytes above us in flight? */ 21289 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21290 if (rptr != (*xmit_tail)->b_rptr) { 21291 *tail_unsent -= len; 21292 if (len <= mss) /* LSO is unusable */ 21293 tcp->tcp_last_sent_len = (ushort_t)len; 21294 len += tcp_hdr_len; 21295 if (tcp->tcp_ipversion == IPV4_VERSION) 21296 tcp->tcp_ipha->ipha_length = htons(len); 21297 else 21298 tcp->tcp_ip6h->ip6_plen = 21299 htons(len - 21300 ((char *)&tcp->tcp_ip6h[1] - 21301 tcp->tcp_iphc)); 21302 mp = dupb(*xmit_tail); 21303 if (mp == NULL) { 21304 if (ire != NULL) 21305 IRE_REFRELE(ire); 21306 return (-1); /* out_of_mem */ 21307 } 21308 mp->b_rptr = rptr; 21309 /* 21310 * If the old timestamp is no longer in use, 21311 * sample a new timestamp now. 21312 */ 21313 if ((*xmit_tail)->b_next == NULL) { 21314 (*xmit_tail)->b_prev = local_time; 21315 (*xmit_tail)->b_next = 21316 (mblk_t *)(uintptr_t)(*snxt-len); 21317 } 21318 goto must_alloc; 21319 } 21320 } else { 21321 *xmit_tail = (*xmit_tail)->b_cont; 21322 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21323 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21324 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21325 (*xmit_tail)->b_rptr); 21326 } 21327 21328 (*xmit_tail)->b_prev = local_time; 21329 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21330 21331 *tail_unsent -= len; 21332 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21333 tcp->tcp_last_sent_len = (ushort_t)len; 21334 21335 len += tcp_hdr_len; 21336 if (tcp->tcp_ipversion == IPV4_VERSION) 21337 tcp->tcp_ipha->ipha_length = htons(len); 21338 else 21339 tcp->tcp_ip6h->ip6_plen = htons(len - 21340 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21341 21342 mp = dupb(*xmit_tail); 21343 if (mp == NULL) { 21344 if (ire != NULL) 21345 IRE_REFRELE(ire); 21346 return (-1); /* out_of_mem */ 21347 } 21348 21349 len = tcp_hdr_len; 21350 /* 21351 * There are four reasons to allocate a new hdr mblk: 21352 * 1) The bytes above us are in use by another packet 21353 * 2) We don't have good alignment 21354 * 3) The mblk is being shared 21355 * 4) We don't have enough room for a header 21356 */ 21357 rptr = mp->b_rptr - len; 21358 if (!OK_32PTR(rptr) || 21359 ((db = mp->b_datap), db->db_ref != 2) || 21360 rptr < db->db_base + ire_fp_mp_len) { 21361 /* NOTE: we assume allocb returns an OK_32PTR */ 21362 21363 must_alloc:; 21364 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21365 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21366 if (mp1 == NULL) { 21367 freemsg(mp); 21368 if (ire != NULL) 21369 IRE_REFRELE(ire); 21370 return (-1); /* out_of_mem */ 21371 } 21372 mp1->b_cont = mp; 21373 mp = mp1; 21374 /* Leave room for Link Level header */ 21375 len = tcp_hdr_len; 21376 rptr = 21377 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21378 mp->b_wptr = &rptr[len]; 21379 } 21380 21381 /* 21382 * Fill in the header using the template header, and add 21383 * options such as time-stamp, ECN and/or SACK, as needed. 21384 */ 21385 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21386 21387 mp->b_rptr = rptr; 21388 21389 if (*tail_unsent) { 21390 int spill = *tail_unsent; 21391 21392 mp1 = mp->b_cont; 21393 if (mp1 == NULL) 21394 mp1 = mp; 21395 21396 /* 21397 * If we're a little short, tack on more mblks until 21398 * there is no more spillover. 21399 */ 21400 while (spill < 0) { 21401 mblk_t *nmp; 21402 int nmpsz; 21403 21404 nmp = (*xmit_tail)->b_cont; 21405 nmpsz = MBLKL(nmp); 21406 21407 /* 21408 * Excess data in mblk; can we split it? 21409 * If MDT is enabled for the connection, 21410 * keep on splitting as this is a transient 21411 * send path. 21412 */ 21413 if (!do_lso_send && !tcp->tcp_mdt && 21414 (spill + nmpsz > 0)) { 21415 /* 21416 * Don't split if stream head was 21417 * told to break up larger writes 21418 * into smaller ones. 21419 */ 21420 if (tcp->tcp_maxpsz > 0) 21421 break; 21422 21423 /* 21424 * Next mblk is less than SMSS/2 21425 * rounded up to nearest 64-byte; 21426 * let it get sent as part of the 21427 * next segment. 21428 */ 21429 if (tcp->tcp_localnet && 21430 !tcp->tcp_cork && 21431 (nmpsz < roundup((mss >> 1), 64))) 21432 break; 21433 } 21434 21435 *xmit_tail = nmp; 21436 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21437 /* Stash for rtt use later */ 21438 (*xmit_tail)->b_prev = local_time; 21439 (*xmit_tail)->b_next = 21440 (mblk_t *)(uintptr_t)(*snxt - len); 21441 mp1->b_cont = dupb(*xmit_tail); 21442 mp1 = mp1->b_cont; 21443 21444 spill += nmpsz; 21445 if (mp1 == NULL) { 21446 *tail_unsent = spill; 21447 freemsg(mp); 21448 if (ire != NULL) 21449 IRE_REFRELE(ire); 21450 return (-1); /* out_of_mem */ 21451 } 21452 } 21453 21454 /* Trim back any surplus on the last mblk */ 21455 if (spill >= 0) { 21456 mp1->b_wptr -= spill; 21457 *tail_unsent = spill; 21458 } else { 21459 /* 21460 * We did not send everything we could in 21461 * order to remain within the b_cont limit. 21462 */ 21463 *usable -= spill; 21464 *snxt += spill; 21465 tcp->tcp_last_sent_len += spill; 21466 UPDATE_MIB(&tcps->tcps_mib, 21467 tcpOutDataBytes, spill); 21468 /* 21469 * Adjust the checksum 21470 */ 21471 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21472 sum += spill; 21473 sum = (sum >> 16) + (sum & 0xFFFF); 21474 U16_TO_ABE16(sum, tcph->th_sum); 21475 if (tcp->tcp_ipversion == IPV4_VERSION) { 21476 sum = ntohs( 21477 ((ipha_t *)rptr)->ipha_length) + 21478 spill; 21479 ((ipha_t *)rptr)->ipha_length = 21480 htons(sum); 21481 } else { 21482 sum = ntohs( 21483 ((ip6_t *)rptr)->ip6_plen) + 21484 spill; 21485 ((ip6_t *)rptr)->ip6_plen = 21486 htons(sum); 21487 } 21488 *tail_unsent = 0; 21489 } 21490 } 21491 if (tcp->tcp_ip_forward_progress) { 21492 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21493 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21494 tcp->tcp_ip_forward_progress = B_FALSE; 21495 } 21496 21497 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21498 if (do_lso_send) { 21499 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21500 num_lso_seg); 21501 tcp->tcp_obsegs += num_lso_seg; 21502 21503 TCP_STAT(tcps, tcp_lso_times); 21504 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21505 } else { 21506 tcp_send_data(tcp, q, mp); 21507 BUMP_LOCAL(tcp->tcp_obsegs); 21508 } 21509 } 21510 21511 if (ire != NULL) 21512 IRE_REFRELE(ire); 21513 return (0); 21514 } 21515 21516 /* Unlink and return any mblk that looks like it contains a MDT info */ 21517 static mblk_t * 21518 tcp_mdt_info_mp(mblk_t *mp) 21519 { 21520 mblk_t *prev_mp; 21521 21522 for (;;) { 21523 prev_mp = mp; 21524 /* no more to process? */ 21525 if ((mp = mp->b_cont) == NULL) 21526 break; 21527 21528 switch (DB_TYPE(mp)) { 21529 case M_CTL: 21530 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21531 continue; 21532 ASSERT(prev_mp != NULL); 21533 prev_mp->b_cont = mp->b_cont; 21534 mp->b_cont = NULL; 21535 return (mp); 21536 default: 21537 break; 21538 } 21539 } 21540 return (mp); 21541 } 21542 21543 /* MDT info update routine, called when IP notifies us about MDT */ 21544 static void 21545 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21546 { 21547 boolean_t prev_state; 21548 tcp_stack_t *tcps = tcp->tcp_tcps; 21549 21550 /* 21551 * IP is telling us to abort MDT on this connection? We know 21552 * this because the capability is only turned off when IP 21553 * encounters some pathological cases, e.g. link-layer change 21554 * where the new driver doesn't support MDT, or in situation 21555 * where MDT usage on the link-layer has been switched off. 21556 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21557 * if the link-layer doesn't support MDT, and if it does, it 21558 * will indicate that the feature is to be turned on. 21559 */ 21560 prev_state = tcp->tcp_mdt; 21561 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21562 if (!tcp->tcp_mdt && !first) { 21563 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21564 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21565 (void *)tcp->tcp_connp)); 21566 } 21567 21568 /* 21569 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21570 * so disable MDT otherwise. The checks are done here 21571 * and in tcp_wput_data(). 21572 */ 21573 if (tcp->tcp_mdt && 21574 (tcp->tcp_ipversion == IPV4_VERSION && 21575 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21576 (tcp->tcp_ipversion == IPV6_VERSION && 21577 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21578 tcp->tcp_mdt = B_FALSE; 21579 21580 if (tcp->tcp_mdt) { 21581 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21582 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21583 "version (%d), expected version is %d", 21584 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21585 tcp->tcp_mdt = B_FALSE; 21586 return; 21587 } 21588 21589 /* 21590 * We need the driver to be able to handle at least three 21591 * spans per packet in order for tcp MDT to be utilized. 21592 * The first is for the header portion, while the rest are 21593 * needed to handle a packet that straddles across two 21594 * virtually non-contiguous buffers; a typical tcp packet 21595 * therefore consists of only two spans. Note that we take 21596 * a zero as "don't care". 21597 */ 21598 if (mdt_capab->ill_mdt_span_limit > 0 && 21599 mdt_capab->ill_mdt_span_limit < 3) { 21600 tcp->tcp_mdt = B_FALSE; 21601 return; 21602 } 21603 21604 /* a zero means driver wants default value */ 21605 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21606 tcps->tcps_mdt_max_pbufs); 21607 if (tcp->tcp_mdt_max_pld == 0) 21608 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21609 21610 /* ensure 32-bit alignment */ 21611 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21612 mdt_capab->ill_mdt_hdr_head), 4); 21613 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21614 mdt_capab->ill_mdt_hdr_tail), 4); 21615 21616 if (!first && !prev_state) { 21617 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21618 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21619 (void *)tcp->tcp_connp)); 21620 } 21621 } 21622 } 21623 21624 /* Unlink and return any mblk that looks like it contains a LSO info */ 21625 static mblk_t * 21626 tcp_lso_info_mp(mblk_t *mp) 21627 { 21628 mblk_t *prev_mp; 21629 21630 for (;;) { 21631 prev_mp = mp; 21632 /* no more to process? */ 21633 if ((mp = mp->b_cont) == NULL) 21634 break; 21635 21636 switch (DB_TYPE(mp)) { 21637 case M_CTL: 21638 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21639 continue; 21640 ASSERT(prev_mp != NULL); 21641 prev_mp->b_cont = mp->b_cont; 21642 mp->b_cont = NULL; 21643 return (mp); 21644 default: 21645 break; 21646 } 21647 } 21648 21649 return (mp); 21650 } 21651 21652 /* LSO info update routine, called when IP notifies us about LSO */ 21653 static void 21654 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21655 { 21656 tcp_stack_t *tcps = tcp->tcp_tcps; 21657 21658 /* 21659 * IP is telling us to abort LSO on this connection? We know 21660 * this because the capability is only turned off when IP 21661 * encounters some pathological cases, e.g. link-layer change 21662 * where the new NIC/driver doesn't support LSO, or in situation 21663 * where LSO usage on the link-layer has been switched off. 21664 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21665 * if the link-layer doesn't support LSO, and if it does, it 21666 * will indicate that the feature is to be turned on. 21667 */ 21668 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21669 TCP_STAT(tcps, tcp_lso_enabled); 21670 21671 /* 21672 * We currently only support LSO on simple TCP/IPv4, 21673 * so disable LSO otherwise. The checks are done here 21674 * and in tcp_wput_data(). 21675 */ 21676 if (tcp->tcp_lso && 21677 (tcp->tcp_ipversion == IPV4_VERSION && 21678 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21679 (tcp->tcp_ipversion == IPV6_VERSION)) { 21680 tcp->tcp_lso = B_FALSE; 21681 TCP_STAT(tcps, tcp_lso_disabled); 21682 } else { 21683 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21684 lso_capab->ill_lso_max); 21685 } 21686 } 21687 21688 static void 21689 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21690 { 21691 conn_t *connp = tcp->tcp_connp; 21692 tcp_stack_t *tcps = tcp->tcp_tcps; 21693 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21694 21695 ASSERT(ire != NULL); 21696 21697 /* 21698 * We may be in the fastpath here, and although we essentially do 21699 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21700 * we try to keep things as brief as possible. After all, these 21701 * are only best-effort checks, and we do more thorough ones prior 21702 * to calling tcp_send()/tcp_multisend(). 21703 */ 21704 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21705 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21706 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21707 !(ire->ire_flags & RTF_MULTIRT) && 21708 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21709 CONN_IS_LSO_MD_FASTPATH(connp)) { 21710 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21711 /* Cache the result */ 21712 connp->conn_lso_ok = B_TRUE; 21713 21714 ASSERT(ill->ill_lso_capab != NULL); 21715 if (!ill->ill_lso_capab->ill_lso_on) { 21716 ill->ill_lso_capab->ill_lso_on = 1; 21717 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21718 "LSO for interface %s\n", (void *)connp, 21719 ill->ill_name)); 21720 } 21721 tcp_lso_update(tcp, ill->ill_lso_capab); 21722 } else if (ipst->ips_ip_multidata_outbound && 21723 ILL_MDT_CAPABLE(ill)) { 21724 /* Cache the result */ 21725 connp->conn_mdt_ok = B_TRUE; 21726 21727 ASSERT(ill->ill_mdt_capab != NULL); 21728 if (!ill->ill_mdt_capab->ill_mdt_on) { 21729 ill->ill_mdt_capab->ill_mdt_on = 1; 21730 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21731 "MDT for interface %s\n", (void *)connp, 21732 ill->ill_name)); 21733 } 21734 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21735 } 21736 } 21737 21738 /* 21739 * The goal is to reduce the number of generated tcp segments by 21740 * setting the maxpsz multiplier to 0; this will have an affect on 21741 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21742 * into each packet, up to SMSS bytes. Doing this reduces the number 21743 * of outbound segments and incoming ACKs, thus allowing for better 21744 * network and system performance. In contrast the legacy behavior 21745 * may result in sending less than SMSS size, because the last mblk 21746 * for some packets may have more data than needed to make up SMSS, 21747 * and the legacy code refused to "split" it. 21748 * 21749 * We apply the new behavior on following situations: 21750 * 21751 * 1) Loopback connections, 21752 * 2) Connections in which the remote peer is not on local subnet, 21753 * 3) Local subnet connections over the bge interface (see below). 21754 * 21755 * Ideally, we would like this behavior to apply for interfaces other 21756 * than bge. However, doing so would negatively impact drivers which 21757 * perform dynamic mapping and unmapping of DMA resources, which are 21758 * increased by setting the maxpsz multiplier to 0 (more mblks per 21759 * packet will be generated by tcp). The bge driver does not suffer 21760 * from this, as it copies the mblks into pre-mapped buffers, and 21761 * therefore does not require more I/O resources than before. 21762 * 21763 * Otherwise, this behavior is present on all network interfaces when 21764 * the destination endpoint is non-local, since reducing the number 21765 * of packets in general is good for the network. 21766 * 21767 * TODO We need to remove this hard-coded conditional for bge once 21768 * a better "self-tuning" mechanism, or a way to comprehend 21769 * the driver transmit strategy is devised. Until the solution 21770 * is found and well understood, we live with this hack. 21771 */ 21772 if (!tcp_static_maxpsz && 21773 (tcp->tcp_loopback || !tcp->tcp_localnet || 21774 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21775 /* override the default value */ 21776 tcp->tcp_maxpsz = 0; 21777 21778 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21779 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21780 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21781 } 21782 21783 /* set the stream head parameters accordingly */ 21784 (void) tcp_maxpsz_set(tcp, B_TRUE); 21785 } 21786 21787 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21788 static void 21789 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21790 { 21791 uchar_t fval = *mp->b_rptr; 21792 mblk_t *tail; 21793 queue_t *q = tcp->tcp_wq; 21794 21795 /* TODO: How should flush interact with urgent data? */ 21796 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21797 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21798 /* 21799 * Flush only data that has not yet been put on the wire. If 21800 * we flush data that we have already transmitted, life, as we 21801 * know it, may come to an end. 21802 */ 21803 tail = tcp->tcp_xmit_tail; 21804 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21805 tcp->tcp_xmit_tail_unsent = 0; 21806 tcp->tcp_unsent = 0; 21807 if (tail->b_wptr != tail->b_rptr) 21808 tail = tail->b_cont; 21809 if (tail) { 21810 mblk_t **excess = &tcp->tcp_xmit_head; 21811 for (;;) { 21812 mblk_t *mp1 = *excess; 21813 if (mp1 == tail) 21814 break; 21815 tcp->tcp_xmit_tail = mp1; 21816 tcp->tcp_xmit_last = mp1; 21817 excess = &mp1->b_cont; 21818 } 21819 *excess = NULL; 21820 tcp_close_mpp(&tail); 21821 if (tcp->tcp_snd_zcopy_aware) 21822 tcp_zcopy_notify(tcp); 21823 } 21824 /* 21825 * We have no unsent data, so unsent must be less than 21826 * tcp_xmit_lowater, so re-enable flow. 21827 */ 21828 mutex_enter(&tcp->tcp_non_sq_lock); 21829 if (tcp->tcp_flow_stopped) { 21830 tcp_clrqfull(tcp); 21831 } 21832 mutex_exit(&tcp->tcp_non_sq_lock); 21833 } 21834 /* 21835 * TODO: you can't just flush these, you have to increase rwnd for one 21836 * thing. For another, how should urgent data interact? 21837 */ 21838 if (fval & FLUSHR) { 21839 *mp->b_rptr = fval & ~FLUSHW; 21840 /* XXX */ 21841 qreply(q, mp); 21842 return; 21843 } 21844 freemsg(mp); 21845 } 21846 21847 /* 21848 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21849 * messages. 21850 */ 21851 static void 21852 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21853 { 21854 mblk_t *mp1; 21855 STRUCT_HANDLE(strbuf, sb); 21856 uint16_t port; 21857 queue_t *q = tcp->tcp_wq; 21858 in6_addr_t v6addr; 21859 ipaddr_t v4addr; 21860 uint32_t flowinfo = 0; 21861 int addrlen; 21862 21863 /* Make sure it is one of ours. */ 21864 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21865 case TI_GETMYNAME: 21866 case TI_GETPEERNAME: 21867 break; 21868 default: 21869 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21870 return; 21871 } 21872 switch (mi_copy_state(q, mp, &mp1)) { 21873 case -1: 21874 return; 21875 case MI_COPY_CASE(MI_COPY_IN, 1): 21876 break; 21877 case MI_COPY_CASE(MI_COPY_OUT, 1): 21878 /* Copy out the strbuf. */ 21879 mi_copyout(q, mp); 21880 return; 21881 case MI_COPY_CASE(MI_COPY_OUT, 2): 21882 /* All done. */ 21883 mi_copy_done(q, mp, 0); 21884 return; 21885 default: 21886 mi_copy_done(q, mp, EPROTO); 21887 return; 21888 } 21889 /* Check alignment of the strbuf */ 21890 if (!OK_32PTR(mp1->b_rptr)) { 21891 mi_copy_done(q, mp, EINVAL); 21892 return; 21893 } 21894 21895 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 21896 (void *)mp1->b_rptr); 21897 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21898 21899 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21900 mi_copy_done(q, mp, EINVAL); 21901 return; 21902 } 21903 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21904 case TI_GETMYNAME: 21905 if (tcp->tcp_family == AF_INET) { 21906 if (tcp->tcp_ipversion == IPV4_VERSION) { 21907 v4addr = tcp->tcp_ipha->ipha_src; 21908 } else { 21909 /* can't return an address in this case */ 21910 v4addr = 0; 21911 } 21912 } else { 21913 /* tcp->tcp_family == AF_INET6 */ 21914 if (tcp->tcp_ipversion == IPV4_VERSION) { 21915 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 21916 &v6addr); 21917 } else { 21918 v6addr = tcp->tcp_ip6h->ip6_src; 21919 } 21920 } 21921 port = tcp->tcp_lport; 21922 break; 21923 case TI_GETPEERNAME: 21924 if (tcp->tcp_family == AF_INET) { 21925 if (tcp->tcp_ipversion == IPV4_VERSION) { 21926 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 21927 v4addr); 21928 } else { 21929 /* can't return an address in this case */ 21930 v4addr = 0; 21931 } 21932 } else { 21933 /* tcp->tcp_family == AF_INET6) */ 21934 v6addr = tcp->tcp_remote_v6; 21935 if (tcp->tcp_ipversion == IPV6_VERSION) { 21936 /* 21937 * No flowinfo if tcp->tcp_ipversion is v4. 21938 * 21939 * flowinfo was already initialized to zero 21940 * where it was declared above, so only 21941 * set it if ipversion is v6. 21942 */ 21943 flowinfo = tcp->tcp_ip6h->ip6_vcf & 21944 ~IPV6_VERS_AND_FLOW_MASK; 21945 } 21946 } 21947 port = tcp->tcp_fport; 21948 break; 21949 default: 21950 mi_copy_done(q, mp, EPROTO); 21951 return; 21952 } 21953 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21954 if (!mp1) 21955 return; 21956 21957 if (tcp->tcp_family == AF_INET) { 21958 sin_t *sin; 21959 21960 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 21961 sin = (sin_t *)mp1->b_rptr; 21962 mp1->b_wptr = (uchar_t *)&sin[1]; 21963 *sin = sin_null; 21964 sin->sin_family = AF_INET; 21965 sin->sin_addr.s_addr = v4addr; 21966 sin->sin_port = port; 21967 } else { 21968 /* tcp->tcp_family == AF_INET6 */ 21969 sin6_t *sin6; 21970 21971 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 21972 sin6 = (sin6_t *)mp1->b_rptr; 21973 mp1->b_wptr = (uchar_t *)&sin6[1]; 21974 *sin6 = sin6_null; 21975 sin6->sin6_family = AF_INET6; 21976 sin6->sin6_flowinfo = flowinfo; 21977 sin6->sin6_addr = v6addr; 21978 sin6->sin6_port = port; 21979 } 21980 /* Copy out the address */ 21981 mi_copyout(q, mp); 21982 } 21983 21984 /* 21985 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21986 * messages. 21987 */ 21988 /* ARGSUSED */ 21989 static void 21990 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21991 { 21992 conn_t *connp = (conn_t *)arg; 21993 tcp_t *tcp = connp->conn_tcp; 21994 queue_t *q = tcp->tcp_wq; 21995 struct iocblk *iocp; 21996 tcp_stack_t *tcps = tcp->tcp_tcps; 21997 21998 ASSERT(DB_TYPE(mp) == M_IOCTL); 21999 /* 22000 * Try and ASSERT the minimum possible references on the 22001 * conn early enough. Since we are executing on write side, 22002 * the connection is obviously not detached and that means 22003 * there is a ref each for TCP and IP. Since we are behind 22004 * the squeue, the minimum references needed are 3. If the 22005 * conn is in classifier hash list, there should be an 22006 * extra ref for that (we check both the possibilities). 22007 */ 22008 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22009 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22010 22011 iocp = (struct iocblk *)mp->b_rptr; 22012 switch (iocp->ioc_cmd) { 22013 case TCP_IOC_DEFAULT_Q: 22014 /* Wants to be the default wq. */ 22015 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22016 iocp->ioc_error = EPERM; 22017 iocp->ioc_count = 0; 22018 mp->b_datap->db_type = M_IOCACK; 22019 qreply(q, mp); 22020 return; 22021 } 22022 tcp_def_q_set(tcp, mp); 22023 return; 22024 case _SIOCSOCKFALLBACK: 22025 /* 22026 * Either sockmod is about to be popped and the socket 22027 * would now be treated as a plain stream, or a module 22028 * is about to be pushed so we could no longer use read- 22029 * side synchronous streams for fused loopback tcp. 22030 * Drain any queued data and disable direct sockfs 22031 * interface from now on. 22032 */ 22033 if (!tcp->tcp_issocket) { 22034 DB_TYPE(mp) = M_IOCNAK; 22035 iocp->ioc_error = EINVAL; 22036 } else { 22037 #ifdef _ILP32 22038 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 22039 #else 22040 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22041 #endif 22042 /* 22043 * Insert this socket into the acceptor hash. 22044 * We might need it for T_CONN_RES message 22045 */ 22046 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22047 22048 if (tcp->tcp_fused) { 22049 /* 22050 * This is a fused loopback tcp; disable 22051 * read-side synchronous streams interface 22052 * and drain any queued data. It is okay 22053 * to do this for non-synchronous streams 22054 * fused tcp as well. 22055 */ 22056 tcp_fuse_disable_pair(tcp, B_FALSE); 22057 } 22058 tcp->tcp_issocket = B_FALSE; 22059 TCP_STAT(tcps, tcp_sock_fallback); 22060 22061 DB_TYPE(mp) = M_IOCACK; 22062 iocp->ioc_error = 0; 22063 } 22064 iocp->ioc_count = 0; 22065 iocp->ioc_rval = 0; 22066 qreply(q, mp); 22067 return; 22068 } 22069 CALL_IP_WPUT(connp, q, mp); 22070 } 22071 22072 /* 22073 * This routine is called by tcp_wput() to handle all TPI requests. 22074 */ 22075 /* ARGSUSED */ 22076 static void 22077 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22078 { 22079 conn_t *connp = (conn_t *)arg; 22080 tcp_t *tcp = connp->conn_tcp; 22081 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22082 uchar_t *rptr; 22083 t_scalar_t type; 22084 int len; 22085 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22086 22087 /* 22088 * Try and ASSERT the minimum possible references on the 22089 * conn early enough. Since we are executing on write side, 22090 * the connection is obviously not detached and that means 22091 * there is a ref each for TCP and IP. Since we are behind 22092 * the squeue, the minimum references needed are 3. If the 22093 * conn is in classifier hash list, there should be an 22094 * extra ref for that (we check both the possibilities). 22095 */ 22096 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22097 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22098 22099 rptr = mp->b_rptr; 22100 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22101 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22102 type = ((union T_primitives *)rptr)->type; 22103 if (type == T_EXDATA_REQ) { 22104 uint32_t msize = msgdsize(mp->b_cont); 22105 22106 len = msize - 1; 22107 if (len < 0) { 22108 freemsg(mp); 22109 return; 22110 } 22111 /* 22112 * Try to force urgent data out on the wire. 22113 * Even if we have unsent data this will 22114 * at least send the urgent flag. 22115 * XXX does not handle more flag correctly. 22116 */ 22117 len += tcp->tcp_unsent; 22118 len += tcp->tcp_snxt; 22119 tcp->tcp_urg = len; 22120 tcp->tcp_valid_bits |= TCP_URG_VALID; 22121 22122 /* Bypass tcp protocol for fused tcp loopback */ 22123 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22124 return; 22125 } else if (type != T_DATA_REQ) { 22126 goto non_urgent_data; 22127 } 22128 /* TODO: options, flags, ... from user */ 22129 /* Set length to zero for reclamation below */ 22130 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22131 freeb(mp); 22132 return; 22133 } else { 22134 if (tcp->tcp_debug) { 22135 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22136 "tcp_wput_proto, dropping one..."); 22137 } 22138 freemsg(mp); 22139 return; 22140 } 22141 22142 non_urgent_data: 22143 22144 switch ((int)tprim->type) { 22145 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22146 /* 22147 * save the kssl_ent_t from the next block, and convert this 22148 * back to a normal bind_req. 22149 */ 22150 if (mp->b_cont != NULL) { 22151 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22152 22153 if (tcp->tcp_kssl_ent != NULL) { 22154 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22155 KSSL_NO_PROXY); 22156 tcp->tcp_kssl_ent = NULL; 22157 } 22158 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22159 sizeof (kssl_ent_t)); 22160 kssl_hold_ent(tcp->tcp_kssl_ent); 22161 freemsg(mp->b_cont); 22162 mp->b_cont = NULL; 22163 } 22164 tprim->type = T_BIND_REQ; 22165 22166 /* FALLTHROUGH */ 22167 case O_T_BIND_REQ: /* bind request */ 22168 case T_BIND_REQ: /* new semantics bind request */ 22169 tcp_bind(tcp, mp); 22170 break; 22171 case T_UNBIND_REQ: /* unbind request */ 22172 tcp_unbind(tcp, mp); 22173 break; 22174 case O_T_CONN_RES: /* old connection response XXX */ 22175 case T_CONN_RES: /* connection response */ 22176 tcp_accept(tcp, mp); 22177 break; 22178 case T_CONN_REQ: /* connection request */ 22179 tcp_connect(tcp, mp); 22180 break; 22181 case T_DISCON_REQ: /* disconnect request */ 22182 tcp_disconnect(tcp, mp); 22183 break; 22184 case T_CAPABILITY_REQ: 22185 tcp_capability_req(tcp, mp); /* capability request */ 22186 break; 22187 case T_INFO_REQ: /* information request */ 22188 tcp_info_req(tcp, mp); 22189 break; 22190 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22191 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, 22192 &tcp_opt_obj, B_TRUE); 22193 break; 22194 case T_OPTMGMT_REQ: 22195 /* 22196 * Note: no support for snmpcom_req() through new 22197 * T_OPTMGMT_REQ. See comments in ip.c 22198 */ 22199 /* Only IP is allowed to return meaningful value */ 22200 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22201 B_TRUE); 22202 break; 22203 22204 case T_UNITDATA_REQ: /* unitdata request */ 22205 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22206 break; 22207 case T_ORDREL_REQ: /* orderly release req */ 22208 freemsg(mp); 22209 22210 if (tcp->tcp_fused) 22211 tcp_unfuse(tcp); 22212 22213 if (tcp_xmit_end(tcp) != 0) { 22214 /* 22215 * We were crossing FINs and got a reset from 22216 * the other side. Just ignore it. 22217 */ 22218 if (tcp->tcp_debug) { 22219 (void) strlog(TCP_MOD_ID, 0, 1, 22220 SL_ERROR|SL_TRACE, 22221 "tcp_wput_proto, T_ORDREL_REQ out of " 22222 "state %s", 22223 tcp_display(tcp, NULL, 22224 DISP_ADDR_AND_PORT)); 22225 } 22226 } 22227 break; 22228 case T_ADDR_REQ: 22229 tcp_addr_req(tcp, mp); 22230 break; 22231 default: 22232 if (tcp->tcp_debug) { 22233 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22234 "tcp_wput_proto, bogus TPI msg, type %d", 22235 tprim->type); 22236 } 22237 /* 22238 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22239 * to recover. 22240 */ 22241 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22242 break; 22243 } 22244 } 22245 22246 /* 22247 * The TCP write service routine should never be called... 22248 */ 22249 /* ARGSUSED */ 22250 static void 22251 tcp_wsrv(queue_t *q) 22252 { 22253 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22254 22255 TCP_STAT(tcps, tcp_wsrv_called); 22256 } 22257 22258 /* Non overlapping byte exchanger */ 22259 static void 22260 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22261 { 22262 uchar_t uch; 22263 22264 while (len-- > 0) { 22265 uch = a[len]; 22266 a[len] = b[len]; 22267 b[len] = uch; 22268 } 22269 } 22270 22271 /* 22272 * Send out a control packet on the tcp connection specified. This routine 22273 * is typically called where we need a simple ACK or RST generated. 22274 */ 22275 static void 22276 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22277 { 22278 uchar_t *rptr; 22279 tcph_t *tcph; 22280 ipha_t *ipha = NULL; 22281 ip6_t *ip6h = NULL; 22282 uint32_t sum; 22283 int tcp_hdr_len; 22284 int tcp_ip_hdr_len; 22285 mblk_t *mp; 22286 tcp_stack_t *tcps = tcp->tcp_tcps; 22287 22288 /* 22289 * Save sum for use in source route later. 22290 */ 22291 ASSERT(tcp != NULL); 22292 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22293 tcp_hdr_len = tcp->tcp_hdr_len; 22294 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22295 22296 /* If a text string is passed in with the request, pass it to strlog. */ 22297 if (str != NULL && tcp->tcp_debug) { 22298 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22299 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22300 str, seq, ack, ctl); 22301 } 22302 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22303 BPRI_MED); 22304 if (mp == NULL) { 22305 return; 22306 } 22307 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22308 mp->b_rptr = rptr; 22309 mp->b_wptr = &rptr[tcp_hdr_len]; 22310 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22311 22312 if (tcp->tcp_ipversion == IPV4_VERSION) { 22313 ipha = (ipha_t *)rptr; 22314 ipha->ipha_length = htons(tcp_hdr_len); 22315 } else { 22316 ip6h = (ip6_t *)rptr; 22317 ASSERT(tcp != NULL); 22318 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22319 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22320 } 22321 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22322 tcph->th_flags[0] = (uint8_t)ctl; 22323 if (ctl & TH_RST) { 22324 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22325 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22326 /* 22327 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22328 */ 22329 if (tcp->tcp_snd_ts_ok && 22330 tcp->tcp_state > TCPS_SYN_SENT) { 22331 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22332 *(mp->b_wptr) = TCPOPT_EOL; 22333 if (tcp->tcp_ipversion == IPV4_VERSION) { 22334 ipha->ipha_length = htons(tcp_hdr_len - 22335 TCPOPT_REAL_TS_LEN); 22336 } else { 22337 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22338 TCPOPT_REAL_TS_LEN); 22339 } 22340 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22341 sum -= TCPOPT_REAL_TS_LEN; 22342 } 22343 } 22344 if (ctl & TH_ACK) { 22345 if (tcp->tcp_snd_ts_ok) { 22346 U32_TO_BE32(lbolt, 22347 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22348 U32_TO_BE32(tcp->tcp_ts_recent, 22349 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22350 } 22351 22352 /* Update the latest receive window size in TCP header. */ 22353 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22354 tcph->th_win); 22355 tcp->tcp_rack = ack; 22356 tcp->tcp_rack_cnt = 0; 22357 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22358 } 22359 BUMP_LOCAL(tcp->tcp_obsegs); 22360 U32_TO_BE32(seq, tcph->th_seq); 22361 U32_TO_BE32(ack, tcph->th_ack); 22362 /* 22363 * Include the adjustment for a source route if any. 22364 */ 22365 sum = (sum >> 16) + (sum & 0xFFFF); 22366 U16_TO_BE16(sum, tcph->th_sum); 22367 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22368 tcp_send_data(tcp, tcp->tcp_wq, mp); 22369 } 22370 22371 /* 22372 * If this routine returns B_TRUE, TCP can generate a RST in response 22373 * to a segment. If it returns B_FALSE, TCP should not respond. 22374 */ 22375 static boolean_t 22376 tcp_send_rst_chk(tcp_stack_t *tcps) 22377 { 22378 clock_t now; 22379 22380 /* 22381 * TCP needs to protect itself from generating too many RSTs. 22382 * This can be a DoS attack by sending us random segments 22383 * soliciting RSTs. 22384 * 22385 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22386 * in each 1 second interval. In this way, TCP still generate 22387 * RSTs in normal cases but when under attack, the impact is 22388 * limited. 22389 */ 22390 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22391 now = lbolt; 22392 /* lbolt can wrap around. */ 22393 if ((tcps->tcps_last_rst_intrvl > now) || 22394 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22395 1*SECONDS)) { 22396 tcps->tcps_last_rst_intrvl = now; 22397 tcps->tcps_rst_cnt = 1; 22398 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22399 return (B_FALSE); 22400 } 22401 } 22402 return (B_TRUE); 22403 } 22404 22405 /* 22406 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22407 */ 22408 static void 22409 tcp_ip_ire_mark_advice(tcp_t *tcp) 22410 { 22411 mblk_t *mp; 22412 ipic_t *ipic; 22413 22414 if (tcp->tcp_ipversion == IPV4_VERSION) { 22415 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22416 &ipic); 22417 } else { 22418 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22419 &ipic); 22420 } 22421 if (mp == NULL) 22422 return; 22423 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22424 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22425 } 22426 22427 /* 22428 * Return an IP advice ioctl mblk and set ipic to be the pointer 22429 * to the advice structure. 22430 */ 22431 static mblk_t * 22432 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22433 { 22434 struct iocblk *ioc; 22435 mblk_t *mp, *mp1; 22436 22437 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22438 if (mp == NULL) 22439 return (NULL); 22440 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22441 *ipic = (ipic_t *)mp->b_rptr; 22442 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22443 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22444 22445 bcopy(addr, *ipic + 1, addr_len); 22446 22447 (*ipic)->ipic_addr_length = addr_len; 22448 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22449 22450 mp1 = mkiocb(IP_IOCTL); 22451 if (mp1 == NULL) { 22452 freemsg(mp); 22453 return (NULL); 22454 } 22455 mp1->b_cont = mp; 22456 ioc = (struct iocblk *)mp1->b_rptr; 22457 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22458 22459 return (mp1); 22460 } 22461 22462 /* 22463 * Generate a reset based on an inbound packet, connp is set by caller 22464 * when RST is in response to an unexpected inbound packet for which 22465 * there is active tcp state in the system. 22466 * 22467 * IPSEC NOTE : Try to send the reply with the same protection as it came 22468 * in. We still have the ipsec_mp that the packet was attached to. Thus 22469 * the packet will go out at the same level of protection as it came in by 22470 * converting the IPSEC_IN to IPSEC_OUT. 22471 */ 22472 static void 22473 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22474 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22475 tcp_stack_t *tcps, conn_t *connp) 22476 { 22477 ipha_t *ipha = NULL; 22478 ip6_t *ip6h = NULL; 22479 ushort_t len; 22480 tcph_t *tcph; 22481 int i; 22482 mblk_t *ipsec_mp; 22483 boolean_t mctl_present; 22484 ipic_t *ipic; 22485 ipaddr_t v4addr; 22486 in6_addr_t v6addr; 22487 int addr_len; 22488 void *addr; 22489 queue_t *q = tcps->tcps_g_q; 22490 tcp_t *tcp; 22491 cred_t *cr; 22492 mblk_t *nmp; 22493 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22494 22495 if (tcps->tcps_g_q == NULL) { 22496 /* 22497 * For non-zero stackids the default queue isn't created 22498 * until the first open, thus there can be a need to send 22499 * a reset before then. But we can't do that, hence we just 22500 * drop the packet. Later during boot, when the default queue 22501 * has been setup, a retransmitted packet from the peer 22502 * will result in a reset. 22503 */ 22504 ASSERT(tcps->tcps_netstack->netstack_stackid != 22505 GLOBAL_NETSTACKID); 22506 freemsg(mp); 22507 return; 22508 } 22509 22510 if (connp != NULL) 22511 tcp = connp->conn_tcp; 22512 else 22513 tcp = Q_TO_TCP(q); 22514 22515 if (!tcp_send_rst_chk(tcps)) { 22516 tcps->tcps_rst_unsent++; 22517 freemsg(mp); 22518 return; 22519 } 22520 22521 if (mp->b_datap->db_type == M_CTL) { 22522 ipsec_mp = mp; 22523 mp = mp->b_cont; 22524 mctl_present = B_TRUE; 22525 } else { 22526 ipsec_mp = mp; 22527 mctl_present = B_FALSE; 22528 } 22529 22530 if (str && q && tcps->tcps_dbg) { 22531 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22532 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22533 "flags 0x%x", 22534 str, seq, ack, ctl); 22535 } 22536 if (mp->b_datap->db_ref != 1) { 22537 mblk_t *mp1 = copyb(mp); 22538 freemsg(mp); 22539 mp = mp1; 22540 if (!mp) { 22541 if (mctl_present) 22542 freeb(ipsec_mp); 22543 return; 22544 } else { 22545 if (mctl_present) { 22546 ipsec_mp->b_cont = mp; 22547 } else { 22548 ipsec_mp = mp; 22549 } 22550 } 22551 } else if (mp->b_cont) { 22552 freemsg(mp->b_cont); 22553 mp->b_cont = NULL; 22554 } 22555 /* 22556 * We skip reversing source route here. 22557 * (for now we replace all IP options with EOL) 22558 */ 22559 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22560 ipha = (ipha_t *)mp->b_rptr; 22561 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22562 mp->b_rptr[i] = IPOPT_EOL; 22563 /* 22564 * Make sure that src address isn't flagrantly invalid. 22565 * Not all broadcast address checking for the src address 22566 * is possible, since we don't know the netmask of the src 22567 * addr. No check for destination address is done, since 22568 * IP will not pass up a packet with a broadcast dest 22569 * address to TCP. Similar checks are done below for IPv6. 22570 */ 22571 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22572 CLASSD(ipha->ipha_src)) { 22573 freemsg(ipsec_mp); 22574 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22575 return; 22576 } 22577 } else { 22578 ip6h = (ip6_t *)mp->b_rptr; 22579 22580 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22581 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22582 freemsg(ipsec_mp); 22583 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22584 return; 22585 } 22586 22587 /* Remove any extension headers assuming partial overlay */ 22588 if (ip_hdr_len > IPV6_HDR_LEN) { 22589 uint8_t *to; 22590 22591 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22592 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22593 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22594 ip_hdr_len = IPV6_HDR_LEN; 22595 ip6h = (ip6_t *)mp->b_rptr; 22596 ip6h->ip6_nxt = IPPROTO_TCP; 22597 } 22598 } 22599 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22600 if (tcph->th_flags[0] & TH_RST) { 22601 freemsg(ipsec_mp); 22602 return; 22603 } 22604 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22605 len = ip_hdr_len + sizeof (tcph_t); 22606 mp->b_wptr = &mp->b_rptr[len]; 22607 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22608 ipha->ipha_length = htons(len); 22609 /* Swap addresses */ 22610 v4addr = ipha->ipha_src; 22611 ipha->ipha_src = ipha->ipha_dst; 22612 ipha->ipha_dst = v4addr; 22613 ipha->ipha_ident = 0; 22614 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22615 addr_len = IP_ADDR_LEN; 22616 addr = &v4addr; 22617 } else { 22618 /* No ip6i_t in this case */ 22619 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22620 /* Swap addresses */ 22621 v6addr = ip6h->ip6_src; 22622 ip6h->ip6_src = ip6h->ip6_dst; 22623 ip6h->ip6_dst = v6addr; 22624 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22625 addr_len = IPV6_ADDR_LEN; 22626 addr = &v6addr; 22627 } 22628 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22629 U32_TO_BE32(ack, tcph->th_ack); 22630 U32_TO_BE32(seq, tcph->th_seq); 22631 U16_TO_BE16(0, tcph->th_win); 22632 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22633 tcph->th_flags[0] = (uint8_t)ctl; 22634 if (ctl & TH_RST) { 22635 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22636 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22637 } 22638 22639 /* IP trusts us to set up labels when required. */ 22640 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 22641 crgetlabel(cr) != NULL) { 22642 int err, adjust; 22643 22644 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22645 err = tsol_check_label(cr, &mp, &adjust, 22646 tcp->tcp_connp->conn_mac_exempt, 22647 tcps->tcps_netstack->netstack_ip); 22648 else 22649 err = tsol_check_label_v6(cr, &mp, &adjust, 22650 tcp->tcp_connp->conn_mac_exempt, 22651 tcps->tcps_netstack->netstack_ip); 22652 if (mctl_present) 22653 ipsec_mp->b_cont = mp; 22654 else 22655 ipsec_mp = mp; 22656 if (err != 0) { 22657 freemsg(ipsec_mp); 22658 return; 22659 } 22660 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22661 ipha = (ipha_t *)mp->b_rptr; 22662 adjust += ntohs(ipha->ipha_length); 22663 ipha->ipha_length = htons(adjust); 22664 } else { 22665 ip6h = (ip6_t *)mp->b_rptr; 22666 } 22667 } 22668 22669 if (mctl_present) { 22670 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22671 22672 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22673 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22674 return; 22675 } 22676 } 22677 if (zoneid == ALL_ZONES) 22678 zoneid = GLOBAL_ZONEID; 22679 22680 /* Add the zoneid so ip_output routes it properly */ 22681 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22682 freemsg(ipsec_mp); 22683 return; 22684 } 22685 ipsec_mp = nmp; 22686 22687 /* 22688 * NOTE: one might consider tracing a TCP packet here, but 22689 * this function has no active TCP state and no tcp structure 22690 * that has a trace buffer. If we traced here, we would have 22691 * to keep a local trace buffer in tcp_record_trace(). 22692 * 22693 * TSol note: The mblk that contains the incoming packet was 22694 * reused by tcp_xmit_listener_reset, so it already contains 22695 * the right credentials and we don't need to call mblk_setcred. 22696 * Also the conn's cred is not right since it is associated 22697 * with tcps_g_q. 22698 */ 22699 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22700 22701 /* 22702 * Tell IP to mark the IRE used for this destination temporary. 22703 * This way, we can limit our exposure to DoS attack because IP 22704 * creates an IRE for each destination. If there are too many, 22705 * the time to do any routing lookup will be extremely long. And 22706 * the lookup can be in interrupt context. 22707 * 22708 * Note that in normal circumstances, this marking should not 22709 * affect anything. It would be nice if only 1 message is 22710 * needed to inform IP that the IRE created for this RST should 22711 * not be added to the cache table. But there is currently 22712 * not such communication mechanism between TCP and IP. So 22713 * the best we can do now is to send the advice ioctl to IP 22714 * to mark the IRE temporary. 22715 */ 22716 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22717 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22718 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22719 } 22720 } 22721 22722 /* 22723 * Initiate closedown sequence on an active connection. (May be called as 22724 * writer.) Return value zero for OK return, non-zero for error return. 22725 */ 22726 static int 22727 tcp_xmit_end(tcp_t *tcp) 22728 { 22729 ipic_t *ipic; 22730 mblk_t *mp; 22731 tcp_stack_t *tcps = tcp->tcp_tcps; 22732 22733 if (tcp->tcp_state < TCPS_SYN_RCVD || 22734 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22735 /* 22736 * Invalid state, only states TCPS_SYN_RCVD, 22737 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22738 */ 22739 return (-1); 22740 } 22741 22742 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22743 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22744 /* 22745 * If there is nothing more unsent, send the FIN now. 22746 * Otherwise, it will go out with the last segment. 22747 */ 22748 if (tcp->tcp_unsent == 0) { 22749 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22750 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22751 22752 if (mp) { 22753 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22754 tcp_send_data(tcp, tcp->tcp_wq, mp); 22755 } else { 22756 /* 22757 * Couldn't allocate msg. Pretend we got it out. 22758 * Wait for rexmit timeout. 22759 */ 22760 tcp->tcp_snxt = tcp->tcp_fss + 1; 22761 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22762 } 22763 22764 /* 22765 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22766 * changed. 22767 */ 22768 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22769 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22770 } 22771 } else { 22772 /* 22773 * If tcp->tcp_cork is set, then the data will not get sent, 22774 * so we have to check that and unset it first. 22775 */ 22776 if (tcp->tcp_cork) 22777 tcp->tcp_cork = B_FALSE; 22778 tcp_wput_data(tcp, NULL, B_FALSE); 22779 } 22780 22781 /* 22782 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22783 * is 0, don't update the cache. 22784 */ 22785 if (tcps->tcps_rtt_updates == 0 || 22786 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22787 return (0); 22788 22789 /* 22790 * NOTE: should not update if source routes i.e. if tcp_remote if 22791 * different from the destination. 22792 */ 22793 if (tcp->tcp_ipversion == IPV4_VERSION) { 22794 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22795 return (0); 22796 } 22797 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22798 &ipic); 22799 } else { 22800 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22801 &tcp->tcp_ip6h->ip6_dst))) { 22802 return (0); 22803 } 22804 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22805 &ipic); 22806 } 22807 22808 /* Record route attributes in the IRE for use by future connections. */ 22809 if (mp == NULL) 22810 return (0); 22811 22812 /* 22813 * We do not have a good algorithm to update ssthresh at this time. 22814 * So don't do any update. 22815 */ 22816 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22817 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22818 22819 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22820 return (0); 22821 } 22822 22823 /* 22824 * Generate a "no listener here" RST in response to an "unknown" segment. 22825 * connp is set by caller when RST is in response to an unexpected 22826 * inbound packet for which there is active tcp state in the system. 22827 * Note that we are reusing the incoming mp to construct the outgoing RST. 22828 */ 22829 void 22830 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22831 tcp_stack_t *tcps, conn_t *connp) 22832 { 22833 uchar_t *rptr; 22834 uint32_t seg_len; 22835 tcph_t *tcph; 22836 uint32_t seg_seq; 22837 uint32_t seg_ack; 22838 uint_t flags; 22839 mblk_t *ipsec_mp; 22840 ipha_t *ipha; 22841 ip6_t *ip6h; 22842 boolean_t mctl_present = B_FALSE; 22843 boolean_t check = B_TRUE; 22844 boolean_t policy_present; 22845 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22846 22847 TCP_STAT(tcps, tcp_no_listener); 22848 22849 ipsec_mp = mp; 22850 22851 if (mp->b_datap->db_type == M_CTL) { 22852 ipsec_in_t *ii; 22853 22854 mctl_present = B_TRUE; 22855 mp = mp->b_cont; 22856 22857 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22858 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22859 if (ii->ipsec_in_dont_check) { 22860 check = B_FALSE; 22861 if (!ii->ipsec_in_secure) { 22862 freeb(ipsec_mp); 22863 mctl_present = B_FALSE; 22864 ipsec_mp = mp; 22865 } 22866 } 22867 } 22868 22869 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22870 policy_present = ipss->ipsec_inbound_v4_policy_present; 22871 ipha = (ipha_t *)mp->b_rptr; 22872 ip6h = NULL; 22873 } else { 22874 policy_present = ipss->ipsec_inbound_v6_policy_present; 22875 ipha = NULL; 22876 ip6h = (ip6_t *)mp->b_rptr; 22877 } 22878 22879 if (check && policy_present) { 22880 /* 22881 * The conn_t parameter is NULL because we already know 22882 * nobody's home. 22883 */ 22884 ipsec_mp = ipsec_check_global_policy( 22885 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22886 tcps->tcps_netstack); 22887 if (ipsec_mp == NULL) 22888 return; 22889 } 22890 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22891 DTRACE_PROBE2( 22892 tx__ip__log__error__nolistener__tcp, 22893 char *, "Could not reply with RST to mp(1)", 22894 mblk_t *, mp); 22895 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22896 freemsg(ipsec_mp); 22897 return; 22898 } 22899 22900 rptr = mp->b_rptr; 22901 22902 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22903 seg_seq = BE32_TO_U32(tcph->th_seq); 22904 seg_ack = BE32_TO_U32(tcph->th_ack); 22905 flags = tcph->th_flags[0]; 22906 22907 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22908 if (flags & TH_RST) { 22909 freemsg(ipsec_mp); 22910 } else if (flags & TH_ACK) { 22911 tcp_xmit_early_reset("no tcp, reset", 22912 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 22913 connp); 22914 } else { 22915 if (flags & TH_SYN) { 22916 seg_len++; 22917 } else { 22918 /* 22919 * Here we violate the RFC. Note that a normal 22920 * TCP will never send a segment without the ACK 22921 * flag, except for RST or SYN segment. This 22922 * segment is neither. Just drop it on the 22923 * floor. 22924 */ 22925 freemsg(ipsec_mp); 22926 tcps->tcps_rst_unsent++; 22927 return; 22928 } 22929 22930 tcp_xmit_early_reset("no tcp, reset/ack", 22931 ipsec_mp, 0, seg_seq + seg_len, 22932 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 22933 } 22934 } 22935 22936 /* 22937 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22938 * ip and tcp header ready to pass down to IP. If the mp passed in is 22939 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22940 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22941 * otherwise it will dup partial mblks.) 22942 * Otherwise, an appropriate ACK packet will be generated. This 22943 * routine is not usually called to send new data for the first time. It 22944 * is mostly called out of the timer for retransmits, and to generate ACKs. 22945 * 22946 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22947 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22948 * of the original mblk chain will be returned in *offset and *end_mp. 22949 */ 22950 mblk_t * 22951 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22952 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22953 boolean_t rexmit) 22954 { 22955 int data_length; 22956 int32_t off = 0; 22957 uint_t flags; 22958 mblk_t *mp1; 22959 mblk_t *mp2; 22960 uchar_t *rptr; 22961 tcph_t *tcph; 22962 int32_t num_sack_blk = 0; 22963 int32_t sack_opt_len = 0; 22964 tcp_stack_t *tcps = tcp->tcp_tcps; 22965 22966 /* Allocate for our maximum TCP header + link-level */ 22967 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22968 tcps->tcps_wroff_xtra, BPRI_MED); 22969 if (!mp1) 22970 return (NULL); 22971 data_length = 0; 22972 22973 /* 22974 * Note that tcp_mss has been adjusted to take into account the 22975 * timestamp option if applicable. Because SACK options do not 22976 * appear in every TCP segments and they are of variable lengths, 22977 * they cannot be included in tcp_mss. Thus we need to calculate 22978 * the actual segment length when we need to send a segment which 22979 * includes SACK options. 22980 */ 22981 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22982 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22983 tcp->tcp_num_sack_blk); 22984 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22985 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22986 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22987 max_to_send -= sack_opt_len; 22988 } 22989 22990 if (offset != NULL) { 22991 off = *offset; 22992 /* We use offset as an indicator that end_mp is not NULL. */ 22993 *end_mp = NULL; 22994 } 22995 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22996 /* This could be faster with cooperation from downstream */ 22997 if (mp2 != mp1 && !sendall && 22998 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22999 max_to_send) 23000 /* 23001 * Don't send the next mblk since the whole mblk 23002 * does not fit. 23003 */ 23004 break; 23005 mp2->b_cont = dupb(mp); 23006 mp2 = mp2->b_cont; 23007 if (!mp2) { 23008 freemsg(mp1); 23009 return (NULL); 23010 } 23011 mp2->b_rptr += off; 23012 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23013 (uintptr_t)INT_MAX); 23014 23015 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23016 if (data_length > max_to_send) { 23017 mp2->b_wptr -= data_length - max_to_send; 23018 data_length = max_to_send; 23019 off = mp2->b_wptr - mp->b_rptr; 23020 break; 23021 } else { 23022 off = 0; 23023 } 23024 } 23025 if (offset != NULL) { 23026 *offset = off; 23027 *end_mp = mp; 23028 } 23029 if (seg_len != NULL) { 23030 *seg_len = data_length; 23031 } 23032 23033 /* Update the latest receive window size in TCP header. */ 23034 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23035 tcp->tcp_tcph->th_win); 23036 23037 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23038 mp1->b_rptr = rptr; 23039 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23040 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23041 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23042 U32_TO_ABE32(seq, tcph->th_seq); 23043 23044 /* 23045 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23046 * that this function was called from tcp_wput_data. Thus, when called 23047 * to retransmit data the setting of the PUSH bit may appear some 23048 * what random in that it might get set when it should not. This 23049 * should not pose any performance issues. 23050 */ 23051 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23052 tcp->tcp_unsent == data_length)) { 23053 flags = TH_ACK | TH_PUSH; 23054 } else { 23055 flags = TH_ACK; 23056 } 23057 23058 if (tcp->tcp_ecn_ok) { 23059 if (tcp->tcp_ecn_echo_on) 23060 flags |= TH_ECE; 23061 23062 /* 23063 * Only set ECT bit and ECN_CWR if a segment contains new data. 23064 * There is no TCP flow control for non-data segments, and 23065 * only data segment is transmitted reliably. 23066 */ 23067 if (data_length > 0 && !rexmit) { 23068 SET_ECT(tcp, rptr); 23069 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23070 flags |= TH_CWR; 23071 tcp->tcp_ecn_cwr_sent = B_TRUE; 23072 } 23073 } 23074 } 23075 23076 if (tcp->tcp_valid_bits) { 23077 uint32_t u1; 23078 23079 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23080 seq == tcp->tcp_iss) { 23081 uchar_t *wptr; 23082 23083 /* 23084 * If TCP_ISS_VALID and the seq number is tcp_iss, 23085 * TCP can only be in SYN-SENT, SYN-RCVD or 23086 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23087 * our SYN is not ack'ed but the app closes this 23088 * TCP connection. 23089 */ 23090 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23091 tcp->tcp_state == TCPS_SYN_RCVD || 23092 tcp->tcp_state == TCPS_FIN_WAIT_1); 23093 23094 /* 23095 * Tack on the MSS option. It is always needed 23096 * for both active and passive open. 23097 * 23098 * MSS option value should be interface MTU - MIN 23099 * TCP/IP header according to RFC 793 as it means 23100 * the maximum segment size TCP can receive. But 23101 * to get around some broken middle boxes/end hosts 23102 * out there, we allow the option value to be the 23103 * same as the MSS option size on the peer side. 23104 * In this way, the other side will not send 23105 * anything larger than they can receive. 23106 * 23107 * Note that for SYN_SENT state, the ndd param 23108 * tcp_use_smss_as_mss_opt has no effect as we 23109 * don't know the peer's MSS option value. So 23110 * the only case we need to take care of is in 23111 * SYN_RCVD state, which is done later. 23112 */ 23113 wptr = mp1->b_wptr; 23114 wptr[0] = TCPOPT_MAXSEG; 23115 wptr[1] = TCPOPT_MAXSEG_LEN; 23116 wptr += 2; 23117 u1 = tcp->tcp_if_mtu - 23118 (tcp->tcp_ipversion == IPV4_VERSION ? 23119 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23120 TCP_MIN_HEADER_LENGTH; 23121 U16_TO_BE16(u1, wptr); 23122 mp1->b_wptr = wptr + 2; 23123 /* Update the offset to cover the additional word */ 23124 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23125 23126 /* 23127 * Note that the following way of filling in 23128 * TCP options are not optimal. Some NOPs can 23129 * be saved. But there is no need at this time 23130 * to optimize it. When it is needed, we will 23131 * do it. 23132 */ 23133 switch (tcp->tcp_state) { 23134 case TCPS_SYN_SENT: 23135 flags = TH_SYN; 23136 23137 if (tcp->tcp_snd_ts_ok) { 23138 uint32_t llbolt = (uint32_t)lbolt; 23139 23140 wptr = mp1->b_wptr; 23141 wptr[0] = TCPOPT_NOP; 23142 wptr[1] = TCPOPT_NOP; 23143 wptr[2] = TCPOPT_TSTAMP; 23144 wptr[3] = TCPOPT_TSTAMP_LEN; 23145 wptr += 4; 23146 U32_TO_BE32(llbolt, wptr); 23147 wptr += 4; 23148 ASSERT(tcp->tcp_ts_recent == 0); 23149 U32_TO_BE32(0L, wptr); 23150 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23151 tcph->th_offset_and_rsrvd[0] += 23152 (3 << 4); 23153 } 23154 23155 /* 23156 * Set up all the bits to tell other side 23157 * we are ECN capable. 23158 */ 23159 if (tcp->tcp_ecn_ok) { 23160 flags |= (TH_ECE | TH_CWR); 23161 } 23162 break; 23163 case TCPS_SYN_RCVD: 23164 flags |= TH_SYN; 23165 23166 /* 23167 * Reset the MSS option value to be SMSS 23168 * We should probably add back the bytes 23169 * for timestamp option and IPsec. We 23170 * don't do that as this is a workaround 23171 * for broken middle boxes/end hosts, it 23172 * is better for us to be more cautious. 23173 * They may not take these things into 23174 * account in their SMSS calculation. Thus 23175 * the peer's calculated SMSS may be smaller 23176 * than what it can be. This should be OK. 23177 */ 23178 if (tcps->tcps_use_smss_as_mss_opt) { 23179 u1 = tcp->tcp_mss; 23180 U16_TO_BE16(u1, wptr); 23181 } 23182 23183 /* 23184 * If the other side is ECN capable, reply 23185 * that we are also ECN capable. 23186 */ 23187 if (tcp->tcp_ecn_ok) 23188 flags |= TH_ECE; 23189 break; 23190 default: 23191 /* 23192 * The above ASSERT() makes sure that this 23193 * must be FIN-WAIT-1 state. Our SYN has 23194 * not been ack'ed so retransmit it. 23195 */ 23196 flags |= TH_SYN; 23197 break; 23198 } 23199 23200 if (tcp->tcp_snd_ws_ok) { 23201 wptr = mp1->b_wptr; 23202 wptr[0] = TCPOPT_NOP; 23203 wptr[1] = TCPOPT_WSCALE; 23204 wptr[2] = TCPOPT_WS_LEN; 23205 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23206 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23207 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23208 } 23209 23210 if (tcp->tcp_snd_sack_ok) { 23211 wptr = mp1->b_wptr; 23212 wptr[0] = TCPOPT_NOP; 23213 wptr[1] = TCPOPT_NOP; 23214 wptr[2] = TCPOPT_SACK_PERMITTED; 23215 wptr[3] = TCPOPT_SACK_OK_LEN; 23216 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23217 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23218 } 23219 23220 /* allocb() of adequate mblk assures space */ 23221 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23222 (uintptr_t)INT_MAX); 23223 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23224 /* 23225 * Get IP set to checksum on our behalf 23226 * Include the adjustment for a source route if any. 23227 */ 23228 u1 += tcp->tcp_sum; 23229 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23230 U16_TO_BE16(u1, tcph->th_sum); 23231 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23232 } 23233 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23234 (seq + data_length) == tcp->tcp_fss) { 23235 if (!tcp->tcp_fin_acked) { 23236 flags |= TH_FIN; 23237 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23238 } 23239 if (!tcp->tcp_fin_sent) { 23240 tcp->tcp_fin_sent = B_TRUE; 23241 switch (tcp->tcp_state) { 23242 case TCPS_SYN_RCVD: 23243 case TCPS_ESTABLISHED: 23244 tcp->tcp_state = TCPS_FIN_WAIT_1; 23245 break; 23246 case TCPS_CLOSE_WAIT: 23247 tcp->tcp_state = TCPS_LAST_ACK; 23248 break; 23249 } 23250 if (tcp->tcp_suna == tcp->tcp_snxt) 23251 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23252 tcp->tcp_snxt = tcp->tcp_fss + 1; 23253 } 23254 } 23255 /* 23256 * Note the trick here. u1 is unsigned. When tcp_urg 23257 * is smaller than seq, u1 will become a very huge value. 23258 * So the comparison will fail. Also note that tcp_urp 23259 * should be positive, see RFC 793 page 17. 23260 */ 23261 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23262 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23263 u1 < (uint32_t)(64 * 1024)) { 23264 flags |= TH_URG; 23265 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23266 U32_TO_ABE16(u1, tcph->th_urp); 23267 } 23268 } 23269 tcph->th_flags[0] = (uchar_t)flags; 23270 tcp->tcp_rack = tcp->tcp_rnxt; 23271 tcp->tcp_rack_cnt = 0; 23272 23273 if (tcp->tcp_snd_ts_ok) { 23274 if (tcp->tcp_state != TCPS_SYN_SENT) { 23275 uint32_t llbolt = (uint32_t)lbolt; 23276 23277 U32_TO_BE32(llbolt, 23278 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23279 U32_TO_BE32(tcp->tcp_ts_recent, 23280 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23281 } 23282 } 23283 23284 if (num_sack_blk > 0) { 23285 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23286 sack_blk_t *tmp; 23287 int32_t i; 23288 23289 wptr[0] = TCPOPT_NOP; 23290 wptr[1] = TCPOPT_NOP; 23291 wptr[2] = TCPOPT_SACK; 23292 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23293 sizeof (sack_blk_t); 23294 wptr += TCPOPT_REAL_SACK_LEN; 23295 23296 tmp = tcp->tcp_sack_list; 23297 for (i = 0; i < num_sack_blk; i++) { 23298 U32_TO_BE32(tmp[i].begin, wptr); 23299 wptr += sizeof (tcp_seq); 23300 U32_TO_BE32(tmp[i].end, wptr); 23301 wptr += sizeof (tcp_seq); 23302 } 23303 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23304 } 23305 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23306 data_length += (int)(mp1->b_wptr - rptr); 23307 if (tcp->tcp_ipversion == IPV4_VERSION) { 23308 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23309 } else { 23310 ip6_t *ip6 = (ip6_t *)(rptr + 23311 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23312 sizeof (ip6i_t) : 0)); 23313 23314 ip6->ip6_plen = htons(data_length - 23315 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23316 } 23317 23318 /* 23319 * Prime pump for IP 23320 * Include the adjustment for a source route if any. 23321 */ 23322 data_length -= tcp->tcp_ip_hdr_len; 23323 data_length += tcp->tcp_sum; 23324 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23325 U16_TO_ABE16(data_length, tcph->th_sum); 23326 if (tcp->tcp_ip_forward_progress) { 23327 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23328 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23329 tcp->tcp_ip_forward_progress = B_FALSE; 23330 } 23331 return (mp1); 23332 } 23333 23334 /* This function handles the push timeout. */ 23335 void 23336 tcp_push_timer(void *arg) 23337 { 23338 conn_t *connp = (conn_t *)arg; 23339 tcp_t *tcp = connp->conn_tcp; 23340 tcp_stack_t *tcps = tcp->tcp_tcps; 23341 23342 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23343 23344 ASSERT(tcp->tcp_listener == NULL); 23345 23346 /* 23347 * We need to plug synchronous streams during our drain to prevent 23348 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23349 */ 23350 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23351 tcp->tcp_push_tid = 0; 23352 if ((tcp->tcp_rcv_list != NULL) && 23353 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 23354 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23355 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23356 } 23357 23358 /* 23359 * This function handles delayed ACK timeout. 23360 */ 23361 static void 23362 tcp_ack_timer(void *arg) 23363 { 23364 conn_t *connp = (conn_t *)arg; 23365 tcp_t *tcp = connp->conn_tcp; 23366 mblk_t *mp; 23367 tcp_stack_t *tcps = tcp->tcp_tcps; 23368 23369 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23370 23371 tcp->tcp_ack_tid = 0; 23372 23373 if (tcp->tcp_fused) 23374 return; 23375 23376 /* 23377 * Do not send ACK if there is no outstanding unack'ed data. 23378 */ 23379 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23380 return; 23381 } 23382 23383 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23384 /* 23385 * Make sure we don't allow deferred ACKs to result in 23386 * timer-based ACKing. If we have held off an ACK 23387 * when there was more than an mss here, and the timer 23388 * goes off, we have to worry about the possibility 23389 * that the sender isn't doing slow-start, or is out 23390 * of step with us for some other reason. We fall 23391 * permanently back in the direction of 23392 * ACK-every-other-packet as suggested in RFC 1122. 23393 */ 23394 if (tcp->tcp_rack_abs_max > 2) 23395 tcp->tcp_rack_abs_max--; 23396 tcp->tcp_rack_cur_max = 2; 23397 } 23398 mp = tcp_ack_mp(tcp); 23399 23400 if (mp != NULL) { 23401 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 23402 BUMP_LOCAL(tcp->tcp_obsegs); 23403 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23404 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23405 tcp_send_data(tcp, tcp->tcp_wq, mp); 23406 } 23407 } 23408 23409 23410 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23411 static mblk_t * 23412 tcp_ack_mp(tcp_t *tcp) 23413 { 23414 uint32_t seq_no; 23415 tcp_stack_t *tcps = tcp->tcp_tcps; 23416 23417 /* 23418 * There are a few cases to be considered while setting the sequence no. 23419 * Essentially, we can come here while processing an unacceptable pkt 23420 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23421 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23422 * If we are here for a zero window probe, stick with suna. In all 23423 * other cases, we check if suna + swnd encompasses snxt and set 23424 * the sequence number to snxt, if so. If snxt falls outside the 23425 * window (the receiver probably shrunk its window), we will go with 23426 * suna + swnd, otherwise the sequence no will be unacceptable to the 23427 * receiver. 23428 */ 23429 if (tcp->tcp_zero_win_probe) { 23430 seq_no = tcp->tcp_suna; 23431 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23432 ASSERT(tcp->tcp_swnd == 0); 23433 seq_no = tcp->tcp_snxt; 23434 } else { 23435 seq_no = SEQ_GT(tcp->tcp_snxt, 23436 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23437 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23438 } 23439 23440 if (tcp->tcp_valid_bits) { 23441 /* 23442 * For the complex case where we have to send some 23443 * controls (FIN or SYN), let tcp_xmit_mp do it. 23444 */ 23445 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23446 NULL, B_FALSE)); 23447 } else { 23448 /* Generate a simple ACK */ 23449 int data_length; 23450 uchar_t *rptr; 23451 tcph_t *tcph; 23452 mblk_t *mp1; 23453 int32_t tcp_hdr_len; 23454 int32_t tcp_tcp_hdr_len; 23455 int32_t num_sack_blk = 0; 23456 int32_t sack_opt_len; 23457 23458 /* 23459 * Allocate space for TCP + IP headers 23460 * and link-level header 23461 */ 23462 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23463 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23464 tcp->tcp_num_sack_blk); 23465 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23466 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23467 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23468 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23469 } else { 23470 tcp_hdr_len = tcp->tcp_hdr_len; 23471 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23472 } 23473 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23474 if (!mp1) 23475 return (NULL); 23476 23477 /* Update the latest receive window size in TCP header. */ 23478 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23479 tcp->tcp_tcph->th_win); 23480 /* copy in prototype TCP + IP header */ 23481 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23482 mp1->b_rptr = rptr; 23483 mp1->b_wptr = rptr + tcp_hdr_len; 23484 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23485 23486 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23487 23488 /* Set the TCP sequence number. */ 23489 U32_TO_ABE32(seq_no, tcph->th_seq); 23490 23491 /* Set up the TCP flag field. */ 23492 tcph->th_flags[0] = (uchar_t)TH_ACK; 23493 if (tcp->tcp_ecn_echo_on) 23494 tcph->th_flags[0] |= TH_ECE; 23495 23496 tcp->tcp_rack = tcp->tcp_rnxt; 23497 tcp->tcp_rack_cnt = 0; 23498 23499 /* fill in timestamp option if in use */ 23500 if (tcp->tcp_snd_ts_ok) { 23501 uint32_t llbolt = (uint32_t)lbolt; 23502 23503 U32_TO_BE32(llbolt, 23504 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23505 U32_TO_BE32(tcp->tcp_ts_recent, 23506 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23507 } 23508 23509 /* Fill in SACK options */ 23510 if (num_sack_blk > 0) { 23511 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23512 sack_blk_t *tmp; 23513 int32_t i; 23514 23515 wptr[0] = TCPOPT_NOP; 23516 wptr[1] = TCPOPT_NOP; 23517 wptr[2] = TCPOPT_SACK; 23518 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23519 sizeof (sack_blk_t); 23520 wptr += TCPOPT_REAL_SACK_LEN; 23521 23522 tmp = tcp->tcp_sack_list; 23523 for (i = 0; i < num_sack_blk; i++) { 23524 U32_TO_BE32(tmp[i].begin, wptr); 23525 wptr += sizeof (tcp_seq); 23526 U32_TO_BE32(tmp[i].end, wptr); 23527 wptr += sizeof (tcp_seq); 23528 } 23529 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23530 << 4); 23531 } 23532 23533 if (tcp->tcp_ipversion == IPV4_VERSION) { 23534 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23535 } else { 23536 /* Check for ip6i_t header in sticky hdrs */ 23537 ip6_t *ip6 = (ip6_t *)(rptr + 23538 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23539 sizeof (ip6i_t) : 0)); 23540 23541 ip6->ip6_plen = htons(tcp_hdr_len - 23542 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23543 } 23544 23545 /* 23546 * Prime pump for checksum calculation in IP. Include the 23547 * adjustment for a source route if any. 23548 */ 23549 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23550 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23551 U16_TO_ABE16(data_length, tcph->th_sum); 23552 23553 if (tcp->tcp_ip_forward_progress) { 23554 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23555 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23556 tcp->tcp_ip_forward_progress = B_FALSE; 23557 } 23558 return (mp1); 23559 } 23560 } 23561 23562 /* 23563 * To create a temporary tcp structure for inserting into bind hash list. 23564 * The parameter is assumed to be in network byte order, ready for use. 23565 */ 23566 /* ARGSUSED */ 23567 static tcp_t * 23568 tcp_alloc_temp_tcp(in_port_t port, tcp_stack_t *tcps) 23569 { 23570 conn_t *connp; 23571 tcp_t *tcp; 23572 23573 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP, tcps->tcps_netstack); 23574 if (connp == NULL) 23575 return (NULL); 23576 23577 tcp = connp->conn_tcp; 23578 tcp->tcp_tcps = tcps; 23579 TCPS_REFHOLD(tcps); 23580 23581 /* 23582 * Only initialize the necessary info in those structures. Note 23583 * that since INADDR_ANY is all 0, we do not need to set 23584 * tcp_bound_source to INADDR_ANY here. 23585 */ 23586 tcp->tcp_state = TCPS_BOUND; 23587 tcp->tcp_lport = port; 23588 tcp->tcp_exclbind = 1; 23589 tcp->tcp_reserved_port = 1; 23590 23591 /* Just for place holding... */ 23592 tcp->tcp_ipversion = IPV4_VERSION; 23593 23594 return (tcp); 23595 } 23596 23597 /* 23598 * To remove a port range specified by lo_port and hi_port from the 23599 * reserved port ranges. This is one of the three public functions of 23600 * the reserved port interface. Note that a port range has to be removed 23601 * as a whole. Ports in a range cannot be removed individually. 23602 * 23603 * Params: 23604 * in_port_t lo_port: the beginning port of the reserved port range to 23605 * be deleted. 23606 * in_port_t hi_port: the ending port of the reserved port range to 23607 * be deleted. 23608 * 23609 * Return: 23610 * B_TRUE if the deletion is successful, B_FALSE otherwise. 23611 * 23612 * Assumes that nca is only for zoneid=0 23613 */ 23614 boolean_t 23615 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 23616 { 23617 int i, j; 23618 int size; 23619 tcp_t **temp_tcp_array; 23620 tcp_t *tcp; 23621 tcp_stack_t *tcps; 23622 23623 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23624 ASSERT(tcps != NULL); 23625 23626 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23627 23628 /* First make sure that the port ranage is indeed reserved. */ 23629 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23630 if (tcps->tcps_reserved_port[i].lo_port == lo_port) { 23631 hi_port = tcps->tcps_reserved_port[i].hi_port; 23632 temp_tcp_array = 23633 tcps->tcps_reserved_port[i].temp_tcp_array; 23634 break; 23635 } 23636 } 23637 if (i == tcps->tcps_reserved_port_array_size) { 23638 rw_exit(&tcps->tcps_reserved_port_lock); 23639 netstack_rele(tcps->tcps_netstack); 23640 return (B_FALSE); 23641 } 23642 23643 /* 23644 * Remove the range from the array. This simple loop is possible 23645 * because port ranges are inserted in ascending order. 23646 */ 23647 for (j = i; j < tcps->tcps_reserved_port_array_size - 1; j++) { 23648 tcps->tcps_reserved_port[j].lo_port = 23649 tcps->tcps_reserved_port[j+1].lo_port; 23650 tcps->tcps_reserved_port[j].hi_port = 23651 tcps->tcps_reserved_port[j+1].hi_port; 23652 tcps->tcps_reserved_port[j].temp_tcp_array = 23653 tcps->tcps_reserved_port[j+1].temp_tcp_array; 23654 } 23655 23656 /* Remove all the temporary tcp structures. */ 23657 size = hi_port - lo_port + 1; 23658 while (size > 0) { 23659 tcp = temp_tcp_array[size - 1]; 23660 ASSERT(tcp != NULL); 23661 tcp_bind_hash_remove(tcp); 23662 CONN_DEC_REF(tcp->tcp_connp); 23663 size--; 23664 } 23665 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 23666 tcps->tcps_reserved_port_array_size--; 23667 rw_exit(&tcps->tcps_reserved_port_lock); 23668 netstack_rele(tcps->tcps_netstack); 23669 return (B_TRUE); 23670 } 23671 23672 /* 23673 * Macro to remove temporary tcp structure from the bind hash list. The 23674 * first parameter is the list of tcp to be removed. The second parameter 23675 * is the number of tcps in the array. 23676 */ 23677 #define TCP_TMP_TCP_REMOVE(tcp_array, num, tcps) \ 23678 { \ 23679 while ((num) > 0) { \ 23680 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 23681 tf_t *tbf; \ 23682 tcp_t *tcpnext; \ 23683 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 23684 mutex_enter(&tbf->tf_lock); \ 23685 tcpnext = tcp->tcp_bind_hash; \ 23686 if (tcpnext) { \ 23687 tcpnext->tcp_ptpbhn = \ 23688 tcp->tcp_ptpbhn; \ 23689 } \ 23690 *tcp->tcp_ptpbhn = tcpnext; \ 23691 mutex_exit(&tbf->tf_lock); \ 23692 kmem_free(tcp, sizeof (tcp_t)); \ 23693 (tcp_array)[(num) - 1] = NULL; \ 23694 (num)--; \ 23695 } \ 23696 } 23697 23698 /* 23699 * The public interface for other modules to call to reserve a port range 23700 * in TCP. The caller passes in how large a port range it wants. TCP 23701 * will try to find a range and return it via lo_port and hi_port. This is 23702 * used by NCA's nca_conn_init. 23703 * NCA can only be used in the global zone so this only affects the global 23704 * zone's ports. 23705 * 23706 * Params: 23707 * int size: the size of the port range to be reserved. 23708 * in_port_t *lo_port (referenced): returns the beginning port of the 23709 * reserved port range added. 23710 * in_port_t *hi_port (referenced): returns the ending port of the 23711 * reserved port range added. 23712 * 23713 * Return: 23714 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 23715 * 23716 * Assumes that nca is only for zoneid=0 23717 */ 23718 boolean_t 23719 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 23720 { 23721 tcp_t *tcp; 23722 tcp_t *tmp_tcp; 23723 tcp_t **temp_tcp_array; 23724 tf_t *tbf; 23725 in_port_t net_port; 23726 in_port_t port; 23727 int32_t cur_size; 23728 int i, j; 23729 boolean_t used; 23730 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 23731 zoneid_t zoneid = GLOBAL_ZONEID; 23732 tcp_stack_t *tcps; 23733 23734 /* Sanity check. */ 23735 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 23736 return (B_FALSE); 23737 } 23738 23739 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23740 ASSERT(tcps != NULL); 23741 23742 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23743 if (tcps->tcps_reserved_port_array_size == 23744 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 23745 rw_exit(&tcps->tcps_reserved_port_lock); 23746 netstack_rele(tcps->tcps_netstack); 23747 return (B_FALSE); 23748 } 23749 23750 /* 23751 * Find the starting port to try. Since the port ranges are ordered 23752 * in the reserved port array, we can do a simple search here. 23753 */ 23754 *lo_port = TCP_SMALLEST_RESERVED_PORT; 23755 *hi_port = TCP_LARGEST_RESERVED_PORT; 23756 for (i = 0; i < tcps->tcps_reserved_port_array_size; 23757 *lo_port = tcps->tcps_reserved_port[i].hi_port + 1, i++) { 23758 if (tcps->tcps_reserved_port[i].lo_port - *lo_port >= size) { 23759 *hi_port = tcps->tcps_reserved_port[i].lo_port - 1; 23760 break; 23761 } 23762 } 23763 /* No available port range. */ 23764 if (i == tcps->tcps_reserved_port_array_size && 23765 *hi_port - *lo_port < size) { 23766 rw_exit(&tcps->tcps_reserved_port_lock); 23767 netstack_rele(tcps->tcps_netstack); 23768 return (B_FALSE); 23769 } 23770 23771 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 23772 if (temp_tcp_array == NULL) { 23773 rw_exit(&tcps->tcps_reserved_port_lock); 23774 netstack_rele(tcps->tcps_netstack); 23775 return (B_FALSE); 23776 } 23777 23778 /* Go thru the port range to see if some ports are already bound. */ 23779 for (port = *lo_port, cur_size = 0; 23780 cur_size < size && port <= *hi_port; 23781 cur_size++, port++) { 23782 used = B_FALSE; 23783 net_port = htons(port); 23784 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(net_port)]; 23785 mutex_enter(&tbf->tf_lock); 23786 for (tcp = tbf->tf_tcp; tcp != NULL; 23787 tcp = tcp->tcp_bind_hash) { 23788 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 23789 net_port == tcp->tcp_lport) { 23790 /* 23791 * A port is already bound. Search again 23792 * starting from port + 1. Release all 23793 * temporary tcps. 23794 */ 23795 mutex_exit(&tbf->tf_lock); 23796 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23797 tcps); 23798 *lo_port = port + 1; 23799 cur_size = -1; 23800 used = B_TRUE; 23801 break; 23802 } 23803 } 23804 if (!used) { 23805 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port, tcps)) == 23806 NULL) { 23807 /* 23808 * Allocation failure. Just fail the request. 23809 * Need to remove all those temporary tcp 23810 * structures. 23811 */ 23812 mutex_exit(&tbf->tf_lock); 23813 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23814 tcps); 23815 rw_exit(&tcps->tcps_reserved_port_lock); 23816 kmem_free(temp_tcp_array, 23817 (hi_port - lo_port + 1) * 23818 sizeof (tcp_t *)); 23819 netstack_rele(tcps->tcps_netstack); 23820 return (B_FALSE); 23821 } 23822 temp_tcp_array[cur_size] = tmp_tcp; 23823 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 23824 mutex_exit(&tbf->tf_lock); 23825 } 23826 } 23827 23828 /* 23829 * The current range is not large enough. We can actually do another 23830 * search if this search is done between 2 reserved port ranges. But 23831 * for first release, we just stop here and return saying that no port 23832 * range is available. 23833 */ 23834 if (cur_size < size) { 23835 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, tcps); 23836 rw_exit(&tcps->tcps_reserved_port_lock); 23837 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 23838 netstack_rele(tcps->tcps_netstack); 23839 return (B_FALSE); 23840 } 23841 *hi_port = port - 1; 23842 23843 /* 23844 * Insert range into array in ascending order. Since this function 23845 * must not be called often, we choose to use the simplest method. 23846 * The above array should not consume excessive stack space as 23847 * the size must be very small. If in future releases, we find 23848 * that we should provide more reserved port ranges, this function 23849 * has to be modified to be more efficient. 23850 */ 23851 if (tcps->tcps_reserved_port_array_size == 0) { 23852 tcps->tcps_reserved_port[0].lo_port = *lo_port; 23853 tcps->tcps_reserved_port[0].hi_port = *hi_port; 23854 tcps->tcps_reserved_port[0].temp_tcp_array = temp_tcp_array; 23855 } else { 23856 for (i = 0, j = 0; i < tcps->tcps_reserved_port_array_size; 23857 i++, j++) { 23858 if (*lo_port < tcps->tcps_reserved_port[i].lo_port && 23859 i == j) { 23860 tmp_ports[j].lo_port = *lo_port; 23861 tmp_ports[j].hi_port = *hi_port; 23862 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23863 j++; 23864 } 23865 tmp_ports[j].lo_port = 23866 tcps->tcps_reserved_port[i].lo_port; 23867 tmp_ports[j].hi_port = 23868 tcps->tcps_reserved_port[i].hi_port; 23869 tmp_ports[j].temp_tcp_array = 23870 tcps->tcps_reserved_port[i].temp_tcp_array; 23871 } 23872 if (j == i) { 23873 tmp_ports[j].lo_port = *lo_port; 23874 tmp_ports[j].hi_port = *hi_port; 23875 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23876 } 23877 bcopy(tmp_ports, tcps->tcps_reserved_port, sizeof (tmp_ports)); 23878 } 23879 tcps->tcps_reserved_port_array_size++; 23880 rw_exit(&tcps->tcps_reserved_port_lock); 23881 netstack_rele(tcps->tcps_netstack); 23882 return (B_TRUE); 23883 } 23884 23885 /* 23886 * Check to see if a port is in any reserved port range. 23887 * 23888 * Params: 23889 * in_port_t port: the port to be verified. 23890 * 23891 * Return: 23892 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 23893 */ 23894 boolean_t 23895 tcp_reserved_port_check(in_port_t port, tcp_stack_t *tcps) 23896 { 23897 int i; 23898 23899 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23900 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23901 if (port >= tcps->tcps_reserved_port[i].lo_port || 23902 port <= tcps->tcps_reserved_port[i].hi_port) { 23903 rw_exit(&tcps->tcps_reserved_port_lock); 23904 return (B_TRUE); 23905 } 23906 } 23907 rw_exit(&tcps->tcps_reserved_port_lock); 23908 return (B_FALSE); 23909 } 23910 23911 /* 23912 * To list all reserved port ranges. This is the function to handle 23913 * ndd tcp_reserved_port_list. 23914 */ 23915 /* ARGSUSED */ 23916 static int 23917 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23918 { 23919 int i; 23920 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23921 23922 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23923 if (tcps->tcps_reserved_port_array_size > 0) 23924 (void) mi_mpprintf(mp, "The following ports are reserved:"); 23925 else 23926 (void) mi_mpprintf(mp, "No port is reserved."); 23927 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23928 (void) mi_mpprintf(mp, "%d-%d", 23929 tcps->tcps_reserved_port[i].lo_port, 23930 tcps->tcps_reserved_port[i].hi_port); 23931 } 23932 rw_exit(&tcps->tcps_reserved_port_lock); 23933 return (0); 23934 } 23935 23936 /* 23937 * Hash list insertion routine for tcp_t structures. 23938 * Inserts entries with the ones bound to a specific IP address first 23939 * followed by those bound to INADDR_ANY. 23940 */ 23941 static void 23942 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23943 { 23944 tcp_t **tcpp; 23945 tcp_t *tcpnext; 23946 23947 if (tcp->tcp_ptpbhn != NULL) { 23948 ASSERT(!caller_holds_lock); 23949 tcp_bind_hash_remove(tcp); 23950 } 23951 tcpp = &tbf->tf_tcp; 23952 if (!caller_holds_lock) { 23953 mutex_enter(&tbf->tf_lock); 23954 } else { 23955 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23956 } 23957 tcpnext = tcpp[0]; 23958 if (tcpnext) { 23959 /* 23960 * If the new tcp bound to the INADDR_ANY address 23961 * and the first one in the list is not bound to 23962 * INADDR_ANY we skip all entries until we find the 23963 * first one bound to INADDR_ANY. 23964 * This makes sure that applications binding to a 23965 * specific address get preference over those binding to 23966 * INADDR_ANY. 23967 */ 23968 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23969 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23970 while ((tcpnext = tcpp[0]) != NULL && 23971 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23972 tcpp = &(tcpnext->tcp_bind_hash); 23973 if (tcpnext) 23974 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23975 } else 23976 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23977 } 23978 tcp->tcp_bind_hash = tcpnext; 23979 tcp->tcp_ptpbhn = tcpp; 23980 tcpp[0] = tcp; 23981 if (!caller_holds_lock) 23982 mutex_exit(&tbf->tf_lock); 23983 } 23984 23985 /* 23986 * Hash list removal routine for tcp_t structures. 23987 */ 23988 static void 23989 tcp_bind_hash_remove(tcp_t *tcp) 23990 { 23991 tcp_t *tcpnext; 23992 kmutex_t *lockp; 23993 tcp_stack_t *tcps = tcp->tcp_tcps; 23994 23995 if (tcp->tcp_ptpbhn == NULL) 23996 return; 23997 23998 /* 23999 * Extract the lock pointer in case there are concurrent 24000 * hash_remove's for this instance. 24001 */ 24002 ASSERT(tcp->tcp_lport != 0); 24003 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 24004 24005 ASSERT(lockp != NULL); 24006 mutex_enter(lockp); 24007 if (tcp->tcp_ptpbhn) { 24008 tcpnext = tcp->tcp_bind_hash; 24009 if (tcpnext) { 24010 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24011 tcp->tcp_bind_hash = NULL; 24012 } 24013 *tcp->tcp_ptpbhn = tcpnext; 24014 tcp->tcp_ptpbhn = NULL; 24015 } 24016 mutex_exit(lockp); 24017 } 24018 24019 24020 /* 24021 * Hash list lookup routine for tcp_t structures. 24022 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 24023 */ 24024 static tcp_t * 24025 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 24026 { 24027 tf_t *tf; 24028 tcp_t *tcp; 24029 24030 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24031 mutex_enter(&tf->tf_lock); 24032 for (tcp = tf->tf_tcp; tcp != NULL; 24033 tcp = tcp->tcp_acceptor_hash) { 24034 if (tcp->tcp_acceptor_id == id) { 24035 CONN_INC_REF(tcp->tcp_connp); 24036 mutex_exit(&tf->tf_lock); 24037 return (tcp); 24038 } 24039 } 24040 mutex_exit(&tf->tf_lock); 24041 return (NULL); 24042 } 24043 24044 24045 /* 24046 * Hash list insertion routine for tcp_t structures. 24047 */ 24048 void 24049 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24050 { 24051 tf_t *tf; 24052 tcp_t **tcpp; 24053 tcp_t *tcpnext; 24054 tcp_stack_t *tcps = tcp->tcp_tcps; 24055 24056 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24057 24058 if (tcp->tcp_ptpahn != NULL) 24059 tcp_acceptor_hash_remove(tcp); 24060 tcpp = &tf->tf_tcp; 24061 mutex_enter(&tf->tf_lock); 24062 tcpnext = tcpp[0]; 24063 if (tcpnext) 24064 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24065 tcp->tcp_acceptor_hash = tcpnext; 24066 tcp->tcp_ptpahn = tcpp; 24067 tcpp[0] = tcp; 24068 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24069 mutex_exit(&tf->tf_lock); 24070 } 24071 24072 /* 24073 * Hash list removal routine for tcp_t structures. 24074 */ 24075 static void 24076 tcp_acceptor_hash_remove(tcp_t *tcp) 24077 { 24078 tcp_t *tcpnext; 24079 kmutex_t *lockp; 24080 24081 /* 24082 * Extract the lock pointer in case there are concurrent 24083 * hash_remove's for this instance. 24084 */ 24085 lockp = tcp->tcp_acceptor_lockp; 24086 24087 if (tcp->tcp_ptpahn == NULL) 24088 return; 24089 24090 ASSERT(lockp != NULL); 24091 mutex_enter(lockp); 24092 if (tcp->tcp_ptpahn) { 24093 tcpnext = tcp->tcp_acceptor_hash; 24094 if (tcpnext) { 24095 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24096 tcp->tcp_acceptor_hash = NULL; 24097 } 24098 *tcp->tcp_ptpahn = tcpnext; 24099 tcp->tcp_ptpahn = NULL; 24100 } 24101 mutex_exit(lockp); 24102 tcp->tcp_acceptor_lockp = NULL; 24103 } 24104 24105 /* ARGSUSED */ 24106 static int 24107 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 24108 { 24109 int error = 0; 24110 int retval; 24111 char *end; 24112 tcp_hsp_t *hsp; 24113 tcp_hsp_t *hspprev; 24114 ipaddr_t addr = 0; /* Address we're looking for */ 24115 in6_addr_t v6addr; /* Address we're looking for */ 24116 uint32_t hash; /* Hash of that address */ 24117 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24118 24119 /* 24120 * If the following variables are still zero after parsing the input 24121 * string, the user didn't specify them and we don't change them in 24122 * the HSP. 24123 */ 24124 24125 ipaddr_t mask = 0; /* Subnet mask */ 24126 in6_addr_t v6mask; 24127 long sendspace = 0; /* Send buffer size */ 24128 long recvspace = 0; /* Receive buffer size */ 24129 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 24130 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 24131 24132 rw_enter(&tcps->tcps_hsp_lock, RW_WRITER); 24133 24134 /* Parse and validate address */ 24135 if (af == AF_INET) { 24136 retval = inet_pton(af, value, &addr); 24137 if (retval == 1) 24138 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 24139 } else if (af == AF_INET6) { 24140 retval = inet_pton(af, value, &v6addr); 24141 } else { 24142 error = EINVAL; 24143 goto done; 24144 } 24145 if (retval == 0) { 24146 error = EINVAL; 24147 goto done; 24148 } 24149 24150 while ((*value) && *value != ' ') 24151 value++; 24152 24153 /* Parse individual keywords, set variables if found */ 24154 while (*value) { 24155 /* Skip leading blanks */ 24156 24157 while (*value == ' ' || *value == '\t') 24158 value++; 24159 24160 /* If at end of string, we're done */ 24161 24162 if (!*value) 24163 break; 24164 24165 /* We have a word, figure out what it is */ 24166 24167 if (strncmp("mask", value, 4) == 0) { 24168 value += 4; 24169 while (*value == ' ' || *value == '\t') 24170 value++; 24171 /* Parse subnet mask */ 24172 if (af == AF_INET) { 24173 retval = inet_pton(af, value, &mask); 24174 if (retval == 1) { 24175 V4MASK_TO_V6(mask, v6mask); 24176 } 24177 } else if (af == AF_INET6) { 24178 retval = inet_pton(af, value, &v6mask); 24179 } 24180 if (retval != 1) { 24181 error = EINVAL; 24182 goto done; 24183 } 24184 while ((*value) && *value != ' ') 24185 value++; 24186 } else if (strncmp("sendspace", value, 9) == 0) { 24187 value += 9; 24188 24189 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 24190 sendspace < TCP_XMIT_HIWATER || 24191 sendspace >= (1L<<30)) { 24192 error = EINVAL; 24193 goto done; 24194 } 24195 value = end; 24196 } else if (strncmp("recvspace", value, 9) == 0) { 24197 value += 9; 24198 24199 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 24200 recvspace < TCP_RECV_HIWATER || 24201 recvspace >= (1L<<30)) { 24202 error = EINVAL; 24203 goto done; 24204 } 24205 value = end; 24206 } else if (strncmp("timestamp", value, 9) == 0) { 24207 value += 9; 24208 24209 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 24210 timestamp < 0 || timestamp > 1) { 24211 error = EINVAL; 24212 goto done; 24213 } 24214 24215 /* 24216 * We increment timestamp so we know it's been set; 24217 * this is undone when we put it in the HSP 24218 */ 24219 timestamp++; 24220 value = end; 24221 } else if (strncmp("delete", value, 6) == 0) { 24222 value += 6; 24223 delete = B_TRUE; 24224 } else { 24225 error = EINVAL; 24226 goto done; 24227 } 24228 } 24229 24230 /* Hash address for lookup */ 24231 24232 hash = TCP_HSP_HASH(addr); 24233 24234 if (delete) { 24235 /* 24236 * Note that deletes don't return an error if the thing 24237 * we're trying to delete isn't there. 24238 */ 24239 if (tcps->tcps_hsp_hash == NULL) 24240 goto done; 24241 hsp = tcps->tcps_hsp_hash[hash]; 24242 24243 if (hsp) { 24244 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24245 &v6addr)) { 24246 tcps->tcps_hsp_hash[hash] = hsp->tcp_hsp_next; 24247 mi_free((char *)hsp); 24248 } else { 24249 hspprev = hsp; 24250 while ((hsp = hsp->tcp_hsp_next) != NULL) { 24251 if (IN6_ARE_ADDR_EQUAL( 24252 &hsp->tcp_hsp_addr_v6, &v6addr)) { 24253 hspprev->tcp_hsp_next = 24254 hsp->tcp_hsp_next; 24255 mi_free((char *)hsp); 24256 break; 24257 } 24258 hspprev = hsp; 24259 } 24260 } 24261 } 24262 } else { 24263 /* 24264 * We're adding/modifying an HSP. If we haven't already done 24265 * so, allocate the hash table. 24266 */ 24267 24268 if (!tcps->tcps_hsp_hash) { 24269 tcps->tcps_hsp_hash = (tcp_hsp_t **) 24270 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 24271 if (!tcps->tcps_hsp_hash) { 24272 error = EINVAL; 24273 goto done; 24274 } 24275 } 24276 24277 /* Get head of hash chain */ 24278 24279 hsp = tcps->tcps_hsp_hash[hash]; 24280 24281 /* Try to find pre-existing hsp on hash chain */ 24282 /* Doesn't handle CIDR prefixes. */ 24283 while (hsp) { 24284 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 24285 break; 24286 hsp = hsp->tcp_hsp_next; 24287 } 24288 24289 /* 24290 * If we didn't, create one with default values and put it 24291 * at head of hash chain 24292 */ 24293 24294 if (!hsp) { 24295 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 24296 if (!hsp) { 24297 error = EINVAL; 24298 goto done; 24299 } 24300 hsp->tcp_hsp_next = tcps->tcps_hsp_hash[hash]; 24301 tcps->tcps_hsp_hash[hash] = hsp; 24302 } 24303 24304 /* Set values that the user asked us to change */ 24305 24306 hsp->tcp_hsp_addr_v6 = v6addr; 24307 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 24308 hsp->tcp_hsp_vers = IPV4_VERSION; 24309 else 24310 hsp->tcp_hsp_vers = IPV6_VERSION; 24311 hsp->tcp_hsp_subnet_v6 = v6mask; 24312 if (sendspace > 0) 24313 hsp->tcp_hsp_sendspace = sendspace; 24314 if (recvspace > 0) 24315 hsp->tcp_hsp_recvspace = recvspace; 24316 if (timestamp > 0) 24317 hsp->tcp_hsp_tstamp = timestamp - 1; 24318 } 24319 24320 done: 24321 rw_exit(&tcps->tcps_hsp_lock); 24322 return (error); 24323 } 24324 24325 /* Set callback routine passed to nd_load by tcp_param_register. */ 24326 /* ARGSUSED */ 24327 static int 24328 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 24329 { 24330 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 24331 } 24332 /* ARGSUSED */ 24333 static int 24334 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24335 cred_t *cr) 24336 { 24337 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 24338 } 24339 24340 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 24341 /* ARGSUSED */ 24342 static int 24343 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 24344 { 24345 tcp_hsp_t *hsp; 24346 int i; 24347 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 24348 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24349 24350 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24351 (void) mi_mpprintf(mp, 24352 "Hash HSP " MI_COL_HDRPAD_STR 24353 "Address Subnet Mask Send Receive TStamp"); 24354 if (tcps->tcps_hsp_hash) { 24355 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 24356 hsp = tcps->tcps_hsp_hash[i]; 24357 while (hsp) { 24358 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 24359 (void) inet_ntop(AF_INET, 24360 &hsp->tcp_hsp_addr, 24361 addrbuf, sizeof (addrbuf)); 24362 (void) inet_ntop(AF_INET, 24363 &hsp->tcp_hsp_subnet, 24364 subnetbuf, sizeof (subnetbuf)); 24365 } else { 24366 (void) inet_ntop(AF_INET6, 24367 &hsp->tcp_hsp_addr_v6, 24368 addrbuf, sizeof (addrbuf)); 24369 (void) inet_ntop(AF_INET6, 24370 &hsp->tcp_hsp_subnet_v6, 24371 subnetbuf, sizeof (subnetbuf)); 24372 } 24373 (void) mi_mpprintf(mp, 24374 " %03d " MI_COL_PTRFMT_STR 24375 "%s %s %010d %010d %d", 24376 i, 24377 (void *)hsp, 24378 addrbuf, 24379 subnetbuf, 24380 hsp->tcp_hsp_sendspace, 24381 hsp->tcp_hsp_recvspace, 24382 hsp->tcp_hsp_tstamp); 24383 24384 hsp = hsp->tcp_hsp_next; 24385 } 24386 } 24387 } 24388 rw_exit(&tcps->tcps_hsp_lock); 24389 return (0); 24390 } 24391 24392 24393 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24394 24395 static ipaddr_t netmasks[] = { 24396 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24397 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24398 }; 24399 24400 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24401 24402 /* 24403 * XXX This routine should go away and instead we should use the metrics 24404 * associated with the routes to determine the default sndspace and rcvspace. 24405 */ 24406 static tcp_hsp_t * 24407 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24408 { 24409 tcp_hsp_t *hsp = NULL; 24410 24411 /* Quick check without acquiring the lock. */ 24412 if (tcps->tcps_hsp_hash == NULL) 24413 return (NULL); 24414 24415 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24416 24417 /* This routine finds the best-matching HSP for address addr. */ 24418 24419 if (tcps->tcps_hsp_hash) { 24420 int i; 24421 ipaddr_t srchaddr; 24422 tcp_hsp_t *hsp_net; 24423 24424 /* We do three passes: host, network, and subnet. */ 24425 24426 srchaddr = addr; 24427 24428 for (i = 1; i <= 3; i++) { 24429 /* Look for exact match on srchaddr */ 24430 24431 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24432 while (hsp) { 24433 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24434 hsp->tcp_hsp_addr == srchaddr) 24435 break; 24436 hsp = hsp->tcp_hsp_next; 24437 } 24438 ASSERT(hsp == NULL || 24439 hsp->tcp_hsp_vers == IPV4_VERSION); 24440 24441 /* 24442 * If this is the first pass: 24443 * If we found a match, great, return it. 24444 * If not, search for the network on the second pass. 24445 */ 24446 24447 if (i == 1) 24448 if (hsp) 24449 break; 24450 else 24451 { 24452 srchaddr = addr & netmask(addr); 24453 continue; 24454 } 24455 24456 /* 24457 * If this is the second pass: 24458 * If we found a match, but there's a subnet mask, 24459 * save the match but try again using the subnet 24460 * mask on the third pass. 24461 * Otherwise, return whatever we found. 24462 */ 24463 24464 if (i == 2) { 24465 if (hsp && hsp->tcp_hsp_subnet) { 24466 hsp_net = hsp; 24467 srchaddr = addr & hsp->tcp_hsp_subnet; 24468 continue; 24469 } else { 24470 break; 24471 } 24472 } 24473 24474 /* 24475 * This must be the third pass. If we didn't find 24476 * anything, return the saved network HSP instead. 24477 */ 24478 24479 if (!hsp) 24480 hsp = hsp_net; 24481 } 24482 } 24483 24484 rw_exit(&tcps->tcps_hsp_lock); 24485 return (hsp); 24486 } 24487 24488 /* 24489 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24490 * match lookup. 24491 */ 24492 static tcp_hsp_t * 24493 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24494 { 24495 tcp_hsp_t *hsp = NULL; 24496 24497 /* Quick check without acquiring the lock. */ 24498 if (tcps->tcps_hsp_hash == NULL) 24499 return (NULL); 24500 24501 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24502 24503 /* This routine finds the best-matching HSP for address addr. */ 24504 24505 if (tcps->tcps_hsp_hash) { 24506 int i; 24507 in6_addr_t v6srchaddr; 24508 tcp_hsp_t *hsp_net; 24509 24510 /* We do three passes: host, network, and subnet. */ 24511 24512 v6srchaddr = *v6addr; 24513 24514 for (i = 1; i <= 3; i++) { 24515 /* Look for exact match on srchaddr */ 24516 24517 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24518 V4_PART_OF_V6(v6srchaddr))]; 24519 while (hsp) { 24520 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24521 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24522 &v6srchaddr)) 24523 break; 24524 hsp = hsp->tcp_hsp_next; 24525 } 24526 24527 /* 24528 * If this is the first pass: 24529 * If we found a match, great, return it. 24530 * If not, search for the network on the second pass. 24531 */ 24532 24533 if (i == 1) 24534 if (hsp) 24535 break; 24536 else { 24537 /* Assume a 64 bit mask */ 24538 v6srchaddr.s6_addr32[0] = 24539 v6addr->s6_addr32[0]; 24540 v6srchaddr.s6_addr32[1] = 24541 v6addr->s6_addr32[1]; 24542 v6srchaddr.s6_addr32[2] = 0; 24543 v6srchaddr.s6_addr32[3] = 0; 24544 continue; 24545 } 24546 24547 /* 24548 * If this is the second pass: 24549 * If we found a match, but there's a subnet mask, 24550 * save the match but try again using the subnet 24551 * mask on the third pass. 24552 * Otherwise, return whatever we found. 24553 */ 24554 24555 if (i == 2) { 24556 ASSERT(hsp == NULL || 24557 hsp->tcp_hsp_vers == IPV6_VERSION); 24558 if (hsp && 24559 !IN6_IS_ADDR_UNSPECIFIED( 24560 &hsp->tcp_hsp_subnet_v6)) { 24561 hsp_net = hsp; 24562 V6_MASK_COPY(*v6addr, 24563 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24564 continue; 24565 } else { 24566 break; 24567 } 24568 } 24569 24570 /* 24571 * This must be the third pass. If we didn't find 24572 * anything, return the saved network HSP instead. 24573 */ 24574 24575 if (!hsp) 24576 hsp = hsp_net; 24577 } 24578 } 24579 24580 rw_exit(&tcps->tcps_hsp_lock); 24581 return (hsp); 24582 } 24583 24584 /* 24585 * Type three generator adapted from the random() function in 4.4 BSD: 24586 */ 24587 24588 /* 24589 * Copyright (c) 1983, 1993 24590 * The Regents of the University of California. All rights reserved. 24591 * 24592 * Redistribution and use in source and binary forms, with or without 24593 * modification, are permitted provided that the following conditions 24594 * are met: 24595 * 1. Redistributions of source code must retain the above copyright 24596 * notice, this list of conditions and the following disclaimer. 24597 * 2. Redistributions in binary form must reproduce the above copyright 24598 * notice, this list of conditions and the following disclaimer in the 24599 * documentation and/or other materials provided with the distribution. 24600 * 3. All advertising materials mentioning features or use of this software 24601 * must display the following acknowledgement: 24602 * This product includes software developed by the University of 24603 * California, Berkeley and its contributors. 24604 * 4. Neither the name of the University nor the names of its contributors 24605 * may be used to endorse or promote products derived from this software 24606 * without specific prior written permission. 24607 * 24608 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24609 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24610 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24611 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24612 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24613 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24614 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24615 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24616 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24617 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24618 * SUCH DAMAGE. 24619 */ 24620 24621 /* Type 3 -- x**31 + x**3 + 1 */ 24622 #define DEG_3 31 24623 #define SEP_3 3 24624 24625 24626 /* Protected by tcp_random_lock */ 24627 static int tcp_randtbl[DEG_3 + 1]; 24628 24629 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24630 static int *tcp_random_rptr = &tcp_randtbl[1]; 24631 24632 static int *tcp_random_state = &tcp_randtbl[1]; 24633 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24634 24635 kmutex_t tcp_random_lock; 24636 24637 void 24638 tcp_random_init(void) 24639 { 24640 int i; 24641 hrtime_t hrt; 24642 time_t wallclock; 24643 uint64_t result; 24644 24645 /* 24646 * Use high-res timer and current time for seed. Gethrtime() returns 24647 * a longlong, which may contain resolution down to nanoseconds. 24648 * The current time will either be a 32-bit or a 64-bit quantity. 24649 * XOR the two together in a 64-bit result variable. 24650 * Convert the result to a 32-bit value by multiplying the high-order 24651 * 32-bits by the low-order 32-bits. 24652 */ 24653 24654 hrt = gethrtime(); 24655 (void) drv_getparm(TIME, &wallclock); 24656 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24657 mutex_enter(&tcp_random_lock); 24658 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24659 (result & 0xffffffff); 24660 24661 for (i = 1; i < DEG_3; i++) 24662 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24663 + 12345; 24664 tcp_random_fptr = &tcp_random_state[SEP_3]; 24665 tcp_random_rptr = &tcp_random_state[0]; 24666 mutex_exit(&tcp_random_lock); 24667 for (i = 0; i < 10 * DEG_3; i++) 24668 (void) tcp_random(); 24669 } 24670 24671 /* 24672 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24673 * This range is selected to be approximately centered on TCP_ISS / 2, 24674 * and easy to compute. We get this value by generating a 32-bit random 24675 * number, selecting out the high-order 17 bits, and then adding one so 24676 * that we never return zero. 24677 */ 24678 int 24679 tcp_random(void) 24680 { 24681 int i; 24682 24683 mutex_enter(&tcp_random_lock); 24684 *tcp_random_fptr += *tcp_random_rptr; 24685 24686 /* 24687 * The high-order bits are more random than the low-order bits, 24688 * so we select out the high-order 17 bits and add one so that 24689 * we never return zero. 24690 */ 24691 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24692 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24693 tcp_random_fptr = tcp_random_state; 24694 ++tcp_random_rptr; 24695 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24696 tcp_random_rptr = tcp_random_state; 24697 24698 mutex_exit(&tcp_random_lock); 24699 return (i); 24700 } 24701 24702 /* 24703 * XXX This will go away when TPI is extended to send 24704 * info reqs to sockfs/timod ..... 24705 * Given a queue, set the max packet size for the write 24706 * side of the queue below stream head. This value is 24707 * cached on the stream head. 24708 * Returns 1 on success, 0 otherwise. 24709 */ 24710 static int 24711 setmaxps(queue_t *q, int maxpsz) 24712 { 24713 struct stdata *stp; 24714 queue_t *wq; 24715 stp = STREAM(q); 24716 24717 /* 24718 * At this point change of a queue parameter is not allowed 24719 * when a multiplexor is sitting on top. 24720 */ 24721 if (stp->sd_flag & STPLEX) 24722 return (0); 24723 24724 claimstr(stp->sd_wrq); 24725 wq = stp->sd_wrq->q_next; 24726 ASSERT(wq != NULL); 24727 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24728 releasestr(stp->sd_wrq); 24729 return (1); 24730 } 24731 24732 static int 24733 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24734 int *t_errorp, int *sys_errorp) 24735 { 24736 int error; 24737 int is_absreq_failure; 24738 t_scalar_t *opt_lenp; 24739 t_scalar_t opt_offset; 24740 int prim_type; 24741 struct T_conn_req *tcreqp; 24742 struct T_conn_res *tcresp; 24743 cred_t *cr; 24744 24745 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24746 24747 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24748 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24749 prim_type == T_CONN_RES); 24750 24751 switch (prim_type) { 24752 case T_CONN_REQ: 24753 tcreqp = (struct T_conn_req *)mp->b_rptr; 24754 opt_offset = tcreqp->OPT_offset; 24755 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24756 break; 24757 case O_T_CONN_RES: 24758 case T_CONN_RES: 24759 tcresp = (struct T_conn_res *)mp->b_rptr; 24760 opt_offset = tcresp->OPT_offset; 24761 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24762 break; 24763 } 24764 24765 *t_errorp = 0; 24766 *sys_errorp = 0; 24767 *do_disconnectp = 0; 24768 24769 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24770 opt_offset, cr, &tcp_opt_obj, 24771 NULL, &is_absreq_failure); 24772 24773 switch (error) { 24774 case 0: /* no error */ 24775 ASSERT(is_absreq_failure == 0); 24776 return (0); 24777 case ENOPROTOOPT: 24778 *t_errorp = TBADOPT; 24779 break; 24780 case EACCES: 24781 *t_errorp = TACCES; 24782 break; 24783 default: 24784 *t_errorp = TSYSERR; *sys_errorp = error; 24785 break; 24786 } 24787 if (is_absreq_failure != 0) { 24788 /* 24789 * The connection request should get the local ack 24790 * T_OK_ACK and then a T_DISCON_IND. 24791 */ 24792 *do_disconnectp = 1; 24793 } 24794 return (-1); 24795 } 24796 24797 /* 24798 * Split this function out so that if the secret changes, I'm okay. 24799 * 24800 * Initialize the tcp_iss_cookie and tcp_iss_key. 24801 */ 24802 24803 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24804 24805 static void 24806 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24807 { 24808 struct { 24809 int32_t current_time; 24810 uint32_t randnum; 24811 uint16_t pad; 24812 uint8_t ether[6]; 24813 uint8_t passwd[PASSWD_SIZE]; 24814 } tcp_iss_cookie; 24815 time_t t; 24816 24817 /* 24818 * Start with the current absolute time. 24819 */ 24820 (void) drv_getparm(TIME, &t); 24821 tcp_iss_cookie.current_time = t; 24822 24823 /* 24824 * XXX - Need a more random number per RFC 1750, not this crap. 24825 * OTOH, if what follows is pretty random, then I'm in better shape. 24826 */ 24827 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24828 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24829 24830 /* 24831 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24832 * as a good template. 24833 */ 24834 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24835 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24836 24837 /* 24838 * The pass-phrase. Normally this is supplied by user-called NDD. 24839 */ 24840 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24841 24842 /* 24843 * See 4010593 if this section becomes a problem again, 24844 * but the local ethernet address is useful here. 24845 */ 24846 (void) localetheraddr(NULL, 24847 (struct ether_addr *)&tcp_iss_cookie.ether); 24848 24849 /* 24850 * Hash 'em all together. The MD5Final is called per-connection. 24851 */ 24852 mutex_enter(&tcps->tcps_iss_key_lock); 24853 MD5Init(&tcps->tcps_iss_key); 24854 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24855 sizeof (tcp_iss_cookie)); 24856 mutex_exit(&tcps->tcps_iss_key_lock); 24857 } 24858 24859 /* 24860 * Set the RFC 1948 pass phrase 24861 */ 24862 /* ARGSUSED */ 24863 static int 24864 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24865 cred_t *cr) 24866 { 24867 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24868 24869 /* 24870 * Basically, value contains a new pass phrase. Pass it along! 24871 */ 24872 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24873 return (0); 24874 } 24875 24876 /* ARGSUSED */ 24877 static int 24878 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24879 { 24880 bzero(buf, sizeof (tcp_sack_info_t)); 24881 return (0); 24882 } 24883 24884 /* ARGSUSED */ 24885 static int 24886 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24887 { 24888 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24889 return (0); 24890 } 24891 24892 /* 24893 * Make sure we wait until the default queue is setup, yet allow 24894 * tcp_g_q_create() to open a TCP stream. 24895 * We need to allow tcp_g_q_create() do do an open 24896 * of tcp, hence we compare curhread. 24897 * All others have to wait until the tcps_g_q has been 24898 * setup. 24899 */ 24900 void 24901 tcp_g_q_setup(tcp_stack_t *tcps) 24902 { 24903 mutex_enter(&tcps->tcps_g_q_lock); 24904 if (tcps->tcps_g_q != NULL) { 24905 mutex_exit(&tcps->tcps_g_q_lock); 24906 return; 24907 } 24908 if (tcps->tcps_g_q_creator == NULL) { 24909 /* This thread will set it up */ 24910 tcps->tcps_g_q_creator = curthread; 24911 mutex_exit(&tcps->tcps_g_q_lock); 24912 tcp_g_q_create(tcps); 24913 mutex_enter(&tcps->tcps_g_q_lock); 24914 ASSERT(tcps->tcps_g_q_creator == curthread); 24915 tcps->tcps_g_q_creator = NULL; 24916 cv_signal(&tcps->tcps_g_q_cv); 24917 ASSERT(tcps->tcps_g_q != NULL); 24918 mutex_exit(&tcps->tcps_g_q_lock); 24919 return; 24920 } 24921 /* Everybody but the creator has to wait */ 24922 if (tcps->tcps_g_q_creator != curthread) { 24923 while (tcps->tcps_g_q == NULL) 24924 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24925 } 24926 mutex_exit(&tcps->tcps_g_q_lock); 24927 } 24928 24929 #define IP "ip" 24930 24931 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24932 24933 /* 24934 * Create a default tcp queue here instead of in strplumb 24935 */ 24936 void 24937 tcp_g_q_create(tcp_stack_t *tcps) 24938 { 24939 int error; 24940 ldi_handle_t lh = NULL; 24941 ldi_ident_t li = NULL; 24942 int rval; 24943 cred_t *cr; 24944 major_t IP_MAJ; 24945 24946 #ifdef NS_DEBUG 24947 (void) printf("tcp_g_q_create()\n"); 24948 #endif 24949 24950 IP_MAJ = ddi_name_to_major(IP); 24951 24952 ASSERT(tcps->tcps_g_q_creator == curthread); 24953 24954 error = ldi_ident_from_major(IP_MAJ, &li); 24955 if (error) { 24956 #ifdef DEBUG 24957 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24958 error); 24959 #endif 24960 return; 24961 } 24962 24963 cr = zone_get_kcred(netstackid_to_zoneid( 24964 tcps->tcps_netstack->netstack_stackid)); 24965 ASSERT(cr != NULL); 24966 /* 24967 * We set the tcp default queue to IPv6 because IPv4 falls 24968 * back to IPv6 when it can't find a client, but 24969 * IPv6 does not fall back to IPv4. 24970 */ 24971 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24972 if (error) { 24973 #ifdef DEBUG 24974 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24975 error); 24976 #endif 24977 goto out; 24978 } 24979 24980 /* 24981 * This ioctl causes the tcp framework to cache a pointer to 24982 * this stream, so we don't want to close the stream after 24983 * this operation. 24984 * Use the kernel credentials that are for the zone we're in. 24985 */ 24986 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24987 (intptr_t)0, FKIOCTL, cr, &rval); 24988 if (error) { 24989 #ifdef DEBUG 24990 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24991 "error %d\n", error); 24992 #endif 24993 goto out; 24994 } 24995 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24996 lh = NULL; 24997 out: 24998 /* Close layered handles */ 24999 if (li) 25000 ldi_ident_release(li); 25001 /* Keep cred around until _inactive needs it */ 25002 tcps->tcps_g_q_cr = cr; 25003 } 25004 25005 /* 25006 * We keep tcp_g_q set until all other tcp_t's in the zone 25007 * has gone away, and then when tcp_g_q_inactive() is called 25008 * we clear it. 25009 */ 25010 void 25011 tcp_g_q_destroy(tcp_stack_t *tcps) 25012 { 25013 #ifdef NS_DEBUG 25014 (void) printf("tcp_g_q_destroy()for stack %d\n", 25015 tcps->tcps_netstack->netstack_stackid); 25016 #endif 25017 25018 if (tcps->tcps_g_q == NULL) { 25019 return; /* Nothing to cleanup */ 25020 } 25021 /* 25022 * Drop reference corresponding to the default queue. 25023 * This reference was added from tcp_open when the default queue 25024 * was created, hence we compensate for this extra drop in 25025 * tcp_g_q_close. If the refcnt drops to zero here it means 25026 * the default queue was the last one to be open, in which 25027 * case, then tcp_g_q_inactive will be 25028 * called as a result of the refrele. 25029 */ 25030 TCPS_REFRELE(tcps); 25031 } 25032 25033 /* 25034 * Called when last tcp_t drops reference count using TCPS_REFRELE. 25035 * Run by tcp_q_q_inactive using a taskq. 25036 */ 25037 static void 25038 tcp_g_q_close(void *arg) 25039 { 25040 tcp_stack_t *tcps = arg; 25041 int error; 25042 ldi_handle_t lh = NULL; 25043 ldi_ident_t li = NULL; 25044 cred_t *cr; 25045 major_t IP_MAJ; 25046 25047 IP_MAJ = ddi_name_to_major(IP); 25048 25049 #ifdef NS_DEBUG 25050 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 25051 tcps->tcps_netstack->netstack_stackid, 25052 tcps->tcps_netstack->netstack_refcnt); 25053 #endif 25054 lh = tcps->tcps_g_q_lh; 25055 if (lh == NULL) 25056 return; /* Nothing to cleanup */ 25057 25058 ASSERT(tcps->tcps_refcnt == 1); 25059 ASSERT(tcps->tcps_g_q != NULL); 25060 25061 error = ldi_ident_from_major(IP_MAJ, &li); 25062 if (error) { 25063 #ifdef DEBUG 25064 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 25065 error); 25066 #endif 25067 return; 25068 } 25069 25070 cr = tcps->tcps_g_q_cr; 25071 tcps->tcps_g_q_cr = NULL; 25072 ASSERT(cr != NULL); 25073 25074 /* 25075 * Make sure we can break the recursion when tcp_close decrements 25076 * the reference count causing g_q_inactive to be called again. 25077 */ 25078 tcps->tcps_g_q_lh = NULL; 25079 25080 /* close the default queue */ 25081 (void) ldi_close(lh, FREAD|FWRITE, cr); 25082 /* 25083 * At this point in time tcps and the rest of netstack_t might 25084 * have been deleted. 25085 */ 25086 tcps = NULL; 25087 25088 /* Close layered handles */ 25089 ldi_ident_release(li); 25090 crfree(cr); 25091 } 25092 25093 /* 25094 * Called when last tcp_t drops reference count using TCPS_REFRELE. 25095 * 25096 * Have to ensure that the ldi routines are not used by an 25097 * interrupt thread by using a taskq. 25098 */ 25099 void 25100 tcp_g_q_inactive(tcp_stack_t *tcps) 25101 { 25102 if (tcps->tcps_g_q_lh == NULL) 25103 return; /* Nothing to cleanup */ 25104 25105 ASSERT(tcps->tcps_refcnt == 0); 25106 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 25107 25108 if (servicing_interrupt()) { 25109 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 25110 (void *) tcps, TQ_SLEEP); 25111 } else { 25112 tcp_g_q_close(tcps); 25113 } 25114 } 25115 25116 /* 25117 * Called by IP when IP is loaded into the kernel 25118 */ 25119 void 25120 tcp_ddi_g_init(void) 25121 { 25122 tcp_timercache = kmem_cache_create("tcp_timercache", 25123 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 25124 NULL, NULL, NULL, NULL, NULL, 0); 25125 25126 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 25127 sizeof (tcp_sack_info_t), 0, 25128 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 25129 25130 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 25131 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 25132 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 25133 25134 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 25135 25136 /* Initialize the random number generator */ 25137 tcp_random_init(); 25138 25139 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 25140 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 25141 25142 /* A single callback independently of how many netstacks we have */ 25143 ip_squeue_init(tcp_squeue_add); 25144 25145 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 25146 25147 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 25148 TASKQ_PREPOPULATE); 25149 25150 /* 25151 * We want to be informed each time a stack is created or 25152 * destroyed in the kernel, so we can maintain the 25153 * set of tcp_stack_t's. 25154 */ 25155 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 25156 tcp_stack_fini); 25157 } 25158 25159 25160 /* 25161 * Initialize the TCP stack instance. 25162 */ 25163 static void * 25164 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 25165 { 25166 tcp_stack_t *tcps; 25167 tcpparam_t *pa; 25168 int i; 25169 25170 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 25171 tcps->tcps_netstack = ns; 25172 25173 /* Initialize locks */ 25174 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 25175 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 25176 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 25177 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 25178 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 25179 rw_init(&tcps->tcps_reserved_port_lock, NULL, RW_DEFAULT, NULL); 25180 25181 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 25182 tcps->tcps_g_epriv_ports[0] = 2049; 25183 tcps->tcps_g_epriv_ports[1] = 4045; 25184 tcps->tcps_min_anonpriv_port = 512; 25185 25186 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 25187 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 25188 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 25189 TCP_FANOUT_SIZE, KM_SLEEP); 25190 tcps->tcps_reserved_port = kmem_zalloc(sizeof (tcp_rport_t) * 25191 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE, KM_SLEEP); 25192 25193 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25194 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 25195 MUTEX_DEFAULT, NULL); 25196 } 25197 25198 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25199 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 25200 MUTEX_DEFAULT, NULL); 25201 } 25202 25203 /* TCP's IPsec code calls the packet dropper. */ 25204 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 25205 25206 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 25207 tcps->tcps_params = pa; 25208 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25209 25210 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 25211 A_CNT(lcl_tcp_param_arr), tcps); 25212 25213 /* 25214 * Note: To really walk the device tree you need the devinfo 25215 * pointer to your device which is only available after probe/attach. 25216 * The following is safe only because it uses ddi_root_node() 25217 */ 25218 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 25219 tcp_opt_obj.odb_opt_arr_cnt); 25220 25221 /* 25222 * Initialize RFC 1948 secret values. This will probably be reset once 25223 * by the boot scripts. 25224 * 25225 * Use NULL name, as the name is caught by the new lockstats. 25226 * 25227 * Initialize with some random, non-guessable string, like the global 25228 * T_INFO_ACK. 25229 */ 25230 25231 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 25232 sizeof (tcp_g_t_info_ack), tcps); 25233 25234 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 25235 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 25236 25237 return (tcps); 25238 } 25239 25240 /* 25241 * Called when the IP module is about to be unloaded. 25242 */ 25243 void 25244 tcp_ddi_g_destroy(void) 25245 { 25246 tcp_g_kstat_fini(tcp_g_kstat); 25247 tcp_g_kstat = NULL; 25248 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 25249 25250 mutex_destroy(&tcp_random_lock); 25251 25252 kmem_cache_destroy(tcp_timercache); 25253 kmem_cache_destroy(tcp_sack_info_cache); 25254 kmem_cache_destroy(tcp_iphc_cache); 25255 25256 netstack_unregister(NS_TCP); 25257 taskq_destroy(tcp_taskq); 25258 } 25259 25260 /* 25261 * Shut down the TCP stack instance. 25262 */ 25263 /* ARGSUSED */ 25264 static void 25265 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25266 { 25267 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25268 25269 tcp_g_q_destroy(tcps); 25270 } 25271 25272 /* 25273 * Free the TCP stack instance. 25274 */ 25275 static void 25276 tcp_stack_fini(netstackid_t stackid, void *arg) 25277 { 25278 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25279 int i; 25280 25281 nd_free(&tcps->tcps_g_nd); 25282 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25283 tcps->tcps_params = NULL; 25284 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25285 tcps->tcps_wroff_xtra_param = NULL; 25286 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25287 tcps->tcps_mdt_head_param = NULL; 25288 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25289 tcps->tcps_mdt_tail_param = NULL; 25290 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25291 tcps->tcps_mdt_max_pbufs_param = NULL; 25292 25293 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25294 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25295 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25296 } 25297 25298 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25299 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25300 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25301 } 25302 25303 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25304 tcps->tcps_bind_fanout = NULL; 25305 25306 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25307 tcps->tcps_acceptor_fanout = NULL; 25308 25309 kmem_free(tcps->tcps_reserved_port, sizeof (tcp_rport_t) * 25310 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE); 25311 tcps->tcps_reserved_port = NULL; 25312 25313 mutex_destroy(&tcps->tcps_iss_key_lock); 25314 rw_destroy(&tcps->tcps_hsp_lock); 25315 mutex_destroy(&tcps->tcps_g_q_lock); 25316 cv_destroy(&tcps->tcps_g_q_cv); 25317 mutex_destroy(&tcps->tcps_epriv_port_lock); 25318 rw_destroy(&tcps->tcps_reserved_port_lock); 25319 25320 ip_drop_unregister(&tcps->tcps_dropper); 25321 25322 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25323 tcps->tcps_kstat = NULL; 25324 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25325 25326 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25327 tcps->tcps_mibkp = NULL; 25328 25329 kmem_free(tcps, sizeof (*tcps)); 25330 } 25331 25332 /* 25333 * Generate ISS, taking into account NDD changes may happen halfway through. 25334 * (If the iss is not zero, set it.) 25335 */ 25336 25337 static void 25338 tcp_iss_init(tcp_t *tcp) 25339 { 25340 MD5_CTX context; 25341 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25342 uint32_t answer[4]; 25343 tcp_stack_t *tcps = tcp->tcp_tcps; 25344 25345 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25346 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25347 switch (tcps->tcps_strong_iss) { 25348 case 2: 25349 mutex_enter(&tcps->tcps_iss_key_lock); 25350 context = tcps->tcps_iss_key; 25351 mutex_exit(&tcps->tcps_iss_key_lock); 25352 arg.ports = tcp->tcp_ports; 25353 if (tcp->tcp_ipversion == IPV4_VERSION) { 25354 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25355 &arg.src); 25356 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25357 &arg.dst); 25358 } else { 25359 arg.src = tcp->tcp_ip6h->ip6_src; 25360 arg.dst = tcp->tcp_ip6h->ip6_dst; 25361 } 25362 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25363 MD5Final((uchar_t *)answer, &context); 25364 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25365 /* 25366 * Now that we've hashed into a unique per-connection sequence 25367 * space, add a random increment per strong_iss == 1. So I 25368 * guess we'll have to... 25369 */ 25370 /* FALLTHRU */ 25371 case 1: 25372 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25373 break; 25374 default: 25375 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25376 break; 25377 } 25378 tcp->tcp_valid_bits = TCP_ISS_VALID; 25379 tcp->tcp_fss = tcp->tcp_iss - 1; 25380 tcp->tcp_suna = tcp->tcp_iss; 25381 tcp->tcp_snxt = tcp->tcp_iss + 1; 25382 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25383 tcp->tcp_csuna = tcp->tcp_snxt; 25384 } 25385 25386 /* 25387 * Exported routine for extracting active tcp connection status. 25388 * 25389 * This is used by the Solaris Cluster Networking software to 25390 * gather a list of connections that need to be forwarded to 25391 * specific nodes in the cluster when configuration changes occur. 25392 * 25393 * The callback is invoked for each tcp_t structure. Returning 25394 * non-zero from the callback routine terminates the search. 25395 */ 25396 int 25397 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25398 void *arg) 25399 { 25400 netstack_handle_t nh; 25401 netstack_t *ns; 25402 int ret = 0; 25403 25404 netstack_next_init(&nh); 25405 while ((ns = netstack_next(&nh)) != NULL) { 25406 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25407 ns->netstack_tcp); 25408 netstack_rele(ns); 25409 } 25410 netstack_next_fini(&nh); 25411 return (ret); 25412 } 25413 25414 static int 25415 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25416 tcp_stack_t *tcps) 25417 { 25418 tcp_t *tcp; 25419 cl_tcp_info_t cl_tcpi; 25420 connf_t *connfp; 25421 conn_t *connp; 25422 int i; 25423 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25424 25425 ASSERT(callback != NULL); 25426 25427 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25428 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25429 connp = NULL; 25430 25431 while ((connp = 25432 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25433 25434 tcp = connp->conn_tcp; 25435 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25436 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25437 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25438 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25439 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25440 /* 25441 * The macros tcp_laddr and tcp_faddr give the IPv4 25442 * addresses. They are copied implicitly below as 25443 * mapped addresses. 25444 */ 25445 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25446 if (tcp->tcp_ipversion == IPV4_VERSION) { 25447 cl_tcpi.cl_tcpi_faddr = 25448 tcp->tcp_ipha->ipha_dst; 25449 } else { 25450 cl_tcpi.cl_tcpi_faddr_v6 = 25451 tcp->tcp_ip6h->ip6_dst; 25452 } 25453 25454 /* 25455 * If the callback returns non-zero 25456 * we terminate the traversal. 25457 */ 25458 if ((*callback)(&cl_tcpi, arg) != 0) { 25459 CONN_DEC_REF(tcp->tcp_connp); 25460 return (1); 25461 } 25462 } 25463 } 25464 25465 return (0); 25466 } 25467 25468 /* 25469 * Macros used for accessing the different types of sockaddr 25470 * structures inside a tcp_ioc_abort_conn_t. 25471 */ 25472 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25473 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25474 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25475 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25476 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25477 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25478 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25479 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25480 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25481 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25482 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25483 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25484 25485 /* 25486 * Return the correct error code to mimic the behavior 25487 * of a connection reset. 25488 */ 25489 #define TCP_AC_GET_ERRCODE(state, err) { \ 25490 switch ((state)) { \ 25491 case TCPS_SYN_SENT: \ 25492 case TCPS_SYN_RCVD: \ 25493 (err) = ECONNREFUSED; \ 25494 break; \ 25495 case TCPS_ESTABLISHED: \ 25496 case TCPS_FIN_WAIT_1: \ 25497 case TCPS_FIN_WAIT_2: \ 25498 case TCPS_CLOSE_WAIT: \ 25499 (err) = ECONNRESET; \ 25500 break; \ 25501 case TCPS_CLOSING: \ 25502 case TCPS_LAST_ACK: \ 25503 case TCPS_TIME_WAIT: \ 25504 (err) = 0; \ 25505 break; \ 25506 default: \ 25507 (err) = ENXIO; \ 25508 } \ 25509 } 25510 25511 /* 25512 * Check if a tcp structure matches the info in acp. 25513 */ 25514 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25515 (((acp)->ac_local.ss_family == AF_INET) ? \ 25516 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25517 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25518 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25519 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25520 (TCP_AC_V4LPORT((acp)) == 0 || \ 25521 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25522 (TCP_AC_V4RPORT((acp)) == 0 || \ 25523 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25524 (acp)->ac_start <= (tcp)->tcp_state && \ 25525 (acp)->ac_end >= (tcp)->tcp_state) : \ 25526 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25527 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25528 &(tcp)->tcp_ip_src_v6)) && \ 25529 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25530 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25531 &(tcp)->tcp_remote_v6)) && \ 25532 (TCP_AC_V6LPORT((acp)) == 0 || \ 25533 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25534 (TCP_AC_V6RPORT((acp)) == 0 || \ 25535 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25536 (acp)->ac_start <= (tcp)->tcp_state && \ 25537 (acp)->ac_end >= (tcp)->tcp_state)) 25538 25539 #define TCP_AC_MATCH(acp, tcp) \ 25540 (((acp)->ac_zoneid == ALL_ZONES || \ 25541 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25542 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25543 25544 /* 25545 * Build a message containing a tcp_ioc_abort_conn_t structure 25546 * which is filled in with information from acp and tp. 25547 */ 25548 static mblk_t * 25549 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25550 { 25551 mblk_t *mp; 25552 tcp_ioc_abort_conn_t *tacp; 25553 25554 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25555 if (mp == NULL) 25556 return (NULL); 25557 25558 mp->b_datap->db_type = M_CTL; 25559 25560 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25561 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25562 sizeof (uint32_t)); 25563 25564 tacp->ac_start = acp->ac_start; 25565 tacp->ac_end = acp->ac_end; 25566 tacp->ac_zoneid = acp->ac_zoneid; 25567 25568 if (acp->ac_local.ss_family == AF_INET) { 25569 tacp->ac_local.ss_family = AF_INET; 25570 tacp->ac_remote.ss_family = AF_INET; 25571 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25572 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25573 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25574 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25575 } else { 25576 tacp->ac_local.ss_family = AF_INET6; 25577 tacp->ac_remote.ss_family = AF_INET6; 25578 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25579 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25580 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25581 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25582 } 25583 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25584 return (mp); 25585 } 25586 25587 /* 25588 * Print a tcp_ioc_abort_conn_t structure. 25589 */ 25590 static void 25591 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25592 { 25593 char lbuf[128]; 25594 char rbuf[128]; 25595 sa_family_t af; 25596 in_port_t lport, rport; 25597 ushort_t logflags; 25598 25599 af = acp->ac_local.ss_family; 25600 25601 if (af == AF_INET) { 25602 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25603 lbuf, 128); 25604 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25605 rbuf, 128); 25606 lport = ntohs(TCP_AC_V4LPORT(acp)); 25607 rport = ntohs(TCP_AC_V4RPORT(acp)); 25608 } else { 25609 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25610 lbuf, 128); 25611 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25612 rbuf, 128); 25613 lport = ntohs(TCP_AC_V6LPORT(acp)); 25614 rport = ntohs(TCP_AC_V6RPORT(acp)); 25615 } 25616 25617 logflags = SL_TRACE | SL_NOTE; 25618 /* 25619 * Don't print this message to the console if the operation was done 25620 * to a non-global zone. 25621 */ 25622 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25623 logflags |= SL_CONSOLE; 25624 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25625 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25626 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25627 acp->ac_start, acp->ac_end); 25628 } 25629 25630 /* 25631 * Called inside tcp_rput when a message built using 25632 * tcp_ioctl_abort_build_msg is put into a queue. 25633 * Note that when we get here there is no wildcard in acp any more. 25634 */ 25635 static void 25636 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25637 { 25638 tcp_ioc_abort_conn_t *acp; 25639 25640 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25641 if (tcp->tcp_state <= acp->ac_end) { 25642 /* 25643 * If we get here, we are already on the correct 25644 * squeue. This ioctl follows the following path 25645 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25646 * ->tcp_ioctl_abort->squeue_fill (if on a 25647 * different squeue) 25648 */ 25649 int errcode; 25650 25651 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25652 (void) tcp_clean_death(tcp, errcode, 26); 25653 } 25654 freemsg(mp); 25655 } 25656 25657 /* 25658 * Abort all matching connections on a hash chain. 25659 */ 25660 static int 25661 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25662 boolean_t exact, tcp_stack_t *tcps) 25663 { 25664 int nmatch, err = 0; 25665 tcp_t *tcp; 25666 MBLKP mp, last, listhead = NULL; 25667 conn_t *tconnp; 25668 connf_t *connfp; 25669 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25670 25671 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25672 25673 startover: 25674 nmatch = 0; 25675 25676 mutex_enter(&connfp->connf_lock); 25677 for (tconnp = connfp->connf_head; tconnp != NULL; 25678 tconnp = tconnp->conn_next) { 25679 tcp = tconnp->conn_tcp; 25680 if (TCP_AC_MATCH(acp, tcp)) { 25681 CONN_INC_REF(tcp->tcp_connp); 25682 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25683 if (mp == NULL) { 25684 err = ENOMEM; 25685 CONN_DEC_REF(tcp->tcp_connp); 25686 break; 25687 } 25688 mp->b_prev = (mblk_t *)tcp; 25689 25690 if (listhead == NULL) { 25691 listhead = mp; 25692 last = mp; 25693 } else { 25694 last->b_next = mp; 25695 last = mp; 25696 } 25697 nmatch++; 25698 if (exact) 25699 break; 25700 } 25701 25702 /* Avoid holding lock for too long. */ 25703 if (nmatch >= 500) 25704 break; 25705 } 25706 mutex_exit(&connfp->connf_lock); 25707 25708 /* Pass mp into the correct tcp */ 25709 while ((mp = listhead) != NULL) { 25710 listhead = listhead->b_next; 25711 tcp = (tcp_t *)mp->b_prev; 25712 mp->b_next = mp->b_prev = NULL; 25713 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 25714 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 25715 } 25716 25717 *count += nmatch; 25718 if (nmatch >= 500 && err == 0) 25719 goto startover; 25720 return (err); 25721 } 25722 25723 /* 25724 * Abort all connections that matches the attributes specified in acp. 25725 */ 25726 static int 25727 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25728 { 25729 sa_family_t af; 25730 uint32_t ports; 25731 uint16_t *pports; 25732 int err = 0, count = 0; 25733 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25734 int index = -1; 25735 ushort_t logflags; 25736 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25737 25738 af = acp->ac_local.ss_family; 25739 25740 if (af == AF_INET) { 25741 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25742 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25743 pports = (uint16_t *)&ports; 25744 pports[1] = TCP_AC_V4LPORT(acp); 25745 pports[0] = TCP_AC_V4RPORT(acp); 25746 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25747 } 25748 } else { 25749 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25750 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25751 pports = (uint16_t *)&ports; 25752 pports[1] = TCP_AC_V6LPORT(acp); 25753 pports[0] = TCP_AC_V6RPORT(acp); 25754 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25755 } 25756 } 25757 25758 /* 25759 * For cases where remote addr, local port, and remote port are non- 25760 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25761 */ 25762 if (index != -1) { 25763 err = tcp_ioctl_abort_bucket(acp, index, 25764 &count, exact, tcps); 25765 } else { 25766 /* 25767 * loop through all entries for wildcard case 25768 */ 25769 for (index = 0; 25770 index < ipst->ips_ipcl_conn_fanout_size; 25771 index++) { 25772 err = tcp_ioctl_abort_bucket(acp, index, 25773 &count, exact, tcps); 25774 if (err != 0) 25775 break; 25776 } 25777 } 25778 25779 logflags = SL_TRACE | SL_NOTE; 25780 /* 25781 * Don't print this message to the console if the operation was done 25782 * to a non-global zone. 25783 */ 25784 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25785 logflags |= SL_CONSOLE; 25786 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25787 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25788 if (err == 0 && count == 0) 25789 err = ENOENT; 25790 return (err); 25791 } 25792 25793 /* 25794 * Process the TCP_IOC_ABORT_CONN ioctl request. 25795 */ 25796 static void 25797 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25798 { 25799 int err; 25800 IOCP iocp; 25801 MBLKP mp1; 25802 sa_family_t laf, raf; 25803 tcp_ioc_abort_conn_t *acp; 25804 zone_t *zptr; 25805 conn_t *connp = Q_TO_CONN(q); 25806 zoneid_t zoneid = connp->conn_zoneid; 25807 tcp_t *tcp = connp->conn_tcp; 25808 tcp_stack_t *tcps = tcp->tcp_tcps; 25809 25810 iocp = (IOCP)mp->b_rptr; 25811 25812 if ((mp1 = mp->b_cont) == NULL || 25813 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25814 err = EINVAL; 25815 goto out; 25816 } 25817 25818 /* check permissions */ 25819 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25820 err = EPERM; 25821 goto out; 25822 } 25823 25824 if (mp1->b_cont != NULL) { 25825 freemsg(mp1->b_cont); 25826 mp1->b_cont = NULL; 25827 } 25828 25829 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25830 laf = acp->ac_local.ss_family; 25831 raf = acp->ac_remote.ss_family; 25832 25833 /* check that a zone with the supplied zoneid exists */ 25834 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25835 zptr = zone_find_by_id(zoneid); 25836 if (zptr != NULL) { 25837 zone_rele(zptr); 25838 } else { 25839 err = EINVAL; 25840 goto out; 25841 } 25842 } 25843 25844 /* 25845 * For exclusive stacks we set the zoneid to zero 25846 * to make TCP operate as if in the global zone. 25847 */ 25848 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25849 acp->ac_zoneid = GLOBAL_ZONEID; 25850 25851 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25852 acp->ac_start > acp->ac_end || laf != raf || 25853 (laf != AF_INET && laf != AF_INET6)) { 25854 err = EINVAL; 25855 goto out; 25856 } 25857 25858 tcp_ioctl_abort_dump(acp); 25859 err = tcp_ioctl_abort(acp, tcps); 25860 25861 out: 25862 if (mp1 != NULL) { 25863 freemsg(mp1); 25864 mp->b_cont = NULL; 25865 } 25866 25867 if (err != 0) 25868 miocnak(q, mp, 0, err); 25869 else 25870 miocack(q, mp, 0, 0); 25871 } 25872 25873 /* 25874 * tcp_time_wait_processing() handles processing of incoming packets when 25875 * the tcp is in the TIME_WAIT state. 25876 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25877 * on the time wait list. 25878 */ 25879 void 25880 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25881 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25882 { 25883 int32_t bytes_acked; 25884 int32_t gap; 25885 int32_t rgap; 25886 tcp_opt_t tcpopt; 25887 uint_t flags; 25888 uint32_t new_swnd = 0; 25889 conn_t *connp; 25890 tcp_stack_t *tcps = tcp->tcp_tcps; 25891 25892 BUMP_LOCAL(tcp->tcp_ibsegs); 25893 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 25894 25895 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25896 new_swnd = BE16_TO_U16(tcph->th_win) << 25897 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25898 if (tcp->tcp_snd_ts_ok) { 25899 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25900 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25901 tcp->tcp_rnxt, TH_ACK); 25902 goto done; 25903 } 25904 } 25905 gap = seg_seq - tcp->tcp_rnxt; 25906 rgap = tcp->tcp_rwnd - (gap + seg_len); 25907 if (gap < 0) { 25908 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25909 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25910 (seg_len > -gap ? -gap : seg_len)); 25911 seg_len += gap; 25912 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25913 if (flags & TH_RST) { 25914 goto done; 25915 } 25916 if ((flags & TH_FIN) && seg_len == -1) { 25917 /* 25918 * When TCP receives a duplicate FIN in 25919 * TIME_WAIT state, restart the 2 MSL timer. 25920 * See page 73 in RFC 793. Make sure this TCP 25921 * is already on the TIME_WAIT list. If not, 25922 * just restart the timer. 25923 */ 25924 if (TCP_IS_DETACHED(tcp)) { 25925 if (tcp_time_wait_remove(tcp, NULL) == 25926 B_TRUE) { 25927 tcp_time_wait_append(tcp); 25928 TCP_DBGSTAT(tcps, 25929 tcp_rput_time_wait); 25930 } 25931 } else { 25932 ASSERT(tcp != NULL); 25933 TCP_TIMER_RESTART(tcp, 25934 tcps->tcps_time_wait_interval); 25935 } 25936 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25937 tcp->tcp_rnxt, TH_ACK); 25938 goto done; 25939 } 25940 flags |= TH_ACK_NEEDED; 25941 seg_len = 0; 25942 goto process_ack; 25943 } 25944 25945 /* Fix seg_seq, and chew the gap off the front. */ 25946 seg_seq = tcp->tcp_rnxt; 25947 } 25948 25949 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25950 /* 25951 * Make sure that when we accept the connection, pick 25952 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25953 * old connection. 25954 * 25955 * The next ISS generated is equal to tcp_iss_incr_extra 25956 * + ISS_INCR/2 + other components depending on the 25957 * value of tcp_strong_iss. We pre-calculate the new 25958 * ISS here and compare with tcp_snxt to determine if 25959 * we need to make adjustment to tcp_iss_incr_extra. 25960 * 25961 * The above calculation is ugly and is a 25962 * waste of CPU cycles... 25963 */ 25964 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25965 int32_t adj; 25966 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25967 25968 switch (tcps->tcps_strong_iss) { 25969 case 2: { 25970 /* Add time and MD5 components. */ 25971 uint32_t answer[4]; 25972 struct { 25973 uint32_t ports; 25974 in6_addr_t src; 25975 in6_addr_t dst; 25976 } arg; 25977 MD5_CTX context; 25978 25979 mutex_enter(&tcps->tcps_iss_key_lock); 25980 context = tcps->tcps_iss_key; 25981 mutex_exit(&tcps->tcps_iss_key_lock); 25982 arg.ports = tcp->tcp_ports; 25983 /* We use MAPPED addresses in tcp_iss_init */ 25984 arg.src = tcp->tcp_ip_src_v6; 25985 if (tcp->tcp_ipversion == IPV4_VERSION) { 25986 IN6_IPADDR_TO_V4MAPPED( 25987 tcp->tcp_ipha->ipha_dst, 25988 &arg.dst); 25989 } else { 25990 arg.dst = 25991 tcp->tcp_ip6h->ip6_dst; 25992 } 25993 MD5Update(&context, (uchar_t *)&arg, 25994 sizeof (arg)); 25995 MD5Final((uchar_t *)answer, &context); 25996 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25997 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25998 break; 25999 } 26000 case 1: 26001 /* Add time component and min random (i.e. 1). */ 26002 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 26003 break; 26004 default: 26005 /* Add only time component. */ 26006 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 26007 break; 26008 } 26009 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 26010 /* 26011 * New ISS not guaranteed to be ISS_INCR/2 26012 * ahead of the current tcp_snxt, so add the 26013 * difference to tcp_iss_incr_extra. 26014 */ 26015 tcps->tcps_iss_incr_extra += adj; 26016 } 26017 /* 26018 * If tcp_clean_death() can not perform the task now, 26019 * drop the SYN packet and let the other side re-xmit. 26020 * Otherwise pass the SYN packet back in, since the 26021 * old tcp state has been cleaned up or freed. 26022 */ 26023 if (tcp_clean_death(tcp, 0, 27) == -1) 26024 goto done; 26025 /* 26026 * We will come back to tcp_rput_data 26027 * on the global queue. Packets destined 26028 * for the global queue will be checked 26029 * with global policy. But the policy for 26030 * this packet has already been checked as 26031 * this was destined for the detached 26032 * connection. We need to bypass policy 26033 * check this time by attaching a dummy 26034 * ipsec_in with ipsec_in_dont_check set. 26035 */ 26036 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 26037 if (connp != NULL) { 26038 TCP_STAT(tcps, tcp_time_wait_syn_success); 26039 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 26040 return; 26041 } 26042 goto done; 26043 } 26044 26045 /* 26046 * rgap is the amount of stuff received out of window. A negative 26047 * value is the amount out of window. 26048 */ 26049 if (rgap < 0) { 26050 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 26051 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 26052 /* Fix seg_len and make sure there is something left. */ 26053 seg_len += rgap; 26054 if (seg_len <= 0) { 26055 if (flags & TH_RST) { 26056 goto done; 26057 } 26058 flags |= TH_ACK_NEEDED; 26059 seg_len = 0; 26060 goto process_ack; 26061 } 26062 } 26063 /* 26064 * Check whether we can update tcp_ts_recent. This test is 26065 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 26066 * Extensions for High Performance: An Update", Internet Draft. 26067 */ 26068 if (tcp->tcp_snd_ts_ok && 26069 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 26070 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 26071 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 26072 tcp->tcp_last_rcv_lbolt = lbolt64; 26073 } 26074 26075 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 26076 /* Always ack out of order packets */ 26077 flags |= TH_ACK_NEEDED; 26078 seg_len = 0; 26079 } else if (seg_len > 0) { 26080 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 26081 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 26082 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 26083 } 26084 if (flags & TH_RST) { 26085 (void) tcp_clean_death(tcp, 0, 28); 26086 goto done; 26087 } 26088 if (flags & TH_SYN) { 26089 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 26090 TH_RST|TH_ACK); 26091 /* 26092 * Do not delete the TCP structure if it is in 26093 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 26094 */ 26095 goto done; 26096 } 26097 process_ack: 26098 if (flags & TH_ACK) { 26099 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 26100 if (bytes_acked <= 0) { 26101 if (bytes_acked == 0 && seg_len == 0 && 26102 new_swnd == tcp->tcp_swnd) 26103 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 26104 } else { 26105 /* Acks something not sent */ 26106 flags |= TH_ACK_NEEDED; 26107 } 26108 } 26109 if (flags & TH_ACK_NEEDED) { 26110 /* 26111 * Time to send an ack for some reason. 26112 */ 26113 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 26114 tcp->tcp_rnxt, TH_ACK); 26115 } 26116 done: 26117 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26118 DB_CKSUMSTART(mp) = 0; 26119 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 26120 TCP_STAT(tcps, tcp_time_wait_syn_fail); 26121 } 26122 freemsg(mp); 26123 } 26124 26125 /* 26126 * Allocate a T_SVR4_OPTMGMT_REQ. 26127 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 26128 * that tcp_rput_other can drop the acks. 26129 */ 26130 static mblk_t * 26131 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 26132 { 26133 mblk_t *mp; 26134 struct T_optmgmt_req *tor; 26135 struct opthdr *oh; 26136 uint_t size; 26137 char *optptr; 26138 26139 size = sizeof (*tor) + sizeof (*oh) + optlen; 26140 mp = allocb(size, BPRI_MED); 26141 if (mp == NULL) 26142 return (NULL); 26143 26144 mp->b_wptr += size; 26145 mp->b_datap->db_type = M_PROTO; 26146 tor = (struct T_optmgmt_req *)mp->b_rptr; 26147 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 26148 tor->MGMT_flags = T_NEGOTIATE; 26149 tor->OPT_length = sizeof (*oh) + optlen; 26150 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 26151 26152 oh = (struct opthdr *)&tor[1]; 26153 oh->level = level; 26154 oh->name = cmd; 26155 oh->len = optlen; 26156 if (optlen != 0) { 26157 optptr = (char *)&oh[1]; 26158 bcopy(opt, optptr, optlen); 26159 } 26160 return (mp); 26161 } 26162 26163 /* 26164 * TCP Timers Implementation. 26165 */ 26166 timeout_id_t 26167 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 26168 { 26169 mblk_t *mp; 26170 tcp_timer_t *tcpt; 26171 tcp_t *tcp = connp->conn_tcp; 26172 tcp_stack_t *tcps = tcp->tcp_tcps; 26173 26174 ASSERT(connp->conn_sqp != NULL); 26175 26176 TCP_DBGSTAT(tcps, tcp_timeout_calls); 26177 26178 if (tcp->tcp_timercache == NULL) { 26179 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 26180 } else { 26181 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 26182 mp = tcp->tcp_timercache; 26183 tcp->tcp_timercache = mp->b_next; 26184 mp->b_next = NULL; 26185 ASSERT(mp->b_wptr == NULL); 26186 } 26187 26188 CONN_INC_REF(connp); 26189 tcpt = (tcp_timer_t *)mp->b_rptr; 26190 tcpt->connp = connp; 26191 tcpt->tcpt_proc = f; 26192 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 26193 return ((timeout_id_t)mp); 26194 } 26195 26196 static void 26197 tcp_timer_callback(void *arg) 26198 { 26199 mblk_t *mp = (mblk_t *)arg; 26200 tcp_timer_t *tcpt; 26201 conn_t *connp; 26202 26203 tcpt = (tcp_timer_t *)mp->b_rptr; 26204 connp = tcpt->connp; 26205 squeue_fill(connp->conn_sqp, mp, 26206 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 26207 } 26208 26209 static void 26210 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 26211 { 26212 tcp_timer_t *tcpt; 26213 conn_t *connp = (conn_t *)arg; 26214 tcp_t *tcp = connp->conn_tcp; 26215 26216 tcpt = (tcp_timer_t *)mp->b_rptr; 26217 ASSERT(connp == tcpt->connp); 26218 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 26219 26220 /* 26221 * If the TCP has reached the closed state, don't proceed any 26222 * further. This TCP logically does not exist on the system. 26223 * tcpt_proc could for example access queues, that have already 26224 * been qprocoff'ed off. Also see comments at the start of tcp_input 26225 */ 26226 if (tcp->tcp_state != TCPS_CLOSED) { 26227 (*tcpt->tcpt_proc)(connp); 26228 } else { 26229 tcp->tcp_timer_tid = 0; 26230 } 26231 tcp_timer_free(connp->conn_tcp, mp); 26232 } 26233 26234 /* 26235 * There is potential race with untimeout and the handler firing at the same 26236 * time. The mblock may be freed by the handler while we are trying to use 26237 * it. But since both should execute on the same squeue, this race should not 26238 * occur. 26239 */ 26240 clock_t 26241 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 26242 { 26243 mblk_t *mp = (mblk_t *)id; 26244 tcp_timer_t *tcpt; 26245 clock_t delta; 26246 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26247 26248 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 26249 26250 if (mp == NULL) 26251 return (-1); 26252 26253 tcpt = (tcp_timer_t *)mp->b_rptr; 26254 ASSERT(tcpt->connp == connp); 26255 26256 delta = untimeout(tcpt->tcpt_tid); 26257 26258 if (delta >= 0) { 26259 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 26260 tcp_timer_free(connp->conn_tcp, mp); 26261 CONN_DEC_REF(connp); 26262 } 26263 26264 return (delta); 26265 } 26266 26267 /* 26268 * Allocate space for the timer event. The allocation looks like mblk, but it is 26269 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26270 * 26271 * Dealing with failures: If we can't allocate from the timer cache we try 26272 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26273 * points to b_rptr. 26274 * If we can't allocate anything using allocb_tryhard(), we perform a last 26275 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26276 * save the actual allocation size in b_datap. 26277 */ 26278 mblk_t * 26279 tcp_timermp_alloc(int kmflags) 26280 { 26281 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26282 kmflags & ~KM_PANIC); 26283 26284 if (mp != NULL) { 26285 mp->b_next = mp->b_prev = NULL; 26286 mp->b_rptr = (uchar_t *)(&mp[1]); 26287 mp->b_wptr = NULL; 26288 mp->b_datap = NULL; 26289 mp->b_queue = NULL; 26290 mp->b_cont = NULL; 26291 } else if (kmflags & KM_PANIC) { 26292 /* 26293 * Failed to allocate memory for the timer. Try allocating from 26294 * dblock caches. 26295 */ 26296 /* ipclassifier calls this from a constructor - hence no tcps */ 26297 TCP_G_STAT(tcp_timermp_allocfail); 26298 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26299 if (mp == NULL) { 26300 size_t size = 0; 26301 /* 26302 * Memory is really low. Try tryhard allocation. 26303 * 26304 * ipclassifier calls this from a constructor - 26305 * hence no tcps 26306 */ 26307 TCP_G_STAT(tcp_timermp_allocdblfail); 26308 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26309 sizeof (tcp_timer_t), &size, kmflags); 26310 mp->b_rptr = (uchar_t *)(&mp[1]); 26311 mp->b_next = mp->b_prev = NULL; 26312 mp->b_wptr = (uchar_t *)-1; 26313 mp->b_datap = (dblk_t *)size; 26314 mp->b_queue = NULL; 26315 mp->b_cont = NULL; 26316 } 26317 ASSERT(mp->b_wptr != NULL); 26318 } 26319 /* ipclassifier calls this from a constructor - hence no tcps */ 26320 TCP_G_DBGSTAT(tcp_timermp_alloced); 26321 26322 return (mp); 26323 } 26324 26325 /* 26326 * Free per-tcp timer cache. 26327 * It can only contain entries from tcp_timercache. 26328 */ 26329 void 26330 tcp_timermp_free(tcp_t *tcp) 26331 { 26332 mblk_t *mp; 26333 26334 while ((mp = tcp->tcp_timercache) != NULL) { 26335 ASSERT(mp->b_wptr == NULL); 26336 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26337 kmem_cache_free(tcp_timercache, mp); 26338 } 26339 } 26340 26341 /* 26342 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26343 * events there already (currently at most two events are cached). 26344 * If the event is not allocated from the timer cache, free it right away. 26345 */ 26346 static void 26347 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26348 { 26349 mblk_t *mp1 = tcp->tcp_timercache; 26350 tcp_stack_t *tcps = tcp->tcp_tcps; 26351 26352 if (mp->b_wptr != NULL) { 26353 /* 26354 * This allocation is not from a timer cache, free it right 26355 * away. 26356 */ 26357 if (mp->b_wptr != (uchar_t *)-1) 26358 freeb(mp); 26359 else 26360 kmem_free(mp, (size_t)mp->b_datap); 26361 } else if (mp1 == NULL || mp1->b_next == NULL) { 26362 /* Cache this timer block for future allocations */ 26363 mp->b_rptr = (uchar_t *)(&mp[1]); 26364 mp->b_next = mp1; 26365 tcp->tcp_timercache = mp; 26366 } else { 26367 kmem_cache_free(tcp_timercache, mp); 26368 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26369 } 26370 } 26371 26372 /* 26373 * End of TCP Timers implementation. 26374 */ 26375 26376 /* 26377 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26378 * on the specified backing STREAMS q. Note, the caller may make the 26379 * decision to call based on the tcp_t.tcp_flow_stopped value which 26380 * when check outside the q's lock is only an advisory check ... 26381 */ 26382 26383 void 26384 tcp_setqfull(tcp_t *tcp) 26385 { 26386 queue_t *q = tcp->tcp_wq; 26387 tcp_stack_t *tcps = tcp->tcp_tcps; 26388 26389 if (!(q->q_flag & QFULL)) { 26390 mutex_enter(QLOCK(q)); 26391 if (!(q->q_flag & QFULL)) { 26392 /* still need to set QFULL */ 26393 q->q_flag |= QFULL; 26394 tcp->tcp_flow_stopped = B_TRUE; 26395 mutex_exit(QLOCK(q)); 26396 TCP_STAT(tcps, tcp_flwctl_on); 26397 } else { 26398 mutex_exit(QLOCK(q)); 26399 } 26400 } 26401 } 26402 26403 void 26404 tcp_clrqfull(tcp_t *tcp) 26405 { 26406 queue_t *q = tcp->tcp_wq; 26407 26408 if (q->q_flag & QFULL) { 26409 mutex_enter(QLOCK(q)); 26410 if (q->q_flag & QFULL) { 26411 q->q_flag &= ~QFULL; 26412 tcp->tcp_flow_stopped = B_FALSE; 26413 mutex_exit(QLOCK(q)); 26414 if (q->q_flag & QWANTW) 26415 qbackenable(q, 0); 26416 } else { 26417 mutex_exit(QLOCK(q)); 26418 } 26419 } 26420 } 26421 26422 26423 /* 26424 * kstats related to squeues i.e. not per IP instance 26425 */ 26426 static void * 26427 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26428 { 26429 kstat_t *ksp; 26430 26431 tcp_g_stat_t template = { 26432 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26433 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26434 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26435 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26436 }; 26437 26438 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26439 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26440 KSTAT_FLAG_VIRTUAL); 26441 26442 if (ksp == NULL) 26443 return (NULL); 26444 26445 bcopy(&template, tcp_g_statp, sizeof (template)); 26446 ksp->ks_data = (void *)tcp_g_statp; 26447 26448 kstat_install(ksp); 26449 return (ksp); 26450 } 26451 26452 static void 26453 tcp_g_kstat_fini(kstat_t *ksp) 26454 { 26455 if (ksp != NULL) { 26456 kstat_delete(ksp); 26457 } 26458 } 26459 26460 26461 static void * 26462 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26463 { 26464 kstat_t *ksp; 26465 26466 tcp_stat_t template = { 26467 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26468 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26469 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26470 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26471 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26472 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26473 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26474 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26475 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26476 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26477 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26478 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26479 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26480 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26481 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26482 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26483 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26484 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26485 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26486 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26487 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26488 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26489 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26490 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26491 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26492 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26493 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26494 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26495 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26496 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26497 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26498 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26499 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26500 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26501 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26502 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26503 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26504 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26505 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26506 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26507 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26508 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26509 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26510 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26511 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26512 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26513 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26514 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26515 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26516 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26517 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26518 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26519 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26520 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26521 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26522 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26523 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26524 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26525 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26526 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26527 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26528 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26529 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26530 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26531 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26532 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26533 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26534 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26535 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26536 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26537 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26538 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26539 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26540 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26541 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26542 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26543 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26544 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26545 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26546 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26547 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26548 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26549 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26550 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26551 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26552 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26553 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26554 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26555 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26556 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26557 }; 26558 26559 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26560 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26561 KSTAT_FLAG_VIRTUAL, stackid); 26562 26563 if (ksp == NULL) 26564 return (NULL); 26565 26566 bcopy(&template, tcps_statisticsp, sizeof (template)); 26567 ksp->ks_data = (void *)tcps_statisticsp; 26568 ksp->ks_private = (void *)(uintptr_t)stackid; 26569 26570 kstat_install(ksp); 26571 return (ksp); 26572 } 26573 26574 static void 26575 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26576 { 26577 if (ksp != NULL) { 26578 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26579 kstat_delete_netstack(ksp, stackid); 26580 } 26581 } 26582 26583 /* 26584 * TCP Kstats implementation 26585 */ 26586 static void * 26587 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26588 { 26589 kstat_t *ksp; 26590 26591 tcp_named_kstat_t template = { 26592 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26593 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26594 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26595 { "maxConn", KSTAT_DATA_INT32, 0 }, 26596 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26597 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26598 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26599 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26600 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26601 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26602 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26603 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26604 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26605 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26606 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26607 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26608 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26609 { "outAck", KSTAT_DATA_UINT32, 0 }, 26610 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26611 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26612 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26613 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26614 { "outControl", KSTAT_DATA_UINT32, 0 }, 26615 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26616 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26617 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26618 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26619 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26620 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26621 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26622 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26623 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26624 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26625 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26626 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26627 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26628 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26629 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26630 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26631 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26632 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26633 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26634 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26635 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26636 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26637 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26638 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26639 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26640 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26641 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26642 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26643 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26644 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26645 }; 26646 26647 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26648 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26649 26650 if (ksp == NULL) 26651 return (NULL); 26652 26653 template.rtoAlgorithm.value.ui32 = 4; 26654 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26655 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26656 template.maxConn.value.i32 = -1; 26657 26658 bcopy(&template, ksp->ks_data, sizeof (template)); 26659 ksp->ks_update = tcp_kstat_update; 26660 ksp->ks_private = (void *)(uintptr_t)stackid; 26661 26662 kstat_install(ksp); 26663 return (ksp); 26664 } 26665 26666 static void 26667 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26668 { 26669 if (ksp != NULL) { 26670 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26671 kstat_delete_netstack(ksp, stackid); 26672 } 26673 } 26674 26675 static int 26676 tcp_kstat_update(kstat_t *kp, int rw) 26677 { 26678 tcp_named_kstat_t *tcpkp; 26679 tcp_t *tcp; 26680 connf_t *connfp; 26681 conn_t *connp; 26682 int i; 26683 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26684 netstack_t *ns; 26685 tcp_stack_t *tcps; 26686 ip_stack_t *ipst; 26687 26688 if ((kp == NULL) || (kp->ks_data == NULL)) 26689 return (EIO); 26690 26691 if (rw == KSTAT_WRITE) 26692 return (EACCES); 26693 26694 ns = netstack_find_by_stackid(stackid); 26695 if (ns == NULL) 26696 return (-1); 26697 tcps = ns->netstack_tcp; 26698 if (tcps == NULL) { 26699 netstack_rele(ns); 26700 return (-1); 26701 } 26702 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26703 26704 tcpkp->currEstab.value.ui32 = 0; 26705 26706 ipst = ns->netstack_ip; 26707 26708 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26709 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26710 connp = NULL; 26711 while ((connp = 26712 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26713 tcp = connp->conn_tcp; 26714 switch (tcp_snmp_state(tcp)) { 26715 case MIB2_TCP_established: 26716 case MIB2_TCP_closeWait: 26717 tcpkp->currEstab.value.ui32++; 26718 break; 26719 } 26720 } 26721 } 26722 26723 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26724 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26725 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26726 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26727 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26728 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26729 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26730 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26731 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26732 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26733 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26734 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26735 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26736 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26737 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26738 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26739 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26740 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26741 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26742 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26743 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26744 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26745 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26746 tcpkp->inDataInorderSegs.value.ui32 = 26747 tcps->tcps_mib.tcpInDataInorderSegs; 26748 tcpkp->inDataInorderBytes.value.ui32 = 26749 tcps->tcps_mib.tcpInDataInorderBytes; 26750 tcpkp->inDataUnorderSegs.value.ui32 = 26751 tcps->tcps_mib.tcpInDataUnorderSegs; 26752 tcpkp->inDataUnorderBytes.value.ui32 = 26753 tcps->tcps_mib.tcpInDataUnorderBytes; 26754 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26755 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26756 tcpkp->inDataPartDupSegs.value.ui32 = 26757 tcps->tcps_mib.tcpInDataPartDupSegs; 26758 tcpkp->inDataPartDupBytes.value.ui32 = 26759 tcps->tcps_mib.tcpInDataPartDupBytes; 26760 tcpkp->inDataPastWinSegs.value.ui32 = 26761 tcps->tcps_mib.tcpInDataPastWinSegs; 26762 tcpkp->inDataPastWinBytes.value.ui32 = 26763 tcps->tcps_mib.tcpInDataPastWinBytes; 26764 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26765 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26766 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26767 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26768 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26769 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26770 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26771 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26772 tcpkp->timKeepaliveProbe.value.ui32 = 26773 tcps->tcps_mib.tcpTimKeepaliveProbe; 26774 tcpkp->timKeepaliveDrop.value.ui32 = 26775 tcps->tcps_mib.tcpTimKeepaliveDrop; 26776 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26777 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26778 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26779 tcpkp->outSackRetransSegs.value.ui32 = 26780 tcps->tcps_mib.tcpOutSackRetransSegs; 26781 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26782 26783 netstack_rele(ns); 26784 return (0); 26785 } 26786 26787 void 26788 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26789 { 26790 uint16_t hdr_len; 26791 ipha_t *ipha; 26792 uint8_t *nexthdrp; 26793 tcph_t *tcph; 26794 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26795 26796 /* Already has an eager */ 26797 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26798 TCP_STAT(tcps, tcp_reinput_syn); 26799 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 26800 connp, SQTAG_TCP_REINPUT_EAGER); 26801 return; 26802 } 26803 26804 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26805 case IPV4_VERSION: 26806 ipha = (ipha_t *)mp->b_rptr; 26807 hdr_len = IPH_HDR_LENGTH(ipha); 26808 break; 26809 case IPV6_VERSION: 26810 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26811 &hdr_len, &nexthdrp)) { 26812 CONN_DEC_REF(connp); 26813 freemsg(mp); 26814 return; 26815 } 26816 break; 26817 } 26818 26819 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26820 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26821 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26822 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26823 } 26824 26825 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 26826 SQTAG_TCP_REINPUT); 26827 } 26828 26829 static squeue_func_t 26830 tcp_squeue_switch(int val) 26831 { 26832 squeue_func_t rval = squeue_fill; 26833 26834 switch (val) { 26835 case 1: 26836 rval = squeue_enter_nodrain; 26837 break; 26838 case 2: 26839 rval = squeue_enter; 26840 break; 26841 default: 26842 break; 26843 } 26844 return (rval); 26845 } 26846 26847 /* 26848 * This is called once for each squeue - globally for all stack 26849 * instances. 26850 */ 26851 static void 26852 tcp_squeue_add(squeue_t *sqp) 26853 { 26854 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26855 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26856 26857 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26858 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 26859 sqp, TCP_TIME_WAIT_DELAY); 26860 if (tcp_free_list_max_cnt == 0) { 26861 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26862 max_ncpus : boot_max_ncpus); 26863 26864 /* 26865 * Limit number of entries to 1% of availble memory / tcp_ncpus 26866 */ 26867 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26868 (tcp_ncpus * sizeof (tcp_t) * 100); 26869 } 26870 tcp_time_wait->tcp_free_list_cnt = 0; 26871 } 26872