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 /* Must not cache forwarding table routes. */ 2953 if (ire_cacheable) { 2954 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2955 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2956 connp->conn_ire_cache = ire; 2957 IRE_UNTRACE_REF(ire); 2958 rw_exit(&ire->ire_bucket->irb_lock); 2959 mutex_exit(&connp->conn_lock); 2960 return (1); 2961 } 2962 rw_exit(&ire->ire_bucket->irb_lock); 2963 } 2964 mutex_exit(&connp->conn_lock); 2965 2966 if (ire->ire_mp == NULL) 2967 ire_refrele(ire); 2968 return (1); 2969 2970 error: 2971 if (ire->ire_mp == NULL) 2972 ire_refrele(ire); 2973 if (sire != NULL) 2974 ire_refrele(sire); 2975 return (0); 2976 } 2977 2978 /* 2979 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 2980 * O_T_BIND_REQ/T_BIND_REQ message. 2981 */ 2982 static void 2983 tcp_bind(tcp_t *tcp, mblk_t *mp) 2984 { 2985 sin_t *sin; 2986 sin6_t *sin6; 2987 mblk_t *mp1; 2988 in_port_t requested_port; 2989 in_port_t allocated_port; 2990 struct T_bind_req *tbr; 2991 boolean_t bind_to_req_port_only; 2992 boolean_t backlog_update = B_FALSE; 2993 boolean_t user_specified; 2994 in6_addr_t v6addr; 2995 ipaddr_t v4addr; 2996 uint_t origipversion; 2997 int err; 2998 queue_t *q = tcp->tcp_wq; 2999 conn_t *connp; 3000 mlp_type_t addrtype, mlptype; 3001 zone_t *zone; 3002 cred_t *cr; 3003 in_port_t mlp_port; 3004 tcp_stack_t *tcps = tcp->tcp_tcps; 3005 3006 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3007 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3008 if (tcp->tcp_debug) { 3009 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3010 "tcp_bind: bad req, len %u", 3011 (uint_t)(mp->b_wptr - mp->b_rptr)); 3012 } 3013 tcp_err_ack(tcp, mp, TPROTO, 0); 3014 return; 3015 } 3016 /* Make sure the largest address fits */ 3017 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3018 if (mp1 == NULL) { 3019 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3020 return; 3021 } 3022 mp = mp1; 3023 tbr = (struct T_bind_req *)mp->b_rptr; 3024 if (tcp->tcp_state >= TCPS_BOUND) { 3025 if ((tcp->tcp_state == TCPS_BOUND || 3026 tcp->tcp_state == TCPS_LISTEN) && 3027 tcp->tcp_conn_req_max != tbr->CONIND_number && 3028 tbr->CONIND_number > 0) { 3029 /* 3030 * Handle listen() increasing CONIND_number. 3031 * This is more "liberal" then what the TPI spec 3032 * requires but is needed to avoid a t_unbind 3033 * when handling listen() since the port number 3034 * might be "stolen" between the unbind and bind. 3035 */ 3036 backlog_update = B_TRUE; 3037 goto do_bind; 3038 } 3039 if (tcp->tcp_debug) { 3040 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3041 "tcp_bind: bad state, %d", tcp->tcp_state); 3042 } 3043 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3044 return; 3045 } 3046 origipversion = tcp->tcp_ipversion; 3047 3048 switch (tbr->ADDR_length) { 3049 case 0: /* request for a generic port */ 3050 tbr->ADDR_offset = sizeof (struct T_bind_req); 3051 if (tcp->tcp_family == AF_INET) { 3052 tbr->ADDR_length = sizeof (sin_t); 3053 sin = (sin_t *)&tbr[1]; 3054 *sin = sin_null; 3055 sin->sin_family = AF_INET; 3056 mp->b_wptr = (uchar_t *)&sin[1]; 3057 tcp->tcp_ipversion = IPV4_VERSION; 3058 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3059 } else { 3060 ASSERT(tcp->tcp_family == AF_INET6); 3061 tbr->ADDR_length = sizeof (sin6_t); 3062 sin6 = (sin6_t *)&tbr[1]; 3063 *sin6 = sin6_null; 3064 sin6->sin6_family = AF_INET6; 3065 mp->b_wptr = (uchar_t *)&sin6[1]; 3066 tcp->tcp_ipversion = IPV6_VERSION; 3067 V6_SET_ZERO(v6addr); 3068 } 3069 requested_port = 0; 3070 break; 3071 3072 case sizeof (sin_t): /* Complete IPv4 address */ 3073 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3074 sizeof (sin_t)); 3075 if (sin == NULL || !OK_32PTR((char *)sin)) { 3076 if (tcp->tcp_debug) { 3077 (void) strlog(TCP_MOD_ID, 0, 1, 3078 SL_ERROR|SL_TRACE, 3079 "tcp_bind: bad address parameter, " 3080 "offset %d, len %d", 3081 tbr->ADDR_offset, tbr->ADDR_length); 3082 } 3083 tcp_err_ack(tcp, mp, TPROTO, 0); 3084 return; 3085 } 3086 /* 3087 * With sockets sockfs will accept bogus sin_family in 3088 * bind() and replace it with the family used in the socket 3089 * call. 3090 */ 3091 if (sin->sin_family != AF_INET || 3092 tcp->tcp_family != AF_INET) { 3093 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3094 return; 3095 } 3096 requested_port = ntohs(sin->sin_port); 3097 tcp->tcp_ipversion = IPV4_VERSION; 3098 v4addr = sin->sin_addr.s_addr; 3099 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3100 break; 3101 3102 case sizeof (sin6_t): /* Complete IPv6 address */ 3103 sin6 = (sin6_t *)mi_offset_param(mp, 3104 tbr->ADDR_offset, sizeof (sin6_t)); 3105 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3106 if (tcp->tcp_debug) { 3107 (void) strlog(TCP_MOD_ID, 0, 1, 3108 SL_ERROR|SL_TRACE, 3109 "tcp_bind: bad IPv6 address parameter, " 3110 "offset %d, len %d", tbr->ADDR_offset, 3111 tbr->ADDR_length); 3112 } 3113 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3114 return; 3115 } 3116 if (sin6->sin6_family != AF_INET6 || 3117 tcp->tcp_family != AF_INET6) { 3118 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3119 return; 3120 } 3121 requested_port = ntohs(sin6->sin6_port); 3122 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3123 IPV4_VERSION : IPV6_VERSION; 3124 v6addr = sin6->sin6_addr; 3125 break; 3126 3127 default: 3128 if (tcp->tcp_debug) { 3129 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3130 "tcp_bind: bad address length, %d", 3131 tbr->ADDR_length); 3132 } 3133 tcp_err_ack(tcp, mp, TBADADDR, 0); 3134 return; 3135 } 3136 tcp->tcp_bound_source_v6 = v6addr; 3137 3138 /* Check for change in ipversion */ 3139 if (origipversion != tcp->tcp_ipversion) { 3140 ASSERT(tcp->tcp_family == AF_INET6); 3141 err = tcp->tcp_ipversion == IPV6_VERSION ? 3142 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3143 if (err) { 3144 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3145 return; 3146 } 3147 } 3148 3149 /* 3150 * Initialize family specific fields. Copy of the src addr. 3151 * in tcp_t is needed for the lookup funcs. 3152 */ 3153 if (tcp->tcp_ipversion == IPV6_VERSION) { 3154 tcp->tcp_ip6h->ip6_src = v6addr; 3155 } else { 3156 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3157 } 3158 tcp->tcp_ip_src_v6 = v6addr; 3159 3160 /* 3161 * For O_T_BIND_REQ: 3162 * Verify that the target port/addr is available, or choose 3163 * another. 3164 * For T_BIND_REQ: 3165 * Verify that the target port/addr is available or fail. 3166 * In both cases when it succeeds the tcp is inserted in the 3167 * bind hash table. This ensures that the operation is atomic 3168 * under the lock on the hash bucket. 3169 */ 3170 bind_to_req_port_only = requested_port != 0 && 3171 tbr->PRIM_type != O_T_BIND_REQ; 3172 /* 3173 * Get a valid port (within the anonymous range and should not 3174 * be a privileged one) to use if the user has not given a port. 3175 * If multiple threads are here, they may all start with 3176 * with the same initial port. But, it should be fine as long as 3177 * tcp_bindi will ensure that no two threads will be assigned 3178 * the same port. 3179 * 3180 * NOTE: XXX If a privileged process asks for an anonymous port, we 3181 * still check for ports only in the range > tcp_smallest_non_priv_port, 3182 * unless TCP_ANONPRIVBIND option is set. 3183 */ 3184 mlptype = mlptSingle; 3185 mlp_port = requested_port; 3186 if (requested_port == 0) { 3187 requested_port = tcp->tcp_anon_priv_bind ? 3188 tcp_get_next_priv_port(tcp) : 3189 tcp_update_next_port(tcps->tcps_next_port_to_try, 3190 tcp, B_TRUE); 3191 if (requested_port == 0) { 3192 tcp_err_ack(tcp, mp, TNOADDR, 0); 3193 return; 3194 } 3195 user_specified = B_FALSE; 3196 3197 /* 3198 * If the user went through one of the RPC interfaces to create 3199 * this socket and RPC is MLP in this zone, then give him an 3200 * anonymous MLP. 3201 */ 3202 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3203 connp = tcp->tcp_connp; 3204 if (connp->conn_anon_mlp && is_system_labeled()) { 3205 zone = crgetzone(cr); 3206 addrtype = tsol_mlp_addr_type(zone->zone_id, 3207 IPV6_VERSION, &v6addr, 3208 tcps->tcps_netstack->netstack_ip); 3209 if (addrtype == mlptSingle) { 3210 tcp_err_ack(tcp, mp, TNOADDR, 0); 3211 return; 3212 } 3213 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3214 PMAPPORT, addrtype); 3215 mlp_port = PMAPPORT; 3216 } 3217 } else { 3218 int i; 3219 boolean_t priv = B_FALSE; 3220 3221 /* 3222 * If the requested_port is in the well-known privileged range, 3223 * verify that the stream was opened by a privileged user. 3224 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3225 * but instead the code relies on: 3226 * - the fact that the address of the array and its size never 3227 * changes 3228 * - the atomic assignment of the elements of the array 3229 */ 3230 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3231 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 3232 priv = B_TRUE; 3233 } else { 3234 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 3235 if (requested_port == 3236 tcps->tcps_g_epriv_ports[i]) { 3237 priv = B_TRUE; 3238 break; 3239 } 3240 } 3241 } 3242 if (priv) { 3243 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3244 if (tcp->tcp_debug) { 3245 (void) strlog(TCP_MOD_ID, 0, 1, 3246 SL_ERROR|SL_TRACE, 3247 "tcp_bind: no priv for port %d", 3248 requested_port); 3249 } 3250 tcp_err_ack(tcp, mp, TACCES, 0); 3251 return; 3252 } 3253 } 3254 user_specified = B_TRUE; 3255 3256 connp = tcp->tcp_connp; 3257 if (is_system_labeled()) { 3258 zone = crgetzone(cr); 3259 addrtype = tsol_mlp_addr_type(zone->zone_id, 3260 IPV6_VERSION, &v6addr, 3261 tcps->tcps_netstack->netstack_ip); 3262 if (addrtype == mlptSingle) { 3263 tcp_err_ack(tcp, mp, TNOADDR, 0); 3264 return; 3265 } 3266 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3267 requested_port, addrtype); 3268 } 3269 } 3270 3271 if (mlptype != mlptSingle) { 3272 if (secpolicy_net_bindmlp(cr) != 0) { 3273 if (tcp->tcp_debug) { 3274 (void) strlog(TCP_MOD_ID, 0, 1, 3275 SL_ERROR|SL_TRACE, 3276 "tcp_bind: no priv for multilevel port %d", 3277 requested_port); 3278 } 3279 tcp_err_ack(tcp, mp, TACCES, 0); 3280 return; 3281 } 3282 3283 /* 3284 * If we're specifically binding a shared IP address and the 3285 * port is MLP on shared addresses, then check to see if this 3286 * zone actually owns the MLP. Reject if not. 3287 */ 3288 if (mlptype == mlptShared && addrtype == mlptShared) { 3289 /* 3290 * No need to handle exclusive-stack zones since 3291 * ALL_ZONES only applies to the shared stack. 3292 */ 3293 zoneid_t mlpzone; 3294 3295 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3296 htons(mlp_port)); 3297 if (connp->conn_zoneid != mlpzone) { 3298 if (tcp->tcp_debug) { 3299 (void) strlog(TCP_MOD_ID, 0, 1, 3300 SL_ERROR|SL_TRACE, 3301 "tcp_bind: attempt to bind port " 3302 "%d on shared addr in zone %d " 3303 "(should be %d)", 3304 mlp_port, connp->conn_zoneid, 3305 mlpzone); 3306 } 3307 tcp_err_ack(tcp, mp, TACCES, 0); 3308 return; 3309 } 3310 } 3311 3312 if (!user_specified) { 3313 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3314 requested_port, B_TRUE); 3315 if (err != 0) { 3316 if (tcp->tcp_debug) { 3317 (void) strlog(TCP_MOD_ID, 0, 1, 3318 SL_ERROR|SL_TRACE, 3319 "tcp_bind: cannot establish anon " 3320 "MLP for port %d", 3321 requested_port); 3322 } 3323 tcp_err_ack(tcp, mp, TSYSERR, err); 3324 return; 3325 } 3326 connp->conn_anon_port = B_TRUE; 3327 } 3328 connp->conn_mlp_type = mlptype; 3329 } 3330 3331 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3332 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3333 3334 if (allocated_port == 0) { 3335 connp->conn_mlp_type = mlptSingle; 3336 if (connp->conn_anon_port) { 3337 connp->conn_anon_port = B_FALSE; 3338 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3339 requested_port, B_FALSE); 3340 } 3341 if (bind_to_req_port_only) { 3342 if (tcp->tcp_debug) { 3343 (void) strlog(TCP_MOD_ID, 0, 1, 3344 SL_ERROR|SL_TRACE, 3345 "tcp_bind: requested addr busy"); 3346 } 3347 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3348 } else { 3349 /* If we are out of ports, fail the bind. */ 3350 if (tcp->tcp_debug) { 3351 (void) strlog(TCP_MOD_ID, 0, 1, 3352 SL_ERROR|SL_TRACE, 3353 "tcp_bind: out of ports?"); 3354 } 3355 tcp_err_ack(tcp, mp, TNOADDR, 0); 3356 } 3357 return; 3358 } 3359 ASSERT(tcp->tcp_state == TCPS_BOUND); 3360 do_bind: 3361 if (!backlog_update) { 3362 if (tcp->tcp_family == AF_INET) 3363 sin->sin_port = htons(allocated_port); 3364 else 3365 sin6->sin6_port = htons(allocated_port); 3366 } 3367 if (tcp->tcp_family == AF_INET) { 3368 if (tbr->CONIND_number != 0) { 3369 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3370 sizeof (sin_t)); 3371 } else { 3372 /* Just verify the local IP address */ 3373 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3374 } 3375 } else { 3376 if (tbr->CONIND_number != 0) { 3377 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3378 sizeof (sin6_t)); 3379 } else { 3380 /* Just verify the local IP address */ 3381 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3382 IPV6_ADDR_LEN); 3383 } 3384 } 3385 if (mp1 == NULL) { 3386 if (connp->conn_anon_port) { 3387 connp->conn_anon_port = B_FALSE; 3388 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3389 requested_port, B_FALSE); 3390 } 3391 connp->conn_mlp_type = mlptSingle; 3392 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3393 return; 3394 } 3395 3396 tbr->PRIM_type = T_BIND_ACK; 3397 mp->b_datap->db_type = M_PCPROTO; 3398 3399 /* Chain in the reply mp for tcp_rput() */ 3400 mp1->b_cont = mp; 3401 mp = mp1; 3402 3403 tcp->tcp_conn_req_max = tbr->CONIND_number; 3404 if (tcp->tcp_conn_req_max) { 3405 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 3406 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 3407 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 3408 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 3409 /* 3410 * If this is a listener, do not reset the eager list 3411 * and other stuffs. Note that we don't check if the 3412 * existing eager list meets the new tcp_conn_req_max 3413 * requirement. 3414 */ 3415 if (tcp->tcp_state != TCPS_LISTEN) { 3416 tcp->tcp_state = TCPS_LISTEN; 3417 /* Initialize the chain. Don't need the eager_lock */ 3418 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3419 tcp->tcp_eager_next_drop_q0 = tcp; 3420 tcp->tcp_eager_prev_drop_q0 = tcp; 3421 tcp->tcp_second_ctimer_threshold = 3422 tcps->tcps_ip_abort_linterval; 3423 } 3424 } 3425 3426 /* 3427 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3428 * processing continues in tcp_rput_other(). 3429 */ 3430 if (tcp->tcp_family == AF_INET6) { 3431 ASSERT(tcp->tcp_connp->conn_af_isv6); 3432 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3433 } else { 3434 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3435 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3436 } 3437 /* 3438 * If the bind cannot complete immediately 3439 * IP will arrange to call tcp_rput_other 3440 * when the bind completes. 3441 */ 3442 if (mp != NULL) { 3443 tcp_rput_other(tcp, mp); 3444 } else { 3445 /* 3446 * Bind will be resumed later. Need to ensure 3447 * that conn doesn't disappear when that happens. 3448 * This will be decremented in ip_resume_tcp_bind(). 3449 */ 3450 CONN_INC_REF(tcp->tcp_connp); 3451 } 3452 } 3453 3454 3455 /* 3456 * If the "bind_to_req_port_only" parameter is set, if the requested port 3457 * number is available, return it, If not return 0 3458 * 3459 * If "bind_to_req_port_only" parameter is not set and 3460 * If the requested port number is available, return it. If not, return 3461 * the first anonymous port we happen across. If no anonymous ports are 3462 * available, return 0. addr is the requested local address, if any. 3463 * 3464 * In either case, when succeeding update the tcp_t to record the port number 3465 * and insert it in the bind hash table. 3466 * 3467 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3468 * without setting SO_REUSEADDR. This is needed so that they 3469 * can be viewed as two independent transport protocols. 3470 */ 3471 static in_port_t 3472 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3473 int reuseaddr, boolean_t quick_connect, 3474 boolean_t bind_to_req_port_only, boolean_t user_specified) 3475 { 3476 /* number of times we have run around the loop */ 3477 int count = 0; 3478 /* maximum number of times to run around the loop */ 3479 int loopmax; 3480 conn_t *connp = tcp->tcp_connp; 3481 zoneid_t zoneid = connp->conn_zoneid; 3482 tcp_stack_t *tcps = tcp->tcp_tcps; 3483 3484 /* 3485 * Lookup for free addresses is done in a loop and "loopmax" 3486 * influences how long we spin in the loop 3487 */ 3488 if (bind_to_req_port_only) { 3489 /* 3490 * If the requested port is busy, don't bother to look 3491 * for a new one. Setting loop maximum count to 1 has 3492 * that effect. 3493 */ 3494 loopmax = 1; 3495 } else { 3496 /* 3497 * If the requested port is busy, look for a free one 3498 * in the anonymous port range. 3499 * Set loopmax appropriately so that one does not look 3500 * forever in the case all of the anonymous ports are in use. 3501 */ 3502 if (tcp->tcp_anon_priv_bind) { 3503 /* 3504 * loopmax = 3505 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3506 */ 3507 loopmax = IPPORT_RESERVED - 3508 tcps->tcps_min_anonpriv_port; 3509 } else { 3510 loopmax = (tcps->tcps_largest_anon_port - 3511 tcps->tcps_smallest_anon_port + 1); 3512 } 3513 } 3514 do { 3515 uint16_t lport; 3516 tf_t *tbf; 3517 tcp_t *ltcp; 3518 conn_t *lconnp; 3519 3520 lport = htons(port); 3521 3522 /* 3523 * Ensure that the tcp_t is not currently in the bind hash. 3524 * Hold the lock on the hash bucket to ensure that 3525 * the duplicate check plus the insertion is an atomic 3526 * operation. 3527 * 3528 * This function does an inline lookup on the bind hash list 3529 * Make sure that we access only members of tcp_t 3530 * and that we don't look at tcp_tcp, since we are not 3531 * doing a CONN_INC_REF. 3532 */ 3533 tcp_bind_hash_remove(tcp); 3534 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3535 mutex_enter(&tbf->tf_lock); 3536 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3537 ltcp = ltcp->tcp_bind_hash) { 3538 boolean_t not_socket; 3539 boolean_t exclbind; 3540 3541 if (lport != ltcp->tcp_lport) 3542 continue; 3543 3544 lconnp = ltcp->tcp_connp; 3545 3546 /* 3547 * On a labeled system, we must treat bindings to ports 3548 * on shared IP addresses by sockets with MAC exemption 3549 * privilege as being in all zones, as there's 3550 * otherwise no way to identify the right receiver. 3551 */ 3552 if (!IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) && 3553 !lconnp->conn_mac_exempt && 3554 !connp->conn_mac_exempt) 3555 continue; 3556 3557 /* 3558 * If TCP_EXCLBIND is set for either the bound or 3559 * binding endpoint, the semantics of bind 3560 * is changed according to the following. 3561 * 3562 * spec = specified address (v4 or v6) 3563 * unspec = unspecified address (v4 or v6) 3564 * A = specified addresses are different for endpoints 3565 * 3566 * bound bind to allowed 3567 * ------------------------------------- 3568 * unspec unspec no 3569 * unspec spec no 3570 * spec unspec no 3571 * spec spec yes if A 3572 * 3573 * For labeled systems, SO_MAC_EXEMPT behaves the same 3574 * as TCP_EXCLBIND, except that zoneid is ignored. 3575 * 3576 * Note: 3577 * 3578 * 1. Because of TLI semantics, an endpoint can go 3579 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3580 * TCPS_BOUND, depending on whether it is originally 3581 * a listener or not. That is why we need to check 3582 * for states greater than or equal to TCPS_BOUND 3583 * here. 3584 * 3585 * 2. Ideally, we should only check for state equals 3586 * to TCPS_LISTEN. And the following check should be 3587 * added. 3588 * 3589 * if (ltcp->tcp_state == TCPS_LISTEN || 3590 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3591 * ... 3592 * } 3593 * 3594 * The semantics will be changed to this. If the 3595 * endpoint on the list is in state not equal to 3596 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3597 * set, let the bind succeed. 3598 * 3599 * Because of (1), we cannot do that for TLI 3600 * endpoints. But we can do that for socket endpoints. 3601 * If in future, we can change this going back 3602 * semantics, we can use the above check for TLI also. 3603 */ 3604 not_socket = !(TCP_IS_SOCKET(ltcp) && 3605 TCP_IS_SOCKET(tcp)); 3606 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3607 3608 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3609 (exclbind && (not_socket || 3610 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3611 if (V6_OR_V4_INADDR_ANY( 3612 ltcp->tcp_bound_source_v6) || 3613 V6_OR_V4_INADDR_ANY(*laddr) || 3614 IN6_ARE_ADDR_EQUAL(laddr, 3615 <cp->tcp_bound_source_v6)) { 3616 break; 3617 } 3618 continue; 3619 } 3620 3621 /* 3622 * Check ipversion to allow IPv4 and IPv6 sockets to 3623 * have disjoint port number spaces, if *_EXCLBIND 3624 * is not set and only if the application binds to a 3625 * specific port. We use the same autoassigned port 3626 * number space for IPv4 and IPv6 sockets. 3627 */ 3628 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3629 bind_to_req_port_only) 3630 continue; 3631 3632 /* 3633 * Ideally, we should make sure that the source 3634 * address, remote address, and remote port in the 3635 * four tuple for this tcp-connection is unique. 3636 * However, trying to find out the local source 3637 * address would require too much code duplication 3638 * with IP, since IP needs needs to have that code 3639 * to support userland TCP implementations. 3640 */ 3641 if (quick_connect && 3642 (ltcp->tcp_state > TCPS_LISTEN) && 3643 ((tcp->tcp_fport != ltcp->tcp_fport) || 3644 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3645 <cp->tcp_remote_v6))) 3646 continue; 3647 3648 if (!reuseaddr) { 3649 /* 3650 * No socket option SO_REUSEADDR. 3651 * If existing port is bound to 3652 * a non-wildcard IP address 3653 * and the requesting stream is 3654 * bound to a distinct 3655 * different IP addresses 3656 * (non-wildcard, also), keep 3657 * going. 3658 */ 3659 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3660 !V6_OR_V4_INADDR_ANY( 3661 ltcp->tcp_bound_source_v6) && 3662 !IN6_ARE_ADDR_EQUAL(laddr, 3663 <cp->tcp_bound_source_v6)) 3664 continue; 3665 if (ltcp->tcp_state >= TCPS_BOUND) { 3666 /* 3667 * This port is being used and 3668 * its state is >= TCPS_BOUND, 3669 * so we can't bind to it. 3670 */ 3671 break; 3672 } 3673 } else { 3674 /* 3675 * socket option SO_REUSEADDR is set on the 3676 * binding tcp_t. 3677 * 3678 * If two streams are bound to 3679 * same IP address or both addr 3680 * and bound source are wildcards 3681 * (INADDR_ANY), we want to stop 3682 * searching. 3683 * We have found a match of IP source 3684 * address and source port, which is 3685 * refused regardless of the 3686 * SO_REUSEADDR setting, so we break. 3687 */ 3688 if (IN6_ARE_ADDR_EQUAL(laddr, 3689 <cp->tcp_bound_source_v6) && 3690 (ltcp->tcp_state == TCPS_LISTEN || 3691 ltcp->tcp_state == TCPS_BOUND)) 3692 break; 3693 } 3694 } 3695 if (ltcp != NULL) { 3696 /* The port number is busy */ 3697 mutex_exit(&tbf->tf_lock); 3698 } else { 3699 /* 3700 * This port is ours. Insert in fanout and mark as 3701 * bound to prevent others from getting the port 3702 * number. 3703 */ 3704 tcp->tcp_state = TCPS_BOUND; 3705 tcp->tcp_lport = htons(port); 3706 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3707 3708 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3709 tcp->tcp_lport)] == tbf); 3710 tcp_bind_hash_insert(tbf, tcp, 1); 3711 3712 mutex_exit(&tbf->tf_lock); 3713 3714 /* 3715 * We don't want tcp_next_port_to_try to "inherit" 3716 * a port number supplied by the user in a bind. 3717 */ 3718 if (user_specified) 3719 return (port); 3720 3721 /* 3722 * This is the only place where tcp_next_port_to_try 3723 * is updated. After the update, it may or may not 3724 * be in the valid range. 3725 */ 3726 if (!tcp->tcp_anon_priv_bind) 3727 tcps->tcps_next_port_to_try = port + 1; 3728 return (port); 3729 } 3730 3731 if (tcp->tcp_anon_priv_bind) { 3732 port = tcp_get_next_priv_port(tcp); 3733 } else { 3734 if (count == 0 && user_specified) { 3735 /* 3736 * We may have to return an anonymous port. So 3737 * get one to start with. 3738 */ 3739 port = 3740 tcp_update_next_port( 3741 tcps->tcps_next_port_to_try, 3742 tcp, B_TRUE); 3743 user_specified = B_FALSE; 3744 } else { 3745 port = tcp_update_next_port(port + 1, tcp, 3746 B_FALSE); 3747 } 3748 } 3749 if (port == 0) 3750 break; 3751 3752 /* 3753 * Don't let this loop run forever in the case where 3754 * all of the anonymous ports are in use. 3755 */ 3756 } while (++count < loopmax); 3757 return (0); 3758 } 3759 3760 /* 3761 * tcp_clean_death / tcp_close_detached must not be called more than once 3762 * on a tcp. Thus every function that potentially calls tcp_clean_death 3763 * must check for the tcp state before calling tcp_clean_death. 3764 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3765 * tcp_timer_handler, all check for the tcp state. 3766 */ 3767 /* ARGSUSED */ 3768 void 3769 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3770 { 3771 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3772 3773 freemsg(mp); 3774 if (tcp->tcp_state > TCPS_BOUND) 3775 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, ETIMEDOUT, 5); 3776 } 3777 3778 /* 3779 * We are dying for some reason. Try to do it gracefully. (May be called 3780 * as writer.) 3781 * 3782 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3783 * done by a service procedure). 3784 * TBD - Should the return value distinguish between the tcp_t being 3785 * freed and it being reinitialized? 3786 */ 3787 static int 3788 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3789 { 3790 mblk_t *mp; 3791 queue_t *q; 3792 tcp_stack_t *tcps = tcp->tcp_tcps; 3793 3794 TCP_CLD_STAT(tag); 3795 3796 #if TCP_TAG_CLEAN_DEATH 3797 tcp->tcp_cleandeathtag = tag; 3798 #endif 3799 3800 if (tcp->tcp_fused) 3801 tcp_unfuse(tcp); 3802 3803 if (tcp->tcp_linger_tid != 0 && 3804 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3805 tcp_stop_lingering(tcp); 3806 } 3807 3808 ASSERT(tcp != NULL); 3809 ASSERT((tcp->tcp_family == AF_INET && 3810 tcp->tcp_ipversion == IPV4_VERSION) || 3811 (tcp->tcp_family == AF_INET6 && 3812 (tcp->tcp_ipversion == IPV4_VERSION || 3813 tcp->tcp_ipversion == IPV6_VERSION))); 3814 3815 if (TCP_IS_DETACHED(tcp)) { 3816 if (tcp->tcp_hard_binding) { 3817 /* 3818 * Its an eager that we are dealing with. We close the 3819 * eager but in case a conn_ind has already gone to the 3820 * listener, let tcp_accept_finish() send a discon_ind 3821 * to the listener and drop the last reference. If the 3822 * listener doesn't even know about the eager i.e. the 3823 * conn_ind hasn't gone up, blow away the eager and drop 3824 * the last reference as well. If the conn_ind has gone 3825 * up, state should be BOUND. tcp_accept_finish 3826 * will figure out that the connection has received a 3827 * RST and will send a DISCON_IND to the application. 3828 */ 3829 tcp_closei_local(tcp); 3830 if (!tcp->tcp_tconnind_started) { 3831 CONN_DEC_REF(tcp->tcp_connp); 3832 } else { 3833 tcp->tcp_state = TCPS_BOUND; 3834 } 3835 } else { 3836 tcp_close_detached(tcp); 3837 } 3838 return (0); 3839 } 3840 3841 TCP_STAT(tcps, tcp_clean_death_nondetached); 3842 3843 /* 3844 * If T_ORDREL_IND has not been sent yet (done when service routine 3845 * is run) postpone cleaning up the endpoint until service routine 3846 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3847 * client_errno since tcp_close uses the client_errno field. 3848 */ 3849 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3850 if (err != 0) 3851 tcp->tcp_client_errno = err; 3852 3853 tcp->tcp_deferred_clean_death = B_TRUE; 3854 return (-1); 3855 } 3856 3857 q = tcp->tcp_rq; 3858 3859 /* Trash all inbound data */ 3860 flushq(q, FLUSHALL); 3861 3862 /* 3863 * If we are at least part way open and there is error 3864 * (err==0 implies no error) 3865 * notify our client by a T_DISCON_IND. 3866 */ 3867 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3868 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3869 !TCP_IS_SOCKET(tcp)) { 3870 /* 3871 * Send M_FLUSH according to TPI. Because sockets will 3872 * (and must) ignore FLUSHR we do that only for TPI 3873 * endpoints and sockets in STREAMS mode. 3874 */ 3875 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3876 } 3877 if (tcp->tcp_debug) { 3878 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3879 "tcp_clean_death: discon err %d", err); 3880 } 3881 mp = mi_tpi_discon_ind(NULL, err, 0); 3882 if (mp != NULL) { 3883 putnext(q, mp); 3884 } else { 3885 if (tcp->tcp_debug) { 3886 (void) strlog(TCP_MOD_ID, 0, 1, 3887 SL_ERROR|SL_TRACE, 3888 "tcp_clean_death, sending M_ERROR"); 3889 } 3890 (void) putnextctl1(q, M_ERROR, EPROTO); 3891 } 3892 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3893 /* SYN_SENT or SYN_RCVD */ 3894 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3895 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3896 /* ESTABLISHED or CLOSE_WAIT */ 3897 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3898 } 3899 } 3900 3901 tcp_reinit(tcp); 3902 return (-1); 3903 } 3904 3905 /* 3906 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3907 * to expire, stop the wait and finish the close. 3908 */ 3909 static void 3910 tcp_stop_lingering(tcp_t *tcp) 3911 { 3912 clock_t delta = 0; 3913 tcp_stack_t *tcps = tcp->tcp_tcps; 3914 3915 tcp->tcp_linger_tid = 0; 3916 if (tcp->tcp_state > TCPS_LISTEN) { 3917 tcp_acceptor_hash_remove(tcp); 3918 mutex_enter(&tcp->tcp_non_sq_lock); 3919 if (tcp->tcp_flow_stopped) { 3920 tcp_clrqfull(tcp); 3921 } 3922 mutex_exit(&tcp->tcp_non_sq_lock); 3923 3924 if (tcp->tcp_timer_tid != 0) { 3925 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3926 tcp->tcp_timer_tid = 0; 3927 } 3928 /* 3929 * Need to cancel those timers which will not be used when 3930 * TCP is detached. This has to be done before the tcp_wq 3931 * is set to the global queue. 3932 */ 3933 tcp_timers_stop(tcp); 3934 3935 3936 tcp->tcp_detached = B_TRUE; 3937 ASSERT(tcps->tcps_g_q != NULL); 3938 tcp->tcp_rq = tcps->tcps_g_q; 3939 tcp->tcp_wq = WR(tcps->tcps_g_q); 3940 3941 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3942 tcp_time_wait_append(tcp); 3943 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3944 goto finish; 3945 } 3946 3947 /* 3948 * If delta is zero the timer event wasn't executed and was 3949 * successfully canceled. In this case we need to restart it 3950 * with the minimal delta possible. 3951 */ 3952 if (delta >= 0) { 3953 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3954 delta ? delta : 1); 3955 } 3956 } else { 3957 tcp_closei_local(tcp); 3958 CONN_DEC_REF(tcp->tcp_connp); 3959 } 3960 finish: 3961 /* Signal closing thread that it can complete close */ 3962 mutex_enter(&tcp->tcp_closelock); 3963 tcp->tcp_detached = B_TRUE; 3964 ASSERT(tcps->tcps_g_q != NULL); 3965 tcp->tcp_rq = tcps->tcps_g_q; 3966 tcp->tcp_wq = WR(tcps->tcps_g_q); 3967 tcp->tcp_closed = 1; 3968 cv_signal(&tcp->tcp_closecv); 3969 mutex_exit(&tcp->tcp_closelock); 3970 } 3971 3972 /* 3973 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3974 * expires. 3975 */ 3976 static void 3977 tcp_close_linger_timeout(void *arg) 3978 { 3979 conn_t *connp = (conn_t *)arg; 3980 tcp_t *tcp = connp->conn_tcp; 3981 3982 tcp->tcp_client_errno = ETIMEDOUT; 3983 tcp_stop_lingering(tcp); 3984 } 3985 3986 static int 3987 tcp_close(queue_t *q, int flags) 3988 { 3989 conn_t *connp = Q_TO_CONN(q); 3990 tcp_t *tcp = connp->conn_tcp; 3991 mblk_t *mp = &tcp->tcp_closemp; 3992 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3993 boolean_t linger_interrupted = B_FALSE; 3994 mblk_t *bp; 3995 3996 ASSERT(WR(q)->q_next == NULL); 3997 ASSERT(connp->conn_ref >= 2); 3998 ASSERT((connp->conn_flags & IPCL_TCPMOD) == 0); 3999 4000 /* 4001 * We are being closed as /dev/tcp or /dev/tcp6. 4002 * 4003 * Mark the conn as closing. ill_pending_mp_add will not 4004 * add any mp to the pending mp list, after this conn has 4005 * started closing. Same for sq_pending_mp_add 4006 */ 4007 mutex_enter(&connp->conn_lock); 4008 connp->conn_state_flags |= CONN_CLOSING; 4009 if (connp->conn_oper_pending_ill != NULL) 4010 conn_ioctl_cleanup_reqd = B_TRUE; 4011 CONN_INC_REF_LOCKED(connp); 4012 mutex_exit(&connp->conn_lock); 4013 tcp->tcp_closeflags = (uint8_t)flags; 4014 ASSERT(connp->conn_ref >= 3); 4015 4016 /* 4017 * tcp_closemp_used is used below without any protection of a lock 4018 * as we don't expect any one else to use it concurrently at this 4019 * point otherwise it would be a major defect, though we do 4020 * increment tcp_closemp_used to record any attempt to reuse 4021 * tcp_closemp while it is still in use. This would help debugging. 4022 */ 4023 4024 if (mp->b_prev == NULL) { 4025 tcp->tcp_closemp_used = 1; 4026 } else { 4027 tcp->tcp_closemp_used++; 4028 ASSERT(mp->b_prev == NULL); 4029 } 4030 4031 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4032 4033 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4034 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4035 4036 mutex_enter(&tcp->tcp_closelock); 4037 while (!tcp->tcp_closed) { 4038 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4039 /* 4040 * We got interrupted. Check if we are lingering, 4041 * if yes, post a message to stop and wait until 4042 * tcp_closed is set. If we aren't lingering, 4043 * just go back around. 4044 */ 4045 if (tcp->tcp_linger && 4046 tcp->tcp_lingertime > 0 && 4047 !linger_interrupted) { 4048 mutex_exit(&tcp->tcp_closelock); 4049 /* Entering squeue, bump ref count. */ 4050 CONN_INC_REF(connp); 4051 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4052 squeue_enter(connp->conn_sqp, bp, 4053 tcp_linger_interrupted, connp, 4054 SQTAG_IP_TCP_CLOSE); 4055 linger_interrupted = B_TRUE; 4056 mutex_enter(&tcp->tcp_closelock); 4057 } 4058 } 4059 } 4060 mutex_exit(&tcp->tcp_closelock); 4061 4062 /* 4063 * In the case of listener streams that have eagers in the q or q0 4064 * we wait for the eagers to drop their reference to us. tcp_rq and 4065 * tcp_wq of the eagers point to our queues. By waiting for the 4066 * refcnt to drop to 1, we are sure that the eagers have cleaned 4067 * up their queue pointers and also dropped their references to us. 4068 */ 4069 if (tcp->tcp_wait_for_eagers) { 4070 mutex_enter(&connp->conn_lock); 4071 while (connp->conn_ref != 1) { 4072 cv_wait(&connp->conn_cv, &connp->conn_lock); 4073 } 4074 mutex_exit(&connp->conn_lock); 4075 } 4076 /* 4077 * ioctl cleanup. The mp is queued in the 4078 * ill_pending_mp or in the sq_pending_mp. 4079 */ 4080 if (conn_ioctl_cleanup_reqd) 4081 conn_ioctl_cleanup(connp); 4082 4083 qprocsoff(q); 4084 inet_minor_free(ip_minor_arena, connp->conn_dev); 4085 4086 tcp->tcp_cpid = -1; 4087 4088 /* 4089 * Drop IP's reference on the conn. This is the last reference 4090 * on the connp if the state was less than established. If the 4091 * connection has gone into timewait state, then we will have 4092 * one ref for the TCP and one more ref (total of two) for the 4093 * classifier connected hash list (a timewait connections stays 4094 * in connected hash till closed). 4095 * 4096 * We can't assert the references because there might be other 4097 * transient reference places because of some walkers or queued 4098 * packets in squeue for the timewait state. 4099 */ 4100 CONN_DEC_REF(connp); 4101 q->q_ptr = WR(q)->q_ptr = NULL; 4102 return (0); 4103 } 4104 4105 static int 4106 tcpclose_accept(queue_t *q) 4107 { 4108 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4109 4110 /* 4111 * We had opened an acceptor STREAM for sockfs which is 4112 * now being closed due to some error. 4113 */ 4114 qprocsoff(q); 4115 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 4116 q->q_ptr = WR(q)->q_ptr = NULL; 4117 return (0); 4118 } 4119 4120 /* 4121 * Called by tcp_close() routine via squeue when lingering is 4122 * interrupted by a signal. 4123 */ 4124 4125 /* ARGSUSED */ 4126 static void 4127 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4128 { 4129 conn_t *connp = (conn_t *)arg; 4130 tcp_t *tcp = connp->conn_tcp; 4131 4132 freeb(mp); 4133 if (tcp->tcp_linger_tid != 0 && 4134 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4135 tcp_stop_lingering(tcp); 4136 tcp->tcp_client_errno = EINTR; 4137 } 4138 } 4139 4140 /* 4141 * Called by streams close routine via squeues when our client blows off her 4142 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4143 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4144 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4145 * acked. 4146 * 4147 * NOTE: tcp_close potentially returns error when lingering. 4148 * However, the stream head currently does not pass these errors 4149 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4150 * errors to the application (from tsleep()) and not errors 4151 * like ECONNRESET caused by receiving a reset packet. 4152 */ 4153 4154 /* ARGSUSED */ 4155 static void 4156 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4157 { 4158 char *msg; 4159 conn_t *connp = (conn_t *)arg; 4160 tcp_t *tcp = connp->conn_tcp; 4161 clock_t delta = 0; 4162 tcp_stack_t *tcps = tcp->tcp_tcps; 4163 4164 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4165 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4166 4167 /* Cancel any pending timeout */ 4168 if (tcp->tcp_ordrelid != 0) { 4169 if (tcp->tcp_timeout) { 4170 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4171 } 4172 tcp->tcp_ordrelid = 0; 4173 tcp->tcp_timeout = B_FALSE; 4174 } 4175 4176 mutex_enter(&tcp->tcp_eager_lock); 4177 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4178 /* Cleanup for listener */ 4179 tcp_eager_cleanup(tcp, 0); 4180 tcp->tcp_wait_for_eagers = 1; 4181 } 4182 mutex_exit(&tcp->tcp_eager_lock); 4183 4184 connp->conn_mdt_ok = B_FALSE; 4185 tcp->tcp_mdt = B_FALSE; 4186 4187 connp->conn_lso_ok = B_FALSE; 4188 tcp->tcp_lso = B_FALSE; 4189 4190 msg = NULL; 4191 switch (tcp->tcp_state) { 4192 case TCPS_CLOSED: 4193 case TCPS_IDLE: 4194 case TCPS_BOUND: 4195 case TCPS_LISTEN: 4196 break; 4197 case TCPS_SYN_SENT: 4198 msg = "tcp_close, during connect"; 4199 break; 4200 case TCPS_SYN_RCVD: 4201 /* 4202 * Close during the connect 3-way handshake 4203 * but here there may or may not be pending data 4204 * already on queue. Process almost same as in 4205 * the ESTABLISHED state. 4206 */ 4207 /* FALLTHRU */ 4208 default: 4209 if (tcp->tcp_fused) 4210 tcp_unfuse(tcp); 4211 4212 /* 4213 * If SO_LINGER has set a zero linger time, abort the 4214 * connection with a reset. 4215 */ 4216 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4217 msg = "tcp_close, zero lingertime"; 4218 break; 4219 } 4220 4221 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4222 /* 4223 * Abort connection if there is unread data queued. 4224 */ 4225 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4226 msg = "tcp_close, unread data"; 4227 break; 4228 } 4229 /* 4230 * tcp_hard_bound is now cleared thus all packets go through 4231 * tcp_lookup. This fact is used by tcp_detach below. 4232 * 4233 * We have done a qwait() above which could have possibly 4234 * drained more messages in turn causing transition to a 4235 * different state. Check whether we have to do the rest 4236 * of the processing or not. 4237 */ 4238 if (tcp->tcp_state <= TCPS_LISTEN) 4239 break; 4240 4241 /* 4242 * Transmit the FIN before detaching the tcp_t. 4243 * After tcp_detach returns this queue/perimeter 4244 * no longer owns the tcp_t thus others can modify it. 4245 */ 4246 (void) tcp_xmit_end(tcp); 4247 4248 /* 4249 * If lingering on close then wait until the fin is acked, 4250 * the SO_LINGER time passes, or a reset is sent/received. 4251 */ 4252 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4253 !(tcp->tcp_fin_acked) && 4254 tcp->tcp_state >= TCPS_ESTABLISHED) { 4255 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4256 tcp->tcp_client_errno = EWOULDBLOCK; 4257 } else if (tcp->tcp_client_errno == 0) { 4258 4259 ASSERT(tcp->tcp_linger_tid == 0); 4260 4261 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4262 tcp_close_linger_timeout, 4263 tcp->tcp_lingertime * hz); 4264 4265 /* tcp_close_linger_timeout will finish close */ 4266 if (tcp->tcp_linger_tid == 0) 4267 tcp->tcp_client_errno = ENOSR; 4268 else 4269 return; 4270 } 4271 4272 /* 4273 * Check if we need to detach or just close 4274 * the instance. 4275 */ 4276 if (tcp->tcp_state <= TCPS_LISTEN) 4277 break; 4278 } 4279 4280 /* 4281 * Make sure that no other thread will access the tcp_rq of 4282 * this instance (through lookups etc.) as tcp_rq will go 4283 * away shortly. 4284 */ 4285 tcp_acceptor_hash_remove(tcp); 4286 4287 mutex_enter(&tcp->tcp_non_sq_lock); 4288 if (tcp->tcp_flow_stopped) { 4289 tcp_clrqfull(tcp); 4290 } 4291 mutex_exit(&tcp->tcp_non_sq_lock); 4292 4293 if (tcp->tcp_timer_tid != 0) { 4294 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4295 tcp->tcp_timer_tid = 0; 4296 } 4297 /* 4298 * Need to cancel those timers which will not be used when 4299 * TCP is detached. This has to be done before the tcp_wq 4300 * is set to the global queue. 4301 */ 4302 tcp_timers_stop(tcp); 4303 4304 tcp->tcp_detached = B_TRUE; 4305 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4306 tcp_time_wait_append(tcp); 4307 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4308 ASSERT(connp->conn_ref >= 3); 4309 goto finish; 4310 } 4311 4312 /* 4313 * If delta is zero the timer event wasn't executed and was 4314 * successfully canceled. In this case we need to restart it 4315 * with the minimal delta possible. 4316 */ 4317 if (delta >= 0) 4318 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4319 delta ? delta : 1); 4320 4321 ASSERT(connp->conn_ref >= 3); 4322 goto finish; 4323 } 4324 4325 /* Detach did not complete. Still need to remove q from stream. */ 4326 if (msg) { 4327 if (tcp->tcp_state == TCPS_ESTABLISHED || 4328 tcp->tcp_state == TCPS_CLOSE_WAIT) 4329 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4330 if (tcp->tcp_state == TCPS_SYN_SENT || 4331 tcp->tcp_state == TCPS_SYN_RCVD) 4332 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4333 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4334 } 4335 4336 tcp_closei_local(tcp); 4337 CONN_DEC_REF(connp); 4338 ASSERT(connp->conn_ref >= 2); 4339 4340 finish: 4341 /* 4342 * Although packets are always processed on the correct 4343 * tcp's perimeter and access is serialized via squeue's, 4344 * IP still needs a queue when sending packets in time_wait 4345 * state so use WR(tcps_g_q) till ip_output() can be 4346 * changed to deal with just connp. For read side, we 4347 * could have set tcp_rq to NULL but there are some cases 4348 * in tcp_rput_data() from early days of this code which 4349 * do a putnext without checking if tcp is closed. Those 4350 * need to be identified before both tcp_rq and tcp_wq 4351 * can be set to NULL and tcps_g_q can disappear forever. 4352 */ 4353 mutex_enter(&tcp->tcp_closelock); 4354 /* 4355 * Don't change the queues in the case of a listener that has 4356 * eagers in its q or q0. It could surprise the eagers. 4357 * Instead wait for the eagers outside the squeue. 4358 */ 4359 if (!tcp->tcp_wait_for_eagers) { 4360 tcp->tcp_detached = B_TRUE; 4361 /* 4362 * When default queue is closing we set tcps_g_q to NULL 4363 * after the close is done. 4364 */ 4365 ASSERT(tcps->tcps_g_q != NULL); 4366 tcp->tcp_rq = tcps->tcps_g_q; 4367 tcp->tcp_wq = WR(tcps->tcps_g_q); 4368 } 4369 4370 /* Signal tcp_close() to finish closing. */ 4371 tcp->tcp_closed = 1; 4372 cv_signal(&tcp->tcp_closecv); 4373 mutex_exit(&tcp->tcp_closelock); 4374 } 4375 4376 4377 /* 4378 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4379 * Some stream heads get upset if they see these later on as anything but NULL. 4380 */ 4381 static void 4382 tcp_close_mpp(mblk_t **mpp) 4383 { 4384 mblk_t *mp; 4385 4386 if ((mp = *mpp) != NULL) { 4387 do { 4388 mp->b_next = NULL; 4389 mp->b_prev = NULL; 4390 } while ((mp = mp->b_cont) != NULL); 4391 4392 mp = *mpp; 4393 *mpp = NULL; 4394 freemsg(mp); 4395 } 4396 } 4397 4398 /* Do detached close. */ 4399 static void 4400 tcp_close_detached(tcp_t *tcp) 4401 { 4402 if (tcp->tcp_fused) 4403 tcp_unfuse(tcp); 4404 4405 /* 4406 * Clustering code serializes TCP disconnect callbacks and 4407 * cluster tcp list walks by blocking a TCP disconnect callback 4408 * if a cluster tcp list walk is in progress. This ensures 4409 * accurate accounting of TCPs in the cluster code even though 4410 * the TCP list walk itself is not atomic. 4411 */ 4412 tcp_closei_local(tcp); 4413 CONN_DEC_REF(tcp->tcp_connp); 4414 } 4415 4416 /* 4417 * Stop all TCP timers, and free the timer mblks if requested. 4418 */ 4419 void 4420 tcp_timers_stop(tcp_t *tcp) 4421 { 4422 if (tcp->tcp_timer_tid != 0) { 4423 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4424 tcp->tcp_timer_tid = 0; 4425 } 4426 if (tcp->tcp_ka_tid != 0) { 4427 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4428 tcp->tcp_ka_tid = 0; 4429 } 4430 if (tcp->tcp_ack_tid != 0) { 4431 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4432 tcp->tcp_ack_tid = 0; 4433 } 4434 if (tcp->tcp_push_tid != 0) { 4435 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4436 tcp->tcp_push_tid = 0; 4437 } 4438 } 4439 4440 /* 4441 * The tcp_t is going away. Remove it from all lists and set it 4442 * to TCPS_CLOSED. The freeing up of memory is deferred until 4443 * tcp_inactive. This is needed since a thread in tcp_rput might have 4444 * done a CONN_INC_REF on this structure before it was removed from the 4445 * hashes. 4446 */ 4447 static void 4448 tcp_closei_local(tcp_t *tcp) 4449 { 4450 ire_t *ire; 4451 conn_t *connp = tcp->tcp_connp; 4452 tcp_stack_t *tcps = tcp->tcp_tcps; 4453 4454 if (!TCP_IS_SOCKET(tcp)) 4455 tcp_acceptor_hash_remove(tcp); 4456 4457 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4458 tcp->tcp_ibsegs = 0; 4459 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4460 tcp->tcp_obsegs = 0; 4461 4462 /* 4463 * If we are an eager connection hanging off a listener that 4464 * hasn't formally accepted the connection yet, get off his 4465 * list and blow off any data that we have accumulated. 4466 */ 4467 if (tcp->tcp_listener != NULL) { 4468 tcp_t *listener = tcp->tcp_listener; 4469 mutex_enter(&listener->tcp_eager_lock); 4470 /* 4471 * tcp_tconnind_started == B_TRUE means that the 4472 * conn_ind has already gone to listener. At 4473 * this point, eager will be closed but we 4474 * leave it in listeners eager list so that 4475 * if listener decides to close without doing 4476 * accept, we can clean this up. In tcp_wput_accept 4477 * we take care of the case of accept on closed 4478 * eager. 4479 */ 4480 if (!tcp->tcp_tconnind_started) { 4481 tcp_eager_unlink(tcp); 4482 mutex_exit(&listener->tcp_eager_lock); 4483 /* 4484 * We don't want to have any pointers to the 4485 * listener queue, after we have released our 4486 * reference on the listener 4487 */ 4488 ASSERT(tcps->tcps_g_q != NULL); 4489 tcp->tcp_rq = tcps->tcps_g_q; 4490 tcp->tcp_wq = WR(tcps->tcps_g_q); 4491 CONN_DEC_REF(listener->tcp_connp); 4492 } else { 4493 mutex_exit(&listener->tcp_eager_lock); 4494 } 4495 } 4496 4497 /* Stop all the timers */ 4498 tcp_timers_stop(tcp); 4499 4500 if (tcp->tcp_state == TCPS_LISTEN) { 4501 if (tcp->tcp_ip_addr_cache) { 4502 kmem_free((void *)tcp->tcp_ip_addr_cache, 4503 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4504 tcp->tcp_ip_addr_cache = NULL; 4505 } 4506 } 4507 mutex_enter(&tcp->tcp_non_sq_lock); 4508 if (tcp->tcp_flow_stopped) 4509 tcp_clrqfull(tcp); 4510 mutex_exit(&tcp->tcp_non_sq_lock); 4511 4512 tcp_bind_hash_remove(tcp); 4513 /* 4514 * If the tcp_time_wait_collector (which runs outside the squeue) 4515 * is trying to remove this tcp from the time wait list, we will 4516 * block in tcp_time_wait_remove while trying to acquire the 4517 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4518 * requires the ipcl_hash_remove to be ordered after the 4519 * tcp_time_wait_remove for the refcnt checks to work correctly. 4520 */ 4521 if (tcp->tcp_state == TCPS_TIME_WAIT) 4522 (void) tcp_time_wait_remove(tcp, NULL); 4523 CL_INET_DISCONNECT(tcp); 4524 ipcl_hash_remove(connp); 4525 4526 /* 4527 * Delete the cached ire in conn_ire_cache and also mark 4528 * the conn as CONDEMNED 4529 */ 4530 mutex_enter(&connp->conn_lock); 4531 connp->conn_state_flags |= CONN_CONDEMNED; 4532 ire = connp->conn_ire_cache; 4533 connp->conn_ire_cache = NULL; 4534 mutex_exit(&connp->conn_lock); 4535 if (ire != NULL) 4536 IRE_REFRELE_NOTR(ire); 4537 4538 /* Need to cleanup any pending ioctls */ 4539 ASSERT(tcp->tcp_time_wait_next == NULL); 4540 ASSERT(tcp->tcp_time_wait_prev == NULL); 4541 ASSERT(tcp->tcp_time_wait_expire == 0); 4542 tcp->tcp_state = TCPS_CLOSED; 4543 4544 /* Release any SSL context */ 4545 if (tcp->tcp_kssl_ent != NULL) { 4546 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4547 tcp->tcp_kssl_ent = NULL; 4548 } 4549 if (tcp->tcp_kssl_ctx != NULL) { 4550 kssl_release_ctx(tcp->tcp_kssl_ctx); 4551 tcp->tcp_kssl_ctx = NULL; 4552 } 4553 tcp->tcp_kssl_pending = B_FALSE; 4554 4555 tcp_ipsec_cleanup(tcp); 4556 } 4557 4558 /* 4559 * tcp is dying (called from ipcl_conn_destroy and error cases). 4560 * Free the tcp_t in either case. 4561 */ 4562 void 4563 tcp_free(tcp_t *tcp) 4564 { 4565 mblk_t *mp; 4566 ip6_pkt_t *ipp; 4567 4568 ASSERT(tcp != NULL); 4569 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4570 4571 tcp->tcp_rq = NULL; 4572 tcp->tcp_wq = NULL; 4573 4574 tcp_close_mpp(&tcp->tcp_xmit_head); 4575 tcp_close_mpp(&tcp->tcp_reass_head); 4576 if (tcp->tcp_rcv_list != NULL) { 4577 /* Free b_next chain */ 4578 tcp_close_mpp(&tcp->tcp_rcv_list); 4579 } 4580 if ((mp = tcp->tcp_urp_mp) != NULL) { 4581 freemsg(mp); 4582 } 4583 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4584 freemsg(mp); 4585 } 4586 4587 if (tcp->tcp_fused_sigurg_mp != NULL) { 4588 freeb(tcp->tcp_fused_sigurg_mp); 4589 tcp->tcp_fused_sigurg_mp = NULL; 4590 } 4591 4592 if (tcp->tcp_sack_info != NULL) { 4593 if (tcp->tcp_notsack_list != NULL) { 4594 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4595 } 4596 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4597 } 4598 4599 if (tcp->tcp_hopopts != NULL) { 4600 mi_free(tcp->tcp_hopopts); 4601 tcp->tcp_hopopts = NULL; 4602 tcp->tcp_hopoptslen = 0; 4603 } 4604 ASSERT(tcp->tcp_hopoptslen == 0); 4605 if (tcp->tcp_dstopts != NULL) { 4606 mi_free(tcp->tcp_dstopts); 4607 tcp->tcp_dstopts = NULL; 4608 tcp->tcp_dstoptslen = 0; 4609 } 4610 ASSERT(tcp->tcp_dstoptslen == 0); 4611 if (tcp->tcp_rtdstopts != NULL) { 4612 mi_free(tcp->tcp_rtdstopts); 4613 tcp->tcp_rtdstopts = NULL; 4614 tcp->tcp_rtdstoptslen = 0; 4615 } 4616 ASSERT(tcp->tcp_rtdstoptslen == 0); 4617 if (tcp->tcp_rthdr != NULL) { 4618 mi_free(tcp->tcp_rthdr); 4619 tcp->tcp_rthdr = NULL; 4620 tcp->tcp_rthdrlen = 0; 4621 } 4622 ASSERT(tcp->tcp_rthdrlen == 0); 4623 4624 ipp = &tcp->tcp_sticky_ipp; 4625 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4626 IPPF_RTHDR)) 4627 ip6_pkt_free(ipp); 4628 4629 /* 4630 * Free memory associated with the tcp/ip header template. 4631 */ 4632 4633 if (tcp->tcp_iphc != NULL) 4634 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4635 4636 /* 4637 * Following is really a blowing away a union. 4638 * It happens to have exactly two members of identical size 4639 * the following code is enough. 4640 */ 4641 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4642 4643 if (tcp->tcp_tracebuf != NULL) { 4644 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4645 tcp->tcp_tracebuf = NULL; 4646 } 4647 } 4648 4649 4650 /* 4651 * Put a connection confirmation message upstream built from the 4652 * address information within 'iph' and 'tcph'. Report our success or failure. 4653 */ 4654 static boolean_t 4655 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4656 mblk_t **defermp) 4657 { 4658 sin_t sin; 4659 sin6_t sin6; 4660 mblk_t *mp; 4661 char *optp = NULL; 4662 int optlen = 0; 4663 cred_t *cr; 4664 4665 if (defermp != NULL) 4666 *defermp = NULL; 4667 4668 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4669 /* 4670 * Return in T_CONN_CON results of option negotiation through 4671 * the T_CONN_REQ. Note: If there is an real end-to-end option 4672 * negotiation, then what is received from remote end needs 4673 * to be taken into account but there is no such thing (yet?) 4674 * in our TCP/IP. 4675 * Note: We do not use mi_offset_param() here as 4676 * tcp_opts_conn_req contents do not directly come from 4677 * an application and are either generated in kernel or 4678 * from user input that was already verified. 4679 */ 4680 mp = tcp->tcp_conn.tcp_opts_conn_req; 4681 optp = (char *)(mp->b_rptr + 4682 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4683 optlen = (int) 4684 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4685 } 4686 4687 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4688 ipha_t *ipha = (ipha_t *)iphdr; 4689 4690 /* packet is IPv4 */ 4691 if (tcp->tcp_family == AF_INET) { 4692 sin = sin_null; 4693 sin.sin_addr.s_addr = ipha->ipha_src; 4694 sin.sin_port = *(uint16_t *)tcph->th_lport; 4695 sin.sin_family = AF_INET; 4696 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4697 (int)sizeof (sin_t), optp, optlen); 4698 } else { 4699 sin6 = sin6_null; 4700 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4701 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4702 sin6.sin6_family = AF_INET6; 4703 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4704 (int)sizeof (sin6_t), optp, optlen); 4705 4706 } 4707 } else { 4708 ip6_t *ip6h = (ip6_t *)iphdr; 4709 4710 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4711 ASSERT(tcp->tcp_family == AF_INET6); 4712 sin6 = sin6_null; 4713 sin6.sin6_addr = ip6h->ip6_src; 4714 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4715 sin6.sin6_family = AF_INET6; 4716 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4717 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4718 (int)sizeof (sin6_t), optp, optlen); 4719 } 4720 4721 if (!mp) 4722 return (B_FALSE); 4723 4724 if ((cr = DB_CRED(idmp)) != NULL) { 4725 mblk_setcred(mp, cr); 4726 DB_CPID(mp) = DB_CPID(idmp); 4727 } 4728 4729 if (defermp == NULL) 4730 putnext(tcp->tcp_rq, mp); 4731 else 4732 *defermp = mp; 4733 4734 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4735 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4736 return (B_TRUE); 4737 } 4738 4739 /* 4740 * Defense for the SYN attack - 4741 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4742 * one from the list of droppable eagers. This list is a subset of q0. 4743 * see comments before the definition of MAKE_DROPPABLE(). 4744 * 2. Don't drop a SYN request before its first timeout. This gives every 4745 * request at least til the first timeout to complete its 3-way handshake. 4746 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4747 * requests currently on the queue that has timed out. This will be used 4748 * as an indicator of whether an attack is under way, so that appropriate 4749 * actions can be taken. (It's incremented in tcp_timer() and decremented 4750 * either when eager goes into ESTABLISHED, or gets freed up.) 4751 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4752 * # of timeout drops back to <= q0len/32 => SYN alert off 4753 */ 4754 static boolean_t 4755 tcp_drop_q0(tcp_t *tcp) 4756 { 4757 tcp_t *eager; 4758 mblk_t *mp; 4759 tcp_stack_t *tcps = tcp->tcp_tcps; 4760 4761 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4762 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4763 4764 /* Pick oldest eager from the list of droppable eagers */ 4765 eager = tcp->tcp_eager_prev_drop_q0; 4766 4767 /* If list is empty. return B_FALSE */ 4768 if (eager == tcp) { 4769 return (B_FALSE); 4770 } 4771 4772 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4773 if ((mp = allocb(0, BPRI_HI)) == NULL) 4774 return (B_FALSE); 4775 4776 /* 4777 * Take this eager out from the list of droppable eagers since we are 4778 * going to drop it. 4779 */ 4780 MAKE_UNDROPPABLE(eager); 4781 4782 if (tcp->tcp_debug) { 4783 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4784 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4785 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4786 tcp->tcp_conn_req_cnt_q0, 4787 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4788 } 4789 4790 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4791 4792 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4793 CONN_INC_REF(eager->tcp_connp); 4794 4795 /* Mark the IRE created for this SYN request temporary */ 4796 tcp_ip_ire_mark_advice(eager); 4797 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4798 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4799 4800 return (B_TRUE); 4801 } 4802 4803 int 4804 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4805 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4806 { 4807 tcp_t *ltcp = lconnp->conn_tcp; 4808 tcp_t *tcp = connp->conn_tcp; 4809 mblk_t *tpi_mp; 4810 ipha_t *ipha; 4811 ip6_t *ip6h; 4812 sin6_t sin6; 4813 in6_addr_t v6dst; 4814 int err; 4815 int ifindex = 0; 4816 cred_t *cr; 4817 tcp_stack_t *tcps = tcp->tcp_tcps; 4818 4819 if (ipvers == IPV4_VERSION) { 4820 ipha = (ipha_t *)mp->b_rptr; 4821 4822 connp->conn_send = ip_output; 4823 connp->conn_recv = tcp_input; 4824 4825 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4826 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4827 4828 sin6 = sin6_null; 4829 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4830 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4831 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4832 sin6.sin6_family = AF_INET6; 4833 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4834 lconnp->conn_zoneid, tcps->tcps_netstack); 4835 if (tcp->tcp_recvdstaddr) { 4836 sin6_t sin6d; 4837 4838 sin6d = sin6_null; 4839 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4840 &sin6d.sin6_addr); 4841 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4842 sin6d.sin6_family = AF_INET; 4843 tpi_mp = mi_tpi_extconn_ind(NULL, 4844 (char *)&sin6d, sizeof (sin6_t), 4845 (char *)&tcp, 4846 (t_scalar_t)sizeof (intptr_t), 4847 (char *)&sin6d, sizeof (sin6_t), 4848 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4849 } else { 4850 tpi_mp = mi_tpi_conn_ind(NULL, 4851 (char *)&sin6, sizeof (sin6_t), 4852 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4853 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4854 } 4855 } else { 4856 ip6h = (ip6_t *)mp->b_rptr; 4857 4858 connp->conn_send = ip_output_v6; 4859 connp->conn_recv = tcp_input; 4860 4861 connp->conn_srcv6 = ip6h->ip6_dst; 4862 connp->conn_remv6 = ip6h->ip6_src; 4863 4864 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4865 ifindex = (int)DB_CKSUMSTUFF(mp); 4866 DB_CKSUMSTUFF(mp) = 0; 4867 4868 sin6 = sin6_null; 4869 sin6.sin6_addr = ip6h->ip6_src; 4870 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4871 sin6.sin6_family = AF_INET6; 4872 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4873 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4874 lconnp->conn_zoneid, tcps->tcps_netstack); 4875 4876 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4877 /* Pass up the scope_id of remote addr */ 4878 sin6.sin6_scope_id = ifindex; 4879 } else { 4880 sin6.sin6_scope_id = 0; 4881 } 4882 if (tcp->tcp_recvdstaddr) { 4883 sin6_t sin6d; 4884 4885 sin6d = sin6_null; 4886 sin6.sin6_addr = ip6h->ip6_dst; 4887 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4888 sin6d.sin6_family = AF_INET; 4889 tpi_mp = mi_tpi_extconn_ind(NULL, 4890 (char *)&sin6d, sizeof (sin6_t), 4891 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4892 (char *)&sin6d, sizeof (sin6_t), 4893 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4894 } else { 4895 tpi_mp = mi_tpi_conn_ind(NULL, 4896 (char *)&sin6, sizeof (sin6_t), 4897 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4898 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4899 } 4900 } 4901 4902 if (tpi_mp == NULL) 4903 return (ENOMEM); 4904 4905 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4906 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4907 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4908 connp->conn_fully_bound = B_FALSE; 4909 4910 if (tcps->tcps_trace) 4911 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4912 4913 /* Inherit information from the "parent" */ 4914 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4915 tcp->tcp_family = ltcp->tcp_family; 4916 tcp->tcp_wq = ltcp->tcp_wq; 4917 tcp->tcp_rq = ltcp->tcp_rq; 4918 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4919 tcp->tcp_detached = B_TRUE; 4920 if ((err = tcp_init_values(tcp)) != 0) { 4921 freemsg(tpi_mp); 4922 return (err); 4923 } 4924 4925 if (ipvers == IPV4_VERSION) { 4926 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4927 freemsg(tpi_mp); 4928 return (err); 4929 } 4930 ASSERT(tcp->tcp_ipha != NULL); 4931 } else { 4932 /* ifindex must be already set */ 4933 ASSERT(ifindex != 0); 4934 4935 if (ltcp->tcp_bound_if != 0) { 4936 /* 4937 * Set newtcp's bound_if equal to 4938 * listener's value. If ifindex is 4939 * not the same as ltcp->tcp_bound_if, 4940 * it must be a packet for the ipmp group 4941 * of interfaces 4942 */ 4943 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4944 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4945 tcp->tcp_bound_if = ifindex; 4946 } 4947 4948 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4949 tcp->tcp_recvifindex = 0; 4950 tcp->tcp_recvhops = 0xffffffffU; 4951 ASSERT(tcp->tcp_ip6h != NULL); 4952 } 4953 4954 tcp->tcp_lport = ltcp->tcp_lport; 4955 4956 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4957 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4958 /* 4959 * Listener had options of some sort; eager inherits. 4960 * Free up the eager template and allocate one 4961 * of the right size. 4962 */ 4963 if (tcp->tcp_hdr_grown) { 4964 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4965 } else { 4966 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4967 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4968 } 4969 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4970 KM_NOSLEEP); 4971 if (tcp->tcp_iphc == NULL) { 4972 tcp->tcp_iphc_len = 0; 4973 freemsg(tpi_mp); 4974 return (ENOMEM); 4975 } 4976 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4977 tcp->tcp_hdr_grown = B_TRUE; 4978 } 4979 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4980 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4981 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4982 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4983 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4984 4985 /* 4986 * Copy the IP+TCP header template from listener to eager 4987 */ 4988 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4989 if (tcp->tcp_ipversion == IPV6_VERSION) { 4990 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4991 IPPROTO_RAW) { 4992 tcp->tcp_ip6h = 4993 (ip6_t *)(tcp->tcp_iphc + 4994 sizeof (ip6i_t)); 4995 } else { 4996 tcp->tcp_ip6h = 4997 (ip6_t *)(tcp->tcp_iphc); 4998 } 4999 tcp->tcp_ipha = NULL; 5000 } else { 5001 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5002 tcp->tcp_ip6h = NULL; 5003 } 5004 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5005 tcp->tcp_ip_hdr_len); 5006 } else { 5007 /* 5008 * only valid case when ipversion of listener and 5009 * eager differ is when listener is IPv6 and 5010 * eager is IPv4. 5011 * Eager header template has been initialized to the 5012 * maximum v4 header sizes, which includes space for 5013 * TCP and IP options. 5014 */ 5015 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5016 (tcp->tcp_ipversion == IPV4_VERSION)); 5017 ASSERT(tcp->tcp_iphc_len >= 5018 TCP_MAX_COMBINED_HEADER_LENGTH); 5019 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5020 /* copy IP header fields individually */ 5021 tcp->tcp_ipha->ipha_ttl = 5022 ltcp->tcp_ip6h->ip6_hops; 5023 bcopy(ltcp->tcp_tcph->th_lport, 5024 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5025 } 5026 5027 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5028 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5029 sizeof (in_port_t)); 5030 5031 if (ltcp->tcp_lport == 0) { 5032 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5033 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5034 sizeof (in_port_t)); 5035 } 5036 5037 if (tcp->tcp_ipversion == IPV4_VERSION) { 5038 ASSERT(ipha != NULL); 5039 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5040 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5041 5042 /* Source routing option copyover (reverse it) */ 5043 if (tcps->tcps_rev_src_routes) 5044 tcp_opt_reverse(tcp, ipha); 5045 } else { 5046 ASSERT(ip6h != NULL); 5047 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5048 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5049 } 5050 5051 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5052 ASSERT(!tcp->tcp_tconnind_started); 5053 /* 5054 * If the SYN contains a credential, it's a loopback packet; attach 5055 * the credential to the TPI message. 5056 */ 5057 if ((cr = DB_CRED(idmp)) != NULL) { 5058 mblk_setcred(tpi_mp, cr); 5059 DB_CPID(tpi_mp) = DB_CPID(idmp); 5060 } 5061 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5062 5063 /* Inherit the listener's SSL protection state */ 5064 5065 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5066 kssl_hold_ent(tcp->tcp_kssl_ent); 5067 tcp->tcp_kssl_pending = B_TRUE; 5068 } 5069 5070 return (0); 5071 } 5072 5073 5074 int 5075 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5076 tcph_t *tcph, mblk_t *idmp) 5077 { 5078 tcp_t *ltcp = lconnp->conn_tcp; 5079 tcp_t *tcp = connp->conn_tcp; 5080 sin_t sin; 5081 mblk_t *tpi_mp = NULL; 5082 int err; 5083 cred_t *cr; 5084 tcp_stack_t *tcps = tcp->tcp_tcps; 5085 5086 sin = sin_null; 5087 sin.sin_addr.s_addr = ipha->ipha_src; 5088 sin.sin_port = *(uint16_t *)tcph->th_lport; 5089 sin.sin_family = AF_INET; 5090 if (ltcp->tcp_recvdstaddr) { 5091 sin_t sind; 5092 5093 sind = sin_null; 5094 sind.sin_addr.s_addr = ipha->ipha_dst; 5095 sind.sin_port = *(uint16_t *)tcph->th_fport; 5096 sind.sin_family = AF_INET; 5097 tpi_mp = mi_tpi_extconn_ind(NULL, 5098 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5099 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5100 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5101 } else { 5102 tpi_mp = mi_tpi_conn_ind(NULL, 5103 (char *)&sin, sizeof (sin_t), 5104 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5105 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5106 } 5107 5108 if (tpi_mp == NULL) { 5109 return (ENOMEM); 5110 } 5111 5112 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5113 connp->conn_send = ip_output; 5114 connp->conn_recv = tcp_input; 5115 connp->conn_fully_bound = B_FALSE; 5116 5117 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5118 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5119 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5120 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5121 5122 if (tcps->tcps_trace) { 5123 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 5124 } 5125 5126 /* Inherit information from the "parent" */ 5127 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5128 tcp->tcp_family = ltcp->tcp_family; 5129 tcp->tcp_wq = ltcp->tcp_wq; 5130 tcp->tcp_rq = ltcp->tcp_rq; 5131 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 5132 tcp->tcp_detached = B_TRUE; 5133 if ((err = tcp_init_values(tcp)) != 0) { 5134 freemsg(tpi_mp); 5135 return (err); 5136 } 5137 5138 /* 5139 * Let's make sure that eager tcp template has enough space to 5140 * copy IPv4 listener's tcp template. Since the conn_t structure is 5141 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5142 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5143 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5144 * extension headers or with ip6i_t struct). Note that bcopy() below 5145 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5146 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5147 */ 5148 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5149 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5150 5151 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5152 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5153 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5154 tcp->tcp_ttl = ltcp->tcp_ttl; 5155 tcp->tcp_tos = ltcp->tcp_tos; 5156 5157 /* Copy the IP+TCP header template from listener to eager */ 5158 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5159 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5160 tcp->tcp_ip6h = NULL; 5161 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5162 tcp->tcp_ip_hdr_len); 5163 5164 /* Initialize the IP addresses and Ports */ 5165 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5166 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5167 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5168 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5169 5170 /* Source routing option copyover (reverse it) */ 5171 if (tcps->tcps_rev_src_routes) 5172 tcp_opt_reverse(tcp, ipha); 5173 5174 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5175 ASSERT(!tcp->tcp_tconnind_started); 5176 5177 /* 5178 * If the SYN contains a credential, it's a loopback packet; attach 5179 * the credential to the TPI message. 5180 */ 5181 if ((cr = DB_CRED(idmp)) != NULL) { 5182 mblk_setcred(tpi_mp, cr); 5183 DB_CPID(tpi_mp) = DB_CPID(idmp); 5184 } 5185 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5186 5187 /* Inherit the listener's SSL protection state */ 5188 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5189 kssl_hold_ent(tcp->tcp_kssl_ent); 5190 tcp->tcp_kssl_pending = B_TRUE; 5191 } 5192 5193 return (0); 5194 } 5195 5196 /* 5197 * sets up conn for ipsec. 5198 * if the first mblk is M_CTL it is consumed and mpp is updated. 5199 * in case of error mpp is freed. 5200 */ 5201 conn_t * 5202 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5203 { 5204 conn_t *connp = tcp->tcp_connp; 5205 conn_t *econnp; 5206 squeue_t *new_sqp; 5207 mblk_t *first_mp = *mpp; 5208 mblk_t *mp = *mpp; 5209 boolean_t mctl_present = B_FALSE; 5210 uint_t ipvers; 5211 5212 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 5213 if (econnp == NULL) { 5214 freemsg(first_mp); 5215 return (NULL); 5216 } 5217 if (DB_TYPE(mp) == M_CTL) { 5218 if (mp->b_cont == NULL || 5219 mp->b_cont->b_datap->db_type != M_DATA) { 5220 freemsg(first_mp); 5221 return (NULL); 5222 } 5223 mp = mp->b_cont; 5224 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5225 freemsg(first_mp); 5226 return (NULL); 5227 } 5228 5229 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5230 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5231 mctl_present = B_TRUE; 5232 } else { 5233 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5234 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5235 } 5236 5237 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5238 DB_CKSUMSTART(mp) = 0; 5239 5240 ASSERT(OK_32PTR(mp->b_rptr)); 5241 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5242 if (ipvers == IPV4_VERSION) { 5243 uint16_t *up; 5244 uint32_t ports; 5245 ipha_t *ipha; 5246 5247 ipha = (ipha_t *)mp->b_rptr; 5248 up = (uint16_t *)((uchar_t *)ipha + 5249 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5250 ports = *(uint32_t *)up; 5251 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5252 ipha->ipha_dst, ipha->ipha_src, ports); 5253 } else { 5254 uint16_t *up; 5255 uint32_t ports; 5256 uint16_t ip_hdr_len; 5257 uint8_t *nexthdrp; 5258 ip6_t *ip6h; 5259 tcph_t *tcph; 5260 5261 ip6h = (ip6_t *)mp->b_rptr; 5262 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5263 ip_hdr_len = IPV6_HDR_LEN; 5264 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5265 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5266 CONN_DEC_REF(econnp); 5267 freemsg(first_mp); 5268 return (NULL); 5269 } 5270 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5271 up = (uint16_t *)tcph->th_lport; 5272 ports = *(uint32_t *)up; 5273 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5274 ip6h->ip6_dst, ip6h->ip6_src, ports); 5275 } 5276 5277 /* 5278 * The caller already ensured that there is a sqp present. 5279 */ 5280 econnp->conn_sqp = new_sqp; 5281 5282 if (connp->conn_policy != NULL) { 5283 ipsec_in_t *ii; 5284 ii = (ipsec_in_t *)(first_mp->b_rptr); 5285 ASSERT(ii->ipsec_in_policy == NULL); 5286 IPPH_REFHOLD(connp->conn_policy); 5287 ii->ipsec_in_policy = connp->conn_policy; 5288 5289 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5290 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5291 CONN_DEC_REF(econnp); 5292 freemsg(first_mp); 5293 return (NULL); 5294 } 5295 } 5296 5297 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5298 CONN_DEC_REF(econnp); 5299 freemsg(first_mp); 5300 return (NULL); 5301 } 5302 5303 /* 5304 * If we know we have some policy, pass the "IPSEC" 5305 * options size TCP uses this adjust the MSS. 5306 */ 5307 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5308 if (mctl_present) { 5309 freeb(first_mp); 5310 *mpp = mp; 5311 } 5312 5313 return (econnp); 5314 } 5315 5316 /* 5317 * tcp_get_conn/tcp_free_conn 5318 * 5319 * tcp_get_conn is used to get a clean tcp connection structure. 5320 * It tries to reuse the connections put on the freelist by the 5321 * time_wait_collector failing which it goes to kmem_cache. This 5322 * way has two benefits compared to just allocating from and 5323 * freeing to kmem_cache. 5324 * 1) The time_wait_collector can free (which includes the cleanup) 5325 * outside the squeue. So when the interrupt comes, we have a clean 5326 * connection sitting in the freelist. Obviously, this buys us 5327 * performance. 5328 * 5329 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5330 * has multiple disadvantages - tying up the squeue during alloc, and the 5331 * fact that IPSec policy initialization has to happen here which 5332 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5333 * But allocating the conn/tcp in IP land is also not the best since 5334 * we can't check the 'q' and 'q0' which are protected by squeue and 5335 * blindly allocate memory which might have to be freed here if we are 5336 * not allowed to accept the connection. By using the freelist and 5337 * putting the conn/tcp back in freelist, we don't pay a penalty for 5338 * allocating memory without checking 'q/q0' and freeing it if we can't 5339 * accept the connection. 5340 * 5341 * Care should be taken to put the conn back in the same squeue's freelist 5342 * from which it was allocated. Best results are obtained if conn is 5343 * allocated from listener's squeue and freed to the same. Time wait 5344 * collector will free up the freelist is the connection ends up sitting 5345 * there for too long. 5346 */ 5347 void * 5348 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5349 { 5350 tcp_t *tcp = NULL; 5351 conn_t *connp = NULL; 5352 squeue_t *sqp = (squeue_t *)arg; 5353 tcp_squeue_priv_t *tcp_time_wait; 5354 netstack_t *ns; 5355 5356 tcp_time_wait = 5357 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5358 5359 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5360 tcp = tcp_time_wait->tcp_free_list; 5361 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5362 if (tcp != NULL) { 5363 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5364 tcp_time_wait->tcp_free_list_cnt--; 5365 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5366 tcp->tcp_time_wait_next = NULL; 5367 connp = tcp->tcp_connp; 5368 connp->conn_flags |= IPCL_REUSED; 5369 5370 ASSERT(tcp->tcp_tcps == NULL); 5371 ASSERT(connp->conn_netstack == NULL); 5372 ns = tcps->tcps_netstack; 5373 netstack_hold(ns); 5374 connp->conn_netstack = ns; 5375 tcp->tcp_tcps = tcps; 5376 TCPS_REFHOLD(tcps); 5377 ipcl_globalhash_insert(connp); 5378 return ((void *)connp); 5379 } 5380 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5381 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5382 tcps->tcps_netstack)) == NULL) 5383 return (NULL); 5384 tcp = connp->conn_tcp; 5385 tcp->tcp_tcps = tcps; 5386 TCPS_REFHOLD(tcps); 5387 return ((void *)connp); 5388 } 5389 5390 /* 5391 * Update the cached label for the given tcp_t. This should be called once per 5392 * connection, and before any packets are sent or tcp_process_options is 5393 * invoked. Returns B_FALSE if the correct label could not be constructed. 5394 */ 5395 static boolean_t 5396 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5397 { 5398 conn_t *connp = tcp->tcp_connp; 5399 5400 if (tcp->tcp_ipversion == IPV4_VERSION) { 5401 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5402 int added; 5403 5404 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5405 connp->conn_mac_exempt, 5406 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5407 return (B_FALSE); 5408 5409 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5410 if (added == -1) 5411 return (B_FALSE); 5412 tcp->tcp_hdr_len += added; 5413 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5414 tcp->tcp_ip_hdr_len += added; 5415 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5416 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5417 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5418 tcp->tcp_hdr_len); 5419 if (added == -1) 5420 return (B_FALSE); 5421 tcp->tcp_hdr_len += added; 5422 tcp->tcp_tcph = (tcph_t *) 5423 ((uchar_t *)tcp->tcp_tcph + added); 5424 tcp->tcp_ip_hdr_len += added; 5425 } 5426 } else { 5427 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5428 5429 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5430 connp->conn_mac_exempt, 5431 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5432 return (B_FALSE); 5433 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5434 &tcp->tcp_label_len, optbuf) != 0) 5435 return (B_FALSE); 5436 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5437 return (B_FALSE); 5438 } 5439 5440 connp->conn_ulp_labeled = 1; 5441 5442 return (B_TRUE); 5443 } 5444 5445 /* BEGIN CSTYLED */ 5446 /* 5447 * 5448 * The sockfs ACCEPT path: 5449 * ======================= 5450 * 5451 * The eager is now established in its own perimeter as soon as SYN is 5452 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5453 * completes the accept processing on the acceptor STREAM. The sending 5454 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5455 * listener but a TLI/XTI listener completes the accept processing 5456 * on the listener perimeter. 5457 * 5458 * Common control flow for 3 way handshake: 5459 * ---------------------------------------- 5460 * 5461 * incoming SYN (listener perimeter) -> tcp_rput_data() 5462 * -> tcp_conn_request() 5463 * 5464 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5465 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5466 * 5467 * Sockfs ACCEPT Path: 5468 * ------------------- 5469 * 5470 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5471 * as STREAM entry point) 5472 * 5473 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5474 * 5475 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5476 * association (we are not behind eager's squeue but sockfs is protecting us 5477 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5478 * is changed to point at tcp_wput(). 5479 * 5480 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5481 * listener (done on listener's perimeter). 5482 * 5483 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5484 * accept. 5485 * 5486 * TLI/XTI client ACCEPT path: 5487 * --------------------------- 5488 * 5489 * soaccept() sends T_CONN_RES on the listener STREAM. 5490 * 5491 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5492 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5493 * 5494 * Locks: 5495 * ====== 5496 * 5497 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5498 * and listeners->tcp_eager_next_q. 5499 * 5500 * Referencing: 5501 * ============ 5502 * 5503 * 1) We start out in tcp_conn_request by eager placing a ref on 5504 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5505 * 5506 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5507 * doing so we place a ref on the eager. This ref is finally dropped at the 5508 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5509 * reference is dropped by the squeue framework. 5510 * 5511 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5512 * 5513 * The reference must be released by the same entity that added the reference 5514 * In the above scheme, the eager is the entity that adds and releases the 5515 * references. Note that tcp_accept_finish executes in the squeue of the eager 5516 * (albeit after it is attached to the acceptor stream). Though 1. executes 5517 * in the listener's squeue, the eager is nascent at this point and the 5518 * reference can be considered to have been added on behalf of the eager. 5519 * 5520 * Eager getting a Reset or listener closing: 5521 * ========================================== 5522 * 5523 * Once the listener and eager are linked, the listener never does the unlink. 5524 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5525 * a message on all eager perimeter. The eager then does the unlink, clears 5526 * any pointers to the listener's queue and drops the reference to the 5527 * listener. The listener waits in tcp_close outside the squeue until its 5528 * refcount has dropped to 1. This ensures that the listener has waited for 5529 * all eagers to clear their association with the listener. 5530 * 5531 * Similarly, if eager decides to go away, it can unlink itself and close. 5532 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5533 * the reference to eager is still valid because of the extra ref we put 5534 * in tcp_send_conn_ind. 5535 * 5536 * Listener can always locate the eager under the protection 5537 * of the listener->tcp_eager_lock, and then do a refhold 5538 * on the eager during the accept processing. 5539 * 5540 * The acceptor stream accesses the eager in the accept processing 5541 * based on the ref placed on eager before sending T_conn_ind. 5542 * The only entity that can negate this refhold is a listener close 5543 * which is mutually exclusive with an active acceptor stream. 5544 * 5545 * Eager's reference on the listener 5546 * =================================== 5547 * 5548 * If the accept happens (even on a closed eager) the eager drops its 5549 * reference on the listener at the start of tcp_accept_finish. If the 5550 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5551 * the reference is dropped in tcp_closei_local. If the listener closes, 5552 * the reference is dropped in tcp_eager_kill. In all cases the reference 5553 * is dropped while executing in the eager's context (squeue). 5554 */ 5555 /* END CSTYLED */ 5556 5557 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5558 5559 /* 5560 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5561 * tcp_rput_data will not see any SYN packets. 5562 */ 5563 /* ARGSUSED */ 5564 void 5565 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5566 { 5567 tcph_t *tcph; 5568 uint32_t seg_seq; 5569 tcp_t *eager; 5570 uint_t ipvers; 5571 ipha_t *ipha; 5572 ip6_t *ip6h; 5573 int err; 5574 conn_t *econnp = NULL; 5575 squeue_t *new_sqp; 5576 mblk_t *mp1; 5577 uint_t ip_hdr_len; 5578 conn_t *connp = (conn_t *)arg; 5579 tcp_t *tcp = connp->conn_tcp; 5580 ire_t *ire; 5581 cred_t *credp; 5582 tcp_stack_t *tcps = tcp->tcp_tcps; 5583 ip_stack_t *ipst; 5584 5585 if (tcp->tcp_state != TCPS_LISTEN) 5586 goto error2; 5587 5588 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5589 5590 mutex_enter(&tcp->tcp_eager_lock); 5591 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5592 mutex_exit(&tcp->tcp_eager_lock); 5593 TCP_STAT(tcps, tcp_listendrop); 5594 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5595 if (tcp->tcp_debug) { 5596 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5597 "tcp_conn_request: listen backlog (max=%d) " 5598 "overflow (%d pending) on %s", 5599 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5600 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5601 } 5602 goto error2; 5603 } 5604 5605 if (tcp->tcp_conn_req_cnt_q0 >= 5606 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5607 /* 5608 * Q0 is full. Drop a pending half-open req from the queue 5609 * to make room for the new SYN req. Also mark the time we 5610 * drop a SYN. 5611 * 5612 * A more aggressive defense against SYN attack will 5613 * be to set the "tcp_syn_defense" flag now. 5614 */ 5615 TCP_STAT(tcps, tcp_listendropq0); 5616 tcp->tcp_last_rcv_lbolt = lbolt64; 5617 if (!tcp_drop_q0(tcp)) { 5618 mutex_exit(&tcp->tcp_eager_lock); 5619 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5620 if (tcp->tcp_debug) { 5621 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5622 "tcp_conn_request: listen half-open queue " 5623 "(max=%d) full (%d pending) on %s", 5624 tcps->tcps_conn_req_max_q0, 5625 tcp->tcp_conn_req_cnt_q0, 5626 tcp_display(tcp, NULL, 5627 DISP_PORT_ONLY)); 5628 } 5629 goto error2; 5630 } 5631 } 5632 mutex_exit(&tcp->tcp_eager_lock); 5633 5634 /* 5635 * IP adds STRUIO_EAGER and ensures that the received packet is 5636 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5637 * link local address. If IPSec is enabled, db_struioflag has 5638 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5639 * otherwise an error case if neither of them is set. 5640 */ 5641 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5642 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5643 DB_CKSUMSTART(mp) = 0; 5644 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5645 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5646 if (econnp == NULL) 5647 goto error2; 5648 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5649 econnp->conn_sqp = new_sqp; 5650 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5651 /* 5652 * mp is updated in tcp_get_ipsec_conn(). 5653 */ 5654 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5655 if (econnp == NULL) { 5656 /* 5657 * mp freed by tcp_get_ipsec_conn. 5658 */ 5659 return; 5660 } 5661 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5662 } else { 5663 goto error2; 5664 } 5665 5666 ASSERT(DB_TYPE(mp) == M_DATA); 5667 5668 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5669 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5670 ASSERT(OK_32PTR(mp->b_rptr)); 5671 if (ipvers == IPV4_VERSION) { 5672 ipha = (ipha_t *)mp->b_rptr; 5673 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5674 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5675 } else { 5676 ip6h = (ip6_t *)mp->b_rptr; 5677 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5678 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5679 } 5680 5681 if (tcp->tcp_family == AF_INET) { 5682 ASSERT(ipvers == IPV4_VERSION); 5683 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5684 } else { 5685 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5686 } 5687 5688 if (err) 5689 goto error3; 5690 5691 eager = econnp->conn_tcp; 5692 5693 /* Inherit various TCP parameters from the listener */ 5694 eager->tcp_naglim = tcp->tcp_naglim; 5695 eager->tcp_first_timer_threshold = 5696 tcp->tcp_first_timer_threshold; 5697 eager->tcp_second_timer_threshold = 5698 tcp->tcp_second_timer_threshold; 5699 5700 eager->tcp_first_ctimer_threshold = 5701 tcp->tcp_first_ctimer_threshold; 5702 eager->tcp_second_ctimer_threshold = 5703 tcp->tcp_second_ctimer_threshold; 5704 5705 /* 5706 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5707 * If it does not, the eager's receive window will be set to the 5708 * listener's receive window later in this function. 5709 */ 5710 eager->tcp_rwnd = 0; 5711 5712 /* 5713 * Inherit listener's tcp_init_cwnd. Need to do this before 5714 * calling tcp_process_options() where tcp_mss_set() is called 5715 * to set the initial cwnd. 5716 */ 5717 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5718 5719 /* 5720 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5721 * zone id before the accept is completed in tcp_wput_accept(). 5722 */ 5723 econnp->conn_zoneid = connp->conn_zoneid; 5724 econnp->conn_allzones = connp->conn_allzones; 5725 5726 /* Copy nexthop information from listener to eager */ 5727 if (connp->conn_nexthop_set) { 5728 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5729 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5730 } 5731 5732 /* 5733 * TSOL: tsol_input_proc() needs the eager's cred before the 5734 * eager is accepted 5735 */ 5736 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5737 crhold(credp); 5738 5739 /* 5740 * If the caller has the process-wide flag set, then default to MAC 5741 * exempt mode. This allows read-down to unlabeled hosts. 5742 */ 5743 if (getpflags(NET_MAC_AWARE, credp) != 0) 5744 econnp->conn_mac_exempt = B_TRUE; 5745 5746 if (is_system_labeled()) { 5747 cred_t *cr; 5748 5749 if (connp->conn_mlp_type != mlptSingle) { 5750 cr = econnp->conn_peercred = DB_CRED(mp); 5751 if (cr != NULL) 5752 crhold(cr); 5753 else 5754 cr = econnp->conn_cred; 5755 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5756 econnp, cred_t *, cr) 5757 } else { 5758 cr = econnp->conn_cred; 5759 DTRACE_PROBE2(syn_accept, conn_t *, 5760 econnp, cred_t *, cr) 5761 } 5762 5763 if (!tcp_update_label(eager, cr)) { 5764 DTRACE_PROBE3( 5765 tx__ip__log__error__connrequest__tcp, 5766 char *, "eager connp(1) label on SYN mp(2) failed", 5767 conn_t *, econnp, mblk_t *, mp); 5768 goto error3; 5769 } 5770 } 5771 5772 eager->tcp_hard_binding = B_TRUE; 5773 5774 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5775 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5776 5777 CL_INET_CONNECT(eager); 5778 5779 /* 5780 * No need to check for multicast destination since ip will only pass 5781 * up multicasts to those that have expressed interest 5782 * TODO: what about rejecting broadcasts? 5783 * Also check that source is not a multicast or broadcast address. 5784 */ 5785 eager->tcp_state = TCPS_SYN_RCVD; 5786 5787 5788 /* 5789 * There should be no ire in the mp as we are being called after 5790 * receiving the SYN. 5791 */ 5792 ASSERT(tcp_ire_mp(mp) == NULL); 5793 5794 /* 5795 * Adapt our mss, ttl, ... according to information provided in IRE. 5796 */ 5797 5798 if (tcp_adapt_ire(eager, NULL) == 0) { 5799 /* Undo the bind_hash_insert */ 5800 tcp_bind_hash_remove(eager); 5801 goto error3; 5802 } 5803 5804 /* Process all TCP options. */ 5805 tcp_process_options(eager, tcph); 5806 5807 /* Is the other end ECN capable? */ 5808 if (tcps->tcps_ecn_permitted >= 1 && 5809 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5810 eager->tcp_ecn_ok = B_TRUE; 5811 } 5812 5813 /* 5814 * listener->tcp_rq->q_hiwat should be the default window size or a 5815 * window size changed via SO_RCVBUF option. First round up the 5816 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5817 * scale option value if needed. Call tcp_rwnd_set() to finish the 5818 * setting. 5819 * 5820 * Note if there is a rpipe metric associated with the remote host, 5821 * we should not inherit receive window size from listener. 5822 */ 5823 eager->tcp_rwnd = MSS_ROUNDUP( 5824 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5825 eager->tcp_rwnd), eager->tcp_mss); 5826 if (eager->tcp_snd_ws_ok) 5827 tcp_set_ws_value(eager); 5828 /* 5829 * Note that this is the only place tcp_rwnd_set() is called for 5830 * accepting a connection. We need to call it here instead of 5831 * after the 3-way handshake because we need to tell the other 5832 * side our rwnd in the SYN-ACK segment. 5833 */ 5834 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5835 5836 /* 5837 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5838 * via soaccept()->soinheritoptions() which essentially applies 5839 * all the listener options to the new STREAM. The options that we 5840 * need to take care of are: 5841 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5842 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5843 * SO_SNDBUF, SO_RCVBUF. 5844 * 5845 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5846 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5847 * tcp_maxpsz_set() gets called later from 5848 * tcp_accept_finish(), the option takes effect. 5849 * 5850 */ 5851 /* Set the TCP options */ 5852 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5853 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5854 eager->tcp_oobinline = tcp->tcp_oobinline; 5855 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5856 eager->tcp_broadcast = tcp->tcp_broadcast; 5857 eager->tcp_useloopback = tcp->tcp_useloopback; 5858 eager->tcp_dontroute = tcp->tcp_dontroute; 5859 eager->tcp_linger = tcp->tcp_linger; 5860 eager->tcp_lingertime = tcp->tcp_lingertime; 5861 if (tcp->tcp_ka_enabled) 5862 eager->tcp_ka_enabled = 1; 5863 5864 /* Set the IP options */ 5865 econnp->conn_broadcast = connp->conn_broadcast; 5866 econnp->conn_loopback = connp->conn_loopback; 5867 econnp->conn_dontroute = connp->conn_dontroute; 5868 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5869 5870 /* Put a ref on the listener for the eager. */ 5871 CONN_INC_REF(connp); 5872 mutex_enter(&tcp->tcp_eager_lock); 5873 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5874 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5875 tcp->tcp_eager_next_q0 = eager; 5876 eager->tcp_eager_prev_q0 = tcp; 5877 5878 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5879 eager->tcp_listener = tcp; 5880 eager->tcp_saved_listener = tcp; 5881 5882 /* 5883 * Tag this detached tcp vector for later retrieval 5884 * by our listener client in tcp_accept(). 5885 */ 5886 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5887 tcp->tcp_conn_req_cnt_q0++; 5888 if (++tcp->tcp_conn_req_seqnum == -1) { 5889 /* 5890 * -1 is "special" and defined in TPI as something 5891 * that should never be used in T_CONN_IND 5892 */ 5893 ++tcp->tcp_conn_req_seqnum; 5894 } 5895 mutex_exit(&tcp->tcp_eager_lock); 5896 5897 if (tcp->tcp_syn_defense) { 5898 /* Don't drop the SYN that comes from a good IP source */ 5899 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5900 if (addr_cache != NULL && eager->tcp_remote == 5901 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5902 eager->tcp_dontdrop = B_TRUE; 5903 } 5904 } 5905 5906 /* 5907 * We need to insert the eager in its own perimeter but as soon 5908 * as we do that, we expose the eager to the classifier and 5909 * should not touch any field outside the eager's perimeter. 5910 * So do all the work necessary before inserting the eager 5911 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5912 * will succeed but undo everything if it fails. 5913 */ 5914 seg_seq = ABE32_TO_U32(tcph->th_seq); 5915 eager->tcp_irs = seg_seq; 5916 eager->tcp_rack = seg_seq; 5917 eager->tcp_rnxt = seg_seq + 1; 5918 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5919 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5920 eager->tcp_state = TCPS_SYN_RCVD; 5921 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5922 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5923 if (mp1 == NULL) 5924 goto error1; 5925 DB_CPID(mp1) = tcp->tcp_cpid; 5926 eager->tcp_cpid = tcp->tcp_cpid; 5927 eager->tcp_open_time = lbolt64; 5928 5929 /* 5930 * We need to start the rto timer. In normal case, we start 5931 * the timer after sending the packet on the wire (or at 5932 * least believing that packet was sent by waiting for 5933 * CALL_IP_WPUT() to return). Since this is the first packet 5934 * being sent on the wire for the eager, our initial tcp_rto 5935 * is at least tcp_rexmit_interval_min which is a fairly 5936 * large value to allow the algorithm to adjust slowly to large 5937 * fluctuations of RTT during first few transmissions. 5938 * 5939 * Starting the timer first and then sending the packet in this 5940 * case shouldn't make much difference since tcp_rexmit_interval_min 5941 * is of the order of several 100ms and starting the timer 5942 * first and then sending the packet will result in difference 5943 * of few micro seconds. 5944 * 5945 * Without this optimization, we are forced to hold the fanout 5946 * lock across the ipcl_bind_insert() and sending the packet 5947 * so that we don't race against an incoming packet (maybe RST) 5948 * for this eager. 5949 */ 5950 5951 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5952 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5953 5954 5955 /* 5956 * Insert the eager in its own perimeter now. We are ready to deal 5957 * with any packets on eager. 5958 */ 5959 if (eager->tcp_ipversion == IPV4_VERSION) { 5960 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5961 goto error; 5962 } 5963 } else { 5964 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5965 goto error; 5966 } 5967 } 5968 5969 /* mark conn as fully-bound */ 5970 econnp->conn_fully_bound = B_TRUE; 5971 5972 /* Send the SYN-ACK */ 5973 tcp_send_data(eager, eager->tcp_wq, mp1); 5974 freemsg(mp); 5975 5976 return; 5977 error: 5978 (void) TCP_TIMER_CANCEL(eager, eager->tcp_timer_tid); 5979 freemsg(mp1); 5980 error1: 5981 /* Undo what we did above */ 5982 mutex_enter(&tcp->tcp_eager_lock); 5983 tcp_eager_unlink(eager); 5984 mutex_exit(&tcp->tcp_eager_lock); 5985 /* Drop eager's reference on the listener */ 5986 CONN_DEC_REF(connp); 5987 5988 /* 5989 * Delete the cached ire in conn_ire_cache and also mark 5990 * the conn as CONDEMNED 5991 */ 5992 mutex_enter(&econnp->conn_lock); 5993 econnp->conn_state_flags |= CONN_CONDEMNED; 5994 ire = econnp->conn_ire_cache; 5995 econnp->conn_ire_cache = NULL; 5996 mutex_exit(&econnp->conn_lock); 5997 if (ire != NULL) 5998 IRE_REFRELE_NOTR(ire); 5999 6000 /* 6001 * tcp_accept_comm inserts the eager to the bind_hash 6002 * we need to remove it from the hash if ipcl_conn_insert 6003 * fails. 6004 */ 6005 tcp_bind_hash_remove(eager); 6006 /* Drop the eager ref placed in tcp_open_detached */ 6007 CONN_DEC_REF(econnp); 6008 6009 /* 6010 * If a connection already exists, send the mp to that connections so 6011 * that it can be appropriately dealt with. 6012 */ 6013 ipst = tcps->tcps_netstack->netstack_ip; 6014 6015 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 6016 if (!IPCL_IS_CONNECTED(econnp)) { 6017 /* 6018 * Something bad happened. ipcl_conn_insert() 6019 * failed because a connection already existed 6020 * in connected hash but we can't find it 6021 * anymore (someone blew it away). Just 6022 * free this message and hopefully remote 6023 * will retransmit at which time the SYN can be 6024 * treated as a new connection or dealth with 6025 * a TH_RST if a connection already exists. 6026 */ 6027 CONN_DEC_REF(econnp); 6028 freemsg(mp); 6029 } else { 6030 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6031 econnp, SQTAG_TCP_CONN_REQ); 6032 } 6033 } else { 6034 /* Nobody wants this packet */ 6035 freemsg(mp); 6036 } 6037 return; 6038 error2: 6039 freemsg(mp); 6040 return; 6041 error3: 6042 CONN_DEC_REF(econnp); 6043 freemsg(mp); 6044 } 6045 6046 /* 6047 * In an ideal case of vertical partition in NUMA architecture, its 6048 * beneficial to have the listener and all the incoming connections 6049 * tied to the same squeue. The other constraint is that incoming 6050 * connections should be tied to the squeue attached to interrupted 6051 * CPU for obvious locality reason so this leaves the listener to 6052 * be tied to the same squeue. Our only problem is that when listener 6053 * is binding, the CPU that will get interrupted by the NIC whose 6054 * IP address the listener is binding to is not even known. So 6055 * the code below allows us to change that binding at the time the 6056 * CPU is interrupted by virtue of incoming connection's squeue. 6057 * 6058 * This is usefull only in case of a listener bound to a specific IP 6059 * address. For other kind of listeners, they get bound the 6060 * very first time and there is no attempt to rebind them. 6061 */ 6062 void 6063 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6064 { 6065 conn_t *connp = (conn_t *)arg; 6066 squeue_t *sqp = (squeue_t *)arg2; 6067 squeue_t *new_sqp; 6068 uint32_t conn_flags; 6069 6070 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6071 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6072 } else { 6073 goto done; 6074 } 6075 6076 if (connp->conn_fanout == NULL) 6077 goto done; 6078 6079 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6080 mutex_enter(&connp->conn_fanout->connf_lock); 6081 mutex_enter(&connp->conn_lock); 6082 /* 6083 * No one from read or write side can access us now 6084 * except for already queued packets on this squeue. 6085 * But since we haven't changed the squeue yet, they 6086 * can't execute. If they are processed after we have 6087 * changed the squeue, they are sent back to the 6088 * correct squeue down below. 6089 * But a listner close can race with processing of 6090 * incoming SYN. If incoming SYN processing changes 6091 * the squeue then the listener close which is waiting 6092 * to enter the squeue would operate on the wrong 6093 * squeue. Hence we don't change the squeue here unless 6094 * the refcount is exactly the minimum refcount. The 6095 * minimum refcount of 4 is counted as - 1 each for 6096 * TCP and IP, 1 for being in the classifier hash, and 6097 * 1 for the mblk being processed. 6098 */ 6099 6100 if (connp->conn_ref != 4 || 6101 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6102 mutex_exit(&connp->conn_lock); 6103 mutex_exit(&connp->conn_fanout->connf_lock); 6104 goto done; 6105 } 6106 if (connp->conn_sqp != new_sqp) { 6107 while (connp->conn_sqp != new_sqp) 6108 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6109 } 6110 6111 do { 6112 conn_flags = connp->conn_flags; 6113 conn_flags |= IPCL_FULLY_BOUND; 6114 (void) cas32(&connp->conn_flags, connp->conn_flags, 6115 conn_flags); 6116 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6117 6118 mutex_exit(&connp->conn_fanout->connf_lock); 6119 mutex_exit(&connp->conn_lock); 6120 } 6121 6122 done: 6123 if (connp->conn_sqp != sqp) { 6124 CONN_INC_REF(connp); 6125 squeue_fill(connp->conn_sqp, mp, 6126 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6127 } else { 6128 tcp_conn_request(connp, mp, sqp); 6129 } 6130 } 6131 6132 /* 6133 * Successful connect request processing begins when our client passes 6134 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6135 * our T_OK_ACK reply message upstream. The control flow looks like this: 6136 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6137 * upstream <- tcp_rput() <- IP 6138 * After various error checks are completed, tcp_connect() lays 6139 * the target address and port into the composite header template, 6140 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6141 * request followed by an IRE request, and passes the three mblk message 6142 * down to IP looking like this: 6143 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6144 * Processing continues in tcp_rput() when we receive the following message: 6145 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6146 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6147 * to fire off the connection request, and then passes the T_OK_ACK mblk 6148 * upstream that we filled in below. There are, of course, numerous 6149 * error conditions along the way which truncate the processing described 6150 * above. 6151 */ 6152 static void 6153 tcp_connect(tcp_t *tcp, mblk_t *mp) 6154 { 6155 sin_t *sin; 6156 sin6_t *sin6; 6157 queue_t *q = tcp->tcp_wq; 6158 struct T_conn_req *tcr; 6159 ipaddr_t *dstaddrp; 6160 in_port_t dstport; 6161 uint_t srcid; 6162 6163 tcr = (struct T_conn_req *)mp->b_rptr; 6164 6165 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6166 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6167 tcp_err_ack(tcp, mp, TPROTO, 0); 6168 return; 6169 } 6170 6171 /* 6172 * Determine packet type based on type of address passed in 6173 * the request should contain an IPv4 or IPv6 address. 6174 * Make sure that address family matches the type of 6175 * family of the the address passed down 6176 */ 6177 switch (tcr->DEST_length) { 6178 default: 6179 tcp_err_ack(tcp, mp, TBADADDR, 0); 6180 return; 6181 6182 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6183 /* 6184 * XXX: The check for valid DEST_length was not there 6185 * in earlier releases and some buggy 6186 * TLI apps (e.g Sybase) got away with not feeding 6187 * in sin_zero part of address. 6188 * We allow that bug to keep those buggy apps humming. 6189 * Test suites require the check on DEST_length. 6190 * We construct a new mblk with valid DEST_length 6191 * free the original so the rest of the code does 6192 * not have to keep track of this special shorter 6193 * length address case. 6194 */ 6195 mblk_t *nmp; 6196 struct T_conn_req *ntcr; 6197 sin_t *nsin; 6198 6199 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6200 tcr->OPT_length, BPRI_HI); 6201 if (nmp == NULL) { 6202 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6203 return; 6204 } 6205 ntcr = (struct T_conn_req *)nmp->b_rptr; 6206 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6207 ntcr->PRIM_type = T_CONN_REQ; 6208 ntcr->DEST_length = sizeof (sin_t); 6209 ntcr->DEST_offset = sizeof (struct T_conn_req); 6210 6211 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6212 *nsin = sin_null; 6213 /* Get pointer to shorter address to copy from original mp */ 6214 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6215 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6216 if (sin == NULL || !OK_32PTR((char *)sin)) { 6217 freemsg(nmp); 6218 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6219 return; 6220 } 6221 nsin->sin_family = sin->sin_family; 6222 nsin->sin_port = sin->sin_port; 6223 nsin->sin_addr = sin->sin_addr; 6224 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6225 nmp->b_wptr = (uchar_t *)&nsin[1]; 6226 if (tcr->OPT_length != 0) { 6227 ntcr->OPT_length = tcr->OPT_length; 6228 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6229 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6230 (uchar_t *)ntcr + ntcr->OPT_offset, 6231 tcr->OPT_length); 6232 nmp->b_wptr += tcr->OPT_length; 6233 } 6234 freemsg(mp); /* original mp freed */ 6235 mp = nmp; /* re-initialize original variables */ 6236 tcr = ntcr; 6237 } 6238 /* FALLTHRU */ 6239 6240 case sizeof (sin_t): 6241 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6242 sizeof (sin_t)); 6243 if (sin == NULL || !OK_32PTR((char *)sin)) { 6244 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6245 return; 6246 } 6247 if (tcp->tcp_family != AF_INET || 6248 sin->sin_family != AF_INET) { 6249 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6250 return; 6251 } 6252 if (sin->sin_port == 0) { 6253 tcp_err_ack(tcp, mp, TBADADDR, 0); 6254 return; 6255 } 6256 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6257 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6258 return; 6259 } 6260 6261 break; 6262 6263 case sizeof (sin6_t): 6264 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6265 sizeof (sin6_t)); 6266 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6267 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6268 return; 6269 } 6270 if (tcp->tcp_family != AF_INET6 || 6271 sin6->sin6_family != AF_INET6) { 6272 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6273 return; 6274 } 6275 if (sin6->sin6_port == 0) { 6276 tcp_err_ack(tcp, mp, TBADADDR, 0); 6277 return; 6278 } 6279 break; 6280 } 6281 /* 6282 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6283 * should key on their sequence number and cut them loose. 6284 */ 6285 6286 /* 6287 * If options passed in, feed it for verification and handling 6288 */ 6289 if (tcr->OPT_length != 0) { 6290 mblk_t *ok_mp; 6291 mblk_t *discon_mp; 6292 mblk_t *conn_opts_mp; 6293 int t_error, sys_error, do_disconnect; 6294 6295 conn_opts_mp = NULL; 6296 6297 if (tcp_conprim_opt_process(tcp, mp, 6298 &do_disconnect, &t_error, &sys_error) < 0) { 6299 if (do_disconnect) { 6300 ASSERT(t_error == 0 && sys_error == 0); 6301 discon_mp = mi_tpi_discon_ind(NULL, 6302 ECONNREFUSED, 0); 6303 if (!discon_mp) { 6304 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6305 TSYSERR, ENOMEM); 6306 return; 6307 } 6308 ok_mp = mi_tpi_ok_ack_alloc(mp); 6309 if (!ok_mp) { 6310 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6311 TSYSERR, ENOMEM); 6312 return; 6313 } 6314 qreply(q, ok_mp); 6315 qreply(q, discon_mp); /* no flush! */ 6316 } else { 6317 ASSERT(t_error != 0); 6318 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6319 sys_error); 6320 } 6321 return; 6322 } 6323 /* 6324 * Success in setting options, the mp option buffer represented 6325 * by OPT_length/offset has been potentially modified and 6326 * contains results of option processing. We copy it in 6327 * another mp to save it for potentially influencing returning 6328 * it in T_CONN_CONN. 6329 */ 6330 if (tcr->OPT_length != 0) { /* there are resulting options */ 6331 conn_opts_mp = copyb(mp); 6332 if (!conn_opts_mp) { 6333 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6334 TSYSERR, ENOMEM); 6335 return; 6336 } 6337 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6338 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6339 /* 6340 * Note: 6341 * These resulting option negotiation can include any 6342 * end-to-end negotiation options but there no such 6343 * thing (yet?) in our TCP/IP. 6344 */ 6345 } 6346 } 6347 6348 /* 6349 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6350 * make sure that the template IP header in the tcp structure is an 6351 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6352 * need to this before we call tcp_bindi() so that the port lookup 6353 * code will look for ports in the correct port space (IPv4 and 6354 * IPv6 have separate port spaces). 6355 */ 6356 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6357 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6358 int err = 0; 6359 6360 err = tcp_header_init_ipv4(tcp); 6361 if (err != 0) { 6362 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6363 goto connect_failed; 6364 } 6365 if (tcp->tcp_lport != 0) 6366 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6367 } 6368 6369 switch (tcp->tcp_state) { 6370 case TCPS_IDLE: 6371 /* 6372 * We support quick connect, refer to comments in 6373 * tcp_connect_*() 6374 */ 6375 /* FALLTHRU */ 6376 case TCPS_BOUND: 6377 case TCPS_LISTEN: 6378 if (tcp->tcp_family == AF_INET6) { 6379 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6380 tcp_connect_ipv6(tcp, mp, 6381 &sin6->sin6_addr, 6382 sin6->sin6_port, sin6->sin6_flowinfo, 6383 sin6->__sin6_src_id, sin6->sin6_scope_id); 6384 return; 6385 } 6386 /* 6387 * Destination adress is mapped IPv6 address. 6388 * Source bound address should be unspecified or 6389 * IPv6 mapped address as well. 6390 */ 6391 if (!IN6_IS_ADDR_UNSPECIFIED( 6392 &tcp->tcp_bound_source_v6) && 6393 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6394 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6395 EADDRNOTAVAIL); 6396 break; 6397 } 6398 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6399 dstport = sin6->sin6_port; 6400 srcid = sin6->__sin6_src_id; 6401 } else { 6402 dstaddrp = &sin->sin_addr.s_addr; 6403 dstport = sin->sin_port; 6404 srcid = 0; 6405 } 6406 6407 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6408 return; 6409 default: 6410 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6411 break; 6412 } 6413 /* 6414 * Note: Code below is the "failure" case 6415 */ 6416 /* return error ack and blow away saved option results if any */ 6417 connect_failed: 6418 if (mp != NULL) 6419 putnext(tcp->tcp_rq, mp); 6420 else { 6421 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6422 TSYSERR, ENOMEM); 6423 } 6424 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6425 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6426 } 6427 6428 /* 6429 * Handle connect to IPv4 destinations, including connections for AF_INET6 6430 * sockets connecting to IPv4 mapped IPv6 destinations. 6431 */ 6432 static void 6433 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6434 uint_t srcid) 6435 { 6436 tcph_t *tcph; 6437 mblk_t *mp1; 6438 ipaddr_t dstaddr = *dstaddrp; 6439 int32_t oldstate; 6440 uint16_t lport; 6441 tcp_stack_t *tcps = tcp->tcp_tcps; 6442 6443 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6444 6445 /* Check for attempt to connect to INADDR_ANY */ 6446 if (dstaddr == INADDR_ANY) { 6447 /* 6448 * SunOS 4.x and 4.3 BSD allow an application 6449 * to connect a TCP socket to INADDR_ANY. 6450 * When they do this, the kernel picks the 6451 * address of one interface and uses it 6452 * instead. The kernel usually ends up 6453 * picking the address of the loopback 6454 * interface. This is an undocumented feature. 6455 * However, we provide the same thing here 6456 * in order to have source and binary 6457 * compatibility with SunOS 4.x. 6458 * Update the T_CONN_REQ (sin/sin6) since it is used to 6459 * generate the T_CONN_CON. 6460 */ 6461 dstaddr = htonl(INADDR_LOOPBACK); 6462 *dstaddrp = dstaddr; 6463 } 6464 6465 /* Handle __sin6_src_id if socket not bound to an IP address */ 6466 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6467 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6468 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6469 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6470 tcp->tcp_ipha->ipha_src); 6471 } 6472 6473 /* 6474 * Don't let an endpoint connect to itself. Note that 6475 * the test here does not catch the case where the 6476 * source IP addr was left unspecified by the user. In 6477 * this case, the source addr is set in tcp_adapt_ire() 6478 * using the reply to the T_BIND message that we send 6479 * down to IP here and the check is repeated in tcp_rput_other. 6480 */ 6481 if (dstaddr == tcp->tcp_ipha->ipha_src && 6482 dstport == tcp->tcp_lport) { 6483 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6484 goto failed; 6485 } 6486 6487 tcp->tcp_ipha->ipha_dst = dstaddr; 6488 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6489 6490 /* 6491 * Massage a source route if any putting the first hop 6492 * in iph_dst. Compute a starting value for the checksum which 6493 * takes into account that the original iph_dst should be 6494 * included in the checksum but that ip will include the 6495 * first hop in the source route in the tcp checksum. 6496 */ 6497 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6498 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6499 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6500 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6501 if ((int)tcp->tcp_sum < 0) 6502 tcp->tcp_sum--; 6503 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6504 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6505 (tcp->tcp_sum >> 16)); 6506 tcph = tcp->tcp_tcph; 6507 *(uint16_t *)tcph->th_fport = dstport; 6508 tcp->tcp_fport = dstport; 6509 6510 oldstate = tcp->tcp_state; 6511 /* 6512 * At this point the remote destination address and remote port fields 6513 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6514 * have to see which state tcp was in so we can take apropriate action. 6515 */ 6516 if (oldstate == TCPS_IDLE) { 6517 /* 6518 * We support a quick connect capability here, allowing 6519 * clients to transition directly from IDLE to SYN_SENT 6520 * tcp_bindi will pick an unused port, insert the connection 6521 * in the bind hash and transition to BOUND state. 6522 */ 6523 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6524 tcp, B_TRUE); 6525 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6526 B_FALSE, B_FALSE); 6527 if (lport == 0) { 6528 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6529 goto failed; 6530 } 6531 } 6532 tcp->tcp_state = TCPS_SYN_SENT; 6533 6534 /* 6535 * TODO: allow data with connect requests 6536 * by unlinking M_DATA trailers here and 6537 * linking them in behind the T_OK_ACK mblk. 6538 * The tcp_rput() bind ack handler would then 6539 * feed them to tcp_wput_data() rather than call 6540 * tcp_timer(). 6541 */ 6542 mp = mi_tpi_ok_ack_alloc(mp); 6543 if (!mp) { 6544 tcp->tcp_state = oldstate; 6545 goto failed; 6546 } 6547 if (tcp->tcp_family == AF_INET) { 6548 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6549 sizeof (ipa_conn_t)); 6550 } else { 6551 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6552 sizeof (ipa6_conn_t)); 6553 } 6554 if (mp1) { 6555 /* Hang onto the T_OK_ACK for later. */ 6556 linkb(mp1, mp); 6557 mblk_setcred(mp1, tcp->tcp_cred); 6558 if (tcp->tcp_family == AF_INET) 6559 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6560 else { 6561 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6562 &tcp->tcp_sticky_ipp); 6563 } 6564 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6565 tcp->tcp_active_open = 1; 6566 /* 6567 * If the bind cannot complete immediately 6568 * IP will arrange to call tcp_rput_other 6569 * when the bind completes. 6570 */ 6571 if (mp1 != NULL) 6572 tcp_rput_other(tcp, mp1); 6573 return; 6574 } 6575 /* Error case */ 6576 tcp->tcp_state = oldstate; 6577 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6578 6579 failed: 6580 /* return error ack and blow away saved option results if any */ 6581 if (mp != NULL) 6582 putnext(tcp->tcp_rq, mp); 6583 else { 6584 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6585 TSYSERR, ENOMEM); 6586 } 6587 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6588 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6589 6590 } 6591 6592 /* 6593 * Handle connect to IPv6 destinations. 6594 */ 6595 static void 6596 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6597 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6598 { 6599 tcph_t *tcph; 6600 mblk_t *mp1; 6601 ip6_rthdr_t *rth; 6602 int32_t oldstate; 6603 uint16_t lport; 6604 tcp_stack_t *tcps = tcp->tcp_tcps; 6605 6606 ASSERT(tcp->tcp_family == AF_INET6); 6607 6608 /* 6609 * If we're here, it means that the destination address is a native 6610 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6611 * reason why it might not be IPv6 is if the socket was bound to an 6612 * IPv4-mapped IPv6 address. 6613 */ 6614 if (tcp->tcp_ipversion != IPV6_VERSION) { 6615 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6616 goto failed; 6617 } 6618 6619 /* 6620 * Interpret a zero destination to mean loopback. 6621 * Update the T_CONN_REQ (sin/sin6) since it is used to 6622 * generate the T_CONN_CON. 6623 */ 6624 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6625 *dstaddrp = ipv6_loopback; 6626 } 6627 6628 /* Handle __sin6_src_id if socket not bound to an IP address */ 6629 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6630 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6631 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6632 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6633 } 6634 6635 /* 6636 * Take care of the scope_id now and add ip6i_t 6637 * if ip6i_t is not already allocated through TCP 6638 * sticky options. At this point tcp_ip6h does not 6639 * have dst info, thus use dstaddrp. 6640 */ 6641 if (scope_id != 0 && 6642 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6643 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6644 ip6i_t *ip6i; 6645 6646 ipp->ipp_ifindex = scope_id; 6647 ip6i = (ip6i_t *)tcp->tcp_iphc; 6648 6649 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6650 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6651 /* Already allocated */ 6652 ip6i->ip6i_flags |= IP6I_IFINDEX; 6653 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6654 ipp->ipp_fields |= IPPF_SCOPE_ID; 6655 } else { 6656 int reterr; 6657 6658 ipp->ipp_fields |= IPPF_SCOPE_ID; 6659 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6660 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6661 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6662 if (reterr != 0) 6663 goto failed; 6664 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6665 } 6666 } 6667 6668 /* 6669 * Don't let an endpoint connect to itself. Note that 6670 * the test here does not catch the case where the 6671 * source IP addr was left unspecified by the user. In 6672 * this case, the source addr is set in tcp_adapt_ire() 6673 * using the reply to the T_BIND message that we send 6674 * down to IP here and the check is repeated in tcp_rput_other. 6675 */ 6676 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6677 (dstport == tcp->tcp_lport)) { 6678 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6679 goto failed; 6680 } 6681 6682 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6683 tcp->tcp_remote_v6 = *dstaddrp; 6684 tcp->tcp_ip6h->ip6_vcf = 6685 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6686 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6687 6688 6689 /* 6690 * Massage a routing header (if present) putting the first hop 6691 * in ip6_dst. Compute a starting value for the checksum which 6692 * takes into account that the original ip6_dst should be 6693 * included in the checksum but that ip will include the 6694 * first hop in the source route in the tcp checksum. 6695 */ 6696 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6697 if (rth != NULL) { 6698 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6699 tcps->tcps_netstack); 6700 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6701 (tcp->tcp_sum >> 16)); 6702 } else { 6703 tcp->tcp_sum = 0; 6704 } 6705 6706 tcph = tcp->tcp_tcph; 6707 *(uint16_t *)tcph->th_fport = dstport; 6708 tcp->tcp_fport = dstport; 6709 6710 oldstate = tcp->tcp_state; 6711 /* 6712 * At this point the remote destination address and remote port fields 6713 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6714 * have to see which state tcp was in so we can take apropriate action. 6715 */ 6716 if (oldstate == TCPS_IDLE) { 6717 /* 6718 * We support a quick connect capability here, allowing 6719 * clients to transition directly from IDLE to SYN_SENT 6720 * tcp_bindi will pick an unused port, insert the connection 6721 * in the bind hash and transition to BOUND state. 6722 */ 6723 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6724 tcp, B_TRUE); 6725 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6726 B_FALSE, B_FALSE); 6727 if (lport == 0) { 6728 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6729 goto failed; 6730 } 6731 } 6732 tcp->tcp_state = TCPS_SYN_SENT; 6733 /* 6734 * TODO: allow data with connect requests 6735 * by unlinking M_DATA trailers here and 6736 * linking them in behind the T_OK_ACK mblk. 6737 * The tcp_rput() bind ack handler would then 6738 * feed them to tcp_wput_data() rather than call 6739 * tcp_timer(). 6740 */ 6741 mp = mi_tpi_ok_ack_alloc(mp); 6742 if (!mp) { 6743 tcp->tcp_state = oldstate; 6744 goto failed; 6745 } 6746 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6747 if (mp1) { 6748 /* Hang onto the T_OK_ACK for later. */ 6749 linkb(mp1, mp); 6750 mblk_setcred(mp1, tcp->tcp_cred); 6751 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6752 &tcp->tcp_sticky_ipp); 6753 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6754 tcp->tcp_active_open = 1; 6755 /* ip_bind_v6() may return ACK or ERROR */ 6756 if (mp1 != NULL) 6757 tcp_rput_other(tcp, mp1); 6758 return; 6759 } 6760 /* Error case */ 6761 tcp->tcp_state = oldstate; 6762 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6763 6764 failed: 6765 /* return error ack and blow away saved option results if any */ 6766 if (mp != NULL) 6767 putnext(tcp->tcp_rq, mp); 6768 else { 6769 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6770 TSYSERR, ENOMEM); 6771 } 6772 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6773 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6774 } 6775 6776 /* 6777 * We need a stream q for detached closing tcp connections 6778 * to use. Our client hereby indicates that this q is the 6779 * one to use. 6780 */ 6781 static void 6782 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6783 { 6784 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6785 queue_t *q = tcp->tcp_wq; 6786 tcp_stack_t *tcps = tcp->tcp_tcps; 6787 6788 #ifdef NS_DEBUG 6789 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6790 tcps->tcps_netstack->netstack_stackid); 6791 #endif 6792 mp->b_datap->db_type = M_IOCACK; 6793 iocp->ioc_count = 0; 6794 mutex_enter(&tcps->tcps_g_q_lock); 6795 if (tcps->tcps_g_q != NULL) { 6796 mutex_exit(&tcps->tcps_g_q_lock); 6797 iocp->ioc_error = EALREADY; 6798 } else { 6799 mblk_t *mp1; 6800 6801 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6802 if (mp1 == NULL) { 6803 mutex_exit(&tcps->tcps_g_q_lock); 6804 iocp->ioc_error = ENOMEM; 6805 } else { 6806 tcps->tcps_g_q = tcp->tcp_rq; 6807 mutex_exit(&tcps->tcps_g_q_lock); 6808 iocp->ioc_error = 0; 6809 iocp->ioc_rval = 0; 6810 /* 6811 * We are passing tcp_sticky_ipp as NULL 6812 * as it is not useful for tcp_default queue 6813 */ 6814 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6815 if (mp1 != NULL) 6816 tcp_rput_other(tcp, mp1); 6817 } 6818 } 6819 qreply(q, mp); 6820 } 6821 6822 /* 6823 * Our client hereby directs us to reject the connection request 6824 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6825 * of sending the appropriate RST, not an ICMP error. 6826 */ 6827 static void 6828 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6829 { 6830 tcp_t *ltcp = NULL; 6831 t_scalar_t seqnum; 6832 conn_t *connp; 6833 tcp_stack_t *tcps = tcp->tcp_tcps; 6834 6835 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6836 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6837 tcp_err_ack(tcp, mp, TPROTO, 0); 6838 return; 6839 } 6840 6841 /* 6842 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6843 * when the stream is in BOUND state. Do not send a reset, 6844 * since the destination IP address is not valid, and it can 6845 * be the initialized value of all zeros (broadcast address). 6846 * 6847 * If TCP has sent down a bind request to IP and has not 6848 * received the reply, reject the request. Otherwise, TCP 6849 * will be confused. 6850 */ 6851 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6852 if (tcp->tcp_debug) { 6853 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6854 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6855 } 6856 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6857 return; 6858 } 6859 6860 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6861 6862 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6863 6864 /* 6865 * According to TPI, for non-listeners, ignore seqnum 6866 * and disconnect. 6867 * Following interpretation of -1 seqnum is historical 6868 * and implied TPI ? (TPI only states that for T_CONN_IND, 6869 * a valid seqnum should not be -1). 6870 * 6871 * -1 means disconnect everything 6872 * regardless even on a listener. 6873 */ 6874 6875 int old_state = tcp->tcp_state; 6876 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6877 6878 /* 6879 * The connection can't be on the tcp_time_wait_head list 6880 * since it is not detached. 6881 */ 6882 ASSERT(tcp->tcp_time_wait_next == NULL); 6883 ASSERT(tcp->tcp_time_wait_prev == NULL); 6884 ASSERT(tcp->tcp_time_wait_expire == 0); 6885 ltcp = NULL; 6886 /* 6887 * If it used to be a listener, check to make sure no one else 6888 * has taken the port before switching back to LISTEN state. 6889 */ 6890 if (tcp->tcp_ipversion == IPV4_VERSION) { 6891 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6892 tcp->tcp_ipha->ipha_src, 6893 tcp->tcp_connp->conn_zoneid, ipst); 6894 if (connp != NULL) 6895 ltcp = connp->conn_tcp; 6896 } else { 6897 /* Allow tcp_bound_if listeners? */ 6898 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6899 &tcp->tcp_ip6h->ip6_src, 0, 6900 tcp->tcp_connp->conn_zoneid, ipst); 6901 if (connp != NULL) 6902 ltcp = connp->conn_tcp; 6903 } 6904 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6905 tcp->tcp_state = TCPS_LISTEN; 6906 } else if (old_state > TCPS_BOUND) { 6907 tcp->tcp_conn_req_max = 0; 6908 tcp->tcp_state = TCPS_BOUND; 6909 } 6910 if (ltcp != NULL) 6911 CONN_DEC_REF(ltcp->tcp_connp); 6912 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6913 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6914 } else if (old_state == TCPS_ESTABLISHED || 6915 old_state == TCPS_CLOSE_WAIT) { 6916 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6917 } 6918 6919 if (tcp->tcp_fused) 6920 tcp_unfuse(tcp); 6921 6922 mutex_enter(&tcp->tcp_eager_lock); 6923 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6924 (tcp->tcp_conn_req_cnt_q != 0)) { 6925 tcp_eager_cleanup(tcp, 0); 6926 } 6927 mutex_exit(&tcp->tcp_eager_lock); 6928 6929 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6930 tcp->tcp_rnxt, TH_RST | TH_ACK); 6931 6932 tcp_reinit(tcp); 6933 6934 if (old_state >= TCPS_ESTABLISHED) { 6935 /* Send M_FLUSH according to TPI */ 6936 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6937 } 6938 mp = mi_tpi_ok_ack_alloc(mp); 6939 if (mp) 6940 putnext(tcp->tcp_rq, mp); 6941 return; 6942 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6943 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6944 return; 6945 } 6946 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6947 /* Send M_FLUSH according to TPI */ 6948 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6949 } 6950 mp = mi_tpi_ok_ack_alloc(mp); 6951 if (mp) 6952 putnext(tcp->tcp_rq, mp); 6953 } 6954 6955 /* 6956 * Diagnostic routine used to return a string associated with the tcp state. 6957 * Note that if the caller does not supply a buffer, it will use an internal 6958 * static string. This means that if multiple threads call this function at 6959 * the same time, output can be corrupted... Note also that this function 6960 * does not check the size of the supplied buffer. The caller has to make 6961 * sure that it is big enough. 6962 */ 6963 static char * 6964 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6965 { 6966 char buf1[30]; 6967 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6968 char *buf; 6969 char *cp; 6970 in6_addr_t local, remote; 6971 char local_addrbuf[INET6_ADDRSTRLEN]; 6972 char remote_addrbuf[INET6_ADDRSTRLEN]; 6973 6974 if (sup_buf != NULL) 6975 buf = sup_buf; 6976 else 6977 buf = priv_buf; 6978 6979 if (tcp == NULL) 6980 return ("NULL_TCP"); 6981 switch (tcp->tcp_state) { 6982 case TCPS_CLOSED: 6983 cp = "TCP_CLOSED"; 6984 break; 6985 case TCPS_IDLE: 6986 cp = "TCP_IDLE"; 6987 break; 6988 case TCPS_BOUND: 6989 cp = "TCP_BOUND"; 6990 break; 6991 case TCPS_LISTEN: 6992 cp = "TCP_LISTEN"; 6993 break; 6994 case TCPS_SYN_SENT: 6995 cp = "TCP_SYN_SENT"; 6996 break; 6997 case TCPS_SYN_RCVD: 6998 cp = "TCP_SYN_RCVD"; 6999 break; 7000 case TCPS_ESTABLISHED: 7001 cp = "TCP_ESTABLISHED"; 7002 break; 7003 case TCPS_CLOSE_WAIT: 7004 cp = "TCP_CLOSE_WAIT"; 7005 break; 7006 case TCPS_FIN_WAIT_1: 7007 cp = "TCP_FIN_WAIT_1"; 7008 break; 7009 case TCPS_CLOSING: 7010 cp = "TCP_CLOSING"; 7011 break; 7012 case TCPS_LAST_ACK: 7013 cp = "TCP_LAST_ACK"; 7014 break; 7015 case TCPS_FIN_WAIT_2: 7016 cp = "TCP_FIN_WAIT_2"; 7017 break; 7018 case TCPS_TIME_WAIT: 7019 cp = "TCP_TIME_WAIT"; 7020 break; 7021 default: 7022 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7023 cp = buf1; 7024 break; 7025 } 7026 switch (format) { 7027 case DISP_ADDR_AND_PORT: 7028 if (tcp->tcp_ipversion == IPV4_VERSION) { 7029 /* 7030 * Note that we use the remote address in the tcp_b 7031 * structure. This means that it will print out 7032 * the real destination address, not the next hop's 7033 * address if source routing is used. 7034 */ 7035 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7036 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7037 7038 } else { 7039 local = tcp->tcp_ip_src_v6; 7040 remote = tcp->tcp_remote_v6; 7041 } 7042 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7043 sizeof (local_addrbuf)); 7044 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7045 sizeof (remote_addrbuf)); 7046 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7047 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7048 ntohs(tcp->tcp_fport), cp); 7049 break; 7050 case DISP_PORT_ONLY: 7051 default: 7052 (void) mi_sprintf(buf, "[%u, %u] %s", 7053 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7054 break; 7055 } 7056 7057 return (buf); 7058 } 7059 7060 /* 7061 * Called via squeue to get on to eager's perimeter to send a 7062 * TH_RST. The listener wants the eager to disappear either 7063 * by means of tcp_eager_blowoff() or tcp_eager_cleanup() 7064 * being called. 7065 */ 7066 /* ARGSUSED */ 7067 void 7068 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7069 { 7070 conn_t *econnp = (conn_t *)arg; 7071 tcp_t *eager = econnp->conn_tcp; 7072 tcp_t *listener = eager->tcp_listener; 7073 tcp_stack_t *tcps = eager->tcp_tcps; 7074 7075 /* 7076 * We could be called because listener is closing. Since 7077 * the eager is using listener's queue's, its not safe. 7078 * Better use the default queue just to send the TH_RST 7079 * out. 7080 */ 7081 ASSERT(tcps->tcps_g_q != NULL); 7082 eager->tcp_rq = tcps->tcps_g_q; 7083 eager->tcp_wq = WR(tcps->tcps_g_q); 7084 7085 if (eager->tcp_state > TCPS_LISTEN) { 7086 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7087 eager, eager->tcp_snxt, 0, TH_RST); 7088 } 7089 7090 /* We are here because listener wants this eager gone */ 7091 if (listener != NULL) { 7092 mutex_enter(&listener->tcp_eager_lock); 7093 tcp_eager_unlink(eager); 7094 if (eager->tcp_tconnind_started) { 7095 /* 7096 * The eager has sent a conn_ind up to the 7097 * listener but listener decides to close 7098 * instead. We need to drop the extra ref 7099 * placed on eager in tcp_rput_data() before 7100 * sending the conn_ind to listener. 7101 */ 7102 CONN_DEC_REF(econnp); 7103 } 7104 mutex_exit(&listener->tcp_eager_lock); 7105 CONN_DEC_REF(listener->tcp_connp); 7106 } 7107 7108 if (eager->tcp_state > TCPS_BOUND) 7109 tcp_close_detached(eager); 7110 } 7111 7112 /* 7113 * Reset any eager connection hanging off this listener marked 7114 * with 'seqnum' and then reclaim it's resources. 7115 */ 7116 static boolean_t 7117 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7118 { 7119 tcp_t *eager; 7120 mblk_t *mp; 7121 tcp_stack_t *tcps = listener->tcp_tcps; 7122 7123 TCP_STAT(tcps, tcp_eager_blowoff_calls); 7124 eager = listener; 7125 mutex_enter(&listener->tcp_eager_lock); 7126 do { 7127 eager = eager->tcp_eager_next_q; 7128 if (eager == NULL) { 7129 mutex_exit(&listener->tcp_eager_lock); 7130 return (B_FALSE); 7131 } 7132 } while (eager->tcp_conn_req_seqnum != seqnum); 7133 7134 if (eager->tcp_closemp_used > 0) { 7135 mutex_exit(&listener->tcp_eager_lock); 7136 return (B_TRUE); 7137 } 7138 eager->tcp_closemp_used = 1; 7139 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7140 CONN_INC_REF(eager->tcp_connp); 7141 mutex_exit(&listener->tcp_eager_lock); 7142 mp = &eager->tcp_closemp; 7143 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7144 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7145 return (B_TRUE); 7146 } 7147 7148 /* 7149 * Reset any eager connection hanging off this listener 7150 * and then reclaim it's resources. 7151 */ 7152 static void 7153 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7154 { 7155 tcp_t *eager; 7156 mblk_t *mp; 7157 tcp_stack_t *tcps = listener->tcp_tcps; 7158 7159 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7160 7161 if (!q0_only) { 7162 /* First cleanup q */ 7163 TCP_STAT(tcps, tcp_eager_blowoff_q); 7164 eager = listener->tcp_eager_next_q; 7165 while (eager != NULL) { 7166 if (eager->tcp_closemp_used == 0) { 7167 eager->tcp_closemp_used = 1; 7168 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7169 CONN_INC_REF(eager->tcp_connp); 7170 mp = &eager->tcp_closemp; 7171 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7172 tcp_eager_kill, eager->tcp_connp, 7173 SQTAG_TCP_EAGER_CLEANUP); 7174 } 7175 eager = eager->tcp_eager_next_q; 7176 } 7177 } 7178 /* Then cleanup q0 */ 7179 TCP_STAT(tcps, tcp_eager_blowoff_q0); 7180 eager = listener->tcp_eager_next_q0; 7181 while (eager != listener) { 7182 if (eager->tcp_closemp_used == 0) { 7183 eager->tcp_closemp_used = 1; 7184 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7185 CONN_INC_REF(eager->tcp_connp); 7186 mp = &eager->tcp_closemp; 7187 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7188 tcp_eager_kill, eager->tcp_connp, 7189 SQTAG_TCP_EAGER_CLEANUP_Q0); 7190 } 7191 eager = eager->tcp_eager_next_q0; 7192 } 7193 } 7194 7195 /* 7196 * If we are an eager connection hanging off a listener that hasn't 7197 * formally accepted the connection yet, get off his list and blow off 7198 * any data that we have accumulated. 7199 */ 7200 static void 7201 tcp_eager_unlink(tcp_t *tcp) 7202 { 7203 tcp_t *listener = tcp->tcp_listener; 7204 7205 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7206 ASSERT(listener != NULL); 7207 if (tcp->tcp_eager_next_q0 != NULL) { 7208 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7209 7210 /* Remove the eager tcp from q0 */ 7211 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7212 tcp->tcp_eager_prev_q0; 7213 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7214 tcp->tcp_eager_next_q0; 7215 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7216 listener->tcp_conn_req_cnt_q0--; 7217 7218 tcp->tcp_eager_next_q0 = NULL; 7219 tcp->tcp_eager_prev_q0 = NULL; 7220 7221 /* 7222 * Take the eager out, if it is in the list of droppable 7223 * eagers. 7224 */ 7225 MAKE_UNDROPPABLE(tcp); 7226 7227 if (tcp->tcp_syn_rcvd_timeout != 0) { 7228 /* we have timed out before */ 7229 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7230 listener->tcp_syn_rcvd_timeout--; 7231 } 7232 } else { 7233 tcp_t **tcpp = &listener->tcp_eager_next_q; 7234 tcp_t *prev = NULL; 7235 7236 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7237 if (tcpp[0] == tcp) { 7238 if (listener->tcp_eager_last_q == tcp) { 7239 /* 7240 * If we are unlinking the last 7241 * element on the list, adjust 7242 * tail pointer. Set tail pointer 7243 * to nil when list is empty. 7244 */ 7245 ASSERT(tcp->tcp_eager_next_q == NULL); 7246 if (listener->tcp_eager_last_q == 7247 listener->tcp_eager_next_q) { 7248 listener->tcp_eager_last_q = 7249 NULL; 7250 } else { 7251 /* 7252 * We won't get here if there 7253 * is only one eager in the 7254 * list. 7255 */ 7256 ASSERT(prev != NULL); 7257 listener->tcp_eager_last_q = 7258 prev; 7259 } 7260 } 7261 tcpp[0] = tcp->tcp_eager_next_q; 7262 tcp->tcp_eager_next_q = NULL; 7263 tcp->tcp_eager_last_q = NULL; 7264 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7265 listener->tcp_conn_req_cnt_q--; 7266 break; 7267 } 7268 prev = tcpp[0]; 7269 } 7270 } 7271 tcp->tcp_listener = NULL; 7272 } 7273 7274 /* Shorthand to generate and send TPI error acks to our client */ 7275 static void 7276 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7277 { 7278 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7279 putnext(tcp->tcp_rq, mp); 7280 } 7281 7282 /* Shorthand to generate and send TPI error acks to our client */ 7283 static void 7284 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7285 int t_error, int sys_error) 7286 { 7287 struct T_error_ack *teackp; 7288 7289 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7290 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7291 teackp = (struct T_error_ack *)mp->b_rptr; 7292 teackp->ERROR_prim = primitive; 7293 teackp->TLI_error = t_error; 7294 teackp->UNIX_error = sys_error; 7295 putnext(tcp->tcp_rq, mp); 7296 } 7297 } 7298 7299 /* 7300 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7301 * but instead the code relies on: 7302 * - the fact that the address of the array and its size never changes 7303 * - the atomic assignment of the elements of the array 7304 */ 7305 /* ARGSUSED */ 7306 static int 7307 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7308 { 7309 int i; 7310 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7311 7312 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7313 if (tcps->tcps_g_epriv_ports[i] != 0) 7314 (void) mi_mpprintf(mp, "%d ", 7315 tcps->tcps_g_epriv_ports[i]); 7316 } 7317 return (0); 7318 } 7319 7320 /* 7321 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7322 * threads from changing it at the same time. 7323 */ 7324 /* ARGSUSED */ 7325 static int 7326 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7327 cred_t *cr) 7328 { 7329 long new_value; 7330 int i; 7331 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7332 7333 /* 7334 * Fail the request if the new value does not lie within the 7335 * port number limits. 7336 */ 7337 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7338 new_value <= 0 || new_value >= 65536) { 7339 return (EINVAL); 7340 } 7341 7342 mutex_enter(&tcps->tcps_epriv_port_lock); 7343 /* Check if the value is already in the list */ 7344 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7345 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7346 mutex_exit(&tcps->tcps_epriv_port_lock); 7347 return (EEXIST); 7348 } 7349 } 7350 /* Find an empty slot */ 7351 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7352 if (tcps->tcps_g_epriv_ports[i] == 0) 7353 break; 7354 } 7355 if (i == tcps->tcps_g_num_epriv_ports) { 7356 mutex_exit(&tcps->tcps_epriv_port_lock); 7357 return (EOVERFLOW); 7358 } 7359 /* Set the new value */ 7360 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7361 mutex_exit(&tcps->tcps_epriv_port_lock); 7362 return (0); 7363 } 7364 7365 /* 7366 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7367 * threads from changing it at the same time. 7368 */ 7369 /* ARGSUSED */ 7370 static int 7371 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7372 cred_t *cr) 7373 { 7374 long new_value; 7375 int i; 7376 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7377 7378 /* 7379 * Fail the request if the new value does not lie within the 7380 * port number limits. 7381 */ 7382 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7383 new_value >= 65536) { 7384 return (EINVAL); 7385 } 7386 7387 mutex_enter(&tcps->tcps_epriv_port_lock); 7388 /* Check that the value is already in the list */ 7389 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7390 if (tcps->tcps_g_epriv_ports[i] == new_value) 7391 break; 7392 } 7393 if (i == tcps->tcps_g_num_epriv_ports) { 7394 mutex_exit(&tcps->tcps_epriv_port_lock); 7395 return (ESRCH); 7396 } 7397 /* Clear the value */ 7398 tcps->tcps_g_epriv_ports[i] = 0; 7399 mutex_exit(&tcps->tcps_epriv_port_lock); 7400 return (0); 7401 } 7402 7403 /* Return the TPI/TLI equivalent of our current tcp_state */ 7404 static int 7405 tcp_tpistate(tcp_t *tcp) 7406 { 7407 switch (tcp->tcp_state) { 7408 case TCPS_IDLE: 7409 return (TS_UNBND); 7410 case TCPS_LISTEN: 7411 /* 7412 * Return whether there are outstanding T_CONN_IND waiting 7413 * for the matching T_CONN_RES. Therefore don't count q0. 7414 */ 7415 if (tcp->tcp_conn_req_cnt_q > 0) 7416 return (TS_WRES_CIND); 7417 else 7418 return (TS_IDLE); 7419 case TCPS_BOUND: 7420 return (TS_IDLE); 7421 case TCPS_SYN_SENT: 7422 return (TS_WCON_CREQ); 7423 case TCPS_SYN_RCVD: 7424 /* 7425 * Note: assumption: this has to the active open SYN_RCVD. 7426 * The passive instance is detached in SYN_RCVD stage of 7427 * incoming connection processing so we cannot get request 7428 * for T_info_ack on it. 7429 */ 7430 return (TS_WACK_CRES); 7431 case TCPS_ESTABLISHED: 7432 return (TS_DATA_XFER); 7433 case TCPS_CLOSE_WAIT: 7434 return (TS_WREQ_ORDREL); 7435 case TCPS_FIN_WAIT_1: 7436 return (TS_WIND_ORDREL); 7437 case TCPS_FIN_WAIT_2: 7438 return (TS_WIND_ORDREL); 7439 7440 case TCPS_CLOSING: 7441 case TCPS_LAST_ACK: 7442 case TCPS_TIME_WAIT: 7443 case TCPS_CLOSED: 7444 /* 7445 * Following TS_WACK_DREQ7 is a rendition of "not 7446 * yet TS_IDLE" TPI state. There is no best match to any 7447 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7448 * choose a value chosen that will map to TLI/XTI level 7449 * state of TSTATECHNG (state is process of changing) which 7450 * captures what this dummy state represents. 7451 */ 7452 return (TS_WACK_DREQ7); 7453 default: 7454 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7455 tcp->tcp_state, tcp_display(tcp, NULL, 7456 DISP_PORT_ONLY)); 7457 return (TS_UNBND); 7458 } 7459 } 7460 7461 static void 7462 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7463 { 7464 tcp_stack_t *tcps = tcp->tcp_tcps; 7465 7466 if (tcp->tcp_family == AF_INET6) 7467 *tia = tcp_g_t_info_ack_v6; 7468 else 7469 *tia = tcp_g_t_info_ack; 7470 tia->CURRENT_state = tcp_tpistate(tcp); 7471 tia->OPT_size = tcp_max_optsize; 7472 if (tcp->tcp_mss == 0) { 7473 /* Not yet set - tcp_open does not set mss */ 7474 if (tcp->tcp_ipversion == IPV4_VERSION) 7475 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7476 else 7477 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7478 } else { 7479 tia->TIDU_size = tcp->tcp_mss; 7480 } 7481 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7482 } 7483 7484 /* 7485 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7486 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7487 * tcp_g_t_info_ack. The current state of the stream is copied from 7488 * tcp_state. 7489 */ 7490 static void 7491 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7492 { 7493 t_uscalar_t cap_bits1; 7494 struct T_capability_ack *tcap; 7495 7496 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7497 freemsg(mp); 7498 return; 7499 } 7500 7501 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7502 7503 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7504 mp->b_datap->db_type, T_CAPABILITY_ACK); 7505 if (mp == NULL) 7506 return; 7507 7508 tcap = (struct T_capability_ack *)mp->b_rptr; 7509 tcap->CAP_bits1 = 0; 7510 7511 if (cap_bits1 & TC1_INFO) { 7512 tcp_copy_info(&tcap->INFO_ack, tcp); 7513 tcap->CAP_bits1 |= TC1_INFO; 7514 } 7515 7516 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7517 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7518 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7519 } 7520 7521 putnext(tcp->tcp_rq, mp); 7522 } 7523 7524 /* 7525 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7526 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7527 * The current state of the stream is copied from tcp_state. 7528 */ 7529 static void 7530 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7531 { 7532 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7533 T_INFO_ACK); 7534 if (!mp) { 7535 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7536 return; 7537 } 7538 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7539 putnext(tcp->tcp_rq, mp); 7540 } 7541 7542 /* Respond to the TPI addr request */ 7543 static void 7544 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7545 { 7546 sin_t *sin; 7547 mblk_t *ackmp; 7548 struct T_addr_ack *taa; 7549 7550 /* Make it large enough for worst case */ 7551 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7552 2 * sizeof (sin6_t), 1); 7553 if (ackmp == NULL) { 7554 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7555 return; 7556 } 7557 7558 if (tcp->tcp_ipversion == IPV6_VERSION) { 7559 tcp_addr_req_ipv6(tcp, ackmp); 7560 return; 7561 } 7562 taa = (struct T_addr_ack *)ackmp->b_rptr; 7563 7564 bzero(taa, sizeof (struct T_addr_ack)); 7565 ackmp->b_wptr = (uchar_t *)&taa[1]; 7566 7567 taa->PRIM_type = T_ADDR_ACK; 7568 ackmp->b_datap->db_type = M_PCPROTO; 7569 7570 /* 7571 * Note: Following code assumes 32 bit alignment of basic 7572 * data structures like sin_t and struct T_addr_ack. 7573 */ 7574 if (tcp->tcp_state >= TCPS_BOUND) { 7575 /* 7576 * Fill in local address 7577 */ 7578 taa->LOCADDR_length = sizeof (sin_t); 7579 taa->LOCADDR_offset = sizeof (*taa); 7580 7581 sin = (sin_t *)&taa[1]; 7582 7583 /* Fill zeroes and then intialize non-zero fields */ 7584 *sin = sin_null; 7585 7586 sin->sin_family = AF_INET; 7587 7588 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7589 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7590 7591 ackmp->b_wptr = (uchar_t *)&sin[1]; 7592 7593 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7594 /* 7595 * Fill in Remote address 7596 */ 7597 taa->REMADDR_length = sizeof (sin_t); 7598 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7599 taa->LOCADDR_length); 7600 7601 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7602 *sin = sin_null; 7603 sin->sin_family = AF_INET; 7604 sin->sin_addr.s_addr = tcp->tcp_remote; 7605 sin->sin_port = tcp->tcp_fport; 7606 7607 ackmp->b_wptr = (uchar_t *)&sin[1]; 7608 } 7609 } 7610 putnext(tcp->tcp_rq, ackmp); 7611 } 7612 7613 /* Assumes that tcp_addr_req gets enough space and alignment */ 7614 static void 7615 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7616 { 7617 sin6_t *sin6; 7618 struct T_addr_ack *taa; 7619 7620 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7621 ASSERT(OK_32PTR(ackmp->b_rptr)); 7622 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7623 2 * sizeof (sin6_t)); 7624 7625 taa = (struct T_addr_ack *)ackmp->b_rptr; 7626 7627 bzero(taa, sizeof (struct T_addr_ack)); 7628 ackmp->b_wptr = (uchar_t *)&taa[1]; 7629 7630 taa->PRIM_type = T_ADDR_ACK; 7631 ackmp->b_datap->db_type = M_PCPROTO; 7632 7633 /* 7634 * Note: Following code assumes 32 bit alignment of basic 7635 * data structures like sin6_t and struct T_addr_ack. 7636 */ 7637 if (tcp->tcp_state >= TCPS_BOUND) { 7638 /* 7639 * Fill in local address 7640 */ 7641 taa->LOCADDR_length = sizeof (sin6_t); 7642 taa->LOCADDR_offset = sizeof (*taa); 7643 7644 sin6 = (sin6_t *)&taa[1]; 7645 *sin6 = sin6_null; 7646 7647 sin6->sin6_family = AF_INET6; 7648 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7649 sin6->sin6_port = tcp->tcp_lport; 7650 7651 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7652 7653 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7654 /* 7655 * Fill in Remote address 7656 */ 7657 taa->REMADDR_length = sizeof (sin6_t); 7658 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7659 taa->LOCADDR_length); 7660 7661 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7662 *sin6 = sin6_null; 7663 sin6->sin6_family = AF_INET6; 7664 sin6->sin6_flowinfo = 7665 tcp->tcp_ip6h->ip6_vcf & 7666 ~IPV6_VERS_AND_FLOW_MASK; 7667 sin6->sin6_addr = tcp->tcp_remote_v6; 7668 sin6->sin6_port = tcp->tcp_fport; 7669 7670 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7671 } 7672 } 7673 putnext(tcp->tcp_rq, ackmp); 7674 } 7675 7676 /* 7677 * Handle reinitialization of a tcp structure. 7678 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7679 */ 7680 static void 7681 tcp_reinit(tcp_t *tcp) 7682 { 7683 mblk_t *mp; 7684 int err; 7685 tcp_stack_t *tcps = tcp->tcp_tcps; 7686 7687 TCP_STAT(tcps, tcp_reinit_calls); 7688 7689 /* tcp_reinit should never be called for detached tcp_t's */ 7690 ASSERT(tcp->tcp_listener == NULL); 7691 ASSERT((tcp->tcp_family == AF_INET && 7692 tcp->tcp_ipversion == IPV4_VERSION) || 7693 (tcp->tcp_family == AF_INET6 && 7694 (tcp->tcp_ipversion == IPV4_VERSION || 7695 tcp->tcp_ipversion == IPV6_VERSION))); 7696 7697 /* Cancel outstanding timers */ 7698 tcp_timers_stop(tcp); 7699 7700 /* 7701 * Reset everything in the state vector, after updating global 7702 * MIB data from instance counters. 7703 */ 7704 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7705 tcp->tcp_ibsegs = 0; 7706 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7707 tcp->tcp_obsegs = 0; 7708 7709 tcp_close_mpp(&tcp->tcp_xmit_head); 7710 if (tcp->tcp_snd_zcopy_aware) 7711 tcp_zcopy_notify(tcp); 7712 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7713 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7714 mutex_enter(&tcp->tcp_non_sq_lock); 7715 if (tcp->tcp_flow_stopped && 7716 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7717 tcp_clrqfull(tcp); 7718 } 7719 mutex_exit(&tcp->tcp_non_sq_lock); 7720 tcp_close_mpp(&tcp->tcp_reass_head); 7721 tcp->tcp_reass_tail = NULL; 7722 if (tcp->tcp_rcv_list != NULL) { 7723 /* Free b_next chain */ 7724 tcp_close_mpp(&tcp->tcp_rcv_list); 7725 tcp->tcp_rcv_last_head = NULL; 7726 tcp->tcp_rcv_last_tail = NULL; 7727 tcp->tcp_rcv_cnt = 0; 7728 } 7729 tcp->tcp_rcv_last_tail = NULL; 7730 7731 if ((mp = tcp->tcp_urp_mp) != NULL) { 7732 freemsg(mp); 7733 tcp->tcp_urp_mp = NULL; 7734 } 7735 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7736 freemsg(mp); 7737 tcp->tcp_urp_mark_mp = NULL; 7738 } 7739 if (tcp->tcp_fused_sigurg_mp != NULL) { 7740 freeb(tcp->tcp_fused_sigurg_mp); 7741 tcp->tcp_fused_sigurg_mp = NULL; 7742 } 7743 7744 /* 7745 * Following is a union with two members which are 7746 * identical types and size so the following cleanup 7747 * is enough. 7748 */ 7749 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7750 7751 CL_INET_DISCONNECT(tcp); 7752 7753 /* 7754 * The connection can't be on the tcp_time_wait_head list 7755 * since it is not detached. 7756 */ 7757 ASSERT(tcp->tcp_time_wait_next == NULL); 7758 ASSERT(tcp->tcp_time_wait_prev == NULL); 7759 ASSERT(tcp->tcp_time_wait_expire == 0); 7760 7761 if (tcp->tcp_kssl_pending) { 7762 tcp->tcp_kssl_pending = B_FALSE; 7763 7764 /* Don't reset if the initialized by bind. */ 7765 if (tcp->tcp_kssl_ent != NULL) { 7766 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7767 KSSL_NO_PROXY); 7768 } 7769 } 7770 if (tcp->tcp_kssl_ctx != NULL) { 7771 kssl_release_ctx(tcp->tcp_kssl_ctx); 7772 tcp->tcp_kssl_ctx = NULL; 7773 } 7774 7775 /* 7776 * Reset/preserve other values 7777 */ 7778 tcp_reinit_values(tcp); 7779 ipcl_hash_remove(tcp->tcp_connp); 7780 conn_delete_ire(tcp->tcp_connp, NULL); 7781 tcp_ipsec_cleanup(tcp); 7782 7783 if (tcp->tcp_conn_req_max != 0) { 7784 /* 7785 * This is the case when a TLI program uses the same 7786 * transport end point to accept a connection. This 7787 * makes the TCP both a listener and acceptor. When 7788 * this connection is closed, we need to set the state 7789 * back to TCPS_LISTEN. Make sure that the eager list 7790 * is reinitialized. 7791 * 7792 * Note that this stream is still bound to the four 7793 * tuples of the previous connection in IP. If a new 7794 * SYN with different foreign address comes in, IP will 7795 * not find it and will send it to the global queue. In 7796 * the global queue, TCP will do a tcp_lookup_listener() 7797 * to find this stream. This works because this stream 7798 * is only removed from connected hash. 7799 * 7800 */ 7801 tcp->tcp_state = TCPS_LISTEN; 7802 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7803 tcp->tcp_eager_next_drop_q0 = tcp; 7804 tcp->tcp_eager_prev_drop_q0 = tcp; 7805 tcp->tcp_connp->conn_recv = tcp_conn_request; 7806 if (tcp->tcp_family == AF_INET6) { 7807 ASSERT(tcp->tcp_connp->conn_af_isv6); 7808 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7809 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7810 } else { 7811 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7812 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7813 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7814 } 7815 } else { 7816 tcp->tcp_state = TCPS_BOUND; 7817 } 7818 7819 /* 7820 * Initialize to default values 7821 * Can't fail since enough header template space already allocated 7822 * at open(). 7823 */ 7824 err = tcp_init_values(tcp); 7825 ASSERT(err == 0); 7826 /* Restore state in tcp_tcph */ 7827 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7828 if (tcp->tcp_ipversion == IPV4_VERSION) 7829 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7830 else 7831 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7832 /* 7833 * Copy of the src addr. in tcp_t is needed in tcp_t 7834 * since the lookup funcs can only lookup on tcp_t 7835 */ 7836 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7837 7838 ASSERT(tcp->tcp_ptpbhn != NULL); 7839 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7840 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7841 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7842 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7843 } 7844 7845 /* 7846 * Force values to zero that need be zero. 7847 * Do not touch values asociated with the BOUND or LISTEN state 7848 * since the connection will end up in that state after the reinit. 7849 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7850 * structure! 7851 */ 7852 static void 7853 tcp_reinit_values(tcp) 7854 tcp_t *tcp; 7855 { 7856 tcp_stack_t *tcps = tcp->tcp_tcps; 7857 7858 #ifndef lint 7859 #define DONTCARE(x) 7860 #define PRESERVE(x) 7861 #else 7862 #define DONTCARE(x) ((x) = (x)) 7863 #define PRESERVE(x) ((x) = (x)) 7864 #endif /* lint */ 7865 7866 PRESERVE(tcp->tcp_bind_hash); 7867 PRESERVE(tcp->tcp_ptpbhn); 7868 PRESERVE(tcp->tcp_acceptor_hash); 7869 PRESERVE(tcp->tcp_ptpahn); 7870 7871 /* Should be ASSERT NULL on these with new code! */ 7872 ASSERT(tcp->tcp_time_wait_next == NULL); 7873 ASSERT(tcp->tcp_time_wait_prev == NULL); 7874 ASSERT(tcp->tcp_time_wait_expire == 0); 7875 PRESERVE(tcp->tcp_state); 7876 PRESERVE(tcp->tcp_rq); 7877 PRESERVE(tcp->tcp_wq); 7878 7879 ASSERT(tcp->tcp_xmit_head == NULL); 7880 ASSERT(tcp->tcp_xmit_last == NULL); 7881 ASSERT(tcp->tcp_unsent == 0); 7882 ASSERT(tcp->tcp_xmit_tail == NULL); 7883 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7884 7885 tcp->tcp_snxt = 0; /* Displayed in mib */ 7886 tcp->tcp_suna = 0; /* Displayed in mib */ 7887 tcp->tcp_swnd = 0; 7888 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7889 7890 ASSERT(tcp->tcp_ibsegs == 0); 7891 ASSERT(tcp->tcp_obsegs == 0); 7892 7893 if (tcp->tcp_iphc != NULL) { 7894 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7895 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7896 } 7897 7898 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7899 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7900 DONTCARE(tcp->tcp_ipha); 7901 DONTCARE(tcp->tcp_ip6h); 7902 DONTCARE(tcp->tcp_ip_hdr_len); 7903 DONTCARE(tcp->tcp_tcph); 7904 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7905 tcp->tcp_valid_bits = 0; 7906 7907 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7908 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7909 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7910 tcp->tcp_last_rcv_lbolt = 0; 7911 7912 tcp->tcp_init_cwnd = 0; 7913 7914 tcp->tcp_urp_last_valid = 0; 7915 tcp->tcp_hard_binding = 0; 7916 tcp->tcp_hard_bound = 0; 7917 PRESERVE(tcp->tcp_cred); 7918 PRESERVE(tcp->tcp_cpid); 7919 PRESERVE(tcp->tcp_open_time); 7920 PRESERVE(tcp->tcp_exclbind); 7921 7922 tcp->tcp_fin_acked = 0; 7923 tcp->tcp_fin_rcvd = 0; 7924 tcp->tcp_fin_sent = 0; 7925 tcp->tcp_ordrel_done = 0; 7926 7927 tcp->tcp_debug = 0; 7928 tcp->tcp_dontroute = 0; 7929 tcp->tcp_broadcast = 0; 7930 7931 tcp->tcp_useloopback = 0; 7932 tcp->tcp_reuseaddr = 0; 7933 tcp->tcp_oobinline = 0; 7934 tcp->tcp_dgram_errind = 0; 7935 7936 tcp->tcp_detached = 0; 7937 tcp->tcp_bind_pending = 0; 7938 tcp->tcp_unbind_pending = 0; 7939 tcp->tcp_deferred_clean_death = 0; 7940 7941 tcp->tcp_snd_ws_ok = B_FALSE; 7942 tcp->tcp_snd_ts_ok = B_FALSE; 7943 tcp->tcp_linger = 0; 7944 tcp->tcp_ka_enabled = 0; 7945 tcp->tcp_zero_win_probe = 0; 7946 7947 tcp->tcp_loopback = 0; 7948 tcp->tcp_localnet = 0; 7949 tcp->tcp_syn_defense = 0; 7950 tcp->tcp_set_timer = 0; 7951 7952 tcp->tcp_active_open = 0; 7953 ASSERT(tcp->tcp_timeout == B_FALSE); 7954 tcp->tcp_rexmit = B_FALSE; 7955 tcp->tcp_xmit_zc_clean = B_FALSE; 7956 7957 tcp->tcp_snd_sack_ok = B_FALSE; 7958 PRESERVE(tcp->tcp_recvdstaddr); 7959 tcp->tcp_hwcksum = B_FALSE; 7960 7961 tcp->tcp_ire_ill_check_done = B_FALSE; 7962 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7963 7964 tcp->tcp_mdt = B_FALSE; 7965 tcp->tcp_mdt_hdr_head = 0; 7966 tcp->tcp_mdt_hdr_tail = 0; 7967 7968 tcp->tcp_conn_def_q0 = 0; 7969 tcp->tcp_ip_forward_progress = B_FALSE; 7970 tcp->tcp_anon_priv_bind = 0; 7971 tcp->tcp_ecn_ok = B_FALSE; 7972 7973 tcp->tcp_cwr = B_FALSE; 7974 tcp->tcp_ecn_echo_on = B_FALSE; 7975 7976 if (tcp->tcp_sack_info != NULL) { 7977 if (tcp->tcp_notsack_list != NULL) { 7978 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7979 } 7980 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7981 tcp->tcp_sack_info = NULL; 7982 } 7983 7984 tcp->tcp_rcv_ws = 0; 7985 tcp->tcp_snd_ws = 0; 7986 tcp->tcp_ts_recent = 0; 7987 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7988 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7989 tcp->tcp_if_mtu = 0; 7990 7991 ASSERT(tcp->tcp_reass_head == NULL); 7992 ASSERT(tcp->tcp_reass_tail == NULL); 7993 7994 tcp->tcp_cwnd_cnt = 0; 7995 7996 ASSERT(tcp->tcp_rcv_list == NULL); 7997 ASSERT(tcp->tcp_rcv_last_head == NULL); 7998 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7999 ASSERT(tcp->tcp_rcv_cnt == 0); 8000 8001 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8002 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8003 tcp->tcp_csuna = 0; 8004 8005 tcp->tcp_rto = 0; /* Displayed in MIB */ 8006 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8007 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8008 tcp->tcp_rtt_update = 0; 8009 8010 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8011 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8012 8013 tcp->tcp_rack = 0; /* Displayed in mib */ 8014 tcp->tcp_rack_cnt = 0; 8015 tcp->tcp_rack_cur_max = 0; 8016 tcp->tcp_rack_abs_max = 0; 8017 8018 tcp->tcp_max_swnd = 0; 8019 8020 ASSERT(tcp->tcp_listener == NULL); 8021 8022 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8023 8024 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8025 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8026 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8027 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8028 8029 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8030 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8031 PRESERVE(tcp->tcp_conn_req_max); 8032 PRESERVE(tcp->tcp_conn_req_seqnum); 8033 8034 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8035 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8036 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8037 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8038 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8039 8040 tcp->tcp_lingertime = 0; 8041 8042 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8043 ASSERT(tcp->tcp_urp_mp == NULL); 8044 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8045 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8046 8047 ASSERT(tcp->tcp_eager_next_q == NULL); 8048 ASSERT(tcp->tcp_eager_last_q == NULL); 8049 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8050 tcp->tcp_eager_prev_q0 == NULL) || 8051 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8052 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8053 8054 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8055 tcp->tcp_eager_prev_drop_q0 == NULL) || 8056 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8057 8058 tcp->tcp_client_errno = 0; 8059 8060 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8061 8062 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8063 8064 PRESERVE(tcp->tcp_bound_source_v6); 8065 tcp->tcp_last_sent_len = 0; 8066 tcp->tcp_dupack_cnt = 0; 8067 8068 tcp->tcp_fport = 0; /* Displayed in MIB */ 8069 PRESERVE(tcp->tcp_lport); 8070 8071 PRESERVE(tcp->tcp_acceptor_lockp); 8072 8073 ASSERT(tcp->tcp_ordrelid == 0); 8074 PRESERVE(tcp->tcp_acceptor_id); 8075 DONTCARE(tcp->tcp_ipsec_overhead); 8076 8077 /* 8078 * If tcp_tracing flag is ON (i.e. We have a trace buffer 8079 * in tcp structure and now tracing), Re-initialize all 8080 * members of tcp_traceinfo. 8081 */ 8082 if (tcp->tcp_tracebuf != NULL) { 8083 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 8084 } 8085 8086 PRESERVE(tcp->tcp_family); 8087 if (tcp->tcp_family == AF_INET6) { 8088 tcp->tcp_ipversion = IPV6_VERSION; 8089 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8090 } else { 8091 tcp->tcp_ipversion = IPV4_VERSION; 8092 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8093 } 8094 8095 tcp->tcp_bound_if = 0; 8096 tcp->tcp_ipv6_recvancillary = 0; 8097 tcp->tcp_recvifindex = 0; 8098 tcp->tcp_recvhops = 0; 8099 tcp->tcp_closed = 0; 8100 tcp->tcp_cleandeathtag = 0; 8101 if (tcp->tcp_hopopts != NULL) { 8102 mi_free(tcp->tcp_hopopts); 8103 tcp->tcp_hopopts = NULL; 8104 tcp->tcp_hopoptslen = 0; 8105 } 8106 ASSERT(tcp->tcp_hopoptslen == 0); 8107 if (tcp->tcp_dstopts != NULL) { 8108 mi_free(tcp->tcp_dstopts); 8109 tcp->tcp_dstopts = NULL; 8110 tcp->tcp_dstoptslen = 0; 8111 } 8112 ASSERT(tcp->tcp_dstoptslen == 0); 8113 if (tcp->tcp_rtdstopts != NULL) { 8114 mi_free(tcp->tcp_rtdstopts); 8115 tcp->tcp_rtdstopts = NULL; 8116 tcp->tcp_rtdstoptslen = 0; 8117 } 8118 ASSERT(tcp->tcp_rtdstoptslen == 0); 8119 if (tcp->tcp_rthdr != NULL) { 8120 mi_free(tcp->tcp_rthdr); 8121 tcp->tcp_rthdr = NULL; 8122 tcp->tcp_rthdrlen = 0; 8123 } 8124 ASSERT(tcp->tcp_rthdrlen == 0); 8125 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8126 8127 /* Reset fusion-related fields */ 8128 tcp->tcp_fused = B_FALSE; 8129 tcp->tcp_unfusable = B_FALSE; 8130 tcp->tcp_fused_sigurg = B_FALSE; 8131 tcp->tcp_direct_sockfs = B_FALSE; 8132 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8133 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8134 tcp->tcp_loopback_peer = NULL; 8135 tcp->tcp_fuse_rcv_hiwater = 0; 8136 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8137 tcp->tcp_fuse_rcv_unread_cnt = 0; 8138 8139 tcp->tcp_lso = B_FALSE; 8140 8141 tcp->tcp_in_ack_unsent = 0; 8142 tcp->tcp_cork = B_FALSE; 8143 tcp->tcp_tconnind_started = B_FALSE; 8144 8145 PRESERVE(tcp->tcp_squeue_bytes); 8146 8147 ASSERT(tcp->tcp_kssl_ctx == NULL); 8148 ASSERT(!tcp->tcp_kssl_pending); 8149 PRESERVE(tcp->tcp_kssl_ent); 8150 8151 tcp->tcp_closemp_used = 0; 8152 8153 #ifdef DEBUG 8154 DONTCARE(tcp->tcmp_stk[0]); 8155 #endif 8156 8157 8158 #undef DONTCARE 8159 #undef PRESERVE 8160 } 8161 8162 /* 8163 * Allocate necessary resources and initialize state vector. 8164 * Guaranteed not to fail so that when an error is returned, 8165 * the caller doesn't need to do any additional cleanup. 8166 */ 8167 int 8168 tcp_init(tcp_t *tcp, queue_t *q) 8169 { 8170 int err; 8171 8172 tcp->tcp_rq = q; 8173 tcp->tcp_wq = WR(q); 8174 tcp->tcp_state = TCPS_IDLE; 8175 if ((err = tcp_init_values(tcp)) != 0) 8176 tcp_timers_stop(tcp); 8177 return (err); 8178 } 8179 8180 static int 8181 tcp_init_values(tcp_t *tcp) 8182 { 8183 int err; 8184 tcp_stack_t *tcps = tcp->tcp_tcps; 8185 8186 ASSERT((tcp->tcp_family == AF_INET && 8187 tcp->tcp_ipversion == IPV4_VERSION) || 8188 (tcp->tcp_family == AF_INET6 && 8189 (tcp->tcp_ipversion == IPV4_VERSION || 8190 tcp->tcp_ipversion == IPV6_VERSION))); 8191 8192 /* 8193 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8194 * will be close to tcp_rexmit_interval_initial. By doing this, we 8195 * allow the algorithm to adjust slowly to large fluctuations of RTT 8196 * during first few transmissions of a connection as seen in slow 8197 * links. 8198 */ 8199 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8200 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8201 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8202 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8203 tcps->tcps_conn_grace_period; 8204 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8205 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8206 tcp->tcp_timer_backoff = 0; 8207 tcp->tcp_ms_we_have_waited = 0; 8208 tcp->tcp_last_recv_time = lbolt; 8209 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8210 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8211 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8212 8213 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8214 8215 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8216 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8217 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8218 /* 8219 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8220 * passive open. 8221 */ 8222 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8223 8224 tcp->tcp_naglim = tcps->tcps_naglim_def; 8225 8226 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8227 8228 tcp->tcp_mdt_hdr_head = 0; 8229 tcp->tcp_mdt_hdr_tail = 0; 8230 8231 /* Reset fusion-related fields */ 8232 tcp->tcp_fused = B_FALSE; 8233 tcp->tcp_unfusable = B_FALSE; 8234 tcp->tcp_fused_sigurg = B_FALSE; 8235 tcp->tcp_direct_sockfs = B_FALSE; 8236 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8237 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8238 tcp->tcp_loopback_peer = NULL; 8239 tcp->tcp_fuse_rcv_hiwater = 0; 8240 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8241 tcp->tcp_fuse_rcv_unread_cnt = 0; 8242 8243 /* Initialize the header template */ 8244 if (tcp->tcp_ipversion == IPV4_VERSION) { 8245 err = tcp_header_init_ipv4(tcp); 8246 } else { 8247 err = tcp_header_init_ipv6(tcp); 8248 } 8249 if (err) 8250 return (err); 8251 8252 /* 8253 * Init the window scale to the max so tcp_rwnd_set() won't pare 8254 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8255 */ 8256 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8257 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8258 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8259 8260 tcp->tcp_cork = B_FALSE; 8261 /* 8262 * Init the tcp_debug option. This value determines whether TCP 8263 * calls strlog() to print out debug messages. Doing this 8264 * initialization here means that this value is not inherited thru 8265 * tcp_reinit(). 8266 */ 8267 tcp->tcp_debug = tcps->tcps_dbg; 8268 8269 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8270 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8271 8272 return (0); 8273 } 8274 8275 /* 8276 * Initialize the IPv4 header. Loses any record of any IP options. 8277 */ 8278 static int 8279 tcp_header_init_ipv4(tcp_t *tcp) 8280 { 8281 tcph_t *tcph; 8282 uint32_t sum; 8283 conn_t *connp; 8284 tcp_stack_t *tcps = tcp->tcp_tcps; 8285 8286 /* 8287 * This is a simple initialization. If there's 8288 * already a template, it should never be too small, 8289 * so reuse it. Otherwise, allocate space for the new one. 8290 */ 8291 if (tcp->tcp_iphc == NULL) { 8292 ASSERT(tcp->tcp_iphc_len == 0); 8293 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8294 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8295 if (tcp->tcp_iphc == NULL) { 8296 tcp->tcp_iphc_len = 0; 8297 return (ENOMEM); 8298 } 8299 } 8300 8301 /* options are gone; may need a new label */ 8302 connp = tcp->tcp_connp; 8303 connp->conn_mlp_type = mlptSingle; 8304 connp->conn_ulp_labeled = !is_system_labeled(); 8305 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8306 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8307 tcp->tcp_ip6h = NULL; 8308 tcp->tcp_ipversion = IPV4_VERSION; 8309 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8310 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8311 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8312 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8313 tcp->tcp_ipha->ipha_version_and_hdr_length 8314 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8315 tcp->tcp_ipha->ipha_ident = 0; 8316 8317 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8318 tcp->tcp_tos = 0; 8319 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8320 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8321 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8322 8323 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8324 tcp->tcp_tcph = tcph; 8325 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8326 /* 8327 * IP wants our header length in the checksum field to 8328 * allow it to perform a single pseudo-header+checksum 8329 * calculation on behalf of TCP. 8330 * Include the adjustment for a source route once IP_OPTIONS is set. 8331 */ 8332 sum = sizeof (tcph_t) + tcp->tcp_sum; 8333 sum = (sum >> 16) + (sum & 0xFFFF); 8334 U16_TO_ABE16(sum, tcph->th_sum); 8335 return (0); 8336 } 8337 8338 /* 8339 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8340 */ 8341 static int 8342 tcp_header_init_ipv6(tcp_t *tcp) 8343 { 8344 tcph_t *tcph; 8345 uint32_t sum; 8346 conn_t *connp; 8347 tcp_stack_t *tcps = tcp->tcp_tcps; 8348 8349 /* 8350 * This is a simple initialization. If there's 8351 * already a template, it should never be too small, 8352 * so reuse it. Otherwise, allocate space for the new one. 8353 * Ensure that there is enough space to "downgrade" the tcp_t 8354 * to an IPv4 tcp_t. This requires having space for a full load 8355 * of IPv4 options, as well as a full load of TCP options 8356 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8357 * than a v6 header and a TCP header with a full load of TCP options 8358 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8359 * We want to avoid reallocation in the "downgraded" case when 8360 * processing outbound IPv4 options. 8361 */ 8362 if (tcp->tcp_iphc == NULL) { 8363 ASSERT(tcp->tcp_iphc_len == 0); 8364 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8365 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8366 if (tcp->tcp_iphc == NULL) { 8367 tcp->tcp_iphc_len = 0; 8368 return (ENOMEM); 8369 } 8370 } 8371 8372 /* options are gone; may need a new label */ 8373 connp = tcp->tcp_connp; 8374 connp->conn_mlp_type = mlptSingle; 8375 connp->conn_ulp_labeled = !is_system_labeled(); 8376 8377 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8378 tcp->tcp_ipversion = IPV6_VERSION; 8379 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8380 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8381 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8382 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8383 tcp->tcp_ipha = NULL; 8384 8385 /* Initialize the header template */ 8386 8387 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8388 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8389 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8390 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8391 8392 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8393 tcp->tcp_tcph = tcph; 8394 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8395 /* 8396 * IP wants our header length in the checksum field to 8397 * allow it to perform a single psuedo-header+checksum 8398 * calculation on behalf of TCP. 8399 * Include the adjustment for a source route when IPV6_RTHDR is set. 8400 */ 8401 sum = sizeof (tcph_t) + tcp->tcp_sum; 8402 sum = (sum >> 16) + (sum & 0xFFFF); 8403 U16_TO_ABE16(sum, tcph->th_sum); 8404 return (0); 8405 } 8406 8407 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8408 #define ICMP_MIN_TCP_HDR 8 8409 8410 /* 8411 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8412 * passed up by IP. The message is always received on the correct tcp_t. 8413 * Assumes that IP has pulled up everything up to and including the ICMP header. 8414 */ 8415 void 8416 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8417 { 8418 icmph_t *icmph; 8419 ipha_t *ipha; 8420 int iph_hdr_length; 8421 tcph_t *tcph; 8422 boolean_t ipsec_mctl = B_FALSE; 8423 boolean_t secure; 8424 mblk_t *first_mp = mp; 8425 uint32_t new_mss; 8426 uint32_t ratio; 8427 size_t mp_size = MBLKL(mp); 8428 uint32_t seg_seq; 8429 tcp_stack_t *tcps = tcp->tcp_tcps; 8430 8431 /* Assume IP provides aligned packets - otherwise toss */ 8432 if (!OK_32PTR(mp->b_rptr)) { 8433 freemsg(mp); 8434 return; 8435 } 8436 8437 /* 8438 * Since ICMP errors are normal data marked with M_CTL when sent 8439 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8440 * packets starting with an ipsec_info_t, see ipsec_info.h. 8441 */ 8442 if ((mp_size == sizeof (ipsec_info_t)) && 8443 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8444 ASSERT(mp->b_cont != NULL); 8445 mp = mp->b_cont; 8446 /* IP should have done this */ 8447 ASSERT(OK_32PTR(mp->b_rptr)); 8448 mp_size = MBLKL(mp); 8449 ipsec_mctl = B_TRUE; 8450 } 8451 8452 /* 8453 * Verify that we have a complete outer IP header. If not, drop it. 8454 */ 8455 if (mp_size < sizeof (ipha_t)) { 8456 noticmpv4: 8457 freemsg(first_mp); 8458 return; 8459 } 8460 8461 ipha = (ipha_t *)mp->b_rptr; 8462 /* 8463 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8464 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8465 */ 8466 switch (IPH_HDR_VERSION(ipha)) { 8467 case IPV6_VERSION: 8468 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8469 return; 8470 case IPV4_VERSION: 8471 break; 8472 default: 8473 goto noticmpv4; 8474 } 8475 8476 /* Skip past the outer IP and ICMP headers */ 8477 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8478 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8479 /* 8480 * If we don't have the correct outer IP header length or if the ULP 8481 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8482 * send it upstream. 8483 */ 8484 if (iph_hdr_length < sizeof (ipha_t) || 8485 ipha->ipha_protocol != IPPROTO_ICMP || 8486 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8487 goto noticmpv4; 8488 } 8489 ipha = (ipha_t *)&icmph[1]; 8490 8491 /* Skip past the inner IP and find the ULP header */ 8492 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8493 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8494 /* 8495 * If we don't have the correct inner IP header length or if the ULP 8496 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8497 * bytes of TCP header, drop it. 8498 */ 8499 if (iph_hdr_length < sizeof (ipha_t) || 8500 ipha->ipha_protocol != IPPROTO_TCP || 8501 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8502 goto noticmpv4; 8503 } 8504 8505 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8506 if (ipsec_mctl) { 8507 secure = ipsec_in_is_secure(first_mp); 8508 } else { 8509 secure = B_FALSE; 8510 } 8511 if (secure) { 8512 /* 8513 * If we are willing to accept this in clear 8514 * we don't have to verify policy. 8515 */ 8516 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8517 if (!tcp_check_policy(tcp, first_mp, 8518 ipha, NULL, secure, ipsec_mctl)) { 8519 /* 8520 * tcp_check_policy called 8521 * ip_drop_packet() on failure. 8522 */ 8523 return; 8524 } 8525 } 8526 } 8527 } else if (ipsec_mctl) { 8528 /* 8529 * This is a hard_bound connection. IP has already 8530 * verified policy. We don't have to do it again. 8531 */ 8532 freeb(first_mp); 8533 first_mp = mp; 8534 ipsec_mctl = B_FALSE; 8535 } 8536 8537 seg_seq = ABE32_TO_U32(tcph->th_seq); 8538 /* 8539 * TCP SHOULD check that the TCP sequence number contained in 8540 * payload of the ICMP error message is within the range 8541 * SND.UNA <= SEG.SEQ < SND.NXT. 8542 */ 8543 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8544 /* 8545 * If the ICMP message is bogus, should we kill the 8546 * connection, or should we just drop the bogus ICMP 8547 * message? It would probably make more sense to just 8548 * drop the message so that if this one managed to get 8549 * in, the real connection should not suffer. 8550 */ 8551 goto noticmpv4; 8552 } 8553 8554 switch (icmph->icmph_type) { 8555 case ICMP_DEST_UNREACHABLE: 8556 switch (icmph->icmph_code) { 8557 case ICMP_FRAGMENTATION_NEEDED: 8558 /* 8559 * Reduce the MSS based on the new MTU. This will 8560 * eliminate any fragmentation locally. 8561 * N.B. There may well be some funny side-effects on 8562 * the local send policy and the remote receive policy. 8563 * Pending further research, we provide 8564 * tcp_ignore_path_mtu just in case this proves 8565 * disastrous somewhere. 8566 * 8567 * After updating the MSS, retransmit part of the 8568 * dropped segment using the new mss by calling 8569 * tcp_wput_data(). Need to adjust all those 8570 * params to make sure tcp_wput_data() work properly. 8571 */ 8572 if (tcps->tcps_ignore_path_mtu) 8573 break; 8574 8575 /* 8576 * Decrease the MSS by time stamp options 8577 * IP options and IPSEC options. tcp_hdr_len 8578 * includes time stamp option and IP option 8579 * length. 8580 */ 8581 8582 new_mss = ntohs(icmph->icmph_du_mtu) - 8583 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8584 8585 /* 8586 * Only update the MSS if the new one is 8587 * smaller than the previous one. This is 8588 * to avoid problems when getting multiple 8589 * ICMP errors for the same MTU. 8590 */ 8591 if (new_mss >= tcp->tcp_mss) 8592 break; 8593 8594 /* 8595 * Stop doing PMTU if new_mss is less than 68 8596 * or less than tcp_mss_min. 8597 * The value 68 comes from rfc 1191. 8598 */ 8599 if (new_mss < MAX(68, tcps->tcps_mss_min)) 8600 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8601 0; 8602 8603 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8604 ASSERT(ratio >= 1); 8605 tcp_mss_set(tcp, new_mss); 8606 8607 /* 8608 * Make sure we have something to 8609 * send. 8610 */ 8611 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8612 (tcp->tcp_xmit_head != NULL)) { 8613 /* 8614 * Shrink tcp_cwnd in 8615 * proportion to the old MSS/new MSS. 8616 */ 8617 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8618 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8619 (tcp->tcp_unsent == 0)) { 8620 tcp->tcp_rexmit_max = tcp->tcp_fss; 8621 } else { 8622 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8623 } 8624 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8625 tcp->tcp_rexmit = B_TRUE; 8626 tcp->tcp_dupack_cnt = 0; 8627 tcp->tcp_snd_burst = TCP_CWND_SS; 8628 tcp_ss_rexmit(tcp); 8629 } 8630 break; 8631 case ICMP_PORT_UNREACHABLE: 8632 case ICMP_PROTOCOL_UNREACHABLE: 8633 switch (tcp->tcp_state) { 8634 case TCPS_SYN_SENT: 8635 case TCPS_SYN_RCVD: 8636 /* 8637 * ICMP can snipe away incipient 8638 * TCP connections as long as 8639 * seq number is same as initial 8640 * send seq number. 8641 */ 8642 if (seg_seq == tcp->tcp_iss) { 8643 (void) tcp_clean_death(tcp, 8644 ECONNREFUSED, 6); 8645 } 8646 break; 8647 } 8648 break; 8649 case ICMP_HOST_UNREACHABLE: 8650 case ICMP_NET_UNREACHABLE: 8651 /* Record the error in case we finally time out. */ 8652 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8653 tcp->tcp_client_errno = EHOSTUNREACH; 8654 else 8655 tcp->tcp_client_errno = ENETUNREACH; 8656 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8657 if (tcp->tcp_listener != NULL && 8658 tcp->tcp_listener->tcp_syn_defense) { 8659 /* 8660 * Ditch the half-open connection if we 8661 * suspect a SYN attack is under way. 8662 */ 8663 tcp_ip_ire_mark_advice(tcp); 8664 (void) tcp_clean_death(tcp, 8665 tcp->tcp_client_errno, 7); 8666 } 8667 } 8668 break; 8669 default: 8670 break; 8671 } 8672 break; 8673 case ICMP_SOURCE_QUENCH: { 8674 /* 8675 * use a global boolean to control 8676 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8677 * The default is false. 8678 */ 8679 if (tcp_icmp_source_quench) { 8680 /* 8681 * Reduce the sending rate as if we got a 8682 * retransmit timeout 8683 */ 8684 uint32_t npkt; 8685 8686 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8687 tcp->tcp_mss; 8688 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8689 tcp->tcp_cwnd = tcp->tcp_mss; 8690 tcp->tcp_cwnd_cnt = 0; 8691 } 8692 break; 8693 } 8694 } 8695 freemsg(first_mp); 8696 } 8697 8698 /* 8699 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8700 * error messages passed up by IP. 8701 * Assumes that IP has pulled up all the extension headers as well 8702 * as the ICMPv6 header. 8703 */ 8704 static void 8705 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8706 { 8707 icmp6_t *icmp6; 8708 ip6_t *ip6h; 8709 uint16_t iph_hdr_length; 8710 tcpha_t *tcpha; 8711 uint8_t *nexthdrp; 8712 uint32_t new_mss; 8713 uint32_t ratio; 8714 boolean_t secure; 8715 mblk_t *first_mp = mp; 8716 size_t mp_size; 8717 uint32_t seg_seq; 8718 tcp_stack_t *tcps = tcp->tcp_tcps; 8719 8720 /* 8721 * The caller has determined if this is an IPSEC_IN packet and 8722 * set ipsec_mctl appropriately (see tcp_icmp_error). 8723 */ 8724 if (ipsec_mctl) 8725 mp = mp->b_cont; 8726 8727 mp_size = MBLKL(mp); 8728 8729 /* 8730 * Verify that we have a complete IP header. If not, send it upstream. 8731 */ 8732 if (mp_size < sizeof (ip6_t)) { 8733 noticmpv6: 8734 freemsg(first_mp); 8735 return; 8736 } 8737 8738 /* 8739 * Verify this is an ICMPV6 packet, else send it upstream. 8740 */ 8741 ip6h = (ip6_t *)mp->b_rptr; 8742 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8743 iph_hdr_length = IPV6_HDR_LEN; 8744 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8745 &nexthdrp) || 8746 *nexthdrp != IPPROTO_ICMPV6) { 8747 goto noticmpv6; 8748 } 8749 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8750 ip6h = (ip6_t *)&icmp6[1]; 8751 /* 8752 * Verify if we have a complete ICMP and inner IP header. 8753 */ 8754 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8755 goto noticmpv6; 8756 8757 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8758 goto noticmpv6; 8759 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8760 /* 8761 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8762 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8763 * packet. 8764 */ 8765 if ((*nexthdrp != IPPROTO_TCP) || 8766 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8767 goto noticmpv6; 8768 } 8769 8770 /* 8771 * ICMP errors come on the right queue or come on 8772 * listener/global queue for detached connections and 8773 * get switched to the right queue. If it comes on the 8774 * right queue, policy check has already been done by IP 8775 * and thus free the first_mp without verifying the policy. 8776 * If it has come for a non-hard bound connection, we need 8777 * to verify policy as IP may not have done it. 8778 */ 8779 if (!tcp->tcp_hard_bound) { 8780 if (ipsec_mctl) { 8781 secure = ipsec_in_is_secure(first_mp); 8782 } else { 8783 secure = B_FALSE; 8784 } 8785 if (secure) { 8786 /* 8787 * If we are willing to accept this in clear 8788 * we don't have to verify policy. 8789 */ 8790 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8791 if (!tcp_check_policy(tcp, first_mp, 8792 NULL, ip6h, secure, ipsec_mctl)) { 8793 /* 8794 * tcp_check_policy called 8795 * ip_drop_packet() on failure. 8796 */ 8797 return; 8798 } 8799 } 8800 } 8801 } else if (ipsec_mctl) { 8802 /* 8803 * This is a hard_bound connection. IP has already 8804 * verified policy. We don't have to do it again. 8805 */ 8806 freeb(first_mp); 8807 first_mp = mp; 8808 ipsec_mctl = B_FALSE; 8809 } 8810 8811 seg_seq = ntohl(tcpha->tha_seq); 8812 /* 8813 * TCP SHOULD check that the TCP sequence number contained in 8814 * payload of the ICMP error message is within the range 8815 * SND.UNA <= SEG.SEQ < SND.NXT. 8816 */ 8817 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8818 /* 8819 * If the ICMP message is bogus, should we kill the 8820 * connection, or should we just drop the bogus ICMP 8821 * message? It would probably make more sense to just 8822 * drop the message so that if this one managed to get 8823 * in, the real connection should not suffer. 8824 */ 8825 goto noticmpv6; 8826 } 8827 8828 switch (icmp6->icmp6_type) { 8829 case ICMP6_PACKET_TOO_BIG: 8830 /* 8831 * Reduce the MSS based on the new MTU. This will 8832 * eliminate any fragmentation locally. 8833 * N.B. There may well be some funny side-effects on 8834 * the local send policy and the remote receive policy. 8835 * Pending further research, we provide 8836 * tcp_ignore_path_mtu just in case this proves 8837 * disastrous somewhere. 8838 * 8839 * After updating the MSS, retransmit part of the 8840 * dropped segment using the new mss by calling 8841 * tcp_wput_data(). Need to adjust all those 8842 * params to make sure tcp_wput_data() work properly. 8843 */ 8844 if (tcps->tcps_ignore_path_mtu) 8845 break; 8846 8847 /* 8848 * Decrease the MSS by time stamp options 8849 * IP options and IPSEC options. tcp_hdr_len 8850 * includes time stamp option and IP option 8851 * length. 8852 */ 8853 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8854 tcp->tcp_ipsec_overhead; 8855 8856 /* 8857 * Only update the MSS if the new one is 8858 * smaller than the previous one. This is 8859 * to avoid problems when getting multiple 8860 * ICMP errors for the same MTU. 8861 */ 8862 if (new_mss >= tcp->tcp_mss) 8863 break; 8864 8865 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8866 ASSERT(ratio >= 1); 8867 tcp_mss_set(tcp, new_mss); 8868 8869 /* 8870 * Make sure we have something to 8871 * send. 8872 */ 8873 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8874 (tcp->tcp_xmit_head != NULL)) { 8875 /* 8876 * Shrink tcp_cwnd in 8877 * proportion to the old MSS/new MSS. 8878 */ 8879 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8880 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8881 (tcp->tcp_unsent == 0)) { 8882 tcp->tcp_rexmit_max = tcp->tcp_fss; 8883 } else { 8884 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8885 } 8886 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8887 tcp->tcp_rexmit = B_TRUE; 8888 tcp->tcp_dupack_cnt = 0; 8889 tcp->tcp_snd_burst = TCP_CWND_SS; 8890 tcp_ss_rexmit(tcp); 8891 } 8892 break; 8893 8894 case ICMP6_DST_UNREACH: 8895 switch (icmp6->icmp6_code) { 8896 case ICMP6_DST_UNREACH_NOPORT: 8897 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8898 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8899 (seg_seq == tcp->tcp_iss)) { 8900 (void) tcp_clean_death(tcp, 8901 ECONNREFUSED, 8); 8902 } 8903 break; 8904 8905 case ICMP6_DST_UNREACH_ADMIN: 8906 case ICMP6_DST_UNREACH_NOROUTE: 8907 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8908 case ICMP6_DST_UNREACH_ADDR: 8909 /* Record the error in case we finally time out. */ 8910 tcp->tcp_client_errno = EHOSTUNREACH; 8911 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8912 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8913 (seg_seq == tcp->tcp_iss)) { 8914 if (tcp->tcp_listener != NULL && 8915 tcp->tcp_listener->tcp_syn_defense) { 8916 /* 8917 * Ditch the half-open connection if we 8918 * suspect a SYN attack is under way. 8919 */ 8920 tcp_ip_ire_mark_advice(tcp); 8921 (void) tcp_clean_death(tcp, 8922 tcp->tcp_client_errno, 9); 8923 } 8924 } 8925 8926 8927 break; 8928 default: 8929 break; 8930 } 8931 break; 8932 8933 case ICMP6_PARAM_PROB: 8934 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8935 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8936 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8937 (uchar_t *)nexthdrp) { 8938 if (tcp->tcp_state == TCPS_SYN_SENT || 8939 tcp->tcp_state == TCPS_SYN_RCVD) { 8940 (void) tcp_clean_death(tcp, 8941 ECONNREFUSED, 10); 8942 } 8943 break; 8944 } 8945 break; 8946 8947 case ICMP6_TIME_EXCEEDED: 8948 default: 8949 break; 8950 } 8951 freemsg(first_mp); 8952 } 8953 8954 /* 8955 * IP recognizes seven kinds of bind requests: 8956 * 8957 * - A zero-length address binds only to the protocol number. 8958 * 8959 * - A 4-byte address is treated as a request to 8960 * validate that the address is a valid local IPv4 8961 * address, appropriate for an application to bind to. 8962 * IP does the verification, but does not make any note 8963 * of the address at this time. 8964 * 8965 * - A 16-byte address contains is treated as a request 8966 * to validate a local IPv6 address, as the 4-byte 8967 * address case above. 8968 * 8969 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 8970 * use it for the inbound fanout of packets. 8971 * 8972 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 8973 * use it for the inbound fanout of packets. 8974 * 8975 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 8976 * information consisting of local and remote addresses 8977 * and ports. In this case, the addresses are both 8978 * validated as appropriate for this operation, and, if 8979 * so, the information is retained for use in the 8980 * inbound fanout. 8981 * 8982 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 8983 * fanout information, like the 12-byte case above. 8984 * 8985 * IP will also fill in the IRE request mblk with information 8986 * regarding our peer. In all cases, we notify IP of our protocol 8987 * type by appending a single protocol byte to the bind request. 8988 */ 8989 static mblk_t * 8990 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 8991 { 8992 char *cp; 8993 mblk_t *mp; 8994 struct T_bind_req *tbr; 8995 ipa_conn_t *ac; 8996 ipa6_conn_t *ac6; 8997 sin_t *sin; 8998 sin6_t *sin6; 8999 9000 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9001 ASSERT((tcp->tcp_family == AF_INET && 9002 tcp->tcp_ipversion == IPV4_VERSION) || 9003 (tcp->tcp_family == AF_INET6 && 9004 (tcp->tcp_ipversion == IPV4_VERSION || 9005 tcp->tcp_ipversion == IPV6_VERSION))); 9006 9007 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9008 if (!mp) 9009 return (mp); 9010 mp->b_datap->db_type = M_PROTO; 9011 tbr = (struct T_bind_req *)mp->b_rptr; 9012 tbr->PRIM_type = bind_prim; 9013 tbr->ADDR_offset = sizeof (*tbr); 9014 tbr->CONIND_number = 0; 9015 tbr->ADDR_length = addr_length; 9016 cp = (char *)&tbr[1]; 9017 switch (addr_length) { 9018 case sizeof (ipa_conn_t): 9019 ASSERT(tcp->tcp_family == AF_INET); 9020 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9021 9022 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9023 if (mp->b_cont == NULL) { 9024 freemsg(mp); 9025 return (NULL); 9026 } 9027 mp->b_cont->b_wptr += sizeof (ire_t); 9028 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9029 9030 /* cp known to be 32 bit aligned */ 9031 ac = (ipa_conn_t *)cp; 9032 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9033 ac->ac_faddr = tcp->tcp_remote; 9034 ac->ac_fport = tcp->tcp_fport; 9035 ac->ac_lport = tcp->tcp_lport; 9036 tcp->tcp_hard_binding = 1; 9037 break; 9038 9039 case sizeof (ipa6_conn_t): 9040 ASSERT(tcp->tcp_family == AF_INET6); 9041 9042 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9043 if (mp->b_cont == NULL) { 9044 freemsg(mp); 9045 return (NULL); 9046 } 9047 mp->b_cont->b_wptr += sizeof (ire_t); 9048 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9049 9050 /* cp known to be 32 bit aligned */ 9051 ac6 = (ipa6_conn_t *)cp; 9052 if (tcp->tcp_ipversion == IPV4_VERSION) { 9053 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9054 &ac6->ac6_laddr); 9055 } else { 9056 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9057 } 9058 ac6->ac6_faddr = tcp->tcp_remote_v6; 9059 ac6->ac6_fport = tcp->tcp_fport; 9060 ac6->ac6_lport = tcp->tcp_lport; 9061 tcp->tcp_hard_binding = 1; 9062 break; 9063 9064 case sizeof (sin_t): 9065 /* 9066 * NOTE: IPV6_ADDR_LEN also has same size. 9067 * Use family to discriminate. 9068 */ 9069 if (tcp->tcp_family == AF_INET) { 9070 sin = (sin_t *)cp; 9071 9072 *sin = sin_null; 9073 sin->sin_family = AF_INET; 9074 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9075 sin->sin_port = tcp->tcp_lport; 9076 break; 9077 } else { 9078 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9079 } 9080 break; 9081 9082 case sizeof (sin6_t): 9083 ASSERT(tcp->tcp_family == AF_INET6); 9084 sin6 = (sin6_t *)cp; 9085 9086 *sin6 = sin6_null; 9087 sin6->sin6_family = AF_INET6; 9088 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9089 sin6->sin6_port = tcp->tcp_lport; 9090 break; 9091 9092 case IP_ADDR_LEN: 9093 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9094 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9095 break; 9096 9097 } 9098 /* Add protocol number to end */ 9099 cp[addr_length] = (char)IPPROTO_TCP; 9100 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9101 return (mp); 9102 } 9103 9104 /* 9105 * Notify IP that we are having trouble with this connection. IP should 9106 * blow the IRE away and start over. 9107 */ 9108 static void 9109 tcp_ip_notify(tcp_t *tcp) 9110 { 9111 struct iocblk *iocp; 9112 ipid_t *ipid; 9113 mblk_t *mp; 9114 9115 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9116 if (tcp->tcp_ipversion == IPV6_VERSION) 9117 return; 9118 9119 mp = mkiocb(IP_IOCTL); 9120 if (mp == NULL) 9121 return; 9122 9123 iocp = (struct iocblk *)mp->b_rptr; 9124 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9125 9126 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9127 if (!mp->b_cont) { 9128 freeb(mp); 9129 return; 9130 } 9131 9132 ipid = (ipid_t *)mp->b_cont->b_rptr; 9133 mp->b_cont->b_wptr += iocp->ioc_count; 9134 bzero(ipid, sizeof (*ipid)); 9135 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9136 ipid->ipid_ire_type = IRE_CACHE; 9137 ipid->ipid_addr_offset = sizeof (ipid_t); 9138 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9139 /* 9140 * Note: in the case of source routing we want to blow away the 9141 * route to the first source route hop. 9142 */ 9143 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9144 sizeof (tcp->tcp_ipha->ipha_dst)); 9145 9146 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9147 } 9148 9149 /* Unlink and return any mblk that looks like it contains an ire */ 9150 static mblk_t * 9151 tcp_ire_mp(mblk_t *mp) 9152 { 9153 mblk_t *prev_mp; 9154 9155 for (;;) { 9156 prev_mp = mp; 9157 mp = mp->b_cont; 9158 if (mp == NULL) 9159 break; 9160 switch (DB_TYPE(mp)) { 9161 case IRE_DB_TYPE: 9162 case IRE_DB_REQ_TYPE: 9163 if (prev_mp != NULL) 9164 prev_mp->b_cont = mp->b_cont; 9165 mp->b_cont = NULL; 9166 return (mp); 9167 default: 9168 break; 9169 } 9170 } 9171 return (mp); 9172 } 9173 9174 /* 9175 * Timer callback routine for keepalive probe. We do a fake resend of 9176 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9177 * check to see if we have heard anything from the other end for the last 9178 * RTO period. If we have, set the timer to expire for another 9179 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9180 * RTO << 1 and check again when it expires. Keep exponentially increasing 9181 * the timeout if we have not heard from the other side. If for more than 9182 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9183 * kill the connection unless the keepalive abort threshold is 0. In 9184 * that case, we will probe "forever." 9185 */ 9186 static void 9187 tcp_keepalive_killer(void *arg) 9188 { 9189 mblk_t *mp; 9190 conn_t *connp = (conn_t *)arg; 9191 tcp_t *tcp = connp->conn_tcp; 9192 int32_t firetime; 9193 int32_t idletime; 9194 int32_t ka_intrvl; 9195 tcp_stack_t *tcps = tcp->tcp_tcps; 9196 9197 tcp->tcp_ka_tid = 0; 9198 9199 if (tcp->tcp_fused) 9200 return; 9201 9202 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9203 ka_intrvl = tcp->tcp_ka_interval; 9204 9205 /* 9206 * Keepalive probe should only be sent if the application has not 9207 * done a close on the connection. 9208 */ 9209 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9210 return; 9211 } 9212 /* Timer fired too early, restart it. */ 9213 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9214 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9215 MSEC_TO_TICK(ka_intrvl)); 9216 return; 9217 } 9218 9219 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9220 /* 9221 * If we have not heard from the other side for a long 9222 * time, kill the connection unless the keepalive abort 9223 * threshold is 0. In that case, we will probe "forever." 9224 */ 9225 if (tcp->tcp_ka_abort_thres != 0 && 9226 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9227 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9228 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9229 tcp->tcp_client_errno : ETIMEDOUT, 11); 9230 return; 9231 } 9232 9233 if (tcp->tcp_snxt == tcp->tcp_suna && 9234 idletime >= ka_intrvl) { 9235 /* Fake resend of last ACKed byte. */ 9236 mblk_t *mp1 = allocb(1, BPRI_LO); 9237 9238 if (mp1 != NULL) { 9239 *mp1->b_wptr++ = '\0'; 9240 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9241 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9242 freeb(mp1); 9243 /* 9244 * if allocation failed, fall through to start the 9245 * timer back. 9246 */ 9247 if (mp != NULL) { 9248 TCP_RECORD_TRACE(tcp, mp, 9249 TCP_TRACE_SEND_PKT); 9250 tcp_send_data(tcp, tcp->tcp_wq, mp); 9251 BUMP_MIB(&tcps->tcps_mib, 9252 tcpTimKeepaliveProbe); 9253 if (tcp->tcp_ka_last_intrvl != 0) { 9254 int max; 9255 /* 9256 * We should probe again at least 9257 * in ka_intrvl, but not more than 9258 * tcp_rexmit_interval_max. 9259 */ 9260 max = tcps->tcps_rexmit_interval_max; 9261 firetime = MIN(ka_intrvl - 1, 9262 tcp->tcp_ka_last_intrvl << 1); 9263 if (firetime > max) 9264 firetime = max; 9265 } else { 9266 firetime = tcp->tcp_rto; 9267 } 9268 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9269 tcp_keepalive_killer, 9270 MSEC_TO_TICK(firetime)); 9271 tcp->tcp_ka_last_intrvl = firetime; 9272 return; 9273 } 9274 } 9275 } else { 9276 tcp->tcp_ka_last_intrvl = 0; 9277 } 9278 9279 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9280 if ((firetime = ka_intrvl - idletime) < 0) { 9281 firetime = ka_intrvl; 9282 } 9283 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9284 MSEC_TO_TICK(firetime)); 9285 } 9286 9287 int 9288 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9289 { 9290 queue_t *q = tcp->tcp_rq; 9291 int32_t mss = tcp->tcp_mss; 9292 int maxpsz; 9293 9294 if (TCP_IS_DETACHED(tcp)) 9295 return (mss); 9296 9297 if (tcp->tcp_fused) { 9298 maxpsz = tcp_fuse_maxpsz_set(tcp); 9299 mss = INFPSZ; 9300 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9301 /* 9302 * Set the sd_qn_maxpsz according to the socket send buffer 9303 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9304 * instruct the stream head to copyin user data into contiguous 9305 * kernel-allocated buffers without breaking it up into smaller 9306 * chunks. We round up the buffer size to the nearest SMSS. 9307 */ 9308 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9309 if (tcp->tcp_kssl_ctx == NULL) 9310 mss = INFPSZ; 9311 else 9312 mss = SSL3_MAX_RECORD_LEN; 9313 } else { 9314 /* 9315 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9316 * (and a multiple of the mss). This instructs the stream 9317 * head to break down larger than SMSS writes into SMSS- 9318 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9319 */ 9320 maxpsz = tcp->tcp_maxpsz * mss; 9321 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9322 maxpsz = tcp->tcp_xmit_hiwater/2; 9323 /* Round up to nearest mss */ 9324 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9325 } 9326 } 9327 (void) setmaxps(q, maxpsz); 9328 tcp->tcp_wq->q_maxpsz = maxpsz; 9329 9330 if (set_maxblk) 9331 (void) mi_set_sth_maxblk(q, mss); 9332 9333 return (mss); 9334 } 9335 9336 /* 9337 * Extract option values from a tcp header. We put any found values into the 9338 * tcpopt struct and return a bitmask saying which options were found. 9339 */ 9340 static int 9341 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9342 { 9343 uchar_t *endp; 9344 int len; 9345 uint32_t mss; 9346 uchar_t *up = (uchar_t *)tcph; 9347 int found = 0; 9348 int32_t sack_len; 9349 tcp_seq sack_begin, sack_end; 9350 tcp_t *tcp; 9351 9352 endp = up + TCP_HDR_LENGTH(tcph); 9353 up += TCP_MIN_HEADER_LENGTH; 9354 while (up < endp) { 9355 len = endp - up; 9356 switch (*up) { 9357 case TCPOPT_EOL: 9358 break; 9359 9360 case TCPOPT_NOP: 9361 up++; 9362 continue; 9363 9364 case TCPOPT_MAXSEG: 9365 if (len < TCPOPT_MAXSEG_LEN || 9366 up[1] != TCPOPT_MAXSEG_LEN) 9367 break; 9368 9369 mss = BE16_TO_U16(up+2); 9370 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9371 tcpopt->tcp_opt_mss = mss; 9372 found |= TCP_OPT_MSS_PRESENT; 9373 9374 up += TCPOPT_MAXSEG_LEN; 9375 continue; 9376 9377 case TCPOPT_WSCALE: 9378 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9379 break; 9380 9381 if (up[2] > TCP_MAX_WINSHIFT) 9382 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9383 else 9384 tcpopt->tcp_opt_wscale = up[2]; 9385 found |= TCP_OPT_WSCALE_PRESENT; 9386 9387 up += TCPOPT_WS_LEN; 9388 continue; 9389 9390 case TCPOPT_SACK_PERMITTED: 9391 if (len < TCPOPT_SACK_OK_LEN || 9392 up[1] != TCPOPT_SACK_OK_LEN) 9393 break; 9394 found |= TCP_OPT_SACK_OK_PRESENT; 9395 up += TCPOPT_SACK_OK_LEN; 9396 continue; 9397 9398 case TCPOPT_SACK: 9399 if (len <= 2 || up[1] <= 2 || len < up[1]) 9400 break; 9401 9402 /* If TCP is not interested in SACK blks... */ 9403 if ((tcp = tcpopt->tcp) == NULL) { 9404 up += up[1]; 9405 continue; 9406 } 9407 sack_len = up[1] - TCPOPT_HEADER_LEN; 9408 up += TCPOPT_HEADER_LEN; 9409 9410 /* 9411 * If the list is empty, allocate one and assume 9412 * nothing is sack'ed. 9413 */ 9414 ASSERT(tcp->tcp_sack_info != NULL); 9415 if (tcp->tcp_notsack_list == NULL) { 9416 tcp_notsack_update(&(tcp->tcp_notsack_list), 9417 tcp->tcp_suna, tcp->tcp_snxt, 9418 &(tcp->tcp_num_notsack_blk), 9419 &(tcp->tcp_cnt_notsack_list)); 9420 9421 /* 9422 * Make sure tcp_notsack_list is not NULL. 9423 * This happens when kmem_alloc(KM_NOSLEEP) 9424 * returns NULL. 9425 */ 9426 if (tcp->tcp_notsack_list == NULL) { 9427 up += sack_len; 9428 continue; 9429 } 9430 tcp->tcp_fack = tcp->tcp_suna; 9431 } 9432 9433 while (sack_len > 0) { 9434 if (up + 8 > endp) { 9435 up = endp; 9436 break; 9437 } 9438 sack_begin = BE32_TO_U32(up); 9439 up += 4; 9440 sack_end = BE32_TO_U32(up); 9441 up += 4; 9442 sack_len -= 8; 9443 /* 9444 * Bounds checking. Make sure the SACK 9445 * info is within tcp_suna and tcp_snxt. 9446 * If this SACK blk is out of bound, ignore 9447 * it but continue to parse the following 9448 * blks. 9449 */ 9450 if (SEQ_LEQ(sack_end, sack_begin) || 9451 SEQ_LT(sack_begin, tcp->tcp_suna) || 9452 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9453 continue; 9454 } 9455 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9456 sack_begin, sack_end, 9457 &(tcp->tcp_num_notsack_blk), 9458 &(tcp->tcp_cnt_notsack_list)); 9459 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9460 tcp->tcp_fack = sack_end; 9461 } 9462 } 9463 found |= TCP_OPT_SACK_PRESENT; 9464 continue; 9465 9466 case TCPOPT_TSTAMP: 9467 if (len < TCPOPT_TSTAMP_LEN || 9468 up[1] != TCPOPT_TSTAMP_LEN) 9469 break; 9470 9471 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9472 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9473 9474 found |= TCP_OPT_TSTAMP_PRESENT; 9475 9476 up += TCPOPT_TSTAMP_LEN; 9477 continue; 9478 9479 default: 9480 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9481 break; 9482 up += up[1]; 9483 continue; 9484 } 9485 break; 9486 } 9487 return (found); 9488 } 9489 9490 /* 9491 * Set the mss associated with a particular tcp based on its current value, 9492 * and a new one passed in. Observe minimums and maximums, and reset 9493 * other state variables that we want to view as multiples of mss. 9494 * 9495 * This function is called in various places mainly because 9496 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9497 * other side's SYN/SYN-ACK packet arrives. 9498 * 2) PMTUd may get us a new MSS. 9499 * 3) If the other side stops sending us timestamp option, we need to 9500 * increase the MSS size to use the extra bytes available. 9501 */ 9502 static void 9503 tcp_mss_set(tcp_t *tcp, uint32_t mss) 9504 { 9505 uint32_t mss_max; 9506 tcp_stack_t *tcps = tcp->tcp_tcps; 9507 9508 if (tcp->tcp_ipversion == IPV4_VERSION) 9509 mss_max = tcps->tcps_mss_max_ipv4; 9510 else 9511 mss_max = tcps->tcps_mss_max_ipv6; 9512 9513 if (mss < tcps->tcps_mss_min) 9514 mss = tcps->tcps_mss_min; 9515 if (mss > mss_max) 9516 mss = mss_max; 9517 /* 9518 * Unless naglim has been set by our client to 9519 * a non-mss value, force naglim to track mss. 9520 * This can help to aggregate small writes. 9521 */ 9522 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9523 tcp->tcp_naglim = mss; 9524 /* 9525 * TCP should be able to buffer at least 4 MSS data for obvious 9526 * performance reason. 9527 */ 9528 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9529 tcp->tcp_xmit_hiwater = mss << 2; 9530 9531 /* 9532 * Check if we need to apply the tcp_init_cwnd here. If 9533 * it is set and the MSS gets bigger (should not happen 9534 * normally), we need to adjust the resulting tcp_cwnd properly. 9535 * The new tcp_cwnd should not get bigger. 9536 */ 9537 if (tcp->tcp_init_cwnd == 0) { 9538 tcp->tcp_cwnd = MIN(tcps->tcps_slow_start_initial * mss, 9539 MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9540 } else { 9541 if (tcp->tcp_mss < mss) { 9542 tcp->tcp_cwnd = MAX(1, 9543 (tcp->tcp_init_cwnd * tcp->tcp_mss / mss)) * mss; 9544 } else { 9545 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9546 } 9547 } 9548 tcp->tcp_mss = mss; 9549 tcp->tcp_cwnd_cnt = 0; 9550 (void) tcp_maxpsz_set(tcp, B_TRUE); 9551 } 9552 9553 static int 9554 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9555 { 9556 tcp_t *tcp = NULL; 9557 conn_t *connp; 9558 int err; 9559 dev_t conn_dev; 9560 zoneid_t zoneid; 9561 tcp_stack_t *tcps = NULL; 9562 9563 if (q->q_ptr != NULL) 9564 return (0); 9565 9566 if (!(flag & SO_ACCEPTOR)) { 9567 /* 9568 * Special case for install: miniroot needs to be able to 9569 * access files via NFS as though it were always in the 9570 * global zone. 9571 */ 9572 if (credp == kcred && nfs_global_client_only != 0) { 9573 zoneid = GLOBAL_ZONEID; 9574 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9575 netstack_tcp; 9576 ASSERT(tcps != NULL); 9577 } else { 9578 netstack_t *ns; 9579 9580 ns = netstack_find_by_cred(credp); 9581 ASSERT(ns != NULL); 9582 tcps = ns->netstack_tcp; 9583 ASSERT(tcps != NULL); 9584 9585 /* 9586 * For exclusive stacks we set the zoneid to zero 9587 * to make TCP operate as if in the global zone. 9588 */ 9589 if (tcps->tcps_netstack->netstack_stackid != 9590 GLOBAL_NETSTACKID) 9591 zoneid = GLOBAL_ZONEID; 9592 else 9593 zoneid = crgetzoneid(credp); 9594 } 9595 /* 9596 * For stackid zero this is done from strplumb.c, but 9597 * non-zero stackids are handled here. 9598 */ 9599 if (tcps->tcps_g_q == NULL && 9600 tcps->tcps_netstack->netstack_stackid != 9601 GLOBAL_NETSTACKID) { 9602 tcp_g_q_setup(tcps); 9603 } 9604 } 9605 if (sflag == MODOPEN) { 9606 /* 9607 * This is a special case. The purpose of a modopen 9608 * is to allow just the T_SVR4_OPTMGMT_REQ to pass 9609 * through for MIB browsers. Everything else is failed. 9610 */ 9611 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9612 /* tcp_get_conn incremented refcnt */ 9613 netstack_rele(tcps->tcps_netstack); 9614 9615 if (connp == NULL) 9616 return (ENOMEM); 9617 9618 connp->conn_flags |= IPCL_TCPMOD; 9619 connp->conn_cred = credp; 9620 connp->conn_zoneid = zoneid; 9621 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9622 ASSERT(connp->conn_netstack->netstack_tcp == tcps); 9623 q->q_ptr = WR(q)->q_ptr = connp; 9624 crhold(credp); 9625 q->q_qinfo = &tcp_mod_rinit; 9626 WR(q)->q_qinfo = &tcp_mod_winit; 9627 qprocson(q); 9628 return (0); 9629 } 9630 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 9631 if (tcps != NULL) 9632 netstack_rele(tcps->tcps_netstack); 9633 return (EBUSY); 9634 } 9635 9636 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9637 9638 if (flag & SO_ACCEPTOR) { 9639 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9640 ASSERT(tcps == NULL); 9641 q->q_qinfo = &tcp_acceptor_rinit; 9642 q->q_ptr = (void *)conn_dev; 9643 WR(q)->q_qinfo = &tcp_acceptor_winit; 9644 WR(q)->q_ptr = (void *)conn_dev; 9645 qprocson(q); 9646 return (0); 9647 } 9648 9649 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9650 /* 9651 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9652 * so we drop it by one. 9653 */ 9654 netstack_rele(tcps->tcps_netstack); 9655 if (connp == NULL) { 9656 inet_minor_free(ip_minor_arena, conn_dev); 9657 q->q_ptr = NULL; 9658 return (ENOSR); 9659 } 9660 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9661 tcp = connp->conn_tcp; 9662 9663 q->q_ptr = WR(q)->q_ptr = connp; 9664 if (getmajor(*devp) == TCP6_MAJ) { 9665 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9666 connp->conn_send = ip_output_v6; 9667 connp->conn_af_isv6 = B_TRUE; 9668 connp->conn_pkt_isv6 = B_TRUE; 9669 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9670 tcp->tcp_ipversion = IPV6_VERSION; 9671 tcp->tcp_family = AF_INET6; 9672 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9673 } else { 9674 connp->conn_flags |= IPCL_TCP4; 9675 connp->conn_send = ip_output; 9676 connp->conn_af_isv6 = B_FALSE; 9677 connp->conn_pkt_isv6 = B_FALSE; 9678 tcp->tcp_ipversion = IPV4_VERSION; 9679 tcp->tcp_family = AF_INET; 9680 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9681 } 9682 9683 /* 9684 * TCP keeps a copy of cred for cache locality reasons but 9685 * we put a reference only once. If connp->conn_cred 9686 * becomes invalid, tcp_cred should also be set to NULL. 9687 */ 9688 tcp->tcp_cred = connp->conn_cred = credp; 9689 crhold(connp->conn_cred); 9690 tcp->tcp_cpid = curproc->p_pid; 9691 tcp->tcp_open_time = lbolt64; 9692 connp->conn_zoneid = zoneid; 9693 connp->conn_mlp_type = mlptSingle; 9694 connp->conn_ulp_labeled = !is_system_labeled(); 9695 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9696 ASSERT(tcp->tcp_tcps == tcps); 9697 9698 /* 9699 * If the caller has the process-wide flag set, then default to MAC 9700 * exempt mode. This allows read-down to unlabeled hosts. 9701 */ 9702 if (getpflags(NET_MAC_AWARE, credp) != 0) 9703 connp->conn_mac_exempt = B_TRUE; 9704 9705 connp->conn_dev = conn_dev; 9706 9707 ASSERT(q->q_qinfo == &tcp_rinit); 9708 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9709 9710 if (flag & SO_SOCKSTR) { 9711 /* 9712 * No need to insert a socket in tcp acceptor hash. 9713 * If it was a socket acceptor stream, we dealt with 9714 * it above. A socket listener can never accept a 9715 * connection and doesn't need acceptor_id. 9716 */ 9717 connp->conn_flags |= IPCL_SOCKET; 9718 tcp->tcp_issocket = 1; 9719 WR(q)->q_qinfo = &tcp_sock_winit; 9720 } else { 9721 #ifdef _ILP32 9722 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9723 #else 9724 tcp->tcp_acceptor_id = conn_dev; 9725 #endif /* _ILP32 */ 9726 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9727 } 9728 9729 if (tcps->tcps_trace) 9730 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9731 9732 err = tcp_init(tcp, q); 9733 if (err != 0) { 9734 inet_minor_free(ip_minor_arena, connp->conn_dev); 9735 tcp_acceptor_hash_remove(tcp); 9736 CONN_DEC_REF(connp); 9737 q->q_ptr = WR(q)->q_ptr = NULL; 9738 return (err); 9739 } 9740 9741 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9742 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9743 9744 /* Non-zero default values */ 9745 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9746 /* 9747 * Put the ref for TCP. Ref for IP was already put 9748 * by ipcl_conn_create. Also Make the conn_t globally 9749 * visible to walkers 9750 */ 9751 mutex_enter(&connp->conn_lock); 9752 CONN_INC_REF_LOCKED(connp); 9753 ASSERT(connp->conn_ref == 2); 9754 connp->conn_state_flags &= ~CONN_INCIPIENT; 9755 mutex_exit(&connp->conn_lock); 9756 9757 qprocson(q); 9758 return (0); 9759 } 9760 9761 /* 9762 * Some TCP options can be "set" by requesting them in the option 9763 * buffer. This is needed for XTI feature test though we do not 9764 * allow it in general. We interpret that this mechanism is more 9765 * applicable to OSI protocols and need not be allowed in general. 9766 * This routine filters out options for which it is not allowed (most) 9767 * and lets through those (few) for which it is. [ The XTI interface 9768 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9769 * ever implemented will have to be allowed here ]. 9770 */ 9771 static boolean_t 9772 tcp_allow_connopt_set(int level, int name) 9773 { 9774 9775 switch (level) { 9776 case IPPROTO_TCP: 9777 switch (name) { 9778 case TCP_NODELAY: 9779 return (B_TRUE); 9780 default: 9781 return (B_FALSE); 9782 } 9783 /*NOTREACHED*/ 9784 default: 9785 return (B_FALSE); 9786 } 9787 /*NOTREACHED*/ 9788 } 9789 9790 /* 9791 * This routine gets default values of certain options whose default 9792 * values are maintained by protocol specific code 9793 */ 9794 /* ARGSUSED */ 9795 int 9796 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9797 { 9798 int32_t *i1 = (int32_t *)ptr; 9799 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9800 9801 switch (level) { 9802 case IPPROTO_TCP: 9803 switch (name) { 9804 case TCP_NOTIFY_THRESHOLD: 9805 *i1 = tcps->tcps_ip_notify_interval; 9806 break; 9807 case TCP_ABORT_THRESHOLD: 9808 *i1 = tcps->tcps_ip_abort_interval; 9809 break; 9810 case TCP_CONN_NOTIFY_THRESHOLD: 9811 *i1 = tcps->tcps_ip_notify_cinterval; 9812 break; 9813 case TCP_CONN_ABORT_THRESHOLD: 9814 *i1 = tcps->tcps_ip_abort_cinterval; 9815 break; 9816 default: 9817 return (-1); 9818 } 9819 break; 9820 case IPPROTO_IP: 9821 switch (name) { 9822 case IP_TTL: 9823 *i1 = tcps->tcps_ipv4_ttl; 9824 break; 9825 default: 9826 return (-1); 9827 } 9828 break; 9829 case IPPROTO_IPV6: 9830 switch (name) { 9831 case IPV6_UNICAST_HOPS: 9832 *i1 = tcps->tcps_ipv6_hoplimit; 9833 break; 9834 default: 9835 return (-1); 9836 } 9837 break; 9838 default: 9839 return (-1); 9840 } 9841 return (sizeof (int)); 9842 } 9843 9844 9845 /* 9846 * TCP routine to get the values of options. 9847 */ 9848 int 9849 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9850 { 9851 int *i1 = (int *)ptr; 9852 conn_t *connp = Q_TO_CONN(q); 9853 tcp_t *tcp = connp->conn_tcp; 9854 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9855 9856 switch (level) { 9857 case SOL_SOCKET: 9858 switch (name) { 9859 case SO_LINGER: { 9860 struct linger *lgr = (struct linger *)ptr; 9861 9862 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9863 lgr->l_linger = tcp->tcp_lingertime; 9864 } 9865 return (sizeof (struct linger)); 9866 case SO_DEBUG: 9867 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9868 break; 9869 case SO_KEEPALIVE: 9870 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9871 break; 9872 case SO_DONTROUTE: 9873 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9874 break; 9875 case SO_USELOOPBACK: 9876 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9877 break; 9878 case SO_BROADCAST: 9879 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9880 break; 9881 case SO_REUSEADDR: 9882 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9883 break; 9884 case SO_OOBINLINE: 9885 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9886 break; 9887 case SO_DGRAM_ERRIND: 9888 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9889 break; 9890 case SO_TYPE: 9891 *i1 = SOCK_STREAM; 9892 break; 9893 case SO_SNDBUF: 9894 *i1 = tcp->tcp_xmit_hiwater; 9895 break; 9896 case SO_RCVBUF: 9897 *i1 = RD(q)->q_hiwat; 9898 break; 9899 case SO_SND_COPYAVOID: 9900 *i1 = tcp->tcp_snd_zcopy_on ? 9901 SO_SND_COPYAVOID : 0; 9902 break; 9903 case SO_ALLZONES: 9904 *i1 = connp->conn_allzones ? 1 : 0; 9905 break; 9906 case SO_ANON_MLP: 9907 *i1 = connp->conn_anon_mlp; 9908 break; 9909 case SO_MAC_EXEMPT: 9910 *i1 = connp->conn_mac_exempt; 9911 break; 9912 case SO_EXCLBIND: 9913 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9914 break; 9915 case SO_PROTOTYPE: 9916 *i1 = IPPROTO_TCP; 9917 break; 9918 case SO_DOMAIN: 9919 *i1 = tcp->tcp_family; 9920 break; 9921 default: 9922 return (-1); 9923 } 9924 break; 9925 case IPPROTO_TCP: 9926 switch (name) { 9927 case TCP_NODELAY: 9928 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9929 break; 9930 case TCP_MAXSEG: 9931 *i1 = tcp->tcp_mss; 9932 break; 9933 case TCP_NOTIFY_THRESHOLD: 9934 *i1 = (int)tcp->tcp_first_timer_threshold; 9935 break; 9936 case TCP_ABORT_THRESHOLD: 9937 *i1 = tcp->tcp_second_timer_threshold; 9938 break; 9939 case TCP_CONN_NOTIFY_THRESHOLD: 9940 *i1 = tcp->tcp_first_ctimer_threshold; 9941 break; 9942 case TCP_CONN_ABORT_THRESHOLD: 9943 *i1 = tcp->tcp_second_ctimer_threshold; 9944 break; 9945 case TCP_RECVDSTADDR: 9946 *i1 = tcp->tcp_recvdstaddr; 9947 break; 9948 case TCP_ANONPRIVBIND: 9949 *i1 = tcp->tcp_anon_priv_bind; 9950 break; 9951 case TCP_EXCLBIND: 9952 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9953 break; 9954 case TCP_INIT_CWND: 9955 *i1 = tcp->tcp_init_cwnd; 9956 break; 9957 case TCP_KEEPALIVE_THRESHOLD: 9958 *i1 = tcp->tcp_ka_interval; 9959 break; 9960 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9961 *i1 = tcp->tcp_ka_abort_thres; 9962 break; 9963 case TCP_CORK: 9964 *i1 = tcp->tcp_cork; 9965 break; 9966 default: 9967 return (-1); 9968 } 9969 break; 9970 case IPPROTO_IP: 9971 if (tcp->tcp_family != AF_INET) 9972 return (-1); 9973 switch (name) { 9974 case IP_OPTIONS: 9975 case T_IP_OPTIONS: { 9976 /* 9977 * This is compatible with BSD in that in only return 9978 * the reverse source route with the final destination 9979 * as the last entry. The first 4 bytes of the option 9980 * will contain the final destination. 9981 */ 9982 int opt_len; 9983 9984 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9985 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9986 ASSERT(opt_len >= 0); 9987 /* Caller ensures enough space */ 9988 if (opt_len > 0) { 9989 /* 9990 * TODO: Do we have to handle getsockopt on an 9991 * initiator as well? 9992 */ 9993 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9994 } 9995 return (0); 9996 } 9997 case IP_TOS: 9998 case T_IP_TOS: 9999 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10000 break; 10001 case IP_TTL: 10002 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10003 break; 10004 case IP_NEXTHOP: 10005 /* Handled at IP level */ 10006 return (-EINVAL); 10007 default: 10008 return (-1); 10009 } 10010 break; 10011 case IPPROTO_IPV6: 10012 /* 10013 * IPPROTO_IPV6 options are only supported for sockets 10014 * that are using IPv6 on the wire. 10015 */ 10016 if (tcp->tcp_ipversion != IPV6_VERSION) { 10017 return (-1); 10018 } 10019 switch (name) { 10020 case IPV6_UNICAST_HOPS: 10021 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10022 break; /* goto sizeof (int) option return */ 10023 case IPV6_BOUND_IF: 10024 /* Zero if not set */ 10025 *i1 = tcp->tcp_bound_if; 10026 break; /* goto sizeof (int) option return */ 10027 case IPV6_RECVPKTINFO: 10028 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10029 *i1 = 1; 10030 else 10031 *i1 = 0; 10032 break; /* goto sizeof (int) option return */ 10033 case IPV6_RECVTCLASS: 10034 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10035 *i1 = 1; 10036 else 10037 *i1 = 0; 10038 break; /* goto sizeof (int) option return */ 10039 case IPV6_RECVHOPLIMIT: 10040 if (tcp->tcp_ipv6_recvancillary & 10041 TCP_IPV6_RECVHOPLIMIT) 10042 *i1 = 1; 10043 else 10044 *i1 = 0; 10045 break; /* goto sizeof (int) option return */ 10046 case IPV6_RECVHOPOPTS: 10047 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10048 *i1 = 1; 10049 else 10050 *i1 = 0; 10051 break; /* goto sizeof (int) option return */ 10052 case IPV6_RECVDSTOPTS: 10053 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10054 *i1 = 1; 10055 else 10056 *i1 = 0; 10057 break; /* goto sizeof (int) option return */ 10058 case _OLD_IPV6_RECVDSTOPTS: 10059 if (tcp->tcp_ipv6_recvancillary & 10060 TCP_OLD_IPV6_RECVDSTOPTS) 10061 *i1 = 1; 10062 else 10063 *i1 = 0; 10064 break; /* goto sizeof (int) option return */ 10065 case IPV6_RECVRTHDR: 10066 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10067 *i1 = 1; 10068 else 10069 *i1 = 0; 10070 break; /* goto sizeof (int) option return */ 10071 case IPV6_RECVRTHDRDSTOPTS: 10072 if (tcp->tcp_ipv6_recvancillary & 10073 TCP_IPV6_RECVRTDSTOPTS) 10074 *i1 = 1; 10075 else 10076 *i1 = 0; 10077 break; /* goto sizeof (int) option return */ 10078 case IPV6_PKTINFO: { 10079 /* XXX assumes that caller has room for max size! */ 10080 struct in6_pktinfo *pkti; 10081 10082 pkti = (struct in6_pktinfo *)ptr; 10083 if (ipp->ipp_fields & IPPF_IFINDEX) 10084 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10085 else 10086 pkti->ipi6_ifindex = 0; 10087 if (ipp->ipp_fields & IPPF_ADDR) 10088 pkti->ipi6_addr = ipp->ipp_addr; 10089 else 10090 pkti->ipi6_addr = ipv6_all_zeros; 10091 return (sizeof (struct in6_pktinfo)); 10092 } 10093 case IPV6_TCLASS: 10094 if (ipp->ipp_fields & IPPF_TCLASS) 10095 *i1 = ipp->ipp_tclass; 10096 else 10097 *i1 = IPV6_FLOW_TCLASS( 10098 IPV6_DEFAULT_VERS_AND_FLOW); 10099 break; /* goto sizeof (int) option return */ 10100 case IPV6_NEXTHOP: { 10101 sin6_t *sin6 = (sin6_t *)ptr; 10102 10103 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10104 return (0); 10105 *sin6 = sin6_null; 10106 sin6->sin6_family = AF_INET6; 10107 sin6->sin6_addr = ipp->ipp_nexthop; 10108 return (sizeof (sin6_t)); 10109 } 10110 case IPV6_HOPOPTS: 10111 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10112 return (0); 10113 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10114 return (0); 10115 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10116 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10117 if (tcp->tcp_label_len > 0) { 10118 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10119 ptr[1] = (ipp->ipp_hopoptslen - 10120 tcp->tcp_label_len + 7) / 8 - 1; 10121 } 10122 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10123 case IPV6_RTHDRDSTOPTS: 10124 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10125 return (0); 10126 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10127 return (ipp->ipp_rtdstoptslen); 10128 case IPV6_RTHDR: 10129 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10130 return (0); 10131 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10132 return (ipp->ipp_rthdrlen); 10133 case IPV6_DSTOPTS: 10134 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10135 return (0); 10136 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10137 return (ipp->ipp_dstoptslen); 10138 case IPV6_SRC_PREFERENCES: 10139 return (ip6_get_src_preferences(connp, 10140 (uint32_t *)ptr)); 10141 case IPV6_PATHMTU: { 10142 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10143 10144 if (tcp->tcp_state < TCPS_ESTABLISHED) 10145 return (-1); 10146 10147 return (ip_fill_mtuinfo(&connp->conn_remv6, 10148 connp->conn_fport, mtuinfo, 10149 connp->conn_netstack)); 10150 } 10151 default: 10152 return (-1); 10153 } 10154 break; 10155 default: 10156 return (-1); 10157 } 10158 return (sizeof (int)); 10159 } 10160 10161 /* 10162 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10163 * Parameters are assumed to be verified by the caller. 10164 */ 10165 /* ARGSUSED */ 10166 int 10167 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10168 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10169 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10170 { 10171 conn_t *connp = Q_TO_CONN(q); 10172 tcp_t *tcp = connp->conn_tcp; 10173 int *i1 = (int *)invalp; 10174 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10175 boolean_t checkonly; 10176 int reterr; 10177 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10178 10179 switch (optset_context) { 10180 case SETFN_OPTCOM_CHECKONLY: 10181 checkonly = B_TRUE; 10182 /* 10183 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10184 * inlen != 0 implies value supplied and 10185 * we have to "pretend" to set it. 10186 * inlen == 0 implies that there is no 10187 * value part in T_CHECK request and just validation 10188 * done elsewhere should be enough, we just return here. 10189 */ 10190 if (inlen == 0) { 10191 *outlenp = 0; 10192 return (0); 10193 } 10194 break; 10195 case SETFN_OPTCOM_NEGOTIATE: 10196 checkonly = B_FALSE; 10197 break; 10198 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10199 case SETFN_CONN_NEGOTIATE: 10200 checkonly = B_FALSE; 10201 /* 10202 * Negotiating local and "association-related" options 10203 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10204 * primitives is allowed by XTI, but we choose 10205 * to not implement this style negotiation for Internet 10206 * protocols (We interpret it is a must for OSI world but 10207 * optional for Internet protocols) for all options. 10208 * [ Will do only for the few options that enable test 10209 * suites that our XTI implementation of this feature 10210 * works for transports that do allow it ] 10211 */ 10212 if (!tcp_allow_connopt_set(level, name)) { 10213 *outlenp = 0; 10214 return (EINVAL); 10215 } 10216 break; 10217 default: 10218 /* 10219 * We should never get here 10220 */ 10221 *outlenp = 0; 10222 return (EINVAL); 10223 } 10224 10225 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10226 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10227 10228 /* 10229 * For TCP, we should have no ancillary data sent down 10230 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10231 * has to be zero. 10232 */ 10233 ASSERT(thisdg_attrs == NULL); 10234 10235 /* 10236 * For fixed length options, no sanity check 10237 * of passed in length is done. It is assumed *_optcom_req() 10238 * routines do the right thing. 10239 */ 10240 10241 switch (level) { 10242 case SOL_SOCKET: 10243 switch (name) { 10244 case SO_LINGER: { 10245 struct linger *lgr = (struct linger *)invalp; 10246 10247 if (!checkonly) { 10248 if (lgr->l_onoff) { 10249 tcp->tcp_linger = 1; 10250 tcp->tcp_lingertime = lgr->l_linger; 10251 } else { 10252 tcp->tcp_linger = 0; 10253 tcp->tcp_lingertime = 0; 10254 } 10255 /* struct copy */ 10256 *(struct linger *)outvalp = *lgr; 10257 } else { 10258 if (!lgr->l_onoff) { 10259 ((struct linger *)outvalp)->l_onoff = 0; 10260 ((struct linger *)outvalp)->l_linger = 0; 10261 } else { 10262 /* struct copy */ 10263 *(struct linger *)outvalp = *lgr; 10264 } 10265 } 10266 *outlenp = sizeof (struct linger); 10267 return (0); 10268 } 10269 case SO_DEBUG: 10270 if (!checkonly) 10271 tcp->tcp_debug = onoff; 10272 break; 10273 case SO_KEEPALIVE: 10274 if (checkonly) { 10275 /* T_CHECK case */ 10276 break; 10277 } 10278 10279 if (!onoff) { 10280 if (tcp->tcp_ka_enabled) { 10281 if (tcp->tcp_ka_tid != 0) { 10282 (void) TCP_TIMER_CANCEL(tcp, 10283 tcp->tcp_ka_tid); 10284 tcp->tcp_ka_tid = 0; 10285 } 10286 tcp->tcp_ka_enabled = 0; 10287 } 10288 break; 10289 } 10290 if (!tcp->tcp_ka_enabled) { 10291 /* Crank up the keepalive timer */ 10292 tcp->tcp_ka_last_intrvl = 0; 10293 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10294 tcp_keepalive_killer, 10295 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10296 tcp->tcp_ka_enabled = 1; 10297 } 10298 break; 10299 case SO_DONTROUTE: 10300 /* 10301 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10302 * only of interest to IP. We track them here only so 10303 * that we can report their current value. 10304 */ 10305 if (!checkonly) { 10306 tcp->tcp_dontroute = onoff; 10307 tcp->tcp_connp->conn_dontroute = onoff; 10308 } 10309 break; 10310 case SO_USELOOPBACK: 10311 if (!checkonly) { 10312 tcp->tcp_useloopback = onoff; 10313 tcp->tcp_connp->conn_loopback = onoff; 10314 } 10315 break; 10316 case SO_BROADCAST: 10317 if (!checkonly) { 10318 tcp->tcp_broadcast = onoff; 10319 tcp->tcp_connp->conn_broadcast = onoff; 10320 } 10321 break; 10322 case SO_REUSEADDR: 10323 if (!checkonly) { 10324 tcp->tcp_reuseaddr = onoff; 10325 tcp->tcp_connp->conn_reuseaddr = onoff; 10326 } 10327 break; 10328 case SO_OOBINLINE: 10329 if (!checkonly) 10330 tcp->tcp_oobinline = onoff; 10331 break; 10332 case SO_DGRAM_ERRIND: 10333 if (!checkonly) 10334 tcp->tcp_dgram_errind = onoff; 10335 break; 10336 case SO_SNDBUF: { 10337 if (*i1 > tcps->tcps_max_buf) { 10338 *outlenp = 0; 10339 return (ENOBUFS); 10340 } 10341 if (checkonly) 10342 break; 10343 10344 tcp->tcp_xmit_hiwater = *i1; 10345 if (tcps->tcps_snd_lowat_fraction != 0) 10346 tcp->tcp_xmit_lowater = 10347 tcp->tcp_xmit_hiwater / 10348 tcps->tcps_snd_lowat_fraction; 10349 (void) tcp_maxpsz_set(tcp, B_TRUE); 10350 /* 10351 * If we are flow-controlled, recheck the condition. 10352 * There are apps that increase SO_SNDBUF size when 10353 * flow-controlled (EWOULDBLOCK), and expect the flow 10354 * control condition to be lifted right away. 10355 */ 10356 mutex_enter(&tcp->tcp_non_sq_lock); 10357 if (tcp->tcp_flow_stopped && 10358 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10359 tcp_clrqfull(tcp); 10360 } 10361 mutex_exit(&tcp->tcp_non_sq_lock); 10362 break; 10363 } 10364 case SO_RCVBUF: 10365 if (*i1 > tcps->tcps_max_buf) { 10366 *outlenp = 0; 10367 return (ENOBUFS); 10368 } 10369 /* Silently ignore zero */ 10370 if (!checkonly && *i1 != 0) { 10371 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10372 (void) tcp_rwnd_set(tcp, *i1); 10373 } 10374 /* 10375 * XXX should we return the rwnd here 10376 * and tcp_opt_get ? 10377 */ 10378 break; 10379 case SO_SND_COPYAVOID: 10380 if (!checkonly) { 10381 /* we only allow enable at most once for now */ 10382 if (tcp->tcp_loopback || 10383 (!tcp->tcp_snd_zcopy_aware && 10384 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10385 *outlenp = 0; 10386 return (EOPNOTSUPP); 10387 } 10388 tcp->tcp_snd_zcopy_aware = 1; 10389 } 10390 break; 10391 case SO_ALLZONES: 10392 /* Handled at the IP level */ 10393 return (-EINVAL); 10394 case SO_ANON_MLP: 10395 if (!checkonly) { 10396 mutex_enter(&connp->conn_lock); 10397 connp->conn_anon_mlp = onoff; 10398 mutex_exit(&connp->conn_lock); 10399 } 10400 break; 10401 case SO_MAC_EXEMPT: 10402 if (secpolicy_net_mac_aware(cr) != 0 || 10403 IPCL_IS_BOUND(connp)) 10404 return (EACCES); 10405 if (!checkonly) { 10406 mutex_enter(&connp->conn_lock); 10407 connp->conn_mac_exempt = onoff; 10408 mutex_exit(&connp->conn_lock); 10409 } 10410 break; 10411 case SO_EXCLBIND: 10412 if (!checkonly) 10413 tcp->tcp_exclbind = onoff; 10414 break; 10415 default: 10416 *outlenp = 0; 10417 return (EINVAL); 10418 } 10419 break; 10420 case IPPROTO_TCP: 10421 switch (name) { 10422 case TCP_NODELAY: 10423 if (!checkonly) 10424 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10425 break; 10426 case TCP_NOTIFY_THRESHOLD: 10427 if (!checkonly) 10428 tcp->tcp_first_timer_threshold = *i1; 10429 break; 10430 case TCP_ABORT_THRESHOLD: 10431 if (!checkonly) 10432 tcp->tcp_second_timer_threshold = *i1; 10433 break; 10434 case TCP_CONN_NOTIFY_THRESHOLD: 10435 if (!checkonly) 10436 tcp->tcp_first_ctimer_threshold = *i1; 10437 break; 10438 case TCP_CONN_ABORT_THRESHOLD: 10439 if (!checkonly) 10440 tcp->tcp_second_ctimer_threshold = *i1; 10441 break; 10442 case TCP_RECVDSTADDR: 10443 if (tcp->tcp_state > TCPS_LISTEN) 10444 return (EOPNOTSUPP); 10445 if (!checkonly) 10446 tcp->tcp_recvdstaddr = onoff; 10447 break; 10448 case TCP_ANONPRIVBIND: 10449 if ((reterr = secpolicy_net_privaddr(cr, 0)) != 0) { 10450 *outlenp = 0; 10451 return (reterr); 10452 } 10453 if (!checkonly) { 10454 tcp->tcp_anon_priv_bind = onoff; 10455 } 10456 break; 10457 case TCP_EXCLBIND: 10458 if (!checkonly) 10459 tcp->tcp_exclbind = onoff; 10460 break; /* goto sizeof (int) option return */ 10461 case TCP_INIT_CWND: { 10462 uint32_t init_cwnd = *((uint32_t *)invalp); 10463 10464 if (checkonly) 10465 break; 10466 10467 /* 10468 * Only allow socket with network configuration 10469 * privilege to set the initial cwnd to be larger 10470 * than allowed by RFC 3390. 10471 */ 10472 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10473 tcp->tcp_init_cwnd = init_cwnd; 10474 break; 10475 } 10476 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10477 *outlenp = 0; 10478 return (reterr); 10479 } 10480 if (init_cwnd > TCP_MAX_INIT_CWND) { 10481 *outlenp = 0; 10482 return (EINVAL); 10483 } 10484 tcp->tcp_init_cwnd = init_cwnd; 10485 break; 10486 } 10487 case TCP_KEEPALIVE_THRESHOLD: 10488 if (checkonly) 10489 break; 10490 10491 if (*i1 < tcps->tcps_keepalive_interval_low || 10492 *i1 > tcps->tcps_keepalive_interval_high) { 10493 *outlenp = 0; 10494 return (EINVAL); 10495 } 10496 if (*i1 != tcp->tcp_ka_interval) { 10497 tcp->tcp_ka_interval = *i1; 10498 /* 10499 * Check if we need to restart the 10500 * keepalive timer. 10501 */ 10502 if (tcp->tcp_ka_tid != 0) { 10503 ASSERT(tcp->tcp_ka_enabled); 10504 (void) TCP_TIMER_CANCEL(tcp, 10505 tcp->tcp_ka_tid); 10506 tcp->tcp_ka_last_intrvl = 0; 10507 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10508 tcp_keepalive_killer, 10509 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10510 } 10511 } 10512 break; 10513 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10514 if (!checkonly) { 10515 if (*i1 < 10516 tcps->tcps_keepalive_abort_interval_low || 10517 *i1 > 10518 tcps->tcps_keepalive_abort_interval_high) { 10519 *outlenp = 0; 10520 return (EINVAL); 10521 } 10522 tcp->tcp_ka_abort_thres = *i1; 10523 } 10524 break; 10525 case TCP_CORK: 10526 if (!checkonly) { 10527 /* 10528 * if tcp->tcp_cork was set and is now 10529 * being unset, we have to make sure that 10530 * the remaining data gets sent out. Also 10531 * unset tcp->tcp_cork so that tcp_wput_data() 10532 * can send data even if it is less than mss 10533 */ 10534 if (tcp->tcp_cork && onoff == 0 && 10535 tcp->tcp_unsent > 0) { 10536 tcp->tcp_cork = B_FALSE; 10537 tcp_wput_data(tcp, NULL, B_FALSE); 10538 } 10539 tcp->tcp_cork = onoff; 10540 } 10541 break; 10542 default: 10543 *outlenp = 0; 10544 return (EINVAL); 10545 } 10546 break; 10547 case IPPROTO_IP: 10548 if (tcp->tcp_family != AF_INET) { 10549 *outlenp = 0; 10550 return (ENOPROTOOPT); 10551 } 10552 switch (name) { 10553 case IP_OPTIONS: 10554 case T_IP_OPTIONS: 10555 reterr = tcp_opt_set_header(tcp, checkonly, 10556 invalp, inlen); 10557 if (reterr) { 10558 *outlenp = 0; 10559 return (reterr); 10560 } 10561 /* OK return - copy input buffer into output buffer */ 10562 if (invalp != outvalp) { 10563 /* don't trust bcopy for identical src/dst */ 10564 bcopy(invalp, outvalp, inlen); 10565 } 10566 *outlenp = inlen; 10567 return (0); 10568 case IP_TOS: 10569 case T_IP_TOS: 10570 if (!checkonly) { 10571 tcp->tcp_ipha->ipha_type_of_service = 10572 (uchar_t)*i1; 10573 tcp->tcp_tos = (uchar_t)*i1; 10574 } 10575 break; 10576 case IP_TTL: 10577 if (!checkonly) { 10578 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10579 tcp->tcp_ttl = (uchar_t)*i1; 10580 } 10581 break; 10582 case IP_BOUND_IF: 10583 case IP_NEXTHOP: 10584 /* Handled at the IP level */ 10585 return (-EINVAL); 10586 case IP_SEC_OPT: 10587 /* 10588 * We should not allow policy setting after 10589 * we start listening for connections. 10590 */ 10591 if (tcp->tcp_state == TCPS_LISTEN) { 10592 return (EINVAL); 10593 } else { 10594 /* Handled at the IP level */ 10595 return (-EINVAL); 10596 } 10597 default: 10598 *outlenp = 0; 10599 return (EINVAL); 10600 } 10601 break; 10602 case IPPROTO_IPV6: { 10603 ip6_pkt_t *ipp; 10604 10605 /* 10606 * IPPROTO_IPV6 options are only supported for sockets 10607 * that are using IPv6 on the wire. 10608 */ 10609 if (tcp->tcp_ipversion != IPV6_VERSION) { 10610 *outlenp = 0; 10611 return (ENOPROTOOPT); 10612 } 10613 /* 10614 * Only sticky options; no ancillary data 10615 */ 10616 ASSERT(thisdg_attrs == NULL); 10617 ipp = &tcp->tcp_sticky_ipp; 10618 10619 switch (name) { 10620 case IPV6_UNICAST_HOPS: 10621 /* -1 means use default */ 10622 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10623 *outlenp = 0; 10624 return (EINVAL); 10625 } 10626 if (!checkonly) { 10627 if (*i1 == -1) { 10628 tcp->tcp_ip6h->ip6_hops = 10629 ipp->ipp_unicast_hops = 10630 (uint8_t)tcps->tcps_ipv6_hoplimit; 10631 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10632 /* Pass modified value to IP. */ 10633 *i1 = tcp->tcp_ip6h->ip6_hops; 10634 } else { 10635 tcp->tcp_ip6h->ip6_hops = 10636 ipp->ipp_unicast_hops = 10637 (uint8_t)*i1; 10638 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10639 } 10640 reterr = tcp_build_hdrs(q, tcp); 10641 if (reterr != 0) 10642 return (reterr); 10643 } 10644 break; 10645 case IPV6_BOUND_IF: 10646 if (!checkonly) { 10647 int error = 0; 10648 10649 tcp->tcp_bound_if = *i1; 10650 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10651 B_TRUE, checkonly, level, name, mblk); 10652 if (error != 0) { 10653 *outlenp = 0; 10654 return (error); 10655 } 10656 } 10657 break; 10658 /* 10659 * Set boolean switches for ancillary data delivery 10660 */ 10661 case IPV6_RECVPKTINFO: 10662 if (!checkonly) { 10663 if (onoff) 10664 tcp->tcp_ipv6_recvancillary |= 10665 TCP_IPV6_RECVPKTINFO; 10666 else 10667 tcp->tcp_ipv6_recvancillary &= 10668 ~TCP_IPV6_RECVPKTINFO; 10669 /* Force it to be sent up with the next msg */ 10670 tcp->tcp_recvifindex = 0; 10671 } 10672 break; 10673 case IPV6_RECVTCLASS: 10674 if (!checkonly) { 10675 if (onoff) 10676 tcp->tcp_ipv6_recvancillary |= 10677 TCP_IPV6_RECVTCLASS; 10678 else 10679 tcp->tcp_ipv6_recvancillary &= 10680 ~TCP_IPV6_RECVTCLASS; 10681 } 10682 break; 10683 case IPV6_RECVHOPLIMIT: 10684 if (!checkonly) { 10685 if (onoff) 10686 tcp->tcp_ipv6_recvancillary |= 10687 TCP_IPV6_RECVHOPLIMIT; 10688 else 10689 tcp->tcp_ipv6_recvancillary &= 10690 ~TCP_IPV6_RECVHOPLIMIT; 10691 /* Force it to be sent up with the next msg */ 10692 tcp->tcp_recvhops = 0xffffffffU; 10693 } 10694 break; 10695 case IPV6_RECVHOPOPTS: 10696 if (!checkonly) { 10697 if (onoff) 10698 tcp->tcp_ipv6_recvancillary |= 10699 TCP_IPV6_RECVHOPOPTS; 10700 else 10701 tcp->tcp_ipv6_recvancillary &= 10702 ~TCP_IPV6_RECVHOPOPTS; 10703 } 10704 break; 10705 case IPV6_RECVDSTOPTS: 10706 if (!checkonly) { 10707 if (onoff) 10708 tcp->tcp_ipv6_recvancillary |= 10709 TCP_IPV6_RECVDSTOPTS; 10710 else 10711 tcp->tcp_ipv6_recvancillary &= 10712 ~TCP_IPV6_RECVDSTOPTS; 10713 } 10714 break; 10715 case _OLD_IPV6_RECVDSTOPTS: 10716 if (!checkonly) { 10717 if (onoff) 10718 tcp->tcp_ipv6_recvancillary |= 10719 TCP_OLD_IPV6_RECVDSTOPTS; 10720 else 10721 tcp->tcp_ipv6_recvancillary &= 10722 ~TCP_OLD_IPV6_RECVDSTOPTS; 10723 } 10724 break; 10725 case IPV6_RECVRTHDR: 10726 if (!checkonly) { 10727 if (onoff) 10728 tcp->tcp_ipv6_recvancillary |= 10729 TCP_IPV6_RECVRTHDR; 10730 else 10731 tcp->tcp_ipv6_recvancillary &= 10732 ~TCP_IPV6_RECVRTHDR; 10733 } 10734 break; 10735 case IPV6_RECVRTHDRDSTOPTS: 10736 if (!checkonly) { 10737 if (onoff) 10738 tcp->tcp_ipv6_recvancillary |= 10739 TCP_IPV6_RECVRTDSTOPTS; 10740 else 10741 tcp->tcp_ipv6_recvancillary &= 10742 ~TCP_IPV6_RECVRTDSTOPTS; 10743 } 10744 break; 10745 case IPV6_PKTINFO: 10746 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10747 return (EINVAL); 10748 if (checkonly) 10749 break; 10750 10751 if (inlen == 0) { 10752 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10753 } else { 10754 struct in6_pktinfo *pkti; 10755 10756 pkti = (struct in6_pktinfo *)invalp; 10757 /* 10758 * RFC 3542 states that ipi6_addr must be 10759 * the unspecified address when setting the 10760 * IPV6_PKTINFO sticky socket option on a 10761 * TCP socket. 10762 */ 10763 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10764 return (EINVAL); 10765 /* 10766 * ip6_set_pktinfo() validates the source 10767 * address and interface index. 10768 */ 10769 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10770 pkti, mblk); 10771 if (reterr != 0) 10772 return (reterr); 10773 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10774 ipp->ipp_addr = pkti->ipi6_addr; 10775 if (ipp->ipp_ifindex != 0) 10776 ipp->ipp_fields |= IPPF_IFINDEX; 10777 else 10778 ipp->ipp_fields &= ~IPPF_IFINDEX; 10779 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10780 ipp->ipp_fields |= IPPF_ADDR; 10781 else 10782 ipp->ipp_fields &= ~IPPF_ADDR; 10783 } 10784 reterr = tcp_build_hdrs(q, tcp); 10785 if (reterr != 0) 10786 return (reterr); 10787 break; 10788 case IPV6_TCLASS: 10789 if (inlen != 0 && inlen != sizeof (int)) 10790 return (EINVAL); 10791 if (checkonly) 10792 break; 10793 10794 if (inlen == 0) { 10795 ipp->ipp_fields &= ~IPPF_TCLASS; 10796 } else { 10797 if (*i1 > 255 || *i1 < -1) 10798 return (EINVAL); 10799 if (*i1 == -1) { 10800 ipp->ipp_tclass = 0; 10801 *i1 = 0; 10802 } else { 10803 ipp->ipp_tclass = *i1; 10804 } 10805 ipp->ipp_fields |= IPPF_TCLASS; 10806 } 10807 reterr = tcp_build_hdrs(q, tcp); 10808 if (reterr != 0) 10809 return (reterr); 10810 break; 10811 case IPV6_NEXTHOP: 10812 /* 10813 * IP will verify that the nexthop is reachable 10814 * and fail for sticky options. 10815 */ 10816 if (inlen != 0 && inlen != sizeof (sin6_t)) 10817 return (EINVAL); 10818 if (checkonly) 10819 break; 10820 10821 if (inlen == 0) { 10822 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10823 } else { 10824 sin6_t *sin6 = (sin6_t *)invalp; 10825 10826 if (sin6->sin6_family != AF_INET6) 10827 return (EAFNOSUPPORT); 10828 if (IN6_IS_ADDR_V4MAPPED( 10829 &sin6->sin6_addr)) 10830 return (EADDRNOTAVAIL); 10831 ipp->ipp_nexthop = sin6->sin6_addr; 10832 if (!IN6_IS_ADDR_UNSPECIFIED( 10833 &ipp->ipp_nexthop)) 10834 ipp->ipp_fields |= IPPF_NEXTHOP; 10835 else 10836 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10837 } 10838 reterr = tcp_build_hdrs(q, tcp); 10839 if (reterr != 0) 10840 return (reterr); 10841 break; 10842 case IPV6_HOPOPTS: { 10843 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10844 10845 /* 10846 * Sanity checks - minimum size, size a multiple of 10847 * eight bytes, and matching size passed in. 10848 */ 10849 if (inlen != 0 && 10850 inlen != (8 * (hopts->ip6h_len + 1))) 10851 return (EINVAL); 10852 10853 if (checkonly) 10854 break; 10855 10856 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10857 (uchar_t **)&ipp->ipp_hopopts, 10858 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10859 if (reterr != 0) 10860 return (reterr); 10861 if (ipp->ipp_hopoptslen == 0) 10862 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10863 else 10864 ipp->ipp_fields |= IPPF_HOPOPTS; 10865 reterr = tcp_build_hdrs(q, tcp); 10866 if (reterr != 0) 10867 return (reterr); 10868 break; 10869 } 10870 case IPV6_RTHDRDSTOPTS: { 10871 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10872 10873 /* 10874 * Sanity checks - minimum size, size a multiple of 10875 * eight bytes, and matching size passed in. 10876 */ 10877 if (inlen != 0 && 10878 inlen != (8 * (dopts->ip6d_len + 1))) 10879 return (EINVAL); 10880 10881 if (checkonly) 10882 break; 10883 10884 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10885 (uchar_t **)&ipp->ipp_rtdstopts, 10886 &ipp->ipp_rtdstoptslen, 0); 10887 if (reterr != 0) 10888 return (reterr); 10889 if (ipp->ipp_rtdstoptslen == 0) 10890 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10891 else 10892 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10893 reterr = tcp_build_hdrs(q, tcp); 10894 if (reterr != 0) 10895 return (reterr); 10896 break; 10897 } 10898 case IPV6_DSTOPTS: { 10899 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10900 10901 /* 10902 * Sanity checks - minimum size, size a multiple of 10903 * eight bytes, and matching size passed in. 10904 */ 10905 if (inlen != 0 && 10906 inlen != (8 * (dopts->ip6d_len + 1))) 10907 return (EINVAL); 10908 10909 if (checkonly) 10910 break; 10911 10912 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10913 (uchar_t **)&ipp->ipp_dstopts, 10914 &ipp->ipp_dstoptslen, 0); 10915 if (reterr != 0) 10916 return (reterr); 10917 if (ipp->ipp_dstoptslen == 0) 10918 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10919 else 10920 ipp->ipp_fields |= IPPF_DSTOPTS; 10921 reterr = tcp_build_hdrs(q, tcp); 10922 if (reterr != 0) 10923 return (reterr); 10924 break; 10925 } 10926 case IPV6_RTHDR: { 10927 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10928 10929 /* 10930 * Sanity checks - minimum size, size a multiple of 10931 * eight bytes, and matching size passed in. 10932 */ 10933 if (inlen != 0 && 10934 inlen != (8 * (rt->ip6r_len + 1))) 10935 return (EINVAL); 10936 10937 if (checkonly) 10938 break; 10939 10940 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10941 (uchar_t **)&ipp->ipp_rthdr, 10942 &ipp->ipp_rthdrlen, 0); 10943 if (reterr != 0) 10944 return (reterr); 10945 if (ipp->ipp_rthdrlen == 0) 10946 ipp->ipp_fields &= ~IPPF_RTHDR; 10947 else 10948 ipp->ipp_fields |= IPPF_RTHDR; 10949 reterr = tcp_build_hdrs(q, tcp); 10950 if (reterr != 0) 10951 return (reterr); 10952 break; 10953 } 10954 case IPV6_V6ONLY: 10955 if (!checkonly) 10956 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10957 break; 10958 case IPV6_USE_MIN_MTU: 10959 if (inlen != sizeof (int)) 10960 return (EINVAL); 10961 10962 if (*i1 < -1 || *i1 > 1) 10963 return (EINVAL); 10964 10965 if (checkonly) 10966 break; 10967 10968 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10969 ipp->ipp_use_min_mtu = *i1; 10970 break; 10971 case IPV6_BOUND_PIF: 10972 /* Handled at the IP level */ 10973 return (-EINVAL); 10974 case IPV6_SEC_OPT: 10975 /* 10976 * We should not allow policy setting after 10977 * we start listening for connections. 10978 */ 10979 if (tcp->tcp_state == TCPS_LISTEN) { 10980 return (EINVAL); 10981 } else { 10982 /* Handled at the IP level */ 10983 return (-EINVAL); 10984 } 10985 case IPV6_SRC_PREFERENCES: 10986 if (inlen != sizeof (uint32_t)) 10987 return (EINVAL); 10988 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10989 *(uint32_t *)invalp); 10990 if (reterr != 0) { 10991 *outlenp = 0; 10992 return (reterr); 10993 } 10994 break; 10995 default: 10996 *outlenp = 0; 10997 return (EINVAL); 10998 } 10999 break; 11000 } /* end IPPROTO_IPV6 */ 11001 default: 11002 *outlenp = 0; 11003 return (EINVAL); 11004 } 11005 /* 11006 * Common case of OK return with outval same as inval 11007 */ 11008 if (invalp != outvalp) { 11009 /* don't trust bcopy for identical src/dst */ 11010 (void) bcopy(invalp, outvalp, inlen); 11011 } 11012 *outlenp = inlen; 11013 return (0); 11014 } 11015 11016 /* 11017 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 11018 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 11019 * headers, and the maximum size tcp header (to avoid reallocation 11020 * on the fly for additional tcp options). 11021 * Returns failure if can't allocate memory. 11022 */ 11023 static int 11024 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 11025 { 11026 char *hdrs; 11027 uint_t hdrs_len; 11028 ip6i_t *ip6i; 11029 char buf[TCP_MAX_HDR_LENGTH]; 11030 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 11031 in6_addr_t src, dst; 11032 tcp_stack_t *tcps = tcp->tcp_tcps; 11033 11034 /* 11035 * save the existing tcp header and source/dest IP addresses 11036 */ 11037 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 11038 src = tcp->tcp_ip6h->ip6_src; 11039 dst = tcp->tcp_ip6h->ip6_dst; 11040 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 11041 ASSERT(hdrs_len != 0); 11042 if (hdrs_len > tcp->tcp_iphc_len) { 11043 /* Need to reallocate */ 11044 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 11045 if (hdrs == NULL) 11046 return (ENOMEM); 11047 if (tcp->tcp_iphc != NULL) { 11048 if (tcp->tcp_hdr_grown) { 11049 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 11050 } else { 11051 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 11052 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 11053 } 11054 tcp->tcp_iphc_len = 0; 11055 } 11056 ASSERT(tcp->tcp_iphc_len == 0); 11057 tcp->tcp_iphc = hdrs; 11058 tcp->tcp_iphc_len = hdrs_len; 11059 tcp->tcp_hdr_grown = B_TRUE; 11060 } 11061 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11062 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11063 11064 /* Set header fields not in ipp */ 11065 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11066 ip6i = (ip6i_t *)tcp->tcp_iphc; 11067 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11068 } else { 11069 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11070 } 11071 /* 11072 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11073 * 11074 * tcp->tcp_tcp_hdr_len doesn't change here. 11075 */ 11076 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11077 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11078 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11079 11080 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11081 11082 tcp->tcp_ip6h->ip6_src = src; 11083 tcp->tcp_ip6h->ip6_dst = dst; 11084 11085 /* 11086 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11087 * the default value for TCP. 11088 */ 11089 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11090 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 11091 11092 /* 11093 * If we're setting extension headers after a connection 11094 * has been established, and if we have a routing header 11095 * among the extension headers, call ip_massage_options_v6 to 11096 * manipulate the routing header/ip6_dst set the checksum 11097 * difference in the tcp header template. 11098 * (This happens in tcp_connect_ipv6 if the routing header 11099 * is set prior to the connect.) 11100 * Set the tcp_sum to zero first in case we've cleared a 11101 * routing header or don't have one at all. 11102 */ 11103 tcp->tcp_sum = 0; 11104 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11105 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11106 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11107 (uint8_t *)tcp->tcp_tcph); 11108 if (rth != NULL) { 11109 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11110 rth, tcps->tcps_netstack); 11111 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11112 (tcp->tcp_sum >> 16)); 11113 } 11114 } 11115 11116 /* Try to get everything in a single mblk */ 11117 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcps->tcps_wroff_xtra); 11118 return (0); 11119 } 11120 11121 /* 11122 * Transfer any source route option from ipha to buf/dst in reversed form. 11123 */ 11124 static int 11125 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11126 { 11127 ipoptp_t opts; 11128 uchar_t *opt; 11129 uint8_t optval; 11130 uint8_t optlen; 11131 uint32_t len = 0; 11132 11133 for (optval = ipoptp_first(&opts, ipha); 11134 optval != IPOPT_EOL; 11135 optval = ipoptp_next(&opts)) { 11136 opt = opts.ipoptp_cur; 11137 optlen = opts.ipoptp_len; 11138 switch (optval) { 11139 int off1, off2; 11140 case IPOPT_SSRR: 11141 case IPOPT_LSRR: 11142 11143 /* Reverse source route */ 11144 /* 11145 * First entry should be the next to last one in the 11146 * current source route (the last entry is our 11147 * address.) 11148 * The last entry should be the final destination. 11149 */ 11150 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11151 buf[IPOPT_OLEN] = (uint8_t)optlen; 11152 off1 = IPOPT_MINOFF_SR - 1; 11153 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11154 if (off2 < 0) { 11155 /* No entries in source route */ 11156 break; 11157 } 11158 bcopy(opt + off2, dst, IP_ADDR_LEN); 11159 /* 11160 * Note: use src since ipha has not had its src 11161 * and dst reversed (it is in the state it was 11162 * received. 11163 */ 11164 bcopy(&ipha->ipha_src, buf + off2, 11165 IP_ADDR_LEN); 11166 off2 -= IP_ADDR_LEN; 11167 11168 while (off2 > 0) { 11169 bcopy(opt + off2, buf + off1, 11170 IP_ADDR_LEN); 11171 off1 += IP_ADDR_LEN; 11172 off2 -= IP_ADDR_LEN; 11173 } 11174 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11175 buf += optlen; 11176 len += optlen; 11177 break; 11178 } 11179 } 11180 done: 11181 /* Pad the resulting options */ 11182 while (len & 0x3) { 11183 *buf++ = IPOPT_EOL; 11184 len++; 11185 } 11186 return (len); 11187 } 11188 11189 11190 /* 11191 * Extract and revert a source route from ipha (if any) 11192 * and then update the relevant fields in both tcp_t and the standard header. 11193 */ 11194 static void 11195 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11196 { 11197 char buf[TCP_MAX_HDR_LENGTH]; 11198 uint_t tcph_len; 11199 int len; 11200 11201 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11202 len = IPH_HDR_LENGTH(ipha); 11203 if (len == IP_SIMPLE_HDR_LENGTH) 11204 /* Nothing to do */ 11205 return; 11206 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11207 (len & 0x3)) 11208 return; 11209 11210 tcph_len = tcp->tcp_tcp_hdr_len; 11211 bcopy(tcp->tcp_tcph, buf, tcph_len); 11212 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11213 (tcp->tcp_ipha->ipha_dst & 0xffff); 11214 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11215 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11216 len += IP_SIMPLE_HDR_LENGTH; 11217 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11218 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11219 if ((int)tcp->tcp_sum < 0) 11220 tcp->tcp_sum--; 11221 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11222 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11223 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11224 bcopy(buf, tcp->tcp_tcph, tcph_len); 11225 tcp->tcp_ip_hdr_len = len; 11226 tcp->tcp_ipha->ipha_version_and_hdr_length = 11227 (IP_VERSION << 4) | (len >> 2); 11228 len += tcph_len; 11229 tcp->tcp_hdr_len = len; 11230 } 11231 11232 /* 11233 * Copy the standard header into its new location, 11234 * lay in the new options and then update the relevant 11235 * fields in both tcp_t and the standard header. 11236 */ 11237 static int 11238 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11239 { 11240 uint_t tcph_len; 11241 uint8_t *ip_optp; 11242 tcph_t *new_tcph; 11243 tcp_stack_t *tcps = tcp->tcp_tcps; 11244 11245 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11246 return (EINVAL); 11247 11248 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11249 return (EINVAL); 11250 11251 if (checkonly) { 11252 /* 11253 * do not really set, just pretend to - T_CHECK 11254 */ 11255 return (0); 11256 } 11257 11258 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11259 if (tcp->tcp_label_len > 0) { 11260 int padlen; 11261 uint8_t opt; 11262 11263 /* convert list termination to no-ops */ 11264 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11265 ip_optp += ip_optp[IPOPT_OLEN]; 11266 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11267 while (--padlen >= 0) 11268 *ip_optp++ = opt; 11269 } 11270 tcph_len = tcp->tcp_tcp_hdr_len; 11271 new_tcph = (tcph_t *)(ip_optp + len); 11272 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11273 tcp->tcp_tcph = new_tcph; 11274 bcopy(ptr, ip_optp, len); 11275 11276 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11277 11278 tcp->tcp_ip_hdr_len = len; 11279 tcp->tcp_ipha->ipha_version_and_hdr_length = 11280 (IP_VERSION << 4) | (len >> 2); 11281 tcp->tcp_hdr_len = len + tcph_len; 11282 if (!TCP_IS_DETACHED(tcp)) { 11283 /* Always allocate room for all options. */ 11284 (void) mi_set_sth_wroff(tcp->tcp_rq, 11285 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11286 } 11287 return (0); 11288 } 11289 11290 /* Get callback routine passed to nd_load by tcp_param_register */ 11291 /* ARGSUSED */ 11292 static int 11293 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11294 { 11295 tcpparam_t *tcppa = (tcpparam_t *)cp; 11296 11297 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11298 return (0); 11299 } 11300 11301 /* 11302 * Walk through the param array specified registering each element with the 11303 * named dispatch handler. 11304 */ 11305 static boolean_t 11306 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11307 { 11308 for (; cnt-- > 0; tcppa++) { 11309 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11310 if (!nd_load(ndp, tcppa->tcp_param_name, 11311 tcp_param_get, tcp_param_set, 11312 (caddr_t)tcppa)) { 11313 nd_free(ndp); 11314 return (B_FALSE); 11315 } 11316 } 11317 } 11318 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11319 KM_SLEEP); 11320 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11321 sizeof (tcpparam_t)); 11322 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11323 tcp_param_get, tcp_param_set_aligned, 11324 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11325 nd_free(ndp); 11326 return (B_FALSE); 11327 } 11328 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11329 KM_SLEEP); 11330 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11331 sizeof (tcpparam_t)); 11332 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11333 tcp_param_get, tcp_param_set_aligned, 11334 (caddr_t)tcps->tcps_mdt_head_param)) { 11335 nd_free(ndp); 11336 return (B_FALSE); 11337 } 11338 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11339 KM_SLEEP); 11340 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11341 sizeof (tcpparam_t)); 11342 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11343 tcp_param_get, tcp_param_set_aligned, 11344 (caddr_t)tcps->tcps_mdt_tail_param)) { 11345 nd_free(ndp); 11346 return (B_FALSE); 11347 } 11348 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11349 KM_SLEEP); 11350 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11351 sizeof (tcpparam_t)); 11352 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11353 tcp_param_get, tcp_param_set_aligned, 11354 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11355 nd_free(ndp); 11356 return (B_FALSE); 11357 } 11358 if (!nd_load(ndp, "tcp_extra_priv_ports", 11359 tcp_extra_priv_ports_get, NULL, NULL)) { 11360 nd_free(ndp); 11361 return (B_FALSE); 11362 } 11363 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11364 NULL, tcp_extra_priv_ports_add, NULL)) { 11365 nd_free(ndp); 11366 return (B_FALSE); 11367 } 11368 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11369 NULL, tcp_extra_priv_ports_del, NULL)) { 11370 nd_free(ndp); 11371 return (B_FALSE); 11372 } 11373 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11374 NULL)) { 11375 nd_free(ndp); 11376 return (B_FALSE); 11377 } 11378 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11379 NULL, NULL)) { 11380 nd_free(ndp); 11381 return (B_FALSE); 11382 } 11383 if (!nd_load(ndp, "tcp_listen_hash", 11384 tcp_listen_hash_report, NULL, NULL)) { 11385 nd_free(ndp); 11386 return (B_FALSE); 11387 } 11388 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11389 NULL, NULL)) { 11390 nd_free(ndp); 11391 return (B_FALSE); 11392 } 11393 if (!nd_load(ndp, "tcp_acceptor_hash", 11394 tcp_acceptor_hash_report, NULL, NULL)) { 11395 nd_free(ndp); 11396 return (B_FALSE); 11397 } 11398 if (!nd_load(ndp, "tcp_host_param", tcp_host_param_report, 11399 tcp_host_param_set, NULL)) { 11400 nd_free(ndp); 11401 return (B_FALSE); 11402 } 11403 if (!nd_load(ndp, "tcp_host_param_ipv6", 11404 tcp_host_param_report, tcp_host_param_set_ipv6, NULL)) { 11405 nd_free(ndp); 11406 return (B_FALSE); 11407 } 11408 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11409 tcp_1948_phrase_set, NULL)) { 11410 nd_free(ndp); 11411 return (B_FALSE); 11412 } 11413 if (!nd_load(ndp, "tcp_reserved_port_list", 11414 tcp_reserved_port_list, NULL, NULL)) { 11415 nd_free(ndp); 11416 return (B_FALSE); 11417 } 11418 /* 11419 * Dummy ndd variables - only to convey obsolescence information 11420 * through printing of their name (no get or set routines) 11421 * XXX Remove in future releases ? 11422 */ 11423 if (!nd_load(ndp, 11424 "tcp_close_wait_interval(obsoleted - " 11425 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11426 nd_free(ndp); 11427 return (B_FALSE); 11428 } 11429 return (B_TRUE); 11430 } 11431 11432 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11433 /* ARGSUSED */ 11434 static int 11435 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11436 cred_t *cr) 11437 { 11438 long new_value; 11439 tcpparam_t *tcppa = (tcpparam_t *)cp; 11440 11441 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11442 new_value < tcppa->tcp_param_min || 11443 new_value > tcppa->tcp_param_max) { 11444 return (EINVAL); 11445 } 11446 /* 11447 * Need to make sure new_value is a multiple of 4. If it is not, 11448 * round it up. For future 64 bit requirement, we actually make it 11449 * a multiple of 8. 11450 */ 11451 if (new_value & 0x7) { 11452 new_value = (new_value & ~0x7) + 0x8; 11453 } 11454 tcppa->tcp_param_val = new_value; 11455 return (0); 11456 } 11457 11458 /* Set callback routine passed to nd_load by tcp_param_register */ 11459 /* ARGSUSED */ 11460 static int 11461 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11462 { 11463 long new_value; 11464 tcpparam_t *tcppa = (tcpparam_t *)cp; 11465 11466 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11467 new_value < tcppa->tcp_param_min || 11468 new_value > tcppa->tcp_param_max) { 11469 return (EINVAL); 11470 } 11471 tcppa->tcp_param_val = new_value; 11472 return (0); 11473 } 11474 11475 /* 11476 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11477 * is filled, return as much as we can. The message passed in may be 11478 * multi-part, chained using b_cont. "start" is the starting sequence 11479 * number for this piece. 11480 */ 11481 static mblk_t * 11482 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11483 { 11484 uint32_t end; 11485 mblk_t *mp1; 11486 mblk_t *mp2; 11487 mblk_t *next_mp; 11488 uint32_t u1; 11489 tcp_stack_t *tcps = tcp->tcp_tcps; 11490 11491 /* Walk through all the new pieces. */ 11492 do { 11493 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11494 (uintptr_t)INT_MAX); 11495 end = start + (int)(mp->b_wptr - mp->b_rptr); 11496 next_mp = mp->b_cont; 11497 if (start == end) { 11498 /* Empty. Blast it. */ 11499 freeb(mp); 11500 continue; 11501 } 11502 mp->b_cont = NULL; 11503 TCP_REASS_SET_SEQ(mp, start); 11504 TCP_REASS_SET_END(mp, end); 11505 mp1 = tcp->tcp_reass_tail; 11506 if (!mp1) { 11507 tcp->tcp_reass_tail = mp; 11508 tcp->tcp_reass_head = mp; 11509 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11510 UPDATE_MIB(&tcps->tcps_mib, 11511 tcpInDataUnorderBytes, end - start); 11512 continue; 11513 } 11514 /* New stuff completely beyond tail? */ 11515 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11516 /* Link it on end. */ 11517 mp1->b_cont = mp; 11518 tcp->tcp_reass_tail = mp; 11519 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11520 UPDATE_MIB(&tcps->tcps_mib, 11521 tcpInDataUnorderBytes, end - start); 11522 continue; 11523 } 11524 mp1 = tcp->tcp_reass_head; 11525 u1 = TCP_REASS_SEQ(mp1); 11526 /* New stuff at the front? */ 11527 if (SEQ_LT(start, u1)) { 11528 /* Yes... Check for overlap. */ 11529 mp->b_cont = mp1; 11530 tcp->tcp_reass_head = mp; 11531 tcp_reass_elim_overlap(tcp, mp); 11532 continue; 11533 } 11534 /* 11535 * The new piece fits somewhere between the head and tail. 11536 * We find our slot, where mp1 precedes us and mp2 trails. 11537 */ 11538 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11539 u1 = TCP_REASS_SEQ(mp2); 11540 if (SEQ_LEQ(start, u1)) 11541 break; 11542 } 11543 /* Link ourselves in */ 11544 mp->b_cont = mp2; 11545 mp1->b_cont = mp; 11546 11547 /* Trim overlap with following mblk(s) first */ 11548 tcp_reass_elim_overlap(tcp, mp); 11549 11550 /* Trim overlap with preceding mblk */ 11551 tcp_reass_elim_overlap(tcp, mp1); 11552 11553 } while (start = end, mp = next_mp); 11554 mp1 = tcp->tcp_reass_head; 11555 /* Anything ready to go? */ 11556 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11557 return (NULL); 11558 /* Eat what we can off the queue */ 11559 for (;;) { 11560 mp = mp1->b_cont; 11561 end = TCP_REASS_END(mp1); 11562 TCP_REASS_SET_SEQ(mp1, 0); 11563 TCP_REASS_SET_END(mp1, 0); 11564 if (!mp) { 11565 tcp->tcp_reass_tail = NULL; 11566 break; 11567 } 11568 if (end != TCP_REASS_SEQ(mp)) { 11569 mp1->b_cont = NULL; 11570 break; 11571 } 11572 mp1 = mp; 11573 } 11574 mp1 = tcp->tcp_reass_head; 11575 tcp->tcp_reass_head = mp; 11576 return (mp1); 11577 } 11578 11579 /* Eliminate any overlap that mp may have over later mblks */ 11580 static void 11581 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11582 { 11583 uint32_t end; 11584 mblk_t *mp1; 11585 uint32_t u1; 11586 tcp_stack_t *tcps = tcp->tcp_tcps; 11587 11588 end = TCP_REASS_END(mp); 11589 while ((mp1 = mp->b_cont) != NULL) { 11590 u1 = TCP_REASS_SEQ(mp1); 11591 if (!SEQ_GT(end, u1)) 11592 break; 11593 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11594 mp->b_wptr -= end - u1; 11595 TCP_REASS_SET_END(mp, u1); 11596 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11597 UPDATE_MIB(&tcps->tcps_mib, 11598 tcpInDataPartDupBytes, end - u1); 11599 break; 11600 } 11601 mp->b_cont = mp1->b_cont; 11602 TCP_REASS_SET_SEQ(mp1, 0); 11603 TCP_REASS_SET_END(mp1, 0); 11604 freeb(mp1); 11605 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11606 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11607 } 11608 if (!mp1) 11609 tcp->tcp_reass_tail = mp; 11610 } 11611 11612 /* 11613 * Send up all messages queued on tcp_rcv_list. 11614 */ 11615 static uint_t 11616 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11617 { 11618 mblk_t *mp; 11619 uint_t ret = 0; 11620 uint_t thwin; 11621 #ifdef DEBUG 11622 uint_t cnt = 0; 11623 #endif 11624 tcp_stack_t *tcps = tcp->tcp_tcps; 11625 11626 /* Can't drain on an eager connection */ 11627 if (tcp->tcp_listener != NULL) 11628 return (ret); 11629 11630 /* 11631 * Handle two cases here: we are currently fused or we were 11632 * previously fused and have some urgent data to be delivered 11633 * upstream. The latter happens because we either ran out of 11634 * memory or were detached and therefore sending the SIGURG was 11635 * deferred until this point. In either case we pass control 11636 * over to tcp_fuse_rcv_drain() since it may need to complete 11637 * some work. 11638 */ 11639 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11640 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11641 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11642 &tcp->tcp_fused_sigurg_mp)) 11643 return (ret); 11644 } 11645 11646 while ((mp = tcp->tcp_rcv_list) != NULL) { 11647 tcp->tcp_rcv_list = mp->b_next; 11648 mp->b_next = NULL; 11649 #ifdef DEBUG 11650 cnt += msgdsize(mp); 11651 #endif 11652 /* Does this need SSL processing first? */ 11653 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11654 tcp_kssl_input(tcp, mp); 11655 continue; 11656 } 11657 putnext(q, mp); 11658 } 11659 ASSERT(cnt == tcp->tcp_rcv_cnt); 11660 tcp->tcp_rcv_last_head = NULL; 11661 tcp->tcp_rcv_last_tail = NULL; 11662 tcp->tcp_rcv_cnt = 0; 11663 11664 /* Learn the latest rwnd information that we sent to the other side. */ 11665 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11666 << tcp->tcp_rcv_ws; 11667 /* This is peer's calculated send window (our receive window). */ 11668 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11669 /* 11670 * Increase the receive window to max. But we need to do receiver 11671 * SWS avoidance. This means that we need to check the increase of 11672 * of receive window is at least 1 MSS. 11673 */ 11674 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11675 /* 11676 * If the window that the other side knows is less than max 11677 * deferred acks segments, send an update immediately. 11678 */ 11679 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11680 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11681 ret = TH_ACK_NEEDED; 11682 } 11683 tcp->tcp_rwnd = q->q_hiwat; 11684 } 11685 /* No need for the push timer now. */ 11686 if (tcp->tcp_push_tid != 0) { 11687 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11688 tcp->tcp_push_tid = 0; 11689 } 11690 return (ret); 11691 } 11692 11693 /* 11694 * Queue data on tcp_rcv_list which is a b_next chain. 11695 * tcp_rcv_last_head/tail is the last element of this chain. 11696 * Each element of the chain is a b_cont chain. 11697 * 11698 * M_DATA messages are added to the current element. 11699 * Other messages are added as new (b_next) elements. 11700 */ 11701 void 11702 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11703 { 11704 ASSERT(seg_len == msgdsize(mp)); 11705 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11706 11707 if (tcp->tcp_rcv_list == NULL) { 11708 ASSERT(tcp->tcp_rcv_last_head == NULL); 11709 tcp->tcp_rcv_list = mp; 11710 tcp->tcp_rcv_last_head = mp; 11711 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11712 tcp->tcp_rcv_last_tail->b_cont = mp; 11713 } else { 11714 tcp->tcp_rcv_last_head->b_next = mp; 11715 tcp->tcp_rcv_last_head = mp; 11716 } 11717 11718 while (mp->b_cont) 11719 mp = mp->b_cont; 11720 11721 tcp->tcp_rcv_last_tail = mp; 11722 tcp->tcp_rcv_cnt += seg_len; 11723 tcp->tcp_rwnd -= seg_len; 11724 } 11725 11726 /* 11727 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11728 * 11729 * This is the default entry function into TCP on the read side. TCP is 11730 * always entered via squeue i.e. using squeue's for mutual exclusion. 11731 * When classifier does a lookup to find the tcp, it also puts a reference 11732 * on the conn structure associated so the tcp is guaranteed to exist 11733 * when we come here. We still need to check the state because it might 11734 * as well has been closed. The squeue processing function i.e. squeue_enter, 11735 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 11736 * CONN_DEC_REF. 11737 * 11738 * Apart from the default entry point, IP also sends packets directly to 11739 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11740 * connections. 11741 */ 11742 void 11743 tcp_input(void *arg, mblk_t *mp, void *arg2) 11744 { 11745 conn_t *connp = (conn_t *)arg; 11746 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11747 11748 /* arg2 is the sqp */ 11749 ASSERT(arg2 != NULL); 11750 ASSERT(mp != NULL); 11751 11752 /* 11753 * Don't accept any input on a closed tcp as this TCP logically does 11754 * not exist on the system. Don't proceed further with this TCP. 11755 * For eg. this packet could trigger another close of this tcp 11756 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11757 * tcp_clean_death / tcp_closei_local must be called at most once 11758 * on a TCP. In this case we need to refeed the packet into the 11759 * classifier and figure out where the packet should go. Need to 11760 * preserve the recv_ill somehow. Until we figure that out, for 11761 * now just drop the packet if we can't classify the packet. 11762 */ 11763 if (tcp->tcp_state == TCPS_CLOSED || 11764 tcp->tcp_state == TCPS_BOUND) { 11765 conn_t *new_connp; 11766 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11767 11768 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11769 if (new_connp != NULL) { 11770 tcp_reinput(new_connp, mp, arg2); 11771 return; 11772 } 11773 /* We failed to classify. For now just drop the packet */ 11774 freemsg(mp); 11775 return; 11776 } 11777 11778 if (DB_TYPE(mp) == M_DATA) 11779 tcp_rput_data(connp, mp, arg2); 11780 else 11781 tcp_rput_common(tcp, mp); 11782 } 11783 11784 /* 11785 * The read side put procedure. 11786 * The packets passed up by ip are assume to be aligned according to 11787 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11788 */ 11789 static void 11790 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11791 { 11792 /* 11793 * tcp_rput_data() does not expect M_CTL except for the case 11794 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11795 * type. Need to make sure that any other M_CTLs don't make 11796 * it to tcp_rput_data since it is not expecting any and doesn't 11797 * check for it. 11798 */ 11799 if (DB_TYPE(mp) == M_CTL) { 11800 switch (*(uint32_t *)(mp->b_rptr)) { 11801 case TCP_IOC_ABORT_CONN: 11802 /* 11803 * Handle connection abort request. 11804 */ 11805 tcp_ioctl_abort_handler(tcp, mp); 11806 return; 11807 case IPSEC_IN: 11808 /* 11809 * Only secure icmp arrive in TCP and they 11810 * don't go through data path. 11811 */ 11812 tcp_icmp_error(tcp, mp); 11813 return; 11814 case IN_PKTINFO: 11815 /* 11816 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11817 * sockets that are receiving IPv4 traffic. tcp 11818 */ 11819 ASSERT(tcp->tcp_family == AF_INET6); 11820 ASSERT(tcp->tcp_ipv6_recvancillary & 11821 TCP_IPV6_RECVPKTINFO); 11822 tcp_rput_data(tcp->tcp_connp, mp, 11823 tcp->tcp_connp->conn_sqp); 11824 return; 11825 case MDT_IOC_INFO_UPDATE: 11826 /* 11827 * Handle Multidata information update; the 11828 * following routine will free the message. 11829 */ 11830 if (tcp->tcp_connp->conn_mdt_ok) { 11831 tcp_mdt_update(tcp, 11832 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11833 B_FALSE); 11834 } 11835 freemsg(mp); 11836 return; 11837 case LSO_IOC_INFO_UPDATE: 11838 /* 11839 * Handle LSO information update; the following 11840 * routine will free the message. 11841 */ 11842 if (tcp->tcp_connp->conn_lso_ok) { 11843 tcp_lso_update(tcp, 11844 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11845 } 11846 freemsg(mp); 11847 return; 11848 default: 11849 /* 11850 * tcp_icmp_err() will process the M_CTL packets. 11851 * Non-ICMP packets, if any, will be discarded in 11852 * tcp_icmp_err(). We will process the ICMP packet 11853 * even if we are TCP_IS_DETACHED_NONEAGER as the 11854 * incoming ICMP packet may result in changing 11855 * the tcp_mss, which we would need if we have 11856 * packets to retransmit. 11857 */ 11858 tcp_icmp_error(tcp, mp); 11859 return; 11860 } 11861 } 11862 11863 /* No point processing the message if tcp is already closed */ 11864 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11865 freemsg(mp); 11866 return; 11867 } 11868 11869 tcp_rput_other(tcp, mp); 11870 } 11871 11872 11873 /* The minimum of smoothed mean deviation in RTO calculation. */ 11874 #define TCP_SD_MIN 400 11875 11876 /* 11877 * Set RTO for this connection. The formula is from Jacobson and Karels' 11878 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11879 * are the same as those in Appendix A.2 of that paper. 11880 * 11881 * m = new measurement 11882 * sa = smoothed RTT average (8 * average estimates). 11883 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11884 */ 11885 static void 11886 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11887 { 11888 long m = TICK_TO_MSEC(rtt); 11889 clock_t sa = tcp->tcp_rtt_sa; 11890 clock_t sv = tcp->tcp_rtt_sd; 11891 clock_t rto; 11892 tcp_stack_t *tcps = tcp->tcp_tcps; 11893 11894 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11895 tcp->tcp_rtt_update++; 11896 11897 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11898 if (sa != 0) { 11899 /* 11900 * Update average estimator: 11901 * new rtt = 7/8 old rtt + 1/8 Error 11902 */ 11903 11904 /* m is now Error in estimate. */ 11905 m -= sa >> 3; 11906 if ((sa += m) <= 0) { 11907 /* 11908 * Don't allow the smoothed average to be negative. 11909 * We use 0 to denote reinitialization of the 11910 * variables. 11911 */ 11912 sa = 1; 11913 } 11914 11915 /* 11916 * Update deviation estimator: 11917 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11918 */ 11919 if (m < 0) 11920 m = -m; 11921 m -= sv >> 2; 11922 sv += m; 11923 } else { 11924 /* 11925 * This follows BSD's implementation. So the reinitialized 11926 * RTO is 3 * m. We cannot go less than 2 because if the 11927 * link is bandwidth dominated, doubling the window size 11928 * during slow start means doubling the RTT. We want to be 11929 * more conservative when we reinitialize our estimates. 3 11930 * is just a convenient number. 11931 */ 11932 sa = m << 3; 11933 sv = m << 1; 11934 } 11935 if (sv < TCP_SD_MIN) { 11936 /* 11937 * We do not know that if sa captures the delay ACK 11938 * effect as in a long train of segments, a receiver 11939 * does not delay its ACKs. So set the minimum of sv 11940 * to be TCP_SD_MIN, which is default to 400 ms, twice 11941 * of BSD DATO. That means the minimum of mean 11942 * deviation is 100 ms. 11943 * 11944 */ 11945 sv = TCP_SD_MIN; 11946 } 11947 tcp->tcp_rtt_sa = sa; 11948 tcp->tcp_rtt_sd = sv; 11949 /* 11950 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11951 * 11952 * Add tcp_rexmit_interval extra in case of extreme environment 11953 * where the algorithm fails to work. The default value of 11954 * tcp_rexmit_interval_extra should be 0. 11955 * 11956 * As we use a finer grained clock than BSD and update 11957 * RTO for every ACKs, add in another .25 of RTT to the 11958 * deviation of RTO to accomodate burstiness of 1/4 of 11959 * window size. 11960 */ 11961 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 11962 11963 if (rto > tcps->tcps_rexmit_interval_max) { 11964 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 11965 } else if (rto < tcps->tcps_rexmit_interval_min) { 11966 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 11967 } else { 11968 tcp->tcp_rto = rto; 11969 } 11970 11971 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11972 tcp->tcp_timer_backoff = 0; 11973 } 11974 11975 /* 11976 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11977 * send queue which starts at the given seq. no. 11978 * 11979 * Parameters: 11980 * tcp_t *tcp: the tcp instance pointer. 11981 * uint32_t seq: the starting seq. no of the requested segment. 11982 * int32_t *off: after the execution, *off will be the offset to 11983 * the returned mblk which points to the requested seq no. 11984 * It is the caller's responsibility to send in a non-null off. 11985 * 11986 * Return: 11987 * A mblk_t pointer pointing to the requested segment in send queue. 11988 */ 11989 static mblk_t * 11990 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11991 { 11992 int32_t cnt; 11993 mblk_t *mp; 11994 11995 /* Defensive coding. Make sure we don't send incorrect data. */ 11996 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 11997 return (NULL); 11998 11999 cnt = seq - tcp->tcp_suna; 12000 mp = tcp->tcp_xmit_head; 12001 while (cnt > 0 && mp != NULL) { 12002 cnt -= mp->b_wptr - mp->b_rptr; 12003 if (cnt < 0) { 12004 cnt += mp->b_wptr - mp->b_rptr; 12005 break; 12006 } 12007 mp = mp->b_cont; 12008 } 12009 ASSERT(mp != NULL); 12010 *off = cnt; 12011 return (mp); 12012 } 12013 12014 /* 12015 * This function handles all retransmissions if SACK is enabled for this 12016 * connection. First it calculates how many segments can be retransmitted 12017 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12018 * segments. A segment is eligible if sack_cnt for that segment is greater 12019 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12020 * all eligible segments, it checks to see if TCP can send some new segments 12021 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12022 * 12023 * Parameters: 12024 * tcp_t *tcp: the tcp structure of the connection. 12025 * uint_t *flags: in return, appropriate value will be set for 12026 * tcp_rput_data(). 12027 */ 12028 static void 12029 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12030 { 12031 notsack_blk_t *notsack_blk; 12032 int32_t usable_swnd; 12033 int32_t mss; 12034 uint32_t seg_len; 12035 mblk_t *xmit_mp; 12036 tcp_stack_t *tcps = tcp->tcp_tcps; 12037 12038 ASSERT(tcp->tcp_sack_info != NULL); 12039 ASSERT(tcp->tcp_notsack_list != NULL); 12040 ASSERT(tcp->tcp_rexmit == B_FALSE); 12041 12042 /* Defensive coding in case there is a bug... */ 12043 if (tcp->tcp_notsack_list == NULL) { 12044 return; 12045 } 12046 notsack_blk = tcp->tcp_notsack_list; 12047 mss = tcp->tcp_mss; 12048 12049 /* 12050 * Limit the num of outstanding data in the network to be 12051 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12052 */ 12053 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12054 12055 /* At least retransmit 1 MSS of data. */ 12056 if (usable_swnd <= 0) { 12057 usable_swnd = mss; 12058 } 12059 12060 /* Make sure no new RTT samples will be taken. */ 12061 tcp->tcp_csuna = tcp->tcp_snxt; 12062 12063 notsack_blk = tcp->tcp_notsack_list; 12064 while (usable_swnd > 0) { 12065 mblk_t *snxt_mp, *tmp_mp; 12066 tcp_seq begin = tcp->tcp_sack_snxt; 12067 tcp_seq end; 12068 int32_t off; 12069 12070 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12071 if (SEQ_GT(notsack_blk->end, begin) && 12072 (notsack_blk->sack_cnt >= 12073 tcps->tcps_dupack_fast_retransmit)) { 12074 end = notsack_blk->end; 12075 if (SEQ_LT(begin, notsack_blk->begin)) { 12076 begin = notsack_blk->begin; 12077 } 12078 break; 12079 } 12080 } 12081 /* 12082 * All holes are filled. Manipulate tcp_cwnd to send more 12083 * if we can. Note that after the SACK recovery, tcp_cwnd is 12084 * set to tcp_cwnd_ssthresh. 12085 */ 12086 if (notsack_blk == NULL) { 12087 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12088 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12089 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12090 ASSERT(tcp->tcp_cwnd > 0); 12091 return; 12092 } else { 12093 usable_swnd = usable_swnd / mss; 12094 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12095 MAX(usable_swnd * mss, mss); 12096 *flags |= TH_XMIT_NEEDED; 12097 return; 12098 } 12099 } 12100 12101 /* 12102 * Note that we may send more than usable_swnd allows here 12103 * because of round off, but no more than 1 MSS of data. 12104 */ 12105 seg_len = end - begin; 12106 if (seg_len > mss) 12107 seg_len = mss; 12108 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12109 ASSERT(snxt_mp != NULL); 12110 /* This should not happen. Defensive coding again... */ 12111 if (snxt_mp == NULL) { 12112 return; 12113 } 12114 12115 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12116 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12117 if (xmit_mp == NULL) 12118 return; 12119 12120 usable_swnd -= seg_len; 12121 tcp->tcp_pipe += seg_len; 12122 tcp->tcp_sack_snxt = begin + seg_len; 12123 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 12124 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12125 12126 /* 12127 * Update the send timestamp to avoid false retransmission. 12128 */ 12129 snxt_mp->b_prev = (mblk_t *)lbolt; 12130 12131 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12132 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12133 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12134 /* 12135 * Update tcp_rexmit_max to extend this SACK recovery phase. 12136 * This happens when new data sent during fast recovery is 12137 * also lost. If TCP retransmits those new data, it needs 12138 * to extend SACK recover phase to avoid starting another 12139 * fast retransmit/recovery unnecessarily. 12140 */ 12141 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12142 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12143 } 12144 } 12145 } 12146 12147 /* 12148 * This function handles policy checking at TCP level for non-hard_bound/ 12149 * detached connections. 12150 */ 12151 static boolean_t 12152 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12153 boolean_t secure, boolean_t mctl_present) 12154 { 12155 ipsec_latch_t *ipl = NULL; 12156 ipsec_action_t *act = NULL; 12157 mblk_t *data_mp; 12158 ipsec_in_t *ii; 12159 const char *reason; 12160 kstat_named_t *counter; 12161 tcp_stack_t *tcps = tcp->tcp_tcps; 12162 ipsec_stack_t *ipss; 12163 ip_stack_t *ipst; 12164 12165 ASSERT(mctl_present || !secure); 12166 12167 ASSERT((ipha == NULL && ip6h != NULL) || 12168 (ip6h == NULL && ipha != NULL)); 12169 12170 /* 12171 * We don't necessarily have an ipsec_in_act action to verify 12172 * policy because of assymetrical policy where we have only 12173 * outbound policy and no inbound policy (possible with global 12174 * policy). 12175 */ 12176 if (!secure) { 12177 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12178 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12179 return (B_TRUE); 12180 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12181 "tcp_check_policy", ipha, ip6h, secure, 12182 tcps->tcps_netstack); 12183 ipss = tcps->tcps_netstack->netstack_ipsec; 12184 12185 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12186 DROPPER(ipss, ipds_tcp_clear), 12187 &tcps->tcps_dropper); 12188 return (B_FALSE); 12189 } 12190 12191 /* 12192 * We have a secure packet. 12193 */ 12194 if (act == NULL) { 12195 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12196 "tcp_check_policy", ipha, ip6h, secure, 12197 tcps->tcps_netstack); 12198 ipss = tcps->tcps_netstack->netstack_ipsec; 12199 12200 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12201 DROPPER(ipss, ipds_tcp_secure), 12202 &tcps->tcps_dropper); 12203 return (B_FALSE); 12204 } 12205 12206 /* 12207 * XXX This whole routine is currently incorrect. ipl should 12208 * be set to the latch pointer, but is currently not set, so 12209 * we initialize it to NULL to avoid picking up random garbage. 12210 */ 12211 if (ipl == NULL) 12212 return (B_TRUE); 12213 12214 data_mp = first_mp->b_cont; 12215 12216 ii = (ipsec_in_t *)first_mp->b_rptr; 12217 12218 ipst = tcps->tcps_netstack->netstack_ip; 12219 12220 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12221 &counter, tcp->tcp_connp)) { 12222 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12223 return (B_TRUE); 12224 } 12225 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12226 "tcp inbound policy mismatch: %s, packet dropped\n", 12227 reason); 12228 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12229 12230 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12231 &tcps->tcps_dropper); 12232 return (B_FALSE); 12233 } 12234 12235 /* 12236 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12237 * retransmission after a timeout. 12238 * 12239 * To limit the number of duplicate segments, we limit the number of segment 12240 * to be sent in one time to tcp_snd_burst, the burst variable. 12241 */ 12242 static void 12243 tcp_ss_rexmit(tcp_t *tcp) 12244 { 12245 uint32_t snxt; 12246 uint32_t smax; 12247 int32_t win; 12248 int32_t mss; 12249 int32_t off; 12250 int32_t burst = tcp->tcp_snd_burst; 12251 mblk_t *snxt_mp; 12252 tcp_stack_t *tcps = tcp->tcp_tcps; 12253 12254 /* 12255 * Note that tcp_rexmit can be set even though TCP has retransmitted 12256 * all unack'ed segments. 12257 */ 12258 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12259 smax = tcp->tcp_rexmit_max; 12260 snxt = tcp->tcp_rexmit_nxt; 12261 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12262 snxt = tcp->tcp_suna; 12263 } 12264 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12265 win -= snxt - tcp->tcp_suna; 12266 mss = tcp->tcp_mss; 12267 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12268 12269 while (SEQ_LT(snxt, smax) && (win > 0) && 12270 (burst > 0) && (snxt_mp != NULL)) { 12271 mblk_t *xmit_mp; 12272 mblk_t *old_snxt_mp = snxt_mp; 12273 uint32_t cnt = mss; 12274 12275 if (win < cnt) { 12276 cnt = win; 12277 } 12278 if (SEQ_GT(snxt + cnt, smax)) { 12279 cnt = smax - snxt; 12280 } 12281 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12282 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12283 if (xmit_mp == NULL) 12284 return; 12285 12286 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12287 12288 snxt += cnt; 12289 win -= cnt; 12290 /* 12291 * Update the send timestamp to avoid false 12292 * retransmission. 12293 */ 12294 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12295 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12296 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12297 12298 tcp->tcp_rexmit_nxt = snxt; 12299 burst--; 12300 } 12301 /* 12302 * If we have transmitted all we have at the time 12303 * we started the retranmission, we can leave 12304 * the rest of the job to tcp_wput_data(). But we 12305 * need to check the send window first. If the 12306 * win is not 0, go on with tcp_wput_data(). 12307 */ 12308 if (SEQ_LT(snxt, smax) || win == 0) { 12309 return; 12310 } 12311 } 12312 /* Only call tcp_wput_data() if there is data to be sent. */ 12313 if (tcp->tcp_unsent) { 12314 tcp_wput_data(tcp, NULL, B_FALSE); 12315 } 12316 } 12317 12318 /* 12319 * Process all TCP option in SYN segment. Note that this function should 12320 * be called after tcp_adapt_ire() is called so that the necessary info 12321 * from IRE is already set in the tcp structure. 12322 * 12323 * This function sets up the correct tcp_mss value according to the 12324 * MSS option value and our header size. It also sets up the window scale 12325 * and timestamp values, and initialize SACK info blocks. But it does not 12326 * change receive window size after setting the tcp_mss value. The caller 12327 * should do the appropriate change. 12328 */ 12329 void 12330 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12331 { 12332 int options; 12333 tcp_opt_t tcpopt; 12334 uint32_t mss_max; 12335 char *tmp_tcph; 12336 tcp_stack_t *tcps = tcp->tcp_tcps; 12337 12338 tcpopt.tcp = NULL; 12339 options = tcp_parse_options(tcph, &tcpopt); 12340 12341 /* 12342 * Process MSS option. Note that MSS option value does not account 12343 * for IP or TCP options. This means that it is equal to MTU - minimum 12344 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12345 * IPv6. 12346 */ 12347 if (!(options & TCP_OPT_MSS_PRESENT)) { 12348 if (tcp->tcp_ipversion == IPV4_VERSION) 12349 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12350 else 12351 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12352 } else { 12353 if (tcp->tcp_ipversion == IPV4_VERSION) 12354 mss_max = tcps->tcps_mss_max_ipv4; 12355 else 12356 mss_max = tcps->tcps_mss_max_ipv6; 12357 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12358 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12359 else if (tcpopt.tcp_opt_mss > mss_max) 12360 tcpopt.tcp_opt_mss = mss_max; 12361 } 12362 12363 /* Process Window Scale option. */ 12364 if (options & TCP_OPT_WSCALE_PRESENT) { 12365 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12366 tcp->tcp_snd_ws_ok = B_TRUE; 12367 } else { 12368 tcp->tcp_snd_ws = B_FALSE; 12369 tcp->tcp_snd_ws_ok = B_FALSE; 12370 tcp->tcp_rcv_ws = B_FALSE; 12371 } 12372 12373 /* Process Timestamp option. */ 12374 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12375 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12376 tmp_tcph = (char *)tcp->tcp_tcph; 12377 12378 tcp->tcp_snd_ts_ok = B_TRUE; 12379 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12380 tcp->tcp_last_rcv_lbolt = lbolt64; 12381 ASSERT(OK_32PTR(tmp_tcph)); 12382 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12383 12384 /* Fill in our template header with basic timestamp option. */ 12385 tmp_tcph += tcp->tcp_tcp_hdr_len; 12386 tmp_tcph[0] = TCPOPT_NOP; 12387 tmp_tcph[1] = TCPOPT_NOP; 12388 tmp_tcph[2] = TCPOPT_TSTAMP; 12389 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12390 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12391 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12392 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12393 } else { 12394 tcp->tcp_snd_ts_ok = B_FALSE; 12395 } 12396 12397 /* 12398 * Process SACK options. If SACK is enabled for this connection, 12399 * then allocate the SACK info structure. Note the following ways 12400 * when tcp_snd_sack_ok is set to true. 12401 * 12402 * For active connection: in tcp_adapt_ire() called in 12403 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12404 * is checked. 12405 * 12406 * For passive connection: in tcp_adapt_ire() called in 12407 * tcp_accept_comm(). 12408 * 12409 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12410 * That check makes sure that if we did not send a SACK OK option, 12411 * we will not enable SACK for this connection even though the other 12412 * side sends us SACK OK option. For active connection, the SACK 12413 * info structure has already been allocated. So we need to free 12414 * it if SACK is disabled. 12415 */ 12416 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12417 (tcp->tcp_snd_sack_ok || 12418 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12419 /* This should be true only in the passive case. */ 12420 if (tcp->tcp_sack_info == NULL) { 12421 ASSERT(TCP_IS_DETACHED(tcp)); 12422 tcp->tcp_sack_info = 12423 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12424 } 12425 if (tcp->tcp_sack_info == NULL) { 12426 tcp->tcp_snd_sack_ok = B_FALSE; 12427 } else { 12428 tcp->tcp_snd_sack_ok = B_TRUE; 12429 if (tcp->tcp_snd_ts_ok) { 12430 tcp->tcp_max_sack_blk = 3; 12431 } else { 12432 tcp->tcp_max_sack_blk = 4; 12433 } 12434 } 12435 } else { 12436 /* 12437 * Resetting tcp_snd_sack_ok to B_FALSE so that 12438 * no SACK info will be used for this 12439 * connection. This assumes that SACK usage 12440 * permission is negotiated. This may need 12441 * to be changed once this is clarified. 12442 */ 12443 if (tcp->tcp_sack_info != NULL) { 12444 ASSERT(tcp->tcp_notsack_list == NULL); 12445 kmem_cache_free(tcp_sack_info_cache, 12446 tcp->tcp_sack_info); 12447 tcp->tcp_sack_info = NULL; 12448 } 12449 tcp->tcp_snd_sack_ok = B_FALSE; 12450 } 12451 12452 /* 12453 * Now we know the exact TCP/IP header length, subtract 12454 * that from tcp_mss to get our side's MSS. 12455 */ 12456 tcp->tcp_mss -= tcp->tcp_hdr_len; 12457 /* 12458 * Here we assume that the other side's header size will be equal to 12459 * our header size. We calculate the real MSS accordingly. Need to 12460 * take into additional stuffs IPsec puts in. 12461 * 12462 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12463 */ 12464 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12465 ((tcp->tcp_ipversion == IPV4_VERSION ? 12466 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12467 12468 /* 12469 * Set MSS to the smaller one of both ends of the connection. 12470 * We should not have called tcp_mss_set() before, but our 12471 * side of the MSS should have been set to a proper value 12472 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12473 * STREAM head parameters properly. 12474 * 12475 * If we have a larger-than-16-bit window but the other side 12476 * didn't want to do window scale, tcp_rwnd_set() will take 12477 * care of that. 12478 */ 12479 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss)); 12480 } 12481 12482 /* 12483 * Sends the T_CONN_IND to the listener. The caller calls this 12484 * functions via squeue to get inside the listener's perimeter 12485 * once the 3 way hand shake is done a T_CONN_IND needs to be 12486 * sent. As an optimization, the caller can call this directly 12487 * if listener's perimeter is same as eager's. 12488 */ 12489 /* ARGSUSED */ 12490 void 12491 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12492 { 12493 conn_t *lconnp = (conn_t *)arg; 12494 tcp_t *listener = lconnp->conn_tcp; 12495 tcp_t *tcp; 12496 struct T_conn_ind *conn_ind; 12497 ipaddr_t *addr_cache; 12498 boolean_t need_send_conn_ind = B_FALSE; 12499 tcp_stack_t *tcps = listener->tcp_tcps; 12500 12501 /* retrieve the eager */ 12502 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12503 ASSERT(conn_ind->OPT_offset != 0 && 12504 conn_ind->OPT_length == sizeof (intptr_t)); 12505 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12506 conn_ind->OPT_length); 12507 12508 /* 12509 * TLI/XTI applications will get confused by 12510 * sending eager as an option since it violates 12511 * the option semantics. So remove the eager as 12512 * option since TLI/XTI app doesn't need it anyway. 12513 */ 12514 if (!TCP_IS_SOCKET(listener)) { 12515 conn_ind->OPT_length = 0; 12516 conn_ind->OPT_offset = 0; 12517 } 12518 if (listener->tcp_state == TCPS_CLOSED || 12519 TCP_IS_DETACHED(listener)) { 12520 /* 12521 * If listener has closed, it would have caused a 12522 * a cleanup/blowoff to happen for the eager. We 12523 * just need to return. 12524 */ 12525 freemsg(mp); 12526 return; 12527 } 12528 12529 12530 /* 12531 * if the conn_req_q is full defer passing up the 12532 * T_CONN_IND until space is availabe after t_accept() 12533 * processing 12534 */ 12535 mutex_enter(&listener->tcp_eager_lock); 12536 12537 /* 12538 * Take the eager out, if it is in the list of droppable eagers 12539 * as we are here because the 3W handshake is over. 12540 */ 12541 MAKE_UNDROPPABLE(tcp); 12542 12543 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12544 tcp_t *tail; 12545 12546 /* 12547 * The eager already has an extra ref put in tcp_rput_data 12548 * so that it stays till accept comes back even though it 12549 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12550 */ 12551 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12552 listener->tcp_conn_req_cnt_q0--; 12553 listener->tcp_conn_req_cnt_q++; 12554 12555 /* Move from SYN_RCVD to ESTABLISHED list */ 12556 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12557 tcp->tcp_eager_prev_q0; 12558 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12559 tcp->tcp_eager_next_q0; 12560 tcp->tcp_eager_prev_q0 = NULL; 12561 tcp->tcp_eager_next_q0 = NULL; 12562 12563 /* 12564 * Insert at end of the queue because sockfs 12565 * sends down T_CONN_RES in chronological 12566 * order. Leaving the older conn indications 12567 * at front of the queue helps reducing search 12568 * time. 12569 */ 12570 tail = listener->tcp_eager_last_q; 12571 if (tail != NULL) 12572 tail->tcp_eager_next_q = tcp; 12573 else 12574 listener->tcp_eager_next_q = tcp; 12575 listener->tcp_eager_last_q = tcp; 12576 tcp->tcp_eager_next_q = NULL; 12577 /* 12578 * Delay sending up the T_conn_ind until we are 12579 * done with the eager. Once we have have sent up 12580 * the T_conn_ind, the accept can potentially complete 12581 * any time and release the refhold we have on the eager. 12582 */ 12583 need_send_conn_ind = B_TRUE; 12584 } else { 12585 /* 12586 * Defer connection on q0 and set deferred 12587 * connection bit true 12588 */ 12589 tcp->tcp_conn_def_q0 = B_TRUE; 12590 12591 /* take tcp out of q0 ... */ 12592 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12593 tcp->tcp_eager_next_q0; 12594 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12595 tcp->tcp_eager_prev_q0; 12596 12597 /* ... and place it at the end of q0 */ 12598 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12599 tcp->tcp_eager_next_q0 = listener; 12600 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12601 listener->tcp_eager_prev_q0 = tcp; 12602 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12603 } 12604 12605 /* we have timed out before */ 12606 if (tcp->tcp_syn_rcvd_timeout != 0) { 12607 tcp->tcp_syn_rcvd_timeout = 0; 12608 listener->tcp_syn_rcvd_timeout--; 12609 if (listener->tcp_syn_defense && 12610 listener->tcp_syn_rcvd_timeout <= 12611 (tcps->tcps_conn_req_max_q0 >> 5) && 12612 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12613 listener->tcp_last_rcv_lbolt)) { 12614 /* 12615 * Turn off the defense mode if we 12616 * believe the SYN attack is over. 12617 */ 12618 listener->tcp_syn_defense = B_FALSE; 12619 if (listener->tcp_ip_addr_cache) { 12620 kmem_free((void *)listener->tcp_ip_addr_cache, 12621 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12622 listener->tcp_ip_addr_cache = NULL; 12623 } 12624 } 12625 } 12626 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12627 if (addr_cache != NULL) { 12628 /* 12629 * We have finished a 3-way handshake with this 12630 * remote host. This proves the IP addr is good. 12631 * Cache it! 12632 */ 12633 addr_cache[IP_ADDR_CACHE_HASH( 12634 tcp->tcp_remote)] = tcp->tcp_remote; 12635 } 12636 mutex_exit(&listener->tcp_eager_lock); 12637 if (need_send_conn_ind) 12638 putnext(listener->tcp_rq, mp); 12639 } 12640 12641 mblk_t * 12642 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12643 uint_t *ifindexp, ip6_pkt_t *ippp) 12644 { 12645 ip_pktinfo_t *pinfo; 12646 ip6_t *ip6h; 12647 uchar_t *rptr; 12648 mblk_t *first_mp = mp; 12649 boolean_t mctl_present = B_FALSE; 12650 uint_t ifindex = 0; 12651 ip6_pkt_t ipp; 12652 uint_t ipvers; 12653 uint_t ip_hdr_len; 12654 tcp_stack_t *tcps = tcp->tcp_tcps; 12655 12656 rptr = mp->b_rptr; 12657 ASSERT(OK_32PTR(rptr)); 12658 ASSERT(tcp != NULL); 12659 ipp.ipp_fields = 0; 12660 12661 switch DB_TYPE(mp) { 12662 case M_CTL: 12663 mp = mp->b_cont; 12664 if (mp == NULL) { 12665 freemsg(first_mp); 12666 return (NULL); 12667 } 12668 if (DB_TYPE(mp) != M_DATA) { 12669 freemsg(first_mp); 12670 return (NULL); 12671 } 12672 mctl_present = B_TRUE; 12673 break; 12674 case M_DATA: 12675 break; 12676 default: 12677 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12678 freemsg(mp); 12679 return (NULL); 12680 } 12681 ipvers = IPH_HDR_VERSION(rptr); 12682 if (ipvers == IPV4_VERSION) { 12683 if (tcp == NULL) { 12684 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12685 goto done; 12686 } 12687 12688 ipp.ipp_fields |= IPPF_HOPLIMIT; 12689 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12690 12691 /* 12692 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12693 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12694 */ 12695 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12696 mctl_present) { 12697 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12698 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12699 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12700 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12701 ipp.ipp_fields |= IPPF_IFINDEX; 12702 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12703 ifindex = pinfo->ip_pkt_ifindex; 12704 } 12705 freeb(first_mp); 12706 mctl_present = B_FALSE; 12707 } 12708 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12709 } else { 12710 ip6h = (ip6_t *)rptr; 12711 12712 ASSERT(ipvers == IPV6_VERSION); 12713 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12714 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12715 ipp.ipp_hoplimit = ip6h->ip6_hops; 12716 12717 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12718 uint8_t nexthdrp; 12719 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12720 12721 /* Look for ifindex information */ 12722 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12723 ip6i_t *ip6i = (ip6i_t *)ip6h; 12724 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12725 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12726 freemsg(first_mp); 12727 return (NULL); 12728 } 12729 12730 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12731 ASSERT(ip6i->ip6i_ifindex != 0); 12732 ipp.ipp_fields |= IPPF_IFINDEX; 12733 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12734 ifindex = ip6i->ip6i_ifindex; 12735 } 12736 rptr = (uchar_t *)&ip6i[1]; 12737 mp->b_rptr = rptr; 12738 if (rptr == mp->b_wptr) { 12739 mblk_t *mp1; 12740 mp1 = mp->b_cont; 12741 freeb(mp); 12742 mp = mp1; 12743 rptr = mp->b_rptr; 12744 } 12745 if (MBLKL(mp) < IPV6_HDR_LEN + 12746 sizeof (tcph_t)) { 12747 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12748 freemsg(first_mp); 12749 return (NULL); 12750 } 12751 ip6h = (ip6_t *)rptr; 12752 } 12753 12754 /* 12755 * Find any potentially interesting extension headers 12756 * as well as the length of the IPv6 + extension 12757 * headers. 12758 */ 12759 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12760 /* Verify if this is a TCP packet */ 12761 if (nexthdrp != IPPROTO_TCP) { 12762 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12763 freemsg(first_mp); 12764 return (NULL); 12765 } 12766 } else { 12767 ip_hdr_len = IPV6_HDR_LEN; 12768 } 12769 } 12770 12771 done: 12772 if (ipversp != NULL) 12773 *ipversp = ipvers; 12774 if (ip_hdr_lenp != NULL) 12775 *ip_hdr_lenp = ip_hdr_len; 12776 if (ippp != NULL) 12777 *ippp = ipp; 12778 if (ifindexp != NULL) 12779 *ifindexp = ifindex; 12780 if (mctl_present) { 12781 freeb(first_mp); 12782 } 12783 return (mp); 12784 } 12785 12786 /* 12787 * Handle M_DATA messages from IP. Its called directly from IP via 12788 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12789 * in this path. 12790 * 12791 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12792 * v4 and v6), we are called through tcp_input() and a M_CTL can 12793 * be present for options but tcp_find_pktinfo() deals with it. We 12794 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12795 * 12796 * The first argument is always the connp/tcp to which the mp belongs. 12797 * There are no exceptions to this rule. The caller has already put 12798 * a reference on this connp/tcp and once tcp_rput_data() returns, 12799 * the squeue will do the refrele. 12800 * 12801 * The TH_SYN for the listener directly go to tcp_conn_request via 12802 * squeue. 12803 * 12804 * sqp: NULL = recursive, sqp != NULL means called from squeue 12805 */ 12806 void 12807 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12808 { 12809 int32_t bytes_acked; 12810 int32_t gap; 12811 mblk_t *mp1; 12812 uint_t flags; 12813 uint32_t new_swnd = 0; 12814 uchar_t *iphdr; 12815 uchar_t *rptr; 12816 int32_t rgap; 12817 uint32_t seg_ack; 12818 int seg_len; 12819 uint_t ip_hdr_len; 12820 uint32_t seg_seq; 12821 tcph_t *tcph; 12822 int urp; 12823 tcp_opt_t tcpopt; 12824 uint_t ipvers; 12825 ip6_pkt_t ipp; 12826 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12827 uint32_t cwnd; 12828 uint32_t add; 12829 int npkt; 12830 int mss; 12831 conn_t *connp = (conn_t *)arg; 12832 squeue_t *sqp = (squeue_t *)arg2; 12833 tcp_t *tcp = connp->conn_tcp; 12834 tcp_stack_t *tcps = tcp->tcp_tcps; 12835 12836 /* 12837 * RST from fused tcp loopback peer should trigger an unfuse. 12838 */ 12839 if (tcp->tcp_fused) { 12840 TCP_STAT(tcps, tcp_fusion_aborted); 12841 tcp_unfuse(tcp); 12842 } 12843 12844 iphdr = mp->b_rptr; 12845 rptr = mp->b_rptr; 12846 ASSERT(OK_32PTR(rptr)); 12847 12848 /* 12849 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12850 * processing here. For rest call tcp_find_pktinfo to fill up the 12851 * necessary information. 12852 */ 12853 if (IPCL_IS_TCP4(connp)) { 12854 ipvers = IPV4_VERSION; 12855 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12856 } else { 12857 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12858 NULL, &ipp); 12859 if (mp == NULL) { 12860 TCP_STAT(tcps, tcp_rput_v6_error); 12861 return; 12862 } 12863 iphdr = mp->b_rptr; 12864 rptr = mp->b_rptr; 12865 } 12866 ASSERT(DB_TYPE(mp) == M_DATA); 12867 12868 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12869 seg_seq = ABE32_TO_U32(tcph->th_seq); 12870 seg_ack = ABE32_TO_U32(tcph->th_ack); 12871 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12872 seg_len = (int)(mp->b_wptr - rptr) - 12873 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12874 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12875 do { 12876 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12877 (uintptr_t)INT_MAX); 12878 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12879 } while ((mp1 = mp1->b_cont) != NULL && 12880 mp1->b_datap->db_type == M_DATA); 12881 } 12882 12883 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12884 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12885 seg_len, tcph); 12886 return; 12887 } 12888 12889 if (sqp != NULL) { 12890 /* 12891 * This is the correct place to update tcp_last_recv_time. Note 12892 * that it is also updated for tcp structure that belongs to 12893 * global and listener queues which do not really need updating. 12894 * But that should not cause any harm. And it is updated for 12895 * all kinds of incoming segments, not only for data segments. 12896 */ 12897 tcp->tcp_last_recv_time = lbolt; 12898 } 12899 12900 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12901 12902 BUMP_LOCAL(tcp->tcp_ibsegs); 12903 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 12904 12905 if ((flags & TH_URG) && sqp != NULL) { 12906 /* 12907 * TCP can't handle urgent pointers that arrive before 12908 * the connection has been accept()ed since it can't 12909 * buffer OOB data. Discard segment if this happens. 12910 * 12911 * Nor can it reassemble urgent pointers, so discard 12912 * if it's not the next segment expected. 12913 * 12914 * Otherwise, collapse chain into one mblk (discard if 12915 * that fails). This makes sure the headers, retransmitted 12916 * data, and new data all are in the same mblk. 12917 */ 12918 ASSERT(mp != NULL); 12919 if (tcp->tcp_listener || !pullupmsg(mp, -1)) { 12920 freemsg(mp); 12921 return; 12922 } 12923 /* Update pointers into message */ 12924 iphdr = rptr = mp->b_rptr; 12925 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12926 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12927 /* 12928 * Since we can't handle any data with this urgent 12929 * pointer that is out of sequence, we expunge 12930 * the data. This allows us to still register 12931 * the urgent mark and generate the M_PCSIG, 12932 * which we can do. 12933 */ 12934 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12935 seg_len = 0; 12936 } 12937 } 12938 12939 switch (tcp->tcp_state) { 12940 case TCPS_SYN_SENT: 12941 if (flags & TH_ACK) { 12942 /* 12943 * Note that our stack cannot send data before a 12944 * connection is established, therefore the 12945 * following check is valid. Otherwise, it has 12946 * to be changed. 12947 */ 12948 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12949 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12950 freemsg(mp); 12951 if (flags & TH_RST) 12952 return; 12953 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12954 tcp, seg_ack, 0, TH_RST); 12955 return; 12956 } 12957 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12958 } 12959 if (flags & TH_RST) { 12960 freemsg(mp); 12961 if (flags & TH_ACK) 12962 (void) tcp_clean_death(tcp, 12963 ECONNREFUSED, 13); 12964 return; 12965 } 12966 if (!(flags & TH_SYN)) { 12967 freemsg(mp); 12968 return; 12969 } 12970 12971 /* Process all TCP options. */ 12972 tcp_process_options(tcp, tcph); 12973 /* 12974 * The following changes our rwnd to be a multiple of the 12975 * MIN(peer MSS, our MSS) for performance reason. 12976 */ 12977 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 12978 tcp->tcp_mss)); 12979 12980 /* Is the other end ECN capable? */ 12981 if (tcp->tcp_ecn_ok) { 12982 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12983 tcp->tcp_ecn_ok = B_FALSE; 12984 } 12985 } 12986 /* 12987 * Clear ECN flags because it may interfere with later 12988 * processing. 12989 */ 12990 flags &= ~(TH_ECE|TH_CWR); 12991 12992 tcp->tcp_irs = seg_seq; 12993 tcp->tcp_rack = seg_seq; 12994 tcp->tcp_rnxt = seg_seq + 1; 12995 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12996 if (!TCP_IS_DETACHED(tcp)) { 12997 /* Allocate room for SACK options if needed. */ 12998 if (tcp->tcp_snd_sack_ok) { 12999 (void) mi_set_sth_wroff(tcp->tcp_rq, 13000 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13001 (tcp->tcp_loopback ? 0 : 13002 tcps->tcps_wroff_xtra)); 13003 } else { 13004 (void) mi_set_sth_wroff(tcp->tcp_rq, 13005 tcp->tcp_hdr_len + 13006 (tcp->tcp_loopback ? 0 : 13007 tcps->tcps_wroff_xtra)); 13008 } 13009 } 13010 if (flags & TH_ACK) { 13011 /* 13012 * If we can't get the confirmation upstream, pretend 13013 * we didn't even see this one. 13014 * 13015 * XXX: how can we pretend we didn't see it if we 13016 * have updated rnxt et. al. 13017 * 13018 * For loopback we defer sending up the T_CONN_CON 13019 * until after some checks below. 13020 */ 13021 mp1 = NULL; 13022 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13023 tcp->tcp_loopback ? &mp1 : NULL)) { 13024 freemsg(mp); 13025 return; 13026 } 13027 /* SYN was acked - making progress */ 13028 if (tcp->tcp_ipversion == IPV6_VERSION) 13029 tcp->tcp_ip_forward_progress = B_TRUE; 13030 13031 /* One for the SYN */ 13032 tcp->tcp_suna = tcp->tcp_iss + 1; 13033 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13034 tcp->tcp_state = TCPS_ESTABLISHED; 13035 13036 /* 13037 * If SYN was retransmitted, need to reset all 13038 * retransmission info. This is because this 13039 * segment will be treated as a dup ACK. 13040 */ 13041 if (tcp->tcp_rexmit) { 13042 tcp->tcp_rexmit = B_FALSE; 13043 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13044 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13045 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13046 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13047 tcp->tcp_ms_we_have_waited = 0; 13048 13049 /* 13050 * Set tcp_cwnd back to 1 MSS, per 13051 * recommendation from 13052 * draft-floyd-incr-init-win-01.txt, 13053 * Increasing TCP's Initial Window. 13054 */ 13055 tcp->tcp_cwnd = tcp->tcp_mss; 13056 } 13057 13058 tcp->tcp_swl1 = seg_seq; 13059 tcp->tcp_swl2 = seg_ack; 13060 13061 new_swnd = BE16_TO_U16(tcph->th_win); 13062 tcp->tcp_swnd = new_swnd; 13063 if (new_swnd > tcp->tcp_max_swnd) 13064 tcp->tcp_max_swnd = new_swnd; 13065 13066 /* 13067 * Always send the three-way handshake ack immediately 13068 * in order to make the connection complete as soon as 13069 * possible on the accepting host. 13070 */ 13071 flags |= TH_ACK_NEEDED; 13072 13073 /* 13074 * Special case for loopback. At this point we have 13075 * received SYN-ACK from the remote endpoint. In 13076 * order to ensure that both endpoints reach the 13077 * fused state prior to any data exchange, the final 13078 * ACK needs to be sent before we indicate T_CONN_CON 13079 * to the module upstream. 13080 */ 13081 if (tcp->tcp_loopback) { 13082 mblk_t *ack_mp; 13083 13084 ASSERT(!tcp->tcp_unfusable); 13085 ASSERT(mp1 != NULL); 13086 /* 13087 * For loopback, we always get a pure SYN-ACK 13088 * and only need to send back the final ACK 13089 * with no data (this is because the other 13090 * tcp is ours and we don't do T/TCP). This 13091 * final ACK triggers the passive side to 13092 * perform fusion in ESTABLISHED state. 13093 */ 13094 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13095 if (tcp->tcp_ack_tid != 0) { 13096 (void) TCP_TIMER_CANCEL(tcp, 13097 tcp->tcp_ack_tid); 13098 tcp->tcp_ack_tid = 0; 13099 } 13100 TCP_RECORD_TRACE(tcp, ack_mp, 13101 TCP_TRACE_SEND_PKT); 13102 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13103 BUMP_LOCAL(tcp->tcp_obsegs); 13104 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13105 13106 /* Send up T_CONN_CON */ 13107 putnext(tcp->tcp_rq, mp1); 13108 13109 freemsg(mp); 13110 return; 13111 } 13112 /* 13113 * Forget fusion; we need to handle more 13114 * complex cases below. Send the deferred 13115 * T_CONN_CON message upstream and proceed 13116 * as usual. Mark this tcp as not capable 13117 * of fusion. 13118 */ 13119 TCP_STAT(tcps, tcp_fusion_unfusable); 13120 tcp->tcp_unfusable = B_TRUE; 13121 putnext(tcp->tcp_rq, mp1); 13122 } 13123 13124 /* 13125 * Check to see if there is data to be sent. If 13126 * yes, set the transmit flag. Then check to see 13127 * if received data processing needs to be done. 13128 * If not, go straight to xmit_check. This short 13129 * cut is OK as we don't support T/TCP. 13130 */ 13131 if (tcp->tcp_unsent) 13132 flags |= TH_XMIT_NEEDED; 13133 13134 if (seg_len == 0 && !(flags & TH_URG)) { 13135 freemsg(mp); 13136 goto xmit_check; 13137 } 13138 13139 flags &= ~TH_SYN; 13140 seg_seq++; 13141 break; 13142 } 13143 tcp->tcp_state = TCPS_SYN_RCVD; 13144 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13145 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13146 if (mp1) { 13147 DB_CPID(mp1) = tcp->tcp_cpid; 13148 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 13149 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13150 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13151 } 13152 freemsg(mp); 13153 return; 13154 case TCPS_SYN_RCVD: 13155 if (flags & TH_ACK) { 13156 /* 13157 * In this state, a SYN|ACK packet is either bogus 13158 * because the other side must be ACKing our SYN which 13159 * indicates it has seen the ACK for their SYN and 13160 * shouldn't retransmit it or we're crossing SYNs 13161 * on active open. 13162 */ 13163 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13164 freemsg(mp); 13165 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13166 tcp, seg_ack, 0, TH_RST); 13167 return; 13168 } 13169 /* 13170 * NOTE: RFC 793 pg. 72 says this should be 13171 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13172 * but that would mean we have an ack that ignored 13173 * our SYN. 13174 */ 13175 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13176 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13177 freemsg(mp); 13178 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13179 tcp, seg_ack, 0, TH_RST); 13180 return; 13181 } 13182 } 13183 break; 13184 case TCPS_LISTEN: 13185 /* 13186 * Only a TLI listener can come through this path when a 13187 * acceptor is going back to be a listener and a packet 13188 * for the acceptor hits the classifier. For a socket 13189 * listener, this can never happen because a listener 13190 * can never accept connection on itself and hence a 13191 * socket acceptor can not go back to being a listener. 13192 */ 13193 ASSERT(!TCP_IS_SOCKET(tcp)); 13194 /*FALLTHRU*/ 13195 case TCPS_CLOSED: 13196 case TCPS_BOUND: { 13197 conn_t *new_connp; 13198 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13199 13200 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13201 if (new_connp != NULL) { 13202 tcp_reinput(new_connp, mp, connp->conn_sqp); 13203 return; 13204 } 13205 /* We failed to classify. For now just drop the packet */ 13206 freemsg(mp); 13207 return; 13208 } 13209 case TCPS_IDLE: 13210 /* 13211 * Handle the case where the tcp_clean_death() has happened 13212 * on a connection (application hasn't closed yet) but a packet 13213 * was already queued on squeue before tcp_clean_death() 13214 * was processed. Calling tcp_clean_death() twice on same 13215 * connection can result in weird behaviour. 13216 */ 13217 freemsg(mp); 13218 return; 13219 default: 13220 break; 13221 } 13222 13223 /* 13224 * Already on the correct queue/perimeter. 13225 * If this is a detached connection and not an eager 13226 * connection hanging off a listener then new data 13227 * (past the FIN) will cause a reset. 13228 * We do a special check here where it 13229 * is out of the main line, rather than check 13230 * if we are detached every time we see new 13231 * data down below. 13232 */ 13233 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13234 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13235 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13236 TCP_RECORD_TRACE(tcp, 13237 mp, TCP_TRACE_RECV_PKT); 13238 13239 freemsg(mp); 13240 /* 13241 * This could be an SSL closure alert. We're detached so just 13242 * acknowledge it this last time. 13243 */ 13244 if (tcp->tcp_kssl_ctx != NULL) { 13245 kssl_release_ctx(tcp->tcp_kssl_ctx); 13246 tcp->tcp_kssl_ctx = NULL; 13247 13248 tcp->tcp_rnxt += seg_len; 13249 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13250 flags |= TH_ACK_NEEDED; 13251 goto ack_check; 13252 } 13253 13254 tcp_xmit_ctl("new data when detached", tcp, 13255 tcp->tcp_snxt, 0, TH_RST); 13256 (void) tcp_clean_death(tcp, EPROTO, 12); 13257 return; 13258 } 13259 13260 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13261 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13262 new_swnd = BE16_TO_U16(tcph->th_win) << 13263 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13264 mss = tcp->tcp_mss; 13265 13266 if (tcp->tcp_snd_ts_ok) { 13267 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13268 /* 13269 * This segment is not acceptable. 13270 * Drop it and send back an ACK. 13271 */ 13272 freemsg(mp); 13273 flags |= TH_ACK_NEEDED; 13274 goto ack_check; 13275 } 13276 } else if (tcp->tcp_snd_sack_ok) { 13277 ASSERT(tcp->tcp_sack_info != NULL); 13278 tcpopt.tcp = tcp; 13279 /* 13280 * SACK info in already updated in tcp_parse_options. Ignore 13281 * all other TCP options... 13282 */ 13283 (void) tcp_parse_options(tcph, &tcpopt); 13284 } 13285 try_again:; 13286 gap = seg_seq - tcp->tcp_rnxt; 13287 rgap = tcp->tcp_rwnd - (gap + seg_len); 13288 /* 13289 * gap is the amount of sequence space between what we expect to see 13290 * and what we got for seg_seq. A positive value for gap means 13291 * something got lost. A negative value means we got some old stuff. 13292 */ 13293 if (gap < 0) { 13294 /* Old stuff present. Is the SYN in there? */ 13295 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13296 (seg_len != 0)) { 13297 flags &= ~TH_SYN; 13298 seg_seq++; 13299 urp--; 13300 /* Recompute the gaps after noting the SYN. */ 13301 goto try_again; 13302 } 13303 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13304 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13305 (seg_len > -gap ? -gap : seg_len)); 13306 /* Remove the old stuff from seg_len. */ 13307 seg_len += gap; 13308 /* 13309 * Anything left? 13310 * Make sure to check for unack'd FIN when rest of data 13311 * has been previously ack'd. 13312 */ 13313 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13314 /* 13315 * Resets are only valid if they lie within our offered 13316 * window. If the RST bit is set, we just ignore this 13317 * segment. 13318 */ 13319 if (flags & TH_RST) { 13320 freemsg(mp); 13321 return; 13322 } 13323 13324 /* 13325 * The arriving of dup data packets indicate that we 13326 * may have postponed an ack for too long, or the other 13327 * side's RTT estimate is out of shape. Start acking 13328 * more often. 13329 */ 13330 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13331 tcp->tcp_rack_cnt >= 1 && 13332 tcp->tcp_rack_abs_max > 2) { 13333 tcp->tcp_rack_abs_max--; 13334 } 13335 tcp->tcp_rack_cur_max = 1; 13336 13337 /* 13338 * This segment is "unacceptable". None of its 13339 * sequence space lies within our advertized window. 13340 * 13341 * Adjust seg_len to the original value for tracing. 13342 */ 13343 seg_len -= gap; 13344 if (tcp->tcp_debug) { 13345 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13346 "tcp_rput: unacceptable, gap %d, rgap %d, " 13347 "flags 0x%x, seg_seq %u, seg_ack %u, " 13348 "seg_len %d, rnxt %u, snxt %u, %s", 13349 gap, rgap, flags, seg_seq, seg_ack, 13350 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13351 tcp_display(tcp, NULL, 13352 DISP_ADDR_AND_PORT)); 13353 } 13354 13355 /* 13356 * Arrange to send an ACK in response to the 13357 * unacceptable segment per RFC 793 page 69. There 13358 * is only one small difference between ours and the 13359 * acceptability test in the RFC - we accept ACK-only 13360 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13361 * will be generated. 13362 * 13363 * Note that we have to ACK an ACK-only packet at least 13364 * for stacks that send 0-length keep-alives with 13365 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13366 * section 4.2.3.6. As long as we don't ever generate 13367 * an unacceptable packet in response to an incoming 13368 * packet that is unacceptable, it should not cause 13369 * "ACK wars". 13370 */ 13371 flags |= TH_ACK_NEEDED; 13372 13373 /* 13374 * Continue processing this segment in order to use the 13375 * ACK information it contains, but skip all other 13376 * sequence-number processing. Processing the ACK 13377 * information is necessary in order to 13378 * re-synchronize connections that may have lost 13379 * synchronization. 13380 * 13381 * We clear seg_len and flag fields related to 13382 * sequence number processing as they are not 13383 * to be trusted for an unacceptable segment. 13384 */ 13385 seg_len = 0; 13386 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13387 goto process_ack; 13388 } 13389 13390 /* Fix seg_seq, and chew the gap off the front. */ 13391 seg_seq = tcp->tcp_rnxt; 13392 urp += gap; 13393 do { 13394 mblk_t *mp2; 13395 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13396 (uintptr_t)UINT_MAX); 13397 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13398 if (gap > 0) { 13399 mp->b_rptr = mp->b_wptr - gap; 13400 break; 13401 } 13402 mp2 = mp; 13403 mp = mp->b_cont; 13404 freeb(mp2); 13405 } while (gap < 0); 13406 /* 13407 * If the urgent data has already been acknowledged, we 13408 * should ignore TH_URG below 13409 */ 13410 if (urp < 0) 13411 flags &= ~TH_URG; 13412 } 13413 /* 13414 * rgap is the amount of stuff received out of window. A negative 13415 * value is the amount out of window. 13416 */ 13417 if (rgap < 0) { 13418 mblk_t *mp2; 13419 13420 if (tcp->tcp_rwnd == 0) { 13421 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13422 } else { 13423 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13424 UPDATE_MIB(&tcps->tcps_mib, 13425 tcpInDataPastWinBytes, -rgap); 13426 } 13427 13428 /* 13429 * seg_len does not include the FIN, so if more than 13430 * just the FIN is out of window, we act like we don't 13431 * see it. (If just the FIN is out of window, rgap 13432 * will be zero and we will go ahead and acknowledge 13433 * the FIN.) 13434 */ 13435 flags &= ~TH_FIN; 13436 13437 /* Fix seg_len and make sure there is something left. */ 13438 seg_len += rgap; 13439 if (seg_len <= 0) { 13440 /* 13441 * Resets are only valid if they lie within our offered 13442 * window. If the RST bit is set, we just ignore this 13443 * segment. 13444 */ 13445 if (flags & TH_RST) { 13446 freemsg(mp); 13447 return; 13448 } 13449 13450 /* Per RFC 793, we need to send back an ACK. */ 13451 flags |= TH_ACK_NEEDED; 13452 13453 /* 13454 * Send SIGURG as soon as possible i.e. even 13455 * if the TH_URG was delivered in a window probe 13456 * packet (which will be unacceptable). 13457 * 13458 * We generate a signal if none has been generated 13459 * for this connection or if this is a new urgent 13460 * byte. Also send a zero-length "unmarked" message 13461 * to inform SIOCATMARK that this is not the mark. 13462 * 13463 * tcp_urp_last_valid is cleared when the T_exdata_ind 13464 * is sent up. This plus the check for old data 13465 * (gap >= 0) handles the wraparound of the sequence 13466 * number space without having to always track the 13467 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13468 * this max in its rcv_up variable). 13469 * 13470 * This prevents duplicate SIGURGS due to a "late" 13471 * zero-window probe when the T_EXDATA_IND has already 13472 * been sent up. 13473 */ 13474 if ((flags & TH_URG) && 13475 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13476 tcp->tcp_urp_last))) { 13477 mp1 = allocb(0, BPRI_MED); 13478 if (mp1 == NULL) { 13479 freemsg(mp); 13480 return; 13481 } 13482 if (!TCP_IS_DETACHED(tcp) && 13483 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13484 SIGURG)) { 13485 /* Try again on the rexmit. */ 13486 freemsg(mp1); 13487 freemsg(mp); 13488 return; 13489 } 13490 /* 13491 * If the next byte would be the mark 13492 * then mark with MARKNEXT else mark 13493 * with NOTMARKNEXT. 13494 */ 13495 if (gap == 0 && urp == 0) 13496 mp1->b_flag |= MSGMARKNEXT; 13497 else 13498 mp1->b_flag |= MSGNOTMARKNEXT; 13499 freemsg(tcp->tcp_urp_mark_mp); 13500 tcp->tcp_urp_mark_mp = mp1; 13501 flags |= TH_SEND_URP_MARK; 13502 tcp->tcp_urp_last_valid = B_TRUE; 13503 tcp->tcp_urp_last = urp + seg_seq; 13504 } 13505 /* 13506 * If this is a zero window probe, continue to 13507 * process the ACK part. But we need to set seg_len 13508 * to 0 to avoid data processing. Otherwise just 13509 * drop the segment and send back an ACK. 13510 */ 13511 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13512 flags &= ~(TH_SYN | TH_URG); 13513 seg_len = 0; 13514 goto process_ack; 13515 } else { 13516 freemsg(mp); 13517 goto ack_check; 13518 } 13519 } 13520 /* Pitch out of window stuff off the end. */ 13521 rgap = seg_len; 13522 mp2 = mp; 13523 do { 13524 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13525 (uintptr_t)INT_MAX); 13526 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13527 if (rgap < 0) { 13528 mp2->b_wptr += rgap; 13529 if ((mp1 = mp2->b_cont) != NULL) { 13530 mp2->b_cont = NULL; 13531 freemsg(mp1); 13532 } 13533 break; 13534 } 13535 } while ((mp2 = mp2->b_cont) != NULL); 13536 } 13537 ok:; 13538 /* 13539 * TCP should check ECN info for segments inside the window only. 13540 * Therefore the check should be done here. 13541 */ 13542 if (tcp->tcp_ecn_ok) { 13543 if (flags & TH_CWR) { 13544 tcp->tcp_ecn_echo_on = B_FALSE; 13545 } 13546 /* 13547 * Note that both ECN_CE and CWR can be set in the 13548 * same segment. In this case, we once again turn 13549 * on ECN_ECHO. 13550 */ 13551 if (tcp->tcp_ipversion == IPV4_VERSION) { 13552 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13553 13554 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13555 tcp->tcp_ecn_echo_on = B_TRUE; 13556 } 13557 } else { 13558 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13559 13560 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13561 htonl(IPH_ECN_CE << 20)) { 13562 tcp->tcp_ecn_echo_on = B_TRUE; 13563 } 13564 } 13565 } 13566 13567 /* 13568 * Check whether we can update tcp_ts_recent. This test is 13569 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13570 * Extensions for High Performance: An Update", Internet Draft. 13571 */ 13572 if (tcp->tcp_snd_ts_ok && 13573 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13574 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13575 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13576 tcp->tcp_last_rcv_lbolt = lbolt64; 13577 } 13578 13579 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13580 /* 13581 * FIN in an out of order segment. We record this in 13582 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13583 * Clear the FIN so that any check on FIN flag will fail. 13584 * Remember that FIN also counts in the sequence number 13585 * space. So we need to ack out of order FIN only segments. 13586 */ 13587 if (flags & TH_FIN) { 13588 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13589 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13590 flags &= ~TH_FIN; 13591 flags |= TH_ACK_NEEDED; 13592 } 13593 if (seg_len > 0) { 13594 /* Fill in the SACK blk list. */ 13595 if (tcp->tcp_snd_sack_ok) { 13596 ASSERT(tcp->tcp_sack_info != NULL); 13597 tcp_sack_insert(tcp->tcp_sack_list, 13598 seg_seq, seg_seq + seg_len, 13599 &(tcp->tcp_num_sack_blk)); 13600 } 13601 13602 /* 13603 * Attempt reassembly and see if we have something 13604 * ready to go. 13605 */ 13606 mp = tcp_reass(tcp, mp, seg_seq); 13607 /* Always ack out of order packets */ 13608 flags |= TH_ACK_NEEDED | TH_PUSH; 13609 if (mp) { 13610 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13611 (uintptr_t)INT_MAX); 13612 seg_len = mp->b_cont ? msgdsize(mp) : 13613 (int)(mp->b_wptr - mp->b_rptr); 13614 seg_seq = tcp->tcp_rnxt; 13615 /* 13616 * A gap is filled and the seq num and len 13617 * of the gap match that of a previously 13618 * received FIN, put the FIN flag back in. 13619 */ 13620 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13621 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13622 flags |= TH_FIN; 13623 tcp->tcp_valid_bits &= 13624 ~TCP_OFO_FIN_VALID; 13625 } 13626 } else { 13627 /* 13628 * Keep going even with NULL mp. 13629 * There may be a useful ACK or something else 13630 * we don't want to miss. 13631 * 13632 * But TCP should not perform fast retransmit 13633 * because of the ack number. TCP uses 13634 * seg_len == 0 to determine if it is a pure 13635 * ACK. And this is not a pure ACK. 13636 */ 13637 seg_len = 0; 13638 ofo_seg = B_TRUE; 13639 } 13640 } 13641 } else if (seg_len > 0) { 13642 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13643 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13644 /* 13645 * If an out of order FIN was received before, and the seq 13646 * num and len of the new segment match that of the FIN, 13647 * put the FIN flag back in. 13648 */ 13649 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13650 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13651 flags |= TH_FIN; 13652 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13653 } 13654 } 13655 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13656 if (flags & TH_RST) { 13657 freemsg(mp); 13658 switch (tcp->tcp_state) { 13659 case TCPS_SYN_RCVD: 13660 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13661 break; 13662 case TCPS_ESTABLISHED: 13663 case TCPS_FIN_WAIT_1: 13664 case TCPS_FIN_WAIT_2: 13665 case TCPS_CLOSE_WAIT: 13666 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13667 break; 13668 case TCPS_CLOSING: 13669 case TCPS_LAST_ACK: 13670 (void) tcp_clean_death(tcp, 0, 16); 13671 break; 13672 default: 13673 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13674 (void) tcp_clean_death(tcp, ENXIO, 17); 13675 break; 13676 } 13677 return; 13678 } 13679 if (flags & TH_SYN) { 13680 /* 13681 * See RFC 793, Page 71 13682 * 13683 * The seq number must be in the window as it should 13684 * be "fixed" above. If it is outside window, it should 13685 * be already rejected. Note that we allow seg_seq to be 13686 * rnxt + rwnd because we want to accept 0 window probe. 13687 */ 13688 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13689 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13690 freemsg(mp); 13691 /* 13692 * If the ACK flag is not set, just use our snxt as the 13693 * seq number of the RST segment. 13694 */ 13695 if (!(flags & TH_ACK)) { 13696 seg_ack = tcp->tcp_snxt; 13697 } 13698 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13699 TH_RST|TH_ACK); 13700 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13701 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13702 return; 13703 } 13704 /* 13705 * urp could be -1 when the urp field in the packet is 0 13706 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13707 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13708 */ 13709 if (flags & TH_URG && urp >= 0) { 13710 if (!tcp->tcp_urp_last_valid || 13711 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13712 /* 13713 * If we haven't generated the signal yet for this 13714 * urgent pointer value, do it now. Also, send up a 13715 * zero-length M_DATA indicating whether or not this is 13716 * the mark. The latter is not needed when a 13717 * T_EXDATA_IND is sent up. However, if there are 13718 * allocation failures this code relies on the sender 13719 * retransmitting and the socket code for determining 13720 * the mark should not block waiting for the peer to 13721 * transmit. Thus, for simplicity we always send up the 13722 * mark indication. 13723 */ 13724 mp1 = allocb(0, BPRI_MED); 13725 if (mp1 == NULL) { 13726 freemsg(mp); 13727 return; 13728 } 13729 if (!TCP_IS_DETACHED(tcp) && 13730 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13731 /* Try again on the rexmit. */ 13732 freemsg(mp1); 13733 freemsg(mp); 13734 return; 13735 } 13736 /* 13737 * Mark with NOTMARKNEXT for now. 13738 * The code below will change this to MARKNEXT 13739 * if we are at the mark. 13740 * 13741 * If there are allocation failures (e.g. in dupmsg 13742 * below) the next time tcp_rput_data sees the urgent 13743 * segment it will send up the MSG*MARKNEXT message. 13744 */ 13745 mp1->b_flag |= MSGNOTMARKNEXT; 13746 freemsg(tcp->tcp_urp_mark_mp); 13747 tcp->tcp_urp_mark_mp = mp1; 13748 flags |= TH_SEND_URP_MARK; 13749 #ifdef DEBUG 13750 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13751 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13752 "last %x, %s", 13753 seg_seq, urp, tcp->tcp_urp_last, 13754 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13755 #endif /* DEBUG */ 13756 tcp->tcp_urp_last_valid = B_TRUE; 13757 tcp->tcp_urp_last = urp + seg_seq; 13758 } else if (tcp->tcp_urp_mark_mp != NULL) { 13759 /* 13760 * An allocation failure prevented the previous 13761 * tcp_rput_data from sending up the allocated 13762 * MSG*MARKNEXT message - send it up this time 13763 * around. 13764 */ 13765 flags |= TH_SEND_URP_MARK; 13766 } 13767 13768 /* 13769 * If the urgent byte is in this segment, make sure that it is 13770 * all by itself. This makes it much easier to deal with the 13771 * possibility of an allocation failure on the T_exdata_ind. 13772 * Note that seg_len is the number of bytes in the segment, and 13773 * urp is the offset into the segment of the urgent byte. 13774 * urp < seg_len means that the urgent byte is in this segment. 13775 */ 13776 if (urp < seg_len) { 13777 if (seg_len != 1) { 13778 uint32_t tmp_rnxt; 13779 /* 13780 * Break it up and feed it back in. 13781 * Re-attach the IP header. 13782 */ 13783 mp->b_rptr = iphdr; 13784 if (urp > 0) { 13785 /* 13786 * There is stuff before the urgent 13787 * byte. 13788 */ 13789 mp1 = dupmsg(mp); 13790 if (!mp1) { 13791 /* 13792 * Trim from urgent byte on. 13793 * The rest will come back. 13794 */ 13795 (void) adjmsg(mp, 13796 urp - seg_len); 13797 tcp_rput_data(connp, 13798 mp, NULL); 13799 return; 13800 } 13801 (void) adjmsg(mp1, urp - seg_len); 13802 /* Feed this piece back in. */ 13803 tmp_rnxt = tcp->tcp_rnxt; 13804 tcp_rput_data(connp, mp1, NULL); 13805 /* 13806 * If the data passed back in was not 13807 * processed (ie: bad ACK) sending 13808 * the remainder back in will cause a 13809 * loop. In this case, drop the 13810 * packet and let the sender try 13811 * sending a good packet. 13812 */ 13813 if (tmp_rnxt == tcp->tcp_rnxt) { 13814 freemsg(mp); 13815 return; 13816 } 13817 } 13818 if (urp != seg_len - 1) { 13819 uint32_t tmp_rnxt; 13820 /* 13821 * There is stuff after the urgent 13822 * byte. 13823 */ 13824 mp1 = dupmsg(mp); 13825 if (!mp1) { 13826 /* 13827 * Trim everything beyond the 13828 * urgent byte. The rest will 13829 * come back. 13830 */ 13831 (void) adjmsg(mp, 13832 urp + 1 - seg_len); 13833 tcp_rput_data(connp, 13834 mp, NULL); 13835 return; 13836 } 13837 (void) adjmsg(mp1, urp + 1 - seg_len); 13838 tmp_rnxt = tcp->tcp_rnxt; 13839 tcp_rput_data(connp, mp1, NULL); 13840 /* 13841 * If the data passed back in was not 13842 * processed (ie: bad ACK) sending 13843 * the remainder back in will cause a 13844 * loop. In this case, drop the 13845 * packet and let the sender try 13846 * sending a good packet. 13847 */ 13848 if (tmp_rnxt == tcp->tcp_rnxt) { 13849 freemsg(mp); 13850 return; 13851 } 13852 } 13853 tcp_rput_data(connp, mp, NULL); 13854 return; 13855 } 13856 /* 13857 * This segment contains only the urgent byte. We 13858 * have to allocate the T_exdata_ind, if we can. 13859 */ 13860 if (!tcp->tcp_urp_mp) { 13861 struct T_exdata_ind *tei; 13862 mp1 = allocb(sizeof (struct T_exdata_ind), 13863 BPRI_MED); 13864 if (!mp1) { 13865 /* 13866 * Sigh... It'll be back. 13867 * Generate any MSG*MARK message now. 13868 */ 13869 freemsg(mp); 13870 seg_len = 0; 13871 if (flags & TH_SEND_URP_MARK) { 13872 13873 13874 ASSERT(tcp->tcp_urp_mark_mp); 13875 tcp->tcp_urp_mark_mp->b_flag &= 13876 ~MSGNOTMARKNEXT; 13877 tcp->tcp_urp_mark_mp->b_flag |= 13878 MSGMARKNEXT; 13879 } 13880 goto ack_check; 13881 } 13882 mp1->b_datap->db_type = M_PROTO; 13883 tei = (struct T_exdata_ind *)mp1->b_rptr; 13884 tei->PRIM_type = T_EXDATA_IND; 13885 tei->MORE_flag = 0; 13886 mp1->b_wptr = (uchar_t *)&tei[1]; 13887 tcp->tcp_urp_mp = mp1; 13888 #ifdef DEBUG 13889 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13890 "tcp_rput: allocated exdata_ind %s", 13891 tcp_display(tcp, NULL, 13892 DISP_PORT_ONLY)); 13893 #endif /* DEBUG */ 13894 /* 13895 * There is no need to send a separate MSG*MARK 13896 * message since the T_EXDATA_IND will be sent 13897 * now. 13898 */ 13899 flags &= ~TH_SEND_URP_MARK; 13900 freemsg(tcp->tcp_urp_mark_mp); 13901 tcp->tcp_urp_mark_mp = NULL; 13902 } 13903 /* 13904 * Now we are all set. On the next putnext upstream, 13905 * tcp_urp_mp will be non-NULL and will get prepended 13906 * to what has to be this piece containing the urgent 13907 * byte. If for any reason we abort this segment below, 13908 * if it comes back, we will have this ready, or it 13909 * will get blown off in close. 13910 */ 13911 } else if (urp == seg_len) { 13912 /* 13913 * The urgent byte is the next byte after this sequence 13914 * number. If there is data it is marked with 13915 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13916 * since it is not needed. Otherwise, if the code 13917 * above just allocated a zero-length tcp_urp_mark_mp 13918 * message, that message is tagged with MSGMARKNEXT. 13919 * Sending up these MSGMARKNEXT messages makes 13920 * SIOCATMARK work correctly even though 13921 * the T_EXDATA_IND will not be sent up until the 13922 * urgent byte arrives. 13923 */ 13924 if (seg_len != 0) { 13925 flags |= TH_MARKNEXT_NEEDED; 13926 freemsg(tcp->tcp_urp_mark_mp); 13927 tcp->tcp_urp_mark_mp = NULL; 13928 flags &= ~TH_SEND_URP_MARK; 13929 } else if (tcp->tcp_urp_mark_mp != NULL) { 13930 flags |= TH_SEND_URP_MARK; 13931 tcp->tcp_urp_mark_mp->b_flag &= 13932 ~MSGNOTMARKNEXT; 13933 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13934 } 13935 #ifdef DEBUG 13936 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13937 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13938 seg_len, flags, 13939 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13940 #endif /* DEBUG */ 13941 } else { 13942 /* Data left until we hit mark */ 13943 #ifdef DEBUG 13944 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13945 "tcp_rput: URP %d bytes left, %s", 13946 urp - seg_len, tcp_display(tcp, NULL, 13947 DISP_PORT_ONLY)); 13948 #endif /* DEBUG */ 13949 } 13950 } 13951 13952 process_ack: 13953 if (!(flags & TH_ACK)) { 13954 freemsg(mp); 13955 goto xmit_check; 13956 } 13957 } 13958 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13959 13960 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13961 tcp->tcp_ip_forward_progress = B_TRUE; 13962 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13963 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13964 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13965 /* 3-way handshake complete - pass up the T_CONN_IND */ 13966 tcp_t *listener = tcp->tcp_listener; 13967 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13968 13969 tcp->tcp_tconnind_started = B_TRUE; 13970 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13971 /* 13972 * We are here means eager is fine but it can 13973 * get a TH_RST at any point between now and till 13974 * accept completes and disappear. We need to 13975 * ensure that reference to eager is valid after 13976 * we get out of eager's perimeter. So we do 13977 * an extra refhold. 13978 */ 13979 CONN_INC_REF(connp); 13980 13981 /* 13982 * The listener also exists because of the refhold 13983 * done in tcp_conn_request. Its possible that it 13984 * might have closed. We will check that once we 13985 * get inside listeners context. 13986 */ 13987 CONN_INC_REF(listener->tcp_connp); 13988 if (listener->tcp_connp->conn_sqp == 13989 connp->conn_sqp) { 13990 tcp_send_conn_ind(listener->tcp_connp, mp, 13991 listener->tcp_connp->conn_sqp); 13992 CONN_DEC_REF(listener->tcp_connp); 13993 } else if (!tcp->tcp_loopback) { 13994 squeue_fill(listener->tcp_connp->conn_sqp, mp, 13995 tcp_send_conn_ind, 13996 listener->tcp_connp, SQTAG_TCP_CONN_IND); 13997 } else { 13998 squeue_enter(listener->tcp_connp->conn_sqp, mp, 13999 tcp_send_conn_ind, listener->tcp_connp, 14000 SQTAG_TCP_CONN_IND); 14001 } 14002 } 14003 14004 if (tcp->tcp_active_open) { 14005 /* 14006 * We are seeing the final ack in the three way 14007 * hand shake of a active open'ed connection 14008 * so we must send up a T_CONN_CON 14009 */ 14010 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14011 freemsg(mp); 14012 return; 14013 } 14014 /* 14015 * Don't fuse the loopback endpoints for 14016 * simultaneous active opens. 14017 */ 14018 if (tcp->tcp_loopback) { 14019 TCP_STAT(tcps, tcp_fusion_unfusable); 14020 tcp->tcp_unfusable = B_TRUE; 14021 } 14022 } 14023 14024 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14025 bytes_acked--; 14026 /* SYN was acked - making progress */ 14027 if (tcp->tcp_ipversion == IPV6_VERSION) 14028 tcp->tcp_ip_forward_progress = B_TRUE; 14029 14030 /* 14031 * If SYN was retransmitted, need to reset all 14032 * retransmission info as this segment will be 14033 * treated as a dup ACK. 14034 */ 14035 if (tcp->tcp_rexmit) { 14036 tcp->tcp_rexmit = B_FALSE; 14037 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14038 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14039 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14040 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14041 tcp->tcp_ms_we_have_waited = 0; 14042 tcp->tcp_cwnd = mss; 14043 } 14044 14045 /* 14046 * We set the send window to zero here. 14047 * This is needed if there is data to be 14048 * processed already on the queue. 14049 * Later (at swnd_update label), the 14050 * "new_swnd > tcp_swnd" condition is satisfied 14051 * the XMIT_NEEDED flag is set in the current 14052 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14053 * called if there is already data on queue in 14054 * this state. 14055 */ 14056 tcp->tcp_swnd = 0; 14057 14058 if (new_swnd > tcp->tcp_max_swnd) 14059 tcp->tcp_max_swnd = new_swnd; 14060 tcp->tcp_swl1 = seg_seq; 14061 tcp->tcp_swl2 = seg_ack; 14062 tcp->tcp_state = TCPS_ESTABLISHED; 14063 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14064 14065 /* Fuse when both sides are in ESTABLISHED state */ 14066 if (tcp->tcp_loopback && do_tcp_fusion) 14067 tcp_fuse(tcp, iphdr, tcph); 14068 14069 } 14070 /* This code follows 4.4BSD-Lite2 mostly. */ 14071 if (bytes_acked < 0) 14072 goto est; 14073 14074 /* 14075 * If TCP is ECN capable and the congestion experience bit is 14076 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14077 * done once per window (or more loosely, per RTT). 14078 */ 14079 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14080 tcp->tcp_cwr = B_FALSE; 14081 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14082 if (!tcp->tcp_cwr) { 14083 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14084 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14085 tcp->tcp_cwnd = npkt * mss; 14086 /* 14087 * If the cwnd is 0, use the timer to clock out 14088 * new segments. This is required by the ECN spec. 14089 */ 14090 if (npkt == 0) { 14091 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14092 /* 14093 * This makes sure that when the ACK comes 14094 * back, we will increase tcp_cwnd by 1 MSS. 14095 */ 14096 tcp->tcp_cwnd_cnt = 0; 14097 } 14098 tcp->tcp_cwr = B_TRUE; 14099 /* 14100 * This marks the end of the current window of in 14101 * flight data. That is why we don't use 14102 * tcp_suna + tcp_swnd. Only data in flight can 14103 * provide ECN info. 14104 */ 14105 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14106 tcp->tcp_ecn_cwr_sent = B_FALSE; 14107 } 14108 } 14109 14110 mp1 = tcp->tcp_xmit_head; 14111 if (bytes_acked == 0) { 14112 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14113 int dupack_cnt; 14114 14115 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14116 /* 14117 * Fast retransmit. When we have seen exactly three 14118 * identical ACKs while we have unacked data 14119 * outstanding we take it as a hint that our peer 14120 * dropped something. 14121 * 14122 * If TCP is retransmitting, don't do fast retransmit. 14123 */ 14124 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14125 ! tcp->tcp_rexmit) { 14126 /* Do Limited Transmit */ 14127 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14128 tcps->tcps_dupack_fast_retransmit) { 14129 /* 14130 * RFC 3042 14131 * 14132 * What we need to do is temporarily 14133 * increase tcp_cwnd so that new 14134 * data can be sent if it is allowed 14135 * by the receive window (tcp_rwnd). 14136 * tcp_wput_data() will take care of 14137 * the rest. 14138 * 14139 * If the connection is SACK capable, 14140 * only do limited xmit when there 14141 * is SACK info. 14142 * 14143 * Note how tcp_cwnd is incremented. 14144 * The first dup ACK will increase 14145 * it by 1 MSS. The second dup ACK 14146 * will increase it by 2 MSS. This 14147 * means that only 1 new segment will 14148 * be sent for each dup ACK. 14149 */ 14150 if (tcp->tcp_unsent > 0 && 14151 (!tcp->tcp_snd_sack_ok || 14152 (tcp->tcp_snd_sack_ok && 14153 tcp->tcp_notsack_list != NULL))) { 14154 tcp->tcp_cwnd += mss << 14155 (tcp->tcp_dupack_cnt - 1); 14156 flags |= TH_LIMIT_XMIT; 14157 } 14158 } else if (dupack_cnt == 14159 tcps->tcps_dupack_fast_retransmit) { 14160 14161 /* 14162 * If we have reduced tcp_ssthresh 14163 * because of ECN, do not reduce it again 14164 * unless it is already one window of data 14165 * away. After one window of data, tcp_cwr 14166 * should then be cleared. Note that 14167 * for non ECN capable connection, tcp_cwr 14168 * should always be false. 14169 * 14170 * Adjust cwnd since the duplicate 14171 * ack indicates that a packet was 14172 * dropped (due to congestion.) 14173 */ 14174 if (!tcp->tcp_cwr) { 14175 npkt = ((tcp->tcp_snxt - 14176 tcp->tcp_suna) >> 1) / mss; 14177 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14178 mss; 14179 tcp->tcp_cwnd = (npkt + 14180 tcp->tcp_dupack_cnt) * mss; 14181 } 14182 if (tcp->tcp_ecn_ok) { 14183 tcp->tcp_cwr = B_TRUE; 14184 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14185 tcp->tcp_ecn_cwr_sent = B_FALSE; 14186 } 14187 14188 /* 14189 * We do Hoe's algorithm. Refer to her 14190 * paper "Improving the Start-up Behavior 14191 * of a Congestion Control Scheme for TCP," 14192 * appeared in SIGCOMM'96. 14193 * 14194 * Save highest seq no we have sent so far. 14195 * Be careful about the invisible FIN byte. 14196 */ 14197 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14198 (tcp->tcp_unsent == 0)) { 14199 tcp->tcp_rexmit_max = tcp->tcp_fss; 14200 } else { 14201 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14202 } 14203 14204 /* 14205 * Do not allow bursty traffic during. 14206 * fast recovery. Refer to Fall and Floyd's 14207 * paper "Simulation-based Comparisons of 14208 * Tahoe, Reno and SACK TCP" (in CCR?) 14209 * This is a best current practise. 14210 */ 14211 tcp->tcp_snd_burst = TCP_CWND_SS; 14212 14213 /* 14214 * For SACK: 14215 * Calculate tcp_pipe, which is the 14216 * estimated number of bytes in 14217 * network. 14218 * 14219 * tcp_fack is the highest sack'ed seq num 14220 * TCP has received. 14221 * 14222 * tcp_pipe is explained in the above quoted 14223 * Fall and Floyd's paper. tcp_fack is 14224 * explained in Mathis and Mahdavi's 14225 * "Forward Acknowledgment: Refining TCP 14226 * Congestion Control" in SIGCOMM '96. 14227 */ 14228 if (tcp->tcp_snd_sack_ok) { 14229 ASSERT(tcp->tcp_sack_info != NULL); 14230 if (tcp->tcp_notsack_list != NULL) { 14231 tcp->tcp_pipe = tcp->tcp_snxt - 14232 tcp->tcp_fack; 14233 tcp->tcp_sack_snxt = seg_ack; 14234 flags |= TH_NEED_SACK_REXMIT; 14235 } else { 14236 /* 14237 * Always initialize tcp_pipe 14238 * even though we don't have 14239 * any SACK info. If later 14240 * we get SACK info and 14241 * tcp_pipe is not initialized, 14242 * funny things will happen. 14243 */ 14244 tcp->tcp_pipe = 14245 tcp->tcp_cwnd_ssthresh; 14246 } 14247 } else { 14248 flags |= TH_REXMIT_NEEDED; 14249 } /* tcp_snd_sack_ok */ 14250 14251 } else { 14252 /* 14253 * Here we perform congestion 14254 * avoidance, but NOT slow start. 14255 * This is known as the Fast 14256 * Recovery Algorithm. 14257 */ 14258 if (tcp->tcp_snd_sack_ok && 14259 tcp->tcp_notsack_list != NULL) { 14260 flags |= TH_NEED_SACK_REXMIT; 14261 tcp->tcp_pipe -= mss; 14262 if (tcp->tcp_pipe < 0) 14263 tcp->tcp_pipe = 0; 14264 } else { 14265 /* 14266 * We know that one more packet has 14267 * left the pipe thus we can update 14268 * cwnd. 14269 */ 14270 cwnd = tcp->tcp_cwnd + mss; 14271 if (cwnd > tcp->tcp_cwnd_max) 14272 cwnd = tcp->tcp_cwnd_max; 14273 tcp->tcp_cwnd = cwnd; 14274 if (tcp->tcp_unsent > 0) 14275 flags |= TH_XMIT_NEEDED; 14276 } 14277 } 14278 } 14279 } else if (tcp->tcp_zero_win_probe) { 14280 /* 14281 * If the window has opened, need to arrange 14282 * to send additional data. 14283 */ 14284 if (new_swnd != 0) { 14285 /* tcp_suna != tcp_snxt */ 14286 /* Packet contains a window update */ 14287 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14288 tcp->tcp_zero_win_probe = 0; 14289 tcp->tcp_timer_backoff = 0; 14290 tcp->tcp_ms_we_have_waited = 0; 14291 14292 /* 14293 * Transmit starting with tcp_suna since 14294 * the one byte probe is not ack'ed. 14295 * If TCP has sent more than one identical 14296 * probe, tcp_rexmit will be set. That means 14297 * tcp_ss_rexmit() will send out the one 14298 * byte along with new data. Otherwise, 14299 * fake the retransmission. 14300 */ 14301 flags |= TH_XMIT_NEEDED; 14302 if (!tcp->tcp_rexmit) { 14303 tcp->tcp_rexmit = B_TRUE; 14304 tcp->tcp_dupack_cnt = 0; 14305 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14306 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14307 } 14308 } 14309 } 14310 goto swnd_update; 14311 } 14312 14313 /* 14314 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14315 * If the ACK value acks something that we have not yet sent, it might 14316 * be an old duplicate segment. Send an ACK to re-synchronize the 14317 * other side. 14318 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14319 * state is handled above, so we can always just drop the segment and 14320 * send an ACK here. 14321 * 14322 * Should we send ACKs in response to ACK only segments? 14323 */ 14324 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14325 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14326 /* drop the received segment */ 14327 freemsg(mp); 14328 14329 /* 14330 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14331 * greater than 0, check if the number of such 14332 * bogus ACks is greater than that count. If yes, 14333 * don't send back any ACK. This prevents TCP from 14334 * getting into an ACK storm if somehow an attacker 14335 * successfully spoofs an acceptable segment to our 14336 * peer. 14337 */ 14338 if (tcp_drop_ack_unsent_cnt > 0 && 14339 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14340 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14341 return; 14342 } 14343 mp = tcp_ack_mp(tcp); 14344 if (mp != NULL) { 14345 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 14346 BUMP_LOCAL(tcp->tcp_obsegs); 14347 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14348 tcp_send_data(tcp, tcp->tcp_wq, mp); 14349 } 14350 return; 14351 } 14352 14353 /* 14354 * TCP gets a new ACK, update the notsack'ed list to delete those 14355 * blocks that are covered by this ACK. 14356 */ 14357 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14358 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14359 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14360 } 14361 14362 /* 14363 * If we got an ACK after fast retransmit, check to see 14364 * if it is a partial ACK. If it is not and the congestion 14365 * window was inflated to account for the other side's 14366 * cached packets, retract it. If it is, do Hoe's algorithm. 14367 */ 14368 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14369 ASSERT(tcp->tcp_rexmit == B_FALSE); 14370 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14371 tcp->tcp_dupack_cnt = 0; 14372 /* 14373 * Restore the orig tcp_cwnd_ssthresh after 14374 * fast retransmit phase. 14375 */ 14376 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14377 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14378 } 14379 tcp->tcp_rexmit_max = seg_ack; 14380 tcp->tcp_cwnd_cnt = 0; 14381 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14382 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14383 14384 /* 14385 * Remove all notsack info to avoid confusion with 14386 * the next fast retrasnmit/recovery phase. 14387 */ 14388 if (tcp->tcp_snd_sack_ok && 14389 tcp->tcp_notsack_list != NULL) { 14390 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14391 } 14392 } else { 14393 if (tcp->tcp_snd_sack_ok && 14394 tcp->tcp_notsack_list != NULL) { 14395 flags |= TH_NEED_SACK_REXMIT; 14396 tcp->tcp_pipe -= mss; 14397 if (tcp->tcp_pipe < 0) 14398 tcp->tcp_pipe = 0; 14399 } else { 14400 /* 14401 * Hoe's algorithm: 14402 * 14403 * Retransmit the unack'ed segment and 14404 * restart fast recovery. Note that we 14405 * need to scale back tcp_cwnd to the 14406 * original value when we started fast 14407 * recovery. This is to prevent overly 14408 * aggressive behaviour in sending new 14409 * segments. 14410 */ 14411 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14412 tcps->tcps_dupack_fast_retransmit * mss; 14413 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14414 flags |= TH_REXMIT_NEEDED; 14415 } 14416 } 14417 } else { 14418 tcp->tcp_dupack_cnt = 0; 14419 if (tcp->tcp_rexmit) { 14420 /* 14421 * TCP is retranmitting. If the ACK ack's all 14422 * outstanding data, update tcp_rexmit_max and 14423 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14424 * to the correct value. 14425 * 14426 * Note that SEQ_LEQ() is used. This is to avoid 14427 * unnecessary fast retransmit caused by dup ACKs 14428 * received when TCP does slow start retransmission 14429 * after a time out. During this phase, TCP may 14430 * send out segments which are already received. 14431 * This causes dup ACKs to be sent back. 14432 */ 14433 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14434 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14435 tcp->tcp_rexmit_nxt = seg_ack; 14436 } 14437 if (seg_ack != tcp->tcp_rexmit_max) { 14438 flags |= TH_XMIT_NEEDED; 14439 } 14440 } else { 14441 tcp->tcp_rexmit = B_FALSE; 14442 tcp->tcp_xmit_zc_clean = B_FALSE; 14443 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14444 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14445 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14446 } 14447 tcp->tcp_ms_we_have_waited = 0; 14448 } 14449 } 14450 14451 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14452 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14453 tcp->tcp_suna = seg_ack; 14454 if (tcp->tcp_zero_win_probe != 0) { 14455 tcp->tcp_zero_win_probe = 0; 14456 tcp->tcp_timer_backoff = 0; 14457 } 14458 14459 /* 14460 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14461 * Note that it cannot be the SYN being ack'ed. The code flow 14462 * will not reach here. 14463 */ 14464 if (mp1 == NULL) { 14465 goto fin_acked; 14466 } 14467 14468 /* 14469 * Update the congestion window. 14470 * 14471 * If TCP is not ECN capable or TCP is ECN capable but the 14472 * congestion experience bit is not set, increase the tcp_cwnd as 14473 * usual. 14474 */ 14475 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14476 cwnd = tcp->tcp_cwnd; 14477 add = mss; 14478 14479 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14480 /* 14481 * This is to prevent an increase of less than 1 MSS of 14482 * tcp_cwnd. With partial increase, tcp_wput_data() 14483 * may send out tinygrams in order to preserve mblk 14484 * boundaries. 14485 * 14486 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14487 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14488 * increased by 1 MSS for every RTTs. 14489 */ 14490 if (tcp->tcp_cwnd_cnt <= 0) { 14491 tcp->tcp_cwnd_cnt = cwnd + add; 14492 } else { 14493 tcp->tcp_cwnd_cnt -= add; 14494 add = 0; 14495 } 14496 } 14497 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14498 } 14499 14500 /* See if the latest urgent data has been acknowledged */ 14501 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14502 SEQ_GT(seg_ack, tcp->tcp_urg)) 14503 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14504 14505 /* Can we update the RTT estimates? */ 14506 if (tcp->tcp_snd_ts_ok) { 14507 /* Ignore zero timestamp echo-reply. */ 14508 if (tcpopt.tcp_opt_ts_ecr != 0) { 14509 tcp_set_rto(tcp, (int32_t)lbolt - 14510 (int32_t)tcpopt.tcp_opt_ts_ecr); 14511 } 14512 14513 /* If needed, restart the timer. */ 14514 if (tcp->tcp_set_timer == 1) { 14515 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14516 tcp->tcp_set_timer = 0; 14517 } 14518 /* 14519 * Update tcp_csuna in case the other side stops sending 14520 * us timestamps. 14521 */ 14522 tcp->tcp_csuna = tcp->tcp_snxt; 14523 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14524 /* 14525 * An ACK sequence we haven't seen before, so get the RTT 14526 * and update the RTO. But first check if the timestamp is 14527 * valid to use. 14528 */ 14529 if ((mp1->b_next != NULL) && 14530 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14531 tcp_set_rto(tcp, (int32_t)lbolt - 14532 (int32_t)(intptr_t)mp1->b_prev); 14533 else 14534 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14535 14536 /* Remeber the last sequence to be ACKed */ 14537 tcp->tcp_csuna = seg_ack; 14538 if (tcp->tcp_set_timer == 1) { 14539 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14540 tcp->tcp_set_timer = 0; 14541 } 14542 } else { 14543 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14544 } 14545 14546 /* Eat acknowledged bytes off the xmit queue. */ 14547 for (;;) { 14548 mblk_t *mp2; 14549 uchar_t *wptr; 14550 14551 wptr = mp1->b_wptr; 14552 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14553 bytes_acked -= (int)(wptr - mp1->b_rptr); 14554 if (bytes_acked < 0) { 14555 mp1->b_rptr = wptr + bytes_acked; 14556 /* 14557 * Set a new timestamp if all the bytes timed by the 14558 * old timestamp have been ack'ed. 14559 */ 14560 if (SEQ_GT(seg_ack, 14561 (uint32_t)(uintptr_t)(mp1->b_next))) { 14562 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14563 mp1->b_next = NULL; 14564 } 14565 break; 14566 } 14567 mp1->b_next = NULL; 14568 mp1->b_prev = NULL; 14569 mp2 = mp1; 14570 mp1 = mp1->b_cont; 14571 14572 /* 14573 * This notification is required for some zero-copy 14574 * clients to maintain a copy semantic. After the data 14575 * is ack'ed, client is safe to modify or reuse the buffer. 14576 */ 14577 if (tcp->tcp_snd_zcopy_aware && 14578 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14579 tcp_zcopy_notify(tcp); 14580 freeb(mp2); 14581 if (bytes_acked == 0) { 14582 if (mp1 == NULL) { 14583 /* Everything is ack'ed, clear the tail. */ 14584 tcp->tcp_xmit_tail = NULL; 14585 /* 14586 * Cancel the timer unless we are still 14587 * waiting for an ACK for the FIN packet. 14588 */ 14589 if (tcp->tcp_timer_tid != 0 && 14590 tcp->tcp_snxt == tcp->tcp_suna) { 14591 (void) TCP_TIMER_CANCEL(tcp, 14592 tcp->tcp_timer_tid); 14593 tcp->tcp_timer_tid = 0; 14594 } 14595 goto pre_swnd_update; 14596 } 14597 if (mp2 != tcp->tcp_xmit_tail) 14598 break; 14599 tcp->tcp_xmit_tail = mp1; 14600 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14601 (uintptr_t)INT_MAX); 14602 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14603 mp1->b_rptr); 14604 break; 14605 } 14606 if (mp1 == NULL) { 14607 /* 14608 * More was acked but there is nothing more 14609 * outstanding. This means that the FIN was 14610 * just acked or that we're talking to a clown. 14611 */ 14612 fin_acked: 14613 ASSERT(tcp->tcp_fin_sent); 14614 tcp->tcp_xmit_tail = NULL; 14615 if (tcp->tcp_fin_sent) { 14616 /* FIN was acked - making progress */ 14617 if (tcp->tcp_ipversion == IPV6_VERSION && 14618 !tcp->tcp_fin_acked) 14619 tcp->tcp_ip_forward_progress = B_TRUE; 14620 tcp->tcp_fin_acked = B_TRUE; 14621 if (tcp->tcp_linger_tid != 0 && 14622 TCP_TIMER_CANCEL(tcp, 14623 tcp->tcp_linger_tid) >= 0) { 14624 tcp_stop_lingering(tcp); 14625 } 14626 } else { 14627 /* 14628 * We should never get here because 14629 * we have already checked that the 14630 * number of bytes ack'ed should be 14631 * smaller than or equal to what we 14632 * have sent so far (it is the 14633 * acceptability check of the ACK). 14634 * We can only get here if the send 14635 * queue is corrupted. 14636 * 14637 * Terminate the connection and 14638 * panic the system. It is better 14639 * for us to panic instead of 14640 * continuing to avoid other disaster. 14641 */ 14642 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14643 tcp->tcp_rnxt, TH_RST|TH_ACK); 14644 panic("Memory corruption " 14645 "detected for connection %s.", 14646 tcp_display(tcp, NULL, 14647 DISP_ADDR_AND_PORT)); 14648 /*NOTREACHED*/ 14649 } 14650 goto pre_swnd_update; 14651 } 14652 ASSERT(mp2 != tcp->tcp_xmit_tail); 14653 } 14654 if (tcp->tcp_unsent) { 14655 flags |= TH_XMIT_NEEDED; 14656 } 14657 pre_swnd_update: 14658 tcp->tcp_xmit_head = mp1; 14659 swnd_update: 14660 /* 14661 * The following check is different from most other implementations. 14662 * For bi-directional transfer, when segments are dropped, the 14663 * "normal" check will not accept a window update in those 14664 * retransmitted segemnts. Failing to do that, TCP may send out 14665 * segments which are outside receiver's window. As TCP accepts 14666 * the ack in those retransmitted segments, if the window update in 14667 * the same segment is not accepted, TCP will incorrectly calculates 14668 * that it can send more segments. This can create a deadlock 14669 * with the receiver if its window becomes zero. 14670 */ 14671 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14672 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14673 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14674 /* 14675 * The criteria for update is: 14676 * 14677 * 1. the segment acknowledges some data. Or 14678 * 2. the segment is new, i.e. it has a higher seq num. Or 14679 * 3. the segment is not old and the advertised window is 14680 * larger than the previous advertised window. 14681 */ 14682 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14683 flags |= TH_XMIT_NEEDED; 14684 tcp->tcp_swnd = new_swnd; 14685 if (new_swnd > tcp->tcp_max_swnd) 14686 tcp->tcp_max_swnd = new_swnd; 14687 tcp->tcp_swl1 = seg_seq; 14688 tcp->tcp_swl2 = seg_ack; 14689 } 14690 est: 14691 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14692 14693 switch (tcp->tcp_state) { 14694 case TCPS_FIN_WAIT_1: 14695 if (tcp->tcp_fin_acked) { 14696 tcp->tcp_state = TCPS_FIN_WAIT_2; 14697 /* 14698 * We implement the non-standard BSD/SunOS 14699 * FIN_WAIT_2 flushing algorithm. 14700 * If there is no user attached to this 14701 * TCP endpoint, then this TCP struct 14702 * could hang around forever in FIN_WAIT_2 14703 * state if the peer forgets to send us 14704 * a FIN. To prevent this, we wait only 14705 * 2*MSL (a convenient time value) for 14706 * the FIN to arrive. If it doesn't show up, 14707 * we flush the TCP endpoint. This algorithm, 14708 * though a violation of RFC-793, has worked 14709 * for over 10 years in BSD systems. 14710 * Note: SunOS 4.x waits 675 seconds before 14711 * flushing the FIN_WAIT_2 connection. 14712 */ 14713 TCP_TIMER_RESTART(tcp, 14714 tcps->tcps_fin_wait_2_flush_interval); 14715 } 14716 break; 14717 case TCPS_FIN_WAIT_2: 14718 break; /* Shutdown hook? */ 14719 case TCPS_LAST_ACK: 14720 freemsg(mp); 14721 if (tcp->tcp_fin_acked) { 14722 (void) tcp_clean_death(tcp, 0, 19); 14723 return; 14724 } 14725 goto xmit_check; 14726 case TCPS_CLOSING: 14727 if (tcp->tcp_fin_acked) { 14728 tcp->tcp_state = TCPS_TIME_WAIT; 14729 /* 14730 * Unconditionally clear the exclusive binding 14731 * bit so this TIME-WAIT connection won't 14732 * interfere with new ones. 14733 */ 14734 tcp->tcp_exclbind = 0; 14735 if (!TCP_IS_DETACHED(tcp)) { 14736 TCP_TIMER_RESTART(tcp, 14737 tcps->tcps_time_wait_interval); 14738 } else { 14739 tcp_time_wait_append(tcp); 14740 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14741 } 14742 } 14743 /*FALLTHRU*/ 14744 case TCPS_CLOSE_WAIT: 14745 freemsg(mp); 14746 goto xmit_check; 14747 default: 14748 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14749 break; 14750 } 14751 } 14752 if (flags & TH_FIN) { 14753 /* Make sure we ack the fin */ 14754 flags |= TH_ACK_NEEDED; 14755 if (!tcp->tcp_fin_rcvd) { 14756 tcp->tcp_fin_rcvd = B_TRUE; 14757 tcp->tcp_rnxt++; 14758 tcph = tcp->tcp_tcph; 14759 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14760 14761 /* 14762 * Generate the ordrel_ind at the end unless we 14763 * are an eager guy. 14764 * In the eager case tcp_rsrv will do this when run 14765 * after tcp_accept is done. 14766 */ 14767 if (tcp->tcp_listener == NULL && 14768 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14769 flags |= TH_ORDREL_NEEDED; 14770 switch (tcp->tcp_state) { 14771 case TCPS_SYN_RCVD: 14772 case TCPS_ESTABLISHED: 14773 tcp->tcp_state = TCPS_CLOSE_WAIT; 14774 /* Keepalive? */ 14775 break; 14776 case TCPS_FIN_WAIT_1: 14777 if (!tcp->tcp_fin_acked) { 14778 tcp->tcp_state = TCPS_CLOSING; 14779 break; 14780 } 14781 /* FALLTHRU */ 14782 case TCPS_FIN_WAIT_2: 14783 tcp->tcp_state = TCPS_TIME_WAIT; 14784 /* 14785 * Unconditionally clear the exclusive binding 14786 * bit so this TIME-WAIT connection won't 14787 * interfere with new ones. 14788 */ 14789 tcp->tcp_exclbind = 0; 14790 if (!TCP_IS_DETACHED(tcp)) { 14791 TCP_TIMER_RESTART(tcp, 14792 tcps->tcps_time_wait_interval); 14793 } else { 14794 tcp_time_wait_append(tcp); 14795 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14796 } 14797 if (seg_len) { 14798 /* 14799 * implies data piggybacked on FIN. 14800 * break to handle data. 14801 */ 14802 break; 14803 } 14804 freemsg(mp); 14805 goto ack_check; 14806 } 14807 } 14808 } 14809 if (mp == NULL) 14810 goto xmit_check; 14811 if (seg_len == 0) { 14812 freemsg(mp); 14813 goto xmit_check; 14814 } 14815 if (mp->b_rptr == mp->b_wptr) { 14816 /* 14817 * The header has been consumed, so we remove the 14818 * zero-length mblk here. 14819 */ 14820 mp1 = mp; 14821 mp = mp->b_cont; 14822 freeb(mp1); 14823 } 14824 tcph = tcp->tcp_tcph; 14825 tcp->tcp_rack_cnt++; 14826 { 14827 uint32_t cur_max; 14828 14829 cur_max = tcp->tcp_rack_cur_max; 14830 if (tcp->tcp_rack_cnt >= cur_max) { 14831 /* 14832 * We have more unacked data than we should - send 14833 * an ACK now. 14834 */ 14835 flags |= TH_ACK_NEEDED; 14836 cur_max++; 14837 if (cur_max > tcp->tcp_rack_abs_max) 14838 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14839 else 14840 tcp->tcp_rack_cur_max = cur_max; 14841 } else if (TCP_IS_DETACHED(tcp)) { 14842 /* We don't have an ACK timer for detached TCP. */ 14843 flags |= TH_ACK_NEEDED; 14844 } else if (seg_len < mss) { 14845 /* 14846 * If we get a segment that is less than an mss, and we 14847 * already have unacknowledged data, and the amount 14848 * unacknowledged is not a multiple of mss, then we 14849 * better generate an ACK now. Otherwise, this may be 14850 * the tail piece of a transaction, and we would rather 14851 * wait for the response. 14852 */ 14853 uint32_t udif; 14854 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14855 (uintptr_t)INT_MAX); 14856 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14857 if (udif && (udif % mss)) 14858 flags |= TH_ACK_NEEDED; 14859 else 14860 flags |= TH_ACK_TIMER_NEEDED; 14861 } else { 14862 /* Start delayed ack timer */ 14863 flags |= TH_ACK_TIMER_NEEDED; 14864 } 14865 } 14866 tcp->tcp_rnxt += seg_len; 14867 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14868 14869 /* Update SACK list */ 14870 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14871 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14872 &(tcp->tcp_num_sack_blk)); 14873 } 14874 14875 if (tcp->tcp_urp_mp) { 14876 tcp->tcp_urp_mp->b_cont = mp; 14877 mp = tcp->tcp_urp_mp; 14878 tcp->tcp_urp_mp = NULL; 14879 /* Ready for a new signal. */ 14880 tcp->tcp_urp_last_valid = B_FALSE; 14881 #ifdef DEBUG 14882 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14883 "tcp_rput: sending exdata_ind %s", 14884 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14885 #endif /* DEBUG */ 14886 } 14887 14888 /* 14889 * Check for ancillary data changes compared to last segment. 14890 */ 14891 if (tcp->tcp_ipv6_recvancillary != 0) { 14892 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14893 if (mp == NULL) 14894 return; 14895 } 14896 14897 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14898 /* 14899 * Side queue inbound data until the accept happens. 14900 * tcp_accept/tcp_rput drains this when the accept happens. 14901 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14902 * T_EXDATA_IND) it is queued on b_next. 14903 * XXX Make urgent data use this. Requires: 14904 * Removing tcp_listener check for TH_URG 14905 * Making M_PCPROTO and MARK messages skip the eager case 14906 */ 14907 14908 if (tcp->tcp_kssl_pending) { 14909 tcp_kssl_input(tcp, mp); 14910 } else { 14911 tcp_rcv_enqueue(tcp, mp, seg_len); 14912 } 14913 } else { 14914 if (mp->b_datap->db_type != M_DATA || 14915 (flags & TH_MARKNEXT_NEEDED)) { 14916 if (tcp->tcp_rcv_list != NULL) { 14917 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14918 } 14919 ASSERT(tcp->tcp_rcv_list == NULL || 14920 tcp->tcp_fused_sigurg); 14921 if (flags & TH_MARKNEXT_NEEDED) { 14922 #ifdef DEBUG 14923 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14924 "tcp_rput: sending MSGMARKNEXT %s", 14925 tcp_display(tcp, NULL, 14926 DISP_PORT_ONLY)); 14927 #endif /* DEBUG */ 14928 mp->b_flag |= MSGMARKNEXT; 14929 flags &= ~TH_MARKNEXT_NEEDED; 14930 } 14931 14932 /* Does this need SSL processing first? */ 14933 if ((tcp->tcp_kssl_ctx != NULL) && 14934 (DB_TYPE(mp) == M_DATA)) { 14935 tcp_kssl_input(tcp, mp); 14936 } else { 14937 putnext(tcp->tcp_rq, mp); 14938 if (!canputnext(tcp->tcp_rq)) 14939 tcp->tcp_rwnd -= seg_len; 14940 } 14941 } else if ((flags & (TH_PUSH|TH_FIN)) || 14942 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 14943 if (tcp->tcp_rcv_list != NULL) { 14944 /* 14945 * Enqueue the new segment first and then 14946 * call tcp_rcv_drain() to send all data 14947 * up. The other way to do this is to 14948 * send all queued data up and then call 14949 * putnext() to send the new segment up. 14950 * This way can remove the else part later 14951 * on. 14952 * 14953 * We don't this to avoid one more call to 14954 * canputnext() as tcp_rcv_drain() needs to 14955 * call canputnext(). 14956 */ 14957 tcp_rcv_enqueue(tcp, mp, seg_len); 14958 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14959 } else { 14960 /* Does this need SSL processing first? */ 14961 if ((tcp->tcp_kssl_ctx != NULL) && 14962 (DB_TYPE(mp) == M_DATA)) { 14963 tcp_kssl_input(tcp, mp); 14964 } else { 14965 putnext(tcp->tcp_rq, mp); 14966 if (!canputnext(tcp->tcp_rq)) 14967 tcp->tcp_rwnd -= seg_len; 14968 } 14969 } 14970 } else { 14971 /* 14972 * Enqueue all packets when processing an mblk 14973 * from the co queue and also enqueue normal packets. 14974 */ 14975 tcp_rcv_enqueue(tcp, mp, seg_len); 14976 } 14977 /* 14978 * Make sure the timer is running if we have data waiting 14979 * for a push bit. This provides resiliency against 14980 * implementations that do not correctly generate push bits. 14981 */ 14982 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 14983 /* 14984 * The connection may be closed at this point, so don't 14985 * do anything for a detached tcp. 14986 */ 14987 if (!TCP_IS_DETACHED(tcp)) 14988 tcp->tcp_push_tid = TCP_TIMER(tcp, 14989 tcp_push_timer, 14990 MSEC_TO_TICK(tcps->tcps_push_timer_interval)); 14991 } 14992 } 14993 xmit_check: 14994 /* Is there anything left to do? */ 14995 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14996 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 14997 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 14998 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14999 goto done; 15000 15001 /* Any transmit work to do and a non-zero window? */ 15002 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15003 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15004 if (flags & TH_REXMIT_NEEDED) { 15005 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15006 15007 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15008 if (snd_size > mss) 15009 snd_size = mss; 15010 if (snd_size > tcp->tcp_swnd) 15011 snd_size = tcp->tcp_swnd; 15012 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15013 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15014 B_TRUE); 15015 15016 if (mp1 != NULL) { 15017 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15018 tcp->tcp_csuna = tcp->tcp_snxt; 15019 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15020 UPDATE_MIB(&tcps->tcps_mib, 15021 tcpRetransBytes, snd_size); 15022 TCP_RECORD_TRACE(tcp, mp1, 15023 TCP_TRACE_SEND_PKT); 15024 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15025 } 15026 } 15027 if (flags & TH_NEED_SACK_REXMIT) { 15028 tcp_sack_rxmit(tcp, &flags); 15029 } 15030 /* 15031 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15032 * out new segment. Note that tcp_rexmit should not be 15033 * set, otherwise TH_LIMIT_XMIT should not be set. 15034 */ 15035 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15036 if (!tcp->tcp_rexmit) { 15037 tcp_wput_data(tcp, NULL, B_FALSE); 15038 } else { 15039 tcp_ss_rexmit(tcp); 15040 } 15041 } 15042 /* 15043 * Adjust tcp_cwnd back to normal value after sending 15044 * new data segments. 15045 */ 15046 if (flags & TH_LIMIT_XMIT) { 15047 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15048 /* 15049 * This will restart the timer. Restarting the 15050 * timer is used to avoid a timeout before the 15051 * limited transmitted segment's ACK gets back. 15052 */ 15053 if (tcp->tcp_xmit_head != NULL) 15054 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15055 } 15056 15057 /* Anything more to do? */ 15058 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15059 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15060 goto done; 15061 } 15062 ack_check: 15063 if (flags & TH_SEND_URP_MARK) { 15064 ASSERT(tcp->tcp_urp_mark_mp); 15065 /* 15066 * Send up any queued data and then send the mark message 15067 */ 15068 if (tcp->tcp_rcv_list != NULL) { 15069 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15070 } 15071 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15072 15073 mp1 = tcp->tcp_urp_mark_mp; 15074 tcp->tcp_urp_mark_mp = NULL; 15075 #ifdef DEBUG 15076 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15077 "tcp_rput: sending zero-length %s %s", 15078 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15079 "MSGNOTMARKNEXT"), 15080 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15081 #endif /* DEBUG */ 15082 putnext(tcp->tcp_rq, mp1); 15083 flags &= ~TH_SEND_URP_MARK; 15084 } 15085 if (flags & TH_ACK_NEEDED) { 15086 /* 15087 * Time to send an ack for some reason. 15088 */ 15089 mp1 = tcp_ack_mp(tcp); 15090 15091 if (mp1 != NULL) { 15092 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 15093 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15094 BUMP_LOCAL(tcp->tcp_obsegs); 15095 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15096 } 15097 if (tcp->tcp_ack_tid != 0) { 15098 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15099 tcp->tcp_ack_tid = 0; 15100 } 15101 } 15102 if (flags & TH_ACK_TIMER_NEEDED) { 15103 /* 15104 * Arrange for deferred ACK or push wait timeout. 15105 * Start timer if it is not already running. 15106 */ 15107 if (tcp->tcp_ack_tid == 0) { 15108 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15109 MSEC_TO_TICK(tcp->tcp_localnet ? 15110 (clock_t)tcps->tcps_local_dack_interval : 15111 (clock_t)tcps->tcps_deferred_ack_interval)); 15112 } 15113 } 15114 if (flags & TH_ORDREL_NEEDED) { 15115 /* 15116 * Send up the ordrel_ind unless we are an eager guy. 15117 * In the eager case tcp_rsrv will do this when run 15118 * after tcp_accept is done. 15119 */ 15120 ASSERT(tcp->tcp_listener == NULL); 15121 if (tcp->tcp_rcv_list != NULL) { 15122 /* 15123 * Push any mblk(s) enqueued from co processing. 15124 */ 15125 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15126 } 15127 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15128 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 15129 tcp->tcp_ordrel_done = B_TRUE; 15130 putnext(tcp->tcp_rq, mp1); 15131 if (tcp->tcp_deferred_clean_death) { 15132 /* 15133 * tcp_clean_death was deferred 15134 * for T_ORDREL_IND - do it now 15135 */ 15136 (void) tcp_clean_death(tcp, 15137 tcp->tcp_client_errno, 20); 15138 tcp->tcp_deferred_clean_death = B_FALSE; 15139 } 15140 } else { 15141 /* 15142 * Run the orderly release in the 15143 * service routine. 15144 */ 15145 qenable(tcp->tcp_rq); 15146 /* 15147 * Caveat(XXX): The machine may be so 15148 * overloaded that tcp_rsrv() is not scheduled 15149 * until after the endpoint has transitioned 15150 * to TCPS_TIME_WAIT 15151 * and tcp_time_wait_interval expires. Then 15152 * tcp_timer() will blow away state in tcp_t 15153 * and T_ORDREL_IND will never be delivered 15154 * upstream. Unlikely but potentially 15155 * a problem. 15156 */ 15157 } 15158 } 15159 done: 15160 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15161 } 15162 15163 /* 15164 * This function does PAWS protection check. Returns B_TRUE if the 15165 * segment passes the PAWS test, else returns B_FALSE. 15166 */ 15167 boolean_t 15168 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15169 { 15170 uint8_t flags; 15171 int options; 15172 uint8_t *up; 15173 15174 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15175 /* 15176 * If timestamp option is aligned nicely, get values inline, 15177 * otherwise call general routine to parse. Only do that 15178 * if timestamp is the only option. 15179 */ 15180 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15181 TCPOPT_REAL_TS_LEN && 15182 OK_32PTR((up = ((uint8_t *)tcph) + 15183 TCP_MIN_HEADER_LENGTH)) && 15184 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15185 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15186 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15187 15188 options = TCP_OPT_TSTAMP_PRESENT; 15189 } else { 15190 if (tcp->tcp_snd_sack_ok) { 15191 tcpoptp->tcp = tcp; 15192 } else { 15193 tcpoptp->tcp = NULL; 15194 } 15195 options = tcp_parse_options(tcph, tcpoptp); 15196 } 15197 15198 if (options & TCP_OPT_TSTAMP_PRESENT) { 15199 /* 15200 * Do PAWS per RFC 1323 section 4.2. Accept RST 15201 * regardless of the timestamp, page 18 RFC 1323.bis. 15202 */ 15203 if ((flags & TH_RST) == 0 && 15204 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15205 tcp->tcp_ts_recent)) { 15206 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15207 PAWS_TIMEOUT)) { 15208 /* This segment is not acceptable. */ 15209 return (B_FALSE); 15210 } else { 15211 /* 15212 * Connection has been idle for 15213 * too long. Reset the timestamp 15214 * and assume the segment is valid. 15215 */ 15216 tcp->tcp_ts_recent = 15217 tcpoptp->tcp_opt_ts_val; 15218 } 15219 } 15220 } else { 15221 /* 15222 * If we don't get a timestamp on every packet, we 15223 * figure we can't really trust 'em, so we stop sending 15224 * and parsing them. 15225 */ 15226 tcp->tcp_snd_ts_ok = B_FALSE; 15227 15228 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15229 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15230 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15231 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN); 15232 if (tcp->tcp_snd_sack_ok) { 15233 ASSERT(tcp->tcp_sack_info != NULL); 15234 tcp->tcp_max_sack_blk = 4; 15235 } 15236 } 15237 return (B_TRUE); 15238 } 15239 15240 /* 15241 * Attach ancillary data to a received TCP segments for the 15242 * ancillary pieces requested by the application that are 15243 * different than they were in the previous data segment. 15244 * 15245 * Save the "current" values once memory allocation is ok so that 15246 * when memory allocation fails we can just wait for the next data segment. 15247 */ 15248 static mblk_t * 15249 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15250 { 15251 struct T_optdata_ind *todi; 15252 int optlen; 15253 uchar_t *optptr; 15254 struct T_opthdr *toh; 15255 uint_t addflag; /* Which pieces to add */ 15256 mblk_t *mp1; 15257 15258 optlen = 0; 15259 addflag = 0; 15260 /* If app asked for pktinfo and the index has changed ... */ 15261 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15262 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15263 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15264 optlen += sizeof (struct T_opthdr) + 15265 sizeof (struct in6_pktinfo); 15266 addflag |= TCP_IPV6_RECVPKTINFO; 15267 } 15268 /* If app asked for hoplimit and it has changed ... */ 15269 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15270 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15271 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15272 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15273 addflag |= TCP_IPV6_RECVHOPLIMIT; 15274 } 15275 /* If app asked for tclass and it has changed ... */ 15276 if ((ipp->ipp_fields & IPPF_TCLASS) && 15277 ipp->ipp_tclass != tcp->tcp_recvtclass && 15278 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15279 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15280 addflag |= TCP_IPV6_RECVTCLASS; 15281 } 15282 /* 15283 * If app asked for hopbyhop headers and it has changed ... 15284 * For security labels, note that (1) security labels can't change on 15285 * a connected socket at all, (2) we're connected to at most one peer, 15286 * (3) if anything changes, then it must be some other extra option. 15287 */ 15288 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15289 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15290 (ipp->ipp_fields & IPPF_HOPOPTS), 15291 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15292 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15293 tcp->tcp_label_len; 15294 addflag |= TCP_IPV6_RECVHOPOPTS; 15295 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15296 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15297 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15298 return (mp); 15299 } 15300 /* If app asked for dst headers before routing headers ... */ 15301 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15302 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15303 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15304 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15305 optlen += sizeof (struct T_opthdr) + 15306 ipp->ipp_rtdstoptslen; 15307 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15308 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15309 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15310 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15311 return (mp); 15312 } 15313 /* If app asked for routing headers and it has changed ... */ 15314 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15315 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15316 (ipp->ipp_fields & IPPF_RTHDR), 15317 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15318 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15319 addflag |= TCP_IPV6_RECVRTHDR; 15320 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15321 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15322 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15323 return (mp); 15324 } 15325 /* If app asked for dest headers and it has changed ... */ 15326 if ((tcp->tcp_ipv6_recvancillary & 15327 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15328 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15329 (ipp->ipp_fields & IPPF_DSTOPTS), 15330 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15331 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15332 addflag |= TCP_IPV6_RECVDSTOPTS; 15333 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15334 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15335 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15336 return (mp); 15337 } 15338 15339 if (optlen == 0) { 15340 /* Nothing to add */ 15341 return (mp); 15342 } 15343 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15344 if (mp1 == NULL) { 15345 /* 15346 * Defer sending ancillary data until the next TCP segment 15347 * arrives. 15348 */ 15349 return (mp); 15350 } 15351 mp1->b_cont = mp; 15352 mp = mp1; 15353 mp->b_wptr += sizeof (*todi) + optlen; 15354 mp->b_datap->db_type = M_PROTO; 15355 todi = (struct T_optdata_ind *)mp->b_rptr; 15356 todi->PRIM_type = T_OPTDATA_IND; 15357 todi->DATA_flag = 1; /* MORE data */ 15358 todi->OPT_length = optlen; 15359 todi->OPT_offset = sizeof (*todi); 15360 optptr = (uchar_t *)&todi[1]; 15361 /* 15362 * If app asked for pktinfo and the index has changed ... 15363 * Note that the local address never changes for the connection. 15364 */ 15365 if (addflag & TCP_IPV6_RECVPKTINFO) { 15366 struct in6_pktinfo *pkti; 15367 15368 toh = (struct T_opthdr *)optptr; 15369 toh->level = IPPROTO_IPV6; 15370 toh->name = IPV6_PKTINFO; 15371 toh->len = sizeof (*toh) + sizeof (*pkti); 15372 toh->status = 0; 15373 optptr += sizeof (*toh); 15374 pkti = (struct in6_pktinfo *)optptr; 15375 if (tcp->tcp_ipversion == IPV6_VERSION) 15376 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15377 else 15378 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15379 &pkti->ipi6_addr); 15380 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15381 optptr += sizeof (*pkti); 15382 ASSERT(OK_32PTR(optptr)); 15383 /* Save as "last" value */ 15384 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15385 } 15386 /* If app asked for hoplimit and it has changed ... */ 15387 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15388 toh = (struct T_opthdr *)optptr; 15389 toh->level = IPPROTO_IPV6; 15390 toh->name = IPV6_HOPLIMIT; 15391 toh->len = sizeof (*toh) + sizeof (uint_t); 15392 toh->status = 0; 15393 optptr += sizeof (*toh); 15394 *(uint_t *)optptr = ipp->ipp_hoplimit; 15395 optptr += sizeof (uint_t); 15396 ASSERT(OK_32PTR(optptr)); 15397 /* Save as "last" value */ 15398 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15399 } 15400 /* If app asked for tclass and it has changed ... */ 15401 if (addflag & TCP_IPV6_RECVTCLASS) { 15402 toh = (struct T_opthdr *)optptr; 15403 toh->level = IPPROTO_IPV6; 15404 toh->name = IPV6_TCLASS; 15405 toh->len = sizeof (*toh) + sizeof (uint_t); 15406 toh->status = 0; 15407 optptr += sizeof (*toh); 15408 *(uint_t *)optptr = ipp->ipp_tclass; 15409 optptr += sizeof (uint_t); 15410 ASSERT(OK_32PTR(optptr)); 15411 /* Save as "last" value */ 15412 tcp->tcp_recvtclass = ipp->ipp_tclass; 15413 } 15414 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15415 toh = (struct T_opthdr *)optptr; 15416 toh->level = IPPROTO_IPV6; 15417 toh->name = IPV6_HOPOPTS; 15418 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15419 tcp->tcp_label_len; 15420 toh->status = 0; 15421 optptr += sizeof (*toh); 15422 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15423 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15424 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15425 ASSERT(OK_32PTR(optptr)); 15426 /* Save as last value */ 15427 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15428 (ipp->ipp_fields & IPPF_HOPOPTS), 15429 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15430 } 15431 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15432 toh = (struct T_opthdr *)optptr; 15433 toh->level = IPPROTO_IPV6; 15434 toh->name = IPV6_RTHDRDSTOPTS; 15435 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15436 toh->status = 0; 15437 optptr += sizeof (*toh); 15438 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15439 optptr += ipp->ipp_rtdstoptslen; 15440 ASSERT(OK_32PTR(optptr)); 15441 /* Save as last value */ 15442 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15443 &tcp->tcp_rtdstoptslen, 15444 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15445 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15446 } 15447 if (addflag & TCP_IPV6_RECVRTHDR) { 15448 toh = (struct T_opthdr *)optptr; 15449 toh->level = IPPROTO_IPV6; 15450 toh->name = IPV6_RTHDR; 15451 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15452 toh->status = 0; 15453 optptr += sizeof (*toh); 15454 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15455 optptr += ipp->ipp_rthdrlen; 15456 ASSERT(OK_32PTR(optptr)); 15457 /* Save as last value */ 15458 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15459 (ipp->ipp_fields & IPPF_RTHDR), 15460 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15461 } 15462 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15463 toh = (struct T_opthdr *)optptr; 15464 toh->level = IPPROTO_IPV6; 15465 toh->name = IPV6_DSTOPTS; 15466 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15467 toh->status = 0; 15468 optptr += sizeof (*toh); 15469 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15470 optptr += ipp->ipp_dstoptslen; 15471 ASSERT(OK_32PTR(optptr)); 15472 /* Save as last value */ 15473 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15474 (ipp->ipp_fields & IPPF_DSTOPTS), 15475 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15476 } 15477 ASSERT(optptr == mp->b_wptr); 15478 return (mp); 15479 } 15480 15481 15482 /* 15483 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15484 * or a "bad" IRE detected by tcp_adapt_ire. 15485 * We can't tell if the failure was due to the laddr or the faddr 15486 * thus we clear out all addresses and ports. 15487 */ 15488 static void 15489 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15490 { 15491 queue_t *q = tcp->tcp_rq; 15492 tcph_t *tcph; 15493 struct T_error_ack *tea; 15494 conn_t *connp = tcp->tcp_connp; 15495 15496 15497 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15498 15499 if (mp->b_cont) { 15500 freemsg(mp->b_cont); 15501 mp->b_cont = NULL; 15502 } 15503 tea = (struct T_error_ack *)mp->b_rptr; 15504 switch (tea->PRIM_type) { 15505 case T_BIND_ACK: 15506 /* 15507 * Need to unbind with classifier since we were just told that 15508 * our bind succeeded. 15509 */ 15510 tcp->tcp_hard_bound = B_FALSE; 15511 tcp->tcp_hard_binding = B_FALSE; 15512 15513 ipcl_hash_remove(connp); 15514 /* Reuse the mblk if possible */ 15515 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15516 sizeof (*tea)); 15517 mp->b_rptr = mp->b_datap->db_base; 15518 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15519 tea = (struct T_error_ack *)mp->b_rptr; 15520 tea->PRIM_type = T_ERROR_ACK; 15521 tea->TLI_error = TSYSERR; 15522 tea->UNIX_error = error; 15523 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15524 tea->ERROR_prim = T_CONN_REQ; 15525 } else { 15526 tea->ERROR_prim = O_T_BIND_REQ; 15527 } 15528 break; 15529 15530 case T_ERROR_ACK: 15531 if (tcp->tcp_state >= TCPS_SYN_SENT) 15532 tea->ERROR_prim = T_CONN_REQ; 15533 break; 15534 default: 15535 panic("tcp_bind_failed: unexpected TPI type"); 15536 /*NOTREACHED*/ 15537 } 15538 15539 tcp->tcp_state = TCPS_IDLE; 15540 if (tcp->tcp_ipversion == IPV4_VERSION) 15541 tcp->tcp_ipha->ipha_src = 0; 15542 else 15543 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15544 /* 15545 * Copy of the src addr. in tcp_t is needed since 15546 * the lookup funcs. can only look at tcp_t 15547 */ 15548 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15549 15550 tcph = tcp->tcp_tcph; 15551 tcph->th_lport[0] = 0; 15552 tcph->th_lport[1] = 0; 15553 tcp_bind_hash_remove(tcp); 15554 bzero(&connp->u_port, sizeof (connp->u_port)); 15555 /* blow away saved option results if any */ 15556 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15557 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15558 15559 conn_delete_ire(tcp->tcp_connp, NULL); 15560 putnext(q, mp); 15561 } 15562 15563 /* 15564 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15565 * messages. 15566 */ 15567 void 15568 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15569 { 15570 mblk_t *mp1; 15571 uchar_t *rptr = mp->b_rptr; 15572 queue_t *q = tcp->tcp_rq; 15573 struct T_error_ack *tea; 15574 uint32_t mss; 15575 mblk_t *syn_mp; 15576 mblk_t *mdti; 15577 mblk_t *lsoi; 15578 int retval; 15579 mblk_t *ire_mp; 15580 tcp_stack_t *tcps = tcp->tcp_tcps; 15581 15582 switch (mp->b_datap->db_type) { 15583 case M_PROTO: 15584 case M_PCPROTO: 15585 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15586 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15587 break; 15588 tea = (struct T_error_ack *)rptr; 15589 switch (tea->PRIM_type) { 15590 case T_BIND_ACK: 15591 /* 15592 * Adapt Multidata information, if any. The 15593 * following tcp_mdt_update routine will free 15594 * the message. 15595 */ 15596 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15597 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15598 b_rptr)->mdt_capab, B_TRUE); 15599 freemsg(mdti); 15600 } 15601 15602 /* 15603 * Check to update LSO information with tcp, and 15604 * tcp_lso_update routine will free the message. 15605 */ 15606 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 15607 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 15608 b_rptr)->lso_capab); 15609 freemsg(lsoi); 15610 } 15611 15612 /* Get the IRE, if we had requested for it */ 15613 ire_mp = tcp_ire_mp(mp); 15614 15615 if (tcp->tcp_hard_binding) { 15616 tcp->tcp_hard_binding = B_FALSE; 15617 tcp->tcp_hard_bound = B_TRUE; 15618 CL_INET_CONNECT(tcp); 15619 } else { 15620 if (ire_mp != NULL) 15621 freeb(ire_mp); 15622 goto after_syn_sent; 15623 } 15624 15625 retval = tcp_adapt_ire(tcp, ire_mp); 15626 if (ire_mp != NULL) 15627 freeb(ire_mp); 15628 if (retval == 0) { 15629 tcp_bind_failed(tcp, mp, 15630 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15631 ENETUNREACH : EADDRNOTAVAIL)); 15632 return; 15633 } 15634 /* 15635 * Don't let an endpoint connect to itself. 15636 * Also checked in tcp_connect() but that 15637 * check can't handle the case when the 15638 * local IP address is INADDR_ANY. 15639 */ 15640 if (tcp->tcp_ipversion == IPV4_VERSION) { 15641 if ((tcp->tcp_ipha->ipha_dst == 15642 tcp->tcp_ipha->ipha_src) && 15643 (BE16_EQL(tcp->tcp_tcph->th_lport, 15644 tcp->tcp_tcph->th_fport))) { 15645 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15646 return; 15647 } 15648 } else { 15649 if (IN6_ARE_ADDR_EQUAL( 15650 &tcp->tcp_ip6h->ip6_dst, 15651 &tcp->tcp_ip6h->ip6_src) && 15652 (BE16_EQL(tcp->tcp_tcph->th_lport, 15653 tcp->tcp_tcph->th_fport))) { 15654 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15655 return; 15656 } 15657 } 15658 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15659 /* 15660 * This should not be possible! Just for 15661 * defensive coding... 15662 */ 15663 if (tcp->tcp_state != TCPS_SYN_SENT) 15664 goto after_syn_sent; 15665 15666 if (is_system_labeled() && 15667 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 15668 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 15669 return; 15670 } 15671 15672 ASSERT(q == tcp->tcp_rq); 15673 /* 15674 * tcp_adapt_ire() does not adjust 15675 * for TCP/IP header length. 15676 */ 15677 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15678 15679 /* 15680 * Just make sure our rwnd is at 15681 * least tcp_recv_hiwat_mss * MSS 15682 * large, and round up to the nearest 15683 * MSS. 15684 * 15685 * We do the round up here because 15686 * we need to get the interface 15687 * MTU first before we can do the 15688 * round up. 15689 */ 15690 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15691 tcps->tcps_recv_hiwat_minmss * mss); 15692 q->q_hiwat = tcp->tcp_rwnd; 15693 tcp_set_ws_value(tcp); 15694 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15695 tcp->tcp_tcph->th_win); 15696 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 15697 tcp->tcp_snd_ws_ok = B_TRUE; 15698 15699 /* 15700 * Set tcp_snd_ts_ok to true 15701 * so that tcp_xmit_mp will 15702 * include the timestamp 15703 * option in the SYN segment. 15704 */ 15705 if (tcps->tcps_tstamp_always || 15706 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 15707 tcp->tcp_snd_ts_ok = B_TRUE; 15708 } 15709 15710 /* 15711 * tcp_snd_sack_ok can be set in 15712 * tcp_adapt_ire() if the sack metric 15713 * is set. So check it here also. 15714 */ 15715 if (tcps->tcps_sack_permitted == 2 || 15716 tcp->tcp_snd_sack_ok) { 15717 if (tcp->tcp_sack_info == NULL) { 15718 tcp->tcp_sack_info = 15719 kmem_cache_alloc(tcp_sack_info_cache, 15720 KM_SLEEP); 15721 } 15722 tcp->tcp_snd_sack_ok = B_TRUE; 15723 } 15724 15725 /* 15726 * Should we use ECN? Note that the current 15727 * default value (SunOS 5.9) of tcp_ecn_permitted 15728 * is 1. The reason for doing this is that there 15729 * are equipments out there that will drop ECN 15730 * enabled IP packets. Setting it to 1 avoids 15731 * compatibility problems. 15732 */ 15733 if (tcps->tcps_ecn_permitted == 2) 15734 tcp->tcp_ecn_ok = B_TRUE; 15735 15736 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15737 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15738 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15739 if (syn_mp) { 15740 cred_t *cr; 15741 pid_t pid; 15742 15743 /* 15744 * Obtain the credential from the 15745 * thread calling connect(); the credential 15746 * lives on in the second mblk which 15747 * originated from T_CONN_REQ and is echoed 15748 * with the T_BIND_ACK from ip. If none 15749 * can be found, default to the creator 15750 * of the socket. 15751 */ 15752 if (mp->b_cont == NULL || 15753 (cr = DB_CRED(mp->b_cont)) == NULL) { 15754 cr = tcp->tcp_cred; 15755 pid = tcp->tcp_cpid; 15756 } else { 15757 pid = DB_CPID(mp->b_cont); 15758 } 15759 15760 TCP_RECORD_TRACE(tcp, syn_mp, 15761 TCP_TRACE_SEND_PKT); 15762 mblk_setcred(syn_mp, cr); 15763 DB_CPID(syn_mp) = pid; 15764 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15765 } 15766 after_syn_sent: 15767 /* 15768 * A trailer mblk indicates a waiting client upstream. 15769 * We complete here the processing begun in 15770 * either tcp_bind() or tcp_connect() by passing 15771 * upstream the reply message they supplied. 15772 */ 15773 mp1 = mp; 15774 mp = mp->b_cont; 15775 freeb(mp1); 15776 if (mp) 15777 break; 15778 return; 15779 case T_ERROR_ACK: 15780 if (tcp->tcp_debug) { 15781 (void) strlog(TCP_MOD_ID, 0, 1, 15782 SL_TRACE|SL_ERROR, 15783 "tcp_rput_other: case T_ERROR_ACK, " 15784 "ERROR_prim == %d", 15785 tea->ERROR_prim); 15786 } 15787 switch (tea->ERROR_prim) { 15788 case O_T_BIND_REQ: 15789 case T_BIND_REQ: 15790 tcp_bind_failed(tcp, mp, 15791 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15792 ENETUNREACH : EADDRNOTAVAIL)); 15793 return; 15794 case T_UNBIND_REQ: 15795 tcp->tcp_hard_binding = B_FALSE; 15796 tcp->tcp_hard_bound = B_FALSE; 15797 if (mp->b_cont) { 15798 freemsg(mp->b_cont); 15799 mp->b_cont = NULL; 15800 } 15801 if (tcp->tcp_unbind_pending) 15802 tcp->tcp_unbind_pending = 0; 15803 else { 15804 /* From tcp_ip_unbind() - free */ 15805 freemsg(mp); 15806 return; 15807 } 15808 break; 15809 case T_SVR4_OPTMGMT_REQ: 15810 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15811 /* T_OPTMGMT_REQ generated by TCP */ 15812 printf("T_SVR4_OPTMGMT_REQ failed " 15813 "%d/%d - dropped (cnt %d)\n", 15814 tea->TLI_error, tea->UNIX_error, 15815 tcp->tcp_drop_opt_ack_cnt); 15816 freemsg(mp); 15817 tcp->tcp_drop_opt_ack_cnt--; 15818 return; 15819 } 15820 break; 15821 } 15822 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15823 tcp->tcp_drop_opt_ack_cnt > 0) { 15824 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15825 "- dropped (cnt %d)\n", 15826 tea->TLI_error, tea->UNIX_error, 15827 tcp->tcp_drop_opt_ack_cnt); 15828 freemsg(mp); 15829 tcp->tcp_drop_opt_ack_cnt--; 15830 return; 15831 } 15832 break; 15833 case T_OPTMGMT_ACK: 15834 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15835 /* T_OPTMGMT_REQ generated by TCP */ 15836 freemsg(mp); 15837 tcp->tcp_drop_opt_ack_cnt--; 15838 return; 15839 } 15840 break; 15841 default: 15842 break; 15843 } 15844 break; 15845 case M_FLUSH: 15846 if (*rptr & FLUSHR) 15847 flushq(q, FLUSHDATA); 15848 break; 15849 default: 15850 /* M_CTL will be directly sent to tcp_icmp_error() */ 15851 ASSERT(DB_TYPE(mp) != M_CTL); 15852 break; 15853 } 15854 /* 15855 * Make sure we set this bit before sending the ACK for 15856 * bind. Otherwise accept could possibly run and free 15857 * this tcp struct. 15858 */ 15859 putnext(q, mp); 15860 } 15861 15862 /* 15863 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15864 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15865 * tcp_rsrv() try again. 15866 */ 15867 static void 15868 tcp_ordrel_kick(void *arg) 15869 { 15870 conn_t *connp = (conn_t *)arg; 15871 tcp_t *tcp = connp->conn_tcp; 15872 15873 tcp->tcp_ordrelid = 0; 15874 tcp->tcp_timeout = B_FALSE; 15875 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15876 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15877 qenable(tcp->tcp_rq); 15878 } 15879 } 15880 15881 /* ARGSUSED */ 15882 static void 15883 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15884 { 15885 conn_t *connp = (conn_t *)arg; 15886 tcp_t *tcp = connp->conn_tcp; 15887 queue_t *q = tcp->tcp_rq; 15888 uint_t thwin; 15889 tcp_stack_t *tcps = tcp->tcp_tcps; 15890 15891 freeb(mp); 15892 15893 TCP_STAT(tcps, tcp_rsrv_calls); 15894 15895 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15896 return; 15897 } 15898 15899 if (tcp->tcp_fused) { 15900 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15901 15902 ASSERT(tcp->tcp_fused); 15903 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15904 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15905 ASSERT(!TCP_IS_DETACHED(tcp)); 15906 ASSERT(tcp->tcp_connp->conn_sqp == 15907 peer_tcp->tcp_connp->conn_sqp); 15908 15909 /* 15910 * Normally we would not get backenabled in synchronous 15911 * streams mode, but in case this happens, we need to plug 15912 * synchronous streams during our drain to prevent a race 15913 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15914 */ 15915 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15916 if (tcp->tcp_rcv_list != NULL) 15917 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15918 15919 tcp_clrqfull(peer_tcp); 15920 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15921 TCP_STAT(tcps, tcp_fusion_backenabled); 15922 return; 15923 } 15924 15925 if (canputnext(q)) { 15926 tcp->tcp_rwnd = q->q_hiwat; 15927 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15928 << tcp->tcp_rcv_ws; 15929 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15930 /* 15931 * Send back a window update immediately if TCP is above 15932 * ESTABLISHED state and the increase of the rcv window 15933 * that the other side knows is at least 1 MSS after flow 15934 * control is lifted. 15935 */ 15936 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15937 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15938 tcp_xmit_ctl(NULL, tcp, 15939 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15940 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15941 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 15942 } 15943 } 15944 /* Handle a failure to allocate a T_ORDREL_IND here */ 15945 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15946 ASSERT(tcp->tcp_listener == NULL); 15947 if (tcp->tcp_rcv_list != NULL) { 15948 (void) tcp_rcv_drain(q, tcp); 15949 } 15950 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15951 mp = mi_tpi_ordrel_ind(); 15952 if (mp) { 15953 tcp->tcp_ordrel_done = B_TRUE; 15954 putnext(q, mp); 15955 if (tcp->tcp_deferred_clean_death) { 15956 /* 15957 * tcp_clean_death was deferred for 15958 * T_ORDREL_IND - do it now 15959 */ 15960 tcp->tcp_deferred_clean_death = B_FALSE; 15961 (void) tcp_clean_death(tcp, 15962 tcp->tcp_client_errno, 22); 15963 } 15964 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15965 /* 15966 * If there isn't already a timer running 15967 * start one. Use a 4 second 15968 * timer as a fallback since it can't fail. 15969 */ 15970 tcp->tcp_timeout = B_TRUE; 15971 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15972 MSEC_TO_TICK(4000)); 15973 } 15974 } 15975 } 15976 15977 /* 15978 * The read side service routine is called mostly when we get back-enabled as a 15979 * result of flow control relief. Since we don't actually queue anything in 15980 * TCP, we have no data to send out of here. What we do is clear the receive 15981 * window, and send out a window update. 15982 * This routine is also called to drive an orderly release message upstream 15983 * if the attempt in tcp_rput failed. 15984 */ 15985 static void 15986 tcp_rsrv(queue_t *q) 15987 { 15988 conn_t *connp = Q_TO_CONN(q); 15989 tcp_t *tcp = connp->conn_tcp; 15990 mblk_t *mp; 15991 tcp_stack_t *tcps = tcp->tcp_tcps; 15992 15993 /* No code does a putq on the read side */ 15994 ASSERT(q->q_first == NULL); 15995 15996 /* Nothing to do for the default queue */ 15997 if (q == tcps->tcps_g_q) { 15998 return; 15999 } 16000 16001 mp = allocb(0, BPRI_HI); 16002 if (mp == NULL) { 16003 /* 16004 * We are under memory pressure. Return for now and we 16005 * we will be called again later. 16006 */ 16007 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16008 /* 16009 * If there isn't already a timer running 16010 * start one. Use a 4 second 16011 * timer as a fallback since it can't fail. 16012 */ 16013 tcp->tcp_timeout = B_TRUE; 16014 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16015 MSEC_TO_TICK(4000)); 16016 } 16017 return; 16018 } 16019 CONN_INC_REF(connp); 16020 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16021 SQTAG_TCP_RSRV); 16022 } 16023 16024 /* 16025 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16026 * We do not allow the receive window to shrink. After setting rwnd, 16027 * set the flow control hiwat of the stream. 16028 * 16029 * This function is called in 2 cases: 16030 * 16031 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16032 * connection (passive open) and in tcp_rput_data() for active connect. 16033 * This is called after tcp_mss_set() when the desired MSS value is known. 16034 * This makes sure that our window size is a mutiple of the other side's 16035 * MSS. 16036 * 2) Handling SO_RCVBUF option. 16037 * 16038 * It is ASSUMED that the requested size is a multiple of the current MSS. 16039 * 16040 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16041 * user requests so. 16042 */ 16043 static int 16044 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16045 { 16046 uint32_t mss = tcp->tcp_mss; 16047 uint32_t old_max_rwnd; 16048 uint32_t max_transmittable_rwnd; 16049 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16050 tcp_stack_t *tcps = tcp->tcp_tcps; 16051 16052 if (tcp->tcp_fused) { 16053 size_t sth_hiwat; 16054 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16055 16056 ASSERT(peer_tcp != NULL); 16057 /* 16058 * Record the stream head's high water mark for 16059 * this endpoint; this is used for flow-control 16060 * purposes in tcp_fuse_output(). 16061 */ 16062 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16063 if (!tcp_detached) 16064 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16065 16066 /* 16067 * In the fusion case, the maxpsz stream head value of 16068 * our peer is set according to its send buffer size 16069 * and our receive buffer size; since the latter may 16070 * have changed we need to update the peer's maxpsz. 16071 */ 16072 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16073 return (rwnd); 16074 } 16075 16076 if (tcp_detached) 16077 old_max_rwnd = tcp->tcp_rwnd; 16078 else 16079 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16080 16081 /* 16082 * Insist on a receive window that is at least 16083 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16084 * funny TCP interactions of Nagle algorithm, SWS avoidance 16085 * and delayed acknowledgement. 16086 */ 16087 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16088 16089 /* 16090 * If window size info has already been exchanged, TCP should not 16091 * shrink the window. Shrinking window is doable if done carefully. 16092 * We may add that support later. But so far there is not a real 16093 * need to do that. 16094 */ 16095 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16096 /* MSS may have changed, do a round up again. */ 16097 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16098 } 16099 16100 /* 16101 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16102 * can be applied even before the window scale option is decided. 16103 */ 16104 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16105 if (rwnd > max_transmittable_rwnd) { 16106 rwnd = max_transmittable_rwnd - 16107 (max_transmittable_rwnd % mss); 16108 if (rwnd < mss) 16109 rwnd = max_transmittable_rwnd; 16110 /* 16111 * If we're over the limit we may have to back down tcp_rwnd. 16112 * The increment below won't work for us. So we set all three 16113 * here and the increment below will have no effect. 16114 */ 16115 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16116 } 16117 if (tcp->tcp_localnet) { 16118 tcp->tcp_rack_abs_max = 16119 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16120 } else { 16121 /* 16122 * For a remote host on a different subnet (through a router), 16123 * we ack every other packet to be conforming to RFC1122. 16124 * tcp_deferred_acks_max is default to 2. 16125 */ 16126 tcp->tcp_rack_abs_max = 16127 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16128 } 16129 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16130 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16131 else 16132 tcp->tcp_rack_cur_max = 0; 16133 /* 16134 * Increment the current rwnd by the amount the maximum grew (we 16135 * can not overwrite it since we might be in the middle of a 16136 * connection.) 16137 */ 16138 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16139 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16140 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16141 tcp->tcp_cwnd_max = rwnd; 16142 16143 if (tcp_detached) 16144 return (rwnd); 16145 /* 16146 * We set the maximum receive window into rq->q_hiwat. 16147 * This is not actually used for flow control. 16148 */ 16149 tcp->tcp_rq->q_hiwat = rwnd; 16150 /* 16151 * Set the Stream head high water mark. This doesn't have to be 16152 * here, since we are simply using default values, but we would 16153 * prefer to choose these values algorithmically, with a likely 16154 * relationship to rwnd. 16155 */ 16156 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16157 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16158 return (rwnd); 16159 } 16160 16161 /* 16162 * Return SNMP stuff in buffer in mpdata. 16163 */ 16164 int 16165 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16166 { 16167 mblk_t *mpdata; 16168 mblk_t *mp_conn_ctl = NULL; 16169 mblk_t *mp_conn_tail; 16170 mblk_t *mp_attr_ctl = NULL; 16171 mblk_t *mp_attr_tail; 16172 mblk_t *mp6_conn_ctl = NULL; 16173 mblk_t *mp6_conn_tail; 16174 mblk_t *mp6_attr_ctl = NULL; 16175 mblk_t *mp6_attr_tail; 16176 struct opthdr *optp; 16177 mib2_tcpConnEntry_t tce; 16178 mib2_tcp6ConnEntry_t tce6; 16179 mib2_transportMLPEntry_t mlp; 16180 connf_t *connfp; 16181 conn_t *connp; 16182 int i; 16183 boolean_t ispriv; 16184 zoneid_t zoneid; 16185 int v4_conn_idx; 16186 int v6_conn_idx; 16187 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16188 ip_stack_t *ipst; 16189 16190 if (mpctl == NULL || 16191 (mpdata = mpctl->b_cont) == NULL || 16192 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16193 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16194 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16195 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16196 freemsg(mp_conn_ctl); 16197 freemsg(mp_attr_ctl); 16198 freemsg(mp6_conn_ctl); 16199 freemsg(mp6_attr_ctl); 16200 return (0); 16201 } 16202 16203 /* build table of connections -- need count in fixed part */ 16204 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16205 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16206 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16207 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16208 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16209 16210 ispriv = 16211 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16212 zoneid = Q_TO_CONN(q)->conn_zoneid; 16213 16214 v4_conn_idx = v6_conn_idx = 0; 16215 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16216 16217 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16218 ipst = tcps->tcps_netstack->netstack_ip; 16219 16220 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16221 16222 connp = NULL; 16223 16224 while ((connp = 16225 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16226 tcp_t *tcp; 16227 boolean_t needattr; 16228 16229 if (connp->conn_zoneid != zoneid) 16230 continue; /* not in this zone */ 16231 16232 tcp = connp->conn_tcp; 16233 UPDATE_MIB(&tcps->tcps_mib, 16234 tcpHCInSegs, tcp->tcp_ibsegs); 16235 tcp->tcp_ibsegs = 0; 16236 UPDATE_MIB(&tcps->tcps_mib, 16237 tcpHCOutSegs, tcp->tcp_obsegs); 16238 tcp->tcp_obsegs = 0; 16239 16240 tce6.tcp6ConnState = tce.tcpConnState = 16241 tcp_snmp_state(tcp); 16242 if (tce.tcpConnState == MIB2_TCP_established || 16243 tce.tcpConnState == MIB2_TCP_closeWait) 16244 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16245 16246 needattr = B_FALSE; 16247 bzero(&mlp, sizeof (mlp)); 16248 if (connp->conn_mlp_type != mlptSingle) { 16249 if (connp->conn_mlp_type == mlptShared || 16250 connp->conn_mlp_type == mlptBoth) 16251 mlp.tme_flags |= MIB2_TMEF_SHARED; 16252 if (connp->conn_mlp_type == mlptPrivate || 16253 connp->conn_mlp_type == mlptBoth) 16254 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16255 needattr = B_TRUE; 16256 } 16257 if (connp->conn_peercred != NULL) { 16258 ts_label_t *tsl; 16259 16260 tsl = crgetlabel(connp->conn_peercred); 16261 mlp.tme_doi = label2doi(tsl); 16262 mlp.tme_label = *label2bslabel(tsl); 16263 needattr = B_TRUE; 16264 } 16265 16266 /* Create a message to report on IPv6 entries */ 16267 if (tcp->tcp_ipversion == IPV6_VERSION) { 16268 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16269 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16270 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16271 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16272 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16273 /* Don't want just anybody seeing these... */ 16274 if (ispriv) { 16275 tce6.tcp6ConnEntryInfo.ce_snxt = 16276 tcp->tcp_snxt; 16277 tce6.tcp6ConnEntryInfo.ce_suna = 16278 tcp->tcp_suna; 16279 tce6.tcp6ConnEntryInfo.ce_rnxt = 16280 tcp->tcp_rnxt; 16281 tce6.tcp6ConnEntryInfo.ce_rack = 16282 tcp->tcp_rack; 16283 } else { 16284 /* 16285 * Netstat, unfortunately, uses this to 16286 * get send/receive queue sizes. How to fix? 16287 * Why not compute the difference only? 16288 */ 16289 tce6.tcp6ConnEntryInfo.ce_snxt = 16290 tcp->tcp_snxt - tcp->tcp_suna; 16291 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16292 tce6.tcp6ConnEntryInfo.ce_rnxt = 16293 tcp->tcp_rnxt - tcp->tcp_rack; 16294 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16295 } 16296 16297 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16298 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16299 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16300 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16301 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16302 16303 tce6.tcp6ConnCreationProcess = 16304 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16305 tcp->tcp_cpid; 16306 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16307 16308 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16309 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16310 16311 mlp.tme_connidx = v6_conn_idx++; 16312 if (needattr) 16313 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16314 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16315 } 16316 /* 16317 * Create an IPv4 table entry for IPv4 entries and also 16318 * for IPv6 entries which are bound to in6addr_any 16319 * but don't have IPV6_V6ONLY set. 16320 * (i.e. anything an IPv4 peer could connect to) 16321 */ 16322 if (tcp->tcp_ipversion == IPV4_VERSION || 16323 (tcp->tcp_state <= TCPS_LISTEN && 16324 !tcp->tcp_connp->conn_ipv6_v6only && 16325 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16326 if (tcp->tcp_ipversion == IPV6_VERSION) { 16327 tce.tcpConnRemAddress = INADDR_ANY; 16328 tce.tcpConnLocalAddress = INADDR_ANY; 16329 } else { 16330 tce.tcpConnRemAddress = 16331 tcp->tcp_remote; 16332 tce.tcpConnLocalAddress = 16333 tcp->tcp_ip_src; 16334 } 16335 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16336 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16337 /* Don't want just anybody seeing these... */ 16338 if (ispriv) { 16339 tce.tcpConnEntryInfo.ce_snxt = 16340 tcp->tcp_snxt; 16341 tce.tcpConnEntryInfo.ce_suna = 16342 tcp->tcp_suna; 16343 tce.tcpConnEntryInfo.ce_rnxt = 16344 tcp->tcp_rnxt; 16345 tce.tcpConnEntryInfo.ce_rack = 16346 tcp->tcp_rack; 16347 } else { 16348 /* 16349 * Netstat, unfortunately, uses this to 16350 * get send/receive queue sizes. How 16351 * to fix? 16352 * Why not compute the difference only? 16353 */ 16354 tce.tcpConnEntryInfo.ce_snxt = 16355 tcp->tcp_snxt - tcp->tcp_suna; 16356 tce.tcpConnEntryInfo.ce_suna = 0; 16357 tce.tcpConnEntryInfo.ce_rnxt = 16358 tcp->tcp_rnxt - tcp->tcp_rack; 16359 tce.tcpConnEntryInfo.ce_rack = 0; 16360 } 16361 16362 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16363 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16364 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16365 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16366 tce.tcpConnEntryInfo.ce_state = 16367 tcp->tcp_state; 16368 16369 tce.tcpConnCreationProcess = 16370 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16371 tcp->tcp_cpid; 16372 tce.tcpConnCreationTime = tcp->tcp_open_time; 16373 16374 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16375 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16376 16377 mlp.tme_connidx = v4_conn_idx++; 16378 if (needattr) 16379 (void) snmp_append_data2( 16380 mp_attr_ctl->b_cont, 16381 &mp_attr_tail, (char *)&mlp, 16382 sizeof (mlp)); 16383 } 16384 } 16385 } 16386 16387 /* fixed length structure for IPv4 and IPv6 counters */ 16388 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16389 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16390 sizeof (mib2_tcp6ConnEntry_t)); 16391 /* synchronize 32- and 64-bit counters */ 16392 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16393 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16394 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16395 optp->level = MIB2_TCP; 16396 optp->name = 0; 16397 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16398 sizeof (tcps->tcps_mib)); 16399 optp->len = msgdsize(mpdata); 16400 qreply(q, mpctl); 16401 16402 /* table of connections... */ 16403 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16404 sizeof (struct T_optmgmt_ack)]; 16405 optp->level = MIB2_TCP; 16406 optp->name = MIB2_TCP_CONN; 16407 optp->len = msgdsize(mp_conn_ctl->b_cont); 16408 qreply(q, mp_conn_ctl); 16409 16410 /* table of MLP attributes... */ 16411 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16412 sizeof (struct T_optmgmt_ack)]; 16413 optp->level = MIB2_TCP; 16414 optp->name = EXPER_XPORT_MLP; 16415 optp->len = msgdsize(mp_attr_ctl->b_cont); 16416 if (optp->len == 0) 16417 freemsg(mp_attr_ctl); 16418 else 16419 qreply(q, mp_attr_ctl); 16420 16421 /* table of IPv6 connections... */ 16422 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16423 sizeof (struct T_optmgmt_ack)]; 16424 optp->level = MIB2_TCP6; 16425 optp->name = MIB2_TCP6_CONN; 16426 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16427 qreply(q, mp6_conn_ctl); 16428 16429 /* table of IPv6 MLP attributes... */ 16430 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16431 sizeof (struct T_optmgmt_ack)]; 16432 optp->level = MIB2_TCP6; 16433 optp->name = EXPER_XPORT_MLP; 16434 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16435 if (optp->len == 0) 16436 freemsg(mp6_attr_ctl); 16437 else 16438 qreply(q, mp6_attr_ctl); 16439 return (1); 16440 } 16441 16442 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16443 /* ARGSUSED */ 16444 int 16445 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16446 { 16447 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16448 16449 switch (level) { 16450 case MIB2_TCP: 16451 switch (name) { 16452 case 13: 16453 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16454 return (0); 16455 /* TODO: delete entry defined by tce */ 16456 return (1); 16457 default: 16458 return (0); 16459 } 16460 default: 16461 return (1); 16462 } 16463 } 16464 16465 /* Translate TCP state to MIB2 TCP state. */ 16466 static int 16467 tcp_snmp_state(tcp_t *tcp) 16468 { 16469 if (tcp == NULL) 16470 return (0); 16471 16472 switch (tcp->tcp_state) { 16473 case TCPS_CLOSED: 16474 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16475 case TCPS_BOUND: 16476 return (MIB2_TCP_closed); 16477 case TCPS_LISTEN: 16478 return (MIB2_TCP_listen); 16479 case TCPS_SYN_SENT: 16480 return (MIB2_TCP_synSent); 16481 case TCPS_SYN_RCVD: 16482 return (MIB2_TCP_synReceived); 16483 case TCPS_ESTABLISHED: 16484 return (MIB2_TCP_established); 16485 case TCPS_CLOSE_WAIT: 16486 return (MIB2_TCP_closeWait); 16487 case TCPS_FIN_WAIT_1: 16488 return (MIB2_TCP_finWait1); 16489 case TCPS_CLOSING: 16490 return (MIB2_TCP_closing); 16491 case TCPS_LAST_ACK: 16492 return (MIB2_TCP_lastAck); 16493 case TCPS_FIN_WAIT_2: 16494 return (MIB2_TCP_finWait2); 16495 case TCPS_TIME_WAIT: 16496 return (MIB2_TCP_timeWait); 16497 default: 16498 return (0); 16499 } 16500 } 16501 16502 static char tcp_report_header[] = 16503 "TCP " MI_COL_HDRPAD_STR 16504 "zone dest snxt suna " 16505 "swnd rnxt rack rwnd rto mss w sw rw t " 16506 "recent [lport,fport] state"; 16507 16508 /* 16509 * TCP status report triggered via the Named Dispatch mechanism. 16510 */ 16511 /* ARGSUSED */ 16512 static void 16513 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16514 cred_t *cr) 16515 { 16516 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16517 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16518 char cflag; 16519 in6_addr_t v6dst; 16520 char buf[80]; 16521 uint_t print_len, buf_len; 16522 16523 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16524 if (buf_len <= 0) 16525 return; 16526 16527 if (hashval >= 0) 16528 (void) sprintf(hash, "%03d ", hashval); 16529 else 16530 hash[0] = '\0'; 16531 16532 /* 16533 * Note that we use the remote address in the tcp_b structure. 16534 * This means that it will print out the real destination address, 16535 * not the next hop's address if source routing is used. This 16536 * avoid the confusion on the output because user may not 16537 * know that source routing is used for a connection. 16538 */ 16539 if (tcp->tcp_ipversion == IPV4_VERSION) { 16540 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16541 } else { 16542 v6dst = tcp->tcp_remote_v6; 16543 } 16544 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16545 /* 16546 * the ispriv checks are so that normal users cannot determine 16547 * sequence number information using NDD. 16548 */ 16549 16550 if (TCP_IS_DETACHED(tcp)) 16551 cflag = '*'; 16552 else 16553 cflag = ' '; 16554 print_len = snprintf((char *)mp->b_wptr, buf_len, 16555 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16556 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16557 hash, 16558 (void *)tcp, 16559 tcp->tcp_connp->conn_zoneid, 16560 addrbuf, 16561 (ispriv) ? tcp->tcp_snxt : 0, 16562 (ispriv) ? tcp->tcp_suna : 0, 16563 tcp->tcp_swnd, 16564 (ispriv) ? tcp->tcp_rnxt : 0, 16565 (ispriv) ? tcp->tcp_rack : 0, 16566 tcp->tcp_rwnd, 16567 tcp->tcp_rto, 16568 tcp->tcp_mss, 16569 tcp->tcp_snd_ws_ok, 16570 tcp->tcp_snd_ws, 16571 tcp->tcp_rcv_ws, 16572 tcp->tcp_snd_ts_ok, 16573 tcp->tcp_ts_recent, 16574 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16575 if (print_len < buf_len) { 16576 ((mblk_t *)mp)->b_wptr += print_len; 16577 } else { 16578 ((mblk_t *)mp)->b_wptr += buf_len; 16579 } 16580 } 16581 16582 /* 16583 * TCP status report (for listeners only) triggered via the Named Dispatch 16584 * mechanism. 16585 */ 16586 /* ARGSUSED */ 16587 static void 16588 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16589 { 16590 char addrbuf[INET6_ADDRSTRLEN]; 16591 in6_addr_t v6dst; 16592 uint_t print_len, buf_len; 16593 16594 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16595 if (buf_len <= 0) 16596 return; 16597 16598 if (tcp->tcp_ipversion == IPV4_VERSION) { 16599 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16600 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16601 } else { 16602 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16603 addrbuf, sizeof (addrbuf)); 16604 } 16605 print_len = snprintf((char *)mp->b_wptr, buf_len, 16606 "%03d " 16607 MI_COL_PTRFMT_STR 16608 "%d %s %05u %08u %d/%d/%d%c\n", 16609 hashval, (void *)tcp, 16610 tcp->tcp_connp->conn_zoneid, 16611 addrbuf, 16612 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16613 tcp->tcp_conn_req_seqnum, 16614 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16615 tcp->tcp_conn_req_max, 16616 tcp->tcp_syn_defense ? '*' : ' '); 16617 if (print_len < buf_len) { 16618 ((mblk_t *)mp)->b_wptr += print_len; 16619 } else { 16620 ((mblk_t *)mp)->b_wptr += buf_len; 16621 } 16622 } 16623 16624 /* TCP status report triggered via the Named Dispatch mechanism. */ 16625 /* ARGSUSED */ 16626 static int 16627 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16628 { 16629 tcp_t *tcp; 16630 int i; 16631 conn_t *connp; 16632 connf_t *connfp; 16633 zoneid_t zoneid; 16634 tcp_stack_t *tcps; 16635 ip_stack_t *ipst; 16636 16637 zoneid = Q_TO_CONN(q)->conn_zoneid; 16638 tcps = Q_TO_TCP(q)->tcp_tcps; 16639 16640 /* 16641 * Because of the ndd constraint, at most we can have 64K buffer 16642 * to put in all TCP info. So to be more efficient, just 16643 * allocate a 64K buffer here, assuming we need that large buffer. 16644 * This may be a problem as any user can read tcp_status. Therefore 16645 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16646 * This should be OK as normal users should not do this too often. 16647 */ 16648 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16649 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16650 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16651 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16652 return (0); 16653 } 16654 } 16655 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16656 /* The following may work even if we cannot get a large buf. */ 16657 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16658 return (0); 16659 } 16660 16661 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16662 16663 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16664 16665 ipst = tcps->tcps_netstack->netstack_ip; 16666 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16667 16668 connp = NULL; 16669 16670 while ((connp = 16671 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16672 tcp = connp->conn_tcp; 16673 if (zoneid != GLOBAL_ZONEID && 16674 zoneid != connp->conn_zoneid) 16675 continue; 16676 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16677 cr); 16678 } 16679 16680 } 16681 16682 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16683 return (0); 16684 } 16685 16686 /* TCP status report triggered via the Named Dispatch mechanism. */ 16687 /* ARGSUSED */ 16688 static int 16689 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16690 { 16691 tf_t *tbf; 16692 tcp_t *tcp; 16693 int i; 16694 zoneid_t zoneid; 16695 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16696 16697 zoneid = Q_TO_CONN(q)->conn_zoneid; 16698 16699 /* Refer to comments in tcp_status_report(). */ 16700 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16701 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16702 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16703 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16704 return (0); 16705 } 16706 } 16707 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16708 /* The following may work even if we cannot get a large buf. */ 16709 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16710 return (0); 16711 } 16712 16713 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16714 16715 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16716 tbf = &tcps->tcps_bind_fanout[i]; 16717 mutex_enter(&tbf->tf_lock); 16718 for (tcp = tbf->tf_tcp; tcp != NULL; 16719 tcp = tcp->tcp_bind_hash) { 16720 if (zoneid != GLOBAL_ZONEID && 16721 zoneid != tcp->tcp_connp->conn_zoneid) 16722 continue; 16723 CONN_INC_REF(tcp->tcp_connp); 16724 tcp_report_item(mp->b_cont, tcp, i, 16725 Q_TO_TCP(q), cr); 16726 CONN_DEC_REF(tcp->tcp_connp); 16727 } 16728 mutex_exit(&tbf->tf_lock); 16729 } 16730 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16731 return (0); 16732 } 16733 16734 /* TCP status report triggered via the Named Dispatch mechanism. */ 16735 /* ARGSUSED */ 16736 static int 16737 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16738 { 16739 connf_t *connfp; 16740 conn_t *connp; 16741 tcp_t *tcp; 16742 int i; 16743 zoneid_t zoneid; 16744 tcp_stack_t *tcps; 16745 ip_stack_t *ipst; 16746 16747 zoneid = Q_TO_CONN(q)->conn_zoneid; 16748 tcps = Q_TO_TCP(q)->tcp_tcps; 16749 16750 /* Refer to comments in tcp_status_report(). */ 16751 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16752 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16753 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16754 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16755 return (0); 16756 } 16757 } 16758 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16759 /* The following may work even if we cannot get a large buf. */ 16760 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16761 return (0); 16762 } 16763 16764 (void) mi_mpprintf(mp, 16765 " TCP " MI_COL_HDRPAD_STR 16766 "zone IP addr port seqnum backlog (q0/q/max)"); 16767 16768 ipst = tcps->tcps_netstack->netstack_ip; 16769 16770 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16771 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16772 connp = NULL; 16773 while ((connp = 16774 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16775 tcp = connp->conn_tcp; 16776 if (zoneid != GLOBAL_ZONEID && 16777 zoneid != connp->conn_zoneid) 16778 continue; 16779 tcp_report_listener(mp->b_cont, tcp, i); 16780 } 16781 } 16782 16783 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16784 return (0); 16785 } 16786 16787 /* TCP status report triggered via the Named Dispatch mechanism. */ 16788 /* ARGSUSED */ 16789 static int 16790 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16791 { 16792 connf_t *connfp; 16793 conn_t *connp; 16794 tcp_t *tcp; 16795 int i; 16796 zoneid_t zoneid; 16797 tcp_stack_t *tcps; 16798 ip_stack_t *ipst; 16799 16800 zoneid = Q_TO_CONN(q)->conn_zoneid; 16801 tcps = Q_TO_TCP(q)->tcp_tcps; 16802 ipst = tcps->tcps_netstack->netstack_ip; 16803 16804 /* Refer to comments in tcp_status_report(). */ 16805 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16806 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16807 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16808 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16809 return (0); 16810 } 16811 } 16812 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16813 /* The following may work even if we cannot get a large buf. */ 16814 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16815 return (0); 16816 } 16817 16818 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16819 ipst->ips_ipcl_conn_fanout_size); 16820 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16821 16822 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16823 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16824 connp = NULL; 16825 while ((connp = 16826 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16827 tcp = connp->conn_tcp; 16828 if (zoneid != GLOBAL_ZONEID && 16829 zoneid != connp->conn_zoneid) 16830 continue; 16831 tcp_report_item(mp->b_cont, tcp, i, 16832 Q_TO_TCP(q), cr); 16833 } 16834 } 16835 16836 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16837 return (0); 16838 } 16839 16840 /* TCP status report triggered via the Named Dispatch mechanism. */ 16841 /* ARGSUSED */ 16842 static int 16843 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16844 { 16845 tf_t *tf; 16846 tcp_t *tcp; 16847 int i; 16848 zoneid_t zoneid; 16849 tcp_stack_t *tcps; 16850 16851 zoneid = Q_TO_CONN(q)->conn_zoneid; 16852 tcps = Q_TO_TCP(q)->tcp_tcps; 16853 16854 /* Refer to comments in tcp_status_report(). */ 16855 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16856 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16857 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16858 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16859 return (0); 16860 } 16861 } 16862 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16863 /* The following may work even if we cannot get a large buf. */ 16864 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16865 return (0); 16866 } 16867 16868 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16869 16870 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16871 tf = &tcps->tcps_acceptor_fanout[i]; 16872 mutex_enter(&tf->tf_lock); 16873 for (tcp = tf->tf_tcp; tcp != NULL; 16874 tcp = tcp->tcp_acceptor_hash) { 16875 if (zoneid != GLOBAL_ZONEID && 16876 zoneid != tcp->tcp_connp->conn_zoneid) 16877 continue; 16878 tcp_report_item(mp->b_cont, tcp, i, 16879 Q_TO_TCP(q), cr); 16880 } 16881 mutex_exit(&tf->tf_lock); 16882 } 16883 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16884 return (0); 16885 } 16886 16887 /* 16888 * tcp_timer is the timer service routine. It handles the retransmission, 16889 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16890 * from the state of the tcp instance what kind of action needs to be done 16891 * at the time it is called. 16892 */ 16893 static void 16894 tcp_timer(void *arg) 16895 { 16896 mblk_t *mp; 16897 clock_t first_threshold; 16898 clock_t second_threshold; 16899 clock_t ms; 16900 uint32_t mss; 16901 conn_t *connp = (conn_t *)arg; 16902 tcp_t *tcp = connp->conn_tcp; 16903 tcp_stack_t *tcps = tcp->tcp_tcps; 16904 16905 tcp->tcp_timer_tid = 0; 16906 16907 if (tcp->tcp_fused) 16908 return; 16909 16910 first_threshold = tcp->tcp_first_timer_threshold; 16911 second_threshold = tcp->tcp_second_timer_threshold; 16912 switch (tcp->tcp_state) { 16913 case TCPS_IDLE: 16914 case TCPS_BOUND: 16915 case TCPS_LISTEN: 16916 return; 16917 case TCPS_SYN_RCVD: { 16918 tcp_t *listener = tcp->tcp_listener; 16919 16920 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16921 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16922 /* it's our first timeout */ 16923 tcp->tcp_syn_rcvd_timeout = 1; 16924 mutex_enter(&listener->tcp_eager_lock); 16925 listener->tcp_syn_rcvd_timeout++; 16926 if (!tcp->tcp_dontdrop && tcp->tcp_closemp_used == 0) { 16927 /* 16928 * Make this eager available for drop if we 16929 * need to drop one to accomodate a new 16930 * incoming SYN request. 16931 */ 16932 MAKE_DROPPABLE(listener, tcp); 16933 } 16934 if (!listener->tcp_syn_defense && 16935 (listener->tcp_syn_rcvd_timeout > 16936 (tcps->tcps_conn_req_max_q0 >> 2)) && 16937 (tcps->tcps_conn_req_max_q0 > 200)) { 16938 /* We may be under attack. Put on a defense. */ 16939 listener->tcp_syn_defense = B_TRUE; 16940 cmn_err(CE_WARN, "High TCP connect timeout " 16941 "rate! System (port %d) may be under a " 16942 "SYN flood attack!", 16943 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16944 16945 listener->tcp_ip_addr_cache = kmem_zalloc( 16946 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16947 KM_NOSLEEP); 16948 } 16949 mutex_exit(&listener->tcp_eager_lock); 16950 } else if (listener != NULL) { 16951 mutex_enter(&listener->tcp_eager_lock); 16952 tcp->tcp_syn_rcvd_timeout++; 16953 if (tcp->tcp_syn_rcvd_timeout > 1 && 16954 tcp->tcp_closemp_used == 0) { 16955 /* 16956 * This is our second timeout. Put the tcp in 16957 * the list of droppable eagers to allow it to 16958 * be dropped, if needed. We don't check 16959 * whether tcp_dontdrop is set or not to 16960 * protect ourselve from a SYN attack where a 16961 * remote host can spoof itself as one of the 16962 * good IP source and continue to hold 16963 * resources too long. 16964 */ 16965 MAKE_DROPPABLE(listener, tcp); 16966 } 16967 mutex_exit(&listener->tcp_eager_lock); 16968 } 16969 } 16970 /* FALLTHRU */ 16971 case TCPS_SYN_SENT: 16972 first_threshold = tcp->tcp_first_ctimer_threshold; 16973 second_threshold = tcp->tcp_second_ctimer_threshold; 16974 break; 16975 case TCPS_ESTABLISHED: 16976 case TCPS_FIN_WAIT_1: 16977 case TCPS_CLOSING: 16978 case TCPS_CLOSE_WAIT: 16979 case TCPS_LAST_ACK: 16980 /* If we have data to rexmit */ 16981 if (tcp->tcp_suna != tcp->tcp_snxt) { 16982 clock_t time_to_wait; 16983 16984 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 16985 if (!tcp->tcp_xmit_head) 16986 break; 16987 time_to_wait = lbolt - 16988 (clock_t)tcp->tcp_xmit_head->b_prev; 16989 time_to_wait = tcp->tcp_rto - 16990 TICK_TO_MSEC(time_to_wait); 16991 /* 16992 * If the timer fires too early, 1 clock tick earlier, 16993 * restart the timer. 16994 */ 16995 if (time_to_wait > msec_per_tick) { 16996 TCP_STAT(tcps, tcp_timer_fire_early); 16997 TCP_TIMER_RESTART(tcp, time_to_wait); 16998 return; 16999 } 17000 /* 17001 * When we probe zero windows, we force the swnd open. 17002 * If our peer acks with a closed window swnd will be 17003 * set to zero by tcp_rput(). As long as we are 17004 * receiving acks tcp_rput will 17005 * reset 'tcp_ms_we_have_waited' so as not to trip the 17006 * first and second interval actions. NOTE: the timer 17007 * interval is allowed to continue its exponential 17008 * backoff. 17009 */ 17010 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17011 if (tcp->tcp_debug) { 17012 (void) strlog(TCP_MOD_ID, 0, 1, 17013 SL_TRACE, "tcp_timer: zero win"); 17014 } 17015 } else { 17016 /* 17017 * After retransmission, we need to do 17018 * slow start. Set the ssthresh to one 17019 * half of current effective window and 17020 * cwnd to one MSS. Also reset 17021 * tcp_cwnd_cnt. 17022 * 17023 * Note that if tcp_ssthresh is reduced because 17024 * of ECN, do not reduce it again unless it is 17025 * already one window of data away (tcp_cwr 17026 * should then be cleared) or this is a 17027 * timeout for a retransmitted segment. 17028 */ 17029 uint32_t npkt; 17030 17031 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17032 npkt = ((tcp->tcp_timer_backoff ? 17033 tcp->tcp_cwnd_ssthresh : 17034 tcp->tcp_snxt - 17035 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17036 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17037 tcp->tcp_mss; 17038 } 17039 tcp->tcp_cwnd = tcp->tcp_mss; 17040 tcp->tcp_cwnd_cnt = 0; 17041 if (tcp->tcp_ecn_ok) { 17042 tcp->tcp_cwr = B_TRUE; 17043 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17044 tcp->tcp_ecn_cwr_sent = B_FALSE; 17045 } 17046 } 17047 break; 17048 } 17049 /* 17050 * We have something to send yet we cannot send. The 17051 * reason can be: 17052 * 17053 * 1. Zero send window: we need to do zero window probe. 17054 * 2. Zero cwnd: because of ECN, we need to "clock out 17055 * segments. 17056 * 3. SWS avoidance: receiver may have shrunk window, 17057 * reset our knowledge. 17058 * 17059 * Note that condition 2 can happen with either 1 or 17060 * 3. But 1 and 3 are exclusive. 17061 */ 17062 if (tcp->tcp_unsent != 0) { 17063 if (tcp->tcp_cwnd == 0) { 17064 /* 17065 * Set tcp_cwnd to 1 MSS so that a 17066 * new segment can be sent out. We 17067 * are "clocking out" new data when 17068 * the network is really congested. 17069 */ 17070 ASSERT(tcp->tcp_ecn_ok); 17071 tcp->tcp_cwnd = tcp->tcp_mss; 17072 } 17073 if (tcp->tcp_swnd == 0) { 17074 /* Extend window for zero window probe */ 17075 tcp->tcp_swnd++; 17076 tcp->tcp_zero_win_probe = B_TRUE; 17077 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17078 } else { 17079 /* 17080 * Handle timeout from sender SWS avoidance. 17081 * Reset our knowledge of the max send window 17082 * since the receiver might have reduced its 17083 * receive buffer. Avoid setting tcp_max_swnd 17084 * to one since that will essentially disable 17085 * the SWS checks. 17086 * 17087 * Note that since we don't have a SWS 17088 * state variable, if the timeout is set 17089 * for ECN but not for SWS, this 17090 * code will also be executed. This is 17091 * fine as tcp_max_swnd is updated 17092 * constantly and it will not affect 17093 * anything. 17094 */ 17095 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17096 } 17097 tcp_wput_data(tcp, NULL, B_FALSE); 17098 return; 17099 } 17100 /* Is there a FIN that needs to be to re retransmitted? */ 17101 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17102 !tcp->tcp_fin_acked) 17103 break; 17104 /* Nothing to do, return without restarting timer. */ 17105 TCP_STAT(tcps, tcp_timer_fire_miss); 17106 return; 17107 case TCPS_FIN_WAIT_2: 17108 /* 17109 * User closed the TCP endpoint and peer ACK'ed our FIN. 17110 * We waited some time for for peer's FIN, but it hasn't 17111 * arrived. We flush the connection now to avoid 17112 * case where the peer has rebooted. 17113 */ 17114 if (TCP_IS_DETACHED(tcp)) { 17115 (void) tcp_clean_death(tcp, 0, 23); 17116 } else { 17117 TCP_TIMER_RESTART(tcp, 17118 tcps->tcps_fin_wait_2_flush_interval); 17119 } 17120 return; 17121 case TCPS_TIME_WAIT: 17122 (void) tcp_clean_death(tcp, 0, 24); 17123 return; 17124 default: 17125 if (tcp->tcp_debug) { 17126 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17127 "tcp_timer: strange state (%d) %s", 17128 tcp->tcp_state, tcp_display(tcp, NULL, 17129 DISP_PORT_ONLY)); 17130 } 17131 return; 17132 } 17133 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17134 /* 17135 * For zero window probe, we need to send indefinitely, 17136 * unless we have not heard from the other side for some 17137 * time... 17138 */ 17139 if ((tcp->tcp_zero_win_probe == 0) || 17140 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17141 second_threshold)) { 17142 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17143 /* 17144 * If TCP is in SYN_RCVD state, send back a 17145 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17146 * should be zero in TCPS_SYN_RCVD state. 17147 */ 17148 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17149 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17150 "in SYN_RCVD", 17151 tcp, tcp->tcp_snxt, 17152 tcp->tcp_rnxt, TH_RST | TH_ACK); 17153 } 17154 (void) tcp_clean_death(tcp, 17155 tcp->tcp_client_errno ? 17156 tcp->tcp_client_errno : ETIMEDOUT, 25); 17157 return; 17158 } else { 17159 /* 17160 * Set tcp_ms_we_have_waited to second_threshold 17161 * so that in next timeout, we will do the above 17162 * check (lbolt - tcp_last_recv_time). This is 17163 * also to avoid overflow. 17164 * 17165 * We don't need to decrement tcp_timer_backoff 17166 * to avoid overflow because it will be decremented 17167 * later if new timeout value is greater than 17168 * tcp_rexmit_interval_max. In the case when 17169 * tcp_rexmit_interval_max is greater than 17170 * second_threshold, it means that we will wait 17171 * longer than second_threshold to send the next 17172 * window probe. 17173 */ 17174 tcp->tcp_ms_we_have_waited = second_threshold; 17175 } 17176 } else if (ms > first_threshold) { 17177 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17178 tcp->tcp_xmit_head != NULL) { 17179 tcp->tcp_xmit_head = 17180 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17181 } 17182 /* 17183 * We have been retransmitting for too long... The RTT 17184 * we calculated is probably incorrect. Reinitialize it. 17185 * Need to compensate for 0 tcp_rtt_sa. Reset 17186 * tcp_rtt_update so that we won't accidentally cache a 17187 * bad value. But only do this if this is not a zero 17188 * window probe. 17189 */ 17190 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17191 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17192 (tcp->tcp_rtt_sa >> 5); 17193 tcp->tcp_rtt_sa = 0; 17194 tcp_ip_notify(tcp); 17195 tcp->tcp_rtt_update = 0; 17196 } 17197 } 17198 tcp->tcp_timer_backoff++; 17199 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17200 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17201 tcps->tcps_rexmit_interval_min) { 17202 /* 17203 * This means the original RTO is tcp_rexmit_interval_min. 17204 * So we will use tcp_rexmit_interval_min as the RTO value 17205 * and do the backoff. 17206 */ 17207 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17208 } else { 17209 ms <<= tcp->tcp_timer_backoff; 17210 } 17211 if (ms > tcps->tcps_rexmit_interval_max) { 17212 ms = tcps->tcps_rexmit_interval_max; 17213 /* 17214 * ms is at max, decrement tcp_timer_backoff to avoid 17215 * overflow. 17216 */ 17217 tcp->tcp_timer_backoff--; 17218 } 17219 tcp->tcp_ms_we_have_waited += ms; 17220 if (tcp->tcp_zero_win_probe == 0) { 17221 tcp->tcp_rto = ms; 17222 } 17223 TCP_TIMER_RESTART(tcp, ms); 17224 /* 17225 * This is after a timeout and tcp_rto is backed off. Set 17226 * tcp_set_timer to 1 so that next time RTO is updated, we will 17227 * restart the timer with a correct value. 17228 */ 17229 tcp->tcp_set_timer = 1; 17230 mss = tcp->tcp_snxt - tcp->tcp_suna; 17231 if (mss > tcp->tcp_mss) 17232 mss = tcp->tcp_mss; 17233 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17234 mss = tcp->tcp_swnd; 17235 17236 if ((mp = tcp->tcp_xmit_head) != NULL) 17237 mp->b_prev = (mblk_t *)lbolt; 17238 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17239 B_TRUE); 17240 17241 /* 17242 * When slow start after retransmission begins, start with 17243 * this seq no. tcp_rexmit_max marks the end of special slow 17244 * start phase. tcp_snd_burst controls how many segments 17245 * can be sent because of an ack. 17246 */ 17247 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17248 tcp->tcp_snd_burst = TCP_CWND_SS; 17249 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17250 (tcp->tcp_unsent == 0)) { 17251 tcp->tcp_rexmit_max = tcp->tcp_fss; 17252 } else { 17253 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17254 } 17255 tcp->tcp_rexmit = B_TRUE; 17256 tcp->tcp_dupack_cnt = 0; 17257 17258 /* 17259 * Remove all rexmit SACK blk to start from fresh. 17260 */ 17261 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17262 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17263 tcp->tcp_num_notsack_blk = 0; 17264 tcp->tcp_cnt_notsack_list = 0; 17265 } 17266 if (mp == NULL) { 17267 return; 17268 } 17269 /* Attach credentials to retransmitted initial SYNs. */ 17270 if (tcp->tcp_state == TCPS_SYN_SENT) { 17271 mblk_setcred(mp, tcp->tcp_cred); 17272 DB_CPID(mp) = tcp->tcp_cpid; 17273 } 17274 17275 tcp->tcp_csuna = tcp->tcp_snxt; 17276 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17277 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17278 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 17279 tcp_send_data(tcp, tcp->tcp_wq, mp); 17280 17281 } 17282 17283 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17284 static void 17285 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17286 { 17287 conn_t *connp; 17288 17289 switch (tcp->tcp_state) { 17290 case TCPS_BOUND: 17291 case TCPS_LISTEN: 17292 break; 17293 default: 17294 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17295 return; 17296 } 17297 17298 /* 17299 * Need to clean up all the eagers since after the unbind, segments 17300 * will no longer be delivered to this listener stream. 17301 */ 17302 mutex_enter(&tcp->tcp_eager_lock); 17303 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17304 tcp_eager_cleanup(tcp, 0); 17305 } 17306 mutex_exit(&tcp->tcp_eager_lock); 17307 17308 if (tcp->tcp_ipversion == IPV4_VERSION) { 17309 tcp->tcp_ipha->ipha_src = 0; 17310 } else { 17311 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17312 } 17313 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17314 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17315 tcp_bind_hash_remove(tcp); 17316 tcp->tcp_state = TCPS_IDLE; 17317 tcp->tcp_mdt = B_FALSE; 17318 /* Send M_FLUSH according to TPI */ 17319 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17320 connp = tcp->tcp_connp; 17321 connp->conn_mdt_ok = B_FALSE; 17322 ipcl_hash_remove(connp); 17323 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17324 mp = mi_tpi_ok_ack_alloc(mp); 17325 putnext(tcp->tcp_rq, mp); 17326 } 17327 17328 /* 17329 * Don't let port fall into the privileged range. 17330 * Since the extra privileged ports can be arbitrary we also 17331 * ensure that we exclude those from consideration. 17332 * tcp_g_epriv_ports is not sorted thus we loop over it until 17333 * there are no changes. 17334 * 17335 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17336 * but instead the code relies on: 17337 * - the fact that the address of the array and its size never changes 17338 * - the atomic assignment of the elements of the array 17339 * 17340 * Returns 0 if there are no more ports available. 17341 * 17342 * TS note: skip multilevel ports. 17343 */ 17344 static in_port_t 17345 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17346 { 17347 int i; 17348 boolean_t restart = B_FALSE; 17349 tcp_stack_t *tcps = tcp->tcp_tcps; 17350 17351 if (random && tcp_random_anon_port != 0) { 17352 (void) random_get_pseudo_bytes((uint8_t *)&port, 17353 sizeof (in_port_t)); 17354 /* 17355 * Unless changed by a sys admin, the smallest anon port 17356 * is 32768 and the largest anon port is 65535. It is 17357 * very likely (50%) for the random port to be smaller 17358 * than the smallest anon port. When that happens, 17359 * add port % (anon port range) to the smallest anon 17360 * port to get the random port. It should fall into the 17361 * valid anon port range. 17362 */ 17363 if (port < tcps->tcps_smallest_anon_port) { 17364 port = tcps->tcps_smallest_anon_port + 17365 port % (tcps->tcps_largest_anon_port - 17366 tcps->tcps_smallest_anon_port); 17367 } 17368 } 17369 17370 retry: 17371 if (port < tcps->tcps_smallest_anon_port) 17372 port = (in_port_t)tcps->tcps_smallest_anon_port; 17373 17374 if (port > tcps->tcps_largest_anon_port) { 17375 if (restart) 17376 return (0); 17377 restart = B_TRUE; 17378 port = (in_port_t)tcps->tcps_smallest_anon_port; 17379 } 17380 17381 if (port < tcps->tcps_smallest_nonpriv_port) 17382 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17383 17384 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17385 if (port == tcps->tcps_g_epriv_ports[i]) { 17386 port++; 17387 /* 17388 * Make sure whether the port is in the 17389 * valid range. 17390 */ 17391 goto retry; 17392 } 17393 } 17394 if (is_system_labeled() && 17395 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17396 IPPROTO_TCP, B_TRUE)) != 0) { 17397 port = i; 17398 goto retry; 17399 } 17400 return (port); 17401 } 17402 17403 /* 17404 * Return the next anonymous port in the privileged port range for 17405 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17406 * downwards. This is the same behavior as documented in the userland 17407 * library call rresvport(3N). 17408 * 17409 * TS note: skip multilevel ports. 17410 */ 17411 static in_port_t 17412 tcp_get_next_priv_port(const tcp_t *tcp) 17413 { 17414 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17415 in_port_t nextport; 17416 boolean_t restart = B_FALSE; 17417 tcp_stack_t *tcps = tcp->tcp_tcps; 17418 retry: 17419 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17420 next_priv_port >= IPPORT_RESERVED) { 17421 next_priv_port = IPPORT_RESERVED - 1; 17422 if (restart) 17423 return (0); 17424 restart = B_TRUE; 17425 } 17426 if (is_system_labeled() && 17427 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17428 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17429 next_priv_port = nextport; 17430 goto retry; 17431 } 17432 return (next_priv_port--); 17433 } 17434 17435 /* The write side r/w procedure. */ 17436 17437 #if CCS_STATS 17438 struct { 17439 struct { 17440 int64_t count, bytes; 17441 } tot, hit; 17442 } wrw_stats; 17443 #endif 17444 17445 /* 17446 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17447 * messages. 17448 */ 17449 /* ARGSUSED */ 17450 static void 17451 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17452 { 17453 conn_t *connp = (conn_t *)arg; 17454 tcp_t *tcp = connp->conn_tcp; 17455 queue_t *q = tcp->tcp_wq; 17456 17457 ASSERT(DB_TYPE(mp) != M_IOCTL); 17458 /* 17459 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17460 * Once the close starts, streamhead and sockfs will not let any data 17461 * packets come down (close ensures that there are no threads using the 17462 * queue and no new threads will come down) but since qprocsoff() 17463 * hasn't happened yet, a M_FLUSH or some non data message might 17464 * get reflected back (in response to our own FLUSHRW) and get 17465 * processed after tcp_close() is done. The conn would still be valid 17466 * because a ref would have added but we need to check the state 17467 * before actually processing the packet. 17468 */ 17469 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17470 freemsg(mp); 17471 return; 17472 } 17473 17474 switch (DB_TYPE(mp)) { 17475 case M_IOCDATA: 17476 tcp_wput_iocdata(tcp, mp); 17477 break; 17478 case M_FLUSH: 17479 tcp_wput_flush(tcp, mp); 17480 break; 17481 default: 17482 CALL_IP_WPUT(connp, q, mp); 17483 break; 17484 } 17485 } 17486 17487 /* 17488 * The TCP fast path write put procedure. 17489 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17490 */ 17491 /* ARGSUSED */ 17492 void 17493 tcp_output(void *arg, mblk_t *mp, void *arg2) 17494 { 17495 int len; 17496 int hdrlen; 17497 int plen; 17498 mblk_t *mp1; 17499 uchar_t *rptr; 17500 uint32_t snxt; 17501 tcph_t *tcph; 17502 struct datab *db; 17503 uint32_t suna; 17504 uint32_t mss; 17505 ipaddr_t *dst; 17506 ipaddr_t *src; 17507 uint32_t sum; 17508 int usable; 17509 conn_t *connp = (conn_t *)arg; 17510 tcp_t *tcp = connp->conn_tcp; 17511 uint32_t msize; 17512 tcp_stack_t *tcps = tcp->tcp_tcps; 17513 17514 /* 17515 * Try and ASSERT the minimum possible references on the 17516 * conn early enough. Since we are executing on write side, 17517 * the connection is obviously not detached and that means 17518 * there is a ref each for TCP and IP. Since we are behind 17519 * the squeue, the minimum references needed are 3. If the 17520 * conn is in classifier hash list, there should be an 17521 * extra ref for that (we check both the possibilities). 17522 */ 17523 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17524 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17525 17526 ASSERT(DB_TYPE(mp) == M_DATA); 17527 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17528 17529 mutex_enter(&tcp->tcp_non_sq_lock); 17530 tcp->tcp_squeue_bytes -= msize; 17531 mutex_exit(&tcp->tcp_non_sq_lock); 17532 17533 /* Bypass tcp protocol for fused tcp loopback */ 17534 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17535 return; 17536 17537 mss = tcp->tcp_mss; 17538 if (tcp->tcp_xmit_zc_clean) 17539 mp = tcp_zcopy_backoff(tcp, mp, 0); 17540 17541 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17542 len = (int)(mp->b_wptr - mp->b_rptr); 17543 17544 /* 17545 * Criteria for fast path: 17546 * 17547 * 1. no unsent data 17548 * 2. single mblk in request 17549 * 3. connection established 17550 * 4. data in mblk 17551 * 5. len <= mss 17552 * 6. no tcp_valid bits 17553 */ 17554 if ((tcp->tcp_unsent != 0) || 17555 (tcp->tcp_cork) || 17556 (mp->b_cont != NULL) || 17557 (tcp->tcp_state != TCPS_ESTABLISHED) || 17558 (len == 0) || 17559 (len > mss) || 17560 (tcp->tcp_valid_bits != 0)) { 17561 tcp_wput_data(tcp, mp, B_FALSE); 17562 return; 17563 } 17564 17565 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17566 ASSERT(tcp->tcp_fin_sent == 0); 17567 17568 /* queue new packet onto retransmission queue */ 17569 if (tcp->tcp_xmit_head == NULL) { 17570 tcp->tcp_xmit_head = mp; 17571 } else { 17572 tcp->tcp_xmit_last->b_cont = mp; 17573 } 17574 tcp->tcp_xmit_last = mp; 17575 tcp->tcp_xmit_tail = mp; 17576 17577 /* find out how much we can send */ 17578 /* BEGIN CSTYLED */ 17579 /* 17580 * un-acked usable 17581 * |--------------|-----------------| 17582 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17583 */ 17584 /* END CSTYLED */ 17585 17586 /* start sending from tcp_snxt */ 17587 snxt = tcp->tcp_snxt; 17588 17589 /* 17590 * Check to see if this connection has been idled for some 17591 * time and no ACK is expected. If it is, we need to slow 17592 * start again to get back the connection's "self-clock" as 17593 * described in VJ's paper. 17594 * 17595 * Refer to the comment in tcp_mss_set() for the calculation 17596 * of tcp_cwnd after idle. 17597 */ 17598 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17599 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17600 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17601 } 17602 17603 usable = tcp->tcp_swnd; /* tcp window size */ 17604 if (usable > tcp->tcp_cwnd) 17605 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17606 usable -= snxt; /* subtract stuff already sent */ 17607 suna = tcp->tcp_suna; 17608 usable += suna; 17609 /* usable can be < 0 if the congestion window is smaller */ 17610 if (len > usable) { 17611 /* Can't send complete M_DATA in one shot */ 17612 goto slow; 17613 } 17614 17615 mutex_enter(&tcp->tcp_non_sq_lock); 17616 if (tcp->tcp_flow_stopped && 17617 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17618 tcp_clrqfull(tcp); 17619 } 17620 mutex_exit(&tcp->tcp_non_sq_lock); 17621 17622 /* 17623 * determine if anything to send (Nagle). 17624 * 17625 * 1. len < tcp_mss (i.e. small) 17626 * 2. unacknowledged data present 17627 * 3. len < nagle limit 17628 * 4. last packet sent < nagle limit (previous packet sent) 17629 */ 17630 if ((len < mss) && (snxt != suna) && 17631 (len < (int)tcp->tcp_naglim) && 17632 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17633 /* 17634 * This was the first unsent packet and normally 17635 * mss < xmit_hiwater so there is no need to worry 17636 * about flow control. The next packet will go 17637 * through the flow control check in tcp_wput_data(). 17638 */ 17639 /* leftover work from above */ 17640 tcp->tcp_unsent = len; 17641 tcp->tcp_xmit_tail_unsent = len; 17642 17643 return; 17644 } 17645 17646 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17647 17648 if (snxt == suna) { 17649 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17650 } 17651 17652 /* we have always sent something */ 17653 tcp->tcp_rack_cnt = 0; 17654 17655 tcp->tcp_snxt = snxt + len; 17656 tcp->tcp_rack = tcp->tcp_rnxt; 17657 17658 if ((mp1 = dupb(mp)) == 0) 17659 goto no_memory; 17660 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17661 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17662 17663 /* adjust tcp header information */ 17664 tcph = tcp->tcp_tcph; 17665 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17666 17667 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17668 sum = (sum >> 16) + (sum & 0xFFFF); 17669 U16_TO_ABE16(sum, tcph->th_sum); 17670 17671 U32_TO_ABE32(snxt, tcph->th_seq); 17672 17673 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17674 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17675 BUMP_LOCAL(tcp->tcp_obsegs); 17676 17677 /* Update the latest receive window size in TCP header. */ 17678 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17679 tcph->th_win); 17680 17681 tcp->tcp_last_sent_len = (ushort_t)len; 17682 17683 plen = len + tcp->tcp_hdr_len; 17684 17685 if (tcp->tcp_ipversion == IPV4_VERSION) { 17686 tcp->tcp_ipha->ipha_length = htons(plen); 17687 } else { 17688 tcp->tcp_ip6h->ip6_plen = htons(plen - 17689 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17690 } 17691 17692 /* see if we need to allocate a mblk for the headers */ 17693 hdrlen = tcp->tcp_hdr_len; 17694 rptr = mp1->b_rptr - hdrlen; 17695 db = mp1->b_datap; 17696 if ((db->db_ref != 2) || rptr < db->db_base || 17697 (!OK_32PTR(rptr))) { 17698 /* NOTE: we assume allocb returns an OK_32PTR */ 17699 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17700 tcps->tcps_wroff_xtra, BPRI_MED); 17701 if (!mp) { 17702 freemsg(mp1); 17703 goto no_memory; 17704 } 17705 mp->b_cont = mp1; 17706 mp1 = mp; 17707 /* Leave room for Link Level header */ 17708 /* hdrlen = tcp->tcp_hdr_len; */ 17709 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17710 mp1->b_wptr = &rptr[hdrlen]; 17711 } 17712 mp1->b_rptr = rptr; 17713 17714 /* Fill in the timestamp option. */ 17715 if (tcp->tcp_snd_ts_ok) { 17716 U32_TO_BE32((uint32_t)lbolt, 17717 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17718 U32_TO_BE32(tcp->tcp_ts_recent, 17719 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17720 } else { 17721 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17722 } 17723 17724 /* copy header into outgoing packet */ 17725 dst = (ipaddr_t *)rptr; 17726 src = (ipaddr_t *)tcp->tcp_iphc; 17727 dst[0] = src[0]; 17728 dst[1] = src[1]; 17729 dst[2] = src[2]; 17730 dst[3] = src[3]; 17731 dst[4] = src[4]; 17732 dst[5] = src[5]; 17733 dst[6] = src[6]; 17734 dst[7] = src[7]; 17735 dst[8] = src[8]; 17736 dst[9] = src[9]; 17737 if (hdrlen -= 40) { 17738 hdrlen >>= 2; 17739 dst += 10; 17740 src += 10; 17741 do { 17742 *dst++ = *src++; 17743 } while (--hdrlen); 17744 } 17745 17746 /* 17747 * Set the ECN info in the TCP header. Note that this 17748 * is not the template header. 17749 */ 17750 if (tcp->tcp_ecn_ok) { 17751 SET_ECT(tcp, rptr); 17752 17753 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17754 if (tcp->tcp_ecn_echo_on) 17755 tcph->th_flags[0] |= TH_ECE; 17756 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17757 tcph->th_flags[0] |= TH_CWR; 17758 tcp->tcp_ecn_cwr_sent = B_TRUE; 17759 } 17760 } 17761 17762 if (tcp->tcp_ip_forward_progress) { 17763 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17764 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17765 tcp->tcp_ip_forward_progress = B_FALSE; 17766 } 17767 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17768 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17769 return; 17770 17771 /* 17772 * If we ran out of memory, we pretend to have sent the packet 17773 * and that it was lost on the wire. 17774 */ 17775 no_memory: 17776 return; 17777 17778 slow: 17779 /* leftover work from above */ 17780 tcp->tcp_unsent = len; 17781 tcp->tcp_xmit_tail_unsent = len; 17782 tcp_wput_data(tcp, NULL, B_FALSE); 17783 } 17784 17785 /* 17786 * The function called through squeue to get behind eager's perimeter to 17787 * finish the accept processing. 17788 */ 17789 /* ARGSUSED */ 17790 void 17791 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17792 { 17793 conn_t *connp = (conn_t *)arg; 17794 tcp_t *tcp = connp->conn_tcp; 17795 queue_t *q = tcp->tcp_rq; 17796 mblk_t *mp1; 17797 mblk_t *stropt_mp = mp; 17798 struct stroptions *stropt; 17799 uint_t thwin; 17800 tcp_stack_t *tcps = tcp->tcp_tcps; 17801 17802 /* 17803 * Drop the eager's ref on the listener, that was placed when 17804 * this eager began life in tcp_conn_request. 17805 */ 17806 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17807 17808 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17809 /* 17810 * Someone blewoff the eager before we could finish 17811 * the accept. 17812 * 17813 * The only reason eager exists it because we put in 17814 * a ref on it when conn ind went up. We need to send 17815 * a disconnect indication up while the last reference 17816 * on the eager will be dropped by the squeue when we 17817 * return. 17818 */ 17819 ASSERT(tcp->tcp_listener == NULL); 17820 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17821 struct T_discon_ind *tdi; 17822 17823 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17824 /* 17825 * Let us reuse the incoming mblk to avoid memory 17826 * allocation failure problems. We know that the 17827 * size of the incoming mblk i.e. stroptions is greater 17828 * than sizeof T_discon_ind. So the reallocb below 17829 * can't fail. 17830 */ 17831 freemsg(mp->b_cont); 17832 mp->b_cont = NULL; 17833 ASSERT(DB_REF(mp) == 1); 17834 mp = reallocb(mp, sizeof (struct T_discon_ind), 17835 B_FALSE); 17836 ASSERT(mp != NULL); 17837 DB_TYPE(mp) = M_PROTO; 17838 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17839 tdi = (struct T_discon_ind *)mp->b_rptr; 17840 if (tcp->tcp_issocket) { 17841 tdi->DISCON_reason = ECONNREFUSED; 17842 tdi->SEQ_number = 0; 17843 } else { 17844 tdi->DISCON_reason = ENOPROTOOPT; 17845 tdi->SEQ_number = 17846 tcp->tcp_conn_req_seqnum; 17847 } 17848 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17849 putnext(q, mp); 17850 } else { 17851 freemsg(mp); 17852 } 17853 if (tcp->tcp_hard_binding) { 17854 tcp->tcp_hard_binding = B_FALSE; 17855 tcp->tcp_hard_bound = B_TRUE; 17856 } 17857 tcp->tcp_detached = B_FALSE; 17858 return; 17859 } 17860 17861 mp1 = stropt_mp->b_cont; 17862 stropt_mp->b_cont = NULL; 17863 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17864 stropt = (struct stroptions *)stropt_mp->b_rptr; 17865 17866 while (mp1 != NULL) { 17867 mp = mp1; 17868 mp1 = mp1->b_cont; 17869 mp->b_cont = NULL; 17870 tcp->tcp_drop_opt_ack_cnt++; 17871 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17872 } 17873 mp = NULL; 17874 17875 /* 17876 * For a loopback connection with tcp_direct_sockfs on, note that 17877 * we don't have to protect tcp_rcv_list yet because synchronous 17878 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17879 * possibly race with us. 17880 */ 17881 17882 /* 17883 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17884 * properly. This is the first time we know of the acceptor' 17885 * queue. So we do it here. 17886 */ 17887 if (tcp->tcp_rcv_list == NULL) { 17888 /* 17889 * Recv queue is empty, tcp_rwnd should not have changed. 17890 * That means it should be equal to the listener's tcp_rwnd. 17891 */ 17892 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17893 } else { 17894 #ifdef DEBUG 17895 uint_t cnt = 0; 17896 17897 mp1 = tcp->tcp_rcv_list; 17898 while ((mp = mp1) != NULL) { 17899 mp1 = mp->b_next; 17900 cnt += msgdsize(mp); 17901 } 17902 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17903 #endif 17904 /* There is some data, add them back to get the max. */ 17905 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17906 } 17907 17908 stropt->so_flags = SO_HIWAT; 17909 stropt->so_hiwat = MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 17910 17911 stropt->so_flags |= SO_MAXBLK; 17912 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17913 17914 /* 17915 * This is the first time we run on the correct 17916 * queue after tcp_accept. So fix all the q parameters 17917 * here. 17918 */ 17919 /* Allocate room for SACK options if needed. */ 17920 stropt->so_flags |= SO_WROFF; 17921 if (tcp->tcp_fused) { 17922 ASSERT(tcp->tcp_loopback); 17923 ASSERT(tcp->tcp_loopback_peer != NULL); 17924 /* 17925 * For fused tcp loopback, set the stream head's write 17926 * offset value to zero since we won't be needing any room 17927 * for TCP/IP headers. This would also improve performance 17928 * since it would reduce the amount of work done by kmem. 17929 * Non-fused tcp loopback case is handled separately below. 17930 */ 17931 stropt->so_wroff = 0; 17932 /* 17933 * Record the stream head's high water mark for this endpoint; 17934 * this is used for flow-control purposes in tcp_fuse_output(). 17935 */ 17936 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 17937 /* 17938 * Update the peer's transmit parameters according to 17939 * our recently calculated high water mark value. 17940 */ 17941 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17942 } else if (tcp->tcp_snd_sack_ok) { 17943 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17944 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17945 } else { 17946 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17947 tcps->tcps_wroff_xtra); 17948 } 17949 17950 /* 17951 * If this is endpoint is handling SSL, then reserve extra 17952 * offset and space at the end. 17953 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17954 * overriding the previous setting. The extra cost of signing and 17955 * encrypting multiple MSS-size records (12 of them with Ethernet), 17956 * instead of a single contiguous one by the stream head 17957 * largely outweighs the statistical reduction of ACKs, when 17958 * applicable. The peer will also save on decyption and verification 17959 * costs. 17960 */ 17961 if (tcp->tcp_kssl_ctx != NULL) { 17962 stropt->so_wroff += SSL3_WROFFSET; 17963 17964 stropt->so_flags |= SO_TAIL; 17965 stropt->so_tail = SSL3_MAX_TAIL_LEN; 17966 17967 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 17968 } 17969 17970 /* Send the options up */ 17971 putnext(q, stropt_mp); 17972 17973 /* 17974 * Pass up any data and/or a fin that has been received. 17975 * 17976 * Adjust receive window in case it had decreased 17977 * (because there is data <=> tcp_rcv_list != NULL) 17978 * while the connection was detached. Note that 17979 * in case the eager was flow-controlled, w/o this 17980 * code, the rwnd may never open up again! 17981 */ 17982 if (tcp->tcp_rcv_list != NULL) { 17983 /* We drain directly in case of fused tcp loopback */ 17984 if (!tcp->tcp_fused && canputnext(q)) { 17985 tcp->tcp_rwnd = q->q_hiwat; 17986 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 17987 << tcp->tcp_rcv_ws; 17988 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 17989 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17990 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 17991 tcp_xmit_ctl(NULL, 17992 tcp, (tcp->tcp_swnd == 0) ? 17993 tcp->tcp_suna : tcp->tcp_snxt, 17994 tcp->tcp_rnxt, TH_ACK); 17995 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 17996 } 17997 17998 } 17999 (void) tcp_rcv_drain(q, tcp); 18000 18001 /* 18002 * For fused tcp loopback, back-enable peer endpoint 18003 * if it's currently flow-controlled. 18004 */ 18005 if (tcp->tcp_fused) { 18006 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18007 18008 ASSERT(peer_tcp != NULL); 18009 ASSERT(peer_tcp->tcp_fused); 18010 /* 18011 * In order to change the peer's tcp_flow_stopped, 18012 * we need to take locks for both end points. The 18013 * highest address is taken first. 18014 */ 18015 if (peer_tcp > tcp) { 18016 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18017 mutex_enter(&tcp->tcp_non_sq_lock); 18018 } else { 18019 mutex_enter(&tcp->tcp_non_sq_lock); 18020 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18021 } 18022 if (peer_tcp->tcp_flow_stopped) { 18023 tcp_clrqfull(peer_tcp); 18024 TCP_STAT(tcps, tcp_fusion_backenabled); 18025 } 18026 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18027 mutex_exit(&tcp->tcp_non_sq_lock); 18028 } 18029 } 18030 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18031 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18032 mp = mi_tpi_ordrel_ind(); 18033 if (mp) { 18034 tcp->tcp_ordrel_done = B_TRUE; 18035 putnext(q, mp); 18036 if (tcp->tcp_deferred_clean_death) { 18037 /* 18038 * tcp_clean_death was deferred 18039 * for T_ORDREL_IND - do it now 18040 */ 18041 (void) tcp_clean_death(tcp, 18042 tcp->tcp_client_errno, 21); 18043 tcp->tcp_deferred_clean_death = B_FALSE; 18044 } 18045 } else { 18046 /* 18047 * Run the orderly release in the 18048 * service routine. 18049 */ 18050 qenable(q); 18051 } 18052 } 18053 if (tcp->tcp_hard_binding) { 18054 tcp->tcp_hard_binding = B_FALSE; 18055 tcp->tcp_hard_bound = B_TRUE; 18056 } 18057 18058 tcp->tcp_detached = B_FALSE; 18059 18060 /* We can enable synchronous streams now */ 18061 if (tcp->tcp_fused) { 18062 tcp_fuse_syncstr_enable_pair(tcp); 18063 } 18064 18065 if (tcp->tcp_ka_enabled) { 18066 tcp->tcp_ka_last_intrvl = 0; 18067 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18068 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18069 } 18070 18071 /* 18072 * At this point, eager is fully established and will 18073 * have the following references - 18074 * 18075 * 2 references for connection to exist (1 for TCP and 1 for IP). 18076 * 1 reference for the squeue which will be dropped by the squeue as 18077 * soon as this function returns. 18078 * There will be 1 additonal reference for being in classifier 18079 * hash list provided something bad hasn't happened. 18080 */ 18081 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18082 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18083 } 18084 18085 /* 18086 * The function called through squeue to get behind listener's perimeter to 18087 * send a deffered conn_ind. 18088 */ 18089 /* ARGSUSED */ 18090 void 18091 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18092 { 18093 conn_t *connp = (conn_t *)arg; 18094 tcp_t *listener = connp->conn_tcp; 18095 18096 if (listener->tcp_state == TCPS_CLOSED || 18097 TCP_IS_DETACHED(listener)) { 18098 /* 18099 * If listener has closed, it would have caused a 18100 * a cleanup/blowoff to happen for the eager. 18101 */ 18102 tcp_t *tcp; 18103 struct T_conn_ind *conn_ind; 18104 18105 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18106 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18107 conn_ind->OPT_length); 18108 /* 18109 * We need to drop the ref on eager that was put 18110 * tcp_rput_data() before trying to send the conn_ind 18111 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18112 * and tcp_wput_accept() is sending this deferred conn_ind but 18113 * listener is closed so we drop the ref. 18114 */ 18115 CONN_DEC_REF(tcp->tcp_connp); 18116 freemsg(mp); 18117 return; 18118 } 18119 putnext(listener->tcp_rq, mp); 18120 } 18121 18122 18123 /* 18124 * This is the STREAMS entry point for T_CONN_RES coming down on 18125 * Acceptor STREAM when sockfs listener does accept processing. 18126 * Read the block comment on top of tcp_conn_request(). 18127 */ 18128 void 18129 tcp_wput_accept(queue_t *q, mblk_t *mp) 18130 { 18131 queue_t *rq = RD(q); 18132 struct T_conn_res *conn_res; 18133 tcp_t *eager; 18134 tcp_t *listener; 18135 struct T_ok_ack *ok; 18136 t_scalar_t PRIM_type; 18137 mblk_t *opt_mp; 18138 conn_t *econnp; 18139 18140 ASSERT(DB_TYPE(mp) == M_PROTO); 18141 18142 conn_res = (struct T_conn_res *)mp->b_rptr; 18143 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18144 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18145 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18146 if (mp != NULL) 18147 putnext(rq, mp); 18148 return; 18149 } 18150 switch (conn_res->PRIM_type) { 18151 case O_T_CONN_RES: 18152 case T_CONN_RES: 18153 /* 18154 * We pass up an err ack if allocb fails. This will 18155 * cause sockfs to issue a T_DISCON_REQ which will cause 18156 * tcp_eager_blowoff to be called. sockfs will then call 18157 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18158 * we need to do the allocb up here because we have to 18159 * make sure rq->q_qinfo->qi_qclose still points to the 18160 * correct function (tcpclose_accept) in case allocb 18161 * fails. 18162 */ 18163 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18164 if (opt_mp == NULL) { 18165 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18166 if (mp != NULL) 18167 putnext(rq, mp); 18168 return; 18169 } 18170 18171 bcopy(mp->b_rptr + conn_res->OPT_offset, 18172 &eager, conn_res->OPT_length); 18173 PRIM_type = conn_res->PRIM_type; 18174 mp->b_datap->db_type = M_PCPROTO; 18175 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18176 ok = (struct T_ok_ack *)mp->b_rptr; 18177 ok->PRIM_type = T_OK_ACK; 18178 ok->CORRECT_prim = PRIM_type; 18179 econnp = eager->tcp_connp; 18180 econnp->conn_dev = (dev_t)q->q_ptr; 18181 eager->tcp_rq = rq; 18182 eager->tcp_wq = q; 18183 rq->q_ptr = econnp; 18184 rq->q_qinfo = &tcp_rinit; 18185 q->q_ptr = econnp; 18186 q->q_qinfo = &tcp_winit; 18187 listener = eager->tcp_listener; 18188 eager->tcp_issocket = B_TRUE; 18189 18190 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18191 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18192 ASSERT(econnp->conn_netstack == 18193 listener->tcp_connp->conn_netstack); 18194 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18195 18196 /* Put the ref for IP */ 18197 CONN_INC_REF(econnp); 18198 18199 /* 18200 * We should have minimum of 3 references on the conn 18201 * at this point. One each for TCP and IP and one for 18202 * the T_conn_ind that was sent up when the 3-way handshake 18203 * completed. In the normal case we would also have another 18204 * reference (making a total of 4) for the conn being in the 18205 * classifier hash list. However the eager could have received 18206 * an RST subsequently and tcp_closei_local could have removed 18207 * the eager from the classifier hash list, hence we can't 18208 * assert that reference. 18209 */ 18210 ASSERT(econnp->conn_ref >= 3); 18211 18212 /* 18213 * Send the new local address also up to sockfs. There 18214 * should already be enough space in the mp that came 18215 * down from soaccept(). 18216 */ 18217 if (eager->tcp_family == AF_INET) { 18218 sin_t *sin; 18219 18220 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18221 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18222 sin = (sin_t *)mp->b_wptr; 18223 mp->b_wptr += sizeof (sin_t); 18224 sin->sin_family = AF_INET; 18225 sin->sin_port = eager->tcp_lport; 18226 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18227 } else { 18228 sin6_t *sin6; 18229 18230 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18231 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18232 sin6 = (sin6_t *)mp->b_wptr; 18233 mp->b_wptr += sizeof (sin6_t); 18234 sin6->sin6_family = AF_INET6; 18235 sin6->sin6_port = eager->tcp_lport; 18236 if (eager->tcp_ipversion == IPV4_VERSION) { 18237 sin6->sin6_flowinfo = 0; 18238 IN6_IPADDR_TO_V4MAPPED( 18239 eager->tcp_ipha->ipha_src, 18240 &sin6->sin6_addr); 18241 } else { 18242 ASSERT(eager->tcp_ip6h != NULL); 18243 sin6->sin6_flowinfo = 18244 eager->tcp_ip6h->ip6_vcf & 18245 ~IPV6_VERS_AND_FLOW_MASK; 18246 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18247 } 18248 sin6->sin6_scope_id = 0; 18249 sin6->__sin6_src_id = 0; 18250 } 18251 18252 putnext(rq, mp); 18253 18254 opt_mp->b_datap->db_type = M_SETOPTS; 18255 opt_mp->b_wptr += sizeof (struct stroptions); 18256 18257 /* 18258 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18259 * from listener to acceptor. The message is chained on the 18260 * bind_mp which tcp_rput_other will send down to IP. 18261 */ 18262 if (listener->tcp_bound_if != 0) { 18263 /* allocate optmgmt req */ 18264 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18265 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18266 sizeof (int)); 18267 if (mp != NULL) 18268 linkb(opt_mp, mp); 18269 } 18270 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18271 uint_t on = 1; 18272 18273 /* allocate optmgmt req */ 18274 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18275 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18276 if (mp != NULL) 18277 linkb(opt_mp, mp); 18278 } 18279 18280 18281 mutex_enter(&listener->tcp_eager_lock); 18282 18283 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18284 18285 tcp_t *tail; 18286 tcp_t *tcp; 18287 mblk_t *mp1; 18288 18289 tcp = listener->tcp_eager_prev_q0; 18290 /* 18291 * listener->tcp_eager_prev_q0 points to the TAIL of the 18292 * deferred T_conn_ind queue. We need to get to the head 18293 * of the queue in order to send up T_conn_ind the same 18294 * order as how the 3WHS is completed. 18295 */ 18296 while (tcp != listener) { 18297 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18298 !tcp->tcp_kssl_pending) 18299 break; 18300 else 18301 tcp = tcp->tcp_eager_prev_q0; 18302 } 18303 /* None of the pending eagers can be sent up now */ 18304 if (tcp == listener) 18305 goto no_more_eagers; 18306 18307 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18308 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18309 /* Move from q0 to q */ 18310 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18311 listener->tcp_conn_req_cnt_q0--; 18312 listener->tcp_conn_req_cnt_q++; 18313 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18314 tcp->tcp_eager_prev_q0; 18315 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18316 tcp->tcp_eager_next_q0; 18317 tcp->tcp_eager_prev_q0 = NULL; 18318 tcp->tcp_eager_next_q0 = NULL; 18319 tcp->tcp_conn_def_q0 = B_FALSE; 18320 18321 /* Make sure the tcp isn't in the list of droppables */ 18322 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18323 tcp->tcp_eager_prev_drop_q0 == NULL); 18324 18325 /* 18326 * Insert at end of the queue because sockfs sends 18327 * down T_CONN_RES in chronological order. Leaving 18328 * the older conn indications at front of the queue 18329 * helps reducing search time. 18330 */ 18331 tail = listener->tcp_eager_last_q; 18332 if (tail != NULL) { 18333 tail->tcp_eager_next_q = tcp; 18334 } else { 18335 listener->tcp_eager_next_q = tcp; 18336 } 18337 listener->tcp_eager_last_q = tcp; 18338 tcp->tcp_eager_next_q = NULL; 18339 18340 /* Need to get inside the listener perimeter */ 18341 CONN_INC_REF(listener->tcp_connp); 18342 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18343 tcp_send_pending, listener->tcp_connp, 18344 SQTAG_TCP_SEND_PENDING); 18345 } 18346 no_more_eagers: 18347 tcp_eager_unlink(eager); 18348 mutex_exit(&listener->tcp_eager_lock); 18349 18350 /* 18351 * At this point, the eager is detached from the listener 18352 * but we still have an extra refs on eager (apart from the 18353 * usual tcp references). The ref was placed in tcp_rput_data 18354 * before sending the conn_ind in tcp_send_conn_ind. 18355 * The ref will be dropped in tcp_accept_finish(). 18356 */ 18357 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18358 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18359 return; 18360 default: 18361 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18362 if (mp != NULL) 18363 putnext(rq, mp); 18364 return; 18365 } 18366 } 18367 18368 void 18369 tcp_wput(queue_t *q, mblk_t *mp) 18370 { 18371 conn_t *connp = Q_TO_CONN(q); 18372 tcp_t *tcp; 18373 void (*output_proc)(); 18374 t_scalar_t type; 18375 uchar_t *rptr; 18376 struct iocblk *iocp; 18377 uint32_t msize; 18378 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18379 18380 ASSERT(connp->conn_ref >= 2); 18381 18382 switch (DB_TYPE(mp)) { 18383 case M_DATA: 18384 tcp = connp->conn_tcp; 18385 ASSERT(tcp != NULL); 18386 18387 msize = msgdsize(mp); 18388 18389 mutex_enter(&tcp->tcp_non_sq_lock); 18390 tcp->tcp_squeue_bytes += msize; 18391 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18392 tcp_setqfull(tcp); 18393 } 18394 mutex_exit(&tcp->tcp_non_sq_lock); 18395 18396 CONN_INC_REF(connp); 18397 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18398 tcp_output, connp, SQTAG_TCP_OUTPUT); 18399 return; 18400 case M_PROTO: 18401 case M_PCPROTO: 18402 /* 18403 * if it is a snmp message, don't get behind the squeue 18404 */ 18405 tcp = connp->conn_tcp; 18406 rptr = mp->b_rptr; 18407 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18408 type = ((union T_primitives *)rptr)->type; 18409 } else { 18410 if (tcp->tcp_debug) { 18411 (void) strlog(TCP_MOD_ID, 0, 1, 18412 SL_ERROR|SL_TRACE, 18413 "tcp_wput_proto, dropping one..."); 18414 } 18415 freemsg(mp); 18416 return; 18417 } 18418 if (type == T_SVR4_OPTMGMT_REQ) { 18419 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18420 if (snmpcom_req(q, mp, tcp_snmp_set, tcp_snmp_get, 18421 cr)) { 18422 /* 18423 * This was a SNMP request 18424 */ 18425 return; 18426 } else { 18427 output_proc = tcp_wput_proto; 18428 } 18429 } else { 18430 output_proc = tcp_wput_proto; 18431 } 18432 break; 18433 case M_IOCTL: 18434 /* 18435 * Most ioctls can be processed right away without going via 18436 * squeues - process them right here. Those that do require 18437 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18438 * are processed by tcp_wput_ioctl(). 18439 */ 18440 iocp = (struct iocblk *)mp->b_rptr; 18441 tcp = connp->conn_tcp; 18442 18443 switch (iocp->ioc_cmd) { 18444 case TCP_IOC_ABORT_CONN: 18445 tcp_ioctl_abort_conn(q, mp); 18446 return; 18447 case TI_GETPEERNAME: 18448 if (tcp->tcp_state < TCPS_SYN_RCVD) { 18449 iocp->ioc_error = ENOTCONN; 18450 iocp->ioc_count = 0; 18451 mp->b_datap->db_type = M_IOCACK; 18452 qreply(q, mp); 18453 return; 18454 } 18455 /* FALLTHRU */ 18456 case TI_GETMYNAME: 18457 mi_copyin(q, mp, NULL, 18458 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18459 return; 18460 case ND_SET: 18461 /* nd_getset does the necessary checks */ 18462 case ND_GET: 18463 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18464 CALL_IP_WPUT(connp, q, mp); 18465 return; 18466 } 18467 qreply(q, mp); 18468 return; 18469 case TCP_IOC_DEFAULT_Q: 18470 /* 18471 * Wants to be the default wq. Check the credentials 18472 * first, the rest is executed via squeue. 18473 */ 18474 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18475 iocp->ioc_error = EPERM; 18476 iocp->ioc_count = 0; 18477 mp->b_datap->db_type = M_IOCACK; 18478 qreply(q, mp); 18479 return; 18480 } 18481 output_proc = tcp_wput_ioctl; 18482 break; 18483 default: 18484 output_proc = tcp_wput_ioctl; 18485 break; 18486 } 18487 break; 18488 default: 18489 output_proc = tcp_wput_nondata; 18490 break; 18491 } 18492 18493 CONN_INC_REF(connp); 18494 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18495 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18496 } 18497 18498 /* 18499 * Initial STREAMS write side put() procedure for sockets. It tries to 18500 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18501 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18502 * are handled by tcp_wput() as usual. 18503 * 18504 * All further messages will also be handled by tcp_wput() because we cannot 18505 * be sure that the above short cut is safe later. 18506 */ 18507 static void 18508 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18509 { 18510 conn_t *connp = Q_TO_CONN(wq); 18511 tcp_t *tcp = connp->conn_tcp; 18512 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18513 18514 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18515 wq->q_qinfo = &tcp_winit; 18516 18517 ASSERT(IPCL_IS_TCP(connp)); 18518 ASSERT(TCP_IS_SOCKET(tcp)); 18519 18520 if (DB_TYPE(mp) == M_PCPROTO && 18521 MBLKL(mp) == sizeof (struct T_capability_req) && 18522 car->PRIM_type == T_CAPABILITY_REQ) { 18523 tcp_capability_req(tcp, mp); 18524 return; 18525 } 18526 18527 tcp_wput(wq, mp); 18528 } 18529 18530 static boolean_t 18531 tcp_zcopy_check(tcp_t *tcp) 18532 { 18533 conn_t *connp = tcp->tcp_connp; 18534 ire_t *ire; 18535 boolean_t zc_enabled = B_FALSE; 18536 tcp_stack_t *tcps = tcp->tcp_tcps; 18537 18538 if (do_tcpzcopy == 2) 18539 zc_enabled = B_TRUE; 18540 else if (tcp->tcp_ipversion == IPV4_VERSION && 18541 IPCL_IS_CONNECTED(connp) && 18542 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18543 connp->conn_dontroute == 0 && 18544 !connp->conn_nexthop_set && 18545 connp->conn_xmit_if_ill == NULL && 18546 connp->conn_nofailover_ill == NULL && 18547 do_tcpzcopy == 1) { 18548 /* 18549 * the checks above closely resemble the fast path checks 18550 * in tcp_send_data(). 18551 */ 18552 mutex_enter(&connp->conn_lock); 18553 ire = connp->conn_ire_cache; 18554 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18555 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18556 IRE_REFHOLD(ire); 18557 if (ire->ire_stq != NULL) { 18558 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18559 18560 zc_enabled = ill && (ill->ill_capabilities & 18561 ILL_CAPAB_ZEROCOPY) && 18562 (ill->ill_zerocopy_capab-> 18563 ill_zerocopy_flags != 0); 18564 } 18565 IRE_REFRELE(ire); 18566 } 18567 mutex_exit(&connp->conn_lock); 18568 } 18569 tcp->tcp_snd_zcopy_on = zc_enabled; 18570 if (!TCP_IS_DETACHED(tcp)) { 18571 if (zc_enabled) { 18572 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 18573 TCP_STAT(tcps, tcp_zcopy_on); 18574 } else { 18575 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18576 TCP_STAT(tcps, tcp_zcopy_off); 18577 } 18578 } 18579 return (zc_enabled); 18580 } 18581 18582 static mblk_t * 18583 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18584 { 18585 tcp_stack_t *tcps = tcp->tcp_tcps; 18586 18587 if (do_tcpzcopy == 2) 18588 return (bp); 18589 else if (tcp->tcp_snd_zcopy_on) { 18590 tcp->tcp_snd_zcopy_on = B_FALSE; 18591 if (!TCP_IS_DETACHED(tcp)) { 18592 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18593 TCP_STAT(tcps, tcp_zcopy_disable); 18594 } 18595 } 18596 return (tcp_zcopy_backoff(tcp, bp, 0)); 18597 } 18598 18599 /* 18600 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18601 * the original desballoca'ed segmapped mblk. 18602 */ 18603 static mblk_t * 18604 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18605 { 18606 mblk_t *head, *tail, *nbp; 18607 tcp_stack_t *tcps = tcp->tcp_tcps; 18608 18609 if (IS_VMLOANED_MBLK(bp)) { 18610 TCP_STAT(tcps, tcp_zcopy_backoff); 18611 if ((head = copyb(bp)) == NULL) { 18612 /* fail to backoff; leave it for the next backoff */ 18613 tcp->tcp_xmit_zc_clean = B_FALSE; 18614 return (bp); 18615 } 18616 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18617 if (fix_xmitlist) 18618 tcp_zcopy_notify(tcp); 18619 else 18620 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18621 } 18622 nbp = bp->b_cont; 18623 if (fix_xmitlist) { 18624 head->b_prev = bp->b_prev; 18625 head->b_next = bp->b_next; 18626 if (tcp->tcp_xmit_tail == bp) 18627 tcp->tcp_xmit_tail = head; 18628 } 18629 bp->b_next = NULL; 18630 bp->b_prev = NULL; 18631 freeb(bp); 18632 } else { 18633 head = bp; 18634 nbp = bp->b_cont; 18635 } 18636 tail = head; 18637 while (nbp) { 18638 if (IS_VMLOANED_MBLK(nbp)) { 18639 TCP_STAT(tcps, tcp_zcopy_backoff); 18640 if ((tail->b_cont = copyb(nbp)) == NULL) { 18641 tcp->tcp_xmit_zc_clean = B_FALSE; 18642 tail->b_cont = nbp; 18643 return (head); 18644 } 18645 tail = tail->b_cont; 18646 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18647 if (fix_xmitlist) 18648 tcp_zcopy_notify(tcp); 18649 else 18650 tail->b_datap->db_struioflag |= 18651 STRUIO_ZCNOTIFY; 18652 } 18653 bp = nbp; 18654 nbp = nbp->b_cont; 18655 if (fix_xmitlist) { 18656 tail->b_prev = bp->b_prev; 18657 tail->b_next = bp->b_next; 18658 if (tcp->tcp_xmit_tail == bp) 18659 tcp->tcp_xmit_tail = tail; 18660 } 18661 bp->b_next = NULL; 18662 bp->b_prev = NULL; 18663 freeb(bp); 18664 } else { 18665 tail->b_cont = nbp; 18666 tail = nbp; 18667 nbp = nbp->b_cont; 18668 } 18669 } 18670 if (fix_xmitlist) { 18671 tcp->tcp_xmit_last = tail; 18672 tcp->tcp_xmit_zc_clean = B_TRUE; 18673 } 18674 return (head); 18675 } 18676 18677 static void 18678 tcp_zcopy_notify(tcp_t *tcp) 18679 { 18680 struct stdata *stp; 18681 18682 if (tcp->tcp_detached) 18683 return; 18684 stp = STREAM(tcp->tcp_rq); 18685 mutex_enter(&stp->sd_lock); 18686 stp->sd_flag |= STZCNOTIFY; 18687 cv_broadcast(&stp->sd_zcopy_wait); 18688 mutex_exit(&stp->sd_lock); 18689 } 18690 18691 static boolean_t 18692 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18693 { 18694 ire_t *ire; 18695 conn_t *connp = tcp->tcp_connp; 18696 tcp_stack_t *tcps = tcp->tcp_tcps; 18697 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18698 18699 mutex_enter(&connp->conn_lock); 18700 ire = connp->conn_ire_cache; 18701 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18702 18703 if ((ire != NULL) && 18704 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18705 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18706 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18707 IRE_REFHOLD(ire); 18708 mutex_exit(&connp->conn_lock); 18709 } else { 18710 boolean_t cached = B_FALSE; 18711 ts_label_t *tsl; 18712 18713 /* force a recheck later on */ 18714 tcp->tcp_ire_ill_check_done = B_FALSE; 18715 18716 TCP_DBGSTAT(tcps, tcp_ire_null1); 18717 connp->conn_ire_cache = NULL; 18718 mutex_exit(&connp->conn_lock); 18719 18720 if (ire != NULL) 18721 IRE_REFRELE_NOTR(ire); 18722 18723 tsl = crgetlabel(CONN_CRED(connp)); 18724 ire = (dst ? 18725 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18726 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18727 connp->conn_zoneid, tsl, ipst)); 18728 18729 if (ire == NULL) { 18730 TCP_STAT(tcps, tcp_ire_null); 18731 return (B_FALSE); 18732 } 18733 18734 IRE_REFHOLD_NOTR(ire); 18735 /* 18736 * Since we are inside the squeue, there cannot be another 18737 * thread in TCP trying to set the conn_ire_cache now. The 18738 * check for IRE_MARK_CONDEMNED ensures that an interface 18739 * unplumb thread has not yet started cleaning up the conns. 18740 * Hence we don't need to grab the conn lock. 18741 */ 18742 if (!(connp->conn_state_flags & CONN_CLOSING)) { 18743 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18744 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18745 connp->conn_ire_cache = ire; 18746 cached = B_TRUE; 18747 } 18748 rw_exit(&ire->ire_bucket->irb_lock); 18749 } 18750 18751 /* 18752 * We can continue to use the ire but since it was 18753 * not cached, we should drop the extra reference. 18754 */ 18755 if (!cached) 18756 IRE_REFRELE_NOTR(ire); 18757 18758 /* 18759 * Rampart note: no need to select a new label here, since 18760 * labels are not allowed to change during the life of a TCP 18761 * connection. 18762 */ 18763 } 18764 18765 *irep = ire; 18766 18767 return (B_TRUE); 18768 } 18769 18770 /* 18771 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18772 * 18773 * 0 = success; 18774 * 1 = failed to find ire and ill. 18775 */ 18776 static boolean_t 18777 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18778 { 18779 ipha_t *ipha; 18780 ipaddr_t dst; 18781 ire_t *ire; 18782 ill_t *ill; 18783 conn_t *connp = tcp->tcp_connp; 18784 mblk_t *ire_fp_mp; 18785 tcp_stack_t *tcps = tcp->tcp_tcps; 18786 18787 if (mp != NULL) 18788 ipha = (ipha_t *)mp->b_rptr; 18789 else 18790 ipha = tcp->tcp_ipha; 18791 dst = ipha->ipha_dst; 18792 18793 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18794 return (B_FALSE); 18795 18796 if ((ire->ire_flags & RTF_MULTIRT) || 18797 (ire->ire_stq == NULL) || 18798 (ire->ire_nce == NULL) || 18799 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18800 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18801 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18802 TCP_STAT(tcps, tcp_ip_ire_send); 18803 IRE_REFRELE(ire); 18804 return (B_FALSE); 18805 } 18806 18807 ill = ire_to_ill(ire); 18808 if (connp->conn_outgoing_ill != NULL) { 18809 ill_t *conn_outgoing_ill = NULL; 18810 /* 18811 * Choose a good ill in the group to send the packets on. 18812 */ 18813 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18814 ill = ire_to_ill(ire); 18815 } 18816 ASSERT(ill != NULL); 18817 18818 if (!tcp->tcp_ire_ill_check_done) { 18819 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18820 tcp->tcp_ire_ill_check_done = B_TRUE; 18821 } 18822 18823 *irep = ire; 18824 *illp = ill; 18825 18826 return (B_TRUE); 18827 } 18828 18829 static void 18830 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18831 { 18832 ipha_t *ipha; 18833 ipaddr_t src; 18834 ipaddr_t dst; 18835 uint32_t cksum; 18836 ire_t *ire; 18837 uint16_t *up; 18838 ill_t *ill; 18839 conn_t *connp = tcp->tcp_connp; 18840 uint32_t hcksum_txflags = 0; 18841 mblk_t *ire_fp_mp; 18842 uint_t ire_fp_mp_len; 18843 tcp_stack_t *tcps = tcp->tcp_tcps; 18844 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18845 18846 ASSERT(DB_TYPE(mp) == M_DATA); 18847 18848 if (DB_CRED(mp) == NULL) 18849 mblk_setcred(mp, CONN_CRED(connp)); 18850 18851 ipha = (ipha_t *)mp->b_rptr; 18852 src = ipha->ipha_src; 18853 dst = ipha->ipha_dst; 18854 18855 /* 18856 * Drop off fast path for IPv6 and also if options are present or 18857 * we need to resolve a TS label. 18858 */ 18859 if (tcp->tcp_ipversion != IPV4_VERSION || 18860 !IPCL_IS_CONNECTED(connp) || 18861 !CONN_IS_LSO_MD_FASTPATH(connp) || 18862 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18863 !connp->conn_ulp_labeled || 18864 ipha->ipha_ident == IP_HDR_INCLUDED || 18865 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18866 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18867 if (tcp->tcp_snd_zcopy_aware) 18868 mp = tcp_zcopy_disable(tcp, mp); 18869 TCP_STAT(tcps, tcp_ip_send); 18870 CALL_IP_WPUT(connp, q, mp); 18871 return; 18872 } 18873 18874 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18875 if (tcp->tcp_snd_zcopy_aware) 18876 mp = tcp_zcopy_backoff(tcp, mp, 0); 18877 CALL_IP_WPUT(connp, q, mp); 18878 return; 18879 } 18880 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18881 ire_fp_mp_len = MBLKL(ire_fp_mp); 18882 18883 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18884 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18885 #ifndef _BIG_ENDIAN 18886 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18887 #endif 18888 18889 /* 18890 * Check to see if we need to re-enable LSO/MDT for this connection 18891 * because it was previously disabled due to changes in the ill; 18892 * note that by doing it here, this re-enabling only applies when 18893 * the packet is not dispatched through CALL_IP_WPUT(). 18894 * 18895 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18896 * case, since that's how we ended up here. For IPv6, we do the 18897 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18898 */ 18899 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18900 /* 18901 * Restore LSO for this connection, so that next time around 18902 * it is eligible to go through tcp_lsosend() path again. 18903 */ 18904 TCP_STAT(tcps, tcp_lso_enabled); 18905 tcp->tcp_lso = B_TRUE; 18906 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18907 "interface %s\n", (void *)connp, ill->ill_name)); 18908 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18909 /* 18910 * Restore MDT for this connection, so that next time around 18911 * it is eligible to go through tcp_multisend() path again. 18912 */ 18913 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18914 tcp->tcp_mdt = B_TRUE; 18915 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18916 "interface %s\n", (void *)connp, ill->ill_name)); 18917 } 18918 18919 if (tcp->tcp_snd_zcopy_aware) { 18920 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18921 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18922 mp = tcp_zcopy_disable(tcp, mp); 18923 /* 18924 * we shouldn't need to reset ipha as the mp containing 18925 * ipha should never be a zero-copy mp. 18926 */ 18927 } 18928 18929 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18930 ASSERT(ill->ill_hcksum_capab != NULL); 18931 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18932 } 18933 18934 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18935 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18936 18937 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18938 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18939 18940 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18941 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18942 18943 /* Software checksum? */ 18944 if (DB_CKSUMFLAGS(mp) == 0) { 18945 TCP_STAT(tcps, tcp_out_sw_cksum); 18946 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18947 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18948 } 18949 18950 ipha->ipha_fragment_offset_and_flags |= 18951 (uint32_t)htons(ire->ire_frag_flag); 18952 18953 /* Calculate IP header checksum if hardware isn't capable */ 18954 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18955 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18956 ((uint16_t *)ipha)[4]); 18957 } 18958 18959 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18960 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18961 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18962 18963 UPDATE_OB_PKT_COUNT(ire); 18964 ire->ire_last_used_time = lbolt; 18965 18966 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 18967 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 18968 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 18969 ntohs(ipha->ipha_length)); 18970 18971 if (ILL_DLS_CAPABLE(ill)) { 18972 /* 18973 * Send the packet directly to DLD, where it may be queued 18974 * depending on the availability of transmit resources at 18975 * the media layer. 18976 */ 18977 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 18978 } else { 18979 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 18980 DTRACE_PROBE4(ip4__physical__out__start, 18981 ill_t *, NULL, ill_t *, out_ill, 18982 ipha_t *, ipha, mblk_t *, mp); 18983 FW_HOOKS(ipst->ips_ip4_physical_out_event, 18984 ipst->ips_ipv4firewall_physical_out, 18985 NULL, out_ill, ipha, mp, mp, ipst); 18986 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 18987 if (mp != NULL) 18988 putnext(ire->ire_stq, mp); 18989 } 18990 IRE_REFRELE(ire); 18991 } 18992 18993 /* 18994 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18995 * if the receiver shrinks the window, i.e. moves the right window to the 18996 * left, the we should not send new data, but should retransmit normally the 18997 * old unacked data between suna and suna + swnd. We might has sent data 18998 * that is now outside the new window, pretend that we didn't send it. 18999 */ 19000 static void 19001 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19002 { 19003 uint32_t snxt = tcp->tcp_snxt; 19004 mblk_t *xmit_tail; 19005 int32_t offset; 19006 19007 ASSERT(shrunk_count > 0); 19008 19009 /* Pretend we didn't send the data outside the window */ 19010 snxt -= shrunk_count; 19011 19012 /* Get the mblk and the offset in it per the shrunk window */ 19013 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19014 19015 ASSERT(xmit_tail != NULL); 19016 19017 /* Reset all the values per the now shrunk window */ 19018 tcp->tcp_snxt = snxt; 19019 tcp->tcp_xmit_tail = xmit_tail; 19020 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19021 offset; 19022 tcp->tcp_unsent += shrunk_count; 19023 19024 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19025 /* 19026 * Make sure the timer is running so that we will probe a zero 19027 * window. 19028 */ 19029 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19030 } 19031 19032 19033 /* 19034 * The TCP normal data output path. 19035 * NOTE: the logic of the fast path is duplicated from this function. 19036 */ 19037 static void 19038 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19039 { 19040 int len; 19041 mblk_t *local_time; 19042 mblk_t *mp1; 19043 uint32_t snxt; 19044 int tail_unsent; 19045 int tcpstate; 19046 int usable = 0; 19047 mblk_t *xmit_tail; 19048 queue_t *q = tcp->tcp_wq; 19049 int32_t mss; 19050 int32_t num_sack_blk = 0; 19051 int32_t tcp_hdr_len; 19052 int32_t tcp_tcp_hdr_len; 19053 int mdt_thres; 19054 int rc; 19055 tcp_stack_t *tcps = tcp->tcp_tcps; 19056 ip_stack_t *ipst; 19057 19058 tcpstate = tcp->tcp_state; 19059 if (mp == NULL) { 19060 /* 19061 * tcp_wput_data() with NULL mp should only be called when 19062 * there is unsent data. 19063 */ 19064 ASSERT(tcp->tcp_unsent > 0); 19065 /* Really tacky... but we need this for detached closes. */ 19066 len = tcp->tcp_unsent; 19067 goto data_null; 19068 } 19069 19070 #if CCS_STATS 19071 wrw_stats.tot.count++; 19072 wrw_stats.tot.bytes += msgdsize(mp); 19073 #endif 19074 ASSERT(mp->b_datap->db_type == M_DATA); 19075 /* 19076 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19077 * or before a connection attempt has begun. 19078 */ 19079 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19080 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19081 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19082 #ifdef DEBUG 19083 cmn_err(CE_WARN, 19084 "tcp_wput_data: data after ordrel, %s", 19085 tcp_display(tcp, NULL, 19086 DISP_ADDR_AND_PORT)); 19087 #else 19088 if (tcp->tcp_debug) { 19089 (void) strlog(TCP_MOD_ID, 0, 1, 19090 SL_TRACE|SL_ERROR, 19091 "tcp_wput_data: data after ordrel, %s\n", 19092 tcp_display(tcp, NULL, 19093 DISP_ADDR_AND_PORT)); 19094 } 19095 #endif /* DEBUG */ 19096 } 19097 if (tcp->tcp_snd_zcopy_aware && 19098 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19099 tcp_zcopy_notify(tcp); 19100 freemsg(mp); 19101 mutex_enter(&tcp->tcp_non_sq_lock); 19102 if (tcp->tcp_flow_stopped && 19103 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19104 tcp_clrqfull(tcp); 19105 } 19106 mutex_exit(&tcp->tcp_non_sq_lock); 19107 return; 19108 } 19109 19110 /* Strip empties */ 19111 for (;;) { 19112 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19113 (uintptr_t)INT_MAX); 19114 len = (int)(mp->b_wptr - mp->b_rptr); 19115 if (len > 0) 19116 break; 19117 mp1 = mp; 19118 mp = mp->b_cont; 19119 freeb(mp1); 19120 if (!mp) { 19121 return; 19122 } 19123 } 19124 19125 /* If we are the first on the list ... */ 19126 if (tcp->tcp_xmit_head == NULL) { 19127 tcp->tcp_xmit_head = mp; 19128 tcp->tcp_xmit_tail = mp; 19129 tcp->tcp_xmit_tail_unsent = len; 19130 } else { 19131 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19132 struct datab *dp; 19133 19134 mp1 = tcp->tcp_xmit_last; 19135 if (len < tcp_tx_pull_len && 19136 (dp = mp1->b_datap)->db_ref == 1 && 19137 dp->db_lim - mp1->b_wptr >= len) { 19138 ASSERT(len > 0); 19139 ASSERT(!mp1->b_cont); 19140 if (len == 1) { 19141 *mp1->b_wptr++ = *mp->b_rptr; 19142 } else { 19143 bcopy(mp->b_rptr, mp1->b_wptr, len); 19144 mp1->b_wptr += len; 19145 } 19146 if (mp1 == tcp->tcp_xmit_tail) 19147 tcp->tcp_xmit_tail_unsent += len; 19148 mp1->b_cont = mp->b_cont; 19149 if (tcp->tcp_snd_zcopy_aware && 19150 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19151 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19152 freeb(mp); 19153 mp = mp1; 19154 } else { 19155 tcp->tcp_xmit_last->b_cont = mp; 19156 } 19157 len += tcp->tcp_unsent; 19158 } 19159 19160 /* Tack on however many more positive length mblks we have */ 19161 if ((mp1 = mp->b_cont) != NULL) { 19162 do { 19163 int tlen; 19164 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19165 (uintptr_t)INT_MAX); 19166 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19167 if (tlen <= 0) { 19168 mp->b_cont = mp1->b_cont; 19169 freeb(mp1); 19170 } else { 19171 len += tlen; 19172 mp = mp1; 19173 } 19174 } while ((mp1 = mp->b_cont) != NULL); 19175 } 19176 tcp->tcp_xmit_last = mp; 19177 tcp->tcp_unsent = len; 19178 19179 if (urgent) 19180 usable = 1; 19181 19182 data_null: 19183 snxt = tcp->tcp_snxt; 19184 xmit_tail = tcp->tcp_xmit_tail; 19185 tail_unsent = tcp->tcp_xmit_tail_unsent; 19186 19187 /* 19188 * Note that tcp_mss has been adjusted to take into account the 19189 * timestamp option if applicable. Because SACK options do not 19190 * appear in every TCP segments and they are of variable lengths, 19191 * they cannot be included in tcp_mss. Thus we need to calculate 19192 * the actual segment length when we need to send a segment which 19193 * includes SACK options. 19194 */ 19195 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19196 int32_t opt_len; 19197 19198 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19199 tcp->tcp_num_sack_blk); 19200 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19201 2 + TCPOPT_HEADER_LEN; 19202 mss = tcp->tcp_mss - opt_len; 19203 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19204 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19205 } else { 19206 mss = tcp->tcp_mss; 19207 tcp_hdr_len = tcp->tcp_hdr_len; 19208 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19209 } 19210 19211 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19212 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19213 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19214 } 19215 if (tcpstate == TCPS_SYN_RCVD) { 19216 /* 19217 * The three-way connection establishment handshake is not 19218 * complete yet. We want to queue the data for transmission 19219 * after entering ESTABLISHED state (RFC793). A jump to 19220 * "done" label effectively leaves data on the queue. 19221 */ 19222 goto done; 19223 } else { 19224 int usable_r; 19225 19226 /* 19227 * In the special case when cwnd is zero, which can only 19228 * happen if the connection is ECN capable, return now. 19229 * New segments is sent using tcp_timer(). The timer 19230 * is set in tcp_rput_data(). 19231 */ 19232 if (tcp->tcp_cwnd == 0) { 19233 /* 19234 * Note that tcp_cwnd is 0 before 3-way handshake is 19235 * finished. 19236 */ 19237 ASSERT(tcp->tcp_ecn_ok || 19238 tcp->tcp_state < TCPS_ESTABLISHED); 19239 return; 19240 } 19241 19242 /* NOTE: trouble if xmitting while SYN not acked? */ 19243 usable_r = snxt - tcp->tcp_suna; 19244 usable_r = tcp->tcp_swnd - usable_r; 19245 19246 /* 19247 * Check if the receiver has shrunk the window. If 19248 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19249 * cannot be set as there is unsent data, so FIN cannot 19250 * be sent out. Otherwise, we need to take into account 19251 * of FIN as it consumes an "invisible" sequence number. 19252 */ 19253 ASSERT(tcp->tcp_fin_sent == 0); 19254 if (usable_r < 0) { 19255 /* 19256 * The receiver has shrunk the window and we have sent 19257 * -usable_r date beyond the window, re-adjust. 19258 * 19259 * If TCP window scaling is enabled, there can be 19260 * round down error as the advertised receive window 19261 * is actually right shifted n bits. This means that 19262 * the lower n bits info is wiped out. It will look 19263 * like the window is shrunk. Do a check here to 19264 * see if the shrunk amount is actually within the 19265 * error in window calculation. If it is, just 19266 * return. Note that this check is inside the 19267 * shrunk window check. This makes sure that even 19268 * though tcp_process_shrunk_swnd() is not called, 19269 * we will stop further processing. 19270 */ 19271 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19272 tcp_process_shrunk_swnd(tcp, -usable_r); 19273 } 19274 return; 19275 } 19276 19277 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19278 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19279 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19280 19281 /* usable = MIN(usable, unsent) */ 19282 if (usable_r > len) 19283 usable_r = len; 19284 19285 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19286 if (usable_r > 0) { 19287 usable = usable_r; 19288 } else { 19289 /* Bypass all other unnecessary processing. */ 19290 goto done; 19291 } 19292 } 19293 19294 local_time = (mblk_t *)lbolt; 19295 19296 /* 19297 * "Our" Nagle Algorithm. This is not the same as in the old 19298 * BSD. This is more in line with the true intent of Nagle. 19299 * 19300 * The conditions are: 19301 * 1. The amount of unsent data (or amount of data which can be 19302 * sent, whichever is smaller) is less than Nagle limit. 19303 * 2. The last sent size is also less than Nagle limit. 19304 * 3. There is unack'ed data. 19305 * 4. Urgent pointer is not set. Send urgent data ignoring the 19306 * Nagle algorithm. This reduces the probability that urgent 19307 * bytes get "merged" together. 19308 * 5. The app has not closed the connection. This eliminates the 19309 * wait time of the receiving side waiting for the last piece of 19310 * (small) data. 19311 * 19312 * If all are satisified, exit without sending anything. Note 19313 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19314 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19315 * 4095). 19316 */ 19317 if (usable < (int)tcp->tcp_naglim && 19318 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19319 snxt != tcp->tcp_suna && 19320 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19321 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19322 goto done; 19323 } 19324 19325 if (tcp->tcp_cork) { 19326 /* 19327 * if the tcp->tcp_cork option is set, then we have to force 19328 * TCP not to send partial segment (smaller than MSS bytes). 19329 * We are calculating the usable now based on full mss and 19330 * will save the rest of remaining data for later. 19331 */ 19332 if (usable < mss) 19333 goto done; 19334 usable = (usable / mss) * mss; 19335 } 19336 19337 /* Update the latest receive window size in TCP header. */ 19338 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19339 tcp->tcp_tcph->th_win); 19340 19341 /* 19342 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19343 * 19344 * 1. Simple TCP/IP{v4,v6} (no options). 19345 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19346 * 3. If the TCP connection is in ESTABLISHED state. 19347 * 4. The TCP is not detached. 19348 * 19349 * If any of the above conditions have changed during the 19350 * connection, stop using LSO/MDT and restore the stream head 19351 * parameters accordingly. 19352 */ 19353 ipst = tcps->tcps_netstack->netstack_ip; 19354 19355 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19356 ((tcp->tcp_ipversion == IPV4_VERSION && 19357 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19358 (tcp->tcp_ipversion == IPV6_VERSION && 19359 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19360 tcp->tcp_state != TCPS_ESTABLISHED || 19361 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19362 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19363 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19364 if (tcp->tcp_lso) { 19365 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19366 tcp->tcp_lso = B_FALSE; 19367 } else { 19368 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19369 tcp->tcp_mdt = B_FALSE; 19370 } 19371 19372 /* Anything other than detached is considered pathological */ 19373 if (!TCP_IS_DETACHED(tcp)) { 19374 if (tcp->tcp_lso) 19375 TCP_STAT(tcps, tcp_lso_disabled); 19376 else 19377 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19378 (void) tcp_maxpsz_set(tcp, B_TRUE); 19379 } 19380 } 19381 19382 /* Use MDT if sendable amount is greater than the threshold */ 19383 if (tcp->tcp_mdt && 19384 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19385 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19386 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19387 (tcp->tcp_valid_bits == 0 || 19388 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19389 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19390 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19391 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19392 local_time, mdt_thres); 19393 } else { 19394 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19395 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19396 local_time, INT_MAX); 19397 } 19398 19399 /* Pretend that all we were trying to send really got sent */ 19400 if (rc < 0 && tail_unsent < 0) { 19401 do { 19402 xmit_tail = xmit_tail->b_cont; 19403 xmit_tail->b_prev = local_time; 19404 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19405 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19406 tail_unsent += (int)(xmit_tail->b_wptr - 19407 xmit_tail->b_rptr); 19408 } while (tail_unsent < 0); 19409 } 19410 done:; 19411 tcp->tcp_xmit_tail = xmit_tail; 19412 tcp->tcp_xmit_tail_unsent = tail_unsent; 19413 len = tcp->tcp_snxt - snxt; 19414 if (len) { 19415 /* 19416 * If new data was sent, need to update the notsack 19417 * list, which is, afterall, data blocks that have 19418 * not been sack'ed by the receiver. New data is 19419 * not sack'ed. 19420 */ 19421 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19422 /* len is a negative value. */ 19423 tcp->tcp_pipe -= len; 19424 tcp_notsack_update(&(tcp->tcp_notsack_list), 19425 tcp->tcp_snxt, snxt, 19426 &(tcp->tcp_num_notsack_blk), 19427 &(tcp->tcp_cnt_notsack_list)); 19428 } 19429 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19430 tcp->tcp_rack = tcp->tcp_rnxt; 19431 tcp->tcp_rack_cnt = 0; 19432 if ((snxt + len) == tcp->tcp_suna) { 19433 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19434 } 19435 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19436 /* 19437 * Didn't send anything. Make sure the timer is running 19438 * so that we will probe a zero window. 19439 */ 19440 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19441 } 19442 /* Note that len is the amount we just sent but with a negative sign */ 19443 tcp->tcp_unsent += len; 19444 mutex_enter(&tcp->tcp_non_sq_lock); 19445 if (tcp->tcp_flow_stopped) { 19446 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19447 tcp_clrqfull(tcp); 19448 } 19449 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19450 tcp_setqfull(tcp); 19451 } 19452 mutex_exit(&tcp->tcp_non_sq_lock); 19453 } 19454 19455 /* 19456 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19457 * outgoing TCP header with the template header, as well as other 19458 * options such as time-stamp, ECN and/or SACK. 19459 */ 19460 static void 19461 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19462 { 19463 tcph_t *tcp_tmpl, *tcp_h; 19464 uint32_t *dst, *src; 19465 int hdrlen; 19466 19467 ASSERT(OK_32PTR(rptr)); 19468 19469 /* Template header */ 19470 tcp_tmpl = tcp->tcp_tcph; 19471 19472 /* Header of outgoing packet */ 19473 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19474 19475 /* dst and src are opaque 32-bit fields, used for copying */ 19476 dst = (uint32_t *)rptr; 19477 src = (uint32_t *)tcp->tcp_iphc; 19478 hdrlen = tcp->tcp_hdr_len; 19479 19480 /* Fill time-stamp option if needed */ 19481 if (tcp->tcp_snd_ts_ok) { 19482 U32_TO_BE32((uint32_t)now, 19483 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19484 U32_TO_BE32(tcp->tcp_ts_recent, 19485 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19486 } else { 19487 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19488 } 19489 19490 /* 19491 * Copy the template header; is this really more efficient than 19492 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19493 * but perhaps not for other scenarios. 19494 */ 19495 dst[0] = src[0]; 19496 dst[1] = src[1]; 19497 dst[2] = src[2]; 19498 dst[3] = src[3]; 19499 dst[4] = src[4]; 19500 dst[5] = src[5]; 19501 dst[6] = src[6]; 19502 dst[7] = src[7]; 19503 dst[8] = src[8]; 19504 dst[9] = src[9]; 19505 if (hdrlen -= 40) { 19506 hdrlen >>= 2; 19507 dst += 10; 19508 src += 10; 19509 do { 19510 *dst++ = *src++; 19511 } while (--hdrlen); 19512 } 19513 19514 /* 19515 * Set the ECN info in the TCP header if it is not a zero 19516 * window probe. Zero window probe is only sent in 19517 * tcp_wput_data() and tcp_timer(). 19518 */ 19519 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19520 SET_ECT(tcp, rptr); 19521 19522 if (tcp->tcp_ecn_echo_on) 19523 tcp_h->th_flags[0] |= TH_ECE; 19524 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19525 tcp_h->th_flags[0] |= TH_CWR; 19526 tcp->tcp_ecn_cwr_sent = B_TRUE; 19527 } 19528 } 19529 19530 /* Fill in SACK options */ 19531 if (num_sack_blk > 0) { 19532 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19533 sack_blk_t *tmp; 19534 int32_t i; 19535 19536 wptr[0] = TCPOPT_NOP; 19537 wptr[1] = TCPOPT_NOP; 19538 wptr[2] = TCPOPT_SACK; 19539 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19540 sizeof (sack_blk_t); 19541 wptr += TCPOPT_REAL_SACK_LEN; 19542 19543 tmp = tcp->tcp_sack_list; 19544 for (i = 0; i < num_sack_blk; i++) { 19545 U32_TO_BE32(tmp[i].begin, wptr); 19546 wptr += sizeof (tcp_seq); 19547 U32_TO_BE32(tmp[i].end, wptr); 19548 wptr += sizeof (tcp_seq); 19549 } 19550 tcp_h->th_offset_and_rsrvd[0] += 19551 ((num_sack_blk * 2 + 1) << 4); 19552 } 19553 } 19554 19555 /* 19556 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19557 * the destination address and SAP attribute, and if necessary, the 19558 * hardware checksum offload attribute to a Multidata message. 19559 */ 19560 static int 19561 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19562 const uint32_t start, const uint32_t stuff, const uint32_t end, 19563 const uint32_t flags, tcp_stack_t *tcps) 19564 { 19565 /* Add global destination address & SAP attribute */ 19566 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19567 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19568 "destination address+SAP\n")); 19569 19570 if (dlmp != NULL) 19571 TCP_STAT(tcps, tcp_mdt_allocfail); 19572 return (-1); 19573 } 19574 19575 /* Add global hwcksum attribute */ 19576 if (hwcksum && 19577 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19578 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19579 "checksum attribute\n")); 19580 19581 TCP_STAT(tcps, tcp_mdt_allocfail); 19582 return (-1); 19583 } 19584 19585 return (0); 19586 } 19587 19588 /* 19589 * Smaller and private version of pdescinfo_t used specifically for TCP, 19590 * which allows for only two payload spans per packet. 19591 */ 19592 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19593 19594 /* 19595 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19596 * scheme, and returns one the following: 19597 * 19598 * -1 = failed allocation. 19599 * 0 = success; burst count reached, or usable send window is too small, 19600 * and that we'd rather wait until later before sending again. 19601 */ 19602 static int 19603 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19604 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19605 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19606 const int mdt_thres) 19607 { 19608 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19609 multidata_t *mmd; 19610 uint_t obsegs, obbytes, hdr_frag_sz; 19611 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19612 int num_burst_seg, max_pld; 19613 pdesc_t *pkt; 19614 tcp_pdescinfo_t tcp_pkt_info; 19615 pdescinfo_t *pkt_info; 19616 int pbuf_idx, pbuf_idx_nxt; 19617 int seg_len, len, spill, af; 19618 boolean_t add_buffer, zcopy, clusterwide; 19619 boolean_t buf_trunked = B_FALSE; 19620 boolean_t rconfirm = B_FALSE; 19621 boolean_t done = B_FALSE; 19622 uint32_t cksum; 19623 uint32_t hwcksum_flags; 19624 ire_t *ire = NULL; 19625 ill_t *ill; 19626 ipha_t *ipha; 19627 ip6_t *ip6h; 19628 ipaddr_t src, dst; 19629 ill_zerocopy_capab_t *zc_cap = NULL; 19630 uint16_t *up; 19631 int err; 19632 conn_t *connp; 19633 mblk_t *mp, *mp1, *fw_mp_head = NULL; 19634 uchar_t *pld_start; 19635 tcp_stack_t *tcps = tcp->tcp_tcps; 19636 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19637 19638 #ifdef _BIG_ENDIAN 19639 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19640 #else 19641 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19642 #endif 19643 19644 #define PREP_NEW_MULTIDATA() { \ 19645 mmd = NULL; \ 19646 md_mp = md_hbuf = NULL; \ 19647 cur_hdr_off = 0; \ 19648 max_pld = tcp->tcp_mdt_max_pld; \ 19649 pbuf_idx = pbuf_idx_nxt = -1; \ 19650 add_buffer = B_TRUE; \ 19651 zcopy = B_FALSE; \ 19652 } 19653 19654 #define PREP_NEW_PBUF() { \ 19655 md_pbuf = md_pbuf_nxt = NULL; \ 19656 pbuf_idx = pbuf_idx_nxt = -1; \ 19657 cur_pld_off = 0; \ 19658 first_snxt = *snxt; \ 19659 ASSERT(*tail_unsent > 0); \ 19660 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19661 } 19662 19663 ASSERT(mdt_thres >= mss); 19664 ASSERT(*usable > 0 && *usable > mdt_thres); 19665 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19666 ASSERT(!TCP_IS_DETACHED(tcp)); 19667 ASSERT(tcp->tcp_valid_bits == 0 || 19668 tcp->tcp_valid_bits == TCP_FSS_VALID); 19669 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19670 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19671 (tcp->tcp_ipversion == IPV6_VERSION && 19672 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19673 19674 connp = tcp->tcp_connp; 19675 ASSERT(connp != NULL); 19676 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19677 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19678 19679 /* 19680 * Note that tcp will only declare at most 2 payload spans per 19681 * packet, which is much lower than the maximum allowable number 19682 * of packet spans per Multidata. For this reason, we use the 19683 * privately declared and smaller descriptor info structure, in 19684 * order to save some stack space. 19685 */ 19686 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19687 19688 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19689 if (af == AF_INET) { 19690 dst = tcp->tcp_ipha->ipha_dst; 19691 src = tcp->tcp_ipha->ipha_src; 19692 ASSERT(!CLASSD(dst)); 19693 } 19694 ASSERT(af == AF_INET || 19695 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19696 19697 obsegs = obbytes = 0; 19698 num_burst_seg = tcp->tcp_snd_burst; 19699 md_mp_head = NULL; 19700 PREP_NEW_MULTIDATA(); 19701 19702 /* 19703 * Before we go on further, make sure there is an IRE that we can 19704 * use, and that the ILL supports MDT. Otherwise, there's no point 19705 * in proceeding any further, and we should just hand everything 19706 * off to the legacy path. 19707 */ 19708 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19709 goto legacy_send_no_md; 19710 19711 ASSERT(ire != NULL); 19712 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19713 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19714 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19715 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19716 /* 19717 * If we do support loopback for MDT (which requires modifications 19718 * to the receiving paths), the following assertions should go away, 19719 * and we would be sending the Multidata to loopback conn later on. 19720 */ 19721 ASSERT(!IRE_IS_LOCAL(ire)); 19722 ASSERT(ire->ire_stq != NULL); 19723 19724 ill = ire_to_ill(ire); 19725 ASSERT(ill != NULL); 19726 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19727 19728 if (!tcp->tcp_ire_ill_check_done) { 19729 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19730 tcp->tcp_ire_ill_check_done = B_TRUE; 19731 } 19732 19733 /* 19734 * If the underlying interface conditions have changed, or if the 19735 * new interface does not support MDT, go back to legacy path. 19736 */ 19737 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19738 /* don't go through this path anymore for this connection */ 19739 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19740 tcp->tcp_mdt = B_FALSE; 19741 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19742 "interface %s\n", (void *)connp, ill->ill_name)); 19743 /* IRE will be released prior to returning */ 19744 goto legacy_send_no_md; 19745 } 19746 19747 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19748 zc_cap = ill->ill_zerocopy_capab; 19749 19750 /* 19751 * Check if we can take tcp fast-path. Note that "incomplete" 19752 * ire's (where the link-layer for next hop is not resolved 19753 * or where the fast-path header in nce_fp_mp is not available 19754 * yet) are sent down the legacy (slow) path. 19755 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19756 */ 19757 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19758 /* IRE will be released prior to returning */ 19759 goto legacy_send_no_md; 19760 } 19761 19762 /* go to legacy path if interface doesn't support zerocopy */ 19763 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19764 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19765 /* IRE will be released prior to returning */ 19766 goto legacy_send_no_md; 19767 } 19768 19769 /* does the interface support hardware checksum offload? */ 19770 hwcksum_flags = 0; 19771 if (ILL_HCKSUM_CAPABLE(ill) && 19772 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19773 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19774 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19775 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19776 HCKSUM_IPHDRCKSUM) 19777 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19778 19779 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19780 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19781 hwcksum_flags |= HCK_FULLCKSUM; 19782 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19783 HCKSUM_INET_PARTIAL) 19784 hwcksum_flags |= HCK_PARTIALCKSUM; 19785 } 19786 19787 /* 19788 * Each header fragment consists of the leading extra space, 19789 * followed by the TCP/IP header, and the trailing extra space. 19790 * We make sure that each header fragment begins on a 32-bit 19791 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19792 * aligned in tcp_mdt_update). 19793 */ 19794 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19795 tcp->tcp_mdt_hdr_tail), 4); 19796 19797 /* are we starting from the beginning of data block? */ 19798 if (*tail_unsent == 0) { 19799 *xmit_tail = (*xmit_tail)->b_cont; 19800 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19801 *tail_unsent = (int)MBLKL(*xmit_tail); 19802 } 19803 19804 /* 19805 * Here we create one or more Multidata messages, each made up of 19806 * one header buffer and up to N payload buffers. This entire 19807 * operation is done within two loops: 19808 * 19809 * The outer loop mostly deals with creating the Multidata message, 19810 * as well as the header buffer that gets added to it. It also 19811 * links the Multidata messages together such that all of them can 19812 * be sent down to the lower layer in a single putnext call; this 19813 * linking behavior depends on the tcp_mdt_chain tunable. 19814 * 19815 * The inner loop takes an existing Multidata message, and adds 19816 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19817 * packetizes those buffers by filling up the corresponding header 19818 * buffer fragments with the proper IP and TCP headers, and by 19819 * describing the layout of each packet in the packet descriptors 19820 * that get added to the Multidata. 19821 */ 19822 do { 19823 /* 19824 * If usable send window is too small, or data blocks in 19825 * transmit list are smaller than our threshold (i.e. app 19826 * performs large writes followed by small ones), we hand 19827 * off the control over to the legacy path. Note that we'll 19828 * get back the control once it encounters a large block. 19829 */ 19830 if (*usable < mss || (*tail_unsent <= mdt_thres && 19831 (*xmit_tail)->b_cont != NULL && 19832 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19833 /* send down what we've got so far */ 19834 if (md_mp_head != NULL) { 19835 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19836 obsegs, obbytes, &rconfirm); 19837 } 19838 /* 19839 * Pass control over to tcp_send(), but tell it to 19840 * return to us once a large-size transmission is 19841 * possible. 19842 */ 19843 TCP_STAT(tcps, tcp_mdt_legacy_small); 19844 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19845 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19846 tail_unsent, xmit_tail, local_time, 19847 mdt_thres)) <= 0) { 19848 /* burst count reached, or alloc failed */ 19849 IRE_REFRELE(ire); 19850 return (err); 19851 } 19852 19853 /* tcp_send() may have sent everything, so check */ 19854 if (*usable <= 0) { 19855 IRE_REFRELE(ire); 19856 return (0); 19857 } 19858 19859 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19860 /* 19861 * We may have delivered the Multidata, so make sure 19862 * to re-initialize before the next round. 19863 */ 19864 md_mp_head = NULL; 19865 obsegs = obbytes = 0; 19866 num_burst_seg = tcp->tcp_snd_burst; 19867 PREP_NEW_MULTIDATA(); 19868 19869 /* are we starting from the beginning of data block? */ 19870 if (*tail_unsent == 0) { 19871 *xmit_tail = (*xmit_tail)->b_cont; 19872 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19873 (uintptr_t)INT_MAX); 19874 *tail_unsent = (int)MBLKL(*xmit_tail); 19875 } 19876 } 19877 19878 /* 19879 * max_pld limits the number of mblks in tcp's transmit 19880 * queue that can be added to a Multidata message. Once 19881 * this counter reaches zero, no more additional mblks 19882 * can be added to it. What happens afterwards depends 19883 * on whether or not we are set to chain the Multidata 19884 * messages. If we are to link them together, reset 19885 * max_pld to its original value (tcp_mdt_max_pld) and 19886 * prepare to create a new Multidata message which will 19887 * get linked to md_mp_head. Else, leave it alone and 19888 * let the inner loop break on its own. 19889 */ 19890 if (tcp_mdt_chain && max_pld == 0) 19891 PREP_NEW_MULTIDATA(); 19892 19893 /* adding a payload buffer; re-initialize values */ 19894 if (add_buffer) 19895 PREP_NEW_PBUF(); 19896 19897 /* 19898 * If we don't have a Multidata, either because we just 19899 * (re)entered this outer loop, or after we branched off 19900 * to tcp_send above, setup the Multidata and header 19901 * buffer to be used. 19902 */ 19903 if (md_mp == NULL) { 19904 int md_hbuflen; 19905 uint32_t start, stuff; 19906 19907 /* 19908 * Calculate Multidata header buffer size large enough 19909 * to hold all of the headers that can possibly be 19910 * sent at this moment. We'd rather over-estimate 19911 * the size than running out of space; this is okay 19912 * since this buffer is small anyway. 19913 */ 19914 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19915 19916 /* 19917 * Start and stuff offset for partial hardware 19918 * checksum offload; these are currently for IPv4. 19919 * For full checksum offload, they are set to zero. 19920 */ 19921 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19922 if (af == AF_INET) { 19923 start = IP_SIMPLE_HDR_LENGTH; 19924 stuff = IP_SIMPLE_HDR_LENGTH + 19925 TCP_CHECKSUM_OFFSET; 19926 } else { 19927 start = IPV6_HDR_LEN; 19928 stuff = IPV6_HDR_LEN + 19929 TCP_CHECKSUM_OFFSET; 19930 } 19931 } else { 19932 start = stuff = 0; 19933 } 19934 19935 /* 19936 * Create the header buffer, Multidata, as well as 19937 * any necessary attributes (destination address, 19938 * SAP and hardware checksum offload) that should 19939 * be associated with the Multidata message. 19940 */ 19941 ASSERT(cur_hdr_off == 0); 19942 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19943 ((md_hbuf->b_wptr += md_hbuflen), 19944 (mmd = mmd_alloc(md_hbuf, &md_mp, 19945 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19946 /* fastpath mblk */ 19947 ire->ire_nce->nce_res_mp, 19948 /* hardware checksum enabled */ 19949 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19950 /* hardware checksum offsets */ 19951 start, stuff, 0, 19952 /* hardware checksum flag */ 19953 hwcksum_flags, tcps) != 0)) { 19954 legacy_send: 19955 if (md_mp != NULL) { 19956 /* Unlink message from the chain */ 19957 if (md_mp_head != NULL) { 19958 err = (intptr_t)rmvb(md_mp_head, 19959 md_mp); 19960 /* 19961 * We can't assert that rmvb 19962 * did not return -1, since we 19963 * may get here before linkb 19964 * happens. We do, however, 19965 * check if we just removed the 19966 * only element in the list. 19967 */ 19968 if (err == 0) 19969 md_mp_head = NULL; 19970 } 19971 /* md_hbuf gets freed automatically */ 19972 TCP_STAT(tcps, tcp_mdt_discarded); 19973 freeb(md_mp); 19974 } else { 19975 /* Either allocb or mmd_alloc failed */ 19976 TCP_STAT(tcps, tcp_mdt_allocfail); 19977 if (md_hbuf != NULL) 19978 freeb(md_hbuf); 19979 } 19980 19981 /* send down what we've got so far */ 19982 if (md_mp_head != NULL) { 19983 tcp_multisend_data(tcp, ire, ill, 19984 md_mp_head, obsegs, obbytes, 19985 &rconfirm); 19986 } 19987 legacy_send_no_md: 19988 if (ire != NULL) 19989 IRE_REFRELE(ire); 19990 /* 19991 * Too bad; let the legacy path handle this. 19992 * We specify INT_MAX for the threshold, since 19993 * we gave up with the Multidata processings 19994 * and let the old path have it all. 19995 */ 19996 TCP_STAT(tcps, tcp_mdt_legacy_all); 19997 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19998 tcp_tcp_hdr_len, num_sack_blk, usable, 19999 snxt, tail_unsent, xmit_tail, local_time, 20000 INT_MAX)); 20001 } 20002 20003 /* link to any existing ones, if applicable */ 20004 TCP_STAT(tcps, tcp_mdt_allocd); 20005 if (md_mp_head == NULL) { 20006 md_mp_head = md_mp; 20007 } else if (tcp_mdt_chain) { 20008 TCP_STAT(tcps, tcp_mdt_linked); 20009 linkb(md_mp_head, md_mp); 20010 } 20011 } 20012 20013 ASSERT(md_mp_head != NULL); 20014 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20015 ASSERT(md_mp != NULL && mmd != NULL); 20016 ASSERT(md_hbuf != NULL); 20017 20018 /* 20019 * Packetize the transmittable portion of the data block; 20020 * each data block is essentially added to the Multidata 20021 * as a payload buffer. We also deal with adding more 20022 * than one payload buffers, which happens when the remaining 20023 * packetized portion of the current payload buffer is less 20024 * than MSS, while the next data block in transmit queue 20025 * has enough data to make up for one. This "spillover" 20026 * case essentially creates a split-packet, where portions 20027 * of the packet's payload fragments may span across two 20028 * virtually discontiguous address blocks. 20029 */ 20030 seg_len = mss; 20031 do { 20032 len = seg_len; 20033 20034 ASSERT(len > 0); 20035 ASSERT(max_pld >= 0); 20036 ASSERT(!add_buffer || cur_pld_off == 0); 20037 20038 /* 20039 * First time around for this payload buffer; note 20040 * in the case of a spillover, the following has 20041 * been done prior to adding the split-packet 20042 * descriptor to Multidata, and we don't want to 20043 * repeat the process. 20044 */ 20045 if (add_buffer) { 20046 ASSERT(mmd != NULL); 20047 ASSERT(md_pbuf == NULL); 20048 ASSERT(md_pbuf_nxt == NULL); 20049 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20050 20051 /* 20052 * Have we reached the limit? We'd get to 20053 * this case when we're not chaining the 20054 * Multidata messages together, and since 20055 * we're done, terminate this loop. 20056 */ 20057 if (max_pld == 0) 20058 break; /* done */ 20059 20060 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20061 TCP_STAT(tcps, tcp_mdt_allocfail); 20062 goto legacy_send; /* out_of_mem */ 20063 } 20064 20065 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20066 zc_cap != NULL) { 20067 if (!ip_md_zcopy_attr(mmd, NULL, 20068 zc_cap->ill_zerocopy_flags)) { 20069 freeb(md_pbuf); 20070 TCP_STAT(tcps, 20071 tcp_mdt_allocfail); 20072 /* out_of_mem */ 20073 goto legacy_send; 20074 } 20075 zcopy = B_TRUE; 20076 } 20077 20078 md_pbuf->b_rptr += base_pld_off; 20079 20080 /* 20081 * Add a payload buffer to the Multidata; this 20082 * operation must not fail, or otherwise our 20083 * logic in this routine is broken. There 20084 * is no memory allocation done by the 20085 * routine, so any returned failure simply 20086 * tells us that we've done something wrong. 20087 * 20088 * A failure tells us that either we're adding 20089 * the same payload buffer more than once, or 20090 * we're trying to add more buffers than 20091 * allowed (max_pld calculation is wrong). 20092 * None of the above cases should happen, and 20093 * we panic because either there's horrible 20094 * heap corruption, and/or programming mistake. 20095 */ 20096 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20097 if (pbuf_idx < 0) { 20098 cmn_err(CE_PANIC, "tcp_multisend: " 20099 "payload buffer logic error " 20100 "detected for tcp %p mmd %p " 20101 "pbuf %p (%d)\n", 20102 (void *)tcp, (void *)mmd, 20103 (void *)md_pbuf, pbuf_idx); 20104 } 20105 20106 ASSERT(max_pld > 0); 20107 --max_pld; 20108 add_buffer = B_FALSE; 20109 } 20110 20111 ASSERT(md_mp_head != NULL); 20112 ASSERT(md_pbuf != NULL); 20113 ASSERT(md_pbuf_nxt == NULL); 20114 ASSERT(pbuf_idx != -1); 20115 ASSERT(pbuf_idx_nxt == -1); 20116 ASSERT(*usable > 0); 20117 20118 /* 20119 * We spillover to the next payload buffer only 20120 * if all of the following is true: 20121 * 20122 * 1. There is not enough data on the current 20123 * payload buffer to make up `len', 20124 * 2. We are allowed to send `len', 20125 * 3. The next payload buffer length is large 20126 * enough to accomodate `spill'. 20127 */ 20128 if ((spill = len - *tail_unsent) > 0 && 20129 *usable >= len && 20130 MBLKL((*xmit_tail)->b_cont) >= spill && 20131 max_pld > 0) { 20132 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20133 if (md_pbuf_nxt == NULL) { 20134 TCP_STAT(tcps, tcp_mdt_allocfail); 20135 goto legacy_send; /* out_of_mem */ 20136 } 20137 20138 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20139 zc_cap != NULL) { 20140 if (!ip_md_zcopy_attr(mmd, NULL, 20141 zc_cap->ill_zerocopy_flags)) { 20142 freeb(md_pbuf_nxt); 20143 TCP_STAT(tcps, 20144 tcp_mdt_allocfail); 20145 /* out_of_mem */ 20146 goto legacy_send; 20147 } 20148 zcopy = B_TRUE; 20149 } 20150 20151 /* 20152 * See comments above on the first call to 20153 * mmd_addpldbuf for explanation on the panic. 20154 */ 20155 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20156 if (pbuf_idx_nxt < 0) { 20157 panic("tcp_multisend: " 20158 "next payload buffer logic error " 20159 "detected for tcp %p mmd %p " 20160 "pbuf %p (%d)\n", 20161 (void *)tcp, (void *)mmd, 20162 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20163 } 20164 20165 ASSERT(max_pld > 0); 20166 --max_pld; 20167 } else if (spill > 0) { 20168 /* 20169 * If there's a spillover, but the following 20170 * xmit_tail couldn't give us enough octets 20171 * to reach "len", then stop the current 20172 * Multidata creation and let the legacy 20173 * tcp_send() path take over. We don't want 20174 * to send the tiny segment as part of this 20175 * Multidata for performance reasons; instead, 20176 * we let the legacy path deal with grouping 20177 * it with the subsequent small mblks. 20178 */ 20179 if (*usable >= len && 20180 MBLKL((*xmit_tail)->b_cont) < spill) { 20181 max_pld = 0; 20182 break; /* done */ 20183 } 20184 20185 /* 20186 * We can't spillover, and we are near 20187 * the end of the current payload buffer, 20188 * so send what's left. 20189 */ 20190 ASSERT(*tail_unsent > 0); 20191 len = *tail_unsent; 20192 } 20193 20194 /* tail_unsent is negated if there is a spillover */ 20195 *tail_unsent -= len; 20196 *usable -= len; 20197 ASSERT(*usable >= 0); 20198 20199 if (*usable < mss) 20200 seg_len = *usable; 20201 /* 20202 * Sender SWS avoidance; see comments in tcp_send(); 20203 * everything else is the same, except that we only 20204 * do this here if there is no more data to be sent 20205 * following the current xmit_tail. We don't check 20206 * for 1-byte urgent data because we shouldn't get 20207 * here if TCP_URG_VALID is set. 20208 */ 20209 if (*usable > 0 && *usable < mss && 20210 ((md_pbuf_nxt == NULL && 20211 (*xmit_tail)->b_cont == NULL) || 20212 (md_pbuf_nxt != NULL && 20213 (*xmit_tail)->b_cont->b_cont == NULL)) && 20214 seg_len < (tcp->tcp_max_swnd >> 1) && 20215 (tcp->tcp_unsent - 20216 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20217 !tcp->tcp_zero_win_probe) { 20218 if ((*snxt + len) == tcp->tcp_snxt && 20219 (*snxt + len) == tcp->tcp_suna) { 20220 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20221 } 20222 done = B_TRUE; 20223 } 20224 20225 /* 20226 * Prime pump for IP's checksumming on our behalf; 20227 * include the adjustment for a source route if any. 20228 * Do this only for software/partial hardware checksum 20229 * offload, as this field gets zeroed out later for 20230 * the full hardware checksum offload case. 20231 */ 20232 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20233 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20234 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20235 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20236 } 20237 20238 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20239 *snxt += len; 20240 20241 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20242 /* 20243 * We set the PUSH bit only if TCP has no more buffered 20244 * data to be transmitted (or if sender SWS avoidance 20245 * takes place), as opposed to setting it for every 20246 * last packet in the burst. 20247 */ 20248 if (done || 20249 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20250 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20251 20252 /* 20253 * Set FIN bit if this is our last segment; snxt 20254 * already includes its length, and it will not 20255 * be adjusted after this point. 20256 */ 20257 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20258 *snxt == tcp->tcp_fss) { 20259 if (!tcp->tcp_fin_acked) { 20260 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20261 BUMP_MIB(&tcps->tcps_mib, 20262 tcpOutControl); 20263 } 20264 if (!tcp->tcp_fin_sent) { 20265 tcp->tcp_fin_sent = B_TRUE; 20266 /* 20267 * tcp state must be ESTABLISHED 20268 * in order for us to get here in 20269 * the first place. 20270 */ 20271 tcp->tcp_state = TCPS_FIN_WAIT_1; 20272 20273 /* 20274 * Upon returning from this routine, 20275 * tcp_wput_data() will set tcp_snxt 20276 * to be equal to snxt + tcp_fin_sent. 20277 * This is essentially the same as 20278 * setting it to tcp_fss + 1. 20279 */ 20280 } 20281 } 20282 20283 tcp->tcp_last_sent_len = (ushort_t)len; 20284 20285 len += tcp_hdr_len; 20286 if (tcp->tcp_ipversion == IPV4_VERSION) 20287 tcp->tcp_ipha->ipha_length = htons(len); 20288 else 20289 tcp->tcp_ip6h->ip6_plen = htons(len - 20290 ((char *)&tcp->tcp_ip6h[1] - 20291 tcp->tcp_iphc)); 20292 20293 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20294 20295 /* setup header fragment */ 20296 PDESC_HDR_ADD(pkt_info, 20297 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20298 tcp->tcp_mdt_hdr_head, /* head room */ 20299 tcp_hdr_len, /* len */ 20300 tcp->tcp_mdt_hdr_tail); /* tail room */ 20301 20302 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20303 hdr_frag_sz); 20304 ASSERT(MBLKIN(md_hbuf, 20305 (pkt_info->hdr_base - md_hbuf->b_rptr), 20306 PDESC_HDRSIZE(pkt_info))); 20307 20308 /* setup first payload fragment */ 20309 PDESC_PLD_INIT(pkt_info); 20310 PDESC_PLD_SPAN_ADD(pkt_info, 20311 pbuf_idx, /* index */ 20312 md_pbuf->b_rptr + cur_pld_off, /* start */ 20313 tcp->tcp_last_sent_len); /* len */ 20314 20315 /* create a split-packet in case of a spillover */ 20316 if (md_pbuf_nxt != NULL) { 20317 ASSERT(spill > 0); 20318 ASSERT(pbuf_idx_nxt > pbuf_idx); 20319 ASSERT(!add_buffer); 20320 20321 md_pbuf = md_pbuf_nxt; 20322 md_pbuf_nxt = NULL; 20323 pbuf_idx = pbuf_idx_nxt; 20324 pbuf_idx_nxt = -1; 20325 cur_pld_off = spill; 20326 20327 /* trim out first payload fragment */ 20328 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20329 20330 /* setup second payload fragment */ 20331 PDESC_PLD_SPAN_ADD(pkt_info, 20332 pbuf_idx, /* index */ 20333 md_pbuf->b_rptr, /* start */ 20334 spill); /* len */ 20335 20336 if ((*xmit_tail)->b_next == NULL) { 20337 /* 20338 * Store the lbolt used for RTT 20339 * estimation. We can only record one 20340 * timestamp per mblk so we do it when 20341 * we reach the end of the payload 20342 * buffer. Also we only take a new 20343 * timestamp sample when the previous 20344 * timed data from the same mblk has 20345 * been ack'ed. 20346 */ 20347 (*xmit_tail)->b_prev = local_time; 20348 (*xmit_tail)->b_next = 20349 (mblk_t *)(uintptr_t)first_snxt; 20350 } 20351 20352 first_snxt = *snxt - spill; 20353 20354 /* 20355 * Advance xmit_tail; usable could be 0 by 20356 * the time we got here, but we made sure 20357 * above that we would only spillover to 20358 * the next data block if usable includes 20359 * the spilled-over amount prior to the 20360 * subtraction. Therefore, we are sure 20361 * that xmit_tail->b_cont can't be NULL. 20362 */ 20363 ASSERT((*xmit_tail)->b_cont != NULL); 20364 *xmit_tail = (*xmit_tail)->b_cont; 20365 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20366 (uintptr_t)INT_MAX); 20367 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20368 } else { 20369 cur_pld_off += tcp->tcp_last_sent_len; 20370 } 20371 20372 /* 20373 * Fill in the header using the template header, and 20374 * add options such as time-stamp, ECN and/or SACK, 20375 * as needed. 20376 */ 20377 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20378 (clock_t)local_time, num_sack_blk); 20379 20380 /* take care of some IP header businesses */ 20381 if (af == AF_INET) { 20382 ipha = (ipha_t *)pkt_info->hdr_rptr; 20383 20384 ASSERT(OK_32PTR((uchar_t *)ipha)); 20385 ASSERT(PDESC_HDRL(pkt_info) >= 20386 IP_SIMPLE_HDR_LENGTH); 20387 ASSERT(ipha->ipha_version_and_hdr_length == 20388 IP_SIMPLE_HDR_VERSION); 20389 20390 /* 20391 * Assign ident value for current packet; see 20392 * related comments in ip_wput_ire() about the 20393 * contract private interface with clustering 20394 * group. 20395 */ 20396 clusterwide = B_FALSE; 20397 if (cl_inet_ipident != NULL) { 20398 ASSERT(cl_inet_isclusterwide != NULL); 20399 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20400 AF_INET, 20401 (uint8_t *)(uintptr_t)src)) { 20402 ipha->ipha_ident = 20403 (*cl_inet_ipident) 20404 (IPPROTO_IP, AF_INET, 20405 (uint8_t *)(uintptr_t)src, 20406 (uint8_t *)(uintptr_t)dst); 20407 clusterwide = B_TRUE; 20408 } 20409 } 20410 20411 if (!clusterwide) { 20412 ipha->ipha_ident = (uint16_t) 20413 atomic_add_32_nv( 20414 &ire->ire_ident, 1); 20415 } 20416 #ifndef _BIG_ENDIAN 20417 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20418 (ipha->ipha_ident >> 8); 20419 #endif 20420 } else { 20421 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20422 20423 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20424 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20425 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20426 ASSERT(PDESC_HDRL(pkt_info) >= 20427 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20428 TCP_CHECKSUM_SIZE)); 20429 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20430 20431 if (tcp->tcp_ip_forward_progress) { 20432 rconfirm = B_TRUE; 20433 tcp->tcp_ip_forward_progress = B_FALSE; 20434 } 20435 } 20436 20437 /* at least one payload span, and at most two */ 20438 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20439 20440 /* add the packet descriptor to Multidata */ 20441 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20442 KM_NOSLEEP)) == NULL) { 20443 /* 20444 * Any failure other than ENOMEM indicates 20445 * that we have passed in invalid pkt_info 20446 * or parameters to mmd_addpdesc, which must 20447 * not happen. 20448 * 20449 * EINVAL is a result of failure on boundary 20450 * checks against the pkt_info contents. It 20451 * should not happen, and we panic because 20452 * either there's horrible heap corruption, 20453 * and/or programming mistake. 20454 */ 20455 if (err != ENOMEM) { 20456 cmn_err(CE_PANIC, "tcp_multisend: " 20457 "pdesc logic error detected for " 20458 "tcp %p mmd %p pinfo %p (%d)\n", 20459 (void *)tcp, (void *)mmd, 20460 (void *)pkt_info, err); 20461 } 20462 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20463 goto legacy_send; /* out_of_mem */ 20464 } 20465 ASSERT(pkt != NULL); 20466 20467 /* calculate IP header and TCP checksums */ 20468 if (af == AF_INET) { 20469 /* calculate pseudo-header checksum */ 20470 cksum = (dst >> 16) + (dst & 0xFFFF) + 20471 (src >> 16) + (src & 0xFFFF); 20472 20473 /* offset for TCP header checksum */ 20474 up = IPH_TCPH_CHECKSUMP(ipha, 20475 IP_SIMPLE_HDR_LENGTH); 20476 } else { 20477 up = (uint16_t *)&ip6h->ip6_src; 20478 20479 /* calculate pseudo-header checksum */ 20480 cksum = up[0] + up[1] + up[2] + up[3] + 20481 up[4] + up[5] + up[6] + up[7] + 20482 up[8] + up[9] + up[10] + up[11] + 20483 up[12] + up[13] + up[14] + up[15]; 20484 20485 /* Fold the initial sum */ 20486 cksum = (cksum & 0xffff) + (cksum >> 16); 20487 20488 up = (uint16_t *)(((uchar_t *)ip6h) + 20489 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20490 } 20491 20492 if (hwcksum_flags & HCK_FULLCKSUM) { 20493 /* clear checksum field for hardware */ 20494 *up = 0; 20495 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20496 uint32_t sum; 20497 20498 /* pseudo-header checksumming */ 20499 sum = *up + cksum + IP_TCP_CSUM_COMP; 20500 sum = (sum & 0xFFFF) + (sum >> 16); 20501 *up = (sum & 0xFFFF) + (sum >> 16); 20502 } else { 20503 /* software checksumming */ 20504 TCP_STAT(tcps, tcp_out_sw_cksum); 20505 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20506 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20507 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20508 cksum + IP_TCP_CSUM_COMP); 20509 if (*up == 0) 20510 *up = 0xFFFF; 20511 } 20512 20513 /* IPv4 header checksum */ 20514 if (af == AF_INET) { 20515 ipha->ipha_fragment_offset_and_flags |= 20516 (uint32_t)htons(ire->ire_frag_flag); 20517 20518 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20519 ipha->ipha_hdr_checksum = 0; 20520 } else { 20521 IP_HDR_CKSUM(ipha, cksum, 20522 ((uint32_t *)ipha)[0], 20523 ((uint16_t *)ipha)[4]); 20524 } 20525 } 20526 20527 if (af == AF_INET && 20528 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20529 af == AF_INET6 && 20530 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20531 /* build header(IP/TCP) mblk for this segment */ 20532 if ((mp = dupb(md_hbuf)) == NULL) 20533 goto legacy_send; 20534 20535 mp->b_rptr = pkt_info->hdr_rptr; 20536 mp->b_wptr = pkt_info->hdr_wptr; 20537 20538 /* build payload mblk for this segment */ 20539 if ((mp1 = dupb(*xmit_tail)) == NULL) { 20540 freemsg(mp); 20541 goto legacy_send; 20542 } 20543 mp1->b_wptr = md_pbuf->b_rptr + cur_pld_off; 20544 mp1->b_rptr = mp1->b_wptr - 20545 tcp->tcp_last_sent_len; 20546 linkb(mp, mp1); 20547 20548 pld_start = mp1->b_rptr; 20549 20550 if (af == AF_INET) { 20551 DTRACE_PROBE4( 20552 ip4__physical__out__start, 20553 ill_t *, NULL, 20554 ill_t *, ill, 20555 ipha_t *, ipha, 20556 mblk_t *, mp); 20557 FW_HOOKS( 20558 ipst->ips_ip4_physical_out_event, 20559 ipst->ips_ipv4firewall_physical_out, 20560 NULL, ill, ipha, mp, mp, ipst); 20561 DTRACE_PROBE1( 20562 ip4__physical__out__end, 20563 mblk_t *, mp); 20564 } else { 20565 DTRACE_PROBE4( 20566 ip6__physical__out_start, 20567 ill_t *, NULL, 20568 ill_t *, ill, 20569 ip6_t *, ip6h, 20570 mblk_t *, mp); 20571 FW_HOOKS6( 20572 ipst->ips_ip6_physical_out_event, 20573 ipst->ips_ipv6firewall_physical_out, 20574 NULL, ill, ip6h, mp, mp, ipst); 20575 DTRACE_PROBE1( 20576 ip6__physical__out__end, 20577 mblk_t *, mp); 20578 } 20579 20580 if (buf_trunked && mp != NULL) { 20581 /* 20582 * Need to pass it to normal path. 20583 */ 20584 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20585 } else if (mp == NULL || 20586 mp->b_rptr != pkt_info->hdr_rptr || 20587 mp->b_wptr != pkt_info->hdr_wptr || 20588 (mp1 = mp->b_cont) == NULL || 20589 mp1->b_rptr != pld_start || 20590 mp1->b_wptr != pld_start + 20591 tcp->tcp_last_sent_len || 20592 mp1->b_cont != NULL) { 20593 /* 20594 * Need to pass all packets of this 20595 * buffer to normal path, either when 20596 * packet is blocked, or when boundary 20597 * of header buffer or payload buffer 20598 * has been changed by FW_HOOKS[6]. 20599 */ 20600 buf_trunked = B_TRUE; 20601 if (md_mp_head != NULL) { 20602 err = (intptr_t)rmvb(md_mp_head, 20603 md_mp); 20604 if (err == 0) 20605 md_mp_head = NULL; 20606 } 20607 20608 /* send down what we've got so far */ 20609 if (md_mp_head != NULL) { 20610 tcp_multisend_data(tcp, ire, 20611 ill, md_mp_head, obsegs, 20612 obbytes, &rconfirm); 20613 } 20614 md_mp_head = NULL; 20615 20616 if (mp != NULL) 20617 CALL_IP_WPUT(tcp->tcp_connp, 20618 q, mp); 20619 20620 mp1 = fw_mp_head; 20621 do { 20622 mp = mp1; 20623 mp1 = mp1->b_next; 20624 mp->b_next = NULL; 20625 mp->b_prev = NULL; 20626 CALL_IP_WPUT(tcp->tcp_connp, 20627 q, mp); 20628 } while (mp1 != NULL); 20629 20630 fw_mp_head = NULL; 20631 } else { 20632 if (fw_mp_head == NULL) 20633 fw_mp_head = mp; 20634 else 20635 fw_mp_head->b_prev->b_next = mp; 20636 fw_mp_head->b_prev = mp; 20637 } 20638 } 20639 20640 /* advance header offset */ 20641 cur_hdr_off += hdr_frag_sz; 20642 20643 obbytes += tcp->tcp_last_sent_len; 20644 ++obsegs; 20645 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20646 *tail_unsent > 0); 20647 20648 if ((*xmit_tail)->b_next == NULL) { 20649 /* 20650 * Store the lbolt used for RTT estimation. We can only 20651 * record one timestamp per mblk so we do it when we 20652 * reach the end of the payload buffer. Also we only 20653 * take a new timestamp sample when the previous timed 20654 * data from the same mblk has been ack'ed. 20655 */ 20656 (*xmit_tail)->b_prev = local_time; 20657 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20658 } 20659 20660 ASSERT(*tail_unsent >= 0); 20661 if (*tail_unsent > 0) { 20662 /* 20663 * We got here because we broke out of the above 20664 * loop due to of one of the following cases: 20665 * 20666 * 1. len < adjusted MSS (i.e. small), 20667 * 2. Sender SWS avoidance, 20668 * 3. max_pld is zero. 20669 * 20670 * We are done for this Multidata, so trim our 20671 * last payload buffer (if any) accordingly. 20672 */ 20673 if (md_pbuf != NULL) 20674 md_pbuf->b_wptr -= *tail_unsent; 20675 } else if (*usable > 0) { 20676 *xmit_tail = (*xmit_tail)->b_cont; 20677 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20678 (uintptr_t)INT_MAX); 20679 *tail_unsent = (int)MBLKL(*xmit_tail); 20680 add_buffer = B_TRUE; 20681 } 20682 20683 while (fw_mp_head) { 20684 mp = fw_mp_head; 20685 fw_mp_head = fw_mp_head->b_next; 20686 mp->b_prev = mp->b_next = NULL; 20687 freemsg(mp); 20688 } 20689 if (buf_trunked) { 20690 TCP_STAT(tcps, tcp_mdt_discarded); 20691 freeb(md_mp); 20692 buf_trunked = B_FALSE; 20693 } 20694 } while (!done && *usable > 0 && num_burst_seg > 0 && 20695 (tcp_mdt_chain || max_pld > 0)); 20696 20697 if (md_mp_head != NULL) { 20698 /* send everything down */ 20699 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20700 &rconfirm); 20701 } 20702 20703 #undef PREP_NEW_MULTIDATA 20704 #undef PREP_NEW_PBUF 20705 #undef IPVER 20706 20707 IRE_REFRELE(ire); 20708 return (0); 20709 } 20710 20711 /* 20712 * A wrapper function for sending one or more Multidata messages down to 20713 * the module below ip; this routine does not release the reference of the 20714 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20715 */ 20716 static void 20717 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20718 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20719 { 20720 uint64_t delta; 20721 nce_t *nce; 20722 tcp_stack_t *tcps = tcp->tcp_tcps; 20723 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20724 20725 ASSERT(ire != NULL && ill != NULL); 20726 ASSERT(ire->ire_stq != NULL); 20727 ASSERT(md_mp_head != NULL); 20728 ASSERT(rconfirm != NULL); 20729 20730 /* adjust MIBs and IRE timestamp */ 20731 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 20732 tcp->tcp_obsegs += obsegs; 20733 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20734 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20735 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20736 20737 if (tcp->tcp_ipversion == IPV4_VERSION) { 20738 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20739 } else { 20740 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20741 } 20742 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20743 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20744 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20745 20746 ire->ire_ob_pkt_count += obsegs; 20747 if (ire->ire_ipif != NULL) 20748 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20749 ire->ire_last_used_time = lbolt; 20750 20751 /* send it down */ 20752 putnext(ire->ire_stq, md_mp_head); 20753 20754 /* we're done for TCP/IPv4 */ 20755 if (tcp->tcp_ipversion == IPV4_VERSION) 20756 return; 20757 20758 nce = ire->ire_nce; 20759 20760 ASSERT(nce != NULL); 20761 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20762 ASSERT(nce->nce_state != ND_INCOMPLETE); 20763 20764 /* reachability confirmation? */ 20765 if (*rconfirm) { 20766 nce->nce_last = TICK_TO_MSEC(lbolt64); 20767 if (nce->nce_state != ND_REACHABLE) { 20768 mutex_enter(&nce->nce_lock); 20769 nce->nce_state = ND_REACHABLE; 20770 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20771 mutex_exit(&nce->nce_lock); 20772 (void) untimeout(nce->nce_timeout_id); 20773 if (ip_debug > 2) { 20774 /* ip1dbg */ 20775 pr_addr_dbg("tcp_multisend_data: state " 20776 "for %s changed to REACHABLE\n", 20777 AF_INET6, &ire->ire_addr_v6); 20778 } 20779 } 20780 /* reset transport reachability confirmation */ 20781 *rconfirm = B_FALSE; 20782 } 20783 20784 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20785 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20786 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20787 20788 if (delta > (uint64_t)ill->ill_reachable_time) { 20789 mutex_enter(&nce->nce_lock); 20790 switch (nce->nce_state) { 20791 case ND_REACHABLE: 20792 case ND_STALE: 20793 /* 20794 * ND_REACHABLE is identical to ND_STALE in this 20795 * specific case. If reachable time has expired for 20796 * this neighbor (delta is greater than reachable 20797 * time), conceptually, the neighbor cache is no 20798 * longer in REACHABLE state, but already in STALE 20799 * state. So the correct transition here is to 20800 * ND_DELAY. 20801 */ 20802 nce->nce_state = ND_DELAY; 20803 mutex_exit(&nce->nce_lock); 20804 NDP_RESTART_TIMER(nce, 20805 ipst->ips_delay_first_probe_time); 20806 if (ip_debug > 3) { 20807 /* ip2dbg */ 20808 pr_addr_dbg("tcp_multisend_data: state " 20809 "for %s changed to DELAY\n", 20810 AF_INET6, &ire->ire_addr_v6); 20811 } 20812 break; 20813 case ND_DELAY: 20814 case ND_PROBE: 20815 mutex_exit(&nce->nce_lock); 20816 /* Timers have already started */ 20817 break; 20818 case ND_UNREACHABLE: 20819 /* 20820 * ndp timer has detected that this nce is 20821 * unreachable and initiated deleting this nce 20822 * and all its associated IREs. This is a race 20823 * where we found the ire before it was deleted 20824 * and have just sent out a packet using this 20825 * unreachable nce. 20826 */ 20827 mutex_exit(&nce->nce_lock); 20828 break; 20829 default: 20830 ASSERT(0); 20831 } 20832 } 20833 } 20834 20835 /* 20836 * Derived from tcp_send_data(). 20837 */ 20838 static void 20839 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20840 int num_lso_seg) 20841 { 20842 ipha_t *ipha; 20843 mblk_t *ire_fp_mp; 20844 uint_t ire_fp_mp_len; 20845 uint32_t hcksum_txflags = 0; 20846 ipaddr_t src; 20847 ipaddr_t dst; 20848 uint32_t cksum; 20849 uint16_t *up; 20850 tcp_stack_t *tcps = tcp->tcp_tcps; 20851 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20852 20853 ASSERT(DB_TYPE(mp) == M_DATA); 20854 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20855 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20856 ASSERT(tcp->tcp_connp != NULL); 20857 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20858 20859 ipha = (ipha_t *)mp->b_rptr; 20860 src = ipha->ipha_src; 20861 dst = ipha->ipha_dst; 20862 20863 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20864 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20865 num_lso_seg); 20866 #ifndef _BIG_ENDIAN 20867 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20868 #endif 20869 if (tcp->tcp_snd_zcopy_aware) { 20870 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20871 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20872 mp = tcp_zcopy_disable(tcp, mp); 20873 } 20874 20875 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20876 ASSERT(ill->ill_hcksum_capab != NULL); 20877 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20878 } 20879 20880 /* 20881 * Since the TCP checksum should be recalculated by h/w, we can just 20882 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20883 * pseudo-header checksum for HCK_PARTIALCKSUM. 20884 * The partial pseudo-header excludes TCP length, that was calculated 20885 * in tcp_send(), so to zero *up before further processing. 20886 */ 20887 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20888 20889 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20890 *up = 0; 20891 20892 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20893 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20894 20895 /* 20896 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 20897 */ 20898 DB_LSOFLAGS(mp) |= HW_LSO; 20899 DB_LSOMSS(mp) = mss; 20900 20901 ipha->ipha_fragment_offset_and_flags |= 20902 (uint32_t)htons(ire->ire_frag_flag); 20903 20904 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20905 ire_fp_mp_len = MBLKL(ire_fp_mp); 20906 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20907 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20908 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20909 20910 UPDATE_OB_PKT_COUNT(ire); 20911 ire->ire_last_used_time = lbolt; 20912 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20913 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20914 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20915 ntohs(ipha->ipha_length)); 20916 20917 if (ILL_DLS_CAPABLE(ill)) { 20918 /* 20919 * Send the packet directly to DLD, where it may be queued 20920 * depending on the availability of transmit resources at 20921 * the media layer. 20922 */ 20923 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 20924 } else { 20925 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 20926 DTRACE_PROBE4(ip4__physical__out__start, 20927 ill_t *, NULL, ill_t *, out_ill, 20928 ipha_t *, ipha, mblk_t *, mp); 20929 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20930 ipst->ips_ipv4firewall_physical_out, 20931 NULL, out_ill, ipha, mp, mp, ipst); 20932 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20933 if (mp != NULL) 20934 putnext(ire->ire_stq, mp); 20935 } 20936 } 20937 20938 /* 20939 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20940 * scheme, and returns one of the following: 20941 * 20942 * -1 = failed allocation. 20943 * 0 = success; burst count reached, or usable send window is too small, 20944 * and that we'd rather wait until later before sending again. 20945 * 1 = success; we are called from tcp_multisend(), and both usable send 20946 * window and tail_unsent are greater than the MDT threshold, and thus 20947 * Multidata Transmit should be used instead. 20948 */ 20949 static int 20950 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20951 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20952 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20953 const int mdt_thres) 20954 { 20955 int num_burst_seg = tcp->tcp_snd_burst; 20956 ire_t *ire = NULL; 20957 ill_t *ill = NULL; 20958 mblk_t *ire_fp_mp = NULL; 20959 uint_t ire_fp_mp_len = 0; 20960 int num_lso_seg = 1; 20961 uint_t lso_usable; 20962 boolean_t do_lso_send = B_FALSE; 20963 tcp_stack_t *tcps = tcp->tcp_tcps; 20964 20965 /* 20966 * Check LSO capability before any further work. And the similar check 20967 * need to be done in for(;;) loop. 20968 * LSO will be deployed when therer is more than one mss of available 20969 * data and a burst transmission is allowed. 20970 */ 20971 if (tcp->tcp_lso && 20972 (tcp->tcp_valid_bits == 0 || 20973 tcp->tcp_valid_bits == TCP_FSS_VALID) && 20974 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20975 /* 20976 * Try to find usable IRE/ILL and do basic check to the ILL. 20977 */ 20978 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 20979 /* 20980 * Enable LSO with this transmission. 20981 * Since IRE has been hold in 20982 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 20983 * should be called before return. 20984 */ 20985 do_lso_send = B_TRUE; 20986 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20987 ire_fp_mp_len = MBLKL(ire_fp_mp); 20988 /* Round up to multiple of 4 */ 20989 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 20990 } else { 20991 do_lso_send = B_FALSE; 20992 ill = NULL; 20993 } 20994 } 20995 20996 for (;;) { 20997 struct datab *db; 20998 tcph_t *tcph; 20999 uint32_t sum; 21000 mblk_t *mp, *mp1; 21001 uchar_t *rptr; 21002 int len; 21003 21004 /* 21005 * If we're called by tcp_multisend(), and the amount of 21006 * sendable data as well as the size of current xmit_tail 21007 * is beyond the MDT threshold, return to the caller and 21008 * let the large data transmit be done using MDT. 21009 */ 21010 if (*usable > 0 && *usable > mdt_thres && 21011 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21012 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21013 ASSERT(tcp->tcp_mdt); 21014 return (1); /* success; do large send */ 21015 } 21016 21017 if (num_burst_seg == 0) 21018 break; /* success; burst count reached */ 21019 21020 /* 21021 * Calculate the maximum payload length we can send in *one* 21022 * time. 21023 */ 21024 if (do_lso_send) { 21025 /* 21026 * Check whether need to do LSO any more. 21027 */ 21028 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21029 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21030 lso_usable = MIN(lso_usable, 21031 num_burst_seg * mss); 21032 21033 num_lso_seg = lso_usable / mss; 21034 if (lso_usable % mss) { 21035 num_lso_seg++; 21036 tcp->tcp_last_sent_len = (ushort_t) 21037 (lso_usable % mss); 21038 } else { 21039 tcp->tcp_last_sent_len = (ushort_t)mss; 21040 } 21041 } else { 21042 do_lso_send = B_FALSE; 21043 num_lso_seg = 1; 21044 lso_usable = mss; 21045 } 21046 } 21047 21048 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21049 21050 /* 21051 * Adjust num_burst_seg here. 21052 */ 21053 num_burst_seg -= num_lso_seg; 21054 21055 len = mss; 21056 if (len > *usable) { 21057 ASSERT(do_lso_send == B_FALSE); 21058 21059 len = *usable; 21060 if (len <= 0) { 21061 /* Terminate the loop */ 21062 break; /* success; too small */ 21063 } 21064 /* 21065 * Sender silly-window avoidance. 21066 * Ignore this if we are going to send a 21067 * zero window probe out. 21068 * 21069 * TODO: force data into microscopic window? 21070 * ==> (!pushed || (unsent > usable)) 21071 */ 21072 if (len < (tcp->tcp_max_swnd >> 1) && 21073 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21074 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21075 len == 1) && (! tcp->tcp_zero_win_probe)) { 21076 /* 21077 * If the retransmit timer is not running 21078 * we start it so that we will retransmit 21079 * in the case when the the receiver has 21080 * decremented the window. 21081 */ 21082 if (*snxt == tcp->tcp_snxt && 21083 *snxt == tcp->tcp_suna) { 21084 /* 21085 * We are not supposed to send 21086 * anything. So let's wait a little 21087 * bit longer before breaking SWS 21088 * avoidance. 21089 * 21090 * What should the value be? 21091 * Suggestion: MAX(init rexmit time, 21092 * tcp->tcp_rto) 21093 */ 21094 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21095 } 21096 break; /* success; too small */ 21097 } 21098 } 21099 21100 tcph = tcp->tcp_tcph; 21101 21102 /* 21103 * The reason to adjust len here is that we need to set flags 21104 * and calculate checksum. 21105 */ 21106 if (do_lso_send) 21107 len = lso_usable; 21108 21109 *usable -= len; /* Approximate - can be adjusted later */ 21110 if (*usable > 0) 21111 tcph->th_flags[0] = TH_ACK; 21112 else 21113 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21114 21115 /* 21116 * Prime pump for IP's checksumming on our behalf 21117 * Include the adjustment for a source route if any. 21118 */ 21119 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21120 sum = (sum >> 16) + (sum & 0xFFFF); 21121 U16_TO_ABE16(sum, tcph->th_sum); 21122 21123 U32_TO_ABE32(*snxt, tcph->th_seq); 21124 21125 /* 21126 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21127 * set. For the case when TCP_FSS_VALID is the only valid 21128 * bit (normal active close), branch off only when we think 21129 * that the FIN flag needs to be set. Note for this case, 21130 * that (snxt + len) may not reflect the actual seg_len, 21131 * as len may be further reduced in tcp_xmit_mp(). If len 21132 * gets modified, we will end up here again. 21133 */ 21134 if (tcp->tcp_valid_bits != 0 && 21135 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21136 ((*snxt + len) == tcp->tcp_fss))) { 21137 uchar_t *prev_rptr; 21138 uint32_t prev_snxt = tcp->tcp_snxt; 21139 21140 if (*tail_unsent == 0) { 21141 ASSERT((*xmit_tail)->b_cont != NULL); 21142 *xmit_tail = (*xmit_tail)->b_cont; 21143 prev_rptr = (*xmit_tail)->b_rptr; 21144 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21145 (*xmit_tail)->b_rptr); 21146 } else { 21147 prev_rptr = (*xmit_tail)->b_rptr; 21148 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21149 *tail_unsent; 21150 } 21151 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21152 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21153 /* Restore tcp_snxt so we get amount sent right. */ 21154 tcp->tcp_snxt = prev_snxt; 21155 if (prev_rptr == (*xmit_tail)->b_rptr) { 21156 /* 21157 * If the previous timestamp is still in use, 21158 * don't stomp on it. 21159 */ 21160 if ((*xmit_tail)->b_next == NULL) { 21161 (*xmit_tail)->b_prev = local_time; 21162 (*xmit_tail)->b_next = 21163 (mblk_t *)(uintptr_t)(*snxt); 21164 } 21165 } else 21166 (*xmit_tail)->b_rptr = prev_rptr; 21167 21168 if (mp == NULL) { 21169 if (ire != NULL) 21170 IRE_REFRELE(ire); 21171 return (-1); 21172 } 21173 mp1 = mp->b_cont; 21174 21175 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21176 tcp->tcp_last_sent_len = (ushort_t)len; 21177 while (mp1->b_cont) { 21178 *xmit_tail = (*xmit_tail)->b_cont; 21179 (*xmit_tail)->b_prev = local_time; 21180 (*xmit_tail)->b_next = 21181 (mblk_t *)(uintptr_t)(*snxt); 21182 mp1 = mp1->b_cont; 21183 } 21184 *snxt += len; 21185 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21186 BUMP_LOCAL(tcp->tcp_obsegs); 21187 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21188 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21189 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21190 tcp_send_data(tcp, q, mp); 21191 continue; 21192 } 21193 21194 *snxt += len; /* Adjust later if we don't send all of len */ 21195 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21196 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21197 21198 if (*tail_unsent) { 21199 /* Are the bytes above us in flight? */ 21200 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21201 if (rptr != (*xmit_tail)->b_rptr) { 21202 *tail_unsent -= len; 21203 if (len <= mss) /* LSO is unusable */ 21204 tcp->tcp_last_sent_len = (ushort_t)len; 21205 len += tcp_hdr_len; 21206 if (tcp->tcp_ipversion == IPV4_VERSION) 21207 tcp->tcp_ipha->ipha_length = htons(len); 21208 else 21209 tcp->tcp_ip6h->ip6_plen = 21210 htons(len - 21211 ((char *)&tcp->tcp_ip6h[1] - 21212 tcp->tcp_iphc)); 21213 mp = dupb(*xmit_tail); 21214 if (mp == NULL) { 21215 if (ire != NULL) 21216 IRE_REFRELE(ire); 21217 return (-1); /* out_of_mem */ 21218 } 21219 mp->b_rptr = rptr; 21220 /* 21221 * If the old timestamp is no longer in use, 21222 * sample a new timestamp now. 21223 */ 21224 if ((*xmit_tail)->b_next == NULL) { 21225 (*xmit_tail)->b_prev = local_time; 21226 (*xmit_tail)->b_next = 21227 (mblk_t *)(uintptr_t)(*snxt-len); 21228 } 21229 goto must_alloc; 21230 } 21231 } else { 21232 *xmit_tail = (*xmit_tail)->b_cont; 21233 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21234 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21235 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21236 (*xmit_tail)->b_rptr); 21237 } 21238 21239 (*xmit_tail)->b_prev = local_time; 21240 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21241 21242 *tail_unsent -= len; 21243 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21244 tcp->tcp_last_sent_len = (ushort_t)len; 21245 21246 len += tcp_hdr_len; 21247 if (tcp->tcp_ipversion == IPV4_VERSION) 21248 tcp->tcp_ipha->ipha_length = htons(len); 21249 else 21250 tcp->tcp_ip6h->ip6_plen = htons(len - 21251 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21252 21253 mp = dupb(*xmit_tail); 21254 if (mp == NULL) { 21255 if (ire != NULL) 21256 IRE_REFRELE(ire); 21257 return (-1); /* out_of_mem */ 21258 } 21259 21260 len = tcp_hdr_len; 21261 /* 21262 * There are four reasons to allocate a new hdr mblk: 21263 * 1) The bytes above us are in use by another packet 21264 * 2) We don't have good alignment 21265 * 3) The mblk is being shared 21266 * 4) We don't have enough room for a header 21267 */ 21268 rptr = mp->b_rptr - len; 21269 if (!OK_32PTR(rptr) || 21270 ((db = mp->b_datap), db->db_ref != 2) || 21271 rptr < db->db_base + ire_fp_mp_len) { 21272 /* NOTE: we assume allocb returns an OK_32PTR */ 21273 21274 must_alloc:; 21275 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21276 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21277 if (mp1 == NULL) { 21278 freemsg(mp); 21279 if (ire != NULL) 21280 IRE_REFRELE(ire); 21281 return (-1); /* out_of_mem */ 21282 } 21283 mp1->b_cont = mp; 21284 mp = mp1; 21285 /* Leave room for Link Level header */ 21286 len = tcp_hdr_len; 21287 rptr = 21288 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21289 mp->b_wptr = &rptr[len]; 21290 } 21291 21292 /* 21293 * Fill in the header using the template header, and add 21294 * options such as time-stamp, ECN and/or SACK, as needed. 21295 */ 21296 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21297 21298 mp->b_rptr = rptr; 21299 21300 if (*tail_unsent) { 21301 int spill = *tail_unsent; 21302 21303 mp1 = mp->b_cont; 21304 if (mp1 == NULL) 21305 mp1 = mp; 21306 21307 /* 21308 * If we're a little short, tack on more mblks until 21309 * there is no more spillover. 21310 */ 21311 while (spill < 0) { 21312 mblk_t *nmp; 21313 int nmpsz; 21314 21315 nmp = (*xmit_tail)->b_cont; 21316 nmpsz = MBLKL(nmp); 21317 21318 /* 21319 * Excess data in mblk; can we split it? 21320 * If MDT is enabled for the connection, 21321 * keep on splitting as this is a transient 21322 * send path. 21323 */ 21324 if (!do_lso_send && !tcp->tcp_mdt && 21325 (spill + nmpsz > 0)) { 21326 /* 21327 * Don't split if stream head was 21328 * told to break up larger writes 21329 * into smaller ones. 21330 */ 21331 if (tcp->tcp_maxpsz > 0) 21332 break; 21333 21334 /* 21335 * Next mblk is less than SMSS/2 21336 * rounded up to nearest 64-byte; 21337 * let it get sent as part of the 21338 * next segment. 21339 */ 21340 if (tcp->tcp_localnet && 21341 !tcp->tcp_cork && 21342 (nmpsz < roundup((mss >> 1), 64))) 21343 break; 21344 } 21345 21346 *xmit_tail = nmp; 21347 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21348 /* Stash for rtt use later */ 21349 (*xmit_tail)->b_prev = local_time; 21350 (*xmit_tail)->b_next = 21351 (mblk_t *)(uintptr_t)(*snxt - len); 21352 mp1->b_cont = dupb(*xmit_tail); 21353 mp1 = mp1->b_cont; 21354 21355 spill += nmpsz; 21356 if (mp1 == NULL) { 21357 *tail_unsent = spill; 21358 freemsg(mp); 21359 if (ire != NULL) 21360 IRE_REFRELE(ire); 21361 return (-1); /* out_of_mem */ 21362 } 21363 } 21364 21365 /* Trim back any surplus on the last mblk */ 21366 if (spill >= 0) { 21367 mp1->b_wptr -= spill; 21368 *tail_unsent = spill; 21369 } else { 21370 /* 21371 * We did not send everything we could in 21372 * order to remain within the b_cont limit. 21373 */ 21374 *usable -= spill; 21375 *snxt += spill; 21376 tcp->tcp_last_sent_len += spill; 21377 UPDATE_MIB(&tcps->tcps_mib, 21378 tcpOutDataBytes, spill); 21379 /* 21380 * Adjust the checksum 21381 */ 21382 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21383 sum += spill; 21384 sum = (sum >> 16) + (sum & 0xFFFF); 21385 U16_TO_ABE16(sum, tcph->th_sum); 21386 if (tcp->tcp_ipversion == IPV4_VERSION) { 21387 sum = ntohs( 21388 ((ipha_t *)rptr)->ipha_length) + 21389 spill; 21390 ((ipha_t *)rptr)->ipha_length = 21391 htons(sum); 21392 } else { 21393 sum = ntohs( 21394 ((ip6_t *)rptr)->ip6_plen) + 21395 spill; 21396 ((ip6_t *)rptr)->ip6_plen = 21397 htons(sum); 21398 } 21399 *tail_unsent = 0; 21400 } 21401 } 21402 if (tcp->tcp_ip_forward_progress) { 21403 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21404 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21405 tcp->tcp_ip_forward_progress = B_FALSE; 21406 } 21407 21408 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21409 if (do_lso_send) { 21410 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21411 num_lso_seg); 21412 tcp->tcp_obsegs += num_lso_seg; 21413 21414 TCP_STAT(tcps, tcp_lso_times); 21415 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21416 } else { 21417 tcp_send_data(tcp, q, mp); 21418 BUMP_LOCAL(tcp->tcp_obsegs); 21419 } 21420 } 21421 21422 if (ire != NULL) 21423 IRE_REFRELE(ire); 21424 return (0); 21425 } 21426 21427 /* Unlink and return any mblk that looks like it contains a MDT info */ 21428 static mblk_t * 21429 tcp_mdt_info_mp(mblk_t *mp) 21430 { 21431 mblk_t *prev_mp; 21432 21433 for (;;) { 21434 prev_mp = mp; 21435 /* no more to process? */ 21436 if ((mp = mp->b_cont) == NULL) 21437 break; 21438 21439 switch (DB_TYPE(mp)) { 21440 case M_CTL: 21441 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21442 continue; 21443 ASSERT(prev_mp != NULL); 21444 prev_mp->b_cont = mp->b_cont; 21445 mp->b_cont = NULL; 21446 return (mp); 21447 default: 21448 break; 21449 } 21450 } 21451 return (mp); 21452 } 21453 21454 /* MDT info update routine, called when IP notifies us about MDT */ 21455 static void 21456 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21457 { 21458 boolean_t prev_state; 21459 tcp_stack_t *tcps = tcp->tcp_tcps; 21460 21461 /* 21462 * IP is telling us to abort MDT on this connection? We know 21463 * this because the capability is only turned off when IP 21464 * encounters some pathological cases, e.g. link-layer change 21465 * where the new driver doesn't support MDT, or in situation 21466 * where MDT usage on the link-layer has been switched off. 21467 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21468 * if the link-layer doesn't support MDT, and if it does, it 21469 * will indicate that the feature is to be turned on. 21470 */ 21471 prev_state = tcp->tcp_mdt; 21472 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21473 if (!tcp->tcp_mdt && !first) { 21474 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21475 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21476 (void *)tcp->tcp_connp)); 21477 } 21478 21479 /* 21480 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21481 * so disable MDT otherwise. The checks are done here 21482 * and in tcp_wput_data(). 21483 */ 21484 if (tcp->tcp_mdt && 21485 (tcp->tcp_ipversion == IPV4_VERSION && 21486 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21487 (tcp->tcp_ipversion == IPV6_VERSION && 21488 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21489 tcp->tcp_mdt = B_FALSE; 21490 21491 if (tcp->tcp_mdt) { 21492 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21493 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21494 "version (%d), expected version is %d", 21495 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21496 tcp->tcp_mdt = B_FALSE; 21497 return; 21498 } 21499 21500 /* 21501 * We need the driver to be able to handle at least three 21502 * spans per packet in order for tcp MDT to be utilized. 21503 * The first is for the header portion, while the rest are 21504 * needed to handle a packet that straddles across two 21505 * virtually non-contiguous buffers; a typical tcp packet 21506 * therefore consists of only two spans. Note that we take 21507 * a zero as "don't care". 21508 */ 21509 if (mdt_capab->ill_mdt_span_limit > 0 && 21510 mdt_capab->ill_mdt_span_limit < 3) { 21511 tcp->tcp_mdt = B_FALSE; 21512 return; 21513 } 21514 21515 /* a zero means driver wants default value */ 21516 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21517 tcps->tcps_mdt_max_pbufs); 21518 if (tcp->tcp_mdt_max_pld == 0) 21519 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21520 21521 /* ensure 32-bit alignment */ 21522 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21523 mdt_capab->ill_mdt_hdr_head), 4); 21524 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21525 mdt_capab->ill_mdt_hdr_tail), 4); 21526 21527 if (!first && !prev_state) { 21528 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21529 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21530 (void *)tcp->tcp_connp)); 21531 } 21532 } 21533 } 21534 21535 /* Unlink and return any mblk that looks like it contains a LSO info */ 21536 static mblk_t * 21537 tcp_lso_info_mp(mblk_t *mp) 21538 { 21539 mblk_t *prev_mp; 21540 21541 for (;;) { 21542 prev_mp = mp; 21543 /* no more to process? */ 21544 if ((mp = mp->b_cont) == NULL) 21545 break; 21546 21547 switch (DB_TYPE(mp)) { 21548 case M_CTL: 21549 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21550 continue; 21551 ASSERT(prev_mp != NULL); 21552 prev_mp->b_cont = mp->b_cont; 21553 mp->b_cont = NULL; 21554 return (mp); 21555 default: 21556 break; 21557 } 21558 } 21559 21560 return (mp); 21561 } 21562 21563 /* LSO info update routine, called when IP notifies us about LSO */ 21564 static void 21565 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21566 { 21567 tcp_stack_t *tcps = tcp->tcp_tcps; 21568 21569 /* 21570 * IP is telling us to abort LSO on this connection? We know 21571 * this because the capability is only turned off when IP 21572 * encounters some pathological cases, e.g. link-layer change 21573 * where the new NIC/driver doesn't support LSO, or in situation 21574 * where LSO usage on the link-layer has been switched off. 21575 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21576 * if the link-layer doesn't support LSO, and if it does, it 21577 * will indicate that the feature is to be turned on. 21578 */ 21579 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21580 TCP_STAT(tcps, tcp_lso_enabled); 21581 21582 /* 21583 * We currently only support LSO on simple TCP/IPv4, 21584 * so disable LSO otherwise. The checks are done here 21585 * and in tcp_wput_data(). 21586 */ 21587 if (tcp->tcp_lso && 21588 (tcp->tcp_ipversion == IPV4_VERSION && 21589 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21590 (tcp->tcp_ipversion == IPV6_VERSION)) { 21591 tcp->tcp_lso = B_FALSE; 21592 TCP_STAT(tcps, tcp_lso_disabled); 21593 } else { 21594 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21595 lso_capab->ill_lso_max); 21596 } 21597 } 21598 21599 static void 21600 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21601 { 21602 conn_t *connp = tcp->tcp_connp; 21603 tcp_stack_t *tcps = tcp->tcp_tcps; 21604 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21605 21606 ASSERT(ire != NULL); 21607 21608 /* 21609 * We may be in the fastpath here, and although we essentially do 21610 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21611 * we try to keep things as brief as possible. After all, these 21612 * are only best-effort checks, and we do more thorough ones prior 21613 * to calling tcp_send()/tcp_multisend(). 21614 */ 21615 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21616 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21617 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21618 !(ire->ire_flags & RTF_MULTIRT) && 21619 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21620 CONN_IS_LSO_MD_FASTPATH(connp)) { 21621 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21622 /* Cache the result */ 21623 connp->conn_lso_ok = B_TRUE; 21624 21625 ASSERT(ill->ill_lso_capab != NULL); 21626 if (!ill->ill_lso_capab->ill_lso_on) { 21627 ill->ill_lso_capab->ill_lso_on = 1; 21628 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21629 "LSO for interface %s\n", (void *)connp, 21630 ill->ill_name)); 21631 } 21632 tcp_lso_update(tcp, ill->ill_lso_capab); 21633 } else if (ipst->ips_ip_multidata_outbound && 21634 ILL_MDT_CAPABLE(ill)) { 21635 /* Cache the result */ 21636 connp->conn_mdt_ok = B_TRUE; 21637 21638 ASSERT(ill->ill_mdt_capab != NULL); 21639 if (!ill->ill_mdt_capab->ill_mdt_on) { 21640 ill->ill_mdt_capab->ill_mdt_on = 1; 21641 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21642 "MDT for interface %s\n", (void *)connp, 21643 ill->ill_name)); 21644 } 21645 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21646 } 21647 } 21648 21649 /* 21650 * The goal is to reduce the number of generated tcp segments by 21651 * setting the maxpsz multiplier to 0; this will have an affect on 21652 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21653 * into each packet, up to SMSS bytes. Doing this reduces the number 21654 * of outbound segments and incoming ACKs, thus allowing for better 21655 * network and system performance. In contrast the legacy behavior 21656 * may result in sending less than SMSS size, because the last mblk 21657 * for some packets may have more data than needed to make up SMSS, 21658 * and the legacy code refused to "split" it. 21659 * 21660 * We apply the new behavior on following situations: 21661 * 21662 * 1) Loopback connections, 21663 * 2) Connections in which the remote peer is not on local subnet, 21664 * 3) Local subnet connections over the bge interface (see below). 21665 * 21666 * Ideally, we would like this behavior to apply for interfaces other 21667 * than bge. However, doing so would negatively impact drivers which 21668 * perform dynamic mapping and unmapping of DMA resources, which are 21669 * increased by setting the maxpsz multiplier to 0 (more mblks per 21670 * packet will be generated by tcp). The bge driver does not suffer 21671 * from this, as it copies the mblks into pre-mapped buffers, and 21672 * therefore does not require more I/O resources than before. 21673 * 21674 * Otherwise, this behavior is present on all network interfaces when 21675 * the destination endpoint is non-local, since reducing the number 21676 * of packets in general is good for the network. 21677 * 21678 * TODO We need to remove this hard-coded conditional for bge once 21679 * a better "self-tuning" mechanism, or a way to comprehend 21680 * the driver transmit strategy is devised. Until the solution 21681 * is found and well understood, we live with this hack. 21682 */ 21683 if (!tcp_static_maxpsz && 21684 (tcp->tcp_loopback || !tcp->tcp_localnet || 21685 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21686 /* override the default value */ 21687 tcp->tcp_maxpsz = 0; 21688 21689 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21690 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21691 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21692 } 21693 21694 /* set the stream head parameters accordingly */ 21695 (void) tcp_maxpsz_set(tcp, B_TRUE); 21696 } 21697 21698 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21699 static void 21700 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21701 { 21702 uchar_t fval = *mp->b_rptr; 21703 mblk_t *tail; 21704 queue_t *q = tcp->tcp_wq; 21705 21706 /* TODO: How should flush interact with urgent data? */ 21707 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21708 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21709 /* 21710 * Flush only data that has not yet been put on the wire. If 21711 * we flush data that we have already transmitted, life, as we 21712 * know it, may come to an end. 21713 */ 21714 tail = tcp->tcp_xmit_tail; 21715 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21716 tcp->tcp_xmit_tail_unsent = 0; 21717 tcp->tcp_unsent = 0; 21718 if (tail->b_wptr != tail->b_rptr) 21719 tail = tail->b_cont; 21720 if (tail) { 21721 mblk_t **excess = &tcp->tcp_xmit_head; 21722 for (;;) { 21723 mblk_t *mp1 = *excess; 21724 if (mp1 == tail) 21725 break; 21726 tcp->tcp_xmit_tail = mp1; 21727 tcp->tcp_xmit_last = mp1; 21728 excess = &mp1->b_cont; 21729 } 21730 *excess = NULL; 21731 tcp_close_mpp(&tail); 21732 if (tcp->tcp_snd_zcopy_aware) 21733 tcp_zcopy_notify(tcp); 21734 } 21735 /* 21736 * We have no unsent data, so unsent must be less than 21737 * tcp_xmit_lowater, so re-enable flow. 21738 */ 21739 mutex_enter(&tcp->tcp_non_sq_lock); 21740 if (tcp->tcp_flow_stopped) { 21741 tcp_clrqfull(tcp); 21742 } 21743 mutex_exit(&tcp->tcp_non_sq_lock); 21744 } 21745 /* 21746 * TODO: you can't just flush these, you have to increase rwnd for one 21747 * thing. For another, how should urgent data interact? 21748 */ 21749 if (fval & FLUSHR) { 21750 *mp->b_rptr = fval & ~FLUSHW; 21751 /* XXX */ 21752 qreply(q, mp); 21753 return; 21754 } 21755 freemsg(mp); 21756 } 21757 21758 /* 21759 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21760 * messages. 21761 */ 21762 static void 21763 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21764 { 21765 mblk_t *mp1; 21766 STRUCT_HANDLE(strbuf, sb); 21767 uint16_t port; 21768 queue_t *q = tcp->tcp_wq; 21769 in6_addr_t v6addr; 21770 ipaddr_t v4addr; 21771 uint32_t flowinfo = 0; 21772 int addrlen; 21773 21774 /* Make sure it is one of ours. */ 21775 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21776 case TI_GETMYNAME: 21777 case TI_GETPEERNAME: 21778 break; 21779 default: 21780 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21781 return; 21782 } 21783 switch (mi_copy_state(q, mp, &mp1)) { 21784 case -1: 21785 return; 21786 case MI_COPY_CASE(MI_COPY_IN, 1): 21787 break; 21788 case MI_COPY_CASE(MI_COPY_OUT, 1): 21789 /* Copy out the strbuf. */ 21790 mi_copyout(q, mp); 21791 return; 21792 case MI_COPY_CASE(MI_COPY_OUT, 2): 21793 /* All done. */ 21794 mi_copy_done(q, mp, 0); 21795 return; 21796 default: 21797 mi_copy_done(q, mp, EPROTO); 21798 return; 21799 } 21800 /* Check alignment of the strbuf */ 21801 if (!OK_32PTR(mp1->b_rptr)) { 21802 mi_copy_done(q, mp, EINVAL); 21803 return; 21804 } 21805 21806 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 21807 (void *)mp1->b_rptr); 21808 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21809 21810 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21811 mi_copy_done(q, mp, EINVAL); 21812 return; 21813 } 21814 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21815 case TI_GETMYNAME: 21816 if (tcp->tcp_family == AF_INET) { 21817 if (tcp->tcp_ipversion == IPV4_VERSION) { 21818 v4addr = tcp->tcp_ipha->ipha_src; 21819 } else { 21820 /* can't return an address in this case */ 21821 v4addr = 0; 21822 } 21823 } else { 21824 /* tcp->tcp_family == AF_INET6 */ 21825 if (tcp->tcp_ipversion == IPV4_VERSION) { 21826 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 21827 &v6addr); 21828 } else { 21829 v6addr = tcp->tcp_ip6h->ip6_src; 21830 } 21831 } 21832 port = tcp->tcp_lport; 21833 break; 21834 case TI_GETPEERNAME: 21835 if (tcp->tcp_family == AF_INET) { 21836 if (tcp->tcp_ipversion == IPV4_VERSION) { 21837 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 21838 v4addr); 21839 } else { 21840 /* can't return an address in this case */ 21841 v4addr = 0; 21842 } 21843 } else { 21844 /* tcp->tcp_family == AF_INET6) */ 21845 v6addr = tcp->tcp_remote_v6; 21846 if (tcp->tcp_ipversion == IPV6_VERSION) { 21847 /* 21848 * No flowinfo if tcp->tcp_ipversion is v4. 21849 * 21850 * flowinfo was already initialized to zero 21851 * where it was declared above, so only 21852 * set it if ipversion is v6. 21853 */ 21854 flowinfo = tcp->tcp_ip6h->ip6_vcf & 21855 ~IPV6_VERS_AND_FLOW_MASK; 21856 } 21857 } 21858 port = tcp->tcp_fport; 21859 break; 21860 default: 21861 mi_copy_done(q, mp, EPROTO); 21862 return; 21863 } 21864 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21865 if (!mp1) 21866 return; 21867 21868 if (tcp->tcp_family == AF_INET) { 21869 sin_t *sin; 21870 21871 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 21872 sin = (sin_t *)mp1->b_rptr; 21873 mp1->b_wptr = (uchar_t *)&sin[1]; 21874 *sin = sin_null; 21875 sin->sin_family = AF_INET; 21876 sin->sin_addr.s_addr = v4addr; 21877 sin->sin_port = port; 21878 } else { 21879 /* tcp->tcp_family == AF_INET6 */ 21880 sin6_t *sin6; 21881 21882 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 21883 sin6 = (sin6_t *)mp1->b_rptr; 21884 mp1->b_wptr = (uchar_t *)&sin6[1]; 21885 *sin6 = sin6_null; 21886 sin6->sin6_family = AF_INET6; 21887 sin6->sin6_flowinfo = flowinfo; 21888 sin6->sin6_addr = v6addr; 21889 sin6->sin6_port = port; 21890 } 21891 /* Copy out the address */ 21892 mi_copyout(q, mp); 21893 } 21894 21895 /* 21896 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21897 * messages. 21898 */ 21899 /* ARGSUSED */ 21900 static void 21901 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21902 { 21903 conn_t *connp = (conn_t *)arg; 21904 tcp_t *tcp = connp->conn_tcp; 21905 queue_t *q = tcp->tcp_wq; 21906 struct iocblk *iocp; 21907 tcp_stack_t *tcps = tcp->tcp_tcps; 21908 21909 ASSERT(DB_TYPE(mp) == M_IOCTL); 21910 /* 21911 * Try and ASSERT the minimum possible references on the 21912 * conn early enough. Since we are executing on write side, 21913 * the connection is obviously not detached and that means 21914 * there is a ref each for TCP and IP. Since we are behind 21915 * the squeue, the minimum references needed are 3. If the 21916 * conn is in classifier hash list, there should be an 21917 * extra ref for that (we check both the possibilities). 21918 */ 21919 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21920 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21921 21922 iocp = (struct iocblk *)mp->b_rptr; 21923 switch (iocp->ioc_cmd) { 21924 case TCP_IOC_DEFAULT_Q: 21925 /* Wants to be the default wq. */ 21926 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21927 iocp->ioc_error = EPERM; 21928 iocp->ioc_count = 0; 21929 mp->b_datap->db_type = M_IOCACK; 21930 qreply(q, mp); 21931 return; 21932 } 21933 tcp_def_q_set(tcp, mp); 21934 return; 21935 case _SIOCSOCKFALLBACK: 21936 /* 21937 * Either sockmod is about to be popped and the socket 21938 * would now be treated as a plain stream, or a module 21939 * is about to be pushed so we could no longer use read- 21940 * side synchronous streams for fused loopback tcp. 21941 * Drain any queued data and disable direct sockfs 21942 * interface from now on. 21943 */ 21944 if (!tcp->tcp_issocket) { 21945 DB_TYPE(mp) = M_IOCNAK; 21946 iocp->ioc_error = EINVAL; 21947 } else { 21948 #ifdef _ILP32 21949 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 21950 #else 21951 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21952 #endif 21953 /* 21954 * Insert this socket into the acceptor hash. 21955 * We might need it for T_CONN_RES message 21956 */ 21957 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21958 21959 if (tcp->tcp_fused) { 21960 /* 21961 * This is a fused loopback tcp; disable 21962 * read-side synchronous streams interface 21963 * and drain any queued data. It is okay 21964 * to do this for non-synchronous streams 21965 * fused tcp as well. 21966 */ 21967 tcp_fuse_disable_pair(tcp, B_FALSE); 21968 } 21969 tcp->tcp_issocket = B_FALSE; 21970 TCP_STAT(tcps, tcp_sock_fallback); 21971 21972 DB_TYPE(mp) = M_IOCACK; 21973 iocp->ioc_error = 0; 21974 } 21975 iocp->ioc_count = 0; 21976 iocp->ioc_rval = 0; 21977 qreply(q, mp); 21978 return; 21979 } 21980 CALL_IP_WPUT(connp, q, mp); 21981 } 21982 21983 /* 21984 * This routine is called by tcp_wput() to handle all TPI requests. 21985 */ 21986 /* ARGSUSED */ 21987 static void 21988 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21989 { 21990 conn_t *connp = (conn_t *)arg; 21991 tcp_t *tcp = connp->conn_tcp; 21992 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21993 uchar_t *rptr; 21994 t_scalar_t type; 21995 int len; 21996 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 21997 21998 /* 21999 * Try and ASSERT the minimum possible references on the 22000 * conn early enough. Since we are executing on write side, 22001 * the connection is obviously not detached and that means 22002 * there is a ref each for TCP and IP. Since we are behind 22003 * the squeue, the minimum references needed are 3. If the 22004 * conn is in classifier hash list, there should be an 22005 * extra ref for that (we check both the possibilities). 22006 */ 22007 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22008 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22009 22010 rptr = mp->b_rptr; 22011 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22012 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22013 type = ((union T_primitives *)rptr)->type; 22014 if (type == T_EXDATA_REQ) { 22015 uint32_t msize = msgdsize(mp->b_cont); 22016 22017 len = msize - 1; 22018 if (len < 0) { 22019 freemsg(mp); 22020 return; 22021 } 22022 /* 22023 * Try to force urgent data out on the wire. 22024 * Even if we have unsent data this will 22025 * at least send the urgent flag. 22026 * XXX does not handle more flag correctly. 22027 */ 22028 len += tcp->tcp_unsent; 22029 len += tcp->tcp_snxt; 22030 tcp->tcp_urg = len; 22031 tcp->tcp_valid_bits |= TCP_URG_VALID; 22032 22033 /* Bypass tcp protocol for fused tcp loopback */ 22034 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22035 return; 22036 } else if (type != T_DATA_REQ) { 22037 goto non_urgent_data; 22038 } 22039 /* TODO: options, flags, ... from user */ 22040 /* Set length to zero for reclamation below */ 22041 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22042 freeb(mp); 22043 return; 22044 } else { 22045 if (tcp->tcp_debug) { 22046 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22047 "tcp_wput_proto, dropping one..."); 22048 } 22049 freemsg(mp); 22050 return; 22051 } 22052 22053 non_urgent_data: 22054 22055 switch ((int)tprim->type) { 22056 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22057 /* 22058 * save the kssl_ent_t from the next block, and convert this 22059 * back to a normal bind_req. 22060 */ 22061 if (mp->b_cont != NULL) { 22062 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22063 22064 if (tcp->tcp_kssl_ent != NULL) { 22065 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22066 KSSL_NO_PROXY); 22067 tcp->tcp_kssl_ent = NULL; 22068 } 22069 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22070 sizeof (kssl_ent_t)); 22071 kssl_hold_ent(tcp->tcp_kssl_ent); 22072 freemsg(mp->b_cont); 22073 mp->b_cont = NULL; 22074 } 22075 tprim->type = T_BIND_REQ; 22076 22077 /* FALLTHROUGH */ 22078 case O_T_BIND_REQ: /* bind request */ 22079 case T_BIND_REQ: /* new semantics bind request */ 22080 tcp_bind(tcp, mp); 22081 break; 22082 case T_UNBIND_REQ: /* unbind request */ 22083 tcp_unbind(tcp, mp); 22084 break; 22085 case O_T_CONN_RES: /* old connection response XXX */ 22086 case T_CONN_RES: /* connection response */ 22087 tcp_accept(tcp, mp); 22088 break; 22089 case T_CONN_REQ: /* connection request */ 22090 tcp_connect(tcp, mp); 22091 break; 22092 case T_DISCON_REQ: /* disconnect request */ 22093 tcp_disconnect(tcp, mp); 22094 break; 22095 case T_CAPABILITY_REQ: 22096 tcp_capability_req(tcp, mp); /* capability request */ 22097 break; 22098 case T_INFO_REQ: /* information request */ 22099 tcp_info_req(tcp, mp); 22100 break; 22101 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22102 /* Only IP is allowed to return meaningful value */ 22103 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 22104 break; 22105 case T_OPTMGMT_REQ: 22106 /* 22107 * Note: no support for snmpcom_req() through new 22108 * T_OPTMGMT_REQ. See comments in ip.c 22109 */ 22110 /* Only IP is allowed to return meaningful value */ 22111 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 22112 break; 22113 22114 case T_UNITDATA_REQ: /* unitdata request */ 22115 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22116 break; 22117 case T_ORDREL_REQ: /* orderly release req */ 22118 freemsg(mp); 22119 22120 if (tcp->tcp_fused) 22121 tcp_unfuse(tcp); 22122 22123 if (tcp_xmit_end(tcp) != 0) { 22124 /* 22125 * We were crossing FINs and got a reset from 22126 * the other side. Just ignore it. 22127 */ 22128 if (tcp->tcp_debug) { 22129 (void) strlog(TCP_MOD_ID, 0, 1, 22130 SL_ERROR|SL_TRACE, 22131 "tcp_wput_proto, T_ORDREL_REQ out of " 22132 "state %s", 22133 tcp_display(tcp, NULL, 22134 DISP_ADDR_AND_PORT)); 22135 } 22136 } 22137 break; 22138 case T_ADDR_REQ: 22139 tcp_addr_req(tcp, mp); 22140 break; 22141 default: 22142 if (tcp->tcp_debug) { 22143 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22144 "tcp_wput_proto, bogus TPI msg, type %d", 22145 tprim->type); 22146 } 22147 /* 22148 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22149 * to recover. 22150 */ 22151 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22152 break; 22153 } 22154 } 22155 22156 /* 22157 * The TCP write service routine should never be called... 22158 */ 22159 /* ARGSUSED */ 22160 static void 22161 tcp_wsrv(queue_t *q) 22162 { 22163 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22164 22165 TCP_STAT(tcps, tcp_wsrv_called); 22166 } 22167 22168 /* Non overlapping byte exchanger */ 22169 static void 22170 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22171 { 22172 uchar_t uch; 22173 22174 while (len-- > 0) { 22175 uch = a[len]; 22176 a[len] = b[len]; 22177 b[len] = uch; 22178 } 22179 } 22180 22181 /* 22182 * Send out a control packet on the tcp connection specified. This routine 22183 * is typically called where we need a simple ACK or RST generated. 22184 */ 22185 static void 22186 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22187 { 22188 uchar_t *rptr; 22189 tcph_t *tcph; 22190 ipha_t *ipha = NULL; 22191 ip6_t *ip6h = NULL; 22192 uint32_t sum; 22193 int tcp_hdr_len; 22194 int tcp_ip_hdr_len; 22195 mblk_t *mp; 22196 tcp_stack_t *tcps = tcp->tcp_tcps; 22197 22198 /* 22199 * Save sum for use in source route later. 22200 */ 22201 ASSERT(tcp != NULL); 22202 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22203 tcp_hdr_len = tcp->tcp_hdr_len; 22204 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22205 22206 /* If a text string is passed in with the request, pass it to strlog. */ 22207 if (str != NULL && tcp->tcp_debug) { 22208 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22209 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22210 str, seq, ack, ctl); 22211 } 22212 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22213 BPRI_MED); 22214 if (mp == NULL) { 22215 return; 22216 } 22217 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22218 mp->b_rptr = rptr; 22219 mp->b_wptr = &rptr[tcp_hdr_len]; 22220 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22221 22222 if (tcp->tcp_ipversion == IPV4_VERSION) { 22223 ipha = (ipha_t *)rptr; 22224 ipha->ipha_length = htons(tcp_hdr_len); 22225 } else { 22226 ip6h = (ip6_t *)rptr; 22227 ASSERT(tcp != NULL); 22228 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22229 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22230 } 22231 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22232 tcph->th_flags[0] = (uint8_t)ctl; 22233 if (ctl & TH_RST) { 22234 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22235 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22236 /* 22237 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22238 */ 22239 if (tcp->tcp_snd_ts_ok && 22240 tcp->tcp_state > TCPS_SYN_SENT) { 22241 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22242 *(mp->b_wptr) = TCPOPT_EOL; 22243 if (tcp->tcp_ipversion == IPV4_VERSION) { 22244 ipha->ipha_length = htons(tcp_hdr_len - 22245 TCPOPT_REAL_TS_LEN); 22246 } else { 22247 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22248 TCPOPT_REAL_TS_LEN); 22249 } 22250 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22251 sum -= TCPOPT_REAL_TS_LEN; 22252 } 22253 } 22254 if (ctl & TH_ACK) { 22255 if (tcp->tcp_snd_ts_ok) { 22256 U32_TO_BE32(lbolt, 22257 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22258 U32_TO_BE32(tcp->tcp_ts_recent, 22259 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22260 } 22261 22262 /* Update the latest receive window size in TCP header. */ 22263 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22264 tcph->th_win); 22265 tcp->tcp_rack = ack; 22266 tcp->tcp_rack_cnt = 0; 22267 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22268 } 22269 BUMP_LOCAL(tcp->tcp_obsegs); 22270 U32_TO_BE32(seq, tcph->th_seq); 22271 U32_TO_BE32(ack, tcph->th_ack); 22272 /* 22273 * Include the adjustment for a source route if any. 22274 */ 22275 sum = (sum >> 16) + (sum & 0xFFFF); 22276 U16_TO_BE16(sum, tcph->th_sum); 22277 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22278 tcp_send_data(tcp, tcp->tcp_wq, mp); 22279 } 22280 22281 /* 22282 * If this routine returns B_TRUE, TCP can generate a RST in response 22283 * to a segment. If it returns B_FALSE, TCP should not respond. 22284 */ 22285 static boolean_t 22286 tcp_send_rst_chk(tcp_stack_t *tcps) 22287 { 22288 clock_t now; 22289 22290 /* 22291 * TCP needs to protect itself from generating too many RSTs. 22292 * This can be a DoS attack by sending us random segments 22293 * soliciting RSTs. 22294 * 22295 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22296 * in each 1 second interval. In this way, TCP still generate 22297 * RSTs in normal cases but when under attack, the impact is 22298 * limited. 22299 */ 22300 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22301 now = lbolt; 22302 /* lbolt can wrap around. */ 22303 if ((tcps->tcps_last_rst_intrvl > now) || 22304 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22305 1*SECONDS)) { 22306 tcps->tcps_last_rst_intrvl = now; 22307 tcps->tcps_rst_cnt = 1; 22308 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22309 return (B_FALSE); 22310 } 22311 } 22312 return (B_TRUE); 22313 } 22314 22315 /* 22316 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22317 */ 22318 static void 22319 tcp_ip_ire_mark_advice(tcp_t *tcp) 22320 { 22321 mblk_t *mp; 22322 ipic_t *ipic; 22323 22324 if (tcp->tcp_ipversion == IPV4_VERSION) { 22325 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22326 &ipic); 22327 } else { 22328 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22329 &ipic); 22330 } 22331 if (mp == NULL) 22332 return; 22333 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22334 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22335 } 22336 22337 /* 22338 * Return an IP advice ioctl mblk and set ipic to be the pointer 22339 * to the advice structure. 22340 */ 22341 static mblk_t * 22342 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22343 { 22344 struct iocblk *ioc; 22345 mblk_t *mp, *mp1; 22346 22347 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22348 if (mp == NULL) 22349 return (NULL); 22350 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22351 *ipic = (ipic_t *)mp->b_rptr; 22352 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22353 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22354 22355 bcopy(addr, *ipic + 1, addr_len); 22356 22357 (*ipic)->ipic_addr_length = addr_len; 22358 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22359 22360 mp1 = mkiocb(IP_IOCTL); 22361 if (mp1 == NULL) { 22362 freemsg(mp); 22363 return (NULL); 22364 } 22365 mp1->b_cont = mp; 22366 ioc = (struct iocblk *)mp1->b_rptr; 22367 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22368 22369 return (mp1); 22370 } 22371 22372 /* 22373 * Generate a reset based on an inbound packet for which there is no active 22374 * tcp state that we can find. 22375 * 22376 * IPSEC NOTE : Try to send the reply with the same protection as it came 22377 * in. We still have the ipsec_mp that the packet was attached to. Thus 22378 * the packet will go out at the same level of protection as it came in by 22379 * converting the IPSEC_IN to IPSEC_OUT. 22380 */ 22381 static void 22382 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22383 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22384 tcp_stack_t *tcps) 22385 { 22386 ipha_t *ipha = NULL; 22387 ip6_t *ip6h = NULL; 22388 ushort_t len; 22389 tcph_t *tcph; 22390 int i; 22391 mblk_t *ipsec_mp; 22392 boolean_t mctl_present; 22393 ipic_t *ipic; 22394 ipaddr_t v4addr; 22395 in6_addr_t v6addr; 22396 int addr_len; 22397 void *addr; 22398 queue_t *q = tcps->tcps_g_q; 22399 tcp_t *tcp; 22400 cred_t *cr; 22401 mblk_t *nmp; 22402 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22403 22404 if (tcps->tcps_g_q == NULL) { 22405 /* 22406 * For non-zero stackids the default queue isn't created 22407 * until the first open, thus there can be a need to send 22408 * a reset before then. But we can't do that, hence we just 22409 * drop the packet. Later during boot, when the default queue 22410 * has been setup, a retransmitted packet from the peer 22411 * will result in a reset. 22412 */ 22413 ASSERT(tcps->tcps_netstack->netstack_stackid != 22414 GLOBAL_NETSTACKID); 22415 freemsg(mp); 22416 return; 22417 } 22418 22419 tcp = Q_TO_TCP(q); 22420 22421 if (!tcp_send_rst_chk(tcps)) { 22422 tcps->tcps_rst_unsent++; 22423 freemsg(mp); 22424 return; 22425 } 22426 22427 if (mp->b_datap->db_type == M_CTL) { 22428 ipsec_mp = mp; 22429 mp = mp->b_cont; 22430 mctl_present = B_TRUE; 22431 } else { 22432 ipsec_mp = mp; 22433 mctl_present = B_FALSE; 22434 } 22435 22436 if (str && q && tcps->tcps_dbg) { 22437 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22438 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22439 "flags 0x%x", 22440 str, seq, ack, ctl); 22441 } 22442 if (mp->b_datap->db_ref != 1) { 22443 mblk_t *mp1 = copyb(mp); 22444 freemsg(mp); 22445 mp = mp1; 22446 if (!mp) { 22447 if (mctl_present) 22448 freeb(ipsec_mp); 22449 return; 22450 } else { 22451 if (mctl_present) { 22452 ipsec_mp->b_cont = mp; 22453 } else { 22454 ipsec_mp = mp; 22455 } 22456 } 22457 } else if (mp->b_cont) { 22458 freemsg(mp->b_cont); 22459 mp->b_cont = NULL; 22460 } 22461 /* 22462 * We skip reversing source route here. 22463 * (for now we replace all IP options with EOL) 22464 */ 22465 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22466 ipha = (ipha_t *)mp->b_rptr; 22467 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22468 mp->b_rptr[i] = IPOPT_EOL; 22469 /* 22470 * Make sure that src address isn't flagrantly invalid. 22471 * Not all broadcast address checking for the src address 22472 * is possible, since we don't know the netmask of the src 22473 * addr. No check for destination address is done, since 22474 * IP will not pass up a packet with a broadcast dest 22475 * address to TCP. Similar checks are done below for IPv6. 22476 */ 22477 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22478 CLASSD(ipha->ipha_src)) { 22479 freemsg(ipsec_mp); 22480 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22481 return; 22482 } 22483 } else { 22484 ip6h = (ip6_t *)mp->b_rptr; 22485 22486 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22487 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22488 freemsg(ipsec_mp); 22489 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22490 return; 22491 } 22492 22493 /* Remove any extension headers assuming partial overlay */ 22494 if (ip_hdr_len > IPV6_HDR_LEN) { 22495 uint8_t *to; 22496 22497 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22498 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22499 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22500 ip_hdr_len = IPV6_HDR_LEN; 22501 ip6h = (ip6_t *)mp->b_rptr; 22502 ip6h->ip6_nxt = IPPROTO_TCP; 22503 } 22504 } 22505 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22506 if (tcph->th_flags[0] & TH_RST) { 22507 freemsg(ipsec_mp); 22508 return; 22509 } 22510 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22511 len = ip_hdr_len + sizeof (tcph_t); 22512 mp->b_wptr = &mp->b_rptr[len]; 22513 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22514 ipha->ipha_length = htons(len); 22515 /* Swap addresses */ 22516 v4addr = ipha->ipha_src; 22517 ipha->ipha_src = ipha->ipha_dst; 22518 ipha->ipha_dst = v4addr; 22519 ipha->ipha_ident = 0; 22520 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22521 addr_len = IP_ADDR_LEN; 22522 addr = &v4addr; 22523 } else { 22524 /* No ip6i_t in this case */ 22525 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22526 /* Swap addresses */ 22527 v6addr = ip6h->ip6_src; 22528 ip6h->ip6_src = ip6h->ip6_dst; 22529 ip6h->ip6_dst = v6addr; 22530 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22531 addr_len = IPV6_ADDR_LEN; 22532 addr = &v6addr; 22533 } 22534 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22535 U32_TO_BE32(ack, tcph->th_ack); 22536 U32_TO_BE32(seq, tcph->th_seq); 22537 U16_TO_BE16(0, tcph->th_win); 22538 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22539 tcph->th_flags[0] = (uint8_t)ctl; 22540 if (ctl & TH_RST) { 22541 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22542 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22543 } 22544 22545 /* IP trusts us to set up labels when required. */ 22546 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 22547 crgetlabel(cr) != NULL) { 22548 int err, adjust; 22549 22550 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22551 err = tsol_check_label(cr, &mp, &adjust, 22552 tcp->tcp_connp->conn_mac_exempt, 22553 tcps->tcps_netstack->netstack_ip); 22554 else 22555 err = tsol_check_label_v6(cr, &mp, &adjust, 22556 tcp->tcp_connp->conn_mac_exempt, 22557 tcps->tcps_netstack->netstack_ip); 22558 if (mctl_present) 22559 ipsec_mp->b_cont = mp; 22560 else 22561 ipsec_mp = mp; 22562 if (err != 0) { 22563 freemsg(ipsec_mp); 22564 return; 22565 } 22566 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22567 ipha = (ipha_t *)mp->b_rptr; 22568 adjust += ntohs(ipha->ipha_length); 22569 ipha->ipha_length = htons(adjust); 22570 } else { 22571 ip6h = (ip6_t *)mp->b_rptr; 22572 } 22573 } 22574 22575 if (mctl_present) { 22576 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22577 22578 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22579 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22580 return; 22581 } 22582 } 22583 if (zoneid == ALL_ZONES) 22584 zoneid = GLOBAL_ZONEID; 22585 22586 /* Add the zoneid so ip_output routes it properly */ 22587 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22588 freemsg(ipsec_mp); 22589 return; 22590 } 22591 ipsec_mp = nmp; 22592 22593 /* 22594 * NOTE: one might consider tracing a TCP packet here, but 22595 * this function has no active TCP state and no tcp structure 22596 * that has a trace buffer. If we traced here, we would have 22597 * to keep a local trace buffer in tcp_record_trace(). 22598 * 22599 * TSol note: The mblk that contains the incoming packet was 22600 * reused by tcp_xmit_listener_reset, so it already contains 22601 * the right credentials and we don't need to call mblk_setcred. 22602 * Also the conn's cred is not right since it is associated 22603 * with tcps_g_q. 22604 */ 22605 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22606 22607 /* 22608 * Tell IP to mark the IRE used for this destination temporary. 22609 * This way, we can limit our exposure to DoS attack because IP 22610 * creates an IRE for each destination. If there are too many, 22611 * the time to do any routing lookup will be extremely long. And 22612 * the lookup can be in interrupt context. 22613 * 22614 * Note that in normal circumstances, this marking should not 22615 * affect anything. It would be nice if only 1 message is 22616 * needed to inform IP that the IRE created for this RST should 22617 * not be added to the cache table. But there is currently 22618 * not such communication mechanism between TCP and IP. So 22619 * the best we can do now is to send the advice ioctl to IP 22620 * to mark the IRE temporary. 22621 */ 22622 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22623 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22624 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22625 } 22626 } 22627 22628 /* 22629 * Initiate closedown sequence on an active connection. (May be called as 22630 * writer.) Return value zero for OK return, non-zero for error return. 22631 */ 22632 static int 22633 tcp_xmit_end(tcp_t *tcp) 22634 { 22635 ipic_t *ipic; 22636 mblk_t *mp; 22637 tcp_stack_t *tcps = tcp->tcp_tcps; 22638 22639 if (tcp->tcp_state < TCPS_SYN_RCVD || 22640 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22641 /* 22642 * Invalid state, only states TCPS_SYN_RCVD, 22643 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22644 */ 22645 return (-1); 22646 } 22647 22648 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22649 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22650 /* 22651 * If there is nothing more unsent, send the FIN now. 22652 * Otherwise, it will go out with the last segment. 22653 */ 22654 if (tcp->tcp_unsent == 0) { 22655 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22656 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22657 22658 if (mp) { 22659 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22660 tcp_send_data(tcp, tcp->tcp_wq, mp); 22661 } else { 22662 /* 22663 * Couldn't allocate msg. Pretend we got it out. 22664 * Wait for rexmit timeout. 22665 */ 22666 tcp->tcp_snxt = tcp->tcp_fss + 1; 22667 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22668 } 22669 22670 /* 22671 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22672 * changed. 22673 */ 22674 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22675 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22676 } 22677 } else { 22678 /* 22679 * If tcp->tcp_cork is set, then the data will not get sent, 22680 * so we have to check that and unset it first. 22681 */ 22682 if (tcp->tcp_cork) 22683 tcp->tcp_cork = B_FALSE; 22684 tcp_wput_data(tcp, NULL, B_FALSE); 22685 } 22686 22687 /* 22688 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22689 * is 0, don't update the cache. 22690 */ 22691 if (tcps->tcps_rtt_updates == 0 || 22692 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22693 return (0); 22694 22695 /* 22696 * NOTE: should not update if source routes i.e. if tcp_remote if 22697 * different from the destination. 22698 */ 22699 if (tcp->tcp_ipversion == IPV4_VERSION) { 22700 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22701 return (0); 22702 } 22703 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22704 &ipic); 22705 } else { 22706 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22707 &tcp->tcp_ip6h->ip6_dst))) { 22708 return (0); 22709 } 22710 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22711 &ipic); 22712 } 22713 22714 /* Record route attributes in the IRE for use by future connections. */ 22715 if (mp == NULL) 22716 return (0); 22717 22718 /* 22719 * We do not have a good algorithm to update ssthresh at this time. 22720 * So don't do any update. 22721 */ 22722 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22723 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22724 22725 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22726 return (0); 22727 } 22728 22729 /* 22730 * Generate a "no listener here" RST in response to an "unknown" segment. 22731 * Note that we are reusing the incoming mp to construct the outgoing 22732 * RST. 22733 */ 22734 void 22735 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22736 tcp_stack_t *tcps) 22737 { 22738 uchar_t *rptr; 22739 uint32_t seg_len; 22740 tcph_t *tcph; 22741 uint32_t seg_seq; 22742 uint32_t seg_ack; 22743 uint_t flags; 22744 mblk_t *ipsec_mp; 22745 ipha_t *ipha; 22746 ip6_t *ip6h; 22747 boolean_t mctl_present = B_FALSE; 22748 boolean_t check = B_TRUE; 22749 boolean_t policy_present; 22750 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22751 22752 TCP_STAT(tcps, tcp_no_listener); 22753 22754 ipsec_mp = mp; 22755 22756 if (mp->b_datap->db_type == M_CTL) { 22757 ipsec_in_t *ii; 22758 22759 mctl_present = B_TRUE; 22760 mp = mp->b_cont; 22761 22762 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22763 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22764 if (ii->ipsec_in_dont_check) { 22765 check = B_FALSE; 22766 if (!ii->ipsec_in_secure) { 22767 freeb(ipsec_mp); 22768 mctl_present = B_FALSE; 22769 ipsec_mp = mp; 22770 } 22771 } 22772 } 22773 22774 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22775 policy_present = ipss->ipsec_inbound_v4_policy_present; 22776 ipha = (ipha_t *)mp->b_rptr; 22777 ip6h = NULL; 22778 } else { 22779 policy_present = ipss->ipsec_inbound_v6_policy_present; 22780 ipha = NULL; 22781 ip6h = (ip6_t *)mp->b_rptr; 22782 } 22783 22784 if (check && policy_present) { 22785 /* 22786 * The conn_t parameter is NULL because we already know 22787 * nobody's home. 22788 */ 22789 ipsec_mp = ipsec_check_global_policy( 22790 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22791 tcps->tcps_netstack); 22792 if (ipsec_mp == NULL) 22793 return; 22794 } 22795 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22796 DTRACE_PROBE2( 22797 tx__ip__log__error__nolistener__tcp, 22798 char *, "Could not reply with RST to mp(1)", 22799 mblk_t *, mp); 22800 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22801 freemsg(ipsec_mp); 22802 return; 22803 } 22804 22805 rptr = mp->b_rptr; 22806 22807 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22808 seg_seq = BE32_TO_U32(tcph->th_seq); 22809 seg_ack = BE32_TO_U32(tcph->th_ack); 22810 flags = tcph->th_flags[0]; 22811 22812 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22813 if (flags & TH_RST) { 22814 freemsg(ipsec_mp); 22815 } else if (flags & TH_ACK) { 22816 tcp_xmit_early_reset("no tcp, reset", 22817 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps); 22818 } else { 22819 if (flags & TH_SYN) { 22820 seg_len++; 22821 } else { 22822 /* 22823 * Here we violate the RFC. Note that a normal 22824 * TCP will never send a segment without the ACK 22825 * flag, except for RST or SYN segment. This 22826 * segment is neither. Just drop it on the 22827 * floor. 22828 */ 22829 freemsg(ipsec_mp); 22830 tcps->tcps_rst_unsent++; 22831 return; 22832 } 22833 22834 tcp_xmit_early_reset("no tcp, reset/ack", 22835 ipsec_mp, 0, seg_seq + seg_len, 22836 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps); 22837 } 22838 } 22839 22840 /* 22841 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22842 * ip and tcp header ready to pass down to IP. If the mp passed in is 22843 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22844 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22845 * otherwise it will dup partial mblks.) 22846 * Otherwise, an appropriate ACK packet will be generated. This 22847 * routine is not usually called to send new data for the first time. It 22848 * is mostly called out of the timer for retransmits, and to generate ACKs. 22849 * 22850 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22851 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22852 * of the original mblk chain will be returned in *offset and *end_mp. 22853 */ 22854 mblk_t * 22855 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22856 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22857 boolean_t rexmit) 22858 { 22859 int data_length; 22860 int32_t off = 0; 22861 uint_t flags; 22862 mblk_t *mp1; 22863 mblk_t *mp2; 22864 uchar_t *rptr; 22865 tcph_t *tcph; 22866 int32_t num_sack_blk = 0; 22867 int32_t sack_opt_len = 0; 22868 tcp_stack_t *tcps = tcp->tcp_tcps; 22869 22870 /* Allocate for our maximum TCP header + link-level */ 22871 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22872 tcps->tcps_wroff_xtra, BPRI_MED); 22873 if (!mp1) 22874 return (NULL); 22875 data_length = 0; 22876 22877 /* 22878 * Note that tcp_mss has been adjusted to take into account the 22879 * timestamp option if applicable. Because SACK options do not 22880 * appear in every TCP segments and they are of variable lengths, 22881 * they cannot be included in tcp_mss. Thus we need to calculate 22882 * the actual segment length when we need to send a segment which 22883 * includes SACK options. 22884 */ 22885 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22886 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22887 tcp->tcp_num_sack_blk); 22888 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22889 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22890 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22891 max_to_send -= sack_opt_len; 22892 } 22893 22894 if (offset != NULL) { 22895 off = *offset; 22896 /* We use offset as an indicator that end_mp is not NULL. */ 22897 *end_mp = NULL; 22898 } 22899 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22900 /* This could be faster with cooperation from downstream */ 22901 if (mp2 != mp1 && !sendall && 22902 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22903 max_to_send) 22904 /* 22905 * Don't send the next mblk since the whole mblk 22906 * does not fit. 22907 */ 22908 break; 22909 mp2->b_cont = dupb(mp); 22910 mp2 = mp2->b_cont; 22911 if (!mp2) { 22912 freemsg(mp1); 22913 return (NULL); 22914 } 22915 mp2->b_rptr += off; 22916 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22917 (uintptr_t)INT_MAX); 22918 22919 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22920 if (data_length > max_to_send) { 22921 mp2->b_wptr -= data_length - max_to_send; 22922 data_length = max_to_send; 22923 off = mp2->b_wptr - mp->b_rptr; 22924 break; 22925 } else { 22926 off = 0; 22927 } 22928 } 22929 if (offset != NULL) { 22930 *offset = off; 22931 *end_mp = mp; 22932 } 22933 if (seg_len != NULL) { 22934 *seg_len = data_length; 22935 } 22936 22937 /* Update the latest receive window size in TCP header. */ 22938 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22939 tcp->tcp_tcph->th_win); 22940 22941 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22942 mp1->b_rptr = rptr; 22943 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22944 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22945 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22946 U32_TO_ABE32(seq, tcph->th_seq); 22947 22948 /* 22949 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22950 * that this function was called from tcp_wput_data. Thus, when called 22951 * to retransmit data the setting of the PUSH bit may appear some 22952 * what random in that it might get set when it should not. This 22953 * should not pose any performance issues. 22954 */ 22955 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22956 tcp->tcp_unsent == data_length)) { 22957 flags = TH_ACK | TH_PUSH; 22958 } else { 22959 flags = TH_ACK; 22960 } 22961 22962 if (tcp->tcp_ecn_ok) { 22963 if (tcp->tcp_ecn_echo_on) 22964 flags |= TH_ECE; 22965 22966 /* 22967 * Only set ECT bit and ECN_CWR if a segment contains new data. 22968 * There is no TCP flow control for non-data segments, and 22969 * only data segment is transmitted reliably. 22970 */ 22971 if (data_length > 0 && !rexmit) { 22972 SET_ECT(tcp, rptr); 22973 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 22974 flags |= TH_CWR; 22975 tcp->tcp_ecn_cwr_sent = B_TRUE; 22976 } 22977 } 22978 } 22979 22980 if (tcp->tcp_valid_bits) { 22981 uint32_t u1; 22982 22983 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 22984 seq == tcp->tcp_iss) { 22985 uchar_t *wptr; 22986 22987 /* 22988 * If TCP_ISS_VALID and the seq number is tcp_iss, 22989 * TCP can only be in SYN-SENT, SYN-RCVD or 22990 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 22991 * our SYN is not ack'ed but the app closes this 22992 * TCP connection. 22993 */ 22994 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 22995 tcp->tcp_state == TCPS_SYN_RCVD || 22996 tcp->tcp_state == TCPS_FIN_WAIT_1); 22997 22998 /* 22999 * Tack on the MSS option. It is always needed 23000 * for both active and passive open. 23001 * 23002 * MSS option value should be interface MTU - MIN 23003 * TCP/IP header according to RFC 793 as it means 23004 * the maximum segment size TCP can receive. But 23005 * to get around some broken middle boxes/end hosts 23006 * out there, we allow the option value to be the 23007 * same as the MSS option size on the peer side. 23008 * In this way, the other side will not send 23009 * anything larger than they can receive. 23010 * 23011 * Note that for SYN_SENT state, the ndd param 23012 * tcp_use_smss_as_mss_opt has no effect as we 23013 * don't know the peer's MSS option value. So 23014 * the only case we need to take care of is in 23015 * SYN_RCVD state, which is done later. 23016 */ 23017 wptr = mp1->b_wptr; 23018 wptr[0] = TCPOPT_MAXSEG; 23019 wptr[1] = TCPOPT_MAXSEG_LEN; 23020 wptr += 2; 23021 u1 = tcp->tcp_if_mtu - 23022 (tcp->tcp_ipversion == IPV4_VERSION ? 23023 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23024 TCP_MIN_HEADER_LENGTH; 23025 U16_TO_BE16(u1, wptr); 23026 mp1->b_wptr = wptr + 2; 23027 /* Update the offset to cover the additional word */ 23028 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23029 23030 /* 23031 * Note that the following way of filling in 23032 * TCP options are not optimal. Some NOPs can 23033 * be saved. But there is no need at this time 23034 * to optimize it. When it is needed, we will 23035 * do it. 23036 */ 23037 switch (tcp->tcp_state) { 23038 case TCPS_SYN_SENT: 23039 flags = TH_SYN; 23040 23041 if (tcp->tcp_snd_ts_ok) { 23042 uint32_t llbolt = (uint32_t)lbolt; 23043 23044 wptr = mp1->b_wptr; 23045 wptr[0] = TCPOPT_NOP; 23046 wptr[1] = TCPOPT_NOP; 23047 wptr[2] = TCPOPT_TSTAMP; 23048 wptr[3] = TCPOPT_TSTAMP_LEN; 23049 wptr += 4; 23050 U32_TO_BE32(llbolt, wptr); 23051 wptr += 4; 23052 ASSERT(tcp->tcp_ts_recent == 0); 23053 U32_TO_BE32(0L, wptr); 23054 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23055 tcph->th_offset_and_rsrvd[0] += 23056 (3 << 4); 23057 } 23058 23059 /* 23060 * Set up all the bits to tell other side 23061 * we are ECN capable. 23062 */ 23063 if (tcp->tcp_ecn_ok) { 23064 flags |= (TH_ECE | TH_CWR); 23065 } 23066 break; 23067 case TCPS_SYN_RCVD: 23068 flags |= TH_SYN; 23069 23070 /* 23071 * Reset the MSS option value to be SMSS 23072 * We should probably add back the bytes 23073 * for timestamp option and IPsec. We 23074 * don't do that as this is a workaround 23075 * for broken middle boxes/end hosts, it 23076 * is better for us to be more cautious. 23077 * They may not take these things into 23078 * account in their SMSS calculation. Thus 23079 * the peer's calculated SMSS may be smaller 23080 * than what it can be. This should be OK. 23081 */ 23082 if (tcps->tcps_use_smss_as_mss_opt) { 23083 u1 = tcp->tcp_mss; 23084 U16_TO_BE16(u1, wptr); 23085 } 23086 23087 /* 23088 * If the other side is ECN capable, reply 23089 * that we are also ECN capable. 23090 */ 23091 if (tcp->tcp_ecn_ok) 23092 flags |= TH_ECE; 23093 break; 23094 default: 23095 /* 23096 * The above ASSERT() makes sure that this 23097 * must be FIN-WAIT-1 state. Our SYN has 23098 * not been ack'ed so retransmit it. 23099 */ 23100 flags |= TH_SYN; 23101 break; 23102 } 23103 23104 if (tcp->tcp_snd_ws_ok) { 23105 wptr = mp1->b_wptr; 23106 wptr[0] = TCPOPT_NOP; 23107 wptr[1] = TCPOPT_WSCALE; 23108 wptr[2] = TCPOPT_WS_LEN; 23109 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23110 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23111 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23112 } 23113 23114 if (tcp->tcp_snd_sack_ok) { 23115 wptr = mp1->b_wptr; 23116 wptr[0] = TCPOPT_NOP; 23117 wptr[1] = TCPOPT_NOP; 23118 wptr[2] = TCPOPT_SACK_PERMITTED; 23119 wptr[3] = TCPOPT_SACK_OK_LEN; 23120 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23121 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23122 } 23123 23124 /* allocb() of adequate mblk assures space */ 23125 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23126 (uintptr_t)INT_MAX); 23127 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23128 /* 23129 * Get IP set to checksum on our behalf 23130 * Include the adjustment for a source route if any. 23131 */ 23132 u1 += tcp->tcp_sum; 23133 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23134 U16_TO_BE16(u1, tcph->th_sum); 23135 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23136 } 23137 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23138 (seq + data_length) == tcp->tcp_fss) { 23139 if (!tcp->tcp_fin_acked) { 23140 flags |= TH_FIN; 23141 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23142 } 23143 if (!tcp->tcp_fin_sent) { 23144 tcp->tcp_fin_sent = B_TRUE; 23145 switch (tcp->tcp_state) { 23146 case TCPS_SYN_RCVD: 23147 case TCPS_ESTABLISHED: 23148 tcp->tcp_state = TCPS_FIN_WAIT_1; 23149 break; 23150 case TCPS_CLOSE_WAIT: 23151 tcp->tcp_state = TCPS_LAST_ACK; 23152 break; 23153 } 23154 if (tcp->tcp_suna == tcp->tcp_snxt) 23155 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23156 tcp->tcp_snxt = tcp->tcp_fss + 1; 23157 } 23158 } 23159 /* 23160 * Note the trick here. u1 is unsigned. When tcp_urg 23161 * is smaller than seq, u1 will become a very huge value. 23162 * So the comparison will fail. Also note that tcp_urp 23163 * should be positive, see RFC 793 page 17. 23164 */ 23165 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23166 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23167 u1 < (uint32_t)(64 * 1024)) { 23168 flags |= TH_URG; 23169 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23170 U32_TO_ABE16(u1, tcph->th_urp); 23171 } 23172 } 23173 tcph->th_flags[0] = (uchar_t)flags; 23174 tcp->tcp_rack = tcp->tcp_rnxt; 23175 tcp->tcp_rack_cnt = 0; 23176 23177 if (tcp->tcp_snd_ts_ok) { 23178 if (tcp->tcp_state != TCPS_SYN_SENT) { 23179 uint32_t llbolt = (uint32_t)lbolt; 23180 23181 U32_TO_BE32(llbolt, 23182 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23183 U32_TO_BE32(tcp->tcp_ts_recent, 23184 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23185 } 23186 } 23187 23188 if (num_sack_blk > 0) { 23189 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23190 sack_blk_t *tmp; 23191 int32_t i; 23192 23193 wptr[0] = TCPOPT_NOP; 23194 wptr[1] = TCPOPT_NOP; 23195 wptr[2] = TCPOPT_SACK; 23196 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23197 sizeof (sack_blk_t); 23198 wptr += TCPOPT_REAL_SACK_LEN; 23199 23200 tmp = tcp->tcp_sack_list; 23201 for (i = 0; i < num_sack_blk; i++) { 23202 U32_TO_BE32(tmp[i].begin, wptr); 23203 wptr += sizeof (tcp_seq); 23204 U32_TO_BE32(tmp[i].end, wptr); 23205 wptr += sizeof (tcp_seq); 23206 } 23207 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23208 } 23209 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23210 data_length += (int)(mp1->b_wptr - rptr); 23211 if (tcp->tcp_ipversion == IPV4_VERSION) { 23212 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23213 } else { 23214 ip6_t *ip6 = (ip6_t *)(rptr + 23215 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23216 sizeof (ip6i_t) : 0)); 23217 23218 ip6->ip6_plen = htons(data_length - 23219 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23220 } 23221 23222 /* 23223 * Prime pump for IP 23224 * Include the adjustment for a source route if any. 23225 */ 23226 data_length -= tcp->tcp_ip_hdr_len; 23227 data_length += tcp->tcp_sum; 23228 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23229 U16_TO_ABE16(data_length, tcph->th_sum); 23230 if (tcp->tcp_ip_forward_progress) { 23231 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23232 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23233 tcp->tcp_ip_forward_progress = B_FALSE; 23234 } 23235 return (mp1); 23236 } 23237 23238 /* This function handles the push timeout. */ 23239 void 23240 tcp_push_timer(void *arg) 23241 { 23242 conn_t *connp = (conn_t *)arg; 23243 tcp_t *tcp = connp->conn_tcp; 23244 tcp_stack_t *tcps = tcp->tcp_tcps; 23245 23246 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23247 23248 ASSERT(tcp->tcp_listener == NULL); 23249 23250 /* 23251 * We need to plug synchronous streams during our drain to prevent 23252 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23253 */ 23254 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23255 tcp->tcp_push_tid = 0; 23256 if ((tcp->tcp_rcv_list != NULL) && 23257 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 23258 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23259 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23260 } 23261 23262 /* 23263 * This function handles delayed ACK timeout. 23264 */ 23265 static void 23266 tcp_ack_timer(void *arg) 23267 { 23268 conn_t *connp = (conn_t *)arg; 23269 tcp_t *tcp = connp->conn_tcp; 23270 mblk_t *mp; 23271 tcp_stack_t *tcps = tcp->tcp_tcps; 23272 23273 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23274 23275 tcp->tcp_ack_tid = 0; 23276 23277 if (tcp->tcp_fused) 23278 return; 23279 23280 /* 23281 * Do not send ACK if there is no outstanding unack'ed data. 23282 */ 23283 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23284 return; 23285 } 23286 23287 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23288 /* 23289 * Make sure we don't allow deferred ACKs to result in 23290 * timer-based ACKing. If we have held off an ACK 23291 * when there was more than an mss here, and the timer 23292 * goes off, we have to worry about the possibility 23293 * that the sender isn't doing slow-start, or is out 23294 * of step with us for some other reason. We fall 23295 * permanently back in the direction of 23296 * ACK-every-other-packet as suggested in RFC 1122. 23297 */ 23298 if (tcp->tcp_rack_abs_max > 2) 23299 tcp->tcp_rack_abs_max--; 23300 tcp->tcp_rack_cur_max = 2; 23301 } 23302 mp = tcp_ack_mp(tcp); 23303 23304 if (mp != NULL) { 23305 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 23306 BUMP_LOCAL(tcp->tcp_obsegs); 23307 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23308 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23309 tcp_send_data(tcp, tcp->tcp_wq, mp); 23310 } 23311 } 23312 23313 23314 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23315 static mblk_t * 23316 tcp_ack_mp(tcp_t *tcp) 23317 { 23318 uint32_t seq_no; 23319 tcp_stack_t *tcps = tcp->tcp_tcps; 23320 23321 /* 23322 * There are a few cases to be considered while setting the sequence no. 23323 * Essentially, we can come here while processing an unacceptable pkt 23324 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23325 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23326 * If we are here for a zero window probe, stick with suna. In all 23327 * other cases, we check if suna + swnd encompasses snxt and set 23328 * the sequence number to snxt, if so. If snxt falls outside the 23329 * window (the receiver probably shrunk its window), we will go with 23330 * suna + swnd, otherwise the sequence no will be unacceptable to the 23331 * receiver. 23332 */ 23333 if (tcp->tcp_zero_win_probe) { 23334 seq_no = tcp->tcp_suna; 23335 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23336 ASSERT(tcp->tcp_swnd == 0); 23337 seq_no = tcp->tcp_snxt; 23338 } else { 23339 seq_no = SEQ_GT(tcp->tcp_snxt, 23340 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23341 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23342 } 23343 23344 if (tcp->tcp_valid_bits) { 23345 /* 23346 * For the complex case where we have to send some 23347 * controls (FIN or SYN), let tcp_xmit_mp do it. 23348 */ 23349 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23350 NULL, B_FALSE)); 23351 } else { 23352 /* Generate a simple ACK */ 23353 int data_length; 23354 uchar_t *rptr; 23355 tcph_t *tcph; 23356 mblk_t *mp1; 23357 int32_t tcp_hdr_len; 23358 int32_t tcp_tcp_hdr_len; 23359 int32_t num_sack_blk = 0; 23360 int32_t sack_opt_len; 23361 23362 /* 23363 * Allocate space for TCP + IP headers 23364 * and link-level header 23365 */ 23366 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23367 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23368 tcp->tcp_num_sack_blk); 23369 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23370 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23371 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23372 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23373 } else { 23374 tcp_hdr_len = tcp->tcp_hdr_len; 23375 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23376 } 23377 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23378 if (!mp1) 23379 return (NULL); 23380 23381 /* Update the latest receive window size in TCP header. */ 23382 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23383 tcp->tcp_tcph->th_win); 23384 /* copy in prototype TCP + IP header */ 23385 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23386 mp1->b_rptr = rptr; 23387 mp1->b_wptr = rptr + tcp_hdr_len; 23388 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23389 23390 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23391 23392 /* Set the TCP sequence number. */ 23393 U32_TO_ABE32(seq_no, tcph->th_seq); 23394 23395 /* Set up the TCP flag field. */ 23396 tcph->th_flags[0] = (uchar_t)TH_ACK; 23397 if (tcp->tcp_ecn_echo_on) 23398 tcph->th_flags[0] |= TH_ECE; 23399 23400 tcp->tcp_rack = tcp->tcp_rnxt; 23401 tcp->tcp_rack_cnt = 0; 23402 23403 /* fill in timestamp option if in use */ 23404 if (tcp->tcp_snd_ts_ok) { 23405 uint32_t llbolt = (uint32_t)lbolt; 23406 23407 U32_TO_BE32(llbolt, 23408 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23409 U32_TO_BE32(tcp->tcp_ts_recent, 23410 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23411 } 23412 23413 /* Fill in SACK options */ 23414 if (num_sack_blk > 0) { 23415 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23416 sack_blk_t *tmp; 23417 int32_t i; 23418 23419 wptr[0] = TCPOPT_NOP; 23420 wptr[1] = TCPOPT_NOP; 23421 wptr[2] = TCPOPT_SACK; 23422 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23423 sizeof (sack_blk_t); 23424 wptr += TCPOPT_REAL_SACK_LEN; 23425 23426 tmp = tcp->tcp_sack_list; 23427 for (i = 0; i < num_sack_blk; i++) { 23428 U32_TO_BE32(tmp[i].begin, wptr); 23429 wptr += sizeof (tcp_seq); 23430 U32_TO_BE32(tmp[i].end, wptr); 23431 wptr += sizeof (tcp_seq); 23432 } 23433 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23434 << 4); 23435 } 23436 23437 if (tcp->tcp_ipversion == IPV4_VERSION) { 23438 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23439 } else { 23440 /* Check for ip6i_t header in sticky hdrs */ 23441 ip6_t *ip6 = (ip6_t *)(rptr + 23442 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23443 sizeof (ip6i_t) : 0)); 23444 23445 ip6->ip6_plen = htons(tcp_hdr_len - 23446 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23447 } 23448 23449 /* 23450 * Prime pump for checksum calculation in IP. Include the 23451 * adjustment for a source route if any. 23452 */ 23453 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23454 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23455 U16_TO_ABE16(data_length, tcph->th_sum); 23456 23457 if (tcp->tcp_ip_forward_progress) { 23458 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23459 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23460 tcp->tcp_ip_forward_progress = B_FALSE; 23461 } 23462 return (mp1); 23463 } 23464 } 23465 23466 /* 23467 * To create a temporary tcp structure for inserting into bind hash list. 23468 * The parameter is assumed to be in network byte order, ready for use. 23469 */ 23470 /* ARGSUSED */ 23471 static tcp_t * 23472 tcp_alloc_temp_tcp(in_port_t port, tcp_stack_t *tcps) 23473 { 23474 conn_t *connp; 23475 tcp_t *tcp; 23476 23477 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP, tcps->tcps_netstack); 23478 if (connp == NULL) 23479 return (NULL); 23480 23481 tcp = connp->conn_tcp; 23482 tcp->tcp_tcps = tcps; 23483 TCPS_REFHOLD(tcps); 23484 23485 /* 23486 * Only initialize the necessary info in those structures. Note 23487 * that since INADDR_ANY is all 0, we do not need to set 23488 * tcp_bound_source to INADDR_ANY here. 23489 */ 23490 tcp->tcp_state = TCPS_BOUND; 23491 tcp->tcp_lport = port; 23492 tcp->tcp_exclbind = 1; 23493 tcp->tcp_reserved_port = 1; 23494 23495 /* Just for place holding... */ 23496 tcp->tcp_ipversion = IPV4_VERSION; 23497 23498 return (tcp); 23499 } 23500 23501 /* 23502 * To remove a port range specified by lo_port and hi_port from the 23503 * reserved port ranges. This is one of the three public functions of 23504 * the reserved port interface. Note that a port range has to be removed 23505 * as a whole. Ports in a range cannot be removed individually. 23506 * 23507 * Params: 23508 * in_port_t lo_port: the beginning port of the reserved port range to 23509 * be deleted. 23510 * in_port_t hi_port: the ending port of the reserved port range to 23511 * be deleted. 23512 * 23513 * Return: 23514 * B_TRUE if the deletion is successful, B_FALSE otherwise. 23515 * 23516 * Assumes that nca is only for zoneid=0 23517 */ 23518 boolean_t 23519 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 23520 { 23521 int i, j; 23522 int size; 23523 tcp_t **temp_tcp_array; 23524 tcp_t *tcp; 23525 tcp_stack_t *tcps; 23526 23527 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23528 ASSERT(tcps != NULL); 23529 23530 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23531 23532 /* First make sure that the port ranage is indeed reserved. */ 23533 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23534 if (tcps->tcps_reserved_port[i].lo_port == lo_port) { 23535 hi_port = tcps->tcps_reserved_port[i].hi_port; 23536 temp_tcp_array = 23537 tcps->tcps_reserved_port[i].temp_tcp_array; 23538 break; 23539 } 23540 } 23541 if (i == tcps->tcps_reserved_port_array_size) { 23542 rw_exit(&tcps->tcps_reserved_port_lock); 23543 netstack_rele(tcps->tcps_netstack); 23544 return (B_FALSE); 23545 } 23546 23547 /* 23548 * Remove the range from the array. This simple loop is possible 23549 * because port ranges are inserted in ascending order. 23550 */ 23551 for (j = i; j < tcps->tcps_reserved_port_array_size - 1; j++) { 23552 tcps->tcps_reserved_port[j].lo_port = 23553 tcps->tcps_reserved_port[j+1].lo_port; 23554 tcps->tcps_reserved_port[j].hi_port = 23555 tcps->tcps_reserved_port[j+1].hi_port; 23556 tcps->tcps_reserved_port[j].temp_tcp_array = 23557 tcps->tcps_reserved_port[j+1].temp_tcp_array; 23558 } 23559 23560 /* Remove all the temporary tcp structures. */ 23561 size = hi_port - lo_port + 1; 23562 while (size > 0) { 23563 tcp = temp_tcp_array[size - 1]; 23564 ASSERT(tcp != NULL); 23565 tcp_bind_hash_remove(tcp); 23566 CONN_DEC_REF(tcp->tcp_connp); 23567 size--; 23568 } 23569 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 23570 tcps->tcps_reserved_port_array_size--; 23571 rw_exit(&tcps->tcps_reserved_port_lock); 23572 netstack_rele(tcps->tcps_netstack); 23573 return (B_TRUE); 23574 } 23575 23576 /* 23577 * Macro to remove temporary tcp structure from the bind hash list. The 23578 * first parameter is the list of tcp to be removed. The second parameter 23579 * is the number of tcps in the array. 23580 */ 23581 #define TCP_TMP_TCP_REMOVE(tcp_array, num, tcps) \ 23582 { \ 23583 while ((num) > 0) { \ 23584 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 23585 tf_t *tbf; \ 23586 tcp_t *tcpnext; \ 23587 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 23588 mutex_enter(&tbf->tf_lock); \ 23589 tcpnext = tcp->tcp_bind_hash; \ 23590 if (tcpnext) { \ 23591 tcpnext->tcp_ptpbhn = \ 23592 tcp->tcp_ptpbhn; \ 23593 } \ 23594 *tcp->tcp_ptpbhn = tcpnext; \ 23595 mutex_exit(&tbf->tf_lock); \ 23596 kmem_free(tcp, sizeof (tcp_t)); \ 23597 (tcp_array)[(num) - 1] = NULL; \ 23598 (num)--; \ 23599 } \ 23600 } 23601 23602 /* 23603 * The public interface for other modules to call to reserve a port range 23604 * in TCP. The caller passes in how large a port range it wants. TCP 23605 * will try to find a range and return it via lo_port and hi_port. This is 23606 * used by NCA's nca_conn_init. 23607 * NCA can only be used in the global zone so this only affects the global 23608 * zone's ports. 23609 * 23610 * Params: 23611 * int size: the size of the port range to be reserved. 23612 * in_port_t *lo_port (referenced): returns the beginning port of the 23613 * reserved port range added. 23614 * in_port_t *hi_port (referenced): returns the ending port of the 23615 * reserved port range added. 23616 * 23617 * Return: 23618 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 23619 * 23620 * Assumes that nca is only for zoneid=0 23621 */ 23622 boolean_t 23623 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 23624 { 23625 tcp_t *tcp; 23626 tcp_t *tmp_tcp; 23627 tcp_t **temp_tcp_array; 23628 tf_t *tbf; 23629 in_port_t net_port; 23630 in_port_t port; 23631 int32_t cur_size; 23632 int i, j; 23633 boolean_t used; 23634 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 23635 zoneid_t zoneid = GLOBAL_ZONEID; 23636 tcp_stack_t *tcps; 23637 23638 /* Sanity check. */ 23639 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 23640 return (B_FALSE); 23641 } 23642 23643 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 23644 ASSERT(tcps != NULL); 23645 23646 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 23647 if (tcps->tcps_reserved_port_array_size == 23648 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 23649 rw_exit(&tcps->tcps_reserved_port_lock); 23650 netstack_rele(tcps->tcps_netstack); 23651 return (B_FALSE); 23652 } 23653 23654 /* 23655 * Find the starting port to try. Since the port ranges are ordered 23656 * in the reserved port array, we can do a simple search here. 23657 */ 23658 *lo_port = TCP_SMALLEST_RESERVED_PORT; 23659 *hi_port = TCP_LARGEST_RESERVED_PORT; 23660 for (i = 0; i < tcps->tcps_reserved_port_array_size; 23661 *lo_port = tcps->tcps_reserved_port[i].hi_port + 1, i++) { 23662 if (tcps->tcps_reserved_port[i].lo_port - *lo_port >= size) { 23663 *hi_port = tcps->tcps_reserved_port[i].lo_port - 1; 23664 break; 23665 } 23666 } 23667 /* No available port range. */ 23668 if (i == tcps->tcps_reserved_port_array_size && 23669 *hi_port - *lo_port < size) { 23670 rw_exit(&tcps->tcps_reserved_port_lock); 23671 netstack_rele(tcps->tcps_netstack); 23672 return (B_FALSE); 23673 } 23674 23675 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 23676 if (temp_tcp_array == NULL) { 23677 rw_exit(&tcps->tcps_reserved_port_lock); 23678 netstack_rele(tcps->tcps_netstack); 23679 return (B_FALSE); 23680 } 23681 23682 /* Go thru the port range to see if some ports are already bound. */ 23683 for (port = *lo_port, cur_size = 0; 23684 cur_size < size && port <= *hi_port; 23685 cur_size++, port++) { 23686 used = B_FALSE; 23687 net_port = htons(port); 23688 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(net_port)]; 23689 mutex_enter(&tbf->tf_lock); 23690 for (tcp = tbf->tf_tcp; tcp != NULL; 23691 tcp = tcp->tcp_bind_hash) { 23692 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 23693 net_port == tcp->tcp_lport) { 23694 /* 23695 * A port is already bound. Search again 23696 * starting from port + 1. Release all 23697 * temporary tcps. 23698 */ 23699 mutex_exit(&tbf->tf_lock); 23700 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23701 tcps); 23702 *lo_port = port + 1; 23703 cur_size = -1; 23704 used = B_TRUE; 23705 break; 23706 } 23707 } 23708 if (!used) { 23709 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port, tcps)) == 23710 NULL) { 23711 /* 23712 * Allocation failure. Just fail the request. 23713 * Need to remove all those temporary tcp 23714 * structures. 23715 */ 23716 mutex_exit(&tbf->tf_lock); 23717 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 23718 tcps); 23719 rw_exit(&tcps->tcps_reserved_port_lock); 23720 kmem_free(temp_tcp_array, 23721 (hi_port - lo_port + 1) * 23722 sizeof (tcp_t *)); 23723 netstack_rele(tcps->tcps_netstack); 23724 return (B_FALSE); 23725 } 23726 temp_tcp_array[cur_size] = tmp_tcp; 23727 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 23728 mutex_exit(&tbf->tf_lock); 23729 } 23730 } 23731 23732 /* 23733 * The current range is not large enough. We can actually do another 23734 * search if this search is done between 2 reserved port ranges. But 23735 * for first release, we just stop here and return saying that no port 23736 * range is available. 23737 */ 23738 if (cur_size < size) { 23739 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, tcps); 23740 rw_exit(&tcps->tcps_reserved_port_lock); 23741 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 23742 netstack_rele(tcps->tcps_netstack); 23743 return (B_FALSE); 23744 } 23745 *hi_port = port - 1; 23746 23747 /* 23748 * Insert range into array in ascending order. Since this function 23749 * must not be called often, we choose to use the simplest method. 23750 * The above array should not consume excessive stack space as 23751 * the size must be very small. If in future releases, we find 23752 * that we should provide more reserved port ranges, this function 23753 * has to be modified to be more efficient. 23754 */ 23755 if (tcps->tcps_reserved_port_array_size == 0) { 23756 tcps->tcps_reserved_port[0].lo_port = *lo_port; 23757 tcps->tcps_reserved_port[0].hi_port = *hi_port; 23758 tcps->tcps_reserved_port[0].temp_tcp_array = temp_tcp_array; 23759 } else { 23760 for (i = 0, j = 0; i < tcps->tcps_reserved_port_array_size; 23761 i++, j++) { 23762 if (*lo_port < tcps->tcps_reserved_port[i].lo_port && 23763 i == j) { 23764 tmp_ports[j].lo_port = *lo_port; 23765 tmp_ports[j].hi_port = *hi_port; 23766 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23767 j++; 23768 } 23769 tmp_ports[j].lo_port = 23770 tcps->tcps_reserved_port[i].lo_port; 23771 tmp_ports[j].hi_port = 23772 tcps->tcps_reserved_port[i].hi_port; 23773 tmp_ports[j].temp_tcp_array = 23774 tcps->tcps_reserved_port[i].temp_tcp_array; 23775 } 23776 if (j == i) { 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 } 23781 bcopy(tmp_ports, tcps->tcps_reserved_port, sizeof (tmp_ports)); 23782 } 23783 tcps->tcps_reserved_port_array_size++; 23784 rw_exit(&tcps->tcps_reserved_port_lock); 23785 netstack_rele(tcps->tcps_netstack); 23786 return (B_TRUE); 23787 } 23788 23789 /* 23790 * Check to see if a port is in any reserved port range. 23791 * 23792 * Params: 23793 * in_port_t port: the port to be verified. 23794 * 23795 * Return: 23796 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 23797 */ 23798 boolean_t 23799 tcp_reserved_port_check(in_port_t port, tcp_stack_t *tcps) 23800 { 23801 int i; 23802 23803 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23804 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23805 if (port >= tcps->tcps_reserved_port[i].lo_port || 23806 port <= tcps->tcps_reserved_port[i].hi_port) { 23807 rw_exit(&tcps->tcps_reserved_port_lock); 23808 return (B_TRUE); 23809 } 23810 } 23811 rw_exit(&tcps->tcps_reserved_port_lock); 23812 return (B_FALSE); 23813 } 23814 23815 /* 23816 * To list all reserved port ranges. This is the function to handle 23817 * ndd tcp_reserved_port_list. 23818 */ 23819 /* ARGSUSED */ 23820 static int 23821 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23822 { 23823 int i; 23824 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23825 23826 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 23827 if (tcps->tcps_reserved_port_array_size > 0) 23828 (void) mi_mpprintf(mp, "The following ports are reserved:"); 23829 else 23830 (void) mi_mpprintf(mp, "No port is reserved."); 23831 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 23832 (void) mi_mpprintf(mp, "%d-%d", 23833 tcps->tcps_reserved_port[i].lo_port, 23834 tcps->tcps_reserved_port[i].hi_port); 23835 } 23836 rw_exit(&tcps->tcps_reserved_port_lock); 23837 return (0); 23838 } 23839 23840 /* 23841 * Hash list insertion routine for tcp_t structures. 23842 * Inserts entries with the ones bound to a specific IP address first 23843 * followed by those bound to INADDR_ANY. 23844 */ 23845 static void 23846 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23847 { 23848 tcp_t **tcpp; 23849 tcp_t *tcpnext; 23850 23851 if (tcp->tcp_ptpbhn != NULL) { 23852 ASSERT(!caller_holds_lock); 23853 tcp_bind_hash_remove(tcp); 23854 } 23855 tcpp = &tbf->tf_tcp; 23856 if (!caller_holds_lock) { 23857 mutex_enter(&tbf->tf_lock); 23858 } else { 23859 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23860 } 23861 tcpnext = tcpp[0]; 23862 if (tcpnext) { 23863 /* 23864 * If the new tcp bound to the INADDR_ANY address 23865 * and the first one in the list is not bound to 23866 * INADDR_ANY we skip all entries until we find the 23867 * first one bound to INADDR_ANY. 23868 * This makes sure that applications binding to a 23869 * specific address get preference over those binding to 23870 * INADDR_ANY. 23871 */ 23872 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23873 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23874 while ((tcpnext = tcpp[0]) != NULL && 23875 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23876 tcpp = &(tcpnext->tcp_bind_hash); 23877 if (tcpnext) 23878 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23879 } else 23880 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23881 } 23882 tcp->tcp_bind_hash = tcpnext; 23883 tcp->tcp_ptpbhn = tcpp; 23884 tcpp[0] = tcp; 23885 if (!caller_holds_lock) 23886 mutex_exit(&tbf->tf_lock); 23887 } 23888 23889 /* 23890 * Hash list removal routine for tcp_t structures. 23891 */ 23892 static void 23893 tcp_bind_hash_remove(tcp_t *tcp) 23894 { 23895 tcp_t *tcpnext; 23896 kmutex_t *lockp; 23897 tcp_stack_t *tcps = tcp->tcp_tcps; 23898 23899 if (tcp->tcp_ptpbhn == NULL) 23900 return; 23901 23902 /* 23903 * Extract the lock pointer in case there are concurrent 23904 * hash_remove's for this instance. 23905 */ 23906 ASSERT(tcp->tcp_lport != 0); 23907 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23908 23909 ASSERT(lockp != NULL); 23910 mutex_enter(lockp); 23911 if (tcp->tcp_ptpbhn) { 23912 tcpnext = tcp->tcp_bind_hash; 23913 if (tcpnext) { 23914 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23915 tcp->tcp_bind_hash = NULL; 23916 } 23917 *tcp->tcp_ptpbhn = tcpnext; 23918 tcp->tcp_ptpbhn = NULL; 23919 } 23920 mutex_exit(lockp); 23921 } 23922 23923 23924 /* 23925 * Hash list lookup routine for tcp_t structures. 23926 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23927 */ 23928 static tcp_t * 23929 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23930 { 23931 tf_t *tf; 23932 tcp_t *tcp; 23933 23934 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23935 mutex_enter(&tf->tf_lock); 23936 for (tcp = tf->tf_tcp; tcp != NULL; 23937 tcp = tcp->tcp_acceptor_hash) { 23938 if (tcp->tcp_acceptor_id == id) { 23939 CONN_INC_REF(tcp->tcp_connp); 23940 mutex_exit(&tf->tf_lock); 23941 return (tcp); 23942 } 23943 } 23944 mutex_exit(&tf->tf_lock); 23945 return (NULL); 23946 } 23947 23948 23949 /* 23950 * Hash list insertion routine for tcp_t structures. 23951 */ 23952 void 23953 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23954 { 23955 tf_t *tf; 23956 tcp_t **tcpp; 23957 tcp_t *tcpnext; 23958 tcp_stack_t *tcps = tcp->tcp_tcps; 23959 23960 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23961 23962 if (tcp->tcp_ptpahn != NULL) 23963 tcp_acceptor_hash_remove(tcp); 23964 tcpp = &tf->tf_tcp; 23965 mutex_enter(&tf->tf_lock); 23966 tcpnext = tcpp[0]; 23967 if (tcpnext) 23968 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23969 tcp->tcp_acceptor_hash = tcpnext; 23970 tcp->tcp_ptpahn = tcpp; 23971 tcpp[0] = tcp; 23972 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23973 mutex_exit(&tf->tf_lock); 23974 } 23975 23976 /* 23977 * Hash list removal routine for tcp_t structures. 23978 */ 23979 static void 23980 tcp_acceptor_hash_remove(tcp_t *tcp) 23981 { 23982 tcp_t *tcpnext; 23983 kmutex_t *lockp; 23984 23985 /* 23986 * Extract the lock pointer in case there are concurrent 23987 * hash_remove's for this instance. 23988 */ 23989 lockp = tcp->tcp_acceptor_lockp; 23990 23991 if (tcp->tcp_ptpahn == NULL) 23992 return; 23993 23994 ASSERT(lockp != NULL); 23995 mutex_enter(lockp); 23996 if (tcp->tcp_ptpahn) { 23997 tcpnext = tcp->tcp_acceptor_hash; 23998 if (tcpnext) { 23999 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24000 tcp->tcp_acceptor_hash = NULL; 24001 } 24002 *tcp->tcp_ptpahn = tcpnext; 24003 tcp->tcp_ptpahn = NULL; 24004 } 24005 mutex_exit(lockp); 24006 tcp->tcp_acceptor_lockp = NULL; 24007 } 24008 24009 /* ARGSUSED */ 24010 static int 24011 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 24012 { 24013 int error = 0; 24014 int retval; 24015 char *end; 24016 tcp_hsp_t *hsp; 24017 tcp_hsp_t *hspprev; 24018 ipaddr_t addr = 0; /* Address we're looking for */ 24019 in6_addr_t v6addr; /* Address we're looking for */ 24020 uint32_t hash; /* Hash of that address */ 24021 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24022 24023 /* 24024 * If the following variables are still zero after parsing the input 24025 * string, the user didn't specify them and we don't change them in 24026 * the HSP. 24027 */ 24028 24029 ipaddr_t mask = 0; /* Subnet mask */ 24030 in6_addr_t v6mask; 24031 long sendspace = 0; /* Send buffer size */ 24032 long recvspace = 0; /* Receive buffer size */ 24033 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 24034 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 24035 24036 rw_enter(&tcps->tcps_hsp_lock, RW_WRITER); 24037 24038 /* Parse and validate address */ 24039 if (af == AF_INET) { 24040 retval = inet_pton(af, value, &addr); 24041 if (retval == 1) 24042 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 24043 } else if (af == AF_INET6) { 24044 retval = inet_pton(af, value, &v6addr); 24045 } else { 24046 error = EINVAL; 24047 goto done; 24048 } 24049 if (retval == 0) { 24050 error = EINVAL; 24051 goto done; 24052 } 24053 24054 while ((*value) && *value != ' ') 24055 value++; 24056 24057 /* Parse individual keywords, set variables if found */ 24058 while (*value) { 24059 /* Skip leading blanks */ 24060 24061 while (*value == ' ' || *value == '\t') 24062 value++; 24063 24064 /* If at end of string, we're done */ 24065 24066 if (!*value) 24067 break; 24068 24069 /* We have a word, figure out what it is */ 24070 24071 if (strncmp("mask", value, 4) == 0) { 24072 value += 4; 24073 while (*value == ' ' || *value == '\t') 24074 value++; 24075 /* Parse subnet mask */ 24076 if (af == AF_INET) { 24077 retval = inet_pton(af, value, &mask); 24078 if (retval == 1) { 24079 V4MASK_TO_V6(mask, v6mask); 24080 } 24081 } else if (af == AF_INET6) { 24082 retval = inet_pton(af, value, &v6mask); 24083 } 24084 if (retval != 1) { 24085 error = EINVAL; 24086 goto done; 24087 } 24088 while ((*value) && *value != ' ') 24089 value++; 24090 } else if (strncmp("sendspace", value, 9) == 0) { 24091 value += 9; 24092 24093 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 24094 sendspace < TCP_XMIT_HIWATER || 24095 sendspace >= (1L<<30)) { 24096 error = EINVAL; 24097 goto done; 24098 } 24099 value = end; 24100 } else if (strncmp("recvspace", value, 9) == 0) { 24101 value += 9; 24102 24103 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 24104 recvspace < TCP_RECV_HIWATER || 24105 recvspace >= (1L<<30)) { 24106 error = EINVAL; 24107 goto done; 24108 } 24109 value = end; 24110 } else if (strncmp("timestamp", value, 9) == 0) { 24111 value += 9; 24112 24113 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 24114 timestamp < 0 || timestamp > 1) { 24115 error = EINVAL; 24116 goto done; 24117 } 24118 24119 /* 24120 * We increment timestamp so we know it's been set; 24121 * this is undone when we put it in the HSP 24122 */ 24123 timestamp++; 24124 value = end; 24125 } else if (strncmp("delete", value, 6) == 0) { 24126 value += 6; 24127 delete = B_TRUE; 24128 } else { 24129 error = EINVAL; 24130 goto done; 24131 } 24132 } 24133 24134 /* Hash address for lookup */ 24135 24136 hash = TCP_HSP_HASH(addr); 24137 24138 if (delete) { 24139 /* 24140 * Note that deletes don't return an error if the thing 24141 * we're trying to delete isn't there. 24142 */ 24143 if (tcps->tcps_hsp_hash == NULL) 24144 goto done; 24145 hsp = tcps->tcps_hsp_hash[hash]; 24146 24147 if (hsp) { 24148 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24149 &v6addr)) { 24150 tcps->tcps_hsp_hash[hash] = hsp->tcp_hsp_next; 24151 mi_free((char *)hsp); 24152 } else { 24153 hspprev = hsp; 24154 while ((hsp = hsp->tcp_hsp_next) != NULL) { 24155 if (IN6_ARE_ADDR_EQUAL( 24156 &hsp->tcp_hsp_addr_v6, &v6addr)) { 24157 hspprev->tcp_hsp_next = 24158 hsp->tcp_hsp_next; 24159 mi_free((char *)hsp); 24160 break; 24161 } 24162 hspprev = hsp; 24163 } 24164 } 24165 } 24166 } else { 24167 /* 24168 * We're adding/modifying an HSP. If we haven't already done 24169 * so, allocate the hash table. 24170 */ 24171 24172 if (!tcps->tcps_hsp_hash) { 24173 tcps->tcps_hsp_hash = (tcp_hsp_t **) 24174 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 24175 if (!tcps->tcps_hsp_hash) { 24176 error = EINVAL; 24177 goto done; 24178 } 24179 } 24180 24181 /* Get head of hash chain */ 24182 24183 hsp = tcps->tcps_hsp_hash[hash]; 24184 24185 /* Try to find pre-existing hsp on hash chain */ 24186 /* Doesn't handle CIDR prefixes. */ 24187 while (hsp) { 24188 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 24189 break; 24190 hsp = hsp->tcp_hsp_next; 24191 } 24192 24193 /* 24194 * If we didn't, create one with default values and put it 24195 * at head of hash chain 24196 */ 24197 24198 if (!hsp) { 24199 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 24200 if (!hsp) { 24201 error = EINVAL; 24202 goto done; 24203 } 24204 hsp->tcp_hsp_next = tcps->tcps_hsp_hash[hash]; 24205 tcps->tcps_hsp_hash[hash] = hsp; 24206 } 24207 24208 /* Set values that the user asked us to change */ 24209 24210 hsp->tcp_hsp_addr_v6 = v6addr; 24211 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 24212 hsp->tcp_hsp_vers = IPV4_VERSION; 24213 else 24214 hsp->tcp_hsp_vers = IPV6_VERSION; 24215 hsp->tcp_hsp_subnet_v6 = v6mask; 24216 if (sendspace > 0) 24217 hsp->tcp_hsp_sendspace = sendspace; 24218 if (recvspace > 0) 24219 hsp->tcp_hsp_recvspace = recvspace; 24220 if (timestamp > 0) 24221 hsp->tcp_hsp_tstamp = timestamp - 1; 24222 } 24223 24224 done: 24225 rw_exit(&tcps->tcps_hsp_lock); 24226 return (error); 24227 } 24228 24229 /* Set callback routine passed to nd_load by tcp_param_register. */ 24230 /* ARGSUSED */ 24231 static int 24232 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 24233 { 24234 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 24235 } 24236 /* ARGSUSED */ 24237 static int 24238 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24239 cred_t *cr) 24240 { 24241 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 24242 } 24243 24244 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 24245 /* ARGSUSED */ 24246 static int 24247 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 24248 { 24249 tcp_hsp_t *hsp; 24250 int i; 24251 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 24252 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24253 24254 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24255 (void) mi_mpprintf(mp, 24256 "Hash HSP " MI_COL_HDRPAD_STR 24257 "Address Subnet Mask Send Receive TStamp"); 24258 if (tcps->tcps_hsp_hash) { 24259 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 24260 hsp = tcps->tcps_hsp_hash[i]; 24261 while (hsp) { 24262 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 24263 (void) inet_ntop(AF_INET, 24264 &hsp->tcp_hsp_addr, 24265 addrbuf, sizeof (addrbuf)); 24266 (void) inet_ntop(AF_INET, 24267 &hsp->tcp_hsp_subnet, 24268 subnetbuf, sizeof (subnetbuf)); 24269 } else { 24270 (void) inet_ntop(AF_INET6, 24271 &hsp->tcp_hsp_addr_v6, 24272 addrbuf, sizeof (addrbuf)); 24273 (void) inet_ntop(AF_INET6, 24274 &hsp->tcp_hsp_subnet_v6, 24275 subnetbuf, sizeof (subnetbuf)); 24276 } 24277 (void) mi_mpprintf(mp, 24278 " %03d " MI_COL_PTRFMT_STR 24279 "%s %s %010d %010d %d", 24280 i, 24281 (void *)hsp, 24282 addrbuf, 24283 subnetbuf, 24284 hsp->tcp_hsp_sendspace, 24285 hsp->tcp_hsp_recvspace, 24286 hsp->tcp_hsp_tstamp); 24287 24288 hsp = hsp->tcp_hsp_next; 24289 } 24290 } 24291 } 24292 rw_exit(&tcps->tcps_hsp_lock); 24293 return (0); 24294 } 24295 24296 24297 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24298 24299 static ipaddr_t netmasks[] = { 24300 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24301 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24302 }; 24303 24304 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24305 24306 /* 24307 * XXX This routine should go away and instead we should use the metrics 24308 * associated with the routes to determine the default sndspace and rcvspace. 24309 */ 24310 static tcp_hsp_t * 24311 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24312 { 24313 tcp_hsp_t *hsp = NULL; 24314 24315 /* Quick check without acquiring the lock. */ 24316 if (tcps->tcps_hsp_hash == NULL) 24317 return (NULL); 24318 24319 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24320 24321 /* This routine finds the best-matching HSP for address addr. */ 24322 24323 if (tcps->tcps_hsp_hash) { 24324 int i; 24325 ipaddr_t srchaddr; 24326 tcp_hsp_t *hsp_net; 24327 24328 /* We do three passes: host, network, and subnet. */ 24329 24330 srchaddr = addr; 24331 24332 for (i = 1; i <= 3; i++) { 24333 /* Look for exact match on srchaddr */ 24334 24335 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24336 while (hsp) { 24337 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24338 hsp->tcp_hsp_addr == srchaddr) 24339 break; 24340 hsp = hsp->tcp_hsp_next; 24341 } 24342 ASSERT(hsp == NULL || 24343 hsp->tcp_hsp_vers == IPV4_VERSION); 24344 24345 /* 24346 * If this is the first pass: 24347 * If we found a match, great, return it. 24348 * If not, search for the network on the second pass. 24349 */ 24350 24351 if (i == 1) 24352 if (hsp) 24353 break; 24354 else 24355 { 24356 srchaddr = addr & netmask(addr); 24357 continue; 24358 } 24359 24360 /* 24361 * If this is the second pass: 24362 * If we found a match, but there's a subnet mask, 24363 * save the match but try again using the subnet 24364 * mask on the third pass. 24365 * Otherwise, return whatever we found. 24366 */ 24367 24368 if (i == 2) { 24369 if (hsp && hsp->tcp_hsp_subnet) { 24370 hsp_net = hsp; 24371 srchaddr = addr & hsp->tcp_hsp_subnet; 24372 continue; 24373 } else { 24374 break; 24375 } 24376 } 24377 24378 /* 24379 * This must be the third pass. If we didn't find 24380 * anything, return the saved network HSP instead. 24381 */ 24382 24383 if (!hsp) 24384 hsp = hsp_net; 24385 } 24386 } 24387 24388 rw_exit(&tcps->tcps_hsp_lock); 24389 return (hsp); 24390 } 24391 24392 /* 24393 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24394 * match lookup. 24395 */ 24396 static tcp_hsp_t * 24397 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24398 { 24399 tcp_hsp_t *hsp = NULL; 24400 24401 /* Quick check without acquiring the lock. */ 24402 if (tcps->tcps_hsp_hash == NULL) 24403 return (NULL); 24404 24405 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24406 24407 /* This routine finds the best-matching HSP for address addr. */ 24408 24409 if (tcps->tcps_hsp_hash) { 24410 int i; 24411 in6_addr_t v6srchaddr; 24412 tcp_hsp_t *hsp_net; 24413 24414 /* We do three passes: host, network, and subnet. */ 24415 24416 v6srchaddr = *v6addr; 24417 24418 for (i = 1; i <= 3; i++) { 24419 /* Look for exact match on srchaddr */ 24420 24421 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24422 V4_PART_OF_V6(v6srchaddr))]; 24423 while (hsp) { 24424 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24425 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24426 &v6srchaddr)) 24427 break; 24428 hsp = hsp->tcp_hsp_next; 24429 } 24430 24431 /* 24432 * If this is the first pass: 24433 * If we found a match, great, return it. 24434 * If not, search for the network on the second pass. 24435 */ 24436 24437 if (i == 1) 24438 if (hsp) 24439 break; 24440 else { 24441 /* Assume a 64 bit mask */ 24442 v6srchaddr.s6_addr32[0] = 24443 v6addr->s6_addr32[0]; 24444 v6srchaddr.s6_addr32[1] = 24445 v6addr->s6_addr32[1]; 24446 v6srchaddr.s6_addr32[2] = 0; 24447 v6srchaddr.s6_addr32[3] = 0; 24448 continue; 24449 } 24450 24451 /* 24452 * If this is the second pass: 24453 * If we found a match, but there's a subnet mask, 24454 * save the match but try again using the subnet 24455 * mask on the third pass. 24456 * Otherwise, return whatever we found. 24457 */ 24458 24459 if (i == 2) { 24460 ASSERT(hsp == NULL || 24461 hsp->tcp_hsp_vers == IPV6_VERSION); 24462 if (hsp && 24463 !IN6_IS_ADDR_UNSPECIFIED( 24464 &hsp->tcp_hsp_subnet_v6)) { 24465 hsp_net = hsp; 24466 V6_MASK_COPY(*v6addr, 24467 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24468 continue; 24469 } else { 24470 break; 24471 } 24472 } 24473 24474 /* 24475 * This must be the third pass. If we didn't find 24476 * anything, return the saved network HSP instead. 24477 */ 24478 24479 if (!hsp) 24480 hsp = hsp_net; 24481 } 24482 } 24483 24484 rw_exit(&tcps->tcps_hsp_lock); 24485 return (hsp); 24486 } 24487 24488 /* 24489 * Type three generator adapted from the random() function in 4.4 BSD: 24490 */ 24491 24492 /* 24493 * Copyright (c) 1983, 1993 24494 * The Regents of the University of California. All rights reserved. 24495 * 24496 * Redistribution and use in source and binary forms, with or without 24497 * modification, are permitted provided that the following conditions 24498 * are met: 24499 * 1. Redistributions of source code must retain the above copyright 24500 * notice, this list of conditions and the following disclaimer. 24501 * 2. Redistributions in binary form must reproduce the above copyright 24502 * notice, this list of conditions and the following disclaimer in the 24503 * documentation and/or other materials provided with the distribution. 24504 * 3. All advertising materials mentioning features or use of this software 24505 * must display the following acknowledgement: 24506 * This product includes software developed by the University of 24507 * California, Berkeley and its contributors. 24508 * 4. Neither the name of the University nor the names of its contributors 24509 * may be used to endorse or promote products derived from this software 24510 * without specific prior written permission. 24511 * 24512 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24513 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24514 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24515 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24516 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24517 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24518 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24519 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24520 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24521 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24522 * SUCH DAMAGE. 24523 */ 24524 24525 /* Type 3 -- x**31 + x**3 + 1 */ 24526 #define DEG_3 31 24527 #define SEP_3 3 24528 24529 24530 /* Protected by tcp_random_lock */ 24531 static int tcp_randtbl[DEG_3 + 1]; 24532 24533 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24534 static int *tcp_random_rptr = &tcp_randtbl[1]; 24535 24536 static int *tcp_random_state = &tcp_randtbl[1]; 24537 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24538 24539 kmutex_t tcp_random_lock; 24540 24541 void 24542 tcp_random_init(void) 24543 { 24544 int i; 24545 hrtime_t hrt; 24546 time_t wallclock; 24547 uint64_t result; 24548 24549 /* 24550 * Use high-res timer and current time for seed. Gethrtime() returns 24551 * a longlong, which may contain resolution down to nanoseconds. 24552 * The current time will either be a 32-bit or a 64-bit quantity. 24553 * XOR the two together in a 64-bit result variable. 24554 * Convert the result to a 32-bit value by multiplying the high-order 24555 * 32-bits by the low-order 32-bits. 24556 */ 24557 24558 hrt = gethrtime(); 24559 (void) drv_getparm(TIME, &wallclock); 24560 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24561 mutex_enter(&tcp_random_lock); 24562 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24563 (result & 0xffffffff); 24564 24565 for (i = 1; i < DEG_3; i++) 24566 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24567 + 12345; 24568 tcp_random_fptr = &tcp_random_state[SEP_3]; 24569 tcp_random_rptr = &tcp_random_state[0]; 24570 mutex_exit(&tcp_random_lock); 24571 for (i = 0; i < 10 * DEG_3; i++) 24572 (void) tcp_random(); 24573 } 24574 24575 /* 24576 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24577 * This range is selected to be approximately centered on TCP_ISS / 2, 24578 * and easy to compute. We get this value by generating a 32-bit random 24579 * number, selecting out the high-order 17 bits, and then adding one so 24580 * that we never return zero. 24581 */ 24582 int 24583 tcp_random(void) 24584 { 24585 int i; 24586 24587 mutex_enter(&tcp_random_lock); 24588 *tcp_random_fptr += *tcp_random_rptr; 24589 24590 /* 24591 * The high-order bits are more random than the low-order bits, 24592 * so we select out the high-order 17 bits and add one so that 24593 * we never return zero. 24594 */ 24595 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24596 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24597 tcp_random_fptr = tcp_random_state; 24598 ++tcp_random_rptr; 24599 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24600 tcp_random_rptr = tcp_random_state; 24601 24602 mutex_exit(&tcp_random_lock); 24603 return (i); 24604 } 24605 24606 /* 24607 * XXX This will go away when TPI is extended to send 24608 * info reqs to sockfs/timod ..... 24609 * Given a queue, set the max packet size for the write 24610 * side of the queue below stream head. This value is 24611 * cached on the stream head. 24612 * Returns 1 on success, 0 otherwise. 24613 */ 24614 static int 24615 setmaxps(queue_t *q, int maxpsz) 24616 { 24617 struct stdata *stp; 24618 queue_t *wq; 24619 stp = STREAM(q); 24620 24621 /* 24622 * At this point change of a queue parameter is not allowed 24623 * when a multiplexor is sitting on top. 24624 */ 24625 if (stp->sd_flag & STPLEX) 24626 return (0); 24627 24628 claimstr(stp->sd_wrq); 24629 wq = stp->sd_wrq->q_next; 24630 ASSERT(wq != NULL); 24631 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24632 releasestr(stp->sd_wrq); 24633 return (1); 24634 } 24635 24636 static int 24637 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24638 int *t_errorp, int *sys_errorp) 24639 { 24640 int error; 24641 int is_absreq_failure; 24642 t_scalar_t *opt_lenp; 24643 t_scalar_t opt_offset; 24644 int prim_type; 24645 struct T_conn_req *tcreqp; 24646 struct T_conn_res *tcresp; 24647 cred_t *cr; 24648 24649 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24650 24651 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24652 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24653 prim_type == T_CONN_RES); 24654 24655 switch (prim_type) { 24656 case T_CONN_REQ: 24657 tcreqp = (struct T_conn_req *)mp->b_rptr; 24658 opt_offset = tcreqp->OPT_offset; 24659 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24660 break; 24661 case O_T_CONN_RES: 24662 case T_CONN_RES: 24663 tcresp = (struct T_conn_res *)mp->b_rptr; 24664 opt_offset = tcresp->OPT_offset; 24665 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24666 break; 24667 } 24668 24669 *t_errorp = 0; 24670 *sys_errorp = 0; 24671 *do_disconnectp = 0; 24672 24673 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24674 opt_offset, cr, &tcp_opt_obj, 24675 NULL, &is_absreq_failure); 24676 24677 switch (error) { 24678 case 0: /* no error */ 24679 ASSERT(is_absreq_failure == 0); 24680 return (0); 24681 case ENOPROTOOPT: 24682 *t_errorp = TBADOPT; 24683 break; 24684 case EACCES: 24685 *t_errorp = TACCES; 24686 break; 24687 default: 24688 *t_errorp = TSYSERR; *sys_errorp = error; 24689 break; 24690 } 24691 if (is_absreq_failure != 0) { 24692 /* 24693 * The connection request should get the local ack 24694 * T_OK_ACK and then a T_DISCON_IND. 24695 */ 24696 *do_disconnectp = 1; 24697 } 24698 return (-1); 24699 } 24700 24701 /* 24702 * Split this function out so that if the secret changes, I'm okay. 24703 * 24704 * Initialize the tcp_iss_cookie and tcp_iss_key. 24705 */ 24706 24707 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24708 24709 static void 24710 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24711 { 24712 struct { 24713 int32_t current_time; 24714 uint32_t randnum; 24715 uint16_t pad; 24716 uint8_t ether[6]; 24717 uint8_t passwd[PASSWD_SIZE]; 24718 } tcp_iss_cookie; 24719 time_t t; 24720 24721 /* 24722 * Start with the current absolute time. 24723 */ 24724 (void) drv_getparm(TIME, &t); 24725 tcp_iss_cookie.current_time = t; 24726 24727 /* 24728 * XXX - Need a more random number per RFC 1750, not this crap. 24729 * OTOH, if what follows is pretty random, then I'm in better shape. 24730 */ 24731 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24732 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24733 24734 /* 24735 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24736 * as a good template. 24737 */ 24738 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24739 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24740 24741 /* 24742 * The pass-phrase. Normally this is supplied by user-called NDD. 24743 */ 24744 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24745 24746 /* 24747 * See 4010593 if this section becomes a problem again, 24748 * but the local ethernet address is useful here. 24749 */ 24750 (void) localetheraddr(NULL, 24751 (struct ether_addr *)&tcp_iss_cookie.ether); 24752 24753 /* 24754 * Hash 'em all together. The MD5Final is called per-connection. 24755 */ 24756 mutex_enter(&tcps->tcps_iss_key_lock); 24757 MD5Init(&tcps->tcps_iss_key); 24758 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24759 sizeof (tcp_iss_cookie)); 24760 mutex_exit(&tcps->tcps_iss_key_lock); 24761 } 24762 24763 /* 24764 * Set the RFC 1948 pass phrase 24765 */ 24766 /* ARGSUSED */ 24767 static int 24768 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24769 cred_t *cr) 24770 { 24771 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24772 24773 /* 24774 * Basically, value contains a new pass phrase. Pass it along! 24775 */ 24776 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24777 return (0); 24778 } 24779 24780 /* ARGSUSED */ 24781 static int 24782 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24783 { 24784 bzero(buf, sizeof (tcp_sack_info_t)); 24785 return (0); 24786 } 24787 24788 /* ARGSUSED */ 24789 static int 24790 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24791 { 24792 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24793 return (0); 24794 } 24795 24796 /* 24797 * Make sure we wait until the default queue is setup, yet allow 24798 * tcp_g_q_create() to open a TCP stream. 24799 * We need to allow tcp_g_q_create() do do an open 24800 * of tcp, hence we compare curhread. 24801 * All others have to wait until the tcps_g_q has been 24802 * setup. 24803 */ 24804 void 24805 tcp_g_q_setup(tcp_stack_t *tcps) 24806 { 24807 mutex_enter(&tcps->tcps_g_q_lock); 24808 if (tcps->tcps_g_q != NULL) { 24809 mutex_exit(&tcps->tcps_g_q_lock); 24810 return; 24811 } 24812 if (tcps->tcps_g_q_creator == NULL) { 24813 /* This thread will set it up */ 24814 tcps->tcps_g_q_creator = curthread; 24815 mutex_exit(&tcps->tcps_g_q_lock); 24816 tcp_g_q_create(tcps); 24817 mutex_enter(&tcps->tcps_g_q_lock); 24818 ASSERT(tcps->tcps_g_q_creator == curthread); 24819 tcps->tcps_g_q_creator = NULL; 24820 cv_signal(&tcps->tcps_g_q_cv); 24821 ASSERT(tcps->tcps_g_q != NULL); 24822 mutex_exit(&tcps->tcps_g_q_lock); 24823 return; 24824 } 24825 /* Everybody but the creator has to wait */ 24826 if (tcps->tcps_g_q_creator != curthread) { 24827 while (tcps->tcps_g_q == NULL) 24828 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24829 } 24830 mutex_exit(&tcps->tcps_g_q_lock); 24831 } 24832 24833 major_t IP_MAJ; 24834 #define IP "ip" 24835 24836 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24837 24838 /* 24839 * Create a default tcp queue here instead of in strplumb 24840 */ 24841 void 24842 tcp_g_q_create(tcp_stack_t *tcps) 24843 { 24844 int error; 24845 ldi_handle_t lh = NULL; 24846 ldi_ident_t li = NULL; 24847 int rval; 24848 cred_t *cr; 24849 24850 #ifdef NS_DEBUG 24851 (void) printf("tcp_g_q_create()\n"); 24852 #endif 24853 24854 ASSERT(tcps->tcps_g_q_creator == curthread); 24855 24856 error = ldi_ident_from_major(IP_MAJ, &li); 24857 if (error) { 24858 #ifdef DEBUG 24859 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24860 error); 24861 #endif 24862 return; 24863 } 24864 24865 cr = zone_get_kcred(netstackid_to_zoneid( 24866 tcps->tcps_netstack->netstack_stackid)); 24867 ASSERT(cr != NULL); 24868 /* 24869 * We set the tcp default queue to IPv6 because IPv4 falls 24870 * back to IPv6 when it can't find a client, but 24871 * IPv6 does not fall back to IPv4. 24872 */ 24873 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24874 if (error) { 24875 #ifdef DEBUG 24876 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24877 error); 24878 #endif 24879 goto out; 24880 } 24881 24882 /* 24883 * This ioctl causes the tcp framework to cache a pointer to 24884 * this stream, so we don't want to close the stream after 24885 * this operation. 24886 * Use the kernel credentials that are for the zone we're in. 24887 */ 24888 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24889 (intptr_t)0, FKIOCTL, cr, &rval); 24890 if (error) { 24891 #ifdef DEBUG 24892 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24893 "error %d\n", error); 24894 #endif 24895 goto out; 24896 } 24897 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24898 lh = NULL; 24899 out: 24900 /* Close layered handles */ 24901 if (li) 24902 ldi_ident_release(li); 24903 /* Keep cred around until _inactive needs it */ 24904 tcps->tcps_g_q_cr = cr; 24905 } 24906 24907 /* 24908 * We keep tcp_g_q set until all other tcp_t's in the zone 24909 * has gone away, and then when tcp_g_q_inactive() is called 24910 * we clear it. 24911 */ 24912 void 24913 tcp_g_q_destroy(tcp_stack_t *tcps) 24914 { 24915 #ifdef NS_DEBUG 24916 (void) printf("tcp_g_q_destroy()for stack %d\n", 24917 tcps->tcps_netstack->netstack_stackid); 24918 #endif 24919 24920 if (tcps->tcps_g_q == NULL) { 24921 return; /* Nothing to cleanup */ 24922 } 24923 /* 24924 * Drop reference corresponding to the default queue. 24925 * This reference was added from tcp_open when the default queue 24926 * was created, hence we compensate for this extra drop in 24927 * tcp_g_q_close. If the refcnt drops to zero here it means 24928 * the default queue was the last one to be open, in which 24929 * case, then tcp_g_q_inactive will be 24930 * called as a result of the refrele. 24931 */ 24932 TCPS_REFRELE(tcps); 24933 } 24934 24935 /* 24936 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24937 * Run by tcp_q_q_inactive using a taskq. 24938 */ 24939 static void 24940 tcp_g_q_close(void *arg) 24941 { 24942 tcp_stack_t *tcps = arg; 24943 int error; 24944 ldi_handle_t lh = NULL; 24945 ldi_ident_t li = NULL; 24946 cred_t *cr; 24947 24948 #ifdef NS_DEBUG 24949 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24950 tcps->tcps_netstack->netstack_stackid, 24951 tcps->tcps_netstack->netstack_refcnt); 24952 #endif 24953 lh = tcps->tcps_g_q_lh; 24954 if (lh == NULL) 24955 return; /* Nothing to cleanup */ 24956 24957 ASSERT(tcps->tcps_refcnt == 1); 24958 ASSERT(tcps->tcps_g_q != NULL); 24959 24960 error = ldi_ident_from_major(IP_MAJ, &li); 24961 if (error) { 24962 #ifdef DEBUG 24963 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24964 error); 24965 #endif 24966 return; 24967 } 24968 24969 cr = tcps->tcps_g_q_cr; 24970 tcps->tcps_g_q_cr = NULL; 24971 ASSERT(cr != NULL); 24972 24973 /* 24974 * Make sure we can break the recursion when tcp_close decrements 24975 * the reference count causing g_q_inactive to be called again. 24976 */ 24977 tcps->tcps_g_q_lh = NULL; 24978 24979 /* close the default queue */ 24980 (void) ldi_close(lh, FREAD|FWRITE, cr); 24981 /* 24982 * At this point in time tcps and the rest of netstack_t might 24983 * have been deleted. 24984 */ 24985 tcps = NULL; 24986 24987 /* Close layered handles */ 24988 ldi_ident_release(li); 24989 crfree(cr); 24990 } 24991 24992 /* 24993 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24994 * 24995 * Have to ensure that the ldi routines are not used by an 24996 * interrupt thread by using a taskq. 24997 */ 24998 void 24999 tcp_g_q_inactive(tcp_stack_t *tcps) 25000 { 25001 if (tcps->tcps_g_q_lh == NULL) 25002 return; /* Nothing to cleanup */ 25003 25004 ASSERT(tcps->tcps_refcnt == 0); 25005 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 25006 25007 if (servicing_interrupt()) { 25008 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 25009 (void *) tcps, TQ_SLEEP); 25010 } else { 25011 tcp_g_q_close(tcps); 25012 } 25013 } 25014 25015 /* 25016 * Called by IP when IP is loaded into the kernel 25017 */ 25018 void 25019 tcp_ddi_g_init(void) 25020 { 25021 IP_MAJ = ddi_name_to_major(IP); 25022 25023 tcp_timercache = kmem_cache_create("tcp_timercache", 25024 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 25025 NULL, NULL, NULL, NULL, NULL, 0); 25026 25027 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 25028 sizeof (tcp_sack_info_t), 0, 25029 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 25030 25031 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 25032 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 25033 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 25034 25035 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 25036 25037 /* Initialize the random number generator */ 25038 tcp_random_init(); 25039 25040 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 25041 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 25042 25043 /* A single callback independently of how many netstacks we have */ 25044 ip_squeue_init(tcp_squeue_add); 25045 25046 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 25047 25048 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 25049 TASKQ_PREPOPULATE); 25050 25051 /* 25052 * We want to be informed each time a stack is created or 25053 * destroyed in the kernel, so we can maintain the 25054 * set of tcp_stack_t's. 25055 */ 25056 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 25057 tcp_stack_fini); 25058 } 25059 25060 25061 /* 25062 * Initialize the TCP stack instance. 25063 */ 25064 static void * 25065 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 25066 { 25067 tcp_stack_t *tcps; 25068 tcpparam_t *pa; 25069 int i; 25070 25071 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 25072 tcps->tcps_netstack = ns; 25073 25074 /* Initialize locks */ 25075 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 25076 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 25077 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 25078 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 25079 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 25080 rw_init(&tcps->tcps_reserved_port_lock, NULL, RW_DEFAULT, NULL); 25081 25082 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 25083 tcps->tcps_g_epriv_ports[0] = 2049; 25084 tcps->tcps_g_epriv_ports[1] = 4045; 25085 tcps->tcps_min_anonpriv_port = 512; 25086 25087 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 25088 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 25089 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 25090 TCP_FANOUT_SIZE, KM_SLEEP); 25091 tcps->tcps_reserved_port = kmem_zalloc(sizeof (tcp_rport_t) * 25092 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE, KM_SLEEP); 25093 25094 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25095 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 25096 MUTEX_DEFAULT, NULL); 25097 } 25098 25099 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25100 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 25101 MUTEX_DEFAULT, NULL); 25102 } 25103 25104 /* TCP's IPsec code calls the packet dropper. */ 25105 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 25106 25107 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 25108 tcps->tcps_params = pa; 25109 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25110 25111 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 25112 A_CNT(lcl_tcp_param_arr), tcps); 25113 25114 /* 25115 * Note: To really walk the device tree you need the devinfo 25116 * pointer to your device which is only available after probe/attach. 25117 * The following is safe only because it uses ddi_root_node() 25118 */ 25119 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 25120 tcp_opt_obj.odb_opt_arr_cnt); 25121 25122 /* 25123 * Initialize RFC 1948 secret values. This will probably be reset once 25124 * by the boot scripts. 25125 * 25126 * Use NULL name, as the name is caught by the new lockstats. 25127 * 25128 * Initialize with some random, non-guessable string, like the global 25129 * T_INFO_ACK. 25130 */ 25131 25132 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 25133 sizeof (tcp_g_t_info_ack), tcps); 25134 25135 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 25136 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 25137 25138 return (tcps); 25139 } 25140 25141 /* 25142 * Called when the IP module is about to be unloaded. 25143 */ 25144 void 25145 tcp_ddi_g_destroy(void) 25146 { 25147 tcp_g_kstat_fini(tcp_g_kstat); 25148 tcp_g_kstat = NULL; 25149 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 25150 25151 mutex_destroy(&tcp_random_lock); 25152 25153 kmem_cache_destroy(tcp_timercache); 25154 kmem_cache_destroy(tcp_sack_info_cache); 25155 kmem_cache_destroy(tcp_iphc_cache); 25156 25157 netstack_unregister(NS_TCP); 25158 taskq_destroy(tcp_taskq); 25159 } 25160 25161 /* 25162 * Shut down the TCP stack instance. 25163 */ 25164 /* ARGSUSED */ 25165 static void 25166 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25167 { 25168 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25169 25170 tcp_g_q_destroy(tcps); 25171 } 25172 25173 /* 25174 * Free the TCP stack instance. 25175 */ 25176 static void 25177 tcp_stack_fini(netstackid_t stackid, void *arg) 25178 { 25179 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25180 int i; 25181 25182 nd_free(&tcps->tcps_g_nd); 25183 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25184 tcps->tcps_params = NULL; 25185 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25186 tcps->tcps_wroff_xtra_param = NULL; 25187 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25188 tcps->tcps_mdt_head_param = NULL; 25189 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25190 tcps->tcps_mdt_tail_param = NULL; 25191 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25192 tcps->tcps_mdt_max_pbufs_param = NULL; 25193 25194 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25195 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25196 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25197 } 25198 25199 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25200 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25201 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25202 } 25203 25204 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25205 tcps->tcps_bind_fanout = NULL; 25206 25207 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25208 tcps->tcps_acceptor_fanout = NULL; 25209 25210 kmem_free(tcps->tcps_reserved_port, sizeof (tcp_rport_t) * 25211 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE); 25212 tcps->tcps_reserved_port = NULL; 25213 25214 mutex_destroy(&tcps->tcps_iss_key_lock); 25215 rw_destroy(&tcps->tcps_hsp_lock); 25216 mutex_destroy(&tcps->tcps_g_q_lock); 25217 cv_destroy(&tcps->tcps_g_q_cv); 25218 mutex_destroy(&tcps->tcps_epriv_port_lock); 25219 rw_destroy(&tcps->tcps_reserved_port_lock); 25220 25221 ip_drop_unregister(&tcps->tcps_dropper); 25222 25223 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25224 tcps->tcps_kstat = NULL; 25225 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25226 25227 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25228 tcps->tcps_mibkp = NULL; 25229 25230 kmem_free(tcps, sizeof (*tcps)); 25231 } 25232 25233 /* 25234 * Generate ISS, taking into account NDD changes may happen halfway through. 25235 * (If the iss is not zero, set it.) 25236 */ 25237 25238 static void 25239 tcp_iss_init(tcp_t *tcp) 25240 { 25241 MD5_CTX context; 25242 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25243 uint32_t answer[4]; 25244 tcp_stack_t *tcps = tcp->tcp_tcps; 25245 25246 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25247 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25248 switch (tcps->tcps_strong_iss) { 25249 case 2: 25250 mutex_enter(&tcps->tcps_iss_key_lock); 25251 context = tcps->tcps_iss_key; 25252 mutex_exit(&tcps->tcps_iss_key_lock); 25253 arg.ports = tcp->tcp_ports; 25254 if (tcp->tcp_ipversion == IPV4_VERSION) { 25255 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25256 &arg.src); 25257 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25258 &arg.dst); 25259 } else { 25260 arg.src = tcp->tcp_ip6h->ip6_src; 25261 arg.dst = tcp->tcp_ip6h->ip6_dst; 25262 } 25263 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25264 MD5Final((uchar_t *)answer, &context); 25265 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25266 /* 25267 * Now that we've hashed into a unique per-connection sequence 25268 * space, add a random increment per strong_iss == 1. So I 25269 * guess we'll have to... 25270 */ 25271 /* FALLTHRU */ 25272 case 1: 25273 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25274 break; 25275 default: 25276 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25277 break; 25278 } 25279 tcp->tcp_valid_bits = TCP_ISS_VALID; 25280 tcp->tcp_fss = tcp->tcp_iss - 1; 25281 tcp->tcp_suna = tcp->tcp_iss; 25282 tcp->tcp_snxt = tcp->tcp_iss + 1; 25283 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25284 tcp->tcp_csuna = tcp->tcp_snxt; 25285 } 25286 25287 /* 25288 * Exported routine for extracting active tcp connection status. 25289 * 25290 * This is used by the Solaris Cluster Networking software to 25291 * gather a list of connections that need to be forwarded to 25292 * specific nodes in the cluster when configuration changes occur. 25293 * 25294 * The callback is invoked for each tcp_t structure. Returning 25295 * non-zero from the callback routine terminates the search. 25296 */ 25297 int 25298 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25299 void *arg) 25300 { 25301 netstack_handle_t nh; 25302 netstack_t *ns; 25303 int ret = 0; 25304 25305 netstack_next_init(&nh); 25306 while ((ns = netstack_next(&nh)) != NULL) { 25307 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25308 ns->netstack_tcp); 25309 netstack_rele(ns); 25310 } 25311 netstack_next_fini(&nh); 25312 return (ret); 25313 } 25314 25315 static int 25316 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25317 tcp_stack_t *tcps) 25318 { 25319 tcp_t *tcp; 25320 cl_tcp_info_t cl_tcpi; 25321 connf_t *connfp; 25322 conn_t *connp; 25323 int i; 25324 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25325 25326 ASSERT(callback != NULL); 25327 25328 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25329 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25330 connp = NULL; 25331 25332 while ((connp = 25333 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25334 25335 tcp = connp->conn_tcp; 25336 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25337 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25338 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25339 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25340 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25341 /* 25342 * The macros tcp_laddr and tcp_faddr give the IPv4 25343 * addresses. They are copied implicitly below as 25344 * mapped addresses. 25345 */ 25346 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25347 if (tcp->tcp_ipversion == IPV4_VERSION) { 25348 cl_tcpi.cl_tcpi_faddr = 25349 tcp->tcp_ipha->ipha_dst; 25350 } else { 25351 cl_tcpi.cl_tcpi_faddr_v6 = 25352 tcp->tcp_ip6h->ip6_dst; 25353 } 25354 25355 /* 25356 * If the callback returns non-zero 25357 * we terminate the traversal. 25358 */ 25359 if ((*callback)(&cl_tcpi, arg) != 0) { 25360 CONN_DEC_REF(tcp->tcp_connp); 25361 return (1); 25362 } 25363 } 25364 } 25365 25366 return (0); 25367 } 25368 25369 /* 25370 * Macros used for accessing the different types of sockaddr 25371 * structures inside a tcp_ioc_abort_conn_t. 25372 */ 25373 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25374 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25375 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25376 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25377 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25378 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25379 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25380 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25381 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25382 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25383 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25384 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25385 25386 /* 25387 * Return the correct error code to mimic the behavior 25388 * of a connection reset. 25389 */ 25390 #define TCP_AC_GET_ERRCODE(state, err) { \ 25391 switch ((state)) { \ 25392 case TCPS_SYN_SENT: \ 25393 case TCPS_SYN_RCVD: \ 25394 (err) = ECONNREFUSED; \ 25395 break; \ 25396 case TCPS_ESTABLISHED: \ 25397 case TCPS_FIN_WAIT_1: \ 25398 case TCPS_FIN_WAIT_2: \ 25399 case TCPS_CLOSE_WAIT: \ 25400 (err) = ECONNRESET; \ 25401 break; \ 25402 case TCPS_CLOSING: \ 25403 case TCPS_LAST_ACK: \ 25404 case TCPS_TIME_WAIT: \ 25405 (err) = 0; \ 25406 break; \ 25407 default: \ 25408 (err) = ENXIO; \ 25409 } \ 25410 } 25411 25412 /* 25413 * Check if a tcp structure matches the info in acp. 25414 */ 25415 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25416 (((acp)->ac_local.ss_family == AF_INET) ? \ 25417 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25418 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25419 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25420 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25421 (TCP_AC_V4LPORT((acp)) == 0 || \ 25422 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25423 (TCP_AC_V4RPORT((acp)) == 0 || \ 25424 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25425 (acp)->ac_start <= (tcp)->tcp_state && \ 25426 (acp)->ac_end >= (tcp)->tcp_state) : \ 25427 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25428 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25429 &(tcp)->tcp_ip_src_v6)) && \ 25430 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25431 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25432 &(tcp)->tcp_remote_v6)) && \ 25433 (TCP_AC_V6LPORT((acp)) == 0 || \ 25434 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25435 (TCP_AC_V6RPORT((acp)) == 0 || \ 25436 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25437 (acp)->ac_start <= (tcp)->tcp_state && \ 25438 (acp)->ac_end >= (tcp)->tcp_state)) 25439 25440 #define TCP_AC_MATCH(acp, tcp) \ 25441 (((acp)->ac_zoneid == ALL_ZONES || \ 25442 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25443 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25444 25445 /* 25446 * Build a message containing a tcp_ioc_abort_conn_t structure 25447 * which is filled in with information from acp and tp. 25448 */ 25449 static mblk_t * 25450 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25451 { 25452 mblk_t *mp; 25453 tcp_ioc_abort_conn_t *tacp; 25454 25455 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25456 if (mp == NULL) 25457 return (NULL); 25458 25459 mp->b_datap->db_type = M_CTL; 25460 25461 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25462 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25463 sizeof (uint32_t)); 25464 25465 tacp->ac_start = acp->ac_start; 25466 tacp->ac_end = acp->ac_end; 25467 tacp->ac_zoneid = acp->ac_zoneid; 25468 25469 if (acp->ac_local.ss_family == AF_INET) { 25470 tacp->ac_local.ss_family = AF_INET; 25471 tacp->ac_remote.ss_family = AF_INET; 25472 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25473 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25474 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25475 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25476 } else { 25477 tacp->ac_local.ss_family = AF_INET6; 25478 tacp->ac_remote.ss_family = AF_INET6; 25479 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25480 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25481 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25482 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25483 } 25484 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25485 return (mp); 25486 } 25487 25488 /* 25489 * Print a tcp_ioc_abort_conn_t structure. 25490 */ 25491 static void 25492 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25493 { 25494 char lbuf[128]; 25495 char rbuf[128]; 25496 sa_family_t af; 25497 in_port_t lport, rport; 25498 ushort_t logflags; 25499 25500 af = acp->ac_local.ss_family; 25501 25502 if (af == AF_INET) { 25503 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25504 lbuf, 128); 25505 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25506 rbuf, 128); 25507 lport = ntohs(TCP_AC_V4LPORT(acp)); 25508 rport = ntohs(TCP_AC_V4RPORT(acp)); 25509 } else { 25510 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25511 lbuf, 128); 25512 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25513 rbuf, 128); 25514 lport = ntohs(TCP_AC_V6LPORT(acp)); 25515 rport = ntohs(TCP_AC_V6RPORT(acp)); 25516 } 25517 25518 logflags = SL_TRACE | SL_NOTE; 25519 /* 25520 * Don't print this message to the console if the operation was done 25521 * to a non-global zone. 25522 */ 25523 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25524 logflags |= SL_CONSOLE; 25525 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25526 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25527 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25528 acp->ac_start, acp->ac_end); 25529 } 25530 25531 /* 25532 * Called inside tcp_rput when a message built using 25533 * tcp_ioctl_abort_build_msg is put into a queue. 25534 * Note that when we get here there is no wildcard in acp any more. 25535 */ 25536 static void 25537 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25538 { 25539 tcp_ioc_abort_conn_t *acp; 25540 25541 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25542 if (tcp->tcp_state <= acp->ac_end) { 25543 /* 25544 * If we get here, we are already on the correct 25545 * squeue. This ioctl follows the following path 25546 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25547 * ->tcp_ioctl_abort->squeue_fill (if on a 25548 * different squeue) 25549 */ 25550 int errcode; 25551 25552 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25553 (void) tcp_clean_death(tcp, errcode, 26); 25554 } 25555 freemsg(mp); 25556 } 25557 25558 /* 25559 * Abort all matching connections on a hash chain. 25560 */ 25561 static int 25562 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25563 boolean_t exact, tcp_stack_t *tcps) 25564 { 25565 int nmatch, err = 0; 25566 tcp_t *tcp; 25567 MBLKP mp, last, listhead = NULL; 25568 conn_t *tconnp; 25569 connf_t *connfp; 25570 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25571 25572 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25573 25574 startover: 25575 nmatch = 0; 25576 25577 mutex_enter(&connfp->connf_lock); 25578 for (tconnp = connfp->connf_head; tconnp != NULL; 25579 tconnp = tconnp->conn_next) { 25580 tcp = tconnp->conn_tcp; 25581 if (TCP_AC_MATCH(acp, tcp)) { 25582 CONN_INC_REF(tcp->tcp_connp); 25583 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25584 if (mp == NULL) { 25585 err = ENOMEM; 25586 CONN_DEC_REF(tcp->tcp_connp); 25587 break; 25588 } 25589 mp->b_prev = (mblk_t *)tcp; 25590 25591 if (listhead == NULL) { 25592 listhead = mp; 25593 last = mp; 25594 } else { 25595 last->b_next = mp; 25596 last = mp; 25597 } 25598 nmatch++; 25599 if (exact) 25600 break; 25601 } 25602 25603 /* Avoid holding lock for too long. */ 25604 if (nmatch >= 500) 25605 break; 25606 } 25607 mutex_exit(&connfp->connf_lock); 25608 25609 /* Pass mp into the correct tcp */ 25610 while ((mp = listhead) != NULL) { 25611 listhead = listhead->b_next; 25612 tcp = (tcp_t *)mp->b_prev; 25613 mp->b_next = mp->b_prev = NULL; 25614 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 25615 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 25616 } 25617 25618 *count += nmatch; 25619 if (nmatch >= 500 && err == 0) 25620 goto startover; 25621 return (err); 25622 } 25623 25624 /* 25625 * Abort all connections that matches the attributes specified in acp. 25626 */ 25627 static int 25628 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25629 { 25630 sa_family_t af; 25631 uint32_t ports; 25632 uint16_t *pports; 25633 int err = 0, count = 0; 25634 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25635 int index = -1; 25636 ushort_t logflags; 25637 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25638 25639 af = acp->ac_local.ss_family; 25640 25641 if (af == AF_INET) { 25642 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25643 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25644 pports = (uint16_t *)&ports; 25645 pports[1] = TCP_AC_V4LPORT(acp); 25646 pports[0] = TCP_AC_V4RPORT(acp); 25647 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25648 } 25649 } else { 25650 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25651 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25652 pports = (uint16_t *)&ports; 25653 pports[1] = TCP_AC_V6LPORT(acp); 25654 pports[0] = TCP_AC_V6RPORT(acp); 25655 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25656 } 25657 } 25658 25659 /* 25660 * For cases where remote addr, local port, and remote port are non- 25661 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25662 */ 25663 if (index != -1) { 25664 err = tcp_ioctl_abort_bucket(acp, index, 25665 &count, exact, tcps); 25666 } else { 25667 /* 25668 * loop through all entries for wildcard case 25669 */ 25670 for (index = 0; 25671 index < ipst->ips_ipcl_conn_fanout_size; 25672 index++) { 25673 err = tcp_ioctl_abort_bucket(acp, index, 25674 &count, exact, tcps); 25675 if (err != 0) 25676 break; 25677 } 25678 } 25679 25680 logflags = SL_TRACE | SL_NOTE; 25681 /* 25682 * Don't print this message to the console if the operation was done 25683 * to a non-global zone. 25684 */ 25685 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25686 logflags |= SL_CONSOLE; 25687 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25688 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25689 if (err == 0 && count == 0) 25690 err = ENOENT; 25691 return (err); 25692 } 25693 25694 /* 25695 * Process the TCP_IOC_ABORT_CONN ioctl request. 25696 */ 25697 static void 25698 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25699 { 25700 int err; 25701 IOCP iocp; 25702 MBLKP mp1; 25703 sa_family_t laf, raf; 25704 tcp_ioc_abort_conn_t *acp; 25705 zone_t *zptr; 25706 conn_t *connp = Q_TO_CONN(q); 25707 zoneid_t zoneid = connp->conn_zoneid; 25708 tcp_t *tcp = connp->conn_tcp; 25709 tcp_stack_t *tcps = tcp->tcp_tcps; 25710 25711 iocp = (IOCP)mp->b_rptr; 25712 25713 if ((mp1 = mp->b_cont) == NULL || 25714 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25715 err = EINVAL; 25716 goto out; 25717 } 25718 25719 /* check permissions */ 25720 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25721 err = EPERM; 25722 goto out; 25723 } 25724 25725 if (mp1->b_cont != NULL) { 25726 freemsg(mp1->b_cont); 25727 mp1->b_cont = NULL; 25728 } 25729 25730 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25731 laf = acp->ac_local.ss_family; 25732 raf = acp->ac_remote.ss_family; 25733 25734 /* check that a zone with the supplied zoneid exists */ 25735 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25736 zptr = zone_find_by_id(zoneid); 25737 if (zptr != NULL) { 25738 zone_rele(zptr); 25739 } else { 25740 err = EINVAL; 25741 goto out; 25742 } 25743 } 25744 25745 /* 25746 * For exclusive stacks we set the zoneid to zero 25747 * to make TCP operate as if in the global zone. 25748 */ 25749 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25750 acp->ac_zoneid = GLOBAL_ZONEID; 25751 25752 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25753 acp->ac_start > acp->ac_end || laf != raf || 25754 (laf != AF_INET && laf != AF_INET6)) { 25755 err = EINVAL; 25756 goto out; 25757 } 25758 25759 tcp_ioctl_abort_dump(acp); 25760 err = tcp_ioctl_abort(acp, tcps); 25761 25762 out: 25763 if (mp1 != NULL) { 25764 freemsg(mp1); 25765 mp->b_cont = NULL; 25766 } 25767 25768 if (err != 0) 25769 miocnak(q, mp, 0, err); 25770 else 25771 miocack(q, mp, 0, 0); 25772 } 25773 25774 /* 25775 * tcp_time_wait_processing() handles processing of incoming packets when 25776 * the tcp is in the TIME_WAIT state. 25777 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25778 * on the time wait list. 25779 */ 25780 void 25781 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25782 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25783 { 25784 int32_t bytes_acked; 25785 int32_t gap; 25786 int32_t rgap; 25787 tcp_opt_t tcpopt; 25788 uint_t flags; 25789 uint32_t new_swnd = 0; 25790 conn_t *connp; 25791 tcp_stack_t *tcps = tcp->tcp_tcps; 25792 25793 BUMP_LOCAL(tcp->tcp_ibsegs); 25794 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 25795 25796 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25797 new_swnd = BE16_TO_U16(tcph->th_win) << 25798 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25799 if (tcp->tcp_snd_ts_ok) { 25800 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25801 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25802 tcp->tcp_rnxt, TH_ACK); 25803 goto done; 25804 } 25805 } 25806 gap = seg_seq - tcp->tcp_rnxt; 25807 rgap = tcp->tcp_rwnd - (gap + seg_len); 25808 if (gap < 0) { 25809 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25810 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25811 (seg_len > -gap ? -gap : seg_len)); 25812 seg_len += gap; 25813 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25814 if (flags & TH_RST) { 25815 goto done; 25816 } 25817 if ((flags & TH_FIN) && seg_len == -1) { 25818 /* 25819 * When TCP receives a duplicate FIN in 25820 * TIME_WAIT state, restart the 2 MSL timer. 25821 * See page 73 in RFC 793. Make sure this TCP 25822 * is already on the TIME_WAIT list. If not, 25823 * just restart the timer. 25824 */ 25825 if (TCP_IS_DETACHED(tcp)) { 25826 if (tcp_time_wait_remove(tcp, NULL) == 25827 B_TRUE) { 25828 tcp_time_wait_append(tcp); 25829 TCP_DBGSTAT(tcps, 25830 tcp_rput_time_wait); 25831 } 25832 } else { 25833 ASSERT(tcp != NULL); 25834 TCP_TIMER_RESTART(tcp, 25835 tcps->tcps_time_wait_interval); 25836 } 25837 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25838 tcp->tcp_rnxt, TH_ACK); 25839 goto done; 25840 } 25841 flags |= TH_ACK_NEEDED; 25842 seg_len = 0; 25843 goto process_ack; 25844 } 25845 25846 /* Fix seg_seq, and chew the gap off the front. */ 25847 seg_seq = tcp->tcp_rnxt; 25848 } 25849 25850 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25851 /* 25852 * Make sure that when we accept the connection, pick 25853 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25854 * old connection. 25855 * 25856 * The next ISS generated is equal to tcp_iss_incr_extra 25857 * + ISS_INCR/2 + other components depending on the 25858 * value of tcp_strong_iss. We pre-calculate the new 25859 * ISS here and compare with tcp_snxt to determine if 25860 * we need to make adjustment to tcp_iss_incr_extra. 25861 * 25862 * The above calculation is ugly and is a 25863 * waste of CPU cycles... 25864 */ 25865 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25866 int32_t adj; 25867 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25868 25869 switch (tcps->tcps_strong_iss) { 25870 case 2: { 25871 /* Add time and MD5 components. */ 25872 uint32_t answer[4]; 25873 struct { 25874 uint32_t ports; 25875 in6_addr_t src; 25876 in6_addr_t dst; 25877 } arg; 25878 MD5_CTX context; 25879 25880 mutex_enter(&tcps->tcps_iss_key_lock); 25881 context = tcps->tcps_iss_key; 25882 mutex_exit(&tcps->tcps_iss_key_lock); 25883 arg.ports = tcp->tcp_ports; 25884 /* We use MAPPED addresses in tcp_iss_init */ 25885 arg.src = tcp->tcp_ip_src_v6; 25886 if (tcp->tcp_ipversion == IPV4_VERSION) { 25887 IN6_IPADDR_TO_V4MAPPED( 25888 tcp->tcp_ipha->ipha_dst, 25889 &arg.dst); 25890 } else { 25891 arg.dst = 25892 tcp->tcp_ip6h->ip6_dst; 25893 } 25894 MD5Update(&context, (uchar_t *)&arg, 25895 sizeof (arg)); 25896 MD5Final((uchar_t *)answer, &context); 25897 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25898 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25899 break; 25900 } 25901 case 1: 25902 /* Add time component and min random (i.e. 1). */ 25903 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25904 break; 25905 default: 25906 /* Add only time component. */ 25907 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25908 break; 25909 } 25910 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25911 /* 25912 * New ISS not guaranteed to be ISS_INCR/2 25913 * ahead of the current tcp_snxt, so add the 25914 * difference to tcp_iss_incr_extra. 25915 */ 25916 tcps->tcps_iss_incr_extra += adj; 25917 } 25918 /* 25919 * If tcp_clean_death() can not perform the task now, 25920 * drop the SYN packet and let the other side re-xmit. 25921 * Otherwise pass the SYN packet back in, since the 25922 * old tcp state has been cleaned up or freed. 25923 */ 25924 if (tcp_clean_death(tcp, 0, 27) == -1) 25925 goto done; 25926 /* 25927 * We will come back to tcp_rput_data 25928 * on the global queue. Packets destined 25929 * for the global queue will be checked 25930 * with global policy. But the policy for 25931 * this packet has already been checked as 25932 * this was destined for the detached 25933 * connection. We need to bypass policy 25934 * check this time by attaching a dummy 25935 * ipsec_in with ipsec_in_dont_check set. 25936 */ 25937 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25938 if (connp != NULL) { 25939 TCP_STAT(tcps, tcp_time_wait_syn_success); 25940 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25941 return; 25942 } 25943 goto done; 25944 } 25945 25946 /* 25947 * rgap is the amount of stuff received out of window. A negative 25948 * value is the amount out of window. 25949 */ 25950 if (rgap < 0) { 25951 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25952 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25953 /* Fix seg_len and make sure there is something left. */ 25954 seg_len += rgap; 25955 if (seg_len <= 0) { 25956 if (flags & TH_RST) { 25957 goto done; 25958 } 25959 flags |= TH_ACK_NEEDED; 25960 seg_len = 0; 25961 goto process_ack; 25962 } 25963 } 25964 /* 25965 * Check whether we can update tcp_ts_recent. This test is 25966 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25967 * Extensions for High Performance: An Update", Internet Draft. 25968 */ 25969 if (tcp->tcp_snd_ts_ok && 25970 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25971 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25972 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25973 tcp->tcp_last_rcv_lbolt = lbolt64; 25974 } 25975 25976 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25977 /* Always ack out of order packets */ 25978 flags |= TH_ACK_NEEDED; 25979 seg_len = 0; 25980 } else if (seg_len > 0) { 25981 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25982 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25983 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25984 } 25985 if (flags & TH_RST) { 25986 (void) tcp_clean_death(tcp, 0, 28); 25987 goto done; 25988 } 25989 if (flags & TH_SYN) { 25990 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25991 TH_RST|TH_ACK); 25992 /* 25993 * Do not delete the TCP structure if it is in 25994 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25995 */ 25996 goto done; 25997 } 25998 process_ack: 25999 if (flags & TH_ACK) { 26000 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 26001 if (bytes_acked <= 0) { 26002 if (bytes_acked == 0 && seg_len == 0 && 26003 new_swnd == tcp->tcp_swnd) 26004 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 26005 } else { 26006 /* Acks something not sent */ 26007 flags |= TH_ACK_NEEDED; 26008 } 26009 } 26010 if (flags & TH_ACK_NEEDED) { 26011 /* 26012 * Time to send an ack for some reason. 26013 */ 26014 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 26015 tcp->tcp_rnxt, TH_ACK); 26016 } 26017 done: 26018 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26019 DB_CKSUMSTART(mp) = 0; 26020 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 26021 TCP_STAT(tcps, tcp_time_wait_syn_fail); 26022 } 26023 freemsg(mp); 26024 } 26025 26026 /* 26027 * Allocate a T_SVR4_OPTMGMT_REQ. 26028 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 26029 * that tcp_rput_other can drop the acks. 26030 */ 26031 static mblk_t * 26032 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 26033 { 26034 mblk_t *mp; 26035 struct T_optmgmt_req *tor; 26036 struct opthdr *oh; 26037 uint_t size; 26038 char *optptr; 26039 26040 size = sizeof (*tor) + sizeof (*oh) + optlen; 26041 mp = allocb(size, BPRI_MED); 26042 if (mp == NULL) 26043 return (NULL); 26044 26045 mp->b_wptr += size; 26046 mp->b_datap->db_type = M_PROTO; 26047 tor = (struct T_optmgmt_req *)mp->b_rptr; 26048 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 26049 tor->MGMT_flags = T_NEGOTIATE; 26050 tor->OPT_length = sizeof (*oh) + optlen; 26051 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 26052 26053 oh = (struct opthdr *)&tor[1]; 26054 oh->level = level; 26055 oh->name = cmd; 26056 oh->len = optlen; 26057 if (optlen != 0) { 26058 optptr = (char *)&oh[1]; 26059 bcopy(opt, optptr, optlen); 26060 } 26061 return (mp); 26062 } 26063 26064 /* 26065 * TCP Timers Implementation. 26066 */ 26067 timeout_id_t 26068 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 26069 { 26070 mblk_t *mp; 26071 tcp_timer_t *tcpt; 26072 tcp_t *tcp = connp->conn_tcp; 26073 tcp_stack_t *tcps = tcp->tcp_tcps; 26074 26075 ASSERT(connp->conn_sqp != NULL); 26076 26077 TCP_DBGSTAT(tcps, tcp_timeout_calls); 26078 26079 if (tcp->tcp_timercache == NULL) { 26080 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 26081 } else { 26082 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 26083 mp = tcp->tcp_timercache; 26084 tcp->tcp_timercache = mp->b_next; 26085 mp->b_next = NULL; 26086 ASSERT(mp->b_wptr == NULL); 26087 } 26088 26089 CONN_INC_REF(connp); 26090 tcpt = (tcp_timer_t *)mp->b_rptr; 26091 tcpt->connp = connp; 26092 tcpt->tcpt_proc = f; 26093 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 26094 return ((timeout_id_t)mp); 26095 } 26096 26097 static void 26098 tcp_timer_callback(void *arg) 26099 { 26100 mblk_t *mp = (mblk_t *)arg; 26101 tcp_timer_t *tcpt; 26102 conn_t *connp; 26103 26104 tcpt = (tcp_timer_t *)mp->b_rptr; 26105 connp = tcpt->connp; 26106 squeue_fill(connp->conn_sqp, mp, 26107 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 26108 } 26109 26110 static void 26111 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 26112 { 26113 tcp_timer_t *tcpt; 26114 conn_t *connp = (conn_t *)arg; 26115 tcp_t *tcp = connp->conn_tcp; 26116 26117 tcpt = (tcp_timer_t *)mp->b_rptr; 26118 ASSERT(connp == tcpt->connp); 26119 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 26120 26121 /* 26122 * If the TCP has reached the closed state, don't proceed any 26123 * further. This TCP logically does not exist on the system. 26124 * tcpt_proc could for example access queues, that have already 26125 * been qprocoff'ed off. Also see comments at the start of tcp_input 26126 */ 26127 if (tcp->tcp_state != TCPS_CLOSED) { 26128 (*tcpt->tcpt_proc)(connp); 26129 } else { 26130 tcp->tcp_timer_tid = 0; 26131 } 26132 tcp_timer_free(connp->conn_tcp, mp); 26133 } 26134 26135 /* 26136 * There is potential race with untimeout and the handler firing at the same 26137 * time. The mblock may be freed by the handler while we are trying to use 26138 * it. But since both should execute on the same squeue, this race should not 26139 * occur. 26140 */ 26141 clock_t 26142 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 26143 { 26144 mblk_t *mp = (mblk_t *)id; 26145 tcp_timer_t *tcpt; 26146 clock_t delta; 26147 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26148 26149 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 26150 26151 if (mp == NULL) 26152 return (-1); 26153 26154 tcpt = (tcp_timer_t *)mp->b_rptr; 26155 ASSERT(tcpt->connp == connp); 26156 26157 delta = untimeout(tcpt->tcpt_tid); 26158 26159 if (delta >= 0) { 26160 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 26161 tcp_timer_free(connp->conn_tcp, mp); 26162 CONN_DEC_REF(connp); 26163 } 26164 26165 return (delta); 26166 } 26167 26168 /* 26169 * Allocate space for the timer event. The allocation looks like mblk, but it is 26170 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26171 * 26172 * Dealing with failures: If we can't allocate from the timer cache we try 26173 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26174 * points to b_rptr. 26175 * If we can't allocate anything using allocb_tryhard(), we perform a last 26176 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26177 * save the actual allocation size in b_datap. 26178 */ 26179 mblk_t * 26180 tcp_timermp_alloc(int kmflags) 26181 { 26182 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26183 kmflags & ~KM_PANIC); 26184 26185 if (mp != NULL) { 26186 mp->b_next = mp->b_prev = NULL; 26187 mp->b_rptr = (uchar_t *)(&mp[1]); 26188 mp->b_wptr = NULL; 26189 mp->b_datap = NULL; 26190 mp->b_queue = NULL; 26191 mp->b_cont = NULL; 26192 } else if (kmflags & KM_PANIC) { 26193 /* 26194 * Failed to allocate memory for the timer. Try allocating from 26195 * dblock caches. 26196 */ 26197 /* ipclassifier calls this from a constructor - hence no tcps */ 26198 TCP_G_STAT(tcp_timermp_allocfail); 26199 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26200 if (mp == NULL) { 26201 size_t size = 0; 26202 /* 26203 * Memory is really low. Try tryhard allocation. 26204 * 26205 * ipclassifier calls this from a constructor - 26206 * hence no tcps 26207 */ 26208 TCP_G_STAT(tcp_timermp_allocdblfail); 26209 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26210 sizeof (tcp_timer_t), &size, kmflags); 26211 mp->b_rptr = (uchar_t *)(&mp[1]); 26212 mp->b_next = mp->b_prev = NULL; 26213 mp->b_wptr = (uchar_t *)-1; 26214 mp->b_datap = (dblk_t *)size; 26215 mp->b_queue = NULL; 26216 mp->b_cont = NULL; 26217 } 26218 ASSERT(mp->b_wptr != NULL); 26219 } 26220 /* ipclassifier calls this from a constructor - hence no tcps */ 26221 TCP_G_DBGSTAT(tcp_timermp_alloced); 26222 26223 return (mp); 26224 } 26225 26226 /* 26227 * Free per-tcp timer cache. 26228 * It can only contain entries from tcp_timercache. 26229 */ 26230 void 26231 tcp_timermp_free(tcp_t *tcp) 26232 { 26233 mblk_t *mp; 26234 26235 while ((mp = tcp->tcp_timercache) != NULL) { 26236 ASSERT(mp->b_wptr == NULL); 26237 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26238 kmem_cache_free(tcp_timercache, mp); 26239 } 26240 } 26241 26242 /* 26243 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26244 * events there already (currently at most two events are cached). 26245 * If the event is not allocated from the timer cache, free it right away. 26246 */ 26247 static void 26248 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26249 { 26250 mblk_t *mp1 = tcp->tcp_timercache; 26251 tcp_stack_t *tcps = tcp->tcp_tcps; 26252 26253 if (mp->b_wptr != NULL) { 26254 /* 26255 * This allocation is not from a timer cache, free it right 26256 * away. 26257 */ 26258 if (mp->b_wptr != (uchar_t *)-1) 26259 freeb(mp); 26260 else 26261 kmem_free(mp, (size_t)mp->b_datap); 26262 } else if (mp1 == NULL || mp1->b_next == NULL) { 26263 /* Cache this timer block for future allocations */ 26264 mp->b_rptr = (uchar_t *)(&mp[1]); 26265 mp->b_next = mp1; 26266 tcp->tcp_timercache = mp; 26267 } else { 26268 kmem_cache_free(tcp_timercache, mp); 26269 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26270 } 26271 } 26272 26273 /* 26274 * End of TCP Timers implementation. 26275 */ 26276 26277 /* 26278 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26279 * on the specified backing STREAMS q. Note, the caller may make the 26280 * decision to call based on the tcp_t.tcp_flow_stopped value which 26281 * when check outside the q's lock is only an advisory check ... 26282 */ 26283 26284 void 26285 tcp_setqfull(tcp_t *tcp) 26286 { 26287 queue_t *q = tcp->tcp_wq; 26288 tcp_stack_t *tcps = tcp->tcp_tcps; 26289 26290 if (!(q->q_flag & QFULL)) { 26291 mutex_enter(QLOCK(q)); 26292 if (!(q->q_flag & QFULL)) { 26293 /* still need to set QFULL */ 26294 q->q_flag |= QFULL; 26295 tcp->tcp_flow_stopped = B_TRUE; 26296 mutex_exit(QLOCK(q)); 26297 TCP_STAT(tcps, tcp_flwctl_on); 26298 } else { 26299 mutex_exit(QLOCK(q)); 26300 } 26301 } 26302 } 26303 26304 void 26305 tcp_clrqfull(tcp_t *tcp) 26306 { 26307 queue_t *q = tcp->tcp_wq; 26308 26309 if (q->q_flag & QFULL) { 26310 mutex_enter(QLOCK(q)); 26311 if (q->q_flag & QFULL) { 26312 q->q_flag &= ~QFULL; 26313 tcp->tcp_flow_stopped = B_FALSE; 26314 mutex_exit(QLOCK(q)); 26315 if (q->q_flag & QWANTW) 26316 qbackenable(q, 0); 26317 } else { 26318 mutex_exit(QLOCK(q)); 26319 } 26320 } 26321 } 26322 26323 26324 /* 26325 * kstats related to squeues i.e. not per IP instance 26326 */ 26327 static void * 26328 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26329 { 26330 kstat_t *ksp; 26331 26332 tcp_g_stat_t template = { 26333 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26334 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26335 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26336 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26337 }; 26338 26339 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26340 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26341 KSTAT_FLAG_VIRTUAL); 26342 26343 if (ksp == NULL) 26344 return (NULL); 26345 26346 bcopy(&template, tcp_g_statp, sizeof (template)); 26347 ksp->ks_data = (void *)tcp_g_statp; 26348 26349 kstat_install(ksp); 26350 return (ksp); 26351 } 26352 26353 static void 26354 tcp_g_kstat_fini(kstat_t *ksp) 26355 { 26356 if (ksp != NULL) { 26357 kstat_delete(ksp); 26358 } 26359 } 26360 26361 26362 static void * 26363 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26364 { 26365 kstat_t *ksp; 26366 26367 tcp_stat_t template = { 26368 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26369 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26370 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26371 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26372 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26373 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26374 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26375 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26376 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26377 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26378 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26379 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26380 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26381 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26382 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26383 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26384 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26385 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26386 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26387 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26388 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26389 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26390 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26391 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26392 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26393 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26394 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26395 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26396 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26397 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26398 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26399 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26400 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26401 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26402 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26403 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26404 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26405 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26406 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26407 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26408 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26409 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26410 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26411 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26412 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26413 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26414 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26415 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26416 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26417 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26418 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26419 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26420 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26421 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26422 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26423 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26424 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26425 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26426 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26427 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26428 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26429 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26430 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26431 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26432 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26433 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26434 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26435 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26436 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26437 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26438 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26439 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26440 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26441 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26442 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26443 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26444 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26445 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26446 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26447 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26448 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26449 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26450 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26451 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26452 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26453 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26454 }; 26455 26456 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26457 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26458 KSTAT_FLAG_VIRTUAL, stackid); 26459 26460 if (ksp == NULL) 26461 return (NULL); 26462 26463 bcopy(&template, tcps_statisticsp, sizeof (template)); 26464 ksp->ks_data = (void *)tcps_statisticsp; 26465 ksp->ks_private = (void *)(uintptr_t)stackid; 26466 26467 kstat_install(ksp); 26468 return (ksp); 26469 } 26470 26471 static void 26472 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26473 { 26474 if (ksp != NULL) { 26475 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26476 kstat_delete_netstack(ksp, stackid); 26477 } 26478 } 26479 26480 /* 26481 * TCP Kstats implementation 26482 */ 26483 static void * 26484 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26485 { 26486 kstat_t *ksp; 26487 26488 tcp_named_kstat_t template = { 26489 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26490 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26491 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26492 { "maxConn", KSTAT_DATA_INT32, 0 }, 26493 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26494 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26495 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26496 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26497 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26498 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26499 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26500 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26501 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26502 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26503 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26504 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26505 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26506 { "outAck", KSTAT_DATA_UINT32, 0 }, 26507 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26508 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26509 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26510 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26511 { "outControl", KSTAT_DATA_UINT32, 0 }, 26512 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26513 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26514 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26515 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26516 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26517 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26518 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26519 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26520 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26521 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26522 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26523 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26524 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26525 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26526 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26527 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26528 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26529 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26530 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26531 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26532 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26533 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26534 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26535 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26536 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26537 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26538 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26539 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26540 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26541 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26542 }; 26543 26544 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26545 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26546 26547 if (ksp == NULL) 26548 return (NULL); 26549 26550 template.rtoAlgorithm.value.ui32 = 4; 26551 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26552 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26553 template.maxConn.value.i32 = -1; 26554 26555 bcopy(&template, ksp->ks_data, sizeof (template)); 26556 ksp->ks_update = tcp_kstat_update; 26557 ksp->ks_private = (void *)(uintptr_t)stackid; 26558 26559 kstat_install(ksp); 26560 return (ksp); 26561 } 26562 26563 static void 26564 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26565 { 26566 if (ksp != NULL) { 26567 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26568 kstat_delete_netstack(ksp, stackid); 26569 } 26570 } 26571 26572 static int 26573 tcp_kstat_update(kstat_t *kp, int rw) 26574 { 26575 tcp_named_kstat_t *tcpkp; 26576 tcp_t *tcp; 26577 connf_t *connfp; 26578 conn_t *connp; 26579 int i; 26580 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26581 netstack_t *ns; 26582 tcp_stack_t *tcps; 26583 ip_stack_t *ipst; 26584 26585 if ((kp == NULL) || (kp->ks_data == NULL)) 26586 return (EIO); 26587 26588 if (rw == KSTAT_WRITE) 26589 return (EACCES); 26590 26591 ns = netstack_find_by_stackid(stackid); 26592 if (ns == NULL) 26593 return (-1); 26594 tcps = ns->netstack_tcp; 26595 if (tcps == NULL) { 26596 netstack_rele(ns); 26597 return (-1); 26598 } 26599 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26600 26601 tcpkp->currEstab.value.ui32 = 0; 26602 26603 ipst = ns->netstack_ip; 26604 26605 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26606 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26607 connp = NULL; 26608 while ((connp = 26609 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26610 tcp = connp->conn_tcp; 26611 switch (tcp_snmp_state(tcp)) { 26612 case MIB2_TCP_established: 26613 case MIB2_TCP_closeWait: 26614 tcpkp->currEstab.value.ui32++; 26615 break; 26616 } 26617 } 26618 } 26619 26620 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26621 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26622 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26623 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26624 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26625 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26626 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26627 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26628 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26629 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26630 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26631 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26632 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26633 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26634 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26635 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26636 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26637 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26638 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26639 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26640 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26641 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26642 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26643 tcpkp->inDataInorderSegs.value.ui32 = 26644 tcps->tcps_mib.tcpInDataInorderSegs; 26645 tcpkp->inDataInorderBytes.value.ui32 = 26646 tcps->tcps_mib.tcpInDataInorderBytes; 26647 tcpkp->inDataUnorderSegs.value.ui32 = 26648 tcps->tcps_mib.tcpInDataUnorderSegs; 26649 tcpkp->inDataUnorderBytes.value.ui32 = 26650 tcps->tcps_mib.tcpInDataUnorderBytes; 26651 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26652 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26653 tcpkp->inDataPartDupSegs.value.ui32 = 26654 tcps->tcps_mib.tcpInDataPartDupSegs; 26655 tcpkp->inDataPartDupBytes.value.ui32 = 26656 tcps->tcps_mib.tcpInDataPartDupBytes; 26657 tcpkp->inDataPastWinSegs.value.ui32 = 26658 tcps->tcps_mib.tcpInDataPastWinSegs; 26659 tcpkp->inDataPastWinBytes.value.ui32 = 26660 tcps->tcps_mib.tcpInDataPastWinBytes; 26661 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26662 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26663 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26664 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26665 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26666 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26667 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26668 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26669 tcpkp->timKeepaliveProbe.value.ui32 = 26670 tcps->tcps_mib.tcpTimKeepaliveProbe; 26671 tcpkp->timKeepaliveDrop.value.ui32 = 26672 tcps->tcps_mib.tcpTimKeepaliveDrop; 26673 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26674 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26675 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26676 tcpkp->outSackRetransSegs.value.ui32 = 26677 tcps->tcps_mib.tcpOutSackRetransSegs; 26678 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26679 26680 netstack_rele(ns); 26681 return (0); 26682 } 26683 26684 void 26685 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26686 { 26687 uint16_t hdr_len; 26688 ipha_t *ipha; 26689 uint8_t *nexthdrp; 26690 tcph_t *tcph; 26691 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26692 26693 /* Already has an eager */ 26694 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26695 TCP_STAT(tcps, tcp_reinput_syn); 26696 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 26697 connp, SQTAG_TCP_REINPUT_EAGER); 26698 return; 26699 } 26700 26701 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26702 case IPV4_VERSION: 26703 ipha = (ipha_t *)mp->b_rptr; 26704 hdr_len = IPH_HDR_LENGTH(ipha); 26705 break; 26706 case IPV6_VERSION: 26707 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26708 &hdr_len, &nexthdrp)) { 26709 CONN_DEC_REF(connp); 26710 freemsg(mp); 26711 return; 26712 } 26713 break; 26714 } 26715 26716 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26717 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26718 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26719 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26720 } 26721 26722 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 26723 SQTAG_TCP_REINPUT); 26724 } 26725 26726 static squeue_func_t 26727 tcp_squeue_switch(int val) 26728 { 26729 squeue_func_t rval = squeue_fill; 26730 26731 switch (val) { 26732 case 1: 26733 rval = squeue_enter_nodrain; 26734 break; 26735 case 2: 26736 rval = squeue_enter; 26737 break; 26738 default: 26739 break; 26740 } 26741 return (rval); 26742 } 26743 26744 /* 26745 * This is called once for each squeue - globally for all stack 26746 * instances. 26747 */ 26748 static void 26749 tcp_squeue_add(squeue_t *sqp) 26750 { 26751 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26752 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26753 26754 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26755 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 26756 sqp, TCP_TIME_WAIT_DELAY); 26757 if (tcp_free_list_max_cnt == 0) { 26758 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26759 max_ncpus : boot_max_ncpus); 26760 26761 /* 26762 * Limit number of entries to 1% of availble memory / tcp_ncpus 26763 */ 26764 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26765 (tcp_ncpus * sizeof (tcp_t) * 100); 26766 } 26767 tcp_time_wait->tcp_free_list_cnt = 0; 26768 } 26769