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 2006 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 61 #include <sys/errno.h> 62 #include <sys/signal.h> 63 #include <sys/socket.h> 64 #include <sys/sockio.h> 65 #include <sys/isa_defs.h> 66 #include <sys/md5.h> 67 #include <sys/random.h> 68 #include <netinet/in.h> 69 #include <netinet/tcp.h> 70 #include <netinet/ip6.h> 71 #include <netinet/icmp6.h> 72 #include <net/if.h> 73 #include <net/route.h> 74 #include <inet/ipsec_impl.h> 75 76 #include <inet/common.h> 77 #include <inet/ip.h> 78 #include <inet/ip_impl.h> 79 #include <inet/ip6.h> 80 #include <inet/ip_ndp.h> 81 #include <inet/mi.h> 82 #include <inet/mib2.h> 83 #include <inet/nd.h> 84 #include <inet/optcom.h> 85 #include <inet/snmpcom.h> 86 #include <inet/kstatcom.h> 87 #include <inet/tcp.h> 88 #include <inet/tcp_impl.h> 89 #include <net/pfkeyv2.h> 90 #include <inet/ipsec_info.h> 91 #include <inet/ipdrop.h> 92 #include <inet/tcp_trace.h> 93 94 #include <inet/ipclassifier.h> 95 #include <inet/ip_ire.h> 96 #include <inet/ip_ftable.h> 97 #include <inet/ip_if.h> 98 #include <inet/ipp_common.h> 99 #include <inet/ip_netinfo.h> 100 #include <sys/squeue.h> 101 #include <inet/kssl/ksslapi.h> 102 #include <sys/tsol/label.h> 103 #include <sys/tsol/tnet.h> 104 #include <sys/sdt.h> 105 #include <rpc/pmap_prot.h> 106 107 /* 108 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 109 * 110 * (Read the detailed design doc in PSARC case directory) 111 * 112 * The entire tcp state is contained in tcp_t and conn_t structure 113 * which are allocated in tandem using ipcl_conn_create() and passing 114 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 115 * the references on the tcp_t. The tcp_t structure is never compressed 116 * and packets always land on the correct TCP perimeter from the time 117 * eager is created till the time tcp_t dies (as such the old mentat 118 * TCP global queue is not used for detached state and no IPSEC checking 119 * is required). The global queue is still allocated to send out resets 120 * for connection which have no listeners and IP directly calls 121 * tcp_xmit_listeners_reset() which does any policy check. 122 * 123 * Protection and Synchronisation mechanism: 124 * 125 * The tcp data structure does not use any kind of lock for protecting 126 * its state but instead uses 'squeues' for mutual exclusion from various 127 * read and write side threads. To access a tcp member, the thread should 128 * always be behind squeue (via squeue_enter, squeue_enter_nodrain, or 129 * squeue_fill). Since the squeues allow a direct function call, caller 130 * can pass any tcp function having prototype of edesc_t as argument 131 * (different from traditional STREAMs model where packets come in only 132 * designated entry points). The list of functions that can be directly 133 * called via squeue are listed before the usual function prototype. 134 * 135 * Referencing: 136 * 137 * TCP is MT-Hot and we use a reference based scheme to make sure that the 138 * tcp structure doesn't disappear when its needed. When the application 139 * creates an outgoing connection or accepts an incoming connection, we 140 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 141 * The IP reference is just a symbolic reference since ip_tcpclose() 142 * looks at tcp structure after tcp_close_output() returns which could 143 * have dropped the last TCP reference. So as long as the connection is 144 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 145 * conn_t. The classifier puts its own reference when the connection is 146 * inserted in listen or connected hash. Anytime a thread needs to enter 147 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 148 * on write side or by doing a classify on read side and then puts a 149 * reference on the conn before doing squeue_enter/tryenter/fill. For 150 * read side, the classifier itself puts the reference under fanout lock 151 * to make sure that tcp can't disappear before it gets processed. The 152 * squeue will drop this reference automatically so the called function 153 * doesn't have to do a DEC_REF. 154 * 155 * Opening a new connection: 156 * 157 * The outgoing connection open is pretty simple. ip_tcpopen() does the 158 * work in creating the conn/tcp structure and initializing it. The 159 * squeue assignment is done based on the CPU the application 160 * is running on. So for outbound connections, processing is always done 161 * on application CPU which might be different from the incoming CPU 162 * being interrupted by the NIC. An optimal way would be to figure out 163 * the NIC <-> CPU binding at listen time, and assign the outgoing 164 * connection to the squeue attached to the CPU that will be interrupted 165 * for incoming packets (we know the NIC based on the bind IP address). 166 * This might seem like a problem if more data is going out but the 167 * fact is that in most cases the transmit is ACK driven transmit where 168 * the outgoing data normally sits on TCP's xmit queue waiting to be 169 * transmitted. 170 * 171 * Accepting a connection: 172 * 173 * This is a more interesting case because of various races involved in 174 * establishing a eager in its own perimeter. Read the meta comment on 175 * top of tcp_conn_request(). But briefly, the squeue is picked by 176 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 177 * 178 * Closing a connection: 179 * 180 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 181 * via squeue to do the close and mark the tcp as detached if the connection 182 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 183 * reference but tcp_close() drop IP's reference always. So if tcp was 184 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 185 * and 1 because it is in classifier's connected hash. This is the condition 186 * we use to determine that its OK to clean up the tcp outside of squeue 187 * when time wait expires (check the ref under fanout and conn_lock and 188 * if it is 2, remove it from fanout hash and kill it). 189 * 190 * Although close just drops the necessary references and marks the 191 * tcp_detached state, tcp_close needs to know the tcp_detached has been 192 * set (under squeue) before letting the STREAM go away (because a 193 * inbound packet might attempt to go up the STREAM while the close 194 * has happened and tcp_detached is not set). So a special lock and 195 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 196 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 197 * tcp_detached. 198 * 199 * Special provisions and fast paths: 200 * 201 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 202 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 203 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 204 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 205 * check to send packets directly to tcp_rput_data via squeue. Everyone 206 * else comes through tcp_input() on the read side. 207 * 208 * We also make special provisions for sockfs by marking tcp_issocket 209 * whenever we have only sockfs on top of TCP. This allows us to skip 210 * putting the tcp in acceptor hash since a sockfs listener can never 211 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 212 * since eager has already been allocated and the accept now happens 213 * on acceptor STREAM. There is a big blob of comment on top of 214 * tcp_conn_request explaining the new accept. When socket is POP'd, 215 * sockfs sends us an ioctl to mark the fact and we go back to old 216 * behaviour. Once tcp_issocket is unset, its never set for the 217 * life of that connection. 218 * 219 * IPsec notes : 220 * 221 * Since a packet is always executed on the correct TCP perimeter 222 * all IPsec processing is defered to IP including checking new 223 * connections and setting IPSEC policies for new connection. The 224 * only exception is tcp_xmit_listeners_reset() which is called 225 * directly from IP and needs to policy check to see if TH_RST 226 * can be sent out. 227 * 228 * PFHooks notes : 229 * 230 * For mdt case, one meta buffer contains multiple packets. Mblks for every 231 * packet are assembled and passed to the hooks. When packets are blocked, 232 * or boundary of any packet is changed, the mdt processing is stopped, and 233 * packets of the meta buffer are send to the IP path one by one. 234 */ 235 236 extern major_t TCP6_MAJ; 237 238 /* 239 * Values for squeue switch: 240 * 1: squeue_enter_nodrain 241 * 2: squeue_enter 242 * 3: squeue_fill 243 */ 244 int tcp_squeue_close = 2; 245 int tcp_squeue_wput = 2; 246 247 squeue_func_t tcp_squeue_close_proc; 248 squeue_func_t tcp_squeue_wput_proc; 249 250 /* 251 * This controls how tiny a write must be before we try to copy it 252 * into the the mblk on the tail of the transmit queue. Not much 253 * speedup is observed for values larger than sixteen. Zero will 254 * disable the optimisation. 255 */ 256 int tcp_tx_pull_len = 16; 257 258 /* 259 * TCP Statistics. 260 * 261 * How TCP statistics work. 262 * 263 * There are two types of statistics invoked by two macros. 264 * 265 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 266 * supposed to be used in non MT-hot paths of the code. 267 * 268 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 269 * supposed to be used for DEBUG purposes and may be used on a hot path. 270 * 271 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 272 * (use "kstat tcp" to get them). 273 * 274 * There is also additional debugging facility that marks tcp_clean_death() 275 * instances and saves them in tcp_t structure. It is triggered by 276 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 277 * tcp_clean_death() calls that counts the number of times each tag was hit. It 278 * is triggered by TCP_CLD_COUNTERS define. 279 * 280 * How to add new counters. 281 * 282 * 1) Add a field in the tcp_stat structure describing your counter. 283 * 2) Add a line in tcp_statistics with the name of the counter. 284 * 285 * IMPORTANT!! - make sure that both are in sync !! 286 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 287 * 288 * Please avoid using private counters which are not kstat-exported. 289 * 290 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 291 * in tcp_t structure. 292 * 293 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 294 */ 295 296 #ifndef TCP_DEBUG_COUNTER 297 #ifdef DEBUG 298 #define TCP_DEBUG_COUNTER 1 299 #else 300 #define TCP_DEBUG_COUNTER 0 301 #endif 302 #endif 303 304 #define TCP_CLD_COUNTERS 0 305 306 #define TCP_TAG_CLEAN_DEATH 1 307 #define TCP_MAX_CLEAN_DEATH_TAG 32 308 309 #ifdef lint 310 static int _lint_dummy_; 311 #endif 312 313 #if TCP_CLD_COUNTERS 314 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 315 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 316 #elif defined(lint) 317 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 318 #else 319 #define TCP_CLD_STAT(x) 320 #endif 321 322 #if TCP_DEBUG_COUNTER 323 #define TCP_DBGSTAT(x) atomic_add_64(&(tcp_statistics.x.value.ui64), 1) 324 #elif defined(lint) 325 #define TCP_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 326 #else 327 #define TCP_DBGSTAT(x) 328 #endif 329 330 tcp_stat_t tcp_statistics = { 331 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 332 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 333 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 334 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 335 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 336 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 337 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 338 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 339 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 340 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 341 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 342 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 343 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 344 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 345 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 346 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 347 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 348 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 349 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 350 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 351 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 352 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 353 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 354 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 355 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 356 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 357 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 358 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 359 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 360 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 361 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 362 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 363 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 364 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 365 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 366 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 367 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 368 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 369 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 370 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 371 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 372 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 373 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 374 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 375 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 376 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 377 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 378 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 379 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 380 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 381 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 382 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 383 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 384 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 385 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 386 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 387 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 388 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 389 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 390 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 391 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 392 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 393 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 394 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 395 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 396 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 397 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 398 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 399 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 400 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 401 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 402 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 403 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 404 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 405 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 406 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 407 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 408 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 409 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 410 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 411 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 412 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 413 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 414 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 415 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 416 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 417 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 418 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 419 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 420 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 421 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 422 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 423 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 424 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 425 }; 426 427 static kstat_t *tcp_kstat; 428 429 /* 430 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 431 * tcp write side. 432 */ 433 #define CALL_IP_WPUT(connp, q, mp) { \ 434 ASSERT(((q)->q_flag & QREADR) == 0); \ 435 TCP_DBGSTAT(tcp_ip_output); \ 436 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 437 } 438 439 /* Macros for timestamp comparisons */ 440 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 441 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 442 443 /* 444 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 445 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 446 * by adding three components: a time component which grows by 1 every 4096 447 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 448 * a per-connection component which grows by 125000 for every new connection; 449 * and an "extra" component that grows by a random amount centered 450 * approximately on 64000. This causes the the ISS generator to cycle every 451 * 4.89 hours if no TCP connections are made, and faster if connections are 452 * made. 453 * 454 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 455 * components: a time component which grows by 250000 every second; and 456 * a per-connection component which grows by 125000 for every new connections. 457 * 458 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 459 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 460 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 461 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 462 * password. 463 */ 464 #define ISS_INCR 250000 465 #define ISS_NSEC_SHT 12 466 467 static uint32_t tcp_iss_incr_extra; /* Incremented for each connection */ 468 static kmutex_t tcp_iss_key_lock; 469 static MD5_CTX tcp_iss_key; 470 static sin_t sin_null; /* Zero address for quick clears */ 471 static sin6_t sin6_null; /* Zero address for quick clears */ 472 473 /* Packet dropper for TCP IPsec policy drops. */ 474 static ipdropper_t tcp_dropper; 475 476 /* 477 * This implementation follows the 4.3BSD interpretation of the urgent 478 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 479 * incompatible changes in protocols like telnet and rlogin. 480 */ 481 #define TCP_OLD_URP_INTERPRETATION 1 482 483 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 484 (TCP_IS_DETACHED(tcp) && \ 485 (!(tcp)->tcp_hard_binding)) 486 487 /* 488 * TCP reassembly macros. We hide starting and ending sequence numbers in 489 * b_next and b_prev of messages on the reassembly queue. The messages are 490 * chained using b_cont. These macros are used in tcp_reass() so we don't 491 * have to see the ugly casts and assignments. 492 */ 493 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 494 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 495 (mblk_t *)(uintptr_t)(u)) 496 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 497 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 498 (mblk_t *)(uintptr_t)(u)) 499 500 /* 501 * Implementation of TCP Timers. 502 * ============================= 503 * 504 * INTERFACE: 505 * 506 * There are two basic functions dealing with tcp timers: 507 * 508 * timeout_id_t tcp_timeout(connp, func, time) 509 * clock_t tcp_timeout_cancel(connp, timeout_id) 510 * TCP_TIMER_RESTART(tcp, intvl) 511 * 512 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 513 * after 'time' ticks passed. The function called by timeout() must adhere to 514 * the same restrictions as a driver soft interrupt handler - it must not sleep 515 * or call other functions that might sleep. The value returned is the opaque 516 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 517 * cancel the request. The call to tcp_timeout() may fail in which case it 518 * returns zero. This is different from the timeout(9F) function which never 519 * fails. 520 * 521 * The call-back function 'func' always receives 'connp' as its single 522 * argument. It is always executed in the squeue corresponding to the tcp 523 * structure. The tcp structure is guaranteed to be present at the time the 524 * call-back is called. 525 * 526 * NOTE: The call-back function 'func' is never called if tcp is in 527 * the TCPS_CLOSED state. 528 * 529 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 530 * request. locks acquired by the call-back routine should not be held across 531 * the call to tcp_timeout_cancel() or a deadlock may result. 532 * 533 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 534 * Otherwise, it returns an integer value greater than or equal to 0. In 535 * particular, if the call-back function is already placed on the squeue, it can 536 * not be canceled. 537 * 538 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 539 * within squeue context corresponding to the tcp instance. Since the 540 * call-back is also called via the same squeue, there are no race 541 * conditions described in untimeout(9F) manual page since all calls are 542 * strictly serialized. 543 * 544 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 545 * stored in tcp_timer_tid and starts a new one using 546 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 547 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 548 * field. 549 * 550 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 551 * call-back may still be called, so it is possible tcp_timer() will be 552 * called several times. This should not be a problem since tcp_timer() 553 * should always check the tcp instance state. 554 * 555 * 556 * IMPLEMENTATION: 557 * 558 * TCP timers are implemented using three-stage process. The call to 559 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 560 * when the timer expires. The tcp_timer_callback() arranges the call of the 561 * tcp_timer_handler() function via squeue corresponding to the tcp 562 * instance. The tcp_timer_handler() calls actual requested timeout call-back 563 * and passes tcp instance as an argument to it. Information is passed between 564 * stages using the tcp_timer_t structure which contains the connp pointer, the 565 * tcp call-back to call and the timeout id returned by the timeout(9F). 566 * 567 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 568 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 569 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 570 * returns the pointer to this mblk. 571 * 572 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 573 * looks like a normal mblk without actual dblk attached to it. 574 * 575 * To optimize performance each tcp instance holds a small cache of timer 576 * mblocks. In the current implementation it caches up to two timer mblocks per 577 * tcp instance. The cache is preserved over tcp frees and is only freed when 578 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 579 * timer processing happens on a corresponding squeue, the cache manipulation 580 * does not require any locks. Experiments show that majority of timer mblocks 581 * allocations are satisfied from the tcp cache and do not involve kmem calls. 582 * 583 * The tcp_timeout() places a refhold on the connp instance which guarantees 584 * that it will be present at the time the call-back function fires. The 585 * tcp_timer_handler() drops the reference after calling the call-back, so the 586 * call-back function does not need to manipulate the references explicitly. 587 */ 588 589 typedef struct tcp_timer_s { 590 conn_t *connp; 591 void (*tcpt_proc)(void *); 592 timeout_id_t tcpt_tid; 593 } tcp_timer_t; 594 595 static kmem_cache_t *tcp_timercache; 596 kmem_cache_t *tcp_sack_info_cache; 597 kmem_cache_t *tcp_iphc_cache; 598 599 /* 600 * For scalability, we must not run a timer for every TCP connection 601 * in TIME_WAIT state. To see why, consider (for time wait interval of 602 * 4 minutes): 603 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 604 * 605 * This list is ordered by time, so you need only delete from the head 606 * until you get to entries which aren't old enough to delete yet. 607 * The list consists of only the detached TIME_WAIT connections. 608 * 609 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 610 * becomes detached TIME_WAIT (either by changing the state and already 611 * being detached or the other way around). This means that the TIME_WAIT 612 * state can be extended (up to doubled) if the connection doesn't become 613 * detached for a long time. 614 * 615 * The list manipulations (including tcp_time_wait_next/prev) 616 * are protected by the tcp_time_wait_lock. The content of the 617 * detached TIME_WAIT connections is protected by the normal perimeters. 618 */ 619 620 typedef struct tcp_squeue_priv_s { 621 kmutex_t tcp_time_wait_lock; 622 /* Protects the next 3 globals */ 623 timeout_id_t tcp_time_wait_tid; 624 tcp_t *tcp_time_wait_head; 625 tcp_t *tcp_time_wait_tail; 626 tcp_t *tcp_free_list; 627 uint_t tcp_free_list_cnt; 628 } tcp_squeue_priv_t; 629 630 /* 631 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 632 * Running it every 5 seconds seems to give the best results. 633 */ 634 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 635 636 /* 637 * To prevent memory hog, limit the number of entries in tcp_free_list 638 * to 1% of available memory / number of cpus 639 */ 640 uint_t tcp_free_list_max_cnt = 0; 641 642 #define TCP_XMIT_LOWATER 4096 643 #define TCP_XMIT_HIWATER 49152 644 #define TCP_RECV_LOWATER 2048 645 #define TCP_RECV_HIWATER 49152 646 647 /* 648 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 649 */ 650 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 651 652 #define TIDUSZ 4096 /* transport interface data unit size */ 653 654 /* 655 * Bind hash list size and has function. It has to be a power of 2 for 656 * hashing. 657 */ 658 #define TCP_BIND_FANOUT_SIZE 512 659 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 660 /* 661 * Size of listen and acceptor hash list. It has to be a power of 2 for 662 * hashing. 663 */ 664 #define TCP_FANOUT_SIZE 256 665 666 #ifdef _ILP32 667 #define TCP_ACCEPTOR_HASH(accid) \ 668 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 669 #else 670 #define TCP_ACCEPTOR_HASH(accid) \ 671 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 672 #endif /* _ILP32 */ 673 674 #define IP_ADDR_CACHE_SIZE 2048 675 #define IP_ADDR_CACHE_HASH(faddr) \ 676 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 677 678 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 679 #define TCP_HSP_HASH_SIZE 256 680 681 #define TCP_HSP_HASH(addr) \ 682 (((addr>>24) ^ (addr >>16) ^ \ 683 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 684 685 /* 686 * TCP options struct returned from tcp_parse_options. 687 */ 688 typedef struct tcp_opt_s { 689 uint32_t tcp_opt_mss; 690 uint32_t tcp_opt_wscale; 691 uint32_t tcp_opt_ts_val; 692 uint32_t tcp_opt_ts_ecr; 693 tcp_t *tcp; 694 } tcp_opt_t; 695 696 /* 697 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 698 */ 699 700 #ifdef _BIG_ENDIAN 701 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 702 (TCPOPT_TSTAMP << 8) | 10) 703 #else 704 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 705 (TCPOPT_NOP << 8) | TCPOPT_NOP) 706 #endif 707 708 /* 709 * Flags returned from tcp_parse_options. 710 */ 711 #define TCP_OPT_MSS_PRESENT 1 712 #define TCP_OPT_WSCALE_PRESENT 2 713 #define TCP_OPT_TSTAMP_PRESENT 4 714 #define TCP_OPT_SACK_OK_PRESENT 8 715 #define TCP_OPT_SACK_PRESENT 16 716 717 /* TCP option length */ 718 #define TCPOPT_NOP_LEN 1 719 #define TCPOPT_MAXSEG_LEN 4 720 #define TCPOPT_WS_LEN 3 721 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 722 #define TCPOPT_TSTAMP_LEN 10 723 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 724 #define TCPOPT_SACK_OK_LEN 2 725 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 726 #define TCPOPT_REAL_SACK_LEN 4 727 #define TCPOPT_MAX_SACK_LEN 36 728 #define TCPOPT_HEADER_LEN 2 729 730 /* TCP cwnd burst factor. */ 731 #define TCP_CWND_INFINITE 65535 732 #define TCP_CWND_SS 3 733 #define TCP_CWND_NORMAL 5 734 735 /* Maximum TCP initial cwin (start/restart). */ 736 #define TCP_MAX_INIT_CWND 8 737 738 /* 739 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 740 * either tcp_slow_start_initial or tcp_slow_start_after idle 741 * depending on the caller. If the upper layer has not used the 742 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 743 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 744 * If the upper layer has changed set the tcp_init_cwnd, just use 745 * it to calculate the tcp_cwnd. 746 */ 747 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 748 { \ 749 if ((tcp)->tcp_init_cwnd == 0) { \ 750 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 751 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 752 } else { \ 753 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 754 } \ 755 tcp->tcp_cwnd_cnt = 0; \ 756 } 757 758 /* TCP Timer control structure */ 759 typedef struct tcpt_s { 760 pfv_t tcpt_pfv; /* The routine we are to call */ 761 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 762 } tcpt_t; 763 764 /* Host Specific Parameter structure */ 765 typedef struct tcp_hsp { 766 struct tcp_hsp *tcp_hsp_next; 767 in6_addr_t tcp_hsp_addr_v6; 768 in6_addr_t tcp_hsp_subnet_v6; 769 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 770 int32_t tcp_hsp_sendspace; 771 int32_t tcp_hsp_recvspace; 772 int32_t tcp_hsp_tstamp; 773 } tcp_hsp_t; 774 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 775 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 776 777 /* 778 * Functions called directly via squeue having a prototype of edesc_t. 779 */ 780 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 781 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 782 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 783 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 784 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 785 void tcp_input(void *arg, mblk_t *mp, void *arg2); 786 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 787 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 788 void tcp_output(void *arg, mblk_t *mp, void *arg2); 789 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 790 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 791 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 792 793 794 /* Prototype for TCP functions */ 795 static void tcp_random_init(void); 796 int tcp_random(void); 797 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 798 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 799 tcp_t *eager); 800 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 801 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 802 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 803 boolean_t user_specified); 804 static void tcp_closei_local(tcp_t *tcp); 805 static void tcp_close_detached(tcp_t *tcp); 806 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 807 mblk_t *idmp, mblk_t **defermp); 808 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 809 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 810 in_port_t dstport, uint_t srcid); 811 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 812 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 813 uint32_t scope_id); 814 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 815 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 816 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 817 static char *tcp_display(tcp_t *tcp, char *, char); 818 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 819 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 820 static void tcp_eager_unlink(tcp_t *tcp); 821 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 822 int unixerr); 823 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 824 int tlierr, int unixerr); 825 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 826 cred_t *cr); 827 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 828 char *value, caddr_t cp, cred_t *cr); 829 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 830 char *value, caddr_t cp, cred_t *cr); 831 static int tcp_tpistate(tcp_t *tcp); 832 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 833 int caller_holds_lock); 834 static void tcp_bind_hash_remove(tcp_t *tcp); 835 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id); 836 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 837 static void tcp_acceptor_hash_remove(tcp_t *tcp); 838 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 839 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 840 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 841 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 842 static int tcp_header_init_ipv4(tcp_t *tcp); 843 static int tcp_header_init_ipv6(tcp_t *tcp); 844 int tcp_init(tcp_t *tcp, queue_t *q); 845 static int tcp_init_values(tcp_t *tcp); 846 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 847 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 848 t_scalar_t addr_length); 849 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 850 static void tcp_ip_notify(tcp_t *tcp); 851 static mblk_t *tcp_ire_mp(mblk_t *mp); 852 static void tcp_iss_init(tcp_t *tcp); 853 static void tcp_keepalive_killer(void *arg); 854 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 855 static void tcp_mss_set(tcp_t *tcp, uint32_t size); 856 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 857 int *do_disconnectp, int *t_errorp, int *sys_errorp); 858 static boolean_t tcp_allow_connopt_set(int level, int name); 859 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 860 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 861 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 862 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 863 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 864 mblk_t *mblk); 865 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 866 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 867 uchar_t *ptr, uint_t len); 868 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 869 static boolean_t tcp_param_register(tcpparam_t *tcppa, int cnt); 870 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 871 caddr_t cp, cred_t *cr); 872 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 873 caddr_t cp, cred_t *cr); 874 static void tcp_iss_key_init(uint8_t *phrase, int len); 875 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 876 caddr_t cp, cred_t *cr); 877 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 878 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 879 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 880 static void tcp_reinit(tcp_t *tcp); 881 static void tcp_reinit_values(tcp_t *tcp); 882 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 883 tcp_t *thisstream, cred_t *cr); 884 885 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 886 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 887 static boolean_t tcp_send_rst_chk(void); 888 static void tcp_ss_rexmit(tcp_t *tcp); 889 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 890 static void tcp_process_options(tcp_t *, tcph_t *); 891 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 892 static void tcp_rsrv(queue_t *q); 893 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 894 static int tcp_snmp_state(tcp_t *tcp); 895 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 896 cred_t *cr); 897 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 898 cred_t *cr); 899 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 900 cred_t *cr); 901 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 902 cred_t *cr); 903 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 904 cred_t *cr); 905 static int tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, 906 caddr_t cp, cred_t *cr); 907 static int tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, 908 caddr_t cp, cred_t *cr); 909 static int tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, 910 cred_t *cr); 911 static void tcp_timer(void *arg); 912 static void tcp_timer_callback(void *); 913 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 914 boolean_t random); 915 static in_port_t tcp_get_next_priv_port(const tcp_t *); 916 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 917 void tcp_wput_accept(queue_t *q, mblk_t *mp); 918 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 919 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 920 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 921 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 922 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 923 const int num_sack_blk, int *usable, uint_t *snxt, 924 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 925 const int mdt_thres); 926 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 927 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 928 const int num_sack_blk, int *usable, uint_t *snxt, 929 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 930 const int mdt_thres); 931 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 932 int num_sack_blk); 933 static void tcp_wsrv(queue_t *q); 934 static int tcp_xmit_end(tcp_t *tcp); 935 static void tcp_ack_timer(void *arg); 936 static mblk_t *tcp_ack_mp(tcp_t *tcp); 937 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 938 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 939 zoneid_t zoneid); 940 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 941 uint32_t ack, int ctl); 942 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr); 943 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr); 944 static int setmaxps(queue_t *q, int maxpsz); 945 static void tcp_set_rto(tcp_t *, time_t); 946 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 947 boolean_t, boolean_t); 948 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 949 boolean_t ipsec_mctl); 950 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 951 char *opt, int optlen); 952 static int tcp_build_hdrs(queue_t *, tcp_t *); 953 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 954 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 955 tcph_t *tcph); 956 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 957 boolean_t tcp_reserved_port_add(int, in_port_t *, in_port_t *); 958 boolean_t tcp_reserved_port_del(in_port_t, in_port_t); 959 boolean_t tcp_reserved_port_check(in_port_t); 960 static tcp_t *tcp_alloc_temp_tcp(in_port_t); 961 static int tcp_reserved_port_list(queue_t *, mblk_t *, caddr_t, cred_t *); 962 static mblk_t *tcp_mdt_info_mp(mblk_t *); 963 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 964 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 965 const boolean_t, const uint32_t, const uint32_t, 966 const uint32_t, const uint32_t); 967 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 968 const uint_t, const uint_t, boolean_t *); 969 static mblk_t *tcp_lso_info_mp(mblk_t *); 970 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 971 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 972 extern mblk_t *tcp_timermp_alloc(int); 973 extern void tcp_timermp_free(tcp_t *); 974 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 975 static void tcp_stop_lingering(tcp_t *tcp); 976 static void tcp_close_linger_timeout(void *arg); 977 void tcp_ddi_init(void); 978 void tcp_ddi_destroy(void); 979 static void tcp_kstat_init(void); 980 static void tcp_kstat_fini(void); 981 static int tcp_kstat_update(kstat_t *kp, int rw); 982 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 983 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 984 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 985 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 986 tcph_t *tcph, mblk_t *idmp); 987 static squeue_func_t tcp_squeue_switch(int); 988 989 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *); 990 static int tcp_close(queue_t *, int); 991 static int tcpclose_accept(queue_t *); 992 static int tcp_modclose(queue_t *); 993 static void tcp_wput_mod(queue_t *, mblk_t *); 994 995 static void tcp_squeue_add(squeue_t *); 996 static boolean_t tcp_zcopy_check(tcp_t *); 997 static void tcp_zcopy_notify(tcp_t *); 998 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 999 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 1000 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 1001 1002 extern void tcp_kssl_input(tcp_t *, mblk_t *); 1003 1004 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 1005 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 1006 1007 /* 1008 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 1009 * 1010 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 1011 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 1012 * (defined in tcp.h) needs to be filled in and passed into the kernel 1013 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 1014 * structure contains the four-tuple of a TCP connection and a range of TCP 1015 * states (specified by ac_start and ac_end). The use of wildcard addresses 1016 * and ports is allowed. Connections with a matching four tuple and a state 1017 * within the specified range will be aborted. The valid states for the 1018 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 1019 * inclusive. 1020 * 1021 * An application which has its connection aborted by this ioctl will receive 1022 * an error that is dependent on the connection state at the time of the abort. 1023 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 1024 * though a RST packet has been received. If the connection state is equal to 1025 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 1026 * and all resources associated with the connection will be freed. 1027 */ 1028 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 1029 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 1030 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 1031 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *); 1032 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 1033 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 1034 boolean_t); 1035 1036 static struct module_info tcp_rinfo = { 1037 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 1038 }; 1039 1040 static struct module_info tcp_winfo = { 1041 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 1042 }; 1043 1044 /* 1045 * Entry points for TCP as a module. It only allows SNMP requests 1046 * to pass through. 1047 */ 1048 struct qinit tcp_mod_rinit = { 1049 (pfi_t)putnext, NULL, tcp_open, ip_snmpmod_close, NULL, &tcp_rinfo, 1050 }; 1051 1052 struct qinit tcp_mod_winit = { 1053 (pfi_t)ip_snmpmod_wput, NULL, tcp_open, ip_snmpmod_close, NULL, 1054 &tcp_rinfo 1055 }; 1056 1057 /* 1058 * Entry points for TCP as a device. The normal case which supports 1059 * the TCP functionality. 1060 */ 1061 struct qinit tcp_rinit = { 1062 NULL, (pfi_t)tcp_rsrv, tcp_open, tcp_close, NULL, &tcp_rinfo 1063 }; 1064 1065 struct qinit tcp_winit = { 1066 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1067 }; 1068 1069 /* Initial entry point for TCP in socket mode. */ 1070 struct qinit tcp_sock_winit = { 1071 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1072 }; 1073 1074 /* 1075 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 1076 * an accept. Avoid allocating data structures since eager has already 1077 * been created. 1078 */ 1079 struct qinit tcp_acceptor_rinit = { 1080 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1081 }; 1082 1083 struct qinit tcp_acceptor_winit = { 1084 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1085 }; 1086 1087 /* 1088 * Entry points for TCP loopback (read side only) 1089 */ 1090 struct qinit tcp_loopback_rinit = { 1091 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_open, tcp_close, (pfi_t)0, 1092 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1093 }; 1094 1095 struct streamtab tcpinfo = { 1096 &tcp_rinit, &tcp_winit 1097 }; 1098 1099 extern squeue_func_t tcp_squeue_wput_proc; 1100 extern squeue_func_t tcp_squeue_timer_proc; 1101 1102 /* Protected by tcp_g_q_lock */ 1103 static queue_t *tcp_g_q; /* Default queue used during detached closes */ 1104 kmutex_t tcp_g_q_lock; 1105 1106 /* Protected by tcp_hsp_lock */ 1107 /* 1108 * XXX The host param mechanism should go away and instead we should use 1109 * the metrics associated with the routes to determine the default sndspace 1110 * and rcvspace. 1111 */ 1112 static tcp_hsp_t **tcp_hsp_hash; /* Hash table for HSPs */ 1113 krwlock_t tcp_hsp_lock; 1114 1115 /* 1116 * Extra privileged ports. In host byte order. 1117 * Protected by tcp_epriv_port_lock. 1118 */ 1119 #define TCP_NUM_EPRIV_PORTS 64 1120 static int tcp_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 1121 static uint16_t tcp_g_epriv_ports[TCP_NUM_EPRIV_PORTS] = { 2049, 4045 }; 1122 kmutex_t tcp_epriv_port_lock; 1123 1124 /* 1125 * The smallest anonymous port in the privileged port range which TCP 1126 * looks for free port. Use in the option TCP_ANONPRIVBIND. 1127 */ 1128 static in_port_t tcp_min_anonpriv_port = 512; 1129 1130 /* Only modified during _init and _fini thus no locking is needed. */ 1131 static caddr_t tcp_g_nd; /* Head of 'named dispatch' variable list */ 1132 1133 /* Hint not protected by any lock */ 1134 static uint_t tcp_next_port_to_try; 1135 1136 1137 /* TCP bind hash list - all tcp_t with state >= BOUND. */ 1138 tf_t tcp_bind_fanout[TCP_BIND_FANOUT_SIZE]; 1139 1140 /* TCP queue hash list - all tcp_t in case they will be an acceptor. */ 1141 static tf_t tcp_acceptor_fanout[TCP_FANOUT_SIZE]; 1142 1143 /* 1144 * TCP has a private interface for other kernel modules to reserve a 1145 * port range for them to use. Once reserved, TCP will not use any ports 1146 * in the range. This interface relies on the TCP_EXCLBIND feature. If 1147 * the semantics of TCP_EXCLBIND is changed, implementation of this interface 1148 * has to be verified. 1149 * 1150 * There can be TCP_RESERVED_PORTS_ARRAY_MAX_SIZE port ranges. Each port 1151 * range can cover at most TCP_RESERVED_PORTS_RANGE_MAX ports. A port 1152 * range is [port a, port b] inclusive. And each port range is between 1153 * TCP_LOWESET_RESERVED_PORT and TCP_LARGEST_RESERVED_PORT inclusive. 1154 * 1155 * Note that the default anonymous port range starts from 32768. There is 1156 * no port "collision" between that and the reserved port range. If there 1157 * is port collision (because the default smallest anonymous port is lowered 1158 * or some apps specifically bind to ports in the reserved port range), the 1159 * system may not be able to reserve a port range even there are enough 1160 * unbound ports as a reserved port range contains consecutive ports . 1161 */ 1162 #define TCP_RESERVED_PORTS_ARRAY_MAX_SIZE 5 1163 #define TCP_RESERVED_PORTS_RANGE_MAX 1000 1164 #define TCP_SMALLEST_RESERVED_PORT 10240 1165 #define TCP_LARGEST_RESERVED_PORT 20480 1166 1167 /* Structure to represent those reserved port ranges. */ 1168 typedef struct tcp_rport_s { 1169 in_port_t lo_port; 1170 in_port_t hi_port; 1171 tcp_t **temp_tcp_array; 1172 } tcp_rport_t; 1173 1174 /* The reserved port array. */ 1175 static tcp_rport_t tcp_reserved_port[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 1176 1177 /* Locks to protect the tcp_reserved_ports array. */ 1178 static krwlock_t tcp_reserved_port_lock; 1179 1180 /* The number of ranges in the array. */ 1181 uint32_t tcp_reserved_port_array_size = 0; 1182 1183 /* 1184 * MIB-2 stuff for SNMP 1185 * Note: tcpInErrs {tcp 15} is accumulated in ip.c 1186 */ 1187 mib2_tcp_t tcp_mib; /* SNMP fixed size info */ 1188 kstat_t *tcp_mibkp; /* kstat exporting tcp_mib data */ 1189 1190 boolean_t tcp_icmp_source_quench = B_FALSE; 1191 /* 1192 * Following assumes TPI alignment requirements stay along 32 bit 1193 * boundaries 1194 */ 1195 #define ROUNDUP32(x) \ 1196 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1197 1198 /* Template for response to info request. */ 1199 static struct T_info_ack tcp_g_t_info_ack = { 1200 T_INFO_ACK, /* PRIM_type */ 1201 0, /* TSDU_size */ 1202 T_INFINITE, /* ETSDU_size */ 1203 T_INVALID, /* CDATA_size */ 1204 T_INVALID, /* DDATA_size */ 1205 sizeof (sin_t), /* ADDR_size */ 1206 0, /* OPT_size - not initialized here */ 1207 TIDUSZ, /* TIDU_size */ 1208 T_COTS_ORD, /* SERV_type */ 1209 TCPS_IDLE, /* CURRENT_state */ 1210 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1211 }; 1212 1213 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1214 T_INFO_ACK, /* PRIM_type */ 1215 0, /* TSDU_size */ 1216 T_INFINITE, /* ETSDU_size */ 1217 T_INVALID, /* CDATA_size */ 1218 T_INVALID, /* DDATA_size */ 1219 sizeof (sin6_t), /* ADDR_size */ 1220 0, /* OPT_size - not initialized here */ 1221 TIDUSZ, /* TIDU_size */ 1222 T_COTS_ORD, /* SERV_type */ 1223 TCPS_IDLE, /* CURRENT_state */ 1224 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1225 }; 1226 1227 #define MS 1L 1228 #define SECONDS (1000 * MS) 1229 #define MINUTES (60 * SECONDS) 1230 #define HOURS (60 * MINUTES) 1231 #define DAYS (24 * HOURS) 1232 1233 #define PARAM_MAX (~(uint32_t)0) 1234 1235 /* Max size IP datagram is 64k - 1 */ 1236 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1237 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1238 /* Max of the above */ 1239 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1240 1241 /* Largest TCP port number */ 1242 #define TCP_MAX_PORT (64 * 1024 - 1) 1243 1244 /* 1245 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1246 * layer header. It has to be a multiple of 4. 1247 */ 1248 static tcpparam_t tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1249 #define tcp_wroff_xtra tcp_wroff_xtra_param.tcp_param_val 1250 1251 /* 1252 * All of these are alterable, within the min/max values given, at run time. 1253 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1254 * per the TCP spec. 1255 */ 1256 /* BEGIN CSTYLED */ 1257 tcpparam_t tcp_param_arr[] = { 1258 /*min max value name */ 1259 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1260 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1261 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1262 { 1, 1024, 1, "tcp_conn_req_min" }, 1263 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1264 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1265 { 0, 10, 0, "tcp_debug" }, 1266 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1267 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1268 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1269 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1270 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1271 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1272 { 1, 255, 64, "tcp_ipv4_ttl"}, 1273 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1274 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1275 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1276 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1277 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1278 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1279 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1280 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1281 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1282 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1283 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1284 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1285 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1286 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1287 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1288 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1289 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1290 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1291 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1292 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1293 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1294 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1295 { 0, TCP_MSS_MAX, 64, "tcp_co_min"}, 1296 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1297 /* 1298 * Question: What default value should I set for tcp_strong_iss? 1299 */ 1300 { 0, 2, 1, "tcp_strong_iss"}, 1301 { 0, 65536, 20, "tcp_rtt_updates"}, 1302 { 0, 1, 1, "tcp_wscale_always"}, 1303 { 0, 1, 0, "tcp_tstamp_always"}, 1304 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1305 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1306 { 0, 16, 2, "tcp_deferred_acks_max"}, 1307 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1308 { 1, 4, 4, "tcp_slow_start_initial"}, 1309 { 10*MS, 50*MS, 20*MS, "tcp_co_timer_interval"}, 1310 { 0, 2, 2, "tcp_sack_permitted"}, 1311 { 0, 1, 0, "tcp_trace"}, 1312 { 0, 1, 1, "tcp_compression_enabled"}, 1313 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1314 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1315 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1316 { 0, 1, 0, "tcp_rev_src_routes"}, 1317 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1318 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1319 { 0, 16, 8, "tcp_local_dacks_max"}, 1320 { 0, 2, 1, "tcp_ecn_permitted"}, 1321 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1322 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1323 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1324 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1325 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1326 }; 1327 /* END CSTYLED */ 1328 1329 /* 1330 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1331 * each header fragment in the header buffer. Each parameter value has 1332 * to be a multiple of 4 (32-bit aligned). 1333 */ 1334 static tcpparam_t tcp_mdt_head_param = { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1335 static tcpparam_t tcp_mdt_tail_param = { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1336 #define tcp_mdt_hdr_head_min tcp_mdt_head_param.tcp_param_val 1337 #define tcp_mdt_hdr_tail_min tcp_mdt_tail_param.tcp_param_val 1338 1339 /* 1340 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1341 * the maximum number of payload buffers associated per Multidata. 1342 */ 1343 static tcpparam_t tcp_mdt_max_pbufs_param = 1344 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1345 #define tcp_mdt_max_pbufs tcp_mdt_max_pbufs_param.tcp_param_val 1346 1347 /* Round up the value to the nearest mss. */ 1348 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1349 1350 /* 1351 * Set ECN capable transport (ECT) code point in IP header. 1352 * 1353 * Note that there are 2 ECT code points '01' and '10', which are called 1354 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1355 * point ECT(0) for TCP as described in RFC 2481. 1356 */ 1357 #define SET_ECT(tcp, iph) \ 1358 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1359 /* We need to clear the code point first. */ \ 1360 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1361 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1362 } else { \ 1363 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1364 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1365 } 1366 1367 /* 1368 * The format argument to pass to tcp_display(). 1369 * DISP_PORT_ONLY means that the returned string has only port info. 1370 * DISP_ADDR_AND_PORT means that the returned string also contains the 1371 * remote and local IP address. 1372 */ 1373 #define DISP_PORT_ONLY 1 1374 #define DISP_ADDR_AND_PORT 2 1375 1376 /* 1377 * This controls the rate some ndd info report functions can be used 1378 * by non-privileged users. It stores the last time such info is 1379 * requested. When those report functions are called again, this 1380 * is checked with the current time and compare with the ndd param 1381 * tcp_ndd_get_info_interval. 1382 */ 1383 static clock_t tcp_last_ndd_get_info_time = 0; 1384 #define NDD_TOO_QUICK_MSG \ 1385 "ndd get info rate too high for non-privileged users, try again " \ 1386 "later.\n" 1387 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1388 1389 #define IS_VMLOANED_MBLK(mp) \ 1390 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1391 1392 /* 1393 * These two variables control the rate for TCP to generate RSTs in 1394 * response to segments not belonging to any connections. We limit 1395 * TCP to sent out tcp_rst_sent_rate (ndd param) number of RSTs in 1396 * each 1 second interval. This is to protect TCP against DoS attack. 1397 */ 1398 static clock_t tcp_last_rst_intrvl; 1399 static uint32_t tcp_rst_cnt; 1400 1401 /* The number of RST not sent because of the rate limit. */ 1402 static uint32_t tcp_rst_unsent; 1403 1404 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1405 boolean_t tcp_mdt_chain = B_TRUE; 1406 1407 /* 1408 * MDT threshold in the form of effective send MSS multiplier; we take 1409 * the MDT path if the amount of unsent data exceeds the threshold value 1410 * (default threshold is 1*SMSS). 1411 */ 1412 uint_t tcp_mdt_smss_threshold = 1; 1413 1414 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1415 1416 /* 1417 * Forces all connections to obey the value of the tcp_maxpsz_multiplier 1418 * tunable settable via NDD. Otherwise, the per-connection behavior is 1419 * determined dynamically during tcp_adapt_ire(), which is the default. 1420 */ 1421 boolean_t tcp_static_maxpsz = B_FALSE; 1422 1423 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1424 uint32_t tcp_random_anon_port = 1; 1425 1426 /* 1427 * To reach to an eager in Q0 which can be dropped due to an incoming 1428 * new SYN request when Q0 is full, a new doubly linked list is 1429 * introduced. This list allows to select an eager from Q0 in O(1) time. 1430 * This is needed to avoid spending too much time walking through the 1431 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1432 * this new list has to be a member of Q0. 1433 * This list is headed by listener's tcp_t. When the list is empty, 1434 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1435 * of listener's tcp_t point to listener's tcp_t itself. 1436 * 1437 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1438 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1439 * These macros do not affect the eager's membership to Q0. 1440 */ 1441 1442 1443 #define MAKE_DROPPABLE(listener, eager) \ 1444 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1445 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1446 = (eager); \ 1447 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1448 (eager)->tcp_eager_next_drop_q0 = \ 1449 (listener)->tcp_eager_next_drop_q0; \ 1450 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1451 } 1452 1453 #define MAKE_UNDROPPABLE(eager) \ 1454 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1455 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1456 = (eager)->tcp_eager_prev_drop_q0; \ 1457 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1458 = (eager)->tcp_eager_next_drop_q0; \ 1459 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1460 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1461 } 1462 1463 /* 1464 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1465 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1466 * data, TCP will not respond with an ACK. RFC 793 requires that 1467 * TCP responds with an ACK for such a bogus ACK. By not following 1468 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1469 * an attacker successfully spoofs an acceptable segment to our 1470 * peer; or when our peer is "confused." 1471 */ 1472 uint32_t tcp_drop_ack_unsent_cnt = 10; 1473 1474 /* 1475 * Hook functions to enable cluster networking 1476 * On non-clustered systems these vectors must always be NULL. 1477 */ 1478 1479 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1480 uint8_t *laddrp, in_port_t lport) = NULL; 1481 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1482 uint8_t *laddrp, in_port_t lport) = NULL; 1483 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1484 uint8_t *laddrp, in_port_t lport, 1485 uint8_t *faddrp, in_port_t fport) = NULL; 1486 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1487 uint8_t *laddrp, in_port_t lport, 1488 uint8_t *faddrp, in_port_t fport) = NULL; 1489 1490 /* 1491 * The following are defined in ip.c 1492 */ 1493 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1494 uint8_t *laddrp); 1495 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1496 uint8_t *laddrp, uint8_t *faddrp); 1497 1498 #define CL_INET_CONNECT(tcp) { \ 1499 if (cl_inet_connect != NULL) { \ 1500 /* \ 1501 * Running in cluster mode - register active connection \ 1502 * information \ 1503 */ \ 1504 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1505 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1506 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1507 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1508 (in_port_t)(tcp)->tcp_lport, \ 1509 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1510 (in_port_t)(tcp)->tcp_fport); \ 1511 } \ 1512 } else { \ 1513 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1514 &(tcp)->tcp_ip6h->ip6_src)) {\ 1515 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1516 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1517 (in_port_t)(tcp)->tcp_lport, \ 1518 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1519 (in_port_t)(tcp)->tcp_fport); \ 1520 } \ 1521 } \ 1522 } \ 1523 } 1524 1525 #define CL_INET_DISCONNECT(tcp) { \ 1526 if (cl_inet_disconnect != NULL) { \ 1527 /* \ 1528 * Running in cluster mode - deregister active \ 1529 * connection information \ 1530 */ \ 1531 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1532 if ((tcp)->tcp_ip_src != 0) { \ 1533 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1534 AF_INET, \ 1535 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1536 (in_port_t)(tcp)->tcp_lport, \ 1537 (uint8_t *) \ 1538 (&((tcp)->tcp_ipha->ipha_dst)),\ 1539 (in_port_t)(tcp)->tcp_fport); \ 1540 } \ 1541 } else { \ 1542 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1543 &(tcp)->tcp_ip_src_v6)) { \ 1544 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1545 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1546 (in_port_t)(tcp)->tcp_lport, \ 1547 (uint8_t *) \ 1548 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1549 (in_port_t)(tcp)->tcp_fport); \ 1550 } \ 1551 } \ 1552 } \ 1553 } 1554 1555 /* 1556 * Cluster networking hook for traversing current connection list. 1557 * This routine is used to extract the current list of live connections 1558 * which must continue to to be dispatched to this node. 1559 */ 1560 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1561 1562 /* 1563 * Figure out the value of window scale opton. Note that the rwnd is 1564 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1565 * We cannot find the scale value and then do a round up of tcp_rwnd 1566 * because the scale value may not be correct after that. 1567 * 1568 * Set the compiler flag to make this function inline. 1569 */ 1570 static void 1571 tcp_set_ws_value(tcp_t *tcp) 1572 { 1573 int i; 1574 uint32_t rwnd = tcp->tcp_rwnd; 1575 1576 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1577 i++, rwnd >>= 1) 1578 ; 1579 tcp->tcp_rcv_ws = i; 1580 } 1581 1582 /* 1583 * Remove a connection from the list of detached TIME_WAIT connections. 1584 * It returns B_FALSE if it can't remove the connection from the list 1585 * as the connection has already been removed from the list due to an 1586 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1587 */ 1588 static boolean_t 1589 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1590 { 1591 boolean_t locked = B_FALSE; 1592 1593 if (tcp_time_wait == NULL) { 1594 tcp_time_wait = *((tcp_squeue_priv_t **) 1595 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1596 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1597 locked = B_TRUE; 1598 } 1599 1600 if (tcp->tcp_time_wait_expire == 0) { 1601 ASSERT(tcp->tcp_time_wait_next == NULL); 1602 ASSERT(tcp->tcp_time_wait_prev == NULL); 1603 if (locked) 1604 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1605 return (B_FALSE); 1606 } 1607 ASSERT(TCP_IS_DETACHED(tcp)); 1608 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1609 1610 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1611 ASSERT(tcp->tcp_time_wait_prev == NULL); 1612 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1613 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1614 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1615 NULL; 1616 } else { 1617 tcp_time_wait->tcp_time_wait_tail = NULL; 1618 } 1619 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1620 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1621 ASSERT(tcp->tcp_time_wait_next == NULL); 1622 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1623 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1624 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1625 } else { 1626 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1627 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1628 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1629 tcp->tcp_time_wait_next; 1630 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1631 tcp->tcp_time_wait_prev; 1632 } 1633 tcp->tcp_time_wait_next = NULL; 1634 tcp->tcp_time_wait_prev = NULL; 1635 tcp->tcp_time_wait_expire = 0; 1636 1637 if (locked) 1638 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1639 return (B_TRUE); 1640 } 1641 1642 /* 1643 * Add a connection to the list of detached TIME_WAIT connections 1644 * and set its time to expire. 1645 */ 1646 static void 1647 tcp_time_wait_append(tcp_t *tcp) 1648 { 1649 tcp_squeue_priv_t *tcp_time_wait = 1650 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1651 SQPRIVATE_TCP)); 1652 1653 tcp_timers_stop(tcp); 1654 1655 /* Freed above */ 1656 ASSERT(tcp->tcp_timer_tid == 0); 1657 ASSERT(tcp->tcp_ack_tid == 0); 1658 1659 /* must have happened at the time of detaching the tcp */ 1660 ASSERT(tcp->tcp_ptpahn == NULL); 1661 ASSERT(tcp->tcp_flow_stopped == 0); 1662 ASSERT(tcp->tcp_time_wait_next == NULL); 1663 ASSERT(tcp->tcp_time_wait_prev == NULL); 1664 ASSERT(tcp->tcp_time_wait_expire == NULL); 1665 ASSERT(tcp->tcp_listener == NULL); 1666 1667 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1668 /* 1669 * The value computed below in tcp->tcp_time_wait_expire may 1670 * appear negative or wrap around. That is ok since our 1671 * interest is only in the difference between the current lbolt 1672 * value and tcp->tcp_time_wait_expire. But the value should not 1673 * be zero, since it means the tcp is not in the TIME_WAIT list. 1674 * The corresponding comparison in tcp_time_wait_collector() uses 1675 * modular arithmetic. 1676 */ 1677 tcp->tcp_time_wait_expire += 1678 drv_usectohz(tcp_time_wait_interval * 1000); 1679 if (tcp->tcp_time_wait_expire == 0) 1680 tcp->tcp_time_wait_expire = 1; 1681 1682 ASSERT(TCP_IS_DETACHED(tcp)); 1683 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1684 ASSERT(tcp->tcp_time_wait_next == NULL); 1685 ASSERT(tcp->tcp_time_wait_prev == NULL); 1686 TCP_DBGSTAT(tcp_time_wait); 1687 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1688 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1689 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1690 tcp_time_wait->tcp_time_wait_head = tcp; 1691 } else { 1692 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1693 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1694 TCPS_TIME_WAIT); 1695 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1696 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1697 } 1698 tcp_time_wait->tcp_time_wait_tail = tcp; 1699 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1700 } 1701 1702 /* ARGSUSED */ 1703 void 1704 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1705 { 1706 conn_t *connp = (conn_t *)arg; 1707 tcp_t *tcp = connp->conn_tcp; 1708 1709 ASSERT(tcp != NULL); 1710 if (tcp->tcp_state == TCPS_CLOSED) { 1711 return; 1712 } 1713 1714 ASSERT((tcp->tcp_family == AF_INET && 1715 tcp->tcp_ipversion == IPV4_VERSION) || 1716 (tcp->tcp_family == AF_INET6 && 1717 (tcp->tcp_ipversion == IPV4_VERSION || 1718 tcp->tcp_ipversion == IPV6_VERSION))); 1719 ASSERT(!tcp->tcp_listener); 1720 1721 TCP_STAT(tcp_time_wait_reap); 1722 ASSERT(TCP_IS_DETACHED(tcp)); 1723 1724 /* 1725 * Because they have no upstream client to rebind or tcp_close() 1726 * them later, we axe the connection here and now. 1727 */ 1728 tcp_close_detached(tcp); 1729 } 1730 1731 void 1732 tcp_cleanup(tcp_t *tcp) 1733 { 1734 mblk_t *mp; 1735 char *tcp_iphc; 1736 int tcp_iphc_len; 1737 int tcp_hdr_grown; 1738 tcp_sack_info_t *tcp_sack_info; 1739 conn_t *connp = tcp->tcp_connp; 1740 1741 tcp_bind_hash_remove(tcp); 1742 tcp_free(tcp); 1743 1744 /* Release any SSL context */ 1745 if (tcp->tcp_kssl_ent != NULL) { 1746 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1747 tcp->tcp_kssl_ent = NULL; 1748 } 1749 1750 if (tcp->tcp_kssl_ctx != NULL) { 1751 kssl_release_ctx(tcp->tcp_kssl_ctx); 1752 tcp->tcp_kssl_ctx = NULL; 1753 } 1754 tcp->tcp_kssl_pending = B_FALSE; 1755 1756 conn_delete_ire(connp, NULL); 1757 if (connp->conn_flags & IPCL_TCPCONN) { 1758 if (connp->conn_latch != NULL) 1759 IPLATCH_REFRELE(connp->conn_latch); 1760 if (connp->conn_policy != NULL) 1761 IPPH_REFRELE(connp->conn_policy); 1762 } 1763 1764 /* 1765 * Since we will bzero the entire structure, we need to 1766 * remove it and reinsert it in global hash list. We 1767 * know the walkers can't get to this conn because we 1768 * had set CONDEMNED flag earlier and checked reference 1769 * under conn_lock so walker won't pick it and when we 1770 * go the ipcl_globalhash_remove() below, no walker 1771 * can get to it. 1772 */ 1773 ipcl_globalhash_remove(connp); 1774 1775 /* Save some state */ 1776 mp = tcp->tcp_timercache; 1777 1778 tcp_sack_info = tcp->tcp_sack_info; 1779 tcp_iphc = tcp->tcp_iphc; 1780 tcp_iphc_len = tcp->tcp_iphc_len; 1781 tcp_hdr_grown = tcp->tcp_hdr_grown; 1782 1783 if (connp->conn_cred != NULL) 1784 crfree(connp->conn_cred); 1785 if (connp->conn_peercred != NULL) 1786 crfree(connp->conn_peercred); 1787 bzero(connp, sizeof (conn_t)); 1788 bzero(tcp, sizeof (tcp_t)); 1789 1790 /* restore the state */ 1791 tcp->tcp_timercache = mp; 1792 1793 tcp->tcp_sack_info = tcp_sack_info; 1794 tcp->tcp_iphc = tcp_iphc; 1795 tcp->tcp_iphc_len = tcp_iphc_len; 1796 tcp->tcp_hdr_grown = tcp_hdr_grown; 1797 1798 1799 tcp->tcp_connp = connp; 1800 1801 connp->conn_tcp = tcp; 1802 connp->conn_flags = IPCL_TCPCONN; 1803 connp->conn_state_flags = CONN_INCIPIENT; 1804 connp->conn_ulp = IPPROTO_TCP; 1805 connp->conn_ref = 1; 1806 1807 ipcl_globalhash_insert(connp); 1808 } 1809 1810 /* 1811 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1812 * is done forwards from the head. 1813 */ 1814 /* ARGSUSED */ 1815 void 1816 tcp_time_wait_collector(void *arg) 1817 { 1818 tcp_t *tcp; 1819 clock_t now; 1820 mblk_t *mp; 1821 conn_t *connp; 1822 kmutex_t *lock; 1823 boolean_t removed; 1824 1825 squeue_t *sqp = (squeue_t *)arg; 1826 tcp_squeue_priv_t *tcp_time_wait = 1827 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1828 1829 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1830 tcp_time_wait->tcp_time_wait_tid = 0; 1831 1832 if (tcp_time_wait->tcp_free_list != NULL && 1833 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1834 TCP_STAT(tcp_freelist_cleanup); 1835 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1836 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1837 CONN_DEC_REF(tcp->tcp_connp); 1838 } 1839 tcp_time_wait->tcp_free_list_cnt = 0; 1840 } 1841 1842 /* 1843 * In order to reap time waits reliably, we should use a 1844 * source of time that is not adjustable by the user -- hence 1845 * the call to ddi_get_lbolt(). 1846 */ 1847 now = ddi_get_lbolt(); 1848 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1849 /* 1850 * Compare times using modular arithmetic, since 1851 * lbolt can wrapover. 1852 */ 1853 if ((now - tcp->tcp_time_wait_expire) < 0) { 1854 break; 1855 } 1856 1857 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1858 ASSERT(removed); 1859 1860 connp = tcp->tcp_connp; 1861 ASSERT(connp->conn_fanout != NULL); 1862 lock = &connp->conn_fanout->connf_lock; 1863 /* 1864 * This is essentially a TW reclaim fast path optimization for 1865 * performance where the timewait collector checks under the 1866 * fanout lock (so that no one else can get access to the 1867 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1868 * the classifier hash list. If ref count is indeed 2, we can 1869 * just remove the conn under the fanout lock and avoid 1870 * cleaning up the conn under the squeue, provided that 1871 * clustering callbacks are not enabled. If clustering is 1872 * enabled, we need to make the clustering callback before 1873 * setting the CONDEMNED flag and after dropping all locks and 1874 * so we forego this optimization and fall back to the slow 1875 * path. Also please see the comments in tcp_closei_local 1876 * regarding the refcnt logic. 1877 * 1878 * Since we are holding the tcp_time_wait_lock, its better 1879 * not to block on the fanout_lock because other connections 1880 * can't add themselves to time_wait list. So we do a 1881 * tryenter instead of mutex_enter. 1882 */ 1883 if (mutex_tryenter(lock)) { 1884 mutex_enter(&connp->conn_lock); 1885 if ((connp->conn_ref == 2) && 1886 (cl_inet_disconnect == NULL)) { 1887 ipcl_hash_remove_locked(connp, 1888 connp->conn_fanout); 1889 /* 1890 * Set the CONDEMNED flag now itself so that 1891 * the refcnt cannot increase due to any 1892 * walker. But we have still not cleaned up 1893 * conn_ire_cache. This is still ok since 1894 * we are going to clean it up in tcp_cleanup 1895 * immediately and any interface unplumb 1896 * thread will wait till the ire is blown away 1897 */ 1898 connp->conn_state_flags |= CONN_CONDEMNED; 1899 mutex_exit(lock); 1900 mutex_exit(&connp->conn_lock); 1901 if (tcp_time_wait->tcp_free_list_cnt < 1902 tcp_free_list_max_cnt) { 1903 /* Add to head of tcp_free_list */ 1904 mutex_exit( 1905 &tcp_time_wait->tcp_time_wait_lock); 1906 tcp_cleanup(tcp); 1907 mutex_enter( 1908 &tcp_time_wait->tcp_time_wait_lock); 1909 tcp->tcp_time_wait_next = 1910 tcp_time_wait->tcp_free_list; 1911 tcp_time_wait->tcp_free_list = tcp; 1912 tcp_time_wait->tcp_free_list_cnt++; 1913 continue; 1914 } else { 1915 /* Do not add to tcp_free_list */ 1916 mutex_exit( 1917 &tcp_time_wait->tcp_time_wait_lock); 1918 tcp_bind_hash_remove(tcp); 1919 conn_delete_ire(tcp->tcp_connp, NULL); 1920 CONN_DEC_REF(tcp->tcp_connp); 1921 } 1922 } else { 1923 CONN_INC_REF_LOCKED(connp); 1924 mutex_exit(lock); 1925 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1926 mutex_exit(&connp->conn_lock); 1927 /* 1928 * We can reuse the closemp here since conn has 1929 * detached (otherwise we wouldn't even be in 1930 * time_wait list). tcp_closemp_used can safely 1931 * be changed without taking a lock as no other 1932 * thread can concurrently access it at this 1933 * point in the connection lifecycle. We 1934 * increment tcp_closemp_used to record any 1935 * attempt to reuse tcp_closemp while it is 1936 * still in use. 1937 */ 1938 1939 if (tcp->tcp_closemp.b_prev == NULL) 1940 tcp->tcp_closemp_used = 1; 1941 else 1942 tcp->tcp_closemp_used++; 1943 1944 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1945 mp = &tcp->tcp_closemp; 1946 squeue_fill(connp->conn_sqp, mp, 1947 tcp_timewait_output, connp, 1948 SQTAG_TCP_TIMEWAIT); 1949 } 1950 } else { 1951 mutex_enter(&connp->conn_lock); 1952 CONN_INC_REF_LOCKED(connp); 1953 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1954 mutex_exit(&connp->conn_lock); 1955 /* 1956 * We can reuse the closemp here since conn has 1957 * detached (otherwise we wouldn't even be in 1958 * time_wait list). tcp_closemp_used can safely 1959 * be changed without taking a lock as no other 1960 * thread can concurrently access it at this 1961 * point in the connection lifecycle. We 1962 * increment tcp_closemp_used to record any 1963 * attempt to reuse tcp_closemp while it is 1964 * still in use. 1965 */ 1966 1967 if (tcp->tcp_closemp.b_prev == NULL) 1968 tcp->tcp_closemp_used = 1; 1969 else 1970 tcp->tcp_closemp_used++; 1971 1972 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1973 mp = &tcp->tcp_closemp; 1974 squeue_fill(connp->conn_sqp, mp, 1975 tcp_timewait_output, connp, 0); 1976 } 1977 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1978 } 1979 1980 if (tcp_time_wait->tcp_free_list != NULL) 1981 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1982 1983 tcp_time_wait->tcp_time_wait_tid = 1984 timeout(tcp_time_wait_collector, sqp, TCP_TIME_WAIT_DELAY); 1985 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1986 } 1987 1988 /* 1989 * Reply to a clients T_CONN_RES TPI message. This function 1990 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1991 * on the acceptor STREAM and processed in tcp_wput_accept(). 1992 * Read the block comment on top of tcp_conn_request(). 1993 */ 1994 static void 1995 tcp_accept(tcp_t *listener, mblk_t *mp) 1996 { 1997 tcp_t *acceptor; 1998 tcp_t *eager; 1999 tcp_t *tcp; 2000 struct T_conn_res *tcr; 2001 t_uscalar_t acceptor_id; 2002 t_scalar_t seqnum; 2003 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 2004 mblk_t *ok_mp; 2005 mblk_t *mp1; 2006 2007 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 2008 tcp_err_ack(listener, mp, TPROTO, 0); 2009 return; 2010 } 2011 tcr = (struct T_conn_res *)mp->b_rptr; 2012 2013 /* 2014 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 2015 * read side queue of the streams device underneath us i.e. the 2016 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 2017 * look it up in the queue_hash. Under LP64 it sends down the 2018 * minor_t of the accepting endpoint. 2019 * 2020 * Once the acceptor/eager are modified (in tcp_accept_swap) the 2021 * fanout hash lock is held. 2022 * This prevents any thread from entering the acceptor queue from 2023 * below (since it has not been hard bound yet i.e. any inbound 2024 * packets will arrive on the listener or default tcp queue and 2025 * go through tcp_lookup). 2026 * The CONN_INC_REF will prevent the acceptor from closing. 2027 * 2028 * XXX It is still possible for a tli application to send down data 2029 * on the accepting stream while another thread calls t_accept. 2030 * This should not be a problem for well-behaved applications since 2031 * the T_OK_ACK is sent after the queue swapping is completed. 2032 * 2033 * If the accepting fd is the same as the listening fd, avoid 2034 * queue hash lookup since that will return an eager listener in a 2035 * already established state. 2036 */ 2037 acceptor_id = tcr->ACCEPTOR_id; 2038 mutex_enter(&listener->tcp_eager_lock); 2039 if (listener->tcp_acceptor_id == acceptor_id) { 2040 eager = listener->tcp_eager_next_q; 2041 /* only count how many T_CONN_INDs so don't count q0 */ 2042 if ((listener->tcp_conn_req_cnt_q != 1) || 2043 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 2044 mutex_exit(&listener->tcp_eager_lock); 2045 tcp_err_ack(listener, mp, TBADF, 0); 2046 return; 2047 } 2048 if (listener->tcp_conn_req_cnt_q0 != 0) { 2049 /* Throw away all the eagers on q0. */ 2050 tcp_eager_cleanup(listener, 1); 2051 } 2052 if (listener->tcp_syn_defense) { 2053 listener->tcp_syn_defense = B_FALSE; 2054 if (listener->tcp_ip_addr_cache != NULL) { 2055 kmem_free(listener->tcp_ip_addr_cache, 2056 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 2057 listener->tcp_ip_addr_cache = NULL; 2058 } 2059 } 2060 /* 2061 * Transfer tcp_conn_req_max to the eager so that when 2062 * a disconnect occurs we can revert the endpoint to the 2063 * listen state. 2064 */ 2065 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 2066 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 2067 /* 2068 * Get a reference on the acceptor just like the 2069 * tcp_acceptor_hash_lookup below. 2070 */ 2071 acceptor = listener; 2072 CONN_INC_REF(acceptor->tcp_connp); 2073 } else { 2074 acceptor = tcp_acceptor_hash_lookup(acceptor_id); 2075 if (acceptor == NULL) { 2076 if (listener->tcp_debug) { 2077 (void) strlog(TCP_MOD_ID, 0, 1, 2078 SL_ERROR|SL_TRACE, 2079 "tcp_accept: did not find acceptor 0x%x\n", 2080 acceptor_id); 2081 } 2082 mutex_exit(&listener->tcp_eager_lock); 2083 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 2084 return; 2085 } 2086 /* 2087 * Verify acceptor state. The acceptable states for an acceptor 2088 * include TCPS_IDLE and TCPS_BOUND. 2089 */ 2090 switch (acceptor->tcp_state) { 2091 case TCPS_IDLE: 2092 /* FALLTHRU */ 2093 case TCPS_BOUND: 2094 break; 2095 default: 2096 CONN_DEC_REF(acceptor->tcp_connp); 2097 mutex_exit(&listener->tcp_eager_lock); 2098 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2099 return; 2100 } 2101 } 2102 2103 /* The listener must be in TCPS_LISTEN */ 2104 if (listener->tcp_state != TCPS_LISTEN) { 2105 CONN_DEC_REF(acceptor->tcp_connp); 2106 mutex_exit(&listener->tcp_eager_lock); 2107 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2108 return; 2109 } 2110 2111 /* 2112 * Rendezvous with an eager connection request packet hanging off 2113 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2114 * tcp structure when the connection packet arrived in 2115 * tcp_conn_request(). 2116 */ 2117 seqnum = tcr->SEQ_number; 2118 eager = listener; 2119 do { 2120 eager = eager->tcp_eager_next_q; 2121 if (eager == NULL) { 2122 CONN_DEC_REF(acceptor->tcp_connp); 2123 mutex_exit(&listener->tcp_eager_lock); 2124 tcp_err_ack(listener, mp, TBADSEQ, 0); 2125 return; 2126 } 2127 } while (eager->tcp_conn_req_seqnum != seqnum); 2128 mutex_exit(&listener->tcp_eager_lock); 2129 2130 /* 2131 * At this point, both acceptor and listener have 2 ref 2132 * that they begin with. Acceptor has one additional ref 2133 * we placed in lookup while listener has 3 additional 2134 * ref for being behind the squeue (tcp_accept() is 2135 * done on listener's squeue); being in classifier hash; 2136 * and eager's ref on listener. 2137 */ 2138 ASSERT(listener->tcp_connp->conn_ref >= 5); 2139 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2140 2141 /* 2142 * The eager at this point is set in its own squeue and 2143 * could easily have been killed (tcp_accept_finish will 2144 * deal with that) because of a TH_RST so we can only 2145 * ASSERT for a single ref. 2146 */ 2147 ASSERT(eager->tcp_connp->conn_ref >= 1); 2148 2149 /* Pre allocate the stroptions mblk also */ 2150 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2151 if (opt_mp == NULL) { 2152 CONN_DEC_REF(acceptor->tcp_connp); 2153 CONN_DEC_REF(eager->tcp_connp); 2154 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2155 return; 2156 } 2157 DB_TYPE(opt_mp) = M_SETOPTS; 2158 opt_mp->b_wptr += sizeof (struct stroptions); 2159 2160 /* 2161 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2162 * from listener to acceptor. The message is chained on opt_mp 2163 * which will be sent onto eager's squeue. 2164 */ 2165 if (listener->tcp_bound_if != 0) { 2166 /* allocate optmgmt req */ 2167 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2168 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2169 sizeof (int)); 2170 if (mp1 != NULL) 2171 linkb(opt_mp, mp1); 2172 } 2173 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2174 uint_t on = 1; 2175 2176 /* allocate optmgmt req */ 2177 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2178 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2179 if (mp1 != NULL) 2180 linkb(opt_mp, mp1); 2181 } 2182 2183 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2184 if ((mp1 = copymsg(mp)) == NULL) { 2185 CONN_DEC_REF(acceptor->tcp_connp); 2186 CONN_DEC_REF(eager->tcp_connp); 2187 freemsg(opt_mp); 2188 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2189 return; 2190 } 2191 2192 tcr = (struct T_conn_res *)mp1->b_rptr; 2193 2194 /* 2195 * This is an expanded version of mi_tpi_ok_ack_alloc() 2196 * which allocates a larger mblk and appends the new 2197 * local address to the ok_ack. The address is copied by 2198 * soaccept() for getsockname(). 2199 */ 2200 { 2201 int extra; 2202 2203 extra = (eager->tcp_family == AF_INET) ? 2204 sizeof (sin_t) : sizeof (sin6_t); 2205 2206 /* 2207 * Try to re-use mp, if possible. Otherwise, allocate 2208 * an mblk and return it as ok_mp. In any case, mp 2209 * is no longer usable upon return. 2210 */ 2211 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2212 CONN_DEC_REF(acceptor->tcp_connp); 2213 CONN_DEC_REF(eager->tcp_connp); 2214 freemsg(opt_mp); 2215 /* Original mp has been freed by now, so use mp1 */ 2216 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2217 return; 2218 } 2219 2220 mp = NULL; /* We should never use mp after this point */ 2221 2222 switch (extra) { 2223 case sizeof (sin_t): { 2224 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2225 2226 ok_mp->b_wptr += extra; 2227 sin->sin_family = AF_INET; 2228 sin->sin_port = eager->tcp_lport; 2229 sin->sin_addr.s_addr = 2230 eager->tcp_ipha->ipha_src; 2231 break; 2232 } 2233 case sizeof (sin6_t): { 2234 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2235 2236 ok_mp->b_wptr += extra; 2237 sin6->sin6_family = AF_INET6; 2238 sin6->sin6_port = eager->tcp_lport; 2239 if (eager->tcp_ipversion == IPV4_VERSION) { 2240 sin6->sin6_flowinfo = 0; 2241 IN6_IPADDR_TO_V4MAPPED( 2242 eager->tcp_ipha->ipha_src, 2243 &sin6->sin6_addr); 2244 } else { 2245 ASSERT(eager->tcp_ip6h != NULL); 2246 sin6->sin6_flowinfo = 2247 eager->tcp_ip6h->ip6_vcf & 2248 ~IPV6_VERS_AND_FLOW_MASK; 2249 sin6->sin6_addr = 2250 eager->tcp_ip6h->ip6_src; 2251 } 2252 break; 2253 } 2254 default: 2255 break; 2256 } 2257 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2258 } 2259 2260 /* 2261 * If there are no options we know that the T_CONN_RES will 2262 * succeed. However, we can't send the T_OK_ACK upstream until 2263 * the tcp_accept_swap is done since it would be dangerous to 2264 * let the application start using the new fd prior to the swap. 2265 */ 2266 tcp_accept_swap(listener, acceptor, eager); 2267 2268 /* 2269 * tcp_accept_swap unlinks eager from listener but does not drop 2270 * the eager's reference on the listener. 2271 */ 2272 ASSERT(eager->tcp_listener == NULL); 2273 ASSERT(listener->tcp_connp->conn_ref >= 5); 2274 2275 /* 2276 * The eager is now associated with its own queue. Insert in 2277 * the hash so that the connection can be reused for a future 2278 * T_CONN_RES. 2279 */ 2280 tcp_acceptor_hash_insert(acceptor_id, eager); 2281 2282 /* 2283 * We now do the processing of options with T_CONN_RES. 2284 * We delay till now since we wanted to have queue to pass to 2285 * option processing routines that points back to the right 2286 * instance structure which does not happen until after 2287 * tcp_accept_swap(). 2288 * 2289 * Note: 2290 * The sanity of the logic here assumes that whatever options 2291 * are appropriate to inherit from listner=>eager are done 2292 * before this point, and whatever were to be overridden (or not) 2293 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2294 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2295 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2296 * This may not be true at this point in time but can be fixed 2297 * independently. This option processing code starts with 2298 * the instantiated acceptor instance and the final queue at 2299 * this point. 2300 */ 2301 2302 if (tcr->OPT_length != 0) { 2303 /* Options to process */ 2304 int t_error = 0; 2305 int sys_error = 0; 2306 int do_disconnect = 0; 2307 2308 if (tcp_conprim_opt_process(eager, mp1, 2309 &do_disconnect, &t_error, &sys_error) < 0) { 2310 eager->tcp_accept_error = 1; 2311 if (do_disconnect) { 2312 /* 2313 * An option failed which does not allow 2314 * connection to be accepted. 2315 * 2316 * We allow T_CONN_RES to succeed and 2317 * put a T_DISCON_IND on the eager queue. 2318 */ 2319 ASSERT(t_error == 0 && sys_error == 0); 2320 eager->tcp_send_discon_ind = 1; 2321 } else { 2322 ASSERT(t_error != 0); 2323 freemsg(ok_mp); 2324 /* 2325 * Original mp was either freed or set 2326 * to ok_mp above, so use mp1 instead. 2327 */ 2328 tcp_err_ack(listener, mp1, t_error, sys_error); 2329 goto finish; 2330 } 2331 } 2332 /* 2333 * Most likely success in setting options (except if 2334 * eager->tcp_send_discon_ind set). 2335 * mp1 option buffer represented by OPT_length/offset 2336 * potentially modified and contains results of setting 2337 * options at this point 2338 */ 2339 } 2340 2341 /* We no longer need mp1, since all options processing has passed */ 2342 freemsg(mp1); 2343 2344 putnext(listener->tcp_rq, ok_mp); 2345 2346 mutex_enter(&listener->tcp_eager_lock); 2347 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2348 tcp_t *tail; 2349 mblk_t *conn_ind; 2350 2351 /* 2352 * This path should not be executed if listener and 2353 * acceptor streams are the same. 2354 */ 2355 ASSERT(listener != acceptor); 2356 2357 tcp = listener->tcp_eager_prev_q0; 2358 /* 2359 * listener->tcp_eager_prev_q0 points to the TAIL of the 2360 * deferred T_conn_ind queue. We need to get to the head of 2361 * the queue in order to send up T_conn_ind the same order as 2362 * how the 3WHS is completed. 2363 */ 2364 while (tcp != listener) { 2365 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2366 break; 2367 else 2368 tcp = tcp->tcp_eager_prev_q0; 2369 } 2370 ASSERT(tcp != listener); 2371 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2372 ASSERT(conn_ind != NULL); 2373 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2374 2375 /* Move from q0 to q */ 2376 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2377 listener->tcp_conn_req_cnt_q0--; 2378 listener->tcp_conn_req_cnt_q++; 2379 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2380 tcp->tcp_eager_prev_q0; 2381 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2382 tcp->tcp_eager_next_q0; 2383 tcp->tcp_eager_prev_q0 = NULL; 2384 tcp->tcp_eager_next_q0 = NULL; 2385 tcp->tcp_conn_def_q0 = B_FALSE; 2386 2387 /* Make sure the tcp isn't in the list of droppables */ 2388 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2389 tcp->tcp_eager_prev_drop_q0 == NULL); 2390 2391 /* 2392 * Insert at end of the queue because sockfs sends 2393 * down T_CONN_RES in chronological order. Leaving 2394 * the older conn indications at front of the queue 2395 * helps reducing search time. 2396 */ 2397 tail = listener->tcp_eager_last_q; 2398 if (tail != NULL) 2399 tail->tcp_eager_next_q = tcp; 2400 else 2401 listener->tcp_eager_next_q = tcp; 2402 listener->tcp_eager_last_q = tcp; 2403 tcp->tcp_eager_next_q = NULL; 2404 mutex_exit(&listener->tcp_eager_lock); 2405 putnext(tcp->tcp_rq, conn_ind); 2406 } else { 2407 mutex_exit(&listener->tcp_eager_lock); 2408 } 2409 2410 /* 2411 * Done with the acceptor - free it 2412 * 2413 * Note: from this point on, no access to listener should be made 2414 * as listener can be equal to acceptor. 2415 */ 2416 finish: 2417 ASSERT(acceptor->tcp_detached); 2418 acceptor->tcp_rq = tcp_g_q; 2419 acceptor->tcp_wq = WR(tcp_g_q); 2420 (void) tcp_clean_death(acceptor, 0, 2); 2421 CONN_DEC_REF(acceptor->tcp_connp); 2422 2423 /* 2424 * In case we already received a FIN we have to make tcp_rput send 2425 * the ordrel_ind. This will also send up a window update if the window 2426 * has opened up. 2427 * 2428 * In the normal case of a successful connection acceptance 2429 * we give the O_T_BIND_REQ to the read side put procedure as an 2430 * indication that this was just accepted. This tells tcp_rput to 2431 * pass up any data queued in tcp_rcv_list. 2432 * 2433 * In the fringe case where options sent with T_CONN_RES failed and 2434 * we required, we would be indicating a T_DISCON_IND to blow 2435 * away this connection. 2436 */ 2437 2438 /* 2439 * XXX: we currently have a problem if XTI application closes the 2440 * acceptor stream in between. This problem exists in on10-gate also 2441 * and is well know but nothing can be done short of major rewrite 2442 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2443 * eager same squeue as listener (we can distinguish non socket 2444 * listeners at the time of handling a SYN in tcp_conn_request) 2445 * and do most of the work that tcp_accept_finish does here itself 2446 * and then get behind the acceptor squeue to access the acceptor 2447 * queue. 2448 */ 2449 /* 2450 * We already have a ref on tcp so no need to do one before squeue_fill 2451 */ 2452 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2453 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2454 } 2455 2456 /* 2457 * Swap information between the eager and acceptor for a TLI/XTI client. 2458 * The sockfs accept is done on the acceptor stream and control goes 2459 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2460 * called. In either case, both the eager and listener are in their own 2461 * perimeter (squeue) and the code has to deal with potential race. 2462 * 2463 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2464 */ 2465 static void 2466 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2467 { 2468 conn_t *econnp, *aconnp; 2469 2470 ASSERT(eager->tcp_rq == listener->tcp_rq); 2471 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2472 ASSERT(!eager->tcp_hard_bound); 2473 ASSERT(!TCP_IS_SOCKET(acceptor)); 2474 ASSERT(!TCP_IS_SOCKET(eager)); 2475 ASSERT(!TCP_IS_SOCKET(listener)); 2476 2477 acceptor->tcp_detached = B_TRUE; 2478 /* 2479 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2480 * the acceptor id. 2481 */ 2482 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2483 2484 /* remove eager from listen list... */ 2485 mutex_enter(&listener->tcp_eager_lock); 2486 tcp_eager_unlink(eager); 2487 ASSERT(eager->tcp_eager_next_q == NULL && 2488 eager->tcp_eager_last_q == NULL); 2489 ASSERT(eager->tcp_eager_next_q0 == NULL && 2490 eager->tcp_eager_prev_q0 == NULL); 2491 mutex_exit(&listener->tcp_eager_lock); 2492 eager->tcp_rq = acceptor->tcp_rq; 2493 eager->tcp_wq = acceptor->tcp_wq; 2494 2495 econnp = eager->tcp_connp; 2496 aconnp = acceptor->tcp_connp; 2497 2498 eager->tcp_rq->q_ptr = econnp; 2499 eager->tcp_wq->q_ptr = econnp; 2500 2501 /* 2502 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2503 * which might be a different squeue from our peer TCP instance. 2504 * For TCP Fusion, the peer expects that whenever tcp_detached is 2505 * clear, our TCP queues point to the acceptor's queues. Thus, use 2506 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2507 * above reach global visibility prior to the clearing of tcp_detached. 2508 */ 2509 membar_producer(); 2510 eager->tcp_detached = B_FALSE; 2511 2512 ASSERT(eager->tcp_ack_tid == 0); 2513 2514 econnp->conn_dev = aconnp->conn_dev; 2515 if (eager->tcp_cred != NULL) 2516 crfree(eager->tcp_cred); 2517 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2518 aconnp->conn_cred = NULL; 2519 2520 econnp->conn_zoneid = aconnp->conn_zoneid; 2521 econnp->conn_allzones = aconnp->conn_allzones; 2522 2523 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2524 aconnp->conn_mac_exempt = B_FALSE; 2525 2526 ASSERT(aconnp->conn_peercred == NULL); 2527 2528 /* Do the IPC initialization */ 2529 CONN_INC_REF(econnp); 2530 2531 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2532 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2533 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2534 econnp->conn_ulp = aconnp->conn_ulp; 2535 2536 /* Done with old IPC. Drop its ref on its connp */ 2537 CONN_DEC_REF(aconnp); 2538 } 2539 2540 2541 /* 2542 * Adapt to the information, such as rtt and rtt_sd, provided from the 2543 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2544 * 2545 * Checks for multicast and broadcast destination address. 2546 * Returns zero on failure; non-zero if ok. 2547 * 2548 * Note that the MSS calculation here is based on the info given in 2549 * the IRE. We do not do any calculation based on TCP options. They 2550 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2551 * knows which options to use. 2552 * 2553 * Note on how TCP gets its parameters for a connection. 2554 * 2555 * When a tcp_t structure is allocated, it gets all the default parameters. 2556 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2557 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2558 * default. But if there is an associated tcp_host_param, it will override 2559 * the metrics. 2560 * 2561 * An incoming SYN with a multicast or broadcast destination address, is dropped 2562 * in 1 of 2 places. 2563 * 2564 * 1. If the packet was received over the wire it is dropped in 2565 * ip_rput_process_broadcast() 2566 * 2567 * 2. If the packet was received through internal IP loopback, i.e. the packet 2568 * was generated and received on the same machine, it is dropped in 2569 * ip_wput_local() 2570 * 2571 * An incoming SYN with a multicast or broadcast source address is always 2572 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2573 * reject an attempt to connect to a broadcast or multicast (destination) 2574 * address. 2575 */ 2576 static int 2577 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2578 { 2579 tcp_hsp_t *hsp; 2580 ire_t *ire; 2581 ire_t *sire = NULL; 2582 iulp_t *ire_uinfo = NULL; 2583 uint32_t mss_max; 2584 uint32_t mss; 2585 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2586 conn_t *connp = tcp->tcp_connp; 2587 boolean_t ire_cacheable = B_FALSE; 2588 zoneid_t zoneid = connp->conn_zoneid; 2589 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2590 MATCH_IRE_SECATTR; 2591 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2592 ill_t *ill = NULL; 2593 boolean_t incoming = (ire_mp == NULL); 2594 2595 ASSERT(connp->conn_ire_cache == NULL); 2596 2597 if (tcp->tcp_ipversion == IPV4_VERSION) { 2598 2599 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2600 BUMP_MIB(&ip_mib, ipInDiscards); 2601 return (0); 2602 } 2603 /* 2604 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2605 * for the destination with the nexthop as gateway. 2606 * ire_ctable_lookup() is used because this particular 2607 * ire, if it exists, will be marked private. 2608 * If that is not available, use the interface ire 2609 * for the nexthop. 2610 * 2611 * TSol: tcp_update_label will detect label mismatches based 2612 * only on the destination's label, but that would not 2613 * detect label mismatches based on the security attributes 2614 * of routes or next hop gateway. Hence we need to pass the 2615 * label to ire_ftable_lookup below in order to locate the 2616 * right prefix (and/or) ire cache. Similarly we also need 2617 * pass the label to the ire_cache_lookup below to locate 2618 * the right ire that also matches on the label. 2619 */ 2620 if (tcp->tcp_connp->conn_nexthop_set) { 2621 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2622 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2623 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW); 2624 if (ire == NULL) { 2625 ire = ire_ftable_lookup( 2626 tcp->tcp_connp->conn_nexthop_v4, 2627 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2628 tsl, match_flags); 2629 if (ire == NULL) 2630 return (0); 2631 } else { 2632 ire_uinfo = &ire->ire_uinfo; 2633 } 2634 } else { 2635 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2636 zoneid, tsl); 2637 if (ire != NULL) { 2638 ire_cacheable = B_TRUE; 2639 ire_uinfo = (ire_mp != NULL) ? 2640 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2641 &ire->ire_uinfo; 2642 2643 } else { 2644 if (ire_mp == NULL) { 2645 ire = ire_ftable_lookup( 2646 tcp->tcp_connp->conn_rem, 2647 0, 0, 0, NULL, &sire, zoneid, 0, 2648 tsl, (MATCH_IRE_RECURSIVE | 2649 MATCH_IRE_DEFAULT)); 2650 if (ire == NULL) 2651 return (0); 2652 ire_uinfo = (sire != NULL) ? 2653 &sire->ire_uinfo : 2654 &ire->ire_uinfo; 2655 } else { 2656 ire = (ire_t *)ire_mp->b_rptr; 2657 ire_uinfo = 2658 &((ire_t *) 2659 ire_mp->b_rptr)->ire_uinfo; 2660 } 2661 } 2662 } 2663 ASSERT(ire != NULL); 2664 2665 if ((ire->ire_src_addr == INADDR_ANY) || 2666 (ire->ire_type & IRE_BROADCAST)) { 2667 /* 2668 * ire->ire_mp is non null when ire_mp passed in is used 2669 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2670 */ 2671 if (ire->ire_mp == NULL) 2672 ire_refrele(ire); 2673 if (sire != NULL) 2674 ire_refrele(sire); 2675 return (0); 2676 } 2677 2678 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2679 ipaddr_t src_addr; 2680 2681 /* 2682 * ip_bind_connected() has stored the correct source 2683 * address in conn_src. 2684 */ 2685 src_addr = tcp->tcp_connp->conn_src; 2686 tcp->tcp_ipha->ipha_src = src_addr; 2687 /* 2688 * Copy of the src addr. in tcp_t is needed 2689 * for the lookup funcs. 2690 */ 2691 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2692 } 2693 /* 2694 * Set the fragment bit so that IP will tell us if the MTU 2695 * should change. IP tells us the latest setting of 2696 * ip_path_mtu_discovery through ire_frag_flag. 2697 */ 2698 if (ip_path_mtu_discovery) { 2699 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2700 htons(IPH_DF); 2701 } 2702 /* 2703 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2704 * for IP_NEXTHOP. No cache ire has been found for the 2705 * destination and we are working with the nexthop's 2706 * interface ire. Since we need to forward all packets 2707 * to the nexthop first, we "blindly" set tcp_localnet 2708 * to false, eventhough the destination may also be 2709 * onlink. 2710 */ 2711 if (ire_uinfo == NULL) 2712 tcp->tcp_localnet = 0; 2713 else 2714 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2715 } else { 2716 /* 2717 * For incoming connection ire_mp = NULL 2718 * For outgoing connection ire_mp != NULL 2719 * Technically we should check conn_incoming_ill 2720 * when ire_mp is NULL and conn_outgoing_ill when 2721 * ire_mp is non-NULL. But this is performance 2722 * critical path and for IPV*_BOUND_IF, outgoing 2723 * and incoming ill are always set to the same value. 2724 */ 2725 ill_t *dst_ill = NULL; 2726 ipif_t *dst_ipif = NULL; 2727 2728 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2729 2730 if (connp->conn_outgoing_ill != NULL) { 2731 /* Outgoing or incoming path */ 2732 int err; 2733 2734 dst_ill = conn_get_held_ill(connp, 2735 &connp->conn_outgoing_ill, &err); 2736 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2737 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2738 return (0); 2739 } 2740 match_flags |= MATCH_IRE_ILL; 2741 dst_ipif = dst_ill->ill_ipif; 2742 } 2743 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2744 0, 0, dst_ipif, zoneid, tsl, match_flags); 2745 2746 if (ire != NULL) { 2747 ire_cacheable = B_TRUE; 2748 ire_uinfo = (ire_mp != NULL) ? 2749 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2750 &ire->ire_uinfo; 2751 } else { 2752 if (ire_mp == NULL) { 2753 ire = ire_ftable_lookup_v6( 2754 &tcp->tcp_connp->conn_remv6, 2755 0, 0, 0, dst_ipif, &sire, zoneid, 2756 0, tsl, match_flags); 2757 if (ire == NULL) { 2758 if (dst_ill != NULL) 2759 ill_refrele(dst_ill); 2760 return (0); 2761 } 2762 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2763 &ire->ire_uinfo; 2764 } else { 2765 ire = (ire_t *)ire_mp->b_rptr; 2766 ire_uinfo = 2767 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2768 } 2769 } 2770 if (dst_ill != NULL) 2771 ill_refrele(dst_ill); 2772 2773 ASSERT(ire != NULL); 2774 ASSERT(ire_uinfo != NULL); 2775 2776 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2777 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2778 /* 2779 * ire->ire_mp is non null when ire_mp passed in is used 2780 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2781 */ 2782 if (ire->ire_mp == NULL) 2783 ire_refrele(ire); 2784 if (sire != NULL) 2785 ire_refrele(sire); 2786 return (0); 2787 } 2788 2789 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2790 in6_addr_t src_addr; 2791 2792 /* 2793 * ip_bind_connected_v6() has stored the correct source 2794 * address per IPv6 addr. selection policy in 2795 * conn_src_v6. 2796 */ 2797 src_addr = tcp->tcp_connp->conn_srcv6; 2798 2799 tcp->tcp_ip6h->ip6_src = src_addr; 2800 /* 2801 * Copy of the src addr. in tcp_t is needed 2802 * for the lookup funcs. 2803 */ 2804 tcp->tcp_ip_src_v6 = src_addr; 2805 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2806 &connp->conn_srcv6)); 2807 } 2808 tcp->tcp_localnet = 2809 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2810 } 2811 2812 /* 2813 * This allows applications to fail quickly when connections are made 2814 * to dead hosts. Hosts can be labeled dead by adding a reject route 2815 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2816 */ 2817 if ((ire->ire_flags & RTF_REJECT) && 2818 (ire->ire_flags & RTF_PRIVATE)) 2819 goto error; 2820 2821 /* 2822 * Make use of the cached rtt and rtt_sd values to calculate the 2823 * initial RTO. Note that they are already initialized in 2824 * tcp_init_values(). 2825 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2826 * IP_NEXTHOP, but instead are using the interface ire for the 2827 * nexthop, then we do not use the ire_uinfo from that ire to 2828 * do any initializations. 2829 */ 2830 if (ire_uinfo != NULL) { 2831 if (ire_uinfo->iulp_rtt != 0) { 2832 clock_t rto; 2833 2834 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2835 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2836 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2837 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5); 2838 2839 if (rto > tcp_rexmit_interval_max) { 2840 tcp->tcp_rto = tcp_rexmit_interval_max; 2841 } else if (rto < tcp_rexmit_interval_min) { 2842 tcp->tcp_rto = tcp_rexmit_interval_min; 2843 } else { 2844 tcp->tcp_rto = rto; 2845 } 2846 } 2847 if (ire_uinfo->iulp_ssthresh != 0) 2848 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2849 else 2850 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2851 if (ire_uinfo->iulp_spipe > 0) { 2852 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2853 tcp_max_buf); 2854 if (tcp_snd_lowat_fraction != 0) 2855 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2856 tcp_snd_lowat_fraction; 2857 (void) tcp_maxpsz_set(tcp, B_TRUE); 2858 } 2859 /* 2860 * Note that up till now, acceptor always inherits receive 2861 * window from the listener. But if there is a metrics 2862 * associated with a host, we should use that instead of 2863 * inheriting it from listener. Thus we need to pass this 2864 * info back to the caller. 2865 */ 2866 if (ire_uinfo->iulp_rpipe > 0) { 2867 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, tcp_max_buf); 2868 } 2869 2870 if (ire_uinfo->iulp_rtomax > 0) { 2871 tcp->tcp_second_timer_threshold = 2872 ire_uinfo->iulp_rtomax; 2873 } 2874 2875 /* 2876 * Use the metric option settings, iulp_tstamp_ok and 2877 * iulp_wscale_ok, only for active open. What this means 2878 * is that if the other side uses timestamp or window 2879 * scale option, TCP will also use those options. That 2880 * is for passive open. If the application sets a 2881 * large window, window scale is enabled regardless of 2882 * the value in iulp_wscale_ok. This is the behavior 2883 * since 2.6. So we keep it. 2884 * The only case left in passive open processing is the 2885 * check for SACK. 2886 * For ECN, it should probably be like SACK. But the 2887 * current value is binary, so we treat it like the other 2888 * cases. The metric only controls active open.For passive 2889 * open, the ndd param, tcp_ecn_permitted, controls the 2890 * behavior. 2891 */ 2892 if (!tcp_detached) { 2893 /* 2894 * The if check means that the following can only 2895 * be turned on by the metrics only IRE, but not off. 2896 */ 2897 if (ire_uinfo->iulp_tstamp_ok) 2898 tcp->tcp_snd_ts_ok = B_TRUE; 2899 if (ire_uinfo->iulp_wscale_ok) 2900 tcp->tcp_snd_ws_ok = B_TRUE; 2901 if (ire_uinfo->iulp_sack == 2) 2902 tcp->tcp_snd_sack_ok = B_TRUE; 2903 if (ire_uinfo->iulp_ecn_ok) 2904 tcp->tcp_ecn_ok = B_TRUE; 2905 } else { 2906 /* 2907 * Passive open. 2908 * 2909 * As above, the if check means that SACK can only be 2910 * turned on by the metric only IRE. 2911 */ 2912 if (ire_uinfo->iulp_sack > 0) { 2913 tcp->tcp_snd_sack_ok = B_TRUE; 2914 } 2915 } 2916 } 2917 2918 2919 /* 2920 * XXX: Note that currently, ire_max_frag can be as small as 68 2921 * because of PMTUd. So tcp_mss may go to negative if combined 2922 * length of all those options exceeds 28 bytes. But because 2923 * of the tcp_mss_min check below, we may not have a problem if 2924 * tcp_mss_min is of a reasonable value. The default is 1 so 2925 * the negative problem still exists. And the check defeats PMTUd. 2926 * In fact, if PMTUd finds that the MSS should be smaller than 2927 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2928 * value. 2929 * 2930 * We do not deal with that now. All those problems related to 2931 * PMTUd will be fixed later. 2932 */ 2933 ASSERT(ire->ire_max_frag != 0); 2934 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2935 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2936 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2937 mss = MIN(mss, IPV6_MIN_MTU); 2938 } 2939 } 2940 2941 /* Sanity check for MSS value. */ 2942 if (tcp->tcp_ipversion == IPV4_VERSION) 2943 mss_max = tcp_mss_max_ipv4; 2944 else 2945 mss_max = tcp_mss_max_ipv6; 2946 2947 if (tcp->tcp_ipversion == IPV6_VERSION && 2948 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2949 /* 2950 * After receiving an ICMPv6 "packet too big" message with a 2951 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2952 * will insert a 8-byte fragment header in every packet; we 2953 * reduce the MSS by that amount here. 2954 */ 2955 mss -= sizeof (ip6_frag_t); 2956 } 2957 2958 if (tcp->tcp_ipsec_overhead == 0) 2959 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2960 2961 mss -= tcp->tcp_ipsec_overhead; 2962 2963 if (mss < tcp_mss_min) 2964 mss = tcp_mss_min; 2965 if (mss > mss_max) 2966 mss = mss_max; 2967 2968 /* Note that this is the maximum MSS, excluding all options. */ 2969 tcp->tcp_mss = mss; 2970 2971 /* 2972 * Initialize the ISS here now that we have the full connection ID. 2973 * The RFC 1948 method of initial sequence number generation requires 2974 * knowledge of the full connection ID before setting the ISS. 2975 */ 2976 2977 tcp_iss_init(tcp); 2978 2979 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2980 tcp->tcp_loopback = B_TRUE; 2981 2982 if (tcp->tcp_ipversion == IPV4_VERSION) { 2983 hsp = tcp_hsp_lookup(tcp->tcp_remote); 2984 } else { 2985 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6); 2986 } 2987 2988 if (hsp != NULL) { 2989 /* Only modify if we're going to make them bigger */ 2990 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2991 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2992 if (tcp_snd_lowat_fraction != 0) 2993 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2994 tcp_snd_lowat_fraction; 2995 } 2996 2997 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2998 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2999 } 3000 3001 /* Copy timestamp flag only for active open */ 3002 if (!tcp_detached) 3003 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 3004 } 3005 3006 if (sire != NULL) 3007 IRE_REFRELE(sire); 3008 3009 /* 3010 * If we got an IRE_CACHE and an ILL, go through their properties; 3011 * otherwise, this is deferred until later when we have an IRE_CACHE. 3012 */ 3013 if (tcp->tcp_loopback || 3014 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 3015 /* 3016 * For incoming, see if this tcp may be MDT-capable. For 3017 * outgoing, this process has been taken care of through 3018 * tcp_rput_other. 3019 */ 3020 tcp_ire_ill_check(tcp, ire, ill, incoming); 3021 tcp->tcp_ire_ill_check_done = B_TRUE; 3022 } 3023 3024 mutex_enter(&connp->conn_lock); 3025 /* 3026 * Make sure that conn is not marked incipient 3027 * for incoming connections. A blind 3028 * removal of incipient flag is cheaper than 3029 * check and removal. 3030 */ 3031 connp->conn_state_flags &= ~CONN_INCIPIENT; 3032 3033 /* Must not cache forwarding table routes. */ 3034 if (ire_cacheable) { 3035 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 3036 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 3037 connp->conn_ire_cache = ire; 3038 IRE_UNTRACE_REF(ire); 3039 rw_exit(&ire->ire_bucket->irb_lock); 3040 mutex_exit(&connp->conn_lock); 3041 return (1); 3042 } 3043 rw_exit(&ire->ire_bucket->irb_lock); 3044 } 3045 mutex_exit(&connp->conn_lock); 3046 3047 if (ire->ire_mp == NULL) 3048 ire_refrele(ire); 3049 return (1); 3050 3051 error: 3052 if (ire->ire_mp == NULL) 3053 ire_refrele(ire); 3054 if (sire != NULL) 3055 ire_refrele(sire); 3056 return (0); 3057 } 3058 3059 /* 3060 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 3061 * O_T_BIND_REQ/T_BIND_REQ message. 3062 */ 3063 static void 3064 tcp_bind(tcp_t *tcp, mblk_t *mp) 3065 { 3066 sin_t *sin; 3067 sin6_t *sin6; 3068 mblk_t *mp1; 3069 in_port_t requested_port; 3070 in_port_t allocated_port; 3071 struct T_bind_req *tbr; 3072 boolean_t bind_to_req_port_only; 3073 boolean_t backlog_update = B_FALSE; 3074 boolean_t user_specified; 3075 in6_addr_t v6addr; 3076 ipaddr_t v4addr; 3077 uint_t origipversion; 3078 int err; 3079 queue_t *q = tcp->tcp_wq; 3080 conn_t *connp; 3081 mlp_type_t addrtype, mlptype; 3082 zone_t *zone; 3083 cred_t *cr; 3084 in_port_t mlp_port; 3085 3086 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3087 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3088 if (tcp->tcp_debug) { 3089 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3090 "tcp_bind: bad req, len %u", 3091 (uint_t)(mp->b_wptr - mp->b_rptr)); 3092 } 3093 tcp_err_ack(tcp, mp, TPROTO, 0); 3094 return; 3095 } 3096 /* Make sure the largest address fits */ 3097 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3098 if (mp1 == NULL) { 3099 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3100 return; 3101 } 3102 mp = mp1; 3103 tbr = (struct T_bind_req *)mp->b_rptr; 3104 if (tcp->tcp_state >= TCPS_BOUND) { 3105 if ((tcp->tcp_state == TCPS_BOUND || 3106 tcp->tcp_state == TCPS_LISTEN) && 3107 tcp->tcp_conn_req_max != tbr->CONIND_number && 3108 tbr->CONIND_number > 0) { 3109 /* 3110 * Handle listen() increasing CONIND_number. 3111 * This is more "liberal" then what the TPI spec 3112 * requires but is needed to avoid a t_unbind 3113 * when handling listen() since the port number 3114 * might be "stolen" between the unbind and bind. 3115 */ 3116 backlog_update = B_TRUE; 3117 goto do_bind; 3118 } 3119 if (tcp->tcp_debug) { 3120 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3121 "tcp_bind: bad state, %d", tcp->tcp_state); 3122 } 3123 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3124 return; 3125 } 3126 origipversion = tcp->tcp_ipversion; 3127 3128 switch (tbr->ADDR_length) { 3129 case 0: /* request for a generic port */ 3130 tbr->ADDR_offset = sizeof (struct T_bind_req); 3131 if (tcp->tcp_family == AF_INET) { 3132 tbr->ADDR_length = sizeof (sin_t); 3133 sin = (sin_t *)&tbr[1]; 3134 *sin = sin_null; 3135 sin->sin_family = AF_INET; 3136 mp->b_wptr = (uchar_t *)&sin[1]; 3137 tcp->tcp_ipversion = IPV4_VERSION; 3138 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3139 } else { 3140 ASSERT(tcp->tcp_family == AF_INET6); 3141 tbr->ADDR_length = sizeof (sin6_t); 3142 sin6 = (sin6_t *)&tbr[1]; 3143 *sin6 = sin6_null; 3144 sin6->sin6_family = AF_INET6; 3145 mp->b_wptr = (uchar_t *)&sin6[1]; 3146 tcp->tcp_ipversion = IPV6_VERSION; 3147 V6_SET_ZERO(v6addr); 3148 } 3149 requested_port = 0; 3150 break; 3151 3152 case sizeof (sin_t): /* Complete IPv4 address */ 3153 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3154 sizeof (sin_t)); 3155 if (sin == NULL || !OK_32PTR((char *)sin)) { 3156 if (tcp->tcp_debug) { 3157 (void) strlog(TCP_MOD_ID, 0, 1, 3158 SL_ERROR|SL_TRACE, 3159 "tcp_bind: bad address parameter, " 3160 "offset %d, len %d", 3161 tbr->ADDR_offset, tbr->ADDR_length); 3162 } 3163 tcp_err_ack(tcp, mp, TPROTO, 0); 3164 return; 3165 } 3166 /* 3167 * With sockets sockfs will accept bogus sin_family in 3168 * bind() and replace it with the family used in the socket 3169 * call. 3170 */ 3171 if (sin->sin_family != AF_INET || 3172 tcp->tcp_family != AF_INET) { 3173 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3174 return; 3175 } 3176 requested_port = ntohs(sin->sin_port); 3177 tcp->tcp_ipversion = IPV4_VERSION; 3178 v4addr = sin->sin_addr.s_addr; 3179 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3180 break; 3181 3182 case sizeof (sin6_t): /* Complete IPv6 address */ 3183 sin6 = (sin6_t *)mi_offset_param(mp, 3184 tbr->ADDR_offset, sizeof (sin6_t)); 3185 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3186 if (tcp->tcp_debug) { 3187 (void) strlog(TCP_MOD_ID, 0, 1, 3188 SL_ERROR|SL_TRACE, 3189 "tcp_bind: bad IPv6 address parameter, " 3190 "offset %d, len %d", tbr->ADDR_offset, 3191 tbr->ADDR_length); 3192 } 3193 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3194 return; 3195 } 3196 if (sin6->sin6_family != AF_INET6 || 3197 tcp->tcp_family != AF_INET6) { 3198 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3199 return; 3200 } 3201 requested_port = ntohs(sin6->sin6_port); 3202 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3203 IPV4_VERSION : IPV6_VERSION; 3204 v6addr = sin6->sin6_addr; 3205 break; 3206 3207 default: 3208 if (tcp->tcp_debug) { 3209 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3210 "tcp_bind: bad address length, %d", 3211 tbr->ADDR_length); 3212 } 3213 tcp_err_ack(tcp, mp, TBADADDR, 0); 3214 return; 3215 } 3216 tcp->tcp_bound_source_v6 = v6addr; 3217 3218 /* Check for change in ipversion */ 3219 if (origipversion != tcp->tcp_ipversion) { 3220 ASSERT(tcp->tcp_family == AF_INET6); 3221 err = tcp->tcp_ipversion == IPV6_VERSION ? 3222 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3223 if (err) { 3224 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3225 return; 3226 } 3227 } 3228 3229 /* 3230 * Initialize family specific fields. Copy of the src addr. 3231 * in tcp_t is needed for the lookup funcs. 3232 */ 3233 if (tcp->tcp_ipversion == IPV6_VERSION) { 3234 tcp->tcp_ip6h->ip6_src = v6addr; 3235 } else { 3236 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3237 } 3238 tcp->tcp_ip_src_v6 = v6addr; 3239 3240 /* 3241 * For O_T_BIND_REQ: 3242 * Verify that the target port/addr is available, or choose 3243 * another. 3244 * For T_BIND_REQ: 3245 * Verify that the target port/addr is available or fail. 3246 * In both cases when it succeeds the tcp is inserted in the 3247 * bind hash table. This ensures that the operation is atomic 3248 * under the lock on the hash bucket. 3249 */ 3250 bind_to_req_port_only = requested_port != 0 && 3251 tbr->PRIM_type != O_T_BIND_REQ; 3252 /* 3253 * Get a valid port (within the anonymous range and should not 3254 * be a privileged one) to use if the user has not given a port. 3255 * If multiple threads are here, they may all start with 3256 * with the same initial port. But, it should be fine as long as 3257 * tcp_bindi will ensure that no two threads will be assigned 3258 * the same port. 3259 * 3260 * NOTE: XXX If a privileged process asks for an anonymous port, we 3261 * still check for ports only in the range > tcp_smallest_non_priv_port, 3262 * unless TCP_ANONPRIVBIND option is set. 3263 */ 3264 mlptype = mlptSingle; 3265 mlp_port = requested_port; 3266 if (requested_port == 0) { 3267 requested_port = tcp->tcp_anon_priv_bind ? 3268 tcp_get_next_priv_port(tcp) : 3269 tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 3270 if (requested_port == 0) { 3271 tcp_err_ack(tcp, mp, TNOADDR, 0); 3272 return; 3273 } 3274 user_specified = B_FALSE; 3275 3276 /* 3277 * If the user went through one of the RPC interfaces to create 3278 * this socket and RPC is MLP in this zone, then give him an 3279 * anonymous MLP. 3280 */ 3281 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3282 connp = tcp->tcp_connp; 3283 if (connp->conn_anon_mlp && is_system_labeled()) { 3284 zone = crgetzone(cr); 3285 addrtype = tsol_mlp_addr_type(zone->zone_id, 3286 IPV6_VERSION, &v6addr); 3287 if (addrtype == mlptSingle) { 3288 tcp_err_ack(tcp, mp, TNOADDR, 0); 3289 return; 3290 } 3291 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3292 PMAPPORT, addrtype); 3293 mlp_port = PMAPPORT; 3294 } 3295 } else { 3296 int i; 3297 boolean_t priv = B_FALSE; 3298 3299 /* 3300 * If the requested_port is in the well-known privileged range, 3301 * verify that the stream was opened by a privileged user. 3302 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3303 * but instead the code relies on: 3304 * - the fact that the address of the array and its size never 3305 * changes 3306 * - the atomic assignment of the elements of the array 3307 */ 3308 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3309 if (requested_port < tcp_smallest_nonpriv_port) { 3310 priv = B_TRUE; 3311 } else { 3312 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 3313 if (requested_port == 3314 tcp_g_epriv_ports[i]) { 3315 priv = B_TRUE; 3316 break; 3317 } 3318 } 3319 } 3320 if (priv) { 3321 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3322 if (tcp->tcp_debug) { 3323 (void) strlog(TCP_MOD_ID, 0, 1, 3324 SL_ERROR|SL_TRACE, 3325 "tcp_bind: no priv for port %d", 3326 requested_port); 3327 } 3328 tcp_err_ack(tcp, mp, TACCES, 0); 3329 return; 3330 } 3331 } 3332 user_specified = B_TRUE; 3333 3334 connp = tcp->tcp_connp; 3335 if (is_system_labeled()) { 3336 zone = crgetzone(cr); 3337 addrtype = tsol_mlp_addr_type(zone->zone_id, 3338 IPV6_VERSION, &v6addr); 3339 if (addrtype == mlptSingle) { 3340 tcp_err_ack(tcp, mp, TNOADDR, 0); 3341 return; 3342 } 3343 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3344 requested_port, addrtype); 3345 } 3346 } 3347 3348 if (mlptype != mlptSingle) { 3349 if (secpolicy_net_bindmlp(cr) != 0) { 3350 if (tcp->tcp_debug) { 3351 (void) strlog(TCP_MOD_ID, 0, 1, 3352 SL_ERROR|SL_TRACE, 3353 "tcp_bind: no priv for multilevel port %d", 3354 requested_port); 3355 } 3356 tcp_err_ack(tcp, mp, TACCES, 0); 3357 return; 3358 } 3359 3360 /* 3361 * If we're specifically binding a shared IP address and the 3362 * port is MLP on shared addresses, then check to see if this 3363 * zone actually owns the MLP. Reject if not. 3364 */ 3365 if (mlptype == mlptShared && addrtype == mlptShared) { 3366 zoneid_t mlpzone; 3367 3368 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3369 htons(mlp_port)); 3370 if (connp->conn_zoneid != mlpzone) { 3371 if (tcp->tcp_debug) { 3372 (void) strlog(TCP_MOD_ID, 0, 1, 3373 SL_ERROR|SL_TRACE, 3374 "tcp_bind: attempt to bind port " 3375 "%d on shared addr in zone %d " 3376 "(should be %d)", 3377 mlp_port, connp->conn_zoneid, 3378 mlpzone); 3379 } 3380 tcp_err_ack(tcp, mp, TACCES, 0); 3381 return; 3382 } 3383 } 3384 3385 if (!user_specified) { 3386 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3387 requested_port, B_TRUE); 3388 if (err != 0) { 3389 if (tcp->tcp_debug) { 3390 (void) strlog(TCP_MOD_ID, 0, 1, 3391 SL_ERROR|SL_TRACE, 3392 "tcp_bind: cannot establish anon " 3393 "MLP for port %d", 3394 requested_port); 3395 } 3396 tcp_err_ack(tcp, mp, TSYSERR, err); 3397 return; 3398 } 3399 connp->conn_anon_port = B_TRUE; 3400 } 3401 connp->conn_mlp_type = mlptype; 3402 } 3403 3404 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3405 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3406 3407 if (allocated_port == 0) { 3408 connp->conn_mlp_type = mlptSingle; 3409 if (connp->conn_anon_port) { 3410 connp->conn_anon_port = B_FALSE; 3411 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3412 requested_port, B_FALSE); 3413 } 3414 if (bind_to_req_port_only) { 3415 if (tcp->tcp_debug) { 3416 (void) strlog(TCP_MOD_ID, 0, 1, 3417 SL_ERROR|SL_TRACE, 3418 "tcp_bind: requested addr busy"); 3419 } 3420 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3421 } else { 3422 /* If we are out of ports, fail the bind. */ 3423 if (tcp->tcp_debug) { 3424 (void) strlog(TCP_MOD_ID, 0, 1, 3425 SL_ERROR|SL_TRACE, 3426 "tcp_bind: out of ports?"); 3427 } 3428 tcp_err_ack(tcp, mp, TNOADDR, 0); 3429 } 3430 return; 3431 } 3432 ASSERT(tcp->tcp_state == TCPS_BOUND); 3433 do_bind: 3434 if (!backlog_update) { 3435 if (tcp->tcp_family == AF_INET) 3436 sin->sin_port = htons(allocated_port); 3437 else 3438 sin6->sin6_port = htons(allocated_port); 3439 } 3440 if (tcp->tcp_family == AF_INET) { 3441 if (tbr->CONIND_number != 0) { 3442 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3443 sizeof (sin_t)); 3444 } else { 3445 /* Just verify the local IP address */ 3446 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3447 } 3448 } else { 3449 if (tbr->CONIND_number != 0) { 3450 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3451 sizeof (sin6_t)); 3452 } else { 3453 /* Just verify the local IP address */ 3454 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3455 IPV6_ADDR_LEN); 3456 } 3457 } 3458 if (mp1 == NULL) { 3459 if (connp->conn_anon_port) { 3460 connp->conn_anon_port = B_FALSE; 3461 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3462 requested_port, B_FALSE); 3463 } 3464 connp->conn_mlp_type = mlptSingle; 3465 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3466 return; 3467 } 3468 3469 tbr->PRIM_type = T_BIND_ACK; 3470 mp->b_datap->db_type = M_PCPROTO; 3471 3472 /* Chain in the reply mp for tcp_rput() */ 3473 mp1->b_cont = mp; 3474 mp = mp1; 3475 3476 tcp->tcp_conn_req_max = tbr->CONIND_number; 3477 if (tcp->tcp_conn_req_max) { 3478 if (tcp->tcp_conn_req_max < tcp_conn_req_min) 3479 tcp->tcp_conn_req_max = tcp_conn_req_min; 3480 if (tcp->tcp_conn_req_max > tcp_conn_req_max_q) 3481 tcp->tcp_conn_req_max = tcp_conn_req_max_q; 3482 /* 3483 * If this is a listener, do not reset the eager list 3484 * and other stuffs. Note that we don't check if the 3485 * existing eager list meets the new tcp_conn_req_max 3486 * requirement. 3487 */ 3488 if (tcp->tcp_state != TCPS_LISTEN) { 3489 tcp->tcp_state = TCPS_LISTEN; 3490 /* Initialize the chain. Don't need the eager_lock */ 3491 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3492 tcp->tcp_eager_next_drop_q0 = tcp; 3493 tcp->tcp_eager_prev_drop_q0 = tcp; 3494 tcp->tcp_second_ctimer_threshold = 3495 tcp_ip_abort_linterval; 3496 } 3497 } 3498 3499 /* 3500 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3501 * processing continues in tcp_rput_other(). 3502 */ 3503 if (tcp->tcp_family == AF_INET6) { 3504 ASSERT(tcp->tcp_connp->conn_af_isv6); 3505 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3506 } else { 3507 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3508 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3509 } 3510 /* 3511 * If the bind cannot complete immediately 3512 * IP will arrange to call tcp_rput_other 3513 * when the bind completes. 3514 */ 3515 if (mp != NULL) { 3516 tcp_rput_other(tcp, mp); 3517 } else { 3518 /* 3519 * Bind will be resumed later. Need to ensure 3520 * that conn doesn't disappear when that happens. 3521 * This will be decremented in ip_resume_tcp_bind(). 3522 */ 3523 CONN_INC_REF(tcp->tcp_connp); 3524 } 3525 } 3526 3527 3528 /* 3529 * If the "bind_to_req_port_only" parameter is set, if the requested port 3530 * number is available, return it, If not return 0 3531 * 3532 * If "bind_to_req_port_only" parameter is not set and 3533 * If the requested port number is available, return it. If not, return 3534 * the first anonymous port we happen across. If no anonymous ports are 3535 * available, return 0. addr is the requested local address, if any. 3536 * 3537 * In either case, when succeeding update the tcp_t to record the port number 3538 * and insert it in the bind hash table. 3539 * 3540 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3541 * without setting SO_REUSEADDR. This is needed so that they 3542 * can be viewed as two independent transport protocols. 3543 */ 3544 static in_port_t 3545 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3546 int reuseaddr, boolean_t quick_connect, 3547 boolean_t bind_to_req_port_only, boolean_t user_specified) 3548 { 3549 /* number of times we have run around the loop */ 3550 int count = 0; 3551 /* maximum number of times to run around the loop */ 3552 int loopmax; 3553 conn_t *connp = tcp->tcp_connp; 3554 zoneid_t zoneid = connp->conn_zoneid; 3555 3556 /* 3557 * Lookup for free addresses is done in a loop and "loopmax" 3558 * influences how long we spin in the loop 3559 */ 3560 if (bind_to_req_port_only) { 3561 /* 3562 * If the requested port is busy, don't bother to look 3563 * for a new one. Setting loop maximum count to 1 has 3564 * that effect. 3565 */ 3566 loopmax = 1; 3567 } else { 3568 /* 3569 * If the requested port is busy, look for a free one 3570 * in the anonymous port range. 3571 * Set loopmax appropriately so that one does not look 3572 * forever in the case all of the anonymous ports are in use. 3573 */ 3574 if (tcp->tcp_anon_priv_bind) { 3575 /* 3576 * loopmax = 3577 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3578 */ 3579 loopmax = IPPORT_RESERVED - tcp_min_anonpriv_port; 3580 } else { 3581 loopmax = (tcp_largest_anon_port - 3582 tcp_smallest_anon_port + 1); 3583 } 3584 } 3585 do { 3586 uint16_t lport; 3587 tf_t *tbf; 3588 tcp_t *ltcp; 3589 conn_t *lconnp; 3590 3591 lport = htons(port); 3592 3593 /* 3594 * Ensure that the tcp_t is not currently in the bind hash. 3595 * Hold the lock on the hash bucket to ensure that 3596 * the duplicate check plus the insertion is an atomic 3597 * operation. 3598 * 3599 * This function does an inline lookup on the bind hash list 3600 * Make sure that we access only members of tcp_t 3601 * and that we don't look at tcp_tcp, since we are not 3602 * doing a CONN_INC_REF. 3603 */ 3604 tcp_bind_hash_remove(tcp); 3605 tbf = &tcp_bind_fanout[TCP_BIND_HASH(lport)]; 3606 mutex_enter(&tbf->tf_lock); 3607 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3608 ltcp = ltcp->tcp_bind_hash) { 3609 boolean_t not_socket; 3610 boolean_t exclbind; 3611 3612 if (lport != ltcp->tcp_lport) 3613 continue; 3614 3615 lconnp = ltcp->tcp_connp; 3616 3617 /* 3618 * On a labeled system, we must treat bindings to ports 3619 * on shared IP addresses by sockets with MAC exemption 3620 * privilege as being in all zones, as there's 3621 * otherwise no way to identify the right receiver. 3622 */ 3623 if (!IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) && 3624 !lconnp->conn_mac_exempt && 3625 !connp->conn_mac_exempt) 3626 continue; 3627 3628 /* 3629 * If TCP_EXCLBIND is set for either the bound or 3630 * binding endpoint, the semantics of bind 3631 * is changed according to the following. 3632 * 3633 * spec = specified address (v4 or v6) 3634 * unspec = unspecified address (v4 or v6) 3635 * A = specified addresses are different for endpoints 3636 * 3637 * bound bind to allowed 3638 * ------------------------------------- 3639 * unspec unspec no 3640 * unspec spec no 3641 * spec unspec no 3642 * spec spec yes if A 3643 * 3644 * For labeled systems, SO_MAC_EXEMPT behaves the same 3645 * as TCP_EXCLBIND, except that zoneid is ignored. 3646 * 3647 * Note: 3648 * 3649 * 1. Because of TLI semantics, an endpoint can go 3650 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3651 * TCPS_BOUND, depending on whether it is originally 3652 * a listener or not. That is why we need to check 3653 * for states greater than or equal to TCPS_BOUND 3654 * here. 3655 * 3656 * 2. Ideally, we should only check for state equals 3657 * to TCPS_LISTEN. And the following check should be 3658 * added. 3659 * 3660 * if (ltcp->tcp_state == TCPS_LISTEN || 3661 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3662 * ... 3663 * } 3664 * 3665 * The semantics will be changed to this. If the 3666 * endpoint on the list is in state not equal to 3667 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3668 * set, let the bind succeed. 3669 * 3670 * Because of (1), we cannot do that for TLI 3671 * endpoints. But we can do that for socket endpoints. 3672 * If in future, we can change this going back 3673 * semantics, we can use the above check for TLI also. 3674 */ 3675 not_socket = !(TCP_IS_SOCKET(ltcp) && 3676 TCP_IS_SOCKET(tcp)); 3677 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3678 3679 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3680 (exclbind && (not_socket || 3681 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3682 if (V6_OR_V4_INADDR_ANY( 3683 ltcp->tcp_bound_source_v6) || 3684 V6_OR_V4_INADDR_ANY(*laddr) || 3685 IN6_ARE_ADDR_EQUAL(laddr, 3686 <cp->tcp_bound_source_v6)) { 3687 break; 3688 } 3689 continue; 3690 } 3691 3692 /* 3693 * Check ipversion to allow IPv4 and IPv6 sockets to 3694 * have disjoint port number spaces, if *_EXCLBIND 3695 * is not set and only if the application binds to a 3696 * specific port. We use the same autoassigned port 3697 * number space for IPv4 and IPv6 sockets. 3698 */ 3699 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3700 bind_to_req_port_only) 3701 continue; 3702 3703 /* 3704 * Ideally, we should make sure that the source 3705 * address, remote address, and remote port in the 3706 * four tuple for this tcp-connection is unique. 3707 * However, trying to find out the local source 3708 * address would require too much code duplication 3709 * with IP, since IP needs needs to have that code 3710 * to support userland TCP implementations. 3711 */ 3712 if (quick_connect && 3713 (ltcp->tcp_state > TCPS_LISTEN) && 3714 ((tcp->tcp_fport != ltcp->tcp_fport) || 3715 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3716 <cp->tcp_remote_v6))) 3717 continue; 3718 3719 if (!reuseaddr) { 3720 /* 3721 * No socket option SO_REUSEADDR. 3722 * If existing port is bound to 3723 * a non-wildcard IP address 3724 * and the requesting stream is 3725 * bound to a distinct 3726 * different IP addresses 3727 * (non-wildcard, also), keep 3728 * going. 3729 */ 3730 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3731 !V6_OR_V4_INADDR_ANY( 3732 ltcp->tcp_bound_source_v6) && 3733 !IN6_ARE_ADDR_EQUAL(laddr, 3734 <cp->tcp_bound_source_v6)) 3735 continue; 3736 if (ltcp->tcp_state >= TCPS_BOUND) { 3737 /* 3738 * This port is being used and 3739 * its state is >= TCPS_BOUND, 3740 * so we can't bind to it. 3741 */ 3742 break; 3743 } 3744 } else { 3745 /* 3746 * socket option SO_REUSEADDR is set on the 3747 * binding tcp_t. 3748 * 3749 * If two streams are bound to 3750 * same IP address or both addr 3751 * and bound source are wildcards 3752 * (INADDR_ANY), we want to stop 3753 * searching. 3754 * We have found a match of IP source 3755 * address and source port, which is 3756 * refused regardless of the 3757 * SO_REUSEADDR setting, so we break. 3758 */ 3759 if (IN6_ARE_ADDR_EQUAL(laddr, 3760 <cp->tcp_bound_source_v6) && 3761 (ltcp->tcp_state == TCPS_LISTEN || 3762 ltcp->tcp_state == TCPS_BOUND)) 3763 break; 3764 } 3765 } 3766 if (ltcp != NULL) { 3767 /* The port number is busy */ 3768 mutex_exit(&tbf->tf_lock); 3769 } else { 3770 /* 3771 * This port is ours. Insert in fanout and mark as 3772 * bound to prevent others from getting the port 3773 * number. 3774 */ 3775 tcp->tcp_state = TCPS_BOUND; 3776 tcp->tcp_lport = htons(port); 3777 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3778 3779 ASSERT(&tcp_bind_fanout[TCP_BIND_HASH( 3780 tcp->tcp_lport)] == tbf); 3781 tcp_bind_hash_insert(tbf, tcp, 1); 3782 3783 mutex_exit(&tbf->tf_lock); 3784 3785 /* 3786 * We don't want tcp_next_port_to_try to "inherit" 3787 * a port number supplied by the user in a bind. 3788 */ 3789 if (user_specified) 3790 return (port); 3791 3792 /* 3793 * This is the only place where tcp_next_port_to_try 3794 * is updated. After the update, it may or may not 3795 * be in the valid range. 3796 */ 3797 if (!tcp->tcp_anon_priv_bind) 3798 tcp_next_port_to_try = port + 1; 3799 return (port); 3800 } 3801 3802 if (tcp->tcp_anon_priv_bind) { 3803 port = tcp_get_next_priv_port(tcp); 3804 } else { 3805 if (count == 0 && user_specified) { 3806 /* 3807 * We may have to return an anonymous port. So 3808 * get one to start with. 3809 */ 3810 port = 3811 tcp_update_next_port(tcp_next_port_to_try, 3812 tcp, B_TRUE); 3813 user_specified = B_FALSE; 3814 } else { 3815 port = tcp_update_next_port(port + 1, tcp, 3816 B_FALSE); 3817 } 3818 } 3819 if (port == 0) 3820 break; 3821 3822 /* 3823 * Don't let this loop run forever in the case where 3824 * all of the anonymous ports are in use. 3825 */ 3826 } while (++count < loopmax); 3827 return (0); 3828 } 3829 3830 /* 3831 * tcp_clean_death / tcp_close_detached must not be called more than once 3832 * on a tcp. Thus every function that potentially calls tcp_clean_death 3833 * must check for the tcp state before calling tcp_clean_death. 3834 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3835 * tcp_timer_handler, all check for the tcp state. 3836 */ 3837 /* ARGSUSED */ 3838 void 3839 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3840 { 3841 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3842 3843 freemsg(mp); 3844 if (tcp->tcp_state > TCPS_BOUND) 3845 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, ETIMEDOUT, 5); 3846 } 3847 3848 /* 3849 * We are dying for some reason. Try to do it gracefully. (May be called 3850 * as writer.) 3851 * 3852 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3853 * done by a service procedure). 3854 * TBD - Should the return value distinguish between the tcp_t being 3855 * freed and it being reinitialized? 3856 */ 3857 static int 3858 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3859 { 3860 mblk_t *mp; 3861 queue_t *q; 3862 3863 TCP_CLD_STAT(tag); 3864 3865 #if TCP_TAG_CLEAN_DEATH 3866 tcp->tcp_cleandeathtag = tag; 3867 #endif 3868 3869 if (tcp->tcp_fused) 3870 tcp_unfuse(tcp); 3871 3872 if (tcp->tcp_linger_tid != 0 && 3873 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3874 tcp_stop_lingering(tcp); 3875 } 3876 3877 ASSERT(tcp != NULL); 3878 ASSERT((tcp->tcp_family == AF_INET && 3879 tcp->tcp_ipversion == IPV4_VERSION) || 3880 (tcp->tcp_family == AF_INET6 && 3881 (tcp->tcp_ipversion == IPV4_VERSION || 3882 tcp->tcp_ipversion == IPV6_VERSION))); 3883 3884 if (TCP_IS_DETACHED(tcp)) { 3885 if (tcp->tcp_hard_binding) { 3886 /* 3887 * Its an eager that we are dealing with. We close the 3888 * eager but in case a conn_ind has already gone to the 3889 * listener, let tcp_accept_finish() send a discon_ind 3890 * to the listener and drop the last reference. If the 3891 * listener doesn't even know about the eager i.e. the 3892 * conn_ind hasn't gone up, blow away the eager and drop 3893 * the last reference as well. If the conn_ind has gone 3894 * up, state should be BOUND. tcp_accept_finish 3895 * will figure out that the connection has received a 3896 * RST and will send a DISCON_IND to the application. 3897 */ 3898 tcp_closei_local(tcp); 3899 if (!tcp->tcp_tconnind_started) { 3900 CONN_DEC_REF(tcp->tcp_connp); 3901 } else { 3902 tcp->tcp_state = TCPS_BOUND; 3903 } 3904 } else { 3905 tcp_close_detached(tcp); 3906 } 3907 return (0); 3908 } 3909 3910 TCP_STAT(tcp_clean_death_nondetached); 3911 3912 /* 3913 * If T_ORDREL_IND has not been sent yet (done when service routine 3914 * is run) postpone cleaning up the endpoint until service routine 3915 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3916 * client_errno since tcp_close uses the client_errno field. 3917 */ 3918 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3919 if (err != 0) 3920 tcp->tcp_client_errno = err; 3921 3922 tcp->tcp_deferred_clean_death = B_TRUE; 3923 return (-1); 3924 } 3925 3926 q = tcp->tcp_rq; 3927 3928 /* Trash all inbound data */ 3929 flushq(q, FLUSHALL); 3930 3931 /* 3932 * If we are at least part way open and there is error 3933 * (err==0 implies no error) 3934 * notify our client by a T_DISCON_IND. 3935 */ 3936 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3937 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3938 !TCP_IS_SOCKET(tcp)) { 3939 /* 3940 * Send M_FLUSH according to TPI. Because sockets will 3941 * (and must) ignore FLUSHR we do that only for TPI 3942 * endpoints and sockets in STREAMS mode. 3943 */ 3944 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3945 } 3946 if (tcp->tcp_debug) { 3947 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3948 "tcp_clean_death: discon err %d", err); 3949 } 3950 mp = mi_tpi_discon_ind(NULL, err, 0); 3951 if (mp != NULL) { 3952 putnext(q, mp); 3953 } else { 3954 if (tcp->tcp_debug) { 3955 (void) strlog(TCP_MOD_ID, 0, 1, 3956 SL_ERROR|SL_TRACE, 3957 "tcp_clean_death, sending M_ERROR"); 3958 } 3959 (void) putnextctl1(q, M_ERROR, EPROTO); 3960 } 3961 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3962 /* SYN_SENT or SYN_RCVD */ 3963 BUMP_MIB(&tcp_mib, tcpAttemptFails); 3964 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3965 /* ESTABLISHED or CLOSE_WAIT */ 3966 BUMP_MIB(&tcp_mib, tcpEstabResets); 3967 } 3968 } 3969 3970 tcp_reinit(tcp); 3971 return (-1); 3972 } 3973 3974 /* 3975 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3976 * to expire, stop the wait and finish the close. 3977 */ 3978 static void 3979 tcp_stop_lingering(tcp_t *tcp) 3980 { 3981 clock_t delta = 0; 3982 3983 tcp->tcp_linger_tid = 0; 3984 if (tcp->tcp_state > TCPS_LISTEN) { 3985 tcp_acceptor_hash_remove(tcp); 3986 if (tcp->tcp_flow_stopped) { 3987 tcp_clrqfull(tcp); 3988 } 3989 3990 if (tcp->tcp_timer_tid != 0) { 3991 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3992 tcp->tcp_timer_tid = 0; 3993 } 3994 /* 3995 * Need to cancel those timers which will not be used when 3996 * TCP is detached. This has to be done before the tcp_wq 3997 * is set to the global queue. 3998 */ 3999 tcp_timers_stop(tcp); 4000 4001 4002 tcp->tcp_detached = B_TRUE; 4003 tcp->tcp_rq = tcp_g_q; 4004 tcp->tcp_wq = WR(tcp_g_q); 4005 4006 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4007 tcp_time_wait_append(tcp); 4008 TCP_DBGSTAT(tcp_detach_time_wait); 4009 goto finish; 4010 } 4011 4012 /* 4013 * If delta is zero the timer event wasn't executed and was 4014 * successfully canceled. In this case we need to restart it 4015 * with the minimal delta possible. 4016 */ 4017 if (delta >= 0) { 4018 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4019 delta ? delta : 1); 4020 } 4021 } else { 4022 tcp_closei_local(tcp); 4023 CONN_DEC_REF(tcp->tcp_connp); 4024 } 4025 finish: 4026 /* Signal closing thread that it can complete close */ 4027 mutex_enter(&tcp->tcp_closelock); 4028 tcp->tcp_detached = B_TRUE; 4029 tcp->tcp_rq = tcp_g_q; 4030 tcp->tcp_wq = WR(tcp_g_q); 4031 tcp->tcp_closed = 1; 4032 cv_signal(&tcp->tcp_closecv); 4033 mutex_exit(&tcp->tcp_closelock); 4034 } 4035 4036 /* 4037 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 4038 * expires. 4039 */ 4040 static void 4041 tcp_close_linger_timeout(void *arg) 4042 { 4043 conn_t *connp = (conn_t *)arg; 4044 tcp_t *tcp = connp->conn_tcp; 4045 4046 tcp->tcp_client_errno = ETIMEDOUT; 4047 tcp_stop_lingering(tcp); 4048 } 4049 4050 static int 4051 tcp_close(queue_t *q, int flags) 4052 { 4053 conn_t *connp = Q_TO_CONN(q); 4054 tcp_t *tcp = connp->conn_tcp; 4055 mblk_t *mp = &tcp->tcp_closemp; 4056 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 4057 boolean_t linger_interrupted = B_FALSE; 4058 mblk_t *bp; 4059 4060 ASSERT(WR(q)->q_next == NULL); 4061 ASSERT(connp->conn_ref >= 2); 4062 ASSERT((connp->conn_flags & IPCL_TCPMOD) == 0); 4063 4064 /* 4065 * We are being closed as /dev/tcp or /dev/tcp6. 4066 * 4067 * Mark the conn as closing. ill_pending_mp_add will not 4068 * add any mp to the pending mp list, after this conn has 4069 * started closing. Same for sq_pending_mp_add 4070 */ 4071 mutex_enter(&connp->conn_lock); 4072 connp->conn_state_flags |= CONN_CLOSING; 4073 if (connp->conn_oper_pending_ill != NULL) 4074 conn_ioctl_cleanup_reqd = B_TRUE; 4075 CONN_INC_REF_LOCKED(connp); 4076 mutex_exit(&connp->conn_lock); 4077 tcp->tcp_closeflags = (uint8_t)flags; 4078 ASSERT(connp->conn_ref >= 3); 4079 4080 /* 4081 * tcp_closemp_used is used below without any protection of a lock 4082 * as we don't expect any one else to use it concurrently at this 4083 * point otherwise it would be a major defect, though we do 4084 * increment tcp_closemp_used to record any attempt to reuse 4085 * tcp_closemp while it is still in use. This would help debugging. 4086 */ 4087 4088 if (mp->b_prev == NULL) { 4089 tcp->tcp_closemp_used = 1; 4090 } else { 4091 tcp->tcp_closemp_used++; 4092 ASSERT(mp->b_prev == NULL); 4093 } 4094 4095 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4096 4097 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4098 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4099 4100 mutex_enter(&tcp->tcp_closelock); 4101 while (!tcp->tcp_closed) { 4102 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4103 /* 4104 * We got interrupted. Check if we are lingering, 4105 * if yes, post a message to stop and wait until 4106 * tcp_closed is set. If we aren't lingering, 4107 * just go back around. 4108 */ 4109 if (tcp->tcp_linger && 4110 tcp->tcp_lingertime > 0 && 4111 !linger_interrupted) { 4112 mutex_exit(&tcp->tcp_closelock); 4113 /* Entering squeue, bump ref count. */ 4114 CONN_INC_REF(connp); 4115 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4116 squeue_enter(connp->conn_sqp, bp, 4117 tcp_linger_interrupted, connp, 4118 SQTAG_IP_TCP_CLOSE); 4119 linger_interrupted = B_TRUE; 4120 mutex_enter(&tcp->tcp_closelock); 4121 } 4122 } 4123 } 4124 mutex_exit(&tcp->tcp_closelock); 4125 4126 /* 4127 * In the case of listener streams that have eagers in the q or q0 4128 * we wait for the eagers to drop their reference to us. tcp_rq and 4129 * tcp_wq of the eagers point to our queues. By waiting for the 4130 * refcnt to drop to 1, we are sure that the eagers have cleaned 4131 * up their queue pointers and also dropped their references to us. 4132 */ 4133 if (tcp->tcp_wait_for_eagers) { 4134 mutex_enter(&connp->conn_lock); 4135 while (connp->conn_ref != 1) { 4136 cv_wait(&connp->conn_cv, &connp->conn_lock); 4137 } 4138 mutex_exit(&connp->conn_lock); 4139 } 4140 /* 4141 * ioctl cleanup. The mp is queued in the 4142 * ill_pending_mp or in the sq_pending_mp. 4143 */ 4144 if (conn_ioctl_cleanup_reqd) 4145 conn_ioctl_cleanup(connp); 4146 4147 qprocsoff(q); 4148 inet_minor_free(ip_minor_arena, connp->conn_dev); 4149 4150 tcp->tcp_cpid = -1; 4151 4152 /* 4153 * Drop IP's reference on the conn. This is the last reference 4154 * on the connp if the state was less than established. If the 4155 * connection has gone into timewait state, then we will have 4156 * one ref for the TCP and one more ref (total of two) for the 4157 * classifier connected hash list (a timewait connections stays 4158 * in connected hash till closed). 4159 * 4160 * We can't assert the references because there might be other 4161 * transient reference places because of some walkers or queued 4162 * packets in squeue for the timewait state. 4163 */ 4164 CONN_DEC_REF(connp); 4165 q->q_ptr = WR(q)->q_ptr = NULL; 4166 return (0); 4167 } 4168 4169 static int 4170 tcpclose_accept(queue_t *q) 4171 { 4172 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4173 4174 /* 4175 * We had opened an acceptor STREAM for sockfs which is 4176 * now being closed due to some error. 4177 */ 4178 qprocsoff(q); 4179 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 4180 q->q_ptr = WR(q)->q_ptr = NULL; 4181 return (0); 4182 } 4183 4184 /* 4185 * Called by tcp_close() routine via squeue when lingering is 4186 * interrupted by a signal. 4187 */ 4188 4189 /* ARGSUSED */ 4190 static void 4191 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4192 { 4193 conn_t *connp = (conn_t *)arg; 4194 tcp_t *tcp = connp->conn_tcp; 4195 4196 freeb(mp); 4197 if (tcp->tcp_linger_tid != 0 && 4198 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4199 tcp_stop_lingering(tcp); 4200 tcp->tcp_client_errno = EINTR; 4201 } 4202 } 4203 4204 /* 4205 * Called by streams close routine via squeues when our client blows off her 4206 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4207 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4208 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4209 * acked. 4210 * 4211 * NOTE: tcp_close potentially returns error when lingering. 4212 * However, the stream head currently does not pass these errors 4213 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4214 * errors to the application (from tsleep()) and not errors 4215 * like ECONNRESET caused by receiving a reset packet. 4216 */ 4217 4218 /* ARGSUSED */ 4219 static void 4220 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4221 { 4222 char *msg; 4223 conn_t *connp = (conn_t *)arg; 4224 tcp_t *tcp = connp->conn_tcp; 4225 clock_t delta = 0; 4226 4227 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4228 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4229 4230 /* Cancel any pending timeout */ 4231 if (tcp->tcp_ordrelid != 0) { 4232 if (tcp->tcp_timeout) { 4233 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4234 } 4235 tcp->tcp_ordrelid = 0; 4236 tcp->tcp_timeout = B_FALSE; 4237 } 4238 4239 mutex_enter(&tcp->tcp_eager_lock); 4240 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4241 /* Cleanup for listener */ 4242 tcp_eager_cleanup(tcp, 0); 4243 tcp->tcp_wait_for_eagers = 1; 4244 } 4245 mutex_exit(&tcp->tcp_eager_lock); 4246 4247 connp->conn_mdt_ok = B_FALSE; 4248 tcp->tcp_mdt = B_FALSE; 4249 4250 connp->conn_lso_ok = B_FALSE; 4251 tcp->tcp_lso = B_FALSE; 4252 4253 msg = NULL; 4254 switch (tcp->tcp_state) { 4255 case TCPS_CLOSED: 4256 case TCPS_IDLE: 4257 case TCPS_BOUND: 4258 case TCPS_LISTEN: 4259 break; 4260 case TCPS_SYN_SENT: 4261 msg = "tcp_close, during connect"; 4262 break; 4263 case TCPS_SYN_RCVD: 4264 /* 4265 * Close during the connect 3-way handshake 4266 * but here there may or may not be pending data 4267 * already on queue. Process almost same as in 4268 * the ESTABLISHED state. 4269 */ 4270 /* FALLTHRU */ 4271 default: 4272 if (tcp->tcp_fused) 4273 tcp_unfuse(tcp); 4274 4275 /* 4276 * If SO_LINGER has set a zero linger time, abort the 4277 * connection with a reset. 4278 */ 4279 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4280 msg = "tcp_close, zero lingertime"; 4281 break; 4282 } 4283 4284 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4285 /* 4286 * Abort connection if there is unread data queued. 4287 */ 4288 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4289 msg = "tcp_close, unread data"; 4290 break; 4291 } 4292 /* 4293 * tcp_hard_bound is now cleared thus all packets go through 4294 * tcp_lookup. This fact is used by tcp_detach below. 4295 * 4296 * We have done a qwait() above which could have possibly 4297 * drained more messages in turn causing transition to a 4298 * different state. Check whether we have to do the rest 4299 * of the processing or not. 4300 */ 4301 if (tcp->tcp_state <= TCPS_LISTEN) 4302 break; 4303 4304 /* 4305 * Transmit the FIN before detaching the tcp_t. 4306 * After tcp_detach returns this queue/perimeter 4307 * no longer owns the tcp_t thus others can modify it. 4308 */ 4309 (void) tcp_xmit_end(tcp); 4310 4311 /* 4312 * If lingering on close then wait until the fin is acked, 4313 * the SO_LINGER time passes, or a reset is sent/received. 4314 */ 4315 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4316 !(tcp->tcp_fin_acked) && 4317 tcp->tcp_state >= TCPS_ESTABLISHED) { 4318 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4319 tcp->tcp_client_errno = EWOULDBLOCK; 4320 } else if (tcp->tcp_client_errno == 0) { 4321 4322 ASSERT(tcp->tcp_linger_tid == 0); 4323 4324 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4325 tcp_close_linger_timeout, 4326 tcp->tcp_lingertime * hz); 4327 4328 /* tcp_close_linger_timeout will finish close */ 4329 if (tcp->tcp_linger_tid == 0) 4330 tcp->tcp_client_errno = ENOSR; 4331 else 4332 return; 4333 } 4334 4335 /* 4336 * Check if we need to detach or just close 4337 * the instance. 4338 */ 4339 if (tcp->tcp_state <= TCPS_LISTEN) 4340 break; 4341 } 4342 4343 /* 4344 * Make sure that no other thread will access the tcp_rq of 4345 * this instance (through lookups etc.) as tcp_rq will go 4346 * away shortly. 4347 */ 4348 tcp_acceptor_hash_remove(tcp); 4349 4350 if (tcp->tcp_flow_stopped) { 4351 tcp_clrqfull(tcp); 4352 } 4353 4354 if (tcp->tcp_timer_tid != 0) { 4355 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4356 tcp->tcp_timer_tid = 0; 4357 } 4358 /* 4359 * Need to cancel those timers which will not be used when 4360 * TCP is detached. This has to be done before the tcp_wq 4361 * is set to the global queue. 4362 */ 4363 tcp_timers_stop(tcp); 4364 4365 tcp->tcp_detached = B_TRUE; 4366 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4367 tcp_time_wait_append(tcp); 4368 TCP_DBGSTAT(tcp_detach_time_wait); 4369 ASSERT(connp->conn_ref >= 3); 4370 goto finish; 4371 } 4372 4373 /* 4374 * If delta is zero the timer event wasn't executed and was 4375 * successfully canceled. In this case we need to restart it 4376 * with the minimal delta possible. 4377 */ 4378 if (delta >= 0) 4379 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4380 delta ? delta : 1); 4381 4382 ASSERT(connp->conn_ref >= 3); 4383 goto finish; 4384 } 4385 4386 /* Detach did not complete. Still need to remove q from stream. */ 4387 if (msg) { 4388 if (tcp->tcp_state == TCPS_ESTABLISHED || 4389 tcp->tcp_state == TCPS_CLOSE_WAIT) 4390 BUMP_MIB(&tcp_mib, tcpEstabResets); 4391 if (tcp->tcp_state == TCPS_SYN_SENT || 4392 tcp->tcp_state == TCPS_SYN_RCVD) 4393 BUMP_MIB(&tcp_mib, tcpAttemptFails); 4394 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4395 } 4396 4397 tcp_closei_local(tcp); 4398 CONN_DEC_REF(connp); 4399 ASSERT(connp->conn_ref >= 2); 4400 4401 finish: 4402 /* 4403 * Although packets are always processed on the correct 4404 * tcp's perimeter and access is serialized via squeue's, 4405 * IP still needs a queue when sending packets in time_wait 4406 * state so use WR(tcp_g_q) till ip_output() can be 4407 * changed to deal with just connp. For read side, we 4408 * could have set tcp_rq to NULL but there are some cases 4409 * in tcp_rput_data() from early days of this code which 4410 * do a putnext without checking if tcp is closed. Those 4411 * need to be identified before both tcp_rq and tcp_wq 4412 * can be set to NULL and tcp_q_q can disappear forever. 4413 */ 4414 mutex_enter(&tcp->tcp_closelock); 4415 /* 4416 * Don't change the queues in the case of a listener that has 4417 * eagers in its q or q0. It could surprise the eagers. 4418 * Instead wait for the eagers outside the squeue. 4419 */ 4420 if (!tcp->tcp_wait_for_eagers) { 4421 tcp->tcp_detached = B_TRUE; 4422 tcp->tcp_rq = tcp_g_q; 4423 tcp->tcp_wq = WR(tcp_g_q); 4424 } 4425 4426 /* Signal tcp_close() to finish closing. */ 4427 tcp->tcp_closed = 1; 4428 cv_signal(&tcp->tcp_closecv); 4429 mutex_exit(&tcp->tcp_closelock); 4430 } 4431 4432 4433 /* 4434 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4435 * Some stream heads get upset if they see these later on as anything but NULL. 4436 */ 4437 static void 4438 tcp_close_mpp(mblk_t **mpp) 4439 { 4440 mblk_t *mp; 4441 4442 if ((mp = *mpp) != NULL) { 4443 do { 4444 mp->b_next = NULL; 4445 mp->b_prev = NULL; 4446 } while ((mp = mp->b_cont) != NULL); 4447 4448 mp = *mpp; 4449 *mpp = NULL; 4450 freemsg(mp); 4451 } 4452 } 4453 4454 /* Do detached close. */ 4455 static void 4456 tcp_close_detached(tcp_t *tcp) 4457 { 4458 if (tcp->tcp_fused) 4459 tcp_unfuse(tcp); 4460 4461 /* 4462 * Clustering code serializes TCP disconnect callbacks and 4463 * cluster tcp list walks by blocking a TCP disconnect callback 4464 * if a cluster tcp list walk is in progress. This ensures 4465 * accurate accounting of TCPs in the cluster code even though 4466 * the TCP list walk itself is not atomic. 4467 */ 4468 tcp_closei_local(tcp); 4469 CONN_DEC_REF(tcp->tcp_connp); 4470 } 4471 4472 /* 4473 * Stop all TCP timers, and free the timer mblks if requested. 4474 */ 4475 void 4476 tcp_timers_stop(tcp_t *tcp) 4477 { 4478 if (tcp->tcp_timer_tid != 0) { 4479 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4480 tcp->tcp_timer_tid = 0; 4481 } 4482 if (tcp->tcp_ka_tid != 0) { 4483 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4484 tcp->tcp_ka_tid = 0; 4485 } 4486 if (tcp->tcp_ack_tid != 0) { 4487 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4488 tcp->tcp_ack_tid = 0; 4489 } 4490 if (tcp->tcp_push_tid != 0) { 4491 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4492 tcp->tcp_push_tid = 0; 4493 } 4494 } 4495 4496 /* 4497 * The tcp_t is going away. Remove it from all lists and set it 4498 * to TCPS_CLOSED. The freeing up of memory is deferred until 4499 * tcp_inactive. This is needed since a thread in tcp_rput might have 4500 * done a CONN_INC_REF on this structure before it was removed from the 4501 * hashes. 4502 */ 4503 static void 4504 tcp_closei_local(tcp_t *tcp) 4505 { 4506 ire_t *ire; 4507 conn_t *connp = tcp->tcp_connp; 4508 4509 if (!TCP_IS_SOCKET(tcp)) 4510 tcp_acceptor_hash_remove(tcp); 4511 4512 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 4513 tcp->tcp_ibsegs = 0; 4514 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 4515 tcp->tcp_obsegs = 0; 4516 4517 /* 4518 * If we are an eager connection hanging off a listener that 4519 * hasn't formally accepted the connection yet, get off his 4520 * list and blow off any data that we have accumulated. 4521 */ 4522 if (tcp->tcp_listener != NULL) { 4523 tcp_t *listener = tcp->tcp_listener; 4524 mutex_enter(&listener->tcp_eager_lock); 4525 /* 4526 * tcp_tconnind_started == B_TRUE means that the 4527 * conn_ind has already gone to listener. At 4528 * this point, eager will be closed but we 4529 * leave it in listeners eager list so that 4530 * if listener decides to close without doing 4531 * accept, we can clean this up. In tcp_wput_accept 4532 * we take care of the case of accept on closed 4533 * eager. 4534 */ 4535 if (!tcp->tcp_tconnind_started) { 4536 tcp_eager_unlink(tcp); 4537 mutex_exit(&listener->tcp_eager_lock); 4538 /* 4539 * We don't want to have any pointers to the 4540 * listener queue, after we have released our 4541 * reference on the listener 4542 */ 4543 tcp->tcp_rq = tcp_g_q; 4544 tcp->tcp_wq = WR(tcp_g_q); 4545 CONN_DEC_REF(listener->tcp_connp); 4546 } else { 4547 mutex_exit(&listener->tcp_eager_lock); 4548 } 4549 } 4550 4551 /* Stop all the timers */ 4552 tcp_timers_stop(tcp); 4553 4554 if (tcp->tcp_state == TCPS_LISTEN) { 4555 if (tcp->tcp_ip_addr_cache) { 4556 kmem_free((void *)tcp->tcp_ip_addr_cache, 4557 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4558 tcp->tcp_ip_addr_cache = NULL; 4559 } 4560 } 4561 if (tcp->tcp_flow_stopped) 4562 tcp_clrqfull(tcp); 4563 4564 tcp_bind_hash_remove(tcp); 4565 /* 4566 * If the tcp_time_wait_collector (which runs outside the squeue) 4567 * is trying to remove this tcp from the time wait list, we will 4568 * block in tcp_time_wait_remove while trying to acquire the 4569 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4570 * requires the ipcl_hash_remove to be ordered after the 4571 * tcp_time_wait_remove for the refcnt checks to work correctly. 4572 */ 4573 if (tcp->tcp_state == TCPS_TIME_WAIT) 4574 (void) tcp_time_wait_remove(tcp, NULL); 4575 CL_INET_DISCONNECT(tcp); 4576 ipcl_hash_remove(connp); 4577 4578 /* 4579 * Delete the cached ire in conn_ire_cache and also mark 4580 * the conn as CONDEMNED 4581 */ 4582 mutex_enter(&connp->conn_lock); 4583 connp->conn_state_flags |= CONN_CONDEMNED; 4584 ire = connp->conn_ire_cache; 4585 connp->conn_ire_cache = NULL; 4586 mutex_exit(&connp->conn_lock); 4587 if (ire != NULL) 4588 IRE_REFRELE_NOTR(ire); 4589 4590 /* Need to cleanup any pending ioctls */ 4591 ASSERT(tcp->tcp_time_wait_next == NULL); 4592 ASSERT(tcp->tcp_time_wait_prev == NULL); 4593 ASSERT(tcp->tcp_time_wait_expire == 0); 4594 tcp->tcp_state = TCPS_CLOSED; 4595 4596 /* Release any SSL context */ 4597 if (tcp->tcp_kssl_ent != NULL) { 4598 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4599 tcp->tcp_kssl_ent = NULL; 4600 } 4601 if (tcp->tcp_kssl_ctx != NULL) { 4602 kssl_release_ctx(tcp->tcp_kssl_ctx); 4603 tcp->tcp_kssl_ctx = NULL; 4604 } 4605 tcp->tcp_kssl_pending = B_FALSE; 4606 } 4607 4608 /* 4609 * tcp is dying (called from ipcl_conn_destroy and error cases). 4610 * Free the tcp_t in either case. 4611 */ 4612 void 4613 tcp_free(tcp_t *tcp) 4614 { 4615 mblk_t *mp; 4616 ip6_pkt_t *ipp; 4617 4618 ASSERT(tcp != NULL); 4619 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4620 4621 tcp->tcp_rq = NULL; 4622 tcp->tcp_wq = NULL; 4623 4624 tcp_close_mpp(&tcp->tcp_xmit_head); 4625 tcp_close_mpp(&tcp->tcp_reass_head); 4626 if (tcp->tcp_rcv_list != NULL) { 4627 /* Free b_next chain */ 4628 tcp_close_mpp(&tcp->tcp_rcv_list); 4629 } 4630 if ((mp = tcp->tcp_urp_mp) != NULL) { 4631 freemsg(mp); 4632 } 4633 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4634 freemsg(mp); 4635 } 4636 4637 if (tcp->tcp_fused_sigurg_mp != NULL) { 4638 freeb(tcp->tcp_fused_sigurg_mp); 4639 tcp->tcp_fused_sigurg_mp = NULL; 4640 } 4641 4642 if (tcp->tcp_sack_info != NULL) { 4643 if (tcp->tcp_notsack_list != NULL) { 4644 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4645 } 4646 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4647 } 4648 4649 if (tcp->tcp_hopopts != NULL) { 4650 mi_free(tcp->tcp_hopopts); 4651 tcp->tcp_hopopts = NULL; 4652 tcp->tcp_hopoptslen = 0; 4653 } 4654 ASSERT(tcp->tcp_hopoptslen == 0); 4655 if (tcp->tcp_dstopts != NULL) { 4656 mi_free(tcp->tcp_dstopts); 4657 tcp->tcp_dstopts = NULL; 4658 tcp->tcp_dstoptslen = 0; 4659 } 4660 ASSERT(tcp->tcp_dstoptslen == 0); 4661 if (tcp->tcp_rtdstopts != NULL) { 4662 mi_free(tcp->tcp_rtdstopts); 4663 tcp->tcp_rtdstopts = NULL; 4664 tcp->tcp_rtdstoptslen = 0; 4665 } 4666 ASSERT(tcp->tcp_rtdstoptslen == 0); 4667 if (tcp->tcp_rthdr != NULL) { 4668 mi_free(tcp->tcp_rthdr); 4669 tcp->tcp_rthdr = NULL; 4670 tcp->tcp_rthdrlen = 0; 4671 } 4672 ASSERT(tcp->tcp_rthdrlen == 0); 4673 4674 ipp = &tcp->tcp_sticky_ipp; 4675 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4676 IPPF_RTHDR)) 4677 ip6_pkt_free(ipp); 4678 4679 /* 4680 * Free memory associated with the tcp/ip header template. 4681 */ 4682 4683 if (tcp->tcp_iphc != NULL) 4684 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4685 4686 /* 4687 * Following is really a blowing away a union. 4688 * It happens to have exactly two members of identical size 4689 * the following code is enough. 4690 */ 4691 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4692 4693 if (tcp->tcp_tracebuf != NULL) { 4694 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4695 tcp->tcp_tracebuf = NULL; 4696 } 4697 } 4698 4699 4700 /* 4701 * Put a connection confirmation message upstream built from the 4702 * address information within 'iph' and 'tcph'. Report our success or failure. 4703 */ 4704 static boolean_t 4705 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4706 mblk_t **defermp) 4707 { 4708 sin_t sin; 4709 sin6_t sin6; 4710 mblk_t *mp; 4711 char *optp = NULL; 4712 int optlen = 0; 4713 cred_t *cr; 4714 4715 if (defermp != NULL) 4716 *defermp = NULL; 4717 4718 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4719 /* 4720 * Return in T_CONN_CON results of option negotiation through 4721 * the T_CONN_REQ. Note: If there is an real end-to-end option 4722 * negotiation, then what is received from remote end needs 4723 * to be taken into account but there is no such thing (yet?) 4724 * in our TCP/IP. 4725 * Note: We do not use mi_offset_param() here as 4726 * tcp_opts_conn_req contents do not directly come from 4727 * an application and are either generated in kernel or 4728 * from user input that was already verified. 4729 */ 4730 mp = tcp->tcp_conn.tcp_opts_conn_req; 4731 optp = (char *)(mp->b_rptr + 4732 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4733 optlen = (int) 4734 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4735 } 4736 4737 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4738 ipha_t *ipha = (ipha_t *)iphdr; 4739 4740 /* packet is IPv4 */ 4741 if (tcp->tcp_family == AF_INET) { 4742 sin = sin_null; 4743 sin.sin_addr.s_addr = ipha->ipha_src; 4744 sin.sin_port = *(uint16_t *)tcph->th_lport; 4745 sin.sin_family = AF_INET; 4746 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4747 (int)sizeof (sin_t), optp, optlen); 4748 } else { 4749 sin6 = sin6_null; 4750 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4751 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4752 sin6.sin6_family = AF_INET6; 4753 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4754 (int)sizeof (sin6_t), optp, optlen); 4755 4756 } 4757 } else { 4758 ip6_t *ip6h = (ip6_t *)iphdr; 4759 4760 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4761 ASSERT(tcp->tcp_family == AF_INET6); 4762 sin6 = sin6_null; 4763 sin6.sin6_addr = ip6h->ip6_src; 4764 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4765 sin6.sin6_family = AF_INET6; 4766 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4767 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4768 (int)sizeof (sin6_t), optp, optlen); 4769 } 4770 4771 if (!mp) 4772 return (B_FALSE); 4773 4774 if ((cr = DB_CRED(idmp)) != NULL) { 4775 mblk_setcred(mp, cr); 4776 DB_CPID(mp) = DB_CPID(idmp); 4777 } 4778 4779 if (defermp == NULL) 4780 putnext(tcp->tcp_rq, mp); 4781 else 4782 *defermp = mp; 4783 4784 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4785 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4786 return (B_TRUE); 4787 } 4788 4789 /* 4790 * Defense for the SYN attack - 4791 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4792 * one from the list of droppable eagers. This list is a subset of q0. 4793 * see comments before the definition of MAKE_DROPPABLE(). 4794 * 2. Don't drop a SYN request before its first timeout. This gives every 4795 * request at least til the first timeout to complete its 3-way handshake. 4796 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4797 * requests currently on the queue that has timed out. This will be used 4798 * as an indicator of whether an attack is under way, so that appropriate 4799 * actions can be taken. (It's incremented in tcp_timer() and decremented 4800 * either when eager goes into ESTABLISHED, or gets freed up.) 4801 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4802 * # of timeout drops back to <= q0len/32 => SYN alert off 4803 */ 4804 static boolean_t 4805 tcp_drop_q0(tcp_t *tcp) 4806 { 4807 tcp_t *eager; 4808 mblk_t *mp; 4809 4810 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4811 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4812 4813 /* Pick oldest eager from the list of droppable eagers */ 4814 eager = tcp->tcp_eager_prev_drop_q0; 4815 4816 /* If list is empty. return B_FALSE */ 4817 if (eager == tcp) { 4818 return (B_FALSE); 4819 } 4820 4821 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4822 if ((mp = allocb(0, BPRI_HI)) == NULL) 4823 return (B_FALSE); 4824 4825 /* 4826 * Take this eager out from the list of droppable eagers since we are 4827 * going to drop it. 4828 */ 4829 MAKE_UNDROPPABLE(eager); 4830 4831 if (tcp->tcp_debug) { 4832 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4833 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4834 " (%d pending) on %s, drop one", tcp_conn_req_max_q0, 4835 tcp->tcp_conn_req_cnt_q0, 4836 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4837 } 4838 4839 BUMP_MIB(&tcp_mib, tcpHalfOpenDrop); 4840 4841 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4842 CONN_INC_REF(eager->tcp_connp); 4843 4844 /* Mark the IRE created for this SYN request temporary */ 4845 tcp_ip_ire_mark_advice(eager); 4846 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4847 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4848 4849 return (B_TRUE); 4850 } 4851 4852 int 4853 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4854 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4855 { 4856 tcp_t *ltcp = lconnp->conn_tcp; 4857 tcp_t *tcp = connp->conn_tcp; 4858 mblk_t *tpi_mp; 4859 ipha_t *ipha; 4860 ip6_t *ip6h; 4861 sin6_t sin6; 4862 in6_addr_t v6dst; 4863 int err; 4864 int ifindex = 0; 4865 cred_t *cr; 4866 4867 if (ipvers == IPV4_VERSION) { 4868 ipha = (ipha_t *)mp->b_rptr; 4869 4870 connp->conn_send = ip_output; 4871 connp->conn_recv = tcp_input; 4872 4873 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4874 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4875 4876 sin6 = sin6_null; 4877 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4878 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4879 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4880 sin6.sin6_family = AF_INET6; 4881 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4882 lconnp->conn_zoneid); 4883 if (tcp->tcp_recvdstaddr) { 4884 sin6_t sin6d; 4885 4886 sin6d = sin6_null; 4887 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4888 &sin6d.sin6_addr); 4889 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4890 sin6d.sin6_family = AF_INET; 4891 tpi_mp = mi_tpi_extconn_ind(NULL, 4892 (char *)&sin6d, sizeof (sin6_t), 4893 (char *)&tcp, 4894 (t_scalar_t)sizeof (intptr_t), 4895 (char *)&sin6d, sizeof (sin6_t), 4896 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4897 } else { 4898 tpi_mp = mi_tpi_conn_ind(NULL, 4899 (char *)&sin6, sizeof (sin6_t), 4900 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4901 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4902 } 4903 } else { 4904 ip6h = (ip6_t *)mp->b_rptr; 4905 4906 connp->conn_send = ip_output_v6; 4907 connp->conn_recv = tcp_input; 4908 4909 connp->conn_srcv6 = ip6h->ip6_dst; 4910 connp->conn_remv6 = ip6h->ip6_src; 4911 4912 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4913 ifindex = (int)DB_CKSUMSTUFF(mp); 4914 DB_CKSUMSTUFF(mp) = 0; 4915 4916 sin6 = sin6_null; 4917 sin6.sin6_addr = ip6h->ip6_src; 4918 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4919 sin6.sin6_family = AF_INET6; 4920 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4921 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4922 lconnp->conn_zoneid); 4923 4924 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4925 /* Pass up the scope_id of remote addr */ 4926 sin6.sin6_scope_id = ifindex; 4927 } else { 4928 sin6.sin6_scope_id = 0; 4929 } 4930 if (tcp->tcp_recvdstaddr) { 4931 sin6_t sin6d; 4932 4933 sin6d = sin6_null; 4934 sin6.sin6_addr = ip6h->ip6_dst; 4935 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4936 sin6d.sin6_family = AF_INET; 4937 tpi_mp = mi_tpi_extconn_ind(NULL, 4938 (char *)&sin6d, sizeof (sin6_t), 4939 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4940 (char *)&sin6d, sizeof (sin6_t), 4941 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4942 } else { 4943 tpi_mp = mi_tpi_conn_ind(NULL, 4944 (char *)&sin6, sizeof (sin6_t), 4945 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4946 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4947 } 4948 } 4949 4950 if (tpi_mp == NULL) 4951 return (ENOMEM); 4952 4953 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4954 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4955 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4956 connp->conn_fully_bound = B_FALSE; 4957 4958 if (tcp_trace) 4959 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4960 4961 /* Inherit information from the "parent" */ 4962 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4963 tcp->tcp_family = ltcp->tcp_family; 4964 tcp->tcp_wq = ltcp->tcp_wq; 4965 tcp->tcp_rq = ltcp->tcp_rq; 4966 tcp->tcp_mss = tcp_mss_def_ipv6; 4967 tcp->tcp_detached = B_TRUE; 4968 if ((err = tcp_init_values(tcp)) != 0) { 4969 freemsg(tpi_mp); 4970 return (err); 4971 } 4972 4973 if (ipvers == IPV4_VERSION) { 4974 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4975 freemsg(tpi_mp); 4976 return (err); 4977 } 4978 ASSERT(tcp->tcp_ipha != NULL); 4979 } else { 4980 /* ifindex must be already set */ 4981 ASSERT(ifindex != 0); 4982 4983 if (ltcp->tcp_bound_if != 0) { 4984 /* 4985 * Set newtcp's bound_if equal to 4986 * listener's value. If ifindex is 4987 * not the same as ltcp->tcp_bound_if, 4988 * it must be a packet for the ipmp group 4989 * of interfaces 4990 */ 4991 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4992 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4993 tcp->tcp_bound_if = ifindex; 4994 } 4995 4996 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4997 tcp->tcp_recvifindex = 0; 4998 tcp->tcp_recvhops = 0xffffffffU; 4999 ASSERT(tcp->tcp_ip6h != NULL); 5000 } 5001 5002 tcp->tcp_lport = ltcp->tcp_lport; 5003 5004 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 5005 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 5006 /* 5007 * Listener had options of some sort; eager inherits. 5008 * Free up the eager template and allocate one 5009 * of the right size. 5010 */ 5011 if (tcp->tcp_hdr_grown) { 5012 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 5013 } else { 5014 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 5015 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 5016 } 5017 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 5018 KM_NOSLEEP); 5019 if (tcp->tcp_iphc == NULL) { 5020 tcp->tcp_iphc_len = 0; 5021 freemsg(tpi_mp); 5022 return (ENOMEM); 5023 } 5024 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 5025 tcp->tcp_hdr_grown = B_TRUE; 5026 } 5027 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5028 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5029 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5030 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 5031 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 5032 5033 /* 5034 * Copy the IP+TCP header template from listener to eager 5035 */ 5036 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5037 if (tcp->tcp_ipversion == IPV6_VERSION) { 5038 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 5039 IPPROTO_RAW) { 5040 tcp->tcp_ip6h = 5041 (ip6_t *)(tcp->tcp_iphc + 5042 sizeof (ip6i_t)); 5043 } else { 5044 tcp->tcp_ip6h = 5045 (ip6_t *)(tcp->tcp_iphc); 5046 } 5047 tcp->tcp_ipha = NULL; 5048 } else { 5049 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5050 tcp->tcp_ip6h = NULL; 5051 } 5052 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5053 tcp->tcp_ip_hdr_len); 5054 } else { 5055 /* 5056 * only valid case when ipversion of listener and 5057 * eager differ is when listener is IPv6 and 5058 * eager is IPv4. 5059 * Eager header template has been initialized to the 5060 * maximum v4 header sizes, which includes space for 5061 * TCP and IP options. 5062 */ 5063 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5064 (tcp->tcp_ipversion == IPV4_VERSION)); 5065 ASSERT(tcp->tcp_iphc_len >= 5066 TCP_MAX_COMBINED_HEADER_LENGTH); 5067 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5068 /* copy IP header fields individually */ 5069 tcp->tcp_ipha->ipha_ttl = 5070 ltcp->tcp_ip6h->ip6_hops; 5071 bcopy(ltcp->tcp_tcph->th_lport, 5072 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5073 } 5074 5075 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5076 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5077 sizeof (in_port_t)); 5078 5079 if (ltcp->tcp_lport == 0) { 5080 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5081 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5082 sizeof (in_port_t)); 5083 } 5084 5085 if (tcp->tcp_ipversion == IPV4_VERSION) { 5086 ASSERT(ipha != NULL); 5087 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5088 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5089 5090 /* Source routing option copyover (reverse it) */ 5091 if (tcp_rev_src_routes) 5092 tcp_opt_reverse(tcp, ipha); 5093 } else { 5094 ASSERT(ip6h != NULL); 5095 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5096 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5097 } 5098 5099 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5100 ASSERT(!tcp->tcp_tconnind_started); 5101 /* 5102 * If the SYN contains a credential, it's a loopback packet; attach 5103 * the credential to the TPI message. 5104 */ 5105 if ((cr = DB_CRED(idmp)) != NULL) { 5106 mblk_setcred(tpi_mp, cr); 5107 DB_CPID(tpi_mp) = DB_CPID(idmp); 5108 } 5109 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5110 5111 /* Inherit the listener's SSL protection state */ 5112 5113 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5114 kssl_hold_ent(tcp->tcp_kssl_ent); 5115 tcp->tcp_kssl_pending = B_TRUE; 5116 } 5117 5118 return (0); 5119 } 5120 5121 5122 int 5123 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5124 tcph_t *tcph, mblk_t *idmp) 5125 { 5126 tcp_t *ltcp = lconnp->conn_tcp; 5127 tcp_t *tcp = connp->conn_tcp; 5128 sin_t sin; 5129 mblk_t *tpi_mp = NULL; 5130 int err; 5131 cred_t *cr; 5132 5133 sin = sin_null; 5134 sin.sin_addr.s_addr = ipha->ipha_src; 5135 sin.sin_port = *(uint16_t *)tcph->th_lport; 5136 sin.sin_family = AF_INET; 5137 if (ltcp->tcp_recvdstaddr) { 5138 sin_t sind; 5139 5140 sind = sin_null; 5141 sind.sin_addr.s_addr = ipha->ipha_dst; 5142 sind.sin_port = *(uint16_t *)tcph->th_fport; 5143 sind.sin_family = AF_INET; 5144 tpi_mp = mi_tpi_extconn_ind(NULL, 5145 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5146 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5147 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5148 } else { 5149 tpi_mp = mi_tpi_conn_ind(NULL, 5150 (char *)&sin, sizeof (sin_t), 5151 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5152 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5153 } 5154 5155 if (tpi_mp == NULL) { 5156 return (ENOMEM); 5157 } 5158 5159 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5160 connp->conn_send = ip_output; 5161 connp->conn_recv = tcp_input; 5162 connp->conn_fully_bound = B_FALSE; 5163 5164 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5165 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5166 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5167 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5168 5169 if (tcp_trace) { 5170 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 5171 } 5172 5173 /* Inherit information from the "parent" */ 5174 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5175 tcp->tcp_family = ltcp->tcp_family; 5176 tcp->tcp_wq = ltcp->tcp_wq; 5177 tcp->tcp_rq = ltcp->tcp_rq; 5178 tcp->tcp_mss = tcp_mss_def_ipv4; 5179 tcp->tcp_detached = B_TRUE; 5180 if ((err = tcp_init_values(tcp)) != 0) { 5181 freemsg(tpi_mp); 5182 return (err); 5183 } 5184 5185 /* 5186 * Let's make sure that eager tcp template has enough space to 5187 * copy IPv4 listener's tcp template. Since the conn_t structure is 5188 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5189 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5190 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5191 * extension headers or with ip6i_t struct). Note that bcopy() below 5192 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5193 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5194 */ 5195 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5196 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5197 5198 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5199 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5200 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5201 tcp->tcp_ttl = ltcp->tcp_ttl; 5202 tcp->tcp_tos = ltcp->tcp_tos; 5203 5204 /* Copy the IP+TCP header template from listener to eager */ 5205 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5206 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5207 tcp->tcp_ip6h = NULL; 5208 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5209 tcp->tcp_ip_hdr_len); 5210 5211 /* Initialize the IP addresses and Ports */ 5212 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5213 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5214 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5215 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5216 5217 /* Source routing option copyover (reverse it) */ 5218 if (tcp_rev_src_routes) 5219 tcp_opt_reverse(tcp, ipha); 5220 5221 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5222 ASSERT(!tcp->tcp_tconnind_started); 5223 5224 /* 5225 * If the SYN contains a credential, it's a loopback packet; attach 5226 * the credential to the TPI message. 5227 */ 5228 if ((cr = DB_CRED(idmp)) != NULL) { 5229 mblk_setcred(tpi_mp, cr); 5230 DB_CPID(tpi_mp) = DB_CPID(idmp); 5231 } 5232 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5233 5234 /* Inherit the listener's SSL protection state */ 5235 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5236 kssl_hold_ent(tcp->tcp_kssl_ent); 5237 tcp->tcp_kssl_pending = B_TRUE; 5238 } 5239 5240 return (0); 5241 } 5242 5243 /* 5244 * sets up conn for ipsec. 5245 * if the first mblk is M_CTL it is consumed and mpp is updated. 5246 * in case of error mpp is freed. 5247 */ 5248 conn_t * 5249 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5250 { 5251 conn_t *connp = tcp->tcp_connp; 5252 conn_t *econnp; 5253 squeue_t *new_sqp; 5254 mblk_t *first_mp = *mpp; 5255 mblk_t *mp = *mpp; 5256 boolean_t mctl_present = B_FALSE; 5257 uint_t ipvers; 5258 5259 econnp = tcp_get_conn(sqp); 5260 if (econnp == NULL) { 5261 freemsg(first_mp); 5262 return (NULL); 5263 } 5264 if (DB_TYPE(mp) == M_CTL) { 5265 if (mp->b_cont == NULL || 5266 mp->b_cont->b_datap->db_type != M_DATA) { 5267 freemsg(first_mp); 5268 return (NULL); 5269 } 5270 mp = mp->b_cont; 5271 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5272 freemsg(first_mp); 5273 return (NULL); 5274 } 5275 5276 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5277 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5278 mctl_present = B_TRUE; 5279 } else { 5280 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5281 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5282 } 5283 5284 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5285 DB_CKSUMSTART(mp) = 0; 5286 5287 ASSERT(OK_32PTR(mp->b_rptr)); 5288 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5289 if (ipvers == IPV4_VERSION) { 5290 uint16_t *up; 5291 uint32_t ports; 5292 ipha_t *ipha; 5293 5294 ipha = (ipha_t *)mp->b_rptr; 5295 up = (uint16_t *)((uchar_t *)ipha + 5296 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5297 ports = *(uint32_t *)up; 5298 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5299 ipha->ipha_dst, ipha->ipha_src, ports); 5300 } else { 5301 uint16_t *up; 5302 uint32_t ports; 5303 uint16_t ip_hdr_len; 5304 uint8_t *nexthdrp; 5305 ip6_t *ip6h; 5306 tcph_t *tcph; 5307 5308 ip6h = (ip6_t *)mp->b_rptr; 5309 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5310 ip_hdr_len = IPV6_HDR_LEN; 5311 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5312 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5313 CONN_DEC_REF(econnp); 5314 freemsg(first_mp); 5315 return (NULL); 5316 } 5317 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5318 up = (uint16_t *)tcph->th_lport; 5319 ports = *(uint32_t *)up; 5320 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5321 ip6h->ip6_dst, ip6h->ip6_src, ports); 5322 } 5323 5324 /* 5325 * The caller already ensured that there is a sqp present. 5326 */ 5327 econnp->conn_sqp = new_sqp; 5328 5329 if (connp->conn_policy != NULL) { 5330 ipsec_in_t *ii; 5331 ii = (ipsec_in_t *)(first_mp->b_rptr); 5332 ASSERT(ii->ipsec_in_policy == NULL); 5333 IPPH_REFHOLD(connp->conn_policy); 5334 ii->ipsec_in_policy = connp->conn_policy; 5335 5336 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5337 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5338 CONN_DEC_REF(econnp); 5339 freemsg(first_mp); 5340 return (NULL); 5341 } 5342 } 5343 5344 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5345 CONN_DEC_REF(econnp); 5346 freemsg(first_mp); 5347 return (NULL); 5348 } 5349 5350 /* 5351 * If we know we have some policy, pass the "IPSEC" 5352 * options size TCP uses this adjust the MSS. 5353 */ 5354 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5355 if (mctl_present) { 5356 freeb(first_mp); 5357 *mpp = mp; 5358 } 5359 5360 return (econnp); 5361 } 5362 5363 /* 5364 * tcp_get_conn/tcp_free_conn 5365 * 5366 * tcp_get_conn is used to get a clean tcp connection structure. 5367 * It tries to reuse the connections put on the freelist by the 5368 * time_wait_collector failing which it goes to kmem_cache. This 5369 * way has two benefits compared to just allocating from and 5370 * freeing to kmem_cache. 5371 * 1) The time_wait_collector can free (which includes the cleanup) 5372 * outside the squeue. So when the interrupt comes, we have a clean 5373 * connection sitting in the freelist. Obviously, this buys us 5374 * performance. 5375 * 5376 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5377 * has multiple disadvantages - tying up the squeue during alloc, and the 5378 * fact that IPSec policy initialization has to happen here which 5379 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5380 * But allocating the conn/tcp in IP land is also not the best since 5381 * we can't check the 'q' and 'q0' which are protected by squeue and 5382 * blindly allocate memory which might have to be freed here if we are 5383 * not allowed to accept the connection. By using the freelist and 5384 * putting the conn/tcp back in freelist, we don't pay a penalty for 5385 * allocating memory without checking 'q/q0' and freeing it if we can't 5386 * accept the connection. 5387 * 5388 * Care should be taken to put the conn back in the same squeue's freelist 5389 * from which it was allocated. Best results are obtained if conn is 5390 * allocated from listener's squeue and freed to the same. Time wait 5391 * collector will free up the freelist is the connection ends up sitting 5392 * there for too long. 5393 */ 5394 void * 5395 tcp_get_conn(void *arg) 5396 { 5397 tcp_t *tcp = NULL; 5398 conn_t *connp = NULL; 5399 squeue_t *sqp = (squeue_t *)arg; 5400 tcp_squeue_priv_t *tcp_time_wait; 5401 5402 tcp_time_wait = 5403 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5404 5405 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5406 tcp = tcp_time_wait->tcp_free_list; 5407 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5408 if (tcp != NULL) { 5409 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5410 tcp_time_wait->tcp_free_list_cnt--; 5411 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5412 tcp->tcp_time_wait_next = NULL; 5413 connp = tcp->tcp_connp; 5414 connp->conn_flags |= IPCL_REUSED; 5415 return ((void *)connp); 5416 } 5417 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5418 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL) 5419 return (NULL); 5420 return ((void *)connp); 5421 } 5422 5423 /* 5424 * Update the cached label for the given tcp_t. This should be called once per 5425 * connection, and before any packets are sent or tcp_process_options is 5426 * invoked. Returns B_FALSE if the correct label could not be constructed. 5427 */ 5428 static boolean_t 5429 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5430 { 5431 conn_t *connp = tcp->tcp_connp; 5432 5433 if (tcp->tcp_ipversion == IPV4_VERSION) { 5434 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5435 int added; 5436 5437 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5438 connp->conn_mac_exempt) != 0) 5439 return (B_FALSE); 5440 5441 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5442 if (added == -1) 5443 return (B_FALSE); 5444 tcp->tcp_hdr_len += added; 5445 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5446 tcp->tcp_ip_hdr_len += added; 5447 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5448 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5449 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5450 tcp->tcp_hdr_len); 5451 if (added == -1) 5452 return (B_FALSE); 5453 tcp->tcp_hdr_len += added; 5454 tcp->tcp_tcph = (tcph_t *) 5455 ((uchar_t *)tcp->tcp_tcph + added); 5456 tcp->tcp_ip_hdr_len += added; 5457 } 5458 } else { 5459 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5460 5461 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5462 connp->conn_mac_exempt) != 0) 5463 return (B_FALSE); 5464 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5465 &tcp->tcp_label_len, optbuf) != 0) 5466 return (B_FALSE); 5467 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5468 return (B_FALSE); 5469 } 5470 5471 connp->conn_ulp_labeled = 1; 5472 5473 return (B_TRUE); 5474 } 5475 5476 /* BEGIN CSTYLED */ 5477 /* 5478 * 5479 * The sockfs ACCEPT path: 5480 * ======================= 5481 * 5482 * The eager is now established in its own perimeter as soon as SYN is 5483 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5484 * completes the accept processing on the acceptor STREAM. The sending 5485 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5486 * listener but a TLI/XTI listener completes the accept processing 5487 * on the listener perimeter. 5488 * 5489 * Common control flow for 3 way handshake: 5490 * ---------------------------------------- 5491 * 5492 * incoming SYN (listener perimeter) -> tcp_rput_data() 5493 * -> tcp_conn_request() 5494 * 5495 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5496 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5497 * 5498 * Sockfs ACCEPT Path: 5499 * ------------------- 5500 * 5501 * open acceptor stream (ip_tcpopen allocates tcp_wput_accept() 5502 * as STREAM entry point) 5503 * 5504 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5505 * 5506 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5507 * association (we are not behind eager's squeue but sockfs is protecting us 5508 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5509 * is changed to point at tcp_wput(). 5510 * 5511 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5512 * listener (done on listener's perimeter). 5513 * 5514 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5515 * accept. 5516 * 5517 * TLI/XTI client ACCEPT path: 5518 * --------------------------- 5519 * 5520 * soaccept() sends T_CONN_RES on the listener STREAM. 5521 * 5522 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5523 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5524 * 5525 * Locks: 5526 * ====== 5527 * 5528 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5529 * and listeners->tcp_eager_next_q. 5530 * 5531 * Referencing: 5532 * ============ 5533 * 5534 * 1) We start out in tcp_conn_request by eager placing a ref on 5535 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5536 * 5537 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5538 * doing so we place a ref on the eager. This ref is finally dropped at the 5539 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5540 * reference is dropped by the squeue framework. 5541 * 5542 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5543 * 5544 * The reference must be released by the same entity that added the reference 5545 * In the above scheme, the eager is the entity that adds and releases the 5546 * references. Note that tcp_accept_finish executes in the squeue of the eager 5547 * (albeit after it is attached to the acceptor stream). Though 1. executes 5548 * in the listener's squeue, the eager is nascent at this point and the 5549 * reference can be considered to have been added on behalf of the eager. 5550 * 5551 * Eager getting a Reset or listener closing: 5552 * ========================================== 5553 * 5554 * Once the listener and eager are linked, the listener never does the unlink. 5555 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5556 * a message on all eager perimeter. The eager then does the unlink, clears 5557 * any pointers to the listener's queue and drops the reference to the 5558 * listener. The listener waits in tcp_close outside the squeue until its 5559 * refcount has dropped to 1. This ensures that the listener has waited for 5560 * all eagers to clear their association with the listener. 5561 * 5562 * Similarly, if eager decides to go away, it can unlink itself and close. 5563 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5564 * the reference to eager is still valid because of the extra ref we put 5565 * in tcp_send_conn_ind. 5566 * 5567 * Listener can always locate the eager under the protection 5568 * of the listener->tcp_eager_lock, and then do a refhold 5569 * on the eager during the accept processing. 5570 * 5571 * The acceptor stream accesses the eager in the accept processing 5572 * based on the ref placed on eager before sending T_conn_ind. 5573 * The only entity that can negate this refhold is a listener close 5574 * which is mutually exclusive with an active acceptor stream. 5575 * 5576 * Eager's reference on the listener 5577 * =================================== 5578 * 5579 * If the accept happens (even on a closed eager) the eager drops its 5580 * reference on the listener at the start of tcp_accept_finish. If the 5581 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5582 * the reference is dropped in tcp_closei_local. If the listener closes, 5583 * the reference is dropped in tcp_eager_kill. In all cases the reference 5584 * is dropped while executing in the eager's context (squeue). 5585 */ 5586 /* END CSTYLED */ 5587 5588 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5589 5590 /* 5591 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5592 * tcp_rput_data will not see any SYN packets. 5593 */ 5594 /* ARGSUSED */ 5595 void 5596 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5597 { 5598 tcph_t *tcph; 5599 uint32_t seg_seq; 5600 tcp_t *eager; 5601 uint_t ipvers; 5602 ipha_t *ipha; 5603 ip6_t *ip6h; 5604 int err; 5605 conn_t *econnp = NULL; 5606 squeue_t *new_sqp; 5607 mblk_t *mp1; 5608 uint_t ip_hdr_len; 5609 conn_t *connp = (conn_t *)arg; 5610 tcp_t *tcp = connp->conn_tcp; 5611 ire_t *ire; 5612 cred_t *credp; 5613 5614 if (tcp->tcp_state != TCPS_LISTEN) 5615 goto error2; 5616 5617 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5618 5619 mutex_enter(&tcp->tcp_eager_lock); 5620 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5621 mutex_exit(&tcp->tcp_eager_lock); 5622 TCP_STAT(tcp_listendrop); 5623 BUMP_MIB(&tcp_mib, tcpListenDrop); 5624 if (tcp->tcp_debug) { 5625 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5626 "tcp_conn_request: listen backlog (max=%d) " 5627 "overflow (%d pending) on %s", 5628 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5629 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5630 } 5631 goto error2; 5632 } 5633 5634 if (tcp->tcp_conn_req_cnt_q0 >= 5635 tcp->tcp_conn_req_max + tcp_conn_req_max_q0) { 5636 /* 5637 * Q0 is full. Drop a pending half-open req from the queue 5638 * to make room for the new SYN req. Also mark the time we 5639 * drop a SYN. 5640 * 5641 * A more aggressive defense against SYN attack will 5642 * be to set the "tcp_syn_defense" flag now. 5643 */ 5644 TCP_STAT(tcp_listendropq0); 5645 tcp->tcp_last_rcv_lbolt = lbolt64; 5646 if (!tcp_drop_q0(tcp)) { 5647 mutex_exit(&tcp->tcp_eager_lock); 5648 BUMP_MIB(&tcp_mib, tcpListenDropQ0); 5649 if (tcp->tcp_debug) { 5650 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5651 "tcp_conn_request: listen half-open queue " 5652 "(max=%d) full (%d pending) on %s", 5653 tcp_conn_req_max_q0, 5654 tcp->tcp_conn_req_cnt_q0, 5655 tcp_display(tcp, NULL, 5656 DISP_PORT_ONLY)); 5657 } 5658 goto error2; 5659 } 5660 } 5661 mutex_exit(&tcp->tcp_eager_lock); 5662 5663 /* 5664 * IP adds STRUIO_EAGER and ensures that the received packet is 5665 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5666 * link local address. If IPSec is enabled, db_struioflag has 5667 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5668 * otherwise an error case if neither of them is set. 5669 */ 5670 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5671 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5672 DB_CKSUMSTART(mp) = 0; 5673 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5674 econnp = (conn_t *)tcp_get_conn(arg2); 5675 if (econnp == NULL) 5676 goto error2; 5677 econnp->conn_sqp = new_sqp; 5678 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5679 /* 5680 * mp is updated in tcp_get_ipsec_conn(). 5681 */ 5682 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5683 if (econnp == NULL) { 5684 /* 5685 * mp freed by tcp_get_ipsec_conn. 5686 */ 5687 return; 5688 } 5689 } else { 5690 goto error2; 5691 } 5692 5693 ASSERT(DB_TYPE(mp) == M_DATA); 5694 5695 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5696 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5697 ASSERT(OK_32PTR(mp->b_rptr)); 5698 if (ipvers == IPV4_VERSION) { 5699 ipha = (ipha_t *)mp->b_rptr; 5700 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5701 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5702 } else { 5703 ip6h = (ip6_t *)mp->b_rptr; 5704 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5705 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5706 } 5707 5708 if (tcp->tcp_family == AF_INET) { 5709 ASSERT(ipvers == IPV4_VERSION); 5710 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5711 } else { 5712 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5713 } 5714 5715 if (err) 5716 goto error3; 5717 5718 eager = econnp->conn_tcp; 5719 5720 /* Inherit various TCP parameters from the listener */ 5721 eager->tcp_naglim = tcp->tcp_naglim; 5722 eager->tcp_first_timer_threshold = 5723 tcp->tcp_first_timer_threshold; 5724 eager->tcp_second_timer_threshold = 5725 tcp->tcp_second_timer_threshold; 5726 5727 eager->tcp_first_ctimer_threshold = 5728 tcp->tcp_first_ctimer_threshold; 5729 eager->tcp_second_ctimer_threshold = 5730 tcp->tcp_second_ctimer_threshold; 5731 5732 /* 5733 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5734 * If it does not, the eager's receive window will be set to the 5735 * listener's receive window later in this function. 5736 */ 5737 eager->tcp_rwnd = 0; 5738 5739 /* 5740 * Inherit listener's tcp_init_cwnd. Need to do this before 5741 * calling tcp_process_options() where tcp_mss_set() is called 5742 * to set the initial cwnd. 5743 */ 5744 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5745 5746 /* 5747 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5748 * zone id before the accept is completed in tcp_wput_accept(). 5749 */ 5750 econnp->conn_zoneid = connp->conn_zoneid; 5751 econnp->conn_allzones = connp->conn_allzones; 5752 5753 /* Copy nexthop information from listener to eager */ 5754 if (connp->conn_nexthop_set) { 5755 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5756 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5757 } 5758 5759 /* 5760 * TSOL: tsol_input_proc() needs the eager's cred before the 5761 * eager is accepted 5762 */ 5763 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5764 crhold(credp); 5765 5766 /* 5767 * If the caller has the process-wide flag set, then default to MAC 5768 * exempt mode. This allows read-down to unlabeled hosts. 5769 */ 5770 if (getpflags(NET_MAC_AWARE, credp) != 0) 5771 econnp->conn_mac_exempt = B_TRUE; 5772 5773 if (is_system_labeled()) { 5774 cred_t *cr; 5775 5776 if (connp->conn_mlp_type != mlptSingle) { 5777 cr = econnp->conn_peercred = DB_CRED(mp); 5778 if (cr != NULL) 5779 crhold(cr); 5780 else 5781 cr = econnp->conn_cred; 5782 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5783 econnp, cred_t *, cr) 5784 } else { 5785 cr = econnp->conn_cred; 5786 DTRACE_PROBE2(syn_accept, conn_t *, 5787 econnp, cred_t *, cr) 5788 } 5789 5790 if (!tcp_update_label(eager, cr)) { 5791 DTRACE_PROBE3( 5792 tx__ip__log__error__connrequest__tcp, 5793 char *, "eager connp(1) label on SYN mp(2) failed", 5794 conn_t *, econnp, mblk_t *, mp); 5795 goto error3; 5796 } 5797 } 5798 5799 eager->tcp_hard_binding = B_TRUE; 5800 5801 tcp_bind_hash_insert(&tcp_bind_fanout[ 5802 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5803 5804 CL_INET_CONNECT(eager); 5805 5806 /* 5807 * No need to check for multicast destination since ip will only pass 5808 * up multicasts to those that have expressed interest 5809 * TODO: what about rejecting broadcasts? 5810 * Also check that source is not a multicast or broadcast address. 5811 */ 5812 eager->tcp_state = TCPS_SYN_RCVD; 5813 5814 5815 /* 5816 * There should be no ire in the mp as we are being called after 5817 * receiving the SYN. 5818 */ 5819 ASSERT(tcp_ire_mp(mp) == NULL); 5820 5821 /* 5822 * Adapt our mss, ttl, ... according to information provided in IRE. 5823 */ 5824 5825 if (tcp_adapt_ire(eager, NULL) == 0) { 5826 /* Undo the bind_hash_insert */ 5827 tcp_bind_hash_remove(eager); 5828 goto error3; 5829 } 5830 5831 /* Process all TCP options. */ 5832 tcp_process_options(eager, tcph); 5833 5834 /* Is the other end ECN capable? */ 5835 if (tcp_ecn_permitted >= 1 && 5836 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5837 eager->tcp_ecn_ok = B_TRUE; 5838 } 5839 5840 /* 5841 * listener->tcp_rq->q_hiwat should be the default window size or a 5842 * window size changed via SO_RCVBUF option. First round up the 5843 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5844 * scale option value if needed. Call tcp_rwnd_set() to finish the 5845 * setting. 5846 * 5847 * Note if there is a rpipe metric associated with the remote host, 5848 * we should not inherit receive window size from listener. 5849 */ 5850 eager->tcp_rwnd = MSS_ROUNDUP( 5851 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5852 eager->tcp_rwnd), eager->tcp_mss); 5853 if (eager->tcp_snd_ws_ok) 5854 tcp_set_ws_value(eager); 5855 /* 5856 * Note that this is the only place tcp_rwnd_set() is called for 5857 * accepting a connection. We need to call it here instead of 5858 * after the 3-way handshake because we need to tell the other 5859 * side our rwnd in the SYN-ACK segment. 5860 */ 5861 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5862 5863 /* 5864 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5865 * via soaccept()->soinheritoptions() which essentially applies 5866 * all the listener options to the new STREAM. The options that we 5867 * need to take care of are: 5868 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5869 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5870 * SO_SNDBUF, SO_RCVBUF. 5871 * 5872 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5873 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5874 * tcp_maxpsz_set() gets called later from 5875 * tcp_accept_finish(), the option takes effect. 5876 * 5877 */ 5878 /* Set the TCP options */ 5879 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5880 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5881 eager->tcp_oobinline = tcp->tcp_oobinline; 5882 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5883 eager->tcp_broadcast = tcp->tcp_broadcast; 5884 eager->tcp_useloopback = tcp->tcp_useloopback; 5885 eager->tcp_dontroute = tcp->tcp_dontroute; 5886 eager->tcp_linger = tcp->tcp_linger; 5887 eager->tcp_lingertime = tcp->tcp_lingertime; 5888 if (tcp->tcp_ka_enabled) 5889 eager->tcp_ka_enabled = 1; 5890 5891 /* Set the IP options */ 5892 econnp->conn_broadcast = connp->conn_broadcast; 5893 econnp->conn_loopback = connp->conn_loopback; 5894 econnp->conn_dontroute = connp->conn_dontroute; 5895 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5896 5897 /* Put a ref on the listener for the eager. */ 5898 CONN_INC_REF(connp); 5899 mutex_enter(&tcp->tcp_eager_lock); 5900 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5901 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5902 tcp->tcp_eager_next_q0 = eager; 5903 eager->tcp_eager_prev_q0 = tcp; 5904 5905 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5906 eager->tcp_listener = tcp; 5907 eager->tcp_saved_listener = tcp; 5908 5909 /* 5910 * Tag this detached tcp vector for later retrieval 5911 * by our listener client in tcp_accept(). 5912 */ 5913 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5914 tcp->tcp_conn_req_cnt_q0++; 5915 if (++tcp->tcp_conn_req_seqnum == -1) { 5916 /* 5917 * -1 is "special" and defined in TPI as something 5918 * that should never be used in T_CONN_IND 5919 */ 5920 ++tcp->tcp_conn_req_seqnum; 5921 } 5922 mutex_exit(&tcp->tcp_eager_lock); 5923 5924 if (tcp->tcp_syn_defense) { 5925 /* Don't drop the SYN that comes from a good IP source */ 5926 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5927 if (addr_cache != NULL && eager->tcp_remote == 5928 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5929 eager->tcp_dontdrop = B_TRUE; 5930 } 5931 } 5932 5933 /* 5934 * We need to insert the eager in its own perimeter but as soon 5935 * as we do that, we expose the eager to the classifier and 5936 * should not touch any field outside the eager's perimeter. 5937 * So do all the work necessary before inserting the eager 5938 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5939 * will succeed but undo everything if it fails. 5940 */ 5941 seg_seq = ABE32_TO_U32(tcph->th_seq); 5942 eager->tcp_irs = seg_seq; 5943 eager->tcp_rack = seg_seq; 5944 eager->tcp_rnxt = seg_seq + 1; 5945 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5946 BUMP_MIB(&tcp_mib, tcpPassiveOpens); 5947 eager->tcp_state = TCPS_SYN_RCVD; 5948 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5949 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5950 if (mp1 == NULL) 5951 goto error1; 5952 DB_CPID(mp1) = tcp->tcp_cpid; 5953 5954 /* 5955 * We need to start the rto timer. In normal case, we start 5956 * the timer after sending the packet on the wire (or at 5957 * least believing that packet was sent by waiting for 5958 * CALL_IP_WPUT() to return). Since this is the first packet 5959 * being sent on the wire for the eager, our initial tcp_rto 5960 * is at least tcp_rexmit_interval_min which is a fairly 5961 * large value to allow the algorithm to adjust slowly to large 5962 * fluctuations of RTT during first few transmissions. 5963 * 5964 * Starting the timer first and then sending the packet in this 5965 * case shouldn't make much difference since tcp_rexmit_interval_min 5966 * is of the order of several 100ms and starting the timer 5967 * first and then sending the packet will result in difference 5968 * of few micro seconds. 5969 * 5970 * Without this optimization, we are forced to hold the fanout 5971 * lock across the ipcl_bind_insert() and sending the packet 5972 * so that we don't race against an incoming packet (maybe RST) 5973 * for this eager. 5974 */ 5975 5976 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5977 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5978 5979 5980 /* 5981 * Insert the eager in its own perimeter now. We are ready to deal 5982 * with any packets on eager. 5983 */ 5984 if (eager->tcp_ipversion == IPV4_VERSION) { 5985 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5986 goto error; 5987 } 5988 } else { 5989 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5990 goto error; 5991 } 5992 } 5993 5994 /* mark conn as fully-bound */ 5995 econnp->conn_fully_bound = B_TRUE; 5996 5997 /* Send the SYN-ACK */ 5998 tcp_send_data(eager, eager->tcp_wq, mp1); 5999 freemsg(mp); 6000 6001 return; 6002 error: 6003 (void) TCP_TIMER_CANCEL(eager, eager->tcp_timer_tid); 6004 freemsg(mp1); 6005 error1: 6006 /* Undo what we did above */ 6007 mutex_enter(&tcp->tcp_eager_lock); 6008 tcp_eager_unlink(eager); 6009 mutex_exit(&tcp->tcp_eager_lock); 6010 /* Drop eager's reference on the listener */ 6011 CONN_DEC_REF(connp); 6012 6013 /* 6014 * Delete the cached ire in conn_ire_cache and also mark 6015 * the conn as CONDEMNED 6016 */ 6017 mutex_enter(&econnp->conn_lock); 6018 econnp->conn_state_flags |= CONN_CONDEMNED; 6019 ire = econnp->conn_ire_cache; 6020 econnp->conn_ire_cache = NULL; 6021 mutex_exit(&econnp->conn_lock); 6022 if (ire != NULL) 6023 IRE_REFRELE_NOTR(ire); 6024 6025 /* 6026 * tcp_accept_comm inserts the eager to the bind_hash 6027 * we need to remove it from the hash if ipcl_conn_insert 6028 * fails. 6029 */ 6030 tcp_bind_hash_remove(eager); 6031 /* Drop the eager ref placed in tcp_open_detached */ 6032 CONN_DEC_REF(econnp); 6033 6034 /* 6035 * If a connection already exists, send the mp to that connections so 6036 * that it can be appropriately dealt with. 6037 */ 6038 if ((econnp = ipcl_classify(mp, connp->conn_zoneid)) != NULL) { 6039 if (!IPCL_IS_CONNECTED(econnp)) { 6040 /* 6041 * Something bad happened. ipcl_conn_insert() 6042 * failed because a connection already existed 6043 * in connected hash but we can't find it 6044 * anymore (someone blew it away). Just 6045 * free this message and hopefully remote 6046 * will retransmit at which time the SYN can be 6047 * treated as a new connection or dealth with 6048 * a TH_RST if a connection already exists. 6049 */ 6050 CONN_DEC_REF(econnp); 6051 freemsg(mp); 6052 } else { 6053 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6054 econnp, SQTAG_TCP_CONN_REQ); 6055 } 6056 } else { 6057 /* Nobody wants this packet */ 6058 freemsg(mp); 6059 } 6060 return; 6061 error2: 6062 freemsg(mp); 6063 return; 6064 error3: 6065 CONN_DEC_REF(econnp); 6066 freemsg(mp); 6067 } 6068 6069 /* 6070 * In an ideal case of vertical partition in NUMA architecture, its 6071 * beneficial to have the listener and all the incoming connections 6072 * tied to the same squeue. The other constraint is that incoming 6073 * connections should be tied to the squeue attached to interrupted 6074 * CPU for obvious locality reason so this leaves the listener to 6075 * be tied to the same squeue. Our only problem is that when listener 6076 * is binding, the CPU that will get interrupted by the NIC whose 6077 * IP address the listener is binding to is not even known. So 6078 * the code below allows us to change that binding at the time the 6079 * CPU is interrupted by virtue of incoming connection's squeue. 6080 * 6081 * This is usefull only in case of a listener bound to a specific IP 6082 * address. For other kind of listeners, they get bound the 6083 * very first time and there is no attempt to rebind them. 6084 */ 6085 void 6086 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6087 { 6088 conn_t *connp = (conn_t *)arg; 6089 squeue_t *sqp = (squeue_t *)arg2; 6090 squeue_t *new_sqp; 6091 uint32_t conn_flags; 6092 6093 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6094 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6095 } else { 6096 goto done; 6097 } 6098 6099 if (connp->conn_fanout == NULL) 6100 goto done; 6101 6102 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6103 mutex_enter(&connp->conn_fanout->connf_lock); 6104 mutex_enter(&connp->conn_lock); 6105 /* 6106 * No one from read or write side can access us now 6107 * except for already queued packets on this squeue. 6108 * But since we haven't changed the squeue yet, they 6109 * can't execute. If they are processed after we have 6110 * changed the squeue, they are sent back to the 6111 * correct squeue down below. 6112 * But a listner close can race with processing of 6113 * incoming SYN. If incoming SYN processing changes 6114 * the squeue then the listener close which is waiting 6115 * to enter the squeue would operate on the wrong 6116 * squeue. Hence we don't change the squeue here unless 6117 * the refcount is exactly the minimum refcount. The 6118 * minimum refcount of 4 is counted as - 1 each for 6119 * TCP and IP, 1 for being in the classifier hash, and 6120 * 1 for the mblk being processed. 6121 */ 6122 6123 if (connp->conn_ref != 4 || 6124 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6125 mutex_exit(&connp->conn_lock); 6126 mutex_exit(&connp->conn_fanout->connf_lock); 6127 goto done; 6128 } 6129 if (connp->conn_sqp != new_sqp) { 6130 while (connp->conn_sqp != new_sqp) 6131 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6132 } 6133 6134 do { 6135 conn_flags = connp->conn_flags; 6136 conn_flags |= IPCL_FULLY_BOUND; 6137 (void) cas32(&connp->conn_flags, connp->conn_flags, 6138 conn_flags); 6139 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6140 6141 mutex_exit(&connp->conn_fanout->connf_lock); 6142 mutex_exit(&connp->conn_lock); 6143 } 6144 6145 done: 6146 if (connp->conn_sqp != sqp) { 6147 CONN_INC_REF(connp); 6148 squeue_fill(connp->conn_sqp, mp, 6149 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6150 } else { 6151 tcp_conn_request(connp, mp, sqp); 6152 } 6153 } 6154 6155 /* 6156 * Successful connect request processing begins when our client passes 6157 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6158 * our T_OK_ACK reply message upstream. The control flow looks like this: 6159 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6160 * upstream <- tcp_rput() <- IP 6161 * After various error checks are completed, tcp_connect() lays 6162 * the target address and port into the composite header template, 6163 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6164 * request followed by an IRE request, and passes the three mblk message 6165 * down to IP looking like this: 6166 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6167 * Processing continues in tcp_rput() when we receive the following message: 6168 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6169 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6170 * to fire off the connection request, and then passes the T_OK_ACK mblk 6171 * upstream that we filled in below. There are, of course, numerous 6172 * error conditions along the way which truncate the processing described 6173 * above. 6174 */ 6175 static void 6176 tcp_connect(tcp_t *tcp, mblk_t *mp) 6177 { 6178 sin_t *sin; 6179 sin6_t *sin6; 6180 queue_t *q = tcp->tcp_wq; 6181 struct T_conn_req *tcr; 6182 ipaddr_t *dstaddrp; 6183 in_port_t dstport; 6184 uint_t srcid; 6185 6186 tcr = (struct T_conn_req *)mp->b_rptr; 6187 6188 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6189 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6190 tcp_err_ack(tcp, mp, TPROTO, 0); 6191 return; 6192 } 6193 6194 /* 6195 * Determine packet type based on type of address passed in 6196 * the request should contain an IPv4 or IPv6 address. 6197 * Make sure that address family matches the type of 6198 * family of the the address passed down 6199 */ 6200 switch (tcr->DEST_length) { 6201 default: 6202 tcp_err_ack(tcp, mp, TBADADDR, 0); 6203 return; 6204 6205 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6206 /* 6207 * XXX: The check for valid DEST_length was not there 6208 * in earlier releases and some buggy 6209 * TLI apps (e.g Sybase) got away with not feeding 6210 * in sin_zero part of address. 6211 * We allow that bug to keep those buggy apps humming. 6212 * Test suites require the check on DEST_length. 6213 * We construct a new mblk with valid DEST_length 6214 * free the original so the rest of the code does 6215 * not have to keep track of this special shorter 6216 * length address case. 6217 */ 6218 mblk_t *nmp; 6219 struct T_conn_req *ntcr; 6220 sin_t *nsin; 6221 6222 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6223 tcr->OPT_length, BPRI_HI); 6224 if (nmp == NULL) { 6225 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6226 return; 6227 } 6228 ntcr = (struct T_conn_req *)nmp->b_rptr; 6229 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6230 ntcr->PRIM_type = T_CONN_REQ; 6231 ntcr->DEST_length = sizeof (sin_t); 6232 ntcr->DEST_offset = sizeof (struct T_conn_req); 6233 6234 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6235 *nsin = sin_null; 6236 /* Get pointer to shorter address to copy from original mp */ 6237 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6238 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6239 if (sin == NULL || !OK_32PTR((char *)sin)) { 6240 freemsg(nmp); 6241 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6242 return; 6243 } 6244 nsin->sin_family = sin->sin_family; 6245 nsin->sin_port = sin->sin_port; 6246 nsin->sin_addr = sin->sin_addr; 6247 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6248 nmp->b_wptr = (uchar_t *)&nsin[1]; 6249 if (tcr->OPT_length != 0) { 6250 ntcr->OPT_length = tcr->OPT_length; 6251 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6252 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6253 (uchar_t *)ntcr + ntcr->OPT_offset, 6254 tcr->OPT_length); 6255 nmp->b_wptr += tcr->OPT_length; 6256 } 6257 freemsg(mp); /* original mp freed */ 6258 mp = nmp; /* re-initialize original variables */ 6259 tcr = ntcr; 6260 } 6261 /* FALLTHRU */ 6262 6263 case sizeof (sin_t): 6264 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6265 sizeof (sin_t)); 6266 if (sin == NULL || !OK_32PTR((char *)sin)) { 6267 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6268 return; 6269 } 6270 if (tcp->tcp_family != AF_INET || 6271 sin->sin_family != AF_INET) { 6272 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6273 return; 6274 } 6275 if (sin->sin_port == 0) { 6276 tcp_err_ack(tcp, mp, TBADADDR, 0); 6277 return; 6278 } 6279 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6280 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6281 return; 6282 } 6283 6284 break; 6285 6286 case sizeof (sin6_t): 6287 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6288 sizeof (sin6_t)); 6289 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6290 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6291 return; 6292 } 6293 if (tcp->tcp_family != AF_INET6 || 6294 sin6->sin6_family != AF_INET6) { 6295 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6296 return; 6297 } 6298 if (sin6->sin6_port == 0) { 6299 tcp_err_ack(tcp, mp, TBADADDR, 0); 6300 return; 6301 } 6302 break; 6303 } 6304 /* 6305 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6306 * should key on their sequence number and cut them loose. 6307 */ 6308 6309 /* 6310 * If options passed in, feed it for verification and handling 6311 */ 6312 if (tcr->OPT_length != 0) { 6313 mblk_t *ok_mp; 6314 mblk_t *discon_mp; 6315 mblk_t *conn_opts_mp; 6316 int t_error, sys_error, do_disconnect; 6317 6318 conn_opts_mp = NULL; 6319 6320 if (tcp_conprim_opt_process(tcp, mp, 6321 &do_disconnect, &t_error, &sys_error) < 0) { 6322 if (do_disconnect) { 6323 ASSERT(t_error == 0 && sys_error == 0); 6324 discon_mp = mi_tpi_discon_ind(NULL, 6325 ECONNREFUSED, 0); 6326 if (!discon_mp) { 6327 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6328 TSYSERR, ENOMEM); 6329 return; 6330 } 6331 ok_mp = mi_tpi_ok_ack_alloc(mp); 6332 if (!ok_mp) { 6333 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6334 TSYSERR, ENOMEM); 6335 return; 6336 } 6337 qreply(q, ok_mp); 6338 qreply(q, discon_mp); /* no flush! */ 6339 } else { 6340 ASSERT(t_error != 0); 6341 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6342 sys_error); 6343 } 6344 return; 6345 } 6346 /* 6347 * Success in setting options, the mp option buffer represented 6348 * by OPT_length/offset has been potentially modified and 6349 * contains results of option processing. We copy it in 6350 * another mp to save it for potentially influencing returning 6351 * it in T_CONN_CONN. 6352 */ 6353 if (tcr->OPT_length != 0) { /* there are resulting options */ 6354 conn_opts_mp = copyb(mp); 6355 if (!conn_opts_mp) { 6356 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6357 TSYSERR, ENOMEM); 6358 return; 6359 } 6360 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6361 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6362 /* 6363 * Note: 6364 * These resulting option negotiation can include any 6365 * end-to-end negotiation options but there no such 6366 * thing (yet?) in our TCP/IP. 6367 */ 6368 } 6369 } 6370 6371 /* 6372 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6373 * make sure that the template IP header in the tcp structure is an 6374 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6375 * need to this before we call tcp_bindi() so that the port lookup 6376 * code will look for ports in the correct port space (IPv4 and 6377 * IPv6 have separate port spaces). 6378 */ 6379 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6380 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6381 int err = 0; 6382 6383 err = tcp_header_init_ipv4(tcp); 6384 if (err != 0) { 6385 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6386 goto connect_failed; 6387 } 6388 if (tcp->tcp_lport != 0) 6389 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6390 } 6391 6392 switch (tcp->tcp_state) { 6393 case TCPS_IDLE: 6394 /* 6395 * We support quick connect, refer to comments in 6396 * tcp_connect_*() 6397 */ 6398 /* FALLTHRU */ 6399 case TCPS_BOUND: 6400 case TCPS_LISTEN: 6401 if (tcp->tcp_family == AF_INET6) { 6402 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6403 tcp_connect_ipv6(tcp, mp, 6404 &sin6->sin6_addr, 6405 sin6->sin6_port, sin6->sin6_flowinfo, 6406 sin6->__sin6_src_id, sin6->sin6_scope_id); 6407 return; 6408 } 6409 /* 6410 * Destination adress is mapped IPv6 address. 6411 * Source bound address should be unspecified or 6412 * IPv6 mapped address as well. 6413 */ 6414 if (!IN6_IS_ADDR_UNSPECIFIED( 6415 &tcp->tcp_bound_source_v6) && 6416 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6417 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6418 EADDRNOTAVAIL); 6419 break; 6420 } 6421 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6422 dstport = sin6->sin6_port; 6423 srcid = sin6->__sin6_src_id; 6424 } else { 6425 dstaddrp = &sin->sin_addr.s_addr; 6426 dstport = sin->sin_port; 6427 srcid = 0; 6428 } 6429 6430 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6431 return; 6432 default: 6433 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6434 break; 6435 } 6436 /* 6437 * Note: Code below is the "failure" case 6438 */ 6439 /* return error ack and blow away saved option results if any */ 6440 connect_failed: 6441 if (mp != NULL) 6442 putnext(tcp->tcp_rq, mp); 6443 else { 6444 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6445 TSYSERR, ENOMEM); 6446 } 6447 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6448 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6449 } 6450 6451 /* 6452 * Handle connect to IPv4 destinations, including connections for AF_INET6 6453 * sockets connecting to IPv4 mapped IPv6 destinations. 6454 */ 6455 static void 6456 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6457 uint_t srcid) 6458 { 6459 tcph_t *tcph; 6460 mblk_t *mp1; 6461 ipaddr_t dstaddr = *dstaddrp; 6462 int32_t oldstate; 6463 uint16_t lport; 6464 6465 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6466 6467 /* Check for attempt to connect to INADDR_ANY */ 6468 if (dstaddr == INADDR_ANY) { 6469 /* 6470 * SunOS 4.x and 4.3 BSD allow an application 6471 * to connect a TCP socket to INADDR_ANY. 6472 * When they do this, the kernel picks the 6473 * address of one interface and uses it 6474 * instead. The kernel usually ends up 6475 * picking the address of the loopback 6476 * interface. This is an undocumented feature. 6477 * However, we provide the same thing here 6478 * in order to have source and binary 6479 * compatibility with SunOS 4.x. 6480 * Update the T_CONN_REQ (sin/sin6) since it is used to 6481 * generate the T_CONN_CON. 6482 */ 6483 dstaddr = htonl(INADDR_LOOPBACK); 6484 *dstaddrp = dstaddr; 6485 } 6486 6487 /* Handle __sin6_src_id if socket not bound to an IP address */ 6488 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6489 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6490 tcp->tcp_connp->conn_zoneid); 6491 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6492 tcp->tcp_ipha->ipha_src); 6493 } 6494 6495 /* 6496 * Don't let an endpoint connect to itself. Note that 6497 * the test here does not catch the case where the 6498 * source IP addr was left unspecified by the user. In 6499 * this case, the source addr is set in tcp_adapt_ire() 6500 * using the reply to the T_BIND message that we send 6501 * down to IP here and the check is repeated in tcp_rput_other. 6502 */ 6503 if (dstaddr == tcp->tcp_ipha->ipha_src && 6504 dstport == tcp->tcp_lport) { 6505 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6506 goto failed; 6507 } 6508 6509 tcp->tcp_ipha->ipha_dst = dstaddr; 6510 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6511 6512 /* 6513 * Massage a source route if any putting the first hop 6514 * in iph_dst. Compute a starting value for the checksum which 6515 * takes into account that the original iph_dst should be 6516 * included in the checksum but that ip will include the 6517 * first hop in the source route in the tcp checksum. 6518 */ 6519 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha); 6520 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6521 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6522 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6523 if ((int)tcp->tcp_sum < 0) 6524 tcp->tcp_sum--; 6525 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6526 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6527 (tcp->tcp_sum >> 16)); 6528 tcph = tcp->tcp_tcph; 6529 *(uint16_t *)tcph->th_fport = dstport; 6530 tcp->tcp_fport = dstport; 6531 6532 oldstate = tcp->tcp_state; 6533 /* 6534 * At this point the remote destination address and remote port fields 6535 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6536 * have to see which state tcp was in so we can take apropriate action. 6537 */ 6538 if (oldstate == TCPS_IDLE) { 6539 /* 6540 * We support a quick connect capability here, allowing 6541 * clients to transition directly from IDLE to SYN_SENT 6542 * tcp_bindi will pick an unused port, insert the connection 6543 * in the bind hash and transition to BOUND state. 6544 */ 6545 lport = tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 6546 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6547 B_FALSE, B_FALSE); 6548 if (lport == 0) { 6549 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6550 goto failed; 6551 } 6552 } 6553 tcp->tcp_state = TCPS_SYN_SENT; 6554 6555 /* 6556 * TODO: allow data with connect requests 6557 * by unlinking M_DATA trailers here and 6558 * linking them in behind the T_OK_ACK mblk. 6559 * The tcp_rput() bind ack handler would then 6560 * feed them to tcp_wput_data() rather than call 6561 * tcp_timer(). 6562 */ 6563 mp = mi_tpi_ok_ack_alloc(mp); 6564 if (!mp) { 6565 tcp->tcp_state = oldstate; 6566 goto failed; 6567 } 6568 if (tcp->tcp_family == AF_INET) { 6569 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6570 sizeof (ipa_conn_t)); 6571 } else { 6572 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6573 sizeof (ipa6_conn_t)); 6574 } 6575 if (mp1) { 6576 /* Hang onto the T_OK_ACK for later. */ 6577 linkb(mp1, mp); 6578 mblk_setcred(mp1, tcp->tcp_cred); 6579 if (tcp->tcp_family == AF_INET) 6580 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6581 else { 6582 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6583 &tcp->tcp_sticky_ipp); 6584 } 6585 BUMP_MIB(&tcp_mib, tcpActiveOpens); 6586 tcp->tcp_active_open = 1; 6587 /* 6588 * If the bind cannot complete immediately 6589 * IP will arrange to call tcp_rput_other 6590 * when the bind completes. 6591 */ 6592 if (mp1 != NULL) 6593 tcp_rput_other(tcp, mp1); 6594 return; 6595 } 6596 /* Error case */ 6597 tcp->tcp_state = oldstate; 6598 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6599 6600 failed: 6601 /* return error ack and blow away saved option results if any */ 6602 if (mp != NULL) 6603 putnext(tcp->tcp_rq, mp); 6604 else { 6605 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6606 TSYSERR, ENOMEM); 6607 } 6608 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6609 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6610 6611 } 6612 6613 /* 6614 * Handle connect to IPv6 destinations. 6615 */ 6616 static void 6617 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6618 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6619 { 6620 tcph_t *tcph; 6621 mblk_t *mp1; 6622 ip6_rthdr_t *rth; 6623 int32_t oldstate; 6624 uint16_t lport; 6625 6626 ASSERT(tcp->tcp_family == AF_INET6); 6627 6628 /* 6629 * If we're here, it means that the destination address is a native 6630 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6631 * reason why it might not be IPv6 is if the socket was bound to an 6632 * IPv4-mapped IPv6 address. 6633 */ 6634 if (tcp->tcp_ipversion != IPV6_VERSION) { 6635 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6636 goto failed; 6637 } 6638 6639 /* 6640 * Interpret a zero destination to mean loopback. 6641 * Update the T_CONN_REQ (sin/sin6) since it is used to 6642 * generate the T_CONN_CON. 6643 */ 6644 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6645 *dstaddrp = ipv6_loopback; 6646 } 6647 6648 /* Handle __sin6_src_id if socket not bound to an IP address */ 6649 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6650 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6651 tcp->tcp_connp->conn_zoneid); 6652 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6653 } 6654 6655 /* 6656 * Take care of the scope_id now and add ip6i_t 6657 * if ip6i_t is not already allocated through TCP 6658 * sticky options. At this point tcp_ip6h does not 6659 * have dst info, thus use dstaddrp. 6660 */ 6661 if (scope_id != 0 && 6662 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6663 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6664 ip6i_t *ip6i; 6665 6666 ipp->ipp_ifindex = scope_id; 6667 ip6i = (ip6i_t *)tcp->tcp_iphc; 6668 6669 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6670 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6671 /* Already allocated */ 6672 ip6i->ip6i_flags |= IP6I_IFINDEX; 6673 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6674 ipp->ipp_fields |= IPPF_SCOPE_ID; 6675 } else { 6676 int reterr; 6677 6678 ipp->ipp_fields |= IPPF_SCOPE_ID; 6679 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6680 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6681 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6682 if (reterr != 0) 6683 goto failed; 6684 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6685 } 6686 } 6687 6688 /* 6689 * Don't let an endpoint connect to itself. Note that 6690 * the test here does not catch the case where the 6691 * source IP addr was left unspecified by the user. In 6692 * this case, the source addr is set in tcp_adapt_ire() 6693 * using the reply to the T_BIND message that we send 6694 * down to IP here and the check is repeated in tcp_rput_other. 6695 */ 6696 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6697 (dstport == tcp->tcp_lport)) { 6698 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6699 goto failed; 6700 } 6701 6702 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6703 tcp->tcp_remote_v6 = *dstaddrp; 6704 tcp->tcp_ip6h->ip6_vcf = 6705 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6706 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6707 6708 6709 /* 6710 * Massage a routing header (if present) putting the first hop 6711 * in ip6_dst. Compute a starting value for the checksum which 6712 * takes into account that the original ip6_dst should be 6713 * included in the checksum but that ip will include the 6714 * first hop in the source route in the tcp checksum. 6715 */ 6716 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6717 if (rth != NULL) { 6718 6719 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth); 6720 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6721 (tcp->tcp_sum >> 16)); 6722 } else { 6723 tcp->tcp_sum = 0; 6724 } 6725 6726 tcph = tcp->tcp_tcph; 6727 *(uint16_t *)tcph->th_fport = dstport; 6728 tcp->tcp_fport = dstport; 6729 6730 oldstate = tcp->tcp_state; 6731 /* 6732 * At this point the remote destination address and remote port fields 6733 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6734 * have to see which state tcp was in so we can take apropriate action. 6735 */ 6736 if (oldstate == TCPS_IDLE) { 6737 /* 6738 * We support a quick connect capability here, allowing 6739 * clients to transition directly from IDLE to SYN_SENT 6740 * tcp_bindi will pick an unused port, insert the connection 6741 * in the bind hash and transition to BOUND state. 6742 */ 6743 lport = tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 6744 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6745 B_FALSE, B_FALSE); 6746 if (lport == 0) { 6747 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6748 goto failed; 6749 } 6750 } 6751 tcp->tcp_state = TCPS_SYN_SENT; 6752 /* 6753 * TODO: allow data with connect requests 6754 * by unlinking M_DATA trailers here and 6755 * linking them in behind the T_OK_ACK mblk. 6756 * The tcp_rput() bind ack handler would then 6757 * feed them to tcp_wput_data() rather than call 6758 * tcp_timer(). 6759 */ 6760 mp = mi_tpi_ok_ack_alloc(mp); 6761 if (!mp) { 6762 tcp->tcp_state = oldstate; 6763 goto failed; 6764 } 6765 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6766 if (mp1) { 6767 /* Hang onto the T_OK_ACK for later. */ 6768 linkb(mp1, mp); 6769 mblk_setcred(mp1, tcp->tcp_cred); 6770 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6771 &tcp->tcp_sticky_ipp); 6772 BUMP_MIB(&tcp_mib, tcpActiveOpens); 6773 tcp->tcp_active_open = 1; 6774 /* ip_bind_v6() may return ACK or ERROR */ 6775 if (mp1 != NULL) 6776 tcp_rput_other(tcp, mp1); 6777 return; 6778 } 6779 /* Error case */ 6780 tcp->tcp_state = oldstate; 6781 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6782 6783 failed: 6784 /* return error ack and blow away saved option results if any */ 6785 if (mp != NULL) 6786 putnext(tcp->tcp_rq, mp); 6787 else { 6788 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6789 TSYSERR, ENOMEM); 6790 } 6791 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6792 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6793 } 6794 6795 /* 6796 * We need a stream q for detached closing tcp connections 6797 * to use. Our client hereby indicates that this q is the 6798 * one to use. 6799 */ 6800 static void 6801 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6802 { 6803 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6804 queue_t *q = tcp->tcp_wq; 6805 6806 mp->b_datap->db_type = M_IOCACK; 6807 iocp->ioc_count = 0; 6808 mutex_enter(&tcp_g_q_lock); 6809 if (tcp_g_q != NULL) { 6810 mutex_exit(&tcp_g_q_lock); 6811 iocp->ioc_error = EALREADY; 6812 } else { 6813 mblk_t *mp1; 6814 6815 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6816 if (mp1 == NULL) { 6817 mutex_exit(&tcp_g_q_lock); 6818 iocp->ioc_error = ENOMEM; 6819 } else { 6820 tcp_g_q = tcp->tcp_rq; 6821 mutex_exit(&tcp_g_q_lock); 6822 iocp->ioc_error = 0; 6823 iocp->ioc_rval = 0; 6824 /* 6825 * We are passing tcp_sticky_ipp as NULL 6826 * as it is not useful for tcp_default queue 6827 */ 6828 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6829 if (mp1 != NULL) 6830 tcp_rput_other(tcp, mp1); 6831 } 6832 } 6833 qreply(q, mp); 6834 } 6835 6836 /* 6837 * Our client hereby directs us to reject the connection request 6838 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6839 * of sending the appropriate RST, not an ICMP error. 6840 */ 6841 static void 6842 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6843 { 6844 tcp_t *ltcp = NULL; 6845 t_scalar_t seqnum; 6846 conn_t *connp; 6847 6848 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6849 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6850 tcp_err_ack(tcp, mp, TPROTO, 0); 6851 return; 6852 } 6853 6854 /* 6855 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6856 * when the stream is in BOUND state. Do not send a reset, 6857 * since the destination IP address is not valid, and it can 6858 * be the initialized value of all zeros (broadcast address). 6859 * 6860 * If TCP has sent down a bind request to IP and has not 6861 * received the reply, reject the request. Otherwise, TCP 6862 * will be confused. 6863 */ 6864 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6865 if (tcp->tcp_debug) { 6866 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6867 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6868 } 6869 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6870 return; 6871 } 6872 6873 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6874 6875 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6876 6877 /* 6878 * According to TPI, for non-listeners, ignore seqnum 6879 * and disconnect. 6880 * Following interpretation of -1 seqnum is historical 6881 * and implied TPI ? (TPI only states that for T_CONN_IND, 6882 * a valid seqnum should not be -1). 6883 * 6884 * -1 means disconnect everything 6885 * regardless even on a listener. 6886 */ 6887 6888 int old_state = tcp->tcp_state; 6889 6890 /* 6891 * The connection can't be on the tcp_time_wait_head list 6892 * since it is not detached. 6893 */ 6894 ASSERT(tcp->tcp_time_wait_next == NULL); 6895 ASSERT(tcp->tcp_time_wait_prev == NULL); 6896 ASSERT(tcp->tcp_time_wait_expire == 0); 6897 ltcp = NULL; 6898 /* 6899 * If it used to be a listener, check to make sure no one else 6900 * has taken the port before switching back to LISTEN state. 6901 */ 6902 if (tcp->tcp_ipversion == IPV4_VERSION) { 6903 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6904 tcp->tcp_ipha->ipha_src, 6905 tcp->tcp_connp->conn_zoneid); 6906 if (connp != NULL) 6907 ltcp = connp->conn_tcp; 6908 } else { 6909 /* Allow tcp_bound_if listeners? */ 6910 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6911 &tcp->tcp_ip6h->ip6_src, 0, 6912 tcp->tcp_connp->conn_zoneid); 6913 if (connp != NULL) 6914 ltcp = connp->conn_tcp; 6915 } 6916 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6917 tcp->tcp_state = TCPS_LISTEN; 6918 } else if (old_state > TCPS_BOUND) { 6919 tcp->tcp_conn_req_max = 0; 6920 tcp->tcp_state = TCPS_BOUND; 6921 } 6922 if (ltcp != NULL) 6923 CONN_DEC_REF(ltcp->tcp_connp); 6924 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6925 BUMP_MIB(&tcp_mib, tcpAttemptFails); 6926 } else if (old_state == TCPS_ESTABLISHED || 6927 old_state == TCPS_CLOSE_WAIT) { 6928 BUMP_MIB(&tcp_mib, tcpEstabResets); 6929 } 6930 6931 if (tcp->tcp_fused) 6932 tcp_unfuse(tcp); 6933 6934 mutex_enter(&tcp->tcp_eager_lock); 6935 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6936 (tcp->tcp_conn_req_cnt_q != 0)) { 6937 tcp_eager_cleanup(tcp, 0); 6938 } 6939 mutex_exit(&tcp->tcp_eager_lock); 6940 6941 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6942 tcp->tcp_rnxt, TH_RST | TH_ACK); 6943 6944 tcp_reinit(tcp); 6945 6946 if (old_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 return; 6954 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6955 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6956 return; 6957 } 6958 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6959 /* Send M_FLUSH according to TPI */ 6960 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6961 } 6962 mp = mi_tpi_ok_ack_alloc(mp); 6963 if (mp) 6964 putnext(tcp->tcp_rq, mp); 6965 } 6966 6967 /* 6968 * Diagnostic routine used to return a string associated with the tcp state. 6969 * Note that if the caller does not supply a buffer, it will use an internal 6970 * static string. This means that if multiple threads call this function at 6971 * the same time, output can be corrupted... Note also that this function 6972 * does not check the size of the supplied buffer. The caller has to make 6973 * sure that it is big enough. 6974 */ 6975 static char * 6976 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6977 { 6978 char buf1[30]; 6979 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6980 char *buf; 6981 char *cp; 6982 in6_addr_t local, remote; 6983 char local_addrbuf[INET6_ADDRSTRLEN]; 6984 char remote_addrbuf[INET6_ADDRSTRLEN]; 6985 6986 if (sup_buf != NULL) 6987 buf = sup_buf; 6988 else 6989 buf = priv_buf; 6990 6991 if (tcp == NULL) 6992 return ("NULL_TCP"); 6993 switch (tcp->tcp_state) { 6994 case TCPS_CLOSED: 6995 cp = "TCP_CLOSED"; 6996 break; 6997 case TCPS_IDLE: 6998 cp = "TCP_IDLE"; 6999 break; 7000 case TCPS_BOUND: 7001 cp = "TCP_BOUND"; 7002 break; 7003 case TCPS_LISTEN: 7004 cp = "TCP_LISTEN"; 7005 break; 7006 case TCPS_SYN_SENT: 7007 cp = "TCP_SYN_SENT"; 7008 break; 7009 case TCPS_SYN_RCVD: 7010 cp = "TCP_SYN_RCVD"; 7011 break; 7012 case TCPS_ESTABLISHED: 7013 cp = "TCP_ESTABLISHED"; 7014 break; 7015 case TCPS_CLOSE_WAIT: 7016 cp = "TCP_CLOSE_WAIT"; 7017 break; 7018 case TCPS_FIN_WAIT_1: 7019 cp = "TCP_FIN_WAIT_1"; 7020 break; 7021 case TCPS_CLOSING: 7022 cp = "TCP_CLOSING"; 7023 break; 7024 case TCPS_LAST_ACK: 7025 cp = "TCP_LAST_ACK"; 7026 break; 7027 case TCPS_FIN_WAIT_2: 7028 cp = "TCP_FIN_WAIT_2"; 7029 break; 7030 case TCPS_TIME_WAIT: 7031 cp = "TCP_TIME_WAIT"; 7032 break; 7033 default: 7034 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7035 cp = buf1; 7036 break; 7037 } 7038 switch (format) { 7039 case DISP_ADDR_AND_PORT: 7040 if (tcp->tcp_ipversion == IPV4_VERSION) { 7041 /* 7042 * Note that we use the remote address in the tcp_b 7043 * structure. This means that it will print out 7044 * the real destination address, not the next hop's 7045 * address if source routing is used. 7046 */ 7047 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7048 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7049 7050 } else { 7051 local = tcp->tcp_ip_src_v6; 7052 remote = tcp->tcp_remote_v6; 7053 } 7054 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7055 sizeof (local_addrbuf)); 7056 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7057 sizeof (remote_addrbuf)); 7058 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7059 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7060 ntohs(tcp->tcp_fport), cp); 7061 break; 7062 case DISP_PORT_ONLY: 7063 default: 7064 (void) mi_sprintf(buf, "[%u, %u] %s", 7065 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7066 break; 7067 } 7068 7069 return (buf); 7070 } 7071 7072 /* 7073 * Called via squeue to get on to eager's perimeter to send a 7074 * TH_RST. The listener wants the eager to disappear either 7075 * by means of tcp_eager_blowoff() or tcp_eager_cleanup() 7076 * being called. 7077 */ 7078 /* ARGSUSED */ 7079 void 7080 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7081 { 7082 conn_t *econnp = (conn_t *)arg; 7083 tcp_t *eager = econnp->conn_tcp; 7084 tcp_t *listener = eager->tcp_listener; 7085 7086 /* 7087 * We could be called because listener is closing. Since 7088 * the eager is using listener's queue's, its not safe. 7089 * Better use the default queue just to send the TH_RST 7090 * out. 7091 */ 7092 eager->tcp_rq = tcp_g_q; 7093 eager->tcp_wq = WR(tcp_g_q); 7094 7095 if (eager->tcp_state > TCPS_LISTEN) { 7096 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7097 eager, eager->tcp_snxt, 0, TH_RST); 7098 } 7099 7100 /* We are here because listener wants this eager gone */ 7101 if (listener != NULL) { 7102 mutex_enter(&listener->tcp_eager_lock); 7103 tcp_eager_unlink(eager); 7104 if (eager->tcp_tconnind_started) { 7105 /* 7106 * The eager has sent a conn_ind up to the 7107 * listener but listener decides to close 7108 * instead. We need to drop the extra ref 7109 * placed on eager in tcp_rput_data() before 7110 * sending the conn_ind to listener. 7111 */ 7112 CONN_DEC_REF(econnp); 7113 } 7114 mutex_exit(&listener->tcp_eager_lock); 7115 CONN_DEC_REF(listener->tcp_connp); 7116 } 7117 7118 if (eager->tcp_state > TCPS_BOUND) 7119 tcp_close_detached(eager); 7120 } 7121 7122 /* 7123 * Reset any eager connection hanging off this listener marked 7124 * with 'seqnum' and then reclaim it's resources. 7125 */ 7126 static boolean_t 7127 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7128 { 7129 tcp_t *eager; 7130 mblk_t *mp; 7131 7132 TCP_STAT(tcp_eager_blowoff_calls); 7133 eager = listener; 7134 mutex_enter(&listener->tcp_eager_lock); 7135 do { 7136 eager = eager->tcp_eager_next_q; 7137 if (eager == NULL) { 7138 mutex_exit(&listener->tcp_eager_lock); 7139 return (B_FALSE); 7140 } 7141 } while (eager->tcp_conn_req_seqnum != seqnum); 7142 7143 if (eager->tcp_closemp_used > 0) { 7144 mutex_exit(&listener->tcp_eager_lock); 7145 return (B_TRUE); 7146 } 7147 eager->tcp_closemp_used = 1; 7148 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7149 CONN_INC_REF(eager->tcp_connp); 7150 mutex_exit(&listener->tcp_eager_lock); 7151 mp = &eager->tcp_closemp; 7152 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7153 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7154 return (B_TRUE); 7155 } 7156 7157 /* 7158 * Reset any eager connection hanging off this listener 7159 * and then reclaim it's resources. 7160 */ 7161 static void 7162 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7163 { 7164 tcp_t *eager; 7165 mblk_t *mp; 7166 7167 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7168 7169 if (!q0_only) { 7170 /* First cleanup q */ 7171 TCP_STAT(tcp_eager_blowoff_q); 7172 eager = listener->tcp_eager_next_q; 7173 while (eager != NULL) { 7174 if (eager->tcp_closemp_used == 0) { 7175 eager->tcp_closemp_used = 1; 7176 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7177 CONN_INC_REF(eager->tcp_connp); 7178 mp = &eager->tcp_closemp; 7179 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7180 tcp_eager_kill, eager->tcp_connp, 7181 SQTAG_TCP_EAGER_CLEANUP); 7182 } 7183 eager = eager->tcp_eager_next_q; 7184 } 7185 } 7186 /* Then cleanup q0 */ 7187 TCP_STAT(tcp_eager_blowoff_q0); 7188 eager = listener->tcp_eager_next_q0; 7189 while (eager != listener) { 7190 if (eager->tcp_closemp_used == 0) { 7191 eager->tcp_closemp_used = 1; 7192 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7193 CONN_INC_REF(eager->tcp_connp); 7194 mp = &eager->tcp_closemp; 7195 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7196 tcp_eager_kill, eager->tcp_connp, 7197 SQTAG_TCP_EAGER_CLEANUP_Q0); 7198 } 7199 eager = eager->tcp_eager_next_q0; 7200 } 7201 } 7202 7203 /* 7204 * If we are an eager connection hanging off a listener that hasn't 7205 * formally accepted the connection yet, get off his list and blow off 7206 * any data that we have accumulated. 7207 */ 7208 static void 7209 tcp_eager_unlink(tcp_t *tcp) 7210 { 7211 tcp_t *listener = tcp->tcp_listener; 7212 7213 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7214 ASSERT(listener != NULL); 7215 if (tcp->tcp_eager_next_q0 != NULL) { 7216 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7217 7218 /* Remove the eager tcp from q0 */ 7219 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7220 tcp->tcp_eager_prev_q0; 7221 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7222 tcp->tcp_eager_next_q0; 7223 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7224 listener->tcp_conn_req_cnt_q0--; 7225 7226 tcp->tcp_eager_next_q0 = NULL; 7227 tcp->tcp_eager_prev_q0 = NULL; 7228 7229 /* 7230 * Take the eager out, if it is in the list of droppable 7231 * eagers. 7232 */ 7233 MAKE_UNDROPPABLE(tcp); 7234 7235 if (tcp->tcp_syn_rcvd_timeout != 0) { 7236 /* we have timed out before */ 7237 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7238 listener->tcp_syn_rcvd_timeout--; 7239 } 7240 } else { 7241 tcp_t **tcpp = &listener->tcp_eager_next_q; 7242 tcp_t *prev = NULL; 7243 7244 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7245 if (tcpp[0] == tcp) { 7246 if (listener->tcp_eager_last_q == tcp) { 7247 /* 7248 * If we are unlinking the last 7249 * element on the list, adjust 7250 * tail pointer. Set tail pointer 7251 * to nil when list is empty. 7252 */ 7253 ASSERT(tcp->tcp_eager_next_q == NULL); 7254 if (listener->tcp_eager_last_q == 7255 listener->tcp_eager_next_q) { 7256 listener->tcp_eager_last_q = 7257 NULL; 7258 } else { 7259 /* 7260 * We won't get here if there 7261 * is only one eager in the 7262 * list. 7263 */ 7264 ASSERT(prev != NULL); 7265 listener->tcp_eager_last_q = 7266 prev; 7267 } 7268 } 7269 tcpp[0] = tcp->tcp_eager_next_q; 7270 tcp->tcp_eager_next_q = NULL; 7271 tcp->tcp_eager_last_q = NULL; 7272 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7273 listener->tcp_conn_req_cnt_q--; 7274 break; 7275 } 7276 prev = tcpp[0]; 7277 } 7278 } 7279 tcp->tcp_listener = NULL; 7280 } 7281 7282 /* Shorthand to generate and send TPI error acks to our client */ 7283 static void 7284 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7285 { 7286 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7287 putnext(tcp->tcp_rq, mp); 7288 } 7289 7290 /* Shorthand to generate and send TPI error acks to our client */ 7291 static void 7292 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7293 int t_error, int sys_error) 7294 { 7295 struct T_error_ack *teackp; 7296 7297 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7298 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7299 teackp = (struct T_error_ack *)mp->b_rptr; 7300 teackp->ERROR_prim = primitive; 7301 teackp->TLI_error = t_error; 7302 teackp->UNIX_error = sys_error; 7303 putnext(tcp->tcp_rq, mp); 7304 } 7305 } 7306 7307 /* 7308 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7309 * but instead the code relies on: 7310 * - the fact that the address of the array and its size never changes 7311 * - the atomic assignment of the elements of the array 7312 */ 7313 /* ARGSUSED */ 7314 static int 7315 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7316 { 7317 int i; 7318 7319 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7320 if (tcp_g_epriv_ports[i] != 0) 7321 (void) mi_mpprintf(mp, "%d ", tcp_g_epriv_ports[i]); 7322 } 7323 return (0); 7324 } 7325 7326 /* 7327 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7328 * threads from changing it at the same time. 7329 */ 7330 /* ARGSUSED */ 7331 static int 7332 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7333 cred_t *cr) 7334 { 7335 long new_value; 7336 int i; 7337 7338 /* 7339 * Fail the request if the new value does not lie within the 7340 * port number limits. 7341 */ 7342 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7343 new_value <= 0 || new_value >= 65536) { 7344 return (EINVAL); 7345 } 7346 7347 mutex_enter(&tcp_epriv_port_lock); 7348 /* Check if the value is already in the list */ 7349 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7350 if (new_value == tcp_g_epriv_ports[i]) { 7351 mutex_exit(&tcp_epriv_port_lock); 7352 return (EEXIST); 7353 } 7354 } 7355 /* Find an empty slot */ 7356 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7357 if (tcp_g_epriv_ports[i] == 0) 7358 break; 7359 } 7360 if (i == tcp_g_num_epriv_ports) { 7361 mutex_exit(&tcp_epriv_port_lock); 7362 return (EOVERFLOW); 7363 } 7364 /* Set the new value */ 7365 tcp_g_epriv_ports[i] = (uint16_t)new_value; 7366 mutex_exit(&tcp_epriv_port_lock); 7367 return (0); 7368 } 7369 7370 /* 7371 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7372 * threads from changing it at the same time. 7373 */ 7374 /* ARGSUSED */ 7375 static int 7376 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7377 cred_t *cr) 7378 { 7379 long new_value; 7380 int i; 7381 7382 /* 7383 * Fail the request if the new value does not lie within the 7384 * port number limits. 7385 */ 7386 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7387 new_value >= 65536) { 7388 return (EINVAL); 7389 } 7390 7391 mutex_enter(&tcp_epriv_port_lock); 7392 /* Check that the value is already in the list */ 7393 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7394 if (tcp_g_epriv_ports[i] == new_value) 7395 break; 7396 } 7397 if (i == tcp_g_num_epriv_ports) { 7398 mutex_exit(&tcp_epriv_port_lock); 7399 return (ESRCH); 7400 } 7401 /* Clear the value */ 7402 tcp_g_epriv_ports[i] = 0; 7403 mutex_exit(&tcp_epriv_port_lock); 7404 return (0); 7405 } 7406 7407 /* Return the TPI/TLI equivalent of our current tcp_state */ 7408 static int 7409 tcp_tpistate(tcp_t *tcp) 7410 { 7411 switch (tcp->tcp_state) { 7412 case TCPS_IDLE: 7413 return (TS_UNBND); 7414 case TCPS_LISTEN: 7415 /* 7416 * Return whether there are outstanding T_CONN_IND waiting 7417 * for the matching T_CONN_RES. Therefore don't count q0. 7418 */ 7419 if (tcp->tcp_conn_req_cnt_q > 0) 7420 return (TS_WRES_CIND); 7421 else 7422 return (TS_IDLE); 7423 case TCPS_BOUND: 7424 return (TS_IDLE); 7425 case TCPS_SYN_SENT: 7426 return (TS_WCON_CREQ); 7427 case TCPS_SYN_RCVD: 7428 /* 7429 * Note: assumption: this has to the active open SYN_RCVD. 7430 * The passive instance is detached in SYN_RCVD stage of 7431 * incoming connection processing so we cannot get request 7432 * for T_info_ack on it. 7433 */ 7434 return (TS_WACK_CRES); 7435 case TCPS_ESTABLISHED: 7436 return (TS_DATA_XFER); 7437 case TCPS_CLOSE_WAIT: 7438 return (TS_WREQ_ORDREL); 7439 case TCPS_FIN_WAIT_1: 7440 return (TS_WIND_ORDREL); 7441 case TCPS_FIN_WAIT_2: 7442 return (TS_WIND_ORDREL); 7443 7444 case TCPS_CLOSING: 7445 case TCPS_LAST_ACK: 7446 case TCPS_TIME_WAIT: 7447 case TCPS_CLOSED: 7448 /* 7449 * Following TS_WACK_DREQ7 is a rendition of "not 7450 * yet TS_IDLE" TPI state. There is no best match to any 7451 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7452 * choose a value chosen that will map to TLI/XTI level 7453 * state of TSTATECHNG (state is process of changing) which 7454 * captures what this dummy state represents. 7455 */ 7456 return (TS_WACK_DREQ7); 7457 default: 7458 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7459 tcp->tcp_state, tcp_display(tcp, NULL, 7460 DISP_PORT_ONLY)); 7461 return (TS_UNBND); 7462 } 7463 } 7464 7465 static void 7466 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7467 { 7468 if (tcp->tcp_family == AF_INET6) 7469 *tia = tcp_g_t_info_ack_v6; 7470 else 7471 *tia = tcp_g_t_info_ack; 7472 tia->CURRENT_state = tcp_tpistate(tcp); 7473 tia->OPT_size = tcp_max_optsize; 7474 if (tcp->tcp_mss == 0) { 7475 /* Not yet set - tcp_open does not set mss */ 7476 if (tcp->tcp_ipversion == IPV4_VERSION) 7477 tia->TIDU_size = tcp_mss_def_ipv4; 7478 else 7479 tia->TIDU_size = tcp_mss_def_ipv6; 7480 } else { 7481 tia->TIDU_size = tcp->tcp_mss; 7482 } 7483 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7484 } 7485 7486 /* 7487 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7488 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7489 * tcp_g_t_info_ack. The current state of the stream is copied from 7490 * tcp_state. 7491 */ 7492 static void 7493 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7494 { 7495 t_uscalar_t cap_bits1; 7496 struct T_capability_ack *tcap; 7497 7498 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7499 freemsg(mp); 7500 return; 7501 } 7502 7503 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7504 7505 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7506 mp->b_datap->db_type, T_CAPABILITY_ACK); 7507 if (mp == NULL) 7508 return; 7509 7510 tcap = (struct T_capability_ack *)mp->b_rptr; 7511 tcap->CAP_bits1 = 0; 7512 7513 if (cap_bits1 & TC1_INFO) { 7514 tcp_copy_info(&tcap->INFO_ack, tcp); 7515 tcap->CAP_bits1 |= TC1_INFO; 7516 } 7517 7518 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7519 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7520 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7521 } 7522 7523 putnext(tcp->tcp_rq, mp); 7524 } 7525 7526 /* 7527 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7528 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7529 * The current state of the stream is copied from tcp_state. 7530 */ 7531 static void 7532 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7533 { 7534 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7535 T_INFO_ACK); 7536 if (!mp) { 7537 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7538 return; 7539 } 7540 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7541 putnext(tcp->tcp_rq, mp); 7542 } 7543 7544 /* Respond to the TPI addr request */ 7545 static void 7546 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7547 { 7548 sin_t *sin; 7549 mblk_t *ackmp; 7550 struct T_addr_ack *taa; 7551 7552 /* Make it large enough for worst case */ 7553 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7554 2 * sizeof (sin6_t), 1); 7555 if (ackmp == NULL) { 7556 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7557 return; 7558 } 7559 7560 if (tcp->tcp_ipversion == IPV6_VERSION) { 7561 tcp_addr_req_ipv6(tcp, ackmp); 7562 return; 7563 } 7564 taa = (struct T_addr_ack *)ackmp->b_rptr; 7565 7566 bzero(taa, sizeof (struct T_addr_ack)); 7567 ackmp->b_wptr = (uchar_t *)&taa[1]; 7568 7569 taa->PRIM_type = T_ADDR_ACK; 7570 ackmp->b_datap->db_type = M_PCPROTO; 7571 7572 /* 7573 * Note: Following code assumes 32 bit alignment of basic 7574 * data structures like sin_t and struct T_addr_ack. 7575 */ 7576 if (tcp->tcp_state >= TCPS_BOUND) { 7577 /* 7578 * Fill in local address 7579 */ 7580 taa->LOCADDR_length = sizeof (sin_t); 7581 taa->LOCADDR_offset = sizeof (*taa); 7582 7583 sin = (sin_t *)&taa[1]; 7584 7585 /* Fill zeroes and then intialize non-zero fields */ 7586 *sin = sin_null; 7587 7588 sin->sin_family = AF_INET; 7589 7590 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7591 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7592 7593 ackmp->b_wptr = (uchar_t *)&sin[1]; 7594 7595 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7596 /* 7597 * Fill in Remote address 7598 */ 7599 taa->REMADDR_length = sizeof (sin_t); 7600 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7601 taa->LOCADDR_length); 7602 7603 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7604 *sin = sin_null; 7605 sin->sin_family = AF_INET; 7606 sin->sin_addr.s_addr = tcp->tcp_remote; 7607 sin->sin_port = tcp->tcp_fport; 7608 7609 ackmp->b_wptr = (uchar_t *)&sin[1]; 7610 } 7611 } 7612 putnext(tcp->tcp_rq, ackmp); 7613 } 7614 7615 /* Assumes that tcp_addr_req gets enough space and alignment */ 7616 static void 7617 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7618 { 7619 sin6_t *sin6; 7620 struct T_addr_ack *taa; 7621 7622 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7623 ASSERT(OK_32PTR(ackmp->b_rptr)); 7624 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7625 2 * sizeof (sin6_t)); 7626 7627 taa = (struct T_addr_ack *)ackmp->b_rptr; 7628 7629 bzero(taa, sizeof (struct T_addr_ack)); 7630 ackmp->b_wptr = (uchar_t *)&taa[1]; 7631 7632 taa->PRIM_type = T_ADDR_ACK; 7633 ackmp->b_datap->db_type = M_PCPROTO; 7634 7635 /* 7636 * Note: Following code assumes 32 bit alignment of basic 7637 * data structures like sin6_t and struct T_addr_ack. 7638 */ 7639 if (tcp->tcp_state >= TCPS_BOUND) { 7640 /* 7641 * Fill in local address 7642 */ 7643 taa->LOCADDR_length = sizeof (sin6_t); 7644 taa->LOCADDR_offset = sizeof (*taa); 7645 7646 sin6 = (sin6_t *)&taa[1]; 7647 *sin6 = sin6_null; 7648 7649 sin6->sin6_family = AF_INET6; 7650 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7651 sin6->sin6_port = tcp->tcp_lport; 7652 7653 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7654 7655 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7656 /* 7657 * Fill in Remote address 7658 */ 7659 taa->REMADDR_length = sizeof (sin6_t); 7660 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7661 taa->LOCADDR_length); 7662 7663 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7664 *sin6 = sin6_null; 7665 sin6->sin6_family = AF_INET6; 7666 sin6->sin6_flowinfo = 7667 tcp->tcp_ip6h->ip6_vcf & 7668 ~IPV6_VERS_AND_FLOW_MASK; 7669 sin6->sin6_addr = tcp->tcp_remote_v6; 7670 sin6->sin6_port = tcp->tcp_fport; 7671 7672 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7673 } 7674 } 7675 putnext(tcp->tcp_rq, ackmp); 7676 } 7677 7678 /* 7679 * Handle reinitialization of a tcp structure. 7680 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7681 */ 7682 static void 7683 tcp_reinit(tcp_t *tcp) 7684 { 7685 mblk_t *mp; 7686 int err; 7687 7688 TCP_STAT(tcp_reinit_calls); 7689 7690 /* tcp_reinit should never be called for detached tcp_t's */ 7691 ASSERT(tcp->tcp_listener == NULL); 7692 ASSERT((tcp->tcp_family == AF_INET && 7693 tcp->tcp_ipversion == IPV4_VERSION) || 7694 (tcp->tcp_family == AF_INET6 && 7695 (tcp->tcp_ipversion == IPV4_VERSION || 7696 tcp->tcp_ipversion == IPV6_VERSION))); 7697 7698 /* Cancel outstanding timers */ 7699 tcp_timers_stop(tcp); 7700 7701 /* 7702 * Reset everything in the state vector, after updating global 7703 * MIB data from instance counters. 7704 */ 7705 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 7706 tcp->tcp_ibsegs = 0; 7707 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 7708 tcp->tcp_obsegs = 0; 7709 7710 tcp_close_mpp(&tcp->tcp_xmit_head); 7711 if (tcp->tcp_snd_zcopy_aware) 7712 tcp_zcopy_notify(tcp); 7713 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7714 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7715 if (tcp->tcp_flow_stopped && 7716 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7717 tcp_clrqfull(tcp); 7718 } 7719 tcp_close_mpp(&tcp->tcp_reass_head); 7720 tcp->tcp_reass_tail = NULL; 7721 if (tcp->tcp_rcv_list != NULL) { 7722 /* Free b_next chain */ 7723 tcp_close_mpp(&tcp->tcp_rcv_list); 7724 tcp->tcp_rcv_last_head = NULL; 7725 tcp->tcp_rcv_last_tail = NULL; 7726 tcp->tcp_rcv_cnt = 0; 7727 } 7728 tcp->tcp_rcv_last_tail = NULL; 7729 7730 if ((mp = tcp->tcp_urp_mp) != NULL) { 7731 freemsg(mp); 7732 tcp->tcp_urp_mp = NULL; 7733 } 7734 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7735 freemsg(mp); 7736 tcp->tcp_urp_mark_mp = NULL; 7737 } 7738 if (tcp->tcp_fused_sigurg_mp != NULL) { 7739 freeb(tcp->tcp_fused_sigurg_mp); 7740 tcp->tcp_fused_sigurg_mp = NULL; 7741 } 7742 7743 /* 7744 * Following is a union with two members which are 7745 * identical types and size so the following cleanup 7746 * is enough. 7747 */ 7748 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7749 7750 CL_INET_DISCONNECT(tcp); 7751 7752 /* 7753 * The connection can't be on the tcp_time_wait_head list 7754 * since it is not detached. 7755 */ 7756 ASSERT(tcp->tcp_time_wait_next == NULL); 7757 ASSERT(tcp->tcp_time_wait_prev == NULL); 7758 ASSERT(tcp->tcp_time_wait_expire == 0); 7759 7760 if (tcp->tcp_kssl_pending) { 7761 tcp->tcp_kssl_pending = B_FALSE; 7762 7763 /* Don't reset if the initialized by bind. */ 7764 if (tcp->tcp_kssl_ent != NULL) { 7765 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7766 KSSL_NO_PROXY); 7767 } 7768 } 7769 if (tcp->tcp_kssl_ctx != NULL) { 7770 kssl_release_ctx(tcp->tcp_kssl_ctx); 7771 tcp->tcp_kssl_ctx = NULL; 7772 } 7773 7774 /* 7775 * Reset/preserve other values 7776 */ 7777 tcp_reinit_values(tcp); 7778 ipcl_hash_remove(tcp->tcp_connp); 7779 conn_delete_ire(tcp->tcp_connp, NULL); 7780 7781 if (tcp->tcp_conn_req_max != 0) { 7782 /* 7783 * This is the case when a TLI program uses the same 7784 * transport end point to accept a connection. This 7785 * makes the TCP both a listener and acceptor. When 7786 * this connection is closed, we need to set the state 7787 * back to TCPS_LISTEN. Make sure that the eager list 7788 * is reinitialized. 7789 * 7790 * Note that this stream is still bound to the four 7791 * tuples of the previous connection in IP. If a new 7792 * SYN with different foreign address comes in, IP will 7793 * not find it and will send it to the global queue. In 7794 * the global queue, TCP will do a tcp_lookup_listener() 7795 * to find this stream. This works because this stream 7796 * is only removed from connected hash. 7797 * 7798 */ 7799 tcp->tcp_state = TCPS_LISTEN; 7800 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7801 tcp->tcp_eager_next_drop_q0 = tcp; 7802 tcp->tcp_eager_prev_drop_q0 = tcp; 7803 tcp->tcp_connp->conn_recv = tcp_conn_request; 7804 if (tcp->tcp_family == AF_INET6) { 7805 ASSERT(tcp->tcp_connp->conn_af_isv6); 7806 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7807 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7808 } else { 7809 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7810 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7811 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7812 } 7813 } else { 7814 tcp->tcp_state = TCPS_BOUND; 7815 } 7816 7817 /* 7818 * Initialize to default values 7819 * Can't fail since enough header template space already allocated 7820 * at open(). 7821 */ 7822 err = tcp_init_values(tcp); 7823 ASSERT(err == 0); 7824 /* Restore state in tcp_tcph */ 7825 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7826 if (tcp->tcp_ipversion == IPV4_VERSION) 7827 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7828 else 7829 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7830 /* 7831 * Copy of the src addr. in tcp_t is needed in tcp_t 7832 * since the lookup funcs can only lookup on tcp_t 7833 */ 7834 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7835 7836 ASSERT(tcp->tcp_ptpbhn != NULL); 7837 tcp->tcp_rq->q_hiwat = tcp_recv_hiwat; 7838 tcp->tcp_rwnd = tcp_recv_hiwat; 7839 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7840 tcp_mss_def_ipv6 : tcp_mss_def_ipv4; 7841 } 7842 7843 /* 7844 * Force values to zero that need be zero. 7845 * Do not touch values asociated with the BOUND or LISTEN state 7846 * since the connection will end up in that state after the reinit. 7847 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7848 * structure! 7849 */ 7850 static void 7851 tcp_reinit_values(tcp) 7852 tcp_t *tcp; 7853 { 7854 #ifndef lint 7855 #define DONTCARE(x) 7856 #define PRESERVE(x) 7857 #else 7858 #define DONTCARE(x) ((x) = (x)) 7859 #define PRESERVE(x) ((x) = (x)) 7860 #endif /* lint */ 7861 7862 PRESERVE(tcp->tcp_bind_hash); 7863 PRESERVE(tcp->tcp_ptpbhn); 7864 PRESERVE(tcp->tcp_acceptor_hash); 7865 PRESERVE(tcp->tcp_ptpahn); 7866 7867 /* Should be ASSERT NULL on these with new code! */ 7868 ASSERT(tcp->tcp_time_wait_next == NULL); 7869 ASSERT(tcp->tcp_time_wait_prev == NULL); 7870 ASSERT(tcp->tcp_time_wait_expire == 0); 7871 PRESERVE(tcp->tcp_state); 7872 PRESERVE(tcp->tcp_rq); 7873 PRESERVE(tcp->tcp_wq); 7874 7875 ASSERT(tcp->tcp_xmit_head == NULL); 7876 ASSERT(tcp->tcp_xmit_last == NULL); 7877 ASSERT(tcp->tcp_unsent == 0); 7878 ASSERT(tcp->tcp_xmit_tail == NULL); 7879 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7880 7881 tcp->tcp_snxt = 0; /* Displayed in mib */ 7882 tcp->tcp_suna = 0; /* Displayed in mib */ 7883 tcp->tcp_swnd = 0; 7884 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7885 7886 ASSERT(tcp->tcp_ibsegs == 0); 7887 ASSERT(tcp->tcp_obsegs == 0); 7888 7889 if (tcp->tcp_iphc != NULL) { 7890 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7891 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7892 } 7893 7894 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7895 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7896 DONTCARE(tcp->tcp_ipha); 7897 DONTCARE(tcp->tcp_ip6h); 7898 DONTCARE(tcp->tcp_ip_hdr_len); 7899 DONTCARE(tcp->tcp_tcph); 7900 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7901 tcp->tcp_valid_bits = 0; 7902 7903 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7904 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7905 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7906 tcp->tcp_last_rcv_lbolt = 0; 7907 7908 tcp->tcp_init_cwnd = 0; 7909 7910 tcp->tcp_urp_last_valid = 0; 7911 tcp->tcp_hard_binding = 0; 7912 tcp->tcp_hard_bound = 0; 7913 PRESERVE(tcp->tcp_cred); 7914 PRESERVE(tcp->tcp_cpid); 7915 PRESERVE(tcp->tcp_exclbind); 7916 7917 tcp->tcp_fin_acked = 0; 7918 tcp->tcp_fin_rcvd = 0; 7919 tcp->tcp_fin_sent = 0; 7920 tcp->tcp_ordrel_done = 0; 7921 7922 tcp->tcp_debug = 0; 7923 tcp->tcp_dontroute = 0; 7924 tcp->tcp_broadcast = 0; 7925 7926 tcp->tcp_useloopback = 0; 7927 tcp->tcp_reuseaddr = 0; 7928 tcp->tcp_oobinline = 0; 7929 tcp->tcp_dgram_errind = 0; 7930 7931 tcp->tcp_detached = 0; 7932 tcp->tcp_bind_pending = 0; 7933 tcp->tcp_unbind_pending = 0; 7934 tcp->tcp_deferred_clean_death = 0; 7935 7936 tcp->tcp_snd_ws_ok = B_FALSE; 7937 tcp->tcp_snd_ts_ok = B_FALSE; 7938 tcp->tcp_linger = 0; 7939 tcp->tcp_ka_enabled = 0; 7940 tcp->tcp_zero_win_probe = 0; 7941 7942 tcp->tcp_loopback = 0; 7943 tcp->tcp_localnet = 0; 7944 tcp->tcp_syn_defense = 0; 7945 tcp->tcp_set_timer = 0; 7946 7947 tcp->tcp_active_open = 0; 7948 ASSERT(tcp->tcp_timeout == B_FALSE); 7949 tcp->tcp_rexmit = B_FALSE; 7950 tcp->tcp_xmit_zc_clean = B_FALSE; 7951 7952 tcp->tcp_snd_sack_ok = B_FALSE; 7953 PRESERVE(tcp->tcp_recvdstaddr); 7954 tcp->tcp_hwcksum = B_FALSE; 7955 7956 tcp->tcp_ire_ill_check_done = B_FALSE; 7957 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7958 7959 tcp->tcp_mdt = B_FALSE; 7960 tcp->tcp_mdt_hdr_head = 0; 7961 tcp->tcp_mdt_hdr_tail = 0; 7962 7963 tcp->tcp_conn_def_q0 = 0; 7964 tcp->tcp_ip_forward_progress = B_FALSE; 7965 tcp->tcp_anon_priv_bind = 0; 7966 tcp->tcp_ecn_ok = B_FALSE; 7967 7968 tcp->tcp_cwr = B_FALSE; 7969 tcp->tcp_ecn_echo_on = B_FALSE; 7970 7971 if (tcp->tcp_sack_info != NULL) { 7972 if (tcp->tcp_notsack_list != NULL) { 7973 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7974 } 7975 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7976 tcp->tcp_sack_info = NULL; 7977 } 7978 7979 tcp->tcp_rcv_ws = 0; 7980 tcp->tcp_snd_ws = 0; 7981 tcp->tcp_ts_recent = 0; 7982 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7983 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7984 tcp->tcp_if_mtu = 0; 7985 7986 ASSERT(tcp->tcp_reass_head == NULL); 7987 ASSERT(tcp->tcp_reass_tail == NULL); 7988 7989 tcp->tcp_cwnd_cnt = 0; 7990 7991 ASSERT(tcp->tcp_rcv_list == NULL); 7992 ASSERT(tcp->tcp_rcv_last_head == NULL); 7993 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7994 ASSERT(tcp->tcp_rcv_cnt == 0); 7995 7996 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7997 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7998 tcp->tcp_csuna = 0; 7999 8000 tcp->tcp_rto = 0; /* Displayed in MIB */ 8001 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8002 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8003 tcp->tcp_rtt_update = 0; 8004 8005 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8006 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8007 8008 tcp->tcp_rack = 0; /* Displayed in mib */ 8009 tcp->tcp_rack_cnt = 0; 8010 tcp->tcp_rack_cur_max = 0; 8011 tcp->tcp_rack_abs_max = 0; 8012 8013 tcp->tcp_max_swnd = 0; 8014 8015 ASSERT(tcp->tcp_listener == NULL); 8016 8017 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8018 8019 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8020 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8021 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8022 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8023 8024 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8025 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8026 PRESERVE(tcp->tcp_conn_req_max); 8027 PRESERVE(tcp->tcp_conn_req_seqnum); 8028 8029 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8030 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8031 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8032 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8033 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8034 8035 tcp->tcp_lingertime = 0; 8036 8037 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8038 ASSERT(tcp->tcp_urp_mp == NULL); 8039 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8040 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8041 8042 ASSERT(tcp->tcp_eager_next_q == NULL); 8043 ASSERT(tcp->tcp_eager_last_q == NULL); 8044 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8045 tcp->tcp_eager_prev_q0 == NULL) || 8046 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8047 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8048 8049 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8050 tcp->tcp_eager_prev_drop_q0 == NULL) || 8051 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8052 8053 tcp->tcp_client_errno = 0; 8054 8055 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8056 8057 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8058 8059 PRESERVE(tcp->tcp_bound_source_v6); 8060 tcp->tcp_last_sent_len = 0; 8061 tcp->tcp_dupack_cnt = 0; 8062 8063 tcp->tcp_fport = 0; /* Displayed in MIB */ 8064 PRESERVE(tcp->tcp_lport); 8065 8066 PRESERVE(tcp->tcp_acceptor_lockp); 8067 8068 ASSERT(tcp->tcp_ordrelid == 0); 8069 PRESERVE(tcp->tcp_acceptor_id); 8070 DONTCARE(tcp->tcp_ipsec_overhead); 8071 8072 /* 8073 * If tcp_tracing flag is ON (i.e. We have a trace buffer 8074 * in tcp structure and now tracing), Re-initialize all 8075 * members of tcp_traceinfo. 8076 */ 8077 if (tcp->tcp_tracebuf != NULL) { 8078 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 8079 } 8080 8081 PRESERVE(tcp->tcp_family); 8082 if (tcp->tcp_family == AF_INET6) { 8083 tcp->tcp_ipversion = IPV6_VERSION; 8084 tcp->tcp_mss = tcp_mss_def_ipv6; 8085 } else { 8086 tcp->tcp_ipversion = IPV4_VERSION; 8087 tcp->tcp_mss = tcp_mss_def_ipv4; 8088 } 8089 8090 tcp->tcp_bound_if = 0; 8091 tcp->tcp_ipv6_recvancillary = 0; 8092 tcp->tcp_recvifindex = 0; 8093 tcp->tcp_recvhops = 0; 8094 tcp->tcp_closed = 0; 8095 tcp->tcp_cleandeathtag = 0; 8096 if (tcp->tcp_hopopts != NULL) { 8097 mi_free(tcp->tcp_hopopts); 8098 tcp->tcp_hopopts = NULL; 8099 tcp->tcp_hopoptslen = 0; 8100 } 8101 ASSERT(tcp->tcp_hopoptslen == 0); 8102 if (tcp->tcp_dstopts != NULL) { 8103 mi_free(tcp->tcp_dstopts); 8104 tcp->tcp_dstopts = NULL; 8105 tcp->tcp_dstoptslen = 0; 8106 } 8107 ASSERT(tcp->tcp_dstoptslen == 0); 8108 if (tcp->tcp_rtdstopts != NULL) { 8109 mi_free(tcp->tcp_rtdstopts); 8110 tcp->tcp_rtdstopts = NULL; 8111 tcp->tcp_rtdstoptslen = 0; 8112 } 8113 ASSERT(tcp->tcp_rtdstoptslen == 0); 8114 if (tcp->tcp_rthdr != NULL) { 8115 mi_free(tcp->tcp_rthdr); 8116 tcp->tcp_rthdr = NULL; 8117 tcp->tcp_rthdrlen = 0; 8118 } 8119 ASSERT(tcp->tcp_rthdrlen == 0); 8120 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8121 8122 /* Reset fusion-related fields */ 8123 tcp->tcp_fused = B_FALSE; 8124 tcp->tcp_unfusable = B_FALSE; 8125 tcp->tcp_fused_sigurg = B_FALSE; 8126 tcp->tcp_direct_sockfs = B_FALSE; 8127 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8128 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8129 tcp->tcp_loopback_peer = NULL; 8130 tcp->tcp_fuse_rcv_hiwater = 0; 8131 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8132 tcp->tcp_fuse_rcv_unread_cnt = 0; 8133 8134 tcp->tcp_lso = B_FALSE; 8135 8136 tcp->tcp_in_ack_unsent = 0; 8137 tcp->tcp_cork = B_FALSE; 8138 tcp->tcp_tconnind_started = B_FALSE; 8139 8140 PRESERVE(tcp->tcp_squeue_bytes); 8141 8142 ASSERT(tcp->tcp_kssl_ctx == NULL); 8143 ASSERT(!tcp->tcp_kssl_pending); 8144 PRESERVE(tcp->tcp_kssl_ent); 8145 8146 tcp->tcp_closemp_used = 0; 8147 8148 #ifdef DEBUG 8149 DONTCARE(tcp->tcmp_stk[0]); 8150 #endif 8151 8152 8153 #undef DONTCARE 8154 #undef PRESERVE 8155 } 8156 8157 /* 8158 * Allocate necessary resources and initialize state vector. 8159 * Guaranteed not to fail so that when an error is returned, 8160 * the caller doesn't need to do any additional cleanup. 8161 */ 8162 int 8163 tcp_init(tcp_t *tcp, queue_t *q) 8164 { 8165 int err; 8166 8167 tcp->tcp_rq = q; 8168 tcp->tcp_wq = WR(q); 8169 tcp->tcp_state = TCPS_IDLE; 8170 if ((err = tcp_init_values(tcp)) != 0) 8171 tcp_timers_stop(tcp); 8172 return (err); 8173 } 8174 8175 static int 8176 tcp_init_values(tcp_t *tcp) 8177 { 8178 int err; 8179 8180 ASSERT((tcp->tcp_family == AF_INET && 8181 tcp->tcp_ipversion == IPV4_VERSION) || 8182 (tcp->tcp_family == AF_INET6 && 8183 (tcp->tcp_ipversion == IPV4_VERSION || 8184 tcp->tcp_ipversion == IPV6_VERSION))); 8185 8186 /* 8187 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8188 * will be close to tcp_rexmit_interval_initial. By doing this, we 8189 * allow the algorithm to adjust slowly to large fluctuations of RTT 8190 * during first few transmissions of a connection as seen in slow 8191 * links. 8192 */ 8193 tcp->tcp_rtt_sa = tcp_rexmit_interval_initial << 2; 8194 tcp->tcp_rtt_sd = tcp_rexmit_interval_initial >> 1; 8195 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8196 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8197 tcp_conn_grace_period; 8198 if (tcp->tcp_rto < tcp_rexmit_interval_min) 8199 tcp->tcp_rto = tcp_rexmit_interval_min; 8200 tcp->tcp_timer_backoff = 0; 8201 tcp->tcp_ms_we_have_waited = 0; 8202 tcp->tcp_last_recv_time = lbolt; 8203 tcp->tcp_cwnd_max = tcp_cwnd_max_; 8204 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8205 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8206 8207 tcp->tcp_maxpsz = tcp_maxpsz_multiplier; 8208 8209 tcp->tcp_first_timer_threshold = tcp_ip_notify_interval; 8210 tcp->tcp_first_ctimer_threshold = tcp_ip_notify_cinterval; 8211 tcp->tcp_second_timer_threshold = tcp_ip_abort_interval; 8212 /* 8213 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8214 * passive open. 8215 */ 8216 tcp->tcp_second_ctimer_threshold = tcp_ip_abort_cinterval; 8217 8218 tcp->tcp_naglim = tcp_naglim_def; 8219 8220 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8221 8222 tcp->tcp_mdt_hdr_head = 0; 8223 tcp->tcp_mdt_hdr_tail = 0; 8224 8225 /* Reset fusion-related fields */ 8226 tcp->tcp_fused = B_FALSE; 8227 tcp->tcp_unfusable = B_FALSE; 8228 tcp->tcp_fused_sigurg = B_FALSE; 8229 tcp->tcp_direct_sockfs = B_FALSE; 8230 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8231 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8232 tcp->tcp_loopback_peer = NULL; 8233 tcp->tcp_fuse_rcv_hiwater = 0; 8234 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8235 tcp->tcp_fuse_rcv_unread_cnt = 0; 8236 8237 /* Initialize the header template */ 8238 if (tcp->tcp_ipversion == IPV4_VERSION) { 8239 err = tcp_header_init_ipv4(tcp); 8240 } else { 8241 err = tcp_header_init_ipv6(tcp); 8242 } 8243 if (err) 8244 return (err); 8245 8246 /* 8247 * Init the window scale to the max so tcp_rwnd_set() won't pare 8248 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8249 */ 8250 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8251 tcp->tcp_xmit_lowater = tcp_xmit_lowat; 8252 tcp->tcp_xmit_hiwater = tcp_xmit_hiwat; 8253 8254 tcp->tcp_cork = B_FALSE; 8255 /* 8256 * Init the tcp_debug option. This value determines whether TCP 8257 * calls strlog() to print out debug messages. Doing this 8258 * initialization here means that this value is not inherited thru 8259 * tcp_reinit(). 8260 */ 8261 tcp->tcp_debug = tcp_dbg; 8262 8263 tcp->tcp_ka_interval = tcp_keepalive_interval; 8264 tcp->tcp_ka_abort_thres = tcp_keepalive_abort_interval; 8265 8266 return (0); 8267 } 8268 8269 /* 8270 * Initialize the IPv4 header. Loses any record of any IP options. 8271 */ 8272 static int 8273 tcp_header_init_ipv4(tcp_t *tcp) 8274 { 8275 tcph_t *tcph; 8276 uint32_t sum; 8277 conn_t *connp; 8278 8279 /* 8280 * This is a simple initialization. If there's 8281 * already a template, it should never be too small, 8282 * so reuse it. Otherwise, allocate space for the new one. 8283 */ 8284 if (tcp->tcp_iphc == NULL) { 8285 ASSERT(tcp->tcp_iphc_len == 0); 8286 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8287 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8288 if (tcp->tcp_iphc == NULL) { 8289 tcp->tcp_iphc_len = 0; 8290 return (ENOMEM); 8291 } 8292 } 8293 8294 /* options are gone; may need a new label */ 8295 connp = tcp->tcp_connp; 8296 connp->conn_mlp_type = mlptSingle; 8297 connp->conn_ulp_labeled = !is_system_labeled(); 8298 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8299 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8300 tcp->tcp_ip6h = NULL; 8301 tcp->tcp_ipversion = IPV4_VERSION; 8302 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8303 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8304 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8305 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8306 tcp->tcp_ipha->ipha_version_and_hdr_length 8307 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8308 tcp->tcp_ipha->ipha_ident = 0; 8309 8310 tcp->tcp_ttl = (uchar_t)tcp_ipv4_ttl; 8311 tcp->tcp_tos = 0; 8312 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8313 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcp_ipv4_ttl; 8314 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8315 8316 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8317 tcp->tcp_tcph = tcph; 8318 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8319 /* 8320 * IP wants our header length in the checksum field to 8321 * allow it to perform a single pseudo-header+checksum 8322 * calculation on behalf of TCP. 8323 * Include the adjustment for a source route once IP_OPTIONS is set. 8324 */ 8325 sum = sizeof (tcph_t) + tcp->tcp_sum; 8326 sum = (sum >> 16) + (sum & 0xFFFF); 8327 U16_TO_ABE16(sum, tcph->th_sum); 8328 return (0); 8329 } 8330 8331 /* 8332 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8333 */ 8334 static int 8335 tcp_header_init_ipv6(tcp_t *tcp) 8336 { 8337 tcph_t *tcph; 8338 uint32_t sum; 8339 conn_t *connp; 8340 8341 /* 8342 * This is a simple initialization. If there's 8343 * already a template, it should never be too small, 8344 * so reuse it. Otherwise, allocate space for the new one. 8345 * Ensure that there is enough space to "downgrade" the tcp_t 8346 * to an IPv4 tcp_t. This requires having space for a full load 8347 * of IPv4 options, as well as a full load of TCP options 8348 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8349 * than a v6 header and a TCP header with a full load of TCP options 8350 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8351 * We want to avoid reallocation in the "downgraded" case when 8352 * processing outbound IPv4 options. 8353 */ 8354 if (tcp->tcp_iphc == NULL) { 8355 ASSERT(tcp->tcp_iphc_len == 0); 8356 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8357 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8358 if (tcp->tcp_iphc == NULL) { 8359 tcp->tcp_iphc_len = 0; 8360 return (ENOMEM); 8361 } 8362 } 8363 8364 /* options are gone; may need a new label */ 8365 connp = tcp->tcp_connp; 8366 connp->conn_mlp_type = mlptSingle; 8367 connp->conn_ulp_labeled = !is_system_labeled(); 8368 8369 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8370 tcp->tcp_ipversion = IPV6_VERSION; 8371 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8372 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8373 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8374 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8375 tcp->tcp_ipha = NULL; 8376 8377 /* Initialize the header template */ 8378 8379 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8380 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8381 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8382 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcp_ipv6_hoplimit; 8383 8384 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8385 tcp->tcp_tcph = tcph; 8386 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8387 /* 8388 * IP wants our header length in the checksum field to 8389 * allow it to perform a single psuedo-header+checksum 8390 * calculation on behalf of TCP. 8391 * Include the adjustment for a source route when IPV6_RTHDR is set. 8392 */ 8393 sum = sizeof (tcph_t) + tcp->tcp_sum; 8394 sum = (sum >> 16) + (sum & 0xFFFF); 8395 U16_TO_ABE16(sum, tcph->th_sum); 8396 return (0); 8397 } 8398 8399 /* At minimum we need 4 bytes in the TCP header for the lookup */ 8400 #define ICMP_MIN_TCP_HDR 12 8401 8402 /* 8403 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8404 * passed up by IP. The message is always received on the correct tcp_t. 8405 * Assumes that IP has pulled up everything up to and including the ICMP header. 8406 */ 8407 void 8408 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8409 { 8410 icmph_t *icmph; 8411 ipha_t *ipha; 8412 int iph_hdr_length; 8413 tcph_t *tcph; 8414 boolean_t ipsec_mctl = B_FALSE; 8415 boolean_t secure; 8416 mblk_t *first_mp = mp; 8417 uint32_t new_mss; 8418 uint32_t ratio; 8419 size_t mp_size = MBLKL(mp); 8420 uint32_t seg_ack; 8421 uint32_t seg_seq; 8422 8423 /* Assume IP provides aligned packets - otherwise toss */ 8424 if (!OK_32PTR(mp->b_rptr)) { 8425 freemsg(mp); 8426 return; 8427 } 8428 8429 /* 8430 * Since ICMP errors are normal data marked with M_CTL when sent 8431 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8432 * packets starting with an ipsec_info_t, see ipsec_info.h. 8433 */ 8434 if ((mp_size == sizeof (ipsec_info_t)) && 8435 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8436 ASSERT(mp->b_cont != NULL); 8437 mp = mp->b_cont; 8438 /* IP should have done this */ 8439 ASSERT(OK_32PTR(mp->b_rptr)); 8440 mp_size = MBLKL(mp); 8441 ipsec_mctl = B_TRUE; 8442 } 8443 8444 /* 8445 * Verify that we have a complete outer IP header. If not, drop it. 8446 */ 8447 if (mp_size < sizeof (ipha_t)) { 8448 noticmpv4: 8449 freemsg(first_mp); 8450 return; 8451 } 8452 8453 ipha = (ipha_t *)mp->b_rptr; 8454 /* 8455 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8456 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8457 */ 8458 switch (IPH_HDR_VERSION(ipha)) { 8459 case IPV6_VERSION: 8460 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8461 return; 8462 case IPV4_VERSION: 8463 break; 8464 default: 8465 goto noticmpv4; 8466 } 8467 8468 /* Skip past the outer IP and ICMP headers */ 8469 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8470 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8471 /* 8472 * If we don't have the correct outer IP header length or if the ULP 8473 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8474 * send it upstream. 8475 */ 8476 if (iph_hdr_length < sizeof (ipha_t) || 8477 ipha->ipha_protocol != IPPROTO_ICMP || 8478 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8479 goto noticmpv4; 8480 } 8481 ipha = (ipha_t *)&icmph[1]; 8482 8483 /* Skip past the inner IP and find the ULP header */ 8484 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8485 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8486 /* 8487 * If we don't have the correct inner IP header length or if the ULP 8488 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8489 * bytes of TCP header, drop it. 8490 */ 8491 if (iph_hdr_length < sizeof (ipha_t) || 8492 ipha->ipha_protocol != IPPROTO_TCP || 8493 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8494 goto noticmpv4; 8495 } 8496 8497 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8498 if (ipsec_mctl) { 8499 secure = ipsec_in_is_secure(first_mp); 8500 } else { 8501 secure = B_FALSE; 8502 } 8503 if (secure) { 8504 /* 8505 * If we are willing to accept this in clear 8506 * we don't have to verify policy. 8507 */ 8508 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8509 if (!tcp_check_policy(tcp, first_mp, 8510 ipha, NULL, secure, ipsec_mctl)) { 8511 /* 8512 * tcp_check_policy called 8513 * ip_drop_packet() on failure. 8514 */ 8515 return; 8516 } 8517 } 8518 } 8519 } else if (ipsec_mctl) { 8520 /* 8521 * This is a hard_bound connection. IP has already 8522 * verified policy. We don't have to do it again. 8523 */ 8524 freeb(first_mp); 8525 first_mp = mp; 8526 ipsec_mctl = B_FALSE; 8527 } 8528 8529 seg_ack = ABE32_TO_U32(tcph->th_ack); 8530 seg_seq = ABE32_TO_U32(tcph->th_seq); 8531 /* 8532 * TCP SHOULD check that the TCP sequence number contained in 8533 * payload of the ICMP error message is within the range 8534 * SND.UNA <= SEG.SEQ < SND.NXT. and also SEG.ACK <= RECV.NXT 8535 */ 8536 if (SEQ_LT(seg_seq, tcp->tcp_suna) || 8537 SEQ_GEQ(seg_seq, tcp->tcp_snxt) || 8538 SEQ_GT(seg_ack, tcp->tcp_rnxt)) { 8539 /* 8540 * If the ICMP message is bogus, should we kill the 8541 * connection, or should we just drop the bogus ICMP 8542 * message? It would probably make more sense to just 8543 * drop the message so that if this one managed to get 8544 * in, the real connection should not suffer. 8545 */ 8546 goto noticmpv4; 8547 } 8548 8549 switch (icmph->icmph_type) { 8550 case ICMP_DEST_UNREACHABLE: 8551 switch (icmph->icmph_code) { 8552 case ICMP_FRAGMENTATION_NEEDED: 8553 /* 8554 * Reduce the MSS based on the new MTU. This will 8555 * eliminate any fragmentation locally. 8556 * N.B. There may well be some funny side-effects on 8557 * the local send policy and the remote receive policy. 8558 * Pending further research, we provide 8559 * tcp_ignore_path_mtu just in case this proves 8560 * disastrous somewhere. 8561 * 8562 * After updating the MSS, retransmit part of the 8563 * dropped segment using the new mss by calling 8564 * tcp_wput_data(). Need to adjust all those 8565 * params to make sure tcp_wput_data() work properly. 8566 */ 8567 if (tcp_ignore_path_mtu) 8568 break; 8569 8570 /* 8571 * Decrease the MSS by time stamp options 8572 * IP options and IPSEC options. tcp_hdr_len 8573 * includes time stamp option and IP option 8574 * length. 8575 */ 8576 8577 new_mss = ntohs(icmph->icmph_du_mtu) - 8578 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8579 8580 /* 8581 * Only update the MSS if the new one is 8582 * smaller than the previous one. This is 8583 * to avoid problems when getting multiple 8584 * ICMP errors for the same MTU. 8585 */ 8586 if (new_mss >= tcp->tcp_mss) 8587 break; 8588 8589 /* 8590 * Stop doing PMTU if new_mss is less than 68 8591 * or less than tcp_mss_min. 8592 * The value 68 comes from rfc 1191. 8593 */ 8594 if (new_mss < MAX(68, tcp_mss_min)) 8595 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8596 0; 8597 8598 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8599 ASSERT(ratio >= 1); 8600 tcp_mss_set(tcp, new_mss); 8601 8602 /* 8603 * Make sure we have something to 8604 * send. 8605 */ 8606 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8607 (tcp->tcp_xmit_head != NULL)) { 8608 /* 8609 * Shrink tcp_cwnd in 8610 * proportion to the old MSS/new MSS. 8611 */ 8612 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8613 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8614 (tcp->tcp_unsent == 0)) { 8615 tcp->tcp_rexmit_max = tcp->tcp_fss; 8616 } else { 8617 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8618 } 8619 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8620 tcp->tcp_rexmit = B_TRUE; 8621 tcp->tcp_dupack_cnt = 0; 8622 tcp->tcp_snd_burst = TCP_CWND_SS; 8623 tcp_ss_rexmit(tcp); 8624 } 8625 break; 8626 case ICMP_PORT_UNREACHABLE: 8627 case ICMP_PROTOCOL_UNREACHABLE: 8628 switch (tcp->tcp_state) { 8629 case TCPS_SYN_SENT: 8630 case TCPS_SYN_RCVD: 8631 /* 8632 * ICMP can snipe away incipient 8633 * TCP connections as long as 8634 * seq number is same as initial 8635 * send seq number. 8636 */ 8637 if (seg_seq == tcp->tcp_iss) { 8638 (void) tcp_clean_death(tcp, 8639 ECONNREFUSED, 6); 8640 } 8641 break; 8642 } 8643 break; 8644 case ICMP_HOST_UNREACHABLE: 8645 case ICMP_NET_UNREACHABLE: 8646 /* Record the error in case we finally time out. */ 8647 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8648 tcp->tcp_client_errno = EHOSTUNREACH; 8649 else 8650 tcp->tcp_client_errno = ENETUNREACH; 8651 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8652 if (tcp->tcp_listener != NULL && 8653 tcp->tcp_listener->tcp_syn_defense) { 8654 /* 8655 * Ditch the half-open connection if we 8656 * suspect a SYN attack is under way. 8657 */ 8658 tcp_ip_ire_mark_advice(tcp); 8659 (void) tcp_clean_death(tcp, 8660 tcp->tcp_client_errno, 7); 8661 } 8662 } 8663 break; 8664 default: 8665 break; 8666 } 8667 break; 8668 case ICMP_SOURCE_QUENCH: { 8669 /* 8670 * use a global boolean to control 8671 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8672 * The default is false. 8673 */ 8674 if (tcp_icmp_source_quench) { 8675 /* 8676 * Reduce the sending rate as if we got a 8677 * retransmit timeout 8678 */ 8679 uint32_t npkt; 8680 8681 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8682 tcp->tcp_mss; 8683 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8684 tcp->tcp_cwnd = tcp->tcp_mss; 8685 tcp->tcp_cwnd_cnt = 0; 8686 } 8687 break; 8688 } 8689 } 8690 freemsg(first_mp); 8691 } 8692 8693 /* 8694 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8695 * error messages passed up by IP. 8696 * Assumes that IP has pulled up all the extension headers as well 8697 * as the ICMPv6 header. 8698 */ 8699 static void 8700 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8701 { 8702 icmp6_t *icmp6; 8703 ip6_t *ip6h; 8704 uint16_t iph_hdr_length; 8705 tcpha_t *tcpha; 8706 uint8_t *nexthdrp; 8707 uint32_t new_mss; 8708 uint32_t ratio; 8709 boolean_t secure; 8710 mblk_t *first_mp = mp; 8711 size_t mp_size; 8712 uint32_t seg_ack; 8713 uint32_t seg_seq; 8714 8715 /* 8716 * The caller has determined if this is an IPSEC_IN packet and 8717 * set ipsec_mctl appropriately (see tcp_icmp_error). 8718 */ 8719 if (ipsec_mctl) 8720 mp = mp->b_cont; 8721 8722 mp_size = MBLKL(mp); 8723 8724 /* 8725 * Verify that we have a complete IP header. If not, send it upstream. 8726 */ 8727 if (mp_size < sizeof (ip6_t)) { 8728 noticmpv6: 8729 freemsg(first_mp); 8730 return; 8731 } 8732 8733 /* 8734 * Verify this is an ICMPV6 packet, else send it upstream. 8735 */ 8736 ip6h = (ip6_t *)mp->b_rptr; 8737 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8738 iph_hdr_length = IPV6_HDR_LEN; 8739 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8740 &nexthdrp) || 8741 *nexthdrp != IPPROTO_ICMPV6) { 8742 goto noticmpv6; 8743 } 8744 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8745 ip6h = (ip6_t *)&icmp6[1]; 8746 /* 8747 * Verify if we have a complete ICMP and inner IP header. 8748 */ 8749 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8750 goto noticmpv6; 8751 8752 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8753 goto noticmpv6; 8754 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8755 /* 8756 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8757 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8758 * packet. 8759 */ 8760 if ((*nexthdrp != IPPROTO_TCP) || 8761 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8762 goto noticmpv6; 8763 } 8764 8765 /* 8766 * ICMP errors come on the right queue or come on 8767 * listener/global queue for detached connections and 8768 * get switched to the right queue. If it comes on the 8769 * right queue, policy check has already been done by IP 8770 * and thus free the first_mp without verifying the policy. 8771 * If it has come for a non-hard bound connection, we need 8772 * to verify policy as IP may not have done it. 8773 */ 8774 if (!tcp->tcp_hard_bound) { 8775 if (ipsec_mctl) { 8776 secure = ipsec_in_is_secure(first_mp); 8777 } else { 8778 secure = B_FALSE; 8779 } 8780 if (secure) { 8781 /* 8782 * If we are willing to accept this in clear 8783 * we don't have to verify policy. 8784 */ 8785 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8786 if (!tcp_check_policy(tcp, first_mp, 8787 NULL, ip6h, secure, ipsec_mctl)) { 8788 /* 8789 * tcp_check_policy called 8790 * ip_drop_packet() on failure. 8791 */ 8792 return; 8793 } 8794 } 8795 } 8796 } else if (ipsec_mctl) { 8797 /* 8798 * This is a hard_bound connection. IP has already 8799 * verified policy. We don't have to do it again. 8800 */ 8801 freeb(first_mp); 8802 first_mp = mp; 8803 ipsec_mctl = B_FALSE; 8804 } 8805 8806 seg_ack = ntohl(tcpha->tha_ack); 8807 seg_seq = ntohl(tcpha->tha_seq); 8808 /* 8809 * TCP SHOULD check that the TCP sequence number contained in 8810 * payload of the ICMP error message is within the range 8811 * SND.UNA <= SEG.SEQ < SND.NXT. and also SEG.ACK <= RECV.NXT 8812 */ 8813 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt) || 8814 SEQ_GT(seg_ack, tcp->tcp_rnxt)) { 8815 /* 8816 * If the ICMP message is bogus, should we kill the 8817 * connection, or should we just drop the bogus ICMP 8818 * message? It would probably make more sense to just 8819 * drop the message so that if this one managed to get 8820 * in, the real connection should not suffer. 8821 */ 8822 goto noticmpv6; 8823 } 8824 8825 switch (icmp6->icmp6_type) { 8826 case ICMP6_PACKET_TOO_BIG: 8827 /* 8828 * Reduce the MSS based on the new MTU. This will 8829 * eliminate any fragmentation locally. 8830 * N.B. There may well be some funny side-effects on 8831 * the local send policy and the remote receive policy. 8832 * Pending further research, we provide 8833 * tcp_ignore_path_mtu just in case this proves 8834 * disastrous somewhere. 8835 * 8836 * After updating the MSS, retransmit part of the 8837 * dropped segment using the new mss by calling 8838 * tcp_wput_data(). Need to adjust all those 8839 * params to make sure tcp_wput_data() work properly. 8840 */ 8841 if (tcp_ignore_path_mtu) 8842 break; 8843 8844 /* 8845 * Decrease the MSS by time stamp options 8846 * IP options and IPSEC options. tcp_hdr_len 8847 * includes time stamp option and IP option 8848 * length. 8849 */ 8850 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8851 tcp->tcp_ipsec_overhead; 8852 8853 /* 8854 * Only update the MSS if the new one is 8855 * smaller than the previous one. This is 8856 * to avoid problems when getting multiple 8857 * ICMP errors for the same MTU. 8858 */ 8859 if (new_mss >= tcp->tcp_mss) 8860 break; 8861 8862 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8863 ASSERT(ratio >= 1); 8864 tcp_mss_set(tcp, new_mss); 8865 8866 /* 8867 * Make sure we have something to 8868 * send. 8869 */ 8870 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8871 (tcp->tcp_xmit_head != NULL)) { 8872 /* 8873 * Shrink tcp_cwnd in 8874 * proportion to the old MSS/new MSS. 8875 */ 8876 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8877 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8878 (tcp->tcp_unsent == 0)) { 8879 tcp->tcp_rexmit_max = tcp->tcp_fss; 8880 } else { 8881 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8882 } 8883 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8884 tcp->tcp_rexmit = B_TRUE; 8885 tcp->tcp_dupack_cnt = 0; 8886 tcp->tcp_snd_burst = TCP_CWND_SS; 8887 tcp_ss_rexmit(tcp); 8888 } 8889 break; 8890 8891 case ICMP6_DST_UNREACH: 8892 switch (icmp6->icmp6_code) { 8893 case ICMP6_DST_UNREACH_NOPORT: 8894 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8895 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8896 (seg_seq == tcp->tcp_iss)) { 8897 (void) tcp_clean_death(tcp, 8898 ECONNREFUSED, 8); 8899 } 8900 break; 8901 8902 case ICMP6_DST_UNREACH_ADMIN: 8903 case ICMP6_DST_UNREACH_NOROUTE: 8904 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8905 case ICMP6_DST_UNREACH_ADDR: 8906 /* Record the error in case we finally time out. */ 8907 tcp->tcp_client_errno = EHOSTUNREACH; 8908 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8909 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8910 (seg_seq == tcp->tcp_iss)) { 8911 if (tcp->tcp_listener != NULL && 8912 tcp->tcp_listener->tcp_syn_defense) { 8913 /* 8914 * Ditch the half-open connection if we 8915 * suspect a SYN attack is under way. 8916 */ 8917 tcp_ip_ire_mark_advice(tcp); 8918 (void) tcp_clean_death(tcp, 8919 tcp->tcp_client_errno, 9); 8920 } 8921 } 8922 8923 8924 break; 8925 default: 8926 break; 8927 } 8928 break; 8929 8930 case ICMP6_PARAM_PROB: 8931 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8932 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8933 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8934 (uchar_t *)nexthdrp) { 8935 if (tcp->tcp_state == TCPS_SYN_SENT || 8936 tcp->tcp_state == TCPS_SYN_RCVD) { 8937 (void) tcp_clean_death(tcp, 8938 ECONNREFUSED, 10); 8939 } 8940 break; 8941 } 8942 break; 8943 8944 case ICMP6_TIME_EXCEEDED: 8945 default: 8946 break; 8947 } 8948 freemsg(first_mp); 8949 } 8950 8951 /* 8952 * IP recognizes seven kinds of bind requests: 8953 * 8954 * - A zero-length address binds only to the protocol number. 8955 * 8956 * - A 4-byte address is treated as a request to 8957 * validate that the address is a valid local IPv4 8958 * address, appropriate for an application to bind to. 8959 * IP does the verification, but does not make any note 8960 * of the address at this time. 8961 * 8962 * - A 16-byte address contains is treated as a request 8963 * to validate a local IPv6 address, as the 4-byte 8964 * address case above. 8965 * 8966 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 8967 * use it for the inbound fanout of packets. 8968 * 8969 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 8970 * use it for the inbound fanout of packets. 8971 * 8972 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 8973 * information consisting of local and remote addresses 8974 * and ports. In this case, the addresses are both 8975 * validated as appropriate for this operation, and, if 8976 * so, the information is retained for use in the 8977 * inbound fanout. 8978 * 8979 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 8980 * fanout information, like the 12-byte case above. 8981 * 8982 * IP will also fill in the IRE request mblk with information 8983 * regarding our peer. In all cases, we notify IP of our protocol 8984 * type by appending a single protocol byte to the bind request. 8985 */ 8986 static mblk_t * 8987 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 8988 { 8989 char *cp; 8990 mblk_t *mp; 8991 struct T_bind_req *tbr; 8992 ipa_conn_t *ac; 8993 ipa6_conn_t *ac6; 8994 sin_t *sin; 8995 sin6_t *sin6; 8996 8997 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 8998 ASSERT((tcp->tcp_family == AF_INET && 8999 tcp->tcp_ipversion == IPV4_VERSION) || 9000 (tcp->tcp_family == AF_INET6 && 9001 (tcp->tcp_ipversion == IPV4_VERSION || 9002 tcp->tcp_ipversion == IPV6_VERSION))); 9003 9004 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9005 if (!mp) 9006 return (mp); 9007 mp->b_datap->db_type = M_PROTO; 9008 tbr = (struct T_bind_req *)mp->b_rptr; 9009 tbr->PRIM_type = bind_prim; 9010 tbr->ADDR_offset = sizeof (*tbr); 9011 tbr->CONIND_number = 0; 9012 tbr->ADDR_length = addr_length; 9013 cp = (char *)&tbr[1]; 9014 switch (addr_length) { 9015 case sizeof (ipa_conn_t): 9016 ASSERT(tcp->tcp_family == AF_INET); 9017 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9018 9019 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9020 if (mp->b_cont == NULL) { 9021 freemsg(mp); 9022 return (NULL); 9023 } 9024 mp->b_cont->b_wptr += sizeof (ire_t); 9025 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9026 9027 /* cp known to be 32 bit aligned */ 9028 ac = (ipa_conn_t *)cp; 9029 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9030 ac->ac_faddr = tcp->tcp_remote; 9031 ac->ac_fport = tcp->tcp_fport; 9032 ac->ac_lport = tcp->tcp_lport; 9033 tcp->tcp_hard_binding = 1; 9034 break; 9035 9036 case sizeof (ipa6_conn_t): 9037 ASSERT(tcp->tcp_family == AF_INET6); 9038 9039 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9040 if (mp->b_cont == NULL) { 9041 freemsg(mp); 9042 return (NULL); 9043 } 9044 mp->b_cont->b_wptr += sizeof (ire_t); 9045 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9046 9047 /* cp known to be 32 bit aligned */ 9048 ac6 = (ipa6_conn_t *)cp; 9049 if (tcp->tcp_ipversion == IPV4_VERSION) { 9050 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9051 &ac6->ac6_laddr); 9052 } else { 9053 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9054 } 9055 ac6->ac6_faddr = tcp->tcp_remote_v6; 9056 ac6->ac6_fport = tcp->tcp_fport; 9057 ac6->ac6_lport = tcp->tcp_lport; 9058 tcp->tcp_hard_binding = 1; 9059 break; 9060 9061 case sizeof (sin_t): 9062 /* 9063 * NOTE: IPV6_ADDR_LEN also has same size. 9064 * Use family to discriminate. 9065 */ 9066 if (tcp->tcp_family == AF_INET) { 9067 sin = (sin_t *)cp; 9068 9069 *sin = sin_null; 9070 sin->sin_family = AF_INET; 9071 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9072 sin->sin_port = tcp->tcp_lport; 9073 break; 9074 } else { 9075 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9076 } 9077 break; 9078 9079 case sizeof (sin6_t): 9080 ASSERT(tcp->tcp_family == AF_INET6); 9081 sin6 = (sin6_t *)cp; 9082 9083 *sin6 = sin6_null; 9084 sin6->sin6_family = AF_INET6; 9085 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9086 sin6->sin6_port = tcp->tcp_lport; 9087 break; 9088 9089 case IP_ADDR_LEN: 9090 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9091 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9092 break; 9093 9094 } 9095 /* Add protocol number to end */ 9096 cp[addr_length] = (char)IPPROTO_TCP; 9097 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9098 return (mp); 9099 } 9100 9101 /* 9102 * Notify IP that we are having trouble with this connection. IP should 9103 * blow the IRE away and start over. 9104 */ 9105 static void 9106 tcp_ip_notify(tcp_t *tcp) 9107 { 9108 struct iocblk *iocp; 9109 ipid_t *ipid; 9110 mblk_t *mp; 9111 9112 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9113 if (tcp->tcp_ipversion == IPV6_VERSION) 9114 return; 9115 9116 mp = mkiocb(IP_IOCTL); 9117 if (mp == NULL) 9118 return; 9119 9120 iocp = (struct iocblk *)mp->b_rptr; 9121 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9122 9123 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9124 if (!mp->b_cont) { 9125 freeb(mp); 9126 return; 9127 } 9128 9129 ipid = (ipid_t *)mp->b_cont->b_rptr; 9130 mp->b_cont->b_wptr += iocp->ioc_count; 9131 bzero(ipid, sizeof (*ipid)); 9132 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9133 ipid->ipid_ire_type = IRE_CACHE; 9134 ipid->ipid_addr_offset = sizeof (ipid_t); 9135 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9136 /* 9137 * Note: in the case of source routing we want to blow away the 9138 * route to the first source route hop. 9139 */ 9140 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9141 sizeof (tcp->tcp_ipha->ipha_dst)); 9142 9143 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9144 } 9145 9146 /* Unlink and return any mblk that looks like it contains an ire */ 9147 static mblk_t * 9148 tcp_ire_mp(mblk_t *mp) 9149 { 9150 mblk_t *prev_mp; 9151 9152 for (;;) { 9153 prev_mp = mp; 9154 mp = mp->b_cont; 9155 if (mp == NULL) 9156 break; 9157 switch (DB_TYPE(mp)) { 9158 case IRE_DB_TYPE: 9159 case IRE_DB_REQ_TYPE: 9160 if (prev_mp != NULL) 9161 prev_mp->b_cont = mp->b_cont; 9162 mp->b_cont = NULL; 9163 return (mp); 9164 default: 9165 break; 9166 } 9167 } 9168 return (mp); 9169 } 9170 9171 /* 9172 * Timer callback routine for keepalive probe. We do a fake resend of 9173 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9174 * check to see if we have heard anything from the other end for the last 9175 * RTO period. If we have, set the timer to expire for another 9176 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9177 * RTO << 1 and check again when it expires. Keep exponentially increasing 9178 * the timeout if we have not heard from the other side. If for more than 9179 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9180 * kill the connection unless the keepalive abort threshold is 0. In 9181 * that case, we will probe "forever." 9182 */ 9183 static void 9184 tcp_keepalive_killer(void *arg) 9185 { 9186 mblk_t *mp; 9187 conn_t *connp = (conn_t *)arg; 9188 tcp_t *tcp = connp->conn_tcp; 9189 int32_t firetime; 9190 int32_t idletime; 9191 int32_t ka_intrvl; 9192 9193 tcp->tcp_ka_tid = 0; 9194 9195 if (tcp->tcp_fused) 9196 return; 9197 9198 BUMP_MIB(&tcp_mib, tcpTimKeepalive); 9199 ka_intrvl = tcp->tcp_ka_interval; 9200 9201 /* 9202 * Keepalive probe should only be sent if the application has not 9203 * done a close on the connection. 9204 */ 9205 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9206 return; 9207 } 9208 /* Timer fired too early, restart it. */ 9209 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9210 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9211 MSEC_TO_TICK(ka_intrvl)); 9212 return; 9213 } 9214 9215 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9216 /* 9217 * If we have not heard from the other side for a long 9218 * time, kill the connection unless the keepalive abort 9219 * threshold is 0. In that case, we will probe "forever." 9220 */ 9221 if (tcp->tcp_ka_abort_thres != 0 && 9222 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9223 BUMP_MIB(&tcp_mib, tcpTimKeepaliveDrop); 9224 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9225 tcp->tcp_client_errno : ETIMEDOUT, 11); 9226 return; 9227 } 9228 9229 if (tcp->tcp_snxt == tcp->tcp_suna && 9230 idletime >= ka_intrvl) { 9231 /* Fake resend of last ACKed byte. */ 9232 mblk_t *mp1 = allocb(1, BPRI_LO); 9233 9234 if (mp1 != NULL) { 9235 *mp1->b_wptr++ = '\0'; 9236 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9237 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9238 freeb(mp1); 9239 /* 9240 * if allocation failed, fall through to start the 9241 * timer back. 9242 */ 9243 if (mp != NULL) { 9244 TCP_RECORD_TRACE(tcp, mp, 9245 TCP_TRACE_SEND_PKT); 9246 tcp_send_data(tcp, tcp->tcp_wq, mp); 9247 BUMP_MIB(&tcp_mib, tcpTimKeepaliveProbe); 9248 if (tcp->tcp_ka_last_intrvl != 0) { 9249 /* 9250 * We should probe again at least 9251 * in ka_intrvl, but not more than 9252 * tcp_rexmit_interval_max. 9253 */ 9254 firetime = MIN(ka_intrvl - 1, 9255 tcp->tcp_ka_last_intrvl << 1); 9256 if (firetime > tcp_rexmit_interval_max) 9257 firetime = 9258 tcp_rexmit_interval_max; 9259 } else { 9260 firetime = tcp->tcp_rto; 9261 } 9262 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9263 tcp_keepalive_killer, 9264 MSEC_TO_TICK(firetime)); 9265 tcp->tcp_ka_last_intrvl = firetime; 9266 return; 9267 } 9268 } 9269 } else { 9270 tcp->tcp_ka_last_intrvl = 0; 9271 } 9272 9273 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9274 if ((firetime = ka_intrvl - idletime) < 0) { 9275 firetime = ka_intrvl; 9276 } 9277 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9278 MSEC_TO_TICK(firetime)); 9279 } 9280 9281 int 9282 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9283 { 9284 queue_t *q = tcp->tcp_rq; 9285 int32_t mss = tcp->tcp_mss; 9286 int maxpsz; 9287 9288 if (TCP_IS_DETACHED(tcp)) 9289 return (mss); 9290 9291 if (tcp->tcp_fused) { 9292 maxpsz = tcp_fuse_maxpsz_set(tcp); 9293 mss = INFPSZ; 9294 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9295 /* 9296 * Set the sd_qn_maxpsz according to the socket send buffer 9297 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9298 * instruct the stream head to copyin user data into contiguous 9299 * kernel-allocated buffers without breaking it up into smaller 9300 * chunks. We round up the buffer size to the nearest SMSS. 9301 */ 9302 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9303 if (tcp->tcp_kssl_ctx == NULL) 9304 mss = INFPSZ; 9305 else 9306 mss = SSL3_MAX_RECORD_LEN; 9307 } else { 9308 /* 9309 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9310 * (and a multiple of the mss). This instructs the stream 9311 * head to break down larger than SMSS writes into SMSS- 9312 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9313 */ 9314 maxpsz = tcp->tcp_maxpsz * mss; 9315 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9316 maxpsz = tcp->tcp_xmit_hiwater/2; 9317 /* Round up to nearest mss */ 9318 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9319 } 9320 } 9321 (void) setmaxps(q, maxpsz); 9322 tcp->tcp_wq->q_maxpsz = maxpsz; 9323 9324 if (set_maxblk) 9325 (void) mi_set_sth_maxblk(q, mss); 9326 9327 return (mss); 9328 } 9329 9330 /* 9331 * Extract option values from a tcp header. We put any found values into the 9332 * tcpopt struct and return a bitmask saying which options were found. 9333 */ 9334 static int 9335 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9336 { 9337 uchar_t *endp; 9338 int len; 9339 uint32_t mss; 9340 uchar_t *up = (uchar_t *)tcph; 9341 int found = 0; 9342 int32_t sack_len; 9343 tcp_seq sack_begin, sack_end; 9344 tcp_t *tcp; 9345 9346 endp = up + TCP_HDR_LENGTH(tcph); 9347 up += TCP_MIN_HEADER_LENGTH; 9348 while (up < endp) { 9349 len = endp - up; 9350 switch (*up) { 9351 case TCPOPT_EOL: 9352 break; 9353 9354 case TCPOPT_NOP: 9355 up++; 9356 continue; 9357 9358 case TCPOPT_MAXSEG: 9359 if (len < TCPOPT_MAXSEG_LEN || 9360 up[1] != TCPOPT_MAXSEG_LEN) 9361 break; 9362 9363 mss = BE16_TO_U16(up+2); 9364 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9365 tcpopt->tcp_opt_mss = mss; 9366 found |= TCP_OPT_MSS_PRESENT; 9367 9368 up += TCPOPT_MAXSEG_LEN; 9369 continue; 9370 9371 case TCPOPT_WSCALE: 9372 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9373 break; 9374 9375 if (up[2] > TCP_MAX_WINSHIFT) 9376 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9377 else 9378 tcpopt->tcp_opt_wscale = up[2]; 9379 found |= TCP_OPT_WSCALE_PRESENT; 9380 9381 up += TCPOPT_WS_LEN; 9382 continue; 9383 9384 case TCPOPT_SACK_PERMITTED: 9385 if (len < TCPOPT_SACK_OK_LEN || 9386 up[1] != TCPOPT_SACK_OK_LEN) 9387 break; 9388 found |= TCP_OPT_SACK_OK_PRESENT; 9389 up += TCPOPT_SACK_OK_LEN; 9390 continue; 9391 9392 case TCPOPT_SACK: 9393 if (len <= 2 || up[1] <= 2 || len < up[1]) 9394 break; 9395 9396 /* If TCP is not interested in SACK blks... */ 9397 if ((tcp = tcpopt->tcp) == NULL) { 9398 up += up[1]; 9399 continue; 9400 } 9401 sack_len = up[1] - TCPOPT_HEADER_LEN; 9402 up += TCPOPT_HEADER_LEN; 9403 9404 /* 9405 * If the list is empty, allocate one and assume 9406 * nothing is sack'ed. 9407 */ 9408 ASSERT(tcp->tcp_sack_info != NULL); 9409 if (tcp->tcp_notsack_list == NULL) { 9410 tcp_notsack_update(&(tcp->tcp_notsack_list), 9411 tcp->tcp_suna, tcp->tcp_snxt, 9412 &(tcp->tcp_num_notsack_blk), 9413 &(tcp->tcp_cnt_notsack_list)); 9414 9415 /* 9416 * Make sure tcp_notsack_list is not NULL. 9417 * This happens when kmem_alloc(KM_NOSLEEP) 9418 * returns NULL. 9419 */ 9420 if (tcp->tcp_notsack_list == NULL) { 9421 up += sack_len; 9422 continue; 9423 } 9424 tcp->tcp_fack = tcp->tcp_suna; 9425 } 9426 9427 while (sack_len > 0) { 9428 if (up + 8 > endp) { 9429 up = endp; 9430 break; 9431 } 9432 sack_begin = BE32_TO_U32(up); 9433 up += 4; 9434 sack_end = BE32_TO_U32(up); 9435 up += 4; 9436 sack_len -= 8; 9437 /* 9438 * Bounds checking. Make sure the SACK 9439 * info is within tcp_suna and tcp_snxt. 9440 * If this SACK blk is out of bound, ignore 9441 * it but continue to parse the following 9442 * blks. 9443 */ 9444 if (SEQ_LEQ(sack_end, sack_begin) || 9445 SEQ_LT(sack_begin, tcp->tcp_suna) || 9446 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9447 continue; 9448 } 9449 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9450 sack_begin, sack_end, 9451 &(tcp->tcp_num_notsack_blk), 9452 &(tcp->tcp_cnt_notsack_list)); 9453 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9454 tcp->tcp_fack = sack_end; 9455 } 9456 } 9457 found |= TCP_OPT_SACK_PRESENT; 9458 continue; 9459 9460 case TCPOPT_TSTAMP: 9461 if (len < TCPOPT_TSTAMP_LEN || 9462 up[1] != TCPOPT_TSTAMP_LEN) 9463 break; 9464 9465 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9466 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9467 9468 found |= TCP_OPT_TSTAMP_PRESENT; 9469 9470 up += TCPOPT_TSTAMP_LEN; 9471 continue; 9472 9473 default: 9474 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9475 break; 9476 up += up[1]; 9477 continue; 9478 } 9479 break; 9480 } 9481 return (found); 9482 } 9483 9484 /* 9485 * Set the mss associated with a particular tcp based on its current value, 9486 * and a new one passed in. Observe minimums and maximums, and reset 9487 * other state variables that we want to view as multiples of mss. 9488 * 9489 * This function is called in various places mainly because 9490 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9491 * other side's SYN/SYN-ACK packet arrives. 9492 * 2) PMTUd may get us a new MSS. 9493 * 3) If the other side stops sending us timestamp option, we need to 9494 * increase the MSS size to use the extra bytes available. 9495 */ 9496 static void 9497 tcp_mss_set(tcp_t *tcp, uint32_t mss) 9498 { 9499 uint32_t mss_max; 9500 9501 if (tcp->tcp_ipversion == IPV4_VERSION) 9502 mss_max = tcp_mss_max_ipv4; 9503 else 9504 mss_max = tcp_mss_max_ipv6; 9505 9506 if (mss < tcp_mss_min) 9507 mss = tcp_mss_min; 9508 if (mss > mss_max) 9509 mss = mss_max; 9510 /* 9511 * Unless naglim has been set by our client to 9512 * a non-mss value, force naglim to track mss. 9513 * This can help to aggregate small writes. 9514 */ 9515 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9516 tcp->tcp_naglim = mss; 9517 /* 9518 * TCP should be able to buffer at least 4 MSS data for obvious 9519 * performance reason. 9520 */ 9521 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9522 tcp->tcp_xmit_hiwater = mss << 2; 9523 9524 /* 9525 * Check if we need to apply the tcp_init_cwnd here. If 9526 * it is set and the MSS gets bigger (should not happen 9527 * normally), we need to adjust the resulting tcp_cwnd properly. 9528 * The new tcp_cwnd should not get bigger. 9529 */ 9530 if (tcp->tcp_init_cwnd == 0) { 9531 tcp->tcp_cwnd = MIN(tcp_slow_start_initial * mss, 9532 MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9533 } else { 9534 if (tcp->tcp_mss < mss) { 9535 tcp->tcp_cwnd = MAX(1, 9536 (tcp->tcp_init_cwnd * tcp->tcp_mss / mss)) * mss; 9537 } else { 9538 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9539 } 9540 } 9541 tcp->tcp_mss = mss; 9542 tcp->tcp_cwnd_cnt = 0; 9543 (void) tcp_maxpsz_set(tcp, B_TRUE); 9544 } 9545 9546 static int 9547 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9548 { 9549 tcp_t *tcp = NULL; 9550 conn_t *connp; 9551 int err; 9552 dev_t conn_dev; 9553 zoneid_t zoneid = getzoneid(); 9554 9555 /* 9556 * Special case for install: miniroot needs to be able to access files 9557 * via NFS as though it were always in the global zone. 9558 */ 9559 if (credp == kcred && nfs_global_client_only != 0) 9560 zoneid = GLOBAL_ZONEID; 9561 9562 if (q->q_ptr != NULL) 9563 return (0); 9564 9565 if (sflag == MODOPEN) { 9566 /* 9567 * This is a special case. The purpose of a modopen 9568 * is to allow just the T_SVR4_OPTMGMT_REQ to pass 9569 * through for MIB browsers. Everything else is failed. 9570 */ 9571 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt)); 9572 9573 if (connp == NULL) 9574 return (ENOMEM); 9575 9576 connp->conn_flags |= IPCL_TCPMOD; 9577 connp->conn_cred = credp; 9578 connp->conn_zoneid = zoneid; 9579 q->q_ptr = WR(q)->q_ptr = connp; 9580 crhold(credp); 9581 q->q_qinfo = &tcp_mod_rinit; 9582 WR(q)->q_qinfo = &tcp_mod_winit; 9583 qprocson(q); 9584 return (0); 9585 } 9586 9587 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) 9588 return (EBUSY); 9589 9590 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9591 9592 if (flag & SO_ACCEPTOR) { 9593 q->q_qinfo = &tcp_acceptor_rinit; 9594 q->q_ptr = (void *)conn_dev; 9595 WR(q)->q_qinfo = &tcp_acceptor_winit; 9596 WR(q)->q_ptr = (void *)conn_dev; 9597 qprocson(q); 9598 return (0); 9599 } 9600 9601 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt)); 9602 if (connp == NULL) { 9603 inet_minor_free(ip_minor_arena, conn_dev); 9604 q->q_ptr = NULL; 9605 return (ENOSR); 9606 } 9607 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9608 tcp = connp->conn_tcp; 9609 9610 q->q_ptr = WR(q)->q_ptr = connp; 9611 if (getmajor(*devp) == TCP6_MAJ) { 9612 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9613 connp->conn_send = ip_output_v6; 9614 connp->conn_af_isv6 = B_TRUE; 9615 connp->conn_pkt_isv6 = B_TRUE; 9616 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9617 tcp->tcp_ipversion = IPV6_VERSION; 9618 tcp->tcp_family = AF_INET6; 9619 tcp->tcp_mss = tcp_mss_def_ipv6; 9620 } else { 9621 connp->conn_flags |= IPCL_TCP4; 9622 connp->conn_send = ip_output; 9623 connp->conn_af_isv6 = B_FALSE; 9624 connp->conn_pkt_isv6 = B_FALSE; 9625 tcp->tcp_ipversion = IPV4_VERSION; 9626 tcp->tcp_family = AF_INET; 9627 tcp->tcp_mss = tcp_mss_def_ipv4; 9628 } 9629 9630 /* 9631 * TCP keeps a copy of cred for cache locality reasons but 9632 * we put a reference only once. If connp->conn_cred 9633 * becomes invalid, tcp_cred should also be set to NULL. 9634 */ 9635 tcp->tcp_cred = connp->conn_cred = credp; 9636 crhold(connp->conn_cred); 9637 tcp->tcp_cpid = curproc->p_pid; 9638 connp->conn_zoneid = zoneid; 9639 connp->conn_mlp_type = mlptSingle; 9640 connp->conn_ulp_labeled = !is_system_labeled(); 9641 9642 /* 9643 * If the caller has the process-wide flag set, then default to MAC 9644 * exempt mode. This allows read-down to unlabeled hosts. 9645 */ 9646 if (getpflags(NET_MAC_AWARE, credp) != 0) 9647 connp->conn_mac_exempt = B_TRUE; 9648 9649 connp->conn_dev = conn_dev; 9650 9651 ASSERT(q->q_qinfo == &tcp_rinit); 9652 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9653 9654 if (flag & SO_SOCKSTR) { 9655 /* 9656 * No need to insert a socket in tcp acceptor hash. 9657 * If it was a socket acceptor stream, we dealt with 9658 * it above. A socket listener can never accept a 9659 * connection and doesn't need acceptor_id. 9660 */ 9661 connp->conn_flags |= IPCL_SOCKET; 9662 tcp->tcp_issocket = 1; 9663 WR(q)->q_qinfo = &tcp_sock_winit; 9664 } else { 9665 #ifdef _ILP32 9666 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9667 #else 9668 tcp->tcp_acceptor_id = conn_dev; 9669 #endif /* _ILP32 */ 9670 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9671 } 9672 9673 if (tcp_trace) 9674 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9675 9676 err = tcp_init(tcp, q); 9677 if (err != 0) { 9678 inet_minor_free(ip_minor_arena, connp->conn_dev); 9679 tcp_acceptor_hash_remove(tcp); 9680 CONN_DEC_REF(connp); 9681 q->q_ptr = WR(q)->q_ptr = NULL; 9682 return (err); 9683 } 9684 9685 RD(q)->q_hiwat = tcp_recv_hiwat; 9686 tcp->tcp_rwnd = tcp_recv_hiwat; 9687 9688 /* Non-zero default values */ 9689 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9690 /* 9691 * Put the ref for TCP. Ref for IP was already put 9692 * by ipcl_conn_create. Also Make the conn_t globally 9693 * visible to walkers 9694 */ 9695 mutex_enter(&connp->conn_lock); 9696 CONN_INC_REF_LOCKED(connp); 9697 ASSERT(connp->conn_ref == 2); 9698 connp->conn_state_flags &= ~CONN_INCIPIENT; 9699 mutex_exit(&connp->conn_lock); 9700 9701 qprocson(q); 9702 return (0); 9703 } 9704 9705 /* 9706 * Some TCP options can be "set" by requesting them in the option 9707 * buffer. This is needed for XTI feature test though we do not 9708 * allow it in general. We interpret that this mechanism is more 9709 * applicable to OSI protocols and need not be allowed in general. 9710 * This routine filters out options for which it is not allowed (most) 9711 * and lets through those (few) for which it is. [ The XTI interface 9712 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9713 * ever implemented will have to be allowed here ]. 9714 */ 9715 static boolean_t 9716 tcp_allow_connopt_set(int level, int name) 9717 { 9718 9719 switch (level) { 9720 case IPPROTO_TCP: 9721 switch (name) { 9722 case TCP_NODELAY: 9723 return (B_TRUE); 9724 default: 9725 return (B_FALSE); 9726 } 9727 /*NOTREACHED*/ 9728 default: 9729 return (B_FALSE); 9730 } 9731 /*NOTREACHED*/ 9732 } 9733 9734 /* 9735 * This routine gets default values of certain options whose default 9736 * values are maintained by protocol specific code 9737 */ 9738 /* ARGSUSED */ 9739 int 9740 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9741 { 9742 int32_t *i1 = (int32_t *)ptr; 9743 9744 switch (level) { 9745 case IPPROTO_TCP: 9746 switch (name) { 9747 case TCP_NOTIFY_THRESHOLD: 9748 *i1 = tcp_ip_notify_interval; 9749 break; 9750 case TCP_ABORT_THRESHOLD: 9751 *i1 = tcp_ip_abort_interval; 9752 break; 9753 case TCP_CONN_NOTIFY_THRESHOLD: 9754 *i1 = tcp_ip_notify_cinterval; 9755 break; 9756 case TCP_CONN_ABORT_THRESHOLD: 9757 *i1 = tcp_ip_abort_cinterval; 9758 break; 9759 default: 9760 return (-1); 9761 } 9762 break; 9763 case IPPROTO_IP: 9764 switch (name) { 9765 case IP_TTL: 9766 *i1 = tcp_ipv4_ttl; 9767 break; 9768 default: 9769 return (-1); 9770 } 9771 break; 9772 case IPPROTO_IPV6: 9773 switch (name) { 9774 case IPV6_UNICAST_HOPS: 9775 *i1 = tcp_ipv6_hoplimit; 9776 break; 9777 default: 9778 return (-1); 9779 } 9780 break; 9781 default: 9782 return (-1); 9783 } 9784 return (sizeof (int)); 9785 } 9786 9787 9788 /* 9789 * TCP routine to get the values of options. 9790 */ 9791 int 9792 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9793 { 9794 int *i1 = (int *)ptr; 9795 conn_t *connp = Q_TO_CONN(q); 9796 tcp_t *tcp = connp->conn_tcp; 9797 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9798 9799 switch (level) { 9800 case SOL_SOCKET: 9801 switch (name) { 9802 case SO_LINGER: { 9803 struct linger *lgr = (struct linger *)ptr; 9804 9805 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9806 lgr->l_linger = tcp->tcp_lingertime; 9807 } 9808 return (sizeof (struct linger)); 9809 case SO_DEBUG: 9810 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9811 break; 9812 case SO_KEEPALIVE: 9813 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9814 break; 9815 case SO_DONTROUTE: 9816 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9817 break; 9818 case SO_USELOOPBACK: 9819 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9820 break; 9821 case SO_BROADCAST: 9822 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9823 break; 9824 case SO_REUSEADDR: 9825 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9826 break; 9827 case SO_OOBINLINE: 9828 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9829 break; 9830 case SO_DGRAM_ERRIND: 9831 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9832 break; 9833 case SO_TYPE: 9834 *i1 = SOCK_STREAM; 9835 break; 9836 case SO_SNDBUF: 9837 *i1 = tcp->tcp_xmit_hiwater; 9838 break; 9839 case SO_RCVBUF: 9840 *i1 = RD(q)->q_hiwat; 9841 break; 9842 case SO_SND_COPYAVOID: 9843 *i1 = tcp->tcp_snd_zcopy_on ? 9844 SO_SND_COPYAVOID : 0; 9845 break; 9846 case SO_ALLZONES: 9847 *i1 = connp->conn_allzones ? 1 : 0; 9848 break; 9849 case SO_ANON_MLP: 9850 *i1 = connp->conn_anon_mlp; 9851 break; 9852 case SO_MAC_EXEMPT: 9853 *i1 = connp->conn_mac_exempt; 9854 break; 9855 case SO_EXCLBIND: 9856 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9857 break; 9858 default: 9859 return (-1); 9860 } 9861 break; 9862 case IPPROTO_TCP: 9863 switch (name) { 9864 case TCP_NODELAY: 9865 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9866 break; 9867 case TCP_MAXSEG: 9868 *i1 = tcp->tcp_mss; 9869 break; 9870 case TCP_NOTIFY_THRESHOLD: 9871 *i1 = (int)tcp->tcp_first_timer_threshold; 9872 break; 9873 case TCP_ABORT_THRESHOLD: 9874 *i1 = tcp->tcp_second_timer_threshold; 9875 break; 9876 case TCP_CONN_NOTIFY_THRESHOLD: 9877 *i1 = tcp->tcp_first_ctimer_threshold; 9878 break; 9879 case TCP_CONN_ABORT_THRESHOLD: 9880 *i1 = tcp->tcp_second_ctimer_threshold; 9881 break; 9882 case TCP_RECVDSTADDR: 9883 *i1 = tcp->tcp_recvdstaddr; 9884 break; 9885 case TCP_ANONPRIVBIND: 9886 *i1 = tcp->tcp_anon_priv_bind; 9887 break; 9888 case TCP_EXCLBIND: 9889 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9890 break; 9891 case TCP_INIT_CWND: 9892 *i1 = tcp->tcp_init_cwnd; 9893 break; 9894 case TCP_KEEPALIVE_THRESHOLD: 9895 *i1 = tcp->tcp_ka_interval; 9896 break; 9897 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9898 *i1 = tcp->tcp_ka_abort_thres; 9899 break; 9900 case TCP_CORK: 9901 *i1 = tcp->tcp_cork; 9902 break; 9903 default: 9904 return (-1); 9905 } 9906 break; 9907 case IPPROTO_IP: 9908 if (tcp->tcp_family != AF_INET) 9909 return (-1); 9910 switch (name) { 9911 case IP_OPTIONS: 9912 case T_IP_OPTIONS: { 9913 /* 9914 * This is compatible with BSD in that in only return 9915 * the reverse source route with the final destination 9916 * as the last entry. The first 4 bytes of the option 9917 * will contain the final destination. 9918 */ 9919 int opt_len; 9920 9921 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9922 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9923 ASSERT(opt_len >= 0); 9924 /* Caller ensures enough space */ 9925 if (opt_len > 0) { 9926 /* 9927 * TODO: Do we have to handle getsockopt on an 9928 * initiator as well? 9929 */ 9930 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9931 } 9932 return (0); 9933 } 9934 case IP_TOS: 9935 case T_IP_TOS: 9936 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9937 break; 9938 case IP_TTL: 9939 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9940 break; 9941 case IP_NEXTHOP: 9942 /* Handled at IP level */ 9943 return (-EINVAL); 9944 default: 9945 return (-1); 9946 } 9947 break; 9948 case IPPROTO_IPV6: 9949 /* 9950 * IPPROTO_IPV6 options are only supported for sockets 9951 * that are using IPv6 on the wire. 9952 */ 9953 if (tcp->tcp_ipversion != IPV6_VERSION) { 9954 return (-1); 9955 } 9956 switch (name) { 9957 case IPV6_UNICAST_HOPS: 9958 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9959 break; /* goto sizeof (int) option return */ 9960 case IPV6_BOUND_IF: 9961 /* Zero if not set */ 9962 *i1 = tcp->tcp_bound_if; 9963 break; /* goto sizeof (int) option return */ 9964 case IPV6_RECVPKTINFO: 9965 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9966 *i1 = 1; 9967 else 9968 *i1 = 0; 9969 break; /* goto sizeof (int) option return */ 9970 case IPV6_RECVTCLASS: 9971 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9972 *i1 = 1; 9973 else 9974 *i1 = 0; 9975 break; /* goto sizeof (int) option return */ 9976 case IPV6_RECVHOPLIMIT: 9977 if (tcp->tcp_ipv6_recvancillary & 9978 TCP_IPV6_RECVHOPLIMIT) 9979 *i1 = 1; 9980 else 9981 *i1 = 0; 9982 break; /* goto sizeof (int) option return */ 9983 case IPV6_RECVHOPOPTS: 9984 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9985 *i1 = 1; 9986 else 9987 *i1 = 0; 9988 break; /* goto sizeof (int) option return */ 9989 case IPV6_RECVDSTOPTS: 9990 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9991 *i1 = 1; 9992 else 9993 *i1 = 0; 9994 break; /* goto sizeof (int) option return */ 9995 case _OLD_IPV6_RECVDSTOPTS: 9996 if (tcp->tcp_ipv6_recvancillary & 9997 TCP_OLD_IPV6_RECVDSTOPTS) 9998 *i1 = 1; 9999 else 10000 *i1 = 0; 10001 break; /* goto sizeof (int) option return */ 10002 case IPV6_RECVRTHDR: 10003 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10004 *i1 = 1; 10005 else 10006 *i1 = 0; 10007 break; /* goto sizeof (int) option return */ 10008 case IPV6_RECVRTHDRDSTOPTS: 10009 if (tcp->tcp_ipv6_recvancillary & 10010 TCP_IPV6_RECVRTDSTOPTS) 10011 *i1 = 1; 10012 else 10013 *i1 = 0; 10014 break; /* goto sizeof (int) option return */ 10015 case IPV6_PKTINFO: { 10016 /* XXX assumes that caller has room for max size! */ 10017 struct in6_pktinfo *pkti; 10018 10019 pkti = (struct in6_pktinfo *)ptr; 10020 if (ipp->ipp_fields & IPPF_IFINDEX) 10021 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10022 else 10023 pkti->ipi6_ifindex = 0; 10024 if (ipp->ipp_fields & IPPF_ADDR) 10025 pkti->ipi6_addr = ipp->ipp_addr; 10026 else 10027 pkti->ipi6_addr = ipv6_all_zeros; 10028 return (sizeof (struct in6_pktinfo)); 10029 } 10030 case IPV6_TCLASS: 10031 if (ipp->ipp_fields & IPPF_TCLASS) 10032 *i1 = ipp->ipp_tclass; 10033 else 10034 *i1 = IPV6_FLOW_TCLASS( 10035 IPV6_DEFAULT_VERS_AND_FLOW); 10036 break; /* goto sizeof (int) option return */ 10037 case IPV6_NEXTHOP: { 10038 sin6_t *sin6 = (sin6_t *)ptr; 10039 10040 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10041 return (0); 10042 *sin6 = sin6_null; 10043 sin6->sin6_family = AF_INET6; 10044 sin6->sin6_addr = ipp->ipp_nexthop; 10045 return (sizeof (sin6_t)); 10046 } 10047 case IPV6_HOPOPTS: 10048 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10049 return (0); 10050 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10051 return (0); 10052 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10053 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10054 if (tcp->tcp_label_len > 0) { 10055 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10056 ptr[1] = (ipp->ipp_hopoptslen - 10057 tcp->tcp_label_len + 7) / 8 - 1; 10058 } 10059 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10060 case IPV6_RTHDRDSTOPTS: 10061 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10062 return (0); 10063 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10064 return (ipp->ipp_rtdstoptslen); 10065 case IPV6_RTHDR: 10066 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10067 return (0); 10068 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10069 return (ipp->ipp_rthdrlen); 10070 case IPV6_DSTOPTS: 10071 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10072 return (0); 10073 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10074 return (ipp->ipp_dstoptslen); 10075 case IPV6_SRC_PREFERENCES: 10076 return (ip6_get_src_preferences(connp, 10077 (uint32_t *)ptr)); 10078 case IPV6_PATHMTU: { 10079 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10080 10081 if (tcp->tcp_state < TCPS_ESTABLISHED) 10082 return (-1); 10083 10084 return (ip_fill_mtuinfo(&connp->conn_remv6, 10085 connp->conn_fport, mtuinfo)); 10086 } 10087 default: 10088 return (-1); 10089 } 10090 break; 10091 default: 10092 return (-1); 10093 } 10094 return (sizeof (int)); 10095 } 10096 10097 /* 10098 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10099 * Parameters are assumed to be verified by the caller. 10100 */ 10101 /* ARGSUSED */ 10102 int 10103 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10104 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10105 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10106 { 10107 conn_t *connp = Q_TO_CONN(q); 10108 tcp_t *tcp = connp->conn_tcp; 10109 int *i1 = (int *)invalp; 10110 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10111 boolean_t checkonly; 10112 int reterr; 10113 10114 switch (optset_context) { 10115 case SETFN_OPTCOM_CHECKONLY: 10116 checkonly = B_TRUE; 10117 /* 10118 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10119 * inlen != 0 implies value supplied and 10120 * we have to "pretend" to set it. 10121 * inlen == 0 implies that there is no 10122 * value part in T_CHECK request and just validation 10123 * done elsewhere should be enough, we just return here. 10124 */ 10125 if (inlen == 0) { 10126 *outlenp = 0; 10127 return (0); 10128 } 10129 break; 10130 case SETFN_OPTCOM_NEGOTIATE: 10131 checkonly = B_FALSE; 10132 break; 10133 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10134 case SETFN_CONN_NEGOTIATE: 10135 checkonly = B_FALSE; 10136 /* 10137 * Negotiating local and "association-related" options 10138 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10139 * primitives is allowed by XTI, but we choose 10140 * to not implement this style negotiation for Internet 10141 * protocols (We interpret it is a must for OSI world but 10142 * optional for Internet protocols) for all options. 10143 * [ Will do only for the few options that enable test 10144 * suites that our XTI implementation of this feature 10145 * works for transports that do allow it ] 10146 */ 10147 if (!tcp_allow_connopt_set(level, name)) { 10148 *outlenp = 0; 10149 return (EINVAL); 10150 } 10151 break; 10152 default: 10153 /* 10154 * We should never get here 10155 */ 10156 *outlenp = 0; 10157 return (EINVAL); 10158 } 10159 10160 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10161 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10162 10163 /* 10164 * For TCP, we should have no ancillary data sent down 10165 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10166 * has to be zero. 10167 */ 10168 ASSERT(thisdg_attrs == NULL); 10169 10170 /* 10171 * For fixed length options, no sanity check 10172 * of passed in length is done. It is assumed *_optcom_req() 10173 * routines do the right thing. 10174 */ 10175 10176 switch (level) { 10177 case SOL_SOCKET: 10178 switch (name) { 10179 case SO_LINGER: { 10180 struct linger *lgr = (struct linger *)invalp; 10181 10182 if (!checkonly) { 10183 if (lgr->l_onoff) { 10184 tcp->tcp_linger = 1; 10185 tcp->tcp_lingertime = lgr->l_linger; 10186 } else { 10187 tcp->tcp_linger = 0; 10188 tcp->tcp_lingertime = 0; 10189 } 10190 /* struct copy */ 10191 *(struct linger *)outvalp = *lgr; 10192 } else { 10193 if (!lgr->l_onoff) { 10194 ((struct linger *)outvalp)->l_onoff = 0; 10195 ((struct linger *)outvalp)->l_linger = 0; 10196 } else { 10197 /* struct copy */ 10198 *(struct linger *)outvalp = *lgr; 10199 } 10200 } 10201 *outlenp = sizeof (struct linger); 10202 return (0); 10203 } 10204 case SO_DEBUG: 10205 if (!checkonly) 10206 tcp->tcp_debug = onoff; 10207 break; 10208 case SO_KEEPALIVE: 10209 if (checkonly) { 10210 /* T_CHECK case */ 10211 break; 10212 } 10213 10214 if (!onoff) { 10215 if (tcp->tcp_ka_enabled) { 10216 if (tcp->tcp_ka_tid != 0) { 10217 (void) TCP_TIMER_CANCEL(tcp, 10218 tcp->tcp_ka_tid); 10219 tcp->tcp_ka_tid = 0; 10220 } 10221 tcp->tcp_ka_enabled = 0; 10222 } 10223 break; 10224 } 10225 if (!tcp->tcp_ka_enabled) { 10226 /* Crank up the keepalive timer */ 10227 tcp->tcp_ka_last_intrvl = 0; 10228 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10229 tcp_keepalive_killer, 10230 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10231 tcp->tcp_ka_enabled = 1; 10232 } 10233 break; 10234 case SO_DONTROUTE: 10235 /* 10236 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10237 * only of interest to IP. We track them here only so 10238 * that we can report their current value. 10239 */ 10240 if (!checkonly) { 10241 tcp->tcp_dontroute = onoff; 10242 tcp->tcp_connp->conn_dontroute = onoff; 10243 } 10244 break; 10245 case SO_USELOOPBACK: 10246 if (!checkonly) { 10247 tcp->tcp_useloopback = onoff; 10248 tcp->tcp_connp->conn_loopback = onoff; 10249 } 10250 break; 10251 case SO_BROADCAST: 10252 if (!checkonly) { 10253 tcp->tcp_broadcast = onoff; 10254 tcp->tcp_connp->conn_broadcast = onoff; 10255 } 10256 break; 10257 case SO_REUSEADDR: 10258 if (!checkonly) { 10259 tcp->tcp_reuseaddr = onoff; 10260 tcp->tcp_connp->conn_reuseaddr = onoff; 10261 } 10262 break; 10263 case SO_OOBINLINE: 10264 if (!checkonly) 10265 tcp->tcp_oobinline = onoff; 10266 break; 10267 case SO_DGRAM_ERRIND: 10268 if (!checkonly) 10269 tcp->tcp_dgram_errind = onoff; 10270 break; 10271 case SO_SNDBUF: { 10272 tcp_t *peer_tcp; 10273 10274 if (*i1 > tcp_max_buf) { 10275 *outlenp = 0; 10276 return (ENOBUFS); 10277 } 10278 if (checkonly) 10279 break; 10280 10281 tcp->tcp_xmit_hiwater = *i1; 10282 if (tcp_snd_lowat_fraction != 0) 10283 tcp->tcp_xmit_lowater = 10284 tcp->tcp_xmit_hiwater / 10285 tcp_snd_lowat_fraction; 10286 (void) tcp_maxpsz_set(tcp, B_TRUE); 10287 /* 10288 * If we are flow-controlled, recheck the condition. 10289 * There are apps that increase SO_SNDBUF size when 10290 * flow-controlled (EWOULDBLOCK), and expect the flow 10291 * control condition to be lifted right away. 10292 * 10293 * For the fused tcp loopback case, in order to avoid 10294 * a race with the peer's tcp_fuse_rrw() we need to 10295 * hold its fuse_lock while accessing tcp_flow_stopped. 10296 */ 10297 peer_tcp = tcp->tcp_loopback_peer; 10298 ASSERT(!tcp->tcp_fused || peer_tcp != NULL); 10299 if (tcp->tcp_fused) 10300 mutex_enter(&peer_tcp->tcp_fuse_lock); 10301 10302 if (tcp->tcp_flow_stopped && 10303 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10304 tcp_clrqfull(tcp); 10305 } 10306 if (tcp->tcp_fused) 10307 mutex_exit(&peer_tcp->tcp_fuse_lock); 10308 break; 10309 } 10310 case SO_RCVBUF: 10311 if (*i1 > tcp_max_buf) { 10312 *outlenp = 0; 10313 return (ENOBUFS); 10314 } 10315 /* Silently ignore zero */ 10316 if (!checkonly && *i1 != 0) { 10317 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10318 (void) tcp_rwnd_set(tcp, *i1); 10319 } 10320 /* 10321 * XXX should we return the rwnd here 10322 * and tcp_opt_get ? 10323 */ 10324 break; 10325 case SO_SND_COPYAVOID: 10326 if (!checkonly) { 10327 /* we only allow enable at most once for now */ 10328 if (tcp->tcp_loopback || 10329 (!tcp->tcp_snd_zcopy_aware && 10330 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10331 *outlenp = 0; 10332 return (EOPNOTSUPP); 10333 } 10334 tcp->tcp_snd_zcopy_aware = 1; 10335 } 10336 break; 10337 case SO_ALLZONES: 10338 /* Handled at the IP level */ 10339 return (-EINVAL); 10340 case SO_ANON_MLP: 10341 if (!checkonly) { 10342 mutex_enter(&connp->conn_lock); 10343 connp->conn_anon_mlp = onoff; 10344 mutex_exit(&connp->conn_lock); 10345 } 10346 break; 10347 case SO_MAC_EXEMPT: 10348 if (secpolicy_net_mac_aware(cr) != 0 || 10349 IPCL_IS_BOUND(connp)) 10350 return (EACCES); 10351 if (!checkonly) { 10352 mutex_enter(&connp->conn_lock); 10353 connp->conn_mac_exempt = onoff; 10354 mutex_exit(&connp->conn_lock); 10355 } 10356 break; 10357 case SO_EXCLBIND: 10358 if (!checkonly) 10359 tcp->tcp_exclbind = onoff; 10360 break; 10361 default: 10362 *outlenp = 0; 10363 return (EINVAL); 10364 } 10365 break; 10366 case IPPROTO_TCP: 10367 switch (name) { 10368 case TCP_NODELAY: 10369 if (!checkonly) 10370 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10371 break; 10372 case TCP_NOTIFY_THRESHOLD: 10373 if (!checkonly) 10374 tcp->tcp_first_timer_threshold = *i1; 10375 break; 10376 case TCP_ABORT_THRESHOLD: 10377 if (!checkonly) 10378 tcp->tcp_second_timer_threshold = *i1; 10379 break; 10380 case TCP_CONN_NOTIFY_THRESHOLD: 10381 if (!checkonly) 10382 tcp->tcp_first_ctimer_threshold = *i1; 10383 break; 10384 case TCP_CONN_ABORT_THRESHOLD: 10385 if (!checkonly) 10386 tcp->tcp_second_ctimer_threshold = *i1; 10387 break; 10388 case TCP_RECVDSTADDR: 10389 if (tcp->tcp_state > TCPS_LISTEN) 10390 return (EOPNOTSUPP); 10391 if (!checkonly) 10392 tcp->tcp_recvdstaddr = onoff; 10393 break; 10394 case TCP_ANONPRIVBIND: 10395 if ((reterr = secpolicy_net_privaddr(cr, 0)) != 0) { 10396 *outlenp = 0; 10397 return (reterr); 10398 } 10399 if (!checkonly) { 10400 tcp->tcp_anon_priv_bind = onoff; 10401 } 10402 break; 10403 case TCP_EXCLBIND: 10404 if (!checkonly) 10405 tcp->tcp_exclbind = onoff; 10406 break; /* goto sizeof (int) option return */ 10407 case TCP_INIT_CWND: { 10408 uint32_t init_cwnd = *((uint32_t *)invalp); 10409 10410 if (checkonly) 10411 break; 10412 10413 /* 10414 * Only allow socket with network configuration 10415 * privilege to set the initial cwnd to be larger 10416 * than allowed by RFC 3390. 10417 */ 10418 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10419 tcp->tcp_init_cwnd = init_cwnd; 10420 break; 10421 } 10422 if ((reterr = secpolicy_net_config(cr, B_TRUE)) != 0) { 10423 *outlenp = 0; 10424 return (reterr); 10425 } 10426 if (init_cwnd > TCP_MAX_INIT_CWND) { 10427 *outlenp = 0; 10428 return (EINVAL); 10429 } 10430 tcp->tcp_init_cwnd = init_cwnd; 10431 break; 10432 } 10433 case TCP_KEEPALIVE_THRESHOLD: 10434 if (checkonly) 10435 break; 10436 10437 if (*i1 < tcp_keepalive_interval_low || 10438 *i1 > tcp_keepalive_interval_high) { 10439 *outlenp = 0; 10440 return (EINVAL); 10441 } 10442 if (*i1 != tcp->tcp_ka_interval) { 10443 tcp->tcp_ka_interval = *i1; 10444 /* 10445 * Check if we need to restart the 10446 * keepalive timer. 10447 */ 10448 if (tcp->tcp_ka_tid != 0) { 10449 ASSERT(tcp->tcp_ka_enabled); 10450 (void) TCP_TIMER_CANCEL(tcp, 10451 tcp->tcp_ka_tid); 10452 tcp->tcp_ka_last_intrvl = 0; 10453 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10454 tcp_keepalive_killer, 10455 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10456 } 10457 } 10458 break; 10459 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10460 if (!checkonly) { 10461 if (*i1 < tcp_keepalive_abort_interval_low || 10462 *i1 > tcp_keepalive_abort_interval_high) { 10463 *outlenp = 0; 10464 return (EINVAL); 10465 } 10466 tcp->tcp_ka_abort_thres = *i1; 10467 } 10468 break; 10469 case TCP_CORK: 10470 if (!checkonly) { 10471 /* 10472 * if tcp->tcp_cork was set and is now 10473 * being unset, we have to make sure that 10474 * the remaining data gets sent out. Also 10475 * unset tcp->tcp_cork so that tcp_wput_data() 10476 * can send data even if it is less than mss 10477 */ 10478 if (tcp->tcp_cork && onoff == 0 && 10479 tcp->tcp_unsent > 0) { 10480 tcp->tcp_cork = B_FALSE; 10481 tcp_wput_data(tcp, NULL, B_FALSE); 10482 } 10483 tcp->tcp_cork = onoff; 10484 } 10485 break; 10486 default: 10487 *outlenp = 0; 10488 return (EINVAL); 10489 } 10490 break; 10491 case IPPROTO_IP: 10492 if (tcp->tcp_family != AF_INET) { 10493 *outlenp = 0; 10494 return (ENOPROTOOPT); 10495 } 10496 switch (name) { 10497 case IP_OPTIONS: 10498 case T_IP_OPTIONS: 10499 reterr = tcp_opt_set_header(tcp, checkonly, 10500 invalp, inlen); 10501 if (reterr) { 10502 *outlenp = 0; 10503 return (reterr); 10504 } 10505 /* OK return - copy input buffer into output buffer */ 10506 if (invalp != outvalp) { 10507 /* don't trust bcopy for identical src/dst */ 10508 bcopy(invalp, outvalp, inlen); 10509 } 10510 *outlenp = inlen; 10511 return (0); 10512 case IP_TOS: 10513 case T_IP_TOS: 10514 if (!checkonly) { 10515 tcp->tcp_ipha->ipha_type_of_service = 10516 (uchar_t)*i1; 10517 tcp->tcp_tos = (uchar_t)*i1; 10518 } 10519 break; 10520 case IP_TTL: 10521 if (!checkonly) { 10522 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10523 tcp->tcp_ttl = (uchar_t)*i1; 10524 } 10525 break; 10526 case IP_BOUND_IF: 10527 case IP_NEXTHOP: 10528 /* Handled at the IP level */ 10529 return (-EINVAL); 10530 case IP_SEC_OPT: 10531 /* 10532 * We should not allow policy setting after 10533 * we start listening for connections. 10534 */ 10535 if (tcp->tcp_state == TCPS_LISTEN) { 10536 return (EINVAL); 10537 } else { 10538 /* Handled at the IP level */ 10539 return (-EINVAL); 10540 } 10541 default: 10542 *outlenp = 0; 10543 return (EINVAL); 10544 } 10545 break; 10546 case IPPROTO_IPV6: { 10547 ip6_pkt_t *ipp; 10548 10549 /* 10550 * IPPROTO_IPV6 options are only supported for sockets 10551 * that are using IPv6 on the wire. 10552 */ 10553 if (tcp->tcp_ipversion != IPV6_VERSION) { 10554 *outlenp = 0; 10555 return (ENOPROTOOPT); 10556 } 10557 /* 10558 * Only sticky options; no ancillary data 10559 */ 10560 ASSERT(thisdg_attrs == NULL); 10561 ipp = &tcp->tcp_sticky_ipp; 10562 10563 switch (name) { 10564 case IPV6_UNICAST_HOPS: 10565 /* -1 means use default */ 10566 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10567 *outlenp = 0; 10568 return (EINVAL); 10569 } 10570 if (!checkonly) { 10571 if (*i1 == -1) { 10572 tcp->tcp_ip6h->ip6_hops = 10573 ipp->ipp_unicast_hops = 10574 (uint8_t)tcp_ipv6_hoplimit; 10575 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10576 /* Pass modified value to IP. */ 10577 *i1 = tcp->tcp_ip6h->ip6_hops; 10578 } else { 10579 tcp->tcp_ip6h->ip6_hops = 10580 ipp->ipp_unicast_hops = 10581 (uint8_t)*i1; 10582 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10583 } 10584 reterr = tcp_build_hdrs(q, tcp); 10585 if (reterr != 0) 10586 return (reterr); 10587 } 10588 break; 10589 case IPV6_BOUND_IF: 10590 if (!checkonly) { 10591 int error = 0; 10592 10593 tcp->tcp_bound_if = *i1; 10594 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10595 B_TRUE, checkonly, level, name, mblk); 10596 if (error != 0) { 10597 *outlenp = 0; 10598 return (error); 10599 } 10600 } 10601 break; 10602 /* 10603 * Set boolean switches for ancillary data delivery 10604 */ 10605 case IPV6_RECVPKTINFO: 10606 if (!checkonly) { 10607 if (onoff) 10608 tcp->tcp_ipv6_recvancillary |= 10609 TCP_IPV6_RECVPKTINFO; 10610 else 10611 tcp->tcp_ipv6_recvancillary &= 10612 ~TCP_IPV6_RECVPKTINFO; 10613 /* Force it to be sent up with the next msg */ 10614 tcp->tcp_recvifindex = 0; 10615 } 10616 break; 10617 case IPV6_RECVTCLASS: 10618 if (!checkonly) { 10619 if (onoff) 10620 tcp->tcp_ipv6_recvancillary |= 10621 TCP_IPV6_RECVTCLASS; 10622 else 10623 tcp->tcp_ipv6_recvancillary &= 10624 ~TCP_IPV6_RECVTCLASS; 10625 } 10626 break; 10627 case IPV6_RECVHOPLIMIT: 10628 if (!checkonly) { 10629 if (onoff) 10630 tcp->tcp_ipv6_recvancillary |= 10631 TCP_IPV6_RECVHOPLIMIT; 10632 else 10633 tcp->tcp_ipv6_recvancillary &= 10634 ~TCP_IPV6_RECVHOPLIMIT; 10635 /* Force it to be sent up with the next msg */ 10636 tcp->tcp_recvhops = 0xffffffffU; 10637 } 10638 break; 10639 case IPV6_RECVHOPOPTS: 10640 if (!checkonly) { 10641 if (onoff) 10642 tcp->tcp_ipv6_recvancillary |= 10643 TCP_IPV6_RECVHOPOPTS; 10644 else 10645 tcp->tcp_ipv6_recvancillary &= 10646 ~TCP_IPV6_RECVHOPOPTS; 10647 } 10648 break; 10649 case IPV6_RECVDSTOPTS: 10650 if (!checkonly) { 10651 if (onoff) 10652 tcp->tcp_ipv6_recvancillary |= 10653 TCP_IPV6_RECVDSTOPTS; 10654 else 10655 tcp->tcp_ipv6_recvancillary &= 10656 ~TCP_IPV6_RECVDSTOPTS; 10657 } 10658 break; 10659 case _OLD_IPV6_RECVDSTOPTS: 10660 if (!checkonly) { 10661 if (onoff) 10662 tcp->tcp_ipv6_recvancillary |= 10663 TCP_OLD_IPV6_RECVDSTOPTS; 10664 else 10665 tcp->tcp_ipv6_recvancillary &= 10666 ~TCP_OLD_IPV6_RECVDSTOPTS; 10667 } 10668 break; 10669 case IPV6_RECVRTHDR: 10670 if (!checkonly) { 10671 if (onoff) 10672 tcp->tcp_ipv6_recvancillary |= 10673 TCP_IPV6_RECVRTHDR; 10674 else 10675 tcp->tcp_ipv6_recvancillary &= 10676 ~TCP_IPV6_RECVRTHDR; 10677 } 10678 break; 10679 case IPV6_RECVRTHDRDSTOPTS: 10680 if (!checkonly) { 10681 if (onoff) 10682 tcp->tcp_ipv6_recvancillary |= 10683 TCP_IPV6_RECVRTDSTOPTS; 10684 else 10685 tcp->tcp_ipv6_recvancillary &= 10686 ~TCP_IPV6_RECVRTDSTOPTS; 10687 } 10688 break; 10689 case IPV6_PKTINFO: 10690 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10691 return (EINVAL); 10692 if (checkonly) 10693 break; 10694 10695 if (inlen == 0) { 10696 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10697 } else { 10698 struct in6_pktinfo *pkti; 10699 10700 pkti = (struct in6_pktinfo *)invalp; 10701 /* 10702 * RFC 3542 states that ipi6_addr must be 10703 * the unspecified address when setting the 10704 * IPV6_PKTINFO sticky socket option on a 10705 * TCP socket. 10706 */ 10707 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10708 return (EINVAL); 10709 /* 10710 * ip6_set_pktinfo() validates the source 10711 * address and interface index. 10712 */ 10713 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10714 pkti, mblk); 10715 if (reterr != 0) 10716 return (reterr); 10717 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10718 ipp->ipp_addr = pkti->ipi6_addr; 10719 if (ipp->ipp_ifindex != 0) 10720 ipp->ipp_fields |= IPPF_IFINDEX; 10721 else 10722 ipp->ipp_fields &= ~IPPF_IFINDEX; 10723 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10724 ipp->ipp_fields |= IPPF_ADDR; 10725 else 10726 ipp->ipp_fields &= ~IPPF_ADDR; 10727 } 10728 reterr = tcp_build_hdrs(q, tcp); 10729 if (reterr != 0) 10730 return (reterr); 10731 break; 10732 case IPV6_TCLASS: 10733 if (inlen != 0 && inlen != sizeof (int)) 10734 return (EINVAL); 10735 if (checkonly) 10736 break; 10737 10738 if (inlen == 0) { 10739 ipp->ipp_fields &= ~IPPF_TCLASS; 10740 } else { 10741 if (*i1 > 255 || *i1 < -1) 10742 return (EINVAL); 10743 if (*i1 == -1) { 10744 ipp->ipp_tclass = 0; 10745 *i1 = 0; 10746 } else { 10747 ipp->ipp_tclass = *i1; 10748 } 10749 ipp->ipp_fields |= IPPF_TCLASS; 10750 } 10751 reterr = tcp_build_hdrs(q, tcp); 10752 if (reterr != 0) 10753 return (reterr); 10754 break; 10755 case IPV6_NEXTHOP: 10756 /* 10757 * IP will verify that the nexthop is reachable 10758 * and fail for sticky options. 10759 */ 10760 if (inlen != 0 && inlen != sizeof (sin6_t)) 10761 return (EINVAL); 10762 if (checkonly) 10763 break; 10764 10765 if (inlen == 0) { 10766 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10767 } else { 10768 sin6_t *sin6 = (sin6_t *)invalp; 10769 10770 if (sin6->sin6_family != AF_INET6) 10771 return (EAFNOSUPPORT); 10772 if (IN6_IS_ADDR_V4MAPPED( 10773 &sin6->sin6_addr)) 10774 return (EADDRNOTAVAIL); 10775 ipp->ipp_nexthop = sin6->sin6_addr; 10776 if (!IN6_IS_ADDR_UNSPECIFIED( 10777 &ipp->ipp_nexthop)) 10778 ipp->ipp_fields |= IPPF_NEXTHOP; 10779 else 10780 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10781 } 10782 reterr = tcp_build_hdrs(q, tcp); 10783 if (reterr != 0) 10784 return (reterr); 10785 break; 10786 case IPV6_HOPOPTS: { 10787 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10788 10789 /* 10790 * Sanity checks - minimum size, size a multiple of 10791 * eight bytes, and matching size passed in. 10792 */ 10793 if (inlen != 0 && 10794 inlen != (8 * (hopts->ip6h_len + 1))) 10795 return (EINVAL); 10796 10797 if (checkonly) 10798 break; 10799 10800 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10801 (uchar_t **)&ipp->ipp_hopopts, 10802 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10803 if (reterr != 0) 10804 return (reterr); 10805 if (ipp->ipp_hopoptslen == 0) 10806 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10807 else 10808 ipp->ipp_fields |= IPPF_HOPOPTS; 10809 reterr = tcp_build_hdrs(q, tcp); 10810 if (reterr != 0) 10811 return (reterr); 10812 break; 10813 } 10814 case IPV6_RTHDRDSTOPTS: { 10815 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10816 10817 /* 10818 * Sanity checks - minimum size, size a multiple of 10819 * eight bytes, and matching size passed in. 10820 */ 10821 if (inlen != 0 && 10822 inlen != (8 * (dopts->ip6d_len + 1))) 10823 return (EINVAL); 10824 10825 if (checkonly) 10826 break; 10827 10828 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10829 (uchar_t **)&ipp->ipp_rtdstopts, 10830 &ipp->ipp_rtdstoptslen, 0); 10831 if (reterr != 0) 10832 return (reterr); 10833 if (ipp->ipp_rtdstoptslen == 0) 10834 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10835 else 10836 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10837 reterr = tcp_build_hdrs(q, tcp); 10838 if (reterr != 0) 10839 return (reterr); 10840 break; 10841 } 10842 case IPV6_DSTOPTS: { 10843 ip6_dest_t *dopts = (ip6_dest_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 * (dopts->ip6d_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_dstopts, 10858 &ipp->ipp_dstoptslen, 0); 10859 if (reterr != 0) 10860 return (reterr); 10861 if (ipp->ipp_dstoptslen == 0) 10862 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10863 else 10864 ipp->ipp_fields |= IPPF_DSTOPTS; 10865 reterr = tcp_build_hdrs(q, tcp); 10866 if (reterr != 0) 10867 return (reterr); 10868 break; 10869 } 10870 case IPV6_RTHDR: { 10871 ip6_rthdr_t *rt = (ip6_rthdr_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 * (rt->ip6r_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_rthdr, 10886 &ipp->ipp_rthdrlen, 0); 10887 if (reterr != 0) 10888 return (reterr); 10889 if (ipp->ipp_rthdrlen == 0) 10890 ipp->ipp_fields &= ~IPPF_RTHDR; 10891 else 10892 ipp->ipp_fields |= IPPF_RTHDR; 10893 reterr = tcp_build_hdrs(q, tcp); 10894 if (reterr != 0) 10895 return (reterr); 10896 break; 10897 } 10898 case IPV6_V6ONLY: 10899 if (!checkonly) 10900 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10901 break; 10902 case IPV6_USE_MIN_MTU: 10903 if (inlen != sizeof (int)) 10904 return (EINVAL); 10905 10906 if (*i1 < -1 || *i1 > 1) 10907 return (EINVAL); 10908 10909 if (checkonly) 10910 break; 10911 10912 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10913 ipp->ipp_use_min_mtu = *i1; 10914 break; 10915 case IPV6_BOUND_PIF: 10916 /* Handled at the IP level */ 10917 return (-EINVAL); 10918 case IPV6_SEC_OPT: 10919 /* 10920 * We should not allow policy setting after 10921 * we start listening for connections. 10922 */ 10923 if (tcp->tcp_state == TCPS_LISTEN) { 10924 return (EINVAL); 10925 } else { 10926 /* Handled at the IP level */ 10927 return (-EINVAL); 10928 } 10929 case IPV6_SRC_PREFERENCES: 10930 if (inlen != sizeof (uint32_t)) 10931 return (EINVAL); 10932 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10933 *(uint32_t *)invalp); 10934 if (reterr != 0) { 10935 *outlenp = 0; 10936 return (reterr); 10937 } 10938 break; 10939 default: 10940 *outlenp = 0; 10941 return (EINVAL); 10942 } 10943 break; 10944 } /* end IPPROTO_IPV6 */ 10945 default: 10946 *outlenp = 0; 10947 return (EINVAL); 10948 } 10949 /* 10950 * Common case of OK return with outval same as inval 10951 */ 10952 if (invalp != outvalp) { 10953 /* don't trust bcopy for identical src/dst */ 10954 (void) bcopy(invalp, outvalp, inlen); 10955 } 10956 *outlenp = inlen; 10957 return (0); 10958 } 10959 10960 /* 10961 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10962 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10963 * headers, and the maximum size tcp header (to avoid reallocation 10964 * on the fly for additional tcp options). 10965 * Returns failure if can't allocate memory. 10966 */ 10967 static int 10968 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 10969 { 10970 char *hdrs; 10971 uint_t hdrs_len; 10972 ip6i_t *ip6i; 10973 char buf[TCP_MAX_HDR_LENGTH]; 10974 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10975 in6_addr_t src, dst; 10976 10977 /* 10978 * save the existing tcp header and source/dest IP addresses 10979 */ 10980 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10981 src = tcp->tcp_ip6h->ip6_src; 10982 dst = tcp->tcp_ip6h->ip6_dst; 10983 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10984 ASSERT(hdrs_len != 0); 10985 if (hdrs_len > tcp->tcp_iphc_len) { 10986 /* Need to reallocate */ 10987 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10988 if (hdrs == NULL) 10989 return (ENOMEM); 10990 if (tcp->tcp_iphc != NULL) { 10991 if (tcp->tcp_hdr_grown) { 10992 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10993 } else { 10994 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10995 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10996 } 10997 tcp->tcp_iphc_len = 0; 10998 } 10999 ASSERT(tcp->tcp_iphc_len == 0); 11000 tcp->tcp_iphc = hdrs; 11001 tcp->tcp_iphc_len = hdrs_len; 11002 tcp->tcp_hdr_grown = B_TRUE; 11003 } 11004 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11005 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11006 11007 /* Set header fields not in ipp */ 11008 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11009 ip6i = (ip6i_t *)tcp->tcp_iphc; 11010 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11011 } else { 11012 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11013 } 11014 /* 11015 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11016 * 11017 * tcp->tcp_tcp_hdr_len doesn't change here. 11018 */ 11019 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11020 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11021 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11022 11023 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11024 11025 tcp->tcp_ip6h->ip6_src = src; 11026 tcp->tcp_ip6h->ip6_dst = dst; 11027 11028 /* 11029 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11030 * the default value for TCP. 11031 */ 11032 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11033 tcp->tcp_ip6h->ip6_hops = tcp_ipv6_hoplimit; 11034 11035 /* 11036 * If we're setting extension headers after a connection 11037 * has been established, and if we have a routing header 11038 * among the extension headers, call ip_massage_options_v6 to 11039 * manipulate the routing header/ip6_dst set the checksum 11040 * difference in the tcp header template. 11041 * (This happens in tcp_connect_ipv6 if the routing header 11042 * is set prior to the connect.) 11043 * Set the tcp_sum to zero first in case we've cleared a 11044 * routing header or don't have one at all. 11045 */ 11046 tcp->tcp_sum = 0; 11047 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11048 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11049 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11050 (uint8_t *)tcp->tcp_tcph); 11051 if (rth != NULL) { 11052 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11053 rth); 11054 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11055 (tcp->tcp_sum >> 16)); 11056 } 11057 } 11058 11059 /* Try to get everything in a single mblk */ 11060 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcp_wroff_xtra); 11061 return (0); 11062 } 11063 11064 /* 11065 * Transfer any source route option from ipha to buf/dst in reversed form. 11066 */ 11067 static int 11068 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11069 { 11070 ipoptp_t opts; 11071 uchar_t *opt; 11072 uint8_t optval; 11073 uint8_t optlen; 11074 uint32_t len = 0; 11075 11076 for (optval = ipoptp_first(&opts, ipha); 11077 optval != IPOPT_EOL; 11078 optval = ipoptp_next(&opts)) { 11079 opt = opts.ipoptp_cur; 11080 optlen = opts.ipoptp_len; 11081 switch (optval) { 11082 int off1, off2; 11083 case IPOPT_SSRR: 11084 case IPOPT_LSRR: 11085 11086 /* Reverse source route */ 11087 /* 11088 * First entry should be the next to last one in the 11089 * current source route (the last entry is our 11090 * address.) 11091 * The last entry should be the final destination. 11092 */ 11093 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11094 buf[IPOPT_OLEN] = (uint8_t)optlen; 11095 off1 = IPOPT_MINOFF_SR - 1; 11096 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11097 if (off2 < 0) { 11098 /* No entries in source route */ 11099 break; 11100 } 11101 bcopy(opt + off2, dst, IP_ADDR_LEN); 11102 /* 11103 * Note: use src since ipha has not had its src 11104 * and dst reversed (it is in the state it was 11105 * received. 11106 */ 11107 bcopy(&ipha->ipha_src, buf + off2, 11108 IP_ADDR_LEN); 11109 off2 -= IP_ADDR_LEN; 11110 11111 while (off2 > 0) { 11112 bcopy(opt + off2, buf + off1, 11113 IP_ADDR_LEN); 11114 off1 += IP_ADDR_LEN; 11115 off2 -= IP_ADDR_LEN; 11116 } 11117 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11118 buf += optlen; 11119 len += optlen; 11120 break; 11121 } 11122 } 11123 done: 11124 /* Pad the resulting options */ 11125 while (len & 0x3) { 11126 *buf++ = IPOPT_EOL; 11127 len++; 11128 } 11129 return (len); 11130 } 11131 11132 11133 /* 11134 * Extract and revert a source route from ipha (if any) 11135 * and then update the relevant fields in both tcp_t and the standard header. 11136 */ 11137 static void 11138 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11139 { 11140 char buf[TCP_MAX_HDR_LENGTH]; 11141 uint_t tcph_len; 11142 int len; 11143 11144 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11145 len = IPH_HDR_LENGTH(ipha); 11146 if (len == IP_SIMPLE_HDR_LENGTH) 11147 /* Nothing to do */ 11148 return; 11149 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11150 (len & 0x3)) 11151 return; 11152 11153 tcph_len = tcp->tcp_tcp_hdr_len; 11154 bcopy(tcp->tcp_tcph, buf, tcph_len); 11155 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11156 (tcp->tcp_ipha->ipha_dst & 0xffff); 11157 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11158 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11159 len += IP_SIMPLE_HDR_LENGTH; 11160 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11161 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11162 if ((int)tcp->tcp_sum < 0) 11163 tcp->tcp_sum--; 11164 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11165 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11166 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11167 bcopy(buf, tcp->tcp_tcph, tcph_len); 11168 tcp->tcp_ip_hdr_len = len; 11169 tcp->tcp_ipha->ipha_version_and_hdr_length = 11170 (IP_VERSION << 4) | (len >> 2); 11171 len += tcph_len; 11172 tcp->tcp_hdr_len = len; 11173 } 11174 11175 /* 11176 * Copy the standard header into its new location, 11177 * lay in the new options and then update the relevant 11178 * fields in both tcp_t and the standard header. 11179 */ 11180 static int 11181 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11182 { 11183 uint_t tcph_len; 11184 uint8_t *ip_optp; 11185 tcph_t *new_tcph; 11186 11187 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11188 return (EINVAL); 11189 11190 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11191 return (EINVAL); 11192 11193 if (checkonly) { 11194 /* 11195 * do not really set, just pretend to - T_CHECK 11196 */ 11197 return (0); 11198 } 11199 11200 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11201 if (tcp->tcp_label_len > 0) { 11202 int padlen; 11203 uint8_t opt; 11204 11205 /* convert list termination to no-ops */ 11206 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11207 ip_optp += ip_optp[IPOPT_OLEN]; 11208 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11209 while (--padlen >= 0) 11210 *ip_optp++ = opt; 11211 } 11212 tcph_len = tcp->tcp_tcp_hdr_len; 11213 new_tcph = (tcph_t *)(ip_optp + len); 11214 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11215 tcp->tcp_tcph = new_tcph; 11216 bcopy(ptr, ip_optp, len); 11217 11218 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11219 11220 tcp->tcp_ip_hdr_len = len; 11221 tcp->tcp_ipha->ipha_version_and_hdr_length = 11222 (IP_VERSION << 4) | (len >> 2); 11223 tcp->tcp_hdr_len = len + tcph_len; 11224 if (!TCP_IS_DETACHED(tcp)) { 11225 /* Always allocate room for all options. */ 11226 (void) mi_set_sth_wroff(tcp->tcp_rq, 11227 TCP_MAX_COMBINED_HEADER_LENGTH + tcp_wroff_xtra); 11228 } 11229 return (0); 11230 } 11231 11232 /* Get callback routine passed to nd_load by tcp_param_register */ 11233 /* ARGSUSED */ 11234 static int 11235 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11236 { 11237 tcpparam_t *tcppa = (tcpparam_t *)cp; 11238 11239 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11240 return (0); 11241 } 11242 11243 /* 11244 * Walk through the param array specified registering each element with the 11245 * named dispatch handler. 11246 */ 11247 static boolean_t 11248 tcp_param_register(tcpparam_t *tcppa, int cnt) 11249 { 11250 for (; cnt-- > 0; tcppa++) { 11251 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11252 if (!nd_load(&tcp_g_nd, tcppa->tcp_param_name, 11253 tcp_param_get, tcp_param_set, 11254 (caddr_t)tcppa)) { 11255 nd_free(&tcp_g_nd); 11256 return (B_FALSE); 11257 } 11258 } 11259 } 11260 if (!nd_load(&tcp_g_nd, tcp_wroff_xtra_param.tcp_param_name, 11261 tcp_param_get, tcp_param_set_aligned, 11262 (caddr_t)&tcp_wroff_xtra_param)) { 11263 nd_free(&tcp_g_nd); 11264 return (B_FALSE); 11265 } 11266 if (!nd_load(&tcp_g_nd, tcp_mdt_head_param.tcp_param_name, 11267 tcp_param_get, tcp_param_set_aligned, 11268 (caddr_t)&tcp_mdt_head_param)) { 11269 nd_free(&tcp_g_nd); 11270 return (B_FALSE); 11271 } 11272 if (!nd_load(&tcp_g_nd, tcp_mdt_tail_param.tcp_param_name, 11273 tcp_param_get, tcp_param_set_aligned, 11274 (caddr_t)&tcp_mdt_tail_param)) { 11275 nd_free(&tcp_g_nd); 11276 return (B_FALSE); 11277 } 11278 if (!nd_load(&tcp_g_nd, tcp_mdt_max_pbufs_param.tcp_param_name, 11279 tcp_param_get, tcp_param_set, 11280 (caddr_t)&tcp_mdt_max_pbufs_param)) { 11281 nd_free(&tcp_g_nd); 11282 return (B_FALSE); 11283 } 11284 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports", 11285 tcp_extra_priv_ports_get, NULL, NULL)) { 11286 nd_free(&tcp_g_nd); 11287 return (B_FALSE); 11288 } 11289 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports_add", 11290 NULL, tcp_extra_priv_ports_add, NULL)) { 11291 nd_free(&tcp_g_nd); 11292 return (B_FALSE); 11293 } 11294 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports_del", 11295 NULL, tcp_extra_priv_ports_del, NULL)) { 11296 nd_free(&tcp_g_nd); 11297 return (B_FALSE); 11298 } 11299 if (!nd_load(&tcp_g_nd, "tcp_status", tcp_status_report, NULL, 11300 NULL)) { 11301 nd_free(&tcp_g_nd); 11302 return (B_FALSE); 11303 } 11304 if (!nd_load(&tcp_g_nd, "tcp_bind_hash", tcp_bind_hash_report, 11305 NULL, NULL)) { 11306 nd_free(&tcp_g_nd); 11307 return (B_FALSE); 11308 } 11309 if (!nd_load(&tcp_g_nd, "tcp_listen_hash", tcp_listen_hash_report, 11310 NULL, NULL)) { 11311 nd_free(&tcp_g_nd); 11312 return (B_FALSE); 11313 } 11314 if (!nd_load(&tcp_g_nd, "tcp_conn_hash", tcp_conn_hash_report, 11315 NULL, NULL)) { 11316 nd_free(&tcp_g_nd); 11317 return (B_FALSE); 11318 } 11319 if (!nd_load(&tcp_g_nd, "tcp_acceptor_hash", tcp_acceptor_hash_report, 11320 NULL, NULL)) { 11321 nd_free(&tcp_g_nd); 11322 return (B_FALSE); 11323 } 11324 if (!nd_load(&tcp_g_nd, "tcp_host_param", tcp_host_param_report, 11325 tcp_host_param_set, NULL)) { 11326 nd_free(&tcp_g_nd); 11327 return (B_FALSE); 11328 } 11329 if (!nd_load(&tcp_g_nd, "tcp_host_param_ipv6", tcp_host_param_report, 11330 tcp_host_param_set_ipv6, NULL)) { 11331 nd_free(&tcp_g_nd); 11332 return (B_FALSE); 11333 } 11334 if (!nd_load(&tcp_g_nd, "tcp_1948_phrase", NULL, tcp_1948_phrase_set, 11335 NULL)) { 11336 nd_free(&tcp_g_nd); 11337 return (B_FALSE); 11338 } 11339 if (!nd_load(&tcp_g_nd, "tcp_reserved_port_list", 11340 tcp_reserved_port_list, NULL, NULL)) { 11341 nd_free(&tcp_g_nd); 11342 return (B_FALSE); 11343 } 11344 /* 11345 * Dummy ndd variables - only to convey obsolescence information 11346 * through printing of their name (no get or set routines) 11347 * XXX Remove in future releases ? 11348 */ 11349 if (!nd_load(&tcp_g_nd, 11350 "tcp_close_wait_interval(obsoleted - " 11351 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11352 nd_free(&tcp_g_nd); 11353 return (B_FALSE); 11354 } 11355 return (B_TRUE); 11356 } 11357 11358 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11359 /* ARGSUSED */ 11360 static int 11361 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11362 cred_t *cr) 11363 { 11364 long new_value; 11365 tcpparam_t *tcppa = (tcpparam_t *)cp; 11366 11367 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11368 new_value < tcppa->tcp_param_min || 11369 new_value > tcppa->tcp_param_max) { 11370 return (EINVAL); 11371 } 11372 /* 11373 * Need to make sure new_value is a multiple of 4. If it is not, 11374 * round it up. For future 64 bit requirement, we actually make it 11375 * a multiple of 8. 11376 */ 11377 if (new_value & 0x7) { 11378 new_value = (new_value & ~0x7) + 0x8; 11379 } 11380 tcppa->tcp_param_val = new_value; 11381 return (0); 11382 } 11383 11384 /* Set callback routine passed to nd_load by tcp_param_register */ 11385 /* ARGSUSED */ 11386 static int 11387 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11388 { 11389 long new_value; 11390 tcpparam_t *tcppa = (tcpparam_t *)cp; 11391 11392 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11393 new_value < tcppa->tcp_param_min || 11394 new_value > tcppa->tcp_param_max) { 11395 return (EINVAL); 11396 } 11397 tcppa->tcp_param_val = new_value; 11398 return (0); 11399 } 11400 11401 /* 11402 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11403 * is filled, return as much as we can. The message passed in may be 11404 * multi-part, chained using b_cont. "start" is the starting sequence 11405 * number for this piece. 11406 */ 11407 static mblk_t * 11408 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11409 { 11410 uint32_t end; 11411 mblk_t *mp1; 11412 mblk_t *mp2; 11413 mblk_t *next_mp; 11414 uint32_t u1; 11415 11416 /* Walk through all the new pieces. */ 11417 do { 11418 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11419 (uintptr_t)INT_MAX); 11420 end = start + (int)(mp->b_wptr - mp->b_rptr); 11421 next_mp = mp->b_cont; 11422 if (start == end) { 11423 /* Empty. Blast it. */ 11424 freeb(mp); 11425 continue; 11426 } 11427 mp->b_cont = NULL; 11428 TCP_REASS_SET_SEQ(mp, start); 11429 TCP_REASS_SET_END(mp, end); 11430 mp1 = tcp->tcp_reass_tail; 11431 if (!mp1) { 11432 tcp->tcp_reass_tail = mp; 11433 tcp->tcp_reass_head = mp; 11434 BUMP_MIB(&tcp_mib, tcpInDataUnorderSegs); 11435 UPDATE_MIB(&tcp_mib, 11436 tcpInDataUnorderBytes, end - start); 11437 continue; 11438 } 11439 /* New stuff completely beyond tail? */ 11440 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11441 /* Link it on end. */ 11442 mp1->b_cont = mp; 11443 tcp->tcp_reass_tail = mp; 11444 BUMP_MIB(&tcp_mib, tcpInDataUnorderSegs); 11445 UPDATE_MIB(&tcp_mib, 11446 tcpInDataUnorderBytes, end - start); 11447 continue; 11448 } 11449 mp1 = tcp->tcp_reass_head; 11450 u1 = TCP_REASS_SEQ(mp1); 11451 /* New stuff at the front? */ 11452 if (SEQ_LT(start, u1)) { 11453 /* Yes... Check for overlap. */ 11454 mp->b_cont = mp1; 11455 tcp->tcp_reass_head = mp; 11456 tcp_reass_elim_overlap(tcp, mp); 11457 continue; 11458 } 11459 /* 11460 * The new piece fits somewhere between the head and tail. 11461 * We find our slot, where mp1 precedes us and mp2 trails. 11462 */ 11463 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11464 u1 = TCP_REASS_SEQ(mp2); 11465 if (SEQ_LEQ(start, u1)) 11466 break; 11467 } 11468 /* Link ourselves in */ 11469 mp->b_cont = mp2; 11470 mp1->b_cont = mp; 11471 11472 /* Trim overlap with following mblk(s) first */ 11473 tcp_reass_elim_overlap(tcp, mp); 11474 11475 /* Trim overlap with preceding mblk */ 11476 tcp_reass_elim_overlap(tcp, mp1); 11477 11478 } while (start = end, mp = next_mp); 11479 mp1 = tcp->tcp_reass_head; 11480 /* Anything ready to go? */ 11481 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11482 return (NULL); 11483 /* Eat what we can off the queue */ 11484 for (;;) { 11485 mp = mp1->b_cont; 11486 end = TCP_REASS_END(mp1); 11487 TCP_REASS_SET_SEQ(mp1, 0); 11488 TCP_REASS_SET_END(mp1, 0); 11489 if (!mp) { 11490 tcp->tcp_reass_tail = NULL; 11491 break; 11492 } 11493 if (end != TCP_REASS_SEQ(mp)) { 11494 mp1->b_cont = NULL; 11495 break; 11496 } 11497 mp1 = mp; 11498 } 11499 mp1 = tcp->tcp_reass_head; 11500 tcp->tcp_reass_head = mp; 11501 return (mp1); 11502 } 11503 11504 /* Eliminate any overlap that mp may have over later mblks */ 11505 static void 11506 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11507 { 11508 uint32_t end; 11509 mblk_t *mp1; 11510 uint32_t u1; 11511 11512 end = TCP_REASS_END(mp); 11513 while ((mp1 = mp->b_cont) != NULL) { 11514 u1 = TCP_REASS_SEQ(mp1); 11515 if (!SEQ_GT(end, u1)) 11516 break; 11517 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11518 mp->b_wptr -= end - u1; 11519 TCP_REASS_SET_END(mp, u1); 11520 BUMP_MIB(&tcp_mib, tcpInDataPartDupSegs); 11521 UPDATE_MIB(&tcp_mib, tcpInDataPartDupBytes, end - u1); 11522 break; 11523 } 11524 mp->b_cont = mp1->b_cont; 11525 TCP_REASS_SET_SEQ(mp1, 0); 11526 TCP_REASS_SET_END(mp1, 0); 11527 freeb(mp1); 11528 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 11529 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, end - u1); 11530 } 11531 if (!mp1) 11532 tcp->tcp_reass_tail = mp; 11533 } 11534 11535 /* 11536 * Send up all messages queued on tcp_rcv_list. 11537 */ 11538 static uint_t 11539 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11540 { 11541 mblk_t *mp; 11542 uint_t ret = 0; 11543 uint_t thwin; 11544 #ifdef DEBUG 11545 uint_t cnt = 0; 11546 #endif 11547 /* Can't drain on an eager connection */ 11548 if (tcp->tcp_listener != NULL) 11549 return (ret); 11550 11551 /* 11552 * Handle two cases here: we are currently fused or we were 11553 * previously fused and have some urgent data to be delivered 11554 * upstream. The latter happens because we either ran out of 11555 * memory or were detached and therefore sending the SIGURG was 11556 * deferred until this point. In either case we pass control 11557 * over to tcp_fuse_rcv_drain() since it may need to complete 11558 * some work. 11559 */ 11560 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11561 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11562 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11563 &tcp->tcp_fused_sigurg_mp)) 11564 return (ret); 11565 } 11566 11567 while ((mp = tcp->tcp_rcv_list) != NULL) { 11568 tcp->tcp_rcv_list = mp->b_next; 11569 mp->b_next = NULL; 11570 #ifdef DEBUG 11571 cnt += msgdsize(mp); 11572 #endif 11573 /* Does this need SSL processing first? */ 11574 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11575 tcp_kssl_input(tcp, mp); 11576 continue; 11577 } 11578 putnext(q, mp); 11579 } 11580 ASSERT(cnt == tcp->tcp_rcv_cnt); 11581 tcp->tcp_rcv_last_head = NULL; 11582 tcp->tcp_rcv_last_tail = NULL; 11583 tcp->tcp_rcv_cnt = 0; 11584 11585 /* Learn the latest rwnd information that we sent to the other side. */ 11586 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11587 << tcp->tcp_rcv_ws; 11588 /* This is peer's calculated send window (our receive window). */ 11589 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11590 /* 11591 * Increase the receive window to max. But we need to do receiver 11592 * SWS avoidance. This means that we need to check the increase of 11593 * of receive window is at least 1 MSS. 11594 */ 11595 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11596 /* 11597 * If the window that the other side knows is less than max 11598 * deferred acks segments, send an update immediately. 11599 */ 11600 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11601 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 11602 ret = TH_ACK_NEEDED; 11603 } 11604 tcp->tcp_rwnd = q->q_hiwat; 11605 } 11606 /* No need for the push timer now. */ 11607 if (tcp->tcp_push_tid != 0) { 11608 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11609 tcp->tcp_push_tid = 0; 11610 } 11611 return (ret); 11612 } 11613 11614 /* 11615 * Queue data on tcp_rcv_list which is a b_next chain. 11616 * tcp_rcv_last_head/tail is the last element of this chain. 11617 * Each element of the chain is a b_cont chain. 11618 * 11619 * M_DATA messages are added to the current element. 11620 * Other messages are added as new (b_next) elements. 11621 */ 11622 void 11623 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11624 { 11625 ASSERT(seg_len == msgdsize(mp)); 11626 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11627 11628 if (tcp->tcp_rcv_list == NULL) { 11629 ASSERT(tcp->tcp_rcv_last_head == NULL); 11630 tcp->tcp_rcv_list = mp; 11631 tcp->tcp_rcv_last_head = mp; 11632 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11633 tcp->tcp_rcv_last_tail->b_cont = mp; 11634 } else { 11635 tcp->tcp_rcv_last_head->b_next = mp; 11636 tcp->tcp_rcv_last_head = mp; 11637 } 11638 11639 while (mp->b_cont) 11640 mp = mp->b_cont; 11641 11642 tcp->tcp_rcv_last_tail = mp; 11643 tcp->tcp_rcv_cnt += seg_len; 11644 tcp->tcp_rwnd -= seg_len; 11645 } 11646 11647 /* 11648 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11649 * 11650 * This is the default entry function into TCP on the read side. TCP is 11651 * always entered via squeue i.e. using squeue's for mutual exclusion. 11652 * When classifier does a lookup to find the tcp, it also puts a reference 11653 * on the conn structure associated so the tcp is guaranteed to exist 11654 * when we come here. We still need to check the state because it might 11655 * as well has been closed. The squeue processing function i.e. squeue_enter, 11656 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 11657 * CONN_DEC_REF. 11658 * 11659 * Apart from the default entry point, IP also sends packets directly to 11660 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11661 * connections. 11662 */ 11663 void 11664 tcp_input(void *arg, mblk_t *mp, void *arg2) 11665 { 11666 conn_t *connp = (conn_t *)arg; 11667 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11668 11669 /* arg2 is the sqp */ 11670 ASSERT(arg2 != NULL); 11671 ASSERT(mp != NULL); 11672 11673 /* 11674 * Don't accept any input on a closed tcp as this TCP logically does 11675 * not exist on the system. Don't proceed further with this TCP. 11676 * For eg. this packet could trigger another close of this tcp 11677 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11678 * tcp_clean_death / tcp_closei_local must be called at most once 11679 * on a TCP. In this case we need to refeed the packet into the 11680 * classifier and figure out where the packet should go. Need to 11681 * preserve the recv_ill somehow. Until we figure that out, for 11682 * now just drop the packet if we can't classify the packet. 11683 */ 11684 if (tcp->tcp_state == TCPS_CLOSED || 11685 tcp->tcp_state == TCPS_BOUND) { 11686 conn_t *new_connp; 11687 11688 new_connp = ipcl_classify(mp, connp->conn_zoneid); 11689 if (new_connp != NULL) { 11690 tcp_reinput(new_connp, mp, arg2); 11691 return; 11692 } 11693 /* We failed to classify. For now just drop the packet */ 11694 freemsg(mp); 11695 return; 11696 } 11697 11698 if (DB_TYPE(mp) == M_DATA) 11699 tcp_rput_data(connp, mp, arg2); 11700 else 11701 tcp_rput_common(tcp, mp); 11702 } 11703 11704 /* 11705 * The read side put procedure. 11706 * The packets passed up by ip are assume to be aligned according to 11707 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11708 */ 11709 static void 11710 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11711 { 11712 /* 11713 * tcp_rput_data() does not expect M_CTL except for the case 11714 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11715 * type. Need to make sure that any other M_CTLs don't make 11716 * it to tcp_rput_data since it is not expecting any and doesn't 11717 * check for it. 11718 */ 11719 if (DB_TYPE(mp) == M_CTL) { 11720 switch (*(uint32_t *)(mp->b_rptr)) { 11721 case TCP_IOC_ABORT_CONN: 11722 /* 11723 * Handle connection abort request. 11724 */ 11725 tcp_ioctl_abort_handler(tcp, mp); 11726 return; 11727 case IPSEC_IN: 11728 /* 11729 * Only secure icmp arrive in TCP and they 11730 * don't go through data path. 11731 */ 11732 tcp_icmp_error(tcp, mp); 11733 return; 11734 case IN_PKTINFO: 11735 /* 11736 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11737 * sockets that are receiving IPv4 traffic. tcp 11738 */ 11739 ASSERT(tcp->tcp_family == AF_INET6); 11740 ASSERT(tcp->tcp_ipv6_recvancillary & 11741 TCP_IPV6_RECVPKTINFO); 11742 tcp_rput_data(tcp->tcp_connp, mp, 11743 tcp->tcp_connp->conn_sqp); 11744 return; 11745 case MDT_IOC_INFO_UPDATE: 11746 /* 11747 * Handle Multidata information update; the 11748 * following routine will free the message. 11749 */ 11750 if (tcp->tcp_connp->conn_mdt_ok) { 11751 tcp_mdt_update(tcp, 11752 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11753 B_FALSE); 11754 } 11755 freemsg(mp); 11756 return; 11757 case LSO_IOC_INFO_UPDATE: 11758 /* 11759 * Handle LSO information update; the following 11760 * routine will free the message. 11761 */ 11762 if (tcp->tcp_connp->conn_lso_ok) { 11763 tcp_lso_update(tcp, 11764 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11765 } 11766 freemsg(mp); 11767 return; 11768 default: 11769 break; 11770 } 11771 } 11772 11773 /* No point processing the message if tcp is already closed */ 11774 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11775 freemsg(mp); 11776 return; 11777 } 11778 11779 tcp_rput_other(tcp, mp); 11780 } 11781 11782 11783 /* The minimum of smoothed mean deviation in RTO calculation. */ 11784 #define TCP_SD_MIN 400 11785 11786 /* 11787 * Set RTO for this connection. The formula is from Jacobson and Karels' 11788 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11789 * are the same as those in Appendix A.2 of that paper. 11790 * 11791 * m = new measurement 11792 * sa = smoothed RTT average (8 * average estimates). 11793 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11794 */ 11795 static void 11796 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11797 { 11798 long m = TICK_TO_MSEC(rtt); 11799 clock_t sa = tcp->tcp_rtt_sa; 11800 clock_t sv = tcp->tcp_rtt_sd; 11801 clock_t rto; 11802 11803 BUMP_MIB(&tcp_mib, tcpRttUpdate); 11804 tcp->tcp_rtt_update++; 11805 11806 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11807 if (sa != 0) { 11808 /* 11809 * Update average estimator: 11810 * new rtt = 7/8 old rtt + 1/8 Error 11811 */ 11812 11813 /* m is now Error in estimate. */ 11814 m -= sa >> 3; 11815 if ((sa += m) <= 0) { 11816 /* 11817 * Don't allow the smoothed average to be negative. 11818 * We use 0 to denote reinitialization of the 11819 * variables. 11820 */ 11821 sa = 1; 11822 } 11823 11824 /* 11825 * Update deviation estimator: 11826 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11827 */ 11828 if (m < 0) 11829 m = -m; 11830 m -= sv >> 2; 11831 sv += m; 11832 } else { 11833 /* 11834 * This follows BSD's implementation. So the reinitialized 11835 * RTO is 3 * m. We cannot go less than 2 because if the 11836 * link is bandwidth dominated, doubling the window size 11837 * during slow start means doubling the RTT. We want to be 11838 * more conservative when we reinitialize our estimates. 3 11839 * is just a convenient number. 11840 */ 11841 sa = m << 3; 11842 sv = m << 1; 11843 } 11844 if (sv < TCP_SD_MIN) { 11845 /* 11846 * We do not know that if sa captures the delay ACK 11847 * effect as in a long train of segments, a receiver 11848 * does not delay its ACKs. So set the minimum of sv 11849 * to be TCP_SD_MIN, which is default to 400 ms, twice 11850 * of BSD DATO. That means the minimum of mean 11851 * deviation is 100 ms. 11852 * 11853 */ 11854 sv = TCP_SD_MIN; 11855 } 11856 tcp->tcp_rtt_sa = sa; 11857 tcp->tcp_rtt_sd = sv; 11858 /* 11859 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11860 * 11861 * Add tcp_rexmit_interval extra in case of extreme environment 11862 * where the algorithm fails to work. The default value of 11863 * tcp_rexmit_interval_extra should be 0. 11864 * 11865 * As we use a finer grained clock than BSD and update 11866 * RTO for every ACKs, add in another .25 of RTT to the 11867 * deviation of RTO to accomodate burstiness of 1/4 of 11868 * window size. 11869 */ 11870 rto = (sa >> 3) + sv + tcp_rexmit_interval_extra + (sa >> 5); 11871 11872 if (rto > tcp_rexmit_interval_max) { 11873 tcp->tcp_rto = tcp_rexmit_interval_max; 11874 } else if (rto < tcp_rexmit_interval_min) { 11875 tcp->tcp_rto = tcp_rexmit_interval_min; 11876 } else { 11877 tcp->tcp_rto = rto; 11878 } 11879 11880 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11881 tcp->tcp_timer_backoff = 0; 11882 } 11883 11884 /* 11885 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11886 * send queue which starts at the given seq. no. 11887 * 11888 * Parameters: 11889 * tcp_t *tcp: the tcp instance pointer. 11890 * uint32_t seq: the starting seq. no of the requested segment. 11891 * int32_t *off: after the execution, *off will be the offset to 11892 * the returned mblk which points to the requested seq no. 11893 * It is the caller's responsibility to send in a non-null off. 11894 * 11895 * Return: 11896 * A mblk_t pointer pointing to the requested segment in send queue. 11897 */ 11898 static mblk_t * 11899 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11900 { 11901 int32_t cnt; 11902 mblk_t *mp; 11903 11904 /* Defensive coding. Make sure we don't send incorrect data. */ 11905 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 11906 return (NULL); 11907 11908 cnt = seq - tcp->tcp_suna; 11909 mp = tcp->tcp_xmit_head; 11910 while (cnt > 0 && mp != NULL) { 11911 cnt -= mp->b_wptr - mp->b_rptr; 11912 if (cnt < 0) { 11913 cnt += mp->b_wptr - mp->b_rptr; 11914 break; 11915 } 11916 mp = mp->b_cont; 11917 } 11918 ASSERT(mp != NULL); 11919 *off = cnt; 11920 return (mp); 11921 } 11922 11923 /* 11924 * This function handles all retransmissions if SACK is enabled for this 11925 * connection. First it calculates how many segments can be retransmitted 11926 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11927 * segments. A segment is eligible if sack_cnt for that segment is greater 11928 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11929 * all eligible segments, it checks to see if TCP can send some new segments 11930 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11931 * 11932 * Parameters: 11933 * tcp_t *tcp: the tcp structure of the connection. 11934 * uint_t *flags: in return, appropriate value will be set for 11935 * tcp_rput_data(). 11936 */ 11937 static void 11938 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11939 { 11940 notsack_blk_t *notsack_blk; 11941 int32_t usable_swnd; 11942 int32_t mss; 11943 uint32_t seg_len; 11944 mblk_t *xmit_mp; 11945 11946 ASSERT(tcp->tcp_sack_info != NULL); 11947 ASSERT(tcp->tcp_notsack_list != NULL); 11948 ASSERT(tcp->tcp_rexmit == B_FALSE); 11949 11950 /* Defensive coding in case there is a bug... */ 11951 if (tcp->tcp_notsack_list == NULL) { 11952 return; 11953 } 11954 notsack_blk = tcp->tcp_notsack_list; 11955 mss = tcp->tcp_mss; 11956 11957 /* 11958 * Limit the num of outstanding data in the network to be 11959 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11960 */ 11961 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11962 11963 /* At least retransmit 1 MSS of data. */ 11964 if (usable_swnd <= 0) { 11965 usable_swnd = mss; 11966 } 11967 11968 /* Make sure no new RTT samples will be taken. */ 11969 tcp->tcp_csuna = tcp->tcp_snxt; 11970 11971 notsack_blk = tcp->tcp_notsack_list; 11972 while (usable_swnd > 0) { 11973 mblk_t *snxt_mp, *tmp_mp; 11974 tcp_seq begin = tcp->tcp_sack_snxt; 11975 tcp_seq end; 11976 int32_t off; 11977 11978 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11979 if (SEQ_GT(notsack_blk->end, begin) && 11980 (notsack_blk->sack_cnt >= 11981 tcp_dupack_fast_retransmit)) { 11982 end = notsack_blk->end; 11983 if (SEQ_LT(begin, notsack_blk->begin)) { 11984 begin = notsack_blk->begin; 11985 } 11986 break; 11987 } 11988 } 11989 /* 11990 * All holes are filled. Manipulate tcp_cwnd to send more 11991 * if we can. Note that after the SACK recovery, tcp_cwnd is 11992 * set to tcp_cwnd_ssthresh. 11993 */ 11994 if (notsack_blk == NULL) { 11995 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11996 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11997 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11998 ASSERT(tcp->tcp_cwnd > 0); 11999 return; 12000 } else { 12001 usable_swnd = usable_swnd / mss; 12002 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12003 MAX(usable_swnd * mss, mss); 12004 *flags |= TH_XMIT_NEEDED; 12005 return; 12006 } 12007 } 12008 12009 /* 12010 * Note that we may send more than usable_swnd allows here 12011 * because of round off, but no more than 1 MSS of data. 12012 */ 12013 seg_len = end - begin; 12014 if (seg_len > mss) 12015 seg_len = mss; 12016 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12017 ASSERT(snxt_mp != NULL); 12018 /* This should not happen. Defensive coding again... */ 12019 if (snxt_mp == NULL) { 12020 return; 12021 } 12022 12023 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12024 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12025 if (xmit_mp == NULL) 12026 return; 12027 12028 usable_swnd -= seg_len; 12029 tcp->tcp_pipe += seg_len; 12030 tcp->tcp_sack_snxt = begin + seg_len; 12031 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 12032 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12033 12034 /* 12035 * Update the send timestamp to avoid false retransmission. 12036 */ 12037 snxt_mp->b_prev = (mblk_t *)lbolt; 12038 12039 BUMP_MIB(&tcp_mib, tcpRetransSegs); 12040 UPDATE_MIB(&tcp_mib, tcpRetransBytes, seg_len); 12041 BUMP_MIB(&tcp_mib, tcpOutSackRetransSegs); 12042 /* 12043 * Update tcp_rexmit_max to extend this SACK recovery phase. 12044 * This happens when new data sent during fast recovery is 12045 * also lost. If TCP retransmits those new data, it needs 12046 * to extend SACK recover phase to avoid starting another 12047 * fast retransmit/recovery unnecessarily. 12048 */ 12049 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12050 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12051 } 12052 } 12053 } 12054 12055 /* 12056 * This function handles policy checking at TCP level for non-hard_bound/ 12057 * detached connections. 12058 */ 12059 static boolean_t 12060 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12061 boolean_t secure, boolean_t mctl_present) 12062 { 12063 ipsec_latch_t *ipl = NULL; 12064 ipsec_action_t *act = NULL; 12065 mblk_t *data_mp; 12066 ipsec_in_t *ii; 12067 const char *reason; 12068 kstat_named_t *counter; 12069 12070 ASSERT(mctl_present || !secure); 12071 12072 ASSERT((ipha == NULL && ip6h != NULL) || 12073 (ip6h == NULL && ipha != NULL)); 12074 12075 /* 12076 * We don't necessarily have an ipsec_in_act action to verify 12077 * policy because of assymetrical policy where we have only 12078 * outbound policy and no inbound policy (possible with global 12079 * policy). 12080 */ 12081 if (!secure) { 12082 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12083 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12084 return (B_TRUE); 12085 ipsec_log_policy_failure(tcp->tcp_wq, IPSEC_POLICY_MISMATCH, 12086 "tcp_check_policy", ipha, ip6h, secure); 12087 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12088 &ipdrops_tcp_clear, &tcp_dropper); 12089 return (B_FALSE); 12090 } 12091 12092 /* 12093 * We have a secure packet. 12094 */ 12095 if (act == NULL) { 12096 ipsec_log_policy_failure(tcp->tcp_wq, 12097 IPSEC_POLICY_NOT_NEEDED, "tcp_check_policy", ipha, ip6h, 12098 secure); 12099 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12100 &ipdrops_tcp_secure, &tcp_dropper); 12101 return (B_FALSE); 12102 } 12103 12104 /* 12105 * XXX This whole routine is currently incorrect. ipl should 12106 * be set to the latch pointer, but is currently not set, so 12107 * we initialize it to NULL to avoid picking up random garbage. 12108 */ 12109 if (ipl == NULL) 12110 return (B_TRUE); 12111 12112 data_mp = first_mp->b_cont; 12113 12114 ii = (ipsec_in_t *)first_mp->b_rptr; 12115 12116 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12117 &counter, tcp->tcp_connp)) { 12118 BUMP_MIB(&ip_mib, ipsecInSucceeded); 12119 return (B_TRUE); 12120 } 12121 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12122 "tcp inbound policy mismatch: %s, packet dropped\n", 12123 reason); 12124 BUMP_MIB(&ip_mib, ipsecInFailed); 12125 12126 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, &tcp_dropper); 12127 return (B_FALSE); 12128 } 12129 12130 /* 12131 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12132 * retransmission after a timeout. 12133 * 12134 * To limit the number of duplicate segments, we limit the number of segment 12135 * to be sent in one time to tcp_snd_burst, the burst variable. 12136 */ 12137 static void 12138 tcp_ss_rexmit(tcp_t *tcp) 12139 { 12140 uint32_t snxt; 12141 uint32_t smax; 12142 int32_t win; 12143 int32_t mss; 12144 int32_t off; 12145 int32_t burst = tcp->tcp_snd_burst; 12146 mblk_t *snxt_mp; 12147 12148 /* 12149 * Note that tcp_rexmit can be set even though TCP has retransmitted 12150 * all unack'ed segments. 12151 */ 12152 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12153 smax = tcp->tcp_rexmit_max; 12154 snxt = tcp->tcp_rexmit_nxt; 12155 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12156 snxt = tcp->tcp_suna; 12157 } 12158 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12159 win -= snxt - tcp->tcp_suna; 12160 mss = tcp->tcp_mss; 12161 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12162 12163 while (SEQ_LT(snxt, smax) && (win > 0) && 12164 (burst > 0) && (snxt_mp != NULL)) { 12165 mblk_t *xmit_mp; 12166 mblk_t *old_snxt_mp = snxt_mp; 12167 uint32_t cnt = mss; 12168 12169 if (win < cnt) { 12170 cnt = win; 12171 } 12172 if (SEQ_GT(snxt + cnt, smax)) { 12173 cnt = smax - snxt; 12174 } 12175 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12176 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12177 if (xmit_mp == NULL) 12178 return; 12179 12180 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12181 12182 snxt += cnt; 12183 win -= cnt; 12184 /* 12185 * Update the send timestamp to avoid false 12186 * retransmission. 12187 */ 12188 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12189 BUMP_MIB(&tcp_mib, tcpRetransSegs); 12190 UPDATE_MIB(&tcp_mib, tcpRetransBytes, cnt); 12191 12192 tcp->tcp_rexmit_nxt = snxt; 12193 burst--; 12194 } 12195 /* 12196 * If we have transmitted all we have at the time 12197 * we started the retranmission, we can leave 12198 * the rest of the job to tcp_wput_data(). But we 12199 * need to check the send window first. If the 12200 * win is not 0, go on with tcp_wput_data(). 12201 */ 12202 if (SEQ_LT(snxt, smax) || win == 0) { 12203 return; 12204 } 12205 } 12206 /* Only call tcp_wput_data() if there is data to be sent. */ 12207 if (tcp->tcp_unsent) { 12208 tcp_wput_data(tcp, NULL, B_FALSE); 12209 } 12210 } 12211 12212 /* 12213 * Process all TCP option in SYN segment. Note that this function should 12214 * be called after tcp_adapt_ire() is called so that the necessary info 12215 * from IRE is already set in the tcp structure. 12216 * 12217 * This function sets up the correct tcp_mss value according to the 12218 * MSS option value and our header size. It also sets up the window scale 12219 * and timestamp values, and initialize SACK info blocks. But it does not 12220 * change receive window size after setting the tcp_mss value. The caller 12221 * should do the appropriate change. 12222 */ 12223 void 12224 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12225 { 12226 int options; 12227 tcp_opt_t tcpopt; 12228 uint32_t mss_max; 12229 char *tmp_tcph; 12230 12231 tcpopt.tcp = NULL; 12232 options = tcp_parse_options(tcph, &tcpopt); 12233 12234 /* 12235 * Process MSS option. Note that MSS option value does not account 12236 * for IP or TCP options. This means that it is equal to MTU - minimum 12237 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12238 * IPv6. 12239 */ 12240 if (!(options & TCP_OPT_MSS_PRESENT)) { 12241 if (tcp->tcp_ipversion == IPV4_VERSION) 12242 tcpopt.tcp_opt_mss = tcp_mss_def_ipv4; 12243 else 12244 tcpopt.tcp_opt_mss = tcp_mss_def_ipv6; 12245 } else { 12246 if (tcp->tcp_ipversion == IPV4_VERSION) 12247 mss_max = tcp_mss_max_ipv4; 12248 else 12249 mss_max = tcp_mss_max_ipv6; 12250 if (tcpopt.tcp_opt_mss < tcp_mss_min) 12251 tcpopt.tcp_opt_mss = tcp_mss_min; 12252 else if (tcpopt.tcp_opt_mss > mss_max) 12253 tcpopt.tcp_opt_mss = mss_max; 12254 } 12255 12256 /* Process Window Scale option. */ 12257 if (options & TCP_OPT_WSCALE_PRESENT) { 12258 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12259 tcp->tcp_snd_ws_ok = B_TRUE; 12260 } else { 12261 tcp->tcp_snd_ws = B_FALSE; 12262 tcp->tcp_snd_ws_ok = B_FALSE; 12263 tcp->tcp_rcv_ws = B_FALSE; 12264 } 12265 12266 /* Process Timestamp option. */ 12267 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12268 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12269 tmp_tcph = (char *)tcp->tcp_tcph; 12270 12271 tcp->tcp_snd_ts_ok = B_TRUE; 12272 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12273 tcp->tcp_last_rcv_lbolt = lbolt64; 12274 ASSERT(OK_32PTR(tmp_tcph)); 12275 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12276 12277 /* Fill in our template header with basic timestamp option. */ 12278 tmp_tcph += tcp->tcp_tcp_hdr_len; 12279 tmp_tcph[0] = TCPOPT_NOP; 12280 tmp_tcph[1] = TCPOPT_NOP; 12281 tmp_tcph[2] = TCPOPT_TSTAMP; 12282 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12283 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12284 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12285 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12286 } else { 12287 tcp->tcp_snd_ts_ok = B_FALSE; 12288 } 12289 12290 /* 12291 * Process SACK options. If SACK is enabled for this connection, 12292 * then allocate the SACK info structure. Note the following ways 12293 * when tcp_snd_sack_ok is set to true. 12294 * 12295 * For active connection: in tcp_adapt_ire() called in 12296 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12297 * is checked. 12298 * 12299 * For passive connection: in tcp_adapt_ire() called in 12300 * tcp_accept_comm(). 12301 * 12302 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12303 * That check makes sure that if we did not send a SACK OK option, 12304 * we will not enable SACK for this connection even though the other 12305 * side sends us SACK OK option. For active connection, the SACK 12306 * info structure has already been allocated. So we need to free 12307 * it if SACK is disabled. 12308 */ 12309 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12310 (tcp->tcp_snd_sack_ok || 12311 (tcp_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12312 /* This should be true only in the passive case. */ 12313 if (tcp->tcp_sack_info == NULL) { 12314 ASSERT(TCP_IS_DETACHED(tcp)); 12315 tcp->tcp_sack_info = 12316 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12317 } 12318 if (tcp->tcp_sack_info == NULL) { 12319 tcp->tcp_snd_sack_ok = B_FALSE; 12320 } else { 12321 tcp->tcp_snd_sack_ok = B_TRUE; 12322 if (tcp->tcp_snd_ts_ok) { 12323 tcp->tcp_max_sack_blk = 3; 12324 } else { 12325 tcp->tcp_max_sack_blk = 4; 12326 } 12327 } 12328 } else { 12329 /* 12330 * Resetting tcp_snd_sack_ok to B_FALSE so that 12331 * no SACK info will be used for this 12332 * connection. This assumes that SACK usage 12333 * permission is negotiated. This may need 12334 * to be changed once this is clarified. 12335 */ 12336 if (tcp->tcp_sack_info != NULL) { 12337 ASSERT(tcp->tcp_notsack_list == NULL); 12338 kmem_cache_free(tcp_sack_info_cache, 12339 tcp->tcp_sack_info); 12340 tcp->tcp_sack_info = NULL; 12341 } 12342 tcp->tcp_snd_sack_ok = B_FALSE; 12343 } 12344 12345 /* 12346 * Now we know the exact TCP/IP header length, subtract 12347 * that from tcp_mss to get our side's MSS. 12348 */ 12349 tcp->tcp_mss -= tcp->tcp_hdr_len; 12350 /* 12351 * Here we assume that the other side's header size will be equal to 12352 * our header size. We calculate the real MSS accordingly. Need to 12353 * take into additional stuffs IPsec puts in. 12354 * 12355 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12356 */ 12357 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12358 ((tcp->tcp_ipversion == IPV4_VERSION ? 12359 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12360 12361 /* 12362 * Set MSS to the smaller one of both ends of the connection. 12363 * We should not have called tcp_mss_set() before, but our 12364 * side of the MSS should have been set to a proper value 12365 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12366 * STREAM head parameters properly. 12367 * 12368 * If we have a larger-than-16-bit window but the other side 12369 * didn't want to do window scale, tcp_rwnd_set() will take 12370 * care of that. 12371 */ 12372 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss)); 12373 } 12374 12375 /* 12376 * Sends the T_CONN_IND to the listener. The caller calls this 12377 * functions via squeue to get inside the listener's perimeter 12378 * once the 3 way hand shake is done a T_CONN_IND needs to be 12379 * sent. As an optimization, the caller can call this directly 12380 * if listener's perimeter is same as eager's. 12381 */ 12382 /* ARGSUSED */ 12383 void 12384 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12385 { 12386 conn_t *lconnp = (conn_t *)arg; 12387 tcp_t *listener = lconnp->conn_tcp; 12388 tcp_t *tcp; 12389 struct T_conn_ind *conn_ind; 12390 ipaddr_t *addr_cache; 12391 boolean_t need_send_conn_ind = B_FALSE; 12392 12393 /* retrieve the eager */ 12394 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12395 ASSERT(conn_ind->OPT_offset != 0 && 12396 conn_ind->OPT_length == sizeof (intptr_t)); 12397 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12398 conn_ind->OPT_length); 12399 12400 /* 12401 * TLI/XTI applications will get confused by 12402 * sending eager as an option since it violates 12403 * the option semantics. So remove the eager as 12404 * option since TLI/XTI app doesn't need it anyway. 12405 */ 12406 if (!TCP_IS_SOCKET(listener)) { 12407 conn_ind->OPT_length = 0; 12408 conn_ind->OPT_offset = 0; 12409 } 12410 if (listener->tcp_state == TCPS_CLOSED || 12411 TCP_IS_DETACHED(listener)) { 12412 /* 12413 * If listener has closed, it would have caused a 12414 * a cleanup/blowoff to happen for the eager. We 12415 * just need to return. 12416 */ 12417 freemsg(mp); 12418 return; 12419 } 12420 12421 12422 /* 12423 * if the conn_req_q is full defer passing up the 12424 * T_CONN_IND until space is availabe after t_accept() 12425 * processing 12426 */ 12427 mutex_enter(&listener->tcp_eager_lock); 12428 12429 /* 12430 * Take the eager out, if it is in the list of droppable eagers 12431 * as we are here because the 3W handshake is over. 12432 */ 12433 MAKE_UNDROPPABLE(tcp); 12434 12435 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12436 tcp_t *tail; 12437 12438 /* 12439 * The eager already has an extra ref put in tcp_rput_data 12440 * so that it stays till accept comes back even though it 12441 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12442 */ 12443 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12444 listener->tcp_conn_req_cnt_q0--; 12445 listener->tcp_conn_req_cnt_q++; 12446 12447 /* Move from SYN_RCVD to ESTABLISHED list */ 12448 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12449 tcp->tcp_eager_prev_q0; 12450 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12451 tcp->tcp_eager_next_q0; 12452 tcp->tcp_eager_prev_q0 = NULL; 12453 tcp->tcp_eager_next_q0 = NULL; 12454 12455 /* 12456 * Insert at end of the queue because sockfs 12457 * sends down T_CONN_RES in chronological 12458 * order. Leaving the older conn indications 12459 * at front of the queue helps reducing search 12460 * time. 12461 */ 12462 tail = listener->tcp_eager_last_q; 12463 if (tail != NULL) 12464 tail->tcp_eager_next_q = tcp; 12465 else 12466 listener->tcp_eager_next_q = tcp; 12467 listener->tcp_eager_last_q = tcp; 12468 tcp->tcp_eager_next_q = NULL; 12469 /* 12470 * Delay sending up the T_conn_ind until we are 12471 * done with the eager. Once we have have sent up 12472 * the T_conn_ind, the accept can potentially complete 12473 * any time and release the refhold we have on the eager. 12474 */ 12475 need_send_conn_ind = B_TRUE; 12476 } else { 12477 /* 12478 * Defer connection on q0 and set deferred 12479 * connection bit true 12480 */ 12481 tcp->tcp_conn_def_q0 = B_TRUE; 12482 12483 /* take tcp out of q0 ... */ 12484 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12485 tcp->tcp_eager_next_q0; 12486 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12487 tcp->tcp_eager_prev_q0; 12488 12489 /* ... and place it at the end of q0 */ 12490 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12491 tcp->tcp_eager_next_q0 = listener; 12492 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12493 listener->tcp_eager_prev_q0 = tcp; 12494 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12495 } 12496 12497 /* we have timed out before */ 12498 if (tcp->tcp_syn_rcvd_timeout != 0) { 12499 tcp->tcp_syn_rcvd_timeout = 0; 12500 listener->tcp_syn_rcvd_timeout--; 12501 if (listener->tcp_syn_defense && 12502 listener->tcp_syn_rcvd_timeout <= 12503 (tcp_conn_req_max_q0 >> 5) && 12504 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12505 listener->tcp_last_rcv_lbolt)) { 12506 /* 12507 * Turn off the defense mode if we 12508 * believe the SYN attack is over. 12509 */ 12510 listener->tcp_syn_defense = B_FALSE; 12511 if (listener->tcp_ip_addr_cache) { 12512 kmem_free((void *)listener->tcp_ip_addr_cache, 12513 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12514 listener->tcp_ip_addr_cache = NULL; 12515 } 12516 } 12517 } 12518 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12519 if (addr_cache != NULL) { 12520 /* 12521 * We have finished a 3-way handshake with this 12522 * remote host. This proves the IP addr is good. 12523 * Cache it! 12524 */ 12525 addr_cache[IP_ADDR_CACHE_HASH( 12526 tcp->tcp_remote)] = tcp->tcp_remote; 12527 } 12528 mutex_exit(&listener->tcp_eager_lock); 12529 if (need_send_conn_ind) 12530 putnext(listener->tcp_rq, mp); 12531 } 12532 12533 mblk_t * 12534 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12535 uint_t *ifindexp, ip6_pkt_t *ippp) 12536 { 12537 in_pktinfo_t *pinfo; 12538 ip6_t *ip6h; 12539 uchar_t *rptr; 12540 mblk_t *first_mp = mp; 12541 boolean_t mctl_present = B_FALSE; 12542 uint_t ifindex = 0; 12543 ip6_pkt_t ipp; 12544 uint_t ipvers; 12545 uint_t ip_hdr_len; 12546 12547 rptr = mp->b_rptr; 12548 ASSERT(OK_32PTR(rptr)); 12549 ASSERT(tcp != NULL); 12550 ipp.ipp_fields = 0; 12551 12552 switch DB_TYPE(mp) { 12553 case M_CTL: 12554 mp = mp->b_cont; 12555 if (mp == NULL) { 12556 freemsg(first_mp); 12557 return (NULL); 12558 } 12559 if (DB_TYPE(mp) != M_DATA) { 12560 freemsg(first_mp); 12561 return (NULL); 12562 } 12563 mctl_present = B_TRUE; 12564 break; 12565 case M_DATA: 12566 break; 12567 default: 12568 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12569 freemsg(mp); 12570 return (NULL); 12571 } 12572 ipvers = IPH_HDR_VERSION(rptr); 12573 if (ipvers == IPV4_VERSION) { 12574 if (tcp == NULL) { 12575 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12576 goto done; 12577 } 12578 12579 ipp.ipp_fields |= IPPF_HOPLIMIT; 12580 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12581 12582 /* 12583 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12584 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12585 */ 12586 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12587 mctl_present) { 12588 pinfo = (in_pktinfo_t *)first_mp->b_rptr; 12589 if ((MBLKL(first_mp) == sizeof (in_pktinfo_t)) && 12590 (pinfo->in_pkt_ulp_type == IN_PKTINFO) && 12591 (pinfo->in_pkt_flags & IPF_RECVIF)) { 12592 ipp.ipp_fields |= IPPF_IFINDEX; 12593 ipp.ipp_ifindex = pinfo->in_pkt_ifindex; 12594 ifindex = pinfo->in_pkt_ifindex; 12595 } 12596 freeb(first_mp); 12597 mctl_present = B_FALSE; 12598 } 12599 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12600 } else { 12601 ip6h = (ip6_t *)rptr; 12602 12603 ASSERT(ipvers == IPV6_VERSION); 12604 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12605 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12606 ipp.ipp_hoplimit = ip6h->ip6_hops; 12607 12608 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12609 uint8_t nexthdrp; 12610 12611 /* Look for ifindex information */ 12612 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12613 ip6i_t *ip6i = (ip6i_t *)ip6h; 12614 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12615 BUMP_MIB(&ip_mib, tcpInErrs); 12616 freemsg(first_mp); 12617 return (NULL); 12618 } 12619 12620 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12621 ASSERT(ip6i->ip6i_ifindex != 0); 12622 ipp.ipp_fields |= IPPF_IFINDEX; 12623 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12624 ifindex = ip6i->ip6i_ifindex; 12625 } 12626 rptr = (uchar_t *)&ip6i[1]; 12627 mp->b_rptr = rptr; 12628 if (rptr == mp->b_wptr) { 12629 mblk_t *mp1; 12630 mp1 = mp->b_cont; 12631 freeb(mp); 12632 mp = mp1; 12633 rptr = mp->b_rptr; 12634 } 12635 if (MBLKL(mp) < IPV6_HDR_LEN + 12636 sizeof (tcph_t)) { 12637 BUMP_MIB(&ip_mib, tcpInErrs); 12638 freemsg(first_mp); 12639 return (NULL); 12640 } 12641 ip6h = (ip6_t *)rptr; 12642 } 12643 12644 /* 12645 * Find any potentially interesting extension headers 12646 * as well as the length of the IPv6 + extension 12647 * headers. 12648 */ 12649 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12650 /* Verify if this is a TCP packet */ 12651 if (nexthdrp != IPPROTO_TCP) { 12652 BUMP_MIB(&ip_mib, tcpInErrs); 12653 freemsg(first_mp); 12654 return (NULL); 12655 } 12656 } else { 12657 ip_hdr_len = IPV6_HDR_LEN; 12658 } 12659 } 12660 12661 done: 12662 if (ipversp != NULL) 12663 *ipversp = ipvers; 12664 if (ip_hdr_lenp != NULL) 12665 *ip_hdr_lenp = ip_hdr_len; 12666 if (ippp != NULL) 12667 *ippp = ipp; 12668 if (ifindexp != NULL) 12669 *ifindexp = ifindex; 12670 if (mctl_present) { 12671 freeb(first_mp); 12672 } 12673 return (mp); 12674 } 12675 12676 /* 12677 * Handle M_DATA messages from IP. Its called directly from IP via 12678 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12679 * in this path. 12680 * 12681 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12682 * v4 and v6), we are called through tcp_input() and a M_CTL can 12683 * be present for options but tcp_find_pktinfo() deals with it. We 12684 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12685 * 12686 * The first argument is always the connp/tcp to which the mp belongs. 12687 * There are no exceptions to this rule. The caller has already put 12688 * a reference on this connp/tcp and once tcp_rput_data() returns, 12689 * the squeue will do the refrele. 12690 * 12691 * The TH_SYN for the listener directly go to tcp_conn_request via 12692 * squeue. 12693 * 12694 * sqp: NULL = recursive, sqp != NULL means called from squeue 12695 */ 12696 void 12697 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12698 { 12699 int32_t bytes_acked; 12700 int32_t gap; 12701 mblk_t *mp1; 12702 uint_t flags; 12703 uint32_t new_swnd = 0; 12704 uchar_t *iphdr; 12705 uchar_t *rptr; 12706 int32_t rgap; 12707 uint32_t seg_ack; 12708 int seg_len; 12709 uint_t ip_hdr_len; 12710 uint32_t seg_seq; 12711 tcph_t *tcph; 12712 int urp; 12713 tcp_opt_t tcpopt; 12714 uint_t ipvers; 12715 ip6_pkt_t ipp; 12716 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12717 uint32_t cwnd; 12718 uint32_t add; 12719 int npkt; 12720 int mss; 12721 conn_t *connp = (conn_t *)arg; 12722 squeue_t *sqp = (squeue_t *)arg2; 12723 tcp_t *tcp = connp->conn_tcp; 12724 12725 /* 12726 * RST from fused tcp loopback peer should trigger an unfuse. 12727 */ 12728 if (tcp->tcp_fused) { 12729 TCP_STAT(tcp_fusion_aborted); 12730 tcp_unfuse(tcp); 12731 } 12732 12733 iphdr = mp->b_rptr; 12734 rptr = mp->b_rptr; 12735 ASSERT(OK_32PTR(rptr)); 12736 12737 /* 12738 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12739 * processing here. For rest call tcp_find_pktinfo to fill up the 12740 * necessary information. 12741 */ 12742 if (IPCL_IS_TCP4(connp)) { 12743 ipvers = IPV4_VERSION; 12744 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12745 } else { 12746 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12747 NULL, &ipp); 12748 if (mp == NULL) { 12749 TCP_STAT(tcp_rput_v6_error); 12750 return; 12751 } 12752 iphdr = mp->b_rptr; 12753 rptr = mp->b_rptr; 12754 } 12755 ASSERT(DB_TYPE(mp) == M_DATA); 12756 12757 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12758 seg_seq = ABE32_TO_U32(tcph->th_seq); 12759 seg_ack = ABE32_TO_U32(tcph->th_ack); 12760 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12761 seg_len = (int)(mp->b_wptr - rptr) - 12762 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12763 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12764 do { 12765 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12766 (uintptr_t)INT_MAX); 12767 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12768 } while ((mp1 = mp1->b_cont) != NULL && 12769 mp1->b_datap->db_type == M_DATA); 12770 } 12771 12772 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12773 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12774 seg_len, tcph); 12775 return; 12776 } 12777 12778 if (sqp != NULL) { 12779 /* 12780 * This is the correct place to update tcp_last_recv_time. Note 12781 * that it is also updated for tcp structure that belongs to 12782 * global and listener queues which do not really need updating. 12783 * But that should not cause any harm. And it is updated for 12784 * all kinds of incoming segments, not only for data segments. 12785 */ 12786 tcp->tcp_last_recv_time = lbolt; 12787 } 12788 12789 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12790 12791 BUMP_LOCAL(tcp->tcp_ibsegs); 12792 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 12793 12794 if ((flags & TH_URG) && sqp != NULL) { 12795 /* 12796 * TCP can't handle urgent pointers that arrive before 12797 * the connection has been accept()ed since it can't 12798 * buffer OOB data. Discard segment if this happens. 12799 * 12800 * Nor can it reassemble urgent pointers, so discard 12801 * if it's not the next segment expected. 12802 * 12803 * Otherwise, collapse chain into one mblk (discard if 12804 * that fails). This makes sure the headers, retransmitted 12805 * data, and new data all are in the same mblk. 12806 */ 12807 ASSERT(mp != NULL); 12808 if (tcp->tcp_listener || !pullupmsg(mp, -1)) { 12809 freemsg(mp); 12810 return; 12811 } 12812 /* Update pointers into message */ 12813 iphdr = rptr = mp->b_rptr; 12814 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12815 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12816 /* 12817 * Since we can't handle any data with this urgent 12818 * pointer that is out of sequence, we expunge 12819 * the data. This allows us to still register 12820 * the urgent mark and generate the M_PCSIG, 12821 * which we can do. 12822 */ 12823 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12824 seg_len = 0; 12825 } 12826 } 12827 12828 switch (tcp->tcp_state) { 12829 case TCPS_SYN_SENT: 12830 if (flags & TH_ACK) { 12831 /* 12832 * Note that our stack cannot send data before a 12833 * connection is established, therefore the 12834 * following check is valid. Otherwise, it has 12835 * to be changed. 12836 */ 12837 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12838 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12839 freemsg(mp); 12840 if (flags & TH_RST) 12841 return; 12842 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12843 tcp, seg_ack, 0, TH_RST); 12844 return; 12845 } 12846 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12847 } 12848 if (flags & TH_RST) { 12849 freemsg(mp); 12850 if (flags & TH_ACK) 12851 (void) tcp_clean_death(tcp, 12852 ECONNREFUSED, 13); 12853 return; 12854 } 12855 if (!(flags & TH_SYN)) { 12856 freemsg(mp); 12857 return; 12858 } 12859 12860 /* Process all TCP options. */ 12861 tcp_process_options(tcp, tcph); 12862 /* 12863 * The following changes our rwnd to be a multiple of the 12864 * MIN(peer MSS, our MSS) for performance reason. 12865 */ 12866 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 12867 tcp->tcp_mss)); 12868 12869 /* Is the other end ECN capable? */ 12870 if (tcp->tcp_ecn_ok) { 12871 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12872 tcp->tcp_ecn_ok = B_FALSE; 12873 } 12874 } 12875 /* 12876 * Clear ECN flags because it may interfere with later 12877 * processing. 12878 */ 12879 flags &= ~(TH_ECE|TH_CWR); 12880 12881 tcp->tcp_irs = seg_seq; 12882 tcp->tcp_rack = seg_seq; 12883 tcp->tcp_rnxt = seg_seq + 1; 12884 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12885 if (!TCP_IS_DETACHED(tcp)) { 12886 /* Allocate room for SACK options if needed. */ 12887 if (tcp->tcp_snd_sack_ok) { 12888 (void) mi_set_sth_wroff(tcp->tcp_rq, 12889 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 12890 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra)); 12891 } else { 12892 (void) mi_set_sth_wroff(tcp->tcp_rq, 12893 tcp->tcp_hdr_len + 12894 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra)); 12895 } 12896 } 12897 if (flags & TH_ACK) { 12898 /* 12899 * If we can't get the confirmation upstream, pretend 12900 * we didn't even see this one. 12901 * 12902 * XXX: how can we pretend we didn't see it if we 12903 * have updated rnxt et. al. 12904 * 12905 * For loopback we defer sending up the T_CONN_CON 12906 * until after some checks below. 12907 */ 12908 mp1 = NULL; 12909 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12910 tcp->tcp_loopback ? &mp1 : NULL)) { 12911 freemsg(mp); 12912 return; 12913 } 12914 /* SYN was acked - making progress */ 12915 if (tcp->tcp_ipversion == IPV6_VERSION) 12916 tcp->tcp_ip_forward_progress = B_TRUE; 12917 12918 /* One for the SYN */ 12919 tcp->tcp_suna = tcp->tcp_iss + 1; 12920 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12921 tcp->tcp_state = TCPS_ESTABLISHED; 12922 12923 /* 12924 * If SYN was retransmitted, need to reset all 12925 * retransmission info. This is because this 12926 * segment will be treated as a dup ACK. 12927 */ 12928 if (tcp->tcp_rexmit) { 12929 tcp->tcp_rexmit = B_FALSE; 12930 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12931 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12932 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12933 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12934 tcp->tcp_ms_we_have_waited = 0; 12935 12936 /* 12937 * Set tcp_cwnd back to 1 MSS, per 12938 * recommendation from 12939 * draft-floyd-incr-init-win-01.txt, 12940 * Increasing TCP's Initial Window. 12941 */ 12942 tcp->tcp_cwnd = tcp->tcp_mss; 12943 } 12944 12945 tcp->tcp_swl1 = seg_seq; 12946 tcp->tcp_swl2 = seg_ack; 12947 12948 new_swnd = BE16_TO_U16(tcph->th_win); 12949 tcp->tcp_swnd = new_swnd; 12950 if (new_swnd > tcp->tcp_max_swnd) 12951 tcp->tcp_max_swnd = new_swnd; 12952 12953 /* 12954 * Always send the three-way handshake ack immediately 12955 * in order to make the connection complete as soon as 12956 * possible on the accepting host. 12957 */ 12958 flags |= TH_ACK_NEEDED; 12959 12960 /* 12961 * Special case for loopback. At this point we have 12962 * received SYN-ACK from the remote endpoint. In 12963 * order to ensure that both endpoints reach the 12964 * fused state prior to any data exchange, the final 12965 * ACK needs to be sent before we indicate T_CONN_CON 12966 * to the module upstream. 12967 */ 12968 if (tcp->tcp_loopback) { 12969 mblk_t *ack_mp; 12970 12971 ASSERT(!tcp->tcp_unfusable); 12972 ASSERT(mp1 != NULL); 12973 /* 12974 * For loopback, we always get a pure SYN-ACK 12975 * and only need to send back the final ACK 12976 * with no data (this is because the other 12977 * tcp is ours and we don't do T/TCP). This 12978 * final ACK triggers the passive side to 12979 * perform fusion in ESTABLISHED state. 12980 */ 12981 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 12982 if (tcp->tcp_ack_tid != 0) { 12983 (void) TCP_TIMER_CANCEL(tcp, 12984 tcp->tcp_ack_tid); 12985 tcp->tcp_ack_tid = 0; 12986 } 12987 TCP_RECORD_TRACE(tcp, ack_mp, 12988 TCP_TRACE_SEND_PKT); 12989 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 12990 BUMP_LOCAL(tcp->tcp_obsegs); 12991 BUMP_MIB(&tcp_mib, tcpOutAck); 12992 12993 /* Send up T_CONN_CON */ 12994 putnext(tcp->tcp_rq, mp1); 12995 12996 freemsg(mp); 12997 return; 12998 } 12999 /* 13000 * Forget fusion; we need to handle more 13001 * complex cases below. Send the deferred 13002 * T_CONN_CON message upstream and proceed 13003 * as usual. Mark this tcp as not capable 13004 * of fusion. 13005 */ 13006 TCP_STAT(tcp_fusion_unfusable); 13007 tcp->tcp_unfusable = B_TRUE; 13008 putnext(tcp->tcp_rq, mp1); 13009 } 13010 13011 /* 13012 * Check to see if there is data to be sent. If 13013 * yes, set the transmit flag. Then check to see 13014 * if received data processing needs to be done. 13015 * If not, go straight to xmit_check. This short 13016 * cut is OK as we don't support T/TCP. 13017 */ 13018 if (tcp->tcp_unsent) 13019 flags |= TH_XMIT_NEEDED; 13020 13021 if (seg_len == 0 && !(flags & TH_URG)) { 13022 freemsg(mp); 13023 goto xmit_check; 13024 } 13025 13026 flags &= ~TH_SYN; 13027 seg_seq++; 13028 break; 13029 } 13030 tcp->tcp_state = TCPS_SYN_RCVD; 13031 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13032 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13033 if (mp1) { 13034 DB_CPID(mp1) = tcp->tcp_cpid; 13035 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 13036 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13037 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13038 } 13039 freemsg(mp); 13040 return; 13041 case TCPS_SYN_RCVD: 13042 if (flags & TH_ACK) { 13043 /* 13044 * In this state, a SYN|ACK packet is either bogus 13045 * because the other side must be ACKing our SYN which 13046 * indicates it has seen the ACK for their SYN and 13047 * shouldn't retransmit it or we're crossing SYNs 13048 * on active open. 13049 */ 13050 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13051 freemsg(mp); 13052 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13053 tcp, seg_ack, 0, TH_RST); 13054 return; 13055 } 13056 /* 13057 * NOTE: RFC 793 pg. 72 says this should be 13058 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13059 * but that would mean we have an ack that ignored 13060 * our SYN. 13061 */ 13062 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13063 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13064 freemsg(mp); 13065 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13066 tcp, seg_ack, 0, TH_RST); 13067 return; 13068 } 13069 } 13070 break; 13071 case TCPS_LISTEN: 13072 /* 13073 * Only a TLI listener can come through this path when a 13074 * acceptor is going back to be a listener and a packet 13075 * for the acceptor hits the classifier. For a socket 13076 * listener, this can never happen because a listener 13077 * can never accept connection on itself and hence a 13078 * socket acceptor can not go back to being a listener. 13079 */ 13080 ASSERT(!TCP_IS_SOCKET(tcp)); 13081 /*FALLTHRU*/ 13082 case TCPS_CLOSED: 13083 case TCPS_BOUND: { 13084 conn_t *new_connp; 13085 13086 new_connp = ipcl_classify(mp, connp->conn_zoneid); 13087 if (new_connp != NULL) { 13088 tcp_reinput(new_connp, mp, connp->conn_sqp); 13089 return; 13090 } 13091 /* We failed to classify. For now just drop the packet */ 13092 freemsg(mp); 13093 return; 13094 } 13095 case TCPS_IDLE: 13096 /* 13097 * Handle the case where the tcp_clean_death() has happened 13098 * on a connection (application hasn't closed yet) but a packet 13099 * was already queued on squeue before tcp_clean_death() 13100 * was processed. Calling tcp_clean_death() twice on same 13101 * connection can result in weird behaviour. 13102 */ 13103 freemsg(mp); 13104 return; 13105 default: 13106 break; 13107 } 13108 13109 /* 13110 * Already on the correct queue/perimeter. 13111 * If this is a detached connection and not an eager 13112 * connection hanging off a listener then new data 13113 * (past the FIN) will cause a reset. 13114 * We do a special check here where it 13115 * is out of the main line, rather than check 13116 * if we are detached every time we see new 13117 * data down below. 13118 */ 13119 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13120 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13121 BUMP_MIB(&tcp_mib, tcpInClosed); 13122 TCP_RECORD_TRACE(tcp, 13123 mp, TCP_TRACE_RECV_PKT); 13124 13125 freemsg(mp); 13126 /* 13127 * This could be an SSL closure alert. We're detached so just 13128 * acknowledge it this last time. 13129 */ 13130 if (tcp->tcp_kssl_ctx != NULL) { 13131 kssl_release_ctx(tcp->tcp_kssl_ctx); 13132 tcp->tcp_kssl_ctx = NULL; 13133 13134 tcp->tcp_rnxt += seg_len; 13135 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13136 flags |= TH_ACK_NEEDED; 13137 goto ack_check; 13138 } 13139 13140 tcp_xmit_ctl("new data when detached", tcp, 13141 tcp->tcp_snxt, 0, TH_RST); 13142 (void) tcp_clean_death(tcp, EPROTO, 12); 13143 return; 13144 } 13145 13146 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13147 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13148 new_swnd = BE16_TO_U16(tcph->th_win) << 13149 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13150 mss = tcp->tcp_mss; 13151 13152 if (tcp->tcp_snd_ts_ok) { 13153 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13154 /* 13155 * This segment is not acceptable. 13156 * Drop it and send back an ACK. 13157 */ 13158 freemsg(mp); 13159 flags |= TH_ACK_NEEDED; 13160 goto ack_check; 13161 } 13162 } else if (tcp->tcp_snd_sack_ok) { 13163 ASSERT(tcp->tcp_sack_info != NULL); 13164 tcpopt.tcp = tcp; 13165 /* 13166 * SACK info in already updated in tcp_parse_options. Ignore 13167 * all other TCP options... 13168 */ 13169 (void) tcp_parse_options(tcph, &tcpopt); 13170 } 13171 try_again:; 13172 gap = seg_seq - tcp->tcp_rnxt; 13173 rgap = tcp->tcp_rwnd - (gap + seg_len); 13174 /* 13175 * gap is the amount of sequence space between what we expect to see 13176 * and what we got for seg_seq. A positive value for gap means 13177 * something got lost. A negative value means we got some old stuff. 13178 */ 13179 if (gap < 0) { 13180 /* Old stuff present. Is the SYN in there? */ 13181 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13182 (seg_len != 0)) { 13183 flags &= ~TH_SYN; 13184 seg_seq++; 13185 urp--; 13186 /* Recompute the gaps after noting the SYN. */ 13187 goto try_again; 13188 } 13189 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 13190 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, 13191 (seg_len > -gap ? -gap : seg_len)); 13192 /* Remove the old stuff from seg_len. */ 13193 seg_len += gap; 13194 /* 13195 * Anything left? 13196 * Make sure to check for unack'd FIN when rest of data 13197 * has been previously ack'd. 13198 */ 13199 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13200 /* 13201 * Resets are only valid if they lie within our offered 13202 * window. If the RST bit is set, we just ignore this 13203 * segment. 13204 */ 13205 if (flags & TH_RST) { 13206 freemsg(mp); 13207 return; 13208 } 13209 13210 /* 13211 * The arriving of dup data packets indicate that we 13212 * may have postponed an ack for too long, or the other 13213 * side's RTT estimate is out of shape. Start acking 13214 * more often. 13215 */ 13216 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13217 tcp->tcp_rack_cnt >= 1 && 13218 tcp->tcp_rack_abs_max > 2) { 13219 tcp->tcp_rack_abs_max--; 13220 } 13221 tcp->tcp_rack_cur_max = 1; 13222 13223 /* 13224 * This segment is "unacceptable". None of its 13225 * sequence space lies within our advertized window. 13226 * 13227 * Adjust seg_len to the original value for tracing. 13228 */ 13229 seg_len -= gap; 13230 if (tcp->tcp_debug) { 13231 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13232 "tcp_rput: unacceptable, gap %d, rgap %d, " 13233 "flags 0x%x, seg_seq %u, seg_ack %u, " 13234 "seg_len %d, rnxt %u, snxt %u, %s", 13235 gap, rgap, flags, seg_seq, seg_ack, 13236 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13237 tcp_display(tcp, NULL, 13238 DISP_ADDR_AND_PORT)); 13239 } 13240 13241 /* 13242 * Arrange to send an ACK in response to the 13243 * unacceptable segment per RFC 793 page 69. There 13244 * is only one small difference between ours and the 13245 * acceptability test in the RFC - we accept ACK-only 13246 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13247 * will be generated. 13248 * 13249 * Note that we have to ACK an ACK-only packet at least 13250 * for stacks that send 0-length keep-alives with 13251 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13252 * section 4.2.3.6. As long as we don't ever generate 13253 * an unacceptable packet in response to an incoming 13254 * packet that is unacceptable, it should not cause 13255 * "ACK wars". 13256 */ 13257 flags |= TH_ACK_NEEDED; 13258 13259 /* 13260 * Continue processing this segment in order to use the 13261 * ACK information it contains, but skip all other 13262 * sequence-number processing. Processing the ACK 13263 * information is necessary in order to 13264 * re-synchronize connections that may have lost 13265 * synchronization. 13266 * 13267 * We clear seg_len and flag fields related to 13268 * sequence number processing as they are not 13269 * to be trusted for an unacceptable segment. 13270 */ 13271 seg_len = 0; 13272 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13273 goto process_ack; 13274 } 13275 13276 /* Fix seg_seq, and chew the gap off the front. */ 13277 seg_seq = tcp->tcp_rnxt; 13278 urp += gap; 13279 do { 13280 mblk_t *mp2; 13281 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13282 (uintptr_t)UINT_MAX); 13283 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13284 if (gap > 0) { 13285 mp->b_rptr = mp->b_wptr - gap; 13286 break; 13287 } 13288 mp2 = mp; 13289 mp = mp->b_cont; 13290 freeb(mp2); 13291 } while (gap < 0); 13292 /* 13293 * If the urgent data has already been acknowledged, we 13294 * should ignore TH_URG below 13295 */ 13296 if (urp < 0) 13297 flags &= ~TH_URG; 13298 } 13299 /* 13300 * rgap is the amount of stuff received out of window. A negative 13301 * value is the amount out of window. 13302 */ 13303 if (rgap < 0) { 13304 mblk_t *mp2; 13305 13306 if (tcp->tcp_rwnd == 0) { 13307 BUMP_MIB(&tcp_mib, tcpInWinProbe); 13308 } else { 13309 BUMP_MIB(&tcp_mib, tcpInDataPastWinSegs); 13310 UPDATE_MIB(&tcp_mib, tcpInDataPastWinBytes, -rgap); 13311 } 13312 13313 /* 13314 * seg_len does not include the FIN, so if more than 13315 * just the FIN is out of window, we act like we don't 13316 * see it. (If just the FIN is out of window, rgap 13317 * will be zero and we will go ahead and acknowledge 13318 * the FIN.) 13319 */ 13320 flags &= ~TH_FIN; 13321 13322 /* Fix seg_len and make sure there is something left. */ 13323 seg_len += rgap; 13324 if (seg_len <= 0) { 13325 /* 13326 * Resets are only valid if they lie within our offered 13327 * window. If the RST bit is set, we just ignore this 13328 * segment. 13329 */ 13330 if (flags & TH_RST) { 13331 freemsg(mp); 13332 return; 13333 } 13334 13335 /* Per RFC 793, we need to send back an ACK. */ 13336 flags |= TH_ACK_NEEDED; 13337 13338 /* 13339 * Send SIGURG as soon as possible i.e. even 13340 * if the TH_URG was delivered in a window probe 13341 * packet (which will be unacceptable). 13342 * 13343 * We generate a signal if none has been generated 13344 * for this connection or if this is a new urgent 13345 * byte. Also send a zero-length "unmarked" message 13346 * to inform SIOCATMARK that this is not the mark. 13347 * 13348 * tcp_urp_last_valid is cleared when the T_exdata_ind 13349 * is sent up. This plus the check for old data 13350 * (gap >= 0) handles the wraparound of the sequence 13351 * number space without having to always track the 13352 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13353 * this max in its rcv_up variable). 13354 * 13355 * This prevents duplicate SIGURGS due to a "late" 13356 * zero-window probe when the T_EXDATA_IND has already 13357 * been sent up. 13358 */ 13359 if ((flags & TH_URG) && 13360 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13361 tcp->tcp_urp_last))) { 13362 mp1 = allocb(0, BPRI_MED); 13363 if (mp1 == NULL) { 13364 freemsg(mp); 13365 return; 13366 } 13367 if (!TCP_IS_DETACHED(tcp) && 13368 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13369 SIGURG)) { 13370 /* Try again on the rexmit. */ 13371 freemsg(mp1); 13372 freemsg(mp); 13373 return; 13374 } 13375 /* 13376 * If the next byte would be the mark 13377 * then mark with MARKNEXT else mark 13378 * with NOTMARKNEXT. 13379 */ 13380 if (gap == 0 && urp == 0) 13381 mp1->b_flag |= MSGMARKNEXT; 13382 else 13383 mp1->b_flag |= MSGNOTMARKNEXT; 13384 freemsg(tcp->tcp_urp_mark_mp); 13385 tcp->tcp_urp_mark_mp = mp1; 13386 flags |= TH_SEND_URP_MARK; 13387 tcp->tcp_urp_last_valid = B_TRUE; 13388 tcp->tcp_urp_last = urp + seg_seq; 13389 } 13390 /* 13391 * If this is a zero window probe, continue to 13392 * process the ACK part. But we need to set seg_len 13393 * to 0 to avoid data processing. Otherwise just 13394 * drop the segment and send back an ACK. 13395 */ 13396 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13397 flags &= ~(TH_SYN | TH_URG); 13398 seg_len = 0; 13399 goto process_ack; 13400 } else { 13401 freemsg(mp); 13402 goto ack_check; 13403 } 13404 } 13405 /* Pitch out of window stuff off the end. */ 13406 rgap = seg_len; 13407 mp2 = mp; 13408 do { 13409 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13410 (uintptr_t)INT_MAX); 13411 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13412 if (rgap < 0) { 13413 mp2->b_wptr += rgap; 13414 if ((mp1 = mp2->b_cont) != NULL) { 13415 mp2->b_cont = NULL; 13416 freemsg(mp1); 13417 } 13418 break; 13419 } 13420 } while ((mp2 = mp2->b_cont) != NULL); 13421 } 13422 ok:; 13423 /* 13424 * TCP should check ECN info for segments inside the window only. 13425 * Therefore the check should be done here. 13426 */ 13427 if (tcp->tcp_ecn_ok) { 13428 if (flags & TH_CWR) { 13429 tcp->tcp_ecn_echo_on = B_FALSE; 13430 } 13431 /* 13432 * Note that both ECN_CE and CWR can be set in the 13433 * same segment. In this case, we once again turn 13434 * on ECN_ECHO. 13435 */ 13436 if (tcp->tcp_ipversion == IPV4_VERSION) { 13437 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13438 13439 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13440 tcp->tcp_ecn_echo_on = B_TRUE; 13441 } 13442 } else { 13443 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13444 13445 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13446 htonl(IPH_ECN_CE << 20)) { 13447 tcp->tcp_ecn_echo_on = B_TRUE; 13448 } 13449 } 13450 } 13451 13452 /* 13453 * Check whether we can update tcp_ts_recent. This test is 13454 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13455 * Extensions for High Performance: An Update", Internet Draft. 13456 */ 13457 if (tcp->tcp_snd_ts_ok && 13458 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13459 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13460 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13461 tcp->tcp_last_rcv_lbolt = lbolt64; 13462 } 13463 13464 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13465 /* 13466 * FIN in an out of order segment. We record this in 13467 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13468 * Clear the FIN so that any check on FIN flag will fail. 13469 * Remember that FIN also counts in the sequence number 13470 * space. So we need to ack out of order FIN only segments. 13471 */ 13472 if (flags & TH_FIN) { 13473 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13474 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13475 flags &= ~TH_FIN; 13476 flags |= TH_ACK_NEEDED; 13477 } 13478 if (seg_len > 0) { 13479 /* Fill in the SACK blk list. */ 13480 if (tcp->tcp_snd_sack_ok) { 13481 ASSERT(tcp->tcp_sack_info != NULL); 13482 tcp_sack_insert(tcp->tcp_sack_list, 13483 seg_seq, seg_seq + seg_len, 13484 &(tcp->tcp_num_sack_blk)); 13485 } 13486 13487 /* 13488 * Attempt reassembly and see if we have something 13489 * ready to go. 13490 */ 13491 mp = tcp_reass(tcp, mp, seg_seq); 13492 /* Always ack out of order packets */ 13493 flags |= TH_ACK_NEEDED | TH_PUSH; 13494 if (mp) { 13495 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13496 (uintptr_t)INT_MAX); 13497 seg_len = mp->b_cont ? msgdsize(mp) : 13498 (int)(mp->b_wptr - mp->b_rptr); 13499 seg_seq = tcp->tcp_rnxt; 13500 /* 13501 * A gap is filled and the seq num and len 13502 * of the gap match that of a previously 13503 * received FIN, put the FIN flag back in. 13504 */ 13505 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13506 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13507 flags |= TH_FIN; 13508 tcp->tcp_valid_bits &= 13509 ~TCP_OFO_FIN_VALID; 13510 } 13511 } else { 13512 /* 13513 * Keep going even with NULL mp. 13514 * There may be a useful ACK or something else 13515 * we don't want to miss. 13516 * 13517 * But TCP should not perform fast retransmit 13518 * because of the ack number. TCP uses 13519 * seg_len == 0 to determine if it is a pure 13520 * ACK. And this is not a pure ACK. 13521 */ 13522 seg_len = 0; 13523 ofo_seg = B_TRUE; 13524 } 13525 } 13526 } else if (seg_len > 0) { 13527 BUMP_MIB(&tcp_mib, tcpInDataInorderSegs); 13528 UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, seg_len); 13529 /* 13530 * If an out of order FIN was received before, and the seq 13531 * num and len of the new segment match that of the FIN, 13532 * put the FIN flag back in. 13533 */ 13534 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13535 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13536 flags |= TH_FIN; 13537 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13538 } 13539 } 13540 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13541 if (flags & TH_RST) { 13542 freemsg(mp); 13543 switch (tcp->tcp_state) { 13544 case TCPS_SYN_RCVD: 13545 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13546 break; 13547 case TCPS_ESTABLISHED: 13548 case TCPS_FIN_WAIT_1: 13549 case TCPS_FIN_WAIT_2: 13550 case TCPS_CLOSE_WAIT: 13551 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13552 break; 13553 case TCPS_CLOSING: 13554 case TCPS_LAST_ACK: 13555 (void) tcp_clean_death(tcp, 0, 16); 13556 break; 13557 default: 13558 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13559 (void) tcp_clean_death(tcp, ENXIO, 17); 13560 break; 13561 } 13562 return; 13563 } 13564 if (flags & TH_SYN) { 13565 /* 13566 * See RFC 793, Page 71 13567 * 13568 * The seq number must be in the window as it should 13569 * be "fixed" above. If it is outside window, it should 13570 * be already rejected. Note that we allow seg_seq to be 13571 * rnxt + rwnd because we want to accept 0 window probe. 13572 */ 13573 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13574 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13575 freemsg(mp); 13576 /* 13577 * If the ACK flag is not set, just use our snxt as the 13578 * seq number of the RST segment. 13579 */ 13580 if (!(flags & TH_ACK)) { 13581 seg_ack = tcp->tcp_snxt; 13582 } 13583 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13584 TH_RST|TH_ACK); 13585 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13586 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13587 return; 13588 } 13589 /* 13590 * urp could be -1 when the urp field in the packet is 0 13591 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13592 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13593 */ 13594 if (flags & TH_URG && urp >= 0) { 13595 if (!tcp->tcp_urp_last_valid || 13596 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13597 /* 13598 * If we haven't generated the signal yet for this 13599 * urgent pointer value, do it now. Also, send up a 13600 * zero-length M_DATA indicating whether or not this is 13601 * the mark. The latter is not needed when a 13602 * T_EXDATA_IND is sent up. However, if there are 13603 * allocation failures this code relies on the sender 13604 * retransmitting and the socket code for determining 13605 * the mark should not block waiting for the peer to 13606 * transmit. Thus, for simplicity we always send up the 13607 * mark indication. 13608 */ 13609 mp1 = allocb(0, BPRI_MED); 13610 if (mp1 == NULL) { 13611 freemsg(mp); 13612 return; 13613 } 13614 if (!TCP_IS_DETACHED(tcp) && 13615 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13616 /* Try again on the rexmit. */ 13617 freemsg(mp1); 13618 freemsg(mp); 13619 return; 13620 } 13621 /* 13622 * Mark with NOTMARKNEXT for now. 13623 * The code below will change this to MARKNEXT 13624 * if we are at the mark. 13625 * 13626 * If there are allocation failures (e.g. in dupmsg 13627 * below) the next time tcp_rput_data sees the urgent 13628 * segment it will send up the MSG*MARKNEXT message. 13629 */ 13630 mp1->b_flag |= MSGNOTMARKNEXT; 13631 freemsg(tcp->tcp_urp_mark_mp); 13632 tcp->tcp_urp_mark_mp = mp1; 13633 flags |= TH_SEND_URP_MARK; 13634 #ifdef DEBUG 13635 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13636 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13637 "last %x, %s", 13638 seg_seq, urp, tcp->tcp_urp_last, 13639 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13640 #endif /* DEBUG */ 13641 tcp->tcp_urp_last_valid = B_TRUE; 13642 tcp->tcp_urp_last = urp + seg_seq; 13643 } else if (tcp->tcp_urp_mark_mp != NULL) { 13644 /* 13645 * An allocation failure prevented the previous 13646 * tcp_rput_data from sending up the allocated 13647 * MSG*MARKNEXT message - send it up this time 13648 * around. 13649 */ 13650 flags |= TH_SEND_URP_MARK; 13651 } 13652 13653 /* 13654 * If the urgent byte is in this segment, make sure that it is 13655 * all by itself. This makes it much easier to deal with the 13656 * possibility of an allocation failure on the T_exdata_ind. 13657 * Note that seg_len is the number of bytes in the segment, and 13658 * urp is the offset into the segment of the urgent byte. 13659 * urp < seg_len means that the urgent byte is in this segment. 13660 */ 13661 if (urp < seg_len) { 13662 if (seg_len != 1) { 13663 uint32_t tmp_rnxt; 13664 /* 13665 * Break it up and feed it back in. 13666 * Re-attach the IP header. 13667 */ 13668 mp->b_rptr = iphdr; 13669 if (urp > 0) { 13670 /* 13671 * There is stuff before the urgent 13672 * byte. 13673 */ 13674 mp1 = dupmsg(mp); 13675 if (!mp1) { 13676 /* 13677 * Trim from urgent byte on. 13678 * The rest will come back. 13679 */ 13680 (void) adjmsg(mp, 13681 urp - seg_len); 13682 tcp_rput_data(connp, 13683 mp, NULL); 13684 return; 13685 } 13686 (void) adjmsg(mp1, urp - seg_len); 13687 /* Feed this piece back in. */ 13688 tmp_rnxt = tcp->tcp_rnxt; 13689 tcp_rput_data(connp, mp1, NULL); 13690 /* 13691 * If the data passed back in was not 13692 * processed (ie: bad ACK) sending 13693 * the remainder back in will cause a 13694 * loop. In this case, drop the 13695 * packet and let the sender try 13696 * sending a good packet. 13697 */ 13698 if (tmp_rnxt == tcp->tcp_rnxt) { 13699 freemsg(mp); 13700 return; 13701 } 13702 } 13703 if (urp != seg_len - 1) { 13704 uint32_t tmp_rnxt; 13705 /* 13706 * There is stuff after the urgent 13707 * byte. 13708 */ 13709 mp1 = dupmsg(mp); 13710 if (!mp1) { 13711 /* 13712 * Trim everything beyond the 13713 * urgent byte. The rest will 13714 * come back. 13715 */ 13716 (void) adjmsg(mp, 13717 urp + 1 - seg_len); 13718 tcp_rput_data(connp, 13719 mp, NULL); 13720 return; 13721 } 13722 (void) adjmsg(mp1, urp + 1 - seg_len); 13723 tmp_rnxt = tcp->tcp_rnxt; 13724 tcp_rput_data(connp, mp1, NULL); 13725 /* 13726 * If the data passed back in was not 13727 * processed (ie: bad ACK) sending 13728 * the remainder back in will cause a 13729 * loop. In this case, drop the 13730 * packet and let the sender try 13731 * sending a good packet. 13732 */ 13733 if (tmp_rnxt == tcp->tcp_rnxt) { 13734 freemsg(mp); 13735 return; 13736 } 13737 } 13738 tcp_rput_data(connp, mp, NULL); 13739 return; 13740 } 13741 /* 13742 * This segment contains only the urgent byte. We 13743 * have to allocate the T_exdata_ind, if we can. 13744 */ 13745 if (!tcp->tcp_urp_mp) { 13746 struct T_exdata_ind *tei; 13747 mp1 = allocb(sizeof (struct T_exdata_ind), 13748 BPRI_MED); 13749 if (!mp1) { 13750 /* 13751 * Sigh... It'll be back. 13752 * Generate any MSG*MARK message now. 13753 */ 13754 freemsg(mp); 13755 seg_len = 0; 13756 if (flags & TH_SEND_URP_MARK) { 13757 13758 13759 ASSERT(tcp->tcp_urp_mark_mp); 13760 tcp->tcp_urp_mark_mp->b_flag &= 13761 ~MSGNOTMARKNEXT; 13762 tcp->tcp_urp_mark_mp->b_flag |= 13763 MSGMARKNEXT; 13764 } 13765 goto ack_check; 13766 } 13767 mp1->b_datap->db_type = M_PROTO; 13768 tei = (struct T_exdata_ind *)mp1->b_rptr; 13769 tei->PRIM_type = T_EXDATA_IND; 13770 tei->MORE_flag = 0; 13771 mp1->b_wptr = (uchar_t *)&tei[1]; 13772 tcp->tcp_urp_mp = mp1; 13773 #ifdef DEBUG 13774 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13775 "tcp_rput: allocated exdata_ind %s", 13776 tcp_display(tcp, NULL, 13777 DISP_PORT_ONLY)); 13778 #endif /* DEBUG */ 13779 /* 13780 * There is no need to send a separate MSG*MARK 13781 * message since the T_EXDATA_IND will be sent 13782 * now. 13783 */ 13784 flags &= ~TH_SEND_URP_MARK; 13785 freemsg(tcp->tcp_urp_mark_mp); 13786 tcp->tcp_urp_mark_mp = NULL; 13787 } 13788 /* 13789 * Now we are all set. On the next putnext upstream, 13790 * tcp_urp_mp will be non-NULL and will get prepended 13791 * to what has to be this piece containing the urgent 13792 * byte. If for any reason we abort this segment below, 13793 * if it comes back, we will have this ready, or it 13794 * will get blown off in close. 13795 */ 13796 } else if (urp == seg_len) { 13797 /* 13798 * The urgent byte is the next byte after this sequence 13799 * number. If there is data it is marked with 13800 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13801 * since it is not needed. Otherwise, if the code 13802 * above just allocated a zero-length tcp_urp_mark_mp 13803 * message, that message is tagged with MSGMARKNEXT. 13804 * Sending up these MSGMARKNEXT messages makes 13805 * SIOCATMARK work correctly even though 13806 * the T_EXDATA_IND will not be sent up until the 13807 * urgent byte arrives. 13808 */ 13809 if (seg_len != 0) { 13810 flags |= TH_MARKNEXT_NEEDED; 13811 freemsg(tcp->tcp_urp_mark_mp); 13812 tcp->tcp_urp_mark_mp = NULL; 13813 flags &= ~TH_SEND_URP_MARK; 13814 } else if (tcp->tcp_urp_mark_mp != NULL) { 13815 flags |= TH_SEND_URP_MARK; 13816 tcp->tcp_urp_mark_mp->b_flag &= 13817 ~MSGNOTMARKNEXT; 13818 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13819 } 13820 #ifdef DEBUG 13821 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13822 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13823 seg_len, flags, 13824 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13825 #endif /* DEBUG */ 13826 } else { 13827 /* Data left until we hit mark */ 13828 #ifdef DEBUG 13829 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13830 "tcp_rput: URP %d bytes left, %s", 13831 urp - seg_len, tcp_display(tcp, NULL, 13832 DISP_PORT_ONLY)); 13833 #endif /* DEBUG */ 13834 } 13835 } 13836 13837 process_ack: 13838 if (!(flags & TH_ACK)) { 13839 freemsg(mp); 13840 goto xmit_check; 13841 } 13842 } 13843 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13844 13845 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13846 tcp->tcp_ip_forward_progress = B_TRUE; 13847 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13848 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13849 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13850 /* 3-way handshake complete - pass up the T_CONN_IND */ 13851 tcp_t *listener = tcp->tcp_listener; 13852 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13853 13854 tcp->tcp_tconnind_started = B_TRUE; 13855 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13856 /* 13857 * We are here means eager is fine but it can 13858 * get a TH_RST at any point between now and till 13859 * accept completes and disappear. We need to 13860 * ensure that reference to eager is valid after 13861 * we get out of eager's perimeter. So we do 13862 * an extra refhold. 13863 */ 13864 CONN_INC_REF(connp); 13865 13866 /* 13867 * The listener also exists because of the refhold 13868 * done in tcp_conn_request. Its possible that it 13869 * might have closed. We will check that once we 13870 * get inside listeners context. 13871 */ 13872 CONN_INC_REF(listener->tcp_connp); 13873 if (listener->tcp_connp->conn_sqp == 13874 connp->conn_sqp) { 13875 tcp_send_conn_ind(listener->tcp_connp, mp, 13876 listener->tcp_connp->conn_sqp); 13877 CONN_DEC_REF(listener->tcp_connp); 13878 } else if (!tcp->tcp_loopback) { 13879 squeue_fill(listener->tcp_connp->conn_sqp, mp, 13880 tcp_send_conn_ind, 13881 listener->tcp_connp, SQTAG_TCP_CONN_IND); 13882 } else { 13883 squeue_enter(listener->tcp_connp->conn_sqp, mp, 13884 tcp_send_conn_ind, listener->tcp_connp, 13885 SQTAG_TCP_CONN_IND); 13886 } 13887 } 13888 13889 if (tcp->tcp_active_open) { 13890 /* 13891 * We are seeing the final ack in the three way 13892 * hand shake of a active open'ed connection 13893 * so we must send up a T_CONN_CON 13894 */ 13895 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13896 freemsg(mp); 13897 return; 13898 } 13899 /* 13900 * Don't fuse the loopback endpoints for 13901 * simultaneous active opens. 13902 */ 13903 if (tcp->tcp_loopback) { 13904 TCP_STAT(tcp_fusion_unfusable); 13905 tcp->tcp_unfusable = B_TRUE; 13906 } 13907 } 13908 13909 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 13910 bytes_acked--; 13911 /* SYN was acked - making progress */ 13912 if (tcp->tcp_ipversion == IPV6_VERSION) 13913 tcp->tcp_ip_forward_progress = B_TRUE; 13914 13915 /* 13916 * If SYN was retransmitted, need to reset all 13917 * retransmission info as this segment will be 13918 * treated as a dup ACK. 13919 */ 13920 if (tcp->tcp_rexmit) { 13921 tcp->tcp_rexmit = B_FALSE; 13922 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13923 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13924 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13925 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13926 tcp->tcp_ms_we_have_waited = 0; 13927 tcp->tcp_cwnd = mss; 13928 } 13929 13930 /* 13931 * We set the send window to zero here. 13932 * This is needed if there is data to be 13933 * processed already on the queue. 13934 * Later (at swnd_update label), the 13935 * "new_swnd > tcp_swnd" condition is satisfied 13936 * the XMIT_NEEDED flag is set in the current 13937 * (SYN_RCVD) state. This ensures tcp_wput_data() is 13938 * called if there is already data on queue in 13939 * this state. 13940 */ 13941 tcp->tcp_swnd = 0; 13942 13943 if (new_swnd > tcp->tcp_max_swnd) 13944 tcp->tcp_max_swnd = new_swnd; 13945 tcp->tcp_swl1 = seg_seq; 13946 tcp->tcp_swl2 = seg_ack; 13947 tcp->tcp_state = TCPS_ESTABLISHED; 13948 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13949 13950 /* Fuse when both sides are in ESTABLISHED state */ 13951 if (tcp->tcp_loopback && do_tcp_fusion) 13952 tcp_fuse(tcp, iphdr, tcph); 13953 13954 } 13955 /* This code follows 4.4BSD-Lite2 mostly. */ 13956 if (bytes_acked < 0) 13957 goto est; 13958 13959 /* 13960 * If TCP is ECN capable and the congestion experience bit is 13961 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 13962 * done once per window (or more loosely, per RTT). 13963 */ 13964 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 13965 tcp->tcp_cwr = B_FALSE; 13966 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 13967 if (!tcp->tcp_cwr) { 13968 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 13969 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 13970 tcp->tcp_cwnd = npkt * mss; 13971 /* 13972 * If the cwnd is 0, use the timer to clock out 13973 * new segments. This is required by the ECN spec. 13974 */ 13975 if (npkt == 0) { 13976 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13977 /* 13978 * This makes sure that when the ACK comes 13979 * back, we will increase tcp_cwnd by 1 MSS. 13980 */ 13981 tcp->tcp_cwnd_cnt = 0; 13982 } 13983 tcp->tcp_cwr = B_TRUE; 13984 /* 13985 * This marks the end of the current window of in 13986 * flight data. That is why we don't use 13987 * tcp_suna + tcp_swnd. Only data in flight can 13988 * provide ECN info. 13989 */ 13990 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13991 tcp->tcp_ecn_cwr_sent = B_FALSE; 13992 } 13993 } 13994 13995 mp1 = tcp->tcp_xmit_head; 13996 if (bytes_acked == 0) { 13997 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 13998 int dupack_cnt; 13999 14000 BUMP_MIB(&tcp_mib, tcpInDupAck); 14001 /* 14002 * Fast retransmit. When we have seen exactly three 14003 * identical ACKs while we have unacked data 14004 * outstanding we take it as a hint that our peer 14005 * dropped something. 14006 * 14007 * If TCP is retransmitting, don't do fast retransmit. 14008 */ 14009 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14010 ! tcp->tcp_rexmit) { 14011 /* Do Limited Transmit */ 14012 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14013 tcp_dupack_fast_retransmit) { 14014 /* 14015 * RFC 3042 14016 * 14017 * What we need to do is temporarily 14018 * increase tcp_cwnd so that new 14019 * data can be sent if it is allowed 14020 * by the receive window (tcp_rwnd). 14021 * tcp_wput_data() will take care of 14022 * the rest. 14023 * 14024 * If the connection is SACK capable, 14025 * only do limited xmit when there 14026 * is SACK info. 14027 * 14028 * Note how tcp_cwnd is incremented. 14029 * The first dup ACK will increase 14030 * it by 1 MSS. The second dup ACK 14031 * will increase it by 2 MSS. This 14032 * means that only 1 new segment will 14033 * be sent for each dup ACK. 14034 */ 14035 if (tcp->tcp_unsent > 0 && 14036 (!tcp->tcp_snd_sack_ok || 14037 (tcp->tcp_snd_sack_ok && 14038 tcp->tcp_notsack_list != NULL))) { 14039 tcp->tcp_cwnd += mss << 14040 (tcp->tcp_dupack_cnt - 1); 14041 flags |= TH_LIMIT_XMIT; 14042 } 14043 } else if (dupack_cnt == 14044 tcp_dupack_fast_retransmit) { 14045 14046 /* 14047 * If we have reduced tcp_ssthresh 14048 * because of ECN, do not reduce it again 14049 * unless it is already one window of data 14050 * away. After one window of data, tcp_cwr 14051 * should then be cleared. Note that 14052 * for non ECN capable connection, tcp_cwr 14053 * should always be false. 14054 * 14055 * Adjust cwnd since the duplicate 14056 * ack indicates that a packet was 14057 * dropped (due to congestion.) 14058 */ 14059 if (!tcp->tcp_cwr) { 14060 npkt = ((tcp->tcp_snxt - 14061 tcp->tcp_suna) >> 1) / mss; 14062 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14063 mss; 14064 tcp->tcp_cwnd = (npkt + 14065 tcp->tcp_dupack_cnt) * mss; 14066 } 14067 if (tcp->tcp_ecn_ok) { 14068 tcp->tcp_cwr = B_TRUE; 14069 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14070 tcp->tcp_ecn_cwr_sent = B_FALSE; 14071 } 14072 14073 /* 14074 * We do Hoe's algorithm. Refer to her 14075 * paper "Improving the Start-up Behavior 14076 * of a Congestion Control Scheme for TCP," 14077 * appeared in SIGCOMM'96. 14078 * 14079 * Save highest seq no we have sent so far. 14080 * Be careful about the invisible FIN byte. 14081 */ 14082 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14083 (tcp->tcp_unsent == 0)) { 14084 tcp->tcp_rexmit_max = tcp->tcp_fss; 14085 } else { 14086 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14087 } 14088 14089 /* 14090 * Do not allow bursty traffic during. 14091 * fast recovery. Refer to Fall and Floyd's 14092 * paper "Simulation-based Comparisons of 14093 * Tahoe, Reno and SACK TCP" (in CCR?) 14094 * This is a best current practise. 14095 */ 14096 tcp->tcp_snd_burst = TCP_CWND_SS; 14097 14098 /* 14099 * For SACK: 14100 * Calculate tcp_pipe, which is the 14101 * estimated number of bytes in 14102 * network. 14103 * 14104 * tcp_fack is the highest sack'ed seq num 14105 * TCP has received. 14106 * 14107 * tcp_pipe is explained in the above quoted 14108 * Fall and Floyd's paper. tcp_fack is 14109 * explained in Mathis and Mahdavi's 14110 * "Forward Acknowledgment: Refining TCP 14111 * Congestion Control" in SIGCOMM '96. 14112 */ 14113 if (tcp->tcp_snd_sack_ok) { 14114 ASSERT(tcp->tcp_sack_info != NULL); 14115 if (tcp->tcp_notsack_list != NULL) { 14116 tcp->tcp_pipe = tcp->tcp_snxt - 14117 tcp->tcp_fack; 14118 tcp->tcp_sack_snxt = seg_ack; 14119 flags |= TH_NEED_SACK_REXMIT; 14120 } else { 14121 /* 14122 * Always initialize tcp_pipe 14123 * even though we don't have 14124 * any SACK info. If later 14125 * we get SACK info and 14126 * tcp_pipe is not initialized, 14127 * funny things will happen. 14128 */ 14129 tcp->tcp_pipe = 14130 tcp->tcp_cwnd_ssthresh; 14131 } 14132 } else { 14133 flags |= TH_REXMIT_NEEDED; 14134 } /* tcp_snd_sack_ok */ 14135 14136 } else { 14137 /* 14138 * Here we perform congestion 14139 * avoidance, but NOT slow start. 14140 * This is known as the Fast 14141 * Recovery Algorithm. 14142 */ 14143 if (tcp->tcp_snd_sack_ok && 14144 tcp->tcp_notsack_list != NULL) { 14145 flags |= TH_NEED_SACK_REXMIT; 14146 tcp->tcp_pipe -= mss; 14147 if (tcp->tcp_pipe < 0) 14148 tcp->tcp_pipe = 0; 14149 } else { 14150 /* 14151 * We know that one more packet has 14152 * left the pipe thus we can update 14153 * cwnd. 14154 */ 14155 cwnd = tcp->tcp_cwnd + mss; 14156 if (cwnd > tcp->tcp_cwnd_max) 14157 cwnd = tcp->tcp_cwnd_max; 14158 tcp->tcp_cwnd = cwnd; 14159 if (tcp->tcp_unsent > 0) 14160 flags |= TH_XMIT_NEEDED; 14161 } 14162 } 14163 } 14164 } else if (tcp->tcp_zero_win_probe) { 14165 /* 14166 * If the window has opened, need to arrange 14167 * to send additional data. 14168 */ 14169 if (new_swnd != 0) { 14170 /* tcp_suna != tcp_snxt */ 14171 /* Packet contains a window update */ 14172 BUMP_MIB(&tcp_mib, tcpInWinUpdate); 14173 tcp->tcp_zero_win_probe = 0; 14174 tcp->tcp_timer_backoff = 0; 14175 tcp->tcp_ms_we_have_waited = 0; 14176 14177 /* 14178 * Transmit starting with tcp_suna since 14179 * the one byte probe is not ack'ed. 14180 * If TCP has sent more than one identical 14181 * probe, tcp_rexmit will be set. That means 14182 * tcp_ss_rexmit() will send out the one 14183 * byte along with new data. Otherwise, 14184 * fake the retransmission. 14185 */ 14186 flags |= TH_XMIT_NEEDED; 14187 if (!tcp->tcp_rexmit) { 14188 tcp->tcp_rexmit = B_TRUE; 14189 tcp->tcp_dupack_cnt = 0; 14190 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14191 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14192 } 14193 } 14194 } 14195 goto swnd_update; 14196 } 14197 14198 /* 14199 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14200 * If the ACK value acks something that we have not yet sent, it might 14201 * be an old duplicate segment. Send an ACK to re-synchronize the 14202 * other side. 14203 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14204 * state is handled above, so we can always just drop the segment and 14205 * send an ACK here. 14206 * 14207 * Should we send ACKs in response to ACK only segments? 14208 */ 14209 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14210 BUMP_MIB(&tcp_mib, tcpInAckUnsent); 14211 /* drop the received segment */ 14212 freemsg(mp); 14213 14214 /* 14215 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14216 * greater than 0, check if the number of such 14217 * bogus ACks is greater than that count. If yes, 14218 * don't send back any ACK. This prevents TCP from 14219 * getting into an ACK storm if somehow an attacker 14220 * successfully spoofs an acceptable segment to our 14221 * peer. 14222 */ 14223 if (tcp_drop_ack_unsent_cnt > 0 && 14224 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14225 TCP_STAT(tcp_in_ack_unsent_drop); 14226 return; 14227 } 14228 mp = tcp_ack_mp(tcp); 14229 if (mp != NULL) { 14230 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 14231 BUMP_LOCAL(tcp->tcp_obsegs); 14232 BUMP_MIB(&tcp_mib, tcpOutAck); 14233 tcp_send_data(tcp, tcp->tcp_wq, mp); 14234 } 14235 return; 14236 } 14237 14238 /* 14239 * TCP gets a new ACK, update the notsack'ed list to delete those 14240 * blocks that are covered by this ACK. 14241 */ 14242 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14243 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14244 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14245 } 14246 14247 /* 14248 * If we got an ACK after fast retransmit, check to see 14249 * if it is a partial ACK. If it is not and the congestion 14250 * window was inflated to account for the other side's 14251 * cached packets, retract it. If it is, do Hoe's algorithm. 14252 */ 14253 if (tcp->tcp_dupack_cnt >= tcp_dupack_fast_retransmit) { 14254 ASSERT(tcp->tcp_rexmit == B_FALSE); 14255 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14256 tcp->tcp_dupack_cnt = 0; 14257 /* 14258 * Restore the orig tcp_cwnd_ssthresh after 14259 * fast retransmit phase. 14260 */ 14261 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14262 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14263 } 14264 tcp->tcp_rexmit_max = seg_ack; 14265 tcp->tcp_cwnd_cnt = 0; 14266 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14267 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14268 14269 /* 14270 * Remove all notsack info to avoid confusion with 14271 * the next fast retrasnmit/recovery phase. 14272 */ 14273 if (tcp->tcp_snd_sack_ok && 14274 tcp->tcp_notsack_list != NULL) { 14275 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14276 } 14277 } else { 14278 if (tcp->tcp_snd_sack_ok && 14279 tcp->tcp_notsack_list != NULL) { 14280 flags |= TH_NEED_SACK_REXMIT; 14281 tcp->tcp_pipe -= mss; 14282 if (tcp->tcp_pipe < 0) 14283 tcp->tcp_pipe = 0; 14284 } else { 14285 /* 14286 * Hoe's algorithm: 14287 * 14288 * Retransmit the unack'ed segment and 14289 * restart fast recovery. Note that we 14290 * need to scale back tcp_cwnd to the 14291 * original value when we started fast 14292 * recovery. This is to prevent overly 14293 * aggressive behaviour in sending new 14294 * segments. 14295 */ 14296 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14297 tcp_dupack_fast_retransmit * mss; 14298 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14299 flags |= TH_REXMIT_NEEDED; 14300 } 14301 } 14302 } else { 14303 tcp->tcp_dupack_cnt = 0; 14304 if (tcp->tcp_rexmit) { 14305 /* 14306 * TCP is retranmitting. If the ACK ack's all 14307 * outstanding data, update tcp_rexmit_max and 14308 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14309 * to the correct value. 14310 * 14311 * Note that SEQ_LEQ() is used. This is to avoid 14312 * unnecessary fast retransmit caused by dup ACKs 14313 * received when TCP does slow start retransmission 14314 * after a time out. During this phase, TCP may 14315 * send out segments which are already received. 14316 * This causes dup ACKs to be sent back. 14317 */ 14318 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14319 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14320 tcp->tcp_rexmit_nxt = seg_ack; 14321 } 14322 if (seg_ack != tcp->tcp_rexmit_max) { 14323 flags |= TH_XMIT_NEEDED; 14324 } 14325 } else { 14326 tcp->tcp_rexmit = B_FALSE; 14327 tcp->tcp_xmit_zc_clean = B_FALSE; 14328 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14329 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14330 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14331 } 14332 tcp->tcp_ms_we_have_waited = 0; 14333 } 14334 } 14335 14336 BUMP_MIB(&tcp_mib, tcpInAckSegs); 14337 UPDATE_MIB(&tcp_mib, tcpInAckBytes, bytes_acked); 14338 tcp->tcp_suna = seg_ack; 14339 if (tcp->tcp_zero_win_probe != 0) { 14340 tcp->tcp_zero_win_probe = 0; 14341 tcp->tcp_timer_backoff = 0; 14342 } 14343 14344 /* 14345 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14346 * Note that it cannot be the SYN being ack'ed. The code flow 14347 * will not reach here. 14348 */ 14349 if (mp1 == NULL) { 14350 goto fin_acked; 14351 } 14352 14353 /* 14354 * Update the congestion window. 14355 * 14356 * If TCP is not ECN capable or TCP is ECN capable but the 14357 * congestion experience bit is not set, increase the tcp_cwnd as 14358 * usual. 14359 */ 14360 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14361 cwnd = tcp->tcp_cwnd; 14362 add = mss; 14363 14364 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14365 /* 14366 * This is to prevent an increase of less than 1 MSS of 14367 * tcp_cwnd. With partial increase, tcp_wput_data() 14368 * may send out tinygrams in order to preserve mblk 14369 * boundaries. 14370 * 14371 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14372 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14373 * increased by 1 MSS for every RTTs. 14374 */ 14375 if (tcp->tcp_cwnd_cnt <= 0) { 14376 tcp->tcp_cwnd_cnt = cwnd + add; 14377 } else { 14378 tcp->tcp_cwnd_cnt -= add; 14379 add = 0; 14380 } 14381 } 14382 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14383 } 14384 14385 /* See if the latest urgent data has been acknowledged */ 14386 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14387 SEQ_GT(seg_ack, tcp->tcp_urg)) 14388 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14389 14390 /* Can we update the RTT estimates? */ 14391 if (tcp->tcp_snd_ts_ok) { 14392 /* Ignore zero timestamp echo-reply. */ 14393 if (tcpopt.tcp_opt_ts_ecr != 0) { 14394 tcp_set_rto(tcp, (int32_t)lbolt - 14395 (int32_t)tcpopt.tcp_opt_ts_ecr); 14396 } 14397 14398 /* If needed, restart the timer. */ 14399 if (tcp->tcp_set_timer == 1) { 14400 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14401 tcp->tcp_set_timer = 0; 14402 } 14403 /* 14404 * Update tcp_csuna in case the other side stops sending 14405 * us timestamps. 14406 */ 14407 tcp->tcp_csuna = tcp->tcp_snxt; 14408 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14409 /* 14410 * An ACK sequence we haven't seen before, so get the RTT 14411 * and update the RTO. But first check if the timestamp is 14412 * valid to use. 14413 */ 14414 if ((mp1->b_next != NULL) && 14415 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14416 tcp_set_rto(tcp, (int32_t)lbolt - 14417 (int32_t)(intptr_t)mp1->b_prev); 14418 else 14419 BUMP_MIB(&tcp_mib, tcpRttNoUpdate); 14420 14421 /* Remeber the last sequence to be ACKed */ 14422 tcp->tcp_csuna = seg_ack; 14423 if (tcp->tcp_set_timer == 1) { 14424 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14425 tcp->tcp_set_timer = 0; 14426 } 14427 } else { 14428 BUMP_MIB(&tcp_mib, tcpRttNoUpdate); 14429 } 14430 14431 /* Eat acknowledged bytes off the xmit queue. */ 14432 for (;;) { 14433 mblk_t *mp2; 14434 uchar_t *wptr; 14435 14436 wptr = mp1->b_wptr; 14437 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14438 bytes_acked -= (int)(wptr - mp1->b_rptr); 14439 if (bytes_acked < 0) { 14440 mp1->b_rptr = wptr + bytes_acked; 14441 /* 14442 * Set a new timestamp if all the bytes timed by the 14443 * old timestamp have been ack'ed. 14444 */ 14445 if (SEQ_GT(seg_ack, 14446 (uint32_t)(uintptr_t)(mp1->b_next))) { 14447 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14448 mp1->b_next = NULL; 14449 } 14450 break; 14451 } 14452 mp1->b_next = NULL; 14453 mp1->b_prev = NULL; 14454 mp2 = mp1; 14455 mp1 = mp1->b_cont; 14456 14457 /* 14458 * This notification is required for some zero-copy 14459 * clients to maintain a copy semantic. After the data 14460 * is ack'ed, client is safe to modify or reuse the buffer. 14461 */ 14462 if (tcp->tcp_snd_zcopy_aware && 14463 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14464 tcp_zcopy_notify(tcp); 14465 freeb(mp2); 14466 if (bytes_acked == 0) { 14467 if (mp1 == NULL) { 14468 /* Everything is ack'ed, clear the tail. */ 14469 tcp->tcp_xmit_tail = NULL; 14470 /* 14471 * Cancel the timer unless we are still 14472 * waiting for an ACK for the FIN packet. 14473 */ 14474 if (tcp->tcp_timer_tid != 0 && 14475 tcp->tcp_snxt == tcp->tcp_suna) { 14476 (void) TCP_TIMER_CANCEL(tcp, 14477 tcp->tcp_timer_tid); 14478 tcp->tcp_timer_tid = 0; 14479 } 14480 goto pre_swnd_update; 14481 } 14482 if (mp2 != tcp->tcp_xmit_tail) 14483 break; 14484 tcp->tcp_xmit_tail = mp1; 14485 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14486 (uintptr_t)INT_MAX); 14487 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14488 mp1->b_rptr); 14489 break; 14490 } 14491 if (mp1 == NULL) { 14492 /* 14493 * More was acked but there is nothing more 14494 * outstanding. This means that the FIN was 14495 * just acked or that we're talking to a clown. 14496 */ 14497 fin_acked: 14498 ASSERT(tcp->tcp_fin_sent); 14499 tcp->tcp_xmit_tail = NULL; 14500 if (tcp->tcp_fin_sent) { 14501 /* FIN was acked - making progress */ 14502 if (tcp->tcp_ipversion == IPV6_VERSION && 14503 !tcp->tcp_fin_acked) 14504 tcp->tcp_ip_forward_progress = B_TRUE; 14505 tcp->tcp_fin_acked = B_TRUE; 14506 if (tcp->tcp_linger_tid != 0 && 14507 TCP_TIMER_CANCEL(tcp, 14508 tcp->tcp_linger_tid) >= 0) { 14509 tcp_stop_lingering(tcp); 14510 } 14511 } else { 14512 /* 14513 * We should never get here because 14514 * we have already checked that the 14515 * number of bytes ack'ed should be 14516 * smaller than or equal to what we 14517 * have sent so far (it is the 14518 * acceptability check of the ACK). 14519 * We can only get here if the send 14520 * queue is corrupted. 14521 * 14522 * Terminate the connection and 14523 * panic the system. It is better 14524 * for us to panic instead of 14525 * continuing to avoid other disaster. 14526 */ 14527 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14528 tcp->tcp_rnxt, TH_RST|TH_ACK); 14529 panic("Memory corruption " 14530 "detected for connection %s.", 14531 tcp_display(tcp, NULL, 14532 DISP_ADDR_AND_PORT)); 14533 /*NOTREACHED*/ 14534 } 14535 goto pre_swnd_update; 14536 } 14537 ASSERT(mp2 != tcp->tcp_xmit_tail); 14538 } 14539 if (tcp->tcp_unsent) { 14540 flags |= TH_XMIT_NEEDED; 14541 } 14542 pre_swnd_update: 14543 tcp->tcp_xmit_head = mp1; 14544 swnd_update: 14545 /* 14546 * The following check is different from most other implementations. 14547 * For bi-directional transfer, when segments are dropped, the 14548 * "normal" check will not accept a window update in those 14549 * retransmitted segemnts. Failing to do that, TCP may send out 14550 * segments which are outside receiver's window. As TCP accepts 14551 * the ack in those retransmitted segments, if the window update in 14552 * the same segment is not accepted, TCP will incorrectly calculates 14553 * that it can send more segments. This can create a deadlock 14554 * with the receiver if its window becomes zero. 14555 */ 14556 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14557 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14558 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14559 /* 14560 * The criteria for update is: 14561 * 14562 * 1. the segment acknowledges some data. Or 14563 * 2. the segment is new, i.e. it has a higher seq num. Or 14564 * 3. the segment is not old and the advertised window is 14565 * larger than the previous advertised window. 14566 */ 14567 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14568 flags |= TH_XMIT_NEEDED; 14569 tcp->tcp_swnd = new_swnd; 14570 if (new_swnd > tcp->tcp_max_swnd) 14571 tcp->tcp_max_swnd = new_swnd; 14572 tcp->tcp_swl1 = seg_seq; 14573 tcp->tcp_swl2 = seg_ack; 14574 } 14575 est: 14576 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14577 14578 switch (tcp->tcp_state) { 14579 case TCPS_FIN_WAIT_1: 14580 if (tcp->tcp_fin_acked) { 14581 tcp->tcp_state = TCPS_FIN_WAIT_2; 14582 /* 14583 * We implement the non-standard BSD/SunOS 14584 * FIN_WAIT_2 flushing algorithm. 14585 * If there is no user attached to this 14586 * TCP endpoint, then this TCP struct 14587 * could hang around forever in FIN_WAIT_2 14588 * state if the peer forgets to send us 14589 * a FIN. To prevent this, we wait only 14590 * 2*MSL (a convenient time value) for 14591 * the FIN to arrive. If it doesn't show up, 14592 * we flush the TCP endpoint. This algorithm, 14593 * though a violation of RFC-793, has worked 14594 * for over 10 years in BSD systems. 14595 * Note: SunOS 4.x waits 675 seconds before 14596 * flushing the FIN_WAIT_2 connection. 14597 */ 14598 TCP_TIMER_RESTART(tcp, 14599 tcp_fin_wait_2_flush_interval); 14600 } 14601 break; 14602 case TCPS_FIN_WAIT_2: 14603 break; /* Shutdown hook? */ 14604 case TCPS_LAST_ACK: 14605 freemsg(mp); 14606 if (tcp->tcp_fin_acked) { 14607 (void) tcp_clean_death(tcp, 0, 19); 14608 return; 14609 } 14610 goto xmit_check; 14611 case TCPS_CLOSING: 14612 if (tcp->tcp_fin_acked) { 14613 tcp->tcp_state = TCPS_TIME_WAIT; 14614 /* 14615 * Unconditionally clear the exclusive binding 14616 * bit so this TIME-WAIT connection won't 14617 * interfere with new ones. 14618 */ 14619 tcp->tcp_exclbind = 0; 14620 if (!TCP_IS_DETACHED(tcp)) { 14621 TCP_TIMER_RESTART(tcp, 14622 tcp_time_wait_interval); 14623 } else { 14624 tcp_time_wait_append(tcp); 14625 TCP_DBGSTAT(tcp_rput_time_wait); 14626 } 14627 } 14628 /*FALLTHRU*/ 14629 case TCPS_CLOSE_WAIT: 14630 freemsg(mp); 14631 goto xmit_check; 14632 default: 14633 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14634 break; 14635 } 14636 } 14637 if (flags & TH_FIN) { 14638 /* Make sure we ack the fin */ 14639 flags |= TH_ACK_NEEDED; 14640 if (!tcp->tcp_fin_rcvd) { 14641 tcp->tcp_fin_rcvd = B_TRUE; 14642 tcp->tcp_rnxt++; 14643 tcph = tcp->tcp_tcph; 14644 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14645 14646 /* 14647 * Generate the ordrel_ind at the end unless we 14648 * are an eager guy. 14649 * In the eager case tcp_rsrv will do this when run 14650 * after tcp_accept is done. 14651 */ 14652 if (tcp->tcp_listener == NULL && 14653 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14654 flags |= TH_ORDREL_NEEDED; 14655 switch (tcp->tcp_state) { 14656 case TCPS_SYN_RCVD: 14657 case TCPS_ESTABLISHED: 14658 tcp->tcp_state = TCPS_CLOSE_WAIT; 14659 /* Keepalive? */ 14660 break; 14661 case TCPS_FIN_WAIT_1: 14662 if (!tcp->tcp_fin_acked) { 14663 tcp->tcp_state = TCPS_CLOSING; 14664 break; 14665 } 14666 /* FALLTHRU */ 14667 case TCPS_FIN_WAIT_2: 14668 tcp->tcp_state = TCPS_TIME_WAIT; 14669 /* 14670 * Unconditionally clear the exclusive binding 14671 * bit so this TIME-WAIT connection won't 14672 * interfere with new ones. 14673 */ 14674 tcp->tcp_exclbind = 0; 14675 if (!TCP_IS_DETACHED(tcp)) { 14676 TCP_TIMER_RESTART(tcp, 14677 tcp_time_wait_interval); 14678 } else { 14679 tcp_time_wait_append(tcp); 14680 TCP_DBGSTAT(tcp_rput_time_wait); 14681 } 14682 if (seg_len) { 14683 /* 14684 * implies data piggybacked on FIN. 14685 * break to handle data. 14686 */ 14687 break; 14688 } 14689 freemsg(mp); 14690 goto ack_check; 14691 } 14692 } 14693 } 14694 if (mp == NULL) 14695 goto xmit_check; 14696 if (seg_len == 0) { 14697 freemsg(mp); 14698 goto xmit_check; 14699 } 14700 if (mp->b_rptr == mp->b_wptr) { 14701 /* 14702 * The header has been consumed, so we remove the 14703 * zero-length mblk here. 14704 */ 14705 mp1 = mp; 14706 mp = mp->b_cont; 14707 freeb(mp1); 14708 } 14709 tcph = tcp->tcp_tcph; 14710 tcp->tcp_rack_cnt++; 14711 { 14712 uint32_t cur_max; 14713 14714 cur_max = tcp->tcp_rack_cur_max; 14715 if (tcp->tcp_rack_cnt >= cur_max) { 14716 /* 14717 * We have more unacked data than we should - send 14718 * an ACK now. 14719 */ 14720 flags |= TH_ACK_NEEDED; 14721 cur_max++; 14722 if (cur_max > tcp->tcp_rack_abs_max) 14723 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14724 else 14725 tcp->tcp_rack_cur_max = cur_max; 14726 } else if (TCP_IS_DETACHED(tcp)) { 14727 /* We don't have an ACK timer for detached TCP. */ 14728 flags |= TH_ACK_NEEDED; 14729 } else if (seg_len < mss) { 14730 /* 14731 * If we get a segment that is less than an mss, and we 14732 * already have unacknowledged data, and the amount 14733 * unacknowledged is not a multiple of mss, then we 14734 * better generate an ACK now. Otherwise, this may be 14735 * the tail piece of a transaction, and we would rather 14736 * wait for the response. 14737 */ 14738 uint32_t udif; 14739 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14740 (uintptr_t)INT_MAX); 14741 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14742 if (udif && (udif % mss)) 14743 flags |= TH_ACK_NEEDED; 14744 else 14745 flags |= TH_ACK_TIMER_NEEDED; 14746 } else { 14747 /* Start delayed ack timer */ 14748 flags |= TH_ACK_TIMER_NEEDED; 14749 } 14750 } 14751 tcp->tcp_rnxt += seg_len; 14752 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14753 14754 /* Update SACK list */ 14755 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14756 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14757 &(tcp->tcp_num_sack_blk)); 14758 } 14759 14760 if (tcp->tcp_urp_mp) { 14761 tcp->tcp_urp_mp->b_cont = mp; 14762 mp = tcp->tcp_urp_mp; 14763 tcp->tcp_urp_mp = NULL; 14764 /* Ready for a new signal. */ 14765 tcp->tcp_urp_last_valid = B_FALSE; 14766 #ifdef DEBUG 14767 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14768 "tcp_rput: sending exdata_ind %s", 14769 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14770 #endif /* DEBUG */ 14771 } 14772 14773 /* 14774 * Check for ancillary data changes compared to last segment. 14775 */ 14776 if (tcp->tcp_ipv6_recvancillary != 0) { 14777 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14778 if (mp == NULL) 14779 return; 14780 } 14781 14782 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14783 /* 14784 * Side queue inbound data until the accept happens. 14785 * tcp_accept/tcp_rput drains this when the accept happens. 14786 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14787 * T_EXDATA_IND) it is queued on b_next. 14788 * XXX Make urgent data use this. Requires: 14789 * Removing tcp_listener check for TH_URG 14790 * Making M_PCPROTO and MARK messages skip the eager case 14791 */ 14792 14793 if (tcp->tcp_kssl_pending) { 14794 tcp_kssl_input(tcp, mp); 14795 } else { 14796 tcp_rcv_enqueue(tcp, mp, seg_len); 14797 } 14798 } else { 14799 if (mp->b_datap->db_type != M_DATA || 14800 (flags & TH_MARKNEXT_NEEDED)) { 14801 if (tcp->tcp_rcv_list != NULL) { 14802 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14803 } 14804 ASSERT(tcp->tcp_rcv_list == NULL || 14805 tcp->tcp_fused_sigurg); 14806 if (flags & TH_MARKNEXT_NEEDED) { 14807 #ifdef DEBUG 14808 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14809 "tcp_rput: sending MSGMARKNEXT %s", 14810 tcp_display(tcp, NULL, 14811 DISP_PORT_ONLY)); 14812 #endif /* DEBUG */ 14813 mp->b_flag |= MSGMARKNEXT; 14814 flags &= ~TH_MARKNEXT_NEEDED; 14815 } 14816 14817 /* Does this need SSL processing first? */ 14818 if ((tcp->tcp_kssl_ctx != NULL) && 14819 (DB_TYPE(mp) == M_DATA)) { 14820 tcp_kssl_input(tcp, mp); 14821 } else { 14822 putnext(tcp->tcp_rq, mp); 14823 if (!canputnext(tcp->tcp_rq)) 14824 tcp->tcp_rwnd -= seg_len; 14825 } 14826 } else if ((flags & (TH_PUSH|TH_FIN)) || 14827 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 14828 if (tcp->tcp_rcv_list != NULL) { 14829 /* 14830 * Enqueue the new segment first and then 14831 * call tcp_rcv_drain() to send all data 14832 * up. The other way to do this is to 14833 * send all queued data up and then call 14834 * putnext() to send the new segment up. 14835 * This way can remove the else part later 14836 * on. 14837 * 14838 * We don't this to avoid one more call to 14839 * canputnext() as tcp_rcv_drain() needs to 14840 * call canputnext(). 14841 */ 14842 tcp_rcv_enqueue(tcp, mp, seg_len); 14843 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14844 } else { 14845 /* Does this need SSL processing first? */ 14846 if ((tcp->tcp_kssl_ctx != NULL) && 14847 (DB_TYPE(mp) == M_DATA)) { 14848 tcp_kssl_input(tcp, mp); 14849 } else { 14850 putnext(tcp->tcp_rq, mp); 14851 if (!canputnext(tcp->tcp_rq)) 14852 tcp->tcp_rwnd -= seg_len; 14853 } 14854 } 14855 } else { 14856 /* 14857 * Enqueue all packets when processing an mblk 14858 * from the co queue and also enqueue normal packets. 14859 */ 14860 tcp_rcv_enqueue(tcp, mp, seg_len); 14861 } 14862 /* 14863 * Make sure the timer is running if we have data waiting 14864 * for a push bit. This provides resiliency against 14865 * implementations that do not correctly generate push bits. 14866 */ 14867 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 14868 /* 14869 * The connection may be closed at this point, so don't 14870 * do anything for a detached tcp. 14871 */ 14872 if (!TCP_IS_DETACHED(tcp)) 14873 tcp->tcp_push_tid = TCP_TIMER(tcp, 14874 tcp_push_timer, 14875 MSEC_TO_TICK(tcp_push_timer_interval)); 14876 } 14877 } 14878 xmit_check: 14879 /* Is there anything left to do? */ 14880 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14881 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 14882 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 14883 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14884 goto done; 14885 14886 /* Any transmit work to do and a non-zero window? */ 14887 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 14888 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 14889 if (flags & TH_REXMIT_NEEDED) { 14890 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 14891 14892 BUMP_MIB(&tcp_mib, tcpOutFastRetrans); 14893 if (snd_size > mss) 14894 snd_size = mss; 14895 if (snd_size > tcp->tcp_swnd) 14896 snd_size = tcp->tcp_swnd; 14897 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 14898 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 14899 B_TRUE); 14900 14901 if (mp1 != NULL) { 14902 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14903 tcp->tcp_csuna = tcp->tcp_snxt; 14904 BUMP_MIB(&tcp_mib, tcpRetransSegs); 14905 UPDATE_MIB(&tcp_mib, tcpRetransBytes, snd_size); 14906 TCP_RECORD_TRACE(tcp, mp1, 14907 TCP_TRACE_SEND_PKT); 14908 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14909 } 14910 } 14911 if (flags & TH_NEED_SACK_REXMIT) { 14912 tcp_sack_rxmit(tcp, &flags); 14913 } 14914 /* 14915 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 14916 * out new segment. Note that tcp_rexmit should not be 14917 * set, otherwise TH_LIMIT_XMIT should not be set. 14918 */ 14919 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 14920 if (!tcp->tcp_rexmit) { 14921 tcp_wput_data(tcp, NULL, B_FALSE); 14922 } else { 14923 tcp_ss_rexmit(tcp); 14924 } 14925 } 14926 /* 14927 * Adjust tcp_cwnd back to normal value after sending 14928 * new data segments. 14929 */ 14930 if (flags & TH_LIMIT_XMIT) { 14931 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 14932 /* 14933 * This will restart the timer. Restarting the 14934 * timer is used to avoid a timeout before the 14935 * limited transmitted segment's ACK gets back. 14936 */ 14937 if (tcp->tcp_xmit_head != NULL) 14938 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14939 } 14940 14941 /* Anything more to do? */ 14942 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 14943 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14944 goto done; 14945 } 14946 ack_check: 14947 if (flags & TH_SEND_URP_MARK) { 14948 ASSERT(tcp->tcp_urp_mark_mp); 14949 /* 14950 * Send up any queued data and then send the mark message 14951 */ 14952 if (tcp->tcp_rcv_list != NULL) { 14953 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14954 } 14955 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14956 14957 mp1 = tcp->tcp_urp_mark_mp; 14958 tcp->tcp_urp_mark_mp = NULL; 14959 #ifdef DEBUG 14960 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14961 "tcp_rput: sending zero-length %s %s", 14962 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 14963 "MSGNOTMARKNEXT"), 14964 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14965 #endif /* DEBUG */ 14966 putnext(tcp->tcp_rq, mp1); 14967 flags &= ~TH_SEND_URP_MARK; 14968 } 14969 if (flags & TH_ACK_NEEDED) { 14970 /* 14971 * Time to send an ack for some reason. 14972 */ 14973 mp1 = tcp_ack_mp(tcp); 14974 14975 if (mp1 != NULL) { 14976 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 14977 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14978 BUMP_LOCAL(tcp->tcp_obsegs); 14979 BUMP_MIB(&tcp_mib, tcpOutAck); 14980 } 14981 if (tcp->tcp_ack_tid != 0) { 14982 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 14983 tcp->tcp_ack_tid = 0; 14984 } 14985 } 14986 if (flags & TH_ACK_TIMER_NEEDED) { 14987 /* 14988 * Arrange for deferred ACK or push wait timeout. 14989 * Start timer if it is not already running. 14990 */ 14991 if (tcp->tcp_ack_tid == 0) { 14992 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 14993 MSEC_TO_TICK(tcp->tcp_localnet ? 14994 (clock_t)tcp_local_dack_interval : 14995 (clock_t)tcp_deferred_ack_interval)); 14996 } 14997 } 14998 if (flags & TH_ORDREL_NEEDED) { 14999 /* 15000 * Send up the ordrel_ind unless we are an eager guy. 15001 * In the eager case tcp_rsrv will do this when run 15002 * after tcp_accept is done. 15003 */ 15004 ASSERT(tcp->tcp_listener == NULL); 15005 if (tcp->tcp_rcv_list != NULL) { 15006 /* 15007 * Push any mblk(s) enqueued from co processing. 15008 */ 15009 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15010 } 15011 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15012 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 15013 tcp->tcp_ordrel_done = B_TRUE; 15014 putnext(tcp->tcp_rq, mp1); 15015 if (tcp->tcp_deferred_clean_death) { 15016 /* 15017 * tcp_clean_death was deferred 15018 * for T_ORDREL_IND - do it now 15019 */ 15020 (void) tcp_clean_death(tcp, 15021 tcp->tcp_client_errno, 20); 15022 tcp->tcp_deferred_clean_death = B_FALSE; 15023 } 15024 } else { 15025 /* 15026 * Run the orderly release in the 15027 * service routine. 15028 */ 15029 qenable(tcp->tcp_rq); 15030 /* 15031 * Caveat(XXX): The machine may be so 15032 * overloaded that tcp_rsrv() is not scheduled 15033 * until after the endpoint has transitioned 15034 * to TCPS_TIME_WAIT 15035 * and tcp_time_wait_interval expires. Then 15036 * tcp_timer() will blow away state in tcp_t 15037 * and T_ORDREL_IND will never be delivered 15038 * upstream. Unlikely but potentially 15039 * a problem. 15040 */ 15041 } 15042 } 15043 done: 15044 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15045 } 15046 15047 /* 15048 * This function does PAWS protection check. Returns B_TRUE if the 15049 * segment passes the PAWS test, else returns B_FALSE. 15050 */ 15051 boolean_t 15052 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15053 { 15054 uint8_t flags; 15055 int options; 15056 uint8_t *up; 15057 15058 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15059 /* 15060 * If timestamp option is aligned nicely, get values inline, 15061 * otherwise call general routine to parse. Only do that 15062 * if timestamp is the only option. 15063 */ 15064 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15065 TCPOPT_REAL_TS_LEN && 15066 OK_32PTR((up = ((uint8_t *)tcph) + 15067 TCP_MIN_HEADER_LENGTH)) && 15068 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15069 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15070 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15071 15072 options = TCP_OPT_TSTAMP_PRESENT; 15073 } else { 15074 if (tcp->tcp_snd_sack_ok) { 15075 tcpoptp->tcp = tcp; 15076 } else { 15077 tcpoptp->tcp = NULL; 15078 } 15079 options = tcp_parse_options(tcph, tcpoptp); 15080 } 15081 15082 if (options & TCP_OPT_TSTAMP_PRESENT) { 15083 /* 15084 * Do PAWS per RFC 1323 section 4.2. Accept RST 15085 * regardless of the timestamp, page 18 RFC 1323.bis. 15086 */ 15087 if ((flags & TH_RST) == 0 && 15088 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15089 tcp->tcp_ts_recent)) { 15090 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15091 PAWS_TIMEOUT)) { 15092 /* This segment is not acceptable. */ 15093 return (B_FALSE); 15094 } else { 15095 /* 15096 * Connection has been idle for 15097 * too long. Reset the timestamp 15098 * and assume the segment is valid. 15099 */ 15100 tcp->tcp_ts_recent = 15101 tcpoptp->tcp_opt_ts_val; 15102 } 15103 } 15104 } else { 15105 /* 15106 * If we don't get a timestamp on every packet, we 15107 * figure we can't really trust 'em, so we stop sending 15108 * and parsing them. 15109 */ 15110 tcp->tcp_snd_ts_ok = B_FALSE; 15111 15112 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15113 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15114 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15115 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN); 15116 if (tcp->tcp_snd_sack_ok) { 15117 ASSERT(tcp->tcp_sack_info != NULL); 15118 tcp->tcp_max_sack_blk = 4; 15119 } 15120 } 15121 return (B_TRUE); 15122 } 15123 15124 /* 15125 * Attach ancillary data to a received TCP segments for the 15126 * ancillary pieces requested by the application that are 15127 * different than they were in the previous data segment. 15128 * 15129 * Save the "current" values once memory allocation is ok so that 15130 * when memory allocation fails we can just wait for the next data segment. 15131 */ 15132 static mblk_t * 15133 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15134 { 15135 struct T_optdata_ind *todi; 15136 int optlen; 15137 uchar_t *optptr; 15138 struct T_opthdr *toh; 15139 uint_t addflag; /* Which pieces to add */ 15140 mblk_t *mp1; 15141 15142 optlen = 0; 15143 addflag = 0; 15144 /* If app asked for pktinfo and the index has changed ... */ 15145 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15146 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15147 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15148 optlen += sizeof (struct T_opthdr) + 15149 sizeof (struct in6_pktinfo); 15150 addflag |= TCP_IPV6_RECVPKTINFO; 15151 } 15152 /* If app asked for hoplimit and it has changed ... */ 15153 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15154 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15155 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15156 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15157 addflag |= TCP_IPV6_RECVHOPLIMIT; 15158 } 15159 /* If app asked for tclass and it has changed ... */ 15160 if ((ipp->ipp_fields & IPPF_TCLASS) && 15161 ipp->ipp_tclass != tcp->tcp_recvtclass && 15162 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15163 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15164 addflag |= TCP_IPV6_RECVTCLASS; 15165 } 15166 /* 15167 * If app asked for hopbyhop headers and it has changed ... 15168 * For security labels, note that (1) security labels can't change on 15169 * a connected socket at all, (2) we're connected to at most one peer, 15170 * (3) if anything changes, then it must be some other extra option. 15171 */ 15172 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15173 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15174 (ipp->ipp_fields & IPPF_HOPOPTS), 15175 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15176 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15177 tcp->tcp_label_len; 15178 addflag |= TCP_IPV6_RECVHOPOPTS; 15179 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15180 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15181 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15182 return (mp); 15183 } 15184 /* If app asked for dst headers before routing headers ... */ 15185 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15186 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15187 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15188 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15189 optlen += sizeof (struct T_opthdr) + 15190 ipp->ipp_rtdstoptslen; 15191 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15192 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15193 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15194 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15195 return (mp); 15196 } 15197 /* If app asked for routing headers and it has changed ... */ 15198 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15199 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15200 (ipp->ipp_fields & IPPF_RTHDR), 15201 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15202 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15203 addflag |= TCP_IPV6_RECVRTHDR; 15204 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15205 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15206 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15207 return (mp); 15208 } 15209 /* If app asked for dest headers and it has changed ... */ 15210 if ((tcp->tcp_ipv6_recvancillary & 15211 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15212 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15213 (ipp->ipp_fields & IPPF_DSTOPTS), 15214 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15215 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15216 addflag |= TCP_IPV6_RECVDSTOPTS; 15217 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15218 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15219 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15220 return (mp); 15221 } 15222 15223 if (optlen == 0) { 15224 /* Nothing to add */ 15225 return (mp); 15226 } 15227 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15228 if (mp1 == NULL) { 15229 /* 15230 * Defer sending ancillary data until the next TCP segment 15231 * arrives. 15232 */ 15233 return (mp); 15234 } 15235 mp1->b_cont = mp; 15236 mp = mp1; 15237 mp->b_wptr += sizeof (*todi) + optlen; 15238 mp->b_datap->db_type = M_PROTO; 15239 todi = (struct T_optdata_ind *)mp->b_rptr; 15240 todi->PRIM_type = T_OPTDATA_IND; 15241 todi->DATA_flag = 1; /* MORE data */ 15242 todi->OPT_length = optlen; 15243 todi->OPT_offset = sizeof (*todi); 15244 optptr = (uchar_t *)&todi[1]; 15245 /* 15246 * If app asked for pktinfo and the index has changed ... 15247 * Note that the local address never changes for the connection. 15248 */ 15249 if (addflag & TCP_IPV6_RECVPKTINFO) { 15250 struct in6_pktinfo *pkti; 15251 15252 toh = (struct T_opthdr *)optptr; 15253 toh->level = IPPROTO_IPV6; 15254 toh->name = IPV6_PKTINFO; 15255 toh->len = sizeof (*toh) + sizeof (*pkti); 15256 toh->status = 0; 15257 optptr += sizeof (*toh); 15258 pkti = (struct in6_pktinfo *)optptr; 15259 if (tcp->tcp_ipversion == IPV6_VERSION) 15260 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15261 else 15262 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15263 &pkti->ipi6_addr); 15264 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15265 optptr += sizeof (*pkti); 15266 ASSERT(OK_32PTR(optptr)); 15267 /* Save as "last" value */ 15268 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15269 } 15270 /* If app asked for hoplimit and it has changed ... */ 15271 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15272 toh = (struct T_opthdr *)optptr; 15273 toh->level = IPPROTO_IPV6; 15274 toh->name = IPV6_HOPLIMIT; 15275 toh->len = sizeof (*toh) + sizeof (uint_t); 15276 toh->status = 0; 15277 optptr += sizeof (*toh); 15278 *(uint_t *)optptr = ipp->ipp_hoplimit; 15279 optptr += sizeof (uint_t); 15280 ASSERT(OK_32PTR(optptr)); 15281 /* Save as "last" value */ 15282 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15283 } 15284 /* If app asked for tclass and it has changed ... */ 15285 if (addflag & TCP_IPV6_RECVTCLASS) { 15286 toh = (struct T_opthdr *)optptr; 15287 toh->level = IPPROTO_IPV6; 15288 toh->name = IPV6_TCLASS; 15289 toh->len = sizeof (*toh) + sizeof (uint_t); 15290 toh->status = 0; 15291 optptr += sizeof (*toh); 15292 *(uint_t *)optptr = ipp->ipp_tclass; 15293 optptr += sizeof (uint_t); 15294 ASSERT(OK_32PTR(optptr)); 15295 /* Save as "last" value */ 15296 tcp->tcp_recvtclass = ipp->ipp_tclass; 15297 } 15298 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15299 toh = (struct T_opthdr *)optptr; 15300 toh->level = IPPROTO_IPV6; 15301 toh->name = IPV6_HOPOPTS; 15302 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15303 tcp->tcp_label_len; 15304 toh->status = 0; 15305 optptr += sizeof (*toh); 15306 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15307 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15308 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15309 ASSERT(OK_32PTR(optptr)); 15310 /* Save as last value */ 15311 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15312 (ipp->ipp_fields & IPPF_HOPOPTS), 15313 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15314 } 15315 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15316 toh = (struct T_opthdr *)optptr; 15317 toh->level = IPPROTO_IPV6; 15318 toh->name = IPV6_RTHDRDSTOPTS; 15319 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15320 toh->status = 0; 15321 optptr += sizeof (*toh); 15322 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15323 optptr += ipp->ipp_rtdstoptslen; 15324 ASSERT(OK_32PTR(optptr)); 15325 /* Save as last value */ 15326 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15327 &tcp->tcp_rtdstoptslen, 15328 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15329 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15330 } 15331 if (addflag & TCP_IPV6_RECVRTHDR) { 15332 toh = (struct T_opthdr *)optptr; 15333 toh->level = IPPROTO_IPV6; 15334 toh->name = IPV6_RTHDR; 15335 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15336 toh->status = 0; 15337 optptr += sizeof (*toh); 15338 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15339 optptr += ipp->ipp_rthdrlen; 15340 ASSERT(OK_32PTR(optptr)); 15341 /* Save as last value */ 15342 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15343 (ipp->ipp_fields & IPPF_RTHDR), 15344 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15345 } 15346 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15347 toh = (struct T_opthdr *)optptr; 15348 toh->level = IPPROTO_IPV6; 15349 toh->name = IPV6_DSTOPTS; 15350 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15351 toh->status = 0; 15352 optptr += sizeof (*toh); 15353 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15354 optptr += ipp->ipp_dstoptslen; 15355 ASSERT(OK_32PTR(optptr)); 15356 /* Save as last value */ 15357 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15358 (ipp->ipp_fields & IPPF_DSTOPTS), 15359 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15360 } 15361 ASSERT(optptr == mp->b_wptr); 15362 return (mp); 15363 } 15364 15365 15366 /* 15367 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15368 * or a "bad" IRE detected by tcp_adapt_ire. 15369 * We can't tell if the failure was due to the laddr or the faddr 15370 * thus we clear out all addresses and ports. 15371 */ 15372 static void 15373 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15374 { 15375 queue_t *q = tcp->tcp_rq; 15376 tcph_t *tcph; 15377 struct T_error_ack *tea; 15378 conn_t *connp = tcp->tcp_connp; 15379 15380 15381 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15382 15383 if (mp->b_cont) { 15384 freemsg(mp->b_cont); 15385 mp->b_cont = NULL; 15386 } 15387 tea = (struct T_error_ack *)mp->b_rptr; 15388 switch (tea->PRIM_type) { 15389 case T_BIND_ACK: 15390 /* 15391 * Need to unbind with classifier since we were just told that 15392 * our bind succeeded. 15393 */ 15394 tcp->tcp_hard_bound = B_FALSE; 15395 tcp->tcp_hard_binding = B_FALSE; 15396 15397 ipcl_hash_remove(connp); 15398 /* Reuse the mblk if possible */ 15399 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15400 sizeof (*tea)); 15401 mp->b_rptr = mp->b_datap->db_base; 15402 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15403 tea = (struct T_error_ack *)mp->b_rptr; 15404 tea->PRIM_type = T_ERROR_ACK; 15405 tea->TLI_error = TSYSERR; 15406 tea->UNIX_error = error; 15407 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15408 tea->ERROR_prim = T_CONN_REQ; 15409 } else { 15410 tea->ERROR_prim = O_T_BIND_REQ; 15411 } 15412 break; 15413 15414 case T_ERROR_ACK: 15415 if (tcp->tcp_state >= TCPS_SYN_SENT) 15416 tea->ERROR_prim = T_CONN_REQ; 15417 break; 15418 default: 15419 panic("tcp_bind_failed: unexpected TPI type"); 15420 /*NOTREACHED*/ 15421 } 15422 15423 tcp->tcp_state = TCPS_IDLE; 15424 if (tcp->tcp_ipversion == IPV4_VERSION) 15425 tcp->tcp_ipha->ipha_src = 0; 15426 else 15427 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15428 /* 15429 * Copy of the src addr. in tcp_t is needed since 15430 * the lookup funcs. can only look at tcp_t 15431 */ 15432 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15433 15434 tcph = tcp->tcp_tcph; 15435 tcph->th_lport[0] = 0; 15436 tcph->th_lport[1] = 0; 15437 tcp_bind_hash_remove(tcp); 15438 bzero(&connp->u_port, sizeof (connp->u_port)); 15439 /* blow away saved option results if any */ 15440 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15441 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15442 15443 conn_delete_ire(tcp->tcp_connp, NULL); 15444 putnext(q, mp); 15445 } 15446 15447 /* 15448 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15449 * messages. 15450 */ 15451 void 15452 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15453 { 15454 mblk_t *mp1; 15455 uchar_t *rptr = mp->b_rptr; 15456 queue_t *q = tcp->tcp_rq; 15457 struct T_error_ack *tea; 15458 uint32_t mss; 15459 mblk_t *syn_mp; 15460 mblk_t *mdti; 15461 mblk_t *lsoi; 15462 int retval; 15463 mblk_t *ire_mp; 15464 15465 switch (mp->b_datap->db_type) { 15466 case M_PROTO: 15467 case M_PCPROTO: 15468 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15469 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15470 break; 15471 tea = (struct T_error_ack *)rptr; 15472 switch (tea->PRIM_type) { 15473 case T_BIND_ACK: 15474 /* 15475 * Adapt Multidata information, if any. The 15476 * following tcp_mdt_update routine will free 15477 * the message. 15478 */ 15479 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15480 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15481 b_rptr)->mdt_capab, B_TRUE); 15482 freemsg(mdti); 15483 } 15484 15485 /* 15486 * Check to update LSO information with tcp, and 15487 * tcp_lso_update routine will free the message. 15488 */ 15489 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 15490 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 15491 b_rptr)->lso_capab); 15492 freemsg(lsoi); 15493 } 15494 15495 /* Get the IRE, if we had requested for it */ 15496 ire_mp = tcp_ire_mp(mp); 15497 15498 if (tcp->tcp_hard_binding) { 15499 tcp->tcp_hard_binding = B_FALSE; 15500 tcp->tcp_hard_bound = B_TRUE; 15501 CL_INET_CONNECT(tcp); 15502 } else { 15503 if (ire_mp != NULL) 15504 freeb(ire_mp); 15505 goto after_syn_sent; 15506 } 15507 15508 retval = tcp_adapt_ire(tcp, ire_mp); 15509 if (ire_mp != NULL) 15510 freeb(ire_mp); 15511 if (retval == 0) { 15512 tcp_bind_failed(tcp, mp, 15513 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15514 ENETUNREACH : EADDRNOTAVAIL)); 15515 return; 15516 } 15517 /* 15518 * Don't let an endpoint connect to itself. 15519 * Also checked in tcp_connect() but that 15520 * check can't handle the case when the 15521 * local IP address is INADDR_ANY. 15522 */ 15523 if (tcp->tcp_ipversion == IPV4_VERSION) { 15524 if ((tcp->tcp_ipha->ipha_dst == 15525 tcp->tcp_ipha->ipha_src) && 15526 (BE16_EQL(tcp->tcp_tcph->th_lport, 15527 tcp->tcp_tcph->th_fport))) { 15528 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15529 return; 15530 } 15531 } else { 15532 if (IN6_ARE_ADDR_EQUAL( 15533 &tcp->tcp_ip6h->ip6_dst, 15534 &tcp->tcp_ip6h->ip6_src) && 15535 (BE16_EQL(tcp->tcp_tcph->th_lport, 15536 tcp->tcp_tcph->th_fport))) { 15537 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15538 return; 15539 } 15540 } 15541 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15542 /* 15543 * This should not be possible! Just for 15544 * defensive coding... 15545 */ 15546 if (tcp->tcp_state != TCPS_SYN_SENT) 15547 goto after_syn_sent; 15548 15549 if (is_system_labeled() && 15550 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 15551 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 15552 return; 15553 } 15554 15555 ASSERT(q == tcp->tcp_rq); 15556 /* 15557 * tcp_adapt_ire() does not adjust 15558 * for TCP/IP header length. 15559 */ 15560 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15561 15562 /* 15563 * Just make sure our rwnd is at 15564 * least tcp_recv_hiwat_mss * MSS 15565 * large, and round up to the nearest 15566 * MSS. 15567 * 15568 * We do the round up here because 15569 * we need to get the interface 15570 * MTU first before we can do the 15571 * round up. 15572 */ 15573 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15574 tcp_recv_hiwat_minmss * mss); 15575 q->q_hiwat = tcp->tcp_rwnd; 15576 tcp_set_ws_value(tcp); 15577 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15578 tcp->tcp_tcph->th_win); 15579 if (tcp->tcp_rcv_ws > 0 || tcp_wscale_always) 15580 tcp->tcp_snd_ws_ok = B_TRUE; 15581 15582 /* 15583 * Set tcp_snd_ts_ok to true 15584 * so that tcp_xmit_mp will 15585 * include the timestamp 15586 * option in the SYN segment. 15587 */ 15588 if (tcp_tstamp_always || 15589 (tcp->tcp_rcv_ws && tcp_tstamp_if_wscale)) { 15590 tcp->tcp_snd_ts_ok = B_TRUE; 15591 } 15592 15593 /* 15594 * tcp_snd_sack_ok can be set in 15595 * tcp_adapt_ire() if the sack metric 15596 * is set. So check it here also. 15597 */ 15598 if (tcp_sack_permitted == 2 || 15599 tcp->tcp_snd_sack_ok) { 15600 if (tcp->tcp_sack_info == NULL) { 15601 tcp->tcp_sack_info = 15602 kmem_cache_alloc(tcp_sack_info_cache, 15603 KM_SLEEP); 15604 } 15605 tcp->tcp_snd_sack_ok = B_TRUE; 15606 } 15607 15608 /* 15609 * Should we use ECN? Note that the current 15610 * default value (SunOS 5.9) of tcp_ecn_permitted 15611 * is 1. The reason for doing this is that there 15612 * are equipments out there that will drop ECN 15613 * enabled IP packets. Setting it to 1 avoids 15614 * compatibility problems. 15615 */ 15616 if (tcp_ecn_permitted == 2) 15617 tcp->tcp_ecn_ok = B_TRUE; 15618 15619 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15620 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15621 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15622 if (syn_mp) { 15623 cred_t *cr; 15624 pid_t pid; 15625 15626 /* 15627 * Obtain the credential from the 15628 * thread calling connect(); the credential 15629 * lives on in the second mblk which 15630 * originated from T_CONN_REQ and is echoed 15631 * with the T_BIND_ACK from ip. If none 15632 * can be found, default to the creator 15633 * of the socket. 15634 */ 15635 if (mp->b_cont == NULL || 15636 (cr = DB_CRED(mp->b_cont)) == NULL) { 15637 cr = tcp->tcp_cred; 15638 pid = tcp->tcp_cpid; 15639 } else { 15640 pid = DB_CPID(mp->b_cont); 15641 } 15642 15643 TCP_RECORD_TRACE(tcp, syn_mp, 15644 TCP_TRACE_SEND_PKT); 15645 mblk_setcred(syn_mp, cr); 15646 DB_CPID(syn_mp) = pid; 15647 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15648 } 15649 after_syn_sent: 15650 /* 15651 * A trailer mblk indicates a waiting client upstream. 15652 * We complete here the processing begun in 15653 * either tcp_bind() or tcp_connect() by passing 15654 * upstream the reply message they supplied. 15655 */ 15656 mp1 = mp; 15657 mp = mp->b_cont; 15658 freeb(mp1); 15659 if (mp) 15660 break; 15661 return; 15662 case T_ERROR_ACK: 15663 if (tcp->tcp_debug) { 15664 (void) strlog(TCP_MOD_ID, 0, 1, 15665 SL_TRACE|SL_ERROR, 15666 "tcp_rput_other: case T_ERROR_ACK, " 15667 "ERROR_prim == %d", 15668 tea->ERROR_prim); 15669 } 15670 switch (tea->ERROR_prim) { 15671 case O_T_BIND_REQ: 15672 case T_BIND_REQ: 15673 tcp_bind_failed(tcp, mp, 15674 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15675 ENETUNREACH : EADDRNOTAVAIL)); 15676 return; 15677 case T_UNBIND_REQ: 15678 tcp->tcp_hard_binding = B_FALSE; 15679 tcp->tcp_hard_bound = B_FALSE; 15680 if (mp->b_cont) { 15681 freemsg(mp->b_cont); 15682 mp->b_cont = NULL; 15683 } 15684 if (tcp->tcp_unbind_pending) 15685 tcp->tcp_unbind_pending = 0; 15686 else { 15687 /* From tcp_ip_unbind() - free */ 15688 freemsg(mp); 15689 return; 15690 } 15691 break; 15692 case T_SVR4_OPTMGMT_REQ: 15693 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15694 /* T_OPTMGMT_REQ generated by TCP */ 15695 printf("T_SVR4_OPTMGMT_REQ failed " 15696 "%d/%d - dropped (cnt %d)\n", 15697 tea->TLI_error, tea->UNIX_error, 15698 tcp->tcp_drop_opt_ack_cnt); 15699 freemsg(mp); 15700 tcp->tcp_drop_opt_ack_cnt--; 15701 return; 15702 } 15703 break; 15704 } 15705 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15706 tcp->tcp_drop_opt_ack_cnt > 0) { 15707 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15708 "- dropped (cnt %d)\n", 15709 tea->TLI_error, tea->UNIX_error, 15710 tcp->tcp_drop_opt_ack_cnt); 15711 freemsg(mp); 15712 tcp->tcp_drop_opt_ack_cnt--; 15713 return; 15714 } 15715 break; 15716 case T_OPTMGMT_ACK: 15717 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15718 /* T_OPTMGMT_REQ generated by TCP */ 15719 freemsg(mp); 15720 tcp->tcp_drop_opt_ack_cnt--; 15721 return; 15722 } 15723 break; 15724 default: 15725 break; 15726 } 15727 break; 15728 case M_CTL: 15729 /* 15730 * ICMP messages. 15731 */ 15732 tcp_icmp_error(tcp, mp); 15733 return; 15734 case M_FLUSH: 15735 if (*rptr & FLUSHR) 15736 flushq(q, FLUSHDATA); 15737 break; 15738 default: 15739 break; 15740 } 15741 /* 15742 * Make sure we set this bit before sending the ACK for 15743 * bind. Otherwise accept could possibly run and free 15744 * this tcp struct. 15745 */ 15746 putnext(q, mp); 15747 } 15748 15749 /* 15750 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15751 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15752 * tcp_rsrv() try again. 15753 */ 15754 static void 15755 tcp_ordrel_kick(void *arg) 15756 { 15757 conn_t *connp = (conn_t *)arg; 15758 tcp_t *tcp = connp->conn_tcp; 15759 15760 tcp->tcp_ordrelid = 0; 15761 tcp->tcp_timeout = B_FALSE; 15762 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15763 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15764 qenable(tcp->tcp_rq); 15765 } 15766 } 15767 15768 /* ARGSUSED */ 15769 static void 15770 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15771 { 15772 conn_t *connp = (conn_t *)arg; 15773 tcp_t *tcp = connp->conn_tcp; 15774 queue_t *q = tcp->tcp_rq; 15775 uint_t thwin; 15776 15777 freeb(mp); 15778 15779 TCP_STAT(tcp_rsrv_calls); 15780 15781 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15782 return; 15783 } 15784 15785 if (tcp->tcp_fused) { 15786 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15787 15788 ASSERT(tcp->tcp_fused); 15789 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15790 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15791 ASSERT(!TCP_IS_DETACHED(tcp)); 15792 ASSERT(tcp->tcp_connp->conn_sqp == 15793 peer_tcp->tcp_connp->conn_sqp); 15794 15795 /* 15796 * Normally we would not get backenabled in synchronous 15797 * streams mode, but in case this happens, we need to plug 15798 * synchronous streams during our drain to prevent a race 15799 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15800 */ 15801 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15802 if (tcp->tcp_rcv_list != NULL) 15803 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15804 15805 tcp_clrqfull(peer_tcp); 15806 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15807 TCP_STAT(tcp_fusion_backenabled); 15808 return; 15809 } 15810 15811 if (canputnext(q)) { 15812 tcp->tcp_rwnd = q->q_hiwat; 15813 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15814 << tcp->tcp_rcv_ws; 15815 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15816 /* 15817 * Send back a window update immediately if TCP is above 15818 * ESTABLISHED state and the increase of the rcv window 15819 * that the other side knows is at least 1 MSS after flow 15820 * control is lifted. 15821 */ 15822 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15823 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15824 tcp_xmit_ctl(NULL, tcp, 15825 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15826 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15827 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 15828 } 15829 } 15830 /* Handle a failure to allocate a T_ORDREL_IND here */ 15831 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15832 ASSERT(tcp->tcp_listener == NULL); 15833 if (tcp->tcp_rcv_list != NULL) { 15834 (void) tcp_rcv_drain(q, tcp); 15835 } 15836 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15837 mp = mi_tpi_ordrel_ind(); 15838 if (mp) { 15839 tcp->tcp_ordrel_done = B_TRUE; 15840 putnext(q, mp); 15841 if (tcp->tcp_deferred_clean_death) { 15842 /* 15843 * tcp_clean_death was deferred for 15844 * T_ORDREL_IND - do it now 15845 */ 15846 tcp->tcp_deferred_clean_death = B_FALSE; 15847 (void) tcp_clean_death(tcp, 15848 tcp->tcp_client_errno, 22); 15849 } 15850 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15851 /* 15852 * If there isn't already a timer running 15853 * start one. Use a 4 second 15854 * timer as a fallback since it can't fail. 15855 */ 15856 tcp->tcp_timeout = B_TRUE; 15857 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15858 MSEC_TO_TICK(4000)); 15859 } 15860 } 15861 } 15862 15863 /* 15864 * The read side service routine is called mostly when we get back-enabled as a 15865 * result of flow control relief. Since we don't actually queue anything in 15866 * TCP, we have no data to send out of here. What we do is clear the receive 15867 * window, and send out a window update. 15868 * This routine is also called to drive an orderly release message upstream 15869 * if the attempt in tcp_rput failed. 15870 */ 15871 static void 15872 tcp_rsrv(queue_t *q) 15873 { 15874 conn_t *connp = Q_TO_CONN(q); 15875 tcp_t *tcp = connp->conn_tcp; 15876 mblk_t *mp; 15877 15878 /* No code does a putq on the read side */ 15879 ASSERT(q->q_first == NULL); 15880 15881 /* Nothing to do for the default queue */ 15882 if (q == tcp_g_q) { 15883 return; 15884 } 15885 15886 mp = allocb(0, BPRI_HI); 15887 if (mp == NULL) { 15888 /* 15889 * We are under memory pressure. Return for now and we 15890 * we will be called again later. 15891 */ 15892 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15893 /* 15894 * If there isn't already a timer running 15895 * start one. Use a 4 second 15896 * timer as a fallback since it can't fail. 15897 */ 15898 tcp->tcp_timeout = B_TRUE; 15899 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15900 MSEC_TO_TICK(4000)); 15901 } 15902 return; 15903 } 15904 CONN_INC_REF(connp); 15905 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15906 SQTAG_TCP_RSRV); 15907 } 15908 15909 /* 15910 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15911 * We do not allow the receive window to shrink. After setting rwnd, 15912 * set the flow control hiwat of the stream. 15913 * 15914 * This function is called in 2 cases: 15915 * 15916 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15917 * connection (passive open) and in tcp_rput_data() for active connect. 15918 * This is called after tcp_mss_set() when the desired MSS value is known. 15919 * This makes sure that our window size is a mutiple of the other side's 15920 * MSS. 15921 * 2) Handling SO_RCVBUF option. 15922 * 15923 * It is ASSUMED that the requested size is a multiple of the current MSS. 15924 * 15925 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15926 * user requests so. 15927 */ 15928 static int 15929 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15930 { 15931 uint32_t mss = tcp->tcp_mss; 15932 uint32_t old_max_rwnd; 15933 uint32_t max_transmittable_rwnd; 15934 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15935 15936 if (tcp->tcp_fused) { 15937 size_t sth_hiwat; 15938 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15939 15940 ASSERT(peer_tcp != NULL); 15941 /* 15942 * Record the stream head's high water mark for 15943 * this endpoint; this is used for flow-control 15944 * purposes in tcp_fuse_output(). 15945 */ 15946 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15947 if (!tcp_detached) 15948 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 15949 15950 /* 15951 * In the fusion case, the maxpsz stream head value of 15952 * our peer is set according to its send buffer size 15953 * and our receive buffer size; since the latter may 15954 * have changed we need to update the peer's maxpsz. 15955 */ 15956 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15957 return (rwnd); 15958 } 15959 15960 if (tcp_detached) 15961 old_max_rwnd = tcp->tcp_rwnd; 15962 else 15963 old_max_rwnd = tcp->tcp_rq->q_hiwat; 15964 15965 /* 15966 * Insist on a receive window that is at least 15967 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15968 * funny TCP interactions of Nagle algorithm, SWS avoidance 15969 * and delayed acknowledgement. 15970 */ 15971 rwnd = MAX(rwnd, tcp_recv_hiwat_minmss * mss); 15972 15973 /* 15974 * If window size info has already been exchanged, TCP should not 15975 * shrink the window. Shrinking window is doable if done carefully. 15976 * We may add that support later. But so far there is not a real 15977 * need to do that. 15978 */ 15979 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15980 /* MSS may have changed, do a round up again. */ 15981 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15982 } 15983 15984 /* 15985 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15986 * can be applied even before the window scale option is decided. 15987 */ 15988 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15989 if (rwnd > max_transmittable_rwnd) { 15990 rwnd = max_transmittable_rwnd - 15991 (max_transmittable_rwnd % mss); 15992 if (rwnd < mss) 15993 rwnd = max_transmittable_rwnd; 15994 /* 15995 * If we're over the limit we may have to back down tcp_rwnd. 15996 * The increment below won't work for us. So we set all three 15997 * here and the increment below will have no effect. 15998 */ 15999 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16000 } 16001 if (tcp->tcp_localnet) { 16002 tcp->tcp_rack_abs_max = 16003 MIN(tcp_local_dacks_max, rwnd / mss / 2); 16004 } else { 16005 /* 16006 * For a remote host on a different subnet (through a router), 16007 * we ack every other packet to be conforming to RFC1122. 16008 * tcp_deferred_acks_max is default to 2. 16009 */ 16010 tcp->tcp_rack_abs_max = 16011 MIN(tcp_deferred_acks_max, rwnd / mss / 2); 16012 } 16013 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16014 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16015 else 16016 tcp->tcp_rack_cur_max = 0; 16017 /* 16018 * Increment the current rwnd by the amount the maximum grew (we 16019 * can not overwrite it since we might be in the middle of a 16020 * connection.) 16021 */ 16022 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16023 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16024 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16025 tcp->tcp_cwnd_max = rwnd; 16026 16027 if (tcp_detached) 16028 return (rwnd); 16029 /* 16030 * We set the maximum receive window into rq->q_hiwat. 16031 * This is not actually used for flow control. 16032 */ 16033 tcp->tcp_rq->q_hiwat = rwnd; 16034 /* 16035 * Set the Stream head high water mark. This doesn't have to be 16036 * here, since we are simply using default values, but we would 16037 * prefer to choose these values algorithmically, with a likely 16038 * relationship to rwnd. 16039 */ 16040 (void) mi_set_sth_hiwat(tcp->tcp_rq, MAX(rwnd, tcp_sth_rcv_hiwat)); 16041 return (rwnd); 16042 } 16043 16044 /* 16045 * Return SNMP stuff in buffer in mpdata. 16046 */ 16047 int 16048 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16049 { 16050 mblk_t *mpdata; 16051 mblk_t *mp_conn_ctl = NULL; 16052 mblk_t *mp_conn_tail; 16053 mblk_t *mp_attr_ctl = NULL; 16054 mblk_t *mp_attr_tail; 16055 mblk_t *mp6_conn_ctl = NULL; 16056 mblk_t *mp6_conn_tail; 16057 mblk_t *mp6_attr_ctl = NULL; 16058 mblk_t *mp6_attr_tail; 16059 struct opthdr *optp; 16060 mib2_tcpConnEntry_t tce; 16061 mib2_tcp6ConnEntry_t tce6; 16062 mib2_transportMLPEntry_t mlp; 16063 connf_t *connfp; 16064 conn_t *connp; 16065 int i; 16066 boolean_t ispriv; 16067 zoneid_t zoneid; 16068 int v4_conn_idx; 16069 int v6_conn_idx; 16070 16071 if (mpctl == NULL || 16072 (mpdata = mpctl->b_cont) == NULL || 16073 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16074 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16075 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16076 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16077 freemsg(mp_conn_ctl); 16078 freemsg(mp_attr_ctl); 16079 freemsg(mp6_conn_ctl); 16080 freemsg(mp6_attr_ctl); 16081 return (0); 16082 } 16083 16084 /* build table of connections -- need count in fixed part */ 16085 SET_MIB(tcp_mib.tcpRtoAlgorithm, 4); /* vanj */ 16086 SET_MIB(tcp_mib.tcpRtoMin, tcp_rexmit_interval_min); 16087 SET_MIB(tcp_mib.tcpRtoMax, tcp_rexmit_interval_max); 16088 SET_MIB(tcp_mib.tcpMaxConn, -1); 16089 SET_MIB(tcp_mib.tcpCurrEstab, 0); 16090 16091 ispriv = 16092 secpolicy_net_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16093 zoneid = Q_TO_CONN(q)->conn_zoneid; 16094 16095 v4_conn_idx = v6_conn_idx = 0; 16096 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16097 16098 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16099 16100 connfp = &ipcl_globalhash_fanout[i]; 16101 16102 connp = NULL; 16103 16104 while ((connp = 16105 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16106 tcp_t *tcp; 16107 boolean_t needattr; 16108 16109 if (connp->conn_zoneid != zoneid) 16110 continue; /* not in this zone */ 16111 16112 tcp = connp->conn_tcp; 16113 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 16114 tcp->tcp_ibsegs = 0; 16115 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 16116 tcp->tcp_obsegs = 0; 16117 16118 tce6.tcp6ConnState = tce.tcpConnState = 16119 tcp_snmp_state(tcp); 16120 if (tce.tcpConnState == MIB2_TCP_established || 16121 tce.tcpConnState == MIB2_TCP_closeWait) 16122 BUMP_MIB(&tcp_mib, tcpCurrEstab); 16123 16124 needattr = B_FALSE; 16125 bzero(&mlp, sizeof (mlp)); 16126 if (connp->conn_mlp_type != mlptSingle) { 16127 if (connp->conn_mlp_type == mlptShared || 16128 connp->conn_mlp_type == mlptBoth) 16129 mlp.tme_flags |= MIB2_TMEF_SHARED; 16130 if (connp->conn_mlp_type == mlptPrivate || 16131 connp->conn_mlp_type == mlptBoth) 16132 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16133 needattr = B_TRUE; 16134 } 16135 if (connp->conn_peercred != NULL) { 16136 ts_label_t *tsl; 16137 16138 tsl = crgetlabel(connp->conn_peercred); 16139 mlp.tme_doi = label2doi(tsl); 16140 mlp.tme_label = *label2bslabel(tsl); 16141 needattr = B_TRUE; 16142 } 16143 16144 /* Create a message to report on IPv6 entries */ 16145 if (tcp->tcp_ipversion == IPV6_VERSION) { 16146 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16147 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16148 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16149 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16150 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16151 /* Don't want just anybody seeing these... */ 16152 if (ispriv) { 16153 tce6.tcp6ConnEntryInfo.ce_snxt = 16154 tcp->tcp_snxt; 16155 tce6.tcp6ConnEntryInfo.ce_suna = 16156 tcp->tcp_suna; 16157 tce6.tcp6ConnEntryInfo.ce_rnxt = 16158 tcp->tcp_rnxt; 16159 tce6.tcp6ConnEntryInfo.ce_rack = 16160 tcp->tcp_rack; 16161 } else { 16162 /* 16163 * Netstat, unfortunately, uses this to 16164 * get send/receive queue sizes. How to fix? 16165 * Why not compute the difference only? 16166 */ 16167 tce6.tcp6ConnEntryInfo.ce_snxt = 16168 tcp->tcp_snxt - tcp->tcp_suna; 16169 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16170 tce6.tcp6ConnEntryInfo.ce_rnxt = 16171 tcp->tcp_rnxt - tcp->tcp_rack; 16172 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16173 } 16174 16175 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16176 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16177 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16178 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16179 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16180 16181 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16182 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16183 16184 mlp.tme_connidx = v6_conn_idx++; 16185 if (needattr) 16186 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16187 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16188 } 16189 /* 16190 * Create an IPv4 table entry for IPv4 entries and also 16191 * for IPv6 entries which are bound to in6addr_any 16192 * but don't have IPV6_V6ONLY set. 16193 * (i.e. anything an IPv4 peer could connect to) 16194 */ 16195 if (tcp->tcp_ipversion == IPV4_VERSION || 16196 (tcp->tcp_state <= TCPS_LISTEN && 16197 !tcp->tcp_connp->conn_ipv6_v6only && 16198 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16199 if (tcp->tcp_ipversion == IPV6_VERSION) { 16200 tce.tcpConnRemAddress = INADDR_ANY; 16201 tce.tcpConnLocalAddress = INADDR_ANY; 16202 } else { 16203 tce.tcpConnRemAddress = 16204 tcp->tcp_remote; 16205 tce.tcpConnLocalAddress = 16206 tcp->tcp_ip_src; 16207 } 16208 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16209 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16210 /* Don't want just anybody seeing these... */ 16211 if (ispriv) { 16212 tce.tcpConnEntryInfo.ce_snxt = 16213 tcp->tcp_snxt; 16214 tce.tcpConnEntryInfo.ce_suna = 16215 tcp->tcp_suna; 16216 tce.tcpConnEntryInfo.ce_rnxt = 16217 tcp->tcp_rnxt; 16218 tce.tcpConnEntryInfo.ce_rack = 16219 tcp->tcp_rack; 16220 } else { 16221 /* 16222 * Netstat, unfortunately, uses this to 16223 * get send/receive queue sizes. How 16224 * to fix? 16225 * Why not compute the difference only? 16226 */ 16227 tce.tcpConnEntryInfo.ce_snxt = 16228 tcp->tcp_snxt - tcp->tcp_suna; 16229 tce.tcpConnEntryInfo.ce_suna = 0; 16230 tce.tcpConnEntryInfo.ce_rnxt = 16231 tcp->tcp_rnxt - tcp->tcp_rack; 16232 tce.tcpConnEntryInfo.ce_rack = 0; 16233 } 16234 16235 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16236 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16237 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16238 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16239 tce.tcpConnEntryInfo.ce_state = 16240 tcp->tcp_state; 16241 16242 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16243 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16244 16245 mlp.tme_connidx = v4_conn_idx++; 16246 if (needattr) 16247 (void) snmp_append_data2( 16248 mp_attr_ctl->b_cont, 16249 &mp_attr_tail, (char *)&mlp, 16250 sizeof (mlp)); 16251 } 16252 } 16253 } 16254 16255 /* fixed length structure for IPv4 and IPv6 counters */ 16256 SET_MIB(tcp_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16257 SET_MIB(tcp_mib.tcp6ConnTableSize, sizeof (mib2_tcp6ConnEntry_t)); 16258 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16259 optp->level = MIB2_TCP; 16260 optp->name = 0; 16261 (void) snmp_append_data(mpdata, (char *)&tcp_mib, sizeof (tcp_mib)); 16262 optp->len = msgdsize(mpdata); 16263 qreply(q, mpctl); 16264 16265 /* table of connections... */ 16266 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16267 sizeof (struct T_optmgmt_ack)]; 16268 optp->level = MIB2_TCP; 16269 optp->name = MIB2_TCP_CONN; 16270 optp->len = msgdsize(mp_conn_ctl->b_cont); 16271 qreply(q, mp_conn_ctl); 16272 16273 /* table of MLP attributes... */ 16274 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16275 sizeof (struct T_optmgmt_ack)]; 16276 optp->level = MIB2_TCP; 16277 optp->name = EXPER_XPORT_MLP; 16278 optp->len = msgdsize(mp_attr_ctl->b_cont); 16279 if (optp->len == 0) 16280 freemsg(mp_attr_ctl); 16281 else 16282 qreply(q, mp_attr_ctl); 16283 16284 /* table of IPv6 connections... */ 16285 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16286 sizeof (struct T_optmgmt_ack)]; 16287 optp->level = MIB2_TCP6; 16288 optp->name = MIB2_TCP6_CONN; 16289 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16290 qreply(q, mp6_conn_ctl); 16291 16292 /* table of IPv6 MLP attributes... */ 16293 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16294 sizeof (struct T_optmgmt_ack)]; 16295 optp->level = MIB2_TCP6; 16296 optp->name = EXPER_XPORT_MLP; 16297 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16298 if (optp->len == 0) 16299 freemsg(mp6_attr_ctl); 16300 else 16301 qreply(q, mp6_attr_ctl); 16302 return (1); 16303 } 16304 16305 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16306 /* ARGSUSED */ 16307 int 16308 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16309 { 16310 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16311 16312 switch (level) { 16313 case MIB2_TCP: 16314 switch (name) { 16315 case 13: 16316 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16317 return (0); 16318 /* TODO: delete entry defined by tce */ 16319 return (1); 16320 default: 16321 return (0); 16322 } 16323 default: 16324 return (1); 16325 } 16326 } 16327 16328 /* Translate TCP state to MIB2 TCP state. */ 16329 static int 16330 tcp_snmp_state(tcp_t *tcp) 16331 { 16332 if (tcp == NULL) 16333 return (0); 16334 16335 switch (tcp->tcp_state) { 16336 case TCPS_CLOSED: 16337 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16338 case TCPS_BOUND: 16339 return (MIB2_TCP_closed); 16340 case TCPS_LISTEN: 16341 return (MIB2_TCP_listen); 16342 case TCPS_SYN_SENT: 16343 return (MIB2_TCP_synSent); 16344 case TCPS_SYN_RCVD: 16345 return (MIB2_TCP_synReceived); 16346 case TCPS_ESTABLISHED: 16347 return (MIB2_TCP_established); 16348 case TCPS_CLOSE_WAIT: 16349 return (MIB2_TCP_closeWait); 16350 case TCPS_FIN_WAIT_1: 16351 return (MIB2_TCP_finWait1); 16352 case TCPS_CLOSING: 16353 return (MIB2_TCP_closing); 16354 case TCPS_LAST_ACK: 16355 return (MIB2_TCP_lastAck); 16356 case TCPS_FIN_WAIT_2: 16357 return (MIB2_TCP_finWait2); 16358 case TCPS_TIME_WAIT: 16359 return (MIB2_TCP_timeWait); 16360 default: 16361 return (0); 16362 } 16363 } 16364 16365 static char tcp_report_header[] = 16366 "TCP " MI_COL_HDRPAD_STR 16367 "zone dest snxt suna " 16368 "swnd rnxt rack rwnd rto mss w sw rw t " 16369 "recent [lport,fport] state"; 16370 16371 /* 16372 * TCP status report triggered via the Named Dispatch mechanism. 16373 */ 16374 /* ARGSUSED */ 16375 static void 16376 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16377 cred_t *cr) 16378 { 16379 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16380 boolean_t ispriv = secpolicy_net_config(cr, B_TRUE) == 0; 16381 char cflag; 16382 in6_addr_t v6dst; 16383 char buf[80]; 16384 uint_t print_len, buf_len; 16385 16386 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16387 if (buf_len <= 0) 16388 return; 16389 16390 if (hashval >= 0) 16391 (void) sprintf(hash, "%03d ", hashval); 16392 else 16393 hash[0] = '\0'; 16394 16395 /* 16396 * Note that we use the remote address in the tcp_b structure. 16397 * This means that it will print out the real destination address, 16398 * not the next hop's address if source routing is used. This 16399 * avoid the confusion on the output because user may not 16400 * know that source routing is used for a connection. 16401 */ 16402 if (tcp->tcp_ipversion == IPV4_VERSION) { 16403 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16404 } else { 16405 v6dst = tcp->tcp_remote_v6; 16406 } 16407 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16408 /* 16409 * the ispriv checks are so that normal users cannot determine 16410 * sequence number information using NDD. 16411 */ 16412 16413 if (TCP_IS_DETACHED(tcp)) 16414 cflag = '*'; 16415 else 16416 cflag = ' '; 16417 print_len = snprintf((char *)mp->b_wptr, buf_len, 16418 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16419 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16420 hash, 16421 (void *)tcp, 16422 tcp->tcp_connp->conn_zoneid, 16423 addrbuf, 16424 (ispriv) ? tcp->tcp_snxt : 0, 16425 (ispriv) ? tcp->tcp_suna : 0, 16426 tcp->tcp_swnd, 16427 (ispriv) ? tcp->tcp_rnxt : 0, 16428 (ispriv) ? tcp->tcp_rack : 0, 16429 tcp->tcp_rwnd, 16430 tcp->tcp_rto, 16431 tcp->tcp_mss, 16432 tcp->tcp_snd_ws_ok, 16433 tcp->tcp_snd_ws, 16434 tcp->tcp_rcv_ws, 16435 tcp->tcp_snd_ts_ok, 16436 tcp->tcp_ts_recent, 16437 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16438 if (print_len < buf_len) { 16439 ((mblk_t *)mp)->b_wptr += print_len; 16440 } else { 16441 ((mblk_t *)mp)->b_wptr += buf_len; 16442 } 16443 } 16444 16445 /* 16446 * TCP status report (for listeners only) triggered via the Named Dispatch 16447 * mechanism. 16448 */ 16449 /* ARGSUSED */ 16450 static void 16451 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16452 { 16453 char addrbuf[INET6_ADDRSTRLEN]; 16454 in6_addr_t v6dst; 16455 uint_t print_len, buf_len; 16456 16457 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16458 if (buf_len <= 0) 16459 return; 16460 16461 if (tcp->tcp_ipversion == IPV4_VERSION) { 16462 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16463 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16464 } else { 16465 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16466 addrbuf, sizeof (addrbuf)); 16467 } 16468 print_len = snprintf((char *)mp->b_wptr, buf_len, 16469 "%03d " 16470 MI_COL_PTRFMT_STR 16471 "%d %s %05u %08u %d/%d/%d%c\n", 16472 hashval, (void *)tcp, 16473 tcp->tcp_connp->conn_zoneid, 16474 addrbuf, 16475 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16476 tcp->tcp_conn_req_seqnum, 16477 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16478 tcp->tcp_conn_req_max, 16479 tcp->tcp_syn_defense ? '*' : ' '); 16480 if (print_len < buf_len) { 16481 ((mblk_t *)mp)->b_wptr += print_len; 16482 } else { 16483 ((mblk_t *)mp)->b_wptr += buf_len; 16484 } 16485 } 16486 16487 /* TCP status report triggered via the Named Dispatch mechanism. */ 16488 /* ARGSUSED */ 16489 static int 16490 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16491 { 16492 tcp_t *tcp; 16493 int i; 16494 conn_t *connp; 16495 connf_t *connfp; 16496 zoneid_t zoneid; 16497 16498 /* 16499 * Because of the ndd constraint, at most we can have 64K buffer 16500 * to put in all TCP info. So to be more efficient, just 16501 * allocate a 64K buffer here, assuming we need that large buffer. 16502 * This may be a problem as any user can read tcp_status. Therefore 16503 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16504 * This should be OK as normal users should not do this too often. 16505 */ 16506 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16507 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16508 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16509 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16510 return (0); 16511 } 16512 } 16513 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16514 /* The following may work even if we cannot get a large buf. */ 16515 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16516 return (0); 16517 } 16518 16519 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16520 16521 zoneid = Q_TO_CONN(q)->conn_zoneid; 16522 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16523 16524 connfp = &ipcl_globalhash_fanout[i]; 16525 16526 connp = NULL; 16527 16528 while ((connp = 16529 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16530 tcp = connp->conn_tcp; 16531 if (zoneid != GLOBAL_ZONEID && 16532 zoneid != connp->conn_zoneid) 16533 continue; 16534 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16535 cr); 16536 } 16537 16538 } 16539 16540 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16541 return (0); 16542 } 16543 16544 /* TCP status report triggered via the Named Dispatch mechanism. */ 16545 /* ARGSUSED */ 16546 static int 16547 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16548 { 16549 tf_t *tbf; 16550 tcp_t *tcp; 16551 int i; 16552 zoneid_t zoneid; 16553 16554 /* Refer to comments in tcp_status_report(). */ 16555 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16556 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16557 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16558 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16559 return (0); 16560 } 16561 } 16562 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16563 /* The following may work even if we cannot get a large buf. */ 16564 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16565 return (0); 16566 } 16567 16568 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16569 16570 zoneid = Q_TO_CONN(q)->conn_zoneid; 16571 16572 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 16573 tbf = &tcp_bind_fanout[i]; 16574 mutex_enter(&tbf->tf_lock); 16575 for (tcp = tbf->tf_tcp; tcp != NULL; 16576 tcp = tcp->tcp_bind_hash) { 16577 if (zoneid != GLOBAL_ZONEID && 16578 zoneid != tcp->tcp_connp->conn_zoneid) 16579 continue; 16580 CONN_INC_REF(tcp->tcp_connp); 16581 tcp_report_item(mp->b_cont, tcp, i, 16582 Q_TO_TCP(q), cr); 16583 CONN_DEC_REF(tcp->tcp_connp); 16584 } 16585 mutex_exit(&tbf->tf_lock); 16586 } 16587 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16588 return (0); 16589 } 16590 16591 /* TCP status report triggered via the Named Dispatch mechanism. */ 16592 /* ARGSUSED */ 16593 static int 16594 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16595 { 16596 connf_t *connfp; 16597 conn_t *connp; 16598 tcp_t *tcp; 16599 int i; 16600 zoneid_t zoneid; 16601 16602 /* Refer to comments in tcp_status_report(). */ 16603 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16604 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16605 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16606 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16607 return (0); 16608 } 16609 } 16610 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16611 /* The following may work even if we cannot get a large buf. */ 16612 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16613 return (0); 16614 } 16615 16616 (void) mi_mpprintf(mp, 16617 " TCP " MI_COL_HDRPAD_STR 16618 "zone IP addr port seqnum backlog (q0/q/max)"); 16619 16620 zoneid = Q_TO_CONN(q)->conn_zoneid; 16621 16622 for (i = 0; i < ipcl_bind_fanout_size; i++) { 16623 connfp = &ipcl_bind_fanout[i]; 16624 connp = NULL; 16625 while ((connp = 16626 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16627 tcp = connp->conn_tcp; 16628 if (zoneid != GLOBAL_ZONEID && 16629 zoneid != connp->conn_zoneid) 16630 continue; 16631 tcp_report_listener(mp->b_cont, tcp, i); 16632 } 16633 } 16634 16635 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16636 return (0); 16637 } 16638 16639 /* TCP status report triggered via the Named Dispatch mechanism. */ 16640 /* ARGSUSED */ 16641 static int 16642 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16643 { 16644 connf_t *connfp; 16645 conn_t *connp; 16646 tcp_t *tcp; 16647 int i; 16648 zoneid_t zoneid; 16649 16650 /* Refer to comments in tcp_status_report(). */ 16651 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16652 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16653 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16654 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16655 return (0); 16656 } 16657 } 16658 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16659 /* The following may work even if we cannot get a large buf. */ 16660 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16661 return (0); 16662 } 16663 16664 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16665 ipcl_conn_fanout_size); 16666 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16667 16668 zoneid = Q_TO_CONN(q)->conn_zoneid; 16669 16670 for (i = 0; i < ipcl_conn_fanout_size; i++) { 16671 connfp = &ipcl_conn_fanout[i]; 16672 connp = NULL; 16673 while ((connp = 16674 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16675 tcp = connp->conn_tcp; 16676 if (zoneid != GLOBAL_ZONEID && 16677 zoneid != connp->conn_zoneid) 16678 continue; 16679 tcp_report_item(mp->b_cont, tcp, i, 16680 Q_TO_TCP(q), cr); 16681 } 16682 } 16683 16684 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16685 return (0); 16686 } 16687 16688 /* TCP status report triggered via the Named Dispatch mechanism. */ 16689 /* ARGSUSED */ 16690 static int 16691 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16692 { 16693 tf_t *tf; 16694 tcp_t *tcp; 16695 int i; 16696 zoneid_t zoneid; 16697 16698 /* Refer to comments in tcp_status_report(). */ 16699 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16700 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16701 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16702 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16703 return (0); 16704 } 16705 } 16706 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16707 /* The following may work even if we cannot get a large buf. */ 16708 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16709 return (0); 16710 } 16711 16712 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16713 16714 zoneid = Q_TO_CONN(q)->conn_zoneid; 16715 16716 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 16717 tf = &tcp_acceptor_fanout[i]; 16718 mutex_enter(&tf->tf_lock); 16719 for (tcp = tf->tf_tcp; tcp != NULL; 16720 tcp = tcp->tcp_acceptor_hash) { 16721 if (zoneid != GLOBAL_ZONEID && 16722 zoneid != tcp->tcp_connp->conn_zoneid) 16723 continue; 16724 tcp_report_item(mp->b_cont, tcp, i, 16725 Q_TO_TCP(q), cr); 16726 } 16727 mutex_exit(&tf->tf_lock); 16728 } 16729 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16730 return (0); 16731 } 16732 16733 /* 16734 * tcp_timer is the timer service routine. It handles the retransmission, 16735 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16736 * from the state of the tcp instance what kind of action needs to be done 16737 * at the time it is called. 16738 */ 16739 static void 16740 tcp_timer(void *arg) 16741 { 16742 mblk_t *mp; 16743 clock_t first_threshold; 16744 clock_t second_threshold; 16745 clock_t ms; 16746 uint32_t mss; 16747 conn_t *connp = (conn_t *)arg; 16748 tcp_t *tcp = connp->conn_tcp; 16749 16750 tcp->tcp_timer_tid = 0; 16751 16752 if (tcp->tcp_fused) 16753 return; 16754 16755 first_threshold = tcp->tcp_first_timer_threshold; 16756 second_threshold = tcp->tcp_second_timer_threshold; 16757 switch (tcp->tcp_state) { 16758 case TCPS_IDLE: 16759 case TCPS_BOUND: 16760 case TCPS_LISTEN: 16761 return; 16762 case TCPS_SYN_RCVD: { 16763 tcp_t *listener = tcp->tcp_listener; 16764 16765 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16766 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16767 /* it's our first timeout */ 16768 tcp->tcp_syn_rcvd_timeout = 1; 16769 mutex_enter(&listener->tcp_eager_lock); 16770 listener->tcp_syn_rcvd_timeout++; 16771 if (!tcp->tcp_dontdrop && tcp->tcp_closemp_used == 0) { 16772 /* 16773 * Make this eager available for drop if we 16774 * need to drop one to accomodate a new 16775 * incoming SYN request. 16776 */ 16777 MAKE_DROPPABLE(listener, tcp); 16778 } 16779 if (!listener->tcp_syn_defense && 16780 (listener->tcp_syn_rcvd_timeout > 16781 (tcp_conn_req_max_q0 >> 2)) && 16782 (tcp_conn_req_max_q0 > 200)) { 16783 /* We may be under attack. Put on a defense. */ 16784 listener->tcp_syn_defense = B_TRUE; 16785 cmn_err(CE_WARN, "High TCP connect timeout " 16786 "rate! System (port %d) may be under a " 16787 "SYN flood attack!", 16788 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16789 16790 listener->tcp_ip_addr_cache = kmem_zalloc( 16791 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16792 KM_NOSLEEP); 16793 } 16794 mutex_exit(&listener->tcp_eager_lock); 16795 } else if (listener != NULL) { 16796 mutex_enter(&listener->tcp_eager_lock); 16797 tcp->tcp_syn_rcvd_timeout++; 16798 if (tcp->tcp_syn_rcvd_timeout > 1 && 16799 tcp->tcp_closemp_used == 0) { 16800 /* 16801 * This is our second timeout. Put the tcp in 16802 * the list of droppable eagers to allow it to 16803 * be dropped, if needed. We don't check 16804 * whether tcp_dontdrop is set or not to 16805 * protect ourselve from a SYN attack where a 16806 * remote host can spoof itself as one of the 16807 * good IP source and continue to hold 16808 * resources too long. 16809 */ 16810 MAKE_DROPPABLE(listener, tcp); 16811 } 16812 mutex_exit(&listener->tcp_eager_lock); 16813 } 16814 } 16815 /* FALLTHRU */ 16816 case TCPS_SYN_SENT: 16817 first_threshold = tcp->tcp_first_ctimer_threshold; 16818 second_threshold = tcp->tcp_second_ctimer_threshold; 16819 break; 16820 case TCPS_ESTABLISHED: 16821 case TCPS_FIN_WAIT_1: 16822 case TCPS_CLOSING: 16823 case TCPS_CLOSE_WAIT: 16824 case TCPS_LAST_ACK: 16825 /* If we have data to rexmit */ 16826 if (tcp->tcp_suna != tcp->tcp_snxt) { 16827 clock_t time_to_wait; 16828 16829 BUMP_MIB(&tcp_mib, tcpTimRetrans); 16830 if (!tcp->tcp_xmit_head) 16831 break; 16832 time_to_wait = lbolt - 16833 (clock_t)tcp->tcp_xmit_head->b_prev; 16834 time_to_wait = tcp->tcp_rto - 16835 TICK_TO_MSEC(time_to_wait); 16836 /* 16837 * If the timer fires too early, 1 clock tick earlier, 16838 * restart the timer. 16839 */ 16840 if (time_to_wait > msec_per_tick) { 16841 TCP_STAT(tcp_timer_fire_early); 16842 TCP_TIMER_RESTART(tcp, time_to_wait); 16843 return; 16844 } 16845 /* 16846 * When we probe zero windows, we force the swnd open. 16847 * If our peer acks with a closed window swnd will be 16848 * set to zero by tcp_rput(). As long as we are 16849 * receiving acks tcp_rput will 16850 * reset 'tcp_ms_we_have_waited' so as not to trip the 16851 * first and second interval actions. NOTE: the timer 16852 * interval is allowed to continue its exponential 16853 * backoff. 16854 */ 16855 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16856 if (tcp->tcp_debug) { 16857 (void) strlog(TCP_MOD_ID, 0, 1, 16858 SL_TRACE, "tcp_timer: zero win"); 16859 } 16860 } else { 16861 /* 16862 * After retransmission, we need to do 16863 * slow start. Set the ssthresh to one 16864 * half of current effective window and 16865 * cwnd to one MSS. Also reset 16866 * tcp_cwnd_cnt. 16867 * 16868 * Note that if tcp_ssthresh is reduced because 16869 * of ECN, do not reduce it again unless it is 16870 * already one window of data away (tcp_cwr 16871 * should then be cleared) or this is a 16872 * timeout for a retransmitted segment. 16873 */ 16874 uint32_t npkt; 16875 16876 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16877 npkt = ((tcp->tcp_timer_backoff ? 16878 tcp->tcp_cwnd_ssthresh : 16879 tcp->tcp_snxt - 16880 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16881 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16882 tcp->tcp_mss; 16883 } 16884 tcp->tcp_cwnd = tcp->tcp_mss; 16885 tcp->tcp_cwnd_cnt = 0; 16886 if (tcp->tcp_ecn_ok) { 16887 tcp->tcp_cwr = B_TRUE; 16888 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16889 tcp->tcp_ecn_cwr_sent = B_FALSE; 16890 } 16891 } 16892 break; 16893 } 16894 /* 16895 * We have something to send yet we cannot send. The 16896 * reason can be: 16897 * 16898 * 1. Zero send window: we need to do zero window probe. 16899 * 2. Zero cwnd: because of ECN, we need to "clock out 16900 * segments. 16901 * 3. SWS avoidance: receiver may have shrunk window, 16902 * reset our knowledge. 16903 * 16904 * Note that condition 2 can happen with either 1 or 16905 * 3. But 1 and 3 are exclusive. 16906 */ 16907 if (tcp->tcp_unsent != 0) { 16908 if (tcp->tcp_cwnd == 0) { 16909 /* 16910 * Set tcp_cwnd to 1 MSS so that a 16911 * new segment can be sent out. We 16912 * are "clocking out" new data when 16913 * the network is really congested. 16914 */ 16915 ASSERT(tcp->tcp_ecn_ok); 16916 tcp->tcp_cwnd = tcp->tcp_mss; 16917 } 16918 if (tcp->tcp_swnd == 0) { 16919 /* Extend window for zero window probe */ 16920 tcp->tcp_swnd++; 16921 tcp->tcp_zero_win_probe = B_TRUE; 16922 BUMP_MIB(&tcp_mib, tcpOutWinProbe); 16923 } else { 16924 /* 16925 * Handle timeout from sender SWS avoidance. 16926 * Reset our knowledge of the max send window 16927 * since the receiver might have reduced its 16928 * receive buffer. Avoid setting tcp_max_swnd 16929 * to one since that will essentially disable 16930 * the SWS checks. 16931 * 16932 * Note that since we don't have a SWS 16933 * state variable, if the timeout is set 16934 * for ECN but not for SWS, this 16935 * code will also be executed. This is 16936 * fine as tcp_max_swnd is updated 16937 * constantly and it will not affect 16938 * anything. 16939 */ 16940 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16941 } 16942 tcp_wput_data(tcp, NULL, B_FALSE); 16943 return; 16944 } 16945 /* Is there a FIN that needs to be to re retransmitted? */ 16946 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16947 !tcp->tcp_fin_acked) 16948 break; 16949 /* Nothing to do, return without restarting timer. */ 16950 TCP_STAT(tcp_timer_fire_miss); 16951 return; 16952 case TCPS_FIN_WAIT_2: 16953 /* 16954 * User closed the TCP endpoint and peer ACK'ed our FIN. 16955 * We waited some time for for peer's FIN, but it hasn't 16956 * arrived. We flush the connection now to avoid 16957 * case where the peer has rebooted. 16958 */ 16959 if (TCP_IS_DETACHED(tcp)) { 16960 (void) tcp_clean_death(tcp, 0, 23); 16961 } else { 16962 TCP_TIMER_RESTART(tcp, tcp_fin_wait_2_flush_interval); 16963 } 16964 return; 16965 case TCPS_TIME_WAIT: 16966 (void) tcp_clean_death(tcp, 0, 24); 16967 return; 16968 default: 16969 if (tcp->tcp_debug) { 16970 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16971 "tcp_timer: strange state (%d) %s", 16972 tcp->tcp_state, tcp_display(tcp, NULL, 16973 DISP_PORT_ONLY)); 16974 } 16975 return; 16976 } 16977 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16978 /* 16979 * For zero window probe, we need to send indefinitely, 16980 * unless we have not heard from the other side for some 16981 * time... 16982 */ 16983 if ((tcp->tcp_zero_win_probe == 0) || 16984 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16985 second_threshold)) { 16986 BUMP_MIB(&tcp_mib, tcpTimRetransDrop); 16987 /* 16988 * If TCP is in SYN_RCVD state, send back a 16989 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16990 * should be zero in TCPS_SYN_RCVD state. 16991 */ 16992 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16993 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16994 "in SYN_RCVD", 16995 tcp, tcp->tcp_snxt, 16996 tcp->tcp_rnxt, TH_RST | TH_ACK); 16997 } 16998 (void) tcp_clean_death(tcp, 16999 tcp->tcp_client_errno ? 17000 tcp->tcp_client_errno : ETIMEDOUT, 25); 17001 return; 17002 } else { 17003 /* 17004 * Set tcp_ms_we_have_waited to second_threshold 17005 * so that in next timeout, we will do the above 17006 * check (lbolt - tcp_last_recv_time). This is 17007 * also to avoid overflow. 17008 * 17009 * We don't need to decrement tcp_timer_backoff 17010 * to avoid overflow because it will be decremented 17011 * later if new timeout value is greater than 17012 * tcp_rexmit_interval_max. In the case when 17013 * tcp_rexmit_interval_max is greater than 17014 * second_threshold, it means that we will wait 17015 * longer than second_threshold to send the next 17016 * window probe. 17017 */ 17018 tcp->tcp_ms_we_have_waited = second_threshold; 17019 } 17020 } else if (ms > first_threshold) { 17021 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17022 tcp->tcp_xmit_head != NULL) { 17023 tcp->tcp_xmit_head = 17024 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17025 } 17026 /* 17027 * We have been retransmitting for too long... The RTT 17028 * we calculated is probably incorrect. Reinitialize it. 17029 * Need to compensate for 0 tcp_rtt_sa. Reset 17030 * tcp_rtt_update so that we won't accidentally cache a 17031 * bad value. But only do this if this is not a zero 17032 * window probe. 17033 */ 17034 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17035 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17036 (tcp->tcp_rtt_sa >> 5); 17037 tcp->tcp_rtt_sa = 0; 17038 tcp_ip_notify(tcp); 17039 tcp->tcp_rtt_update = 0; 17040 } 17041 } 17042 tcp->tcp_timer_backoff++; 17043 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17044 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17045 tcp_rexmit_interval_min) { 17046 /* 17047 * This means the original RTO is tcp_rexmit_interval_min. 17048 * So we will use tcp_rexmit_interval_min as the RTO value 17049 * and do the backoff. 17050 */ 17051 ms = tcp_rexmit_interval_min << tcp->tcp_timer_backoff; 17052 } else { 17053 ms <<= tcp->tcp_timer_backoff; 17054 } 17055 if (ms > tcp_rexmit_interval_max) { 17056 ms = tcp_rexmit_interval_max; 17057 /* 17058 * ms is at max, decrement tcp_timer_backoff to avoid 17059 * overflow. 17060 */ 17061 tcp->tcp_timer_backoff--; 17062 } 17063 tcp->tcp_ms_we_have_waited += ms; 17064 if (tcp->tcp_zero_win_probe == 0) { 17065 tcp->tcp_rto = ms; 17066 } 17067 TCP_TIMER_RESTART(tcp, ms); 17068 /* 17069 * This is after a timeout and tcp_rto is backed off. Set 17070 * tcp_set_timer to 1 so that next time RTO is updated, we will 17071 * restart the timer with a correct value. 17072 */ 17073 tcp->tcp_set_timer = 1; 17074 mss = tcp->tcp_snxt - tcp->tcp_suna; 17075 if (mss > tcp->tcp_mss) 17076 mss = tcp->tcp_mss; 17077 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17078 mss = tcp->tcp_swnd; 17079 17080 if ((mp = tcp->tcp_xmit_head) != NULL) 17081 mp->b_prev = (mblk_t *)lbolt; 17082 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17083 B_TRUE); 17084 17085 /* 17086 * When slow start after retransmission begins, start with 17087 * this seq no. tcp_rexmit_max marks the end of special slow 17088 * start phase. tcp_snd_burst controls how many segments 17089 * can be sent because of an ack. 17090 */ 17091 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17092 tcp->tcp_snd_burst = TCP_CWND_SS; 17093 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17094 (tcp->tcp_unsent == 0)) { 17095 tcp->tcp_rexmit_max = tcp->tcp_fss; 17096 } else { 17097 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17098 } 17099 tcp->tcp_rexmit = B_TRUE; 17100 tcp->tcp_dupack_cnt = 0; 17101 17102 /* 17103 * Remove all rexmit SACK blk to start from fresh. 17104 */ 17105 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17106 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17107 tcp->tcp_num_notsack_blk = 0; 17108 tcp->tcp_cnt_notsack_list = 0; 17109 } 17110 if (mp == NULL) { 17111 return; 17112 } 17113 /* Attach credentials to retransmitted initial SYNs. */ 17114 if (tcp->tcp_state == TCPS_SYN_SENT) { 17115 mblk_setcred(mp, tcp->tcp_cred); 17116 DB_CPID(mp) = tcp->tcp_cpid; 17117 } 17118 17119 tcp->tcp_csuna = tcp->tcp_snxt; 17120 BUMP_MIB(&tcp_mib, tcpRetransSegs); 17121 UPDATE_MIB(&tcp_mib, tcpRetransBytes, mss); 17122 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 17123 tcp_send_data(tcp, tcp->tcp_wq, mp); 17124 17125 } 17126 17127 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17128 static void 17129 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17130 { 17131 conn_t *connp; 17132 17133 switch (tcp->tcp_state) { 17134 case TCPS_BOUND: 17135 case TCPS_LISTEN: 17136 break; 17137 default: 17138 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17139 return; 17140 } 17141 17142 /* 17143 * Need to clean up all the eagers since after the unbind, segments 17144 * will no longer be delivered to this listener stream. 17145 */ 17146 mutex_enter(&tcp->tcp_eager_lock); 17147 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17148 tcp_eager_cleanup(tcp, 0); 17149 } 17150 mutex_exit(&tcp->tcp_eager_lock); 17151 17152 if (tcp->tcp_ipversion == IPV4_VERSION) { 17153 tcp->tcp_ipha->ipha_src = 0; 17154 } else { 17155 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17156 } 17157 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17158 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17159 tcp_bind_hash_remove(tcp); 17160 tcp->tcp_state = TCPS_IDLE; 17161 tcp->tcp_mdt = B_FALSE; 17162 /* Send M_FLUSH according to TPI */ 17163 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17164 connp = tcp->tcp_connp; 17165 connp->conn_mdt_ok = B_FALSE; 17166 ipcl_hash_remove(connp); 17167 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17168 mp = mi_tpi_ok_ack_alloc(mp); 17169 putnext(tcp->tcp_rq, mp); 17170 } 17171 17172 /* 17173 * Don't let port fall into the privileged range. 17174 * Since the extra privileged ports can be arbitrary we also 17175 * ensure that we exclude those from consideration. 17176 * tcp_g_epriv_ports is not sorted thus we loop over it until 17177 * there are no changes. 17178 * 17179 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17180 * but instead the code relies on: 17181 * - the fact that the address of the array and its size never changes 17182 * - the atomic assignment of the elements of the array 17183 * 17184 * Returns 0 if there are no more ports available. 17185 * 17186 * TS note: skip multilevel ports. 17187 */ 17188 static in_port_t 17189 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17190 { 17191 int i; 17192 boolean_t restart = B_FALSE; 17193 17194 if (random && tcp_random_anon_port != 0) { 17195 (void) random_get_pseudo_bytes((uint8_t *)&port, 17196 sizeof (in_port_t)); 17197 /* 17198 * Unless changed by a sys admin, the smallest anon port 17199 * is 32768 and the largest anon port is 65535. It is 17200 * very likely (50%) for the random port to be smaller 17201 * than the smallest anon port. When that happens, 17202 * add port % (anon port range) to the smallest anon 17203 * port to get the random port. It should fall into the 17204 * valid anon port range. 17205 */ 17206 if (port < tcp_smallest_anon_port) { 17207 port = tcp_smallest_anon_port + 17208 port % (tcp_largest_anon_port - 17209 tcp_smallest_anon_port); 17210 } 17211 } 17212 17213 retry: 17214 if (port < tcp_smallest_anon_port) 17215 port = (in_port_t)tcp_smallest_anon_port; 17216 17217 if (port > tcp_largest_anon_port) { 17218 if (restart) 17219 return (0); 17220 restart = B_TRUE; 17221 port = (in_port_t)tcp_smallest_anon_port; 17222 } 17223 17224 if (port < tcp_smallest_nonpriv_port) 17225 port = (in_port_t)tcp_smallest_nonpriv_port; 17226 17227 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 17228 if (port == tcp_g_epriv_ports[i]) { 17229 port++; 17230 /* 17231 * Make sure whether the port is in the 17232 * valid range. 17233 */ 17234 goto retry; 17235 } 17236 } 17237 if (is_system_labeled() && 17238 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17239 IPPROTO_TCP, B_TRUE)) != 0) { 17240 port = i; 17241 goto retry; 17242 } 17243 return (port); 17244 } 17245 17246 /* 17247 * Return the next anonymous port in the privileged port range for 17248 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17249 * downwards. This is the same behavior as documented in the userland 17250 * library call rresvport(3N). 17251 * 17252 * TS note: skip multilevel ports. 17253 */ 17254 static in_port_t 17255 tcp_get_next_priv_port(const tcp_t *tcp) 17256 { 17257 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17258 in_port_t nextport; 17259 boolean_t restart = B_FALSE; 17260 17261 retry: 17262 if (next_priv_port < tcp_min_anonpriv_port || 17263 next_priv_port >= IPPORT_RESERVED) { 17264 next_priv_port = IPPORT_RESERVED - 1; 17265 if (restart) 17266 return (0); 17267 restart = B_TRUE; 17268 } 17269 if (is_system_labeled() && 17270 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17271 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17272 next_priv_port = nextport; 17273 goto retry; 17274 } 17275 return (next_priv_port--); 17276 } 17277 17278 /* The write side r/w procedure. */ 17279 17280 #if CCS_STATS 17281 struct { 17282 struct { 17283 int64_t count, bytes; 17284 } tot, hit; 17285 } wrw_stats; 17286 #endif 17287 17288 /* 17289 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17290 * messages. 17291 */ 17292 /* ARGSUSED */ 17293 static void 17294 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17295 { 17296 conn_t *connp = (conn_t *)arg; 17297 tcp_t *tcp = connp->conn_tcp; 17298 queue_t *q = tcp->tcp_wq; 17299 17300 ASSERT(DB_TYPE(mp) != M_IOCTL); 17301 /* 17302 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17303 * Once the close starts, streamhead and sockfs will not let any data 17304 * packets come down (close ensures that there are no threads using the 17305 * queue and no new threads will come down) but since qprocsoff() 17306 * hasn't happened yet, a M_FLUSH or some non data message might 17307 * get reflected back (in response to our own FLUSHRW) and get 17308 * processed after tcp_close() is done. The conn would still be valid 17309 * because a ref would have added but we need to check the state 17310 * before actually processing the packet. 17311 */ 17312 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17313 freemsg(mp); 17314 return; 17315 } 17316 17317 switch (DB_TYPE(mp)) { 17318 case M_IOCDATA: 17319 tcp_wput_iocdata(tcp, mp); 17320 break; 17321 case M_FLUSH: 17322 tcp_wput_flush(tcp, mp); 17323 break; 17324 default: 17325 CALL_IP_WPUT(connp, q, mp); 17326 break; 17327 } 17328 } 17329 17330 /* 17331 * The TCP fast path write put procedure. 17332 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17333 */ 17334 /* ARGSUSED */ 17335 void 17336 tcp_output(void *arg, mblk_t *mp, void *arg2) 17337 { 17338 int len; 17339 int hdrlen; 17340 int plen; 17341 mblk_t *mp1; 17342 uchar_t *rptr; 17343 uint32_t snxt; 17344 tcph_t *tcph; 17345 struct datab *db; 17346 uint32_t suna; 17347 uint32_t mss; 17348 ipaddr_t *dst; 17349 ipaddr_t *src; 17350 uint32_t sum; 17351 int usable; 17352 conn_t *connp = (conn_t *)arg; 17353 tcp_t *tcp = connp->conn_tcp; 17354 uint32_t msize; 17355 17356 /* 17357 * Try and ASSERT the minimum possible references on the 17358 * conn early enough. Since we are executing on write side, 17359 * the connection is obviously not detached and that means 17360 * there is a ref each for TCP and IP. Since we are behind 17361 * the squeue, the minimum references needed are 3. If the 17362 * conn is in classifier hash list, there should be an 17363 * extra ref for that (we check both the possibilities). 17364 */ 17365 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17366 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17367 17368 ASSERT(DB_TYPE(mp) == M_DATA); 17369 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17370 17371 mutex_enter(&connp->conn_lock); 17372 tcp->tcp_squeue_bytes -= msize; 17373 mutex_exit(&connp->conn_lock); 17374 17375 /* Bypass tcp protocol for fused tcp loopback */ 17376 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17377 return; 17378 17379 mss = tcp->tcp_mss; 17380 if (tcp->tcp_xmit_zc_clean) 17381 mp = tcp_zcopy_backoff(tcp, mp, 0); 17382 17383 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17384 len = (int)(mp->b_wptr - mp->b_rptr); 17385 17386 /* 17387 * Criteria for fast path: 17388 * 17389 * 1. no unsent data 17390 * 2. single mblk in request 17391 * 3. connection established 17392 * 4. data in mblk 17393 * 5. len <= mss 17394 * 6. no tcp_valid bits 17395 */ 17396 if ((tcp->tcp_unsent != 0) || 17397 (tcp->tcp_cork) || 17398 (mp->b_cont != NULL) || 17399 (tcp->tcp_state != TCPS_ESTABLISHED) || 17400 (len == 0) || 17401 (len > mss) || 17402 (tcp->tcp_valid_bits != 0)) { 17403 tcp_wput_data(tcp, mp, B_FALSE); 17404 return; 17405 } 17406 17407 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17408 ASSERT(tcp->tcp_fin_sent == 0); 17409 17410 /* queue new packet onto retransmission queue */ 17411 if (tcp->tcp_xmit_head == NULL) { 17412 tcp->tcp_xmit_head = mp; 17413 } else { 17414 tcp->tcp_xmit_last->b_cont = mp; 17415 } 17416 tcp->tcp_xmit_last = mp; 17417 tcp->tcp_xmit_tail = mp; 17418 17419 /* find out how much we can send */ 17420 /* BEGIN CSTYLED */ 17421 /* 17422 * un-acked usable 17423 * |--------------|-----------------| 17424 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17425 */ 17426 /* END CSTYLED */ 17427 17428 /* start sending from tcp_snxt */ 17429 snxt = tcp->tcp_snxt; 17430 17431 /* 17432 * Check to see if this connection has been idled for some 17433 * time and no ACK is expected. If it is, we need to slow 17434 * start again to get back the connection's "self-clock" as 17435 * described in VJ's paper. 17436 * 17437 * Refer to the comment in tcp_mss_set() for the calculation 17438 * of tcp_cwnd after idle. 17439 */ 17440 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17441 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17442 SET_TCP_INIT_CWND(tcp, mss, tcp_slow_start_after_idle); 17443 } 17444 17445 usable = tcp->tcp_swnd; /* tcp window size */ 17446 if (usable > tcp->tcp_cwnd) 17447 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17448 usable -= snxt; /* subtract stuff already sent */ 17449 suna = tcp->tcp_suna; 17450 usable += suna; 17451 /* usable can be < 0 if the congestion window is smaller */ 17452 if (len > usable) { 17453 /* Can't send complete M_DATA in one shot */ 17454 goto slow; 17455 } 17456 17457 if (tcp->tcp_flow_stopped && 17458 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17459 tcp_clrqfull(tcp); 17460 } 17461 17462 /* 17463 * determine if anything to send (Nagle). 17464 * 17465 * 1. len < tcp_mss (i.e. small) 17466 * 2. unacknowledged data present 17467 * 3. len < nagle limit 17468 * 4. last packet sent < nagle limit (previous packet sent) 17469 */ 17470 if ((len < mss) && (snxt != suna) && 17471 (len < (int)tcp->tcp_naglim) && 17472 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17473 /* 17474 * This was the first unsent packet and normally 17475 * mss < xmit_hiwater so there is no need to worry 17476 * about flow control. The next packet will go 17477 * through the flow control check in tcp_wput_data(). 17478 */ 17479 /* leftover work from above */ 17480 tcp->tcp_unsent = len; 17481 tcp->tcp_xmit_tail_unsent = len; 17482 17483 return; 17484 } 17485 17486 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17487 17488 if (snxt == suna) { 17489 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17490 } 17491 17492 /* we have always sent something */ 17493 tcp->tcp_rack_cnt = 0; 17494 17495 tcp->tcp_snxt = snxt + len; 17496 tcp->tcp_rack = tcp->tcp_rnxt; 17497 17498 if ((mp1 = dupb(mp)) == 0) 17499 goto no_memory; 17500 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17501 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17502 17503 /* adjust tcp header information */ 17504 tcph = tcp->tcp_tcph; 17505 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17506 17507 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17508 sum = (sum >> 16) + (sum & 0xFFFF); 17509 U16_TO_ABE16(sum, tcph->th_sum); 17510 17511 U32_TO_ABE32(snxt, tcph->th_seq); 17512 17513 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 17514 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 17515 BUMP_LOCAL(tcp->tcp_obsegs); 17516 17517 /* Update the latest receive window size in TCP header. */ 17518 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17519 tcph->th_win); 17520 17521 tcp->tcp_last_sent_len = (ushort_t)len; 17522 17523 plen = len + tcp->tcp_hdr_len; 17524 17525 if (tcp->tcp_ipversion == IPV4_VERSION) { 17526 tcp->tcp_ipha->ipha_length = htons(plen); 17527 } else { 17528 tcp->tcp_ip6h->ip6_plen = htons(plen - 17529 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17530 } 17531 17532 /* see if we need to allocate a mblk for the headers */ 17533 hdrlen = tcp->tcp_hdr_len; 17534 rptr = mp1->b_rptr - hdrlen; 17535 db = mp1->b_datap; 17536 if ((db->db_ref != 2) || rptr < db->db_base || 17537 (!OK_32PTR(rptr))) { 17538 /* NOTE: we assume allocb returns an OK_32PTR */ 17539 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17540 tcp_wroff_xtra, BPRI_MED); 17541 if (!mp) { 17542 freemsg(mp1); 17543 goto no_memory; 17544 } 17545 mp->b_cont = mp1; 17546 mp1 = mp; 17547 /* Leave room for Link Level header */ 17548 /* hdrlen = tcp->tcp_hdr_len; */ 17549 rptr = &mp1->b_rptr[tcp_wroff_xtra]; 17550 mp1->b_wptr = &rptr[hdrlen]; 17551 } 17552 mp1->b_rptr = rptr; 17553 17554 /* Fill in the timestamp option. */ 17555 if (tcp->tcp_snd_ts_ok) { 17556 U32_TO_BE32((uint32_t)lbolt, 17557 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17558 U32_TO_BE32(tcp->tcp_ts_recent, 17559 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17560 } else { 17561 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17562 } 17563 17564 /* copy header into outgoing packet */ 17565 dst = (ipaddr_t *)rptr; 17566 src = (ipaddr_t *)tcp->tcp_iphc; 17567 dst[0] = src[0]; 17568 dst[1] = src[1]; 17569 dst[2] = src[2]; 17570 dst[3] = src[3]; 17571 dst[4] = src[4]; 17572 dst[5] = src[5]; 17573 dst[6] = src[6]; 17574 dst[7] = src[7]; 17575 dst[8] = src[8]; 17576 dst[9] = src[9]; 17577 if (hdrlen -= 40) { 17578 hdrlen >>= 2; 17579 dst += 10; 17580 src += 10; 17581 do { 17582 *dst++ = *src++; 17583 } while (--hdrlen); 17584 } 17585 17586 /* 17587 * Set the ECN info in the TCP header. Note that this 17588 * is not the template header. 17589 */ 17590 if (tcp->tcp_ecn_ok) { 17591 SET_ECT(tcp, rptr); 17592 17593 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17594 if (tcp->tcp_ecn_echo_on) 17595 tcph->th_flags[0] |= TH_ECE; 17596 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17597 tcph->th_flags[0] |= TH_CWR; 17598 tcp->tcp_ecn_cwr_sent = B_TRUE; 17599 } 17600 } 17601 17602 if (tcp->tcp_ip_forward_progress) { 17603 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17604 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17605 tcp->tcp_ip_forward_progress = B_FALSE; 17606 } 17607 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17608 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17609 return; 17610 17611 /* 17612 * If we ran out of memory, we pretend to have sent the packet 17613 * and that it was lost on the wire. 17614 */ 17615 no_memory: 17616 return; 17617 17618 slow: 17619 /* leftover work from above */ 17620 tcp->tcp_unsent = len; 17621 tcp->tcp_xmit_tail_unsent = len; 17622 tcp_wput_data(tcp, NULL, B_FALSE); 17623 } 17624 17625 /* 17626 * The function called through squeue to get behind eager's perimeter to 17627 * finish the accept processing. 17628 */ 17629 /* ARGSUSED */ 17630 void 17631 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17632 { 17633 conn_t *connp = (conn_t *)arg; 17634 tcp_t *tcp = connp->conn_tcp; 17635 queue_t *q = tcp->tcp_rq; 17636 mblk_t *mp1; 17637 mblk_t *stropt_mp = mp; 17638 struct stroptions *stropt; 17639 uint_t thwin; 17640 17641 /* 17642 * Drop the eager's ref on the listener, that was placed when 17643 * this eager began life in tcp_conn_request. 17644 */ 17645 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17646 17647 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17648 /* 17649 * Someone blewoff the eager before we could finish 17650 * the accept. 17651 * 17652 * The only reason eager exists it because we put in 17653 * a ref on it when conn ind went up. We need to send 17654 * a disconnect indication up while the last reference 17655 * on the eager will be dropped by the squeue when we 17656 * return. 17657 */ 17658 ASSERT(tcp->tcp_listener == NULL); 17659 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17660 struct T_discon_ind *tdi; 17661 17662 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17663 /* 17664 * Let us reuse the incoming mblk to avoid memory 17665 * allocation failure problems. We know that the 17666 * size of the incoming mblk i.e. stroptions is greater 17667 * than sizeof T_discon_ind. So the reallocb below 17668 * can't fail. 17669 */ 17670 freemsg(mp->b_cont); 17671 mp->b_cont = NULL; 17672 ASSERT(DB_REF(mp) == 1); 17673 mp = reallocb(mp, sizeof (struct T_discon_ind), 17674 B_FALSE); 17675 ASSERT(mp != NULL); 17676 DB_TYPE(mp) = M_PROTO; 17677 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17678 tdi = (struct T_discon_ind *)mp->b_rptr; 17679 if (tcp->tcp_issocket) { 17680 tdi->DISCON_reason = ECONNREFUSED; 17681 tdi->SEQ_number = 0; 17682 } else { 17683 tdi->DISCON_reason = ENOPROTOOPT; 17684 tdi->SEQ_number = 17685 tcp->tcp_conn_req_seqnum; 17686 } 17687 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17688 putnext(q, mp); 17689 } else { 17690 freemsg(mp); 17691 } 17692 if (tcp->tcp_hard_binding) { 17693 tcp->tcp_hard_binding = B_FALSE; 17694 tcp->tcp_hard_bound = B_TRUE; 17695 } 17696 tcp->tcp_detached = B_FALSE; 17697 return; 17698 } 17699 17700 mp1 = stropt_mp->b_cont; 17701 stropt_mp->b_cont = NULL; 17702 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17703 stropt = (struct stroptions *)stropt_mp->b_rptr; 17704 17705 while (mp1 != NULL) { 17706 mp = mp1; 17707 mp1 = mp1->b_cont; 17708 mp->b_cont = NULL; 17709 tcp->tcp_drop_opt_ack_cnt++; 17710 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17711 } 17712 mp = NULL; 17713 17714 /* 17715 * For a loopback connection with tcp_direct_sockfs on, note that 17716 * we don't have to protect tcp_rcv_list yet because synchronous 17717 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17718 * possibly race with us. 17719 */ 17720 17721 /* 17722 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17723 * properly. This is the first time we know of the acceptor' 17724 * queue. So we do it here. 17725 */ 17726 if (tcp->tcp_rcv_list == NULL) { 17727 /* 17728 * Recv queue is empty, tcp_rwnd should not have changed. 17729 * That means it should be equal to the listener's tcp_rwnd. 17730 */ 17731 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17732 } else { 17733 #ifdef DEBUG 17734 uint_t cnt = 0; 17735 17736 mp1 = tcp->tcp_rcv_list; 17737 while ((mp = mp1) != NULL) { 17738 mp1 = mp->b_next; 17739 cnt += msgdsize(mp); 17740 } 17741 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17742 #endif 17743 /* There is some data, add them back to get the max. */ 17744 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17745 } 17746 17747 stropt->so_flags = SO_HIWAT; 17748 stropt->so_hiwat = MAX(q->q_hiwat, tcp_sth_rcv_hiwat); 17749 17750 stropt->so_flags |= SO_MAXBLK; 17751 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17752 17753 /* 17754 * This is the first time we run on the correct 17755 * queue after tcp_accept. So fix all the q parameters 17756 * here. 17757 */ 17758 /* Allocate room for SACK options if needed. */ 17759 stropt->so_flags |= SO_WROFF; 17760 if (tcp->tcp_fused) { 17761 ASSERT(tcp->tcp_loopback); 17762 ASSERT(tcp->tcp_loopback_peer != NULL); 17763 /* 17764 * For fused tcp loopback, set the stream head's write 17765 * offset value to zero since we won't be needing any room 17766 * for TCP/IP headers. This would also improve performance 17767 * since it would reduce the amount of work done by kmem. 17768 * Non-fused tcp loopback case is handled separately below. 17769 */ 17770 stropt->so_wroff = 0; 17771 /* 17772 * Record the stream head's high water mark for this endpoint; 17773 * this is used for flow-control purposes in tcp_fuse_output(). 17774 */ 17775 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 17776 /* 17777 * Update the peer's transmit parameters according to 17778 * our recently calculated high water mark value. 17779 */ 17780 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17781 } else if (tcp->tcp_snd_sack_ok) { 17782 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17783 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra); 17784 } else { 17785 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17786 tcp_wroff_xtra); 17787 } 17788 17789 /* 17790 * If this is endpoint is handling SSL, then reserve extra 17791 * offset and space at the end. 17792 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17793 * overriding the previous setting. The extra cost of signing and 17794 * encrypting multiple MSS-size records (12 of them with Ethernet), 17795 * instead of a single contiguous one by the stream head 17796 * largely outweighs the statistical reduction of ACKs, when 17797 * applicable. The peer will also save on decyption and verification 17798 * costs. 17799 */ 17800 if (tcp->tcp_kssl_ctx != NULL) { 17801 stropt->so_wroff += SSL3_WROFFSET; 17802 17803 stropt->so_flags |= SO_TAIL; 17804 stropt->so_tail = SSL3_MAX_TAIL_LEN; 17805 17806 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 17807 } 17808 17809 /* Send the options up */ 17810 putnext(q, stropt_mp); 17811 17812 /* 17813 * Pass up any data and/or a fin that has been received. 17814 * 17815 * Adjust receive window in case it had decreased 17816 * (because there is data <=> tcp_rcv_list != NULL) 17817 * while the connection was detached. Note that 17818 * in case the eager was flow-controlled, w/o this 17819 * code, the rwnd may never open up again! 17820 */ 17821 if (tcp->tcp_rcv_list != NULL) { 17822 /* We drain directly in case of fused tcp loopback */ 17823 if (!tcp->tcp_fused && canputnext(q)) { 17824 tcp->tcp_rwnd = q->q_hiwat; 17825 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 17826 << tcp->tcp_rcv_ws; 17827 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 17828 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17829 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 17830 tcp_xmit_ctl(NULL, 17831 tcp, (tcp->tcp_swnd == 0) ? 17832 tcp->tcp_suna : tcp->tcp_snxt, 17833 tcp->tcp_rnxt, TH_ACK); 17834 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 17835 } 17836 17837 } 17838 (void) tcp_rcv_drain(q, tcp); 17839 17840 /* 17841 * For fused tcp loopback, back-enable peer endpoint 17842 * if it's currently flow-controlled. 17843 */ 17844 if (tcp->tcp_fused && 17845 tcp->tcp_loopback_peer->tcp_flow_stopped) { 17846 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17847 17848 ASSERT(peer_tcp != NULL); 17849 ASSERT(peer_tcp->tcp_fused); 17850 17851 tcp_clrqfull(peer_tcp); 17852 TCP_STAT(tcp_fusion_backenabled); 17853 } 17854 } 17855 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17856 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17857 mp = mi_tpi_ordrel_ind(); 17858 if (mp) { 17859 tcp->tcp_ordrel_done = B_TRUE; 17860 putnext(q, mp); 17861 if (tcp->tcp_deferred_clean_death) { 17862 /* 17863 * tcp_clean_death was deferred 17864 * for T_ORDREL_IND - do it now 17865 */ 17866 (void) tcp_clean_death(tcp, 17867 tcp->tcp_client_errno, 21); 17868 tcp->tcp_deferred_clean_death = B_FALSE; 17869 } 17870 } else { 17871 /* 17872 * Run the orderly release in the 17873 * service routine. 17874 */ 17875 qenable(q); 17876 } 17877 } 17878 if (tcp->tcp_hard_binding) { 17879 tcp->tcp_hard_binding = B_FALSE; 17880 tcp->tcp_hard_bound = B_TRUE; 17881 } 17882 17883 tcp->tcp_detached = B_FALSE; 17884 17885 /* We can enable synchronous streams now */ 17886 if (tcp->tcp_fused) { 17887 tcp_fuse_syncstr_enable_pair(tcp); 17888 } 17889 17890 if (tcp->tcp_ka_enabled) { 17891 tcp->tcp_ka_last_intrvl = 0; 17892 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17893 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17894 } 17895 17896 /* 17897 * At this point, eager is fully established and will 17898 * have the following references - 17899 * 17900 * 2 references for connection to exist (1 for TCP and 1 for IP). 17901 * 1 reference for the squeue which will be dropped by the squeue as 17902 * soon as this function returns. 17903 * There will be 1 additonal reference for being in classifier 17904 * hash list provided something bad hasn't happened. 17905 */ 17906 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17907 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17908 } 17909 17910 /* 17911 * The function called through squeue to get behind listener's perimeter to 17912 * send a deffered conn_ind. 17913 */ 17914 /* ARGSUSED */ 17915 void 17916 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17917 { 17918 conn_t *connp = (conn_t *)arg; 17919 tcp_t *listener = connp->conn_tcp; 17920 17921 if (listener->tcp_state == TCPS_CLOSED || 17922 TCP_IS_DETACHED(listener)) { 17923 /* 17924 * If listener has closed, it would have caused a 17925 * a cleanup/blowoff to happen for the eager. 17926 */ 17927 tcp_t *tcp; 17928 struct T_conn_ind *conn_ind; 17929 17930 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17931 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17932 conn_ind->OPT_length); 17933 /* 17934 * We need to drop the ref on eager that was put 17935 * tcp_rput_data() before trying to send the conn_ind 17936 * to listener. The conn_ind was deferred in tcp_send_conn_ind 17937 * and tcp_wput_accept() is sending this deferred conn_ind but 17938 * listener is closed so we drop the ref. 17939 */ 17940 CONN_DEC_REF(tcp->tcp_connp); 17941 freemsg(mp); 17942 return; 17943 } 17944 putnext(listener->tcp_rq, mp); 17945 } 17946 17947 17948 /* 17949 * This is the STREAMS entry point for T_CONN_RES coming down on 17950 * Acceptor STREAM when sockfs listener does accept processing. 17951 * Read the block comment on top pf tcp_conn_request(). 17952 */ 17953 void 17954 tcp_wput_accept(queue_t *q, mblk_t *mp) 17955 { 17956 queue_t *rq = RD(q); 17957 struct T_conn_res *conn_res; 17958 tcp_t *eager; 17959 tcp_t *listener; 17960 struct T_ok_ack *ok; 17961 t_scalar_t PRIM_type; 17962 mblk_t *opt_mp; 17963 conn_t *econnp; 17964 17965 ASSERT(DB_TYPE(mp) == M_PROTO); 17966 17967 conn_res = (struct T_conn_res *)mp->b_rptr; 17968 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17969 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17970 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17971 if (mp != NULL) 17972 putnext(rq, mp); 17973 return; 17974 } 17975 switch (conn_res->PRIM_type) { 17976 case O_T_CONN_RES: 17977 case T_CONN_RES: 17978 /* 17979 * We pass up an err ack if allocb fails. This will 17980 * cause sockfs to issue a T_DISCON_REQ which will cause 17981 * tcp_eager_blowoff to be called. sockfs will then call 17982 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17983 * we need to do the allocb up here because we have to 17984 * make sure rq->q_qinfo->qi_qclose still points to the 17985 * correct function (tcpclose_accept) in case allocb 17986 * fails. 17987 */ 17988 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17989 if (opt_mp == NULL) { 17990 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17991 if (mp != NULL) 17992 putnext(rq, mp); 17993 return; 17994 } 17995 17996 bcopy(mp->b_rptr + conn_res->OPT_offset, 17997 &eager, conn_res->OPT_length); 17998 PRIM_type = conn_res->PRIM_type; 17999 mp->b_datap->db_type = M_PCPROTO; 18000 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18001 ok = (struct T_ok_ack *)mp->b_rptr; 18002 ok->PRIM_type = T_OK_ACK; 18003 ok->CORRECT_prim = PRIM_type; 18004 econnp = eager->tcp_connp; 18005 econnp->conn_dev = (dev_t)q->q_ptr; 18006 eager->tcp_rq = rq; 18007 eager->tcp_wq = q; 18008 rq->q_ptr = econnp; 18009 rq->q_qinfo = &tcp_rinit; 18010 q->q_ptr = econnp; 18011 q->q_qinfo = &tcp_winit; 18012 listener = eager->tcp_listener; 18013 eager->tcp_issocket = B_TRUE; 18014 18015 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18016 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18017 18018 /* Put the ref for IP */ 18019 CONN_INC_REF(econnp); 18020 18021 /* 18022 * We should have minimum of 3 references on the conn 18023 * at this point. One each for TCP and IP and one for 18024 * the T_conn_ind that was sent up when the 3-way handshake 18025 * completed. In the normal case we would also have another 18026 * reference (making a total of 4) for the conn being in the 18027 * classifier hash list. However the eager could have received 18028 * an RST subsequently and tcp_closei_local could have removed 18029 * the eager from the classifier hash list, hence we can't 18030 * assert that reference. 18031 */ 18032 ASSERT(econnp->conn_ref >= 3); 18033 18034 /* 18035 * Send the new local address also up to sockfs. There 18036 * should already be enough space in the mp that came 18037 * down from soaccept(). 18038 */ 18039 if (eager->tcp_family == AF_INET) { 18040 sin_t *sin; 18041 18042 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18043 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18044 sin = (sin_t *)mp->b_wptr; 18045 mp->b_wptr += sizeof (sin_t); 18046 sin->sin_family = AF_INET; 18047 sin->sin_port = eager->tcp_lport; 18048 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18049 } else { 18050 sin6_t *sin6; 18051 18052 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18053 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18054 sin6 = (sin6_t *)mp->b_wptr; 18055 mp->b_wptr += sizeof (sin6_t); 18056 sin6->sin6_family = AF_INET6; 18057 sin6->sin6_port = eager->tcp_lport; 18058 if (eager->tcp_ipversion == IPV4_VERSION) { 18059 sin6->sin6_flowinfo = 0; 18060 IN6_IPADDR_TO_V4MAPPED( 18061 eager->tcp_ipha->ipha_src, 18062 &sin6->sin6_addr); 18063 } else { 18064 ASSERT(eager->tcp_ip6h != NULL); 18065 sin6->sin6_flowinfo = 18066 eager->tcp_ip6h->ip6_vcf & 18067 ~IPV6_VERS_AND_FLOW_MASK; 18068 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18069 } 18070 sin6->sin6_scope_id = 0; 18071 sin6->__sin6_src_id = 0; 18072 } 18073 18074 putnext(rq, mp); 18075 18076 opt_mp->b_datap->db_type = M_SETOPTS; 18077 opt_mp->b_wptr += sizeof (struct stroptions); 18078 18079 /* 18080 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18081 * from listener to acceptor. The message is chained on the 18082 * bind_mp which tcp_rput_other will send down to IP. 18083 */ 18084 if (listener->tcp_bound_if != 0) { 18085 /* allocate optmgmt req */ 18086 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18087 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18088 sizeof (int)); 18089 if (mp != NULL) 18090 linkb(opt_mp, mp); 18091 } 18092 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18093 uint_t on = 1; 18094 18095 /* allocate optmgmt req */ 18096 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18097 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18098 if (mp != NULL) 18099 linkb(opt_mp, mp); 18100 } 18101 18102 18103 mutex_enter(&listener->tcp_eager_lock); 18104 18105 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18106 18107 tcp_t *tail; 18108 tcp_t *tcp; 18109 mblk_t *mp1; 18110 18111 tcp = listener->tcp_eager_prev_q0; 18112 /* 18113 * listener->tcp_eager_prev_q0 points to the TAIL of the 18114 * deferred T_conn_ind queue. We need to get to the head 18115 * of the queue in order to send up T_conn_ind the same 18116 * order as how the 3WHS is completed. 18117 */ 18118 while (tcp != listener) { 18119 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18120 !tcp->tcp_kssl_pending) 18121 break; 18122 else 18123 tcp = tcp->tcp_eager_prev_q0; 18124 } 18125 /* None of the pending eagers can be sent up now */ 18126 if (tcp == listener) 18127 goto no_more_eagers; 18128 18129 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18130 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18131 /* Move from q0 to q */ 18132 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18133 listener->tcp_conn_req_cnt_q0--; 18134 listener->tcp_conn_req_cnt_q++; 18135 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18136 tcp->tcp_eager_prev_q0; 18137 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18138 tcp->tcp_eager_next_q0; 18139 tcp->tcp_eager_prev_q0 = NULL; 18140 tcp->tcp_eager_next_q0 = NULL; 18141 tcp->tcp_conn_def_q0 = B_FALSE; 18142 18143 /* Make sure the tcp isn't in the list of droppables */ 18144 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18145 tcp->tcp_eager_prev_drop_q0 == NULL); 18146 18147 /* 18148 * Insert at end of the queue because sockfs sends 18149 * down T_CONN_RES in chronological order. Leaving 18150 * the older conn indications at front of the queue 18151 * helps reducing search time. 18152 */ 18153 tail = listener->tcp_eager_last_q; 18154 if (tail != NULL) { 18155 tail->tcp_eager_next_q = tcp; 18156 } else { 18157 listener->tcp_eager_next_q = tcp; 18158 } 18159 listener->tcp_eager_last_q = tcp; 18160 tcp->tcp_eager_next_q = NULL; 18161 18162 /* Need to get inside the listener perimeter */ 18163 CONN_INC_REF(listener->tcp_connp); 18164 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18165 tcp_send_pending, listener->tcp_connp, 18166 SQTAG_TCP_SEND_PENDING); 18167 } 18168 no_more_eagers: 18169 tcp_eager_unlink(eager); 18170 mutex_exit(&listener->tcp_eager_lock); 18171 18172 /* 18173 * At this point, the eager is detached from the listener 18174 * but we still have an extra refs on eager (apart from the 18175 * usual tcp references). The ref was placed in tcp_rput_data 18176 * before sending the conn_ind in tcp_send_conn_ind. 18177 * The ref will be dropped in tcp_accept_finish(). 18178 */ 18179 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18180 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18181 return; 18182 default: 18183 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18184 if (mp != NULL) 18185 putnext(rq, mp); 18186 return; 18187 } 18188 } 18189 18190 void 18191 tcp_wput(queue_t *q, mblk_t *mp) 18192 { 18193 conn_t *connp = Q_TO_CONN(q); 18194 tcp_t *tcp; 18195 void (*output_proc)(); 18196 t_scalar_t type; 18197 uchar_t *rptr; 18198 struct iocblk *iocp; 18199 uint32_t msize; 18200 18201 ASSERT(connp->conn_ref >= 2); 18202 18203 switch (DB_TYPE(mp)) { 18204 case M_DATA: 18205 tcp = connp->conn_tcp; 18206 ASSERT(tcp != NULL); 18207 18208 msize = msgdsize(mp); 18209 18210 mutex_enter(&connp->conn_lock); 18211 CONN_INC_REF_LOCKED(connp); 18212 18213 tcp->tcp_squeue_bytes += msize; 18214 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18215 mutex_exit(&connp->conn_lock); 18216 tcp_setqfull(tcp); 18217 } else 18218 mutex_exit(&connp->conn_lock); 18219 18220 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18221 tcp_output, connp, SQTAG_TCP_OUTPUT); 18222 return; 18223 case M_PROTO: 18224 case M_PCPROTO: 18225 /* 18226 * if it is a snmp message, don't get behind the squeue 18227 */ 18228 tcp = connp->conn_tcp; 18229 rptr = mp->b_rptr; 18230 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18231 type = ((union T_primitives *)rptr)->type; 18232 } else { 18233 if (tcp->tcp_debug) { 18234 (void) strlog(TCP_MOD_ID, 0, 1, 18235 SL_ERROR|SL_TRACE, 18236 "tcp_wput_proto, dropping one..."); 18237 } 18238 freemsg(mp); 18239 return; 18240 } 18241 if (type == T_SVR4_OPTMGMT_REQ) { 18242 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18243 if (snmpcom_req(q, mp, tcp_snmp_set, tcp_snmp_get, 18244 cr)) { 18245 /* 18246 * This was a SNMP request 18247 */ 18248 return; 18249 } else { 18250 output_proc = tcp_wput_proto; 18251 } 18252 } else { 18253 output_proc = tcp_wput_proto; 18254 } 18255 break; 18256 case M_IOCTL: 18257 /* 18258 * Most ioctls can be processed right away without going via 18259 * squeues - process them right here. Those that do require 18260 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18261 * are processed by tcp_wput_ioctl(). 18262 */ 18263 iocp = (struct iocblk *)mp->b_rptr; 18264 tcp = connp->conn_tcp; 18265 18266 switch (iocp->ioc_cmd) { 18267 case TCP_IOC_ABORT_CONN: 18268 tcp_ioctl_abort_conn(q, mp); 18269 return; 18270 case TI_GETPEERNAME: 18271 if (tcp->tcp_state < TCPS_SYN_RCVD) { 18272 iocp->ioc_error = ENOTCONN; 18273 iocp->ioc_count = 0; 18274 mp->b_datap->db_type = M_IOCACK; 18275 qreply(q, mp); 18276 return; 18277 } 18278 /* FALLTHRU */ 18279 case TI_GETMYNAME: 18280 mi_copyin(q, mp, NULL, 18281 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18282 return; 18283 case ND_SET: 18284 /* nd_getset does the necessary checks */ 18285 case ND_GET: 18286 if (!nd_getset(q, tcp_g_nd, mp)) { 18287 CALL_IP_WPUT(connp, q, mp); 18288 return; 18289 } 18290 qreply(q, mp); 18291 return; 18292 case TCP_IOC_DEFAULT_Q: 18293 /* 18294 * Wants to be the default wq. Check the credentials 18295 * first, the rest is executed via squeue. 18296 */ 18297 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 18298 iocp->ioc_error = EPERM; 18299 iocp->ioc_count = 0; 18300 mp->b_datap->db_type = M_IOCACK; 18301 qreply(q, mp); 18302 return; 18303 } 18304 output_proc = tcp_wput_ioctl; 18305 break; 18306 default: 18307 output_proc = tcp_wput_ioctl; 18308 break; 18309 } 18310 break; 18311 default: 18312 output_proc = tcp_wput_nondata; 18313 break; 18314 } 18315 18316 CONN_INC_REF(connp); 18317 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18318 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18319 } 18320 18321 /* 18322 * Initial STREAMS write side put() procedure for sockets. It tries to 18323 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18324 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18325 * are handled by tcp_wput() as usual. 18326 * 18327 * All further messages will also be handled by tcp_wput() because we cannot 18328 * be sure that the above short cut is safe later. 18329 */ 18330 static void 18331 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18332 { 18333 conn_t *connp = Q_TO_CONN(wq); 18334 tcp_t *tcp = connp->conn_tcp; 18335 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18336 18337 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18338 wq->q_qinfo = &tcp_winit; 18339 18340 ASSERT(IPCL_IS_TCP(connp)); 18341 ASSERT(TCP_IS_SOCKET(tcp)); 18342 18343 if (DB_TYPE(mp) == M_PCPROTO && 18344 MBLKL(mp) == sizeof (struct T_capability_req) && 18345 car->PRIM_type == T_CAPABILITY_REQ) { 18346 tcp_capability_req(tcp, mp); 18347 return; 18348 } 18349 18350 tcp_wput(wq, mp); 18351 } 18352 18353 static boolean_t 18354 tcp_zcopy_check(tcp_t *tcp) 18355 { 18356 conn_t *connp = tcp->tcp_connp; 18357 ire_t *ire; 18358 boolean_t zc_enabled = B_FALSE; 18359 18360 if (do_tcpzcopy == 2) 18361 zc_enabled = B_TRUE; 18362 else if (tcp->tcp_ipversion == IPV4_VERSION && 18363 IPCL_IS_CONNECTED(connp) && 18364 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18365 connp->conn_dontroute == 0 && 18366 !connp->conn_nexthop_set && 18367 connp->conn_xmit_if_ill == NULL && 18368 connp->conn_nofailover_ill == NULL && 18369 do_tcpzcopy == 1) { 18370 /* 18371 * the checks above closely resemble the fast path checks 18372 * in tcp_send_data(). 18373 */ 18374 mutex_enter(&connp->conn_lock); 18375 ire = connp->conn_ire_cache; 18376 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18377 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18378 IRE_REFHOLD(ire); 18379 if (ire->ire_stq != NULL) { 18380 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18381 18382 zc_enabled = ill && (ill->ill_capabilities & 18383 ILL_CAPAB_ZEROCOPY) && 18384 (ill->ill_zerocopy_capab-> 18385 ill_zerocopy_flags != 0); 18386 } 18387 IRE_REFRELE(ire); 18388 } 18389 mutex_exit(&connp->conn_lock); 18390 } 18391 tcp->tcp_snd_zcopy_on = zc_enabled; 18392 if (!TCP_IS_DETACHED(tcp)) { 18393 if (zc_enabled) { 18394 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 18395 TCP_STAT(tcp_zcopy_on); 18396 } else { 18397 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18398 TCP_STAT(tcp_zcopy_off); 18399 } 18400 } 18401 return (zc_enabled); 18402 } 18403 18404 static mblk_t * 18405 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18406 { 18407 if (do_tcpzcopy == 2) 18408 return (bp); 18409 else if (tcp->tcp_snd_zcopy_on) { 18410 tcp->tcp_snd_zcopy_on = B_FALSE; 18411 if (!TCP_IS_DETACHED(tcp)) { 18412 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18413 TCP_STAT(tcp_zcopy_disable); 18414 } 18415 } 18416 return (tcp_zcopy_backoff(tcp, bp, 0)); 18417 } 18418 18419 /* 18420 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18421 * the original desballoca'ed segmapped mblk. 18422 */ 18423 static mblk_t * 18424 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18425 { 18426 mblk_t *head, *tail, *nbp; 18427 if (IS_VMLOANED_MBLK(bp)) { 18428 TCP_STAT(tcp_zcopy_backoff); 18429 if ((head = copyb(bp)) == NULL) { 18430 /* fail to backoff; leave it for the next backoff */ 18431 tcp->tcp_xmit_zc_clean = B_FALSE; 18432 return (bp); 18433 } 18434 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18435 if (fix_xmitlist) 18436 tcp_zcopy_notify(tcp); 18437 else 18438 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18439 } 18440 nbp = bp->b_cont; 18441 if (fix_xmitlist) { 18442 head->b_prev = bp->b_prev; 18443 head->b_next = bp->b_next; 18444 if (tcp->tcp_xmit_tail == bp) 18445 tcp->tcp_xmit_tail = head; 18446 } 18447 bp->b_next = NULL; 18448 bp->b_prev = NULL; 18449 freeb(bp); 18450 } else { 18451 head = bp; 18452 nbp = bp->b_cont; 18453 } 18454 tail = head; 18455 while (nbp) { 18456 if (IS_VMLOANED_MBLK(nbp)) { 18457 TCP_STAT(tcp_zcopy_backoff); 18458 if ((tail->b_cont = copyb(nbp)) == NULL) { 18459 tcp->tcp_xmit_zc_clean = B_FALSE; 18460 tail->b_cont = nbp; 18461 return (head); 18462 } 18463 tail = tail->b_cont; 18464 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18465 if (fix_xmitlist) 18466 tcp_zcopy_notify(tcp); 18467 else 18468 tail->b_datap->db_struioflag |= 18469 STRUIO_ZCNOTIFY; 18470 } 18471 bp = nbp; 18472 nbp = nbp->b_cont; 18473 if (fix_xmitlist) { 18474 tail->b_prev = bp->b_prev; 18475 tail->b_next = bp->b_next; 18476 if (tcp->tcp_xmit_tail == bp) 18477 tcp->tcp_xmit_tail = tail; 18478 } 18479 bp->b_next = NULL; 18480 bp->b_prev = NULL; 18481 freeb(bp); 18482 } else { 18483 tail->b_cont = nbp; 18484 tail = nbp; 18485 nbp = nbp->b_cont; 18486 } 18487 } 18488 if (fix_xmitlist) { 18489 tcp->tcp_xmit_last = tail; 18490 tcp->tcp_xmit_zc_clean = B_TRUE; 18491 } 18492 return (head); 18493 } 18494 18495 static void 18496 tcp_zcopy_notify(tcp_t *tcp) 18497 { 18498 struct stdata *stp; 18499 18500 if (tcp->tcp_detached) 18501 return; 18502 stp = STREAM(tcp->tcp_rq); 18503 mutex_enter(&stp->sd_lock); 18504 stp->sd_flag |= STZCNOTIFY; 18505 cv_broadcast(&stp->sd_zcopy_wait); 18506 mutex_exit(&stp->sd_lock); 18507 } 18508 18509 static boolean_t 18510 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18511 { 18512 ire_t *ire; 18513 conn_t *connp = tcp->tcp_connp; 18514 18515 18516 mutex_enter(&connp->conn_lock); 18517 ire = connp->conn_ire_cache; 18518 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18519 18520 if ((ire != NULL) && 18521 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18522 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18523 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18524 IRE_REFHOLD(ire); 18525 mutex_exit(&connp->conn_lock); 18526 } else { 18527 boolean_t cached = B_FALSE; 18528 ts_label_t *tsl; 18529 18530 /* force a recheck later on */ 18531 tcp->tcp_ire_ill_check_done = B_FALSE; 18532 18533 TCP_DBGSTAT(tcp_ire_null1); 18534 connp->conn_ire_cache = NULL; 18535 mutex_exit(&connp->conn_lock); 18536 18537 if (ire != NULL) 18538 IRE_REFRELE_NOTR(ire); 18539 18540 tsl = crgetlabel(CONN_CRED(connp)); 18541 ire = (dst ? ire_cache_lookup(*dst, connp->conn_zoneid, tsl) : 18542 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18543 connp->conn_zoneid, tsl)); 18544 18545 if (ire == NULL) { 18546 TCP_STAT(tcp_ire_null); 18547 return (B_FALSE); 18548 } 18549 18550 IRE_REFHOLD_NOTR(ire); 18551 /* 18552 * Since we are inside the squeue, there cannot be another 18553 * thread in TCP trying to set the conn_ire_cache now. The 18554 * check for IRE_MARK_CONDEMNED ensures that an interface 18555 * unplumb thread has not yet started cleaning up the conns. 18556 * Hence we don't need to grab the conn lock. 18557 */ 18558 if (!(connp->conn_state_flags & CONN_CLOSING)) { 18559 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18560 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18561 connp->conn_ire_cache = ire; 18562 cached = B_TRUE; 18563 } 18564 rw_exit(&ire->ire_bucket->irb_lock); 18565 } 18566 18567 /* 18568 * We can continue to use the ire but since it was 18569 * not cached, we should drop the extra reference. 18570 */ 18571 if (!cached) 18572 IRE_REFRELE_NOTR(ire); 18573 18574 /* 18575 * Rampart note: no need to select a new label here, since 18576 * labels are not allowed to change during the life of a TCP 18577 * connection. 18578 */ 18579 } 18580 18581 *irep = ire; 18582 18583 return (B_TRUE); 18584 } 18585 18586 /* 18587 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18588 * 18589 * 0 = success; 18590 * 1 = failed to find ire and ill. 18591 */ 18592 static boolean_t 18593 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18594 { 18595 ipha_t *ipha; 18596 ipaddr_t dst; 18597 ire_t *ire; 18598 ill_t *ill; 18599 conn_t *connp = tcp->tcp_connp; 18600 mblk_t *ire_fp_mp; 18601 18602 if (mp != NULL) 18603 ipha = (ipha_t *)mp->b_rptr; 18604 else 18605 ipha = tcp->tcp_ipha; 18606 dst = ipha->ipha_dst; 18607 18608 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18609 return (B_FALSE); 18610 18611 if ((ire->ire_flags & RTF_MULTIRT) || 18612 (ire->ire_stq == NULL) || 18613 (ire->ire_nce == NULL) || 18614 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18615 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18616 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18617 TCP_STAT(tcp_ip_ire_send); 18618 IRE_REFRELE(ire); 18619 return (B_FALSE); 18620 } 18621 18622 ill = ire_to_ill(ire); 18623 if (connp->conn_outgoing_ill != NULL) { 18624 ill_t *conn_outgoing_ill = NULL; 18625 /* 18626 * Choose a good ill in the group to send the packets on. 18627 */ 18628 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18629 ill = ire_to_ill(ire); 18630 } 18631 ASSERT(ill != NULL); 18632 18633 if (!tcp->tcp_ire_ill_check_done) { 18634 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18635 tcp->tcp_ire_ill_check_done = B_TRUE; 18636 } 18637 18638 *irep = ire; 18639 *illp = ill; 18640 18641 return (B_TRUE); 18642 } 18643 18644 static void 18645 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18646 { 18647 ipha_t *ipha; 18648 ipaddr_t src; 18649 ipaddr_t dst; 18650 uint32_t cksum; 18651 ire_t *ire; 18652 uint16_t *up; 18653 ill_t *ill; 18654 conn_t *connp = tcp->tcp_connp; 18655 uint32_t hcksum_txflags = 0; 18656 mblk_t *ire_fp_mp; 18657 uint_t ire_fp_mp_len; 18658 18659 ASSERT(DB_TYPE(mp) == M_DATA); 18660 18661 if (DB_CRED(mp) == NULL) 18662 mblk_setcred(mp, CONN_CRED(connp)); 18663 18664 ipha = (ipha_t *)mp->b_rptr; 18665 src = ipha->ipha_src; 18666 dst = ipha->ipha_dst; 18667 18668 /* 18669 * Drop off fast path for IPv6 and also if options are present or 18670 * we need to resolve a TS label. 18671 */ 18672 if (tcp->tcp_ipversion != IPV4_VERSION || 18673 !IPCL_IS_CONNECTED(connp) || 18674 !CONN_IS_LSO_MD_FASTPATH(connp) || 18675 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18676 !connp->conn_ulp_labeled || 18677 ipha->ipha_ident == IP_HDR_INCLUDED || 18678 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18679 IPP_ENABLED(IPP_LOCAL_OUT)) { 18680 if (tcp->tcp_snd_zcopy_aware) 18681 mp = tcp_zcopy_disable(tcp, mp); 18682 TCP_STAT(tcp_ip_send); 18683 CALL_IP_WPUT(connp, q, mp); 18684 return; 18685 } 18686 18687 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18688 if (tcp->tcp_snd_zcopy_aware) 18689 mp = tcp_zcopy_backoff(tcp, mp, 0); 18690 CALL_IP_WPUT(connp, q, mp); 18691 return; 18692 } 18693 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18694 ire_fp_mp_len = MBLKL(ire_fp_mp); 18695 18696 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18697 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18698 #ifndef _BIG_ENDIAN 18699 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18700 #endif 18701 18702 /* 18703 * Check to see if we need to re-enable LSO/MDT for this connection 18704 * because it was previously disabled due to changes in the ill; 18705 * note that by doing it here, this re-enabling only applies when 18706 * the packet is not dispatched through CALL_IP_WPUT(). 18707 * 18708 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18709 * case, since that's how we ended up here. For IPv6, we do the 18710 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18711 */ 18712 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18713 /* 18714 * Restore LSO for this connection, so that next time around 18715 * it is eligible to go through tcp_lsosend() path again. 18716 */ 18717 TCP_STAT(tcp_lso_enabled); 18718 tcp->tcp_lso = B_TRUE; 18719 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18720 "interface %s\n", (void *)connp, ill->ill_name)); 18721 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18722 /* 18723 * Restore MDT for this connection, so that next time around 18724 * it is eligible to go through tcp_multisend() path again. 18725 */ 18726 TCP_STAT(tcp_mdt_conn_resumed1); 18727 tcp->tcp_mdt = B_TRUE; 18728 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18729 "interface %s\n", (void *)connp, ill->ill_name)); 18730 } 18731 18732 if (tcp->tcp_snd_zcopy_aware) { 18733 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18734 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18735 mp = tcp_zcopy_disable(tcp, mp); 18736 /* 18737 * we shouldn't need to reset ipha as the mp containing 18738 * ipha should never be a zero-copy mp. 18739 */ 18740 } 18741 18742 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18743 ASSERT(ill->ill_hcksum_capab != NULL); 18744 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18745 } 18746 18747 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18748 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18749 18750 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18751 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18752 18753 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18754 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18755 18756 /* Software checksum? */ 18757 if (DB_CKSUMFLAGS(mp) == 0) { 18758 TCP_STAT(tcp_out_sw_cksum); 18759 TCP_STAT_UPDATE(tcp_out_sw_cksum_bytes, 18760 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18761 } 18762 18763 ipha->ipha_fragment_offset_and_flags |= 18764 (uint32_t)htons(ire->ire_frag_flag); 18765 18766 /* Calculate IP header checksum if hardware isn't capable */ 18767 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18768 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18769 ((uint16_t *)ipha)[4]); 18770 } 18771 18772 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18773 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18774 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18775 18776 UPDATE_OB_PKT_COUNT(ire); 18777 ire->ire_last_used_time = lbolt; 18778 BUMP_MIB(&ip_mib, ipOutRequests); 18779 18780 if (ILL_DLS_CAPABLE(ill)) { 18781 /* 18782 * Send the packet directly to DLD, where it may be queued 18783 * depending on the availability of transmit resources at 18784 * the media layer. 18785 */ 18786 IP_DLS_ILL_TX(ill, ipha, mp); 18787 } else { 18788 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 18789 DTRACE_PROBE4(ip4__physical__out__start, 18790 ill_t *, NULL, ill_t *, out_ill, 18791 ipha_t *, ipha, mblk_t *, mp); 18792 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 18793 NULL, out_ill, ipha, mp, mp); 18794 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 18795 if (mp != NULL) 18796 putnext(ire->ire_stq, mp); 18797 } 18798 IRE_REFRELE(ire); 18799 } 18800 18801 /* 18802 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18803 * if the receiver shrinks the window, i.e. moves the right window to the 18804 * left, the we should not send new data, but should retransmit normally the 18805 * old unacked data between suna and suna + swnd. We might has sent data 18806 * that is now outside the new window, pretend that we didn't send it. 18807 */ 18808 static void 18809 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18810 { 18811 uint32_t snxt = tcp->tcp_snxt; 18812 mblk_t *xmit_tail; 18813 int32_t offset; 18814 18815 ASSERT(shrunk_count > 0); 18816 18817 /* Pretend we didn't send the data outside the window */ 18818 snxt -= shrunk_count; 18819 18820 /* Get the mblk and the offset in it per the shrunk window */ 18821 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 18822 18823 ASSERT(xmit_tail != NULL); 18824 18825 /* Reset all the values per the now shrunk window */ 18826 tcp->tcp_snxt = snxt; 18827 tcp->tcp_xmit_tail = xmit_tail; 18828 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 18829 offset; 18830 tcp->tcp_unsent += shrunk_count; 18831 18832 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18833 /* 18834 * Make sure the timer is running so that we will probe a zero 18835 * window. 18836 */ 18837 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18838 } 18839 18840 18841 /* 18842 * The TCP normal data output path. 18843 * NOTE: the logic of the fast path is duplicated from this function. 18844 */ 18845 static void 18846 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18847 { 18848 int len; 18849 mblk_t *local_time; 18850 mblk_t *mp1; 18851 uint32_t snxt; 18852 int tail_unsent; 18853 int tcpstate; 18854 int usable = 0; 18855 mblk_t *xmit_tail; 18856 queue_t *q = tcp->tcp_wq; 18857 int32_t mss; 18858 int32_t num_sack_blk = 0; 18859 int32_t tcp_hdr_len; 18860 int32_t tcp_tcp_hdr_len; 18861 int mdt_thres; 18862 int rc; 18863 18864 tcpstate = tcp->tcp_state; 18865 if (mp == NULL) { 18866 /* 18867 * tcp_wput_data() with NULL mp should only be called when 18868 * there is unsent data. 18869 */ 18870 ASSERT(tcp->tcp_unsent > 0); 18871 /* Really tacky... but we need this for detached closes. */ 18872 len = tcp->tcp_unsent; 18873 goto data_null; 18874 } 18875 18876 #if CCS_STATS 18877 wrw_stats.tot.count++; 18878 wrw_stats.tot.bytes += msgdsize(mp); 18879 #endif 18880 ASSERT(mp->b_datap->db_type == M_DATA); 18881 /* 18882 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18883 * or before a connection attempt has begun. 18884 */ 18885 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18886 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18887 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18888 #ifdef DEBUG 18889 cmn_err(CE_WARN, 18890 "tcp_wput_data: data after ordrel, %s", 18891 tcp_display(tcp, NULL, 18892 DISP_ADDR_AND_PORT)); 18893 #else 18894 if (tcp->tcp_debug) { 18895 (void) strlog(TCP_MOD_ID, 0, 1, 18896 SL_TRACE|SL_ERROR, 18897 "tcp_wput_data: data after ordrel, %s\n", 18898 tcp_display(tcp, NULL, 18899 DISP_ADDR_AND_PORT)); 18900 } 18901 #endif /* DEBUG */ 18902 } 18903 if (tcp->tcp_snd_zcopy_aware && 18904 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18905 tcp_zcopy_notify(tcp); 18906 freemsg(mp); 18907 if (tcp->tcp_flow_stopped && 18908 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18909 tcp_clrqfull(tcp); 18910 } 18911 return; 18912 } 18913 18914 /* Strip empties */ 18915 for (;;) { 18916 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18917 (uintptr_t)INT_MAX); 18918 len = (int)(mp->b_wptr - mp->b_rptr); 18919 if (len > 0) 18920 break; 18921 mp1 = mp; 18922 mp = mp->b_cont; 18923 freeb(mp1); 18924 if (!mp) { 18925 return; 18926 } 18927 } 18928 18929 /* If we are the first on the list ... */ 18930 if (tcp->tcp_xmit_head == NULL) { 18931 tcp->tcp_xmit_head = mp; 18932 tcp->tcp_xmit_tail = mp; 18933 tcp->tcp_xmit_tail_unsent = len; 18934 } else { 18935 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18936 struct datab *dp; 18937 18938 mp1 = tcp->tcp_xmit_last; 18939 if (len < tcp_tx_pull_len && 18940 (dp = mp1->b_datap)->db_ref == 1 && 18941 dp->db_lim - mp1->b_wptr >= len) { 18942 ASSERT(len > 0); 18943 ASSERT(!mp1->b_cont); 18944 if (len == 1) { 18945 *mp1->b_wptr++ = *mp->b_rptr; 18946 } else { 18947 bcopy(mp->b_rptr, mp1->b_wptr, len); 18948 mp1->b_wptr += len; 18949 } 18950 if (mp1 == tcp->tcp_xmit_tail) 18951 tcp->tcp_xmit_tail_unsent += len; 18952 mp1->b_cont = mp->b_cont; 18953 if (tcp->tcp_snd_zcopy_aware && 18954 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18955 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18956 freeb(mp); 18957 mp = mp1; 18958 } else { 18959 tcp->tcp_xmit_last->b_cont = mp; 18960 } 18961 len += tcp->tcp_unsent; 18962 } 18963 18964 /* Tack on however many more positive length mblks we have */ 18965 if ((mp1 = mp->b_cont) != NULL) { 18966 do { 18967 int tlen; 18968 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18969 (uintptr_t)INT_MAX); 18970 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18971 if (tlen <= 0) { 18972 mp->b_cont = mp1->b_cont; 18973 freeb(mp1); 18974 } else { 18975 len += tlen; 18976 mp = mp1; 18977 } 18978 } while ((mp1 = mp->b_cont) != NULL); 18979 } 18980 tcp->tcp_xmit_last = mp; 18981 tcp->tcp_unsent = len; 18982 18983 if (urgent) 18984 usable = 1; 18985 18986 data_null: 18987 snxt = tcp->tcp_snxt; 18988 xmit_tail = tcp->tcp_xmit_tail; 18989 tail_unsent = tcp->tcp_xmit_tail_unsent; 18990 18991 /* 18992 * Note that tcp_mss has been adjusted to take into account the 18993 * timestamp option if applicable. Because SACK options do not 18994 * appear in every TCP segments and they are of variable lengths, 18995 * they cannot be included in tcp_mss. Thus we need to calculate 18996 * the actual segment length when we need to send a segment which 18997 * includes SACK options. 18998 */ 18999 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19000 int32_t opt_len; 19001 19002 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19003 tcp->tcp_num_sack_blk); 19004 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19005 2 + TCPOPT_HEADER_LEN; 19006 mss = tcp->tcp_mss - opt_len; 19007 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19008 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19009 } else { 19010 mss = tcp->tcp_mss; 19011 tcp_hdr_len = tcp->tcp_hdr_len; 19012 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19013 } 19014 19015 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19016 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19017 SET_TCP_INIT_CWND(tcp, mss, tcp_slow_start_after_idle); 19018 } 19019 if (tcpstate == TCPS_SYN_RCVD) { 19020 /* 19021 * The three-way connection establishment handshake is not 19022 * complete yet. We want to queue the data for transmission 19023 * after entering ESTABLISHED state (RFC793). A jump to 19024 * "done" label effectively leaves data on the queue. 19025 */ 19026 goto done; 19027 } else { 19028 int usable_r; 19029 19030 /* 19031 * In the special case when cwnd is zero, which can only 19032 * happen if the connection is ECN capable, return now. 19033 * New segments is sent using tcp_timer(). The timer 19034 * is set in tcp_rput_data(). 19035 */ 19036 if (tcp->tcp_cwnd == 0) { 19037 /* 19038 * Note that tcp_cwnd is 0 before 3-way handshake is 19039 * finished. 19040 */ 19041 ASSERT(tcp->tcp_ecn_ok || 19042 tcp->tcp_state < TCPS_ESTABLISHED); 19043 return; 19044 } 19045 19046 /* NOTE: trouble if xmitting while SYN not acked? */ 19047 usable_r = snxt - tcp->tcp_suna; 19048 usable_r = tcp->tcp_swnd - usable_r; 19049 19050 /* 19051 * Check if the receiver has shrunk the window. If 19052 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19053 * cannot be set as there is unsent data, so FIN cannot 19054 * be sent out. Otherwise, we need to take into account 19055 * of FIN as it consumes an "invisible" sequence number. 19056 */ 19057 ASSERT(tcp->tcp_fin_sent == 0); 19058 if (usable_r < 0) { 19059 /* 19060 * The receiver has shrunk the window and we have sent 19061 * -usable_r date beyond the window, re-adjust. 19062 * 19063 * If TCP window scaling is enabled, there can be 19064 * round down error as the advertised receive window 19065 * is actually right shifted n bits. This means that 19066 * the lower n bits info is wiped out. It will look 19067 * like the window is shrunk. Do a check here to 19068 * see if the shrunk amount is actually within the 19069 * error in window calculation. If it is, just 19070 * return. Note that this check is inside the 19071 * shrunk window check. This makes sure that even 19072 * though tcp_process_shrunk_swnd() is not called, 19073 * we will stop further processing. 19074 */ 19075 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19076 tcp_process_shrunk_swnd(tcp, -usable_r); 19077 } 19078 return; 19079 } 19080 19081 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19082 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19083 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19084 19085 /* usable = MIN(usable, unsent) */ 19086 if (usable_r > len) 19087 usable_r = len; 19088 19089 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19090 if (usable_r > 0) { 19091 usable = usable_r; 19092 } else { 19093 /* Bypass all other unnecessary processing. */ 19094 goto done; 19095 } 19096 } 19097 19098 local_time = (mblk_t *)lbolt; 19099 19100 /* 19101 * "Our" Nagle Algorithm. This is not the same as in the old 19102 * BSD. This is more in line with the true intent of Nagle. 19103 * 19104 * The conditions are: 19105 * 1. The amount of unsent data (or amount of data which can be 19106 * sent, whichever is smaller) is less than Nagle limit. 19107 * 2. The last sent size is also less than Nagle limit. 19108 * 3. There is unack'ed data. 19109 * 4. Urgent pointer is not set. Send urgent data ignoring the 19110 * Nagle algorithm. This reduces the probability that urgent 19111 * bytes get "merged" together. 19112 * 5. The app has not closed the connection. This eliminates the 19113 * wait time of the receiving side waiting for the last piece of 19114 * (small) data. 19115 * 19116 * If all are satisified, exit without sending anything. Note 19117 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19118 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19119 * 4095). 19120 */ 19121 if (usable < (int)tcp->tcp_naglim && 19122 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19123 snxt != tcp->tcp_suna && 19124 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19125 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19126 goto done; 19127 } 19128 19129 if (tcp->tcp_cork) { 19130 /* 19131 * if the tcp->tcp_cork option is set, then we have to force 19132 * TCP not to send partial segment (smaller than MSS bytes). 19133 * We are calculating the usable now based on full mss and 19134 * will save the rest of remaining data for later. 19135 */ 19136 if (usable < mss) 19137 goto done; 19138 usable = (usable / mss) * mss; 19139 } 19140 19141 /* Update the latest receive window size in TCP header. */ 19142 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19143 tcp->tcp_tcph->th_win); 19144 19145 /* 19146 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19147 * 19148 * 1. Simple TCP/IP{v4,v6} (no options). 19149 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19150 * 3. If the TCP connection is in ESTABLISHED state. 19151 * 4. The TCP is not detached. 19152 * 19153 * If any of the above conditions have changed during the 19154 * connection, stop using LSO/MDT and restore the stream head 19155 * parameters accordingly. 19156 */ 19157 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19158 ((tcp->tcp_ipversion == IPV4_VERSION && 19159 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19160 (tcp->tcp_ipversion == IPV6_VERSION && 19161 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19162 tcp->tcp_state != TCPS_ESTABLISHED || 19163 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19164 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19165 IPP_ENABLED(IPP_LOCAL_OUT))) { 19166 if (tcp->tcp_lso) { 19167 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19168 tcp->tcp_lso = B_FALSE; 19169 } else { 19170 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19171 tcp->tcp_mdt = B_FALSE; 19172 } 19173 19174 /* Anything other than detached is considered pathological */ 19175 if (!TCP_IS_DETACHED(tcp)) { 19176 if (tcp->tcp_lso) 19177 TCP_STAT(tcp_lso_disabled); 19178 else 19179 TCP_STAT(tcp_mdt_conn_halted1); 19180 (void) tcp_maxpsz_set(tcp, B_TRUE); 19181 } 19182 } 19183 19184 /* Use MDT if sendable amount is greater than the threshold */ 19185 if (tcp->tcp_mdt && 19186 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19187 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19188 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19189 (tcp->tcp_valid_bits == 0 || 19190 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19191 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19192 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19193 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19194 local_time, mdt_thres); 19195 } else { 19196 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19197 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19198 local_time, INT_MAX); 19199 } 19200 19201 /* Pretend that all we were trying to send really got sent */ 19202 if (rc < 0 && tail_unsent < 0) { 19203 do { 19204 xmit_tail = xmit_tail->b_cont; 19205 xmit_tail->b_prev = local_time; 19206 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19207 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19208 tail_unsent += (int)(xmit_tail->b_wptr - 19209 xmit_tail->b_rptr); 19210 } while (tail_unsent < 0); 19211 } 19212 done:; 19213 tcp->tcp_xmit_tail = xmit_tail; 19214 tcp->tcp_xmit_tail_unsent = tail_unsent; 19215 len = tcp->tcp_snxt - snxt; 19216 if (len) { 19217 /* 19218 * If new data was sent, need to update the notsack 19219 * list, which is, afterall, data blocks that have 19220 * not been sack'ed by the receiver. New data is 19221 * not sack'ed. 19222 */ 19223 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19224 /* len is a negative value. */ 19225 tcp->tcp_pipe -= len; 19226 tcp_notsack_update(&(tcp->tcp_notsack_list), 19227 tcp->tcp_snxt, snxt, 19228 &(tcp->tcp_num_notsack_blk), 19229 &(tcp->tcp_cnt_notsack_list)); 19230 } 19231 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19232 tcp->tcp_rack = tcp->tcp_rnxt; 19233 tcp->tcp_rack_cnt = 0; 19234 if ((snxt + len) == tcp->tcp_suna) { 19235 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19236 } 19237 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19238 /* 19239 * Didn't send anything. Make sure the timer is running 19240 * so that we will probe a zero window. 19241 */ 19242 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19243 } 19244 /* Note that len is the amount we just sent but with a negative sign */ 19245 tcp->tcp_unsent += len; 19246 if (tcp->tcp_flow_stopped) { 19247 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19248 tcp_clrqfull(tcp); 19249 } 19250 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19251 tcp_setqfull(tcp); 19252 } 19253 } 19254 19255 /* 19256 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19257 * outgoing TCP header with the template header, as well as other 19258 * options such as time-stamp, ECN and/or SACK. 19259 */ 19260 static void 19261 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19262 { 19263 tcph_t *tcp_tmpl, *tcp_h; 19264 uint32_t *dst, *src; 19265 int hdrlen; 19266 19267 ASSERT(OK_32PTR(rptr)); 19268 19269 /* Template header */ 19270 tcp_tmpl = tcp->tcp_tcph; 19271 19272 /* Header of outgoing packet */ 19273 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19274 19275 /* dst and src are opaque 32-bit fields, used for copying */ 19276 dst = (uint32_t *)rptr; 19277 src = (uint32_t *)tcp->tcp_iphc; 19278 hdrlen = tcp->tcp_hdr_len; 19279 19280 /* Fill time-stamp option if needed */ 19281 if (tcp->tcp_snd_ts_ok) { 19282 U32_TO_BE32((uint32_t)now, 19283 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19284 U32_TO_BE32(tcp->tcp_ts_recent, 19285 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19286 } else { 19287 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19288 } 19289 19290 /* 19291 * Copy the template header; is this really more efficient than 19292 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19293 * but perhaps not for other scenarios. 19294 */ 19295 dst[0] = src[0]; 19296 dst[1] = src[1]; 19297 dst[2] = src[2]; 19298 dst[3] = src[3]; 19299 dst[4] = src[4]; 19300 dst[5] = src[5]; 19301 dst[6] = src[6]; 19302 dst[7] = src[7]; 19303 dst[8] = src[8]; 19304 dst[9] = src[9]; 19305 if (hdrlen -= 40) { 19306 hdrlen >>= 2; 19307 dst += 10; 19308 src += 10; 19309 do { 19310 *dst++ = *src++; 19311 } while (--hdrlen); 19312 } 19313 19314 /* 19315 * Set the ECN info in the TCP header if it is not a zero 19316 * window probe. Zero window probe is only sent in 19317 * tcp_wput_data() and tcp_timer(). 19318 */ 19319 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19320 SET_ECT(tcp, rptr); 19321 19322 if (tcp->tcp_ecn_echo_on) 19323 tcp_h->th_flags[0] |= TH_ECE; 19324 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19325 tcp_h->th_flags[0] |= TH_CWR; 19326 tcp->tcp_ecn_cwr_sent = B_TRUE; 19327 } 19328 } 19329 19330 /* Fill in SACK options */ 19331 if (num_sack_blk > 0) { 19332 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19333 sack_blk_t *tmp; 19334 int32_t i; 19335 19336 wptr[0] = TCPOPT_NOP; 19337 wptr[1] = TCPOPT_NOP; 19338 wptr[2] = TCPOPT_SACK; 19339 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19340 sizeof (sack_blk_t); 19341 wptr += TCPOPT_REAL_SACK_LEN; 19342 19343 tmp = tcp->tcp_sack_list; 19344 for (i = 0; i < num_sack_blk; i++) { 19345 U32_TO_BE32(tmp[i].begin, wptr); 19346 wptr += sizeof (tcp_seq); 19347 U32_TO_BE32(tmp[i].end, wptr); 19348 wptr += sizeof (tcp_seq); 19349 } 19350 tcp_h->th_offset_and_rsrvd[0] += 19351 ((num_sack_blk * 2 + 1) << 4); 19352 } 19353 } 19354 19355 /* 19356 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19357 * the destination address and SAP attribute, and if necessary, the 19358 * hardware checksum offload attribute to a Multidata message. 19359 */ 19360 static int 19361 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19362 const uint32_t start, const uint32_t stuff, const uint32_t end, 19363 const uint32_t flags) 19364 { 19365 /* Add global destination address & SAP attribute */ 19366 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19367 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19368 "destination address+SAP\n")); 19369 19370 if (dlmp != NULL) 19371 TCP_STAT(tcp_mdt_allocfail); 19372 return (-1); 19373 } 19374 19375 /* Add global hwcksum attribute */ 19376 if (hwcksum && 19377 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19378 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19379 "checksum attribute\n")); 19380 19381 TCP_STAT(tcp_mdt_allocfail); 19382 return (-1); 19383 } 19384 19385 return (0); 19386 } 19387 19388 /* 19389 * Smaller and private version of pdescinfo_t used specifically for TCP, 19390 * which allows for only two payload spans per packet. 19391 */ 19392 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19393 19394 /* 19395 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19396 * scheme, and returns one the following: 19397 * 19398 * -1 = failed allocation. 19399 * 0 = success; burst count reached, or usable send window is too small, 19400 * and that we'd rather wait until later before sending again. 19401 */ 19402 static int 19403 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19404 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19405 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19406 const int mdt_thres) 19407 { 19408 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19409 multidata_t *mmd; 19410 uint_t obsegs, obbytes, hdr_frag_sz; 19411 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19412 int num_burst_seg, max_pld; 19413 pdesc_t *pkt; 19414 tcp_pdescinfo_t tcp_pkt_info; 19415 pdescinfo_t *pkt_info; 19416 int pbuf_idx, pbuf_idx_nxt; 19417 int seg_len, len, spill, af; 19418 boolean_t add_buffer, zcopy, clusterwide; 19419 boolean_t buf_trunked = B_FALSE; 19420 boolean_t rconfirm = B_FALSE; 19421 boolean_t done = B_FALSE; 19422 uint32_t cksum; 19423 uint32_t hwcksum_flags; 19424 ire_t *ire = NULL; 19425 ill_t *ill; 19426 ipha_t *ipha; 19427 ip6_t *ip6h; 19428 ipaddr_t src, dst; 19429 ill_zerocopy_capab_t *zc_cap = NULL; 19430 uint16_t *up; 19431 int err; 19432 conn_t *connp; 19433 mblk_t *mp, *mp1, *fw_mp_head = NULL; 19434 uchar_t *pld_start; 19435 19436 #ifdef _BIG_ENDIAN 19437 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19438 #else 19439 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19440 #endif 19441 19442 #define PREP_NEW_MULTIDATA() { \ 19443 mmd = NULL; \ 19444 md_mp = md_hbuf = NULL; \ 19445 cur_hdr_off = 0; \ 19446 max_pld = tcp->tcp_mdt_max_pld; \ 19447 pbuf_idx = pbuf_idx_nxt = -1; \ 19448 add_buffer = B_TRUE; \ 19449 zcopy = B_FALSE; \ 19450 } 19451 19452 #define PREP_NEW_PBUF() { \ 19453 md_pbuf = md_pbuf_nxt = NULL; \ 19454 pbuf_idx = pbuf_idx_nxt = -1; \ 19455 cur_pld_off = 0; \ 19456 first_snxt = *snxt; \ 19457 ASSERT(*tail_unsent > 0); \ 19458 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19459 } 19460 19461 ASSERT(mdt_thres >= mss); 19462 ASSERT(*usable > 0 && *usable > mdt_thres); 19463 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19464 ASSERT(!TCP_IS_DETACHED(tcp)); 19465 ASSERT(tcp->tcp_valid_bits == 0 || 19466 tcp->tcp_valid_bits == TCP_FSS_VALID); 19467 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19468 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19469 (tcp->tcp_ipversion == IPV6_VERSION && 19470 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19471 19472 connp = tcp->tcp_connp; 19473 ASSERT(connp != NULL); 19474 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19475 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19476 19477 /* 19478 * Note that tcp will only declare at most 2 payload spans per 19479 * packet, which is much lower than the maximum allowable number 19480 * of packet spans per Multidata. For this reason, we use the 19481 * privately declared and smaller descriptor info structure, in 19482 * order to save some stack space. 19483 */ 19484 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19485 19486 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19487 if (af == AF_INET) { 19488 dst = tcp->tcp_ipha->ipha_dst; 19489 src = tcp->tcp_ipha->ipha_src; 19490 ASSERT(!CLASSD(dst)); 19491 } 19492 ASSERT(af == AF_INET || 19493 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19494 19495 obsegs = obbytes = 0; 19496 num_burst_seg = tcp->tcp_snd_burst; 19497 md_mp_head = NULL; 19498 PREP_NEW_MULTIDATA(); 19499 19500 /* 19501 * Before we go on further, make sure there is an IRE that we can 19502 * use, and that the ILL supports MDT. Otherwise, there's no point 19503 * in proceeding any further, and we should just hand everything 19504 * off to the legacy path. 19505 */ 19506 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19507 goto legacy_send_no_md; 19508 19509 ASSERT(ire != NULL); 19510 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19511 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19512 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19513 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19514 /* 19515 * If we do support loopback for MDT (which requires modifications 19516 * to the receiving paths), the following assertions should go away, 19517 * and we would be sending the Multidata to loopback conn later on. 19518 */ 19519 ASSERT(!IRE_IS_LOCAL(ire)); 19520 ASSERT(ire->ire_stq != NULL); 19521 19522 ill = ire_to_ill(ire); 19523 ASSERT(ill != NULL); 19524 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19525 19526 if (!tcp->tcp_ire_ill_check_done) { 19527 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19528 tcp->tcp_ire_ill_check_done = B_TRUE; 19529 } 19530 19531 /* 19532 * If the underlying interface conditions have changed, or if the 19533 * new interface does not support MDT, go back to legacy path. 19534 */ 19535 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19536 /* don't go through this path anymore for this connection */ 19537 TCP_STAT(tcp_mdt_conn_halted2); 19538 tcp->tcp_mdt = B_FALSE; 19539 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19540 "interface %s\n", (void *)connp, ill->ill_name)); 19541 /* IRE will be released prior to returning */ 19542 goto legacy_send_no_md; 19543 } 19544 19545 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19546 zc_cap = ill->ill_zerocopy_capab; 19547 19548 /* 19549 * Check if we can take tcp fast-path. Note that "incomplete" 19550 * ire's (where the link-layer for next hop is not resolved 19551 * or where the fast-path header in nce_fp_mp is not available 19552 * yet) are sent down the legacy (slow) path. 19553 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19554 */ 19555 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19556 /* IRE will be released prior to returning */ 19557 goto legacy_send_no_md; 19558 } 19559 19560 /* go to legacy path if interface doesn't support zerocopy */ 19561 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19562 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19563 /* IRE will be released prior to returning */ 19564 goto legacy_send_no_md; 19565 } 19566 19567 /* does the interface support hardware checksum offload? */ 19568 hwcksum_flags = 0; 19569 if (ILL_HCKSUM_CAPABLE(ill) && 19570 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19571 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19572 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19573 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19574 HCKSUM_IPHDRCKSUM) 19575 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19576 19577 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19578 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19579 hwcksum_flags |= HCK_FULLCKSUM; 19580 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19581 HCKSUM_INET_PARTIAL) 19582 hwcksum_flags |= HCK_PARTIALCKSUM; 19583 } 19584 19585 /* 19586 * Each header fragment consists of the leading extra space, 19587 * followed by the TCP/IP header, and the trailing extra space. 19588 * We make sure that each header fragment begins on a 32-bit 19589 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19590 * aligned in tcp_mdt_update). 19591 */ 19592 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19593 tcp->tcp_mdt_hdr_tail), 4); 19594 19595 /* are we starting from the beginning of data block? */ 19596 if (*tail_unsent == 0) { 19597 *xmit_tail = (*xmit_tail)->b_cont; 19598 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19599 *tail_unsent = (int)MBLKL(*xmit_tail); 19600 } 19601 19602 /* 19603 * Here we create one or more Multidata messages, each made up of 19604 * one header buffer and up to N payload buffers. This entire 19605 * operation is done within two loops: 19606 * 19607 * The outer loop mostly deals with creating the Multidata message, 19608 * as well as the header buffer that gets added to it. It also 19609 * links the Multidata messages together such that all of them can 19610 * be sent down to the lower layer in a single putnext call; this 19611 * linking behavior depends on the tcp_mdt_chain tunable. 19612 * 19613 * The inner loop takes an existing Multidata message, and adds 19614 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19615 * packetizes those buffers by filling up the corresponding header 19616 * buffer fragments with the proper IP and TCP headers, and by 19617 * describing the layout of each packet in the packet descriptors 19618 * that get added to the Multidata. 19619 */ 19620 do { 19621 /* 19622 * If usable send window is too small, or data blocks in 19623 * transmit list are smaller than our threshold (i.e. app 19624 * performs large writes followed by small ones), we hand 19625 * off the control over to the legacy path. Note that we'll 19626 * get back the control once it encounters a large block. 19627 */ 19628 if (*usable < mss || (*tail_unsent <= mdt_thres && 19629 (*xmit_tail)->b_cont != NULL && 19630 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19631 /* send down what we've got so far */ 19632 if (md_mp_head != NULL) { 19633 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19634 obsegs, obbytes, &rconfirm); 19635 } 19636 /* 19637 * Pass control over to tcp_send(), but tell it to 19638 * return to us once a large-size transmission is 19639 * possible. 19640 */ 19641 TCP_STAT(tcp_mdt_legacy_small); 19642 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19643 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19644 tail_unsent, xmit_tail, local_time, 19645 mdt_thres)) <= 0) { 19646 /* burst count reached, or alloc failed */ 19647 IRE_REFRELE(ire); 19648 return (err); 19649 } 19650 19651 /* tcp_send() may have sent everything, so check */ 19652 if (*usable <= 0) { 19653 IRE_REFRELE(ire); 19654 return (0); 19655 } 19656 19657 TCP_STAT(tcp_mdt_legacy_ret); 19658 /* 19659 * We may have delivered the Multidata, so make sure 19660 * to re-initialize before the next round. 19661 */ 19662 md_mp_head = NULL; 19663 obsegs = obbytes = 0; 19664 num_burst_seg = tcp->tcp_snd_burst; 19665 PREP_NEW_MULTIDATA(); 19666 19667 /* are we starting from the beginning of data block? */ 19668 if (*tail_unsent == 0) { 19669 *xmit_tail = (*xmit_tail)->b_cont; 19670 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19671 (uintptr_t)INT_MAX); 19672 *tail_unsent = (int)MBLKL(*xmit_tail); 19673 } 19674 } 19675 19676 /* 19677 * max_pld limits the number of mblks in tcp's transmit 19678 * queue that can be added to a Multidata message. Once 19679 * this counter reaches zero, no more additional mblks 19680 * can be added to it. What happens afterwards depends 19681 * on whether or not we are set to chain the Multidata 19682 * messages. If we are to link them together, reset 19683 * max_pld to its original value (tcp_mdt_max_pld) and 19684 * prepare to create a new Multidata message which will 19685 * get linked to md_mp_head. Else, leave it alone and 19686 * let the inner loop break on its own. 19687 */ 19688 if (tcp_mdt_chain && max_pld == 0) 19689 PREP_NEW_MULTIDATA(); 19690 19691 /* adding a payload buffer; re-initialize values */ 19692 if (add_buffer) 19693 PREP_NEW_PBUF(); 19694 19695 /* 19696 * If we don't have a Multidata, either because we just 19697 * (re)entered this outer loop, or after we branched off 19698 * to tcp_send above, setup the Multidata and header 19699 * buffer to be used. 19700 */ 19701 if (md_mp == NULL) { 19702 int md_hbuflen; 19703 uint32_t start, stuff; 19704 19705 /* 19706 * Calculate Multidata header buffer size large enough 19707 * to hold all of the headers that can possibly be 19708 * sent at this moment. We'd rather over-estimate 19709 * the size than running out of space; this is okay 19710 * since this buffer is small anyway. 19711 */ 19712 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19713 19714 /* 19715 * Start and stuff offset for partial hardware 19716 * checksum offload; these are currently for IPv4. 19717 * For full checksum offload, they are set to zero. 19718 */ 19719 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19720 if (af == AF_INET) { 19721 start = IP_SIMPLE_HDR_LENGTH; 19722 stuff = IP_SIMPLE_HDR_LENGTH + 19723 TCP_CHECKSUM_OFFSET; 19724 } else { 19725 start = IPV6_HDR_LEN; 19726 stuff = IPV6_HDR_LEN + 19727 TCP_CHECKSUM_OFFSET; 19728 } 19729 } else { 19730 start = stuff = 0; 19731 } 19732 19733 /* 19734 * Create the header buffer, Multidata, as well as 19735 * any necessary attributes (destination address, 19736 * SAP and hardware checksum offload) that should 19737 * be associated with the Multidata message. 19738 */ 19739 ASSERT(cur_hdr_off == 0); 19740 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19741 ((md_hbuf->b_wptr += md_hbuflen), 19742 (mmd = mmd_alloc(md_hbuf, &md_mp, 19743 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19744 /* fastpath mblk */ 19745 ire->ire_nce->nce_res_mp, 19746 /* hardware checksum enabled */ 19747 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19748 /* hardware checksum offsets */ 19749 start, stuff, 0, 19750 /* hardware checksum flag */ 19751 hwcksum_flags) != 0)) { 19752 legacy_send: 19753 if (md_mp != NULL) { 19754 /* Unlink message from the chain */ 19755 if (md_mp_head != NULL) { 19756 err = (intptr_t)rmvb(md_mp_head, 19757 md_mp); 19758 /* 19759 * We can't assert that rmvb 19760 * did not return -1, since we 19761 * may get here before linkb 19762 * happens. We do, however, 19763 * check if we just removed the 19764 * only element in the list. 19765 */ 19766 if (err == 0) 19767 md_mp_head = NULL; 19768 } 19769 /* md_hbuf gets freed automatically */ 19770 TCP_STAT(tcp_mdt_discarded); 19771 freeb(md_mp); 19772 } else { 19773 /* Either allocb or mmd_alloc failed */ 19774 TCP_STAT(tcp_mdt_allocfail); 19775 if (md_hbuf != NULL) 19776 freeb(md_hbuf); 19777 } 19778 19779 /* send down what we've got so far */ 19780 if (md_mp_head != NULL) { 19781 tcp_multisend_data(tcp, ire, ill, 19782 md_mp_head, obsegs, obbytes, 19783 &rconfirm); 19784 } 19785 legacy_send_no_md: 19786 if (ire != NULL) 19787 IRE_REFRELE(ire); 19788 /* 19789 * Too bad; let the legacy path handle this. 19790 * We specify INT_MAX for the threshold, since 19791 * we gave up with the Multidata processings 19792 * and let the old path have it all. 19793 */ 19794 TCP_STAT(tcp_mdt_legacy_all); 19795 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19796 tcp_tcp_hdr_len, num_sack_blk, usable, 19797 snxt, tail_unsent, xmit_tail, local_time, 19798 INT_MAX)); 19799 } 19800 19801 /* link to any existing ones, if applicable */ 19802 TCP_STAT(tcp_mdt_allocd); 19803 if (md_mp_head == NULL) { 19804 md_mp_head = md_mp; 19805 } else if (tcp_mdt_chain) { 19806 TCP_STAT(tcp_mdt_linked); 19807 linkb(md_mp_head, md_mp); 19808 } 19809 } 19810 19811 ASSERT(md_mp_head != NULL); 19812 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19813 ASSERT(md_mp != NULL && mmd != NULL); 19814 ASSERT(md_hbuf != NULL); 19815 19816 /* 19817 * Packetize the transmittable portion of the data block; 19818 * each data block is essentially added to the Multidata 19819 * as a payload buffer. We also deal with adding more 19820 * than one payload buffers, which happens when the remaining 19821 * packetized portion of the current payload buffer is less 19822 * than MSS, while the next data block in transmit queue 19823 * has enough data to make up for one. This "spillover" 19824 * case essentially creates a split-packet, where portions 19825 * of the packet's payload fragments may span across two 19826 * virtually discontiguous address blocks. 19827 */ 19828 seg_len = mss; 19829 do { 19830 len = seg_len; 19831 19832 ASSERT(len > 0); 19833 ASSERT(max_pld >= 0); 19834 ASSERT(!add_buffer || cur_pld_off == 0); 19835 19836 /* 19837 * First time around for this payload buffer; note 19838 * in the case of a spillover, the following has 19839 * been done prior to adding the split-packet 19840 * descriptor to Multidata, and we don't want to 19841 * repeat the process. 19842 */ 19843 if (add_buffer) { 19844 ASSERT(mmd != NULL); 19845 ASSERT(md_pbuf == NULL); 19846 ASSERT(md_pbuf_nxt == NULL); 19847 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19848 19849 /* 19850 * Have we reached the limit? We'd get to 19851 * this case when we're not chaining the 19852 * Multidata messages together, and since 19853 * we're done, terminate this loop. 19854 */ 19855 if (max_pld == 0) 19856 break; /* done */ 19857 19858 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19859 TCP_STAT(tcp_mdt_allocfail); 19860 goto legacy_send; /* out_of_mem */ 19861 } 19862 19863 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19864 zc_cap != NULL) { 19865 if (!ip_md_zcopy_attr(mmd, NULL, 19866 zc_cap->ill_zerocopy_flags)) { 19867 freeb(md_pbuf); 19868 TCP_STAT(tcp_mdt_allocfail); 19869 /* out_of_mem */ 19870 goto legacy_send; 19871 } 19872 zcopy = B_TRUE; 19873 } 19874 19875 md_pbuf->b_rptr += base_pld_off; 19876 19877 /* 19878 * Add a payload buffer to the Multidata; this 19879 * operation must not fail, or otherwise our 19880 * logic in this routine is broken. There 19881 * is no memory allocation done by the 19882 * routine, so any returned failure simply 19883 * tells us that we've done something wrong. 19884 * 19885 * A failure tells us that either we're adding 19886 * the same payload buffer more than once, or 19887 * we're trying to add more buffers than 19888 * allowed (max_pld calculation is wrong). 19889 * None of the above cases should happen, and 19890 * we panic because either there's horrible 19891 * heap corruption, and/or programming mistake. 19892 */ 19893 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19894 if (pbuf_idx < 0) { 19895 cmn_err(CE_PANIC, "tcp_multisend: " 19896 "payload buffer logic error " 19897 "detected for tcp %p mmd %p " 19898 "pbuf %p (%d)\n", 19899 (void *)tcp, (void *)mmd, 19900 (void *)md_pbuf, pbuf_idx); 19901 } 19902 19903 ASSERT(max_pld > 0); 19904 --max_pld; 19905 add_buffer = B_FALSE; 19906 } 19907 19908 ASSERT(md_mp_head != NULL); 19909 ASSERT(md_pbuf != NULL); 19910 ASSERT(md_pbuf_nxt == NULL); 19911 ASSERT(pbuf_idx != -1); 19912 ASSERT(pbuf_idx_nxt == -1); 19913 ASSERT(*usable > 0); 19914 19915 /* 19916 * We spillover to the next payload buffer only 19917 * if all of the following is true: 19918 * 19919 * 1. There is not enough data on the current 19920 * payload buffer to make up `len', 19921 * 2. We are allowed to send `len', 19922 * 3. The next payload buffer length is large 19923 * enough to accomodate `spill'. 19924 */ 19925 if ((spill = len - *tail_unsent) > 0 && 19926 *usable >= len && 19927 MBLKL((*xmit_tail)->b_cont) >= spill && 19928 max_pld > 0) { 19929 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19930 if (md_pbuf_nxt == NULL) { 19931 TCP_STAT(tcp_mdt_allocfail); 19932 goto legacy_send; /* out_of_mem */ 19933 } 19934 19935 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19936 zc_cap != NULL) { 19937 if (!ip_md_zcopy_attr(mmd, NULL, 19938 zc_cap->ill_zerocopy_flags)) { 19939 freeb(md_pbuf_nxt); 19940 TCP_STAT(tcp_mdt_allocfail); 19941 /* out_of_mem */ 19942 goto legacy_send; 19943 } 19944 zcopy = B_TRUE; 19945 } 19946 19947 /* 19948 * See comments above on the first call to 19949 * mmd_addpldbuf for explanation on the panic. 19950 */ 19951 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19952 if (pbuf_idx_nxt < 0) { 19953 panic("tcp_multisend: " 19954 "next payload buffer logic error " 19955 "detected for tcp %p mmd %p " 19956 "pbuf %p (%d)\n", 19957 (void *)tcp, (void *)mmd, 19958 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19959 } 19960 19961 ASSERT(max_pld > 0); 19962 --max_pld; 19963 } else if (spill > 0) { 19964 /* 19965 * If there's a spillover, but the following 19966 * xmit_tail couldn't give us enough octets 19967 * to reach "len", then stop the current 19968 * Multidata creation and let the legacy 19969 * tcp_send() path take over. We don't want 19970 * to send the tiny segment as part of this 19971 * Multidata for performance reasons; instead, 19972 * we let the legacy path deal with grouping 19973 * it with the subsequent small mblks. 19974 */ 19975 if (*usable >= len && 19976 MBLKL((*xmit_tail)->b_cont) < spill) { 19977 max_pld = 0; 19978 break; /* done */ 19979 } 19980 19981 /* 19982 * We can't spillover, and we are near 19983 * the end of the current payload buffer, 19984 * so send what's left. 19985 */ 19986 ASSERT(*tail_unsent > 0); 19987 len = *tail_unsent; 19988 } 19989 19990 /* tail_unsent is negated if there is a spillover */ 19991 *tail_unsent -= len; 19992 *usable -= len; 19993 ASSERT(*usable >= 0); 19994 19995 if (*usable < mss) 19996 seg_len = *usable; 19997 /* 19998 * Sender SWS avoidance; see comments in tcp_send(); 19999 * everything else is the same, except that we only 20000 * do this here if there is no more data to be sent 20001 * following the current xmit_tail. We don't check 20002 * for 1-byte urgent data because we shouldn't get 20003 * here if TCP_URG_VALID is set. 20004 */ 20005 if (*usable > 0 && *usable < mss && 20006 ((md_pbuf_nxt == NULL && 20007 (*xmit_tail)->b_cont == NULL) || 20008 (md_pbuf_nxt != NULL && 20009 (*xmit_tail)->b_cont->b_cont == NULL)) && 20010 seg_len < (tcp->tcp_max_swnd >> 1) && 20011 (tcp->tcp_unsent - 20012 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20013 !tcp->tcp_zero_win_probe) { 20014 if ((*snxt + len) == tcp->tcp_snxt && 20015 (*snxt + len) == tcp->tcp_suna) { 20016 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20017 } 20018 done = B_TRUE; 20019 } 20020 20021 /* 20022 * Prime pump for IP's checksumming on our behalf; 20023 * include the adjustment for a source route if any. 20024 * Do this only for software/partial hardware checksum 20025 * offload, as this field gets zeroed out later for 20026 * the full hardware checksum offload case. 20027 */ 20028 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20029 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20030 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20031 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20032 } 20033 20034 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20035 *snxt += len; 20036 20037 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20038 /* 20039 * We set the PUSH bit only if TCP has no more buffered 20040 * data to be transmitted (or if sender SWS avoidance 20041 * takes place), as opposed to setting it for every 20042 * last packet in the burst. 20043 */ 20044 if (done || 20045 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20046 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20047 20048 /* 20049 * Set FIN bit if this is our last segment; snxt 20050 * already includes its length, and it will not 20051 * be adjusted after this point. 20052 */ 20053 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20054 *snxt == tcp->tcp_fss) { 20055 if (!tcp->tcp_fin_acked) { 20056 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20057 BUMP_MIB(&tcp_mib, tcpOutControl); 20058 } 20059 if (!tcp->tcp_fin_sent) { 20060 tcp->tcp_fin_sent = B_TRUE; 20061 /* 20062 * tcp state must be ESTABLISHED 20063 * in order for us to get here in 20064 * the first place. 20065 */ 20066 tcp->tcp_state = TCPS_FIN_WAIT_1; 20067 20068 /* 20069 * Upon returning from this routine, 20070 * tcp_wput_data() will set tcp_snxt 20071 * to be equal to snxt + tcp_fin_sent. 20072 * This is essentially the same as 20073 * setting it to tcp_fss + 1. 20074 */ 20075 } 20076 } 20077 20078 tcp->tcp_last_sent_len = (ushort_t)len; 20079 20080 len += tcp_hdr_len; 20081 if (tcp->tcp_ipversion == IPV4_VERSION) 20082 tcp->tcp_ipha->ipha_length = htons(len); 20083 else 20084 tcp->tcp_ip6h->ip6_plen = htons(len - 20085 ((char *)&tcp->tcp_ip6h[1] - 20086 tcp->tcp_iphc)); 20087 20088 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20089 20090 /* setup header fragment */ 20091 PDESC_HDR_ADD(pkt_info, 20092 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20093 tcp->tcp_mdt_hdr_head, /* head room */ 20094 tcp_hdr_len, /* len */ 20095 tcp->tcp_mdt_hdr_tail); /* tail room */ 20096 20097 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20098 hdr_frag_sz); 20099 ASSERT(MBLKIN(md_hbuf, 20100 (pkt_info->hdr_base - md_hbuf->b_rptr), 20101 PDESC_HDRSIZE(pkt_info))); 20102 20103 /* setup first payload fragment */ 20104 PDESC_PLD_INIT(pkt_info); 20105 PDESC_PLD_SPAN_ADD(pkt_info, 20106 pbuf_idx, /* index */ 20107 md_pbuf->b_rptr + cur_pld_off, /* start */ 20108 tcp->tcp_last_sent_len); /* len */ 20109 20110 /* create a split-packet in case of a spillover */ 20111 if (md_pbuf_nxt != NULL) { 20112 ASSERT(spill > 0); 20113 ASSERT(pbuf_idx_nxt > pbuf_idx); 20114 ASSERT(!add_buffer); 20115 20116 md_pbuf = md_pbuf_nxt; 20117 md_pbuf_nxt = NULL; 20118 pbuf_idx = pbuf_idx_nxt; 20119 pbuf_idx_nxt = -1; 20120 cur_pld_off = spill; 20121 20122 /* trim out first payload fragment */ 20123 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20124 20125 /* setup second payload fragment */ 20126 PDESC_PLD_SPAN_ADD(pkt_info, 20127 pbuf_idx, /* index */ 20128 md_pbuf->b_rptr, /* start */ 20129 spill); /* len */ 20130 20131 if ((*xmit_tail)->b_next == NULL) { 20132 /* 20133 * Store the lbolt used for RTT 20134 * estimation. We can only record one 20135 * timestamp per mblk so we do it when 20136 * we reach the end of the payload 20137 * buffer. Also we only take a new 20138 * timestamp sample when the previous 20139 * timed data from the same mblk has 20140 * been ack'ed. 20141 */ 20142 (*xmit_tail)->b_prev = local_time; 20143 (*xmit_tail)->b_next = 20144 (mblk_t *)(uintptr_t)first_snxt; 20145 } 20146 20147 first_snxt = *snxt - spill; 20148 20149 /* 20150 * Advance xmit_tail; usable could be 0 by 20151 * the time we got here, but we made sure 20152 * above that we would only spillover to 20153 * the next data block if usable includes 20154 * the spilled-over amount prior to the 20155 * subtraction. Therefore, we are sure 20156 * that xmit_tail->b_cont can't be NULL. 20157 */ 20158 ASSERT((*xmit_tail)->b_cont != NULL); 20159 *xmit_tail = (*xmit_tail)->b_cont; 20160 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20161 (uintptr_t)INT_MAX); 20162 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20163 } else { 20164 cur_pld_off += tcp->tcp_last_sent_len; 20165 } 20166 20167 /* 20168 * Fill in the header using the template header, and 20169 * add options such as time-stamp, ECN and/or SACK, 20170 * as needed. 20171 */ 20172 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20173 (clock_t)local_time, num_sack_blk); 20174 20175 /* take care of some IP header businesses */ 20176 if (af == AF_INET) { 20177 ipha = (ipha_t *)pkt_info->hdr_rptr; 20178 20179 ASSERT(OK_32PTR((uchar_t *)ipha)); 20180 ASSERT(PDESC_HDRL(pkt_info) >= 20181 IP_SIMPLE_HDR_LENGTH); 20182 ASSERT(ipha->ipha_version_and_hdr_length == 20183 IP_SIMPLE_HDR_VERSION); 20184 20185 /* 20186 * Assign ident value for current packet; see 20187 * related comments in ip_wput_ire() about the 20188 * contract private interface with clustering 20189 * group. 20190 */ 20191 clusterwide = B_FALSE; 20192 if (cl_inet_ipident != NULL) { 20193 ASSERT(cl_inet_isclusterwide != NULL); 20194 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20195 AF_INET, 20196 (uint8_t *)(uintptr_t)src)) { 20197 ipha->ipha_ident = 20198 (*cl_inet_ipident) 20199 (IPPROTO_IP, AF_INET, 20200 (uint8_t *)(uintptr_t)src, 20201 (uint8_t *)(uintptr_t)dst); 20202 clusterwide = B_TRUE; 20203 } 20204 } 20205 20206 if (!clusterwide) { 20207 ipha->ipha_ident = (uint16_t) 20208 atomic_add_32_nv( 20209 &ire->ire_ident, 1); 20210 } 20211 #ifndef _BIG_ENDIAN 20212 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20213 (ipha->ipha_ident >> 8); 20214 #endif 20215 } else { 20216 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20217 20218 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20219 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20220 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20221 ASSERT(PDESC_HDRL(pkt_info) >= 20222 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20223 TCP_CHECKSUM_SIZE)); 20224 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20225 20226 if (tcp->tcp_ip_forward_progress) { 20227 rconfirm = B_TRUE; 20228 tcp->tcp_ip_forward_progress = B_FALSE; 20229 } 20230 } 20231 20232 /* at least one payload span, and at most two */ 20233 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20234 20235 /* add the packet descriptor to Multidata */ 20236 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20237 KM_NOSLEEP)) == NULL) { 20238 /* 20239 * Any failure other than ENOMEM indicates 20240 * that we have passed in invalid pkt_info 20241 * or parameters to mmd_addpdesc, which must 20242 * not happen. 20243 * 20244 * EINVAL is a result of failure on boundary 20245 * checks against the pkt_info contents. It 20246 * should not happen, and we panic because 20247 * either there's horrible heap corruption, 20248 * and/or programming mistake. 20249 */ 20250 if (err != ENOMEM) { 20251 cmn_err(CE_PANIC, "tcp_multisend: " 20252 "pdesc logic error detected for " 20253 "tcp %p mmd %p pinfo %p (%d)\n", 20254 (void *)tcp, (void *)mmd, 20255 (void *)pkt_info, err); 20256 } 20257 TCP_STAT(tcp_mdt_addpdescfail); 20258 goto legacy_send; /* out_of_mem */ 20259 } 20260 ASSERT(pkt != NULL); 20261 20262 /* calculate IP header and TCP checksums */ 20263 if (af == AF_INET) { 20264 /* calculate pseudo-header checksum */ 20265 cksum = (dst >> 16) + (dst & 0xFFFF) + 20266 (src >> 16) + (src & 0xFFFF); 20267 20268 /* offset for TCP header checksum */ 20269 up = IPH_TCPH_CHECKSUMP(ipha, 20270 IP_SIMPLE_HDR_LENGTH); 20271 } else { 20272 up = (uint16_t *)&ip6h->ip6_src; 20273 20274 /* calculate pseudo-header checksum */ 20275 cksum = up[0] + up[1] + up[2] + up[3] + 20276 up[4] + up[5] + up[6] + up[7] + 20277 up[8] + up[9] + up[10] + up[11] + 20278 up[12] + up[13] + up[14] + up[15]; 20279 20280 /* Fold the initial sum */ 20281 cksum = (cksum & 0xffff) + (cksum >> 16); 20282 20283 up = (uint16_t *)(((uchar_t *)ip6h) + 20284 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20285 } 20286 20287 if (hwcksum_flags & HCK_FULLCKSUM) { 20288 /* clear checksum field for hardware */ 20289 *up = 0; 20290 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20291 uint32_t sum; 20292 20293 /* pseudo-header checksumming */ 20294 sum = *up + cksum + IP_TCP_CSUM_COMP; 20295 sum = (sum & 0xFFFF) + (sum >> 16); 20296 *up = (sum & 0xFFFF) + (sum >> 16); 20297 } else { 20298 /* software checksumming */ 20299 TCP_STAT(tcp_out_sw_cksum); 20300 TCP_STAT_UPDATE(tcp_out_sw_cksum_bytes, 20301 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20302 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20303 cksum + IP_TCP_CSUM_COMP); 20304 if (*up == 0) 20305 *up = 0xFFFF; 20306 } 20307 20308 /* IPv4 header checksum */ 20309 if (af == AF_INET) { 20310 ipha->ipha_fragment_offset_and_flags |= 20311 (uint32_t)htons(ire->ire_frag_flag); 20312 20313 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20314 ipha->ipha_hdr_checksum = 0; 20315 } else { 20316 IP_HDR_CKSUM(ipha, cksum, 20317 ((uint32_t *)ipha)[0], 20318 ((uint16_t *)ipha)[4]); 20319 } 20320 } 20321 20322 if (af == AF_INET && HOOKS4_INTERESTED_PHYSICAL_OUT|| 20323 af == AF_INET6 && HOOKS6_INTERESTED_PHYSICAL_OUT) { 20324 /* build header(IP/TCP) mblk for this segment */ 20325 if ((mp = dupb(md_hbuf)) == NULL) 20326 goto legacy_send; 20327 20328 mp->b_rptr = pkt_info->hdr_rptr; 20329 mp->b_wptr = pkt_info->hdr_wptr; 20330 20331 /* build payload mblk for this segment */ 20332 if ((mp1 = dupb(*xmit_tail)) == NULL) { 20333 freemsg(mp); 20334 goto legacy_send; 20335 } 20336 mp1->b_wptr = md_pbuf->b_rptr + cur_pld_off; 20337 mp1->b_rptr = mp1->b_wptr - 20338 tcp->tcp_last_sent_len; 20339 linkb(mp, mp1); 20340 20341 pld_start = mp1->b_rptr; 20342 20343 if (af == AF_INET) { 20344 DTRACE_PROBE4( 20345 ip4__physical__out__start, 20346 ill_t *, NULL, 20347 ill_t *, ill, 20348 ipha_t *, ipha, 20349 mblk_t *, mp); 20350 FW_HOOKS(ip4_physical_out_event, 20351 ipv4firewall_physical_out, 20352 NULL, ill, ipha, mp, mp); 20353 DTRACE_PROBE1( 20354 ip4__physical__out__end, 20355 mblk_t *, mp); 20356 } else { 20357 DTRACE_PROBE4( 20358 ip6__physical__out_start, 20359 ill_t *, NULL, 20360 ill_t *, ill, 20361 ip6_t *, ip6h, 20362 mblk_t *, mp); 20363 FW_HOOKS6(ip6_physical_out_event, 20364 ipv6firewall_physical_out, 20365 NULL, ill, ip6h, mp, mp); 20366 DTRACE_PROBE1( 20367 ip6__physical__out__end, 20368 mblk_t *, mp); 20369 } 20370 20371 if (buf_trunked && mp != NULL) { 20372 /* 20373 * Need to pass it to normal path. 20374 */ 20375 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20376 } else if (mp == NULL || 20377 mp->b_rptr != pkt_info->hdr_rptr || 20378 mp->b_wptr != pkt_info->hdr_wptr || 20379 (mp1 = mp->b_cont) == NULL || 20380 mp1->b_rptr != pld_start || 20381 mp1->b_wptr != pld_start + 20382 tcp->tcp_last_sent_len || 20383 mp1->b_cont != NULL) { 20384 /* 20385 * Need to pass all packets of this 20386 * buffer to normal path, either when 20387 * packet is blocked, or when boundary 20388 * of header buffer or payload buffer 20389 * has been changed by FW_HOOKS[6]. 20390 */ 20391 buf_trunked = B_TRUE; 20392 if (md_mp_head != NULL) { 20393 err = (intptr_t)rmvb(md_mp_head, 20394 md_mp); 20395 if (err == 0) 20396 md_mp_head = NULL; 20397 } 20398 20399 /* send down what we've got so far */ 20400 if (md_mp_head != NULL) { 20401 tcp_multisend_data(tcp, ire, 20402 ill, md_mp_head, obsegs, 20403 obbytes, &rconfirm); 20404 } 20405 md_mp_head = NULL; 20406 20407 if (mp != NULL) 20408 CALL_IP_WPUT(tcp->tcp_connp, 20409 q, mp); 20410 20411 mp1 = fw_mp_head; 20412 do { 20413 mp = mp1; 20414 mp1 = mp1->b_next; 20415 mp->b_next = NULL; 20416 mp->b_prev = NULL; 20417 CALL_IP_WPUT(tcp->tcp_connp, 20418 q, mp); 20419 } while (mp1 != NULL); 20420 20421 fw_mp_head = NULL; 20422 } else { 20423 if (fw_mp_head == NULL) 20424 fw_mp_head = mp; 20425 else 20426 fw_mp_head->b_prev->b_next = mp; 20427 fw_mp_head->b_prev = mp; 20428 } 20429 } 20430 20431 /* advance header offset */ 20432 cur_hdr_off += hdr_frag_sz; 20433 20434 obbytes += tcp->tcp_last_sent_len; 20435 ++obsegs; 20436 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20437 *tail_unsent > 0); 20438 20439 if ((*xmit_tail)->b_next == NULL) { 20440 /* 20441 * Store the lbolt used for RTT estimation. We can only 20442 * record one timestamp per mblk so we do it when we 20443 * reach the end of the payload buffer. Also we only 20444 * take a new timestamp sample when the previous timed 20445 * data from the same mblk has been ack'ed. 20446 */ 20447 (*xmit_tail)->b_prev = local_time; 20448 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20449 } 20450 20451 ASSERT(*tail_unsent >= 0); 20452 if (*tail_unsent > 0) { 20453 /* 20454 * We got here because we broke out of the above 20455 * loop due to of one of the following cases: 20456 * 20457 * 1. len < adjusted MSS (i.e. small), 20458 * 2. Sender SWS avoidance, 20459 * 3. max_pld is zero. 20460 * 20461 * We are done for this Multidata, so trim our 20462 * last payload buffer (if any) accordingly. 20463 */ 20464 if (md_pbuf != NULL) 20465 md_pbuf->b_wptr -= *tail_unsent; 20466 } else if (*usable > 0) { 20467 *xmit_tail = (*xmit_tail)->b_cont; 20468 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20469 (uintptr_t)INT_MAX); 20470 *tail_unsent = (int)MBLKL(*xmit_tail); 20471 add_buffer = B_TRUE; 20472 } 20473 20474 while (fw_mp_head) { 20475 mp = fw_mp_head; 20476 fw_mp_head = fw_mp_head->b_next; 20477 mp->b_prev = mp->b_next = NULL; 20478 freemsg(mp); 20479 } 20480 if (buf_trunked) { 20481 TCP_STAT(tcp_mdt_discarded); 20482 freeb(md_mp); 20483 buf_trunked = B_FALSE; 20484 } 20485 } while (!done && *usable > 0 && num_burst_seg > 0 && 20486 (tcp_mdt_chain || max_pld > 0)); 20487 20488 if (md_mp_head != NULL) { 20489 /* send everything down */ 20490 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20491 &rconfirm); 20492 } 20493 20494 #undef PREP_NEW_MULTIDATA 20495 #undef PREP_NEW_PBUF 20496 #undef IPVER 20497 20498 IRE_REFRELE(ire); 20499 return (0); 20500 } 20501 20502 /* 20503 * A wrapper function for sending one or more Multidata messages down to 20504 * the module below ip; this routine does not release the reference of the 20505 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20506 */ 20507 static void 20508 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20509 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20510 { 20511 uint64_t delta; 20512 nce_t *nce; 20513 20514 ASSERT(ire != NULL && ill != NULL); 20515 ASSERT(ire->ire_stq != NULL); 20516 ASSERT(md_mp_head != NULL); 20517 ASSERT(rconfirm != NULL); 20518 20519 /* adjust MIBs and IRE timestamp */ 20520 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 20521 tcp->tcp_obsegs += obsegs; 20522 UPDATE_MIB(&tcp_mib, tcpOutDataSegs, obsegs); 20523 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, obbytes); 20524 TCP_STAT_UPDATE(tcp_mdt_pkt_out, obsegs); 20525 20526 if (tcp->tcp_ipversion == IPV4_VERSION) { 20527 TCP_STAT_UPDATE(tcp_mdt_pkt_out_v4, obsegs); 20528 UPDATE_MIB(&ip_mib, ipOutRequests, obsegs); 20529 } else { 20530 TCP_STAT_UPDATE(tcp_mdt_pkt_out_v6, obsegs); 20531 UPDATE_MIB(&ip6_mib, ipv6OutRequests, obsegs); 20532 } 20533 20534 ire->ire_ob_pkt_count += obsegs; 20535 if (ire->ire_ipif != NULL) 20536 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20537 ire->ire_last_used_time = lbolt; 20538 20539 /* send it down */ 20540 putnext(ire->ire_stq, md_mp_head); 20541 20542 /* we're done for TCP/IPv4 */ 20543 if (tcp->tcp_ipversion == IPV4_VERSION) 20544 return; 20545 20546 nce = ire->ire_nce; 20547 20548 ASSERT(nce != NULL); 20549 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20550 ASSERT(nce->nce_state != ND_INCOMPLETE); 20551 20552 /* reachability confirmation? */ 20553 if (*rconfirm) { 20554 nce->nce_last = TICK_TO_MSEC(lbolt64); 20555 if (nce->nce_state != ND_REACHABLE) { 20556 mutex_enter(&nce->nce_lock); 20557 nce->nce_state = ND_REACHABLE; 20558 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20559 mutex_exit(&nce->nce_lock); 20560 (void) untimeout(nce->nce_timeout_id); 20561 if (ip_debug > 2) { 20562 /* ip1dbg */ 20563 pr_addr_dbg("tcp_multisend_data: state " 20564 "for %s changed to REACHABLE\n", 20565 AF_INET6, &ire->ire_addr_v6); 20566 } 20567 } 20568 /* reset transport reachability confirmation */ 20569 *rconfirm = B_FALSE; 20570 } 20571 20572 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20573 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20574 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20575 20576 if (delta > (uint64_t)ill->ill_reachable_time) { 20577 mutex_enter(&nce->nce_lock); 20578 switch (nce->nce_state) { 20579 case ND_REACHABLE: 20580 case ND_STALE: 20581 /* 20582 * ND_REACHABLE is identical to ND_STALE in this 20583 * specific case. If reachable time has expired for 20584 * this neighbor (delta is greater than reachable 20585 * time), conceptually, the neighbor cache is no 20586 * longer in REACHABLE state, but already in STALE 20587 * state. So the correct transition here is to 20588 * ND_DELAY. 20589 */ 20590 nce->nce_state = ND_DELAY; 20591 mutex_exit(&nce->nce_lock); 20592 NDP_RESTART_TIMER(nce, delay_first_probe_time); 20593 if (ip_debug > 3) { 20594 /* ip2dbg */ 20595 pr_addr_dbg("tcp_multisend_data: state " 20596 "for %s changed to DELAY\n", 20597 AF_INET6, &ire->ire_addr_v6); 20598 } 20599 break; 20600 case ND_DELAY: 20601 case ND_PROBE: 20602 mutex_exit(&nce->nce_lock); 20603 /* Timers have already started */ 20604 break; 20605 case ND_UNREACHABLE: 20606 /* 20607 * ndp timer has detected that this nce is 20608 * unreachable and initiated deleting this nce 20609 * and all its associated IREs. This is a race 20610 * where we found the ire before it was deleted 20611 * and have just sent out a packet using this 20612 * unreachable nce. 20613 */ 20614 mutex_exit(&nce->nce_lock); 20615 break; 20616 default: 20617 ASSERT(0); 20618 } 20619 } 20620 } 20621 20622 /* 20623 * Derived from tcp_send_data(). 20624 */ 20625 static void 20626 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20627 int num_lso_seg) 20628 { 20629 ipha_t *ipha; 20630 mblk_t *ire_fp_mp; 20631 uint_t ire_fp_mp_len; 20632 uint32_t hcksum_txflags = 0; 20633 ipaddr_t src; 20634 ipaddr_t dst; 20635 uint32_t cksum; 20636 uint16_t *up; 20637 20638 ASSERT(DB_TYPE(mp) == M_DATA); 20639 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20640 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20641 ASSERT(tcp->tcp_connp != NULL); 20642 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20643 20644 ipha = (ipha_t *)mp->b_rptr; 20645 src = ipha->ipha_src; 20646 dst = ipha->ipha_dst; 20647 20648 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20649 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20650 num_lso_seg); 20651 #ifndef _BIG_ENDIAN 20652 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20653 #endif 20654 if (tcp->tcp_snd_zcopy_aware) { 20655 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20656 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20657 mp = tcp_zcopy_disable(tcp, mp); 20658 } 20659 20660 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20661 ASSERT(ill->ill_hcksum_capab != NULL); 20662 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20663 } 20664 20665 /* 20666 * Since the TCP checksum should be recalculated by h/w, we can just 20667 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20668 * pseudo-header checksum for HCK_PARTIALCKSUM. 20669 * The partial pseudo-header excludes TCP length, that was calculated 20670 * in tcp_send(), so to zero *up before further processing. 20671 */ 20672 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20673 20674 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20675 *up = 0; 20676 20677 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20678 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20679 20680 /* 20681 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 20682 */ 20683 DB_LSOFLAGS(mp) |= HW_LSO; 20684 DB_LSOMSS(mp) = mss; 20685 20686 ipha->ipha_fragment_offset_and_flags |= 20687 (uint32_t)htons(ire->ire_frag_flag); 20688 20689 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20690 ire_fp_mp_len = MBLKL(ire_fp_mp); 20691 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20692 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20693 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20694 20695 UPDATE_OB_PKT_COUNT(ire); 20696 ire->ire_last_used_time = lbolt; 20697 BUMP_MIB(&ip_mib, ipOutRequests); 20698 20699 if (ILL_DLS_CAPABLE(ill)) { 20700 /* 20701 * Send the packet directly to DLD, where it may be queued 20702 * depending on the availability of transmit resources at 20703 * the media layer. 20704 */ 20705 IP_DLS_ILL_TX(ill, ipha, mp); 20706 } else { 20707 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 20708 DTRACE_PROBE4(ip4__physical__out__start, 20709 ill_t *, NULL, ill_t *, out_ill, 20710 ipha_t *, ipha, mblk_t *, mp); 20711 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 20712 NULL, out_ill, ipha, mp, mp); 20713 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20714 if (mp != NULL) 20715 putnext(ire->ire_stq, mp); 20716 } 20717 } 20718 20719 /* 20720 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20721 * scheme, and returns one of the following: 20722 * 20723 * -1 = failed allocation. 20724 * 0 = success; burst count reached, or usable send window is too small, 20725 * and that we'd rather wait until later before sending again. 20726 * 1 = success; we are called from tcp_multisend(), and both usable send 20727 * window and tail_unsent are greater than the MDT threshold, and thus 20728 * Multidata Transmit should be used instead. 20729 */ 20730 static int 20731 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20732 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20733 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20734 const int mdt_thres) 20735 { 20736 int num_burst_seg = tcp->tcp_snd_burst; 20737 ire_t *ire = NULL; 20738 ill_t *ill = NULL; 20739 mblk_t *ire_fp_mp = NULL; 20740 uint_t ire_fp_mp_len = 0; 20741 int num_lso_seg = 1; 20742 uint_t lso_usable; 20743 boolean_t do_lso_send = B_FALSE; 20744 20745 /* 20746 * Check LSO capability before any further work. And the similar check 20747 * need to be done in for(;;) loop. 20748 * LSO will be deployed when therer is more than one mss of available 20749 * data and a burst transmission is allowed. 20750 */ 20751 if (tcp->tcp_lso && 20752 (tcp->tcp_valid_bits == 0 || 20753 tcp->tcp_valid_bits == TCP_FSS_VALID) && 20754 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20755 /* 20756 * Try to find usable IRE/ILL and do basic check to the ILL. 20757 */ 20758 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 20759 /* 20760 * Enable LSO with this transmission. 20761 * Since IRE has been hold in 20762 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 20763 * should be called before return. 20764 */ 20765 do_lso_send = B_TRUE; 20766 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20767 ire_fp_mp_len = MBLKL(ire_fp_mp); 20768 /* Round up to multiple of 4 */ 20769 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 20770 } else { 20771 do_lso_send = B_FALSE; 20772 ill = NULL; 20773 } 20774 } 20775 20776 for (;;) { 20777 struct datab *db; 20778 tcph_t *tcph; 20779 uint32_t sum; 20780 mblk_t *mp, *mp1; 20781 uchar_t *rptr; 20782 int len; 20783 20784 /* 20785 * If we're called by tcp_multisend(), and the amount of 20786 * sendable data as well as the size of current xmit_tail 20787 * is beyond the MDT threshold, return to the caller and 20788 * let the large data transmit be done using MDT. 20789 */ 20790 if (*usable > 0 && *usable > mdt_thres && 20791 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 20792 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 20793 ASSERT(tcp->tcp_mdt); 20794 return (1); /* success; do large send */ 20795 } 20796 20797 if (num_burst_seg == 0) 20798 break; /* success; burst count reached */ 20799 20800 /* 20801 * Calculate the maximum payload length we can send in *one* 20802 * time. 20803 */ 20804 if (do_lso_send) { 20805 /* 20806 * Check whether need to do LSO any more. 20807 */ 20808 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20809 lso_usable = MIN(tcp->tcp_lso_max, *usable); 20810 lso_usable = MIN(lso_usable, 20811 num_burst_seg * mss); 20812 20813 num_lso_seg = lso_usable / mss; 20814 if (lso_usable % mss) { 20815 num_lso_seg++; 20816 tcp->tcp_last_sent_len = (ushort_t) 20817 (lso_usable % mss); 20818 } else { 20819 tcp->tcp_last_sent_len = (ushort_t)mss; 20820 } 20821 } else { 20822 do_lso_send = B_FALSE; 20823 num_lso_seg = 1; 20824 lso_usable = mss; 20825 } 20826 } 20827 20828 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 20829 20830 /* 20831 * Adjust num_burst_seg here. 20832 */ 20833 num_burst_seg -= num_lso_seg; 20834 20835 len = mss; 20836 if (len > *usable) { 20837 ASSERT(do_lso_send == B_FALSE); 20838 20839 len = *usable; 20840 if (len <= 0) { 20841 /* Terminate the loop */ 20842 break; /* success; too small */ 20843 } 20844 /* 20845 * Sender silly-window avoidance. 20846 * Ignore this if we are going to send a 20847 * zero window probe out. 20848 * 20849 * TODO: force data into microscopic window? 20850 * ==> (!pushed || (unsent > usable)) 20851 */ 20852 if (len < (tcp->tcp_max_swnd >> 1) && 20853 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 20854 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 20855 len == 1) && (! tcp->tcp_zero_win_probe)) { 20856 /* 20857 * If the retransmit timer is not running 20858 * we start it so that we will retransmit 20859 * in the case when the the receiver has 20860 * decremented the window. 20861 */ 20862 if (*snxt == tcp->tcp_snxt && 20863 *snxt == tcp->tcp_suna) { 20864 /* 20865 * We are not supposed to send 20866 * anything. So let's wait a little 20867 * bit longer before breaking SWS 20868 * avoidance. 20869 * 20870 * What should the value be? 20871 * Suggestion: MAX(init rexmit time, 20872 * tcp->tcp_rto) 20873 */ 20874 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20875 } 20876 break; /* success; too small */ 20877 } 20878 } 20879 20880 tcph = tcp->tcp_tcph; 20881 20882 /* 20883 * The reason to adjust len here is that we need to set flags 20884 * and calculate checksum. 20885 */ 20886 if (do_lso_send) 20887 len = lso_usable; 20888 20889 *usable -= len; /* Approximate - can be adjusted later */ 20890 if (*usable > 0) 20891 tcph->th_flags[0] = TH_ACK; 20892 else 20893 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 20894 20895 /* 20896 * Prime pump for IP's checksumming on our behalf 20897 * Include the adjustment for a source route if any. 20898 */ 20899 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20900 sum = (sum >> 16) + (sum & 0xFFFF); 20901 U16_TO_ABE16(sum, tcph->th_sum); 20902 20903 U32_TO_ABE32(*snxt, tcph->th_seq); 20904 20905 /* 20906 * Branch off to tcp_xmit_mp() if any of the VALID bits is 20907 * set. For the case when TCP_FSS_VALID is the only valid 20908 * bit (normal active close), branch off only when we think 20909 * that the FIN flag needs to be set. Note for this case, 20910 * that (snxt + len) may not reflect the actual seg_len, 20911 * as len may be further reduced in tcp_xmit_mp(). If len 20912 * gets modified, we will end up here again. 20913 */ 20914 if (tcp->tcp_valid_bits != 0 && 20915 (tcp->tcp_valid_bits != TCP_FSS_VALID || 20916 ((*snxt + len) == tcp->tcp_fss))) { 20917 uchar_t *prev_rptr; 20918 uint32_t prev_snxt = tcp->tcp_snxt; 20919 20920 if (*tail_unsent == 0) { 20921 ASSERT((*xmit_tail)->b_cont != NULL); 20922 *xmit_tail = (*xmit_tail)->b_cont; 20923 prev_rptr = (*xmit_tail)->b_rptr; 20924 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20925 (*xmit_tail)->b_rptr); 20926 } else { 20927 prev_rptr = (*xmit_tail)->b_rptr; 20928 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 20929 *tail_unsent; 20930 } 20931 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 20932 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20933 /* Restore tcp_snxt so we get amount sent right. */ 20934 tcp->tcp_snxt = prev_snxt; 20935 if (prev_rptr == (*xmit_tail)->b_rptr) { 20936 /* 20937 * If the previous timestamp is still in use, 20938 * don't stomp on it. 20939 */ 20940 if ((*xmit_tail)->b_next == NULL) { 20941 (*xmit_tail)->b_prev = local_time; 20942 (*xmit_tail)->b_next = 20943 (mblk_t *)(uintptr_t)(*snxt); 20944 } 20945 } else 20946 (*xmit_tail)->b_rptr = prev_rptr; 20947 20948 if (mp == NULL) { 20949 if (ire != NULL) 20950 IRE_REFRELE(ire); 20951 return (-1); 20952 } 20953 mp1 = mp->b_cont; 20954 20955 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20956 tcp->tcp_last_sent_len = (ushort_t)len; 20957 while (mp1->b_cont) { 20958 *xmit_tail = (*xmit_tail)->b_cont; 20959 (*xmit_tail)->b_prev = local_time; 20960 (*xmit_tail)->b_next = 20961 (mblk_t *)(uintptr_t)(*snxt); 20962 mp1 = mp1->b_cont; 20963 } 20964 *snxt += len; 20965 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20966 BUMP_LOCAL(tcp->tcp_obsegs); 20967 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 20968 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 20969 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 20970 tcp_send_data(tcp, q, mp); 20971 continue; 20972 } 20973 20974 *snxt += len; /* Adjust later if we don't send all of len */ 20975 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 20976 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 20977 20978 if (*tail_unsent) { 20979 /* Are the bytes above us in flight? */ 20980 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20981 if (rptr != (*xmit_tail)->b_rptr) { 20982 *tail_unsent -= len; 20983 if (len <= mss) /* LSO is unusable */ 20984 tcp->tcp_last_sent_len = (ushort_t)len; 20985 len += tcp_hdr_len; 20986 if (tcp->tcp_ipversion == IPV4_VERSION) 20987 tcp->tcp_ipha->ipha_length = htons(len); 20988 else 20989 tcp->tcp_ip6h->ip6_plen = 20990 htons(len - 20991 ((char *)&tcp->tcp_ip6h[1] - 20992 tcp->tcp_iphc)); 20993 mp = dupb(*xmit_tail); 20994 if (mp == NULL) { 20995 if (ire != NULL) 20996 IRE_REFRELE(ire); 20997 return (-1); /* out_of_mem */ 20998 } 20999 mp->b_rptr = rptr; 21000 /* 21001 * If the old timestamp is no longer in use, 21002 * sample a new timestamp now. 21003 */ 21004 if ((*xmit_tail)->b_next == NULL) { 21005 (*xmit_tail)->b_prev = local_time; 21006 (*xmit_tail)->b_next = 21007 (mblk_t *)(uintptr_t)(*snxt-len); 21008 } 21009 goto must_alloc; 21010 } 21011 } else { 21012 *xmit_tail = (*xmit_tail)->b_cont; 21013 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21014 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21015 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21016 (*xmit_tail)->b_rptr); 21017 } 21018 21019 (*xmit_tail)->b_prev = local_time; 21020 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21021 21022 *tail_unsent -= len; 21023 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21024 tcp->tcp_last_sent_len = (ushort_t)len; 21025 21026 len += tcp_hdr_len; 21027 if (tcp->tcp_ipversion == IPV4_VERSION) 21028 tcp->tcp_ipha->ipha_length = htons(len); 21029 else 21030 tcp->tcp_ip6h->ip6_plen = htons(len - 21031 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21032 21033 mp = dupb(*xmit_tail); 21034 if (mp == NULL) { 21035 if (ire != NULL) 21036 IRE_REFRELE(ire); 21037 return (-1); /* out_of_mem */ 21038 } 21039 21040 len = tcp_hdr_len; 21041 /* 21042 * There are four reasons to allocate a new hdr mblk: 21043 * 1) The bytes above us are in use by another packet 21044 * 2) We don't have good alignment 21045 * 3) The mblk is being shared 21046 * 4) We don't have enough room for a header 21047 */ 21048 rptr = mp->b_rptr - len; 21049 if (!OK_32PTR(rptr) || 21050 ((db = mp->b_datap), db->db_ref != 2) || 21051 rptr < db->db_base + ire_fp_mp_len) { 21052 /* NOTE: we assume allocb returns an OK_32PTR */ 21053 21054 must_alloc:; 21055 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21056 tcp_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21057 if (mp1 == NULL) { 21058 freemsg(mp); 21059 if (ire != NULL) 21060 IRE_REFRELE(ire); 21061 return (-1); /* out_of_mem */ 21062 } 21063 mp1->b_cont = mp; 21064 mp = mp1; 21065 /* Leave room for Link Level header */ 21066 len = tcp_hdr_len; 21067 rptr = &mp->b_rptr[tcp_wroff_xtra + ire_fp_mp_len]; 21068 mp->b_wptr = &rptr[len]; 21069 } 21070 21071 /* 21072 * Fill in the header using the template header, and add 21073 * options such as time-stamp, ECN and/or SACK, as needed. 21074 */ 21075 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21076 21077 mp->b_rptr = rptr; 21078 21079 if (*tail_unsent) { 21080 int spill = *tail_unsent; 21081 21082 mp1 = mp->b_cont; 21083 if (mp1 == NULL) 21084 mp1 = mp; 21085 21086 /* 21087 * If we're a little short, tack on more mblks until 21088 * there is no more spillover. 21089 */ 21090 while (spill < 0) { 21091 mblk_t *nmp; 21092 int nmpsz; 21093 21094 nmp = (*xmit_tail)->b_cont; 21095 nmpsz = MBLKL(nmp); 21096 21097 /* 21098 * Excess data in mblk; can we split it? 21099 * If MDT is enabled for the connection, 21100 * keep on splitting as this is a transient 21101 * send path. 21102 */ 21103 if (!do_lso_send && !tcp->tcp_mdt && 21104 (spill + nmpsz > 0)) { 21105 /* 21106 * Don't split if stream head was 21107 * told to break up larger writes 21108 * into smaller ones. 21109 */ 21110 if (tcp->tcp_maxpsz > 0) 21111 break; 21112 21113 /* 21114 * Next mblk is less than SMSS/2 21115 * rounded up to nearest 64-byte; 21116 * let it get sent as part of the 21117 * next segment. 21118 */ 21119 if (tcp->tcp_localnet && 21120 !tcp->tcp_cork && 21121 (nmpsz < roundup((mss >> 1), 64))) 21122 break; 21123 } 21124 21125 *xmit_tail = nmp; 21126 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21127 /* Stash for rtt use later */ 21128 (*xmit_tail)->b_prev = local_time; 21129 (*xmit_tail)->b_next = 21130 (mblk_t *)(uintptr_t)(*snxt - len); 21131 mp1->b_cont = dupb(*xmit_tail); 21132 mp1 = mp1->b_cont; 21133 21134 spill += nmpsz; 21135 if (mp1 == NULL) { 21136 *tail_unsent = spill; 21137 freemsg(mp); 21138 if (ire != NULL) 21139 IRE_REFRELE(ire); 21140 return (-1); /* out_of_mem */ 21141 } 21142 } 21143 21144 /* Trim back any surplus on the last mblk */ 21145 if (spill >= 0) { 21146 mp1->b_wptr -= spill; 21147 *tail_unsent = spill; 21148 } else { 21149 /* 21150 * We did not send everything we could in 21151 * order to remain within the b_cont limit. 21152 */ 21153 *usable -= spill; 21154 *snxt += spill; 21155 tcp->tcp_last_sent_len += spill; 21156 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, spill); 21157 /* 21158 * Adjust the checksum 21159 */ 21160 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21161 sum += spill; 21162 sum = (sum >> 16) + (sum & 0xFFFF); 21163 U16_TO_ABE16(sum, tcph->th_sum); 21164 if (tcp->tcp_ipversion == IPV4_VERSION) { 21165 sum = ntohs( 21166 ((ipha_t *)rptr)->ipha_length) + 21167 spill; 21168 ((ipha_t *)rptr)->ipha_length = 21169 htons(sum); 21170 } else { 21171 sum = ntohs( 21172 ((ip6_t *)rptr)->ip6_plen) + 21173 spill; 21174 ((ip6_t *)rptr)->ip6_plen = 21175 htons(sum); 21176 } 21177 *tail_unsent = 0; 21178 } 21179 } 21180 if (tcp->tcp_ip_forward_progress) { 21181 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21182 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21183 tcp->tcp_ip_forward_progress = B_FALSE; 21184 } 21185 21186 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21187 if (do_lso_send) { 21188 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21189 num_lso_seg); 21190 tcp->tcp_obsegs += num_lso_seg; 21191 21192 TCP_STAT(tcp_lso_times); 21193 TCP_STAT_UPDATE(tcp_lso_pkt_out, num_lso_seg); 21194 } else { 21195 tcp_send_data(tcp, q, mp); 21196 BUMP_LOCAL(tcp->tcp_obsegs); 21197 } 21198 } 21199 21200 if (ire != NULL) 21201 IRE_REFRELE(ire); 21202 return (0); 21203 } 21204 21205 /* Unlink and return any mblk that looks like it contains a MDT info */ 21206 static mblk_t * 21207 tcp_mdt_info_mp(mblk_t *mp) 21208 { 21209 mblk_t *prev_mp; 21210 21211 for (;;) { 21212 prev_mp = mp; 21213 /* no more to process? */ 21214 if ((mp = mp->b_cont) == NULL) 21215 break; 21216 21217 switch (DB_TYPE(mp)) { 21218 case M_CTL: 21219 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21220 continue; 21221 ASSERT(prev_mp != NULL); 21222 prev_mp->b_cont = mp->b_cont; 21223 mp->b_cont = NULL; 21224 return (mp); 21225 default: 21226 break; 21227 } 21228 } 21229 return (mp); 21230 } 21231 21232 /* MDT info update routine, called when IP notifies us about MDT */ 21233 static void 21234 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21235 { 21236 boolean_t prev_state; 21237 21238 /* 21239 * IP is telling us to abort MDT on this connection? We know 21240 * this because the capability is only turned off when IP 21241 * encounters some pathological cases, e.g. link-layer change 21242 * where the new driver doesn't support MDT, or in situation 21243 * where MDT usage on the link-layer has been switched off. 21244 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21245 * if the link-layer doesn't support MDT, and if it does, it 21246 * will indicate that the feature is to be turned on. 21247 */ 21248 prev_state = tcp->tcp_mdt; 21249 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21250 if (!tcp->tcp_mdt && !first) { 21251 TCP_STAT(tcp_mdt_conn_halted3); 21252 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21253 (void *)tcp->tcp_connp)); 21254 } 21255 21256 /* 21257 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21258 * so disable MDT otherwise. The checks are done here 21259 * and in tcp_wput_data(). 21260 */ 21261 if (tcp->tcp_mdt && 21262 (tcp->tcp_ipversion == IPV4_VERSION && 21263 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21264 (tcp->tcp_ipversion == IPV6_VERSION && 21265 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21266 tcp->tcp_mdt = B_FALSE; 21267 21268 if (tcp->tcp_mdt) { 21269 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21270 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21271 "version (%d), expected version is %d", 21272 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21273 tcp->tcp_mdt = B_FALSE; 21274 return; 21275 } 21276 21277 /* 21278 * We need the driver to be able to handle at least three 21279 * spans per packet in order for tcp MDT to be utilized. 21280 * The first is for the header portion, while the rest are 21281 * needed to handle a packet that straddles across two 21282 * virtually non-contiguous buffers; a typical tcp packet 21283 * therefore consists of only two spans. Note that we take 21284 * a zero as "don't care". 21285 */ 21286 if (mdt_capab->ill_mdt_span_limit > 0 && 21287 mdt_capab->ill_mdt_span_limit < 3) { 21288 tcp->tcp_mdt = B_FALSE; 21289 return; 21290 } 21291 21292 /* a zero means driver wants default value */ 21293 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21294 tcp_mdt_max_pbufs); 21295 if (tcp->tcp_mdt_max_pld == 0) 21296 tcp->tcp_mdt_max_pld = tcp_mdt_max_pbufs; 21297 21298 /* ensure 32-bit alignment */ 21299 tcp->tcp_mdt_hdr_head = roundup(MAX(tcp_mdt_hdr_head_min, 21300 mdt_capab->ill_mdt_hdr_head), 4); 21301 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcp_mdt_hdr_tail_min, 21302 mdt_capab->ill_mdt_hdr_tail), 4); 21303 21304 if (!first && !prev_state) { 21305 TCP_STAT(tcp_mdt_conn_resumed2); 21306 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21307 (void *)tcp->tcp_connp)); 21308 } 21309 } 21310 } 21311 21312 /* Unlink and return any mblk that looks like it contains a LSO info */ 21313 static mblk_t * 21314 tcp_lso_info_mp(mblk_t *mp) 21315 { 21316 mblk_t *prev_mp; 21317 21318 for (;;) { 21319 prev_mp = mp; 21320 /* no more to process? */ 21321 if ((mp = mp->b_cont) == NULL) 21322 break; 21323 21324 switch (DB_TYPE(mp)) { 21325 case M_CTL: 21326 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21327 continue; 21328 ASSERT(prev_mp != NULL); 21329 prev_mp->b_cont = mp->b_cont; 21330 mp->b_cont = NULL; 21331 return (mp); 21332 default: 21333 break; 21334 } 21335 } 21336 21337 return (mp); 21338 } 21339 21340 /* LSO info update routine, called when IP notifies us about LSO */ 21341 static void 21342 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21343 { 21344 /* 21345 * IP is telling us to abort LSO on this connection? We know 21346 * this because the capability is only turned off when IP 21347 * encounters some pathological cases, e.g. link-layer change 21348 * where the new NIC/driver doesn't support LSO, or in situation 21349 * where LSO usage on the link-layer has been switched off. 21350 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21351 * if the link-layer doesn't support LSO, and if it does, it 21352 * will indicate that the feature is to be turned on. 21353 */ 21354 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21355 TCP_STAT(tcp_lso_enabled); 21356 21357 /* 21358 * We currently only support LSO on simple TCP/IPv4, 21359 * so disable LSO otherwise. The checks are done here 21360 * and in tcp_wput_data(). 21361 */ 21362 if (tcp->tcp_lso && 21363 (tcp->tcp_ipversion == IPV4_VERSION && 21364 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21365 (tcp->tcp_ipversion == IPV6_VERSION)) { 21366 tcp->tcp_lso = B_FALSE; 21367 TCP_STAT(tcp_lso_disabled); 21368 } else { 21369 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21370 lso_capab->ill_lso_max); 21371 } 21372 } 21373 21374 static void 21375 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21376 { 21377 conn_t *connp = tcp->tcp_connp; 21378 21379 ASSERT(ire != NULL); 21380 21381 /* 21382 * We may be in the fastpath here, and although we essentially do 21383 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21384 * we try to keep things as brief as possible. After all, these 21385 * are only best-effort checks, and we do more thorough ones prior 21386 * to calling tcp_send()/tcp_multisend(). 21387 */ 21388 if ((ip_lso_outbound || ip_multidata_outbound) && check_lso_mdt && 21389 !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21390 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21391 !(ire->ire_flags & RTF_MULTIRT) && 21392 !IPP_ENABLED(IPP_LOCAL_OUT) && 21393 CONN_IS_LSO_MD_FASTPATH(connp)) { 21394 if (ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21395 /* Cache the result */ 21396 connp->conn_lso_ok = B_TRUE; 21397 21398 ASSERT(ill->ill_lso_capab != NULL); 21399 if (!ill->ill_lso_capab->ill_lso_on) { 21400 ill->ill_lso_capab->ill_lso_on = 1; 21401 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21402 "LSO for interface %s\n", (void *)connp, 21403 ill->ill_name)); 21404 } 21405 tcp_lso_update(tcp, ill->ill_lso_capab); 21406 } else if (ip_multidata_outbound && ILL_MDT_CAPABLE(ill)) { 21407 /* Cache the result */ 21408 connp->conn_mdt_ok = B_TRUE; 21409 21410 ASSERT(ill->ill_mdt_capab != NULL); 21411 if (!ill->ill_mdt_capab->ill_mdt_on) { 21412 ill->ill_mdt_capab->ill_mdt_on = 1; 21413 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21414 "MDT for interface %s\n", (void *)connp, 21415 ill->ill_name)); 21416 } 21417 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21418 } 21419 } 21420 21421 /* 21422 * The goal is to reduce the number of generated tcp segments by 21423 * setting the maxpsz multiplier to 0; this will have an affect on 21424 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21425 * into each packet, up to SMSS bytes. Doing this reduces the number 21426 * of outbound segments and incoming ACKs, thus allowing for better 21427 * network and system performance. In contrast the legacy behavior 21428 * may result in sending less than SMSS size, because the last mblk 21429 * for some packets may have more data than needed to make up SMSS, 21430 * and the legacy code refused to "split" it. 21431 * 21432 * We apply the new behavior on following situations: 21433 * 21434 * 1) Loopback connections, 21435 * 2) Connections in which the remote peer is not on local subnet, 21436 * 3) Local subnet connections over the bge interface (see below). 21437 * 21438 * Ideally, we would like this behavior to apply for interfaces other 21439 * than bge. However, doing so would negatively impact drivers which 21440 * perform dynamic mapping and unmapping of DMA resources, which are 21441 * increased by setting the maxpsz multiplier to 0 (more mblks per 21442 * packet will be generated by tcp). The bge driver does not suffer 21443 * from this, as it copies the mblks into pre-mapped buffers, and 21444 * therefore does not require more I/O resources than before. 21445 * 21446 * Otherwise, this behavior is present on all network interfaces when 21447 * the destination endpoint is non-local, since reducing the number 21448 * of packets in general is good for the network. 21449 * 21450 * TODO We need to remove this hard-coded conditional for bge once 21451 * a better "self-tuning" mechanism, or a way to comprehend 21452 * the driver transmit strategy is devised. Until the solution 21453 * is found and well understood, we live with this hack. 21454 */ 21455 if (!tcp_static_maxpsz && 21456 (tcp->tcp_loopback || !tcp->tcp_localnet || 21457 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21458 /* override the default value */ 21459 tcp->tcp_maxpsz = 0; 21460 21461 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21462 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21463 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21464 } 21465 21466 /* set the stream head parameters accordingly */ 21467 (void) tcp_maxpsz_set(tcp, B_TRUE); 21468 } 21469 21470 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21471 static void 21472 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21473 { 21474 uchar_t fval = *mp->b_rptr; 21475 mblk_t *tail; 21476 queue_t *q = tcp->tcp_wq; 21477 21478 /* TODO: How should flush interact with urgent data? */ 21479 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21480 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21481 /* 21482 * Flush only data that has not yet been put on the wire. If 21483 * we flush data that we have already transmitted, life, as we 21484 * know it, may come to an end. 21485 */ 21486 tail = tcp->tcp_xmit_tail; 21487 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21488 tcp->tcp_xmit_tail_unsent = 0; 21489 tcp->tcp_unsent = 0; 21490 if (tail->b_wptr != tail->b_rptr) 21491 tail = tail->b_cont; 21492 if (tail) { 21493 mblk_t **excess = &tcp->tcp_xmit_head; 21494 for (;;) { 21495 mblk_t *mp1 = *excess; 21496 if (mp1 == tail) 21497 break; 21498 tcp->tcp_xmit_tail = mp1; 21499 tcp->tcp_xmit_last = mp1; 21500 excess = &mp1->b_cont; 21501 } 21502 *excess = NULL; 21503 tcp_close_mpp(&tail); 21504 if (tcp->tcp_snd_zcopy_aware) 21505 tcp_zcopy_notify(tcp); 21506 } 21507 /* 21508 * We have no unsent data, so unsent must be less than 21509 * tcp_xmit_lowater, so re-enable flow. 21510 */ 21511 if (tcp->tcp_flow_stopped) { 21512 tcp_clrqfull(tcp); 21513 } 21514 } 21515 /* 21516 * TODO: you can't just flush these, you have to increase rwnd for one 21517 * thing. For another, how should urgent data interact? 21518 */ 21519 if (fval & FLUSHR) { 21520 *mp->b_rptr = fval & ~FLUSHW; 21521 /* XXX */ 21522 qreply(q, mp); 21523 return; 21524 } 21525 freemsg(mp); 21526 } 21527 21528 /* 21529 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21530 * messages. 21531 */ 21532 static void 21533 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21534 { 21535 mblk_t *mp1; 21536 STRUCT_HANDLE(strbuf, sb); 21537 uint16_t port; 21538 queue_t *q = tcp->tcp_wq; 21539 in6_addr_t v6addr; 21540 ipaddr_t v4addr; 21541 uint32_t flowinfo = 0; 21542 int addrlen; 21543 21544 /* Make sure it is one of ours. */ 21545 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21546 case TI_GETMYNAME: 21547 case TI_GETPEERNAME: 21548 break; 21549 default: 21550 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21551 return; 21552 } 21553 switch (mi_copy_state(q, mp, &mp1)) { 21554 case -1: 21555 return; 21556 case MI_COPY_CASE(MI_COPY_IN, 1): 21557 break; 21558 case MI_COPY_CASE(MI_COPY_OUT, 1): 21559 /* Copy out the strbuf. */ 21560 mi_copyout(q, mp); 21561 return; 21562 case MI_COPY_CASE(MI_COPY_OUT, 2): 21563 /* All done. */ 21564 mi_copy_done(q, mp, 0); 21565 return; 21566 default: 21567 mi_copy_done(q, mp, EPROTO); 21568 return; 21569 } 21570 /* Check alignment of the strbuf */ 21571 if (!OK_32PTR(mp1->b_rptr)) { 21572 mi_copy_done(q, mp, EINVAL); 21573 return; 21574 } 21575 21576 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 21577 (void *)mp1->b_rptr); 21578 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21579 21580 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21581 mi_copy_done(q, mp, EINVAL); 21582 return; 21583 } 21584 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21585 case TI_GETMYNAME: 21586 if (tcp->tcp_family == AF_INET) { 21587 if (tcp->tcp_ipversion == IPV4_VERSION) { 21588 v4addr = tcp->tcp_ipha->ipha_src; 21589 } else { 21590 /* can't return an address in this case */ 21591 v4addr = 0; 21592 } 21593 } else { 21594 /* tcp->tcp_family == AF_INET6 */ 21595 if (tcp->tcp_ipversion == IPV4_VERSION) { 21596 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 21597 &v6addr); 21598 } else { 21599 v6addr = tcp->tcp_ip6h->ip6_src; 21600 } 21601 } 21602 port = tcp->tcp_lport; 21603 break; 21604 case TI_GETPEERNAME: 21605 if (tcp->tcp_family == AF_INET) { 21606 if (tcp->tcp_ipversion == IPV4_VERSION) { 21607 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 21608 v4addr); 21609 } else { 21610 /* can't return an address in this case */ 21611 v4addr = 0; 21612 } 21613 } else { 21614 /* tcp->tcp_family == AF_INET6) */ 21615 v6addr = tcp->tcp_remote_v6; 21616 if (tcp->tcp_ipversion == IPV6_VERSION) { 21617 /* 21618 * No flowinfo if tcp->tcp_ipversion is v4. 21619 * 21620 * flowinfo was already initialized to zero 21621 * where it was declared above, so only 21622 * set it if ipversion is v6. 21623 */ 21624 flowinfo = tcp->tcp_ip6h->ip6_vcf & 21625 ~IPV6_VERS_AND_FLOW_MASK; 21626 } 21627 } 21628 port = tcp->tcp_fport; 21629 break; 21630 default: 21631 mi_copy_done(q, mp, EPROTO); 21632 return; 21633 } 21634 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21635 if (!mp1) 21636 return; 21637 21638 if (tcp->tcp_family == AF_INET) { 21639 sin_t *sin; 21640 21641 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 21642 sin = (sin_t *)mp1->b_rptr; 21643 mp1->b_wptr = (uchar_t *)&sin[1]; 21644 *sin = sin_null; 21645 sin->sin_family = AF_INET; 21646 sin->sin_addr.s_addr = v4addr; 21647 sin->sin_port = port; 21648 } else { 21649 /* tcp->tcp_family == AF_INET6 */ 21650 sin6_t *sin6; 21651 21652 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 21653 sin6 = (sin6_t *)mp1->b_rptr; 21654 mp1->b_wptr = (uchar_t *)&sin6[1]; 21655 *sin6 = sin6_null; 21656 sin6->sin6_family = AF_INET6; 21657 sin6->sin6_flowinfo = flowinfo; 21658 sin6->sin6_addr = v6addr; 21659 sin6->sin6_port = port; 21660 } 21661 /* Copy out the address */ 21662 mi_copyout(q, mp); 21663 } 21664 21665 /* 21666 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21667 * messages. 21668 */ 21669 /* ARGSUSED */ 21670 static void 21671 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21672 { 21673 conn_t *connp = (conn_t *)arg; 21674 tcp_t *tcp = connp->conn_tcp; 21675 queue_t *q = tcp->tcp_wq; 21676 struct iocblk *iocp; 21677 21678 ASSERT(DB_TYPE(mp) == M_IOCTL); 21679 /* 21680 * Try and ASSERT the minimum possible references on the 21681 * conn early enough. Since we are executing on write side, 21682 * the connection is obviously not detached and that means 21683 * there is a ref each for TCP and IP. Since we are behind 21684 * the squeue, the minimum references needed are 3. If the 21685 * conn is in classifier hash list, there should be an 21686 * extra ref for that (we check both the possibilities). 21687 */ 21688 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21689 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21690 21691 iocp = (struct iocblk *)mp->b_rptr; 21692 switch (iocp->ioc_cmd) { 21693 case TCP_IOC_DEFAULT_Q: 21694 /* Wants to be the default wq. */ 21695 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 21696 iocp->ioc_error = EPERM; 21697 iocp->ioc_count = 0; 21698 mp->b_datap->db_type = M_IOCACK; 21699 qreply(q, mp); 21700 return; 21701 } 21702 tcp_def_q_set(tcp, mp); 21703 return; 21704 case _SIOCSOCKFALLBACK: 21705 /* 21706 * Either sockmod is about to be popped and the socket 21707 * would now be treated as a plain stream, or a module 21708 * is about to be pushed so we could no longer use read- 21709 * side synchronous streams for fused loopback tcp. 21710 * Drain any queued data and disable direct sockfs 21711 * interface from now on. 21712 */ 21713 if (!tcp->tcp_issocket) { 21714 DB_TYPE(mp) = M_IOCNAK; 21715 iocp->ioc_error = EINVAL; 21716 } else { 21717 #ifdef _ILP32 21718 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 21719 #else 21720 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21721 #endif 21722 /* 21723 * Insert this socket into the acceptor hash. 21724 * We might need it for T_CONN_RES message 21725 */ 21726 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21727 21728 if (tcp->tcp_fused) { 21729 /* 21730 * This is a fused loopback tcp; disable 21731 * read-side synchronous streams interface 21732 * and drain any queued data. It is okay 21733 * to do this for non-synchronous streams 21734 * fused tcp as well. 21735 */ 21736 tcp_fuse_disable_pair(tcp, B_FALSE); 21737 } 21738 tcp->tcp_issocket = B_FALSE; 21739 TCP_STAT(tcp_sock_fallback); 21740 21741 DB_TYPE(mp) = M_IOCACK; 21742 iocp->ioc_error = 0; 21743 } 21744 iocp->ioc_count = 0; 21745 iocp->ioc_rval = 0; 21746 qreply(q, mp); 21747 return; 21748 } 21749 CALL_IP_WPUT(connp, q, mp); 21750 } 21751 21752 /* 21753 * This routine is called by tcp_wput() to handle all TPI requests. 21754 */ 21755 /* ARGSUSED */ 21756 static void 21757 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21758 { 21759 conn_t *connp = (conn_t *)arg; 21760 tcp_t *tcp = connp->conn_tcp; 21761 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21762 uchar_t *rptr; 21763 t_scalar_t type; 21764 int len; 21765 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 21766 21767 /* 21768 * Try and ASSERT the minimum possible references on the 21769 * conn early enough. Since we are executing on write side, 21770 * the connection is obviously not detached and that means 21771 * there is a ref each for TCP and IP. Since we are behind 21772 * the squeue, the minimum references needed are 3. If the 21773 * conn is in classifier hash list, there should be an 21774 * extra ref for that (we check both the possibilities). 21775 */ 21776 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21777 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21778 21779 rptr = mp->b_rptr; 21780 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21781 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 21782 type = ((union T_primitives *)rptr)->type; 21783 if (type == T_EXDATA_REQ) { 21784 uint32_t msize = msgdsize(mp->b_cont); 21785 21786 len = msize - 1; 21787 if (len < 0) { 21788 freemsg(mp); 21789 return; 21790 } 21791 /* 21792 * Try to force urgent data out on the wire. 21793 * Even if we have unsent data this will 21794 * at least send the urgent flag. 21795 * XXX does not handle more flag correctly. 21796 */ 21797 len += tcp->tcp_unsent; 21798 len += tcp->tcp_snxt; 21799 tcp->tcp_urg = len; 21800 tcp->tcp_valid_bits |= TCP_URG_VALID; 21801 21802 /* Bypass tcp protocol for fused tcp loopback */ 21803 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 21804 return; 21805 } else if (type != T_DATA_REQ) { 21806 goto non_urgent_data; 21807 } 21808 /* TODO: options, flags, ... from user */ 21809 /* Set length to zero for reclamation below */ 21810 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 21811 freeb(mp); 21812 return; 21813 } else { 21814 if (tcp->tcp_debug) { 21815 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21816 "tcp_wput_proto, dropping one..."); 21817 } 21818 freemsg(mp); 21819 return; 21820 } 21821 21822 non_urgent_data: 21823 21824 switch ((int)tprim->type) { 21825 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 21826 /* 21827 * save the kssl_ent_t from the next block, and convert this 21828 * back to a normal bind_req. 21829 */ 21830 if (mp->b_cont != NULL) { 21831 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 21832 21833 if (tcp->tcp_kssl_ent != NULL) { 21834 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 21835 KSSL_NO_PROXY); 21836 tcp->tcp_kssl_ent = NULL; 21837 } 21838 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 21839 sizeof (kssl_ent_t)); 21840 kssl_hold_ent(tcp->tcp_kssl_ent); 21841 freemsg(mp->b_cont); 21842 mp->b_cont = NULL; 21843 } 21844 tprim->type = T_BIND_REQ; 21845 21846 /* FALLTHROUGH */ 21847 case O_T_BIND_REQ: /* bind request */ 21848 case T_BIND_REQ: /* new semantics bind request */ 21849 tcp_bind(tcp, mp); 21850 break; 21851 case T_UNBIND_REQ: /* unbind request */ 21852 tcp_unbind(tcp, mp); 21853 break; 21854 case O_T_CONN_RES: /* old connection response XXX */ 21855 case T_CONN_RES: /* connection response */ 21856 tcp_accept(tcp, mp); 21857 break; 21858 case T_CONN_REQ: /* connection request */ 21859 tcp_connect(tcp, mp); 21860 break; 21861 case T_DISCON_REQ: /* disconnect request */ 21862 tcp_disconnect(tcp, mp); 21863 break; 21864 case T_CAPABILITY_REQ: 21865 tcp_capability_req(tcp, mp); /* capability request */ 21866 break; 21867 case T_INFO_REQ: /* information request */ 21868 tcp_info_req(tcp, mp); 21869 break; 21870 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 21871 /* Only IP is allowed to return meaningful value */ 21872 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 21873 break; 21874 case T_OPTMGMT_REQ: 21875 /* 21876 * Note: no support for snmpcom_req() through new 21877 * T_OPTMGMT_REQ. See comments in ip.c 21878 */ 21879 /* Only IP is allowed to return meaningful value */ 21880 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 21881 break; 21882 21883 case T_UNITDATA_REQ: /* unitdata request */ 21884 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21885 break; 21886 case T_ORDREL_REQ: /* orderly release req */ 21887 freemsg(mp); 21888 21889 if (tcp->tcp_fused) 21890 tcp_unfuse(tcp); 21891 21892 if (tcp_xmit_end(tcp) != 0) { 21893 /* 21894 * We were crossing FINs and got a reset from 21895 * the other side. Just ignore it. 21896 */ 21897 if (tcp->tcp_debug) { 21898 (void) strlog(TCP_MOD_ID, 0, 1, 21899 SL_ERROR|SL_TRACE, 21900 "tcp_wput_proto, T_ORDREL_REQ out of " 21901 "state %s", 21902 tcp_display(tcp, NULL, 21903 DISP_ADDR_AND_PORT)); 21904 } 21905 } 21906 break; 21907 case T_ADDR_REQ: 21908 tcp_addr_req(tcp, mp); 21909 break; 21910 default: 21911 if (tcp->tcp_debug) { 21912 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21913 "tcp_wput_proto, bogus TPI msg, type %d", 21914 tprim->type); 21915 } 21916 /* 21917 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 21918 * to recover. 21919 */ 21920 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21921 break; 21922 } 21923 } 21924 21925 /* 21926 * The TCP write service routine should never be called... 21927 */ 21928 /* ARGSUSED */ 21929 static void 21930 tcp_wsrv(queue_t *q) 21931 { 21932 TCP_STAT(tcp_wsrv_called); 21933 } 21934 21935 /* Non overlapping byte exchanger */ 21936 static void 21937 tcp_xchg(uchar_t *a, uchar_t *b, int len) 21938 { 21939 uchar_t uch; 21940 21941 while (len-- > 0) { 21942 uch = a[len]; 21943 a[len] = b[len]; 21944 b[len] = uch; 21945 } 21946 } 21947 21948 /* 21949 * Send out a control packet on the tcp connection specified. This routine 21950 * is typically called where we need a simple ACK or RST generated. 21951 */ 21952 static void 21953 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 21954 { 21955 uchar_t *rptr; 21956 tcph_t *tcph; 21957 ipha_t *ipha = NULL; 21958 ip6_t *ip6h = NULL; 21959 uint32_t sum; 21960 int tcp_hdr_len; 21961 int tcp_ip_hdr_len; 21962 mblk_t *mp; 21963 21964 /* 21965 * Save sum for use in source route later. 21966 */ 21967 ASSERT(tcp != NULL); 21968 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 21969 tcp_hdr_len = tcp->tcp_hdr_len; 21970 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 21971 21972 /* If a text string is passed in with the request, pass it to strlog. */ 21973 if (str != NULL && tcp->tcp_debug) { 21974 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21975 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 21976 str, seq, ack, ctl); 21977 } 21978 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 21979 BPRI_MED); 21980 if (mp == NULL) { 21981 return; 21982 } 21983 rptr = &mp->b_rptr[tcp_wroff_xtra]; 21984 mp->b_rptr = rptr; 21985 mp->b_wptr = &rptr[tcp_hdr_len]; 21986 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 21987 21988 if (tcp->tcp_ipversion == IPV4_VERSION) { 21989 ipha = (ipha_t *)rptr; 21990 ipha->ipha_length = htons(tcp_hdr_len); 21991 } else { 21992 ip6h = (ip6_t *)rptr; 21993 ASSERT(tcp != NULL); 21994 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 21995 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21996 } 21997 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 21998 tcph->th_flags[0] = (uint8_t)ctl; 21999 if (ctl & TH_RST) { 22000 BUMP_MIB(&tcp_mib, tcpOutRsts); 22001 BUMP_MIB(&tcp_mib, tcpOutControl); 22002 /* 22003 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22004 */ 22005 if (tcp->tcp_snd_ts_ok && 22006 tcp->tcp_state > TCPS_SYN_SENT) { 22007 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22008 *(mp->b_wptr) = TCPOPT_EOL; 22009 if (tcp->tcp_ipversion == IPV4_VERSION) { 22010 ipha->ipha_length = htons(tcp_hdr_len - 22011 TCPOPT_REAL_TS_LEN); 22012 } else { 22013 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22014 TCPOPT_REAL_TS_LEN); 22015 } 22016 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22017 sum -= TCPOPT_REAL_TS_LEN; 22018 } 22019 } 22020 if (ctl & TH_ACK) { 22021 if (tcp->tcp_snd_ts_ok) { 22022 U32_TO_BE32(lbolt, 22023 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22024 U32_TO_BE32(tcp->tcp_ts_recent, 22025 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22026 } 22027 22028 /* Update the latest receive window size in TCP header. */ 22029 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22030 tcph->th_win); 22031 tcp->tcp_rack = ack; 22032 tcp->tcp_rack_cnt = 0; 22033 BUMP_MIB(&tcp_mib, tcpOutAck); 22034 } 22035 BUMP_LOCAL(tcp->tcp_obsegs); 22036 U32_TO_BE32(seq, tcph->th_seq); 22037 U32_TO_BE32(ack, tcph->th_ack); 22038 /* 22039 * Include the adjustment for a source route if any. 22040 */ 22041 sum = (sum >> 16) + (sum & 0xFFFF); 22042 U16_TO_BE16(sum, tcph->th_sum); 22043 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22044 tcp_send_data(tcp, tcp->tcp_wq, mp); 22045 } 22046 22047 /* 22048 * If this routine returns B_TRUE, TCP can generate a RST in response 22049 * to a segment. If it returns B_FALSE, TCP should not respond. 22050 */ 22051 static boolean_t 22052 tcp_send_rst_chk(void) 22053 { 22054 clock_t now; 22055 22056 /* 22057 * TCP needs to protect itself from generating too many RSTs. 22058 * This can be a DoS attack by sending us random segments 22059 * soliciting RSTs. 22060 * 22061 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22062 * in each 1 second interval. In this way, TCP still generate 22063 * RSTs in normal cases but when under attack, the impact is 22064 * limited. 22065 */ 22066 if (tcp_rst_sent_rate_enabled != 0) { 22067 now = lbolt; 22068 /* lbolt can wrap around. */ 22069 if ((tcp_last_rst_intrvl > now) || 22070 (TICK_TO_MSEC(now - tcp_last_rst_intrvl) > 1*SECONDS)) { 22071 tcp_last_rst_intrvl = now; 22072 tcp_rst_cnt = 1; 22073 } else if (++tcp_rst_cnt > tcp_rst_sent_rate) { 22074 return (B_FALSE); 22075 } 22076 } 22077 return (B_TRUE); 22078 } 22079 22080 /* 22081 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22082 */ 22083 static void 22084 tcp_ip_ire_mark_advice(tcp_t *tcp) 22085 { 22086 mblk_t *mp; 22087 ipic_t *ipic; 22088 22089 if (tcp->tcp_ipversion == IPV4_VERSION) { 22090 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22091 &ipic); 22092 } else { 22093 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22094 &ipic); 22095 } 22096 if (mp == NULL) 22097 return; 22098 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22099 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22100 } 22101 22102 /* 22103 * Return an IP advice ioctl mblk and set ipic to be the pointer 22104 * to the advice structure. 22105 */ 22106 static mblk_t * 22107 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22108 { 22109 struct iocblk *ioc; 22110 mblk_t *mp, *mp1; 22111 22112 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22113 if (mp == NULL) 22114 return (NULL); 22115 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22116 *ipic = (ipic_t *)mp->b_rptr; 22117 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22118 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22119 22120 bcopy(addr, *ipic + 1, addr_len); 22121 22122 (*ipic)->ipic_addr_length = addr_len; 22123 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22124 22125 mp1 = mkiocb(IP_IOCTL); 22126 if (mp1 == NULL) { 22127 freemsg(mp); 22128 return (NULL); 22129 } 22130 mp1->b_cont = mp; 22131 ioc = (struct iocblk *)mp1->b_rptr; 22132 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22133 22134 return (mp1); 22135 } 22136 22137 /* 22138 * Generate a reset based on an inbound packet for which there is no active 22139 * tcp state that we can find. 22140 * 22141 * IPSEC NOTE : Try to send the reply with the same protection as it came 22142 * in. We still have the ipsec_mp that the packet was attached to. Thus 22143 * the packet will go out at the same level of protection as it came in by 22144 * converting the IPSEC_IN to IPSEC_OUT. 22145 */ 22146 static void 22147 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22148 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid) 22149 { 22150 ipha_t *ipha = NULL; 22151 ip6_t *ip6h = NULL; 22152 ushort_t len; 22153 tcph_t *tcph; 22154 int i; 22155 mblk_t *ipsec_mp; 22156 boolean_t mctl_present; 22157 ipic_t *ipic; 22158 ipaddr_t v4addr; 22159 in6_addr_t v6addr; 22160 int addr_len; 22161 void *addr; 22162 queue_t *q = tcp_g_q; 22163 tcp_t *tcp = Q_TO_TCP(q); 22164 cred_t *cr; 22165 mblk_t *nmp; 22166 22167 if (!tcp_send_rst_chk()) { 22168 tcp_rst_unsent++; 22169 freemsg(mp); 22170 return; 22171 } 22172 22173 if (mp->b_datap->db_type == M_CTL) { 22174 ipsec_mp = mp; 22175 mp = mp->b_cont; 22176 mctl_present = B_TRUE; 22177 } else { 22178 ipsec_mp = mp; 22179 mctl_present = B_FALSE; 22180 } 22181 22182 if (str && q && tcp_dbg) { 22183 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22184 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22185 "flags 0x%x", 22186 str, seq, ack, ctl); 22187 } 22188 if (mp->b_datap->db_ref != 1) { 22189 mblk_t *mp1 = copyb(mp); 22190 freemsg(mp); 22191 mp = mp1; 22192 if (!mp) { 22193 if (mctl_present) 22194 freeb(ipsec_mp); 22195 return; 22196 } else { 22197 if (mctl_present) { 22198 ipsec_mp->b_cont = mp; 22199 } else { 22200 ipsec_mp = mp; 22201 } 22202 } 22203 } else if (mp->b_cont) { 22204 freemsg(mp->b_cont); 22205 mp->b_cont = NULL; 22206 } 22207 /* 22208 * We skip reversing source route here. 22209 * (for now we replace all IP options with EOL) 22210 */ 22211 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22212 ipha = (ipha_t *)mp->b_rptr; 22213 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22214 mp->b_rptr[i] = IPOPT_EOL; 22215 /* 22216 * Make sure that src address isn't flagrantly invalid. 22217 * Not all broadcast address checking for the src address 22218 * is possible, since we don't know the netmask of the src 22219 * addr. No check for destination address is done, since 22220 * IP will not pass up a packet with a broadcast dest 22221 * address to TCP. Similar checks are done below for IPv6. 22222 */ 22223 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22224 CLASSD(ipha->ipha_src)) { 22225 freemsg(ipsec_mp); 22226 BUMP_MIB(&ip_mib, ipInDiscards); 22227 return; 22228 } 22229 } else { 22230 ip6h = (ip6_t *)mp->b_rptr; 22231 22232 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22233 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22234 freemsg(ipsec_mp); 22235 BUMP_MIB(&ip6_mib, ipv6InDiscards); 22236 return; 22237 } 22238 22239 /* Remove any extension headers assuming partial overlay */ 22240 if (ip_hdr_len > IPV6_HDR_LEN) { 22241 uint8_t *to; 22242 22243 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22244 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22245 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22246 ip_hdr_len = IPV6_HDR_LEN; 22247 ip6h = (ip6_t *)mp->b_rptr; 22248 ip6h->ip6_nxt = IPPROTO_TCP; 22249 } 22250 } 22251 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22252 if (tcph->th_flags[0] & TH_RST) { 22253 freemsg(ipsec_mp); 22254 return; 22255 } 22256 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22257 len = ip_hdr_len + sizeof (tcph_t); 22258 mp->b_wptr = &mp->b_rptr[len]; 22259 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22260 ipha->ipha_length = htons(len); 22261 /* Swap addresses */ 22262 v4addr = ipha->ipha_src; 22263 ipha->ipha_src = ipha->ipha_dst; 22264 ipha->ipha_dst = v4addr; 22265 ipha->ipha_ident = 0; 22266 ipha->ipha_ttl = (uchar_t)tcp_ipv4_ttl; 22267 addr_len = IP_ADDR_LEN; 22268 addr = &v4addr; 22269 } else { 22270 /* No ip6i_t in this case */ 22271 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22272 /* Swap addresses */ 22273 v6addr = ip6h->ip6_src; 22274 ip6h->ip6_src = ip6h->ip6_dst; 22275 ip6h->ip6_dst = v6addr; 22276 ip6h->ip6_hops = (uchar_t)tcp_ipv6_hoplimit; 22277 addr_len = IPV6_ADDR_LEN; 22278 addr = &v6addr; 22279 } 22280 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22281 U32_TO_BE32(ack, tcph->th_ack); 22282 U32_TO_BE32(seq, tcph->th_seq); 22283 U16_TO_BE16(0, tcph->th_win); 22284 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22285 tcph->th_flags[0] = (uint8_t)ctl; 22286 if (ctl & TH_RST) { 22287 BUMP_MIB(&tcp_mib, tcpOutRsts); 22288 BUMP_MIB(&tcp_mib, tcpOutControl); 22289 } 22290 22291 /* IP trusts us to set up labels when required. */ 22292 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 22293 crgetlabel(cr) != NULL) { 22294 int err, adjust; 22295 22296 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22297 err = tsol_check_label(cr, &mp, &adjust, 22298 tcp->tcp_connp->conn_mac_exempt); 22299 else 22300 err = tsol_check_label_v6(cr, &mp, &adjust, 22301 tcp->tcp_connp->conn_mac_exempt); 22302 if (mctl_present) 22303 ipsec_mp->b_cont = mp; 22304 else 22305 ipsec_mp = mp; 22306 if (err != 0) { 22307 freemsg(ipsec_mp); 22308 return; 22309 } 22310 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22311 ipha = (ipha_t *)mp->b_rptr; 22312 adjust += ntohs(ipha->ipha_length); 22313 ipha->ipha_length = htons(adjust); 22314 } else { 22315 ip6h = (ip6_t *)mp->b_rptr; 22316 } 22317 } 22318 22319 if (mctl_present) { 22320 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22321 22322 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22323 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22324 return; 22325 } 22326 } 22327 if (zoneid == ALL_ZONES) 22328 zoneid = GLOBAL_ZONEID; 22329 22330 /* Add the zoneid so ip_output routes it properly */ 22331 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid)) == NULL) { 22332 freemsg(ipsec_mp); 22333 return; 22334 } 22335 ipsec_mp = nmp; 22336 22337 /* 22338 * NOTE: one might consider tracing a TCP packet here, but 22339 * this function has no active TCP state and no tcp structure 22340 * that has a trace buffer. If we traced here, we would have 22341 * to keep a local trace buffer in tcp_record_trace(). 22342 * 22343 * TSol note: The mblk that contains the incoming packet was 22344 * reused by tcp_xmit_listener_reset, so it already contains 22345 * the right credentials and we don't need to call mblk_setcred. 22346 * Also the conn's cred is not right since it is associated 22347 * with tcp_g_q. 22348 */ 22349 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22350 22351 /* 22352 * Tell IP to mark the IRE used for this destination temporary. 22353 * This way, we can limit our exposure to DoS attack because IP 22354 * creates an IRE for each destination. If there are too many, 22355 * the time to do any routing lookup will be extremely long. And 22356 * the lookup can be in interrupt context. 22357 * 22358 * Note that in normal circumstances, this marking should not 22359 * affect anything. It would be nice if only 1 message is 22360 * needed to inform IP that the IRE created for this RST should 22361 * not be added to the cache table. But there is currently 22362 * not such communication mechanism between TCP and IP. So 22363 * the best we can do now is to send the advice ioctl to IP 22364 * to mark the IRE temporary. 22365 */ 22366 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22367 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22368 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22369 } 22370 } 22371 22372 /* 22373 * Initiate closedown sequence on an active connection. (May be called as 22374 * writer.) Return value zero for OK return, non-zero for error return. 22375 */ 22376 static int 22377 tcp_xmit_end(tcp_t *tcp) 22378 { 22379 ipic_t *ipic; 22380 mblk_t *mp; 22381 22382 if (tcp->tcp_state < TCPS_SYN_RCVD || 22383 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22384 /* 22385 * Invalid state, only states TCPS_SYN_RCVD, 22386 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22387 */ 22388 return (-1); 22389 } 22390 22391 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22392 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22393 /* 22394 * If there is nothing more unsent, send the FIN now. 22395 * Otherwise, it will go out with the last segment. 22396 */ 22397 if (tcp->tcp_unsent == 0) { 22398 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22399 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22400 22401 if (mp) { 22402 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22403 tcp_send_data(tcp, tcp->tcp_wq, mp); 22404 } else { 22405 /* 22406 * Couldn't allocate msg. Pretend we got it out. 22407 * Wait for rexmit timeout. 22408 */ 22409 tcp->tcp_snxt = tcp->tcp_fss + 1; 22410 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22411 } 22412 22413 /* 22414 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22415 * changed. 22416 */ 22417 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22418 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22419 } 22420 } else { 22421 /* 22422 * If tcp->tcp_cork is set, then the data will not get sent, 22423 * so we have to check that and unset it first. 22424 */ 22425 if (tcp->tcp_cork) 22426 tcp->tcp_cork = B_FALSE; 22427 tcp_wput_data(tcp, NULL, B_FALSE); 22428 } 22429 22430 /* 22431 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22432 * is 0, don't update the cache. 22433 */ 22434 if (tcp_rtt_updates == 0 || tcp->tcp_rtt_update < tcp_rtt_updates) 22435 return (0); 22436 22437 /* 22438 * NOTE: should not update if source routes i.e. if tcp_remote if 22439 * different from the destination. 22440 */ 22441 if (tcp->tcp_ipversion == IPV4_VERSION) { 22442 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22443 return (0); 22444 } 22445 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22446 &ipic); 22447 } else { 22448 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22449 &tcp->tcp_ip6h->ip6_dst))) { 22450 return (0); 22451 } 22452 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22453 &ipic); 22454 } 22455 22456 /* Record route attributes in the IRE for use by future connections. */ 22457 if (mp == NULL) 22458 return (0); 22459 22460 /* 22461 * We do not have a good algorithm to update ssthresh at this time. 22462 * So don't do any update. 22463 */ 22464 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22465 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22466 22467 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22468 return (0); 22469 } 22470 22471 /* 22472 * Generate a "no listener here" RST in response to an "unknown" segment. 22473 * Note that we are reusing the incoming mp to construct the outgoing 22474 * RST. 22475 */ 22476 void 22477 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid) 22478 { 22479 uchar_t *rptr; 22480 uint32_t seg_len; 22481 tcph_t *tcph; 22482 uint32_t seg_seq; 22483 uint32_t seg_ack; 22484 uint_t flags; 22485 mblk_t *ipsec_mp; 22486 ipha_t *ipha; 22487 ip6_t *ip6h; 22488 boolean_t mctl_present = B_FALSE; 22489 boolean_t check = B_TRUE; 22490 boolean_t policy_present; 22491 22492 TCP_STAT(tcp_no_listener); 22493 22494 ipsec_mp = mp; 22495 22496 if (mp->b_datap->db_type == M_CTL) { 22497 ipsec_in_t *ii; 22498 22499 mctl_present = B_TRUE; 22500 mp = mp->b_cont; 22501 22502 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22503 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22504 if (ii->ipsec_in_dont_check) { 22505 check = B_FALSE; 22506 if (!ii->ipsec_in_secure) { 22507 freeb(ipsec_mp); 22508 mctl_present = B_FALSE; 22509 ipsec_mp = mp; 22510 } 22511 } 22512 } 22513 22514 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22515 policy_present = ipsec_inbound_v4_policy_present; 22516 ipha = (ipha_t *)mp->b_rptr; 22517 ip6h = NULL; 22518 } else { 22519 policy_present = ipsec_inbound_v6_policy_present; 22520 ipha = NULL; 22521 ip6h = (ip6_t *)mp->b_rptr; 22522 } 22523 22524 if (check && policy_present) { 22525 /* 22526 * The conn_t parameter is NULL because we already know 22527 * nobody's home. 22528 */ 22529 ipsec_mp = ipsec_check_global_policy( 22530 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present); 22531 if (ipsec_mp == NULL) 22532 return; 22533 } 22534 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22535 DTRACE_PROBE2( 22536 tx__ip__log__error__nolistener__tcp, 22537 char *, "Could not reply with RST to mp(1)", 22538 mblk_t *, mp); 22539 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22540 freemsg(ipsec_mp); 22541 return; 22542 } 22543 22544 rptr = mp->b_rptr; 22545 22546 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22547 seg_seq = BE32_TO_U32(tcph->th_seq); 22548 seg_ack = BE32_TO_U32(tcph->th_ack); 22549 flags = tcph->th_flags[0]; 22550 22551 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22552 if (flags & TH_RST) { 22553 freemsg(ipsec_mp); 22554 } else if (flags & TH_ACK) { 22555 tcp_xmit_early_reset("no tcp, reset", 22556 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid); 22557 } else { 22558 if (flags & TH_SYN) { 22559 seg_len++; 22560 } else { 22561 /* 22562 * Here we violate the RFC. Note that a normal 22563 * TCP will never send a segment without the ACK 22564 * flag, except for RST or SYN segment. This 22565 * segment is neither. Just drop it on the 22566 * floor. 22567 */ 22568 freemsg(ipsec_mp); 22569 tcp_rst_unsent++; 22570 return; 22571 } 22572 22573 tcp_xmit_early_reset("no tcp, reset/ack", 22574 ipsec_mp, 0, seg_seq + seg_len, 22575 TH_RST | TH_ACK, ip_hdr_len, zoneid); 22576 } 22577 } 22578 22579 /* 22580 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22581 * ip and tcp header ready to pass down to IP. If the mp passed in is 22582 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22583 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22584 * otherwise it will dup partial mblks.) 22585 * Otherwise, an appropriate ACK packet will be generated. This 22586 * routine is not usually called to send new data for the first time. It 22587 * is mostly called out of the timer for retransmits, and to generate ACKs. 22588 * 22589 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22590 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22591 * of the original mblk chain will be returned in *offset and *end_mp. 22592 */ 22593 mblk_t * 22594 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22595 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22596 boolean_t rexmit) 22597 { 22598 int data_length; 22599 int32_t off = 0; 22600 uint_t flags; 22601 mblk_t *mp1; 22602 mblk_t *mp2; 22603 uchar_t *rptr; 22604 tcph_t *tcph; 22605 int32_t num_sack_blk = 0; 22606 int32_t sack_opt_len = 0; 22607 22608 /* Allocate for our maximum TCP header + link-level */ 22609 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 22610 BPRI_MED); 22611 if (!mp1) 22612 return (NULL); 22613 data_length = 0; 22614 22615 /* 22616 * Note that tcp_mss has been adjusted to take into account the 22617 * timestamp option if applicable. Because SACK options do not 22618 * appear in every TCP segments and they are of variable lengths, 22619 * they cannot be included in tcp_mss. Thus we need to calculate 22620 * the actual segment length when we need to send a segment which 22621 * includes SACK options. 22622 */ 22623 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22624 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22625 tcp->tcp_num_sack_blk); 22626 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22627 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22628 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22629 max_to_send -= sack_opt_len; 22630 } 22631 22632 if (offset != NULL) { 22633 off = *offset; 22634 /* We use offset as an indicator that end_mp is not NULL. */ 22635 *end_mp = NULL; 22636 } 22637 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22638 /* This could be faster with cooperation from downstream */ 22639 if (mp2 != mp1 && !sendall && 22640 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22641 max_to_send) 22642 /* 22643 * Don't send the next mblk since the whole mblk 22644 * does not fit. 22645 */ 22646 break; 22647 mp2->b_cont = dupb(mp); 22648 mp2 = mp2->b_cont; 22649 if (!mp2) { 22650 freemsg(mp1); 22651 return (NULL); 22652 } 22653 mp2->b_rptr += off; 22654 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22655 (uintptr_t)INT_MAX); 22656 22657 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22658 if (data_length > max_to_send) { 22659 mp2->b_wptr -= data_length - max_to_send; 22660 data_length = max_to_send; 22661 off = mp2->b_wptr - mp->b_rptr; 22662 break; 22663 } else { 22664 off = 0; 22665 } 22666 } 22667 if (offset != NULL) { 22668 *offset = off; 22669 *end_mp = mp; 22670 } 22671 if (seg_len != NULL) { 22672 *seg_len = data_length; 22673 } 22674 22675 /* Update the latest receive window size in TCP header. */ 22676 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22677 tcp->tcp_tcph->th_win); 22678 22679 rptr = mp1->b_rptr + tcp_wroff_xtra; 22680 mp1->b_rptr = rptr; 22681 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22682 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22683 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22684 U32_TO_ABE32(seq, tcph->th_seq); 22685 22686 /* 22687 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22688 * that this function was called from tcp_wput_data. Thus, when called 22689 * to retransmit data the setting of the PUSH bit may appear some 22690 * what random in that it might get set when it should not. This 22691 * should not pose any performance issues. 22692 */ 22693 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22694 tcp->tcp_unsent == data_length)) { 22695 flags = TH_ACK | TH_PUSH; 22696 } else { 22697 flags = TH_ACK; 22698 } 22699 22700 if (tcp->tcp_ecn_ok) { 22701 if (tcp->tcp_ecn_echo_on) 22702 flags |= TH_ECE; 22703 22704 /* 22705 * Only set ECT bit and ECN_CWR if a segment contains new data. 22706 * There is no TCP flow control for non-data segments, and 22707 * only data segment is transmitted reliably. 22708 */ 22709 if (data_length > 0 && !rexmit) { 22710 SET_ECT(tcp, rptr); 22711 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 22712 flags |= TH_CWR; 22713 tcp->tcp_ecn_cwr_sent = B_TRUE; 22714 } 22715 } 22716 } 22717 22718 if (tcp->tcp_valid_bits) { 22719 uint32_t u1; 22720 22721 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 22722 seq == tcp->tcp_iss) { 22723 uchar_t *wptr; 22724 22725 /* 22726 * If TCP_ISS_VALID and the seq number is tcp_iss, 22727 * TCP can only be in SYN-SENT, SYN-RCVD or 22728 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 22729 * our SYN is not ack'ed but the app closes this 22730 * TCP connection. 22731 */ 22732 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 22733 tcp->tcp_state == TCPS_SYN_RCVD || 22734 tcp->tcp_state == TCPS_FIN_WAIT_1); 22735 22736 /* 22737 * Tack on the MSS option. It is always needed 22738 * for both active and passive open. 22739 * 22740 * MSS option value should be interface MTU - MIN 22741 * TCP/IP header according to RFC 793 as it means 22742 * the maximum segment size TCP can receive. But 22743 * to get around some broken middle boxes/end hosts 22744 * out there, we allow the option value to be the 22745 * same as the MSS option size on the peer side. 22746 * In this way, the other side will not send 22747 * anything larger than they can receive. 22748 * 22749 * Note that for SYN_SENT state, the ndd param 22750 * tcp_use_smss_as_mss_opt has no effect as we 22751 * don't know the peer's MSS option value. So 22752 * the only case we need to take care of is in 22753 * SYN_RCVD state, which is done later. 22754 */ 22755 wptr = mp1->b_wptr; 22756 wptr[0] = TCPOPT_MAXSEG; 22757 wptr[1] = TCPOPT_MAXSEG_LEN; 22758 wptr += 2; 22759 u1 = tcp->tcp_if_mtu - 22760 (tcp->tcp_ipversion == IPV4_VERSION ? 22761 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 22762 TCP_MIN_HEADER_LENGTH; 22763 U16_TO_BE16(u1, wptr); 22764 mp1->b_wptr = wptr + 2; 22765 /* Update the offset to cover the additional word */ 22766 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22767 22768 /* 22769 * Note that the following way of filling in 22770 * TCP options are not optimal. Some NOPs can 22771 * be saved. But there is no need at this time 22772 * to optimize it. When it is needed, we will 22773 * do it. 22774 */ 22775 switch (tcp->tcp_state) { 22776 case TCPS_SYN_SENT: 22777 flags = TH_SYN; 22778 22779 if (tcp->tcp_snd_ts_ok) { 22780 uint32_t llbolt = (uint32_t)lbolt; 22781 22782 wptr = mp1->b_wptr; 22783 wptr[0] = TCPOPT_NOP; 22784 wptr[1] = TCPOPT_NOP; 22785 wptr[2] = TCPOPT_TSTAMP; 22786 wptr[3] = TCPOPT_TSTAMP_LEN; 22787 wptr += 4; 22788 U32_TO_BE32(llbolt, wptr); 22789 wptr += 4; 22790 ASSERT(tcp->tcp_ts_recent == 0); 22791 U32_TO_BE32(0L, wptr); 22792 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 22793 tcph->th_offset_and_rsrvd[0] += 22794 (3 << 4); 22795 } 22796 22797 /* 22798 * Set up all the bits to tell other side 22799 * we are ECN capable. 22800 */ 22801 if (tcp->tcp_ecn_ok) { 22802 flags |= (TH_ECE | TH_CWR); 22803 } 22804 break; 22805 case TCPS_SYN_RCVD: 22806 flags |= TH_SYN; 22807 22808 /* 22809 * Reset the MSS option value to be SMSS 22810 * We should probably add back the bytes 22811 * for timestamp option and IPsec. We 22812 * don't do that as this is a workaround 22813 * for broken middle boxes/end hosts, it 22814 * is better for us to be more cautious. 22815 * They may not take these things into 22816 * account in their SMSS calculation. Thus 22817 * the peer's calculated SMSS may be smaller 22818 * than what it can be. This should be OK. 22819 */ 22820 if (tcp_use_smss_as_mss_opt) { 22821 u1 = tcp->tcp_mss; 22822 U16_TO_BE16(u1, wptr); 22823 } 22824 22825 /* 22826 * If the other side is ECN capable, reply 22827 * that we are also ECN capable. 22828 */ 22829 if (tcp->tcp_ecn_ok) 22830 flags |= TH_ECE; 22831 break; 22832 default: 22833 /* 22834 * The above ASSERT() makes sure that this 22835 * must be FIN-WAIT-1 state. Our SYN has 22836 * not been ack'ed so retransmit it. 22837 */ 22838 flags |= TH_SYN; 22839 break; 22840 } 22841 22842 if (tcp->tcp_snd_ws_ok) { 22843 wptr = mp1->b_wptr; 22844 wptr[0] = TCPOPT_NOP; 22845 wptr[1] = TCPOPT_WSCALE; 22846 wptr[2] = TCPOPT_WS_LEN; 22847 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 22848 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 22849 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22850 } 22851 22852 if (tcp->tcp_snd_sack_ok) { 22853 wptr = mp1->b_wptr; 22854 wptr[0] = TCPOPT_NOP; 22855 wptr[1] = TCPOPT_NOP; 22856 wptr[2] = TCPOPT_SACK_PERMITTED; 22857 wptr[3] = TCPOPT_SACK_OK_LEN; 22858 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 22859 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22860 } 22861 22862 /* allocb() of adequate mblk assures space */ 22863 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 22864 (uintptr_t)INT_MAX); 22865 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 22866 /* 22867 * Get IP set to checksum on our behalf 22868 * Include the adjustment for a source route if any. 22869 */ 22870 u1 += tcp->tcp_sum; 22871 u1 = (u1 >> 16) + (u1 & 0xFFFF); 22872 U16_TO_BE16(u1, tcph->th_sum); 22873 BUMP_MIB(&tcp_mib, tcpOutControl); 22874 } 22875 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 22876 (seq + data_length) == tcp->tcp_fss) { 22877 if (!tcp->tcp_fin_acked) { 22878 flags |= TH_FIN; 22879 BUMP_MIB(&tcp_mib, tcpOutControl); 22880 } 22881 if (!tcp->tcp_fin_sent) { 22882 tcp->tcp_fin_sent = B_TRUE; 22883 switch (tcp->tcp_state) { 22884 case TCPS_SYN_RCVD: 22885 case TCPS_ESTABLISHED: 22886 tcp->tcp_state = TCPS_FIN_WAIT_1; 22887 break; 22888 case TCPS_CLOSE_WAIT: 22889 tcp->tcp_state = TCPS_LAST_ACK; 22890 break; 22891 } 22892 if (tcp->tcp_suna == tcp->tcp_snxt) 22893 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22894 tcp->tcp_snxt = tcp->tcp_fss + 1; 22895 } 22896 } 22897 /* 22898 * Note the trick here. u1 is unsigned. When tcp_urg 22899 * is smaller than seq, u1 will become a very huge value. 22900 * So the comparison will fail. Also note that tcp_urp 22901 * should be positive, see RFC 793 page 17. 22902 */ 22903 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 22904 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 22905 u1 < (uint32_t)(64 * 1024)) { 22906 flags |= TH_URG; 22907 BUMP_MIB(&tcp_mib, tcpOutUrg); 22908 U32_TO_ABE16(u1, tcph->th_urp); 22909 } 22910 } 22911 tcph->th_flags[0] = (uchar_t)flags; 22912 tcp->tcp_rack = tcp->tcp_rnxt; 22913 tcp->tcp_rack_cnt = 0; 22914 22915 if (tcp->tcp_snd_ts_ok) { 22916 if (tcp->tcp_state != TCPS_SYN_SENT) { 22917 uint32_t llbolt = (uint32_t)lbolt; 22918 22919 U32_TO_BE32(llbolt, 22920 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22921 U32_TO_BE32(tcp->tcp_ts_recent, 22922 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22923 } 22924 } 22925 22926 if (num_sack_blk > 0) { 22927 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22928 sack_blk_t *tmp; 22929 int32_t i; 22930 22931 wptr[0] = TCPOPT_NOP; 22932 wptr[1] = TCPOPT_NOP; 22933 wptr[2] = TCPOPT_SACK; 22934 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22935 sizeof (sack_blk_t); 22936 wptr += TCPOPT_REAL_SACK_LEN; 22937 22938 tmp = tcp->tcp_sack_list; 22939 for (i = 0; i < num_sack_blk; i++) { 22940 U32_TO_BE32(tmp[i].begin, wptr); 22941 wptr += sizeof (tcp_seq); 22942 U32_TO_BE32(tmp[i].end, wptr); 22943 wptr += sizeof (tcp_seq); 22944 } 22945 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 22946 } 22947 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22948 data_length += (int)(mp1->b_wptr - rptr); 22949 if (tcp->tcp_ipversion == IPV4_VERSION) { 22950 ((ipha_t *)rptr)->ipha_length = htons(data_length); 22951 } else { 22952 ip6_t *ip6 = (ip6_t *)(rptr + 22953 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22954 sizeof (ip6i_t) : 0)); 22955 22956 ip6->ip6_plen = htons(data_length - 22957 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22958 } 22959 22960 /* 22961 * Prime pump for IP 22962 * Include the adjustment for a source route if any. 22963 */ 22964 data_length -= tcp->tcp_ip_hdr_len; 22965 data_length += tcp->tcp_sum; 22966 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22967 U16_TO_ABE16(data_length, tcph->th_sum); 22968 if (tcp->tcp_ip_forward_progress) { 22969 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22970 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22971 tcp->tcp_ip_forward_progress = B_FALSE; 22972 } 22973 return (mp1); 22974 } 22975 22976 /* This function handles the push timeout. */ 22977 void 22978 tcp_push_timer(void *arg) 22979 { 22980 conn_t *connp = (conn_t *)arg; 22981 tcp_t *tcp = connp->conn_tcp; 22982 22983 TCP_DBGSTAT(tcp_push_timer_cnt); 22984 22985 ASSERT(tcp->tcp_listener == NULL); 22986 22987 /* 22988 * We need to plug synchronous streams during our drain to prevent 22989 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 22990 */ 22991 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 22992 tcp->tcp_push_tid = 0; 22993 if ((tcp->tcp_rcv_list != NULL) && 22994 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 22995 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 22996 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 22997 } 22998 22999 /* 23000 * This function handles delayed ACK timeout. 23001 */ 23002 static void 23003 tcp_ack_timer(void *arg) 23004 { 23005 conn_t *connp = (conn_t *)arg; 23006 tcp_t *tcp = connp->conn_tcp; 23007 mblk_t *mp; 23008 23009 TCP_DBGSTAT(tcp_ack_timer_cnt); 23010 23011 tcp->tcp_ack_tid = 0; 23012 23013 if (tcp->tcp_fused) 23014 return; 23015 23016 /* 23017 * Do not send ACK if there is no outstanding unack'ed data. 23018 */ 23019 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23020 return; 23021 } 23022 23023 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23024 /* 23025 * Make sure we don't allow deferred ACKs to result in 23026 * timer-based ACKing. If we have held off an ACK 23027 * when there was more than an mss here, and the timer 23028 * goes off, we have to worry about the possibility 23029 * that the sender isn't doing slow-start, or is out 23030 * of step with us for some other reason. We fall 23031 * permanently back in the direction of 23032 * ACK-every-other-packet as suggested in RFC 1122. 23033 */ 23034 if (tcp->tcp_rack_abs_max > 2) 23035 tcp->tcp_rack_abs_max--; 23036 tcp->tcp_rack_cur_max = 2; 23037 } 23038 mp = tcp_ack_mp(tcp); 23039 23040 if (mp != NULL) { 23041 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 23042 BUMP_LOCAL(tcp->tcp_obsegs); 23043 BUMP_MIB(&tcp_mib, tcpOutAck); 23044 BUMP_MIB(&tcp_mib, tcpOutAckDelayed); 23045 tcp_send_data(tcp, tcp->tcp_wq, mp); 23046 } 23047 } 23048 23049 23050 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23051 static mblk_t * 23052 tcp_ack_mp(tcp_t *tcp) 23053 { 23054 uint32_t seq_no; 23055 23056 /* 23057 * There are a few cases to be considered while setting the sequence no. 23058 * Essentially, we can come here while processing an unacceptable pkt 23059 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23060 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23061 * If we are here for a zero window probe, stick with suna. In all 23062 * other cases, we check if suna + swnd encompasses snxt and set 23063 * the sequence number to snxt, if so. If snxt falls outside the 23064 * window (the receiver probably shrunk its window), we will go with 23065 * suna + swnd, otherwise the sequence no will be unacceptable to the 23066 * receiver. 23067 */ 23068 if (tcp->tcp_zero_win_probe) { 23069 seq_no = tcp->tcp_suna; 23070 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23071 ASSERT(tcp->tcp_swnd == 0); 23072 seq_no = tcp->tcp_snxt; 23073 } else { 23074 seq_no = SEQ_GT(tcp->tcp_snxt, 23075 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23076 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23077 } 23078 23079 if (tcp->tcp_valid_bits) { 23080 /* 23081 * For the complex case where we have to send some 23082 * controls (FIN or SYN), let tcp_xmit_mp do it. 23083 */ 23084 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23085 NULL, B_FALSE)); 23086 } else { 23087 /* Generate a simple ACK */ 23088 int data_length; 23089 uchar_t *rptr; 23090 tcph_t *tcph; 23091 mblk_t *mp1; 23092 int32_t tcp_hdr_len; 23093 int32_t tcp_tcp_hdr_len; 23094 int32_t num_sack_blk = 0; 23095 int32_t sack_opt_len; 23096 23097 /* 23098 * Allocate space for TCP + IP headers 23099 * and link-level header 23100 */ 23101 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23102 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23103 tcp->tcp_num_sack_blk); 23104 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23105 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23106 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23107 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23108 } else { 23109 tcp_hdr_len = tcp->tcp_hdr_len; 23110 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23111 } 23112 mp1 = allocb(tcp_hdr_len + tcp_wroff_xtra, BPRI_MED); 23113 if (!mp1) 23114 return (NULL); 23115 23116 /* Update the latest receive window size in TCP header. */ 23117 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23118 tcp->tcp_tcph->th_win); 23119 /* copy in prototype TCP + IP header */ 23120 rptr = mp1->b_rptr + tcp_wroff_xtra; 23121 mp1->b_rptr = rptr; 23122 mp1->b_wptr = rptr + tcp_hdr_len; 23123 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23124 23125 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23126 23127 /* Set the TCP sequence number. */ 23128 U32_TO_ABE32(seq_no, tcph->th_seq); 23129 23130 /* Set up the TCP flag field. */ 23131 tcph->th_flags[0] = (uchar_t)TH_ACK; 23132 if (tcp->tcp_ecn_echo_on) 23133 tcph->th_flags[0] |= TH_ECE; 23134 23135 tcp->tcp_rack = tcp->tcp_rnxt; 23136 tcp->tcp_rack_cnt = 0; 23137 23138 /* fill in timestamp option if in use */ 23139 if (tcp->tcp_snd_ts_ok) { 23140 uint32_t llbolt = (uint32_t)lbolt; 23141 23142 U32_TO_BE32(llbolt, 23143 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23144 U32_TO_BE32(tcp->tcp_ts_recent, 23145 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23146 } 23147 23148 /* Fill in SACK options */ 23149 if (num_sack_blk > 0) { 23150 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23151 sack_blk_t *tmp; 23152 int32_t i; 23153 23154 wptr[0] = TCPOPT_NOP; 23155 wptr[1] = TCPOPT_NOP; 23156 wptr[2] = TCPOPT_SACK; 23157 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23158 sizeof (sack_blk_t); 23159 wptr += TCPOPT_REAL_SACK_LEN; 23160 23161 tmp = tcp->tcp_sack_list; 23162 for (i = 0; i < num_sack_blk; i++) { 23163 U32_TO_BE32(tmp[i].begin, wptr); 23164 wptr += sizeof (tcp_seq); 23165 U32_TO_BE32(tmp[i].end, wptr); 23166 wptr += sizeof (tcp_seq); 23167 } 23168 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23169 << 4); 23170 } 23171 23172 if (tcp->tcp_ipversion == IPV4_VERSION) { 23173 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23174 } else { 23175 /* Check for ip6i_t header in sticky hdrs */ 23176 ip6_t *ip6 = (ip6_t *)(rptr + 23177 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23178 sizeof (ip6i_t) : 0)); 23179 23180 ip6->ip6_plen = htons(tcp_hdr_len - 23181 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23182 } 23183 23184 /* 23185 * Prime pump for checksum calculation in IP. Include the 23186 * adjustment for a source route if any. 23187 */ 23188 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23189 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23190 U16_TO_ABE16(data_length, tcph->th_sum); 23191 23192 if (tcp->tcp_ip_forward_progress) { 23193 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23194 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23195 tcp->tcp_ip_forward_progress = B_FALSE; 23196 } 23197 return (mp1); 23198 } 23199 } 23200 23201 /* 23202 * To create a temporary tcp structure for inserting into bind hash list. 23203 * The parameter is assumed to be in network byte order, ready for use. 23204 */ 23205 /* ARGSUSED */ 23206 static tcp_t * 23207 tcp_alloc_temp_tcp(in_port_t port) 23208 { 23209 conn_t *connp; 23210 tcp_t *tcp; 23211 23212 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP); 23213 if (connp == NULL) 23214 return (NULL); 23215 23216 tcp = connp->conn_tcp; 23217 23218 /* 23219 * Only initialize the necessary info in those structures. Note 23220 * that since INADDR_ANY is all 0, we do not need to set 23221 * tcp_bound_source to INADDR_ANY here. 23222 */ 23223 tcp->tcp_state = TCPS_BOUND; 23224 tcp->tcp_lport = port; 23225 tcp->tcp_exclbind = 1; 23226 tcp->tcp_reserved_port = 1; 23227 23228 /* Just for place holding... */ 23229 tcp->tcp_ipversion = IPV4_VERSION; 23230 23231 return (tcp); 23232 } 23233 23234 /* 23235 * To remove a port range specified by lo_port and hi_port from the 23236 * reserved port ranges. This is one of the three public functions of 23237 * the reserved port interface. Note that a port range has to be removed 23238 * as a whole. Ports in a range cannot be removed individually. 23239 * 23240 * Params: 23241 * in_port_t lo_port: the beginning port of the reserved port range to 23242 * be deleted. 23243 * in_port_t hi_port: the ending port of the reserved port range to 23244 * be deleted. 23245 * 23246 * Return: 23247 * B_TRUE if the deletion is successful, B_FALSE otherwise. 23248 */ 23249 boolean_t 23250 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 23251 { 23252 int i, j; 23253 int size; 23254 tcp_t **temp_tcp_array; 23255 tcp_t *tcp; 23256 23257 rw_enter(&tcp_reserved_port_lock, RW_WRITER); 23258 23259 /* First make sure that the port ranage is indeed reserved. */ 23260 for (i = 0; i < tcp_reserved_port_array_size; i++) { 23261 if (tcp_reserved_port[i].lo_port == lo_port) { 23262 hi_port = tcp_reserved_port[i].hi_port; 23263 temp_tcp_array = tcp_reserved_port[i].temp_tcp_array; 23264 break; 23265 } 23266 } 23267 if (i == tcp_reserved_port_array_size) { 23268 rw_exit(&tcp_reserved_port_lock); 23269 return (B_FALSE); 23270 } 23271 23272 /* 23273 * Remove the range from the array. This simple loop is possible 23274 * because port ranges are inserted in ascending order. 23275 */ 23276 for (j = i; j < tcp_reserved_port_array_size - 1; j++) { 23277 tcp_reserved_port[j].lo_port = tcp_reserved_port[j+1].lo_port; 23278 tcp_reserved_port[j].hi_port = tcp_reserved_port[j+1].hi_port; 23279 tcp_reserved_port[j].temp_tcp_array = 23280 tcp_reserved_port[j+1].temp_tcp_array; 23281 } 23282 23283 /* Remove all the temporary tcp structures. */ 23284 size = hi_port - lo_port + 1; 23285 while (size > 0) { 23286 tcp = temp_tcp_array[size - 1]; 23287 ASSERT(tcp != NULL); 23288 tcp_bind_hash_remove(tcp); 23289 CONN_DEC_REF(tcp->tcp_connp); 23290 size--; 23291 } 23292 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 23293 tcp_reserved_port_array_size--; 23294 rw_exit(&tcp_reserved_port_lock); 23295 return (B_TRUE); 23296 } 23297 23298 /* 23299 * Macro to remove temporary tcp structure from the bind hash list. The 23300 * first parameter is the list of tcp to be removed. The second parameter 23301 * is the number of tcps in the array. 23302 */ 23303 #define TCP_TMP_TCP_REMOVE(tcp_array, num) \ 23304 { \ 23305 while ((num) > 0) { \ 23306 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 23307 tf_t *tbf; \ 23308 tcp_t *tcpnext; \ 23309 tbf = &tcp_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 23310 mutex_enter(&tbf->tf_lock); \ 23311 tcpnext = tcp->tcp_bind_hash; \ 23312 if (tcpnext) { \ 23313 tcpnext->tcp_ptpbhn = \ 23314 tcp->tcp_ptpbhn; \ 23315 } \ 23316 *tcp->tcp_ptpbhn = tcpnext; \ 23317 mutex_exit(&tbf->tf_lock); \ 23318 kmem_free(tcp, sizeof (tcp_t)); \ 23319 (tcp_array)[(num) - 1] = NULL; \ 23320 (num)--; \ 23321 } \ 23322 } 23323 23324 /* 23325 * The public interface for other modules to call to reserve a port range 23326 * in TCP. The caller passes in how large a port range it wants. TCP 23327 * will try to find a range and return it via lo_port and hi_port. This is 23328 * used by NCA's nca_conn_init. 23329 * NCA can only be used in the global zone so this only affects the global 23330 * zone's ports. 23331 * 23332 * Params: 23333 * int size: the size of the port range to be reserved. 23334 * in_port_t *lo_port (referenced): returns the beginning port of the 23335 * reserved port range added. 23336 * in_port_t *hi_port (referenced): returns the ending port of the 23337 * reserved port range added. 23338 * 23339 * Return: 23340 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 23341 */ 23342 boolean_t 23343 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 23344 { 23345 tcp_t *tcp; 23346 tcp_t *tmp_tcp; 23347 tcp_t **temp_tcp_array; 23348 tf_t *tbf; 23349 in_port_t net_port; 23350 in_port_t port; 23351 int32_t cur_size; 23352 int i, j; 23353 boolean_t used; 23354 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 23355 zoneid_t zoneid = GLOBAL_ZONEID; 23356 23357 /* Sanity check. */ 23358 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 23359 return (B_FALSE); 23360 } 23361 23362 rw_enter(&tcp_reserved_port_lock, RW_WRITER); 23363 if (tcp_reserved_port_array_size == TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 23364 rw_exit(&tcp_reserved_port_lock); 23365 return (B_FALSE); 23366 } 23367 23368 /* 23369 * Find the starting port to try. Since the port ranges are ordered 23370 * in the reserved port array, we can do a simple search here. 23371 */ 23372 *lo_port = TCP_SMALLEST_RESERVED_PORT; 23373 *hi_port = TCP_LARGEST_RESERVED_PORT; 23374 for (i = 0; i < tcp_reserved_port_array_size; 23375 *lo_port = tcp_reserved_port[i].hi_port + 1, i++) { 23376 if (tcp_reserved_port[i].lo_port - *lo_port >= size) { 23377 *hi_port = tcp_reserved_port[i].lo_port - 1; 23378 break; 23379 } 23380 } 23381 /* No available port range. */ 23382 if (i == tcp_reserved_port_array_size && *hi_port - *lo_port < size) { 23383 rw_exit(&tcp_reserved_port_lock); 23384 return (B_FALSE); 23385 } 23386 23387 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 23388 if (temp_tcp_array == NULL) { 23389 rw_exit(&tcp_reserved_port_lock); 23390 return (B_FALSE); 23391 } 23392 23393 /* Go thru the port range to see if some ports are already bound. */ 23394 for (port = *lo_port, cur_size = 0; 23395 cur_size < size && port <= *hi_port; 23396 cur_size++, port++) { 23397 used = B_FALSE; 23398 net_port = htons(port); 23399 tbf = &tcp_bind_fanout[TCP_BIND_HASH(net_port)]; 23400 mutex_enter(&tbf->tf_lock); 23401 for (tcp = tbf->tf_tcp; tcp != NULL; 23402 tcp = tcp->tcp_bind_hash) { 23403 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 23404 net_port == tcp->tcp_lport) { 23405 /* 23406 * A port is already bound. Search again 23407 * starting from port + 1. Release all 23408 * temporary tcps. 23409 */ 23410 mutex_exit(&tbf->tf_lock); 23411 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 23412 *lo_port = port + 1; 23413 cur_size = -1; 23414 used = B_TRUE; 23415 break; 23416 } 23417 } 23418 if (!used) { 23419 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port)) == NULL) { 23420 /* 23421 * Allocation failure. Just fail the request. 23422 * Need to remove all those temporary tcp 23423 * structures. 23424 */ 23425 mutex_exit(&tbf->tf_lock); 23426 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 23427 rw_exit(&tcp_reserved_port_lock); 23428 kmem_free(temp_tcp_array, 23429 (hi_port - lo_port + 1) * 23430 sizeof (tcp_t *)); 23431 return (B_FALSE); 23432 } 23433 temp_tcp_array[cur_size] = tmp_tcp; 23434 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 23435 mutex_exit(&tbf->tf_lock); 23436 } 23437 } 23438 23439 /* 23440 * The current range is not large enough. We can actually do another 23441 * search if this search is done between 2 reserved port ranges. But 23442 * for first release, we just stop here and return saying that no port 23443 * range is available. 23444 */ 23445 if (cur_size < size) { 23446 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 23447 rw_exit(&tcp_reserved_port_lock); 23448 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 23449 return (B_FALSE); 23450 } 23451 *hi_port = port - 1; 23452 23453 /* 23454 * Insert range into array in ascending order. Since this function 23455 * must not be called often, we choose to use the simplest method. 23456 * The above array should not consume excessive stack space as 23457 * the size must be very small. If in future releases, we find 23458 * that we should provide more reserved port ranges, this function 23459 * has to be modified to be more efficient. 23460 */ 23461 if (tcp_reserved_port_array_size == 0) { 23462 tcp_reserved_port[0].lo_port = *lo_port; 23463 tcp_reserved_port[0].hi_port = *hi_port; 23464 tcp_reserved_port[0].temp_tcp_array = temp_tcp_array; 23465 } else { 23466 for (i = 0, j = 0; i < tcp_reserved_port_array_size; i++, j++) { 23467 if (*lo_port < tcp_reserved_port[i].lo_port && i == j) { 23468 tmp_ports[j].lo_port = *lo_port; 23469 tmp_ports[j].hi_port = *hi_port; 23470 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23471 j++; 23472 } 23473 tmp_ports[j].lo_port = tcp_reserved_port[i].lo_port; 23474 tmp_ports[j].hi_port = tcp_reserved_port[i].hi_port; 23475 tmp_ports[j].temp_tcp_array = 23476 tcp_reserved_port[i].temp_tcp_array; 23477 } 23478 if (j == i) { 23479 tmp_ports[j].lo_port = *lo_port; 23480 tmp_ports[j].hi_port = *hi_port; 23481 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23482 } 23483 bcopy(tmp_ports, tcp_reserved_port, sizeof (tmp_ports)); 23484 } 23485 tcp_reserved_port_array_size++; 23486 rw_exit(&tcp_reserved_port_lock); 23487 return (B_TRUE); 23488 } 23489 23490 /* 23491 * Check to see if a port is in any reserved port range. 23492 * 23493 * Params: 23494 * in_port_t port: the port to be verified. 23495 * 23496 * Return: 23497 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 23498 */ 23499 boolean_t 23500 tcp_reserved_port_check(in_port_t port) 23501 { 23502 int i; 23503 23504 rw_enter(&tcp_reserved_port_lock, RW_READER); 23505 for (i = 0; i < tcp_reserved_port_array_size; i++) { 23506 if (port >= tcp_reserved_port[i].lo_port || 23507 port <= tcp_reserved_port[i].hi_port) { 23508 rw_exit(&tcp_reserved_port_lock); 23509 return (B_TRUE); 23510 } 23511 } 23512 rw_exit(&tcp_reserved_port_lock); 23513 return (B_FALSE); 23514 } 23515 23516 /* 23517 * To list all reserved port ranges. This is the function to handle 23518 * ndd tcp_reserved_port_list. 23519 */ 23520 /* ARGSUSED */ 23521 static int 23522 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23523 { 23524 int i; 23525 23526 rw_enter(&tcp_reserved_port_lock, RW_READER); 23527 if (tcp_reserved_port_array_size > 0) 23528 (void) mi_mpprintf(mp, "The following ports are reserved:"); 23529 else 23530 (void) mi_mpprintf(mp, "No port is reserved."); 23531 for (i = 0; i < tcp_reserved_port_array_size; i++) { 23532 (void) mi_mpprintf(mp, "%d-%d", 23533 tcp_reserved_port[i].lo_port, tcp_reserved_port[i].hi_port); 23534 } 23535 rw_exit(&tcp_reserved_port_lock); 23536 return (0); 23537 } 23538 23539 /* 23540 * Hash list insertion routine for tcp_t structures. 23541 * Inserts entries with the ones bound to a specific IP address first 23542 * followed by those bound to INADDR_ANY. 23543 */ 23544 static void 23545 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23546 { 23547 tcp_t **tcpp; 23548 tcp_t *tcpnext; 23549 23550 if (tcp->tcp_ptpbhn != NULL) { 23551 ASSERT(!caller_holds_lock); 23552 tcp_bind_hash_remove(tcp); 23553 } 23554 tcpp = &tbf->tf_tcp; 23555 if (!caller_holds_lock) { 23556 mutex_enter(&tbf->tf_lock); 23557 } else { 23558 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23559 } 23560 tcpnext = tcpp[0]; 23561 if (tcpnext) { 23562 /* 23563 * If the new tcp bound to the INADDR_ANY address 23564 * and the first one in the list is not bound to 23565 * INADDR_ANY we skip all entries until we find the 23566 * first one bound to INADDR_ANY. 23567 * This makes sure that applications binding to a 23568 * specific address get preference over those binding to 23569 * INADDR_ANY. 23570 */ 23571 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23572 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23573 while ((tcpnext = tcpp[0]) != NULL && 23574 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23575 tcpp = &(tcpnext->tcp_bind_hash); 23576 if (tcpnext) 23577 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23578 } else 23579 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23580 } 23581 tcp->tcp_bind_hash = tcpnext; 23582 tcp->tcp_ptpbhn = tcpp; 23583 tcpp[0] = tcp; 23584 if (!caller_holds_lock) 23585 mutex_exit(&tbf->tf_lock); 23586 } 23587 23588 /* 23589 * Hash list removal routine for tcp_t structures. 23590 */ 23591 static void 23592 tcp_bind_hash_remove(tcp_t *tcp) 23593 { 23594 tcp_t *tcpnext; 23595 kmutex_t *lockp; 23596 23597 if (tcp->tcp_ptpbhn == NULL) 23598 return; 23599 23600 /* 23601 * Extract the lock pointer in case there are concurrent 23602 * hash_remove's for this instance. 23603 */ 23604 ASSERT(tcp->tcp_lport != 0); 23605 lockp = &tcp_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23606 23607 ASSERT(lockp != NULL); 23608 mutex_enter(lockp); 23609 if (tcp->tcp_ptpbhn) { 23610 tcpnext = tcp->tcp_bind_hash; 23611 if (tcpnext) { 23612 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23613 tcp->tcp_bind_hash = NULL; 23614 } 23615 *tcp->tcp_ptpbhn = tcpnext; 23616 tcp->tcp_ptpbhn = NULL; 23617 } 23618 mutex_exit(lockp); 23619 } 23620 23621 23622 /* 23623 * Hash list lookup routine for tcp_t structures. 23624 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23625 */ 23626 static tcp_t * 23627 tcp_acceptor_hash_lookup(t_uscalar_t id) 23628 { 23629 tf_t *tf; 23630 tcp_t *tcp; 23631 23632 tf = &tcp_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23633 mutex_enter(&tf->tf_lock); 23634 for (tcp = tf->tf_tcp; tcp != NULL; 23635 tcp = tcp->tcp_acceptor_hash) { 23636 if (tcp->tcp_acceptor_id == id) { 23637 CONN_INC_REF(tcp->tcp_connp); 23638 mutex_exit(&tf->tf_lock); 23639 return (tcp); 23640 } 23641 } 23642 mutex_exit(&tf->tf_lock); 23643 return (NULL); 23644 } 23645 23646 23647 /* 23648 * Hash list insertion routine for tcp_t structures. 23649 */ 23650 void 23651 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23652 { 23653 tf_t *tf; 23654 tcp_t **tcpp; 23655 tcp_t *tcpnext; 23656 23657 tf = &tcp_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23658 23659 if (tcp->tcp_ptpahn != NULL) 23660 tcp_acceptor_hash_remove(tcp); 23661 tcpp = &tf->tf_tcp; 23662 mutex_enter(&tf->tf_lock); 23663 tcpnext = tcpp[0]; 23664 if (tcpnext) 23665 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23666 tcp->tcp_acceptor_hash = tcpnext; 23667 tcp->tcp_ptpahn = tcpp; 23668 tcpp[0] = tcp; 23669 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23670 mutex_exit(&tf->tf_lock); 23671 } 23672 23673 /* 23674 * Hash list removal routine for tcp_t structures. 23675 */ 23676 static void 23677 tcp_acceptor_hash_remove(tcp_t *tcp) 23678 { 23679 tcp_t *tcpnext; 23680 kmutex_t *lockp; 23681 23682 /* 23683 * Extract the lock pointer in case there are concurrent 23684 * hash_remove's for this instance. 23685 */ 23686 lockp = tcp->tcp_acceptor_lockp; 23687 23688 if (tcp->tcp_ptpahn == NULL) 23689 return; 23690 23691 ASSERT(lockp != NULL); 23692 mutex_enter(lockp); 23693 if (tcp->tcp_ptpahn) { 23694 tcpnext = tcp->tcp_acceptor_hash; 23695 if (tcpnext) { 23696 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 23697 tcp->tcp_acceptor_hash = NULL; 23698 } 23699 *tcp->tcp_ptpahn = tcpnext; 23700 tcp->tcp_ptpahn = NULL; 23701 } 23702 mutex_exit(lockp); 23703 tcp->tcp_acceptor_lockp = NULL; 23704 } 23705 23706 /* ARGSUSED */ 23707 static int 23708 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 23709 { 23710 int error = 0; 23711 int retval; 23712 char *end; 23713 23714 tcp_hsp_t *hsp; 23715 tcp_hsp_t *hspprev; 23716 23717 ipaddr_t addr = 0; /* Address we're looking for */ 23718 in6_addr_t v6addr; /* Address we're looking for */ 23719 uint32_t hash; /* Hash of that address */ 23720 23721 /* 23722 * If the following variables are still zero after parsing the input 23723 * string, the user didn't specify them and we don't change them in 23724 * the HSP. 23725 */ 23726 23727 ipaddr_t mask = 0; /* Subnet mask */ 23728 in6_addr_t v6mask; 23729 long sendspace = 0; /* Send buffer size */ 23730 long recvspace = 0; /* Receive buffer size */ 23731 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 23732 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 23733 23734 rw_enter(&tcp_hsp_lock, RW_WRITER); 23735 23736 /* Parse and validate address */ 23737 if (af == AF_INET) { 23738 retval = inet_pton(af, value, &addr); 23739 if (retval == 1) 23740 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 23741 } else if (af == AF_INET6) { 23742 retval = inet_pton(af, value, &v6addr); 23743 } else { 23744 error = EINVAL; 23745 goto done; 23746 } 23747 if (retval == 0) { 23748 error = EINVAL; 23749 goto done; 23750 } 23751 23752 while ((*value) && *value != ' ') 23753 value++; 23754 23755 /* Parse individual keywords, set variables if found */ 23756 while (*value) { 23757 /* Skip leading blanks */ 23758 23759 while (*value == ' ' || *value == '\t') 23760 value++; 23761 23762 /* If at end of string, we're done */ 23763 23764 if (!*value) 23765 break; 23766 23767 /* We have a word, figure out what it is */ 23768 23769 if (strncmp("mask", value, 4) == 0) { 23770 value += 4; 23771 while (*value == ' ' || *value == '\t') 23772 value++; 23773 /* Parse subnet mask */ 23774 if (af == AF_INET) { 23775 retval = inet_pton(af, value, &mask); 23776 if (retval == 1) { 23777 V4MASK_TO_V6(mask, v6mask); 23778 } 23779 } else if (af == AF_INET6) { 23780 retval = inet_pton(af, value, &v6mask); 23781 } 23782 if (retval != 1) { 23783 error = EINVAL; 23784 goto done; 23785 } 23786 while ((*value) && *value != ' ') 23787 value++; 23788 } else if (strncmp("sendspace", value, 9) == 0) { 23789 value += 9; 23790 23791 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 23792 sendspace < TCP_XMIT_HIWATER || 23793 sendspace >= (1L<<30)) { 23794 error = EINVAL; 23795 goto done; 23796 } 23797 value = end; 23798 } else if (strncmp("recvspace", value, 9) == 0) { 23799 value += 9; 23800 23801 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 23802 recvspace < TCP_RECV_HIWATER || 23803 recvspace >= (1L<<30)) { 23804 error = EINVAL; 23805 goto done; 23806 } 23807 value = end; 23808 } else if (strncmp("timestamp", value, 9) == 0) { 23809 value += 9; 23810 23811 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 23812 timestamp < 0 || timestamp > 1) { 23813 error = EINVAL; 23814 goto done; 23815 } 23816 23817 /* 23818 * We increment timestamp so we know it's been set; 23819 * this is undone when we put it in the HSP 23820 */ 23821 timestamp++; 23822 value = end; 23823 } else if (strncmp("delete", value, 6) == 0) { 23824 value += 6; 23825 delete = B_TRUE; 23826 } else { 23827 error = EINVAL; 23828 goto done; 23829 } 23830 } 23831 23832 /* Hash address for lookup */ 23833 23834 hash = TCP_HSP_HASH(addr); 23835 23836 if (delete) { 23837 /* 23838 * Note that deletes don't return an error if the thing 23839 * we're trying to delete isn't there. 23840 */ 23841 if (tcp_hsp_hash == NULL) 23842 goto done; 23843 hsp = tcp_hsp_hash[hash]; 23844 23845 if (hsp) { 23846 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 23847 &v6addr)) { 23848 tcp_hsp_hash[hash] = hsp->tcp_hsp_next; 23849 mi_free((char *)hsp); 23850 } else { 23851 hspprev = hsp; 23852 while ((hsp = hsp->tcp_hsp_next) != NULL) { 23853 if (IN6_ARE_ADDR_EQUAL( 23854 &hsp->tcp_hsp_addr_v6, &v6addr)) { 23855 hspprev->tcp_hsp_next = 23856 hsp->tcp_hsp_next; 23857 mi_free((char *)hsp); 23858 break; 23859 } 23860 hspprev = hsp; 23861 } 23862 } 23863 } 23864 } else { 23865 /* 23866 * We're adding/modifying an HSP. If we haven't already done 23867 * so, allocate the hash table. 23868 */ 23869 23870 if (!tcp_hsp_hash) { 23871 tcp_hsp_hash = (tcp_hsp_t **) 23872 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 23873 if (!tcp_hsp_hash) { 23874 error = EINVAL; 23875 goto done; 23876 } 23877 } 23878 23879 /* Get head of hash chain */ 23880 23881 hsp = tcp_hsp_hash[hash]; 23882 23883 /* Try to find pre-existing hsp on hash chain */ 23884 /* Doesn't handle CIDR prefixes. */ 23885 while (hsp) { 23886 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 23887 break; 23888 hsp = hsp->tcp_hsp_next; 23889 } 23890 23891 /* 23892 * If we didn't, create one with default values and put it 23893 * at head of hash chain 23894 */ 23895 23896 if (!hsp) { 23897 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 23898 if (!hsp) { 23899 error = EINVAL; 23900 goto done; 23901 } 23902 hsp->tcp_hsp_next = tcp_hsp_hash[hash]; 23903 tcp_hsp_hash[hash] = hsp; 23904 } 23905 23906 /* Set values that the user asked us to change */ 23907 23908 hsp->tcp_hsp_addr_v6 = v6addr; 23909 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 23910 hsp->tcp_hsp_vers = IPV4_VERSION; 23911 else 23912 hsp->tcp_hsp_vers = IPV6_VERSION; 23913 hsp->tcp_hsp_subnet_v6 = v6mask; 23914 if (sendspace > 0) 23915 hsp->tcp_hsp_sendspace = sendspace; 23916 if (recvspace > 0) 23917 hsp->tcp_hsp_recvspace = recvspace; 23918 if (timestamp > 0) 23919 hsp->tcp_hsp_tstamp = timestamp - 1; 23920 } 23921 23922 done: 23923 rw_exit(&tcp_hsp_lock); 23924 return (error); 23925 } 23926 23927 /* Set callback routine passed to nd_load by tcp_param_register. */ 23928 /* ARGSUSED */ 23929 static int 23930 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 23931 { 23932 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 23933 } 23934 /* ARGSUSED */ 23935 static int 23936 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23937 cred_t *cr) 23938 { 23939 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 23940 } 23941 23942 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 23943 /* ARGSUSED */ 23944 static int 23945 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23946 { 23947 tcp_hsp_t *hsp; 23948 int i; 23949 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 23950 23951 rw_enter(&tcp_hsp_lock, RW_READER); 23952 (void) mi_mpprintf(mp, 23953 "Hash HSP " MI_COL_HDRPAD_STR 23954 "Address Subnet Mask Send Receive TStamp"); 23955 if (tcp_hsp_hash) { 23956 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 23957 hsp = tcp_hsp_hash[i]; 23958 while (hsp) { 23959 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 23960 (void) inet_ntop(AF_INET, 23961 &hsp->tcp_hsp_addr, 23962 addrbuf, sizeof (addrbuf)); 23963 (void) inet_ntop(AF_INET, 23964 &hsp->tcp_hsp_subnet, 23965 subnetbuf, sizeof (subnetbuf)); 23966 } else { 23967 (void) inet_ntop(AF_INET6, 23968 &hsp->tcp_hsp_addr_v6, 23969 addrbuf, sizeof (addrbuf)); 23970 (void) inet_ntop(AF_INET6, 23971 &hsp->tcp_hsp_subnet_v6, 23972 subnetbuf, sizeof (subnetbuf)); 23973 } 23974 (void) mi_mpprintf(mp, 23975 " %03d " MI_COL_PTRFMT_STR 23976 "%s %s %010d %010d %d", 23977 i, 23978 (void *)hsp, 23979 addrbuf, 23980 subnetbuf, 23981 hsp->tcp_hsp_sendspace, 23982 hsp->tcp_hsp_recvspace, 23983 hsp->tcp_hsp_tstamp); 23984 23985 hsp = hsp->tcp_hsp_next; 23986 } 23987 } 23988 } 23989 rw_exit(&tcp_hsp_lock); 23990 return (0); 23991 } 23992 23993 23994 /* Data for fast netmask macro used by tcp_hsp_lookup */ 23995 23996 static ipaddr_t netmasks[] = { 23997 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 23998 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 23999 }; 24000 24001 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24002 24003 /* 24004 * XXX This routine should go away and instead we should use the metrics 24005 * associated with the routes to determine the default sndspace and rcvspace. 24006 */ 24007 static tcp_hsp_t * 24008 tcp_hsp_lookup(ipaddr_t addr) 24009 { 24010 tcp_hsp_t *hsp = NULL; 24011 24012 /* Quick check without acquiring the lock. */ 24013 if (tcp_hsp_hash == NULL) 24014 return (NULL); 24015 24016 rw_enter(&tcp_hsp_lock, RW_READER); 24017 24018 /* This routine finds the best-matching HSP for address addr. */ 24019 24020 if (tcp_hsp_hash) { 24021 int i; 24022 ipaddr_t srchaddr; 24023 tcp_hsp_t *hsp_net; 24024 24025 /* We do three passes: host, network, and subnet. */ 24026 24027 srchaddr = addr; 24028 24029 for (i = 1; i <= 3; i++) { 24030 /* Look for exact match on srchaddr */ 24031 24032 hsp = tcp_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24033 while (hsp) { 24034 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24035 hsp->tcp_hsp_addr == srchaddr) 24036 break; 24037 hsp = hsp->tcp_hsp_next; 24038 } 24039 ASSERT(hsp == NULL || 24040 hsp->tcp_hsp_vers == IPV4_VERSION); 24041 24042 /* 24043 * If this is the first pass: 24044 * If we found a match, great, return it. 24045 * If not, search for the network on the second pass. 24046 */ 24047 24048 if (i == 1) 24049 if (hsp) 24050 break; 24051 else 24052 { 24053 srchaddr = addr & netmask(addr); 24054 continue; 24055 } 24056 24057 /* 24058 * If this is the second pass: 24059 * If we found a match, but there's a subnet mask, 24060 * save the match but try again using the subnet 24061 * mask on the third pass. 24062 * Otherwise, return whatever we found. 24063 */ 24064 24065 if (i == 2) { 24066 if (hsp && hsp->tcp_hsp_subnet) { 24067 hsp_net = hsp; 24068 srchaddr = addr & hsp->tcp_hsp_subnet; 24069 continue; 24070 } else { 24071 break; 24072 } 24073 } 24074 24075 /* 24076 * This must be the third pass. If we didn't find 24077 * anything, return the saved network HSP instead. 24078 */ 24079 24080 if (!hsp) 24081 hsp = hsp_net; 24082 } 24083 } 24084 24085 rw_exit(&tcp_hsp_lock); 24086 return (hsp); 24087 } 24088 24089 /* 24090 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24091 * match lookup. 24092 */ 24093 static tcp_hsp_t * 24094 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr) 24095 { 24096 tcp_hsp_t *hsp = NULL; 24097 24098 /* Quick check without acquiring the lock. */ 24099 if (tcp_hsp_hash == NULL) 24100 return (NULL); 24101 24102 rw_enter(&tcp_hsp_lock, RW_READER); 24103 24104 /* This routine finds the best-matching HSP for address addr. */ 24105 24106 if (tcp_hsp_hash) { 24107 int i; 24108 in6_addr_t v6srchaddr; 24109 tcp_hsp_t *hsp_net; 24110 24111 /* We do three passes: host, network, and subnet. */ 24112 24113 v6srchaddr = *v6addr; 24114 24115 for (i = 1; i <= 3; i++) { 24116 /* Look for exact match on srchaddr */ 24117 24118 hsp = tcp_hsp_hash[TCP_HSP_HASH( 24119 V4_PART_OF_V6(v6srchaddr))]; 24120 while (hsp) { 24121 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24122 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24123 &v6srchaddr)) 24124 break; 24125 hsp = hsp->tcp_hsp_next; 24126 } 24127 24128 /* 24129 * If this is the first pass: 24130 * If we found a match, great, return it. 24131 * If not, search for the network on the second pass. 24132 */ 24133 24134 if (i == 1) 24135 if (hsp) 24136 break; 24137 else { 24138 /* Assume a 64 bit mask */ 24139 v6srchaddr.s6_addr32[0] = 24140 v6addr->s6_addr32[0]; 24141 v6srchaddr.s6_addr32[1] = 24142 v6addr->s6_addr32[1]; 24143 v6srchaddr.s6_addr32[2] = 0; 24144 v6srchaddr.s6_addr32[3] = 0; 24145 continue; 24146 } 24147 24148 /* 24149 * If this is the second pass: 24150 * If we found a match, but there's a subnet mask, 24151 * save the match but try again using the subnet 24152 * mask on the third pass. 24153 * Otherwise, return whatever we found. 24154 */ 24155 24156 if (i == 2) { 24157 ASSERT(hsp == NULL || 24158 hsp->tcp_hsp_vers == IPV6_VERSION); 24159 if (hsp && 24160 !IN6_IS_ADDR_UNSPECIFIED( 24161 &hsp->tcp_hsp_subnet_v6)) { 24162 hsp_net = hsp; 24163 V6_MASK_COPY(*v6addr, 24164 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24165 continue; 24166 } else { 24167 break; 24168 } 24169 } 24170 24171 /* 24172 * This must be the third pass. If we didn't find 24173 * anything, return the saved network HSP instead. 24174 */ 24175 24176 if (!hsp) 24177 hsp = hsp_net; 24178 } 24179 } 24180 24181 rw_exit(&tcp_hsp_lock); 24182 return (hsp); 24183 } 24184 24185 /* 24186 * Type three generator adapted from the random() function in 4.4 BSD: 24187 */ 24188 24189 /* 24190 * Copyright (c) 1983, 1993 24191 * The Regents of the University of California. All rights reserved. 24192 * 24193 * Redistribution and use in source and binary forms, with or without 24194 * modification, are permitted provided that the following conditions 24195 * are met: 24196 * 1. Redistributions of source code must retain the above copyright 24197 * notice, this list of conditions and the following disclaimer. 24198 * 2. Redistributions in binary form must reproduce the above copyright 24199 * notice, this list of conditions and the following disclaimer in the 24200 * documentation and/or other materials provided with the distribution. 24201 * 3. All advertising materials mentioning features or use of this software 24202 * must display the following acknowledgement: 24203 * This product includes software developed by the University of 24204 * California, Berkeley and its contributors. 24205 * 4. Neither the name of the University nor the names of its contributors 24206 * may be used to endorse or promote products derived from this software 24207 * without specific prior written permission. 24208 * 24209 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24210 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24211 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24212 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24213 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24214 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24215 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24216 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24217 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24218 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24219 * SUCH DAMAGE. 24220 */ 24221 24222 /* Type 3 -- x**31 + x**3 + 1 */ 24223 #define DEG_3 31 24224 #define SEP_3 3 24225 24226 24227 /* Protected by tcp_random_lock */ 24228 static int tcp_randtbl[DEG_3 + 1]; 24229 24230 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24231 static int *tcp_random_rptr = &tcp_randtbl[1]; 24232 24233 static int *tcp_random_state = &tcp_randtbl[1]; 24234 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24235 24236 kmutex_t tcp_random_lock; 24237 24238 void 24239 tcp_random_init(void) 24240 { 24241 int i; 24242 hrtime_t hrt; 24243 time_t wallclock; 24244 uint64_t result; 24245 24246 /* 24247 * Use high-res timer and current time for seed. Gethrtime() returns 24248 * a longlong, which may contain resolution down to nanoseconds. 24249 * The current time will either be a 32-bit or a 64-bit quantity. 24250 * XOR the two together in a 64-bit result variable. 24251 * Convert the result to a 32-bit value by multiplying the high-order 24252 * 32-bits by the low-order 32-bits. 24253 */ 24254 24255 hrt = gethrtime(); 24256 (void) drv_getparm(TIME, &wallclock); 24257 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24258 mutex_enter(&tcp_random_lock); 24259 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24260 (result & 0xffffffff); 24261 24262 for (i = 1; i < DEG_3; i++) 24263 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24264 + 12345; 24265 tcp_random_fptr = &tcp_random_state[SEP_3]; 24266 tcp_random_rptr = &tcp_random_state[0]; 24267 mutex_exit(&tcp_random_lock); 24268 for (i = 0; i < 10 * DEG_3; i++) 24269 (void) tcp_random(); 24270 } 24271 24272 /* 24273 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24274 * This range is selected to be approximately centered on TCP_ISS / 2, 24275 * and easy to compute. We get this value by generating a 32-bit random 24276 * number, selecting out the high-order 17 bits, and then adding one so 24277 * that we never return zero. 24278 */ 24279 int 24280 tcp_random(void) 24281 { 24282 int i; 24283 24284 mutex_enter(&tcp_random_lock); 24285 *tcp_random_fptr += *tcp_random_rptr; 24286 24287 /* 24288 * The high-order bits are more random than the low-order bits, 24289 * so we select out the high-order 17 bits and add one so that 24290 * we never return zero. 24291 */ 24292 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24293 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24294 tcp_random_fptr = tcp_random_state; 24295 ++tcp_random_rptr; 24296 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24297 tcp_random_rptr = tcp_random_state; 24298 24299 mutex_exit(&tcp_random_lock); 24300 return (i); 24301 } 24302 24303 /* 24304 * XXX This will go away when TPI is extended to send 24305 * info reqs to sockfs/timod ..... 24306 * Given a queue, set the max packet size for the write 24307 * side of the queue below stream head. This value is 24308 * cached on the stream head. 24309 * Returns 1 on success, 0 otherwise. 24310 */ 24311 static int 24312 setmaxps(queue_t *q, int maxpsz) 24313 { 24314 struct stdata *stp; 24315 queue_t *wq; 24316 stp = STREAM(q); 24317 24318 /* 24319 * At this point change of a queue parameter is not allowed 24320 * when a multiplexor is sitting on top. 24321 */ 24322 if (stp->sd_flag & STPLEX) 24323 return (0); 24324 24325 claimstr(stp->sd_wrq); 24326 wq = stp->sd_wrq->q_next; 24327 ASSERT(wq != NULL); 24328 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24329 releasestr(stp->sd_wrq); 24330 return (1); 24331 } 24332 24333 static int 24334 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24335 int *t_errorp, int *sys_errorp) 24336 { 24337 int error; 24338 int is_absreq_failure; 24339 t_scalar_t *opt_lenp; 24340 t_scalar_t opt_offset; 24341 int prim_type; 24342 struct T_conn_req *tcreqp; 24343 struct T_conn_res *tcresp; 24344 cred_t *cr; 24345 24346 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24347 24348 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24349 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24350 prim_type == T_CONN_RES); 24351 24352 switch (prim_type) { 24353 case T_CONN_REQ: 24354 tcreqp = (struct T_conn_req *)mp->b_rptr; 24355 opt_offset = tcreqp->OPT_offset; 24356 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24357 break; 24358 case O_T_CONN_RES: 24359 case T_CONN_RES: 24360 tcresp = (struct T_conn_res *)mp->b_rptr; 24361 opt_offset = tcresp->OPT_offset; 24362 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24363 break; 24364 } 24365 24366 *t_errorp = 0; 24367 *sys_errorp = 0; 24368 *do_disconnectp = 0; 24369 24370 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24371 opt_offset, cr, &tcp_opt_obj, 24372 NULL, &is_absreq_failure); 24373 24374 switch (error) { 24375 case 0: /* no error */ 24376 ASSERT(is_absreq_failure == 0); 24377 return (0); 24378 case ENOPROTOOPT: 24379 *t_errorp = TBADOPT; 24380 break; 24381 case EACCES: 24382 *t_errorp = TACCES; 24383 break; 24384 default: 24385 *t_errorp = TSYSERR; *sys_errorp = error; 24386 break; 24387 } 24388 if (is_absreq_failure != 0) { 24389 /* 24390 * The connection request should get the local ack 24391 * T_OK_ACK and then a T_DISCON_IND. 24392 */ 24393 *do_disconnectp = 1; 24394 } 24395 return (-1); 24396 } 24397 24398 /* 24399 * Split this function out so that if the secret changes, I'm okay. 24400 * 24401 * Initialize the tcp_iss_cookie and tcp_iss_key. 24402 */ 24403 24404 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24405 24406 static void 24407 tcp_iss_key_init(uint8_t *phrase, int len) 24408 { 24409 struct { 24410 int32_t current_time; 24411 uint32_t randnum; 24412 uint16_t pad; 24413 uint8_t ether[6]; 24414 uint8_t passwd[PASSWD_SIZE]; 24415 } tcp_iss_cookie; 24416 time_t t; 24417 24418 /* 24419 * Start with the current absolute time. 24420 */ 24421 (void) drv_getparm(TIME, &t); 24422 tcp_iss_cookie.current_time = t; 24423 24424 /* 24425 * XXX - Need a more random number per RFC 1750, not this crap. 24426 * OTOH, if what follows is pretty random, then I'm in better shape. 24427 */ 24428 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24429 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24430 24431 /* 24432 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24433 * as a good template. 24434 */ 24435 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24436 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24437 24438 /* 24439 * The pass-phrase. Normally this is supplied by user-called NDD. 24440 */ 24441 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24442 24443 /* 24444 * See 4010593 if this section becomes a problem again, 24445 * but the local ethernet address is useful here. 24446 */ 24447 (void) localetheraddr(NULL, 24448 (struct ether_addr *)&tcp_iss_cookie.ether); 24449 24450 /* 24451 * Hash 'em all together. The MD5Final is called per-connection. 24452 */ 24453 mutex_enter(&tcp_iss_key_lock); 24454 MD5Init(&tcp_iss_key); 24455 MD5Update(&tcp_iss_key, (uchar_t *)&tcp_iss_cookie, 24456 sizeof (tcp_iss_cookie)); 24457 mutex_exit(&tcp_iss_key_lock); 24458 } 24459 24460 /* 24461 * Set the RFC 1948 pass phrase 24462 */ 24463 /* ARGSUSED */ 24464 static int 24465 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24466 cred_t *cr) 24467 { 24468 /* 24469 * Basically, value contains a new pass phrase. Pass it along! 24470 */ 24471 tcp_iss_key_init((uint8_t *)value, strlen(value)); 24472 return (0); 24473 } 24474 24475 /* ARGSUSED */ 24476 static int 24477 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24478 { 24479 bzero(buf, sizeof (tcp_sack_info_t)); 24480 return (0); 24481 } 24482 24483 /* ARGSUSED */ 24484 static int 24485 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24486 { 24487 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24488 return (0); 24489 } 24490 24491 void 24492 tcp_ddi_init(void) 24493 { 24494 int i; 24495 24496 /* Initialize locks */ 24497 rw_init(&tcp_hsp_lock, NULL, RW_DEFAULT, NULL); 24498 mutex_init(&tcp_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24499 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24500 mutex_init(&tcp_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24501 mutex_init(&tcp_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24502 rw_init(&tcp_reserved_port_lock, NULL, RW_DEFAULT, NULL); 24503 24504 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 24505 mutex_init(&tcp_bind_fanout[i].tf_lock, NULL, 24506 MUTEX_DEFAULT, NULL); 24507 } 24508 24509 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 24510 mutex_init(&tcp_acceptor_fanout[i].tf_lock, NULL, 24511 MUTEX_DEFAULT, NULL); 24512 } 24513 24514 /* TCP's IPsec code calls the packet dropper. */ 24515 ip_drop_register(&tcp_dropper, "TCP IPsec policy enforcement"); 24516 24517 if (!tcp_g_nd) { 24518 if (!tcp_param_register(tcp_param_arr, A_CNT(tcp_param_arr))) { 24519 nd_free(&tcp_g_nd); 24520 } 24521 } 24522 24523 /* 24524 * Note: To really walk the device tree you need the devinfo 24525 * pointer to your device which is only available after probe/attach. 24526 * The following is safe only because it uses ddi_root_node() 24527 */ 24528 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24529 tcp_opt_obj.odb_opt_arr_cnt); 24530 24531 tcp_timercache = kmem_cache_create("tcp_timercache", 24532 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24533 NULL, NULL, NULL, NULL, NULL, 0); 24534 24535 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24536 sizeof (tcp_sack_info_t), 0, 24537 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24538 24539 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24540 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24541 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24542 24543 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 24544 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 24545 24546 ip_squeue_init(tcp_squeue_add); 24547 24548 /* Initialize the random number generator */ 24549 tcp_random_init(); 24550 24551 /* 24552 * Initialize RFC 1948 secret values. This will probably be reset once 24553 * by the boot scripts. 24554 * 24555 * Use NULL name, as the name is caught by the new lockstats. 24556 * 24557 * Initialize with some random, non-guessable string, like the global 24558 * T_INFO_ACK. 24559 */ 24560 24561 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24562 sizeof (tcp_g_t_info_ack)); 24563 24564 if ((tcp_kstat = kstat_create(TCP_MOD_NAME, 0, "tcpstat", 24565 "net", KSTAT_TYPE_NAMED, 24566 sizeof (tcp_statistics) / sizeof (kstat_named_t), 24567 KSTAT_FLAG_VIRTUAL)) != NULL) { 24568 tcp_kstat->ks_data = &tcp_statistics; 24569 kstat_install(tcp_kstat); 24570 } 24571 24572 tcp_kstat_init(); 24573 } 24574 24575 void 24576 tcp_ddi_destroy(void) 24577 { 24578 int i; 24579 24580 nd_free(&tcp_g_nd); 24581 24582 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 24583 mutex_destroy(&tcp_bind_fanout[i].tf_lock); 24584 } 24585 24586 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 24587 mutex_destroy(&tcp_acceptor_fanout[i].tf_lock); 24588 } 24589 24590 mutex_destroy(&tcp_iss_key_lock); 24591 rw_destroy(&tcp_hsp_lock); 24592 mutex_destroy(&tcp_g_q_lock); 24593 mutex_destroy(&tcp_random_lock); 24594 mutex_destroy(&tcp_epriv_port_lock); 24595 rw_destroy(&tcp_reserved_port_lock); 24596 24597 ip_drop_unregister(&tcp_dropper); 24598 24599 kmem_cache_destroy(tcp_timercache); 24600 kmem_cache_destroy(tcp_sack_info_cache); 24601 kmem_cache_destroy(tcp_iphc_cache); 24602 24603 tcp_kstat_fini(); 24604 } 24605 24606 /* 24607 * Generate ISS, taking into account NDD changes may happen halfway through. 24608 * (If the iss is not zero, set it.) 24609 */ 24610 24611 static void 24612 tcp_iss_init(tcp_t *tcp) 24613 { 24614 MD5_CTX context; 24615 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 24616 uint32_t answer[4]; 24617 24618 tcp_iss_incr_extra += (ISS_INCR >> 1); 24619 tcp->tcp_iss = tcp_iss_incr_extra; 24620 switch (tcp_strong_iss) { 24621 case 2: 24622 mutex_enter(&tcp_iss_key_lock); 24623 context = tcp_iss_key; 24624 mutex_exit(&tcp_iss_key_lock); 24625 arg.ports = tcp->tcp_ports; 24626 if (tcp->tcp_ipversion == IPV4_VERSION) { 24627 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 24628 &arg.src); 24629 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 24630 &arg.dst); 24631 } else { 24632 arg.src = tcp->tcp_ip6h->ip6_src; 24633 arg.dst = tcp->tcp_ip6h->ip6_dst; 24634 } 24635 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 24636 MD5Final((uchar_t *)answer, &context); 24637 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 24638 /* 24639 * Now that we've hashed into a unique per-connection sequence 24640 * space, add a random increment per strong_iss == 1. So I 24641 * guess we'll have to... 24642 */ 24643 /* FALLTHRU */ 24644 case 1: 24645 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 24646 break; 24647 default: 24648 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24649 break; 24650 } 24651 tcp->tcp_valid_bits = TCP_ISS_VALID; 24652 tcp->tcp_fss = tcp->tcp_iss - 1; 24653 tcp->tcp_suna = tcp->tcp_iss; 24654 tcp->tcp_snxt = tcp->tcp_iss + 1; 24655 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 24656 tcp->tcp_csuna = tcp->tcp_snxt; 24657 } 24658 24659 /* 24660 * Exported routine for extracting active tcp connection status. 24661 * 24662 * This is used by the Solaris Cluster Networking software to 24663 * gather a list of connections that need to be forwarded to 24664 * specific nodes in the cluster when configuration changes occur. 24665 * 24666 * The callback is invoked for each tcp_t structure. Returning 24667 * non-zero from the callback routine terminates the search. 24668 */ 24669 int 24670 cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg) 24671 { 24672 tcp_t *tcp; 24673 cl_tcp_info_t cl_tcpi; 24674 connf_t *connfp; 24675 conn_t *connp; 24676 int i; 24677 24678 ASSERT(callback != NULL); 24679 24680 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24681 24682 connfp = &ipcl_globalhash_fanout[i]; 24683 connp = NULL; 24684 24685 while ((connp = 24686 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24687 24688 tcp = connp->conn_tcp; 24689 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 24690 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 24691 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 24692 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 24693 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 24694 /* 24695 * The macros tcp_laddr and tcp_faddr give the IPv4 24696 * addresses. They are copied implicitly below as 24697 * mapped addresses. 24698 */ 24699 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 24700 if (tcp->tcp_ipversion == IPV4_VERSION) { 24701 cl_tcpi.cl_tcpi_faddr = 24702 tcp->tcp_ipha->ipha_dst; 24703 } else { 24704 cl_tcpi.cl_tcpi_faddr_v6 = 24705 tcp->tcp_ip6h->ip6_dst; 24706 } 24707 24708 /* 24709 * If the callback returns non-zero 24710 * we terminate the traversal. 24711 */ 24712 if ((*callback)(&cl_tcpi, arg) != 0) { 24713 CONN_DEC_REF(tcp->tcp_connp); 24714 return (1); 24715 } 24716 } 24717 } 24718 24719 return (0); 24720 } 24721 24722 /* 24723 * Macros used for accessing the different types of sockaddr 24724 * structures inside a tcp_ioc_abort_conn_t. 24725 */ 24726 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 24727 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 24728 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 24729 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 24730 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 24731 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 24732 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 24733 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 24734 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 24735 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 24736 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 24737 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 24738 24739 /* 24740 * Return the correct error code to mimic the behavior 24741 * of a connection reset. 24742 */ 24743 #define TCP_AC_GET_ERRCODE(state, err) { \ 24744 switch ((state)) { \ 24745 case TCPS_SYN_SENT: \ 24746 case TCPS_SYN_RCVD: \ 24747 (err) = ECONNREFUSED; \ 24748 break; \ 24749 case TCPS_ESTABLISHED: \ 24750 case TCPS_FIN_WAIT_1: \ 24751 case TCPS_FIN_WAIT_2: \ 24752 case TCPS_CLOSE_WAIT: \ 24753 (err) = ECONNRESET; \ 24754 break; \ 24755 case TCPS_CLOSING: \ 24756 case TCPS_LAST_ACK: \ 24757 case TCPS_TIME_WAIT: \ 24758 (err) = 0; \ 24759 break; \ 24760 default: \ 24761 (err) = ENXIO; \ 24762 } \ 24763 } 24764 24765 /* 24766 * Check if a tcp structure matches the info in acp. 24767 */ 24768 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 24769 (((acp)->ac_local.ss_family == AF_INET) ? \ 24770 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 24771 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 24772 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 24773 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 24774 (TCP_AC_V4LPORT((acp)) == 0 || \ 24775 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 24776 (TCP_AC_V4RPORT((acp)) == 0 || \ 24777 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24778 (acp)->ac_start <= (tcp)->tcp_state && \ 24779 (acp)->ac_end >= (tcp)->tcp_state) : \ 24780 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24781 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24782 &(tcp)->tcp_ip_src_v6)) && \ 24783 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24784 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24785 &(tcp)->tcp_remote_v6)) && \ 24786 (TCP_AC_V6LPORT((acp)) == 0 || \ 24787 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24788 (TCP_AC_V6RPORT((acp)) == 0 || \ 24789 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24790 (acp)->ac_start <= (tcp)->tcp_state && \ 24791 (acp)->ac_end >= (tcp)->tcp_state)) 24792 24793 #define TCP_AC_MATCH(acp, tcp) \ 24794 (((acp)->ac_zoneid == ALL_ZONES || \ 24795 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24796 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24797 24798 /* 24799 * Build a message containing a tcp_ioc_abort_conn_t structure 24800 * which is filled in with information from acp and tp. 24801 */ 24802 static mblk_t * 24803 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24804 { 24805 mblk_t *mp; 24806 tcp_ioc_abort_conn_t *tacp; 24807 24808 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24809 if (mp == NULL) 24810 return (NULL); 24811 24812 mp->b_datap->db_type = M_CTL; 24813 24814 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24815 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24816 sizeof (uint32_t)); 24817 24818 tacp->ac_start = acp->ac_start; 24819 tacp->ac_end = acp->ac_end; 24820 tacp->ac_zoneid = acp->ac_zoneid; 24821 24822 if (acp->ac_local.ss_family == AF_INET) { 24823 tacp->ac_local.ss_family = AF_INET; 24824 tacp->ac_remote.ss_family = AF_INET; 24825 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24826 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24827 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24828 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24829 } else { 24830 tacp->ac_local.ss_family = AF_INET6; 24831 tacp->ac_remote.ss_family = AF_INET6; 24832 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24833 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24834 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24835 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24836 } 24837 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24838 return (mp); 24839 } 24840 24841 /* 24842 * Print a tcp_ioc_abort_conn_t structure. 24843 */ 24844 static void 24845 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24846 { 24847 char lbuf[128]; 24848 char rbuf[128]; 24849 sa_family_t af; 24850 in_port_t lport, rport; 24851 ushort_t logflags; 24852 24853 af = acp->ac_local.ss_family; 24854 24855 if (af == AF_INET) { 24856 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24857 lbuf, 128); 24858 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24859 rbuf, 128); 24860 lport = ntohs(TCP_AC_V4LPORT(acp)); 24861 rport = ntohs(TCP_AC_V4RPORT(acp)); 24862 } else { 24863 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24864 lbuf, 128); 24865 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24866 rbuf, 128); 24867 lport = ntohs(TCP_AC_V6LPORT(acp)); 24868 rport = ntohs(TCP_AC_V6RPORT(acp)); 24869 } 24870 24871 logflags = SL_TRACE | SL_NOTE; 24872 /* 24873 * Don't print this message to the console if the operation was done 24874 * to a non-global zone. 24875 */ 24876 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24877 logflags |= SL_CONSOLE; 24878 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24879 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24880 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24881 acp->ac_start, acp->ac_end); 24882 } 24883 24884 /* 24885 * Called inside tcp_rput when a message built using 24886 * tcp_ioctl_abort_build_msg is put into a queue. 24887 * Note that when we get here there is no wildcard in acp any more. 24888 */ 24889 static void 24890 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24891 { 24892 tcp_ioc_abort_conn_t *acp; 24893 24894 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24895 if (tcp->tcp_state <= acp->ac_end) { 24896 /* 24897 * If we get here, we are already on the correct 24898 * squeue. This ioctl follows the following path 24899 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24900 * ->tcp_ioctl_abort->squeue_fill (if on a 24901 * different squeue) 24902 */ 24903 int errcode; 24904 24905 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24906 (void) tcp_clean_death(tcp, errcode, 26); 24907 } 24908 freemsg(mp); 24909 } 24910 24911 /* 24912 * Abort all matching connections on a hash chain. 24913 */ 24914 static int 24915 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24916 boolean_t exact) 24917 { 24918 int nmatch, err = 0; 24919 tcp_t *tcp; 24920 MBLKP mp, last, listhead = NULL; 24921 conn_t *tconnp; 24922 connf_t *connfp = &ipcl_conn_fanout[index]; 24923 24924 startover: 24925 nmatch = 0; 24926 24927 mutex_enter(&connfp->connf_lock); 24928 for (tconnp = connfp->connf_head; tconnp != NULL; 24929 tconnp = tconnp->conn_next) { 24930 tcp = tconnp->conn_tcp; 24931 if (TCP_AC_MATCH(acp, tcp)) { 24932 CONN_INC_REF(tcp->tcp_connp); 24933 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24934 if (mp == NULL) { 24935 err = ENOMEM; 24936 CONN_DEC_REF(tcp->tcp_connp); 24937 break; 24938 } 24939 mp->b_prev = (mblk_t *)tcp; 24940 24941 if (listhead == NULL) { 24942 listhead = mp; 24943 last = mp; 24944 } else { 24945 last->b_next = mp; 24946 last = mp; 24947 } 24948 nmatch++; 24949 if (exact) 24950 break; 24951 } 24952 24953 /* Avoid holding lock for too long. */ 24954 if (nmatch >= 500) 24955 break; 24956 } 24957 mutex_exit(&connfp->connf_lock); 24958 24959 /* Pass mp into the correct tcp */ 24960 while ((mp = listhead) != NULL) { 24961 listhead = listhead->b_next; 24962 tcp = (tcp_t *)mp->b_prev; 24963 mp->b_next = mp->b_prev = NULL; 24964 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 24965 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 24966 } 24967 24968 *count += nmatch; 24969 if (nmatch >= 500 && err == 0) 24970 goto startover; 24971 return (err); 24972 } 24973 24974 /* 24975 * Abort all connections that matches the attributes specified in acp. 24976 */ 24977 static int 24978 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp) 24979 { 24980 sa_family_t af; 24981 uint32_t ports; 24982 uint16_t *pports; 24983 int err = 0, count = 0; 24984 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24985 int index = -1; 24986 ushort_t logflags; 24987 24988 af = acp->ac_local.ss_family; 24989 24990 if (af == AF_INET) { 24991 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24992 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24993 pports = (uint16_t *)&ports; 24994 pports[1] = TCP_AC_V4LPORT(acp); 24995 pports[0] = TCP_AC_V4RPORT(acp); 24996 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24997 } 24998 } else { 24999 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25000 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25001 pports = (uint16_t *)&ports; 25002 pports[1] = TCP_AC_V6LPORT(acp); 25003 pports[0] = TCP_AC_V6RPORT(acp); 25004 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25005 } 25006 } 25007 25008 /* 25009 * For cases where remote addr, local port, and remote port are non- 25010 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25011 */ 25012 if (index != -1) { 25013 err = tcp_ioctl_abort_bucket(acp, index, 25014 &count, exact); 25015 } else { 25016 /* 25017 * loop through all entries for wildcard case 25018 */ 25019 for (index = 0; index < ipcl_conn_fanout_size; index++) { 25020 err = tcp_ioctl_abort_bucket(acp, index, 25021 &count, exact); 25022 if (err != 0) 25023 break; 25024 } 25025 } 25026 25027 logflags = SL_TRACE | SL_NOTE; 25028 /* 25029 * Don't print this message to the console if the operation was done 25030 * to a non-global zone. 25031 */ 25032 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25033 logflags |= SL_CONSOLE; 25034 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25035 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25036 if (err == 0 && count == 0) 25037 err = ENOENT; 25038 return (err); 25039 } 25040 25041 /* 25042 * Process the TCP_IOC_ABORT_CONN ioctl request. 25043 */ 25044 static void 25045 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25046 { 25047 int err; 25048 IOCP iocp; 25049 MBLKP mp1; 25050 sa_family_t laf, raf; 25051 tcp_ioc_abort_conn_t *acp; 25052 zone_t *zptr; 25053 zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid; 25054 25055 iocp = (IOCP)mp->b_rptr; 25056 25057 if ((mp1 = mp->b_cont) == NULL || 25058 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25059 err = EINVAL; 25060 goto out; 25061 } 25062 25063 /* check permissions */ 25064 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 25065 err = EPERM; 25066 goto out; 25067 } 25068 25069 if (mp1->b_cont != NULL) { 25070 freemsg(mp1->b_cont); 25071 mp1->b_cont = NULL; 25072 } 25073 25074 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25075 laf = acp->ac_local.ss_family; 25076 raf = acp->ac_remote.ss_family; 25077 25078 /* check that a zone with the supplied zoneid exists */ 25079 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25080 zptr = zone_find_by_id(zoneid); 25081 if (zptr != NULL) { 25082 zone_rele(zptr); 25083 } else { 25084 err = EINVAL; 25085 goto out; 25086 } 25087 } 25088 25089 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25090 acp->ac_start > acp->ac_end || laf != raf || 25091 (laf != AF_INET && laf != AF_INET6)) { 25092 err = EINVAL; 25093 goto out; 25094 } 25095 25096 tcp_ioctl_abort_dump(acp); 25097 err = tcp_ioctl_abort(acp); 25098 25099 out: 25100 if (mp1 != NULL) { 25101 freemsg(mp1); 25102 mp->b_cont = NULL; 25103 } 25104 25105 if (err != 0) 25106 miocnak(q, mp, 0, err); 25107 else 25108 miocack(q, mp, 0, 0); 25109 } 25110 25111 /* 25112 * tcp_time_wait_processing() handles processing of incoming packets when 25113 * the tcp is in the TIME_WAIT state. 25114 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25115 * on the time wait list. 25116 */ 25117 void 25118 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25119 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25120 { 25121 int32_t bytes_acked; 25122 int32_t gap; 25123 int32_t rgap; 25124 tcp_opt_t tcpopt; 25125 uint_t flags; 25126 uint32_t new_swnd = 0; 25127 conn_t *connp; 25128 25129 BUMP_LOCAL(tcp->tcp_ibsegs); 25130 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 25131 25132 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25133 new_swnd = BE16_TO_U16(tcph->th_win) << 25134 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25135 if (tcp->tcp_snd_ts_ok) { 25136 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25137 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25138 tcp->tcp_rnxt, TH_ACK); 25139 goto done; 25140 } 25141 } 25142 gap = seg_seq - tcp->tcp_rnxt; 25143 rgap = tcp->tcp_rwnd - (gap + seg_len); 25144 if (gap < 0) { 25145 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 25146 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, 25147 (seg_len > -gap ? -gap : seg_len)); 25148 seg_len += gap; 25149 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25150 if (flags & TH_RST) { 25151 goto done; 25152 } 25153 if ((flags & TH_FIN) && seg_len == -1) { 25154 /* 25155 * When TCP receives a duplicate FIN in 25156 * TIME_WAIT state, restart the 2 MSL timer. 25157 * See page 73 in RFC 793. Make sure this TCP 25158 * is already on the TIME_WAIT list. If not, 25159 * just restart the timer. 25160 */ 25161 if (TCP_IS_DETACHED(tcp)) { 25162 if (tcp_time_wait_remove(tcp, NULL) == 25163 B_TRUE) { 25164 tcp_time_wait_append(tcp); 25165 TCP_DBGSTAT(tcp_rput_time_wait); 25166 } 25167 } else { 25168 ASSERT(tcp != NULL); 25169 TCP_TIMER_RESTART(tcp, 25170 tcp_time_wait_interval); 25171 } 25172 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25173 tcp->tcp_rnxt, TH_ACK); 25174 goto done; 25175 } 25176 flags |= TH_ACK_NEEDED; 25177 seg_len = 0; 25178 goto process_ack; 25179 } 25180 25181 /* Fix seg_seq, and chew the gap off the front. */ 25182 seg_seq = tcp->tcp_rnxt; 25183 } 25184 25185 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25186 /* 25187 * Make sure that when we accept the connection, pick 25188 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25189 * old connection. 25190 * 25191 * The next ISS generated is equal to tcp_iss_incr_extra 25192 * + ISS_INCR/2 + other components depending on the 25193 * value of tcp_strong_iss. We pre-calculate the new 25194 * ISS here and compare with tcp_snxt to determine if 25195 * we need to make adjustment to tcp_iss_incr_extra. 25196 * 25197 * The above calculation is ugly and is a 25198 * waste of CPU cycles... 25199 */ 25200 uint32_t new_iss = tcp_iss_incr_extra; 25201 int32_t adj; 25202 25203 switch (tcp_strong_iss) { 25204 case 2: { 25205 /* Add time and MD5 components. */ 25206 uint32_t answer[4]; 25207 struct { 25208 uint32_t ports; 25209 in6_addr_t src; 25210 in6_addr_t dst; 25211 } arg; 25212 MD5_CTX context; 25213 25214 mutex_enter(&tcp_iss_key_lock); 25215 context = tcp_iss_key; 25216 mutex_exit(&tcp_iss_key_lock); 25217 arg.ports = tcp->tcp_ports; 25218 /* We use MAPPED addresses in tcp_iss_init */ 25219 arg.src = tcp->tcp_ip_src_v6; 25220 if (tcp->tcp_ipversion == IPV4_VERSION) { 25221 IN6_IPADDR_TO_V4MAPPED( 25222 tcp->tcp_ipha->ipha_dst, 25223 &arg.dst); 25224 } else { 25225 arg.dst = 25226 tcp->tcp_ip6h->ip6_dst; 25227 } 25228 MD5Update(&context, (uchar_t *)&arg, 25229 sizeof (arg)); 25230 MD5Final((uchar_t *)answer, &context); 25231 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25232 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25233 break; 25234 } 25235 case 1: 25236 /* Add time component and min random (i.e. 1). */ 25237 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25238 break; 25239 default: 25240 /* Add only time component. */ 25241 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25242 break; 25243 } 25244 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25245 /* 25246 * New ISS not guaranteed to be ISS_INCR/2 25247 * ahead of the current tcp_snxt, so add the 25248 * difference to tcp_iss_incr_extra. 25249 */ 25250 tcp_iss_incr_extra += adj; 25251 } 25252 /* 25253 * If tcp_clean_death() can not perform the task now, 25254 * drop the SYN packet and let the other side re-xmit. 25255 * Otherwise pass the SYN packet back in, since the 25256 * old tcp state has been cleaned up or freed. 25257 */ 25258 if (tcp_clean_death(tcp, 0, 27) == -1) 25259 goto done; 25260 /* 25261 * We will come back to tcp_rput_data 25262 * on the global queue. Packets destined 25263 * for the global queue will be checked 25264 * with global policy. But the policy for 25265 * this packet has already been checked as 25266 * this was destined for the detached 25267 * connection. We need to bypass policy 25268 * check this time by attaching a dummy 25269 * ipsec_in with ipsec_in_dont_check set. 25270 */ 25271 if ((connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid)) != 25272 NULL) { 25273 TCP_STAT(tcp_time_wait_syn_success); 25274 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25275 return; 25276 } 25277 goto done; 25278 } 25279 25280 /* 25281 * rgap is the amount of stuff received out of window. A negative 25282 * value is the amount out of window. 25283 */ 25284 if (rgap < 0) { 25285 BUMP_MIB(&tcp_mib, tcpInDataPastWinSegs); 25286 UPDATE_MIB(&tcp_mib, tcpInDataPastWinBytes, -rgap); 25287 /* Fix seg_len and make sure there is something left. */ 25288 seg_len += rgap; 25289 if (seg_len <= 0) { 25290 if (flags & TH_RST) { 25291 goto done; 25292 } 25293 flags |= TH_ACK_NEEDED; 25294 seg_len = 0; 25295 goto process_ack; 25296 } 25297 } 25298 /* 25299 * Check whether we can update tcp_ts_recent. This test is 25300 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25301 * Extensions for High Performance: An Update", Internet Draft. 25302 */ 25303 if (tcp->tcp_snd_ts_ok && 25304 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25305 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25306 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25307 tcp->tcp_last_rcv_lbolt = lbolt64; 25308 } 25309 25310 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25311 /* Always ack out of order packets */ 25312 flags |= TH_ACK_NEEDED; 25313 seg_len = 0; 25314 } else if (seg_len > 0) { 25315 BUMP_MIB(&tcp_mib, tcpInClosed); 25316 BUMP_MIB(&tcp_mib, tcpInDataInorderSegs); 25317 UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, seg_len); 25318 } 25319 if (flags & TH_RST) { 25320 (void) tcp_clean_death(tcp, 0, 28); 25321 goto done; 25322 } 25323 if (flags & TH_SYN) { 25324 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25325 TH_RST|TH_ACK); 25326 /* 25327 * Do not delete the TCP structure if it is in 25328 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25329 */ 25330 goto done; 25331 } 25332 process_ack: 25333 if (flags & TH_ACK) { 25334 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25335 if (bytes_acked <= 0) { 25336 if (bytes_acked == 0 && seg_len == 0 && 25337 new_swnd == tcp->tcp_swnd) 25338 BUMP_MIB(&tcp_mib, tcpInDupAck); 25339 } else { 25340 /* Acks something not sent */ 25341 flags |= TH_ACK_NEEDED; 25342 } 25343 } 25344 if (flags & TH_ACK_NEEDED) { 25345 /* 25346 * Time to send an ack for some reason. 25347 */ 25348 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25349 tcp->tcp_rnxt, TH_ACK); 25350 } 25351 done: 25352 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25353 DB_CKSUMSTART(mp) = 0; 25354 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25355 TCP_STAT(tcp_time_wait_syn_fail); 25356 } 25357 freemsg(mp); 25358 } 25359 25360 /* 25361 * Allocate a T_SVR4_OPTMGMT_REQ. 25362 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 25363 * that tcp_rput_other can drop the acks. 25364 */ 25365 static mblk_t * 25366 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 25367 { 25368 mblk_t *mp; 25369 struct T_optmgmt_req *tor; 25370 struct opthdr *oh; 25371 uint_t size; 25372 char *optptr; 25373 25374 size = sizeof (*tor) + sizeof (*oh) + optlen; 25375 mp = allocb(size, BPRI_MED); 25376 if (mp == NULL) 25377 return (NULL); 25378 25379 mp->b_wptr += size; 25380 mp->b_datap->db_type = M_PROTO; 25381 tor = (struct T_optmgmt_req *)mp->b_rptr; 25382 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 25383 tor->MGMT_flags = T_NEGOTIATE; 25384 tor->OPT_length = sizeof (*oh) + optlen; 25385 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 25386 25387 oh = (struct opthdr *)&tor[1]; 25388 oh->level = level; 25389 oh->name = cmd; 25390 oh->len = optlen; 25391 if (optlen != 0) { 25392 optptr = (char *)&oh[1]; 25393 bcopy(opt, optptr, optlen); 25394 } 25395 return (mp); 25396 } 25397 25398 /* 25399 * TCP Timers Implementation. 25400 */ 25401 timeout_id_t 25402 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25403 { 25404 mblk_t *mp; 25405 tcp_timer_t *tcpt; 25406 tcp_t *tcp = connp->conn_tcp; 25407 25408 ASSERT(connp->conn_sqp != NULL); 25409 25410 TCP_DBGSTAT(tcp_timeout_calls); 25411 25412 if (tcp->tcp_timercache == NULL) { 25413 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25414 } else { 25415 TCP_DBGSTAT(tcp_timeout_cached_alloc); 25416 mp = tcp->tcp_timercache; 25417 tcp->tcp_timercache = mp->b_next; 25418 mp->b_next = NULL; 25419 ASSERT(mp->b_wptr == NULL); 25420 } 25421 25422 CONN_INC_REF(connp); 25423 tcpt = (tcp_timer_t *)mp->b_rptr; 25424 tcpt->connp = connp; 25425 tcpt->tcpt_proc = f; 25426 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 25427 return ((timeout_id_t)mp); 25428 } 25429 25430 static void 25431 tcp_timer_callback(void *arg) 25432 { 25433 mblk_t *mp = (mblk_t *)arg; 25434 tcp_timer_t *tcpt; 25435 conn_t *connp; 25436 25437 tcpt = (tcp_timer_t *)mp->b_rptr; 25438 connp = tcpt->connp; 25439 squeue_fill(connp->conn_sqp, mp, 25440 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 25441 } 25442 25443 static void 25444 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25445 { 25446 tcp_timer_t *tcpt; 25447 conn_t *connp = (conn_t *)arg; 25448 tcp_t *tcp = connp->conn_tcp; 25449 25450 tcpt = (tcp_timer_t *)mp->b_rptr; 25451 ASSERT(connp == tcpt->connp); 25452 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25453 25454 /* 25455 * If the TCP has reached the closed state, don't proceed any 25456 * further. This TCP logically does not exist on the system. 25457 * tcpt_proc could for example access queues, that have already 25458 * been qprocoff'ed off. Also see comments at the start of tcp_input 25459 */ 25460 if (tcp->tcp_state != TCPS_CLOSED) { 25461 (*tcpt->tcpt_proc)(connp); 25462 } else { 25463 tcp->tcp_timer_tid = 0; 25464 } 25465 tcp_timer_free(connp->conn_tcp, mp); 25466 } 25467 25468 /* 25469 * There is potential race with untimeout and the handler firing at the same 25470 * time. The mblock may be freed by the handler while we are trying to use 25471 * it. But since both should execute on the same squeue, this race should not 25472 * occur. 25473 */ 25474 clock_t 25475 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25476 { 25477 mblk_t *mp = (mblk_t *)id; 25478 tcp_timer_t *tcpt; 25479 clock_t delta; 25480 25481 TCP_DBGSTAT(tcp_timeout_cancel_reqs); 25482 25483 if (mp == NULL) 25484 return (-1); 25485 25486 tcpt = (tcp_timer_t *)mp->b_rptr; 25487 ASSERT(tcpt->connp == connp); 25488 25489 delta = untimeout(tcpt->tcpt_tid); 25490 25491 if (delta >= 0) { 25492 TCP_DBGSTAT(tcp_timeout_canceled); 25493 tcp_timer_free(connp->conn_tcp, mp); 25494 CONN_DEC_REF(connp); 25495 } 25496 25497 return (delta); 25498 } 25499 25500 /* 25501 * Allocate space for the timer event. The allocation looks like mblk, but it is 25502 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 25503 * 25504 * Dealing with failures: If we can't allocate from the timer cache we try 25505 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 25506 * points to b_rptr. 25507 * If we can't allocate anything using allocb_tryhard(), we perform a last 25508 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 25509 * save the actual allocation size in b_datap. 25510 */ 25511 mblk_t * 25512 tcp_timermp_alloc(int kmflags) 25513 { 25514 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 25515 kmflags & ~KM_PANIC); 25516 25517 if (mp != NULL) { 25518 mp->b_next = mp->b_prev = NULL; 25519 mp->b_rptr = (uchar_t *)(&mp[1]); 25520 mp->b_wptr = NULL; 25521 mp->b_datap = NULL; 25522 mp->b_queue = NULL; 25523 } else if (kmflags & KM_PANIC) { 25524 /* 25525 * Failed to allocate memory for the timer. Try allocating from 25526 * dblock caches. 25527 */ 25528 TCP_STAT(tcp_timermp_allocfail); 25529 mp = allocb_tryhard(sizeof (tcp_timer_t)); 25530 if (mp == NULL) { 25531 size_t size = 0; 25532 /* 25533 * Memory is really low. Try tryhard allocation. 25534 */ 25535 TCP_STAT(tcp_timermp_allocdblfail); 25536 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 25537 sizeof (tcp_timer_t), &size, kmflags); 25538 mp->b_rptr = (uchar_t *)(&mp[1]); 25539 mp->b_next = mp->b_prev = NULL; 25540 mp->b_wptr = (uchar_t *)-1; 25541 mp->b_datap = (dblk_t *)size; 25542 mp->b_queue = NULL; 25543 } 25544 ASSERT(mp->b_wptr != NULL); 25545 } 25546 TCP_DBGSTAT(tcp_timermp_alloced); 25547 25548 return (mp); 25549 } 25550 25551 /* 25552 * Free per-tcp timer cache. 25553 * It can only contain entries from tcp_timercache. 25554 */ 25555 void 25556 tcp_timermp_free(tcp_t *tcp) 25557 { 25558 mblk_t *mp; 25559 25560 while ((mp = tcp->tcp_timercache) != NULL) { 25561 ASSERT(mp->b_wptr == NULL); 25562 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 25563 kmem_cache_free(tcp_timercache, mp); 25564 } 25565 } 25566 25567 /* 25568 * Free timer event. Put it on the per-tcp timer cache if there is not too many 25569 * events there already (currently at most two events are cached). 25570 * If the event is not allocated from the timer cache, free it right away. 25571 */ 25572 static void 25573 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 25574 { 25575 mblk_t *mp1 = tcp->tcp_timercache; 25576 25577 if (mp->b_wptr != NULL) { 25578 /* 25579 * This allocation is not from a timer cache, free it right 25580 * away. 25581 */ 25582 if (mp->b_wptr != (uchar_t *)-1) 25583 freeb(mp); 25584 else 25585 kmem_free(mp, (size_t)mp->b_datap); 25586 } else if (mp1 == NULL || mp1->b_next == NULL) { 25587 /* Cache this timer block for future allocations */ 25588 mp->b_rptr = (uchar_t *)(&mp[1]); 25589 mp->b_next = mp1; 25590 tcp->tcp_timercache = mp; 25591 } else { 25592 kmem_cache_free(tcp_timercache, mp); 25593 TCP_DBGSTAT(tcp_timermp_freed); 25594 } 25595 } 25596 25597 /* 25598 * End of TCP Timers implementation. 25599 */ 25600 25601 /* 25602 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 25603 * on the specified backing STREAMS q. Note, the caller may make the 25604 * decision to call based on the tcp_t.tcp_flow_stopped value which 25605 * when check outside the q's lock is only an advisory check ... 25606 */ 25607 25608 void 25609 tcp_setqfull(tcp_t *tcp) 25610 { 25611 queue_t *q = tcp->tcp_wq; 25612 25613 if (!(q->q_flag & QFULL)) { 25614 mutex_enter(QLOCK(q)); 25615 if (!(q->q_flag & QFULL)) { 25616 /* still need to set QFULL */ 25617 q->q_flag |= QFULL; 25618 tcp->tcp_flow_stopped = B_TRUE; 25619 mutex_exit(QLOCK(q)); 25620 TCP_STAT(tcp_flwctl_on); 25621 } else { 25622 mutex_exit(QLOCK(q)); 25623 } 25624 } 25625 } 25626 25627 void 25628 tcp_clrqfull(tcp_t *tcp) 25629 { 25630 queue_t *q = tcp->tcp_wq; 25631 25632 if (q->q_flag & QFULL) { 25633 mutex_enter(QLOCK(q)); 25634 if (q->q_flag & QFULL) { 25635 q->q_flag &= ~QFULL; 25636 tcp->tcp_flow_stopped = B_FALSE; 25637 mutex_exit(QLOCK(q)); 25638 if (q->q_flag & QWANTW) 25639 qbackenable(q, 0); 25640 } else { 25641 mutex_exit(QLOCK(q)); 25642 } 25643 } 25644 } 25645 25646 /* 25647 * TCP Kstats implementation 25648 */ 25649 static void 25650 tcp_kstat_init(void) 25651 { 25652 tcp_named_kstat_t template = { 25653 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 25654 { "rtoMin", KSTAT_DATA_INT32, 0 }, 25655 { "rtoMax", KSTAT_DATA_INT32, 0 }, 25656 { "maxConn", KSTAT_DATA_INT32, 0 }, 25657 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 25658 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 25659 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 25660 { "estabResets", KSTAT_DATA_UINT32, 0 }, 25661 { "currEstab", KSTAT_DATA_UINT32, 0 }, 25662 { "inSegs", KSTAT_DATA_UINT32, 0 }, 25663 { "outSegs", KSTAT_DATA_UINT32, 0 }, 25664 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 25665 { "connTableSize", KSTAT_DATA_INT32, 0 }, 25666 { "outRsts", KSTAT_DATA_UINT32, 0 }, 25667 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 25668 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 25669 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 25670 { "outAck", KSTAT_DATA_UINT32, 0 }, 25671 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 25672 { "outUrg", KSTAT_DATA_UINT32, 0 }, 25673 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 25674 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 25675 { "outControl", KSTAT_DATA_UINT32, 0 }, 25676 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 25677 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 25678 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 25679 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 25680 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 25681 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 25682 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 25683 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 25684 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 25685 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 25686 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 25687 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 25688 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 25689 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 25690 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 25691 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 25692 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 25693 { "inClosed", KSTAT_DATA_UINT32, 0 }, 25694 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 25695 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 25696 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 25697 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 25698 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 25699 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 25700 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 25701 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 25702 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 25703 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 25704 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 25705 { "connTableSize6", KSTAT_DATA_INT32, 0 } 25706 }; 25707 25708 tcp_mibkp = kstat_create(TCP_MOD_NAME, 0, TCP_MOD_NAME, 25709 "mib2", KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0); 25710 25711 if (tcp_mibkp == NULL) 25712 return; 25713 25714 template.rtoAlgorithm.value.ui32 = 4; 25715 template.rtoMin.value.ui32 = tcp_rexmit_interval_min; 25716 template.rtoMax.value.ui32 = tcp_rexmit_interval_max; 25717 template.maxConn.value.i32 = -1; 25718 25719 bcopy(&template, tcp_mibkp->ks_data, sizeof (template)); 25720 25721 tcp_mibkp->ks_update = tcp_kstat_update; 25722 25723 kstat_install(tcp_mibkp); 25724 } 25725 25726 static void 25727 tcp_kstat_fini(void) 25728 { 25729 25730 if (tcp_mibkp != NULL) { 25731 kstat_delete(tcp_mibkp); 25732 tcp_mibkp = NULL; 25733 } 25734 } 25735 25736 static int 25737 tcp_kstat_update(kstat_t *kp, int rw) 25738 { 25739 tcp_named_kstat_t *tcpkp; 25740 tcp_t *tcp; 25741 connf_t *connfp; 25742 conn_t *connp; 25743 int i; 25744 25745 if (!kp || !kp->ks_data) 25746 return (EIO); 25747 25748 if (rw == KSTAT_WRITE) 25749 return (EACCES); 25750 25751 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 25752 25753 tcpkp->currEstab.value.ui32 = 0; 25754 25755 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25756 connfp = &ipcl_globalhash_fanout[i]; 25757 connp = NULL; 25758 while ((connp = 25759 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25760 tcp = connp->conn_tcp; 25761 switch (tcp_snmp_state(tcp)) { 25762 case MIB2_TCP_established: 25763 case MIB2_TCP_closeWait: 25764 tcpkp->currEstab.value.ui32++; 25765 break; 25766 } 25767 } 25768 } 25769 25770 tcpkp->activeOpens.value.ui32 = tcp_mib.tcpActiveOpens; 25771 tcpkp->passiveOpens.value.ui32 = tcp_mib.tcpPassiveOpens; 25772 tcpkp->attemptFails.value.ui32 = tcp_mib.tcpAttemptFails; 25773 tcpkp->estabResets.value.ui32 = tcp_mib.tcpEstabResets; 25774 tcpkp->inSegs.value.ui32 = tcp_mib.tcpInSegs; 25775 tcpkp->outSegs.value.ui32 = tcp_mib.tcpOutSegs; 25776 tcpkp->retransSegs.value.ui32 = tcp_mib.tcpRetransSegs; 25777 tcpkp->connTableSize.value.i32 = tcp_mib.tcpConnTableSize; 25778 tcpkp->outRsts.value.ui32 = tcp_mib.tcpOutRsts; 25779 tcpkp->outDataSegs.value.ui32 = tcp_mib.tcpOutDataSegs; 25780 tcpkp->outDataBytes.value.ui32 = tcp_mib.tcpOutDataBytes; 25781 tcpkp->retransBytes.value.ui32 = tcp_mib.tcpRetransBytes; 25782 tcpkp->outAck.value.ui32 = tcp_mib.tcpOutAck; 25783 tcpkp->outAckDelayed.value.ui32 = tcp_mib.tcpOutAckDelayed; 25784 tcpkp->outUrg.value.ui32 = tcp_mib.tcpOutUrg; 25785 tcpkp->outWinUpdate.value.ui32 = tcp_mib.tcpOutWinUpdate; 25786 tcpkp->outWinProbe.value.ui32 = tcp_mib.tcpOutWinProbe; 25787 tcpkp->outControl.value.ui32 = tcp_mib.tcpOutControl; 25788 tcpkp->outFastRetrans.value.ui32 = tcp_mib.tcpOutFastRetrans; 25789 tcpkp->inAckSegs.value.ui32 = tcp_mib.tcpInAckSegs; 25790 tcpkp->inAckBytes.value.ui32 = tcp_mib.tcpInAckBytes; 25791 tcpkp->inDupAck.value.ui32 = tcp_mib.tcpInDupAck; 25792 tcpkp->inAckUnsent.value.ui32 = tcp_mib.tcpInAckUnsent; 25793 tcpkp->inDataInorderSegs.value.ui32 = tcp_mib.tcpInDataInorderSegs; 25794 tcpkp->inDataInorderBytes.value.ui32 = tcp_mib.tcpInDataInorderBytes; 25795 tcpkp->inDataUnorderSegs.value.ui32 = tcp_mib.tcpInDataUnorderSegs; 25796 tcpkp->inDataUnorderBytes.value.ui32 = tcp_mib.tcpInDataUnorderBytes; 25797 tcpkp->inDataDupSegs.value.ui32 = tcp_mib.tcpInDataDupSegs; 25798 tcpkp->inDataDupBytes.value.ui32 = tcp_mib.tcpInDataDupBytes; 25799 tcpkp->inDataPartDupSegs.value.ui32 = tcp_mib.tcpInDataPartDupSegs; 25800 tcpkp->inDataPartDupBytes.value.ui32 = tcp_mib.tcpInDataPartDupBytes; 25801 tcpkp->inDataPastWinSegs.value.ui32 = tcp_mib.tcpInDataPastWinSegs; 25802 tcpkp->inDataPastWinBytes.value.ui32 = tcp_mib.tcpInDataPastWinBytes; 25803 tcpkp->inWinProbe.value.ui32 = tcp_mib.tcpInWinProbe; 25804 tcpkp->inWinUpdate.value.ui32 = tcp_mib.tcpInWinUpdate; 25805 tcpkp->inClosed.value.ui32 = tcp_mib.tcpInClosed; 25806 tcpkp->rttNoUpdate.value.ui32 = tcp_mib.tcpRttNoUpdate; 25807 tcpkp->rttUpdate.value.ui32 = tcp_mib.tcpRttUpdate; 25808 tcpkp->timRetrans.value.ui32 = tcp_mib.tcpTimRetrans; 25809 tcpkp->timRetransDrop.value.ui32 = tcp_mib.tcpTimRetransDrop; 25810 tcpkp->timKeepalive.value.ui32 = tcp_mib.tcpTimKeepalive; 25811 tcpkp->timKeepaliveProbe.value.ui32 = tcp_mib.tcpTimKeepaliveProbe; 25812 tcpkp->timKeepaliveDrop.value.ui32 = tcp_mib.tcpTimKeepaliveDrop; 25813 tcpkp->listenDrop.value.ui32 = tcp_mib.tcpListenDrop; 25814 tcpkp->listenDropQ0.value.ui32 = tcp_mib.tcpListenDropQ0; 25815 tcpkp->halfOpenDrop.value.ui32 = tcp_mib.tcpHalfOpenDrop; 25816 tcpkp->outSackRetransSegs.value.ui32 = tcp_mib.tcpOutSackRetransSegs; 25817 tcpkp->connTableSize6.value.i32 = tcp_mib.tcp6ConnTableSize; 25818 25819 return (0); 25820 } 25821 25822 void 25823 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25824 { 25825 uint16_t hdr_len; 25826 ipha_t *ipha; 25827 uint8_t *nexthdrp; 25828 tcph_t *tcph; 25829 25830 /* Already has an eager */ 25831 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25832 TCP_STAT(tcp_reinput_syn); 25833 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 25834 connp, SQTAG_TCP_REINPUT_EAGER); 25835 return; 25836 } 25837 25838 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25839 case IPV4_VERSION: 25840 ipha = (ipha_t *)mp->b_rptr; 25841 hdr_len = IPH_HDR_LENGTH(ipha); 25842 break; 25843 case IPV6_VERSION: 25844 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25845 &hdr_len, &nexthdrp)) { 25846 CONN_DEC_REF(connp); 25847 freemsg(mp); 25848 return; 25849 } 25850 break; 25851 } 25852 25853 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25854 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25855 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25856 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25857 } 25858 25859 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 25860 SQTAG_TCP_REINPUT); 25861 } 25862 25863 static squeue_func_t 25864 tcp_squeue_switch(int val) 25865 { 25866 squeue_func_t rval = squeue_fill; 25867 25868 switch (val) { 25869 case 1: 25870 rval = squeue_enter_nodrain; 25871 break; 25872 case 2: 25873 rval = squeue_enter; 25874 break; 25875 default: 25876 break; 25877 } 25878 return (rval); 25879 } 25880 25881 static void 25882 tcp_squeue_add(squeue_t *sqp) 25883 { 25884 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25885 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25886 25887 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25888 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 25889 sqp, TCP_TIME_WAIT_DELAY); 25890 if (tcp_free_list_max_cnt == 0) { 25891 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25892 max_ncpus : boot_max_ncpus); 25893 25894 /* 25895 * Limit number of entries to 1% of availble memory / tcp_ncpus 25896 */ 25897 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25898 (tcp_ncpus * sizeof (tcp_t) * 100); 25899 } 25900 tcp_time_wait->tcp_free_list_cnt = 0; 25901 } 25902