1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 const char tcp_version[] = "%Z%%M% %I% %E% SMI"; 30 31 32 #include <sys/types.h> 33 #include <sys/stream.h> 34 #include <sys/strsun.h> 35 #include <sys/strsubr.h> 36 #include <sys/stropts.h> 37 #include <sys/strlog.h> 38 #include <sys/strsun.h> 39 #define _SUN_TPI_VERSION 2 40 #include <sys/tihdr.h> 41 #include <sys/timod.h> 42 #include <sys/ddi.h> 43 #include <sys/sunddi.h> 44 #include <sys/suntpi.h> 45 #include <sys/xti_inet.h> 46 #include <sys/cmn_err.h> 47 #include <sys/debug.h> 48 #include <sys/sdt.h> 49 #include <sys/vtrace.h> 50 #include <sys/kmem.h> 51 #include <sys/ethernet.h> 52 #include <sys/cpuvar.h> 53 #include <sys/dlpi.h> 54 #include <sys/multidata.h> 55 #include <sys/multidata_impl.h> 56 #include <sys/pattr.h> 57 #include <sys/policy.h> 58 #include <sys/priv.h> 59 #include <sys/zone.h> 60 #include <sys/sunldi.h> 61 62 #include <sys/errno.h> 63 #include <sys/signal.h> 64 #include <sys/socket.h> 65 #include <sys/sockio.h> 66 #include <sys/isa_defs.h> 67 #include <sys/md5.h> 68 #include <sys/random.h> 69 #include <netinet/in.h> 70 #include <netinet/tcp.h> 71 #include <netinet/ip6.h> 72 #include <netinet/icmp6.h> 73 #include <net/if.h> 74 #include <net/route.h> 75 #include <inet/ipsec_impl.h> 76 77 #include <inet/common.h> 78 #include <inet/ip.h> 79 #include <inet/ip_impl.h> 80 #include <inet/ip6.h> 81 #include <inet/ip_ndp.h> 82 #include <inet/mi.h> 83 #include <inet/mib2.h> 84 #include <inet/nd.h> 85 #include <inet/optcom.h> 86 #include <inet/snmpcom.h> 87 #include <inet/kstatcom.h> 88 #include <inet/tcp.h> 89 #include <inet/tcp_impl.h> 90 #include <net/pfkeyv2.h> 91 #include <inet/ipsec_info.h> 92 #include <inet/ipdrop.h> 93 #include <inet/tcp_trace.h> 94 95 #include <inet/ipclassifier.h> 96 #include <inet/ip_ire.h> 97 #include <inet/ip_ftable.h> 98 #include <inet/ip_if.h> 99 #include <inet/ipp_common.h> 100 #include <inet/ip_netinfo.h> 101 #include <sys/squeue.h> 102 #include <inet/kssl/ksslapi.h> 103 #include <sys/tsol/label.h> 104 #include <sys/tsol/tnet.h> 105 #include <sys/sdt.h> 106 #include <rpc/pmap_prot.h> 107 108 /* 109 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 110 * 111 * (Read the detailed design doc in PSARC case directory) 112 * 113 * The entire tcp state is contained in tcp_t and conn_t structure 114 * which are allocated in tandem using ipcl_conn_create() and passing 115 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 116 * the references on the tcp_t. The tcp_t structure is never compressed 117 * and packets always land on the correct TCP perimeter from the time 118 * eager is created till the time tcp_t dies (as such the old mentat 119 * TCP global queue is not used for detached state and no IPSEC checking 120 * is required). The global queue is still allocated to send out resets 121 * for connection which have no listeners and IP directly calls 122 * tcp_xmit_listeners_reset() which does any policy check. 123 * 124 * Protection and Synchronisation mechanism: 125 * 126 * The tcp data structure does not use any kind of lock for protecting 127 * its state but instead uses 'squeues' for mutual exclusion from various 128 * read and write side threads. To access a tcp member, the thread should 129 * always be behind squeue (via squeue_enter, squeue_enter_nodrain, or 130 * squeue_fill). Since the squeues allow a direct function call, caller 131 * can pass any tcp function having prototype of edesc_t as argument 132 * (different from traditional STREAMs model where packets come in only 133 * designated entry points). The list of functions that can be directly 134 * called via squeue are listed before the usual function prototype. 135 * 136 * Referencing: 137 * 138 * TCP is MT-Hot and we use a reference based scheme to make sure that the 139 * tcp structure doesn't disappear when its needed. When the application 140 * creates an outgoing connection or accepts an incoming connection, we 141 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 142 * The IP reference is just a symbolic reference since ip_tcpclose() 143 * looks at tcp structure after tcp_close_output() returns which could 144 * have dropped the last TCP reference. So as long as the connection is 145 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 146 * conn_t. The classifier puts its own reference when the connection is 147 * inserted in listen or connected hash. Anytime a thread needs to enter 148 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 149 * on write side or by doing a classify on read side and then puts a 150 * reference on the conn before doing squeue_enter/tryenter/fill. For 151 * read side, the classifier itself puts the reference under fanout lock 152 * to make sure that tcp can't disappear before it gets processed. The 153 * squeue will drop this reference automatically so the called function 154 * doesn't have to do a DEC_REF. 155 * 156 * Opening a new connection: 157 * 158 * The outgoing connection open is pretty simple. tcp_open() does the 159 * work in creating the conn/tcp structure and initializing it. The 160 * squeue assignment is done based on the CPU the application 161 * is running on. So for outbound connections, processing is always done 162 * on application CPU which might be different from the incoming CPU 163 * being interrupted by the NIC. An optimal way would be to figure out 164 * the NIC <-> CPU binding at listen time, and assign the outgoing 165 * connection to the squeue attached to the CPU that will be interrupted 166 * for incoming packets (we know the NIC based on the bind IP address). 167 * This might seem like a problem if more data is going out but the 168 * fact is that in most cases the transmit is ACK driven transmit where 169 * the outgoing data normally sits on TCP's xmit queue waiting to be 170 * transmitted. 171 * 172 * Accepting a connection: 173 * 174 * This is a more interesting case because of various races involved in 175 * establishing a eager in its own perimeter. Read the meta comment on 176 * top of tcp_conn_request(). But briefly, the squeue is picked by 177 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 178 * 179 * Closing a connection: 180 * 181 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 182 * via squeue to do the close and mark the tcp as detached if the connection 183 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 184 * reference but tcp_close() drop IP's reference always. So if tcp was 185 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 186 * and 1 because it is in classifier's connected hash. This is the condition 187 * we use to determine that its OK to clean up the tcp outside of squeue 188 * when time wait expires (check the ref under fanout and conn_lock and 189 * if it is 2, remove it from fanout hash and kill it). 190 * 191 * Although close just drops the necessary references and marks the 192 * tcp_detached state, tcp_close needs to know the tcp_detached has been 193 * set (under squeue) before letting the STREAM go away (because a 194 * inbound packet might attempt to go up the STREAM while the close 195 * has happened and tcp_detached is not set). So a special lock and 196 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 197 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 198 * tcp_detached. 199 * 200 * Special provisions and fast paths: 201 * 202 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 203 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 204 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 205 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 206 * check to send packets directly to tcp_rput_data via squeue. Everyone 207 * else comes through tcp_input() on the read side. 208 * 209 * We also make special provisions for sockfs by marking tcp_issocket 210 * whenever we have only sockfs on top of TCP. This allows us to skip 211 * putting the tcp in acceptor hash since a sockfs listener can never 212 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 213 * since eager has already been allocated and the accept now happens 214 * on acceptor STREAM. There is a big blob of comment on top of 215 * tcp_conn_request explaining the new accept. When socket is POP'd, 216 * sockfs sends us an ioctl to mark the fact and we go back to old 217 * behaviour. Once tcp_issocket is unset, its never set for the 218 * life of that connection. 219 * 220 * IPsec notes : 221 * 222 * Since a packet is always executed on the correct TCP perimeter 223 * all IPsec processing is defered to IP including checking new 224 * connections and setting IPSEC policies for new connection. The 225 * only exception is tcp_xmit_listeners_reset() which is called 226 * directly from IP and needs to policy check to see if TH_RST 227 * can be sent out. 228 * 229 * PFHooks notes : 230 * 231 * For mdt case, one meta buffer contains multiple packets. Mblks for every 232 * packet are assembled and passed to the hooks. When packets are blocked, 233 * or boundary of any packet is changed, the mdt processing is stopped, and 234 * packets of the meta buffer are send to the IP path one by one. 235 */ 236 237 extern major_t TCP6_MAJ; 238 239 /* 240 * Values for squeue switch: 241 * 1: squeue_enter_nodrain 242 * 2: squeue_enter 243 * 3: squeue_fill 244 */ 245 int tcp_squeue_close = 2; /* Setable in /etc/system */ 246 int tcp_squeue_wput = 2; 247 248 squeue_func_t tcp_squeue_close_proc; 249 squeue_func_t tcp_squeue_wput_proc; 250 251 /* 252 * This controls how tiny a write must be before we try to copy it 253 * into the the mblk on the tail of the transmit queue. Not much 254 * speedup is observed for values larger than sixteen. Zero will 255 * disable the optimisation. 256 */ 257 int tcp_tx_pull_len = 16; 258 259 /* 260 * TCP Statistics. 261 * 262 * How TCP statistics work. 263 * 264 * There are two types of statistics invoked by two macros. 265 * 266 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 267 * supposed to be used in non MT-hot paths of the code. 268 * 269 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 270 * supposed to be used for DEBUG purposes and may be used on a hot path. 271 * 272 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 273 * (use "kstat tcp" to get them). 274 * 275 * There is also additional debugging facility that marks tcp_clean_death() 276 * instances and saves them in tcp_t structure. It is triggered by 277 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 278 * tcp_clean_death() calls that counts the number of times each tag was hit. It 279 * is triggered by TCP_CLD_COUNTERS define. 280 * 281 * How to add new counters. 282 * 283 * 1) Add a field in the tcp_stat structure describing your counter. 284 * 2) Add a line in the template in tcp_kstat2_init() with the name 285 * of the counter. 286 * 287 * IMPORTANT!! - make sure that both are in sync !! 288 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 289 * 290 * Please avoid using private counters which are not kstat-exported. 291 * 292 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 293 * in tcp_t structure. 294 * 295 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 296 */ 297 298 #ifndef TCP_DEBUG_COUNTER 299 #ifdef DEBUG 300 #define TCP_DEBUG_COUNTER 1 301 #else 302 #define TCP_DEBUG_COUNTER 0 303 #endif 304 #endif 305 306 #define TCP_CLD_COUNTERS 0 307 308 #define TCP_TAG_CLEAN_DEATH 1 309 #define TCP_MAX_CLEAN_DEATH_TAG 32 310 311 #ifdef lint 312 static int _lint_dummy_; 313 #endif 314 315 #if TCP_CLD_COUNTERS 316 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 317 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 318 #elif defined(lint) 319 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 320 #else 321 #define TCP_CLD_STAT(x) 322 #endif 323 324 #if TCP_DEBUG_COUNTER 325 #define TCP_DBGSTAT(tcps, x) \ 326 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 327 #define TCP_G_DBGSTAT(x) \ 328 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 329 #elif defined(lint) 330 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 331 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 332 #else 333 #define TCP_DBGSTAT(tcps, x) 334 #define TCP_G_DBGSTAT(x) 335 #endif 336 337 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 338 339 tcp_g_stat_t tcp_g_statistics; 340 kstat_t *tcp_g_kstat; 341 342 /* 343 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 344 * tcp write side. 345 */ 346 #define CALL_IP_WPUT(connp, q, mp) { \ 347 tcp_stack_t *tcps; \ 348 \ 349 tcps = connp->conn_netstack->netstack_tcp; \ 350 ASSERT(((q)->q_flag & QREADR) == 0); \ 351 TCP_DBGSTAT(tcps, tcp_ip_output); \ 352 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 353 } 354 355 /* Macros for timestamp comparisons */ 356 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 357 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 358 359 /* 360 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 361 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 362 * by adding three components: a time component which grows by 1 every 4096 363 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 364 * a per-connection component which grows by 125000 for every new connection; 365 * and an "extra" component that grows by a random amount centered 366 * approximately on 64000. This causes the the ISS generator to cycle every 367 * 4.89 hours if no TCP connections are made, and faster if connections are 368 * made. 369 * 370 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 371 * components: a time component which grows by 250000 every second; and 372 * a per-connection component which grows by 125000 for every new connections. 373 * 374 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 375 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 376 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 377 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 378 * password. 379 */ 380 #define ISS_INCR 250000 381 #define ISS_NSEC_SHT 12 382 383 static sin_t sin_null; /* Zero address for quick clears */ 384 static sin6_t sin6_null; /* Zero address for quick clears */ 385 386 /* 387 * This implementation follows the 4.3BSD interpretation of the urgent 388 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 389 * incompatible changes in protocols like telnet and rlogin. 390 */ 391 #define TCP_OLD_URP_INTERPRETATION 1 392 393 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 394 (TCP_IS_DETACHED(tcp) && \ 395 (!(tcp)->tcp_hard_binding)) 396 397 /* 398 * TCP reassembly macros. We hide starting and ending sequence numbers in 399 * b_next and b_prev of messages on the reassembly queue. The messages are 400 * chained using b_cont. These macros are used in tcp_reass() so we don't 401 * have to see the ugly casts and assignments. 402 */ 403 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 404 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 405 (mblk_t *)(uintptr_t)(u)) 406 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 407 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 408 (mblk_t *)(uintptr_t)(u)) 409 410 /* 411 * Implementation of TCP Timers. 412 * ============================= 413 * 414 * INTERFACE: 415 * 416 * There are two basic functions dealing with tcp timers: 417 * 418 * timeout_id_t tcp_timeout(connp, func, time) 419 * clock_t tcp_timeout_cancel(connp, timeout_id) 420 * TCP_TIMER_RESTART(tcp, intvl) 421 * 422 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 423 * after 'time' ticks passed. The function called by timeout() must adhere to 424 * the same restrictions as a driver soft interrupt handler - it must not sleep 425 * or call other functions that might sleep. The value returned is the opaque 426 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 427 * cancel the request. The call to tcp_timeout() may fail in which case it 428 * returns zero. This is different from the timeout(9F) function which never 429 * fails. 430 * 431 * The call-back function 'func' always receives 'connp' as its single 432 * argument. It is always executed in the squeue corresponding to the tcp 433 * structure. The tcp structure is guaranteed to be present at the time the 434 * call-back is called. 435 * 436 * NOTE: The call-back function 'func' is never called if tcp is in 437 * the TCPS_CLOSED state. 438 * 439 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 440 * request. locks acquired by the call-back routine should not be held across 441 * the call to tcp_timeout_cancel() or a deadlock may result. 442 * 443 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 444 * Otherwise, it returns an integer value greater than or equal to 0. In 445 * particular, if the call-back function is already placed on the squeue, it can 446 * not be canceled. 447 * 448 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 449 * within squeue context corresponding to the tcp instance. Since the 450 * call-back is also called via the same squeue, there are no race 451 * conditions described in untimeout(9F) manual page since all calls are 452 * strictly serialized. 453 * 454 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 455 * stored in tcp_timer_tid and starts a new one using 456 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 457 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 458 * field. 459 * 460 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 461 * call-back may still be called, so it is possible tcp_timer() will be 462 * called several times. This should not be a problem since tcp_timer() 463 * should always check the tcp instance state. 464 * 465 * 466 * IMPLEMENTATION: 467 * 468 * TCP timers are implemented using three-stage process. The call to 469 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 470 * when the timer expires. The tcp_timer_callback() arranges the call of the 471 * tcp_timer_handler() function via squeue corresponding to the tcp 472 * instance. The tcp_timer_handler() calls actual requested timeout call-back 473 * and passes tcp instance as an argument to it. Information is passed between 474 * stages using the tcp_timer_t structure which contains the connp pointer, the 475 * tcp call-back to call and the timeout id returned by the timeout(9F). 476 * 477 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 478 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 479 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 480 * returns the pointer to this mblk. 481 * 482 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 483 * looks like a normal mblk without actual dblk attached to it. 484 * 485 * To optimize performance each tcp instance holds a small cache of timer 486 * mblocks. In the current implementation it caches up to two timer mblocks per 487 * tcp instance. The cache is preserved over tcp frees and is only freed when 488 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 489 * timer processing happens on a corresponding squeue, the cache manipulation 490 * does not require any locks. Experiments show that majority of timer mblocks 491 * allocations are satisfied from the tcp cache and do not involve kmem calls. 492 * 493 * The tcp_timeout() places a refhold on the connp instance which guarantees 494 * that it will be present at the time the call-back function fires. The 495 * tcp_timer_handler() drops the reference after calling the call-back, so the 496 * call-back function does not need to manipulate the references explicitly. 497 */ 498 499 typedef struct tcp_timer_s { 500 conn_t *connp; 501 void (*tcpt_proc)(void *); 502 timeout_id_t tcpt_tid; 503 } tcp_timer_t; 504 505 static kmem_cache_t *tcp_timercache; 506 kmem_cache_t *tcp_sack_info_cache; 507 kmem_cache_t *tcp_iphc_cache; 508 509 /* 510 * For scalability, we must not run a timer for every TCP connection 511 * in TIME_WAIT state. To see why, consider (for time wait interval of 512 * 4 minutes): 513 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 514 * 515 * This list is ordered by time, so you need only delete from the head 516 * until you get to entries which aren't old enough to delete yet. 517 * The list consists of only the detached TIME_WAIT connections. 518 * 519 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 520 * becomes detached TIME_WAIT (either by changing the state and already 521 * being detached or the other way around). This means that the TIME_WAIT 522 * state can be extended (up to doubled) if the connection doesn't become 523 * detached for a long time. 524 * 525 * The list manipulations (including tcp_time_wait_next/prev) 526 * are protected by the tcp_time_wait_lock. The content of the 527 * detached TIME_WAIT connections is protected by the normal perimeters. 528 * 529 * This list is per squeue and squeues are shared across the tcp_stack_t's. 530 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 531 * and conn_netstack. 532 * The tcp_t's that are added to tcp_free_list are disassociated and 533 * have NULL tcp_tcps and conn_netstack pointers. 534 */ 535 typedef struct tcp_squeue_priv_s { 536 kmutex_t tcp_time_wait_lock; 537 timeout_id_t tcp_time_wait_tid; 538 tcp_t *tcp_time_wait_head; 539 tcp_t *tcp_time_wait_tail; 540 tcp_t *tcp_free_list; 541 uint_t tcp_free_list_cnt; 542 } tcp_squeue_priv_t; 543 544 /* 545 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 546 * Running it every 5 seconds seems to give the best results. 547 */ 548 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 549 550 /* 551 * To prevent memory hog, limit the number of entries in tcp_free_list 552 * to 1% of available memory / number of cpus 553 */ 554 uint_t tcp_free_list_max_cnt = 0; 555 556 #define TCP_XMIT_LOWATER 4096 557 #define TCP_XMIT_HIWATER 49152 558 #define TCP_RECV_LOWATER 2048 559 #define TCP_RECV_HIWATER 49152 560 561 /* 562 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 563 */ 564 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 565 566 #define TIDUSZ 4096 /* transport interface data unit size */ 567 568 /* 569 * Bind hash list size and has function. It has to be a power of 2 for 570 * hashing. 571 */ 572 #define TCP_BIND_FANOUT_SIZE 512 573 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 574 /* 575 * Size of listen and acceptor hash list. It has to be a power of 2 for 576 * hashing. 577 */ 578 #define TCP_FANOUT_SIZE 256 579 580 #ifdef _ILP32 581 #define TCP_ACCEPTOR_HASH(accid) \ 582 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 583 #else 584 #define TCP_ACCEPTOR_HASH(accid) \ 585 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 586 #endif /* _ILP32 */ 587 588 #define IP_ADDR_CACHE_SIZE 2048 589 #define IP_ADDR_CACHE_HASH(faddr) \ 590 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 591 592 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 593 #define TCP_HSP_HASH_SIZE 256 594 595 #define TCP_HSP_HASH(addr) \ 596 (((addr>>24) ^ (addr >>16) ^ \ 597 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 598 599 /* 600 * TCP options struct returned from tcp_parse_options. 601 */ 602 typedef struct tcp_opt_s { 603 uint32_t tcp_opt_mss; 604 uint32_t tcp_opt_wscale; 605 uint32_t tcp_opt_ts_val; 606 uint32_t tcp_opt_ts_ecr; 607 tcp_t *tcp; 608 } tcp_opt_t; 609 610 /* 611 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 612 */ 613 614 #ifdef _BIG_ENDIAN 615 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 616 (TCPOPT_TSTAMP << 8) | 10) 617 #else 618 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 619 (TCPOPT_NOP << 8) | TCPOPT_NOP) 620 #endif 621 622 /* 623 * Flags returned from tcp_parse_options. 624 */ 625 #define TCP_OPT_MSS_PRESENT 1 626 #define TCP_OPT_WSCALE_PRESENT 2 627 #define TCP_OPT_TSTAMP_PRESENT 4 628 #define TCP_OPT_SACK_OK_PRESENT 8 629 #define TCP_OPT_SACK_PRESENT 16 630 631 /* TCP option length */ 632 #define TCPOPT_NOP_LEN 1 633 #define TCPOPT_MAXSEG_LEN 4 634 #define TCPOPT_WS_LEN 3 635 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 636 #define TCPOPT_TSTAMP_LEN 10 637 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 638 #define TCPOPT_SACK_OK_LEN 2 639 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 640 #define TCPOPT_REAL_SACK_LEN 4 641 #define TCPOPT_MAX_SACK_LEN 36 642 #define TCPOPT_HEADER_LEN 2 643 644 /* TCP cwnd burst factor. */ 645 #define TCP_CWND_INFINITE 65535 646 #define TCP_CWND_SS 3 647 #define TCP_CWND_NORMAL 5 648 649 /* Maximum TCP initial cwin (start/restart). */ 650 #define TCP_MAX_INIT_CWND 8 651 652 /* 653 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 654 * either tcp_slow_start_initial or tcp_slow_start_after idle 655 * depending on the caller. If the upper layer has not used the 656 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 657 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 658 * If the upper layer has changed set the tcp_init_cwnd, just use 659 * it to calculate the tcp_cwnd. 660 */ 661 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 662 { \ 663 if ((tcp)->tcp_init_cwnd == 0) { \ 664 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 665 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 666 } else { \ 667 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 668 } \ 669 tcp->tcp_cwnd_cnt = 0; \ 670 } 671 672 /* TCP Timer control structure */ 673 typedef struct tcpt_s { 674 pfv_t tcpt_pfv; /* The routine we are to call */ 675 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 676 } tcpt_t; 677 678 /* Host Specific Parameter structure */ 679 typedef struct tcp_hsp { 680 struct tcp_hsp *tcp_hsp_next; 681 in6_addr_t tcp_hsp_addr_v6; 682 in6_addr_t tcp_hsp_subnet_v6; 683 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 684 int32_t tcp_hsp_sendspace; 685 int32_t tcp_hsp_recvspace; 686 int32_t tcp_hsp_tstamp; 687 } tcp_hsp_t; 688 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 689 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 690 691 /* 692 * Functions called directly via squeue having a prototype of edesc_t. 693 */ 694 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 695 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 696 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 697 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 698 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 699 void tcp_input(void *arg, mblk_t *mp, void *arg2); 700 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 701 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 702 void tcp_output(void *arg, mblk_t *mp, void *arg2); 703 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 704 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 705 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 706 707 708 /* Prototype for TCP functions */ 709 static void tcp_random_init(void); 710 int tcp_random(void); 711 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 712 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 713 tcp_t *eager); 714 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 715 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 716 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 717 boolean_t user_specified); 718 static void tcp_closei_local(tcp_t *tcp); 719 static void tcp_close_detached(tcp_t *tcp); 720 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 721 mblk_t *idmp, mblk_t **defermp); 722 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 723 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 724 in_port_t dstport, uint_t srcid); 725 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 726 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 727 uint32_t scope_id); 728 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 729 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 730 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 731 static char *tcp_display(tcp_t *tcp, char *, char); 732 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 733 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 734 static void tcp_eager_unlink(tcp_t *tcp); 735 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 736 int unixerr); 737 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 738 int tlierr, int unixerr); 739 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 740 cred_t *cr); 741 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 742 char *value, caddr_t cp, cred_t *cr); 743 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 744 char *value, caddr_t cp, cred_t *cr); 745 static int tcp_tpistate(tcp_t *tcp); 746 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 747 int caller_holds_lock); 748 static void tcp_bind_hash_remove(tcp_t *tcp); 749 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 750 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 751 static void tcp_acceptor_hash_remove(tcp_t *tcp); 752 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 753 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 754 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 755 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 756 void tcp_g_q_setup(tcp_stack_t *); 757 void tcp_g_q_create(tcp_stack_t *); 758 void tcp_g_q_destroy(tcp_stack_t *); 759 static int tcp_header_init_ipv4(tcp_t *tcp); 760 static int tcp_header_init_ipv6(tcp_t *tcp); 761 int tcp_init(tcp_t *tcp, queue_t *q); 762 static int tcp_init_values(tcp_t *tcp); 763 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 764 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 765 t_scalar_t addr_length); 766 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 767 static void tcp_ip_notify(tcp_t *tcp); 768 static mblk_t *tcp_ire_mp(mblk_t *mp); 769 static void tcp_iss_init(tcp_t *tcp); 770 static void tcp_keepalive_killer(void *arg); 771 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 772 static void tcp_mss_set(tcp_t *tcp, uint32_t size); 773 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 774 int *do_disconnectp, int *t_errorp, int *sys_errorp); 775 static boolean_t tcp_allow_connopt_set(int level, int name); 776 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 777 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 778 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 779 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 780 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 781 mblk_t *mblk); 782 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 783 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 784 uchar_t *ptr, uint_t len); 785 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 786 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 787 tcp_stack_t *); 788 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 789 caddr_t cp, cred_t *cr); 790 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 791 caddr_t cp, cred_t *cr); 792 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 793 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 794 caddr_t cp, cred_t *cr); 795 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 796 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 797 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 798 static void tcp_reinit(tcp_t *tcp); 799 static void tcp_reinit_values(tcp_t *tcp); 800 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 801 tcp_t *thisstream, cred_t *cr); 802 803 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 804 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 805 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 806 static void tcp_ss_rexmit(tcp_t *tcp); 807 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 808 static void tcp_process_options(tcp_t *, tcph_t *); 809 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 810 static void tcp_rsrv(queue_t *q); 811 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 812 static int tcp_snmp_state(tcp_t *tcp); 813 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 814 cred_t *cr); 815 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 816 cred_t *cr); 817 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 818 cred_t *cr); 819 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 820 cred_t *cr); 821 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 822 cred_t *cr); 823 static int tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, 824 caddr_t cp, cred_t *cr); 825 static int tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, 826 caddr_t cp, cred_t *cr); 827 static int tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, 828 cred_t *cr); 829 static void tcp_timer(void *arg); 830 static void tcp_timer_callback(void *); 831 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 832 boolean_t random); 833 static in_port_t tcp_get_next_priv_port(const tcp_t *); 834 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 835 void tcp_wput_accept(queue_t *q, mblk_t *mp); 836 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 837 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 838 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 839 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 840 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 841 const int num_sack_blk, int *usable, uint_t *snxt, 842 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 843 const int mdt_thres); 844 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 845 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 846 const int num_sack_blk, int *usable, uint_t *snxt, 847 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 848 const int mdt_thres); 849 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 850 int num_sack_blk); 851 static void tcp_wsrv(queue_t *q); 852 static int tcp_xmit_end(tcp_t *tcp); 853 static void tcp_ack_timer(void *arg); 854 static mblk_t *tcp_ack_mp(tcp_t *tcp); 855 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 856 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 857 zoneid_t zoneid, tcp_stack_t *); 858 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 859 uint32_t ack, int ctl); 860 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 861 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 862 static int setmaxps(queue_t *q, int maxpsz); 863 static void tcp_set_rto(tcp_t *, time_t); 864 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 865 boolean_t, boolean_t); 866 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 867 boolean_t ipsec_mctl); 868 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 869 char *opt, int optlen); 870 static int tcp_build_hdrs(queue_t *, tcp_t *); 871 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 872 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 873 tcph_t *tcph); 874 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 875 boolean_t tcp_reserved_port_add(int, in_port_t *, in_port_t *); 876 boolean_t tcp_reserved_port_del(in_port_t, in_port_t); 877 boolean_t tcp_reserved_port_check(in_port_t, tcp_stack_t *); 878 static tcp_t *tcp_alloc_temp_tcp(in_port_t, tcp_stack_t *); 879 static int tcp_reserved_port_list(queue_t *, mblk_t *, caddr_t, cred_t *); 880 static mblk_t *tcp_mdt_info_mp(mblk_t *); 881 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 882 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 883 const boolean_t, const uint32_t, const uint32_t, 884 const uint32_t, const uint32_t, tcp_stack_t *); 885 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 886 const uint_t, const uint_t, boolean_t *); 887 static mblk_t *tcp_lso_info_mp(mblk_t *); 888 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 889 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 890 extern mblk_t *tcp_timermp_alloc(int); 891 extern void tcp_timermp_free(tcp_t *); 892 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 893 static void tcp_stop_lingering(tcp_t *tcp); 894 static void tcp_close_linger_timeout(void *arg); 895 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 896 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 897 static void tcp_stack_fini(netstackid_t stackid, void *arg); 898 static void *tcp_g_kstat_init(tcp_g_stat_t *); 899 static void tcp_g_kstat_fini(kstat_t *); 900 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 901 static void tcp_kstat_fini(netstackid_t, kstat_t *); 902 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 903 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 904 static int tcp_kstat_update(kstat_t *kp, int rw); 905 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 906 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 907 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 908 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 909 tcph_t *tcph, mblk_t *idmp); 910 static squeue_func_t tcp_squeue_switch(int); 911 912 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *); 913 static int tcp_close(queue_t *, int); 914 static int tcpclose_accept(queue_t *); 915 static int tcp_modclose(queue_t *); 916 static void tcp_wput_mod(queue_t *, mblk_t *); 917 918 static void tcp_squeue_add(squeue_t *); 919 static boolean_t tcp_zcopy_check(tcp_t *); 920 static void tcp_zcopy_notify(tcp_t *); 921 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 922 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 923 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 924 925 extern void tcp_kssl_input(tcp_t *, mblk_t *); 926 927 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 928 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 929 930 /* 931 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 932 * 933 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 934 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 935 * (defined in tcp.h) needs to be filled in and passed into the kernel 936 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 937 * structure contains the four-tuple of a TCP connection and a range of TCP 938 * states (specified by ac_start and ac_end). The use of wildcard addresses 939 * and ports is allowed. Connections with a matching four tuple and a state 940 * within the specified range will be aborted. The valid states for the 941 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 942 * inclusive. 943 * 944 * An application which has its connection aborted by this ioctl will receive 945 * an error that is dependent on the connection state at the time of the abort. 946 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 947 * though a RST packet has been received. If the connection state is equal to 948 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 949 * and all resources associated with the connection will be freed. 950 */ 951 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 952 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 953 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 954 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 955 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 956 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 957 boolean_t, tcp_stack_t *); 958 959 static struct module_info tcp_rinfo = { 960 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 961 }; 962 963 static struct module_info tcp_winfo = { 964 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 965 }; 966 967 /* 968 * Entry points for TCP as a module. It only allows SNMP requests 969 * to pass through. 970 */ 971 struct qinit tcp_mod_rinit = { 972 (pfi_t)putnext, NULL, tcp_open, ip_snmpmod_close, NULL, &tcp_rinfo, 973 }; 974 975 struct qinit tcp_mod_winit = { 976 (pfi_t)ip_snmpmod_wput, NULL, tcp_open, ip_snmpmod_close, NULL, 977 &tcp_rinfo 978 }; 979 980 /* 981 * Entry points for TCP as a device. The normal case which supports 982 * the TCP functionality. 983 */ 984 struct qinit tcp_rinit = { 985 NULL, (pfi_t)tcp_rsrv, tcp_open, tcp_close, NULL, &tcp_rinfo 986 }; 987 988 struct qinit tcp_winit = { 989 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 990 }; 991 992 /* Initial entry point for TCP in socket mode. */ 993 struct qinit tcp_sock_winit = { 994 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 995 }; 996 997 /* 998 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 999 * an accept. Avoid allocating data structures since eager has already 1000 * been created. 1001 */ 1002 struct qinit tcp_acceptor_rinit = { 1003 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1004 }; 1005 1006 struct qinit tcp_acceptor_winit = { 1007 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1008 }; 1009 1010 /* 1011 * Entry points for TCP loopback (read side only) 1012 */ 1013 struct qinit tcp_loopback_rinit = { 1014 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_open, tcp_close, (pfi_t)0, 1015 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1016 }; 1017 1018 struct streamtab tcpinfo = { 1019 &tcp_rinit, &tcp_winit 1020 }; 1021 1022 /* 1023 * Have to ensure that tcp_g_q_close is not done by an 1024 * interrupt thread. 1025 */ 1026 static taskq_t *tcp_taskq; 1027 1028 /* 1029 * TCP has a private interface for other kernel modules to reserve a 1030 * port range for them to use. Once reserved, TCP will not use any ports 1031 * in the range. This interface relies on the TCP_EXCLBIND feature. If 1032 * the semantics of TCP_EXCLBIND is changed, implementation of this interface 1033 * has to be verified. 1034 * 1035 * There can be TCP_RESERVED_PORTS_ARRAY_MAX_SIZE port ranges. Each port 1036 * range can cover at most TCP_RESERVED_PORTS_RANGE_MAX ports. A port 1037 * range is [port a, port b] inclusive. And each port range is between 1038 * TCP_LOWESET_RESERVED_PORT and TCP_LARGEST_RESERVED_PORT inclusive. 1039 * 1040 * Note that the default anonymous port range starts from 32768. There is 1041 * no port "collision" between that and the reserved port range. If there 1042 * is port collision (because the default smallest anonymous port is lowered 1043 * or some apps specifically bind to ports in the reserved port range), the 1044 * system may not be able to reserve a port range even there are enough 1045 * unbound ports as a reserved port range contains consecutive ports . 1046 */ 1047 #define TCP_RESERVED_PORTS_ARRAY_MAX_SIZE 5 1048 #define TCP_RESERVED_PORTS_RANGE_MAX 1000 1049 #define TCP_SMALLEST_RESERVED_PORT 10240 1050 #define TCP_LARGEST_RESERVED_PORT 20480 1051 1052 /* Structure to represent those reserved port ranges. */ 1053 typedef struct tcp_rport_s { 1054 in_port_t lo_port; 1055 in_port_t hi_port; 1056 tcp_t **temp_tcp_array; 1057 } tcp_rport_t; 1058 1059 /* Setable only in /etc/system. Move to ndd? */ 1060 boolean_t tcp_icmp_source_quench = B_FALSE; 1061 1062 /* 1063 * Following assumes TPI alignment requirements stay along 32 bit 1064 * boundaries 1065 */ 1066 #define ROUNDUP32(x) \ 1067 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1068 1069 /* Template for response to info request. */ 1070 static struct T_info_ack tcp_g_t_info_ack = { 1071 T_INFO_ACK, /* PRIM_type */ 1072 0, /* TSDU_size */ 1073 T_INFINITE, /* ETSDU_size */ 1074 T_INVALID, /* CDATA_size */ 1075 T_INVALID, /* DDATA_size */ 1076 sizeof (sin_t), /* ADDR_size */ 1077 0, /* OPT_size - not initialized here */ 1078 TIDUSZ, /* TIDU_size */ 1079 T_COTS_ORD, /* SERV_type */ 1080 TCPS_IDLE, /* CURRENT_state */ 1081 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1082 }; 1083 1084 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1085 T_INFO_ACK, /* PRIM_type */ 1086 0, /* TSDU_size */ 1087 T_INFINITE, /* ETSDU_size */ 1088 T_INVALID, /* CDATA_size */ 1089 T_INVALID, /* DDATA_size */ 1090 sizeof (sin6_t), /* ADDR_size */ 1091 0, /* OPT_size - not initialized here */ 1092 TIDUSZ, /* TIDU_size */ 1093 T_COTS_ORD, /* SERV_type */ 1094 TCPS_IDLE, /* CURRENT_state */ 1095 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1096 }; 1097 1098 #define MS 1L 1099 #define SECONDS (1000 * MS) 1100 #define MINUTES (60 * SECONDS) 1101 #define HOURS (60 * MINUTES) 1102 #define DAYS (24 * HOURS) 1103 1104 #define PARAM_MAX (~(uint32_t)0) 1105 1106 /* Max size IP datagram is 64k - 1 */ 1107 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1108 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1109 /* Max of the above */ 1110 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1111 1112 /* Largest TCP port number */ 1113 #define TCP_MAX_PORT (64 * 1024 - 1) 1114 1115 /* 1116 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1117 * layer header. It has to be a multiple of 4. 1118 */ 1119 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1120 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1121 1122 /* 1123 * All of these are alterable, within the min/max values given, at run time. 1124 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1125 * per the TCP spec. 1126 */ 1127 /* BEGIN CSTYLED */ 1128 static tcpparam_t lcl_tcp_param_arr[] = { 1129 /*min max value name */ 1130 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1131 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1132 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1133 { 1, 1024, 1, "tcp_conn_req_min" }, 1134 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1135 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1136 { 0, 10, 0, "tcp_debug" }, 1137 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1138 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1139 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1140 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1141 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1142 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1143 { 1, 255, 64, "tcp_ipv4_ttl"}, 1144 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1145 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1146 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1147 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1148 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1149 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1150 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1151 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1152 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1153 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1154 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1155 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1156 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1157 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1158 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1159 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1160 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1161 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1162 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1163 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1164 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1165 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1166 { 0, TCP_MSS_MAX, 64, "tcp_co_min"}, 1167 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1168 /* 1169 * Question: What default value should I set for tcp_strong_iss? 1170 */ 1171 { 0, 2, 1, "tcp_strong_iss"}, 1172 { 0, 65536, 20, "tcp_rtt_updates"}, 1173 { 0, 1, 1, "tcp_wscale_always"}, 1174 { 0, 1, 0, "tcp_tstamp_always"}, 1175 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1176 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1177 { 0, 16, 2, "tcp_deferred_acks_max"}, 1178 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1179 { 1, 4, 4, "tcp_slow_start_initial"}, 1180 { 10*MS, 50*MS, 20*MS, "tcp_co_timer_interval"}, 1181 { 0, 2, 2, "tcp_sack_permitted"}, 1182 { 0, 1, 0, "tcp_trace"}, 1183 { 0, 1, 1, "tcp_compression_enabled"}, 1184 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1185 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1186 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1187 { 0, 1, 0, "tcp_rev_src_routes"}, 1188 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1189 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1190 { 0, 16, 8, "tcp_local_dacks_max"}, 1191 { 0, 2, 1, "tcp_ecn_permitted"}, 1192 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1193 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1194 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1195 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1196 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1197 }; 1198 /* END CSTYLED */ 1199 1200 /* 1201 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1202 * each header fragment in the header buffer. Each parameter value has 1203 * to be a multiple of 4 (32-bit aligned). 1204 */ 1205 static tcpparam_t lcl_tcp_mdt_head_param = 1206 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1207 static tcpparam_t lcl_tcp_mdt_tail_param = 1208 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1209 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1210 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1211 1212 /* 1213 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1214 * the maximum number of payload buffers associated per Multidata. 1215 */ 1216 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1217 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1218 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1219 1220 /* Round up the value to the nearest mss. */ 1221 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1222 1223 /* 1224 * Set ECN capable transport (ECT) code point in IP header. 1225 * 1226 * Note that there are 2 ECT code points '01' and '10', which are called 1227 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1228 * point ECT(0) for TCP as described in RFC 2481. 1229 */ 1230 #define SET_ECT(tcp, iph) \ 1231 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1232 /* We need to clear the code point first. */ \ 1233 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1234 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1235 } else { \ 1236 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1237 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1238 } 1239 1240 /* 1241 * The format argument to pass to tcp_display(). 1242 * DISP_PORT_ONLY means that the returned string has only port info. 1243 * DISP_ADDR_AND_PORT means that the returned string also contains the 1244 * remote and local IP address. 1245 */ 1246 #define DISP_PORT_ONLY 1 1247 #define DISP_ADDR_AND_PORT 2 1248 1249 #define NDD_TOO_QUICK_MSG \ 1250 "ndd get info rate too high for non-privileged users, try again " \ 1251 "later.\n" 1252 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1253 1254 #define IS_VMLOANED_MBLK(mp) \ 1255 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1256 1257 1258 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1259 boolean_t tcp_mdt_chain = B_TRUE; 1260 1261 /* 1262 * MDT threshold in the form of effective send MSS multiplier; we take 1263 * the MDT path if the amount of unsent data exceeds the threshold value 1264 * (default threshold is 1*SMSS). 1265 */ 1266 uint_t tcp_mdt_smss_threshold = 1; 1267 1268 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1269 1270 /* 1271 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1272 * tunable settable via NDD. Otherwise, the per-connection behavior is 1273 * determined dynamically during tcp_adapt_ire(), which is the default. 1274 */ 1275 boolean_t tcp_static_maxpsz = B_FALSE; 1276 1277 /* Setable in /etc/system */ 1278 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1279 uint32_t tcp_random_anon_port = 1; 1280 1281 /* 1282 * To reach to an eager in Q0 which can be dropped due to an incoming 1283 * new SYN request when Q0 is full, a new doubly linked list is 1284 * introduced. This list allows to select an eager from Q0 in O(1) time. 1285 * This is needed to avoid spending too much time walking through the 1286 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1287 * this new list has to be a member of Q0. 1288 * This list is headed by listener's tcp_t. When the list is empty, 1289 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1290 * of listener's tcp_t point to listener's tcp_t itself. 1291 * 1292 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1293 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1294 * These macros do not affect the eager's membership to Q0. 1295 */ 1296 1297 1298 #define MAKE_DROPPABLE(listener, eager) \ 1299 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1300 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1301 = (eager); \ 1302 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1303 (eager)->tcp_eager_next_drop_q0 = \ 1304 (listener)->tcp_eager_next_drop_q0; \ 1305 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1306 } 1307 1308 #define MAKE_UNDROPPABLE(eager) \ 1309 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1310 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1311 = (eager)->tcp_eager_prev_drop_q0; \ 1312 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1313 = (eager)->tcp_eager_next_drop_q0; \ 1314 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1315 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1316 } 1317 1318 /* 1319 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1320 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1321 * data, TCP will not respond with an ACK. RFC 793 requires that 1322 * TCP responds with an ACK for such a bogus ACK. By not following 1323 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1324 * an attacker successfully spoofs an acceptable segment to our 1325 * peer; or when our peer is "confused." 1326 */ 1327 uint32_t tcp_drop_ack_unsent_cnt = 10; 1328 1329 /* 1330 * Hook functions to enable cluster networking 1331 * On non-clustered systems these vectors must always be NULL. 1332 */ 1333 1334 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1335 uint8_t *laddrp, in_port_t lport) = NULL; 1336 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1337 uint8_t *laddrp, in_port_t lport) = NULL; 1338 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1339 uint8_t *laddrp, in_port_t lport, 1340 uint8_t *faddrp, in_port_t fport) = NULL; 1341 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1342 uint8_t *laddrp, in_port_t lport, 1343 uint8_t *faddrp, in_port_t fport) = NULL; 1344 1345 /* 1346 * The following are defined in ip.c 1347 */ 1348 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1349 uint8_t *laddrp); 1350 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1351 uint8_t *laddrp, uint8_t *faddrp); 1352 1353 #define CL_INET_CONNECT(tcp) { \ 1354 if (cl_inet_connect != NULL) { \ 1355 /* \ 1356 * Running in cluster mode - register active connection \ 1357 * information \ 1358 */ \ 1359 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1360 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1361 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1362 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1363 (in_port_t)(tcp)->tcp_lport, \ 1364 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1365 (in_port_t)(tcp)->tcp_fport); \ 1366 } \ 1367 } else { \ 1368 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1369 &(tcp)->tcp_ip6h->ip6_src)) {\ 1370 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1371 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1372 (in_port_t)(tcp)->tcp_lport, \ 1373 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1374 (in_port_t)(tcp)->tcp_fport); \ 1375 } \ 1376 } \ 1377 } \ 1378 } 1379 1380 #define CL_INET_DISCONNECT(tcp) { \ 1381 if (cl_inet_disconnect != NULL) { \ 1382 /* \ 1383 * Running in cluster mode - deregister active \ 1384 * connection information \ 1385 */ \ 1386 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1387 if ((tcp)->tcp_ip_src != 0) { \ 1388 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1389 AF_INET, \ 1390 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1391 (in_port_t)(tcp)->tcp_lport, \ 1392 (uint8_t *) \ 1393 (&((tcp)->tcp_ipha->ipha_dst)),\ 1394 (in_port_t)(tcp)->tcp_fport); \ 1395 } \ 1396 } else { \ 1397 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1398 &(tcp)->tcp_ip_src_v6)) { \ 1399 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1400 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1401 (in_port_t)(tcp)->tcp_lport, \ 1402 (uint8_t *) \ 1403 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1404 (in_port_t)(tcp)->tcp_fport); \ 1405 } \ 1406 } \ 1407 } \ 1408 } 1409 1410 /* 1411 * Cluster networking hook for traversing current connection list. 1412 * This routine is used to extract the current list of live connections 1413 * which must continue to to be dispatched to this node. 1414 */ 1415 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1416 1417 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1418 void *arg, tcp_stack_t *tcps); 1419 1420 /* 1421 * Figure out the value of window scale opton. Note that the rwnd is 1422 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1423 * We cannot find the scale value and then do a round up of tcp_rwnd 1424 * because the scale value may not be correct after that. 1425 * 1426 * Set the compiler flag to make this function inline. 1427 */ 1428 static void 1429 tcp_set_ws_value(tcp_t *tcp) 1430 { 1431 int i; 1432 uint32_t rwnd = tcp->tcp_rwnd; 1433 1434 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1435 i++, rwnd >>= 1) 1436 ; 1437 tcp->tcp_rcv_ws = i; 1438 } 1439 1440 /* 1441 * Remove a connection from the list of detached TIME_WAIT connections. 1442 * It returns B_FALSE if it can't remove the connection from the list 1443 * as the connection has already been removed from the list due to an 1444 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1445 */ 1446 static boolean_t 1447 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1448 { 1449 boolean_t locked = B_FALSE; 1450 1451 if (tcp_time_wait == NULL) { 1452 tcp_time_wait = *((tcp_squeue_priv_t **) 1453 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1454 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1455 locked = B_TRUE; 1456 } else { 1457 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1458 } 1459 1460 if (tcp->tcp_time_wait_expire == 0) { 1461 ASSERT(tcp->tcp_time_wait_next == NULL); 1462 ASSERT(tcp->tcp_time_wait_prev == NULL); 1463 if (locked) 1464 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1465 return (B_FALSE); 1466 } 1467 ASSERT(TCP_IS_DETACHED(tcp)); 1468 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1469 1470 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1471 ASSERT(tcp->tcp_time_wait_prev == NULL); 1472 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1473 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1474 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1475 NULL; 1476 } else { 1477 tcp_time_wait->tcp_time_wait_tail = NULL; 1478 } 1479 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1480 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1481 ASSERT(tcp->tcp_time_wait_next == NULL); 1482 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1483 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1484 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1485 } else { 1486 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1487 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1488 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1489 tcp->tcp_time_wait_next; 1490 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1491 tcp->tcp_time_wait_prev; 1492 } 1493 tcp->tcp_time_wait_next = NULL; 1494 tcp->tcp_time_wait_prev = NULL; 1495 tcp->tcp_time_wait_expire = 0; 1496 1497 if (locked) 1498 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1499 return (B_TRUE); 1500 } 1501 1502 /* 1503 * Add a connection to the list of detached TIME_WAIT connections 1504 * and set its time to expire. 1505 */ 1506 static void 1507 tcp_time_wait_append(tcp_t *tcp) 1508 { 1509 tcp_stack_t *tcps = tcp->tcp_tcps; 1510 tcp_squeue_priv_t *tcp_time_wait = 1511 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1512 SQPRIVATE_TCP)); 1513 1514 tcp_timers_stop(tcp); 1515 1516 /* Freed above */ 1517 ASSERT(tcp->tcp_timer_tid == 0); 1518 ASSERT(tcp->tcp_ack_tid == 0); 1519 1520 /* must have happened at the time of detaching the tcp */ 1521 ASSERT(tcp->tcp_ptpahn == NULL); 1522 ASSERT(tcp->tcp_flow_stopped == 0); 1523 ASSERT(tcp->tcp_time_wait_next == NULL); 1524 ASSERT(tcp->tcp_time_wait_prev == NULL); 1525 ASSERT(tcp->tcp_time_wait_expire == NULL); 1526 ASSERT(tcp->tcp_listener == NULL); 1527 1528 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1529 /* 1530 * The value computed below in tcp->tcp_time_wait_expire may 1531 * appear negative or wrap around. That is ok since our 1532 * interest is only in the difference between the current lbolt 1533 * value and tcp->tcp_time_wait_expire. But the value should not 1534 * be zero, since it means the tcp is not in the TIME_WAIT list. 1535 * The corresponding comparison in tcp_time_wait_collector() uses 1536 * modular arithmetic. 1537 */ 1538 tcp->tcp_time_wait_expire += 1539 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1540 if (tcp->tcp_time_wait_expire == 0) 1541 tcp->tcp_time_wait_expire = 1; 1542 1543 ASSERT(TCP_IS_DETACHED(tcp)); 1544 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1545 ASSERT(tcp->tcp_time_wait_next == NULL); 1546 ASSERT(tcp->tcp_time_wait_prev == NULL); 1547 TCP_DBGSTAT(tcps, tcp_time_wait); 1548 1549 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1550 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1551 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1552 tcp_time_wait->tcp_time_wait_head = tcp; 1553 } else { 1554 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1555 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1556 TCPS_TIME_WAIT); 1557 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1558 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1559 } 1560 tcp_time_wait->tcp_time_wait_tail = tcp; 1561 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1562 } 1563 1564 /* ARGSUSED */ 1565 void 1566 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1567 { 1568 conn_t *connp = (conn_t *)arg; 1569 tcp_t *tcp = connp->conn_tcp; 1570 tcp_stack_t *tcps = tcp->tcp_tcps; 1571 1572 ASSERT(tcp != NULL); 1573 if (tcp->tcp_state == TCPS_CLOSED) { 1574 return; 1575 } 1576 1577 ASSERT((tcp->tcp_family == AF_INET && 1578 tcp->tcp_ipversion == IPV4_VERSION) || 1579 (tcp->tcp_family == AF_INET6 && 1580 (tcp->tcp_ipversion == IPV4_VERSION || 1581 tcp->tcp_ipversion == IPV6_VERSION))); 1582 ASSERT(!tcp->tcp_listener); 1583 1584 TCP_STAT(tcps, tcp_time_wait_reap); 1585 ASSERT(TCP_IS_DETACHED(tcp)); 1586 1587 /* 1588 * Because they have no upstream client to rebind or tcp_close() 1589 * them later, we axe the connection here and now. 1590 */ 1591 tcp_close_detached(tcp); 1592 } 1593 1594 /* 1595 * Remove cached/latched IPsec references. 1596 */ 1597 void 1598 tcp_ipsec_cleanup(tcp_t *tcp) 1599 { 1600 conn_t *connp = tcp->tcp_connp; 1601 1602 if (connp->conn_flags & IPCL_TCPCONN) { 1603 if (connp->conn_latch != NULL) { 1604 IPLATCH_REFRELE(connp->conn_latch, 1605 connp->conn_netstack); 1606 connp->conn_latch = NULL; 1607 } 1608 if (connp->conn_policy != NULL) { 1609 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1610 connp->conn_policy = NULL; 1611 } 1612 } 1613 } 1614 1615 /* 1616 * Cleaup before placing on free list. 1617 * Disassociate from the netstack/tcp_stack_t since the freelist 1618 * is per squeue and not per netstack. 1619 */ 1620 void 1621 tcp_cleanup(tcp_t *tcp) 1622 { 1623 mblk_t *mp; 1624 char *tcp_iphc; 1625 int tcp_iphc_len; 1626 int tcp_hdr_grown; 1627 tcp_sack_info_t *tcp_sack_info; 1628 conn_t *connp = tcp->tcp_connp; 1629 tcp_stack_t *tcps = tcp->tcp_tcps; 1630 netstack_t *ns = tcps->tcps_netstack; 1631 1632 tcp_bind_hash_remove(tcp); 1633 1634 /* Cleanup that which needs the netstack first */ 1635 tcp_ipsec_cleanup(tcp); 1636 1637 tcp_free(tcp); 1638 1639 /* Release any SSL context */ 1640 if (tcp->tcp_kssl_ent != NULL) { 1641 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1642 tcp->tcp_kssl_ent = NULL; 1643 } 1644 1645 if (tcp->tcp_kssl_ctx != NULL) { 1646 kssl_release_ctx(tcp->tcp_kssl_ctx); 1647 tcp->tcp_kssl_ctx = NULL; 1648 } 1649 tcp->tcp_kssl_pending = B_FALSE; 1650 1651 conn_delete_ire(connp, NULL); 1652 1653 /* 1654 * Since we will bzero the entire structure, we need to 1655 * remove it and reinsert it in global hash list. We 1656 * know the walkers can't get to this conn because we 1657 * had set CONDEMNED flag earlier and checked reference 1658 * under conn_lock so walker won't pick it and when we 1659 * go the ipcl_globalhash_remove() below, no walker 1660 * can get to it. 1661 */ 1662 ipcl_globalhash_remove(connp); 1663 1664 /* 1665 * Now it is safe to decrement the reference counts. 1666 * This might be the last reference on the netstack and TCPS 1667 * in which case it will cause the tcp_g_q_close and 1668 * the freeing of the IP Instance. 1669 */ 1670 connp->conn_netstack = NULL; 1671 netstack_rele(ns); 1672 ASSERT(tcps != NULL); 1673 tcp->tcp_tcps = NULL; 1674 TCPS_REFRELE(tcps); 1675 1676 /* Save some state */ 1677 mp = tcp->tcp_timercache; 1678 1679 tcp_sack_info = tcp->tcp_sack_info; 1680 tcp_iphc = tcp->tcp_iphc; 1681 tcp_iphc_len = tcp->tcp_iphc_len; 1682 tcp_hdr_grown = tcp->tcp_hdr_grown; 1683 1684 if (connp->conn_cred != NULL) 1685 crfree(connp->conn_cred); 1686 if (connp->conn_peercred != NULL) 1687 crfree(connp->conn_peercred); 1688 bzero(connp, sizeof (conn_t)); 1689 bzero(tcp, sizeof (tcp_t)); 1690 1691 /* restore the state */ 1692 tcp->tcp_timercache = mp; 1693 1694 tcp->tcp_sack_info = tcp_sack_info; 1695 tcp->tcp_iphc = tcp_iphc; 1696 tcp->tcp_iphc_len = tcp_iphc_len; 1697 tcp->tcp_hdr_grown = tcp_hdr_grown; 1698 1699 1700 tcp->tcp_connp = connp; 1701 1702 connp->conn_tcp = tcp; 1703 connp->conn_flags = IPCL_TCPCONN; 1704 connp->conn_state_flags = CONN_INCIPIENT; 1705 connp->conn_ulp = IPPROTO_TCP; 1706 connp->conn_ref = 1; 1707 } 1708 1709 /* 1710 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1711 * is done forwards from the head. 1712 * This walks all stack instances since 1713 * tcp_time_wait remains global across all stacks. 1714 */ 1715 /* ARGSUSED */ 1716 void 1717 tcp_time_wait_collector(void *arg) 1718 { 1719 tcp_t *tcp; 1720 clock_t now; 1721 mblk_t *mp; 1722 conn_t *connp; 1723 kmutex_t *lock; 1724 boolean_t removed; 1725 1726 squeue_t *sqp = (squeue_t *)arg; 1727 tcp_squeue_priv_t *tcp_time_wait = 1728 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1729 1730 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1731 tcp_time_wait->tcp_time_wait_tid = 0; 1732 1733 if (tcp_time_wait->tcp_free_list != NULL && 1734 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1735 TCP_G_STAT(tcp_freelist_cleanup); 1736 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1737 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1738 tcp->tcp_time_wait_next = NULL; 1739 tcp_time_wait->tcp_free_list_cnt--; 1740 ASSERT(tcp->tcp_tcps == NULL); 1741 CONN_DEC_REF(tcp->tcp_connp); 1742 } 1743 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1744 } 1745 1746 /* 1747 * In order to reap time waits reliably, we should use a 1748 * source of time that is not adjustable by the user -- hence 1749 * the call to ddi_get_lbolt(). 1750 */ 1751 now = ddi_get_lbolt(); 1752 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1753 /* 1754 * Compare times using modular arithmetic, since 1755 * lbolt can wrapover. 1756 */ 1757 if ((now - tcp->tcp_time_wait_expire) < 0) { 1758 break; 1759 } 1760 1761 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1762 ASSERT(removed); 1763 1764 connp = tcp->tcp_connp; 1765 ASSERT(connp->conn_fanout != NULL); 1766 lock = &connp->conn_fanout->connf_lock; 1767 /* 1768 * This is essentially a TW reclaim fast path optimization for 1769 * performance where the timewait collector checks under the 1770 * fanout lock (so that no one else can get access to the 1771 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1772 * the classifier hash list. If ref count is indeed 2, we can 1773 * just remove the conn under the fanout lock and avoid 1774 * cleaning up the conn under the squeue, provided that 1775 * clustering callbacks are not enabled. If clustering is 1776 * enabled, we need to make the clustering callback before 1777 * setting the CONDEMNED flag and after dropping all locks and 1778 * so we forego this optimization and fall back to the slow 1779 * path. Also please see the comments in tcp_closei_local 1780 * regarding the refcnt logic. 1781 * 1782 * Since we are holding the tcp_time_wait_lock, its better 1783 * not to block on the fanout_lock because other connections 1784 * can't add themselves to time_wait list. So we do a 1785 * tryenter instead of mutex_enter. 1786 */ 1787 if (mutex_tryenter(lock)) { 1788 mutex_enter(&connp->conn_lock); 1789 if ((connp->conn_ref == 2) && 1790 (cl_inet_disconnect == NULL)) { 1791 ipcl_hash_remove_locked(connp, 1792 connp->conn_fanout); 1793 /* 1794 * Set the CONDEMNED flag now itself so that 1795 * the refcnt cannot increase due to any 1796 * walker. But we have still not cleaned up 1797 * conn_ire_cache. This is still ok since 1798 * we are going to clean it up in tcp_cleanup 1799 * immediately and any interface unplumb 1800 * thread will wait till the ire is blown away 1801 */ 1802 connp->conn_state_flags |= CONN_CONDEMNED; 1803 mutex_exit(lock); 1804 mutex_exit(&connp->conn_lock); 1805 if (tcp_time_wait->tcp_free_list_cnt < 1806 tcp_free_list_max_cnt) { 1807 /* Add to head of tcp_free_list */ 1808 mutex_exit( 1809 &tcp_time_wait->tcp_time_wait_lock); 1810 tcp_cleanup(tcp); 1811 ASSERT(connp->conn_latch == NULL); 1812 ASSERT(connp->conn_policy == NULL); 1813 ASSERT(tcp->tcp_tcps == NULL); 1814 ASSERT(connp->conn_netstack == NULL); 1815 1816 mutex_enter( 1817 &tcp_time_wait->tcp_time_wait_lock); 1818 tcp->tcp_time_wait_next = 1819 tcp_time_wait->tcp_free_list; 1820 tcp_time_wait->tcp_free_list = tcp; 1821 tcp_time_wait->tcp_free_list_cnt++; 1822 continue; 1823 } else { 1824 /* Do not add to tcp_free_list */ 1825 mutex_exit( 1826 &tcp_time_wait->tcp_time_wait_lock); 1827 tcp_bind_hash_remove(tcp); 1828 conn_delete_ire(tcp->tcp_connp, NULL); 1829 tcp_ipsec_cleanup(tcp); 1830 CONN_DEC_REF(tcp->tcp_connp); 1831 } 1832 } else { 1833 CONN_INC_REF_LOCKED(connp); 1834 mutex_exit(lock); 1835 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1836 mutex_exit(&connp->conn_lock); 1837 /* 1838 * We can reuse the closemp here since conn has 1839 * detached (otherwise we wouldn't even be in 1840 * time_wait list). tcp_closemp_used can safely 1841 * be changed without taking a lock as no other 1842 * thread can concurrently access it at this 1843 * point in the connection lifecycle. We 1844 * increment tcp_closemp_used to record any 1845 * attempt to reuse tcp_closemp while it is 1846 * still in use. 1847 */ 1848 1849 if (tcp->tcp_closemp.b_prev == NULL) 1850 tcp->tcp_closemp_used = 1; 1851 else 1852 tcp->tcp_closemp_used++; 1853 1854 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1855 mp = &tcp->tcp_closemp; 1856 squeue_fill(connp->conn_sqp, mp, 1857 tcp_timewait_output, connp, 1858 SQTAG_TCP_TIMEWAIT); 1859 } 1860 } else { 1861 mutex_enter(&connp->conn_lock); 1862 CONN_INC_REF_LOCKED(connp); 1863 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1864 mutex_exit(&connp->conn_lock); 1865 /* 1866 * We can reuse the closemp here since conn has 1867 * detached (otherwise we wouldn't even be in 1868 * time_wait list). tcp_closemp_used can safely 1869 * be changed without taking a lock as no other 1870 * thread can concurrently access it at this 1871 * point in the connection lifecycle. We 1872 * increment tcp_closemp_used to record any 1873 * attempt to reuse tcp_closemp while it is 1874 * still in use. 1875 */ 1876 1877 if (tcp->tcp_closemp.b_prev == NULL) 1878 tcp->tcp_closemp_used = 1; 1879 else 1880 tcp->tcp_closemp_used++; 1881 1882 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1883 mp = &tcp->tcp_closemp; 1884 squeue_fill(connp->conn_sqp, mp, 1885 tcp_timewait_output, connp, 0); 1886 } 1887 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1888 } 1889 1890 if (tcp_time_wait->tcp_free_list != NULL) 1891 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1892 1893 tcp_time_wait->tcp_time_wait_tid = 1894 timeout(tcp_time_wait_collector, sqp, TCP_TIME_WAIT_DELAY); 1895 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1896 } 1897 /* 1898 * Reply to a clients T_CONN_RES TPI message. This function 1899 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1900 * on the acceptor STREAM and processed in tcp_wput_accept(). 1901 * Read the block comment on top of tcp_conn_request(). 1902 */ 1903 static void 1904 tcp_accept(tcp_t *listener, mblk_t *mp) 1905 { 1906 tcp_t *acceptor; 1907 tcp_t *eager; 1908 tcp_t *tcp; 1909 struct T_conn_res *tcr; 1910 t_uscalar_t acceptor_id; 1911 t_scalar_t seqnum; 1912 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1913 mblk_t *ok_mp; 1914 mblk_t *mp1; 1915 tcp_stack_t *tcps = listener->tcp_tcps; 1916 1917 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1918 tcp_err_ack(listener, mp, TPROTO, 0); 1919 return; 1920 } 1921 tcr = (struct T_conn_res *)mp->b_rptr; 1922 1923 /* 1924 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1925 * read side queue of the streams device underneath us i.e. the 1926 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1927 * look it up in the queue_hash. Under LP64 it sends down the 1928 * minor_t of the accepting endpoint. 1929 * 1930 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1931 * fanout hash lock is held. 1932 * This prevents any thread from entering the acceptor queue from 1933 * below (since it has not been hard bound yet i.e. any inbound 1934 * packets will arrive on the listener or default tcp queue and 1935 * go through tcp_lookup). 1936 * The CONN_INC_REF will prevent the acceptor from closing. 1937 * 1938 * XXX It is still possible for a tli application to send down data 1939 * on the accepting stream while another thread calls t_accept. 1940 * This should not be a problem for well-behaved applications since 1941 * the T_OK_ACK is sent after the queue swapping is completed. 1942 * 1943 * If the accepting fd is the same as the listening fd, avoid 1944 * queue hash lookup since that will return an eager listener in a 1945 * already established state. 1946 */ 1947 acceptor_id = tcr->ACCEPTOR_id; 1948 mutex_enter(&listener->tcp_eager_lock); 1949 if (listener->tcp_acceptor_id == acceptor_id) { 1950 eager = listener->tcp_eager_next_q; 1951 /* only count how many T_CONN_INDs so don't count q0 */ 1952 if ((listener->tcp_conn_req_cnt_q != 1) || 1953 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1954 mutex_exit(&listener->tcp_eager_lock); 1955 tcp_err_ack(listener, mp, TBADF, 0); 1956 return; 1957 } 1958 if (listener->tcp_conn_req_cnt_q0 != 0) { 1959 /* Throw away all the eagers on q0. */ 1960 tcp_eager_cleanup(listener, 1); 1961 } 1962 if (listener->tcp_syn_defense) { 1963 listener->tcp_syn_defense = B_FALSE; 1964 if (listener->tcp_ip_addr_cache != NULL) { 1965 kmem_free(listener->tcp_ip_addr_cache, 1966 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1967 listener->tcp_ip_addr_cache = NULL; 1968 } 1969 } 1970 /* 1971 * Transfer tcp_conn_req_max to the eager so that when 1972 * a disconnect occurs we can revert the endpoint to the 1973 * listen state. 1974 */ 1975 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1976 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1977 /* 1978 * Get a reference on the acceptor just like the 1979 * tcp_acceptor_hash_lookup below. 1980 */ 1981 acceptor = listener; 1982 CONN_INC_REF(acceptor->tcp_connp); 1983 } else { 1984 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1985 if (acceptor == NULL) { 1986 if (listener->tcp_debug) { 1987 (void) strlog(TCP_MOD_ID, 0, 1, 1988 SL_ERROR|SL_TRACE, 1989 "tcp_accept: did not find acceptor 0x%x\n", 1990 acceptor_id); 1991 } 1992 mutex_exit(&listener->tcp_eager_lock); 1993 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 1994 return; 1995 } 1996 /* 1997 * Verify acceptor state. The acceptable states for an acceptor 1998 * include TCPS_IDLE and TCPS_BOUND. 1999 */ 2000 switch (acceptor->tcp_state) { 2001 case TCPS_IDLE: 2002 /* FALLTHRU */ 2003 case TCPS_BOUND: 2004 break; 2005 default: 2006 CONN_DEC_REF(acceptor->tcp_connp); 2007 mutex_exit(&listener->tcp_eager_lock); 2008 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2009 return; 2010 } 2011 } 2012 2013 /* The listener must be in TCPS_LISTEN */ 2014 if (listener->tcp_state != TCPS_LISTEN) { 2015 CONN_DEC_REF(acceptor->tcp_connp); 2016 mutex_exit(&listener->tcp_eager_lock); 2017 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2018 return; 2019 } 2020 2021 /* 2022 * Rendezvous with an eager connection request packet hanging off 2023 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2024 * tcp structure when the connection packet arrived in 2025 * tcp_conn_request(). 2026 */ 2027 seqnum = tcr->SEQ_number; 2028 eager = listener; 2029 do { 2030 eager = eager->tcp_eager_next_q; 2031 if (eager == NULL) { 2032 CONN_DEC_REF(acceptor->tcp_connp); 2033 mutex_exit(&listener->tcp_eager_lock); 2034 tcp_err_ack(listener, mp, TBADSEQ, 0); 2035 return; 2036 } 2037 } while (eager->tcp_conn_req_seqnum != seqnum); 2038 mutex_exit(&listener->tcp_eager_lock); 2039 2040 /* 2041 * At this point, both acceptor and listener have 2 ref 2042 * that they begin with. Acceptor has one additional ref 2043 * we placed in lookup while listener has 3 additional 2044 * ref for being behind the squeue (tcp_accept() is 2045 * done on listener's squeue); being in classifier hash; 2046 * and eager's ref on listener. 2047 */ 2048 ASSERT(listener->tcp_connp->conn_ref >= 5); 2049 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2050 2051 /* 2052 * The eager at this point is set in its own squeue and 2053 * could easily have been killed (tcp_accept_finish will 2054 * deal with that) because of a TH_RST so we can only 2055 * ASSERT for a single ref. 2056 */ 2057 ASSERT(eager->tcp_connp->conn_ref >= 1); 2058 2059 /* Pre allocate the stroptions mblk also */ 2060 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2061 if (opt_mp == NULL) { 2062 CONN_DEC_REF(acceptor->tcp_connp); 2063 CONN_DEC_REF(eager->tcp_connp); 2064 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2065 return; 2066 } 2067 DB_TYPE(opt_mp) = M_SETOPTS; 2068 opt_mp->b_wptr += sizeof (struct stroptions); 2069 2070 /* 2071 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2072 * from listener to acceptor. The message is chained on opt_mp 2073 * which will be sent onto eager's squeue. 2074 */ 2075 if (listener->tcp_bound_if != 0) { 2076 /* allocate optmgmt req */ 2077 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2078 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2079 sizeof (int)); 2080 if (mp1 != NULL) 2081 linkb(opt_mp, mp1); 2082 } 2083 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2084 uint_t on = 1; 2085 2086 /* allocate optmgmt req */ 2087 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2088 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2089 if (mp1 != NULL) 2090 linkb(opt_mp, mp1); 2091 } 2092 2093 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2094 if ((mp1 = copymsg(mp)) == NULL) { 2095 CONN_DEC_REF(acceptor->tcp_connp); 2096 CONN_DEC_REF(eager->tcp_connp); 2097 freemsg(opt_mp); 2098 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2099 return; 2100 } 2101 2102 tcr = (struct T_conn_res *)mp1->b_rptr; 2103 2104 /* 2105 * This is an expanded version of mi_tpi_ok_ack_alloc() 2106 * which allocates a larger mblk and appends the new 2107 * local address to the ok_ack. The address is copied by 2108 * soaccept() for getsockname(). 2109 */ 2110 { 2111 int extra; 2112 2113 extra = (eager->tcp_family == AF_INET) ? 2114 sizeof (sin_t) : sizeof (sin6_t); 2115 2116 /* 2117 * Try to re-use mp, if possible. Otherwise, allocate 2118 * an mblk and return it as ok_mp. In any case, mp 2119 * is no longer usable upon return. 2120 */ 2121 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2122 CONN_DEC_REF(acceptor->tcp_connp); 2123 CONN_DEC_REF(eager->tcp_connp); 2124 freemsg(opt_mp); 2125 /* Original mp has been freed by now, so use mp1 */ 2126 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2127 return; 2128 } 2129 2130 mp = NULL; /* We should never use mp after this point */ 2131 2132 switch (extra) { 2133 case sizeof (sin_t): { 2134 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2135 2136 ok_mp->b_wptr += extra; 2137 sin->sin_family = AF_INET; 2138 sin->sin_port = eager->tcp_lport; 2139 sin->sin_addr.s_addr = 2140 eager->tcp_ipha->ipha_src; 2141 break; 2142 } 2143 case sizeof (sin6_t): { 2144 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2145 2146 ok_mp->b_wptr += extra; 2147 sin6->sin6_family = AF_INET6; 2148 sin6->sin6_port = eager->tcp_lport; 2149 if (eager->tcp_ipversion == IPV4_VERSION) { 2150 sin6->sin6_flowinfo = 0; 2151 IN6_IPADDR_TO_V4MAPPED( 2152 eager->tcp_ipha->ipha_src, 2153 &sin6->sin6_addr); 2154 } else { 2155 ASSERT(eager->tcp_ip6h != NULL); 2156 sin6->sin6_flowinfo = 2157 eager->tcp_ip6h->ip6_vcf & 2158 ~IPV6_VERS_AND_FLOW_MASK; 2159 sin6->sin6_addr = 2160 eager->tcp_ip6h->ip6_src; 2161 } 2162 break; 2163 } 2164 default: 2165 break; 2166 } 2167 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2168 } 2169 2170 /* 2171 * If there are no options we know that the T_CONN_RES will 2172 * succeed. However, we can't send the T_OK_ACK upstream until 2173 * the tcp_accept_swap is done since it would be dangerous to 2174 * let the application start using the new fd prior to the swap. 2175 */ 2176 tcp_accept_swap(listener, acceptor, eager); 2177 2178 /* 2179 * tcp_accept_swap unlinks eager from listener but does not drop 2180 * the eager's reference on the listener. 2181 */ 2182 ASSERT(eager->tcp_listener == NULL); 2183 ASSERT(listener->tcp_connp->conn_ref >= 5); 2184 2185 /* 2186 * The eager is now associated with its own queue. Insert in 2187 * the hash so that the connection can be reused for a future 2188 * T_CONN_RES. 2189 */ 2190 tcp_acceptor_hash_insert(acceptor_id, eager); 2191 2192 /* 2193 * We now do the processing of options with T_CONN_RES. 2194 * We delay till now since we wanted to have queue to pass to 2195 * option processing routines that points back to the right 2196 * instance structure which does not happen until after 2197 * tcp_accept_swap(). 2198 * 2199 * Note: 2200 * The sanity of the logic here assumes that whatever options 2201 * are appropriate to inherit from listner=>eager are done 2202 * before this point, and whatever were to be overridden (or not) 2203 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2204 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2205 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2206 * This may not be true at this point in time but can be fixed 2207 * independently. This option processing code starts with 2208 * the instantiated acceptor instance and the final queue at 2209 * this point. 2210 */ 2211 2212 if (tcr->OPT_length != 0) { 2213 /* Options to process */ 2214 int t_error = 0; 2215 int sys_error = 0; 2216 int do_disconnect = 0; 2217 2218 if (tcp_conprim_opt_process(eager, mp1, 2219 &do_disconnect, &t_error, &sys_error) < 0) { 2220 eager->tcp_accept_error = 1; 2221 if (do_disconnect) { 2222 /* 2223 * An option failed which does not allow 2224 * connection to be accepted. 2225 * 2226 * We allow T_CONN_RES to succeed and 2227 * put a T_DISCON_IND on the eager queue. 2228 */ 2229 ASSERT(t_error == 0 && sys_error == 0); 2230 eager->tcp_send_discon_ind = 1; 2231 } else { 2232 ASSERT(t_error != 0); 2233 freemsg(ok_mp); 2234 /* 2235 * Original mp was either freed or set 2236 * to ok_mp above, so use mp1 instead. 2237 */ 2238 tcp_err_ack(listener, mp1, t_error, sys_error); 2239 goto finish; 2240 } 2241 } 2242 /* 2243 * Most likely success in setting options (except if 2244 * eager->tcp_send_discon_ind set). 2245 * mp1 option buffer represented by OPT_length/offset 2246 * potentially modified and contains results of setting 2247 * options at this point 2248 */ 2249 } 2250 2251 /* We no longer need mp1, since all options processing has passed */ 2252 freemsg(mp1); 2253 2254 putnext(listener->tcp_rq, ok_mp); 2255 2256 mutex_enter(&listener->tcp_eager_lock); 2257 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2258 tcp_t *tail; 2259 mblk_t *conn_ind; 2260 2261 /* 2262 * This path should not be executed if listener and 2263 * acceptor streams are the same. 2264 */ 2265 ASSERT(listener != acceptor); 2266 2267 tcp = listener->tcp_eager_prev_q0; 2268 /* 2269 * listener->tcp_eager_prev_q0 points to the TAIL of the 2270 * deferred T_conn_ind queue. We need to get to the head of 2271 * the queue in order to send up T_conn_ind the same order as 2272 * how the 3WHS is completed. 2273 */ 2274 while (tcp != listener) { 2275 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2276 break; 2277 else 2278 tcp = tcp->tcp_eager_prev_q0; 2279 } 2280 ASSERT(tcp != listener); 2281 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2282 ASSERT(conn_ind != NULL); 2283 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2284 2285 /* Move from q0 to q */ 2286 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2287 listener->tcp_conn_req_cnt_q0--; 2288 listener->tcp_conn_req_cnt_q++; 2289 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2290 tcp->tcp_eager_prev_q0; 2291 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2292 tcp->tcp_eager_next_q0; 2293 tcp->tcp_eager_prev_q0 = NULL; 2294 tcp->tcp_eager_next_q0 = NULL; 2295 tcp->tcp_conn_def_q0 = B_FALSE; 2296 2297 /* Make sure the tcp isn't in the list of droppables */ 2298 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2299 tcp->tcp_eager_prev_drop_q0 == NULL); 2300 2301 /* 2302 * Insert at end of the queue because sockfs sends 2303 * down T_CONN_RES in chronological order. Leaving 2304 * the older conn indications at front of the queue 2305 * helps reducing search time. 2306 */ 2307 tail = listener->tcp_eager_last_q; 2308 if (tail != NULL) 2309 tail->tcp_eager_next_q = tcp; 2310 else 2311 listener->tcp_eager_next_q = tcp; 2312 listener->tcp_eager_last_q = tcp; 2313 tcp->tcp_eager_next_q = NULL; 2314 mutex_exit(&listener->tcp_eager_lock); 2315 putnext(tcp->tcp_rq, conn_ind); 2316 } else { 2317 mutex_exit(&listener->tcp_eager_lock); 2318 } 2319 2320 /* 2321 * Done with the acceptor - free it 2322 * 2323 * Note: from this point on, no access to listener should be made 2324 * as listener can be equal to acceptor. 2325 */ 2326 finish: 2327 ASSERT(acceptor->tcp_detached); 2328 ASSERT(tcps->tcps_g_q != NULL); 2329 acceptor->tcp_rq = tcps->tcps_g_q; 2330 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2331 (void) tcp_clean_death(acceptor, 0, 2); 2332 CONN_DEC_REF(acceptor->tcp_connp); 2333 2334 /* 2335 * In case we already received a FIN we have to make tcp_rput send 2336 * the ordrel_ind. This will also send up a window update if the window 2337 * has opened up. 2338 * 2339 * In the normal case of a successful connection acceptance 2340 * we give the O_T_BIND_REQ to the read side put procedure as an 2341 * indication that this was just accepted. This tells tcp_rput to 2342 * pass up any data queued in tcp_rcv_list. 2343 * 2344 * In the fringe case where options sent with T_CONN_RES failed and 2345 * we required, we would be indicating a T_DISCON_IND to blow 2346 * away this connection. 2347 */ 2348 2349 /* 2350 * XXX: we currently have a problem if XTI application closes the 2351 * acceptor stream in between. This problem exists in on10-gate also 2352 * and is well know but nothing can be done short of major rewrite 2353 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2354 * eager same squeue as listener (we can distinguish non socket 2355 * listeners at the time of handling a SYN in tcp_conn_request) 2356 * and do most of the work that tcp_accept_finish does here itself 2357 * and then get behind the acceptor squeue to access the acceptor 2358 * queue. 2359 */ 2360 /* 2361 * We already have a ref on tcp so no need to do one before squeue_fill 2362 */ 2363 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2364 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2365 } 2366 2367 /* 2368 * Swap information between the eager and acceptor for a TLI/XTI client. 2369 * The sockfs accept is done on the acceptor stream and control goes 2370 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2371 * called. In either case, both the eager and listener are in their own 2372 * perimeter (squeue) and the code has to deal with potential race. 2373 * 2374 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2375 */ 2376 static void 2377 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2378 { 2379 conn_t *econnp, *aconnp; 2380 2381 ASSERT(eager->tcp_rq == listener->tcp_rq); 2382 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2383 ASSERT(!eager->tcp_hard_bound); 2384 ASSERT(!TCP_IS_SOCKET(acceptor)); 2385 ASSERT(!TCP_IS_SOCKET(eager)); 2386 ASSERT(!TCP_IS_SOCKET(listener)); 2387 2388 acceptor->tcp_detached = B_TRUE; 2389 /* 2390 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2391 * the acceptor id. 2392 */ 2393 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2394 2395 /* remove eager from listen list... */ 2396 mutex_enter(&listener->tcp_eager_lock); 2397 tcp_eager_unlink(eager); 2398 ASSERT(eager->tcp_eager_next_q == NULL && 2399 eager->tcp_eager_last_q == NULL); 2400 ASSERT(eager->tcp_eager_next_q0 == NULL && 2401 eager->tcp_eager_prev_q0 == NULL); 2402 mutex_exit(&listener->tcp_eager_lock); 2403 eager->tcp_rq = acceptor->tcp_rq; 2404 eager->tcp_wq = acceptor->tcp_wq; 2405 2406 econnp = eager->tcp_connp; 2407 aconnp = acceptor->tcp_connp; 2408 2409 eager->tcp_rq->q_ptr = econnp; 2410 eager->tcp_wq->q_ptr = econnp; 2411 2412 /* 2413 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2414 * which might be a different squeue from our peer TCP instance. 2415 * For TCP Fusion, the peer expects that whenever tcp_detached is 2416 * clear, our TCP queues point to the acceptor's queues. Thus, use 2417 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2418 * above reach global visibility prior to the clearing of tcp_detached. 2419 */ 2420 membar_producer(); 2421 eager->tcp_detached = B_FALSE; 2422 2423 ASSERT(eager->tcp_ack_tid == 0); 2424 2425 econnp->conn_dev = aconnp->conn_dev; 2426 if (eager->tcp_cred != NULL) 2427 crfree(eager->tcp_cred); 2428 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2429 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2430 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2431 2432 aconnp->conn_cred = NULL; 2433 2434 econnp->conn_zoneid = aconnp->conn_zoneid; 2435 econnp->conn_allzones = aconnp->conn_allzones; 2436 2437 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2438 aconnp->conn_mac_exempt = B_FALSE; 2439 2440 ASSERT(aconnp->conn_peercred == NULL); 2441 2442 /* Do the IPC initialization */ 2443 CONN_INC_REF(econnp); 2444 2445 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2446 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2447 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2448 econnp->conn_ulp = aconnp->conn_ulp; 2449 2450 /* Done with old IPC. Drop its ref on its connp */ 2451 CONN_DEC_REF(aconnp); 2452 } 2453 2454 2455 /* 2456 * Adapt to the information, such as rtt and rtt_sd, provided from the 2457 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2458 * 2459 * Checks for multicast and broadcast destination address. 2460 * Returns zero on failure; non-zero if ok. 2461 * 2462 * Note that the MSS calculation here is based on the info given in 2463 * the IRE. We do not do any calculation based on TCP options. They 2464 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2465 * knows which options to use. 2466 * 2467 * Note on how TCP gets its parameters for a connection. 2468 * 2469 * When a tcp_t structure is allocated, it gets all the default parameters. 2470 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2471 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2472 * default. But if there is an associated tcp_host_param, it will override 2473 * the metrics. 2474 * 2475 * An incoming SYN with a multicast or broadcast destination address, is dropped 2476 * in 1 of 2 places. 2477 * 2478 * 1. If the packet was received over the wire it is dropped in 2479 * ip_rput_process_broadcast() 2480 * 2481 * 2. If the packet was received through internal IP loopback, i.e. the packet 2482 * was generated and received on the same machine, it is dropped in 2483 * ip_wput_local() 2484 * 2485 * An incoming SYN with a multicast or broadcast source address is always 2486 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2487 * reject an attempt to connect to a broadcast or multicast (destination) 2488 * address. 2489 */ 2490 static int 2491 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2492 { 2493 tcp_hsp_t *hsp; 2494 ire_t *ire; 2495 ire_t *sire = NULL; 2496 iulp_t *ire_uinfo = NULL; 2497 uint32_t mss_max; 2498 uint32_t mss; 2499 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2500 conn_t *connp = tcp->tcp_connp; 2501 boolean_t ire_cacheable = B_FALSE; 2502 zoneid_t zoneid = connp->conn_zoneid; 2503 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2504 MATCH_IRE_SECATTR; 2505 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2506 ill_t *ill = NULL; 2507 boolean_t incoming = (ire_mp == NULL); 2508 tcp_stack_t *tcps = tcp->tcp_tcps; 2509 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2510 2511 ASSERT(connp->conn_ire_cache == NULL); 2512 2513 if (tcp->tcp_ipversion == IPV4_VERSION) { 2514 2515 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2516 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2517 return (0); 2518 } 2519 /* 2520 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2521 * for the destination with the nexthop as gateway. 2522 * ire_ctable_lookup() is used because this particular 2523 * ire, if it exists, will be marked private. 2524 * If that is not available, use the interface ire 2525 * for the nexthop. 2526 * 2527 * TSol: tcp_update_label will detect label mismatches based 2528 * only on the destination's label, but that would not 2529 * detect label mismatches based on the security attributes 2530 * of routes or next hop gateway. Hence we need to pass the 2531 * label to ire_ftable_lookup below in order to locate the 2532 * right prefix (and/or) ire cache. Similarly we also need 2533 * pass the label to the ire_cache_lookup below to locate 2534 * the right ire that also matches on the label. 2535 */ 2536 if (tcp->tcp_connp->conn_nexthop_set) { 2537 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2538 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2539 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2540 ipst); 2541 if (ire == NULL) { 2542 ire = ire_ftable_lookup( 2543 tcp->tcp_connp->conn_nexthop_v4, 2544 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2545 tsl, match_flags, ipst); 2546 if (ire == NULL) 2547 return (0); 2548 } else { 2549 ire_uinfo = &ire->ire_uinfo; 2550 } 2551 } else { 2552 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2553 zoneid, tsl, ipst); 2554 if (ire != NULL) { 2555 ire_cacheable = B_TRUE; 2556 ire_uinfo = (ire_mp != NULL) ? 2557 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2558 &ire->ire_uinfo; 2559 2560 } else { 2561 if (ire_mp == NULL) { 2562 ire = ire_ftable_lookup( 2563 tcp->tcp_connp->conn_rem, 2564 0, 0, 0, NULL, &sire, zoneid, 0, 2565 tsl, (MATCH_IRE_RECURSIVE | 2566 MATCH_IRE_DEFAULT), ipst); 2567 if (ire == NULL) 2568 return (0); 2569 ire_uinfo = (sire != NULL) ? 2570 &sire->ire_uinfo : 2571 &ire->ire_uinfo; 2572 } else { 2573 ire = (ire_t *)ire_mp->b_rptr; 2574 ire_uinfo = 2575 &((ire_t *) 2576 ire_mp->b_rptr)->ire_uinfo; 2577 } 2578 } 2579 } 2580 ASSERT(ire != NULL); 2581 2582 if ((ire->ire_src_addr == INADDR_ANY) || 2583 (ire->ire_type & IRE_BROADCAST)) { 2584 /* 2585 * ire->ire_mp is non null when ire_mp passed in is used 2586 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2587 */ 2588 if (ire->ire_mp == NULL) 2589 ire_refrele(ire); 2590 if (sire != NULL) 2591 ire_refrele(sire); 2592 return (0); 2593 } 2594 2595 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2596 ipaddr_t src_addr; 2597 2598 /* 2599 * ip_bind_connected() has stored the correct source 2600 * address in conn_src. 2601 */ 2602 src_addr = tcp->tcp_connp->conn_src; 2603 tcp->tcp_ipha->ipha_src = src_addr; 2604 /* 2605 * Copy of the src addr. in tcp_t is needed 2606 * for the lookup funcs. 2607 */ 2608 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2609 } 2610 /* 2611 * Set the fragment bit so that IP will tell us if the MTU 2612 * should change. IP tells us the latest setting of 2613 * ip_path_mtu_discovery through ire_frag_flag. 2614 */ 2615 if (ipst->ips_ip_path_mtu_discovery) { 2616 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2617 htons(IPH_DF); 2618 } 2619 /* 2620 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2621 * for IP_NEXTHOP. No cache ire has been found for the 2622 * destination and we are working with the nexthop's 2623 * interface ire. Since we need to forward all packets 2624 * to the nexthop first, we "blindly" set tcp_localnet 2625 * to false, eventhough the destination may also be 2626 * onlink. 2627 */ 2628 if (ire_uinfo == NULL) 2629 tcp->tcp_localnet = 0; 2630 else 2631 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2632 } else { 2633 /* 2634 * For incoming connection ire_mp = NULL 2635 * For outgoing connection ire_mp != NULL 2636 * Technically we should check conn_incoming_ill 2637 * when ire_mp is NULL and conn_outgoing_ill when 2638 * ire_mp is non-NULL. But this is performance 2639 * critical path and for IPV*_BOUND_IF, outgoing 2640 * and incoming ill are always set to the same value. 2641 */ 2642 ill_t *dst_ill = NULL; 2643 ipif_t *dst_ipif = NULL; 2644 2645 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2646 2647 if (connp->conn_outgoing_ill != NULL) { 2648 /* Outgoing or incoming path */ 2649 int err; 2650 2651 dst_ill = conn_get_held_ill(connp, 2652 &connp->conn_outgoing_ill, &err); 2653 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2654 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2655 return (0); 2656 } 2657 match_flags |= MATCH_IRE_ILL; 2658 dst_ipif = dst_ill->ill_ipif; 2659 } 2660 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2661 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2662 2663 if (ire != NULL) { 2664 ire_cacheable = B_TRUE; 2665 ire_uinfo = (ire_mp != NULL) ? 2666 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2667 &ire->ire_uinfo; 2668 } else { 2669 if (ire_mp == NULL) { 2670 ire = ire_ftable_lookup_v6( 2671 &tcp->tcp_connp->conn_remv6, 2672 0, 0, 0, dst_ipif, &sire, zoneid, 2673 0, tsl, match_flags, ipst); 2674 if (ire == NULL) { 2675 if (dst_ill != NULL) 2676 ill_refrele(dst_ill); 2677 return (0); 2678 } 2679 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2680 &ire->ire_uinfo; 2681 } else { 2682 ire = (ire_t *)ire_mp->b_rptr; 2683 ire_uinfo = 2684 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2685 } 2686 } 2687 if (dst_ill != NULL) 2688 ill_refrele(dst_ill); 2689 2690 ASSERT(ire != NULL); 2691 ASSERT(ire_uinfo != NULL); 2692 2693 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2694 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2695 /* 2696 * ire->ire_mp is non null when ire_mp passed in is used 2697 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2698 */ 2699 if (ire->ire_mp == NULL) 2700 ire_refrele(ire); 2701 if (sire != NULL) 2702 ire_refrele(sire); 2703 return (0); 2704 } 2705 2706 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2707 in6_addr_t src_addr; 2708 2709 /* 2710 * ip_bind_connected_v6() has stored the correct source 2711 * address per IPv6 addr. selection policy in 2712 * conn_src_v6. 2713 */ 2714 src_addr = tcp->tcp_connp->conn_srcv6; 2715 2716 tcp->tcp_ip6h->ip6_src = src_addr; 2717 /* 2718 * Copy of the src addr. in tcp_t is needed 2719 * for the lookup funcs. 2720 */ 2721 tcp->tcp_ip_src_v6 = src_addr; 2722 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2723 &connp->conn_srcv6)); 2724 } 2725 tcp->tcp_localnet = 2726 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2727 } 2728 2729 /* 2730 * This allows applications to fail quickly when connections are made 2731 * to dead hosts. Hosts can be labeled dead by adding a reject route 2732 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2733 */ 2734 if ((ire->ire_flags & RTF_REJECT) && 2735 (ire->ire_flags & RTF_PRIVATE)) 2736 goto error; 2737 2738 /* 2739 * Make use of the cached rtt and rtt_sd values to calculate the 2740 * initial RTO. Note that they are already initialized in 2741 * tcp_init_values(). 2742 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2743 * IP_NEXTHOP, but instead are using the interface ire for the 2744 * nexthop, then we do not use the ire_uinfo from that ire to 2745 * do any initializations. 2746 */ 2747 if (ire_uinfo != NULL) { 2748 if (ire_uinfo->iulp_rtt != 0) { 2749 clock_t rto; 2750 2751 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2752 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2753 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2754 tcps->tcps_rexmit_interval_extra + 2755 (tcp->tcp_rtt_sa >> 5); 2756 2757 if (rto > tcps->tcps_rexmit_interval_max) { 2758 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2759 } else if (rto < tcps->tcps_rexmit_interval_min) { 2760 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2761 } else { 2762 tcp->tcp_rto = rto; 2763 } 2764 } 2765 if (ire_uinfo->iulp_ssthresh != 0) 2766 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2767 else 2768 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2769 if (ire_uinfo->iulp_spipe > 0) { 2770 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2771 tcps->tcps_max_buf); 2772 if (tcps->tcps_snd_lowat_fraction != 0) 2773 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2774 tcps->tcps_snd_lowat_fraction; 2775 (void) tcp_maxpsz_set(tcp, B_TRUE); 2776 } 2777 /* 2778 * Note that up till now, acceptor always inherits receive 2779 * window from the listener. But if there is a metrics 2780 * associated with a host, we should use that instead of 2781 * inheriting it from listener. Thus we need to pass this 2782 * info back to the caller. 2783 */ 2784 if (ire_uinfo->iulp_rpipe > 0) { 2785 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2786 tcps->tcps_max_buf); 2787 } 2788 2789 if (ire_uinfo->iulp_rtomax > 0) { 2790 tcp->tcp_second_timer_threshold = 2791 ire_uinfo->iulp_rtomax; 2792 } 2793 2794 /* 2795 * Use the metric option settings, iulp_tstamp_ok and 2796 * iulp_wscale_ok, only for active open. What this means 2797 * is that if the other side uses timestamp or window 2798 * scale option, TCP will also use those options. That 2799 * is for passive open. If the application sets a 2800 * large window, window scale is enabled regardless of 2801 * the value in iulp_wscale_ok. This is the behavior 2802 * since 2.6. So we keep it. 2803 * The only case left in passive open processing is the 2804 * check for SACK. 2805 * For ECN, it should probably be like SACK. But the 2806 * current value is binary, so we treat it like the other 2807 * cases. The metric only controls active open.For passive 2808 * open, the ndd param, tcp_ecn_permitted, controls the 2809 * behavior. 2810 */ 2811 if (!tcp_detached) { 2812 /* 2813 * The if check means that the following can only 2814 * be turned on by the metrics only IRE, but not off. 2815 */ 2816 if (ire_uinfo->iulp_tstamp_ok) 2817 tcp->tcp_snd_ts_ok = B_TRUE; 2818 if (ire_uinfo->iulp_wscale_ok) 2819 tcp->tcp_snd_ws_ok = B_TRUE; 2820 if (ire_uinfo->iulp_sack == 2) 2821 tcp->tcp_snd_sack_ok = B_TRUE; 2822 if (ire_uinfo->iulp_ecn_ok) 2823 tcp->tcp_ecn_ok = B_TRUE; 2824 } else { 2825 /* 2826 * Passive open. 2827 * 2828 * As above, the if check means that SACK can only be 2829 * turned on by the metric only IRE. 2830 */ 2831 if (ire_uinfo->iulp_sack > 0) { 2832 tcp->tcp_snd_sack_ok = B_TRUE; 2833 } 2834 } 2835 } 2836 2837 2838 /* 2839 * XXX: Note that currently, ire_max_frag can be as small as 68 2840 * because of PMTUd. So tcp_mss may go to negative if combined 2841 * length of all those options exceeds 28 bytes. But because 2842 * of the tcp_mss_min check below, we may not have a problem if 2843 * tcp_mss_min is of a reasonable value. The default is 1 so 2844 * the negative problem still exists. And the check defeats PMTUd. 2845 * In fact, if PMTUd finds that the MSS should be smaller than 2846 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2847 * value. 2848 * 2849 * We do not deal with that now. All those problems related to 2850 * PMTUd will be fixed later. 2851 */ 2852 ASSERT(ire->ire_max_frag != 0); 2853 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2854 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2855 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2856 mss = MIN(mss, IPV6_MIN_MTU); 2857 } 2858 } 2859 2860 /* Sanity check for MSS value. */ 2861 if (tcp->tcp_ipversion == IPV4_VERSION) 2862 mss_max = tcps->tcps_mss_max_ipv4; 2863 else 2864 mss_max = tcps->tcps_mss_max_ipv6; 2865 2866 if (tcp->tcp_ipversion == IPV6_VERSION && 2867 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2868 /* 2869 * After receiving an ICMPv6 "packet too big" message with a 2870 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2871 * will insert a 8-byte fragment header in every packet; we 2872 * reduce the MSS by that amount here. 2873 */ 2874 mss -= sizeof (ip6_frag_t); 2875 } 2876 2877 if (tcp->tcp_ipsec_overhead == 0) 2878 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2879 2880 mss -= tcp->tcp_ipsec_overhead; 2881 2882 if (mss < tcps->tcps_mss_min) 2883 mss = tcps->tcps_mss_min; 2884 if (mss > mss_max) 2885 mss = mss_max; 2886 2887 /* Note that this is the maximum MSS, excluding all options. */ 2888 tcp->tcp_mss = mss; 2889 2890 /* 2891 * Initialize the ISS here now that we have the full connection ID. 2892 * The RFC 1948 method of initial sequence number generation requires 2893 * knowledge of the full connection ID before setting the ISS. 2894 */ 2895 2896 tcp_iss_init(tcp); 2897 2898 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2899 tcp->tcp_loopback = B_TRUE; 2900 2901 if (tcp->tcp_ipversion == IPV4_VERSION) { 2902 hsp = tcp_hsp_lookup(tcp->tcp_remote, tcps); 2903 } else { 2904 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6, tcps); 2905 } 2906 2907 if (hsp != NULL) { 2908 /* Only modify if we're going to make them bigger */ 2909 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2910 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2911 if (tcps->tcps_snd_lowat_fraction != 0) 2912 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2913 tcps->tcps_snd_lowat_fraction; 2914 } 2915 2916 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2917 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2918 } 2919 2920 /* Copy timestamp flag only for active open */ 2921 if (!tcp_detached) 2922 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2923 } 2924 2925 if (sire != NULL) 2926 IRE_REFRELE(sire); 2927 2928 /* 2929 * If we got an IRE_CACHE and an ILL, go through their properties; 2930 * otherwise, this is deferred until later when we have an IRE_CACHE. 2931 */ 2932 if (tcp->tcp_loopback || 2933 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2934 /* 2935 * For incoming, see if this tcp may be MDT-capable. For 2936 * outgoing, this process has been taken care of through 2937 * tcp_rput_other. 2938 */ 2939 tcp_ire_ill_check(tcp, ire, ill, incoming); 2940 tcp->tcp_ire_ill_check_done = B_TRUE; 2941 } 2942 2943 mutex_enter(&connp->conn_lock); 2944 /* 2945 * Make sure that conn is not marked incipient 2946 * for incoming connections. A blind 2947 * removal of incipient flag is cheaper than 2948 * check and removal. 2949 */ 2950 connp->conn_state_flags &= ~CONN_INCIPIENT; 2951 2952 /* 2953 * Must not cache forwarding table routes 2954 * or recache an IRE after the conn_t has 2955 * had conn_ire_cache cleared and is flagged 2956 * unusable, (see the CONN_CACHE_IRE() macro). 2957 */ 2958 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2959 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2960 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2961 connp->conn_ire_cache = ire; 2962 IRE_UNTRACE_REF(ire); 2963 rw_exit(&ire->ire_bucket->irb_lock); 2964 mutex_exit(&connp->conn_lock); 2965 return (1); 2966 } 2967 rw_exit(&ire->ire_bucket->irb_lock); 2968 } 2969 mutex_exit(&connp->conn_lock); 2970 2971 if (ire->ire_mp == NULL) 2972 ire_refrele(ire); 2973 return (1); 2974 2975 error: 2976 if (ire->ire_mp == NULL) 2977 ire_refrele(ire); 2978 if (sire != NULL) 2979 ire_refrele(sire); 2980 return (0); 2981 } 2982 2983 /* 2984 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 2985 * O_T_BIND_REQ/T_BIND_REQ message. 2986 */ 2987 static void 2988 tcp_bind(tcp_t *tcp, mblk_t *mp) 2989 { 2990 sin_t *sin; 2991 sin6_t *sin6; 2992 mblk_t *mp1; 2993 in_port_t requested_port; 2994 in_port_t allocated_port; 2995 struct T_bind_req *tbr; 2996 boolean_t bind_to_req_port_only; 2997 boolean_t backlog_update = B_FALSE; 2998 boolean_t user_specified; 2999 in6_addr_t v6addr; 3000 ipaddr_t v4addr; 3001 uint_t origipversion; 3002 int err; 3003 queue_t *q = tcp->tcp_wq; 3004 conn_t *connp; 3005 mlp_type_t addrtype, mlptype; 3006 zone_t *zone; 3007 cred_t *cr; 3008 in_port_t mlp_port; 3009 tcp_stack_t *tcps = tcp->tcp_tcps; 3010 3011 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3012 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3013 if (tcp->tcp_debug) { 3014 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3015 "tcp_bind: bad req, len %u", 3016 (uint_t)(mp->b_wptr - mp->b_rptr)); 3017 } 3018 tcp_err_ack(tcp, mp, TPROTO, 0); 3019 return; 3020 } 3021 /* Make sure the largest address fits */ 3022 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3023 if (mp1 == NULL) { 3024 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3025 return; 3026 } 3027 mp = mp1; 3028 tbr = (struct T_bind_req *)mp->b_rptr; 3029 if (tcp->tcp_state >= TCPS_BOUND) { 3030 if ((tcp->tcp_state == TCPS_BOUND || 3031 tcp->tcp_state == TCPS_LISTEN) && 3032 tcp->tcp_conn_req_max != tbr->CONIND_number && 3033 tbr->CONIND_number > 0) { 3034 /* 3035 * Handle listen() increasing CONIND_number. 3036 * This is more "liberal" then what the TPI spec 3037 * requires but is needed to avoid a t_unbind 3038 * when handling listen() since the port number 3039 * might be "stolen" between the unbind and bind. 3040 */ 3041 backlog_update = B_TRUE; 3042 goto do_bind; 3043 } 3044 if (tcp->tcp_debug) { 3045 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3046 "tcp_bind: bad state, %d", tcp->tcp_state); 3047 } 3048 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3049 return; 3050 } 3051 origipversion = tcp->tcp_ipversion; 3052 3053 switch (tbr->ADDR_length) { 3054 case 0: /* request for a generic port */ 3055 tbr->ADDR_offset = sizeof (struct T_bind_req); 3056 if (tcp->tcp_family == AF_INET) { 3057 tbr->ADDR_length = sizeof (sin_t); 3058 sin = (sin_t *)&tbr[1]; 3059 *sin = sin_null; 3060 sin->sin_family = AF_INET; 3061 mp->b_wptr = (uchar_t *)&sin[1]; 3062 tcp->tcp_ipversion = IPV4_VERSION; 3063 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3064 } else { 3065 ASSERT(tcp->tcp_family == AF_INET6); 3066 tbr->ADDR_length = sizeof (sin6_t); 3067 sin6 = (sin6_t *)&tbr[1]; 3068 *sin6 = sin6_null; 3069 sin6->sin6_family = AF_INET6; 3070 mp->b_wptr = (uchar_t *)&sin6[1]; 3071 tcp->tcp_ipversion = IPV6_VERSION; 3072 V6_SET_ZERO(v6addr); 3073 } 3074 requested_port = 0; 3075 break; 3076 3077 case sizeof (sin_t): /* Complete IPv4 address */ 3078 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3079 sizeof (sin_t)); 3080 if (sin == NULL || !OK_32PTR((char *)sin)) { 3081 if (tcp->tcp_debug) { 3082 (void) strlog(TCP_MOD_ID, 0, 1, 3083 SL_ERROR|SL_TRACE, 3084 "tcp_bind: bad address parameter, " 3085 "offset %d, len %d", 3086 tbr->ADDR_offset, tbr->ADDR_length); 3087 } 3088 tcp_err_ack(tcp, mp, TPROTO, 0); 3089 return; 3090 } 3091 /* 3092 * With sockets sockfs will accept bogus sin_family in 3093 * bind() and replace it with the family used in the socket 3094 * call. 3095 */ 3096 if (sin->sin_family != AF_INET || 3097 tcp->tcp_family != AF_INET) { 3098 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3099 return; 3100 } 3101 requested_port = ntohs(sin->sin_port); 3102 tcp->tcp_ipversion = IPV4_VERSION; 3103 v4addr = sin->sin_addr.s_addr; 3104 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3105 break; 3106 3107 case sizeof (sin6_t): /* Complete IPv6 address */ 3108 sin6 = (sin6_t *)mi_offset_param(mp, 3109 tbr->ADDR_offset, sizeof (sin6_t)); 3110 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3111 if (tcp->tcp_debug) { 3112 (void) strlog(TCP_MOD_ID, 0, 1, 3113 SL_ERROR|SL_TRACE, 3114 "tcp_bind: bad IPv6 address parameter, " 3115 "offset %d, len %d", tbr->ADDR_offset, 3116 tbr->ADDR_length); 3117 } 3118 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3119 return; 3120 } 3121 if (sin6->sin6_family != AF_INET6 || 3122 tcp->tcp_family != AF_INET6) { 3123 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3124 return; 3125 } 3126 requested_port = ntohs(sin6->sin6_port); 3127 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3128 IPV4_VERSION : IPV6_VERSION; 3129 v6addr = sin6->sin6_addr; 3130 break; 3131 3132 default: 3133 if (tcp->tcp_debug) { 3134 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3135 "tcp_bind: bad address length, %d", 3136 tbr->ADDR_length); 3137 } 3138 tcp_err_ack(tcp, mp, TBADADDR, 0); 3139 return; 3140 } 3141 tcp->tcp_bound_source_v6 = v6addr; 3142 3143 /* Check for change in ipversion */ 3144 if (origipversion != tcp->tcp_ipversion) { 3145 ASSERT(tcp->tcp_family == AF_INET6); 3146 err = tcp->tcp_ipversion == IPV6_VERSION ? 3147 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3148 if (err) { 3149 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3150 return; 3151 } 3152 } 3153 3154 /* 3155 * Initialize family specific fields. Copy of the src addr. 3156 * in tcp_t is needed for the lookup funcs. 3157 */ 3158 if (tcp->tcp_ipversion == IPV6_VERSION) { 3159 tcp->tcp_ip6h->ip6_src = v6addr; 3160 } else { 3161 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3162 } 3163 tcp->tcp_ip_src_v6 = v6addr; 3164 3165 /* 3166 * For O_T_BIND_REQ: 3167 * Verify that the target port/addr is available, or choose 3168 * another. 3169 * For T_BIND_REQ: 3170 * Verify that the target port/addr is available or fail. 3171 * In both cases when it succeeds the tcp is inserted in the 3172 * bind hash table. This ensures that the operation is atomic 3173 * under the lock on the hash bucket. 3174 */ 3175 bind_to_req_port_only = requested_port != 0 && 3176 tbr->PRIM_type != O_T_BIND_REQ; 3177 /* 3178 * Get a valid port (within the anonymous range and should not 3179 * be a privileged one) to use if the user has not given a port. 3180 * If multiple threads are here, they may all start with 3181 * with the same initial port. But, it should be fine as long as 3182 * tcp_bindi will ensure that no two threads will be assigned 3183 * the same port. 3184 * 3185 * NOTE: XXX If a privileged process asks for an anonymous port, we 3186 * still check for ports only in the range > tcp_smallest_non_priv_port, 3187 * unless TCP_ANONPRIVBIND option is set. 3188 */ 3189 mlptype = mlptSingle; 3190 mlp_port = requested_port; 3191 if (requested_port == 0) { 3192 requested_port = tcp->tcp_anon_priv_bind ? 3193 tcp_get_next_priv_port(tcp) : 3194 tcp_update_next_port(tcps->tcps_next_port_to_try, 3195 tcp, B_TRUE); 3196 if (requested_port == 0) { 3197 tcp_err_ack(tcp, mp, TNOADDR, 0); 3198 return; 3199 } 3200 user_specified = B_FALSE; 3201 3202 /* 3203 * If the user went through one of the RPC interfaces to create 3204 * this socket and RPC is MLP in this zone, then give him an 3205 * anonymous MLP. 3206 */ 3207 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3208 connp = tcp->tcp_connp; 3209 if (connp->conn_anon_mlp && is_system_labeled()) { 3210 zone = crgetzone(cr); 3211 addrtype = tsol_mlp_addr_type(zone->zone_id, 3212 IPV6_VERSION, &v6addr, 3213 tcps->tcps_netstack->netstack_ip); 3214 if (addrtype == mlptSingle) { 3215 tcp_err_ack(tcp, mp, TNOADDR, 0); 3216 return; 3217 } 3218 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3219 PMAPPORT, addrtype); 3220 mlp_port = PMAPPORT; 3221 } 3222 } else { 3223 int i; 3224 boolean_t priv = B_FALSE; 3225 3226 /* 3227 * If the requested_port is in the well-known privileged range, 3228 * verify that the stream was opened by a privileged user. 3229 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3230 * but instead the code relies on: 3231 * - the fact that the address of the array and its size never 3232 * changes 3233 * - the atomic assignment of the elements of the array 3234 */ 3235 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3236 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 3237 priv = B_TRUE; 3238 } else { 3239 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 3240 if (requested_port == 3241 tcps->tcps_g_epriv_ports[i]) { 3242 priv = B_TRUE; 3243 break; 3244 } 3245 } 3246 } 3247 if (priv) { 3248 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3249 if (tcp->tcp_debug) { 3250 (void) strlog(TCP_MOD_ID, 0, 1, 3251 SL_ERROR|SL_TRACE, 3252 "tcp_bind: no priv for port %d", 3253 requested_port); 3254 } 3255 tcp_err_ack(tcp, mp, TACCES, 0); 3256 return; 3257 } 3258 } 3259 user_specified = B_TRUE; 3260 3261 connp = tcp->tcp_connp; 3262 if (is_system_labeled()) { 3263 zone = crgetzone(cr); 3264 addrtype = tsol_mlp_addr_type(zone->zone_id, 3265 IPV6_VERSION, &v6addr, 3266 tcps->tcps_netstack->netstack_ip); 3267 if (addrtype == mlptSingle) { 3268 tcp_err_ack(tcp, mp, TNOADDR, 0); 3269 return; 3270 } 3271 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3272 requested_port, addrtype); 3273 } 3274 } 3275 3276 if (mlptype != mlptSingle) { 3277 if (secpolicy_net_bindmlp(cr) != 0) { 3278 if (tcp->tcp_debug) { 3279 (void) strlog(TCP_MOD_ID, 0, 1, 3280 SL_ERROR|SL_TRACE, 3281 "tcp_bind: no priv for multilevel port %d", 3282 requested_port); 3283 } 3284 tcp_err_ack(tcp, mp, TACCES, 0); 3285 return; 3286 } 3287 3288 /* 3289 * If we're specifically binding a shared IP address and the 3290 * port is MLP on shared addresses, then check to see if this 3291 * zone actually owns the MLP. Reject if not. 3292 */ 3293 if (mlptype == mlptShared && addrtype == mlptShared) { 3294 /* 3295 * No need to handle exclusive-stack zones since 3296 * ALL_ZONES only applies to the shared stack. 3297 */ 3298 zoneid_t mlpzone; 3299 3300 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3301 htons(mlp_port)); 3302 if (connp->conn_zoneid != mlpzone) { 3303 if (tcp->tcp_debug) { 3304 (void) strlog(TCP_MOD_ID, 0, 1, 3305 SL_ERROR|SL_TRACE, 3306 "tcp_bind: attempt to bind port " 3307 "%d on shared addr in zone %d " 3308 "(should be %d)", 3309 mlp_port, connp->conn_zoneid, 3310 mlpzone); 3311 } 3312 tcp_err_ack(tcp, mp, TACCES, 0); 3313 return; 3314 } 3315 } 3316 3317 if (!user_specified) { 3318 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3319 requested_port, B_TRUE); 3320 if (err != 0) { 3321 if (tcp->tcp_debug) { 3322 (void) strlog(TCP_MOD_ID, 0, 1, 3323 SL_ERROR|SL_TRACE, 3324 "tcp_bind: cannot establish anon " 3325 "MLP for port %d", 3326 requested_port); 3327 } 3328 tcp_err_ack(tcp, mp, TSYSERR, err); 3329 return; 3330 } 3331 connp->conn_anon_port = B_TRUE; 3332 } 3333 connp->conn_mlp_type = mlptype; 3334 } 3335 3336 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3337 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3338 3339 if (allocated_port == 0) { 3340 connp->conn_mlp_type = mlptSingle; 3341 if (connp->conn_anon_port) { 3342 connp->conn_anon_port = B_FALSE; 3343 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3344 requested_port, B_FALSE); 3345 } 3346 if (bind_to_req_port_only) { 3347 if (tcp->tcp_debug) { 3348 (void) strlog(TCP_MOD_ID, 0, 1, 3349 SL_ERROR|SL_TRACE, 3350 "tcp_bind: requested addr busy"); 3351 } 3352 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3353 } else { 3354 /* If we are out of ports, fail the bind. */ 3355 if (tcp->tcp_debug) { 3356 (void) strlog(TCP_MOD_ID, 0, 1, 3357 SL_ERROR|SL_TRACE, 3358 "tcp_bind: out of ports?"); 3359 } 3360 tcp_err_ack(tcp, mp, TNOADDR, 0); 3361 } 3362 return; 3363 } 3364 ASSERT(tcp->tcp_state == TCPS_BOUND); 3365 do_bind: 3366 if (!backlog_update) { 3367 if (tcp->tcp_family == AF_INET) 3368 sin->sin_port = htons(allocated_port); 3369 else 3370 sin6->sin6_port = htons(allocated_port); 3371 } 3372 if (tcp->tcp_family == AF_INET) { 3373 if (tbr->CONIND_number != 0) { 3374 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3375 sizeof (sin_t)); 3376 } else { 3377 /* Just verify the local IP address */ 3378 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3379 } 3380 } else { 3381 if (tbr->CONIND_number != 0) { 3382 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3383 sizeof (sin6_t)); 3384 } else { 3385 /* Just verify the local IP address */ 3386 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3387 IPV6_ADDR_LEN); 3388 } 3389 } 3390 if (mp1 == NULL) { 3391 if (connp->conn_anon_port) { 3392 connp->conn_anon_port = B_FALSE; 3393 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3394 requested_port, B_FALSE); 3395 } 3396 connp->conn_mlp_type = mlptSingle; 3397 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3398 return; 3399 } 3400 3401 tbr->PRIM_type = T_BIND_ACK; 3402 mp->b_datap->db_type = M_PCPROTO; 3403 3404 /* Chain in the reply mp for tcp_rput() */ 3405 mp1->b_cont = mp; 3406 mp = mp1; 3407 3408 tcp->tcp_conn_req_max = tbr->CONIND_number; 3409 if (tcp->tcp_conn_req_max) { 3410 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 3411 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 3412 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 3413 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 3414 /* 3415 * If this is a listener, do not reset the eager list 3416 * and other stuffs. Note that we don't check if the 3417 * existing eager list meets the new tcp_conn_req_max 3418 * requirement. 3419 */ 3420 if (tcp->tcp_state != TCPS_LISTEN) { 3421 tcp->tcp_state = TCPS_LISTEN; 3422 /* Initialize the chain. Don't need the eager_lock */ 3423 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3424 tcp->tcp_eager_next_drop_q0 = tcp; 3425 tcp->tcp_eager_prev_drop_q0 = tcp; 3426 tcp->tcp_second_ctimer_threshold = 3427 tcps->tcps_ip_abort_linterval; 3428 } 3429 } 3430 3431 /* 3432 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3433 * processing continues in tcp_rput_other(). 3434 */ 3435 if (tcp->tcp_family == AF_INET6) { 3436 ASSERT(tcp->tcp_connp->conn_af_isv6); 3437 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3438 } else { 3439 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3440 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3441 } 3442 /* 3443 * If the bind cannot complete immediately 3444 * IP will arrange to call tcp_rput_other 3445 * when the bind completes. 3446 */ 3447 if (mp != NULL) { 3448 tcp_rput_other(tcp, mp); 3449 } else { 3450 /* 3451 * Bind will be resumed later. Need to ensure 3452 * that conn doesn't disappear when that happens. 3453 * This will be decremented in ip_resume_tcp_bind(). 3454 */ 3455 CONN_INC_REF(tcp->tcp_connp); 3456 } 3457 } 3458 3459 3460 /* 3461 * If the "bind_to_req_port_only" parameter is set, if the requested port 3462 * number is available, return it, If not return 0 3463 * 3464 * If "bind_to_req_port_only" parameter is not set and 3465 * If the requested port number is available, return it. If not, return 3466 * the first anonymous port we happen across. If no anonymous ports are 3467 * available, return 0. addr is the requested local address, if any. 3468 * 3469 * In either case, when succeeding update the tcp_t to record the port number 3470 * and insert it in the bind hash table. 3471 * 3472 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3473 * without setting SO_REUSEADDR. This is needed so that they 3474 * can be viewed as two independent transport protocols. 3475 */ 3476 static in_port_t 3477 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3478 int reuseaddr, boolean_t quick_connect, 3479 boolean_t bind_to_req_port_only, boolean_t user_specified) 3480 { 3481 /* number of times we have run around the loop */ 3482 int count = 0; 3483 /* maximum number of times to run around the loop */ 3484 int loopmax; 3485 conn_t *connp = tcp->tcp_connp; 3486 zoneid_t zoneid = connp->conn_zoneid; 3487 tcp_stack_t *tcps = tcp->tcp_tcps; 3488 3489 /* 3490 * Lookup for free addresses is done in a loop and "loopmax" 3491 * influences how long we spin in the loop 3492 */ 3493 if (bind_to_req_port_only) { 3494 /* 3495 * If the requested port is busy, don't bother to look 3496 * for a new one. Setting loop maximum count to 1 has 3497 * that effect. 3498 */ 3499 loopmax = 1; 3500 } else { 3501 /* 3502 * If the requested port is busy, look for a free one 3503 * in the anonymous port range. 3504 * Set loopmax appropriately so that one does not look 3505 * forever in the case all of the anonymous ports are in use. 3506 */ 3507 if (tcp->tcp_anon_priv_bind) { 3508 /* 3509 * loopmax = 3510 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3511 */ 3512 loopmax = IPPORT_RESERVED - 3513 tcps->tcps_min_anonpriv_port; 3514 } else { 3515 loopmax = (tcps->tcps_largest_anon_port - 3516 tcps->tcps_smallest_anon_port + 1); 3517 } 3518 } 3519 do { 3520 uint16_t lport; 3521 tf_t *tbf; 3522 tcp_t *ltcp; 3523 conn_t *lconnp; 3524 3525 lport = htons(port); 3526 3527 /* 3528 * Ensure that the tcp_t is not currently in the bind hash. 3529 * Hold the lock on the hash bucket to ensure that 3530 * the duplicate check plus the insertion is an atomic 3531 * operation. 3532 * 3533 * This function does an inline lookup on the bind hash list 3534 * Make sure that we access only members of tcp_t 3535 * and that we don't look at tcp_tcp, since we are not 3536 * doing a CONN_INC_REF. 3537 */ 3538 tcp_bind_hash_remove(tcp); 3539 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3540 mutex_enter(&tbf->tf_lock); 3541 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3542 ltcp = ltcp->tcp_bind_hash) { 3543 boolean_t not_socket; 3544 boolean_t exclbind; 3545 3546 if (lport != ltcp->tcp_lport) 3547 continue; 3548 3549 lconnp = ltcp->tcp_connp; 3550 3551 /* 3552 * On a labeled system, we must treat bindings to ports 3553 * on shared IP addresses by sockets with MAC exemption 3554 * privilege as being in all zones, as there's 3555 * otherwise no way to identify the right receiver. 3556 */ 3557 if (!IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) && 3558 !lconnp->conn_mac_exempt && 3559 !connp->conn_mac_exempt) 3560 continue; 3561 3562 /* 3563 * If TCP_EXCLBIND is set for either the bound or 3564 * binding endpoint, the semantics of bind 3565 * is changed according to the following. 3566 * 3567 * spec = specified address (v4 or v6) 3568 * unspec = unspecified address (v4 or v6) 3569 * A = specified addresses are different for endpoints 3570 * 3571 * bound bind to allowed 3572 * ------------------------------------- 3573 * unspec unspec no 3574 * unspec spec no 3575 * spec unspec no 3576 * spec spec yes if A 3577 * 3578 * For labeled systems, SO_MAC_EXEMPT behaves the same 3579 * as TCP_EXCLBIND, except that zoneid is ignored. 3580 * 3581 * Note: 3582 * 3583 * 1. Because of TLI semantics, an endpoint can go 3584 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3585 * TCPS_BOUND, depending on whether it is originally 3586 * a listener or not. That is why we need to check 3587 * for states greater than or equal to TCPS_BOUND 3588 * here. 3589 * 3590 * 2. Ideally, we should only check for state equals 3591 * to TCPS_LISTEN. And the following check should be 3592 * added. 3593 * 3594 * if (ltcp->tcp_state == TCPS_LISTEN || 3595 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3596 * ... 3597 * } 3598 * 3599 * The semantics will be changed to this. If the 3600 * endpoint on the list is in state not equal to 3601 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3602 * set, let the bind succeed. 3603 * 3604 * Because of (1), we cannot do that for TLI 3605 * endpoints. But we can do that for socket endpoints. 3606 * If in future, we can change this going back 3607 * semantics, we can use the above check for TLI also. 3608 */ 3609 not_socket = !(TCP_IS_SOCKET(ltcp) && 3610 TCP_IS_SOCKET(tcp)); 3611 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3612 3613 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3614 (exclbind && (not_socket || 3615 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3616 if (V6_OR_V4_INADDR_ANY( 3617 ltcp->tcp_bound_source_v6) || 3618 V6_OR_V4_INADDR_ANY(*laddr) || 3619 IN6_ARE_ADDR_EQUAL(laddr, 3620 <cp->tcp_bound_source_v6)) { 3621 break; 3622 } 3623 continue; 3624 } 3625 3626 /* 3627 * Check ipversion to allow IPv4 and IPv6 sockets to 3628 * have disjoint port number spaces, if *_EXCLBIND 3629 * is not set and only if the application binds to a 3630 * specific port. We use the same autoassigned port 3631 * number space for IPv4 and IPv6 sockets. 3632 */ 3633 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3634 bind_to_req_port_only) 3635 continue; 3636 3637 /* 3638 * Ideally, we should make sure that the source 3639 * address, remote address, and remote port in the 3640 * four tuple for this tcp-connection is unique. 3641 * However, trying to find out the local source 3642 * address would require too much code duplication 3643 * with IP, since IP needs needs to have that code 3644 * to support userland TCP implementations. 3645 */ 3646 if (quick_connect && 3647 (ltcp->tcp_state > TCPS_LISTEN) && 3648 ((tcp->tcp_fport != ltcp->tcp_fport) || 3649 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3650 <cp->tcp_remote_v6))) 3651 continue; 3652 3653 if (!reuseaddr) { 3654 /* 3655 * No socket option SO_REUSEADDR. 3656 * If existing port is bound to 3657 * a non-wildcard IP address 3658 * and the requesting stream is 3659 * bound to a distinct 3660 * different IP addresses 3661 * (non-wildcard, also), keep 3662 * going. 3663 */ 3664 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3665 !V6_OR_V4_INADDR_ANY( 3666 ltcp->tcp_bound_source_v6) && 3667 !IN6_ARE_ADDR_EQUAL(laddr, 3668 <cp->tcp_bound_source_v6)) 3669 continue; 3670 if (ltcp->tcp_state >= TCPS_BOUND) { 3671 /* 3672 * This port is being used and 3673 * its state is >= TCPS_BOUND, 3674 * so we can't bind to it. 3675 */ 3676 break; 3677 } 3678 } else { 3679 /* 3680 * socket option SO_REUSEADDR is set on the 3681 * binding tcp_t. 3682 * 3683 * If two streams are bound to 3684 * same IP address or both addr 3685 * and bound source are wildcards 3686 * (INADDR_ANY), we want to stop 3687 * searching. 3688 * We have found a match of IP source 3689 * address and source port, which is 3690 * refused regardless of the 3691 * SO_REUSEADDR setting, so we break. 3692 */ 3693 if (IN6_ARE_ADDR_EQUAL(laddr, 3694 <cp->tcp_bound_source_v6) && 3695 (ltcp->tcp_state == TCPS_LISTEN || 3696 ltcp->tcp_state == TCPS_BOUND)) 3697 break; 3698 } 3699 } 3700 if (ltcp != NULL) { 3701 /* The port number is busy */ 3702 mutex_exit(&tbf->tf_lock); 3703 } else { 3704 /* 3705 * This port is ours. Insert in fanout and mark as 3706 * bound to prevent others from getting the port 3707 * number. 3708 */ 3709 tcp->tcp_state = TCPS_BOUND; 3710 tcp->tcp_lport = htons(port); 3711 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3712 3713 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3714 tcp->tcp_lport)] == tbf); 3715 tcp_bind_hash_insert(tbf, tcp, 1); 3716 3717 mutex_exit(&tbf->tf_lock); 3718 3719 /* 3720 * We don't want tcp_next_port_to_try to "inherit" 3721 * a port number supplied by the user in a bind. 3722 */ 3723 if (user_specified) 3724 return (port); 3725 3726 /* 3727 * This is the only place where tcp_next_port_to_try 3728 * is updated. After the update, it may or may not 3729 * be in the valid range. 3730 */ 3731 if (!tcp->tcp_anon_priv_bind) 3732 tcps->tcps_next_port_to_try = port + 1; 3733 return (port); 3734 } 3735 3736 if (tcp->tcp_anon_priv_bind) { 3737 port = tcp_get_next_priv_port(tcp); 3738 } else { 3739 if (count == 0 && user_specified) { 3740 /* 3741 * We may have to return an anonymous port. So 3742 * get one to start with. 3743 */ 3744 port = 3745 tcp_update_next_port( 3746 tcps->tcps_next_port_to_try, 3747 tcp, B_TRUE); 3748 user_specified = B_FALSE; 3749 } else { 3750 port = tcp_update_next_port(port + 1, tcp, 3751 B_FALSE); 3752 } 3753 } 3754 if (port == 0) 3755 break; 3756 3757 /* 3758 * Don't let this loop run forever in the case where 3759 * all of the anonymous ports are in use. 3760 */ 3761 } while (++count < loopmax); 3762 return (0); 3763 } 3764 3765 /* 3766 * tcp_clean_death / tcp_close_detached must not be called more than once 3767 * on a tcp. Thus every function that potentially calls tcp_clean_death 3768 * must check for the tcp state before calling tcp_clean_death. 3769 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3770 * tcp_timer_handler, all check for the tcp state. 3771 */ 3772 /* ARGSUSED */ 3773 void 3774 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3775 { 3776 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3777 3778 freemsg(mp); 3779 if (tcp->tcp_state > TCPS_BOUND) 3780 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, ETIMEDOUT, 5); 3781 } 3782 3783 /* 3784 * We are dying for some reason. Try to do it gracefully. (May be called 3785 * as writer.) 3786 * 3787 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3788 * done by a service procedure). 3789 * TBD - Should the return value distinguish between the tcp_t being 3790 * freed and it being reinitialized? 3791 */ 3792 static int 3793 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3794 { 3795 mblk_t *mp; 3796 queue_t *q; 3797 tcp_stack_t *tcps = tcp->tcp_tcps; 3798 3799 TCP_CLD_STAT(tag); 3800 3801 #if TCP_TAG_CLEAN_DEATH 3802 tcp->tcp_cleandeathtag = tag; 3803 #endif 3804 3805 if (tcp->tcp_fused) 3806 tcp_unfuse(tcp); 3807 3808 if (tcp->tcp_linger_tid != 0 && 3809 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3810 tcp_stop_lingering(tcp); 3811 } 3812 3813 ASSERT(tcp != NULL); 3814 ASSERT((tcp->tcp_family == AF_INET && 3815 tcp->tcp_ipversion == IPV4_VERSION) || 3816 (tcp->tcp_family == AF_INET6 && 3817 (tcp->tcp_ipversion == IPV4_VERSION || 3818 tcp->tcp_ipversion == IPV6_VERSION))); 3819 3820 if (TCP_IS_DETACHED(tcp)) { 3821 if (tcp->tcp_hard_binding) { 3822 /* 3823 * Its an eager that we are dealing with. We close the 3824 * eager but in case a conn_ind has already gone to the 3825 * listener, let tcp_accept_finish() send a discon_ind 3826 * to the listener and drop the last reference. If the 3827 * listener doesn't even know about the eager i.e. the 3828 * conn_ind hasn't gone up, blow away the eager and drop 3829 * the last reference as well. If the conn_ind has gone 3830 * up, state should be BOUND. tcp_accept_finish 3831 * will figure out that the connection has received a 3832 * RST and will send a DISCON_IND to the application. 3833 */ 3834 tcp_closei_local(tcp); 3835 if (!tcp->tcp_tconnind_started) { 3836 CONN_DEC_REF(tcp->tcp_connp); 3837 } else { 3838 tcp->tcp_state = TCPS_BOUND; 3839 } 3840 } else { 3841 tcp_close_detached(tcp); 3842 } 3843 return (0); 3844 } 3845 3846 TCP_STAT(tcps, tcp_clean_death_nondetached); 3847 3848 /* 3849 * If T_ORDREL_IND has not been sent yet (done when service routine 3850 * is run) postpone cleaning up the endpoint until service routine 3851 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3852 * client_errno since tcp_close uses the client_errno field. 3853 */ 3854 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3855 if (err != 0) 3856 tcp->tcp_client_errno = err; 3857 3858 tcp->tcp_deferred_clean_death = B_TRUE; 3859 return (-1); 3860 } 3861 3862 q = tcp->tcp_rq; 3863 3864 /* Trash all inbound data */ 3865 flushq(q, FLUSHALL); 3866 3867 /* 3868 * If we are at least part way open and there is error 3869 * (err==0 implies no error) 3870 * notify our client by a T_DISCON_IND. 3871 */ 3872 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3873 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3874 !TCP_IS_SOCKET(tcp)) { 3875 /* 3876 * Send M_FLUSH according to TPI. Because sockets will 3877 * (and must) ignore FLUSHR we do that only for TPI 3878 * endpoints and sockets in STREAMS mode. 3879 */ 3880 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3881 } 3882 if (tcp->tcp_debug) { 3883 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3884 "tcp_clean_death: discon err %d", err); 3885 } 3886 mp = mi_tpi_discon_ind(NULL, err, 0); 3887 if (mp != NULL) { 3888 putnext(q, mp); 3889 } else { 3890 if (tcp->tcp_debug) { 3891 (void) strlog(TCP_MOD_ID, 0, 1, 3892 SL_ERROR|SL_TRACE, 3893 "tcp_clean_death, sending M_ERROR"); 3894 } 3895 (void) putnextctl1(q, M_ERROR, EPROTO); 3896 } 3897 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3898 /* SYN_SENT or SYN_RCVD */ 3899 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3900 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3901 /* ESTABLISHED or CLOSE_WAIT */ 3902 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3903 } 3904 } 3905 3906 tcp_reinit(tcp); 3907 return (-1); 3908 } 3909 3910 /* 3911 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3912 * to expire, stop the wait and finish the close. 3913 */ 3914 static void 3915 tcp_stop_lingering(tcp_t *tcp) 3916 { 3917 clock_t delta = 0; 3918 tcp_stack_t *tcps = tcp->tcp_tcps; 3919 3920 tcp->tcp_linger_tid = 0; 3921 if (tcp->tcp_state > TCPS_LISTEN) { 3922 tcp_acceptor_hash_remove(tcp); 3923 mutex_enter(&tcp->tcp_non_sq_lock); 3924 if (tcp->tcp_flow_stopped) { 3925 tcp_clrqfull(tcp); 3926 } 3927 mutex_exit(&tcp->tcp_non_sq_lock); 3928 3929 if (tcp->tcp_timer_tid != 0) { 3930 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3931 tcp->tcp_timer_tid = 0; 3932 } 3933 /* 3934 * Need to cancel those timers which will not be used when 3935 * TCP is detached. This has to be done before the tcp_wq 3936 * is set to the global queue. 3937 */ 3938 tcp_timers_stop(tcp); 3939 3940 3941 tcp->tcp_detached = B_TRUE; 3942 ASSERT(tcps->tcps_g_q != NULL); 3943 tcp->tcp_rq = tcps->tcps_g_q; 3944 tcp->tcp_wq = WR(tcps->tcps_g_q); 3945 3946 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3947 tcp_time_wait_append(tcp); 3948 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3949 goto finish; 3950 } 3951 3952 /* 3953 * If delta is zero the timer event wasn't executed and was 3954 * successfully canceled. In this case we need to restart it 3955 * with the minimal delta possible. 3956 */ 3957 if (delta >= 0) { 3958 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3959 delta ? delta : 1); 3960 } 3961 } else { 3962 tcp_closei_local(tcp); 3963 CONN_DEC_REF(tcp->tcp_connp); 3964 } 3965 finish: 3966 /* Signal closing thread that it can complete close */ 3967 mutex_enter(&tcp->tcp_closelock); 3968 tcp->tcp_detached = B_TRUE; 3969 ASSERT(tcps->tcps_g_q != NULL); 3970 tcp->tcp_rq = tcps->tcps_g_q; 3971 tcp->tcp_wq = WR(tcps->tcps_g_q); 3972 tcp->tcp_closed = 1; 3973 cv_signal(&tcp->tcp_closecv); 3974 mutex_exit(&tcp->tcp_closelock); 3975 } 3976 3977 /* 3978 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3979 * expires. 3980 */ 3981 static void 3982 tcp_close_linger_timeout(void *arg) 3983 { 3984 conn_t *connp = (conn_t *)arg; 3985 tcp_t *tcp = connp->conn_tcp; 3986 3987 tcp->tcp_client_errno = ETIMEDOUT; 3988 tcp_stop_lingering(tcp); 3989 } 3990 3991 static int 3992 tcp_close(queue_t *q, int flags) 3993 { 3994 conn_t *connp = Q_TO_CONN(q); 3995 tcp_t *tcp = connp->conn_tcp; 3996 mblk_t *mp = &tcp->tcp_closemp; 3997 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3998 boolean_t linger_interrupted = B_FALSE; 3999 mblk_t *bp; 4000 4001 ASSERT(WR(q)->q_next == NULL); 4002 ASSERT(connp->conn_ref >= 2); 4003 ASSERT((connp->conn_flags & IPCL_TCPMOD) == 0); 4004 4005 /* 4006 * We are being closed as /dev/tcp or /dev/tcp6. 4007 * 4008 * Mark the conn as closing. ill_pending_mp_add will not 4009 * add any mp to the pending mp list, after this conn has 4010 * started closing. Same for sq_pending_mp_add 4011 */ 4012 mutex_enter(&connp->conn_lock); 4013 connp->conn_state_flags |= CONN_CLOSING; 4014 if (connp->conn_oper_pending_ill != NULL) 4015 conn_ioctl_cleanup_reqd = B_TRUE; 4016 CONN_INC_REF_LOCKED(connp); 4017 mutex_exit(&connp->conn_lock); 4018 tcp->tcp_closeflags = (uint8_t)flags; 4019 ASSERT(connp->conn_ref >= 3); 4020 4021 /* 4022 * tcp_closemp_used is used below without any protection of a lock 4023 * as we don't expect any one else to use it concurrently at this 4024 * point otherwise it would be a major defect, though we do 4025 * increment tcp_closemp_used to record any attempt to reuse 4026 * tcp_closemp while it is still in use. This would help debugging. 4027 */ 4028 4029 if (mp->b_prev == NULL) { 4030 tcp->tcp_closemp_used = 1; 4031 } else { 4032 tcp->tcp_closemp_used++; 4033 ASSERT(mp->b_prev == NULL); 4034 } 4035 4036 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4037 4038 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4039 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4040 4041 mutex_enter(&tcp->tcp_closelock); 4042 while (!tcp->tcp_closed) { 4043 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4044 /* 4045 * We got interrupted. Check if we are lingering, 4046 * if yes, post a message to stop and wait until 4047 * tcp_closed is set. If we aren't lingering, 4048 * just go back around. 4049 */ 4050 if (tcp->tcp_linger && 4051 tcp->tcp_lingertime > 0 && 4052 !linger_interrupted) { 4053 mutex_exit(&tcp->tcp_closelock); 4054 /* Entering squeue, bump ref count. */ 4055 CONN_INC_REF(connp); 4056 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4057 squeue_enter(connp->conn_sqp, bp, 4058 tcp_linger_interrupted, connp, 4059 SQTAG_IP_TCP_CLOSE); 4060 linger_interrupted = B_TRUE; 4061 mutex_enter(&tcp->tcp_closelock); 4062 } 4063 } 4064 } 4065 mutex_exit(&tcp->tcp_closelock); 4066 4067 /* 4068 * In the case of listener streams that have eagers in the q or q0 4069 * we wait for the eagers to drop their reference to us. tcp_rq and 4070 * tcp_wq of the eagers point to our queues. By waiting for the 4071 * refcnt to drop to 1, we are sure that the eagers have cleaned 4072 * up their queue pointers and also dropped their references to us. 4073 */ 4074 if (tcp->tcp_wait_for_eagers) { 4075 mutex_enter(&connp->conn_lock); 4076 while (connp->conn_ref != 1) { 4077 cv_wait(&connp->conn_cv, &connp->conn_lock); 4078 } 4079 mutex_exit(&connp->conn_lock); 4080 } 4081 /* 4082 * ioctl cleanup. The mp is queued in the 4083 * ill_pending_mp or in the sq_pending_mp. 4084 */ 4085 if (conn_ioctl_cleanup_reqd) 4086 conn_ioctl_cleanup(connp); 4087 4088 qprocsoff(q); 4089 inet_minor_free(ip_minor_arena, connp->conn_dev); 4090 4091 tcp->tcp_cpid = -1; 4092 4093 /* 4094 * Drop IP's reference on the conn. This is the last reference 4095 * on the connp if the state was less than established. If the 4096 * connection has gone into timewait state, then we will have 4097 * one ref for the TCP and one more ref (total of two) for the 4098 * classifier connected hash list (a timewait connections stays 4099 * in connected hash till closed). 4100 * 4101 * We can't assert the references because there might be other 4102 * transient reference places because of some walkers or queued 4103 * packets in squeue for the timewait state. 4104 */ 4105 CONN_DEC_REF(connp); 4106 q->q_ptr = WR(q)->q_ptr = NULL; 4107 return (0); 4108 } 4109 4110 static int 4111 tcpclose_accept(queue_t *q) 4112 { 4113 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4114 4115 /* 4116 * We had opened an acceptor STREAM for sockfs which is 4117 * now being closed due to some error. 4118 */ 4119 qprocsoff(q); 4120 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 4121 q->q_ptr = WR(q)->q_ptr = NULL; 4122 return (0); 4123 } 4124 4125 /* 4126 * Called by tcp_close() routine via squeue when lingering is 4127 * interrupted by a signal. 4128 */ 4129 4130 /* ARGSUSED */ 4131 static void 4132 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4133 { 4134 conn_t *connp = (conn_t *)arg; 4135 tcp_t *tcp = connp->conn_tcp; 4136 4137 freeb(mp); 4138 if (tcp->tcp_linger_tid != 0 && 4139 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4140 tcp_stop_lingering(tcp); 4141 tcp->tcp_client_errno = EINTR; 4142 } 4143 } 4144 4145 /* 4146 * Called by streams close routine via squeues when our client blows off her 4147 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4148 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4149 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4150 * acked. 4151 * 4152 * NOTE: tcp_close potentially returns error when lingering. 4153 * However, the stream head currently does not pass these errors 4154 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4155 * errors to the application (from tsleep()) and not errors 4156 * like ECONNRESET caused by receiving a reset packet. 4157 */ 4158 4159 /* ARGSUSED */ 4160 static void 4161 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4162 { 4163 char *msg; 4164 conn_t *connp = (conn_t *)arg; 4165 tcp_t *tcp = connp->conn_tcp; 4166 clock_t delta = 0; 4167 tcp_stack_t *tcps = tcp->tcp_tcps; 4168 4169 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4170 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4171 4172 /* Cancel any pending timeout */ 4173 if (tcp->tcp_ordrelid != 0) { 4174 if (tcp->tcp_timeout) { 4175 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4176 } 4177 tcp->tcp_ordrelid = 0; 4178 tcp->tcp_timeout = B_FALSE; 4179 } 4180 4181 mutex_enter(&tcp->tcp_eager_lock); 4182 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4183 /* Cleanup for listener */ 4184 tcp_eager_cleanup(tcp, 0); 4185 tcp->tcp_wait_for_eagers = 1; 4186 } 4187 mutex_exit(&tcp->tcp_eager_lock); 4188 4189 connp->conn_mdt_ok = B_FALSE; 4190 tcp->tcp_mdt = B_FALSE; 4191 4192 connp->conn_lso_ok = B_FALSE; 4193 tcp->tcp_lso = B_FALSE; 4194 4195 msg = NULL; 4196 switch (tcp->tcp_state) { 4197 case TCPS_CLOSED: 4198 case TCPS_IDLE: 4199 case TCPS_BOUND: 4200 case TCPS_LISTEN: 4201 break; 4202 case TCPS_SYN_SENT: 4203 msg = "tcp_close, during connect"; 4204 break; 4205 case TCPS_SYN_RCVD: 4206 /* 4207 * Close during the connect 3-way handshake 4208 * but here there may or may not be pending data 4209 * already on queue. Process almost same as in 4210 * the ESTABLISHED state. 4211 */ 4212 /* FALLTHRU */ 4213 default: 4214 if (tcp->tcp_fused) 4215 tcp_unfuse(tcp); 4216 4217 /* 4218 * If SO_LINGER has set a zero linger time, abort the 4219 * connection with a reset. 4220 */ 4221 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4222 msg = "tcp_close, zero lingertime"; 4223 break; 4224 } 4225 4226 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4227 /* 4228 * Abort connection if there is unread data queued. 4229 */ 4230 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4231 msg = "tcp_close, unread data"; 4232 break; 4233 } 4234 /* 4235 * tcp_hard_bound is now cleared thus all packets go through 4236 * tcp_lookup. This fact is used by tcp_detach below. 4237 * 4238 * We have done a qwait() above which could have possibly 4239 * drained more messages in turn causing transition to a 4240 * different state. Check whether we have to do the rest 4241 * of the processing or not. 4242 */ 4243 if (tcp->tcp_state <= TCPS_LISTEN) 4244 break; 4245 4246 /* 4247 * Transmit the FIN before detaching the tcp_t. 4248 * After tcp_detach returns this queue/perimeter 4249 * no longer owns the tcp_t thus others can modify it. 4250 */ 4251 (void) tcp_xmit_end(tcp); 4252 4253 /* 4254 * If lingering on close then wait until the fin is acked, 4255 * the SO_LINGER time passes, or a reset is sent/received. 4256 */ 4257 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4258 !(tcp->tcp_fin_acked) && 4259 tcp->tcp_state >= TCPS_ESTABLISHED) { 4260 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4261 tcp->tcp_client_errno = EWOULDBLOCK; 4262 } else if (tcp->tcp_client_errno == 0) { 4263 4264 ASSERT(tcp->tcp_linger_tid == 0); 4265 4266 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4267 tcp_close_linger_timeout, 4268 tcp->tcp_lingertime * hz); 4269 4270 /* tcp_close_linger_timeout will finish close */ 4271 if (tcp->tcp_linger_tid == 0) 4272 tcp->tcp_client_errno = ENOSR; 4273 else 4274 return; 4275 } 4276 4277 /* 4278 * Check if we need to detach or just close 4279 * the instance. 4280 */ 4281 if (tcp->tcp_state <= TCPS_LISTEN) 4282 break; 4283 } 4284 4285 /* 4286 * Make sure that no other thread will access the tcp_rq of 4287 * this instance (through lookups etc.) as tcp_rq will go 4288 * away shortly. 4289 */ 4290 tcp_acceptor_hash_remove(tcp); 4291 4292 mutex_enter(&tcp->tcp_non_sq_lock); 4293 if (tcp->tcp_flow_stopped) { 4294 tcp_clrqfull(tcp); 4295 } 4296 mutex_exit(&tcp->tcp_non_sq_lock); 4297 4298 if (tcp->tcp_timer_tid != 0) { 4299 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4300 tcp->tcp_timer_tid = 0; 4301 } 4302 /* 4303 * Need to cancel those timers which will not be used when 4304 * TCP is detached. This has to be done before the tcp_wq 4305 * is set to the global queue. 4306 */ 4307 tcp_timers_stop(tcp); 4308 4309 tcp->tcp_detached = B_TRUE; 4310 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4311 tcp_time_wait_append(tcp); 4312 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4313 ASSERT(connp->conn_ref >= 3); 4314 goto finish; 4315 } 4316 4317 /* 4318 * If delta is zero the timer event wasn't executed and was 4319 * successfully canceled. In this case we need to restart it 4320 * with the minimal delta possible. 4321 */ 4322 if (delta >= 0) 4323 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4324 delta ? delta : 1); 4325 4326 ASSERT(connp->conn_ref >= 3); 4327 goto finish; 4328 } 4329 4330 /* Detach did not complete. Still need to remove q from stream. */ 4331 if (msg) { 4332 if (tcp->tcp_state == TCPS_ESTABLISHED || 4333 tcp->tcp_state == TCPS_CLOSE_WAIT) 4334 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4335 if (tcp->tcp_state == TCPS_SYN_SENT || 4336 tcp->tcp_state == TCPS_SYN_RCVD) 4337 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4338 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4339 } 4340 4341 tcp_closei_local(tcp); 4342 CONN_DEC_REF(connp); 4343 ASSERT(connp->conn_ref >= 2); 4344 4345 finish: 4346 /* 4347 * Although packets are always processed on the correct 4348 * tcp's perimeter and access is serialized via squeue's, 4349 * IP still needs a queue when sending packets in time_wait 4350 * state so use WR(tcps_g_q) till ip_output() can be 4351 * changed to deal with just connp. For read side, we 4352 * could have set tcp_rq to NULL but there are some cases 4353 * in tcp_rput_data() from early days of this code which 4354 * do a putnext without checking if tcp is closed. Those 4355 * need to be identified before both tcp_rq and tcp_wq 4356 * can be set to NULL and tcps_g_q can disappear forever. 4357 */ 4358 mutex_enter(&tcp->tcp_closelock); 4359 /* 4360 * Don't change the queues in the case of a listener that has 4361 * eagers in its q or q0. It could surprise the eagers. 4362 * Instead wait for the eagers outside the squeue. 4363 */ 4364 if (!tcp->tcp_wait_for_eagers) { 4365 tcp->tcp_detached = B_TRUE; 4366 /* 4367 * When default queue is closing we set tcps_g_q to NULL 4368 * after the close is done. 4369 */ 4370 ASSERT(tcps->tcps_g_q != NULL); 4371 tcp->tcp_rq = tcps->tcps_g_q; 4372 tcp->tcp_wq = WR(tcps->tcps_g_q); 4373 } 4374 4375 /* Signal tcp_close() to finish closing. */ 4376 tcp->tcp_closed = 1; 4377 cv_signal(&tcp->tcp_closecv); 4378 mutex_exit(&tcp->tcp_closelock); 4379 } 4380 4381 4382 /* 4383 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4384 * Some stream heads get upset if they see these later on as anything but NULL. 4385 */ 4386 static void 4387 tcp_close_mpp(mblk_t **mpp) 4388 { 4389 mblk_t *mp; 4390 4391 if ((mp = *mpp) != NULL) { 4392 do { 4393 mp->b_next = NULL; 4394 mp->b_prev = NULL; 4395 } while ((mp = mp->b_cont) != NULL); 4396 4397 mp = *mpp; 4398 *mpp = NULL; 4399 freemsg(mp); 4400 } 4401 } 4402 4403 /* Do detached close. */ 4404 static void 4405 tcp_close_detached(tcp_t *tcp) 4406 { 4407 if (tcp->tcp_fused) 4408 tcp_unfuse(tcp); 4409 4410 /* 4411 * Clustering code serializes TCP disconnect callbacks and 4412 * cluster tcp list walks by blocking a TCP disconnect callback 4413 * if a cluster tcp list walk is in progress. This ensures 4414 * accurate accounting of TCPs in the cluster code even though 4415 * the TCP list walk itself is not atomic. 4416 */ 4417 tcp_closei_local(tcp); 4418 CONN_DEC_REF(tcp->tcp_connp); 4419 } 4420 4421 /* 4422 * Stop all TCP timers, and free the timer mblks if requested. 4423 */ 4424 void 4425 tcp_timers_stop(tcp_t *tcp) 4426 { 4427 if (tcp->tcp_timer_tid != 0) { 4428 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4429 tcp->tcp_timer_tid = 0; 4430 } 4431 if (tcp->tcp_ka_tid != 0) { 4432 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4433 tcp->tcp_ka_tid = 0; 4434 } 4435 if (tcp->tcp_ack_tid != 0) { 4436 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4437 tcp->tcp_ack_tid = 0; 4438 } 4439 if (tcp->tcp_push_tid != 0) { 4440 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4441 tcp->tcp_push_tid = 0; 4442 } 4443 } 4444 4445 /* 4446 * The tcp_t is going away. Remove it from all lists and set it 4447 * to TCPS_CLOSED. The freeing up of memory is deferred until 4448 * tcp_inactive. This is needed since a thread in tcp_rput might have 4449 * done a CONN_INC_REF on this structure before it was removed from the 4450 * hashes. 4451 */ 4452 static void 4453 tcp_closei_local(tcp_t *tcp) 4454 { 4455 ire_t *ire; 4456 conn_t *connp = tcp->tcp_connp; 4457 tcp_stack_t *tcps = tcp->tcp_tcps; 4458 4459 if (!TCP_IS_SOCKET(tcp)) 4460 tcp_acceptor_hash_remove(tcp); 4461 4462 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4463 tcp->tcp_ibsegs = 0; 4464 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4465 tcp->tcp_obsegs = 0; 4466 4467 /* 4468 * If we are an eager connection hanging off a listener that 4469 * hasn't formally accepted the connection yet, get off his 4470 * list and blow off any data that we have accumulated. 4471 */ 4472 if (tcp->tcp_listener != NULL) { 4473 tcp_t *listener = tcp->tcp_listener; 4474 mutex_enter(&listener->tcp_eager_lock); 4475 /* 4476 * tcp_tconnind_started == B_TRUE means that the 4477 * conn_ind has already gone to listener. At 4478 * this point, eager will be closed but we 4479 * leave it in listeners eager list so that 4480 * if listener decides to close without doing 4481 * accept, we can clean this up. In tcp_wput_accept 4482 * we take care of the case of accept on closed 4483 * eager. 4484 */ 4485 if (!tcp->tcp_tconnind_started) { 4486 tcp_eager_unlink(tcp); 4487 mutex_exit(&listener->tcp_eager_lock); 4488 /* 4489 * We don't want to have any pointers to the 4490 * listener queue, after we have released our 4491 * reference on the listener 4492 */ 4493 ASSERT(tcps->tcps_g_q != NULL); 4494 tcp->tcp_rq = tcps->tcps_g_q; 4495 tcp->tcp_wq = WR(tcps->tcps_g_q); 4496 CONN_DEC_REF(listener->tcp_connp); 4497 } else { 4498 mutex_exit(&listener->tcp_eager_lock); 4499 } 4500 } 4501 4502 /* Stop all the timers */ 4503 tcp_timers_stop(tcp); 4504 4505 if (tcp->tcp_state == TCPS_LISTEN) { 4506 if (tcp->tcp_ip_addr_cache) { 4507 kmem_free((void *)tcp->tcp_ip_addr_cache, 4508 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4509 tcp->tcp_ip_addr_cache = NULL; 4510 } 4511 } 4512 mutex_enter(&tcp->tcp_non_sq_lock); 4513 if (tcp->tcp_flow_stopped) 4514 tcp_clrqfull(tcp); 4515 mutex_exit(&tcp->tcp_non_sq_lock); 4516 4517 tcp_bind_hash_remove(tcp); 4518 /* 4519 * If the tcp_time_wait_collector (which runs outside the squeue) 4520 * is trying to remove this tcp from the time wait list, we will 4521 * block in tcp_time_wait_remove while trying to acquire the 4522 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4523 * requires the ipcl_hash_remove to be ordered after the 4524 * tcp_time_wait_remove for the refcnt checks to work correctly. 4525 */ 4526 if (tcp->tcp_state == TCPS_TIME_WAIT) 4527 (void) tcp_time_wait_remove(tcp, NULL); 4528 CL_INET_DISCONNECT(tcp); 4529 ipcl_hash_remove(connp); 4530 4531 /* 4532 * Delete the cached ire in conn_ire_cache and also mark 4533 * the conn as CONDEMNED 4534 */ 4535 mutex_enter(&connp->conn_lock); 4536 connp->conn_state_flags |= CONN_CONDEMNED; 4537 ire = connp->conn_ire_cache; 4538 connp->conn_ire_cache = NULL; 4539 mutex_exit(&connp->conn_lock); 4540 if (ire != NULL) 4541 IRE_REFRELE_NOTR(ire); 4542 4543 /* Need to cleanup any pending ioctls */ 4544 ASSERT(tcp->tcp_time_wait_next == NULL); 4545 ASSERT(tcp->tcp_time_wait_prev == NULL); 4546 ASSERT(tcp->tcp_time_wait_expire == 0); 4547 tcp->tcp_state = TCPS_CLOSED; 4548 4549 /* Release any SSL context */ 4550 if (tcp->tcp_kssl_ent != NULL) { 4551 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4552 tcp->tcp_kssl_ent = NULL; 4553 } 4554 if (tcp->tcp_kssl_ctx != NULL) { 4555 kssl_release_ctx(tcp->tcp_kssl_ctx); 4556 tcp->tcp_kssl_ctx = NULL; 4557 } 4558 tcp->tcp_kssl_pending = B_FALSE; 4559 4560 tcp_ipsec_cleanup(tcp); 4561 } 4562 4563 /* 4564 * tcp is dying (called from ipcl_conn_destroy and error cases). 4565 * Free the tcp_t in either case. 4566 */ 4567 void 4568 tcp_free(tcp_t *tcp) 4569 { 4570 mblk_t *mp; 4571 ip6_pkt_t *ipp; 4572 4573 ASSERT(tcp != NULL); 4574 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4575 4576 tcp->tcp_rq = NULL; 4577 tcp->tcp_wq = NULL; 4578 4579 tcp_close_mpp(&tcp->tcp_xmit_head); 4580 tcp_close_mpp(&tcp->tcp_reass_head); 4581 if (tcp->tcp_rcv_list != NULL) { 4582 /* Free b_next chain */ 4583 tcp_close_mpp(&tcp->tcp_rcv_list); 4584 } 4585 if ((mp = tcp->tcp_urp_mp) != NULL) { 4586 freemsg(mp); 4587 } 4588 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4589 freemsg(mp); 4590 } 4591 4592 if (tcp->tcp_fused_sigurg_mp != NULL) { 4593 freeb(tcp->tcp_fused_sigurg_mp); 4594 tcp->tcp_fused_sigurg_mp = NULL; 4595 } 4596 4597 if (tcp->tcp_sack_info != NULL) { 4598 if (tcp->tcp_notsack_list != NULL) { 4599 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4600 } 4601 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4602 } 4603 4604 if (tcp->tcp_hopopts != NULL) { 4605 mi_free(tcp->tcp_hopopts); 4606 tcp->tcp_hopopts = NULL; 4607 tcp->tcp_hopoptslen = 0; 4608 } 4609 ASSERT(tcp->tcp_hopoptslen == 0); 4610 if (tcp->tcp_dstopts != NULL) { 4611 mi_free(tcp->tcp_dstopts); 4612 tcp->tcp_dstopts = NULL; 4613 tcp->tcp_dstoptslen = 0; 4614 } 4615 ASSERT(tcp->tcp_dstoptslen == 0); 4616 if (tcp->tcp_rtdstopts != NULL) { 4617 mi_free(tcp->tcp_rtdstopts); 4618 tcp->tcp_rtdstopts = NULL; 4619 tcp->tcp_rtdstoptslen = 0; 4620 } 4621 ASSERT(tcp->tcp_rtdstoptslen == 0); 4622 if (tcp->tcp_rthdr != NULL) { 4623 mi_free(tcp->tcp_rthdr); 4624 tcp->tcp_rthdr = NULL; 4625 tcp->tcp_rthdrlen = 0; 4626 } 4627 ASSERT(tcp->tcp_rthdrlen == 0); 4628 4629 ipp = &tcp->tcp_sticky_ipp; 4630 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4631 IPPF_RTHDR)) 4632 ip6_pkt_free(ipp); 4633 4634 /* 4635 * Free memory associated with the tcp/ip header template. 4636 */ 4637 4638 if (tcp->tcp_iphc != NULL) 4639 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4640 4641 /* 4642 * Following is really a blowing away a union. 4643 * It happens to have exactly two members of identical size 4644 * the following code is enough. 4645 */ 4646 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4647 4648 if (tcp->tcp_tracebuf != NULL) { 4649 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4650 tcp->tcp_tracebuf = NULL; 4651 } 4652 } 4653 4654 4655 /* 4656 * Put a connection confirmation message upstream built from the 4657 * address information within 'iph' and 'tcph'. Report our success or failure. 4658 */ 4659 static boolean_t 4660 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4661 mblk_t **defermp) 4662 { 4663 sin_t sin; 4664 sin6_t sin6; 4665 mblk_t *mp; 4666 char *optp = NULL; 4667 int optlen = 0; 4668 cred_t *cr; 4669 4670 if (defermp != NULL) 4671 *defermp = NULL; 4672 4673 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4674 /* 4675 * Return in T_CONN_CON results of option negotiation through 4676 * the T_CONN_REQ. Note: If there is an real end-to-end option 4677 * negotiation, then what is received from remote end needs 4678 * to be taken into account but there is no such thing (yet?) 4679 * in our TCP/IP. 4680 * Note: We do not use mi_offset_param() here as 4681 * tcp_opts_conn_req contents do not directly come from 4682 * an application and are either generated in kernel or 4683 * from user input that was already verified. 4684 */ 4685 mp = tcp->tcp_conn.tcp_opts_conn_req; 4686 optp = (char *)(mp->b_rptr + 4687 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4688 optlen = (int) 4689 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4690 } 4691 4692 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4693 ipha_t *ipha = (ipha_t *)iphdr; 4694 4695 /* packet is IPv4 */ 4696 if (tcp->tcp_family == AF_INET) { 4697 sin = sin_null; 4698 sin.sin_addr.s_addr = ipha->ipha_src; 4699 sin.sin_port = *(uint16_t *)tcph->th_lport; 4700 sin.sin_family = AF_INET; 4701 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4702 (int)sizeof (sin_t), optp, optlen); 4703 } else { 4704 sin6 = sin6_null; 4705 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4706 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4707 sin6.sin6_family = AF_INET6; 4708 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4709 (int)sizeof (sin6_t), optp, optlen); 4710 4711 } 4712 } else { 4713 ip6_t *ip6h = (ip6_t *)iphdr; 4714 4715 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4716 ASSERT(tcp->tcp_family == AF_INET6); 4717 sin6 = sin6_null; 4718 sin6.sin6_addr = ip6h->ip6_src; 4719 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4720 sin6.sin6_family = AF_INET6; 4721 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4722 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4723 (int)sizeof (sin6_t), optp, optlen); 4724 } 4725 4726 if (!mp) 4727 return (B_FALSE); 4728 4729 if ((cr = DB_CRED(idmp)) != NULL) { 4730 mblk_setcred(mp, cr); 4731 DB_CPID(mp) = DB_CPID(idmp); 4732 } 4733 4734 if (defermp == NULL) 4735 putnext(tcp->tcp_rq, mp); 4736 else 4737 *defermp = mp; 4738 4739 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4740 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4741 return (B_TRUE); 4742 } 4743 4744 /* 4745 * Defense for the SYN attack - 4746 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4747 * one from the list of droppable eagers. This list is a subset of q0. 4748 * see comments before the definition of MAKE_DROPPABLE(). 4749 * 2. Don't drop a SYN request before its first timeout. This gives every 4750 * request at least til the first timeout to complete its 3-way handshake. 4751 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4752 * requests currently on the queue that has timed out. This will be used 4753 * as an indicator of whether an attack is under way, so that appropriate 4754 * actions can be taken. (It's incremented in tcp_timer() and decremented 4755 * either when eager goes into ESTABLISHED, or gets freed up.) 4756 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4757 * # of timeout drops back to <= q0len/32 => SYN alert off 4758 */ 4759 static boolean_t 4760 tcp_drop_q0(tcp_t *tcp) 4761 { 4762 tcp_t *eager; 4763 mblk_t *mp; 4764 tcp_stack_t *tcps = tcp->tcp_tcps; 4765 4766 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4767 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4768 4769 /* Pick oldest eager from the list of droppable eagers */ 4770 eager = tcp->tcp_eager_prev_drop_q0; 4771 4772 /* If list is empty. return B_FALSE */ 4773 if (eager == tcp) { 4774 return (B_FALSE); 4775 } 4776 4777 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4778 if ((mp = allocb(0, BPRI_HI)) == NULL) 4779 return (B_FALSE); 4780 4781 /* 4782 * Take this eager out from the list of droppable eagers since we are 4783 * going to drop it. 4784 */ 4785 MAKE_UNDROPPABLE(eager); 4786 4787 if (tcp->tcp_debug) { 4788 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4789 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4790 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4791 tcp->tcp_conn_req_cnt_q0, 4792 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4793 } 4794 4795 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4796 4797 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4798 CONN_INC_REF(eager->tcp_connp); 4799 4800 /* Mark the IRE created for this SYN request temporary */ 4801 tcp_ip_ire_mark_advice(eager); 4802 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4803 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4804 4805 return (B_TRUE); 4806 } 4807 4808 int 4809 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4810 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4811 { 4812 tcp_t *ltcp = lconnp->conn_tcp; 4813 tcp_t *tcp = connp->conn_tcp; 4814 mblk_t *tpi_mp; 4815 ipha_t *ipha; 4816 ip6_t *ip6h; 4817 sin6_t sin6; 4818 in6_addr_t v6dst; 4819 int err; 4820 int ifindex = 0; 4821 cred_t *cr; 4822 tcp_stack_t *tcps = tcp->tcp_tcps; 4823 4824 if (ipvers == IPV4_VERSION) { 4825 ipha = (ipha_t *)mp->b_rptr; 4826 4827 connp->conn_send = ip_output; 4828 connp->conn_recv = tcp_input; 4829 4830 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4831 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4832 4833 sin6 = sin6_null; 4834 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4835 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4836 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4837 sin6.sin6_family = AF_INET6; 4838 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4839 lconnp->conn_zoneid, tcps->tcps_netstack); 4840 if (tcp->tcp_recvdstaddr) { 4841 sin6_t sin6d; 4842 4843 sin6d = sin6_null; 4844 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4845 &sin6d.sin6_addr); 4846 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4847 sin6d.sin6_family = AF_INET; 4848 tpi_mp = mi_tpi_extconn_ind(NULL, 4849 (char *)&sin6d, sizeof (sin6_t), 4850 (char *)&tcp, 4851 (t_scalar_t)sizeof (intptr_t), 4852 (char *)&sin6d, sizeof (sin6_t), 4853 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4854 } else { 4855 tpi_mp = mi_tpi_conn_ind(NULL, 4856 (char *)&sin6, sizeof (sin6_t), 4857 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4858 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4859 } 4860 } else { 4861 ip6h = (ip6_t *)mp->b_rptr; 4862 4863 connp->conn_send = ip_output_v6; 4864 connp->conn_recv = tcp_input; 4865 4866 connp->conn_srcv6 = ip6h->ip6_dst; 4867 connp->conn_remv6 = ip6h->ip6_src; 4868 4869 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4870 ifindex = (int)DB_CKSUMSTUFF(mp); 4871 DB_CKSUMSTUFF(mp) = 0; 4872 4873 sin6 = sin6_null; 4874 sin6.sin6_addr = ip6h->ip6_src; 4875 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4876 sin6.sin6_family = AF_INET6; 4877 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4878 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4879 lconnp->conn_zoneid, tcps->tcps_netstack); 4880 4881 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4882 /* Pass up the scope_id of remote addr */ 4883 sin6.sin6_scope_id = ifindex; 4884 } else { 4885 sin6.sin6_scope_id = 0; 4886 } 4887 if (tcp->tcp_recvdstaddr) { 4888 sin6_t sin6d; 4889 4890 sin6d = sin6_null; 4891 sin6.sin6_addr = ip6h->ip6_dst; 4892 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4893 sin6d.sin6_family = AF_INET; 4894 tpi_mp = mi_tpi_extconn_ind(NULL, 4895 (char *)&sin6d, sizeof (sin6_t), 4896 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4897 (char *)&sin6d, sizeof (sin6_t), 4898 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4899 } else { 4900 tpi_mp = mi_tpi_conn_ind(NULL, 4901 (char *)&sin6, sizeof (sin6_t), 4902 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4903 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4904 } 4905 } 4906 4907 if (tpi_mp == NULL) 4908 return (ENOMEM); 4909 4910 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4911 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4912 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4913 connp->conn_fully_bound = B_FALSE; 4914 4915 if (tcps->tcps_trace) 4916 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4917 4918 /* Inherit information from the "parent" */ 4919 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4920 tcp->tcp_family = ltcp->tcp_family; 4921 tcp->tcp_wq = ltcp->tcp_wq; 4922 tcp->tcp_rq = ltcp->tcp_rq; 4923 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4924 tcp->tcp_detached = B_TRUE; 4925 if ((err = tcp_init_values(tcp)) != 0) { 4926 freemsg(tpi_mp); 4927 return (err); 4928 } 4929 4930 if (ipvers == IPV4_VERSION) { 4931 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4932 freemsg(tpi_mp); 4933 return (err); 4934 } 4935 ASSERT(tcp->tcp_ipha != NULL); 4936 } else { 4937 /* ifindex must be already set */ 4938 ASSERT(ifindex != 0); 4939 4940 if (ltcp->tcp_bound_if != 0) { 4941 /* 4942 * Set newtcp's bound_if equal to 4943 * listener's value. If ifindex is 4944 * not the same as ltcp->tcp_bound_if, 4945 * it must be a packet for the ipmp group 4946 * of interfaces 4947 */ 4948 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4949 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4950 tcp->tcp_bound_if = ifindex; 4951 } 4952 4953 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4954 tcp->tcp_recvifindex = 0; 4955 tcp->tcp_recvhops = 0xffffffffU; 4956 ASSERT(tcp->tcp_ip6h != NULL); 4957 } 4958 4959 tcp->tcp_lport = ltcp->tcp_lport; 4960 4961 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4962 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4963 /* 4964 * Listener had options of some sort; eager inherits. 4965 * Free up the eager template and allocate one 4966 * of the right size. 4967 */ 4968 if (tcp->tcp_hdr_grown) { 4969 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4970 } else { 4971 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4972 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4973 } 4974 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4975 KM_NOSLEEP); 4976 if (tcp->tcp_iphc == NULL) { 4977 tcp->tcp_iphc_len = 0; 4978 freemsg(tpi_mp); 4979 return (ENOMEM); 4980 } 4981 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4982 tcp->tcp_hdr_grown = B_TRUE; 4983 } 4984 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4985 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4986 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4987 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4988 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4989 4990 /* 4991 * Copy the IP+TCP header template from listener to eager 4992 */ 4993 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4994 if (tcp->tcp_ipversion == IPV6_VERSION) { 4995 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4996 IPPROTO_RAW) { 4997 tcp->tcp_ip6h = 4998 (ip6_t *)(tcp->tcp_iphc + 4999 sizeof (ip6i_t)); 5000 } else { 5001 tcp->tcp_ip6h = 5002 (ip6_t *)(tcp->tcp_iphc); 5003 } 5004 tcp->tcp_ipha = NULL; 5005 } else { 5006 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5007 tcp->tcp_ip6h = NULL; 5008 } 5009 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5010 tcp->tcp_ip_hdr_len); 5011 } else { 5012 /* 5013 * only valid case when ipversion of listener and 5014 * eager differ is when listener is IPv6 and 5015 * eager is IPv4. 5016 * Eager header template has been initialized to the 5017 * maximum v4 header sizes, which includes space for 5018 * TCP and IP options. 5019 */ 5020 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5021 (tcp->tcp_ipversion == IPV4_VERSION)); 5022 ASSERT(tcp->tcp_iphc_len >= 5023 TCP_MAX_COMBINED_HEADER_LENGTH); 5024 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5025 /* copy IP header fields individually */ 5026 tcp->tcp_ipha->ipha_ttl = 5027 ltcp->tcp_ip6h->ip6_hops; 5028 bcopy(ltcp->tcp_tcph->th_lport, 5029 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5030 } 5031 5032 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5033 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5034 sizeof (in_port_t)); 5035 5036 if (ltcp->tcp_lport == 0) { 5037 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5038 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5039 sizeof (in_port_t)); 5040 } 5041 5042 if (tcp->tcp_ipversion == IPV4_VERSION) { 5043 ASSERT(ipha != NULL); 5044 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5045 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5046 5047 /* Source routing option copyover (reverse it) */ 5048 if (tcps->tcps_rev_src_routes) 5049 tcp_opt_reverse(tcp, ipha); 5050 } else { 5051 ASSERT(ip6h != NULL); 5052 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5053 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5054 } 5055 5056 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5057 ASSERT(!tcp->tcp_tconnind_started); 5058 /* 5059 * If the SYN contains a credential, it's a loopback packet; attach 5060 * the credential to the TPI message. 5061 */ 5062 if ((cr = DB_CRED(idmp)) != NULL) { 5063 mblk_setcred(tpi_mp, cr); 5064 DB_CPID(tpi_mp) = DB_CPID(idmp); 5065 } 5066 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5067 5068 /* Inherit the listener's SSL protection state */ 5069 5070 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5071 kssl_hold_ent(tcp->tcp_kssl_ent); 5072 tcp->tcp_kssl_pending = B_TRUE; 5073 } 5074 5075 return (0); 5076 } 5077 5078 5079 int 5080 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5081 tcph_t *tcph, mblk_t *idmp) 5082 { 5083 tcp_t *ltcp = lconnp->conn_tcp; 5084 tcp_t *tcp = connp->conn_tcp; 5085 sin_t sin; 5086 mblk_t *tpi_mp = NULL; 5087 int err; 5088 cred_t *cr; 5089 tcp_stack_t *tcps = tcp->tcp_tcps; 5090 5091 sin = sin_null; 5092 sin.sin_addr.s_addr = ipha->ipha_src; 5093 sin.sin_port = *(uint16_t *)tcph->th_lport; 5094 sin.sin_family = AF_INET; 5095 if (ltcp->tcp_recvdstaddr) { 5096 sin_t sind; 5097 5098 sind = sin_null; 5099 sind.sin_addr.s_addr = ipha->ipha_dst; 5100 sind.sin_port = *(uint16_t *)tcph->th_fport; 5101 sind.sin_family = AF_INET; 5102 tpi_mp = mi_tpi_extconn_ind(NULL, 5103 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5104 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5105 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5106 } else { 5107 tpi_mp = mi_tpi_conn_ind(NULL, 5108 (char *)&sin, sizeof (sin_t), 5109 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5110 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5111 } 5112 5113 if (tpi_mp == NULL) { 5114 return (ENOMEM); 5115 } 5116 5117 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5118 connp->conn_send = ip_output; 5119 connp->conn_recv = tcp_input; 5120 connp->conn_fully_bound = B_FALSE; 5121 5122 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5123 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5124 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5125 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5126 5127 if (tcps->tcps_trace) { 5128 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 5129 } 5130 5131 /* Inherit information from the "parent" */ 5132 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5133 tcp->tcp_family = ltcp->tcp_family; 5134 tcp->tcp_wq = ltcp->tcp_wq; 5135 tcp->tcp_rq = ltcp->tcp_rq; 5136 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 5137 tcp->tcp_detached = B_TRUE; 5138 if ((err = tcp_init_values(tcp)) != 0) { 5139 freemsg(tpi_mp); 5140 return (err); 5141 } 5142 5143 /* 5144 * Let's make sure that eager tcp template has enough space to 5145 * copy IPv4 listener's tcp template. Since the conn_t structure is 5146 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5147 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5148 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5149 * extension headers or with ip6i_t struct). Note that bcopy() below 5150 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5151 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5152 */ 5153 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5154 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5155 5156 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5157 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5158 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5159 tcp->tcp_ttl = ltcp->tcp_ttl; 5160 tcp->tcp_tos = ltcp->tcp_tos; 5161 5162 /* Copy the IP+TCP header template from listener to eager */ 5163 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5164 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5165 tcp->tcp_ip6h = NULL; 5166 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5167 tcp->tcp_ip_hdr_len); 5168 5169 /* Initialize the IP addresses and Ports */ 5170 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5171 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5172 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5173 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5174 5175 /* Source routing option copyover (reverse it) */ 5176 if (tcps->tcps_rev_src_routes) 5177 tcp_opt_reverse(tcp, ipha); 5178 5179 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5180 ASSERT(!tcp->tcp_tconnind_started); 5181 5182 /* 5183 * If the SYN contains a credential, it's a loopback packet; attach 5184 * the credential to the TPI message. 5185 */ 5186 if ((cr = DB_CRED(idmp)) != NULL) { 5187 mblk_setcred(tpi_mp, cr); 5188 DB_CPID(tpi_mp) = DB_CPID(idmp); 5189 } 5190 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5191 5192 /* Inherit the listener's SSL protection state */ 5193 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5194 kssl_hold_ent(tcp->tcp_kssl_ent); 5195 tcp->tcp_kssl_pending = B_TRUE; 5196 } 5197 5198 return (0); 5199 } 5200 5201 /* 5202 * sets up conn for ipsec. 5203 * if the first mblk is M_CTL it is consumed and mpp is updated. 5204 * in case of error mpp is freed. 5205 */ 5206 conn_t * 5207 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5208 { 5209 conn_t *connp = tcp->tcp_connp; 5210 conn_t *econnp; 5211 squeue_t *new_sqp; 5212 mblk_t *first_mp = *mpp; 5213 mblk_t *mp = *mpp; 5214 boolean_t mctl_present = B_FALSE; 5215 uint_t ipvers; 5216 5217 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 5218 if (econnp == NULL) { 5219 freemsg(first_mp); 5220 return (NULL); 5221 } 5222 if (DB_TYPE(mp) == M_CTL) { 5223 if (mp->b_cont == NULL || 5224 mp->b_cont->b_datap->db_type != M_DATA) { 5225 freemsg(first_mp); 5226 return (NULL); 5227 } 5228 mp = mp->b_cont; 5229 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5230 freemsg(first_mp); 5231 return (NULL); 5232 } 5233 5234 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5235 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5236 mctl_present = B_TRUE; 5237 } else { 5238 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5239 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5240 } 5241 5242 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5243 DB_CKSUMSTART(mp) = 0; 5244 5245 ASSERT(OK_32PTR(mp->b_rptr)); 5246 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5247 if (ipvers == IPV4_VERSION) { 5248 uint16_t *up; 5249 uint32_t ports; 5250 ipha_t *ipha; 5251 5252 ipha = (ipha_t *)mp->b_rptr; 5253 up = (uint16_t *)((uchar_t *)ipha + 5254 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5255 ports = *(uint32_t *)up; 5256 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5257 ipha->ipha_dst, ipha->ipha_src, ports); 5258 } else { 5259 uint16_t *up; 5260 uint32_t ports; 5261 uint16_t ip_hdr_len; 5262 uint8_t *nexthdrp; 5263 ip6_t *ip6h; 5264 tcph_t *tcph; 5265 5266 ip6h = (ip6_t *)mp->b_rptr; 5267 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5268 ip_hdr_len = IPV6_HDR_LEN; 5269 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5270 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5271 CONN_DEC_REF(econnp); 5272 freemsg(first_mp); 5273 return (NULL); 5274 } 5275 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5276 up = (uint16_t *)tcph->th_lport; 5277 ports = *(uint32_t *)up; 5278 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5279 ip6h->ip6_dst, ip6h->ip6_src, ports); 5280 } 5281 5282 /* 5283 * The caller already ensured that there is a sqp present. 5284 */ 5285 econnp->conn_sqp = new_sqp; 5286 5287 if (connp->conn_policy != NULL) { 5288 ipsec_in_t *ii; 5289 ii = (ipsec_in_t *)(first_mp->b_rptr); 5290 ASSERT(ii->ipsec_in_policy == NULL); 5291 IPPH_REFHOLD(connp->conn_policy); 5292 ii->ipsec_in_policy = connp->conn_policy; 5293 5294 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5295 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5296 CONN_DEC_REF(econnp); 5297 freemsg(first_mp); 5298 return (NULL); 5299 } 5300 } 5301 5302 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5303 CONN_DEC_REF(econnp); 5304 freemsg(first_mp); 5305 return (NULL); 5306 } 5307 5308 /* 5309 * If we know we have some policy, pass the "IPSEC" 5310 * options size TCP uses this adjust the MSS. 5311 */ 5312 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5313 if (mctl_present) { 5314 freeb(first_mp); 5315 *mpp = mp; 5316 } 5317 5318 return (econnp); 5319 } 5320 5321 /* 5322 * tcp_get_conn/tcp_free_conn 5323 * 5324 * tcp_get_conn is used to get a clean tcp connection structure. 5325 * It tries to reuse the connections put on the freelist by the 5326 * time_wait_collector failing which it goes to kmem_cache. This 5327 * way has two benefits compared to just allocating from and 5328 * freeing to kmem_cache. 5329 * 1) The time_wait_collector can free (which includes the cleanup) 5330 * outside the squeue. So when the interrupt comes, we have a clean 5331 * connection sitting in the freelist. Obviously, this buys us 5332 * performance. 5333 * 5334 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5335 * has multiple disadvantages - tying up the squeue during alloc, and the 5336 * fact that IPSec policy initialization has to happen here which 5337 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5338 * But allocating the conn/tcp in IP land is also not the best since 5339 * we can't check the 'q' and 'q0' which are protected by squeue and 5340 * blindly allocate memory which might have to be freed here if we are 5341 * not allowed to accept the connection. By using the freelist and 5342 * putting the conn/tcp back in freelist, we don't pay a penalty for 5343 * allocating memory without checking 'q/q0' and freeing it if we can't 5344 * accept the connection. 5345 * 5346 * Care should be taken to put the conn back in the same squeue's freelist 5347 * from which it was allocated. Best results are obtained if conn is 5348 * allocated from listener's squeue and freed to the same. Time wait 5349 * collector will free up the freelist is the connection ends up sitting 5350 * there for too long. 5351 */ 5352 void * 5353 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5354 { 5355 tcp_t *tcp = NULL; 5356 conn_t *connp = NULL; 5357 squeue_t *sqp = (squeue_t *)arg; 5358 tcp_squeue_priv_t *tcp_time_wait; 5359 netstack_t *ns; 5360 5361 tcp_time_wait = 5362 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5363 5364 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5365 tcp = tcp_time_wait->tcp_free_list; 5366 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5367 if (tcp != NULL) { 5368 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5369 tcp_time_wait->tcp_free_list_cnt--; 5370 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5371 tcp->tcp_time_wait_next = NULL; 5372 connp = tcp->tcp_connp; 5373 connp->conn_flags |= IPCL_REUSED; 5374 5375 ASSERT(tcp->tcp_tcps == NULL); 5376 ASSERT(connp->conn_netstack == NULL); 5377 ns = tcps->tcps_netstack; 5378 netstack_hold(ns); 5379 connp->conn_netstack = ns; 5380 tcp->tcp_tcps = tcps; 5381 TCPS_REFHOLD(tcps); 5382 ipcl_globalhash_insert(connp); 5383 return ((void *)connp); 5384 } 5385 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5386 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5387 tcps->tcps_netstack)) == NULL) 5388 return (NULL); 5389 tcp = connp->conn_tcp; 5390 tcp->tcp_tcps = tcps; 5391 TCPS_REFHOLD(tcps); 5392 return ((void *)connp); 5393 } 5394 5395 /* 5396 * Update the cached label for the given tcp_t. This should be called once per 5397 * connection, and before any packets are sent or tcp_process_options is 5398 * invoked. Returns B_FALSE if the correct label could not be constructed. 5399 */ 5400 static boolean_t 5401 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5402 { 5403 conn_t *connp = tcp->tcp_connp; 5404 5405 if (tcp->tcp_ipversion == IPV4_VERSION) { 5406 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5407 int added; 5408 5409 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5410 connp->conn_mac_exempt, 5411 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5412 return (B_FALSE); 5413 5414 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5415 if (added == -1) 5416 return (B_FALSE); 5417 tcp->tcp_hdr_len += added; 5418 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5419 tcp->tcp_ip_hdr_len += added; 5420 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5421 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5422 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5423 tcp->tcp_hdr_len); 5424 if (added == -1) 5425 return (B_FALSE); 5426 tcp->tcp_hdr_len += added; 5427 tcp->tcp_tcph = (tcph_t *) 5428 ((uchar_t *)tcp->tcp_tcph + added); 5429 tcp->tcp_ip_hdr_len += added; 5430 } 5431 } else { 5432 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5433 5434 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5435 connp->conn_mac_exempt, 5436 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5437 return (B_FALSE); 5438 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5439 &tcp->tcp_label_len, optbuf) != 0) 5440 return (B_FALSE); 5441 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5442 return (B_FALSE); 5443 } 5444 5445 connp->conn_ulp_labeled = 1; 5446 5447 return (B_TRUE); 5448 } 5449 5450 /* BEGIN CSTYLED */ 5451 /* 5452 * 5453 * The sockfs ACCEPT path: 5454 * ======================= 5455 * 5456 * The eager is now established in its own perimeter as soon as SYN is 5457 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5458 * completes the accept processing on the acceptor STREAM. The sending 5459 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5460 * listener but a TLI/XTI listener completes the accept processing 5461 * on the listener perimeter. 5462 * 5463 * Common control flow for 3 way handshake: 5464 * ---------------------------------------- 5465 * 5466 * incoming SYN (listener perimeter) -> tcp_rput_data() 5467 * -> tcp_conn_request() 5468 * 5469 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5470 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5471 * 5472 * Sockfs ACCEPT Path: 5473 * ------------------- 5474 * 5475 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5476 * as STREAM entry point) 5477 * 5478 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5479 * 5480 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5481 * association (we are not behind eager's squeue but sockfs is protecting us 5482 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5483 * is changed to point at tcp_wput(). 5484 * 5485 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5486 * listener (done on listener's perimeter). 5487 * 5488 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5489 * accept. 5490 * 5491 * TLI/XTI client ACCEPT path: 5492 * --------------------------- 5493 * 5494 * soaccept() sends T_CONN_RES on the listener STREAM. 5495 * 5496 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5497 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5498 * 5499 * Locks: 5500 * ====== 5501 * 5502 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5503 * and listeners->tcp_eager_next_q. 5504 * 5505 * Referencing: 5506 * ============ 5507 * 5508 * 1) We start out in tcp_conn_request by eager placing a ref on 5509 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5510 * 5511 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5512 * doing so we place a ref on the eager. This ref is finally dropped at the 5513 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5514 * reference is dropped by the squeue framework. 5515 * 5516 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5517 * 5518 * The reference must be released by the same entity that added the reference 5519 * In the above scheme, the eager is the entity that adds and releases the 5520 * references. Note that tcp_accept_finish executes in the squeue of the eager 5521 * (albeit after it is attached to the acceptor stream). Though 1. executes 5522 * in the listener's squeue, the eager is nascent at this point and the 5523 * reference can be considered to have been added on behalf of the eager. 5524 * 5525 * Eager getting a Reset or listener closing: 5526 * ========================================== 5527 * 5528 * Once the listener and eager are linked, the listener never does the unlink. 5529 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5530 * a message on all eager perimeter. The eager then does the unlink, clears 5531 * any pointers to the listener's queue and drops the reference to the 5532 * listener. The listener waits in tcp_close outside the squeue until its 5533 * refcount has dropped to 1. This ensures that the listener has waited for 5534 * all eagers to clear their association with the listener. 5535 * 5536 * Similarly, if eager decides to go away, it can unlink itself and close. 5537 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5538 * the reference to eager is still valid because of the extra ref we put 5539 * in tcp_send_conn_ind. 5540 * 5541 * Listener can always locate the eager under the protection 5542 * of the listener->tcp_eager_lock, and then do a refhold 5543 * on the eager during the accept processing. 5544 * 5545 * The acceptor stream accesses the eager in the accept processing 5546 * based on the ref placed on eager before sending T_conn_ind. 5547 * The only entity that can negate this refhold is a listener close 5548 * which is mutually exclusive with an active acceptor stream. 5549 * 5550 * Eager's reference on the listener 5551 * =================================== 5552 * 5553 * If the accept happens (even on a closed eager) the eager drops its 5554 * reference on the listener at the start of tcp_accept_finish. If the 5555 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5556 * the reference is dropped in tcp_closei_local. If the listener closes, 5557 * the reference is dropped in tcp_eager_kill. In all cases the reference 5558 * is dropped while executing in the eager's context (squeue). 5559 */ 5560 /* END CSTYLED */ 5561 5562 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5563 5564 /* 5565 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5566 * tcp_rput_data will not see any SYN packets. 5567 */ 5568 /* ARGSUSED */ 5569 void 5570 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5571 { 5572 tcph_t *tcph; 5573 uint32_t seg_seq; 5574 tcp_t *eager; 5575 uint_t ipvers; 5576 ipha_t *ipha; 5577 ip6_t *ip6h; 5578 int err; 5579 conn_t *econnp = NULL; 5580 squeue_t *new_sqp; 5581 mblk_t *mp1; 5582 uint_t ip_hdr_len; 5583 conn_t *connp = (conn_t *)arg; 5584 tcp_t *tcp = connp->conn_tcp; 5585 ire_t *ire; 5586 cred_t *credp; 5587 tcp_stack_t *tcps = tcp->tcp_tcps; 5588 ip_stack_t *ipst; 5589 5590 if (tcp->tcp_state != TCPS_LISTEN) 5591 goto error2; 5592 5593 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5594 5595 mutex_enter(&tcp->tcp_eager_lock); 5596 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5597 mutex_exit(&tcp->tcp_eager_lock); 5598 TCP_STAT(tcps, tcp_listendrop); 5599 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5600 if (tcp->tcp_debug) { 5601 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5602 "tcp_conn_request: listen backlog (max=%d) " 5603 "overflow (%d pending) on %s", 5604 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5605 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5606 } 5607 goto error2; 5608 } 5609 5610 if (tcp->tcp_conn_req_cnt_q0 >= 5611 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5612 /* 5613 * Q0 is full. Drop a pending half-open req from the queue 5614 * to make room for the new SYN req. Also mark the time we 5615 * drop a SYN. 5616 * 5617 * A more aggressive defense against SYN attack will 5618 * be to set the "tcp_syn_defense" flag now. 5619 */ 5620 TCP_STAT(tcps, tcp_listendropq0); 5621 tcp->tcp_last_rcv_lbolt = lbolt64; 5622 if (!tcp_drop_q0(tcp)) { 5623 mutex_exit(&tcp->tcp_eager_lock); 5624 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5625 if (tcp->tcp_debug) { 5626 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5627 "tcp_conn_request: listen half-open queue " 5628 "(max=%d) full (%d pending) on %s", 5629 tcps->tcps_conn_req_max_q0, 5630 tcp->tcp_conn_req_cnt_q0, 5631 tcp_display(tcp, NULL, 5632 DISP_PORT_ONLY)); 5633 } 5634 goto error2; 5635 } 5636 } 5637 mutex_exit(&tcp->tcp_eager_lock); 5638 5639 /* 5640 * IP adds STRUIO_EAGER and ensures that the received packet is 5641 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5642 * link local address. If IPSec is enabled, db_struioflag has 5643 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5644 * otherwise an error case if neither of them is set. 5645 */ 5646 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5647 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5648 DB_CKSUMSTART(mp) = 0; 5649 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5650 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5651 if (econnp == NULL) 5652 goto error2; 5653 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5654 econnp->conn_sqp = new_sqp; 5655 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5656 /* 5657 * mp is updated in tcp_get_ipsec_conn(). 5658 */ 5659 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5660 if (econnp == NULL) { 5661 /* 5662 * mp freed by tcp_get_ipsec_conn. 5663 */ 5664 return; 5665 } 5666 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5667 } else { 5668 goto error2; 5669 } 5670 5671 ASSERT(DB_TYPE(mp) == M_DATA); 5672 5673 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5674 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5675 ASSERT(OK_32PTR(mp->b_rptr)); 5676 if (ipvers == IPV4_VERSION) { 5677 ipha = (ipha_t *)mp->b_rptr; 5678 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5679 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5680 } else { 5681 ip6h = (ip6_t *)mp->b_rptr; 5682 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5683 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5684 } 5685 5686 if (tcp->tcp_family == AF_INET) { 5687 ASSERT(ipvers == IPV4_VERSION); 5688 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5689 } else { 5690 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5691 } 5692 5693 if (err) 5694 goto error3; 5695 5696 eager = econnp->conn_tcp; 5697 5698 /* Inherit various TCP parameters from the listener */ 5699 eager->tcp_naglim = tcp->tcp_naglim; 5700 eager->tcp_first_timer_threshold = 5701 tcp->tcp_first_timer_threshold; 5702 eager->tcp_second_timer_threshold = 5703 tcp->tcp_second_timer_threshold; 5704 5705 eager->tcp_first_ctimer_threshold = 5706 tcp->tcp_first_ctimer_threshold; 5707 eager->tcp_second_ctimer_threshold = 5708 tcp->tcp_second_ctimer_threshold; 5709 5710 /* 5711 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5712 * If it does not, the eager's receive window will be set to the 5713 * listener's receive window later in this function. 5714 */ 5715 eager->tcp_rwnd = 0; 5716 5717 /* 5718 * Inherit listener's tcp_init_cwnd. Need to do this before 5719 * calling tcp_process_options() where tcp_mss_set() is called 5720 * to set the initial cwnd. 5721 */ 5722 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5723 5724 /* 5725 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5726 * zone id before the accept is completed in tcp_wput_accept(). 5727 */ 5728 econnp->conn_zoneid = connp->conn_zoneid; 5729 econnp->conn_allzones = connp->conn_allzones; 5730 5731 /* Copy nexthop information from listener to eager */ 5732 if (connp->conn_nexthop_set) { 5733 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5734 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5735 } 5736 5737 /* 5738 * TSOL: tsol_input_proc() needs the eager's cred before the 5739 * eager is accepted 5740 */ 5741 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5742 crhold(credp); 5743 5744 /* 5745 * If the caller has the process-wide flag set, then default to MAC 5746 * exempt mode. This allows read-down to unlabeled hosts. 5747 */ 5748 if (getpflags(NET_MAC_AWARE, credp) != 0) 5749 econnp->conn_mac_exempt = B_TRUE; 5750 5751 if (is_system_labeled()) { 5752 cred_t *cr; 5753 5754 if (connp->conn_mlp_type != mlptSingle) { 5755 cr = econnp->conn_peercred = DB_CRED(mp); 5756 if (cr != NULL) 5757 crhold(cr); 5758 else 5759 cr = econnp->conn_cred; 5760 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5761 econnp, cred_t *, cr) 5762 } else { 5763 cr = econnp->conn_cred; 5764 DTRACE_PROBE2(syn_accept, conn_t *, 5765 econnp, cred_t *, cr) 5766 } 5767 5768 if (!tcp_update_label(eager, cr)) { 5769 DTRACE_PROBE3( 5770 tx__ip__log__error__connrequest__tcp, 5771 char *, "eager connp(1) label on SYN mp(2) failed", 5772 conn_t *, econnp, mblk_t *, mp); 5773 goto error3; 5774 } 5775 } 5776 5777 eager->tcp_hard_binding = B_TRUE; 5778 5779 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5780 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5781 5782 CL_INET_CONNECT(eager); 5783 5784 /* 5785 * No need to check for multicast destination since ip will only pass 5786 * up multicasts to those that have expressed interest 5787 * TODO: what about rejecting broadcasts? 5788 * Also check that source is not a multicast or broadcast address. 5789 */ 5790 eager->tcp_state = TCPS_SYN_RCVD; 5791 5792 5793 /* 5794 * There should be no ire in the mp as we are being called after 5795 * receiving the SYN. 5796 */ 5797 ASSERT(tcp_ire_mp(mp) == NULL); 5798 5799 /* 5800 * Adapt our mss, ttl, ... according to information provided in IRE. 5801 */ 5802 5803 if (tcp_adapt_ire(eager, NULL) == 0) { 5804 /* Undo the bind_hash_insert */ 5805 tcp_bind_hash_remove(eager); 5806 goto error3; 5807 } 5808 5809 /* Process all TCP options. */ 5810 tcp_process_options(eager, tcph); 5811 5812 /* Is the other end ECN capable? */ 5813 if (tcps->tcps_ecn_permitted >= 1 && 5814 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5815 eager->tcp_ecn_ok = B_TRUE; 5816 } 5817 5818 /* 5819 * listener->tcp_rq->q_hiwat should be the default window size or a 5820 * window size changed via SO_RCVBUF option. First round up the 5821 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5822 * scale option value if needed. Call tcp_rwnd_set() to finish the 5823 * setting. 5824 * 5825 * Note if there is a rpipe metric associated with the remote host, 5826 * we should not inherit receive window size from listener. 5827 */ 5828 eager->tcp_rwnd = MSS_ROUNDUP( 5829 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5830 eager->tcp_rwnd), eager->tcp_mss); 5831 if (eager->tcp_snd_ws_ok) 5832 tcp_set_ws_value(eager); 5833 /* 5834 * Note that this is the only place tcp_rwnd_set() is called for 5835 * accepting a connection. We need to call it here instead of 5836 * after the 3-way handshake because we need to tell the other 5837 * side our rwnd in the SYN-ACK segment. 5838 */ 5839 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5840 5841 /* 5842 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5843 * via soaccept()->soinheritoptions() which essentially applies 5844 * all the listener options to the new STREAM. The options that we 5845 * need to take care of are: 5846 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5847 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5848 * SO_SNDBUF, SO_RCVBUF. 5849 * 5850 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5851 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5852 * tcp_maxpsz_set() gets called later from 5853 * tcp_accept_finish(), the option takes effect. 5854 * 5855 */ 5856 /* Set the TCP options */ 5857 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5858 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5859 eager->tcp_oobinline = tcp->tcp_oobinline; 5860 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5861 eager->tcp_broadcast = tcp->tcp_broadcast; 5862 eager->tcp_useloopback = tcp->tcp_useloopback; 5863 eager->tcp_dontroute = tcp->tcp_dontroute; 5864 eager->tcp_linger = tcp->tcp_linger; 5865 eager->tcp_lingertime = tcp->tcp_lingertime; 5866 if (tcp->tcp_ka_enabled) 5867 eager->tcp_ka_enabled = 1; 5868 5869 /* Set the IP options */ 5870 econnp->conn_broadcast = connp->conn_broadcast; 5871 econnp->conn_loopback = connp->conn_loopback; 5872 econnp->conn_dontroute = connp->conn_dontroute; 5873 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5874 5875 /* Put a ref on the listener for the eager. */ 5876 CONN_INC_REF(connp); 5877 mutex_enter(&tcp->tcp_eager_lock); 5878 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5879 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5880 tcp->tcp_eager_next_q0 = eager; 5881 eager->tcp_eager_prev_q0 = tcp; 5882 5883 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5884 eager->tcp_listener = tcp; 5885 eager->tcp_saved_listener = tcp; 5886 5887 /* 5888 * Tag this detached tcp vector for later retrieval 5889 * by our listener client in tcp_accept(). 5890 */ 5891 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5892 tcp->tcp_conn_req_cnt_q0++; 5893 if (++tcp->tcp_conn_req_seqnum == -1) { 5894 /* 5895 * -1 is "special" and defined in TPI as something 5896 * that should never be used in T_CONN_IND 5897 */ 5898 ++tcp->tcp_conn_req_seqnum; 5899 } 5900 mutex_exit(&tcp->tcp_eager_lock); 5901 5902 if (tcp->tcp_syn_defense) { 5903 /* Don't drop the SYN that comes from a good IP source */ 5904 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5905 if (addr_cache != NULL && eager->tcp_remote == 5906 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5907 eager->tcp_dontdrop = B_TRUE; 5908 } 5909 } 5910 5911 /* 5912 * We need to insert the eager in its own perimeter but as soon 5913 * as we do that, we expose the eager to the classifier and 5914 * should not touch any field outside the eager's perimeter. 5915 * So do all the work necessary before inserting the eager 5916 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5917 * will succeed but undo everything if it fails. 5918 */ 5919 seg_seq = ABE32_TO_U32(tcph->th_seq); 5920 eager->tcp_irs = seg_seq; 5921 eager->tcp_rack = seg_seq; 5922 eager->tcp_rnxt = seg_seq + 1; 5923 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5924 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5925 eager->tcp_state = TCPS_SYN_RCVD; 5926 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5927 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5928 if (mp1 == NULL) 5929 goto error1; 5930 DB_CPID(mp1) = tcp->tcp_cpid; 5931 eager->tcp_cpid = tcp->tcp_cpid; 5932 eager->tcp_open_time = lbolt64; 5933 5934 /* 5935 * We need to start the rto timer. In normal case, we start 5936 * the timer after sending the packet on the wire (or at 5937 * least believing that packet was sent by waiting for 5938 * CALL_IP_WPUT() to return). Since this is the first packet 5939 * being sent on the wire for the eager, our initial tcp_rto 5940 * is at least tcp_rexmit_interval_min which is a fairly 5941 * large value to allow the algorithm to adjust slowly to large 5942 * fluctuations of RTT during first few transmissions. 5943 * 5944 * Starting the timer first and then sending the packet in this 5945 * case shouldn't make much difference since tcp_rexmit_interval_min 5946 * is of the order of several 100ms and starting the timer 5947 * first and then sending the packet will result in difference 5948 * of few micro seconds. 5949 * 5950 * Without this optimization, we are forced to hold the fanout 5951 * lock across the ipcl_bind_insert() and sending the packet 5952 * so that we don't race against an incoming packet (maybe RST) 5953 * for this eager. 5954 * 5955 * It is necessary to acquire an extra reference on the eager 5956 * at this point and hold it until after tcp_send_data() to 5957 * ensure against an eager close race. 5958 */ 5959 5960 CONN_INC_REF(eager->tcp_connp); 5961 5962 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5963 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5964 5965 5966 /* 5967 * Insert the eager in its own perimeter now. We are ready to deal 5968 * with any packets on eager. 5969 */ 5970 if (eager->tcp_ipversion == IPV4_VERSION) { 5971 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5972 goto error; 5973 } 5974 } else { 5975 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5976 goto error; 5977 } 5978 } 5979 5980 /* mark conn as fully-bound */ 5981 econnp->conn_fully_bound = B_TRUE; 5982 5983 /* Send the SYN-ACK */ 5984 tcp_send_data(eager, eager->tcp_wq, mp1); 5985 CONN_DEC_REF(eager->tcp_connp); 5986 freemsg(mp); 5987 5988 return; 5989 error: 5990 (void) TCP_TIMER_CANCEL(eager, eager->tcp_timer_tid); 5991 CONN_DEC_REF(eager->tcp_connp); 5992 freemsg(mp1); 5993 error1: 5994 /* Undo what we did above */ 5995 mutex_enter(&tcp->tcp_eager_lock); 5996 tcp_eager_unlink(eager); 5997 mutex_exit(&tcp->tcp_eager_lock); 5998 /* Drop eager's reference on the listener */ 5999 CONN_DEC_REF(connp); 6000 6001 /* 6002 * Delete the cached ire in conn_ire_cache and also mark 6003 * the conn as CONDEMNED 6004 */ 6005 mutex_enter(&econnp->conn_lock); 6006 econnp->conn_state_flags |= CONN_CONDEMNED; 6007 ire = econnp->conn_ire_cache; 6008 econnp->conn_ire_cache = NULL; 6009 mutex_exit(&econnp->conn_lock); 6010 if (ire != NULL) 6011 IRE_REFRELE_NOTR(ire); 6012 6013 /* 6014 * tcp_accept_comm inserts the eager to the bind_hash 6015 * we need to remove it from the hash if ipcl_conn_insert 6016 * fails. 6017 */ 6018 tcp_bind_hash_remove(eager); 6019 /* Drop the eager ref placed in tcp_open_detached */ 6020 CONN_DEC_REF(econnp); 6021 6022 /* 6023 * If a connection already exists, send the mp to that connections so 6024 * that it can be appropriately dealt with. 6025 */ 6026 ipst = tcps->tcps_netstack->netstack_ip; 6027 6028 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 6029 if (!IPCL_IS_CONNECTED(econnp)) { 6030 /* 6031 * Something bad happened. ipcl_conn_insert() 6032 * failed because a connection already existed 6033 * in connected hash but we can't find it 6034 * anymore (someone blew it away). Just 6035 * free this message and hopefully remote 6036 * will retransmit at which time the SYN can be 6037 * treated as a new connection or dealth with 6038 * a TH_RST if a connection already exists. 6039 */ 6040 CONN_DEC_REF(econnp); 6041 freemsg(mp); 6042 } else { 6043 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6044 econnp, SQTAG_TCP_CONN_REQ); 6045 } 6046 } else { 6047 /* Nobody wants this packet */ 6048 freemsg(mp); 6049 } 6050 return; 6051 error2: 6052 freemsg(mp); 6053 return; 6054 error3: 6055 CONN_DEC_REF(econnp); 6056 freemsg(mp); 6057 } 6058 6059 /* 6060 * In an ideal case of vertical partition in NUMA architecture, its 6061 * beneficial to have the listener and all the incoming connections 6062 * tied to the same squeue. The other constraint is that incoming 6063 * connections should be tied to the squeue attached to interrupted 6064 * CPU for obvious locality reason so this leaves the listener to 6065 * be tied to the same squeue. Our only problem is that when listener 6066 * is binding, the CPU that will get interrupted by the NIC whose 6067 * IP address the listener is binding to is not even known. So 6068 * the code below allows us to change that binding at the time the 6069 * CPU is interrupted by virtue of incoming connection's squeue. 6070 * 6071 * This is usefull only in case of a listener bound to a specific IP 6072 * address. For other kind of listeners, they get bound the 6073 * very first time and there is no attempt to rebind them. 6074 */ 6075 void 6076 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6077 { 6078 conn_t *connp = (conn_t *)arg; 6079 squeue_t *sqp = (squeue_t *)arg2; 6080 squeue_t *new_sqp; 6081 uint32_t conn_flags; 6082 6083 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6084 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6085 } else { 6086 goto done; 6087 } 6088 6089 if (connp->conn_fanout == NULL) 6090 goto done; 6091 6092 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6093 mutex_enter(&connp->conn_fanout->connf_lock); 6094 mutex_enter(&connp->conn_lock); 6095 /* 6096 * No one from read or write side can access us now 6097 * except for already queued packets on this squeue. 6098 * But since we haven't changed the squeue yet, they 6099 * can't execute. If they are processed after we have 6100 * changed the squeue, they are sent back to the 6101 * correct squeue down below. 6102 * But a listner close can race with processing of 6103 * incoming SYN. If incoming SYN processing changes 6104 * the squeue then the listener close which is waiting 6105 * to enter the squeue would operate on the wrong 6106 * squeue. Hence we don't change the squeue here unless 6107 * the refcount is exactly the minimum refcount. The 6108 * minimum refcount of 4 is counted as - 1 each for 6109 * TCP and IP, 1 for being in the classifier hash, and 6110 * 1 for the mblk being processed. 6111 */ 6112 6113 if (connp->conn_ref != 4 || 6114 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6115 mutex_exit(&connp->conn_lock); 6116 mutex_exit(&connp->conn_fanout->connf_lock); 6117 goto done; 6118 } 6119 if (connp->conn_sqp != new_sqp) { 6120 while (connp->conn_sqp != new_sqp) 6121 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6122 } 6123 6124 do { 6125 conn_flags = connp->conn_flags; 6126 conn_flags |= IPCL_FULLY_BOUND; 6127 (void) cas32(&connp->conn_flags, connp->conn_flags, 6128 conn_flags); 6129 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6130 6131 mutex_exit(&connp->conn_fanout->connf_lock); 6132 mutex_exit(&connp->conn_lock); 6133 } 6134 6135 done: 6136 if (connp->conn_sqp != sqp) { 6137 CONN_INC_REF(connp); 6138 squeue_fill(connp->conn_sqp, mp, 6139 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6140 } else { 6141 tcp_conn_request(connp, mp, sqp); 6142 } 6143 } 6144 6145 /* 6146 * Successful connect request processing begins when our client passes 6147 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6148 * our T_OK_ACK reply message upstream. The control flow looks like this: 6149 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6150 * upstream <- tcp_rput() <- IP 6151 * After various error checks are completed, tcp_connect() lays 6152 * the target address and port into the composite header template, 6153 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6154 * request followed by an IRE request, and passes the three mblk message 6155 * down to IP looking like this: 6156 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6157 * Processing continues in tcp_rput() when we receive the following message: 6158 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6159 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6160 * to fire off the connection request, and then passes the T_OK_ACK mblk 6161 * upstream that we filled in below. There are, of course, numerous 6162 * error conditions along the way which truncate the processing described 6163 * above. 6164 */ 6165 static void 6166 tcp_connect(tcp_t *tcp, mblk_t *mp) 6167 { 6168 sin_t *sin; 6169 sin6_t *sin6; 6170 queue_t *q = tcp->tcp_wq; 6171 struct T_conn_req *tcr; 6172 ipaddr_t *dstaddrp; 6173 in_port_t dstport; 6174 uint_t srcid; 6175 6176 tcr = (struct T_conn_req *)mp->b_rptr; 6177 6178 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6179 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6180 tcp_err_ack(tcp, mp, TPROTO, 0); 6181 return; 6182 } 6183 6184 /* 6185 * Determine packet type based on type of address passed in 6186 * the request should contain an IPv4 or IPv6 address. 6187 * Make sure that address family matches the type of 6188 * family of the the address passed down 6189 */ 6190 switch (tcr->DEST_length) { 6191 default: 6192 tcp_err_ack(tcp, mp, TBADADDR, 0); 6193 return; 6194 6195 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6196 /* 6197 * XXX: The check for valid DEST_length was not there 6198 * in earlier releases and some buggy 6199 * TLI apps (e.g Sybase) got away with not feeding 6200 * in sin_zero part of address. 6201 * We allow that bug to keep those buggy apps humming. 6202 * Test suites require the check on DEST_length. 6203 * We construct a new mblk with valid DEST_length 6204 * free the original so the rest of the code does 6205 * not have to keep track of this special shorter 6206 * length address case. 6207 */ 6208 mblk_t *nmp; 6209 struct T_conn_req *ntcr; 6210 sin_t *nsin; 6211 6212 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6213 tcr->OPT_length, BPRI_HI); 6214 if (nmp == NULL) { 6215 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6216 return; 6217 } 6218 ntcr = (struct T_conn_req *)nmp->b_rptr; 6219 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6220 ntcr->PRIM_type = T_CONN_REQ; 6221 ntcr->DEST_length = sizeof (sin_t); 6222 ntcr->DEST_offset = sizeof (struct T_conn_req); 6223 6224 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6225 *nsin = sin_null; 6226 /* Get pointer to shorter address to copy from original mp */ 6227 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6228 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6229 if (sin == NULL || !OK_32PTR((char *)sin)) { 6230 freemsg(nmp); 6231 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6232 return; 6233 } 6234 nsin->sin_family = sin->sin_family; 6235 nsin->sin_port = sin->sin_port; 6236 nsin->sin_addr = sin->sin_addr; 6237 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6238 nmp->b_wptr = (uchar_t *)&nsin[1]; 6239 if (tcr->OPT_length != 0) { 6240 ntcr->OPT_length = tcr->OPT_length; 6241 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6242 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6243 (uchar_t *)ntcr + ntcr->OPT_offset, 6244 tcr->OPT_length); 6245 nmp->b_wptr += tcr->OPT_length; 6246 } 6247 freemsg(mp); /* original mp freed */ 6248 mp = nmp; /* re-initialize original variables */ 6249 tcr = ntcr; 6250 } 6251 /* FALLTHRU */ 6252 6253 case sizeof (sin_t): 6254 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6255 sizeof (sin_t)); 6256 if (sin == NULL || !OK_32PTR((char *)sin)) { 6257 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6258 return; 6259 } 6260 if (tcp->tcp_family != AF_INET || 6261 sin->sin_family != AF_INET) { 6262 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6263 return; 6264 } 6265 if (sin->sin_port == 0) { 6266 tcp_err_ack(tcp, mp, TBADADDR, 0); 6267 return; 6268 } 6269 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6270 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6271 return; 6272 } 6273 6274 break; 6275 6276 case sizeof (sin6_t): 6277 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6278 sizeof (sin6_t)); 6279 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6280 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6281 return; 6282 } 6283 if (tcp->tcp_family != AF_INET6 || 6284 sin6->sin6_family != AF_INET6) { 6285 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6286 return; 6287 } 6288 if (sin6->sin6_port == 0) { 6289 tcp_err_ack(tcp, mp, TBADADDR, 0); 6290 return; 6291 } 6292 break; 6293 } 6294 /* 6295 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6296 * should key on their sequence number and cut them loose. 6297 */ 6298 6299 /* 6300 * If options passed in, feed it for verification and handling 6301 */ 6302 if (tcr->OPT_length != 0) { 6303 mblk_t *ok_mp; 6304 mblk_t *discon_mp; 6305 mblk_t *conn_opts_mp; 6306 int t_error, sys_error, do_disconnect; 6307 6308 conn_opts_mp = NULL; 6309 6310 if (tcp_conprim_opt_process(tcp, mp, 6311 &do_disconnect, &t_error, &sys_error) < 0) { 6312 if (do_disconnect) { 6313 ASSERT(t_error == 0 && sys_error == 0); 6314 discon_mp = mi_tpi_discon_ind(NULL, 6315 ECONNREFUSED, 0); 6316 if (!discon_mp) { 6317 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6318 TSYSERR, ENOMEM); 6319 return; 6320 } 6321 ok_mp = mi_tpi_ok_ack_alloc(mp); 6322 if (!ok_mp) { 6323 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6324 TSYSERR, ENOMEM); 6325 return; 6326 } 6327 qreply(q, ok_mp); 6328 qreply(q, discon_mp); /* no flush! */ 6329 } else { 6330 ASSERT(t_error != 0); 6331 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6332 sys_error); 6333 } 6334 return; 6335 } 6336 /* 6337 * Success in setting options, the mp option buffer represented 6338 * by OPT_length/offset has been potentially modified and 6339 * contains results of option processing. We copy it in 6340 * another mp to save it for potentially influencing returning 6341 * it in T_CONN_CONN. 6342 */ 6343 if (tcr->OPT_length != 0) { /* there are resulting options */ 6344 conn_opts_mp = copyb(mp); 6345 if (!conn_opts_mp) { 6346 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6347 TSYSERR, ENOMEM); 6348 return; 6349 } 6350 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6351 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6352 /* 6353 * Note: 6354 * These resulting option negotiation can include any 6355 * end-to-end negotiation options but there no such 6356 * thing (yet?) in our TCP/IP. 6357 */ 6358 } 6359 } 6360 6361 /* 6362 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6363 * make sure that the template IP header in the tcp structure is an 6364 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6365 * need to this before we call tcp_bindi() so that the port lookup 6366 * code will look for ports in the correct port space (IPv4 and 6367 * IPv6 have separate port spaces). 6368 */ 6369 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6370 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6371 int err = 0; 6372 6373 err = tcp_header_init_ipv4(tcp); 6374 if (err != 0) { 6375 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6376 goto connect_failed; 6377 } 6378 if (tcp->tcp_lport != 0) 6379 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6380 } 6381 6382 switch (tcp->tcp_state) { 6383 case TCPS_IDLE: 6384 /* 6385 * We support quick connect, refer to comments in 6386 * tcp_connect_*() 6387 */ 6388 /* FALLTHRU */ 6389 case TCPS_BOUND: 6390 case TCPS_LISTEN: 6391 if (tcp->tcp_family == AF_INET6) { 6392 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6393 tcp_connect_ipv6(tcp, mp, 6394 &sin6->sin6_addr, 6395 sin6->sin6_port, sin6->sin6_flowinfo, 6396 sin6->__sin6_src_id, sin6->sin6_scope_id); 6397 return; 6398 } 6399 /* 6400 * Destination adress is mapped IPv6 address. 6401 * Source bound address should be unspecified or 6402 * IPv6 mapped address as well. 6403 */ 6404 if (!IN6_IS_ADDR_UNSPECIFIED( 6405 &tcp->tcp_bound_source_v6) && 6406 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6407 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6408 EADDRNOTAVAIL); 6409 break; 6410 } 6411 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6412 dstport = sin6->sin6_port; 6413 srcid = sin6->__sin6_src_id; 6414 } else { 6415 dstaddrp = &sin->sin_addr.s_addr; 6416 dstport = sin->sin_port; 6417 srcid = 0; 6418 } 6419 6420 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6421 return; 6422 default: 6423 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6424 break; 6425 } 6426 /* 6427 * Note: Code below is the "failure" case 6428 */ 6429 /* return error ack and blow away saved option results if any */ 6430 connect_failed: 6431 if (mp != NULL) 6432 putnext(tcp->tcp_rq, mp); 6433 else { 6434 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6435 TSYSERR, ENOMEM); 6436 } 6437 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6438 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6439 } 6440 6441 /* 6442 * Handle connect to IPv4 destinations, including connections for AF_INET6 6443 * sockets connecting to IPv4 mapped IPv6 destinations. 6444 */ 6445 static void 6446 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6447 uint_t srcid) 6448 { 6449 tcph_t *tcph; 6450 mblk_t *mp1; 6451 ipaddr_t dstaddr = *dstaddrp; 6452 int32_t oldstate; 6453 uint16_t lport; 6454 tcp_stack_t *tcps = tcp->tcp_tcps; 6455 6456 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6457 6458 /* Check for attempt to connect to INADDR_ANY */ 6459 if (dstaddr == INADDR_ANY) { 6460 /* 6461 * SunOS 4.x and 4.3 BSD allow an application 6462 * to connect a TCP socket to INADDR_ANY. 6463 * When they do this, the kernel picks the 6464 * address of one interface and uses it 6465 * instead. The kernel usually ends up 6466 * picking the address of the loopback 6467 * interface. This is an undocumented feature. 6468 * However, we provide the same thing here 6469 * in order to have source and binary 6470 * compatibility with SunOS 4.x. 6471 * Update the T_CONN_REQ (sin/sin6) since it is used to 6472 * generate the T_CONN_CON. 6473 */ 6474 dstaddr = htonl(INADDR_LOOPBACK); 6475 *dstaddrp = dstaddr; 6476 } 6477 6478 /* Handle __sin6_src_id if socket not bound to an IP address */ 6479 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6480 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6481 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6482 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6483 tcp->tcp_ipha->ipha_src); 6484 } 6485 6486 /* 6487 * Don't let an endpoint connect to itself. Note that 6488 * the test here does not catch the case where the 6489 * source IP addr was left unspecified by the user. In 6490 * this case, the source addr is set in tcp_adapt_ire() 6491 * using the reply to the T_BIND message that we send 6492 * down to IP here and the check is repeated in tcp_rput_other. 6493 */ 6494 if (dstaddr == tcp->tcp_ipha->ipha_src && 6495 dstport == tcp->tcp_lport) { 6496 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6497 goto failed; 6498 } 6499 6500 tcp->tcp_ipha->ipha_dst = dstaddr; 6501 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6502 6503 /* 6504 * Massage a source route if any putting the first hop 6505 * in iph_dst. Compute a starting value for the checksum which 6506 * takes into account that the original iph_dst should be 6507 * included in the checksum but that ip will include the 6508 * first hop in the source route in the tcp checksum. 6509 */ 6510 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6511 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6512 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6513 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6514 if ((int)tcp->tcp_sum < 0) 6515 tcp->tcp_sum--; 6516 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6517 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6518 (tcp->tcp_sum >> 16)); 6519 tcph = tcp->tcp_tcph; 6520 *(uint16_t *)tcph->th_fport = dstport; 6521 tcp->tcp_fport = dstport; 6522 6523 oldstate = tcp->tcp_state; 6524 /* 6525 * At this point the remote destination address and remote port fields 6526 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6527 * have to see which state tcp was in so we can take apropriate action. 6528 */ 6529 if (oldstate == TCPS_IDLE) { 6530 /* 6531 * We support a quick connect capability here, allowing 6532 * clients to transition directly from IDLE to SYN_SENT 6533 * tcp_bindi will pick an unused port, insert the connection 6534 * in the bind hash and transition to BOUND state. 6535 */ 6536 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6537 tcp, B_TRUE); 6538 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6539 B_FALSE, B_FALSE); 6540 if (lport == 0) { 6541 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6542 goto failed; 6543 } 6544 } 6545 tcp->tcp_state = TCPS_SYN_SENT; 6546 6547 /* 6548 * TODO: allow data with connect requests 6549 * by unlinking M_DATA trailers here and 6550 * linking them in behind the T_OK_ACK mblk. 6551 * The tcp_rput() bind ack handler would then 6552 * feed them to tcp_wput_data() rather than call 6553 * tcp_timer(). 6554 */ 6555 mp = mi_tpi_ok_ack_alloc(mp); 6556 if (!mp) { 6557 tcp->tcp_state = oldstate; 6558 goto failed; 6559 } 6560 if (tcp->tcp_family == AF_INET) { 6561 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6562 sizeof (ipa_conn_t)); 6563 } else { 6564 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6565 sizeof (ipa6_conn_t)); 6566 } 6567 if (mp1) { 6568 /* Hang onto the T_OK_ACK for later. */ 6569 linkb(mp1, mp); 6570 mblk_setcred(mp1, tcp->tcp_cred); 6571 if (tcp->tcp_family == AF_INET) 6572 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6573 else { 6574 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6575 &tcp->tcp_sticky_ipp); 6576 } 6577 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6578 tcp->tcp_active_open = 1; 6579 /* 6580 * If the bind cannot complete immediately 6581 * IP will arrange to call tcp_rput_other 6582 * when the bind completes. 6583 */ 6584 if (mp1 != NULL) 6585 tcp_rput_other(tcp, mp1); 6586 return; 6587 } 6588 /* Error case */ 6589 tcp->tcp_state = oldstate; 6590 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6591 6592 failed: 6593 /* return error ack and blow away saved option results if any */ 6594 if (mp != NULL) 6595 putnext(tcp->tcp_rq, mp); 6596 else { 6597 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6598 TSYSERR, ENOMEM); 6599 } 6600 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6601 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6602 6603 } 6604 6605 /* 6606 * Handle connect to IPv6 destinations. 6607 */ 6608 static void 6609 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6610 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6611 { 6612 tcph_t *tcph; 6613 mblk_t *mp1; 6614 ip6_rthdr_t *rth; 6615 int32_t oldstate; 6616 uint16_t lport; 6617 tcp_stack_t *tcps = tcp->tcp_tcps; 6618 6619 ASSERT(tcp->tcp_family == AF_INET6); 6620 6621 /* 6622 * If we're here, it means that the destination address is a native 6623 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6624 * reason why it might not be IPv6 is if the socket was bound to an 6625 * IPv4-mapped IPv6 address. 6626 */ 6627 if (tcp->tcp_ipversion != IPV6_VERSION) { 6628 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6629 goto failed; 6630 } 6631 6632 /* 6633 * Interpret a zero destination to mean loopback. 6634 * Update the T_CONN_REQ (sin/sin6) since it is used to 6635 * generate the T_CONN_CON. 6636 */ 6637 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6638 *dstaddrp = ipv6_loopback; 6639 } 6640 6641 /* Handle __sin6_src_id if socket not bound to an IP address */ 6642 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6643 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6644 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6645 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6646 } 6647 6648 /* 6649 * Take care of the scope_id now and add ip6i_t 6650 * if ip6i_t is not already allocated through TCP 6651 * sticky options. At this point tcp_ip6h does not 6652 * have dst info, thus use dstaddrp. 6653 */ 6654 if (scope_id != 0 && 6655 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6656 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6657 ip6i_t *ip6i; 6658 6659 ipp->ipp_ifindex = scope_id; 6660 ip6i = (ip6i_t *)tcp->tcp_iphc; 6661 6662 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6663 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6664 /* Already allocated */ 6665 ip6i->ip6i_flags |= IP6I_IFINDEX; 6666 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6667 ipp->ipp_fields |= IPPF_SCOPE_ID; 6668 } else { 6669 int reterr; 6670 6671 ipp->ipp_fields |= IPPF_SCOPE_ID; 6672 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6673 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6674 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6675 if (reterr != 0) 6676 goto failed; 6677 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6678 } 6679 } 6680 6681 /* 6682 * Don't let an endpoint connect to itself. Note that 6683 * the test here does not catch the case where the 6684 * source IP addr was left unspecified by the user. In 6685 * this case, the source addr is set in tcp_adapt_ire() 6686 * using the reply to the T_BIND message that we send 6687 * down to IP here and the check is repeated in tcp_rput_other. 6688 */ 6689 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6690 (dstport == tcp->tcp_lport)) { 6691 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6692 goto failed; 6693 } 6694 6695 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6696 tcp->tcp_remote_v6 = *dstaddrp; 6697 tcp->tcp_ip6h->ip6_vcf = 6698 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6699 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6700 6701 6702 /* 6703 * Massage a routing header (if present) putting the first hop 6704 * in ip6_dst. Compute a starting value for the checksum which 6705 * takes into account that the original ip6_dst should be 6706 * included in the checksum but that ip will include the 6707 * first hop in the source route in the tcp checksum. 6708 */ 6709 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6710 if (rth != NULL) { 6711 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6712 tcps->tcps_netstack); 6713 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6714 (tcp->tcp_sum >> 16)); 6715 } else { 6716 tcp->tcp_sum = 0; 6717 } 6718 6719 tcph = tcp->tcp_tcph; 6720 *(uint16_t *)tcph->th_fport = dstport; 6721 tcp->tcp_fport = dstport; 6722 6723 oldstate = tcp->tcp_state; 6724 /* 6725 * At this point the remote destination address and remote port fields 6726 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6727 * have to see which state tcp was in so we can take apropriate action. 6728 */ 6729 if (oldstate == TCPS_IDLE) { 6730 /* 6731 * We support a quick connect capability here, allowing 6732 * clients to transition directly from IDLE to SYN_SENT 6733 * tcp_bindi will pick an unused port, insert the connection 6734 * in the bind hash and transition to BOUND state. 6735 */ 6736 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6737 tcp, B_TRUE); 6738 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6739 B_FALSE, B_FALSE); 6740 if (lport == 0) { 6741 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6742 goto failed; 6743 } 6744 } 6745 tcp->tcp_state = TCPS_SYN_SENT; 6746 /* 6747 * TODO: allow data with connect requests 6748 * by unlinking M_DATA trailers here and 6749 * linking them in behind the T_OK_ACK mblk. 6750 * The tcp_rput() bind ack handler would then 6751 * feed them to tcp_wput_data() rather than call 6752 * tcp_timer(). 6753 */ 6754 mp = mi_tpi_ok_ack_alloc(mp); 6755 if (!mp) { 6756 tcp->tcp_state = oldstate; 6757 goto failed; 6758 } 6759 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6760 if (mp1) { 6761 /* Hang onto the T_OK_ACK for later. */ 6762 linkb(mp1, mp); 6763 mblk_setcred(mp1, tcp->tcp_cred); 6764 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6765 &tcp->tcp_sticky_ipp); 6766 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6767 tcp->tcp_active_open = 1; 6768 /* ip_bind_v6() may return ACK or ERROR */ 6769 if (mp1 != NULL) 6770 tcp_rput_other(tcp, mp1); 6771 return; 6772 } 6773 /* Error case */ 6774 tcp->tcp_state = oldstate; 6775 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6776 6777 failed: 6778 /* return error ack and blow away saved option results if any */ 6779 if (mp != NULL) 6780 putnext(tcp->tcp_rq, mp); 6781 else { 6782 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6783 TSYSERR, ENOMEM); 6784 } 6785 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6786 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6787 } 6788 6789 /* 6790 * We need a stream q for detached closing tcp connections 6791 * to use. Our client hereby indicates that this q is the 6792 * one to use. 6793 */ 6794 static void 6795 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6796 { 6797 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6798 queue_t *q = tcp->tcp_wq; 6799 tcp_stack_t *tcps = tcp->tcp_tcps; 6800 6801 #ifdef NS_DEBUG 6802 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6803 tcps->tcps_netstack->netstack_stackid); 6804 #endif 6805 mp->b_datap->db_type = M_IOCACK; 6806 iocp->ioc_count = 0; 6807 mutex_enter(&tcps->tcps_g_q_lock); 6808 if (tcps->tcps_g_q != NULL) { 6809 mutex_exit(&tcps->tcps_g_q_lock); 6810 iocp->ioc_error = EALREADY; 6811 } else { 6812 mblk_t *mp1; 6813 6814 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6815 if (mp1 == NULL) { 6816 mutex_exit(&tcps->tcps_g_q_lock); 6817 iocp->ioc_error = ENOMEM; 6818 } else { 6819 tcps->tcps_g_q = tcp->tcp_rq; 6820 mutex_exit(&tcps->tcps_g_q_lock); 6821 iocp->ioc_error = 0; 6822 iocp->ioc_rval = 0; 6823 /* 6824 * We are passing tcp_sticky_ipp as NULL 6825 * as it is not useful for tcp_default queue 6826 */ 6827 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6828 if (mp1 != NULL) 6829 tcp_rput_other(tcp, mp1); 6830 } 6831 } 6832 qreply(q, mp); 6833 } 6834 6835 /* 6836 * Our client hereby directs us to reject the connection request 6837 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6838 * of sending the appropriate RST, not an ICMP error. 6839 */ 6840 static void 6841 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6842 { 6843 tcp_t *ltcp = NULL; 6844 t_scalar_t seqnum; 6845 conn_t *connp; 6846 tcp_stack_t *tcps = tcp->tcp_tcps; 6847 6848 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6849 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6850 tcp_err_ack(tcp, mp, TPROTO, 0); 6851 return; 6852 } 6853 6854 /* 6855 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6856 * when the stream is in BOUND state. Do not send a reset, 6857 * since the destination IP address is not valid, and it can 6858 * be the initialized value of all zeros (broadcast address). 6859 * 6860 * If TCP has sent down a bind request to IP and has not 6861 * received the reply, reject the request. Otherwise, TCP 6862 * will be confused. 6863 */ 6864 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6865 if (tcp->tcp_debug) { 6866 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6867 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6868 } 6869 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6870 return; 6871 } 6872 6873 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6874 6875 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6876 6877 /* 6878 * According to TPI, for non-listeners, ignore seqnum 6879 * and disconnect. 6880 * Following interpretation of -1 seqnum is historical 6881 * and implied TPI ? (TPI only states that for T_CONN_IND, 6882 * a valid seqnum should not be -1). 6883 * 6884 * -1 means disconnect everything 6885 * regardless even on a listener. 6886 */ 6887 6888 int old_state = tcp->tcp_state; 6889 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6890 6891 /* 6892 * The connection can't be on the tcp_time_wait_head list 6893 * since it is not detached. 6894 */ 6895 ASSERT(tcp->tcp_time_wait_next == NULL); 6896 ASSERT(tcp->tcp_time_wait_prev == NULL); 6897 ASSERT(tcp->tcp_time_wait_expire == 0); 6898 ltcp = NULL; 6899 /* 6900 * If it used to be a listener, check to make sure no one else 6901 * has taken the port before switching back to LISTEN state. 6902 */ 6903 if (tcp->tcp_ipversion == IPV4_VERSION) { 6904 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6905 tcp->tcp_ipha->ipha_src, 6906 tcp->tcp_connp->conn_zoneid, ipst); 6907 if (connp != NULL) 6908 ltcp = connp->conn_tcp; 6909 } else { 6910 /* Allow tcp_bound_if listeners? */ 6911 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6912 &tcp->tcp_ip6h->ip6_src, 0, 6913 tcp->tcp_connp->conn_zoneid, ipst); 6914 if (connp != NULL) 6915 ltcp = connp->conn_tcp; 6916 } 6917 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6918 tcp->tcp_state = TCPS_LISTEN; 6919 } else if (old_state > TCPS_BOUND) { 6920 tcp->tcp_conn_req_max = 0; 6921 tcp->tcp_state = TCPS_BOUND; 6922 } 6923 if (ltcp != NULL) 6924 CONN_DEC_REF(ltcp->tcp_connp); 6925 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6926 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6927 } else if (old_state == TCPS_ESTABLISHED || 6928 old_state == TCPS_CLOSE_WAIT) { 6929 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6930 } 6931 6932 if (tcp->tcp_fused) 6933 tcp_unfuse(tcp); 6934 6935 mutex_enter(&tcp->tcp_eager_lock); 6936 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6937 (tcp->tcp_conn_req_cnt_q != 0)) { 6938 tcp_eager_cleanup(tcp, 0); 6939 } 6940 mutex_exit(&tcp->tcp_eager_lock); 6941 6942 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6943 tcp->tcp_rnxt, TH_RST | TH_ACK); 6944 6945 tcp_reinit(tcp); 6946 6947 if (old_state >= TCPS_ESTABLISHED) { 6948 /* Send M_FLUSH according to TPI */ 6949 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6950 } 6951 mp = mi_tpi_ok_ack_alloc(mp); 6952 if (mp) 6953 putnext(tcp->tcp_rq, mp); 6954 return; 6955 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6956 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6957 return; 6958 } 6959 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6960 /* Send M_FLUSH according to TPI */ 6961 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6962 } 6963 mp = mi_tpi_ok_ack_alloc(mp); 6964 if (mp) 6965 putnext(tcp->tcp_rq, mp); 6966 } 6967 6968 /* 6969 * Diagnostic routine used to return a string associated with the tcp state. 6970 * Note that if the caller does not supply a buffer, it will use an internal 6971 * static string. This means that if multiple threads call this function at 6972 * the same time, output can be corrupted... Note also that this function 6973 * does not check the size of the supplied buffer. The caller has to make 6974 * sure that it is big enough. 6975 */ 6976 static char * 6977 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6978 { 6979 char buf1[30]; 6980 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6981 char *buf; 6982 char *cp; 6983 in6_addr_t local, remote; 6984 char local_addrbuf[INET6_ADDRSTRLEN]; 6985 char remote_addrbuf[INET6_ADDRSTRLEN]; 6986 6987 if (sup_buf != NULL) 6988 buf = sup_buf; 6989 else 6990 buf = priv_buf; 6991 6992 if (tcp == NULL) 6993 return ("NULL_TCP"); 6994 switch (tcp->tcp_state) { 6995 case TCPS_CLOSED: 6996 cp = "TCP_CLOSED"; 6997 break; 6998 case TCPS_IDLE: 6999 cp = "TCP_IDLE"; 7000 break; 7001 case TCPS_BOUND: 7002 cp = "TCP_BOUND"; 7003 break; 7004 case TCPS_LISTEN: 7005 cp = "TCP_LISTEN"; 7006 break; 7007 case TCPS_SYN_SENT: 7008 cp = "TCP_SYN_SENT"; 7009 break; 7010 case TCPS_SYN_RCVD: 7011 cp = "TCP_SYN_RCVD"; 7012 break; 7013 case TCPS_ESTABLISHED: 7014 cp = "TCP_ESTABLISHED"; 7015 break; 7016 case TCPS_CLOSE_WAIT: 7017 cp = "TCP_CLOSE_WAIT"; 7018 break; 7019 case TCPS_FIN_WAIT_1: 7020 cp = "TCP_FIN_WAIT_1"; 7021 break; 7022 case TCPS_CLOSING: 7023 cp = "TCP_CLOSING"; 7024 break; 7025 case TCPS_LAST_ACK: 7026 cp = "TCP_LAST_ACK"; 7027 break; 7028 case TCPS_FIN_WAIT_2: 7029 cp = "TCP_FIN_WAIT_2"; 7030 break; 7031 case TCPS_TIME_WAIT: 7032 cp = "TCP_TIME_WAIT"; 7033 break; 7034 default: 7035 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7036 cp = buf1; 7037 break; 7038 } 7039 switch (format) { 7040 case DISP_ADDR_AND_PORT: 7041 if (tcp->tcp_ipversion == IPV4_VERSION) { 7042 /* 7043 * Note that we use the remote address in the tcp_b 7044 * structure. This means that it will print out 7045 * the real destination address, not the next hop's 7046 * address if source routing is used. 7047 */ 7048 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7049 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7050 7051 } else { 7052 local = tcp->tcp_ip_src_v6; 7053 remote = tcp->tcp_remote_v6; 7054 } 7055 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7056 sizeof (local_addrbuf)); 7057 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7058 sizeof (remote_addrbuf)); 7059 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7060 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7061 ntohs(tcp->tcp_fport), cp); 7062 break; 7063 case DISP_PORT_ONLY: 7064 default: 7065 (void) mi_sprintf(buf, "[%u, %u] %s", 7066 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7067 break; 7068 } 7069 7070 return (buf); 7071 } 7072 7073 /* 7074 * Called via squeue to get on to eager's perimeter to send a 7075 * TH_RST. The listener wants the eager to disappear either 7076 * by means of tcp_eager_blowoff() or tcp_eager_cleanup() 7077 * being called. 7078 */ 7079 /* ARGSUSED */ 7080 void 7081 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7082 { 7083 conn_t *econnp = (conn_t *)arg; 7084 tcp_t *eager = econnp->conn_tcp; 7085 tcp_t *listener = eager->tcp_listener; 7086 tcp_stack_t *tcps = eager->tcp_tcps; 7087 7088 /* 7089 * We could be called because listener is closing. Since 7090 * the eager is using listener's queue's, its not safe. 7091 * Better use the default queue just to send the TH_RST 7092 * out. 7093 */ 7094 ASSERT(tcps->tcps_g_q != NULL); 7095 eager->tcp_rq = tcps->tcps_g_q; 7096 eager->tcp_wq = WR(tcps->tcps_g_q); 7097 7098 if (eager->tcp_state > TCPS_LISTEN) { 7099 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7100 eager, eager->tcp_snxt, 0, TH_RST); 7101 } 7102 7103 /* We are here because listener wants this eager gone */ 7104 if (listener != NULL) { 7105 mutex_enter(&listener->tcp_eager_lock); 7106 tcp_eager_unlink(eager); 7107 if (eager->tcp_tconnind_started) { 7108 /* 7109 * The eager has sent a conn_ind up to the 7110 * listener but listener decides to close 7111 * instead. We need to drop the extra ref 7112 * placed on eager in tcp_rput_data() before 7113 * sending the conn_ind to listener. 7114 */ 7115 CONN_DEC_REF(econnp); 7116 } 7117 mutex_exit(&listener->tcp_eager_lock); 7118 CONN_DEC_REF(listener->tcp_connp); 7119 } 7120 7121 if (eager->tcp_state > TCPS_BOUND) 7122 tcp_close_detached(eager); 7123 } 7124 7125 /* 7126 * Reset any eager connection hanging off this listener marked 7127 * with 'seqnum' and then reclaim it's resources. 7128 */ 7129 static boolean_t 7130 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7131 { 7132 tcp_t *eager; 7133 mblk_t *mp; 7134 tcp_stack_t *tcps = listener->tcp_tcps; 7135 7136 TCP_STAT(tcps, tcp_eager_blowoff_calls); 7137 eager = listener; 7138 mutex_enter(&listener->tcp_eager_lock); 7139 do { 7140 eager = eager->tcp_eager_next_q; 7141 if (eager == NULL) { 7142 mutex_exit(&listener->tcp_eager_lock); 7143 return (B_FALSE); 7144 } 7145 } while (eager->tcp_conn_req_seqnum != seqnum); 7146 7147 if (eager->tcp_closemp_used > 0) { 7148 mutex_exit(&listener->tcp_eager_lock); 7149 return (B_TRUE); 7150 } 7151 eager->tcp_closemp_used = 1; 7152 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7153 CONN_INC_REF(eager->tcp_connp); 7154 mutex_exit(&listener->tcp_eager_lock); 7155 mp = &eager->tcp_closemp; 7156 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7157 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7158 return (B_TRUE); 7159 } 7160 7161 /* 7162 * Reset any eager connection hanging off this listener 7163 * and then reclaim it's resources. 7164 */ 7165 static void 7166 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7167 { 7168 tcp_t *eager; 7169 mblk_t *mp; 7170 tcp_stack_t *tcps = listener->tcp_tcps; 7171 7172 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7173 7174 if (!q0_only) { 7175 /* First cleanup q */ 7176 TCP_STAT(tcps, tcp_eager_blowoff_q); 7177 eager = listener->tcp_eager_next_q; 7178 while (eager != NULL) { 7179 if (eager->tcp_closemp_used == 0) { 7180 eager->tcp_closemp_used = 1; 7181 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7182 CONN_INC_REF(eager->tcp_connp); 7183 mp = &eager->tcp_closemp; 7184 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7185 tcp_eager_kill, eager->tcp_connp, 7186 SQTAG_TCP_EAGER_CLEANUP); 7187 } 7188 eager = eager->tcp_eager_next_q; 7189 } 7190 } 7191 /* Then cleanup q0 */ 7192 TCP_STAT(tcps, tcp_eager_blowoff_q0); 7193 eager = listener->tcp_eager_next_q0; 7194 while (eager != listener) { 7195 if (eager->tcp_closemp_used == 0) { 7196 eager->tcp_closemp_used = 1; 7197 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7198 CONN_INC_REF(eager->tcp_connp); 7199 mp = &eager->tcp_closemp; 7200 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7201 tcp_eager_kill, eager->tcp_connp, 7202 SQTAG_TCP_EAGER_CLEANUP_Q0); 7203 } 7204 eager = eager->tcp_eager_next_q0; 7205 } 7206 } 7207 7208 /* 7209 * If we are an eager connection hanging off a listener that hasn't 7210 * formally accepted the connection yet, get off his list and blow off 7211 * any data that we have accumulated. 7212 */ 7213 static void 7214 tcp_eager_unlink(tcp_t *tcp) 7215 { 7216 tcp_t *listener = tcp->tcp_listener; 7217 7218 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7219 ASSERT(listener != NULL); 7220 if (tcp->tcp_eager_next_q0 != NULL) { 7221 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7222 7223 /* Remove the eager tcp from q0 */ 7224 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7225 tcp->tcp_eager_prev_q0; 7226 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7227 tcp->tcp_eager_next_q0; 7228 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7229 listener->tcp_conn_req_cnt_q0--; 7230 7231 tcp->tcp_eager_next_q0 = NULL; 7232 tcp->tcp_eager_prev_q0 = NULL; 7233 7234 /* 7235 * Take the eager out, if it is in the list of droppable 7236 * eagers. 7237 */ 7238 MAKE_UNDROPPABLE(tcp); 7239 7240 if (tcp->tcp_syn_rcvd_timeout != 0) { 7241 /* we have timed out before */ 7242 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7243 listener->tcp_syn_rcvd_timeout--; 7244 } 7245 } else { 7246 tcp_t **tcpp = &listener->tcp_eager_next_q; 7247 tcp_t *prev = NULL; 7248 7249 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7250 if (tcpp[0] == tcp) { 7251 if (listener->tcp_eager_last_q == tcp) { 7252 /* 7253 * If we are unlinking the last 7254 * element on the list, adjust 7255 * tail pointer. Set tail pointer 7256 * to nil when list is empty. 7257 */ 7258 ASSERT(tcp->tcp_eager_next_q == NULL); 7259 if (listener->tcp_eager_last_q == 7260 listener->tcp_eager_next_q) { 7261 listener->tcp_eager_last_q = 7262 NULL; 7263 } else { 7264 /* 7265 * We won't get here if there 7266 * is only one eager in the 7267 * list. 7268 */ 7269 ASSERT(prev != NULL); 7270 listener->tcp_eager_last_q = 7271 prev; 7272 } 7273 } 7274 tcpp[0] = tcp->tcp_eager_next_q; 7275 tcp->tcp_eager_next_q = NULL; 7276 tcp->tcp_eager_last_q = NULL; 7277 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7278 listener->tcp_conn_req_cnt_q--; 7279 break; 7280 } 7281 prev = tcpp[0]; 7282 } 7283 } 7284 tcp->tcp_listener = NULL; 7285 } 7286 7287 /* Shorthand to generate and send TPI error acks to our client */ 7288 static void 7289 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7290 { 7291 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7292 putnext(tcp->tcp_rq, mp); 7293 } 7294 7295 /* Shorthand to generate and send TPI error acks to our client */ 7296 static void 7297 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7298 int t_error, int sys_error) 7299 { 7300 struct T_error_ack *teackp; 7301 7302 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7303 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7304 teackp = (struct T_error_ack *)mp->b_rptr; 7305 teackp->ERROR_prim = primitive; 7306 teackp->TLI_error = t_error; 7307 teackp->UNIX_error = sys_error; 7308 putnext(tcp->tcp_rq, mp); 7309 } 7310 } 7311 7312 /* 7313 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7314 * but instead the code relies on: 7315 * - the fact that the address of the array and its size never changes 7316 * - the atomic assignment of the elements of the array 7317 */ 7318 /* ARGSUSED */ 7319 static int 7320 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7321 { 7322 int i; 7323 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7324 7325 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7326 if (tcps->tcps_g_epriv_ports[i] != 0) 7327 (void) mi_mpprintf(mp, "%d ", 7328 tcps->tcps_g_epriv_ports[i]); 7329 } 7330 return (0); 7331 } 7332 7333 /* 7334 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7335 * threads from changing it at the same time. 7336 */ 7337 /* ARGSUSED */ 7338 static int 7339 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7340 cred_t *cr) 7341 { 7342 long new_value; 7343 int i; 7344 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7345 7346 /* 7347 * Fail the request if the new value does not lie within the 7348 * port number limits. 7349 */ 7350 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7351 new_value <= 0 || new_value >= 65536) { 7352 return (EINVAL); 7353 } 7354 7355 mutex_enter(&tcps->tcps_epriv_port_lock); 7356 /* Check if the value is already in the list */ 7357 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7358 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7359 mutex_exit(&tcps->tcps_epriv_port_lock); 7360 return (EEXIST); 7361 } 7362 } 7363 /* Find an empty slot */ 7364 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7365 if (tcps->tcps_g_epriv_ports[i] == 0) 7366 break; 7367 } 7368 if (i == tcps->tcps_g_num_epriv_ports) { 7369 mutex_exit(&tcps->tcps_epriv_port_lock); 7370 return (EOVERFLOW); 7371 } 7372 /* Set the new value */ 7373 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7374 mutex_exit(&tcps->tcps_epriv_port_lock); 7375 return (0); 7376 } 7377 7378 /* 7379 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7380 * threads from changing it at the same time. 7381 */ 7382 /* ARGSUSED */ 7383 static int 7384 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7385 cred_t *cr) 7386 { 7387 long new_value; 7388 int i; 7389 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7390 7391 /* 7392 * Fail the request if the new value does not lie within the 7393 * port number limits. 7394 */ 7395 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7396 new_value >= 65536) { 7397 return (EINVAL); 7398 } 7399 7400 mutex_enter(&tcps->tcps_epriv_port_lock); 7401 /* Check that the value is already in the list */ 7402 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7403 if (tcps->tcps_g_epriv_ports[i] == new_value) 7404 break; 7405 } 7406 if (i == tcps->tcps_g_num_epriv_ports) { 7407 mutex_exit(&tcps->tcps_epriv_port_lock); 7408 return (ESRCH); 7409 } 7410 /* Clear the value */ 7411 tcps->tcps_g_epriv_ports[i] = 0; 7412 mutex_exit(&tcps->tcps_epriv_port_lock); 7413 return (0); 7414 } 7415 7416 /* Return the TPI/TLI equivalent of our current tcp_state */ 7417 static int 7418 tcp_tpistate(tcp_t *tcp) 7419 { 7420 switch (tcp->tcp_state) { 7421 case TCPS_IDLE: 7422 return (TS_UNBND); 7423 case TCPS_LISTEN: 7424 /* 7425 * Return whether there are outstanding T_CONN_IND waiting 7426 * for the matching T_CONN_RES. Therefore don't count q0. 7427 */ 7428 if (tcp->tcp_conn_req_cnt_q > 0) 7429 return (TS_WRES_CIND); 7430 else 7431 return (TS_IDLE); 7432 case TCPS_BOUND: 7433 return (TS_IDLE); 7434 case TCPS_SYN_SENT: 7435 return (TS_WCON_CREQ); 7436 case TCPS_SYN_RCVD: 7437 /* 7438 * Note: assumption: this has to the active open SYN_RCVD. 7439 * The passive instance is detached in SYN_RCVD stage of 7440 * incoming connection processing so we cannot get request 7441 * for T_info_ack on it. 7442 */ 7443 return (TS_WACK_CRES); 7444 case TCPS_ESTABLISHED: 7445 return (TS_DATA_XFER); 7446 case TCPS_CLOSE_WAIT: 7447 return (TS_WREQ_ORDREL); 7448 case TCPS_FIN_WAIT_1: 7449 return (TS_WIND_ORDREL); 7450 case TCPS_FIN_WAIT_2: 7451 return (TS_WIND_ORDREL); 7452 7453 case TCPS_CLOSING: 7454 case TCPS_LAST_ACK: 7455 case TCPS_TIME_WAIT: 7456 case TCPS_CLOSED: 7457 /* 7458 * Following TS_WACK_DREQ7 is a rendition of "not 7459 * yet TS_IDLE" TPI state. There is no best match to any 7460 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7461 * choose a value chosen that will map to TLI/XTI level 7462 * state of TSTATECHNG (state is process of changing) which 7463 * captures what this dummy state represents. 7464 */ 7465 return (TS_WACK_DREQ7); 7466 default: 7467 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7468 tcp->tcp_state, tcp_display(tcp, NULL, 7469 DISP_PORT_ONLY)); 7470 return (TS_UNBND); 7471 } 7472 } 7473 7474 static void 7475 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7476 { 7477 tcp_stack_t *tcps = tcp->tcp_tcps; 7478 7479 if (tcp->tcp_family == AF_INET6) 7480 *tia = tcp_g_t_info_ack_v6; 7481 else 7482 *tia = tcp_g_t_info_ack; 7483 tia->CURRENT_state = tcp_tpistate(tcp); 7484 tia->OPT_size = tcp_max_optsize; 7485 if (tcp->tcp_mss == 0) { 7486 /* Not yet set - tcp_open does not set mss */ 7487 if (tcp->tcp_ipversion == IPV4_VERSION) 7488 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7489 else 7490 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7491 } else { 7492 tia->TIDU_size = tcp->tcp_mss; 7493 } 7494 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7495 } 7496 7497 /* 7498 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7499 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7500 * tcp_g_t_info_ack. The current state of the stream is copied from 7501 * tcp_state. 7502 */ 7503 static void 7504 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7505 { 7506 t_uscalar_t cap_bits1; 7507 struct T_capability_ack *tcap; 7508 7509 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7510 freemsg(mp); 7511 return; 7512 } 7513 7514 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7515 7516 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7517 mp->b_datap->db_type, T_CAPABILITY_ACK); 7518 if (mp == NULL) 7519 return; 7520 7521 tcap = (struct T_capability_ack *)mp->b_rptr; 7522 tcap->CAP_bits1 = 0; 7523 7524 if (cap_bits1 & TC1_INFO) { 7525 tcp_copy_info(&tcap->INFO_ack, tcp); 7526 tcap->CAP_bits1 |= TC1_INFO; 7527 } 7528 7529 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7530 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7531 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7532 } 7533 7534 putnext(tcp->tcp_rq, mp); 7535 } 7536 7537 /* 7538 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7539 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7540 * The current state of the stream is copied from tcp_state. 7541 */ 7542 static void 7543 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7544 { 7545 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7546 T_INFO_ACK); 7547 if (!mp) { 7548 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7549 return; 7550 } 7551 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7552 putnext(tcp->tcp_rq, mp); 7553 } 7554 7555 /* Respond to the TPI addr request */ 7556 static void 7557 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7558 { 7559 sin_t *sin; 7560 mblk_t *ackmp; 7561 struct T_addr_ack *taa; 7562 7563 /* Make it large enough for worst case */ 7564 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7565 2 * sizeof (sin6_t), 1); 7566 if (ackmp == NULL) { 7567 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7568 return; 7569 } 7570 7571 if (tcp->tcp_ipversion == IPV6_VERSION) { 7572 tcp_addr_req_ipv6(tcp, ackmp); 7573 return; 7574 } 7575 taa = (struct T_addr_ack *)ackmp->b_rptr; 7576 7577 bzero(taa, sizeof (struct T_addr_ack)); 7578 ackmp->b_wptr = (uchar_t *)&taa[1]; 7579 7580 taa->PRIM_type = T_ADDR_ACK; 7581 ackmp->b_datap->db_type = M_PCPROTO; 7582 7583 /* 7584 * Note: Following code assumes 32 bit alignment of basic 7585 * data structures like sin_t and struct T_addr_ack. 7586 */ 7587 if (tcp->tcp_state >= TCPS_BOUND) { 7588 /* 7589 * Fill in local address 7590 */ 7591 taa->LOCADDR_length = sizeof (sin_t); 7592 taa->LOCADDR_offset = sizeof (*taa); 7593 7594 sin = (sin_t *)&taa[1]; 7595 7596 /* Fill zeroes and then intialize non-zero fields */ 7597 *sin = sin_null; 7598 7599 sin->sin_family = AF_INET; 7600 7601 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7602 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7603 7604 ackmp->b_wptr = (uchar_t *)&sin[1]; 7605 7606 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7607 /* 7608 * Fill in Remote address 7609 */ 7610 taa->REMADDR_length = sizeof (sin_t); 7611 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7612 taa->LOCADDR_length); 7613 7614 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7615 *sin = sin_null; 7616 sin->sin_family = AF_INET; 7617 sin->sin_addr.s_addr = tcp->tcp_remote; 7618 sin->sin_port = tcp->tcp_fport; 7619 7620 ackmp->b_wptr = (uchar_t *)&sin[1]; 7621 } 7622 } 7623 putnext(tcp->tcp_rq, ackmp); 7624 } 7625 7626 /* Assumes that tcp_addr_req gets enough space and alignment */ 7627 static void 7628 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7629 { 7630 sin6_t *sin6; 7631 struct T_addr_ack *taa; 7632 7633 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7634 ASSERT(OK_32PTR(ackmp->b_rptr)); 7635 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7636 2 * sizeof (sin6_t)); 7637 7638 taa = (struct T_addr_ack *)ackmp->b_rptr; 7639 7640 bzero(taa, sizeof (struct T_addr_ack)); 7641 ackmp->b_wptr = (uchar_t *)&taa[1]; 7642 7643 taa->PRIM_type = T_ADDR_ACK; 7644 ackmp->b_datap->db_type = M_PCPROTO; 7645 7646 /* 7647 * Note: Following code assumes 32 bit alignment of basic 7648 * data structures like sin6_t and struct T_addr_ack. 7649 */ 7650 if (tcp->tcp_state >= TCPS_BOUND) { 7651 /* 7652 * Fill in local address 7653 */ 7654 taa->LOCADDR_length = sizeof (sin6_t); 7655 taa->LOCADDR_offset = sizeof (*taa); 7656 7657 sin6 = (sin6_t *)&taa[1]; 7658 *sin6 = sin6_null; 7659 7660 sin6->sin6_family = AF_INET6; 7661 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7662 sin6->sin6_port = tcp->tcp_lport; 7663 7664 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7665 7666 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7667 /* 7668 * Fill in Remote address 7669 */ 7670 taa->REMADDR_length = sizeof (sin6_t); 7671 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7672 taa->LOCADDR_length); 7673 7674 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7675 *sin6 = sin6_null; 7676 sin6->sin6_family = AF_INET6; 7677 sin6->sin6_flowinfo = 7678 tcp->tcp_ip6h->ip6_vcf & 7679 ~IPV6_VERS_AND_FLOW_MASK; 7680 sin6->sin6_addr = tcp->tcp_remote_v6; 7681 sin6->sin6_port = tcp->tcp_fport; 7682 7683 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7684 } 7685 } 7686 putnext(tcp->tcp_rq, ackmp); 7687 } 7688 7689 /* 7690 * Handle reinitialization of a tcp structure. 7691 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7692 */ 7693 static void 7694 tcp_reinit(tcp_t *tcp) 7695 { 7696 mblk_t *mp; 7697 int err; 7698 tcp_stack_t *tcps = tcp->tcp_tcps; 7699 7700 TCP_STAT(tcps, tcp_reinit_calls); 7701 7702 /* tcp_reinit should never be called for detached tcp_t's */ 7703 ASSERT(tcp->tcp_listener == NULL); 7704 ASSERT((tcp->tcp_family == AF_INET && 7705 tcp->tcp_ipversion == IPV4_VERSION) || 7706 (tcp->tcp_family == AF_INET6 && 7707 (tcp->tcp_ipversion == IPV4_VERSION || 7708 tcp->tcp_ipversion == IPV6_VERSION))); 7709 7710 /* Cancel outstanding timers */ 7711 tcp_timers_stop(tcp); 7712 7713 /* 7714 * Reset everything in the state vector, after updating global 7715 * MIB data from instance counters. 7716 */ 7717 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7718 tcp->tcp_ibsegs = 0; 7719 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7720 tcp->tcp_obsegs = 0; 7721 7722 tcp_close_mpp(&tcp->tcp_xmit_head); 7723 if (tcp->tcp_snd_zcopy_aware) 7724 tcp_zcopy_notify(tcp); 7725 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7726 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7727 mutex_enter(&tcp->tcp_non_sq_lock); 7728 if (tcp->tcp_flow_stopped && 7729 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7730 tcp_clrqfull(tcp); 7731 } 7732 mutex_exit(&tcp->tcp_non_sq_lock); 7733 tcp_close_mpp(&tcp->tcp_reass_head); 7734 tcp->tcp_reass_tail = NULL; 7735 if (tcp->tcp_rcv_list != NULL) { 7736 /* Free b_next chain */ 7737 tcp_close_mpp(&tcp->tcp_rcv_list); 7738 tcp->tcp_rcv_last_head = NULL; 7739 tcp->tcp_rcv_last_tail = NULL; 7740 tcp->tcp_rcv_cnt = 0; 7741 } 7742 tcp->tcp_rcv_last_tail = NULL; 7743 7744 if ((mp = tcp->tcp_urp_mp) != NULL) { 7745 freemsg(mp); 7746 tcp->tcp_urp_mp = NULL; 7747 } 7748 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7749 freemsg(mp); 7750 tcp->tcp_urp_mark_mp = NULL; 7751 } 7752 if (tcp->tcp_fused_sigurg_mp != NULL) { 7753 freeb(tcp->tcp_fused_sigurg_mp); 7754 tcp->tcp_fused_sigurg_mp = NULL; 7755 } 7756 7757 /* 7758 * Following is a union with two members which are 7759 * identical types and size so the following cleanup 7760 * is enough. 7761 */ 7762 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7763 7764 CL_INET_DISCONNECT(tcp); 7765 7766 /* 7767 * The connection can't be on the tcp_time_wait_head list 7768 * since it is not detached. 7769 */ 7770 ASSERT(tcp->tcp_time_wait_next == NULL); 7771 ASSERT(tcp->tcp_time_wait_prev == NULL); 7772 ASSERT(tcp->tcp_time_wait_expire == 0); 7773 7774 if (tcp->tcp_kssl_pending) { 7775 tcp->tcp_kssl_pending = B_FALSE; 7776 7777 /* Don't reset if the initialized by bind. */ 7778 if (tcp->tcp_kssl_ent != NULL) { 7779 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7780 KSSL_NO_PROXY); 7781 } 7782 } 7783 if (tcp->tcp_kssl_ctx != NULL) { 7784 kssl_release_ctx(tcp->tcp_kssl_ctx); 7785 tcp->tcp_kssl_ctx = NULL; 7786 } 7787 7788 /* 7789 * Reset/preserve other values 7790 */ 7791 tcp_reinit_values(tcp); 7792 ipcl_hash_remove(tcp->tcp_connp); 7793 conn_delete_ire(tcp->tcp_connp, NULL); 7794 tcp_ipsec_cleanup(tcp); 7795 7796 if (tcp->tcp_conn_req_max != 0) { 7797 /* 7798 * This is the case when a TLI program uses the same 7799 * transport end point to accept a connection. This 7800 * makes the TCP both a listener and acceptor. When 7801 * this connection is closed, we need to set the state 7802 * back to TCPS_LISTEN. Make sure that the eager list 7803 * is reinitialized. 7804 * 7805 * Note that this stream is still bound to the four 7806 * tuples of the previous connection in IP. If a new 7807 * SYN with different foreign address comes in, IP will 7808 * not find it and will send it to the global queue. In 7809 * the global queue, TCP will do a tcp_lookup_listener() 7810 * to find this stream. This works because this stream 7811 * is only removed from connected hash. 7812 * 7813 */ 7814 tcp->tcp_state = TCPS_LISTEN; 7815 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7816 tcp->tcp_eager_next_drop_q0 = tcp; 7817 tcp->tcp_eager_prev_drop_q0 = tcp; 7818 tcp->tcp_connp->conn_recv = tcp_conn_request; 7819 if (tcp->tcp_family == AF_INET6) { 7820 ASSERT(tcp->tcp_connp->conn_af_isv6); 7821 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7822 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7823 } else { 7824 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7825 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7826 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7827 } 7828 } else { 7829 tcp->tcp_state = TCPS_BOUND; 7830 } 7831 7832 /* 7833 * Initialize to default values 7834 * Can't fail since enough header template space already allocated 7835 * at open(). 7836 */ 7837 err = tcp_init_values(tcp); 7838 ASSERT(err == 0); 7839 /* Restore state in tcp_tcph */ 7840 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7841 if (tcp->tcp_ipversion == IPV4_VERSION) 7842 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7843 else 7844 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7845 /* 7846 * Copy of the src addr. in tcp_t is needed in tcp_t 7847 * since the lookup funcs can only lookup on tcp_t 7848 */ 7849 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7850 7851 ASSERT(tcp->tcp_ptpbhn != NULL); 7852 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7853 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7854 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7855 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7856 } 7857 7858 /* 7859 * Force values to zero that need be zero. 7860 * Do not touch values asociated with the BOUND or LISTEN state 7861 * since the connection will end up in that state after the reinit. 7862 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7863 * structure! 7864 */ 7865 static void 7866 tcp_reinit_values(tcp) 7867 tcp_t *tcp; 7868 { 7869 tcp_stack_t *tcps = tcp->tcp_tcps; 7870 7871 #ifndef lint 7872 #define DONTCARE(x) 7873 #define PRESERVE(x) 7874 #else 7875 #define DONTCARE(x) ((x) = (x)) 7876 #define PRESERVE(x) ((x) = (x)) 7877 #endif /* lint */ 7878 7879 PRESERVE(tcp->tcp_bind_hash); 7880 PRESERVE(tcp->tcp_ptpbhn); 7881 PRESERVE(tcp->tcp_acceptor_hash); 7882 PRESERVE(tcp->tcp_ptpahn); 7883 7884 /* Should be ASSERT NULL on these with new code! */ 7885 ASSERT(tcp->tcp_time_wait_next == NULL); 7886 ASSERT(tcp->tcp_time_wait_prev == NULL); 7887 ASSERT(tcp->tcp_time_wait_expire == 0); 7888 PRESERVE(tcp->tcp_state); 7889 PRESERVE(tcp->tcp_rq); 7890 PRESERVE(tcp->tcp_wq); 7891 7892 ASSERT(tcp->tcp_xmit_head == NULL); 7893 ASSERT(tcp->tcp_xmit_last == NULL); 7894 ASSERT(tcp->tcp_unsent == 0); 7895 ASSERT(tcp->tcp_xmit_tail == NULL); 7896 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7897 7898 tcp->tcp_snxt = 0; /* Displayed in mib */ 7899 tcp->tcp_suna = 0; /* Displayed in mib */ 7900 tcp->tcp_swnd = 0; 7901 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7902 7903 ASSERT(tcp->tcp_ibsegs == 0); 7904 ASSERT(tcp->tcp_obsegs == 0); 7905 7906 if (tcp->tcp_iphc != NULL) { 7907 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7908 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7909 } 7910 7911 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7912 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7913 DONTCARE(tcp->tcp_ipha); 7914 DONTCARE(tcp->tcp_ip6h); 7915 DONTCARE(tcp->tcp_ip_hdr_len); 7916 DONTCARE(tcp->tcp_tcph); 7917 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7918 tcp->tcp_valid_bits = 0; 7919 7920 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7921 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7922 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7923 tcp->tcp_last_rcv_lbolt = 0; 7924 7925 tcp->tcp_init_cwnd = 0; 7926 7927 tcp->tcp_urp_last_valid = 0; 7928 tcp->tcp_hard_binding = 0; 7929 tcp->tcp_hard_bound = 0; 7930 PRESERVE(tcp->tcp_cred); 7931 PRESERVE(tcp->tcp_cpid); 7932 PRESERVE(tcp->tcp_open_time); 7933 PRESERVE(tcp->tcp_exclbind); 7934 7935 tcp->tcp_fin_acked = 0; 7936 tcp->tcp_fin_rcvd = 0; 7937 tcp->tcp_fin_sent = 0; 7938 tcp->tcp_ordrel_done = 0; 7939 7940 tcp->tcp_debug = 0; 7941 tcp->tcp_dontroute = 0; 7942 tcp->tcp_broadcast = 0; 7943 7944 tcp->tcp_useloopback = 0; 7945 tcp->tcp_reuseaddr = 0; 7946 tcp->tcp_oobinline = 0; 7947 tcp->tcp_dgram_errind = 0; 7948 7949 tcp->tcp_detached = 0; 7950 tcp->tcp_bind_pending = 0; 7951 tcp->tcp_unbind_pending = 0; 7952 tcp->tcp_deferred_clean_death = 0; 7953 7954 tcp->tcp_snd_ws_ok = B_FALSE; 7955 tcp->tcp_snd_ts_ok = B_FALSE; 7956 tcp->tcp_linger = 0; 7957 tcp->tcp_ka_enabled = 0; 7958 tcp->tcp_zero_win_probe = 0; 7959 7960 tcp->tcp_loopback = 0; 7961 tcp->tcp_localnet = 0; 7962 tcp->tcp_syn_defense = 0; 7963 tcp->tcp_set_timer = 0; 7964 7965 tcp->tcp_active_open = 0; 7966 ASSERT(tcp->tcp_timeout == B_FALSE); 7967 tcp->tcp_rexmit = B_FALSE; 7968 tcp->tcp_xmit_zc_clean = B_FALSE; 7969 7970 tcp->tcp_snd_sack_ok = B_FALSE; 7971 PRESERVE(tcp->tcp_recvdstaddr); 7972 tcp->tcp_hwcksum = B_FALSE; 7973 7974 tcp->tcp_ire_ill_check_done = B_FALSE; 7975 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7976 7977 tcp->tcp_mdt = B_FALSE; 7978 tcp->tcp_mdt_hdr_head = 0; 7979 tcp->tcp_mdt_hdr_tail = 0; 7980 7981 tcp->tcp_conn_def_q0 = 0; 7982 tcp->tcp_ip_forward_progress = B_FALSE; 7983 tcp->tcp_anon_priv_bind = 0; 7984 tcp->tcp_ecn_ok = B_FALSE; 7985 7986 tcp->tcp_cwr = B_FALSE; 7987 tcp->tcp_ecn_echo_on = B_FALSE; 7988 7989 if (tcp->tcp_sack_info != NULL) { 7990 if (tcp->tcp_notsack_list != NULL) { 7991 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7992 } 7993 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7994 tcp->tcp_sack_info = NULL; 7995 } 7996 7997 tcp->tcp_rcv_ws = 0; 7998 tcp->tcp_snd_ws = 0; 7999 tcp->tcp_ts_recent = 0; 8000 tcp->tcp_rnxt = 0; /* Displayed in mib */ 8001 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 8002 tcp->tcp_if_mtu = 0; 8003 8004 ASSERT(tcp->tcp_reass_head == NULL); 8005 ASSERT(tcp->tcp_reass_tail == NULL); 8006 8007 tcp->tcp_cwnd_cnt = 0; 8008 8009 ASSERT(tcp->tcp_rcv_list == NULL); 8010 ASSERT(tcp->tcp_rcv_last_head == NULL); 8011 ASSERT(tcp->tcp_rcv_last_tail == NULL); 8012 ASSERT(tcp->tcp_rcv_cnt == 0); 8013 8014 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8015 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8016 tcp->tcp_csuna = 0; 8017 8018 tcp->tcp_rto = 0; /* Displayed in MIB */ 8019 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8020 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8021 tcp->tcp_rtt_update = 0; 8022 8023 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8024 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8025 8026 tcp->tcp_rack = 0; /* Displayed in mib */ 8027 tcp->tcp_rack_cnt = 0; 8028 tcp->tcp_rack_cur_max = 0; 8029 tcp->tcp_rack_abs_max = 0; 8030 8031 tcp->tcp_max_swnd = 0; 8032 8033 ASSERT(tcp->tcp_listener == NULL); 8034 8035 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8036 8037 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8038 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8039 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8040 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8041 8042 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8043 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8044 PRESERVE(tcp->tcp_conn_req_max); 8045 PRESERVE(tcp->tcp_conn_req_seqnum); 8046 8047 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8048 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8049 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8050 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8051 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8052 8053 tcp->tcp_lingertime = 0; 8054 8055 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8056 ASSERT(tcp->tcp_urp_mp == NULL); 8057 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8058 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8059 8060 ASSERT(tcp->tcp_eager_next_q == NULL); 8061 ASSERT(tcp->tcp_eager_last_q == NULL); 8062 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8063 tcp->tcp_eager_prev_q0 == NULL) || 8064 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8065 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8066 8067 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8068 tcp->tcp_eager_prev_drop_q0 == NULL) || 8069 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8070 8071 tcp->tcp_client_errno = 0; 8072 8073 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8074 8075 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8076 8077 PRESERVE(tcp->tcp_bound_source_v6); 8078 tcp->tcp_last_sent_len = 0; 8079 tcp->tcp_dupack_cnt = 0; 8080 8081 tcp->tcp_fport = 0; /* Displayed in MIB */ 8082 PRESERVE(tcp->tcp_lport); 8083 8084 PRESERVE(tcp->tcp_acceptor_lockp); 8085 8086 ASSERT(tcp->tcp_ordrelid == 0); 8087 PRESERVE(tcp->tcp_acceptor_id); 8088 DONTCARE(tcp->tcp_ipsec_overhead); 8089 8090 /* 8091 * If tcp_tracing flag is ON (i.e. We have a trace buffer 8092 * in tcp structure and now tracing), Re-initialize all 8093 * members of tcp_traceinfo. 8094 */ 8095 if (tcp->tcp_tracebuf != NULL) { 8096 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 8097 } 8098 8099 PRESERVE(tcp->tcp_family); 8100 if (tcp->tcp_family == AF_INET6) { 8101 tcp->tcp_ipversion = IPV6_VERSION; 8102 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8103 } else { 8104 tcp->tcp_ipversion = IPV4_VERSION; 8105 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8106 } 8107 8108 tcp->tcp_bound_if = 0; 8109 tcp->tcp_ipv6_recvancillary = 0; 8110 tcp->tcp_recvifindex = 0; 8111 tcp->tcp_recvhops = 0; 8112 tcp->tcp_closed = 0; 8113 tcp->tcp_cleandeathtag = 0; 8114 if (tcp->tcp_hopopts != NULL) { 8115 mi_free(tcp->tcp_hopopts); 8116 tcp->tcp_hopopts = NULL; 8117 tcp->tcp_hopoptslen = 0; 8118 } 8119 ASSERT(tcp->tcp_hopoptslen == 0); 8120 if (tcp->tcp_dstopts != NULL) { 8121 mi_free(tcp->tcp_dstopts); 8122 tcp->tcp_dstopts = NULL; 8123 tcp->tcp_dstoptslen = 0; 8124 } 8125 ASSERT(tcp->tcp_dstoptslen == 0); 8126 if (tcp->tcp_rtdstopts != NULL) { 8127 mi_free(tcp->tcp_rtdstopts); 8128 tcp->tcp_rtdstopts = NULL; 8129 tcp->tcp_rtdstoptslen = 0; 8130 } 8131 ASSERT(tcp->tcp_rtdstoptslen == 0); 8132 if (tcp->tcp_rthdr != NULL) { 8133 mi_free(tcp->tcp_rthdr); 8134 tcp->tcp_rthdr = NULL; 8135 tcp->tcp_rthdrlen = 0; 8136 } 8137 ASSERT(tcp->tcp_rthdrlen == 0); 8138 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8139 8140 /* Reset fusion-related fields */ 8141 tcp->tcp_fused = B_FALSE; 8142 tcp->tcp_unfusable = B_FALSE; 8143 tcp->tcp_fused_sigurg = B_FALSE; 8144 tcp->tcp_direct_sockfs = B_FALSE; 8145 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8146 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8147 tcp->tcp_loopback_peer = NULL; 8148 tcp->tcp_fuse_rcv_hiwater = 0; 8149 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8150 tcp->tcp_fuse_rcv_unread_cnt = 0; 8151 8152 tcp->tcp_lso = B_FALSE; 8153 8154 tcp->tcp_in_ack_unsent = 0; 8155 tcp->tcp_cork = B_FALSE; 8156 tcp->tcp_tconnind_started = B_FALSE; 8157 8158 PRESERVE(tcp->tcp_squeue_bytes); 8159 8160 ASSERT(tcp->tcp_kssl_ctx == NULL); 8161 ASSERT(!tcp->tcp_kssl_pending); 8162 PRESERVE(tcp->tcp_kssl_ent); 8163 8164 tcp->tcp_closemp_used = 0; 8165 8166 #ifdef DEBUG 8167 DONTCARE(tcp->tcmp_stk[0]); 8168 #endif 8169 8170 8171 #undef DONTCARE 8172 #undef PRESERVE 8173 } 8174 8175 /* 8176 * Allocate necessary resources and initialize state vector. 8177 * Guaranteed not to fail so that when an error is returned, 8178 * the caller doesn't need to do any additional cleanup. 8179 */ 8180 int 8181 tcp_init(tcp_t *tcp, queue_t *q) 8182 { 8183 int err; 8184 8185 tcp->tcp_rq = q; 8186 tcp->tcp_wq = WR(q); 8187 tcp->tcp_state = TCPS_IDLE; 8188 if ((err = tcp_init_values(tcp)) != 0) 8189 tcp_timers_stop(tcp); 8190 return (err); 8191 } 8192 8193 static int 8194 tcp_init_values(tcp_t *tcp) 8195 { 8196 int err; 8197 tcp_stack_t *tcps = tcp->tcp_tcps; 8198 8199 ASSERT((tcp->tcp_family == AF_INET && 8200 tcp->tcp_ipversion == IPV4_VERSION) || 8201 (tcp->tcp_family == AF_INET6 && 8202 (tcp->tcp_ipversion == IPV4_VERSION || 8203 tcp->tcp_ipversion == IPV6_VERSION))); 8204 8205 /* 8206 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8207 * will be close to tcp_rexmit_interval_initial. By doing this, we 8208 * allow the algorithm to adjust slowly to large fluctuations of RTT 8209 * during first few transmissions of a connection as seen in slow 8210 * links. 8211 */ 8212 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8213 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8214 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8215 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8216 tcps->tcps_conn_grace_period; 8217 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8218 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8219 tcp->tcp_timer_backoff = 0; 8220 tcp->tcp_ms_we_have_waited = 0; 8221 tcp->tcp_last_recv_time = lbolt; 8222 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8223 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8224 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8225 8226 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8227 8228 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8229 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8230 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8231 /* 8232 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8233 * passive open. 8234 */ 8235 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8236 8237 tcp->tcp_naglim = tcps->tcps_naglim_def; 8238 8239 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8240 8241 tcp->tcp_mdt_hdr_head = 0; 8242 tcp->tcp_mdt_hdr_tail = 0; 8243 8244 /* Reset fusion-related fields */ 8245 tcp->tcp_fused = B_FALSE; 8246 tcp->tcp_unfusable = B_FALSE; 8247 tcp->tcp_fused_sigurg = B_FALSE; 8248 tcp->tcp_direct_sockfs = B_FALSE; 8249 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8250 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8251 tcp->tcp_loopback_peer = NULL; 8252 tcp->tcp_fuse_rcv_hiwater = 0; 8253 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8254 tcp->tcp_fuse_rcv_unread_cnt = 0; 8255 8256 /* Initialize the header template */ 8257 if (tcp->tcp_ipversion == IPV4_VERSION) { 8258 err = tcp_header_init_ipv4(tcp); 8259 } else { 8260 err = tcp_header_init_ipv6(tcp); 8261 } 8262 if (err) 8263 return (err); 8264 8265 /* 8266 * Init the window scale to the max so tcp_rwnd_set() won't pare 8267 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8268 */ 8269 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8270 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8271 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8272 8273 tcp->tcp_cork = B_FALSE; 8274 /* 8275 * Init the tcp_debug option. This value determines whether TCP 8276 * calls strlog() to print out debug messages. Doing this 8277 * initialization here means that this value is not inherited thru 8278 * tcp_reinit(). 8279 */ 8280 tcp->tcp_debug = tcps->tcps_dbg; 8281 8282 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8283 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8284 8285 return (0); 8286 } 8287 8288 /* 8289 * Initialize the IPv4 header. Loses any record of any IP options. 8290 */ 8291 static int 8292 tcp_header_init_ipv4(tcp_t *tcp) 8293 { 8294 tcph_t *tcph; 8295 uint32_t sum; 8296 conn_t *connp; 8297 tcp_stack_t *tcps = tcp->tcp_tcps; 8298 8299 /* 8300 * This is a simple initialization. If there's 8301 * already a template, it should never be too small, 8302 * so reuse it. Otherwise, allocate space for the new one. 8303 */ 8304 if (tcp->tcp_iphc == NULL) { 8305 ASSERT(tcp->tcp_iphc_len == 0); 8306 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8307 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8308 if (tcp->tcp_iphc == NULL) { 8309 tcp->tcp_iphc_len = 0; 8310 return (ENOMEM); 8311 } 8312 } 8313 8314 /* options are gone; may need a new label */ 8315 connp = tcp->tcp_connp; 8316 connp->conn_mlp_type = mlptSingle; 8317 connp->conn_ulp_labeled = !is_system_labeled(); 8318 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8319 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8320 tcp->tcp_ip6h = NULL; 8321 tcp->tcp_ipversion = IPV4_VERSION; 8322 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8323 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8324 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8325 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8326 tcp->tcp_ipha->ipha_version_and_hdr_length 8327 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8328 tcp->tcp_ipha->ipha_ident = 0; 8329 8330 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8331 tcp->tcp_tos = 0; 8332 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8333 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8334 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8335 8336 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8337 tcp->tcp_tcph = tcph; 8338 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8339 /* 8340 * IP wants our header length in the checksum field to 8341 * allow it to perform a single pseudo-header+checksum 8342 * calculation on behalf of TCP. 8343 * Include the adjustment for a source route once IP_OPTIONS is set. 8344 */ 8345 sum = sizeof (tcph_t) + tcp->tcp_sum; 8346 sum = (sum >> 16) + (sum & 0xFFFF); 8347 U16_TO_ABE16(sum, tcph->th_sum); 8348 return (0); 8349 } 8350 8351 /* 8352 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8353 */ 8354 static int 8355 tcp_header_init_ipv6(tcp_t *tcp) 8356 { 8357 tcph_t *tcph; 8358 uint32_t sum; 8359 conn_t *connp; 8360 tcp_stack_t *tcps = tcp->tcp_tcps; 8361 8362 /* 8363 * This is a simple initialization. If there's 8364 * already a template, it should never be too small, 8365 * so reuse it. Otherwise, allocate space for the new one. 8366 * Ensure that there is enough space to "downgrade" the tcp_t 8367 * to an IPv4 tcp_t. This requires having space for a full load 8368 * of IPv4 options, as well as a full load of TCP options 8369 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8370 * than a v6 header and a TCP header with a full load of TCP options 8371 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8372 * We want to avoid reallocation in the "downgraded" case when 8373 * processing outbound IPv4 options. 8374 */ 8375 if (tcp->tcp_iphc == NULL) { 8376 ASSERT(tcp->tcp_iphc_len == 0); 8377 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8378 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8379 if (tcp->tcp_iphc == NULL) { 8380 tcp->tcp_iphc_len = 0; 8381 return (ENOMEM); 8382 } 8383 } 8384 8385 /* options are gone; may need a new label */ 8386 connp = tcp->tcp_connp; 8387 connp->conn_mlp_type = mlptSingle; 8388 connp->conn_ulp_labeled = !is_system_labeled(); 8389 8390 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8391 tcp->tcp_ipversion = IPV6_VERSION; 8392 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8393 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8394 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8395 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8396 tcp->tcp_ipha = NULL; 8397 8398 /* Initialize the header template */ 8399 8400 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8401 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8402 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8403 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8404 8405 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8406 tcp->tcp_tcph = tcph; 8407 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8408 /* 8409 * IP wants our header length in the checksum field to 8410 * allow it to perform a single psuedo-header+checksum 8411 * calculation on behalf of TCP. 8412 * Include the adjustment for a source route when IPV6_RTHDR is set. 8413 */ 8414 sum = sizeof (tcph_t) + tcp->tcp_sum; 8415 sum = (sum >> 16) + (sum & 0xFFFF); 8416 U16_TO_ABE16(sum, tcph->th_sum); 8417 return (0); 8418 } 8419 8420 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8421 #define ICMP_MIN_TCP_HDR 8 8422 8423 /* 8424 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8425 * passed up by IP. The message is always received on the correct tcp_t. 8426 * Assumes that IP has pulled up everything up to and including the ICMP header. 8427 */ 8428 void 8429 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8430 { 8431 icmph_t *icmph; 8432 ipha_t *ipha; 8433 int iph_hdr_length; 8434 tcph_t *tcph; 8435 boolean_t ipsec_mctl = B_FALSE; 8436 boolean_t secure; 8437 mblk_t *first_mp = mp; 8438 uint32_t new_mss; 8439 uint32_t ratio; 8440 size_t mp_size = MBLKL(mp); 8441 uint32_t seg_seq; 8442 tcp_stack_t *tcps = tcp->tcp_tcps; 8443 8444 /* Assume IP provides aligned packets - otherwise toss */ 8445 if (!OK_32PTR(mp->b_rptr)) { 8446 freemsg(mp); 8447 return; 8448 } 8449 8450 /* 8451 * Since ICMP errors are normal data marked with M_CTL when sent 8452 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8453 * packets starting with an ipsec_info_t, see ipsec_info.h. 8454 */ 8455 if ((mp_size == sizeof (ipsec_info_t)) && 8456 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8457 ASSERT(mp->b_cont != NULL); 8458 mp = mp->b_cont; 8459 /* IP should have done this */ 8460 ASSERT(OK_32PTR(mp->b_rptr)); 8461 mp_size = MBLKL(mp); 8462 ipsec_mctl = B_TRUE; 8463 } 8464 8465 /* 8466 * Verify that we have a complete outer IP header. If not, drop it. 8467 */ 8468 if (mp_size < sizeof (ipha_t)) { 8469 noticmpv4: 8470 freemsg(first_mp); 8471 return; 8472 } 8473 8474 ipha = (ipha_t *)mp->b_rptr; 8475 /* 8476 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8477 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8478 */ 8479 switch (IPH_HDR_VERSION(ipha)) { 8480 case IPV6_VERSION: 8481 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8482 return; 8483 case IPV4_VERSION: 8484 break; 8485 default: 8486 goto noticmpv4; 8487 } 8488 8489 /* Skip past the outer IP and ICMP headers */ 8490 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8491 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8492 /* 8493 * If we don't have the correct outer IP header length or if the ULP 8494 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8495 * send it upstream. 8496 */ 8497 if (iph_hdr_length < sizeof (ipha_t) || 8498 ipha->ipha_protocol != IPPROTO_ICMP || 8499 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8500 goto noticmpv4; 8501 } 8502 ipha = (ipha_t *)&icmph[1]; 8503 8504 /* Skip past the inner IP and find the ULP header */ 8505 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8506 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8507 /* 8508 * If we don't have the correct inner IP header length or if the ULP 8509 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8510 * bytes of TCP header, drop it. 8511 */ 8512 if (iph_hdr_length < sizeof (ipha_t) || 8513 ipha->ipha_protocol != IPPROTO_TCP || 8514 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8515 goto noticmpv4; 8516 } 8517 8518 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8519 if (ipsec_mctl) { 8520 secure = ipsec_in_is_secure(first_mp); 8521 } else { 8522 secure = B_FALSE; 8523 } 8524 if (secure) { 8525 /* 8526 * If we are willing to accept this in clear 8527 * we don't have to verify policy. 8528 */ 8529 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8530 if (!tcp_check_policy(tcp, first_mp, 8531 ipha, NULL, secure, ipsec_mctl)) { 8532 /* 8533 * tcp_check_policy called 8534 * ip_drop_packet() on failure. 8535 */ 8536 return; 8537 } 8538 } 8539 } 8540 } else if (ipsec_mctl) { 8541 /* 8542 * This is a hard_bound connection. IP has already 8543 * verified policy. We don't have to do it again. 8544 */ 8545 freeb(first_mp); 8546 first_mp = mp; 8547 ipsec_mctl = B_FALSE; 8548 } 8549 8550 seg_seq = ABE32_TO_U32(tcph->th_seq); 8551 /* 8552 * TCP SHOULD check that the TCP sequence number contained in 8553 * payload of the ICMP error message is within the range 8554 * SND.UNA <= SEG.SEQ < SND.NXT. 8555 */ 8556 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8557 /* 8558 * If the ICMP message is bogus, should we kill the 8559 * connection, or should we just drop the bogus ICMP 8560 * message? It would probably make more sense to just 8561 * drop the message so that if this one managed to get 8562 * in, the real connection should not suffer. 8563 */ 8564 goto noticmpv4; 8565 } 8566 8567 switch (icmph->icmph_type) { 8568 case ICMP_DEST_UNREACHABLE: 8569 switch (icmph->icmph_code) { 8570 case ICMP_FRAGMENTATION_NEEDED: 8571 /* 8572 * Reduce the MSS based on the new MTU. This will 8573 * eliminate any fragmentation locally. 8574 * N.B. There may well be some funny side-effects on 8575 * the local send policy and the remote receive policy. 8576 * Pending further research, we provide 8577 * tcp_ignore_path_mtu just in case this proves 8578 * disastrous somewhere. 8579 * 8580 * After updating the MSS, retransmit part of the 8581 * dropped segment using the new mss by calling 8582 * tcp_wput_data(). Need to adjust all those 8583 * params to make sure tcp_wput_data() work properly. 8584 */ 8585 if (tcps->tcps_ignore_path_mtu) 8586 break; 8587 8588 /* 8589 * Decrease the MSS by time stamp options 8590 * IP options and IPSEC options. tcp_hdr_len 8591 * includes time stamp option and IP option 8592 * length. 8593 */ 8594 8595 new_mss = ntohs(icmph->icmph_du_mtu) - 8596 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8597 8598 /* 8599 * Only update the MSS if the new one is 8600 * smaller than the previous one. This is 8601 * to avoid problems when getting multiple 8602 * ICMP errors for the same MTU. 8603 */ 8604 if (new_mss >= tcp->tcp_mss) 8605 break; 8606 8607 /* 8608 * Stop doing PMTU if new_mss is less than 68 8609 * or less than tcp_mss_min. 8610 * The value 68 comes from rfc 1191. 8611 */ 8612 if (new_mss < MAX(68, tcps->tcps_mss_min)) 8613 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8614 0; 8615 8616 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8617 ASSERT(ratio >= 1); 8618 tcp_mss_set(tcp, new_mss); 8619 8620 /* 8621 * Make sure we have something to 8622 * send. 8623 */ 8624 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8625 (tcp->tcp_xmit_head != NULL)) { 8626 /* 8627 * Shrink tcp_cwnd in 8628 * proportion to the old MSS/new MSS. 8629 */ 8630 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8631 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8632 (tcp->tcp_unsent == 0)) { 8633 tcp->tcp_rexmit_max = tcp->tcp_fss; 8634 } else { 8635 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8636 } 8637 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8638 tcp->tcp_rexmit = B_TRUE; 8639 tcp->tcp_dupack_cnt = 0; 8640 tcp->tcp_snd_burst = TCP_CWND_SS; 8641 tcp_ss_rexmit(tcp); 8642 } 8643 break; 8644 case ICMP_PORT_UNREACHABLE: 8645 case ICMP_PROTOCOL_UNREACHABLE: 8646 switch (tcp->tcp_state) { 8647 case TCPS_SYN_SENT: 8648 case TCPS_SYN_RCVD: 8649 /* 8650 * ICMP can snipe away incipient 8651 * TCP connections as long as 8652 * seq number is same as initial 8653 * send seq number. 8654 */ 8655 if (seg_seq == tcp->tcp_iss) { 8656 (void) tcp_clean_death(tcp, 8657 ECONNREFUSED, 6); 8658 } 8659 break; 8660 } 8661 break; 8662 case ICMP_HOST_UNREACHABLE: 8663 case ICMP_NET_UNREACHABLE: 8664 /* Record the error in case we finally time out. */ 8665 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8666 tcp->tcp_client_errno = EHOSTUNREACH; 8667 else 8668 tcp->tcp_client_errno = ENETUNREACH; 8669 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8670 if (tcp->tcp_listener != NULL && 8671 tcp->tcp_listener->tcp_syn_defense) { 8672 /* 8673 * Ditch the half-open connection if we 8674 * suspect a SYN attack is under way. 8675 */ 8676 tcp_ip_ire_mark_advice(tcp); 8677 (void) tcp_clean_death(tcp, 8678 tcp->tcp_client_errno, 7); 8679 } 8680 } 8681 break; 8682 default: 8683 break; 8684 } 8685 break; 8686 case ICMP_SOURCE_QUENCH: { 8687 /* 8688 * use a global boolean to control 8689 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8690 * The default is false. 8691 */ 8692 if (tcp_icmp_source_quench) { 8693 /* 8694 * Reduce the sending rate as if we got a 8695 * retransmit timeout 8696 */ 8697 uint32_t npkt; 8698 8699 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8700 tcp->tcp_mss; 8701 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8702 tcp->tcp_cwnd = tcp->tcp_mss; 8703 tcp->tcp_cwnd_cnt = 0; 8704 } 8705 break; 8706 } 8707 } 8708 freemsg(first_mp); 8709 } 8710 8711 /* 8712 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8713 * error messages passed up by IP. 8714 * Assumes that IP has pulled up all the extension headers as well 8715 * as the ICMPv6 header. 8716 */ 8717 static void 8718 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8719 { 8720 icmp6_t *icmp6; 8721 ip6_t *ip6h; 8722 uint16_t iph_hdr_length; 8723 tcpha_t *tcpha; 8724 uint8_t *nexthdrp; 8725 uint32_t new_mss; 8726 uint32_t ratio; 8727 boolean_t secure; 8728 mblk_t *first_mp = mp; 8729 size_t mp_size; 8730 uint32_t seg_seq; 8731 tcp_stack_t *tcps = tcp->tcp_tcps; 8732 8733 /* 8734 * The caller has determined if this is an IPSEC_IN packet and 8735 * set ipsec_mctl appropriately (see tcp_icmp_error). 8736 */ 8737 if (ipsec_mctl) 8738 mp = mp->b_cont; 8739 8740 mp_size = MBLKL(mp); 8741 8742 /* 8743 * Verify that we have a complete IP header. If not, send it upstream. 8744 */ 8745 if (mp_size < sizeof (ip6_t)) { 8746 noticmpv6: 8747 freemsg(first_mp); 8748 return; 8749 } 8750 8751 /* 8752 * Verify this is an ICMPV6 packet, else send it upstream. 8753 */ 8754 ip6h = (ip6_t *)mp->b_rptr; 8755 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8756 iph_hdr_length = IPV6_HDR_LEN; 8757 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8758 &nexthdrp) || 8759 *nexthdrp != IPPROTO_ICMPV6) { 8760 goto noticmpv6; 8761 } 8762 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8763 ip6h = (ip6_t *)&icmp6[1]; 8764 /* 8765 * Verify if we have a complete ICMP and inner IP header. 8766 */ 8767 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8768 goto noticmpv6; 8769 8770 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8771 goto noticmpv6; 8772 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8773 /* 8774 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8775 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8776 * packet. 8777 */ 8778 if ((*nexthdrp != IPPROTO_TCP) || 8779 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8780 goto noticmpv6; 8781 } 8782 8783 /* 8784 * ICMP errors come on the right queue or come on 8785 * listener/global queue for detached connections and 8786 * get switched to the right queue. If it comes on the 8787 * right queue, policy check has already been done by IP 8788 * and thus free the first_mp without verifying the policy. 8789 * If it has come for a non-hard bound connection, we need 8790 * to verify policy as IP may not have done it. 8791 */ 8792 if (!tcp->tcp_hard_bound) { 8793 if (ipsec_mctl) { 8794 secure = ipsec_in_is_secure(first_mp); 8795 } else { 8796 secure = B_FALSE; 8797 } 8798 if (secure) { 8799 /* 8800 * If we are willing to accept this in clear 8801 * we don't have to verify policy. 8802 */ 8803 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8804 if (!tcp_check_policy(tcp, first_mp, 8805 NULL, ip6h, secure, ipsec_mctl)) { 8806 /* 8807 * tcp_check_policy called 8808 * ip_drop_packet() on failure. 8809 */ 8810 return; 8811 } 8812 } 8813 } 8814 } else if (ipsec_mctl) { 8815 /* 8816 * This is a hard_bound connection. IP has already 8817 * verified policy. We don't have to do it again. 8818 */ 8819 freeb(first_mp); 8820 first_mp = mp; 8821 ipsec_mctl = B_FALSE; 8822 } 8823 8824 seg_seq = ntohl(tcpha->tha_seq); 8825 /* 8826 * TCP SHOULD check that the TCP sequence number contained in 8827 * payload of the ICMP error message is within the range 8828 * SND.UNA <= SEG.SEQ < SND.NXT. 8829 */ 8830 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8831 /* 8832 * If the ICMP message is bogus, should we kill the 8833 * connection, or should we just drop the bogus ICMP 8834 * message? It would probably make more sense to just 8835 * drop the message so that if this one managed to get 8836 * in, the real connection should not suffer. 8837 */ 8838 goto noticmpv6; 8839 } 8840 8841 switch (icmp6->icmp6_type) { 8842 case ICMP6_PACKET_TOO_BIG: 8843 /* 8844 * Reduce the MSS based on the new MTU. This will 8845 * eliminate any fragmentation locally. 8846 * N.B. There may well be some funny side-effects on 8847 * the local send policy and the remote receive policy. 8848 * Pending further research, we provide 8849 * tcp_ignore_path_mtu just in case this proves 8850 * disastrous somewhere. 8851 * 8852 * After updating the MSS, retransmit part of the 8853 * dropped segment using the new mss by calling 8854 * tcp_wput_data(). Need to adjust all those 8855 * params to make sure tcp_wput_data() work properly. 8856 */ 8857 if (tcps->tcps_ignore_path_mtu) 8858 break; 8859 8860 /* 8861 * Decrease the MSS by time stamp options 8862 * IP options and IPSEC options. tcp_hdr_len 8863 * includes time stamp option and IP option 8864 * length. 8865 */ 8866 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8867 tcp->tcp_ipsec_overhead; 8868 8869 /* 8870 * Only update the MSS if the new one is 8871 * smaller than the previous one. This is 8872 * to avoid problems when getting multiple 8873 * ICMP errors for the same MTU. 8874 */ 8875 if (new_mss >= tcp->tcp_mss) 8876 break; 8877 8878 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8879 ASSERT(ratio >= 1); 8880 tcp_mss_set(tcp, new_mss); 8881 8882 /* 8883 * Make sure we have something to 8884 * send. 8885 */ 8886 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8887 (tcp->tcp_xmit_head != NULL)) { 8888 /* 8889 * Shrink tcp_cwnd in 8890 * proportion to the old MSS/new MSS. 8891 */ 8892 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8893 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8894 (tcp->tcp_unsent == 0)) { 8895 tcp->tcp_rexmit_max = tcp->tcp_fss; 8896 } else { 8897 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8898 } 8899 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8900 tcp->tcp_rexmit = B_TRUE; 8901 tcp->tcp_dupack_cnt = 0; 8902 tcp->tcp_snd_burst = TCP_CWND_SS; 8903 tcp_ss_rexmit(tcp); 8904 } 8905 break; 8906 8907 case ICMP6_DST_UNREACH: 8908 switch (icmp6->icmp6_code) { 8909 case ICMP6_DST_UNREACH_NOPORT: 8910 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8911 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8912 (seg_seq == tcp->tcp_iss)) { 8913 (void) tcp_clean_death(tcp, 8914 ECONNREFUSED, 8); 8915 } 8916 break; 8917 8918 case ICMP6_DST_UNREACH_ADMIN: 8919 case ICMP6_DST_UNREACH_NOROUTE: 8920 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8921 case ICMP6_DST_UNREACH_ADDR: 8922 /* Record the error in case we finally time out. */ 8923 tcp->tcp_client_errno = EHOSTUNREACH; 8924 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8925 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8926 (seg_seq == tcp->tcp_iss)) { 8927 if (tcp->tcp_listener != NULL && 8928 tcp->tcp_listener->tcp_syn_defense) { 8929 /* 8930 * Ditch the half-open connection if we 8931 * suspect a SYN attack is under way. 8932 */ 8933 tcp_ip_ire_mark_advice(tcp); 8934 (void) tcp_clean_death(tcp, 8935 tcp->tcp_client_errno, 9); 8936 } 8937 } 8938 8939 8940 break; 8941 default: 8942 break; 8943 } 8944 break; 8945 8946 case ICMP6_PARAM_PROB: 8947 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8948 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8949 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8950 (uchar_t *)nexthdrp) { 8951 if (tcp->tcp_state == TCPS_SYN_SENT || 8952 tcp->tcp_state == TCPS_SYN_RCVD) { 8953 (void) tcp_clean_death(tcp, 8954 ECONNREFUSED, 10); 8955 } 8956 break; 8957 } 8958 break; 8959 8960 case ICMP6_TIME_EXCEEDED: 8961 default: 8962 break; 8963 } 8964 freemsg(first_mp); 8965 } 8966 8967 /* 8968 * IP recognizes seven kinds of bind requests: 8969 * 8970 * - A zero-length address binds only to the protocol number. 8971 * 8972 * - A 4-byte address is treated as a request to 8973 * validate that the address is a valid local IPv4 8974 * address, appropriate for an application to bind to. 8975 * IP does the verification, but does not make any note 8976 * of the address at this time. 8977 * 8978 * - A 16-byte address contains is treated as a request 8979 * to validate a local IPv6 address, as the 4-byte 8980 * address case above. 8981 * 8982 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 8983 * use it for the inbound fanout of packets. 8984 * 8985 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 8986 * use it for the inbound fanout of packets. 8987 * 8988 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 8989 * information consisting of local and remote addresses 8990 * and ports. In this case, the addresses are both 8991 * validated as appropriate for this operation, and, if 8992 * so, the information is retained for use in the 8993 * inbound fanout. 8994 * 8995 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 8996 * fanout information, like the 12-byte case above. 8997 * 8998 * IP will also fill in the IRE request mblk with information 8999 * regarding our peer. In all cases, we notify IP of our protocol 9000 * type by appending a single protocol byte to the bind request. 9001 */ 9002 static mblk_t * 9003 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 9004 { 9005 char *cp; 9006 mblk_t *mp; 9007 struct T_bind_req *tbr; 9008 ipa_conn_t *ac; 9009 ipa6_conn_t *ac6; 9010 sin_t *sin; 9011 sin6_t *sin6; 9012 9013 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9014 ASSERT((tcp->tcp_family == AF_INET && 9015 tcp->tcp_ipversion == IPV4_VERSION) || 9016 (tcp->tcp_family == AF_INET6 && 9017 (tcp->tcp_ipversion == IPV4_VERSION || 9018 tcp->tcp_ipversion == IPV6_VERSION))); 9019 9020 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9021 if (!mp) 9022 return (mp); 9023 mp->b_datap->db_type = M_PROTO; 9024 tbr = (struct T_bind_req *)mp->b_rptr; 9025 tbr->PRIM_type = bind_prim; 9026 tbr->ADDR_offset = sizeof (*tbr); 9027 tbr->CONIND_number = 0; 9028 tbr->ADDR_length = addr_length; 9029 cp = (char *)&tbr[1]; 9030 switch (addr_length) { 9031 case sizeof (ipa_conn_t): 9032 ASSERT(tcp->tcp_family == AF_INET); 9033 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9034 9035 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9036 if (mp->b_cont == NULL) { 9037 freemsg(mp); 9038 return (NULL); 9039 } 9040 mp->b_cont->b_wptr += sizeof (ire_t); 9041 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9042 9043 /* cp known to be 32 bit aligned */ 9044 ac = (ipa_conn_t *)cp; 9045 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9046 ac->ac_faddr = tcp->tcp_remote; 9047 ac->ac_fport = tcp->tcp_fport; 9048 ac->ac_lport = tcp->tcp_lport; 9049 tcp->tcp_hard_binding = 1; 9050 break; 9051 9052 case sizeof (ipa6_conn_t): 9053 ASSERT(tcp->tcp_family == AF_INET6); 9054 9055 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9056 if (mp->b_cont == NULL) { 9057 freemsg(mp); 9058 return (NULL); 9059 } 9060 mp->b_cont->b_wptr += sizeof (ire_t); 9061 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9062 9063 /* cp known to be 32 bit aligned */ 9064 ac6 = (ipa6_conn_t *)cp; 9065 if (tcp->tcp_ipversion == IPV4_VERSION) { 9066 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9067 &ac6->ac6_laddr); 9068 } else { 9069 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9070 } 9071 ac6->ac6_faddr = tcp->tcp_remote_v6; 9072 ac6->ac6_fport = tcp->tcp_fport; 9073 ac6->ac6_lport = tcp->tcp_lport; 9074 tcp->tcp_hard_binding = 1; 9075 break; 9076 9077 case sizeof (sin_t): 9078 /* 9079 * NOTE: IPV6_ADDR_LEN also has same size. 9080 * Use family to discriminate. 9081 */ 9082 if (tcp->tcp_family == AF_INET) { 9083 sin = (sin_t *)cp; 9084 9085 *sin = sin_null; 9086 sin->sin_family = AF_INET; 9087 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9088 sin->sin_port = tcp->tcp_lport; 9089 break; 9090 } else { 9091 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9092 } 9093 break; 9094 9095 case sizeof (sin6_t): 9096 ASSERT(tcp->tcp_family == AF_INET6); 9097 sin6 = (sin6_t *)cp; 9098 9099 *sin6 = sin6_null; 9100 sin6->sin6_family = AF_INET6; 9101 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9102 sin6->sin6_port = tcp->tcp_lport; 9103 break; 9104 9105 case IP_ADDR_LEN: 9106 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9107 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9108 break; 9109 9110 } 9111 /* Add protocol number to end */ 9112 cp[addr_length] = (char)IPPROTO_TCP; 9113 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9114 return (mp); 9115 } 9116 9117 /* 9118 * Notify IP that we are having trouble with this connection. IP should 9119 * blow the IRE away and start over. 9120 */ 9121 static void 9122 tcp_ip_notify(tcp_t *tcp) 9123 { 9124 struct iocblk *iocp; 9125 ipid_t *ipid; 9126 mblk_t *mp; 9127 9128 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9129 if (tcp->tcp_ipversion == IPV6_VERSION) 9130 return; 9131 9132 mp = mkiocb(IP_IOCTL); 9133 if (mp == NULL) 9134 return; 9135 9136 iocp = (struct iocblk *)mp->b_rptr; 9137 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9138 9139 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9140 if (!mp->b_cont) { 9141 freeb(mp); 9142 return; 9143 } 9144 9145 ipid = (ipid_t *)mp->b_cont->b_rptr; 9146 mp->b_cont->b_wptr += iocp->ioc_count; 9147 bzero(ipid, sizeof (*ipid)); 9148 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9149 ipid->ipid_ire_type = IRE_CACHE; 9150 ipid->ipid_addr_offset = sizeof (ipid_t); 9151 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9152 /* 9153 * Note: in the case of source routing we want to blow away the 9154 * route to the first source route hop. 9155 */ 9156 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9157 sizeof (tcp->tcp_ipha->ipha_dst)); 9158 9159 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9160 } 9161 9162 /* Unlink and return any mblk that looks like it contains an ire */ 9163 static mblk_t * 9164 tcp_ire_mp(mblk_t *mp) 9165 { 9166 mblk_t *prev_mp; 9167 9168 for (;;) { 9169 prev_mp = mp; 9170 mp = mp->b_cont; 9171 if (mp == NULL) 9172 break; 9173 switch (DB_TYPE(mp)) { 9174 case IRE_DB_TYPE: 9175 case IRE_DB_REQ_TYPE: 9176 if (prev_mp != NULL) 9177 prev_mp->b_cont = mp->b_cont; 9178 mp->b_cont = NULL; 9179 return (mp); 9180 default: 9181 break; 9182 } 9183 } 9184 return (mp); 9185 } 9186 9187 /* 9188 * Timer callback routine for keepalive probe. We do a fake resend of 9189 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9190 * check to see if we have heard anything from the other end for the last 9191 * RTO period. If we have, set the timer to expire for another 9192 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9193 * RTO << 1 and check again when it expires. Keep exponentially increasing 9194 * the timeout if we have not heard from the other side. If for more than 9195 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9196 * kill the connection unless the keepalive abort threshold is 0. In 9197 * that case, we will probe "forever." 9198 */ 9199 static void 9200 tcp_keepalive_killer(void *arg) 9201 { 9202 mblk_t *mp; 9203 conn_t *connp = (conn_t *)arg; 9204 tcp_t *tcp = connp->conn_tcp; 9205 int32_t firetime; 9206 int32_t idletime; 9207 int32_t ka_intrvl; 9208 tcp_stack_t *tcps = tcp->tcp_tcps; 9209 9210 tcp->tcp_ka_tid = 0; 9211 9212 if (tcp->tcp_fused) 9213 return; 9214 9215 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9216 ka_intrvl = tcp->tcp_ka_interval; 9217 9218 /* 9219 * Keepalive probe should only be sent if the application has not 9220 * done a close on the connection. 9221 */ 9222 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9223 return; 9224 } 9225 /* Timer fired too early, restart it. */ 9226 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9227 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9228 MSEC_TO_TICK(ka_intrvl)); 9229 return; 9230 } 9231 9232 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9233 /* 9234 * If we have not heard from the other side for a long 9235 * time, kill the connection unless the keepalive abort 9236 * threshold is 0. In that case, we will probe "forever." 9237 */ 9238 if (tcp->tcp_ka_abort_thres != 0 && 9239 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9240 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9241 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9242 tcp->tcp_client_errno : ETIMEDOUT, 11); 9243 return; 9244 } 9245 9246 if (tcp->tcp_snxt == tcp->tcp_suna && 9247 idletime >= ka_intrvl) { 9248 /* Fake resend of last ACKed byte. */ 9249 mblk_t *mp1 = allocb(1, BPRI_LO); 9250 9251 if (mp1 != NULL) { 9252 *mp1->b_wptr++ = '\0'; 9253 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9254 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9255 freeb(mp1); 9256 /* 9257 * if allocation failed, fall through to start the 9258 * timer back. 9259 */ 9260 if (mp != NULL) { 9261 TCP_RECORD_TRACE(tcp, mp, 9262 TCP_TRACE_SEND_PKT); 9263 tcp_send_data(tcp, tcp->tcp_wq, mp); 9264 BUMP_MIB(&tcps->tcps_mib, 9265 tcpTimKeepaliveProbe); 9266 if (tcp->tcp_ka_last_intrvl != 0) { 9267 int max; 9268 /* 9269 * We should probe again at least 9270 * in ka_intrvl, but not more than 9271 * tcp_rexmit_interval_max. 9272 */ 9273 max = tcps->tcps_rexmit_interval_max; 9274 firetime = MIN(ka_intrvl - 1, 9275 tcp->tcp_ka_last_intrvl << 1); 9276 if (firetime > max) 9277 firetime = max; 9278 } else { 9279 firetime = tcp->tcp_rto; 9280 } 9281 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9282 tcp_keepalive_killer, 9283 MSEC_TO_TICK(firetime)); 9284 tcp->tcp_ka_last_intrvl = firetime; 9285 return; 9286 } 9287 } 9288 } else { 9289 tcp->tcp_ka_last_intrvl = 0; 9290 } 9291 9292 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9293 if ((firetime = ka_intrvl - idletime) < 0) { 9294 firetime = ka_intrvl; 9295 } 9296 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9297 MSEC_TO_TICK(firetime)); 9298 } 9299 9300 int 9301 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9302 { 9303 queue_t *q = tcp->tcp_rq; 9304 int32_t mss = tcp->tcp_mss; 9305 int maxpsz; 9306 9307 if (TCP_IS_DETACHED(tcp)) 9308 return (mss); 9309 9310 if (tcp->tcp_fused) { 9311 maxpsz = tcp_fuse_maxpsz_set(tcp); 9312 mss = INFPSZ; 9313 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9314 /* 9315 * Set the sd_qn_maxpsz according to the socket send buffer 9316 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9317 * instruct the stream head to copyin user data into contiguous 9318 * kernel-allocated buffers without breaking it up into smaller 9319 * chunks. We round up the buffer size to the nearest SMSS. 9320 */ 9321 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9322 if (tcp->tcp_kssl_ctx == NULL) 9323 mss = INFPSZ; 9324 else 9325 mss = SSL3_MAX_RECORD_LEN; 9326 } else { 9327 /* 9328 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9329 * (and a multiple of the mss). This instructs the stream 9330 * head to break down larger than SMSS writes into SMSS- 9331 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9332 */ 9333 maxpsz = tcp->tcp_maxpsz * mss; 9334 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9335 maxpsz = tcp->tcp_xmit_hiwater/2; 9336 /* Round up to nearest mss */ 9337 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9338 } 9339 } 9340 (void) setmaxps(q, maxpsz); 9341 tcp->tcp_wq->q_maxpsz = maxpsz; 9342 9343 if (set_maxblk) 9344 (void) mi_set_sth_maxblk(q, mss); 9345 9346 return (mss); 9347 } 9348 9349 /* 9350 * Extract option values from a tcp header. We put any found values into the 9351 * tcpopt struct and return a bitmask saying which options were found. 9352 */ 9353 static int 9354 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9355 { 9356 uchar_t *endp; 9357 int len; 9358 uint32_t mss; 9359 uchar_t *up = (uchar_t *)tcph; 9360 int found = 0; 9361 int32_t sack_len; 9362 tcp_seq sack_begin, sack_end; 9363 tcp_t *tcp; 9364 9365 endp = up + TCP_HDR_LENGTH(tcph); 9366 up += TCP_MIN_HEADER_LENGTH; 9367 while (up < endp) { 9368 len = endp - up; 9369 switch (*up) { 9370 case TCPOPT_EOL: 9371 break; 9372 9373 case TCPOPT_NOP: 9374 up++; 9375 continue; 9376 9377 case TCPOPT_MAXSEG: 9378 if (len < TCPOPT_MAXSEG_LEN || 9379 up[1] != TCPOPT_MAXSEG_LEN) 9380 break; 9381 9382 mss = BE16_TO_U16(up+2); 9383 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9384 tcpopt->tcp_opt_mss = mss; 9385 found |= TCP_OPT_MSS_PRESENT; 9386 9387 up += TCPOPT_MAXSEG_LEN; 9388 continue; 9389 9390 case TCPOPT_WSCALE: 9391 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9392 break; 9393 9394 if (up[2] > TCP_MAX_WINSHIFT) 9395 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9396 else 9397 tcpopt->tcp_opt_wscale = up[2]; 9398 found |= TCP_OPT_WSCALE_PRESENT; 9399 9400 up += TCPOPT_WS_LEN; 9401 continue; 9402 9403 case TCPOPT_SACK_PERMITTED: 9404 if (len < TCPOPT_SACK_OK_LEN || 9405 up[1] != TCPOPT_SACK_OK_LEN) 9406 break; 9407 found |= TCP_OPT_SACK_OK_PRESENT; 9408 up += TCPOPT_SACK_OK_LEN; 9409 continue; 9410 9411 case TCPOPT_SACK: 9412 if (len <= 2 || up[1] <= 2 || len < up[1]) 9413 break; 9414 9415 /* If TCP is not interested in SACK blks... */ 9416 if ((tcp = tcpopt->tcp) == NULL) { 9417 up += up[1]; 9418 continue; 9419 } 9420 sack_len = up[1] - TCPOPT_HEADER_LEN; 9421 up += TCPOPT_HEADER_LEN; 9422 9423 /* 9424 * If the list is empty, allocate one and assume 9425 * nothing is sack'ed. 9426 */ 9427 ASSERT(tcp->tcp_sack_info != NULL); 9428 if (tcp->tcp_notsack_list == NULL) { 9429 tcp_notsack_update(&(tcp->tcp_notsack_list), 9430 tcp->tcp_suna, tcp->tcp_snxt, 9431 &(tcp->tcp_num_notsack_blk), 9432 &(tcp->tcp_cnt_notsack_list)); 9433 9434 /* 9435 * Make sure tcp_notsack_list is not NULL. 9436 * This happens when kmem_alloc(KM_NOSLEEP) 9437 * returns NULL. 9438 */ 9439 if (tcp->tcp_notsack_list == NULL) { 9440 up += sack_len; 9441 continue; 9442 } 9443 tcp->tcp_fack = tcp->tcp_suna; 9444 } 9445 9446 while (sack_len > 0) { 9447 if (up + 8 > endp) { 9448 up = endp; 9449 break; 9450 } 9451 sack_begin = BE32_TO_U32(up); 9452 up += 4; 9453 sack_end = BE32_TO_U32(up); 9454 up += 4; 9455 sack_len -= 8; 9456 /* 9457 * Bounds checking. Make sure the SACK 9458 * info is within tcp_suna and tcp_snxt. 9459 * If this SACK blk is out of bound, ignore 9460 * it but continue to parse the following 9461 * blks. 9462 */ 9463 if (SEQ_LEQ(sack_end, sack_begin) || 9464 SEQ_LT(sack_begin, tcp->tcp_suna) || 9465 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9466 continue; 9467 } 9468 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9469 sack_begin, sack_end, 9470 &(tcp->tcp_num_notsack_blk), 9471 &(tcp->tcp_cnt_notsack_list)); 9472 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9473 tcp->tcp_fack = sack_end; 9474 } 9475 } 9476 found |= TCP_OPT_SACK_PRESENT; 9477 continue; 9478 9479 case TCPOPT_TSTAMP: 9480 if (len < TCPOPT_TSTAMP_LEN || 9481 up[1] != TCPOPT_TSTAMP_LEN) 9482 break; 9483 9484 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9485 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9486 9487 found |= TCP_OPT_TSTAMP_PRESENT; 9488 9489 up += TCPOPT_TSTAMP_LEN; 9490 continue; 9491 9492 default: 9493 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9494 break; 9495 up += up[1]; 9496 continue; 9497 } 9498 break; 9499 } 9500 return (found); 9501 } 9502 9503 /* 9504 * Set the mss associated with a particular tcp based on its current value, 9505 * and a new one passed in. Observe minimums and maximums, and reset 9506 * other state variables that we want to view as multiples of mss. 9507 * 9508 * This function is called in various places mainly because 9509 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9510 * other side's SYN/SYN-ACK packet arrives. 9511 * 2) PMTUd may get us a new MSS. 9512 * 3) If the other side stops sending us timestamp option, we need to 9513 * increase the MSS size to use the extra bytes available. 9514 */ 9515 static void 9516 tcp_mss_set(tcp_t *tcp, uint32_t mss) 9517 { 9518 uint32_t mss_max; 9519 tcp_stack_t *tcps = tcp->tcp_tcps; 9520 9521 if (tcp->tcp_ipversion == IPV4_VERSION) 9522 mss_max = tcps->tcps_mss_max_ipv4; 9523 else 9524 mss_max = tcps->tcps_mss_max_ipv6; 9525 9526 if (mss < tcps->tcps_mss_min) 9527 mss = tcps->tcps_mss_min; 9528 if (mss > mss_max) 9529 mss = mss_max; 9530 /* 9531 * Unless naglim has been set by our client to 9532 * a non-mss value, force naglim to track mss. 9533 * This can help to aggregate small writes. 9534 */ 9535 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9536 tcp->tcp_naglim = mss; 9537 /* 9538 * TCP should be able to buffer at least 4 MSS data for obvious 9539 * performance reason. 9540 */ 9541 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9542 tcp->tcp_xmit_hiwater = mss << 2; 9543 9544 /* 9545 * Check if we need to apply the tcp_init_cwnd here. If 9546 * it is set and the MSS gets bigger (should not happen 9547 * normally), we need to adjust the resulting tcp_cwnd properly. 9548 * The new tcp_cwnd should not get bigger. 9549 */ 9550 if (tcp->tcp_init_cwnd == 0) { 9551 tcp->tcp_cwnd = MIN(tcps->tcps_slow_start_initial * mss, 9552 MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9553 } else { 9554 if (tcp->tcp_mss < mss) { 9555 tcp->tcp_cwnd = MAX(1, 9556 (tcp->tcp_init_cwnd * tcp->tcp_mss / mss)) * mss; 9557 } else { 9558 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9559 } 9560 } 9561 tcp->tcp_mss = mss; 9562 tcp->tcp_cwnd_cnt = 0; 9563 (void) tcp_maxpsz_set(tcp, B_TRUE); 9564 } 9565 9566 static int 9567 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9568 { 9569 tcp_t *tcp = NULL; 9570 conn_t *connp; 9571 int err; 9572 dev_t conn_dev; 9573 zoneid_t zoneid; 9574 tcp_stack_t *tcps = NULL; 9575 9576 if (q->q_ptr != NULL) 9577 return (0); 9578 9579 if (!(flag & SO_ACCEPTOR)) { 9580 /* 9581 * Special case for install: miniroot needs to be able to 9582 * access files via NFS as though it were always in the 9583 * global zone. 9584 */ 9585 if (credp == kcred && nfs_global_client_only != 0) { 9586 zoneid = GLOBAL_ZONEID; 9587 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9588 netstack_tcp; 9589 ASSERT(tcps != NULL); 9590 } else { 9591 netstack_t *ns; 9592 9593 ns = netstack_find_by_cred(credp); 9594 ASSERT(ns != NULL); 9595 tcps = ns->netstack_tcp; 9596 ASSERT(tcps != NULL); 9597 9598 /* 9599 * For exclusive stacks we set the zoneid to zero 9600 * to make TCP operate as if in the global zone. 9601 */ 9602 if (tcps->tcps_netstack->netstack_stackid != 9603 GLOBAL_NETSTACKID) 9604 zoneid = GLOBAL_ZONEID; 9605 else 9606 zoneid = crgetzoneid(credp); 9607 } 9608 /* 9609 * For stackid zero this is done from strplumb.c, but 9610 * non-zero stackids are handled here. 9611 */ 9612 if (tcps->tcps_g_q == NULL && 9613 tcps->tcps_netstack->netstack_stackid != 9614 GLOBAL_NETSTACKID) { 9615 tcp_g_q_setup(tcps); 9616 } 9617 } 9618 if (sflag == MODOPEN) { 9619 /* 9620 * This is a special case. The purpose of a modopen 9621 * is to allow just the T_SVR4_OPTMGMT_REQ to pass 9622 * through for MIB browsers. Everything else is failed. 9623 */ 9624 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9625 /* tcp_get_conn incremented refcnt */ 9626 netstack_rele(tcps->tcps_netstack); 9627 9628 if (connp == NULL) 9629 return (ENOMEM); 9630 9631 connp->conn_flags |= IPCL_TCPMOD; 9632 connp->conn_cred = credp; 9633 connp->conn_zoneid = zoneid; 9634 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9635 ASSERT(connp->conn_netstack->netstack_tcp == tcps); 9636 q->q_ptr = WR(q)->q_ptr = connp; 9637 crhold(credp); 9638 q->q_qinfo = &tcp_mod_rinit; 9639 WR(q)->q_qinfo = &tcp_mod_winit; 9640 qprocson(q); 9641 return (0); 9642 } 9643 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 9644 if (tcps != NULL) 9645 netstack_rele(tcps->tcps_netstack); 9646 return (EBUSY); 9647 } 9648 9649 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9650 9651 if (flag & SO_ACCEPTOR) { 9652 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9653 ASSERT(tcps == NULL); 9654 q->q_qinfo = &tcp_acceptor_rinit; 9655 q->q_ptr = (void *)conn_dev; 9656 WR(q)->q_qinfo = &tcp_acceptor_winit; 9657 WR(q)->q_ptr = (void *)conn_dev; 9658 qprocson(q); 9659 return (0); 9660 } 9661 9662 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9663 /* 9664 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9665 * so we drop it by one. 9666 */ 9667 netstack_rele(tcps->tcps_netstack); 9668 if (connp == NULL) { 9669 inet_minor_free(ip_minor_arena, conn_dev); 9670 q->q_ptr = NULL; 9671 return (ENOSR); 9672 } 9673 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9674 tcp = connp->conn_tcp; 9675 9676 q->q_ptr = WR(q)->q_ptr = connp; 9677 if (getmajor(*devp) == TCP6_MAJ) { 9678 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9679 connp->conn_send = ip_output_v6; 9680 connp->conn_af_isv6 = B_TRUE; 9681 connp->conn_pkt_isv6 = B_TRUE; 9682 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9683 tcp->tcp_ipversion = IPV6_VERSION; 9684 tcp->tcp_family = AF_INET6; 9685 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9686 } else { 9687 connp->conn_flags |= IPCL_TCP4; 9688 connp->conn_send = ip_output; 9689 connp->conn_af_isv6 = B_FALSE; 9690 connp->conn_pkt_isv6 = B_FALSE; 9691 tcp->tcp_ipversion = IPV4_VERSION; 9692 tcp->tcp_family = AF_INET; 9693 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9694 } 9695 9696 /* 9697 * TCP keeps a copy of cred for cache locality reasons but 9698 * we put a reference only once. If connp->conn_cred 9699 * becomes invalid, tcp_cred should also be set to NULL. 9700 */ 9701 tcp->tcp_cred = connp->conn_cred = credp; 9702 crhold(connp->conn_cred); 9703 tcp->tcp_cpid = curproc->p_pid; 9704 tcp->tcp_open_time = lbolt64; 9705 connp->conn_zoneid = zoneid; 9706 connp->conn_mlp_type = mlptSingle; 9707 connp->conn_ulp_labeled = !is_system_labeled(); 9708 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9709 ASSERT(tcp->tcp_tcps == tcps); 9710 9711 /* 9712 * If the caller has the process-wide flag set, then default to MAC 9713 * exempt mode. This allows read-down to unlabeled hosts. 9714 */ 9715 if (getpflags(NET_MAC_AWARE, credp) != 0) 9716 connp->conn_mac_exempt = B_TRUE; 9717 9718 connp->conn_dev = conn_dev; 9719 9720 ASSERT(q->q_qinfo == &tcp_rinit); 9721 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9722 9723 if (flag & SO_SOCKSTR) { 9724 /* 9725 * No need to insert a socket in tcp acceptor hash. 9726 * If it was a socket acceptor stream, we dealt with 9727 * it above. A socket listener can never accept a 9728 * connection and doesn't need acceptor_id. 9729 */ 9730 connp->conn_flags |= IPCL_SOCKET; 9731 tcp->tcp_issocket = 1; 9732 WR(q)->q_qinfo = &tcp_sock_winit; 9733 } else { 9734 #ifdef _ILP32 9735 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9736 #else 9737 tcp->tcp_acceptor_id = conn_dev; 9738 #endif /* _ILP32 */ 9739 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9740 } 9741 9742 if (tcps->tcps_trace) 9743 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9744 9745 err = tcp_init(tcp, q); 9746 if (err != 0) { 9747 inet_minor_free(ip_minor_arena, connp->conn_dev); 9748 tcp_acceptor_hash_remove(tcp); 9749 CONN_DEC_REF(connp); 9750 q->q_ptr = WR(q)->q_ptr = NULL; 9751 return (err); 9752 } 9753 9754 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9755 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9756 9757 /* Non-zero default values */ 9758 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9759 /* 9760 * Put the ref for TCP. Ref for IP was already put 9761 * by ipcl_conn_create. Also Make the conn_t globally 9762 * visible to walkers 9763 */ 9764 mutex_enter(&connp->conn_lock); 9765 CONN_INC_REF_LOCKED(connp); 9766 ASSERT(connp->conn_ref == 2); 9767 connp->conn_state_flags &= ~CONN_INCIPIENT; 9768 mutex_exit(&connp->conn_lock); 9769 9770 qprocson(q); 9771 return (0); 9772 } 9773 9774 /* 9775 * Some TCP options can be "set" by requesting them in the option 9776 * buffer. This is needed for XTI feature test though we do not 9777 * allow it in general. We interpret that this mechanism is more 9778 * applicable to OSI protocols and need not be allowed in general. 9779 * This routine filters out options for which it is not allowed (most) 9780 * and lets through those (few) for which it is. [ The XTI interface 9781 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9782 * ever implemented will have to be allowed here ]. 9783 */ 9784 static boolean_t 9785 tcp_allow_connopt_set(int level, int name) 9786 { 9787 9788 switch (level) { 9789 case IPPROTO_TCP: 9790 switch (name) { 9791 case TCP_NODELAY: 9792 return (B_TRUE); 9793 default: 9794 return (B_FALSE); 9795 } 9796 /*NOTREACHED*/ 9797 default: 9798 return (B_FALSE); 9799 } 9800 /*NOTREACHED*/ 9801 } 9802 9803 /* 9804 * This routine gets default values of certain options whose default 9805 * values are maintained by protocol specific code 9806 */ 9807 /* ARGSUSED */ 9808 int 9809 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9810 { 9811 int32_t *i1 = (int32_t *)ptr; 9812 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9813 9814 switch (level) { 9815 case IPPROTO_TCP: 9816 switch (name) { 9817 case TCP_NOTIFY_THRESHOLD: 9818 *i1 = tcps->tcps_ip_notify_interval; 9819 break; 9820 case TCP_ABORT_THRESHOLD: 9821 *i1 = tcps->tcps_ip_abort_interval; 9822 break; 9823 case TCP_CONN_NOTIFY_THRESHOLD: 9824 *i1 = tcps->tcps_ip_notify_cinterval; 9825 break; 9826 case TCP_CONN_ABORT_THRESHOLD: 9827 *i1 = tcps->tcps_ip_abort_cinterval; 9828 break; 9829 default: 9830 return (-1); 9831 } 9832 break; 9833 case IPPROTO_IP: 9834 switch (name) { 9835 case IP_TTL: 9836 *i1 = tcps->tcps_ipv4_ttl; 9837 break; 9838 default: 9839 return (-1); 9840 } 9841 break; 9842 case IPPROTO_IPV6: 9843 switch (name) { 9844 case IPV6_UNICAST_HOPS: 9845 *i1 = tcps->tcps_ipv6_hoplimit; 9846 break; 9847 default: 9848 return (-1); 9849 } 9850 break; 9851 default: 9852 return (-1); 9853 } 9854 return (sizeof (int)); 9855 } 9856 9857 9858 /* 9859 * TCP routine to get the values of options. 9860 */ 9861 int 9862 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9863 { 9864 int *i1 = (int *)ptr; 9865 conn_t *connp = Q_TO_CONN(q); 9866 tcp_t *tcp = connp->conn_tcp; 9867 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9868 9869 switch (level) { 9870 case SOL_SOCKET: 9871 switch (name) { 9872 case SO_LINGER: { 9873 struct linger *lgr = (struct linger *)ptr; 9874 9875 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9876 lgr->l_linger = tcp->tcp_lingertime; 9877 } 9878 return (sizeof (struct linger)); 9879 case SO_DEBUG: 9880 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9881 break; 9882 case SO_KEEPALIVE: 9883 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9884 break; 9885 case SO_DONTROUTE: 9886 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9887 break; 9888 case SO_USELOOPBACK: 9889 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9890 break; 9891 case SO_BROADCAST: 9892 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9893 break; 9894 case SO_REUSEADDR: 9895 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9896 break; 9897 case SO_OOBINLINE: 9898 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9899 break; 9900 case SO_DGRAM_ERRIND: 9901 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9902 break; 9903 case SO_TYPE: 9904 *i1 = SOCK_STREAM; 9905 break; 9906 case SO_SNDBUF: 9907 *i1 = tcp->tcp_xmit_hiwater; 9908 break; 9909 case SO_RCVBUF: 9910 *i1 = RD(q)->q_hiwat; 9911 break; 9912 case SO_SND_COPYAVOID: 9913 *i1 = tcp->tcp_snd_zcopy_on ? 9914 SO_SND_COPYAVOID : 0; 9915 break; 9916 case SO_ALLZONES: 9917 *i1 = connp->conn_allzones ? 1 : 0; 9918 break; 9919 case SO_ANON_MLP: 9920 *i1 = connp->conn_anon_mlp; 9921 break; 9922 case SO_MAC_EXEMPT: 9923 *i1 = connp->conn_mac_exempt; 9924 break; 9925 case SO_EXCLBIND: 9926 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9927 break; 9928 case SO_PROTOTYPE: 9929 *i1 = IPPROTO_TCP; 9930 break; 9931 case SO_DOMAIN: 9932 *i1 = tcp->tcp_family; 9933 break; 9934 default: 9935 return (-1); 9936 } 9937 break; 9938 case IPPROTO_TCP: 9939 switch (name) { 9940 case TCP_NODELAY: 9941 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9942 break; 9943 case TCP_MAXSEG: 9944 *i1 = tcp->tcp_mss; 9945 break; 9946 case TCP_NOTIFY_THRESHOLD: 9947 *i1 = (int)tcp->tcp_first_timer_threshold; 9948 break; 9949 case TCP_ABORT_THRESHOLD: 9950 *i1 = tcp->tcp_second_timer_threshold; 9951 break; 9952 case TCP_CONN_NOTIFY_THRESHOLD: 9953 *i1 = tcp->tcp_first_ctimer_threshold; 9954 break; 9955 case TCP_CONN_ABORT_THRESHOLD: 9956 *i1 = tcp->tcp_second_ctimer_threshold; 9957 break; 9958 case TCP_RECVDSTADDR: 9959 *i1 = tcp->tcp_recvdstaddr; 9960 break; 9961 case TCP_ANONPRIVBIND: 9962 *i1 = tcp->tcp_anon_priv_bind; 9963 break; 9964 case TCP_EXCLBIND: 9965 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9966 break; 9967 case TCP_INIT_CWND: 9968 *i1 = tcp->tcp_init_cwnd; 9969 break; 9970 case TCP_KEEPALIVE_THRESHOLD: 9971 *i1 = tcp->tcp_ka_interval; 9972 break; 9973 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9974 *i1 = tcp->tcp_ka_abort_thres; 9975 break; 9976 case TCP_CORK: 9977 *i1 = tcp->tcp_cork; 9978 break; 9979 default: 9980 return (-1); 9981 } 9982 break; 9983 case IPPROTO_IP: 9984 if (tcp->tcp_family != AF_INET) 9985 return (-1); 9986 switch (name) { 9987 case IP_OPTIONS: 9988 case T_IP_OPTIONS: { 9989 /* 9990 * This is compatible with BSD in that in only return 9991 * the reverse source route with the final destination 9992 * as the last entry. The first 4 bytes of the option 9993 * will contain the final destination. 9994 */ 9995 int opt_len; 9996 9997 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9998 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9999 ASSERT(opt_len >= 0); 10000 /* Caller ensures enough space */ 10001 if (opt_len > 0) { 10002 /* 10003 * TODO: Do we have to handle getsockopt on an 10004 * initiator as well? 10005 */ 10006 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 10007 } 10008 return (0); 10009 } 10010 case IP_TOS: 10011 case T_IP_TOS: 10012 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10013 break; 10014 case IP_TTL: 10015 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10016 break; 10017 case IP_NEXTHOP: 10018 /* Handled at IP level */ 10019 return (-EINVAL); 10020 default: 10021 return (-1); 10022 } 10023 break; 10024 case IPPROTO_IPV6: 10025 /* 10026 * IPPROTO_IPV6 options are only supported for sockets 10027 * that are using IPv6 on the wire. 10028 */ 10029 if (tcp->tcp_ipversion != IPV6_VERSION) { 10030 return (-1); 10031 } 10032 switch (name) { 10033 case IPV6_UNICAST_HOPS: 10034 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10035 break; /* goto sizeof (int) option return */ 10036 case IPV6_BOUND_IF: 10037 /* Zero if not set */ 10038 *i1 = tcp->tcp_bound_if; 10039 break; /* goto sizeof (int) option return */ 10040 case IPV6_RECVPKTINFO: 10041 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10042 *i1 = 1; 10043 else 10044 *i1 = 0; 10045 break; /* goto sizeof (int) option return */ 10046 case IPV6_RECVTCLASS: 10047 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10048 *i1 = 1; 10049 else 10050 *i1 = 0; 10051 break; /* goto sizeof (int) option return */ 10052 case IPV6_RECVHOPLIMIT: 10053 if (tcp->tcp_ipv6_recvancillary & 10054 TCP_IPV6_RECVHOPLIMIT) 10055 *i1 = 1; 10056 else 10057 *i1 = 0; 10058 break; /* goto sizeof (int) option return */ 10059 case IPV6_RECVHOPOPTS: 10060 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10061 *i1 = 1; 10062 else 10063 *i1 = 0; 10064 break; /* goto sizeof (int) option return */ 10065 case IPV6_RECVDSTOPTS: 10066 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10067 *i1 = 1; 10068 else 10069 *i1 = 0; 10070 break; /* goto sizeof (int) option return */ 10071 case _OLD_IPV6_RECVDSTOPTS: 10072 if (tcp->tcp_ipv6_recvancillary & 10073 TCP_OLD_IPV6_RECVDSTOPTS) 10074 *i1 = 1; 10075 else 10076 *i1 = 0; 10077 break; /* goto sizeof (int) option return */ 10078 case IPV6_RECVRTHDR: 10079 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10080 *i1 = 1; 10081 else 10082 *i1 = 0; 10083 break; /* goto sizeof (int) option return */ 10084 case IPV6_RECVRTHDRDSTOPTS: 10085 if (tcp->tcp_ipv6_recvancillary & 10086 TCP_IPV6_RECVRTDSTOPTS) 10087 *i1 = 1; 10088 else 10089 *i1 = 0; 10090 break; /* goto sizeof (int) option return */ 10091 case IPV6_PKTINFO: { 10092 /* XXX assumes that caller has room for max size! */ 10093 struct in6_pktinfo *pkti; 10094 10095 pkti = (struct in6_pktinfo *)ptr; 10096 if (ipp->ipp_fields & IPPF_IFINDEX) 10097 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10098 else 10099 pkti->ipi6_ifindex = 0; 10100 if (ipp->ipp_fields & IPPF_ADDR) 10101 pkti->ipi6_addr = ipp->ipp_addr; 10102 else 10103 pkti->ipi6_addr = ipv6_all_zeros; 10104 return (sizeof (struct in6_pktinfo)); 10105 } 10106 case IPV6_TCLASS: 10107 if (ipp->ipp_fields & IPPF_TCLASS) 10108 *i1 = ipp->ipp_tclass; 10109 else 10110 *i1 = IPV6_FLOW_TCLASS( 10111 IPV6_DEFAULT_VERS_AND_FLOW); 10112 break; /* goto sizeof (int) option return */ 10113 case IPV6_NEXTHOP: { 10114 sin6_t *sin6 = (sin6_t *)ptr; 10115 10116 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10117 return (0); 10118 *sin6 = sin6_null; 10119 sin6->sin6_family = AF_INET6; 10120 sin6->sin6_addr = ipp->ipp_nexthop; 10121 return (sizeof (sin6_t)); 10122 } 10123 case IPV6_HOPOPTS: 10124 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10125 return (0); 10126 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10127 return (0); 10128 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10129 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10130 if (tcp->tcp_label_len > 0) { 10131 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10132 ptr[1] = (ipp->ipp_hopoptslen - 10133 tcp->tcp_label_len + 7) / 8 - 1; 10134 } 10135 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10136 case IPV6_RTHDRDSTOPTS: 10137 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10138 return (0); 10139 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10140 return (ipp->ipp_rtdstoptslen); 10141 case IPV6_RTHDR: 10142 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10143 return (0); 10144 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10145 return (ipp->ipp_rthdrlen); 10146 case IPV6_DSTOPTS: 10147 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10148 return (0); 10149 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10150 return (ipp->ipp_dstoptslen); 10151 case IPV6_SRC_PREFERENCES: 10152 return (ip6_get_src_preferences(connp, 10153 (uint32_t *)ptr)); 10154 case IPV6_PATHMTU: { 10155 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10156 10157 if (tcp->tcp_state < TCPS_ESTABLISHED) 10158 return (-1); 10159 10160 return (ip_fill_mtuinfo(&connp->conn_remv6, 10161 connp->conn_fport, mtuinfo, 10162 connp->conn_netstack)); 10163 } 10164 default: 10165 return (-1); 10166 } 10167 break; 10168 default: 10169 return (-1); 10170 } 10171 return (sizeof (int)); 10172 } 10173 10174 /* 10175 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10176 * Parameters are assumed to be verified by the caller. 10177 */ 10178 /* ARGSUSED */ 10179 int 10180 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10181 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10182 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10183 { 10184 conn_t *connp = Q_TO_CONN(q); 10185 tcp_t *tcp = connp->conn_tcp; 10186 int *i1 = (int *)invalp; 10187 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10188 boolean_t checkonly; 10189 int reterr; 10190 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10191 10192 switch (optset_context) { 10193 case SETFN_OPTCOM_CHECKONLY: 10194 checkonly = B_TRUE; 10195 /* 10196 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10197 * inlen != 0 implies value supplied and 10198 * we have to "pretend" to set it. 10199 * inlen == 0 implies that there is no 10200 * value part in T_CHECK request and just validation 10201 * done elsewhere should be enough, we just return here. 10202 */ 10203 if (inlen == 0) { 10204 *outlenp = 0; 10205 return (0); 10206 } 10207 break; 10208 case SETFN_OPTCOM_NEGOTIATE: 10209 checkonly = B_FALSE; 10210 break; 10211 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10212 case SETFN_CONN_NEGOTIATE: 10213 checkonly = B_FALSE; 10214 /* 10215 * Negotiating local and "association-related" options 10216 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10217 * primitives is allowed by XTI, but we choose 10218 * to not implement this style negotiation for Internet 10219 * protocols (We interpret it is a must for OSI world but 10220 * optional for Internet protocols) for all options. 10221 * [ Will do only for the few options that enable test 10222 * suites that our XTI implementation of this feature 10223 * works for transports that do allow it ] 10224 */ 10225 if (!tcp_allow_connopt_set(level, name)) { 10226 *outlenp = 0; 10227 return (EINVAL); 10228 } 10229 break; 10230 default: 10231 /* 10232 * We should never get here 10233 */ 10234 *outlenp = 0; 10235 return (EINVAL); 10236 } 10237 10238 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10239 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10240 10241 /* 10242 * For TCP, we should have no ancillary data sent down 10243 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10244 * has to be zero. 10245 */ 10246 ASSERT(thisdg_attrs == NULL); 10247 10248 /* 10249 * For fixed length options, no sanity check 10250 * of passed in length is done. It is assumed *_optcom_req() 10251 * routines do the right thing. 10252 */ 10253 10254 switch (level) { 10255 case SOL_SOCKET: 10256 switch (name) { 10257 case SO_LINGER: { 10258 struct linger *lgr = (struct linger *)invalp; 10259 10260 if (!checkonly) { 10261 if (lgr->l_onoff) { 10262 tcp->tcp_linger = 1; 10263 tcp->tcp_lingertime = lgr->l_linger; 10264 } else { 10265 tcp->tcp_linger = 0; 10266 tcp->tcp_lingertime = 0; 10267 } 10268 /* struct copy */ 10269 *(struct linger *)outvalp = *lgr; 10270 } else { 10271 if (!lgr->l_onoff) { 10272 ((struct linger *)outvalp)->l_onoff = 0; 10273 ((struct linger *)outvalp)->l_linger = 0; 10274 } else { 10275 /* struct copy */ 10276 *(struct linger *)outvalp = *lgr; 10277 } 10278 } 10279 *outlenp = sizeof (struct linger); 10280 return (0); 10281 } 10282 case SO_DEBUG: 10283 if (!checkonly) 10284 tcp->tcp_debug = onoff; 10285 break; 10286 case SO_KEEPALIVE: 10287 if (checkonly) { 10288 /* T_CHECK case */ 10289 break; 10290 } 10291 10292 if (!onoff) { 10293 if (tcp->tcp_ka_enabled) { 10294 if (tcp->tcp_ka_tid != 0) { 10295 (void) TCP_TIMER_CANCEL(tcp, 10296 tcp->tcp_ka_tid); 10297 tcp->tcp_ka_tid = 0; 10298 } 10299 tcp->tcp_ka_enabled = 0; 10300 } 10301 break; 10302 } 10303 if (!tcp->tcp_ka_enabled) { 10304 /* Crank up the keepalive timer */ 10305 tcp->tcp_ka_last_intrvl = 0; 10306 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10307 tcp_keepalive_killer, 10308 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10309 tcp->tcp_ka_enabled = 1; 10310 } 10311 break; 10312 case SO_DONTROUTE: 10313 /* 10314 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10315 * only of interest to IP. We track them here only so 10316 * that we can report their current value. 10317 */ 10318 if (!checkonly) { 10319 tcp->tcp_dontroute = onoff; 10320 tcp->tcp_connp->conn_dontroute = onoff; 10321 } 10322 break; 10323 case SO_USELOOPBACK: 10324 if (!checkonly) { 10325 tcp->tcp_useloopback = onoff; 10326 tcp->tcp_connp->conn_loopback = onoff; 10327 } 10328 break; 10329 case SO_BROADCAST: 10330 if (!checkonly) { 10331 tcp->tcp_broadcast = onoff; 10332 tcp->tcp_connp->conn_broadcast = onoff; 10333 } 10334 break; 10335 case SO_REUSEADDR: 10336 if (!checkonly) { 10337 tcp->tcp_reuseaddr = onoff; 10338 tcp->tcp_connp->conn_reuseaddr = onoff; 10339 } 10340 break; 10341 case SO_OOBINLINE: 10342 if (!checkonly) 10343 tcp->tcp_oobinline = onoff; 10344 break; 10345 case SO_DGRAM_ERRIND: 10346 if (!checkonly) 10347 tcp->tcp_dgram_errind = onoff; 10348 break; 10349 case SO_SNDBUF: { 10350 if (*i1 > tcps->tcps_max_buf) { 10351 *outlenp = 0; 10352 return (ENOBUFS); 10353 } 10354 if (checkonly) 10355 break; 10356 10357 tcp->tcp_xmit_hiwater = *i1; 10358 if (tcps->tcps_snd_lowat_fraction != 0) 10359 tcp->tcp_xmit_lowater = 10360 tcp->tcp_xmit_hiwater / 10361 tcps->tcps_snd_lowat_fraction; 10362 (void) tcp_maxpsz_set(tcp, B_TRUE); 10363 /* 10364 * If we are flow-controlled, recheck the condition. 10365 * There are apps that increase SO_SNDBUF size when 10366 * flow-controlled (EWOULDBLOCK), and expect the flow 10367 * control condition to be lifted right away. 10368 */ 10369 mutex_enter(&tcp->tcp_non_sq_lock); 10370 if (tcp->tcp_flow_stopped && 10371 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10372 tcp_clrqfull(tcp); 10373 } 10374 mutex_exit(&tcp->tcp_non_sq_lock); 10375 break; 10376 } 10377 case SO_RCVBUF: 10378 if (*i1 > tcps->tcps_max_buf) { 10379 *outlenp = 0; 10380 return (ENOBUFS); 10381 } 10382 /* Silently ignore zero */ 10383 if (!checkonly && *i1 != 0) { 10384 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10385 (void) tcp_rwnd_set(tcp, *i1); 10386 } 10387 /* 10388 * XXX should we return the rwnd here 10389 * and tcp_opt_get ? 10390 */ 10391 break; 10392 case SO_SND_COPYAVOID: 10393 if (!checkonly) { 10394 /* we only allow enable at most once for now */ 10395 if (tcp->tcp_loopback || 10396 (!tcp->tcp_snd_zcopy_aware && 10397 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10398 *outlenp = 0; 10399 return (EOPNOTSUPP); 10400 } 10401 tcp->tcp_snd_zcopy_aware = 1; 10402 } 10403 break; 10404 case SO_ALLZONES: 10405 /* Handled at the IP level */ 10406 return (-EINVAL); 10407 case SO_ANON_MLP: 10408 if (!checkonly) { 10409 mutex_enter(&connp->conn_lock); 10410 connp->conn_anon_mlp = onoff; 10411 mutex_exit(&connp->conn_lock); 10412 } 10413 break; 10414 case SO_MAC_EXEMPT: 10415 if (secpolicy_net_mac_aware(cr) != 0 || 10416 IPCL_IS_BOUND(connp)) 10417 return (EACCES); 10418 if (!checkonly) { 10419 mutex_enter(&connp->conn_lock); 10420 connp->conn_mac_exempt = onoff; 10421 mutex_exit(&connp->conn_lock); 10422 } 10423 break; 10424 case SO_EXCLBIND: 10425 if (!checkonly) 10426 tcp->tcp_exclbind = onoff; 10427 break; 10428 default: 10429 *outlenp = 0; 10430 return (EINVAL); 10431 } 10432 break; 10433 case IPPROTO_TCP: 10434 switch (name) { 10435 case TCP_NODELAY: 10436 if (!checkonly) 10437 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10438 break; 10439 case TCP_NOTIFY_THRESHOLD: 10440 if (!checkonly) 10441 tcp->tcp_first_timer_threshold = *i1; 10442 break; 10443 case TCP_ABORT_THRESHOLD: 10444 if (!checkonly) 10445 tcp->tcp_second_timer_threshold = *i1; 10446 break; 10447 case TCP_CONN_NOTIFY_THRESHOLD: 10448 if (!checkonly) 10449 tcp->tcp_first_ctimer_threshold = *i1; 10450 break; 10451 case TCP_CONN_ABORT_THRESHOLD: 10452 if (!checkonly) 10453 tcp->tcp_second_ctimer_threshold = *i1; 10454 break; 10455 case TCP_RECVDSTADDR: 10456 if (tcp->tcp_state > TCPS_LISTEN) 10457 return (EOPNOTSUPP); 10458 if (!checkonly) 10459 tcp->tcp_recvdstaddr = onoff; 10460 break; 10461 case TCP_ANONPRIVBIND: 10462 if ((reterr = secpolicy_net_privaddr(cr, 0)) != 0) { 10463 *outlenp = 0; 10464 return (reterr); 10465 } 10466 if (!checkonly) { 10467 tcp->tcp_anon_priv_bind = onoff; 10468 } 10469 break; 10470 case TCP_EXCLBIND: 10471 if (!checkonly) 10472 tcp->tcp_exclbind = onoff; 10473 break; /* goto sizeof (int) option return */ 10474 case TCP_INIT_CWND: { 10475 uint32_t init_cwnd = *((uint32_t *)invalp); 10476 10477 if (checkonly) 10478 break; 10479 10480 /* 10481 * Only allow socket with network configuration 10482 * privilege to set the initial cwnd to be larger 10483 * than allowed by RFC 3390. 10484 */ 10485 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10486 tcp->tcp_init_cwnd = init_cwnd; 10487 break; 10488 } 10489 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10490 *outlenp = 0; 10491 return (reterr); 10492 } 10493 if (init_cwnd > TCP_MAX_INIT_CWND) { 10494 *outlenp = 0; 10495 return (EINVAL); 10496 } 10497 tcp->tcp_init_cwnd = init_cwnd; 10498 break; 10499 } 10500 case TCP_KEEPALIVE_THRESHOLD: 10501 if (checkonly) 10502 break; 10503 10504 if (*i1 < tcps->tcps_keepalive_interval_low || 10505 *i1 > tcps->tcps_keepalive_interval_high) { 10506 *outlenp = 0; 10507 return (EINVAL); 10508 } 10509 if (*i1 != tcp->tcp_ka_interval) { 10510 tcp->tcp_ka_interval = *i1; 10511 /* 10512 * Check if we need to restart the 10513 * keepalive timer. 10514 */ 10515 if (tcp->tcp_ka_tid != 0) { 10516 ASSERT(tcp->tcp_ka_enabled); 10517 (void) TCP_TIMER_CANCEL(tcp, 10518 tcp->tcp_ka_tid); 10519 tcp->tcp_ka_last_intrvl = 0; 10520 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10521 tcp_keepalive_killer, 10522 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10523 } 10524 } 10525 break; 10526 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10527 if (!checkonly) { 10528 if (*i1 < 10529 tcps->tcps_keepalive_abort_interval_low || 10530 *i1 > 10531 tcps->tcps_keepalive_abort_interval_high) { 10532 *outlenp = 0; 10533 return (EINVAL); 10534 } 10535 tcp->tcp_ka_abort_thres = *i1; 10536 } 10537 break; 10538 case TCP_CORK: 10539 if (!checkonly) { 10540 /* 10541 * if tcp->tcp_cork was set and is now 10542 * being unset, we have to make sure that 10543 * the remaining data gets sent out. Also 10544 * unset tcp->tcp_cork so that tcp_wput_data() 10545 * can send data even if it is less than mss 10546 */ 10547 if (tcp->tcp_cork && onoff == 0 && 10548 tcp->tcp_unsent > 0) { 10549 tcp->tcp_cork = B_FALSE; 10550 tcp_wput_data(tcp, NULL, B_FALSE); 10551 } 10552 tcp->tcp_cork = onoff; 10553 } 10554 break; 10555 default: 10556 *outlenp = 0; 10557 return (EINVAL); 10558 } 10559 break; 10560 case IPPROTO_IP: 10561 if (tcp->tcp_family != AF_INET) { 10562 *outlenp = 0; 10563 return (ENOPROTOOPT); 10564 } 10565 switch (name) { 10566 case IP_OPTIONS: 10567 case T_IP_OPTIONS: 10568 reterr = tcp_opt_set_header(tcp, checkonly, 10569 invalp, inlen); 10570 if (reterr) { 10571 *outlenp = 0; 10572 return (reterr); 10573 } 10574 /* OK return - copy input buffer into output buffer */ 10575 if (invalp != outvalp) { 10576 /* don't trust bcopy for identical src/dst */ 10577 bcopy(invalp, outvalp, inlen); 10578 } 10579 *outlenp = inlen; 10580 return (0); 10581 case IP_TOS: 10582 case T_IP_TOS: 10583 if (!checkonly) { 10584 tcp->tcp_ipha->ipha_type_of_service = 10585 (uchar_t)*i1; 10586 tcp->tcp_tos = (uchar_t)*i1; 10587 } 10588 break; 10589 case IP_TTL: 10590 if (!checkonly) { 10591 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10592 tcp->tcp_ttl = (uchar_t)*i1; 10593 } 10594 break; 10595 case IP_BOUND_IF: 10596 case IP_NEXTHOP: 10597 /* Handled at the IP level */ 10598 return (-EINVAL); 10599 case IP_SEC_OPT: 10600 /* 10601 * We should not allow policy setting after 10602 * we start listening for connections. 10603 */ 10604 if (tcp->tcp_state == TCPS_LISTEN) { 10605 return (EINVAL); 10606 } else { 10607 /* Handled at the IP level */ 10608 return (-EINVAL); 10609 } 10610 default: 10611 *outlenp = 0; 10612 return (EINVAL); 10613 } 10614 break; 10615 case IPPROTO_IPV6: { 10616 ip6_pkt_t *ipp; 10617 10618 /* 10619 * IPPROTO_IPV6 options are only supported for sockets 10620 * that are using IPv6 on the wire. 10621 */ 10622 if (tcp->tcp_ipversion != IPV6_VERSION) { 10623 *outlenp = 0; 10624 return (ENOPROTOOPT); 10625 } 10626 /* 10627 * Only sticky options; no ancillary data 10628 */ 10629 ASSERT(thisdg_attrs == NULL); 10630 ipp = &tcp->tcp_sticky_ipp; 10631 10632 switch (name) { 10633 case IPV6_UNICAST_HOPS: 10634 /* -1 means use default */ 10635 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10636 *outlenp = 0; 10637 return (EINVAL); 10638 } 10639 if (!checkonly) { 10640 if (*i1 == -1) { 10641 tcp->tcp_ip6h->ip6_hops = 10642 ipp->ipp_unicast_hops = 10643 (uint8_t)tcps->tcps_ipv6_hoplimit; 10644 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10645 /* Pass modified value to IP. */ 10646 *i1 = tcp->tcp_ip6h->ip6_hops; 10647 } else { 10648 tcp->tcp_ip6h->ip6_hops = 10649 ipp->ipp_unicast_hops = 10650 (uint8_t)*i1; 10651 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10652 } 10653 reterr = tcp_build_hdrs(q, tcp); 10654 if (reterr != 0) 10655 return (reterr); 10656 } 10657 break; 10658 case IPV6_BOUND_IF: 10659 if (!checkonly) { 10660 int error = 0; 10661 10662 tcp->tcp_bound_if = *i1; 10663 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10664 B_TRUE, checkonly, level, name, mblk); 10665 if (error != 0) { 10666 *outlenp = 0; 10667 return (error); 10668 } 10669 } 10670 break; 10671 /* 10672 * Set boolean switches for ancillary data delivery 10673 */ 10674 case IPV6_RECVPKTINFO: 10675 if (!checkonly) { 10676 if (onoff) 10677 tcp->tcp_ipv6_recvancillary |= 10678 TCP_IPV6_RECVPKTINFO; 10679 else 10680 tcp->tcp_ipv6_recvancillary &= 10681 ~TCP_IPV6_RECVPKTINFO; 10682 /* Force it to be sent up with the next msg */ 10683 tcp->tcp_recvifindex = 0; 10684 } 10685 break; 10686 case IPV6_RECVTCLASS: 10687 if (!checkonly) { 10688 if (onoff) 10689 tcp->tcp_ipv6_recvancillary |= 10690 TCP_IPV6_RECVTCLASS; 10691 else 10692 tcp->tcp_ipv6_recvancillary &= 10693 ~TCP_IPV6_RECVTCLASS; 10694 } 10695 break; 10696 case IPV6_RECVHOPLIMIT: 10697 if (!checkonly) { 10698 if (onoff) 10699 tcp->tcp_ipv6_recvancillary |= 10700 TCP_IPV6_RECVHOPLIMIT; 10701 else 10702 tcp->tcp_ipv6_recvancillary &= 10703 ~TCP_IPV6_RECVHOPLIMIT; 10704 /* Force it to be sent up with the next msg */ 10705 tcp->tcp_recvhops = 0xffffffffU; 10706 } 10707 break; 10708 case IPV6_RECVHOPOPTS: 10709 if (!checkonly) { 10710 if (onoff) 10711 tcp->tcp_ipv6_recvancillary |= 10712 TCP_IPV6_RECVHOPOPTS; 10713 else 10714 tcp->tcp_ipv6_recvancillary &= 10715 ~TCP_IPV6_RECVHOPOPTS; 10716 } 10717 break; 10718 case IPV6_RECVDSTOPTS: 10719 if (!checkonly) { 10720 if (onoff) 10721 tcp->tcp_ipv6_recvancillary |= 10722 TCP_IPV6_RECVDSTOPTS; 10723 else 10724 tcp->tcp_ipv6_recvancillary &= 10725 ~TCP_IPV6_RECVDSTOPTS; 10726 } 10727 break; 10728 case _OLD_IPV6_RECVDSTOPTS: 10729 if (!checkonly) { 10730 if (onoff) 10731 tcp->tcp_ipv6_recvancillary |= 10732 TCP_OLD_IPV6_RECVDSTOPTS; 10733 else 10734 tcp->tcp_ipv6_recvancillary &= 10735 ~TCP_OLD_IPV6_RECVDSTOPTS; 10736 } 10737 break; 10738 case IPV6_RECVRTHDR: 10739 if (!checkonly) { 10740 if (onoff) 10741 tcp->tcp_ipv6_recvancillary |= 10742 TCP_IPV6_RECVRTHDR; 10743 else 10744 tcp->tcp_ipv6_recvancillary &= 10745 ~TCP_IPV6_RECVRTHDR; 10746 } 10747 break; 10748 case IPV6_RECVRTHDRDSTOPTS: 10749 if (!checkonly) { 10750 if (onoff) 10751 tcp->tcp_ipv6_recvancillary |= 10752 TCP_IPV6_RECVRTDSTOPTS; 10753 else 10754 tcp->tcp_ipv6_recvancillary &= 10755 ~TCP_IPV6_RECVRTDSTOPTS; 10756 } 10757 break; 10758 case IPV6_PKTINFO: 10759 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10760 return (EINVAL); 10761 if (checkonly) 10762 break; 10763 10764 if (inlen == 0) { 10765 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10766 } else { 10767 struct in6_pktinfo *pkti; 10768 10769 pkti = (struct in6_pktinfo *)invalp; 10770 /* 10771 * RFC 3542 states that ipi6_addr must be 10772 * the unspecified address when setting the 10773 * IPV6_PKTINFO sticky socket option on a 10774 * TCP socket. 10775 */ 10776 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10777 return (EINVAL); 10778 /* 10779 * ip6_set_pktinfo() validates the source 10780 * address and interface index. 10781 */ 10782 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10783 pkti, mblk); 10784 if (reterr != 0) 10785 return (reterr); 10786 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10787 ipp->ipp_addr = pkti->ipi6_addr; 10788 if (ipp->ipp_ifindex != 0) 10789 ipp->ipp_fields |= IPPF_IFINDEX; 10790 else 10791 ipp->ipp_fields &= ~IPPF_IFINDEX; 10792 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10793 ipp->ipp_fields |= IPPF_ADDR; 10794 else 10795 ipp->ipp_fields &= ~IPPF_ADDR; 10796 } 10797 reterr = tcp_build_hdrs(q, tcp); 10798 if (reterr != 0) 10799 return (reterr); 10800 break; 10801 case IPV6_TCLASS: 10802 if (inlen != 0 && inlen != sizeof (int)) 10803 return (EINVAL); 10804 if (checkonly) 10805 break; 10806 10807 if (inlen == 0) { 10808 ipp->ipp_fields &= ~IPPF_TCLASS; 10809 } else { 10810 if (*i1 > 255 || *i1 < -1) 10811 return (EINVAL); 10812 if (*i1 == -1) { 10813 ipp->ipp_tclass = 0; 10814 *i1 = 0; 10815 } else { 10816 ipp->ipp_tclass = *i1; 10817 } 10818 ipp->ipp_fields |= IPPF_TCLASS; 10819 } 10820 reterr = tcp_build_hdrs(q, tcp); 10821 if (reterr != 0) 10822 return (reterr); 10823 break; 10824 case IPV6_NEXTHOP: 10825 /* 10826 * IP will verify that the nexthop is reachable 10827 * and fail for sticky options. 10828 */ 10829 if (inlen != 0 && inlen != sizeof (sin6_t)) 10830 return (EINVAL); 10831 if (checkonly) 10832 break; 10833 10834 if (inlen == 0) { 10835 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10836 } else { 10837 sin6_t *sin6 = (sin6_t *)invalp; 10838 10839 if (sin6->sin6_family != AF_INET6) 10840 return (EAFNOSUPPORT); 10841 if (IN6_IS_ADDR_V4MAPPED( 10842 &sin6->sin6_addr)) 10843 return (EADDRNOTAVAIL); 10844 ipp->ipp_nexthop = sin6->sin6_addr; 10845 if (!IN6_IS_ADDR_UNSPECIFIED( 10846 &ipp->ipp_nexthop)) 10847 ipp->ipp_fields |= IPPF_NEXTHOP; 10848 else 10849 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10850 } 10851 reterr = tcp_build_hdrs(q, tcp); 10852 if (reterr != 0) 10853 return (reterr); 10854 break; 10855 case IPV6_HOPOPTS: { 10856 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10857 10858 /* 10859 * Sanity checks - minimum size, size a multiple of 10860 * eight bytes, and matching size passed in. 10861 */ 10862 if (inlen != 0 && 10863 inlen != (8 * (hopts->ip6h_len + 1))) 10864 return (EINVAL); 10865 10866 if (checkonly) 10867 break; 10868 10869 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10870 (uchar_t **)&ipp->ipp_hopopts, 10871 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10872 if (reterr != 0) 10873 return (reterr); 10874 if (ipp->ipp_hopoptslen == 0) 10875 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10876 else 10877 ipp->ipp_fields |= IPPF_HOPOPTS; 10878 reterr = tcp_build_hdrs(q, tcp); 10879 if (reterr != 0) 10880 return (reterr); 10881 break; 10882 } 10883 case IPV6_RTHDRDSTOPTS: { 10884 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10885 10886 /* 10887 * Sanity checks - minimum size, size a multiple of 10888 * eight bytes, and matching size passed in. 10889 */ 10890 if (inlen != 0 && 10891 inlen != (8 * (dopts->ip6d_len + 1))) 10892 return (EINVAL); 10893 10894 if (checkonly) 10895 break; 10896 10897 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10898 (uchar_t **)&ipp->ipp_rtdstopts, 10899 &ipp->ipp_rtdstoptslen, 0); 10900 if (reterr != 0) 10901 return (reterr); 10902 if (ipp->ipp_rtdstoptslen == 0) 10903 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10904 else 10905 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10906 reterr = tcp_build_hdrs(q, tcp); 10907 if (reterr != 0) 10908 return (reterr); 10909 break; 10910 } 10911 case IPV6_DSTOPTS: { 10912 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10913 10914 /* 10915 * Sanity checks - minimum size, size a multiple of 10916 * eight bytes, and matching size passed in. 10917 */ 10918 if (inlen != 0 && 10919 inlen != (8 * (dopts->ip6d_len + 1))) 10920 return (EINVAL); 10921 10922 if (checkonly) 10923 break; 10924 10925 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10926 (uchar_t **)&ipp->ipp_dstopts, 10927 &ipp->ipp_dstoptslen, 0); 10928 if (reterr != 0) 10929 return (reterr); 10930 if (ipp->ipp_dstoptslen == 0) 10931 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10932 else 10933 ipp->ipp_fields |= IPPF_DSTOPTS; 10934 reterr = tcp_build_hdrs(q, tcp); 10935 if (reterr != 0) 10936 return (reterr); 10937 break; 10938 } 10939 case IPV6_RTHDR: { 10940 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10941 10942 /* 10943 * Sanity checks - minimum size, size a multiple of 10944 * eight bytes, and matching size passed in. 10945 */ 10946 if (inlen != 0 && 10947 inlen != (8 * (rt->ip6r_len + 1))) 10948 return (EINVAL); 10949 10950 if (checkonly) 10951 break; 10952 10953 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10954 (uchar_t **)&ipp->ipp_rthdr, 10955 &ipp->ipp_rthdrlen, 0); 10956 if (reterr != 0) 10957 return (reterr); 10958 if (ipp->ipp_rthdrlen == 0) 10959 ipp->ipp_fields &= ~IPPF_RTHDR; 10960 else 10961 ipp->ipp_fields |= IPPF_RTHDR; 10962 reterr = tcp_build_hdrs(q, tcp); 10963 if (reterr != 0) 10964 return (reterr); 10965 break; 10966 } 10967 case IPV6_V6ONLY: 10968 if (!checkonly) 10969 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10970 break; 10971 case IPV6_USE_MIN_MTU: 10972 if (inlen != sizeof (int)) 10973 return (EINVAL); 10974 10975 if (*i1 < -1 || *i1 > 1) 10976 return (EINVAL); 10977 10978 if (checkonly) 10979 break; 10980 10981 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10982 ipp->ipp_use_min_mtu = *i1; 10983 break; 10984 case IPV6_BOUND_PIF: 10985 /* Handled at the IP level */ 10986 return (-EINVAL); 10987 case IPV6_SEC_OPT: 10988 /* 10989 * We should not allow policy setting after 10990 * we start listening for connections. 10991 */ 10992 if (tcp->tcp_state == TCPS_LISTEN) { 10993 return (EINVAL); 10994 } else { 10995 /* Handled at the IP level */ 10996 return (-EINVAL); 10997 } 10998 case IPV6_SRC_PREFERENCES: 10999 if (inlen != sizeof (uint32_t)) 11000 return (EINVAL); 11001 reterr = ip6_set_src_preferences(tcp->tcp_connp, 11002 *(uint32_t *)invalp); 11003 if (reterr != 0) { 11004 *outlenp = 0; 11005 return (reterr); 11006 } 11007 break; 11008 default: 11009 *outlenp = 0; 11010 return (EINVAL); 11011 } 11012 break; 11013 } /* end IPPROTO_IPV6 */ 11014 default: 11015 *outlenp = 0; 11016 return (EINVAL); 11017 } 11018 /* 11019 * Common case of OK return with outval same as inval 11020 */ 11021 if (invalp != outvalp) { 11022 /* don't trust bcopy for identical src/dst */ 11023 (void) bcopy(invalp, outvalp, inlen); 11024 } 11025 *outlenp = inlen; 11026 return (0); 11027 } 11028 11029 /* 11030 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 11031 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 11032 * headers, and the maximum size tcp header (to avoid reallocation 11033 * on the fly for additional tcp options). 11034 * Returns failure if can't allocate memory. 11035 */ 11036 static int 11037 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 11038 { 11039 char *hdrs; 11040 uint_t hdrs_len; 11041 ip6i_t *ip6i; 11042 char buf[TCP_MAX_HDR_LENGTH]; 11043 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 11044 in6_addr_t src, dst; 11045 tcp_stack_t *tcps = tcp->tcp_tcps; 11046 11047 /* 11048 * save the existing tcp header and source/dest IP addresses 11049 */ 11050 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 11051 src = tcp->tcp_ip6h->ip6_src; 11052 dst = tcp->tcp_ip6h->ip6_dst; 11053 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 11054 ASSERT(hdrs_len != 0); 11055 if (hdrs_len > tcp->tcp_iphc_len) { 11056 /* Need to reallocate */ 11057 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 11058 if (hdrs == NULL) 11059 return (ENOMEM); 11060 if (tcp->tcp_iphc != NULL) { 11061 if (tcp->tcp_hdr_grown) { 11062 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 11063 } else { 11064 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 11065 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 11066 } 11067 tcp->tcp_iphc_len = 0; 11068 } 11069 ASSERT(tcp->tcp_iphc_len == 0); 11070 tcp->tcp_iphc = hdrs; 11071 tcp->tcp_iphc_len = hdrs_len; 11072 tcp->tcp_hdr_grown = B_TRUE; 11073 } 11074 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11075 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11076 11077 /* Set header fields not in ipp */ 11078 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11079 ip6i = (ip6i_t *)tcp->tcp_iphc; 11080 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11081 } else { 11082 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11083 } 11084 /* 11085 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11086 * 11087 * tcp->tcp_tcp_hdr_len doesn't change here. 11088 */ 11089 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11090 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11091 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11092 11093 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11094 11095 tcp->tcp_ip6h->ip6_src = src; 11096 tcp->tcp_ip6h->ip6_dst = dst; 11097 11098 /* 11099 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11100 * the default value for TCP. 11101 */ 11102 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11103 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 11104 11105 /* 11106 * If we're setting extension headers after a connection 11107 * has been established, and if we have a routing header 11108 * among the extension headers, call ip_massage_options_v6 to 11109 * manipulate the routing header/ip6_dst set the checksum 11110 * difference in the tcp header template. 11111 * (This happens in tcp_connect_ipv6 if the routing header 11112 * is set prior to the connect.) 11113 * Set the tcp_sum to zero first in case we've cleared a 11114 * routing header or don't have one at all. 11115 */ 11116 tcp->tcp_sum = 0; 11117 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11118 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11119 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11120 (uint8_t *)tcp->tcp_tcph); 11121 if (rth != NULL) { 11122 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11123 rth, tcps->tcps_netstack); 11124 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11125 (tcp->tcp_sum >> 16)); 11126 } 11127 } 11128 11129 /* Try to get everything in a single mblk */ 11130 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcps->tcps_wroff_xtra); 11131 return (0); 11132 } 11133 11134 /* 11135 * Transfer any source route option from ipha to buf/dst in reversed form. 11136 */ 11137 static int 11138 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11139 { 11140 ipoptp_t opts; 11141 uchar_t *opt; 11142 uint8_t optval; 11143 uint8_t optlen; 11144 uint32_t len = 0; 11145 11146 for (optval = ipoptp_first(&opts, ipha); 11147 optval != IPOPT_EOL; 11148 optval = ipoptp_next(&opts)) { 11149 opt = opts.ipoptp_cur; 11150 optlen = opts.ipoptp_len; 11151 switch (optval) { 11152 int off1, off2; 11153 case IPOPT_SSRR: 11154 case IPOPT_LSRR: 11155 11156 /* Reverse source route */ 11157 /* 11158 * First entry should be the next to last one in the 11159 * current source route (the last entry is our 11160 * address.) 11161 * The last entry should be the final destination. 11162 */ 11163 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11164 buf[IPOPT_OLEN] = (uint8_t)optlen; 11165 off1 = IPOPT_MINOFF_SR - 1; 11166 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11167 if (off2 < 0) { 11168 /* No entries in source route */ 11169 break; 11170 } 11171 bcopy(opt + off2, dst, IP_ADDR_LEN); 11172 /* 11173 * Note: use src since ipha has not had its src 11174 * and dst reversed (it is in the state it was 11175 * received. 11176 */ 11177 bcopy(&ipha->ipha_src, buf + off2, 11178 IP_ADDR_LEN); 11179 off2 -= IP_ADDR_LEN; 11180 11181 while (off2 > 0) { 11182 bcopy(opt + off2, buf + off1, 11183 IP_ADDR_LEN); 11184 off1 += IP_ADDR_LEN; 11185 off2 -= IP_ADDR_LEN; 11186 } 11187 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11188 buf += optlen; 11189 len += optlen; 11190 break; 11191 } 11192 } 11193 done: 11194 /* Pad the resulting options */ 11195 while (len & 0x3) { 11196 *buf++ = IPOPT_EOL; 11197 len++; 11198 } 11199 return (len); 11200 } 11201 11202 11203 /* 11204 * Extract and revert a source route from ipha (if any) 11205 * and then update the relevant fields in both tcp_t and the standard header. 11206 */ 11207 static void 11208 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11209 { 11210 char buf[TCP_MAX_HDR_LENGTH]; 11211 uint_t tcph_len; 11212 int len; 11213 11214 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11215 len = IPH_HDR_LENGTH(ipha); 11216 if (len == IP_SIMPLE_HDR_LENGTH) 11217 /* Nothing to do */ 11218 return; 11219 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11220 (len & 0x3)) 11221 return; 11222 11223 tcph_len = tcp->tcp_tcp_hdr_len; 11224 bcopy(tcp->tcp_tcph, buf, tcph_len); 11225 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11226 (tcp->tcp_ipha->ipha_dst & 0xffff); 11227 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11228 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11229 len += IP_SIMPLE_HDR_LENGTH; 11230 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11231 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11232 if ((int)tcp->tcp_sum < 0) 11233 tcp->tcp_sum--; 11234 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11235 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11236 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11237 bcopy(buf, tcp->tcp_tcph, tcph_len); 11238 tcp->tcp_ip_hdr_len = len; 11239 tcp->tcp_ipha->ipha_version_and_hdr_length = 11240 (IP_VERSION << 4) | (len >> 2); 11241 len += tcph_len; 11242 tcp->tcp_hdr_len = len; 11243 } 11244 11245 /* 11246 * Copy the standard header into its new location, 11247 * lay in the new options and then update the relevant 11248 * fields in both tcp_t and the standard header. 11249 */ 11250 static int 11251 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11252 { 11253 uint_t tcph_len; 11254 uint8_t *ip_optp; 11255 tcph_t *new_tcph; 11256 tcp_stack_t *tcps = tcp->tcp_tcps; 11257 11258 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11259 return (EINVAL); 11260 11261 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11262 return (EINVAL); 11263 11264 if (checkonly) { 11265 /* 11266 * do not really set, just pretend to - T_CHECK 11267 */ 11268 return (0); 11269 } 11270 11271 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11272 if (tcp->tcp_label_len > 0) { 11273 int padlen; 11274 uint8_t opt; 11275 11276 /* convert list termination to no-ops */ 11277 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11278 ip_optp += ip_optp[IPOPT_OLEN]; 11279 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11280 while (--padlen >= 0) 11281 *ip_optp++ = opt; 11282 } 11283 tcph_len = tcp->tcp_tcp_hdr_len; 11284 new_tcph = (tcph_t *)(ip_optp + len); 11285 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11286 tcp->tcp_tcph = new_tcph; 11287 bcopy(ptr, ip_optp, len); 11288 11289 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11290 11291 tcp->tcp_ip_hdr_len = len; 11292 tcp->tcp_ipha->ipha_version_and_hdr_length = 11293 (IP_VERSION << 4) | (len >> 2); 11294 tcp->tcp_hdr_len = len + tcph_len; 11295 if (!TCP_IS_DETACHED(tcp)) { 11296 /* Always allocate room for all options. */ 11297 (void) mi_set_sth_wroff(tcp->tcp_rq, 11298 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11299 } 11300 return (0); 11301 } 11302 11303 /* Get callback routine passed to nd_load by tcp_param_register */ 11304 /* ARGSUSED */ 11305 static int 11306 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11307 { 11308 tcpparam_t *tcppa = (tcpparam_t *)cp; 11309 11310 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11311 return (0); 11312 } 11313 11314 /* 11315 * Walk through the param array specified registering each element with the 11316 * named dispatch handler. 11317 */ 11318 static boolean_t 11319 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11320 { 11321 for (; cnt-- > 0; tcppa++) { 11322 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11323 if (!nd_load(ndp, tcppa->tcp_param_name, 11324 tcp_param_get, tcp_param_set, 11325 (caddr_t)tcppa)) { 11326 nd_free(ndp); 11327 return (B_FALSE); 11328 } 11329 } 11330 } 11331 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11332 KM_SLEEP); 11333 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11334 sizeof (tcpparam_t)); 11335 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11336 tcp_param_get, tcp_param_set_aligned, 11337 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11338 nd_free(ndp); 11339 return (B_FALSE); 11340 } 11341 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11342 KM_SLEEP); 11343 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11344 sizeof (tcpparam_t)); 11345 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11346 tcp_param_get, tcp_param_set_aligned, 11347 (caddr_t)tcps->tcps_mdt_head_param)) { 11348 nd_free(ndp); 11349 return (B_FALSE); 11350 } 11351 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11352 KM_SLEEP); 11353 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11354 sizeof (tcpparam_t)); 11355 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11356 tcp_param_get, tcp_param_set_aligned, 11357 (caddr_t)tcps->tcps_mdt_tail_param)) { 11358 nd_free(ndp); 11359 return (B_FALSE); 11360 } 11361 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11362 KM_SLEEP); 11363 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11364 sizeof (tcpparam_t)); 11365 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11366 tcp_param_get, tcp_param_set_aligned, 11367 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11368 nd_free(ndp); 11369 return (B_FALSE); 11370 } 11371 if (!nd_load(ndp, "tcp_extra_priv_ports", 11372 tcp_extra_priv_ports_get, NULL, NULL)) { 11373 nd_free(ndp); 11374 return (B_FALSE); 11375 } 11376 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11377 NULL, tcp_extra_priv_ports_add, NULL)) { 11378 nd_free(ndp); 11379 return (B_FALSE); 11380 } 11381 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11382 NULL, tcp_extra_priv_ports_del, NULL)) { 11383 nd_free(ndp); 11384 return (B_FALSE); 11385 } 11386 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11387 NULL)) { 11388 nd_free(ndp); 11389 return (B_FALSE); 11390 } 11391 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11392 NULL, NULL)) { 11393 nd_free(ndp); 11394 return (B_FALSE); 11395 } 11396 if (!nd_load(ndp, "tcp_listen_hash", 11397 tcp_listen_hash_report, NULL, NULL)) { 11398 nd_free(ndp); 11399 return (B_FALSE); 11400 } 11401 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11402 NULL, NULL)) { 11403 nd_free(ndp); 11404 return (B_FALSE); 11405 } 11406 if (!nd_load(ndp, "tcp_acceptor_hash", 11407 tcp_acceptor_hash_report, NULL, NULL)) { 11408 nd_free(ndp); 11409 return (B_FALSE); 11410 } 11411 if (!nd_load(ndp, "tcp_host_param", tcp_host_param_report, 11412 tcp_host_param_set, NULL)) { 11413 nd_free(ndp); 11414 return (B_FALSE); 11415 } 11416 if (!nd_load(ndp, "tcp_host_param_ipv6", 11417 tcp_host_param_report, tcp_host_param_set_ipv6, NULL)) { 11418 nd_free(ndp); 11419 return (B_FALSE); 11420 } 11421 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11422 tcp_1948_phrase_set, NULL)) { 11423 nd_free(ndp); 11424 return (B_FALSE); 11425 } 11426 if (!nd_load(ndp, "tcp_reserved_port_list", 11427 tcp_reserved_port_list, NULL, NULL)) { 11428 nd_free(ndp); 11429 return (B_FALSE); 11430 } 11431 /* 11432 * Dummy ndd variables - only to convey obsolescence information 11433 * through printing of their name (no get or set routines) 11434 * XXX Remove in future releases ? 11435 */ 11436 if (!nd_load(ndp, 11437 "tcp_close_wait_interval(obsoleted - " 11438 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11439 nd_free(ndp); 11440 return (B_FALSE); 11441 } 11442 return (B_TRUE); 11443 } 11444 11445 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11446 /* ARGSUSED */ 11447 static int 11448 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11449 cred_t *cr) 11450 { 11451 long new_value; 11452 tcpparam_t *tcppa = (tcpparam_t *)cp; 11453 11454 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11455 new_value < tcppa->tcp_param_min || 11456 new_value > tcppa->tcp_param_max) { 11457 return (EINVAL); 11458 } 11459 /* 11460 * Need to make sure new_value is a multiple of 4. If it is not, 11461 * round it up. For future 64 bit requirement, we actually make it 11462 * a multiple of 8. 11463 */ 11464 if (new_value & 0x7) { 11465 new_value = (new_value & ~0x7) + 0x8; 11466 } 11467 tcppa->tcp_param_val = new_value; 11468 return (0); 11469 } 11470 11471 /* Set callback routine passed to nd_load by tcp_param_register */ 11472 /* ARGSUSED */ 11473 static int 11474 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11475 { 11476 long new_value; 11477 tcpparam_t *tcppa = (tcpparam_t *)cp; 11478 11479 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11480 new_value < tcppa->tcp_param_min || 11481 new_value > tcppa->tcp_param_max) { 11482 return (EINVAL); 11483 } 11484 tcppa->tcp_param_val = new_value; 11485 return (0); 11486 } 11487 11488 /* 11489 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11490 * is filled, return as much as we can. The message passed in may be 11491 * multi-part, chained using b_cont. "start" is the starting sequence 11492 * number for this piece. 11493 */ 11494 static mblk_t * 11495 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11496 { 11497 uint32_t end; 11498 mblk_t *mp1; 11499 mblk_t *mp2; 11500 mblk_t *next_mp; 11501 uint32_t u1; 11502 tcp_stack_t *tcps = tcp->tcp_tcps; 11503 11504 /* Walk through all the new pieces. */ 11505 do { 11506 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11507 (uintptr_t)INT_MAX); 11508 end = start + (int)(mp->b_wptr - mp->b_rptr); 11509 next_mp = mp->b_cont; 11510 if (start == end) { 11511 /* Empty. Blast it. */ 11512 freeb(mp); 11513 continue; 11514 } 11515 mp->b_cont = NULL; 11516 TCP_REASS_SET_SEQ(mp, start); 11517 TCP_REASS_SET_END(mp, end); 11518 mp1 = tcp->tcp_reass_tail; 11519 if (!mp1) { 11520 tcp->tcp_reass_tail = mp; 11521 tcp->tcp_reass_head = mp; 11522 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11523 UPDATE_MIB(&tcps->tcps_mib, 11524 tcpInDataUnorderBytes, end - start); 11525 continue; 11526 } 11527 /* New stuff completely beyond tail? */ 11528 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11529 /* Link it on end. */ 11530 mp1->b_cont = mp; 11531 tcp->tcp_reass_tail = mp; 11532 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11533 UPDATE_MIB(&tcps->tcps_mib, 11534 tcpInDataUnorderBytes, end - start); 11535 continue; 11536 } 11537 mp1 = tcp->tcp_reass_head; 11538 u1 = TCP_REASS_SEQ(mp1); 11539 /* New stuff at the front? */ 11540 if (SEQ_LT(start, u1)) { 11541 /* Yes... Check for overlap. */ 11542 mp->b_cont = mp1; 11543 tcp->tcp_reass_head = mp; 11544 tcp_reass_elim_overlap(tcp, mp); 11545 continue; 11546 } 11547 /* 11548 * The new piece fits somewhere between the head and tail. 11549 * We find our slot, where mp1 precedes us and mp2 trails. 11550 */ 11551 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11552 u1 = TCP_REASS_SEQ(mp2); 11553 if (SEQ_LEQ(start, u1)) 11554 break; 11555 } 11556 /* Link ourselves in */ 11557 mp->b_cont = mp2; 11558 mp1->b_cont = mp; 11559 11560 /* Trim overlap with following mblk(s) first */ 11561 tcp_reass_elim_overlap(tcp, mp); 11562 11563 /* Trim overlap with preceding mblk */ 11564 tcp_reass_elim_overlap(tcp, mp1); 11565 11566 } while (start = end, mp = next_mp); 11567 mp1 = tcp->tcp_reass_head; 11568 /* Anything ready to go? */ 11569 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11570 return (NULL); 11571 /* Eat what we can off the queue */ 11572 for (;;) { 11573 mp = mp1->b_cont; 11574 end = TCP_REASS_END(mp1); 11575 TCP_REASS_SET_SEQ(mp1, 0); 11576 TCP_REASS_SET_END(mp1, 0); 11577 if (!mp) { 11578 tcp->tcp_reass_tail = NULL; 11579 break; 11580 } 11581 if (end != TCP_REASS_SEQ(mp)) { 11582 mp1->b_cont = NULL; 11583 break; 11584 } 11585 mp1 = mp; 11586 } 11587 mp1 = tcp->tcp_reass_head; 11588 tcp->tcp_reass_head = mp; 11589 return (mp1); 11590 } 11591 11592 /* Eliminate any overlap that mp may have over later mblks */ 11593 static void 11594 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11595 { 11596 uint32_t end; 11597 mblk_t *mp1; 11598 uint32_t u1; 11599 tcp_stack_t *tcps = tcp->tcp_tcps; 11600 11601 end = TCP_REASS_END(mp); 11602 while ((mp1 = mp->b_cont) != NULL) { 11603 u1 = TCP_REASS_SEQ(mp1); 11604 if (!SEQ_GT(end, u1)) 11605 break; 11606 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11607 mp->b_wptr -= end - u1; 11608 TCP_REASS_SET_END(mp, u1); 11609 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11610 UPDATE_MIB(&tcps->tcps_mib, 11611 tcpInDataPartDupBytes, end - u1); 11612 break; 11613 } 11614 mp->b_cont = mp1->b_cont; 11615 TCP_REASS_SET_SEQ(mp1, 0); 11616 TCP_REASS_SET_END(mp1, 0); 11617 freeb(mp1); 11618 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11619 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11620 } 11621 if (!mp1) 11622 tcp->tcp_reass_tail = mp; 11623 } 11624 11625 /* 11626 * Send up all messages queued on tcp_rcv_list. 11627 */ 11628 static uint_t 11629 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11630 { 11631 mblk_t *mp; 11632 uint_t ret = 0; 11633 uint_t thwin; 11634 #ifdef DEBUG 11635 uint_t cnt = 0; 11636 #endif 11637 tcp_stack_t *tcps = tcp->tcp_tcps; 11638 11639 /* Can't drain on an eager connection */ 11640 if (tcp->tcp_listener != NULL) 11641 return (ret); 11642 11643 /* 11644 * Handle two cases here: we are currently fused or we were 11645 * previously fused and have some urgent data to be delivered 11646 * upstream. The latter happens because we either ran out of 11647 * memory or were detached and therefore sending the SIGURG was 11648 * deferred until this point. In either case we pass control 11649 * over to tcp_fuse_rcv_drain() since it may need to complete 11650 * some work. 11651 */ 11652 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11653 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11654 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11655 &tcp->tcp_fused_sigurg_mp)) 11656 return (ret); 11657 } 11658 11659 while ((mp = tcp->tcp_rcv_list) != NULL) { 11660 tcp->tcp_rcv_list = mp->b_next; 11661 mp->b_next = NULL; 11662 #ifdef DEBUG 11663 cnt += msgdsize(mp); 11664 #endif 11665 /* Does this need SSL processing first? */ 11666 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11667 tcp_kssl_input(tcp, mp); 11668 continue; 11669 } 11670 putnext(q, mp); 11671 } 11672 ASSERT(cnt == tcp->tcp_rcv_cnt); 11673 tcp->tcp_rcv_last_head = NULL; 11674 tcp->tcp_rcv_last_tail = NULL; 11675 tcp->tcp_rcv_cnt = 0; 11676 11677 /* Learn the latest rwnd information that we sent to the other side. */ 11678 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11679 << tcp->tcp_rcv_ws; 11680 /* This is peer's calculated send window (our receive window). */ 11681 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11682 /* 11683 * Increase the receive window to max. But we need to do receiver 11684 * SWS avoidance. This means that we need to check the increase of 11685 * of receive window is at least 1 MSS. 11686 */ 11687 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11688 /* 11689 * If the window that the other side knows is less than max 11690 * deferred acks segments, send an update immediately. 11691 */ 11692 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11693 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11694 ret = TH_ACK_NEEDED; 11695 } 11696 tcp->tcp_rwnd = q->q_hiwat; 11697 } 11698 /* No need for the push timer now. */ 11699 if (tcp->tcp_push_tid != 0) { 11700 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11701 tcp->tcp_push_tid = 0; 11702 } 11703 return (ret); 11704 } 11705 11706 /* 11707 * Queue data on tcp_rcv_list which is a b_next chain. 11708 * tcp_rcv_last_head/tail is the last element of this chain. 11709 * Each element of the chain is a b_cont chain. 11710 * 11711 * M_DATA messages are added to the current element. 11712 * Other messages are added as new (b_next) elements. 11713 */ 11714 void 11715 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11716 { 11717 ASSERT(seg_len == msgdsize(mp)); 11718 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11719 11720 if (tcp->tcp_rcv_list == NULL) { 11721 ASSERT(tcp->tcp_rcv_last_head == NULL); 11722 tcp->tcp_rcv_list = mp; 11723 tcp->tcp_rcv_last_head = mp; 11724 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11725 tcp->tcp_rcv_last_tail->b_cont = mp; 11726 } else { 11727 tcp->tcp_rcv_last_head->b_next = mp; 11728 tcp->tcp_rcv_last_head = mp; 11729 } 11730 11731 while (mp->b_cont) 11732 mp = mp->b_cont; 11733 11734 tcp->tcp_rcv_last_tail = mp; 11735 tcp->tcp_rcv_cnt += seg_len; 11736 tcp->tcp_rwnd -= seg_len; 11737 } 11738 11739 /* 11740 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11741 * 11742 * This is the default entry function into TCP on the read side. TCP is 11743 * always entered via squeue i.e. using squeue's for mutual exclusion. 11744 * When classifier does a lookup to find the tcp, it also puts a reference 11745 * on the conn structure associated so the tcp is guaranteed to exist 11746 * when we come here. We still need to check the state because it might 11747 * as well has been closed. The squeue processing function i.e. squeue_enter, 11748 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 11749 * CONN_DEC_REF. 11750 * 11751 * Apart from the default entry point, IP also sends packets directly to 11752 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11753 * connections. 11754 */ 11755 void 11756 tcp_input(void *arg, mblk_t *mp, void *arg2) 11757 { 11758 conn_t *connp = (conn_t *)arg; 11759 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11760 11761 /* arg2 is the sqp */ 11762 ASSERT(arg2 != NULL); 11763 ASSERT(mp != NULL); 11764 11765 /* 11766 * Don't accept any input on a closed tcp as this TCP logically does 11767 * not exist on the system. Don't proceed further with this TCP. 11768 * For eg. this packet could trigger another close of this tcp 11769 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11770 * tcp_clean_death / tcp_closei_local must be called at most once 11771 * on a TCP. In this case we need to refeed the packet into the 11772 * classifier and figure out where the packet should go. Need to 11773 * preserve the recv_ill somehow. Until we figure that out, for 11774 * now just drop the packet if we can't classify the packet. 11775 */ 11776 if (tcp->tcp_state == TCPS_CLOSED || 11777 tcp->tcp_state == TCPS_BOUND) { 11778 conn_t *new_connp; 11779 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11780 11781 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11782 if (new_connp != NULL) { 11783 tcp_reinput(new_connp, mp, arg2); 11784 return; 11785 } 11786 /* We failed to classify. For now just drop the packet */ 11787 freemsg(mp); 11788 return; 11789 } 11790 11791 if (DB_TYPE(mp) == M_DATA) 11792 tcp_rput_data(connp, mp, arg2); 11793 else 11794 tcp_rput_common(tcp, mp); 11795 } 11796 11797 /* 11798 * The read side put procedure. 11799 * The packets passed up by ip are assume to be aligned according to 11800 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11801 */ 11802 static void 11803 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11804 { 11805 /* 11806 * tcp_rput_data() does not expect M_CTL except for the case 11807 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11808 * type. Need to make sure that any other M_CTLs don't make 11809 * it to tcp_rput_data since it is not expecting any and doesn't 11810 * check for it. 11811 */ 11812 if (DB_TYPE(mp) == M_CTL) { 11813 switch (*(uint32_t *)(mp->b_rptr)) { 11814 case TCP_IOC_ABORT_CONN: 11815 /* 11816 * Handle connection abort request. 11817 */ 11818 tcp_ioctl_abort_handler(tcp, mp); 11819 return; 11820 case IPSEC_IN: 11821 /* 11822 * Only secure icmp arrive in TCP and they 11823 * don't go through data path. 11824 */ 11825 tcp_icmp_error(tcp, mp); 11826 return; 11827 case IN_PKTINFO: 11828 /* 11829 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11830 * sockets that are receiving IPv4 traffic. tcp 11831 */ 11832 ASSERT(tcp->tcp_family == AF_INET6); 11833 ASSERT(tcp->tcp_ipv6_recvancillary & 11834 TCP_IPV6_RECVPKTINFO); 11835 tcp_rput_data(tcp->tcp_connp, mp, 11836 tcp->tcp_connp->conn_sqp); 11837 return; 11838 case MDT_IOC_INFO_UPDATE: 11839 /* 11840 * Handle Multidata information update; the 11841 * following routine will free the message. 11842 */ 11843 if (tcp->tcp_connp->conn_mdt_ok) { 11844 tcp_mdt_update(tcp, 11845 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11846 B_FALSE); 11847 } 11848 freemsg(mp); 11849 return; 11850 case LSO_IOC_INFO_UPDATE: 11851 /* 11852 * Handle LSO information update; the following 11853 * routine will free the message. 11854 */ 11855 if (tcp->tcp_connp->conn_lso_ok) { 11856 tcp_lso_update(tcp, 11857 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11858 } 11859 freemsg(mp); 11860 return; 11861 default: 11862 /* 11863 * tcp_icmp_err() will process the M_CTL packets. 11864 * Non-ICMP packets, if any, will be discarded in 11865 * tcp_icmp_err(). We will process the ICMP packet 11866 * even if we are TCP_IS_DETACHED_NONEAGER as the 11867 * incoming ICMP packet may result in changing 11868 * the tcp_mss, which we would need if we have 11869 * packets to retransmit. 11870 */ 11871 tcp_icmp_error(tcp, mp); 11872 return; 11873 } 11874 } 11875 11876 /* No point processing the message if tcp is already closed */ 11877 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11878 freemsg(mp); 11879 return; 11880 } 11881 11882 tcp_rput_other(tcp, mp); 11883 } 11884 11885 11886 /* The minimum of smoothed mean deviation in RTO calculation. */ 11887 #define TCP_SD_MIN 400 11888 11889 /* 11890 * Set RTO for this connection. The formula is from Jacobson and Karels' 11891 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11892 * are the same as those in Appendix A.2 of that paper. 11893 * 11894 * m = new measurement 11895 * sa = smoothed RTT average (8 * average estimates). 11896 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11897 */ 11898 static void 11899 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11900 { 11901 long m = TICK_TO_MSEC(rtt); 11902 clock_t sa = tcp->tcp_rtt_sa; 11903 clock_t sv = tcp->tcp_rtt_sd; 11904 clock_t rto; 11905 tcp_stack_t *tcps = tcp->tcp_tcps; 11906 11907 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11908 tcp->tcp_rtt_update++; 11909 11910 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11911 if (sa != 0) { 11912 /* 11913 * Update average estimator: 11914 * new rtt = 7/8 old rtt + 1/8 Error 11915 */ 11916 11917 /* m is now Error in estimate. */ 11918 m -= sa >> 3; 11919 if ((sa += m) <= 0) { 11920 /* 11921 * Don't allow the smoothed average to be negative. 11922 * We use 0 to denote reinitialization of the 11923 * variables. 11924 */ 11925 sa = 1; 11926 } 11927 11928 /* 11929 * Update deviation estimator: 11930 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11931 */ 11932 if (m < 0) 11933 m = -m; 11934 m -= sv >> 2; 11935 sv += m; 11936 } else { 11937 /* 11938 * This follows BSD's implementation. So the reinitialized 11939 * RTO is 3 * m. We cannot go less than 2 because if the 11940 * link is bandwidth dominated, doubling the window size 11941 * during slow start means doubling the RTT. We want to be 11942 * more conservative when we reinitialize our estimates. 3 11943 * is just a convenient number. 11944 */ 11945 sa = m << 3; 11946 sv = m << 1; 11947 } 11948 if (sv < TCP_SD_MIN) { 11949 /* 11950 * We do not know that if sa captures the delay ACK 11951 * effect as in a long train of segments, a receiver 11952 * does not delay its ACKs. So set the minimum of sv 11953 * to be TCP_SD_MIN, which is default to 400 ms, twice 11954 * of BSD DATO. That means the minimum of mean 11955 * deviation is 100 ms. 11956 * 11957 */ 11958 sv = TCP_SD_MIN; 11959 } 11960 tcp->tcp_rtt_sa = sa; 11961 tcp->tcp_rtt_sd = sv; 11962 /* 11963 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11964 * 11965 * Add tcp_rexmit_interval extra in case of extreme environment 11966 * where the algorithm fails to work. The default value of 11967 * tcp_rexmit_interval_extra should be 0. 11968 * 11969 * As we use a finer grained clock than BSD and update 11970 * RTO for every ACKs, add in another .25 of RTT to the 11971 * deviation of RTO to accomodate burstiness of 1/4 of 11972 * window size. 11973 */ 11974 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 11975 11976 if (rto > tcps->tcps_rexmit_interval_max) { 11977 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 11978 } else if (rto < tcps->tcps_rexmit_interval_min) { 11979 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 11980 } else { 11981 tcp->tcp_rto = rto; 11982 } 11983 11984 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11985 tcp->tcp_timer_backoff = 0; 11986 } 11987 11988 /* 11989 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11990 * send queue which starts at the given seq. no. 11991 * 11992 * Parameters: 11993 * tcp_t *tcp: the tcp instance pointer. 11994 * uint32_t seq: the starting seq. no of the requested segment. 11995 * int32_t *off: after the execution, *off will be the offset to 11996 * the returned mblk which points to the requested seq no. 11997 * It is the caller's responsibility to send in a non-null off. 11998 * 11999 * Return: 12000 * A mblk_t pointer pointing to the requested segment in send queue. 12001 */ 12002 static mblk_t * 12003 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12004 { 12005 int32_t cnt; 12006 mblk_t *mp; 12007 12008 /* Defensive coding. Make sure we don't send incorrect data. */ 12009 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12010 return (NULL); 12011 12012 cnt = seq - tcp->tcp_suna; 12013 mp = tcp->tcp_xmit_head; 12014 while (cnt > 0 && mp != NULL) { 12015 cnt -= mp->b_wptr - mp->b_rptr; 12016 if (cnt < 0) { 12017 cnt += mp->b_wptr - mp->b_rptr; 12018 break; 12019 } 12020 mp = mp->b_cont; 12021 } 12022 ASSERT(mp != NULL); 12023 *off = cnt; 12024 return (mp); 12025 } 12026 12027 /* 12028 * This function handles all retransmissions if SACK is enabled for this 12029 * connection. First it calculates how many segments can be retransmitted 12030 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12031 * segments. A segment is eligible if sack_cnt for that segment is greater 12032 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12033 * all eligible segments, it checks to see if TCP can send some new segments 12034 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12035 * 12036 * Parameters: 12037 * tcp_t *tcp: the tcp structure of the connection. 12038 * uint_t *flags: in return, appropriate value will be set for 12039 * tcp_rput_data(). 12040 */ 12041 static void 12042 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12043 { 12044 notsack_blk_t *notsack_blk; 12045 int32_t usable_swnd; 12046 int32_t mss; 12047 uint32_t seg_len; 12048 mblk_t *xmit_mp; 12049 tcp_stack_t *tcps = tcp->tcp_tcps; 12050 12051 ASSERT(tcp->tcp_sack_info != NULL); 12052 ASSERT(tcp->tcp_notsack_list != NULL); 12053 ASSERT(tcp->tcp_rexmit == B_FALSE); 12054 12055 /* Defensive coding in case there is a bug... */ 12056 if (tcp->tcp_notsack_list == NULL) { 12057 return; 12058 } 12059 notsack_blk = tcp->tcp_notsack_list; 12060 mss = tcp->tcp_mss; 12061 12062 /* 12063 * Limit the num of outstanding data in the network to be 12064 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12065 */ 12066 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12067 12068 /* At least retransmit 1 MSS of data. */ 12069 if (usable_swnd <= 0) { 12070 usable_swnd = mss; 12071 } 12072 12073 /* Make sure no new RTT samples will be taken. */ 12074 tcp->tcp_csuna = tcp->tcp_snxt; 12075 12076 notsack_blk = tcp->tcp_notsack_list; 12077 while (usable_swnd > 0) { 12078 mblk_t *snxt_mp, *tmp_mp; 12079 tcp_seq begin = tcp->tcp_sack_snxt; 12080 tcp_seq end; 12081 int32_t off; 12082 12083 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12084 if (SEQ_GT(notsack_blk->end, begin) && 12085 (notsack_blk->sack_cnt >= 12086 tcps->tcps_dupack_fast_retransmit)) { 12087 end = notsack_blk->end; 12088 if (SEQ_LT(begin, notsack_blk->begin)) { 12089 begin = notsack_blk->begin; 12090 } 12091 break; 12092 } 12093 } 12094 /* 12095 * All holes are filled. Manipulate tcp_cwnd to send more 12096 * if we can. Note that after the SACK recovery, tcp_cwnd is 12097 * set to tcp_cwnd_ssthresh. 12098 */ 12099 if (notsack_blk == NULL) { 12100 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12101 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12102 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12103 ASSERT(tcp->tcp_cwnd > 0); 12104 return; 12105 } else { 12106 usable_swnd = usable_swnd / mss; 12107 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12108 MAX(usable_swnd * mss, mss); 12109 *flags |= TH_XMIT_NEEDED; 12110 return; 12111 } 12112 } 12113 12114 /* 12115 * Note that we may send more than usable_swnd allows here 12116 * because of round off, but no more than 1 MSS of data. 12117 */ 12118 seg_len = end - begin; 12119 if (seg_len > mss) 12120 seg_len = mss; 12121 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12122 ASSERT(snxt_mp != NULL); 12123 /* This should not happen. Defensive coding again... */ 12124 if (snxt_mp == NULL) { 12125 return; 12126 } 12127 12128 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12129 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12130 if (xmit_mp == NULL) 12131 return; 12132 12133 usable_swnd -= seg_len; 12134 tcp->tcp_pipe += seg_len; 12135 tcp->tcp_sack_snxt = begin + seg_len; 12136 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 12137 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12138 12139 /* 12140 * Update the send timestamp to avoid false retransmission. 12141 */ 12142 snxt_mp->b_prev = (mblk_t *)lbolt; 12143 12144 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12145 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12146 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12147 /* 12148 * Update tcp_rexmit_max to extend this SACK recovery phase. 12149 * This happens when new data sent during fast recovery is 12150 * also lost. If TCP retransmits those new data, it needs 12151 * to extend SACK recover phase to avoid starting another 12152 * fast retransmit/recovery unnecessarily. 12153 */ 12154 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12155 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12156 } 12157 } 12158 } 12159 12160 /* 12161 * This function handles policy checking at TCP level for non-hard_bound/ 12162 * detached connections. 12163 */ 12164 static boolean_t 12165 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12166 boolean_t secure, boolean_t mctl_present) 12167 { 12168 ipsec_latch_t *ipl = NULL; 12169 ipsec_action_t *act = NULL; 12170 mblk_t *data_mp; 12171 ipsec_in_t *ii; 12172 const char *reason; 12173 kstat_named_t *counter; 12174 tcp_stack_t *tcps = tcp->tcp_tcps; 12175 ipsec_stack_t *ipss; 12176 ip_stack_t *ipst; 12177 12178 ASSERT(mctl_present || !secure); 12179 12180 ASSERT((ipha == NULL && ip6h != NULL) || 12181 (ip6h == NULL && ipha != NULL)); 12182 12183 /* 12184 * We don't necessarily have an ipsec_in_act action to verify 12185 * policy because of assymetrical policy where we have only 12186 * outbound policy and no inbound policy (possible with global 12187 * policy). 12188 */ 12189 if (!secure) { 12190 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12191 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12192 return (B_TRUE); 12193 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12194 "tcp_check_policy", ipha, ip6h, secure, 12195 tcps->tcps_netstack); 12196 ipss = tcps->tcps_netstack->netstack_ipsec; 12197 12198 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12199 DROPPER(ipss, ipds_tcp_clear), 12200 &tcps->tcps_dropper); 12201 return (B_FALSE); 12202 } 12203 12204 /* 12205 * We have a secure packet. 12206 */ 12207 if (act == NULL) { 12208 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12209 "tcp_check_policy", ipha, ip6h, secure, 12210 tcps->tcps_netstack); 12211 ipss = tcps->tcps_netstack->netstack_ipsec; 12212 12213 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12214 DROPPER(ipss, ipds_tcp_secure), 12215 &tcps->tcps_dropper); 12216 return (B_FALSE); 12217 } 12218 12219 /* 12220 * XXX This whole routine is currently incorrect. ipl should 12221 * be set to the latch pointer, but is currently not set, so 12222 * we initialize it to NULL to avoid picking up random garbage. 12223 */ 12224 if (ipl == NULL) 12225 return (B_TRUE); 12226 12227 data_mp = first_mp->b_cont; 12228 12229 ii = (ipsec_in_t *)first_mp->b_rptr; 12230 12231 ipst = tcps->tcps_netstack->netstack_ip; 12232 12233 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12234 &counter, tcp->tcp_connp)) { 12235 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12236 return (B_TRUE); 12237 } 12238 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12239 "tcp inbound policy mismatch: %s, packet dropped\n", 12240 reason); 12241 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12242 12243 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12244 &tcps->tcps_dropper); 12245 return (B_FALSE); 12246 } 12247 12248 /* 12249 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12250 * retransmission after a timeout. 12251 * 12252 * To limit the number of duplicate segments, we limit the number of segment 12253 * to be sent in one time to tcp_snd_burst, the burst variable. 12254 */ 12255 static void 12256 tcp_ss_rexmit(tcp_t *tcp) 12257 { 12258 uint32_t snxt; 12259 uint32_t smax; 12260 int32_t win; 12261 int32_t mss; 12262 int32_t off; 12263 int32_t burst = tcp->tcp_snd_burst; 12264 mblk_t *snxt_mp; 12265 tcp_stack_t *tcps = tcp->tcp_tcps; 12266 12267 /* 12268 * Note that tcp_rexmit can be set even though TCP has retransmitted 12269 * all unack'ed segments. 12270 */ 12271 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12272 smax = tcp->tcp_rexmit_max; 12273 snxt = tcp->tcp_rexmit_nxt; 12274 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12275 snxt = tcp->tcp_suna; 12276 } 12277 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12278 win -= snxt - tcp->tcp_suna; 12279 mss = tcp->tcp_mss; 12280 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12281 12282 while (SEQ_LT(snxt, smax) && (win > 0) && 12283 (burst > 0) && (snxt_mp != NULL)) { 12284 mblk_t *xmit_mp; 12285 mblk_t *old_snxt_mp = snxt_mp; 12286 uint32_t cnt = mss; 12287 12288 if (win < cnt) { 12289 cnt = win; 12290 } 12291 if (SEQ_GT(snxt + cnt, smax)) { 12292 cnt = smax - snxt; 12293 } 12294 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12295 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12296 if (xmit_mp == NULL) 12297 return; 12298 12299 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12300 12301 snxt += cnt; 12302 win -= cnt; 12303 /* 12304 * Update the send timestamp to avoid false 12305 * retransmission. 12306 */ 12307 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12308 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12309 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12310 12311 tcp->tcp_rexmit_nxt = snxt; 12312 burst--; 12313 } 12314 /* 12315 * If we have transmitted all we have at the time 12316 * we started the retranmission, we can leave 12317 * the rest of the job to tcp_wput_data(). But we 12318 * need to check the send window first. If the 12319 * win is not 0, go on with tcp_wput_data(). 12320 */ 12321 if (SEQ_LT(snxt, smax) || win == 0) { 12322 return; 12323 } 12324 } 12325 /* Only call tcp_wput_data() if there is data to be sent. */ 12326 if (tcp->tcp_unsent) { 12327 tcp_wput_data(tcp, NULL, B_FALSE); 12328 } 12329 } 12330 12331 /* 12332 * Process all TCP option in SYN segment. Note that this function should 12333 * be called after tcp_adapt_ire() is called so that the necessary info 12334 * from IRE is already set in the tcp structure. 12335 * 12336 * This function sets up the correct tcp_mss value according to the 12337 * MSS option value and our header size. It also sets up the window scale 12338 * and timestamp values, and initialize SACK info blocks. But it does not 12339 * change receive window size after setting the tcp_mss value. The caller 12340 * should do the appropriate change. 12341 */ 12342 void 12343 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12344 { 12345 int options; 12346 tcp_opt_t tcpopt; 12347 uint32_t mss_max; 12348 char *tmp_tcph; 12349 tcp_stack_t *tcps = tcp->tcp_tcps; 12350 12351 tcpopt.tcp = NULL; 12352 options = tcp_parse_options(tcph, &tcpopt); 12353 12354 /* 12355 * Process MSS option. Note that MSS option value does not account 12356 * for IP or TCP options. This means that it is equal to MTU - minimum 12357 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12358 * IPv6. 12359 */ 12360 if (!(options & TCP_OPT_MSS_PRESENT)) { 12361 if (tcp->tcp_ipversion == IPV4_VERSION) 12362 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12363 else 12364 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12365 } else { 12366 if (tcp->tcp_ipversion == IPV4_VERSION) 12367 mss_max = tcps->tcps_mss_max_ipv4; 12368 else 12369 mss_max = tcps->tcps_mss_max_ipv6; 12370 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12371 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12372 else if (tcpopt.tcp_opt_mss > mss_max) 12373 tcpopt.tcp_opt_mss = mss_max; 12374 } 12375 12376 /* Process Window Scale option. */ 12377 if (options & TCP_OPT_WSCALE_PRESENT) { 12378 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12379 tcp->tcp_snd_ws_ok = B_TRUE; 12380 } else { 12381 tcp->tcp_snd_ws = B_FALSE; 12382 tcp->tcp_snd_ws_ok = B_FALSE; 12383 tcp->tcp_rcv_ws = B_FALSE; 12384 } 12385 12386 /* Process Timestamp option. */ 12387 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12388 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12389 tmp_tcph = (char *)tcp->tcp_tcph; 12390 12391 tcp->tcp_snd_ts_ok = B_TRUE; 12392 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12393 tcp->tcp_last_rcv_lbolt = lbolt64; 12394 ASSERT(OK_32PTR(tmp_tcph)); 12395 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12396 12397 /* Fill in our template header with basic timestamp option. */ 12398 tmp_tcph += tcp->tcp_tcp_hdr_len; 12399 tmp_tcph[0] = TCPOPT_NOP; 12400 tmp_tcph[1] = TCPOPT_NOP; 12401 tmp_tcph[2] = TCPOPT_TSTAMP; 12402 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12403 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12404 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12405 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12406 } else { 12407 tcp->tcp_snd_ts_ok = B_FALSE; 12408 } 12409 12410 /* 12411 * Process SACK options. If SACK is enabled for this connection, 12412 * then allocate the SACK info structure. Note the following ways 12413 * when tcp_snd_sack_ok is set to true. 12414 * 12415 * For active connection: in tcp_adapt_ire() called in 12416 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12417 * is checked. 12418 * 12419 * For passive connection: in tcp_adapt_ire() called in 12420 * tcp_accept_comm(). 12421 * 12422 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12423 * That check makes sure that if we did not send a SACK OK option, 12424 * we will not enable SACK for this connection even though the other 12425 * side sends us SACK OK option. For active connection, the SACK 12426 * info structure has already been allocated. So we need to free 12427 * it if SACK is disabled. 12428 */ 12429 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12430 (tcp->tcp_snd_sack_ok || 12431 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12432 /* This should be true only in the passive case. */ 12433 if (tcp->tcp_sack_info == NULL) { 12434 ASSERT(TCP_IS_DETACHED(tcp)); 12435 tcp->tcp_sack_info = 12436 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12437 } 12438 if (tcp->tcp_sack_info == NULL) { 12439 tcp->tcp_snd_sack_ok = B_FALSE; 12440 } else { 12441 tcp->tcp_snd_sack_ok = B_TRUE; 12442 if (tcp->tcp_snd_ts_ok) { 12443 tcp->tcp_max_sack_blk = 3; 12444 } else { 12445 tcp->tcp_max_sack_blk = 4; 12446 } 12447 } 12448 } else { 12449 /* 12450 * Resetting tcp_snd_sack_ok to B_FALSE so that 12451 * no SACK info will be used for this 12452 * connection. This assumes that SACK usage 12453 * permission is negotiated. This may need 12454 * to be changed once this is clarified. 12455 */ 12456 if (tcp->tcp_sack_info != NULL) { 12457 ASSERT(tcp->tcp_notsack_list == NULL); 12458 kmem_cache_free(tcp_sack_info_cache, 12459 tcp->tcp_sack_info); 12460 tcp->tcp_sack_info = NULL; 12461 } 12462 tcp->tcp_snd_sack_ok = B_FALSE; 12463 } 12464 12465 /* 12466 * Now we know the exact TCP/IP header length, subtract 12467 * that from tcp_mss to get our side's MSS. 12468 */ 12469 tcp->tcp_mss -= tcp->tcp_hdr_len; 12470 /* 12471 * Here we assume that the other side's header size will be equal to 12472 * our header size. We calculate the real MSS accordingly. Need to 12473 * take into additional stuffs IPsec puts in. 12474 * 12475 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12476 */ 12477 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12478 ((tcp->tcp_ipversion == IPV4_VERSION ? 12479 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12480 12481 /* 12482 * Set MSS to the smaller one of both ends of the connection. 12483 * We should not have called tcp_mss_set() before, but our 12484 * side of the MSS should have been set to a proper value 12485 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12486 * STREAM head parameters properly. 12487 * 12488 * If we have a larger-than-16-bit window but the other side 12489 * didn't want to do window scale, tcp_rwnd_set() will take 12490 * care of that. 12491 */ 12492 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss)); 12493 } 12494 12495 /* 12496 * Sends the T_CONN_IND to the listener. The caller calls this 12497 * functions via squeue to get inside the listener's perimeter 12498 * once the 3 way hand shake is done a T_CONN_IND needs to be 12499 * sent. As an optimization, the caller can call this directly 12500 * if listener's perimeter is same as eager's. 12501 */ 12502 /* ARGSUSED */ 12503 void 12504 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12505 { 12506 conn_t *lconnp = (conn_t *)arg; 12507 tcp_t *listener = lconnp->conn_tcp; 12508 tcp_t *tcp; 12509 struct T_conn_ind *conn_ind; 12510 ipaddr_t *addr_cache; 12511 boolean_t need_send_conn_ind = B_FALSE; 12512 tcp_stack_t *tcps = listener->tcp_tcps; 12513 12514 /* retrieve the eager */ 12515 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12516 ASSERT(conn_ind->OPT_offset != 0 && 12517 conn_ind->OPT_length == sizeof (intptr_t)); 12518 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12519 conn_ind->OPT_length); 12520 12521 /* 12522 * TLI/XTI applications will get confused by 12523 * sending eager as an option since it violates 12524 * the option semantics. So remove the eager as 12525 * option since TLI/XTI app doesn't need it anyway. 12526 */ 12527 if (!TCP_IS_SOCKET(listener)) { 12528 conn_ind->OPT_length = 0; 12529 conn_ind->OPT_offset = 0; 12530 } 12531 if (listener->tcp_state == TCPS_CLOSED || 12532 TCP_IS_DETACHED(listener)) { 12533 /* 12534 * If listener has closed, it would have caused a 12535 * a cleanup/blowoff to happen for the eager. We 12536 * just need to return. 12537 */ 12538 freemsg(mp); 12539 return; 12540 } 12541 12542 12543 /* 12544 * if the conn_req_q is full defer passing up the 12545 * T_CONN_IND until space is availabe after t_accept() 12546 * processing 12547 */ 12548 mutex_enter(&listener->tcp_eager_lock); 12549 12550 /* 12551 * Take the eager out, if it is in the list of droppable eagers 12552 * as we are here because the 3W handshake is over. 12553 */ 12554 MAKE_UNDROPPABLE(tcp); 12555 12556 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12557 tcp_t *tail; 12558 12559 /* 12560 * The eager already has an extra ref put in tcp_rput_data 12561 * so that it stays till accept comes back even though it 12562 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12563 */ 12564 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12565 listener->tcp_conn_req_cnt_q0--; 12566 listener->tcp_conn_req_cnt_q++; 12567 12568 /* Move from SYN_RCVD to ESTABLISHED list */ 12569 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12570 tcp->tcp_eager_prev_q0; 12571 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12572 tcp->tcp_eager_next_q0; 12573 tcp->tcp_eager_prev_q0 = NULL; 12574 tcp->tcp_eager_next_q0 = NULL; 12575 12576 /* 12577 * Insert at end of the queue because sockfs 12578 * sends down T_CONN_RES in chronological 12579 * order. Leaving the older conn indications 12580 * at front of the queue helps reducing search 12581 * time. 12582 */ 12583 tail = listener->tcp_eager_last_q; 12584 if (tail != NULL) 12585 tail->tcp_eager_next_q = tcp; 12586 else 12587 listener->tcp_eager_next_q = tcp; 12588 listener->tcp_eager_last_q = tcp; 12589 tcp->tcp_eager_next_q = NULL; 12590 /* 12591 * Delay sending up the T_conn_ind until we are 12592 * done with the eager. Once we have have sent up 12593 * the T_conn_ind, the accept can potentially complete 12594 * any time and release the refhold we have on the eager. 12595 */ 12596 need_send_conn_ind = B_TRUE; 12597 } else { 12598 /* 12599 * Defer connection on q0 and set deferred 12600 * connection bit true 12601 */ 12602 tcp->tcp_conn_def_q0 = B_TRUE; 12603 12604 /* take tcp out of q0 ... */ 12605 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12606 tcp->tcp_eager_next_q0; 12607 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12608 tcp->tcp_eager_prev_q0; 12609 12610 /* ... and place it at the end of q0 */ 12611 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12612 tcp->tcp_eager_next_q0 = listener; 12613 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12614 listener->tcp_eager_prev_q0 = tcp; 12615 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12616 } 12617 12618 /* we have timed out before */ 12619 if (tcp->tcp_syn_rcvd_timeout != 0) { 12620 tcp->tcp_syn_rcvd_timeout = 0; 12621 listener->tcp_syn_rcvd_timeout--; 12622 if (listener->tcp_syn_defense && 12623 listener->tcp_syn_rcvd_timeout <= 12624 (tcps->tcps_conn_req_max_q0 >> 5) && 12625 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12626 listener->tcp_last_rcv_lbolt)) { 12627 /* 12628 * Turn off the defense mode if we 12629 * believe the SYN attack is over. 12630 */ 12631 listener->tcp_syn_defense = B_FALSE; 12632 if (listener->tcp_ip_addr_cache) { 12633 kmem_free((void *)listener->tcp_ip_addr_cache, 12634 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12635 listener->tcp_ip_addr_cache = NULL; 12636 } 12637 } 12638 } 12639 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12640 if (addr_cache != NULL) { 12641 /* 12642 * We have finished a 3-way handshake with this 12643 * remote host. This proves the IP addr is good. 12644 * Cache it! 12645 */ 12646 addr_cache[IP_ADDR_CACHE_HASH( 12647 tcp->tcp_remote)] = tcp->tcp_remote; 12648 } 12649 mutex_exit(&listener->tcp_eager_lock); 12650 if (need_send_conn_ind) 12651 putnext(listener->tcp_rq, mp); 12652 } 12653 12654 mblk_t * 12655 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12656 uint_t *ifindexp, ip6_pkt_t *ippp) 12657 { 12658 ip_pktinfo_t *pinfo; 12659 ip6_t *ip6h; 12660 uchar_t *rptr; 12661 mblk_t *first_mp = mp; 12662 boolean_t mctl_present = B_FALSE; 12663 uint_t ifindex = 0; 12664 ip6_pkt_t ipp; 12665 uint_t ipvers; 12666 uint_t ip_hdr_len; 12667 tcp_stack_t *tcps = tcp->tcp_tcps; 12668 12669 rptr = mp->b_rptr; 12670 ASSERT(OK_32PTR(rptr)); 12671 ASSERT(tcp != NULL); 12672 ipp.ipp_fields = 0; 12673 12674 switch DB_TYPE(mp) { 12675 case M_CTL: 12676 mp = mp->b_cont; 12677 if (mp == NULL) { 12678 freemsg(first_mp); 12679 return (NULL); 12680 } 12681 if (DB_TYPE(mp) != M_DATA) { 12682 freemsg(first_mp); 12683 return (NULL); 12684 } 12685 mctl_present = B_TRUE; 12686 break; 12687 case M_DATA: 12688 break; 12689 default: 12690 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12691 freemsg(mp); 12692 return (NULL); 12693 } 12694 ipvers = IPH_HDR_VERSION(rptr); 12695 if (ipvers == IPV4_VERSION) { 12696 if (tcp == NULL) { 12697 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12698 goto done; 12699 } 12700 12701 ipp.ipp_fields |= IPPF_HOPLIMIT; 12702 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12703 12704 /* 12705 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12706 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12707 */ 12708 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12709 mctl_present) { 12710 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12711 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12712 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12713 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12714 ipp.ipp_fields |= IPPF_IFINDEX; 12715 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12716 ifindex = pinfo->ip_pkt_ifindex; 12717 } 12718 freeb(first_mp); 12719 mctl_present = B_FALSE; 12720 } 12721 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12722 } else { 12723 ip6h = (ip6_t *)rptr; 12724 12725 ASSERT(ipvers == IPV6_VERSION); 12726 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12727 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12728 ipp.ipp_hoplimit = ip6h->ip6_hops; 12729 12730 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12731 uint8_t nexthdrp; 12732 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12733 12734 /* Look for ifindex information */ 12735 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12736 ip6i_t *ip6i = (ip6i_t *)ip6h; 12737 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12738 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12739 freemsg(first_mp); 12740 return (NULL); 12741 } 12742 12743 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12744 ASSERT(ip6i->ip6i_ifindex != 0); 12745 ipp.ipp_fields |= IPPF_IFINDEX; 12746 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12747 ifindex = ip6i->ip6i_ifindex; 12748 } 12749 rptr = (uchar_t *)&ip6i[1]; 12750 mp->b_rptr = rptr; 12751 if (rptr == mp->b_wptr) { 12752 mblk_t *mp1; 12753 mp1 = mp->b_cont; 12754 freeb(mp); 12755 mp = mp1; 12756 rptr = mp->b_rptr; 12757 } 12758 if (MBLKL(mp) < IPV6_HDR_LEN + 12759 sizeof (tcph_t)) { 12760 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12761 freemsg(first_mp); 12762 return (NULL); 12763 } 12764 ip6h = (ip6_t *)rptr; 12765 } 12766 12767 /* 12768 * Find any potentially interesting extension headers 12769 * as well as the length of the IPv6 + extension 12770 * headers. 12771 */ 12772 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12773 /* Verify if this is a TCP packet */ 12774 if (nexthdrp != IPPROTO_TCP) { 12775 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12776 freemsg(first_mp); 12777 return (NULL); 12778 } 12779 } else { 12780 ip_hdr_len = IPV6_HDR_LEN; 12781 } 12782 } 12783 12784 done: 12785 if (ipversp != NULL) 12786 *ipversp = ipvers; 12787 if (ip_hdr_lenp != NULL) 12788 *ip_hdr_lenp = ip_hdr_len; 12789 if (ippp != NULL) 12790 *ippp = ipp; 12791 if (ifindexp != NULL) 12792 *ifindexp = ifindex; 12793 if (mctl_present) { 12794 freeb(first_mp); 12795 } 12796 return (mp); 12797 } 12798 12799 /* 12800 * Handle M_DATA messages from IP. Its called directly from IP via 12801 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12802 * in this path. 12803 * 12804 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12805 * v4 and v6), we are called through tcp_input() and a M_CTL can 12806 * be present for options but tcp_find_pktinfo() deals with it. We 12807 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12808 * 12809 * The first argument is always the connp/tcp to which the mp belongs. 12810 * There are no exceptions to this rule. The caller has already put 12811 * a reference on this connp/tcp and once tcp_rput_data() returns, 12812 * the squeue will do the refrele. 12813 * 12814 * The TH_SYN for the listener directly go to tcp_conn_request via 12815 * squeue. 12816 * 12817 * sqp: NULL = recursive, sqp != NULL means called from squeue 12818 */ 12819 void 12820 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12821 { 12822 int32_t bytes_acked; 12823 int32_t gap; 12824 mblk_t *mp1; 12825 uint_t flags; 12826 uint32_t new_swnd = 0; 12827 uchar_t *iphdr; 12828 uchar_t *rptr; 12829 int32_t rgap; 12830 uint32_t seg_ack; 12831 int seg_len; 12832 uint_t ip_hdr_len; 12833 uint32_t seg_seq; 12834 tcph_t *tcph; 12835 int urp; 12836 tcp_opt_t tcpopt; 12837 uint_t ipvers; 12838 ip6_pkt_t ipp; 12839 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12840 uint32_t cwnd; 12841 uint32_t add; 12842 int npkt; 12843 int mss; 12844 conn_t *connp = (conn_t *)arg; 12845 squeue_t *sqp = (squeue_t *)arg2; 12846 tcp_t *tcp = connp->conn_tcp; 12847 tcp_stack_t *tcps = tcp->tcp_tcps; 12848 12849 /* 12850 * RST from fused tcp loopback peer should trigger an unfuse. 12851 */ 12852 if (tcp->tcp_fused) { 12853 TCP_STAT(tcps, tcp_fusion_aborted); 12854 tcp_unfuse(tcp); 12855 } 12856 12857 iphdr = mp->b_rptr; 12858 rptr = mp->b_rptr; 12859 ASSERT(OK_32PTR(rptr)); 12860 12861 /* 12862 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12863 * processing here. For rest call tcp_find_pktinfo to fill up the 12864 * necessary information. 12865 */ 12866 if (IPCL_IS_TCP4(connp)) { 12867 ipvers = IPV4_VERSION; 12868 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12869 } else { 12870 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12871 NULL, &ipp); 12872 if (mp == NULL) { 12873 TCP_STAT(tcps, tcp_rput_v6_error); 12874 return; 12875 } 12876 iphdr = mp->b_rptr; 12877 rptr = mp->b_rptr; 12878 } 12879 ASSERT(DB_TYPE(mp) == M_DATA); 12880 12881 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12882 seg_seq = ABE32_TO_U32(tcph->th_seq); 12883 seg_ack = ABE32_TO_U32(tcph->th_ack); 12884 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12885 seg_len = (int)(mp->b_wptr - rptr) - 12886 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12887 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12888 do { 12889 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12890 (uintptr_t)INT_MAX); 12891 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12892 } while ((mp1 = mp1->b_cont) != NULL && 12893 mp1->b_datap->db_type == M_DATA); 12894 } 12895 12896 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12897 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12898 seg_len, tcph); 12899 return; 12900 } 12901 12902 if (sqp != NULL) { 12903 /* 12904 * This is the correct place to update tcp_last_recv_time. Note 12905 * that it is also updated for tcp structure that belongs to 12906 * global and listener queues which do not really need updating. 12907 * But that should not cause any harm. And it is updated for 12908 * all kinds of incoming segments, not only for data segments. 12909 */ 12910 tcp->tcp_last_recv_time = lbolt; 12911 } 12912 12913 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12914 12915 BUMP_LOCAL(tcp->tcp_ibsegs); 12916 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 12917 12918 if ((flags & TH_URG) && sqp != NULL) { 12919 /* 12920 * TCP can't handle urgent pointers that arrive before 12921 * the connection has been accept()ed since it can't 12922 * buffer OOB data. Discard segment if this happens. 12923 * 12924 * Nor can it reassemble urgent pointers, so discard 12925 * if it's not the next segment expected. 12926 * 12927 * Otherwise, collapse chain into one mblk (discard if 12928 * that fails). This makes sure the headers, retransmitted 12929 * data, and new data all are in the same mblk. 12930 */ 12931 ASSERT(mp != NULL); 12932 if (tcp->tcp_listener || !pullupmsg(mp, -1)) { 12933 freemsg(mp); 12934 return; 12935 } 12936 /* Update pointers into message */ 12937 iphdr = rptr = mp->b_rptr; 12938 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12939 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12940 /* 12941 * Since we can't handle any data with this urgent 12942 * pointer that is out of sequence, we expunge 12943 * the data. This allows us to still register 12944 * the urgent mark and generate the M_PCSIG, 12945 * which we can do. 12946 */ 12947 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12948 seg_len = 0; 12949 } 12950 } 12951 12952 switch (tcp->tcp_state) { 12953 case TCPS_SYN_SENT: 12954 if (flags & TH_ACK) { 12955 /* 12956 * Note that our stack cannot send data before a 12957 * connection is established, therefore the 12958 * following check is valid. Otherwise, it has 12959 * to be changed. 12960 */ 12961 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12962 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12963 freemsg(mp); 12964 if (flags & TH_RST) 12965 return; 12966 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12967 tcp, seg_ack, 0, TH_RST); 12968 return; 12969 } 12970 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12971 } 12972 if (flags & TH_RST) { 12973 freemsg(mp); 12974 if (flags & TH_ACK) 12975 (void) tcp_clean_death(tcp, 12976 ECONNREFUSED, 13); 12977 return; 12978 } 12979 if (!(flags & TH_SYN)) { 12980 freemsg(mp); 12981 return; 12982 } 12983 12984 /* Process all TCP options. */ 12985 tcp_process_options(tcp, tcph); 12986 /* 12987 * The following changes our rwnd to be a multiple of the 12988 * MIN(peer MSS, our MSS) for performance reason. 12989 */ 12990 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 12991 tcp->tcp_mss)); 12992 12993 /* Is the other end ECN capable? */ 12994 if (tcp->tcp_ecn_ok) { 12995 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12996 tcp->tcp_ecn_ok = B_FALSE; 12997 } 12998 } 12999 /* 13000 * Clear ECN flags because it may interfere with later 13001 * processing. 13002 */ 13003 flags &= ~(TH_ECE|TH_CWR); 13004 13005 tcp->tcp_irs = seg_seq; 13006 tcp->tcp_rack = seg_seq; 13007 tcp->tcp_rnxt = seg_seq + 1; 13008 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13009 if (!TCP_IS_DETACHED(tcp)) { 13010 /* Allocate room for SACK options if needed. */ 13011 if (tcp->tcp_snd_sack_ok) { 13012 (void) mi_set_sth_wroff(tcp->tcp_rq, 13013 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13014 (tcp->tcp_loopback ? 0 : 13015 tcps->tcps_wroff_xtra)); 13016 } else { 13017 (void) mi_set_sth_wroff(tcp->tcp_rq, 13018 tcp->tcp_hdr_len + 13019 (tcp->tcp_loopback ? 0 : 13020 tcps->tcps_wroff_xtra)); 13021 } 13022 } 13023 if (flags & TH_ACK) { 13024 /* 13025 * If we can't get the confirmation upstream, pretend 13026 * we didn't even see this one. 13027 * 13028 * XXX: how can we pretend we didn't see it if we 13029 * have updated rnxt et. al. 13030 * 13031 * For loopback we defer sending up the T_CONN_CON 13032 * until after some checks below. 13033 */ 13034 mp1 = NULL; 13035 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13036 tcp->tcp_loopback ? &mp1 : NULL)) { 13037 freemsg(mp); 13038 return; 13039 } 13040 /* SYN was acked - making progress */ 13041 if (tcp->tcp_ipversion == IPV6_VERSION) 13042 tcp->tcp_ip_forward_progress = B_TRUE; 13043 13044 /* One for the SYN */ 13045 tcp->tcp_suna = tcp->tcp_iss + 1; 13046 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13047 tcp->tcp_state = TCPS_ESTABLISHED; 13048 13049 /* 13050 * If SYN was retransmitted, need to reset all 13051 * retransmission info. This is because this 13052 * segment will be treated as a dup ACK. 13053 */ 13054 if (tcp->tcp_rexmit) { 13055 tcp->tcp_rexmit = B_FALSE; 13056 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13057 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13058 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13059 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13060 tcp->tcp_ms_we_have_waited = 0; 13061 13062 /* 13063 * Set tcp_cwnd back to 1 MSS, per 13064 * recommendation from 13065 * draft-floyd-incr-init-win-01.txt, 13066 * Increasing TCP's Initial Window. 13067 */ 13068 tcp->tcp_cwnd = tcp->tcp_mss; 13069 } 13070 13071 tcp->tcp_swl1 = seg_seq; 13072 tcp->tcp_swl2 = seg_ack; 13073 13074 new_swnd = BE16_TO_U16(tcph->th_win); 13075 tcp->tcp_swnd = new_swnd; 13076 if (new_swnd > tcp->tcp_max_swnd) 13077 tcp->tcp_max_swnd = new_swnd; 13078 13079 /* 13080 * Always send the three-way handshake ack immediately 13081 * in order to make the connection complete as soon as 13082 * possible on the accepting host. 13083 */ 13084 flags |= TH_ACK_NEEDED; 13085 13086 /* 13087 * Special case for loopback. At this point we have 13088 * received SYN-ACK from the remote endpoint. In 13089 * order to ensure that both endpoints reach the 13090 * fused state prior to any data exchange, the final 13091 * ACK needs to be sent before we indicate T_CONN_CON 13092 * to the module upstream. 13093 */ 13094 if (tcp->tcp_loopback) { 13095 mblk_t *ack_mp; 13096 13097 ASSERT(!tcp->tcp_unfusable); 13098 ASSERT(mp1 != NULL); 13099 /* 13100 * For loopback, we always get a pure SYN-ACK 13101 * and only need to send back the final ACK 13102 * with no data (this is because the other 13103 * tcp is ours and we don't do T/TCP). This 13104 * final ACK triggers the passive side to 13105 * perform fusion in ESTABLISHED state. 13106 */ 13107 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13108 if (tcp->tcp_ack_tid != 0) { 13109 (void) TCP_TIMER_CANCEL(tcp, 13110 tcp->tcp_ack_tid); 13111 tcp->tcp_ack_tid = 0; 13112 } 13113 TCP_RECORD_TRACE(tcp, ack_mp, 13114 TCP_TRACE_SEND_PKT); 13115 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13116 BUMP_LOCAL(tcp->tcp_obsegs); 13117 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13118 13119 /* Send up T_CONN_CON */ 13120 putnext(tcp->tcp_rq, mp1); 13121 13122 freemsg(mp); 13123 return; 13124 } 13125 /* 13126 * Forget fusion; we need to handle more 13127 * complex cases below. Send the deferred 13128 * T_CONN_CON message upstream and proceed 13129 * as usual. Mark this tcp as not capable 13130 * of fusion. 13131 */ 13132 TCP_STAT(tcps, tcp_fusion_unfusable); 13133 tcp->tcp_unfusable = B_TRUE; 13134 putnext(tcp->tcp_rq, mp1); 13135 } 13136 13137 /* 13138 * Check to see if there is data to be sent. If 13139 * yes, set the transmit flag. Then check to see 13140 * if received data processing needs to be done. 13141 * If not, go straight to xmit_check. This short 13142 * cut is OK as we don't support T/TCP. 13143 */ 13144 if (tcp->tcp_unsent) 13145 flags |= TH_XMIT_NEEDED; 13146 13147 if (seg_len == 0 && !(flags & TH_URG)) { 13148 freemsg(mp); 13149 goto xmit_check; 13150 } 13151 13152 flags &= ~TH_SYN; 13153 seg_seq++; 13154 break; 13155 } 13156 tcp->tcp_state = TCPS_SYN_RCVD; 13157 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13158 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13159 if (mp1) { 13160 DB_CPID(mp1) = tcp->tcp_cpid; 13161 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 13162 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13163 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13164 } 13165 freemsg(mp); 13166 return; 13167 case TCPS_SYN_RCVD: 13168 if (flags & TH_ACK) { 13169 /* 13170 * In this state, a SYN|ACK packet is either bogus 13171 * because the other side must be ACKing our SYN which 13172 * indicates it has seen the ACK for their SYN and 13173 * shouldn't retransmit it or we're crossing SYNs 13174 * on active open. 13175 */ 13176 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13177 freemsg(mp); 13178 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13179 tcp, seg_ack, 0, TH_RST); 13180 return; 13181 } 13182 /* 13183 * NOTE: RFC 793 pg. 72 says this should be 13184 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13185 * but that would mean we have an ack that ignored 13186 * our SYN. 13187 */ 13188 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13189 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13190 freemsg(mp); 13191 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13192 tcp, seg_ack, 0, TH_RST); 13193 return; 13194 } 13195 } 13196 break; 13197 case TCPS_LISTEN: 13198 /* 13199 * Only a TLI listener can come through this path when a 13200 * acceptor is going back to be a listener and a packet 13201 * for the acceptor hits the classifier. For a socket 13202 * listener, this can never happen because a listener 13203 * can never accept connection on itself and hence a 13204 * socket acceptor can not go back to being a listener. 13205 */ 13206 ASSERT(!TCP_IS_SOCKET(tcp)); 13207 /*FALLTHRU*/ 13208 case TCPS_CLOSED: 13209 case TCPS_BOUND: { 13210 conn_t *new_connp; 13211 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13212 13213 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13214 if (new_connp != NULL) { 13215 tcp_reinput(new_connp, mp, connp->conn_sqp); 13216 return; 13217 } 13218 /* We failed to classify. For now just drop the packet */ 13219 freemsg(mp); 13220 return; 13221 } 13222 case TCPS_IDLE: 13223 /* 13224 * Handle the case where the tcp_clean_death() has happened 13225 * on a connection (application hasn't closed yet) but a packet 13226 * was already queued on squeue before tcp_clean_death() 13227 * was processed. Calling tcp_clean_death() twice on same 13228 * connection can result in weird behaviour. 13229 */ 13230 freemsg(mp); 13231 return; 13232 default: 13233 break; 13234 } 13235 13236 /* 13237 * Already on the correct queue/perimeter. 13238 * If this is a detached connection and not an eager 13239 * connection hanging off a listener then new data 13240 * (past the FIN) will cause a reset. 13241 * We do a special check here where it 13242 * is out of the main line, rather than check 13243 * if we are detached every time we see new 13244 * data down below. 13245 */ 13246 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13247 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13248 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13249 TCP_RECORD_TRACE(tcp, 13250 mp, TCP_TRACE_RECV_PKT); 13251 13252 freemsg(mp); 13253 /* 13254 * This could be an SSL closure alert. We're detached so just 13255 * acknowledge it this last time. 13256 */ 13257 if (tcp->tcp_kssl_ctx != NULL) { 13258 kssl_release_ctx(tcp->tcp_kssl_ctx); 13259 tcp->tcp_kssl_ctx = NULL; 13260 13261 tcp->tcp_rnxt += seg_len; 13262 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13263 flags |= TH_ACK_NEEDED; 13264 goto ack_check; 13265 } 13266 13267 tcp_xmit_ctl("new data when detached", tcp, 13268 tcp->tcp_snxt, 0, TH_RST); 13269 (void) tcp_clean_death(tcp, EPROTO, 12); 13270 return; 13271 } 13272 13273 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13274 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13275 new_swnd = BE16_TO_U16(tcph->th_win) << 13276 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13277 mss = tcp->tcp_mss; 13278 13279 if (tcp->tcp_snd_ts_ok) { 13280 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13281 /* 13282 * This segment is not acceptable. 13283 * Drop it and send back an ACK. 13284 */ 13285 freemsg(mp); 13286 flags |= TH_ACK_NEEDED; 13287 goto ack_check; 13288 } 13289 } else if (tcp->tcp_snd_sack_ok) { 13290 ASSERT(tcp->tcp_sack_info != NULL); 13291 tcpopt.tcp = tcp; 13292 /* 13293 * SACK info in already updated in tcp_parse_options. Ignore 13294 * all other TCP options... 13295 */ 13296 (void) tcp_parse_options(tcph, &tcpopt); 13297 } 13298 try_again:; 13299 gap = seg_seq - tcp->tcp_rnxt; 13300 rgap = tcp->tcp_rwnd - (gap + seg_len); 13301 /* 13302 * gap is the amount of sequence space between what we expect to see 13303 * and what we got for seg_seq. A positive value for gap means 13304 * something got lost. A negative value means we got some old stuff. 13305 */ 13306 if (gap < 0) { 13307 /* Old stuff present. Is the SYN in there? */ 13308 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13309 (seg_len != 0)) { 13310 flags &= ~TH_SYN; 13311 seg_seq++; 13312 urp--; 13313 /* Recompute the gaps after noting the SYN. */ 13314 goto try_again; 13315 } 13316 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13317 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13318 (seg_len > -gap ? -gap : seg_len)); 13319 /* Remove the old stuff from seg_len. */ 13320 seg_len += gap; 13321 /* 13322 * Anything left? 13323 * Make sure to check for unack'd FIN when rest of data 13324 * has been previously ack'd. 13325 */ 13326 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13327 /* 13328 * Resets are only valid if they lie within our offered 13329 * window. If the RST bit is set, we just ignore this 13330 * segment. 13331 */ 13332 if (flags & TH_RST) { 13333 freemsg(mp); 13334 return; 13335 } 13336 13337 /* 13338 * The arriving of dup data packets indicate that we 13339 * may have postponed an ack for too long, or the other 13340 * side's RTT estimate is out of shape. Start acking 13341 * more often. 13342 */ 13343 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13344 tcp->tcp_rack_cnt >= 1 && 13345 tcp->tcp_rack_abs_max > 2) { 13346 tcp->tcp_rack_abs_max--; 13347 } 13348 tcp->tcp_rack_cur_max = 1; 13349 13350 /* 13351 * This segment is "unacceptable". None of its 13352 * sequence space lies within our advertized window. 13353 * 13354 * Adjust seg_len to the original value for tracing. 13355 */ 13356 seg_len -= gap; 13357 if (tcp->tcp_debug) { 13358 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13359 "tcp_rput: unacceptable, gap %d, rgap %d, " 13360 "flags 0x%x, seg_seq %u, seg_ack %u, " 13361 "seg_len %d, rnxt %u, snxt %u, %s", 13362 gap, rgap, flags, seg_seq, seg_ack, 13363 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13364 tcp_display(tcp, NULL, 13365 DISP_ADDR_AND_PORT)); 13366 } 13367 13368 /* 13369 * Arrange to send an ACK in response to the 13370 * unacceptable segment per RFC 793 page 69. There 13371 * is only one small difference between ours and the 13372 * acceptability test in the RFC - we accept ACK-only 13373 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13374 * will be generated. 13375 * 13376 * Note that we have to ACK an ACK-only packet at least 13377 * for stacks that send 0-length keep-alives with 13378 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13379 * section 4.2.3.6. As long as we don't ever generate 13380 * an unacceptable packet in response to an incoming 13381 * packet that is unacceptable, it should not cause 13382 * "ACK wars". 13383 */ 13384 flags |= TH_ACK_NEEDED; 13385 13386 /* 13387 * Continue processing this segment in order to use the 13388 * ACK information it contains, but skip all other 13389 * sequence-number processing. Processing the ACK 13390 * information is necessary in order to 13391 * re-synchronize connections that may have lost 13392 * synchronization. 13393 * 13394 * We clear seg_len and flag fields related to 13395 * sequence number processing as they are not 13396 * to be trusted for an unacceptable segment. 13397 */ 13398 seg_len = 0; 13399 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13400 goto process_ack; 13401 } 13402 13403 /* Fix seg_seq, and chew the gap off the front. */ 13404 seg_seq = tcp->tcp_rnxt; 13405 urp += gap; 13406 do { 13407 mblk_t *mp2; 13408 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13409 (uintptr_t)UINT_MAX); 13410 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13411 if (gap > 0) { 13412 mp->b_rptr = mp->b_wptr - gap; 13413 break; 13414 } 13415 mp2 = mp; 13416 mp = mp->b_cont; 13417 freeb(mp2); 13418 } while (gap < 0); 13419 /* 13420 * If the urgent data has already been acknowledged, we 13421 * should ignore TH_URG below 13422 */ 13423 if (urp < 0) 13424 flags &= ~TH_URG; 13425 } 13426 /* 13427 * rgap is the amount of stuff received out of window. A negative 13428 * value is the amount out of window. 13429 */ 13430 if (rgap < 0) { 13431 mblk_t *mp2; 13432 13433 if (tcp->tcp_rwnd == 0) { 13434 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13435 } else { 13436 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13437 UPDATE_MIB(&tcps->tcps_mib, 13438 tcpInDataPastWinBytes, -rgap); 13439 } 13440 13441 /* 13442 * seg_len does not include the FIN, so if more than 13443 * just the FIN is out of window, we act like we don't 13444 * see it. (If just the FIN is out of window, rgap 13445 * will be zero and we will go ahead and acknowledge 13446 * the FIN.) 13447 */ 13448 flags &= ~TH_FIN; 13449 13450 /* Fix seg_len and make sure there is something left. */ 13451 seg_len += rgap; 13452 if (seg_len <= 0) { 13453 /* 13454 * Resets are only valid if they lie within our offered 13455 * window. If the RST bit is set, we just ignore this 13456 * segment. 13457 */ 13458 if (flags & TH_RST) { 13459 freemsg(mp); 13460 return; 13461 } 13462 13463 /* Per RFC 793, we need to send back an ACK. */ 13464 flags |= TH_ACK_NEEDED; 13465 13466 /* 13467 * Send SIGURG as soon as possible i.e. even 13468 * if the TH_URG was delivered in a window probe 13469 * packet (which will be unacceptable). 13470 * 13471 * We generate a signal if none has been generated 13472 * for this connection or if this is a new urgent 13473 * byte. Also send a zero-length "unmarked" message 13474 * to inform SIOCATMARK that this is not the mark. 13475 * 13476 * tcp_urp_last_valid is cleared when the T_exdata_ind 13477 * is sent up. This plus the check for old data 13478 * (gap >= 0) handles the wraparound of the sequence 13479 * number space without having to always track the 13480 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13481 * this max in its rcv_up variable). 13482 * 13483 * This prevents duplicate SIGURGS due to a "late" 13484 * zero-window probe when the T_EXDATA_IND has already 13485 * been sent up. 13486 */ 13487 if ((flags & TH_URG) && 13488 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13489 tcp->tcp_urp_last))) { 13490 mp1 = allocb(0, BPRI_MED); 13491 if (mp1 == NULL) { 13492 freemsg(mp); 13493 return; 13494 } 13495 if (!TCP_IS_DETACHED(tcp) && 13496 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13497 SIGURG)) { 13498 /* Try again on the rexmit. */ 13499 freemsg(mp1); 13500 freemsg(mp); 13501 return; 13502 } 13503 /* 13504 * If the next byte would be the mark 13505 * then mark with MARKNEXT else mark 13506 * with NOTMARKNEXT. 13507 */ 13508 if (gap == 0 && urp == 0) 13509 mp1->b_flag |= MSGMARKNEXT; 13510 else 13511 mp1->b_flag |= MSGNOTMARKNEXT; 13512 freemsg(tcp->tcp_urp_mark_mp); 13513 tcp->tcp_urp_mark_mp = mp1; 13514 flags |= TH_SEND_URP_MARK; 13515 tcp->tcp_urp_last_valid = B_TRUE; 13516 tcp->tcp_urp_last = urp + seg_seq; 13517 } 13518 /* 13519 * If this is a zero window probe, continue to 13520 * process the ACK part. But we need to set seg_len 13521 * to 0 to avoid data processing. Otherwise just 13522 * drop the segment and send back an ACK. 13523 */ 13524 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13525 flags &= ~(TH_SYN | TH_URG); 13526 seg_len = 0; 13527 goto process_ack; 13528 } else { 13529 freemsg(mp); 13530 goto ack_check; 13531 } 13532 } 13533 /* Pitch out of window stuff off the end. */ 13534 rgap = seg_len; 13535 mp2 = mp; 13536 do { 13537 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13538 (uintptr_t)INT_MAX); 13539 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13540 if (rgap < 0) { 13541 mp2->b_wptr += rgap; 13542 if ((mp1 = mp2->b_cont) != NULL) { 13543 mp2->b_cont = NULL; 13544 freemsg(mp1); 13545 } 13546 break; 13547 } 13548 } while ((mp2 = mp2->b_cont) != NULL); 13549 } 13550 ok:; 13551 /* 13552 * TCP should check ECN info for segments inside the window only. 13553 * Therefore the check should be done here. 13554 */ 13555 if (tcp->tcp_ecn_ok) { 13556 if (flags & TH_CWR) { 13557 tcp->tcp_ecn_echo_on = B_FALSE; 13558 } 13559 /* 13560 * Note that both ECN_CE and CWR can be set in the 13561 * same segment. In this case, we once again turn 13562 * on ECN_ECHO. 13563 */ 13564 if (tcp->tcp_ipversion == IPV4_VERSION) { 13565 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13566 13567 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13568 tcp->tcp_ecn_echo_on = B_TRUE; 13569 } 13570 } else { 13571 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13572 13573 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13574 htonl(IPH_ECN_CE << 20)) { 13575 tcp->tcp_ecn_echo_on = B_TRUE; 13576 } 13577 } 13578 } 13579 13580 /* 13581 * Check whether we can update tcp_ts_recent. This test is 13582 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13583 * Extensions for High Performance: An Update", Internet Draft. 13584 */ 13585 if (tcp->tcp_snd_ts_ok && 13586 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13587 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13588 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13589 tcp->tcp_last_rcv_lbolt = lbolt64; 13590 } 13591 13592 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13593 /* 13594 * FIN in an out of order segment. We record this in 13595 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13596 * Clear the FIN so that any check on FIN flag will fail. 13597 * Remember that FIN also counts in the sequence number 13598 * space. So we need to ack out of order FIN only segments. 13599 */ 13600 if (flags & TH_FIN) { 13601 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13602 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13603 flags &= ~TH_FIN; 13604 flags |= TH_ACK_NEEDED; 13605 } 13606 if (seg_len > 0) { 13607 /* Fill in the SACK blk list. */ 13608 if (tcp->tcp_snd_sack_ok) { 13609 ASSERT(tcp->tcp_sack_info != NULL); 13610 tcp_sack_insert(tcp->tcp_sack_list, 13611 seg_seq, seg_seq + seg_len, 13612 &(tcp->tcp_num_sack_blk)); 13613 } 13614 13615 /* 13616 * Attempt reassembly and see if we have something 13617 * ready to go. 13618 */ 13619 mp = tcp_reass(tcp, mp, seg_seq); 13620 /* Always ack out of order packets */ 13621 flags |= TH_ACK_NEEDED | TH_PUSH; 13622 if (mp) { 13623 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13624 (uintptr_t)INT_MAX); 13625 seg_len = mp->b_cont ? msgdsize(mp) : 13626 (int)(mp->b_wptr - mp->b_rptr); 13627 seg_seq = tcp->tcp_rnxt; 13628 /* 13629 * A gap is filled and the seq num and len 13630 * of the gap match that of a previously 13631 * received FIN, put the FIN flag back in. 13632 */ 13633 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13634 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13635 flags |= TH_FIN; 13636 tcp->tcp_valid_bits &= 13637 ~TCP_OFO_FIN_VALID; 13638 } 13639 } else { 13640 /* 13641 * Keep going even with NULL mp. 13642 * There may be a useful ACK or something else 13643 * we don't want to miss. 13644 * 13645 * But TCP should not perform fast retransmit 13646 * because of the ack number. TCP uses 13647 * seg_len == 0 to determine if it is a pure 13648 * ACK. And this is not a pure ACK. 13649 */ 13650 seg_len = 0; 13651 ofo_seg = B_TRUE; 13652 } 13653 } 13654 } else if (seg_len > 0) { 13655 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13656 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13657 /* 13658 * If an out of order FIN was received before, and the seq 13659 * num and len of the new segment match that of the FIN, 13660 * put the FIN flag back in. 13661 */ 13662 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13663 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13664 flags |= TH_FIN; 13665 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13666 } 13667 } 13668 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13669 if (flags & TH_RST) { 13670 freemsg(mp); 13671 switch (tcp->tcp_state) { 13672 case TCPS_SYN_RCVD: 13673 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13674 break; 13675 case TCPS_ESTABLISHED: 13676 case TCPS_FIN_WAIT_1: 13677 case TCPS_FIN_WAIT_2: 13678 case TCPS_CLOSE_WAIT: 13679 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13680 break; 13681 case TCPS_CLOSING: 13682 case TCPS_LAST_ACK: 13683 (void) tcp_clean_death(tcp, 0, 16); 13684 break; 13685 default: 13686 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13687 (void) tcp_clean_death(tcp, ENXIO, 17); 13688 break; 13689 } 13690 return; 13691 } 13692 if (flags & TH_SYN) { 13693 /* 13694 * See RFC 793, Page 71 13695 * 13696 * The seq number must be in the window as it should 13697 * be "fixed" above. If it is outside window, it should 13698 * be already rejected. Note that we allow seg_seq to be 13699 * rnxt + rwnd because we want to accept 0 window probe. 13700 */ 13701 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13702 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13703 freemsg(mp); 13704 /* 13705 * If the ACK flag is not set, just use our snxt as the 13706 * seq number of the RST segment. 13707 */ 13708 if (!(flags & TH_ACK)) { 13709 seg_ack = tcp->tcp_snxt; 13710 } 13711 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13712 TH_RST|TH_ACK); 13713 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13714 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13715 return; 13716 } 13717 /* 13718 * urp could be -1 when the urp field in the packet is 0 13719 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13720 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13721 */ 13722 if (flags & TH_URG && urp >= 0) { 13723 if (!tcp->tcp_urp_last_valid || 13724 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13725 /* 13726 * If we haven't generated the signal yet for this 13727 * urgent pointer value, do it now. Also, send up a 13728 * zero-length M_DATA indicating whether or not this is 13729 * the mark. The latter is not needed when a 13730 * T_EXDATA_IND is sent up. However, if there are 13731 * allocation failures this code relies on the sender 13732 * retransmitting and the socket code for determining 13733 * the mark should not block waiting for the peer to 13734 * transmit. Thus, for simplicity we always send up the 13735 * mark indication. 13736 */ 13737 mp1 = allocb(0, BPRI_MED); 13738 if (mp1 == NULL) { 13739 freemsg(mp); 13740 return; 13741 } 13742 if (!TCP_IS_DETACHED(tcp) && 13743 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13744 /* Try again on the rexmit. */ 13745 freemsg(mp1); 13746 freemsg(mp); 13747 return; 13748 } 13749 /* 13750 * Mark with NOTMARKNEXT for now. 13751 * The code below will change this to MARKNEXT 13752 * if we are at the mark. 13753 * 13754 * If there are allocation failures (e.g. in dupmsg 13755 * below) the next time tcp_rput_data sees the urgent 13756 * segment it will send up the MSG*MARKNEXT message. 13757 */ 13758 mp1->b_flag |= MSGNOTMARKNEXT; 13759 freemsg(tcp->tcp_urp_mark_mp); 13760 tcp->tcp_urp_mark_mp = mp1; 13761 flags |= TH_SEND_URP_MARK; 13762 #ifdef DEBUG 13763 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13764 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13765 "last %x, %s", 13766 seg_seq, urp, tcp->tcp_urp_last, 13767 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13768 #endif /* DEBUG */ 13769 tcp->tcp_urp_last_valid = B_TRUE; 13770 tcp->tcp_urp_last = urp + seg_seq; 13771 } else if (tcp->tcp_urp_mark_mp != NULL) { 13772 /* 13773 * An allocation failure prevented the previous 13774 * tcp_rput_data from sending up the allocated 13775 * MSG*MARKNEXT message - send it up this time 13776 * around. 13777 */ 13778 flags |= TH_SEND_URP_MARK; 13779 } 13780 13781 /* 13782 * If the urgent byte is in this segment, make sure that it is 13783 * all by itself. This makes it much easier to deal with the 13784 * possibility of an allocation failure on the T_exdata_ind. 13785 * Note that seg_len is the number of bytes in the segment, and 13786 * urp is the offset into the segment of the urgent byte. 13787 * urp < seg_len means that the urgent byte is in this segment. 13788 */ 13789 if (urp < seg_len) { 13790 if (seg_len != 1) { 13791 uint32_t tmp_rnxt; 13792 /* 13793 * Break it up and feed it back in. 13794 * Re-attach the IP header. 13795 */ 13796 mp->b_rptr = iphdr; 13797 if (urp > 0) { 13798 /* 13799 * There is stuff before the urgent 13800 * byte. 13801 */ 13802 mp1 = dupmsg(mp); 13803 if (!mp1) { 13804 /* 13805 * Trim from urgent byte on. 13806 * The rest will come back. 13807 */ 13808 (void) adjmsg(mp, 13809 urp - seg_len); 13810 tcp_rput_data(connp, 13811 mp, NULL); 13812 return; 13813 } 13814 (void) adjmsg(mp1, urp - seg_len); 13815 /* Feed this piece back in. */ 13816 tmp_rnxt = tcp->tcp_rnxt; 13817 tcp_rput_data(connp, mp1, NULL); 13818 /* 13819 * If the data passed back in was not 13820 * processed (ie: bad ACK) sending 13821 * the remainder back in will cause a 13822 * loop. In this case, drop the 13823 * packet and let the sender try 13824 * sending a good packet. 13825 */ 13826 if (tmp_rnxt == tcp->tcp_rnxt) { 13827 freemsg(mp); 13828 return; 13829 } 13830 } 13831 if (urp != seg_len - 1) { 13832 uint32_t tmp_rnxt; 13833 /* 13834 * There is stuff after the urgent 13835 * byte. 13836 */ 13837 mp1 = dupmsg(mp); 13838 if (!mp1) { 13839 /* 13840 * Trim everything beyond the 13841 * urgent byte. The rest will 13842 * come back. 13843 */ 13844 (void) adjmsg(mp, 13845 urp + 1 - seg_len); 13846 tcp_rput_data(connp, 13847 mp, NULL); 13848 return; 13849 } 13850 (void) adjmsg(mp1, urp + 1 - seg_len); 13851 tmp_rnxt = tcp->tcp_rnxt; 13852 tcp_rput_data(connp, mp1, NULL); 13853 /* 13854 * If the data passed back in was not 13855 * processed (ie: bad ACK) sending 13856 * the remainder back in will cause a 13857 * loop. In this case, drop the 13858 * packet and let the sender try 13859 * sending a good packet. 13860 */ 13861 if (tmp_rnxt == tcp->tcp_rnxt) { 13862 freemsg(mp); 13863 return; 13864 } 13865 } 13866 tcp_rput_data(connp, mp, NULL); 13867 return; 13868 } 13869 /* 13870 * This segment contains only the urgent byte. We 13871 * have to allocate the T_exdata_ind, if we can. 13872 */ 13873 if (!tcp->tcp_urp_mp) { 13874 struct T_exdata_ind *tei; 13875 mp1 = allocb(sizeof (struct T_exdata_ind), 13876 BPRI_MED); 13877 if (!mp1) { 13878 /* 13879 * Sigh... It'll be back. 13880 * Generate any MSG*MARK message now. 13881 */ 13882 freemsg(mp); 13883 seg_len = 0; 13884 if (flags & TH_SEND_URP_MARK) { 13885 13886 13887 ASSERT(tcp->tcp_urp_mark_mp); 13888 tcp->tcp_urp_mark_mp->b_flag &= 13889 ~MSGNOTMARKNEXT; 13890 tcp->tcp_urp_mark_mp->b_flag |= 13891 MSGMARKNEXT; 13892 } 13893 goto ack_check; 13894 } 13895 mp1->b_datap->db_type = M_PROTO; 13896 tei = (struct T_exdata_ind *)mp1->b_rptr; 13897 tei->PRIM_type = T_EXDATA_IND; 13898 tei->MORE_flag = 0; 13899 mp1->b_wptr = (uchar_t *)&tei[1]; 13900 tcp->tcp_urp_mp = mp1; 13901 #ifdef DEBUG 13902 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13903 "tcp_rput: allocated exdata_ind %s", 13904 tcp_display(tcp, NULL, 13905 DISP_PORT_ONLY)); 13906 #endif /* DEBUG */ 13907 /* 13908 * There is no need to send a separate MSG*MARK 13909 * message since the T_EXDATA_IND will be sent 13910 * now. 13911 */ 13912 flags &= ~TH_SEND_URP_MARK; 13913 freemsg(tcp->tcp_urp_mark_mp); 13914 tcp->tcp_urp_mark_mp = NULL; 13915 } 13916 /* 13917 * Now we are all set. On the next putnext upstream, 13918 * tcp_urp_mp will be non-NULL and will get prepended 13919 * to what has to be this piece containing the urgent 13920 * byte. If for any reason we abort this segment below, 13921 * if it comes back, we will have this ready, or it 13922 * will get blown off in close. 13923 */ 13924 } else if (urp == seg_len) { 13925 /* 13926 * The urgent byte is the next byte after this sequence 13927 * number. If there is data it is marked with 13928 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13929 * since it is not needed. Otherwise, if the code 13930 * above just allocated a zero-length tcp_urp_mark_mp 13931 * message, that message is tagged with MSGMARKNEXT. 13932 * Sending up these MSGMARKNEXT messages makes 13933 * SIOCATMARK work correctly even though 13934 * the T_EXDATA_IND will not be sent up until the 13935 * urgent byte arrives. 13936 */ 13937 if (seg_len != 0) { 13938 flags |= TH_MARKNEXT_NEEDED; 13939 freemsg(tcp->tcp_urp_mark_mp); 13940 tcp->tcp_urp_mark_mp = NULL; 13941 flags &= ~TH_SEND_URP_MARK; 13942 } else if (tcp->tcp_urp_mark_mp != NULL) { 13943 flags |= TH_SEND_URP_MARK; 13944 tcp->tcp_urp_mark_mp->b_flag &= 13945 ~MSGNOTMARKNEXT; 13946 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13947 } 13948 #ifdef DEBUG 13949 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13950 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13951 seg_len, flags, 13952 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13953 #endif /* DEBUG */ 13954 } else { 13955 /* Data left until we hit mark */ 13956 #ifdef DEBUG 13957 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13958 "tcp_rput: URP %d bytes left, %s", 13959 urp - seg_len, tcp_display(tcp, NULL, 13960 DISP_PORT_ONLY)); 13961 #endif /* DEBUG */ 13962 } 13963 } 13964 13965 process_ack: 13966 if (!(flags & TH_ACK)) { 13967 freemsg(mp); 13968 goto xmit_check; 13969 } 13970 } 13971 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13972 13973 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13974 tcp->tcp_ip_forward_progress = B_TRUE; 13975 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13976 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13977 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13978 /* 3-way handshake complete - pass up the T_CONN_IND */ 13979 tcp_t *listener = tcp->tcp_listener; 13980 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13981 13982 tcp->tcp_tconnind_started = B_TRUE; 13983 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13984 /* 13985 * We are here means eager is fine but it can 13986 * get a TH_RST at any point between now and till 13987 * accept completes and disappear. We need to 13988 * ensure that reference to eager is valid after 13989 * we get out of eager's perimeter. So we do 13990 * an extra refhold. 13991 */ 13992 CONN_INC_REF(connp); 13993 13994 /* 13995 * The listener also exists because of the refhold 13996 * done in tcp_conn_request. Its possible that it 13997 * might have closed. We will check that once we 13998 * get inside listeners context. 13999 */ 14000 CONN_INC_REF(listener->tcp_connp); 14001 if (listener->tcp_connp->conn_sqp == 14002 connp->conn_sqp) { 14003 tcp_send_conn_ind(listener->tcp_connp, mp, 14004 listener->tcp_connp->conn_sqp); 14005 CONN_DEC_REF(listener->tcp_connp); 14006 } else if (!tcp->tcp_loopback) { 14007 squeue_fill(listener->tcp_connp->conn_sqp, mp, 14008 tcp_send_conn_ind, 14009 listener->tcp_connp, SQTAG_TCP_CONN_IND); 14010 } else { 14011 squeue_enter(listener->tcp_connp->conn_sqp, mp, 14012 tcp_send_conn_ind, listener->tcp_connp, 14013 SQTAG_TCP_CONN_IND); 14014 } 14015 } 14016 14017 if (tcp->tcp_active_open) { 14018 /* 14019 * We are seeing the final ack in the three way 14020 * hand shake of a active open'ed connection 14021 * so we must send up a T_CONN_CON 14022 */ 14023 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14024 freemsg(mp); 14025 return; 14026 } 14027 /* 14028 * Don't fuse the loopback endpoints for 14029 * simultaneous active opens. 14030 */ 14031 if (tcp->tcp_loopback) { 14032 TCP_STAT(tcps, tcp_fusion_unfusable); 14033 tcp->tcp_unfusable = B_TRUE; 14034 } 14035 } 14036 14037 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14038 bytes_acked--; 14039 /* SYN was acked - making progress */ 14040 if (tcp->tcp_ipversion == IPV6_VERSION) 14041 tcp->tcp_ip_forward_progress = B_TRUE; 14042 14043 /* 14044 * If SYN was retransmitted, need to reset all 14045 * retransmission info as this segment will be 14046 * treated as a dup ACK. 14047 */ 14048 if (tcp->tcp_rexmit) { 14049 tcp->tcp_rexmit = B_FALSE; 14050 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14051 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14052 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14053 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14054 tcp->tcp_ms_we_have_waited = 0; 14055 tcp->tcp_cwnd = mss; 14056 } 14057 14058 /* 14059 * We set the send window to zero here. 14060 * This is needed if there is data to be 14061 * processed already on the queue. 14062 * Later (at swnd_update label), the 14063 * "new_swnd > tcp_swnd" condition is satisfied 14064 * the XMIT_NEEDED flag is set in the current 14065 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14066 * called if there is already data on queue in 14067 * this state. 14068 */ 14069 tcp->tcp_swnd = 0; 14070 14071 if (new_swnd > tcp->tcp_max_swnd) 14072 tcp->tcp_max_swnd = new_swnd; 14073 tcp->tcp_swl1 = seg_seq; 14074 tcp->tcp_swl2 = seg_ack; 14075 tcp->tcp_state = TCPS_ESTABLISHED; 14076 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14077 14078 /* Fuse when both sides are in ESTABLISHED state */ 14079 if (tcp->tcp_loopback && do_tcp_fusion) 14080 tcp_fuse(tcp, iphdr, tcph); 14081 14082 } 14083 /* This code follows 4.4BSD-Lite2 mostly. */ 14084 if (bytes_acked < 0) 14085 goto est; 14086 14087 /* 14088 * If TCP is ECN capable and the congestion experience bit is 14089 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14090 * done once per window (or more loosely, per RTT). 14091 */ 14092 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14093 tcp->tcp_cwr = B_FALSE; 14094 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14095 if (!tcp->tcp_cwr) { 14096 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14097 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14098 tcp->tcp_cwnd = npkt * mss; 14099 /* 14100 * If the cwnd is 0, use the timer to clock out 14101 * new segments. This is required by the ECN spec. 14102 */ 14103 if (npkt == 0) { 14104 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14105 /* 14106 * This makes sure that when the ACK comes 14107 * back, we will increase tcp_cwnd by 1 MSS. 14108 */ 14109 tcp->tcp_cwnd_cnt = 0; 14110 } 14111 tcp->tcp_cwr = B_TRUE; 14112 /* 14113 * This marks the end of the current window of in 14114 * flight data. That is why we don't use 14115 * tcp_suna + tcp_swnd. Only data in flight can 14116 * provide ECN info. 14117 */ 14118 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14119 tcp->tcp_ecn_cwr_sent = B_FALSE; 14120 } 14121 } 14122 14123 mp1 = tcp->tcp_xmit_head; 14124 if (bytes_acked == 0) { 14125 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14126 int dupack_cnt; 14127 14128 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14129 /* 14130 * Fast retransmit. When we have seen exactly three 14131 * identical ACKs while we have unacked data 14132 * outstanding we take it as a hint that our peer 14133 * dropped something. 14134 * 14135 * If TCP is retransmitting, don't do fast retransmit. 14136 */ 14137 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14138 ! tcp->tcp_rexmit) { 14139 /* Do Limited Transmit */ 14140 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14141 tcps->tcps_dupack_fast_retransmit) { 14142 /* 14143 * RFC 3042 14144 * 14145 * What we need to do is temporarily 14146 * increase tcp_cwnd so that new 14147 * data can be sent if it is allowed 14148 * by the receive window (tcp_rwnd). 14149 * tcp_wput_data() will take care of 14150 * the rest. 14151 * 14152 * If the connection is SACK capable, 14153 * only do limited xmit when there 14154 * is SACK info. 14155 * 14156 * Note how tcp_cwnd is incremented. 14157 * The first dup ACK will increase 14158 * it by 1 MSS. The second dup ACK 14159 * will increase it by 2 MSS. This 14160 * means that only 1 new segment will 14161 * be sent for each dup ACK. 14162 */ 14163 if (tcp->tcp_unsent > 0 && 14164 (!tcp->tcp_snd_sack_ok || 14165 (tcp->tcp_snd_sack_ok && 14166 tcp->tcp_notsack_list != NULL))) { 14167 tcp->tcp_cwnd += mss << 14168 (tcp->tcp_dupack_cnt - 1); 14169 flags |= TH_LIMIT_XMIT; 14170 } 14171 } else if (dupack_cnt == 14172 tcps->tcps_dupack_fast_retransmit) { 14173 14174 /* 14175 * If we have reduced tcp_ssthresh 14176 * because of ECN, do not reduce it again 14177 * unless it is already one window of data 14178 * away. After one window of data, tcp_cwr 14179 * should then be cleared. Note that 14180 * for non ECN capable connection, tcp_cwr 14181 * should always be false. 14182 * 14183 * Adjust cwnd since the duplicate 14184 * ack indicates that a packet was 14185 * dropped (due to congestion.) 14186 */ 14187 if (!tcp->tcp_cwr) { 14188 npkt = ((tcp->tcp_snxt - 14189 tcp->tcp_suna) >> 1) / mss; 14190 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14191 mss; 14192 tcp->tcp_cwnd = (npkt + 14193 tcp->tcp_dupack_cnt) * mss; 14194 } 14195 if (tcp->tcp_ecn_ok) { 14196 tcp->tcp_cwr = B_TRUE; 14197 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14198 tcp->tcp_ecn_cwr_sent = B_FALSE; 14199 } 14200 14201 /* 14202 * We do Hoe's algorithm. Refer to her 14203 * paper "Improving the Start-up Behavior 14204 * of a Congestion Control Scheme for TCP," 14205 * appeared in SIGCOMM'96. 14206 * 14207 * Save highest seq no we have sent so far. 14208 * Be careful about the invisible FIN byte. 14209 */ 14210 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14211 (tcp->tcp_unsent == 0)) { 14212 tcp->tcp_rexmit_max = tcp->tcp_fss; 14213 } else { 14214 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14215 } 14216 14217 /* 14218 * Do not allow bursty traffic during. 14219 * fast recovery. Refer to Fall and Floyd's 14220 * paper "Simulation-based Comparisons of 14221 * Tahoe, Reno and SACK TCP" (in CCR?) 14222 * This is a best current practise. 14223 */ 14224 tcp->tcp_snd_burst = TCP_CWND_SS; 14225 14226 /* 14227 * For SACK: 14228 * Calculate tcp_pipe, which is the 14229 * estimated number of bytes in 14230 * network. 14231 * 14232 * tcp_fack is the highest sack'ed seq num 14233 * TCP has received. 14234 * 14235 * tcp_pipe is explained in the above quoted 14236 * Fall and Floyd's paper. tcp_fack is 14237 * explained in Mathis and Mahdavi's 14238 * "Forward Acknowledgment: Refining TCP 14239 * Congestion Control" in SIGCOMM '96. 14240 */ 14241 if (tcp->tcp_snd_sack_ok) { 14242 ASSERT(tcp->tcp_sack_info != NULL); 14243 if (tcp->tcp_notsack_list != NULL) { 14244 tcp->tcp_pipe = tcp->tcp_snxt - 14245 tcp->tcp_fack; 14246 tcp->tcp_sack_snxt = seg_ack; 14247 flags |= TH_NEED_SACK_REXMIT; 14248 } else { 14249 /* 14250 * Always initialize tcp_pipe 14251 * even though we don't have 14252 * any SACK info. If later 14253 * we get SACK info and 14254 * tcp_pipe is not initialized, 14255 * funny things will happen. 14256 */ 14257 tcp->tcp_pipe = 14258 tcp->tcp_cwnd_ssthresh; 14259 } 14260 } else { 14261 flags |= TH_REXMIT_NEEDED; 14262 } /* tcp_snd_sack_ok */ 14263 14264 } else { 14265 /* 14266 * Here we perform congestion 14267 * avoidance, but NOT slow start. 14268 * This is known as the Fast 14269 * Recovery Algorithm. 14270 */ 14271 if (tcp->tcp_snd_sack_ok && 14272 tcp->tcp_notsack_list != NULL) { 14273 flags |= TH_NEED_SACK_REXMIT; 14274 tcp->tcp_pipe -= mss; 14275 if (tcp->tcp_pipe < 0) 14276 tcp->tcp_pipe = 0; 14277 } else { 14278 /* 14279 * We know that one more packet has 14280 * left the pipe thus we can update 14281 * cwnd. 14282 */ 14283 cwnd = tcp->tcp_cwnd + mss; 14284 if (cwnd > tcp->tcp_cwnd_max) 14285 cwnd = tcp->tcp_cwnd_max; 14286 tcp->tcp_cwnd = cwnd; 14287 if (tcp->tcp_unsent > 0) 14288 flags |= TH_XMIT_NEEDED; 14289 } 14290 } 14291 } 14292 } else if (tcp->tcp_zero_win_probe) { 14293 /* 14294 * If the window has opened, need to arrange 14295 * to send additional data. 14296 */ 14297 if (new_swnd != 0) { 14298 /* tcp_suna != tcp_snxt */ 14299 /* Packet contains a window update */ 14300 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14301 tcp->tcp_zero_win_probe = 0; 14302 tcp->tcp_timer_backoff = 0; 14303 tcp->tcp_ms_we_have_waited = 0; 14304 14305 /* 14306 * Transmit starting with tcp_suna since 14307 * the one byte probe is not ack'ed. 14308 * If TCP has sent more than one identical 14309 * probe, tcp_rexmit will be set. That means 14310 * tcp_ss_rexmit() will send out the one 14311 * byte along with new data. Otherwise, 14312 * fake the retransmission. 14313 */ 14314 flags |= TH_XMIT_NEEDED; 14315 if (!tcp->tcp_rexmit) { 14316 tcp->tcp_rexmit = B_TRUE; 14317 tcp->tcp_dupack_cnt = 0; 14318 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14319 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14320 } 14321 } 14322 } 14323 goto swnd_update; 14324 } 14325 14326 /* 14327 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14328 * If the ACK value acks something that we have not yet sent, it might 14329 * be an old duplicate segment. Send an ACK to re-synchronize the 14330 * other side. 14331 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14332 * state is handled above, so we can always just drop the segment and 14333 * send an ACK here. 14334 * 14335 * Should we send ACKs in response to ACK only segments? 14336 */ 14337 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14338 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14339 /* drop the received segment */ 14340 freemsg(mp); 14341 14342 /* 14343 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14344 * greater than 0, check if the number of such 14345 * bogus ACks is greater than that count. If yes, 14346 * don't send back any ACK. This prevents TCP from 14347 * getting into an ACK storm if somehow an attacker 14348 * successfully spoofs an acceptable segment to our 14349 * peer. 14350 */ 14351 if (tcp_drop_ack_unsent_cnt > 0 && 14352 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14353 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14354 return; 14355 } 14356 mp = tcp_ack_mp(tcp); 14357 if (mp != NULL) { 14358 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 14359 BUMP_LOCAL(tcp->tcp_obsegs); 14360 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14361 tcp_send_data(tcp, tcp->tcp_wq, mp); 14362 } 14363 return; 14364 } 14365 14366 /* 14367 * TCP gets a new ACK, update the notsack'ed list to delete those 14368 * blocks that are covered by this ACK. 14369 */ 14370 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14371 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14372 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14373 } 14374 14375 /* 14376 * If we got an ACK after fast retransmit, check to see 14377 * if it is a partial ACK. If it is not and the congestion 14378 * window was inflated to account for the other side's 14379 * cached packets, retract it. If it is, do Hoe's algorithm. 14380 */ 14381 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14382 ASSERT(tcp->tcp_rexmit == B_FALSE); 14383 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14384 tcp->tcp_dupack_cnt = 0; 14385 /* 14386 * Restore the orig tcp_cwnd_ssthresh after 14387 * fast retransmit phase. 14388 */ 14389 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14390 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14391 } 14392 tcp->tcp_rexmit_max = seg_ack; 14393 tcp->tcp_cwnd_cnt = 0; 14394 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14395 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14396 14397 /* 14398 * Remove all notsack info to avoid confusion with 14399 * the next fast retrasnmit/recovery phase. 14400 */ 14401 if (tcp->tcp_snd_sack_ok && 14402 tcp->tcp_notsack_list != NULL) { 14403 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14404 } 14405 } else { 14406 if (tcp->tcp_snd_sack_ok && 14407 tcp->tcp_notsack_list != NULL) { 14408 flags |= TH_NEED_SACK_REXMIT; 14409 tcp->tcp_pipe -= mss; 14410 if (tcp->tcp_pipe < 0) 14411 tcp->tcp_pipe = 0; 14412 } else { 14413 /* 14414 * Hoe's algorithm: 14415 * 14416 * Retransmit the unack'ed segment and 14417 * restart fast recovery. Note that we 14418 * need to scale back tcp_cwnd to the 14419 * original value when we started fast 14420 * recovery. This is to prevent overly 14421 * aggressive behaviour in sending new 14422 * segments. 14423 */ 14424 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14425 tcps->tcps_dupack_fast_retransmit * mss; 14426 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14427 flags |= TH_REXMIT_NEEDED; 14428 } 14429 } 14430 } else { 14431 tcp->tcp_dupack_cnt = 0; 14432 if (tcp->tcp_rexmit) { 14433 /* 14434 * TCP is retranmitting. If the ACK ack's all 14435 * outstanding data, update tcp_rexmit_max and 14436 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14437 * to the correct value. 14438 * 14439 * Note that SEQ_LEQ() is used. This is to avoid 14440 * unnecessary fast retransmit caused by dup ACKs 14441 * received when TCP does slow start retransmission 14442 * after a time out. During this phase, TCP may 14443 * send out segments which are already received. 14444 * This causes dup ACKs to be sent back. 14445 */ 14446 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14447 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14448 tcp->tcp_rexmit_nxt = seg_ack; 14449 } 14450 if (seg_ack != tcp->tcp_rexmit_max) { 14451 flags |= TH_XMIT_NEEDED; 14452 } 14453 } else { 14454 tcp->tcp_rexmit = B_FALSE; 14455 tcp->tcp_xmit_zc_clean = B_FALSE; 14456 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14457 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14458 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14459 } 14460 tcp->tcp_ms_we_have_waited = 0; 14461 } 14462 } 14463 14464 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14465 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14466 tcp->tcp_suna = seg_ack; 14467 if (tcp->tcp_zero_win_probe != 0) { 14468 tcp->tcp_zero_win_probe = 0; 14469 tcp->tcp_timer_backoff = 0; 14470 } 14471 14472 /* 14473 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14474 * Note that it cannot be the SYN being ack'ed. The code flow 14475 * will not reach here. 14476 */ 14477 if (mp1 == NULL) { 14478 goto fin_acked; 14479 } 14480 14481 /* 14482 * Update the congestion window. 14483 * 14484 * If TCP is not ECN capable or TCP is ECN capable but the 14485 * congestion experience bit is not set, increase the tcp_cwnd as 14486 * usual. 14487 */ 14488 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14489 cwnd = tcp->tcp_cwnd; 14490 add = mss; 14491 14492 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14493 /* 14494 * This is to prevent an increase of less than 1 MSS of 14495 * tcp_cwnd. With partial increase, tcp_wput_data() 14496 * may send out tinygrams in order to preserve mblk 14497 * boundaries. 14498 * 14499 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14500 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14501 * increased by 1 MSS for every RTTs. 14502 */ 14503 if (tcp->tcp_cwnd_cnt <= 0) { 14504 tcp->tcp_cwnd_cnt = cwnd + add; 14505 } else { 14506 tcp->tcp_cwnd_cnt -= add; 14507 add = 0; 14508 } 14509 } 14510 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14511 } 14512 14513 /* See if the latest urgent data has been acknowledged */ 14514 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14515 SEQ_GT(seg_ack, tcp->tcp_urg)) 14516 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14517 14518 /* Can we update the RTT estimates? */ 14519 if (tcp->tcp_snd_ts_ok) { 14520 /* Ignore zero timestamp echo-reply. */ 14521 if (tcpopt.tcp_opt_ts_ecr != 0) { 14522 tcp_set_rto(tcp, (int32_t)lbolt - 14523 (int32_t)tcpopt.tcp_opt_ts_ecr); 14524 } 14525 14526 /* If needed, restart the timer. */ 14527 if (tcp->tcp_set_timer == 1) { 14528 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14529 tcp->tcp_set_timer = 0; 14530 } 14531 /* 14532 * Update tcp_csuna in case the other side stops sending 14533 * us timestamps. 14534 */ 14535 tcp->tcp_csuna = tcp->tcp_snxt; 14536 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14537 /* 14538 * An ACK sequence we haven't seen before, so get the RTT 14539 * and update the RTO. But first check if the timestamp is 14540 * valid to use. 14541 */ 14542 if ((mp1->b_next != NULL) && 14543 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14544 tcp_set_rto(tcp, (int32_t)lbolt - 14545 (int32_t)(intptr_t)mp1->b_prev); 14546 else 14547 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14548 14549 /* Remeber the last sequence to be ACKed */ 14550 tcp->tcp_csuna = seg_ack; 14551 if (tcp->tcp_set_timer == 1) { 14552 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14553 tcp->tcp_set_timer = 0; 14554 } 14555 } else { 14556 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14557 } 14558 14559 /* Eat acknowledged bytes off the xmit queue. */ 14560 for (;;) { 14561 mblk_t *mp2; 14562 uchar_t *wptr; 14563 14564 wptr = mp1->b_wptr; 14565 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14566 bytes_acked -= (int)(wptr - mp1->b_rptr); 14567 if (bytes_acked < 0) { 14568 mp1->b_rptr = wptr + bytes_acked; 14569 /* 14570 * Set a new timestamp if all the bytes timed by the 14571 * old timestamp have been ack'ed. 14572 */ 14573 if (SEQ_GT(seg_ack, 14574 (uint32_t)(uintptr_t)(mp1->b_next))) { 14575 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14576 mp1->b_next = NULL; 14577 } 14578 break; 14579 } 14580 mp1->b_next = NULL; 14581 mp1->b_prev = NULL; 14582 mp2 = mp1; 14583 mp1 = mp1->b_cont; 14584 14585 /* 14586 * This notification is required for some zero-copy 14587 * clients to maintain a copy semantic. After the data 14588 * is ack'ed, client is safe to modify or reuse the buffer. 14589 */ 14590 if (tcp->tcp_snd_zcopy_aware && 14591 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14592 tcp_zcopy_notify(tcp); 14593 freeb(mp2); 14594 if (bytes_acked == 0) { 14595 if (mp1 == NULL) { 14596 /* Everything is ack'ed, clear the tail. */ 14597 tcp->tcp_xmit_tail = NULL; 14598 /* 14599 * Cancel the timer unless we are still 14600 * waiting for an ACK for the FIN packet. 14601 */ 14602 if (tcp->tcp_timer_tid != 0 && 14603 tcp->tcp_snxt == tcp->tcp_suna) { 14604 (void) TCP_TIMER_CANCEL(tcp, 14605 tcp->tcp_timer_tid); 14606 tcp->tcp_timer_tid = 0; 14607 } 14608 goto pre_swnd_update; 14609 } 14610 if (mp2 != tcp->tcp_xmit_tail) 14611 break; 14612 tcp->tcp_xmit_tail = mp1; 14613 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14614 (uintptr_t)INT_MAX); 14615 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14616 mp1->b_rptr); 14617 break; 14618 } 14619 if (mp1 == NULL) { 14620 /* 14621 * More was acked but there is nothing more 14622 * outstanding. This means that the FIN was 14623 * just acked or that we're talking to a clown. 14624 */ 14625 fin_acked: 14626 ASSERT(tcp->tcp_fin_sent); 14627 tcp->tcp_xmit_tail = NULL; 14628 if (tcp->tcp_fin_sent) { 14629 /* FIN was acked - making progress */ 14630 if (tcp->tcp_ipversion == IPV6_VERSION && 14631 !tcp->tcp_fin_acked) 14632 tcp->tcp_ip_forward_progress = B_TRUE; 14633 tcp->tcp_fin_acked = B_TRUE; 14634 if (tcp->tcp_linger_tid != 0 && 14635 TCP_TIMER_CANCEL(tcp, 14636 tcp->tcp_linger_tid) >= 0) { 14637 tcp_stop_lingering(tcp); 14638 } 14639 } else { 14640 /* 14641 * We should never get here because 14642 * we have already checked that the 14643 * number of bytes ack'ed should be 14644 * smaller than or equal to what we 14645 * have sent so far (it is the 14646 * acceptability check of the ACK). 14647 * We can only get here if the send 14648 * queue is corrupted. 14649 * 14650 * Terminate the connection and 14651 * panic the system. It is better 14652 * for us to panic instead of 14653 * continuing to avoid other disaster. 14654 */ 14655 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14656 tcp->tcp_rnxt, TH_RST|TH_ACK); 14657 panic("Memory corruption " 14658 "detected for connection %s.", 14659 tcp_display(tcp, NULL, 14660 DISP_ADDR_AND_PORT)); 14661 /*NOTREACHED*/ 14662 } 14663 goto pre_swnd_update; 14664 } 14665 ASSERT(mp2 != tcp->tcp_xmit_tail); 14666 } 14667 if (tcp->tcp_unsent) { 14668 flags |= TH_XMIT_NEEDED; 14669 } 14670 pre_swnd_update: 14671 tcp->tcp_xmit_head = mp1; 14672 swnd_update: 14673 /* 14674 * The following check is different from most other implementations. 14675 * For bi-directional transfer, when segments are dropped, the 14676 * "normal" check will not accept a window update in those 14677 * retransmitted segemnts. Failing to do that, TCP may send out 14678 * segments which are outside receiver's window. As TCP accepts 14679 * the ack in those retransmitted segments, if the window update in 14680 * the same segment is not accepted, TCP will incorrectly calculates 14681 * that it can send more segments. This can create a deadlock 14682 * with the receiver if its window becomes zero. 14683 */ 14684 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14685 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14686 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14687 /* 14688 * The criteria for update is: 14689 * 14690 * 1. the segment acknowledges some data. Or 14691 * 2. the segment is new, i.e. it has a higher seq num. Or 14692 * 3. the segment is not old and the advertised window is 14693 * larger than the previous advertised window. 14694 */ 14695 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14696 flags |= TH_XMIT_NEEDED; 14697 tcp->tcp_swnd = new_swnd; 14698 if (new_swnd > tcp->tcp_max_swnd) 14699 tcp->tcp_max_swnd = new_swnd; 14700 tcp->tcp_swl1 = seg_seq; 14701 tcp->tcp_swl2 = seg_ack; 14702 } 14703 est: 14704 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14705 14706 switch (tcp->tcp_state) { 14707 case TCPS_FIN_WAIT_1: 14708 if (tcp->tcp_fin_acked) { 14709 tcp->tcp_state = TCPS_FIN_WAIT_2; 14710 /* 14711 * We implement the non-standard BSD/SunOS 14712 * FIN_WAIT_2 flushing algorithm. 14713 * If there is no user attached to this 14714 * TCP endpoint, then this TCP struct 14715 * could hang around forever in FIN_WAIT_2 14716 * state if the peer forgets to send us 14717 * a FIN. To prevent this, we wait only 14718 * 2*MSL (a convenient time value) for 14719 * the FIN to arrive. If it doesn't show up, 14720 * we flush the TCP endpoint. This algorithm, 14721 * though a violation of RFC-793, has worked 14722 * for over 10 years in BSD systems. 14723 * Note: SunOS 4.x waits 675 seconds before 14724 * flushing the FIN_WAIT_2 connection. 14725 */ 14726 TCP_TIMER_RESTART(tcp, 14727 tcps->tcps_fin_wait_2_flush_interval); 14728 } 14729 break; 14730 case TCPS_FIN_WAIT_2: 14731 break; /* Shutdown hook? */ 14732 case TCPS_LAST_ACK: 14733 freemsg(mp); 14734 if (tcp->tcp_fin_acked) { 14735 (void) tcp_clean_death(tcp, 0, 19); 14736 return; 14737 } 14738 goto xmit_check; 14739 case TCPS_CLOSING: 14740 if (tcp->tcp_fin_acked) { 14741 tcp->tcp_state = TCPS_TIME_WAIT; 14742 /* 14743 * Unconditionally clear the exclusive binding 14744 * bit so this TIME-WAIT connection won't 14745 * interfere with new ones. 14746 */ 14747 tcp->tcp_exclbind = 0; 14748 if (!TCP_IS_DETACHED(tcp)) { 14749 TCP_TIMER_RESTART(tcp, 14750 tcps->tcps_time_wait_interval); 14751 } else { 14752 tcp_time_wait_append(tcp); 14753 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14754 } 14755 } 14756 /*FALLTHRU*/ 14757 case TCPS_CLOSE_WAIT: 14758 freemsg(mp); 14759 goto xmit_check; 14760 default: 14761 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14762 break; 14763 } 14764 } 14765 if (flags & TH_FIN) { 14766 /* Make sure we ack the fin */ 14767 flags |= TH_ACK_NEEDED; 14768 if (!tcp->tcp_fin_rcvd) { 14769 tcp->tcp_fin_rcvd = B_TRUE; 14770 tcp->tcp_rnxt++; 14771 tcph = tcp->tcp_tcph; 14772 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14773 14774 /* 14775 * Generate the ordrel_ind at the end unless we 14776 * are an eager guy. 14777 * In the eager case tcp_rsrv will do this when run 14778 * after tcp_accept is done. 14779 */ 14780 if (tcp->tcp_listener == NULL && 14781 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14782 flags |= TH_ORDREL_NEEDED; 14783 switch (tcp->tcp_state) { 14784 case TCPS_SYN_RCVD: 14785 case TCPS_ESTABLISHED: 14786 tcp->tcp_state = TCPS_CLOSE_WAIT; 14787 /* Keepalive? */ 14788 break; 14789 case TCPS_FIN_WAIT_1: 14790 if (!tcp->tcp_fin_acked) { 14791 tcp->tcp_state = TCPS_CLOSING; 14792 break; 14793 } 14794 /* FALLTHRU */ 14795 case TCPS_FIN_WAIT_2: 14796 tcp->tcp_state = TCPS_TIME_WAIT; 14797 /* 14798 * Unconditionally clear the exclusive binding 14799 * bit so this TIME-WAIT connection won't 14800 * interfere with new ones. 14801 */ 14802 tcp->tcp_exclbind = 0; 14803 if (!TCP_IS_DETACHED(tcp)) { 14804 TCP_TIMER_RESTART(tcp, 14805 tcps->tcps_time_wait_interval); 14806 } else { 14807 tcp_time_wait_append(tcp); 14808 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14809 } 14810 if (seg_len) { 14811 /* 14812 * implies data piggybacked on FIN. 14813 * break to handle data. 14814 */ 14815 break; 14816 } 14817 freemsg(mp); 14818 goto ack_check; 14819 } 14820 } 14821 } 14822 if (mp == NULL) 14823 goto xmit_check; 14824 if (seg_len == 0) { 14825 freemsg(mp); 14826 goto xmit_check; 14827 } 14828 if (mp->b_rptr == mp->b_wptr) { 14829 /* 14830 * The header has been consumed, so we remove the 14831 * zero-length mblk here. 14832 */ 14833 mp1 = mp; 14834 mp = mp->b_cont; 14835 freeb(mp1); 14836 } 14837 tcph = tcp->tcp_tcph; 14838 tcp->tcp_rack_cnt++; 14839 { 14840 uint32_t cur_max; 14841 14842 cur_max = tcp->tcp_rack_cur_max; 14843 if (tcp->tcp_rack_cnt >= cur_max) { 14844 /* 14845 * We have more unacked data than we should - send 14846 * an ACK now. 14847 */ 14848 flags |= TH_ACK_NEEDED; 14849 cur_max++; 14850 if (cur_max > tcp->tcp_rack_abs_max) 14851 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14852 else 14853 tcp->tcp_rack_cur_max = cur_max; 14854 } else if (TCP_IS_DETACHED(tcp)) { 14855 /* We don't have an ACK timer for detached TCP. */ 14856 flags |= TH_ACK_NEEDED; 14857 } else if (seg_len < mss) { 14858 /* 14859 * If we get a segment that is less than an mss, and we 14860 * already have unacknowledged data, and the amount 14861 * unacknowledged is not a multiple of mss, then we 14862 * better generate an ACK now. Otherwise, this may be 14863 * the tail piece of a transaction, and we would rather 14864 * wait for the response. 14865 */ 14866 uint32_t udif; 14867 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14868 (uintptr_t)INT_MAX); 14869 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14870 if (udif && (udif % mss)) 14871 flags |= TH_ACK_NEEDED; 14872 else 14873 flags |= TH_ACK_TIMER_NEEDED; 14874 } else { 14875 /* Start delayed ack timer */ 14876 flags |= TH_ACK_TIMER_NEEDED; 14877 } 14878 } 14879 tcp->tcp_rnxt += seg_len; 14880 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14881 14882 /* Update SACK list */ 14883 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14884 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14885 &(tcp->tcp_num_sack_blk)); 14886 } 14887 14888 if (tcp->tcp_urp_mp) { 14889 tcp->tcp_urp_mp->b_cont = mp; 14890 mp = tcp->tcp_urp_mp; 14891 tcp->tcp_urp_mp = NULL; 14892 /* Ready for a new signal. */ 14893 tcp->tcp_urp_last_valid = B_FALSE; 14894 #ifdef DEBUG 14895 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14896 "tcp_rput: sending exdata_ind %s", 14897 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14898 #endif /* DEBUG */ 14899 } 14900 14901 /* 14902 * Check for ancillary data changes compared to last segment. 14903 */ 14904 if (tcp->tcp_ipv6_recvancillary != 0) { 14905 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14906 if (mp == NULL) 14907 return; 14908 } 14909 14910 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14911 /* 14912 * Side queue inbound data until the accept happens. 14913 * tcp_accept/tcp_rput drains this when the accept happens. 14914 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14915 * T_EXDATA_IND) it is queued on b_next. 14916 * XXX Make urgent data use this. Requires: 14917 * Removing tcp_listener check for TH_URG 14918 * Making M_PCPROTO and MARK messages skip the eager case 14919 */ 14920 14921 if (tcp->tcp_kssl_pending) { 14922 tcp_kssl_input(tcp, mp); 14923 } else { 14924 tcp_rcv_enqueue(tcp, mp, seg_len); 14925 } 14926 } else { 14927 if (mp->b_datap->db_type != M_DATA || 14928 (flags & TH_MARKNEXT_NEEDED)) { 14929 if (tcp->tcp_rcv_list != NULL) { 14930 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14931 } 14932 ASSERT(tcp->tcp_rcv_list == NULL || 14933 tcp->tcp_fused_sigurg); 14934 if (flags & TH_MARKNEXT_NEEDED) { 14935 #ifdef DEBUG 14936 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14937 "tcp_rput: sending MSGMARKNEXT %s", 14938 tcp_display(tcp, NULL, 14939 DISP_PORT_ONLY)); 14940 #endif /* DEBUG */ 14941 mp->b_flag |= MSGMARKNEXT; 14942 flags &= ~TH_MARKNEXT_NEEDED; 14943 } 14944 14945 /* Does this need SSL processing first? */ 14946 if ((tcp->tcp_kssl_ctx != NULL) && 14947 (DB_TYPE(mp) == M_DATA)) { 14948 tcp_kssl_input(tcp, mp); 14949 } else { 14950 putnext(tcp->tcp_rq, mp); 14951 if (!canputnext(tcp->tcp_rq)) 14952 tcp->tcp_rwnd -= seg_len; 14953 } 14954 } else if ((flags & (TH_PUSH|TH_FIN)) || 14955 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 14956 if (tcp->tcp_rcv_list != NULL) { 14957 /* 14958 * Enqueue the new segment first and then 14959 * call tcp_rcv_drain() to send all data 14960 * up. The other way to do this is to 14961 * send all queued data up and then call 14962 * putnext() to send the new segment up. 14963 * This way can remove the else part later 14964 * on. 14965 * 14966 * We don't this to avoid one more call to 14967 * canputnext() as tcp_rcv_drain() needs to 14968 * call canputnext(). 14969 */ 14970 tcp_rcv_enqueue(tcp, mp, seg_len); 14971 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14972 } else { 14973 /* Does this need SSL processing first? */ 14974 if ((tcp->tcp_kssl_ctx != NULL) && 14975 (DB_TYPE(mp) == M_DATA)) { 14976 tcp_kssl_input(tcp, mp); 14977 } else { 14978 putnext(tcp->tcp_rq, mp); 14979 if (!canputnext(tcp->tcp_rq)) 14980 tcp->tcp_rwnd -= seg_len; 14981 } 14982 } 14983 } else { 14984 /* 14985 * Enqueue all packets when processing an mblk 14986 * from the co queue and also enqueue normal packets. 14987 */ 14988 tcp_rcv_enqueue(tcp, mp, seg_len); 14989 } 14990 /* 14991 * Make sure the timer is running if we have data waiting 14992 * for a push bit. This provides resiliency against 14993 * implementations that do not correctly generate push bits. 14994 */ 14995 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 14996 /* 14997 * The connection may be closed at this point, so don't 14998 * do anything for a detached tcp. 14999 */ 15000 if (!TCP_IS_DETACHED(tcp)) 15001 tcp->tcp_push_tid = TCP_TIMER(tcp, 15002 tcp_push_timer, 15003 MSEC_TO_TICK(tcps->tcps_push_timer_interval)); 15004 } 15005 } 15006 xmit_check: 15007 /* Is there anything left to do? */ 15008 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15009 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15010 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15011 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15012 goto done; 15013 15014 /* Any transmit work to do and a non-zero window? */ 15015 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15016 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15017 if (flags & TH_REXMIT_NEEDED) { 15018 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15019 15020 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15021 if (snd_size > mss) 15022 snd_size = mss; 15023 if (snd_size > tcp->tcp_swnd) 15024 snd_size = tcp->tcp_swnd; 15025 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15026 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15027 B_TRUE); 15028 15029 if (mp1 != NULL) { 15030 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15031 tcp->tcp_csuna = tcp->tcp_snxt; 15032 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15033 UPDATE_MIB(&tcps->tcps_mib, 15034 tcpRetransBytes, snd_size); 15035 TCP_RECORD_TRACE(tcp, mp1, 15036 TCP_TRACE_SEND_PKT); 15037 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15038 } 15039 } 15040 if (flags & TH_NEED_SACK_REXMIT) { 15041 tcp_sack_rxmit(tcp, &flags); 15042 } 15043 /* 15044 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15045 * out new segment. Note that tcp_rexmit should not be 15046 * set, otherwise TH_LIMIT_XMIT should not be set. 15047 */ 15048 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15049 if (!tcp->tcp_rexmit) { 15050 tcp_wput_data(tcp, NULL, B_FALSE); 15051 } else { 15052 tcp_ss_rexmit(tcp); 15053 } 15054 } 15055 /* 15056 * Adjust tcp_cwnd back to normal value after sending 15057 * new data segments. 15058 */ 15059 if (flags & TH_LIMIT_XMIT) { 15060 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15061 /* 15062 * This will restart the timer. Restarting the 15063 * timer is used to avoid a timeout before the 15064 * limited transmitted segment's ACK gets back. 15065 */ 15066 if (tcp->tcp_xmit_head != NULL) 15067 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15068 } 15069 15070 /* Anything more to do? */ 15071 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15072 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15073 goto done; 15074 } 15075 ack_check: 15076 if (flags & TH_SEND_URP_MARK) { 15077 ASSERT(tcp->tcp_urp_mark_mp); 15078 /* 15079 * Send up any queued data and then send the mark message 15080 */ 15081 if (tcp->tcp_rcv_list != NULL) { 15082 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15083 } 15084 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15085 15086 mp1 = tcp->tcp_urp_mark_mp; 15087 tcp->tcp_urp_mark_mp = NULL; 15088 #ifdef DEBUG 15089 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15090 "tcp_rput: sending zero-length %s %s", 15091 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15092 "MSGNOTMARKNEXT"), 15093 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15094 #endif /* DEBUG */ 15095 putnext(tcp->tcp_rq, mp1); 15096 flags &= ~TH_SEND_URP_MARK; 15097 } 15098 if (flags & TH_ACK_NEEDED) { 15099 /* 15100 * Time to send an ack for some reason. 15101 */ 15102 mp1 = tcp_ack_mp(tcp); 15103 15104 if (mp1 != NULL) { 15105 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 15106 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15107 BUMP_LOCAL(tcp->tcp_obsegs); 15108 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15109 } 15110 if (tcp->tcp_ack_tid != 0) { 15111 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15112 tcp->tcp_ack_tid = 0; 15113 } 15114 } 15115 if (flags & TH_ACK_TIMER_NEEDED) { 15116 /* 15117 * Arrange for deferred ACK or push wait timeout. 15118 * Start timer if it is not already running. 15119 */ 15120 if (tcp->tcp_ack_tid == 0) { 15121 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15122 MSEC_TO_TICK(tcp->tcp_localnet ? 15123 (clock_t)tcps->tcps_local_dack_interval : 15124 (clock_t)tcps->tcps_deferred_ack_interval)); 15125 } 15126 } 15127 if (flags & TH_ORDREL_NEEDED) { 15128 /* 15129 * Send up the ordrel_ind unless we are an eager guy. 15130 * In the eager case tcp_rsrv will do this when run 15131 * after tcp_accept is done. 15132 */ 15133 ASSERT(tcp->tcp_listener == NULL); 15134 if (tcp->tcp_rcv_list != NULL) { 15135 /* 15136 * Push any mblk(s) enqueued from co processing. 15137 */ 15138 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15139 } 15140 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15141 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 15142 tcp->tcp_ordrel_done = B_TRUE; 15143 putnext(tcp->tcp_rq, mp1); 15144 if (tcp->tcp_deferred_clean_death) { 15145 /* 15146 * tcp_clean_death was deferred 15147 * for T_ORDREL_IND - do it now 15148 */ 15149 (void) tcp_clean_death(tcp, 15150 tcp->tcp_client_errno, 20); 15151 tcp->tcp_deferred_clean_death = B_FALSE; 15152 } 15153 } else { 15154 /* 15155 * Run the orderly release in the 15156 * service routine. 15157 */ 15158 qenable(tcp->tcp_rq); 15159 /* 15160 * Caveat(XXX): The machine may be so 15161 * overloaded that tcp_rsrv() is not scheduled 15162 * until after the endpoint has transitioned 15163 * to TCPS_TIME_WAIT 15164 * and tcp_time_wait_interval expires. Then 15165 * tcp_timer() will blow away state in tcp_t 15166 * and T_ORDREL_IND will never be delivered 15167 * upstream. Unlikely but potentially 15168 * a problem. 15169 */ 15170 } 15171 } 15172 done: 15173 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15174 } 15175 15176 /* 15177 * This function does PAWS protection check. Returns B_TRUE if the 15178 * segment passes the PAWS test, else returns B_FALSE. 15179 */ 15180 boolean_t 15181 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15182 { 15183 uint8_t flags; 15184 int options; 15185 uint8_t *up; 15186 15187 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15188 /* 15189 * If timestamp option is aligned nicely, get values inline, 15190 * otherwise call general routine to parse. Only do that 15191 * if timestamp is the only option. 15192 */ 15193 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15194 TCPOPT_REAL_TS_LEN && 15195 OK_32PTR((up = ((uint8_t *)tcph) + 15196 TCP_MIN_HEADER_LENGTH)) && 15197 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15198 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15199 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15200 15201 options = TCP_OPT_TSTAMP_PRESENT; 15202 } else { 15203 if (tcp->tcp_snd_sack_ok) { 15204 tcpoptp->tcp = tcp; 15205 } else { 15206 tcpoptp->tcp = NULL; 15207 } 15208 options = tcp_parse_options(tcph, tcpoptp); 15209 } 15210 15211 if (options & TCP_OPT_TSTAMP_PRESENT) { 15212 /* 15213 * Do PAWS per RFC 1323 section 4.2. Accept RST 15214 * regardless of the timestamp, page 18 RFC 1323.bis. 15215 */ 15216 if ((flags & TH_RST) == 0 && 15217 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15218 tcp->tcp_ts_recent)) { 15219 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15220 PAWS_TIMEOUT)) { 15221 /* This segment is not acceptable. */ 15222 return (B_FALSE); 15223 } else { 15224 /* 15225 * Connection has been idle for 15226 * too long. Reset the timestamp 15227 * and assume the segment is valid. 15228 */ 15229 tcp->tcp_ts_recent = 15230 tcpoptp->tcp_opt_ts_val; 15231 } 15232 } 15233 } else { 15234 /* 15235 * If we don't get a timestamp on every packet, we 15236 * figure we can't really trust 'em, so we stop sending 15237 * and parsing them. 15238 */ 15239 tcp->tcp_snd_ts_ok = B_FALSE; 15240 15241 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15242 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15243 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15244 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN); 15245 if (tcp->tcp_snd_sack_ok) { 15246 ASSERT(tcp->tcp_sack_info != NULL); 15247 tcp->tcp_max_sack_blk = 4; 15248 } 15249 } 15250 return (B_TRUE); 15251 } 15252 15253 /* 15254 * Attach ancillary data to a received TCP segments for the 15255 * ancillary pieces requested by the application that are 15256 * different than they were in the previous data segment. 15257 * 15258 * Save the "current" values once memory allocation is ok so that 15259 * when memory allocation fails we can just wait for the next data segment. 15260 */ 15261 static mblk_t * 15262 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15263 { 15264 struct T_optdata_ind *todi; 15265 int optlen; 15266 uchar_t *optptr; 15267 struct T_opthdr *toh; 15268 uint_t addflag; /* Which pieces to add */ 15269 mblk_t *mp1; 15270 15271 optlen = 0; 15272 addflag = 0; 15273 /* If app asked for pktinfo and the index has changed ... */ 15274 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15275 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15276 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15277 optlen += sizeof (struct T_opthdr) + 15278 sizeof (struct in6_pktinfo); 15279 addflag |= TCP_IPV6_RECVPKTINFO; 15280 } 15281 /* If app asked for hoplimit and it has changed ... */ 15282 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15283 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15284 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15285 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15286 addflag |= TCP_IPV6_RECVHOPLIMIT; 15287 } 15288 /* If app asked for tclass and it has changed ... */ 15289 if ((ipp->ipp_fields & IPPF_TCLASS) && 15290 ipp->ipp_tclass != tcp->tcp_recvtclass && 15291 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15292 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15293 addflag |= TCP_IPV6_RECVTCLASS; 15294 } 15295 /* 15296 * If app asked for hopbyhop headers and it has changed ... 15297 * For security labels, note that (1) security labels can't change on 15298 * a connected socket at all, (2) we're connected to at most one peer, 15299 * (3) if anything changes, then it must be some other extra option. 15300 */ 15301 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15302 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15303 (ipp->ipp_fields & IPPF_HOPOPTS), 15304 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15305 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15306 tcp->tcp_label_len; 15307 addflag |= TCP_IPV6_RECVHOPOPTS; 15308 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15309 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15310 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15311 return (mp); 15312 } 15313 /* If app asked for dst headers before routing headers ... */ 15314 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15315 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15316 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15317 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15318 optlen += sizeof (struct T_opthdr) + 15319 ipp->ipp_rtdstoptslen; 15320 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15321 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15322 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15323 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15324 return (mp); 15325 } 15326 /* If app asked for routing headers and it has changed ... */ 15327 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15328 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15329 (ipp->ipp_fields & IPPF_RTHDR), 15330 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15331 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15332 addflag |= TCP_IPV6_RECVRTHDR; 15333 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15334 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15335 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15336 return (mp); 15337 } 15338 /* If app asked for dest headers and it has changed ... */ 15339 if ((tcp->tcp_ipv6_recvancillary & 15340 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15341 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15342 (ipp->ipp_fields & IPPF_DSTOPTS), 15343 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15344 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15345 addflag |= TCP_IPV6_RECVDSTOPTS; 15346 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15347 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15348 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15349 return (mp); 15350 } 15351 15352 if (optlen == 0) { 15353 /* Nothing to add */ 15354 return (mp); 15355 } 15356 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15357 if (mp1 == NULL) { 15358 /* 15359 * Defer sending ancillary data until the next TCP segment 15360 * arrives. 15361 */ 15362 return (mp); 15363 } 15364 mp1->b_cont = mp; 15365 mp = mp1; 15366 mp->b_wptr += sizeof (*todi) + optlen; 15367 mp->b_datap->db_type = M_PROTO; 15368 todi = (struct T_optdata_ind *)mp->b_rptr; 15369 todi->PRIM_type = T_OPTDATA_IND; 15370 todi->DATA_flag = 1; /* MORE data */ 15371 todi->OPT_length = optlen; 15372 todi->OPT_offset = sizeof (*todi); 15373 optptr = (uchar_t *)&todi[1]; 15374 /* 15375 * If app asked for pktinfo and the index has changed ... 15376 * Note that the local address never changes for the connection. 15377 */ 15378 if (addflag & TCP_IPV6_RECVPKTINFO) { 15379 struct in6_pktinfo *pkti; 15380 15381 toh = (struct T_opthdr *)optptr; 15382 toh->level = IPPROTO_IPV6; 15383 toh->name = IPV6_PKTINFO; 15384 toh->len = sizeof (*toh) + sizeof (*pkti); 15385 toh->status = 0; 15386 optptr += sizeof (*toh); 15387 pkti = (struct in6_pktinfo *)optptr; 15388 if (tcp->tcp_ipversion == IPV6_VERSION) 15389 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15390 else 15391 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15392 &pkti->ipi6_addr); 15393 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15394 optptr += sizeof (*pkti); 15395 ASSERT(OK_32PTR(optptr)); 15396 /* Save as "last" value */ 15397 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15398 } 15399 /* If app asked for hoplimit and it has changed ... */ 15400 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15401 toh = (struct T_opthdr *)optptr; 15402 toh->level = IPPROTO_IPV6; 15403 toh->name = IPV6_HOPLIMIT; 15404 toh->len = sizeof (*toh) + sizeof (uint_t); 15405 toh->status = 0; 15406 optptr += sizeof (*toh); 15407 *(uint_t *)optptr = ipp->ipp_hoplimit; 15408 optptr += sizeof (uint_t); 15409 ASSERT(OK_32PTR(optptr)); 15410 /* Save as "last" value */ 15411 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15412 } 15413 /* If app asked for tclass and it has changed ... */ 15414 if (addflag & TCP_IPV6_RECVTCLASS) { 15415 toh = (struct T_opthdr *)optptr; 15416 toh->level = IPPROTO_IPV6; 15417 toh->name = IPV6_TCLASS; 15418 toh->len = sizeof (*toh) + sizeof (uint_t); 15419 toh->status = 0; 15420 optptr += sizeof (*toh); 15421 *(uint_t *)optptr = ipp->ipp_tclass; 15422 optptr += sizeof (uint_t); 15423 ASSERT(OK_32PTR(optptr)); 15424 /* Save as "last" value */ 15425 tcp->tcp_recvtclass = ipp->ipp_tclass; 15426 } 15427 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15428 toh = (struct T_opthdr *)optptr; 15429 toh->level = IPPROTO_IPV6; 15430 toh->name = IPV6_HOPOPTS; 15431 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15432 tcp->tcp_label_len; 15433 toh->status = 0; 15434 optptr += sizeof (*toh); 15435 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15436 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15437 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15438 ASSERT(OK_32PTR(optptr)); 15439 /* Save as last value */ 15440 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15441 (ipp->ipp_fields & IPPF_HOPOPTS), 15442 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15443 } 15444 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15445 toh = (struct T_opthdr *)optptr; 15446 toh->level = IPPROTO_IPV6; 15447 toh->name = IPV6_RTHDRDSTOPTS; 15448 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15449 toh->status = 0; 15450 optptr += sizeof (*toh); 15451 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15452 optptr += ipp->ipp_rtdstoptslen; 15453 ASSERT(OK_32PTR(optptr)); 15454 /* Save as last value */ 15455 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15456 &tcp->tcp_rtdstoptslen, 15457 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15458 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15459 } 15460 if (addflag & TCP_IPV6_RECVRTHDR) { 15461 toh = (struct T_opthdr *)optptr; 15462 toh->level = IPPROTO_IPV6; 15463 toh->name = IPV6_RTHDR; 15464 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15465 toh->status = 0; 15466 optptr += sizeof (*toh); 15467 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15468 optptr += ipp->ipp_rthdrlen; 15469 ASSERT(OK_32PTR(optptr)); 15470 /* Save as last value */ 15471 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15472 (ipp->ipp_fields & IPPF_RTHDR), 15473 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15474 } 15475 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15476 toh = (struct T_opthdr *)optptr; 15477 toh->level = IPPROTO_IPV6; 15478 toh->name = IPV6_DSTOPTS; 15479 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15480 toh->status = 0; 15481 optptr += sizeof (*toh); 15482 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15483 optptr += ipp->ipp_dstoptslen; 15484 ASSERT(OK_32PTR(optptr)); 15485 /* Save as last value */ 15486 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15487 (ipp->ipp_fields & IPPF_DSTOPTS), 15488 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15489 } 15490 ASSERT(optptr == mp->b_wptr); 15491 return (mp); 15492 } 15493 15494 15495 /* 15496 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15497 * or a "bad" IRE detected by tcp_adapt_ire. 15498 * We can't tell if the failure was due to the laddr or the faddr 15499 * thus we clear out all addresses and ports. 15500 */ 15501 static void 15502 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15503 { 15504 queue_t *q = tcp->tcp_rq; 15505 tcph_t *tcph; 15506 struct T_error_ack *tea; 15507 conn_t *connp = tcp->tcp_connp; 15508 15509 15510 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15511 15512 if (mp->b_cont) { 15513 freemsg(mp->b_cont); 15514 mp->b_cont = NULL; 15515 } 15516 tea = (struct T_error_ack *)mp->b_rptr; 15517 switch (tea->PRIM_type) { 15518 case T_BIND_ACK: 15519 /* 15520 * Need to unbind with classifier since we were just told that 15521 * our bind succeeded. 15522 */ 15523 tcp->tcp_hard_bound = B_FALSE; 15524 tcp->tcp_hard_binding = B_FALSE; 15525 15526 ipcl_hash_remove(connp); 15527 /* Reuse the mblk if possible */ 15528 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15529 sizeof (*tea)); 15530 mp->b_rptr = mp->b_datap->db_base; 15531 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15532 tea = (struct T_error_ack *)mp->b_rptr; 15533 tea->PRIM_type = T_ERROR_ACK; 15534 tea->TLI_error = TSYSERR; 15535 tea->UNIX_error = error; 15536 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15537 tea->ERROR_prim = T_CONN_REQ; 15538 } else { 15539 tea->ERROR_prim = O_T_BIND_REQ; 15540 } 15541 break; 15542 15543 case T_ERROR_ACK: 15544 if (tcp->tcp_state >= TCPS_SYN_SENT) 15545 tea->ERROR_prim = T_CONN_REQ; 15546 break; 15547 default: 15548 panic("tcp_bind_failed: unexpected TPI type"); 15549 /*NOTREACHED*/ 15550 } 15551 15552 tcp->tcp_state = TCPS_IDLE; 15553 if (tcp->tcp_ipversion == IPV4_VERSION) 15554 tcp->tcp_ipha->ipha_src = 0; 15555 else 15556 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15557 /* 15558 * Copy of the src addr. in tcp_t is needed since 15559 * the lookup funcs. can only look at tcp_t 15560 */ 15561 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15562 15563 tcph = tcp->tcp_tcph; 15564 tcph->th_lport[0] = 0; 15565 tcph->th_lport[1] = 0; 15566 tcp_bind_hash_remove(tcp); 15567 bzero(&connp->u_port, sizeof (connp->u_port)); 15568 /* blow away saved option results if any */ 15569 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15570 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15571 15572 conn_delete_ire(tcp->tcp_connp, NULL); 15573 putnext(q, mp); 15574 } 15575 15576 /* 15577 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15578 * messages. 15579 */ 15580 void 15581 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15582 { 15583 mblk_t *mp1; 15584 uchar_t *rptr = mp->b_rptr; 15585 queue_t *q = tcp->tcp_rq; 15586 struct T_error_ack *tea; 15587 uint32_t mss; 15588 mblk_t *syn_mp; 15589 mblk_t *mdti; 15590 mblk_t *lsoi; 15591 int retval; 15592 mblk_t *ire_mp; 15593 tcp_stack_t *tcps = tcp->tcp_tcps; 15594 15595 switch (mp->b_datap->db_type) { 15596 case M_PROTO: 15597 case M_PCPROTO: 15598 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15599 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15600 break; 15601 tea = (struct T_error_ack *)rptr; 15602 switch (tea->PRIM_type) { 15603 case T_BIND_ACK: 15604 /* 15605 * Adapt Multidata information, if any. The 15606 * following tcp_mdt_update routine will free 15607 * the message. 15608 */ 15609 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15610 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15611 b_rptr)->mdt_capab, B_TRUE); 15612 freemsg(mdti); 15613 } 15614 15615 /* 15616 * Check to update LSO information with tcp, and 15617 * tcp_lso_update routine will free the message. 15618 */ 15619 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 15620 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 15621 b_rptr)->lso_capab); 15622 freemsg(lsoi); 15623 } 15624 15625 /* Get the IRE, if we had requested for it */ 15626 ire_mp = tcp_ire_mp(mp); 15627 15628 if (tcp->tcp_hard_binding) { 15629 tcp->tcp_hard_binding = B_FALSE; 15630 tcp->tcp_hard_bound = B_TRUE; 15631 CL_INET_CONNECT(tcp); 15632 } else { 15633 if (ire_mp != NULL) 15634 freeb(ire_mp); 15635 goto after_syn_sent; 15636 } 15637 15638 retval = tcp_adapt_ire(tcp, ire_mp); 15639 if (ire_mp != NULL) 15640 freeb(ire_mp); 15641 if (retval == 0) { 15642 tcp_bind_failed(tcp, mp, 15643 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15644 ENETUNREACH : EADDRNOTAVAIL)); 15645 return; 15646 } 15647 /* 15648 * Don't let an endpoint connect to itself. 15649 * Also checked in tcp_connect() but that 15650 * check can't handle the case when the 15651 * local IP address is INADDR_ANY. 15652 */ 15653 if (tcp->tcp_ipversion == IPV4_VERSION) { 15654 if ((tcp->tcp_ipha->ipha_dst == 15655 tcp->tcp_ipha->ipha_src) && 15656 (BE16_EQL(tcp->tcp_tcph->th_lport, 15657 tcp->tcp_tcph->th_fport))) { 15658 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15659 return; 15660 } 15661 } else { 15662 if (IN6_ARE_ADDR_EQUAL( 15663 &tcp->tcp_ip6h->ip6_dst, 15664 &tcp->tcp_ip6h->ip6_src) && 15665 (BE16_EQL(tcp->tcp_tcph->th_lport, 15666 tcp->tcp_tcph->th_fport))) { 15667 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15668 return; 15669 } 15670 } 15671 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15672 /* 15673 * This should not be possible! Just for 15674 * defensive coding... 15675 */ 15676 if (tcp->tcp_state != TCPS_SYN_SENT) 15677 goto after_syn_sent; 15678 15679 if (is_system_labeled() && 15680 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 15681 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 15682 return; 15683 } 15684 15685 ASSERT(q == tcp->tcp_rq); 15686 /* 15687 * tcp_adapt_ire() does not adjust 15688 * for TCP/IP header length. 15689 */ 15690 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15691 15692 /* 15693 * Just make sure our rwnd is at 15694 * least tcp_recv_hiwat_mss * MSS 15695 * large, and round up to the nearest 15696 * MSS. 15697 * 15698 * We do the round up here because 15699 * we need to get the interface 15700 * MTU first before we can do the 15701 * round up. 15702 */ 15703 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15704 tcps->tcps_recv_hiwat_minmss * mss); 15705 q->q_hiwat = tcp->tcp_rwnd; 15706 tcp_set_ws_value(tcp); 15707 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15708 tcp->tcp_tcph->th_win); 15709 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 15710 tcp->tcp_snd_ws_ok = B_TRUE; 15711 15712 /* 15713 * Set tcp_snd_ts_ok to true 15714 * so that tcp_xmit_mp will 15715 * include the timestamp 15716 * option in the SYN segment. 15717 */ 15718 if (tcps->tcps_tstamp_always || 15719 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 15720 tcp->tcp_snd_ts_ok = B_TRUE; 15721 } 15722 15723 /* 15724 * tcp_snd_sack_ok can be set in 15725 * tcp_adapt_ire() if the sack metric 15726 * is set. So check it here also. 15727 */ 15728 if (tcps->tcps_sack_permitted == 2 || 15729 tcp->tcp_snd_sack_ok) { 15730 if (tcp->tcp_sack_info == NULL) { 15731 tcp->tcp_sack_info = 15732 kmem_cache_alloc(tcp_sack_info_cache, 15733 KM_SLEEP); 15734 } 15735 tcp->tcp_snd_sack_ok = B_TRUE; 15736 } 15737 15738 /* 15739 * Should we use ECN? Note that the current 15740 * default value (SunOS 5.9) of tcp_ecn_permitted 15741 * is 1. The reason for doing this is that there 15742 * are equipments out there that will drop ECN 15743 * enabled IP packets. Setting it to 1 avoids 15744 * compatibility problems. 15745 */ 15746 if (tcps->tcps_ecn_permitted == 2) 15747 tcp->tcp_ecn_ok = B_TRUE; 15748 15749 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15750 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15751 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15752 if (syn_mp) { 15753 cred_t *cr; 15754 pid_t pid; 15755 15756 /* 15757 * Obtain the credential from the 15758 * thread calling connect(); the credential 15759 * lives on in the second mblk which 15760 * originated from T_CONN_REQ and is echoed 15761 * with the T_BIND_ACK from ip. If none 15762 * can be found, default to the creator 15763 * of the socket. 15764 */ 15765 if (mp->b_cont == NULL || 15766 (cr = DB_CRED(mp->b_cont)) == NULL) { 15767 cr = tcp->tcp_cred; 15768 pid = tcp->tcp_cpid; 15769 } else { 15770 pid = DB_CPID(mp->b_cont); 15771 } 15772 15773 TCP_RECORD_TRACE(tcp, syn_mp, 15774 TCP_TRACE_SEND_PKT); 15775 mblk_setcred(syn_mp, cr); 15776 DB_CPID(syn_mp) = pid; 15777 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15778 } 15779 after_syn_sent: 15780 /* 15781 * A trailer mblk indicates a waiting client upstream. 15782 * We complete here the processing begun in 15783 * either tcp_bind() or tcp_connect() by passing 15784 * upstream the reply message they supplied. 15785 */ 15786 mp1 = mp; 15787 mp = mp->b_cont; 15788 freeb(mp1); 15789 if (mp) 15790 break; 15791 return; 15792 case T_ERROR_ACK: 15793 if (tcp->tcp_debug) { 15794 (void) strlog(TCP_MOD_ID, 0, 1, 15795 SL_TRACE|SL_ERROR, 15796 "tcp_rput_other: case T_ERROR_ACK, " 15797 "ERROR_prim == %d", 15798 tea->ERROR_prim); 15799 } 15800 switch (tea->ERROR_prim) { 15801 case O_T_BIND_REQ: 15802 case T_BIND_REQ: 15803 tcp_bind_failed(tcp, mp, 15804 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15805 ENETUNREACH : EADDRNOTAVAIL)); 15806 return; 15807 case T_UNBIND_REQ: 15808 tcp->tcp_hard_binding = B_FALSE; 15809 tcp->tcp_hard_bound = B_FALSE; 15810 if (mp->b_cont) { 15811 freemsg(mp->b_cont); 15812 mp->b_cont = NULL; 15813 } 15814 if (tcp->tcp_unbind_pending) 15815 tcp->tcp_unbind_pending = 0; 15816 else { 15817 /* From tcp_ip_unbind() - free */ 15818 freemsg(mp); 15819 return; 15820 } 15821 break; 15822 case T_SVR4_OPTMGMT_REQ: 15823 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15824 /* T_OPTMGMT_REQ generated by TCP */ 15825 printf("T_SVR4_OPTMGMT_REQ failed " 15826 "%d/%d - dropped (cnt %d)\n", 15827 tea->TLI_error, tea->UNIX_error, 15828 tcp->tcp_drop_opt_ack_cnt); 15829 freemsg(mp); 15830 tcp->tcp_drop_opt_ack_cnt--; 15831 return; 15832 } 15833 break; 15834 } 15835 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15836 tcp->tcp_drop_opt_ack_cnt > 0) { 15837 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15838 "- dropped (cnt %d)\n", 15839 tea->TLI_error, tea->UNIX_error, 15840 tcp->tcp_drop_opt_ack_cnt); 15841 freemsg(mp); 15842 tcp->tcp_drop_opt_ack_cnt--; 15843 return; 15844 } 15845 break; 15846 case T_OPTMGMT_ACK: 15847 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15848 /* T_OPTMGMT_REQ generated by TCP */ 15849 freemsg(mp); 15850 tcp->tcp_drop_opt_ack_cnt--; 15851 return; 15852 } 15853 break; 15854 default: 15855 break; 15856 } 15857 break; 15858 case M_FLUSH: 15859 if (*rptr & FLUSHR) 15860 flushq(q, FLUSHDATA); 15861 break; 15862 default: 15863 /* M_CTL will be directly sent to tcp_icmp_error() */ 15864 ASSERT(DB_TYPE(mp) != M_CTL); 15865 break; 15866 } 15867 /* 15868 * Make sure we set this bit before sending the ACK for 15869 * bind. Otherwise accept could possibly run and free 15870 * this tcp struct. 15871 */ 15872 putnext(q, mp); 15873 } 15874 15875 /* 15876 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15877 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15878 * tcp_rsrv() try again. 15879 */ 15880 static void 15881 tcp_ordrel_kick(void *arg) 15882 { 15883 conn_t *connp = (conn_t *)arg; 15884 tcp_t *tcp = connp->conn_tcp; 15885 15886 tcp->tcp_ordrelid = 0; 15887 tcp->tcp_timeout = B_FALSE; 15888 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15889 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15890 qenable(tcp->tcp_rq); 15891 } 15892 } 15893 15894 /* ARGSUSED */ 15895 static void 15896 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15897 { 15898 conn_t *connp = (conn_t *)arg; 15899 tcp_t *tcp = connp->conn_tcp; 15900 queue_t *q = tcp->tcp_rq; 15901 uint_t thwin; 15902 tcp_stack_t *tcps = tcp->tcp_tcps; 15903 15904 freeb(mp); 15905 15906 TCP_STAT(tcps, tcp_rsrv_calls); 15907 15908 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15909 return; 15910 } 15911 15912 if (tcp->tcp_fused) { 15913 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15914 15915 ASSERT(tcp->tcp_fused); 15916 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15917 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15918 ASSERT(!TCP_IS_DETACHED(tcp)); 15919 ASSERT(tcp->tcp_connp->conn_sqp == 15920 peer_tcp->tcp_connp->conn_sqp); 15921 15922 /* 15923 * Normally we would not get backenabled in synchronous 15924 * streams mode, but in case this happens, we need to plug 15925 * synchronous streams during our drain to prevent a race 15926 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15927 */ 15928 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15929 if (tcp->tcp_rcv_list != NULL) 15930 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15931 15932 tcp_clrqfull(peer_tcp); 15933 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15934 TCP_STAT(tcps, tcp_fusion_backenabled); 15935 return; 15936 } 15937 15938 if (canputnext(q)) { 15939 tcp->tcp_rwnd = q->q_hiwat; 15940 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15941 << tcp->tcp_rcv_ws; 15942 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15943 /* 15944 * Send back a window update immediately if TCP is above 15945 * ESTABLISHED state and the increase of the rcv window 15946 * that the other side knows is at least 1 MSS after flow 15947 * control is lifted. 15948 */ 15949 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15950 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15951 tcp_xmit_ctl(NULL, tcp, 15952 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15953 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15954 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 15955 } 15956 } 15957 /* Handle a failure to allocate a T_ORDREL_IND here */ 15958 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15959 ASSERT(tcp->tcp_listener == NULL); 15960 if (tcp->tcp_rcv_list != NULL) { 15961 (void) tcp_rcv_drain(q, tcp); 15962 } 15963 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15964 mp = mi_tpi_ordrel_ind(); 15965 if (mp) { 15966 tcp->tcp_ordrel_done = B_TRUE; 15967 putnext(q, mp); 15968 if (tcp->tcp_deferred_clean_death) { 15969 /* 15970 * tcp_clean_death was deferred for 15971 * T_ORDREL_IND - do it now 15972 */ 15973 tcp->tcp_deferred_clean_death = B_FALSE; 15974 (void) tcp_clean_death(tcp, 15975 tcp->tcp_client_errno, 22); 15976 } 15977 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15978 /* 15979 * If there isn't already a timer running 15980 * start one. Use a 4 second 15981 * timer as a fallback since it can't fail. 15982 */ 15983 tcp->tcp_timeout = B_TRUE; 15984 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15985 MSEC_TO_TICK(4000)); 15986 } 15987 } 15988 } 15989 15990 /* 15991 * The read side service routine is called mostly when we get back-enabled as a 15992 * result of flow control relief. Since we don't actually queue anything in 15993 * TCP, we have no data to send out of here. What we do is clear the receive 15994 * window, and send out a window update. 15995 * This routine is also called to drive an orderly release message upstream 15996 * if the attempt in tcp_rput failed. 15997 */ 15998 static void 15999 tcp_rsrv(queue_t *q) 16000 { 16001 conn_t *connp = Q_TO_CONN(q); 16002 tcp_t *tcp = connp->conn_tcp; 16003 mblk_t *mp; 16004 tcp_stack_t *tcps = tcp->tcp_tcps; 16005 16006 /* No code does a putq on the read side */ 16007 ASSERT(q->q_first == NULL); 16008 16009 /* Nothing to do for the default queue */ 16010 if (q == tcps->tcps_g_q) { 16011 return; 16012 } 16013 16014 mp = allocb(0, BPRI_HI); 16015 if (mp == NULL) { 16016 /* 16017 * We are under memory pressure. Return for now and we 16018 * we will be called again later. 16019 */ 16020 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16021 /* 16022 * If there isn't already a timer running 16023 * start one. Use a 4 second 16024 * timer as a fallback since it can't fail. 16025 */ 16026 tcp->tcp_timeout = B_TRUE; 16027 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16028 MSEC_TO_TICK(4000)); 16029 } 16030 return; 16031 } 16032 CONN_INC_REF(connp); 16033 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16034 SQTAG_TCP_RSRV); 16035 } 16036 16037 /* 16038 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16039 * We do not allow the receive window to shrink. After setting rwnd, 16040 * set the flow control hiwat of the stream. 16041 * 16042 * This function is called in 2 cases: 16043 * 16044 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16045 * connection (passive open) and in tcp_rput_data() for active connect. 16046 * This is called after tcp_mss_set() when the desired MSS value is known. 16047 * This makes sure that our window size is a mutiple of the other side's 16048 * MSS. 16049 * 2) Handling SO_RCVBUF option. 16050 * 16051 * It is ASSUMED that the requested size is a multiple of the current MSS. 16052 * 16053 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16054 * user requests so. 16055 */ 16056 static int 16057 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16058 { 16059 uint32_t mss = tcp->tcp_mss; 16060 uint32_t old_max_rwnd; 16061 uint32_t max_transmittable_rwnd; 16062 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16063 tcp_stack_t *tcps = tcp->tcp_tcps; 16064 16065 if (tcp->tcp_fused) { 16066 size_t sth_hiwat; 16067 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16068 16069 ASSERT(peer_tcp != NULL); 16070 /* 16071 * Record the stream head's high water mark for 16072 * this endpoint; this is used for flow-control 16073 * purposes in tcp_fuse_output(). 16074 */ 16075 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16076 if (!tcp_detached) 16077 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16078 16079 /* 16080 * In the fusion case, the maxpsz stream head value of 16081 * our peer is set according to its send buffer size 16082 * and our receive buffer size; since the latter may 16083 * have changed we need to update the peer's maxpsz. 16084 */ 16085 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16086 return (rwnd); 16087 } 16088 16089 if (tcp_detached) 16090 old_max_rwnd = tcp->tcp_rwnd; 16091 else 16092 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16093 16094 /* 16095 * Insist on a receive window that is at least 16096 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16097 * funny TCP interactions of Nagle algorithm, SWS avoidance 16098 * and delayed acknowledgement. 16099 */ 16100 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16101 16102 /* 16103 * If window size info has already been exchanged, TCP should not 16104 * shrink the window. Shrinking window is doable if done carefully. 16105 * We may add that support later. But so far there is not a real 16106 * need to do that. 16107 */ 16108 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16109 /* MSS may have changed, do a round up again. */ 16110 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16111 } 16112 16113 /* 16114 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16115 * can be applied even before the window scale option is decided. 16116 */ 16117 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16118 if (rwnd > max_transmittable_rwnd) { 16119 rwnd = max_transmittable_rwnd - 16120 (max_transmittable_rwnd % mss); 16121 if (rwnd < mss) 16122 rwnd = max_transmittable_rwnd; 16123 /* 16124 * If we're over the limit we may have to back down tcp_rwnd. 16125 * The increment below won't work for us. So we set all three 16126 * here and the increment below will have no effect. 16127 */ 16128 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16129 } 16130 if (tcp->tcp_localnet) { 16131 tcp->tcp_rack_abs_max = 16132 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16133 } else { 16134 /* 16135 * For a remote host on a different subnet (through a router), 16136 * we ack every other packet to be conforming to RFC1122. 16137 * tcp_deferred_acks_max is default to 2. 16138 */ 16139 tcp->tcp_rack_abs_max = 16140 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16141 } 16142 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16143 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16144 else 16145 tcp->tcp_rack_cur_max = 0; 16146 /* 16147 * Increment the current rwnd by the amount the maximum grew (we 16148 * can not overwrite it since we might be in the middle of a 16149 * connection.) 16150 */ 16151 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16152 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16153 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16154 tcp->tcp_cwnd_max = rwnd; 16155 16156 if (tcp_detached) 16157 return (rwnd); 16158 /* 16159 * We set the maximum receive window into rq->q_hiwat. 16160 * This is not actually used for flow control. 16161 */ 16162 tcp->tcp_rq->q_hiwat = rwnd; 16163 /* 16164 * Set the Stream head high water mark. This doesn't have to be 16165 * here, since we are simply using default values, but we would 16166 * prefer to choose these values algorithmically, with a likely 16167 * relationship to rwnd. 16168 */ 16169 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16170 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16171 return (rwnd); 16172 } 16173 16174 /* 16175 * Return SNMP stuff in buffer in mpdata. 16176 */ 16177 int 16178 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16179 { 16180 mblk_t *mpdata; 16181 mblk_t *mp_conn_ctl = NULL; 16182 mblk_t *mp_conn_tail; 16183 mblk_t *mp_attr_ctl = NULL; 16184 mblk_t *mp_attr_tail; 16185 mblk_t *mp6_conn_ctl = NULL; 16186 mblk_t *mp6_conn_tail; 16187 mblk_t *mp6_attr_ctl = NULL; 16188 mblk_t *mp6_attr_tail; 16189 struct opthdr *optp; 16190 mib2_tcpConnEntry_t tce; 16191 mib2_tcp6ConnEntry_t tce6; 16192 mib2_transportMLPEntry_t mlp; 16193 connf_t *connfp; 16194 conn_t *connp; 16195 int i; 16196 boolean_t ispriv; 16197 zoneid_t zoneid; 16198 int v4_conn_idx; 16199 int v6_conn_idx; 16200 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16201 ip_stack_t *ipst; 16202 16203 if (mpctl == NULL || 16204 (mpdata = mpctl->b_cont) == NULL || 16205 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16206 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16207 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16208 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16209 freemsg(mp_conn_ctl); 16210 freemsg(mp_attr_ctl); 16211 freemsg(mp6_conn_ctl); 16212 freemsg(mp6_attr_ctl); 16213 return (0); 16214 } 16215 16216 /* build table of connections -- need count in fixed part */ 16217 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16218 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16219 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16220 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16221 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16222 16223 ispriv = 16224 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16225 zoneid = Q_TO_CONN(q)->conn_zoneid; 16226 16227 v4_conn_idx = v6_conn_idx = 0; 16228 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16229 16230 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16231 ipst = tcps->tcps_netstack->netstack_ip; 16232 16233 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16234 16235 connp = NULL; 16236 16237 while ((connp = 16238 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16239 tcp_t *tcp; 16240 boolean_t needattr; 16241 16242 if (connp->conn_zoneid != zoneid) 16243 continue; /* not in this zone */ 16244 16245 tcp = connp->conn_tcp; 16246 UPDATE_MIB(&tcps->tcps_mib, 16247 tcpHCInSegs, tcp->tcp_ibsegs); 16248 tcp->tcp_ibsegs = 0; 16249 UPDATE_MIB(&tcps->tcps_mib, 16250 tcpHCOutSegs, tcp->tcp_obsegs); 16251 tcp->tcp_obsegs = 0; 16252 16253 tce6.tcp6ConnState = tce.tcpConnState = 16254 tcp_snmp_state(tcp); 16255 if (tce.tcpConnState == MIB2_TCP_established || 16256 tce.tcpConnState == MIB2_TCP_closeWait) 16257 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16258 16259 needattr = B_FALSE; 16260 bzero(&mlp, sizeof (mlp)); 16261 if (connp->conn_mlp_type != mlptSingle) { 16262 if (connp->conn_mlp_type == mlptShared || 16263 connp->conn_mlp_type == mlptBoth) 16264 mlp.tme_flags |= MIB2_TMEF_SHARED; 16265 if (connp->conn_mlp_type == mlptPrivate || 16266 connp->conn_mlp_type == mlptBoth) 16267 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16268 needattr = B_TRUE; 16269 } 16270 if (connp->conn_peercred != NULL) { 16271 ts_label_t *tsl; 16272 16273 tsl = crgetlabel(connp->conn_peercred); 16274 mlp.tme_doi = label2doi(tsl); 16275 mlp.tme_label = *label2bslabel(tsl); 16276 needattr = B_TRUE; 16277 } 16278 16279 /* Create a message to report on IPv6 entries */ 16280 if (tcp->tcp_ipversion == IPV6_VERSION) { 16281 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16282 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16283 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16284 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16285 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16286 /* Don't want just anybody seeing these... */ 16287 if (ispriv) { 16288 tce6.tcp6ConnEntryInfo.ce_snxt = 16289 tcp->tcp_snxt; 16290 tce6.tcp6ConnEntryInfo.ce_suna = 16291 tcp->tcp_suna; 16292 tce6.tcp6ConnEntryInfo.ce_rnxt = 16293 tcp->tcp_rnxt; 16294 tce6.tcp6ConnEntryInfo.ce_rack = 16295 tcp->tcp_rack; 16296 } else { 16297 /* 16298 * Netstat, unfortunately, uses this to 16299 * get send/receive queue sizes. How to fix? 16300 * Why not compute the difference only? 16301 */ 16302 tce6.tcp6ConnEntryInfo.ce_snxt = 16303 tcp->tcp_snxt - tcp->tcp_suna; 16304 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16305 tce6.tcp6ConnEntryInfo.ce_rnxt = 16306 tcp->tcp_rnxt - tcp->tcp_rack; 16307 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16308 } 16309 16310 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16311 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16312 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16313 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16314 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16315 16316 tce6.tcp6ConnCreationProcess = 16317 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16318 tcp->tcp_cpid; 16319 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16320 16321 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16322 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16323 16324 mlp.tme_connidx = v6_conn_idx++; 16325 if (needattr) 16326 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16327 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16328 } 16329 /* 16330 * Create an IPv4 table entry for IPv4 entries and also 16331 * for IPv6 entries which are bound to in6addr_any 16332 * but don't have IPV6_V6ONLY set. 16333 * (i.e. anything an IPv4 peer could connect to) 16334 */ 16335 if (tcp->tcp_ipversion == IPV4_VERSION || 16336 (tcp->tcp_state <= TCPS_LISTEN && 16337 !tcp->tcp_connp->conn_ipv6_v6only && 16338 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16339 if (tcp->tcp_ipversion == IPV6_VERSION) { 16340 tce.tcpConnRemAddress = INADDR_ANY; 16341 tce.tcpConnLocalAddress = INADDR_ANY; 16342 } else { 16343 tce.tcpConnRemAddress = 16344 tcp->tcp_remote; 16345 tce.tcpConnLocalAddress = 16346 tcp->tcp_ip_src; 16347 } 16348 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16349 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16350 /* Don't want just anybody seeing these... */ 16351 if (ispriv) { 16352 tce.tcpConnEntryInfo.ce_snxt = 16353 tcp->tcp_snxt; 16354 tce.tcpConnEntryInfo.ce_suna = 16355 tcp->tcp_suna; 16356 tce.tcpConnEntryInfo.ce_rnxt = 16357 tcp->tcp_rnxt; 16358 tce.tcpConnEntryInfo.ce_rack = 16359 tcp->tcp_rack; 16360 } else { 16361 /* 16362 * Netstat, unfortunately, uses this to 16363 * get send/receive queue sizes. How 16364 * to fix? 16365 * Why not compute the difference only? 16366 */ 16367 tce.tcpConnEntryInfo.ce_snxt = 16368 tcp->tcp_snxt - tcp->tcp_suna; 16369 tce.tcpConnEntryInfo.ce_suna = 0; 16370 tce.tcpConnEntryInfo.ce_rnxt = 16371 tcp->tcp_rnxt - tcp->tcp_rack; 16372 tce.tcpConnEntryInfo.ce_rack = 0; 16373 } 16374 16375 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16376 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16377 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16378 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16379 tce.tcpConnEntryInfo.ce_state = 16380 tcp->tcp_state; 16381 16382 tce.tcpConnCreationProcess = 16383 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16384 tcp->tcp_cpid; 16385 tce.tcpConnCreationTime = tcp->tcp_open_time; 16386 16387 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16388 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16389 16390 mlp.tme_connidx = v4_conn_idx++; 16391 if (needattr) 16392 (void) snmp_append_data2( 16393 mp_attr_ctl->b_cont, 16394 &mp_attr_tail, (char *)&mlp, 16395 sizeof (mlp)); 16396 } 16397 } 16398 } 16399 16400 /* fixed length structure for IPv4 and IPv6 counters */ 16401 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16402 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16403 sizeof (mib2_tcp6ConnEntry_t)); 16404 /* synchronize 32- and 64-bit counters */ 16405 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16406 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16407 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16408 optp->level = MIB2_TCP; 16409 optp->name = 0; 16410 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16411 sizeof (tcps->tcps_mib)); 16412 optp->len = msgdsize(mpdata); 16413 qreply(q, mpctl); 16414 16415 /* table of connections... */ 16416 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16417 sizeof (struct T_optmgmt_ack)]; 16418 optp->level = MIB2_TCP; 16419 optp->name = MIB2_TCP_CONN; 16420 optp->len = msgdsize(mp_conn_ctl->b_cont); 16421 qreply(q, mp_conn_ctl); 16422 16423 /* table of MLP attributes... */ 16424 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16425 sizeof (struct T_optmgmt_ack)]; 16426 optp->level = MIB2_TCP; 16427 optp->name = EXPER_XPORT_MLP; 16428 optp->len = msgdsize(mp_attr_ctl->b_cont); 16429 if (optp->len == 0) 16430 freemsg(mp_attr_ctl); 16431 else 16432 qreply(q, mp_attr_ctl); 16433 16434 /* table of IPv6 connections... */ 16435 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16436 sizeof (struct T_optmgmt_ack)]; 16437 optp->level = MIB2_TCP6; 16438 optp->name = MIB2_TCP6_CONN; 16439 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16440 qreply(q, mp6_conn_ctl); 16441 16442 /* table of IPv6 MLP attributes... */ 16443 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16444 sizeof (struct T_optmgmt_ack)]; 16445 optp->level = MIB2_TCP6; 16446 optp->name = EXPER_XPORT_MLP; 16447 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16448 if (optp->len == 0) 16449 freemsg(mp6_attr_ctl); 16450 else 16451 qreply(q, mp6_attr_ctl); 16452 return (1); 16453 } 16454 16455 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16456 /* ARGSUSED */ 16457 int 16458 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16459 { 16460 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16461 16462 switch (level) { 16463 case MIB2_TCP: 16464 switch (name) { 16465 case 13: 16466 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16467 return (0); 16468 /* TODO: delete entry defined by tce */ 16469 return (1); 16470 default: 16471 return (0); 16472 } 16473 default: 16474 return (1); 16475 } 16476 } 16477 16478 /* Translate TCP state to MIB2 TCP state. */ 16479 static int 16480 tcp_snmp_state(tcp_t *tcp) 16481 { 16482 if (tcp == NULL) 16483 return (0); 16484 16485 switch (tcp->tcp_state) { 16486 case TCPS_CLOSED: 16487 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16488 case TCPS_BOUND: 16489 return (MIB2_TCP_closed); 16490 case TCPS_LISTEN: 16491 return (MIB2_TCP_listen); 16492 case TCPS_SYN_SENT: 16493 return (MIB2_TCP_synSent); 16494 case TCPS_SYN_RCVD: 16495 return (MIB2_TCP_synReceived); 16496 case TCPS_ESTABLISHED: 16497 return (MIB2_TCP_established); 16498 case TCPS_CLOSE_WAIT: 16499 return (MIB2_TCP_closeWait); 16500 case TCPS_FIN_WAIT_1: 16501 return (MIB2_TCP_finWait1); 16502 case TCPS_CLOSING: 16503 return (MIB2_TCP_closing); 16504 case TCPS_LAST_ACK: 16505 return (MIB2_TCP_lastAck); 16506 case TCPS_FIN_WAIT_2: 16507 return (MIB2_TCP_finWait2); 16508 case TCPS_TIME_WAIT: 16509 return (MIB2_TCP_timeWait); 16510 default: 16511 return (0); 16512 } 16513 } 16514 16515 static char tcp_report_header[] = 16516 "TCP " MI_COL_HDRPAD_STR 16517 "zone dest snxt suna " 16518 "swnd rnxt rack rwnd rto mss w sw rw t " 16519 "recent [lport,fport] state"; 16520 16521 /* 16522 * TCP status report triggered via the Named Dispatch mechanism. 16523 */ 16524 /* ARGSUSED */ 16525 static void 16526 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16527 cred_t *cr) 16528 { 16529 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16530 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16531 char cflag; 16532 in6_addr_t v6dst; 16533 char buf[80]; 16534 uint_t print_len, buf_len; 16535 16536 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16537 if (buf_len <= 0) 16538 return; 16539 16540 if (hashval >= 0) 16541 (void) sprintf(hash, "%03d ", hashval); 16542 else 16543 hash[0] = '\0'; 16544 16545 /* 16546 * Note that we use the remote address in the tcp_b structure. 16547 * This means that it will print out the real destination address, 16548 * not the next hop's address if source routing is used. This 16549 * avoid the confusion on the output because user may not 16550 * know that source routing is used for a connection. 16551 */ 16552 if (tcp->tcp_ipversion == IPV4_VERSION) { 16553 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16554 } else { 16555 v6dst = tcp->tcp_remote_v6; 16556 } 16557 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16558 /* 16559 * the ispriv checks are so that normal users cannot determine 16560 * sequence number information using NDD. 16561 */ 16562 16563 if (TCP_IS_DETACHED(tcp)) 16564 cflag = '*'; 16565 else 16566 cflag = ' '; 16567 print_len = snprintf((char *)mp->b_wptr, buf_len, 16568 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16569 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16570 hash, 16571 (void *)tcp, 16572 tcp->tcp_connp->conn_zoneid, 16573 addrbuf, 16574 (ispriv) ? tcp->tcp_snxt : 0, 16575 (ispriv) ? tcp->tcp_suna : 0, 16576 tcp->tcp_swnd, 16577 (ispriv) ? tcp->tcp_rnxt : 0, 16578 (ispriv) ? tcp->tcp_rack : 0, 16579 tcp->tcp_rwnd, 16580 tcp->tcp_rto, 16581 tcp->tcp_mss, 16582 tcp->tcp_snd_ws_ok, 16583 tcp->tcp_snd_ws, 16584 tcp->tcp_rcv_ws, 16585 tcp->tcp_snd_ts_ok, 16586 tcp->tcp_ts_recent, 16587 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16588 if (print_len < buf_len) { 16589 ((mblk_t *)mp)->b_wptr += print_len; 16590 } else { 16591 ((mblk_t *)mp)->b_wptr += buf_len; 16592 } 16593 } 16594 16595 /* 16596 * TCP status report (for listeners only) triggered via the Named Dispatch 16597 * mechanism. 16598 */ 16599 /* ARGSUSED */ 16600 static void 16601 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16602 { 16603 char addrbuf[INET6_ADDRSTRLEN]; 16604 in6_addr_t v6dst; 16605 uint_t print_len, buf_len; 16606 16607 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16608 if (buf_len <= 0) 16609 return; 16610 16611 if (tcp->tcp_ipversion == IPV4_VERSION) { 16612 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16613 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16614 } else { 16615 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16616 addrbuf, sizeof (addrbuf)); 16617 } 16618 print_len = snprintf((char *)mp->b_wptr, buf_len, 16619 "%03d " 16620 MI_COL_PTRFMT_STR 16621 "%d %s %05u %08u %d/%d/%d%c\n", 16622 hashval, (void *)tcp, 16623 tcp->tcp_connp->conn_zoneid, 16624 addrbuf, 16625 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16626 tcp->tcp_conn_req_seqnum, 16627 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16628 tcp->tcp_conn_req_max, 16629 tcp->tcp_syn_defense ? '*' : ' '); 16630 if (print_len < buf_len) { 16631 ((mblk_t *)mp)->b_wptr += print_len; 16632 } else { 16633 ((mblk_t *)mp)->b_wptr += buf_len; 16634 } 16635 } 16636 16637 /* TCP status report triggered via the Named Dispatch mechanism. */ 16638 /* ARGSUSED */ 16639 static int 16640 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16641 { 16642 tcp_t *tcp; 16643 int i; 16644 conn_t *connp; 16645 connf_t *connfp; 16646 zoneid_t zoneid; 16647 tcp_stack_t *tcps; 16648 ip_stack_t *ipst; 16649 16650 zoneid = Q_TO_CONN(q)->conn_zoneid; 16651 tcps = Q_TO_TCP(q)->tcp_tcps; 16652 16653 /* 16654 * Because of the ndd constraint, at most we can have 64K buffer 16655 * to put in all TCP info. So to be more efficient, just 16656 * allocate a 64K buffer here, assuming we need that large buffer. 16657 * This may be a problem as any user can read tcp_status. Therefore 16658 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16659 * This should be OK as normal users should not do this too often. 16660 */ 16661 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16662 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16663 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16664 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16665 return (0); 16666 } 16667 } 16668 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16669 /* The following may work even if we cannot get a large buf. */ 16670 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16671 return (0); 16672 } 16673 16674 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16675 16676 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16677 16678 ipst = tcps->tcps_netstack->netstack_ip; 16679 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16680 16681 connp = NULL; 16682 16683 while ((connp = 16684 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16685 tcp = connp->conn_tcp; 16686 if (zoneid != GLOBAL_ZONEID && 16687 zoneid != connp->conn_zoneid) 16688 continue; 16689 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16690 cr); 16691 } 16692 16693 } 16694 16695 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16696 return (0); 16697 } 16698 16699 /* TCP status report triggered via the Named Dispatch mechanism. */ 16700 /* ARGSUSED */ 16701 static int 16702 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16703 { 16704 tf_t *tbf; 16705 tcp_t *tcp; 16706 int i; 16707 zoneid_t zoneid; 16708 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16709 16710 zoneid = Q_TO_CONN(q)->conn_zoneid; 16711 16712 /* Refer to comments in tcp_status_report(). */ 16713 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16714 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16715 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16716 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16717 return (0); 16718 } 16719 } 16720 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16721 /* The following may work even if we cannot get a large buf. */ 16722 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16723 return (0); 16724 } 16725 16726 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16727 16728 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16729 tbf = &tcps->tcps_bind_fanout[i]; 16730 mutex_enter(&tbf->tf_lock); 16731 for (tcp = tbf->tf_tcp; tcp != NULL; 16732 tcp = tcp->tcp_bind_hash) { 16733 if (zoneid != GLOBAL_ZONEID && 16734 zoneid != tcp->tcp_connp->conn_zoneid) 16735 continue; 16736 CONN_INC_REF(tcp->tcp_connp); 16737 tcp_report_item(mp->b_cont, tcp, i, 16738 Q_TO_TCP(q), cr); 16739 CONN_DEC_REF(tcp->tcp_connp); 16740 } 16741 mutex_exit(&tbf->tf_lock); 16742 } 16743 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16744 return (0); 16745 } 16746 16747 /* TCP status report triggered via the Named Dispatch mechanism. */ 16748 /* ARGSUSED */ 16749 static int 16750 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16751 { 16752 connf_t *connfp; 16753 conn_t *connp; 16754 tcp_t *tcp; 16755 int i; 16756 zoneid_t zoneid; 16757 tcp_stack_t *tcps; 16758 ip_stack_t *ipst; 16759 16760 zoneid = Q_TO_CONN(q)->conn_zoneid; 16761 tcps = Q_TO_TCP(q)->tcp_tcps; 16762 16763 /* Refer to comments in tcp_status_report(). */ 16764 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16765 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16766 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16767 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16768 return (0); 16769 } 16770 } 16771 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16772 /* The following may work even if we cannot get a large buf. */ 16773 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16774 return (0); 16775 } 16776 16777 (void) mi_mpprintf(mp, 16778 " TCP " MI_COL_HDRPAD_STR 16779 "zone IP addr port seqnum backlog (q0/q/max)"); 16780 16781 ipst = tcps->tcps_netstack->netstack_ip; 16782 16783 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16784 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16785 connp = NULL; 16786 while ((connp = 16787 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16788 tcp = connp->conn_tcp; 16789 if (zoneid != GLOBAL_ZONEID && 16790 zoneid != connp->conn_zoneid) 16791 continue; 16792 tcp_report_listener(mp->b_cont, tcp, i); 16793 } 16794 } 16795 16796 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16797 return (0); 16798 } 16799 16800 /* TCP status report triggered via the Named Dispatch mechanism. */ 16801 /* ARGSUSED */ 16802 static int 16803 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16804 { 16805 connf_t *connfp; 16806 conn_t *connp; 16807 tcp_t *tcp; 16808 int i; 16809 zoneid_t zoneid; 16810 tcp_stack_t *tcps; 16811 ip_stack_t *ipst; 16812 16813 zoneid = Q_TO_CONN(q)->conn_zoneid; 16814 tcps = Q_TO_TCP(q)->tcp_tcps; 16815 ipst = tcps->tcps_netstack->netstack_ip; 16816 16817 /* Refer to comments in tcp_status_report(). */ 16818 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16819 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16820 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16821 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16822 return (0); 16823 } 16824 } 16825 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16826 /* The following may work even if we cannot get a large buf. */ 16827 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16828 return (0); 16829 } 16830 16831 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16832 ipst->ips_ipcl_conn_fanout_size); 16833 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16834 16835 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16836 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16837 connp = NULL; 16838 while ((connp = 16839 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16840 tcp = connp->conn_tcp; 16841 if (zoneid != GLOBAL_ZONEID && 16842 zoneid != connp->conn_zoneid) 16843 continue; 16844 tcp_report_item(mp->b_cont, tcp, i, 16845 Q_TO_TCP(q), cr); 16846 } 16847 } 16848 16849 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16850 return (0); 16851 } 16852 16853 /* TCP status report triggered via the Named Dispatch mechanism. */ 16854 /* ARGSUSED */ 16855 static int 16856 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16857 { 16858 tf_t *tf; 16859 tcp_t *tcp; 16860 int i; 16861 zoneid_t zoneid; 16862 tcp_stack_t *tcps; 16863 16864 zoneid = Q_TO_CONN(q)->conn_zoneid; 16865 tcps = Q_TO_TCP(q)->tcp_tcps; 16866 16867 /* Refer to comments in tcp_status_report(). */ 16868 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16869 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16870 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16871 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16872 return (0); 16873 } 16874 } 16875 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16876 /* The following may work even if we cannot get a large buf. */ 16877 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16878 return (0); 16879 } 16880 16881 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16882 16883 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16884 tf = &tcps->tcps_acceptor_fanout[i]; 16885 mutex_enter(&tf->tf_lock); 16886 for (tcp = tf->tf_tcp; tcp != NULL; 16887 tcp = tcp->tcp_acceptor_hash) { 16888 if (zoneid != GLOBAL_ZONEID && 16889 zoneid != tcp->tcp_connp->conn_zoneid) 16890 continue; 16891 tcp_report_item(mp->b_cont, tcp, i, 16892 Q_TO_TCP(q), cr); 16893 } 16894 mutex_exit(&tf->tf_lock); 16895 } 16896 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16897 return (0); 16898 } 16899 16900 /* 16901 * tcp_timer is the timer service routine. It handles the retransmission, 16902 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16903 * from the state of the tcp instance what kind of action needs to be done 16904 * at the time it is called. 16905 */ 16906 static void 16907 tcp_timer(void *arg) 16908 { 16909 mblk_t *mp; 16910 clock_t first_threshold; 16911 clock_t second_threshold; 16912 clock_t ms; 16913 uint32_t mss; 16914 conn_t *connp = (conn_t *)arg; 16915 tcp_t *tcp = connp->conn_tcp; 16916 tcp_stack_t *tcps = tcp->tcp_tcps; 16917 16918 tcp->tcp_timer_tid = 0; 16919 16920 if (tcp->tcp_fused) 16921 return; 16922 16923 first_threshold = tcp->tcp_first_timer_threshold; 16924 second_threshold = tcp->tcp_second_timer_threshold; 16925 switch (tcp->tcp_state) { 16926 case TCPS_IDLE: 16927 case TCPS_BOUND: 16928 case TCPS_LISTEN: 16929 return; 16930 case TCPS_SYN_RCVD: { 16931 tcp_t *listener = tcp->tcp_listener; 16932 16933 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16934 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16935 /* it's our first timeout */ 16936 tcp->tcp_syn_rcvd_timeout = 1; 16937 mutex_enter(&listener->tcp_eager_lock); 16938 listener->tcp_syn_rcvd_timeout++; 16939 if (!tcp->tcp_dontdrop && tcp->tcp_closemp_used == 0) { 16940 /* 16941 * Make this eager available for drop if we 16942 * need to drop one to accomodate a new 16943 * incoming SYN request. 16944 */ 16945 MAKE_DROPPABLE(listener, tcp); 16946 } 16947 if (!listener->tcp_syn_defense && 16948 (listener->tcp_syn_rcvd_timeout > 16949 (tcps->tcps_conn_req_max_q0 >> 2)) && 16950 (tcps->tcps_conn_req_max_q0 > 200)) { 16951 /* We may be under attack. Put on a defense. */ 16952 listener->tcp_syn_defense = B_TRUE; 16953 cmn_err(CE_WARN, "High TCP connect timeout " 16954 "rate! System (port %d) may be under a " 16955 "SYN flood attack!", 16956 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16957 16958 listener->tcp_ip_addr_cache = kmem_zalloc( 16959 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16960 KM_NOSLEEP); 16961 } 16962 mutex_exit(&listener->tcp_eager_lock); 16963 } else if (listener != NULL) { 16964 mutex_enter(&listener->tcp_eager_lock); 16965 tcp->tcp_syn_rcvd_timeout++; 16966 if (tcp->tcp_syn_rcvd_timeout > 1 && 16967 tcp->tcp_closemp_used == 0) { 16968 /* 16969 * This is our second timeout. Put the tcp in 16970 * the list of droppable eagers to allow it to 16971 * be dropped, if needed. We don't check 16972 * whether tcp_dontdrop is set or not to 16973 * protect ourselve from a SYN attack where a 16974 * remote host can spoof itself as one of the 16975 * good IP source and continue to hold 16976 * resources too long. 16977 */ 16978 MAKE_DROPPABLE(listener, tcp); 16979 } 16980 mutex_exit(&listener->tcp_eager_lock); 16981 } 16982 } 16983 /* FALLTHRU */ 16984 case TCPS_SYN_SENT: 16985 first_threshold = tcp->tcp_first_ctimer_threshold; 16986 second_threshold = tcp->tcp_second_ctimer_threshold; 16987 break; 16988 case TCPS_ESTABLISHED: 16989 case TCPS_FIN_WAIT_1: 16990 case TCPS_CLOSING: 16991 case TCPS_CLOSE_WAIT: 16992 case TCPS_LAST_ACK: 16993 /* If we have data to rexmit */ 16994 if (tcp->tcp_suna != tcp->tcp_snxt) { 16995 clock_t time_to_wait; 16996 16997 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 16998 if (!tcp->tcp_xmit_head) 16999 break; 17000 time_to_wait = lbolt - 17001 (clock_t)tcp->tcp_xmit_head->b_prev; 17002 time_to_wait = tcp->tcp_rto - 17003 TICK_TO_MSEC(time_to_wait); 17004 /* 17005 * If the timer fires too early, 1 clock tick earlier, 17006 * restart the timer. 17007 */ 17008 if (time_to_wait > msec_per_tick) { 17009 TCP_STAT(tcps, tcp_timer_fire_early); 17010 TCP_TIMER_RESTART(tcp, time_to_wait); 17011 return; 17012 } 17013 /* 17014 * When we probe zero windows, we force the swnd open. 17015 * If our peer acks with a closed window swnd will be 17016 * set to zero by tcp_rput(). As long as we are 17017 * receiving acks tcp_rput will 17018 * reset 'tcp_ms_we_have_waited' so as not to trip the 17019 * first and second interval actions. NOTE: the timer 17020 * interval is allowed to continue its exponential 17021 * backoff. 17022 */ 17023 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17024 if (tcp->tcp_debug) { 17025 (void) strlog(TCP_MOD_ID, 0, 1, 17026 SL_TRACE, "tcp_timer: zero win"); 17027 } 17028 } else { 17029 /* 17030 * After retransmission, we need to do 17031 * slow start. Set the ssthresh to one 17032 * half of current effective window and 17033 * cwnd to one MSS. Also reset 17034 * tcp_cwnd_cnt. 17035 * 17036 * Note that if tcp_ssthresh is reduced because 17037 * of ECN, do not reduce it again unless it is 17038 * already one window of data away (tcp_cwr 17039 * should then be cleared) or this is a 17040 * timeout for a retransmitted segment. 17041 */ 17042 uint32_t npkt; 17043 17044 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17045 npkt = ((tcp->tcp_timer_backoff ? 17046 tcp->tcp_cwnd_ssthresh : 17047 tcp->tcp_snxt - 17048 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17049 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17050 tcp->tcp_mss; 17051 } 17052 tcp->tcp_cwnd = tcp->tcp_mss; 17053 tcp->tcp_cwnd_cnt = 0; 17054 if (tcp->tcp_ecn_ok) { 17055 tcp->tcp_cwr = B_TRUE; 17056 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17057 tcp->tcp_ecn_cwr_sent = B_FALSE; 17058 } 17059 } 17060 break; 17061 } 17062 /* 17063 * We have something to send yet we cannot send. The 17064 * reason can be: 17065 * 17066 * 1. Zero send window: we need to do zero window probe. 17067 * 2. Zero cwnd: because of ECN, we need to "clock out 17068 * segments. 17069 * 3. SWS avoidance: receiver may have shrunk window, 17070 * reset our knowledge. 17071 * 17072 * Note that condition 2 can happen with either 1 or 17073 * 3. But 1 and 3 are exclusive. 17074 */ 17075 if (tcp->tcp_unsent != 0) { 17076 if (tcp->tcp_cwnd == 0) { 17077 /* 17078 * Set tcp_cwnd to 1 MSS so that a 17079 * new segment can be sent out. We 17080 * are "clocking out" new data when 17081 * the network is really congested. 17082 */ 17083 ASSERT(tcp->tcp_ecn_ok); 17084 tcp->tcp_cwnd = tcp->tcp_mss; 17085 } 17086 if (tcp->tcp_swnd == 0) { 17087 /* Extend window for zero window probe */ 17088 tcp->tcp_swnd++; 17089 tcp->tcp_zero_win_probe = B_TRUE; 17090 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17091 } else { 17092 /* 17093 * Handle timeout from sender SWS avoidance. 17094 * Reset our knowledge of the max send window 17095 * since the receiver might have reduced its 17096 * receive buffer. Avoid setting tcp_max_swnd 17097 * to one since that will essentially disable 17098 * the SWS checks. 17099 * 17100 * Note that since we don't have a SWS 17101 * state variable, if the timeout is set 17102 * for ECN but not for SWS, this 17103 * code will also be executed. This is 17104 * fine as tcp_max_swnd is updated 17105 * constantly and it will not affect 17106 * anything. 17107 */ 17108 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17109 } 17110 tcp_wput_data(tcp, NULL, B_FALSE); 17111 return; 17112 } 17113 /* Is there a FIN that needs to be to re retransmitted? */ 17114 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17115 !tcp->tcp_fin_acked) 17116 break; 17117 /* Nothing to do, return without restarting timer. */ 17118 TCP_STAT(tcps, tcp_timer_fire_miss); 17119 return; 17120 case TCPS_FIN_WAIT_2: 17121 /* 17122 * User closed the TCP endpoint and peer ACK'ed our FIN. 17123 * We waited some time for for peer's FIN, but it hasn't 17124 * arrived. We flush the connection now to avoid 17125 * case where the peer has rebooted. 17126 */ 17127 if (TCP_IS_DETACHED(tcp)) { 17128 (void) tcp_clean_death(tcp, 0, 23); 17129 } else { 17130 TCP_TIMER_RESTART(tcp, 17131 tcps->tcps_fin_wait_2_flush_interval); 17132 } 17133 return; 17134 case TCPS_TIME_WAIT: 17135 (void) tcp_clean_death(tcp, 0, 24); 17136 return; 17137 default: 17138 if (tcp->tcp_debug) { 17139 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17140 "tcp_timer: strange state (%d) %s", 17141 tcp->tcp_state, tcp_display(tcp, NULL, 17142 DISP_PORT_ONLY)); 17143 } 17144 return; 17145 } 17146 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17147 /* 17148 * For zero window probe, we need to send indefinitely, 17149 * unless we have not heard from the other side for some 17150 * time... 17151 */ 17152 if ((tcp->tcp_zero_win_probe == 0) || 17153 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17154 second_threshold)) { 17155 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17156 /* 17157 * If TCP is in SYN_RCVD state, send back a 17158 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17159 * should be zero in TCPS_SYN_RCVD state. 17160 */ 17161 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17162 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17163 "in SYN_RCVD", 17164 tcp, tcp->tcp_snxt, 17165 tcp->tcp_rnxt, TH_RST | TH_ACK); 17166 } 17167 (void) tcp_clean_death(tcp, 17168 tcp->tcp_client_errno ? 17169 tcp->tcp_client_errno : ETIMEDOUT, 25); 17170 return; 17171 } else { 17172 /* 17173 * Set tcp_ms_we_have_waited to second_threshold 17174 * so that in next timeout, we will do the above 17175 * check (lbolt - tcp_last_recv_time). This is 17176 * also to avoid overflow. 17177 * 17178 * We don't need to decrement tcp_timer_backoff 17179 * to avoid overflow because it will be decremented 17180 * later if new timeout value is greater than 17181 * tcp_rexmit_interval_max. In the case when 17182 * tcp_rexmit_interval_max is greater than 17183 * second_threshold, it means that we will wait 17184 * longer than second_threshold to send the next 17185 * window probe. 17186 */ 17187 tcp->tcp_ms_we_have_waited = second_threshold; 17188 } 17189 } else if (ms > first_threshold) { 17190 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17191 tcp->tcp_xmit_head != NULL) { 17192 tcp->tcp_xmit_head = 17193 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17194 } 17195 /* 17196 * We have been retransmitting for too long... The RTT 17197 * we calculated is probably incorrect. Reinitialize it. 17198 * Need to compensate for 0 tcp_rtt_sa. Reset 17199 * tcp_rtt_update so that we won't accidentally cache a 17200 * bad value. But only do this if this is not a zero 17201 * window probe. 17202 */ 17203 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17204 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17205 (tcp->tcp_rtt_sa >> 5); 17206 tcp->tcp_rtt_sa = 0; 17207 tcp_ip_notify(tcp); 17208 tcp->tcp_rtt_update = 0; 17209 } 17210 } 17211 tcp->tcp_timer_backoff++; 17212 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17213 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17214 tcps->tcps_rexmit_interval_min) { 17215 /* 17216 * This means the original RTO is tcp_rexmit_interval_min. 17217 * So we will use tcp_rexmit_interval_min as the RTO value 17218 * and do the backoff. 17219 */ 17220 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17221 } else { 17222 ms <<= tcp->tcp_timer_backoff; 17223 } 17224 if (ms > tcps->tcps_rexmit_interval_max) { 17225 ms = tcps->tcps_rexmit_interval_max; 17226 /* 17227 * ms is at max, decrement tcp_timer_backoff to avoid 17228 * overflow. 17229 */ 17230 tcp->tcp_timer_backoff--; 17231 } 17232 tcp->tcp_ms_we_have_waited += ms; 17233 if (tcp->tcp_zero_win_probe == 0) { 17234 tcp->tcp_rto = ms; 17235 } 17236 TCP_TIMER_RESTART(tcp, ms); 17237 /* 17238 * This is after a timeout and tcp_rto is backed off. Set 17239 * tcp_set_timer to 1 so that next time RTO is updated, we will 17240 * restart the timer with a correct value. 17241 */ 17242 tcp->tcp_set_timer = 1; 17243 mss = tcp->tcp_snxt - tcp->tcp_suna; 17244 if (mss > tcp->tcp_mss) 17245 mss = tcp->tcp_mss; 17246 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17247 mss = tcp->tcp_swnd; 17248 17249 if ((mp = tcp->tcp_xmit_head) != NULL) 17250 mp->b_prev = (mblk_t *)lbolt; 17251 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17252 B_TRUE); 17253 17254 /* 17255 * When slow start after retransmission begins, start with 17256 * this seq no. tcp_rexmit_max marks the end of special slow 17257 * start phase. tcp_snd_burst controls how many segments 17258 * can be sent because of an ack. 17259 */ 17260 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17261 tcp->tcp_snd_burst = TCP_CWND_SS; 17262 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17263 (tcp->tcp_unsent == 0)) { 17264 tcp->tcp_rexmit_max = tcp->tcp_fss; 17265 } else { 17266 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17267 } 17268 tcp->tcp_rexmit = B_TRUE; 17269 tcp->tcp_dupack_cnt = 0; 17270 17271 /* 17272 * Remove all rexmit SACK blk to start from fresh. 17273 */ 17274 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17275 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17276 tcp->tcp_num_notsack_blk = 0; 17277 tcp->tcp_cnt_notsack_list = 0; 17278 } 17279 if (mp == NULL) { 17280 return; 17281 } 17282 /* Attach credentials to retransmitted initial SYNs. */ 17283 if (tcp->tcp_state == TCPS_SYN_SENT) { 17284 mblk_setcred(mp, tcp->tcp_cred); 17285 DB_CPID(mp) = tcp->tcp_cpid; 17286 } 17287 17288 tcp->tcp_csuna = tcp->tcp_snxt; 17289 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17290 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17291 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 17292 tcp_send_data(tcp, tcp->tcp_wq, mp); 17293 17294 } 17295 17296 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17297 static void 17298 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17299 { 17300 conn_t *connp; 17301 17302 switch (tcp->tcp_state) { 17303 case TCPS_BOUND: 17304 case TCPS_LISTEN: 17305 break; 17306 default: 17307 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17308 return; 17309 } 17310 17311 /* 17312 * Need to clean up all the eagers since after the unbind, segments 17313 * will no longer be delivered to this listener stream. 17314 */ 17315 mutex_enter(&tcp->tcp_eager_lock); 17316 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17317 tcp_eager_cleanup(tcp, 0); 17318 } 17319 mutex_exit(&tcp->tcp_eager_lock); 17320 17321 if (tcp->tcp_ipversion == IPV4_VERSION) { 17322 tcp->tcp_ipha->ipha_src = 0; 17323 } else { 17324 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17325 } 17326 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17327 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17328 tcp_bind_hash_remove(tcp); 17329 tcp->tcp_state = TCPS_IDLE; 17330 tcp->tcp_mdt = B_FALSE; 17331 /* Send M_FLUSH according to TPI */ 17332 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17333 connp = tcp->tcp_connp; 17334 connp->conn_mdt_ok = B_FALSE; 17335 ipcl_hash_remove(connp); 17336 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17337 mp = mi_tpi_ok_ack_alloc(mp); 17338 putnext(tcp->tcp_rq, mp); 17339 } 17340 17341 /* 17342 * Don't let port fall into the privileged range. 17343 * Since the extra privileged ports can be arbitrary we also 17344 * ensure that we exclude those from consideration. 17345 * tcp_g_epriv_ports is not sorted thus we loop over it until 17346 * there are no changes. 17347 * 17348 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17349 * but instead the code relies on: 17350 * - the fact that the address of the array and its size never changes 17351 * - the atomic assignment of the elements of the array 17352 * 17353 * Returns 0 if there are no more ports available. 17354 * 17355 * TS note: skip multilevel ports. 17356 */ 17357 static in_port_t 17358 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17359 { 17360 int i; 17361 boolean_t restart = B_FALSE; 17362 tcp_stack_t *tcps = tcp->tcp_tcps; 17363 17364 if (random && tcp_random_anon_port != 0) { 17365 (void) random_get_pseudo_bytes((uint8_t *)&port, 17366 sizeof (in_port_t)); 17367 /* 17368 * Unless changed by a sys admin, the smallest anon port 17369 * is 32768 and the largest anon port is 65535. It is 17370 * very likely (50%) for the random port to be smaller 17371 * than the smallest anon port. When that happens, 17372 * add port % (anon port range) to the smallest anon 17373 * port to get the random port. It should fall into the 17374 * valid anon port range. 17375 */ 17376 if (port < tcps->tcps_smallest_anon_port) { 17377 port = tcps->tcps_smallest_anon_port + 17378 port % (tcps->tcps_largest_anon_port - 17379 tcps->tcps_smallest_anon_port); 17380 } 17381 } 17382 17383 retry: 17384 if (port < tcps->tcps_smallest_anon_port) 17385 port = (in_port_t)tcps->tcps_smallest_anon_port; 17386 17387 if (port > tcps->tcps_largest_anon_port) { 17388 if (restart) 17389 return (0); 17390 restart = B_TRUE; 17391 port = (in_port_t)tcps->tcps_smallest_anon_port; 17392 } 17393 17394 if (port < tcps->tcps_smallest_nonpriv_port) 17395 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17396 17397 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17398 if (port == tcps->tcps_g_epriv_ports[i]) { 17399 port++; 17400 /* 17401 * Make sure whether the port is in the 17402 * valid range. 17403 */ 17404 goto retry; 17405 } 17406 } 17407 if (is_system_labeled() && 17408 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17409 IPPROTO_TCP, B_TRUE)) != 0) { 17410 port = i; 17411 goto retry; 17412 } 17413 return (port); 17414 } 17415 17416 /* 17417 * Return the next anonymous port in the privileged port range for 17418 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17419 * downwards. This is the same behavior as documented in the userland 17420 * library call rresvport(3N). 17421 * 17422 * TS note: skip multilevel ports. 17423 */ 17424 static in_port_t 17425 tcp_get_next_priv_port(const tcp_t *tcp) 17426 { 17427 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17428 in_port_t nextport; 17429 boolean_t restart = B_FALSE; 17430 tcp_stack_t *tcps = tcp->tcp_tcps; 17431 retry: 17432 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17433 next_priv_port >= IPPORT_RESERVED) { 17434 next_priv_port = IPPORT_RESERVED - 1; 17435 if (restart) 17436 return (0); 17437 restart = B_TRUE; 17438 } 17439 if (is_system_labeled() && 17440 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17441 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17442 next_priv_port = nextport; 17443 goto retry; 17444 } 17445 return (next_priv_port--); 17446 } 17447 17448 /* The write side r/w procedure. */ 17449 17450 #if CCS_STATS 17451 struct { 17452 struct { 17453 int64_t count, bytes; 17454 } tot, hit; 17455 } wrw_stats; 17456 #endif 17457 17458 /* 17459 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17460 * messages. 17461 */ 17462 /* ARGSUSED */ 17463 static void 17464 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17465 { 17466 conn_t *connp = (conn_t *)arg; 17467 tcp_t *tcp = connp->conn_tcp; 17468 queue_t *q = tcp->tcp_wq; 17469 17470 ASSERT(DB_TYPE(mp) != M_IOCTL); 17471 /* 17472 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17473 * Once the close starts, streamhead and sockfs will not let any data 17474 * packets come down (close ensures that there are no threads using the 17475 * queue and no new threads will come down) but since qprocsoff() 17476 * hasn't happened yet, a M_FLUSH or some non data message might 17477 * get reflected back (in response to our own FLUSHRW) and get 17478 * processed after tcp_close() is done. The conn would still be valid 17479 * because a ref would have added but we need to check the state 17480 * before actually processing the packet. 17481 */ 17482 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17483 freemsg(mp); 17484 return; 17485 } 17486 17487 switch (DB_TYPE(mp)) { 17488 case M_IOCDATA: 17489 tcp_wput_iocdata(tcp, mp); 17490 break; 17491 case M_FLUSH: 17492 tcp_wput_flush(tcp, mp); 17493 break; 17494 default: 17495 CALL_IP_WPUT(connp, q, mp); 17496 break; 17497 } 17498 } 17499 17500 /* 17501 * The TCP fast path write put procedure. 17502 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17503 */ 17504 /* ARGSUSED */ 17505 void 17506 tcp_output(void *arg, mblk_t *mp, void *arg2) 17507 { 17508 int len; 17509 int hdrlen; 17510 int plen; 17511 mblk_t *mp1; 17512 uchar_t *rptr; 17513 uint32_t snxt; 17514 tcph_t *tcph; 17515 struct datab *db; 17516 uint32_t suna; 17517 uint32_t mss; 17518 ipaddr_t *dst; 17519 ipaddr_t *src; 17520 uint32_t sum; 17521 int usable; 17522 conn_t *connp = (conn_t *)arg; 17523 tcp_t *tcp = connp->conn_tcp; 17524 uint32_t msize; 17525 tcp_stack_t *tcps = tcp->tcp_tcps; 17526 17527 /* 17528 * Try and ASSERT the minimum possible references on the 17529 * conn early enough. Since we are executing on write side, 17530 * the connection is obviously not detached and that means 17531 * there is a ref each for TCP and IP. Since we are behind 17532 * the squeue, the minimum references needed are 3. If the 17533 * conn is in classifier hash list, there should be an 17534 * extra ref for that (we check both the possibilities). 17535 */ 17536 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17537 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17538 17539 ASSERT(DB_TYPE(mp) == M_DATA); 17540 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17541 17542 mutex_enter(&tcp->tcp_non_sq_lock); 17543 tcp->tcp_squeue_bytes -= msize; 17544 mutex_exit(&tcp->tcp_non_sq_lock); 17545 17546 /* Bypass tcp protocol for fused tcp loopback */ 17547 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17548 return; 17549 17550 mss = tcp->tcp_mss; 17551 if (tcp->tcp_xmit_zc_clean) 17552 mp = tcp_zcopy_backoff(tcp, mp, 0); 17553 17554 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17555 len = (int)(mp->b_wptr - mp->b_rptr); 17556 17557 /* 17558 * Criteria for fast path: 17559 * 17560 * 1. no unsent data 17561 * 2. single mblk in request 17562 * 3. connection established 17563 * 4. data in mblk 17564 * 5. len <= mss 17565 * 6. no tcp_valid bits 17566 */ 17567 if ((tcp->tcp_unsent != 0) || 17568 (tcp->tcp_cork) || 17569 (mp->b_cont != NULL) || 17570 (tcp->tcp_state != TCPS_ESTABLISHED) || 17571 (len == 0) || 17572 (len > mss) || 17573 (tcp->tcp_valid_bits != 0)) { 17574 tcp_wput_data(tcp, mp, B_FALSE); 17575 return; 17576 } 17577 17578 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17579 ASSERT(tcp->tcp_fin_sent == 0); 17580 17581 /* queue new packet onto retransmission queue */ 17582 if (tcp->tcp_xmit_head == NULL) { 17583 tcp->tcp_xmit_head = mp; 17584 } else { 17585 tcp->tcp_xmit_last->b_cont = mp; 17586 } 17587 tcp->tcp_xmit_last = mp; 17588 tcp->tcp_xmit_tail = mp; 17589 17590 /* find out how much we can send */ 17591 /* BEGIN CSTYLED */ 17592 /* 17593 * un-acked usable 17594 * |--------------|-----------------| 17595 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17596 */ 17597 /* END CSTYLED */ 17598 17599 /* start sending from tcp_snxt */ 17600 snxt = tcp->tcp_snxt; 17601 17602 /* 17603 * Check to see if this connection has been idled for some 17604 * time and no ACK is expected. If it is, we need to slow 17605 * start again to get back the connection's "self-clock" as 17606 * described in VJ's paper. 17607 * 17608 * Refer to the comment in tcp_mss_set() for the calculation 17609 * of tcp_cwnd after idle. 17610 */ 17611 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17612 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17613 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17614 } 17615 17616 usable = tcp->tcp_swnd; /* tcp window size */ 17617 if (usable > tcp->tcp_cwnd) 17618 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17619 usable -= snxt; /* subtract stuff already sent */ 17620 suna = tcp->tcp_suna; 17621 usable += suna; 17622 /* usable can be < 0 if the congestion window is smaller */ 17623 if (len > usable) { 17624 /* Can't send complete M_DATA in one shot */ 17625 goto slow; 17626 } 17627 17628 mutex_enter(&tcp->tcp_non_sq_lock); 17629 if (tcp->tcp_flow_stopped && 17630 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17631 tcp_clrqfull(tcp); 17632 } 17633 mutex_exit(&tcp->tcp_non_sq_lock); 17634 17635 /* 17636 * determine if anything to send (Nagle). 17637 * 17638 * 1. len < tcp_mss (i.e. small) 17639 * 2. unacknowledged data present 17640 * 3. len < nagle limit 17641 * 4. last packet sent < nagle limit (previous packet sent) 17642 */ 17643 if ((len < mss) && (snxt != suna) && 17644 (len < (int)tcp->tcp_naglim) && 17645 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17646 /* 17647 * This was the first unsent packet and normally 17648 * mss < xmit_hiwater so there is no need to worry 17649 * about flow control. The next packet will go 17650 * through the flow control check in tcp_wput_data(). 17651 */ 17652 /* leftover work from above */ 17653 tcp->tcp_unsent = len; 17654 tcp->tcp_xmit_tail_unsent = len; 17655 17656 return; 17657 } 17658 17659 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17660 17661 if (snxt == suna) { 17662 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17663 } 17664 17665 /* we have always sent something */ 17666 tcp->tcp_rack_cnt = 0; 17667 17668 tcp->tcp_snxt = snxt + len; 17669 tcp->tcp_rack = tcp->tcp_rnxt; 17670 17671 if ((mp1 = dupb(mp)) == 0) 17672 goto no_memory; 17673 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17674 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17675 17676 /* adjust tcp header information */ 17677 tcph = tcp->tcp_tcph; 17678 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17679 17680 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17681 sum = (sum >> 16) + (sum & 0xFFFF); 17682 U16_TO_ABE16(sum, tcph->th_sum); 17683 17684 U32_TO_ABE32(snxt, tcph->th_seq); 17685 17686 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17687 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17688 BUMP_LOCAL(tcp->tcp_obsegs); 17689 17690 /* Update the latest receive window size in TCP header. */ 17691 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17692 tcph->th_win); 17693 17694 tcp->tcp_last_sent_len = (ushort_t)len; 17695 17696 plen = len + tcp->tcp_hdr_len; 17697 17698 if (tcp->tcp_ipversion == IPV4_VERSION) { 17699 tcp->tcp_ipha->ipha_length = htons(plen); 17700 } else { 17701 tcp->tcp_ip6h->ip6_plen = htons(plen - 17702 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17703 } 17704 17705 /* see if we need to allocate a mblk for the headers */ 17706 hdrlen = tcp->tcp_hdr_len; 17707 rptr = mp1->b_rptr - hdrlen; 17708 db = mp1->b_datap; 17709 if ((db->db_ref != 2) || rptr < db->db_base || 17710 (!OK_32PTR(rptr))) { 17711 /* NOTE: we assume allocb returns an OK_32PTR */ 17712 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17713 tcps->tcps_wroff_xtra, BPRI_MED); 17714 if (!mp) { 17715 freemsg(mp1); 17716 goto no_memory; 17717 } 17718 mp->b_cont = mp1; 17719 mp1 = mp; 17720 /* Leave room for Link Level header */ 17721 /* hdrlen = tcp->tcp_hdr_len; */ 17722 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17723 mp1->b_wptr = &rptr[hdrlen]; 17724 } 17725 mp1->b_rptr = rptr; 17726 17727 /* Fill in the timestamp option. */ 17728 if (tcp->tcp_snd_ts_ok) { 17729 U32_TO_BE32((uint32_t)lbolt, 17730 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17731 U32_TO_BE32(tcp->tcp_ts_recent, 17732 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17733 } else { 17734 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17735 } 17736 17737 /* copy header into outgoing packet */ 17738 dst = (ipaddr_t *)rptr; 17739 src = (ipaddr_t *)tcp->tcp_iphc; 17740 dst[0] = src[0]; 17741 dst[1] = src[1]; 17742 dst[2] = src[2]; 17743 dst[3] = src[3]; 17744 dst[4] = src[4]; 17745 dst[5] = src[5]; 17746 dst[6] = src[6]; 17747 dst[7] = src[7]; 17748 dst[8] = src[8]; 17749 dst[9] = src[9]; 17750 if (hdrlen -= 40) { 17751 hdrlen >>= 2; 17752 dst += 10; 17753 src += 10; 17754 do { 17755 *dst++ = *src++; 17756 } while (--hdrlen); 17757 } 17758 17759 /* 17760 * Set the ECN info in the TCP header. Note that this 17761 * is not the template header. 17762 */ 17763 if (tcp->tcp_ecn_ok) { 17764 SET_ECT(tcp, rptr); 17765 17766 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17767 if (tcp->tcp_ecn_echo_on) 17768 tcph->th_flags[0] |= TH_ECE; 17769 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17770 tcph->th_flags[0] |= TH_CWR; 17771 tcp->tcp_ecn_cwr_sent = B_TRUE; 17772 } 17773 } 17774 17775 if (tcp->tcp_ip_forward_progress) { 17776 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17777 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17778 tcp->tcp_ip_forward_progress = B_FALSE; 17779 } 17780 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17781 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17782 return; 17783 17784 /* 17785 * If we ran out of memory, we pretend to have sent the packet 17786 * and that it was lost on the wire. 17787 */ 17788 no_memory: 17789 return; 17790 17791 slow: 17792 /* leftover work from above */ 17793 tcp->tcp_unsent = len; 17794 tcp->tcp_xmit_tail_unsent = len; 17795 tcp_wput_data(tcp, NULL, B_FALSE); 17796 } 17797 17798 /* 17799 * The function called through squeue to get behind eager's perimeter to 17800 * finish the accept processing. 17801 */ 17802 /* ARGSUSED */ 17803 void 17804 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17805 { 17806 conn_t *connp = (conn_t *)arg; 17807 tcp_t *tcp = connp->conn_tcp; 17808 queue_t *q = tcp->tcp_rq; 17809 mblk_t *mp1; 17810 mblk_t *stropt_mp = mp; 17811 struct stroptions *stropt; 17812 uint_t thwin; 17813 tcp_stack_t *tcps = tcp->tcp_tcps; 17814 17815 /* 17816 * Drop the eager's ref on the listener, that was placed when 17817 * this eager began life in tcp_conn_request. 17818 */ 17819 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17820 17821 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17822 /* 17823 * Someone blewoff the eager before we could finish 17824 * the accept. 17825 * 17826 * The only reason eager exists it because we put in 17827 * a ref on it when conn ind went up. We need to send 17828 * a disconnect indication up while the last reference 17829 * on the eager will be dropped by the squeue when we 17830 * return. 17831 */ 17832 ASSERT(tcp->tcp_listener == NULL); 17833 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17834 struct T_discon_ind *tdi; 17835 17836 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17837 /* 17838 * Let us reuse the incoming mblk to avoid memory 17839 * allocation failure problems. We know that the 17840 * size of the incoming mblk i.e. stroptions is greater 17841 * than sizeof T_discon_ind. So the reallocb below 17842 * can't fail. 17843 */ 17844 freemsg(mp->b_cont); 17845 mp->b_cont = NULL; 17846 ASSERT(DB_REF(mp) == 1); 17847 mp = reallocb(mp, sizeof (struct T_discon_ind), 17848 B_FALSE); 17849 ASSERT(mp != NULL); 17850 DB_TYPE(mp) = M_PROTO; 17851 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17852 tdi = (struct T_discon_ind *)mp->b_rptr; 17853 if (tcp->tcp_issocket) { 17854 tdi->DISCON_reason = ECONNREFUSED; 17855 tdi->SEQ_number = 0; 17856 } else { 17857 tdi->DISCON_reason = ENOPROTOOPT; 17858 tdi->SEQ_number = 17859 tcp->tcp_conn_req_seqnum; 17860 } 17861 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17862 putnext(q, mp); 17863 } else { 17864 freemsg(mp); 17865 } 17866 if (tcp->tcp_hard_binding) { 17867 tcp->tcp_hard_binding = B_FALSE; 17868 tcp->tcp_hard_bound = B_TRUE; 17869 } 17870 tcp->tcp_detached = B_FALSE; 17871 return; 17872 } 17873 17874 mp1 = stropt_mp->b_cont; 17875 stropt_mp->b_cont = NULL; 17876 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17877 stropt = (struct stroptions *)stropt_mp->b_rptr; 17878 17879 while (mp1 != NULL) { 17880 mp = mp1; 17881 mp1 = mp1->b_cont; 17882 mp->b_cont = NULL; 17883 tcp->tcp_drop_opt_ack_cnt++; 17884 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17885 } 17886 mp = NULL; 17887 17888 /* 17889 * For a loopback connection with tcp_direct_sockfs on, note that 17890 * we don't have to protect tcp_rcv_list yet because synchronous 17891 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17892 * possibly race with us. 17893 */ 17894 17895 /* 17896 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17897 * properly. This is the first time we know of the acceptor' 17898 * queue. So we do it here. 17899 */ 17900 if (tcp->tcp_rcv_list == NULL) { 17901 /* 17902 * Recv queue is empty, tcp_rwnd should not have changed. 17903 * That means it should be equal to the listener's tcp_rwnd. 17904 */ 17905 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17906 } else { 17907 #ifdef DEBUG 17908 uint_t cnt = 0; 17909 17910 mp1 = tcp->tcp_rcv_list; 17911 while ((mp = mp1) != NULL) { 17912 mp1 = mp->b_next; 17913 cnt += msgdsize(mp); 17914 } 17915 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17916 #endif 17917 /* There is some data, add them back to get the max. */ 17918 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17919 } 17920 17921 stropt->so_flags = SO_HIWAT; 17922 stropt->so_hiwat = MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 17923 17924 stropt->so_flags |= SO_MAXBLK; 17925 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17926 17927 /* 17928 * This is the first time we run on the correct 17929 * queue after tcp_accept. So fix all the q parameters 17930 * here. 17931 */ 17932 /* Allocate room for SACK options if needed. */ 17933 stropt->so_flags |= SO_WROFF; 17934 if (tcp->tcp_fused) { 17935 ASSERT(tcp->tcp_loopback); 17936 ASSERT(tcp->tcp_loopback_peer != NULL); 17937 /* 17938 * For fused tcp loopback, set the stream head's write 17939 * offset value to zero since we won't be needing any room 17940 * for TCP/IP headers. This would also improve performance 17941 * since it would reduce the amount of work done by kmem. 17942 * Non-fused tcp loopback case is handled separately below. 17943 */ 17944 stropt->so_wroff = 0; 17945 /* 17946 * Record the stream head's high water mark for this endpoint; 17947 * this is used for flow-control purposes in tcp_fuse_output(). 17948 */ 17949 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 17950 /* 17951 * Update the peer's transmit parameters according to 17952 * our recently calculated high water mark value. 17953 */ 17954 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17955 } else if (tcp->tcp_snd_sack_ok) { 17956 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17957 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17958 } else { 17959 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17960 tcps->tcps_wroff_xtra); 17961 } 17962 17963 /* 17964 * If this is endpoint is handling SSL, then reserve extra 17965 * offset and space at the end. 17966 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17967 * overriding the previous setting. The extra cost of signing and 17968 * encrypting multiple MSS-size records (12 of them with Ethernet), 17969 * instead of a single contiguous one by the stream head 17970 * largely outweighs the statistical reduction of ACKs, when 17971 * applicable. The peer will also save on decyption and verification 17972 * costs. 17973 */ 17974 if (tcp->tcp_kssl_ctx != NULL) { 17975 stropt->so_wroff += SSL3_WROFFSET; 17976 17977 stropt->so_flags |= SO_TAIL; 17978 stropt->so_tail = SSL3_MAX_TAIL_LEN; 17979 17980 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 17981 } 17982 17983 /* Send the options up */ 17984 putnext(q, stropt_mp); 17985 17986 /* 17987 * Pass up any data and/or a fin that has been received. 17988 * 17989 * Adjust receive window in case it had decreased 17990 * (because there is data <=> tcp_rcv_list != NULL) 17991 * while the connection was detached. Note that 17992 * in case the eager was flow-controlled, w/o this 17993 * code, the rwnd may never open up again! 17994 */ 17995 if (tcp->tcp_rcv_list != NULL) { 17996 /* We drain directly in case of fused tcp loopback */ 17997 if (!tcp->tcp_fused && canputnext(q)) { 17998 tcp->tcp_rwnd = q->q_hiwat; 17999 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 18000 << tcp->tcp_rcv_ws; 18001 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 18002 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18003 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 18004 tcp_xmit_ctl(NULL, 18005 tcp, (tcp->tcp_swnd == 0) ? 18006 tcp->tcp_suna : tcp->tcp_snxt, 18007 tcp->tcp_rnxt, TH_ACK); 18008 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 18009 } 18010 18011 } 18012 (void) tcp_rcv_drain(q, tcp); 18013 18014 /* 18015 * For fused tcp loopback, back-enable peer endpoint 18016 * if it's currently flow-controlled. 18017 */ 18018 if (tcp->tcp_fused) { 18019 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18020 18021 ASSERT(peer_tcp != NULL); 18022 ASSERT(peer_tcp->tcp_fused); 18023 /* 18024 * In order to change the peer's tcp_flow_stopped, 18025 * we need to take locks for both end points. The 18026 * highest address is taken first. 18027 */ 18028 if (peer_tcp > tcp) { 18029 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18030 mutex_enter(&tcp->tcp_non_sq_lock); 18031 } else { 18032 mutex_enter(&tcp->tcp_non_sq_lock); 18033 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18034 } 18035 if (peer_tcp->tcp_flow_stopped) { 18036 tcp_clrqfull(peer_tcp); 18037 TCP_STAT(tcps, tcp_fusion_backenabled); 18038 } 18039 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18040 mutex_exit(&tcp->tcp_non_sq_lock); 18041 } 18042 } 18043 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18044 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18045 mp = mi_tpi_ordrel_ind(); 18046 if (mp) { 18047 tcp->tcp_ordrel_done = B_TRUE; 18048 putnext(q, mp); 18049 if (tcp->tcp_deferred_clean_death) { 18050 /* 18051 * tcp_clean_death was deferred 18052 * for T_ORDREL_IND - do it now 18053 */ 18054 (void) tcp_clean_death(tcp, 18055 tcp->tcp_client_errno, 21); 18056 tcp->tcp_deferred_clean_death = B_FALSE; 18057 } 18058 } else { 18059 /* 18060 * Run the orderly release in the 18061 * service routine. 18062 */ 18063 qenable(q); 18064 } 18065 } 18066 if (tcp->tcp_hard_binding) { 18067 tcp->tcp_hard_binding = B_FALSE; 18068 tcp->tcp_hard_bound = B_TRUE; 18069 } 18070 18071 tcp->tcp_detached = B_FALSE; 18072 18073 /* We can enable synchronous streams now */ 18074 if (tcp->tcp_fused) { 18075 tcp_fuse_syncstr_enable_pair(tcp); 18076 } 18077 18078 if (tcp->tcp_ka_enabled) { 18079 tcp->tcp_ka_last_intrvl = 0; 18080 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18081 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18082 } 18083 18084 /* 18085 * At this point, eager is fully established and will 18086 * have the following references - 18087 * 18088 * 2 references for connection to exist (1 for TCP and 1 for IP). 18089 * 1 reference for the squeue which will be dropped by the squeue as 18090 * soon as this function returns. 18091 * There will be 1 additonal reference for being in classifier 18092 * hash list provided something bad hasn't happened. 18093 */ 18094 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18095 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18096 } 18097 18098 /* 18099 * The function called through squeue to get behind listener's perimeter to 18100 * send a deffered conn_ind. 18101 */ 18102 /* ARGSUSED */ 18103 void 18104 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18105 { 18106 conn_t *connp = (conn_t *)arg; 18107 tcp_t *listener = connp->conn_tcp; 18108 18109 if (listener->tcp_state == TCPS_CLOSED || 18110 TCP_IS_DETACHED(listener)) { 18111 /* 18112 * If listener has closed, it would have caused a 18113 * a cleanup/blowoff to happen for the eager. 18114 */ 18115 tcp_t *tcp; 18116 struct T_conn_ind *conn_ind; 18117 18118 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18119 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18120 conn_ind->OPT_length); 18121 /* 18122 * We need to drop the ref on eager that was put 18123 * tcp_rput_data() before trying to send the conn_ind 18124 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18125 * and tcp_wput_accept() is sending this deferred conn_ind but 18126 * listener is closed so we drop the ref. 18127 */ 18128 CONN_DEC_REF(tcp->tcp_connp); 18129 freemsg(mp); 18130 return; 18131 } 18132 putnext(listener->tcp_rq, mp); 18133 } 18134 18135 18136 /* 18137 * This is the STREAMS entry point for T_CONN_RES coming down on 18138 * Acceptor STREAM when sockfs listener does accept processing. 18139 * Read the block comment on top of tcp_conn_request(). 18140 */ 18141 void 18142 tcp_wput_accept(queue_t *q, mblk_t *mp) 18143 { 18144 queue_t *rq = RD(q); 18145 struct T_conn_res *conn_res; 18146 tcp_t *eager; 18147 tcp_t *listener; 18148 struct T_ok_ack *ok; 18149 t_scalar_t PRIM_type; 18150 mblk_t *opt_mp; 18151 conn_t *econnp; 18152 18153 ASSERT(DB_TYPE(mp) == M_PROTO); 18154 18155 conn_res = (struct T_conn_res *)mp->b_rptr; 18156 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18157 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18158 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18159 if (mp != NULL) 18160 putnext(rq, mp); 18161 return; 18162 } 18163 switch (conn_res->PRIM_type) { 18164 case O_T_CONN_RES: 18165 case T_CONN_RES: 18166 /* 18167 * We pass up an err ack if allocb fails. This will 18168 * cause sockfs to issue a T_DISCON_REQ which will cause 18169 * tcp_eager_blowoff to be called. sockfs will then call 18170 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18171 * we need to do the allocb up here because we have to 18172 * make sure rq->q_qinfo->qi_qclose still points to the 18173 * correct function (tcpclose_accept) in case allocb 18174 * fails. 18175 */ 18176 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18177 if (opt_mp == NULL) { 18178 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18179 if (mp != NULL) 18180 putnext(rq, mp); 18181 return; 18182 } 18183 18184 bcopy(mp->b_rptr + conn_res->OPT_offset, 18185 &eager, conn_res->OPT_length); 18186 PRIM_type = conn_res->PRIM_type; 18187 mp->b_datap->db_type = M_PCPROTO; 18188 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18189 ok = (struct T_ok_ack *)mp->b_rptr; 18190 ok->PRIM_type = T_OK_ACK; 18191 ok->CORRECT_prim = PRIM_type; 18192 econnp = eager->tcp_connp; 18193 econnp->conn_dev = (dev_t)q->q_ptr; 18194 eager->tcp_rq = rq; 18195 eager->tcp_wq = q; 18196 rq->q_ptr = econnp; 18197 rq->q_qinfo = &tcp_rinit; 18198 q->q_ptr = econnp; 18199 q->q_qinfo = &tcp_winit; 18200 listener = eager->tcp_listener; 18201 eager->tcp_issocket = B_TRUE; 18202 18203 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18204 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18205 ASSERT(econnp->conn_netstack == 18206 listener->tcp_connp->conn_netstack); 18207 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18208 18209 /* Put the ref for IP */ 18210 CONN_INC_REF(econnp); 18211 18212 /* 18213 * We should have minimum of 3 references on the conn 18214 * at this point. One each for TCP and IP and one for 18215 * the T_conn_ind that was sent up when the 3-way handshake 18216 * completed. In the normal case we would also have another 18217 * reference (making a total of 4) for the conn being in the 18218 * classifier hash list. However the eager could have received 18219 * an RST subsequently and tcp_closei_local could have removed 18220 * the eager from the classifier hash list, hence we can't 18221 * assert that reference. 18222 */ 18223 ASSERT(econnp->conn_ref >= 3); 18224 18225 /* 18226 * Send the new local address also up to sockfs. There 18227 * should already be enough space in the mp that came 18228 * down from soaccept(). 18229 */ 18230 if (eager->tcp_family == AF_INET) { 18231 sin_t *sin; 18232 18233 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18234 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18235 sin = (sin_t *)mp->b_wptr; 18236 mp->b_wptr += sizeof (sin_t); 18237 sin->sin_family = AF_INET; 18238 sin->sin_port = eager->tcp_lport; 18239 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18240 } else { 18241 sin6_t *sin6; 18242 18243 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18244 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18245 sin6 = (sin6_t *)mp->b_wptr; 18246 mp->b_wptr += sizeof (sin6_t); 18247 sin6->sin6_family = AF_INET6; 18248 sin6->sin6_port = eager->tcp_lport; 18249 if (eager->tcp_ipversion == IPV4_VERSION) { 18250 sin6->sin6_flowinfo = 0; 18251 IN6_IPADDR_TO_V4MAPPED( 18252 eager->tcp_ipha->ipha_src, 18253 &sin6->sin6_addr); 18254 } else { 18255 ASSERT(eager->tcp_ip6h != NULL); 18256 sin6->sin6_flowinfo = 18257 eager->tcp_ip6h->ip6_vcf & 18258 ~IPV6_VERS_AND_FLOW_MASK; 18259 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18260 } 18261 sin6->sin6_scope_id = 0; 18262 sin6->__sin6_src_id = 0; 18263 } 18264 18265 putnext(rq, mp); 18266 18267 opt_mp->b_datap->db_type = M_SETOPTS; 18268 opt_mp->b_wptr += sizeof (struct stroptions); 18269 18270 /* 18271 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18272 * from listener to acceptor. The message is chained on the 18273 * bind_mp which tcp_rput_other will send down to IP. 18274 */ 18275 if (listener->tcp_bound_if != 0) { 18276 /* allocate optmgmt req */ 18277 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18278 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18279 sizeof (int)); 18280 if (mp != NULL) 18281 linkb(opt_mp, mp); 18282 } 18283 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18284 uint_t on = 1; 18285 18286 /* allocate optmgmt req */ 18287 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18288 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18289 if (mp != NULL) 18290 linkb(opt_mp, mp); 18291 } 18292 18293 18294 mutex_enter(&listener->tcp_eager_lock); 18295 18296 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18297 18298 tcp_t *tail; 18299 tcp_t *tcp; 18300 mblk_t *mp1; 18301 18302 tcp = listener->tcp_eager_prev_q0; 18303 /* 18304 * listener->tcp_eager_prev_q0 points to the TAIL of the 18305 * deferred T_conn_ind queue. We need to get to the head 18306 * of the queue in order to send up T_conn_ind the same 18307 * order as how the 3WHS is completed. 18308 */ 18309 while (tcp != listener) { 18310 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18311 !tcp->tcp_kssl_pending) 18312 break; 18313 else 18314 tcp = tcp->tcp_eager_prev_q0; 18315 } 18316 /* None of the pending eagers can be sent up now */ 18317 if (tcp == listener) 18318 goto no_more_eagers; 18319 18320 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18321 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18322 /* Move from q0 to q */ 18323 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18324 listener->tcp_conn_req_cnt_q0--; 18325 listener->tcp_conn_req_cnt_q++; 18326 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18327 tcp->tcp_eager_prev_q0; 18328 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18329 tcp->tcp_eager_next_q0; 18330 tcp->tcp_eager_prev_q0 = NULL; 18331 tcp->tcp_eager_next_q0 = NULL; 18332 tcp->tcp_conn_def_q0 = B_FALSE; 18333 18334 /* Make sure the tcp isn't in the list of droppables */ 18335 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18336 tcp->tcp_eager_prev_drop_q0 == NULL); 18337 18338 /* 18339 * Insert at end of the queue because sockfs sends 18340 * down T_CONN_RES in chronological order. Leaving 18341 * the older conn indications at front of the queue 18342 * helps reducing search time. 18343 */ 18344 tail = listener->tcp_eager_last_q; 18345 if (tail != NULL) { 18346 tail->tcp_eager_next_q = tcp; 18347 } else { 18348 listener->tcp_eager_next_q = tcp; 18349 } 18350 listener->tcp_eager_last_q = tcp; 18351 tcp->tcp_eager_next_q = NULL; 18352 18353 /* Need to get inside the listener perimeter */ 18354 CONN_INC_REF(listener->tcp_connp); 18355 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18356 tcp_send_pending, listener->tcp_connp, 18357 SQTAG_TCP_SEND_PENDING); 18358 } 18359 no_more_eagers: 18360 tcp_eager_unlink(eager); 18361 mutex_exit(&listener->tcp_eager_lock); 18362 18363 /* 18364 * At this point, the eager is detached from the listener 18365 * but we still have an extra refs on eager (apart from the 18366 * usual tcp references). The ref was placed in tcp_rput_data 18367 * before sending the conn_ind in tcp_send_conn_ind. 18368 * The ref will be dropped in tcp_accept_finish(). 18369 */ 18370 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18371 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18372 return; 18373 default: 18374 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18375 if (mp != NULL) 18376 putnext(rq, mp); 18377 return; 18378 } 18379 } 18380 18381 void 18382 tcp_wput(queue_t *q, mblk_t *mp) 18383 { 18384 conn_t *connp = Q_TO_CONN(q); 18385 tcp_t *tcp; 18386 void (*output_proc)(); 18387 t_scalar_t type; 18388 uchar_t *rptr; 18389 struct iocblk *iocp; 18390 uint32_t msize; 18391 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18392 18393 ASSERT(connp->conn_ref >= 2); 18394 18395 switch (DB_TYPE(mp)) { 18396 case M_DATA: 18397 tcp = connp->conn_tcp; 18398 ASSERT(tcp != NULL); 18399 18400 msize = msgdsize(mp); 18401 18402 mutex_enter(&tcp->tcp_non_sq_lock); 18403 tcp->tcp_squeue_bytes += msize; 18404 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18405 tcp_setqfull(tcp); 18406 } 18407 mutex_exit(&tcp->tcp_non_sq_lock); 18408 18409 CONN_INC_REF(connp); 18410 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18411 tcp_output, connp, SQTAG_TCP_OUTPUT); 18412 return; 18413 case M_PROTO: 18414 case M_PCPROTO: 18415 /* 18416 * if it is a snmp message, don't get behind the squeue 18417 */ 18418 tcp = connp->conn_tcp; 18419 rptr = mp->b_rptr; 18420 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18421 type = ((union T_primitives *)rptr)->type; 18422 } else { 18423 if (tcp->tcp_debug) { 18424 (void) strlog(TCP_MOD_ID, 0, 1, 18425 SL_ERROR|SL_TRACE, 18426 "tcp_wput_proto, dropping one..."); 18427 } 18428 freemsg(mp); 18429 return; 18430 } 18431 if (type == T_SVR4_OPTMGMT_REQ) { 18432 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18433 if (snmpcom_req(q, mp, tcp_snmp_set, tcp_snmp_get, 18434 cr)) { 18435 /* 18436 * This was a SNMP request 18437 */ 18438 return; 18439 } else { 18440 output_proc = tcp_wput_proto; 18441 } 18442 } else { 18443 output_proc = tcp_wput_proto; 18444 } 18445 break; 18446 case M_IOCTL: 18447 /* 18448 * Most ioctls can be processed right away without going via 18449 * squeues - process them right here. Those that do require 18450 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18451 * are processed by tcp_wput_ioctl(). 18452 */ 18453 iocp = (struct iocblk *)mp->b_rptr; 18454 tcp = connp->conn_tcp; 18455 18456 switch (iocp->ioc_cmd) { 18457 case TCP_IOC_ABORT_CONN: 18458 tcp_ioctl_abort_conn(q, mp); 18459 return; 18460 case TI_GETPEERNAME: 18461 if (tcp->tcp_state < TCPS_SYN_RCVD) { 18462 iocp->ioc_error = ENOTCONN; 18463 iocp->ioc_count = 0; 18464 mp->b_datap->db_type = M_IOCACK; 18465 qreply(q, mp); 18466 return; 18467 } 18468 /* FALLTHRU */ 18469 case TI_GETMYNAME: 18470 mi_copyin(q, mp, NULL, 18471 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18472 return; 18473 case ND_SET: 18474 /* nd_getset does the necessary checks */ 18475 case ND_GET: 18476 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18477 CALL_IP_WPUT(connp, q, mp); 18478 return; 18479 } 18480 qreply(q, mp); 18481 return; 18482 case TCP_IOC_DEFAULT_Q: 18483 /* 18484 * Wants to be the default wq. Check the credentials 18485 * first, the rest is executed via squeue. 18486 */ 18487 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18488 iocp->ioc_error = EPERM; 18489 iocp->ioc_count = 0; 18490 mp->b_datap->db_type = M_IOCACK; 18491 qreply(q, mp); 18492 return; 18493 } 18494 output_proc = tcp_wput_ioctl; 18495 break; 18496 default: 18497 output_proc = tcp_wput_ioctl; 18498 break; 18499 } 18500 break; 18501 default: 18502 output_proc = tcp_wput_nondata; 18503 break; 18504 } 18505 18506 CONN_INC_REF(connp); 18507 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18508 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18509 } 18510 18511 /* 18512 * Initial STREAMS write side put() procedure for sockets. It tries to 18513 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18514 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18515 * are handled by tcp_wput() as usual. 18516 * 18517 * All further messages will also be handled by tcp_wput() because we cannot 18518 * be sure that the above short cut is safe later. 18519 */ 18520 static void 18521 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18522 { 18523 conn_t *connp = Q_TO_CONN(wq); 18524 tcp_t *tcp = connp->conn_tcp; 18525 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18526 18527 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18528 wq->q_qinfo = &tcp_winit; 18529 18530 ASSERT(IPCL_IS_TCP(connp)); 18531 ASSERT(TCP_IS_SOCKET(tcp)); 18532 18533 if (DB_TYPE(mp) == M_PCPROTO && 18534 MBLKL(mp) == sizeof (struct T_capability_req) && 18535 car->PRIM_type == T_CAPABILITY_REQ) { 18536 tcp_capability_req(tcp, mp); 18537 return; 18538 } 18539 18540 tcp_wput(wq, mp); 18541 } 18542 18543 static boolean_t 18544 tcp_zcopy_check(tcp_t *tcp) 18545 { 18546 conn_t *connp = tcp->tcp_connp; 18547 ire_t *ire; 18548 boolean_t zc_enabled = B_FALSE; 18549 tcp_stack_t *tcps = tcp->tcp_tcps; 18550 18551 if (do_tcpzcopy == 2) 18552 zc_enabled = B_TRUE; 18553 else if (tcp->tcp_ipversion == IPV4_VERSION && 18554 IPCL_IS_CONNECTED(connp) && 18555 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18556 connp->conn_dontroute == 0 && 18557 !connp->conn_nexthop_set && 18558 connp->conn_xmit_if_ill == NULL && 18559 connp->conn_nofailover_ill == NULL && 18560 do_tcpzcopy == 1) { 18561 /* 18562 * the checks above closely resemble the fast path checks 18563 * in tcp_send_data(). 18564 */ 18565 mutex_enter(&connp->conn_lock); 18566 ire = connp->conn_ire_cache; 18567 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18568 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18569 IRE_REFHOLD(ire); 18570 if (ire->ire_stq != NULL) { 18571 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18572 18573 zc_enabled = ill && (ill->ill_capabilities & 18574 ILL_CAPAB_ZEROCOPY) && 18575 (ill->ill_zerocopy_capab-> 18576 ill_zerocopy_flags != 0); 18577 } 18578 IRE_REFRELE(ire); 18579 } 18580 mutex_exit(&connp->conn_lock); 18581 } 18582 tcp->tcp_snd_zcopy_on = zc_enabled; 18583 if (!TCP_IS_DETACHED(tcp)) { 18584 if (zc_enabled) { 18585 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 18586 TCP_STAT(tcps, tcp_zcopy_on); 18587 } else { 18588 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18589 TCP_STAT(tcps, tcp_zcopy_off); 18590 } 18591 } 18592 return (zc_enabled); 18593 } 18594 18595 static mblk_t * 18596 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18597 { 18598 tcp_stack_t *tcps = tcp->tcp_tcps; 18599 18600 if (do_tcpzcopy == 2) 18601 return (bp); 18602 else if (tcp->tcp_snd_zcopy_on) { 18603 tcp->tcp_snd_zcopy_on = B_FALSE; 18604 if (!TCP_IS_DETACHED(tcp)) { 18605 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18606 TCP_STAT(tcps, tcp_zcopy_disable); 18607 } 18608 } 18609 return (tcp_zcopy_backoff(tcp, bp, 0)); 18610 } 18611 18612 /* 18613 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18614 * the original desballoca'ed segmapped mblk. 18615 */ 18616 static mblk_t * 18617 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18618 { 18619 mblk_t *head, *tail, *nbp; 18620 tcp_stack_t *tcps = tcp->tcp_tcps; 18621 18622 if (IS_VMLOANED_MBLK(bp)) { 18623 TCP_STAT(tcps, tcp_zcopy_backoff); 18624 if ((head = copyb(bp)) == NULL) { 18625 /* fail to backoff; leave it for the next backoff */ 18626 tcp->tcp_xmit_zc_clean = B_FALSE; 18627 return (bp); 18628 } 18629 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18630 if (fix_xmitlist) 18631 tcp_zcopy_notify(tcp); 18632 else 18633 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18634 } 18635 nbp = bp->b_cont; 18636 if (fix_xmitlist) { 18637 head->b_prev = bp->b_prev; 18638 head->b_next = bp->b_next; 18639 if (tcp->tcp_xmit_tail == bp) 18640 tcp->tcp_xmit_tail = head; 18641 } 18642 bp->b_next = NULL; 18643 bp->b_prev = NULL; 18644 freeb(bp); 18645 } else { 18646 head = bp; 18647 nbp = bp->b_cont; 18648 } 18649 tail = head; 18650 while (nbp) { 18651 if (IS_VMLOANED_MBLK(nbp)) { 18652 TCP_STAT(tcps, tcp_zcopy_backoff); 18653 if ((tail->b_cont = copyb(nbp)) == NULL) { 18654 tcp->tcp_xmit_zc_clean = B_FALSE; 18655 tail->b_cont = nbp; 18656 return (head); 18657 } 18658 tail = tail->b_cont; 18659 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18660 if (fix_xmitlist) 18661 tcp_zcopy_notify(tcp); 18662 else 18663 tail->b_datap->db_struioflag |= 18664 STRUIO_ZCNOTIFY; 18665 } 18666 bp = nbp; 18667 nbp = nbp->b_cont; 18668 if (fix_xmitlist) { 18669 tail->b_prev = bp->b_prev; 18670 tail->b_next = bp->b_next; 18671 if (tcp->tcp_xmit_tail == bp) 18672 tcp->tcp_xmit_tail = tail; 18673 } 18674 bp->b_next = NULL; 18675 bp->b_prev = NULL; 18676 freeb(bp); 18677 } else { 18678 tail->b_cont = nbp; 18679 tail = nbp; 18680 nbp = nbp->b_cont; 18681 } 18682 } 18683 if (fix_xmitlist) { 18684 tcp->tcp_xmit_last = tail; 18685 tcp->tcp_xmit_zc_clean = B_TRUE; 18686 } 18687 return (head); 18688 } 18689 18690 static void 18691 tcp_zcopy_notify(tcp_t *tcp) 18692 { 18693 struct stdata *stp; 18694 18695 if (tcp->tcp_detached) 18696 return; 18697 stp = STREAM(tcp->tcp_rq); 18698 mutex_enter(&stp->sd_lock); 18699 stp->sd_flag |= STZCNOTIFY; 18700 cv_broadcast(&stp->sd_zcopy_wait); 18701 mutex_exit(&stp->sd_lock); 18702 } 18703 18704 static boolean_t 18705 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18706 { 18707 ire_t *ire; 18708 conn_t *connp = tcp->tcp_connp; 18709 tcp_stack_t *tcps = tcp->tcp_tcps; 18710 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18711 18712 mutex_enter(&connp->conn_lock); 18713 ire = connp->conn_ire_cache; 18714 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18715 18716 if ((ire != NULL) && 18717 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18718 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18719 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18720 IRE_REFHOLD(ire); 18721 mutex_exit(&connp->conn_lock); 18722 } else { 18723 boolean_t cached = B_FALSE; 18724 ts_label_t *tsl; 18725 18726 /* force a recheck later on */ 18727 tcp->tcp_ire_ill_check_done = B_FALSE; 18728 18729 TCP_DBGSTAT(tcps, tcp_ire_null1); 18730 connp->conn_ire_cache = NULL; 18731 mutex_exit(&connp->conn_lock); 18732 18733 if (ire != NULL) 18734 IRE_REFRELE_NOTR(ire); 18735 18736 tsl = crgetlabel(CONN_CRED(connp)); 18737 ire = (dst ? 18738 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18739 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18740 connp->conn_zoneid, tsl, ipst)); 18741 18742 if (ire == NULL) { 18743 TCP_STAT(tcps, tcp_ire_null); 18744 return (B_FALSE); 18745 } 18746 18747 IRE_REFHOLD_NOTR(ire); 18748 /* 18749 * Since we are inside the squeue, there cannot be another 18750 * thread in TCP trying to set the conn_ire_cache now. The 18751 * check for IRE_MARK_CONDEMNED ensures that an interface 18752 * unplumb thread has not yet started cleaning up the conns. 18753 * Hence we don't need to grab the conn lock. 18754 */ 18755 if (CONN_CACHE_IRE(connp)) { 18756 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18757 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18758 connp->conn_ire_cache = ire; 18759 cached = B_TRUE; 18760 } 18761 rw_exit(&ire->ire_bucket->irb_lock); 18762 } 18763 18764 /* 18765 * We can continue to use the ire but since it was 18766 * not cached, we should drop the extra reference. 18767 */ 18768 if (!cached) 18769 IRE_REFRELE_NOTR(ire); 18770 18771 /* 18772 * Rampart note: no need to select a new label here, since 18773 * labels are not allowed to change during the life of a TCP 18774 * connection. 18775 */ 18776 } 18777 18778 *irep = ire; 18779 18780 return (B_TRUE); 18781 } 18782 18783 /* 18784 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18785 * 18786 * 0 = success; 18787 * 1 = failed to find ire and ill. 18788 */ 18789 static boolean_t 18790 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18791 { 18792 ipha_t *ipha; 18793 ipaddr_t dst; 18794 ire_t *ire; 18795 ill_t *ill; 18796 conn_t *connp = tcp->tcp_connp; 18797 mblk_t *ire_fp_mp; 18798 tcp_stack_t *tcps = tcp->tcp_tcps; 18799 18800 if (mp != NULL) 18801 ipha = (ipha_t *)mp->b_rptr; 18802 else 18803 ipha = tcp->tcp_ipha; 18804 dst = ipha->ipha_dst; 18805 18806 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18807 return (B_FALSE); 18808 18809 if ((ire->ire_flags & RTF_MULTIRT) || 18810 (ire->ire_stq == NULL) || 18811 (ire->ire_nce == NULL) || 18812 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18813 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18814 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18815 TCP_STAT(tcps, tcp_ip_ire_send); 18816 IRE_REFRELE(ire); 18817 return (B_FALSE); 18818 } 18819 18820 ill = ire_to_ill(ire); 18821 if (connp->conn_outgoing_ill != NULL) { 18822 ill_t *conn_outgoing_ill = NULL; 18823 /* 18824 * Choose a good ill in the group to send the packets on. 18825 */ 18826 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18827 ill = ire_to_ill(ire); 18828 } 18829 ASSERT(ill != NULL); 18830 18831 if (!tcp->tcp_ire_ill_check_done) { 18832 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18833 tcp->tcp_ire_ill_check_done = B_TRUE; 18834 } 18835 18836 *irep = ire; 18837 *illp = ill; 18838 18839 return (B_TRUE); 18840 } 18841 18842 static void 18843 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18844 { 18845 ipha_t *ipha; 18846 ipaddr_t src; 18847 ipaddr_t dst; 18848 uint32_t cksum; 18849 ire_t *ire; 18850 uint16_t *up; 18851 ill_t *ill; 18852 conn_t *connp = tcp->tcp_connp; 18853 uint32_t hcksum_txflags = 0; 18854 mblk_t *ire_fp_mp; 18855 uint_t ire_fp_mp_len; 18856 tcp_stack_t *tcps = tcp->tcp_tcps; 18857 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18858 18859 ASSERT(DB_TYPE(mp) == M_DATA); 18860 18861 if (DB_CRED(mp) == NULL) 18862 mblk_setcred(mp, CONN_CRED(connp)); 18863 18864 ipha = (ipha_t *)mp->b_rptr; 18865 src = ipha->ipha_src; 18866 dst = ipha->ipha_dst; 18867 18868 /* 18869 * Drop off fast path for IPv6 and also if options are present or 18870 * we need to resolve a TS label. 18871 */ 18872 if (tcp->tcp_ipversion != IPV4_VERSION || 18873 !IPCL_IS_CONNECTED(connp) || 18874 !CONN_IS_LSO_MD_FASTPATH(connp) || 18875 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18876 !connp->conn_ulp_labeled || 18877 ipha->ipha_ident == IP_HDR_INCLUDED || 18878 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18879 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18880 if (tcp->tcp_snd_zcopy_aware) 18881 mp = tcp_zcopy_disable(tcp, mp); 18882 TCP_STAT(tcps, tcp_ip_send); 18883 CALL_IP_WPUT(connp, q, mp); 18884 return; 18885 } 18886 18887 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18888 if (tcp->tcp_snd_zcopy_aware) 18889 mp = tcp_zcopy_backoff(tcp, mp, 0); 18890 CALL_IP_WPUT(connp, q, mp); 18891 return; 18892 } 18893 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18894 ire_fp_mp_len = MBLKL(ire_fp_mp); 18895 18896 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18897 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18898 #ifndef _BIG_ENDIAN 18899 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18900 #endif 18901 18902 /* 18903 * Check to see if we need to re-enable LSO/MDT for this connection 18904 * because it was previously disabled due to changes in the ill; 18905 * note that by doing it here, this re-enabling only applies when 18906 * the packet is not dispatched through CALL_IP_WPUT(). 18907 * 18908 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18909 * case, since that's how we ended up here. For IPv6, we do the 18910 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18911 */ 18912 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18913 /* 18914 * Restore LSO for this connection, so that next time around 18915 * it is eligible to go through tcp_lsosend() path again. 18916 */ 18917 TCP_STAT(tcps, tcp_lso_enabled); 18918 tcp->tcp_lso = B_TRUE; 18919 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18920 "interface %s\n", (void *)connp, ill->ill_name)); 18921 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18922 /* 18923 * Restore MDT for this connection, so that next time around 18924 * it is eligible to go through tcp_multisend() path again. 18925 */ 18926 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18927 tcp->tcp_mdt = B_TRUE; 18928 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18929 "interface %s\n", (void *)connp, ill->ill_name)); 18930 } 18931 18932 if (tcp->tcp_snd_zcopy_aware) { 18933 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18934 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18935 mp = tcp_zcopy_disable(tcp, mp); 18936 /* 18937 * we shouldn't need to reset ipha as the mp containing 18938 * ipha should never be a zero-copy mp. 18939 */ 18940 } 18941 18942 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18943 ASSERT(ill->ill_hcksum_capab != NULL); 18944 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18945 } 18946 18947 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18948 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18949 18950 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18951 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18952 18953 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18954 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18955 18956 /* Software checksum? */ 18957 if (DB_CKSUMFLAGS(mp) == 0) { 18958 TCP_STAT(tcps, tcp_out_sw_cksum); 18959 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18960 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18961 } 18962 18963 ipha->ipha_fragment_offset_and_flags |= 18964 (uint32_t)htons(ire->ire_frag_flag); 18965 18966 /* Calculate IP header checksum if hardware isn't capable */ 18967 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18968 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18969 ((uint16_t *)ipha)[4]); 18970 } 18971 18972 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18973 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18974 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18975 18976 UPDATE_OB_PKT_COUNT(ire); 18977 ire->ire_last_used_time = lbolt; 18978 18979 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 18980 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 18981 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 18982 ntohs(ipha->ipha_length)); 18983 18984 if (ILL_DLS_CAPABLE(ill)) { 18985 /* 18986 * Send the packet directly to DLD, where it may be queued 18987 * depending on the availability of transmit resources at 18988 * the media layer. 18989 */ 18990 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 18991 } else { 18992 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 18993 DTRACE_PROBE4(ip4__physical__out__start, 18994 ill_t *, NULL, ill_t *, out_ill, 18995 ipha_t *, ipha, mblk_t *, mp); 18996 FW_HOOKS(ipst->ips_ip4_physical_out_event, 18997 ipst->ips_ipv4firewall_physical_out, 18998 NULL, out_ill, ipha, mp, mp, ipst); 18999 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19000 if (mp != NULL) 19001 putnext(ire->ire_stq, mp); 19002 } 19003 IRE_REFRELE(ire); 19004 } 19005 19006 /* 19007 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19008 * if the receiver shrinks the window, i.e. moves the right window to the 19009 * left, the we should not send new data, but should retransmit normally the 19010 * old unacked data between suna and suna + swnd. We might has sent data 19011 * that is now outside the new window, pretend that we didn't send it. 19012 */ 19013 static void 19014 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19015 { 19016 uint32_t snxt = tcp->tcp_snxt; 19017 mblk_t *xmit_tail; 19018 int32_t offset; 19019 19020 ASSERT(shrunk_count > 0); 19021 19022 /* Pretend we didn't send the data outside the window */ 19023 snxt -= shrunk_count; 19024 19025 /* Get the mblk and the offset in it per the shrunk window */ 19026 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19027 19028 ASSERT(xmit_tail != NULL); 19029 19030 /* Reset all the values per the now shrunk window */ 19031 tcp->tcp_snxt = snxt; 19032 tcp->tcp_xmit_tail = xmit_tail; 19033 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19034 offset; 19035 tcp->tcp_unsent += shrunk_count; 19036 19037 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19038 /* 19039 * Make sure the timer is running so that we will probe a zero 19040 * window. 19041 */ 19042 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19043 } 19044 19045 19046 /* 19047 * The TCP normal data output path. 19048 * NOTE: the logic of the fast path is duplicated from this function. 19049 */ 19050 static void 19051 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19052 { 19053 int len; 19054 mblk_t *local_time; 19055 mblk_t *mp1; 19056 uint32_t snxt; 19057 int tail_unsent; 19058 int tcpstate; 19059 int usable = 0; 19060 mblk_t *xmit_tail; 19061 queue_t *q = tcp->tcp_wq; 19062 int32_t mss; 19063 int32_t num_sack_blk = 0; 19064 int32_t tcp_hdr_len; 19065 int32_t tcp_tcp_hdr_len; 19066 int mdt_thres; 19067 int rc; 19068 tcp_stack_t *tcps = tcp->tcp_tcps; 19069 ip_stack_t *ipst; 19070 19071 tcpstate = tcp->tcp_state; 19072 if (mp == NULL) { 19073 /* 19074 * tcp_wput_data() with NULL mp should only be called when 19075 * there is unsent data. 19076 */ 19077 ASSERT(tcp->tcp_unsent > 0); 19078 /* Really tacky... but we need this for detached closes. */ 19079 len = tcp->tcp_unsent; 19080 goto data_null; 19081 } 19082 19083 #if CCS_STATS 19084 wrw_stats.tot.count++; 19085 wrw_stats.tot.bytes += msgdsize(mp); 19086 #endif 19087 ASSERT(mp->b_datap->db_type == M_DATA); 19088 /* 19089 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19090 * or before a connection attempt has begun. 19091 */ 19092 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19093 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19094 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19095 #ifdef DEBUG 19096 cmn_err(CE_WARN, 19097 "tcp_wput_data: data after ordrel, %s", 19098 tcp_display(tcp, NULL, 19099 DISP_ADDR_AND_PORT)); 19100 #else 19101 if (tcp->tcp_debug) { 19102 (void) strlog(TCP_MOD_ID, 0, 1, 19103 SL_TRACE|SL_ERROR, 19104 "tcp_wput_data: data after ordrel, %s\n", 19105 tcp_display(tcp, NULL, 19106 DISP_ADDR_AND_PORT)); 19107 } 19108 #endif /* DEBUG */ 19109 } 19110 if (tcp->tcp_snd_zcopy_aware && 19111 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19112 tcp_zcopy_notify(tcp); 19113 freemsg(mp); 19114 mutex_enter(&tcp->tcp_non_sq_lock); 19115 if (tcp->tcp_flow_stopped && 19116 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19117 tcp_clrqfull(tcp); 19118 } 19119 mutex_exit(&tcp->tcp_non_sq_lock); 19120 return; 19121 } 19122 19123 /* Strip empties */ 19124 for (;;) { 19125 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19126 (uintptr_t)INT_MAX); 19127 len = (int)(mp->b_wptr - mp->b_rptr); 19128 if (len > 0) 19129 break; 19130 mp1 = mp; 19131 mp = mp->b_cont; 19132 freeb(mp1); 19133 if (!mp) { 19134 return; 19135 } 19136 } 19137 19138 /* If we are the first on the list ... */ 19139 if (tcp->tcp_xmit_head == NULL) { 19140 tcp->tcp_xmit_head = mp; 19141 tcp->tcp_xmit_tail = mp; 19142 tcp->tcp_xmit_tail_unsent = len; 19143 } else { 19144 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19145 struct datab *dp; 19146 19147 mp1 = tcp->tcp_xmit_last; 19148 if (len < tcp_tx_pull_len && 19149 (dp = mp1->b_datap)->db_ref == 1 && 19150 dp->db_lim - mp1->b_wptr >= len) { 19151 ASSERT(len > 0); 19152 ASSERT(!mp1->b_cont); 19153 if (len == 1) { 19154 *mp1->b_wptr++ = *mp->b_rptr; 19155 } else { 19156 bcopy(mp->b_rptr, mp1->b_wptr, len); 19157 mp1->b_wptr += len; 19158 } 19159 if (mp1 == tcp->tcp_xmit_tail) 19160 tcp->tcp_xmit_tail_unsent += len; 19161 mp1->b_cont = mp->b_cont; 19162 if (tcp->tcp_snd_zcopy_aware && 19163 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19164 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19165 freeb(mp); 19166 mp = mp1; 19167 } else { 19168 tcp->tcp_xmit_last->b_cont = mp; 19169 } 19170 len += tcp->tcp_unsent; 19171 } 19172 19173 /* Tack on however many more positive length mblks we have */ 19174 if ((mp1 = mp->b_cont) != NULL) { 19175 do { 19176 int tlen; 19177 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19178 (uintptr_t)INT_MAX); 19179 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19180 if (tlen <= 0) { 19181 mp->b_cont = mp1->b_cont; 19182 freeb(mp1); 19183 } else { 19184 len += tlen; 19185 mp = mp1; 19186 } 19187 } while ((mp1 = mp->b_cont) != NULL); 19188 } 19189 tcp->tcp_xmit_last = mp; 19190 tcp->tcp_unsent = len; 19191 19192 if (urgent) 19193 usable = 1; 19194 19195 data_null: 19196 snxt = tcp->tcp_snxt; 19197 xmit_tail = tcp->tcp_xmit_tail; 19198 tail_unsent = tcp->tcp_xmit_tail_unsent; 19199 19200 /* 19201 * Note that tcp_mss has been adjusted to take into account the 19202 * timestamp option if applicable. Because SACK options do not 19203 * appear in every TCP segments and they are of variable lengths, 19204 * they cannot be included in tcp_mss. Thus we need to calculate 19205 * the actual segment length when we need to send a segment which 19206 * includes SACK options. 19207 */ 19208 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19209 int32_t opt_len; 19210 19211 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19212 tcp->tcp_num_sack_blk); 19213 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19214 2 + TCPOPT_HEADER_LEN; 19215 mss = tcp->tcp_mss - opt_len; 19216 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19217 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19218 } else { 19219 mss = tcp->tcp_mss; 19220 tcp_hdr_len = tcp->tcp_hdr_len; 19221 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19222 } 19223 19224 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19225 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19226 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19227 } 19228 if (tcpstate == TCPS_SYN_RCVD) { 19229 /* 19230 * The three-way connection establishment handshake is not 19231 * complete yet. We want to queue the data for transmission 19232 * after entering ESTABLISHED state (RFC793). A jump to 19233 * "done" label effectively leaves data on the queue. 19234 */ 19235 goto done; 19236 } else { 19237 int usable_r; 19238 19239 /* 19240 * In the special case when cwnd is zero, which can only 19241 * happen if the connection is ECN capable, return now. 19242 * New segments is sent using tcp_timer(). The timer 19243 * is set in tcp_rput_data(). 19244 */ 19245 if (tcp->tcp_cwnd == 0) { 19246 /* 19247 * Note that tcp_cwnd is 0 before 3-way handshake is 19248 * finished. 19249 */ 19250 ASSERT(tcp->tcp_ecn_ok || 19251 tcp->tcp_state < TCPS_ESTABLISHED); 19252 return; 19253 } 19254 19255 /* NOTE: trouble if xmitting while SYN not acked? */ 19256 usable_r = snxt - tcp->tcp_suna; 19257 usable_r = tcp->tcp_swnd - usable_r; 19258 19259 /* 19260 * Check if the receiver has shrunk the window. If 19261 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19262 * cannot be set as there is unsent data, so FIN cannot 19263 * be sent out. Otherwise, we need to take into account 19264 * of FIN as it consumes an "invisible" sequence number. 19265 */ 19266 ASSERT(tcp->tcp_fin_sent == 0); 19267 if (usable_r < 0) { 19268 /* 19269 * The receiver has shrunk the window and we have sent 19270 * -usable_r date beyond the window, re-adjust. 19271 * 19272 * If TCP window scaling is enabled, there can be 19273 * round down error as the advertised receive window 19274 * is actually right shifted n bits. This means that 19275 * the lower n bits info is wiped out. It will look 19276 * like the window is shrunk. Do a check here to 19277 * see if the shrunk amount is actually within the 19278 * error in window calculation. If it is, just 19279 * return. Note that this check is inside the 19280 * shrunk window check. This makes sure that even 19281 * though tcp_process_shrunk_swnd() is not called, 19282 * we will stop further processing. 19283 */ 19284 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19285 tcp_process_shrunk_swnd(tcp, -usable_r); 19286 } 19287 return; 19288 } 19289 19290 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19291 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19292 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19293 19294 /* usable = MIN(usable, unsent) */ 19295 if (usable_r > len) 19296 usable_r = len; 19297 19298 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19299 if (usable_r > 0) { 19300 usable = usable_r; 19301 } else { 19302 /* Bypass all other unnecessary processing. */ 19303 goto done; 19304 } 19305 } 19306 19307 local_time = (mblk_t *)lbolt; 19308 19309 /* 19310 * "Our" Nagle Algorithm. This is not the same as in the old 19311 * BSD. This is more in line with the true intent of Nagle. 19312 * 19313 * The conditions are: 19314 * 1. The amount of unsent data (or amount of data which can be 19315 * sent, whichever is smaller) is less than Nagle limit. 19316 * 2. The last sent size is also less than Nagle limit. 19317 * 3. There is unack'ed data. 19318 * 4. Urgent pointer is not set. Send urgent data ignoring the 19319 * Nagle algorithm. This reduces the probability that urgent 19320 * bytes get "merged" together. 19321 * 5. The app has not closed the connection. This eliminates the 19322 * wait time of the receiving side waiting for the last piece of 19323 * (small) data. 19324 * 19325 * If all are satisified, exit without sending anything. Note 19326 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19327 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19328 * 4095). 19329 */ 19330 if (usable < (int)tcp->tcp_naglim && 19331 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19332 snxt != tcp->tcp_suna && 19333 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19334 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19335 goto done; 19336 } 19337 19338 if (tcp->tcp_cork) { 19339 /* 19340 * if the tcp->tcp_cork option is set, then we have to force 19341 * TCP not to send partial segment (smaller than MSS bytes). 19342 * We are calculating the usable now based on full mss and 19343 * will save the rest of remaining data for later. 19344 */ 19345 if (usable < mss) 19346 goto done; 19347 usable = (usable / mss) * mss; 19348 } 19349 19350 /* Update the latest receive window size in TCP header. */ 19351 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19352 tcp->tcp_tcph->th_win); 19353 19354 /* 19355 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19356 * 19357 * 1. Simple TCP/IP{v4,v6} (no options). 19358 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19359 * 3. If the TCP connection is in ESTABLISHED state. 19360 * 4. The TCP is not detached. 19361 * 19362 * If any of the above conditions have changed during the 19363 * connection, stop using LSO/MDT and restore the stream head 19364 * parameters accordingly. 19365 */ 19366 ipst = tcps->tcps_netstack->netstack_ip; 19367 19368 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19369 ((tcp->tcp_ipversion == IPV4_VERSION && 19370 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19371 (tcp->tcp_ipversion == IPV6_VERSION && 19372 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19373 tcp->tcp_state != TCPS_ESTABLISHED || 19374 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19375 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19376 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19377 if (tcp->tcp_lso) { 19378 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19379 tcp->tcp_lso = B_FALSE; 19380 } else { 19381 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19382 tcp->tcp_mdt = B_FALSE; 19383 } 19384 19385 /* Anything other than detached is considered pathological */ 19386 if (!TCP_IS_DETACHED(tcp)) { 19387 if (tcp->tcp_lso) 19388 TCP_STAT(tcps, tcp_lso_disabled); 19389 else 19390 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19391 (void) tcp_maxpsz_set(tcp, B_TRUE); 19392 } 19393 } 19394 19395 /* Use MDT if sendable amount is greater than the threshold */ 19396 if (tcp->tcp_mdt && 19397 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19398 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19399 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19400 (tcp->tcp_valid_bits == 0 || 19401 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19402 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19403 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19404 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19405 local_time, mdt_thres); 19406 } else { 19407 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19408 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19409 local_time, INT_MAX); 19410 } 19411 19412 /* Pretend that all we were trying to send really got sent */ 19413 if (rc < 0 && tail_unsent < 0) { 19414 do { 19415 xmit_tail = xmit_tail->b_cont; 19416 xmit_tail->b_prev = local_time; 19417 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19418 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19419 tail_unsent += (int)(xmit_tail->b_wptr - 19420 xmit_tail->b_rptr); 19421 } while (tail_unsent < 0); 19422 } 19423 done:; 19424 tcp->tcp_xmit_tail = xmit_tail; 19425 tcp->tcp_xmit_tail_unsent = tail_unsent; 19426 len = tcp->tcp_snxt - snxt; 19427 if (len) { 19428 /* 19429 * If new data was sent, need to update the notsack 19430 * list, which is, afterall, data blocks that have 19431 * not been sack'ed by the receiver. New data is 19432 * not sack'ed. 19433 */ 19434 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19435 /* len is a negative value. */ 19436 tcp->tcp_pipe -= len; 19437 tcp_notsack_update(&(tcp->tcp_notsack_list), 19438 tcp->tcp_snxt, snxt, 19439 &(tcp->tcp_num_notsack_blk), 19440 &(tcp->tcp_cnt_notsack_list)); 19441 } 19442 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19443 tcp->tcp_rack = tcp->tcp_rnxt; 19444 tcp->tcp_rack_cnt = 0; 19445 if ((snxt + len) == tcp->tcp_suna) { 19446 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19447 } 19448 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19449 /* 19450 * Didn't send anything. Make sure the timer is running 19451 * so that we will probe a zero window. 19452 */ 19453 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19454 } 19455 /* Note that len is the amount we just sent but with a negative sign */ 19456 tcp->tcp_unsent += len; 19457 mutex_enter(&tcp->tcp_non_sq_lock); 19458 if (tcp->tcp_flow_stopped) { 19459 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19460 tcp_clrqfull(tcp); 19461 } 19462 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19463 tcp_setqfull(tcp); 19464 } 19465 mutex_exit(&tcp->tcp_non_sq_lock); 19466 } 19467 19468 /* 19469 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19470 * outgoing TCP header with the template header, as well as other 19471 * options such as time-stamp, ECN and/or SACK. 19472 */ 19473 static void 19474 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19475 { 19476 tcph_t *tcp_tmpl, *tcp_h; 19477 uint32_t *dst, *src; 19478 int hdrlen; 19479 19480 ASSERT(OK_32PTR(rptr)); 19481 19482 /* Template header */ 19483 tcp_tmpl = tcp->tcp_tcph; 19484 19485 /* Header of outgoing packet */ 19486 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19487 19488 /* dst and src are opaque 32-bit fields, used for copying */ 19489 dst = (uint32_t *)rptr; 19490 src = (uint32_t *)tcp->tcp_iphc; 19491 hdrlen = tcp->tcp_hdr_len; 19492 19493 /* Fill time-stamp option if needed */ 19494 if (tcp->tcp_snd_ts_ok) { 19495 U32_TO_BE32((uint32_t)now, 19496 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19497 U32_TO_BE32(tcp->tcp_ts_recent, 19498 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19499 } else { 19500 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19501 } 19502 19503 /* 19504 * Copy the template header; is this really more efficient than 19505 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19506 * but perhaps not for other scenarios. 19507 */ 19508 dst[0] = src[0]; 19509 dst[1] = src[1]; 19510 dst[2] = src[2]; 19511 dst[3] = src[3]; 19512 dst[4] = src[4]; 19513 dst[5] = src[5]; 19514 dst[6] = src[6]; 19515 dst[7] = src[7]; 19516 dst[8] = src[8]; 19517 dst[9] = src[9]; 19518 if (hdrlen -= 40) { 19519 hdrlen >>= 2; 19520 dst += 10; 19521 src += 10; 19522 do { 19523 *dst++ = *src++; 19524 } while (--hdrlen); 19525 } 19526 19527 /* 19528 * Set the ECN info in the TCP header if it is not a zero 19529 * window probe. Zero window probe is only sent in 19530 * tcp_wput_data() and tcp_timer(). 19531 */ 19532 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19533 SET_ECT(tcp, rptr); 19534 19535 if (tcp->tcp_ecn_echo_on) 19536 tcp_h->th_flags[0] |= TH_ECE; 19537 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19538 tcp_h->th_flags[0] |= TH_CWR; 19539 tcp->tcp_ecn_cwr_sent = B_TRUE; 19540 } 19541 } 19542 19543 /* Fill in SACK options */ 19544 if (num_sack_blk > 0) { 19545 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19546 sack_blk_t *tmp; 19547 int32_t i; 19548 19549 wptr[0] = TCPOPT_NOP; 19550 wptr[1] = TCPOPT_NOP; 19551 wptr[2] = TCPOPT_SACK; 19552 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19553 sizeof (sack_blk_t); 19554 wptr += TCPOPT_REAL_SACK_LEN; 19555 19556 tmp = tcp->tcp_sack_list; 19557 for (i = 0; i < num_sack_blk; i++) { 19558 U32_TO_BE32(tmp[i].begin, wptr); 19559 wptr += sizeof (tcp_seq); 19560 U32_TO_BE32(tmp[i].end, wptr); 19561 wptr += sizeof (tcp_seq); 19562 } 19563 tcp_h->th_offset_and_rsrvd[0] += 19564 ((num_sack_blk * 2 + 1) << 4); 19565 } 19566 } 19567 19568 /* 19569 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19570 * the destination address and SAP attribute, and if necessary, the 19571 * hardware checksum offload attribute to a Multidata message. 19572 */ 19573 static int 19574 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19575 const uint32_t start, const uint32_t stuff, const uint32_t end, 19576 const uint32_t flags, tcp_stack_t *tcps) 19577 { 19578 /* Add global destination address & SAP attribute */ 19579 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19580 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19581 "destination address+SAP\n")); 19582 19583 if (dlmp != NULL) 19584 TCP_STAT(tcps, tcp_mdt_allocfail); 19585 return (-1); 19586 } 19587 19588 /* Add global hwcksum attribute */ 19589 if (hwcksum && 19590 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19591 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19592 "checksum attribute\n")); 19593 19594 TCP_STAT(tcps, tcp_mdt_allocfail); 19595 return (-1); 19596 } 19597 19598 return (0); 19599 } 19600 19601 /* 19602 * Smaller and private version of pdescinfo_t used specifically for TCP, 19603 * which allows for only two payload spans per packet. 19604 */ 19605 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19606 19607 /* 19608 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19609 * scheme, and returns one the following: 19610 * 19611 * -1 = failed allocation. 19612 * 0 = success; burst count reached, or usable send window is too small, 19613 * and that we'd rather wait until later before sending again. 19614 */ 19615 static int 19616 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19617 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19618 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19619 const int mdt_thres) 19620 { 19621 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19622 multidata_t *mmd; 19623 uint_t obsegs, obbytes, hdr_frag_sz; 19624 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19625 int num_burst_seg, max_pld; 19626 pdesc_t *pkt; 19627 tcp_pdescinfo_t tcp_pkt_info; 19628 pdescinfo_t *pkt_info; 19629 int pbuf_idx, pbuf_idx_nxt; 19630 int seg_len, len, spill, af; 19631 boolean_t add_buffer, zcopy, clusterwide; 19632 boolean_t buf_trunked = B_FALSE; 19633 boolean_t rconfirm = B_FALSE; 19634 boolean_t done = B_FALSE; 19635 uint32_t cksum; 19636 uint32_t hwcksum_flags; 19637 ire_t *ire = NULL; 19638 ill_t *ill; 19639 ipha_t *ipha; 19640 ip6_t *ip6h; 19641 ipaddr_t src, dst; 19642 ill_zerocopy_capab_t *zc_cap = NULL; 19643 uint16_t *up; 19644 int err; 19645 conn_t *connp; 19646 mblk_t *mp, *mp1, *fw_mp_head = NULL; 19647 uchar_t *pld_start; 19648 tcp_stack_t *tcps = tcp->tcp_tcps; 19649 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19650 19651 #ifdef _BIG_ENDIAN 19652 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19653 #else 19654 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19655 #endif 19656 19657 #define PREP_NEW_MULTIDATA() { \ 19658 mmd = NULL; \ 19659 md_mp = md_hbuf = NULL; \ 19660 cur_hdr_off = 0; \ 19661 max_pld = tcp->tcp_mdt_max_pld; \ 19662 pbuf_idx = pbuf_idx_nxt = -1; \ 19663 add_buffer = B_TRUE; \ 19664 zcopy = B_FALSE; \ 19665 } 19666 19667 #define PREP_NEW_PBUF() { \ 19668 md_pbuf = md_pbuf_nxt = NULL; \ 19669 pbuf_idx = pbuf_idx_nxt = -1; \ 19670 cur_pld_off = 0; \ 19671 first_snxt = *snxt; \ 19672 ASSERT(*tail_unsent > 0); \ 19673 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19674 } 19675 19676 ASSERT(mdt_thres >= mss); 19677 ASSERT(*usable > 0 && *usable > mdt_thres); 19678 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19679 ASSERT(!TCP_IS_DETACHED(tcp)); 19680 ASSERT(tcp->tcp_valid_bits == 0 || 19681 tcp->tcp_valid_bits == TCP_FSS_VALID); 19682 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19683 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19684 (tcp->tcp_ipversion == IPV6_VERSION && 19685 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19686 19687 connp = tcp->tcp_connp; 19688 ASSERT(connp != NULL); 19689 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19690 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19691 19692 /* 19693 * Note that tcp will only declare at most 2 payload spans per 19694 * packet, which is much lower than the maximum allowable number 19695 * of packet spans per Multidata. For this reason, we use the 19696 * privately declared and smaller descriptor info structure, in 19697 * order to save some stack space. 19698 */ 19699 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19700 19701 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19702 if (af == AF_INET) { 19703 dst = tcp->tcp_ipha->ipha_dst; 19704 src = tcp->tcp_ipha->ipha_src; 19705 ASSERT(!CLASSD(dst)); 19706 } 19707 ASSERT(af == AF_INET || 19708 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19709 19710 obsegs = obbytes = 0; 19711 num_burst_seg = tcp->tcp_snd_burst; 19712 md_mp_head = NULL; 19713 PREP_NEW_MULTIDATA(); 19714 19715 /* 19716 * Before we go on further, make sure there is an IRE that we can 19717 * use, and that the ILL supports MDT. Otherwise, there's no point 19718 * in proceeding any further, and we should just hand everything 19719 * off to the legacy path. 19720 */ 19721 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19722 goto legacy_send_no_md; 19723 19724 ASSERT(ire != NULL); 19725 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19726 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19727 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19728 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19729 /* 19730 * If we do support loopback for MDT (which requires modifications 19731 * to the receiving paths), the following assertions should go away, 19732 * and we would be sending the Multidata to loopback conn later on. 19733 */ 19734 ASSERT(!IRE_IS_LOCAL(ire)); 19735 ASSERT(ire->ire_stq != NULL); 19736 19737 ill = ire_to_ill(ire); 19738 ASSERT(ill != NULL); 19739 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19740 19741 if (!tcp->tcp_ire_ill_check_done) { 19742 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19743 tcp->tcp_ire_ill_check_done = B_TRUE; 19744 } 19745 19746 /* 19747 * If the underlying interface conditions have changed, or if the 19748 * new interface does not support MDT, go back to legacy path. 19749 */ 19750 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19751 /* don't go through this path anymore for this connection */ 19752 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19753 tcp->tcp_mdt = B_FALSE; 19754 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19755 "interface %s\n", (void *)connp, ill->ill_name)); 19756 /* IRE will be released prior to returning */ 19757 goto legacy_send_no_md; 19758 } 19759 19760 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19761 zc_cap = ill->ill_zerocopy_capab; 19762 19763 /* 19764 * Check if we can take tcp fast-path. Note that "incomplete" 19765 * ire's (where the link-layer for next hop is not resolved 19766 * or where the fast-path header in nce_fp_mp is not available 19767 * yet) are sent down the legacy (slow) path. 19768 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19769 */ 19770 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19771 /* IRE will be released prior to returning */ 19772 goto legacy_send_no_md; 19773 } 19774 19775 /* go to legacy path if interface doesn't support zerocopy */ 19776 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19777 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19778 /* IRE will be released prior to returning */ 19779 goto legacy_send_no_md; 19780 } 19781 19782 /* does the interface support hardware checksum offload? */ 19783 hwcksum_flags = 0; 19784 if (ILL_HCKSUM_CAPABLE(ill) && 19785 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19786 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19787 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19788 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19789 HCKSUM_IPHDRCKSUM) 19790 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19791 19792 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19793 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19794 hwcksum_flags |= HCK_FULLCKSUM; 19795 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19796 HCKSUM_INET_PARTIAL) 19797 hwcksum_flags |= HCK_PARTIALCKSUM; 19798 } 19799 19800 /* 19801 * Each header fragment consists of the leading extra space, 19802 * followed by the TCP/IP header, and the trailing extra space. 19803 * We make sure that each header fragment begins on a 32-bit 19804 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19805 * aligned in tcp_mdt_update). 19806 */ 19807 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19808 tcp->tcp_mdt_hdr_tail), 4); 19809 19810 /* are we starting from the beginning of data block? */ 19811 if (*tail_unsent == 0) { 19812 *xmit_tail = (*xmit_tail)->b_cont; 19813 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19814 *tail_unsent = (int)MBLKL(*xmit_tail); 19815 } 19816 19817 /* 19818 * Here we create one or more Multidata messages, each made up of 19819 * one header buffer and up to N payload buffers. This entire 19820 * operation is done within two loops: 19821 * 19822 * The outer loop mostly deals with creating the Multidata message, 19823 * as well as the header buffer that gets added to it. It also 19824 * links the Multidata messages together such that all of them can 19825 * be sent down to the lower layer in a single putnext call; this 19826 * linking behavior depends on the tcp_mdt_chain tunable. 19827 * 19828 * The inner loop takes an existing Multidata message, and adds 19829 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19830 * packetizes those buffers by filling up the corresponding header 19831 * buffer fragments with the proper IP and TCP headers, and by 19832 * describing the layout of each packet in the packet descriptors 19833 * that get added to the Multidata. 19834 */ 19835 do { 19836 /* 19837 * If usable send window is too small, or data blocks in 19838 * transmit list are smaller than our threshold (i.e. app 19839 * performs large writes followed by small ones), we hand 19840 * off the control over to the legacy path. Note that we'll 19841 * get back the control once it encounters a large block. 19842 */ 19843 if (*usable < mss || (*tail_unsent <= mdt_thres && 19844 (*xmit_tail)->b_cont != NULL && 19845 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19846 /* send down what we've got so far */ 19847 if (md_mp_head != NULL) { 19848 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19849 obsegs, obbytes, &rconfirm); 19850 } 19851 /* 19852 * Pass control over to tcp_send(), but tell it to 19853 * return to us once a large-size transmission is 19854 * possible. 19855 */ 19856 TCP_STAT(tcps, tcp_mdt_legacy_small); 19857 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19858 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19859 tail_unsent, xmit_tail, local_time, 19860 mdt_thres)) <= 0) { 19861 /* burst count reached, or alloc failed */ 19862 IRE_REFRELE(ire); 19863 return (err); 19864 } 19865 19866 /* tcp_send() may have sent everything, so check */ 19867 if (*usable <= 0) { 19868 IRE_REFRELE(ire); 19869 return (0); 19870 } 19871 19872 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19873 /* 19874 * We may have delivered the Multidata, so make sure 19875 * to re-initialize before the next round. 19876 */ 19877 md_mp_head = NULL; 19878 obsegs = obbytes = 0; 19879 num_burst_seg = tcp->tcp_snd_burst; 19880 PREP_NEW_MULTIDATA(); 19881 19882 /* are we starting from the beginning of data block? */ 19883 if (*tail_unsent == 0) { 19884 *xmit_tail = (*xmit_tail)->b_cont; 19885 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19886 (uintptr_t)INT_MAX); 19887 *tail_unsent = (int)MBLKL(*xmit_tail); 19888 } 19889 } 19890 19891 /* 19892 * max_pld limits the number of mblks in tcp's transmit 19893 * queue that can be added to a Multidata message. Once 19894 * this counter reaches zero, no more additional mblks 19895 * can be added to it. What happens afterwards depends 19896 * on whether or not we are set to chain the Multidata 19897 * messages. If we are to link them together, reset 19898 * max_pld to its original value (tcp_mdt_max_pld) and 19899 * prepare to create a new Multidata message which will 19900 * get linked to md_mp_head. Else, leave it alone and 19901 * let the inner loop break on its own. 19902 */ 19903 if (tcp_mdt_chain && max_pld == 0) 19904 PREP_NEW_MULTIDATA(); 19905 19906 /* adding a payload buffer; re-initialize values */ 19907 if (add_buffer) 19908 PREP_NEW_PBUF(); 19909 19910 /* 19911 * If we don't have a Multidata, either because we just 19912 * (re)entered this outer loop, or after we branched off 19913 * to tcp_send above, setup the Multidata and header 19914 * buffer to be used. 19915 */ 19916 if (md_mp == NULL) { 19917 int md_hbuflen; 19918 uint32_t start, stuff; 19919 19920 /* 19921 * Calculate Multidata header buffer size large enough 19922 * to hold all of the headers that can possibly be 19923 * sent at this moment. We'd rather over-estimate 19924 * the size than running out of space; this is okay 19925 * since this buffer is small anyway. 19926 */ 19927 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19928 19929 /* 19930 * Start and stuff offset for partial hardware 19931 * checksum offload; these are currently for IPv4. 19932 * For full checksum offload, they are set to zero. 19933 */ 19934 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19935 if (af == AF_INET) { 19936 start = IP_SIMPLE_HDR_LENGTH; 19937 stuff = IP_SIMPLE_HDR_LENGTH + 19938 TCP_CHECKSUM_OFFSET; 19939 } else { 19940 start = IPV6_HDR_LEN; 19941 stuff = IPV6_HDR_LEN + 19942 TCP_CHECKSUM_OFFSET; 19943 } 19944 } else { 19945 start = stuff = 0; 19946 } 19947 19948 /* 19949 * Create the header buffer, Multidata, as well as 19950 * any necessary attributes (destination address, 19951 * SAP and hardware checksum offload) that should 19952 * be associated with the Multidata message. 19953 */ 19954 ASSERT(cur_hdr_off == 0); 19955 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19956 ((md_hbuf->b_wptr += md_hbuflen), 19957 (mmd = mmd_alloc(md_hbuf, &md_mp, 19958 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19959 /* fastpath mblk */ 19960 ire->ire_nce->nce_res_mp, 19961 /* hardware checksum enabled */ 19962 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19963 /* hardware checksum offsets */ 19964 start, stuff, 0, 19965 /* hardware checksum flag */ 19966 hwcksum_flags, tcps) != 0)) { 19967 legacy_send: 19968 if (md_mp != NULL) { 19969 /* Unlink message from the chain */ 19970 if (md_mp_head != NULL) { 19971 err = (intptr_t)rmvb(md_mp_head, 19972 md_mp); 19973 /* 19974 * We can't assert that rmvb 19975 * did not return -1, since we 19976 * may get here before linkb 19977 * happens. We do, however, 19978 * check if we just removed the 19979 * only element in the list. 19980 */ 19981 if (err == 0) 19982 md_mp_head = NULL; 19983 } 19984 /* md_hbuf gets freed automatically */ 19985 TCP_STAT(tcps, tcp_mdt_discarded); 19986 freeb(md_mp); 19987 } else { 19988 /* Either allocb or mmd_alloc failed */ 19989 TCP_STAT(tcps, tcp_mdt_allocfail); 19990 if (md_hbuf != NULL) 19991 freeb(md_hbuf); 19992 } 19993 19994 /* send down what we've got so far */ 19995 if (md_mp_head != NULL) { 19996 tcp_multisend_data(tcp, ire, ill, 19997 md_mp_head, obsegs, obbytes, 19998 &rconfirm); 19999 } 20000 legacy_send_no_md: 20001 if (ire != NULL) 20002 IRE_REFRELE(ire); 20003 /* 20004 * Too bad; let the legacy path handle this. 20005 * We specify INT_MAX for the threshold, since 20006 * we gave up with the Multidata processings 20007 * and let the old path have it all. 20008 */ 20009 TCP_STAT(tcps, tcp_mdt_legacy_all); 20010 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20011 tcp_tcp_hdr_len, num_sack_blk, usable, 20012 snxt, tail_unsent, xmit_tail, local_time, 20013 INT_MAX)); 20014 } 20015 20016 /* link to any existing ones, if applicable */ 20017 TCP_STAT(tcps, tcp_mdt_allocd); 20018 if (md_mp_head == NULL) { 20019 md_mp_head = md_mp; 20020 } else if (tcp_mdt_chain) { 20021 TCP_STAT(tcps, tcp_mdt_linked); 20022 linkb(md_mp_head, md_mp); 20023 } 20024 } 20025 20026 ASSERT(md_mp_head != NULL); 20027 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20028 ASSERT(md_mp != NULL && mmd != NULL); 20029 ASSERT(md_hbuf != NULL); 20030 20031 /* 20032 * Packetize the transmittable portion of the data block; 20033 * each data block is essentially added to the Multidata 20034 * as a payload buffer. We also deal with adding more 20035 * than one payload buffers, which happens when the remaining 20036 * packetized portion of the current payload buffer is less 20037 * than MSS, while the next data block in transmit queue 20038 * has enough data to make up for one. This "spillover" 20039 * case essentially creates a split-packet, where portions 20040 * of the packet's payload fragments may span across two 20041 * virtually discontiguous address blocks. 20042 */ 20043 seg_len = mss; 20044 do { 20045 len = seg_len; 20046 20047 ASSERT(len > 0); 20048 ASSERT(max_pld >= 0); 20049 ASSERT(!add_buffer || cur_pld_off == 0); 20050 20051 /* 20052 * First time around for this payload buffer; note 20053 * in the case of a spillover, the following has 20054 * been done prior to adding the split-packet 20055 * descriptor to Multidata, and we don't want to 20056 * repeat the process. 20057 */ 20058 if (add_buffer) { 20059 ASSERT(mmd != NULL); 20060 ASSERT(md_pbuf == NULL); 20061 ASSERT(md_pbuf_nxt == NULL); 20062 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20063 20064 /* 20065 * Have we reached the limit? We'd get to 20066 * this case when we're not chaining the 20067 * Multidata messages together, and since 20068 * we're done, terminate this loop. 20069 */ 20070 if (max_pld == 0) 20071 break; /* done */ 20072 20073 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20074 TCP_STAT(tcps, tcp_mdt_allocfail); 20075 goto legacy_send; /* out_of_mem */ 20076 } 20077 20078 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20079 zc_cap != NULL) { 20080 if (!ip_md_zcopy_attr(mmd, NULL, 20081 zc_cap->ill_zerocopy_flags)) { 20082 freeb(md_pbuf); 20083 TCP_STAT(tcps, 20084 tcp_mdt_allocfail); 20085 /* out_of_mem */ 20086 goto legacy_send; 20087 } 20088 zcopy = B_TRUE; 20089 } 20090 20091 md_pbuf->b_rptr += base_pld_off; 20092 20093 /* 20094 * Add a payload buffer to the Multidata; this 20095 * operation must not fail, or otherwise our 20096 * logic in this routine is broken. There 20097 * is no memory allocation done by the 20098 * routine, so any returned failure simply 20099 * tells us that we've done something wrong. 20100 * 20101 * A failure tells us that either we're adding 20102 * the same payload buffer more than once, or 20103 * we're trying to add more buffers than 20104 * allowed (max_pld calculation is wrong). 20105 * None of the above cases should happen, and 20106 * we panic because either there's horrible 20107 * heap corruption, and/or programming mistake. 20108 */ 20109 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20110 if (pbuf_idx < 0) { 20111 cmn_err(CE_PANIC, "tcp_multisend: " 20112 "payload buffer logic error " 20113 "detected for tcp %p mmd %p " 20114 "pbuf %p (%d)\n", 20115 (void *)tcp, (void *)mmd, 20116 (void *)md_pbuf, pbuf_idx); 20117 } 20118 20119 ASSERT(max_pld > 0); 20120 --max_pld; 20121 add_buffer = B_FALSE; 20122 } 20123 20124 ASSERT(md_mp_head != NULL); 20125 ASSERT(md_pbuf != NULL); 20126 ASSERT(md_pbuf_nxt == NULL); 20127 ASSERT(pbuf_idx != -1); 20128 ASSERT(pbuf_idx_nxt == -1); 20129 ASSERT(*usable > 0); 20130 20131 /* 20132 * We spillover to the next payload buffer only 20133 * if all of the following is true: 20134 * 20135 * 1. There is not enough data on the current 20136 * payload buffer to make up `len', 20137 * 2. We are allowed to send `len', 20138 * 3. The next payload buffer length is large 20139 * enough to accomodate `spill'. 20140 */ 20141 if ((spill = len - *tail_unsent) > 0 && 20142 *usable >= len && 20143 MBLKL((*xmit_tail)->b_cont) >= spill && 20144 max_pld > 0) { 20145 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20146 if (md_pbuf_nxt == NULL) { 20147 TCP_STAT(tcps, tcp_mdt_allocfail); 20148 goto legacy_send; /* out_of_mem */ 20149 } 20150 20151 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20152 zc_cap != NULL) { 20153 if (!ip_md_zcopy_attr(mmd, NULL, 20154 zc_cap->ill_zerocopy_flags)) { 20155 freeb(md_pbuf_nxt); 20156 TCP_STAT(tcps, 20157 tcp_mdt_allocfail); 20158 /* out_of_mem */ 20159 goto legacy_send; 20160 } 20161 zcopy = B_TRUE; 20162 } 20163 20164 /* 20165 * See comments above on the first call to 20166 * mmd_addpldbuf for explanation on the panic. 20167 */ 20168 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20169 if (pbuf_idx_nxt < 0) { 20170 panic("tcp_multisend: " 20171 "next payload buffer logic error " 20172 "detected for tcp %p mmd %p " 20173 "pbuf %p (%d)\n", 20174 (void *)tcp, (void *)mmd, 20175 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20176 } 20177 20178 ASSERT(max_pld > 0); 20179 --max_pld; 20180 } else if (spill > 0) { 20181 /* 20182 * If there's a spillover, but the following 20183 * xmit_tail couldn't give us enough octets 20184 * to reach "len", then stop the current 20185 * Multidata creation and let the legacy 20186 * tcp_send() path take over. We don't want 20187 * to send the tiny segment as part of this 20188 * Multidata for performance reasons; instead, 20189 * we let the legacy path deal with grouping 20190 * it with the subsequent small mblks. 20191 */ 20192 if (*usable >= len && 20193 MBLKL((*xmit_tail)->b_cont) < spill) { 20194 max_pld = 0; 20195 break; /* done */ 20196 } 20197 20198 /* 20199 * We can't spillover, and we are near 20200 * the end of the current payload buffer, 20201 * so send what's left. 20202 */ 20203 ASSERT(*tail_unsent > 0); 20204 len = *tail_unsent; 20205 } 20206 20207 /* tail_unsent is negated if there is a spillover */ 20208 *tail_unsent -= len; 20209 *usable -= len; 20210 ASSERT(*usable >= 0); 20211 20212 if (*usable < mss) 20213 seg_len = *usable; 20214 /* 20215 * Sender SWS avoidance; see comments in tcp_send(); 20216 * everything else is the same, except that we only 20217 * do this here if there is no more data to be sent 20218 * following the current xmit_tail. We don't check 20219 * for 1-byte urgent data because we shouldn't get 20220 * here if TCP_URG_VALID is set. 20221 */ 20222 if (*usable > 0 && *usable < mss && 20223 ((md_pbuf_nxt == NULL && 20224 (*xmit_tail)->b_cont == NULL) || 20225 (md_pbuf_nxt != NULL && 20226 (*xmit_tail)->b_cont->b_cont == NULL)) && 20227 seg_len < (tcp->tcp_max_swnd >> 1) && 20228 (tcp->tcp_unsent - 20229 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20230 !tcp->tcp_zero_win_probe) { 20231 if ((*snxt + len) == tcp->tcp_snxt && 20232 (*snxt + len) == tcp->tcp_suna) { 20233 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20234 } 20235 done = B_TRUE; 20236 } 20237 20238 /* 20239 * Prime pump for IP's checksumming on our behalf; 20240 * include the adjustment for a source route if any. 20241 * Do this only for software/partial hardware checksum 20242 * offload, as this field gets zeroed out later for 20243 * the full hardware checksum offload case. 20244 */ 20245 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20246 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20247 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20248 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20249 } 20250 20251 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20252 *snxt += len; 20253 20254 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20255 /* 20256 * We set the PUSH bit only if TCP has no more buffered 20257 * data to be transmitted (or if sender SWS avoidance 20258 * takes place), as opposed to setting it for every 20259 * last packet in the burst. 20260 */ 20261 if (done || 20262 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20263 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20264 20265 /* 20266 * Set FIN bit if this is our last segment; snxt 20267 * already includes its length, and it will not 20268 * be adjusted after this point. 20269 */ 20270 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20271 *snxt == tcp->tcp_fss) { 20272 if (!tcp->tcp_fin_acked) { 20273 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20274 BUMP_MIB(&tcps->tcps_mib, 20275 tcpOutControl); 20276 } 20277 if (!tcp->tcp_fin_sent) { 20278 tcp->tcp_fin_sent = B_TRUE; 20279 /* 20280 * tcp state must be ESTABLISHED 20281 * in order for us to get here in 20282 * the first place. 20283 */ 20284 tcp->tcp_state = TCPS_FIN_WAIT_1; 20285 20286 /* 20287 * Upon returning from this routine, 20288 * tcp_wput_data() will set tcp_snxt 20289 * to be equal to snxt + tcp_fin_sent. 20290 * This is essentially the same as 20291 * setting it to tcp_fss + 1. 20292 */ 20293 } 20294 } 20295 20296 tcp->tcp_last_sent_len = (ushort_t)len; 20297 20298 len += tcp_hdr_len; 20299 if (tcp->tcp_ipversion == IPV4_VERSION) 20300 tcp->tcp_ipha->ipha_length = htons(len); 20301 else 20302 tcp->tcp_ip6h->ip6_plen = htons(len - 20303 ((char *)&tcp->tcp_ip6h[1] - 20304 tcp->tcp_iphc)); 20305 20306 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20307 20308 /* setup header fragment */ 20309 PDESC_HDR_ADD(pkt_info, 20310 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20311 tcp->tcp_mdt_hdr_head, /* head room */ 20312 tcp_hdr_len, /* len */ 20313 tcp->tcp_mdt_hdr_tail); /* tail room */ 20314 20315 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20316 hdr_frag_sz); 20317 ASSERT(MBLKIN(md_hbuf, 20318 (pkt_info->hdr_base - md_hbuf->b_rptr), 20319 PDESC_HDRSIZE(pkt_info))); 20320 20321 /* setup first payload fragment */ 20322 PDESC_PLD_INIT(pkt_info); 20323 PDESC_PLD_SPAN_ADD(pkt_info, 20324 pbuf_idx, /* index */ 20325 md_pbuf->b_rptr + cur_pld_off, /* start */ 20326 tcp->tcp_last_sent_len); /* len */ 20327 20328 /* create a split-packet in case of a spillover */ 20329 if (md_pbuf_nxt != NULL) { 20330 ASSERT(spill > 0); 20331 ASSERT(pbuf_idx_nxt > pbuf_idx); 20332 ASSERT(!add_buffer); 20333 20334 md_pbuf = md_pbuf_nxt; 20335 md_pbuf_nxt = NULL; 20336 pbuf_idx = pbuf_idx_nxt; 20337 pbuf_idx_nxt = -1; 20338 cur_pld_off = spill; 20339 20340 /* trim out first payload fragment */ 20341 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20342 20343 /* setup second payload fragment */ 20344 PDESC_PLD_SPAN_ADD(pkt_info, 20345 pbuf_idx, /* index */ 20346 md_pbuf->b_rptr, /* start */ 20347 spill); /* len */ 20348 20349 if ((*xmit_tail)->b_next == NULL) { 20350 /* 20351 * Store the lbolt used for RTT 20352 * estimation. We can only record one 20353 * timestamp per mblk so we do it when 20354 * we reach the end of the payload 20355 * buffer. Also we only take a new 20356 * timestamp sample when the previous 20357 * timed data from the same mblk has 20358 * been ack'ed. 20359 */ 20360 (*xmit_tail)->b_prev = local_time; 20361 (*xmit_tail)->b_next = 20362 (mblk_t *)(uintptr_t)first_snxt; 20363 } 20364 20365 first_snxt = *snxt - spill; 20366 20367 /* 20368 * Advance xmit_tail; usable could be 0 by 20369 * the time we got here, but we made sure 20370 * above that we would only spillover to 20371 * the next data block if usable includes 20372 * the spilled-over amount prior to the 20373 * subtraction. Therefore, we are sure 20374 * that xmit_tail->b_cont can't be NULL. 20375 */ 20376 ASSERT((*xmit_tail)->b_cont != NULL); 20377 *xmit_tail = (*xmit_tail)->b_cont; 20378 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20379 (uintptr_t)INT_MAX); 20380 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20381 } else { 20382 cur_pld_off += tcp->tcp_last_sent_len; 20383 } 20384 20385 /* 20386 * Fill in the header using the template header, and 20387 * add options such as time-stamp, ECN and/or SACK, 20388 * as needed. 20389 */ 20390 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20391 (clock_t)local_time, num_sack_blk); 20392 20393 /* take care of some IP header businesses */ 20394 if (af == AF_INET) { 20395 ipha = (ipha_t *)pkt_info->hdr_rptr; 20396 20397 ASSERT(OK_32PTR((uchar_t *)ipha)); 20398 ASSERT(PDESC_HDRL(pkt_info) >= 20399 IP_SIMPLE_HDR_LENGTH); 20400 ASSERT(ipha->ipha_version_and_hdr_length == 20401 IP_SIMPLE_HDR_VERSION); 20402 20403 /* 20404 * Assign ident value for current packet; see 20405 * related comments in ip_wput_ire() about the 20406 * contract private interface with clustering 20407 * group. 20408 */ 20409 clusterwide = B_FALSE; 20410 if (cl_inet_ipident != NULL) { 20411 ASSERT(cl_inet_isclusterwide != NULL); 20412 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20413 AF_INET, 20414 (uint8_t *)(uintptr_t)src)) { 20415 ipha->ipha_ident = 20416 (*cl_inet_ipident) 20417 (IPPROTO_IP, AF_INET, 20418 (uint8_t *)(uintptr_t)src, 20419 (uint8_t *)(uintptr_t)dst); 20420 clusterwide = B_TRUE; 20421 } 20422 } 20423 20424 if (!clusterwide) { 20425 ipha->ipha_ident = (uint16_t) 20426 atomic_add_32_nv( 20427 &ire->ire_ident, 1); 20428 } 20429 #ifndef _BIG_ENDIAN 20430 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20431 (ipha->ipha_ident >> 8); 20432 #endif 20433 } else { 20434 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20435 20436 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20437 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20438 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20439 ASSERT(PDESC_HDRL(pkt_info) >= 20440 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20441 TCP_CHECKSUM_SIZE)); 20442 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20443 20444 if (tcp->tcp_ip_forward_progress) { 20445 rconfirm = B_TRUE; 20446 tcp->tcp_ip_forward_progress = B_FALSE; 20447 } 20448 } 20449 20450 /* at least one payload span, and at most two */ 20451 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20452 20453 /* add the packet descriptor to Multidata */ 20454 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20455 KM_NOSLEEP)) == NULL) { 20456 /* 20457 * Any failure other than ENOMEM indicates 20458 * that we have passed in invalid pkt_info 20459 * or parameters to mmd_addpdesc, which must 20460 * not happen. 20461 * 20462 * EINVAL is a result of failure on boundary 20463 * checks against the pkt_info contents. It 20464 * should not happen, and we panic because 20465 * either there's horrible heap corruption, 20466 * and/or programming mistake. 20467 */ 20468 if (err != ENOMEM) { 20469 cmn_err(CE_PANIC, "tcp_multisend: " 20470 "pdesc logic error detected for " 20471 "tcp %p mmd %p pinfo %p (%d)\n", 20472 (void *)tcp, (void *)mmd, 20473 (void *)pkt_info, err); 20474 } 20475 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20476 goto legacy_send; /* out_of_mem */ 20477 } 20478 ASSERT(pkt != NULL); 20479 20480 /* calculate IP header and TCP checksums */ 20481 if (af == AF_INET) { 20482 /* calculate pseudo-header checksum */ 20483 cksum = (dst >> 16) + (dst & 0xFFFF) + 20484 (src >> 16) + (src & 0xFFFF); 20485 20486 /* offset for TCP header checksum */ 20487 up = IPH_TCPH_CHECKSUMP(ipha, 20488 IP_SIMPLE_HDR_LENGTH); 20489 } else { 20490 up = (uint16_t *)&ip6h->ip6_src; 20491 20492 /* calculate pseudo-header checksum */ 20493 cksum = up[0] + up[1] + up[2] + up[3] + 20494 up[4] + up[5] + up[6] + up[7] + 20495 up[8] + up[9] + up[10] + up[11] + 20496 up[12] + up[13] + up[14] + up[15]; 20497 20498 /* Fold the initial sum */ 20499 cksum = (cksum & 0xffff) + (cksum >> 16); 20500 20501 up = (uint16_t *)(((uchar_t *)ip6h) + 20502 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20503 } 20504 20505 if (hwcksum_flags & HCK_FULLCKSUM) { 20506 /* clear checksum field for hardware */ 20507 *up = 0; 20508 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20509 uint32_t sum; 20510 20511 /* pseudo-header checksumming */ 20512 sum = *up + cksum + IP_TCP_CSUM_COMP; 20513 sum = (sum & 0xFFFF) + (sum >> 16); 20514 *up = (sum & 0xFFFF) + (sum >> 16); 20515 } else { 20516 /* software checksumming */ 20517 TCP_STAT(tcps, tcp_out_sw_cksum); 20518 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20519 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20520 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20521 cksum + IP_TCP_CSUM_COMP); 20522 if (*up == 0) 20523 *up = 0xFFFF; 20524 } 20525 20526 /* IPv4 header checksum */ 20527 if (af == AF_INET) { 20528 ipha->ipha_fragment_offset_and_flags |= 20529 (uint32_t)htons(ire->ire_frag_flag); 20530 20531 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20532 ipha->ipha_hdr_checksum = 0; 20533 } else { 20534 IP_HDR_CKSUM(ipha, cksum, 20535 ((uint32_t *)ipha)[0], 20536 ((uint16_t *)ipha)[4]); 20537 } 20538 } 20539 20540 if (af == AF_INET && 20541 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20542 af == AF_INET6 && 20543 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20544 /* build header(IP/TCP) mblk for this segment */ 20545 if ((mp = dupb(md_hbuf)) == NULL) 20546 goto legacy_send; 20547 20548 mp->b_rptr = pkt_info->hdr_rptr; 20549 mp->b_wptr = pkt_info->hdr_wptr; 20550 20551 /* build payload mblk for this segment */ 20552 if ((mp1 = dupb(*xmit_tail)) == NULL) { 20553 freemsg(mp); 20554 goto legacy_send; 20555 } 20556 mp1->b_wptr = md_pbuf->b_rptr + cur_pld_off; 20557 mp1->b_rptr = mp1->b_wptr - 20558 tcp->tcp_last_sent_len; 20559 linkb(mp, mp1); 20560 20561 pld_start = mp1->b_rptr; 20562 20563 if (af == AF_INET) { 20564 DTRACE_PROBE4( 20565 ip4__physical__out__start, 20566 ill_t *, NULL, 20567 ill_t *, ill, 20568 ipha_t *, ipha, 20569 mblk_t *, mp); 20570 FW_HOOKS( 20571 ipst->ips_ip4_physical_out_event, 20572 ipst->ips_ipv4firewall_physical_out, 20573 NULL, ill, ipha, mp, mp, ipst); 20574 DTRACE_PROBE1( 20575 ip4__physical__out__end, 20576 mblk_t *, mp); 20577 } else { 20578 DTRACE_PROBE4( 20579 ip6__physical__out_start, 20580 ill_t *, NULL, 20581 ill_t *, ill, 20582 ip6_t *, ip6h, 20583 mblk_t *, mp); 20584 FW_HOOKS6( 20585 ipst->ips_ip6_physical_out_event, 20586 ipst->ips_ipv6firewall_physical_out, 20587 NULL, ill, ip6h, mp, mp, ipst); 20588 DTRACE_PROBE1( 20589 ip6__physical__out__end, 20590 mblk_t *, mp); 20591 } 20592 20593 if (buf_trunked && mp != NULL) { 20594 /* 20595 * Need to pass it to normal path. 20596 */ 20597 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20598 } else if (mp == NULL || 20599 mp->b_rptr != pkt_info->hdr_rptr || 20600 mp->b_wptr != pkt_info->hdr_wptr || 20601 (mp1 = mp->b_cont) == NULL || 20602 mp1->b_rptr != pld_start || 20603 mp1->b_wptr != pld_start + 20604 tcp->tcp_last_sent_len || 20605 mp1->b_cont != NULL) { 20606 /* 20607 * Need to pass all packets of this 20608 * buffer to normal path, either when 20609 * packet is blocked, or when boundary 20610 * of header buffer or payload buffer 20611 * has been changed by FW_HOOKS[6]. 20612 */ 20613 buf_trunked = B_TRUE; 20614 if (md_mp_head != NULL) { 20615 err = (intptr_t)rmvb(md_mp_head, 20616 md_mp); 20617 if (err == 0) 20618 md_mp_head = NULL; 20619 } 20620 20621 /* send down what we've got so far */ 20622 if (md_mp_head != NULL) { 20623 tcp_multisend_data(tcp, ire, 20624 ill, md_mp_head, obsegs, 20625 obbytes, &rconfirm); 20626 } 20627 md_mp_head = NULL; 20628 20629 if (mp != NULL) 20630 CALL_IP_WPUT(tcp->tcp_connp, 20631 q, mp); 20632 20633 mp1 = fw_mp_head; 20634 do { 20635 mp = mp1; 20636 mp1 = mp1->b_next; 20637 mp->b_next = NULL; 20638 mp->b_prev = NULL; 20639 CALL_IP_WPUT(tcp->tcp_connp, 20640 q, mp); 20641 } while (mp1 != NULL); 20642 20643 fw_mp_head = NULL; 20644 } else { 20645 if (fw_mp_head == NULL) 20646 fw_mp_head = mp; 20647 else 20648 fw_mp_head->b_prev->b_next = mp; 20649 fw_mp_head->b_prev = mp; 20650 } 20651 } 20652 20653 /* advance header offset */ 20654 cur_hdr_off += hdr_frag_sz; 20655 20656 obbytes += tcp->tcp_last_sent_len; 20657 ++obsegs; 20658 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20659 *tail_unsent > 0); 20660 20661 if ((*xmit_tail)->b_next == NULL) { 20662 /* 20663 * Store the lbolt used for RTT estimation. We can only 20664 * record one timestamp per mblk so we do it when we 20665 * reach the end of the payload buffer. Also we only 20666 * take a new timestamp sample when the previous timed 20667 * data from the same mblk has been ack'ed. 20668 */ 20669 (*xmit_tail)->b_prev = local_time; 20670 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20671 } 20672 20673 ASSERT(*tail_unsent >= 0); 20674 if (*tail_unsent > 0) { 20675 /* 20676 * We got here because we broke out of the above 20677 * loop due to of one of the following cases: 20678 * 20679 * 1. len < adjusted MSS (i.e. small), 20680 * 2. Sender SWS avoidance, 20681 * 3. max_pld is zero. 20682 * 20683 * We are done for this Multidata, so trim our 20684 * last payload buffer (if any) accordingly. 20685 */ 20686 if (md_pbuf != NULL) 20687 md_pbuf->b_wptr -= *tail_unsent; 20688 } else if (*usable > 0) { 20689 *xmit_tail = (*xmit_tail)->b_cont; 20690 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20691 (uintptr_t)INT_MAX); 20692 *tail_unsent = (int)MBLKL(*xmit_tail); 20693 add_buffer = B_TRUE; 20694 } 20695 20696 while (fw_mp_head) { 20697 mp = fw_mp_head; 20698 fw_mp_head = fw_mp_head->b_next; 20699 mp->b_prev = mp->b_next = NULL; 20700 freemsg(mp); 20701 } 20702 if (buf_trunked) { 20703 TCP_STAT(tcps, tcp_mdt_discarded); 20704 freeb(md_mp); 20705 buf_trunked = B_FALSE; 20706 } 20707 } while (!done && *usable > 0 && num_burst_seg > 0 && 20708 (tcp_mdt_chain || max_pld > 0)); 20709 20710 if (md_mp_head != NULL) { 20711 /* send everything down */ 20712 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20713 &rconfirm); 20714 } 20715 20716 #undef PREP_NEW_MULTIDATA 20717 #undef PREP_NEW_PBUF 20718 #undef IPVER 20719 20720 IRE_REFRELE(ire); 20721 return (0); 20722 } 20723 20724 /* 20725 * A wrapper function for sending one or more Multidata messages down to 20726 * the module below ip; this routine does not release the reference of the 20727 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20728 */ 20729 static void 20730 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20731 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20732 { 20733 uint64_t delta; 20734 nce_t *nce; 20735 tcp_stack_t *tcps = tcp->tcp_tcps; 20736 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20737 20738 ASSERT(ire != NULL && ill != NULL); 20739 ASSERT(ire->ire_stq != NULL); 20740 ASSERT(md_mp_head != NULL); 20741 ASSERT(rconfirm != NULL); 20742 20743 /* adjust MIBs and IRE timestamp */ 20744 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 20745 tcp->tcp_obsegs += obsegs; 20746 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20747 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20748 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20749 20750 if (tcp->tcp_ipversion == IPV4_VERSION) { 20751 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20752 } else { 20753 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20754 } 20755 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20756 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20757 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20758 20759 ire->ire_ob_pkt_count += obsegs; 20760 if (ire->ire_ipif != NULL) 20761 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20762 ire->ire_last_used_time = lbolt; 20763 20764 /* send it down */ 20765 putnext(ire->ire_stq, md_mp_head); 20766 20767 /* we're done for TCP/IPv4 */ 20768 if (tcp->tcp_ipversion == IPV4_VERSION) 20769 return; 20770 20771 nce = ire->ire_nce; 20772 20773 ASSERT(nce != NULL); 20774 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20775 ASSERT(nce->nce_state != ND_INCOMPLETE); 20776 20777 /* reachability confirmation? */ 20778 if (*rconfirm) { 20779 nce->nce_last = TICK_TO_MSEC(lbolt64); 20780 if (nce->nce_state != ND_REACHABLE) { 20781 mutex_enter(&nce->nce_lock); 20782 nce->nce_state = ND_REACHABLE; 20783 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20784 mutex_exit(&nce->nce_lock); 20785 (void) untimeout(nce->nce_timeout_id); 20786 if (ip_debug > 2) { 20787 /* ip1dbg */ 20788 pr_addr_dbg("tcp_multisend_data: state " 20789 "for %s changed to REACHABLE\n", 20790 AF_INET6, &ire->ire_addr_v6); 20791 } 20792 } 20793 /* reset transport reachability confirmation */ 20794 *rconfirm = B_FALSE; 20795 } 20796 20797 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20798 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20799 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20800 20801 if (delta > (uint64_t)ill->ill_reachable_time) { 20802 mutex_enter(&nce->nce_lock); 20803 switch (nce->nce_state) { 20804 case ND_REACHABLE: 20805 case ND_STALE: 20806 /* 20807 * ND_REACHABLE is identical to ND_STALE in this 20808 * specific case. If reachable time has expired for 20809 * this neighbor (delta is greater than reachable 20810 * time), conceptually, the neighbor cache is no 20811 * longer in REACHABLE state, but already in STALE 20812 * state. So the correct transition here is to 20813 * ND_DELAY. 20814 */ 20815 nce->nce_state = ND_DELAY; 20816 mutex_exit(&nce->nce_lock); 20817 NDP_RESTART_TIMER(nce, 20818 ipst->ips_delay_first_probe_time); 20819 if (ip_debug > 3) { 20820 /* ip2dbg */ 20821 pr_addr_dbg("tcp_multisend_data: state " 20822 "for %s changed to DELAY\n", 20823 AF_INET6, &ire->ire_addr_v6); 20824 } 20825 break; 20826 case ND_DELAY: 20827 case ND_PROBE: 20828 mutex_exit(&nce->nce_lock); 20829 /* Timers have already started */ 20830 break; 20831 case ND_UNREACHABLE: 20832 /* 20833 * ndp timer has detected that this nce is 20834 * unreachable and initiated deleting this nce 20835 * and all its associated IREs. This is a race 20836 * where we found the ire before it was deleted 20837 * and have just sent out a packet using this 20838 * unreachable nce. 20839 */ 20840 mutex_exit(&nce->nce_lock); 20841 break; 20842 default: 20843 ASSERT(0); 20844 } 20845 } 20846 } 20847 20848 /* 20849 * Derived from tcp_send_data(). 20850 */ 20851 static void 20852 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20853 int num_lso_seg) 20854 { 20855 ipha_t *ipha; 20856 mblk_t *ire_fp_mp; 20857 uint_t ire_fp_mp_len; 20858 uint32_t hcksum_txflags = 0; 20859 ipaddr_t src; 20860 ipaddr_t dst; 20861 uint32_t cksum; 20862 uint16_t *up; 20863 tcp_stack_t *tcps = tcp->tcp_tcps; 20864 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20865 20866 ASSERT(DB_TYPE(mp) == M_DATA); 20867 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20868 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20869 ASSERT(tcp->tcp_connp != NULL); 20870 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20871 20872 ipha = (ipha_t *)mp->b_rptr; 20873 src = ipha->ipha_src; 20874 dst = ipha->ipha_dst; 20875 20876 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20877 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20878 num_lso_seg); 20879 #ifndef _BIG_ENDIAN 20880 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20881 #endif 20882 if (tcp->tcp_snd_zcopy_aware) { 20883 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20884 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20885 mp = tcp_zcopy_disable(tcp, mp); 20886 } 20887 20888 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20889 ASSERT(ill->ill_hcksum_capab != NULL); 20890 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20891 } 20892 20893 /* 20894 * Since the TCP checksum should be recalculated by h/w, we can just 20895 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20896 * pseudo-header checksum for HCK_PARTIALCKSUM. 20897 * The partial pseudo-header excludes TCP length, that was calculated 20898 * in tcp_send(), so to zero *up before further processing. 20899 */ 20900 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20901 20902 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20903 *up = 0; 20904 20905 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20906 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20907 20908 /* 20909 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 20910 */ 20911 DB_LSOFLAGS(mp) |= HW_LSO; 20912 DB_LSOMSS(mp) = mss; 20913 20914 ipha->ipha_fragment_offset_and_flags |= 20915 (uint32_t)htons(ire->ire_frag_flag); 20916 20917 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20918 ire_fp_mp_len = MBLKL(ire_fp_mp); 20919 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20920 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20921 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20922 20923 UPDATE_OB_PKT_COUNT(ire); 20924 ire->ire_last_used_time = lbolt; 20925 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20926 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20927 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20928 ntohs(ipha->ipha_length)); 20929 20930 if (ILL_DLS_CAPABLE(ill)) { 20931 /* 20932 * Send the packet directly to DLD, where it may be queued 20933 * depending on the availability of transmit resources at 20934 * the media layer. 20935 */ 20936 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 20937 } else { 20938 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 20939 DTRACE_PROBE4(ip4__physical__out__start, 20940 ill_t *, NULL, ill_t *, out_ill, 20941 ipha_t *, ipha, mblk_t *, mp); 20942 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20943 ipst->ips_ipv4firewall_physical_out, 20944 NULL, out_ill, ipha, mp, mp, ipst); 20945 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20946 if (mp != NULL) 20947 putnext(ire->ire_stq, mp); 20948 } 20949 } 20950 20951 /* 20952 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20953 * scheme, and returns one of the following: 20954 * 20955 * -1 = failed allocation. 20956 * 0 = success; burst count reached, or usable send window is too small, 20957 * and that we'd rather wait until later before sending again. 20958 * 1 = success; we are called from tcp_multisend(), and both usable send 20959 * window and tail_unsent are greater than the MDT threshold, and thus 20960 * Multidata Transmit should be used instead. 20961 */ 20962 static int 20963 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20964 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20965 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20966 const int mdt_thres) 20967 { 20968 int num_burst_seg = tcp->tcp_snd_burst; 20969 ire_t *ire = NULL; 20970 ill_t *ill = NULL; 20971 mblk_t *ire_fp_mp = NULL; 20972 uint_t ire_fp_mp_len = 0; 20973 int num_lso_seg = 1; 20974 uint_t lso_usable; 20975 boolean_t do_lso_send = B_FALSE; 20976 tcp_stack_t *tcps = tcp->tcp_tcps; 20977 20978 /* 20979 * Check LSO capability before any further work. And the similar check 20980 * need to be done in for(;;) loop. 20981 * LSO will be deployed when therer is more than one mss of available 20982 * data and a burst transmission is allowed. 20983 */ 20984 if (tcp->tcp_lso && 20985 (tcp->tcp_valid_bits == 0 || 20986 tcp->tcp_valid_bits == TCP_FSS_VALID) && 20987 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20988 /* 20989 * Try to find usable IRE/ILL and do basic check to the ILL. 20990 */ 20991 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 20992 /* 20993 * Enable LSO with this transmission. 20994 * Since IRE has been hold in 20995 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 20996 * should be called before return. 20997 */ 20998 do_lso_send = B_TRUE; 20999 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21000 ire_fp_mp_len = MBLKL(ire_fp_mp); 21001 /* Round up to multiple of 4 */ 21002 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21003 } else { 21004 do_lso_send = B_FALSE; 21005 ill = NULL; 21006 } 21007 } 21008 21009 for (;;) { 21010 struct datab *db; 21011 tcph_t *tcph; 21012 uint32_t sum; 21013 mblk_t *mp, *mp1; 21014 uchar_t *rptr; 21015 int len; 21016 21017 /* 21018 * If we're called by tcp_multisend(), and the amount of 21019 * sendable data as well as the size of current xmit_tail 21020 * is beyond the MDT threshold, return to the caller and 21021 * let the large data transmit be done using MDT. 21022 */ 21023 if (*usable > 0 && *usable > mdt_thres && 21024 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21025 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21026 ASSERT(tcp->tcp_mdt); 21027 return (1); /* success; do large send */ 21028 } 21029 21030 if (num_burst_seg == 0) 21031 break; /* success; burst count reached */ 21032 21033 /* 21034 * Calculate the maximum payload length we can send in *one* 21035 * time. 21036 */ 21037 if (do_lso_send) { 21038 /* 21039 * Check whether need to do LSO any more. 21040 */ 21041 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21042 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21043 lso_usable = MIN(lso_usable, 21044 num_burst_seg * mss); 21045 21046 num_lso_seg = lso_usable / mss; 21047 if (lso_usable % mss) { 21048 num_lso_seg++; 21049 tcp->tcp_last_sent_len = (ushort_t) 21050 (lso_usable % mss); 21051 } else { 21052 tcp->tcp_last_sent_len = (ushort_t)mss; 21053 } 21054 } else { 21055 do_lso_send = B_FALSE; 21056 num_lso_seg = 1; 21057 lso_usable = mss; 21058 } 21059 } 21060 21061 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21062 21063 /* 21064 * Adjust num_burst_seg here. 21065 */ 21066 num_burst_seg -= num_lso_seg; 21067 21068 len = mss; 21069 if (len > *usable) { 21070 ASSERT(do_lso_send == B_FALSE); 21071 21072 len = *usable; 21073 if (len <= 0) { 21074 /* Terminate the loop */ 21075 break; /* success; too small */ 21076 } 21077 /* 21078 * Sender silly-window avoidance. 21079 * Ignore this if we are going to send a 21080 * zero window probe out. 21081 * 21082 * TODO: force data into microscopic window? 21083 * ==> (!pushed || (unsent > usable)) 21084 */ 21085 if (len < (tcp->tcp_max_swnd >> 1) && 21086 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21087 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21088 len == 1) && (! tcp->tcp_zero_win_probe)) { 21089 /* 21090 * If the retransmit timer is not running 21091 * we start it so that we will retransmit 21092 * in the case when the the receiver has 21093 * decremented the window. 21094 */ 21095 if (*snxt == tcp->tcp_snxt && 21096 *snxt == tcp->tcp_suna) { 21097 /* 21098 * We are not supposed to send 21099 * anything. So let's wait a little 21100 * bit longer before breaking SWS 21101 * avoidance. 21102 * 21103 * What should the value be? 21104 * Suggestion: MAX(init rexmit time, 21105 * tcp->tcp_rto) 21106 */ 21107 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21108 } 21109 break; /* success; too small */ 21110 } 21111 } 21112 21113 tcph = tcp->tcp_tcph; 21114 21115 /* 21116 * The reason to adjust len here is that we need to set flags 21117 * and calculate checksum. 21118 */ 21119 if (do_lso_send) 21120 len = lso_usable; 21121 21122 *usable -= len; /* Approximate - can be adjusted later */ 21123 if (*usable > 0) 21124 tcph->th_flags[0] = TH_ACK; 21125 else 21126 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21127 21128 /* 21129 * Prime pump for IP's checksumming on our behalf 21130 * Include the adjustment for a source route if any. 21131 */ 21132 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21133 sum = (sum >> 16) + (sum & 0xFFFF); 21134 U16_TO_ABE16(sum, tcph->th_sum); 21135 21136 U32_TO_ABE32(*snxt, tcph->th_seq); 21137 21138 /* 21139 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21140 * set. For the case when TCP_FSS_VALID is the only valid 21141 * bit (normal active close), branch off only when we think 21142 * that the FIN flag needs to be set. Note for this case, 21143 * that (snxt + len) may not reflect the actual seg_len, 21144 * as len may be further reduced in tcp_xmit_mp(). If len 21145 * gets modified, we will end up here again. 21146 */ 21147 if (tcp->tcp_valid_bits != 0 && 21148 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21149 ((*snxt + len) == tcp->tcp_fss))) { 21150 uchar_t *prev_rptr; 21151 uint32_t prev_snxt = tcp->tcp_snxt; 21152 21153 if (*tail_unsent == 0) { 21154 ASSERT((*xmit_tail)->b_cont != NULL); 21155 *xmit_tail = (*xmit_tail)->b_cont; 21156 prev_rptr = (*xmit_tail)->b_rptr; 21157 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21158 (*xmit_tail)->b_rptr); 21159 } else { 21160 prev_rptr = (*xmit_tail)->b_rptr; 21161 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21162 *tail_unsent; 21163 } 21164 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21165 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21166 /* Restore tcp_snxt so we get amount sent right. */ 21167 tcp->tcp_snxt = prev_snxt; 21168 if (prev_rptr == (*xmit_tail)->b_rptr) { 21169 /* 21170 * If the previous timestamp is still in use, 21171 * don't stomp on it. 21172 */ 21173 if ((*xmit_tail)->b_next == NULL) { 21174 (*xmit_tail)->b_prev = local_time; 21175 (*xmit_tail)->b_next = 21176 (mblk_t *)(uintptr_t)(*snxt); 21177 } 21178 } else 21179 (*xmit_tail)->b_rptr = prev_rptr; 21180 21181 if (mp == NULL) { 21182 if (ire != NULL) 21183 IRE_REFRELE(ire); 21184 return (-1); 21185 } 21186 mp1 = mp->b_cont; 21187 21188 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21189 tcp->tcp_last_sent_len = (ushort_t)len; 21190 while (mp1->b_cont) { 21191 *xmit_tail = (*xmit_tail)->b_cont; 21192 (*xmit_tail)->b_prev = local_time; 21193 (*xmit_tail)->b_next = 21194 (mblk_t *)(uintptr_t)(*snxt); 21195 mp1 = mp1->b_cont; 21196 } 21197 *snxt += len; 21198 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21199 BUMP_LOCAL(tcp->tcp_obsegs); 21200 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21201 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21202 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21203 tcp_send_data(tcp, q, mp); 21204 continue; 21205 } 21206 21207 *snxt += len; /* Adjust later if we don't send all of len */ 21208 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21209 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21210 21211 if (*tail_unsent) { 21212 /* Are the bytes above us in flight? */ 21213 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21214 if (rptr != (*xmit_tail)->b_rptr) { 21215 *tail_unsent -= len; 21216 if (len <= mss) /* LSO is unusable */ 21217 tcp->tcp_last_sent_len = (ushort_t)len; 21218 len += tcp_hdr_len; 21219 if (tcp->tcp_ipversion == IPV4_VERSION) 21220 tcp->tcp_ipha->ipha_length = htons(len); 21221 else 21222 tcp->tcp_ip6h->ip6_plen = 21223 htons(len - 21224 ((char *)&tcp->tcp_ip6h[1] - 21225 tcp->tcp_iphc)); 21226 mp = dupb(*xmit_tail); 21227 if (mp == NULL) { 21228 if (ire != NULL) 21229 IRE_REFRELE(ire); 21230 return (-1); /* out_of_mem */ 21231 } 21232 mp->b_rptr = rptr; 21233 /* 21234 * If the old timestamp is no longer in use, 21235 * sample a new timestamp now. 21236 */ 21237 if ((*xmit_tail)->b_next == NULL) { 21238 (*xmit_tail)->b_prev = local_time; 21239 (*xmit_tail)->b_next = 21240 (mblk_t *)(uintptr_t)(*snxt-len); 21241 } 21242 goto must_alloc; 21243 } 21244 } else { 21245 *xmit_tail = (*xmit_tail)->b_cont; 21246 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21247 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21248 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21249 (*xmit_tail)->b_rptr); 21250 } 21251 21252 (*xmit_tail)->b_prev = local_time; 21253 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21254 21255 *tail_unsent -= len; 21256 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21257 tcp->tcp_last_sent_len = (ushort_t)len; 21258 21259 len += tcp_hdr_len; 21260 if (tcp->tcp_ipversion == IPV4_VERSION) 21261 tcp->tcp_ipha->ipha_length = htons(len); 21262 else 21263 tcp->tcp_ip6h->ip6_plen = htons(len - 21264 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21265 21266 mp = dupb(*xmit_tail); 21267 if (mp == NULL) { 21268 if (ire != NULL) 21269 IRE_REFRELE(ire); 21270 return (-1); /* out_of_mem */ 21271 } 21272 21273 len = tcp_hdr_len; 21274 /* 21275 * There are four reasons to allocate a new hdr mblk: 21276 * 1) The bytes above us are in use by another packet 21277 * 2) We don't have good alignment 21278 * 3) The mblk is being shared 21279 * 4) We don't have enough room for a header 21280 */ 21281 rptr = mp->b_rptr - len; 21282 if (!OK_32PTR(rptr) || 21283 ((db = mp->b_datap), db->db_ref != 2) || 21284 rptr < db->db_base + ire_fp_mp_len) { 21285 /* NOTE: we assume allocb returns an OK_32PTR */ 21286 21287 must_alloc:; 21288 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21289 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21290 if (mp1 == NULL) { 21291 freemsg(mp); 21292 if (ire != NULL) 21293 IRE_REFRELE(ire); 21294 return (-1); /* out_of_mem */ 21295 } 21296 mp1->b_cont = mp; 21297 mp = mp1; 21298 /* Leave room for Link Level header */ 21299 len = tcp_hdr_len; 21300 rptr = 21301 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21302 mp->b_wptr = &rptr[len]; 21303 } 21304 21305 /* 21306 * Fill in the header using the template header, and add 21307 * options such as time-stamp, ECN and/or SACK, as needed. 21308 */ 21309 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21310 21311 mp->b_rptr = rptr; 21312 21313 if (*tail_unsent) { 21314 int spill = *tail_unsent; 21315 21316 mp1 = mp->b_cont; 21317 if (mp1 == NULL) 21318 mp1 = mp; 21319 21320 /* 21321 * If we're a little short, tack on more mblks until 21322 * there is no more spillover. 21323 */ 21324 while (spill < 0) { 21325 mblk_t *nmp; 21326 int nmpsz; 21327 21328 nmp = (*xmit_tail)->b_cont; 21329 nmpsz = MBLKL(nmp); 21330 21331 /* 21332 * Excess data in mblk; can we split it? 21333 * If MDT is enabled for the connection, 21334 * keep on splitting as this is a transient 21335 * send path. 21336 */ 21337 if (!do_lso_send && !tcp->tcp_mdt && 21338 (spill + nmpsz > 0)) { 21339 /* 21340 * Don't split if stream head was 21341 * told to break up larger writes 21342 * into smaller ones. 21343 */ 21344 if (tcp->tcp_maxpsz > 0) 21345 break; 21346 21347 /* 21348 * Next mblk is less than SMSS/2 21349 * rounded up to nearest 64-byte; 21350 * let it get sent as part of the 21351 * next segment. 21352 */ 21353 if (tcp->tcp_localnet && 21354 !tcp->tcp_cork && 21355 (nmpsz < roundup((mss >> 1), 64))) 21356 break; 21357 } 21358 21359 *xmit_tail = nmp; 21360 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21361 /* Stash for rtt use later */ 21362 (*xmit_tail)->b_prev = local_time; 21363 (*xmit_tail)->b_next = 21364 (mblk_t *)(uintptr_t)(*snxt - len); 21365 mp1->b_cont = dupb(*xmit_tail); 21366 mp1 = mp1->b_cont; 21367 21368 spill += nmpsz; 21369 if (mp1 == NULL) { 21370 *tail_unsent = spill; 21371 freemsg(mp); 21372 if (ire != NULL) 21373 IRE_REFRELE(ire); 21374 return (-1); /* out_of_mem */ 21375 } 21376 } 21377 21378 /* Trim back any surplus on the last mblk */ 21379 if (spill >= 0) { 21380 mp1->b_wptr -= spill; 21381 *tail_unsent = spill; 21382 } else { 21383 /* 21384 * We did not send everything we could in 21385 * order to remain within the b_cont limit. 21386 */ 21387 *usable -= spill; 21388 *snxt += spill; 21389 tcp->tcp_last_sent_len += spill; 21390 UPDATE_MIB(&tcps->tcps_mib, 21391 tcpOutDataBytes, spill); 21392 /* 21393 * Adjust the checksum 21394 */ 21395 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21396 sum += spill; 21397 sum = (sum >> 16) + (sum & 0xFFFF); 21398 U16_TO_ABE16(sum, tcph->th_sum); 21399 if (tcp->tcp_ipversion == IPV4_VERSION) { 21400 sum = ntohs( 21401 ((ipha_t *)rptr)->ipha_length) + 21402 spill; 21403 ((ipha_t *)rptr)->ipha_length = 21404 htons(sum); 21405 } else { 21406 sum = ntohs( 21407 ((ip6_t *)rptr)->ip6_plen) + 21408 spill; 21409 ((ip6_t *)rptr)->ip6_plen = 21410 htons(sum); 21411 } 21412 *tail_unsent = 0; 21413 } 21414 } 21415 if (tcp->tcp_ip_forward_progress) { 21416 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21417 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21418 tcp->tcp_ip_forward_progress = B_FALSE; 21419 } 21420 21421 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21422 if (do_lso_send) { 21423 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21424 num_lso_seg); 21425 tcp->tcp_obsegs += num_lso_seg; 21426 21427 TCP_STAT(tcps, tcp_lso_times); 21428 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21429 } else { 21430 tcp_send_data(tcp, q, mp); 21431 BUMP_LOCAL(tcp->tcp_obsegs); 21432 } 21433 } 21434 21435 if (ire != NULL) 21436 IRE_REFRELE(ire); 21437 return (0); 21438 } 21439 21440 /* Unlink and return any mblk that looks like it contains a MDT info */ 21441 static mblk_t * 21442 tcp_mdt_info_mp(mblk_t *mp) 21443 { 21444 mblk_t *prev_mp; 21445 21446 for (;;) { 21447 prev_mp = mp; 21448 /* no more to process? */ 21449 if ((mp = mp->b_cont) == NULL) 21450 break; 21451 21452 switch (DB_TYPE(mp)) { 21453 case M_CTL: 21454 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21455 continue; 21456 ASSERT(prev_mp != NULL); 21457 prev_mp->b_cont = mp->b_cont; 21458 mp->b_cont = NULL; 21459 return (mp); 21460 default: 21461 break; 21462 } 21463 } 21464 return (mp); 21465 } 21466 21467 /* MDT info update routine, called when IP notifies us about MDT */ 21468 static void 21469 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21470 { 21471 boolean_t prev_state; 21472 tcp_stack_t *tcps = tcp->tcp_tcps; 21473 21474 /* 21475 * IP is telling us to abort MDT on this connection? We know 21476 * this because the capability is only turned off when IP 21477 * encounters some pathological cases, e.g. link-layer change 21478 * where the new driver doesn't support MDT, or in situation 21479 * where MDT usage on the link-layer has been switched off. 21480 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21481 * if the link-layer doesn't support MDT, and if it does, it 21482 * will indicate that the feature is to be turned on. 21483 */ 21484 prev_state = tcp->tcp_mdt; 21485 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21486 if (!tcp->tcp_mdt && !first) { 21487 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21488 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21489 (void *)tcp->tcp_connp)); 21490 } 21491 21492 /* 21493 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21494 * so disable MDT otherwise. The checks are done here 21495 * and in tcp_wput_data(). 21496 */ 21497 if (tcp->tcp_mdt && 21498 (tcp->tcp_ipversion == IPV4_VERSION && 21499 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21500 (tcp->tcp_ipversion == IPV6_VERSION && 21501 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21502 tcp->tcp_mdt = B_FALSE; 21503 21504 if (tcp->tcp_mdt) { 21505 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21506 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21507 "version (%d), expected version is %d", 21508 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21509 tcp->tcp_mdt = B_FALSE; 21510 return; 21511 } 21512 21513 /* 21514 * We need the driver to be able to handle at least three 21515 * spans per packet in order for tcp MDT to be utilized. 21516 * The first is for the header portion, while the rest are 21517 * needed to handle a packet that straddles across two 21518 * virtually non-contiguous buffers; a typical tcp packet 21519 * therefore consists of only two spans. Note that we take 21520 * a zero as "don't care". 21521 */ 21522 if (mdt_capab->ill_mdt_span_limit > 0 && 21523 mdt_capab->ill_mdt_span_limit < 3) { 21524 tcp->tcp_mdt = B_FALSE; 21525 return; 21526 } 21527 21528 /* a zero means driver wants default value */ 21529 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21530 tcps->tcps_mdt_max_pbufs); 21531 if (tcp->tcp_mdt_max_pld == 0) 21532 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21533 21534 /* ensure 32-bit alignment */ 21535 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21536 mdt_capab->ill_mdt_hdr_head), 4); 21537 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21538 mdt_capab->ill_mdt_hdr_tail), 4); 21539 21540 if (!first && !prev_state) { 21541 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21542 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21543 (void *)tcp->tcp_connp)); 21544 } 21545 } 21546 } 21547 21548 /* Unlink and return any mblk that looks like it contains a LSO info */ 21549 static mblk_t * 21550 tcp_lso_info_mp(mblk_t *mp) 21551 { 21552 mblk_t *prev_mp; 21553 21554 for (;;) { 21555 prev_mp = mp; 21556 /* no more to process? */ 21557 if ((mp = mp->b_cont) == NULL) 21558 break; 21559 21560 switch (DB_TYPE(mp)) { 21561 case M_CTL: 21562 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21563 continue; 21564 ASSERT(prev_mp != NULL); 21565 prev_mp->b_cont = mp->b_cont; 21566 mp->b_cont = NULL; 21567 return (mp); 21568 default: 21569 break; 21570 } 21571 } 21572 21573 return (mp); 21574 } 21575 21576 /* LSO info update routine, called when IP notifies us about LSO */ 21577 static void 21578 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21579 { 21580 tcp_stack_t *tcps = tcp->tcp_tcps; 21581 21582 /* 21583 * IP is telling us to abort LSO on this connection? We know 21584 * this because the capability is only turned off when IP 21585 * encounters some pathological cases, e.g. link-layer change 21586 * where the new NIC/driver doesn't support LSO, or in situation 21587 * where LSO usage on the link-layer has been switched off. 21588 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21589 * if the link-layer doesn't support LSO, and if it does, it 21590 * will indicate that the feature is to be turned on. 21591 */ 21592 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21593 TCP_STAT(tcps, tcp_lso_enabled); 21594 21595 /* 21596 * We currently only support LSO on simple TCP/IPv4, 21597 * so disable LSO otherwise. The checks are done here 21598 * and in tcp_wput_data(). 21599 */ 21600 if (tcp->tcp_lso && 21601 (tcp->tcp_ipversion == IPV4_VERSION && 21602 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21603 (tcp->tcp_ipversion == IPV6_VERSION)) { 21604 tcp->tcp_lso = B_FALSE; 21605 TCP_STAT(tcps, tcp_lso_disabled); 21606 } else { 21607 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21608 lso_capab->ill_lso_max); 21609 } 21610 } 21611 21612 static void 21613 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21614 { 21615 conn_t *connp = tcp->tcp_connp; 21616 tcp_stack_t *tcps = tcp->tcp_tcps; 21617 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21618 21619 ASSERT(ire != NULL); 21620 21621 /* 21622 * We may be in the fastpath here, and although we essentially do 21623 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21624 * we try to keep things as brief as possible. After all, these 21625 * are only best-effort checks, and we do more thorough ones prior 21626 * to calling tcp_send()/tcp_multisend(). 21627 */ 21628 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21629 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21630 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21631 !(ire->ire_flags & RTF_MULTIRT) && 21632 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21633 CONN_IS_LSO_MD_FASTPATH(connp)) { 21634 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21635 /* Cache the result */ 21636 connp->conn_lso_ok = B_TRUE; 21637 21638 ASSERT(ill->ill_lso_capab != NULL); 21639 if (!ill->ill_lso_capab->ill_lso_on) { 21640 ill->ill_lso_capab->ill_lso_on = 1; 21641 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21642 "LSO for interface %s\n", (void *)connp, 21643 ill->ill_name)); 21644 } 21645 tcp_lso_update(tcp, ill->ill_lso_capab); 21646 } else if (ipst->ips_ip_multidata_outbound && 21647 ILL_MDT_CAPABLE(ill)) { 21648 /* Cache the result */ 21649 connp->conn_mdt_ok = B_TRUE; 21650 21651 ASSERT(ill->ill_mdt_capab != NULL); 21652 if (!ill->ill_mdt_capab->ill_mdt_on) { 21653 ill->ill_mdt_capab->ill_mdt_on = 1; 21654 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21655 "MDT for interface %s\n", (void *)connp, 21656 ill->ill_name)); 21657 } 21658 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21659 } 21660 } 21661 21662 /* 21663 * The goal is to reduce the number of generated tcp segments by 21664 * setting the maxpsz multiplier to 0; this will have an affect on 21665 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21666 * into each packet, up to SMSS bytes. Doing this reduces the number 21667 * of outbound segments and incoming ACKs, thus allowing for better 21668 * network and system performance. In contrast the legacy behavior 21669 * may result in sending less than SMSS size, because the last mblk 21670 * for some packets may have more data than needed to make up SMSS, 21671 * and the legacy code refused to "split" it. 21672 * 21673 * We apply the new behavior on following situations: 21674 * 21675 * 1) Loopback connections, 21676 * 2) Connections in which the remote peer is not on local subnet, 21677 * 3) Local subnet connections over the bge interface (see below). 21678 * 21679 * Ideally, we would like this behavior to apply for interfaces other 21680 * than bge. However, doing so would negatively impact drivers which 21681 * perform dynamic mapping and unmapping of DMA resources, which are 21682 * increased by setting the maxpsz multiplier to 0 (more mblks per 21683 * packet will be generated by tcp). The bge driver does not suffer 21684 * from this, as it copies the mblks into pre-mapped buffers, and 21685 * therefore does not require more I/O resources than before. 21686 * 21687 * Otherwise, this behavior is present on all network interfaces when 21688 * the destination endpoint is non-local, since reducing the number 21689 * of packets in general is good for the network. 21690 * 21691 * TODO We need to remove this hard-coded conditional for bge once 21692 * a better "self-tuning" mechanism, or a way to comprehend 21693 * the driver transmit strategy is devised. Until the solution 21694 * is found and well understood, we live with this hack. 21695 */ 21696 if (!tcp_static_maxpsz && 21697 (tcp->tcp_loopback || !tcp->tcp_localnet || 21698 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21699 /* override the default value */ 21700 tcp->tcp_maxpsz = 0; 21701 21702 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21703 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21704 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21705 } 21706 21707 /* set the stream head parameters accordingly */ 21708 (void) tcp_maxpsz_set(tcp, B_TRUE); 21709 } 21710 21711 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21712 static void 21713 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21714 { 21715 uchar_t fval = *mp->b_rptr; 21716 mblk_t *tail; 21717 queue_t *q = tcp->tcp_wq; 21718 21719 /* TODO: How should flush interact with urgent data? */ 21720 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21721 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21722 /* 21723 * Flush only data that has not yet been put on the wire. If 21724 * we flush data that we have already transmitted, life, as we 21725 * know it, may come to an end. 21726 */ 21727 tail = tcp->tcp_xmit_tail; 21728 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21729 tcp->tcp_xmit_tail_unsent = 0; 21730 tcp->tcp_unsent = 0; 21731 if (tail->b_wptr != tail->b_rptr) 21732 tail = tail->b_cont; 21733 if (tail) { 21734 mblk_t **excess = &tcp->tcp_xmit_head; 21735 for (;;) { 21736 mblk_t *mp1 = *excess; 21737 if (mp1 == tail) 21738 break; 21739 tcp->tcp_xmit_tail = mp1; 21740 tcp->tcp_xmit_last = mp1; 21741 excess = &mp1->b_cont; 21742 } 21743 *excess = NULL; 21744 tcp_close_mpp(&tail); 21745 if (tcp->tcp_snd_zcopy_aware) 21746 tcp_zcopy_notify(tcp); 21747 } 21748 /* 21749 * We have no unsent data, so unsent must be less than 21750 * tcp_xmit_lowater, so re-enable flow. 21751 */ 21752 mutex_enter(&tcp->tcp_non_sq_lock); 21753 if (tcp->tcp_flow_stopped) { 21754 tcp_clrqfull(tcp); 21755 } 21756 mutex_exit(&tcp->tcp_non_sq_lock); 21757 } 21758 /* 21759 * TODO: you can't just flush these, you have to increase rwnd for one 21760 * thing. For another, how should urgent data interact? 21761 */ 21762 if (fval & FLUSHR) { 21763 *mp->b_rptr = fval & ~FLUSHW; 21764 /* XXX */ 21765 qreply(q, mp); 21766 return; 21767 } 21768 freemsg(mp); 21769 } 21770 21771 /* 21772 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21773 * messages. 21774 */ 21775 static void 21776 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21777 { 21778 mblk_t *mp1; 21779 STRUCT_HANDLE(strbuf, sb); 21780 uint16_t port; 21781 queue_t *q = tcp->tcp_wq; 21782 in6_addr_t v6addr; 21783 ipaddr_t v4addr; 21784 uint32_t flowinfo = 0; 21785 int addrlen; 21786 21787 /* Make sure it is one of ours. */ 21788 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21789 case TI_GETMYNAME: 21790 case TI_GETPEERNAME: 21791 break; 21792 default: 21793 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21794 return; 21795 } 21796 switch (mi_copy_state(q, mp, &mp1)) { 21797 case -1: 21798 return; 21799 case MI_COPY_CASE(MI_COPY_IN, 1): 21800 break; 21801 case MI_COPY_CASE(MI_COPY_OUT, 1): 21802 /* Copy out the strbuf. */ 21803 mi_copyout(q, mp); 21804 return; 21805 case MI_COPY_CASE(MI_COPY_OUT, 2): 21806 /* All done. */ 21807 mi_copy_done(q, mp, 0); 21808 return; 21809 default: 21810 mi_copy_done(q, mp, EPROTO); 21811 return; 21812 } 21813 /* Check alignment of the strbuf */ 21814 if (!OK_32PTR(mp1->b_rptr)) { 21815 mi_copy_done(q, mp, EINVAL); 21816 return; 21817 } 21818 21819 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 21820 (void *)mp1->b_rptr); 21821 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21822 21823 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21824 mi_copy_done(q, mp, EINVAL); 21825 return; 21826 } 21827 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21828 case TI_GETMYNAME: 21829 if (tcp->tcp_family == AF_INET) { 21830 if (tcp->tcp_ipversion == IPV4_VERSION) { 21831 v4addr = tcp->tcp_ipha->ipha_src; 21832 } else { 21833 /* can't return an address in this case */ 21834 v4addr = 0; 21835 } 21836 } else { 21837 /* tcp->tcp_family == AF_INET6 */ 21838 if (tcp->tcp_ipversion == IPV4_VERSION) { 21839 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 21840 &v6addr); 21841 } else { 21842 v6addr = tcp->tcp_ip6h->ip6_src; 21843 } 21844 } 21845 port = tcp->tcp_lport; 21846 break; 21847 case TI_GETPEERNAME: 21848 if (tcp->tcp_family == AF_INET) { 21849 if (tcp->tcp_ipversion == IPV4_VERSION) { 21850 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 21851 v4addr); 21852 } else { 21853 /* can't return an address in this case */ 21854 v4addr = 0; 21855 } 21856 } else { 21857 /* tcp->tcp_family == AF_INET6) */ 21858 v6addr = tcp->tcp_remote_v6; 21859 if (tcp->tcp_ipversion == IPV6_VERSION) { 21860 /* 21861 * No flowinfo if tcp->tcp_ipversion is v4. 21862 * 21863 * flowinfo was already initialized to zero 21864 * where it was declared above, so only 21865 * set it if ipversion is v6. 21866 */ 21867 flowinfo = tcp->tcp_ip6h->ip6_vcf & 21868 ~IPV6_VERS_AND_FLOW_MASK; 21869 } 21870 } 21871 port = tcp->tcp_fport; 21872 break; 21873 default: 21874 mi_copy_done(q, mp, EPROTO); 21875 return; 21876 } 21877 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21878 if (!mp1) 21879 return; 21880 21881 if (tcp->tcp_family == AF_INET) { 21882 sin_t *sin; 21883 21884 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 21885 sin = (sin_t *)mp1->b_rptr; 21886 mp1->b_wptr = (uchar_t *)&sin[1]; 21887 *sin = sin_null; 21888 sin->sin_family = AF_INET; 21889 sin->sin_addr.s_addr = v4addr; 21890 sin->sin_port = port; 21891 } else { 21892 /* tcp->tcp_family == AF_INET6 */ 21893 sin6_t *sin6; 21894 21895 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 21896 sin6 = (sin6_t *)mp1->b_rptr; 21897 mp1->b_wptr = (uchar_t *)&sin6[1]; 21898 *sin6 = sin6_null; 21899 sin6->sin6_family = AF_INET6; 21900 sin6->sin6_flowinfo = flowinfo; 21901 sin6->sin6_addr = v6addr; 21902 sin6->sin6_port = port; 21903 } 21904 /* Copy out the address */ 21905 mi_copyout(q, mp); 21906 } 21907 21908 /* 21909 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21910 * messages. 21911 */ 21912 /* ARGSUSED */ 21913 static void 21914 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21915 { 21916 conn_t *connp = (conn_t *)arg; 21917 tcp_t *tcp = connp->conn_tcp; 21918 queue_t *q = tcp->tcp_wq; 21919 struct iocblk *iocp; 21920 tcp_stack_t *tcps = tcp->tcp_tcps; 21921 21922 ASSERT(DB_TYPE(mp) == M_IOCTL); 21923 /* 21924 * Try and ASSERT the minimum possible references on the 21925 * conn early enough. Since we are executing on write side, 21926 * the connection is obviously not detached and that means 21927 * there is a ref each for TCP and IP. Since we are behind 21928 * the squeue, the minimum references needed are 3. If the 21929 * conn is in classifier hash list, there should be an 21930 * extra ref for that (we check both the possibilities). 21931 */ 21932 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21933 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21934 21935 iocp = (struct iocblk *)mp->b_rptr; 21936 switch (iocp->ioc_cmd) { 21937 case TCP_IOC_DEFAULT_Q: 21938 /* Wants to be the default wq. */ 21939 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21940 iocp->ioc_error = EPERM; 21941 iocp->ioc_count = 0; 21942 mp->b_datap->db_type = M_IOCACK; 21943 qreply(q, mp); 21944 return; 21945 } 21946 tcp_def_q_set(tcp, mp); 21947 return; 21948 case _SIOCSOCKFALLBACK: 21949 /* 21950 * Either sockmod is about to be popped and the socket 21951 * would now be treated as a plain stream, or a module 21952 * is about to be pushed so we could no longer use read- 21953 * side synchronous streams for fused loopback tcp. 21954 * Drain any queued data and disable direct sockfs 21955 * interface from now on. 21956 */ 21957 if (!tcp->tcp_issocket) { 21958 DB_TYPE(mp) = M_IOCNAK; 21959 iocp->ioc_error = EINVAL; 21960 } else { 21961 #ifdef _ILP32 21962 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 21963 #else 21964 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21965 #endif 21966 /* 21967 * Insert this socket into the acceptor hash. 21968 * We might need it for T_CONN_RES message 21969 */ 21970 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21971 21972 if (tcp->tcp_fused) { 21973 /* 21974 * This is a fused loopback tcp; disable 21975 * read-side synchronous streams interface 21976 * and drain any queued data. It is okay 21977 * to do this for non-synchronous streams 21978 * fused tcp as well. 21979 */ 21980 tcp_fuse_disable_pair(tcp, B_FALSE); 21981 } 21982 tcp->tcp_issocket = B_FALSE; 21983 TCP_STAT(tcps, tcp_sock_fallback); 21984 21985 DB_TYPE(mp) = M_IOCACK; 21986 iocp->ioc_error = 0; 21987 } 21988 iocp->ioc_count = 0; 21989 iocp->ioc_rval = 0; 21990 qreply(q, mp); 21991 return; 21992 } 21993 CALL_IP_WPUT(connp, q, mp); 21994 } 21995 21996 /* 21997 * This routine is called by tcp_wput() to handle all TPI requests. 21998 */ 21999 /* ARGSUSED */ 22000 static void 22001 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22002 { 22003 conn_t *connp = (conn_t *)arg; 22004 tcp_t *tcp = connp->conn_tcp; 22005 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22006 uchar_t *rptr; 22007 t_scalar_t type; 22008 int len; 22009 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22010 22011 /* 22012 * Try and ASSERT the minimum possible references on the 22013 * conn early enough. Since we are executing on write side, 22014 * the connection is obviously not detached and that means 22015 * there is a ref each for TCP and IP. Since we are behind 22016 * the squeue, the minimum references needed are 3. If the 22017 * conn is in classifier hash list, there should be an 22018 * extra ref for that (we check both the possibilities). 22019 */ 22020 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22021 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22022 22023 rptr = mp->b_rptr; 22024 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22025 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22026 type = ((union T_primitives *)rptr)->type; 22027 if (type == T_EXDATA_REQ) { 22028 uint32_t msize = msgdsize(mp->b_cont); 22029 22030 len = msize - 1; 22031 if (len < 0) { 22032 freemsg(mp); 22033 return; 22034 } 22035 /* 22036 * Try to force urgent data out on the wire. 22037 * Even if we have unsent data this will 22038 * at least send the urgent flag. 22039 * XXX does not handle more flag correctly. 22040 */ 22041 len += tcp->tcp_unsent; 22042 len += tcp->tcp_snxt; 22043 tcp->tcp_urg = len; 22044 tcp->tcp_valid_bits |= TCP_URG_VALID; 22045 22046 /* Bypass tcp protocol for fused tcp loopback */ 22047 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22048 return; 22049 } else if (type != T_DATA_REQ) { 22050 goto non_urgent_data; 22051 } 22052 /* TODO: options, flags, ... from user */ 22053 /* Set length to zero for reclamation below */ 22054 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22055 freeb(mp); 22056 return; 22057 } else { 22058 if (tcp->tcp_debug) { 22059 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22060 "tcp_wput_proto, dropping one..."); 22061 } 22062 freemsg(mp); 22063 return; 22064 } 22065 22066 non_urgent_data: 22067 22068 switch ((int)tprim->type) { 22069 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22070 /* 22071 * save the kssl_ent_t from the next block, and convert this 22072 * back to a normal bind_req. 22073 */ 22074 if (mp->b_cont != NULL) { 22075 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22076 22077 if (tcp->tcp_kssl_ent != NULL) { 22078 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22079 KSSL_NO_PROXY); 22080 tcp->tcp_kssl_ent = NULL; 22081 } 22082 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22083 sizeof (kssl_ent_t)); 22084 kssl_hold_ent(tcp->tcp_kssl_ent); 22085 freemsg(mp->b_cont); 22086 mp->b_cont = NULL; 22087 } 22088 tprim->type = T_BIND_REQ; 22089 22090 /* FALLTHROUGH */ 22091 case O_T_BIND_REQ: /* bind request */ 22092 case T_BIND_REQ: /* new semantics bind request */ 22093 tcp_bind(tcp, mp); 22094 break; 22095 case T_UNBIND_REQ: /* unbind request */ 22096 tcp_unbind(tcp, mp); 22097 break; 22098 case O_T_CONN_RES: /* old connection response XXX */ 22099 case T_CONN_RES: /* connection response */ 22100 tcp_accept(tcp, mp); 22101 break; 22102 case T_CONN_REQ: /* connection request */ 22103 tcp_connect(tcp, mp); 22104 break; 22105 case T_DISCON_REQ: /* disconnect request */ 22106 tcp_disconnect(tcp, mp); 22107 break; 22108 case T_CAPABILITY_REQ: 22109 tcp_capability_req(tcp, mp); /* capability request */ 22110 break; 22111 case T_INFO_REQ: /* information request */ 22112 tcp_info_req(tcp, mp); 22113 break; 22114 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22115 /* Only IP is allowed to return meaningful value */ 22116 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 22117 break; 22118 case T_OPTMGMT_REQ: 22119 /* 22120 * Note: no support for snmpcom_req() through new 22121 * T_OPTMGMT_REQ. See comments in ip.c 22122 */ 22123 /* Only IP is allowed to return meaningful value */ 22124 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 22125 break; 22126 22127 case T_UNITDATA_REQ: /* unitdata request */ 22128 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22129 break; 22130 case T_ORDREL_REQ: /* orderly release req */ 22131 freemsg(mp); 22132 22133 if (tcp->tcp_fused) 22134 tcp_unfuse(tcp); 22135 22136 if (tcp_xmit_end(tcp) != 0) { 22137 /* 22138 * We were crossing FINs and got a reset from 22139 * the other side. Just ignore it. 22140 */ 22141 if (tcp->tcp_debug) { 22142 (void) strlog(TCP_MOD_ID, 0, 1, 22143 SL_ERROR|SL_TRACE, 22144 "tcp_wput_proto, T_ORDREL_REQ out of " 22145 "state %s", 22146 tcp_display(tcp, NULL, 22147 DISP_ADDR_AND_PORT)); 22148 } 22149 } 22150 break; 22151 case T_ADDR_REQ: 22152 tcp_addr_req(tcp, mp); 22153 break; 22154 default: 22155 if (tcp->tcp_debug) { 22156 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22157 "tcp_wput_proto, bogus TPI msg, type %d", 22158 tprim->type); 22159 } 22160 /* 22161 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22162 * to recover. 22163 */ 22164 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22165 break; 22166 } 22167 } 22168 22169 /* 22170 * The TCP write service routine should never be called... 22171 */ 22172 /* ARGSUSED */ 22173 static void 22174 tcp_wsrv(queue_t *q) 22175 { 22176 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22177 22178 TCP_STAT(tcps, tcp_wsrv_called); 22179 } 22180 22181 /* Non overlapping byte exchanger */ 22182 static void 22183 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22184 { 22185 uchar_t uch; 22186 22187 while (len-- > 0) { 22188 uch = a[len]; 22189 a[len] = b[len]; 22190 b[len] = uch; 22191 } 22192 } 22193 22194 /* 22195 * Send out a control packet on the tcp connection specified. This routine 22196 * is typically called where we need a simple ACK or RST generated. 22197 */ 22198 static void 22199 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22200 { 22201 uchar_t *rptr; 22202 tcph_t *tcph; 22203 ipha_t *ipha = NULL; 22204 ip6_t *ip6h = NULL; 22205 uint32_t sum; 22206 int tcp_hdr_len; 22207 int tcp_ip_hdr_len; 22208 mblk_t *mp; 22209 tcp_stack_t *tcps = tcp->tcp_tcps; 22210 22211 /* 22212 * Save sum for use in source route later. 22213 */ 22214 ASSERT(tcp != NULL); 22215 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22216 tcp_hdr_len = tcp->tcp_hdr_len; 22217 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22218 22219 /* If a text string is passed in with the request, pass it to strlog. */ 22220 if (str != NULL && tcp->tcp_debug) { 22221 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22222 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22223 str, seq, ack, ctl); 22224 } 22225 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22226 BPRI_MED); 22227 if (mp == NULL) { 22228 return; 22229 } 22230 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22231 mp->b_rptr = rptr; 22232 mp->b_wptr = &rptr[tcp_hdr_len]; 22233 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22234 22235 if (tcp->tcp_ipversion == IPV4_VERSION) { 22236 ipha = (ipha_t *)rptr; 22237 ipha->ipha_length = htons(tcp_hdr_len); 22238 } else { 22239 ip6h = (ip6_t *)rptr; 22240 ASSERT(tcp != NULL); 22241 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22242 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22243 } 22244 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22245 tcph->th_flags[0] = (uint8_t)ctl; 22246 if (ctl & TH_RST) { 22247 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22248 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22249 /* 22250 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22251 */ 22252 if (tcp->tcp_snd_ts_ok && 22253 tcp->tcp_state > TCPS_SYN_SENT) { 22254 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22255 *(mp->b_wptr) = TCPOPT_EOL; 22256 if (tcp->tcp_ipversion == IPV4_VERSION) { 22257 ipha->ipha_length = htons(tcp_hdr_len - 22258 TCPOPT_REAL_TS_LEN); 22259 } else { 22260 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22261 TCPOPT_REAL_TS_LEN); 22262 } 22263 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22264 sum -= TCPOPT_REAL_TS_LEN; 22265 } 22266 } 22267 if (ctl & TH_ACK) { 22268 if (tcp->tcp_snd_ts_ok) { 22269 U32_TO_BE32(lbolt, 22270 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22271 U32_TO_BE32(tcp->tcp_ts_recent, 22272 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22273 } 22274 22275 /* Update the latest receive window size in TCP header. */ 22276 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22277 tcph->th_win); 22278 tcp->tcp_rack = ack; 22279 tcp->tcp_rack_cnt = 0; 22280 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22281 } 22282 BUMP_LOCAL(tcp->tcp_obsegs); 22283 U32_TO_BE32(seq, tcph->th_seq); 22284 U32_TO_BE32(ack, tcph->th_ack); 22285 /* 22286 * Include the adjustment for a source route if any. 22287 */ 22288 sum = (sum >> 16) + (sum & 0xFFFF); 22289 U16_TO_BE16(sum, tcph->th_sum); 22290 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22291 tcp_send_data(tcp, tcp->tcp_wq, mp); 22292 } 22293 22294 /* 22295 * If this routine returns B_TRUE, TCP can generate a RST in response 22296 * to a segment. If it returns B_FALSE, TCP should not respond. 22297 */ 22298 static boolean_t 22299 tcp_send_rst_chk(tcp_stack_t *tcps) 22300 { 22301 clock_t now; 22302 22303 /* 22304 * TCP needs to protect itself from generating too many RSTs. 22305 * This can be a DoS attack by sending us random segments 22306 * soliciting RSTs. 22307 * 22308 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22309 * in each 1 second interval. In this way, TCP still generate 22310 * RSTs in normal cases but when under attack, the impact is 22311 * limited. 22312 */ 22313 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22314 now = lbolt; 22315 /* lbolt can wrap around. */ 22316 if ((tcps->tcps_last_rst_intrvl > now) || 22317 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22318 1*SECONDS)) { 22319 tcps->tcps_last_rst_intrvl = now; 22320 tcps->tcps_rst_cnt = 1; 22321 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22322 return (B_FALSE); 22323 } 22324 } 22325 return (B_TRUE); 22326 } 22327 22328 /* 22329 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22330 */ 22331 static void 22332 tcp_ip_ire_mark_advice(tcp_t *tcp) 22333 { 22334 mblk_t *mp; 22335 ipic_t *ipic; 22336 22337 if (tcp->tcp_ipversion == IPV4_VERSION) { 22338 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22339 &ipic); 22340 } else { 22341 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22342 &ipic); 22343 } 22344 if (mp == NULL) 22345 return; 22346 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22347 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22348 } 22349 22350 /* 22351 * Return an IP advice ioctl mblk and set ipic to be the pointer 22352 * to the advice structure. 22353 */ 22354 static mblk_t * 22355 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22356 { 22357 struct iocblk *ioc; 22358 mblk_t *mp, *mp1; 22359 22360 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22361 if (mp == NULL) 22362 return (NULL); 22363 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22364 *ipic = (ipic_t *)mp->b_rptr; 22365 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22366 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22367 22368 bcopy(addr, *ipic + 1, addr_len); 22369 22370 (*ipic)->ipic_addr_length = addr_len; 22371 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22372 22373 mp1 = mkiocb(IP_IOCTL); 22374 if (mp1 == NULL) { 22375 freemsg(mp); 22376 return (NULL); 22377 } 22378 mp1->b_cont = mp; 22379 ioc = (struct iocblk *)mp1->b_rptr; 22380 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22381 22382 return (mp1); 22383 } 22384 22385 /* 22386 * Generate a reset based on an inbound packet for which there is no active 22387 * tcp state that we can find. 22388 * 22389 * IPSEC NOTE : Try to send the reply with the same protection as it came 22390 * in. We still have the ipsec_mp that the packet was attached to. Thus 22391 * the packet will go out at the same level of protection as it came in by 22392 * converting the IPSEC_IN to IPSEC_OUT. 22393 */ 22394 static void 22395 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22396 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22397 tcp_stack_t *tcps) 22398 { 22399 ipha_t *ipha = NULL; 22400 ip6_t *ip6h = NULL; 22401 ushort_t len; 22402 tcph_t *tcph; 22403 int i; 22404 mblk_t *ipsec_mp; 22405 boolean_t mctl_present; 22406 ipic_t *ipic; 22407 ipaddr_t v4addr; 22408 in6_addr_t v6addr; 22409 int addr_len; 22410 void *addr; 22411 queue_t *q = tcps->tcps_g_q; 22412 tcp_t *tcp; 22413 cred_t *cr; 22414 mblk_t *nmp; 22415 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22416 22417 if (tcps->tcps_g_q == NULL) { 22418 /* 22419 * For non-zero stackids the default queue isn't created 22420 * until the first open, thus there can be a need to send 22421 * a reset before then. But we can't do that, hence we just 22422 * drop the packet. Later during boot, when the default queue 22423 * has been setup, a retransmitted packet from the peer 22424 * will result in a reset. 22425 */ 22426 ASSERT(tcps->tcps_netstack->netstack_stackid != 22427 GLOBAL_NETSTACKID); 22428 freemsg(mp); 22429 return; 22430 } 22431 22432 tcp = Q_TO_TCP(q); 22433 22434 if (!tcp_send_rst_chk(tcps)) { 22435 tcps->tcps_rst_unsent++; 22436 freemsg(mp); 22437 return; 22438 } 22439 22440 if (mp->b_datap->db_type == M_CTL) { 22441 ipsec_mp = mp; 22442 mp = mp->b_cont; 22443 mctl_present = B_TRUE; 22444 } else { 22445 ipsec_mp = mp; 22446 mctl_present = B_FALSE; 22447 } 22448 22449 if (str && q && tcps->tcps_dbg) { 22450 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22451 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22452 "flags 0x%x", 22453 str, seq, ack, ctl); 22454 } 22455 if (mp->b_datap->db_ref != 1) { 22456 mblk_t *mp1 = copyb(mp); 22457 freemsg(mp); 22458 mp = mp1; 22459 if (!mp) { 22460 if (mctl_present) 22461 freeb(ipsec_mp); 22462 return; 22463 } else { 22464 if (mctl_present) { 22465 ipsec_mp->b_cont = mp; 22466 } else { 22467 ipsec_mp = mp; 22468 } 22469 } 22470 } else if (mp->b_cont) { 22471 freemsg(mp->b_cont); 22472 mp->b_cont = NULL; 22473 } 22474 /* 22475 * We skip reversing source route here. 22476 * (for now we replace all IP options with EOL) 22477 */ 22478 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22479 ipha = (ipha_t *)mp->b_rptr; 22480 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22481 mp->b_rptr[i] = IPOPT_EOL; 22482 /* 22483 * Make sure that src address isn't flagrantly invalid. 22484 * Not all broadcast address checking for the src address 22485 * is possible, since we don't know the netmask of the src 22486 * addr. No check for destination address is done, since 22487 * IP will not pass up a packet with a broadcast dest 22488 * address to TCP. Similar checks are done below for IPv6. 22489 */ 22490 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22491 CLASSD(ipha->ipha_src)) { 22492 freemsg(ipsec_mp); 22493 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22494 return; 22495 } 22496 } else { 22497 ip6h = (ip6_t *)mp->b_rptr; 22498 22499 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22500 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22501 freemsg(ipsec_mp); 22502 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22503 return; 22504 } 22505 22506 /* Remove any extension headers assuming partial overlay */ 22507 if (ip_hdr_len > IPV6_HDR_LEN) { 22508 uint8_t *to; 22509 22510 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22511 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22512 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22513 ip_hdr_len = IPV6_HDR_LEN; 22514 ip6h = (ip6_t *)mp->b_rptr; 22515 ip6h->ip6_nxt = IPPROTO_TCP; 22516 } 22517 } 22518 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22519 if (tcph->th_flags[0] & TH_RST) { 22520 freemsg(ipsec_mp); 22521 return; 22522 } 22523 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22524 len = ip_hdr_len + sizeof (tcph_t); 22525 mp->b_wptr = &mp->b_rptr[len]; 22526 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22527 ipha->ipha_length = htons(len); 22528 /* Swap addresses */ 22529 v4addr = ipha->ipha_src; 22530 ipha->ipha_src = ipha->ipha_dst; 22531 ipha->ipha_dst = v4addr; 22532 ipha->ipha_ident = 0; 22533 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22534 addr_len = IP_ADDR_LEN; 22535 addr = &v4addr; 22536 } else { 22537 /* No ip6i_t in this case */ 22538 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22539 /* Swap addresses */ 22540 v6addr = ip6h->ip6_src; 22541 ip6h->ip6_src = ip6h->ip6_dst; 22542 ip6h->ip6_dst = v6addr; 22543 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22544 addr_len = IPV6_ADDR_LEN; 22545 addr = &v6addr; 22546 } 22547 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22548 U32_TO_BE32(ack, tcph->th_ack); 22549 U32_TO_BE32(seq, tcph->th_seq); 22550 U16_TO_BE16(0, tcph->th_win); 22551 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22552 tcph->th_flags[0] = (uint8_t)ctl; 22553 if (ctl & TH_RST) { 22554 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22555 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22556 } 22557 22558 /* IP trusts us to set up labels when required. */ 22559 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 22560 crgetlabel(cr) != NULL) { 22561 int err, adjust; 22562 22563 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22564 err = tsol_check_label(cr, &mp, &adjust, 22565 tcp->tcp_connp->conn_mac_exempt, 22566 tcps->tcps_netstack->netstack_ip); 22567 else 22568 err = tsol_check_label_v6(cr, &mp, &adjust, 22569 tcp->tcp_connp->conn_mac_exempt, 22570 tcps->tcps_netstack->netstack_ip); 22571 if (mctl_present) 22572 ipsec_mp->b_cont = mp; 22573 else 22574 ipsec_mp = mp; 22575 if (err != 0) { 22576 freemsg(ipsec_mp); 22577 return; 22578 } 22579 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22580 ipha = (ipha_t *)mp->b_rptr; 22581 adjust += ntohs(ipha->ipha_length); 22582 ipha->ipha_length = htons(adjust); 22583 } else { 22584 ip6h = (ip6_t *)mp->b_rptr; 22585 } 22586 } 22587 22588 if (mctl_present) { 22589 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22590 22591 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22592 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22593 return; 22594 } 22595 } 22596 if (zoneid == ALL_ZONES) 22597 zoneid = GLOBAL_ZONEID; 22598 22599 /* Add the zoneid so ip_output routes it properly */ 22600 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22601 freemsg(ipsec_mp); 22602 return; 22603 } 22604 ipsec_mp = nmp; 22605 22606 /* 22607 * NOTE: one might consider tracing a TCP packet here, but 22608 * this function has no active TCP state and no tcp structure 22609 * that has a trace buffer. If we traced here, we would have 22610 * to keep a local trace buffer in tcp_record_trace(). 22611 * 22612 * TSol note: The mblk that contains the incoming packet was 22613 * reused by tcp_xmit_listener_reset, so it already contains 22614 * the right credentials and we don't need to call mblk_setcred. 22615 * Also the conn's cred is not right since it is associated 22616 * with tcps_g_q. 22617 */ 22618 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22619 22620 /* 22621 * Tell IP to mark the IRE used for this destination temporary. 22622 * This way, we can limit our exposure to DoS attack because IP 22623 * creates an IRE for each destination. If there are too many, 22624 * the time to do any routing lookup will be extremely long. And 22625 * the lookup can be in interrupt context. 22626 * 22627 * Note that in normal circumstances, this marking should not 22628 * affect anything. It would be nice if only 1 message is 22629 * needed to inform IP that the IRE created for this RST should 22630 * not be added to the cache table. But there is currently 22631 * not such communication mechanism between TCP and IP. So 22632 * the best we can do now is to send the advice ioctl to IP 22633 * to mark the IRE temporary. 22634 */ 22635 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22636 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22637 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22638 } 22639 } 22640 22641 /* 22642 * Initiate closedown sequence on an active connection. (May be called as 22643 * writer.) Return value zero for OK return, non-zero for error return. 22644 */ 22645 static int 22646 tcp_xmit_end(tcp_t *tcp) 22647 { 22648 ipic_t *ipic; 22649 mblk_t *mp; 22650 tcp_stack_t *tcps = tcp->tcp_tcps; 22651 22652 if (tcp->tcp_state < TCPS_SYN_RCVD || 22653 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22654 /* 22655 * Invalid state, only states TCPS_SYN_RCVD, 22656 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22657 */ 22658 return (-1); 22659 } 22660 22661 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22662 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22663 /* 22664 * If there is nothing more unsent, send the FIN now. 22665 * Otherwise, it will go out with the last segment. 22666 */ 22667 if (tcp->tcp_unsent == 0) { 22668 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22669 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22670 22671 if (mp) { 22672 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22673 tcp_send_data(tcp, tcp->tcp_wq, mp); 22674 } else { 22675 /* 22676 * Couldn't allocate msg. Pretend we got it out. 22677 * Wait for rexmit timeout. 22678 */ 22679 tcp->tcp_snxt = tcp->tcp_fss + 1; 22680 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22681 } 22682 22683 /* 22684 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22685 * changed. 22686 */ 22687 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22688 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22689 } 22690 } else { 22691 /* 22692 * If tcp->tcp_cork is set, then the data will not get sent, 22693 * so we have to check that and unset it first. 22694 */ 22695 if (tcp->tcp_cork) 22696 tcp->tcp_cork = B_FALSE; 22697 tcp_wput_data(tcp, NULL, B_FALSE); 22698 } 22699 22700 /* 22701 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22702 * is 0, don't update the cache. 22703 */ 22704 if (tcps->tcps_rtt_updates == 0 || 22705 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22706 return (0); 22707 22708 /* 22709 * NOTE: should not update if source routes i.e. if tcp_remote if 22710 * different from the destination. 22711 */ 22712 if (tcp->tcp_ipversion == IPV4_VERSION) { 22713 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22714 return (0); 22715 } 22716 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22717 &ipic); 22718 } else { 22719 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22720 &tcp->tcp_ip6h->ip6_dst))) { 22721 return (0); 22722 } 22723 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22724 &ipic); 22725 } 22726 22727 /* Record route attributes in the IRE for use by future connections. */ 22728 if (mp == NULL) 22729 return (0); 22730 22731 /* 22732 * We do not have a good algorithm to update ssthresh at this time. 22733 * So don't do any update. 22734 */ 22735 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22736 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22737 22738 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22739 return (0); 22740 } 22741 22742 /* 22743 * Generate a "no listener here" RST in response to an "unknown" segment. 22744 * Note that we are reusing the incoming mp to construct the outgoing 22745 * RST. 22746 */ 22747 void 22748 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22749 tcp_stack_t *tcps) 22750 { 22751 uchar_t *rptr; 22752 uint32_t seg_len; 22753 tcph_t *tcph; 22754 uint32_t seg_seq; 22755 uint32_t seg_ack; 22756 uint_t flags; 22757 mblk_t *ipsec_mp; 22758 ipha_t *ipha; 22759 ip6_t *ip6h; 22760 boolean_t mctl_present = B_FALSE; 22761 boolean_t check = B_TRUE; 22762 boolean_t policy_present; 22763 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22764 22765 TCP_STAT(tcps, tcp_no_listener); 22766 22767 ipsec_mp = mp; 22768 22769 if (mp->b_datap->db_type == M_CTL) { 22770 ipsec_in_t *ii; 22771 22772 mctl_present = B_TRUE; 22773 mp = mp->b_cont; 22774 22775 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22776 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22777 if (ii->ipsec_in_dont_check) { 22778 check = B_FALSE; 22779 if (!ii->ipsec_in_secure) { 22780 freeb(ipsec_mp); 22781 mctl_present = B_FALSE; 22782 ipsec_mp = mp; 22783 } 22784 } 22785 } 22786 22787 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22788 policy_present = ipss->ipsec_inbound_v4_policy_present; 22789 ipha = (ipha_t *)mp->b_rptr; 22790 ip6h = NULL; 22791 } else { 22792 policy_present = ipss->ipsec_inbound_v6_policy_present; 22793 ipha = NULL; 22794 ip6h = (ip6_t *)mp->b_rptr; 22795 } 22796 22797 if (check && policy_present) { 22798 /* 22799 * The conn_t parameter is NULL because we already know 22800 * nobody's home. 22801 */ 22802 ipsec_mp = ipsec_check_global_policy( 22803 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22804 tcps->tcps_netstack); 22805 if (ipsec_mp == NULL) 22806 return; 22807 } 22808 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22809 DTRACE_PROBE2( 22810 tx__ip__log__error__nolistener__tcp, 22811 char *, "Could not reply with RST to mp(1)", 22812 mblk_t *, mp); 22813 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22814 freemsg(ipsec_mp); 22815 return; 22816 } 22817 22818 rptr = mp->b_rptr; 22819 22820 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22821 seg_seq = BE32_TO_U32(tcph->th_seq); 22822 seg_ack = BE32_TO_U32(tcph->th_ack); 22823 flags = tcph->th_flags[0]; 22824 22825 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22826 if (flags & TH_RST) { 22827 freemsg(ipsec_mp); 22828 } else if (flags & TH_ACK) { 22829 tcp_xmit_early_reset("no tcp, reset", 22830 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps); 22831 } else { 22832 if (flags & TH_SYN) { 22833 seg_len++; 22834 } else { 22835 /* 22836 * Here we violate the RFC. Note that a normal 22837 * TCP will never send a segment without the ACK 22838 * flag, except for RST or SYN segment. This 22839 * segment is neither. Just drop it on the 22840 * floor. 22841 */ 22842 freemsg(ipsec_mp); 22843 tcps->tcps_rst_unsent++; 22844 return; 22845 } 22846 22847 tcp_xmit_early_reset("no tcp, reset/ack", 22848 ipsec_mp, 0, seg_seq + seg_len, 22849 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps); 22850 } 22851 } 22852 22853 /* 22854 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22855 * ip and tcp header ready to pass down to IP. If the mp passed in is 22856 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22857 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22858 * otherwise it will dup partial mblks.) 22859 * Otherwise, an appropriate ACK packet will be generated. This 22860 * routine is not usually called to send new data for the first time. It 22861 * is mostly called out of the timer for retransmits, and to generate ACKs. 22862 * 22863 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22864 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22865 * of the original mblk chain will be returned in *offset and *end_mp. 22866 */ 22867 mblk_t * 22868 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22869 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22870 boolean_t rexmit) 22871 { 22872 int data_length; 22873 int32_t off = 0; 22874 uint_t flags; 22875 mblk_t *mp1; 22876 mblk_t *mp2; 22877 uchar_t *rptr; 22878 tcph_t *tcph; 22879 int32_t num_sack_blk = 0; 22880 int32_t sack_opt_len = 0; 22881 tcp_stack_t *tcps = tcp->tcp_tcps; 22882 22883 /* Allocate for our maximum TCP header + link-level */ 22884 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22885 tcps->tcps_wroff_xtra, BPRI_MED); 22886 if (!mp1) 22887 return (NULL); 22888 data_length = 0; 22889 22890 /* 22891 * Note that tcp_mss has been adjusted to take into account the 22892 * timestamp option if applicable. Because SACK options do not 22893 * appear in every TCP segments and they are of variable lengths, 22894 * they cannot be included in tcp_mss. Thus we need to calculate 22895 * the actual segment length when we need to send a segment which 22896 * includes SACK options. 22897 */ 22898 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22899 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22900 tcp->tcp_num_sack_blk); 22901 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22902 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22903 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22904 max_to_send -= sack_opt_len; 22905 } 22906 22907 if (offset != NULL) { 22908 off = *offset; 22909 /* We use offset as an indicator that end_mp is not NULL. */ 22910 *end_mp = NULL; 22911 } 22912 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22913 /* This could be faster with cooperation from downstream */ 22914 if (mp2 != mp1 && !sendall && 22915 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22916 max_to_send) 22917 /* 22918 * Don't send the next mblk since the whole mblk 22919 * does not fit. 22920 */ 22921 break; 22922 mp2->b_cont = dupb(mp); 22923 mp2 = mp2->b_cont; 22924 if (!mp2) { 22925 freemsg(mp1); 22926 return (NULL); 22927 } 22928 mp2->b_rptr += off; 22929 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22930 (uintptr_t)INT_MAX); 22931 22932 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22933 if (data_length > max_to_send) { 22934 mp2->b_wptr -= data_length - max_to_send; 22935 data_length = max_to_send; 22936 off = mp2->b_wptr - mp->b_rptr; 22937 break; 22938 } else { 22939 off = 0; 22940 } 22941 } 22942 if (offset != NULL) { 22943 *offset = off; 22944 *end_mp = mp; 22945 } 22946 if (seg_len != NULL) { 22947 *seg_len = data_length; 22948 } 22949 22950 /* Update the latest receive window size in TCP header. */ 22951 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22952 tcp->tcp_tcph->th_win); 22953 22954 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22955 mp1->b_rptr = rptr; 22956 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22957 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22958 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22959 U32_TO_ABE32(seq, tcph->th_seq); 22960 22961 /* 22962 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22963 * that this function was called from tcp_wput_data. Thus, when called 22964 * to retransmit data the setting of the PUSH bit may appear some 22965 * what random in that it might get set when it should not. This 22966 * should not pose any performance issues. 22967 */ 22968 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22969 tcp->tcp_unsent == data_length)) { 22970 flags = TH_ACK | TH_PUSH; 22971 } else { 22972 flags = TH_ACK; 22973 } 22974 22975 if (tcp->tcp_ecn_ok) { 22976 if (tcp->tcp_ecn_echo_on) 22977 flags |= TH_ECE; 22978 22979 /* 22980 * Only set ECT bit and ECN_CWR if a segment contains new data. 22981 * There is no TCP flow control for non-data segments, and 22982 * only data segment is transmitted reliably. 22983 */ 22984 if (data_length > 0 && !rexmit) { 22985 SET_ECT(tcp, rptr); 22986 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 22987 flags |= TH_CWR; 22988 tcp->tcp_ecn_cwr_sent = B_TRUE; 22989 } 22990 } 22991 } 22992 22993 if (tcp->tcp_valid_bits) { 22994 uint32_t u1; 22995 22996 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 22997 seq == tcp->tcp_iss) { 22998 uchar_t *wptr; 22999 23000 /* 23001 * If TCP_ISS_VALID and the seq number is tcp_iss, 23002 * TCP can only be in SYN-SENT, SYN-RCVD or 23003 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23004 * our SYN is not ack'ed but the app closes this 23005 * TCP connection. 23006 */ 23007 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23008 tcp->tcp_state == TCPS_SYN_RCVD || 23009 tcp->tcp_state == TCPS_FIN_WAIT_1); 23010 23011 /* 23012 * Tack on the MSS option. It is always needed 23013 * for both active and passive open. 23014 * 23015 * MSS option value should be interface MTU - MIN 23016 * TCP/IP header according to RFC 793 as it means 23017 * the maximum segment size TCP can receive. But 23018 * to get around some broken middle boxes/end hosts 23019 * out there, we allow the option value to be the 23020 * same as the MSS option size on the peer side. 23021 * In this way, the other side will not send 23022 * anything larger than they can receive. 23023 * 23024 * Note that for SYN_SENT state, the ndd param 23025 * tcp_use_smss_as_mss_opt has no effect as we 23026 * don't know the peer's MSS option value. So 23027 * the only case we need to take care of is in 23028 * SYN_RCVD state, which is done later. 23029 */ 23030 wptr = mp1->b_wptr; 23031 wptr[0] = TCPOPT_MAXSEG; 23032 wptr[1] = TCPOPT_MAXSEG_LEN; 23033 wptr += 2; 23034 u1 = tcp->tcp_if_mtu - 23035 (tcp->tcp_ipversion == IPV4_VERSION ? 23036 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23037 TCP_MIN_HEADER_LENGTH; 23038 U16_TO_BE16(u1, wptr); 23039 mp1->b_wptr = wptr + 2; 23040 /* Update the offset to cover the additional word */ 23041 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23042 23043 /* 23044 * Note that the following way of filling in 23045 * TCP options are not optimal. Some NOPs can 23046 * be saved. But there is no need at this time 23047 * to optimize it. When it is needed, we will 23048 * do it. 23049 */ 23050 switch (tcp->tcp_state) { 23051 case TCPS_SYN_SENT: 23052 flags = TH_SYN; 23053 23054 if (tcp->tcp_snd_ts_ok) { 23055 uint32_t llbolt = (uint32_t)lbolt; 23056 23057 wptr = mp1->b_wptr; 23058 wptr[0] = TCPOPT_NOP; 23059 wptr[1] = TCPOPT_NOP; 23060 wptr[2] = TCPOPT_TSTAMP; 23061 wptr[3] = TCPOPT_TSTAMP_LEN; 23062 wptr += 4; 23063 U32_TO_BE32(llbolt, wptr); 23064 wptr += 4; 23065 ASSERT(tcp->tcp_ts_recent == 0); 23066 U32_TO_BE32(0L, wptr); 23067 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23068 tcph->th_offset_and_rsrvd[0] += 23069 (3 << 4); 23070 } 23071 23072 /* 23073 * Set up all the bits to tell other side 23074 * we are ECN capable. 23075 */ 23076 if (tcp->tcp_ecn_ok) { 23077 flags |= (TH_ECE | TH_CWR); 23078 } 23079 break; 23080 case TCPS_SYN_RCVD: 23081 flags |= TH_SYN; 23082 23083 /* 23084 * Reset the MSS option value to be SMSS 23085 * We should probably add back the bytes 23086 * for timestamp option and IPsec. We 23087 * don't do that as this is a workaround 23088 * for broken middle boxes/end hosts, it 23089 * is better for us to be more cautious. 23090 * They may not take these things into 23091 * account in their SMSS calculation. Thus 23092 * the peer's calculated SMSS may be smaller 23093 * than what it can be. This should be OK. 23094 */ 23095 if (tcps->tcps_use_smss_as_mss_opt) { 23096 u1 = tcp->tcp_mss; 23097 U16_TO_BE16(u1, wptr); 23098 } 23099 23100 /* 23101 * If the other side is ECN capable, reply 23102 * that we are also ECN capable. 23103 */ 23104 if (tcp->tcp_ecn_ok) 23105 flags |= TH_ECE; 23106 break; 23107 default: 23108 /* 23109 * The above ASSERT() makes sure that this 23110 * must be FIN-WAIT-1 state. Our SYN has 23111 * not been ack'ed so retransmit it. 23112 */ 23113 flags |= TH_SYN; 23114 break; 23115 } 23116 23117 if (tcp->tcp_snd_ws_ok) { 23118 wptr = mp1->b_wptr; 23119 wptr[0] = TCPOPT_NOP; 23120 wptr[1] = TCPOPT_WSCALE; 23121 wptr[2] = TCPOPT_WS_LEN; 23122 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23123 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23124 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23125 } 23126 23127 if (tcp->tcp_snd_sack_ok) { 23128 wptr = mp1->b_wptr; 23129 wptr[0] = TCPOPT_NOP; 23130 wptr[1] = TCPOPT_NOP; 23131 wptr[2] = TCPOPT_SACK_PERMITTED; 23132 wptr[3] = TCPOPT_SACK_OK_LEN; 23133 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23134 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23135 } 23136 23137 /* allocb() of adequate mblk assures space */ 23138 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23139 (uintptr_t)INT_MAX); 23140 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23141 /* 23142 * Get IP set to checksum on our behalf 23143 * Include the adjustment for a source route if any. 23144 */ 23145 u1 += tcp->tcp_sum; 23146 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23147 U16_TO_BE16(u1, tcph->th_sum); 23148 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23149 } 23150 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23151 (seq + data_length) == tcp->tcp_fss) { 23152 if (!tcp->tcp_fin_acked) { 23153 flags |= TH_FIN; 23154 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23155 } 23156 if (!tcp->tcp_fin_sent) { 23157 tcp->tcp_fin_sent = B_TRUE; 23158 switch (tcp->tcp_state) { 23159 case TCPS_SYN_RCVD: 23160 case TCPS_ESTABLISHED: 23161 tcp->tcp_state = TCPS_FIN_WAIT_1; 23162 break; 23163 case TCPS_CLOSE_WAIT: 23164 tcp->tcp_state = TCPS_LAST_ACK; 23165 break; 23166 } 23167 if (tcp->tcp_suna == tcp->tcp_snxt) 23168 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23169 tcp->tcp_snxt = tcp->tcp_fss + 1; 23170 } 23171 } 23172 /* 23173 * Note the trick here. u1 is unsigned. When tcp_urg 23174 * is smaller than seq, u1 will become a very huge value. 23175 * So the comparison will fail. Also note that tcp_urp 23176 * should be positive, see RFC 793 page 17. 23177 */ 23178 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23179 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23180 u1 < (uint32_t)(64 * 1024)) { 23181 flags |= TH_URG; 23182 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23183 U32_TO_ABE16(u1, tcph->th_urp); 23184 } 23185 } 23186 tcph->th_flags[0] = (uchar_t)flags; 23187 tcp->tcp_rack = tcp->tcp_rnxt; 23188 tcp->tcp_rack_cnt = 0; 23189 23190 if (tcp->tcp_snd_ts_ok) { 23191 if (tcp->tcp_state != TCPS_SYN_SENT) { 23192 uint32_t llbolt = (uint32_t)lbolt; 23193 23194 U32_TO_BE32(llbolt, 23195 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23196 U32_TO_BE32(tcp->tcp_ts_recent, 23197 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23198 } 23199 } 23200 23201 if (num_sack_blk > 0) { 23202 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23203 sack_blk_t *tmp; 23204 int32_t i; 23205 23206 wptr[0] = TCPOPT_NOP; 23207 wptr[1] = TCPOPT_NOP; 23208 wptr[2] = TCPOPT_SACK; 23209 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23210 sizeof (sack_blk_t); 23211 wptr += TCPOPT_REAL_SACK_LEN; 23212 23213 tmp = tcp->tcp_sack_list; 23214 for (i = 0; i < num_sack_blk; i++) { 23215 U32_TO_BE32(tmp[i].begin, wptr); 23216 wptr += sizeof (tcp_seq); 23217 U32_TO_BE32(tmp[i].end, wptr); 23218 wptr += sizeof (tcp_seq); 23219 } 23220 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23221 } 23222 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23223 data_length += (int)(mp1->b_wptr - rptr); 23224 if (tcp->tcp_ipversion == IPV4_VERSION) { 23225 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23226 } else { 23227 ip6_t *ip6 = (ip6_t *)(rptr + 23228 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23229 sizeof (ip6i_t) : 0)); 23230 23231 ip6->ip6_plen = htons(data_length - 23232 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23233 } 23234 23235 /* 23236 * Prime pump for IP 23237 * Include the adjustment for a source route if any. 23238 */ 23239 data_length -= tcp->tcp_ip_hdr_len; 23240 data_length += tcp->tcp_sum; 23241 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23242 U16_TO_ABE16(data_length, tcph->th_sum); 23243 if (tcp->tcp_ip_forward_progress) { 23244 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23245 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23246 tcp->tcp_ip_forward_progress = B_FALSE; 23247 } 23248 return (mp1); 23249 } 23250 23251 /* This function handles the push timeout. */ 23252 void 23253 tcp_push_timer(void *arg) 23254 { 23255 conn_t *connp = (conn_t *)arg; 23256 tcp_t *tcp = connp->conn_tcp; 23257 tcp_stack_t *tcps = tcp->tcp_tcps; 23258 23259 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23260 23261 ASSERT(tcp->tcp_listener == NULL); 23262 23263 /* 23264 * We need to plug synchronous streams during our drain to prevent 23265 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23266 */ 23267 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23268 tcp->tcp_push_tid = 0; 23269 if ((tcp->tcp_rcv_list != NULL) && 23270 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 23271 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23272 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23273 } 23274 23275 /* 23276 * This function handles delayed ACK timeout. 23277 */ 23278 static void 23279 tcp_ack_timer(void *arg) 23280 { 23281 conn_t *connp = (conn_t *)arg; 23282 tcp_t *tcp = connp->conn_tcp; 23283 mblk_t *mp; 23284 tcp_stack_t *tcps = tcp->tcp_tcps; 23285 23286 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23287 23288 tcp->tcp_ack_tid = 0; 23289 23290 if (tcp->tcp_fused) 23291 return; 23292 23293 /* 23294 * Do not send ACK if there is no outstanding unack'ed data. 23295 */ 23296 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23297 return; 23298 } 23299 23300 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23301 /* 23302 * Make sure we don't allow deferred ACKs to result in 23303 * timer-based ACKing. If we have held off an ACK 23304 * when there was more than an mss here, and the timer 23305 * goes off, we have to worry about the possibility 23306 * that the sender isn't doing slow-start, or is out 23307 * of step with us for some other reason. We fall 23308 * permanently back in the direction of 23309 * ACK-every-other-packet as suggested in RFC 1122. 23310 */ 23311 if (tcp->tcp_rack_abs_max > 2) 23312 tcp->tcp_rack_abs_max--; 23313 tcp->tcp_rack_cur_max = 2; 23314 } 23315 mp = tcp_ack_mp(tcp); 23316 23317 if (mp != NULL) { 23318 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 23319 BUMP_LOCAL(tcp->tcp_obsegs); 23320 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23321 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23322 tcp_send_data(tcp, tcp->tcp_wq, mp); 23323 } 23324 } 23325 23326 23327 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23328 static mblk_t * 23329 tcp_ack_mp(tcp_t *tcp) 23330 { 23331 uint32_t seq_no; 23332 tcp_stack_t *tcps = tcp->tcp_tcps; 23333 23334 /* 23335 * There are a few cases to be considered while setting the sequence no. 23336 * Essentially, we can come here while processing an unacceptable pkt 23337 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23338 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23339 * If we are here for a zero window probe, stick with suna. In all 23340 * other cases, we check if suna + swnd encompasses snxt and set 23341 * the sequence number to snxt, if so. If snxt falls outside the 23342 * window (the receiver probably shrunk its window), we will go with 23343 * suna + swnd, otherwise the sequence no will be unacceptable to the 23344 * receiver. 23345 */ 23346 if (tcp->tcp_zero_win_probe) { 23347 seq_no = tcp->tcp_suna; 23348 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23349 ASSERT(tcp->tcp_swnd == 0); 23350 seq_no = tcp->tcp_snxt; 23351 } else { 23352 seq_no = SEQ_GT(tcp->tcp_snxt, 23353 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23354 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23355 } 23356 23357 if (tcp->tcp_valid_bits) { 23358 /* 23359 * For the complex case where we have to send some 23360 * controls (FIN or SYN), let tcp_xmit_mp do it. 23361 */ 23362 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23363 NULL, B_FALSE)); 23364 } else { 23365 /* Generate a simple ACK */ 23366 int data_length; 23367 uchar_t *rptr; 23368 tcph_t *tcph; 23369 mblk_t *mp1; 23370 int32_t tcp_hdr_len; 23371 int32_t tcp_tcp_hdr_len; 23372 int32_t num_sack_blk = 0; 23373 int32_t sack_opt_len; 23374 23375 /* 23376 * Allocate space for TCP + IP headers 23377 * and link-level header 23378 */ 23379 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23380 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23381 tcp->tcp_num_sack_blk); 23382 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23383 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23384 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23385 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23386 } else { 23387 tcp_hdr_len = tcp->tcp_hdr_len; 23388 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23389 } 23390 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23391 if (!mp1) 23392 return (NULL); 23393 23394 /* Update the latest receive window size in TCP header. */ 23395 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23396 tcp->tcp_tcph->th_win); 23397 /* copy in prototype TCP + IP header */ 23398 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23399 mp1->b_rptr = rptr; 23400 mp1->b_wptr = rptr + tcp_hdr_len; 23401 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23402 23403 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23404 23405 /* Set the TCP sequence number. */ 23406 U32_TO_ABE32(seq_no, tcph->th_seq); 23407 23408 /* Set up the TCP flag field. */ 23409 tcph->th_flags[0] = (uchar_t)TH_ACK; 23410 if (tcp->tcp_ecn_echo_on) 23411 tcph->th_flags[0] |= TH_ECE; 23412 23413 tcp->tcp_rack = tcp->tcp_rnxt; 23414 tcp->tcp_rack_cnt = 0; 23415 23416 /* fill in timestamp option if in use */ 23417 if (tcp->tcp_snd_ts_ok) { 23418 uint32_t llbolt = (uint32_t)lbolt; 23419 23420 U32_TO_BE32(llbolt, 23421 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23422 U32_TO_BE32(tcp->tcp_ts_recent, 23423 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23424 } 23425 23426 /* Fill in SACK options */ 23427 if (num_sack_blk > 0) { 23428 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23429 sack_blk_t *tmp; 23430 int32_t i; 23431 23432 wptr[0] = TCPOPT_NOP; 23433 wptr[1] = TCPOPT_NOP; 23434 wptr[2] = TCPOPT_SACK; 23435 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23436 sizeof (sack_blk_t); 23437 wptr += TCPOPT_REAL_SACK_LEN; 23438 23439 tmp = tcp->tcp_sack_list; 23440 for (i = 0; i < num_sack_blk; i++) { 23441 U32_TO_BE32(tmp[i].begin, wptr); 23442 wptr += sizeof (tcp_seq); 23443 U32_TO_BE32(tmp[i].end, wptr); 23444 wptr += sizeof (tcp_seq); 23445 } 23446 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23447 << 4); 23448 } 23449 23450 if (tcp->tcp_ipversion == IPV4_VERSION) { 23451 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23452 } else { 23453 /* Check for ip6i_t header in sticky hdrs */ 23454 ip6_t *ip6 = (ip6_t *)(rptr + 23455 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23456 sizeof (ip6i_t) : 0)); 23457 23458 ip6->ip6_plen = htons(tcp_hdr_len - 23459 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23460 } 23461 23462 /* 23463 * Prime pump for checksum calculation in IP. Include the 23464 * adjustment for a source route if any. 23465 */ 23466 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23467 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23468 U16_TO_ABE16(data_length, tcph->th_sum); 23469 23470 if (tcp->tcp_ip_forward_progress) { 23471 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23472 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23473 tcp->tcp_ip_forward_progress = B_FALSE; 23474 } 23475 return (mp1); 23476 } 23477 } 23478 23479 /* 23480 * To create a temporary tcp structure for inserting into bind hash list. 23481 * The parameter is assumed to be in network byte order, ready for use. 23482 */ 23483 /* ARGSUSED */ 23484 static tcp_t * 23485 tcp_alloc_temp_tcp(in_port_t port, tcp_stack_t *tcps) 23486 { 23487 conn_t *connp; 23488 tcp_t *tcp; 23489 23490 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP, tcps->tcps_netstack); 23491 if (connp == NULL) 23492 return (NULL); 23493 23494 tcp = connp->conn_tcp; 23495 tcp->tcp_tcps = tcps; 23496 TCPS_REFHOLD(tcps); 23497 23498 /* 23499 * Only initialize the necessary info in those structures. Note 23500 * that since INADDR_ANY is all 0, we do not need to set 23501 * tcp_bound_source to INADDR_ANY here. 23502 */ 23503 tcp->tcp_state = TCPS_BOUND; 23504 tcp->tcp_lport = port; 23505 tcp->tcp_exclbind = 1; 23506 tcp->tcp_reserved_port = 1; 23507 23508 /* Just for place holding... */ 23509 tcp->tcp_ipversion = IPV4_VERSION; 23510 23511 return (tcp); 23512 } 23513 23514 /* 23515 * To remove a port range specified by lo_port and hi_port from the 23516 * reserved port ranges. This is one of the three public functions of 23517 * the reserved port interface. Note that a port range has to be removed 23518 * as a whole. Ports in a range cannot be removed individually. 23519 * 23520 * Params: 23521 * in_port_t lo_port: the beginning port of the reserved port range to 23522 * be deleted. 23523 * in_port_t hi_port: the ending port of the reserved port range to 23524 * be deleted. 23525 * 23526 * Return: 23527 * B_TRUE if the deletion is successful, B_FALSE otherwise. 23528 * 23529 * Assumes that nca is only for zoneid=0 23530 */ 23531 boolean_t 23532 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 23533 { 23534 int i, j; 23535 int size; 23536 tcp_t **temp_tcp_array; 23537 tcp_t *tcp; 23538 tcp_stack_t *tcps; 23539 23540 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23541 ASSERT(tcps != NULL); 23542 23543 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23544 23545 /* First make sure that the port ranage is indeed reserved. */ 23546 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23547 if (tcps->tcps_reserved_port[i].lo_port == lo_port) { 23548 hi_port = tcps->tcps_reserved_port[i].hi_port; 23549 temp_tcp_array = 23550 tcps->tcps_reserved_port[i].temp_tcp_array; 23551 break; 23552 } 23553 } 23554 if (i == tcps->tcps_reserved_port_array_size) { 23555 rw_exit(&tcps->tcps_reserved_port_lock); 23556 netstack_rele(tcps->tcps_netstack); 23557 return (B_FALSE); 23558 } 23559 23560 /* 23561 * Remove the range from the array. This simple loop is possible 23562 * because port ranges are inserted in ascending order. 23563 */ 23564 for (j = i; j < tcps->tcps_reserved_port_array_size - 1; j++) { 23565 tcps->tcps_reserved_port[j].lo_port = 23566 tcps->tcps_reserved_port[j+1].lo_port; 23567 tcps->tcps_reserved_port[j].hi_port = 23568 tcps->tcps_reserved_port[j+1].hi_port; 23569 tcps->tcps_reserved_port[j].temp_tcp_array = 23570 tcps->tcps_reserved_port[j+1].temp_tcp_array; 23571 } 23572 23573 /* Remove all the temporary tcp structures. */ 23574 size = hi_port - lo_port + 1; 23575 while (size > 0) { 23576 tcp = temp_tcp_array[size - 1]; 23577 ASSERT(tcp != NULL); 23578 tcp_bind_hash_remove(tcp); 23579 CONN_DEC_REF(tcp->tcp_connp); 23580 size--; 23581 } 23582 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 23583 tcps->tcps_reserved_port_array_size--; 23584 rw_exit(&tcps->tcps_reserved_port_lock); 23585 netstack_rele(tcps->tcps_netstack); 23586 return (B_TRUE); 23587 } 23588 23589 /* 23590 * Macro to remove temporary tcp structure from the bind hash list. The 23591 * first parameter is the list of tcp to be removed. The second parameter 23592 * is the number of tcps in the array. 23593 */ 23594 #define TCP_TMP_TCP_REMOVE(tcp_array, num, tcps) \ 23595 { \ 23596 while ((num) > 0) { \ 23597 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 23598 tf_t *tbf; \ 23599 tcp_t *tcpnext; \ 23600 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 23601 mutex_enter(&tbf->tf_lock); \ 23602 tcpnext = tcp->tcp_bind_hash; \ 23603 if (tcpnext) { \ 23604 tcpnext->tcp_ptpbhn = \ 23605 tcp->tcp_ptpbhn; \ 23606 } \ 23607 *tcp->tcp_ptpbhn = tcpnext; \ 23608 mutex_exit(&tbf->tf_lock); \ 23609 kmem_free(tcp, sizeof (tcp_t)); \ 23610 (tcp_array)[(num) - 1] = NULL; \ 23611 (num)--; \ 23612 } \ 23613 } 23614 23615 /* 23616 * The public interface for other modules to call to reserve a port range 23617 * in TCP. The caller passes in how large a port range it wants. TCP 23618 * will try to find a range and return it via lo_port and hi_port. This is 23619 * used by NCA's nca_conn_init. 23620 * NCA can only be used in the global zone so this only affects the global 23621 * zone's ports. 23622 * 23623 * Params: 23624 * int size: the size of the port range to be reserved. 23625 * in_port_t *lo_port (referenced): returns the beginning port of the 23626 * reserved port range added. 23627 * in_port_t *hi_port (referenced): returns the ending port of the 23628 * reserved port range added. 23629 * 23630 * Return: 23631 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 23632 * 23633 * Assumes that nca is only for zoneid=0 23634 */ 23635 boolean_t 23636 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 23637 { 23638 tcp_t *tcp; 23639 tcp_t *tmp_tcp; 23640 tcp_t **temp_tcp_array; 23641 tf_t *tbf; 23642 in_port_t net_port; 23643 in_port_t port; 23644 int32_t cur_size; 23645 int i, j; 23646 boolean_t used; 23647 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 23648 zoneid_t zoneid = GLOBAL_ZONEID; 23649 tcp_stack_t *tcps; 23650 23651 /* Sanity check. */ 23652 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 23653 return (B_FALSE); 23654 } 23655 23656 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23657 ASSERT(tcps != NULL); 23658 23659 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23660 if (tcps->tcps_reserved_port_array_size == 23661 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 23662 rw_exit(&tcps->tcps_reserved_port_lock); 23663 netstack_rele(tcps->tcps_netstack); 23664 return (B_FALSE); 23665 } 23666 23667 /* 23668 * Find the starting port to try. Since the port ranges are ordered 23669 * in the reserved port array, we can do a simple search here. 23670 */ 23671 *lo_port = TCP_SMALLEST_RESERVED_PORT; 23672 *hi_port = TCP_LARGEST_RESERVED_PORT; 23673 for (i = 0; i < tcps->tcps_reserved_port_array_size; 23674 *lo_port = tcps->tcps_reserved_port[i].hi_port + 1, i++) { 23675 if (tcps->tcps_reserved_port[i].lo_port - *lo_port >= size) { 23676 *hi_port = tcps->tcps_reserved_port[i].lo_port - 1; 23677 break; 23678 } 23679 } 23680 /* No available port range. */ 23681 if (i == tcps->tcps_reserved_port_array_size && 23682 *hi_port - *lo_port < size) { 23683 rw_exit(&tcps->tcps_reserved_port_lock); 23684 netstack_rele(tcps->tcps_netstack); 23685 return (B_FALSE); 23686 } 23687 23688 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 23689 if (temp_tcp_array == NULL) { 23690 rw_exit(&tcps->tcps_reserved_port_lock); 23691 netstack_rele(tcps->tcps_netstack); 23692 return (B_FALSE); 23693 } 23694 23695 /* Go thru the port range to see if some ports are already bound. */ 23696 for (port = *lo_port, cur_size = 0; 23697 cur_size < size && port <= *hi_port; 23698 cur_size++, port++) { 23699 used = B_FALSE; 23700 net_port = htons(port); 23701 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(net_port)]; 23702 mutex_enter(&tbf->tf_lock); 23703 for (tcp = tbf->tf_tcp; tcp != NULL; 23704 tcp = tcp->tcp_bind_hash) { 23705 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 23706 net_port == tcp->tcp_lport) { 23707 /* 23708 * A port is already bound. Search again 23709 * starting from port + 1. Release all 23710 * temporary tcps. 23711 */ 23712 mutex_exit(&tbf->tf_lock); 23713 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23714 tcps); 23715 *lo_port = port + 1; 23716 cur_size = -1; 23717 used = B_TRUE; 23718 break; 23719 } 23720 } 23721 if (!used) { 23722 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port, tcps)) == 23723 NULL) { 23724 /* 23725 * Allocation failure. Just fail the request. 23726 * Need to remove all those temporary tcp 23727 * structures. 23728 */ 23729 mutex_exit(&tbf->tf_lock); 23730 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23731 tcps); 23732 rw_exit(&tcps->tcps_reserved_port_lock); 23733 kmem_free(temp_tcp_array, 23734 (hi_port - lo_port + 1) * 23735 sizeof (tcp_t *)); 23736 netstack_rele(tcps->tcps_netstack); 23737 return (B_FALSE); 23738 } 23739 temp_tcp_array[cur_size] = tmp_tcp; 23740 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 23741 mutex_exit(&tbf->tf_lock); 23742 } 23743 } 23744 23745 /* 23746 * The current range is not large enough. We can actually do another 23747 * search if this search is done between 2 reserved port ranges. But 23748 * for first release, we just stop here and return saying that no port 23749 * range is available. 23750 */ 23751 if (cur_size < size) { 23752 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, tcps); 23753 rw_exit(&tcps->tcps_reserved_port_lock); 23754 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 23755 netstack_rele(tcps->tcps_netstack); 23756 return (B_FALSE); 23757 } 23758 *hi_port = port - 1; 23759 23760 /* 23761 * Insert range into array in ascending order. Since this function 23762 * must not be called often, we choose to use the simplest method. 23763 * The above array should not consume excessive stack space as 23764 * the size must be very small. If in future releases, we find 23765 * that we should provide more reserved port ranges, this function 23766 * has to be modified to be more efficient. 23767 */ 23768 if (tcps->tcps_reserved_port_array_size == 0) { 23769 tcps->tcps_reserved_port[0].lo_port = *lo_port; 23770 tcps->tcps_reserved_port[0].hi_port = *hi_port; 23771 tcps->tcps_reserved_port[0].temp_tcp_array = temp_tcp_array; 23772 } else { 23773 for (i = 0, j = 0; i < tcps->tcps_reserved_port_array_size; 23774 i++, j++) { 23775 if (*lo_port < tcps->tcps_reserved_port[i].lo_port && 23776 i == j) { 23777 tmp_ports[j].lo_port = *lo_port; 23778 tmp_ports[j].hi_port = *hi_port; 23779 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23780 j++; 23781 } 23782 tmp_ports[j].lo_port = 23783 tcps->tcps_reserved_port[i].lo_port; 23784 tmp_ports[j].hi_port = 23785 tcps->tcps_reserved_port[i].hi_port; 23786 tmp_ports[j].temp_tcp_array = 23787 tcps->tcps_reserved_port[i].temp_tcp_array; 23788 } 23789 if (j == i) { 23790 tmp_ports[j].lo_port = *lo_port; 23791 tmp_ports[j].hi_port = *hi_port; 23792 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23793 } 23794 bcopy(tmp_ports, tcps->tcps_reserved_port, sizeof (tmp_ports)); 23795 } 23796 tcps->tcps_reserved_port_array_size++; 23797 rw_exit(&tcps->tcps_reserved_port_lock); 23798 netstack_rele(tcps->tcps_netstack); 23799 return (B_TRUE); 23800 } 23801 23802 /* 23803 * Check to see if a port is in any reserved port range. 23804 * 23805 * Params: 23806 * in_port_t port: the port to be verified. 23807 * 23808 * Return: 23809 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 23810 */ 23811 boolean_t 23812 tcp_reserved_port_check(in_port_t port, tcp_stack_t *tcps) 23813 { 23814 int i; 23815 23816 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23817 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23818 if (port >= tcps->tcps_reserved_port[i].lo_port || 23819 port <= tcps->tcps_reserved_port[i].hi_port) { 23820 rw_exit(&tcps->tcps_reserved_port_lock); 23821 return (B_TRUE); 23822 } 23823 } 23824 rw_exit(&tcps->tcps_reserved_port_lock); 23825 return (B_FALSE); 23826 } 23827 23828 /* 23829 * To list all reserved port ranges. This is the function to handle 23830 * ndd tcp_reserved_port_list. 23831 */ 23832 /* ARGSUSED */ 23833 static int 23834 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23835 { 23836 int i; 23837 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23838 23839 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23840 if (tcps->tcps_reserved_port_array_size > 0) 23841 (void) mi_mpprintf(mp, "The following ports are reserved:"); 23842 else 23843 (void) mi_mpprintf(mp, "No port is reserved."); 23844 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23845 (void) mi_mpprintf(mp, "%d-%d", 23846 tcps->tcps_reserved_port[i].lo_port, 23847 tcps->tcps_reserved_port[i].hi_port); 23848 } 23849 rw_exit(&tcps->tcps_reserved_port_lock); 23850 return (0); 23851 } 23852 23853 /* 23854 * Hash list insertion routine for tcp_t structures. 23855 * Inserts entries with the ones bound to a specific IP address first 23856 * followed by those bound to INADDR_ANY. 23857 */ 23858 static void 23859 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23860 { 23861 tcp_t **tcpp; 23862 tcp_t *tcpnext; 23863 23864 if (tcp->tcp_ptpbhn != NULL) { 23865 ASSERT(!caller_holds_lock); 23866 tcp_bind_hash_remove(tcp); 23867 } 23868 tcpp = &tbf->tf_tcp; 23869 if (!caller_holds_lock) { 23870 mutex_enter(&tbf->tf_lock); 23871 } else { 23872 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23873 } 23874 tcpnext = tcpp[0]; 23875 if (tcpnext) { 23876 /* 23877 * If the new tcp bound to the INADDR_ANY address 23878 * and the first one in the list is not bound to 23879 * INADDR_ANY we skip all entries until we find the 23880 * first one bound to INADDR_ANY. 23881 * This makes sure that applications binding to a 23882 * specific address get preference over those binding to 23883 * INADDR_ANY. 23884 */ 23885 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23886 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23887 while ((tcpnext = tcpp[0]) != NULL && 23888 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23889 tcpp = &(tcpnext->tcp_bind_hash); 23890 if (tcpnext) 23891 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23892 } else 23893 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23894 } 23895 tcp->tcp_bind_hash = tcpnext; 23896 tcp->tcp_ptpbhn = tcpp; 23897 tcpp[0] = tcp; 23898 if (!caller_holds_lock) 23899 mutex_exit(&tbf->tf_lock); 23900 } 23901 23902 /* 23903 * Hash list removal routine for tcp_t structures. 23904 */ 23905 static void 23906 tcp_bind_hash_remove(tcp_t *tcp) 23907 { 23908 tcp_t *tcpnext; 23909 kmutex_t *lockp; 23910 tcp_stack_t *tcps = tcp->tcp_tcps; 23911 23912 if (tcp->tcp_ptpbhn == NULL) 23913 return; 23914 23915 /* 23916 * Extract the lock pointer in case there are concurrent 23917 * hash_remove's for this instance. 23918 */ 23919 ASSERT(tcp->tcp_lport != 0); 23920 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23921 23922 ASSERT(lockp != NULL); 23923 mutex_enter(lockp); 23924 if (tcp->tcp_ptpbhn) { 23925 tcpnext = tcp->tcp_bind_hash; 23926 if (tcpnext) { 23927 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23928 tcp->tcp_bind_hash = NULL; 23929 } 23930 *tcp->tcp_ptpbhn = tcpnext; 23931 tcp->tcp_ptpbhn = NULL; 23932 } 23933 mutex_exit(lockp); 23934 } 23935 23936 23937 /* 23938 * Hash list lookup routine for tcp_t structures. 23939 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23940 */ 23941 static tcp_t * 23942 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23943 { 23944 tf_t *tf; 23945 tcp_t *tcp; 23946 23947 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23948 mutex_enter(&tf->tf_lock); 23949 for (tcp = tf->tf_tcp; tcp != NULL; 23950 tcp = tcp->tcp_acceptor_hash) { 23951 if (tcp->tcp_acceptor_id == id) { 23952 CONN_INC_REF(tcp->tcp_connp); 23953 mutex_exit(&tf->tf_lock); 23954 return (tcp); 23955 } 23956 } 23957 mutex_exit(&tf->tf_lock); 23958 return (NULL); 23959 } 23960 23961 23962 /* 23963 * Hash list insertion routine for tcp_t structures. 23964 */ 23965 void 23966 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23967 { 23968 tf_t *tf; 23969 tcp_t **tcpp; 23970 tcp_t *tcpnext; 23971 tcp_stack_t *tcps = tcp->tcp_tcps; 23972 23973 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23974 23975 if (tcp->tcp_ptpahn != NULL) 23976 tcp_acceptor_hash_remove(tcp); 23977 tcpp = &tf->tf_tcp; 23978 mutex_enter(&tf->tf_lock); 23979 tcpnext = tcpp[0]; 23980 if (tcpnext) 23981 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23982 tcp->tcp_acceptor_hash = tcpnext; 23983 tcp->tcp_ptpahn = tcpp; 23984 tcpp[0] = tcp; 23985 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23986 mutex_exit(&tf->tf_lock); 23987 } 23988 23989 /* 23990 * Hash list removal routine for tcp_t structures. 23991 */ 23992 static void 23993 tcp_acceptor_hash_remove(tcp_t *tcp) 23994 { 23995 tcp_t *tcpnext; 23996 kmutex_t *lockp; 23997 23998 /* 23999 * Extract the lock pointer in case there are concurrent 24000 * hash_remove's for this instance. 24001 */ 24002 lockp = tcp->tcp_acceptor_lockp; 24003 24004 if (tcp->tcp_ptpahn == NULL) 24005 return; 24006 24007 ASSERT(lockp != NULL); 24008 mutex_enter(lockp); 24009 if (tcp->tcp_ptpahn) { 24010 tcpnext = tcp->tcp_acceptor_hash; 24011 if (tcpnext) { 24012 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24013 tcp->tcp_acceptor_hash = NULL; 24014 } 24015 *tcp->tcp_ptpahn = tcpnext; 24016 tcp->tcp_ptpahn = NULL; 24017 } 24018 mutex_exit(lockp); 24019 tcp->tcp_acceptor_lockp = NULL; 24020 } 24021 24022 /* ARGSUSED */ 24023 static int 24024 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 24025 { 24026 int error = 0; 24027 int retval; 24028 char *end; 24029 tcp_hsp_t *hsp; 24030 tcp_hsp_t *hspprev; 24031 ipaddr_t addr = 0; /* Address we're looking for */ 24032 in6_addr_t v6addr; /* Address we're looking for */ 24033 uint32_t hash; /* Hash of that address */ 24034 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24035 24036 /* 24037 * If the following variables are still zero after parsing the input 24038 * string, the user didn't specify them and we don't change them in 24039 * the HSP. 24040 */ 24041 24042 ipaddr_t mask = 0; /* Subnet mask */ 24043 in6_addr_t v6mask; 24044 long sendspace = 0; /* Send buffer size */ 24045 long recvspace = 0; /* Receive buffer size */ 24046 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 24047 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 24048 24049 rw_enter(&tcps->tcps_hsp_lock, RW_WRITER); 24050 24051 /* Parse and validate address */ 24052 if (af == AF_INET) { 24053 retval = inet_pton(af, value, &addr); 24054 if (retval == 1) 24055 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 24056 } else if (af == AF_INET6) { 24057 retval = inet_pton(af, value, &v6addr); 24058 } else { 24059 error = EINVAL; 24060 goto done; 24061 } 24062 if (retval == 0) { 24063 error = EINVAL; 24064 goto done; 24065 } 24066 24067 while ((*value) && *value != ' ') 24068 value++; 24069 24070 /* Parse individual keywords, set variables if found */ 24071 while (*value) { 24072 /* Skip leading blanks */ 24073 24074 while (*value == ' ' || *value == '\t') 24075 value++; 24076 24077 /* If at end of string, we're done */ 24078 24079 if (!*value) 24080 break; 24081 24082 /* We have a word, figure out what it is */ 24083 24084 if (strncmp("mask", value, 4) == 0) { 24085 value += 4; 24086 while (*value == ' ' || *value == '\t') 24087 value++; 24088 /* Parse subnet mask */ 24089 if (af == AF_INET) { 24090 retval = inet_pton(af, value, &mask); 24091 if (retval == 1) { 24092 V4MASK_TO_V6(mask, v6mask); 24093 } 24094 } else if (af == AF_INET6) { 24095 retval = inet_pton(af, value, &v6mask); 24096 } 24097 if (retval != 1) { 24098 error = EINVAL; 24099 goto done; 24100 } 24101 while ((*value) && *value != ' ') 24102 value++; 24103 } else if (strncmp("sendspace", value, 9) == 0) { 24104 value += 9; 24105 24106 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 24107 sendspace < TCP_XMIT_HIWATER || 24108 sendspace >= (1L<<30)) { 24109 error = EINVAL; 24110 goto done; 24111 } 24112 value = end; 24113 } else if (strncmp("recvspace", value, 9) == 0) { 24114 value += 9; 24115 24116 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 24117 recvspace < TCP_RECV_HIWATER || 24118 recvspace >= (1L<<30)) { 24119 error = EINVAL; 24120 goto done; 24121 } 24122 value = end; 24123 } else if (strncmp("timestamp", value, 9) == 0) { 24124 value += 9; 24125 24126 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 24127 timestamp < 0 || timestamp > 1) { 24128 error = EINVAL; 24129 goto done; 24130 } 24131 24132 /* 24133 * We increment timestamp so we know it's been set; 24134 * this is undone when we put it in the HSP 24135 */ 24136 timestamp++; 24137 value = end; 24138 } else if (strncmp("delete", value, 6) == 0) { 24139 value += 6; 24140 delete = B_TRUE; 24141 } else { 24142 error = EINVAL; 24143 goto done; 24144 } 24145 } 24146 24147 /* Hash address for lookup */ 24148 24149 hash = TCP_HSP_HASH(addr); 24150 24151 if (delete) { 24152 /* 24153 * Note that deletes don't return an error if the thing 24154 * we're trying to delete isn't there. 24155 */ 24156 if (tcps->tcps_hsp_hash == NULL) 24157 goto done; 24158 hsp = tcps->tcps_hsp_hash[hash]; 24159 24160 if (hsp) { 24161 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24162 &v6addr)) { 24163 tcps->tcps_hsp_hash[hash] = hsp->tcp_hsp_next; 24164 mi_free((char *)hsp); 24165 } else { 24166 hspprev = hsp; 24167 while ((hsp = hsp->tcp_hsp_next) != NULL) { 24168 if (IN6_ARE_ADDR_EQUAL( 24169 &hsp->tcp_hsp_addr_v6, &v6addr)) { 24170 hspprev->tcp_hsp_next = 24171 hsp->tcp_hsp_next; 24172 mi_free((char *)hsp); 24173 break; 24174 } 24175 hspprev = hsp; 24176 } 24177 } 24178 } 24179 } else { 24180 /* 24181 * We're adding/modifying an HSP. If we haven't already done 24182 * so, allocate the hash table. 24183 */ 24184 24185 if (!tcps->tcps_hsp_hash) { 24186 tcps->tcps_hsp_hash = (tcp_hsp_t **) 24187 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 24188 if (!tcps->tcps_hsp_hash) { 24189 error = EINVAL; 24190 goto done; 24191 } 24192 } 24193 24194 /* Get head of hash chain */ 24195 24196 hsp = tcps->tcps_hsp_hash[hash]; 24197 24198 /* Try to find pre-existing hsp on hash chain */ 24199 /* Doesn't handle CIDR prefixes. */ 24200 while (hsp) { 24201 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 24202 break; 24203 hsp = hsp->tcp_hsp_next; 24204 } 24205 24206 /* 24207 * If we didn't, create one with default values and put it 24208 * at head of hash chain 24209 */ 24210 24211 if (!hsp) { 24212 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 24213 if (!hsp) { 24214 error = EINVAL; 24215 goto done; 24216 } 24217 hsp->tcp_hsp_next = tcps->tcps_hsp_hash[hash]; 24218 tcps->tcps_hsp_hash[hash] = hsp; 24219 } 24220 24221 /* Set values that the user asked us to change */ 24222 24223 hsp->tcp_hsp_addr_v6 = v6addr; 24224 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 24225 hsp->tcp_hsp_vers = IPV4_VERSION; 24226 else 24227 hsp->tcp_hsp_vers = IPV6_VERSION; 24228 hsp->tcp_hsp_subnet_v6 = v6mask; 24229 if (sendspace > 0) 24230 hsp->tcp_hsp_sendspace = sendspace; 24231 if (recvspace > 0) 24232 hsp->tcp_hsp_recvspace = recvspace; 24233 if (timestamp > 0) 24234 hsp->tcp_hsp_tstamp = timestamp - 1; 24235 } 24236 24237 done: 24238 rw_exit(&tcps->tcps_hsp_lock); 24239 return (error); 24240 } 24241 24242 /* Set callback routine passed to nd_load by tcp_param_register. */ 24243 /* ARGSUSED */ 24244 static int 24245 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 24246 { 24247 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 24248 } 24249 /* ARGSUSED */ 24250 static int 24251 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24252 cred_t *cr) 24253 { 24254 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 24255 } 24256 24257 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 24258 /* ARGSUSED */ 24259 static int 24260 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 24261 { 24262 tcp_hsp_t *hsp; 24263 int i; 24264 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 24265 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24266 24267 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24268 (void) mi_mpprintf(mp, 24269 "Hash HSP " MI_COL_HDRPAD_STR 24270 "Address Subnet Mask Send Receive TStamp"); 24271 if (tcps->tcps_hsp_hash) { 24272 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 24273 hsp = tcps->tcps_hsp_hash[i]; 24274 while (hsp) { 24275 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 24276 (void) inet_ntop(AF_INET, 24277 &hsp->tcp_hsp_addr, 24278 addrbuf, sizeof (addrbuf)); 24279 (void) inet_ntop(AF_INET, 24280 &hsp->tcp_hsp_subnet, 24281 subnetbuf, sizeof (subnetbuf)); 24282 } else { 24283 (void) inet_ntop(AF_INET6, 24284 &hsp->tcp_hsp_addr_v6, 24285 addrbuf, sizeof (addrbuf)); 24286 (void) inet_ntop(AF_INET6, 24287 &hsp->tcp_hsp_subnet_v6, 24288 subnetbuf, sizeof (subnetbuf)); 24289 } 24290 (void) mi_mpprintf(mp, 24291 " %03d " MI_COL_PTRFMT_STR 24292 "%s %s %010d %010d %d", 24293 i, 24294 (void *)hsp, 24295 addrbuf, 24296 subnetbuf, 24297 hsp->tcp_hsp_sendspace, 24298 hsp->tcp_hsp_recvspace, 24299 hsp->tcp_hsp_tstamp); 24300 24301 hsp = hsp->tcp_hsp_next; 24302 } 24303 } 24304 } 24305 rw_exit(&tcps->tcps_hsp_lock); 24306 return (0); 24307 } 24308 24309 24310 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24311 24312 static ipaddr_t netmasks[] = { 24313 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24314 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24315 }; 24316 24317 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24318 24319 /* 24320 * XXX This routine should go away and instead we should use the metrics 24321 * associated with the routes to determine the default sndspace and rcvspace. 24322 */ 24323 static tcp_hsp_t * 24324 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24325 { 24326 tcp_hsp_t *hsp = NULL; 24327 24328 /* Quick check without acquiring the lock. */ 24329 if (tcps->tcps_hsp_hash == NULL) 24330 return (NULL); 24331 24332 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24333 24334 /* This routine finds the best-matching HSP for address addr. */ 24335 24336 if (tcps->tcps_hsp_hash) { 24337 int i; 24338 ipaddr_t srchaddr; 24339 tcp_hsp_t *hsp_net; 24340 24341 /* We do three passes: host, network, and subnet. */ 24342 24343 srchaddr = addr; 24344 24345 for (i = 1; i <= 3; i++) { 24346 /* Look for exact match on srchaddr */ 24347 24348 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24349 while (hsp) { 24350 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24351 hsp->tcp_hsp_addr == srchaddr) 24352 break; 24353 hsp = hsp->tcp_hsp_next; 24354 } 24355 ASSERT(hsp == NULL || 24356 hsp->tcp_hsp_vers == IPV4_VERSION); 24357 24358 /* 24359 * If this is the first pass: 24360 * If we found a match, great, return it. 24361 * If not, search for the network on the second pass. 24362 */ 24363 24364 if (i == 1) 24365 if (hsp) 24366 break; 24367 else 24368 { 24369 srchaddr = addr & netmask(addr); 24370 continue; 24371 } 24372 24373 /* 24374 * If this is the second pass: 24375 * If we found a match, but there's a subnet mask, 24376 * save the match but try again using the subnet 24377 * mask on the third pass. 24378 * Otherwise, return whatever we found. 24379 */ 24380 24381 if (i == 2) { 24382 if (hsp && hsp->tcp_hsp_subnet) { 24383 hsp_net = hsp; 24384 srchaddr = addr & hsp->tcp_hsp_subnet; 24385 continue; 24386 } else { 24387 break; 24388 } 24389 } 24390 24391 /* 24392 * This must be the third pass. If we didn't find 24393 * anything, return the saved network HSP instead. 24394 */ 24395 24396 if (!hsp) 24397 hsp = hsp_net; 24398 } 24399 } 24400 24401 rw_exit(&tcps->tcps_hsp_lock); 24402 return (hsp); 24403 } 24404 24405 /* 24406 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24407 * match lookup. 24408 */ 24409 static tcp_hsp_t * 24410 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24411 { 24412 tcp_hsp_t *hsp = NULL; 24413 24414 /* Quick check without acquiring the lock. */ 24415 if (tcps->tcps_hsp_hash == NULL) 24416 return (NULL); 24417 24418 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24419 24420 /* This routine finds the best-matching HSP for address addr. */ 24421 24422 if (tcps->tcps_hsp_hash) { 24423 int i; 24424 in6_addr_t v6srchaddr; 24425 tcp_hsp_t *hsp_net; 24426 24427 /* We do three passes: host, network, and subnet. */ 24428 24429 v6srchaddr = *v6addr; 24430 24431 for (i = 1; i <= 3; i++) { 24432 /* Look for exact match on srchaddr */ 24433 24434 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24435 V4_PART_OF_V6(v6srchaddr))]; 24436 while (hsp) { 24437 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24438 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24439 &v6srchaddr)) 24440 break; 24441 hsp = hsp->tcp_hsp_next; 24442 } 24443 24444 /* 24445 * If this is the first pass: 24446 * If we found a match, great, return it. 24447 * If not, search for the network on the second pass. 24448 */ 24449 24450 if (i == 1) 24451 if (hsp) 24452 break; 24453 else { 24454 /* Assume a 64 bit mask */ 24455 v6srchaddr.s6_addr32[0] = 24456 v6addr->s6_addr32[0]; 24457 v6srchaddr.s6_addr32[1] = 24458 v6addr->s6_addr32[1]; 24459 v6srchaddr.s6_addr32[2] = 0; 24460 v6srchaddr.s6_addr32[3] = 0; 24461 continue; 24462 } 24463 24464 /* 24465 * If this is the second pass: 24466 * If we found a match, but there's a subnet mask, 24467 * save the match but try again using the subnet 24468 * mask on the third pass. 24469 * Otherwise, return whatever we found. 24470 */ 24471 24472 if (i == 2) { 24473 ASSERT(hsp == NULL || 24474 hsp->tcp_hsp_vers == IPV6_VERSION); 24475 if (hsp && 24476 !IN6_IS_ADDR_UNSPECIFIED( 24477 &hsp->tcp_hsp_subnet_v6)) { 24478 hsp_net = hsp; 24479 V6_MASK_COPY(*v6addr, 24480 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24481 continue; 24482 } else { 24483 break; 24484 } 24485 } 24486 24487 /* 24488 * This must be the third pass. If we didn't find 24489 * anything, return the saved network HSP instead. 24490 */ 24491 24492 if (!hsp) 24493 hsp = hsp_net; 24494 } 24495 } 24496 24497 rw_exit(&tcps->tcps_hsp_lock); 24498 return (hsp); 24499 } 24500 24501 /* 24502 * Type three generator adapted from the random() function in 4.4 BSD: 24503 */ 24504 24505 /* 24506 * Copyright (c) 1983, 1993 24507 * The Regents of the University of California. All rights reserved. 24508 * 24509 * Redistribution and use in source and binary forms, with or without 24510 * modification, are permitted provided that the following conditions 24511 * are met: 24512 * 1. Redistributions of source code must retain the above copyright 24513 * notice, this list of conditions and the following disclaimer. 24514 * 2. Redistributions in binary form must reproduce the above copyright 24515 * notice, this list of conditions and the following disclaimer in the 24516 * documentation and/or other materials provided with the distribution. 24517 * 3. All advertising materials mentioning features or use of this software 24518 * must display the following acknowledgement: 24519 * This product includes software developed by the University of 24520 * California, Berkeley and its contributors. 24521 * 4. Neither the name of the University nor the names of its contributors 24522 * may be used to endorse or promote products derived from this software 24523 * without specific prior written permission. 24524 * 24525 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24526 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24527 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24528 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24529 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24530 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24531 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24532 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24533 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24534 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24535 * SUCH DAMAGE. 24536 */ 24537 24538 /* Type 3 -- x**31 + x**3 + 1 */ 24539 #define DEG_3 31 24540 #define SEP_3 3 24541 24542 24543 /* Protected by tcp_random_lock */ 24544 static int tcp_randtbl[DEG_3 + 1]; 24545 24546 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24547 static int *tcp_random_rptr = &tcp_randtbl[1]; 24548 24549 static int *tcp_random_state = &tcp_randtbl[1]; 24550 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24551 24552 kmutex_t tcp_random_lock; 24553 24554 void 24555 tcp_random_init(void) 24556 { 24557 int i; 24558 hrtime_t hrt; 24559 time_t wallclock; 24560 uint64_t result; 24561 24562 /* 24563 * Use high-res timer and current time for seed. Gethrtime() returns 24564 * a longlong, which may contain resolution down to nanoseconds. 24565 * The current time will either be a 32-bit or a 64-bit quantity. 24566 * XOR the two together in a 64-bit result variable. 24567 * Convert the result to a 32-bit value by multiplying the high-order 24568 * 32-bits by the low-order 32-bits. 24569 */ 24570 24571 hrt = gethrtime(); 24572 (void) drv_getparm(TIME, &wallclock); 24573 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24574 mutex_enter(&tcp_random_lock); 24575 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24576 (result & 0xffffffff); 24577 24578 for (i = 1; i < DEG_3; i++) 24579 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24580 + 12345; 24581 tcp_random_fptr = &tcp_random_state[SEP_3]; 24582 tcp_random_rptr = &tcp_random_state[0]; 24583 mutex_exit(&tcp_random_lock); 24584 for (i = 0; i < 10 * DEG_3; i++) 24585 (void) tcp_random(); 24586 } 24587 24588 /* 24589 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24590 * This range is selected to be approximately centered on TCP_ISS / 2, 24591 * and easy to compute. We get this value by generating a 32-bit random 24592 * number, selecting out the high-order 17 bits, and then adding one so 24593 * that we never return zero. 24594 */ 24595 int 24596 tcp_random(void) 24597 { 24598 int i; 24599 24600 mutex_enter(&tcp_random_lock); 24601 *tcp_random_fptr += *tcp_random_rptr; 24602 24603 /* 24604 * The high-order bits are more random than the low-order bits, 24605 * so we select out the high-order 17 bits and add one so that 24606 * we never return zero. 24607 */ 24608 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24609 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24610 tcp_random_fptr = tcp_random_state; 24611 ++tcp_random_rptr; 24612 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24613 tcp_random_rptr = tcp_random_state; 24614 24615 mutex_exit(&tcp_random_lock); 24616 return (i); 24617 } 24618 24619 /* 24620 * XXX This will go away when TPI is extended to send 24621 * info reqs to sockfs/timod ..... 24622 * Given a queue, set the max packet size for the write 24623 * side of the queue below stream head. This value is 24624 * cached on the stream head. 24625 * Returns 1 on success, 0 otherwise. 24626 */ 24627 static int 24628 setmaxps(queue_t *q, int maxpsz) 24629 { 24630 struct stdata *stp; 24631 queue_t *wq; 24632 stp = STREAM(q); 24633 24634 /* 24635 * At this point change of a queue parameter is not allowed 24636 * when a multiplexor is sitting on top. 24637 */ 24638 if (stp->sd_flag & STPLEX) 24639 return (0); 24640 24641 claimstr(stp->sd_wrq); 24642 wq = stp->sd_wrq->q_next; 24643 ASSERT(wq != NULL); 24644 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24645 releasestr(stp->sd_wrq); 24646 return (1); 24647 } 24648 24649 static int 24650 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24651 int *t_errorp, int *sys_errorp) 24652 { 24653 int error; 24654 int is_absreq_failure; 24655 t_scalar_t *opt_lenp; 24656 t_scalar_t opt_offset; 24657 int prim_type; 24658 struct T_conn_req *tcreqp; 24659 struct T_conn_res *tcresp; 24660 cred_t *cr; 24661 24662 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24663 24664 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24665 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24666 prim_type == T_CONN_RES); 24667 24668 switch (prim_type) { 24669 case T_CONN_REQ: 24670 tcreqp = (struct T_conn_req *)mp->b_rptr; 24671 opt_offset = tcreqp->OPT_offset; 24672 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24673 break; 24674 case O_T_CONN_RES: 24675 case T_CONN_RES: 24676 tcresp = (struct T_conn_res *)mp->b_rptr; 24677 opt_offset = tcresp->OPT_offset; 24678 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24679 break; 24680 } 24681 24682 *t_errorp = 0; 24683 *sys_errorp = 0; 24684 *do_disconnectp = 0; 24685 24686 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24687 opt_offset, cr, &tcp_opt_obj, 24688 NULL, &is_absreq_failure); 24689 24690 switch (error) { 24691 case 0: /* no error */ 24692 ASSERT(is_absreq_failure == 0); 24693 return (0); 24694 case ENOPROTOOPT: 24695 *t_errorp = TBADOPT; 24696 break; 24697 case EACCES: 24698 *t_errorp = TACCES; 24699 break; 24700 default: 24701 *t_errorp = TSYSERR; *sys_errorp = error; 24702 break; 24703 } 24704 if (is_absreq_failure != 0) { 24705 /* 24706 * The connection request should get the local ack 24707 * T_OK_ACK and then a T_DISCON_IND. 24708 */ 24709 *do_disconnectp = 1; 24710 } 24711 return (-1); 24712 } 24713 24714 /* 24715 * Split this function out so that if the secret changes, I'm okay. 24716 * 24717 * Initialize the tcp_iss_cookie and tcp_iss_key. 24718 */ 24719 24720 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24721 24722 static void 24723 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24724 { 24725 struct { 24726 int32_t current_time; 24727 uint32_t randnum; 24728 uint16_t pad; 24729 uint8_t ether[6]; 24730 uint8_t passwd[PASSWD_SIZE]; 24731 } tcp_iss_cookie; 24732 time_t t; 24733 24734 /* 24735 * Start with the current absolute time. 24736 */ 24737 (void) drv_getparm(TIME, &t); 24738 tcp_iss_cookie.current_time = t; 24739 24740 /* 24741 * XXX - Need a more random number per RFC 1750, not this crap. 24742 * OTOH, if what follows is pretty random, then I'm in better shape. 24743 */ 24744 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24745 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24746 24747 /* 24748 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24749 * as a good template. 24750 */ 24751 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24752 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24753 24754 /* 24755 * The pass-phrase. Normally this is supplied by user-called NDD. 24756 */ 24757 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24758 24759 /* 24760 * See 4010593 if this section becomes a problem again, 24761 * but the local ethernet address is useful here. 24762 */ 24763 (void) localetheraddr(NULL, 24764 (struct ether_addr *)&tcp_iss_cookie.ether); 24765 24766 /* 24767 * Hash 'em all together. The MD5Final is called per-connection. 24768 */ 24769 mutex_enter(&tcps->tcps_iss_key_lock); 24770 MD5Init(&tcps->tcps_iss_key); 24771 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24772 sizeof (tcp_iss_cookie)); 24773 mutex_exit(&tcps->tcps_iss_key_lock); 24774 } 24775 24776 /* 24777 * Set the RFC 1948 pass phrase 24778 */ 24779 /* ARGSUSED */ 24780 static int 24781 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24782 cred_t *cr) 24783 { 24784 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24785 24786 /* 24787 * Basically, value contains a new pass phrase. Pass it along! 24788 */ 24789 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24790 return (0); 24791 } 24792 24793 /* ARGSUSED */ 24794 static int 24795 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24796 { 24797 bzero(buf, sizeof (tcp_sack_info_t)); 24798 return (0); 24799 } 24800 24801 /* ARGSUSED */ 24802 static int 24803 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24804 { 24805 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24806 return (0); 24807 } 24808 24809 /* 24810 * Make sure we wait until the default queue is setup, yet allow 24811 * tcp_g_q_create() to open a TCP stream. 24812 * We need to allow tcp_g_q_create() do do an open 24813 * of tcp, hence we compare curhread. 24814 * All others have to wait until the tcps_g_q has been 24815 * setup. 24816 */ 24817 void 24818 tcp_g_q_setup(tcp_stack_t *tcps) 24819 { 24820 mutex_enter(&tcps->tcps_g_q_lock); 24821 if (tcps->tcps_g_q != NULL) { 24822 mutex_exit(&tcps->tcps_g_q_lock); 24823 return; 24824 } 24825 if (tcps->tcps_g_q_creator == NULL) { 24826 /* This thread will set it up */ 24827 tcps->tcps_g_q_creator = curthread; 24828 mutex_exit(&tcps->tcps_g_q_lock); 24829 tcp_g_q_create(tcps); 24830 mutex_enter(&tcps->tcps_g_q_lock); 24831 ASSERT(tcps->tcps_g_q_creator == curthread); 24832 tcps->tcps_g_q_creator = NULL; 24833 cv_signal(&tcps->tcps_g_q_cv); 24834 ASSERT(tcps->tcps_g_q != NULL); 24835 mutex_exit(&tcps->tcps_g_q_lock); 24836 return; 24837 } 24838 /* Everybody but the creator has to wait */ 24839 if (tcps->tcps_g_q_creator != curthread) { 24840 while (tcps->tcps_g_q == NULL) 24841 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24842 } 24843 mutex_exit(&tcps->tcps_g_q_lock); 24844 } 24845 24846 major_t IP_MAJ; 24847 #define IP "ip" 24848 24849 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24850 24851 /* 24852 * Create a default tcp queue here instead of in strplumb 24853 */ 24854 void 24855 tcp_g_q_create(tcp_stack_t *tcps) 24856 { 24857 int error; 24858 ldi_handle_t lh = NULL; 24859 ldi_ident_t li = NULL; 24860 int rval; 24861 cred_t *cr; 24862 24863 #ifdef NS_DEBUG 24864 (void) printf("tcp_g_q_create()\n"); 24865 #endif 24866 24867 ASSERT(tcps->tcps_g_q_creator == curthread); 24868 24869 error = ldi_ident_from_major(IP_MAJ, &li); 24870 if (error) { 24871 #ifdef DEBUG 24872 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24873 error); 24874 #endif 24875 return; 24876 } 24877 24878 cr = zone_get_kcred(netstackid_to_zoneid( 24879 tcps->tcps_netstack->netstack_stackid)); 24880 ASSERT(cr != NULL); 24881 /* 24882 * We set the tcp default queue to IPv6 because IPv4 falls 24883 * back to IPv6 when it can't find a client, but 24884 * IPv6 does not fall back to IPv4. 24885 */ 24886 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24887 if (error) { 24888 #ifdef DEBUG 24889 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24890 error); 24891 #endif 24892 goto out; 24893 } 24894 24895 /* 24896 * This ioctl causes the tcp framework to cache a pointer to 24897 * this stream, so we don't want to close the stream after 24898 * this operation. 24899 * Use the kernel credentials that are for the zone we're in. 24900 */ 24901 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24902 (intptr_t)0, FKIOCTL, cr, &rval); 24903 if (error) { 24904 #ifdef DEBUG 24905 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24906 "error %d\n", error); 24907 #endif 24908 goto out; 24909 } 24910 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24911 lh = NULL; 24912 out: 24913 /* Close layered handles */ 24914 if (li) 24915 ldi_ident_release(li); 24916 /* Keep cred around until _inactive needs it */ 24917 tcps->tcps_g_q_cr = cr; 24918 } 24919 24920 /* 24921 * We keep tcp_g_q set until all other tcp_t's in the zone 24922 * has gone away, and then when tcp_g_q_inactive() is called 24923 * we clear it. 24924 */ 24925 void 24926 tcp_g_q_destroy(tcp_stack_t *tcps) 24927 { 24928 #ifdef NS_DEBUG 24929 (void) printf("tcp_g_q_destroy()for stack %d\n", 24930 tcps->tcps_netstack->netstack_stackid); 24931 #endif 24932 24933 if (tcps->tcps_g_q == NULL) { 24934 return; /* Nothing to cleanup */ 24935 } 24936 /* 24937 * Drop reference corresponding to the default queue. 24938 * This reference was added from tcp_open when the default queue 24939 * was created, hence we compensate for this extra drop in 24940 * tcp_g_q_close. If the refcnt drops to zero here it means 24941 * the default queue was the last one to be open, in which 24942 * case, then tcp_g_q_inactive will be 24943 * called as a result of the refrele. 24944 */ 24945 TCPS_REFRELE(tcps); 24946 } 24947 24948 /* 24949 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24950 * Run by tcp_q_q_inactive using a taskq. 24951 */ 24952 static void 24953 tcp_g_q_close(void *arg) 24954 { 24955 tcp_stack_t *tcps = arg; 24956 int error; 24957 ldi_handle_t lh = NULL; 24958 ldi_ident_t li = NULL; 24959 cred_t *cr; 24960 24961 #ifdef NS_DEBUG 24962 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24963 tcps->tcps_netstack->netstack_stackid, 24964 tcps->tcps_netstack->netstack_refcnt); 24965 #endif 24966 lh = tcps->tcps_g_q_lh; 24967 if (lh == NULL) 24968 return; /* Nothing to cleanup */ 24969 24970 ASSERT(tcps->tcps_refcnt == 1); 24971 ASSERT(tcps->tcps_g_q != NULL); 24972 24973 error = ldi_ident_from_major(IP_MAJ, &li); 24974 if (error) { 24975 #ifdef DEBUG 24976 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24977 error); 24978 #endif 24979 return; 24980 } 24981 24982 cr = tcps->tcps_g_q_cr; 24983 tcps->tcps_g_q_cr = NULL; 24984 ASSERT(cr != NULL); 24985 24986 /* 24987 * Make sure we can break the recursion when tcp_close decrements 24988 * the reference count causing g_q_inactive to be called again. 24989 */ 24990 tcps->tcps_g_q_lh = NULL; 24991 24992 /* close the default queue */ 24993 (void) ldi_close(lh, FREAD|FWRITE, cr); 24994 /* 24995 * At this point in time tcps and the rest of netstack_t might 24996 * have been deleted. 24997 */ 24998 tcps = NULL; 24999 25000 /* Close layered handles */ 25001 ldi_ident_release(li); 25002 crfree(cr); 25003 } 25004 25005 /* 25006 * Called when last tcp_t drops reference count using TCPS_REFRELE. 25007 * 25008 * Have to ensure that the ldi routines are not used by an 25009 * interrupt thread by using a taskq. 25010 */ 25011 void 25012 tcp_g_q_inactive(tcp_stack_t *tcps) 25013 { 25014 if (tcps->tcps_g_q_lh == NULL) 25015 return; /* Nothing to cleanup */ 25016 25017 ASSERT(tcps->tcps_refcnt == 0); 25018 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 25019 25020 if (servicing_interrupt()) { 25021 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 25022 (void *) tcps, TQ_SLEEP); 25023 } else { 25024 tcp_g_q_close(tcps); 25025 } 25026 } 25027 25028 /* 25029 * Called by IP when IP is loaded into the kernel 25030 */ 25031 void 25032 tcp_ddi_g_init(void) 25033 { 25034 IP_MAJ = ddi_name_to_major(IP); 25035 25036 tcp_timercache = kmem_cache_create("tcp_timercache", 25037 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 25038 NULL, NULL, NULL, NULL, NULL, 0); 25039 25040 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 25041 sizeof (tcp_sack_info_t), 0, 25042 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 25043 25044 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 25045 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 25046 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 25047 25048 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 25049 25050 /* Initialize the random number generator */ 25051 tcp_random_init(); 25052 25053 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 25054 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 25055 25056 /* A single callback independently of how many netstacks we have */ 25057 ip_squeue_init(tcp_squeue_add); 25058 25059 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 25060 25061 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 25062 TASKQ_PREPOPULATE); 25063 25064 /* 25065 * We want to be informed each time a stack is created or 25066 * destroyed in the kernel, so we can maintain the 25067 * set of tcp_stack_t's. 25068 */ 25069 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 25070 tcp_stack_fini); 25071 } 25072 25073 25074 /* 25075 * Initialize the TCP stack instance. 25076 */ 25077 static void * 25078 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 25079 { 25080 tcp_stack_t *tcps; 25081 tcpparam_t *pa; 25082 int i; 25083 25084 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 25085 tcps->tcps_netstack = ns; 25086 25087 /* Initialize locks */ 25088 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 25089 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 25090 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 25091 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 25092 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 25093 rw_init(&tcps->tcps_reserved_port_lock, NULL, RW_DEFAULT, NULL); 25094 25095 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 25096 tcps->tcps_g_epriv_ports[0] = 2049; 25097 tcps->tcps_g_epriv_ports[1] = 4045; 25098 tcps->tcps_min_anonpriv_port = 512; 25099 25100 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 25101 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 25102 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 25103 TCP_FANOUT_SIZE, KM_SLEEP); 25104 tcps->tcps_reserved_port = kmem_zalloc(sizeof (tcp_rport_t) * 25105 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE, KM_SLEEP); 25106 25107 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25108 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 25109 MUTEX_DEFAULT, NULL); 25110 } 25111 25112 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25113 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 25114 MUTEX_DEFAULT, NULL); 25115 } 25116 25117 /* TCP's IPsec code calls the packet dropper. */ 25118 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 25119 25120 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 25121 tcps->tcps_params = pa; 25122 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25123 25124 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 25125 A_CNT(lcl_tcp_param_arr), tcps); 25126 25127 /* 25128 * Note: To really walk the device tree you need the devinfo 25129 * pointer to your device which is only available after probe/attach. 25130 * The following is safe only because it uses ddi_root_node() 25131 */ 25132 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 25133 tcp_opt_obj.odb_opt_arr_cnt); 25134 25135 /* 25136 * Initialize RFC 1948 secret values. This will probably be reset once 25137 * by the boot scripts. 25138 * 25139 * Use NULL name, as the name is caught by the new lockstats. 25140 * 25141 * Initialize with some random, non-guessable string, like the global 25142 * T_INFO_ACK. 25143 */ 25144 25145 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 25146 sizeof (tcp_g_t_info_ack), tcps); 25147 25148 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 25149 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 25150 25151 return (tcps); 25152 } 25153 25154 /* 25155 * Called when the IP module is about to be unloaded. 25156 */ 25157 void 25158 tcp_ddi_g_destroy(void) 25159 { 25160 tcp_g_kstat_fini(tcp_g_kstat); 25161 tcp_g_kstat = NULL; 25162 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 25163 25164 mutex_destroy(&tcp_random_lock); 25165 25166 kmem_cache_destroy(tcp_timercache); 25167 kmem_cache_destroy(tcp_sack_info_cache); 25168 kmem_cache_destroy(tcp_iphc_cache); 25169 25170 netstack_unregister(NS_TCP); 25171 taskq_destroy(tcp_taskq); 25172 } 25173 25174 /* 25175 * Shut down the TCP stack instance. 25176 */ 25177 /* ARGSUSED */ 25178 static void 25179 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25180 { 25181 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25182 25183 tcp_g_q_destroy(tcps); 25184 } 25185 25186 /* 25187 * Free the TCP stack instance. 25188 */ 25189 static void 25190 tcp_stack_fini(netstackid_t stackid, void *arg) 25191 { 25192 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25193 int i; 25194 25195 nd_free(&tcps->tcps_g_nd); 25196 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25197 tcps->tcps_params = NULL; 25198 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25199 tcps->tcps_wroff_xtra_param = NULL; 25200 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25201 tcps->tcps_mdt_head_param = NULL; 25202 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25203 tcps->tcps_mdt_tail_param = NULL; 25204 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25205 tcps->tcps_mdt_max_pbufs_param = NULL; 25206 25207 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25208 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25209 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25210 } 25211 25212 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25213 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25214 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25215 } 25216 25217 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25218 tcps->tcps_bind_fanout = NULL; 25219 25220 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25221 tcps->tcps_acceptor_fanout = NULL; 25222 25223 kmem_free(tcps->tcps_reserved_port, sizeof (tcp_rport_t) * 25224 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE); 25225 tcps->tcps_reserved_port = NULL; 25226 25227 mutex_destroy(&tcps->tcps_iss_key_lock); 25228 rw_destroy(&tcps->tcps_hsp_lock); 25229 mutex_destroy(&tcps->tcps_g_q_lock); 25230 cv_destroy(&tcps->tcps_g_q_cv); 25231 mutex_destroy(&tcps->tcps_epriv_port_lock); 25232 rw_destroy(&tcps->tcps_reserved_port_lock); 25233 25234 ip_drop_unregister(&tcps->tcps_dropper); 25235 25236 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25237 tcps->tcps_kstat = NULL; 25238 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25239 25240 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25241 tcps->tcps_mibkp = NULL; 25242 25243 kmem_free(tcps, sizeof (*tcps)); 25244 } 25245 25246 /* 25247 * Generate ISS, taking into account NDD changes may happen halfway through. 25248 * (If the iss is not zero, set it.) 25249 */ 25250 25251 static void 25252 tcp_iss_init(tcp_t *tcp) 25253 { 25254 MD5_CTX context; 25255 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25256 uint32_t answer[4]; 25257 tcp_stack_t *tcps = tcp->tcp_tcps; 25258 25259 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25260 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25261 switch (tcps->tcps_strong_iss) { 25262 case 2: 25263 mutex_enter(&tcps->tcps_iss_key_lock); 25264 context = tcps->tcps_iss_key; 25265 mutex_exit(&tcps->tcps_iss_key_lock); 25266 arg.ports = tcp->tcp_ports; 25267 if (tcp->tcp_ipversion == IPV4_VERSION) { 25268 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25269 &arg.src); 25270 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25271 &arg.dst); 25272 } else { 25273 arg.src = tcp->tcp_ip6h->ip6_src; 25274 arg.dst = tcp->tcp_ip6h->ip6_dst; 25275 } 25276 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25277 MD5Final((uchar_t *)answer, &context); 25278 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25279 /* 25280 * Now that we've hashed into a unique per-connection sequence 25281 * space, add a random increment per strong_iss == 1. So I 25282 * guess we'll have to... 25283 */ 25284 /* FALLTHRU */ 25285 case 1: 25286 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25287 break; 25288 default: 25289 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25290 break; 25291 } 25292 tcp->tcp_valid_bits = TCP_ISS_VALID; 25293 tcp->tcp_fss = tcp->tcp_iss - 1; 25294 tcp->tcp_suna = tcp->tcp_iss; 25295 tcp->tcp_snxt = tcp->tcp_iss + 1; 25296 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25297 tcp->tcp_csuna = tcp->tcp_snxt; 25298 } 25299 25300 /* 25301 * Exported routine for extracting active tcp connection status. 25302 * 25303 * This is used by the Solaris Cluster Networking software to 25304 * gather a list of connections that need to be forwarded to 25305 * specific nodes in the cluster when configuration changes occur. 25306 * 25307 * The callback is invoked for each tcp_t structure. Returning 25308 * non-zero from the callback routine terminates the search. 25309 */ 25310 int 25311 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25312 void *arg) 25313 { 25314 netstack_handle_t nh; 25315 netstack_t *ns; 25316 int ret = 0; 25317 25318 netstack_next_init(&nh); 25319 while ((ns = netstack_next(&nh)) != NULL) { 25320 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25321 ns->netstack_tcp); 25322 netstack_rele(ns); 25323 } 25324 netstack_next_fini(&nh); 25325 return (ret); 25326 } 25327 25328 static int 25329 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25330 tcp_stack_t *tcps) 25331 { 25332 tcp_t *tcp; 25333 cl_tcp_info_t cl_tcpi; 25334 connf_t *connfp; 25335 conn_t *connp; 25336 int i; 25337 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25338 25339 ASSERT(callback != NULL); 25340 25341 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25342 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25343 connp = NULL; 25344 25345 while ((connp = 25346 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25347 25348 tcp = connp->conn_tcp; 25349 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25350 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25351 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25352 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25353 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25354 /* 25355 * The macros tcp_laddr and tcp_faddr give the IPv4 25356 * addresses. They are copied implicitly below as 25357 * mapped addresses. 25358 */ 25359 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25360 if (tcp->tcp_ipversion == IPV4_VERSION) { 25361 cl_tcpi.cl_tcpi_faddr = 25362 tcp->tcp_ipha->ipha_dst; 25363 } else { 25364 cl_tcpi.cl_tcpi_faddr_v6 = 25365 tcp->tcp_ip6h->ip6_dst; 25366 } 25367 25368 /* 25369 * If the callback returns non-zero 25370 * we terminate the traversal. 25371 */ 25372 if ((*callback)(&cl_tcpi, arg) != 0) { 25373 CONN_DEC_REF(tcp->tcp_connp); 25374 return (1); 25375 } 25376 } 25377 } 25378 25379 return (0); 25380 } 25381 25382 /* 25383 * Macros used for accessing the different types of sockaddr 25384 * structures inside a tcp_ioc_abort_conn_t. 25385 */ 25386 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25387 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25388 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25389 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25390 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25391 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25392 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25393 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25394 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25395 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25396 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25397 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25398 25399 /* 25400 * Return the correct error code to mimic the behavior 25401 * of a connection reset. 25402 */ 25403 #define TCP_AC_GET_ERRCODE(state, err) { \ 25404 switch ((state)) { \ 25405 case TCPS_SYN_SENT: \ 25406 case TCPS_SYN_RCVD: \ 25407 (err) = ECONNREFUSED; \ 25408 break; \ 25409 case TCPS_ESTABLISHED: \ 25410 case TCPS_FIN_WAIT_1: \ 25411 case TCPS_FIN_WAIT_2: \ 25412 case TCPS_CLOSE_WAIT: \ 25413 (err) = ECONNRESET; \ 25414 break; \ 25415 case TCPS_CLOSING: \ 25416 case TCPS_LAST_ACK: \ 25417 case TCPS_TIME_WAIT: \ 25418 (err) = 0; \ 25419 break; \ 25420 default: \ 25421 (err) = ENXIO; \ 25422 } \ 25423 } 25424 25425 /* 25426 * Check if a tcp structure matches the info in acp. 25427 */ 25428 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25429 (((acp)->ac_local.ss_family == AF_INET) ? \ 25430 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25431 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25432 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25433 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25434 (TCP_AC_V4LPORT((acp)) == 0 || \ 25435 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25436 (TCP_AC_V4RPORT((acp)) == 0 || \ 25437 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25438 (acp)->ac_start <= (tcp)->tcp_state && \ 25439 (acp)->ac_end >= (tcp)->tcp_state) : \ 25440 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25441 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25442 &(tcp)->tcp_ip_src_v6)) && \ 25443 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25444 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25445 &(tcp)->tcp_remote_v6)) && \ 25446 (TCP_AC_V6LPORT((acp)) == 0 || \ 25447 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25448 (TCP_AC_V6RPORT((acp)) == 0 || \ 25449 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25450 (acp)->ac_start <= (tcp)->tcp_state && \ 25451 (acp)->ac_end >= (tcp)->tcp_state)) 25452 25453 #define TCP_AC_MATCH(acp, tcp) \ 25454 (((acp)->ac_zoneid == ALL_ZONES || \ 25455 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25456 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25457 25458 /* 25459 * Build a message containing a tcp_ioc_abort_conn_t structure 25460 * which is filled in with information from acp and tp. 25461 */ 25462 static mblk_t * 25463 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25464 { 25465 mblk_t *mp; 25466 tcp_ioc_abort_conn_t *tacp; 25467 25468 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25469 if (mp == NULL) 25470 return (NULL); 25471 25472 mp->b_datap->db_type = M_CTL; 25473 25474 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25475 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25476 sizeof (uint32_t)); 25477 25478 tacp->ac_start = acp->ac_start; 25479 tacp->ac_end = acp->ac_end; 25480 tacp->ac_zoneid = acp->ac_zoneid; 25481 25482 if (acp->ac_local.ss_family == AF_INET) { 25483 tacp->ac_local.ss_family = AF_INET; 25484 tacp->ac_remote.ss_family = AF_INET; 25485 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25486 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25487 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25488 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25489 } else { 25490 tacp->ac_local.ss_family = AF_INET6; 25491 tacp->ac_remote.ss_family = AF_INET6; 25492 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25493 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25494 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25495 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25496 } 25497 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25498 return (mp); 25499 } 25500 25501 /* 25502 * Print a tcp_ioc_abort_conn_t structure. 25503 */ 25504 static void 25505 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25506 { 25507 char lbuf[128]; 25508 char rbuf[128]; 25509 sa_family_t af; 25510 in_port_t lport, rport; 25511 ushort_t logflags; 25512 25513 af = acp->ac_local.ss_family; 25514 25515 if (af == AF_INET) { 25516 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25517 lbuf, 128); 25518 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25519 rbuf, 128); 25520 lport = ntohs(TCP_AC_V4LPORT(acp)); 25521 rport = ntohs(TCP_AC_V4RPORT(acp)); 25522 } else { 25523 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25524 lbuf, 128); 25525 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25526 rbuf, 128); 25527 lport = ntohs(TCP_AC_V6LPORT(acp)); 25528 rport = ntohs(TCP_AC_V6RPORT(acp)); 25529 } 25530 25531 logflags = SL_TRACE | SL_NOTE; 25532 /* 25533 * Don't print this message to the console if the operation was done 25534 * to a non-global zone. 25535 */ 25536 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25537 logflags |= SL_CONSOLE; 25538 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25539 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25540 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25541 acp->ac_start, acp->ac_end); 25542 } 25543 25544 /* 25545 * Called inside tcp_rput when a message built using 25546 * tcp_ioctl_abort_build_msg is put into a queue. 25547 * Note that when we get here there is no wildcard in acp any more. 25548 */ 25549 static void 25550 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25551 { 25552 tcp_ioc_abort_conn_t *acp; 25553 25554 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25555 if (tcp->tcp_state <= acp->ac_end) { 25556 /* 25557 * If we get here, we are already on the correct 25558 * squeue. This ioctl follows the following path 25559 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25560 * ->tcp_ioctl_abort->squeue_fill (if on a 25561 * different squeue) 25562 */ 25563 int errcode; 25564 25565 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25566 (void) tcp_clean_death(tcp, errcode, 26); 25567 } 25568 freemsg(mp); 25569 } 25570 25571 /* 25572 * Abort all matching connections on a hash chain. 25573 */ 25574 static int 25575 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25576 boolean_t exact, tcp_stack_t *tcps) 25577 { 25578 int nmatch, err = 0; 25579 tcp_t *tcp; 25580 MBLKP mp, last, listhead = NULL; 25581 conn_t *tconnp; 25582 connf_t *connfp; 25583 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25584 25585 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25586 25587 startover: 25588 nmatch = 0; 25589 25590 mutex_enter(&connfp->connf_lock); 25591 for (tconnp = connfp->connf_head; tconnp != NULL; 25592 tconnp = tconnp->conn_next) { 25593 tcp = tconnp->conn_tcp; 25594 if (TCP_AC_MATCH(acp, tcp)) { 25595 CONN_INC_REF(tcp->tcp_connp); 25596 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25597 if (mp == NULL) { 25598 err = ENOMEM; 25599 CONN_DEC_REF(tcp->tcp_connp); 25600 break; 25601 } 25602 mp->b_prev = (mblk_t *)tcp; 25603 25604 if (listhead == NULL) { 25605 listhead = mp; 25606 last = mp; 25607 } else { 25608 last->b_next = mp; 25609 last = mp; 25610 } 25611 nmatch++; 25612 if (exact) 25613 break; 25614 } 25615 25616 /* Avoid holding lock for too long. */ 25617 if (nmatch >= 500) 25618 break; 25619 } 25620 mutex_exit(&connfp->connf_lock); 25621 25622 /* Pass mp into the correct tcp */ 25623 while ((mp = listhead) != NULL) { 25624 listhead = listhead->b_next; 25625 tcp = (tcp_t *)mp->b_prev; 25626 mp->b_next = mp->b_prev = NULL; 25627 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 25628 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 25629 } 25630 25631 *count += nmatch; 25632 if (nmatch >= 500 && err == 0) 25633 goto startover; 25634 return (err); 25635 } 25636 25637 /* 25638 * Abort all connections that matches the attributes specified in acp. 25639 */ 25640 static int 25641 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25642 { 25643 sa_family_t af; 25644 uint32_t ports; 25645 uint16_t *pports; 25646 int err = 0, count = 0; 25647 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25648 int index = -1; 25649 ushort_t logflags; 25650 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25651 25652 af = acp->ac_local.ss_family; 25653 25654 if (af == AF_INET) { 25655 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25656 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25657 pports = (uint16_t *)&ports; 25658 pports[1] = TCP_AC_V4LPORT(acp); 25659 pports[0] = TCP_AC_V4RPORT(acp); 25660 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25661 } 25662 } else { 25663 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25664 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25665 pports = (uint16_t *)&ports; 25666 pports[1] = TCP_AC_V6LPORT(acp); 25667 pports[0] = TCP_AC_V6RPORT(acp); 25668 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25669 } 25670 } 25671 25672 /* 25673 * For cases where remote addr, local port, and remote port are non- 25674 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25675 */ 25676 if (index != -1) { 25677 err = tcp_ioctl_abort_bucket(acp, index, 25678 &count, exact, tcps); 25679 } else { 25680 /* 25681 * loop through all entries for wildcard case 25682 */ 25683 for (index = 0; 25684 index < ipst->ips_ipcl_conn_fanout_size; 25685 index++) { 25686 err = tcp_ioctl_abort_bucket(acp, index, 25687 &count, exact, tcps); 25688 if (err != 0) 25689 break; 25690 } 25691 } 25692 25693 logflags = SL_TRACE | SL_NOTE; 25694 /* 25695 * Don't print this message to the console if the operation was done 25696 * to a non-global zone. 25697 */ 25698 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25699 logflags |= SL_CONSOLE; 25700 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25701 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25702 if (err == 0 && count == 0) 25703 err = ENOENT; 25704 return (err); 25705 } 25706 25707 /* 25708 * Process the TCP_IOC_ABORT_CONN ioctl request. 25709 */ 25710 static void 25711 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25712 { 25713 int err; 25714 IOCP iocp; 25715 MBLKP mp1; 25716 sa_family_t laf, raf; 25717 tcp_ioc_abort_conn_t *acp; 25718 zone_t *zptr; 25719 conn_t *connp = Q_TO_CONN(q); 25720 zoneid_t zoneid = connp->conn_zoneid; 25721 tcp_t *tcp = connp->conn_tcp; 25722 tcp_stack_t *tcps = tcp->tcp_tcps; 25723 25724 iocp = (IOCP)mp->b_rptr; 25725 25726 if ((mp1 = mp->b_cont) == NULL || 25727 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25728 err = EINVAL; 25729 goto out; 25730 } 25731 25732 /* check permissions */ 25733 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25734 err = EPERM; 25735 goto out; 25736 } 25737 25738 if (mp1->b_cont != NULL) { 25739 freemsg(mp1->b_cont); 25740 mp1->b_cont = NULL; 25741 } 25742 25743 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25744 laf = acp->ac_local.ss_family; 25745 raf = acp->ac_remote.ss_family; 25746 25747 /* check that a zone with the supplied zoneid exists */ 25748 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25749 zptr = zone_find_by_id(zoneid); 25750 if (zptr != NULL) { 25751 zone_rele(zptr); 25752 } else { 25753 err = EINVAL; 25754 goto out; 25755 } 25756 } 25757 25758 /* 25759 * For exclusive stacks we set the zoneid to zero 25760 * to make TCP operate as if in the global zone. 25761 */ 25762 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25763 acp->ac_zoneid = GLOBAL_ZONEID; 25764 25765 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25766 acp->ac_start > acp->ac_end || laf != raf || 25767 (laf != AF_INET && laf != AF_INET6)) { 25768 err = EINVAL; 25769 goto out; 25770 } 25771 25772 tcp_ioctl_abort_dump(acp); 25773 err = tcp_ioctl_abort(acp, tcps); 25774 25775 out: 25776 if (mp1 != NULL) { 25777 freemsg(mp1); 25778 mp->b_cont = NULL; 25779 } 25780 25781 if (err != 0) 25782 miocnak(q, mp, 0, err); 25783 else 25784 miocack(q, mp, 0, 0); 25785 } 25786 25787 /* 25788 * tcp_time_wait_processing() handles processing of incoming packets when 25789 * the tcp is in the TIME_WAIT state. 25790 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25791 * on the time wait list. 25792 */ 25793 void 25794 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25795 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25796 { 25797 int32_t bytes_acked; 25798 int32_t gap; 25799 int32_t rgap; 25800 tcp_opt_t tcpopt; 25801 uint_t flags; 25802 uint32_t new_swnd = 0; 25803 conn_t *connp; 25804 tcp_stack_t *tcps = tcp->tcp_tcps; 25805 25806 BUMP_LOCAL(tcp->tcp_ibsegs); 25807 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 25808 25809 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25810 new_swnd = BE16_TO_U16(tcph->th_win) << 25811 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25812 if (tcp->tcp_snd_ts_ok) { 25813 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25814 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25815 tcp->tcp_rnxt, TH_ACK); 25816 goto done; 25817 } 25818 } 25819 gap = seg_seq - tcp->tcp_rnxt; 25820 rgap = tcp->tcp_rwnd - (gap + seg_len); 25821 if (gap < 0) { 25822 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25823 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25824 (seg_len > -gap ? -gap : seg_len)); 25825 seg_len += gap; 25826 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25827 if (flags & TH_RST) { 25828 goto done; 25829 } 25830 if ((flags & TH_FIN) && seg_len == -1) { 25831 /* 25832 * When TCP receives a duplicate FIN in 25833 * TIME_WAIT state, restart the 2 MSL timer. 25834 * See page 73 in RFC 793. Make sure this TCP 25835 * is already on the TIME_WAIT list. If not, 25836 * just restart the timer. 25837 */ 25838 if (TCP_IS_DETACHED(tcp)) { 25839 if (tcp_time_wait_remove(tcp, NULL) == 25840 B_TRUE) { 25841 tcp_time_wait_append(tcp); 25842 TCP_DBGSTAT(tcps, 25843 tcp_rput_time_wait); 25844 } 25845 } else { 25846 ASSERT(tcp != NULL); 25847 TCP_TIMER_RESTART(tcp, 25848 tcps->tcps_time_wait_interval); 25849 } 25850 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25851 tcp->tcp_rnxt, TH_ACK); 25852 goto done; 25853 } 25854 flags |= TH_ACK_NEEDED; 25855 seg_len = 0; 25856 goto process_ack; 25857 } 25858 25859 /* Fix seg_seq, and chew the gap off the front. */ 25860 seg_seq = tcp->tcp_rnxt; 25861 } 25862 25863 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25864 /* 25865 * Make sure that when we accept the connection, pick 25866 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25867 * old connection. 25868 * 25869 * The next ISS generated is equal to tcp_iss_incr_extra 25870 * + ISS_INCR/2 + other components depending on the 25871 * value of tcp_strong_iss. We pre-calculate the new 25872 * ISS here and compare with tcp_snxt to determine if 25873 * we need to make adjustment to tcp_iss_incr_extra. 25874 * 25875 * The above calculation is ugly and is a 25876 * waste of CPU cycles... 25877 */ 25878 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25879 int32_t adj; 25880 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25881 25882 switch (tcps->tcps_strong_iss) { 25883 case 2: { 25884 /* Add time and MD5 components. */ 25885 uint32_t answer[4]; 25886 struct { 25887 uint32_t ports; 25888 in6_addr_t src; 25889 in6_addr_t dst; 25890 } arg; 25891 MD5_CTX context; 25892 25893 mutex_enter(&tcps->tcps_iss_key_lock); 25894 context = tcps->tcps_iss_key; 25895 mutex_exit(&tcps->tcps_iss_key_lock); 25896 arg.ports = tcp->tcp_ports; 25897 /* We use MAPPED addresses in tcp_iss_init */ 25898 arg.src = tcp->tcp_ip_src_v6; 25899 if (tcp->tcp_ipversion == IPV4_VERSION) { 25900 IN6_IPADDR_TO_V4MAPPED( 25901 tcp->tcp_ipha->ipha_dst, 25902 &arg.dst); 25903 } else { 25904 arg.dst = 25905 tcp->tcp_ip6h->ip6_dst; 25906 } 25907 MD5Update(&context, (uchar_t *)&arg, 25908 sizeof (arg)); 25909 MD5Final((uchar_t *)answer, &context); 25910 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25911 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25912 break; 25913 } 25914 case 1: 25915 /* Add time component and min random (i.e. 1). */ 25916 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25917 break; 25918 default: 25919 /* Add only time component. */ 25920 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25921 break; 25922 } 25923 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25924 /* 25925 * New ISS not guaranteed to be ISS_INCR/2 25926 * ahead of the current tcp_snxt, so add the 25927 * difference to tcp_iss_incr_extra. 25928 */ 25929 tcps->tcps_iss_incr_extra += adj; 25930 } 25931 /* 25932 * If tcp_clean_death() can not perform the task now, 25933 * drop the SYN packet and let the other side re-xmit. 25934 * Otherwise pass the SYN packet back in, since the 25935 * old tcp state has been cleaned up or freed. 25936 */ 25937 if (tcp_clean_death(tcp, 0, 27) == -1) 25938 goto done; 25939 /* 25940 * We will come back to tcp_rput_data 25941 * on the global queue. Packets destined 25942 * for the global queue will be checked 25943 * with global policy. But the policy for 25944 * this packet has already been checked as 25945 * this was destined for the detached 25946 * connection. We need to bypass policy 25947 * check this time by attaching a dummy 25948 * ipsec_in with ipsec_in_dont_check set. 25949 */ 25950 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25951 if (connp != NULL) { 25952 TCP_STAT(tcps, tcp_time_wait_syn_success); 25953 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25954 return; 25955 } 25956 goto done; 25957 } 25958 25959 /* 25960 * rgap is the amount of stuff received out of window. A negative 25961 * value is the amount out of window. 25962 */ 25963 if (rgap < 0) { 25964 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25965 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25966 /* Fix seg_len and make sure there is something left. */ 25967 seg_len += rgap; 25968 if (seg_len <= 0) { 25969 if (flags & TH_RST) { 25970 goto done; 25971 } 25972 flags |= TH_ACK_NEEDED; 25973 seg_len = 0; 25974 goto process_ack; 25975 } 25976 } 25977 /* 25978 * Check whether we can update tcp_ts_recent. This test is 25979 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25980 * Extensions for High Performance: An Update", Internet Draft. 25981 */ 25982 if (tcp->tcp_snd_ts_ok && 25983 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25984 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25985 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25986 tcp->tcp_last_rcv_lbolt = lbolt64; 25987 } 25988 25989 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25990 /* Always ack out of order packets */ 25991 flags |= TH_ACK_NEEDED; 25992 seg_len = 0; 25993 } else if (seg_len > 0) { 25994 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25995 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25996 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25997 } 25998 if (flags & TH_RST) { 25999 (void) tcp_clean_death(tcp, 0, 28); 26000 goto done; 26001 } 26002 if (flags & TH_SYN) { 26003 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 26004 TH_RST|TH_ACK); 26005 /* 26006 * Do not delete the TCP structure if it is in 26007 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 26008 */ 26009 goto done; 26010 } 26011 process_ack: 26012 if (flags & TH_ACK) { 26013 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 26014 if (bytes_acked <= 0) { 26015 if (bytes_acked == 0 && seg_len == 0 && 26016 new_swnd == tcp->tcp_swnd) 26017 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 26018 } else { 26019 /* Acks something not sent */ 26020 flags |= TH_ACK_NEEDED; 26021 } 26022 } 26023 if (flags & TH_ACK_NEEDED) { 26024 /* 26025 * Time to send an ack for some reason. 26026 */ 26027 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 26028 tcp->tcp_rnxt, TH_ACK); 26029 } 26030 done: 26031 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26032 DB_CKSUMSTART(mp) = 0; 26033 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 26034 TCP_STAT(tcps, tcp_time_wait_syn_fail); 26035 } 26036 freemsg(mp); 26037 } 26038 26039 /* 26040 * Allocate a T_SVR4_OPTMGMT_REQ. 26041 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 26042 * that tcp_rput_other can drop the acks. 26043 */ 26044 static mblk_t * 26045 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 26046 { 26047 mblk_t *mp; 26048 struct T_optmgmt_req *tor; 26049 struct opthdr *oh; 26050 uint_t size; 26051 char *optptr; 26052 26053 size = sizeof (*tor) + sizeof (*oh) + optlen; 26054 mp = allocb(size, BPRI_MED); 26055 if (mp == NULL) 26056 return (NULL); 26057 26058 mp->b_wptr += size; 26059 mp->b_datap->db_type = M_PROTO; 26060 tor = (struct T_optmgmt_req *)mp->b_rptr; 26061 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 26062 tor->MGMT_flags = T_NEGOTIATE; 26063 tor->OPT_length = sizeof (*oh) + optlen; 26064 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 26065 26066 oh = (struct opthdr *)&tor[1]; 26067 oh->level = level; 26068 oh->name = cmd; 26069 oh->len = optlen; 26070 if (optlen != 0) { 26071 optptr = (char *)&oh[1]; 26072 bcopy(opt, optptr, optlen); 26073 } 26074 return (mp); 26075 } 26076 26077 /* 26078 * TCP Timers Implementation. 26079 */ 26080 timeout_id_t 26081 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 26082 { 26083 mblk_t *mp; 26084 tcp_timer_t *tcpt; 26085 tcp_t *tcp = connp->conn_tcp; 26086 tcp_stack_t *tcps = tcp->tcp_tcps; 26087 26088 ASSERT(connp->conn_sqp != NULL); 26089 26090 TCP_DBGSTAT(tcps, tcp_timeout_calls); 26091 26092 if (tcp->tcp_timercache == NULL) { 26093 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 26094 } else { 26095 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 26096 mp = tcp->tcp_timercache; 26097 tcp->tcp_timercache = mp->b_next; 26098 mp->b_next = NULL; 26099 ASSERT(mp->b_wptr == NULL); 26100 } 26101 26102 CONN_INC_REF(connp); 26103 tcpt = (tcp_timer_t *)mp->b_rptr; 26104 tcpt->connp = connp; 26105 tcpt->tcpt_proc = f; 26106 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 26107 return ((timeout_id_t)mp); 26108 } 26109 26110 static void 26111 tcp_timer_callback(void *arg) 26112 { 26113 mblk_t *mp = (mblk_t *)arg; 26114 tcp_timer_t *tcpt; 26115 conn_t *connp; 26116 26117 tcpt = (tcp_timer_t *)mp->b_rptr; 26118 connp = tcpt->connp; 26119 squeue_fill(connp->conn_sqp, mp, 26120 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 26121 } 26122 26123 static void 26124 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 26125 { 26126 tcp_timer_t *tcpt; 26127 conn_t *connp = (conn_t *)arg; 26128 tcp_t *tcp = connp->conn_tcp; 26129 26130 tcpt = (tcp_timer_t *)mp->b_rptr; 26131 ASSERT(connp == tcpt->connp); 26132 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 26133 26134 /* 26135 * If the TCP has reached the closed state, don't proceed any 26136 * further. This TCP logically does not exist on the system. 26137 * tcpt_proc could for example access queues, that have already 26138 * been qprocoff'ed off. Also see comments at the start of tcp_input 26139 */ 26140 if (tcp->tcp_state != TCPS_CLOSED) { 26141 (*tcpt->tcpt_proc)(connp); 26142 } else { 26143 tcp->tcp_timer_tid = 0; 26144 } 26145 tcp_timer_free(connp->conn_tcp, mp); 26146 } 26147 26148 /* 26149 * There is potential race with untimeout and the handler firing at the same 26150 * time. The mblock may be freed by the handler while we are trying to use 26151 * it. But since both should execute on the same squeue, this race should not 26152 * occur. 26153 */ 26154 clock_t 26155 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 26156 { 26157 mblk_t *mp = (mblk_t *)id; 26158 tcp_timer_t *tcpt; 26159 clock_t delta; 26160 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26161 26162 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 26163 26164 if (mp == NULL) 26165 return (-1); 26166 26167 tcpt = (tcp_timer_t *)mp->b_rptr; 26168 ASSERT(tcpt->connp == connp); 26169 26170 delta = untimeout(tcpt->tcpt_tid); 26171 26172 if (delta >= 0) { 26173 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 26174 tcp_timer_free(connp->conn_tcp, mp); 26175 CONN_DEC_REF(connp); 26176 } 26177 26178 return (delta); 26179 } 26180 26181 /* 26182 * Allocate space for the timer event. The allocation looks like mblk, but it is 26183 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26184 * 26185 * Dealing with failures: If we can't allocate from the timer cache we try 26186 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26187 * points to b_rptr. 26188 * If we can't allocate anything using allocb_tryhard(), we perform a last 26189 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26190 * save the actual allocation size in b_datap. 26191 */ 26192 mblk_t * 26193 tcp_timermp_alloc(int kmflags) 26194 { 26195 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26196 kmflags & ~KM_PANIC); 26197 26198 if (mp != NULL) { 26199 mp->b_next = mp->b_prev = NULL; 26200 mp->b_rptr = (uchar_t *)(&mp[1]); 26201 mp->b_wptr = NULL; 26202 mp->b_datap = NULL; 26203 mp->b_queue = NULL; 26204 mp->b_cont = NULL; 26205 } else if (kmflags & KM_PANIC) { 26206 /* 26207 * Failed to allocate memory for the timer. Try allocating from 26208 * dblock caches. 26209 */ 26210 /* ipclassifier calls this from a constructor - hence no tcps */ 26211 TCP_G_STAT(tcp_timermp_allocfail); 26212 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26213 if (mp == NULL) { 26214 size_t size = 0; 26215 /* 26216 * Memory is really low. Try tryhard allocation. 26217 * 26218 * ipclassifier calls this from a constructor - 26219 * hence no tcps 26220 */ 26221 TCP_G_STAT(tcp_timermp_allocdblfail); 26222 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26223 sizeof (tcp_timer_t), &size, kmflags); 26224 mp->b_rptr = (uchar_t *)(&mp[1]); 26225 mp->b_next = mp->b_prev = NULL; 26226 mp->b_wptr = (uchar_t *)-1; 26227 mp->b_datap = (dblk_t *)size; 26228 mp->b_queue = NULL; 26229 mp->b_cont = NULL; 26230 } 26231 ASSERT(mp->b_wptr != NULL); 26232 } 26233 /* ipclassifier calls this from a constructor - hence no tcps */ 26234 TCP_G_DBGSTAT(tcp_timermp_alloced); 26235 26236 return (mp); 26237 } 26238 26239 /* 26240 * Free per-tcp timer cache. 26241 * It can only contain entries from tcp_timercache. 26242 */ 26243 void 26244 tcp_timermp_free(tcp_t *tcp) 26245 { 26246 mblk_t *mp; 26247 26248 while ((mp = tcp->tcp_timercache) != NULL) { 26249 ASSERT(mp->b_wptr == NULL); 26250 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26251 kmem_cache_free(tcp_timercache, mp); 26252 } 26253 } 26254 26255 /* 26256 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26257 * events there already (currently at most two events are cached). 26258 * If the event is not allocated from the timer cache, free it right away. 26259 */ 26260 static void 26261 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26262 { 26263 mblk_t *mp1 = tcp->tcp_timercache; 26264 tcp_stack_t *tcps = tcp->tcp_tcps; 26265 26266 if (mp->b_wptr != NULL) { 26267 /* 26268 * This allocation is not from a timer cache, free it right 26269 * away. 26270 */ 26271 if (mp->b_wptr != (uchar_t *)-1) 26272 freeb(mp); 26273 else 26274 kmem_free(mp, (size_t)mp->b_datap); 26275 } else if (mp1 == NULL || mp1->b_next == NULL) { 26276 /* Cache this timer block for future allocations */ 26277 mp->b_rptr = (uchar_t *)(&mp[1]); 26278 mp->b_next = mp1; 26279 tcp->tcp_timercache = mp; 26280 } else { 26281 kmem_cache_free(tcp_timercache, mp); 26282 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26283 } 26284 } 26285 26286 /* 26287 * End of TCP Timers implementation. 26288 */ 26289 26290 /* 26291 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26292 * on the specified backing STREAMS q. Note, the caller may make the 26293 * decision to call based on the tcp_t.tcp_flow_stopped value which 26294 * when check outside the q's lock is only an advisory check ... 26295 */ 26296 26297 void 26298 tcp_setqfull(tcp_t *tcp) 26299 { 26300 queue_t *q = tcp->tcp_wq; 26301 tcp_stack_t *tcps = tcp->tcp_tcps; 26302 26303 if (!(q->q_flag & QFULL)) { 26304 mutex_enter(QLOCK(q)); 26305 if (!(q->q_flag & QFULL)) { 26306 /* still need to set QFULL */ 26307 q->q_flag |= QFULL; 26308 tcp->tcp_flow_stopped = B_TRUE; 26309 mutex_exit(QLOCK(q)); 26310 TCP_STAT(tcps, tcp_flwctl_on); 26311 } else { 26312 mutex_exit(QLOCK(q)); 26313 } 26314 } 26315 } 26316 26317 void 26318 tcp_clrqfull(tcp_t *tcp) 26319 { 26320 queue_t *q = tcp->tcp_wq; 26321 26322 if (q->q_flag & QFULL) { 26323 mutex_enter(QLOCK(q)); 26324 if (q->q_flag & QFULL) { 26325 q->q_flag &= ~QFULL; 26326 tcp->tcp_flow_stopped = B_FALSE; 26327 mutex_exit(QLOCK(q)); 26328 if (q->q_flag & QWANTW) 26329 qbackenable(q, 0); 26330 } else { 26331 mutex_exit(QLOCK(q)); 26332 } 26333 } 26334 } 26335 26336 26337 /* 26338 * kstats related to squeues i.e. not per IP instance 26339 */ 26340 static void * 26341 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26342 { 26343 kstat_t *ksp; 26344 26345 tcp_g_stat_t template = { 26346 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26347 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26348 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26349 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26350 }; 26351 26352 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26353 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26354 KSTAT_FLAG_VIRTUAL); 26355 26356 if (ksp == NULL) 26357 return (NULL); 26358 26359 bcopy(&template, tcp_g_statp, sizeof (template)); 26360 ksp->ks_data = (void *)tcp_g_statp; 26361 26362 kstat_install(ksp); 26363 return (ksp); 26364 } 26365 26366 static void 26367 tcp_g_kstat_fini(kstat_t *ksp) 26368 { 26369 if (ksp != NULL) { 26370 kstat_delete(ksp); 26371 } 26372 } 26373 26374 26375 static void * 26376 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26377 { 26378 kstat_t *ksp; 26379 26380 tcp_stat_t template = { 26381 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26382 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26383 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26384 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26385 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26386 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26387 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26388 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26389 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26390 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26391 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26392 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26393 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26394 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26395 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26396 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26397 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26398 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26399 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26400 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26401 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26402 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26403 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26404 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26405 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26406 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26407 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26408 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26409 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26410 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26411 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26412 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26413 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26414 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26415 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26416 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26417 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26418 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26419 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26420 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26421 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26422 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26423 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26424 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26425 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26426 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26427 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26428 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26429 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26430 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26431 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26432 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26433 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26434 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26435 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26436 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26437 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26438 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26439 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26440 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26441 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26442 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26443 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26444 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26445 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26446 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26447 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26448 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26449 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26450 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26451 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26452 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26453 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26454 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26455 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26456 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26457 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26458 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26459 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26460 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26461 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26462 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26463 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26464 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26465 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26466 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26467 }; 26468 26469 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26470 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26471 KSTAT_FLAG_VIRTUAL, stackid); 26472 26473 if (ksp == NULL) 26474 return (NULL); 26475 26476 bcopy(&template, tcps_statisticsp, sizeof (template)); 26477 ksp->ks_data = (void *)tcps_statisticsp; 26478 ksp->ks_private = (void *)(uintptr_t)stackid; 26479 26480 kstat_install(ksp); 26481 return (ksp); 26482 } 26483 26484 static void 26485 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26486 { 26487 if (ksp != NULL) { 26488 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26489 kstat_delete_netstack(ksp, stackid); 26490 } 26491 } 26492 26493 /* 26494 * TCP Kstats implementation 26495 */ 26496 static void * 26497 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26498 { 26499 kstat_t *ksp; 26500 26501 tcp_named_kstat_t template = { 26502 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26503 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26504 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26505 { "maxConn", KSTAT_DATA_INT32, 0 }, 26506 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26507 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26508 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26509 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26510 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26511 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26512 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26513 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26514 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26515 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26516 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26517 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26518 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26519 { "outAck", KSTAT_DATA_UINT32, 0 }, 26520 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26521 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26522 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26523 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26524 { "outControl", KSTAT_DATA_UINT32, 0 }, 26525 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26526 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26527 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26528 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26529 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26530 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26531 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26532 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26533 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26534 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26535 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26536 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26537 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26538 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26539 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26540 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26541 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26542 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26543 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26544 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26545 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26546 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26547 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26548 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26549 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26550 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26551 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26552 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26553 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26554 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26555 }; 26556 26557 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26558 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26559 26560 if (ksp == NULL) 26561 return (NULL); 26562 26563 template.rtoAlgorithm.value.ui32 = 4; 26564 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26565 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26566 template.maxConn.value.i32 = -1; 26567 26568 bcopy(&template, ksp->ks_data, sizeof (template)); 26569 ksp->ks_update = tcp_kstat_update; 26570 ksp->ks_private = (void *)(uintptr_t)stackid; 26571 26572 kstat_install(ksp); 26573 return (ksp); 26574 } 26575 26576 static void 26577 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26578 { 26579 if (ksp != NULL) { 26580 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26581 kstat_delete_netstack(ksp, stackid); 26582 } 26583 } 26584 26585 static int 26586 tcp_kstat_update(kstat_t *kp, int rw) 26587 { 26588 tcp_named_kstat_t *tcpkp; 26589 tcp_t *tcp; 26590 connf_t *connfp; 26591 conn_t *connp; 26592 int i; 26593 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26594 netstack_t *ns; 26595 tcp_stack_t *tcps; 26596 ip_stack_t *ipst; 26597 26598 if ((kp == NULL) || (kp->ks_data == NULL)) 26599 return (EIO); 26600 26601 if (rw == KSTAT_WRITE) 26602 return (EACCES); 26603 26604 ns = netstack_find_by_stackid(stackid); 26605 if (ns == NULL) 26606 return (-1); 26607 tcps = ns->netstack_tcp; 26608 if (tcps == NULL) { 26609 netstack_rele(ns); 26610 return (-1); 26611 } 26612 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26613 26614 tcpkp->currEstab.value.ui32 = 0; 26615 26616 ipst = ns->netstack_ip; 26617 26618 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26619 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26620 connp = NULL; 26621 while ((connp = 26622 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26623 tcp = connp->conn_tcp; 26624 switch (tcp_snmp_state(tcp)) { 26625 case MIB2_TCP_established: 26626 case MIB2_TCP_closeWait: 26627 tcpkp->currEstab.value.ui32++; 26628 break; 26629 } 26630 } 26631 } 26632 26633 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26634 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26635 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26636 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26637 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26638 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26639 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26640 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26641 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26642 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26643 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26644 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26645 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26646 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26647 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26648 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26649 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26650 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26651 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26652 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26653 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26654 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26655 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26656 tcpkp->inDataInorderSegs.value.ui32 = 26657 tcps->tcps_mib.tcpInDataInorderSegs; 26658 tcpkp->inDataInorderBytes.value.ui32 = 26659 tcps->tcps_mib.tcpInDataInorderBytes; 26660 tcpkp->inDataUnorderSegs.value.ui32 = 26661 tcps->tcps_mib.tcpInDataUnorderSegs; 26662 tcpkp->inDataUnorderBytes.value.ui32 = 26663 tcps->tcps_mib.tcpInDataUnorderBytes; 26664 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26665 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26666 tcpkp->inDataPartDupSegs.value.ui32 = 26667 tcps->tcps_mib.tcpInDataPartDupSegs; 26668 tcpkp->inDataPartDupBytes.value.ui32 = 26669 tcps->tcps_mib.tcpInDataPartDupBytes; 26670 tcpkp->inDataPastWinSegs.value.ui32 = 26671 tcps->tcps_mib.tcpInDataPastWinSegs; 26672 tcpkp->inDataPastWinBytes.value.ui32 = 26673 tcps->tcps_mib.tcpInDataPastWinBytes; 26674 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26675 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26676 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26677 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26678 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26679 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26680 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26681 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26682 tcpkp->timKeepaliveProbe.value.ui32 = 26683 tcps->tcps_mib.tcpTimKeepaliveProbe; 26684 tcpkp->timKeepaliveDrop.value.ui32 = 26685 tcps->tcps_mib.tcpTimKeepaliveDrop; 26686 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26687 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26688 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26689 tcpkp->outSackRetransSegs.value.ui32 = 26690 tcps->tcps_mib.tcpOutSackRetransSegs; 26691 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26692 26693 netstack_rele(ns); 26694 return (0); 26695 } 26696 26697 void 26698 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26699 { 26700 uint16_t hdr_len; 26701 ipha_t *ipha; 26702 uint8_t *nexthdrp; 26703 tcph_t *tcph; 26704 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26705 26706 /* Already has an eager */ 26707 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26708 TCP_STAT(tcps, tcp_reinput_syn); 26709 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 26710 connp, SQTAG_TCP_REINPUT_EAGER); 26711 return; 26712 } 26713 26714 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26715 case IPV4_VERSION: 26716 ipha = (ipha_t *)mp->b_rptr; 26717 hdr_len = IPH_HDR_LENGTH(ipha); 26718 break; 26719 case IPV6_VERSION: 26720 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26721 &hdr_len, &nexthdrp)) { 26722 CONN_DEC_REF(connp); 26723 freemsg(mp); 26724 return; 26725 } 26726 break; 26727 } 26728 26729 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26730 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26731 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26732 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26733 } 26734 26735 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 26736 SQTAG_TCP_REINPUT); 26737 } 26738 26739 static squeue_func_t 26740 tcp_squeue_switch(int val) 26741 { 26742 squeue_func_t rval = squeue_fill; 26743 26744 switch (val) { 26745 case 1: 26746 rval = squeue_enter_nodrain; 26747 break; 26748 case 2: 26749 rval = squeue_enter; 26750 break; 26751 default: 26752 break; 26753 } 26754 return (rval); 26755 } 26756 26757 /* 26758 * This is called once for each squeue - globally for all stack 26759 * instances. 26760 */ 26761 static void 26762 tcp_squeue_add(squeue_t *sqp) 26763 { 26764 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26765 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26766 26767 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26768 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 26769 sqp, TCP_TIME_WAIT_DELAY); 26770 if (tcp_free_list_max_cnt == 0) { 26771 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26772 max_ncpus : boot_max_ncpus); 26773 26774 /* 26775 * Limit number of entries to 1% of availble memory / tcp_ncpus 26776 */ 26777 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26778 (tcp_ncpus * sizeof (tcp_t) * 100); 26779 } 26780 tcp_time_wait->tcp_free_list_cnt = 0; 26781 } 26782