1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 const char tcp_version[] = "%Z%%M% %I% %E% SMI"; 30 31 32 #include <sys/types.h> 33 #include <sys/stream.h> 34 #include <sys/strsun.h> 35 #include <sys/strsubr.h> 36 #include <sys/stropts.h> 37 #include <sys/strlog.h> 38 #include <sys/strsun.h> 39 #define _SUN_TPI_VERSION 2 40 #include <sys/tihdr.h> 41 #include <sys/timod.h> 42 #include <sys/ddi.h> 43 #include <sys/sunddi.h> 44 #include <sys/suntpi.h> 45 #include <sys/xti_inet.h> 46 #include <sys/cmn_err.h> 47 #include <sys/debug.h> 48 #include <sys/sdt.h> 49 #include <sys/vtrace.h> 50 #include <sys/kmem.h> 51 #include <sys/ethernet.h> 52 #include <sys/cpuvar.h> 53 #include <sys/dlpi.h> 54 #include <sys/multidata.h> 55 #include <sys/multidata_impl.h> 56 #include <sys/pattr.h> 57 #include <sys/policy.h> 58 #include <sys/priv.h> 59 #include <sys/zone.h> 60 #include <sys/sunldi.h> 61 62 #include <sys/errno.h> 63 #include <sys/signal.h> 64 #include <sys/socket.h> 65 #include <sys/sockio.h> 66 #include <sys/isa_defs.h> 67 #include <sys/md5.h> 68 #include <sys/random.h> 69 #include <netinet/in.h> 70 #include <netinet/tcp.h> 71 #include <netinet/ip6.h> 72 #include <netinet/icmp6.h> 73 #include <net/if.h> 74 #include <net/route.h> 75 #include <inet/ipsec_impl.h> 76 77 #include <inet/common.h> 78 #include <inet/ip.h> 79 #include <inet/ip_impl.h> 80 #include <inet/ip6.h> 81 #include <inet/ip_ndp.h> 82 #include <inet/mi.h> 83 #include <inet/mib2.h> 84 #include <inet/nd.h> 85 #include <inet/optcom.h> 86 #include <inet/snmpcom.h> 87 #include <inet/kstatcom.h> 88 #include <inet/tcp.h> 89 #include <inet/tcp_impl.h> 90 #include <net/pfkeyv2.h> 91 #include <inet/ipsec_info.h> 92 #include <inet/ipdrop.h> 93 #include <inet/tcp_trace.h> 94 95 #include <inet/ipclassifier.h> 96 #include <inet/ip_ire.h> 97 #include <inet/ip_ftable.h> 98 #include <inet/ip_if.h> 99 #include <inet/ipp_common.h> 100 #include <inet/ip_netinfo.h> 101 #include <sys/squeue.h> 102 #include <inet/kssl/ksslapi.h> 103 #include <sys/tsol/label.h> 104 #include <sys/tsol/tnet.h> 105 #include <sys/sdt.h> 106 #include <rpc/pmap_prot.h> 107 108 /* 109 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 110 * 111 * (Read the detailed design doc in PSARC case directory) 112 * 113 * The entire tcp state is contained in tcp_t and conn_t structure 114 * which are allocated in tandem using ipcl_conn_create() and passing 115 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 116 * the references on the tcp_t. The tcp_t structure is never compressed 117 * and packets always land on the correct TCP perimeter from the time 118 * eager is created till the time tcp_t dies (as such the old mentat 119 * TCP global queue is not used for detached state and no IPSEC checking 120 * is required). The global queue is still allocated to send out resets 121 * for connection which have no listeners and IP directly calls 122 * tcp_xmit_listeners_reset() which does any policy check. 123 * 124 * Protection and Synchronisation mechanism: 125 * 126 * The tcp data structure does not use any kind of lock for protecting 127 * its state but instead uses 'squeues' for mutual exclusion from various 128 * read and write side threads. To access a tcp member, the thread should 129 * always be behind squeue (via squeue_enter, squeue_enter_nodrain, or 130 * squeue_fill). Since the squeues allow a direct function call, caller 131 * can pass any tcp function having prototype of edesc_t as argument 132 * (different from traditional STREAMs model where packets come in only 133 * designated entry points). The list of functions that can be directly 134 * called via squeue are listed before the usual function prototype. 135 * 136 * Referencing: 137 * 138 * TCP is MT-Hot and we use a reference based scheme to make sure that the 139 * tcp structure doesn't disappear when its needed. When the application 140 * creates an outgoing connection or accepts an incoming connection, we 141 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 142 * The IP reference is just a symbolic reference since ip_tcpclose() 143 * looks at tcp structure after tcp_close_output() returns which could 144 * have dropped the last TCP reference. So as long as the connection is 145 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 146 * conn_t. The classifier puts its own reference when the connection is 147 * inserted in listen or connected hash. Anytime a thread needs to enter 148 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 149 * on write side or by doing a classify on read side and then puts a 150 * reference on the conn before doing squeue_enter/tryenter/fill. For 151 * read side, the classifier itself puts the reference under fanout lock 152 * to make sure that tcp can't disappear before it gets processed. The 153 * squeue will drop this reference automatically so the called function 154 * doesn't have to do a DEC_REF. 155 * 156 * Opening a new connection: 157 * 158 * The outgoing connection open is pretty simple. tcp_open() does the 159 * work in creating the conn/tcp structure and initializing it. The 160 * squeue assignment is done based on the CPU the application 161 * is running on. So for outbound connections, processing is always done 162 * on application CPU which might be different from the incoming CPU 163 * being interrupted by the NIC. An optimal way would be to figure out 164 * the NIC <-> CPU binding at listen time, and assign the outgoing 165 * connection to the squeue attached to the CPU that will be interrupted 166 * for incoming packets (we know the NIC based on the bind IP address). 167 * This might seem like a problem if more data is going out but the 168 * fact is that in most cases the transmit is ACK driven transmit where 169 * the outgoing data normally sits on TCP's xmit queue waiting to be 170 * transmitted. 171 * 172 * Accepting a connection: 173 * 174 * This is a more interesting case because of various races involved in 175 * establishing a eager in its own perimeter. Read the meta comment on 176 * top of tcp_conn_request(). But briefly, the squeue is picked by 177 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 178 * 179 * Closing a connection: 180 * 181 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 182 * via squeue to do the close and mark the tcp as detached if the connection 183 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 184 * reference but tcp_close() drop IP's reference always. So if tcp was 185 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 186 * and 1 because it is in classifier's connected hash. This is the condition 187 * we use to determine that its OK to clean up the tcp outside of squeue 188 * when time wait expires (check the ref under fanout and conn_lock and 189 * if it is 2, remove it from fanout hash and kill it). 190 * 191 * Although close just drops the necessary references and marks the 192 * tcp_detached state, tcp_close needs to know the tcp_detached has been 193 * set (under squeue) before letting the STREAM go away (because a 194 * inbound packet might attempt to go up the STREAM while the close 195 * has happened and tcp_detached is not set). So a special lock and 196 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 197 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 198 * tcp_detached. 199 * 200 * Special provisions and fast paths: 201 * 202 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 203 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 204 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 205 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 206 * check to send packets directly to tcp_rput_data via squeue. Everyone 207 * else comes through tcp_input() on the read side. 208 * 209 * We also make special provisions for sockfs by marking tcp_issocket 210 * whenever we have only sockfs on top of TCP. This allows us to skip 211 * putting the tcp in acceptor hash since a sockfs listener can never 212 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 213 * since eager has already been allocated and the accept now happens 214 * on acceptor STREAM. There is a big blob of comment on top of 215 * tcp_conn_request explaining the new accept. When socket is POP'd, 216 * sockfs sends us an ioctl to mark the fact and we go back to old 217 * behaviour. Once tcp_issocket is unset, its never set for the 218 * life of that connection. 219 * 220 * IPsec notes : 221 * 222 * Since a packet is always executed on the correct TCP perimeter 223 * all IPsec processing is defered to IP including checking new 224 * connections and setting IPSEC policies for new connection. The 225 * only exception is tcp_xmit_listeners_reset() which is called 226 * directly from IP and needs to policy check to see if TH_RST 227 * can be sent out. 228 * 229 * PFHooks notes : 230 * 231 * For mdt case, one meta buffer contains multiple packets. Mblks for every 232 * packet are assembled and passed to the hooks. When packets are blocked, 233 * or boundary of any packet is changed, the mdt processing is stopped, and 234 * packets of the meta buffer are send to the IP path one by one. 235 */ 236 237 extern major_t TCP6_MAJ; 238 239 /* 240 * Values for squeue switch: 241 * 1: squeue_enter_nodrain 242 * 2: squeue_enter 243 * 3: squeue_fill 244 */ 245 int tcp_squeue_close = 2; /* Setable in /etc/system */ 246 int tcp_squeue_wput = 2; 247 248 squeue_func_t tcp_squeue_close_proc; 249 squeue_func_t tcp_squeue_wput_proc; 250 251 /* 252 * This controls how tiny a write must be before we try to copy it 253 * into the the mblk on the tail of the transmit queue. Not much 254 * speedup is observed for values larger than sixteen. Zero will 255 * disable the optimisation. 256 */ 257 int tcp_tx_pull_len = 16; 258 259 /* 260 * TCP Statistics. 261 * 262 * How TCP statistics work. 263 * 264 * There are two types of statistics invoked by two macros. 265 * 266 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 267 * supposed to be used in non MT-hot paths of the code. 268 * 269 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 270 * supposed to be used for DEBUG purposes and may be used on a hot path. 271 * 272 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 273 * (use "kstat tcp" to get them). 274 * 275 * There is also additional debugging facility that marks tcp_clean_death() 276 * instances and saves them in tcp_t structure. It is triggered by 277 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 278 * tcp_clean_death() calls that counts the number of times each tag was hit. It 279 * is triggered by TCP_CLD_COUNTERS define. 280 * 281 * How to add new counters. 282 * 283 * 1) Add a field in the tcp_stat structure describing your counter. 284 * 2) Add a line in the template in tcp_kstat2_init() with the name 285 * of the counter. 286 * 287 * IMPORTANT!! - make sure that both are in sync !! 288 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 289 * 290 * Please avoid using private counters which are not kstat-exported. 291 * 292 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 293 * in tcp_t structure. 294 * 295 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 296 */ 297 298 #ifndef TCP_DEBUG_COUNTER 299 #ifdef DEBUG 300 #define TCP_DEBUG_COUNTER 1 301 #else 302 #define TCP_DEBUG_COUNTER 0 303 #endif 304 #endif 305 306 #define TCP_CLD_COUNTERS 0 307 308 #define TCP_TAG_CLEAN_DEATH 1 309 #define TCP_MAX_CLEAN_DEATH_TAG 32 310 311 #ifdef lint 312 static int _lint_dummy_; 313 #endif 314 315 #if TCP_CLD_COUNTERS 316 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 317 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 318 #elif defined(lint) 319 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 320 #else 321 #define TCP_CLD_STAT(x) 322 #endif 323 324 #if TCP_DEBUG_COUNTER 325 #define TCP_DBGSTAT(tcps, x) \ 326 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 327 #define TCP_G_DBGSTAT(x) \ 328 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 329 #elif defined(lint) 330 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 331 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 332 #else 333 #define TCP_DBGSTAT(tcps, x) 334 #define TCP_G_DBGSTAT(x) 335 #endif 336 337 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 338 339 tcp_g_stat_t tcp_g_statistics; 340 kstat_t *tcp_g_kstat; 341 342 /* 343 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 344 * tcp write side. 345 */ 346 #define CALL_IP_WPUT(connp, q, mp) { \ 347 tcp_stack_t *tcps; \ 348 \ 349 tcps = connp->conn_netstack->netstack_tcp; \ 350 ASSERT(((q)->q_flag & QREADR) == 0); \ 351 TCP_DBGSTAT(tcps, tcp_ip_output); \ 352 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 353 } 354 355 /* Macros for timestamp comparisons */ 356 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 357 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 358 359 /* 360 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 361 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 362 * by adding three components: a time component which grows by 1 every 4096 363 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 364 * a per-connection component which grows by 125000 for every new connection; 365 * and an "extra" component that grows by a random amount centered 366 * approximately on 64000. This causes the the ISS generator to cycle every 367 * 4.89 hours if no TCP connections are made, and faster if connections are 368 * made. 369 * 370 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 371 * components: a time component which grows by 250000 every second; and 372 * a per-connection component which grows by 125000 for every new connections. 373 * 374 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 375 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 376 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 377 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 378 * password. 379 */ 380 #define ISS_INCR 250000 381 #define ISS_NSEC_SHT 12 382 383 static sin_t sin_null; /* Zero address for quick clears */ 384 static sin6_t sin6_null; /* Zero address for quick clears */ 385 386 /* 387 * This implementation follows the 4.3BSD interpretation of the urgent 388 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 389 * incompatible changes in protocols like telnet and rlogin. 390 */ 391 #define TCP_OLD_URP_INTERPRETATION 1 392 393 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 394 (TCP_IS_DETACHED(tcp) && \ 395 (!(tcp)->tcp_hard_binding)) 396 397 /* 398 * TCP reassembly macros. We hide starting and ending sequence numbers in 399 * b_next and b_prev of messages on the reassembly queue. The messages are 400 * chained using b_cont. These macros are used in tcp_reass() so we don't 401 * have to see the ugly casts and assignments. 402 */ 403 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 404 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 405 (mblk_t *)(uintptr_t)(u)) 406 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 407 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 408 (mblk_t *)(uintptr_t)(u)) 409 410 /* 411 * Implementation of TCP Timers. 412 * ============================= 413 * 414 * INTERFACE: 415 * 416 * There are two basic functions dealing with tcp timers: 417 * 418 * timeout_id_t tcp_timeout(connp, func, time) 419 * clock_t tcp_timeout_cancel(connp, timeout_id) 420 * TCP_TIMER_RESTART(tcp, intvl) 421 * 422 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 423 * after 'time' ticks passed. The function called by timeout() must adhere to 424 * the same restrictions as a driver soft interrupt handler - it must not sleep 425 * or call other functions that might sleep. The value returned is the opaque 426 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 427 * cancel the request. The call to tcp_timeout() may fail in which case it 428 * returns zero. This is different from the timeout(9F) function which never 429 * fails. 430 * 431 * The call-back function 'func' always receives 'connp' as its single 432 * argument. It is always executed in the squeue corresponding to the tcp 433 * structure. The tcp structure is guaranteed to be present at the time the 434 * call-back is called. 435 * 436 * NOTE: The call-back function 'func' is never called if tcp is in 437 * the TCPS_CLOSED state. 438 * 439 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 440 * request. locks acquired by the call-back routine should not be held across 441 * the call to tcp_timeout_cancel() or a deadlock may result. 442 * 443 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 444 * Otherwise, it returns an integer value greater than or equal to 0. In 445 * particular, if the call-back function is already placed on the squeue, it can 446 * not be canceled. 447 * 448 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 449 * within squeue context corresponding to the tcp instance. Since the 450 * call-back is also called via the same squeue, there are no race 451 * conditions described in untimeout(9F) manual page since all calls are 452 * strictly serialized. 453 * 454 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 455 * stored in tcp_timer_tid and starts a new one using 456 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 457 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 458 * field. 459 * 460 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 461 * call-back may still be called, so it is possible tcp_timer() will be 462 * called several times. This should not be a problem since tcp_timer() 463 * should always check the tcp instance state. 464 * 465 * 466 * IMPLEMENTATION: 467 * 468 * TCP timers are implemented using three-stage process. The call to 469 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 470 * when the timer expires. The tcp_timer_callback() arranges the call of the 471 * tcp_timer_handler() function via squeue corresponding to the tcp 472 * instance. The tcp_timer_handler() calls actual requested timeout call-back 473 * and passes tcp instance as an argument to it. Information is passed between 474 * stages using the tcp_timer_t structure which contains the connp pointer, the 475 * tcp call-back to call and the timeout id returned by the timeout(9F). 476 * 477 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 478 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 479 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 480 * returns the pointer to this mblk. 481 * 482 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 483 * looks like a normal mblk without actual dblk attached to it. 484 * 485 * To optimize performance each tcp instance holds a small cache of timer 486 * mblocks. In the current implementation it caches up to two timer mblocks per 487 * tcp instance. The cache is preserved over tcp frees and is only freed when 488 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 489 * timer processing happens on a corresponding squeue, the cache manipulation 490 * does not require any locks. Experiments show that majority of timer mblocks 491 * allocations are satisfied from the tcp cache and do not involve kmem calls. 492 * 493 * The tcp_timeout() places a refhold on the connp instance which guarantees 494 * that it will be present at the time the call-back function fires. The 495 * tcp_timer_handler() drops the reference after calling the call-back, so the 496 * call-back function does not need to manipulate the references explicitly. 497 */ 498 499 typedef struct tcp_timer_s { 500 conn_t *connp; 501 void (*tcpt_proc)(void *); 502 timeout_id_t tcpt_tid; 503 } tcp_timer_t; 504 505 static kmem_cache_t *tcp_timercache; 506 kmem_cache_t *tcp_sack_info_cache; 507 kmem_cache_t *tcp_iphc_cache; 508 509 /* 510 * For scalability, we must not run a timer for every TCP connection 511 * in TIME_WAIT state. To see why, consider (for time wait interval of 512 * 4 minutes): 513 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 514 * 515 * This list is ordered by time, so you need only delete from the head 516 * until you get to entries which aren't old enough to delete yet. 517 * The list consists of only the detached TIME_WAIT connections. 518 * 519 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 520 * becomes detached TIME_WAIT (either by changing the state and already 521 * being detached or the other way around). This means that the TIME_WAIT 522 * state can be extended (up to doubled) if the connection doesn't become 523 * detached for a long time. 524 * 525 * The list manipulations (including tcp_time_wait_next/prev) 526 * are protected by the tcp_time_wait_lock. The content of the 527 * detached TIME_WAIT connections is protected by the normal perimeters. 528 * 529 * This list is per squeue and squeues are shared across the tcp_stack_t's. 530 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 531 * and conn_netstack. 532 * The tcp_t's that are added to tcp_free_list are disassociated and 533 * have NULL tcp_tcps and conn_netstack pointers. 534 */ 535 typedef struct tcp_squeue_priv_s { 536 kmutex_t tcp_time_wait_lock; 537 timeout_id_t tcp_time_wait_tid; 538 tcp_t *tcp_time_wait_head; 539 tcp_t *tcp_time_wait_tail; 540 tcp_t *tcp_free_list; 541 uint_t tcp_free_list_cnt; 542 } tcp_squeue_priv_t; 543 544 /* 545 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 546 * Running it every 5 seconds seems to give the best results. 547 */ 548 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 549 550 /* 551 * To prevent memory hog, limit the number of entries in tcp_free_list 552 * to 1% of available memory / number of cpus 553 */ 554 uint_t tcp_free_list_max_cnt = 0; 555 556 #define TCP_XMIT_LOWATER 4096 557 #define TCP_XMIT_HIWATER 49152 558 #define TCP_RECV_LOWATER 2048 559 #define TCP_RECV_HIWATER 49152 560 561 /* 562 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 563 */ 564 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 565 566 #define TIDUSZ 4096 /* transport interface data unit size */ 567 568 /* 569 * Bind hash list size and has function. It has to be a power of 2 for 570 * hashing. 571 */ 572 #define TCP_BIND_FANOUT_SIZE 512 573 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 574 /* 575 * Size of listen and acceptor hash list. It has to be a power of 2 for 576 * hashing. 577 */ 578 #define TCP_FANOUT_SIZE 256 579 580 #ifdef _ILP32 581 #define TCP_ACCEPTOR_HASH(accid) \ 582 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 583 #else 584 #define TCP_ACCEPTOR_HASH(accid) \ 585 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 586 #endif /* _ILP32 */ 587 588 #define IP_ADDR_CACHE_SIZE 2048 589 #define IP_ADDR_CACHE_HASH(faddr) \ 590 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 591 592 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 593 #define TCP_HSP_HASH_SIZE 256 594 595 #define TCP_HSP_HASH(addr) \ 596 (((addr>>24) ^ (addr >>16) ^ \ 597 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 598 599 /* 600 * TCP options struct returned from tcp_parse_options. 601 */ 602 typedef struct tcp_opt_s { 603 uint32_t tcp_opt_mss; 604 uint32_t tcp_opt_wscale; 605 uint32_t tcp_opt_ts_val; 606 uint32_t tcp_opt_ts_ecr; 607 tcp_t *tcp; 608 } tcp_opt_t; 609 610 /* 611 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 612 */ 613 614 #ifdef _BIG_ENDIAN 615 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 616 (TCPOPT_TSTAMP << 8) | 10) 617 #else 618 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 619 (TCPOPT_NOP << 8) | TCPOPT_NOP) 620 #endif 621 622 /* 623 * Flags returned from tcp_parse_options. 624 */ 625 #define TCP_OPT_MSS_PRESENT 1 626 #define TCP_OPT_WSCALE_PRESENT 2 627 #define TCP_OPT_TSTAMP_PRESENT 4 628 #define TCP_OPT_SACK_OK_PRESENT 8 629 #define TCP_OPT_SACK_PRESENT 16 630 631 /* TCP option length */ 632 #define TCPOPT_NOP_LEN 1 633 #define TCPOPT_MAXSEG_LEN 4 634 #define TCPOPT_WS_LEN 3 635 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 636 #define TCPOPT_TSTAMP_LEN 10 637 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 638 #define TCPOPT_SACK_OK_LEN 2 639 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 640 #define TCPOPT_REAL_SACK_LEN 4 641 #define TCPOPT_MAX_SACK_LEN 36 642 #define TCPOPT_HEADER_LEN 2 643 644 /* TCP cwnd burst factor. */ 645 #define TCP_CWND_INFINITE 65535 646 #define TCP_CWND_SS 3 647 #define TCP_CWND_NORMAL 5 648 649 /* Maximum TCP initial cwin (start/restart). */ 650 #define TCP_MAX_INIT_CWND 8 651 652 /* 653 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 654 * either tcp_slow_start_initial or tcp_slow_start_after idle 655 * depending on the caller. If the upper layer has not used the 656 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 657 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 658 * If the upper layer has changed set the tcp_init_cwnd, just use 659 * it to calculate the tcp_cwnd. 660 */ 661 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 662 { \ 663 if ((tcp)->tcp_init_cwnd == 0) { \ 664 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 665 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 666 } else { \ 667 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 668 } \ 669 tcp->tcp_cwnd_cnt = 0; \ 670 } 671 672 /* TCP Timer control structure */ 673 typedef struct tcpt_s { 674 pfv_t tcpt_pfv; /* The routine we are to call */ 675 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 676 } tcpt_t; 677 678 /* Host Specific Parameter structure */ 679 typedef struct tcp_hsp { 680 struct tcp_hsp *tcp_hsp_next; 681 in6_addr_t tcp_hsp_addr_v6; 682 in6_addr_t tcp_hsp_subnet_v6; 683 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 684 int32_t tcp_hsp_sendspace; 685 int32_t tcp_hsp_recvspace; 686 int32_t tcp_hsp_tstamp; 687 } tcp_hsp_t; 688 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 689 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 690 691 /* 692 * Functions called directly via squeue having a prototype of edesc_t. 693 */ 694 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 695 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 696 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 697 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 698 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 699 void tcp_input(void *arg, mblk_t *mp, void *arg2); 700 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 701 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 702 void tcp_output(void *arg, mblk_t *mp, void *arg2); 703 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 704 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 705 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 706 707 708 /* Prototype for TCP functions */ 709 static void tcp_random_init(void); 710 int tcp_random(void); 711 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 712 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 713 tcp_t *eager); 714 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 715 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 716 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 717 boolean_t user_specified); 718 static void tcp_closei_local(tcp_t *tcp); 719 static void tcp_close_detached(tcp_t *tcp); 720 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 721 mblk_t *idmp, mblk_t **defermp); 722 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 723 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 724 in_port_t dstport, uint_t srcid); 725 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 726 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 727 uint32_t scope_id); 728 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 729 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 730 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 731 static char *tcp_display(tcp_t *tcp, char *, char); 732 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 733 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 734 static void tcp_eager_unlink(tcp_t *tcp); 735 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 736 int unixerr); 737 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 738 int tlierr, int unixerr); 739 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 740 cred_t *cr); 741 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 742 char *value, caddr_t cp, cred_t *cr); 743 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 744 char *value, caddr_t cp, cred_t *cr); 745 static int tcp_tpistate(tcp_t *tcp); 746 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 747 int caller_holds_lock); 748 static void tcp_bind_hash_remove(tcp_t *tcp); 749 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 750 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 751 static void tcp_acceptor_hash_remove(tcp_t *tcp); 752 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 753 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 754 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 755 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 756 void tcp_g_q_setup(tcp_stack_t *); 757 void tcp_g_q_create(tcp_stack_t *); 758 void tcp_g_q_destroy(tcp_stack_t *); 759 static int tcp_header_init_ipv4(tcp_t *tcp); 760 static int tcp_header_init_ipv6(tcp_t *tcp); 761 int tcp_init(tcp_t *tcp, queue_t *q); 762 static int tcp_init_values(tcp_t *tcp); 763 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 764 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 765 t_scalar_t addr_length); 766 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 767 static void tcp_ip_notify(tcp_t *tcp); 768 static mblk_t *tcp_ire_mp(mblk_t *mp); 769 static void tcp_iss_init(tcp_t *tcp); 770 static void tcp_keepalive_killer(void *arg); 771 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 772 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 773 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 774 int *do_disconnectp, int *t_errorp, int *sys_errorp); 775 static boolean_t tcp_allow_connopt_set(int level, int name); 776 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 777 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 778 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 779 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 780 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 781 mblk_t *mblk); 782 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 783 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 784 uchar_t *ptr, uint_t len); 785 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 786 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 787 tcp_stack_t *); 788 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 789 caddr_t cp, cred_t *cr); 790 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 791 caddr_t cp, cred_t *cr); 792 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 793 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 794 caddr_t cp, cred_t *cr); 795 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 796 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 797 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 798 static void tcp_reinit(tcp_t *tcp); 799 static void tcp_reinit_values(tcp_t *tcp); 800 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 801 tcp_t *thisstream, cred_t *cr); 802 803 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 804 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 805 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 806 static void tcp_ss_rexmit(tcp_t *tcp); 807 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 808 static void tcp_process_options(tcp_t *, tcph_t *); 809 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 810 static void tcp_rsrv(queue_t *q); 811 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 812 static int tcp_snmp_state(tcp_t *tcp); 813 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 814 cred_t *cr); 815 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 816 cred_t *cr); 817 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 818 cred_t *cr); 819 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 820 cred_t *cr); 821 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 822 cred_t *cr); 823 static int tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, 824 caddr_t cp, cred_t *cr); 825 static int tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, 826 caddr_t cp, cred_t *cr); 827 static int tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, 828 cred_t *cr); 829 static void tcp_timer(void *arg); 830 static void tcp_timer_callback(void *); 831 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 832 boolean_t random); 833 static in_port_t tcp_get_next_priv_port(const tcp_t *); 834 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 835 void tcp_wput_accept(queue_t *q, mblk_t *mp); 836 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 837 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 838 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 839 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 840 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 841 const int num_sack_blk, int *usable, uint_t *snxt, 842 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 843 const int mdt_thres); 844 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 845 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 846 const int num_sack_blk, int *usable, uint_t *snxt, 847 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 848 const int mdt_thres); 849 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 850 int num_sack_blk); 851 static void tcp_wsrv(queue_t *q); 852 static int tcp_xmit_end(tcp_t *tcp); 853 static void tcp_ack_timer(void *arg); 854 static mblk_t *tcp_ack_mp(tcp_t *tcp); 855 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 856 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 857 zoneid_t zoneid, tcp_stack_t *); 858 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 859 uint32_t ack, int ctl); 860 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 861 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 862 static int setmaxps(queue_t *q, int maxpsz); 863 static void tcp_set_rto(tcp_t *, time_t); 864 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 865 boolean_t, boolean_t); 866 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 867 boolean_t ipsec_mctl); 868 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 869 char *opt, int optlen); 870 static int tcp_build_hdrs(queue_t *, tcp_t *); 871 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 872 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 873 tcph_t *tcph); 874 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 875 boolean_t tcp_reserved_port_add(int, in_port_t *, in_port_t *); 876 boolean_t tcp_reserved_port_del(in_port_t, in_port_t); 877 boolean_t tcp_reserved_port_check(in_port_t, tcp_stack_t *); 878 static tcp_t *tcp_alloc_temp_tcp(in_port_t, tcp_stack_t *); 879 static int tcp_reserved_port_list(queue_t *, mblk_t *, caddr_t, cred_t *); 880 static mblk_t *tcp_mdt_info_mp(mblk_t *); 881 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 882 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 883 const boolean_t, const uint32_t, const uint32_t, 884 const uint32_t, const uint32_t, tcp_stack_t *); 885 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 886 const uint_t, const uint_t, boolean_t *); 887 static mblk_t *tcp_lso_info_mp(mblk_t *); 888 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 889 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 890 extern mblk_t *tcp_timermp_alloc(int); 891 extern void tcp_timermp_free(tcp_t *); 892 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 893 static void tcp_stop_lingering(tcp_t *tcp); 894 static void tcp_close_linger_timeout(void *arg); 895 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 896 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 897 static void tcp_stack_fini(netstackid_t stackid, void *arg); 898 static void *tcp_g_kstat_init(tcp_g_stat_t *); 899 static void tcp_g_kstat_fini(kstat_t *); 900 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 901 static void tcp_kstat_fini(netstackid_t, kstat_t *); 902 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 903 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 904 static int tcp_kstat_update(kstat_t *kp, int rw); 905 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 906 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 907 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 908 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 909 tcph_t *tcph, mblk_t *idmp); 910 static squeue_func_t tcp_squeue_switch(int); 911 912 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *); 913 static int tcp_close(queue_t *, int); 914 static int tcpclose_accept(queue_t *); 915 static int tcp_modclose(queue_t *); 916 static void tcp_wput_mod(queue_t *, mblk_t *); 917 918 static void tcp_squeue_add(squeue_t *); 919 static boolean_t tcp_zcopy_check(tcp_t *); 920 static void tcp_zcopy_notify(tcp_t *); 921 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 922 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 923 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 924 925 extern void tcp_kssl_input(tcp_t *, mblk_t *); 926 927 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 928 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 929 930 /* 931 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 932 * 933 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 934 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 935 * (defined in tcp.h) needs to be filled in and passed into the kernel 936 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 937 * structure contains the four-tuple of a TCP connection and a range of TCP 938 * states (specified by ac_start and ac_end). The use of wildcard addresses 939 * and ports is allowed. Connections with a matching four tuple and a state 940 * within the specified range will be aborted. The valid states for the 941 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 942 * inclusive. 943 * 944 * An application which has its connection aborted by this ioctl will receive 945 * an error that is dependent on the connection state at the time of the abort. 946 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 947 * though a RST packet has been received. If the connection state is equal to 948 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 949 * and all resources associated with the connection will be freed. 950 */ 951 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 952 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 953 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 954 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 955 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 956 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 957 boolean_t, tcp_stack_t *); 958 959 static struct module_info tcp_rinfo = { 960 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 961 }; 962 963 static struct module_info tcp_winfo = { 964 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 965 }; 966 967 /* 968 * Entry points for TCP as a module. It only allows SNMP requests 969 * to pass through. 970 */ 971 struct qinit tcp_mod_rinit = { 972 (pfi_t)putnext, NULL, tcp_open, ip_snmpmod_close, NULL, &tcp_rinfo, 973 }; 974 975 struct qinit tcp_mod_winit = { 976 (pfi_t)ip_snmpmod_wput, NULL, tcp_open, ip_snmpmod_close, NULL, 977 &tcp_rinfo 978 }; 979 980 /* 981 * Entry points for TCP as a device. The normal case which supports 982 * the TCP functionality. 983 */ 984 struct qinit tcp_rinit = { 985 NULL, (pfi_t)tcp_rsrv, tcp_open, tcp_close, NULL, &tcp_rinfo 986 }; 987 988 struct qinit tcp_winit = { 989 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 990 }; 991 992 /* Initial entry point for TCP in socket mode. */ 993 struct qinit tcp_sock_winit = { 994 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 995 }; 996 997 /* 998 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 999 * an accept. Avoid allocating data structures since eager has already 1000 * been created. 1001 */ 1002 struct qinit tcp_acceptor_rinit = { 1003 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1004 }; 1005 1006 struct qinit tcp_acceptor_winit = { 1007 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1008 }; 1009 1010 /* 1011 * Entry points for TCP loopback (read side only) 1012 */ 1013 struct qinit tcp_loopback_rinit = { 1014 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_open, tcp_close, (pfi_t)0, 1015 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1016 }; 1017 1018 struct streamtab tcpinfo = { 1019 &tcp_rinit, &tcp_winit 1020 }; 1021 1022 /* 1023 * Have to ensure that tcp_g_q_close is not done by an 1024 * interrupt thread. 1025 */ 1026 static taskq_t *tcp_taskq; 1027 1028 /* 1029 * TCP has a private interface for other kernel modules to reserve a 1030 * port range for them to use. Once reserved, TCP will not use any ports 1031 * in the range. This interface relies on the TCP_EXCLBIND feature. If 1032 * the semantics of TCP_EXCLBIND is changed, implementation of this interface 1033 * has to be verified. 1034 * 1035 * There can be TCP_RESERVED_PORTS_ARRAY_MAX_SIZE port ranges. Each port 1036 * range can cover at most TCP_RESERVED_PORTS_RANGE_MAX ports. A port 1037 * range is [port a, port b] inclusive. And each port range is between 1038 * TCP_LOWESET_RESERVED_PORT and TCP_LARGEST_RESERVED_PORT inclusive. 1039 * 1040 * Note that the default anonymous port range starts from 32768. There is 1041 * no port "collision" between that and the reserved port range. If there 1042 * is port collision (because the default smallest anonymous port is lowered 1043 * or some apps specifically bind to ports in the reserved port range), the 1044 * system may not be able to reserve a port range even there are enough 1045 * unbound ports as a reserved port range contains consecutive ports . 1046 */ 1047 #define TCP_RESERVED_PORTS_ARRAY_MAX_SIZE 5 1048 #define TCP_RESERVED_PORTS_RANGE_MAX 1000 1049 #define TCP_SMALLEST_RESERVED_PORT 10240 1050 #define TCP_LARGEST_RESERVED_PORT 20480 1051 1052 /* Structure to represent those reserved port ranges. */ 1053 typedef struct tcp_rport_s { 1054 in_port_t lo_port; 1055 in_port_t hi_port; 1056 tcp_t **temp_tcp_array; 1057 } tcp_rport_t; 1058 1059 /* Setable only in /etc/system. Move to ndd? */ 1060 boolean_t tcp_icmp_source_quench = B_FALSE; 1061 1062 /* 1063 * Following assumes TPI alignment requirements stay along 32 bit 1064 * boundaries 1065 */ 1066 #define ROUNDUP32(x) \ 1067 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1068 1069 /* Template for response to info request. */ 1070 static struct T_info_ack tcp_g_t_info_ack = { 1071 T_INFO_ACK, /* PRIM_type */ 1072 0, /* TSDU_size */ 1073 T_INFINITE, /* ETSDU_size */ 1074 T_INVALID, /* CDATA_size */ 1075 T_INVALID, /* DDATA_size */ 1076 sizeof (sin_t), /* ADDR_size */ 1077 0, /* OPT_size - not initialized here */ 1078 TIDUSZ, /* TIDU_size */ 1079 T_COTS_ORD, /* SERV_type */ 1080 TCPS_IDLE, /* CURRENT_state */ 1081 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1082 }; 1083 1084 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1085 T_INFO_ACK, /* PRIM_type */ 1086 0, /* TSDU_size */ 1087 T_INFINITE, /* ETSDU_size */ 1088 T_INVALID, /* CDATA_size */ 1089 T_INVALID, /* DDATA_size */ 1090 sizeof (sin6_t), /* ADDR_size */ 1091 0, /* OPT_size - not initialized here */ 1092 TIDUSZ, /* TIDU_size */ 1093 T_COTS_ORD, /* SERV_type */ 1094 TCPS_IDLE, /* CURRENT_state */ 1095 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1096 }; 1097 1098 #define MS 1L 1099 #define SECONDS (1000 * MS) 1100 #define MINUTES (60 * SECONDS) 1101 #define HOURS (60 * MINUTES) 1102 #define DAYS (24 * HOURS) 1103 1104 #define PARAM_MAX (~(uint32_t)0) 1105 1106 /* Max size IP datagram is 64k - 1 */ 1107 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1108 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1109 /* Max of the above */ 1110 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1111 1112 /* Largest TCP port number */ 1113 #define TCP_MAX_PORT (64 * 1024 - 1) 1114 1115 /* 1116 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1117 * layer header. It has to be a multiple of 4. 1118 */ 1119 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1120 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1121 1122 /* 1123 * All of these are alterable, within the min/max values given, at run time. 1124 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1125 * per the TCP spec. 1126 */ 1127 /* BEGIN CSTYLED */ 1128 static tcpparam_t lcl_tcp_param_arr[] = { 1129 /*min max value name */ 1130 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1131 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1132 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1133 { 1, 1024, 1, "tcp_conn_req_min" }, 1134 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1135 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1136 { 0, 10, 0, "tcp_debug" }, 1137 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1138 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1139 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1140 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1141 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1142 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1143 { 1, 255, 64, "tcp_ipv4_ttl"}, 1144 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1145 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1146 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1147 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1148 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1149 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1150 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1151 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1152 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1153 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1154 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1155 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1156 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1157 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1158 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1159 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1160 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1161 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1162 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1163 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1164 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1165 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1166 { 0, TCP_MSS_MAX, 64, "tcp_co_min"}, 1167 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1168 /* 1169 * Question: What default value should I set for tcp_strong_iss? 1170 */ 1171 { 0, 2, 1, "tcp_strong_iss"}, 1172 { 0, 65536, 20, "tcp_rtt_updates"}, 1173 { 0, 1, 1, "tcp_wscale_always"}, 1174 { 0, 1, 0, "tcp_tstamp_always"}, 1175 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1176 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1177 { 0, 16, 2, "tcp_deferred_acks_max"}, 1178 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1179 { 1, 4, 4, "tcp_slow_start_initial"}, 1180 { 10*MS, 50*MS, 20*MS, "tcp_co_timer_interval"}, 1181 { 0, 2, 2, "tcp_sack_permitted"}, 1182 { 0, 1, 0, "tcp_trace"}, 1183 { 0, 1, 1, "tcp_compression_enabled"}, 1184 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1185 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1186 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1187 { 0, 1, 0, "tcp_rev_src_routes"}, 1188 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1189 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1190 { 0, 16, 8, "tcp_local_dacks_max"}, 1191 { 0, 2, 1, "tcp_ecn_permitted"}, 1192 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1193 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1194 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1195 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1196 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1197 }; 1198 /* END CSTYLED */ 1199 1200 /* 1201 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1202 * each header fragment in the header buffer. Each parameter value has 1203 * to be a multiple of 4 (32-bit aligned). 1204 */ 1205 static tcpparam_t lcl_tcp_mdt_head_param = 1206 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1207 static tcpparam_t lcl_tcp_mdt_tail_param = 1208 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1209 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1210 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1211 1212 /* 1213 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1214 * the maximum number of payload buffers associated per Multidata. 1215 */ 1216 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1217 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1218 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1219 1220 /* Round up the value to the nearest mss. */ 1221 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1222 1223 /* 1224 * Set ECN capable transport (ECT) code point in IP header. 1225 * 1226 * Note that there are 2 ECT code points '01' and '10', which are called 1227 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1228 * point ECT(0) for TCP as described in RFC 2481. 1229 */ 1230 #define SET_ECT(tcp, iph) \ 1231 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1232 /* We need to clear the code point first. */ \ 1233 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1234 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1235 } else { \ 1236 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1237 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1238 } 1239 1240 /* 1241 * The format argument to pass to tcp_display(). 1242 * DISP_PORT_ONLY means that the returned string has only port info. 1243 * DISP_ADDR_AND_PORT means that the returned string also contains the 1244 * remote and local IP address. 1245 */ 1246 #define DISP_PORT_ONLY 1 1247 #define DISP_ADDR_AND_PORT 2 1248 1249 #define NDD_TOO_QUICK_MSG \ 1250 "ndd get info rate too high for non-privileged users, try again " \ 1251 "later.\n" 1252 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1253 1254 #define IS_VMLOANED_MBLK(mp) \ 1255 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1256 1257 1258 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1259 boolean_t tcp_mdt_chain = B_TRUE; 1260 1261 /* 1262 * MDT threshold in the form of effective send MSS multiplier; we take 1263 * the MDT path if the amount of unsent data exceeds the threshold value 1264 * (default threshold is 1*SMSS). 1265 */ 1266 uint_t tcp_mdt_smss_threshold = 1; 1267 1268 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1269 1270 /* 1271 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1272 * tunable settable via NDD. Otherwise, the per-connection behavior is 1273 * determined dynamically during tcp_adapt_ire(), which is the default. 1274 */ 1275 boolean_t tcp_static_maxpsz = B_FALSE; 1276 1277 /* Setable in /etc/system */ 1278 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1279 uint32_t tcp_random_anon_port = 1; 1280 1281 /* 1282 * To reach to an eager in Q0 which can be dropped due to an incoming 1283 * new SYN request when Q0 is full, a new doubly linked list is 1284 * introduced. This list allows to select an eager from Q0 in O(1) time. 1285 * This is needed to avoid spending too much time walking through the 1286 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1287 * this new list has to be a member of Q0. 1288 * This list is headed by listener's tcp_t. When the list is empty, 1289 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1290 * of listener's tcp_t point to listener's tcp_t itself. 1291 * 1292 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1293 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1294 * These macros do not affect the eager's membership to Q0. 1295 */ 1296 1297 1298 #define MAKE_DROPPABLE(listener, eager) \ 1299 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1300 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1301 = (eager); \ 1302 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1303 (eager)->tcp_eager_next_drop_q0 = \ 1304 (listener)->tcp_eager_next_drop_q0; \ 1305 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1306 } 1307 1308 #define MAKE_UNDROPPABLE(eager) \ 1309 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1310 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1311 = (eager)->tcp_eager_prev_drop_q0; \ 1312 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1313 = (eager)->tcp_eager_next_drop_q0; \ 1314 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1315 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1316 } 1317 1318 /* 1319 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1320 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1321 * data, TCP will not respond with an ACK. RFC 793 requires that 1322 * TCP responds with an ACK for such a bogus ACK. By not following 1323 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1324 * an attacker successfully spoofs an acceptable segment to our 1325 * peer; or when our peer is "confused." 1326 */ 1327 uint32_t tcp_drop_ack_unsent_cnt = 10; 1328 1329 /* 1330 * Hook functions to enable cluster networking 1331 * On non-clustered systems these vectors must always be NULL. 1332 */ 1333 1334 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1335 uint8_t *laddrp, in_port_t lport) = NULL; 1336 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1337 uint8_t *laddrp, in_port_t lport) = NULL; 1338 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1339 uint8_t *laddrp, in_port_t lport, 1340 uint8_t *faddrp, in_port_t fport) = NULL; 1341 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1342 uint8_t *laddrp, in_port_t lport, 1343 uint8_t *faddrp, in_port_t fport) = NULL; 1344 1345 /* 1346 * The following are defined in ip.c 1347 */ 1348 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1349 uint8_t *laddrp); 1350 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1351 uint8_t *laddrp, uint8_t *faddrp); 1352 1353 #define CL_INET_CONNECT(tcp) { \ 1354 if (cl_inet_connect != NULL) { \ 1355 /* \ 1356 * Running in cluster mode - register active connection \ 1357 * information \ 1358 */ \ 1359 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1360 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1361 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1362 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1363 (in_port_t)(tcp)->tcp_lport, \ 1364 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1365 (in_port_t)(tcp)->tcp_fport); \ 1366 } \ 1367 } else { \ 1368 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1369 &(tcp)->tcp_ip6h->ip6_src)) {\ 1370 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1371 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1372 (in_port_t)(tcp)->tcp_lport, \ 1373 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1374 (in_port_t)(tcp)->tcp_fport); \ 1375 } \ 1376 } \ 1377 } \ 1378 } 1379 1380 #define CL_INET_DISCONNECT(tcp) { \ 1381 if (cl_inet_disconnect != NULL) { \ 1382 /* \ 1383 * Running in cluster mode - deregister active \ 1384 * connection information \ 1385 */ \ 1386 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1387 if ((tcp)->tcp_ip_src != 0) { \ 1388 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1389 AF_INET, \ 1390 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1391 (in_port_t)(tcp)->tcp_lport, \ 1392 (uint8_t *) \ 1393 (&((tcp)->tcp_ipha->ipha_dst)),\ 1394 (in_port_t)(tcp)->tcp_fport); \ 1395 } \ 1396 } else { \ 1397 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1398 &(tcp)->tcp_ip_src_v6)) { \ 1399 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1400 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1401 (in_port_t)(tcp)->tcp_lport, \ 1402 (uint8_t *) \ 1403 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1404 (in_port_t)(tcp)->tcp_fport); \ 1405 } \ 1406 } \ 1407 } \ 1408 } 1409 1410 /* 1411 * Cluster networking hook for traversing current connection list. 1412 * This routine is used to extract the current list of live connections 1413 * which must continue to to be dispatched to this node. 1414 */ 1415 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1416 1417 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1418 void *arg, tcp_stack_t *tcps); 1419 1420 /* 1421 * Figure out the value of window scale opton. Note that the rwnd is 1422 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1423 * We cannot find the scale value and then do a round up of tcp_rwnd 1424 * because the scale value may not be correct after that. 1425 * 1426 * Set the compiler flag to make this function inline. 1427 */ 1428 static void 1429 tcp_set_ws_value(tcp_t *tcp) 1430 { 1431 int i; 1432 uint32_t rwnd = tcp->tcp_rwnd; 1433 1434 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1435 i++, rwnd >>= 1) 1436 ; 1437 tcp->tcp_rcv_ws = i; 1438 } 1439 1440 /* 1441 * Remove a connection from the list of detached TIME_WAIT connections. 1442 * It returns B_FALSE if it can't remove the connection from the list 1443 * as the connection has already been removed from the list due to an 1444 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1445 */ 1446 static boolean_t 1447 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1448 { 1449 boolean_t locked = B_FALSE; 1450 1451 if (tcp_time_wait == NULL) { 1452 tcp_time_wait = *((tcp_squeue_priv_t **) 1453 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1454 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1455 locked = B_TRUE; 1456 } else { 1457 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1458 } 1459 1460 if (tcp->tcp_time_wait_expire == 0) { 1461 ASSERT(tcp->tcp_time_wait_next == NULL); 1462 ASSERT(tcp->tcp_time_wait_prev == NULL); 1463 if (locked) 1464 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1465 return (B_FALSE); 1466 } 1467 ASSERT(TCP_IS_DETACHED(tcp)); 1468 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1469 1470 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1471 ASSERT(tcp->tcp_time_wait_prev == NULL); 1472 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1473 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1474 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1475 NULL; 1476 } else { 1477 tcp_time_wait->tcp_time_wait_tail = NULL; 1478 } 1479 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1480 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1481 ASSERT(tcp->tcp_time_wait_next == NULL); 1482 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1483 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1484 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1485 } else { 1486 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1487 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1488 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1489 tcp->tcp_time_wait_next; 1490 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1491 tcp->tcp_time_wait_prev; 1492 } 1493 tcp->tcp_time_wait_next = NULL; 1494 tcp->tcp_time_wait_prev = NULL; 1495 tcp->tcp_time_wait_expire = 0; 1496 1497 if (locked) 1498 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1499 return (B_TRUE); 1500 } 1501 1502 /* 1503 * Add a connection to the list of detached TIME_WAIT connections 1504 * and set its time to expire. 1505 */ 1506 static void 1507 tcp_time_wait_append(tcp_t *tcp) 1508 { 1509 tcp_stack_t *tcps = tcp->tcp_tcps; 1510 tcp_squeue_priv_t *tcp_time_wait = 1511 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1512 SQPRIVATE_TCP)); 1513 1514 tcp_timers_stop(tcp); 1515 1516 /* Freed above */ 1517 ASSERT(tcp->tcp_timer_tid == 0); 1518 ASSERT(tcp->tcp_ack_tid == 0); 1519 1520 /* must have happened at the time of detaching the tcp */ 1521 ASSERT(tcp->tcp_ptpahn == NULL); 1522 ASSERT(tcp->tcp_flow_stopped == 0); 1523 ASSERT(tcp->tcp_time_wait_next == NULL); 1524 ASSERT(tcp->tcp_time_wait_prev == NULL); 1525 ASSERT(tcp->tcp_time_wait_expire == NULL); 1526 ASSERT(tcp->tcp_listener == NULL); 1527 1528 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1529 /* 1530 * The value computed below in tcp->tcp_time_wait_expire may 1531 * appear negative or wrap around. That is ok since our 1532 * interest is only in the difference between the current lbolt 1533 * value and tcp->tcp_time_wait_expire. But the value should not 1534 * be zero, since it means the tcp is not in the TIME_WAIT list. 1535 * The corresponding comparison in tcp_time_wait_collector() uses 1536 * modular arithmetic. 1537 */ 1538 tcp->tcp_time_wait_expire += 1539 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1540 if (tcp->tcp_time_wait_expire == 0) 1541 tcp->tcp_time_wait_expire = 1; 1542 1543 ASSERT(TCP_IS_DETACHED(tcp)); 1544 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1545 ASSERT(tcp->tcp_time_wait_next == NULL); 1546 ASSERT(tcp->tcp_time_wait_prev == NULL); 1547 TCP_DBGSTAT(tcps, tcp_time_wait); 1548 1549 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1550 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1551 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1552 tcp_time_wait->tcp_time_wait_head = tcp; 1553 } else { 1554 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1555 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1556 TCPS_TIME_WAIT); 1557 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1558 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1559 } 1560 tcp_time_wait->tcp_time_wait_tail = tcp; 1561 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1562 } 1563 1564 /* ARGSUSED */ 1565 void 1566 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1567 { 1568 conn_t *connp = (conn_t *)arg; 1569 tcp_t *tcp = connp->conn_tcp; 1570 tcp_stack_t *tcps = tcp->tcp_tcps; 1571 1572 ASSERT(tcp != NULL); 1573 if (tcp->tcp_state == TCPS_CLOSED) { 1574 return; 1575 } 1576 1577 ASSERT((tcp->tcp_family == AF_INET && 1578 tcp->tcp_ipversion == IPV4_VERSION) || 1579 (tcp->tcp_family == AF_INET6 && 1580 (tcp->tcp_ipversion == IPV4_VERSION || 1581 tcp->tcp_ipversion == IPV6_VERSION))); 1582 ASSERT(!tcp->tcp_listener); 1583 1584 TCP_STAT(tcps, tcp_time_wait_reap); 1585 ASSERT(TCP_IS_DETACHED(tcp)); 1586 1587 /* 1588 * Because they have no upstream client to rebind or tcp_close() 1589 * them later, we axe the connection here and now. 1590 */ 1591 tcp_close_detached(tcp); 1592 } 1593 1594 /* 1595 * Remove cached/latched IPsec references. 1596 */ 1597 void 1598 tcp_ipsec_cleanup(tcp_t *tcp) 1599 { 1600 conn_t *connp = tcp->tcp_connp; 1601 1602 if (connp->conn_flags & IPCL_TCPCONN) { 1603 if (connp->conn_latch != NULL) { 1604 IPLATCH_REFRELE(connp->conn_latch, 1605 connp->conn_netstack); 1606 connp->conn_latch = NULL; 1607 } 1608 if (connp->conn_policy != NULL) { 1609 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1610 connp->conn_policy = NULL; 1611 } 1612 } 1613 } 1614 1615 /* 1616 * Cleaup before placing on free list. 1617 * Disassociate from the netstack/tcp_stack_t since the freelist 1618 * is per squeue and not per netstack. 1619 */ 1620 void 1621 tcp_cleanup(tcp_t *tcp) 1622 { 1623 mblk_t *mp; 1624 char *tcp_iphc; 1625 int tcp_iphc_len; 1626 int tcp_hdr_grown; 1627 tcp_sack_info_t *tcp_sack_info; 1628 conn_t *connp = tcp->tcp_connp; 1629 tcp_stack_t *tcps = tcp->tcp_tcps; 1630 netstack_t *ns = tcps->tcps_netstack; 1631 1632 tcp_bind_hash_remove(tcp); 1633 1634 /* Cleanup that which needs the netstack first */ 1635 tcp_ipsec_cleanup(tcp); 1636 1637 tcp_free(tcp); 1638 1639 /* Release any SSL context */ 1640 if (tcp->tcp_kssl_ent != NULL) { 1641 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1642 tcp->tcp_kssl_ent = NULL; 1643 } 1644 1645 if (tcp->tcp_kssl_ctx != NULL) { 1646 kssl_release_ctx(tcp->tcp_kssl_ctx); 1647 tcp->tcp_kssl_ctx = NULL; 1648 } 1649 tcp->tcp_kssl_pending = B_FALSE; 1650 1651 conn_delete_ire(connp, NULL); 1652 1653 /* 1654 * Since we will bzero the entire structure, we need to 1655 * remove it and reinsert it in global hash list. We 1656 * know the walkers can't get to this conn because we 1657 * had set CONDEMNED flag earlier and checked reference 1658 * under conn_lock so walker won't pick it and when we 1659 * go the ipcl_globalhash_remove() below, no walker 1660 * can get to it. 1661 */ 1662 ipcl_globalhash_remove(connp); 1663 1664 /* 1665 * Now it is safe to decrement the reference counts. 1666 * This might be the last reference on the netstack and TCPS 1667 * in which case it will cause the tcp_g_q_close and 1668 * the freeing of the IP Instance. 1669 */ 1670 connp->conn_netstack = NULL; 1671 netstack_rele(ns); 1672 ASSERT(tcps != NULL); 1673 tcp->tcp_tcps = NULL; 1674 TCPS_REFRELE(tcps); 1675 1676 /* Save some state */ 1677 mp = tcp->tcp_timercache; 1678 1679 tcp_sack_info = tcp->tcp_sack_info; 1680 tcp_iphc = tcp->tcp_iphc; 1681 tcp_iphc_len = tcp->tcp_iphc_len; 1682 tcp_hdr_grown = tcp->tcp_hdr_grown; 1683 1684 if (connp->conn_cred != NULL) 1685 crfree(connp->conn_cred); 1686 if (connp->conn_peercred != NULL) 1687 crfree(connp->conn_peercred); 1688 bzero(connp, sizeof (conn_t)); 1689 bzero(tcp, sizeof (tcp_t)); 1690 1691 /* restore the state */ 1692 tcp->tcp_timercache = mp; 1693 1694 tcp->tcp_sack_info = tcp_sack_info; 1695 tcp->tcp_iphc = tcp_iphc; 1696 tcp->tcp_iphc_len = tcp_iphc_len; 1697 tcp->tcp_hdr_grown = tcp_hdr_grown; 1698 1699 1700 tcp->tcp_connp = connp; 1701 1702 connp->conn_tcp = tcp; 1703 connp->conn_flags = IPCL_TCPCONN; 1704 connp->conn_state_flags = CONN_INCIPIENT; 1705 connp->conn_ulp = IPPROTO_TCP; 1706 connp->conn_ref = 1; 1707 } 1708 1709 /* 1710 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1711 * is done forwards from the head. 1712 * This walks all stack instances since 1713 * tcp_time_wait remains global across all stacks. 1714 */ 1715 /* ARGSUSED */ 1716 void 1717 tcp_time_wait_collector(void *arg) 1718 { 1719 tcp_t *tcp; 1720 clock_t now; 1721 mblk_t *mp; 1722 conn_t *connp; 1723 kmutex_t *lock; 1724 boolean_t removed; 1725 1726 squeue_t *sqp = (squeue_t *)arg; 1727 tcp_squeue_priv_t *tcp_time_wait = 1728 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1729 1730 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1731 tcp_time_wait->tcp_time_wait_tid = 0; 1732 1733 if (tcp_time_wait->tcp_free_list != NULL && 1734 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1735 TCP_G_STAT(tcp_freelist_cleanup); 1736 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1737 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1738 tcp->tcp_time_wait_next = NULL; 1739 tcp_time_wait->tcp_free_list_cnt--; 1740 ASSERT(tcp->tcp_tcps == NULL); 1741 CONN_DEC_REF(tcp->tcp_connp); 1742 } 1743 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1744 } 1745 1746 /* 1747 * In order to reap time waits reliably, we should use a 1748 * source of time that is not adjustable by the user -- hence 1749 * the call to ddi_get_lbolt(). 1750 */ 1751 now = ddi_get_lbolt(); 1752 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1753 /* 1754 * Compare times using modular arithmetic, since 1755 * lbolt can wrapover. 1756 */ 1757 if ((now - tcp->tcp_time_wait_expire) < 0) { 1758 break; 1759 } 1760 1761 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1762 ASSERT(removed); 1763 1764 connp = tcp->tcp_connp; 1765 ASSERT(connp->conn_fanout != NULL); 1766 lock = &connp->conn_fanout->connf_lock; 1767 /* 1768 * This is essentially a TW reclaim fast path optimization for 1769 * performance where the timewait collector checks under the 1770 * fanout lock (so that no one else can get access to the 1771 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1772 * the classifier hash list. If ref count is indeed 2, we can 1773 * just remove the conn under the fanout lock and avoid 1774 * cleaning up the conn under the squeue, provided that 1775 * clustering callbacks are not enabled. If clustering is 1776 * enabled, we need to make the clustering callback before 1777 * setting the CONDEMNED flag and after dropping all locks and 1778 * so we forego this optimization and fall back to the slow 1779 * path. Also please see the comments in tcp_closei_local 1780 * regarding the refcnt logic. 1781 * 1782 * Since we are holding the tcp_time_wait_lock, its better 1783 * not to block on the fanout_lock because other connections 1784 * can't add themselves to time_wait list. So we do a 1785 * tryenter instead of mutex_enter. 1786 */ 1787 if (mutex_tryenter(lock)) { 1788 mutex_enter(&connp->conn_lock); 1789 if ((connp->conn_ref == 2) && 1790 (cl_inet_disconnect == NULL)) { 1791 ipcl_hash_remove_locked(connp, 1792 connp->conn_fanout); 1793 /* 1794 * Set the CONDEMNED flag now itself so that 1795 * the refcnt cannot increase due to any 1796 * walker. But we have still not cleaned up 1797 * conn_ire_cache. This is still ok since 1798 * we are going to clean it up in tcp_cleanup 1799 * immediately and any interface unplumb 1800 * thread will wait till the ire is blown away 1801 */ 1802 connp->conn_state_flags |= CONN_CONDEMNED; 1803 mutex_exit(lock); 1804 mutex_exit(&connp->conn_lock); 1805 if (tcp_time_wait->tcp_free_list_cnt < 1806 tcp_free_list_max_cnt) { 1807 /* Add to head of tcp_free_list */ 1808 mutex_exit( 1809 &tcp_time_wait->tcp_time_wait_lock); 1810 tcp_cleanup(tcp); 1811 ASSERT(connp->conn_latch == NULL); 1812 ASSERT(connp->conn_policy == NULL); 1813 ASSERT(tcp->tcp_tcps == NULL); 1814 ASSERT(connp->conn_netstack == NULL); 1815 1816 mutex_enter( 1817 &tcp_time_wait->tcp_time_wait_lock); 1818 tcp->tcp_time_wait_next = 1819 tcp_time_wait->tcp_free_list; 1820 tcp_time_wait->tcp_free_list = tcp; 1821 tcp_time_wait->tcp_free_list_cnt++; 1822 continue; 1823 } else { 1824 /* Do not add to tcp_free_list */ 1825 mutex_exit( 1826 &tcp_time_wait->tcp_time_wait_lock); 1827 tcp_bind_hash_remove(tcp); 1828 conn_delete_ire(tcp->tcp_connp, NULL); 1829 tcp_ipsec_cleanup(tcp); 1830 CONN_DEC_REF(tcp->tcp_connp); 1831 } 1832 } else { 1833 CONN_INC_REF_LOCKED(connp); 1834 mutex_exit(lock); 1835 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1836 mutex_exit(&connp->conn_lock); 1837 /* 1838 * We can reuse the closemp here since conn has 1839 * detached (otherwise we wouldn't even be in 1840 * time_wait list). tcp_closemp_used can safely 1841 * be changed without taking a lock as no other 1842 * thread can concurrently access it at this 1843 * point in the connection lifecycle. We 1844 * increment tcp_closemp_used to record any 1845 * attempt to reuse tcp_closemp while it is 1846 * still in use. 1847 */ 1848 1849 if (tcp->tcp_closemp.b_prev == NULL) 1850 tcp->tcp_closemp_used = 1; 1851 else 1852 tcp->tcp_closemp_used++; 1853 1854 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1855 mp = &tcp->tcp_closemp; 1856 squeue_fill(connp->conn_sqp, mp, 1857 tcp_timewait_output, connp, 1858 SQTAG_TCP_TIMEWAIT); 1859 } 1860 } else { 1861 mutex_enter(&connp->conn_lock); 1862 CONN_INC_REF_LOCKED(connp); 1863 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1864 mutex_exit(&connp->conn_lock); 1865 /* 1866 * We can reuse the closemp here since conn has 1867 * detached (otherwise we wouldn't even be in 1868 * time_wait list). tcp_closemp_used can safely 1869 * be changed without taking a lock as no other 1870 * thread can concurrently access it at this 1871 * point in the connection lifecycle. We 1872 * increment tcp_closemp_used to record any 1873 * attempt to reuse tcp_closemp while it is 1874 * still in use. 1875 */ 1876 1877 if (tcp->tcp_closemp.b_prev == NULL) 1878 tcp->tcp_closemp_used = 1; 1879 else 1880 tcp->tcp_closemp_used++; 1881 1882 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1883 mp = &tcp->tcp_closemp; 1884 squeue_fill(connp->conn_sqp, mp, 1885 tcp_timewait_output, connp, 0); 1886 } 1887 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1888 } 1889 1890 if (tcp_time_wait->tcp_free_list != NULL) 1891 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1892 1893 tcp_time_wait->tcp_time_wait_tid = 1894 timeout(tcp_time_wait_collector, sqp, TCP_TIME_WAIT_DELAY); 1895 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1896 } 1897 /* 1898 * Reply to a clients T_CONN_RES TPI message. This function 1899 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1900 * on the acceptor STREAM and processed in tcp_wput_accept(). 1901 * Read the block comment on top of tcp_conn_request(). 1902 */ 1903 static void 1904 tcp_accept(tcp_t *listener, mblk_t *mp) 1905 { 1906 tcp_t *acceptor; 1907 tcp_t *eager; 1908 tcp_t *tcp; 1909 struct T_conn_res *tcr; 1910 t_uscalar_t acceptor_id; 1911 t_scalar_t seqnum; 1912 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1913 mblk_t *ok_mp; 1914 mblk_t *mp1; 1915 tcp_stack_t *tcps = listener->tcp_tcps; 1916 1917 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1918 tcp_err_ack(listener, mp, TPROTO, 0); 1919 return; 1920 } 1921 tcr = (struct T_conn_res *)mp->b_rptr; 1922 1923 /* 1924 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1925 * read side queue of the streams device underneath us i.e. the 1926 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1927 * look it up in the queue_hash. Under LP64 it sends down the 1928 * minor_t of the accepting endpoint. 1929 * 1930 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1931 * fanout hash lock is held. 1932 * This prevents any thread from entering the acceptor queue from 1933 * below (since it has not been hard bound yet i.e. any inbound 1934 * packets will arrive on the listener or default tcp queue and 1935 * go through tcp_lookup). 1936 * The CONN_INC_REF will prevent the acceptor from closing. 1937 * 1938 * XXX It is still possible for a tli application to send down data 1939 * on the accepting stream while another thread calls t_accept. 1940 * This should not be a problem for well-behaved applications since 1941 * the T_OK_ACK is sent after the queue swapping is completed. 1942 * 1943 * If the accepting fd is the same as the listening fd, avoid 1944 * queue hash lookup since that will return an eager listener in a 1945 * already established state. 1946 */ 1947 acceptor_id = tcr->ACCEPTOR_id; 1948 mutex_enter(&listener->tcp_eager_lock); 1949 if (listener->tcp_acceptor_id == acceptor_id) { 1950 eager = listener->tcp_eager_next_q; 1951 /* only count how many T_CONN_INDs so don't count q0 */ 1952 if ((listener->tcp_conn_req_cnt_q != 1) || 1953 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1954 mutex_exit(&listener->tcp_eager_lock); 1955 tcp_err_ack(listener, mp, TBADF, 0); 1956 return; 1957 } 1958 if (listener->tcp_conn_req_cnt_q0 != 0) { 1959 /* Throw away all the eagers on q0. */ 1960 tcp_eager_cleanup(listener, 1); 1961 } 1962 if (listener->tcp_syn_defense) { 1963 listener->tcp_syn_defense = B_FALSE; 1964 if (listener->tcp_ip_addr_cache != NULL) { 1965 kmem_free(listener->tcp_ip_addr_cache, 1966 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1967 listener->tcp_ip_addr_cache = NULL; 1968 } 1969 } 1970 /* 1971 * Transfer tcp_conn_req_max to the eager so that when 1972 * a disconnect occurs we can revert the endpoint to the 1973 * listen state. 1974 */ 1975 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1976 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1977 /* 1978 * Get a reference on the acceptor just like the 1979 * tcp_acceptor_hash_lookup below. 1980 */ 1981 acceptor = listener; 1982 CONN_INC_REF(acceptor->tcp_connp); 1983 } else { 1984 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1985 if (acceptor == NULL) { 1986 if (listener->tcp_debug) { 1987 (void) strlog(TCP_MOD_ID, 0, 1, 1988 SL_ERROR|SL_TRACE, 1989 "tcp_accept: did not find acceptor 0x%x\n", 1990 acceptor_id); 1991 } 1992 mutex_exit(&listener->tcp_eager_lock); 1993 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 1994 return; 1995 } 1996 /* 1997 * Verify acceptor state. The acceptable states for an acceptor 1998 * include TCPS_IDLE and TCPS_BOUND. 1999 */ 2000 switch (acceptor->tcp_state) { 2001 case TCPS_IDLE: 2002 /* FALLTHRU */ 2003 case TCPS_BOUND: 2004 break; 2005 default: 2006 CONN_DEC_REF(acceptor->tcp_connp); 2007 mutex_exit(&listener->tcp_eager_lock); 2008 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2009 return; 2010 } 2011 } 2012 2013 /* The listener must be in TCPS_LISTEN */ 2014 if (listener->tcp_state != TCPS_LISTEN) { 2015 CONN_DEC_REF(acceptor->tcp_connp); 2016 mutex_exit(&listener->tcp_eager_lock); 2017 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2018 return; 2019 } 2020 2021 /* 2022 * Rendezvous with an eager connection request packet hanging off 2023 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2024 * tcp structure when the connection packet arrived in 2025 * tcp_conn_request(). 2026 */ 2027 seqnum = tcr->SEQ_number; 2028 eager = listener; 2029 do { 2030 eager = eager->tcp_eager_next_q; 2031 if (eager == NULL) { 2032 CONN_DEC_REF(acceptor->tcp_connp); 2033 mutex_exit(&listener->tcp_eager_lock); 2034 tcp_err_ack(listener, mp, TBADSEQ, 0); 2035 return; 2036 } 2037 } while (eager->tcp_conn_req_seqnum != seqnum); 2038 mutex_exit(&listener->tcp_eager_lock); 2039 2040 /* 2041 * At this point, both acceptor and listener have 2 ref 2042 * that they begin with. Acceptor has one additional ref 2043 * we placed in lookup while listener has 3 additional 2044 * ref for being behind the squeue (tcp_accept() is 2045 * done on listener's squeue); being in classifier hash; 2046 * and eager's ref on listener. 2047 */ 2048 ASSERT(listener->tcp_connp->conn_ref >= 5); 2049 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2050 2051 /* 2052 * The eager at this point is set in its own squeue and 2053 * could easily have been killed (tcp_accept_finish will 2054 * deal with that) because of a TH_RST so we can only 2055 * ASSERT for a single ref. 2056 */ 2057 ASSERT(eager->tcp_connp->conn_ref >= 1); 2058 2059 /* Pre allocate the stroptions mblk also */ 2060 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2061 if (opt_mp == NULL) { 2062 CONN_DEC_REF(acceptor->tcp_connp); 2063 CONN_DEC_REF(eager->tcp_connp); 2064 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2065 return; 2066 } 2067 DB_TYPE(opt_mp) = M_SETOPTS; 2068 opt_mp->b_wptr += sizeof (struct stroptions); 2069 2070 /* 2071 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2072 * from listener to acceptor. The message is chained on opt_mp 2073 * which will be sent onto eager's squeue. 2074 */ 2075 if (listener->tcp_bound_if != 0) { 2076 /* allocate optmgmt req */ 2077 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2078 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2079 sizeof (int)); 2080 if (mp1 != NULL) 2081 linkb(opt_mp, mp1); 2082 } 2083 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2084 uint_t on = 1; 2085 2086 /* allocate optmgmt req */ 2087 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2088 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2089 if (mp1 != NULL) 2090 linkb(opt_mp, mp1); 2091 } 2092 2093 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2094 if ((mp1 = copymsg(mp)) == NULL) { 2095 CONN_DEC_REF(acceptor->tcp_connp); 2096 CONN_DEC_REF(eager->tcp_connp); 2097 freemsg(opt_mp); 2098 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2099 return; 2100 } 2101 2102 tcr = (struct T_conn_res *)mp1->b_rptr; 2103 2104 /* 2105 * This is an expanded version of mi_tpi_ok_ack_alloc() 2106 * which allocates a larger mblk and appends the new 2107 * local address to the ok_ack. The address is copied by 2108 * soaccept() for getsockname(). 2109 */ 2110 { 2111 int extra; 2112 2113 extra = (eager->tcp_family == AF_INET) ? 2114 sizeof (sin_t) : sizeof (sin6_t); 2115 2116 /* 2117 * Try to re-use mp, if possible. Otherwise, allocate 2118 * an mblk and return it as ok_mp. In any case, mp 2119 * is no longer usable upon return. 2120 */ 2121 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2122 CONN_DEC_REF(acceptor->tcp_connp); 2123 CONN_DEC_REF(eager->tcp_connp); 2124 freemsg(opt_mp); 2125 /* Original mp has been freed by now, so use mp1 */ 2126 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2127 return; 2128 } 2129 2130 mp = NULL; /* We should never use mp after this point */ 2131 2132 switch (extra) { 2133 case sizeof (sin_t): { 2134 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2135 2136 ok_mp->b_wptr += extra; 2137 sin->sin_family = AF_INET; 2138 sin->sin_port = eager->tcp_lport; 2139 sin->sin_addr.s_addr = 2140 eager->tcp_ipha->ipha_src; 2141 break; 2142 } 2143 case sizeof (sin6_t): { 2144 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2145 2146 ok_mp->b_wptr += extra; 2147 sin6->sin6_family = AF_INET6; 2148 sin6->sin6_port = eager->tcp_lport; 2149 if (eager->tcp_ipversion == IPV4_VERSION) { 2150 sin6->sin6_flowinfo = 0; 2151 IN6_IPADDR_TO_V4MAPPED( 2152 eager->tcp_ipha->ipha_src, 2153 &sin6->sin6_addr); 2154 } else { 2155 ASSERT(eager->tcp_ip6h != NULL); 2156 sin6->sin6_flowinfo = 2157 eager->tcp_ip6h->ip6_vcf & 2158 ~IPV6_VERS_AND_FLOW_MASK; 2159 sin6->sin6_addr = 2160 eager->tcp_ip6h->ip6_src; 2161 } 2162 break; 2163 } 2164 default: 2165 break; 2166 } 2167 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2168 } 2169 2170 /* 2171 * If there are no options we know that the T_CONN_RES will 2172 * succeed. However, we can't send the T_OK_ACK upstream until 2173 * the tcp_accept_swap is done since it would be dangerous to 2174 * let the application start using the new fd prior to the swap. 2175 */ 2176 tcp_accept_swap(listener, acceptor, eager); 2177 2178 /* 2179 * tcp_accept_swap unlinks eager from listener but does not drop 2180 * the eager's reference on the listener. 2181 */ 2182 ASSERT(eager->tcp_listener == NULL); 2183 ASSERT(listener->tcp_connp->conn_ref >= 5); 2184 2185 /* 2186 * The eager is now associated with its own queue. Insert in 2187 * the hash so that the connection can be reused for a future 2188 * T_CONN_RES. 2189 */ 2190 tcp_acceptor_hash_insert(acceptor_id, eager); 2191 2192 /* 2193 * We now do the processing of options with T_CONN_RES. 2194 * We delay till now since we wanted to have queue to pass to 2195 * option processing routines that points back to the right 2196 * instance structure which does not happen until after 2197 * tcp_accept_swap(). 2198 * 2199 * Note: 2200 * The sanity of the logic here assumes that whatever options 2201 * are appropriate to inherit from listner=>eager are done 2202 * before this point, and whatever were to be overridden (or not) 2203 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2204 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2205 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2206 * This may not be true at this point in time but can be fixed 2207 * independently. This option processing code starts with 2208 * the instantiated acceptor instance and the final queue at 2209 * this point. 2210 */ 2211 2212 if (tcr->OPT_length != 0) { 2213 /* Options to process */ 2214 int t_error = 0; 2215 int sys_error = 0; 2216 int do_disconnect = 0; 2217 2218 if (tcp_conprim_opt_process(eager, mp1, 2219 &do_disconnect, &t_error, &sys_error) < 0) { 2220 eager->tcp_accept_error = 1; 2221 if (do_disconnect) { 2222 /* 2223 * An option failed which does not allow 2224 * connection to be accepted. 2225 * 2226 * We allow T_CONN_RES to succeed and 2227 * put a T_DISCON_IND on the eager queue. 2228 */ 2229 ASSERT(t_error == 0 && sys_error == 0); 2230 eager->tcp_send_discon_ind = 1; 2231 } else { 2232 ASSERT(t_error != 0); 2233 freemsg(ok_mp); 2234 /* 2235 * Original mp was either freed or set 2236 * to ok_mp above, so use mp1 instead. 2237 */ 2238 tcp_err_ack(listener, mp1, t_error, sys_error); 2239 goto finish; 2240 } 2241 } 2242 /* 2243 * Most likely success in setting options (except if 2244 * eager->tcp_send_discon_ind set). 2245 * mp1 option buffer represented by OPT_length/offset 2246 * potentially modified and contains results of setting 2247 * options at this point 2248 */ 2249 } 2250 2251 /* We no longer need mp1, since all options processing has passed */ 2252 freemsg(mp1); 2253 2254 putnext(listener->tcp_rq, ok_mp); 2255 2256 mutex_enter(&listener->tcp_eager_lock); 2257 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2258 tcp_t *tail; 2259 mblk_t *conn_ind; 2260 2261 /* 2262 * This path should not be executed if listener and 2263 * acceptor streams are the same. 2264 */ 2265 ASSERT(listener != acceptor); 2266 2267 tcp = listener->tcp_eager_prev_q0; 2268 /* 2269 * listener->tcp_eager_prev_q0 points to the TAIL of the 2270 * deferred T_conn_ind queue. We need to get to the head of 2271 * the queue in order to send up T_conn_ind the same order as 2272 * how the 3WHS is completed. 2273 */ 2274 while (tcp != listener) { 2275 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2276 break; 2277 else 2278 tcp = tcp->tcp_eager_prev_q0; 2279 } 2280 ASSERT(tcp != listener); 2281 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2282 ASSERT(conn_ind != NULL); 2283 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2284 2285 /* Move from q0 to q */ 2286 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2287 listener->tcp_conn_req_cnt_q0--; 2288 listener->tcp_conn_req_cnt_q++; 2289 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2290 tcp->tcp_eager_prev_q0; 2291 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2292 tcp->tcp_eager_next_q0; 2293 tcp->tcp_eager_prev_q0 = NULL; 2294 tcp->tcp_eager_next_q0 = NULL; 2295 tcp->tcp_conn_def_q0 = B_FALSE; 2296 2297 /* Make sure the tcp isn't in the list of droppables */ 2298 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2299 tcp->tcp_eager_prev_drop_q0 == NULL); 2300 2301 /* 2302 * Insert at end of the queue because sockfs sends 2303 * down T_CONN_RES in chronological order. Leaving 2304 * the older conn indications at front of the queue 2305 * helps reducing search time. 2306 */ 2307 tail = listener->tcp_eager_last_q; 2308 if (tail != NULL) 2309 tail->tcp_eager_next_q = tcp; 2310 else 2311 listener->tcp_eager_next_q = tcp; 2312 listener->tcp_eager_last_q = tcp; 2313 tcp->tcp_eager_next_q = NULL; 2314 mutex_exit(&listener->tcp_eager_lock); 2315 putnext(tcp->tcp_rq, conn_ind); 2316 } else { 2317 mutex_exit(&listener->tcp_eager_lock); 2318 } 2319 2320 /* 2321 * Done with the acceptor - free it 2322 * 2323 * Note: from this point on, no access to listener should be made 2324 * as listener can be equal to acceptor. 2325 */ 2326 finish: 2327 ASSERT(acceptor->tcp_detached); 2328 ASSERT(tcps->tcps_g_q != NULL); 2329 acceptor->tcp_rq = tcps->tcps_g_q; 2330 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2331 (void) tcp_clean_death(acceptor, 0, 2); 2332 CONN_DEC_REF(acceptor->tcp_connp); 2333 2334 /* 2335 * In case we already received a FIN we have to make tcp_rput send 2336 * the ordrel_ind. This will also send up a window update if the window 2337 * has opened up. 2338 * 2339 * In the normal case of a successful connection acceptance 2340 * we give the O_T_BIND_REQ to the read side put procedure as an 2341 * indication that this was just accepted. This tells tcp_rput to 2342 * pass up any data queued in tcp_rcv_list. 2343 * 2344 * In the fringe case where options sent with T_CONN_RES failed and 2345 * we required, we would be indicating a T_DISCON_IND to blow 2346 * away this connection. 2347 */ 2348 2349 /* 2350 * XXX: we currently have a problem if XTI application closes the 2351 * acceptor stream in between. This problem exists in on10-gate also 2352 * and is well know but nothing can be done short of major rewrite 2353 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2354 * eager same squeue as listener (we can distinguish non socket 2355 * listeners at the time of handling a SYN in tcp_conn_request) 2356 * and do most of the work that tcp_accept_finish does here itself 2357 * and then get behind the acceptor squeue to access the acceptor 2358 * queue. 2359 */ 2360 /* 2361 * We already have a ref on tcp so no need to do one before squeue_fill 2362 */ 2363 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2364 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2365 } 2366 2367 /* 2368 * Swap information between the eager and acceptor for a TLI/XTI client. 2369 * The sockfs accept is done on the acceptor stream and control goes 2370 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2371 * called. In either case, both the eager and listener are in their own 2372 * perimeter (squeue) and the code has to deal with potential race. 2373 * 2374 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2375 */ 2376 static void 2377 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2378 { 2379 conn_t *econnp, *aconnp; 2380 2381 ASSERT(eager->tcp_rq == listener->tcp_rq); 2382 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2383 ASSERT(!eager->tcp_hard_bound); 2384 ASSERT(!TCP_IS_SOCKET(acceptor)); 2385 ASSERT(!TCP_IS_SOCKET(eager)); 2386 ASSERT(!TCP_IS_SOCKET(listener)); 2387 2388 acceptor->tcp_detached = B_TRUE; 2389 /* 2390 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2391 * the acceptor id. 2392 */ 2393 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2394 2395 /* remove eager from listen list... */ 2396 mutex_enter(&listener->tcp_eager_lock); 2397 tcp_eager_unlink(eager); 2398 ASSERT(eager->tcp_eager_next_q == NULL && 2399 eager->tcp_eager_last_q == NULL); 2400 ASSERT(eager->tcp_eager_next_q0 == NULL && 2401 eager->tcp_eager_prev_q0 == NULL); 2402 mutex_exit(&listener->tcp_eager_lock); 2403 eager->tcp_rq = acceptor->tcp_rq; 2404 eager->tcp_wq = acceptor->tcp_wq; 2405 2406 econnp = eager->tcp_connp; 2407 aconnp = acceptor->tcp_connp; 2408 2409 eager->tcp_rq->q_ptr = econnp; 2410 eager->tcp_wq->q_ptr = econnp; 2411 2412 /* 2413 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2414 * which might be a different squeue from our peer TCP instance. 2415 * For TCP Fusion, the peer expects that whenever tcp_detached is 2416 * clear, our TCP queues point to the acceptor's queues. Thus, use 2417 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2418 * above reach global visibility prior to the clearing of tcp_detached. 2419 */ 2420 membar_producer(); 2421 eager->tcp_detached = B_FALSE; 2422 2423 ASSERT(eager->tcp_ack_tid == 0); 2424 2425 econnp->conn_dev = aconnp->conn_dev; 2426 if (eager->tcp_cred != NULL) 2427 crfree(eager->tcp_cred); 2428 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2429 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2430 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2431 2432 aconnp->conn_cred = NULL; 2433 2434 econnp->conn_zoneid = aconnp->conn_zoneid; 2435 econnp->conn_allzones = aconnp->conn_allzones; 2436 2437 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2438 aconnp->conn_mac_exempt = B_FALSE; 2439 2440 ASSERT(aconnp->conn_peercred == NULL); 2441 2442 /* Do the IPC initialization */ 2443 CONN_INC_REF(econnp); 2444 2445 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2446 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2447 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2448 econnp->conn_ulp = aconnp->conn_ulp; 2449 2450 /* Done with old IPC. Drop its ref on its connp */ 2451 CONN_DEC_REF(aconnp); 2452 } 2453 2454 2455 /* 2456 * Adapt to the information, such as rtt and rtt_sd, provided from the 2457 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2458 * 2459 * Checks for multicast and broadcast destination address. 2460 * Returns zero on failure; non-zero if ok. 2461 * 2462 * Note that the MSS calculation here is based on the info given in 2463 * the IRE. We do not do any calculation based on TCP options. They 2464 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2465 * knows which options to use. 2466 * 2467 * Note on how TCP gets its parameters for a connection. 2468 * 2469 * When a tcp_t structure is allocated, it gets all the default parameters. 2470 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2471 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2472 * default. But if there is an associated tcp_host_param, it will override 2473 * the metrics. 2474 * 2475 * An incoming SYN with a multicast or broadcast destination address, is dropped 2476 * in 1 of 2 places. 2477 * 2478 * 1. If the packet was received over the wire it is dropped in 2479 * ip_rput_process_broadcast() 2480 * 2481 * 2. If the packet was received through internal IP loopback, i.e. the packet 2482 * was generated and received on the same machine, it is dropped in 2483 * ip_wput_local() 2484 * 2485 * An incoming SYN with a multicast or broadcast source address is always 2486 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2487 * reject an attempt to connect to a broadcast or multicast (destination) 2488 * address. 2489 */ 2490 static int 2491 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2492 { 2493 tcp_hsp_t *hsp; 2494 ire_t *ire; 2495 ire_t *sire = NULL; 2496 iulp_t *ire_uinfo = NULL; 2497 uint32_t mss_max; 2498 uint32_t mss; 2499 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2500 conn_t *connp = tcp->tcp_connp; 2501 boolean_t ire_cacheable = B_FALSE; 2502 zoneid_t zoneid = connp->conn_zoneid; 2503 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2504 MATCH_IRE_SECATTR; 2505 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2506 ill_t *ill = NULL; 2507 boolean_t incoming = (ire_mp == NULL); 2508 tcp_stack_t *tcps = tcp->tcp_tcps; 2509 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2510 2511 ASSERT(connp->conn_ire_cache == NULL); 2512 2513 if (tcp->tcp_ipversion == IPV4_VERSION) { 2514 2515 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2516 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2517 return (0); 2518 } 2519 /* 2520 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2521 * for the destination with the nexthop as gateway. 2522 * ire_ctable_lookup() is used because this particular 2523 * ire, if it exists, will be marked private. 2524 * If that is not available, use the interface ire 2525 * for the nexthop. 2526 * 2527 * TSol: tcp_update_label will detect label mismatches based 2528 * only on the destination's label, but that would not 2529 * detect label mismatches based on the security attributes 2530 * of routes or next hop gateway. Hence we need to pass the 2531 * label to ire_ftable_lookup below in order to locate the 2532 * right prefix (and/or) ire cache. Similarly we also need 2533 * pass the label to the ire_cache_lookup below to locate 2534 * the right ire that also matches on the label. 2535 */ 2536 if (tcp->tcp_connp->conn_nexthop_set) { 2537 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2538 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2539 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2540 ipst); 2541 if (ire == NULL) { 2542 ire = ire_ftable_lookup( 2543 tcp->tcp_connp->conn_nexthop_v4, 2544 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2545 tsl, match_flags, ipst); 2546 if (ire == NULL) 2547 return (0); 2548 } else { 2549 ire_uinfo = &ire->ire_uinfo; 2550 } 2551 } else { 2552 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2553 zoneid, tsl, ipst); 2554 if (ire != NULL) { 2555 ire_cacheable = B_TRUE; 2556 ire_uinfo = (ire_mp != NULL) ? 2557 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2558 &ire->ire_uinfo; 2559 2560 } else { 2561 if (ire_mp == NULL) { 2562 ire = ire_ftable_lookup( 2563 tcp->tcp_connp->conn_rem, 2564 0, 0, 0, NULL, &sire, zoneid, 0, 2565 tsl, (MATCH_IRE_RECURSIVE | 2566 MATCH_IRE_DEFAULT), ipst); 2567 if (ire == NULL) 2568 return (0); 2569 ire_uinfo = (sire != NULL) ? 2570 &sire->ire_uinfo : 2571 &ire->ire_uinfo; 2572 } else { 2573 ire = (ire_t *)ire_mp->b_rptr; 2574 ire_uinfo = 2575 &((ire_t *) 2576 ire_mp->b_rptr)->ire_uinfo; 2577 } 2578 } 2579 } 2580 ASSERT(ire != NULL); 2581 2582 if ((ire->ire_src_addr == INADDR_ANY) || 2583 (ire->ire_type & IRE_BROADCAST)) { 2584 /* 2585 * ire->ire_mp is non null when ire_mp passed in is used 2586 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2587 */ 2588 if (ire->ire_mp == NULL) 2589 ire_refrele(ire); 2590 if (sire != NULL) 2591 ire_refrele(sire); 2592 return (0); 2593 } 2594 2595 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2596 ipaddr_t src_addr; 2597 2598 /* 2599 * ip_bind_connected() has stored the correct source 2600 * address in conn_src. 2601 */ 2602 src_addr = tcp->tcp_connp->conn_src; 2603 tcp->tcp_ipha->ipha_src = src_addr; 2604 /* 2605 * Copy of the src addr. in tcp_t is needed 2606 * for the lookup funcs. 2607 */ 2608 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2609 } 2610 /* 2611 * Set the fragment bit so that IP will tell us if the MTU 2612 * should change. IP tells us the latest setting of 2613 * ip_path_mtu_discovery through ire_frag_flag. 2614 */ 2615 if (ipst->ips_ip_path_mtu_discovery) { 2616 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2617 htons(IPH_DF); 2618 } 2619 /* 2620 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2621 * for IP_NEXTHOP. No cache ire has been found for the 2622 * destination and we are working with the nexthop's 2623 * interface ire. Since we need to forward all packets 2624 * to the nexthop first, we "blindly" set tcp_localnet 2625 * to false, eventhough the destination may also be 2626 * onlink. 2627 */ 2628 if (ire_uinfo == NULL) 2629 tcp->tcp_localnet = 0; 2630 else 2631 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2632 } else { 2633 /* 2634 * For incoming connection ire_mp = NULL 2635 * For outgoing connection ire_mp != NULL 2636 * Technically we should check conn_incoming_ill 2637 * when ire_mp is NULL and conn_outgoing_ill when 2638 * ire_mp is non-NULL. But this is performance 2639 * critical path and for IPV*_BOUND_IF, outgoing 2640 * and incoming ill are always set to the same value. 2641 */ 2642 ill_t *dst_ill = NULL; 2643 ipif_t *dst_ipif = NULL; 2644 2645 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2646 2647 if (connp->conn_outgoing_ill != NULL) { 2648 /* Outgoing or incoming path */ 2649 int err; 2650 2651 dst_ill = conn_get_held_ill(connp, 2652 &connp->conn_outgoing_ill, &err); 2653 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2654 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2655 return (0); 2656 } 2657 match_flags |= MATCH_IRE_ILL; 2658 dst_ipif = dst_ill->ill_ipif; 2659 } 2660 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2661 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2662 2663 if (ire != NULL) { 2664 ire_cacheable = B_TRUE; 2665 ire_uinfo = (ire_mp != NULL) ? 2666 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2667 &ire->ire_uinfo; 2668 } else { 2669 if (ire_mp == NULL) { 2670 ire = ire_ftable_lookup_v6( 2671 &tcp->tcp_connp->conn_remv6, 2672 0, 0, 0, dst_ipif, &sire, zoneid, 2673 0, tsl, match_flags, ipst); 2674 if (ire == NULL) { 2675 if (dst_ill != NULL) 2676 ill_refrele(dst_ill); 2677 return (0); 2678 } 2679 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2680 &ire->ire_uinfo; 2681 } else { 2682 ire = (ire_t *)ire_mp->b_rptr; 2683 ire_uinfo = 2684 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2685 } 2686 } 2687 if (dst_ill != NULL) 2688 ill_refrele(dst_ill); 2689 2690 ASSERT(ire != NULL); 2691 ASSERT(ire_uinfo != NULL); 2692 2693 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2694 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2695 /* 2696 * ire->ire_mp is non null when ire_mp passed in is used 2697 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2698 */ 2699 if (ire->ire_mp == NULL) 2700 ire_refrele(ire); 2701 if (sire != NULL) 2702 ire_refrele(sire); 2703 return (0); 2704 } 2705 2706 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2707 in6_addr_t src_addr; 2708 2709 /* 2710 * ip_bind_connected_v6() has stored the correct source 2711 * address per IPv6 addr. selection policy in 2712 * conn_src_v6. 2713 */ 2714 src_addr = tcp->tcp_connp->conn_srcv6; 2715 2716 tcp->tcp_ip6h->ip6_src = src_addr; 2717 /* 2718 * Copy of the src addr. in tcp_t is needed 2719 * for the lookup funcs. 2720 */ 2721 tcp->tcp_ip_src_v6 = src_addr; 2722 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2723 &connp->conn_srcv6)); 2724 } 2725 tcp->tcp_localnet = 2726 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2727 } 2728 2729 /* 2730 * This allows applications to fail quickly when connections are made 2731 * to dead hosts. Hosts can be labeled dead by adding a reject route 2732 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2733 */ 2734 if ((ire->ire_flags & RTF_REJECT) && 2735 (ire->ire_flags & RTF_PRIVATE)) 2736 goto error; 2737 2738 /* 2739 * Make use of the cached rtt and rtt_sd values to calculate the 2740 * initial RTO. Note that they are already initialized in 2741 * tcp_init_values(). 2742 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2743 * IP_NEXTHOP, but instead are using the interface ire for the 2744 * nexthop, then we do not use the ire_uinfo from that ire to 2745 * do any initializations. 2746 */ 2747 if (ire_uinfo != NULL) { 2748 if (ire_uinfo->iulp_rtt != 0) { 2749 clock_t rto; 2750 2751 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2752 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2753 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2754 tcps->tcps_rexmit_interval_extra + 2755 (tcp->tcp_rtt_sa >> 5); 2756 2757 if (rto > tcps->tcps_rexmit_interval_max) { 2758 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2759 } else if (rto < tcps->tcps_rexmit_interval_min) { 2760 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2761 } else { 2762 tcp->tcp_rto = rto; 2763 } 2764 } 2765 if (ire_uinfo->iulp_ssthresh != 0) 2766 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2767 else 2768 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2769 if (ire_uinfo->iulp_spipe > 0) { 2770 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2771 tcps->tcps_max_buf); 2772 if (tcps->tcps_snd_lowat_fraction != 0) 2773 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2774 tcps->tcps_snd_lowat_fraction; 2775 (void) tcp_maxpsz_set(tcp, B_TRUE); 2776 } 2777 /* 2778 * Note that up till now, acceptor always inherits receive 2779 * window from the listener. But if there is a metrics 2780 * associated with a host, we should use that instead of 2781 * inheriting it from listener. Thus we need to pass this 2782 * info back to the caller. 2783 */ 2784 if (ire_uinfo->iulp_rpipe > 0) { 2785 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2786 tcps->tcps_max_buf); 2787 } 2788 2789 if (ire_uinfo->iulp_rtomax > 0) { 2790 tcp->tcp_second_timer_threshold = 2791 ire_uinfo->iulp_rtomax; 2792 } 2793 2794 /* 2795 * Use the metric option settings, iulp_tstamp_ok and 2796 * iulp_wscale_ok, only for active open. What this means 2797 * is that if the other side uses timestamp or window 2798 * scale option, TCP will also use those options. That 2799 * is for passive open. If the application sets a 2800 * large window, window scale is enabled regardless of 2801 * the value in iulp_wscale_ok. This is the behavior 2802 * since 2.6. So we keep it. 2803 * The only case left in passive open processing is the 2804 * check for SACK. 2805 * For ECN, it should probably be like SACK. But the 2806 * current value is binary, so we treat it like the other 2807 * cases. The metric only controls active open.For passive 2808 * open, the ndd param, tcp_ecn_permitted, controls the 2809 * behavior. 2810 */ 2811 if (!tcp_detached) { 2812 /* 2813 * The if check means that the following can only 2814 * be turned on by the metrics only IRE, but not off. 2815 */ 2816 if (ire_uinfo->iulp_tstamp_ok) 2817 tcp->tcp_snd_ts_ok = B_TRUE; 2818 if (ire_uinfo->iulp_wscale_ok) 2819 tcp->tcp_snd_ws_ok = B_TRUE; 2820 if (ire_uinfo->iulp_sack == 2) 2821 tcp->tcp_snd_sack_ok = B_TRUE; 2822 if (ire_uinfo->iulp_ecn_ok) 2823 tcp->tcp_ecn_ok = B_TRUE; 2824 } else { 2825 /* 2826 * Passive open. 2827 * 2828 * As above, the if check means that SACK can only be 2829 * turned on by the metric only IRE. 2830 */ 2831 if (ire_uinfo->iulp_sack > 0) { 2832 tcp->tcp_snd_sack_ok = B_TRUE; 2833 } 2834 } 2835 } 2836 2837 2838 /* 2839 * XXX: Note that currently, ire_max_frag can be as small as 68 2840 * because of PMTUd. So tcp_mss may go to negative if combined 2841 * length of all those options exceeds 28 bytes. But because 2842 * of the tcp_mss_min check below, we may not have a problem if 2843 * tcp_mss_min is of a reasonable value. The default is 1 so 2844 * the negative problem still exists. And the check defeats PMTUd. 2845 * In fact, if PMTUd finds that the MSS should be smaller than 2846 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2847 * value. 2848 * 2849 * We do not deal with that now. All those problems related to 2850 * PMTUd will be fixed later. 2851 */ 2852 ASSERT(ire->ire_max_frag != 0); 2853 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2854 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2855 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2856 mss = MIN(mss, IPV6_MIN_MTU); 2857 } 2858 } 2859 2860 /* Sanity check for MSS value. */ 2861 if (tcp->tcp_ipversion == IPV4_VERSION) 2862 mss_max = tcps->tcps_mss_max_ipv4; 2863 else 2864 mss_max = tcps->tcps_mss_max_ipv6; 2865 2866 if (tcp->tcp_ipversion == IPV6_VERSION && 2867 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2868 /* 2869 * After receiving an ICMPv6 "packet too big" message with a 2870 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2871 * will insert a 8-byte fragment header in every packet; we 2872 * reduce the MSS by that amount here. 2873 */ 2874 mss -= sizeof (ip6_frag_t); 2875 } 2876 2877 if (tcp->tcp_ipsec_overhead == 0) 2878 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2879 2880 mss -= tcp->tcp_ipsec_overhead; 2881 2882 if (mss < tcps->tcps_mss_min) 2883 mss = tcps->tcps_mss_min; 2884 if (mss > mss_max) 2885 mss = mss_max; 2886 2887 /* Note that this is the maximum MSS, excluding all options. */ 2888 tcp->tcp_mss = mss; 2889 2890 /* 2891 * Initialize the ISS here now that we have the full connection ID. 2892 * The RFC 1948 method of initial sequence number generation requires 2893 * knowledge of the full connection ID before setting the ISS. 2894 */ 2895 2896 tcp_iss_init(tcp); 2897 2898 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2899 tcp->tcp_loopback = B_TRUE; 2900 2901 if (tcp->tcp_ipversion == IPV4_VERSION) { 2902 hsp = tcp_hsp_lookup(tcp->tcp_remote, tcps); 2903 } else { 2904 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6, tcps); 2905 } 2906 2907 if (hsp != NULL) { 2908 /* Only modify if we're going to make them bigger */ 2909 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2910 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2911 if (tcps->tcps_snd_lowat_fraction != 0) 2912 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2913 tcps->tcps_snd_lowat_fraction; 2914 } 2915 2916 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2917 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2918 } 2919 2920 /* Copy timestamp flag only for active open */ 2921 if (!tcp_detached) 2922 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2923 } 2924 2925 if (sire != NULL) 2926 IRE_REFRELE(sire); 2927 2928 /* 2929 * If we got an IRE_CACHE and an ILL, go through their properties; 2930 * otherwise, this is deferred until later when we have an IRE_CACHE. 2931 */ 2932 if (tcp->tcp_loopback || 2933 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2934 /* 2935 * For incoming, see if this tcp may be MDT-capable. For 2936 * outgoing, this process has been taken care of through 2937 * tcp_rput_other. 2938 */ 2939 tcp_ire_ill_check(tcp, ire, ill, incoming); 2940 tcp->tcp_ire_ill_check_done = B_TRUE; 2941 } 2942 2943 mutex_enter(&connp->conn_lock); 2944 /* 2945 * Make sure that conn is not marked incipient 2946 * for incoming connections. A blind 2947 * removal of incipient flag is cheaper than 2948 * check and removal. 2949 */ 2950 connp->conn_state_flags &= ~CONN_INCIPIENT; 2951 2952 /* 2953 * Must not cache forwarding table routes 2954 * or recache an IRE after the conn_t has 2955 * had conn_ire_cache cleared and is flagged 2956 * unusable, (see the CONN_CACHE_IRE() macro). 2957 */ 2958 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2959 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2960 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2961 connp->conn_ire_cache = ire; 2962 IRE_UNTRACE_REF(ire); 2963 rw_exit(&ire->ire_bucket->irb_lock); 2964 mutex_exit(&connp->conn_lock); 2965 return (1); 2966 } 2967 rw_exit(&ire->ire_bucket->irb_lock); 2968 } 2969 mutex_exit(&connp->conn_lock); 2970 2971 if (ire->ire_mp == NULL) 2972 ire_refrele(ire); 2973 return (1); 2974 2975 error: 2976 if (ire->ire_mp == NULL) 2977 ire_refrele(ire); 2978 if (sire != NULL) 2979 ire_refrele(sire); 2980 return (0); 2981 } 2982 2983 /* 2984 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 2985 * O_T_BIND_REQ/T_BIND_REQ message. 2986 */ 2987 static void 2988 tcp_bind(tcp_t *tcp, mblk_t *mp) 2989 { 2990 sin_t *sin; 2991 sin6_t *sin6; 2992 mblk_t *mp1; 2993 in_port_t requested_port; 2994 in_port_t allocated_port; 2995 struct T_bind_req *tbr; 2996 boolean_t bind_to_req_port_only; 2997 boolean_t backlog_update = B_FALSE; 2998 boolean_t user_specified; 2999 in6_addr_t v6addr; 3000 ipaddr_t v4addr; 3001 uint_t origipversion; 3002 int err; 3003 queue_t *q = tcp->tcp_wq; 3004 conn_t *connp; 3005 mlp_type_t addrtype, mlptype; 3006 zone_t *zone; 3007 cred_t *cr; 3008 in_port_t mlp_port; 3009 tcp_stack_t *tcps = tcp->tcp_tcps; 3010 3011 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3012 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3013 if (tcp->tcp_debug) { 3014 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3015 "tcp_bind: bad req, len %u", 3016 (uint_t)(mp->b_wptr - mp->b_rptr)); 3017 } 3018 tcp_err_ack(tcp, mp, TPROTO, 0); 3019 return; 3020 } 3021 /* Make sure the largest address fits */ 3022 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3023 if (mp1 == NULL) { 3024 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3025 return; 3026 } 3027 mp = mp1; 3028 tbr = (struct T_bind_req *)mp->b_rptr; 3029 if (tcp->tcp_state >= TCPS_BOUND) { 3030 if ((tcp->tcp_state == TCPS_BOUND || 3031 tcp->tcp_state == TCPS_LISTEN) && 3032 tcp->tcp_conn_req_max != tbr->CONIND_number && 3033 tbr->CONIND_number > 0) { 3034 /* 3035 * Handle listen() increasing CONIND_number. 3036 * This is more "liberal" then what the TPI spec 3037 * requires but is needed to avoid a t_unbind 3038 * when handling listen() since the port number 3039 * might be "stolen" between the unbind and bind. 3040 */ 3041 backlog_update = B_TRUE; 3042 goto do_bind; 3043 } 3044 if (tcp->tcp_debug) { 3045 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3046 "tcp_bind: bad state, %d", tcp->tcp_state); 3047 } 3048 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3049 return; 3050 } 3051 origipversion = tcp->tcp_ipversion; 3052 3053 switch (tbr->ADDR_length) { 3054 case 0: /* request for a generic port */ 3055 tbr->ADDR_offset = sizeof (struct T_bind_req); 3056 if (tcp->tcp_family == AF_INET) { 3057 tbr->ADDR_length = sizeof (sin_t); 3058 sin = (sin_t *)&tbr[1]; 3059 *sin = sin_null; 3060 sin->sin_family = AF_INET; 3061 mp->b_wptr = (uchar_t *)&sin[1]; 3062 tcp->tcp_ipversion = IPV4_VERSION; 3063 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3064 } else { 3065 ASSERT(tcp->tcp_family == AF_INET6); 3066 tbr->ADDR_length = sizeof (sin6_t); 3067 sin6 = (sin6_t *)&tbr[1]; 3068 *sin6 = sin6_null; 3069 sin6->sin6_family = AF_INET6; 3070 mp->b_wptr = (uchar_t *)&sin6[1]; 3071 tcp->tcp_ipversion = IPV6_VERSION; 3072 V6_SET_ZERO(v6addr); 3073 } 3074 requested_port = 0; 3075 break; 3076 3077 case sizeof (sin_t): /* Complete IPv4 address */ 3078 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3079 sizeof (sin_t)); 3080 if (sin == NULL || !OK_32PTR((char *)sin)) { 3081 if (tcp->tcp_debug) { 3082 (void) strlog(TCP_MOD_ID, 0, 1, 3083 SL_ERROR|SL_TRACE, 3084 "tcp_bind: bad address parameter, " 3085 "offset %d, len %d", 3086 tbr->ADDR_offset, tbr->ADDR_length); 3087 } 3088 tcp_err_ack(tcp, mp, TPROTO, 0); 3089 return; 3090 } 3091 /* 3092 * With sockets sockfs will accept bogus sin_family in 3093 * bind() and replace it with the family used in the socket 3094 * call. 3095 */ 3096 if (sin->sin_family != AF_INET || 3097 tcp->tcp_family != AF_INET) { 3098 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3099 return; 3100 } 3101 requested_port = ntohs(sin->sin_port); 3102 tcp->tcp_ipversion = IPV4_VERSION; 3103 v4addr = sin->sin_addr.s_addr; 3104 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3105 break; 3106 3107 case sizeof (sin6_t): /* Complete IPv6 address */ 3108 sin6 = (sin6_t *)mi_offset_param(mp, 3109 tbr->ADDR_offset, sizeof (sin6_t)); 3110 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3111 if (tcp->tcp_debug) { 3112 (void) strlog(TCP_MOD_ID, 0, 1, 3113 SL_ERROR|SL_TRACE, 3114 "tcp_bind: bad IPv6 address parameter, " 3115 "offset %d, len %d", tbr->ADDR_offset, 3116 tbr->ADDR_length); 3117 } 3118 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3119 return; 3120 } 3121 if (sin6->sin6_family != AF_INET6 || 3122 tcp->tcp_family != AF_INET6) { 3123 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3124 return; 3125 } 3126 requested_port = ntohs(sin6->sin6_port); 3127 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3128 IPV4_VERSION : IPV6_VERSION; 3129 v6addr = sin6->sin6_addr; 3130 break; 3131 3132 default: 3133 if (tcp->tcp_debug) { 3134 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3135 "tcp_bind: bad address length, %d", 3136 tbr->ADDR_length); 3137 } 3138 tcp_err_ack(tcp, mp, TBADADDR, 0); 3139 return; 3140 } 3141 tcp->tcp_bound_source_v6 = v6addr; 3142 3143 /* Check for change in ipversion */ 3144 if (origipversion != tcp->tcp_ipversion) { 3145 ASSERT(tcp->tcp_family == AF_INET6); 3146 err = tcp->tcp_ipversion == IPV6_VERSION ? 3147 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3148 if (err) { 3149 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3150 return; 3151 } 3152 } 3153 3154 /* 3155 * Initialize family specific fields. Copy of the src addr. 3156 * in tcp_t is needed for the lookup funcs. 3157 */ 3158 if (tcp->tcp_ipversion == IPV6_VERSION) { 3159 tcp->tcp_ip6h->ip6_src = v6addr; 3160 } else { 3161 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3162 } 3163 tcp->tcp_ip_src_v6 = v6addr; 3164 3165 /* 3166 * For O_T_BIND_REQ: 3167 * Verify that the target port/addr is available, or choose 3168 * another. 3169 * For T_BIND_REQ: 3170 * Verify that the target port/addr is available or fail. 3171 * In both cases when it succeeds the tcp is inserted in the 3172 * bind hash table. This ensures that the operation is atomic 3173 * under the lock on the hash bucket. 3174 */ 3175 bind_to_req_port_only = requested_port != 0 && 3176 tbr->PRIM_type != O_T_BIND_REQ; 3177 /* 3178 * Get a valid port (within the anonymous range and should not 3179 * be a privileged one) to use if the user has not given a port. 3180 * If multiple threads are here, they may all start with 3181 * with the same initial port. But, it should be fine as long as 3182 * tcp_bindi will ensure that no two threads will be assigned 3183 * the same port. 3184 * 3185 * NOTE: XXX If a privileged process asks for an anonymous port, we 3186 * still check for ports only in the range > tcp_smallest_non_priv_port, 3187 * unless TCP_ANONPRIVBIND option is set. 3188 */ 3189 mlptype = mlptSingle; 3190 mlp_port = requested_port; 3191 if (requested_port == 0) { 3192 requested_port = tcp->tcp_anon_priv_bind ? 3193 tcp_get_next_priv_port(tcp) : 3194 tcp_update_next_port(tcps->tcps_next_port_to_try, 3195 tcp, B_TRUE); 3196 if (requested_port == 0) { 3197 tcp_err_ack(tcp, mp, TNOADDR, 0); 3198 return; 3199 } 3200 user_specified = B_FALSE; 3201 3202 /* 3203 * If the user went through one of the RPC interfaces to create 3204 * this socket and RPC is MLP in this zone, then give him an 3205 * anonymous MLP. 3206 */ 3207 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3208 connp = tcp->tcp_connp; 3209 if (connp->conn_anon_mlp && is_system_labeled()) { 3210 zone = crgetzone(cr); 3211 addrtype = tsol_mlp_addr_type(zone->zone_id, 3212 IPV6_VERSION, &v6addr, 3213 tcps->tcps_netstack->netstack_ip); 3214 if (addrtype == mlptSingle) { 3215 tcp_err_ack(tcp, mp, TNOADDR, 0); 3216 return; 3217 } 3218 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3219 PMAPPORT, addrtype); 3220 mlp_port = PMAPPORT; 3221 } 3222 } else { 3223 int i; 3224 boolean_t priv = B_FALSE; 3225 3226 /* 3227 * If the requested_port is in the well-known privileged range, 3228 * verify that the stream was opened by a privileged user. 3229 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3230 * but instead the code relies on: 3231 * - the fact that the address of the array and its size never 3232 * changes 3233 * - the atomic assignment of the elements of the array 3234 */ 3235 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3236 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 3237 priv = B_TRUE; 3238 } else { 3239 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 3240 if (requested_port == 3241 tcps->tcps_g_epriv_ports[i]) { 3242 priv = B_TRUE; 3243 break; 3244 } 3245 } 3246 } 3247 if (priv) { 3248 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3249 if (tcp->tcp_debug) { 3250 (void) strlog(TCP_MOD_ID, 0, 1, 3251 SL_ERROR|SL_TRACE, 3252 "tcp_bind: no priv for port %d", 3253 requested_port); 3254 } 3255 tcp_err_ack(tcp, mp, TACCES, 0); 3256 return; 3257 } 3258 } 3259 user_specified = B_TRUE; 3260 3261 connp = tcp->tcp_connp; 3262 if (is_system_labeled()) { 3263 zone = crgetzone(cr); 3264 addrtype = tsol_mlp_addr_type(zone->zone_id, 3265 IPV6_VERSION, &v6addr, 3266 tcps->tcps_netstack->netstack_ip); 3267 if (addrtype == mlptSingle) { 3268 tcp_err_ack(tcp, mp, TNOADDR, 0); 3269 return; 3270 } 3271 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3272 requested_port, addrtype); 3273 } 3274 } 3275 3276 if (mlptype != mlptSingle) { 3277 if (secpolicy_net_bindmlp(cr) != 0) { 3278 if (tcp->tcp_debug) { 3279 (void) strlog(TCP_MOD_ID, 0, 1, 3280 SL_ERROR|SL_TRACE, 3281 "tcp_bind: no priv for multilevel port %d", 3282 requested_port); 3283 } 3284 tcp_err_ack(tcp, mp, TACCES, 0); 3285 return; 3286 } 3287 3288 /* 3289 * If we're specifically binding a shared IP address and the 3290 * port is MLP on shared addresses, then check to see if this 3291 * zone actually owns the MLP. Reject if not. 3292 */ 3293 if (mlptype == mlptShared && addrtype == mlptShared) { 3294 /* 3295 * No need to handle exclusive-stack zones since 3296 * ALL_ZONES only applies to the shared stack. 3297 */ 3298 zoneid_t mlpzone; 3299 3300 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3301 htons(mlp_port)); 3302 if (connp->conn_zoneid != mlpzone) { 3303 if (tcp->tcp_debug) { 3304 (void) strlog(TCP_MOD_ID, 0, 1, 3305 SL_ERROR|SL_TRACE, 3306 "tcp_bind: attempt to bind port " 3307 "%d on shared addr in zone %d " 3308 "(should be %d)", 3309 mlp_port, connp->conn_zoneid, 3310 mlpzone); 3311 } 3312 tcp_err_ack(tcp, mp, TACCES, 0); 3313 return; 3314 } 3315 } 3316 3317 if (!user_specified) { 3318 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3319 requested_port, B_TRUE); 3320 if (err != 0) { 3321 if (tcp->tcp_debug) { 3322 (void) strlog(TCP_MOD_ID, 0, 1, 3323 SL_ERROR|SL_TRACE, 3324 "tcp_bind: cannot establish anon " 3325 "MLP for port %d", 3326 requested_port); 3327 } 3328 tcp_err_ack(tcp, mp, TSYSERR, err); 3329 return; 3330 } 3331 connp->conn_anon_port = B_TRUE; 3332 } 3333 connp->conn_mlp_type = mlptype; 3334 } 3335 3336 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3337 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3338 3339 if (allocated_port == 0) { 3340 connp->conn_mlp_type = mlptSingle; 3341 if (connp->conn_anon_port) { 3342 connp->conn_anon_port = B_FALSE; 3343 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3344 requested_port, B_FALSE); 3345 } 3346 if (bind_to_req_port_only) { 3347 if (tcp->tcp_debug) { 3348 (void) strlog(TCP_MOD_ID, 0, 1, 3349 SL_ERROR|SL_TRACE, 3350 "tcp_bind: requested addr busy"); 3351 } 3352 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3353 } else { 3354 /* If we are out of ports, fail the bind. */ 3355 if (tcp->tcp_debug) { 3356 (void) strlog(TCP_MOD_ID, 0, 1, 3357 SL_ERROR|SL_TRACE, 3358 "tcp_bind: out of ports?"); 3359 } 3360 tcp_err_ack(tcp, mp, TNOADDR, 0); 3361 } 3362 return; 3363 } 3364 ASSERT(tcp->tcp_state == TCPS_BOUND); 3365 do_bind: 3366 if (!backlog_update) { 3367 if (tcp->tcp_family == AF_INET) 3368 sin->sin_port = htons(allocated_port); 3369 else 3370 sin6->sin6_port = htons(allocated_port); 3371 } 3372 if (tcp->tcp_family == AF_INET) { 3373 if (tbr->CONIND_number != 0) { 3374 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3375 sizeof (sin_t)); 3376 } else { 3377 /* Just verify the local IP address */ 3378 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3379 } 3380 } else { 3381 if (tbr->CONIND_number != 0) { 3382 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3383 sizeof (sin6_t)); 3384 } else { 3385 /* Just verify the local IP address */ 3386 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3387 IPV6_ADDR_LEN); 3388 } 3389 } 3390 if (mp1 == NULL) { 3391 if (connp->conn_anon_port) { 3392 connp->conn_anon_port = B_FALSE; 3393 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3394 requested_port, B_FALSE); 3395 } 3396 connp->conn_mlp_type = mlptSingle; 3397 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3398 return; 3399 } 3400 3401 tbr->PRIM_type = T_BIND_ACK; 3402 mp->b_datap->db_type = M_PCPROTO; 3403 3404 /* Chain in the reply mp for tcp_rput() */ 3405 mp1->b_cont = mp; 3406 mp = mp1; 3407 3408 tcp->tcp_conn_req_max = tbr->CONIND_number; 3409 if (tcp->tcp_conn_req_max) { 3410 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 3411 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 3412 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 3413 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 3414 /* 3415 * If this is a listener, do not reset the eager list 3416 * and other stuffs. Note that we don't check if the 3417 * existing eager list meets the new tcp_conn_req_max 3418 * requirement. 3419 */ 3420 if (tcp->tcp_state != TCPS_LISTEN) { 3421 tcp->tcp_state = TCPS_LISTEN; 3422 /* Initialize the chain. Don't need the eager_lock */ 3423 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3424 tcp->tcp_eager_next_drop_q0 = tcp; 3425 tcp->tcp_eager_prev_drop_q0 = tcp; 3426 tcp->tcp_second_ctimer_threshold = 3427 tcps->tcps_ip_abort_linterval; 3428 } 3429 } 3430 3431 /* 3432 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3433 * processing continues in tcp_rput_other(). 3434 */ 3435 if (tcp->tcp_family == AF_INET6) { 3436 ASSERT(tcp->tcp_connp->conn_af_isv6); 3437 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3438 } else { 3439 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3440 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3441 } 3442 /* 3443 * If the bind cannot complete immediately 3444 * IP will arrange to call tcp_rput_other 3445 * when the bind completes. 3446 */ 3447 if (mp != NULL) { 3448 tcp_rput_other(tcp, mp); 3449 } else { 3450 /* 3451 * Bind will be resumed later. Need to ensure 3452 * that conn doesn't disappear when that happens. 3453 * This will be decremented in ip_resume_tcp_bind(). 3454 */ 3455 CONN_INC_REF(tcp->tcp_connp); 3456 } 3457 } 3458 3459 3460 /* 3461 * If the "bind_to_req_port_only" parameter is set, if the requested port 3462 * number is available, return it, If not return 0 3463 * 3464 * If "bind_to_req_port_only" parameter is not set and 3465 * If the requested port number is available, return it. If not, return 3466 * the first anonymous port we happen across. If no anonymous ports are 3467 * available, return 0. addr is the requested local address, if any. 3468 * 3469 * In either case, when succeeding update the tcp_t to record the port number 3470 * and insert it in the bind hash table. 3471 * 3472 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3473 * without setting SO_REUSEADDR. This is needed so that they 3474 * can be viewed as two independent transport protocols. 3475 */ 3476 static in_port_t 3477 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3478 int reuseaddr, boolean_t quick_connect, 3479 boolean_t bind_to_req_port_only, boolean_t user_specified) 3480 { 3481 /* number of times we have run around the loop */ 3482 int count = 0; 3483 /* maximum number of times to run around the loop */ 3484 int loopmax; 3485 conn_t *connp = tcp->tcp_connp; 3486 zoneid_t zoneid = connp->conn_zoneid; 3487 tcp_stack_t *tcps = tcp->tcp_tcps; 3488 3489 /* 3490 * Lookup for free addresses is done in a loop and "loopmax" 3491 * influences how long we spin in the loop 3492 */ 3493 if (bind_to_req_port_only) { 3494 /* 3495 * If the requested port is busy, don't bother to look 3496 * for a new one. Setting loop maximum count to 1 has 3497 * that effect. 3498 */ 3499 loopmax = 1; 3500 } else { 3501 /* 3502 * If the requested port is busy, look for a free one 3503 * in the anonymous port range. 3504 * Set loopmax appropriately so that one does not look 3505 * forever in the case all of the anonymous ports are in use. 3506 */ 3507 if (tcp->tcp_anon_priv_bind) { 3508 /* 3509 * loopmax = 3510 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3511 */ 3512 loopmax = IPPORT_RESERVED - 3513 tcps->tcps_min_anonpriv_port; 3514 } else { 3515 loopmax = (tcps->tcps_largest_anon_port - 3516 tcps->tcps_smallest_anon_port + 1); 3517 } 3518 } 3519 do { 3520 uint16_t lport; 3521 tf_t *tbf; 3522 tcp_t *ltcp; 3523 conn_t *lconnp; 3524 3525 lport = htons(port); 3526 3527 /* 3528 * Ensure that the tcp_t is not currently in the bind hash. 3529 * Hold the lock on the hash bucket to ensure that 3530 * the duplicate check plus the insertion is an atomic 3531 * operation. 3532 * 3533 * This function does an inline lookup on the bind hash list 3534 * Make sure that we access only members of tcp_t 3535 * and that we don't look at tcp_tcp, since we are not 3536 * doing a CONN_INC_REF. 3537 */ 3538 tcp_bind_hash_remove(tcp); 3539 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3540 mutex_enter(&tbf->tf_lock); 3541 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3542 ltcp = ltcp->tcp_bind_hash) { 3543 boolean_t not_socket; 3544 boolean_t exclbind; 3545 3546 if (lport != ltcp->tcp_lport) 3547 continue; 3548 3549 lconnp = ltcp->tcp_connp; 3550 3551 /* 3552 * On a labeled system, we must treat bindings to ports 3553 * on shared IP addresses by sockets with MAC exemption 3554 * privilege as being in all zones, as there's 3555 * otherwise no way to identify the right receiver. 3556 */ 3557 if (!IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) && 3558 !lconnp->conn_mac_exempt && 3559 !connp->conn_mac_exempt) 3560 continue; 3561 3562 /* 3563 * If TCP_EXCLBIND is set for either the bound or 3564 * binding endpoint, the semantics of bind 3565 * is changed according to the following. 3566 * 3567 * spec = specified address (v4 or v6) 3568 * unspec = unspecified address (v4 or v6) 3569 * A = specified addresses are different for endpoints 3570 * 3571 * bound bind to allowed 3572 * ------------------------------------- 3573 * unspec unspec no 3574 * unspec spec no 3575 * spec unspec no 3576 * spec spec yes if A 3577 * 3578 * For labeled systems, SO_MAC_EXEMPT behaves the same 3579 * as TCP_EXCLBIND, except that zoneid is ignored. 3580 * 3581 * Note: 3582 * 3583 * 1. Because of TLI semantics, an endpoint can go 3584 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3585 * TCPS_BOUND, depending on whether it is originally 3586 * a listener or not. That is why we need to check 3587 * for states greater than or equal to TCPS_BOUND 3588 * here. 3589 * 3590 * 2. Ideally, we should only check for state equals 3591 * to TCPS_LISTEN. And the following check should be 3592 * added. 3593 * 3594 * if (ltcp->tcp_state == TCPS_LISTEN || 3595 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3596 * ... 3597 * } 3598 * 3599 * The semantics will be changed to this. If the 3600 * endpoint on the list is in state not equal to 3601 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3602 * set, let the bind succeed. 3603 * 3604 * Because of (1), we cannot do that for TLI 3605 * endpoints. But we can do that for socket endpoints. 3606 * If in future, we can change this going back 3607 * semantics, we can use the above check for TLI also. 3608 */ 3609 not_socket = !(TCP_IS_SOCKET(ltcp) && 3610 TCP_IS_SOCKET(tcp)); 3611 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3612 3613 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3614 (exclbind && (not_socket || 3615 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3616 if (V6_OR_V4_INADDR_ANY( 3617 ltcp->tcp_bound_source_v6) || 3618 V6_OR_V4_INADDR_ANY(*laddr) || 3619 IN6_ARE_ADDR_EQUAL(laddr, 3620 <cp->tcp_bound_source_v6)) { 3621 break; 3622 } 3623 continue; 3624 } 3625 3626 /* 3627 * Check ipversion to allow IPv4 and IPv6 sockets to 3628 * have disjoint port number spaces, if *_EXCLBIND 3629 * is not set and only if the application binds to a 3630 * specific port. We use the same autoassigned port 3631 * number space for IPv4 and IPv6 sockets. 3632 */ 3633 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3634 bind_to_req_port_only) 3635 continue; 3636 3637 /* 3638 * Ideally, we should make sure that the source 3639 * address, remote address, and remote port in the 3640 * four tuple for this tcp-connection is unique. 3641 * However, trying to find out the local source 3642 * address would require too much code duplication 3643 * with IP, since IP needs needs to have that code 3644 * to support userland TCP implementations. 3645 */ 3646 if (quick_connect && 3647 (ltcp->tcp_state > TCPS_LISTEN) && 3648 ((tcp->tcp_fport != ltcp->tcp_fport) || 3649 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3650 <cp->tcp_remote_v6))) 3651 continue; 3652 3653 if (!reuseaddr) { 3654 /* 3655 * No socket option SO_REUSEADDR. 3656 * If existing port is bound to 3657 * a non-wildcard IP address 3658 * and the requesting stream is 3659 * bound to a distinct 3660 * different IP addresses 3661 * (non-wildcard, also), keep 3662 * going. 3663 */ 3664 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3665 !V6_OR_V4_INADDR_ANY( 3666 ltcp->tcp_bound_source_v6) && 3667 !IN6_ARE_ADDR_EQUAL(laddr, 3668 <cp->tcp_bound_source_v6)) 3669 continue; 3670 if (ltcp->tcp_state >= TCPS_BOUND) { 3671 /* 3672 * This port is being used and 3673 * its state is >= TCPS_BOUND, 3674 * so we can't bind to it. 3675 */ 3676 break; 3677 } 3678 } else { 3679 /* 3680 * socket option SO_REUSEADDR is set on the 3681 * binding tcp_t. 3682 * 3683 * If two streams are bound to 3684 * same IP address or both addr 3685 * and bound source are wildcards 3686 * (INADDR_ANY), we want to stop 3687 * searching. 3688 * We have found a match of IP source 3689 * address and source port, which is 3690 * refused regardless of the 3691 * SO_REUSEADDR setting, so we break. 3692 */ 3693 if (IN6_ARE_ADDR_EQUAL(laddr, 3694 <cp->tcp_bound_source_v6) && 3695 (ltcp->tcp_state == TCPS_LISTEN || 3696 ltcp->tcp_state == TCPS_BOUND)) 3697 break; 3698 } 3699 } 3700 if (ltcp != NULL) { 3701 /* The port number is busy */ 3702 mutex_exit(&tbf->tf_lock); 3703 } else { 3704 /* 3705 * This port is ours. Insert in fanout and mark as 3706 * bound to prevent others from getting the port 3707 * number. 3708 */ 3709 tcp->tcp_state = TCPS_BOUND; 3710 tcp->tcp_lport = htons(port); 3711 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3712 3713 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3714 tcp->tcp_lport)] == tbf); 3715 tcp_bind_hash_insert(tbf, tcp, 1); 3716 3717 mutex_exit(&tbf->tf_lock); 3718 3719 /* 3720 * We don't want tcp_next_port_to_try to "inherit" 3721 * a port number supplied by the user in a bind. 3722 */ 3723 if (user_specified) 3724 return (port); 3725 3726 /* 3727 * This is the only place where tcp_next_port_to_try 3728 * is updated. After the update, it may or may not 3729 * be in the valid range. 3730 */ 3731 if (!tcp->tcp_anon_priv_bind) 3732 tcps->tcps_next_port_to_try = port + 1; 3733 return (port); 3734 } 3735 3736 if (tcp->tcp_anon_priv_bind) { 3737 port = tcp_get_next_priv_port(tcp); 3738 } else { 3739 if (count == 0 && user_specified) { 3740 /* 3741 * We may have to return an anonymous port. So 3742 * get one to start with. 3743 */ 3744 port = 3745 tcp_update_next_port( 3746 tcps->tcps_next_port_to_try, 3747 tcp, B_TRUE); 3748 user_specified = B_FALSE; 3749 } else { 3750 port = tcp_update_next_port(port + 1, tcp, 3751 B_FALSE); 3752 } 3753 } 3754 if (port == 0) 3755 break; 3756 3757 /* 3758 * Don't let this loop run forever in the case where 3759 * all of the anonymous ports are in use. 3760 */ 3761 } while (++count < loopmax); 3762 return (0); 3763 } 3764 3765 /* 3766 * tcp_clean_death / tcp_close_detached must not be called more than once 3767 * on a tcp. Thus every function that potentially calls tcp_clean_death 3768 * must check for the tcp state before calling tcp_clean_death. 3769 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3770 * tcp_timer_handler, all check for the tcp state. 3771 */ 3772 /* ARGSUSED */ 3773 void 3774 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3775 { 3776 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3777 3778 freemsg(mp); 3779 if (tcp->tcp_state > TCPS_BOUND) 3780 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, ETIMEDOUT, 5); 3781 } 3782 3783 /* 3784 * We are dying for some reason. Try to do it gracefully. (May be called 3785 * as writer.) 3786 * 3787 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3788 * done by a service procedure). 3789 * TBD - Should the return value distinguish between the tcp_t being 3790 * freed and it being reinitialized? 3791 */ 3792 static int 3793 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3794 { 3795 mblk_t *mp; 3796 queue_t *q; 3797 tcp_stack_t *tcps = tcp->tcp_tcps; 3798 3799 TCP_CLD_STAT(tag); 3800 3801 #if TCP_TAG_CLEAN_DEATH 3802 tcp->tcp_cleandeathtag = tag; 3803 #endif 3804 3805 if (tcp->tcp_fused) 3806 tcp_unfuse(tcp); 3807 3808 if (tcp->tcp_linger_tid != 0 && 3809 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3810 tcp_stop_lingering(tcp); 3811 } 3812 3813 ASSERT(tcp != NULL); 3814 ASSERT((tcp->tcp_family == AF_INET && 3815 tcp->tcp_ipversion == IPV4_VERSION) || 3816 (tcp->tcp_family == AF_INET6 && 3817 (tcp->tcp_ipversion == IPV4_VERSION || 3818 tcp->tcp_ipversion == IPV6_VERSION))); 3819 3820 if (TCP_IS_DETACHED(tcp)) { 3821 if (tcp->tcp_hard_binding) { 3822 /* 3823 * Its an eager that we are dealing with. We close the 3824 * eager but in case a conn_ind has already gone to the 3825 * listener, let tcp_accept_finish() send a discon_ind 3826 * to the listener and drop the last reference. If the 3827 * listener doesn't even know about the eager i.e. the 3828 * conn_ind hasn't gone up, blow away the eager and drop 3829 * the last reference as well. If the conn_ind has gone 3830 * up, state should be BOUND. tcp_accept_finish 3831 * will figure out that the connection has received a 3832 * RST and will send a DISCON_IND to the application. 3833 */ 3834 tcp_closei_local(tcp); 3835 if (!tcp->tcp_tconnind_started) { 3836 CONN_DEC_REF(tcp->tcp_connp); 3837 } else { 3838 tcp->tcp_state = TCPS_BOUND; 3839 } 3840 } else { 3841 tcp_close_detached(tcp); 3842 } 3843 return (0); 3844 } 3845 3846 TCP_STAT(tcps, tcp_clean_death_nondetached); 3847 3848 /* 3849 * If T_ORDREL_IND has not been sent yet (done when service routine 3850 * is run) postpone cleaning up the endpoint until service routine 3851 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3852 * client_errno since tcp_close uses the client_errno field. 3853 */ 3854 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3855 if (err != 0) 3856 tcp->tcp_client_errno = err; 3857 3858 tcp->tcp_deferred_clean_death = B_TRUE; 3859 return (-1); 3860 } 3861 3862 q = tcp->tcp_rq; 3863 3864 /* Trash all inbound data */ 3865 flushq(q, FLUSHALL); 3866 3867 /* 3868 * If we are at least part way open and there is error 3869 * (err==0 implies no error) 3870 * notify our client by a T_DISCON_IND. 3871 */ 3872 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3873 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3874 !TCP_IS_SOCKET(tcp)) { 3875 /* 3876 * Send M_FLUSH according to TPI. Because sockets will 3877 * (and must) ignore FLUSHR we do that only for TPI 3878 * endpoints and sockets in STREAMS mode. 3879 */ 3880 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3881 } 3882 if (tcp->tcp_debug) { 3883 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3884 "tcp_clean_death: discon err %d", err); 3885 } 3886 mp = mi_tpi_discon_ind(NULL, err, 0); 3887 if (mp != NULL) { 3888 putnext(q, mp); 3889 } else { 3890 if (tcp->tcp_debug) { 3891 (void) strlog(TCP_MOD_ID, 0, 1, 3892 SL_ERROR|SL_TRACE, 3893 "tcp_clean_death, sending M_ERROR"); 3894 } 3895 (void) putnextctl1(q, M_ERROR, EPROTO); 3896 } 3897 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3898 /* SYN_SENT or SYN_RCVD */ 3899 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3900 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3901 /* ESTABLISHED or CLOSE_WAIT */ 3902 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3903 } 3904 } 3905 3906 tcp_reinit(tcp); 3907 return (-1); 3908 } 3909 3910 /* 3911 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3912 * to expire, stop the wait and finish the close. 3913 */ 3914 static void 3915 tcp_stop_lingering(tcp_t *tcp) 3916 { 3917 clock_t delta = 0; 3918 tcp_stack_t *tcps = tcp->tcp_tcps; 3919 3920 tcp->tcp_linger_tid = 0; 3921 if (tcp->tcp_state > TCPS_LISTEN) { 3922 tcp_acceptor_hash_remove(tcp); 3923 mutex_enter(&tcp->tcp_non_sq_lock); 3924 if (tcp->tcp_flow_stopped) { 3925 tcp_clrqfull(tcp); 3926 } 3927 mutex_exit(&tcp->tcp_non_sq_lock); 3928 3929 if (tcp->tcp_timer_tid != 0) { 3930 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3931 tcp->tcp_timer_tid = 0; 3932 } 3933 /* 3934 * Need to cancel those timers which will not be used when 3935 * TCP is detached. This has to be done before the tcp_wq 3936 * is set to the global queue. 3937 */ 3938 tcp_timers_stop(tcp); 3939 3940 3941 tcp->tcp_detached = B_TRUE; 3942 ASSERT(tcps->tcps_g_q != NULL); 3943 tcp->tcp_rq = tcps->tcps_g_q; 3944 tcp->tcp_wq = WR(tcps->tcps_g_q); 3945 3946 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3947 tcp_time_wait_append(tcp); 3948 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3949 goto finish; 3950 } 3951 3952 /* 3953 * If delta is zero the timer event wasn't executed and was 3954 * successfully canceled. In this case we need to restart it 3955 * with the minimal delta possible. 3956 */ 3957 if (delta >= 0) { 3958 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3959 delta ? delta : 1); 3960 } 3961 } else { 3962 tcp_closei_local(tcp); 3963 CONN_DEC_REF(tcp->tcp_connp); 3964 } 3965 finish: 3966 /* Signal closing thread that it can complete close */ 3967 mutex_enter(&tcp->tcp_closelock); 3968 tcp->tcp_detached = B_TRUE; 3969 ASSERT(tcps->tcps_g_q != NULL); 3970 tcp->tcp_rq = tcps->tcps_g_q; 3971 tcp->tcp_wq = WR(tcps->tcps_g_q); 3972 tcp->tcp_closed = 1; 3973 cv_signal(&tcp->tcp_closecv); 3974 mutex_exit(&tcp->tcp_closelock); 3975 } 3976 3977 /* 3978 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3979 * expires. 3980 */ 3981 static void 3982 tcp_close_linger_timeout(void *arg) 3983 { 3984 conn_t *connp = (conn_t *)arg; 3985 tcp_t *tcp = connp->conn_tcp; 3986 3987 tcp->tcp_client_errno = ETIMEDOUT; 3988 tcp_stop_lingering(tcp); 3989 } 3990 3991 static int 3992 tcp_close(queue_t *q, int flags) 3993 { 3994 conn_t *connp = Q_TO_CONN(q); 3995 tcp_t *tcp = connp->conn_tcp; 3996 mblk_t *mp = &tcp->tcp_closemp; 3997 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3998 boolean_t linger_interrupted = B_FALSE; 3999 mblk_t *bp; 4000 4001 ASSERT(WR(q)->q_next == NULL); 4002 ASSERT(connp->conn_ref >= 2); 4003 ASSERT((connp->conn_flags & IPCL_TCPMOD) == 0); 4004 4005 /* 4006 * We are being closed as /dev/tcp or /dev/tcp6. 4007 * 4008 * Mark the conn as closing. ill_pending_mp_add will not 4009 * add any mp to the pending mp list, after this conn has 4010 * started closing. Same for sq_pending_mp_add 4011 */ 4012 mutex_enter(&connp->conn_lock); 4013 connp->conn_state_flags |= CONN_CLOSING; 4014 if (connp->conn_oper_pending_ill != NULL) 4015 conn_ioctl_cleanup_reqd = B_TRUE; 4016 CONN_INC_REF_LOCKED(connp); 4017 mutex_exit(&connp->conn_lock); 4018 tcp->tcp_closeflags = (uint8_t)flags; 4019 ASSERT(connp->conn_ref >= 3); 4020 4021 /* 4022 * tcp_closemp_used is used below without any protection of a lock 4023 * as we don't expect any one else to use it concurrently at this 4024 * point otherwise it would be a major defect, though we do 4025 * increment tcp_closemp_used to record any attempt to reuse 4026 * tcp_closemp while it is still in use. This would help debugging. 4027 */ 4028 4029 if (mp->b_prev == NULL) { 4030 tcp->tcp_closemp_used = 1; 4031 } else { 4032 tcp->tcp_closemp_used++; 4033 ASSERT(mp->b_prev == NULL); 4034 } 4035 4036 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4037 4038 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4039 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4040 4041 mutex_enter(&tcp->tcp_closelock); 4042 while (!tcp->tcp_closed) { 4043 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4044 /* 4045 * We got interrupted. Check if we are lingering, 4046 * if yes, post a message to stop and wait until 4047 * tcp_closed is set. If we aren't lingering, 4048 * just go back around. 4049 */ 4050 if (tcp->tcp_linger && 4051 tcp->tcp_lingertime > 0 && 4052 !linger_interrupted) { 4053 mutex_exit(&tcp->tcp_closelock); 4054 /* Entering squeue, bump ref count. */ 4055 CONN_INC_REF(connp); 4056 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4057 squeue_enter(connp->conn_sqp, bp, 4058 tcp_linger_interrupted, connp, 4059 SQTAG_IP_TCP_CLOSE); 4060 linger_interrupted = B_TRUE; 4061 mutex_enter(&tcp->tcp_closelock); 4062 } 4063 } 4064 } 4065 mutex_exit(&tcp->tcp_closelock); 4066 4067 /* 4068 * In the case of listener streams that have eagers in the q or q0 4069 * we wait for the eagers to drop their reference to us. tcp_rq and 4070 * tcp_wq of the eagers point to our queues. By waiting for the 4071 * refcnt to drop to 1, we are sure that the eagers have cleaned 4072 * up their queue pointers and also dropped their references to us. 4073 */ 4074 if (tcp->tcp_wait_for_eagers) { 4075 mutex_enter(&connp->conn_lock); 4076 while (connp->conn_ref != 1) { 4077 cv_wait(&connp->conn_cv, &connp->conn_lock); 4078 } 4079 mutex_exit(&connp->conn_lock); 4080 } 4081 /* 4082 * ioctl cleanup. The mp is queued in the 4083 * ill_pending_mp or in the sq_pending_mp. 4084 */ 4085 if (conn_ioctl_cleanup_reqd) 4086 conn_ioctl_cleanup(connp); 4087 4088 qprocsoff(q); 4089 inet_minor_free(ip_minor_arena, connp->conn_dev); 4090 4091 tcp->tcp_cpid = -1; 4092 4093 /* 4094 * Drop IP's reference on the conn. This is the last reference 4095 * on the connp if the state was less than established. If the 4096 * connection has gone into timewait state, then we will have 4097 * one ref for the TCP and one more ref (total of two) for the 4098 * classifier connected hash list (a timewait connections stays 4099 * in connected hash till closed). 4100 * 4101 * We can't assert the references because there might be other 4102 * transient reference places because of some walkers or queued 4103 * packets in squeue for the timewait state. 4104 */ 4105 CONN_DEC_REF(connp); 4106 q->q_ptr = WR(q)->q_ptr = NULL; 4107 return (0); 4108 } 4109 4110 static int 4111 tcpclose_accept(queue_t *q) 4112 { 4113 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4114 4115 /* 4116 * We had opened an acceptor STREAM for sockfs which is 4117 * now being closed due to some error. 4118 */ 4119 qprocsoff(q); 4120 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 4121 q->q_ptr = WR(q)->q_ptr = NULL; 4122 return (0); 4123 } 4124 4125 /* 4126 * Called by tcp_close() routine via squeue when lingering is 4127 * interrupted by a signal. 4128 */ 4129 4130 /* ARGSUSED */ 4131 static void 4132 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4133 { 4134 conn_t *connp = (conn_t *)arg; 4135 tcp_t *tcp = connp->conn_tcp; 4136 4137 freeb(mp); 4138 if (tcp->tcp_linger_tid != 0 && 4139 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4140 tcp_stop_lingering(tcp); 4141 tcp->tcp_client_errno = EINTR; 4142 } 4143 } 4144 4145 /* 4146 * Called by streams close routine via squeues when our client blows off her 4147 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4148 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4149 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4150 * acked. 4151 * 4152 * NOTE: tcp_close potentially returns error when lingering. 4153 * However, the stream head currently does not pass these errors 4154 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4155 * errors to the application (from tsleep()) and not errors 4156 * like ECONNRESET caused by receiving a reset packet. 4157 */ 4158 4159 /* ARGSUSED */ 4160 static void 4161 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4162 { 4163 char *msg; 4164 conn_t *connp = (conn_t *)arg; 4165 tcp_t *tcp = connp->conn_tcp; 4166 clock_t delta = 0; 4167 tcp_stack_t *tcps = tcp->tcp_tcps; 4168 4169 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4170 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4171 4172 /* Cancel any pending timeout */ 4173 if (tcp->tcp_ordrelid != 0) { 4174 if (tcp->tcp_timeout) { 4175 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4176 } 4177 tcp->tcp_ordrelid = 0; 4178 tcp->tcp_timeout = B_FALSE; 4179 } 4180 4181 mutex_enter(&tcp->tcp_eager_lock); 4182 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4183 /* Cleanup for listener */ 4184 tcp_eager_cleanup(tcp, 0); 4185 tcp->tcp_wait_for_eagers = 1; 4186 } 4187 mutex_exit(&tcp->tcp_eager_lock); 4188 4189 connp->conn_mdt_ok = B_FALSE; 4190 tcp->tcp_mdt = B_FALSE; 4191 4192 connp->conn_lso_ok = B_FALSE; 4193 tcp->tcp_lso = B_FALSE; 4194 4195 msg = NULL; 4196 switch (tcp->tcp_state) { 4197 case TCPS_CLOSED: 4198 case TCPS_IDLE: 4199 case TCPS_BOUND: 4200 case TCPS_LISTEN: 4201 break; 4202 case TCPS_SYN_SENT: 4203 msg = "tcp_close, during connect"; 4204 break; 4205 case TCPS_SYN_RCVD: 4206 /* 4207 * Close during the connect 3-way handshake 4208 * but here there may or may not be pending data 4209 * already on queue. Process almost same as in 4210 * the ESTABLISHED state. 4211 */ 4212 /* FALLTHRU */ 4213 default: 4214 if (tcp->tcp_fused) 4215 tcp_unfuse(tcp); 4216 4217 /* 4218 * If SO_LINGER has set a zero linger time, abort the 4219 * connection with a reset. 4220 */ 4221 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4222 msg = "tcp_close, zero lingertime"; 4223 break; 4224 } 4225 4226 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4227 /* 4228 * Abort connection if there is unread data queued. 4229 */ 4230 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4231 msg = "tcp_close, unread data"; 4232 break; 4233 } 4234 /* 4235 * tcp_hard_bound is now cleared thus all packets go through 4236 * tcp_lookup. This fact is used by tcp_detach below. 4237 * 4238 * We have done a qwait() above which could have possibly 4239 * drained more messages in turn causing transition to a 4240 * different state. Check whether we have to do the rest 4241 * of the processing or not. 4242 */ 4243 if (tcp->tcp_state <= TCPS_LISTEN) 4244 break; 4245 4246 /* 4247 * Transmit the FIN before detaching the tcp_t. 4248 * After tcp_detach returns this queue/perimeter 4249 * no longer owns the tcp_t thus others can modify it. 4250 */ 4251 (void) tcp_xmit_end(tcp); 4252 4253 /* 4254 * If lingering on close then wait until the fin is acked, 4255 * the SO_LINGER time passes, or a reset is sent/received. 4256 */ 4257 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4258 !(tcp->tcp_fin_acked) && 4259 tcp->tcp_state >= TCPS_ESTABLISHED) { 4260 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4261 tcp->tcp_client_errno = EWOULDBLOCK; 4262 } else if (tcp->tcp_client_errno == 0) { 4263 4264 ASSERT(tcp->tcp_linger_tid == 0); 4265 4266 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4267 tcp_close_linger_timeout, 4268 tcp->tcp_lingertime * hz); 4269 4270 /* tcp_close_linger_timeout will finish close */ 4271 if (tcp->tcp_linger_tid == 0) 4272 tcp->tcp_client_errno = ENOSR; 4273 else 4274 return; 4275 } 4276 4277 /* 4278 * Check if we need to detach or just close 4279 * the instance. 4280 */ 4281 if (tcp->tcp_state <= TCPS_LISTEN) 4282 break; 4283 } 4284 4285 /* 4286 * Make sure that no other thread will access the tcp_rq of 4287 * this instance (through lookups etc.) as tcp_rq will go 4288 * away shortly. 4289 */ 4290 tcp_acceptor_hash_remove(tcp); 4291 4292 mutex_enter(&tcp->tcp_non_sq_lock); 4293 if (tcp->tcp_flow_stopped) { 4294 tcp_clrqfull(tcp); 4295 } 4296 mutex_exit(&tcp->tcp_non_sq_lock); 4297 4298 if (tcp->tcp_timer_tid != 0) { 4299 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4300 tcp->tcp_timer_tid = 0; 4301 } 4302 /* 4303 * Need to cancel those timers which will not be used when 4304 * TCP is detached. This has to be done before the tcp_wq 4305 * is set to the global queue. 4306 */ 4307 tcp_timers_stop(tcp); 4308 4309 tcp->tcp_detached = B_TRUE; 4310 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4311 tcp_time_wait_append(tcp); 4312 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4313 ASSERT(connp->conn_ref >= 3); 4314 goto finish; 4315 } 4316 4317 /* 4318 * If delta is zero the timer event wasn't executed and was 4319 * successfully canceled. In this case we need to restart it 4320 * with the minimal delta possible. 4321 */ 4322 if (delta >= 0) 4323 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4324 delta ? delta : 1); 4325 4326 ASSERT(connp->conn_ref >= 3); 4327 goto finish; 4328 } 4329 4330 /* Detach did not complete. Still need to remove q from stream. */ 4331 if (msg) { 4332 if (tcp->tcp_state == TCPS_ESTABLISHED || 4333 tcp->tcp_state == TCPS_CLOSE_WAIT) 4334 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4335 if (tcp->tcp_state == TCPS_SYN_SENT || 4336 tcp->tcp_state == TCPS_SYN_RCVD) 4337 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4338 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4339 } 4340 4341 tcp_closei_local(tcp); 4342 CONN_DEC_REF(connp); 4343 ASSERT(connp->conn_ref >= 2); 4344 4345 finish: 4346 /* 4347 * Although packets are always processed on the correct 4348 * tcp's perimeter and access is serialized via squeue's, 4349 * IP still needs a queue when sending packets in time_wait 4350 * state so use WR(tcps_g_q) till ip_output() can be 4351 * changed to deal with just connp. For read side, we 4352 * could have set tcp_rq to NULL but there are some cases 4353 * in tcp_rput_data() from early days of this code which 4354 * do a putnext without checking if tcp is closed. Those 4355 * need to be identified before both tcp_rq and tcp_wq 4356 * can be set to NULL and tcps_g_q can disappear forever. 4357 */ 4358 mutex_enter(&tcp->tcp_closelock); 4359 /* 4360 * Don't change the queues in the case of a listener that has 4361 * eagers in its q or q0. It could surprise the eagers. 4362 * Instead wait for the eagers outside the squeue. 4363 */ 4364 if (!tcp->tcp_wait_for_eagers) { 4365 tcp->tcp_detached = B_TRUE; 4366 /* 4367 * When default queue is closing we set tcps_g_q to NULL 4368 * after the close is done. 4369 */ 4370 ASSERT(tcps->tcps_g_q != NULL); 4371 tcp->tcp_rq = tcps->tcps_g_q; 4372 tcp->tcp_wq = WR(tcps->tcps_g_q); 4373 } 4374 4375 /* Signal tcp_close() to finish closing. */ 4376 tcp->tcp_closed = 1; 4377 cv_signal(&tcp->tcp_closecv); 4378 mutex_exit(&tcp->tcp_closelock); 4379 } 4380 4381 4382 /* 4383 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4384 * Some stream heads get upset if they see these later on as anything but NULL. 4385 */ 4386 static void 4387 tcp_close_mpp(mblk_t **mpp) 4388 { 4389 mblk_t *mp; 4390 4391 if ((mp = *mpp) != NULL) { 4392 do { 4393 mp->b_next = NULL; 4394 mp->b_prev = NULL; 4395 } while ((mp = mp->b_cont) != NULL); 4396 4397 mp = *mpp; 4398 *mpp = NULL; 4399 freemsg(mp); 4400 } 4401 } 4402 4403 /* Do detached close. */ 4404 static void 4405 tcp_close_detached(tcp_t *tcp) 4406 { 4407 if (tcp->tcp_fused) 4408 tcp_unfuse(tcp); 4409 4410 /* 4411 * Clustering code serializes TCP disconnect callbacks and 4412 * cluster tcp list walks by blocking a TCP disconnect callback 4413 * if a cluster tcp list walk is in progress. This ensures 4414 * accurate accounting of TCPs in the cluster code even though 4415 * the TCP list walk itself is not atomic. 4416 */ 4417 tcp_closei_local(tcp); 4418 CONN_DEC_REF(tcp->tcp_connp); 4419 } 4420 4421 /* 4422 * Stop all TCP timers, and free the timer mblks if requested. 4423 */ 4424 void 4425 tcp_timers_stop(tcp_t *tcp) 4426 { 4427 if (tcp->tcp_timer_tid != 0) { 4428 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4429 tcp->tcp_timer_tid = 0; 4430 } 4431 if (tcp->tcp_ka_tid != 0) { 4432 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4433 tcp->tcp_ka_tid = 0; 4434 } 4435 if (tcp->tcp_ack_tid != 0) { 4436 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4437 tcp->tcp_ack_tid = 0; 4438 } 4439 if (tcp->tcp_push_tid != 0) { 4440 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4441 tcp->tcp_push_tid = 0; 4442 } 4443 } 4444 4445 /* 4446 * The tcp_t is going away. Remove it from all lists and set it 4447 * to TCPS_CLOSED. The freeing up of memory is deferred until 4448 * tcp_inactive. This is needed since a thread in tcp_rput might have 4449 * done a CONN_INC_REF on this structure before it was removed from the 4450 * hashes. 4451 */ 4452 static void 4453 tcp_closei_local(tcp_t *tcp) 4454 { 4455 ire_t *ire; 4456 conn_t *connp = tcp->tcp_connp; 4457 tcp_stack_t *tcps = tcp->tcp_tcps; 4458 4459 if (!TCP_IS_SOCKET(tcp)) 4460 tcp_acceptor_hash_remove(tcp); 4461 4462 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4463 tcp->tcp_ibsegs = 0; 4464 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4465 tcp->tcp_obsegs = 0; 4466 4467 /* 4468 * If we are an eager connection hanging off a listener that 4469 * hasn't formally accepted the connection yet, get off his 4470 * list and blow off any data that we have accumulated. 4471 */ 4472 if (tcp->tcp_listener != NULL) { 4473 tcp_t *listener = tcp->tcp_listener; 4474 mutex_enter(&listener->tcp_eager_lock); 4475 /* 4476 * tcp_tconnind_started == B_TRUE means that the 4477 * conn_ind has already gone to listener. At 4478 * this point, eager will be closed but we 4479 * leave it in listeners eager list so that 4480 * if listener decides to close without doing 4481 * accept, we can clean this up. In tcp_wput_accept 4482 * we take care of the case of accept on closed 4483 * eager. 4484 */ 4485 if (!tcp->tcp_tconnind_started) { 4486 tcp_eager_unlink(tcp); 4487 mutex_exit(&listener->tcp_eager_lock); 4488 /* 4489 * We don't want to have any pointers to the 4490 * listener queue, after we have released our 4491 * reference on the listener 4492 */ 4493 ASSERT(tcps->tcps_g_q != NULL); 4494 tcp->tcp_rq = tcps->tcps_g_q; 4495 tcp->tcp_wq = WR(tcps->tcps_g_q); 4496 CONN_DEC_REF(listener->tcp_connp); 4497 } else { 4498 mutex_exit(&listener->tcp_eager_lock); 4499 } 4500 } 4501 4502 /* Stop all the timers */ 4503 tcp_timers_stop(tcp); 4504 4505 if (tcp->tcp_state == TCPS_LISTEN) { 4506 if (tcp->tcp_ip_addr_cache) { 4507 kmem_free((void *)tcp->tcp_ip_addr_cache, 4508 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4509 tcp->tcp_ip_addr_cache = NULL; 4510 } 4511 } 4512 mutex_enter(&tcp->tcp_non_sq_lock); 4513 if (tcp->tcp_flow_stopped) 4514 tcp_clrqfull(tcp); 4515 mutex_exit(&tcp->tcp_non_sq_lock); 4516 4517 tcp_bind_hash_remove(tcp); 4518 /* 4519 * If the tcp_time_wait_collector (which runs outside the squeue) 4520 * is trying to remove this tcp from the time wait list, we will 4521 * block in tcp_time_wait_remove while trying to acquire the 4522 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4523 * requires the ipcl_hash_remove to be ordered after the 4524 * tcp_time_wait_remove for the refcnt checks to work correctly. 4525 */ 4526 if (tcp->tcp_state == TCPS_TIME_WAIT) 4527 (void) tcp_time_wait_remove(tcp, NULL); 4528 CL_INET_DISCONNECT(tcp); 4529 ipcl_hash_remove(connp); 4530 4531 /* 4532 * Delete the cached ire in conn_ire_cache and also mark 4533 * the conn as CONDEMNED 4534 */ 4535 mutex_enter(&connp->conn_lock); 4536 connp->conn_state_flags |= CONN_CONDEMNED; 4537 ire = connp->conn_ire_cache; 4538 connp->conn_ire_cache = NULL; 4539 mutex_exit(&connp->conn_lock); 4540 if (ire != NULL) 4541 IRE_REFRELE_NOTR(ire); 4542 4543 /* Need to cleanup any pending ioctls */ 4544 ASSERT(tcp->tcp_time_wait_next == NULL); 4545 ASSERT(tcp->tcp_time_wait_prev == NULL); 4546 ASSERT(tcp->tcp_time_wait_expire == 0); 4547 tcp->tcp_state = TCPS_CLOSED; 4548 4549 /* Release any SSL context */ 4550 if (tcp->tcp_kssl_ent != NULL) { 4551 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4552 tcp->tcp_kssl_ent = NULL; 4553 } 4554 if (tcp->tcp_kssl_ctx != NULL) { 4555 kssl_release_ctx(tcp->tcp_kssl_ctx); 4556 tcp->tcp_kssl_ctx = NULL; 4557 } 4558 tcp->tcp_kssl_pending = B_FALSE; 4559 4560 tcp_ipsec_cleanup(tcp); 4561 } 4562 4563 /* 4564 * tcp is dying (called from ipcl_conn_destroy and error cases). 4565 * Free the tcp_t in either case. 4566 */ 4567 void 4568 tcp_free(tcp_t *tcp) 4569 { 4570 mblk_t *mp; 4571 ip6_pkt_t *ipp; 4572 4573 ASSERT(tcp != NULL); 4574 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4575 4576 tcp->tcp_rq = NULL; 4577 tcp->tcp_wq = NULL; 4578 4579 tcp_close_mpp(&tcp->tcp_xmit_head); 4580 tcp_close_mpp(&tcp->tcp_reass_head); 4581 if (tcp->tcp_rcv_list != NULL) { 4582 /* Free b_next chain */ 4583 tcp_close_mpp(&tcp->tcp_rcv_list); 4584 } 4585 if ((mp = tcp->tcp_urp_mp) != NULL) { 4586 freemsg(mp); 4587 } 4588 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4589 freemsg(mp); 4590 } 4591 4592 if (tcp->tcp_fused_sigurg_mp != NULL) { 4593 freeb(tcp->tcp_fused_sigurg_mp); 4594 tcp->tcp_fused_sigurg_mp = NULL; 4595 } 4596 4597 if (tcp->tcp_sack_info != NULL) { 4598 if (tcp->tcp_notsack_list != NULL) { 4599 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4600 } 4601 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4602 } 4603 4604 if (tcp->tcp_hopopts != NULL) { 4605 mi_free(tcp->tcp_hopopts); 4606 tcp->tcp_hopopts = NULL; 4607 tcp->tcp_hopoptslen = 0; 4608 } 4609 ASSERT(tcp->tcp_hopoptslen == 0); 4610 if (tcp->tcp_dstopts != NULL) { 4611 mi_free(tcp->tcp_dstopts); 4612 tcp->tcp_dstopts = NULL; 4613 tcp->tcp_dstoptslen = 0; 4614 } 4615 ASSERT(tcp->tcp_dstoptslen == 0); 4616 if (tcp->tcp_rtdstopts != NULL) { 4617 mi_free(tcp->tcp_rtdstopts); 4618 tcp->tcp_rtdstopts = NULL; 4619 tcp->tcp_rtdstoptslen = 0; 4620 } 4621 ASSERT(tcp->tcp_rtdstoptslen == 0); 4622 if (tcp->tcp_rthdr != NULL) { 4623 mi_free(tcp->tcp_rthdr); 4624 tcp->tcp_rthdr = NULL; 4625 tcp->tcp_rthdrlen = 0; 4626 } 4627 ASSERT(tcp->tcp_rthdrlen == 0); 4628 4629 ipp = &tcp->tcp_sticky_ipp; 4630 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4631 IPPF_RTHDR)) 4632 ip6_pkt_free(ipp); 4633 4634 /* 4635 * Free memory associated with the tcp/ip header template. 4636 */ 4637 4638 if (tcp->tcp_iphc != NULL) 4639 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4640 4641 /* 4642 * Following is really a blowing away a union. 4643 * It happens to have exactly two members of identical size 4644 * the following code is enough. 4645 */ 4646 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4647 4648 if (tcp->tcp_tracebuf != NULL) { 4649 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4650 tcp->tcp_tracebuf = NULL; 4651 } 4652 } 4653 4654 4655 /* 4656 * Put a connection confirmation message upstream built from the 4657 * address information within 'iph' and 'tcph'. Report our success or failure. 4658 */ 4659 static boolean_t 4660 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4661 mblk_t **defermp) 4662 { 4663 sin_t sin; 4664 sin6_t sin6; 4665 mblk_t *mp; 4666 char *optp = NULL; 4667 int optlen = 0; 4668 cred_t *cr; 4669 4670 if (defermp != NULL) 4671 *defermp = NULL; 4672 4673 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4674 /* 4675 * Return in T_CONN_CON results of option negotiation through 4676 * the T_CONN_REQ. Note: If there is an real end-to-end option 4677 * negotiation, then what is received from remote end needs 4678 * to be taken into account but there is no such thing (yet?) 4679 * in our TCP/IP. 4680 * Note: We do not use mi_offset_param() here as 4681 * tcp_opts_conn_req contents do not directly come from 4682 * an application and are either generated in kernel or 4683 * from user input that was already verified. 4684 */ 4685 mp = tcp->tcp_conn.tcp_opts_conn_req; 4686 optp = (char *)(mp->b_rptr + 4687 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4688 optlen = (int) 4689 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4690 } 4691 4692 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4693 ipha_t *ipha = (ipha_t *)iphdr; 4694 4695 /* packet is IPv4 */ 4696 if (tcp->tcp_family == AF_INET) { 4697 sin = sin_null; 4698 sin.sin_addr.s_addr = ipha->ipha_src; 4699 sin.sin_port = *(uint16_t *)tcph->th_lport; 4700 sin.sin_family = AF_INET; 4701 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4702 (int)sizeof (sin_t), optp, optlen); 4703 } else { 4704 sin6 = sin6_null; 4705 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4706 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4707 sin6.sin6_family = AF_INET6; 4708 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4709 (int)sizeof (sin6_t), optp, optlen); 4710 4711 } 4712 } else { 4713 ip6_t *ip6h = (ip6_t *)iphdr; 4714 4715 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4716 ASSERT(tcp->tcp_family == AF_INET6); 4717 sin6 = sin6_null; 4718 sin6.sin6_addr = ip6h->ip6_src; 4719 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4720 sin6.sin6_family = AF_INET6; 4721 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4722 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4723 (int)sizeof (sin6_t), optp, optlen); 4724 } 4725 4726 if (!mp) 4727 return (B_FALSE); 4728 4729 if ((cr = DB_CRED(idmp)) != NULL) { 4730 mblk_setcred(mp, cr); 4731 DB_CPID(mp) = DB_CPID(idmp); 4732 } 4733 4734 if (defermp == NULL) 4735 putnext(tcp->tcp_rq, mp); 4736 else 4737 *defermp = mp; 4738 4739 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4740 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4741 return (B_TRUE); 4742 } 4743 4744 /* 4745 * Defense for the SYN attack - 4746 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4747 * one from the list of droppable eagers. This list is a subset of q0. 4748 * see comments before the definition of MAKE_DROPPABLE(). 4749 * 2. Don't drop a SYN request before its first timeout. This gives every 4750 * request at least til the first timeout to complete its 3-way handshake. 4751 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4752 * requests currently on the queue that has timed out. This will be used 4753 * as an indicator of whether an attack is under way, so that appropriate 4754 * actions can be taken. (It's incremented in tcp_timer() and decremented 4755 * either when eager goes into ESTABLISHED, or gets freed up.) 4756 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4757 * # of timeout drops back to <= q0len/32 => SYN alert off 4758 */ 4759 static boolean_t 4760 tcp_drop_q0(tcp_t *tcp) 4761 { 4762 tcp_t *eager; 4763 mblk_t *mp; 4764 tcp_stack_t *tcps = tcp->tcp_tcps; 4765 4766 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4767 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4768 4769 /* Pick oldest eager from the list of droppable eagers */ 4770 eager = tcp->tcp_eager_prev_drop_q0; 4771 4772 /* If list is empty. return B_FALSE */ 4773 if (eager == tcp) { 4774 return (B_FALSE); 4775 } 4776 4777 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4778 if ((mp = allocb(0, BPRI_HI)) == NULL) 4779 return (B_FALSE); 4780 4781 /* 4782 * Take this eager out from the list of droppable eagers since we are 4783 * going to drop it. 4784 */ 4785 MAKE_UNDROPPABLE(eager); 4786 4787 if (tcp->tcp_debug) { 4788 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4789 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4790 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4791 tcp->tcp_conn_req_cnt_q0, 4792 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4793 } 4794 4795 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4796 4797 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4798 CONN_INC_REF(eager->tcp_connp); 4799 4800 /* Mark the IRE created for this SYN request temporary */ 4801 tcp_ip_ire_mark_advice(eager); 4802 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4803 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4804 4805 return (B_TRUE); 4806 } 4807 4808 int 4809 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4810 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4811 { 4812 tcp_t *ltcp = lconnp->conn_tcp; 4813 tcp_t *tcp = connp->conn_tcp; 4814 mblk_t *tpi_mp; 4815 ipha_t *ipha; 4816 ip6_t *ip6h; 4817 sin6_t sin6; 4818 in6_addr_t v6dst; 4819 int err; 4820 int ifindex = 0; 4821 cred_t *cr; 4822 tcp_stack_t *tcps = tcp->tcp_tcps; 4823 4824 if (ipvers == IPV4_VERSION) { 4825 ipha = (ipha_t *)mp->b_rptr; 4826 4827 connp->conn_send = ip_output; 4828 connp->conn_recv = tcp_input; 4829 4830 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4831 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4832 4833 sin6 = sin6_null; 4834 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4835 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4836 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4837 sin6.sin6_family = AF_INET6; 4838 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4839 lconnp->conn_zoneid, tcps->tcps_netstack); 4840 if (tcp->tcp_recvdstaddr) { 4841 sin6_t sin6d; 4842 4843 sin6d = sin6_null; 4844 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4845 &sin6d.sin6_addr); 4846 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4847 sin6d.sin6_family = AF_INET; 4848 tpi_mp = mi_tpi_extconn_ind(NULL, 4849 (char *)&sin6d, sizeof (sin6_t), 4850 (char *)&tcp, 4851 (t_scalar_t)sizeof (intptr_t), 4852 (char *)&sin6d, sizeof (sin6_t), 4853 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4854 } else { 4855 tpi_mp = mi_tpi_conn_ind(NULL, 4856 (char *)&sin6, sizeof (sin6_t), 4857 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4858 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4859 } 4860 } else { 4861 ip6h = (ip6_t *)mp->b_rptr; 4862 4863 connp->conn_send = ip_output_v6; 4864 connp->conn_recv = tcp_input; 4865 4866 connp->conn_srcv6 = ip6h->ip6_dst; 4867 connp->conn_remv6 = ip6h->ip6_src; 4868 4869 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4870 ifindex = (int)DB_CKSUMSTUFF(mp); 4871 DB_CKSUMSTUFF(mp) = 0; 4872 4873 sin6 = sin6_null; 4874 sin6.sin6_addr = ip6h->ip6_src; 4875 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4876 sin6.sin6_family = AF_INET6; 4877 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4878 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4879 lconnp->conn_zoneid, tcps->tcps_netstack); 4880 4881 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4882 /* Pass up the scope_id of remote addr */ 4883 sin6.sin6_scope_id = ifindex; 4884 } else { 4885 sin6.sin6_scope_id = 0; 4886 } 4887 if (tcp->tcp_recvdstaddr) { 4888 sin6_t sin6d; 4889 4890 sin6d = sin6_null; 4891 sin6.sin6_addr = ip6h->ip6_dst; 4892 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4893 sin6d.sin6_family = AF_INET; 4894 tpi_mp = mi_tpi_extconn_ind(NULL, 4895 (char *)&sin6d, sizeof (sin6_t), 4896 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4897 (char *)&sin6d, sizeof (sin6_t), 4898 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4899 } else { 4900 tpi_mp = mi_tpi_conn_ind(NULL, 4901 (char *)&sin6, sizeof (sin6_t), 4902 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4903 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4904 } 4905 } 4906 4907 if (tpi_mp == NULL) 4908 return (ENOMEM); 4909 4910 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4911 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4912 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4913 connp->conn_fully_bound = B_FALSE; 4914 4915 if (tcps->tcps_trace) 4916 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4917 4918 /* Inherit information from the "parent" */ 4919 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4920 tcp->tcp_family = ltcp->tcp_family; 4921 tcp->tcp_wq = ltcp->tcp_wq; 4922 tcp->tcp_rq = ltcp->tcp_rq; 4923 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4924 tcp->tcp_detached = B_TRUE; 4925 if ((err = tcp_init_values(tcp)) != 0) { 4926 freemsg(tpi_mp); 4927 return (err); 4928 } 4929 4930 if (ipvers == IPV4_VERSION) { 4931 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4932 freemsg(tpi_mp); 4933 return (err); 4934 } 4935 ASSERT(tcp->tcp_ipha != NULL); 4936 } else { 4937 /* ifindex must be already set */ 4938 ASSERT(ifindex != 0); 4939 4940 if (ltcp->tcp_bound_if != 0) { 4941 /* 4942 * Set newtcp's bound_if equal to 4943 * listener's value. If ifindex is 4944 * not the same as ltcp->tcp_bound_if, 4945 * it must be a packet for the ipmp group 4946 * of interfaces 4947 */ 4948 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4949 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4950 tcp->tcp_bound_if = ifindex; 4951 } 4952 4953 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4954 tcp->tcp_recvifindex = 0; 4955 tcp->tcp_recvhops = 0xffffffffU; 4956 ASSERT(tcp->tcp_ip6h != NULL); 4957 } 4958 4959 tcp->tcp_lport = ltcp->tcp_lport; 4960 4961 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4962 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4963 /* 4964 * Listener had options of some sort; eager inherits. 4965 * Free up the eager template and allocate one 4966 * of the right size. 4967 */ 4968 if (tcp->tcp_hdr_grown) { 4969 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4970 } else { 4971 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4972 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4973 } 4974 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4975 KM_NOSLEEP); 4976 if (tcp->tcp_iphc == NULL) { 4977 tcp->tcp_iphc_len = 0; 4978 freemsg(tpi_mp); 4979 return (ENOMEM); 4980 } 4981 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4982 tcp->tcp_hdr_grown = B_TRUE; 4983 } 4984 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4985 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4986 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4987 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4988 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4989 4990 /* 4991 * Copy the IP+TCP header template from listener to eager 4992 */ 4993 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4994 if (tcp->tcp_ipversion == IPV6_VERSION) { 4995 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4996 IPPROTO_RAW) { 4997 tcp->tcp_ip6h = 4998 (ip6_t *)(tcp->tcp_iphc + 4999 sizeof (ip6i_t)); 5000 } else { 5001 tcp->tcp_ip6h = 5002 (ip6_t *)(tcp->tcp_iphc); 5003 } 5004 tcp->tcp_ipha = NULL; 5005 } else { 5006 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5007 tcp->tcp_ip6h = NULL; 5008 } 5009 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5010 tcp->tcp_ip_hdr_len); 5011 } else { 5012 /* 5013 * only valid case when ipversion of listener and 5014 * eager differ is when listener is IPv6 and 5015 * eager is IPv4. 5016 * Eager header template has been initialized to the 5017 * maximum v4 header sizes, which includes space for 5018 * TCP and IP options. 5019 */ 5020 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5021 (tcp->tcp_ipversion == IPV4_VERSION)); 5022 ASSERT(tcp->tcp_iphc_len >= 5023 TCP_MAX_COMBINED_HEADER_LENGTH); 5024 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5025 /* copy IP header fields individually */ 5026 tcp->tcp_ipha->ipha_ttl = 5027 ltcp->tcp_ip6h->ip6_hops; 5028 bcopy(ltcp->tcp_tcph->th_lport, 5029 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5030 } 5031 5032 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5033 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5034 sizeof (in_port_t)); 5035 5036 if (ltcp->tcp_lport == 0) { 5037 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5038 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5039 sizeof (in_port_t)); 5040 } 5041 5042 if (tcp->tcp_ipversion == IPV4_VERSION) { 5043 ASSERT(ipha != NULL); 5044 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5045 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5046 5047 /* Source routing option copyover (reverse it) */ 5048 if (tcps->tcps_rev_src_routes) 5049 tcp_opt_reverse(tcp, ipha); 5050 } else { 5051 ASSERT(ip6h != NULL); 5052 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5053 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5054 } 5055 5056 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5057 ASSERT(!tcp->tcp_tconnind_started); 5058 /* 5059 * If the SYN contains a credential, it's a loopback packet; attach 5060 * the credential to the TPI message. 5061 */ 5062 if ((cr = DB_CRED(idmp)) != NULL) { 5063 mblk_setcred(tpi_mp, cr); 5064 DB_CPID(tpi_mp) = DB_CPID(idmp); 5065 } 5066 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5067 5068 /* Inherit the listener's SSL protection state */ 5069 5070 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5071 kssl_hold_ent(tcp->tcp_kssl_ent); 5072 tcp->tcp_kssl_pending = B_TRUE; 5073 } 5074 5075 return (0); 5076 } 5077 5078 5079 int 5080 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5081 tcph_t *tcph, mblk_t *idmp) 5082 { 5083 tcp_t *ltcp = lconnp->conn_tcp; 5084 tcp_t *tcp = connp->conn_tcp; 5085 sin_t sin; 5086 mblk_t *tpi_mp = NULL; 5087 int err; 5088 cred_t *cr; 5089 tcp_stack_t *tcps = tcp->tcp_tcps; 5090 5091 sin = sin_null; 5092 sin.sin_addr.s_addr = ipha->ipha_src; 5093 sin.sin_port = *(uint16_t *)tcph->th_lport; 5094 sin.sin_family = AF_INET; 5095 if (ltcp->tcp_recvdstaddr) { 5096 sin_t sind; 5097 5098 sind = sin_null; 5099 sind.sin_addr.s_addr = ipha->ipha_dst; 5100 sind.sin_port = *(uint16_t *)tcph->th_fport; 5101 sind.sin_family = AF_INET; 5102 tpi_mp = mi_tpi_extconn_ind(NULL, 5103 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5104 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5105 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5106 } else { 5107 tpi_mp = mi_tpi_conn_ind(NULL, 5108 (char *)&sin, sizeof (sin_t), 5109 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5110 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5111 } 5112 5113 if (tpi_mp == NULL) { 5114 return (ENOMEM); 5115 } 5116 5117 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5118 connp->conn_send = ip_output; 5119 connp->conn_recv = tcp_input; 5120 connp->conn_fully_bound = B_FALSE; 5121 5122 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5123 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5124 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5125 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5126 5127 if (tcps->tcps_trace) { 5128 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 5129 } 5130 5131 /* Inherit information from the "parent" */ 5132 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5133 tcp->tcp_family = ltcp->tcp_family; 5134 tcp->tcp_wq = ltcp->tcp_wq; 5135 tcp->tcp_rq = ltcp->tcp_rq; 5136 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 5137 tcp->tcp_detached = B_TRUE; 5138 if ((err = tcp_init_values(tcp)) != 0) { 5139 freemsg(tpi_mp); 5140 return (err); 5141 } 5142 5143 /* 5144 * Let's make sure that eager tcp template has enough space to 5145 * copy IPv4 listener's tcp template. Since the conn_t structure is 5146 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5147 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5148 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5149 * extension headers or with ip6i_t struct). Note that bcopy() below 5150 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5151 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5152 */ 5153 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5154 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5155 5156 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5157 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5158 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5159 tcp->tcp_ttl = ltcp->tcp_ttl; 5160 tcp->tcp_tos = ltcp->tcp_tos; 5161 5162 /* Copy the IP+TCP header template from listener to eager */ 5163 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5164 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5165 tcp->tcp_ip6h = NULL; 5166 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5167 tcp->tcp_ip_hdr_len); 5168 5169 /* Initialize the IP addresses and Ports */ 5170 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5171 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5172 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5173 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5174 5175 /* Source routing option copyover (reverse it) */ 5176 if (tcps->tcps_rev_src_routes) 5177 tcp_opt_reverse(tcp, ipha); 5178 5179 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5180 ASSERT(!tcp->tcp_tconnind_started); 5181 5182 /* 5183 * If the SYN contains a credential, it's a loopback packet; attach 5184 * the credential to the TPI message. 5185 */ 5186 if ((cr = DB_CRED(idmp)) != NULL) { 5187 mblk_setcred(tpi_mp, cr); 5188 DB_CPID(tpi_mp) = DB_CPID(idmp); 5189 } 5190 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5191 5192 /* Inherit the listener's SSL protection state */ 5193 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5194 kssl_hold_ent(tcp->tcp_kssl_ent); 5195 tcp->tcp_kssl_pending = B_TRUE; 5196 } 5197 5198 return (0); 5199 } 5200 5201 /* 5202 * sets up conn for ipsec. 5203 * if the first mblk is M_CTL it is consumed and mpp is updated. 5204 * in case of error mpp is freed. 5205 */ 5206 conn_t * 5207 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5208 { 5209 conn_t *connp = tcp->tcp_connp; 5210 conn_t *econnp; 5211 squeue_t *new_sqp; 5212 mblk_t *first_mp = *mpp; 5213 mblk_t *mp = *mpp; 5214 boolean_t mctl_present = B_FALSE; 5215 uint_t ipvers; 5216 5217 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 5218 if (econnp == NULL) { 5219 freemsg(first_mp); 5220 return (NULL); 5221 } 5222 if (DB_TYPE(mp) == M_CTL) { 5223 if (mp->b_cont == NULL || 5224 mp->b_cont->b_datap->db_type != M_DATA) { 5225 freemsg(first_mp); 5226 return (NULL); 5227 } 5228 mp = mp->b_cont; 5229 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5230 freemsg(first_mp); 5231 return (NULL); 5232 } 5233 5234 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5235 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5236 mctl_present = B_TRUE; 5237 } else { 5238 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5239 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5240 } 5241 5242 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5243 DB_CKSUMSTART(mp) = 0; 5244 5245 ASSERT(OK_32PTR(mp->b_rptr)); 5246 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5247 if (ipvers == IPV4_VERSION) { 5248 uint16_t *up; 5249 uint32_t ports; 5250 ipha_t *ipha; 5251 5252 ipha = (ipha_t *)mp->b_rptr; 5253 up = (uint16_t *)((uchar_t *)ipha + 5254 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5255 ports = *(uint32_t *)up; 5256 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5257 ipha->ipha_dst, ipha->ipha_src, ports); 5258 } else { 5259 uint16_t *up; 5260 uint32_t ports; 5261 uint16_t ip_hdr_len; 5262 uint8_t *nexthdrp; 5263 ip6_t *ip6h; 5264 tcph_t *tcph; 5265 5266 ip6h = (ip6_t *)mp->b_rptr; 5267 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5268 ip_hdr_len = IPV6_HDR_LEN; 5269 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5270 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5271 CONN_DEC_REF(econnp); 5272 freemsg(first_mp); 5273 return (NULL); 5274 } 5275 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5276 up = (uint16_t *)tcph->th_lport; 5277 ports = *(uint32_t *)up; 5278 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5279 ip6h->ip6_dst, ip6h->ip6_src, ports); 5280 } 5281 5282 /* 5283 * The caller already ensured that there is a sqp present. 5284 */ 5285 econnp->conn_sqp = new_sqp; 5286 5287 if (connp->conn_policy != NULL) { 5288 ipsec_in_t *ii; 5289 ii = (ipsec_in_t *)(first_mp->b_rptr); 5290 ASSERT(ii->ipsec_in_policy == NULL); 5291 IPPH_REFHOLD(connp->conn_policy); 5292 ii->ipsec_in_policy = connp->conn_policy; 5293 5294 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5295 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5296 CONN_DEC_REF(econnp); 5297 freemsg(first_mp); 5298 return (NULL); 5299 } 5300 } 5301 5302 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5303 CONN_DEC_REF(econnp); 5304 freemsg(first_mp); 5305 return (NULL); 5306 } 5307 5308 /* 5309 * If we know we have some policy, pass the "IPSEC" 5310 * options size TCP uses this adjust the MSS. 5311 */ 5312 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5313 if (mctl_present) { 5314 freeb(first_mp); 5315 *mpp = mp; 5316 } 5317 5318 return (econnp); 5319 } 5320 5321 /* 5322 * tcp_get_conn/tcp_free_conn 5323 * 5324 * tcp_get_conn is used to get a clean tcp connection structure. 5325 * It tries to reuse the connections put on the freelist by the 5326 * time_wait_collector failing which it goes to kmem_cache. This 5327 * way has two benefits compared to just allocating from and 5328 * freeing to kmem_cache. 5329 * 1) The time_wait_collector can free (which includes the cleanup) 5330 * outside the squeue. So when the interrupt comes, we have a clean 5331 * connection sitting in the freelist. Obviously, this buys us 5332 * performance. 5333 * 5334 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5335 * has multiple disadvantages - tying up the squeue during alloc, and the 5336 * fact that IPSec policy initialization has to happen here which 5337 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5338 * But allocating the conn/tcp in IP land is also not the best since 5339 * we can't check the 'q' and 'q0' which are protected by squeue and 5340 * blindly allocate memory which might have to be freed here if we are 5341 * not allowed to accept the connection. By using the freelist and 5342 * putting the conn/tcp back in freelist, we don't pay a penalty for 5343 * allocating memory without checking 'q/q0' and freeing it if we can't 5344 * accept the connection. 5345 * 5346 * Care should be taken to put the conn back in the same squeue's freelist 5347 * from which it was allocated. Best results are obtained if conn is 5348 * allocated from listener's squeue and freed to the same. Time wait 5349 * collector will free up the freelist is the connection ends up sitting 5350 * there for too long. 5351 */ 5352 void * 5353 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5354 { 5355 tcp_t *tcp = NULL; 5356 conn_t *connp = NULL; 5357 squeue_t *sqp = (squeue_t *)arg; 5358 tcp_squeue_priv_t *tcp_time_wait; 5359 netstack_t *ns; 5360 5361 tcp_time_wait = 5362 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5363 5364 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5365 tcp = tcp_time_wait->tcp_free_list; 5366 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5367 if (tcp != NULL) { 5368 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5369 tcp_time_wait->tcp_free_list_cnt--; 5370 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5371 tcp->tcp_time_wait_next = NULL; 5372 connp = tcp->tcp_connp; 5373 connp->conn_flags |= IPCL_REUSED; 5374 5375 ASSERT(tcp->tcp_tcps == NULL); 5376 ASSERT(connp->conn_netstack == NULL); 5377 ns = tcps->tcps_netstack; 5378 netstack_hold(ns); 5379 connp->conn_netstack = ns; 5380 tcp->tcp_tcps = tcps; 5381 TCPS_REFHOLD(tcps); 5382 ipcl_globalhash_insert(connp); 5383 return ((void *)connp); 5384 } 5385 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5386 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5387 tcps->tcps_netstack)) == NULL) 5388 return (NULL); 5389 tcp = connp->conn_tcp; 5390 tcp->tcp_tcps = tcps; 5391 TCPS_REFHOLD(tcps); 5392 return ((void *)connp); 5393 } 5394 5395 /* 5396 * Update the cached label for the given tcp_t. This should be called once per 5397 * connection, and before any packets are sent or tcp_process_options is 5398 * invoked. Returns B_FALSE if the correct label could not be constructed. 5399 */ 5400 static boolean_t 5401 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5402 { 5403 conn_t *connp = tcp->tcp_connp; 5404 5405 if (tcp->tcp_ipversion == IPV4_VERSION) { 5406 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5407 int added; 5408 5409 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5410 connp->conn_mac_exempt, 5411 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5412 return (B_FALSE); 5413 5414 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5415 if (added == -1) 5416 return (B_FALSE); 5417 tcp->tcp_hdr_len += added; 5418 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5419 tcp->tcp_ip_hdr_len += added; 5420 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5421 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5422 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5423 tcp->tcp_hdr_len); 5424 if (added == -1) 5425 return (B_FALSE); 5426 tcp->tcp_hdr_len += added; 5427 tcp->tcp_tcph = (tcph_t *) 5428 ((uchar_t *)tcp->tcp_tcph + added); 5429 tcp->tcp_ip_hdr_len += added; 5430 } 5431 } else { 5432 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5433 5434 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5435 connp->conn_mac_exempt, 5436 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5437 return (B_FALSE); 5438 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5439 &tcp->tcp_label_len, optbuf) != 0) 5440 return (B_FALSE); 5441 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5442 return (B_FALSE); 5443 } 5444 5445 connp->conn_ulp_labeled = 1; 5446 5447 return (B_TRUE); 5448 } 5449 5450 /* BEGIN CSTYLED */ 5451 /* 5452 * 5453 * The sockfs ACCEPT path: 5454 * ======================= 5455 * 5456 * The eager is now established in its own perimeter as soon as SYN is 5457 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5458 * completes the accept processing on the acceptor STREAM. The sending 5459 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5460 * listener but a TLI/XTI listener completes the accept processing 5461 * on the listener perimeter. 5462 * 5463 * Common control flow for 3 way handshake: 5464 * ---------------------------------------- 5465 * 5466 * incoming SYN (listener perimeter) -> tcp_rput_data() 5467 * -> tcp_conn_request() 5468 * 5469 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5470 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5471 * 5472 * Sockfs ACCEPT Path: 5473 * ------------------- 5474 * 5475 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5476 * as STREAM entry point) 5477 * 5478 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5479 * 5480 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5481 * association (we are not behind eager's squeue but sockfs is protecting us 5482 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5483 * is changed to point at tcp_wput(). 5484 * 5485 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5486 * listener (done on listener's perimeter). 5487 * 5488 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5489 * accept. 5490 * 5491 * TLI/XTI client ACCEPT path: 5492 * --------------------------- 5493 * 5494 * soaccept() sends T_CONN_RES on the listener STREAM. 5495 * 5496 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5497 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5498 * 5499 * Locks: 5500 * ====== 5501 * 5502 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5503 * and listeners->tcp_eager_next_q. 5504 * 5505 * Referencing: 5506 * ============ 5507 * 5508 * 1) We start out in tcp_conn_request by eager placing a ref on 5509 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5510 * 5511 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5512 * doing so we place a ref on the eager. This ref is finally dropped at the 5513 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5514 * reference is dropped by the squeue framework. 5515 * 5516 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5517 * 5518 * The reference must be released by the same entity that added the reference 5519 * In the above scheme, the eager is the entity that adds and releases the 5520 * references. Note that tcp_accept_finish executes in the squeue of the eager 5521 * (albeit after it is attached to the acceptor stream). Though 1. executes 5522 * in the listener's squeue, the eager is nascent at this point and the 5523 * reference can be considered to have been added on behalf of the eager. 5524 * 5525 * Eager getting a Reset or listener closing: 5526 * ========================================== 5527 * 5528 * Once the listener and eager are linked, the listener never does the unlink. 5529 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5530 * a message on all eager perimeter. The eager then does the unlink, clears 5531 * any pointers to the listener's queue and drops the reference to the 5532 * listener. The listener waits in tcp_close outside the squeue until its 5533 * refcount has dropped to 1. This ensures that the listener has waited for 5534 * all eagers to clear their association with the listener. 5535 * 5536 * Similarly, if eager decides to go away, it can unlink itself and close. 5537 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5538 * the reference to eager is still valid because of the extra ref we put 5539 * in tcp_send_conn_ind. 5540 * 5541 * Listener can always locate the eager under the protection 5542 * of the listener->tcp_eager_lock, and then do a refhold 5543 * on the eager during the accept processing. 5544 * 5545 * The acceptor stream accesses the eager in the accept processing 5546 * based on the ref placed on eager before sending T_conn_ind. 5547 * The only entity that can negate this refhold is a listener close 5548 * which is mutually exclusive with an active acceptor stream. 5549 * 5550 * Eager's reference on the listener 5551 * =================================== 5552 * 5553 * If the accept happens (even on a closed eager) the eager drops its 5554 * reference on the listener at the start of tcp_accept_finish. If the 5555 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5556 * the reference is dropped in tcp_closei_local. If the listener closes, 5557 * the reference is dropped in tcp_eager_kill. In all cases the reference 5558 * is dropped while executing in the eager's context (squeue). 5559 */ 5560 /* END CSTYLED */ 5561 5562 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5563 5564 /* 5565 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5566 * tcp_rput_data will not see any SYN packets. 5567 */ 5568 /* ARGSUSED */ 5569 void 5570 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5571 { 5572 tcph_t *tcph; 5573 uint32_t seg_seq; 5574 tcp_t *eager; 5575 uint_t ipvers; 5576 ipha_t *ipha; 5577 ip6_t *ip6h; 5578 int err; 5579 conn_t *econnp = NULL; 5580 squeue_t *new_sqp; 5581 mblk_t *mp1; 5582 uint_t ip_hdr_len; 5583 conn_t *connp = (conn_t *)arg; 5584 tcp_t *tcp = connp->conn_tcp; 5585 ire_t *ire; 5586 cred_t *credp; 5587 tcp_stack_t *tcps = tcp->tcp_tcps; 5588 ip_stack_t *ipst; 5589 5590 if (tcp->tcp_state != TCPS_LISTEN) 5591 goto error2; 5592 5593 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5594 5595 mutex_enter(&tcp->tcp_eager_lock); 5596 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5597 mutex_exit(&tcp->tcp_eager_lock); 5598 TCP_STAT(tcps, tcp_listendrop); 5599 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5600 if (tcp->tcp_debug) { 5601 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5602 "tcp_conn_request: listen backlog (max=%d) " 5603 "overflow (%d pending) on %s", 5604 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5605 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5606 } 5607 goto error2; 5608 } 5609 5610 if (tcp->tcp_conn_req_cnt_q0 >= 5611 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5612 /* 5613 * Q0 is full. Drop a pending half-open req from the queue 5614 * to make room for the new SYN req. Also mark the time we 5615 * drop a SYN. 5616 * 5617 * A more aggressive defense against SYN attack will 5618 * be to set the "tcp_syn_defense" flag now. 5619 */ 5620 TCP_STAT(tcps, tcp_listendropq0); 5621 tcp->tcp_last_rcv_lbolt = lbolt64; 5622 if (!tcp_drop_q0(tcp)) { 5623 mutex_exit(&tcp->tcp_eager_lock); 5624 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5625 if (tcp->tcp_debug) { 5626 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5627 "tcp_conn_request: listen half-open queue " 5628 "(max=%d) full (%d pending) on %s", 5629 tcps->tcps_conn_req_max_q0, 5630 tcp->tcp_conn_req_cnt_q0, 5631 tcp_display(tcp, NULL, 5632 DISP_PORT_ONLY)); 5633 } 5634 goto error2; 5635 } 5636 } 5637 mutex_exit(&tcp->tcp_eager_lock); 5638 5639 /* 5640 * IP adds STRUIO_EAGER and ensures that the received packet is 5641 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5642 * link local address. If IPSec is enabled, db_struioflag has 5643 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5644 * otherwise an error case if neither of them is set. 5645 */ 5646 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5647 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5648 DB_CKSUMSTART(mp) = 0; 5649 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5650 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5651 if (econnp == NULL) 5652 goto error2; 5653 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5654 econnp->conn_sqp = new_sqp; 5655 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5656 /* 5657 * mp is updated in tcp_get_ipsec_conn(). 5658 */ 5659 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5660 if (econnp == NULL) { 5661 /* 5662 * mp freed by tcp_get_ipsec_conn. 5663 */ 5664 return; 5665 } 5666 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5667 } else { 5668 goto error2; 5669 } 5670 5671 ASSERT(DB_TYPE(mp) == M_DATA); 5672 5673 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5674 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5675 ASSERT(OK_32PTR(mp->b_rptr)); 5676 if (ipvers == IPV4_VERSION) { 5677 ipha = (ipha_t *)mp->b_rptr; 5678 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5679 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5680 } else { 5681 ip6h = (ip6_t *)mp->b_rptr; 5682 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5683 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5684 } 5685 5686 if (tcp->tcp_family == AF_INET) { 5687 ASSERT(ipvers == IPV4_VERSION); 5688 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5689 } else { 5690 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5691 } 5692 5693 if (err) 5694 goto error3; 5695 5696 eager = econnp->conn_tcp; 5697 5698 /* Inherit various TCP parameters from the listener */ 5699 eager->tcp_naglim = tcp->tcp_naglim; 5700 eager->tcp_first_timer_threshold = 5701 tcp->tcp_first_timer_threshold; 5702 eager->tcp_second_timer_threshold = 5703 tcp->tcp_second_timer_threshold; 5704 5705 eager->tcp_first_ctimer_threshold = 5706 tcp->tcp_first_ctimer_threshold; 5707 eager->tcp_second_ctimer_threshold = 5708 tcp->tcp_second_ctimer_threshold; 5709 5710 /* 5711 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5712 * If it does not, the eager's receive window will be set to the 5713 * listener's receive window later in this function. 5714 */ 5715 eager->tcp_rwnd = 0; 5716 5717 /* 5718 * Inherit listener's tcp_init_cwnd. Need to do this before 5719 * calling tcp_process_options() where tcp_mss_set() is called 5720 * to set the initial cwnd. 5721 */ 5722 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5723 5724 /* 5725 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5726 * zone id before the accept is completed in tcp_wput_accept(). 5727 */ 5728 econnp->conn_zoneid = connp->conn_zoneid; 5729 econnp->conn_allzones = connp->conn_allzones; 5730 5731 /* Copy nexthop information from listener to eager */ 5732 if (connp->conn_nexthop_set) { 5733 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5734 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5735 } 5736 5737 /* 5738 * TSOL: tsol_input_proc() needs the eager's cred before the 5739 * eager is accepted 5740 */ 5741 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5742 crhold(credp); 5743 5744 /* 5745 * If the caller has the process-wide flag set, then default to MAC 5746 * exempt mode. This allows read-down to unlabeled hosts. 5747 */ 5748 if (getpflags(NET_MAC_AWARE, credp) != 0) 5749 econnp->conn_mac_exempt = B_TRUE; 5750 5751 if (is_system_labeled()) { 5752 cred_t *cr; 5753 5754 if (connp->conn_mlp_type != mlptSingle) { 5755 cr = econnp->conn_peercred = DB_CRED(mp); 5756 if (cr != NULL) 5757 crhold(cr); 5758 else 5759 cr = econnp->conn_cred; 5760 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5761 econnp, cred_t *, cr) 5762 } else { 5763 cr = econnp->conn_cred; 5764 DTRACE_PROBE2(syn_accept, conn_t *, 5765 econnp, cred_t *, cr) 5766 } 5767 5768 if (!tcp_update_label(eager, cr)) { 5769 DTRACE_PROBE3( 5770 tx__ip__log__error__connrequest__tcp, 5771 char *, "eager connp(1) label on SYN mp(2) failed", 5772 conn_t *, econnp, mblk_t *, mp); 5773 goto error3; 5774 } 5775 } 5776 5777 eager->tcp_hard_binding = B_TRUE; 5778 5779 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5780 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5781 5782 CL_INET_CONNECT(eager); 5783 5784 /* 5785 * No need to check for multicast destination since ip will only pass 5786 * up multicasts to those that have expressed interest 5787 * TODO: what about rejecting broadcasts? 5788 * Also check that source is not a multicast or broadcast address. 5789 */ 5790 eager->tcp_state = TCPS_SYN_RCVD; 5791 5792 5793 /* 5794 * There should be no ire in the mp as we are being called after 5795 * receiving the SYN. 5796 */ 5797 ASSERT(tcp_ire_mp(mp) == NULL); 5798 5799 /* 5800 * Adapt our mss, ttl, ... according to information provided in IRE. 5801 */ 5802 5803 if (tcp_adapt_ire(eager, NULL) == 0) { 5804 /* Undo the bind_hash_insert */ 5805 tcp_bind_hash_remove(eager); 5806 goto error3; 5807 } 5808 5809 /* Process all TCP options. */ 5810 tcp_process_options(eager, tcph); 5811 5812 /* Is the other end ECN capable? */ 5813 if (tcps->tcps_ecn_permitted >= 1 && 5814 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5815 eager->tcp_ecn_ok = B_TRUE; 5816 } 5817 5818 /* 5819 * listener->tcp_rq->q_hiwat should be the default window size or a 5820 * window size changed via SO_RCVBUF option. First round up the 5821 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5822 * scale option value if needed. Call tcp_rwnd_set() to finish the 5823 * setting. 5824 * 5825 * Note if there is a rpipe metric associated with the remote host, 5826 * we should not inherit receive window size from listener. 5827 */ 5828 eager->tcp_rwnd = MSS_ROUNDUP( 5829 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5830 eager->tcp_rwnd), eager->tcp_mss); 5831 if (eager->tcp_snd_ws_ok) 5832 tcp_set_ws_value(eager); 5833 /* 5834 * Note that this is the only place tcp_rwnd_set() is called for 5835 * accepting a connection. We need to call it here instead of 5836 * after the 3-way handshake because we need to tell the other 5837 * side our rwnd in the SYN-ACK segment. 5838 */ 5839 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5840 5841 /* 5842 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5843 * via soaccept()->soinheritoptions() which essentially applies 5844 * all the listener options to the new STREAM. The options that we 5845 * need to take care of are: 5846 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5847 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5848 * SO_SNDBUF, SO_RCVBUF. 5849 * 5850 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5851 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5852 * tcp_maxpsz_set() gets called later from 5853 * tcp_accept_finish(), the option takes effect. 5854 * 5855 */ 5856 /* Set the TCP options */ 5857 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5858 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5859 eager->tcp_oobinline = tcp->tcp_oobinline; 5860 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5861 eager->tcp_broadcast = tcp->tcp_broadcast; 5862 eager->tcp_useloopback = tcp->tcp_useloopback; 5863 eager->tcp_dontroute = tcp->tcp_dontroute; 5864 eager->tcp_linger = tcp->tcp_linger; 5865 eager->tcp_lingertime = tcp->tcp_lingertime; 5866 if (tcp->tcp_ka_enabled) 5867 eager->tcp_ka_enabled = 1; 5868 5869 /* Set the IP options */ 5870 econnp->conn_broadcast = connp->conn_broadcast; 5871 econnp->conn_loopback = connp->conn_loopback; 5872 econnp->conn_dontroute = connp->conn_dontroute; 5873 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5874 5875 /* Put a ref on the listener for the eager. */ 5876 CONN_INC_REF(connp); 5877 mutex_enter(&tcp->tcp_eager_lock); 5878 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5879 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5880 tcp->tcp_eager_next_q0 = eager; 5881 eager->tcp_eager_prev_q0 = tcp; 5882 5883 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5884 eager->tcp_listener = tcp; 5885 eager->tcp_saved_listener = tcp; 5886 5887 /* 5888 * Tag this detached tcp vector for later retrieval 5889 * by our listener client in tcp_accept(). 5890 */ 5891 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5892 tcp->tcp_conn_req_cnt_q0++; 5893 if (++tcp->tcp_conn_req_seqnum == -1) { 5894 /* 5895 * -1 is "special" and defined in TPI as something 5896 * that should never be used in T_CONN_IND 5897 */ 5898 ++tcp->tcp_conn_req_seqnum; 5899 } 5900 mutex_exit(&tcp->tcp_eager_lock); 5901 5902 if (tcp->tcp_syn_defense) { 5903 /* Don't drop the SYN that comes from a good IP source */ 5904 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5905 if (addr_cache != NULL && eager->tcp_remote == 5906 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5907 eager->tcp_dontdrop = B_TRUE; 5908 } 5909 } 5910 5911 /* 5912 * We need to insert the eager in its own perimeter but as soon 5913 * as we do that, we expose the eager to the classifier and 5914 * should not touch any field outside the eager's perimeter. 5915 * So do all the work necessary before inserting the eager 5916 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5917 * will succeed but undo everything if it fails. 5918 */ 5919 seg_seq = ABE32_TO_U32(tcph->th_seq); 5920 eager->tcp_irs = seg_seq; 5921 eager->tcp_rack = seg_seq; 5922 eager->tcp_rnxt = seg_seq + 1; 5923 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5924 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5925 eager->tcp_state = TCPS_SYN_RCVD; 5926 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5927 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5928 if (mp1 == NULL) 5929 goto error1; 5930 DB_CPID(mp1) = tcp->tcp_cpid; 5931 eager->tcp_cpid = tcp->tcp_cpid; 5932 eager->tcp_open_time = lbolt64; 5933 5934 /* 5935 * We need to start the rto timer. In normal case, we start 5936 * the timer after sending the packet on the wire (or at 5937 * least believing that packet was sent by waiting for 5938 * CALL_IP_WPUT() to return). Since this is the first packet 5939 * being sent on the wire for the eager, our initial tcp_rto 5940 * is at least tcp_rexmit_interval_min which is a fairly 5941 * large value to allow the algorithm to adjust slowly to large 5942 * fluctuations of RTT during first few transmissions. 5943 * 5944 * Starting the timer first and then sending the packet in this 5945 * case shouldn't make much difference since tcp_rexmit_interval_min 5946 * is of the order of several 100ms and starting the timer 5947 * first and then sending the packet will result in difference 5948 * of few micro seconds. 5949 * 5950 * Without this optimization, we are forced to hold the fanout 5951 * lock across the ipcl_bind_insert() and sending the packet 5952 * so that we don't race against an incoming packet (maybe RST) 5953 * for this eager. 5954 * 5955 * It is necessary to acquire an extra reference on the eager 5956 * at this point and hold it until after tcp_send_data() to 5957 * ensure against an eager close race. 5958 */ 5959 5960 CONN_INC_REF(eager->tcp_connp); 5961 5962 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5963 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5964 5965 5966 /* 5967 * Insert the eager in its own perimeter now. We are ready to deal 5968 * with any packets on eager. 5969 */ 5970 if (eager->tcp_ipversion == IPV4_VERSION) { 5971 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5972 goto error; 5973 } 5974 } else { 5975 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5976 goto error; 5977 } 5978 } 5979 5980 /* mark conn as fully-bound */ 5981 econnp->conn_fully_bound = B_TRUE; 5982 5983 /* Send the SYN-ACK */ 5984 tcp_send_data(eager, eager->tcp_wq, mp1); 5985 CONN_DEC_REF(eager->tcp_connp); 5986 freemsg(mp); 5987 5988 return; 5989 error: 5990 (void) TCP_TIMER_CANCEL(eager, eager->tcp_timer_tid); 5991 CONN_DEC_REF(eager->tcp_connp); 5992 freemsg(mp1); 5993 error1: 5994 /* Undo what we did above */ 5995 mutex_enter(&tcp->tcp_eager_lock); 5996 tcp_eager_unlink(eager); 5997 mutex_exit(&tcp->tcp_eager_lock); 5998 /* Drop eager's reference on the listener */ 5999 CONN_DEC_REF(connp); 6000 6001 /* 6002 * Delete the cached ire in conn_ire_cache and also mark 6003 * the conn as CONDEMNED 6004 */ 6005 mutex_enter(&econnp->conn_lock); 6006 econnp->conn_state_flags |= CONN_CONDEMNED; 6007 ire = econnp->conn_ire_cache; 6008 econnp->conn_ire_cache = NULL; 6009 mutex_exit(&econnp->conn_lock); 6010 if (ire != NULL) 6011 IRE_REFRELE_NOTR(ire); 6012 6013 /* 6014 * tcp_accept_comm inserts the eager to the bind_hash 6015 * we need to remove it from the hash if ipcl_conn_insert 6016 * fails. 6017 */ 6018 tcp_bind_hash_remove(eager); 6019 /* Drop the eager ref placed in tcp_open_detached */ 6020 CONN_DEC_REF(econnp); 6021 6022 /* 6023 * If a connection already exists, send the mp to that connections so 6024 * that it can be appropriately dealt with. 6025 */ 6026 ipst = tcps->tcps_netstack->netstack_ip; 6027 6028 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 6029 if (!IPCL_IS_CONNECTED(econnp)) { 6030 /* 6031 * Something bad happened. ipcl_conn_insert() 6032 * failed because a connection already existed 6033 * in connected hash but we can't find it 6034 * anymore (someone blew it away). Just 6035 * free this message and hopefully remote 6036 * will retransmit at which time the SYN can be 6037 * treated as a new connection or dealth with 6038 * a TH_RST if a connection already exists. 6039 */ 6040 CONN_DEC_REF(econnp); 6041 freemsg(mp); 6042 } else { 6043 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6044 econnp, SQTAG_TCP_CONN_REQ); 6045 } 6046 } else { 6047 /* Nobody wants this packet */ 6048 freemsg(mp); 6049 } 6050 return; 6051 error2: 6052 freemsg(mp); 6053 return; 6054 error3: 6055 CONN_DEC_REF(econnp); 6056 freemsg(mp); 6057 } 6058 6059 /* 6060 * In an ideal case of vertical partition in NUMA architecture, its 6061 * beneficial to have the listener and all the incoming connections 6062 * tied to the same squeue. The other constraint is that incoming 6063 * connections should be tied to the squeue attached to interrupted 6064 * CPU for obvious locality reason so this leaves the listener to 6065 * be tied to the same squeue. Our only problem is that when listener 6066 * is binding, the CPU that will get interrupted by the NIC whose 6067 * IP address the listener is binding to is not even known. So 6068 * the code below allows us to change that binding at the time the 6069 * CPU is interrupted by virtue of incoming connection's squeue. 6070 * 6071 * This is usefull only in case of a listener bound to a specific IP 6072 * address. For other kind of listeners, they get bound the 6073 * very first time and there is no attempt to rebind them. 6074 */ 6075 void 6076 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6077 { 6078 conn_t *connp = (conn_t *)arg; 6079 squeue_t *sqp = (squeue_t *)arg2; 6080 squeue_t *new_sqp; 6081 uint32_t conn_flags; 6082 6083 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6084 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6085 } else { 6086 goto done; 6087 } 6088 6089 if (connp->conn_fanout == NULL) 6090 goto done; 6091 6092 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6093 mutex_enter(&connp->conn_fanout->connf_lock); 6094 mutex_enter(&connp->conn_lock); 6095 /* 6096 * No one from read or write side can access us now 6097 * except for already queued packets on this squeue. 6098 * But since we haven't changed the squeue yet, they 6099 * can't execute. If they are processed after we have 6100 * changed the squeue, they are sent back to the 6101 * correct squeue down below. 6102 * But a listner close can race with processing of 6103 * incoming SYN. If incoming SYN processing changes 6104 * the squeue then the listener close which is waiting 6105 * to enter the squeue would operate on the wrong 6106 * squeue. Hence we don't change the squeue here unless 6107 * the refcount is exactly the minimum refcount. The 6108 * minimum refcount of 4 is counted as - 1 each for 6109 * TCP and IP, 1 for being in the classifier hash, and 6110 * 1 for the mblk being processed. 6111 */ 6112 6113 if (connp->conn_ref != 4 || 6114 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6115 mutex_exit(&connp->conn_lock); 6116 mutex_exit(&connp->conn_fanout->connf_lock); 6117 goto done; 6118 } 6119 if (connp->conn_sqp != new_sqp) { 6120 while (connp->conn_sqp != new_sqp) 6121 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6122 } 6123 6124 do { 6125 conn_flags = connp->conn_flags; 6126 conn_flags |= IPCL_FULLY_BOUND; 6127 (void) cas32(&connp->conn_flags, connp->conn_flags, 6128 conn_flags); 6129 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6130 6131 mutex_exit(&connp->conn_fanout->connf_lock); 6132 mutex_exit(&connp->conn_lock); 6133 } 6134 6135 done: 6136 if (connp->conn_sqp != sqp) { 6137 CONN_INC_REF(connp); 6138 squeue_fill(connp->conn_sqp, mp, 6139 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6140 } else { 6141 tcp_conn_request(connp, mp, sqp); 6142 } 6143 } 6144 6145 /* 6146 * Successful connect request processing begins when our client passes 6147 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6148 * our T_OK_ACK reply message upstream. The control flow looks like this: 6149 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6150 * upstream <- tcp_rput() <- IP 6151 * After various error checks are completed, tcp_connect() lays 6152 * the target address and port into the composite header template, 6153 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6154 * request followed by an IRE request, and passes the three mblk message 6155 * down to IP looking like this: 6156 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6157 * Processing continues in tcp_rput() when we receive the following message: 6158 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6159 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6160 * to fire off the connection request, and then passes the T_OK_ACK mblk 6161 * upstream that we filled in below. There are, of course, numerous 6162 * error conditions along the way which truncate the processing described 6163 * above. 6164 */ 6165 static void 6166 tcp_connect(tcp_t *tcp, mblk_t *mp) 6167 { 6168 sin_t *sin; 6169 sin6_t *sin6; 6170 queue_t *q = tcp->tcp_wq; 6171 struct T_conn_req *tcr; 6172 ipaddr_t *dstaddrp; 6173 in_port_t dstport; 6174 uint_t srcid; 6175 6176 tcr = (struct T_conn_req *)mp->b_rptr; 6177 6178 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6179 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6180 tcp_err_ack(tcp, mp, TPROTO, 0); 6181 return; 6182 } 6183 6184 /* 6185 * Determine packet type based on type of address passed in 6186 * the request should contain an IPv4 or IPv6 address. 6187 * Make sure that address family matches the type of 6188 * family of the the address passed down 6189 */ 6190 switch (tcr->DEST_length) { 6191 default: 6192 tcp_err_ack(tcp, mp, TBADADDR, 0); 6193 return; 6194 6195 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6196 /* 6197 * XXX: The check for valid DEST_length was not there 6198 * in earlier releases and some buggy 6199 * TLI apps (e.g Sybase) got away with not feeding 6200 * in sin_zero part of address. 6201 * We allow that bug to keep those buggy apps humming. 6202 * Test suites require the check on DEST_length. 6203 * We construct a new mblk with valid DEST_length 6204 * free the original so the rest of the code does 6205 * not have to keep track of this special shorter 6206 * length address case. 6207 */ 6208 mblk_t *nmp; 6209 struct T_conn_req *ntcr; 6210 sin_t *nsin; 6211 6212 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6213 tcr->OPT_length, BPRI_HI); 6214 if (nmp == NULL) { 6215 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6216 return; 6217 } 6218 ntcr = (struct T_conn_req *)nmp->b_rptr; 6219 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6220 ntcr->PRIM_type = T_CONN_REQ; 6221 ntcr->DEST_length = sizeof (sin_t); 6222 ntcr->DEST_offset = sizeof (struct T_conn_req); 6223 6224 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6225 *nsin = sin_null; 6226 /* Get pointer to shorter address to copy from original mp */ 6227 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6228 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6229 if (sin == NULL || !OK_32PTR((char *)sin)) { 6230 freemsg(nmp); 6231 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6232 return; 6233 } 6234 nsin->sin_family = sin->sin_family; 6235 nsin->sin_port = sin->sin_port; 6236 nsin->sin_addr = sin->sin_addr; 6237 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6238 nmp->b_wptr = (uchar_t *)&nsin[1]; 6239 if (tcr->OPT_length != 0) { 6240 ntcr->OPT_length = tcr->OPT_length; 6241 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6242 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6243 (uchar_t *)ntcr + ntcr->OPT_offset, 6244 tcr->OPT_length); 6245 nmp->b_wptr += tcr->OPT_length; 6246 } 6247 freemsg(mp); /* original mp freed */ 6248 mp = nmp; /* re-initialize original variables */ 6249 tcr = ntcr; 6250 } 6251 /* FALLTHRU */ 6252 6253 case sizeof (sin_t): 6254 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6255 sizeof (sin_t)); 6256 if (sin == NULL || !OK_32PTR((char *)sin)) { 6257 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6258 return; 6259 } 6260 if (tcp->tcp_family != AF_INET || 6261 sin->sin_family != AF_INET) { 6262 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6263 return; 6264 } 6265 if (sin->sin_port == 0) { 6266 tcp_err_ack(tcp, mp, TBADADDR, 0); 6267 return; 6268 } 6269 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6270 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6271 return; 6272 } 6273 6274 break; 6275 6276 case sizeof (sin6_t): 6277 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6278 sizeof (sin6_t)); 6279 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6280 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6281 return; 6282 } 6283 if (tcp->tcp_family != AF_INET6 || 6284 sin6->sin6_family != AF_INET6) { 6285 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6286 return; 6287 } 6288 if (sin6->sin6_port == 0) { 6289 tcp_err_ack(tcp, mp, TBADADDR, 0); 6290 return; 6291 } 6292 break; 6293 } 6294 /* 6295 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6296 * should key on their sequence number and cut them loose. 6297 */ 6298 6299 /* 6300 * If options passed in, feed it for verification and handling 6301 */ 6302 if (tcr->OPT_length != 0) { 6303 mblk_t *ok_mp; 6304 mblk_t *discon_mp; 6305 mblk_t *conn_opts_mp; 6306 int t_error, sys_error, do_disconnect; 6307 6308 conn_opts_mp = NULL; 6309 6310 if (tcp_conprim_opt_process(tcp, mp, 6311 &do_disconnect, &t_error, &sys_error) < 0) { 6312 if (do_disconnect) { 6313 ASSERT(t_error == 0 && sys_error == 0); 6314 discon_mp = mi_tpi_discon_ind(NULL, 6315 ECONNREFUSED, 0); 6316 if (!discon_mp) { 6317 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6318 TSYSERR, ENOMEM); 6319 return; 6320 } 6321 ok_mp = mi_tpi_ok_ack_alloc(mp); 6322 if (!ok_mp) { 6323 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6324 TSYSERR, ENOMEM); 6325 return; 6326 } 6327 qreply(q, ok_mp); 6328 qreply(q, discon_mp); /* no flush! */ 6329 } else { 6330 ASSERT(t_error != 0); 6331 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6332 sys_error); 6333 } 6334 return; 6335 } 6336 /* 6337 * Success in setting options, the mp option buffer represented 6338 * by OPT_length/offset has been potentially modified and 6339 * contains results of option processing. We copy it in 6340 * another mp to save it for potentially influencing returning 6341 * it in T_CONN_CONN. 6342 */ 6343 if (tcr->OPT_length != 0) { /* there are resulting options */ 6344 conn_opts_mp = copyb(mp); 6345 if (!conn_opts_mp) { 6346 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6347 TSYSERR, ENOMEM); 6348 return; 6349 } 6350 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6351 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6352 /* 6353 * Note: 6354 * These resulting option negotiation can include any 6355 * end-to-end negotiation options but there no such 6356 * thing (yet?) in our TCP/IP. 6357 */ 6358 } 6359 } 6360 6361 /* 6362 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6363 * make sure that the template IP header in the tcp structure is an 6364 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6365 * need to this before we call tcp_bindi() so that the port lookup 6366 * code will look for ports in the correct port space (IPv4 and 6367 * IPv6 have separate port spaces). 6368 */ 6369 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6370 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6371 int err = 0; 6372 6373 err = tcp_header_init_ipv4(tcp); 6374 if (err != 0) { 6375 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6376 goto connect_failed; 6377 } 6378 if (tcp->tcp_lport != 0) 6379 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6380 } 6381 6382 switch (tcp->tcp_state) { 6383 case TCPS_IDLE: 6384 /* 6385 * We support quick connect, refer to comments in 6386 * tcp_connect_*() 6387 */ 6388 /* FALLTHRU */ 6389 case TCPS_BOUND: 6390 case TCPS_LISTEN: 6391 if (tcp->tcp_family == AF_INET6) { 6392 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6393 tcp_connect_ipv6(tcp, mp, 6394 &sin6->sin6_addr, 6395 sin6->sin6_port, sin6->sin6_flowinfo, 6396 sin6->__sin6_src_id, sin6->sin6_scope_id); 6397 return; 6398 } 6399 /* 6400 * Destination adress is mapped IPv6 address. 6401 * Source bound address should be unspecified or 6402 * IPv6 mapped address as well. 6403 */ 6404 if (!IN6_IS_ADDR_UNSPECIFIED( 6405 &tcp->tcp_bound_source_v6) && 6406 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6407 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6408 EADDRNOTAVAIL); 6409 break; 6410 } 6411 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6412 dstport = sin6->sin6_port; 6413 srcid = sin6->__sin6_src_id; 6414 } else { 6415 dstaddrp = &sin->sin_addr.s_addr; 6416 dstport = sin->sin_port; 6417 srcid = 0; 6418 } 6419 6420 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6421 return; 6422 default: 6423 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6424 break; 6425 } 6426 /* 6427 * Note: Code below is the "failure" case 6428 */ 6429 /* return error ack and blow away saved option results if any */ 6430 connect_failed: 6431 if (mp != NULL) 6432 putnext(tcp->tcp_rq, mp); 6433 else { 6434 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6435 TSYSERR, ENOMEM); 6436 } 6437 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6438 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6439 } 6440 6441 /* 6442 * Handle connect to IPv4 destinations, including connections for AF_INET6 6443 * sockets connecting to IPv4 mapped IPv6 destinations. 6444 */ 6445 static void 6446 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6447 uint_t srcid) 6448 { 6449 tcph_t *tcph; 6450 mblk_t *mp1; 6451 ipaddr_t dstaddr = *dstaddrp; 6452 int32_t oldstate; 6453 uint16_t lport; 6454 tcp_stack_t *tcps = tcp->tcp_tcps; 6455 6456 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6457 6458 /* Check for attempt to connect to INADDR_ANY */ 6459 if (dstaddr == INADDR_ANY) { 6460 /* 6461 * SunOS 4.x and 4.3 BSD allow an application 6462 * to connect a TCP socket to INADDR_ANY. 6463 * When they do this, the kernel picks the 6464 * address of one interface and uses it 6465 * instead. The kernel usually ends up 6466 * picking the address of the loopback 6467 * interface. This is an undocumented feature. 6468 * However, we provide the same thing here 6469 * in order to have source and binary 6470 * compatibility with SunOS 4.x. 6471 * Update the T_CONN_REQ (sin/sin6) since it is used to 6472 * generate the T_CONN_CON. 6473 */ 6474 dstaddr = htonl(INADDR_LOOPBACK); 6475 *dstaddrp = dstaddr; 6476 } 6477 6478 /* Handle __sin6_src_id if socket not bound to an IP address */ 6479 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6480 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6481 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6482 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6483 tcp->tcp_ipha->ipha_src); 6484 } 6485 6486 /* 6487 * Don't let an endpoint connect to itself. Note that 6488 * the test here does not catch the case where the 6489 * source IP addr was left unspecified by the user. In 6490 * this case, the source addr is set in tcp_adapt_ire() 6491 * using the reply to the T_BIND message that we send 6492 * down to IP here and the check is repeated in tcp_rput_other. 6493 */ 6494 if (dstaddr == tcp->tcp_ipha->ipha_src && 6495 dstport == tcp->tcp_lport) { 6496 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6497 goto failed; 6498 } 6499 6500 tcp->tcp_ipha->ipha_dst = dstaddr; 6501 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6502 6503 /* 6504 * Massage a source route if any putting the first hop 6505 * in iph_dst. Compute a starting value for the checksum which 6506 * takes into account that the original iph_dst should be 6507 * included in the checksum but that ip will include the 6508 * first hop in the source route in the tcp checksum. 6509 */ 6510 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6511 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6512 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6513 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6514 if ((int)tcp->tcp_sum < 0) 6515 tcp->tcp_sum--; 6516 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6517 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6518 (tcp->tcp_sum >> 16)); 6519 tcph = tcp->tcp_tcph; 6520 *(uint16_t *)tcph->th_fport = dstport; 6521 tcp->tcp_fport = dstport; 6522 6523 oldstate = tcp->tcp_state; 6524 /* 6525 * At this point the remote destination address and remote port fields 6526 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6527 * have to see which state tcp was in so we can take apropriate action. 6528 */ 6529 if (oldstate == TCPS_IDLE) { 6530 /* 6531 * We support a quick connect capability here, allowing 6532 * clients to transition directly from IDLE to SYN_SENT 6533 * tcp_bindi will pick an unused port, insert the connection 6534 * in the bind hash and transition to BOUND state. 6535 */ 6536 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6537 tcp, B_TRUE); 6538 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6539 B_FALSE, B_FALSE); 6540 if (lport == 0) { 6541 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6542 goto failed; 6543 } 6544 } 6545 tcp->tcp_state = TCPS_SYN_SENT; 6546 6547 /* 6548 * TODO: allow data with connect requests 6549 * by unlinking M_DATA trailers here and 6550 * linking them in behind the T_OK_ACK mblk. 6551 * The tcp_rput() bind ack handler would then 6552 * feed them to tcp_wput_data() rather than call 6553 * tcp_timer(). 6554 */ 6555 mp = mi_tpi_ok_ack_alloc(mp); 6556 if (!mp) { 6557 tcp->tcp_state = oldstate; 6558 goto failed; 6559 } 6560 if (tcp->tcp_family == AF_INET) { 6561 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6562 sizeof (ipa_conn_t)); 6563 } else { 6564 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6565 sizeof (ipa6_conn_t)); 6566 } 6567 if (mp1) { 6568 /* Hang onto the T_OK_ACK for later. */ 6569 linkb(mp1, mp); 6570 mblk_setcred(mp1, tcp->tcp_cred); 6571 if (tcp->tcp_family == AF_INET) 6572 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6573 else { 6574 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6575 &tcp->tcp_sticky_ipp); 6576 } 6577 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6578 tcp->tcp_active_open = 1; 6579 /* 6580 * If the bind cannot complete immediately 6581 * IP will arrange to call tcp_rput_other 6582 * when the bind completes. 6583 */ 6584 if (mp1 != NULL) 6585 tcp_rput_other(tcp, mp1); 6586 return; 6587 } 6588 /* Error case */ 6589 tcp->tcp_state = oldstate; 6590 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6591 6592 failed: 6593 /* return error ack and blow away saved option results if any */ 6594 if (mp != NULL) 6595 putnext(tcp->tcp_rq, mp); 6596 else { 6597 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6598 TSYSERR, ENOMEM); 6599 } 6600 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6601 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6602 6603 } 6604 6605 /* 6606 * Handle connect to IPv6 destinations. 6607 */ 6608 static void 6609 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6610 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6611 { 6612 tcph_t *tcph; 6613 mblk_t *mp1; 6614 ip6_rthdr_t *rth; 6615 int32_t oldstate; 6616 uint16_t lport; 6617 tcp_stack_t *tcps = tcp->tcp_tcps; 6618 6619 ASSERT(tcp->tcp_family == AF_INET6); 6620 6621 /* 6622 * If we're here, it means that the destination address is a native 6623 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6624 * reason why it might not be IPv6 is if the socket was bound to an 6625 * IPv4-mapped IPv6 address. 6626 */ 6627 if (tcp->tcp_ipversion != IPV6_VERSION) { 6628 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6629 goto failed; 6630 } 6631 6632 /* 6633 * Interpret a zero destination to mean loopback. 6634 * Update the T_CONN_REQ (sin/sin6) since it is used to 6635 * generate the T_CONN_CON. 6636 */ 6637 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6638 *dstaddrp = ipv6_loopback; 6639 } 6640 6641 /* Handle __sin6_src_id if socket not bound to an IP address */ 6642 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6643 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6644 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6645 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6646 } 6647 6648 /* 6649 * Take care of the scope_id now and add ip6i_t 6650 * if ip6i_t is not already allocated through TCP 6651 * sticky options. At this point tcp_ip6h does not 6652 * have dst info, thus use dstaddrp. 6653 */ 6654 if (scope_id != 0 && 6655 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6656 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6657 ip6i_t *ip6i; 6658 6659 ipp->ipp_ifindex = scope_id; 6660 ip6i = (ip6i_t *)tcp->tcp_iphc; 6661 6662 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6663 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6664 /* Already allocated */ 6665 ip6i->ip6i_flags |= IP6I_IFINDEX; 6666 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6667 ipp->ipp_fields |= IPPF_SCOPE_ID; 6668 } else { 6669 int reterr; 6670 6671 ipp->ipp_fields |= IPPF_SCOPE_ID; 6672 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6673 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6674 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6675 if (reterr != 0) 6676 goto failed; 6677 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6678 } 6679 } 6680 6681 /* 6682 * Don't let an endpoint connect to itself. Note that 6683 * the test here does not catch the case where the 6684 * source IP addr was left unspecified by the user. In 6685 * this case, the source addr is set in tcp_adapt_ire() 6686 * using the reply to the T_BIND message that we send 6687 * down to IP here and the check is repeated in tcp_rput_other. 6688 */ 6689 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6690 (dstport == tcp->tcp_lport)) { 6691 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6692 goto failed; 6693 } 6694 6695 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6696 tcp->tcp_remote_v6 = *dstaddrp; 6697 tcp->tcp_ip6h->ip6_vcf = 6698 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6699 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6700 6701 6702 /* 6703 * Massage a routing header (if present) putting the first hop 6704 * in ip6_dst. Compute a starting value for the checksum which 6705 * takes into account that the original ip6_dst should be 6706 * included in the checksum but that ip will include the 6707 * first hop in the source route in the tcp checksum. 6708 */ 6709 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6710 if (rth != NULL) { 6711 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6712 tcps->tcps_netstack); 6713 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6714 (tcp->tcp_sum >> 16)); 6715 } else { 6716 tcp->tcp_sum = 0; 6717 } 6718 6719 tcph = tcp->tcp_tcph; 6720 *(uint16_t *)tcph->th_fport = dstport; 6721 tcp->tcp_fport = dstport; 6722 6723 oldstate = tcp->tcp_state; 6724 /* 6725 * At this point the remote destination address and remote port fields 6726 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6727 * have to see which state tcp was in so we can take apropriate action. 6728 */ 6729 if (oldstate == TCPS_IDLE) { 6730 /* 6731 * We support a quick connect capability here, allowing 6732 * clients to transition directly from IDLE to SYN_SENT 6733 * tcp_bindi will pick an unused port, insert the connection 6734 * in the bind hash and transition to BOUND state. 6735 */ 6736 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6737 tcp, B_TRUE); 6738 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6739 B_FALSE, B_FALSE); 6740 if (lport == 0) { 6741 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6742 goto failed; 6743 } 6744 } 6745 tcp->tcp_state = TCPS_SYN_SENT; 6746 /* 6747 * TODO: allow data with connect requests 6748 * by unlinking M_DATA trailers here and 6749 * linking them in behind the T_OK_ACK mblk. 6750 * The tcp_rput() bind ack handler would then 6751 * feed them to tcp_wput_data() rather than call 6752 * tcp_timer(). 6753 */ 6754 mp = mi_tpi_ok_ack_alloc(mp); 6755 if (!mp) { 6756 tcp->tcp_state = oldstate; 6757 goto failed; 6758 } 6759 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6760 if (mp1) { 6761 /* Hang onto the T_OK_ACK for later. */ 6762 linkb(mp1, mp); 6763 mblk_setcred(mp1, tcp->tcp_cred); 6764 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6765 &tcp->tcp_sticky_ipp); 6766 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6767 tcp->tcp_active_open = 1; 6768 /* ip_bind_v6() may return ACK or ERROR */ 6769 if (mp1 != NULL) 6770 tcp_rput_other(tcp, mp1); 6771 return; 6772 } 6773 /* Error case */ 6774 tcp->tcp_state = oldstate; 6775 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6776 6777 failed: 6778 /* return error ack and blow away saved option results if any */ 6779 if (mp != NULL) 6780 putnext(tcp->tcp_rq, mp); 6781 else { 6782 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6783 TSYSERR, ENOMEM); 6784 } 6785 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6786 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6787 } 6788 6789 /* 6790 * We need a stream q for detached closing tcp connections 6791 * to use. Our client hereby indicates that this q is the 6792 * one to use. 6793 */ 6794 static void 6795 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6796 { 6797 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6798 queue_t *q = tcp->tcp_wq; 6799 tcp_stack_t *tcps = tcp->tcp_tcps; 6800 6801 #ifdef NS_DEBUG 6802 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6803 tcps->tcps_netstack->netstack_stackid); 6804 #endif 6805 mp->b_datap->db_type = M_IOCACK; 6806 iocp->ioc_count = 0; 6807 mutex_enter(&tcps->tcps_g_q_lock); 6808 if (tcps->tcps_g_q != NULL) { 6809 mutex_exit(&tcps->tcps_g_q_lock); 6810 iocp->ioc_error = EALREADY; 6811 } else { 6812 mblk_t *mp1; 6813 6814 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6815 if (mp1 == NULL) { 6816 mutex_exit(&tcps->tcps_g_q_lock); 6817 iocp->ioc_error = ENOMEM; 6818 } else { 6819 tcps->tcps_g_q = tcp->tcp_rq; 6820 mutex_exit(&tcps->tcps_g_q_lock); 6821 iocp->ioc_error = 0; 6822 iocp->ioc_rval = 0; 6823 /* 6824 * We are passing tcp_sticky_ipp as NULL 6825 * as it is not useful for tcp_default queue 6826 */ 6827 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6828 if (mp1 != NULL) 6829 tcp_rput_other(tcp, mp1); 6830 } 6831 } 6832 qreply(q, mp); 6833 } 6834 6835 /* 6836 * Our client hereby directs us to reject the connection request 6837 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6838 * of sending the appropriate RST, not an ICMP error. 6839 */ 6840 static void 6841 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6842 { 6843 tcp_t *ltcp = NULL; 6844 t_scalar_t seqnum; 6845 conn_t *connp; 6846 tcp_stack_t *tcps = tcp->tcp_tcps; 6847 6848 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6849 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6850 tcp_err_ack(tcp, mp, TPROTO, 0); 6851 return; 6852 } 6853 6854 /* 6855 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6856 * when the stream is in BOUND state. Do not send a reset, 6857 * since the destination IP address is not valid, and it can 6858 * be the initialized value of all zeros (broadcast address). 6859 * 6860 * If TCP has sent down a bind request to IP and has not 6861 * received the reply, reject the request. Otherwise, TCP 6862 * will be confused. 6863 */ 6864 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6865 if (tcp->tcp_debug) { 6866 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6867 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6868 } 6869 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6870 return; 6871 } 6872 6873 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6874 6875 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6876 6877 /* 6878 * According to TPI, for non-listeners, ignore seqnum 6879 * and disconnect. 6880 * Following interpretation of -1 seqnum is historical 6881 * and implied TPI ? (TPI only states that for T_CONN_IND, 6882 * a valid seqnum should not be -1). 6883 * 6884 * -1 means disconnect everything 6885 * regardless even on a listener. 6886 */ 6887 6888 int old_state = tcp->tcp_state; 6889 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6890 6891 /* 6892 * The connection can't be on the tcp_time_wait_head list 6893 * since it is not detached. 6894 */ 6895 ASSERT(tcp->tcp_time_wait_next == NULL); 6896 ASSERT(tcp->tcp_time_wait_prev == NULL); 6897 ASSERT(tcp->tcp_time_wait_expire == 0); 6898 ltcp = NULL; 6899 /* 6900 * If it used to be a listener, check to make sure no one else 6901 * has taken the port before switching back to LISTEN state. 6902 */ 6903 if (tcp->tcp_ipversion == IPV4_VERSION) { 6904 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6905 tcp->tcp_ipha->ipha_src, 6906 tcp->tcp_connp->conn_zoneid, ipst); 6907 if (connp != NULL) 6908 ltcp = connp->conn_tcp; 6909 } else { 6910 /* Allow tcp_bound_if listeners? */ 6911 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6912 &tcp->tcp_ip6h->ip6_src, 0, 6913 tcp->tcp_connp->conn_zoneid, ipst); 6914 if (connp != NULL) 6915 ltcp = connp->conn_tcp; 6916 } 6917 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6918 tcp->tcp_state = TCPS_LISTEN; 6919 } else if (old_state > TCPS_BOUND) { 6920 tcp->tcp_conn_req_max = 0; 6921 tcp->tcp_state = TCPS_BOUND; 6922 } 6923 if (ltcp != NULL) 6924 CONN_DEC_REF(ltcp->tcp_connp); 6925 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6926 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6927 } else if (old_state == TCPS_ESTABLISHED || 6928 old_state == TCPS_CLOSE_WAIT) { 6929 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6930 } 6931 6932 if (tcp->tcp_fused) 6933 tcp_unfuse(tcp); 6934 6935 mutex_enter(&tcp->tcp_eager_lock); 6936 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6937 (tcp->tcp_conn_req_cnt_q != 0)) { 6938 tcp_eager_cleanup(tcp, 0); 6939 } 6940 mutex_exit(&tcp->tcp_eager_lock); 6941 6942 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6943 tcp->tcp_rnxt, TH_RST | TH_ACK); 6944 6945 tcp_reinit(tcp); 6946 6947 if (old_state >= TCPS_ESTABLISHED) { 6948 /* Send M_FLUSH according to TPI */ 6949 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6950 } 6951 mp = mi_tpi_ok_ack_alloc(mp); 6952 if (mp) 6953 putnext(tcp->tcp_rq, mp); 6954 return; 6955 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6956 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6957 return; 6958 } 6959 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6960 /* Send M_FLUSH according to TPI */ 6961 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6962 } 6963 mp = mi_tpi_ok_ack_alloc(mp); 6964 if (mp) 6965 putnext(tcp->tcp_rq, mp); 6966 } 6967 6968 /* 6969 * Diagnostic routine used to return a string associated with the tcp state. 6970 * Note that if the caller does not supply a buffer, it will use an internal 6971 * static string. This means that if multiple threads call this function at 6972 * the same time, output can be corrupted... Note also that this function 6973 * does not check the size of the supplied buffer. The caller has to make 6974 * sure that it is big enough. 6975 */ 6976 static char * 6977 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6978 { 6979 char buf1[30]; 6980 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6981 char *buf; 6982 char *cp; 6983 in6_addr_t local, remote; 6984 char local_addrbuf[INET6_ADDRSTRLEN]; 6985 char remote_addrbuf[INET6_ADDRSTRLEN]; 6986 6987 if (sup_buf != NULL) 6988 buf = sup_buf; 6989 else 6990 buf = priv_buf; 6991 6992 if (tcp == NULL) 6993 return ("NULL_TCP"); 6994 switch (tcp->tcp_state) { 6995 case TCPS_CLOSED: 6996 cp = "TCP_CLOSED"; 6997 break; 6998 case TCPS_IDLE: 6999 cp = "TCP_IDLE"; 7000 break; 7001 case TCPS_BOUND: 7002 cp = "TCP_BOUND"; 7003 break; 7004 case TCPS_LISTEN: 7005 cp = "TCP_LISTEN"; 7006 break; 7007 case TCPS_SYN_SENT: 7008 cp = "TCP_SYN_SENT"; 7009 break; 7010 case TCPS_SYN_RCVD: 7011 cp = "TCP_SYN_RCVD"; 7012 break; 7013 case TCPS_ESTABLISHED: 7014 cp = "TCP_ESTABLISHED"; 7015 break; 7016 case TCPS_CLOSE_WAIT: 7017 cp = "TCP_CLOSE_WAIT"; 7018 break; 7019 case TCPS_FIN_WAIT_1: 7020 cp = "TCP_FIN_WAIT_1"; 7021 break; 7022 case TCPS_CLOSING: 7023 cp = "TCP_CLOSING"; 7024 break; 7025 case TCPS_LAST_ACK: 7026 cp = "TCP_LAST_ACK"; 7027 break; 7028 case TCPS_FIN_WAIT_2: 7029 cp = "TCP_FIN_WAIT_2"; 7030 break; 7031 case TCPS_TIME_WAIT: 7032 cp = "TCP_TIME_WAIT"; 7033 break; 7034 default: 7035 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7036 cp = buf1; 7037 break; 7038 } 7039 switch (format) { 7040 case DISP_ADDR_AND_PORT: 7041 if (tcp->tcp_ipversion == IPV4_VERSION) { 7042 /* 7043 * Note that we use the remote address in the tcp_b 7044 * structure. This means that it will print out 7045 * the real destination address, not the next hop's 7046 * address if source routing is used. 7047 */ 7048 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7049 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7050 7051 } else { 7052 local = tcp->tcp_ip_src_v6; 7053 remote = tcp->tcp_remote_v6; 7054 } 7055 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7056 sizeof (local_addrbuf)); 7057 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7058 sizeof (remote_addrbuf)); 7059 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7060 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7061 ntohs(tcp->tcp_fport), cp); 7062 break; 7063 case DISP_PORT_ONLY: 7064 default: 7065 (void) mi_sprintf(buf, "[%u, %u] %s", 7066 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7067 break; 7068 } 7069 7070 return (buf); 7071 } 7072 7073 /* 7074 * Called via squeue to get on to eager's perimeter to send a 7075 * TH_RST. The listener wants the eager to disappear either 7076 * by means of tcp_eager_blowoff() or tcp_eager_cleanup() 7077 * being called. 7078 */ 7079 /* ARGSUSED */ 7080 void 7081 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7082 { 7083 conn_t *econnp = (conn_t *)arg; 7084 tcp_t *eager = econnp->conn_tcp; 7085 tcp_t *listener = eager->tcp_listener; 7086 tcp_stack_t *tcps = eager->tcp_tcps; 7087 7088 /* 7089 * We could be called because listener is closing. Since 7090 * the eager is using listener's queue's, its not safe. 7091 * Better use the default queue just to send the TH_RST 7092 * out. 7093 */ 7094 ASSERT(tcps->tcps_g_q != NULL); 7095 eager->tcp_rq = tcps->tcps_g_q; 7096 eager->tcp_wq = WR(tcps->tcps_g_q); 7097 7098 if (eager->tcp_state > TCPS_LISTEN) { 7099 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7100 eager, eager->tcp_snxt, 0, TH_RST); 7101 } 7102 7103 /* We are here because listener wants this eager gone */ 7104 if (listener != NULL) { 7105 mutex_enter(&listener->tcp_eager_lock); 7106 tcp_eager_unlink(eager); 7107 if (eager->tcp_tconnind_started) { 7108 /* 7109 * The eager has sent a conn_ind up to the 7110 * listener but listener decides to close 7111 * instead. We need to drop the extra ref 7112 * placed on eager in tcp_rput_data() before 7113 * sending the conn_ind to listener. 7114 */ 7115 CONN_DEC_REF(econnp); 7116 } 7117 mutex_exit(&listener->tcp_eager_lock); 7118 CONN_DEC_REF(listener->tcp_connp); 7119 } 7120 7121 if (eager->tcp_state > TCPS_BOUND) 7122 tcp_close_detached(eager); 7123 } 7124 7125 /* 7126 * Reset any eager connection hanging off this listener marked 7127 * with 'seqnum' and then reclaim it's resources. 7128 */ 7129 static boolean_t 7130 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7131 { 7132 tcp_t *eager; 7133 mblk_t *mp; 7134 tcp_stack_t *tcps = listener->tcp_tcps; 7135 7136 TCP_STAT(tcps, tcp_eager_blowoff_calls); 7137 eager = listener; 7138 mutex_enter(&listener->tcp_eager_lock); 7139 do { 7140 eager = eager->tcp_eager_next_q; 7141 if (eager == NULL) { 7142 mutex_exit(&listener->tcp_eager_lock); 7143 return (B_FALSE); 7144 } 7145 } while (eager->tcp_conn_req_seqnum != seqnum); 7146 7147 if (eager->tcp_closemp_used > 0) { 7148 mutex_exit(&listener->tcp_eager_lock); 7149 return (B_TRUE); 7150 } 7151 eager->tcp_closemp_used = 1; 7152 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7153 CONN_INC_REF(eager->tcp_connp); 7154 mutex_exit(&listener->tcp_eager_lock); 7155 mp = &eager->tcp_closemp; 7156 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7157 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7158 return (B_TRUE); 7159 } 7160 7161 /* 7162 * Reset any eager connection hanging off this listener 7163 * and then reclaim it's resources. 7164 */ 7165 static void 7166 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7167 { 7168 tcp_t *eager; 7169 mblk_t *mp; 7170 tcp_stack_t *tcps = listener->tcp_tcps; 7171 7172 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7173 7174 if (!q0_only) { 7175 /* First cleanup q */ 7176 TCP_STAT(tcps, tcp_eager_blowoff_q); 7177 eager = listener->tcp_eager_next_q; 7178 while (eager != NULL) { 7179 if (eager->tcp_closemp_used == 0) { 7180 eager->tcp_closemp_used = 1; 7181 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7182 CONN_INC_REF(eager->tcp_connp); 7183 mp = &eager->tcp_closemp; 7184 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7185 tcp_eager_kill, eager->tcp_connp, 7186 SQTAG_TCP_EAGER_CLEANUP); 7187 } 7188 eager = eager->tcp_eager_next_q; 7189 } 7190 } 7191 /* Then cleanup q0 */ 7192 TCP_STAT(tcps, tcp_eager_blowoff_q0); 7193 eager = listener->tcp_eager_next_q0; 7194 while (eager != listener) { 7195 if (eager->tcp_closemp_used == 0) { 7196 eager->tcp_closemp_used = 1; 7197 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7198 CONN_INC_REF(eager->tcp_connp); 7199 mp = &eager->tcp_closemp; 7200 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7201 tcp_eager_kill, eager->tcp_connp, 7202 SQTAG_TCP_EAGER_CLEANUP_Q0); 7203 } 7204 eager = eager->tcp_eager_next_q0; 7205 } 7206 } 7207 7208 /* 7209 * If we are an eager connection hanging off a listener that hasn't 7210 * formally accepted the connection yet, get off his list and blow off 7211 * any data that we have accumulated. 7212 */ 7213 static void 7214 tcp_eager_unlink(tcp_t *tcp) 7215 { 7216 tcp_t *listener = tcp->tcp_listener; 7217 7218 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7219 ASSERT(listener != NULL); 7220 if (tcp->tcp_eager_next_q0 != NULL) { 7221 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7222 7223 /* Remove the eager tcp from q0 */ 7224 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7225 tcp->tcp_eager_prev_q0; 7226 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7227 tcp->tcp_eager_next_q0; 7228 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7229 listener->tcp_conn_req_cnt_q0--; 7230 7231 tcp->tcp_eager_next_q0 = NULL; 7232 tcp->tcp_eager_prev_q0 = NULL; 7233 7234 /* 7235 * Take the eager out, if it is in the list of droppable 7236 * eagers. 7237 */ 7238 MAKE_UNDROPPABLE(tcp); 7239 7240 if (tcp->tcp_syn_rcvd_timeout != 0) { 7241 /* we have timed out before */ 7242 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7243 listener->tcp_syn_rcvd_timeout--; 7244 } 7245 } else { 7246 tcp_t **tcpp = &listener->tcp_eager_next_q; 7247 tcp_t *prev = NULL; 7248 7249 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7250 if (tcpp[0] == tcp) { 7251 if (listener->tcp_eager_last_q == tcp) { 7252 /* 7253 * If we are unlinking the last 7254 * element on the list, adjust 7255 * tail pointer. Set tail pointer 7256 * to nil when list is empty. 7257 */ 7258 ASSERT(tcp->tcp_eager_next_q == NULL); 7259 if (listener->tcp_eager_last_q == 7260 listener->tcp_eager_next_q) { 7261 listener->tcp_eager_last_q = 7262 NULL; 7263 } else { 7264 /* 7265 * We won't get here if there 7266 * is only one eager in the 7267 * list. 7268 */ 7269 ASSERT(prev != NULL); 7270 listener->tcp_eager_last_q = 7271 prev; 7272 } 7273 } 7274 tcpp[0] = tcp->tcp_eager_next_q; 7275 tcp->tcp_eager_next_q = NULL; 7276 tcp->tcp_eager_last_q = NULL; 7277 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7278 listener->tcp_conn_req_cnt_q--; 7279 break; 7280 } 7281 prev = tcpp[0]; 7282 } 7283 } 7284 tcp->tcp_listener = NULL; 7285 } 7286 7287 /* Shorthand to generate and send TPI error acks to our client */ 7288 static void 7289 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7290 { 7291 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7292 putnext(tcp->tcp_rq, mp); 7293 } 7294 7295 /* Shorthand to generate and send TPI error acks to our client */ 7296 static void 7297 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7298 int t_error, int sys_error) 7299 { 7300 struct T_error_ack *teackp; 7301 7302 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7303 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7304 teackp = (struct T_error_ack *)mp->b_rptr; 7305 teackp->ERROR_prim = primitive; 7306 teackp->TLI_error = t_error; 7307 teackp->UNIX_error = sys_error; 7308 putnext(tcp->tcp_rq, mp); 7309 } 7310 } 7311 7312 /* 7313 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7314 * but instead the code relies on: 7315 * - the fact that the address of the array and its size never changes 7316 * - the atomic assignment of the elements of the array 7317 */ 7318 /* ARGSUSED */ 7319 static int 7320 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7321 { 7322 int i; 7323 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7324 7325 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7326 if (tcps->tcps_g_epriv_ports[i] != 0) 7327 (void) mi_mpprintf(mp, "%d ", 7328 tcps->tcps_g_epriv_ports[i]); 7329 } 7330 return (0); 7331 } 7332 7333 /* 7334 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7335 * threads from changing it at the same time. 7336 */ 7337 /* ARGSUSED */ 7338 static int 7339 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7340 cred_t *cr) 7341 { 7342 long new_value; 7343 int i; 7344 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7345 7346 /* 7347 * Fail the request if the new value does not lie within the 7348 * port number limits. 7349 */ 7350 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7351 new_value <= 0 || new_value >= 65536) { 7352 return (EINVAL); 7353 } 7354 7355 mutex_enter(&tcps->tcps_epriv_port_lock); 7356 /* Check if the value is already in the list */ 7357 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7358 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7359 mutex_exit(&tcps->tcps_epriv_port_lock); 7360 return (EEXIST); 7361 } 7362 } 7363 /* Find an empty slot */ 7364 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7365 if (tcps->tcps_g_epriv_ports[i] == 0) 7366 break; 7367 } 7368 if (i == tcps->tcps_g_num_epriv_ports) { 7369 mutex_exit(&tcps->tcps_epriv_port_lock); 7370 return (EOVERFLOW); 7371 } 7372 /* Set the new value */ 7373 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7374 mutex_exit(&tcps->tcps_epriv_port_lock); 7375 return (0); 7376 } 7377 7378 /* 7379 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7380 * threads from changing it at the same time. 7381 */ 7382 /* ARGSUSED */ 7383 static int 7384 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7385 cred_t *cr) 7386 { 7387 long new_value; 7388 int i; 7389 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7390 7391 /* 7392 * Fail the request if the new value does not lie within the 7393 * port number limits. 7394 */ 7395 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7396 new_value >= 65536) { 7397 return (EINVAL); 7398 } 7399 7400 mutex_enter(&tcps->tcps_epriv_port_lock); 7401 /* Check that the value is already in the list */ 7402 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7403 if (tcps->tcps_g_epriv_ports[i] == new_value) 7404 break; 7405 } 7406 if (i == tcps->tcps_g_num_epriv_ports) { 7407 mutex_exit(&tcps->tcps_epriv_port_lock); 7408 return (ESRCH); 7409 } 7410 /* Clear the value */ 7411 tcps->tcps_g_epriv_ports[i] = 0; 7412 mutex_exit(&tcps->tcps_epriv_port_lock); 7413 return (0); 7414 } 7415 7416 /* Return the TPI/TLI equivalent of our current tcp_state */ 7417 static int 7418 tcp_tpistate(tcp_t *tcp) 7419 { 7420 switch (tcp->tcp_state) { 7421 case TCPS_IDLE: 7422 return (TS_UNBND); 7423 case TCPS_LISTEN: 7424 /* 7425 * Return whether there are outstanding T_CONN_IND waiting 7426 * for the matching T_CONN_RES. Therefore don't count q0. 7427 */ 7428 if (tcp->tcp_conn_req_cnt_q > 0) 7429 return (TS_WRES_CIND); 7430 else 7431 return (TS_IDLE); 7432 case TCPS_BOUND: 7433 return (TS_IDLE); 7434 case TCPS_SYN_SENT: 7435 return (TS_WCON_CREQ); 7436 case TCPS_SYN_RCVD: 7437 /* 7438 * Note: assumption: this has to the active open SYN_RCVD. 7439 * The passive instance is detached in SYN_RCVD stage of 7440 * incoming connection processing so we cannot get request 7441 * for T_info_ack on it. 7442 */ 7443 return (TS_WACK_CRES); 7444 case TCPS_ESTABLISHED: 7445 return (TS_DATA_XFER); 7446 case TCPS_CLOSE_WAIT: 7447 return (TS_WREQ_ORDREL); 7448 case TCPS_FIN_WAIT_1: 7449 return (TS_WIND_ORDREL); 7450 case TCPS_FIN_WAIT_2: 7451 return (TS_WIND_ORDREL); 7452 7453 case TCPS_CLOSING: 7454 case TCPS_LAST_ACK: 7455 case TCPS_TIME_WAIT: 7456 case TCPS_CLOSED: 7457 /* 7458 * Following TS_WACK_DREQ7 is a rendition of "not 7459 * yet TS_IDLE" TPI state. There is no best match to any 7460 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7461 * choose a value chosen that will map to TLI/XTI level 7462 * state of TSTATECHNG (state is process of changing) which 7463 * captures what this dummy state represents. 7464 */ 7465 return (TS_WACK_DREQ7); 7466 default: 7467 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7468 tcp->tcp_state, tcp_display(tcp, NULL, 7469 DISP_PORT_ONLY)); 7470 return (TS_UNBND); 7471 } 7472 } 7473 7474 static void 7475 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7476 { 7477 tcp_stack_t *tcps = tcp->tcp_tcps; 7478 7479 if (tcp->tcp_family == AF_INET6) 7480 *tia = tcp_g_t_info_ack_v6; 7481 else 7482 *tia = tcp_g_t_info_ack; 7483 tia->CURRENT_state = tcp_tpistate(tcp); 7484 tia->OPT_size = tcp_max_optsize; 7485 if (tcp->tcp_mss == 0) { 7486 /* Not yet set - tcp_open does not set mss */ 7487 if (tcp->tcp_ipversion == IPV4_VERSION) 7488 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7489 else 7490 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7491 } else { 7492 tia->TIDU_size = tcp->tcp_mss; 7493 } 7494 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7495 } 7496 7497 /* 7498 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7499 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7500 * tcp_g_t_info_ack. The current state of the stream is copied from 7501 * tcp_state. 7502 */ 7503 static void 7504 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7505 { 7506 t_uscalar_t cap_bits1; 7507 struct T_capability_ack *tcap; 7508 7509 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7510 freemsg(mp); 7511 return; 7512 } 7513 7514 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7515 7516 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7517 mp->b_datap->db_type, T_CAPABILITY_ACK); 7518 if (mp == NULL) 7519 return; 7520 7521 tcap = (struct T_capability_ack *)mp->b_rptr; 7522 tcap->CAP_bits1 = 0; 7523 7524 if (cap_bits1 & TC1_INFO) { 7525 tcp_copy_info(&tcap->INFO_ack, tcp); 7526 tcap->CAP_bits1 |= TC1_INFO; 7527 } 7528 7529 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7530 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7531 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7532 } 7533 7534 putnext(tcp->tcp_rq, mp); 7535 } 7536 7537 /* 7538 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7539 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7540 * The current state of the stream is copied from tcp_state. 7541 */ 7542 static void 7543 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7544 { 7545 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7546 T_INFO_ACK); 7547 if (!mp) { 7548 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7549 return; 7550 } 7551 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7552 putnext(tcp->tcp_rq, mp); 7553 } 7554 7555 /* Respond to the TPI addr request */ 7556 static void 7557 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7558 { 7559 sin_t *sin; 7560 mblk_t *ackmp; 7561 struct T_addr_ack *taa; 7562 7563 /* Make it large enough for worst case */ 7564 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7565 2 * sizeof (sin6_t), 1); 7566 if (ackmp == NULL) { 7567 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7568 return; 7569 } 7570 7571 if (tcp->tcp_ipversion == IPV6_VERSION) { 7572 tcp_addr_req_ipv6(tcp, ackmp); 7573 return; 7574 } 7575 taa = (struct T_addr_ack *)ackmp->b_rptr; 7576 7577 bzero(taa, sizeof (struct T_addr_ack)); 7578 ackmp->b_wptr = (uchar_t *)&taa[1]; 7579 7580 taa->PRIM_type = T_ADDR_ACK; 7581 ackmp->b_datap->db_type = M_PCPROTO; 7582 7583 /* 7584 * Note: Following code assumes 32 bit alignment of basic 7585 * data structures like sin_t and struct T_addr_ack. 7586 */ 7587 if (tcp->tcp_state >= TCPS_BOUND) { 7588 /* 7589 * Fill in local address 7590 */ 7591 taa->LOCADDR_length = sizeof (sin_t); 7592 taa->LOCADDR_offset = sizeof (*taa); 7593 7594 sin = (sin_t *)&taa[1]; 7595 7596 /* Fill zeroes and then intialize non-zero fields */ 7597 *sin = sin_null; 7598 7599 sin->sin_family = AF_INET; 7600 7601 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7602 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7603 7604 ackmp->b_wptr = (uchar_t *)&sin[1]; 7605 7606 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7607 /* 7608 * Fill in Remote address 7609 */ 7610 taa->REMADDR_length = sizeof (sin_t); 7611 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7612 taa->LOCADDR_length); 7613 7614 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7615 *sin = sin_null; 7616 sin->sin_family = AF_INET; 7617 sin->sin_addr.s_addr = tcp->tcp_remote; 7618 sin->sin_port = tcp->tcp_fport; 7619 7620 ackmp->b_wptr = (uchar_t *)&sin[1]; 7621 } 7622 } 7623 putnext(tcp->tcp_rq, ackmp); 7624 } 7625 7626 /* Assumes that tcp_addr_req gets enough space and alignment */ 7627 static void 7628 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7629 { 7630 sin6_t *sin6; 7631 struct T_addr_ack *taa; 7632 7633 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7634 ASSERT(OK_32PTR(ackmp->b_rptr)); 7635 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7636 2 * sizeof (sin6_t)); 7637 7638 taa = (struct T_addr_ack *)ackmp->b_rptr; 7639 7640 bzero(taa, sizeof (struct T_addr_ack)); 7641 ackmp->b_wptr = (uchar_t *)&taa[1]; 7642 7643 taa->PRIM_type = T_ADDR_ACK; 7644 ackmp->b_datap->db_type = M_PCPROTO; 7645 7646 /* 7647 * Note: Following code assumes 32 bit alignment of basic 7648 * data structures like sin6_t and struct T_addr_ack. 7649 */ 7650 if (tcp->tcp_state >= TCPS_BOUND) { 7651 /* 7652 * Fill in local address 7653 */ 7654 taa->LOCADDR_length = sizeof (sin6_t); 7655 taa->LOCADDR_offset = sizeof (*taa); 7656 7657 sin6 = (sin6_t *)&taa[1]; 7658 *sin6 = sin6_null; 7659 7660 sin6->sin6_family = AF_INET6; 7661 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7662 sin6->sin6_port = tcp->tcp_lport; 7663 7664 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7665 7666 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7667 /* 7668 * Fill in Remote address 7669 */ 7670 taa->REMADDR_length = sizeof (sin6_t); 7671 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7672 taa->LOCADDR_length); 7673 7674 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7675 *sin6 = sin6_null; 7676 sin6->sin6_family = AF_INET6; 7677 sin6->sin6_flowinfo = 7678 tcp->tcp_ip6h->ip6_vcf & 7679 ~IPV6_VERS_AND_FLOW_MASK; 7680 sin6->sin6_addr = tcp->tcp_remote_v6; 7681 sin6->sin6_port = tcp->tcp_fport; 7682 7683 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7684 } 7685 } 7686 putnext(tcp->tcp_rq, ackmp); 7687 } 7688 7689 /* 7690 * Handle reinitialization of a tcp structure. 7691 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7692 */ 7693 static void 7694 tcp_reinit(tcp_t *tcp) 7695 { 7696 mblk_t *mp; 7697 int err; 7698 tcp_stack_t *tcps = tcp->tcp_tcps; 7699 7700 TCP_STAT(tcps, tcp_reinit_calls); 7701 7702 /* tcp_reinit should never be called for detached tcp_t's */ 7703 ASSERT(tcp->tcp_listener == NULL); 7704 ASSERT((tcp->tcp_family == AF_INET && 7705 tcp->tcp_ipversion == IPV4_VERSION) || 7706 (tcp->tcp_family == AF_INET6 && 7707 (tcp->tcp_ipversion == IPV4_VERSION || 7708 tcp->tcp_ipversion == IPV6_VERSION))); 7709 7710 /* Cancel outstanding timers */ 7711 tcp_timers_stop(tcp); 7712 7713 /* 7714 * Reset everything in the state vector, after updating global 7715 * MIB data from instance counters. 7716 */ 7717 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7718 tcp->tcp_ibsegs = 0; 7719 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7720 tcp->tcp_obsegs = 0; 7721 7722 tcp_close_mpp(&tcp->tcp_xmit_head); 7723 if (tcp->tcp_snd_zcopy_aware) 7724 tcp_zcopy_notify(tcp); 7725 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7726 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7727 mutex_enter(&tcp->tcp_non_sq_lock); 7728 if (tcp->tcp_flow_stopped && 7729 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7730 tcp_clrqfull(tcp); 7731 } 7732 mutex_exit(&tcp->tcp_non_sq_lock); 7733 tcp_close_mpp(&tcp->tcp_reass_head); 7734 tcp->tcp_reass_tail = NULL; 7735 if (tcp->tcp_rcv_list != NULL) { 7736 /* Free b_next chain */ 7737 tcp_close_mpp(&tcp->tcp_rcv_list); 7738 tcp->tcp_rcv_last_head = NULL; 7739 tcp->tcp_rcv_last_tail = NULL; 7740 tcp->tcp_rcv_cnt = 0; 7741 } 7742 tcp->tcp_rcv_last_tail = NULL; 7743 7744 if ((mp = tcp->tcp_urp_mp) != NULL) { 7745 freemsg(mp); 7746 tcp->tcp_urp_mp = NULL; 7747 } 7748 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7749 freemsg(mp); 7750 tcp->tcp_urp_mark_mp = NULL; 7751 } 7752 if (tcp->tcp_fused_sigurg_mp != NULL) { 7753 freeb(tcp->tcp_fused_sigurg_mp); 7754 tcp->tcp_fused_sigurg_mp = NULL; 7755 } 7756 7757 /* 7758 * Following is a union with two members which are 7759 * identical types and size so the following cleanup 7760 * is enough. 7761 */ 7762 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7763 7764 CL_INET_DISCONNECT(tcp); 7765 7766 /* 7767 * The connection can't be on the tcp_time_wait_head list 7768 * since it is not detached. 7769 */ 7770 ASSERT(tcp->tcp_time_wait_next == NULL); 7771 ASSERT(tcp->tcp_time_wait_prev == NULL); 7772 ASSERT(tcp->tcp_time_wait_expire == 0); 7773 7774 if (tcp->tcp_kssl_pending) { 7775 tcp->tcp_kssl_pending = B_FALSE; 7776 7777 /* Don't reset if the initialized by bind. */ 7778 if (tcp->tcp_kssl_ent != NULL) { 7779 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7780 KSSL_NO_PROXY); 7781 } 7782 } 7783 if (tcp->tcp_kssl_ctx != NULL) { 7784 kssl_release_ctx(tcp->tcp_kssl_ctx); 7785 tcp->tcp_kssl_ctx = NULL; 7786 } 7787 7788 /* 7789 * Reset/preserve other values 7790 */ 7791 tcp_reinit_values(tcp); 7792 ipcl_hash_remove(tcp->tcp_connp); 7793 conn_delete_ire(tcp->tcp_connp, NULL); 7794 tcp_ipsec_cleanup(tcp); 7795 7796 if (tcp->tcp_conn_req_max != 0) { 7797 /* 7798 * This is the case when a TLI program uses the same 7799 * transport end point to accept a connection. This 7800 * makes the TCP both a listener and acceptor. When 7801 * this connection is closed, we need to set the state 7802 * back to TCPS_LISTEN. Make sure that the eager list 7803 * is reinitialized. 7804 * 7805 * Note that this stream is still bound to the four 7806 * tuples of the previous connection in IP. If a new 7807 * SYN with different foreign address comes in, IP will 7808 * not find it and will send it to the global queue. In 7809 * the global queue, TCP will do a tcp_lookup_listener() 7810 * to find this stream. This works because this stream 7811 * is only removed from connected hash. 7812 * 7813 */ 7814 tcp->tcp_state = TCPS_LISTEN; 7815 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7816 tcp->tcp_eager_next_drop_q0 = tcp; 7817 tcp->tcp_eager_prev_drop_q0 = tcp; 7818 tcp->tcp_connp->conn_recv = tcp_conn_request; 7819 if (tcp->tcp_family == AF_INET6) { 7820 ASSERT(tcp->tcp_connp->conn_af_isv6); 7821 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7822 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7823 } else { 7824 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7825 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7826 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7827 } 7828 } else { 7829 tcp->tcp_state = TCPS_BOUND; 7830 } 7831 7832 /* 7833 * Initialize to default values 7834 * Can't fail since enough header template space already allocated 7835 * at open(). 7836 */ 7837 err = tcp_init_values(tcp); 7838 ASSERT(err == 0); 7839 /* Restore state in tcp_tcph */ 7840 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7841 if (tcp->tcp_ipversion == IPV4_VERSION) 7842 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7843 else 7844 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7845 /* 7846 * Copy of the src addr. in tcp_t is needed in tcp_t 7847 * since the lookup funcs can only lookup on tcp_t 7848 */ 7849 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7850 7851 ASSERT(tcp->tcp_ptpbhn != NULL); 7852 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7853 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7854 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7855 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7856 } 7857 7858 /* 7859 * Force values to zero that need be zero. 7860 * Do not touch values asociated with the BOUND or LISTEN state 7861 * since the connection will end up in that state after the reinit. 7862 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7863 * structure! 7864 */ 7865 static void 7866 tcp_reinit_values(tcp) 7867 tcp_t *tcp; 7868 { 7869 tcp_stack_t *tcps = tcp->tcp_tcps; 7870 7871 #ifndef lint 7872 #define DONTCARE(x) 7873 #define PRESERVE(x) 7874 #else 7875 #define DONTCARE(x) ((x) = (x)) 7876 #define PRESERVE(x) ((x) = (x)) 7877 #endif /* lint */ 7878 7879 PRESERVE(tcp->tcp_bind_hash); 7880 PRESERVE(tcp->tcp_ptpbhn); 7881 PRESERVE(tcp->tcp_acceptor_hash); 7882 PRESERVE(tcp->tcp_ptpahn); 7883 7884 /* Should be ASSERT NULL on these with new code! */ 7885 ASSERT(tcp->tcp_time_wait_next == NULL); 7886 ASSERT(tcp->tcp_time_wait_prev == NULL); 7887 ASSERT(tcp->tcp_time_wait_expire == 0); 7888 PRESERVE(tcp->tcp_state); 7889 PRESERVE(tcp->tcp_rq); 7890 PRESERVE(tcp->tcp_wq); 7891 7892 ASSERT(tcp->tcp_xmit_head == NULL); 7893 ASSERT(tcp->tcp_xmit_last == NULL); 7894 ASSERT(tcp->tcp_unsent == 0); 7895 ASSERT(tcp->tcp_xmit_tail == NULL); 7896 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7897 7898 tcp->tcp_snxt = 0; /* Displayed in mib */ 7899 tcp->tcp_suna = 0; /* Displayed in mib */ 7900 tcp->tcp_swnd = 0; 7901 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7902 7903 ASSERT(tcp->tcp_ibsegs == 0); 7904 ASSERT(tcp->tcp_obsegs == 0); 7905 7906 if (tcp->tcp_iphc != NULL) { 7907 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7908 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7909 } 7910 7911 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7912 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7913 DONTCARE(tcp->tcp_ipha); 7914 DONTCARE(tcp->tcp_ip6h); 7915 DONTCARE(tcp->tcp_ip_hdr_len); 7916 DONTCARE(tcp->tcp_tcph); 7917 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7918 tcp->tcp_valid_bits = 0; 7919 7920 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7921 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7922 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7923 tcp->tcp_last_rcv_lbolt = 0; 7924 7925 tcp->tcp_init_cwnd = 0; 7926 7927 tcp->tcp_urp_last_valid = 0; 7928 tcp->tcp_hard_binding = 0; 7929 tcp->tcp_hard_bound = 0; 7930 PRESERVE(tcp->tcp_cred); 7931 PRESERVE(tcp->tcp_cpid); 7932 PRESERVE(tcp->tcp_open_time); 7933 PRESERVE(tcp->tcp_exclbind); 7934 7935 tcp->tcp_fin_acked = 0; 7936 tcp->tcp_fin_rcvd = 0; 7937 tcp->tcp_fin_sent = 0; 7938 tcp->tcp_ordrel_done = 0; 7939 7940 tcp->tcp_debug = 0; 7941 tcp->tcp_dontroute = 0; 7942 tcp->tcp_broadcast = 0; 7943 7944 tcp->tcp_useloopback = 0; 7945 tcp->tcp_reuseaddr = 0; 7946 tcp->tcp_oobinline = 0; 7947 tcp->tcp_dgram_errind = 0; 7948 7949 tcp->tcp_detached = 0; 7950 tcp->tcp_bind_pending = 0; 7951 tcp->tcp_unbind_pending = 0; 7952 tcp->tcp_deferred_clean_death = 0; 7953 7954 tcp->tcp_snd_ws_ok = B_FALSE; 7955 tcp->tcp_snd_ts_ok = B_FALSE; 7956 tcp->tcp_linger = 0; 7957 tcp->tcp_ka_enabled = 0; 7958 tcp->tcp_zero_win_probe = 0; 7959 7960 tcp->tcp_loopback = 0; 7961 tcp->tcp_localnet = 0; 7962 tcp->tcp_syn_defense = 0; 7963 tcp->tcp_set_timer = 0; 7964 7965 tcp->tcp_active_open = 0; 7966 ASSERT(tcp->tcp_timeout == B_FALSE); 7967 tcp->tcp_rexmit = B_FALSE; 7968 tcp->tcp_xmit_zc_clean = B_FALSE; 7969 7970 tcp->tcp_snd_sack_ok = B_FALSE; 7971 PRESERVE(tcp->tcp_recvdstaddr); 7972 tcp->tcp_hwcksum = B_FALSE; 7973 7974 tcp->tcp_ire_ill_check_done = B_FALSE; 7975 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7976 7977 tcp->tcp_mdt = B_FALSE; 7978 tcp->tcp_mdt_hdr_head = 0; 7979 tcp->tcp_mdt_hdr_tail = 0; 7980 7981 tcp->tcp_conn_def_q0 = 0; 7982 tcp->tcp_ip_forward_progress = B_FALSE; 7983 tcp->tcp_anon_priv_bind = 0; 7984 tcp->tcp_ecn_ok = B_FALSE; 7985 7986 tcp->tcp_cwr = B_FALSE; 7987 tcp->tcp_ecn_echo_on = B_FALSE; 7988 7989 if (tcp->tcp_sack_info != NULL) { 7990 if (tcp->tcp_notsack_list != NULL) { 7991 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7992 } 7993 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7994 tcp->tcp_sack_info = NULL; 7995 } 7996 7997 tcp->tcp_rcv_ws = 0; 7998 tcp->tcp_snd_ws = 0; 7999 tcp->tcp_ts_recent = 0; 8000 tcp->tcp_rnxt = 0; /* Displayed in mib */ 8001 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 8002 tcp->tcp_if_mtu = 0; 8003 8004 ASSERT(tcp->tcp_reass_head == NULL); 8005 ASSERT(tcp->tcp_reass_tail == NULL); 8006 8007 tcp->tcp_cwnd_cnt = 0; 8008 8009 ASSERT(tcp->tcp_rcv_list == NULL); 8010 ASSERT(tcp->tcp_rcv_last_head == NULL); 8011 ASSERT(tcp->tcp_rcv_last_tail == NULL); 8012 ASSERT(tcp->tcp_rcv_cnt == 0); 8013 8014 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8015 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8016 tcp->tcp_csuna = 0; 8017 8018 tcp->tcp_rto = 0; /* Displayed in MIB */ 8019 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8020 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8021 tcp->tcp_rtt_update = 0; 8022 8023 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8024 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8025 8026 tcp->tcp_rack = 0; /* Displayed in mib */ 8027 tcp->tcp_rack_cnt = 0; 8028 tcp->tcp_rack_cur_max = 0; 8029 tcp->tcp_rack_abs_max = 0; 8030 8031 tcp->tcp_max_swnd = 0; 8032 8033 ASSERT(tcp->tcp_listener == NULL); 8034 8035 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8036 8037 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8038 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8039 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8040 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8041 8042 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8043 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8044 PRESERVE(tcp->tcp_conn_req_max); 8045 PRESERVE(tcp->tcp_conn_req_seqnum); 8046 8047 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8048 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8049 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8050 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8051 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8052 8053 tcp->tcp_lingertime = 0; 8054 8055 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8056 ASSERT(tcp->tcp_urp_mp == NULL); 8057 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8058 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8059 8060 ASSERT(tcp->tcp_eager_next_q == NULL); 8061 ASSERT(tcp->tcp_eager_last_q == NULL); 8062 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8063 tcp->tcp_eager_prev_q0 == NULL) || 8064 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8065 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8066 8067 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8068 tcp->tcp_eager_prev_drop_q0 == NULL) || 8069 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8070 8071 tcp->tcp_client_errno = 0; 8072 8073 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8074 8075 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8076 8077 PRESERVE(tcp->tcp_bound_source_v6); 8078 tcp->tcp_last_sent_len = 0; 8079 tcp->tcp_dupack_cnt = 0; 8080 8081 tcp->tcp_fport = 0; /* Displayed in MIB */ 8082 PRESERVE(tcp->tcp_lport); 8083 8084 PRESERVE(tcp->tcp_acceptor_lockp); 8085 8086 ASSERT(tcp->tcp_ordrelid == 0); 8087 PRESERVE(tcp->tcp_acceptor_id); 8088 DONTCARE(tcp->tcp_ipsec_overhead); 8089 8090 /* 8091 * If tcp_tracing flag is ON (i.e. We have a trace buffer 8092 * in tcp structure and now tracing), Re-initialize all 8093 * members of tcp_traceinfo. 8094 */ 8095 if (tcp->tcp_tracebuf != NULL) { 8096 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 8097 } 8098 8099 PRESERVE(tcp->tcp_family); 8100 if (tcp->tcp_family == AF_INET6) { 8101 tcp->tcp_ipversion = IPV6_VERSION; 8102 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8103 } else { 8104 tcp->tcp_ipversion = IPV4_VERSION; 8105 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8106 } 8107 8108 tcp->tcp_bound_if = 0; 8109 tcp->tcp_ipv6_recvancillary = 0; 8110 tcp->tcp_recvifindex = 0; 8111 tcp->tcp_recvhops = 0; 8112 tcp->tcp_closed = 0; 8113 tcp->tcp_cleandeathtag = 0; 8114 if (tcp->tcp_hopopts != NULL) { 8115 mi_free(tcp->tcp_hopopts); 8116 tcp->tcp_hopopts = NULL; 8117 tcp->tcp_hopoptslen = 0; 8118 } 8119 ASSERT(tcp->tcp_hopoptslen == 0); 8120 if (tcp->tcp_dstopts != NULL) { 8121 mi_free(tcp->tcp_dstopts); 8122 tcp->tcp_dstopts = NULL; 8123 tcp->tcp_dstoptslen = 0; 8124 } 8125 ASSERT(tcp->tcp_dstoptslen == 0); 8126 if (tcp->tcp_rtdstopts != NULL) { 8127 mi_free(tcp->tcp_rtdstopts); 8128 tcp->tcp_rtdstopts = NULL; 8129 tcp->tcp_rtdstoptslen = 0; 8130 } 8131 ASSERT(tcp->tcp_rtdstoptslen == 0); 8132 if (tcp->tcp_rthdr != NULL) { 8133 mi_free(tcp->tcp_rthdr); 8134 tcp->tcp_rthdr = NULL; 8135 tcp->tcp_rthdrlen = 0; 8136 } 8137 ASSERT(tcp->tcp_rthdrlen == 0); 8138 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8139 8140 /* Reset fusion-related fields */ 8141 tcp->tcp_fused = B_FALSE; 8142 tcp->tcp_unfusable = B_FALSE; 8143 tcp->tcp_fused_sigurg = B_FALSE; 8144 tcp->tcp_direct_sockfs = B_FALSE; 8145 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8146 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8147 tcp->tcp_loopback_peer = NULL; 8148 tcp->tcp_fuse_rcv_hiwater = 0; 8149 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8150 tcp->tcp_fuse_rcv_unread_cnt = 0; 8151 8152 tcp->tcp_lso = B_FALSE; 8153 8154 tcp->tcp_in_ack_unsent = 0; 8155 tcp->tcp_cork = B_FALSE; 8156 tcp->tcp_tconnind_started = B_FALSE; 8157 8158 PRESERVE(tcp->tcp_squeue_bytes); 8159 8160 ASSERT(tcp->tcp_kssl_ctx == NULL); 8161 ASSERT(!tcp->tcp_kssl_pending); 8162 PRESERVE(tcp->tcp_kssl_ent); 8163 8164 tcp->tcp_closemp_used = 0; 8165 8166 #ifdef DEBUG 8167 DONTCARE(tcp->tcmp_stk[0]); 8168 #endif 8169 8170 8171 #undef DONTCARE 8172 #undef PRESERVE 8173 } 8174 8175 /* 8176 * Allocate necessary resources and initialize state vector. 8177 * Guaranteed not to fail so that when an error is returned, 8178 * the caller doesn't need to do any additional cleanup. 8179 */ 8180 int 8181 tcp_init(tcp_t *tcp, queue_t *q) 8182 { 8183 int err; 8184 8185 tcp->tcp_rq = q; 8186 tcp->tcp_wq = WR(q); 8187 tcp->tcp_state = TCPS_IDLE; 8188 if ((err = tcp_init_values(tcp)) != 0) 8189 tcp_timers_stop(tcp); 8190 return (err); 8191 } 8192 8193 static int 8194 tcp_init_values(tcp_t *tcp) 8195 { 8196 int err; 8197 tcp_stack_t *tcps = tcp->tcp_tcps; 8198 8199 ASSERT((tcp->tcp_family == AF_INET && 8200 tcp->tcp_ipversion == IPV4_VERSION) || 8201 (tcp->tcp_family == AF_INET6 && 8202 (tcp->tcp_ipversion == IPV4_VERSION || 8203 tcp->tcp_ipversion == IPV6_VERSION))); 8204 8205 /* 8206 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8207 * will be close to tcp_rexmit_interval_initial. By doing this, we 8208 * allow the algorithm to adjust slowly to large fluctuations of RTT 8209 * during first few transmissions of a connection as seen in slow 8210 * links. 8211 */ 8212 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8213 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8214 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8215 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8216 tcps->tcps_conn_grace_period; 8217 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8218 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8219 tcp->tcp_timer_backoff = 0; 8220 tcp->tcp_ms_we_have_waited = 0; 8221 tcp->tcp_last_recv_time = lbolt; 8222 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8223 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8224 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8225 8226 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8227 8228 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8229 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8230 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8231 /* 8232 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8233 * passive open. 8234 */ 8235 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8236 8237 tcp->tcp_naglim = tcps->tcps_naglim_def; 8238 8239 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8240 8241 tcp->tcp_mdt_hdr_head = 0; 8242 tcp->tcp_mdt_hdr_tail = 0; 8243 8244 /* Reset fusion-related fields */ 8245 tcp->tcp_fused = B_FALSE; 8246 tcp->tcp_unfusable = B_FALSE; 8247 tcp->tcp_fused_sigurg = B_FALSE; 8248 tcp->tcp_direct_sockfs = B_FALSE; 8249 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8250 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8251 tcp->tcp_loopback_peer = NULL; 8252 tcp->tcp_fuse_rcv_hiwater = 0; 8253 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8254 tcp->tcp_fuse_rcv_unread_cnt = 0; 8255 8256 /* Initialize the header template */ 8257 if (tcp->tcp_ipversion == IPV4_VERSION) { 8258 err = tcp_header_init_ipv4(tcp); 8259 } else { 8260 err = tcp_header_init_ipv6(tcp); 8261 } 8262 if (err) 8263 return (err); 8264 8265 /* 8266 * Init the window scale to the max so tcp_rwnd_set() won't pare 8267 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8268 */ 8269 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8270 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8271 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8272 8273 tcp->tcp_cork = B_FALSE; 8274 /* 8275 * Init the tcp_debug option. This value determines whether TCP 8276 * calls strlog() to print out debug messages. Doing this 8277 * initialization here means that this value is not inherited thru 8278 * tcp_reinit(). 8279 */ 8280 tcp->tcp_debug = tcps->tcps_dbg; 8281 8282 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8283 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8284 8285 return (0); 8286 } 8287 8288 /* 8289 * Initialize the IPv4 header. Loses any record of any IP options. 8290 */ 8291 static int 8292 tcp_header_init_ipv4(tcp_t *tcp) 8293 { 8294 tcph_t *tcph; 8295 uint32_t sum; 8296 conn_t *connp; 8297 tcp_stack_t *tcps = tcp->tcp_tcps; 8298 8299 /* 8300 * This is a simple initialization. If there's 8301 * already a template, it should never be too small, 8302 * so reuse it. Otherwise, allocate space for the new one. 8303 */ 8304 if (tcp->tcp_iphc == NULL) { 8305 ASSERT(tcp->tcp_iphc_len == 0); 8306 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8307 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8308 if (tcp->tcp_iphc == NULL) { 8309 tcp->tcp_iphc_len = 0; 8310 return (ENOMEM); 8311 } 8312 } 8313 8314 /* options are gone; may need a new label */ 8315 connp = tcp->tcp_connp; 8316 connp->conn_mlp_type = mlptSingle; 8317 connp->conn_ulp_labeled = !is_system_labeled(); 8318 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8319 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8320 tcp->tcp_ip6h = NULL; 8321 tcp->tcp_ipversion = IPV4_VERSION; 8322 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8323 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8324 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8325 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8326 tcp->tcp_ipha->ipha_version_and_hdr_length 8327 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8328 tcp->tcp_ipha->ipha_ident = 0; 8329 8330 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8331 tcp->tcp_tos = 0; 8332 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8333 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8334 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8335 8336 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8337 tcp->tcp_tcph = tcph; 8338 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8339 /* 8340 * IP wants our header length in the checksum field to 8341 * allow it to perform a single pseudo-header+checksum 8342 * calculation on behalf of TCP. 8343 * Include the adjustment for a source route once IP_OPTIONS is set. 8344 */ 8345 sum = sizeof (tcph_t) + tcp->tcp_sum; 8346 sum = (sum >> 16) + (sum & 0xFFFF); 8347 U16_TO_ABE16(sum, tcph->th_sum); 8348 return (0); 8349 } 8350 8351 /* 8352 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8353 */ 8354 static int 8355 tcp_header_init_ipv6(tcp_t *tcp) 8356 { 8357 tcph_t *tcph; 8358 uint32_t sum; 8359 conn_t *connp; 8360 tcp_stack_t *tcps = tcp->tcp_tcps; 8361 8362 /* 8363 * This is a simple initialization. If there's 8364 * already a template, it should never be too small, 8365 * so reuse it. Otherwise, allocate space for the new one. 8366 * Ensure that there is enough space to "downgrade" the tcp_t 8367 * to an IPv4 tcp_t. This requires having space for a full load 8368 * of IPv4 options, as well as a full load of TCP options 8369 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8370 * than a v6 header and a TCP header with a full load of TCP options 8371 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8372 * We want to avoid reallocation in the "downgraded" case when 8373 * processing outbound IPv4 options. 8374 */ 8375 if (tcp->tcp_iphc == NULL) { 8376 ASSERT(tcp->tcp_iphc_len == 0); 8377 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8378 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8379 if (tcp->tcp_iphc == NULL) { 8380 tcp->tcp_iphc_len = 0; 8381 return (ENOMEM); 8382 } 8383 } 8384 8385 /* options are gone; may need a new label */ 8386 connp = tcp->tcp_connp; 8387 connp->conn_mlp_type = mlptSingle; 8388 connp->conn_ulp_labeled = !is_system_labeled(); 8389 8390 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8391 tcp->tcp_ipversion = IPV6_VERSION; 8392 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8393 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8394 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8395 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8396 tcp->tcp_ipha = NULL; 8397 8398 /* Initialize the header template */ 8399 8400 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8401 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8402 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8403 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8404 8405 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8406 tcp->tcp_tcph = tcph; 8407 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8408 /* 8409 * IP wants our header length in the checksum field to 8410 * allow it to perform a single psuedo-header+checksum 8411 * calculation on behalf of TCP. 8412 * Include the adjustment for a source route when IPV6_RTHDR is set. 8413 */ 8414 sum = sizeof (tcph_t) + tcp->tcp_sum; 8415 sum = (sum >> 16) + (sum & 0xFFFF); 8416 U16_TO_ABE16(sum, tcph->th_sum); 8417 return (0); 8418 } 8419 8420 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8421 #define ICMP_MIN_TCP_HDR 8 8422 8423 /* 8424 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8425 * passed up by IP. The message is always received on the correct tcp_t. 8426 * Assumes that IP has pulled up everything up to and including the ICMP header. 8427 */ 8428 void 8429 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8430 { 8431 icmph_t *icmph; 8432 ipha_t *ipha; 8433 int iph_hdr_length; 8434 tcph_t *tcph; 8435 boolean_t ipsec_mctl = B_FALSE; 8436 boolean_t secure; 8437 mblk_t *first_mp = mp; 8438 uint32_t new_mss; 8439 uint32_t ratio; 8440 size_t mp_size = MBLKL(mp); 8441 uint32_t seg_seq; 8442 tcp_stack_t *tcps = tcp->tcp_tcps; 8443 8444 /* Assume IP provides aligned packets - otherwise toss */ 8445 if (!OK_32PTR(mp->b_rptr)) { 8446 freemsg(mp); 8447 return; 8448 } 8449 8450 /* 8451 * Since ICMP errors are normal data marked with M_CTL when sent 8452 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8453 * packets starting with an ipsec_info_t, see ipsec_info.h. 8454 */ 8455 if ((mp_size == sizeof (ipsec_info_t)) && 8456 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8457 ASSERT(mp->b_cont != NULL); 8458 mp = mp->b_cont; 8459 /* IP should have done this */ 8460 ASSERT(OK_32PTR(mp->b_rptr)); 8461 mp_size = MBLKL(mp); 8462 ipsec_mctl = B_TRUE; 8463 } 8464 8465 /* 8466 * Verify that we have a complete outer IP header. If not, drop it. 8467 */ 8468 if (mp_size < sizeof (ipha_t)) { 8469 noticmpv4: 8470 freemsg(first_mp); 8471 return; 8472 } 8473 8474 ipha = (ipha_t *)mp->b_rptr; 8475 /* 8476 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8477 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8478 */ 8479 switch (IPH_HDR_VERSION(ipha)) { 8480 case IPV6_VERSION: 8481 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8482 return; 8483 case IPV4_VERSION: 8484 break; 8485 default: 8486 goto noticmpv4; 8487 } 8488 8489 /* Skip past the outer IP and ICMP headers */ 8490 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8491 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8492 /* 8493 * If we don't have the correct outer IP header length or if the ULP 8494 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8495 * send it upstream. 8496 */ 8497 if (iph_hdr_length < sizeof (ipha_t) || 8498 ipha->ipha_protocol != IPPROTO_ICMP || 8499 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8500 goto noticmpv4; 8501 } 8502 ipha = (ipha_t *)&icmph[1]; 8503 8504 /* Skip past the inner IP and find the ULP header */ 8505 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8506 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8507 /* 8508 * If we don't have the correct inner IP header length or if the ULP 8509 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8510 * bytes of TCP header, drop it. 8511 */ 8512 if (iph_hdr_length < sizeof (ipha_t) || 8513 ipha->ipha_protocol != IPPROTO_TCP || 8514 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8515 goto noticmpv4; 8516 } 8517 8518 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8519 if (ipsec_mctl) { 8520 secure = ipsec_in_is_secure(first_mp); 8521 } else { 8522 secure = B_FALSE; 8523 } 8524 if (secure) { 8525 /* 8526 * If we are willing to accept this in clear 8527 * we don't have to verify policy. 8528 */ 8529 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8530 if (!tcp_check_policy(tcp, first_mp, 8531 ipha, NULL, secure, ipsec_mctl)) { 8532 /* 8533 * tcp_check_policy called 8534 * ip_drop_packet() on failure. 8535 */ 8536 return; 8537 } 8538 } 8539 } 8540 } else if (ipsec_mctl) { 8541 /* 8542 * This is a hard_bound connection. IP has already 8543 * verified policy. We don't have to do it again. 8544 */ 8545 freeb(first_mp); 8546 first_mp = mp; 8547 ipsec_mctl = B_FALSE; 8548 } 8549 8550 seg_seq = ABE32_TO_U32(tcph->th_seq); 8551 /* 8552 * TCP SHOULD check that the TCP sequence number contained in 8553 * payload of the ICMP error message is within the range 8554 * SND.UNA <= SEG.SEQ < SND.NXT. 8555 */ 8556 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8557 /* 8558 * If the ICMP message is bogus, should we kill the 8559 * connection, or should we just drop the bogus ICMP 8560 * message? It would probably make more sense to just 8561 * drop the message so that if this one managed to get 8562 * in, the real connection should not suffer. 8563 */ 8564 goto noticmpv4; 8565 } 8566 8567 switch (icmph->icmph_type) { 8568 case ICMP_DEST_UNREACHABLE: 8569 switch (icmph->icmph_code) { 8570 case ICMP_FRAGMENTATION_NEEDED: 8571 /* 8572 * Reduce the MSS based on the new MTU. This will 8573 * eliminate any fragmentation locally. 8574 * N.B. There may well be some funny side-effects on 8575 * the local send policy and the remote receive policy. 8576 * Pending further research, we provide 8577 * tcp_ignore_path_mtu just in case this proves 8578 * disastrous somewhere. 8579 * 8580 * After updating the MSS, retransmit part of the 8581 * dropped segment using the new mss by calling 8582 * tcp_wput_data(). Need to adjust all those 8583 * params to make sure tcp_wput_data() work properly. 8584 */ 8585 if (tcps->tcps_ignore_path_mtu) 8586 break; 8587 8588 /* 8589 * Decrease the MSS by time stamp options 8590 * IP options and IPSEC options. tcp_hdr_len 8591 * includes time stamp option and IP option 8592 * length. 8593 */ 8594 8595 new_mss = ntohs(icmph->icmph_du_mtu) - 8596 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8597 8598 /* 8599 * Only update the MSS if the new one is 8600 * smaller than the previous one. This is 8601 * to avoid problems when getting multiple 8602 * ICMP errors for the same MTU. 8603 */ 8604 if (new_mss >= tcp->tcp_mss) 8605 break; 8606 8607 /* 8608 * Stop doing PMTU if new_mss is less than 68 8609 * or less than tcp_mss_min. 8610 * The value 68 comes from rfc 1191. 8611 */ 8612 if (new_mss < MAX(68, tcps->tcps_mss_min)) 8613 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8614 0; 8615 8616 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8617 ASSERT(ratio >= 1); 8618 tcp_mss_set(tcp, new_mss, B_TRUE); 8619 8620 /* 8621 * Make sure we have something to 8622 * send. 8623 */ 8624 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8625 (tcp->tcp_xmit_head != NULL)) { 8626 /* 8627 * Shrink tcp_cwnd in 8628 * proportion to the old MSS/new MSS. 8629 */ 8630 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8631 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8632 (tcp->tcp_unsent == 0)) { 8633 tcp->tcp_rexmit_max = tcp->tcp_fss; 8634 } else { 8635 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8636 } 8637 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8638 tcp->tcp_rexmit = B_TRUE; 8639 tcp->tcp_dupack_cnt = 0; 8640 tcp->tcp_snd_burst = TCP_CWND_SS; 8641 tcp_ss_rexmit(tcp); 8642 } 8643 break; 8644 case ICMP_PORT_UNREACHABLE: 8645 case ICMP_PROTOCOL_UNREACHABLE: 8646 switch (tcp->tcp_state) { 8647 case TCPS_SYN_SENT: 8648 case TCPS_SYN_RCVD: 8649 /* 8650 * ICMP can snipe away incipient 8651 * TCP connections as long as 8652 * seq number is same as initial 8653 * send seq number. 8654 */ 8655 if (seg_seq == tcp->tcp_iss) { 8656 (void) tcp_clean_death(tcp, 8657 ECONNREFUSED, 6); 8658 } 8659 break; 8660 } 8661 break; 8662 case ICMP_HOST_UNREACHABLE: 8663 case ICMP_NET_UNREACHABLE: 8664 /* Record the error in case we finally time out. */ 8665 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8666 tcp->tcp_client_errno = EHOSTUNREACH; 8667 else 8668 tcp->tcp_client_errno = ENETUNREACH; 8669 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8670 if (tcp->tcp_listener != NULL && 8671 tcp->tcp_listener->tcp_syn_defense) { 8672 /* 8673 * Ditch the half-open connection if we 8674 * suspect a SYN attack is under way. 8675 */ 8676 tcp_ip_ire_mark_advice(tcp); 8677 (void) tcp_clean_death(tcp, 8678 tcp->tcp_client_errno, 7); 8679 } 8680 } 8681 break; 8682 default: 8683 break; 8684 } 8685 break; 8686 case ICMP_SOURCE_QUENCH: { 8687 /* 8688 * use a global boolean to control 8689 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8690 * The default is false. 8691 */ 8692 if (tcp_icmp_source_quench) { 8693 /* 8694 * Reduce the sending rate as if we got a 8695 * retransmit timeout 8696 */ 8697 uint32_t npkt; 8698 8699 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8700 tcp->tcp_mss; 8701 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8702 tcp->tcp_cwnd = tcp->tcp_mss; 8703 tcp->tcp_cwnd_cnt = 0; 8704 } 8705 break; 8706 } 8707 } 8708 freemsg(first_mp); 8709 } 8710 8711 /* 8712 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8713 * error messages passed up by IP. 8714 * Assumes that IP has pulled up all the extension headers as well 8715 * as the ICMPv6 header. 8716 */ 8717 static void 8718 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8719 { 8720 icmp6_t *icmp6; 8721 ip6_t *ip6h; 8722 uint16_t iph_hdr_length; 8723 tcpha_t *tcpha; 8724 uint8_t *nexthdrp; 8725 uint32_t new_mss; 8726 uint32_t ratio; 8727 boolean_t secure; 8728 mblk_t *first_mp = mp; 8729 size_t mp_size; 8730 uint32_t seg_seq; 8731 tcp_stack_t *tcps = tcp->tcp_tcps; 8732 8733 /* 8734 * The caller has determined if this is an IPSEC_IN packet and 8735 * set ipsec_mctl appropriately (see tcp_icmp_error). 8736 */ 8737 if (ipsec_mctl) 8738 mp = mp->b_cont; 8739 8740 mp_size = MBLKL(mp); 8741 8742 /* 8743 * Verify that we have a complete IP header. If not, send it upstream. 8744 */ 8745 if (mp_size < sizeof (ip6_t)) { 8746 noticmpv6: 8747 freemsg(first_mp); 8748 return; 8749 } 8750 8751 /* 8752 * Verify this is an ICMPV6 packet, else send it upstream. 8753 */ 8754 ip6h = (ip6_t *)mp->b_rptr; 8755 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8756 iph_hdr_length = IPV6_HDR_LEN; 8757 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8758 &nexthdrp) || 8759 *nexthdrp != IPPROTO_ICMPV6) { 8760 goto noticmpv6; 8761 } 8762 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8763 ip6h = (ip6_t *)&icmp6[1]; 8764 /* 8765 * Verify if we have a complete ICMP and inner IP header. 8766 */ 8767 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8768 goto noticmpv6; 8769 8770 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8771 goto noticmpv6; 8772 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8773 /* 8774 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8775 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8776 * packet. 8777 */ 8778 if ((*nexthdrp != IPPROTO_TCP) || 8779 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8780 goto noticmpv6; 8781 } 8782 8783 /* 8784 * ICMP errors come on the right queue or come on 8785 * listener/global queue for detached connections and 8786 * get switched to the right queue. If it comes on the 8787 * right queue, policy check has already been done by IP 8788 * and thus free the first_mp without verifying the policy. 8789 * If it has come for a non-hard bound connection, we need 8790 * to verify policy as IP may not have done it. 8791 */ 8792 if (!tcp->tcp_hard_bound) { 8793 if (ipsec_mctl) { 8794 secure = ipsec_in_is_secure(first_mp); 8795 } else { 8796 secure = B_FALSE; 8797 } 8798 if (secure) { 8799 /* 8800 * If we are willing to accept this in clear 8801 * we don't have to verify policy. 8802 */ 8803 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8804 if (!tcp_check_policy(tcp, first_mp, 8805 NULL, ip6h, secure, ipsec_mctl)) { 8806 /* 8807 * tcp_check_policy called 8808 * ip_drop_packet() on failure. 8809 */ 8810 return; 8811 } 8812 } 8813 } 8814 } else if (ipsec_mctl) { 8815 /* 8816 * This is a hard_bound connection. IP has already 8817 * verified policy. We don't have to do it again. 8818 */ 8819 freeb(first_mp); 8820 first_mp = mp; 8821 ipsec_mctl = B_FALSE; 8822 } 8823 8824 seg_seq = ntohl(tcpha->tha_seq); 8825 /* 8826 * TCP SHOULD check that the TCP sequence number contained in 8827 * payload of the ICMP error message is within the range 8828 * SND.UNA <= SEG.SEQ < SND.NXT. 8829 */ 8830 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8831 /* 8832 * If the ICMP message is bogus, should we kill the 8833 * connection, or should we just drop the bogus ICMP 8834 * message? It would probably make more sense to just 8835 * drop the message so that if this one managed to get 8836 * in, the real connection should not suffer. 8837 */ 8838 goto noticmpv6; 8839 } 8840 8841 switch (icmp6->icmp6_type) { 8842 case ICMP6_PACKET_TOO_BIG: 8843 /* 8844 * Reduce the MSS based on the new MTU. This will 8845 * eliminate any fragmentation locally. 8846 * N.B. There may well be some funny side-effects on 8847 * the local send policy and the remote receive policy. 8848 * Pending further research, we provide 8849 * tcp_ignore_path_mtu just in case this proves 8850 * disastrous somewhere. 8851 * 8852 * After updating the MSS, retransmit part of the 8853 * dropped segment using the new mss by calling 8854 * tcp_wput_data(). Need to adjust all those 8855 * params to make sure tcp_wput_data() work properly. 8856 */ 8857 if (tcps->tcps_ignore_path_mtu) 8858 break; 8859 8860 /* 8861 * Decrease the MSS by time stamp options 8862 * IP options and IPSEC options. tcp_hdr_len 8863 * includes time stamp option and IP option 8864 * length. 8865 */ 8866 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8867 tcp->tcp_ipsec_overhead; 8868 8869 /* 8870 * Only update the MSS if the new one is 8871 * smaller than the previous one. This is 8872 * to avoid problems when getting multiple 8873 * ICMP errors for the same MTU. 8874 */ 8875 if (new_mss >= tcp->tcp_mss) 8876 break; 8877 8878 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8879 ASSERT(ratio >= 1); 8880 tcp_mss_set(tcp, new_mss, B_TRUE); 8881 8882 /* 8883 * Make sure we have something to 8884 * send. 8885 */ 8886 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8887 (tcp->tcp_xmit_head != NULL)) { 8888 /* 8889 * Shrink tcp_cwnd in 8890 * proportion to the old MSS/new MSS. 8891 */ 8892 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8893 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8894 (tcp->tcp_unsent == 0)) { 8895 tcp->tcp_rexmit_max = tcp->tcp_fss; 8896 } else { 8897 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8898 } 8899 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8900 tcp->tcp_rexmit = B_TRUE; 8901 tcp->tcp_dupack_cnt = 0; 8902 tcp->tcp_snd_burst = TCP_CWND_SS; 8903 tcp_ss_rexmit(tcp); 8904 } 8905 break; 8906 8907 case ICMP6_DST_UNREACH: 8908 switch (icmp6->icmp6_code) { 8909 case ICMP6_DST_UNREACH_NOPORT: 8910 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8911 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8912 (seg_seq == tcp->tcp_iss)) { 8913 (void) tcp_clean_death(tcp, 8914 ECONNREFUSED, 8); 8915 } 8916 break; 8917 8918 case ICMP6_DST_UNREACH_ADMIN: 8919 case ICMP6_DST_UNREACH_NOROUTE: 8920 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8921 case ICMP6_DST_UNREACH_ADDR: 8922 /* Record the error in case we finally time out. */ 8923 tcp->tcp_client_errno = EHOSTUNREACH; 8924 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8925 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8926 (seg_seq == tcp->tcp_iss)) { 8927 if (tcp->tcp_listener != NULL && 8928 tcp->tcp_listener->tcp_syn_defense) { 8929 /* 8930 * Ditch the half-open connection if we 8931 * suspect a SYN attack is under way. 8932 */ 8933 tcp_ip_ire_mark_advice(tcp); 8934 (void) tcp_clean_death(tcp, 8935 tcp->tcp_client_errno, 9); 8936 } 8937 } 8938 8939 8940 break; 8941 default: 8942 break; 8943 } 8944 break; 8945 8946 case ICMP6_PARAM_PROB: 8947 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8948 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8949 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8950 (uchar_t *)nexthdrp) { 8951 if (tcp->tcp_state == TCPS_SYN_SENT || 8952 tcp->tcp_state == TCPS_SYN_RCVD) { 8953 (void) tcp_clean_death(tcp, 8954 ECONNREFUSED, 10); 8955 } 8956 break; 8957 } 8958 break; 8959 8960 case ICMP6_TIME_EXCEEDED: 8961 default: 8962 break; 8963 } 8964 freemsg(first_mp); 8965 } 8966 8967 /* 8968 * IP recognizes seven kinds of bind requests: 8969 * 8970 * - A zero-length address binds only to the protocol number. 8971 * 8972 * - A 4-byte address is treated as a request to 8973 * validate that the address is a valid local IPv4 8974 * address, appropriate for an application to bind to. 8975 * IP does the verification, but does not make any note 8976 * of the address at this time. 8977 * 8978 * - A 16-byte address contains is treated as a request 8979 * to validate a local IPv6 address, as the 4-byte 8980 * address case above. 8981 * 8982 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 8983 * use it for the inbound fanout of packets. 8984 * 8985 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 8986 * use it for the inbound fanout of packets. 8987 * 8988 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 8989 * information consisting of local and remote addresses 8990 * and ports. In this case, the addresses are both 8991 * validated as appropriate for this operation, and, if 8992 * so, the information is retained for use in the 8993 * inbound fanout. 8994 * 8995 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 8996 * fanout information, like the 12-byte case above. 8997 * 8998 * IP will also fill in the IRE request mblk with information 8999 * regarding our peer. In all cases, we notify IP of our protocol 9000 * type by appending a single protocol byte to the bind request. 9001 */ 9002 static mblk_t * 9003 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 9004 { 9005 char *cp; 9006 mblk_t *mp; 9007 struct T_bind_req *tbr; 9008 ipa_conn_t *ac; 9009 ipa6_conn_t *ac6; 9010 sin_t *sin; 9011 sin6_t *sin6; 9012 9013 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9014 ASSERT((tcp->tcp_family == AF_INET && 9015 tcp->tcp_ipversion == IPV4_VERSION) || 9016 (tcp->tcp_family == AF_INET6 && 9017 (tcp->tcp_ipversion == IPV4_VERSION || 9018 tcp->tcp_ipversion == IPV6_VERSION))); 9019 9020 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9021 if (!mp) 9022 return (mp); 9023 mp->b_datap->db_type = M_PROTO; 9024 tbr = (struct T_bind_req *)mp->b_rptr; 9025 tbr->PRIM_type = bind_prim; 9026 tbr->ADDR_offset = sizeof (*tbr); 9027 tbr->CONIND_number = 0; 9028 tbr->ADDR_length = addr_length; 9029 cp = (char *)&tbr[1]; 9030 switch (addr_length) { 9031 case sizeof (ipa_conn_t): 9032 ASSERT(tcp->tcp_family == AF_INET); 9033 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9034 9035 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9036 if (mp->b_cont == NULL) { 9037 freemsg(mp); 9038 return (NULL); 9039 } 9040 mp->b_cont->b_wptr += sizeof (ire_t); 9041 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9042 9043 /* cp known to be 32 bit aligned */ 9044 ac = (ipa_conn_t *)cp; 9045 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9046 ac->ac_faddr = tcp->tcp_remote; 9047 ac->ac_fport = tcp->tcp_fport; 9048 ac->ac_lport = tcp->tcp_lport; 9049 tcp->tcp_hard_binding = 1; 9050 break; 9051 9052 case sizeof (ipa6_conn_t): 9053 ASSERT(tcp->tcp_family == AF_INET6); 9054 9055 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9056 if (mp->b_cont == NULL) { 9057 freemsg(mp); 9058 return (NULL); 9059 } 9060 mp->b_cont->b_wptr += sizeof (ire_t); 9061 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9062 9063 /* cp known to be 32 bit aligned */ 9064 ac6 = (ipa6_conn_t *)cp; 9065 if (tcp->tcp_ipversion == IPV4_VERSION) { 9066 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9067 &ac6->ac6_laddr); 9068 } else { 9069 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9070 } 9071 ac6->ac6_faddr = tcp->tcp_remote_v6; 9072 ac6->ac6_fport = tcp->tcp_fport; 9073 ac6->ac6_lport = tcp->tcp_lport; 9074 tcp->tcp_hard_binding = 1; 9075 break; 9076 9077 case sizeof (sin_t): 9078 /* 9079 * NOTE: IPV6_ADDR_LEN also has same size. 9080 * Use family to discriminate. 9081 */ 9082 if (tcp->tcp_family == AF_INET) { 9083 sin = (sin_t *)cp; 9084 9085 *sin = sin_null; 9086 sin->sin_family = AF_INET; 9087 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9088 sin->sin_port = tcp->tcp_lport; 9089 break; 9090 } else { 9091 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9092 } 9093 break; 9094 9095 case sizeof (sin6_t): 9096 ASSERT(tcp->tcp_family == AF_INET6); 9097 sin6 = (sin6_t *)cp; 9098 9099 *sin6 = sin6_null; 9100 sin6->sin6_family = AF_INET6; 9101 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9102 sin6->sin6_port = tcp->tcp_lport; 9103 break; 9104 9105 case IP_ADDR_LEN: 9106 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9107 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9108 break; 9109 9110 } 9111 /* Add protocol number to end */ 9112 cp[addr_length] = (char)IPPROTO_TCP; 9113 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9114 return (mp); 9115 } 9116 9117 /* 9118 * Notify IP that we are having trouble with this connection. IP should 9119 * blow the IRE away and start over. 9120 */ 9121 static void 9122 tcp_ip_notify(tcp_t *tcp) 9123 { 9124 struct iocblk *iocp; 9125 ipid_t *ipid; 9126 mblk_t *mp; 9127 9128 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9129 if (tcp->tcp_ipversion == IPV6_VERSION) 9130 return; 9131 9132 mp = mkiocb(IP_IOCTL); 9133 if (mp == NULL) 9134 return; 9135 9136 iocp = (struct iocblk *)mp->b_rptr; 9137 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9138 9139 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9140 if (!mp->b_cont) { 9141 freeb(mp); 9142 return; 9143 } 9144 9145 ipid = (ipid_t *)mp->b_cont->b_rptr; 9146 mp->b_cont->b_wptr += iocp->ioc_count; 9147 bzero(ipid, sizeof (*ipid)); 9148 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9149 ipid->ipid_ire_type = IRE_CACHE; 9150 ipid->ipid_addr_offset = sizeof (ipid_t); 9151 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9152 /* 9153 * Note: in the case of source routing we want to blow away the 9154 * route to the first source route hop. 9155 */ 9156 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9157 sizeof (tcp->tcp_ipha->ipha_dst)); 9158 9159 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9160 } 9161 9162 /* Unlink and return any mblk that looks like it contains an ire */ 9163 static mblk_t * 9164 tcp_ire_mp(mblk_t *mp) 9165 { 9166 mblk_t *prev_mp; 9167 9168 for (;;) { 9169 prev_mp = mp; 9170 mp = mp->b_cont; 9171 if (mp == NULL) 9172 break; 9173 switch (DB_TYPE(mp)) { 9174 case IRE_DB_TYPE: 9175 case IRE_DB_REQ_TYPE: 9176 if (prev_mp != NULL) 9177 prev_mp->b_cont = mp->b_cont; 9178 mp->b_cont = NULL; 9179 return (mp); 9180 default: 9181 break; 9182 } 9183 } 9184 return (mp); 9185 } 9186 9187 /* 9188 * Timer callback routine for keepalive probe. We do a fake resend of 9189 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9190 * check to see if we have heard anything from the other end for the last 9191 * RTO period. If we have, set the timer to expire for another 9192 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9193 * RTO << 1 and check again when it expires. Keep exponentially increasing 9194 * the timeout if we have not heard from the other side. If for more than 9195 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9196 * kill the connection unless the keepalive abort threshold is 0. In 9197 * that case, we will probe "forever." 9198 */ 9199 static void 9200 tcp_keepalive_killer(void *arg) 9201 { 9202 mblk_t *mp; 9203 conn_t *connp = (conn_t *)arg; 9204 tcp_t *tcp = connp->conn_tcp; 9205 int32_t firetime; 9206 int32_t idletime; 9207 int32_t ka_intrvl; 9208 tcp_stack_t *tcps = tcp->tcp_tcps; 9209 9210 tcp->tcp_ka_tid = 0; 9211 9212 if (tcp->tcp_fused) 9213 return; 9214 9215 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9216 ka_intrvl = tcp->tcp_ka_interval; 9217 9218 /* 9219 * Keepalive probe should only be sent if the application has not 9220 * done a close on the connection. 9221 */ 9222 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9223 return; 9224 } 9225 /* Timer fired too early, restart it. */ 9226 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9227 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9228 MSEC_TO_TICK(ka_intrvl)); 9229 return; 9230 } 9231 9232 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9233 /* 9234 * If we have not heard from the other side for a long 9235 * time, kill the connection unless the keepalive abort 9236 * threshold is 0. In that case, we will probe "forever." 9237 */ 9238 if (tcp->tcp_ka_abort_thres != 0 && 9239 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9240 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9241 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9242 tcp->tcp_client_errno : ETIMEDOUT, 11); 9243 return; 9244 } 9245 9246 if (tcp->tcp_snxt == tcp->tcp_suna && 9247 idletime >= ka_intrvl) { 9248 /* Fake resend of last ACKed byte. */ 9249 mblk_t *mp1 = allocb(1, BPRI_LO); 9250 9251 if (mp1 != NULL) { 9252 *mp1->b_wptr++ = '\0'; 9253 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9254 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9255 freeb(mp1); 9256 /* 9257 * if allocation failed, fall through to start the 9258 * timer back. 9259 */ 9260 if (mp != NULL) { 9261 TCP_RECORD_TRACE(tcp, mp, 9262 TCP_TRACE_SEND_PKT); 9263 tcp_send_data(tcp, tcp->tcp_wq, mp); 9264 BUMP_MIB(&tcps->tcps_mib, 9265 tcpTimKeepaliveProbe); 9266 if (tcp->tcp_ka_last_intrvl != 0) { 9267 int max; 9268 /* 9269 * We should probe again at least 9270 * in ka_intrvl, but not more than 9271 * tcp_rexmit_interval_max. 9272 */ 9273 max = tcps->tcps_rexmit_interval_max; 9274 firetime = MIN(ka_intrvl - 1, 9275 tcp->tcp_ka_last_intrvl << 1); 9276 if (firetime > max) 9277 firetime = max; 9278 } else { 9279 firetime = tcp->tcp_rto; 9280 } 9281 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9282 tcp_keepalive_killer, 9283 MSEC_TO_TICK(firetime)); 9284 tcp->tcp_ka_last_intrvl = firetime; 9285 return; 9286 } 9287 } 9288 } else { 9289 tcp->tcp_ka_last_intrvl = 0; 9290 } 9291 9292 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9293 if ((firetime = ka_intrvl - idletime) < 0) { 9294 firetime = ka_intrvl; 9295 } 9296 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9297 MSEC_TO_TICK(firetime)); 9298 } 9299 9300 int 9301 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9302 { 9303 queue_t *q = tcp->tcp_rq; 9304 int32_t mss = tcp->tcp_mss; 9305 int maxpsz; 9306 9307 if (TCP_IS_DETACHED(tcp)) 9308 return (mss); 9309 9310 if (tcp->tcp_fused) { 9311 maxpsz = tcp_fuse_maxpsz_set(tcp); 9312 mss = INFPSZ; 9313 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9314 /* 9315 * Set the sd_qn_maxpsz according to the socket send buffer 9316 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9317 * instruct the stream head to copyin user data into contiguous 9318 * kernel-allocated buffers without breaking it up into smaller 9319 * chunks. We round up the buffer size to the nearest SMSS. 9320 */ 9321 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9322 if (tcp->tcp_kssl_ctx == NULL) 9323 mss = INFPSZ; 9324 else 9325 mss = SSL3_MAX_RECORD_LEN; 9326 } else { 9327 /* 9328 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9329 * (and a multiple of the mss). This instructs the stream 9330 * head to break down larger than SMSS writes into SMSS- 9331 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9332 */ 9333 maxpsz = tcp->tcp_maxpsz * mss; 9334 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9335 maxpsz = tcp->tcp_xmit_hiwater/2; 9336 /* Round up to nearest mss */ 9337 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9338 } 9339 } 9340 (void) setmaxps(q, maxpsz); 9341 tcp->tcp_wq->q_maxpsz = maxpsz; 9342 9343 if (set_maxblk) 9344 (void) mi_set_sth_maxblk(q, mss); 9345 9346 return (mss); 9347 } 9348 9349 /* 9350 * Extract option values from a tcp header. We put any found values into the 9351 * tcpopt struct and return a bitmask saying which options were found. 9352 */ 9353 static int 9354 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9355 { 9356 uchar_t *endp; 9357 int len; 9358 uint32_t mss; 9359 uchar_t *up = (uchar_t *)tcph; 9360 int found = 0; 9361 int32_t sack_len; 9362 tcp_seq sack_begin, sack_end; 9363 tcp_t *tcp; 9364 9365 endp = up + TCP_HDR_LENGTH(tcph); 9366 up += TCP_MIN_HEADER_LENGTH; 9367 while (up < endp) { 9368 len = endp - up; 9369 switch (*up) { 9370 case TCPOPT_EOL: 9371 break; 9372 9373 case TCPOPT_NOP: 9374 up++; 9375 continue; 9376 9377 case TCPOPT_MAXSEG: 9378 if (len < TCPOPT_MAXSEG_LEN || 9379 up[1] != TCPOPT_MAXSEG_LEN) 9380 break; 9381 9382 mss = BE16_TO_U16(up+2); 9383 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9384 tcpopt->tcp_opt_mss = mss; 9385 found |= TCP_OPT_MSS_PRESENT; 9386 9387 up += TCPOPT_MAXSEG_LEN; 9388 continue; 9389 9390 case TCPOPT_WSCALE: 9391 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9392 break; 9393 9394 if (up[2] > TCP_MAX_WINSHIFT) 9395 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9396 else 9397 tcpopt->tcp_opt_wscale = up[2]; 9398 found |= TCP_OPT_WSCALE_PRESENT; 9399 9400 up += TCPOPT_WS_LEN; 9401 continue; 9402 9403 case TCPOPT_SACK_PERMITTED: 9404 if (len < TCPOPT_SACK_OK_LEN || 9405 up[1] != TCPOPT_SACK_OK_LEN) 9406 break; 9407 found |= TCP_OPT_SACK_OK_PRESENT; 9408 up += TCPOPT_SACK_OK_LEN; 9409 continue; 9410 9411 case TCPOPT_SACK: 9412 if (len <= 2 || up[1] <= 2 || len < up[1]) 9413 break; 9414 9415 /* If TCP is not interested in SACK blks... */ 9416 if ((tcp = tcpopt->tcp) == NULL) { 9417 up += up[1]; 9418 continue; 9419 } 9420 sack_len = up[1] - TCPOPT_HEADER_LEN; 9421 up += TCPOPT_HEADER_LEN; 9422 9423 /* 9424 * If the list is empty, allocate one and assume 9425 * nothing is sack'ed. 9426 */ 9427 ASSERT(tcp->tcp_sack_info != NULL); 9428 if (tcp->tcp_notsack_list == NULL) { 9429 tcp_notsack_update(&(tcp->tcp_notsack_list), 9430 tcp->tcp_suna, tcp->tcp_snxt, 9431 &(tcp->tcp_num_notsack_blk), 9432 &(tcp->tcp_cnt_notsack_list)); 9433 9434 /* 9435 * Make sure tcp_notsack_list is not NULL. 9436 * This happens when kmem_alloc(KM_NOSLEEP) 9437 * returns NULL. 9438 */ 9439 if (tcp->tcp_notsack_list == NULL) { 9440 up += sack_len; 9441 continue; 9442 } 9443 tcp->tcp_fack = tcp->tcp_suna; 9444 } 9445 9446 while (sack_len > 0) { 9447 if (up + 8 > endp) { 9448 up = endp; 9449 break; 9450 } 9451 sack_begin = BE32_TO_U32(up); 9452 up += 4; 9453 sack_end = BE32_TO_U32(up); 9454 up += 4; 9455 sack_len -= 8; 9456 /* 9457 * Bounds checking. Make sure the SACK 9458 * info is within tcp_suna and tcp_snxt. 9459 * If this SACK blk is out of bound, ignore 9460 * it but continue to parse the following 9461 * blks. 9462 */ 9463 if (SEQ_LEQ(sack_end, sack_begin) || 9464 SEQ_LT(sack_begin, tcp->tcp_suna) || 9465 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9466 continue; 9467 } 9468 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9469 sack_begin, sack_end, 9470 &(tcp->tcp_num_notsack_blk), 9471 &(tcp->tcp_cnt_notsack_list)); 9472 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9473 tcp->tcp_fack = sack_end; 9474 } 9475 } 9476 found |= TCP_OPT_SACK_PRESENT; 9477 continue; 9478 9479 case TCPOPT_TSTAMP: 9480 if (len < TCPOPT_TSTAMP_LEN || 9481 up[1] != TCPOPT_TSTAMP_LEN) 9482 break; 9483 9484 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9485 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9486 9487 found |= TCP_OPT_TSTAMP_PRESENT; 9488 9489 up += TCPOPT_TSTAMP_LEN; 9490 continue; 9491 9492 default: 9493 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9494 break; 9495 up += up[1]; 9496 continue; 9497 } 9498 break; 9499 } 9500 return (found); 9501 } 9502 9503 /* 9504 * Set the mss associated with a particular tcp based on its current value, 9505 * and a new one passed in. Observe minimums and maximums, and reset 9506 * other state variables that we want to view as multiples of mss. 9507 * 9508 * This function is called in various places mainly because 9509 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9510 * other side's SYN/SYN-ACK packet arrives. 9511 * 2) PMTUd may get us a new MSS. 9512 * 3) If the other side stops sending us timestamp option, we need to 9513 * increase the MSS size to use the extra bytes available. 9514 * 9515 * do_ss is used to control whether we will be doing slow start or 9516 * not if there is a change in the mss. Note that for some events like 9517 * tcp_paws_check() we allow the tcp_cwnd to adjust to the new mss but 9518 * do not perform a slow start specifically. 9519 */ 9520 static void 9521 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9522 { 9523 uint32_t mss_max; 9524 tcp_stack_t *tcps = tcp->tcp_tcps; 9525 9526 if (tcp->tcp_ipversion == IPV4_VERSION) 9527 mss_max = tcps->tcps_mss_max_ipv4; 9528 else 9529 mss_max = tcps->tcps_mss_max_ipv6; 9530 9531 if (mss < tcps->tcps_mss_min) 9532 mss = tcps->tcps_mss_min; 9533 if (mss > mss_max) 9534 mss = mss_max; 9535 /* 9536 * Unless naglim has been set by our client to 9537 * a non-mss value, force naglim to track mss. 9538 * This can help to aggregate small writes. 9539 */ 9540 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9541 tcp->tcp_naglim = mss; 9542 /* 9543 * TCP should be able to buffer at least 4 MSS data for obvious 9544 * performance reason. 9545 */ 9546 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9547 tcp->tcp_xmit_hiwater = mss << 2; 9548 9549 /* 9550 * Check if we need to apply the tcp_init_cwnd here. If 9551 * it is set and the MSS gets bigger (should not happen 9552 * normally), we need to adjust the resulting tcp_cwnd properly. 9553 * The new tcp_cwnd should not get bigger. 9554 */ 9555 /* 9556 * We need to avoid setting tcp_cwnd to its slow start value 9557 * unnecessarily. However we have to let the tcp_cwnd adjust 9558 * to the modified mss. 9559 */ 9560 if (tcp->tcp_init_cwnd == 0 && do_ss) { 9561 tcp->tcp_cwnd = MIN(tcps->tcps_slow_start_initial * 9562 mss, MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9563 } else { 9564 if (tcp->tcp_mss < mss) { 9565 tcp->tcp_cwnd = MAX(1, 9566 (tcp->tcp_init_cwnd * tcp->tcp_mss / 9567 mss)) * mss; 9568 } else { 9569 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9570 } 9571 } 9572 tcp->tcp_mss = mss; 9573 tcp->tcp_cwnd_cnt = 0; 9574 (void) tcp_maxpsz_set(tcp, B_TRUE); 9575 } 9576 9577 static int 9578 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9579 { 9580 tcp_t *tcp = NULL; 9581 conn_t *connp; 9582 int err; 9583 dev_t conn_dev; 9584 zoneid_t zoneid; 9585 tcp_stack_t *tcps = NULL; 9586 9587 if (q->q_ptr != NULL) 9588 return (0); 9589 9590 if (!(flag & SO_ACCEPTOR)) { 9591 /* 9592 * Special case for install: miniroot needs to be able to 9593 * access files via NFS as though it were always in the 9594 * global zone. 9595 */ 9596 if (credp == kcred && nfs_global_client_only != 0) { 9597 zoneid = GLOBAL_ZONEID; 9598 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9599 netstack_tcp; 9600 ASSERT(tcps != NULL); 9601 } else { 9602 netstack_t *ns; 9603 9604 ns = netstack_find_by_cred(credp); 9605 ASSERT(ns != NULL); 9606 tcps = ns->netstack_tcp; 9607 ASSERT(tcps != NULL); 9608 9609 /* 9610 * For exclusive stacks we set the zoneid to zero 9611 * to make TCP operate as if in the global zone. 9612 */ 9613 if (tcps->tcps_netstack->netstack_stackid != 9614 GLOBAL_NETSTACKID) 9615 zoneid = GLOBAL_ZONEID; 9616 else 9617 zoneid = crgetzoneid(credp); 9618 } 9619 /* 9620 * For stackid zero this is done from strplumb.c, but 9621 * non-zero stackids are handled here. 9622 */ 9623 if (tcps->tcps_g_q == NULL && 9624 tcps->tcps_netstack->netstack_stackid != 9625 GLOBAL_NETSTACKID) { 9626 tcp_g_q_setup(tcps); 9627 } 9628 } 9629 if (sflag == MODOPEN) { 9630 /* 9631 * This is a special case. The purpose of a modopen 9632 * is to allow just the T_SVR4_OPTMGMT_REQ to pass 9633 * through for MIB browsers. Everything else is failed. 9634 */ 9635 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9636 /* tcp_get_conn incremented refcnt */ 9637 netstack_rele(tcps->tcps_netstack); 9638 9639 if (connp == NULL) 9640 return (ENOMEM); 9641 9642 connp->conn_flags |= IPCL_TCPMOD; 9643 connp->conn_cred = credp; 9644 connp->conn_zoneid = zoneid; 9645 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9646 ASSERT(connp->conn_netstack->netstack_tcp == tcps); 9647 q->q_ptr = WR(q)->q_ptr = connp; 9648 crhold(credp); 9649 q->q_qinfo = &tcp_mod_rinit; 9650 WR(q)->q_qinfo = &tcp_mod_winit; 9651 qprocson(q); 9652 return (0); 9653 } 9654 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 9655 if (tcps != NULL) 9656 netstack_rele(tcps->tcps_netstack); 9657 return (EBUSY); 9658 } 9659 9660 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9661 9662 if (flag & SO_ACCEPTOR) { 9663 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9664 ASSERT(tcps == NULL); 9665 q->q_qinfo = &tcp_acceptor_rinit; 9666 q->q_ptr = (void *)conn_dev; 9667 WR(q)->q_qinfo = &tcp_acceptor_winit; 9668 WR(q)->q_ptr = (void *)conn_dev; 9669 qprocson(q); 9670 return (0); 9671 } 9672 9673 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9674 /* 9675 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9676 * so we drop it by one. 9677 */ 9678 netstack_rele(tcps->tcps_netstack); 9679 if (connp == NULL) { 9680 inet_minor_free(ip_minor_arena, conn_dev); 9681 q->q_ptr = NULL; 9682 return (ENOSR); 9683 } 9684 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9685 tcp = connp->conn_tcp; 9686 9687 q->q_ptr = WR(q)->q_ptr = connp; 9688 if (getmajor(*devp) == TCP6_MAJ) { 9689 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9690 connp->conn_send = ip_output_v6; 9691 connp->conn_af_isv6 = B_TRUE; 9692 connp->conn_pkt_isv6 = B_TRUE; 9693 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9694 tcp->tcp_ipversion = IPV6_VERSION; 9695 tcp->tcp_family = AF_INET6; 9696 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9697 } else { 9698 connp->conn_flags |= IPCL_TCP4; 9699 connp->conn_send = ip_output; 9700 connp->conn_af_isv6 = B_FALSE; 9701 connp->conn_pkt_isv6 = B_FALSE; 9702 tcp->tcp_ipversion = IPV4_VERSION; 9703 tcp->tcp_family = AF_INET; 9704 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9705 } 9706 9707 /* 9708 * TCP keeps a copy of cred for cache locality reasons but 9709 * we put a reference only once. If connp->conn_cred 9710 * becomes invalid, tcp_cred should also be set to NULL. 9711 */ 9712 tcp->tcp_cred = connp->conn_cred = credp; 9713 crhold(connp->conn_cred); 9714 tcp->tcp_cpid = curproc->p_pid; 9715 tcp->tcp_open_time = lbolt64; 9716 connp->conn_zoneid = zoneid; 9717 connp->conn_mlp_type = mlptSingle; 9718 connp->conn_ulp_labeled = !is_system_labeled(); 9719 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9720 ASSERT(tcp->tcp_tcps == tcps); 9721 9722 /* 9723 * If the caller has the process-wide flag set, then default to MAC 9724 * exempt mode. This allows read-down to unlabeled hosts. 9725 */ 9726 if (getpflags(NET_MAC_AWARE, credp) != 0) 9727 connp->conn_mac_exempt = B_TRUE; 9728 9729 connp->conn_dev = conn_dev; 9730 9731 ASSERT(q->q_qinfo == &tcp_rinit); 9732 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9733 9734 if (flag & SO_SOCKSTR) { 9735 /* 9736 * No need to insert a socket in tcp acceptor hash. 9737 * If it was a socket acceptor stream, we dealt with 9738 * it above. A socket listener can never accept a 9739 * connection and doesn't need acceptor_id. 9740 */ 9741 connp->conn_flags |= IPCL_SOCKET; 9742 tcp->tcp_issocket = 1; 9743 WR(q)->q_qinfo = &tcp_sock_winit; 9744 } else { 9745 #ifdef _ILP32 9746 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9747 #else 9748 tcp->tcp_acceptor_id = conn_dev; 9749 #endif /* _ILP32 */ 9750 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9751 } 9752 9753 if (tcps->tcps_trace) 9754 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9755 9756 err = tcp_init(tcp, q); 9757 if (err != 0) { 9758 inet_minor_free(ip_minor_arena, connp->conn_dev); 9759 tcp_acceptor_hash_remove(tcp); 9760 CONN_DEC_REF(connp); 9761 q->q_ptr = WR(q)->q_ptr = NULL; 9762 return (err); 9763 } 9764 9765 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9766 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9767 9768 /* Non-zero default values */ 9769 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9770 /* 9771 * Put the ref for TCP. Ref for IP was already put 9772 * by ipcl_conn_create. Also Make the conn_t globally 9773 * visible to walkers 9774 */ 9775 mutex_enter(&connp->conn_lock); 9776 CONN_INC_REF_LOCKED(connp); 9777 ASSERT(connp->conn_ref == 2); 9778 connp->conn_state_flags &= ~CONN_INCIPIENT; 9779 mutex_exit(&connp->conn_lock); 9780 9781 qprocson(q); 9782 return (0); 9783 } 9784 9785 /* 9786 * Some TCP options can be "set" by requesting them in the option 9787 * buffer. This is needed for XTI feature test though we do not 9788 * allow it in general. We interpret that this mechanism is more 9789 * applicable to OSI protocols and need not be allowed in general. 9790 * This routine filters out options for which it is not allowed (most) 9791 * and lets through those (few) for which it is. [ The XTI interface 9792 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9793 * ever implemented will have to be allowed here ]. 9794 */ 9795 static boolean_t 9796 tcp_allow_connopt_set(int level, int name) 9797 { 9798 9799 switch (level) { 9800 case IPPROTO_TCP: 9801 switch (name) { 9802 case TCP_NODELAY: 9803 return (B_TRUE); 9804 default: 9805 return (B_FALSE); 9806 } 9807 /*NOTREACHED*/ 9808 default: 9809 return (B_FALSE); 9810 } 9811 /*NOTREACHED*/ 9812 } 9813 9814 /* 9815 * This routine gets default values of certain options whose default 9816 * values are maintained by protocol specific code 9817 */ 9818 /* ARGSUSED */ 9819 int 9820 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9821 { 9822 int32_t *i1 = (int32_t *)ptr; 9823 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9824 9825 switch (level) { 9826 case IPPROTO_TCP: 9827 switch (name) { 9828 case TCP_NOTIFY_THRESHOLD: 9829 *i1 = tcps->tcps_ip_notify_interval; 9830 break; 9831 case TCP_ABORT_THRESHOLD: 9832 *i1 = tcps->tcps_ip_abort_interval; 9833 break; 9834 case TCP_CONN_NOTIFY_THRESHOLD: 9835 *i1 = tcps->tcps_ip_notify_cinterval; 9836 break; 9837 case TCP_CONN_ABORT_THRESHOLD: 9838 *i1 = tcps->tcps_ip_abort_cinterval; 9839 break; 9840 default: 9841 return (-1); 9842 } 9843 break; 9844 case IPPROTO_IP: 9845 switch (name) { 9846 case IP_TTL: 9847 *i1 = tcps->tcps_ipv4_ttl; 9848 break; 9849 default: 9850 return (-1); 9851 } 9852 break; 9853 case IPPROTO_IPV6: 9854 switch (name) { 9855 case IPV6_UNICAST_HOPS: 9856 *i1 = tcps->tcps_ipv6_hoplimit; 9857 break; 9858 default: 9859 return (-1); 9860 } 9861 break; 9862 default: 9863 return (-1); 9864 } 9865 return (sizeof (int)); 9866 } 9867 9868 9869 /* 9870 * TCP routine to get the values of options. 9871 */ 9872 int 9873 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9874 { 9875 int *i1 = (int *)ptr; 9876 conn_t *connp = Q_TO_CONN(q); 9877 tcp_t *tcp = connp->conn_tcp; 9878 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9879 9880 switch (level) { 9881 case SOL_SOCKET: 9882 switch (name) { 9883 case SO_LINGER: { 9884 struct linger *lgr = (struct linger *)ptr; 9885 9886 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9887 lgr->l_linger = tcp->tcp_lingertime; 9888 } 9889 return (sizeof (struct linger)); 9890 case SO_DEBUG: 9891 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9892 break; 9893 case SO_KEEPALIVE: 9894 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9895 break; 9896 case SO_DONTROUTE: 9897 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9898 break; 9899 case SO_USELOOPBACK: 9900 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9901 break; 9902 case SO_BROADCAST: 9903 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9904 break; 9905 case SO_REUSEADDR: 9906 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9907 break; 9908 case SO_OOBINLINE: 9909 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9910 break; 9911 case SO_DGRAM_ERRIND: 9912 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9913 break; 9914 case SO_TYPE: 9915 *i1 = SOCK_STREAM; 9916 break; 9917 case SO_SNDBUF: 9918 *i1 = tcp->tcp_xmit_hiwater; 9919 break; 9920 case SO_RCVBUF: 9921 *i1 = RD(q)->q_hiwat; 9922 break; 9923 case SO_SND_COPYAVOID: 9924 *i1 = tcp->tcp_snd_zcopy_on ? 9925 SO_SND_COPYAVOID : 0; 9926 break; 9927 case SO_ALLZONES: 9928 *i1 = connp->conn_allzones ? 1 : 0; 9929 break; 9930 case SO_ANON_MLP: 9931 *i1 = connp->conn_anon_mlp; 9932 break; 9933 case SO_MAC_EXEMPT: 9934 *i1 = connp->conn_mac_exempt; 9935 break; 9936 case SO_EXCLBIND: 9937 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9938 break; 9939 case SO_PROTOTYPE: 9940 *i1 = IPPROTO_TCP; 9941 break; 9942 case SO_DOMAIN: 9943 *i1 = tcp->tcp_family; 9944 break; 9945 default: 9946 return (-1); 9947 } 9948 break; 9949 case IPPROTO_TCP: 9950 switch (name) { 9951 case TCP_NODELAY: 9952 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9953 break; 9954 case TCP_MAXSEG: 9955 *i1 = tcp->tcp_mss; 9956 break; 9957 case TCP_NOTIFY_THRESHOLD: 9958 *i1 = (int)tcp->tcp_first_timer_threshold; 9959 break; 9960 case TCP_ABORT_THRESHOLD: 9961 *i1 = tcp->tcp_second_timer_threshold; 9962 break; 9963 case TCP_CONN_NOTIFY_THRESHOLD: 9964 *i1 = tcp->tcp_first_ctimer_threshold; 9965 break; 9966 case TCP_CONN_ABORT_THRESHOLD: 9967 *i1 = tcp->tcp_second_ctimer_threshold; 9968 break; 9969 case TCP_RECVDSTADDR: 9970 *i1 = tcp->tcp_recvdstaddr; 9971 break; 9972 case TCP_ANONPRIVBIND: 9973 *i1 = tcp->tcp_anon_priv_bind; 9974 break; 9975 case TCP_EXCLBIND: 9976 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9977 break; 9978 case TCP_INIT_CWND: 9979 *i1 = tcp->tcp_init_cwnd; 9980 break; 9981 case TCP_KEEPALIVE_THRESHOLD: 9982 *i1 = tcp->tcp_ka_interval; 9983 break; 9984 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9985 *i1 = tcp->tcp_ka_abort_thres; 9986 break; 9987 case TCP_CORK: 9988 *i1 = tcp->tcp_cork; 9989 break; 9990 default: 9991 return (-1); 9992 } 9993 break; 9994 case IPPROTO_IP: 9995 if (tcp->tcp_family != AF_INET) 9996 return (-1); 9997 switch (name) { 9998 case IP_OPTIONS: 9999 case T_IP_OPTIONS: { 10000 /* 10001 * This is compatible with BSD in that in only return 10002 * the reverse source route with the final destination 10003 * as the last entry. The first 4 bytes of the option 10004 * will contain the final destination. 10005 */ 10006 int opt_len; 10007 10008 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 10009 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 10010 ASSERT(opt_len >= 0); 10011 /* Caller ensures enough space */ 10012 if (opt_len > 0) { 10013 /* 10014 * TODO: Do we have to handle getsockopt on an 10015 * initiator as well? 10016 */ 10017 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 10018 } 10019 return (0); 10020 } 10021 case IP_TOS: 10022 case T_IP_TOS: 10023 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10024 break; 10025 case IP_TTL: 10026 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10027 break; 10028 case IP_NEXTHOP: 10029 /* Handled at IP level */ 10030 return (-EINVAL); 10031 default: 10032 return (-1); 10033 } 10034 break; 10035 case IPPROTO_IPV6: 10036 /* 10037 * IPPROTO_IPV6 options are only supported for sockets 10038 * that are using IPv6 on the wire. 10039 */ 10040 if (tcp->tcp_ipversion != IPV6_VERSION) { 10041 return (-1); 10042 } 10043 switch (name) { 10044 case IPV6_UNICAST_HOPS: 10045 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10046 break; /* goto sizeof (int) option return */ 10047 case IPV6_BOUND_IF: 10048 /* Zero if not set */ 10049 *i1 = tcp->tcp_bound_if; 10050 break; /* goto sizeof (int) option return */ 10051 case IPV6_RECVPKTINFO: 10052 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10053 *i1 = 1; 10054 else 10055 *i1 = 0; 10056 break; /* goto sizeof (int) option return */ 10057 case IPV6_RECVTCLASS: 10058 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10059 *i1 = 1; 10060 else 10061 *i1 = 0; 10062 break; /* goto sizeof (int) option return */ 10063 case IPV6_RECVHOPLIMIT: 10064 if (tcp->tcp_ipv6_recvancillary & 10065 TCP_IPV6_RECVHOPLIMIT) 10066 *i1 = 1; 10067 else 10068 *i1 = 0; 10069 break; /* goto sizeof (int) option return */ 10070 case IPV6_RECVHOPOPTS: 10071 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10072 *i1 = 1; 10073 else 10074 *i1 = 0; 10075 break; /* goto sizeof (int) option return */ 10076 case IPV6_RECVDSTOPTS: 10077 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10078 *i1 = 1; 10079 else 10080 *i1 = 0; 10081 break; /* goto sizeof (int) option return */ 10082 case _OLD_IPV6_RECVDSTOPTS: 10083 if (tcp->tcp_ipv6_recvancillary & 10084 TCP_OLD_IPV6_RECVDSTOPTS) 10085 *i1 = 1; 10086 else 10087 *i1 = 0; 10088 break; /* goto sizeof (int) option return */ 10089 case IPV6_RECVRTHDR: 10090 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10091 *i1 = 1; 10092 else 10093 *i1 = 0; 10094 break; /* goto sizeof (int) option return */ 10095 case IPV6_RECVRTHDRDSTOPTS: 10096 if (tcp->tcp_ipv6_recvancillary & 10097 TCP_IPV6_RECVRTDSTOPTS) 10098 *i1 = 1; 10099 else 10100 *i1 = 0; 10101 break; /* goto sizeof (int) option return */ 10102 case IPV6_PKTINFO: { 10103 /* XXX assumes that caller has room for max size! */ 10104 struct in6_pktinfo *pkti; 10105 10106 pkti = (struct in6_pktinfo *)ptr; 10107 if (ipp->ipp_fields & IPPF_IFINDEX) 10108 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10109 else 10110 pkti->ipi6_ifindex = 0; 10111 if (ipp->ipp_fields & IPPF_ADDR) 10112 pkti->ipi6_addr = ipp->ipp_addr; 10113 else 10114 pkti->ipi6_addr = ipv6_all_zeros; 10115 return (sizeof (struct in6_pktinfo)); 10116 } 10117 case IPV6_TCLASS: 10118 if (ipp->ipp_fields & IPPF_TCLASS) 10119 *i1 = ipp->ipp_tclass; 10120 else 10121 *i1 = IPV6_FLOW_TCLASS( 10122 IPV6_DEFAULT_VERS_AND_FLOW); 10123 break; /* goto sizeof (int) option return */ 10124 case IPV6_NEXTHOP: { 10125 sin6_t *sin6 = (sin6_t *)ptr; 10126 10127 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10128 return (0); 10129 *sin6 = sin6_null; 10130 sin6->sin6_family = AF_INET6; 10131 sin6->sin6_addr = ipp->ipp_nexthop; 10132 return (sizeof (sin6_t)); 10133 } 10134 case IPV6_HOPOPTS: 10135 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10136 return (0); 10137 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10138 return (0); 10139 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10140 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10141 if (tcp->tcp_label_len > 0) { 10142 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10143 ptr[1] = (ipp->ipp_hopoptslen - 10144 tcp->tcp_label_len + 7) / 8 - 1; 10145 } 10146 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10147 case IPV6_RTHDRDSTOPTS: 10148 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10149 return (0); 10150 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10151 return (ipp->ipp_rtdstoptslen); 10152 case IPV6_RTHDR: 10153 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10154 return (0); 10155 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10156 return (ipp->ipp_rthdrlen); 10157 case IPV6_DSTOPTS: 10158 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10159 return (0); 10160 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10161 return (ipp->ipp_dstoptslen); 10162 case IPV6_SRC_PREFERENCES: 10163 return (ip6_get_src_preferences(connp, 10164 (uint32_t *)ptr)); 10165 case IPV6_PATHMTU: { 10166 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10167 10168 if (tcp->tcp_state < TCPS_ESTABLISHED) 10169 return (-1); 10170 10171 return (ip_fill_mtuinfo(&connp->conn_remv6, 10172 connp->conn_fport, mtuinfo, 10173 connp->conn_netstack)); 10174 } 10175 default: 10176 return (-1); 10177 } 10178 break; 10179 default: 10180 return (-1); 10181 } 10182 return (sizeof (int)); 10183 } 10184 10185 /* 10186 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10187 * Parameters are assumed to be verified by the caller. 10188 */ 10189 /* ARGSUSED */ 10190 int 10191 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10192 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10193 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10194 { 10195 conn_t *connp = Q_TO_CONN(q); 10196 tcp_t *tcp = connp->conn_tcp; 10197 int *i1 = (int *)invalp; 10198 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10199 boolean_t checkonly; 10200 int reterr; 10201 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10202 10203 switch (optset_context) { 10204 case SETFN_OPTCOM_CHECKONLY: 10205 checkonly = B_TRUE; 10206 /* 10207 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10208 * inlen != 0 implies value supplied and 10209 * we have to "pretend" to set it. 10210 * inlen == 0 implies that there is no 10211 * value part in T_CHECK request and just validation 10212 * done elsewhere should be enough, we just return here. 10213 */ 10214 if (inlen == 0) { 10215 *outlenp = 0; 10216 return (0); 10217 } 10218 break; 10219 case SETFN_OPTCOM_NEGOTIATE: 10220 checkonly = B_FALSE; 10221 break; 10222 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10223 case SETFN_CONN_NEGOTIATE: 10224 checkonly = B_FALSE; 10225 /* 10226 * Negotiating local and "association-related" options 10227 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10228 * primitives is allowed by XTI, but we choose 10229 * to not implement this style negotiation for Internet 10230 * protocols (We interpret it is a must for OSI world but 10231 * optional for Internet protocols) for all options. 10232 * [ Will do only for the few options that enable test 10233 * suites that our XTI implementation of this feature 10234 * works for transports that do allow it ] 10235 */ 10236 if (!tcp_allow_connopt_set(level, name)) { 10237 *outlenp = 0; 10238 return (EINVAL); 10239 } 10240 break; 10241 default: 10242 /* 10243 * We should never get here 10244 */ 10245 *outlenp = 0; 10246 return (EINVAL); 10247 } 10248 10249 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10250 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10251 10252 /* 10253 * For TCP, we should have no ancillary data sent down 10254 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10255 * has to be zero. 10256 */ 10257 ASSERT(thisdg_attrs == NULL); 10258 10259 /* 10260 * For fixed length options, no sanity check 10261 * of passed in length is done. It is assumed *_optcom_req() 10262 * routines do the right thing. 10263 */ 10264 10265 switch (level) { 10266 case SOL_SOCKET: 10267 switch (name) { 10268 case SO_LINGER: { 10269 struct linger *lgr = (struct linger *)invalp; 10270 10271 if (!checkonly) { 10272 if (lgr->l_onoff) { 10273 tcp->tcp_linger = 1; 10274 tcp->tcp_lingertime = lgr->l_linger; 10275 } else { 10276 tcp->tcp_linger = 0; 10277 tcp->tcp_lingertime = 0; 10278 } 10279 /* struct copy */ 10280 *(struct linger *)outvalp = *lgr; 10281 } else { 10282 if (!lgr->l_onoff) { 10283 ((struct linger *)outvalp)->l_onoff = 0; 10284 ((struct linger *)outvalp)->l_linger = 0; 10285 } else { 10286 /* struct copy */ 10287 *(struct linger *)outvalp = *lgr; 10288 } 10289 } 10290 *outlenp = sizeof (struct linger); 10291 return (0); 10292 } 10293 case SO_DEBUG: 10294 if (!checkonly) 10295 tcp->tcp_debug = onoff; 10296 break; 10297 case SO_KEEPALIVE: 10298 if (checkonly) { 10299 /* T_CHECK case */ 10300 break; 10301 } 10302 10303 if (!onoff) { 10304 if (tcp->tcp_ka_enabled) { 10305 if (tcp->tcp_ka_tid != 0) { 10306 (void) TCP_TIMER_CANCEL(tcp, 10307 tcp->tcp_ka_tid); 10308 tcp->tcp_ka_tid = 0; 10309 } 10310 tcp->tcp_ka_enabled = 0; 10311 } 10312 break; 10313 } 10314 if (!tcp->tcp_ka_enabled) { 10315 /* Crank up the keepalive timer */ 10316 tcp->tcp_ka_last_intrvl = 0; 10317 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10318 tcp_keepalive_killer, 10319 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10320 tcp->tcp_ka_enabled = 1; 10321 } 10322 break; 10323 case SO_DONTROUTE: 10324 /* 10325 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10326 * only of interest to IP. We track them here only so 10327 * that we can report their current value. 10328 */ 10329 if (!checkonly) { 10330 tcp->tcp_dontroute = onoff; 10331 tcp->tcp_connp->conn_dontroute = onoff; 10332 } 10333 break; 10334 case SO_USELOOPBACK: 10335 if (!checkonly) { 10336 tcp->tcp_useloopback = onoff; 10337 tcp->tcp_connp->conn_loopback = onoff; 10338 } 10339 break; 10340 case SO_BROADCAST: 10341 if (!checkonly) { 10342 tcp->tcp_broadcast = onoff; 10343 tcp->tcp_connp->conn_broadcast = onoff; 10344 } 10345 break; 10346 case SO_REUSEADDR: 10347 if (!checkonly) { 10348 tcp->tcp_reuseaddr = onoff; 10349 tcp->tcp_connp->conn_reuseaddr = onoff; 10350 } 10351 break; 10352 case SO_OOBINLINE: 10353 if (!checkonly) 10354 tcp->tcp_oobinline = onoff; 10355 break; 10356 case SO_DGRAM_ERRIND: 10357 if (!checkonly) 10358 tcp->tcp_dgram_errind = onoff; 10359 break; 10360 case SO_SNDBUF: { 10361 if (*i1 > tcps->tcps_max_buf) { 10362 *outlenp = 0; 10363 return (ENOBUFS); 10364 } 10365 if (checkonly) 10366 break; 10367 10368 tcp->tcp_xmit_hiwater = *i1; 10369 if (tcps->tcps_snd_lowat_fraction != 0) 10370 tcp->tcp_xmit_lowater = 10371 tcp->tcp_xmit_hiwater / 10372 tcps->tcps_snd_lowat_fraction; 10373 (void) tcp_maxpsz_set(tcp, B_TRUE); 10374 /* 10375 * If we are flow-controlled, recheck the condition. 10376 * There are apps that increase SO_SNDBUF size when 10377 * flow-controlled (EWOULDBLOCK), and expect the flow 10378 * control condition to be lifted right away. 10379 */ 10380 mutex_enter(&tcp->tcp_non_sq_lock); 10381 if (tcp->tcp_flow_stopped && 10382 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10383 tcp_clrqfull(tcp); 10384 } 10385 mutex_exit(&tcp->tcp_non_sq_lock); 10386 break; 10387 } 10388 case SO_RCVBUF: 10389 if (*i1 > tcps->tcps_max_buf) { 10390 *outlenp = 0; 10391 return (ENOBUFS); 10392 } 10393 /* Silently ignore zero */ 10394 if (!checkonly && *i1 != 0) { 10395 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10396 (void) tcp_rwnd_set(tcp, *i1); 10397 } 10398 /* 10399 * XXX should we return the rwnd here 10400 * and tcp_opt_get ? 10401 */ 10402 break; 10403 case SO_SND_COPYAVOID: 10404 if (!checkonly) { 10405 /* we only allow enable at most once for now */ 10406 if (tcp->tcp_loopback || 10407 (!tcp->tcp_snd_zcopy_aware && 10408 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10409 *outlenp = 0; 10410 return (EOPNOTSUPP); 10411 } 10412 tcp->tcp_snd_zcopy_aware = 1; 10413 } 10414 break; 10415 case SO_ALLZONES: 10416 /* Handled at the IP level */ 10417 return (-EINVAL); 10418 case SO_ANON_MLP: 10419 if (!checkonly) { 10420 mutex_enter(&connp->conn_lock); 10421 connp->conn_anon_mlp = onoff; 10422 mutex_exit(&connp->conn_lock); 10423 } 10424 break; 10425 case SO_MAC_EXEMPT: 10426 if (secpolicy_net_mac_aware(cr) != 0 || 10427 IPCL_IS_BOUND(connp)) 10428 return (EACCES); 10429 if (!checkonly) { 10430 mutex_enter(&connp->conn_lock); 10431 connp->conn_mac_exempt = onoff; 10432 mutex_exit(&connp->conn_lock); 10433 } 10434 break; 10435 case SO_EXCLBIND: 10436 if (!checkonly) 10437 tcp->tcp_exclbind = onoff; 10438 break; 10439 default: 10440 *outlenp = 0; 10441 return (EINVAL); 10442 } 10443 break; 10444 case IPPROTO_TCP: 10445 switch (name) { 10446 case TCP_NODELAY: 10447 if (!checkonly) 10448 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10449 break; 10450 case TCP_NOTIFY_THRESHOLD: 10451 if (!checkonly) 10452 tcp->tcp_first_timer_threshold = *i1; 10453 break; 10454 case TCP_ABORT_THRESHOLD: 10455 if (!checkonly) 10456 tcp->tcp_second_timer_threshold = *i1; 10457 break; 10458 case TCP_CONN_NOTIFY_THRESHOLD: 10459 if (!checkonly) 10460 tcp->tcp_first_ctimer_threshold = *i1; 10461 break; 10462 case TCP_CONN_ABORT_THRESHOLD: 10463 if (!checkonly) 10464 tcp->tcp_second_ctimer_threshold = *i1; 10465 break; 10466 case TCP_RECVDSTADDR: 10467 if (tcp->tcp_state > TCPS_LISTEN) 10468 return (EOPNOTSUPP); 10469 if (!checkonly) 10470 tcp->tcp_recvdstaddr = onoff; 10471 break; 10472 case TCP_ANONPRIVBIND: 10473 if ((reterr = secpolicy_net_privaddr(cr, 0)) != 0) { 10474 *outlenp = 0; 10475 return (reterr); 10476 } 10477 if (!checkonly) { 10478 tcp->tcp_anon_priv_bind = onoff; 10479 } 10480 break; 10481 case TCP_EXCLBIND: 10482 if (!checkonly) 10483 tcp->tcp_exclbind = onoff; 10484 break; /* goto sizeof (int) option return */ 10485 case TCP_INIT_CWND: { 10486 uint32_t init_cwnd = *((uint32_t *)invalp); 10487 10488 if (checkonly) 10489 break; 10490 10491 /* 10492 * Only allow socket with network configuration 10493 * privilege to set the initial cwnd to be larger 10494 * than allowed by RFC 3390. 10495 */ 10496 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10497 tcp->tcp_init_cwnd = init_cwnd; 10498 break; 10499 } 10500 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10501 *outlenp = 0; 10502 return (reterr); 10503 } 10504 if (init_cwnd > TCP_MAX_INIT_CWND) { 10505 *outlenp = 0; 10506 return (EINVAL); 10507 } 10508 tcp->tcp_init_cwnd = init_cwnd; 10509 break; 10510 } 10511 case TCP_KEEPALIVE_THRESHOLD: 10512 if (checkonly) 10513 break; 10514 10515 if (*i1 < tcps->tcps_keepalive_interval_low || 10516 *i1 > tcps->tcps_keepalive_interval_high) { 10517 *outlenp = 0; 10518 return (EINVAL); 10519 } 10520 if (*i1 != tcp->tcp_ka_interval) { 10521 tcp->tcp_ka_interval = *i1; 10522 /* 10523 * Check if we need to restart the 10524 * keepalive timer. 10525 */ 10526 if (tcp->tcp_ka_tid != 0) { 10527 ASSERT(tcp->tcp_ka_enabled); 10528 (void) TCP_TIMER_CANCEL(tcp, 10529 tcp->tcp_ka_tid); 10530 tcp->tcp_ka_last_intrvl = 0; 10531 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10532 tcp_keepalive_killer, 10533 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10534 } 10535 } 10536 break; 10537 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10538 if (!checkonly) { 10539 if (*i1 < 10540 tcps->tcps_keepalive_abort_interval_low || 10541 *i1 > 10542 tcps->tcps_keepalive_abort_interval_high) { 10543 *outlenp = 0; 10544 return (EINVAL); 10545 } 10546 tcp->tcp_ka_abort_thres = *i1; 10547 } 10548 break; 10549 case TCP_CORK: 10550 if (!checkonly) { 10551 /* 10552 * if tcp->tcp_cork was set and is now 10553 * being unset, we have to make sure that 10554 * the remaining data gets sent out. Also 10555 * unset tcp->tcp_cork so that tcp_wput_data() 10556 * can send data even if it is less than mss 10557 */ 10558 if (tcp->tcp_cork && onoff == 0 && 10559 tcp->tcp_unsent > 0) { 10560 tcp->tcp_cork = B_FALSE; 10561 tcp_wput_data(tcp, NULL, B_FALSE); 10562 } 10563 tcp->tcp_cork = onoff; 10564 } 10565 break; 10566 default: 10567 *outlenp = 0; 10568 return (EINVAL); 10569 } 10570 break; 10571 case IPPROTO_IP: 10572 if (tcp->tcp_family != AF_INET) { 10573 *outlenp = 0; 10574 return (ENOPROTOOPT); 10575 } 10576 switch (name) { 10577 case IP_OPTIONS: 10578 case T_IP_OPTIONS: 10579 reterr = tcp_opt_set_header(tcp, checkonly, 10580 invalp, inlen); 10581 if (reterr) { 10582 *outlenp = 0; 10583 return (reterr); 10584 } 10585 /* OK return - copy input buffer into output buffer */ 10586 if (invalp != outvalp) { 10587 /* don't trust bcopy for identical src/dst */ 10588 bcopy(invalp, outvalp, inlen); 10589 } 10590 *outlenp = inlen; 10591 return (0); 10592 case IP_TOS: 10593 case T_IP_TOS: 10594 if (!checkonly) { 10595 tcp->tcp_ipha->ipha_type_of_service = 10596 (uchar_t)*i1; 10597 tcp->tcp_tos = (uchar_t)*i1; 10598 } 10599 break; 10600 case IP_TTL: 10601 if (!checkonly) { 10602 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10603 tcp->tcp_ttl = (uchar_t)*i1; 10604 } 10605 break; 10606 case IP_BOUND_IF: 10607 case IP_NEXTHOP: 10608 /* Handled at the IP level */ 10609 return (-EINVAL); 10610 case IP_SEC_OPT: 10611 /* 10612 * We should not allow policy setting after 10613 * we start listening for connections. 10614 */ 10615 if (tcp->tcp_state == TCPS_LISTEN) { 10616 return (EINVAL); 10617 } else { 10618 /* Handled at the IP level */ 10619 return (-EINVAL); 10620 } 10621 default: 10622 *outlenp = 0; 10623 return (EINVAL); 10624 } 10625 break; 10626 case IPPROTO_IPV6: { 10627 ip6_pkt_t *ipp; 10628 10629 /* 10630 * IPPROTO_IPV6 options are only supported for sockets 10631 * that are using IPv6 on the wire. 10632 */ 10633 if (tcp->tcp_ipversion != IPV6_VERSION) { 10634 *outlenp = 0; 10635 return (ENOPROTOOPT); 10636 } 10637 /* 10638 * Only sticky options; no ancillary data 10639 */ 10640 ASSERT(thisdg_attrs == NULL); 10641 ipp = &tcp->tcp_sticky_ipp; 10642 10643 switch (name) { 10644 case IPV6_UNICAST_HOPS: 10645 /* -1 means use default */ 10646 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10647 *outlenp = 0; 10648 return (EINVAL); 10649 } 10650 if (!checkonly) { 10651 if (*i1 == -1) { 10652 tcp->tcp_ip6h->ip6_hops = 10653 ipp->ipp_unicast_hops = 10654 (uint8_t)tcps->tcps_ipv6_hoplimit; 10655 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10656 /* Pass modified value to IP. */ 10657 *i1 = tcp->tcp_ip6h->ip6_hops; 10658 } else { 10659 tcp->tcp_ip6h->ip6_hops = 10660 ipp->ipp_unicast_hops = 10661 (uint8_t)*i1; 10662 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10663 } 10664 reterr = tcp_build_hdrs(q, tcp); 10665 if (reterr != 0) 10666 return (reterr); 10667 } 10668 break; 10669 case IPV6_BOUND_IF: 10670 if (!checkonly) { 10671 int error = 0; 10672 10673 tcp->tcp_bound_if = *i1; 10674 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10675 B_TRUE, checkonly, level, name, mblk); 10676 if (error != 0) { 10677 *outlenp = 0; 10678 return (error); 10679 } 10680 } 10681 break; 10682 /* 10683 * Set boolean switches for ancillary data delivery 10684 */ 10685 case IPV6_RECVPKTINFO: 10686 if (!checkonly) { 10687 if (onoff) 10688 tcp->tcp_ipv6_recvancillary |= 10689 TCP_IPV6_RECVPKTINFO; 10690 else 10691 tcp->tcp_ipv6_recvancillary &= 10692 ~TCP_IPV6_RECVPKTINFO; 10693 /* Force it to be sent up with the next msg */ 10694 tcp->tcp_recvifindex = 0; 10695 } 10696 break; 10697 case IPV6_RECVTCLASS: 10698 if (!checkonly) { 10699 if (onoff) 10700 tcp->tcp_ipv6_recvancillary |= 10701 TCP_IPV6_RECVTCLASS; 10702 else 10703 tcp->tcp_ipv6_recvancillary &= 10704 ~TCP_IPV6_RECVTCLASS; 10705 } 10706 break; 10707 case IPV6_RECVHOPLIMIT: 10708 if (!checkonly) { 10709 if (onoff) 10710 tcp->tcp_ipv6_recvancillary |= 10711 TCP_IPV6_RECVHOPLIMIT; 10712 else 10713 tcp->tcp_ipv6_recvancillary &= 10714 ~TCP_IPV6_RECVHOPLIMIT; 10715 /* Force it to be sent up with the next msg */ 10716 tcp->tcp_recvhops = 0xffffffffU; 10717 } 10718 break; 10719 case IPV6_RECVHOPOPTS: 10720 if (!checkonly) { 10721 if (onoff) 10722 tcp->tcp_ipv6_recvancillary |= 10723 TCP_IPV6_RECVHOPOPTS; 10724 else 10725 tcp->tcp_ipv6_recvancillary &= 10726 ~TCP_IPV6_RECVHOPOPTS; 10727 } 10728 break; 10729 case IPV6_RECVDSTOPTS: 10730 if (!checkonly) { 10731 if (onoff) 10732 tcp->tcp_ipv6_recvancillary |= 10733 TCP_IPV6_RECVDSTOPTS; 10734 else 10735 tcp->tcp_ipv6_recvancillary &= 10736 ~TCP_IPV6_RECVDSTOPTS; 10737 } 10738 break; 10739 case _OLD_IPV6_RECVDSTOPTS: 10740 if (!checkonly) { 10741 if (onoff) 10742 tcp->tcp_ipv6_recvancillary |= 10743 TCP_OLD_IPV6_RECVDSTOPTS; 10744 else 10745 tcp->tcp_ipv6_recvancillary &= 10746 ~TCP_OLD_IPV6_RECVDSTOPTS; 10747 } 10748 break; 10749 case IPV6_RECVRTHDR: 10750 if (!checkonly) { 10751 if (onoff) 10752 tcp->tcp_ipv6_recvancillary |= 10753 TCP_IPV6_RECVRTHDR; 10754 else 10755 tcp->tcp_ipv6_recvancillary &= 10756 ~TCP_IPV6_RECVRTHDR; 10757 } 10758 break; 10759 case IPV6_RECVRTHDRDSTOPTS: 10760 if (!checkonly) { 10761 if (onoff) 10762 tcp->tcp_ipv6_recvancillary |= 10763 TCP_IPV6_RECVRTDSTOPTS; 10764 else 10765 tcp->tcp_ipv6_recvancillary &= 10766 ~TCP_IPV6_RECVRTDSTOPTS; 10767 } 10768 break; 10769 case IPV6_PKTINFO: 10770 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10771 return (EINVAL); 10772 if (checkonly) 10773 break; 10774 10775 if (inlen == 0) { 10776 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10777 } else { 10778 struct in6_pktinfo *pkti; 10779 10780 pkti = (struct in6_pktinfo *)invalp; 10781 /* 10782 * RFC 3542 states that ipi6_addr must be 10783 * the unspecified address when setting the 10784 * IPV6_PKTINFO sticky socket option on a 10785 * TCP socket. 10786 */ 10787 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10788 return (EINVAL); 10789 /* 10790 * ip6_set_pktinfo() validates the source 10791 * address and interface index. 10792 */ 10793 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10794 pkti, mblk); 10795 if (reterr != 0) 10796 return (reterr); 10797 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10798 ipp->ipp_addr = pkti->ipi6_addr; 10799 if (ipp->ipp_ifindex != 0) 10800 ipp->ipp_fields |= IPPF_IFINDEX; 10801 else 10802 ipp->ipp_fields &= ~IPPF_IFINDEX; 10803 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10804 ipp->ipp_fields |= IPPF_ADDR; 10805 else 10806 ipp->ipp_fields &= ~IPPF_ADDR; 10807 } 10808 reterr = tcp_build_hdrs(q, tcp); 10809 if (reterr != 0) 10810 return (reterr); 10811 break; 10812 case IPV6_TCLASS: 10813 if (inlen != 0 && inlen != sizeof (int)) 10814 return (EINVAL); 10815 if (checkonly) 10816 break; 10817 10818 if (inlen == 0) { 10819 ipp->ipp_fields &= ~IPPF_TCLASS; 10820 } else { 10821 if (*i1 > 255 || *i1 < -1) 10822 return (EINVAL); 10823 if (*i1 == -1) { 10824 ipp->ipp_tclass = 0; 10825 *i1 = 0; 10826 } else { 10827 ipp->ipp_tclass = *i1; 10828 } 10829 ipp->ipp_fields |= IPPF_TCLASS; 10830 } 10831 reterr = tcp_build_hdrs(q, tcp); 10832 if (reterr != 0) 10833 return (reterr); 10834 break; 10835 case IPV6_NEXTHOP: 10836 /* 10837 * IP will verify that the nexthop is reachable 10838 * and fail for sticky options. 10839 */ 10840 if (inlen != 0 && inlen != sizeof (sin6_t)) 10841 return (EINVAL); 10842 if (checkonly) 10843 break; 10844 10845 if (inlen == 0) { 10846 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10847 } else { 10848 sin6_t *sin6 = (sin6_t *)invalp; 10849 10850 if (sin6->sin6_family != AF_INET6) 10851 return (EAFNOSUPPORT); 10852 if (IN6_IS_ADDR_V4MAPPED( 10853 &sin6->sin6_addr)) 10854 return (EADDRNOTAVAIL); 10855 ipp->ipp_nexthop = sin6->sin6_addr; 10856 if (!IN6_IS_ADDR_UNSPECIFIED( 10857 &ipp->ipp_nexthop)) 10858 ipp->ipp_fields |= IPPF_NEXTHOP; 10859 else 10860 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10861 } 10862 reterr = tcp_build_hdrs(q, tcp); 10863 if (reterr != 0) 10864 return (reterr); 10865 break; 10866 case IPV6_HOPOPTS: { 10867 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10868 10869 /* 10870 * Sanity checks - minimum size, size a multiple of 10871 * eight bytes, and matching size passed in. 10872 */ 10873 if (inlen != 0 && 10874 inlen != (8 * (hopts->ip6h_len + 1))) 10875 return (EINVAL); 10876 10877 if (checkonly) 10878 break; 10879 10880 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10881 (uchar_t **)&ipp->ipp_hopopts, 10882 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10883 if (reterr != 0) 10884 return (reterr); 10885 if (ipp->ipp_hopoptslen == 0) 10886 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10887 else 10888 ipp->ipp_fields |= IPPF_HOPOPTS; 10889 reterr = tcp_build_hdrs(q, tcp); 10890 if (reterr != 0) 10891 return (reterr); 10892 break; 10893 } 10894 case IPV6_RTHDRDSTOPTS: { 10895 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10896 10897 /* 10898 * Sanity checks - minimum size, size a multiple of 10899 * eight bytes, and matching size passed in. 10900 */ 10901 if (inlen != 0 && 10902 inlen != (8 * (dopts->ip6d_len + 1))) 10903 return (EINVAL); 10904 10905 if (checkonly) 10906 break; 10907 10908 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10909 (uchar_t **)&ipp->ipp_rtdstopts, 10910 &ipp->ipp_rtdstoptslen, 0); 10911 if (reterr != 0) 10912 return (reterr); 10913 if (ipp->ipp_rtdstoptslen == 0) 10914 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10915 else 10916 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10917 reterr = tcp_build_hdrs(q, tcp); 10918 if (reterr != 0) 10919 return (reterr); 10920 break; 10921 } 10922 case IPV6_DSTOPTS: { 10923 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10924 10925 /* 10926 * Sanity checks - minimum size, size a multiple of 10927 * eight bytes, and matching size passed in. 10928 */ 10929 if (inlen != 0 && 10930 inlen != (8 * (dopts->ip6d_len + 1))) 10931 return (EINVAL); 10932 10933 if (checkonly) 10934 break; 10935 10936 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10937 (uchar_t **)&ipp->ipp_dstopts, 10938 &ipp->ipp_dstoptslen, 0); 10939 if (reterr != 0) 10940 return (reterr); 10941 if (ipp->ipp_dstoptslen == 0) 10942 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10943 else 10944 ipp->ipp_fields |= IPPF_DSTOPTS; 10945 reterr = tcp_build_hdrs(q, tcp); 10946 if (reterr != 0) 10947 return (reterr); 10948 break; 10949 } 10950 case IPV6_RTHDR: { 10951 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10952 10953 /* 10954 * Sanity checks - minimum size, size a multiple of 10955 * eight bytes, and matching size passed in. 10956 */ 10957 if (inlen != 0 && 10958 inlen != (8 * (rt->ip6r_len + 1))) 10959 return (EINVAL); 10960 10961 if (checkonly) 10962 break; 10963 10964 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10965 (uchar_t **)&ipp->ipp_rthdr, 10966 &ipp->ipp_rthdrlen, 0); 10967 if (reterr != 0) 10968 return (reterr); 10969 if (ipp->ipp_rthdrlen == 0) 10970 ipp->ipp_fields &= ~IPPF_RTHDR; 10971 else 10972 ipp->ipp_fields |= IPPF_RTHDR; 10973 reterr = tcp_build_hdrs(q, tcp); 10974 if (reterr != 0) 10975 return (reterr); 10976 break; 10977 } 10978 case IPV6_V6ONLY: 10979 if (!checkonly) 10980 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10981 break; 10982 case IPV6_USE_MIN_MTU: 10983 if (inlen != sizeof (int)) 10984 return (EINVAL); 10985 10986 if (*i1 < -1 || *i1 > 1) 10987 return (EINVAL); 10988 10989 if (checkonly) 10990 break; 10991 10992 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10993 ipp->ipp_use_min_mtu = *i1; 10994 break; 10995 case IPV6_BOUND_PIF: 10996 /* Handled at the IP level */ 10997 return (-EINVAL); 10998 case IPV6_SEC_OPT: 10999 /* 11000 * We should not allow policy setting after 11001 * we start listening for connections. 11002 */ 11003 if (tcp->tcp_state == TCPS_LISTEN) { 11004 return (EINVAL); 11005 } else { 11006 /* Handled at the IP level */ 11007 return (-EINVAL); 11008 } 11009 case IPV6_SRC_PREFERENCES: 11010 if (inlen != sizeof (uint32_t)) 11011 return (EINVAL); 11012 reterr = ip6_set_src_preferences(tcp->tcp_connp, 11013 *(uint32_t *)invalp); 11014 if (reterr != 0) { 11015 *outlenp = 0; 11016 return (reterr); 11017 } 11018 break; 11019 default: 11020 *outlenp = 0; 11021 return (EINVAL); 11022 } 11023 break; 11024 } /* end IPPROTO_IPV6 */ 11025 default: 11026 *outlenp = 0; 11027 return (EINVAL); 11028 } 11029 /* 11030 * Common case of OK return with outval same as inval 11031 */ 11032 if (invalp != outvalp) { 11033 /* don't trust bcopy for identical src/dst */ 11034 (void) bcopy(invalp, outvalp, inlen); 11035 } 11036 *outlenp = inlen; 11037 return (0); 11038 } 11039 11040 /* 11041 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 11042 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 11043 * headers, and the maximum size tcp header (to avoid reallocation 11044 * on the fly for additional tcp options). 11045 * Returns failure if can't allocate memory. 11046 */ 11047 static int 11048 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 11049 { 11050 char *hdrs; 11051 uint_t hdrs_len; 11052 ip6i_t *ip6i; 11053 char buf[TCP_MAX_HDR_LENGTH]; 11054 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 11055 in6_addr_t src, dst; 11056 tcp_stack_t *tcps = tcp->tcp_tcps; 11057 11058 /* 11059 * save the existing tcp header and source/dest IP addresses 11060 */ 11061 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 11062 src = tcp->tcp_ip6h->ip6_src; 11063 dst = tcp->tcp_ip6h->ip6_dst; 11064 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 11065 ASSERT(hdrs_len != 0); 11066 if (hdrs_len > tcp->tcp_iphc_len) { 11067 /* Need to reallocate */ 11068 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 11069 if (hdrs == NULL) 11070 return (ENOMEM); 11071 if (tcp->tcp_iphc != NULL) { 11072 if (tcp->tcp_hdr_grown) { 11073 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 11074 } else { 11075 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 11076 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 11077 } 11078 tcp->tcp_iphc_len = 0; 11079 } 11080 ASSERT(tcp->tcp_iphc_len == 0); 11081 tcp->tcp_iphc = hdrs; 11082 tcp->tcp_iphc_len = hdrs_len; 11083 tcp->tcp_hdr_grown = B_TRUE; 11084 } 11085 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11086 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11087 11088 /* Set header fields not in ipp */ 11089 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11090 ip6i = (ip6i_t *)tcp->tcp_iphc; 11091 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11092 } else { 11093 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11094 } 11095 /* 11096 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11097 * 11098 * tcp->tcp_tcp_hdr_len doesn't change here. 11099 */ 11100 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11101 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11102 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11103 11104 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11105 11106 tcp->tcp_ip6h->ip6_src = src; 11107 tcp->tcp_ip6h->ip6_dst = dst; 11108 11109 /* 11110 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11111 * the default value for TCP. 11112 */ 11113 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11114 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 11115 11116 /* 11117 * If we're setting extension headers after a connection 11118 * has been established, and if we have a routing header 11119 * among the extension headers, call ip_massage_options_v6 to 11120 * manipulate the routing header/ip6_dst set the checksum 11121 * difference in the tcp header template. 11122 * (This happens in tcp_connect_ipv6 if the routing header 11123 * is set prior to the connect.) 11124 * Set the tcp_sum to zero first in case we've cleared a 11125 * routing header or don't have one at all. 11126 */ 11127 tcp->tcp_sum = 0; 11128 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11129 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11130 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11131 (uint8_t *)tcp->tcp_tcph); 11132 if (rth != NULL) { 11133 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11134 rth, tcps->tcps_netstack); 11135 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11136 (tcp->tcp_sum >> 16)); 11137 } 11138 } 11139 11140 /* Try to get everything in a single mblk */ 11141 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcps->tcps_wroff_xtra); 11142 return (0); 11143 } 11144 11145 /* 11146 * Transfer any source route option from ipha to buf/dst in reversed form. 11147 */ 11148 static int 11149 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11150 { 11151 ipoptp_t opts; 11152 uchar_t *opt; 11153 uint8_t optval; 11154 uint8_t optlen; 11155 uint32_t len = 0; 11156 11157 for (optval = ipoptp_first(&opts, ipha); 11158 optval != IPOPT_EOL; 11159 optval = ipoptp_next(&opts)) { 11160 opt = opts.ipoptp_cur; 11161 optlen = opts.ipoptp_len; 11162 switch (optval) { 11163 int off1, off2; 11164 case IPOPT_SSRR: 11165 case IPOPT_LSRR: 11166 11167 /* Reverse source route */ 11168 /* 11169 * First entry should be the next to last one in the 11170 * current source route (the last entry is our 11171 * address.) 11172 * The last entry should be the final destination. 11173 */ 11174 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11175 buf[IPOPT_OLEN] = (uint8_t)optlen; 11176 off1 = IPOPT_MINOFF_SR - 1; 11177 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11178 if (off2 < 0) { 11179 /* No entries in source route */ 11180 break; 11181 } 11182 bcopy(opt + off2, dst, IP_ADDR_LEN); 11183 /* 11184 * Note: use src since ipha has not had its src 11185 * and dst reversed (it is in the state it was 11186 * received. 11187 */ 11188 bcopy(&ipha->ipha_src, buf + off2, 11189 IP_ADDR_LEN); 11190 off2 -= IP_ADDR_LEN; 11191 11192 while (off2 > 0) { 11193 bcopy(opt + off2, buf + off1, 11194 IP_ADDR_LEN); 11195 off1 += IP_ADDR_LEN; 11196 off2 -= IP_ADDR_LEN; 11197 } 11198 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11199 buf += optlen; 11200 len += optlen; 11201 break; 11202 } 11203 } 11204 done: 11205 /* Pad the resulting options */ 11206 while (len & 0x3) { 11207 *buf++ = IPOPT_EOL; 11208 len++; 11209 } 11210 return (len); 11211 } 11212 11213 11214 /* 11215 * Extract and revert a source route from ipha (if any) 11216 * and then update the relevant fields in both tcp_t and the standard header. 11217 */ 11218 static void 11219 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11220 { 11221 char buf[TCP_MAX_HDR_LENGTH]; 11222 uint_t tcph_len; 11223 int len; 11224 11225 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11226 len = IPH_HDR_LENGTH(ipha); 11227 if (len == IP_SIMPLE_HDR_LENGTH) 11228 /* Nothing to do */ 11229 return; 11230 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11231 (len & 0x3)) 11232 return; 11233 11234 tcph_len = tcp->tcp_tcp_hdr_len; 11235 bcopy(tcp->tcp_tcph, buf, tcph_len); 11236 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11237 (tcp->tcp_ipha->ipha_dst & 0xffff); 11238 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11239 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11240 len += IP_SIMPLE_HDR_LENGTH; 11241 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11242 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11243 if ((int)tcp->tcp_sum < 0) 11244 tcp->tcp_sum--; 11245 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11246 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11247 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11248 bcopy(buf, tcp->tcp_tcph, tcph_len); 11249 tcp->tcp_ip_hdr_len = len; 11250 tcp->tcp_ipha->ipha_version_and_hdr_length = 11251 (IP_VERSION << 4) | (len >> 2); 11252 len += tcph_len; 11253 tcp->tcp_hdr_len = len; 11254 } 11255 11256 /* 11257 * Copy the standard header into its new location, 11258 * lay in the new options and then update the relevant 11259 * fields in both tcp_t and the standard header. 11260 */ 11261 static int 11262 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11263 { 11264 uint_t tcph_len; 11265 uint8_t *ip_optp; 11266 tcph_t *new_tcph; 11267 tcp_stack_t *tcps = tcp->tcp_tcps; 11268 11269 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11270 return (EINVAL); 11271 11272 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11273 return (EINVAL); 11274 11275 if (checkonly) { 11276 /* 11277 * do not really set, just pretend to - T_CHECK 11278 */ 11279 return (0); 11280 } 11281 11282 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11283 if (tcp->tcp_label_len > 0) { 11284 int padlen; 11285 uint8_t opt; 11286 11287 /* convert list termination to no-ops */ 11288 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11289 ip_optp += ip_optp[IPOPT_OLEN]; 11290 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11291 while (--padlen >= 0) 11292 *ip_optp++ = opt; 11293 } 11294 tcph_len = tcp->tcp_tcp_hdr_len; 11295 new_tcph = (tcph_t *)(ip_optp + len); 11296 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11297 tcp->tcp_tcph = new_tcph; 11298 bcopy(ptr, ip_optp, len); 11299 11300 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11301 11302 tcp->tcp_ip_hdr_len = len; 11303 tcp->tcp_ipha->ipha_version_and_hdr_length = 11304 (IP_VERSION << 4) | (len >> 2); 11305 tcp->tcp_hdr_len = len + tcph_len; 11306 if (!TCP_IS_DETACHED(tcp)) { 11307 /* Always allocate room for all options. */ 11308 (void) mi_set_sth_wroff(tcp->tcp_rq, 11309 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11310 } 11311 return (0); 11312 } 11313 11314 /* Get callback routine passed to nd_load by tcp_param_register */ 11315 /* ARGSUSED */ 11316 static int 11317 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11318 { 11319 tcpparam_t *tcppa = (tcpparam_t *)cp; 11320 11321 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11322 return (0); 11323 } 11324 11325 /* 11326 * Walk through the param array specified registering each element with the 11327 * named dispatch handler. 11328 */ 11329 static boolean_t 11330 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11331 { 11332 for (; cnt-- > 0; tcppa++) { 11333 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11334 if (!nd_load(ndp, tcppa->tcp_param_name, 11335 tcp_param_get, tcp_param_set, 11336 (caddr_t)tcppa)) { 11337 nd_free(ndp); 11338 return (B_FALSE); 11339 } 11340 } 11341 } 11342 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11343 KM_SLEEP); 11344 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11345 sizeof (tcpparam_t)); 11346 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11347 tcp_param_get, tcp_param_set_aligned, 11348 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11349 nd_free(ndp); 11350 return (B_FALSE); 11351 } 11352 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11353 KM_SLEEP); 11354 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11355 sizeof (tcpparam_t)); 11356 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11357 tcp_param_get, tcp_param_set_aligned, 11358 (caddr_t)tcps->tcps_mdt_head_param)) { 11359 nd_free(ndp); 11360 return (B_FALSE); 11361 } 11362 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11363 KM_SLEEP); 11364 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11365 sizeof (tcpparam_t)); 11366 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11367 tcp_param_get, tcp_param_set_aligned, 11368 (caddr_t)tcps->tcps_mdt_tail_param)) { 11369 nd_free(ndp); 11370 return (B_FALSE); 11371 } 11372 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11373 KM_SLEEP); 11374 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11375 sizeof (tcpparam_t)); 11376 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11377 tcp_param_get, tcp_param_set_aligned, 11378 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11379 nd_free(ndp); 11380 return (B_FALSE); 11381 } 11382 if (!nd_load(ndp, "tcp_extra_priv_ports", 11383 tcp_extra_priv_ports_get, NULL, NULL)) { 11384 nd_free(ndp); 11385 return (B_FALSE); 11386 } 11387 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11388 NULL, tcp_extra_priv_ports_add, NULL)) { 11389 nd_free(ndp); 11390 return (B_FALSE); 11391 } 11392 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11393 NULL, tcp_extra_priv_ports_del, NULL)) { 11394 nd_free(ndp); 11395 return (B_FALSE); 11396 } 11397 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11398 NULL)) { 11399 nd_free(ndp); 11400 return (B_FALSE); 11401 } 11402 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11403 NULL, NULL)) { 11404 nd_free(ndp); 11405 return (B_FALSE); 11406 } 11407 if (!nd_load(ndp, "tcp_listen_hash", 11408 tcp_listen_hash_report, NULL, NULL)) { 11409 nd_free(ndp); 11410 return (B_FALSE); 11411 } 11412 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11413 NULL, NULL)) { 11414 nd_free(ndp); 11415 return (B_FALSE); 11416 } 11417 if (!nd_load(ndp, "tcp_acceptor_hash", 11418 tcp_acceptor_hash_report, NULL, NULL)) { 11419 nd_free(ndp); 11420 return (B_FALSE); 11421 } 11422 if (!nd_load(ndp, "tcp_host_param", tcp_host_param_report, 11423 tcp_host_param_set, NULL)) { 11424 nd_free(ndp); 11425 return (B_FALSE); 11426 } 11427 if (!nd_load(ndp, "tcp_host_param_ipv6", 11428 tcp_host_param_report, tcp_host_param_set_ipv6, NULL)) { 11429 nd_free(ndp); 11430 return (B_FALSE); 11431 } 11432 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11433 tcp_1948_phrase_set, NULL)) { 11434 nd_free(ndp); 11435 return (B_FALSE); 11436 } 11437 if (!nd_load(ndp, "tcp_reserved_port_list", 11438 tcp_reserved_port_list, NULL, NULL)) { 11439 nd_free(ndp); 11440 return (B_FALSE); 11441 } 11442 /* 11443 * Dummy ndd variables - only to convey obsolescence information 11444 * through printing of their name (no get or set routines) 11445 * XXX Remove in future releases ? 11446 */ 11447 if (!nd_load(ndp, 11448 "tcp_close_wait_interval(obsoleted - " 11449 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11450 nd_free(ndp); 11451 return (B_FALSE); 11452 } 11453 return (B_TRUE); 11454 } 11455 11456 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11457 /* ARGSUSED */ 11458 static int 11459 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11460 cred_t *cr) 11461 { 11462 long new_value; 11463 tcpparam_t *tcppa = (tcpparam_t *)cp; 11464 11465 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11466 new_value < tcppa->tcp_param_min || 11467 new_value > tcppa->tcp_param_max) { 11468 return (EINVAL); 11469 } 11470 /* 11471 * Need to make sure new_value is a multiple of 4. If it is not, 11472 * round it up. For future 64 bit requirement, we actually make it 11473 * a multiple of 8. 11474 */ 11475 if (new_value & 0x7) { 11476 new_value = (new_value & ~0x7) + 0x8; 11477 } 11478 tcppa->tcp_param_val = new_value; 11479 return (0); 11480 } 11481 11482 /* Set callback routine passed to nd_load by tcp_param_register */ 11483 /* ARGSUSED */ 11484 static int 11485 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11486 { 11487 long new_value; 11488 tcpparam_t *tcppa = (tcpparam_t *)cp; 11489 11490 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11491 new_value < tcppa->tcp_param_min || 11492 new_value > tcppa->tcp_param_max) { 11493 return (EINVAL); 11494 } 11495 tcppa->tcp_param_val = new_value; 11496 return (0); 11497 } 11498 11499 /* 11500 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11501 * is filled, return as much as we can. The message passed in may be 11502 * multi-part, chained using b_cont. "start" is the starting sequence 11503 * number for this piece. 11504 */ 11505 static mblk_t * 11506 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11507 { 11508 uint32_t end; 11509 mblk_t *mp1; 11510 mblk_t *mp2; 11511 mblk_t *next_mp; 11512 uint32_t u1; 11513 tcp_stack_t *tcps = tcp->tcp_tcps; 11514 11515 /* Walk through all the new pieces. */ 11516 do { 11517 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11518 (uintptr_t)INT_MAX); 11519 end = start + (int)(mp->b_wptr - mp->b_rptr); 11520 next_mp = mp->b_cont; 11521 if (start == end) { 11522 /* Empty. Blast it. */ 11523 freeb(mp); 11524 continue; 11525 } 11526 mp->b_cont = NULL; 11527 TCP_REASS_SET_SEQ(mp, start); 11528 TCP_REASS_SET_END(mp, end); 11529 mp1 = tcp->tcp_reass_tail; 11530 if (!mp1) { 11531 tcp->tcp_reass_tail = mp; 11532 tcp->tcp_reass_head = mp; 11533 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11534 UPDATE_MIB(&tcps->tcps_mib, 11535 tcpInDataUnorderBytes, end - start); 11536 continue; 11537 } 11538 /* New stuff completely beyond tail? */ 11539 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11540 /* Link it on end. */ 11541 mp1->b_cont = mp; 11542 tcp->tcp_reass_tail = mp; 11543 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11544 UPDATE_MIB(&tcps->tcps_mib, 11545 tcpInDataUnorderBytes, end - start); 11546 continue; 11547 } 11548 mp1 = tcp->tcp_reass_head; 11549 u1 = TCP_REASS_SEQ(mp1); 11550 /* New stuff at the front? */ 11551 if (SEQ_LT(start, u1)) { 11552 /* Yes... Check for overlap. */ 11553 mp->b_cont = mp1; 11554 tcp->tcp_reass_head = mp; 11555 tcp_reass_elim_overlap(tcp, mp); 11556 continue; 11557 } 11558 /* 11559 * The new piece fits somewhere between the head and tail. 11560 * We find our slot, where mp1 precedes us and mp2 trails. 11561 */ 11562 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11563 u1 = TCP_REASS_SEQ(mp2); 11564 if (SEQ_LEQ(start, u1)) 11565 break; 11566 } 11567 /* Link ourselves in */ 11568 mp->b_cont = mp2; 11569 mp1->b_cont = mp; 11570 11571 /* Trim overlap with following mblk(s) first */ 11572 tcp_reass_elim_overlap(tcp, mp); 11573 11574 /* Trim overlap with preceding mblk */ 11575 tcp_reass_elim_overlap(tcp, mp1); 11576 11577 } while (start = end, mp = next_mp); 11578 mp1 = tcp->tcp_reass_head; 11579 /* Anything ready to go? */ 11580 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11581 return (NULL); 11582 /* Eat what we can off the queue */ 11583 for (;;) { 11584 mp = mp1->b_cont; 11585 end = TCP_REASS_END(mp1); 11586 TCP_REASS_SET_SEQ(mp1, 0); 11587 TCP_REASS_SET_END(mp1, 0); 11588 if (!mp) { 11589 tcp->tcp_reass_tail = NULL; 11590 break; 11591 } 11592 if (end != TCP_REASS_SEQ(mp)) { 11593 mp1->b_cont = NULL; 11594 break; 11595 } 11596 mp1 = mp; 11597 } 11598 mp1 = tcp->tcp_reass_head; 11599 tcp->tcp_reass_head = mp; 11600 return (mp1); 11601 } 11602 11603 /* Eliminate any overlap that mp may have over later mblks */ 11604 static void 11605 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11606 { 11607 uint32_t end; 11608 mblk_t *mp1; 11609 uint32_t u1; 11610 tcp_stack_t *tcps = tcp->tcp_tcps; 11611 11612 end = TCP_REASS_END(mp); 11613 while ((mp1 = mp->b_cont) != NULL) { 11614 u1 = TCP_REASS_SEQ(mp1); 11615 if (!SEQ_GT(end, u1)) 11616 break; 11617 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11618 mp->b_wptr -= end - u1; 11619 TCP_REASS_SET_END(mp, u1); 11620 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11621 UPDATE_MIB(&tcps->tcps_mib, 11622 tcpInDataPartDupBytes, end - u1); 11623 break; 11624 } 11625 mp->b_cont = mp1->b_cont; 11626 TCP_REASS_SET_SEQ(mp1, 0); 11627 TCP_REASS_SET_END(mp1, 0); 11628 freeb(mp1); 11629 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11630 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11631 } 11632 if (!mp1) 11633 tcp->tcp_reass_tail = mp; 11634 } 11635 11636 /* 11637 * Send up all messages queued on tcp_rcv_list. 11638 */ 11639 static uint_t 11640 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11641 { 11642 mblk_t *mp; 11643 uint_t ret = 0; 11644 uint_t thwin; 11645 #ifdef DEBUG 11646 uint_t cnt = 0; 11647 #endif 11648 tcp_stack_t *tcps = tcp->tcp_tcps; 11649 11650 /* Can't drain on an eager connection */ 11651 if (tcp->tcp_listener != NULL) 11652 return (ret); 11653 11654 /* 11655 * Handle two cases here: we are currently fused or we were 11656 * previously fused and have some urgent data to be delivered 11657 * upstream. The latter happens because we either ran out of 11658 * memory or were detached and therefore sending the SIGURG was 11659 * deferred until this point. In either case we pass control 11660 * over to tcp_fuse_rcv_drain() since it may need to complete 11661 * some work. 11662 */ 11663 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11664 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11665 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11666 &tcp->tcp_fused_sigurg_mp)) 11667 return (ret); 11668 } 11669 11670 while ((mp = tcp->tcp_rcv_list) != NULL) { 11671 tcp->tcp_rcv_list = mp->b_next; 11672 mp->b_next = NULL; 11673 #ifdef DEBUG 11674 cnt += msgdsize(mp); 11675 #endif 11676 /* Does this need SSL processing first? */ 11677 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11678 tcp_kssl_input(tcp, mp); 11679 continue; 11680 } 11681 putnext(q, mp); 11682 } 11683 ASSERT(cnt == tcp->tcp_rcv_cnt); 11684 tcp->tcp_rcv_last_head = NULL; 11685 tcp->tcp_rcv_last_tail = NULL; 11686 tcp->tcp_rcv_cnt = 0; 11687 11688 /* Learn the latest rwnd information that we sent to the other side. */ 11689 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11690 << tcp->tcp_rcv_ws; 11691 /* This is peer's calculated send window (our receive window). */ 11692 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11693 /* 11694 * Increase the receive window to max. But we need to do receiver 11695 * SWS avoidance. This means that we need to check the increase of 11696 * of receive window is at least 1 MSS. 11697 */ 11698 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11699 /* 11700 * If the window that the other side knows is less than max 11701 * deferred acks segments, send an update immediately. 11702 */ 11703 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11704 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11705 ret = TH_ACK_NEEDED; 11706 } 11707 tcp->tcp_rwnd = q->q_hiwat; 11708 } 11709 /* No need for the push timer now. */ 11710 if (tcp->tcp_push_tid != 0) { 11711 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11712 tcp->tcp_push_tid = 0; 11713 } 11714 return (ret); 11715 } 11716 11717 /* 11718 * Queue data on tcp_rcv_list which is a b_next chain. 11719 * tcp_rcv_last_head/tail is the last element of this chain. 11720 * Each element of the chain is a b_cont chain. 11721 * 11722 * M_DATA messages are added to the current element. 11723 * Other messages are added as new (b_next) elements. 11724 */ 11725 void 11726 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11727 { 11728 ASSERT(seg_len == msgdsize(mp)); 11729 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11730 11731 if (tcp->tcp_rcv_list == NULL) { 11732 ASSERT(tcp->tcp_rcv_last_head == NULL); 11733 tcp->tcp_rcv_list = mp; 11734 tcp->tcp_rcv_last_head = mp; 11735 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11736 tcp->tcp_rcv_last_tail->b_cont = mp; 11737 } else { 11738 tcp->tcp_rcv_last_head->b_next = mp; 11739 tcp->tcp_rcv_last_head = mp; 11740 } 11741 11742 while (mp->b_cont) 11743 mp = mp->b_cont; 11744 11745 tcp->tcp_rcv_last_tail = mp; 11746 tcp->tcp_rcv_cnt += seg_len; 11747 tcp->tcp_rwnd -= seg_len; 11748 } 11749 11750 /* 11751 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11752 * 11753 * This is the default entry function into TCP on the read side. TCP is 11754 * always entered via squeue i.e. using squeue's for mutual exclusion. 11755 * When classifier does a lookup to find the tcp, it also puts a reference 11756 * on the conn structure associated so the tcp is guaranteed to exist 11757 * when we come here. We still need to check the state because it might 11758 * as well has been closed. The squeue processing function i.e. squeue_enter, 11759 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 11760 * CONN_DEC_REF. 11761 * 11762 * Apart from the default entry point, IP also sends packets directly to 11763 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11764 * connections. 11765 */ 11766 void 11767 tcp_input(void *arg, mblk_t *mp, void *arg2) 11768 { 11769 conn_t *connp = (conn_t *)arg; 11770 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11771 11772 /* arg2 is the sqp */ 11773 ASSERT(arg2 != NULL); 11774 ASSERT(mp != NULL); 11775 11776 /* 11777 * Don't accept any input on a closed tcp as this TCP logically does 11778 * not exist on the system. Don't proceed further with this TCP. 11779 * For eg. this packet could trigger another close of this tcp 11780 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11781 * tcp_clean_death / tcp_closei_local must be called at most once 11782 * on a TCP. In this case we need to refeed the packet into the 11783 * classifier and figure out where the packet should go. Need to 11784 * preserve the recv_ill somehow. Until we figure that out, for 11785 * now just drop the packet if we can't classify the packet. 11786 */ 11787 if (tcp->tcp_state == TCPS_CLOSED || 11788 tcp->tcp_state == TCPS_BOUND) { 11789 conn_t *new_connp; 11790 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11791 11792 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11793 if (new_connp != NULL) { 11794 tcp_reinput(new_connp, mp, arg2); 11795 return; 11796 } 11797 /* We failed to classify. For now just drop the packet */ 11798 freemsg(mp); 11799 return; 11800 } 11801 11802 if (DB_TYPE(mp) == M_DATA) 11803 tcp_rput_data(connp, mp, arg2); 11804 else 11805 tcp_rput_common(tcp, mp); 11806 } 11807 11808 /* 11809 * The read side put procedure. 11810 * The packets passed up by ip are assume to be aligned according to 11811 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11812 */ 11813 static void 11814 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11815 { 11816 /* 11817 * tcp_rput_data() does not expect M_CTL except for the case 11818 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11819 * type. Need to make sure that any other M_CTLs don't make 11820 * it to tcp_rput_data since it is not expecting any and doesn't 11821 * check for it. 11822 */ 11823 if (DB_TYPE(mp) == M_CTL) { 11824 switch (*(uint32_t *)(mp->b_rptr)) { 11825 case TCP_IOC_ABORT_CONN: 11826 /* 11827 * Handle connection abort request. 11828 */ 11829 tcp_ioctl_abort_handler(tcp, mp); 11830 return; 11831 case IPSEC_IN: 11832 /* 11833 * Only secure icmp arrive in TCP and they 11834 * don't go through data path. 11835 */ 11836 tcp_icmp_error(tcp, mp); 11837 return; 11838 case IN_PKTINFO: 11839 /* 11840 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11841 * sockets that are receiving IPv4 traffic. tcp 11842 */ 11843 ASSERT(tcp->tcp_family == AF_INET6); 11844 ASSERT(tcp->tcp_ipv6_recvancillary & 11845 TCP_IPV6_RECVPKTINFO); 11846 tcp_rput_data(tcp->tcp_connp, mp, 11847 tcp->tcp_connp->conn_sqp); 11848 return; 11849 case MDT_IOC_INFO_UPDATE: 11850 /* 11851 * Handle Multidata information update; the 11852 * following routine will free the message. 11853 */ 11854 if (tcp->tcp_connp->conn_mdt_ok) { 11855 tcp_mdt_update(tcp, 11856 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11857 B_FALSE); 11858 } 11859 freemsg(mp); 11860 return; 11861 case LSO_IOC_INFO_UPDATE: 11862 /* 11863 * Handle LSO information update; the following 11864 * routine will free the message. 11865 */ 11866 if (tcp->tcp_connp->conn_lso_ok) { 11867 tcp_lso_update(tcp, 11868 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11869 } 11870 freemsg(mp); 11871 return; 11872 default: 11873 /* 11874 * tcp_icmp_err() will process the M_CTL packets. 11875 * Non-ICMP packets, if any, will be discarded in 11876 * tcp_icmp_err(). We will process the ICMP packet 11877 * even if we are TCP_IS_DETACHED_NONEAGER as the 11878 * incoming ICMP packet may result in changing 11879 * the tcp_mss, which we would need if we have 11880 * packets to retransmit. 11881 */ 11882 tcp_icmp_error(tcp, mp); 11883 return; 11884 } 11885 } 11886 11887 /* No point processing the message if tcp is already closed */ 11888 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11889 freemsg(mp); 11890 return; 11891 } 11892 11893 tcp_rput_other(tcp, mp); 11894 } 11895 11896 11897 /* The minimum of smoothed mean deviation in RTO calculation. */ 11898 #define TCP_SD_MIN 400 11899 11900 /* 11901 * Set RTO for this connection. The formula is from Jacobson and Karels' 11902 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11903 * are the same as those in Appendix A.2 of that paper. 11904 * 11905 * m = new measurement 11906 * sa = smoothed RTT average (8 * average estimates). 11907 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11908 */ 11909 static void 11910 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11911 { 11912 long m = TICK_TO_MSEC(rtt); 11913 clock_t sa = tcp->tcp_rtt_sa; 11914 clock_t sv = tcp->tcp_rtt_sd; 11915 clock_t rto; 11916 tcp_stack_t *tcps = tcp->tcp_tcps; 11917 11918 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11919 tcp->tcp_rtt_update++; 11920 11921 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11922 if (sa != 0) { 11923 /* 11924 * Update average estimator: 11925 * new rtt = 7/8 old rtt + 1/8 Error 11926 */ 11927 11928 /* m is now Error in estimate. */ 11929 m -= sa >> 3; 11930 if ((sa += m) <= 0) { 11931 /* 11932 * Don't allow the smoothed average to be negative. 11933 * We use 0 to denote reinitialization of the 11934 * variables. 11935 */ 11936 sa = 1; 11937 } 11938 11939 /* 11940 * Update deviation estimator: 11941 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11942 */ 11943 if (m < 0) 11944 m = -m; 11945 m -= sv >> 2; 11946 sv += m; 11947 } else { 11948 /* 11949 * This follows BSD's implementation. So the reinitialized 11950 * RTO is 3 * m. We cannot go less than 2 because if the 11951 * link is bandwidth dominated, doubling the window size 11952 * during slow start means doubling the RTT. We want to be 11953 * more conservative when we reinitialize our estimates. 3 11954 * is just a convenient number. 11955 */ 11956 sa = m << 3; 11957 sv = m << 1; 11958 } 11959 if (sv < TCP_SD_MIN) { 11960 /* 11961 * We do not know that if sa captures the delay ACK 11962 * effect as in a long train of segments, a receiver 11963 * does not delay its ACKs. So set the minimum of sv 11964 * to be TCP_SD_MIN, which is default to 400 ms, twice 11965 * of BSD DATO. That means the minimum of mean 11966 * deviation is 100 ms. 11967 * 11968 */ 11969 sv = TCP_SD_MIN; 11970 } 11971 tcp->tcp_rtt_sa = sa; 11972 tcp->tcp_rtt_sd = sv; 11973 /* 11974 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11975 * 11976 * Add tcp_rexmit_interval extra in case of extreme environment 11977 * where the algorithm fails to work. The default value of 11978 * tcp_rexmit_interval_extra should be 0. 11979 * 11980 * As we use a finer grained clock than BSD and update 11981 * RTO for every ACKs, add in another .25 of RTT to the 11982 * deviation of RTO to accomodate burstiness of 1/4 of 11983 * window size. 11984 */ 11985 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 11986 11987 if (rto > tcps->tcps_rexmit_interval_max) { 11988 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 11989 } else if (rto < tcps->tcps_rexmit_interval_min) { 11990 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 11991 } else { 11992 tcp->tcp_rto = rto; 11993 } 11994 11995 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11996 tcp->tcp_timer_backoff = 0; 11997 } 11998 11999 /* 12000 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12001 * send queue which starts at the given seq. no. 12002 * 12003 * Parameters: 12004 * tcp_t *tcp: the tcp instance pointer. 12005 * uint32_t seq: the starting seq. no of the requested segment. 12006 * int32_t *off: after the execution, *off will be the offset to 12007 * the returned mblk which points to the requested seq no. 12008 * It is the caller's responsibility to send in a non-null off. 12009 * 12010 * Return: 12011 * A mblk_t pointer pointing to the requested segment in send queue. 12012 */ 12013 static mblk_t * 12014 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12015 { 12016 int32_t cnt; 12017 mblk_t *mp; 12018 12019 /* Defensive coding. Make sure we don't send incorrect data. */ 12020 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12021 return (NULL); 12022 12023 cnt = seq - tcp->tcp_suna; 12024 mp = tcp->tcp_xmit_head; 12025 while (cnt > 0 && mp != NULL) { 12026 cnt -= mp->b_wptr - mp->b_rptr; 12027 if (cnt < 0) { 12028 cnt += mp->b_wptr - mp->b_rptr; 12029 break; 12030 } 12031 mp = mp->b_cont; 12032 } 12033 ASSERT(mp != NULL); 12034 *off = cnt; 12035 return (mp); 12036 } 12037 12038 /* 12039 * This function handles all retransmissions if SACK is enabled for this 12040 * connection. First it calculates how many segments can be retransmitted 12041 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12042 * segments. A segment is eligible if sack_cnt for that segment is greater 12043 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12044 * all eligible segments, it checks to see if TCP can send some new segments 12045 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12046 * 12047 * Parameters: 12048 * tcp_t *tcp: the tcp structure of the connection. 12049 * uint_t *flags: in return, appropriate value will be set for 12050 * tcp_rput_data(). 12051 */ 12052 static void 12053 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12054 { 12055 notsack_blk_t *notsack_blk; 12056 int32_t usable_swnd; 12057 int32_t mss; 12058 uint32_t seg_len; 12059 mblk_t *xmit_mp; 12060 tcp_stack_t *tcps = tcp->tcp_tcps; 12061 12062 ASSERT(tcp->tcp_sack_info != NULL); 12063 ASSERT(tcp->tcp_notsack_list != NULL); 12064 ASSERT(tcp->tcp_rexmit == B_FALSE); 12065 12066 /* Defensive coding in case there is a bug... */ 12067 if (tcp->tcp_notsack_list == NULL) { 12068 return; 12069 } 12070 notsack_blk = tcp->tcp_notsack_list; 12071 mss = tcp->tcp_mss; 12072 12073 /* 12074 * Limit the num of outstanding data in the network to be 12075 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12076 */ 12077 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12078 12079 /* At least retransmit 1 MSS of data. */ 12080 if (usable_swnd <= 0) { 12081 usable_swnd = mss; 12082 } 12083 12084 /* Make sure no new RTT samples will be taken. */ 12085 tcp->tcp_csuna = tcp->tcp_snxt; 12086 12087 notsack_blk = tcp->tcp_notsack_list; 12088 while (usable_swnd > 0) { 12089 mblk_t *snxt_mp, *tmp_mp; 12090 tcp_seq begin = tcp->tcp_sack_snxt; 12091 tcp_seq end; 12092 int32_t off; 12093 12094 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12095 if (SEQ_GT(notsack_blk->end, begin) && 12096 (notsack_blk->sack_cnt >= 12097 tcps->tcps_dupack_fast_retransmit)) { 12098 end = notsack_blk->end; 12099 if (SEQ_LT(begin, notsack_blk->begin)) { 12100 begin = notsack_blk->begin; 12101 } 12102 break; 12103 } 12104 } 12105 /* 12106 * All holes are filled. Manipulate tcp_cwnd to send more 12107 * if we can. Note that after the SACK recovery, tcp_cwnd is 12108 * set to tcp_cwnd_ssthresh. 12109 */ 12110 if (notsack_blk == NULL) { 12111 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12112 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12113 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12114 ASSERT(tcp->tcp_cwnd > 0); 12115 return; 12116 } else { 12117 usable_swnd = usable_swnd / mss; 12118 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12119 MAX(usable_swnd * mss, mss); 12120 *flags |= TH_XMIT_NEEDED; 12121 return; 12122 } 12123 } 12124 12125 /* 12126 * Note that we may send more than usable_swnd allows here 12127 * because of round off, but no more than 1 MSS of data. 12128 */ 12129 seg_len = end - begin; 12130 if (seg_len > mss) 12131 seg_len = mss; 12132 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12133 ASSERT(snxt_mp != NULL); 12134 /* This should not happen. Defensive coding again... */ 12135 if (snxt_mp == NULL) { 12136 return; 12137 } 12138 12139 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12140 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12141 if (xmit_mp == NULL) 12142 return; 12143 12144 usable_swnd -= seg_len; 12145 tcp->tcp_pipe += seg_len; 12146 tcp->tcp_sack_snxt = begin + seg_len; 12147 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 12148 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12149 12150 /* 12151 * Update the send timestamp to avoid false retransmission. 12152 */ 12153 snxt_mp->b_prev = (mblk_t *)lbolt; 12154 12155 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12156 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12157 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12158 /* 12159 * Update tcp_rexmit_max to extend this SACK recovery phase. 12160 * This happens when new data sent during fast recovery is 12161 * also lost. If TCP retransmits those new data, it needs 12162 * to extend SACK recover phase to avoid starting another 12163 * fast retransmit/recovery unnecessarily. 12164 */ 12165 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12166 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12167 } 12168 } 12169 } 12170 12171 /* 12172 * This function handles policy checking at TCP level for non-hard_bound/ 12173 * detached connections. 12174 */ 12175 static boolean_t 12176 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12177 boolean_t secure, boolean_t mctl_present) 12178 { 12179 ipsec_latch_t *ipl = NULL; 12180 ipsec_action_t *act = NULL; 12181 mblk_t *data_mp; 12182 ipsec_in_t *ii; 12183 const char *reason; 12184 kstat_named_t *counter; 12185 tcp_stack_t *tcps = tcp->tcp_tcps; 12186 ipsec_stack_t *ipss; 12187 ip_stack_t *ipst; 12188 12189 ASSERT(mctl_present || !secure); 12190 12191 ASSERT((ipha == NULL && ip6h != NULL) || 12192 (ip6h == NULL && ipha != NULL)); 12193 12194 /* 12195 * We don't necessarily have an ipsec_in_act action to verify 12196 * policy because of assymetrical policy where we have only 12197 * outbound policy and no inbound policy (possible with global 12198 * policy). 12199 */ 12200 if (!secure) { 12201 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12202 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12203 return (B_TRUE); 12204 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12205 "tcp_check_policy", ipha, ip6h, secure, 12206 tcps->tcps_netstack); 12207 ipss = tcps->tcps_netstack->netstack_ipsec; 12208 12209 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12210 DROPPER(ipss, ipds_tcp_clear), 12211 &tcps->tcps_dropper); 12212 return (B_FALSE); 12213 } 12214 12215 /* 12216 * We have a secure packet. 12217 */ 12218 if (act == NULL) { 12219 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 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_secure), 12226 &tcps->tcps_dropper); 12227 return (B_FALSE); 12228 } 12229 12230 /* 12231 * XXX This whole routine is currently incorrect. ipl should 12232 * be set to the latch pointer, but is currently not set, so 12233 * we initialize it to NULL to avoid picking up random garbage. 12234 */ 12235 if (ipl == NULL) 12236 return (B_TRUE); 12237 12238 data_mp = first_mp->b_cont; 12239 12240 ii = (ipsec_in_t *)first_mp->b_rptr; 12241 12242 ipst = tcps->tcps_netstack->netstack_ip; 12243 12244 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12245 &counter, tcp->tcp_connp)) { 12246 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12247 return (B_TRUE); 12248 } 12249 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12250 "tcp inbound policy mismatch: %s, packet dropped\n", 12251 reason); 12252 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12253 12254 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12255 &tcps->tcps_dropper); 12256 return (B_FALSE); 12257 } 12258 12259 /* 12260 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12261 * retransmission after a timeout. 12262 * 12263 * To limit the number of duplicate segments, we limit the number of segment 12264 * to be sent in one time to tcp_snd_burst, the burst variable. 12265 */ 12266 static void 12267 tcp_ss_rexmit(tcp_t *tcp) 12268 { 12269 uint32_t snxt; 12270 uint32_t smax; 12271 int32_t win; 12272 int32_t mss; 12273 int32_t off; 12274 int32_t burst = tcp->tcp_snd_burst; 12275 mblk_t *snxt_mp; 12276 tcp_stack_t *tcps = tcp->tcp_tcps; 12277 12278 /* 12279 * Note that tcp_rexmit can be set even though TCP has retransmitted 12280 * all unack'ed segments. 12281 */ 12282 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12283 smax = tcp->tcp_rexmit_max; 12284 snxt = tcp->tcp_rexmit_nxt; 12285 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12286 snxt = tcp->tcp_suna; 12287 } 12288 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12289 win -= snxt - tcp->tcp_suna; 12290 mss = tcp->tcp_mss; 12291 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12292 12293 while (SEQ_LT(snxt, smax) && (win > 0) && 12294 (burst > 0) && (snxt_mp != NULL)) { 12295 mblk_t *xmit_mp; 12296 mblk_t *old_snxt_mp = snxt_mp; 12297 uint32_t cnt = mss; 12298 12299 if (win < cnt) { 12300 cnt = win; 12301 } 12302 if (SEQ_GT(snxt + cnt, smax)) { 12303 cnt = smax - snxt; 12304 } 12305 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12306 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12307 if (xmit_mp == NULL) 12308 return; 12309 12310 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12311 12312 snxt += cnt; 12313 win -= cnt; 12314 /* 12315 * Update the send timestamp to avoid false 12316 * retransmission. 12317 */ 12318 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12319 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12320 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12321 12322 tcp->tcp_rexmit_nxt = snxt; 12323 burst--; 12324 } 12325 /* 12326 * If we have transmitted all we have at the time 12327 * we started the retranmission, we can leave 12328 * the rest of the job to tcp_wput_data(). But we 12329 * need to check the send window first. If the 12330 * win is not 0, go on with tcp_wput_data(). 12331 */ 12332 if (SEQ_LT(snxt, smax) || win == 0) { 12333 return; 12334 } 12335 } 12336 /* Only call tcp_wput_data() if there is data to be sent. */ 12337 if (tcp->tcp_unsent) { 12338 tcp_wput_data(tcp, NULL, B_FALSE); 12339 } 12340 } 12341 12342 /* 12343 * Process all TCP option in SYN segment. Note that this function should 12344 * be called after tcp_adapt_ire() is called so that the necessary info 12345 * from IRE is already set in the tcp structure. 12346 * 12347 * This function sets up the correct tcp_mss value according to the 12348 * MSS option value and our header size. It also sets up the window scale 12349 * and timestamp values, and initialize SACK info blocks. But it does not 12350 * change receive window size after setting the tcp_mss value. The caller 12351 * should do the appropriate change. 12352 */ 12353 void 12354 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12355 { 12356 int options; 12357 tcp_opt_t tcpopt; 12358 uint32_t mss_max; 12359 char *tmp_tcph; 12360 tcp_stack_t *tcps = tcp->tcp_tcps; 12361 12362 tcpopt.tcp = NULL; 12363 options = tcp_parse_options(tcph, &tcpopt); 12364 12365 /* 12366 * Process MSS option. Note that MSS option value does not account 12367 * for IP or TCP options. This means that it is equal to MTU - minimum 12368 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12369 * IPv6. 12370 */ 12371 if (!(options & TCP_OPT_MSS_PRESENT)) { 12372 if (tcp->tcp_ipversion == IPV4_VERSION) 12373 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12374 else 12375 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12376 } else { 12377 if (tcp->tcp_ipversion == IPV4_VERSION) 12378 mss_max = tcps->tcps_mss_max_ipv4; 12379 else 12380 mss_max = tcps->tcps_mss_max_ipv6; 12381 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12382 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12383 else if (tcpopt.tcp_opt_mss > mss_max) 12384 tcpopt.tcp_opt_mss = mss_max; 12385 } 12386 12387 /* Process Window Scale option. */ 12388 if (options & TCP_OPT_WSCALE_PRESENT) { 12389 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12390 tcp->tcp_snd_ws_ok = B_TRUE; 12391 } else { 12392 tcp->tcp_snd_ws = B_FALSE; 12393 tcp->tcp_snd_ws_ok = B_FALSE; 12394 tcp->tcp_rcv_ws = B_FALSE; 12395 } 12396 12397 /* Process Timestamp option. */ 12398 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12399 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12400 tmp_tcph = (char *)tcp->tcp_tcph; 12401 12402 tcp->tcp_snd_ts_ok = B_TRUE; 12403 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12404 tcp->tcp_last_rcv_lbolt = lbolt64; 12405 ASSERT(OK_32PTR(tmp_tcph)); 12406 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12407 12408 /* Fill in our template header with basic timestamp option. */ 12409 tmp_tcph += tcp->tcp_tcp_hdr_len; 12410 tmp_tcph[0] = TCPOPT_NOP; 12411 tmp_tcph[1] = TCPOPT_NOP; 12412 tmp_tcph[2] = TCPOPT_TSTAMP; 12413 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12414 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12415 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12416 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12417 } else { 12418 tcp->tcp_snd_ts_ok = B_FALSE; 12419 } 12420 12421 /* 12422 * Process SACK options. If SACK is enabled for this connection, 12423 * then allocate the SACK info structure. Note the following ways 12424 * when tcp_snd_sack_ok is set to true. 12425 * 12426 * For active connection: in tcp_adapt_ire() called in 12427 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12428 * is checked. 12429 * 12430 * For passive connection: in tcp_adapt_ire() called in 12431 * tcp_accept_comm(). 12432 * 12433 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12434 * That check makes sure that if we did not send a SACK OK option, 12435 * we will not enable SACK for this connection even though the other 12436 * side sends us SACK OK option. For active connection, the SACK 12437 * info structure has already been allocated. So we need to free 12438 * it if SACK is disabled. 12439 */ 12440 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12441 (tcp->tcp_snd_sack_ok || 12442 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12443 /* This should be true only in the passive case. */ 12444 if (tcp->tcp_sack_info == NULL) { 12445 ASSERT(TCP_IS_DETACHED(tcp)); 12446 tcp->tcp_sack_info = 12447 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12448 } 12449 if (tcp->tcp_sack_info == NULL) { 12450 tcp->tcp_snd_sack_ok = B_FALSE; 12451 } else { 12452 tcp->tcp_snd_sack_ok = B_TRUE; 12453 if (tcp->tcp_snd_ts_ok) { 12454 tcp->tcp_max_sack_blk = 3; 12455 } else { 12456 tcp->tcp_max_sack_blk = 4; 12457 } 12458 } 12459 } else { 12460 /* 12461 * Resetting tcp_snd_sack_ok to B_FALSE so that 12462 * no SACK info will be used for this 12463 * connection. This assumes that SACK usage 12464 * permission is negotiated. This may need 12465 * to be changed once this is clarified. 12466 */ 12467 if (tcp->tcp_sack_info != NULL) { 12468 ASSERT(tcp->tcp_notsack_list == NULL); 12469 kmem_cache_free(tcp_sack_info_cache, 12470 tcp->tcp_sack_info); 12471 tcp->tcp_sack_info = NULL; 12472 } 12473 tcp->tcp_snd_sack_ok = B_FALSE; 12474 } 12475 12476 /* 12477 * Now we know the exact TCP/IP header length, subtract 12478 * that from tcp_mss to get our side's MSS. 12479 */ 12480 tcp->tcp_mss -= tcp->tcp_hdr_len; 12481 /* 12482 * Here we assume that the other side's header size will be equal to 12483 * our header size. We calculate the real MSS accordingly. Need to 12484 * take into additional stuffs IPsec puts in. 12485 * 12486 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12487 */ 12488 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12489 ((tcp->tcp_ipversion == IPV4_VERSION ? 12490 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12491 12492 /* 12493 * Set MSS to the smaller one of both ends of the connection. 12494 * We should not have called tcp_mss_set() before, but our 12495 * side of the MSS should have been set to a proper value 12496 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12497 * STREAM head parameters properly. 12498 * 12499 * If we have a larger-than-16-bit window but the other side 12500 * didn't want to do window scale, tcp_rwnd_set() will take 12501 * care of that. 12502 */ 12503 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12504 } 12505 12506 /* 12507 * Sends the T_CONN_IND to the listener. The caller calls this 12508 * functions via squeue to get inside the listener's perimeter 12509 * once the 3 way hand shake is done a T_CONN_IND needs to be 12510 * sent. As an optimization, the caller can call this directly 12511 * if listener's perimeter is same as eager's. 12512 */ 12513 /* ARGSUSED */ 12514 void 12515 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12516 { 12517 conn_t *lconnp = (conn_t *)arg; 12518 tcp_t *listener = lconnp->conn_tcp; 12519 tcp_t *tcp; 12520 struct T_conn_ind *conn_ind; 12521 ipaddr_t *addr_cache; 12522 boolean_t need_send_conn_ind = B_FALSE; 12523 tcp_stack_t *tcps = listener->tcp_tcps; 12524 12525 /* retrieve the eager */ 12526 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12527 ASSERT(conn_ind->OPT_offset != 0 && 12528 conn_ind->OPT_length == sizeof (intptr_t)); 12529 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12530 conn_ind->OPT_length); 12531 12532 /* 12533 * TLI/XTI applications will get confused by 12534 * sending eager as an option since it violates 12535 * the option semantics. So remove the eager as 12536 * option since TLI/XTI app doesn't need it anyway. 12537 */ 12538 if (!TCP_IS_SOCKET(listener)) { 12539 conn_ind->OPT_length = 0; 12540 conn_ind->OPT_offset = 0; 12541 } 12542 if (listener->tcp_state == TCPS_CLOSED || 12543 TCP_IS_DETACHED(listener)) { 12544 /* 12545 * If listener has closed, it would have caused a 12546 * a cleanup/blowoff to happen for the eager. We 12547 * just need to return. 12548 */ 12549 freemsg(mp); 12550 return; 12551 } 12552 12553 12554 /* 12555 * if the conn_req_q is full defer passing up the 12556 * T_CONN_IND until space is availabe after t_accept() 12557 * processing 12558 */ 12559 mutex_enter(&listener->tcp_eager_lock); 12560 12561 /* 12562 * Take the eager out, if it is in the list of droppable eagers 12563 * as we are here because the 3W handshake is over. 12564 */ 12565 MAKE_UNDROPPABLE(tcp); 12566 12567 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12568 tcp_t *tail; 12569 12570 /* 12571 * The eager already has an extra ref put in tcp_rput_data 12572 * so that it stays till accept comes back even though it 12573 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12574 */ 12575 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12576 listener->tcp_conn_req_cnt_q0--; 12577 listener->tcp_conn_req_cnt_q++; 12578 12579 /* Move from SYN_RCVD to ESTABLISHED list */ 12580 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12581 tcp->tcp_eager_prev_q0; 12582 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12583 tcp->tcp_eager_next_q0; 12584 tcp->tcp_eager_prev_q0 = NULL; 12585 tcp->tcp_eager_next_q0 = NULL; 12586 12587 /* 12588 * Insert at end of the queue because sockfs 12589 * sends down T_CONN_RES in chronological 12590 * order. Leaving the older conn indications 12591 * at front of the queue helps reducing search 12592 * time. 12593 */ 12594 tail = listener->tcp_eager_last_q; 12595 if (tail != NULL) 12596 tail->tcp_eager_next_q = tcp; 12597 else 12598 listener->tcp_eager_next_q = tcp; 12599 listener->tcp_eager_last_q = tcp; 12600 tcp->tcp_eager_next_q = NULL; 12601 /* 12602 * Delay sending up the T_conn_ind until we are 12603 * done with the eager. Once we have have sent up 12604 * the T_conn_ind, the accept can potentially complete 12605 * any time and release the refhold we have on the eager. 12606 */ 12607 need_send_conn_ind = B_TRUE; 12608 } else { 12609 /* 12610 * Defer connection on q0 and set deferred 12611 * connection bit true 12612 */ 12613 tcp->tcp_conn_def_q0 = B_TRUE; 12614 12615 /* take tcp out of q0 ... */ 12616 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12617 tcp->tcp_eager_next_q0; 12618 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12619 tcp->tcp_eager_prev_q0; 12620 12621 /* ... and place it at the end of q0 */ 12622 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12623 tcp->tcp_eager_next_q0 = listener; 12624 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12625 listener->tcp_eager_prev_q0 = tcp; 12626 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12627 } 12628 12629 /* we have timed out before */ 12630 if (tcp->tcp_syn_rcvd_timeout != 0) { 12631 tcp->tcp_syn_rcvd_timeout = 0; 12632 listener->tcp_syn_rcvd_timeout--; 12633 if (listener->tcp_syn_defense && 12634 listener->tcp_syn_rcvd_timeout <= 12635 (tcps->tcps_conn_req_max_q0 >> 5) && 12636 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12637 listener->tcp_last_rcv_lbolt)) { 12638 /* 12639 * Turn off the defense mode if we 12640 * believe the SYN attack is over. 12641 */ 12642 listener->tcp_syn_defense = B_FALSE; 12643 if (listener->tcp_ip_addr_cache) { 12644 kmem_free((void *)listener->tcp_ip_addr_cache, 12645 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12646 listener->tcp_ip_addr_cache = NULL; 12647 } 12648 } 12649 } 12650 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12651 if (addr_cache != NULL) { 12652 /* 12653 * We have finished a 3-way handshake with this 12654 * remote host. This proves the IP addr is good. 12655 * Cache it! 12656 */ 12657 addr_cache[IP_ADDR_CACHE_HASH( 12658 tcp->tcp_remote)] = tcp->tcp_remote; 12659 } 12660 mutex_exit(&listener->tcp_eager_lock); 12661 if (need_send_conn_ind) 12662 putnext(listener->tcp_rq, mp); 12663 } 12664 12665 mblk_t * 12666 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12667 uint_t *ifindexp, ip6_pkt_t *ippp) 12668 { 12669 ip_pktinfo_t *pinfo; 12670 ip6_t *ip6h; 12671 uchar_t *rptr; 12672 mblk_t *first_mp = mp; 12673 boolean_t mctl_present = B_FALSE; 12674 uint_t ifindex = 0; 12675 ip6_pkt_t ipp; 12676 uint_t ipvers; 12677 uint_t ip_hdr_len; 12678 tcp_stack_t *tcps = tcp->tcp_tcps; 12679 12680 rptr = mp->b_rptr; 12681 ASSERT(OK_32PTR(rptr)); 12682 ASSERT(tcp != NULL); 12683 ipp.ipp_fields = 0; 12684 12685 switch DB_TYPE(mp) { 12686 case M_CTL: 12687 mp = mp->b_cont; 12688 if (mp == NULL) { 12689 freemsg(first_mp); 12690 return (NULL); 12691 } 12692 if (DB_TYPE(mp) != M_DATA) { 12693 freemsg(first_mp); 12694 return (NULL); 12695 } 12696 mctl_present = B_TRUE; 12697 break; 12698 case M_DATA: 12699 break; 12700 default: 12701 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12702 freemsg(mp); 12703 return (NULL); 12704 } 12705 ipvers = IPH_HDR_VERSION(rptr); 12706 if (ipvers == IPV4_VERSION) { 12707 if (tcp == NULL) { 12708 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12709 goto done; 12710 } 12711 12712 ipp.ipp_fields |= IPPF_HOPLIMIT; 12713 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12714 12715 /* 12716 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12717 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12718 */ 12719 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12720 mctl_present) { 12721 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12722 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12723 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12724 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12725 ipp.ipp_fields |= IPPF_IFINDEX; 12726 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12727 ifindex = pinfo->ip_pkt_ifindex; 12728 } 12729 freeb(first_mp); 12730 mctl_present = B_FALSE; 12731 } 12732 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12733 } else { 12734 ip6h = (ip6_t *)rptr; 12735 12736 ASSERT(ipvers == IPV6_VERSION); 12737 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12738 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12739 ipp.ipp_hoplimit = ip6h->ip6_hops; 12740 12741 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12742 uint8_t nexthdrp; 12743 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12744 12745 /* Look for ifindex information */ 12746 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12747 ip6i_t *ip6i = (ip6i_t *)ip6h; 12748 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12749 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12750 freemsg(first_mp); 12751 return (NULL); 12752 } 12753 12754 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12755 ASSERT(ip6i->ip6i_ifindex != 0); 12756 ipp.ipp_fields |= IPPF_IFINDEX; 12757 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12758 ifindex = ip6i->ip6i_ifindex; 12759 } 12760 rptr = (uchar_t *)&ip6i[1]; 12761 mp->b_rptr = rptr; 12762 if (rptr == mp->b_wptr) { 12763 mblk_t *mp1; 12764 mp1 = mp->b_cont; 12765 freeb(mp); 12766 mp = mp1; 12767 rptr = mp->b_rptr; 12768 } 12769 if (MBLKL(mp) < IPV6_HDR_LEN + 12770 sizeof (tcph_t)) { 12771 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12772 freemsg(first_mp); 12773 return (NULL); 12774 } 12775 ip6h = (ip6_t *)rptr; 12776 } 12777 12778 /* 12779 * Find any potentially interesting extension headers 12780 * as well as the length of the IPv6 + extension 12781 * headers. 12782 */ 12783 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12784 /* Verify if this is a TCP packet */ 12785 if (nexthdrp != IPPROTO_TCP) { 12786 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12787 freemsg(first_mp); 12788 return (NULL); 12789 } 12790 } else { 12791 ip_hdr_len = IPV6_HDR_LEN; 12792 } 12793 } 12794 12795 done: 12796 if (ipversp != NULL) 12797 *ipversp = ipvers; 12798 if (ip_hdr_lenp != NULL) 12799 *ip_hdr_lenp = ip_hdr_len; 12800 if (ippp != NULL) 12801 *ippp = ipp; 12802 if (ifindexp != NULL) 12803 *ifindexp = ifindex; 12804 if (mctl_present) { 12805 freeb(first_mp); 12806 } 12807 return (mp); 12808 } 12809 12810 /* 12811 * Handle M_DATA messages from IP. Its called directly from IP via 12812 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12813 * in this path. 12814 * 12815 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12816 * v4 and v6), we are called through tcp_input() and a M_CTL can 12817 * be present for options but tcp_find_pktinfo() deals with it. We 12818 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12819 * 12820 * The first argument is always the connp/tcp to which the mp belongs. 12821 * There are no exceptions to this rule. The caller has already put 12822 * a reference on this connp/tcp and once tcp_rput_data() returns, 12823 * the squeue will do the refrele. 12824 * 12825 * The TH_SYN for the listener directly go to tcp_conn_request via 12826 * squeue. 12827 * 12828 * sqp: NULL = recursive, sqp != NULL means called from squeue 12829 */ 12830 void 12831 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12832 { 12833 int32_t bytes_acked; 12834 int32_t gap; 12835 mblk_t *mp1; 12836 uint_t flags; 12837 uint32_t new_swnd = 0; 12838 uchar_t *iphdr; 12839 uchar_t *rptr; 12840 int32_t rgap; 12841 uint32_t seg_ack; 12842 int seg_len; 12843 uint_t ip_hdr_len; 12844 uint32_t seg_seq; 12845 tcph_t *tcph; 12846 int urp; 12847 tcp_opt_t tcpopt; 12848 uint_t ipvers; 12849 ip6_pkt_t ipp; 12850 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12851 uint32_t cwnd; 12852 uint32_t add; 12853 int npkt; 12854 int mss; 12855 conn_t *connp = (conn_t *)arg; 12856 squeue_t *sqp = (squeue_t *)arg2; 12857 tcp_t *tcp = connp->conn_tcp; 12858 tcp_stack_t *tcps = tcp->tcp_tcps; 12859 12860 /* 12861 * RST from fused tcp loopback peer should trigger an unfuse. 12862 */ 12863 if (tcp->tcp_fused) { 12864 TCP_STAT(tcps, tcp_fusion_aborted); 12865 tcp_unfuse(tcp); 12866 } 12867 12868 iphdr = mp->b_rptr; 12869 rptr = mp->b_rptr; 12870 ASSERT(OK_32PTR(rptr)); 12871 12872 /* 12873 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12874 * processing here. For rest call tcp_find_pktinfo to fill up the 12875 * necessary information. 12876 */ 12877 if (IPCL_IS_TCP4(connp)) { 12878 ipvers = IPV4_VERSION; 12879 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12880 } else { 12881 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12882 NULL, &ipp); 12883 if (mp == NULL) { 12884 TCP_STAT(tcps, tcp_rput_v6_error); 12885 return; 12886 } 12887 iphdr = mp->b_rptr; 12888 rptr = mp->b_rptr; 12889 } 12890 ASSERT(DB_TYPE(mp) == M_DATA); 12891 12892 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12893 seg_seq = ABE32_TO_U32(tcph->th_seq); 12894 seg_ack = ABE32_TO_U32(tcph->th_ack); 12895 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12896 seg_len = (int)(mp->b_wptr - rptr) - 12897 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12898 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12899 do { 12900 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12901 (uintptr_t)INT_MAX); 12902 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12903 } while ((mp1 = mp1->b_cont) != NULL && 12904 mp1->b_datap->db_type == M_DATA); 12905 } 12906 12907 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12908 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12909 seg_len, tcph); 12910 return; 12911 } 12912 12913 if (sqp != NULL) { 12914 /* 12915 * This is the correct place to update tcp_last_recv_time. Note 12916 * that it is also updated for tcp structure that belongs to 12917 * global and listener queues which do not really need updating. 12918 * But that should not cause any harm. And it is updated for 12919 * all kinds of incoming segments, not only for data segments. 12920 */ 12921 tcp->tcp_last_recv_time = lbolt; 12922 } 12923 12924 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12925 12926 BUMP_LOCAL(tcp->tcp_ibsegs); 12927 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 12928 12929 if ((flags & TH_URG) && sqp != NULL) { 12930 /* 12931 * TCP can't handle urgent pointers that arrive before 12932 * the connection has been accept()ed since it can't 12933 * buffer OOB data. Discard segment if this happens. 12934 * 12935 * Nor can it reassemble urgent pointers, so discard 12936 * if it's not the next segment expected. 12937 * 12938 * Otherwise, collapse chain into one mblk (discard if 12939 * that fails). This makes sure the headers, retransmitted 12940 * data, and new data all are in the same mblk. 12941 */ 12942 ASSERT(mp != NULL); 12943 if (tcp->tcp_listener || !pullupmsg(mp, -1)) { 12944 freemsg(mp); 12945 return; 12946 } 12947 /* Update pointers into message */ 12948 iphdr = rptr = mp->b_rptr; 12949 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12950 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12951 /* 12952 * Since we can't handle any data with this urgent 12953 * pointer that is out of sequence, we expunge 12954 * the data. This allows us to still register 12955 * the urgent mark and generate the M_PCSIG, 12956 * which we can do. 12957 */ 12958 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12959 seg_len = 0; 12960 } 12961 } 12962 12963 switch (tcp->tcp_state) { 12964 case TCPS_SYN_SENT: 12965 if (flags & TH_ACK) { 12966 /* 12967 * Note that our stack cannot send data before a 12968 * connection is established, therefore the 12969 * following check is valid. Otherwise, it has 12970 * to be changed. 12971 */ 12972 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12973 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12974 freemsg(mp); 12975 if (flags & TH_RST) 12976 return; 12977 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12978 tcp, seg_ack, 0, TH_RST); 12979 return; 12980 } 12981 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12982 } 12983 if (flags & TH_RST) { 12984 freemsg(mp); 12985 if (flags & TH_ACK) 12986 (void) tcp_clean_death(tcp, 12987 ECONNREFUSED, 13); 12988 return; 12989 } 12990 if (!(flags & TH_SYN)) { 12991 freemsg(mp); 12992 return; 12993 } 12994 12995 /* Process all TCP options. */ 12996 tcp_process_options(tcp, tcph); 12997 /* 12998 * The following changes our rwnd to be a multiple of the 12999 * MIN(peer MSS, our MSS) for performance reason. 13000 */ 13001 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 13002 tcp->tcp_mss)); 13003 13004 /* Is the other end ECN capable? */ 13005 if (tcp->tcp_ecn_ok) { 13006 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13007 tcp->tcp_ecn_ok = B_FALSE; 13008 } 13009 } 13010 /* 13011 * Clear ECN flags because it may interfere with later 13012 * processing. 13013 */ 13014 flags &= ~(TH_ECE|TH_CWR); 13015 13016 tcp->tcp_irs = seg_seq; 13017 tcp->tcp_rack = seg_seq; 13018 tcp->tcp_rnxt = seg_seq + 1; 13019 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13020 if (!TCP_IS_DETACHED(tcp)) { 13021 /* Allocate room for SACK options if needed. */ 13022 if (tcp->tcp_snd_sack_ok) { 13023 (void) mi_set_sth_wroff(tcp->tcp_rq, 13024 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13025 (tcp->tcp_loopback ? 0 : 13026 tcps->tcps_wroff_xtra)); 13027 } else { 13028 (void) mi_set_sth_wroff(tcp->tcp_rq, 13029 tcp->tcp_hdr_len + 13030 (tcp->tcp_loopback ? 0 : 13031 tcps->tcps_wroff_xtra)); 13032 } 13033 } 13034 if (flags & TH_ACK) { 13035 /* 13036 * If we can't get the confirmation upstream, pretend 13037 * we didn't even see this one. 13038 * 13039 * XXX: how can we pretend we didn't see it if we 13040 * have updated rnxt et. al. 13041 * 13042 * For loopback we defer sending up the T_CONN_CON 13043 * until after some checks below. 13044 */ 13045 mp1 = NULL; 13046 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13047 tcp->tcp_loopback ? &mp1 : NULL)) { 13048 freemsg(mp); 13049 return; 13050 } 13051 /* SYN was acked - making progress */ 13052 if (tcp->tcp_ipversion == IPV6_VERSION) 13053 tcp->tcp_ip_forward_progress = B_TRUE; 13054 13055 /* One for the SYN */ 13056 tcp->tcp_suna = tcp->tcp_iss + 1; 13057 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13058 tcp->tcp_state = TCPS_ESTABLISHED; 13059 13060 /* 13061 * If SYN was retransmitted, need to reset all 13062 * retransmission info. This is because this 13063 * segment will be treated as a dup ACK. 13064 */ 13065 if (tcp->tcp_rexmit) { 13066 tcp->tcp_rexmit = B_FALSE; 13067 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13068 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13069 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13070 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13071 tcp->tcp_ms_we_have_waited = 0; 13072 13073 /* 13074 * Set tcp_cwnd back to 1 MSS, per 13075 * recommendation from 13076 * draft-floyd-incr-init-win-01.txt, 13077 * Increasing TCP's Initial Window. 13078 */ 13079 tcp->tcp_cwnd = tcp->tcp_mss; 13080 } 13081 13082 tcp->tcp_swl1 = seg_seq; 13083 tcp->tcp_swl2 = seg_ack; 13084 13085 new_swnd = BE16_TO_U16(tcph->th_win); 13086 tcp->tcp_swnd = new_swnd; 13087 if (new_swnd > tcp->tcp_max_swnd) 13088 tcp->tcp_max_swnd = new_swnd; 13089 13090 /* 13091 * Always send the three-way handshake ack immediately 13092 * in order to make the connection complete as soon as 13093 * possible on the accepting host. 13094 */ 13095 flags |= TH_ACK_NEEDED; 13096 13097 /* 13098 * Special case for loopback. At this point we have 13099 * received SYN-ACK from the remote endpoint. In 13100 * order to ensure that both endpoints reach the 13101 * fused state prior to any data exchange, the final 13102 * ACK needs to be sent before we indicate T_CONN_CON 13103 * to the module upstream. 13104 */ 13105 if (tcp->tcp_loopback) { 13106 mblk_t *ack_mp; 13107 13108 ASSERT(!tcp->tcp_unfusable); 13109 ASSERT(mp1 != NULL); 13110 /* 13111 * For loopback, we always get a pure SYN-ACK 13112 * and only need to send back the final ACK 13113 * with no data (this is because the other 13114 * tcp is ours and we don't do T/TCP). This 13115 * final ACK triggers the passive side to 13116 * perform fusion in ESTABLISHED state. 13117 */ 13118 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13119 if (tcp->tcp_ack_tid != 0) { 13120 (void) TCP_TIMER_CANCEL(tcp, 13121 tcp->tcp_ack_tid); 13122 tcp->tcp_ack_tid = 0; 13123 } 13124 TCP_RECORD_TRACE(tcp, ack_mp, 13125 TCP_TRACE_SEND_PKT); 13126 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13127 BUMP_LOCAL(tcp->tcp_obsegs); 13128 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13129 13130 /* Send up T_CONN_CON */ 13131 putnext(tcp->tcp_rq, mp1); 13132 13133 freemsg(mp); 13134 return; 13135 } 13136 /* 13137 * Forget fusion; we need to handle more 13138 * complex cases below. Send the deferred 13139 * T_CONN_CON message upstream and proceed 13140 * as usual. Mark this tcp as not capable 13141 * of fusion. 13142 */ 13143 TCP_STAT(tcps, tcp_fusion_unfusable); 13144 tcp->tcp_unfusable = B_TRUE; 13145 putnext(tcp->tcp_rq, mp1); 13146 } 13147 13148 /* 13149 * Check to see if there is data to be sent. If 13150 * yes, set the transmit flag. Then check to see 13151 * if received data processing needs to be done. 13152 * If not, go straight to xmit_check. This short 13153 * cut is OK as we don't support T/TCP. 13154 */ 13155 if (tcp->tcp_unsent) 13156 flags |= TH_XMIT_NEEDED; 13157 13158 if (seg_len == 0 && !(flags & TH_URG)) { 13159 freemsg(mp); 13160 goto xmit_check; 13161 } 13162 13163 flags &= ~TH_SYN; 13164 seg_seq++; 13165 break; 13166 } 13167 tcp->tcp_state = TCPS_SYN_RCVD; 13168 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13169 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13170 if (mp1) { 13171 DB_CPID(mp1) = tcp->tcp_cpid; 13172 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 13173 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13174 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13175 } 13176 freemsg(mp); 13177 return; 13178 case TCPS_SYN_RCVD: 13179 if (flags & TH_ACK) { 13180 /* 13181 * In this state, a SYN|ACK packet is either bogus 13182 * because the other side must be ACKing our SYN which 13183 * indicates it has seen the ACK for their SYN and 13184 * shouldn't retransmit it or we're crossing SYNs 13185 * on active open. 13186 */ 13187 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13188 freemsg(mp); 13189 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13190 tcp, seg_ack, 0, TH_RST); 13191 return; 13192 } 13193 /* 13194 * NOTE: RFC 793 pg. 72 says this should be 13195 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13196 * but that would mean we have an ack that ignored 13197 * our SYN. 13198 */ 13199 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13200 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13201 freemsg(mp); 13202 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13203 tcp, seg_ack, 0, TH_RST); 13204 return; 13205 } 13206 } 13207 break; 13208 case TCPS_LISTEN: 13209 /* 13210 * Only a TLI listener can come through this path when a 13211 * acceptor is going back to be a listener and a packet 13212 * for the acceptor hits the classifier. For a socket 13213 * listener, this can never happen because a listener 13214 * can never accept connection on itself and hence a 13215 * socket acceptor can not go back to being a listener. 13216 */ 13217 ASSERT(!TCP_IS_SOCKET(tcp)); 13218 /*FALLTHRU*/ 13219 case TCPS_CLOSED: 13220 case TCPS_BOUND: { 13221 conn_t *new_connp; 13222 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13223 13224 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13225 if (new_connp != NULL) { 13226 tcp_reinput(new_connp, mp, connp->conn_sqp); 13227 return; 13228 } 13229 /* We failed to classify. For now just drop the packet */ 13230 freemsg(mp); 13231 return; 13232 } 13233 case TCPS_IDLE: 13234 /* 13235 * Handle the case where the tcp_clean_death() has happened 13236 * on a connection (application hasn't closed yet) but a packet 13237 * was already queued on squeue before tcp_clean_death() 13238 * was processed. Calling tcp_clean_death() twice on same 13239 * connection can result in weird behaviour. 13240 */ 13241 freemsg(mp); 13242 return; 13243 default: 13244 break; 13245 } 13246 13247 /* 13248 * Already on the correct queue/perimeter. 13249 * If this is a detached connection and not an eager 13250 * connection hanging off a listener then new data 13251 * (past the FIN) will cause a reset. 13252 * We do a special check here where it 13253 * is out of the main line, rather than check 13254 * if we are detached every time we see new 13255 * data down below. 13256 */ 13257 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13258 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13259 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13260 TCP_RECORD_TRACE(tcp, 13261 mp, TCP_TRACE_RECV_PKT); 13262 13263 freemsg(mp); 13264 /* 13265 * This could be an SSL closure alert. We're detached so just 13266 * acknowledge it this last time. 13267 */ 13268 if (tcp->tcp_kssl_ctx != NULL) { 13269 kssl_release_ctx(tcp->tcp_kssl_ctx); 13270 tcp->tcp_kssl_ctx = NULL; 13271 13272 tcp->tcp_rnxt += seg_len; 13273 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13274 flags |= TH_ACK_NEEDED; 13275 goto ack_check; 13276 } 13277 13278 tcp_xmit_ctl("new data when detached", tcp, 13279 tcp->tcp_snxt, 0, TH_RST); 13280 (void) tcp_clean_death(tcp, EPROTO, 12); 13281 return; 13282 } 13283 13284 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13285 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13286 new_swnd = BE16_TO_U16(tcph->th_win) << 13287 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13288 13289 if (tcp->tcp_snd_ts_ok) { 13290 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13291 /* 13292 * This segment is not acceptable. 13293 * Drop it and send back an ACK. 13294 */ 13295 freemsg(mp); 13296 flags |= TH_ACK_NEEDED; 13297 goto ack_check; 13298 } 13299 } else if (tcp->tcp_snd_sack_ok) { 13300 ASSERT(tcp->tcp_sack_info != NULL); 13301 tcpopt.tcp = tcp; 13302 /* 13303 * SACK info in already updated in tcp_parse_options. Ignore 13304 * all other TCP options... 13305 */ 13306 (void) tcp_parse_options(tcph, &tcpopt); 13307 } 13308 try_again:; 13309 mss = tcp->tcp_mss; 13310 gap = seg_seq - tcp->tcp_rnxt; 13311 rgap = tcp->tcp_rwnd - (gap + seg_len); 13312 /* 13313 * gap is the amount of sequence space between what we expect to see 13314 * and what we got for seg_seq. A positive value for gap means 13315 * something got lost. A negative value means we got some old stuff. 13316 */ 13317 if (gap < 0) { 13318 /* Old stuff present. Is the SYN in there? */ 13319 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13320 (seg_len != 0)) { 13321 flags &= ~TH_SYN; 13322 seg_seq++; 13323 urp--; 13324 /* Recompute the gaps after noting the SYN. */ 13325 goto try_again; 13326 } 13327 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13328 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13329 (seg_len > -gap ? -gap : seg_len)); 13330 /* Remove the old stuff from seg_len. */ 13331 seg_len += gap; 13332 /* 13333 * Anything left? 13334 * Make sure to check for unack'd FIN when rest of data 13335 * has been previously ack'd. 13336 */ 13337 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13338 /* 13339 * Resets are only valid if they lie within our offered 13340 * window. If the RST bit is set, we just ignore this 13341 * segment. 13342 */ 13343 if (flags & TH_RST) { 13344 freemsg(mp); 13345 return; 13346 } 13347 13348 /* 13349 * The arriving of dup data packets indicate that we 13350 * may have postponed an ack for too long, or the other 13351 * side's RTT estimate is out of shape. Start acking 13352 * more often. 13353 */ 13354 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13355 tcp->tcp_rack_cnt >= 1 && 13356 tcp->tcp_rack_abs_max > 2) { 13357 tcp->tcp_rack_abs_max--; 13358 } 13359 tcp->tcp_rack_cur_max = 1; 13360 13361 /* 13362 * This segment is "unacceptable". None of its 13363 * sequence space lies within our advertized window. 13364 * 13365 * Adjust seg_len to the original value for tracing. 13366 */ 13367 seg_len -= gap; 13368 if (tcp->tcp_debug) { 13369 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13370 "tcp_rput: unacceptable, gap %d, rgap %d, " 13371 "flags 0x%x, seg_seq %u, seg_ack %u, " 13372 "seg_len %d, rnxt %u, snxt %u, %s", 13373 gap, rgap, flags, seg_seq, seg_ack, 13374 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13375 tcp_display(tcp, NULL, 13376 DISP_ADDR_AND_PORT)); 13377 } 13378 13379 /* 13380 * Arrange to send an ACK in response to the 13381 * unacceptable segment per RFC 793 page 69. There 13382 * is only one small difference between ours and the 13383 * acceptability test in the RFC - we accept ACK-only 13384 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13385 * will be generated. 13386 * 13387 * Note that we have to ACK an ACK-only packet at least 13388 * for stacks that send 0-length keep-alives with 13389 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13390 * section 4.2.3.6. As long as we don't ever generate 13391 * an unacceptable packet in response to an incoming 13392 * packet that is unacceptable, it should not cause 13393 * "ACK wars". 13394 */ 13395 flags |= TH_ACK_NEEDED; 13396 13397 /* 13398 * Continue processing this segment in order to use the 13399 * ACK information it contains, but skip all other 13400 * sequence-number processing. Processing the ACK 13401 * information is necessary in order to 13402 * re-synchronize connections that may have lost 13403 * synchronization. 13404 * 13405 * We clear seg_len and flag fields related to 13406 * sequence number processing as they are not 13407 * to be trusted for an unacceptable segment. 13408 */ 13409 seg_len = 0; 13410 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13411 goto process_ack; 13412 } 13413 13414 /* Fix seg_seq, and chew the gap off the front. */ 13415 seg_seq = tcp->tcp_rnxt; 13416 urp += gap; 13417 do { 13418 mblk_t *mp2; 13419 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13420 (uintptr_t)UINT_MAX); 13421 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13422 if (gap > 0) { 13423 mp->b_rptr = mp->b_wptr - gap; 13424 break; 13425 } 13426 mp2 = mp; 13427 mp = mp->b_cont; 13428 freeb(mp2); 13429 } while (gap < 0); 13430 /* 13431 * If the urgent data has already been acknowledged, we 13432 * should ignore TH_URG below 13433 */ 13434 if (urp < 0) 13435 flags &= ~TH_URG; 13436 } 13437 /* 13438 * rgap is the amount of stuff received out of window. A negative 13439 * value is the amount out of window. 13440 */ 13441 if (rgap < 0) { 13442 mblk_t *mp2; 13443 13444 if (tcp->tcp_rwnd == 0) { 13445 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13446 } else { 13447 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13448 UPDATE_MIB(&tcps->tcps_mib, 13449 tcpInDataPastWinBytes, -rgap); 13450 } 13451 13452 /* 13453 * seg_len does not include the FIN, so if more than 13454 * just the FIN is out of window, we act like we don't 13455 * see it. (If just the FIN is out of window, rgap 13456 * will be zero and we will go ahead and acknowledge 13457 * the FIN.) 13458 */ 13459 flags &= ~TH_FIN; 13460 13461 /* Fix seg_len and make sure there is something left. */ 13462 seg_len += rgap; 13463 if (seg_len <= 0) { 13464 /* 13465 * Resets are only valid if they lie within our offered 13466 * window. If the RST bit is set, we just ignore this 13467 * segment. 13468 */ 13469 if (flags & TH_RST) { 13470 freemsg(mp); 13471 return; 13472 } 13473 13474 /* Per RFC 793, we need to send back an ACK. */ 13475 flags |= TH_ACK_NEEDED; 13476 13477 /* 13478 * Send SIGURG as soon as possible i.e. even 13479 * if the TH_URG was delivered in a window probe 13480 * packet (which will be unacceptable). 13481 * 13482 * We generate a signal if none has been generated 13483 * for this connection or if this is a new urgent 13484 * byte. Also send a zero-length "unmarked" message 13485 * to inform SIOCATMARK that this is not the mark. 13486 * 13487 * tcp_urp_last_valid is cleared when the T_exdata_ind 13488 * is sent up. This plus the check for old data 13489 * (gap >= 0) handles the wraparound of the sequence 13490 * number space without having to always track the 13491 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13492 * this max in its rcv_up variable). 13493 * 13494 * This prevents duplicate SIGURGS due to a "late" 13495 * zero-window probe when the T_EXDATA_IND has already 13496 * been sent up. 13497 */ 13498 if ((flags & TH_URG) && 13499 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13500 tcp->tcp_urp_last))) { 13501 mp1 = allocb(0, BPRI_MED); 13502 if (mp1 == NULL) { 13503 freemsg(mp); 13504 return; 13505 } 13506 if (!TCP_IS_DETACHED(tcp) && 13507 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13508 SIGURG)) { 13509 /* Try again on the rexmit. */ 13510 freemsg(mp1); 13511 freemsg(mp); 13512 return; 13513 } 13514 /* 13515 * If the next byte would be the mark 13516 * then mark with MARKNEXT else mark 13517 * with NOTMARKNEXT. 13518 */ 13519 if (gap == 0 && urp == 0) 13520 mp1->b_flag |= MSGMARKNEXT; 13521 else 13522 mp1->b_flag |= MSGNOTMARKNEXT; 13523 freemsg(tcp->tcp_urp_mark_mp); 13524 tcp->tcp_urp_mark_mp = mp1; 13525 flags |= TH_SEND_URP_MARK; 13526 tcp->tcp_urp_last_valid = B_TRUE; 13527 tcp->tcp_urp_last = urp + seg_seq; 13528 } 13529 /* 13530 * If this is a zero window probe, continue to 13531 * process the ACK part. But we need to set seg_len 13532 * to 0 to avoid data processing. Otherwise just 13533 * drop the segment and send back an ACK. 13534 */ 13535 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13536 flags &= ~(TH_SYN | TH_URG); 13537 seg_len = 0; 13538 goto process_ack; 13539 } else { 13540 freemsg(mp); 13541 goto ack_check; 13542 } 13543 } 13544 /* Pitch out of window stuff off the end. */ 13545 rgap = seg_len; 13546 mp2 = mp; 13547 do { 13548 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13549 (uintptr_t)INT_MAX); 13550 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13551 if (rgap < 0) { 13552 mp2->b_wptr += rgap; 13553 if ((mp1 = mp2->b_cont) != NULL) { 13554 mp2->b_cont = NULL; 13555 freemsg(mp1); 13556 } 13557 break; 13558 } 13559 } while ((mp2 = mp2->b_cont) != NULL); 13560 } 13561 ok:; 13562 /* 13563 * TCP should check ECN info for segments inside the window only. 13564 * Therefore the check should be done here. 13565 */ 13566 if (tcp->tcp_ecn_ok) { 13567 if (flags & TH_CWR) { 13568 tcp->tcp_ecn_echo_on = B_FALSE; 13569 } 13570 /* 13571 * Note that both ECN_CE and CWR can be set in the 13572 * same segment. In this case, we once again turn 13573 * on ECN_ECHO. 13574 */ 13575 if (tcp->tcp_ipversion == IPV4_VERSION) { 13576 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13577 13578 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13579 tcp->tcp_ecn_echo_on = B_TRUE; 13580 } 13581 } else { 13582 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13583 13584 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13585 htonl(IPH_ECN_CE << 20)) { 13586 tcp->tcp_ecn_echo_on = B_TRUE; 13587 } 13588 } 13589 } 13590 13591 /* 13592 * Check whether we can update tcp_ts_recent. This test is 13593 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13594 * Extensions for High Performance: An Update", Internet Draft. 13595 */ 13596 if (tcp->tcp_snd_ts_ok && 13597 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13598 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13599 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13600 tcp->tcp_last_rcv_lbolt = lbolt64; 13601 } 13602 13603 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13604 /* 13605 * FIN in an out of order segment. We record this in 13606 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13607 * Clear the FIN so that any check on FIN flag will fail. 13608 * Remember that FIN also counts in the sequence number 13609 * space. So we need to ack out of order FIN only segments. 13610 */ 13611 if (flags & TH_FIN) { 13612 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13613 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13614 flags &= ~TH_FIN; 13615 flags |= TH_ACK_NEEDED; 13616 } 13617 if (seg_len > 0) { 13618 /* Fill in the SACK blk list. */ 13619 if (tcp->tcp_snd_sack_ok) { 13620 ASSERT(tcp->tcp_sack_info != NULL); 13621 tcp_sack_insert(tcp->tcp_sack_list, 13622 seg_seq, seg_seq + seg_len, 13623 &(tcp->tcp_num_sack_blk)); 13624 } 13625 13626 /* 13627 * Attempt reassembly and see if we have something 13628 * ready to go. 13629 */ 13630 mp = tcp_reass(tcp, mp, seg_seq); 13631 /* Always ack out of order packets */ 13632 flags |= TH_ACK_NEEDED | TH_PUSH; 13633 if (mp) { 13634 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13635 (uintptr_t)INT_MAX); 13636 seg_len = mp->b_cont ? msgdsize(mp) : 13637 (int)(mp->b_wptr - mp->b_rptr); 13638 seg_seq = tcp->tcp_rnxt; 13639 /* 13640 * A gap is filled and the seq num and len 13641 * of the gap match that of a previously 13642 * received FIN, put the FIN flag back in. 13643 */ 13644 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13645 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13646 flags |= TH_FIN; 13647 tcp->tcp_valid_bits &= 13648 ~TCP_OFO_FIN_VALID; 13649 } 13650 } else { 13651 /* 13652 * Keep going even with NULL mp. 13653 * There may be a useful ACK or something else 13654 * we don't want to miss. 13655 * 13656 * But TCP should not perform fast retransmit 13657 * because of the ack number. TCP uses 13658 * seg_len == 0 to determine if it is a pure 13659 * ACK. And this is not a pure ACK. 13660 */ 13661 seg_len = 0; 13662 ofo_seg = B_TRUE; 13663 } 13664 } 13665 } else if (seg_len > 0) { 13666 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13667 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13668 /* 13669 * If an out of order FIN was received before, and the seq 13670 * num and len of the new segment match that of the FIN, 13671 * put the FIN flag back in. 13672 */ 13673 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13674 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13675 flags |= TH_FIN; 13676 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13677 } 13678 } 13679 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13680 if (flags & TH_RST) { 13681 freemsg(mp); 13682 switch (tcp->tcp_state) { 13683 case TCPS_SYN_RCVD: 13684 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13685 break; 13686 case TCPS_ESTABLISHED: 13687 case TCPS_FIN_WAIT_1: 13688 case TCPS_FIN_WAIT_2: 13689 case TCPS_CLOSE_WAIT: 13690 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13691 break; 13692 case TCPS_CLOSING: 13693 case TCPS_LAST_ACK: 13694 (void) tcp_clean_death(tcp, 0, 16); 13695 break; 13696 default: 13697 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13698 (void) tcp_clean_death(tcp, ENXIO, 17); 13699 break; 13700 } 13701 return; 13702 } 13703 if (flags & TH_SYN) { 13704 /* 13705 * See RFC 793, Page 71 13706 * 13707 * The seq number must be in the window as it should 13708 * be "fixed" above. If it is outside window, it should 13709 * be already rejected. Note that we allow seg_seq to be 13710 * rnxt + rwnd because we want to accept 0 window probe. 13711 */ 13712 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13713 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13714 freemsg(mp); 13715 /* 13716 * If the ACK flag is not set, just use our snxt as the 13717 * seq number of the RST segment. 13718 */ 13719 if (!(flags & TH_ACK)) { 13720 seg_ack = tcp->tcp_snxt; 13721 } 13722 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13723 TH_RST|TH_ACK); 13724 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13725 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13726 return; 13727 } 13728 /* 13729 * urp could be -1 when the urp field in the packet is 0 13730 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13731 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13732 */ 13733 if (flags & TH_URG && urp >= 0) { 13734 if (!tcp->tcp_urp_last_valid || 13735 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13736 /* 13737 * If we haven't generated the signal yet for this 13738 * urgent pointer value, do it now. Also, send up a 13739 * zero-length M_DATA indicating whether or not this is 13740 * the mark. The latter is not needed when a 13741 * T_EXDATA_IND is sent up. However, if there are 13742 * allocation failures this code relies on the sender 13743 * retransmitting and the socket code for determining 13744 * the mark should not block waiting for the peer to 13745 * transmit. Thus, for simplicity we always send up the 13746 * mark indication. 13747 */ 13748 mp1 = allocb(0, BPRI_MED); 13749 if (mp1 == NULL) { 13750 freemsg(mp); 13751 return; 13752 } 13753 if (!TCP_IS_DETACHED(tcp) && 13754 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13755 /* Try again on the rexmit. */ 13756 freemsg(mp1); 13757 freemsg(mp); 13758 return; 13759 } 13760 /* 13761 * Mark with NOTMARKNEXT for now. 13762 * The code below will change this to MARKNEXT 13763 * if we are at the mark. 13764 * 13765 * If there are allocation failures (e.g. in dupmsg 13766 * below) the next time tcp_rput_data sees the urgent 13767 * segment it will send up the MSG*MARKNEXT message. 13768 */ 13769 mp1->b_flag |= MSGNOTMARKNEXT; 13770 freemsg(tcp->tcp_urp_mark_mp); 13771 tcp->tcp_urp_mark_mp = mp1; 13772 flags |= TH_SEND_URP_MARK; 13773 #ifdef DEBUG 13774 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13775 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13776 "last %x, %s", 13777 seg_seq, urp, tcp->tcp_urp_last, 13778 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13779 #endif /* DEBUG */ 13780 tcp->tcp_urp_last_valid = B_TRUE; 13781 tcp->tcp_urp_last = urp + seg_seq; 13782 } else if (tcp->tcp_urp_mark_mp != NULL) { 13783 /* 13784 * An allocation failure prevented the previous 13785 * tcp_rput_data from sending up the allocated 13786 * MSG*MARKNEXT message - send it up this time 13787 * around. 13788 */ 13789 flags |= TH_SEND_URP_MARK; 13790 } 13791 13792 /* 13793 * If the urgent byte is in this segment, make sure that it is 13794 * all by itself. This makes it much easier to deal with the 13795 * possibility of an allocation failure on the T_exdata_ind. 13796 * Note that seg_len is the number of bytes in the segment, and 13797 * urp is the offset into the segment of the urgent byte. 13798 * urp < seg_len means that the urgent byte is in this segment. 13799 */ 13800 if (urp < seg_len) { 13801 if (seg_len != 1) { 13802 uint32_t tmp_rnxt; 13803 /* 13804 * Break it up and feed it back in. 13805 * Re-attach the IP header. 13806 */ 13807 mp->b_rptr = iphdr; 13808 if (urp > 0) { 13809 /* 13810 * There is stuff before the urgent 13811 * byte. 13812 */ 13813 mp1 = dupmsg(mp); 13814 if (!mp1) { 13815 /* 13816 * Trim from urgent byte on. 13817 * The rest will come back. 13818 */ 13819 (void) adjmsg(mp, 13820 urp - seg_len); 13821 tcp_rput_data(connp, 13822 mp, NULL); 13823 return; 13824 } 13825 (void) adjmsg(mp1, urp - seg_len); 13826 /* Feed this piece back in. */ 13827 tmp_rnxt = tcp->tcp_rnxt; 13828 tcp_rput_data(connp, mp1, NULL); 13829 /* 13830 * If the data passed back in was not 13831 * processed (ie: bad ACK) sending 13832 * the remainder back in will cause a 13833 * loop. In this case, drop the 13834 * packet and let the sender try 13835 * sending a good packet. 13836 */ 13837 if (tmp_rnxt == tcp->tcp_rnxt) { 13838 freemsg(mp); 13839 return; 13840 } 13841 } 13842 if (urp != seg_len - 1) { 13843 uint32_t tmp_rnxt; 13844 /* 13845 * There is stuff after the urgent 13846 * byte. 13847 */ 13848 mp1 = dupmsg(mp); 13849 if (!mp1) { 13850 /* 13851 * Trim everything beyond the 13852 * urgent byte. The rest will 13853 * come back. 13854 */ 13855 (void) adjmsg(mp, 13856 urp + 1 - seg_len); 13857 tcp_rput_data(connp, 13858 mp, NULL); 13859 return; 13860 } 13861 (void) adjmsg(mp1, urp + 1 - seg_len); 13862 tmp_rnxt = tcp->tcp_rnxt; 13863 tcp_rput_data(connp, mp1, NULL); 13864 /* 13865 * If the data passed back in was not 13866 * processed (ie: bad ACK) sending 13867 * the remainder back in will cause a 13868 * loop. In this case, drop the 13869 * packet and let the sender try 13870 * sending a good packet. 13871 */ 13872 if (tmp_rnxt == tcp->tcp_rnxt) { 13873 freemsg(mp); 13874 return; 13875 } 13876 } 13877 tcp_rput_data(connp, mp, NULL); 13878 return; 13879 } 13880 /* 13881 * This segment contains only the urgent byte. We 13882 * have to allocate the T_exdata_ind, if we can. 13883 */ 13884 if (!tcp->tcp_urp_mp) { 13885 struct T_exdata_ind *tei; 13886 mp1 = allocb(sizeof (struct T_exdata_ind), 13887 BPRI_MED); 13888 if (!mp1) { 13889 /* 13890 * Sigh... It'll be back. 13891 * Generate any MSG*MARK message now. 13892 */ 13893 freemsg(mp); 13894 seg_len = 0; 13895 if (flags & TH_SEND_URP_MARK) { 13896 13897 13898 ASSERT(tcp->tcp_urp_mark_mp); 13899 tcp->tcp_urp_mark_mp->b_flag &= 13900 ~MSGNOTMARKNEXT; 13901 tcp->tcp_urp_mark_mp->b_flag |= 13902 MSGMARKNEXT; 13903 } 13904 goto ack_check; 13905 } 13906 mp1->b_datap->db_type = M_PROTO; 13907 tei = (struct T_exdata_ind *)mp1->b_rptr; 13908 tei->PRIM_type = T_EXDATA_IND; 13909 tei->MORE_flag = 0; 13910 mp1->b_wptr = (uchar_t *)&tei[1]; 13911 tcp->tcp_urp_mp = mp1; 13912 #ifdef DEBUG 13913 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13914 "tcp_rput: allocated exdata_ind %s", 13915 tcp_display(tcp, NULL, 13916 DISP_PORT_ONLY)); 13917 #endif /* DEBUG */ 13918 /* 13919 * There is no need to send a separate MSG*MARK 13920 * message since the T_EXDATA_IND will be sent 13921 * now. 13922 */ 13923 flags &= ~TH_SEND_URP_MARK; 13924 freemsg(tcp->tcp_urp_mark_mp); 13925 tcp->tcp_urp_mark_mp = NULL; 13926 } 13927 /* 13928 * Now we are all set. On the next putnext upstream, 13929 * tcp_urp_mp will be non-NULL and will get prepended 13930 * to what has to be this piece containing the urgent 13931 * byte. If for any reason we abort this segment below, 13932 * if it comes back, we will have this ready, or it 13933 * will get blown off in close. 13934 */ 13935 } else if (urp == seg_len) { 13936 /* 13937 * The urgent byte is the next byte after this sequence 13938 * number. If there is data it is marked with 13939 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13940 * since it is not needed. Otherwise, if the code 13941 * above just allocated a zero-length tcp_urp_mark_mp 13942 * message, that message is tagged with MSGMARKNEXT. 13943 * Sending up these MSGMARKNEXT messages makes 13944 * SIOCATMARK work correctly even though 13945 * the T_EXDATA_IND will not be sent up until the 13946 * urgent byte arrives. 13947 */ 13948 if (seg_len != 0) { 13949 flags |= TH_MARKNEXT_NEEDED; 13950 freemsg(tcp->tcp_urp_mark_mp); 13951 tcp->tcp_urp_mark_mp = NULL; 13952 flags &= ~TH_SEND_URP_MARK; 13953 } else if (tcp->tcp_urp_mark_mp != NULL) { 13954 flags |= TH_SEND_URP_MARK; 13955 tcp->tcp_urp_mark_mp->b_flag &= 13956 ~MSGNOTMARKNEXT; 13957 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13958 } 13959 #ifdef DEBUG 13960 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13961 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13962 seg_len, flags, 13963 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13964 #endif /* DEBUG */ 13965 } else { 13966 /* Data left until we hit mark */ 13967 #ifdef DEBUG 13968 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13969 "tcp_rput: URP %d bytes left, %s", 13970 urp - seg_len, tcp_display(tcp, NULL, 13971 DISP_PORT_ONLY)); 13972 #endif /* DEBUG */ 13973 } 13974 } 13975 13976 process_ack: 13977 if (!(flags & TH_ACK)) { 13978 freemsg(mp); 13979 goto xmit_check; 13980 } 13981 } 13982 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13983 13984 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13985 tcp->tcp_ip_forward_progress = B_TRUE; 13986 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13987 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13988 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13989 /* 3-way handshake complete - pass up the T_CONN_IND */ 13990 tcp_t *listener = tcp->tcp_listener; 13991 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13992 13993 tcp->tcp_tconnind_started = B_TRUE; 13994 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13995 /* 13996 * We are here means eager is fine but it can 13997 * get a TH_RST at any point between now and till 13998 * accept completes and disappear. We need to 13999 * ensure that reference to eager is valid after 14000 * we get out of eager's perimeter. So we do 14001 * an extra refhold. 14002 */ 14003 CONN_INC_REF(connp); 14004 14005 /* 14006 * The listener also exists because of the refhold 14007 * done in tcp_conn_request. Its possible that it 14008 * might have closed. We will check that once we 14009 * get inside listeners context. 14010 */ 14011 CONN_INC_REF(listener->tcp_connp); 14012 if (listener->tcp_connp->conn_sqp == 14013 connp->conn_sqp) { 14014 tcp_send_conn_ind(listener->tcp_connp, mp, 14015 listener->tcp_connp->conn_sqp); 14016 CONN_DEC_REF(listener->tcp_connp); 14017 } else if (!tcp->tcp_loopback) { 14018 squeue_fill(listener->tcp_connp->conn_sqp, mp, 14019 tcp_send_conn_ind, 14020 listener->tcp_connp, SQTAG_TCP_CONN_IND); 14021 } else { 14022 squeue_enter(listener->tcp_connp->conn_sqp, mp, 14023 tcp_send_conn_ind, listener->tcp_connp, 14024 SQTAG_TCP_CONN_IND); 14025 } 14026 } 14027 14028 if (tcp->tcp_active_open) { 14029 /* 14030 * We are seeing the final ack in the three way 14031 * hand shake of a active open'ed connection 14032 * so we must send up a T_CONN_CON 14033 */ 14034 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14035 freemsg(mp); 14036 return; 14037 } 14038 /* 14039 * Don't fuse the loopback endpoints for 14040 * simultaneous active opens. 14041 */ 14042 if (tcp->tcp_loopback) { 14043 TCP_STAT(tcps, tcp_fusion_unfusable); 14044 tcp->tcp_unfusable = B_TRUE; 14045 } 14046 } 14047 14048 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14049 bytes_acked--; 14050 /* SYN was acked - making progress */ 14051 if (tcp->tcp_ipversion == IPV6_VERSION) 14052 tcp->tcp_ip_forward_progress = B_TRUE; 14053 14054 /* 14055 * If SYN was retransmitted, need to reset all 14056 * retransmission info as this segment will be 14057 * treated as a dup ACK. 14058 */ 14059 if (tcp->tcp_rexmit) { 14060 tcp->tcp_rexmit = B_FALSE; 14061 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14062 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14063 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14064 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14065 tcp->tcp_ms_we_have_waited = 0; 14066 tcp->tcp_cwnd = mss; 14067 } 14068 14069 /* 14070 * We set the send window to zero here. 14071 * This is needed if there is data to be 14072 * processed already on the queue. 14073 * Later (at swnd_update label), the 14074 * "new_swnd > tcp_swnd" condition is satisfied 14075 * the XMIT_NEEDED flag is set in the current 14076 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14077 * called if there is already data on queue in 14078 * this state. 14079 */ 14080 tcp->tcp_swnd = 0; 14081 14082 if (new_swnd > tcp->tcp_max_swnd) 14083 tcp->tcp_max_swnd = new_swnd; 14084 tcp->tcp_swl1 = seg_seq; 14085 tcp->tcp_swl2 = seg_ack; 14086 tcp->tcp_state = TCPS_ESTABLISHED; 14087 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14088 14089 /* Fuse when both sides are in ESTABLISHED state */ 14090 if (tcp->tcp_loopback && do_tcp_fusion) 14091 tcp_fuse(tcp, iphdr, tcph); 14092 14093 } 14094 /* This code follows 4.4BSD-Lite2 mostly. */ 14095 if (bytes_acked < 0) 14096 goto est; 14097 14098 /* 14099 * If TCP is ECN capable and the congestion experience bit is 14100 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14101 * done once per window (or more loosely, per RTT). 14102 */ 14103 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14104 tcp->tcp_cwr = B_FALSE; 14105 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14106 if (!tcp->tcp_cwr) { 14107 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14108 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14109 tcp->tcp_cwnd = npkt * mss; 14110 /* 14111 * If the cwnd is 0, use the timer to clock out 14112 * new segments. This is required by the ECN spec. 14113 */ 14114 if (npkt == 0) { 14115 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14116 /* 14117 * This makes sure that when the ACK comes 14118 * back, we will increase tcp_cwnd by 1 MSS. 14119 */ 14120 tcp->tcp_cwnd_cnt = 0; 14121 } 14122 tcp->tcp_cwr = B_TRUE; 14123 /* 14124 * This marks the end of the current window of in 14125 * flight data. That is why we don't use 14126 * tcp_suna + tcp_swnd. Only data in flight can 14127 * provide ECN info. 14128 */ 14129 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14130 tcp->tcp_ecn_cwr_sent = B_FALSE; 14131 } 14132 } 14133 14134 mp1 = tcp->tcp_xmit_head; 14135 if (bytes_acked == 0) { 14136 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14137 int dupack_cnt; 14138 14139 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14140 /* 14141 * Fast retransmit. When we have seen exactly three 14142 * identical ACKs while we have unacked data 14143 * outstanding we take it as a hint that our peer 14144 * dropped something. 14145 * 14146 * If TCP is retransmitting, don't do fast retransmit. 14147 */ 14148 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14149 ! tcp->tcp_rexmit) { 14150 /* Do Limited Transmit */ 14151 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14152 tcps->tcps_dupack_fast_retransmit) { 14153 /* 14154 * RFC 3042 14155 * 14156 * What we need to do is temporarily 14157 * increase tcp_cwnd so that new 14158 * data can be sent if it is allowed 14159 * by the receive window (tcp_rwnd). 14160 * tcp_wput_data() will take care of 14161 * the rest. 14162 * 14163 * If the connection is SACK capable, 14164 * only do limited xmit when there 14165 * is SACK info. 14166 * 14167 * Note how tcp_cwnd is incremented. 14168 * The first dup ACK will increase 14169 * it by 1 MSS. The second dup ACK 14170 * will increase it by 2 MSS. This 14171 * means that only 1 new segment will 14172 * be sent for each dup ACK. 14173 */ 14174 if (tcp->tcp_unsent > 0 && 14175 (!tcp->tcp_snd_sack_ok || 14176 (tcp->tcp_snd_sack_ok && 14177 tcp->tcp_notsack_list != NULL))) { 14178 tcp->tcp_cwnd += mss << 14179 (tcp->tcp_dupack_cnt - 1); 14180 flags |= TH_LIMIT_XMIT; 14181 } 14182 } else if (dupack_cnt == 14183 tcps->tcps_dupack_fast_retransmit) { 14184 14185 /* 14186 * If we have reduced tcp_ssthresh 14187 * because of ECN, do not reduce it again 14188 * unless it is already one window of data 14189 * away. After one window of data, tcp_cwr 14190 * should then be cleared. Note that 14191 * for non ECN capable connection, tcp_cwr 14192 * should always be false. 14193 * 14194 * Adjust cwnd since the duplicate 14195 * ack indicates that a packet was 14196 * dropped (due to congestion.) 14197 */ 14198 if (!tcp->tcp_cwr) { 14199 npkt = ((tcp->tcp_snxt - 14200 tcp->tcp_suna) >> 1) / mss; 14201 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14202 mss; 14203 tcp->tcp_cwnd = (npkt + 14204 tcp->tcp_dupack_cnt) * mss; 14205 } 14206 if (tcp->tcp_ecn_ok) { 14207 tcp->tcp_cwr = B_TRUE; 14208 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14209 tcp->tcp_ecn_cwr_sent = B_FALSE; 14210 } 14211 14212 /* 14213 * We do Hoe's algorithm. Refer to her 14214 * paper "Improving the Start-up Behavior 14215 * of a Congestion Control Scheme for TCP," 14216 * appeared in SIGCOMM'96. 14217 * 14218 * Save highest seq no we have sent so far. 14219 * Be careful about the invisible FIN byte. 14220 */ 14221 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14222 (tcp->tcp_unsent == 0)) { 14223 tcp->tcp_rexmit_max = tcp->tcp_fss; 14224 } else { 14225 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14226 } 14227 14228 /* 14229 * Do not allow bursty traffic during. 14230 * fast recovery. Refer to Fall and Floyd's 14231 * paper "Simulation-based Comparisons of 14232 * Tahoe, Reno and SACK TCP" (in CCR?) 14233 * This is a best current practise. 14234 */ 14235 tcp->tcp_snd_burst = TCP_CWND_SS; 14236 14237 /* 14238 * For SACK: 14239 * Calculate tcp_pipe, which is the 14240 * estimated number of bytes in 14241 * network. 14242 * 14243 * tcp_fack is the highest sack'ed seq num 14244 * TCP has received. 14245 * 14246 * tcp_pipe is explained in the above quoted 14247 * Fall and Floyd's paper. tcp_fack is 14248 * explained in Mathis and Mahdavi's 14249 * "Forward Acknowledgment: Refining TCP 14250 * Congestion Control" in SIGCOMM '96. 14251 */ 14252 if (tcp->tcp_snd_sack_ok) { 14253 ASSERT(tcp->tcp_sack_info != NULL); 14254 if (tcp->tcp_notsack_list != NULL) { 14255 tcp->tcp_pipe = tcp->tcp_snxt - 14256 tcp->tcp_fack; 14257 tcp->tcp_sack_snxt = seg_ack; 14258 flags |= TH_NEED_SACK_REXMIT; 14259 } else { 14260 /* 14261 * Always initialize tcp_pipe 14262 * even though we don't have 14263 * any SACK info. If later 14264 * we get SACK info and 14265 * tcp_pipe is not initialized, 14266 * funny things will happen. 14267 */ 14268 tcp->tcp_pipe = 14269 tcp->tcp_cwnd_ssthresh; 14270 } 14271 } else { 14272 flags |= TH_REXMIT_NEEDED; 14273 } /* tcp_snd_sack_ok */ 14274 14275 } else { 14276 /* 14277 * Here we perform congestion 14278 * avoidance, but NOT slow start. 14279 * This is known as the Fast 14280 * Recovery Algorithm. 14281 */ 14282 if (tcp->tcp_snd_sack_ok && 14283 tcp->tcp_notsack_list != NULL) { 14284 flags |= TH_NEED_SACK_REXMIT; 14285 tcp->tcp_pipe -= mss; 14286 if (tcp->tcp_pipe < 0) 14287 tcp->tcp_pipe = 0; 14288 } else { 14289 /* 14290 * We know that one more packet has 14291 * left the pipe thus we can update 14292 * cwnd. 14293 */ 14294 cwnd = tcp->tcp_cwnd + mss; 14295 if (cwnd > tcp->tcp_cwnd_max) 14296 cwnd = tcp->tcp_cwnd_max; 14297 tcp->tcp_cwnd = cwnd; 14298 if (tcp->tcp_unsent > 0) 14299 flags |= TH_XMIT_NEEDED; 14300 } 14301 } 14302 } 14303 } else if (tcp->tcp_zero_win_probe) { 14304 /* 14305 * If the window has opened, need to arrange 14306 * to send additional data. 14307 */ 14308 if (new_swnd != 0) { 14309 /* tcp_suna != tcp_snxt */ 14310 /* Packet contains a window update */ 14311 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14312 tcp->tcp_zero_win_probe = 0; 14313 tcp->tcp_timer_backoff = 0; 14314 tcp->tcp_ms_we_have_waited = 0; 14315 14316 /* 14317 * Transmit starting with tcp_suna since 14318 * the one byte probe is not ack'ed. 14319 * If TCP has sent more than one identical 14320 * probe, tcp_rexmit will be set. That means 14321 * tcp_ss_rexmit() will send out the one 14322 * byte along with new data. Otherwise, 14323 * fake the retransmission. 14324 */ 14325 flags |= TH_XMIT_NEEDED; 14326 if (!tcp->tcp_rexmit) { 14327 tcp->tcp_rexmit = B_TRUE; 14328 tcp->tcp_dupack_cnt = 0; 14329 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14330 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14331 } 14332 } 14333 } 14334 goto swnd_update; 14335 } 14336 14337 /* 14338 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14339 * If the ACK value acks something that we have not yet sent, it might 14340 * be an old duplicate segment. Send an ACK to re-synchronize the 14341 * other side. 14342 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14343 * state is handled above, so we can always just drop the segment and 14344 * send an ACK here. 14345 * 14346 * Should we send ACKs in response to ACK only segments? 14347 */ 14348 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14349 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14350 /* drop the received segment */ 14351 freemsg(mp); 14352 14353 /* 14354 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14355 * greater than 0, check if the number of such 14356 * bogus ACks is greater than that count. If yes, 14357 * don't send back any ACK. This prevents TCP from 14358 * getting into an ACK storm if somehow an attacker 14359 * successfully spoofs an acceptable segment to our 14360 * peer. 14361 */ 14362 if (tcp_drop_ack_unsent_cnt > 0 && 14363 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14364 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14365 return; 14366 } 14367 mp = tcp_ack_mp(tcp); 14368 if (mp != NULL) { 14369 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 14370 BUMP_LOCAL(tcp->tcp_obsegs); 14371 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14372 tcp_send_data(tcp, tcp->tcp_wq, mp); 14373 } 14374 return; 14375 } 14376 14377 /* 14378 * TCP gets a new ACK, update the notsack'ed list to delete those 14379 * blocks that are covered by this ACK. 14380 */ 14381 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14382 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14383 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14384 } 14385 14386 /* 14387 * If we got an ACK after fast retransmit, check to see 14388 * if it is a partial ACK. If it is not and the congestion 14389 * window was inflated to account for the other side's 14390 * cached packets, retract it. If it is, do Hoe's algorithm. 14391 */ 14392 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14393 ASSERT(tcp->tcp_rexmit == B_FALSE); 14394 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14395 tcp->tcp_dupack_cnt = 0; 14396 /* 14397 * Restore the orig tcp_cwnd_ssthresh after 14398 * fast retransmit phase. 14399 */ 14400 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14401 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14402 } 14403 tcp->tcp_rexmit_max = seg_ack; 14404 tcp->tcp_cwnd_cnt = 0; 14405 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14406 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14407 14408 /* 14409 * Remove all notsack info to avoid confusion with 14410 * the next fast retrasnmit/recovery phase. 14411 */ 14412 if (tcp->tcp_snd_sack_ok && 14413 tcp->tcp_notsack_list != NULL) { 14414 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14415 } 14416 } else { 14417 if (tcp->tcp_snd_sack_ok && 14418 tcp->tcp_notsack_list != NULL) { 14419 flags |= TH_NEED_SACK_REXMIT; 14420 tcp->tcp_pipe -= mss; 14421 if (tcp->tcp_pipe < 0) 14422 tcp->tcp_pipe = 0; 14423 } else { 14424 /* 14425 * Hoe's algorithm: 14426 * 14427 * Retransmit the unack'ed segment and 14428 * restart fast recovery. Note that we 14429 * need to scale back tcp_cwnd to the 14430 * original value when we started fast 14431 * recovery. This is to prevent overly 14432 * aggressive behaviour in sending new 14433 * segments. 14434 */ 14435 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14436 tcps->tcps_dupack_fast_retransmit * mss; 14437 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14438 flags |= TH_REXMIT_NEEDED; 14439 } 14440 } 14441 } else { 14442 tcp->tcp_dupack_cnt = 0; 14443 if (tcp->tcp_rexmit) { 14444 /* 14445 * TCP is retranmitting. If the ACK ack's all 14446 * outstanding data, update tcp_rexmit_max and 14447 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14448 * to the correct value. 14449 * 14450 * Note that SEQ_LEQ() is used. This is to avoid 14451 * unnecessary fast retransmit caused by dup ACKs 14452 * received when TCP does slow start retransmission 14453 * after a time out. During this phase, TCP may 14454 * send out segments which are already received. 14455 * This causes dup ACKs to be sent back. 14456 */ 14457 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14458 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14459 tcp->tcp_rexmit_nxt = seg_ack; 14460 } 14461 if (seg_ack != tcp->tcp_rexmit_max) { 14462 flags |= TH_XMIT_NEEDED; 14463 } 14464 } else { 14465 tcp->tcp_rexmit = B_FALSE; 14466 tcp->tcp_xmit_zc_clean = B_FALSE; 14467 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14468 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14469 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14470 } 14471 tcp->tcp_ms_we_have_waited = 0; 14472 } 14473 } 14474 14475 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14476 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14477 tcp->tcp_suna = seg_ack; 14478 if (tcp->tcp_zero_win_probe != 0) { 14479 tcp->tcp_zero_win_probe = 0; 14480 tcp->tcp_timer_backoff = 0; 14481 } 14482 14483 /* 14484 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14485 * Note that it cannot be the SYN being ack'ed. The code flow 14486 * will not reach here. 14487 */ 14488 if (mp1 == NULL) { 14489 goto fin_acked; 14490 } 14491 14492 /* 14493 * Update the congestion window. 14494 * 14495 * If TCP is not ECN capable or TCP is ECN capable but the 14496 * congestion experience bit is not set, increase the tcp_cwnd as 14497 * usual. 14498 */ 14499 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14500 cwnd = tcp->tcp_cwnd; 14501 add = mss; 14502 14503 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14504 /* 14505 * This is to prevent an increase of less than 1 MSS of 14506 * tcp_cwnd. With partial increase, tcp_wput_data() 14507 * may send out tinygrams in order to preserve mblk 14508 * boundaries. 14509 * 14510 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14511 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14512 * increased by 1 MSS for every RTTs. 14513 */ 14514 if (tcp->tcp_cwnd_cnt <= 0) { 14515 tcp->tcp_cwnd_cnt = cwnd + add; 14516 } else { 14517 tcp->tcp_cwnd_cnt -= add; 14518 add = 0; 14519 } 14520 } 14521 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14522 } 14523 14524 /* See if the latest urgent data has been acknowledged */ 14525 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14526 SEQ_GT(seg_ack, tcp->tcp_urg)) 14527 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14528 14529 /* Can we update the RTT estimates? */ 14530 if (tcp->tcp_snd_ts_ok) { 14531 /* Ignore zero timestamp echo-reply. */ 14532 if (tcpopt.tcp_opt_ts_ecr != 0) { 14533 tcp_set_rto(tcp, (int32_t)lbolt - 14534 (int32_t)tcpopt.tcp_opt_ts_ecr); 14535 } 14536 14537 /* If needed, restart the timer. */ 14538 if (tcp->tcp_set_timer == 1) { 14539 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14540 tcp->tcp_set_timer = 0; 14541 } 14542 /* 14543 * Update tcp_csuna in case the other side stops sending 14544 * us timestamps. 14545 */ 14546 tcp->tcp_csuna = tcp->tcp_snxt; 14547 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14548 /* 14549 * An ACK sequence we haven't seen before, so get the RTT 14550 * and update the RTO. But first check if the timestamp is 14551 * valid to use. 14552 */ 14553 if ((mp1->b_next != NULL) && 14554 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14555 tcp_set_rto(tcp, (int32_t)lbolt - 14556 (int32_t)(intptr_t)mp1->b_prev); 14557 else 14558 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14559 14560 /* Remeber the last sequence to be ACKed */ 14561 tcp->tcp_csuna = seg_ack; 14562 if (tcp->tcp_set_timer == 1) { 14563 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14564 tcp->tcp_set_timer = 0; 14565 } 14566 } else { 14567 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14568 } 14569 14570 /* Eat acknowledged bytes off the xmit queue. */ 14571 for (;;) { 14572 mblk_t *mp2; 14573 uchar_t *wptr; 14574 14575 wptr = mp1->b_wptr; 14576 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14577 bytes_acked -= (int)(wptr - mp1->b_rptr); 14578 if (bytes_acked < 0) { 14579 mp1->b_rptr = wptr + bytes_acked; 14580 /* 14581 * Set a new timestamp if all the bytes timed by the 14582 * old timestamp have been ack'ed. 14583 */ 14584 if (SEQ_GT(seg_ack, 14585 (uint32_t)(uintptr_t)(mp1->b_next))) { 14586 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14587 mp1->b_next = NULL; 14588 } 14589 break; 14590 } 14591 mp1->b_next = NULL; 14592 mp1->b_prev = NULL; 14593 mp2 = mp1; 14594 mp1 = mp1->b_cont; 14595 14596 /* 14597 * This notification is required for some zero-copy 14598 * clients to maintain a copy semantic. After the data 14599 * is ack'ed, client is safe to modify or reuse the buffer. 14600 */ 14601 if (tcp->tcp_snd_zcopy_aware && 14602 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14603 tcp_zcopy_notify(tcp); 14604 freeb(mp2); 14605 if (bytes_acked == 0) { 14606 if (mp1 == NULL) { 14607 /* Everything is ack'ed, clear the tail. */ 14608 tcp->tcp_xmit_tail = NULL; 14609 /* 14610 * Cancel the timer unless we are still 14611 * waiting for an ACK for the FIN packet. 14612 */ 14613 if (tcp->tcp_timer_tid != 0 && 14614 tcp->tcp_snxt == tcp->tcp_suna) { 14615 (void) TCP_TIMER_CANCEL(tcp, 14616 tcp->tcp_timer_tid); 14617 tcp->tcp_timer_tid = 0; 14618 } 14619 goto pre_swnd_update; 14620 } 14621 if (mp2 != tcp->tcp_xmit_tail) 14622 break; 14623 tcp->tcp_xmit_tail = mp1; 14624 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14625 (uintptr_t)INT_MAX); 14626 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14627 mp1->b_rptr); 14628 break; 14629 } 14630 if (mp1 == NULL) { 14631 /* 14632 * More was acked but there is nothing more 14633 * outstanding. This means that the FIN was 14634 * just acked or that we're talking to a clown. 14635 */ 14636 fin_acked: 14637 ASSERT(tcp->tcp_fin_sent); 14638 tcp->tcp_xmit_tail = NULL; 14639 if (tcp->tcp_fin_sent) { 14640 /* FIN was acked - making progress */ 14641 if (tcp->tcp_ipversion == IPV6_VERSION && 14642 !tcp->tcp_fin_acked) 14643 tcp->tcp_ip_forward_progress = B_TRUE; 14644 tcp->tcp_fin_acked = B_TRUE; 14645 if (tcp->tcp_linger_tid != 0 && 14646 TCP_TIMER_CANCEL(tcp, 14647 tcp->tcp_linger_tid) >= 0) { 14648 tcp_stop_lingering(tcp); 14649 } 14650 } else { 14651 /* 14652 * We should never get here because 14653 * we have already checked that the 14654 * number of bytes ack'ed should be 14655 * smaller than or equal to what we 14656 * have sent so far (it is the 14657 * acceptability check of the ACK). 14658 * We can only get here if the send 14659 * queue is corrupted. 14660 * 14661 * Terminate the connection and 14662 * panic the system. It is better 14663 * for us to panic instead of 14664 * continuing to avoid other disaster. 14665 */ 14666 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14667 tcp->tcp_rnxt, TH_RST|TH_ACK); 14668 panic("Memory corruption " 14669 "detected for connection %s.", 14670 tcp_display(tcp, NULL, 14671 DISP_ADDR_AND_PORT)); 14672 /*NOTREACHED*/ 14673 } 14674 goto pre_swnd_update; 14675 } 14676 ASSERT(mp2 != tcp->tcp_xmit_tail); 14677 } 14678 if (tcp->tcp_unsent) { 14679 flags |= TH_XMIT_NEEDED; 14680 } 14681 pre_swnd_update: 14682 tcp->tcp_xmit_head = mp1; 14683 swnd_update: 14684 /* 14685 * The following check is different from most other implementations. 14686 * For bi-directional transfer, when segments are dropped, the 14687 * "normal" check will not accept a window update in those 14688 * retransmitted segemnts. Failing to do that, TCP may send out 14689 * segments which are outside receiver's window. As TCP accepts 14690 * the ack in those retransmitted segments, if the window update in 14691 * the same segment is not accepted, TCP will incorrectly calculates 14692 * that it can send more segments. This can create a deadlock 14693 * with the receiver if its window becomes zero. 14694 */ 14695 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14696 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14697 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14698 /* 14699 * The criteria for update is: 14700 * 14701 * 1. the segment acknowledges some data. Or 14702 * 2. the segment is new, i.e. it has a higher seq num. Or 14703 * 3. the segment is not old and the advertised window is 14704 * larger than the previous advertised window. 14705 */ 14706 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14707 flags |= TH_XMIT_NEEDED; 14708 tcp->tcp_swnd = new_swnd; 14709 if (new_swnd > tcp->tcp_max_swnd) 14710 tcp->tcp_max_swnd = new_swnd; 14711 tcp->tcp_swl1 = seg_seq; 14712 tcp->tcp_swl2 = seg_ack; 14713 } 14714 est: 14715 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14716 14717 switch (tcp->tcp_state) { 14718 case TCPS_FIN_WAIT_1: 14719 if (tcp->tcp_fin_acked) { 14720 tcp->tcp_state = TCPS_FIN_WAIT_2; 14721 /* 14722 * We implement the non-standard BSD/SunOS 14723 * FIN_WAIT_2 flushing algorithm. 14724 * If there is no user attached to this 14725 * TCP endpoint, then this TCP struct 14726 * could hang around forever in FIN_WAIT_2 14727 * state if the peer forgets to send us 14728 * a FIN. To prevent this, we wait only 14729 * 2*MSL (a convenient time value) for 14730 * the FIN to arrive. If it doesn't show up, 14731 * we flush the TCP endpoint. This algorithm, 14732 * though a violation of RFC-793, has worked 14733 * for over 10 years in BSD systems. 14734 * Note: SunOS 4.x waits 675 seconds before 14735 * flushing the FIN_WAIT_2 connection. 14736 */ 14737 TCP_TIMER_RESTART(tcp, 14738 tcps->tcps_fin_wait_2_flush_interval); 14739 } 14740 break; 14741 case TCPS_FIN_WAIT_2: 14742 break; /* Shutdown hook? */ 14743 case TCPS_LAST_ACK: 14744 freemsg(mp); 14745 if (tcp->tcp_fin_acked) { 14746 (void) tcp_clean_death(tcp, 0, 19); 14747 return; 14748 } 14749 goto xmit_check; 14750 case TCPS_CLOSING: 14751 if (tcp->tcp_fin_acked) { 14752 tcp->tcp_state = TCPS_TIME_WAIT; 14753 /* 14754 * Unconditionally clear the exclusive binding 14755 * bit so this TIME-WAIT connection won't 14756 * interfere with new ones. 14757 */ 14758 tcp->tcp_exclbind = 0; 14759 if (!TCP_IS_DETACHED(tcp)) { 14760 TCP_TIMER_RESTART(tcp, 14761 tcps->tcps_time_wait_interval); 14762 } else { 14763 tcp_time_wait_append(tcp); 14764 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14765 } 14766 } 14767 /*FALLTHRU*/ 14768 case TCPS_CLOSE_WAIT: 14769 freemsg(mp); 14770 goto xmit_check; 14771 default: 14772 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14773 break; 14774 } 14775 } 14776 if (flags & TH_FIN) { 14777 /* Make sure we ack the fin */ 14778 flags |= TH_ACK_NEEDED; 14779 if (!tcp->tcp_fin_rcvd) { 14780 tcp->tcp_fin_rcvd = B_TRUE; 14781 tcp->tcp_rnxt++; 14782 tcph = tcp->tcp_tcph; 14783 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14784 14785 /* 14786 * Generate the ordrel_ind at the end unless we 14787 * are an eager guy. 14788 * In the eager case tcp_rsrv will do this when run 14789 * after tcp_accept is done. 14790 */ 14791 if (tcp->tcp_listener == NULL && 14792 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14793 flags |= TH_ORDREL_NEEDED; 14794 switch (tcp->tcp_state) { 14795 case TCPS_SYN_RCVD: 14796 case TCPS_ESTABLISHED: 14797 tcp->tcp_state = TCPS_CLOSE_WAIT; 14798 /* Keepalive? */ 14799 break; 14800 case TCPS_FIN_WAIT_1: 14801 if (!tcp->tcp_fin_acked) { 14802 tcp->tcp_state = TCPS_CLOSING; 14803 break; 14804 } 14805 /* FALLTHRU */ 14806 case TCPS_FIN_WAIT_2: 14807 tcp->tcp_state = TCPS_TIME_WAIT; 14808 /* 14809 * Unconditionally clear the exclusive binding 14810 * bit so this TIME-WAIT connection won't 14811 * interfere with new ones. 14812 */ 14813 tcp->tcp_exclbind = 0; 14814 if (!TCP_IS_DETACHED(tcp)) { 14815 TCP_TIMER_RESTART(tcp, 14816 tcps->tcps_time_wait_interval); 14817 } else { 14818 tcp_time_wait_append(tcp); 14819 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14820 } 14821 if (seg_len) { 14822 /* 14823 * implies data piggybacked on FIN. 14824 * break to handle data. 14825 */ 14826 break; 14827 } 14828 freemsg(mp); 14829 goto ack_check; 14830 } 14831 } 14832 } 14833 if (mp == NULL) 14834 goto xmit_check; 14835 if (seg_len == 0) { 14836 freemsg(mp); 14837 goto xmit_check; 14838 } 14839 if (mp->b_rptr == mp->b_wptr) { 14840 /* 14841 * The header has been consumed, so we remove the 14842 * zero-length mblk here. 14843 */ 14844 mp1 = mp; 14845 mp = mp->b_cont; 14846 freeb(mp1); 14847 } 14848 tcph = tcp->tcp_tcph; 14849 tcp->tcp_rack_cnt++; 14850 { 14851 uint32_t cur_max; 14852 14853 cur_max = tcp->tcp_rack_cur_max; 14854 if (tcp->tcp_rack_cnt >= cur_max) { 14855 /* 14856 * We have more unacked data than we should - send 14857 * an ACK now. 14858 */ 14859 flags |= TH_ACK_NEEDED; 14860 cur_max++; 14861 if (cur_max > tcp->tcp_rack_abs_max) 14862 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14863 else 14864 tcp->tcp_rack_cur_max = cur_max; 14865 } else if (TCP_IS_DETACHED(tcp)) { 14866 /* We don't have an ACK timer for detached TCP. */ 14867 flags |= TH_ACK_NEEDED; 14868 } else if (seg_len < mss) { 14869 /* 14870 * If we get a segment that is less than an mss, and we 14871 * already have unacknowledged data, and the amount 14872 * unacknowledged is not a multiple of mss, then we 14873 * better generate an ACK now. Otherwise, this may be 14874 * the tail piece of a transaction, and we would rather 14875 * wait for the response. 14876 */ 14877 uint32_t udif; 14878 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14879 (uintptr_t)INT_MAX); 14880 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14881 if (udif && (udif % mss)) 14882 flags |= TH_ACK_NEEDED; 14883 else 14884 flags |= TH_ACK_TIMER_NEEDED; 14885 } else { 14886 /* Start delayed ack timer */ 14887 flags |= TH_ACK_TIMER_NEEDED; 14888 } 14889 } 14890 tcp->tcp_rnxt += seg_len; 14891 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14892 14893 /* Update SACK list */ 14894 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14895 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14896 &(tcp->tcp_num_sack_blk)); 14897 } 14898 14899 if (tcp->tcp_urp_mp) { 14900 tcp->tcp_urp_mp->b_cont = mp; 14901 mp = tcp->tcp_urp_mp; 14902 tcp->tcp_urp_mp = NULL; 14903 /* Ready for a new signal. */ 14904 tcp->tcp_urp_last_valid = B_FALSE; 14905 #ifdef DEBUG 14906 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14907 "tcp_rput: sending exdata_ind %s", 14908 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14909 #endif /* DEBUG */ 14910 } 14911 14912 /* 14913 * Check for ancillary data changes compared to last segment. 14914 */ 14915 if (tcp->tcp_ipv6_recvancillary != 0) { 14916 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14917 if (mp == NULL) 14918 return; 14919 } 14920 14921 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14922 /* 14923 * Side queue inbound data until the accept happens. 14924 * tcp_accept/tcp_rput drains this when the accept happens. 14925 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14926 * T_EXDATA_IND) it is queued on b_next. 14927 * XXX Make urgent data use this. Requires: 14928 * Removing tcp_listener check for TH_URG 14929 * Making M_PCPROTO and MARK messages skip the eager case 14930 */ 14931 14932 if (tcp->tcp_kssl_pending) { 14933 tcp_kssl_input(tcp, mp); 14934 } else { 14935 tcp_rcv_enqueue(tcp, mp, seg_len); 14936 } 14937 } else { 14938 if (mp->b_datap->db_type != M_DATA || 14939 (flags & TH_MARKNEXT_NEEDED)) { 14940 if (tcp->tcp_rcv_list != NULL) { 14941 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14942 } 14943 ASSERT(tcp->tcp_rcv_list == NULL || 14944 tcp->tcp_fused_sigurg); 14945 if (flags & TH_MARKNEXT_NEEDED) { 14946 #ifdef DEBUG 14947 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14948 "tcp_rput: sending MSGMARKNEXT %s", 14949 tcp_display(tcp, NULL, 14950 DISP_PORT_ONLY)); 14951 #endif /* DEBUG */ 14952 mp->b_flag |= MSGMARKNEXT; 14953 flags &= ~TH_MARKNEXT_NEEDED; 14954 } 14955 14956 /* Does this need SSL processing first? */ 14957 if ((tcp->tcp_kssl_ctx != NULL) && 14958 (DB_TYPE(mp) == M_DATA)) { 14959 tcp_kssl_input(tcp, mp); 14960 } else { 14961 putnext(tcp->tcp_rq, mp); 14962 if (!canputnext(tcp->tcp_rq)) 14963 tcp->tcp_rwnd -= seg_len; 14964 } 14965 } else if ((flags & (TH_PUSH|TH_FIN)) || 14966 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 14967 if (tcp->tcp_rcv_list != NULL) { 14968 /* 14969 * Enqueue the new segment first and then 14970 * call tcp_rcv_drain() to send all data 14971 * up. The other way to do this is to 14972 * send all queued data up and then call 14973 * putnext() to send the new segment up. 14974 * This way can remove the else part later 14975 * on. 14976 * 14977 * We don't this to avoid one more call to 14978 * canputnext() as tcp_rcv_drain() needs to 14979 * call canputnext(). 14980 */ 14981 tcp_rcv_enqueue(tcp, mp, seg_len); 14982 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14983 } else { 14984 /* Does this need SSL processing first? */ 14985 if ((tcp->tcp_kssl_ctx != NULL) && 14986 (DB_TYPE(mp) == M_DATA)) { 14987 tcp_kssl_input(tcp, mp); 14988 } else { 14989 putnext(tcp->tcp_rq, mp); 14990 if (!canputnext(tcp->tcp_rq)) 14991 tcp->tcp_rwnd -= seg_len; 14992 } 14993 } 14994 } else { 14995 /* 14996 * Enqueue all packets when processing an mblk 14997 * from the co queue and also enqueue normal packets. 14998 */ 14999 tcp_rcv_enqueue(tcp, mp, seg_len); 15000 } 15001 /* 15002 * Make sure the timer is running if we have data waiting 15003 * for a push bit. This provides resiliency against 15004 * implementations that do not correctly generate push bits. 15005 */ 15006 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 15007 /* 15008 * The connection may be closed at this point, so don't 15009 * do anything for a detached tcp. 15010 */ 15011 if (!TCP_IS_DETACHED(tcp)) 15012 tcp->tcp_push_tid = TCP_TIMER(tcp, 15013 tcp_push_timer, 15014 MSEC_TO_TICK(tcps->tcps_push_timer_interval)); 15015 } 15016 } 15017 xmit_check: 15018 /* Is there anything left to do? */ 15019 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15020 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15021 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15022 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15023 goto done; 15024 15025 /* Any transmit work to do and a non-zero window? */ 15026 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15027 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15028 if (flags & TH_REXMIT_NEEDED) { 15029 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15030 15031 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15032 if (snd_size > mss) 15033 snd_size = mss; 15034 if (snd_size > tcp->tcp_swnd) 15035 snd_size = tcp->tcp_swnd; 15036 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15037 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15038 B_TRUE); 15039 15040 if (mp1 != NULL) { 15041 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15042 tcp->tcp_csuna = tcp->tcp_snxt; 15043 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15044 UPDATE_MIB(&tcps->tcps_mib, 15045 tcpRetransBytes, snd_size); 15046 TCP_RECORD_TRACE(tcp, mp1, 15047 TCP_TRACE_SEND_PKT); 15048 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15049 } 15050 } 15051 if (flags & TH_NEED_SACK_REXMIT) { 15052 tcp_sack_rxmit(tcp, &flags); 15053 } 15054 /* 15055 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15056 * out new segment. Note that tcp_rexmit should not be 15057 * set, otherwise TH_LIMIT_XMIT should not be set. 15058 */ 15059 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15060 if (!tcp->tcp_rexmit) { 15061 tcp_wput_data(tcp, NULL, B_FALSE); 15062 } else { 15063 tcp_ss_rexmit(tcp); 15064 } 15065 } 15066 /* 15067 * Adjust tcp_cwnd back to normal value after sending 15068 * new data segments. 15069 */ 15070 if (flags & TH_LIMIT_XMIT) { 15071 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15072 /* 15073 * This will restart the timer. Restarting the 15074 * timer is used to avoid a timeout before the 15075 * limited transmitted segment's ACK gets back. 15076 */ 15077 if (tcp->tcp_xmit_head != NULL) 15078 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15079 } 15080 15081 /* Anything more to do? */ 15082 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15083 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15084 goto done; 15085 } 15086 ack_check: 15087 if (flags & TH_SEND_URP_MARK) { 15088 ASSERT(tcp->tcp_urp_mark_mp); 15089 /* 15090 * Send up any queued data and then send the mark message 15091 */ 15092 if (tcp->tcp_rcv_list != NULL) { 15093 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15094 } 15095 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15096 15097 mp1 = tcp->tcp_urp_mark_mp; 15098 tcp->tcp_urp_mark_mp = NULL; 15099 #ifdef DEBUG 15100 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15101 "tcp_rput: sending zero-length %s %s", 15102 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15103 "MSGNOTMARKNEXT"), 15104 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15105 #endif /* DEBUG */ 15106 putnext(tcp->tcp_rq, mp1); 15107 flags &= ~TH_SEND_URP_MARK; 15108 } 15109 if (flags & TH_ACK_NEEDED) { 15110 /* 15111 * Time to send an ack for some reason. 15112 */ 15113 mp1 = tcp_ack_mp(tcp); 15114 15115 if (mp1 != NULL) { 15116 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 15117 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15118 BUMP_LOCAL(tcp->tcp_obsegs); 15119 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15120 } 15121 if (tcp->tcp_ack_tid != 0) { 15122 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15123 tcp->tcp_ack_tid = 0; 15124 } 15125 } 15126 if (flags & TH_ACK_TIMER_NEEDED) { 15127 /* 15128 * Arrange for deferred ACK or push wait timeout. 15129 * Start timer if it is not already running. 15130 */ 15131 if (tcp->tcp_ack_tid == 0) { 15132 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15133 MSEC_TO_TICK(tcp->tcp_localnet ? 15134 (clock_t)tcps->tcps_local_dack_interval : 15135 (clock_t)tcps->tcps_deferred_ack_interval)); 15136 } 15137 } 15138 if (flags & TH_ORDREL_NEEDED) { 15139 /* 15140 * Send up the ordrel_ind unless we are an eager guy. 15141 * In the eager case tcp_rsrv will do this when run 15142 * after tcp_accept is done. 15143 */ 15144 ASSERT(tcp->tcp_listener == NULL); 15145 if (tcp->tcp_rcv_list != NULL) { 15146 /* 15147 * Push any mblk(s) enqueued from co processing. 15148 */ 15149 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15150 } 15151 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15152 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 15153 tcp->tcp_ordrel_done = B_TRUE; 15154 putnext(tcp->tcp_rq, mp1); 15155 if (tcp->tcp_deferred_clean_death) { 15156 /* 15157 * tcp_clean_death was deferred 15158 * for T_ORDREL_IND - do it now 15159 */ 15160 (void) tcp_clean_death(tcp, 15161 tcp->tcp_client_errno, 20); 15162 tcp->tcp_deferred_clean_death = B_FALSE; 15163 } 15164 } else { 15165 /* 15166 * Run the orderly release in the 15167 * service routine. 15168 */ 15169 qenable(tcp->tcp_rq); 15170 /* 15171 * Caveat(XXX): The machine may be so 15172 * overloaded that tcp_rsrv() is not scheduled 15173 * until after the endpoint has transitioned 15174 * to TCPS_TIME_WAIT 15175 * and tcp_time_wait_interval expires. Then 15176 * tcp_timer() will blow away state in tcp_t 15177 * and T_ORDREL_IND will never be delivered 15178 * upstream. Unlikely but potentially 15179 * a problem. 15180 */ 15181 } 15182 } 15183 done: 15184 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15185 } 15186 15187 /* 15188 * This function does PAWS protection check. Returns B_TRUE if the 15189 * segment passes the PAWS test, else returns B_FALSE. 15190 */ 15191 boolean_t 15192 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15193 { 15194 uint8_t flags; 15195 int options; 15196 uint8_t *up; 15197 15198 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15199 /* 15200 * If timestamp option is aligned nicely, get values inline, 15201 * otherwise call general routine to parse. Only do that 15202 * if timestamp is the only option. 15203 */ 15204 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15205 TCPOPT_REAL_TS_LEN && 15206 OK_32PTR((up = ((uint8_t *)tcph) + 15207 TCP_MIN_HEADER_LENGTH)) && 15208 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15209 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15210 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15211 15212 options = TCP_OPT_TSTAMP_PRESENT; 15213 } else { 15214 if (tcp->tcp_snd_sack_ok) { 15215 tcpoptp->tcp = tcp; 15216 } else { 15217 tcpoptp->tcp = NULL; 15218 } 15219 options = tcp_parse_options(tcph, tcpoptp); 15220 } 15221 15222 if (options & TCP_OPT_TSTAMP_PRESENT) { 15223 /* 15224 * Do PAWS per RFC 1323 section 4.2. Accept RST 15225 * regardless of the timestamp, page 18 RFC 1323.bis. 15226 */ 15227 if ((flags & TH_RST) == 0 && 15228 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15229 tcp->tcp_ts_recent)) { 15230 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15231 PAWS_TIMEOUT)) { 15232 /* This segment is not acceptable. */ 15233 return (B_FALSE); 15234 } else { 15235 /* 15236 * Connection has been idle for 15237 * too long. Reset the timestamp 15238 * and assume the segment is valid. 15239 */ 15240 tcp->tcp_ts_recent = 15241 tcpoptp->tcp_opt_ts_val; 15242 } 15243 } 15244 } else { 15245 /* 15246 * If we don't get a timestamp on every packet, we 15247 * figure we can't really trust 'em, so we stop sending 15248 * and parsing them. 15249 */ 15250 tcp->tcp_snd_ts_ok = B_FALSE; 15251 15252 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15253 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15254 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15255 /* 15256 * Adjust the tcp_mss accordingly. We also need to 15257 * adjust tcp_cwnd here in accordance with the new mss. 15258 * But we avoid doing a slow start here so as to not 15259 * to lose on the transfer rate built up so far. 15260 */ 15261 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15262 if (tcp->tcp_snd_sack_ok) { 15263 ASSERT(tcp->tcp_sack_info != NULL); 15264 tcp->tcp_max_sack_blk = 4; 15265 } 15266 } 15267 return (B_TRUE); 15268 } 15269 15270 /* 15271 * Attach ancillary data to a received TCP segments for the 15272 * ancillary pieces requested by the application that are 15273 * different than they were in the previous data segment. 15274 * 15275 * Save the "current" values once memory allocation is ok so that 15276 * when memory allocation fails we can just wait for the next data segment. 15277 */ 15278 static mblk_t * 15279 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15280 { 15281 struct T_optdata_ind *todi; 15282 int optlen; 15283 uchar_t *optptr; 15284 struct T_opthdr *toh; 15285 uint_t addflag; /* Which pieces to add */ 15286 mblk_t *mp1; 15287 15288 optlen = 0; 15289 addflag = 0; 15290 /* If app asked for pktinfo and the index has changed ... */ 15291 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15292 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15293 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15294 optlen += sizeof (struct T_opthdr) + 15295 sizeof (struct in6_pktinfo); 15296 addflag |= TCP_IPV6_RECVPKTINFO; 15297 } 15298 /* If app asked for hoplimit and it has changed ... */ 15299 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15300 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15301 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15302 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15303 addflag |= TCP_IPV6_RECVHOPLIMIT; 15304 } 15305 /* If app asked for tclass and it has changed ... */ 15306 if ((ipp->ipp_fields & IPPF_TCLASS) && 15307 ipp->ipp_tclass != tcp->tcp_recvtclass && 15308 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15309 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15310 addflag |= TCP_IPV6_RECVTCLASS; 15311 } 15312 /* 15313 * If app asked for hopbyhop headers and it has changed ... 15314 * For security labels, note that (1) security labels can't change on 15315 * a connected socket at all, (2) we're connected to at most one peer, 15316 * (3) if anything changes, then it must be some other extra option. 15317 */ 15318 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15319 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15320 (ipp->ipp_fields & IPPF_HOPOPTS), 15321 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15322 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15323 tcp->tcp_label_len; 15324 addflag |= TCP_IPV6_RECVHOPOPTS; 15325 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15326 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15327 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15328 return (mp); 15329 } 15330 /* If app asked for dst headers before routing headers ... */ 15331 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15332 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15333 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15334 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15335 optlen += sizeof (struct T_opthdr) + 15336 ipp->ipp_rtdstoptslen; 15337 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15338 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15339 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15340 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15341 return (mp); 15342 } 15343 /* If app asked for routing headers and it has changed ... */ 15344 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15345 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15346 (ipp->ipp_fields & IPPF_RTHDR), 15347 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15348 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15349 addflag |= TCP_IPV6_RECVRTHDR; 15350 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15351 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15352 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15353 return (mp); 15354 } 15355 /* If app asked for dest headers and it has changed ... */ 15356 if ((tcp->tcp_ipv6_recvancillary & 15357 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15358 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15359 (ipp->ipp_fields & IPPF_DSTOPTS), 15360 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15361 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15362 addflag |= TCP_IPV6_RECVDSTOPTS; 15363 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15364 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15365 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15366 return (mp); 15367 } 15368 15369 if (optlen == 0) { 15370 /* Nothing to add */ 15371 return (mp); 15372 } 15373 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15374 if (mp1 == NULL) { 15375 /* 15376 * Defer sending ancillary data until the next TCP segment 15377 * arrives. 15378 */ 15379 return (mp); 15380 } 15381 mp1->b_cont = mp; 15382 mp = mp1; 15383 mp->b_wptr += sizeof (*todi) + optlen; 15384 mp->b_datap->db_type = M_PROTO; 15385 todi = (struct T_optdata_ind *)mp->b_rptr; 15386 todi->PRIM_type = T_OPTDATA_IND; 15387 todi->DATA_flag = 1; /* MORE data */ 15388 todi->OPT_length = optlen; 15389 todi->OPT_offset = sizeof (*todi); 15390 optptr = (uchar_t *)&todi[1]; 15391 /* 15392 * If app asked for pktinfo and the index has changed ... 15393 * Note that the local address never changes for the connection. 15394 */ 15395 if (addflag & TCP_IPV6_RECVPKTINFO) { 15396 struct in6_pktinfo *pkti; 15397 15398 toh = (struct T_opthdr *)optptr; 15399 toh->level = IPPROTO_IPV6; 15400 toh->name = IPV6_PKTINFO; 15401 toh->len = sizeof (*toh) + sizeof (*pkti); 15402 toh->status = 0; 15403 optptr += sizeof (*toh); 15404 pkti = (struct in6_pktinfo *)optptr; 15405 if (tcp->tcp_ipversion == IPV6_VERSION) 15406 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15407 else 15408 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15409 &pkti->ipi6_addr); 15410 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15411 optptr += sizeof (*pkti); 15412 ASSERT(OK_32PTR(optptr)); 15413 /* Save as "last" value */ 15414 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15415 } 15416 /* If app asked for hoplimit and it has changed ... */ 15417 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15418 toh = (struct T_opthdr *)optptr; 15419 toh->level = IPPROTO_IPV6; 15420 toh->name = IPV6_HOPLIMIT; 15421 toh->len = sizeof (*toh) + sizeof (uint_t); 15422 toh->status = 0; 15423 optptr += sizeof (*toh); 15424 *(uint_t *)optptr = ipp->ipp_hoplimit; 15425 optptr += sizeof (uint_t); 15426 ASSERT(OK_32PTR(optptr)); 15427 /* Save as "last" value */ 15428 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15429 } 15430 /* If app asked for tclass and it has changed ... */ 15431 if (addflag & TCP_IPV6_RECVTCLASS) { 15432 toh = (struct T_opthdr *)optptr; 15433 toh->level = IPPROTO_IPV6; 15434 toh->name = IPV6_TCLASS; 15435 toh->len = sizeof (*toh) + sizeof (uint_t); 15436 toh->status = 0; 15437 optptr += sizeof (*toh); 15438 *(uint_t *)optptr = ipp->ipp_tclass; 15439 optptr += sizeof (uint_t); 15440 ASSERT(OK_32PTR(optptr)); 15441 /* Save as "last" value */ 15442 tcp->tcp_recvtclass = ipp->ipp_tclass; 15443 } 15444 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15445 toh = (struct T_opthdr *)optptr; 15446 toh->level = IPPROTO_IPV6; 15447 toh->name = IPV6_HOPOPTS; 15448 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15449 tcp->tcp_label_len; 15450 toh->status = 0; 15451 optptr += sizeof (*toh); 15452 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15453 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15454 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15455 ASSERT(OK_32PTR(optptr)); 15456 /* Save as last value */ 15457 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15458 (ipp->ipp_fields & IPPF_HOPOPTS), 15459 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15460 } 15461 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15462 toh = (struct T_opthdr *)optptr; 15463 toh->level = IPPROTO_IPV6; 15464 toh->name = IPV6_RTHDRDSTOPTS; 15465 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15466 toh->status = 0; 15467 optptr += sizeof (*toh); 15468 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15469 optptr += ipp->ipp_rtdstoptslen; 15470 ASSERT(OK_32PTR(optptr)); 15471 /* Save as last value */ 15472 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15473 &tcp->tcp_rtdstoptslen, 15474 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15475 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15476 } 15477 if (addflag & TCP_IPV6_RECVRTHDR) { 15478 toh = (struct T_opthdr *)optptr; 15479 toh->level = IPPROTO_IPV6; 15480 toh->name = IPV6_RTHDR; 15481 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15482 toh->status = 0; 15483 optptr += sizeof (*toh); 15484 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15485 optptr += ipp->ipp_rthdrlen; 15486 ASSERT(OK_32PTR(optptr)); 15487 /* Save as last value */ 15488 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15489 (ipp->ipp_fields & IPPF_RTHDR), 15490 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15491 } 15492 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15493 toh = (struct T_opthdr *)optptr; 15494 toh->level = IPPROTO_IPV6; 15495 toh->name = IPV6_DSTOPTS; 15496 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15497 toh->status = 0; 15498 optptr += sizeof (*toh); 15499 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15500 optptr += ipp->ipp_dstoptslen; 15501 ASSERT(OK_32PTR(optptr)); 15502 /* Save as last value */ 15503 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15504 (ipp->ipp_fields & IPPF_DSTOPTS), 15505 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15506 } 15507 ASSERT(optptr == mp->b_wptr); 15508 return (mp); 15509 } 15510 15511 15512 /* 15513 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15514 * or a "bad" IRE detected by tcp_adapt_ire. 15515 * We can't tell if the failure was due to the laddr or the faddr 15516 * thus we clear out all addresses and ports. 15517 */ 15518 static void 15519 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15520 { 15521 queue_t *q = tcp->tcp_rq; 15522 tcph_t *tcph; 15523 struct T_error_ack *tea; 15524 conn_t *connp = tcp->tcp_connp; 15525 15526 15527 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15528 15529 if (mp->b_cont) { 15530 freemsg(mp->b_cont); 15531 mp->b_cont = NULL; 15532 } 15533 tea = (struct T_error_ack *)mp->b_rptr; 15534 switch (tea->PRIM_type) { 15535 case T_BIND_ACK: 15536 /* 15537 * Need to unbind with classifier since we were just told that 15538 * our bind succeeded. 15539 */ 15540 tcp->tcp_hard_bound = B_FALSE; 15541 tcp->tcp_hard_binding = B_FALSE; 15542 15543 ipcl_hash_remove(connp); 15544 /* Reuse the mblk if possible */ 15545 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15546 sizeof (*tea)); 15547 mp->b_rptr = mp->b_datap->db_base; 15548 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15549 tea = (struct T_error_ack *)mp->b_rptr; 15550 tea->PRIM_type = T_ERROR_ACK; 15551 tea->TLI_error = TSYSERR; 15552 tea->UNIX_error = error; 15553 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15554 tea->ERROR_prim = T_CONN_REQ; 15555 } else { 15556 tea->ERROR_prim = O_T_BIND_REQ; 15557 } 15558 break; 15559 15560 case T_ERROR_ACK: 15561 if (tcp->tcp_state >= TCPS_SYN_SENT) 15562 tea->ERROR_prim = T_CONN_REQ; 15563 break; 15564 default: 15565 panic("tcp_bind_failed: unexpected TPI type"); 15566 /*NOTREACHED*/ 15567 } 15568 15569 tcp->tcp_state = TCPS_IDLE; 15570 if (tcp->tcp_ipversion == IPV4_VERSION) 15571 tcp->tcp_ipha->ipha_src = 0; 15572 else 15573 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15574 /* 15575 * Copy of the src addr. in tcp_t is needed since 15576 * the lookup funcs. can only look at tcp_t 15577 */ 15578 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15579 15580 tcph = tcp->tcp_tcph; 15581 tcph->th_lport[0] = 0; 15582 tcph->th_lport[1] = 0; 15583 tcp_bind_hash_remove(tcp); 15584 bzero(&connp->u_port, sizeof (connp->u_port)); 15585 /* blow away saved option results if any */ 15586 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15587 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15588 15589 conn_delete_ire(tcp->tcp_connp, NULL); 15590 putnext(q, mp); 15591 } 15592 15593 /* 15594 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15595 * messages. 15596 */ 15597 void 15598 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15599 { 15600 mblk_t *mp1; 15601 uchar_t *rptr = mp->b_rptr; 15602 queue_t *q = tcp->tcp_rq; 15603 struct T_error_ack *tea; 15604 uint32_t mss; 15605 mblk_t *syn_mp; 15606 mblk_t *mdti; 15607 mblk_t *lsoi; 15608 int retval; 15609 mblk_t *ire_mp; 15610 tcp_stack_t *tcps = tcp->tcp_tcps; 15611 15612 switch (mp->b_datap->db_type) { 15613 case M_PROTO: 15614 case M_PCPROTO: 15615 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15616 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15617 break; 15618 tea = (struct T_error_ack *)rptr; 15619 switch (tea->PRIM_type) { 15620 case T_BIND_ACK: 15621 /* 15622 * Adapt Multidata information, if any. The 15623 * following tcp_mdt_update routine will free 15624 * the message. 15625 */ 15626 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15627 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15628 b_rptr)->mdt_capab, B_TRUE); 15629 freemsg(mdti); 15630 } 15631 15632 /* 15633 * Check to update LSO information with tcp, and 15634 * tcp_lso_update routine will free the message. 15635 */ 15636 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 15637 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 15638 b_rptr)->lso_capab); 15639 freemsg(lsoi); 15640 } 15641 15642 /* Get the IRE, if we had requested for it */ 15643 ire_mp = tcp_ire_mp(mp); 15644 15645 if (tcp->tcp_hard_binding) { 15646 tcp->tcp_hard_binding = B_FALSE; 15647 tcp->tcp_hard_bound = B_TRUE; 15648 CL_INET_CONNECT(tcp); 15649 } else { 15650 if (ire_mp != NULL) 15651 freeb(ire_mp); 15652 goto after_syn_sent; 15653 } 15654 15655 retval = tcp_adapt_ire(tcp, ire_mp); 15656 if (ire_mp != NULL) 15657 freeb(ire_mp); 15658 if (retval == 0) { 15659 tcp_bind_failed(tcp, mp, 15660 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15661 ENETUNREACH : EADDRNOTAVAIL)); 15662 return; 15663 } 15664 /* 15665 * Don't let an endpoint connect to itself. 15666 * Also checked in tcp_connect() but that 15667 * check can't handle the case when the 15668 * local IP address is INADDR_ANY. 15669 */ 15670 if (tcp->tcp_ipversion == IPV4_VERSION) { 15671 if ((tcp->tcp_ipha->ipha_dst == 15672 tcp->tcp_ipha->ipha_src) && 15673 (BE16_EQL(tcp->tcp_tcph->th_lport, 15674 tcp->tcp_tcph->th_fport))) { 15675 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15676 return; 15677 } 15678 } else { 15679 if (IN6_ARE_ADDR_EQUAL( 15680 &tcp->tcp_ip6h->ip6_dst, 15681 &tcp->tcp_ip6h->ip6_src) && 15682 (BE16_EQL(tcp->tcp_tcph->th_lport, 15683 tcp->tcp_tcph->th_fport))) { 15684 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15685 return; 15686 } 15687 } 15688 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15689 /* 15690 * This should not be possible! Just for 15691 * defensive coding... 15692 */ 15693 if (tcp->tcp_state != TCPS_SYN_SENT) 15694 goto after_syn_sent; 15695 15696 if (is_system_labeled() && 15697 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 15698 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 15699 return; 15700 } 15701 15702 ASSERT(q == tcp->tcp_rq); 15703 /* 15704 * tcp_adapt_ire() does not adjust 15705 * for TCP/IP header length. 15706 */ 15707 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15708 15709 /* 15710 * Just make sure our rwnd is at 15711 * least tcp_recv_hiwat_mss * MSS 15712 * large, and round up to the nearest 15713 * MSS. 15714 * 15715 * We do the round up here because 15716 * we need to get the interface 15717 * MTU first before we can do the 15718 * round up. 15719 */ 15720 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15721 tcps->tcps_recv_hiwat_minmss * mss); 15722 q->q_hiwat = tcp->tcp_rwnd; 15723 tcp_set_ws_value(tcp); 15724 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15725 tcp->tcp_tcph->th_win); 15726 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 15727 tcp->tcp_snd_ws_ok = B_TRUE; 15728 15729 /* 15730 * Set tcp_snd_ts_ok to true 15731 * so that tcp_xmit_mp will 15732 * include the timestamp 15733 * option in the SYN segment. 15734 */ 15735 if (tcps->tcps_tstamp_always || 15736 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 15737 tcp->tcp_snd_ts_ok = B_TRUE; 15738 } 15739 15740 /* 15741 * tcp_snd_sack_ok can be set in 15742 * tcp_adapt_ire() if the sack metric 15743 * is set. So check it here also. 15744 */ 15745 if (tcps->tcps_sack_permitted == 2 || 15746 tcp->tcp_snd_sack_ok) { 15747 if (tcp->tcp_sack_info == NULL) { 15748 tcp->tcp_sack_info = 15749 kmem_cache_alloc(tcp_sack_info_cache, 15750 KM_SLEEP); 15751 } 15752 tcp->tcp_snd_sack_ok = B_TRUE; 15753 } 15754 15755 /* 15756 * Should we use ECN? Note that the current 15757 * default value (SunOS 5.9) of tcp_ecn_permitted 15758 * is 1. The reason for doing this is that there 15759 * are equipments out there that will drop ECN 15760 * enabled IP packets. Setting it to 1 avoids 15761 * compatibility problems. 15762 */ 15763 if (tcps->tcps_ecn_permitted == 2) 15764 tcp->tcp_ecn_ok = B_TRUE; 15765 15766 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15767 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15768 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15769 if (syn_mp) { 15770 cred_t *cr; 15771 pid_t pid; 15772 15773 /* 15774 * Obtain the credential from the 15775 * thread calling connect(); the credential 15776 * lives on in the second mblk which 15777 * originated from T_CONN_REQ and is echoed 15778 * with the T_BIND_ACK from ip. If none 15779 * can be found, default to the creator 15780 * of the socket. 15781 */ 15782 if (mp->b_cont == NULL || 15783 (cr = DB_CRED(mp->b_cont)) == NULL) { 15784 cr = tcp->tcp_cred; 15785 pid = tcp->tcp_cpid; 15786 } else { 15787 pid = DB_CPID(mp->b_cont); 15788 } 15789 15790 TCP_RECORD_TRACE(tcp, syn_mp, 15791 TCP_TRACE_SEND_PKT); 15792 mblk_setcred(syn_mp, cr); 15793 DB_CPID(syn_mp) = pid; 15794 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15795 } 15796 after_syn_sent: 15797 /* 15798 * A trailer mblk indicates a waiting client upstream. 15799 * We complete here the processing begun in 15800 * either tcp_bind() or tcp_connect() by passing 15801 * upstream the reply message they supplied. 15802 */ 15803 mp1 = mp; 15804 mp = mp->b_cont; 15805 freeb(mp1); 15806 if (mp) 15807 break; 15808 return; 15809 case T_ERROR_ACK: 15810 if (tcp->tcp_debug) { 15811 (void) strlog(TCP_MOD_ID, 0, 1, 15812 SL_TRACE|SL_ERROR, 15813 "tcp_rput_other: case T_ERROR_ACK, " 15814 "ERROR_prim == %d", 15815 tea->ERROR_prim); 15816 } 15817 switch (tea->ERROR_prim) { 15818 case O_T_BIND_REQ: 15819 case T_BIND_REQ: 15820 tcp_bind_failed(tcp, mp, 15821 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15822 ENETUNREACH : EADDRNOTAVAIL)); 15823 return; 15824 case T_UNBIND_REQ: 15825 tcp->tcp_hard_binding = B_FALSE; 15826 tcp->tcp_hard_bound = B_FALSE; 15827 if (mp->b_cont) { 15828 freemsg(mp->b_cont); 15829 mp->b_cont = NULL; 15830 } 15831 if (tcp->tcp_unbind_pending) 15832 tcp->tcp_unbind_pending = 0; 15833 else { 15834 /* From tcp_ip_unbind() - free */ 15835 freemsg(mp); 15836 return; 15837 } 15838 break; 15839 case T_SVR4_OPTMGMT_REQ: 15840 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15841 /* T_OPTMGMT_REQ generated by TCP */ 15842 printf("T_SVR4_OPTMGMT_REQ failed " 15843 "%d/%d - dropped (cnt %d)\n", 15844 tea->TLI_error, tea->UNIX_error, 15845 tcp->tcp_drop_opt_ack_cnt); 15846 freemsg(mp); 15847 tcp->tcp_drop_opt_ack_cnt--; 15848 return; 15849 } 15850 break; 15851 } 15852 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15853 tcp->tcp_drop_opt_ack_cnt > 0) { 15854 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15855 "- dropped (cnt %d)\n", 15856 tea->TLI_error, tea->UNIX_error, 15857 tcp->tcp_drop_opt_ack_cnt); 15858 freemsg(mp); 15859 tcp->tcp_drop_opt_ack_cnt--; 15860 return; 15861 } 15862 break; 15863 case T_OPTMGMT_ACK: 15864 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15865 /* T_OPTMGMT_REQ generated by TCP */ 15866 freemsg(mp); 15867 tcp->tcp_drop_opt_ack_cnt--; 15868 return; 15869 } 15870 break; 15871 default: 15872 break; 15873 } 15874 break; 15875 case M_FLUSH: 15876 if (*rptr & FLUSHR) 15877 flushq(q, FLUSHDATA); 15878 break; 15879 default: 15880 /* M_CTL will be directly sent to tcp_icmp_error() */ 15881 ASSERT(DB_TYPE(mp) != M_CTL); 15882 break; 15883 } 15884 /* 15885 * Make sure we set this bit before sending the ACK for 15886 * bind. Otherwise accept could possibly run and free 15887 * this tcp struct. 15888 */ 15889 putnext(q, mp); 15890 } 15891 15892 /* 15893 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15894 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15895 * tcp_rsrv() try again. 15896 */ 15897 static void 15898 tcp_ordrel_kick(void *arg) 15899 { 15900 conn_t *connp = (conn_t *)arg; 15901 tcp_t *tcp = connp->conn_tcp; 15902 15903 tcp->tcp_ordrelid = 0; 15904 tcp->tcp_timeout = B_FALSE; 15905 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15906 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15907 qenable(tcp->tcp_rq); 15908 } 15909 } 15910 15911 /* ARGSUSED */ 15912 static void 15913 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15914 { 15915 conn_t *connp = (conn_t *)arg; 15916 tcp_t *tcp = connp->conn_tcp; 15917 queue_t *q = tcp->tcp_rq; 15918 uint_t thwin; 15919 tcp_stack_t *tcps = tcp->tcp_tcps; 15920 15921 freeb(mp); 15922 15923 TCP_STAT(tcps, tcp_rsrv_calls); 15924 15925 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15926 return; 15927 } 15928 15929 if (tcp->tcp_fused) { 15930 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15931 15932 ASSERT(tcp->tcp_fused); 15933 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15934 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15935 ASSERT(!TCP_IS_DETACHED(tcp)); 15936 ASSERT(tcp->tcp_connp->conn_sqp == 15937 peer_tcp->tcp_connp->conn_sqp); 15938 15939 /* 15940 * Normally we would not get backenabled in synchronous 15941 * streams mode, but in case this happens, we need to plug 15942 * synchronous streams during our drain to prevent a race 15943 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15944 */ 15945 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15946 if (tcp->tcp_rcv_list != NULL) 15947 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15948 15949 if (peer_tcp > tcp) { 15950 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15951 mutex_enter(&tcp->tcp_non_sq_lock); 15952 } else { 15953 mutex_enter(&tcp->tcp_non_sq_lock); 15954 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15955 } 15956 15957 if (peer_tcp->tcp_flow_stopped && 15958 (TCP_UNSENT_BYTES(peer_tcp) <= 15959 peer_tcp->tcp_xmit_lowater)) { 15960 tcp_clrqfull(peer_tcp); 15961 } 15962 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15963 mutex_exit(&tcp->tcp_non_sq_lock); 15964 15965 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15966 TCP_STAT(tcps, tcp_fusion_backenabled); 15967 return; 15968 } 15969 15970 if (canputnext(q)) { 15971 tcp->tcp_rwnd = q->q_hiwat; 15972 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15973 << tcp->tcp_rcv_ws; 15974 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15975 /* 15976 * Send back a window update immediately if TCP is above 15977 * ESTABLISHED state and the increase of the rcv window 15978 * that the other side knows is at least 1 MSS after flow 15979 * control is lifted. 15980 */ 15981 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15982 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15983 tcp_xmit_ctl(NULL, tcp, 15984 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15985 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15986 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 15987 } 15988 } 15989 /* Handle a failure to allocate a T_ORDREL_IND here */ 15990 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15991 ASSERT(tcp->tcp_listener == NULL); 15992 if (tcp->tcp_rcv_list != NULL) { 15993 (void) tcp_rcv_drain(q, tcp); 15994 } 15995 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15996 mp = mi_tpi_ordrel_ind(); 15997 if (mp) { 15998 tcp->tcp_ordrel_done = B_TRUE; 15999 putnext(q, mp); 16000 if (tcp->tcp_deferred_clean_death) { 16001 /* 16002 * tcp_clean_death was deferred for 16003 * T_ORDREL_IND - do it now 16004 */ 16005 tcp->tcp_deferred_clean_death = B_FALSE; 16006 (void) tcp_clean_death(tcp, 16007 tcp->tcp_client_errno, 22); 16008 } 16009 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16010 /* 16011 * If there isn't already a timer running 16012 * start one. Use a 4 second 16013 * timer as a fallback since it can't fail. 16014 */ 16015 tcp->tcp_timeout = B_TRUE; 16016 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16017 MSEC_TO_TICK(4000)); 16018 } 16019 } 16020 } 16021 16022 /* 16023 * The read side service routine is called mostly when we get back-enabled as a 16024 * result of flow control relief. Since we don't actually queue anything in 16025 * TCP, we have no data to send out of here. What we do is clear the receive 16026 * window, and send out a window update. 16027 * This routine is also called to drive an orderly release message upstream 16028 * if the attempt in tcp_rput failed. 16029 */ 16030 static void 16031 tcp_rsrv(queue_t *q) 16032 { 16033 conn_t *connp = Q_TO_CONN(q); 16034 tcp_t *tcp = connp->conn_tcp; 16035 mblk_t *mp; 16036 tcp_stack_t *tcps = tcp->tcp_tcps; 16037 16038 /* No code does a putq on the read side */ 16039 ASSERT(q->q_first == NULL); 16040 16041 /* Nothing to do for the default queue */ 16042 if (q == tcps->tcps_g_q) { 16043 return; 16044 } 16045 16046 mp = allocb(0, BPRI_HI); 16047 if (mp == NULL) { 16048 /* 16049 * We are under memory pressure. Return for now and we 16050 * we will be called again later. 16051 */ 16052 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16053 /* 16054 * If there isn't already a timer running 16055 * start one. Use a 4 second 16056 * timer as a fallback since it can't fail. 16057 */ 16058 tcp->tcp_timeout = B_TRUE; 16059 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16060 MSEC_TO_TICK(4000)); 16061 } 16062 return; 16063 } 16064 CONN_INC_REF(connp); 16065 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16066 SQTAG_TCP_RSRV); 16067 } 16068 16069 /* 16070 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16071 * We do not allow the receive window to shrink. After setting rwnd, 16072 * set the flow control hiwat of the stream. 16073 * 16074 * This function is called in 2 cases: 16075 * 16076 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16077 * connection (passive open) and in tcp_rput_data() for active connect. 16078 * This is called after tcp_mss_set() when the desired MSS value is known. 16079 * This makes sure that our window size is a mutiple of the other side's 16080 * MSS. 16081 * 2) Handling SO_RCVBUF option. 16082 * 16083 * It is ASSUMED that the requested size is a multiple of the current MSS. 16084 * 16085 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16086 * user requests so. 16087 */ 16088 static int 16089 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16090 { 16091 uint32_t mss = tcp->tcp_mss; 16092 uint32_t old_max_rwnd; 16093 uint32_t max_transmittable_rwnd; 16094 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16095 tcp_stack_t *tcps = tcp->tcp_tcps; 16096 16097 if (tcp->tcp_fused) { 16098 size_t sth_hiwat; 16099 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16100 16101 ASSERT(peer_tcp != NULL); 16102 /* 16103 * Record the stream head's high water mark for 16104 * this endpoint; this is used for flow-control 16105 * purposes in tcp_fuse_output(). 16106 */ 16107 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16108 if (!tcp_detached) 16109 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16110 16111 /* 16112 * In the fusion case, the maxpsz stream head value of 16113 * our peer is set according to its send buffer size 16114 * and our receive buffer size; since the latter may 16115 * have changed we need to update the peer's maxpsz. 16116 */ 16117 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16118 return (rwnd); 16119 } 16120 16121 if (tcp_detached) 16122 old_max_rwnd = tcp->tcp_rwnd; 16123 else 16124 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16125 16126 /* 16127 * Insist on a receive window that is at least 16128 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16129 * funny TCP interactions of Nagle algorithm, SWS avoidance 16130 * and delayed acknowledgement. 16131 */ 16132 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16133 16134 /* 16135 * If window size info has already been exchanged, TCP should not 16136 * shrink the window. Shrinking window is doable if done carefully. 16137 * We may add that support later. But so far there is not a real 16138 * need to do that. 16139 */ 16140 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16141 /* MSS may have changed, do a round up again. */ 16142 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16143 } 16144 16145 /* 16146 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16147 * can be applied even before the window scale option is decided. 16148 */ 16149 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16150 if (rwnd > max_transmittable_rwnd) { 16151 rwnd = max_transmittable_rwnd - 16152 (max_transmittable_rwnd % mss); 16153 if (rwnd < mss) 16154 rwnd = max_transmittable_rwnd; 16155 /* 16156 * If we're over the limit we may have to back down tcp_rwnd. 16157 * The increment below won't work for us. So we set all three 16158 * here and the increment below will have no effect. 16159 */ 16160 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16161 } 16162 if (tcp->tcp_localnet) { 16163 tcp->tcp_rack_abs_max = 16164 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16165 } else { 16166 /* 16167 * For a remote host on a different subnet (through a router), 16168 * we ack every other packet to be conforming to RFC1122. 16169 * tcp_deferred_acks_max is default to 2. 16170 */ 16171 tcp->tcp_rack_abs_max = 16172 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16173 } 16174 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16175 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16176 else 16177 tcp->tcp_rack_cur_max = 0; 16178 /* 16179 * Increment the current rwnd by the amount the maximum grew (we 16180 * can not overwrite it since we might be in the middle of a 16181 * connection.) 16182 */ 16183 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16184 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16185 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16186 tcp->tcp_cwnd_max = rwnd; 16187 16188 if (tcp_detached) 16189 return (rwnd); 16190 /* 16191 * We set the maximum receive window into rq->q_hiwat. 16192 * This is not actually used for flow control. 16193 */ 16194 tcp->tcp_rq->q_hiwat = rwnd; 16195 /* 16196 * Set the Stream head high water mark. This doesn't have to be 16197 * here, since we are simply using default values, but we would 16198 * prefer to choose these values algorithmically, with a likely 16199 * relationship to rwnd. 16200 */ 16201 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16202 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16203 return (rwnd); 16204 } 16205 16206 /* 16207 * Return SNMP stuff in buffer in mpdata. 16208 */ 16209 int 16210 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16211 { 16212 mblk_t *mpdata; 16213 mblk_t *mp_conn_ctl = NULL; 16214 mblk_t *mp_conn_tail; 16215 mblk_t *mp_attr_ctl = NULL; 16216 mblk_t *mp_attr_tail; 16217 mblk_t *mp6_conn_ctl = NULL; 16218 mblk_t *mp6_conn_tail; 16219 mblk_t *mp6_attr_ctl = NULL; 16220 mblk_t *mp6_attr_tail; 16221 struct opthdr *optp; 16222 mib2_tcpConnEntry_t tce; 16223 mib2_tcp6ConnEntry_t tce6; 16224 mib2_transportMLPEntry_t mlp; 16225 connf_t *connfp; 16226 conn_t *connp; 16227 int i; 16228 boolean_t ispriv; 16229 zoneid_t zoneid; 16230 int v4_conn_idx; 16231 int v6_conn_idx; 16232 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16233 ip_stack_t *ipst; 16234 16235 if (mpctl == NULL || 16236 (mpdata = mpctl->b_cont) == NULL || 16237 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16238 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16239 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16240 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16241 freemsg(mp_conn_ctl); 16242 freemsg(mp_attr_ctl); 16243 freemsg(mp6_conn_ctl); 16244 freemsg(mp6_attr_ctl); 16245 return (0); 16246 } 16247 16248 /* build table of connections -- need count in fixed part */ 16249 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16250 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16251 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16252 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16253 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16254 16255 ispriv = 16256 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16257 zoneid = Q_TO_CONN(q)->conn_zoneid; 16258 16259 v4_conn_idx = v6_conn_idx = 0; 16260 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16261 16262 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16263 ipst = tcps->tcps_netstack->netstack_ip; 16264 16265 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16266 16267 connp = NULL; 16268 16269 while ((connp = 16270 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16271 tcp_t *tcp; 16272 boolean_t needattr; 16273 16274 if (connp->conn_zoneid != zoneid) 16275 continue; /* not in this zone */ 16276 16277 tcp = connp->conn_tcp; 16278 UPDATE_MIB(&tcps->tcps_mib, 16279 tcpHCInSegs, tcp->tcp_ibsegs); 16280 tcp->tcp_ibsegs = 0; 16281 UPDATE_MIB(&tcps->tcps_mib, 16282 tcpHCOutSegs, tcp->tcp_obsegs); 16283 tcp->tcp_obsegs = 0; 16284 16285 tce6.tcp6ConnState = tce.tcpConnState = 16286 tcp_snmp_state(tcp); 16287 if (tce.tcpConnState == MIB2_TCP_established || 16288 tce.tcpConnState == MIB2_TCP_closeWait) 16289 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16290 16291 needattr = B_FALSE; 16292 bzero(&mlp, sizeof (mlp)); 16293 if (connp->conn_mlp_type != mlptSingle) { 16294 if (connp->conn_mlp_type == mlptShared || 16295 connp->conn_mlp_type == mlptBoth) 16296 mlp.tme_flags |= MIB2_TMEF_SHARED; 16297 if (connp->conn_mlp_type == mlptPrivate || 16298 connp->conn_mlp_type == mlptBoth) 16299 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16300 needattr = B_TRUE; 16301 } 16302 if (connp->conn_peercred != NULL) { 16303 ts_label_t *tsl; 16304 16305 tsl = crgetlabel(connp->conn_peercred); 16306 mlp.tme_doi = label2doi(tsl); 16307 mlp.tme_label = *label2bslabel(tsl); 16308 needattr = B_TRUE; 16309 } 16310 16311 /* Create a message to report on IPv6 entries */ 16312 if (tcp->tcp_ipversion == IPV6_VERSION) { 16313 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16314 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16315 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16316 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16317 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16318 /* Don't want just anybody seeing these... */ 16319 if (ispriv) { 16320 tce6.tcp6ConnEntryInfo.ce_snxt = 16321 tcp->tcp_snxt; 16322 tce6.tcp6ConnEntryInfo.ce_suna = 16323 tcp->tcp_suna; 16324 tce6.tcp6ConnEntryInfo.ce_rnxt = 16325 tcp->tcp_rnxt; 16326 tce6.tcp6ConnEntryInfo.ce_rack = 16327 tcp->tcp_rack; 16328 } else { 16329 /* 16330 * Netstat, unfortunately, uses this to 16331 * get send/receive queue sizes. How to fix? 16332 * Why not compute the difference only? 16333 */ 16334 tce6.tcp6ConnEntryInfo.ce_snxt = 16335 tcp->tcp_snxt - tcp->tcp_suna; 16336 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16337 tce6.tcp6ConnEntryInfo.ce_rnxt = 16338 tcp->tcp_rnxt - tcp->tcp_rack; 16339 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16340 } 16341 16342 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16343 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16344 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16345 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16346 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16347 16348 tce6.tcp6ConnCreationProcess = 16349 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16350 tcp->tcp_cpid; 16351 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16352 16353 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16354 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16355 16356 mlp.tme_connidx = v6_conn_idx++; 16357 if (needattr) 16358 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16359 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16360 } 16361 /* 16362 * Create an IPv4 table entry for IPv4 entries and also 16363 * for IPv6 entries which are bound to in6addr_any 16364 * but don't have IPV6_V6ONLY set. 16365 * (i.e. anything an IPv4 peer could connect to) 16366 */ 16367 if (tcp->tcp_ipversion == IPV4_VERSION || 16368 (tcp->tcp_state <= TCPS_LISTEN && 16369 !tcp->tcp_connp->conn_ipv6_v6only && 16370 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16371 if (tcp->tcp_ipversion == IPV6_VERSION) { 16372 tce.tcpConnRemAddress = INADDR_ANY; 16373 tce.tcpConnLocalAddress = INADDR_ANY; 16374 } else { 16375 tce.tcpConnRemAddress = 16376 tcp->tcp_remote; 16377 tce.tcpConnLocalAddress = 16378 tcp->tcp_ip_src; 16379 } 16380 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16381 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16382 /* Don't want just anybody seeing these... */ 16383 if (ispriv) { 16384 tce.tcpConnEntryInfo.ce_snxt = 16385 tcp->tcp_snxt; 16386 tce.tcpConnEntryInfo.ce_suna = 16387 tcp->tcp_suna; 16388 tce.tcpConnEntryInfo.ce_rnxt = 16389 tcp->tcp_rnxt; 16390 tce.tcpConnEntryInfo.ce_rack = 16391 tcp->tcp_rack; 16392 } else { 16393 /* 16394 * Netstat, unfortunately, uses this to 16395 * get send/receive queue sizes. How 16396 * to fix? 16397 * Why not compute the difference only? 16398 */ 16399 tce.tcpConnEntryInfo.ce_snxt = 16400 tcp->tcp_snxt - tcp->tcp_suna; 16401 tce.tcpConnEntryInfo.ce_suna = 0; 16402 tce.tcpConnEntryInfo.ce_rnxt = 16403 tcp->tcp_rnxt - tcp->tcp_rack; 16404 tce.tcpConnEntryInfo.ce_rack = 0; 16405 } 16406 16407 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16408 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16409 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16410 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16411 tce.tcpConnEntryInfo.ce_state = 16412 tcp->tcp_state; 16413 16414 tce.tcpConnCreationProcess = 16415 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16416 tcp->tcp_cpid; 16417 tce.tcpConnCreationTime = tcp->tcp_open_time; 16418 16419 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16420 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16421 16422 mlp.tme_connidx = v4_conn_idx++; 16423 if (needattr) 16424 (void) snmp_append_data2( 16425 mp_attr_ctl->b_cont, 16426 &mp_attr_tail, (char *)&mlp, 16427 sizeof (mlp)); 16428 } 16429 } 16430 } 16431 16432 /* fixed length structure for IPv4 and IPv6 counters */ 16433 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16434 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16435 sizeof (mib2_tcp6ConnEntry_t)); 16436 /* synchronize 32- and 64-bit counters */ 16437 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16438 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16439 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16440 optp->level = MIB2_TCP; 16441 optp->name = 0; 16442 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16443 sizeof (tcps->tcps_mib)); 16444 optp->len = msgdsize(mpdata); 16445 qreply(q, mpctl); 16446 16447 /* table of connections... */ 16448 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16449 sizeof (struct T_optmgmt_ack)]; 16450 optp->level = MIB2_TCP; 16451 optp->name = MIB2_TCP_CONN; 16452 optp->len = msgdsize(mp_conn_ctl->b_cont); 16453 qreply(q, mp_conn_ctl); 16454 16455 /* table of MLP attributes... */ 16456 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16457 sizeof (struct T_optmgmt_ack)]; 16458 optp->level = MIB2_TCP; 16459 optp->name = EXPER_XPORT_MLP; 16460 optp->len = msgdsize(mp_attr_ctl->b_cont); 16461 if (optp->len == 0) 16462 freemsg(mp_attr_ctl); 16463 else 16464 qreply(q, mp_attr_ctl); 16465 16466 /* table of IPv6 connections... */ 16467 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16468 sizeof (struct T_optmgmt_ack)]; 16469 optp->level = MIB2_TCP6; 16470 optp->name = MIB2_TCP6_CONN; 16471 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16472 qreply(q, mp6_conn_ctl); 16473 16474 /* table of IPv6 MLP attributes... */ 16475 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16476 sizeof (struct T_optmgmt_ack)]; 16477 optp->level = MIB2_TCP6; 16478 optp->name = EXPER_XPORT_MLP; 16479 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16480 if (optp->len == 0) 16481 freemsg(mp6_attr_ctl); 16482 else 16483 qreply(q, mp6_attr_ctl); 16484 return (1); 16485 } 16486 16487 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16488 /* ARGSUSED */ 16489 int 16490 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16491 { 16492 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16493 16494 switch (level) { 16495 case MIB2_TCP: 16496 switch (name) { 16497 case 13: 16498 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16499 return (0); 16500 /* TODO: delete entry defined by tce */ 16501 return (1); 16502 default: 16503 return (0); 16504 } 16505 default: 16506 return (1); 16507 } 16508 } 16509 16510 /* Translate TCP state to MIB2 TCP state. */ 16511 static int 16512 tcp_snmp_state(tcp_t *tcp) 16513 { 16514 if (tcp == NULL) 16515 return (0); 16516 16517 switch (tcp->tcp_state) { 16518 case TCPS_CLOSED: 16519 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16520 case TCPS_BOUND: 16521 return (MIB2_TCP_closed); 16522 case TCPS_LISTEN: 16523 return (MIB2_TCP_listen); 16524 case TCPS_SYN_SENT: 16525 return (MIB2_TCP_synSent); 16526 case TCPS_SYN_RCVD: 16527 return (MIB2_TCP_synReceived); 16528 case TCPS_ESTABLISHED: 16529 return (MIB2_TCP_established); 16530 case TCPS_CLOSE_WAIT: 16531 return (MIB2_TCP_closeWait); 16532 case TCPS_FIN_WAIT_1: 16533 return (MIB2_TCP_finWait1); 16534 case TCPS_CLOSING: 16535 return (MIB2_TCP_closing); 16536 case TCPS_LAST_ACK: 16537 return (MIB2_TCP_lastAck); 16538 case TCPS_FIN_WAIT_2: 16539 return (MIB2_TCP_finWait2); 16540 case TCPS_TIME_WAIT: 16541 return (MIB2_TCP_timeWait); 16542 default: 16543 return (0); 16544 } 16545 } 16546 16547 static char tcp_report_header[] = 16548 "TCP " MI_COL_HDRPAD_STR 16549 "zone dest snxt suna " 16550 "swnd rnxt rack rwnd rto mss w sw rw t " 16551 "recent [lport,fport] state"; 16552 16553 /* 16554 * TCP status report triggered via the Named Dispatch mechanism. 16555 */ 16556 /* ARGSUSED */ 16557 static void 16558 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16559 cred_t *cr) 16560 { 16561 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16562 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16563 char cflag; 16564 in6_addr_t v6dst; 16565 char buf[80]; 16566 uint_t print_len, buf_len; 16567 16568 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16569 if (buf_len <= 0) 16570 return; 16571 16572 if (hashval >= 0) 16573 (void) sprintf(hash, "%03d ", hashval); 16574 else 16575 hash[0] = '\0'; 16576 16577 /* 16578 * Note that we use the remote address in the tcp_b structure. 16579 * This means that it will print out the real destination address, 16580 * not the next hop's address if source routing is used. This 16581 * avoid the confusion on the output because user may not 16582 * know that source routing is used for a connection. 16583 */ 16584 if (tcp->tcp_ipversion == IPV4_VERSION) { 16585 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16586 } else { 16587 v6dst = tcp->tcp_remote_v6; 16588 } 16589 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16590 /* 16591 * the ispriv checks are so that normal users cannot determine 16592 * sequence number information using NDD. 16593 */ 16594 16595 if (TCP_IS_DETACHED(tcp)) 16596 cflag = '*'; 16597 else 16598 cflag = ' '; 16599 print_len = snprintf((char *)mp->b_wptr, buf_len, 16600 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16601 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16602 hash, 16603 (void *)tcp, 16604 tcp->tcp_connp->conn_zoneid, 16605 addrbuf, 16606 (ispriv) ? tcp->tcp_snxt : 0, 16607 (ispriv) ? tcp->tcp_suna : 0, 16608 tcp->tcp_swnd, 16609 (ispriv) ? tcp->tcp_rnxt : 0, 16610 (ispriv) ? tcp->tcp_rack : 0, 16611 tcp->tcp_rwnd, 16612 tcp->tcp_rto, 16613 tcp->tcp_mss, 16614 tcp->tcp_snd_ws_ok, 16615 tcp->tcp_snd_ws, 16616 tcp->tcp_rcv_ws, 16617 tcp->tcp_snd_ts_ok, 16618 tcp->tcp_ts_recent, 16619 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16620 if (print_len < buf_len) { 16621 ((mblk_t *)mp)->b_wptr += print_len; 16622 } else { 16623 ((mblk_t *)mp)->b_wptr += buf_len; 16624 } 16625 } 16626 16627 /* 16628 * TCP status report (for listeners only) triggered via the Named Dispatch 16629 * mechanism. 16630 */ 16631 /* ARGSUSED */ 16632 static void 16633 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16634 { 16635 char addrbuf[INET6_ADDRSTRLEN]; 16636 in6_addr_t v6dst; 16637 uint_t print_len, buf_len; 16638 16639 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16640 if (buf_len <= 0) 16641 return; 16642 16643 if (tcp->tcp_ipversion == IPV4_VERSION) { 16644 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16645 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16646 } else { 16647 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16648 addrbuf, sizeof (addrbuf)); 16649 } 16650 print_len = snprintf((char *)mp->b_wptr, buf_len, 16651 "%03d " 16652 MI_COL_PTRFMT_STR 16653 "%d %s %05u %08u %d/%d/%d%c\n", 16654 hashval, (void *)tcp, 16655 tcp->tcp_connp->conn_zoneid, 16656 addrbuf, 16657 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16658 tcp->tcp_conn_req_seqnum, 16659 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16660 tcp->tcp_conn_req_max, 16661 tcp->tcp_syn_defense ? '*' : ' '); 16662 if (print_len < buf_len) { 16663 ((mblk_t *)mp)->b_wptr += print_len; 16664 } else { 16665 ((mblk_t *)mp)->b_wptr += buf_len; 16666 } 16667 } 16668 16669 /* TCP status report triggered via the Named Dispatch mechanism. */ 16670 /* ARGSUSED */ 16671 static int 16672 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16673 { 16674 tcp_t *tcp; 16675 int i; 16676 conn_t *connp; 16677 connf_t *connfp; 16678 zoneid_t zoneid; 16679 tcp_stack_t *tcps; 16680 ip_stack_t *ipst; 16681 16682 zoneid = Q_TO_CONN(q)->conn_zoneid; 16683 tcps = Q_TO_TCP(q)->tcp_tcps; 16684 16685 /* 16686 * Because of the ndd constraint, at most we can have 64K buffer 16687 * to put in all TCP info. So to be more efficient, just 16688 * allocate a 64K buffer here, assuming we need that large buffer. 16689 * This may be a problem as any user can read tcp_status. Therefore 16690 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16691 * This should be OK as normal users should not do this too often. 16692 */ 16693 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16694 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16695 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16696 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16697 return (0); 16698 } 16699 } 16700 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16701 /* The following may work even if we cannot get a large buf. */ 16702 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16703 return (0); 16704 } 16705 16706 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16707 16708 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16709 16710 ipst = tcps->tcps_netstack->netstack_ip; 16711 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16712 16713 connp = NULL; 16714 16715 while ((connp = 16716 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16717 tcp = connp->conn_tcp; 16718 if (zoneid != GLOBAL_ZONEID && 16719 zoneid != connp->conn_zoneid) 16720 continue; 16721 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16722 cr); 16723 } 16724 16725 } 16726 16727 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16728 return (0); 16729 } 16730 16731 /* TCP status report triggered via the Named Dispatch mechanism. */ 16732 /* ARGSUSED */ 16733 static int 16734 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16735 { 16736 tf_t *tbf; 16737 tcp_t *tcp; 16738 int i; 16739 zoneid_t zoneid; 16740 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16741 16742 zoneid = Q_TO_CONN(q)->conn_zoneid; 16743 16744 /* Refer to comments in tcp_status_report(). */ 16745 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16746 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16747 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16748 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16749 return (0); 16750 } 16751 } 16752 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16753 /* The following may work even if we cannot get a large buf. */ 16754 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16755 return (0); 16756 } 16757 16758 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16759 16760 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16761 tbf = &tcps->tcps_bind_fanout[i]; 16762 mutex_enter(&tbf->tf_lock); 16763 for (tcp = tbf->tf_tcp; tcp != NULL; 16764 tcp = tcp->tcp_bind_hash) { 16765 if (zoneid != GLOBAL_ZONEID && 16766 zoneid != tcp->tcp_connp->conn_zoneid) 16767 continue; 16768 CONN_INC_REF(tcp->tcp_connp); 16769 tcp_report_item(mp->b_cont, tcp, i, 16770 Q_TO_TCP(q), cr); 16771 CONN_DEC_REF(tcp->tcp_connp); 16772 } 16773 mutex_exit(&tbf->tf_lock); 16774 } 16775 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16776 return (0); 16777 } 16778 16779 /* TCP status report triggered via the Named Dispatch mechanism. */ 16780 /* ARGSUSED */ 16781 static int 16782 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16783 { 16784 connf_t *connfp; 16785 conn_t *connp; 16786 tcp_t *tcp; 16787 int i; 16788 zoneid_t zoneid; 16789 tcp_stack_t *tcps; 16790 ip_stack_t *ipst; 16791 16792 zoneid = Q_TO_CONN(q)->conn_zoneid; 16793 tcps = Q_TO_TCP(q)->tcp_tcps; 16794 16795 /* Refer to comments in tcp_status_report(). */ 16796 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16797 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16798 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16799 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16800 return (0); 16801 } 16802 } 16803 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16804 /* The following may work even if we cannot get a large buf. */ 16805 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16806 return (0); 16807 } 16808 16809 (void) mi_mpprintf(mp, 16810 " TCP " MI_COL_HDRPAD_STR 16811 "zone IP addr port seqnum backlog (q0/q/max)"); 16812 16813 ipst = tcps->tcps_netstack->netstack_ip; 16814 16815 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16816 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16817 connp = NULL; 16818 while ((connp = 16819 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16820 tcp = connp->conn_tcp; 16821 if (zoneid != GLOBAL_ZONEID && 16822 zoneid != connp->conn_zoneid) 16823 continue; 16824 tcp_report_listener(mp->b_cont, tcp, i); 16825 } 16826 } 16827 16828 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16829 return (0); 16830 } 16831 16832 /* TCP status report triggered via the Named Dispatch mechanism. */ 16833 /* ARGSUSED */ 16834 static int 16835 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16836 { 16837 connf_t *connfp; 16838 conn_t *connp; 16839 tcp_t *tcp; 16840 int i; 16841 zoneid_t zoneid; 16842 tcp_stack_t *tcps; 16843 ip_stack_t *ipst; 16844 16845 zoneid = Q_TO_CONN(q)->conn_zoneid; 16846 tcps = Q_TO_TCP(q)->tcp_tcps; 16847 ipst = tcps->tcps_netstack->netstack_ip; 16848 16849 /* Refer to comments in tcp_status_report(). */ 16850 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16851 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16852 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16853 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16854 return (0); 16855 } 16856 } 16857 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16858 /* The following may work even if we cannot get a large buf. */ 16859 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16860 return (0); 16861 } 16862 16863 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16864 ipst->ips_ipcl_conn_fanout_size); 16865 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16866 16867 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16868 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16869 connp = NULL; 16870 while ((connp = 16871 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16872 tcp = connp->conn_tcp; 16873 if (zoneid != GLOBAL_ZONEID && 16874 zoneid != connp->conn_zoneid) 16875 continue; 16876 tcp_report_item(mp->b_cont, tcp, i, 16877 Q_TO_TCP(q), cr); 16878 } 16879 } 16880 16881 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16882 return (0); 16883 } 16884 16885 /* TCP status report triggered via the Named Dispatch mechanism. */ 16886 /* ARGSUSED */ 16887 static int 16888 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16889 { 16890 tf_t *tf; 16891 tcp_t *tcp; 16892 int i; 16893 zoneid_t zoneid; 16894 tcp_stack_t *tcps; 16895 16896 zoneid = Q_TO_CONN(q)->conn_zoneid; 16897 tcps = Q_TO_TCP(q)->tcp_tcps; 16898 16899 /* Refer to comments in tcp_status_report(). */ 16900 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16901 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16902 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16903 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16904 return (0); 16905 } 16906 } 16907 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16908 /* The following may work even if we cannot get a large buf. */ 16909 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16910 return (0); 16911 } 16912 16913 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16914 16915 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16916 tf = &tcps->tcps_acceptor_fanout[i]; 16917 mutex_enter(&tf->tf_lock); 16918 for (tcp = tf->tf_tcp; tcp != NULL; 16919 tcp = tcp->tcp_acceptor_hash) { 16920 if (zoneid != GLOBAL_ZONEID && 16921 zoneid != tcp->tcp_connp->conn_zoneid) 16922 continue; 16923 tcp_report_item(mp->b_cont, tcp, i, 16924 Q_TO_TCP(q), cr); 16925 } 16926 mutex_exit(&tf->tf_lock); 16927 } 16928 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16929 return (0); 16930 } 16931 16932 /* 16933 * tcp_timer is the timer service routine. It handles the retransmission, 16934 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16935 * from the state of the tcp instance what kind of action needs to be done 16936 * at the time it is called. 16937 */ 16938 static void 16939 tcp_timer(void *arg) 16940 { 16941 mblk_t *mp; 16942 clock_t first_threshold; 16943 clock_t second_threshold; 16944 clock_t ms; 16945 uint32_t mss; 16946 conn_t *connp = (conn_t *)arg; 16947 tcp_t *tcp = connp->conn_tcp; 16948 tcp_stack_t *tcps = tcp->tcp_tcps; 16949 16950 tcp->tcp_timer_tid = 0; 16951 16952 if (tcp->tcp_fused) 16953 return; 16954 16955 first_threshold = tcp->tcp_first_timer_threshold; 16956 second_threshold = tcp->tcp_second_timer_threshold; 16957 switch (tcp->tcp_state) { 16958 case TCPS_IDLE: 16959 case TCPS_BOUND: 16960 case TCPS_LISTEN: 16961 return; 16962 case TCPS_SYN_RCVD: { 16963 tcp_t *listener = tcp->tcp_listener; 16964 16965 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16966 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16967 /* it's our first timeout */ 16968 tcp->tcp_syn_rcvd_timeout = 1; 16969 mutex_enter(&listener->tcp_eager_lock); 16970 listener->tcp_syn_rcvd_timeout++; 16971 if (!tcp->tcp_dontdrop && tcp->tcp_closemp_used == 0) { 16972 /* 16973 * Make this eager available for drop if we 16974 * need to drop one to accomodate a new 16975 * incoming SYN request. 16976 */ 16977 MAKE_DROPPABLE(listener, tcp); 16978 } 16979 if (!listener->tcp_syn_defense && 16980 (listener->tcp_syn_rcvd_timeout > 16981 (tcps->tcps_conn_req_max_q0 >> 2)) && 16982 (tcps->tcps_conn_req_max_q0 > 200)) { 16983 /* We may be under attack. Put on a defense. */ 16984 listener->tcp_syn_defense = B_TRUE; 16985 cmn_err(CE_WARN, "High TCP connect timeout " 16986 "rate! System (port %d) may be under a " 16987 "SYN flood attack!", 16988 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16989 16990 listener->tcp_ip_addr_cache = kmem_zalloc( 16991 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16992 KM_NOSLEEP); 16993 } 16994 mutex_exit(&listener->tcp_eager_lock); 16995 } else if (listener != NULL) { 16996 mutex_enter(&listener->tcp_eager_lock); 16997 tcp->tcp_syn_rcvd_timeout++; 16998 if (tcp->tcp_syn_rcvd_timeout > 1 && 16999 tcp->tcp_closemp_used == 0) { 17000 /* 17001 * This is our second timeout. Put the tcp in 17002 * the list of droppable eagers to allow it to 17003 * be dropped, if needed. We don't check 17004 * whether tcp_dontdrop is set or not to 17005 * protect ourselve from a SYN attack where a 17006 * remote host can spoof itself as one of the 17007 * good IP source and continue to hold 17008 * resources too long. 17009 */ 17010 MAKE_DROPPABLE(listener, tcp); 17011 } 17012 mutex_exit(&listener->tcp_eager_lock); 17013 } 17014 } 17015 /* FALLTHRU */ 17016 case TCPS_SYN_SENT: 17017 first_threshold = tcp->tcp_first_ctimer_threshold; 17018 second_threshold = tcp->tcp_second_ctimer_threshold; 17019 break; 17020 case TCPS_ESTABLISHED: 17021 case TCPS_FIN_WAIT_1: 17022 case TCPS_CLOSING: 17023 case TCPS_CLOSE_WAIT: 17024 case TCPS_LAST_ACK: 17025 /* If we have data to rexmit */ 17026 if (tcp->tcp_suna != tcp->tcp_snxt) { 17027 clock_t time_to_wait; 17028 17029 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17030 if (!tcp->tcp_xmit_head) 17031 break; 17032 time_to_wait = lbolt - 17033 (clock_t)tcp->tcp_xmit_head->b_prev; 17034 time_to_wait = tcp->tcp_rto - 17035 TICK_TO_MSEC(time_to_wait); 17036 /* 17037 * If the timer fires too early, 1 clock tick earlier, 17038 * restart the timer. 17039 */ 17040 if (time_to_wait > msec_per_tick) { 17041 TCP_STAT(tcps, tcp_timer_fire_early); 17042 TCP_TIMER_RESTART(tcp, time_to_wait); 17043 return; 17044 } 17045 /* 17046 * When we probe zero windows, we force the swnd open. 17047 * If our peer acks with a closed window swnd will be 17048 * set to zero by tcp_rput(). As long as we are 17049 * receiving acks tcp_rput will 17050 * reset 'tcp_ms_we_have_waited' so as not to trip the 17051 * first and second interval actions. NOTE: the timer 17052 * interval is allowed to continue its exponential 17053 * backoff. 17054 */ 17055 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17056 if (tcp->tcp_debug) { 17057 (void) strlog(TCP_MOD_ID, 0, 1, 17058 SL_TRACE, "tcp_timer: zero win"); 17059 } 17060 } else { 17061 /* 17062 * After retransmission, we need to do 17063 * slow start. Set the ssthresh to one 17064 * half of current effective window and 17065 * cwnd to one MSS. Also reset 17066 * tcp_cwnd_cnt. 17067 * 17068 * Note that if tcp_ssthresh is reduced because 17069 * of ECN, do not reduce it again unless it is 17070 * already one window of data away (tcp_cwr 17071 * should then be cleared) or this is a 17072 * timeout for a retransmitted segment. 17073 */ 17074 uint32_t npkt; 17075 17076 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17077 npkt = ((tcp->tcp_timer_backoff ? 17078 tcp->tcp_cwnd_ssthresh : 17079 tcp->tcp_snxt - 17080 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17081 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17082 tcp->tcp_mss; 17083 } 17084 tcp->tcp_cwnd = tcp->tcp_mss; 17085 tcp->tcp_cwnd_cnt = 0; 17086 if (tcp->tcp_ecn_ok) { 17087 tcp->tcp_cwr = B_TRUE; 17088 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17089 tcp->tcp_ecn_cwr_sent = B_FALSE; 17090 } 17091 } 17092 break; 17093 } 17094 /* 17095 * We have something to send yet we cannot send. The 17096 * reason can be: 17097 * 17098 * 1. Zero send window: we need to do zero window probe. 17099 * 2. Zero cwnd: because of ECN, we need to "clock out 17100 * segments. 17101 * 3. SWS avoidance: receiver may have shrunk window, 17102 * reset our knowledge. 17103 * 17104 * Note that condition 2 can happen with either 1 or 17105 * 3. But 1 and 3 are exclusive. 17106 */ 17107 if (tcp->tcp_unsent != 0) { 17108 if (tcp->tcp_cwnd == 0) { 17109 /* 17110 * Set tcp_cwnd to 1 MSS so that a 17111 * new segment can be sent out. We 17112 * are "clocking out" new data when 17113 * the network is really congested. 17114 */ 17115 ASSERT(tcp->tcp_ecn_ok); 17116 tcp->tcp_cwnd = tcp->tcp_mss; 17117 } 17118 if (tcp->tcp_swnd == 0) { 17119 /* Extend window for zero window probe */ 17120 tcp->tcp_swnd++; 17121 tcp->tcp_zero_win_probe = B_TRUE; 17122 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17123 } else { 17124 /* 17125 * Handle timeout from sender SWS avoidance. 17126 * Reset our knowledge of the max send window 17127 * since the receiver might have reduced its 17128 * receive buffer. Avoid setting tcp_max_swnd 17129 * to one since that will essentially disable 17130 * the SWS checks. 17131 * 17132 * Note that since we don't have a SWS 17133 * state variable, if the timeout is set 17134 * for ECN but not for SWS, this 17135 * code will also be executed. This is 17136 * fine as tcp_max_swnd is updated 17137 * constantly and it will not affect 17138 * anything. 17139 */ 17140 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17141 } 17142 tcp_wput_data(tcp, NULL, B_FALSE); 17143 return; 17144 } 17145 /* Is there a FIN that needs to be to re retransmitted? */ 17146 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17147 !tcp->tcp_fin_acked) 17148 break; 17149 /* Nothing to do, return without restarting timer. */ 17150 TCP_STAT(tcps, tcp_timer_fire_miss); 17151 return; 17152 case TCPS_FIN_WAIT_2: 17153 /* 17154 * User closed the TCP endpoint and peer ACK'ed our FIN. 17155 * We waited some time for for peer's FIN, but it hasn't 17156 * arrived. We flush the connection now to avoid 17157 * case where the peer has rebooted. 17158 */ 17159 if (TCP_IS_DETACHED(tcp)) { 17160 (void) tcp_clean_death(tcp, 0, 23); 17161 } else { 17162 TCP_TIMER_RESTART(tcp, 17163 tcps->tcps_fin_wait_2_flush_interval); 17164 } 17165 return; 17166 case TCPS_TIME_WAIT: 17167 (void) tcp_clean_death(tcp, 0, 24); 17168 return; 17169 default: 17170 if (tcp->tcp_debug) { 17171 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17172 "tcp_timer: strange state (%d) %s", 17173 tcp->tcp_state, tcp_display(tcp, NULL, 17174 DISP_PORT_ONLY)); 17175 } 17176 return; 17177 } 17178 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17179 /* 17180 * For zero window probe, we need to send indefinitely, 17181 * unless we have not heard from the other side for some 17182 * time... 17183 */ 17184 if ((tcp->tcp_zero_win_probe == 0) || 17185 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17186 second_threshold)) { 17187 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17188 /* 17189 * If TCP is in SYN_RCVD state, send back a 17190 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17191 * should be zero in TCPS_SYN_RCVD state. 17192 */ 17193 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17194 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17195 "in SYN_RCVD", 17196 tcp, tcp->tcp_snxt, 17197 tcp->tcp_rnxt, TH_RST | TH_ACK); 17198 } 17199 (void) tcp_clean_death(tcp, 17200 tcp->tcp_client_errno ? 17201 tcp->tcp_client_errno : ETIMEDOUT, 25); 17202 return; 17203 } else { 17204 /* 17205 * Set tcp_ms_we_have_waited to second_threshold 17206 * so that in next timeout, we will do the above 17207 * check (lbolt - tcp_last_recv_time). This is 17208 * also to avoid overflow. 17209 * 17210 * We don't need to decrement tcp_timer_backoff 17211 * to avoid overflow because it will be decremented 17212 * later if new timeout value is greater than 17213 * tcp_rexmit_interval_max. In the case when 17214 * tcp_rexmit_interval_max is greater than 17215 * second_threshold, it means that we will wait 17216 * longer than second_threshold to send the next 17217 * window probe. 17218 */ 17219 tcp->tcp_ms_we_have_waited = second_threshold; 17220 } 17221 } else if (ms > first_threshold) { 17222 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17223 tcp->tcp_xmit_head != NULL) { 17224 tcp->tcp_xmit_head = 17225 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17226 } 17227 /* 17228 * We have been retransmitting for too long... The RTT 17229 * we calculated is probably incorrect. Reinitialize it. 17230 * Need to compensate for 0 tcp_rtt_sa. Reset 17231 * tcp_rtt_update so that we won't accidentally cache a 17232 * bad value. But only do this if this is not a zero 17233 * window probe. 17234 */ 17235 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17236 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17237 (tcp->tcp_rtt_sa >> 5); 17238 tcp->tcp_rtt_sa = 0; 17239 tcp_ip_notify(tcp); 17240 tcp->tcp_rtt_update = 0; 17241 } 17242 } 17243 tcp->tcp_timer_backoff++; 17244 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17245 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17246 tcps->tcps_rexmit_interval_min) { 17247 /* 17248 * This means the original RTO is tcp_rexmit_interval_min. 17249 * So we will use tcp_rexmit_interval_min as the RTO value 17250 * and do the backoff. 17251 */ 17252 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17253 } else { 17254 ms <<= tcp->tcp_timer_backoff; 17255 } 17256 if (ms > tcps->tcps_rexmit_interval_max) { 17257 ms = tcps->tcps_rexmit_interval_max; 17258 /* 17259 * ms is at max, decrement tcp_timer_backoff to avoid 17260 * overflow. 17261 */ 17262 tcp->tcp_timer_backoff--; 17263 } 17264 tcp->tcp_ms_we_have_waited += ms; 17265 if (tcp->tcp_zero_win_probe == 0) { 17266 tcp->tcp_rto = ms; 17267 } 17268 TCP_TIMER_RESTART(tcp, ms); 17269 /* 17270 * This is after a timeout and tcp_rto is backed off. Set 17271 * tcp_set_timer to 1 so that next time RTO is updated, we will 17272 * restart the timer with a correct value. 17273 */ 17274 tcp->tcp_set_timer = 1; 17275 mss = tcp->tcp_snxt - tcp->tcp_suna; 17276 if (mss > tcp->tcp_mss) 17277 mss = tcp->tcp_mss; 17278 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17279 mss = tcp->tcp_swnd; 17280 17281 if ((mp = tcp->tcp_xmit_head) != NULL) 17282 mp->b_prev = (mblk_t *)lbolt; 17283 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17284 B_TRUE); 17285 17286 /* 17287 * When slow start after retransmission begins, start with 17288 * this seq no. tcp_rexmit_max marks the end of special slow 17289 * start phase. tcp_snd_burst controls how many segments 17290 * can be sent because of an ack. 17291 */ 17292 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17293 tcp->tcp_snd_burst = TCP_CWND_SS; 17294 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17295 (tcp->tcp_unsent == 0)) { 17296 tcp->tcp_rexmit_max = tcp->tcp_fss; 17297 } else { 17298 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17299 } 17300 tcp->tcp_rexmit = B_TRUE; 17301 tcp->tcp_dupack_cnt = 0; 17302 17303 /* 17304 * Remove all rexmit SACK blk to start from fresh. 17305 */ 17306 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17307 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17308 tcp->tcp_num_notsack_blk = 0; 17309 tcp->tcp_cnt_notsack_list = 0; 17310 } 17311 if (mp == NULL) { 17312 return; 17313 } 17314 /* Attach credentials to retransmitted initial SYNs. */ 17315 if (tcp->tcp_state == TCPS_SYN_SENT) { 17316 mblk_setcred(mp, tcp->tcp_cred); 17317 DB_CPID(mp) = tcp->tcp_cpid; 17318 } 17319 17320 tcp->tcp_csuna = tcp->tcp_snxt; 17321 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17322 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17323 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 17324 tcp_send_data(tcp, tcp->tcp_wq, mp); 17325 17326 } 17327 17328 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17329 static void 17330 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17331 { 17332 conn_t *connp; 17333 17334 switch (tcp->tcp_state) { 17335 case TCPS_BOUND: 17336 case TCPS_LISTEN: 17337 break; 17338 default: 17339 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17340 return; 17341 } 17342 17343 /* 17344 * Need to clean up all the eagers since after the unbind, segments 17345 * will no longer be delivered to this listener stream. 17346 */ 17347 mutex_enter(&tcp->tcp_eager_lock); 17348 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17349 tcp_eager_cleanup(tcp, 0); 17350 } 17351 mutex_exit(&tcp->tcp_eager_lock); 17352 17353 if (tcp->tcp_ipversion == IPV4_VERSION) { 17354 tcp->tcp_ipha->ipha_src = 0; 17355 } else { 17356 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17357 } 17358 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17359 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17360 tcp_bind_hash_remove(tcp); 17361 tcp->tcp_state = TCPS_IDLE; 17362 tcp->tcp_mdt = B_FALSE; 17363 /* Send M_FLUSH according to TPI */ 17364 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17365 connp = tcp->tcp_connp; 17366 connp->conn_mdt_ok = B_FALSE; 17367 ipcl_hash_remove(connp); 17368 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17369 mp = mi_tpi_ok_ack_alloc(mp); 17370 putnext(tcp->tcp_rq, mp); 17371 } 17372 17373 /* 17374 * Don't let port fall into the privileged range. 17375 * Since the extra privileged ports can be arbitrary we also 17376 * ensure that we exclude those from consideration. 17377 * tcp_g_epriv_ports is not sorted thus we loop over it until 17378 * there are no changes. 17379 * 17380 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17381 * but instead the code relies on: 17382 * - the fact that the address of the array and its size never changes 17383 * - the atomic assignment of the elements of the array 17384 * 17385 * Returns 0 if there are no more ports available. 17386 * 17387 * TS note: skip multilevel ports. 17388 */ 17389 static in_port_t 17390 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17391 { 17392 int i; 17393 boolean_t restart = B_FALSE; 17394 tcp_stack_t *tcps = tcp->tcp_tcps; 17395 17396 if (random && tcp_random_anon_port != 0) { 17397 (void) random_get_pseudo_bytes((uint8_t *)&port, 17398 sizeof (in_port_t)); 17399 /* 17400 * Unless changed by a sys admin, the smallest anon port 17401 * is 32768 and the largest anon port is 65535. It is 17402 * very likely (50%) for the random port to be smaller 17403 * than the smallest anon port. When that happens, 17404 * add port % (anon port range) to the smallest anon 17405 * port to get the random port. It should fall into the 17406 * valid anon port range. 17407 */ 17408 if (port < tcps->tcps_smallest_anon_port) { 17409 port = tcps->tcps_smallest_anon_port + 17410 port % (tcps->tcps_largest_anon_port - 17411 tcps->tcps_smallest_anon_port); 17412 } 17413 } 17414 17415 retry: 17416 if (port < tcps->tcps_smallest_anon_port) 17417 port = (in_port_t)tcps->tcps_smallest_anon_port; 17418 17419 if (port > tcps->tcps_largest_anon_port) { 17420 if (restart) 17421 return (0); 17422 restart = B_TRUE; 17423 port = (in_port_t)tcps->tcps_smallest_anon_port; 17424 } 17425 17426 if (port < tcps->tcps_smallest_nonpriv_port) 17427 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17428 17429 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17430 if (port == tcps->tcps_g_epriv_ports[i]) { 17431 port++; 17432 /* 17433 * Make sure whether the port is in the 17434 * valid range. 17435 */ 17436 goto retry; 17437 } 17438 } 17439 if (is_system_labeled() && 17440 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17441 IPPROTO_TCP, B_TRUE)) != 0) { 17442 port = i; 17443 goto retry; 17444 } 17445 return (port); 17446 } 17447 17448 /* 17449 * Return the next anonymous port in the privileged port range for 17450 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17451 * downwards. This is the same behavior as documented in the userland 17452 * library call rresvport(3N). 17453 * 17454 * TS note: skip multilevel ports. 17455 */ 17456 static in_port_t 17457 tcp_get_next_priv_port(const tcp_t *tcp) 17458 { 17459 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17460 in_port_t nextport; 17461 boolean_t restart = B_FALSE; 17462 tcp_stack_t *tcps = tcp->tcp_tcps; 17463 retry: 17464 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17465 next_priv_port >= IPPORT_RESERVED) { 17466 next_priv_port = IPPORT_RESERVED - 1; 17467 if (restart) 17468 return (0); 17469 restart = B_TRUE; 17470 } 17471 if (is_system_labeled() && 17472 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17473 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17474 next_priv_port = nextport; 17475 goto retry; 17476 } 17477 return (next_priv_port--); 17478 } 17479 17480 /* The write side r/w procedure. */ 17481 17482 #if CCS_STATS 17483 struct { 17484 struct { 17485 int64_t count, bytes; 17486 } tot, hit; 17487 } wrw_stats; 17488 #endif 17489 17490 /* 17491 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17492 * messages. 17493 */ 17494 /* ARGSUSED */ 17495 static void 17496 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17497 { 17498 conn_t *connp = (conn_t *)arg; 17499 tcp_t *tcp = connp->conn_tcp; 17500 queue_t *q = tcp->tcp_wq; 17501 17502 ASSERT(DB_TYPE(mp) != M_IOCTL); 17503 /* 17504 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17505 * Once the close starts, streamhead and sockfs will not let any data 17506 * packets come down (close ensures that there are no threads using the 17507 * queue and no new threads will come down) but since qprocsoff() 17508 * hasn't happened yet, a M_FLUSH or some non data message might 17509 * get reflected back (in response to our own FLUSHRW) and get 17510 * processed after tcp_close() is done. The conn would still be valid 17511 * because a ref would have added but we need to check the state 17512 * before actually processing the packet. 17513 */ 17514 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17515 freemsg(mp); 17516 return; 17517 } 17518 17519 switch (DB_TYPE(mp)) { 17520 case M_IOCDATA: 17521 tcp_wput_iocdata(tcp, mp); 17522 break; 17523 case M_FLUSH: 17524 tcp_wput_flush(tcp, mp); 17525 break; 17526 default: 17527 CALL_IP_WPUT(connp, q, mp); 17528 break; 17529 } 17530 } 17531 17532 /* 17533 * The TCP fast path write put procedure. 17534 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17535 */ 17536 /* ARGSUSED */ 17537 void 17538 tcp_output(void *arg, mblk_t *mp, void *arg2) 17539 { 17540 int len; 17541 int hdrlen; 17542 int plen; 17543 mblk_t *mp1; 17544 uchar_t *rptr; 17545 uint32_t snxt; 17546 tcph_t *tcph; 17547 struct datab *db; 17548 uint32_t suna; 17549 uint32_t mss; 17550 ipaddr_t *dst; 17551 ipaddr_t *src; 17552 uint32_t sum; 17553 int usable; 17554 conn_t *connp = (conn_t *)arg; 17555 tcp_t *tcp = connp->conn_tcp; 17556 uint32_t msize; 17557 tcp_stack_t *tcps = tcp->tcp_tcps; 17558 17559 /* 17560 * Try and ASSERT the minimum possible references on the 17561 * conn early enough. Since we are executing on write side, 17562 * the connection is obviously not detached and that means 17563 * there is a ref each for TCP and IP. Since we are behind 17564 * the squeue, the minimum references needed are 3. If the 17565 * conn is in classifier hash list, there should be an 17566 * extra ref for that (we check both the possibilities). 17567 */ 17568 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17569 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17570 17571 ASSERT(DB_TYPE(mp) == M_DATA); 17572 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17573 17574 mutex_enter(&tcp->tcp_non_sq_lock); 17575 tcp->tcp_squeue_bytes -= msize; 17576 mutex_exit(&tcp->tcp_non_sq_lock); 17577 17578 /* Bypass tcp protocol for fused tcp loopback */ 17579 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17580 return; 17581 17582 mss = tcp->tcp_mss; 17583 if (tcp->tcp_xmit_zc_clean) 17584 mp = tcp_zcopy_backoff(tcp, mp, 0); 17585 17586 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17587 len = (int)(mp->b_wptr - mp->b_rptr); 17588 17589 /* 17590 * Criteria for fast path: 17591 * 17592 * 1. no unsent data 17593 * 2. single mblk in request 17594 * 3. connection established 17595 * 4. data in mblk 17596 * 5. len <= mss 17597 * 6. no tcp_valid bits 17598 */ 17599 if ((tcp->tcp_unsent != 0) || 17600 (tcp->tcp_cork) || 17601 (mp->b_cont != NULL) || 17602 (tcp->tcp_state != TCPS_ESTABLISHED) || 17603 (len == 0) || 17604 (len > mss) || 17605 (tcp->tcp_valid_bits != 0)) { 17606 tcp_wput_data(tcp, mp, B_FALSE); 17607 return; 17608 } 17609 17610 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17611 ASSERT(tcp->tcp_fin_sent == 0); 17612 17613 /* queue new packet onto retransmission queue */ 17614 if (tcp->tcp_xmit_head == NULL) { 17615 tcp->tcp_xmit_head = mp; 17616 } else { 17617 tcp->tcp_xmit_last->b_cont = mp; 17618 } 17619 tcp->tcp_xmit_last = mp; 17620 tcp->tcp_xmit_tail = mp; 17621 17622 /* find out how much we can send */ 17623 /* BEGIN CSTYLED */ 17624 /* 17625 * un-acked usable 17626 * |--------------|-----------------| 17627 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17628 */ 17629 /* END CSTYLED */ 17630 17631 /* start sending from tcp_snxt */ 17632 snxt = tcp->tcp_snxt; 17633 17634 /* 17635 * Check to see if this connection has been idled for some 17636 * time and no ACK is expected. If it is, we need to slow 17637 * start again to get back the connection's "self-clock" as 17638 * described in VJ's paper. 17639 * 17640 * Refer to the comment in tcp_mss_set() for the calculation 17641 * of tcp_cwnd after idle. 17642 */ 17643 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17644 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17645 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17646 } 17647 17648 usable = tcp->tcp_swnd; /* tcp window size */ 17649 if (usable > tcp->tcp_cwnd) 17650 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17651 usable -= snxt; /* subtract stuff already sent */ 17652 suna = tcp->tcp_suna; 17653 usable += suna; 17654 /* usable can be < 0 if the congestion window is smaller */ 17655 if (len > usable) { 17656 /* Can't send complete M_DATA in one shot */ 17657 goto slow; 17658 } 17659 17660 mutex_enter(&tcp->tcp_non_sq_lock); 17661 if (tcp->tcp_flow_stopped && 17662 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17663 tcp_clrqfull(tcp); 17664 } 17665 mutex_exit(&tcp->tcp_non_sq_lock); 17666 17667 /* 17668 * determine if anything to send (Nagle). 17669 * 17670 * 1. len < tcp_mss (i.e. small) 17671 * 2. unacknowledged data present 17672 * 3. len < nagle limit 17673 * 4. last packet sent < nagle limit (previous packet sent) 17674 */ 17675 if ((len < mss) && (snxt != suna) && 17676 (len < (int)tcp->tcp_naglim) && 17677 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17678 /* 17679 * This was the first unsent packet and normally 17680 * mss < xmit_hiwater so there is no need to worry 17681 * about flow control. The next packet will go 17682 * through the flow control check in tcp_wput_data(). 17683 */ 17684 /* leftover work from above */ 17685 tcp->tcp_unsent = len; 17686 tcp->tcp_xmit_tail_unsent = len; 17687 17688 return; 17689 } 17690 17691 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17692 17693 if (snxt == suna) { 17694 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17695 } 17696 17697 /* we have always sent something */ 17698 tcp->tcp_rack_cnt = 0; 17699 17700 tcp->tcp_snxt = snxt + len; 17701 tcp->tcp_rack = tcp->tcp_rnxt; 17702 17703 if ((mp1 = dupb(mp)) == 0) 17704 goto no_memory; 17705 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17706 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17707 17708 /* adjust tcp header information */ 17709 tcph = tcp->tcp_tcph; 17710 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17711 17712 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17713 sum = (sum >> 16) + (sum & 0xFFFF); 17714 U16_TO_ABE16(sum, tcph->th_sum); 17715 17716 U32_TO_ABE32(snxt, tcph->th_seq); 17717 17718 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17719 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17720 BUMP_LOCAL(tcp->tcp_obsegs); 17721 17722 /* Update the latest receive window size in TCP header. */ 17723 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17724 tcph->th_win); 17725 17726 tcp->tcp_last_sent_len = (ushort_t)len; 17727 17728 plen = len + tcp->tcp_hdr_len; 17729 17730 if (tcp->tcp_ipversion == IPV4_VERSION) { 17731 tcp->tcp_ipha->ipha_length = htons(plen); 17732 } else { 17733 tcp->tcp_ip6h->ip6_plen = htons(plen - 17734 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17735 } 17736 17737 /* see if we need to allocate a mblk for the headers */ 17738 hdrlen = tcp->tcp_hdr_len; 17739 rptr = mp1->b_rptr - hdrlen; 17740 db = mp1->b_datap; 17741 if ((db->db_ref != 2) || rptr < db->db_base || 17742 (!OK_32PTR(rptr))) { 17743 /* NOTE: we assume allocb returns an OK_32PTR */ 17744 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17745 tcps->tcps_wroff_xtra, BPRI_MED); 17746 if (!mp) { 17747 freemsg(mp1); 17748 goto no_memory; 17749 } 17750 mp->b_cont = mp1; 17751 mp1 = mp; 17752 /* Leave room for Link Level header */ 17753 /* hdrlen = tcp->tcp_hdr_len; */ 17754 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17755 mp1->b_wptr = &rptr[hdrlen]; 17756 } 17757 mp1->b_rptr = rptr; 17758 17759 /* Fill in the timestamp option. */ 17760 if (tcp->tcp_snd_ts_ok) { 17761 U32_TO_BE32((uint32_t)lbolt, 17762 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17763 U32_TO_BE32(tcp->tcp_ts_recent, 17764 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17765 } else { 17766 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17767 } 17768 17769 /* copy header into outgoing packet */ 17770 dst = (ipaddr_t *)rptr; 17771 src = (ipaddr_t *)tcp->tcp_iphc; 17772 dst[0] = src[0]; 17773 dst[1] = src[1]; 17774 dst[2] = src[2]; 17775 dst[3] = src[3]; 17776 dst[4] = src[4]; 17777 dst[5] = src[5]; 17778 dst[6] = src[6]; 17779 dst[7] = src[7]; 17780 dst[8] = src[8]; 17781 dst[9] = src[9]; 17782 if (hdrlen -= 40) { 17783 hdrlen >>= 2; 17784 dst += 10; 17785 src += 10; 17786 do { 17787 *dst++ = *src++; 17788 } while (--hdrlen); 17789 } 17790 17791 /* 17792 * Set the ECN info in the TCP header. Note that this 17793 * is not the template header. 17794 */ 17795 if (tcp->tcp_ecn_ok) { 17796 SET_ECT(tcp, rptr); 17797 17798 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17799 if (tcp->tcp_ecn_echo_on) 17800 tcph->th_flags[0] |= TH_ECE; 17801 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17802 tcph->th_flags[0] |= TH_CWR; 17803 tcp->tcp_ecn_cwr_sent = B_TRUE; 17804 } 17805 } 17806 17807 if (tcp->tcp_ip_forward_progress) { 17808 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17809 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17810 tcp->tcp_ip_forward_progress = B_FALSE; 17811 } 17812 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17813 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17814 return; 17815 17816 /* 17817 * If we ran out of memory, we pretend to have sent the packet 17818 * and that it was lost on the wire. 17819 */ 17820 no_memory: 17821 return; 17822 17823 slow: 17824 /* leftover work from above */ 17825 tcp->tcp_unsent = len; 17826 tcp->tcp_xmit_tail_unsent = len; 17827 tcp_wput_data(tcp, NULL, B_FALSE); 17828 } 17829 17830 /* 17831 * The function called through squeue to get behind eager's perimeter to 17832 * finish the accept processing. 17833 */ 17834 /* ARGSUSED */ 17835 void 17836 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17837 { 17838 conn_t *connp = (conn_t *)arg; 17839 tcp_t *tcp = connp->conn_tcp; 17840 queue_t *q = tcp->tcp_rq; 17841 mblk_t *mp1; 17842 mblk_t *stropt_mp = mp; 17843 struct stroptions *stropt; 17844 uint_t thwin; 17845 tcp_stack_t *tcps = tcp->tcp_tcps; 17846 17847 /* 17848 * Drop the eager's ref on the listener, that was placed when 17849 * this eager began life in tcp_conn_request. 17850 */ 17851 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17852 17853 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17854 /* 17855 * Someone blewoff the eager before we could finish 17856 * the accept. 17857 * 17858 * The only reason eager exists it because we put in 17859 * a ref on it when conn ind went up. We need to send 17860 * a disconnect indication up while the last reference 17861 * on the eager will be dropped by the squeue when we 17862 * return. 17863 */ 17864 ASSERT(tcp->tcp_listener == NULL); 17865 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17866 struct T_discon_ind *tdi; 17867 17868 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17869 /* 17870 * Let us reuse the incoming mblk to avoid memory 17871 * allocation failure problems. We know that the 17872 * size of the incoming mblk i.e. stroptions is greater 17873 * than sizeof T_discon_ind. So the reallocb below 17874 * can't fail. 17875 */ 17876 freemsg(mp->b_cont); 17877 mp->b_cont = NULL; 17878 ASSERT(DB_REF(mp) == 1); 17879 mp = reallocb(mp, sizeof (struct T_discon_ind), 17880 B_FALSE); 17881 ASSERT(mp != NULL); 17882 DB_TYPE(mp) = M_PROTO; 17883 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17884 tdi = (struct T_discon_ind *)mp->b_rptr; 17885 if (tcp->tcp_issocket) { 17886 tdi->DISCON_reason = ECONNREFUSED; 17887 tdi->SEQ_number = 0; 17888 } else { 17889 tdi->DISCON_reason = ENOPROTOOPT; 17890 tdi->SEQ_number = 17891 tcp->tcp_conn_req_seqnum; 17892 } 17893 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17894 putnext(q, mp); 17895 } else { 17896 freemsg(mp); 17897 } 17898 if (tcp->tcp_hard_binding) { 17899 tcp->tcp_hard_binding = B_FALSE; 17900 tcp->tcp_hard_bound = B_TRUE; 17901 } 17902 tcp->tcp_detached = B_FALSE; 17903 return; 17904 } 17905 17906 mp1 = stropt_mp->b_cont; 17907 stropt_mp->b_cont = NULL; 17908 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17909 stropt = (struct stroptions *)stropt_mp->b_rptr; 17910 17911 while (mp1 != NULL) { 17912 mp = mp1; 17913 mp1 = mp1->b_cont; 17914 mp->b_cont = NULL; 17915 tcp->tcp_drop_opt_ack_cnt++; 17916 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17917 } 17918 mp = NULL; 17919 17920 /* 17921 * For a loopback connection with tcp_direct_sockfs on, note that 17922 * we don't have to protect tcp_rcv_list yet because synchronous 17923 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17924 * possibly race with us. 17925 */ 17926 17927 /* 17928 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17929 * properly. This is the first time we know of the acceptor' 17930 * queue. So we do it here. 17931 */ 17932 if (tcp->tcp_rcv_list == NULL) { 17933 /* 17934 * Recv queue is empty, tcp_rwnd should not have changed. 17935 * That means it should be equal to the listener's tcp_rwnd. 17936 */ 17937 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17938 } else { 17939 #ifdef DEBUG 17940 uint_t cnt = 0; 17941 17942 mp1 = tcp->tcp_rcv_list; 17943 while ((mp = mp1) != NULL) { 17944 mp1 = mp->b_next; 17945 cnt += msgdsize(mp); 17946 } 17947 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17948 #endif 17949 /* There is some data, add them back to get the max. */ 17950 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17951 } 17952 17953 stropt->so_flags = SO_HIWAT; 17954 stropt->so_hiwat = MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 17955 17956 stropt->so_flags |= SO_MAXBLK; 17957 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17958 17959 /* 17960 * This is the first time we run on the correct 17961 * queue after tcp_accept. So fix all the q parameters 17962 * here. 17963 */ 17964 /* Allocate room for SACK options if needed. */ 17965 stropt->so_flags |= SO_WROFF; 17966 if (tcp->tcp_fused) { 17967 ASSERT(tcp->tcp_loopback); 17968 ASSERT(tcp->tcp_loopback_peer != NULL); 17969 /* 17970 * For fused tcp loopback, set the stream head's write 17971 * offset value to zero since we won't be needing any room 17972 * for TCP/IP headers. This would also improve performance 17973 * since it would reduce the amount of work done by kmem. 17974 * Non-fused tcp loopback case is handled separately below. 17975 */ 17976 stropt->so_wroff = 0; 17977 /* 17978 * Record the stream head's high water mark for this endpoint; 17979 * this is used for flow-control purposes in tcp_fuse_output(). 17980 */ 17981 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 17982 /* 17983 * Update the peer's transmit parameters according to 17984 * our recently calculated high water mark value. 17985 */ 17986 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17987 } else if (tcp->tcp_snd_sack_ok) { 17988 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17989 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17990 } else { 17991 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17992 tcps->tcps_wroff_xtra); 17993 } 17994 17995 /* 17996 * If this is endpoint is handling SSL, then reserve extra 17997 * offset and space at the end. 17998 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17999 * overriding the previous setting. The extra cost of signing and 18000 * encrypting multiple MSS-size records (12 of them with Ethernet), 18001 * instead of a single contiguous one by the stream head 18002 * largely outweighs the statistical reduction of ACKs, when 18003 * applicable. The peer will also save on decyption and verification 18004 * costs. 18005 */ 18006 if (tcp->tcp_kssl_ctx != NULL) { 18007 stropt->so_wroff += SSL3_WROFFSET; 18008 18009 stropt->so_flags |= SO_TAIL; 18010 stropt->so_tail = SSL3_MAX_TAIL_LEN; 18011 18012 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 18013 } 18014 18015 /* Send the options up */ 18016 putnext(q, stropt_mp); 18017 18018 /* 18019 * Pass up any data and/or a fin that has been received. 18020 * 18021 * Adjust receive window in case it had decreased 18022 * (because there is data <=> tcp_rcv_list != NULL) 18023 * while the connection was detached. Note that 18024 * in case the eager was flow-controlled, w/o this 18025 * code, the rwnd may never open up again! 18026 */ 18027 if (tcp->tcp_rcv_list != NULL) { 18028 /* We drain directly in case of fused tcp loopback */ 18029 if (!tcp->tcp_fused && canputnext(q)) { 18030 tcp->tcp_rwnd = q->q_hiwat; 18031 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 18032 << tcp->tcp_rcv_ws; 18033 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 18034 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18035 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 18036 tcp_xmit_ctl(NULL, 18037 tcp, (tcp->tcp_swnd == 0) ? 18038 tcp->tcp_suna : tcp->tcp_snxt, 18039 tcp->tcp_rnxt, TH_ACK); 18040 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 18041 } 18042 18043 } 18044 (void) tcp_rcv_drain(q, tcp); 18045 18046 /* 18047 * For fused tcp loopback, back-enable peer endpoint 18048 * if it's currently flow-controlled. 18049 */ 18050 if (tcp->tcp_fused) { 18051 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18052 18053 ASSERT(peer_tcp != NULL); 18054 ASSERT(peer_tcp->tcp_fused); 18055 /* 18056 * In order to change the peer's tcp_flow_stopped, 18057 * we need to take locks for both end points. The 18058 * highest address is taken first. 18059 */ 18060 if (peer_tcp > tcp) { 18061 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18062 mutex_enter(&tcp->tcp_non_sq_lock); 18063 } else { 18064 mutex_enter(&tcp->tcp_non_sq_lock); 18065 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18066 } 18067 if (peer_tcp->tcp_flow_stopped) { 18068 tcp_clrqfull(peer_tcp); 18069 TCP_STAT(tcps, tcp_fusion_backenabled); 18070 } 18071 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18072 mutex_exit(&tcp->tcp_non_sq_lock); 18073 } 18074 } 18075 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18076 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18077 mp = mi_tpi_ordrel_ind(); 18078 if (mp) { 18079 tcp->tcp_ordrel_done = B_TRUE; 18080 putnext(q, mp); 18081 if (tcp->tcp_deferred_clean_death) { 18082 /* 18083 * tcp_clean_death was deferred 18084 * for T_ORDREL_IND - do it now 18085 */ 18086 (void) tcp_clean_death(tcp, 18087 tcp->tcp_client_errno, 21); 18088 tcp->tcp_deferred_clean_death = B_FALSE; 18089 } 18090 } else { 18091 /* 18092 * Run the orderly release in the 18093 * service routine. 18094 */ 18095 qenable(q); 18096 } 18097 } 18098 if (tcp->tcp_hard_binding) { 18099 tcp->tcp_hard_binding = B_FALSE; 18100 tcp->tcp_hard_bound = B_TRUE; 18101 } 18102 18103 tcp->tcp_detached = B_FALSE; 18104 18105 /* We can enable synchronous streams now */ 18106 if (tcp->tcp_fused) { 18107 tcp_fuse_syncstr_enable_pair(tcp); 18108 } 18109 18110 if (tcp->tcp_ka_enabled) { 18111 tcp->tcp_ka_last_intrvl = 0; 18112 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18113 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18114 } 18115 18116 /* 18117 * At this point, eager is fully established and will 18118 * have the following references - 18119 * 18120 * 2 references for connection to exist (1 for TCP and 1 for IP). 18121 * 1 reference for the squeue which will be dropped by the squeue as 18122 * soon as this function returns. 18123 * There will be 1 additonal reference for being in classifier 18124 * hash list provided something bad hasn't happened. 18125 */ 18126 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18127 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18128 } 18129 18130 /* 18131 * The function called through squeue to get behind listener's perimeter to 18132 * send a deffered conn_ind. 18133 */ 18134 /* ARGSUSED */ 18135 void 18136 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18137 { 18138 conn_t *connp = (conn_t *)arg; 18139 tcp_t *listener = connp->conn_tcp; 18140 18141 if (listener->tcp_state == TCPS_CLOSED || 18142 TCP_IS_DETACHED(listener)) { 18143 /* 18144 * If listener has closed, it would have caused a 18145 * a cleanup/blowoff to happen for the eager. 18146 */ 18147 tcp_t *tcp; 18148 struct T_conn_ind *conn_ind; 18149 18150 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18151 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18152 conn_ind->OPT_length); 18153 /* 18154 * We need to drop the ref on eager that was put 18155 * tcp_rput_data() before trying to send the conn_ind 18156 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18157 * and tcp_wput_accept() is sending this deferred conn_ind but 18158 * listener is closed so we drop the ref. 18159 */ 18160 CONN_DEC_REF(tcp->tcp_connp); 18161 freemsg(mp); 18162 return; 18163 } 18164 putnext(listener->tcp_rq, mp); 18165 } 18166 18167 18168 /* 18169 * This is the STREAMS entry point for T_CONN_RES coming down on 18170 * Acceptor STREAM when sockfs listener does accept processing. 18171 * Read the block comment on top of tcp_conn_request(). 18172 */ 18173 void 18174 tcp_wput_accept(queue_t *q, mblk_t *mp) 18175 { 18176 queue_t *rq = RD(q); 18177 struct T_conn_res *conn_res; 18178 tcp_t *eager; 18179 tcp_t *listener; 18180 struct T_ok_ack *ok; 18181 t_scalar_t PRIM_type; 18182 mblk_t *opt_mp; 18183 conn_t *econnp; 18184 18185 ASSERT(DB_TYPE(mp) == M_PROTO); 18186 18187 conn_res = (struct T_conn_res *)mp->b_rptr; 18188 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18189 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18190 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18191 if (mp != NULL) 18192 putnext(rq, mp); 18193 return; 18194 } 18195 switch (conn_res->PRIM_type) { 18196 case O_T_CONN_RES: 18197 case T_CONN_RES: 18198 /* 18199 * We pass up an err ack if allocb fails. This will 18200 * cause sockfs to issue a T_DISCON_REQ which will cause 18201 * tcp_eager_blowoff to be called. sockfs will then call 18202 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18203 * we need to do the allocb up here because we have to 18204 * make sure rq->q_qinfo->qi_qclose still points to the 18205 * correct function (tcpclose_accept) in case allocb 18206 * fails. 18207 */ 18208 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18209 if (opt_mp == NULL) { 18210 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18211 if (mp != NULL) 18212 putnext(rq, mp); 18213 return; 18214 } 18215 18216 bcopy(mp->b_rptr + conn_res->OPT_offset, 18217 &eager, conn_res->OPT_length); 18218 PRIM_type = conn_res->PRIM_type; 18219 mp->b_datap->db_type = M_PCPROTO; 18220 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18221 ok = (struct T_ok_ack *)mp->b_rptr; 18222 ok->PRIM_type = T_OK_ACK; 18223 ok->CORRECT_prim = PRIM_type; 18224 econnp = eager->tcp_connp; 18225 econnp->conn_dev = (dev_t)q->q_ptr; 18226 eager->tcp_rq = rq; 18227 eager->tcp_wq = q; 18228 rq->q_ptr = econnp; 18229 rq->q_qinfo = &tcp_rinit; 18230 q->q_ptr = econnp; 18231 q->q_qinfo = &tcp_winit; 18232 listener = eager->tcp_listener; 18233 eager->tcp_issocket = B_TRUE; 18234 18235 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18236 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18237 ASSERT(econnp->conn_netstack == 18238 listener->tcp_connp->conn_netstack); 18239 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18240 18241 /* Put the ref for IP */ 18242 CONN_INC_REF(econnp); 18243 18244 /* 18245 * We should have minimum of 3 references on the conn 18246 * at this point. One each for TCP and IP and one for 18247 * the T_conn_ind that was sent up when the 3-way handshake 18248 * completed. In the normal case we would also have another 18249 * reference (making a total of 4) for the conn being in the 18250 * classifier hash list. However the eager could have received 18251 * an RST subsequently and tcp_closei_local could have removed 18252 * the eager from the classifier hash list, hence we can't 18253 * assert that reference. 18254 */ 18255 ASSERT(econnp->conn_ref >= 3); 18256 18257 /* 18258 * Send the new local address also up to sockfs. There 18259 * should already be enough space in the mp that came 18260 * down from soaccept(). 18261 */ 18262 if (eager->tcp_family == AF_INET) { 18263 sin_t *sin; 18264 18265 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18266 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18267 sin = (sin_t *)mp->b_wptr; 18268 mp->b_wptr += sizeof (sin_t); 18269 sin->sin_family = AF_INET; 18270 sin->sin_port = eager->tcp_lport; 18271 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18272 } else { 18273 sin6_t *sin6; 18274 18275 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18276 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18277 sin6 = (sin6_t *)mp->b_wptr; 18278 mp->b_wptr += sizeof (sin6_t); 18279 sin6->sin6_family = AF_INET6; 18280 sin6->sin6_port = eager->tcp_lport; 18281 if (eager->tcp_ipversion == IPV4_VERSION) { 18282 sin6->sin6_flowinfo = 0; 18283 IN6_IPADDR_TO_V4MAPPED( 18284 eager->tcp_ipha->ipha_src, 18285 &sin6->sin6_addr); 18286 } else { 18287 ASSERT(eager->tcp_ip6h != NULL); 18288 sin6->sin6_flowinfo = 18289 eager->tcp_ip6h->ip6_vcf & 18290 ~IPV6_VERS_AND_FLOW_MASK; 18291 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18292 } 18293 sin6->sin6_scope_id = 0; 18294 sin6->__sin6_src_id = 0; 18295 } 18296 18297 putnext(rq, mp); 18298 18299 opt_mp->b_datap->db_type = M_SETOPTS; 18300 opt_mp->b_wptr += sizeof (struct stroptions); 18301 18302 /* 18303 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18304 * from listener to acceptor. The message is chained on the 18305 * bind_mp which tcp_rput_other will send down to IP. 18306 */ 18307 if (listener->tcp_bound_if != 0) { 18308 /* allocate optmgmt req */ 18309 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18310 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18311 sizeof (int)); 18312 if (mp != NULL) 18313 linkb(opt_mp, mp); 18314 } 18315 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18316 uint_t on = 1; 18317 18318 /* allocate optmgmt req */ 18319 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18320 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18321 if (mp != NULL) 18322 linkb(opt_mp, mp); 18323 } 18324 18325 18326 mutex_enter(&listener->tcp_eager_lock); 18327 18328 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18329 18330 tcp_t *tail; 18331 tcp_t *tcp; 18332 mblk_t *mp1; 18333 18334 tcp = listener->tcp_eager_prev_q0; 18335 /* 18336 * listener->tcp_eager_prev_q0 points to the TAIL of the 18337 * deferred T_conn_ind queue. We need to get to the head 18338 * of the queue in order to send up T_conn_ind the same 18339 * order as how the 3WHS is completed. 18340 */ 18341 while (tcp != listener) { 18342 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18343 !tcp->tcp_kssl_pending) 18344 break; 18345 else 18346 tcp = tcp->tcp_eager_prev_q0; 18347 } 18348 /* None of the pending eagers can be sent up now */ 18349 if (tcp == listener) 18350 goto no_more_eagers; 18351 18352 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18353 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18354 /* Move from q0 to q */ 18355 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18356 listener->tcp_conn_req_cnt_q0--; 18357 listener->tcp_conn_req_cnt_q++; 18358 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18359 tcp->tcp_eager_prev_q0; 18360 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18361 tcp->tcp_eager_next_q0; 18362 tcp->tcp_eager_prev_q0 = NULL; 18363 tcp->tcp_eager_next_q0 = NULL; 18364 tcp->tcp_conn_def_q0 = B_FALSE; 18365 18366 /* Make sure the tcp isn't in the list of droppables */ 18367 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18368 tcp->tcp_eager_prev_drop_q0 == NULL); 18369 18370 /* 18371 * Insert at end of the queue because sockfs sends 18372 * down T_CONN_RES in chronological order. Leaving 18373 * the older conn indications at front of the queue 18374 * helps reducing search time. 18375 */ 18376 tail = listener->tcp_eager_last_q; 18377 if (tail != NULL) { 18378 tail->tcp_eager_next_q = tcp; 18379 } else { 18380 listener->tcp_eager_next_q = tcp; 18381 } 18382 listener->tcp_eager_last_q = tcp; 18383 tcp->tcp_eager_next_q = NULL; 18384 18385 /* Need to get inside the listener perimeter */ 18386 CONN_INC_REF(listener->tcp_connp); 18387 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18388 tcp_send_pending, listener->tcp_connp, 18389 SQTAG_TCP_SEND_PENDING); 18390 } 18391 no_more_eagers: 18392 tcp_eager_unlink(eager); 18393 mutex_exit(&listener->tcp_eager_lock); 18394 18395 /* 18396 * At this point, the eager is detached from the listener 18397 * but we still have an extra refs on eager (apart from the 18398 * usual tcp references). The ref was placed in tcp_rput_data 18399 * before sending the conn_ind in tcp_send_conn_ind. 18400 * The ref will be dropped in tcp_accept_finish(). 18401 */ 18402 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18403 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18404 return; 18405 default: 18406 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18407 if (mp != NULL) 18408 putnext(rq, mp); 18409 return; 18410 } 18411 } 18412 18413 void 18414 tcp_wput(queue_t *q, mblk_t *mp) 18415 { 18416 conn_t *connp = Q_TO_CONN(q); 18417 tcp_t *tcp; 18418 void (*output_proc)(); 18419 t_scalar_t type; 18420 uchar_t *rptr; 18421 struct iocblk *iocp; 18422 uint32_t msize; 18423 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18424 18425 ASSERT(connp->conn_ref >= 2); 18426 18427 switch (DB_TYPE(mp)) { 18428 case M_DATA: 18429 tcp = connp->conn_tcp; 18430 ASSERT(tcp != NULL); 18431 18432 msize = msgdsize(mp); 18433 18434 mutex_enter(&tcp->tcp_non_sq_lock); 18435 tcp->tcp_squeue_bytes += msize; 18436 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18437 tcp_setqfull(tcp); 18438 } 18439 mutex_exit(&tcp->tcp_non_sq_lock); 18440 18441 CONN_INC_REF(connp); 18442 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18443 tcp_output, connp, SQTAG_TCP_OUTPUT); 18444 return; 18445 case M_PROTO: 18446 case M_PCPROTO: 18447 /* 18448 * if it is a snmp message, don't get behind the squeue 18449 */ 18450 tcp = connp->conn_tcp; 18451 rptr = mp->b_rptr; 18452 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18453 type = ((union T_primitives *)rptr)->type; 18454 } else { 18455 if (tcp->tcp_debug) { 18456 (void) strlog(TCP_MOD_ID, 0, 1, 18457 SL_ERROR|SL_TRACE, 18458 "tcp_wput_proto, dropping one..."); 18459 } 18460 freemsg(mp); 18461 return; 18462 } 18463 if (type == T_SVR4_OPTMGMT_REQ) { 18464 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18465 if (snmpcom_req(q, mp, tcp_snmp_set, tcp_snmp_get, 18466 cr)) { 18467 /* 18468 * This was a SNMP request 18469 */ 18470 return; 18471 } else { 18472 output_proc = tcp_wput_proto; 18473 } 18474 } else { 18475 output_proc = tcp_wput_proto; 18476 } 18477 break; 18478 case M_IOCTL: 18479 /* 18480 * Most ioctls can be processed right away without going via 18481 * squeues - process them right here. Those that do require 18482 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18483 * are processed by tcp_wput_ioctl(). 18484 */ 18485 iocp = (struct iocblk *)mp->b_rptr; 18486 tcp = connp->conn_tcp; 18487 18488 switch (iocp->ioc_cmd) { 18489 case TCP_IOC_ABORT_CONN: 18490 tcp_ioctl_abort_conn(q, mp); 18491 return; 18492 case TI_GETPEERNAME: 18493 if (tcp->tcp_state < TCPS_SYN_RCVD) { 18494 iocp->ioc_error = ENOTCONN; 18495 iocp->ioc_count = 0; 18496 mp->b_datap->db_type = M_IOCACK; 18497 qreply(q, mp); 18498 return; 18499 } 18500 /* FALLTHRU */ 18501 case TI_GETMYNAME: 18502 mi_copyin(q, mp, NULL, 18503 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18504 return; 18505 case ND_SET: 18506 /* nd_getset does the necessary checks */ 18507 case ND_GET: 18508 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18509 CALL_IP_WPUT(connp, q, mp); 18510 return; 18511 } 18512 qreply(q, mp); 18513 return; 18514 case TCP_IOC_DEFAULT_Q: 18515 /* 18516 * Wants to be the default wq. Check the credentials 18517 * first, the rest is executed via squeue. 18518 */ 18519 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18520 iocp->ioc_error = EPERM; 18521 iocp->ioc_count = 0; 18522 mp->b_datap->db_type = M_IOCACK; 18523 qreply(q, mp); 18524 return; 18525 } 18526 output_proc = tcp_wput_ioctl; 18527 break; 18528 default: 18529 output_proc = tcp_wput_ioctl; 18530 break; 18531 } 18532 break; 18533 default: 18534 output_proc = tcp_wput_nondata; 18535 break; 18536 } 18537 18538 CONN_INC_REF(connp); 18539 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18540 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18541 } 18542 18543 /* 18544 * Initial STREAMS write side put() procedure for sockets. It tries to 18545 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18546 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18547 * are handled by tcp_wput() as usual. 18548 * 18549 * All further messages will also be handled by tcp_wput() because we cannot 18550 * be sure that the above short cut is safe later. 18551 */ 18552 static void 18553 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18554 { 18555 conn_t *connp = Q_TO_CONN(wq); 18556 tcp_t *tcp = connp->conn_tcp; 18557 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18558 18559 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18560 wq->q_qinfo = &tcp_winit; 18561 18562 ASSERT(IPCL_IS_TCP(connp)); 18563 ASSERT(TCP_IS_SOCKET(tcp)); 18564 18565 if (DB_TYPE(mp) == M_PCPROTO && 18566 MBLKL(mp) == sizeof (struct T_capability_req) && 18567 car->PRIM_type == T_CAPABILITY_REQ) { 18568 tcp_capability_req(tcp, mp); 18569 return; 18570 } 18571 18572 tcp_wput(wq, mp); 18573 } 18574 18575 static boolean_t 18576 tcp_zcopy_check(tcp_t *tcp) 18577 { 18578 conn_t *connp = tcp->tcp_connp; 18579 ire_t *ire; 18580 boolean_t zc_enabled = B_FALSE; 18581 tcp_stack_t *tcps = tcp->tcp_tcps; 18582 18583 if (do_tcpzcopy == 2) 18584 zc_enabled = B_TRUE; 18585 else if (tcp->tcp_ipversion == IPV4_VERSION && 18586 IPCL_IS_CONNECTED(connp) && 18587 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18588 connp->conn_dontroute == 0 && 18589 !connp->conn_nexthop_set && 18590 connp->conn_xmit_if_ill == NULL && 18591 connp->conn_nofailover_ill == NULL && 18592 do_tcpzcopy == 1) { 18593 /* 18594 * the checks above closely resemble the fast path checks 18595 * in tcp_send_data(). 18596 */ 18597 mutex_enter(&connp->conn_lock); 18598 ire = connp->conn_ire_cache; 18599 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18600 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18601 IRE_REFHOLD(ire); 18602 if (ire->ire_stq != NULL) { 18603 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18604 18605 zc_enabled = ill && (ill->ill_capabilities & 18606 ILL_CAPAB_ZEROCOPY) && 18607 (ill->ill_zerocopy_capab-> 18608 ill_zerocopy_flags != 0); 18609 } 18610 IRE_REFRELE(ire); 18611 } 18612 mutex_exit(&connp->conn_lock); 18613 } 18614 tcp->tcp_snd_zcopy_on = zc_enabled; 18615 if (!TCP_IS_DETACHED(tcp)) { 18616 if (zc_enabled) { 18617 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 18618 TCP_STAT(tcps, tcp_zcopy_on); 18619 } else { 18620 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18621 TCP_STAT(tcps, tcp_zcopy_off); 18622 } 18623 } 18624 return (zc_enabled); 18625 } 18626 18627 static mblk_t * 18628 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18629 { 18630 tcp_stack_t *tcps = tcp->tcp_tcps; 18631 18632 if (do_tcpzcopy == 2) 18633 return (bp); 18634 else if (tcp->tcp_snd_zcopy_on) { 18635 tcp->tcp_snd_zcopy_on = B_FALSE; 18636 if (!TCP_IS_DETACHED(tcp)) { 18637 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18638 TCP_STAT(tcps, tcp_zcopy_disable); 18639 } 18640 } 18641 return (tcp_zcopy_backoff(tcp, bp, 0)); 18642 } 18643 18644 /* 18645 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18646 * the original desballoca'ed segmapped mblk. 18647 */ 18648 static mblk_t * 18649 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18650 { 18651 mblk_t *head, *tail, *nbp; 18652 tcp_stack_t *tcps = tcp->tcp_tcps; 18653 18654 if (IS_VMLOANED_MBLK(bp)) { 18655 TCP_STAT(tcps, tcp_zcopy_backoff); 18656 if ((head = copyb(bp)) == NULL) { 18657 /* fail to backoff; leave it for the next backoff */ 18658 tcp->tcp_xmit_zc_clean = B_FALSE; 18659 return (bp); 18660 } 18661 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18662 if (fix_xmitlist) 18663 tcp_zcopy_notify(tcp); 18664 else 18665 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18666 } 18667 nbp = bp->b_cont; 18668 if (fix_xmitlist) { 18669 head->b_prev = bp->b_prev; 18670 head->b_next = bp->b_next; 18671 if (tcp->tcp_xmit_tail == bp) 18672 tcp->tcp_xmit_tail = head; 18673 } 18674 bp->b_next = NULL; 18675 bp->b_prev = NULL; 18676 freeb(bp); 18677 } else { 18678 head = bp; 18679 nbp = bp->b_cont; 18680 } 18681 tail = head; 18682 while (nbp) { 18683 if (IS_VMLOANED_MBLK(nbp)) { 18684 TCP_STAT(tcps, tcp_zcopy_backoff); 18685 if ((tail->b_cont = copyb(nbp)) == NULL) { 18686 tcp->tcp_xmit_zc_clean = B_FALSE; 18687 tail->b_cont = nbp; 18688 return (head); 18689 } 18690 tail = tail->b_cont; 18691 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18692 if (fix_xmitlist) 18693 tcp_zcopy_notify(tcp); 18694 else 18695 tail->b_datap->db_struioflag |= 18696 STRUIO_ZCNOTIFY; 18697 } 18698 bp = nbp; 18699 nbp = nbp->b_cont; 18700 if (fix_xmitlist) { 18701 tail->b_prev = bp->b_prev; 18702 tail->b_next = bp->b_next; 18703 if (tcp->tcp_xmit_tail == bp) 18704 tcp->tcp_xmit_tail = tail; 18705 } 18706 bp->b_next = NULL; 18707 bp->b_prev = NULL; 18708 freeb(bp); 18709 } else { 18710 tail->b_cont = nbp; 18711 tail = nbp; 18712 nbp = nbp->b_cont; 18713 } 18714 } 18715 if (fix_xmitlist) { 18716 tcp->tcp_xmit_last = tail; 18717 tcp->tcp_xmit_zc_clean = B_TRUE; 18718 } 18719 return (head); 18720 } 18721 18722 static void 18723 tcp_zcopy_notify(tcp_t *tcp) 18724 { 18725 struct stdata *stp; 18726 18727 if (tcp->tcp_detached) 18728 return; 18729 stp = STREAM(tcp->tcp_rq); 18730 mutex_enter(&stp->sd_lock); 18731 stp->sd_flag |= STZCNOTIFY; 18732 cv_broadcast(&stp->sd_zcopy_wait); 18733 mutex_exit(&stp->sd_lock); 18734 } 18735 18736 static boolean_t 18737 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18738 { 18739 ire_t *ire; 18740 conn_t *connp = tcp->tcp_connp; 18741 tcp_stack_t *tcps = tcp->tcp_tcps; 18742 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18743 18744 mutex_enter(&connp->conn_lock); 18745 ire = connp->conn_ire_cache; 18746 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18747 18748 if ((ire != NULL) && 18749 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18750 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18751 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18752 IRE_REFHOLD(ire); 18753 mutex_exit(&connp->conn_lock); 18754 } else { 18755 boolean_t cached = B_FALSE; 18756 ts_label_t *tsl; 18757 18758 /* force a recheck later on */ 18759 tcp->tcp_ire_ill_check_done = B_FALSE; 18760 18761 TCP_DBGSTAT(tcps, tcp_ire_null1); 18762 connp->conn_ire_cache = NULL; 18763 mutex_exit(&connp->conn_lock); 18764 18765 if (ire != NULL) 18766 IRE_REFRELE_NOTR(ire); 18767 18768 tsl = crgetlabel(CONN_CRED(connp)); 18769 ire = (dst ? 18770 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18771 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18772 connp->conn_zoneid, tsl, ipst)); 18773 18774 if (ire == NULL) { 18775 TCP_STAT(tcps, tcp_ire_null); 18776 return (B_FALSE); 18777 } 18778 18779 IRE_REFHOLD_NOTR(ire); 18780 /* 18781 * Since we are inside the squeue, there cannot be another 18782 * thread in TCP trying to set the conn_ire_cache now. The 18783 * check for IRE_MARK_CONDEMNED ensures that an interface 18784 * unplumb thread has not yet started cleaning up the conns. 18785 * Hence we don't need to grab the conn lock. 18786 */ 18787 if (CONN_CACHE_IRE(connp)) { 18788 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18789 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18790 connp->conn_ire_cache = ire; 18791 cached = B_TRUE; 18792 } 18793 rw_exit(&ire->ire_bucket->irb_lock); 18794 } 18795 18796 /* 18797 * We can continue to use the ire but since it was 18798 * not cached, we should drop the extra reference. 18799 */ 18800 if (!cached) 18801 IRE_REFRELE_NOTR(ire); 18802 18803 /* 18804 * Rampart note: no need to select a new label here, since 18805 * labels are not allowed to change during the life of a TCP 18806 * connection. 18807 */ 18808 } 18809 18810 *irep = ire; 18811 18812 return (B_TRUE); 18813 } 18814 18815 /* 18816 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18817 * 18818 * 0 = success; 18819 * 1 = failed to find ire and ill. 18820 */ 18821 static boolean_t 18822 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18823 { 18824 ipha_t *ipha; 18825 ipaddr_t dst; 18826 ire_t *ire; 18827 ill_t *ill; 18828 conn_t *connp = tcp->tcp_connp; 18829 mblk_t *ire_fp_mp; 18830 tcp_stack_t *tcps = tcp->tcp_tcps; 18831 18832 if (mp != NULL) 18833 ipha = (ipha_t *)mp->b_rptr; 18834 else 18835 ipha = tcp->tcp_ipha; 18836 dst = ipha->ipha_dst; 18837 18838 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18839 return (B_FALSE); 18840 18841 if ((ire->ire_flags & RTF_MULTIRT) || 18842 (ire->ire_stq == NULL) || 18843 (ire->ire_nce == NULL) || 18844 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18845 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18846 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18847 TCP_STAT(tcps, tcp_ip_ire_send); 18848 IRE_REFRELE(ire); 18849 return (B_FALSE); 18850 } 18851 18852 ill = ire_to_ill(ire); 18853 if (connp->conn_outgoing_ill != NULL) { 18854 ill_t *conn_outgoing_ill = NULL; 18855 /* 18856 * Choose a good ill in the group to send the packets on. 18857 */ 18858 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18859 ill = ire_to_ill(ire); 18860 } 18861 ASSERT(ill != NULL); 18862 18863 if (!tcp->tcp_ire_ill_check_done) { 18864 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18865 tcp->tcp_ire_ill_check_done = B_TRUE; 18866 } 18867 18868 *irep = ire; 18869 *illp = ill; 18870 18871 return (B_TRUE); 18872 } 18873 18874 static void 18875 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18876 { 18877 ipha_t *ipha; 18878 ipaddr_t src; 18879 ipaddr_t dst; 18880 uint32_t cksum; 18881 ire_t *ire; 18882 uint16_t *up; 18883 ill_t *ill; 18884 conn_t *connp = tcp->tcp_connp; 18885 uint32_t hcksum_txflags = 0; 18886 mblk_t *ire_fp_mp; 18887 uint_t ire_fp_mp_len; 18888 tcp_stack_t *tcps = tcp->tcp_tcps; 18889 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18890 18891 ASSERT(DB_TYPE(mp) == M_DATA); 18892 18893 if (DB_CRED(mp) == NULL) 18894 mblk_setcred(mp, CONN_CRED(connp)); 18895 18896 ipha = (ipha_t *)mp->b_rptr; 18897 src = ipha->ipha_src; 18898 dst = ipha->ipha_dst; 18899 18900 /* 18901 * Drop off fast path for IPv6 and also if options are present or 18902 * we need to resolve a TS label. 18903 */ 18904 if (tcp->tcp_ipversion != IPV4_VERSION || 18905 !IPCL_IS_CONNECTED(connp) || 18906 !CONN_IS_LSO_MD_FASTPATH(connp) || 18907 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18908 !connp->conn_ulp_labeled || 18909 ipha->ipha_ident == IP_HDR_INCLUDED || 18910 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18911 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18912 if (tcp->tcp_snd_zcopy_aware) 18913 mp = tcp_zcopy_disable(tcp, mp); 18914 TCP_STAT(tcps, tcp_ip_send); 18915 CALL_IP_WPUT(connp, q, mp); 18916 return; 18917 } 18918 18919 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18920 if (tcp->tcp_snd_zcopy_aware) 18921 mp = tcp_zcopy_backoff(tcp, mp, 0); 18922 CALL_IP_WPUT(connp, q, mp); 18923 return; 18924 } 18925 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18926 ire_fp_mp_len = MBLKL(ire_fp_mp); 18927 18928 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18929 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18930 #ifndef _BIG_ENDIAN 18931 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18932 #endif 18933 18934 /* 18935 * Check to see if we need to re-enable LSO/MDT for this connection 18936 * because it was previously disabled due to changes in the ill; 18937 * note that by doing it here, this re-enabling only applies when 18938 * the packet is not dispatched through CALL_IP_WPUT(). 18939 * 18940 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18941 * case, since that's how we ended up here. For IPv6, we do the 18942 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18943 */ 18944 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18945 /* 18946 * Restore LSO for this connection, so that next time around 18947 * it is eligible to go through tcp_lsosend() path again. 18948 */ 18949 TCP_STAT(tcps, tcp_lso_enabled); 18950 tcp->tcp_lso = B_TRUE; 18951 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18952 "interface %s\n", (void *)connp, ill->ill_name)); 18953 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18954 /* 18955 * Restore MDT for this connection, so that next time around 18956 * it is eligible to go through tcp_multisend() path again. 18957 */ 18958 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18959 tcp->tcp_mdt = B_TRUE; 18960 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18961 "interface %s\n", (void *)connp, ill->ill_name)); 18962 } 18963 18964 if (tcp->tcp_snd_zcopy_aware) { 18965 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18966 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18967 mp = tcp_zcopy_disable(tcp, mp); 18968 /* 18969 * we shouldn't need to reset ipha as the mp containing 18970 * ipha should never be a zero-copy mp. 18971 */ 18972 } 18973 18974 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18975 ASSERT(ill->ill_hcksum_capab != NULL); 18976 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18977 } 18978 18979 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18980 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18981 18982 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18983 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18984 18985 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18986 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18987 18988 /* Software checksum? */ 18989 if (DB_CKSUMFLAGS(mp) == 0) { 18990 TCP_STAT(tcps, tcp_out_sw_cksum); 18991 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18992 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18993 } 18994 18995 ipha->ipha_fragment_offset_and_flags |= 18996 (uint32_t)htons(ire->ire_frag_flag); 18997 18998 /* Calculate IP header checksum if hardware isn't capable */ 18999 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19000 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19001 ((uint16_t *)ipha)[4]); 19002 } 19003 19004 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19005 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19006 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19007 19008 UPDATE_OB_PKT_COUNT(ire); 19009 ire->ire_last_used_time = lbolt; 19010 19011 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19012 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19013 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19014 ntohs(ipha->ipha_length)); 19015 19016 if (ILL_DLS_CAPABLE(ill)) { 19017 /* 19018 * Send the packet directly to DLD, where it may be queued 19019 * depending on the availability of transmit resources at 19020 * the media layer. 19021 */ 19022 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 19023 } else { 19024 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 19025 DTRACE_PROBE4(ip4__physical__out__start, 19026 ill_t *, NULL, ill_t *, out_ill, 19027 ipha_t *, ipha, mblk_t *, mp); 19028 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19029 ipst->ips_ipv4firewall_physical_out, 19030 NULL, out_ill, ipha, mp, mp, ipst); 19031 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19032 if (mp != NULL) 19033 putnext(ire->ire_stq, mp); 19034 } 19035 IRE_REFRELE(ire); 19036 } 19037 19038 /* 19039 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19040 * if the receiver shrinks the window, i.e. moves the right window to the 19041 * left, the we should not send new data, but should retransmit normally the 19042 * old unacked data between suna and suna + swnd. We might has sent data 19043 * that is now outside the new window, pretend that we didn't send it. 19044 */ 19045 static void 19046 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19047 { 19048 uint32_t snxt = tcp->tcp_snxt; 19049 mblk_t *xmit_tail; 19050 int32_t offset; 19051 19052 ASSERT(shrunk_count > 0); 19053 19054 /* Pretend we didn't send the data outside the window */ 19055 snxt -= shrunk_count; 19056 19057 /* Get the mblk and the offset in it per the shrunk window */ 19058 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19059 19060 ASSERT(xmit_tail != NULL); 19061 19062 /* Reset all the values per the now shrunk window */ 19063 tcp->tcp_snxt = snxt; 19064 tcp->tcp_xmit_tail = xmit_tail; 19065 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19066 offset; 19067 tcp->tcp_unsent += shrunk_count; 19068 19069 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19070 /* 19071 * Make sure the timer is running so that we will probe a zero 19072 * window. 19073 */ 19074 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19075 } 19076 19077 19078 /* 19079 * The TCP normal data output path. 19080 * NOTE: the logic of the fast path is duplicated from this function. 19081 */ 19082 static void 19083 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19084 { 19085 int len; 19086 mblk_t *local_time; 19087 mblk_t *mp1; 19088 uint32_t snxt; 19089 int tail_unsent; 19090 int tcpstate; 19091 int usable = 0; 19092 mblk_t *xmit_tail; 19093 queue_t *q = tcp->tcp_wq; 19094 int32_t mss; 19095 int32_t num_sack_blk = 0; 19096 int32_t tcp_hdr_len; 19097 int32_t tcp_tcp_hdr_len; 19098 int mdt_thres; 19099 int rc; 19100 tcp_stack_t *tcps = tcp->tcp_tcps; 19101 ip_stack_t *ipst; 19102 19103 tcpstate = tcp->tcp_state; 19104 if (mp == NULL) { 19105 /* 19106 * tcp_wput_data() with NULL mp should only be called when 19107 * there is unsent data. 19108 */ 19109 ASSERT(tcp->tcp_unsent > 0); 19110 /* Really tacky... but we need this for detached closes. */ 19111 len = tcp->tcp_unsent; 19112 goto data_null; 19113 } 19114 19115 #if CCS_STATS 19116 wrw_stats.tot.count++; 19117 wrw_stats.tot.bytes += msgdsize(mp); 19118 #endif 19119 ASSERT(mp->b_datap->db_type == M_DATA); 19120 /* 19121 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19122 * or before a connection attempt has begun. 19123 */ 19124 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19125 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19126 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19127 #ifdef DEBUG 19128 cmn_err(CE_WARN, 19129 "tcp_wput_data: data after ordrel, %s", 19130 tcp_display(tcp, NULL, 19131 DISP_ADDR_AND_PORT)); 19132 #else 19133 if (tcp->tcp_debug) { 19134 (void) strlog(TCP_MOD_ID, 0, 1, 19135 SL_TRACE|SL_ERROR, 19136 "tcp_wput_data: data after ordrel, %s\n", 19137 tcp_display(tcp, NULL, 19138 DISP_ADDR_AND_PORT)); 19139 } 19140 #endif /* DEBUG */ 19141 } 19142 if (tcp->tcp_snd_zcopy_aware && 19143 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19144 tcp_zcopy_notify(tcp); 19145 freemsg(mp); 19146 mutex_enter(&tcp->tcp_non_sq_lock); 19147 if (tcp->tcp_flow_stopped && 19148 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19149 tcp_clrqfull(tcp); 19150 } 19151 mutex_exit(&tcp->tcp_non_sq_lock); 19152 return; 19153 } 19154 19155 /* Strip empties */ 19156 for (;;) { 19157 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19158 (uintptr_t)INT_MAX); 19159 len = (int)(mp->b_wptr - mp->b_rptr); 19160 if (len > 0) 19161 break; 19162 mp1 = mp; 19163 mp = mp->b_cont; 19164 freeb(mp1); 19165 if (!mp) { 19166 return; 19167 } 19168 } 19169 19170 /* If we are the first on the list ... */ 19171 if (tcp->tcp_xmit_head == NULL) { 19172 tcp->tcp_xmit_head = mp; 19173 tcp->tcp_xmit_tail = mp; 19174 tcp->tcp_xmit_tail_unsent = len; 19175 } else { 19176 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19177 struct datab *dp; 19178 19179 mp1 = tcp->tcp_xmit_last; 19180 if (len < tcp_tx_pull_len && 19181 (dp = mp1->b_datap)->db_ref == 1 && 19182 dp->db_lim - mp1->b_wptr >= len) { 19183 ASSERT(len > 0); 19184 ASSERT(!mp1->b_cont); 19185 if (len == 1) { 19186 *mp1->b_wptr++ = *mp->b_rptr; 19187 } else { 19188 bcopy(mp->b_rptr, mp1->b_wptr, len); 19189 mp1->b_wptr += len; 19190 } 19191 if (mp1 == tcp->tcp_xmit_tail) 19192 tcp->tcp_xmit_tail_unsent += len; 19193 mp1->b_cont = mp->b_cont; 19194 if (tcp->tcp_snd_zcopy_aware && 19195 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19196 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19197 freeb(mp); 19198 mp = mp1; 19199 } else { 19200 tcp->tcp_xmit_last->b_cont = mp; 19201 } 19202 len += tcp->tcp_unsent; 19203 } 19204 19205 /* Tack on however many more positive length mblks we have */ 19206 if ((mp1 = mp->b_cont) != NULL) { 19207 do { 19208 int tlen; 19209 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19210 (uintptr_t)INT_MAX); 19211 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19212 if (tlen <= 0) { 19213 mp->b_cont = mp1->b_cont; 19214 freeb(mp1); 19215 } else { 19216 len += tlen; 19217 mp = mp1; 19218 } 19219 } while ((mp1 = mp->b_cont) != NULL); 19220 } 19221 tcp->tcp_xmit_last = mp; 19222 tcp->tcp_unsent = len; 19223 19224 if (urgent) 19225 usable = 1; 19226 19227 data_null: 19228 snxt = tcp->tcp_snxt; 19229 xmit_tail = tcp->tcp_xmit_tail; 19230 tail_unsent = tcp->tcp_xmit_tail_unsent; 19231 19232 /* 19233 * Note that tcp_mss has been adjusted to take into account the 19234 * timestamp option if applicable. Because SACK options do not 19235 * appear in every TCP segments and they are of variable lengths, 19236 * they cannot be included in tcp_mss. Thus we need to calculate 19237 * the actual segment length when we need to send a segment which 19238 * includes SACK options. 19239 */ 19240 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19241 int32_t opt_len; 19242 19243 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19244 tcp->tcp_num_sack_blk); 19245 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19246 2 + TCPOPT_HEADER_LEN; 19247 mss = tcp->tcp_mss - opt_len; 19248 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19249 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19250 } else { 19251 mss = tcp->tcp_mss; 19252 tcp_hdr_len = tcp->tcp_hdr_len; 19253 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19254 } 19255 19256 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19257 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19258 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19259 } 19260 if (tcpstate == TCPS_SYN_RCVD) { 19261 /* 19262 * The three-way connection establishment handshake is not 19263 * complete yet. We want to queue the data for transmission 19264 * after entering ESTABLISHED state (RFC793). A jump to 19265 * "done" label effectively leaves data on the queue. 19266 */ 19267 goto done; 19268 } else { 19269 int usable_r; 19270 19271 /* 19272 * In the special case when cwnd is zero, which can only 19273 * happen if the connection is ECN capable, return now. 19274 * New segments is sent using tcp_timer(). The timer 19275 * is set in tcp_rput_data(). 19276 */ 19277 if (tcp->tcp_cwnd == 0) { 19278 /* 19279 * Note that tcp_cwnd is 0 before 3-way handshake is 19280 * finished. 19281 */ 19282 ASSERT(tcp->tcp_ecn_ok || 19283 tcp->tcp_state < TCPS_ESTABLISHED); 19284 return; 19285 } 19286 19287 /* NOTE: trouble if xmitting while SYN not acked? */ 19288 usable_r = snxt - tcp->tcp_suna; 19289 usable_r = tcp->tcp_swnd - usable_r; 19290 19291 /* 19292 * Check if the receiver has shrunk the window. If 19293 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19294 * cannot be set as there is unsent data, so FIN cannot 19295 * be sent out. Otherwise, we need to take into account 19296 * of FIN as it consumes an "invisible" sequence number. 19297 */ 19298 ASSERT(tcp->tcp_fin_sent == 0); 19299 if (usable_r < 0) { 19300 /* 19301 * The receiver has shrunk the window and we have sent 19302 * -usable_r date beyond the window, re-adjust. 19303 * 19304 * If TCP window scaling is enabled, there can be 19305 * round down error as the advertised receive window 19306 * is actually right shifted n bits. This means that 19307 * the lower n bits info is wiped out. It will look 19308 * like the window is shrunk. Do a check here to 19309 * see if the shrunk amount is actually within the 19310 * error in window calculation. If it is, just 19311 * return. Note that this check is inside the 19312 * shrunk window check. This makes sure that even 19313 * though tcp_process_shrunk_swnd() is not called, 19314 * we will stop further processing. 19315 */ 19316 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19317 tcp_process_shrunk_swnd(tcp, -usable_r); 19318 } 19319 return; 19320 } 19321 19322 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19323 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19324 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19325 19326 /* usable = MIN(usable, unsent) */ 19327 if (usable_r > len) 19328 usable_r = len; 19329 19330 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19331 if (usable_r > 0) { 19332 usable = usable_r; 19333 } else { 19334 /* Bypass all other unnecessary processing. */ 19335 goto done; 19336 } 19337 } 19338 19339 local_time = (mblk_t *)lbolt; 19340 19341 /* 19342 * "Our" Nagle Algorithm. This is not the same as in the old 19343 * BSD. This is more in line with the true intent of Nagle. 19344 * 19345 * The conditions are: 19346 * 1. The amount of unsent data (or amount of data which can be 19347 * sent, whichever is smaller) is less than Nagle limit. 19348 * 2. The last sent size is also less than Nagle limit. 19349 * 3. There is unack'ed data. 19350 * 4. Urgent pointer is not set. Send urgent data ignoring the 19351 * Nagle algorithm. This reduces the probability that urgent 19352 * bytes get "merged" together. 19353 * 5. The app has not closed the connection. This eliminates the 19354 * wait time of the receiving side waiting for the last piece of 19355 * (small) data. 19356 * 19357 * If all are satisified, exit without sending anything. Note 19358 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19359 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19360 * 4095). 19361 */ 19362 if (usable < (int)tcp->tcp_naglim && 19363 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19364 snxt != tcp->tcp_suna && 19365 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19366 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19367 goto done; 19368 } 19369 19370 if (tcp->tcp_cork) { 19371 /* 19372 * if the tcp->tcp_cork option is set, then we have to force 19373 * TCP not to send partial segment (smaller than MSS bytes). 19374 * We are calculating the usable now based on full mss and 19375 * will save the rest of remaining data for later. 19376 */ 19377 if (usable < mss) 19378 goto done; 19379 usable = (usable / mss) * mss; 19380 } 19381 19382 /* Update the latest receive window size in TCP header. */ 19383 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19384 tcp->tcp_tcph->th_win); 19385 19386 /* 19387 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19388 * 19389 * 1. Simple TCP/IP{v4,v6} (no options). 19390 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19391 * 3. If the TCP connection is in ESTABLISHED state. 19392 * 4. The TCP is not detached. 19393 * 19394 * If any of the above conditions have changed during the 19395 * connection, stop using LSO/MDT and restore the stream head 19396 * parameters accordingly. 19397 */ 19398 ipst = tcps->tcps_netstack->netstack_ip; 19399 19400 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19401 ((tcp->tcp_ipversion == IPV4_VERSION && 19402 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19403 (tcp->tcp_ipversion == IPV6_VERSION && 19404 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19405 tcp->tcp_state != TCPS_ESTABLISHED || 19406 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19407 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19408 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19409 if (tcp->tcp_lso) { 19410 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19411 tcp->tcp_lso = B_FALSE; 19412 } else { 19413 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19414 tcp->tcp_mdt = B_FALSE; 19415 } 19416 19417 /* Anything other than detached is considered pathological */ 19418 if (!TCP_IS_DETACHED(tcp)) { 19419 if (tcp->tcp_lso) 19420 TCP_STAT(tcps, tcp_lso_disabled); 19421 else 19422 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19423 (void) tcp_maxpsz_set(tcp, B_TRUE); 19424 } 19425 } 19426 19427 /* Use MDT if sendable amount is greater than the threshold */ 19428 if (tcp->tcp_mdt && 19429 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19430 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19431 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19432 (tcp->tcp_valid_bits == 0 || 19433 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19434 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19435 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19436 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19437 local_time, mdt_thres); 19438 } else { 19439 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19440 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19441 local_time, INT_MAX); 19442 } 19443 19444 /* Pretend that all we were trying to send really got sent */ 19445 if (rc < 0 && tail_unsent < 0) { 19446 do { 19447 xmit_tail = xmit_tail->b_cont; 19448 xmit_tail->b_prev = local_time; 19449 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19450 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19451 tail_unsent += (int)(xmit_tail->b_wptr - 19452 xmit_tail->b_rptr); 19453 } while (tail_unsent < 0); 19454 } 19455 done:; 19456 tcp->tcp_xmit_tail = xmit_tail; 19457 tcp->tcp_xmit_tail_unsent = tail_unsent; 19458 len = tcp->tcp_snxt - snxt; 19459 if (len) { 19460 /* 19461 * If new data was sent, need to update the notsack 19462 * list, which is, afterall, data blocks that have 19463 * not been sack'ed by the receiver. New data is 19464 * not sack'ed. 19465 */ 19466 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19467 /* len is a negative value. */ 19468 tcp->tcp_pipe -= len; 19469 tcp_notsack_update(&(tcp->tcp_notsack_list), 19470 tcp->tcp_snxt, snxt, 19471 &(tcp->tcp_num_notsack_blk), 19472 &(tcp->tcp_cnt_notsack_list)); 19473 } 19474 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19475 tcp->tcp_rack = tcp->tcp_rnxt; 19476 tcp->tcp_rack_cnt = 0; 19477 if ((snxt + len) == tcp->tcp_suna) { 19478 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19479 } 19480 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19481 /* 19482 * Didn't send anything. Make sure the timer is running 19483 * so that we will probe a zero window. 19484 */ 19485 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19486 } 19487 /* Note that len is the amount we just sent but with a negative sign */ 19488 tcp->tcp_unsent += len; 19489 mutex_enter(&tcp->tcp_non_sq_lock); 19490 if (tcp->tcp_flow_stopped) { 19491 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19492 tcp_clrqfull(tcp); 19493 } 19494 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19495 tcp_setqfull(tcp); 19496 } 19497 mutex_exit(&tcp->tcp_non_sq_lock); 19498 } 19499 19500 /* 19501 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19502 * outgoing TCP header with the template header, as well as other 19503 * options such as time-stamp, ECN and/or SACK. 19504 */ 19505 static void 19506 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19507 { 19508 tcph_t *tcp_tmpl, *tcp_h; 19509 uint32_t *dst, *src; 19510 int hdrlen; 19511 19512 ASSERT(OK_32PTR(rptr)); 19513 19514 /* Template header */ 19515 tcp_tmpl = tcp->tcp_tcph; 19516 19517 /* Header of outgoing packet */ 19518 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19519 19520 /* dst and src are opaque 32-bit fields, used for copying */ 19521 dst = (uint32_t *)rptr; 19522 src = (uint32_t *)tcp->tcp_iphc; 19523 hdrlen = tcp->tcp_hdr_len; 19524 19525 /* Fill time-stamp option if needed */ 19526 if (tcp->tcp_snd_ts_ok) { 19527 U32_TO_BE32((uint32_t)now, 19528 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19529 U32_TO_BE32(tcp->tcp_ts_recent, 19530 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19531 } else { 19532 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19533 } 19534 19535 /* 19536 * Copy the template header; is this really more efficient than 19537 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19538 * but perhaps not for other scenarios. 19539 */ 19540 dst[0] = src[0]; 19541 dst[1] = src[1]; 19542 dst[2] = src[2]; 19543 dst[3] = src[3]; 19544 dst[4] = src[4]; 19545 dst[5] = src[5]; 19546 dst[6] = src[6]; 19547 dst[7] = src[7]; 19548 dst[8] = src[8]; 19549 dst[9] = src[9]; 19550 if (hdrlen -= 40) { 19551 hdrlen >>= 2; 19552 dst += 10; 19553 src += 10; 19554 do { 19555 *dst++ = *src++; 19556 } while (--hdrlen); 19557 } 19558 19559 /* 19560 * Set the ECN info in the TCP header if it is not a zero 19561 * window probe. Zero window probe is only sent in 19562 * tcp_wput_data() and tcp_timer(). 19563 */ 19564 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19565 SET_ECT(tcp, rptr); 19566 19567 if (tcp->tcp_ecn_echo_on) 19568 tcp_h->th_flags[0] |= TH_ECE; 19569 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19570 tcp_h->th_flags[0] |= TH_CWR; 19571 tcp->tcp_ecn_cwr_sent = B_TRUE; 19572 } 19573 } 19574 19575 /* Fill in SACK options */ 19576 if (num_sack_blk > 0) { 19577 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19578 sack_blk_t *tmp; 19579 int32_t i; 19580 19581 wptr[0] = TCPOPT_NOP; 19582 wptr[1] = TCPOPT_NOP; 19583 wptr[2] = TCPOPT_SACK; 19584 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19585 sizeof (sack_blk_t); 19586 wptr += TCPOPT_REAL_SACK_LEN; 19587 19588 tmp = tcp->tcp_sack_list; 19589 for (i = 0; i < num_sack_blk; i++) { 19590 U32_TO_BE32(tmp[i].begin, wptr); 19591 wptr += sizeof (tcp_seq); 19592 U32_TO_BE32(tmp[i].end, wptr); 19593 wptr += sizeof (tcp_seq); 19594 } 19595 tcp_h->th_offset_and_rsrvd[0] += 19596 ((num_sack_blk * 2 + 1) << 4); 19597 } 19598 } 19599 19600 /* 19601 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19602 * the destination address and SAP attribute, and if necessary, the 19603 * hardware checksum offload attribute to a Multidata message. 19604 */ 19605 static int 19606 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19607 const uint32_t start, const uint32_t stuff, const uint32_t end, 19608 const uint32_t flags, tcp_stack_t *tcps) 19609 { 19610 /* Add global destination address & SAP attribute */ 19611 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19612 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19613 "destination address+SAP\n")); 19614 19615 if (dlmp != NULL) 19616 TCP_STAT(tcps, tcp_mdt_allocfail); 19617 return (-1); 19618 } 19619 19620 /* Add global hwcksum attribute */ 19621 if (hwcksum && 19622 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19623 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19624 "checksum attribute\n")); 19625 19626 TCP_STAT(tcps, tcp_mdt_allocfail); 19627 return (-1); 19628 } 19629 19630 return (0); 19631 } 19632 19633 /* 19634 * Smaller and private version of pdescinfo_t used specifically for TCP, 19635 * which allows for only two payload spans per packet. 19636 */ 19637 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19638 19639 /* 19640 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19641 * scheme, and returns one the following: 19642 * 19643 * -1 = failed allocation. 19644 * 0 = success; burst count reached, or usable send window is too small, 19645 * and that we'd rather wait until later before sending again. 19646 */ 19647 static int 19648 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19649 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19650 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19651 const int mdt_thres) 19652 { 19653 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19654 multidata_t *mmd; 19655 uint_t obsegs, obbytes, hdr_frag_sz; 19656 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19657 int num_burst_seg, max_pld; 19658 pdesc_t *pkt; 19659 tcp_pdescinfo_t tcp_pkt_info; 19660 pdescinfo_t *pkt_info; 19661 int pbuf_idx, pbuf_idx_nxt; 19662 int seg_len, len, spill, af; 19663 boolean_t add_buffer, zcopy, clusterwide; 19664 boolean_t buf_trunked = B_FALSE; 19665 boolean_t rconfirm = B_FALSE; 19666 boolean_t done = B_FALSE; 19667 uint32_t cksum; 19668 uint32_t hwcksum_flags; 19669 ire_t *ire = NULL; 19670 ill_t *ill; 19671 ipha_t *ipha; 19672 ip6_t *ip6h; 19673 ipaddr_t src, dst; 19674 ill_zerocopy_capab_t *zc_cap = NULL; 19675 uint16_t *up; 19676 int err; 19677 conn_t *connp; 19678 mblk_t *mp, *mp1, *fw_mp_head = NULL; 19679 uchar_t *pld_start; 19680 tcp_stack_t *tcps = tcp->tcp_tcps; 19681 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19682 19683 #ifdef _BIG_ENDIAN 19684 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19685 #else 19686 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19687 #endif 19688 19689 #define PREP_NEW_MULTIDATA() { \ 19690 mmd = NULL; \ 19691 md_mp = md_hbuf = NULL; \ 19692 cur_hdr_off = 0; \ 19693 max_pld = tcp->tcp_mdt_max_pld; \ 19694 pbuf_idx = pbuf_idx_nxt = -1; \ 19695 add_buffer = B_TRUE; \ 19696 zcopy = B_FALSE; \ 19697 } 19698 19699 #define PREP_NEW_PBUF() { \ 19700 md_pbuf = md_pbuf_nxt = NULL; \ 19701 pbuf_idx = pbuf_idx_nxt = -1; \ 19702 cur_pld_off = 0; \ 19703 first_snxt = *snxt; \ 19704 ASSERT(*tail_unsent > 0); \ 19705 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19706 } 19707 19708 ASSERT(mdt_thres >= mss); 19709 ASSERT(*usable > 0 && *usable > mdt_thres); 19710 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19711 ASSERT(!TCP_IS_DETACHED(tcp)); 19712 ASSERT(tcp->tcp_valid_bits == 0 || 19713 tcp->tcp_valid_bits == TCP_FSS_VALID); 19714 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19715 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19716 (tcp->tcp_ipversion == IPV6_VERSION && 19717 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19718 19719 connp = tcp->tcp_connp; 19720 ASSERT(connp != NULL); 19721 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19722 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19723 19724 /* 19725 * Note that tcp will only declare at most 2 payload spans per 19726 * packet, which is much lower than the maximum allowable number 19727 * of packet spans per Multidata. For this reason, we use the 19728 * privately declared and smaller descriptor info structure, in 19729 * order to save some stack space. 19730 */ 19731 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19732 19733 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19734 if (af == AF_INET) { 19735 dst = tcp->tcp_ipha->ipha_dst; 19736 src = tcp->tcp_ipha->ipha_src; 19737 ASSERT(!CLASSD(dst)); 19738 } 19739 ASSERT(af == AF_INET || 19740 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19741 19742 obsegs = obbytes = 0; 19743 num_burst_seg = tcp->tcp_snd_burst; 19744 md_mp_head = NULL; 19745 PREP_NEW_MULTIDATA(); 19746 19747 /* 19748 * Before we go on further, make sure there is an IRE that we can 19749 * use, and that the ILL supports MDT. Otherwise, there's no point 19750 * in proceeding any further, and we should just hand everything 19751 * off to the legacy path. 19752 */ 19753 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19754 goto legacy_send_no_md; 19755 19756 ASSERT(ire != NULL); 19757 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19758 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19759 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19760 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19761 /* 19762 * If we do support loopback for MDT (which requires modifications 19763 * to the receiving paths), the following assertions should go away, 19764 * and we would be sending the Multidata to loopback conn later on. 19765 */ 19766 ASSERT(!IRE_IS_LOCAL(ire)); 19767 ASSERT(ire->ire_stq != NULL); 19768 19769 ill = ire_to_ill(ire); 19770 ASSERT(ill != NULL); 19771 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19772 19773 if (!tcp->tcp_ire_ill_check_done) { 19774 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19775 tcp->tcp_ire_ill_check_done = B_TRUE; 19776 } 19777 19778 /* 19779 * If the underlying interface conditions have changed, or if the 19780 * new interface does not support MDT, go back to legacy path. 19781 */ 19782 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19783 /* don't go through this path anymore for this connection */ 19784 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19785 tcp->tcp_mdt = B_FALSE; 19786 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19787 "interface %s\n", (void *)connp, ill->ill_name)); 19788 /* IRE will be released prior to returning */ 19789 goto legacy_send_no_md; 19790 } 19791 19792 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19793 zc_cap = ill->ill_zerocopy_capab; 19794 19795 /* 19796 * Check if we can take tcp fast-path. Note that "incomplete" 19797 * ire's (where the link-layer for next hop is not resolved 19798 * or where the fast-path header in nce_fp_mp is not available 19799 * yet) are sent down the legacy (slow) path. 19800 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19801 */ 19802 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19803 /* IRE will be released prior to returning */ 19804 goto legacy_send_no_md; 19805 } 19806 19807 /* go to legacy path if interface doesn't support zerocopy */ 19808 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19809 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19810 /* IRE will be released prior to returning */ 19811 goto legacy_send_no_md; 19812 } 19813 19814 /* does the interface support hardware checksum offload? */ 19815 hwcksum_flags = 0; 19816 if (ILL_HCKSUM_CAPABLE(ill) && 19817 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19818 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19819 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19820 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19821 HCKSUM_IPHDRCKSUM) 19822 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19823 19824 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19825 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19826 hwcksum_flags |= HCK_FULLCKSUM; 19827 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19828 HCKSUM_INET_PARTIAL) 19829 hwcksum_flags |= HCK_PARTIALCKSUM; 19830 } 19831 19832 /* 19833 * Each header fragment consists of the leading extra space, 19834 * followed by the TCP/IP header, and the trailing extra space. 19835 * We make sure that each header fragment begins on a 32-bit 19836 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19837 * aligned in tcp_mdt_update). 19838 */ 19839 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19840 tcp->tcp_mdt_hdr_tail), 4); 19841 19842 /* are we starting from the beginning of data block? */ 19843 if (*tail_unsent == 0) { 19844 *xmit_tail = (*xmit_tail)->b_cont; 19845 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19846 *tail_unsent = (int)MBLKL(*xmit_tail); 19847 } 19848 19849 /* 19850 * Here we create one or more Multidata messages, each made up of 19851 * one header buffer and up to N payload buffers. This entire 19852 * operation is done within two loops: 19853 * 19854 * The outer loop mostly deals with creating the Multidata message, 19855 * as well as the header buffer that gets added to it. It also 19856 * links the Multidata messages together such that all of them can 19857 * be sent down to the lower layer in a single putnext call; this 19858 * linking behavior depends on the tcp_mdt_chain tunable. 19859 * 19860 * The inner loop takes an existing Multidata message, and adds 19861 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19862 * packetizes those buffers by filling up the corresponding header 19863 * buffer fragments with the proper IP and TCP headers, and by 19864 * describing the layout of each packet in the packet descriptors 19865 * that get added to the Multidata. 19866 */ 19867 do { 19868 /* 19869 * If usable send window is too small, or data blocks in 19870 * transmit list are smaller than our threshold (i.e. app 19871 * performs large writes followed by small ones), we hand 19872 * off the control over to the legacy path. Note that we'll 19873 * get back the control once it encounters a large block. 19874 */ 19875 if (*usable < mss || (*tail_unsent <= mdt_thres && 19876 (*xmit_tail)->b_cont != NULL && 19877 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19878 /* send down what we've got so far */ 19879 if (md_mp_head != NULL) { 19880 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19881 obsegs, obbytes, &rconfirm); 19882 } 19883 /* 19884 * Pass control over to tcp_send(), but tell it to 19885 * return to us once a large-size transmission is 19886 * possible. 19887 */ 19888 TCP_STAT(tcps, tcp_mdt_legacy_small); 19889 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19890 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19891 tail_unsent, xmit_tail, local_time, 19892 mdt_thres)) <= 0) { 19893 /* burst count reached, or alloc failed */ 19894 IRE_REFRELE(ire); 19895 return (err); 19896 } 19897 19898 /* tcp_send() may have sent everything, so check */ 19899 if (*usable <= 0) { 19900 IRE_REFRELE(ire); 19901 return (0); 19902 } 19903 19904 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19905 /* 19906 * We may have delivered the Multidata, so make sure 19907 * to re-initialize before the next round. 19908 */ 19909 md_mp_head = NULL; 19910 obsegs = obbytes = 0; 19911 num_burst_seg = tcp->tcp_snd_burst; 19912 PREP_NEW_MULTIDATA(); 19913 19914 /* are we starting from the beginning of data block? */ 19915 if (*tail_unsent == 0) { 19916 *xmit_tail = (*xmit_tail)->b_cont; 19917 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19918 (uintptr_t)INT_MAX); 19919 *tail_unsent = (int)MBLKL(*xmit_tail); 19920 } 19921 } 19922 19923 /* 19924 * max_pld limits the number of mblks in tcp's transmit 19925 * queue that can be added to a Multidata message. Once 19926 * this counter reaches zero, no more additional mblks 19927 * can be added to it. What happens afterwards depends 19928 * on whether or not we are set to chain the Multidata 19929 * messages. If we are to link them together, reset 19930 * max_pld to its original value (tcp_mdt_max_pld) and 19931 * prepare to create a new Multidata message which will 19932 * get linked to md_mp_head. Else, leave it alone and 19933 * let the inner loop break on its own. 19934 */ 19935 if (tcp_mdt_chain && max_pld == 0) 19936 PREP_NEW_MULTIDATA(); 19937 19938 /* adding a payload buffer; re-initialize values */ 19939 if (add_buffer) 19940 PREP_NEW_PBUF(); 19941 19942 /* 19943 * If we don't have a Multidata, either because we just 19944 * (re)entered this outer loop, or after we branched off 19945 * to tcp_send above, setup the Multidata and header 19946 * buffer to be used. 19947 */ 19948 if (md_mp == NULL) { 19949 int md_hbuflen; 19950 uint32_t start, stuff; 19951 19952 /* 19953 * Calculate Multidata header buffer size large enough 19954 * to hold all of the headers that can possibly be 19955 * sent at this moment. We'd rather over-estimate 19956 * the size than running out of space; this is okay 19957 * since this buffer is small anyway. 19958 */ 19959 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19960 19961 /* 19962 * Start and stuff offset for partial hardware 19963 * checksum offload; these are currently for IPv4. 19964 * For full checksum offload, they are set to zero. 19965 */ 19966 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19967 if (af == AF_INET) { 19968 start = IP_SIMPLE_HDR_LENGTH; 19969 stuff = IP_SIMPLE_HDR_LENGTH + 19970 TCP_CHECKSUM_OFFSET; 19971 } else { 19972 start = IPV6_HDR_LEN; 19973 stuff = IPV6_HDR_LEN + 19974 TCP_CHECKSUM_OFFSET; 19975 } 19976 } else { 19977 start = stuff = 0; 19978 } 19979 19980 /* 19981 * Create the header buffer, Multidata, as well as 19982 * any necessary attributes (destination address, 19983 * SAP and hardware checksum offload) that should 19984 * be associated with the Multidata message. 19985 */ 19986 ASSERT(cur_hdr_off == 0); 19987 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19988 ((md_hbuf->b_wptr += md_hbuflen), 19989 (mmd = mmd_alloc(md_hbuf, &md_mp, 19990 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19991 /* fastpath mblk */ 19992 ire->ire_nce->nce_res_mp, 19993 /* hardware checksum enabled */ 19994 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19995 /* hardware checksum offsets */ 19996 start, stuff, 0, 19997 /* hardware checksum flag */ 19998 hwcksum_flags, tcps) != 0)) { 19999 legacy_send: 20000 if (md_mp != NULL) { 20001 /* Unlink message from the chain */ 20002 if (md_mp_head != NULL) { 20003 err = (intptr_t)rmvb(md_mp_head, 20004 md_mp); 20005 /* 20006 * We can't assert that rmvb 20007 * did not return -1, since we 20008 * may get here before linkb 20009 * happens. We do, however, 20010 * check if we just removed the 20011 * only element in the list. 20012 */ 20013 if (err == 0) 20014 md_mp_head = NULL; 20015 } 20016 /* md_hbuf gets freed automatically */ 20017 TCP_STAT(tcps, tcp_mdt_discarded); 20018 freeb(md_mp); 20019 } else { 20020 /* Either allocb or mmd_alloc failed */ 20021 TCP_STAT(tcps, tcp_mdt_allocfail); 20022 if (md_hbuf != NULL) 20023 freeb(md_hbuf); 20024 } 20025 20026 /* send down what we've got so far */ 20027 if (md_mp_head != NULL) { 20028 tcp_multisend_data(tcp, ire, ill, 20029 md_mp_head, obsegs, obbytes, 20030 &rconfirm); 20031 } 20032 legacy_send_no_md: 20033 if (ire != NULL) 20034 IRE_REFRELE(ire); 20035 /* 20036 * Too bad; let the legacy path handle this. 20037 * We specify INT_MAX for the threshold, since 20038 * we gave up with the Multidata processings 20039 * and let the old path have it all. 20040 */ 20041 TCP_STAT(tcps, tcp_mdt_legacy_all); 20042 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20043 tcp_tcp_hdr_len, num_sack_blk, usable, 20044 snxt, tail_unsent, xmit_tail, local_time, 20045 INT_MAX)); 20046 } 20047 20048 /* link to any existing ones, if applicable */ 20049 TCP_STAT(tcps, tcp_mdt_allocd); 20050 if (md_mp_head == NULL) { 20051 md_mp_head = md_mp; 20052 } else if (tcp_mdt_chain) { 20053 TCP_STAT(tcps, tcp_mdt_linked); 20054 linkb(md_mp_head, md_mp); 20055 } 20056 } 20057 20058 ASSERT(md_mp_head != NULL); 20059 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20060 ASSERT(md_mp != NULL && mmd != NULL); 20061 ASSERT(md_hbuf != NULL); 20062 20063 /* 20064 * Packetize the transmittable portion of the data block; 20065 * each data block is essentially added to the Multidata 20066 * as a payload buffer. We also deal with adding more 20067 * than one payload buffers, which happens when the remaining 20068 * packetized portion of the current payload buffer is less 20069 * than MSS, while the next data block in transmit queue 20070 * has enough data to make up for one. This "spillover" 20071 * case essentially creates a split-packet, where portions 20072 * of the packet's payload fragments may span across two 20073 * virtually discontiguous address blocks. 20074 */ 20075 seg_len = mss; 20076 do { 20077 len = seg_len; 20078 20079 ASSERT(len > 0); 20080 ASSERT(max_pld >= 0); 20081 ASSERT(!add_buffer || cur_pld_off == 0); 20082 20083 /* 20084 * First time around for this payload buffer; note 20085 * in the case of a spillover, the following has 20086 * been done prior to adding the split-packet 20087 * descriptor to Multidata, and we don't want to 20088 * repeat the process. 20089 */ 20090 if (add_buffer) { 20091 ASSERT(mmd != NULL); 20092 ASSERT(md_pbuf == NULL); 20093 ASSERT(md_pbuf_nxt == NULL); 20094 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20095 20096 /* 20097 * Have we reached the limit? We'd get to 20098 * this case when we're not chaining the 20099 * Multidata messages together, and since 20100 * we're done, terminate this loop. 20101 */ 20102 if (max_pld == 0) 20103 break; /* done */ 20104 20105 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20106 TCP_STAT(tcps, tcp_mdt_allocfail); 20107 goto legacy_send; /* out_of_mem */ 20108 } 20109 20110 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20111 zc_cap != NULL) { 20112 if (!ip_md_zcopy_attr(mmd, NULL, 20113 zc_cap->ill_zerocopy_flags)) { 20114 freeb(md_pbuf); 20115 TCP_STAT(tcps, 20116 tcp_mdt_allocfail); 20117 /* out_of_mem */ 20118 goto legacy_send; 20119 } 20120 zcopy = B_TRUE; 20121 } 20122 20123 md_pbuf->b_rptr += base_pld_off; 20124 20125 /* 20126 * Add a payload buffer to the Multidata; this 20127 * operation must not fail, or otherwise our 20128 * logic in this routine is broken. There 20129 * is no memory allocation done by the 20130 * routine, so any returned failure simply 20131 * tells us that we've done something wrong. 20132 * 20133 * A failure tells us that either we're adding 20134 * the same payload buffer more than once, or 20135 * we're trying to add more buffers than 20136 * allowed (max_pld calculation is wrong). 20137 * None of the above cases should happen, and 20138 * we panic because either there's horrible 20139 * heap corruption, and/or programming mistake. 20140 */ 20141 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20142 if (pbuf_idx < 0) { 20143 cmn_err(CE_PANIC, "tcp_multisend: " 20144 "payload buffer logic error " 20145 "detected for tcp %p mmd %p " 20146 "pbuf %p (%d)\n", 20147 (void *)tcp, (void *)mmd, 20148 (void *)md_pbuf, pbuf_idx); 20149 } 20150 20151 ASSERT(max_pld > 0); 20152 --max_pld; 20153 add_buffer = B_FALSE; 20154 } 20155 20156 ASSERT(md_mp_head != NULL); 20157 ASSERT(md_pbuf != NULL); 20158 ASSERT(md_pbuf_nxt == NULL); 20159 ASSERT(pbuf_idx != -1); 20160 ASSERT(pbuf_idx_nxt == -1); 20161 ASSERT(*usable > 0); 20162 20163 /* 20164 * We spillover to the next payload buffer only 20165 * if all of the following is true: 20166 * 20167 * 1. There is not enough data on the current 20168 * payload buffer to make up `len', 20169 * 2. We are allowed to send `len', 20170 * 3. The next payload buffer length is large 20171 * enough to accomodate `spill'. 20172 */ 20173 if ((spill = len - *tail_unsent) > 0 && 20174 *usable >= len && 20175 MBLKL((*xmit_tail)->b_cont) >= spill && 20176 max_pld > 0) { 20177 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20178 if (md_pbuf_nxt == NULL) { 20179 TCP_STAT(tcps, tcp_mdt_allocfail); 20180 goto legacy_send; /* out_of_mem */ 20181 } 20182 20183 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20184 zc_cap != NULL) { 20185 if (!ip_md_zcopy_attr(mmd, NULL, 20186 zc_cap->ill_zerocopy_flags)) { 20187 freeb(md_pbuf_nxt); 20188 TCP_STAT(tcps, 20189 tcp_mdt_allocfail); 20190 /* out_of_mem */ 20191 goto legacy_send; 20192 } 20193 zcopy = B_TRUE; 20194 } 20195 20196 /* 20197 * See comments above on the first call to 20198 * mmd_addpldbuf for explanation on the panic. 20199 */ 20200 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20201 if (pbuf_idx_nxt < 0) { 20202 panic("tcp_multisend: " 20203 "next payload buffer logic error " 20204 "detected for tcp %p mmd %p " 20205 "pbuf %p (%d)\n", 20206 (void *)tcp, (void *)mmd, 20207 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20208 } 20209 20210 ASSERT(max_pld > 0); 20211 --max_pld; 20212 } else if (spill > 0) { 20213 /* 20214 * If there's a spillover, but the following 20215 * xmit_tail couldn't give us enough octets 20216 * to reach "len", then stop the current 20217 * Multidata creation and let the legacy 20218 * tcp_send() path take over. We don't want 20219 * to send the tiny segment as part of this 20220 * Multidata for performance reasons; instead, 20221 * we let the legacy path deal with grouping 20222 * it with the subsequent small mblks. 20223 */ 20224 if (*usable >= len && 20225 MBLKL((*xmit_tail)->b_cont) < spill) { 20226 max_pld = 0; 20227 break; /* done */ 20228 } 20229 20230 /* 20231 * We can't spillover, and we are near 20232 * the end of the current payload buffer, 20233 * so send what's left. 20234 */ 20235 ASSERT(*tail_unsent > 0); 20236 len = *tail_unsent; 20237 } 20238 20239 /* tail_unsent is negated if there is a spillover */ 20240 *tail_unsent -= len; 20241 *usable -= len; 20242 ASSERT(*usable >= 0); 20243 20244 if (*usable < mss) 20245 seg_len = *usable; 20246 /* 20247 * Sender SWS avoidance; see comments in tcp_send(); 20248 * everything else is the same, except that we only 20249 * do this here if there is no more data to be sent 20250 * following the current xmit_tail. We don't check 20251 * for 1-byte urgent data because we shouldn't get 20252 * here if TCP_URG_VALID is set. 20253 */ 20254 if (*usable > 0 && *usable < mss && 20255 ((md_pbuf_nxt == NULL && 20256 (*xmit_tail)->b_cont == NULL) || 20257 (md_pbuf_nxt != NULL && 20258 (*xmit_tail)->b_cont->b_cont == NULL)) && 20259 seg_len < (tcp->tcp_max_swnd >> 1) && 20260 (tcp->tcp_unsent - 20261 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20262 !tcp->tcp_zero_win_probe) { 20263 if ((*snxt + len) == tcp->tcp_snxt && 20264 (*snxt + len) == tcp->tcp_suna) { 20265 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20266 } 20267 done = B_TRUE; 20268 } 20269 20270 /* 20271 * Prime pump for IP's checksumming on our behalf; 20272 * include the adjustment for a source route if any. 20273 * Do this only for software/partial hardware checksum 20274 * offload, as this field gets zeroed out later for 20275 * the full hardware checksum offload case. 20276 */ 20277 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20278 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20279 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20280 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20281 } 20282 20283 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20284 *snxt += len; 20285 20286 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20287 /* 20288 * We set the PUSH bit only if TCP has no more buffered 20289 * data to be transmitted (or if sender SWS avoidance 20290 * takes place), as opposed to setting it for every 20291 * last packet in the burst. 20292 */ 20293 if (done || 20294 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20295 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20296 20297 /* 20298 * Set FIN bit if this is our last segment; snxt 20299 * already includes its length, and it will not 20300 * be adjusted after this point. 20301 */ 20302 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20303 *snxt == tcp->tcp_fss) { 20304 if (!tcp->tcp_fin_acked) { 20305 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20306 BUMP_MIB(&tcps->tcps_mib, 20307 tcpOutControl); 20308 } 20309 if (!tcp->tcp_fin_sent) { 20310 tcp->tcp_fin_sent = B_TRUE; 20311 /* 20312 * tcp state must be ESTABLISHED 20313 * in order for us to get here in 20314 * the first place. 20315 */ 20316 tcp->tcp_state = TCPS_FIN_WAIT_1; 20317 20318 /* 20319 * Upon returning from this routine, 20320 * tcp_wput_data() will set tcp_snxt 20321 * to be equal to snxt + tcp_fin_sent. 20322 * This is essentially the same as 20323 * setting it to tcp_fss + 1. 20324 */ 20325 } 20326 } 20327 20328 tcp->tcp_last_sent_len = (ushort_t)len; 20329 20330 len += tcp_hdr_len; 20331 if (tcp->tcp_ipversion == IPV4_VERSION) 20332 tcp->tcp_ipha->ipha_length = htons(len); 20333 else 20334 tcp->tcp_ip6h->ip6_plen = htons(len - 20335 ((char *)&tcp->tcp_ip6h[1] - 20336 tcp->tcp_iphc)); 20337 20338 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20339 20340 /* setup header fragment */ 20341 PDESC_HDR_ADD(pkt_info, 20342 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20343 tcp->tcp_mdt_hdr_head, /* head room */ 20344 tcp_hdr_len, /* len */ 20345 tcp->tcp_mdt_hdr_tail); /* tail room */ 20346 20347 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20348 hdr_frag_sz); 20349 ASSERT(MBLKIN(md_hbuf, 20350 (pkt_info->hdr_base - md_hbuf->b_rptr), 20351 PDESC_HDRSIZE(pkt_info))); 20352 20353 /* setup first payload fragment */ 20354 PDESC_PLD_INIT(pkt_info); 20355 PDESC_PLD_SPAN_ADD(pkt_info, 20356 pbuf_idx, /* index */ 20357 md_pbuf->b_rptr + cur_pld_off, /* start */ 20358 tcp->tcp_last_sent_len); /* len */ 20359 20360 /* create a split-packet in case of a spillover */ 20361 if (md_pbuf_nxt != NULL) { 20362 ASSERT(spill > 0); 20363 ASSERT(pbuf_idx_nxt > pbuf_idx); 20364 ASSERT(!add_buffer); 20365 20366 md_pbuf = md_pbuf_nxt; 20367 md_pbuf_nxt = NULL; 20368 pbuf_idx = pbuf_idx_nxt; 20369 pbuf_idx_nxt = -1; 20370 cur_pld_off = spill; 20371 20372 /* trim out first payload fragment */ 20373 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20374 20375 /* setup second payload fragment */ 20376 PDESC_PLD_SPAN_ADD(pkt_info, 20377 pbuf_idx, /* index */ 20378 md_pbuf->b_rptr, /* start */ 20379 spill); /* len */ 20380 20381 if ((*xmit_tail)->b_next == NULL) { 20382 /* 20383 * Store the lbolt used for RTT 20384 * estimation. We can only record one 20385 * timestamp per mblk so we do it when 20386 * we reach the end of the payload 20387 * buffer. Also we only take a new 20388 * timestamp sample when the previous 20389 * timed data from the same mblk has 20390 * been ack'ed. 20391 */ 20392 (*xmit_tail)->b_prev = local_time; 20393 (*xmit_tail)->b_next = 20394 (mblk_t *)(uintptr_t)first_snxt; 20395 } 20396 20397 first_snxt = *snxt - spill; 20398 20399 /* 20400 * Advance xmit_tail; usable could be 0 by 20401 * the time we got here, but we made sure 20402 * above that we would only spillover to 20403 * the next data block if usable includes 20404 * the spilled-over amount prior to the 20405 * subtraction. Therefore, we are sure 20406 * that xmit_tail->b_cont can't be NULL. 20407 */ 20408 ASSERT((*xmit_tail)->b_cont != NULL); 20409 *xmit_tail = (*xmit_tail)->b_cont; 20410 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20411 (uintptr_t)INT_MAX); 20412 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20413 } else { 20414 cur_pld_off += tcp->tcp_last_sent_len; 20415 } 20416 20417 /* 20418 * Fill in the header using the template header, and 20419 * add options such as time-stamp, ECN and/or SACK, 20420 * as needed. 20421 */ 20422 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20423 (clock_t)local_time, num_sack_blk); 20424 20425 /* take care of some IP header businesses */ 20426 if (af == AF_INET) { 20427 ipha = (ipha_t *)pkt_info->hdr_rptr; 20428 20429 ASSERT(OK_32PTR((uchar_t *)ipha)); 20430 ASSERT(PDESC_HDRL(pkt_info) >= 20431 IP_SIMPLE_HDR_LENGTH); 20432 ASSERT(ipha->ipha_version_and_hdr_length == 20433 IP_SIMPLE_HDR_VERSION); 20434 20435 /* 20436 * Assign ident value for current packet; see 20437 * related comments in ip_wput_ire() about the 20438 * contract private interface with clustering 20439 * group. 20440 */ 20441 clusterwide = B_FALSE; 20442 if (cl_inet_ipident != NULL) { 20443 ASSERT(cl_inet_isclusterwide != NULL); 20444 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20445 AF_INET, 20446 (uint8_t *)(uintptr_t)src)) { 20447 ipha->ipha_ident = 20448 (*cl_inet_ipident) 20449 (IPPROTO_IP, AF_INET, 20450 (uint8_t *)(uintptr_t)src, 20451 (uint8_t *)(uintptr_t)dst); 20452 clusterwide = B_TRUE; 20453 } 20454 } 20455 20456 if (!clusterwide) { 20457 ipha->ipha_ident = (uint16_t) 20458 atomic_add_32_nv( 20459 &ire->ire_ident, 1); 20460 } 20461 #ifndef _BIG_ENDIAN 20462 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20463 (ipha->ipha_ident >> 8); 20464 #endif 20465 } else { 20466 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20467 20468 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20469 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20470 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20471 ASSERT(PDESC_HDRL(pkt_info) >= 20472 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20473 TCP_CHECKSUM_SIZE)); 20474 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20475 20476 if (tcp->tcp_ip_forward_progress) { 20477 rconfirm = B_TRUE; 20478 tcp->tcp_ip_forward_progress = B_FALSE; 20479 } 20480 } 20481 20482 /* at least one payload span, and at most two */ 20483 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20484 20485 /* add the packet descriptor to Multidata */ 20486 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20487 KM_NOSLEEP)) == NULL) { 20488 /* 20489 * Any failure other than ENOMEM indicates 20490 * that we have passed in invalid pkt_info 20491 * or parameters to mmd_addpdesc, which must 20492 * not happen. 20493 * 20494 * EINVAL is a result of failure on boundary 20495 * checks against the pkt_info contents. It 20496 * should not happen, and we panic because 20497 * either there's horrible heap corruption, 20498 * and/or programming mistake. 20499 */ 20500 if (err != ENOMEM) { 20501 cmn_err(CE_PANIC, "tcp_multisend: " 20502 "pdesc logic error detected for " 20503 "tcp %p mmd %p pinfo %p (%d)\n", 20504 (void *)tcp, (void *)mmd, 20505 (void *)pkt_info, err); 20506 } 20507 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20508 goto legacy_send; /* out_of_mem */ 20509 } 20510 ASSERT(pkt != NULL); 20511 20512 /* calculate IP header and TCP checksums */ 20513 if (af == AF_INET) { 20514 /* calculate pseudo-header checksum */ 20515 cksum = (dst >> 16) + (dst & 0xFFFF) + 20516 (src >> 16) + (src & 0xFFFF); 20517 20518 /* offset for TCP header checksum */ 20519 up = IPH_TCPH_CHECKSUMP(ipha, 20520 IP_SIMPLE_HDR_LENGTH); 20521 } else { 20522 up = (uint16_t *)&ip6h->ip6_src; 20523 20524 /* calculate pseudo-header checksum */ 20525 cksum = up[0] + up[1] + up[2] + up[3] + 20526 up[4] + up[5] + up[6] + up[7] + 20527 up[8] + up[9] + up[10] + up[11] + 20528 up[12] + up[13] + up[14] + up[15]; 20529 20530 /* Fold the initial sum */ 20531 cksum = (cksum & 0xffff) + (cksum >> 16); 20532 20533 up = (uint16_t *)(((uchar_t *)ip6h) + 20534 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20535 } 20536 20537 if (hwcksum_flags & HCK_FULLCKSUM) { 20538 /* clear checksum field for hardware */ 20539 *up = 0; 20540 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20541 uint32_t sum; 20542 20543 /* pseudo-header checksumming */ 20544 sum = *up + cksum + IP_TCP_CSUM_COMP; 20545 sum = (sum & 0xFFFF) + (sum >> 16); 20546 *up = (sum & 0xFFFF) + (sum >> 16); 20547 } else { 20548 /* software checksumming */ 20549 TCP_STAT(tcps, tcp_out_sw_cksum); 20550 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20551 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20552 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20553 cksum + IP_TCP_CSUM_COMP); 20554 if (*up == 0) 20555 *up = 0xFFFF; 20556 } 20557 20558 /* IPv4 header checksum */ 20559 if (af == AF_INET) { 20560 ipha->ipha_fragment_offset_and_flags |= 20561 (uint32_t)htons(ire->ire_frag_flag); 20562 20563 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20564 ipha->ipha_hdr_checksum = 0; 20565 } else { 20566 IP_HDR_CKSUM(ipha, cksum, 20567 ((uint32_t *)ipha)[0], 20568 ((uint16_t *)ipha)[4]); 20569 } 20570 } 20571 20572 if (af == AF_INET && 20573 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20574 af == AF_INET6 && 20575 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20576 /* build header(IP/TCP) mblk for this segment */ 20577 if ((mp = dupb(md_hbuf)) == NULL) 20578 goto legacy_send; 20579 20580 mp->b_rptr = pkt_info->hdr_rptr; 20581 mp->b_wptr = pkt_info->hdr_wptr; 20582 20583 /* build payload mblk for this segment */ 20584 if ((mp1 = dupb(*xmit_tail)) == NULL) { 20585 freemsg(mp); 20586 goto legacy_send; 20587 } 20588 mp1->b_wptr = md_pbuf->b_rptr + cur_pld_off; 20589 mp1->b_rptr = mp1->b_wptr - 20590 tcp->tcp_last_sent_len; 20591 linkb(mp, mp1); 20592 20593 pld_start = mp1->b_rptr; 20594 20595 if (af == AF_INET) { 20596 DTRACE_PROBE4( 20597 ip4__physical__out__start, 20598 ill_t *, NULL, 20599 ill_t *, ill, 20600 ipha_t *, ipha, 20601 mblk_t *, mp); 20602 FW_HOOKS( 20603 ipst->ips_ip4_physical_out_event, 20604 ipst->ips_ipv4firewall_physical_out, 20605 NULL, ill, ipha, mp, mp, ipst); 20606 DTRACE_PROBE1( 20607 ip4__physical__out__end, 20608 mblk_t *, mp); 20609 } else { 20610 DTRACE_PROBE4( 20611 ip6__physical__out_start, 20612 ill_t *, NULL, 20613 ill_t *, ill, 20614 ip6_t *, ip6h, 20615 mblk_t *, mp); 20616 FW_HOOKS6( 20617 ipst->ips_ip6_physical_out_event, 20618 ipst->ips_ipv6firewall_physical_out, 20619 NULL, ill, ip6h, mp, mp, ipst); 20620 DTRACE_PROBE1( 20621 ip6__physical__out__end, 20622 mblk_t *, mp); 20623 } 20624 20625 if (buf_trunked && mp != NULL) { 20626 /* 20627 * Need to pass it to normal path. 20628 */ 20629 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20630 } else if (mp == NULL || 20631 mp->b_rptr != pkt_info->hdr_rptr || 20632 mp->b_wptr != pkt_info->hdr_wptr || 20633 (mp1 = mp->b_cont) == NULL || 20634 mp1->b_rptr != pld_start || 20635 mp1->b_wptr != pld_start + 20636 tcp->tcp_last_sent_len || 20637 mp1->b_cont != NULL) { 20638 /* 20639 * Need to pass all packets of this 20640 * buffer to normal path, either when 20641 * packet is blocked, or when boundary 20642 * of header buffer or payload buffer 20643 * has been changed by FW_HOOKS[6]. 20644 */ 20645 buf_trunked = B_TRUE; 20646 if (md_mp_head != NULL) { 20647 err = (intptr_t)rmvb(md_mp_head, 20648 md_mp); 20649 if (err == 0) 20650 md_mp_head = NULL; 20651 } 20652 20653 /* send down what we've got so far */ 20654 if (md_mp_head != NULL) { 20655 tcp_multisend_data(tcp, ire, 20656 ill, md_mp_head, obsegs, 20657 obbytes, &rconfirm); 20658 } 20659 md_mp_head = NULL; 20660 20661 if (mp != NULL) 20662 CALL_IP_WPUT(tcp->tcp_connp, 20663 q, mp); 20664 20665 mp1 = fw_mp_head; 20666 do { 20667 mp = mp1; 20668 mp1 = mp1->b_next; 20669 mp->b_next = NULL; 20670 mp->b_prev = NULL; 20671 CALL_IP_WPUT(tcp->tcp_connp, 20672 q, mp); 20673 } while (mp1 != NULL); 20674 20675 fw_mp_head = NULL; 20676 } else { 20677 if (fw_mp_head == NULL) 20678 fw_mp_head = mp; 20679 else 20680 fw_mp_head->b_prev->b_next = mp; 20681 fw_mp_head->b_prev = mp; 20682 } 20683 } 20684 20685 /* advance header offset */ 20686 cur_hdr_off += hdr_frag_sz; 20687 20688 obbytes += tcp->tcp_last_sent_len; 20689 ++obsegs; 20690 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20691 *tail_unsent > 0); 20692 20693 if ((*xmit_tail)->b_next == NULL) { 20694 /* 20695 * Store the lbolt used for RTT estimation. We can only 20696 * record one timestamp per mblk so we do it when we 20697 * reach the end of the payload buffer. Also we only 20698 * take a new timestamp sample when the previous timed 20699 * data from the same mblk has been ack'ed. 20700 */ 20701 (*xmit_tail)->b_prev = local_time; 20702 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20703 } 20704 20705 ASSERT(*tail_unsent >= 0); 20706 if (*tail_unsent > 0) { 20707 /* 20708 * We got here because we broke out of the above 20709 * loop due to of one of the following cases: 20710 * 20711 * 1. len < adjusted MSS (i.e. small), 20712 * 2. Sender SWS avoidance, 20713 * 3. max_pld is zero. 20714 * 20715 * We are done for this Multidata, so trim our 20716 * last payload buffer (if any) accordingly. 20717 */ 20718 if (md_pbuf != NULL) 20719 md_pbuf->b_wptr -= *tail_unsent; 20720 } else if (*usable > 0) { 20721 *xmit_tail = (*xmit_tail)->b_cont; 20722 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20723 (uintptr_t)INT_MAX); 20724 *tail_unsent = (int)MBLKL(*xmit_tail); 20725 add_buffer = B_TRUE; 20726 } 20727 20728 while (fw_mp_head) { 20729 mp = fw_mp_head; 20730 fw_mp_head = fw_mp_head->b_next; 20731 mp->b_prev = mp->b_next = NULL; 20732 freemsg(mp); 20733 } 20734 if (buf_trunked) { 20735 TCP_STAT(tcps, tcp_mdt_discarded); 20736 freeb(md_mp); 20737 buf_trunked = B_FALSE; 20738 } 20739 } while (!done && *usable > 0 && num_burst_seg > 0 && 20740 (tcp_mdt_chain || max_pld > 0)); 20741 20742 if (md_mp_head != NULL) { 20743 /* send everything down */ 20744 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20745 &rconfirm); 20746 } 20747 20748 #undef PREP_NEW_MULTIDATA 20749 #undef PREP_NEW_PBUF 20750 #undef IPVER 20751 20752 IRE_REFRELE(ire); 20753 return (0); 20754 } 20755 20756 /* 20757 * A wrapper function for sending one or more Multidata messages down to 20758 * the module below ip; this routine does not release the reference of the 20759 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20760 */ 20761 static void 20762 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20763 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20764 { 20765 uint64_t delta; 20766 nce_t *nce; 20767 tcp_stack_t *tcps = tcp->tcp_tcps; 20768 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20769 20770 ASSERT(ire != NULL && ill != NULL); 20771 ASSERT(ire->ire_stq != NULL); 20772 ASSERT(md_mp_head != NULL); 20773 ASSERT(rconfirm != NULL); 20774 20775 /* adjust MIBs and IRE timestamp */ 20776 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 20777 tcp->tcp_obsegs += obsegs; 20778 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20779 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20780 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20781 20782 if (tcp->tcp_ipversion == IPV4_VERSION) { 20783 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20784 } else { 20785 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20786 } 20787 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20788 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20789 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20790 20791 ire->ire_ob_pkt_count += obsegs; 20792 if (ire->ire_ipif != NULL) 20793 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20794 ire->ire_last_used_time = lbolt; 20795 20796 /* send it down */ 20797 putnext(ire->ire_stq, md_mp_head); 20798 20799 /* we're done for TCP/IPv4 */ 20800 if (tcp->tcp_ipversion == IPV4_VERSION) 20801 return; 20802 20803 nce = ire->ire_nce; 20804 20805 ASSERT(nce != NULL); 20806 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20807 ASSERT(nce->nce_state != ND_INCOMPLETE); 20808 20809 /* reachability confirmation? */ 20810 if (*rconfirm) { 20811 nce->nce_last = TICK_TO_MSEC(lbolt64); 20812 if (nce->nce_state != ND_REACHABLE) { 20813 mutex_enter(&nce->nce_lock); 20814 nce->nce_state = ND_REACHABLE; 20815 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20816 mutex_exit(&nce->nce_lock); 20817 (void) untimeout(nce->nce_timeout_id); 20818 if (ip_debug > 2) { 20819 /* ip1dbg */ 20820 pr_addr_dbg("tcp_multisend_data: state " 20821 "for %s changed to REACHABLE\n", 20822 AF_INET6, &ire->ire_addr_v6); 20823 } 20824 } 20825 /* reset transport reachability confirmation */ 20826 *rconfirm = B_FALSE; 20827 } 20828 20829 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20830 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20831 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20832 20833 if (delta > (uint64_t)ill->ill_reachable_time) { 20834 mutex_enter(&nce->nce_lock); 20835 switch (nce->nce_state) { 20836 case ND_REACHABLE: 20837 case ND_STALE: 20838 /* 20839 * ND_REACHABLE is identical to ND_STALE in this 20840 * specific case. If reachable time has expired for 20841 * this neighbor (delta is greater than reachable 20842 * time), conceptually, the neighbor cache is no 20843 * longer in REACHABLE state, but already in STALE 20844 * state. So the correct transition here is to 20845 * ND_DELAY. 20846 */ 20847 nce->nce_state = ND_DELAY; 20848 mutex_exit(&nce->nce_lock); 20849 NDP_RESTART_TIMER(nce, 20850 ipst->ips_delay_first_probe_time); 20851 if (ip_debug > 3) { 20852 /* ip2dbg */ 20853 pr_addr_dbg("tcp_multisend_data: state " 20854 "for %s changed to DELAY\n", 20855 AF_INET6, &ire->ire_addr_v6); 20856 } 20857 break; 20858 case ND_DELAY: 20859 case ND_PROBE: 20860 mutex_exit(&nce->nce_lock); 20861 /* Timers have already started */ 20862 break; 20863 case ND_UNREACHABLE: 20864 /* 20865 * ndp timer has detected that this nce is 20866 * unreachable and initiated deleting this nce 20867 * and all its associated IREs. This is a race 20868 * where we found the ire before it was deleted 20869 * and have just sent out a packet using this 20870 * unreachable nce. 20871 */ 20872 mutex_exit(&nce->nce_lock); 20873 break; 20874 default: 20875 ASSERT(0); 20876 } 20877 } 20878 } 20879 20880 /* 20881 * Derived from tcp_send_data(). 20882 */ 20883 static void 20884 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20885 int num_lso_seg) 20886 { 20887 ipha_t *ipha; 20888 mblk_t *ire_fp_mp; 20889 uint_t ire_fp_mp_len; 20890 uint32_t hcksum_txflags = 0; 20891 ipaddr_t src; 20892 ipaddr_t dst; 20893 uint32_t cksum; 20894 uint16_t *up; 20895 tcp_stack_t *tcps = tcp->tcp_tcps; 20896 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20897 20898 ASSERT(DB_TYPE(mp) == M_DATA); 20899 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20900 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20901 ASSERT(tcp->tcp_connp != NULL); 20902 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20903 20904 ipha = (ipha_t *)mp->b_rptr; 20905 src = ipha->ipha_src; 20906 dst = ipha->ipha_dst; 20907 20908 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20909 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20910 num_lso_seg); 20911 #ifndef _BIG_ENDIAN 20912 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20913 #endif 20914 if (tcp->tcp_snd_zcopy_aware) { 20915 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20916 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20917 mp = tcp_zcopy_disable(tcp, mp); 20918 } 20919 20920 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20921 ASSERT(ill->ill_hcksum_capab != NULL); 20922 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20923 } 20924 20925 /* 20926 * Since the TCP checksum should be recalculated by h/w, we can just 20927 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20928 * pseudo-header checksum for HCK_PARTIALCKSUM. 20929 * The partial pseudo-header excludes TCP length, that was calculated 20930 * in tcp_send(), so to zero *up before further processing. 20931 */ 20932 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20933 20934 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20935 *up = 0; 20936 20937 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20938 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20939 20940 /* 20941 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 20942 */ 20943 DB_LSOFLAGS(mp) |= HW_LSO; 20944 DB_LSOMSS(mp) = mss; 20945 20946 ipha->ipha_fragment_offset_and_flags |= 20947 (uint32_t)htons(ire->ire_frag_flag); 20948 20949 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20950 ire_fp_mp_len = MBLKL(ire_fp_mp); 20951 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20952 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20953 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20954 20955 UPDATE_OB_PKT_COUNT(ire); 20956 ire->ire_last_used_time = lbolt; 20957 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20958 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20959 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20960 ntohs(ipha->ipha_length)); 20961 20962 if (ILL_DLS_CAPABLE(ill)) { 20963 /* 20964 * Send the packet directly to DLD, where it may be queued 20965 * depending on the availability of transmit resources at 20966 * the media layer. 20967 */ 20968 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 20969 } else { 20970 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 20971 DTRACE_PROBE4(ip4__physical__out__start, 20972 ill_t *, NULL, ill_t *, out_ill, 20973 ipha_t *, ipha, mblk_t *, mp); 20974 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20975 ipst->ips_ipv4firewall_physical_out, 20976 NULL, out_ill, ipha, mp, mp, ipst); 20977 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20978 if (mp != NULL) 20979 putnext(ire->ire_stq, mp); 20980 } 20981 } 20982 20983 /* 20984 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20985 * scheme, and returns one of the following: 20986 * 20987 * -1 = failed allocation. 20988 * 0 = success; burst count reached, or usable send window is too small, 20989 * and that we'd rather wait until later before sending again. 20990 * 1 = success; we are called from tcp_multisend(), and both usable send 20991 * window and tail_unsent are greater than the MDT threshold, and thus 20992 * Multidata Transmit should be used instead. 20993 */ 20994 static int 20995 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20996 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20997 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20998 const int mdt_thres) 20999 { 21000 int num_burst_seg = tcp->tcp_snd_burst; 21001 ire_t *ire = NULL; 21002 ill_t *ill = NULL; 21003 mblk_t *ire_fp_mp = NULL; 21004 uint_t ire_fp_mp_len = 0; 21005 int num_lso_seg = 1; 21006 uint_t lso_usable; 21007 boolean_t do_lso_send = B_FALSE; 21008 tcp_stack_t *tcps = tcp->tcp_tcps; 21009 21010 /* 21011 * Check LSO capability before any further work. And the similar check 21012 * need to be done in for(;;) loop. 21013 * LSO will be deployed when therer is more than one mss of available 21014 * data and a burst transmission is allowed. 21015 */ 21016 if (tcp->tcp_lso && 21017 (tcp->tcp_valid_bits == 0 || 21018 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21019 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21020 /* 21021 * Try to find usable IRE/ILL and do basic check to the ILL. 21022 */ 21023 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21024 /* 21025 * Enable LSO with this transmission. 21026 * Since IRE has been hold in 21027 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21028 * should be called before return. 21029 */ 21030 do_lso_send = B_TRUE; 21031 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21032 ire_fp_mp_len = MBLKL(ire_fp_mp); 21033 /* Round up to multiple of 4 */ 21034 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21035 } else { 21036 do_lso_send = B_FALSE; 21037 ill = NULL; 21038 } 21039 } 21040 21041 for (;;) { 21042 struct datab *db; 21043 tcph_t *tcph; 21044 uint32_t sum; 21045 mblk_t *mp, *mp1; 21046 uchar_t *rptr; 21047 int len; 21048 21049 /* 21050 * If we're called by tcp_multisend(), and the amount of 21051 * sendable data as well as the size of current xmit_tail 21052 * is beyond the MDT threshold, return to the caller and 21053 * let the large data transmit be done using MDT. 21054 */ 21055 if (*usable > 0 && *usable > mdt_thres && 21056 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21057 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21058 ASSERT(tcp->tcp_mdt); 21059 return (1); /* success; do large send */ 21060 } 21061 21062 if (num_burst_seg == 0) 21063 break; /* success; burst count reached */ 21064 21065 /* 21066 * Calculate the maximum payload length we can send in *one* 21067 * time. 21068 */ 21069 if (do_lso_send) { 21070 /* 21071 * Check whether need to do LSO any more. 21072 */ 21073 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21074 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21075 lso_usable = MIN(lso_usable, 21076 num_burst_seg * mss); 21077 21078 num_lso_seg = lso_usable / mss; 21079 if (lso_usable % mss) { 21080 num_lso_seg++; 21081 tcp->tcp_last_sent_len = (ushort_t) 21082 (lso_usable % mss); 21083 } else { 21084 tcp->tcp_last_sent_len = (ushort_t)mss; 21085 } 21086 } else { 21087 do_lso_send = B_FALSE; 21088 num_lso_seg = 1; 21089 lso_usable = mss; 21090 } 21091 } 21092 21093 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21094 21095 /* 21096 * Adjust num_burst_seg here. 21097 */ 21098 num_burst_seg -= num_lso_seg; 21099 21100 len = mss; 21101 if (len > *usable) { 21102 ASSERT(do_lso_send == B_FALSE); 21103 21104 len = *usable; 21105 if (len <= 0) { 21106 /* Terminate the loop */ 21107 break; /* success; too small */ 21108 } 21109 /* 21110 * Sender silly-window avoidance. 21111 * Ignore this if we are going to send a 21112 * zero window probe out. 21113 * 21114 * TODO: force data into microscopic window? 21115 * ==> (!pushed || (unsent > usable)) 21116 */ 21117 if (len < (tcp->tcp_max_swnd >> 1) && 21118 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21119 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21120 len == 1) && (! tcp->tcp_zero_win_probe)) { 21121 /* 21122 * If the retransmit timer is not running 21123 * we start it so that we will retransmit 21124 * in the case when the the receiver has 21125 * decremented the window. 21126 */ 21127 if (*snxt == tcp->tcp_snxt && 21128 *snxt == tcp->tcp_suna) { 21129 /* 21130 * We are not supposed to send 21131 * anything. So let's wait a little 21132 * bit longer before breaking SWS 21133 * avoidance. 21134 * 21135 * What should the value be? 21136 * Suggestion: MAX(init rexmit time, 21137 * tcp->tcp_rto) 21138 */ 21139 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21140 } 21141 break; /* success; too small */ 21142 } 21143 } 21144 21145 tcph = tcp->tcp_tcph; 21146 21147 /* 21148 * The reason to adjust len here is that we need to set flags 21149 * and calculate checksum. 21150 */ 21151 if (do_lso_send) 21152 len = lso_usable; 21153 21154 *usable -= len; /* Approximate - can be adjusted later */ 21155 if (*usable > 0) 21156 tcph->th_flags[0] = TH_ACK; 21157 else 21158 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21159 21160 /* 21161 * Prime pump for IP's checksumming on our behalf 21162 * Include the adjustment for a source route if any. 21163 */ 21164 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21165 sum = (sum >> 16) + (sum & 0xFFFF); 21166 U16_TO_ABE16(sum, tcph->th_sum); 21167 21168 U32_TO_ABE32(*snxt, tcph->th_seq); 21169 21170 /* 21171 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21172 * set. For the case when TCP_FSS_VALID is the only valid 21173 * bit (normal active close), branch off only when we think 21174 * that the FIN flag needs to be set. Note for this case, 21175 * that (snxt + len) may not reflect the actual seg_len, 21176 * as len may be further reduced in tcp_xmit_mp(). If len 21177 * gets modified, we will end up here again. 21178 */ 21179 if (tcp->tcp_valid_bits != 0 && 21180 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21181 ((*snxt + len) == tcp->tcp_fss))) { 21182 uchar_t *prev_rptr; 21183 uint32_t prev_snxt = tcp->tcp_snxt; 21184 21185 if (*tail_unsent == 0) { 21186 ASSERT((*xmit_tail)->b_cont != NULL); 21187 *xmit_tail = (*xmit_tail)->b_cont; 21188 prev_rptr = (*xmit_tail)->b_rptr; 21189 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21190 (*xmit_tail)->b_rptr); 21191 } else { 21192 prev_rptr = (*xmit_tail)->b_rptr; 21193 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21194 *tail_unsent; 21195 } 21196 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21197 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21198 /* Restore tcp_snxt so we get amount sent right. */ 21199 tcp->tcp_snxt = prev_snxt; 21200 if (prev_rptr == (*xmit_tail)->b_rptr) { 21201 /* 21202 * If the previous timestamp is still in use, 21203 * don't stomp on it. 21204 */ 21205 if ((*xmit_tail)->b_next == NULL) { 21206 (*xmit_tail)->b_prev = local_time; 21207 (*xmit_tail)->b_next = 21208 (mblk_t *)(uintptr_t)(*snxt); 21209 } 21210 } else 21211 (*xmit_tail)->b_rptr = prev_rptr; 21212 21213 if (mp == NULL) { 21214 if (ire != NULL) 21215 IRE_REFRELE(ire); 21216 return (-1); 21217 } 21218 mp1 = mp->b_cont; 21219 21220 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21221 tcp->tcp_last_sent_len = (ushort_t)len; 21222 while (mp1->b_cont) { 21223 *xmit_tail = (*xmit_tail)->b_cont; 21224 (*xmit_tail)->b_prev = local_time; 21225 (*xmit_tail)->b_next = 21226 (mblk_t *)(uintptr_t)(*snxt); 21227 mp1 = mp1->b_cont; 21228 } 21229 *snxt += len; 21230 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21231 BUMP_LOCAL(tcp->tcp_obsegs); 21232 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21233 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21234 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21235 tcp_send_data(tcp, q, mp); 21236 continue; 21237 } 21238 21239 *snxt += len; /* Adjust later if we don't send all of len */ 21240 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21241 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21242 21243 if (*tail_unsent) { 21244 /* Are the bytes above us in flight? */ 21245 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21246 if (rptr != (*xmit_tail)->b_rptr) { 21247 *tail_unsent -= len; 21248 if (len <= mss) /* LSO is unusable */ 21249 tcp->tcp_last_sent_len = (ushort_t)len; 21250 len += tcp_hdr_len; 21251 if (tcp->tcp_ipversion == IPV4_VERSION) 21252 tcp->tcp_ipha->ipha_length = htons(len); 21253 else 21254 tcp->tcp_ip6h->ip6_plen = 21255 htons(len - 21256 ((char *)&tcp->tcp_ip6h[1] - 21257 tcp->tcp_iphc)); 21258 mp = dupb(*xmit_tail); 21259 if (mp == NULL) { 21260 if (ire != NULL) 21261 IRE_REFRELE(ire); 21262 return (-1); /* out_of_mem */ 21263 } 21264 mp->b_rptr = rptr; 21265 /* 21266 * If the old timestamp is no longer in use, 21267 * sample a new timestamp now. 21268 */ 21269 if ((*xmit_tail)->b_next == NULL) { 21270 (*xmit_tail)->b_prev = local_time; 21271 (*xmit_tail)->b_next = 21272 (mblk_t *)(uintptr_t)(*snxt-len); 21273 } 21274 goto must_alloc; 21275 } 21276 } else { 21277 *xmit_tail = (*xmit_tail)->b_cont; 21278 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21279 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21280 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21281 (*xmit_tail)->b_rptr); 21282 } 21283 21284 (*xmit_tail)->b_prev = local_time; 21285 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21286 21287 *tail_unsent -= len; 21288 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21289 tcp->tcp_last_sent_len = (ushort_t)len; 21290 21291 len += tcp_hdr_len; 21292 if (tcp->tcp_ipversion == IPV4_VERSION) 21293 tcp->tcp_ipha->ipha_length = htons(len); 21294 else 21295 tcp->tcp_ip6h->ip6_plen = htons(len - 21296 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21297 21298 mp = dupb(*xmit_tail); 21299 if (mp == NULL) { 21300 if (ire != NULL) 21301 IRE_REFRELE(ire); 21302 return (-1); /* out_of_mem */ 21303 } 21304 21305 len = tcp_hdr_len; 21306 /* 21307 * There are four reasons to allocate a new hdr mblk: 21308 * 1) The bytes above us are in use by another packet 21309 * 2) We don't have good alignment 21310 * 3) The mblk is being shared 21311 * 4) We don't have enough room for a header 21312 */ 21313 rptr = mp->b_rptr - len; 21314 if (!OK_32PTR(rptr) || 21315 ((db = mp->b_datap), db->db_ref != 2) || 21316 rptr < db->db_base + ire_fp_mp_len) { 21317 /* NOTE: we assume allocb returns an OK_32PTR */ 21318 21319 must_alloc:; 21320 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21321 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21322 if (mp1 == NULL) { 21323 freemsg(mp); 21324 if (ire != NULL) 21325 IRE_REFRELE(ire); 21326 return (-1); /* out_of_mem */ 21327 } 21328 mp1->b_cont = mp; 21329 mp = mp1; 21330 /* Leave room for Link Level header */ 21331 len = tcp_hdr_len; 21332 rptr = 21333 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21334 mp->b_wptr = &rptr[len]; 21335 } 21336 21337 /* 21338 * Fill in the header using the template header, and add 21339 * options such as time-stamp, ECN and/or SACK, as needed. 21340 */ 21341 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21342 21343 mp->b_rptr = rptr; 21344 21345 if (*tail_unsent) { 21346 int spill = *tail_unsent; 21347 21348 mp1 = mp->b_cont; 21349 if (mp1 == NULL) 21350 mp1 = mp; 21351 21352 /* 21353 * If we're a little short, tack on more mblks until 21354 * there is no more spillover. 21355 */ 21356 while (spill < 0) { 21357 mblk_t *nmp; 21358 int nmpsz; 21359 21360 nmp = (*xmit_tail)->b_cont; 21361 nmpsz = MBLKL(nmp); 21362 21363 /* 21364 * Excess data in mblk; can we split it? 21365 * If MDT is enabled for the connection, 21366 * keep on splitting as this is a transient 21367 * send path. 21368 */ 21369 if (!do_lso_send && !tcp->tcp_mdt && 21370 (spill + nmpsz > 0)) { 21371 /* 21372 * Don't split if stream head was 21373 * told to break up larger writes 21374 * into smaller ones. 21375 */ 21376 if (tcp->tcp_maxpsz > 0) 21377 break; 21378 21379 /* 21380 * Next mblk is less than SMSS/2 21381 * rounded up to nearest 64-byte; 21382 * let it get sent as part of the 21383 * next segment. 21384 */ 21385 if (tcp->tcp_localnet && 21386 !tcp->tcp_cork && 21387 (nmpsz < roundup((mss >> 1), 64))) 21388 break; 21389 } 21390 21391 *xmit_tail = nmp; 21392 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21393 /* Stash for rtt use later */ 21394 (*xmit_tail)->b_prev = local_time; 21395 (*xmit_tail)->b_next = 21396 (mblk_t *)(uintptr_t)(*snxt - len); 21397 mp1->b_cont = dupb(*xmit_tail); 21398 mp1 = mp1->b_cont; 21399 21400 spill += nmpsz; 21401 if (mp1 == NULL) { 21402 *tail_unsent = spill; 21403 freemsg(mp); 21404 if (ire != NULL) 21405 IRE_REFRELE(ire); 21406 return (-1); /* out_of_mem */ 21407 } 21408 } 21409 21410 /* Trim back any surplus on the last mblk */ 21411 if (spill >= 0) { 21412 mp1->b_wptr -= spill; 21413 *tail_unsent = spill; 21414 } else { 21415 /* 21416 * We did not send everything we could in 21417 * order to remain within the b_cont limit. 21418 */ 21419 *usable -= spill; 21420 *snxt += spill; 21421 tcp->tcp_last_sent_len += spill; 21422 UPDATE_MIB(&tcps->tcps_mib, 21423 tcpOutDataBytes, spill); 21424 /* 21425 * Adjust the checksum 21426 */ 21427 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21428 sum += spill; 21429 sum = (sum >> 16) + (sum & 0xFFFF); 21430 U16_TO_ABE16(sum, tcph->th_sum); 21431 if (tcp->tcp_ipversion == IPV4_VERSION) { 21432 sum = ntohs( 21433 ((ipha_t *)rptr)->ipha_length) + 21434 spill; 21435 ((ipha_t *)rptr)->ipha_length = 21436 htons(sum); 21437 } else { 21438 sum = ntohs( 21439 ((ip6_t *)rptr)->ip6_plen) + 21440 spill; 21441 ((ip6_t *)rptr)->ip6_plen = 21442 htons(sum); 21443 } 21444 *tail_unsent = 0; 21445 } 21446 } 21447 if (tcp->tcp_ip_forward_progress) { 21448 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21449 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21450 tcp->tcp_ip_forward_progress = B_FALSE; 21451 } 21452 21453 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21454 if (do_lso_send) { 21455 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21456 num_lso_seg); 21457 tcp->tcp_obsegs += num_lso_seg; 21458 21459 TCP_STAT(tcps, tcp_lso_times); 21460 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21461 } else { 21462 tcp_send_data(tcp, q, mp); 21463 BUMP_LOCAL(tcp->tcp_obsegs); 21464 } 21465 } 21466 21467 if (ire != NULL) 21468 IRE_REFRELE(ire); 21469 return (0); 21470 } 21471 21472 /* Unlink and return any mblk that looks like it contains a MDT info */ 21473 static mblk_t * 21474 tcp_mdt_info_mp(mblk_t *mp) 21475 { 21476 mblk_t *prev_mp; 21477 21478 for (;;) { 21479 prev_mp = mp; 21480 /* no more to process? */ 21481 if ((mp = mp->b_cont) == NULL) 21482 break; 21483 21484 switch (DB_TYPE(mp)) { 21485 case M_CTL: 21486 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21487 continue; 21488 ASSERT(prev_mp != NULL); 21489 prev_mp->b_cont = mp->b_cont; 21490 mp->b_cont = NULL; 21491 return (mp); 21492 default: 21493 break; 21494 } 21495 } 21496 return (mp); 21497 } 21498 21499 /* MDT info update routine, called when IP notifies us about MDT */ 21500 static void 21501 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21502 { 21503 boolean_t prev_state; 21504 tcp_stack_t *tcps = tcp->tcp_tcps; 21505 21506 /* 21507 * IP is telling us to abort MDT on this connection? We know 21508 * this because the capability is only turned off when IP 21509 * encounters some pathological cases, e.g. link-layer change 21510 * where the new driver doesn't support MDT, or in situation 21511 * where MDT usage on the link-layer has been switched off. 21512 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21513 * if the link-layer doesn't support MDT, and if it does, it 21514 * will indicate that the feature is to be turned on. 21515 */ 21516 prev_state = tcp->tcp_mdt; 21517 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21518 if (!tcp->tcp_mdt && !first) { 21519 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21520 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21521 (void *)tcp->tcp_connp)); 21522 } 21523 21524 /* 21525 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21526 * so disable MDT otherwise. The checks are done here 21527 * and in tcp_wput_data(). 21528 */ 21529 if (tcp->tcp_mdt && 21530 (tcp->tcp_ipversion == IPV4_VERSION && 21531 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21532 (tcp->tcp_ipversion == IPV6_VERSION && 21533 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21534 tcp->tcp_mdt = B_FALSE; 21535 21536 if (tcp->tcp_mdt) { 21537 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21538 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21539 "version (%d), expected version is %d", 21540 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21541 tcp->tcp_mdt = B_FALSE; 21542 return; 21543 } 21544 21545 /* 21546 * We need the driver to be able to handle at least three 21547 * spans per packet in order for tcp MDT to be utilized. 21548 * The first is for the header portion, while the rest are 21549 * needed to handle a packet that straddles across two 21550 * virtually non-contiguous buffers; a typical tcp packet 21551 * therefore consists of only two spans. Note that we take 21552 * a zero as "don't care". 21553 */ 21554 if (mdt_capab->ill_mdt_span_limit > 0 && 21555 mdt_capab->ill_mdt_span_limit < 3) { 21556 tcp->tcp_mdt = B_FALSE; 21557 return; 21558 } 21559 21560 /* a zero means driver wants default value */ 21561 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21562 tcps->tcps_mdt_max_pbufs); 21563 if (tcp->tcp_mdt_max_pld == 0) 21564 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21565 21566 /* ensure 32-bit alignment */ 21567 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21568 mdt_capab->ill_mdt_hdr_head), 4); 21569 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21570 mdt_capab->ill_mdt_hdr_tail), 4); 21571 21572 if (!first && !prev_state) { 21573 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21574 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21575 (void *)tcp->tcp_connp)); 21576 } 21577 } 21578 } 21579 21580 /* Unlink and return any mblk that looks like it contains a LSO info */ 21581 static mblk_t * 21582 tcp_lso_info_mp(mblk_t *mp) 21583 { 21584 mblk_t *prev_mp; 21585 21586 for (;;) { 21587 prev_mp = mp; 21588 /* no more to process? */ 21589 if ((mp = mp->b_cont) == NULL) 21590 break; 21591 21592 switch (DB_TYPE(mp)) { 21593 case M_CTL: 21594 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21595 continue; 21596 ASSERT(prev_mp != NULL); 21597 prev_mp->b_cont = mp->b_cont; 21598 mp->b_cont = NULL; 21599 return (mp); 21600 default: 21601 break; 21602 } 21603 } 21604 21605 return (mp); 21606 } 21607 21608 /* LSO info update routine, called when IP notifies us about LSO */ 21609 static void 21610 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21611 { 21612 tcp_stack_t *tcps = tcp->tcp_tcps; 21613 21614 /* 21615 * IP is telling us to abort LSO on this connection? We know 21616 * this because the capability is only turned off when IP 21617 * encounters some pathological cases, e.g. link-layer change 21618 * where the new NIC/driver doesn't support LSO, or in situation 21619 * where LSO usage on the link-layer has been switched off. 21620 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21621 * if the link-layer doesn't support LSO, and if it does, it 21622 * will indicate that the feature is to be turned on. 21623 */ 21624 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21625 TCP_STAT(tcps, tcp_lso_enabled); 21626 21627 /* 21628 * We currently only support LSO on simple TCP/IPv4, 21629 * so disable LSO otherwise. The checks are done here 21630 * and in tcp_wput_data(). 21631 */ 21632 if (tcp->tcp_lso && 21633 (tcp->tcp_ipversion == IPV4_VERSION && 21634 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21635 (tcp->tcp_ipversion == IPV6_VERSION)) { 21636 tcp->tcp_lso = B_FALSE; 21637 TCP_STAT(tcps, tcp_lso_disabled); 21638 } else { 21639 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21640 lso_capab->ill_lso_max); 21641 } 21642 } 21643 21644 static void 21645 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21646 { 21647 conn_t *connp = tcp->tcp_connp; 21648 tcp_stack_t *tcps = tcp->tcp_tcps; 21649 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21650 21651 ASSERT(ire != NULL); 21652 21653 /* 21654 * We may be in the fastpath here, and although we essentially do 21655 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21656 * we try to keep things as brief as possible. After all, these 21657 * are only best-effort checks, and we do more thorough ones prior 21658 * to calling tcp_send()/tcp_multisend(). 21659 */ 21660 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21661 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21662 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21663 !(ire->ire_flags & RTF_MULTIRT) && 21664 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21665 CONN_IS_LSO_MD_FASTPATH(connp)) { 21666 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21667 /* Cache the result */ 21668 connp->conn_lso_ok = B_TRUE; 21669 21670 ASSERT(ill->ill_lso_capab != NULL); 21671 if (!ill->ill_lso_capab->ill_lso_on) { 21672 ill->ill_lso_capab->ill_lso_on = 1; 21673 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21674 "LSO for interface %s\n", (void *)connp, 21675 ill->ill_name)); 21676 } 21677 tcp_lso_update(tcp, ill->ill_lso_capab); 21678 } else if (ipst->ips_ip_multidata_outbound && 21679 ILL_MDT_CAPABLE(ill)) { 21680 /* Cache the result */ 21681 connp->conn_mdt_ok = B_TRUE; 21682 21683 ASSERT(ill->ill_mdt_capab != NULL); 21684 if (!ill->ill_mdt_capab->ill_mdt_on) { 21685 ill->ill_mdt_capab->ill_mdt_on = 1; 21686 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21687 "MDT for interface %s\n", (void *)connp, 21688 ill->ill_name)); 21689 } 21690 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21691 } 21692 } 21693 21694 /* 21695 * The goal is to reduce the number of generated tcp segments by 21696 * setting the maxpsz multiplier to 0; this will have an affect on 21697 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21698 * into each packet, up to SMSS bytes. Doing this reduces the number 21699 * of outbound segments and incoming ACKs, thus allowing for better 21700 * network and system performance. In contrast the legacy behavior 21701 * may result in sending less than SMSS size, because the last mblk 21702 * for some packets may have more data than needed to make up SMSS, 21703 * and the legacy code refused to "split" it. 21704 * 21705 * We apply the new behavior on following situations: 21706 * 21707 * 1) Loopback connections, 21708 * 2) Connections in which the remote peer is not on local subnet, 21709 * 3) Local subnet connections over the bge interface (see below). 21710 * 21711 * Ideally, we would like this behavior to apply for interfaces other 21712 * than bge. However, doing so would negatively impact drivers which 21713 * perform dynamic mapping and unmapping of DMA resources, which are 21714 * increased by setting the maxpsz multiplier to 0 (more mblks per 21715 * packet will be generated by tcp). The bge driver does not suffer 21716 * from this, as it copies the mblks into pre-mapped buffers, and 21717 * therefore does not require more I/O resources than before. 21718 * 21719 * Otherwise, this behavior is present on all network interfaces when 21720 * the destination endpoint is non-local, since reducing the number 21721 * of packets in general is good for the network. 21722 * 21723 * TODO We need to remove this hard-coded conditional for bge once 21724 * a better "self-tuning" mechanism, or a way to comprehend 21725 * the driver transmit strategy is devised. Until the solution 21726 * is found and well understood, we live with this hack. 21727 */ 21728 if (!tcp_static_maxpsz && 21729 (tcp->tcp_loopback || !tcp->tcp_localnet || 21730 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21731 /* override the default value */ 21732 tcp->tcp_maxpsz = 0; 21733 21734 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21735 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21736 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21737 } 21738 21739 /* set the stream head parameters accordingly */ 21740 (void) tcp_maxpsz_set(tcp, B_TRUE); 21741 } 21742 21743 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21744 static void 21745 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21746 { 21747 uchar_t fval = *mp->b_rptr; 21748 mblk_t *tail; 21749 queue_t *q = tcp->tcp_wq; 21750 21751 /* TODO: How should flush interact with urgent data? */ 21752 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21753 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21754 /* 21755 * Flush only data that has not yet been put on the wire. If 21756 * we flush data that we have already transmitted, life, as we 21757 * know it, may come to an end. 21758 */ 21759 tail = tcp->tcp_xmit_tail; 21760 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21761 tcp->tcp_xmit_tail_unsent = 0; 21762 tcp->tcp_unsent = 0; 21763 if (tail->b_wptr != tail->b_rptr) 21764 tail = tail->b_cont; 21765 if (tail) { 21766 mblk_t **excess = &tcp->tcp_xmit_head; 21767 for (;;) { 21768 mblk_t *mp1 = *excess; 21769 if (mp1 == tail) 21770 break; 21771 tcp->tcp_xmit_tail = mp1; 21772 tcp->tcp_xmit_last = mp1; 21773 excess = &mp1->b_cont; 21774 } 21775 *excess = NULL; 21776 tcp_close_mpp(&tail); 21777 if (tcp->tcp_snd_zcopy_aware) 21778 tcp_zcopy_notify(tcp); 21779 } 21780 /* 21781 * We have no unsent data, so unsent must be less than 21782 * tcp_xmit_lowater, so re-enable flow. 21783 */ 21784 mutex_enter(&tcp->tcp_non_sq_lock); 21785 if (tcp->tcp_flow_stopped) { 21786 tcp_clrqfull(tcp); 21787 } 21788 mutex_exit(&tcp->tcp_non_sq_lock); 21789 } 21790 /* 21791 * TODO: you can't just flush these, you have to increase rwnd for one 21792 * thing. For another, how should urgent data interact? 21793 */ 21794 if (fval & FLUSHR) { 21795 *mp->b_rptr = fval & ~FLUSHW; 21796 /* XXX */ 21797 qreply(q, mp); 21798 return; 21799 } 21800 freemsg(mp); 21801 } 21802 21803 /* 21804 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21805 * messages. 21806 */ 21807 static void 21808 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21809 { 21810 mblk_t *mp1; 21811 STRUCT_HANDLE(strbuf, sb); 21812 uint16_t port; 21813 queue_t *q = tcp->tcp_wq; 21814 in6_addr_t v6addr; 21815 ipaddr_t v4addr; 21816 uint32_t flowinfo = 0; 21817 int addrlen; 21818 21819 /* Make sure it is one of ours. */ 21820 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21821 case TI_GETMYNAME: 21822 case TI_GETPEERNAME: 21823 break; 21824 default: 21825 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21826 return; 21827 } 21828 switch (mi_copy_state(q, mp, &mp1)) { 21829 case -1: 21830 return; 21831 case MI_COPY_CASE(MI_COPY_IN, 1): 21832 break; 21833 case MI_COPY_CASE(MI_COPY_OUT, 1): 21834 /* Copy out the strbuf. */ 21835 mi_copyout(q, mp); 21836 return; 21837 case MI_COPY_CASE(MI_COPY_OUT, 2): 21838 /* All done. */ 21839 mi_copy_done(q, mp, 0); 21840 return; 21841 default: 21842 mi_copy_done(q, mp, EPROTO); 21843 return; 21844 } 21845 /* Check alignment of the strbuf */ 21846 if (!OK_32PTR(mp1->b_rptr)) { 21847 mi_copy_done(q, mp, EINVAL); 21848 return; 21849 } 21850 21851 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 21852 (void *)mp1->b_rptr); 21853 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21854 21855 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21856 mi_copy_done(q, mp, EINVAL); 21857 return; 21858 } 21859 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21860 case TI_GETMYNAME: 21861 if (tcp->tcp_family == AF_INET) { 21862 if (tcp->tcp_ipversion == IPV4_VERSION) { 21863 v4addr = tcp->tcp_ipha->ipha_src; 21864 } else { 21865 /* can't return an address in this case */ 21866 v4addr = 0; 21867 } 21868 } else { 21869 /* tcp->tcp_family == AF_INET6 */ 21870 if (tcp->tcp_ipversion == IPV4_VERSION) { 21871 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 21872 &v6addr); 21873 } else { 21874 v6addr = tcp->tcp_ip6h->ip6_src; 21875 } 21876 } 21877 port = tcp->tcp_lport; 21878 break; 21879 case TI_GETPEERNAME: 21880 if (tcp->tcp_family == AF_INET) { 21881 if (tcp->tcp_ipversion == IPV4_VERSION) { 21882 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 21883 v4addr); 21884 } else { 21885 /* can't return an address in this case */ 21886 v4addr = 0; 21887 } 21888 } else { 21889 /* tcp->tcp_family == AF_INET6) */ 21890 v6addr = tcp->tcp_remote_v6; 21891 if (tcp->tcp_ipversion == IPV6_VERSION) { 21892 /* 21893 * No flowinfo if tcp->tcp_ipversion is v4. 21894 * 21895 * flowinfo was already initialized to zero 21896 * where it was declared above, so only 21897 * set it if ipversion is v6. 21898 */ 21899 flowinfo = tcp->tcp_ip6h->ip6_vcf & 21900 ~IPV6_VERS_AND_FLOW_MASK; 21901 } 21902 } 21903 port = tcp->tcp_fport; 21904 break; 21905 default: 21906 mi_copy_done(q, mp, EPROTO); 21907 return; 21908 } 21909 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21910 if (!mp1) 21911 return; 21912 21913 if (tcp->tcp_family == AF_INET) { 21914 sin_t *sin; 21915 21916 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 21917 sin = (sin_t *)mp1->b_rptr; 21918 mp1->b_wptr = (uchar_t *)&sin[1]; 21919 *sin = sin_null; 21920 sin->sin_family = AF_INET; 21921 sin->sin_addr.s_addr = v4addr; 21922 sin->sin_port = port; 21923 } else { 21924 /* tcp->tcp_family == AF_INET6 */ 21925 sin6_t *sin6; 21926 21927 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 21928 sin6 = (sin6_t *)mp1->b_rptr; 21929 mp1->b_wptr = (uchar_t *)&sin6[1]; 21930 *sin6 = sin6_null; 21931 sin6->sin6_family = AF_INET6; 21932 sin6->sin6_flowinfo = flowinfo; 21933 sin6->sin6_addr = v6addr; 21934 sin6->sin6_port = port; 21935 } 21936 /* Copy out the address */ 21937 mi_copyout(q, mp); 21938 } 21939 21940 /* 21941 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21942 * messages. 21943 */ 21944 /* ARGSUSED */ 21945 static void 21946 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21947 { 21948 conn_t *connp = (conn_t *)arg; 21949 tcp_t *tcp = connp->conn_tcp; 21950 queue_t *q = tcp->tcp_wq; 21951 struct iocblk *iocp; 21952 tcp_stack_t *tcps = tcp->tcp_tcps; 21953 21954 ASSERT(DB_TYPE(mp) == M_IOCTL); 21955 /* 21956 * Try and ASSERT the minimum possible references on the 21957 * conn early enough. Since we are executing on write side, 21958 * the connection is obviously not detached and that means 21959 * there is a ref each for TCP and IP. Since we are behind 21960 * the squeue, the minimum references needed are 3. If the 21961 * conn is in classifier hash list, there should be an 21962 * extra ref for that (we check both the possibilities). 21963 */ 21964 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21965 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21966 21967 iocp = (struct iocblk *)mp->b_rptr; 21968 switch (iocp->ioc_cmd) { 21969 case TCP_IOC_DEFAULT_Q: 21970 /* Wants to be the default wq. */ 21971 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21972 iocp->ioc_error = EPERM; 21973 iocp->ioc_count = 0; 21974 mp->b_datap->db_type = M_IOCACK; 21975 qreply(q, mp); 21976 return; 21977 } 21978 tcp_def_q_set(tcp, mp); 21979 return; 21980 case _SIOCSOCKFALLBACK: 21981 /* 21982 * Either sockmod is about to be popped and the socket 21983 * would now be treated as a plain stream, or a module 21984 * is about to be pushed so we could no longer use read- 21985 * side synchronous streams for fused loopback tcp. 21986 * Drain any queued data and disable direct sockfs 21987 * interface from now on. 21988 */ 21989 if (!tcp->tcp_issocket) { 21990 DB_TYPE(mp) = M_IOCNAK; 21991 iocp->ioc_error = EINVAL; 21992 } else { 21993 #ifdef _ILP32 21994 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 21995 #else 21996 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21997 #endif 21998 /* 21999 * Insert this socket into the acceptor hash. 22000 * We might need it for T_CONN_RES message 22001 */ 22002 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22003 22004 if (tcp->tcp_fused) { 22005 /* 22006 * This is a fused loopback tcp; disable 22007 * read-side synchronous streams interface 22008 * and drain any queued data. It is okay 22009 * to do this for non-synchronous streams 22010 * fused tcp as well. 22011 */ 22012 tcp_fuse_disable_pair(tcp, B_FALSE); 22013 } 22014 tcp->tcp_issocket = B_FALSE; 22015 TCP_STAT(tcps, tcp_sock_fallback); 22016 22017 DB_TYPE(mp) = M_IOCACK; 22018 iocp->ioc_error = 0; 22019 } 22020 iocp->ioc_count = 0; 22021 iocp->ioc_rval = 0; 22022 qreply(q, mp); 22023 return; 22024 } 22025 CALL_IP_WPUT(connp, q, mp); 22026 } 22027 22028 /* 22029 * This routine is called by tcp_wput() to handle all TPI requests. 22030 */ 22031 /* ARGSUSED */ 22032 static void 22033 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22034 { 22035 conn_t *connp = (conn_t *)arg; 22036 tcp_t *tcp = connp->conn_tcp; 22037 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22038 uchar_t *rptr; 22039 t_scalar_t type; 22040 int len; 22041 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22042 22043 /* 22044 * Try and ASSERT the minimum possible references on the 22045 * conn early enough. Since we are executing on write side, 22046 * the connection is obviously not detached and that means 22047 * there is a ref each for TCP and IP. Since we are behind 22048 * the squeue, the minimum references needed are 3. If the 22049 * conn is in classifier hash list, there should be an 22050 * extra ref for that (we check both the possibilities). 22051 */ 22052 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22053 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22054 22055 rptr = mp->b_rptr; 22056 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22057 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22058 type = ((union T_primitives *)rptr)->type; 22059 if (type == T_EXDATA_REQ) { 22060 uint32_t msize = msgdsize(mp->b_cont); 22061 22062 len = msize - 1; 22063 if (len < 0) { 22064 freemsg(mp); 22065 return; 22066 } 22067 /* 22068 * Try to force urgent data out on the wire. 22069 * Even if we have unsent data this will 22070 * at least send the urgent flag. 22071 * XXX does not handle more flag correctly. 22072 */ 22073 len += tcp->tcp_unsent; 22074 len += tcp->tcp_snxt; 22075 tcp->tcp_urg = len; 22076 tcp->tcp_valid_bits |= TCP_URG_VALID; 22077 22078 /* Bypass tcp protocol for fused tcp loopback */ 22079 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22080 return; 22081 } else if (type != T_DATA_REQ) { 22082 goto non_urgent_data; 22083 } 22084 /* TODO: options, flags, ... from user */ 22085 /* Set length to zero for reclamation below */ 22086 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22087 freeb(mp); 22088 return; 22089 } else { 22090 if (tcp->tcp_debug) { 22091 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22092 "tcp_wput_proto, dropping one..."); 22093 } 22094 freemsg(mp); 22095 return; 22096 } 22097 22098 non_urgent_data: 22099 22100 switch ((int)tprim->type) { 22101 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22102 /* 22103 * save the kssl_ent_t from the next block, and convert this 22104 * back to a normal bind_req. 22105 */ 22106 if (mp->b_cont != NULL) { 22107 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22108 22109 if (tcp->tcp_kssl_ent != NULL) { 22110 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22111 KSSL_NO_PROXY); 22112 tcp->tcp_kssl_ent = NULL; 22113 } 22114 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22115 sizeof (kssl_ent_t)); 22116 kssl_hold_ent(tcp->tcp_kssl_ent); 22117 freemsg(mp->b_cont); 22118 mp->b_cont = NULL; 22119 } 22120 tprim->type = T_BIND_REQ; 22121 22122 /* FALLTHROUGH */ 22123 case O_T_BIND_REQ: /* bind request */ 22124 case T_BIND_REQ: /* new semantics bind request */ 22125 tcp_bind(tcp, mp); 22126 break; 22127 case T_UNBIND_REQ: /* unbind request */ 22128 tcp_unbind(tcp, mp); 22129 break; 22130 case O_T_CONN_RES: /* old connection response XXX */ 22131 case T_CONN_RES: /* connection response */ 22132 tcp_accept(tcp, mp); 22133 break; 22134 case T_CONN_REQ: /* connection request */ 22135 tcp_connect(tcp, mp); 22136 break; 22137 case T_DISCON_REQ: /* disconnect request */ 22138 tcp_disconnect(tcp, mp); 22139 break; 22140 case T_CAPABILITY_REQ: 22141 tcp_capability_req(tcp, mp); /* capability request */ 22142 break; 22143 case T_INFO_REQ: /* information request */ 22144 tcp_info_req(tcp, mp); 22145 break; 22146 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22147 /* Only IP is allowed to return meaningful value */ 22148 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 22149 break; 22150 case T_OPTMGMT_REQ: 22151 /* 22152 * Note: no support for snmpcom_req() through new 22153 * T_OPTMGMT_REQ. See comments in ip.c 22154 */ 22155 /* Only IP is allowed to return meaningful value */ 22156 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 22157 break; 22158 22159 case T_UNITDATA_REQ: /* unitdata request */ 22160 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22161 break; 22162 case T_ORDREL_REQ: /* orderly release req */ 22163 freemsg(mp); 22164 22165 if (tcp->tcp_fused) 22166 tcp_unfuse(tcp); 22167 22168 if (tcp_xmit_end(tcp) != 0) { 22169 /* 22170 * We were crossing FINs and got a reset from 22171 * the other side. Just ignore it. 22172 */ 22173 if (tcp->tcp_debug) { 22174 (void) strlog(TCP_MOD_ID, 0, 1, 22175 SL_ERROR|SL_TRACE, 22176 "tcp_wput_proto, T_ORDREL_REQ out of " 22177 "state %s", 22178 tcp_display(tcp, NULL, 22179 DISP_ADDR_AND_PORT)); 22180 } 22181 } 22182 break; 22183 case T_ADDR_REQ: 22184 tcp_addr_req(tcp, mp); 22185 break; 22186 default: 22187 if (tcp->tcp_debug) { 22188 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22189 "tcp_wput_proto, bogus TPI msg, type %d", 22190 tprim->type); 22191 } 22192 /* 22193 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22194 * to recover. 22195 */ 22196 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22197 break; 22198 } 22199 } 22200 22201 /* 22202 * The TCP write service routine should never be called... 22203 */ 22204 /* ARGSUSED */ 22205 static void 22206 tcp_wsrv(queue_t *q) 22207 { 22208 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22209 22210 TCP_STAT(tcps, tcp_wsrv_called); 22211 } 22212 22213 /* Non overlapping byte exchanger */ 22214 static void 22215 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22216 { 22217 uchar_t uch; 22218 22219 while (len-- > 0) { 22220 uch = a[len]; 22221 a[len] = b[len]; 22222 b[len] = uch; 22223 } 22224 } 22225 22226 /* 22227 * Send out a control packet on the tcp connection specified. This routine 22228 * is typically called where we need a simple ACK or RST generated. 22229 */ 22230 static void 22231 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22232 { 22233 uchar_t *rptr; 22234 tcph_t *tcph; 22235 ipha_t *ipha = NULL; 22236 ip6_t *ip6h = NULL; 22237 uint32_t sum; 22238 int tcp_hdr_len; 22239 int tcp_ip_hdr_len; 22240 mblk_t *mp; 22241 tcp_stack_t *tcps = tcp->tcp_tcps; 22242 22243 /* 22244 * Save sum for use in source route later. 22245 */ 22246 ASSERT(tcp != NULL); 22247 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22248 tcp_hdr_len = tcp->tcp_hdr_len; 22249 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22250 22251 /* If a text string is passed in with the request, pass it to strlog. */ 22252 if (str != NULL && tcp->tcp_debug) { 22253 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22254 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22255 str, seq, ack, ctl); 22256 } 22257 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22258 BPRI_MED); 22259 if (mp == NULL) { 22260 return; 22261 } 22262 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22263 mp->b_rptr = rptr; 22264 mp->b_wptr = &rptr[tcp_hdr_len]; 22265 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22266 22267 if (tcp->tcp_ipversion == IPV4_VERSION) { 22268 ipha = (ipha_t *)rptr; 22269 ipha->ipha_length = htons(tcp_hdr_len); 22270 } else { 22271 ip6h = (ip6_t *)rptr; 22272 ASSERT(tcp != NULL); 22273 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22274 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22275 } 22276 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22277 tcph->th_flags[0] = (uint8_t)ctl; 22278 if (ctl & TH_RST) { 22279 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22280 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22281 /* 22282 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22283 */ 22284 if (tcp->tcp_snd_ts_ok && 22285 tcp->tcp_state > TCPS_SYN_SENT) { 22286 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22287 *(mp->b_wptr) = TCPOPT_EOL; 22288 if (tcp->tcp_ipversion == IPV4_VERSION) { 22289 ipha->ipha_length = htons(tcp_hdr_len - 22290 TCPOPT_REAL_TS_LEN); 22291 } else { 22292 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22293 TCPOPT_REAL_TS_LEN); 22294 } 22295 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22296 sum -= TCPOPT_REAL_TS_LEN; 22297 } 22298 } 22299 if (ctl & TH_ACK) { 22300 if (tcp->tcp_snd_ts_ok) { 22301 U32_TO_BE32(lbolt, 22302 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22303 U32_TO_BE32(tcp->tcp_ts_recent, 22304 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22305 } 22306 22307 /* Update the latest receive window size in TCP header. */ 22308 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22309 tcph->th_win); 22310 tcp->tcp_rack = ack; 22311 tcp->tcp_rack_cnt = 0; 22312 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22313 } 22314 BUMP_LOCAL(tcp->tcp_obsegs); 22315 U32_TO_BE32(seq, tcph->th_seq); 22316 U32_TO_BE32(ack, tcph->th_ack); 22317 /* 22318 * Include the adjustment for a source route if any. 22319 */ 22320 sum = (sum >> 16) + (sum & 0xFFFF); 22321 U16_TO_BE16(sum, tcph->th_sum); 22322 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22323 tcp_send_data(tcp, tcp->tcp_wq, mp); 22324 } 22325 22326 /* 22327 * If this routine returns B_TRUE, TCP can generate a RST in response 22328 * to a segment. If it returns B_FALSE, TCP should not respond. 22329 */ 22330 static boolean_t 22331 tcp_send_rst_chk(tcp_stack_t *tcps) 22332 { 22333 clock_t now; 22334 22335 /* 22336 * TCP needs to protect itself from generating too many RSTs. 22337 * This can be a DoS attack by sending us random segments 22338 * soliciting RSTs. 22339 * 22340 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22341 * in each 1 second interval. In this way, TCP still generate 22342 * RSTs in normal cases but when under attack, the impact is 22343 * limited. 22344 */ 22345 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22346 now = lbolt; 22347 /* lbolt can wrap around. */ 22348 if ((tcps->tcps_last_rst_intrvl > now) || 22349 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22350 1*SECONDS)) { 22351 tcps->tcps_last_rst_intrvl = now; 22352 tcps->tcps_rst_cnt = 1; 22353 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22354 return (B_FALSE); 22355 } 22356 } 22357 return (B_TRUE); 22358 } 22359 22360 /* 22361 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22362 */ 22363 static void 22364 tcp_ip_ire_mark_advice(tcp_t *tcp) 22365 { 22366 mblk_t *mp; 22367 ipic_t *ipic; 22368 22369 if (tcp->tcp_ipversion == IPV4_VERSION) { 22370 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22371 &ipic); 22372 } else { 22373 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22374 &ipic); 22375 } 22376 if (mp == NULL) 22377 return; 22378 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22379 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22380 } 22381 22382 /* 22383 * Return an IP advice ioctl mblk and set ipic to be the pointer 22384 * to the advice structure. 22385 */ 22386 static mblk_t * 22387 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22388 { 22389 struct iocblk *ioc; 22390 mblk_t *mp, *mp1; 22391 22392 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22393 if (mp == NULL) 22394 return (NULL); 22395 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22396 *ipic = (ipic_t *)mp->b_rptr; 22397 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22398 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22399 22400 bcopy(addr, *ipic + 1, addr_len); 22401 22402 (*ipic)->ipic_addr_length = addr_len; 22403 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22404 22405 mp1 = mkiocb(IP_IOCTL); 22406 if (mp1 == NULL) { 22407 freemsg(mp); 22408 return (NULL); 22409 } 22410 mp1->b_cont = mp; 22411 ioc = (struct iocblk *)mp1->b_rptr; 22412 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22413 22414 return (mp1); 22415 } 22416 22417 /* 22418 * Generate a reset based on an inbound packet for which there is no active 22419 * tcp state that we can find. 22420 * 22421 * IPSEC NOTE : Try to send the reply with the same protection as it came 22422 * in. We still have the ipsec_mp that the packet was attached to. Thus 22423 * the packet will go out at the same level of protection as it came in by 22424 * converting the IPSEC_IN to IPSEC_OUT. 22425 */ 22426 static void 22427 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22428 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22429 tcp_stack_t *tcps) 22430 { 22431 ipha_t *ipha = NULL; 22432 ip6_t *ip6h = NULL; 22433 ushort_t len; 22434 tcph_t *tcph; 22435 int i; 22436 mblk_t *ipsec_mp; 22437 boolean_t mctl_present; 22438 ipic_t *ipic; 22439 ipaddr_t v4addr; 22440 in6_addr_t v6addr; 22441 int addr_len; 22442 void *addr; 22443 queue_t *q = tcps->tcps_g_q; 22444 tcp_t *tcp; 22445 cred_t *cr; 22446 mblk_t *nmp; 22447 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22448 22449 if (tcps->tcps_g_q == NULL) { 22450 /* 22451 * For non-zero stackids the default queue isn't created 22452 * until the first open, thus there can be a need to send 22453 * a reset before then. But we can't do that, hence we just 22454 * drop the packet. Later during boot, when the default queue 22455 * has been setup, a retransmitted packet from the peer 22456 * will result in a reset. 22457 */ 22458 ASSERT(tcps->tcps_netstack->netstack_stackid != 22459 GLOBAL_NETSTACKID); 22460 freemsg(mp); 22461 return; 22462 } 22463 22464 tcp = Q_TO_TCP(q); 22465 22466 if (!tcp_send_rst_chk(tcps)) { 22467 tcps->tcps_rst_unsent++; 22468 freemsg(mp); 22469 return; 22470 } 22471 22472 if (mp->b_datap->db_type == M_CTL) { 22473 ipsec_mp = mp; 22474 mp = mp->b_cont; 22475 mctl_present = B_TRUE; 22476 } else { 22477 ipsec_mp = mp; 22478 mctl_present = B_FALSE; 22479 } 22480 22481 if (str && q && tcps->tcps_dbg) { 22482 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22483 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22484 "flags 0x%x", 22485 str, seq, ack, ctl); 22486 } 22487 if (mp->b_datap->db_ref != 1) { 22488 mblk_t *mp1 = copyb(mp); 22489 freemsg(mp); 22490 mp = mp1; 22491 if (!mp) { 22492 if (mctl_present) 22493 freeb(ipsec_mp); 22494 return; 22495 } else { 22496 if (mctl_present) { 22497 ipsec_mp->b_cont = mp; 22498 } else { 22499 ipsec_mp = mp; 22500 } 22501 } 22502 } else if (mp->b_cont) { 22503 freemsg(mp->b_cont); 22504 mp->b_cont = NULL; 22505 } 22506 /* 22507 * We skip reversing source route here. 22508 * (for now we replace all IP options with EOL) 22509 */ 22510 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22511 ipha = (ipha_t *)mp->b_rptr; 22512 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22513 mp->b_rptr[i] = IPOPT_EOL; 22514 /* 22515 * Make sure that src address isn't flagrantly invalid. 22516 * Not all broadcast address checking for the src address 22517 * is possible, since we don't know the netmask of the src 22518 * addr. No check for destination address is done, since 22519 * IP will not pass up a packet with a broadcast dest 22520 * address to TCP. Similar checks are done below for IPv6. 22521 */ 22522 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22523 CLASSD(ipha->ipha_src)) { 22524 freemsg(ipsec_mp); 22525 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22526 return; 22527 } 22528 } else { 22529 ip6h = (ip6_t *)mp->b_rptr; 22530 22531 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22532 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22533 freemsg(ipsec_mp); 22534 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22535 return; 22536 } 22537 22538 /* Remove any extension headers assuming partial overlay */ 22539 if (ip_hdr_len > IPV6_HDR_LEN) { 22540 uint8_t *to; 22541 22542 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22543 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22544 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22545 ip_hdr_len = IPV6_HDR_LEN; 22546 ip6h = (ip6_t *)mp->b_rptr; 22547 ip6h->ip6_nxt = IPPROTO_TCP; 22548 } 22549 } 22550 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22551 if (tcph->th_flags[0] & TH_RST) { 22552 freemsg(ipsec_mp); 22553 return; 22554 } 22555 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22556 len = ip_hdr_len + sizeof (tcph_t); 22557 mp->b_wptr = &mp->b_rptr[len]; 22558 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22559 ipha->ipha_length = htons(len); 22560 /* Swap addresses */ 22561 v4addr = ipha->ipha_src; 22562 ipha->ipha_src = ipha->ipha_dst; 22563 ipha->ipha_dst = v4addr; 22564 ipha->ipha_ident = 0; 22565 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22566 addr_len = IP_ADDR_LEN; 22567 addr = &v4addr; 22568 } else { 22569 /* No ip6i_t in this case */ 22570 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22571 /* Swap addresses */ 22572 v6addr = ip6h->ip6_src; 22573 ip6h->ip6_src = ip6h->ip6_dst; 22574 ip6h->ip6_dst = v6addr; 22575 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22576 addr_len = IPV6_ADDR_LEN; 22577 addr = &v6addr; 22578 } 22579 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22580 U32_TO_BE32(ack, tcph->th_ack); 22581 U32_TO_BE32(seq, tcph->th_seq); 22582 U16_TO_BE16(0, tcph->th_win); 22583 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22584 tcph->th_flags[0] = (uint8_t)ctl; 22585 if (ctl & TH_RST) { 22586 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22587 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22588 } 22589 22590 /* IP trusts us to set up labels when required. */ 22591 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 22592 crgetlabel(cr) != NULL) { 22593 int err, adjust; 22594 22595 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22596 err = tsol_check_label(cr, &mp, &adjust, 22597 tcp->tcp_connp->conn_mac_exempt, 22598 tcps->tcps_netstack->netstack_ip); 22599 else 22600 err = tsol_check_label_v6(cr, &mp, &adjust, 22601 tcp->tcp_connp->conn_mac_exempt, 22602 tcps->tcps_netstack->netstack_ip); 22603 if (mctl_present) 22604 ipsec_mp->b_cont = mp; 22605 else 22606 ipsec_mp = mp; 22607 if (err != 0) { 22608 freemsg(ipsec_mp); 22609 return; 22610 } 22611 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22612 ipha = (ipha_t *)mp->b_rptr; 22613 adjust += ntohs(ipha->ipha_length); 22614 ipha->ipha_length = htons(adjust); 22615 } else { 22616 ip6h = (ip6_t *)mp->b_rptr; 22617 } 22618 } 22619 22620 if (mctl_present) { 22621 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22622 22623 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22624 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22625 return; 22626 } 22627 } 22628 if (zoneid == ALL_ZONES) 22629 zoneid = GLOBAL_ZONEID; 22630 22631 /* Add the zoneid so ip_output routes it properly */ 22632 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22633 freemsg(ipsec_mp); 22634 return; 22635 } 22636 ipsec_mp = nmp; 22637 22638 /* 22639 * NOTE: one might consider tracing a TCP packet here, but 22640 * this function has no active TCP state and no tcp structure 22641 * that has a trace buffer. If we traced here, we would have 22642 * to keep a local trace buffer in tcp_record_trace(). 22643 * 22644 * TSol note: The mblk that contains the incoming packet was 22645 * reused by tcp_xmit_listener_reset, so it already contains 22646 * the right credentials and we don't need to call mblk_setcred. 22647 * Also the conn's cred is not right since it is associated 22648 * with tcps_g_q. 22649 */ 22650 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22651 22652 /* 22653 * Tell IP to mark the IRE used for this destination temporary. 22654 * This way, we can limit our exposure to DoS attack because IP 22655 * creates an IRE for each destination. If there are too many, 22656 * the time to do any routing lookup will be extremely long. And 22657 * the lookup can be in interrupt context. 22658 * 22659 * Note that in normal circumstances, this marking should not 22660 * affect anything. It would be nice if only 1 message is 22661 * needed to inform IP that the IRE created for this RST should 22662 * not be added to the cache table. But there is currently 22663 * not such communication mechanism between TCP and IP. So 22664 * the best we can do now is to send the advice ioctl to IP 22665 * to mark the IRE temporary. 22666 */ 22667 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22668 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22669 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22670 } 22671 } 22672 22673 /* 22674 * Initiate closedown sequence on an active connection. (May be called as 22675 * writer.) Return value zero for OK return, non-zero for error return. 22676 */ 22677 static int 22678 tcp_xmit_end(tcp_t *tcp) 22679 { 22680 ipic_t *ipic; 22681 mblk_t *mp; 22682 tcp_stack_t *tcps = tcp->tcp_tcps; 22683 22684 if (tcp->tcp_state < TCPS_SYN_RCVD || 22685 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22686 /* 22687 * Invalid state, only states TCPS_SYN_RCVD, 22688 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22689 */ 22690 return (-1); 22691 } 22692 22693 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22694 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22695 /* 22696 * If there is nothing more unsent, send the FIN now. 22697 * Otherwise, it will go out with the last segment. 22698 */ 22699 if (tcp->tcp_unsent == 0) { 22700 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22701 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22702 22703 if (mp) { 22704 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22705 tcp_send_data(tcp, tcp->tcp_wq, mp); 22706 } else { 22707 /* 22708 * Couldn't allocate msg. Pretend we got it out. 22709 * Wait for rexmit timeout. 22710 */ 22711 tcp->tcp_snxt = tcp->tcp_fss + 1; 22712 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22713 } 22714 22715 /* 22716 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22717 * changed. 22718 */ 22719 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22720 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22721 } 22722 } else { 22723 /* 22724 * If tcp->tcp_cork is set, then the data will not get sent, 22725 * so we have to check that and unset it first. 22726 */ 22727 if (tcp->tcp_cork) 22728 tcp->tcp_cork = B_FALSE; 22729 tcp_wput_data(tcp, NULL, B_FALSE); 22730 } 22731 22732 /* 22733 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22734 * is 0, don't update the cache. 22735 */ 22736 if (tcps->tcps_rtt_updates == 0 || 22737 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22738 return (0); 22739 22740 /* 22741 * NOTE: should not update if source routes i.e. if tcp_remote if 22742 * different from the destination. 22743 */ 22744 if (tcp->tcp_ipversion == IPV4_VERSION) { 22745 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22746 return (0); 22747 } 22748 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22749 &ipic); 22750 } else { 22751 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22752 &tcp->tcp_ip6h->ip6_dst))) { 22753 return (0); 22754 } 22755 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22756 &ipic); 22757 } 22758 22759 /* Record route attributes in the IRE for use by future connections. */ 22760 if (mp == NULL) 22761 return (0); 22762 22763 /* 22764 * We do not have a good algorithm to update ssthresh at this time. 22765 * So don't do any update. 22766 */ 22767 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22768 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22769 22770 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22771 return (0); 22772 } 22773 22774 /* 22775 * Generate a "no listener here" RST in response to an "unknown" segment. 22776 * Note that we are reusing the incoming mp to construct the outgoing 22777 * RST. 22778 */ 22779 void 22780 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22781 tcp_stack_t *tcps) 22782 { 22783 uchar_t *rptr; 22784 uint32_t seg_len; 22785 tcph_t *tcph; 22786 uint32_t seg_seq; 22787 uint32_t seg_ack; 22788 uint_t flags; 22789 mblk_t *ipsec_mp; 22790 ipha_t *ipha; 22791 ip6_t *ip6h; 22792 boolean_t mctl_present = B_FALSE; 22793 boolean_t check = B_TRUE; 22794 boolean_t policy_present; 22795 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22796 22797 TCP_STAT(tcps, tcp_no_listener); 22798 22799 ipsec_mp = mp; 22800 22801 if (mp->b_datap->db_type == M_CTL) { 22802 ipsec_in_t *ii; 22803 22804 mctl_present = B_TRUE; 22805 mp = mp->b_cont; 22806 22807 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22808 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22809 if (ii->ipsec_in_dont_check) { 22810 check = B_FALSE; 22811 if (!ii->ipsec_in_secure) { 22812 freeb(ipsec_mp); 22813 mctl_present = B_FALSE; 22814 ipsec_mp = mp; 22815 } 22816 } 22817 } 22818 22819 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22820 policy_present = ipss->ipsec_inbound_v4_policy_present; 22821 ipha = (ipha_t *)mp->b_rptr; 22822 ip6h = NULL; 22823 } else { 22824 policy_present = ipss->ipsec_inbound_v6_policy_present; 22825 ipha = NULL; 22826 ip6h = (ip6_t *)mp->b_rptr; 22827 } 22828 22829 if (check && policy_present) { 22830 /* 22831 * The conn_t parameter is NULL because we already know 22832 * nobody's home. 22833 */ 22834 ipsec_mp = ipsec_check_global_policy( 22835 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22836 tcps->tcps_netstack); 22837 if (ipsec_mp == NULL) 22838 return; 22839 } 22840 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22841 DTRACE_PROBE2( 22842 tx__ip__log__error__nolistener__tcp, 22843 char *, "Could not reply with RST to mp(1)", 22844 mblk_t *, mp); 22845 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22846 freemsg(ipsec_mp); 22847 return; 22848 } 22849 22850 rptr = mp->b_rptr; 22851 22852 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22853 seg_seq = BE32_TO_U32(tcph->th_seq); 22854 seg_ack = BE32_TO_U32(tcph->th_ack); 22855 flags = tcph->th_flags[0]; 22856 22857 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22858 if (flags & TH_RST) { 22859 freemsg(ipsec_mp); 22860 } else if (flags & TH_ACK) { 22861 tcp_xmit_early_reset("no tcp, reset", 22862 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps); 22863 } else { 22864 if (flags & TH_SYN) { 22865 seg_len++; 22866 } else { 22867 /* 22868 * Here we violate the RFC. Note that a normal 22869 * TCP will never send a segment without the ACK 22870 * flag, except for RST or SYN segment. This 22871 * segment is neither. Just drop it on the 22872 * floor. 22873 */ 22874 freemsg(ipsec_mp); 22875 tcps->tcps_rst_unsent++; 22876 return; 22877 } 22878 22879 tcp_xmit_early_reset("no tcp, reset/ack", 22880 ipsec_mp, 0, seg_seq + seg_len, 22881 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps); 22882 } 22883 } 22884 22885 /* 22886 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22887 * ip and tcp header ready to pass down to IP. If the mp passed in is 22888 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22889 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22890 * otherwise it will dup partial mblks.) 22891 * Otherwise, an appropriate ACK packet will be generated. This 22892 * routine is not usually called to send new data for the first time. It 22893 * is mostly called out of the timer for retransmits, and to generate ACKs. 22894 * 22895 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22896 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22897 * of the original mblk chain will be returned in *offset and *end_mp. 22898 */ 22899 mblk_t * 22900 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22901 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22902 boolean_t rexmit) 22903 { 22904 int data_length; 22905 int32_t off = 0; 22906 uint_t flags; 22907 mblk_t *mp1; 22908 mblk_t *mp2; 22909 uchar_t *rptr; 22910 tcph_t *tcph; 22911 int32_t num_sack_blk = 0; 22912 int32_t sack_opt_len = 0; 22913 tcp_stack_t *tcps = tcp->tcp_tcps; 22914 22915 /* Allocate for our maximum TCP header + link-level */ 22916 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22917 tcps->tcps_wroff_xtra, BPRI_MED); 22918 if (!mp1) 22919 return (NULL); 22920 data_length = 0; 22921 22922 /* 22923 * Note that tcp_mss has been adjusted to take into account the 22924 * timestamp option if applicable. Because SACK options do not 22925 * appear in every TCP segments and they are of variable lengths, 22926 * they cannot be included in tcp_mss. Thus we need to calculate 22927 * the actual segment length when we need to send a segment which 22928 * includes SACK options. 22929 */ 22930 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22931 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22932 tcp->tcp_num_sack_blk); 22933 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22934 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22935 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22936 max_to_send -= sack_opt_len; 22937 } 22938 22939 if (offset != NULL) { 22940 off = *offset; 22941 /* We use offset as an indicator that end_mp is not NULL. */ 22942 *end_mp = NULL; 22943 } 22944 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22945 /* This could be faster with cooperation from downstream */ 22946 if (mp2 != mp1 && !sendall && 22947 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22948 max_to_send) 22949 /* 22950 * Don't send the next mblk since the whole mblk 22951 * does not fit. 22952 */ 22953 break; 22954 mp2->b_cont = dupb(mp); 22955 mp2 = mp2->b_cont; 22956 if (!mp2) { 22957 freemsg(mp1); 22958 return (NULL); 22959 } 22960 mp2->b_rptr += off; 22961 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22962 (uintptr_t)INT_MAX); 22963 22964 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22965 if (data_length > max_to_send) { 22966 mp2->b_wptr -= data_length - max_to_send; 22967 data_length = max_to_send; 22968 off = mp2->b_wptr - mp->b_rptr; 22969 break; 22970 } else { 22971 off = 0; 22972 } 22973 } 22974 if (offset != NULL) { 22975 *offset = off; 22976 *end_mp = mp; 22977 } 22978 if (seg_len != NULL) { 22979 *seg_len = data_length; 22980 } 22981 22982 /* Update the latest receive window size in TCP header. */ 22983 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22984 tcp->tcp_tcph->th_win); 22985 22986 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22987 mp1->b_rptr = rptr; 22988 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22989 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22990 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22991 U32_TO_ABE32(seq, tcph->th_seq); 22992 22993 /* 22994 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22995 * that this function was called from tcp_wput_data. Thus, when called 22996 * to retransmit data the setting of the PUSH bit may appear some 22997 * what random in that it might get set when it should not. This 22998 * should not pose any performance issues. 22999 */ 23000 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23001 tcp->tcp_unsent == data_length)) { 23002 flags = TH_ACK | TH_PUSH; 23003 } else { 23004 flags = TH_ACK; 23005 } 23006 23007 if (tcp->tcp_ecn_ok) { 23008 if (tcp->tcp_ecn_echo_on) 23009 flags |= TH_ECE; 23010 23011 /* 23012 * Only set ECT bit and ECN_CWR if a segment contains new data. 23013 * There is no TCP flow control for non-data segments, and 23014 * only data segment is transmitted reliably. 23015 */ 23016 if (data_length > 0 && !rexmit) { 23017 SET_ECT(tcp, rptr); 23018 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23019 flags |= TH_CWR; 23020 tcp->tcp_ecn_cwr_sent = B_TRUE; 23021 } 23022 } 23023 } 23024 23025 if (tcp->tcp_valid_bits) { 23026 uint32_t u1; 23027 23028 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23029 seq == tcp->tcp_iss) { 23030 uchar_t *wptr; 23031 23032 /* 23033 * If TCP_ISS_VALID and the seq number is tcp_iss, 23034 * TCP can only be in SYN-SENT, SYN-RCVD or 23035 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23036 * our SYN is not ack'ed but the app closes this 23037 * TCP connection. 23038 */ 23039 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23040 tcp->tcp_state == TCPS_SYN_RCVD || 23041 tcp->tcp_state == TCPS_FIN_WAIT_1); 23042 23043 /* 23044 * Tack on the MSS option. It is always needed 23045 * for both active and passive open. 23046 * 23047 * MSS option value should be interface MTU - MIN 23048 * TCP/IP header according to RFC 793 as it means 23049 * the maximum segment size TCP can receive. But 23050 * to get around some broken middle boxes/end hosts 23051 * out there, we allow the option value to be the 23052 * same as the MSS option size on the peer side. 23053 * In this way, the other side will not send 23054 * anything larger than they can receive. 23055 * 23056 * Note that for SYN_SENT state, the ndd param 23057 * tcp_use_smss_as_mss_opt has no effect as we 23058 * don't know the peer's MSS option value. So 23059 * the only case we need to take care of is in 23060 * SYN_RCVD state, which is done later. 23061 */ 23062 wptr = mp1->b_wptr; 23063 wptr[0] = TCPOPT_MAXSEG; 23064 wptr[1] = TCPOPT_MAXSEG_LEN; 23065 wptr += 2; 23066 u1 = tcp->tcp_if_mtu - 23067 (tcp->tcp_ipversion == IPV4_VERSION ? 23068 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23069 TCP_MIN_HEADER_LENGTH; 23070 U16_TO_BE16(u1, wptr); 23071 mp1->b_wptr = wptr + 2; 23072 /* Update the offset to cover the additional word */ 23073 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23074 23075 /* 23076 * Note that the following way of filling in 23077 * TCP options are not optimal. Some NOPs can 23078 * be saved. But there is no need at this time 23079 * to optimize it. When it is needed, we will 23080 * do it. 23081 */ 23082 switch (tcp->tcp_state) { 23083 case TCPS_SYN_SENT: 23084 flags = TH_SYN; 23085 23086 if (tcp->tcp_snd_ts_ok) { 23087 uint32_t llbolt = (uint32_t)lbolt; 23088 23089 wptr = mp1->b_wptr; 23090 wptr[0] = TCPOPT_NOP; 23091 wptr[1] = TCPOPT_NOP; 23092 wptr[2] = TCPOPT_TSTAMP; 23093 wptr[3] = TCPOPT_TSTAMP_LEN; 23094 wptr += 4; 23095 U32_TO_BE32(llbolt, wptr); 23096 wptr += 4; 23097 ASSERT(tcp->tcp_ts_recent == 0); 23098 U32_TO_BE32(0L, wptr); 23099 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23100 tcph->th_offset_and_rsrvd[0] += 23101 (3 << 4); 23102 } 23103 23104 /* 23105 * Set up all the bits to tell other side 23106 * we are ECN capable. 23107 */ 23108 if (tcp->tcp_ecn_ok) { 23109 flags |= (TH_ECE | TH_CWR); 23110 } 23111 break; 23112 case TCPS_SYN_RCVD: 23113 flags |= TH_SYN; 23114 23115 /* 23116 * Reset the MSS option value to be SMSS 23117 * We should probably add back the bytes 23118 * for timestamp option and IPsec. We 23119 * don't do that as this is a workaround 23120 * for broken middle boxes/end hosts, it 23121 * is better for us to be more cautious. 23122 * They may not take these things into 23123 * account in their SMSS calculation. Thus 23124 * the peer's calculated SMSS may be smaller 23125 * than what it can be. This should be OK. 23126 */ 23127 if (tcps->tcps_use_smss_as_mss_opt) { 23128 u1 = tcp->tcp_mss; 23129 U16_TO_BE16(u1, wptr); 23130 } 23131 23132 /* 23133 * If the other side is ECN capable, reply 23134 * that we are also ECN capable. 23135 */ 23136 if (tcp->tcp_ecn_ok) 23137 flags |= TH_ECE; 23138 break; 23139 default: 23140 /* 23141 * The above ASSERT() makes sure that this 23142 * must be FIN-WAIT-1 state. Our SYN has 23143 * not been ack'ed so retransmit it. 23144 */ 23145 flags |= TH_SYN; 23146 break; 23147 } 23148 23149 if (tcp->tcp_snd_ws_ok) { 23150 wptr = mp1->b_wptr; 23151 wptr[0] = TCPOPT_NOP; 23152 wptr[1] = TCPOPT_WSCALE; 23153 wptr[2] = TCPOPT_WS_LEN; 23154 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23155 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23156 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23157 } 23158 23159 if (tcp->tcp_snd_sack_ok) { 23160 wptr = mp1->b_wptr; 23161 wptr[0] = TCPOPT_NOP; 23162 wptr[1] = TCPOPT_NOP; 23163 wptr[2] = TCPOPT_SACK_PERMITTED; 23164 wptr[3] = TCPOPT_SACK_OK_LEN; 23165 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23166 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23167 } 23168 23169 /* allocb() of adequate mblk assures space */ 23170 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23171 (uintptr_t)INT_MAX); 23172 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23173 /* 23174 * Get IP set to checksum on our behalf 23175 * Include the adjustment for a source route if any. 23176 */ 23177 u1 += tcp->tcp_sum; 23178 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23179 U16_TO_BE16(u1, tcph->th_sum); 23180 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23181 } 23182 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23183 (seq + data_length) == tcp->tcp_fss) { 23184 if (!tcp->tcp_fin_acked) { 23185 flags |= TH_FIN; 23186 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23187 } 23188 if (!tcp->tcp_fin_sent) { 23189 tcp->tcp_fin_sent = B_TRUE; 23190 switch (tcp->tcp_state) { 23191 case TCPS_SYN_RCVD: 23192 case TCPS_ESTABLISHED: 23193 tcp->tcp_state = TCPS_FIN_WAIT_1; 23194 break; 23195 case TCPS_CLOSE_WAIT: 23196 tcp->tcp_state = TCPS_LAST_ACK; 23197 break; 23198 } 23199 if (tcp->tcp_suna == tcp->tcp_snxt) 23200 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23201 tcp->tcp_snxt = tcp->tcp_fss + 1; 23202 } 23203 } 23204 /* 23205 * Note the trick here. u1 is unsigned. When tcp_urg 23206 * is smaller than seq, u1 will become a very huge value. 23207 * So the comparison will fail. Also note that tcp_urp 23208 * should be positive, see RFC 793 page 17. 23209 */ 23210 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23211 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23212 u1 < (uint32_t)(64 * 1024)) { 23213 flags |= TH_URG; 23214 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23215 U32_TO_ABE16(u1, tcph->th_urp); 23216 } 23217 } 23218 tcph->th_flags[0] = (uchar_t)flags; 23219 tcp->tcp_rack = tcp->tcp_rnxt; 23220 tcp->tcp_rack_cnt = 0; 23221 23222 if (tcp->tcp_snd_ts_ok) { 23223 if (tcp->tcp_state != TCPS_SYN_SENT) { 23224 uint32_t llbolt = (uint32_t)lbolt; 23225 23226 U32_TO_BE32(llbolt, 23227 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23228 U32_TO_BE32(tcp->tcp_ts_recent, 23229 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23230 } 23231 } 23232 23233 if (num_sack_blk > 0) { 23234 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23235 sack_blk_t *tmp; 23236 int32_t i; 23237 23238 wptr[0] = TCPOPT_NOP; 23239 wptr[1] = TCPOPT_NOP; 23240 wptr[2] = TCPOPT_SACK; 23241 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23242 sizeof (sack_blk_t); 23243 wptr += TCPOPT_REAL_SACK_LEN; 23244 23245 tmp = tcp->tcp_sack_list; 23246 for (i = 0; i < num_sack_blk; i++) { 23247 U32_TO_BE32(tmp[i].begin, wptr); 23248 wptr += sizeof (tcp_seq); 23249 U32_TO_BE32(tmp[i].end, wptr); 23250 wptr += sizeof (tcp_seq); 23251 } 23252 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23253 } 23254 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23255 data_length += (int)(mp1->b_wptr - rptr); 23256 if (tcp->tcp_ipversion == IPV4_VERSION) { 23257 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23258 } else { 23259 ip6_t *ip6 = (ip6_t *)(rptr + 23260 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23261 sizeof (ip6i_t) : 0)); 23262 23263 ip6->ip6_plen = htons(data_length - 23264 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23265 } 23266 23267 /* 23268 * Prime pump for IP 23269 * Include the adjustment for a source route if any. 23270 */ 23271 data_length -= tcp->tcp_ip_hdr_len; 23272 data_length += tcp->tcp_sum; 23273 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23274 U16_TO_ABE16(data_length, tcph->th_sum); 23275 if (tcp->tcp_ip_forward_progress) { 23276 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23277 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23278 tcp->tcp_ip_forward_progress = B_FALSE; 23279 } 23280 return (mp1); 23281 } 23282 23283 /* This function handles the push timeout. */ 23284 void 23285 tcp_push_timer(void *arg) 23286 { 23287 conn_t *connp = (conn_t *)arg; 23288 tcp_t *tcp = connp->conn_tcp; 23289 tcp_stack_t *tcps = tcp->tcp_tcps; 23290 23291 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23292 23293 ASSERT(tcp->tcp_listener == NULL); 23294 23295 /* 23296 * We need to plug synchronous streams during our drain to prevent 23297 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23298 */ 23299 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23300 tcp->tcp_push_tid = 0; 23301 if ((tcp->tcp_rcv_list != NULL) && 23302 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 23303 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23304 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23305 } 23306 23307 /* 23308 * This function handles delayed ACK timeout. 23309 */ 23310 static void 23311 tcp_ack_timer(void *arg) 23312 { 23313 conn_t *connp = (conn_t *)arg; 23314 tcp_t *tcp = connp->conn_tcp; 23315 mblk_t *mp; 23316 tcp_stack_t *tcps = tcp->tcp_tcps; 23317 23318 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23319 23320 tcp->tcp_ack_tid = 0; 23321 23322 if (tcp->tcp_fused) 23323 return; 23324 23325 /* 23326 * Do not send ACK if there is no outstanding unack'ed data. 23327 */ 23328 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23329 return; 23330 } 23331 23332 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23333 /* 23334 * Make sure we don't allow deferred ACKs to result in 23335 * timer-based ACKing. If we have held off an ACK 23336 * when there was more than an mss here, and the timer 23337 * goes off, we have to worry about the possibility 23338 * that the sender isn't doing slow-start, or is out 23339 * of step with us for some other reason. We fall 23340 * permanently back in the direction of 23341 * ACK-every-other-packet as suggested in RFC 1122. 23342 */ 23343 if (tcp->tcp_rack_abs_max > 2) 23344 tcp->tcp_rack_abs_max--; 23345 tcp->tcp_rack_cur_max = 2; 23346 } 23347 mp = tcp_ack_mp(tcp); 23348 23349 if (mp != NULL) { 23350 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 23351 BUMP_LOCAL(tcp->tcp_obsegs); 23352 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23353 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23354 tcp_send_data(tcp, tcp->tcp_wq, mp); 23355 } 23356 } 23357 23358 23359 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23360 static mblk_t * 23361 tcp_ack_mp(tcp_t *tcp) 23362 { 23363 uint32_t seq_no; 23364 tcp_stack_t *tcps = tcp->tcp_tcps; 23365 23366 /* 23367 * There are a few cases to be considered while setting the sequence no. 23368 * Essentially, we can come here while processing an unacceptable pkt 23369 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23370 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23371 * If we are here for a zero window probe, stick with suna. In all 23372 * other cases, we check if suna + swnd encompasses snxt and set 23373 * the sequence number to snxt, if so. If snxt falls outside the 23374 * window (the receiver probably shrunk its window), we will go with 23375 * suna + swnd, otherwise the sequence no will be unacceptable to the 23376 * receiver. 23377 */ 23378 if (tcp->tcp_zero_win_probe) { 23379 seq_no = tcp->tcp_suna; 23380 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23381 ASSERT(tcp->tcp_swnd == 0); 23382 seq_no = tcp->tcp_snxt; 23383 } else { 23384 seq_no = SEQ_GT(tcp->tcp_snxt, 23385 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23386 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23387 } 23388 23389 if (tcp->tcp_valid_bits) { 23390 /* 23391 * For the complex case where we have to send some 23392 * controls (FIN or SYN), let tcp_xmit_mp do it. 23393 */ 23394 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23395 NULL, B_FALSE)); 23396 } else { 23397 /* Generate a simple ACK */ 23398 int data_length; 23399 uchar_t *rptr; 23400 tcph_t *tcph; 23401 mblk_t *mp1; 23402 int32_t tcp_hdr_len; 23403 int32_t tcp_tcp_hdr_len; 23404 int32_t num_sack_blk = 0; 23405 int32_t sack_opt_len; 23406 23407 /* 23408 * Allocate space for TCP + IP headers 23409 * and link-level header 23410 */ 23411 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23412 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23413 tcp->tcp_num_sack_blk); 23414 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23415 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23416 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23417 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23418 } else { 23419 tcp_hdr_len = tcp->tcp_hdr_len; 23420 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23421 } 23422 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23423 if (!mp1) 23424 return (NULL); 23425 23426 /* Update the latest receive window size in TCP header. */ 23427 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23428 tcp->tcp_tcph->th_win); 23429 /* copy in prototype TCP + IP header */ 23430 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23431 mp1->b_rptr = rptr; 23432 mp1->b_wptr = rptr + tcp_hdr_len; 23433 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23434 23435 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23436 23437 /* Set the TCP sequence number. */ 23438 U32_TO_ABE32(seq_no, tcph->th_seq); 23439 23440 /* Set up the TCP flag field. */ 23441 tcph->th_flags[0] = (uchar_t)TH_ACK; 23442 if (tcp->tcp_ecn_echo_on) 23443 tcph->th_flags[0] |= TH_ECE; 23444 23445 tcp->tcp_rack = tcp->tcp_rnxt; 23446 tcp->tcp_rack_cnt = 0; 23447 23448 /* fill in timestamp option if in use */ 23449 if (tcp->tcp_snd_ts_ok) { 23450 uint32_t llbolt = (uint32_t)lbolt; 23451 23452 U32_TO_BE32(llbolt, 23453 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23454 U32_TO_BE32(tcp->tcp_ts_recent, 23455 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23456 } 23457 23458 /* Fill in SACK options */ 23459 if (num_sack_blk > 0) { 23460 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23461 sack_blk_t *tmp; 23462 int32_t i; 23463 23464 wptr[0] = TCPOPT_NOP; 23465 wptr[1] = TCPOPT_NOP; 23466 wptr[2] = TCPOPT_SACK; 23467 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23468 sizeof (sack_blk_t); 23469 wptr += TCPOPT_REAL_SACK_LEN; 23470 23471 tmp = tcp->tcp_sack_list; 23472 for (i = 0; i < num_sack_blk; i++) { 23473 U32_TO_BE32(tmp[i].begin, wptr); 23474 wptr += sizeof (tcp_seq); 23475 U32_TO_BE32(tmp[i].end, wptr); 23476 wptr += sizeof (tcp_seq); 23477 } 23478 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23479 << 4); 23480 } 23481 23482 if (tcp->tcp_ipversion == IPV4_VERSION) { 23483 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23484 } else { 23485 /* Check for ip6i_t header in sticky hdrs */ 23486 ip6_t *ip6 = (ip6_t *)(rptr + 23487 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23488 sizeof (ip6i_t) : 0)); 23489 23490 ip6->ip6_plen = htons(tcp_hdr_len - 23491 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23492 } 23493 23494 /* 23495 * Prime pump for checksum calculation in IP. Include the 23496 * adjustment for a source route if any. 23497 */ 23498 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23499 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23500 U16_TO_ABE16(data_length, tcph->th_sum); 23501 23502 if (tcp->tcp_ip_forward_progress) { 23503 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23504 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23505 tcp->tcp_ip_forward_progress = B_FALSE; 23506 } 23507 return (mp1); 23508 } 23509 } 23510 23511 /* 23512 * To create a temporary tcp structure for inserting into bind hash list. 23513 * The parameter is assumed to be in network byte order, ready for use. 23514 */ 23515 /* ARGSUSED */ 23516 static tcp_t * 23517 tcp_alloc_temp_tcp(in_port_t port, tcp_stack_t *tcps) 23518 { 23519 conn_t *connp; 23520 tcp_t *tcp; 23521 23522 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP, tcps->tcps_netstack); 23523 if (connp == NULL) 23524 return (NULL); 23525 23526 tcp = connp->conn_tcp; 23527 tcp->tcp_tcps = tcps; 23528 TCPS_REFHOLD(tcps); 23529 23530 /* 23531 * Only initialize the necessary info in those structures. Note 23532 * that since INADDR_ANY is all 0, we do not need to set 23533 * tcp_bound_source to INADDR_ANY here. 23534 */ 23535 tcp->tcp_state = TCPS_BOUND; 23536 tcp->tcp_lport = port; 23537 tcp->tcp_exclbind = 1; 23538 tcp->tcp_reserved_port = 1; 23539 23540 /* Just for place holding... */ 23541 tcp->tcp_ipversion = IPV4_VERSION; 23542 23543 return (tcp); 23544 } 23545 23546 /* 23547 * To remove a port range specified by lo_port and hi_port from the 23548 * reserved port ranges. This is one of the three public functions of 23549 * the reserved port interface. Note that a port range has to be removed 23550 * as a whole. Ports in a range cannot be removed individually. 23551 * 23552 * Params: 23553 * in_port_t lo_port: the beginning port of the reserved port range to 23554 * be deleted. 23555 * in_port_t hi_port: the ending port of the reserved port range to 23556 * be deleted. 23557 * 23558 * Return: 23559 * B_TRUE if the deletion is successful, B_FALSE otherwise. 23560 * 23561 * Assumes that nca is only for zoneid=0 23562 */ 23563 boolean_t 23564 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 23565 { 23566 int i, j; 23567 int size; 23568 tcp_t **temp_tcp_array; 23569 tcp_t *tcp; 23570 tcp_stack_t *tcps; 23571 23572 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23573 ASSERT(tcps != NULL); 23574 23575 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23576 23577 /* First make sure that the port ranage is indeed reserved. */ 23578 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23579 if (tcps->tcps_reserved_port[i].lo_port == lo_port) { 23580 hi_port = tcps->tcps_reserved_port[i].hi_port; 23581 temp_tcp_array = 23582 tcps->tcps_reserved_port[i].temp_tcp_array; 23583 break; 23584 } 23585 } 23586 if (i == tcps->tcps_reserved_port_array_size) { 23587 rw_exit(&tcps->tcps_reserved_port_lock); 23588 netstack_rele(tcps->tcps_netstack); 23589 return (B_FALSE); 23590 } 23591 23592 /* 23593 * Remove the range from the array. This simple loop is possible 23594 * because port ranges are inserted in ascending order. 23595 */ 23596 for (j = i; j < tcps->tcps_reserved_port_array_size - 1; j++) { 23597 tcps->tcps_reserved_port[j].lo_port = 23598 tcps->tcps_reserved_port[j+1].lo_port; 23599 tcps->tcps_reserved_port[j].hi_port = 23600 tcps->tcps_reserved_port[j+1].hi_port; 23601 tcps->tcps_reserved_port[j].temp_tcp_array = 23602 tcps->tcps_reserved_port[j+1].temp_tcp_array; 23603 } 23604 23605 /* Remove all the temporary tcp structures. */ 23606 size = hi_port - lo_port + 1; 23607 while (size > 0) { 23608 tcp = temp_tcp_array[size - 1]; 23609 ASSERT(tcp != NULL); 23610 tcp_bind_hash_remove(tcp); 23611 CONN_DEC_REF(tcp->tcp_connp); 23612 size--; 23613 } 23614 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 23615 tcps->tcps_reserved_port_array_size--; 23616 rw_exit(&tcps->tcps_reserved_port_lock); 23617 netstack_rele(tcps->tcps_netstack); 23618 return (B_TRUE); 23619 } 23620 23621 /* 23622 * Macro to remove temporary tcp structure from the bind hash list. The 23623 * first parameter is the list of tcp to be removed. The second parameter 23624 * is the number of tcps in the array. 23625 */ 23626 #define TCP_TMP_TCP_REMOVE(tcp_array, num, tcps) \ 23627 { \ 23628 while ((num) > 0) { \ 23629 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 23630 tf_t *tbf; \ 23631 tcp_t *tcpnext; \ 23632 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 23633 mutex_enter(&tbf->tf_lock); \ 23634 tcpnext = tcp->tcp_bind_hash; \ 23635 if (tcpnext) { \ 23636 tcpnext->tcp_ptpbhn = \ 23637 tcp->tcp_ptpbhn; \ 23638 } \ 23639 *tcp->tcp_ptpbhn = tcpnext; \ 23640 mutex_exit(&tbf->tf_lock); \ 23641 kmem_free(tcp, sizeof (tcp_t)); \ 23642 (tcp_array)[(num) - 1] = NULL; \ 23643 (num)--; \ 23644 } \ 23645 } 23646 23647 /* 23648 * The public interface for other modules to call to reserve a port range 23649 * in TCP. The caller passes in how large a port range it wants. TCP 23650 * will try to find a range and return it via lo_port and hi_port. This is 23651 * used by NCA's nca_conn_init. 23652 * NCA can only be used in the global zone so this only affects the global 23653 * zone's ports. 23654 * 23655 * Params: 23656 * int size: the size of the port range to be reserved. 23657 * in_port_t *lo_port (referenced): returns the beginning port of the 23658 * reserved port range added. 23659 * in_port_t *hi_port (referenced): returns the ending port of the 23660 * reserved port range added. 23661 * 23662 * Return: 23663 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 23664 * 23665 * Assumes that nca is only for zoneid=0 23666 */ 23667 boolean_t 23668 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 23669 { 23670 tcp_t *tcp; 23671 tcp_t *tmp_tcp; 23672 tcp_t **temp_tcp_array; 23673 tf_t *tbf; 23674 in_port_t net_port; 23675 in_port_t port; 23676 int32_t cur_size; 23677 int i, j; 23678 boolean_t used; 23679 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 23680 zoneid_t zoneid = GLOBAL_ZONEID; 23681 tcp_stack_t *tcps; 23682 23683 /* Sanity check. */ 23684 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 23685 return (B_FALSE); 23686 } 23687 23688 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23689 ASSERT(tcps != NULL); 23690 23691 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23692 if (tcps->tcps_reserved_port_array_size == 23693 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 23694 rw_exit(&tcps->tcps_reserved_port_lock); 23695 netstack_rele(tcps->tcps_netstack); 23696 return (B_FALSE); 23697 } 23698 23699 /* 23700 * Find the starting port to try. Since the port ranges are ordered 23701 * in the reserved port array, we can do a simple search here. 23702 */ 23703 *lo_port = TCP_SMALLEST_RESERVED_PORT; 23704 *hi_port = TCP_LARGEST_RESERVED_PORT; 23705 for (i = 0; i < tcps->tcps_reserved_port_array_size; 23706 *lo_port = tcps->tcps_reserved_port[i].hi_port + 1, i++) { 23707 if (tcps->tcps_reserved_port[i].lo_port - *lo_port >= size) { 23708 *hi_port = tcps->tcps_reserved_port[i].lo_port - 1; 23709 break; 23710 } 23711 } 23712 /* No available port range. */ 23713 if (i == tcps->tcps_reserved_port_array_size && 23714 *hi_port - *lo_port < size) { 23715 rw_exit(&tcps->tcps_reserved_port_lock); 23716 netstack_rele(tcps->tcps_netstack); 23717 return (B_FALSE); 23718 } 23719 23720 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 23721 if (temp_tcp_array == NULL) { 23722 rw_exit(&tcps->tcps_reserved_port_lock); 23723 netstack_rele(tcps->tcps_netstack); 23724 return (B_FALSE); 23725 } 23726 23727 /* Go thru the port range to see if some ports are already bound. */ 23728 for (port = *lo_port, cur_size = 0; 23729 cur_size < size && port <= *hi_port; 23730 cur_size++, port++) { 23731 used = B_FALSE; 23732 net_port = htons(port); 23733 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(net_port)]; 23734 mutex_enter(&tbf->tf_lock); 23735 for (tcp = tbf->tf_tcp; tcp != NULL; 23736 tcp = tcp->tcp_bind_hash) { 23737 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 23738 net_port == tcp->tcp_lport) { 23739 /* 23740 * A port is already bound. Search again 23741 * starting from port + 1. Release all 23742 * temporary tcps. 23743 */ 23744 mutex_exit(&tbf->tf_lock); 23745 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23746 tcps); 23747 *lo_port = port + 1; 23748 cur_size = -1; 23749 used = B_TRUE; 23750 break; 23751 } 23752 } 23753 if (!used) { 23754 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port, tcps)) == 23755 NULL) { 23756 /* 23757 * Allocation failure. Just fail the request. 23758 * Need to remove all those temporary tcp 23759 * structures. 23760 */ 23761 mutex_exit(&tbf->tf_lock); 23762 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23763 tcps); 23764 rw_exit(&tcps->tcps_reserved_port_lock); 23765 kmem_free(temp_tcp_array, 23766 (hi_port - lo_port + 1) * 23767 sizeof (tcp_t *)); 23768 netstack_rele(tcps->tcps_netstack); 23769 return (B_FALSE); 23770 } 23771 temp_tcp_array[cur_size] = tmp_tcp; 23772 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 23773 mutex_exit(&tbf->tf_lock); 23774 } 23775 } 23776 23777 /* 23778 * The current range is not large enough. We can actually do another 23779 * search if this search is done between 2 reserved port ranges. But 23780 * for first release, we just stop here and return saying that no port 23781 * range is available. 23782 */ 23783 if (cur_size < size) { 23784 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, tcps); 23785 rw_exit(&tcps->tcps_reserved_port_lock); 23786 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 23787 netstack_rele(tcps->tcps_netstack); 23788 return (B_FALSE); 23789 } 23790 *hi_port = port - 1; 23791 23792 /* 23793 * Insert range into array in ascending order. Since this function 23794 * must not be called often, we choose to use the simplest method. 23795 * The above array should not consume excessive stack space as 23796 * the size must be very small. If in future releases, we find 23797 * that we should provide more reserved port ranges, this function 23798 * has to be modified to be more efficient. 23799 */ 23800 if (tcps->tcps_reserved_port_array_size == 0) { 23801 tcps->tcps_reserved_port[0].lo_port = *lo_port; 23802 tcps->tcps_reserved_port[0].hi_port = *hi_port; 23803 tcps->tcps_reserved_port[0].temp_tcp_array = temp_tcp_array; 23804 } else { 23805 for (i = 0, j = 0; i < tcps->tcps_reserved_port_array_size; 23806 i++, j++) { 23807 if (*lo_port < tcps->tcps_reserved_port[i].lo_port && 23808 i == j) { 23809 tmp_ports[j].lo_port = *lo_port; 23810 tmp_ports[j].hi_port = *hi_port; 23811 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23812 j++; 23813 } 23814 tmp_ports[j].lo_port = 23815 tcps->tcps_reserved_port[i].lo_port; 23816 tmp_ports[j].hi_port = 23817 tcps->tcps_reserved_port[i].hi_port; 23818 tmp_ports[j].temp_tcp_array = 23819 tcps->tcps_reserved_port[i].temp_tcp_array; 23820 } 23821 if (j == i) { 23822 tmp_ports[j].lo_port = *lo_port; 23823 tmp_ports[j].hi_port = *hi_port; 23824 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23825 } 23826 bcopy(tmp_ports, tcps->tcps_reserved_port, sizeof (tmp_ports)); 23827 } 23828 tcps->tcps_reserved_port_array_size++; 23829 rw_exit(&tcps->tcps_reserved_port_lock); 23830 netstack_rele(tcps->tcps_netstack); 23831 return (B_TRUE); 23832 } 23833 23834 /* 23835 * Check to see if a port is in any reserved port range. 23836 * 23837 * Params: 23838 * in_port_t port: the port to be verified. 23839 * 23840 * Return: 23841 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 23842 */ 23843 boolean_t 23844 tcp_reserved_port_check(in_port_t port, tcp_stack_t *tcps) 23845 { 23846 int i; 23847 23848 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23849 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23850 if (port >= tcps->tcps_reserved_port[i].lo_port || 23851 port <= tcps->tcps_reserved_port[i].hi_port) { 23852 rw_exit(&tcps->tcps_reserved_port_lock); 23853 return (B_TRUE); 23854 } 23855 } 23856 rw_exit(&tcps->tcps_reserved_port_lock); 23857 return (B_FALSE); 23858 } 23859 23860 /* 23861 * To list all reserved port ranges. This is the function to handle 23862 * ndd tcp_reserved_port_list. 23863 */ 23864 /* ARGSUSED */ 23865 static int 23866 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23867 { 23868 int i; 23869 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23870 23871 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23872 if (tcps->tcps_reserved_port_array_size > 0) 23873 (void) mi_mpprintf(mp, "The following ports are reserved:"); 23874 else 23875 (void) mi_mpprintf(mp, "No port is reserved."); 23876 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23877 (void) mi_mpprintf(mp, "%d-%d", 23878 tcps->tcps_reserved_port[i].lo_port, 23879 tcps->tcps_reserved_port[i].hi_port); 23880 } 23881 rw_exit(&tcps->tcps_reserved_port_lock); 23882 return (0); 23883 } 23884 23885 /* 23886 * Hash list insertion routine for tcp_t structures. 23887 * Inserts entries with the ones bound to a specific IP address first 23888 * followed by those bound to INADDR_ANY. 23889 */ 23890 static void 23891 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23892 { 23893 tcp_t **tcpp; 23894 tcp_t *tcpnext; 23895 23896 if (tcp->tcp_ptpbhn != NULL) { 23897 ASSERT(!caller_holds_lock); 23898 tcp_bind_hash_remove(tcp); 23899 } 23900 tcpp = &tbf->tf_tcp; 23901 if (!caller_holds_lock) { 23902 mutex_enter(&tbf->tf_lock); 23903 } else { 23904 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23905 } 23906 tcpnext = tcpp[0]; 23907 if (tcpnext) { 23908 /* 23909 * If the new tcp bound to the INADDR_ANY address 23910 * and the first one in the list is not bound to 23911 * INADDR_ANY we skip all entries until we find the 23912 * first one bound to INADDR_ANY. 23913 * This makes sure that applications binding to a 23914 * specific address get preference over those binding to 23915 * INADDR_ANY. 23916 */ 23917 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23918 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23919 while ((tcpnext = tcpp[0]) != NULL && 23920 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23921 tcpp = &(tcpnext->tcp_bind_hash); 23922 if (tcpnext) 23923 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23924 } else 23925 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23926 } 23927 tcp->tcp_bind_hash = tcpnext; 23928 tcp->tcp_ptpbhn = tcpp; 23929 tcpp[0] = tcp; 23930 if (!caller_holds_lock) 23931 mutex_exit(&tbf->tf_lock); 23932 } 23933 23934 /* 23935 * Hash list removal routine for tcp_t structures. 23936 */ 23937 static void 23938 tcp_bind_hash_remove(tcp_t *tcp) 23939 { 23940 tcp_t *tcpnext; 23941 kmutex_t *lockp; 23942 tcp_stack_t *tcps = tcp->tcp_tcps; 23943 23944 if (tcp->tcp_ptpbhn == NULL) 23945 return; 23946 23947 /* 23948 * Extract the lock pointer in case there are concurrent 23949 * hash_remove's for this instance. 23950 */ 23951 ASSERT(tcp->tcp_lport != 0); 23952 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23953 23954 ASSERT(lockp != NULL); 23955 mutex_enter(lockp); 23956 if (tcp->tcp_ptpbhn) { 23957 tcpnext = tcp->tcp_bind_hash; 23958 if (tcpnext) { 23959 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23960 tcp->tcp_bind_hash = NULL; 23961 } 23962 *tcp->tcp_ptpbhn = tcpnext; 23963 tcp->tcp_ptpbhn = NULL; 23964 } 23965 mutex_exit(lockp); 23966 } 23967 23968 23969 /* 23970 * Hash list lookup routine for tcp_t structures. 23971 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23972 */ 23973 static tcp_t * 23974 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23975 { 23976 tf_t *tf; 23977 tcp_t *tcp; 23978 23979 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23980 mutex_enter(&tf->tf_lock); 23981 for (tcp = tf->tf_tcp; tcp != NULL; 23982 tcp = tcp->tcp_acceptor_hash) { 23983 if (tcp->tcp_acceptor_id == id) { 23984 CONN_INC_REF(tcp->tcp_connp); 23985 mutex_exit(&tf->tf_lock); 23986 return (tcp); 23987 } 23988 } 23989 mutex_exit(&tf->tf_lock); 23990 return (NULL); 23991 } 23992 23993 23994 /* 23995 * Hash list insertion routine for tcp_t structures. 23996 */ 23997 void 23998 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23999 { 24000 tf_t *tf; 24001 tcp_t **tcpp; 24002 tcp_t *tcpnext; 24003 tcp_stack_t *tcps = tcp->tcp_tcps; 24004 24005 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24006 24007 if (tcp->tcp_ptpahn != NULL) 24008 tcp_acceptor_hash_remove(tcp); 24009 tcpp = &tf->tf_tcp; 24010 mutex_enter(&tf->tf_lock); 24011 tcpnext = tcpp[0]; 24012 if (tcpnext) 24013 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24014 tcp->tcp_acceptor_hash = tcpnext; 24015 tcp->tcp_ptpahn = tcpp; 24016 tcpp[0] = tcp; 24017 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24018 mutex_exit(&tf->tf_lock); 24019 } 24020 24021 /* 24022 * Hash list removal routine for tcp_t structures. 24023 */ 24024 static void 24025 tcp_acceptor_hash_remove(tcp_t *tcp) 24026 { 24027 tcp_t *tcpnext; 24028 kmutex_t *lockp; 24029 24030 /* 24031 * Extract the lock pointer in case there are concurrent 24032 * hash_remove's for this instance. 24033 */ 24034 lockp = tcp->tcp_acceptor_lockp; 24035 24036 if (tcp->tcp_ptpahn == NULL) 24037 return; 24038 24039 ASSERT(lockp != NULL); 24040 mutex_enter(lockp); 24041 if (tcp->tcp_ptpahn) { 24042 tcpnext = tcp->tcp_acceptor_hash; 24043 if (tcpnext) { 24044 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24045 tcp->tcp_acceptor_hash = NULL; 24046 } 24047 *tcp->tcp_ptpahn = tcpnext; 24048 tcp->tcp_ptpahn = NULL; 24049 } 24050 mutex_exit(lockp); 24051 tcp->tcp_acceptor_lockp = NULL; 24052 } 24053 24054 /* ARGSUSED */ 24055 static int 24056 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 24057 { 24058 int error = 0; 24059 int retval; 24060 char *end; 24061 tcp_hsp_t *hsp; 24062 tcp_hsp_t *hspprev; 24063 ipaddr_t addr = 0; /* Address we're looking for */ 24064 in6_addr_t v6addr; /* Address we're looking for */ 24065 uint32_t hash; /* Hash of that address */ 24066 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24067 24068 /* 24069 * If the following variables are still zero after parsing the input 24070 * string, the user didn't specify them and we don't change them in 24071 * the HSP. 24072 */ 24073 24074 ipaddr_t mask = 0; /* Subnet mask */ 24075 in6_addr_t v6mask; 24076 long sendspace = 0; /* Send buffer size */ 24077 long recvspace = 0; /* Receive buffer size */ 24078 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 24079 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 24080 24081 rw_enter(&tcps->tcps_hsp_lock, RW_WRITER); 24082 24083 /* Parse and validate address */ 24084 if (af == AF_INET) { 24085 retval = inet_pton(af, value, &addr); 24086 if (retval == 1) 24087 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 24088 } else if (af == AF_INET6) { 24089 retval = inet_pton(af, value, &v6addr); 24090 } else { 24091 error = EINVAL; 24092 goto done; 24093 } 24094 if (retval == 0) { 24095 error = EINVAL; 24096 goto done; 24097 } 24098 24099 while ((*value) && *value != ' ') 24100 value++; 24101 24102 /* Parse individual keywords, set variables if found */ 24103 while (*value) { 24104 /* Skip leading blanks */ 24105 24106 while (*value == ' ' || *value == '\t') 24107 value++; 24108 24109 /* If at end of string, we're done */ 24110 24111 if (!*value) 24112 break; 24113 24114 /* We have a word, figure out what it is */ 24115 24116 if (strncmp("mask", value, 4) == 0) { 24117 value += 4; 24118 while (*value == ' ' || *value == '\t') 24119 value++; 24120 /* Parse subnet mask */ 24121 if (af == AF_INET) { 24122 retval = inet_pton(af, value, &mask); 24123 if (retval == 1) { 24124 V4MASK_TO_V6(mask, v6mask); 24125 } 24126 } else if (af == AF_INET6) { 24127 retval = inet_pton(af, value, &v6mask); 24128 } 24129 if (retval != 1) { 24130 error = EINVAL; 24131 goto done; 24132 } 24133 while ((*value) && *value != ' ') 24134 value++; 24135 } else if (strncmp("sendspace", value, 9) == 0) { 24136 value += 9; 24137 24138 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 24139 sendspace < TCP_XMIT_HIWATER || 24140 sendspace >= (1L<<30)) { 24141 error = EINVAL; 24142 goto done; 24143 } 24144 value = end; 24145 } else if (strncmp("recvspace", value, 9) == 0) { 24146 value += 9; 24147 24148 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 24149 recvspace < TCP_RECV_HIWATER || 24150 recvspace >= (1L<<30)) { 24151 error = EINVAL; 24152 goto done; 24153 } 24154 value = end; 24155 } else if (strncmp("timestamp", value, 9) == 0) { 24156 value += 9; 24157 24158 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 24159 timestamp < 0 || timestamp > 1) { 24160 error = EINVAL; 24161 goto done; 24162 } 24163 24164 /* 24165 * We increment timestamp so we know it's been set; 24166 * this is undone when we put it in the HSP 24167 */ 24168 timestamp++; 24169 value = end; 24170 } else if (strncmp("delete", value, 6) == 0) { 24171 value += 6; 24172 delete = B_TRUE; 24173 } else { 24174 error = EINVAL; 24175 goto done; 24176 } 24177 } 24178 24179 /* Hash address for lookup */ 24180 24181 hash = TCP_HSP_HASH(addr); 24182 24183 if (delete) { 24184 /* 24185 * Note that deletes don't return an error if the thing 24186 * we're trying to delete isn't there. 24187 */ 24188 if (tcps->tcps_hsp_hash == NULL) 24189 goto done; 24190 hsp = tcps->tcps_hsp_hash[hash]; 24191 24192 if (hsp) { 24193 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24194 &v6addr)) { 24195 tcps->tcps_hsp_hash[hash] = hsp->tcp_hsp_next; 24196 mi_free((char *)hsp); 24197 } else { 24198 hspprev = hsp; 24199 while ((hsp = hsp->tcp_hsp_next) != NULL) { 24200 if (IN6_ARE_ADDR_EQUAL( 24201 &hsp->tcp_hsp_addr_v6, &v6addr)) { 24202 hspprev->tcp_hsp_next = 24203 hsp->tcp_hsp_next; 24204 mi_free((char *)hsp); 24205 break; 24206 } 24207 hspprev = hsp; 24208 } 24209 } 24210 } 24211 } else { 24212 /* 24213 * We're adding/modifying an HSP. If we haven't already done 24214 * so, allocate the hash table. 24215 */ 24216 24217 if (!tcps->tcps_hsp_hash) { 24218 tcps->tcps_hsp_hash = (tcp_hsp_t **) 24219 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 24220 if (!tcps->tcps_hsp_hash) { 24221 error = EINVAL; 24222 goto done; 24223 } 24224 } 24225 24226 /* Get head of hash chain */ 24227 24228 hsp = tcps->tcps_hsp_hash[hash]; 24229 24230 /* Try to find pre-existing hsp on hash chain */ 24231 /* Doesn't handle CIDR prefixes. */ 24232 while (hsp) { 24233 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 24234 break; 24235 hsp = hsp->tcp_hsp_next; 24236 } 24237 24238 /* 24239 * If we didn't, create one with default values and put it 24240 * at head of hash chain 24241 */ 24242 24243 if (!hsp) { 24244 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 24245 if (!hsp) { 24246 error = EINVAL; 24247 goto done; 24248 } 24249 hsp->tcp_hsp_next = tcps->tcps_hsp_hash[hash]; 24250 tcps->tcps_hsp_hash[hash] = hsp; 24251 } 24252 24253 /* Set values that the user asked us to change */ 24254 24255 hsp->tcp_hsp_addr_v6 = v6addr; 24256 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 24257 hsp->tcp_hsp_vers = IPV4_VERSION; 24258 else 24259 hsp->tcp_hsp_vers = IPV6_VERSION; 24260 hsp->tcp_hsp_subnet_v6 = v6mask; 24261 if (sendspace > 0) 24262 hsp->tcp_hsp_sendspace = sendspace; 24263 if (recvspace > 0) 24264 hsp->tcp_hsp_recvspace = recvspace; 24265 if (timestamp > 0) 24266 hsp->tcp_hsp_tstamp = timestamp - 1; 24267 } 24268 24269 done: 24270 rw_exit(&tcps->tcps_hsp_lock); 24271 return (error); 24272 } 24273 24274 /* Set callback routine passed to nd_load by tcp_param_register. */ 24275 /* ARGSUSED */ 24276 static int 24277 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 24278 { 24279 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 24280 } 24281 /* ARGSUSED */ 24282 static int 24283 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24284 cred_t *cr) 24285 { 24286 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 24287 } 24288 24289 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 24290 /* ARGSUSED */ 24291 static int 24292 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 24293 { 24294 tcp_hsp_t *hsp; 24295 int i; 24296 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 24297 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24298 24299 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24300 (void) mi_mpprintf(mp, 24301 "Hash HSP " MI_COL_HDRPAD_STR 24302 "Address Subnet Mask Send Receive TStamp"); 24303 if (tcps->tcps_hsp_hash) { 24304 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 24305 hsp = tcps->tcps_hsp_hash[i]; 24306 while (hsp) { 24307 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 24308 (void) inet_ntop(AF_INET, 24309 &hsp->tcp_hsp_addr, 24310 addrbuf, sizeof (addrbuf)); 24311 (void) inet_ntop(AF_INET, 24312 &hsp->tcp_hsp_subnet, 24313 subnetbuf, sizeof (subnetbuf)); 24314 } else { 24315 (void) inet_ntop(AF_INET6, 24316 &hsp->tcp_hsp_addr_v6, 24317 addrbuf, sizeof (addrbuf)); 24318 (void) inet_ntop(AF_INET6, 24319 &hsp->tcp_hsp_subnet_v6, 24320 subnetbuf, sizeof (subnetbuf)); 24321 } 24322 (void) mi_mpprintf(mp, 24323 " %03d " MI_COL_PTRFMT_STR 24324 "%s %s %010d %010d %d", 24325 i, 24326 (void *)hsp, 24327 addrbuf, 24328 subnetbuf, 24329 hsp->tcp_hsp_sendspace, 24330 hsp->tcp_hsp_recvspace, 24331 hsp->tcp_hsp_tstamp); 24332 24333 hsp = hsp->tcp_hsp_next; 24334 } 24335 } 24336 } 24337 rw_exit(&tcps->tcps_hsp_lock); 24338 return (0); 24339 } 24340 24341 24342 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24343 24344 static ipaddr_t netmasks[] = { 24345 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24346 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24347 }; 24348 24349 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24350 24351 /* 24352 * XXX This routine should go away and instead we should use the metrics 24353 * associated with the routes to determine the default sndspace and rcvspace. 24354 */ 24355 static tcp_hsp_t * 24356 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24357 { 24358 tcp_hsp_t *hsp = NULL; 24359 24360 /* Quick check without acquiring the lock. */ 24361 if (tcps->tcps_hsp_hash == NULL) 24362 return (NULL); 24363 24364 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24365 24366 /* This routine finds the best-matching HSP for address addr. */ 24367 24368 if (tcps->tcps_hsp_hash) { 24369 int i; 24370 ipaddr_t srchaddr; 24371 tcp_hsp_t *hsp_net; 24372 24373 /* We do three passes: host, network, and subnet. */ 24374 24375 srchaddr = addr; 24376 24377 for (i = 1; i <= 3; i++) { 24378 /* Look for exact match on srchaddr */ 24379 24380 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24381 while (hsp) { 24382 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24383 hsp->tcp_hsp_addr == srchaddr) 24384 break; 24385 hsp = hsp->tcp_hsp_next; 24386 } 24387 ASSERT(hsp == NULL || 24388 hsp->tcp_hsp_vers == IPV4_VERSION); 24389 24390 /* 24391 * If this is the first pass: 24392 * If we found a match, great, return it. 24393 * If not, search for the network on the second pass. 24394 */ 24395 24396 if (i == 1) 24397 if (hsp) 24398 break; 24399 else 24400 { 24401 srchaddr = addr & netmask(addr); 24402 continue; 24403 } 24404 24405 /* 24406 * If this is the second pass: 24407 * If we found a match, but there's a subnet mask, 24408 * save the match but try again using the subnet 24409 * mask on the third pass. 24410 * Otherwise, return whatever we found. 24411 */ 24412 24413 if (i == 2) { 24414 if (hsp && hsp->tcp_hsp_subnet) { 24415 hsp_net = hsp; 24416 srchaddr = addr & hsp->tcp_hsp_subnet; 24417 continue; 24418 } else { 24419 break; 24420 } 24421 } 24422 24423 /* 24424 * This must be the third pass. If we didn't find 24425 * anything, return the saved network HSP instead. 24426 */ 24427 24428 if (!hsp) 24429 hsp = hsp_net; 24430 } 24431 } 24432 24433 rw_exit(&tcps->tcps_hsp_lock); 24434 return (hsp); 24435 } 24436 24437 /* 24438 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24439 * match lookup. 24440 */ 24441 static tcp_hsp_t * 24442 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24443 { 24444 tcp_hsp_t *hsp = NULL; 24445 24446 /* Quick check without acquiring the lock. */ 24447 if (tcps->tcps_hsp_hash == NULL) 24448 return (NULL); 24449 24450 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24451 24452 /* This routine finds the best-matching HSP for address addr. */ 24453 24454 if (tcps->tcps_hsp_hash) { 24455 int i; 24456 in6_addr_t v6srchaddr; 24457 tcp_hsp_t *hsp_net; 24458 24459 /* We do three passes: host, network, and subnet. */ 24460 24461 v6srchaddr = *v6addr; 24462 24463 for (i = 1; i <= 3; i++) { 24464 /* Look for exact match on srchaddr */ 24465 24466 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24467 V4_PART_OF_V6(v6srchaddr))]; 24468 while (hsp) { 24469 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24470 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24471 &v6srchaddr)) 24472 break; 24473 hsp = hsp->tcp_hsp_next; 24474 } 24475 24476 /* 24477 * If this is the first pass: 24478 * If we found a match, great, return it. 24479 * If not, search for the network on the second pass. 24480 */ 24481 24482 if (i == 1) 24483 if (hsp) 24484 break; 24485 else { 24486 /* Assume a 64 bit mask */ 24487 v6srchaddr.s6_addr32[0] = 24488 v6addr->s6_addr32[0]; 24489 v6srchaddr.s6_addr32[1] = 24490 v6addr->s6_addr32[1]; 24491 v6srchaddr.s6_addr32[2] = 0; 24492 v6srchaddr.s6_addr32[3] = 0; 24493 continue; 24494 } 24495 24496 /* 24497 * If this is the second pass: 24498 * If we found a match, but there's a subnet mask, 24499 * save the match but try again using the subnet 24500 * mask on the third pass. 24501 * Otherwise, return whatever we found. 24502 */ 24503 24504 if (i == 2) { 24505 ASSERT(hsp == NULL || 24506 hsp->tcp_hsp_vers == IPV6_VERSION); 24507 if (hsp && 24508 !IN6_IS_ADDR_UNSPECIFIED( 24509 &hsp->tcp_hsp_subnet_v6)) { 24510 hsp_net = hsp; 24511 V6_MASK_COPY(*v6addr, 24512 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24513 continue; 24514 } else { 24515 break; 24516 } 24517 } 24518 24519 /* 24520 * This must be the third pass. If we didn't find 24521 * anything, return the saved network HSP instead. 24522 */ 24523 24524 if (!hsp) 24525 hsp = hsp_net; 24526 } 24527 } 24528 24529 rw_exit(&tcps->tcps_hsp_lock); 24530 return (hsp); 24531 } 24532 24533 /* 24534 * Type three generator adapted from the random() function in 4.4 BSD: 24535 */ 24536 24537 /* 24538 * Copyright (c) 1983, 1993 24539 * The Regents of the University of California. All rights reserved. 24540 * 24541 * Redistribution and use in source and binary forms, with or without 24542 * modification, are permitted provided that the following conditions 24543 * are met: 24544 * 1. Redistributions of source code must retain the above copyright 24545 * notice, this list of conditions and the following disclaimer. 24546 * 2. Redistributions in binary form must reproduce the above copyright 24547 * notice, this list of conditions and the following disclaimer in the 24548 * documentation and/or other materials provided with the distribution. 24549 * 3. All advertising materials mentioning features or use of this software 24550 * must display the following acknowledgement: 24551 * This product includes software developed by the University of 24552 * California, Berkeley and its contributors. 24553 * 4. Neither the name of the University nor the names of its contributors 24554 * may be used to endorse or promote products derived from this software 24555 * without specific prior written permission. 24556 * 24557 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24558 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24559 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24560 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24561 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24562 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24563 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24564 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24565 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24566 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24567 * SUCH DAMAGE. 24568 */ 24569 24570 /* Type 3 -- x**31 + x**3 + 1 */ 24571 #define DEG_3 31 24572 #define SEP_3 3 24573 24574 24575 /* Protected by tcp_random_lock */ 24576 static int tcp_randtbl[DEG_3 + 1]; 24577 24578 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24579 static int *tcp_random_rptr = &tcp_randtbl[1]; 24580 24581 static int *tcp_random_state = &tcp_randtbl[1]; 24582 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24583 24584 kmutex_t tcp_random_lock; 24585 24586 void 24587 tcp_random_init(void) 24588 { 24589 int i; 24590 hrtime_t hrt; 24591 time_t wallclock; 24592 uint64_t result; 24593 24594 /* 24595 * Use high-res timer and current time for seed. Gethrtime() returns 24596 * a longlong, which may contain resolution down to nanoseconds. 24597 * The current time will either be a 32-bit or a 64-bit quantity. 24598 * XOR the two together in a 64-bit result variable. 24599 * Convert the result to a 32-bit value by multiplying the high-order 24600 * 32-bits by the low-order 32-bits. 24601 */ 24602 24603 hrt = gethrtime(); 24604 (void) drv_getparm(TIME, &wallclock); 24605 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24606 mutex_enter(&tcp_random_lock); 24607 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24608 (result & 0xffffffff); 24609 24610 for (i = 1; i < DEG_3; i++) 24611 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24612 + 12345; 24613 tcp_random_fptr = &tcp_random_state[SEP_3]; 24614 tcp_random_rptr = &tcp_random_state[0]; 24615 mutex_exit(&tcp_random_lock); 24616 for (i = 0; i < 10 * DEG_3; i++) 24617 (void) tcp_random(); 24618 } 24619 24620 /* 24621 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24622 * This range is selected to be approximately centered on TCP_ISS / 2, 24623 * and easy to compute. We get this value by generating a 32-bit random 24624 * number, selecting out the high-order 17 bits, and then adding one so 24625 * that we never return zero. 24626 */ 24627 int 24628 tcp_random(void) 24629 { 24630 int i; 24631 24632 mutex_enter(&tcp_random_lock); 24633 *tcp_random_fptr += *tcp_random_rptr; 24634 24635 /* 24636 * The high-order bits are more random than the low-order bits, 24637 * so we select out the high-order 17 bits and add one so that 24638 * we never return zero. 24639 */ 24640 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24641 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24642 tcp_random_fptr = tcp_random_state; 24643 ++tcp_random_rptr; 24644 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24645 tcp_random_rptr = tcp_random_state; 24646 24647 mutex_exit(&tcp_random_lock); 24648 return (i); 24649 } 24650 24651 /* 24652 * XXX This will go away when TPI is extended to send 24653 * info reqs to sockfs/timod ..... 24654 * Given a queue, set the max packet size for the write 24655 * side of the queue below stream head. This value is 24656 * cached on the stream head. 24657 * Returns 1 on success, 0 otherwise. 24658 */ 24659 static int 24660 setmaxps(queue_t *q, int maxpsz) 24661 { 24662 struct stdata *stp; 24663 queue_t *wq; 24664 stp = STREAM(q); 24665 24666 /* 24667 * At this point change of a queue parameter is not allowed 24668 * when a multiplexor is sitting on top. 24669 */ 24670 if (stp->sd_flag & STPLEX) 24671 return (0); 24672 24673 claimstr(stp->sd_wrq); 24674 wq = stp->sd_wrq->q_next; 24675 ASSERT(wq != NULL); 24676 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24677 releasestr(stp->sd_wrq); 24678 return (1); 24679 } 24680 24681 static int 24682 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24683 int *t_errorp, int *sys_errorp) 24684 { 24685 int error; 24686 int is_absreq_failure; 24687 t_scalar_t *opt_lenp; 24688 t_scalar_t opt_offset; 24689 int prim_type; 24690 struct T_conn_req *tcreqp; 24691 struct T_conn_res *tcresp; 24692 cred_t *cr; 24693 24694 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24695 24696 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24697 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24698 prim_type == T_CONN_RES); 24699 24700 switch (prim_type) { 24701 case T_CONN_REQ: 24702 tcreqp = (struct T_conn_req *)mp->b_rptr; 24703 opt_offset = tcreqp->OPT_offset; 24704 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24705 break; 24706 case O_T_CONN_RES: 24707 case T_CONN_RES: 24708 tcresp = (struct T_conn_res *)mp->b_rptr; 24709 opt_offset = tcresp->OPT_offset; 24710 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24711 break; 24712 } 24713 24714 *t_errorp = 0; 24715 *sys_errorp = 0; 24716 *do_disconnectp = 0; 24717 24718 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24719 opt_offset, cr, &tcp_opt_obj, 24720 NULL, &is_absreq_failure); 24721 24722 switch (error) { 24723 case 0: /* no error */ 24724 ASSERT(is_absreq_failure == 0); 24725 return (0); 24726 case ENOPROTOOPT: 24727 *t_errorp = TBADOPT; 24728 break; 24729 case EACCES: 24730 *t_errorp = TACCES; 24731 break; 24732 default: 24733 *t_errorp = TSYSERR; *sys_errorp = error; 24734 break; 24735 } 24736 if (is_absreq_failure != 0) { 24737 /* 24738 * The connection request should get the local ack 24739 * T_OK_ACK and then a T_DISCON_IND. 24740 */ 24741 *do_disconnectp = 1; 24742 } 24743 return (-1); 24744 } 24745 24746 /* 24747 * Split this function out so that if the secret changes, I'm okay. 24748 * 24749 * Initialize the tcp_iss_cookie and tcp_iss_key. 24750 */ 24751 24752 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24753 24754 static void 24755 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24756 { 24757 struct { 24758 int32_t current_time; 24759 uint32_t randnum; 24760 uint16_t pad; 24761 uint8_t ether[6]; 24762 uint8_t passwd[PASSWD_SIZE]; 24763 } tcp_iss_cookie; 24764 time_t t; 24765 24766 /* 24767 * Start with the current absolute time. 24768 */ 24769 (void) drv_getparm(TIME, &t); 24770 tcp_iss_cookie.current_time = t; 24771 24772 /* 24773 * XXX - Need a more random number per RFC 1750, not this crap. 24774 * OTOH, if what follows is pretty random, then I'm in better shape. 24775 */ 24776 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24777 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24778 24779 /* 24780 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24781 * as a good template. 24782 */ 24783 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24784 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24785 24786 /* 24787 * The pass-phrase. Normally this is supplied by user-called NDD. 24788 */ 24789 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24790 24791 /* 24792 * See 4010593 if this section becomes a problem again, 24793 * but the local ethernet address is useful here. 24794 */ 24795 (void) localetheraddr(NULL, 24796 (struct ether_addr *)&tcp_iss_cookie.ether); 24797 24798 /* 24799 * Hash 'em all together. The MD5Final is called per-connection. 24800 */ 24801 mutex_enter(&tcps->tcps_iss_key_lock); 24802 MD5Init(&tcps->tcps_iss_key); 24803 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24804 sizeof (tcp_iss_cookie)); 24805 mutex_exit(&tcps->tcps_iss_key_lock); 24806 } 24807 24808 /* 24809 * Set the RFC 1948 pass phrase 24810 */ 24811 /* ARGSUSED */ 24812 static int 24813 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24814 cred_t *cr) 24815 { 24816 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24817 24818 /* 24819 * Basically, value contains a new pass phrase. Pass it along! 24820 */ 24821 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24822 return (0); 24823 } 24824 24825 /* ARGSUSED */ 24826 static int 24827 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24828 { 24829 bzero(buf, sizeof (tcp_sack_info_t)); 24830 return (0); 24831 } 24832 24833 /* ARGSUSED */ 24834 static int 24835 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24836 { 24837 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24838 return (0); 24839 } 24840 24841 /* 24842 * Make sure we wait until the default queue is setup, yet allow 24843 * tcp_g_q_create() to open a TCP stream. 24844 * We need to allow tcp_g_q_create() do do an open 24845 * of tcp, hence we compare curhread. 24846 * All others have to wait until the tcps_g_q has been 24847 * setup. 24848 */ 24849 void 24850 tcp_g_q_setup(tcp_stack_t *tcps) 24851 { 24852 mutex_enter(&tcps->tcps_g_q_lock); 24853 if (tcps->tcps_g_q != NULL) { 24854 mutex_exit(&tcps->tcps_g_q_lock); 24855 return; 24856 } 24857 if (tcps->tcps_g_q_creator == NULL) { 24858 /* This thread will set it up */ 24859 tcps->tcps_g_q_creator = curthread; 24860 mutex_exit(&tcps->tcps_g_q_lock); 24861 tcp_g_q_create(tcps); 24862 mutex_enter(&tcps->tcps_g_q_lock); 24863 ASSERT(tcps->tcps_g_q_creator == curthread); 24864 tcps->tcps_g_q_creator = NULL; 24865 cv_signal(&tcps->tcps_g_q_cv); 24866 ASSERT(tcps->tcps_g_q != NULL); 24867 mutex_exit(&tcps->tcps_g_q_lock); 24868 return; 24869 } 24870 /* Everybody but the creator has to wait */ 24871 if (tcps->tcps_g_q_creator != curthread) { 24872 while (tcps->tcps_g_q == NULL) 24873 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24874 } 24875 mutex_exit(&tcps->tcps_g_q_lock); 24876 } 24877 24878 major_t IP_MAJ; 24879 #define IP "ip" 24880 24881 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24882 24883 /* 24884 * Create a default tcp queue here instead of in strplumb 24885 */ 24886 void 24887 tcp_g_q_create(tcp_stack_t *tcps) 24888 { 24889 int error; 24890 ldi_handle_t lh = NULL; 24891 ldi_ident_t li = NULL; 24892 int rval; 24893 cred_t *cr; 24894 24895 #ifdef NS_DEBUG 24896 (void) printf("tcp_g_q_create()\n"); 24897 #endif 24898 24899 ASSERT(tcps->tcps_g_q_creator == curthread); 24900 24901 error = ldi_ident_from_major(IP_MAJ, &li); 24902 if (error) { 24903 #ifdef DEBUG 24904 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24905 error); 24906 #endif 24907 return; 24908 } 24909 24910 cr = zone_get_kcred(netstackid_to_zoneid( 24911 tcps->tcps_netstack->netstack_stackid)); 24912 ASSERT(cr != NULL); 24913 /* 24914 * We set the tcp default queue to IPv6 because IPv4 falls 24915 * back to IPv6 when it can't find a client, but 24916 * IPv6 does not fall back to IPv4. 24917 */ 24918 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24919 if (error) { 24920 #ifdef DEBUG 24921 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24922 error); 24923 #endif 24924 goto out; 24925 } 24926 24927 /* 24928 * This ioctl causes the tcp framework to cache a pointer to 24929 * this stream, so we don't want to close the stream after 24930 * this operation. 24931 * Use the kernel credentials that are for the zone we're in. 24932 */ 24933 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24934 (intptr_t)0, FKIOCTL, cr, &rval); 24935 if (error) { 24936 #ifdef DEBUG 24937 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24938 "error %d\n", error); 24939 #endif 24940 goto out; 24941 } 24942 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24943 lh = NULL; 24944 out: 24945 /* Close layered handles */ 24946 if (li) 24947 ldi_ident_release(li); 24948 /* Keep cred around until _inactive needs it */ 24949 tcps->tcps_g_q_cr = cr; 24950 } 24951 24952 /* 24953 * We keep tcp_g_q set until all other tcp_t's in the zone 24954 * has gone away, and then when tcp_g_q_inactive() is called 24955 * we clear it. 24956 */ 24957 void 24958 tcp_g_q_destroy(tcp_stack_t *tcps) 24959 { 24960 #ifdef NS_DEBUG 24961 (void) printf("tcp_g_q_destroy()for stack %d\n", 24962 tcps->tcps_netstack->netstack_stackid); 24963 #endif 24964 24965 if (tcps->tcps_g_q == NULL) { 24966 return; /* Nothing to cleanup */ 24967 } 24968 /* 24969 * Drop reference corresponding to the default queue. 24970 * This reference was added from tcp_open when the default queue 24971 * was created, hence we compensate for this extra drop in 24972 * tcp_g_q_close. If the refcnt drops to zero here it means 24973 * the default queue was the last one to be open, in which 24974 * case, then tcp_g_q_inactive will be 24975 * called as a result of the refrele. 24976 */ 24977 TCPS_REFRELE(tcps); 24978 } 24979 24980 /* 24981 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24982 * Run by tcp_q_q_inactive using a taskq. 24983 */ 24984 static void 24985 tcp_g_q_close(void *arg) 24986 { 24987 tcp_stack_t *tcps = arg; 24988 int error; 24989 ldi_handle_t lh = NULL; 24990 ldi_ident_t li = NULL; 24991 cred_t *cr; 24992 24993 #ifdef NS_DEBUG 24994 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24995 tcps->tcps_netstack->netstack_stackid, 24996 tcps->tcps_netstack->netstack_refcnt); 24997 #endif 24998 lh = tcps->tcps_g_q_lh; 24999 if (lh == NULL) 25000 return; /* Nothing to cleanup */ 25001 25002 ASSERT(tcps->tcps_refcnt == 1); 25003 ASSERT(tcps->tcps_g_q != NULL); 25004 25005 error = ldi_ident_from_major(IP_MAJ, &li); 25006 if (error) { 25007 #ifdef DEBUG 25008 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 25009 error); 25010 #endif 25011 return; 25012 } 25013 25014 cr = tcps->tcps_g_q_cr; 25015 tcps->tcps_g_q_cr = NULL; 25016 ASSERT(cr != NULL); 25017 25018 /* 25019 * Make sure we can break the recursion when tcp_close decrements 25020 * the reference count causing g_q_inactive to be called again. 25021 */ 25022 tcps->tcps_g_q_lh = NULL; 25023 25024 /* close the default queue */ 25025 (void) ldi_close(lh, FREAD|FWRITE, cr); 25026 /* 25027 * At this point in time tcps and the rest of netstack_t might 25028 * have been deleted. 25029 */ 25030 tcps = NULL; 25031 25032 /* Close layered handles */ 25033 ldi_ident_release(li); 25034 crfree(cr); 25035 } 25036 25037 /* 25038 * Called when last tcp_t drops reference count using TCPS_REFRELE. 25039 * 25040 * Have to ensure that the ldi routines are not used by an 25041 * interrupt thread by using a taskq. 25042 */ 25043 void 25044 tcp_g_q_inactive(tcp_stack_t *tcps) 25045 { 25046 if (tcps->tcps_g_q_lh == NULL) 25047 return; /* Nothing to cleanup */ 25048 25049 ASSERT(tcps->tcps_refcnt == 0); 25050 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 25051 25052 if (servicing_interrupt()) { 25053 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 25054 (void *) tcps, TQ_SLEEP); 25055 } else { 25056 tcp_g_q_close(tcps); 25057 } 25058 } 25059 25060 /* 25061 * Called by IP when IP is loaded into the kernel 25062 */ 25063 void 25064 tcp_ddi_g_init(void) 25065 { 25066 IP_MAJ = ddi_name_to_major(IP); 25067 25068 tcp_timercache = kmem_cache_create("tcp_timercache", 25069 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 25070 NULL, NULL, NULL, NULL, NULL, 0); 25071 25072 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 25073 sizeof (tcp_sack_info_t), 0, 25074 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 25075 25076 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 25077 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 25078 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 25079 25080 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 25081 25082 /* Initialize the random number generator */ 25083 tcp_random_init(); 25084 25085 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 25086 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 25087 25088 /* A single callback independently of how many netstacks we have */ 25089 ip_squeue_init(tcp_squeue_add); 25090 25091 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 25092 25093 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 25094 TASKQ_PREPOPULATE); 25095 25096 /* 25097 * We want to be informed each time a stack is created or 25098 * destroyed in the kernel, so we can maintain the 25099 * set of tcp_stack_t's. 25100 */ 25101 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 25102 tcp_stack_fini); 25103 } 25104 25105 25106 /* 25107 * Initialize the TCP stack instance. 25108 */ 25109 static void * 25110 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 25111 { 25112 tcp_stack_t *tcps; 25113 tcpparam_t *pa; 25114 int i; 25115 25116 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 25117 tcps->tcps_netstack = ns; 25118 25119 /* Initialize locks */ 25120 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 25121 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 25122 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 25123 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 25124 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 25125 rw_init(&tcps->tcps_reserved_port_lock, NULL, RW_DEFAULT, NULL); 25126 25127 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 25128 tcps->tcps_g_epriv_ports[0] = 2049; 25129 tcps->tcps_g_epriv_ports[1] = 4045; 25130 tcps->tcps_min_anonpriv_port = 512; 25131 25132 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 25133 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 25134 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 25135 TCP_FANOUT_SIZE, KM_SLEEP); 25136 tcps->tcps_reserved_port = kmem_zalloc(sizeof (tcp_rport_t) * 25137 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE, KM_SLEEP); 25138 25139 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25140 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 25141 MUTEX_DEFAULT, NULL); 25142 } 25143 25144 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25145 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 25146 MUTEX_DEFAULT, NULL); 25147 } 25148 25149 /* TCP's IPsec code calls the packet dropper. */ 25150 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 25151 25152 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 25153 tcps->tcps_params = pa; 25154 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25155 25156 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 25157 A_CNT(lcl_tcp_param_arr), tcps); 25158 25159 /* 25160 * Note: To really walk the device tree you need the devinfo 25161 * pointer to your device which is only available after probe/attach. 25162 * The following is safe only because it uses ddi_root_node() 25163 */ 25164 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 25165 tcp_opt_obj.odb_opt_arr_cnt); 25166 25167 /* 25168 * Initialize RFC 1948 secret values. This will probably be reset once 25169 * by the boot scripts. 25170 * 25171 * Use NULL name, as the name is caught by the new lockstats. 25172 * 25173 * Initialize with some random, non-guessable string, like the global 25174 * T_INFO_ACK. 25175 */ 25176 25177 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 25178 sizeof (tcp_g_t_info_ack), tcps); 25179 25180 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 25181 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 25182 25183 return (tcps); 25184 } 25185 25186 /* 25187 * Called when the IP module is about to be unloaded. 25188 */ 25189 void 25190 tcp_ddi_g_destroy(void) 25191 { 25192 tcp_g_kstat_fini(tcp_g_kstat); 25193 tcp_g_kstat = NULL; 25194 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 25195 25196 mutex_destroy(&tcp_random_lock); 25197 25198 kmem_cache_destroy(tcp_timercache); 25199 kmem_cache_destroy(tcp_sack_info_cache); 25200 kmem_cache_destroy(tcp_iphc_cache); 25201 25202 netstack_unregister(NS_TCP); 25203 taskq_destroy(tcp_taskq); 25204 } 25205 25206 /* 25207 * Shut down the TCP stack instance. 25208 */ 25209 /* ARGSUSED */ 25210 static void 25211 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25212 { 25213 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25214 25215 tcp_g_q_destroy(tcps); 25216 } 25217 25218 /* 25219 * Free the TCP stack instance. 25220 */ 25221 static void 25222 tcp_stack_fini(netstackid_t stackid, void *arg) 25223 { 25224 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25225 int i; 25226 25227 nd_free(&tcps->tcps_g_nd); 25228 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25229 tcps->tcps_params = NULL; 25230 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25231 tcps->tcps_wroff_xtra_param = NULL; 25232 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25233 tcps->tcps_mdt_head_param = NULL; 25234 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25235 tcps->tcps_mdt_tail_param = NULL; 25236 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25237 tcps->tcps_mdt_max_pbufs_param = NULL; 25238 25239 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25240 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25241 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25242 } 25243 25244 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25245 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25246 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25247 } 25248 25249 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25250 tcps->tcps_bind_fanout = NULL; 25251 25252 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25253 tcps->tcps_acceptor_fanout = NULL; 25254 25255 kmem_free(tcps->tcps_reserved_port, sizeof (tcp_rport_t) * 25256 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE); 25257 tcps->tcps_reserved_port = NULL; 25258 25259 mutex_destroy(&tcps->tcps_iss_key_lock); 25260 rw_destroy(&tcps->tcps_hsp_lock); 25261 mutex_destroy(&tcps->tcps_g_q_lock); 25262 cv_destroy(&tcps->tcps_g_q_cv); 25263 mutex_destroy(&tcps->tcps_epriv_port_lock); 25264 rw_destroy(&tcps->tcps_reserved_port_lock); 25265 25266 ip_drop_unregister(&tcps->tcps_dropper); 25267 25268 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25269 tcps->tcps_kstat = NULL; 25270 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25271 25272 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25273 tcps->tcps_mibkp = NULL; 25274 25275 kmem_free(tcps, sizeof (*tcps)); 25276 } 25277 25278 /* 25279 * Generate ISS, taking into account NDD changes may happen halfway through. 25280 * (If the iss is not zero, set it.) 25281 */ 25282 25283 static void 25284 tcp_iss_init(tcp_t *tcp) 25285 { 25286 MD5_CTX context; 25287 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25288 uint32_t answer[4]; 25289 tcp_stack_t *tcps = tcp->tcp_tcps; 25290 25291 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25292 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25293 switch (tcps->tcps_strong_iss) { 25294 case 2: 25295 mutex_enter(&tcps->tcps_iss_key_lock); 25296 context = tcps->tcps_iss_key; 25297 mutex_exit(&tcps->tcps_iss_key_lock); 25298 arg.ports = tcp->tcp_ports; 25299 if (tcp->tcp_ipversion == IPV4_VERSION) { 25300 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25301 &arg.src); 25302 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25303 &arg.dst); 25304 } else { 25305 arg.src = tcp->tcp_ip6h->ip6_src; 25306 arg.dst = tcp->tcp_ip6h->ip6_dst; 25307 } 25308 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25309 MD5Final((uchar_t *)answer, &context); 25310 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25311 /* 25312 * Now that we've hashed into a unique per-connection sequence 25313 * space, add a random increment per strong_iss == 1. So I 25314 * guess we'll have to... 25315 */ 25316 /* FALLTHRU */ 25317 case 1: 25318 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25319 break; 25320 default: 25321 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25322 break; 25323 } 25324 tcp->tcp_valid_bits = TCP_ISS_VALID; 25325 tcp->tcp_fss = tcp->tcp_iss - 1; 25326 tcp->tcp_suna = tcp->tcp_iss; 25327 tcp->tcp_snxt = tcp->tcp_iss + 1; 25328 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25329 tcp->tcp_csuna = tcp->tcp_snxt; 25330 } 25331 25332 /* 25333 * Exported routine for extracting active tcp connection status. 25334 * 25335 * This is used by the Solaris Cluster Networking software to 25336 * gather a list of connections that need to be forwarded to 25337 * specific nodes in the cluster when configuration changes occur. 25338 * 25339 * The callback is invoked for each tcp_t structure. Returning 25340 * non-zero from the callback routine terminates the search. 25341 */ 25342 int 25343 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25344 void *arg) 25345 { 25346 netstack_handle_t nh; 25347 netstack_t *ns; 25348 int ret = 0; 25349 25350 netstack_next_init(&nh); 25351 while ((ns = netstack_next(&nh)) != NULL) { 25352 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25353 ns->netstack_tcp); 25354 netstack_rele(ns); 25355 } 25356 netstack_next_fini(&nh); 25357 return (ret); 25358 } 25359 25360 static int 25361 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25362 tcp_stack_t *tcps) 25363 { 25364 tcp_t *tcp; 25365 cl_tcp_info_t cl_tcpi; 25366 connf_t *connfp; 25367 conn_t *connp; 25368 int i; 25369 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25370 25371 ASSERT(callback != NULL); 25372 25373 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25374 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25375 connp = NULL; 25376 25377 while ((connp = 25378 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25379 25380 tcp = connp->conn_tcp; 25381 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25382 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25383 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25384 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25385 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25386 /* 25387 * The macros tcp_laddr and tcp_faddr give the IPv4 25388 * addresses. They are copied implicitly below as 25389 * mapped addresses. 25390 */ 25391 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25392 if (tcp->tcp_ipversion == IPV4_VERSION) { 25393 cl_tcpi.cl_tcpi_faddr = 25394 tcp->tcp_ipha->ipha_dst; 25395 } else { 25396 cl_tcpi.cl_tcpi_faddr_v6 = 25397 tcp->tcp_ip6h->ip6_dst; 25398 } 25399 25400 /* 25401 * If the callback returns non-zero 25402 * we terminate the traversal. 25403 */ 25404 if ((*callback)(&cl_tcpi, arg) != 0) { 25405 CONN_DEC_REF(tcp->tcp_connp); 25406 return (1); 25407 } 25408 } 25409 } 25410 25411 return (0); 25412 } 25413 25414 /* 25415 * Macros used for accessing the different types of sockaddr 25416 * structures inside a tcp_ioc_abort_conn_t. 25417 */ 25418 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25419 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25420 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25421 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25422 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25423 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25424 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25425 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25426 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25427 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25428 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25429 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25430 25431 /* 25432 * Return the correct error code to mimic the behavior 25433 * of a connection reset. 25434 */ 25435 #define TCP_AC_GET_ERRCODE(state, err) { \ 25436 switch ((state)) { \ 25437 case TCPS_SYN_SENT: \ 25438 case TCPS_SYN_RCVD: \ 25439 (err) = ECONNREFUSED; \ 25440 break; \ 25441 case TCPS_ESTABLISHED: \ 25442 case TCPS_FIN_WAIT_1: \ 25443 case TCPS_FIN_WAIT_2: \ 25444 case TCPS_CLOSE_WAIT: \ 25445 (err) = ECONNRESET; \ 25446 break; \ 25447 case TCPS_CLOSING: \ 25448 case TCPS_LAST_ACK: \ 25449 case TCPS_TIME_WAIT: \ 25450 (err) = 0; \ 25451 break; \ 25452 default: \ 25453 (err) = ENXIO; \ 25454 } \ 25455 } 25456 25457 /* 25458 * Check if a tcp structure matches the info in acp. 25459 */ 25460 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25461 (((acp)->ac_local.ss_family == AF_INET) ? \ 25462 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25463 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25464 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25465 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25466 (TCP_AC_V4LPORT((acp)) == 0 || \ 25467 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25468 (TCP_AC_V4RPORT((acp)) == 0 || \ 25469 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25470 (acp)->ac_start <= (tcp)->tcp_state && \ 25471 (acp)->ac_end >= (tcp)->tcp_state) : \ 25472 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25473 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25474 &(tcp)->tcp_ip_src_v6)) && \ 25475 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25476 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25477 &(tcp)->tcp_remote_v6)) && \ 25478 (TCP_AC_V6LPORT((acp)) == 0 || \ 25479 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25480 (TCP_AC_V6RPORT((acp)) == 0 || \ 25481 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25482 (acp)->ac_start <= (tcp)->tcp_state && \ 25483 (acp)->ac_end >= (tcp)->tcp_state)) 25484 25485 #define TCP_AC_MATCH(acp, tcp) \ 25486 (((acp)->ac_zoneid == ALL_ZONES || \ 25487 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25488 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25489 25490 /* 25491 * Build a message containing a tcp_ioc_abort_conn_t structure 25492 * which is filled in with information from acp and tp. 25493 */ 25494 static mblk_t * 25495 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25496 { 25497 mblk_t *mp; 25498 tcp_ioc_abort_conn_t *tacp; 25499 25500 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25501 if (mp == NULL) 25502 return (NULL); 25503 25504 mp->b_datap->db_type = M_CTL; 25505 25506 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25507 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25508 sizeof (uint32_t)); 25509 25510 tacp->ac_start = acp->ac_start; 25511 tacp->ac_end = acp->ac_end; 25512 tacp->ac_zoneid = acp->ac_zoneid; 25513 25514 if (acp->ac_local.ss_family == AF_INET) { 25515 tacp->ac_local.ss_family = AF_INET; 25516 tacp->ac_remote.ss_family = AF_INET; 25517 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25518 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25519 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25520 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25521 } else { 25522 tacp->ac_local.ss_family = AF_INET6; 25523 tacp->ac_remote.ss_family = AF_INET6; 25524 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25525 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25526 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25527 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25528 } 25529 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25530 return (mp); 25531 } 25532 25533 /* 25534 * Print a tcp_ioc_abort_conn_t structure. 25535 */ 25536 static void 25537 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25538 { 25539 char lbuf[128]; 25540 char rbuf[128]; 25541 sa_family_t af; 25542 in_port_t lport, rport; 25543 ushort_t logflags; 25544 25545 af = acp->ac_local.ss_family; 25546 25547 if (af == AF_INET) { 25548 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25549 lbuf, 128); 25550 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25551 rbuf, 128); 25552 lport = ntohs(TCP_AC_V4LPORT(acp)); 25553 rport = ntohs(TCP_AC_V4RPORT(acp)); 25554 } else { 25555 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25556 lbuf, 128); 25557 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25558 rbuf, 128); 25559 lport = ntohs(TCP_AC_V6LPORT(acp)); 25560 rport = ntohs(TCP_AC_V6RPORT(acp)); 25561 } 25562 25563 logflags = SL_TRACE | SL_NOTE; 25564 /* 25565 * Don't print this message to the console if the operation was done 25566 * to a non-global zone. 25567 */ 25568 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25569 logflags |= SL_CONSOLE; 25570 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25571 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25572 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25573 acp->ac_start, acp->ac_end); 25574 } 25575 25576 /* 25577 * Called inside tcp_rput when a message built using 25578 * tcp_ioctl_abort_build_msg is put into a queue. 25579 * Note that when we get here there is no wildcard in acp any more. 25580 */ 25581 static void 25582 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25583 { 25584 tcp_ioc_abort_conn_t *acp; 25585 25586 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25587 if (tcp->tcp_state <= acp->ac_end) { 25588 /* 25589 * If we get here, we are already on the correct 25590 * squeue. This ioctl follows the following path 25591 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25592 * ->tcp_ioctl_abort->squeue_fill (if on a 25593 * different squeue) 25594 */ 25595 int errcode; 25596 25597 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25598 (void) tcp_clean_death(tcp, errcode, 26); 25599 } 25600 freemsg(mp); 25601 } 25602 25603 /* 25604 * Abort all matching connections on a hash chain. 25605 */ 25606 static int 25607 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25608 boolean_t exact, tcp_stack_t *tcps) 25609 { 25610 int nmatch, err = 0; 25611 tcp_t *tcp; 25612 MBLKP mp, last, listhead = NULL; 25613 conn_t *tconnp; 25614 connf_t *connfp; 25615 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25616 25617 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25618 25619 startover: 25620 nmatch = 0; 25621 25622 mutex_enter(&connfp->connf_lock); 25623 for (tconnp = connfp->connf_head; tconnp != NULL; 25624 tconnp = tconnp->conn_next) { 25625 tcp = tconnp->conn_tcp; 25626 if (TCP_AC_MATCH(acp, tcp)) { 25627 CONN_INC_REF(tcp->tcp_connp); 25628 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25629 if (mp == NULL) { 25630 err = ENOMEM; 25631 CONN_DEC_REF(tcp->tcp_connp); 25632 break; 25633 } 25634 mp->b_prev = (mblk_t *)tcp; 25635 25636 if (listhead == NULL) { 25637 listhead = mp; 25638 last = mp; 25639 } else { 25640 last->b_next = mp; 25641 last = mp; 25642 } 25643 nmatch++; 25644 if (exact) 25645 break; 25646 } 25647 25648 /* Avoid holding lock for too long. */ 25649 if (nmatch >= 500) 25650 break; 25651 } 25652 mutex_exit(&connfp->connf_lock); 25653 25654 /* Pass mp into the correct tcp */ 25655 while ((mp = listhead) != NULL) { 25656 listhead = listhead->b_next; 25657 tcp = (tcp_t *)mp->b_prev; 25658 mp->b_next = mp->b_prev = NULL; 25659 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 25660 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 25661 } 25662 25663 *count += nmatch; 25664 if (nmatch >= 500 && err == 0) 25665 goto startover; 25666 return (err); 25667 } 25668 25669 /* 25670 * Abort all connections that matches the attributes specified in acp. 25671 */ 25672 static int 25673 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25674 { 25675 sa_family_t af; 25676 uint32_t ports; 25677 uint16_t *pports; 25678 int err = 0, count = 0; 25679 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25680 int index = -1; 25681 ushort_t logflags; 25682 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25683 25684 af = acp->ac_local.ss_family; 25685 25686 if (af == AF_INET) { 25687 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25688 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25689 pports = (uint16_t *)&ports; 25690 pports[1] = TCP_AC_V4LPORT(acp); 25691 pports[0] = TCP_AC_V4RPORT(acp); 25692 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25693 } 25694 } else { 25695 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25696 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25697 pports = (uint16_t *)&ports; 25698 pports[1] = TCP_AC_V6LPORT(acp); 25699 pports[0] = TCP_AC_V6RPORT(acp); 25700 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25701 } 25702 } 25703 25704 /* 25705 * For cases where remote addr, local port, and remote port are non- 25706 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25707 */ 25708 if (index != -1) { 25709 err = tcp_ioctl_abort_bucket(acp, index, 25710 &count, exact, tcps); 25711 } else { 25712 /* 25713 * loop through all entries for wildcard case 25714 */ 25715 for (index = 0; 25716 index < ipst->ips_ipcl_conn_fanout_size; 25717 index++) { 25718 err = tcp_ioctl_abort_bucket(acp, index, 25719 &count, exact, tcps); 25720 if (err != 0) 25721 break; 25722 } 25723 } 25724 25725 logflags = SL_TRACE | SL_NOTE; 25726 /* 25727 * Don't print this message to the console if the operation was done 25728 * to a non-global zone. 25729 */ 25730 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25731 logflags |= SL_CONSOLE; 25732 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25733 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25734 if (err == 0 && count == 0) 25735 err = ENOENT; 25736 return (err); 25737 } 25738 25739 /* 25740 * Process the TCP_IOC_ABORT_CONN ioctl request. 25741 */ 25742 static void 25743 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25744 { 25745 int err; 25746 IOCP iocp; 25747 MBLKP mp1; 25748 sa_family_t laf, raf; 25749 tcp_ioc_abort_conn_t *acp; 25750 zone_t *zptr; 25751 conn_t *connp = Q_TO_CONN(q); 25752 zoneid_t zoneid = connp->conn_zoneid; 25753 tcp_t *tcp = connp->conn_tcp; 25754 tcp_stack_t *tcps = tcp->tcp_tcps; 25755 25756 iocp = (IOCP)mp->b_rptr; 25757 25758 if ((mp1 = mp->b_cont) == NULL || 25759 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25760 err = EINVAL; 25761 goto out; 25762 } 25763 25764 /* check permissions */ 25765 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25766 err = EPERM; 25767 goto out; 25768 } 25769 25770 if (mp1->b_cont != NULL) { 25771 freemsg(mp1->b_cont); 25772 mp1->b_cont = NULL; 25773 } 25774 25775 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25776 laf = acp->ac_local.ss_family; 25777 raf = acp->ac_remote.ss_family; 25778 25779 /* check that a zone with the supplied zoneid exists */ 25780 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25781 zptr = zone_find_by_id(zoneid); 25782 if (zptr != NULL) { 25783 zone_rele(zptr); 25784 } else { 25785 err = EINVAL; 25786 goto out; 25787 } 25788 } 25789 25790 /* 25791 * For exclusive stacks we set the zoneid to zero 25792 * to make TCP operate as if in the global zone. 25793 */ 25794 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25795 acp->ac_zoneid = GLOBAL_ZONEID; 25796 25797 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25798 acp->ac_start > acp->ac_end || laf != raf || 25799 (laf != AF_INET && laf != AF_INET6)) { 25800 err = EINVAL; 25801 goto out; 25802 } 25803 25804 tcp_ioctl_abort_dump(acp); 25805 err = tcp_ioctl_abort(acp, tcps); 25806 25807 out: 25808 if (mp1 != NULL) { 25809 freemsg(mp1); 25810 mp->b_cont = NULL; 25811 } 25812 25813 if (err != 0) 25814 miocnak(q, mp, 0, err); 25815 else 25816 miocack(q, mp, 0, 0); 25817 } 25818 25819 /* 25820 * tcp_time_wait_processing() handles processing of incoming packets when 25821 * the tcp is in the TIME_WAIT state. 25822 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25823 * on the time wait list. 25824 */ 25825 void 25826 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25827 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25828 { 25829 int32_t bytes_acked; 25830 int32_t gap; 25831 int32_t rgap; 25832 tcp_opt_t tcpopt; 25833 uint_t flags; 25834 uint32_t new_swnd = 0; 25835 conn_t *connp; 25836 tcp_stack_t *tcps = tcp->tcp_tcps; 25837 25838 BUMP_LOCAL(tcp->tcp_ibsegs); 25839 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 25840 25841 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25842 new_swnd = BE16_TO_U16(tcph->th_win) << 25843 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25844 if (tcp->tcp_snd_ts_ok) { 25845 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25846 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25847 tcp->tcp_rnxt, TH_ACK); 25848 goto done; 25849 } 25850 } 25851 gap = seg_seq - tcp->tcp_rnxt; 25852 rgap = tcp->tcp_rwnd - (gap + seg_len); 25853 if (gap < 0) { 25854 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25855 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25856 (seg_len > -gap ? -gap : seg_len)); 25857 seg_len += gap; 25858 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25859 if (flags & TH_RST) { 25860 goto done; 25861 } 25862 if ((flags & TH_FIN) && seg_len == -1) { 25863 /* 25864 * When TCP receives a duplicate FIN in 25865 * TIME_WAIT state, restart the 2 MSL timer. 25866 * See page 73 in RFC 793. Make sure this TCP 25867 * is already on the TIME_WAIT list. If not, 25868 * just restart the timer. 25869 */ 25870 if (TCP_IS_DETACHED(tcp)) { 25871 if (tcp_time_wait_remove(tcp, NULL) == 25872 B_TRUE) { 25873 tcp_time_wait_append(tcp); 25874 TCP_DBGSTAT(tcps, 25875 tcp_rput_time_wait); 25876 } 25877 } else { 25878 ASSERT(tcp != NULL); 25879 TCP_TIMER_RESTART(tcp, 25880 tcps->tcps_time_wait_interval); 25881 } 25882 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25883 tcp->tcp_rnxt, TH_ACK); 25884 goto done; 25885 } 25886 flags |= TH_ACK_NEEDED; 25887 seg_len = 0; 25888 goto process_ack; 25889 } 25890 25891 /* Fix seg_seq, and chew the gap off the front. */ 25892 seg_seq = tcp->tcp_rnxt; 25893 } 25894 25895 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25896 /* 25897 * Make sure that when we accept the connection, pick 25898 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25899 * old connection. 25900 * 25901 * The next ISS generated is equal to tcp_iss_incr_extra 25902 * + ISS_INCR/2 + other components depending on the 25903 * value of tcp_strong_iss. We pre-calculate the new 25904 * ISS here and compare with tcp_snxt to determine if 25905 * we need to make adjustment to tcp_iss_incr_extra. 25906 * 25907 * The above calculation is ugly and is a 25908 * waste of CPU cycles... 25909 */ 25910 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25911 int32_t adj; 25912 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25913 25914 switch (tcps->tcps_strong_iss) { 25915 case 2: { 25916 /* Add time and MD5 components. */ 25917 uint32_t answer[4]; 25918 struct { 25919 uint32_t ports; 25920 in6_addr_t src; 25921 in6_addr_t dst; 25922 } arg; 25923 MD5_CTX context; 25924 25925 mutex_enter(&tcps->tcps_iss_key_lock); 25926 context = tcps->tcps_iss_key; 25927 mutex_exit(&tcps->tcps_iss_key_lock); 25928 arg.ports = tcp->tcp_ports; 25929 /* We use MAPPED addresses in tcp_iss_init */ 25930 arg.src = tcp->tcp_ip_src_v6; 25931 if (tcp->tcp_ipversion == IPV4_VERSION) { 25932 IN6_IPADDR_TO_V4MAPPED( 25933 tcp->tcp_ipha->ipha_dst, 25934 &arg.dst); 25935 } else { 25936 arg.dst = 25937 tcp->tcp_ip6h->ip6_dst; 25938 } 25939 MD5Update(&context, (uchar_t *)&arg, 25940 sizeof (arg)); 25941 MD5Final((uchar_t *)answer, &context); 25942 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25943 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25944 break; 25945 } 25946 case 1: 25947 /* Add time component and min random (i.e. 1). */ 25948 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25949 break; 25950 default: 25951 /* Add only time component. */ 25952 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25953 break; 25954 } 25955 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25956 /* 25957 * New ISS not guaranteed to be ISS_INCR/2 25958 * ahead of the current tcp_snxt, so add the 25959 * difference to tcp_iss_incr_extra. 25960 */ 25961 tcps->tcps_iss_incr_extra += adj; 25962 } 25963 /* 25964 * If tcp_clean_death() can not perform the task now, 25965 * drop the SYN packet and let the other side re-xmit. 25966 * Otherwise pass the SYN packet back in, since the 25967 * old tcp state has been cleaned up or freed. 25968 */ 25969 if (tcp_clean_death(tcp, 0, 27) == -1) 25970 goto done; 25971 /* 25972 * We will come back to tcp_rput_data 25973 * on the global queue. Packets destined 25974 * for the global queue will be checked 25975 * with global policy. But the policy for 25976 * this packet has already been checked as 25977 * this was destined for the detached 25978 * connection. We need to bypass policy 25979 * check this time by attaching a dummy 25980 * ipsec_in with ipsec_in_dont_check set. 25981 */ 25982 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25983 if (connp != NULL) { 25984 TCP_STAT(tcps, tcp_time_wait_syn_success); 25985 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25986 return; 25987 } 25988 goto done; 25989 } 25990 25991 /* 25992 * rgap is the amount of stuff received out of window. A negative 25993 * value is the amount out of window. 25994 */ 25995 if (rgap < 0) { 25996 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25997 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25998 /* Fix seg_len and make sure there is something left. */ 25999 seg_len += rgap; 26000 if (seg_len <= 0) { 26001 if (flags & TH_RST) { 26002 goto done; 26003 } 26004 flags |= TH_ACK_NEEDED; 26005 seg_len = 0; 26006 goto process_ack; 26007 } 26008 } 26009 /* 26010 * Check whether we can update tcp_ts_recent. This test is 26011 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 26012 * Extensions for High Performance: An Update", Internet Draft. 26013 */ 26014 if (tcp->tcp_snd_ts_ok && 26015 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 26016 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 26017 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 26018 tcp->tcp_last_rcv_lbolt = lbolt64; 26019 } 26020 26021 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 26022 /* Always ack out of order packets */ 26023 flags |= TH_ACK_NEEDED; 26024 seg_len = 0; 26025 } else if (seg_len > 0) { 26026 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 26027 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 26028 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 26029 } 26030 if (flags & TH_RST) { 26031 (void) tcp_clean_death(tcp, 0, 28); 26032 goto done; 26033 } 26034 if (flags & TH_SYN) { 26035 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 26036 TH_RST|TH_ACK); 26037 /* 26038 * Do not delete the TCP structure if it is in 26039 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 26040 */ 26041 goto done; 26042 } 26043 process_ack: 26044 if (flags & TH_ACK) { 26045 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 26046 if (bytes_acked <= 0) { 26047 if (bytes_acked == 0 && seg_len == 0 && 26048 new_swnd == tcp->tcp_swnd) 26049 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 26050 } else { 26051 /* Acks something not sent */ 26052 flags |= TH_ACK_NEEDED; 26053 } 26054 } 26055 if (flags & TH_ACK_NEEDED) { 26056 /* 26057 * Time to send an ack for some reason. 26058 */ 26059 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 26060 tcp->tcp_rnxt, TH_ACK); 26061 } 26062 done: 26063 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26064 DB_CKSUMSTART(mp) = 0; 26065 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 26066 TCP_STAT(tcps, tcp_time_wait_syn_fail); 26067 } 26068 freemsg(mp); 26069 } 26070 26071 /* 26072 * Allocate a T_SVR4_OPTMGMT_REQ. 26073 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 26074 * that tcp_rput_other can drop the acks. 26075 */ 26076 static mblk_t * 26077 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 26078 { 26079 mblk_t *mp; 26080 struct T_optmgmt_req *tor; 26081 struct opthdr *oh; 26082 uint_t size; 26083 char *optptr; 26084 26085 size = sizeof (*tor) + sizeof (*oh) + optlen; 26086 mp = allocb(size, BPRI_MED); 26087 if (mp == NULL) 26088 return (NULL); 26089 26090 mp->b_wptr += size; 26091 mp->b_datap->db_type = M_PROTO; 26092 tor = (struct T_optmgmt_req *)mp->b_rptr; 26093 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 26094 tor->MGMT_flags = T_NEGOTIATE; 26095 tor->OPT_length = sizeof (*oh) + optlen; 26096 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 26097 26098 oh = (struct opthdr *)&tor[1]; 26099 oh->level = level; 26100 oh->name = cmd; 26101 oh->len = optlen; 26102 if (optlen != 0) { 26103 optptr = (char *)&oh[1]; 26104 bcopy(opt, optptr, optlen); 26105 } 26106 return (mp); 26107 } 26108 26109 /* 26110 * TCP Timers Implementation. 26111 */ 26112 timeout_id_t 26113 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 26114 { 26115 mblk_t *mp; 26116 tcp_timer_t *tcpt; 26117 tcp_t *tcp = connp->conn_tcp; 26118 tcp_stack_t *tcps = tcp->tcp_tcps; 26119 26120 ASSERT(connp->conn_sqp != NULL); 26121 26122 TCP_DBGSTAT(tcps, tcp_timeout_calls); 26123 26124 if (tcp->tcp_timercache == NULL) { 26125 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 26126 } else { 26127 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 26128 mp = tcp->tcp_timercache; 26129 tcp->tcp_timercache = mp->b_next; 26130 mp->b_next = NULL; 26131 ASSERT(mp->b_wptr == NULL); 26132 } 26133 26134 CONN_INC_REF(connp); 26135 tcpt = (tcp_timer_t *)mp->b_rptr; 26136 tcpt->connp = connp; 26137 tcpt->tcpt_proc = f; 26138 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 26139 return ((timeout_id_t)mp); 26140 } 26141 26142 static void 26143 tcp_timer_callback(void *arg) 26144 { 26145 mblk_t *mp = (mblk_t *)arg; 26146 tcp_timer_t *tcpt; 26147 conn_t *connp; 26148 26149 tcpt = (tcp_timer_t *)mp->b_rptr; 26150 connp = tcpt->connp; 26151 squeue_fill(connp->conn_sqp, mp, 26152 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 26153 } 26154 26155 static void 26156 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 26157 { 26158 tcp_timer_t *tcpt; 26159 conn_t *connp = (conn_t *)arg; 26160 tcp_t *tcp = connp->conn_tcp; 26161 26162 tcpt = (tcp_timer_t *)mp->b_rptr; 26163 ASSERT(connp == tcpt->connp); 26164 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 26165 26166 /* 26167 * If the TCP has reached the closed state, don't proceed any 26168 * further. This TCP logically does not exist on the system. 26169 * tcpt_proc could for example access queues, that have already 26170 * been qprocoff'ed off. Also see comments at the start of tcp_input 26171 */ 26172 if (tcp->tcp_state != TCPS_CLOSED) { 26173 (*tcpt->tcpt_proc)(connp); 26174 } else { 26175 tcp->tcp_timer_tid = 0; 26176 } 26177 tcp_timer_free(connp->conn_tcp, mp); 26178 } 26179 26180 /* 26181 * There is potential race with untimeout and the handler firing at the same 26182 * time. The mblock may be freed by the handler while we are trying to use 26183 * it. But since both should execute on the same squeue, this race should not 26184 * occur. 26185 */ 26186 clock_t 26187 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 26188 { 26189 mblk_t *mp = (mblk_t *)id; 26190 tcp_timer_t *tcpt; 26191 clock_t delta; 26192 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26193 26194 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 26195 26196 if (mp == NULL) 26197 return (-1); 26198 26199 tcpt = (tcp_timer_t *)mp->b_rptr; 26200 ASSERT(tcpt->connp == connp); 26201 26202 delta = untimeout(tcpt->tcpt_tid); 26203 26204 if (delta >= 0) { 26205 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 26206 tcp_timer_free(connp->conn_tcp, mp); 26207 CONN_DEC_REF(connp); 26208 } 26209 26210 return (delta); 26211 } 26212 26213 /* 26214 * Allocate space for the timer event. The allocation looks like mblk, but it is 26215 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26216 * 26217 * Dealing with failures: If we can't allocate from the timer cache we try 26218 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26219 * points to b_rptr. 26220 * If we can't allocate anything using allocb_tryhard(), we perform a last 26221 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26222 * save the actual allocation size in b_datap. 26223 */ 26224 mblk_t * 26225 tcp_timermp_alloc(int kmflags) 26226 { 26227 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26228 kmflags & ~KM_PANIC); 26229 26230 if (mp != NULL) { 26231 mp->b_next = mp->b_prev = NULL; 26232 mp->b_rptr = (uchar_t *)(&mp[1]); 26233 mp->b_wptr = NULL; 26234 mp->b_datap = NULL; 26235 mp->b_queue = NULL; 26236 mp->b_cont = NULL; 26237 } else if (kmflags & KM_PANIC) { 26238 /* 26239 * Failed to allocate memory for the timer. Try allocating from 26240 * dblock caches. 26241 */ 26242 /* ipclassifier calls this from a constructor - hence no tcps */ 26243 TCP_G_STAT(tcp_timermp_allocfail); 26244 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26245 if (mp == NULL) { 26246 size_t size = 0; 26247 /* 26248 * Memory is really low. Try tryhard allocation. 26249 * 26250 * ipclassifier calls this from a constructor - 26251 * hence no tcps 26252 */ 26253 TCP_G_STAT(tcp_timermp_allocdblfail); 26254 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26255 sizeof (tcp_timer_t), &size, kmflags); 26256 mp->b_rptr = (uchar_t *)(&mp[1]); 26257 mp->b_next = mp->b_prev = NULL; 26258 mp->b_wptr = (uchar_t *)-1; 26259 mp->b_datap = (dblk_t *)size; 26260 mp->b_queue = NULL; 26261 mp->b_cont = NULL; 26262 } 26263 ASSERT(mp->b_wptr != NULL); 26264 } 26265 /* ipclassifier calls this from a constructor - hence no tcps */ 26266 TCP_G_DBGSTAT(tcp_timermp_alloced); 26267 26268 return (mp); 26269 } 26270 26271 /* 26272 * Free per-tcp timer cache. 26273 * It can only contain entries from tcp_timercache. 26274 */ 26275 void 26276 tcp_timermp_free(tcp_t *tcp) 26277 { 26278 mblk_t *mp; 26279 26280 while ((mp = tcp->tcp_timercache) != NULL) { 26281 ASSERT(mp->b_wptr == NULL); 26282 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26283 kmem_cache_free(tcp_timercache, mp); 26284 } 26285 } 26286 26287 /* 26288 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26289 * events there already (currently at most two events are cached). 26290 * If the event is not allocated from the timer cache, free it right away. 26291 */ 26292 static void 26293 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26294 { 26295 mblk_t *mp1 = tcp->tcp_timercache; 26296 tcp_stack_t *tcps = tcp->tcp_tcps; 26297 26298 if (mp->b_wptr != NULL) { 26299 /* 26300 * This allocation is not from a timer cache, free it right 26301 * away. 26302 */ 26303 if (mp->b_wptr != (uchar_t *)-1) 26304 freeb(mp); 26305 else 26306 kmem_free(mp, (size_t)mp->b_datap); 26307 } else if (mp1 == NULL || mp1->b_next == NULL) { 26308 /* Cache this timer block for future allocations */ 26309 mp->b_rptr = (uchar_t *)(&mp[1]); 26310 mp->b_next = mp1; 26311 tcp->tcp_timercache = mp; 26312 } else { 26313 kmem_cache_free(tcp_timercache, mp); 26314 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26315 } 26316 } 26317 26318 /* 26319 * End of TCP Timers implementation. 26320 */ 26321 26322 /* 26323 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26324 * on the specified backing STREAMS q. Note, the caller may make the 26325 * decision to call based on the tcp_t.tcp_flow_stopped value which 26326 * when check outside the q's lock is only an advisory check ... 26327 */ 26328 26329 void 26330 tcp_setqfull(tcp_t *tcp) 26331 { 26332 queue_t *q = tcp->tcp_wq; 26333 tcp_stack_t *tcps = tcp->tcp_tcps; 26334 26335 if (!(q->q_flag & QFULL)) { 26336 mutex_enter(QLOCK(q)); 26337 if (!(q->q_flag & QFULL)) { 26338 /* still need to set QFULL */ 26339 q->q_flag |= QFULL; 26340 tcp->tcp_flow_stopped = B_TRUE; 26341 mutex_exit(QLOCK(q)); 26342 TCP_STAT(tcps, tcp_flwctl_on); 26343 } else { 26344 mutex_exit(QLOCK(q)); 26345 } 26346 } 26347 } 26348 26349 void 26350 tcp_clrqfull(tcp_t *tcp) 26351 { 26352 queue_t *q = tcp->tcp_wq; 26353 26354 if (q->q_flag & QFULL) { 26355 mutex_enter(QLOCK(q)); 26356 if (q->q_flag & QFULL) { 26357 q->q_flag &= ~QFULL; 26358 tcp->tcp_flow_stopped = B_FALSE; 26359 mutex_exit(QLOCK(q)); 26360 if (q->q_flag & QWANTW) 26361 qbackenable(q, 0); 26362 } else { 26363 mutex_exit(QLOCK(q)); 26364 } 26365 } 26366 } 26367 26368 26369 /* 26370 * kstats related to squeues i.e. not per IP instance 26371 */ 26372 static void * 26373 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26374 { 26375 kstat_t *ksp; 26376 26377 tcp_g_stat_t template = { 26378 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26379 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26380 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26381 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26382 }; 26383 26384 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26385 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26386 KSTAT_FLAG_VIRTUAL); 26387 26388 if (ksp == NULL) 26389 return (NULL); 26390 26391 bcopy(&template, tcp_g_statp, sizeof (template)); 26392 ksp->ks_data = (void *)tcp_g_statp; 26393 26394 kstat_install(ksp); 26395 return (ksp); 26396 } 26397 26398 static void 26399 tcp_g_kstat_fini(kstat_t *ksp) 26400 { 26401 if (ksp != NULL) { 26402 kstat_delete(ksp); 26403 } 26404 } 26405 26406 26407 static void * 26408 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26409 { 26410 kstat_t *ksp; 26411 26412 tcp_stat_t template = { 26413 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26414 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26415 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26416 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26417 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26418 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26419 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26420 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26421 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26422 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26423 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26424 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26425 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26426 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26427 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26428 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26429 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26430 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26431 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26432 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26433 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26434 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26435 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26436 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26437 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26438 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26439 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26440 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26441 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26442 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26443 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26444 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26445 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26446 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26447 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26448 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26449 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26450 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26451 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26452 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26453 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26454 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26455 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26456 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26457 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26458 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26459 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26460 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26461 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26462 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26463 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26464 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26465 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26466 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26467 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26468 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26469 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26470 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26471 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26472 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26473 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26474 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26475 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26476 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26477 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26478 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26479 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26480 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26481 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26482 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26483 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26484 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26485 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26486 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26487 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26488 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26489 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26490 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26491 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26492 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26493 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26494 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26495 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26496 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26497 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26498 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26499 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26500 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26501 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26502 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26503 }; 26504 26505 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26506 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26507 KSTAT_FLAG_VIRTUAL, stackid); 26508 26509 if (ksp == NULL) 26510 return (NULL); 26511 26512 bcopy(&template, tcps_statisticsp, sizeof (template)); 26513 ksp->ks_data = (void *)tcps_statisticsp; 26514 ksp->ks_private = (void *)(uintptr_t)stackid; 26515 26516 kstat_install(ksp); 26517 return (ksp); 26518 } 26519 26520 static void 26521 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26522 { 26523 if (ksp != NULL) { 26524 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26525 kstat_delete_netstack(ksp, stackid); 26526 } 26527 } 26528 26529 /* 26530 * TCP Kstats implementation 26531 */ 26532 static void * 26533 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26534 { 26535 kstat_t *ksp; 26536 26537 tcp_named_kstat_t template = { 26538 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26539 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26540 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26541 { "maxConn", KSTAT_DATA_INT32, 0 }, 26542 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26543 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26544 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26545 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26546 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26547 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26548 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26549 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26550 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26551 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26552 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26553 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26554 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26555 { "outAck", KSTAT_DATA_UINT32, 0 }, 26556 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26557 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26558 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26559 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26560 { "outControl", KSTAT_DATA_UINT32, 0 }, 26561 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26562 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26563 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26564 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26565 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26566 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26567 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26568 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26569 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26570 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26571 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26572 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26573 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26574 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26575 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26576 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26577 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26578 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26579 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26580 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26581 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26582 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26583 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26584 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26585 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26586 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26587 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26588 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26589 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26590 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26591 }; 26592 26593 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26594 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26595 26596 if (ksp == NULL) 26597 return (NULL); 26598 26599 template.rtoAlgorithm.value.ui32 = 4; 26600 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26601 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26602 template.maxConn.value.i32 = -1; 26603 26604 bcopy(&template, ksp->ks_data, sizeof (template)); 26605 ksp->ks_update = tcp_kstat_update; 26606 ksp->ks_private = (void *)(uintptr_t)stackid; 26607 26608 kstat_install(ksp); 26609 return (ksp); 26610 } 26611 26612 static void 26613 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26614 { 26615 if (ksp != NULL) { 26616 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26617 kstat_delete_netstack(ksp, stackid); 26618 } 26619 } 26620 26621 static int 26622 tcp_kstat_update(kstat_t *kp, int rw) 26623 { 26624 tcp_named_kstat_t *tcpkp; 26625 tcp_t *tcp; 26626 connf_t *connfp; 26627 conn_t *connp; 26628 int i; 26629 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26630 netstack_t *ns; 26631 tcp_stack_t *tcps; 26632 ip_stack_t *ipst; 26633 26634 if ((kp == NULL) || (kp->ks_data == NULL)) 26635 return (EIO); 26636 26637 if (rw == KSTAT_WRITE) 26638 return (EACCES); 26639 26640 ns = netstack_find_by_stackid(stackid); 26641 if (ns == NULL) 26642 return (-1); 26643 tcps = ns->netstack_tcp; 26644 if (tcps == NULL) { 26645 netstack_rele(ns); 26646 return (-1); 26647 } 26648 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26649 26650 tcpkp->currEstab.value.ui32 = 0; 26651 26652 ipst = ns->netstack_ip; 26653 26654 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26655 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26656 connp = NULL; 26657 while ((connp = 26658 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26659 tcp = connp->conn_tcp; 26660 switch (tcp_snmp_state(tcp)) { 26661 case MIB2_TCP_established: 26662 case MIB2_TCP_closeWait: 26663 tcpkp->currEstab.value.ui32++; 26664 break; 26665 } 26666 } 26667 } 26668 26669 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26670 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26671 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26672 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26673 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26674 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26675 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26676 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26677 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26678 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26679 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26680 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26681 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26682 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26683 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26684 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26685 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26686 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26687 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26688 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26689 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26690 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26691 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26692 tcpkp->inDataInorderSegs.value.ui32 = 26693 tcps->tcps_mib.tcpInDataInorderSegs; 26694 tcpkp->inDataInorderBytes.value.ui32 = 26695 tcps->tcps_mib.tcpInDataInorderBytes; 26696 tcpkp->inDataUnorderSegs.value.ui32 = 26697 tcps->tcps_mib.tcpInDataUnorderSegs; 26698 tcpkp->inDataUnorderBytes.value.ui32 = 26699 tcps->tcps_mib.tcpInDataUnorderBytes; 26700 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26701 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26702 tcpkp->inDataPartDupSegs.value.ui32 = 26703 tcps->tcps_mib.tcpInDataPartDupSegs; 26704 tcpkp->inDataPartDupBytes.value.ui32 = 26705 tcps->tcps_mib.tcpInDataPartDupBytes; 26706 tcpkp->inDataPastWinSegs.value.ui32 = 26707 tcps->tcps_mib.tcpInDataPastWinSegs; 26708 tcpkp->inDataPastWinBytes.value.ui32 = 26709 tcps->tcps_mib.tcpInDataPastWinBytes; 26710 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26711 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26712 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26713 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26714 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26715 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26716 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26717 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26718 tcpkp->timKeepaliveProbe.value.ui32 = 26719 tcps->tcps_mib.tcpTimKeepaliveProbe; 26720 tcpkp->timKeepaliveDrop.value.ui32 = 26721 tcps->tcps_mib.tcpTimKeepaliveDrop; 26722 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26723 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26724 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26725 tcpkp->outSackRetransSegs.value.ui32 = 26726 tcps->tcps_mib.tcpOutSackRetransSegs; 26727 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26728 26729 netstack_rele(ns); 26730 return (0); 26731 } 26732 26733 void 26734 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26735 { 26736 uint16_t hdr_len; 26737 ipha_t *ipha; 26738 uint8_t *nexthdrp; 26739 tcph_t *tcph; 26740 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26741 26742 /* Already has an eager */ 26743 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26744 TCP_STAT(tcps, tcp_reinput_syn); 26745 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 26746 connp, SQTAG_TCP_REINPUT_EAGER); 26747 return; 26748 } 26749 26750 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26751 case IPV4_VERSION: 26752 ipha = (ipha_t *)mp->b_rptr; 26753 hdr_len = IPH_HDR_LENGTH(ipha); 26754 break; 26755 case IPV6_VERSION: 26756 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26757 &hdr_len, &nexthdrp)) { 26758 CONN_DEC_REF(connp); 26759 freemsg(mp); 26760 return; 26761 } 26762 break; 26763 } 26764 26765 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26766 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26767 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26768 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26769 } 26770 26771 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 26772 SQTAG_TCP_REINPUT); 26773 } 26774 26775 static squeue_func_t 26776 tcp_squeue_switch(int val) 26777 { 26778 squeue_func_t rval = squeue_fill; 26779 26780 switch (val) { 26781 case 1: 26782 rval = squeue_enter_nodrain; 26783 break; 26784 case 2: 26785 rval = squeue_enter; 26786 break; 26787 default: 26788 break; 26789 } 26790 return (rval); 26791 } 26792 26793 /* 26794 * This is called once for each squeue - globally for all stack 26795 * instances. 26796 */ 26797 static void 26798 tcp_squeue_add(squeue_t *sqp) 26799 { 26800 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26801 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26802 26803 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26804 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 26805 sqp, TCP_TIME_WAIT_DELAY); 26806 if (tcp_free_list_max_cnt == 0) { 26807 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26808 max_ncpus : boot_max_ncpus); 26809 26810 /* 26811 * Limit number of entries to 1% of availble memory / tcp_ncpus 26812 */ 26813 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26814 (tcp_ncpus * sizeof (tcp_t) * 100); 26815 } 26816 tcp_time_wait->tcp_free_list_cnt = 0; 26817 } 26818