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 }; 422 423 static kstat_t *tcp_kstat; 424 425 /* 426 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 427 * tcp write side. 428 */ 429 #define CALL_IP_WPUT(connp, q, mp) { \ 430 ASSERT(((q)->q_flag & QREADR) == 0); \ 431 TCP_DBGSTAT(tcp_ip_output); \ 432 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 433 } 434 435 /* Macros for timestamp comparisons */ 436 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 437 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 438 439 /* 440 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 441 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 442 * by adding three components: a time component which grows by 1 every 4096 443 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 444 * a per-connection component which grows by 125000 for every new connection; 445 * and an "extra" component that grows by a random amount centered 446 * approximately on 64000. This causes the the ISS generator to cycle every 447 * 4.89 hours if no TCP connections are made, and faster if connections are 448 * made. 449 * 450 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 451 * components: a time component which grows by 250000 every second; and 452 * a per-connection component which grows by 125000 for every new connections. 453 * 454 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 455 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 456 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 457 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 458 * password. 459 */ 460 #define ISS_INCR 250000 461 #define ISS_NSEC_SHT 12 462 463 static uint32_t tcp_iss_incr_extra; /* Incremented for each connection */ 464 static kmutex_t tcp_iss_key_lock; 465 static MD5_CTX tcp_iss_key; 466 static sin_t sin_null; /* Zero address for quick clears */ 467 static sin6_t sin6_null; /* Zero address for quick clears */ 468 469 /* Packet dropper for TCP IPsec policy drops. */ 470 static ipdropper_t tcp_dropper; 471 472 /* 473 * This implementation follows the 4.3BSD interpretation of the urgent 474 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 475 * incompatible changes in protocols like telnet and rlogin. 476 */ 477 #define TCP_OLD_URP_INTERPRETATION 1 478 479 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 480 (TCP_IS_DETACHED(tcp) && \ 481 (!(tcp)->tcp_hard_binding)) 482 483 /* 484 * TCP reassembly macros. We hide starting and ending sequence numbers in 485 * b_next and b_prev of messages on the reassembly queue. The messages are 486 * chained using b_cont. These macros are used in tcp_reass() so we don't 487 * have to see the ugly casts and assignments. 488 */ 489 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 490 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 491 (mblk_t *)(uintptr_t)(u)) 492 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 493 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 494 (mblk_t *)(uintptr_t)(u)) 495 496 /* 497 * Implementation of TCP Timers. 498 * ============================= 499 * 500 * INTERFACE: 501 * 502 * There are two basic functions dealing with tcp timers: 503 * 504 * timeout_id_t tcp_timeout(connp, func, time) 505 * clock_t tcp_timeout_cancel(connp, timeout_id) 506 * TCP_TIMER_RESTART(tcp, intvl) 507 * 508 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 509 * after 'time' ticks passed. The function called by timeout() must adhere to 510 * the same restrictions as a driver soft interrupt handler - it must not sleep 511 * or call other functions that might sleep. The value returned is the opaque 512 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 513 * cancel the request. The call to tcp_timeout() may fail in which case it 514 * returns zero. This is different from the timeout(9F) function which never 515 * fails. 516 * 517 * The call-back function 'func' always receives 'connp' as its single 518 * argument. It is always executed in the squeue corresponding to the tcp 519 * structure. The tcp structure is guaranteed to be present at the time the 520 * call-back is called. 521 * 522 * NOTE: The call-back function 'func' is never called if tcp is in 523 * the TCPS_CLOSED state. 524 * 525 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 526 * request. locks acquired by the call-back routine should not be held across 527 * the call to tcp_timeout_cancel() or a deadlock may result. 528 * 529 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 530 * Otherwise, it returns an integer value greater than or equal to 0. In 531 * particular, if the call-back function is already placed on the squeue, it can 532 * not be canceled. 533 * 534 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 535 * within squeue context corresponding to the tcp instance. Since the 536 * call-back is also called via the same squeue, there are no race 537 * conditions described in untimeout(9F) manual page since all calls are 538 * strictly serialized. 539 * 540 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 541 * stored in tcp_timer_tid and starts a new one using 542 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 543 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 544 * field. 545 * 546 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 547 * call-back may still be called, so it is possible tcp_timer() will be 548 * called several times. This should not be a problem since tcp_timer() 549 * should always check the tcp instance state. 550 * 551 * 552 * IMPLEMENTATION: 553 * 554 * TCP timers are implemented using three-stage process. The call to 555 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 556 * when the timer expires. The tcp_timer_callback() arranges the call of the 557 * tcp_timer_handler() function via squeue corresponding to the tcp 558 * instance. The tcp_timer_handler() calls actual requested timeout call-back 559 * and passes tcp instance as an argument to it. Information is passed between 560 * stages using the tcp_timer_t structure which contains the connp pointer, the 561 * tcp call-back to call and the timeout id returned by the timeout(9F). 562 * 563 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 564 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 565 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 566 * returns the pointer to this mblk. 567 * 568 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 569 * looks like a normal mblk without actual dblk attached to it. 570 * 571 * To optimize performance each tcp instance holds a small cache of timer 572 * mblocks. In the current implementation it caches up to two timer mblocks per 573 * tcp instance. The cache is preserved over tcp frees and is only freed when 574 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 575 * timer processing happens on a corresponding squeue, the cache manipulation 576 * does not require any locks. Experiments show that majority of timer mblocks 577 * allocations are satisfied from the tcp cache and do not involve kmem calls. 578 * 579 * The tcp_timeout() places a refhold on the connp instance which guarantees 580 * that it will be present at the time the call-back function fires. The 581 * tcp_timer_handler() drops the reference after calling the call-back, so the 582 * call-back function does not need to manipulate the references explicitly. 583 */ 584 585 typedef struct tcp_timer_s { 586 conn_t *connp; 587 void (*tcpt_proc)(void *); 588 timeout_id_t tcpt_tid; 589 } tcp_timer_t; 590 591 static kmem_cache_t *tcp_timercache; 592 kmem_cache_t *tcp_sack_info_cache; 593 kmem_cache_t *tcp_iphc_cache; 594 595 /* 596 * For scalability, we must not run a timer for every TCP connection 597 * in TIME_WAIT state. To see why, consider (for time wait interval of 598 * 4 minutes): 599 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 600 * 601 * This list is ordered by time, so you need only delete from the head 602 * until you get to entries which aren't old enough to delete yet. 603 * The list consists of only the detached TIME_WAIT connections. 604 * 605 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 606 * becomes detached TIME_WAIT (either by changing the state and already 607 * being detached or the other way around). This means that the TIME_WAIT 608 * state can be extended (up to doubled) if the connection doesn't become 609 * detached for a long time. 610 * 611 * The list manipulations (including tcp_time_wait_next/prev) 612 * are protected by the tcp_time_wait_lock. The content of the 613 * detached TIME_WAIT connections is protected by the normal perimeters. 614 */ 615 616 typedef struct tcp_squeue_priv_s { 617 kmutex_t tcp_time_wait_lock; 618 /* Protects the next 3 globals */ 619 timeout_id_t tcp_time_wait_tid; 620 tcp_t *tcp_time_wait_head; 621 tcp_t *tcp_time_wait_tail; 622 tcp_t *tcp_free_list; 623 uint_t tcp_free_list_cnt; 624 } tcp_squeue_priv_t; 625 626 /* 627 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 628 * Running it every 5 seconds seems to give the best results. 629 */ 630 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 631 632 /* 633 * To prevent memory hog, limit the number of entries in tcp_free_list 634 * to 1% of available memory / number of cpus 635 */ 636 uint_t tcp_free_list_max_cnt = 0; 637 638 #define TCP_XMIT_LOWATER 4096 639 #define TCP_XMIT_HIWATER 49152 640 #define TCP_RECV_LOWATER 2048 641 #define TCP_RECV_HIWATER 49152 642 643 /* 644 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 645 */ 646 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 647 648 #define TIDUSZ 4096 /* transport interface data unit size */ 649 650 /* 651 * Bind hash list size and has function. It has to be a power of 2 for 652 * hashing. 653 */ 654 #define TCP_BIND_FANOUT_SIZE 512 655 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 656 /* 657 * Size of listen and acceptor hash list. It has to be a power of 2 for 658 * hashing. 659 */ 660 #define TCP_FANOUT_SIZE 256 661 662 #ifdef _ILP32 663 #define TCP_ACCEPTOR_HASH(accid) \ 664 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 665 #else 666 #define TCP_ACCEPTOR_HASH(accid) \ 667 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 668 #endif /* _ILP32 */ 669 670 #define IP_ADDR_CACHE_SIZE 2048 671 #define IP_ADDR_CACHE_HASH(faddr) \ 672 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 673 674 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 675 #define TCP_HSP_HASH_SIZE 256 676 677 #define TCP_HSP_HASH(addr) \ 678 (((addr>>24) ^ (addr >>16) ^ \ 679 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 680 681 /* 682 * TCP options struct returned from tcp_parse_options. 683 */ 684 typedef struct tcp_opt_s { 685 uint32_t tcp_opt_mss; 686 uint32_t tcp_opt_wscale; 687 uint32_t tcp_opt_ts_val; 688 uint32_t tcp_opt_ts_ecr; 689 tcp_t *tcp; 690 } tcp_opt_t; 691 692 /* 693 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 694 */ 695 696 #ifdef _BIG_ENDIAN 697 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 698 (TCPOPT_TSTAMP << 8) | 10) 699 #else 700 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 701 (TCPOPT_NOP << 8) | TCPOPT_NOP) 702 #endif 703 704 /* 705 * Flags returned from tcp_parse_options. 706 */ 707 #define TCP_OPT_MSS_PRESENT 1 708 #define TCP_OPT_WSCALE_PRESENT 2 709 #define TCP_OPT_TSTAMP_PRESENT 4 710 #define TCP_OPT_SACK_OK_PRESENT 8 711 #define TCP_OPT_SACK_PRESENT 16 712 713 /* TCP option length */ 714 #define TCPOPT_NOP_LEN 1 715 #define TCPOPT_MAXSEG_LEN 4 716 #define TCPOPT_WS_LEN 3 717 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 718 #define TCPOPT_TSTAMP_LEN 10 719 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 720 #define TCPOPT_SACK_OK_LEN 2 721 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 722 #define TCPOPT_REAL_SACK_LEN 4 723 #define TCPOPT_MAX_SACK_LEN 36 724 #define TCPOPT_HEADER_LEN 2 725 726 /* TCP cwnd burst factor. */ 727 #define TCP_CWND_INFINITE 65535 728 #define TCP_CWND_SS 3 729 #define TCP_CWND_NORMAL 5 730 731 /* Maximum TCP initial cwin (start/restart). */ 732 #define TCP_MAX_INIT_CWND 8 733 734 /* 735 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 736 * either tcp_slow_start_initial or tcp_slow_start_after idle 737 * depending on the caller. If the upper layer has not used the 738 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 739 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 740 * If the upper layer has changed set the tcp_init_cwnd, just use 741 * it to calculate the tcp_cwnd. 742 */ 743 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 744 { \ 745 if ((tcp)->tcp_init_cwnd == 0) { \ 746 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 747 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 748 } else { \ 749 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 750 } \ 751 tcp->tcp_cwnd_cnt = 0; \ 752 } 753 754 /* TCP Timer control structure */ 755 typedef struct tcpt_s { 756 pfv_t tcpt_pfv; /* The routine we are to call */ 757 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 758 } tcpt_t; 759 760 /* Host Specific Parameter structure */ 761 typedef struct tcp_hsp { 762 struct tcp_hsp *tcp_hsp_next; 763 in6_addr_t tcp_hsp_addr_v6; 764 in6_addr_t tcp_hsp_subnet_v6; 765 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 766 int32_t tcp_hsp_sendspace; 767 int32_t tcp_hsp_recvspace; 768 int32_t tcp_hsp_tstamp; 769 } tcp_hsp_t; 770 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 771 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 772 773 /* 774 * Functions called directly via squeue having a prototype of edesc_t. 775 */ 776 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 777 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 778 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 779 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 780 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 781 void tcp_input(void *arg, mblk_t *mp, void *arg2); 782 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 783 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 784 void tcp_output(void *arg, mblk_t *mp, void *arg2); 785 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 786 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 787 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 788 789 790 /* Prototype for TCP functions */ 791 static void tcp_random_init(void); 792 int tcp_random(void); 793 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 794 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 795 tcp_t *eager); 796 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 797 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 798 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 799 boolean_t user_specified); 800 static void tcp_closei_local(tcp_t *tcp); 801 static void tcp_close_detached(tcp_t *tcp); 802 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 803 mblk_t *idmp, mblk_t **defermp); 804 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 805 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 806 in_port_t dstport, uint_t srcid); 807 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 808 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 809 uint32_t scope_id); 810 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 811 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 812 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 813 static char *tcp_display(tcp_t *tcp, char *, char); 814 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 815 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 816 static void tcp_eager_unlink(tcp_t *tcp); 817 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 818 int unixerr); 819 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 820 int tlierr, int unixerr); 821 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 822 cred_t *cr); 823 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 824 char *value, caddr_t cp, cred_t *cr); 825 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 826 char *value, caddr_t cp, cred_t *cr); 827 static int tcp_tpistate(tcp_t *tcp); 828 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 829 int caller_holds_lock); 830 static void tcp_bind_hash_remove(tcp_t *tcp); 831 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id); 832 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 833 static void tcp_acceptor_hash_remove(tcp_t *tcp); 834 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 835 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 836 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 837 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 838 static int tcp_header_init_ipv4(tcp_t *tcp); 839 static int tcp_header_init_ipv6(tcp_t *tcp); 840 int tcp_init(tcp_t *tcp, queue_t *q); 841 static int tcp_init_values(tcp_t *tcp); 842 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 843 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 844 t_scalar_t addr_length); 845 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 846 static void tcp_ip_notify(tcp_t *tcp); 847 static mblk_t *tcp_ire_mp(mblk_t *mp); 848 static void tcp_iss_init(tcp_t *tcp); 849 static void tcp_keepalive_killer(void *arg); 850 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 851 static void tcp_mss_set(tcp_t *tcp, uint32_t size); 852 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 853 int *do_disconnectp, int *t_errorp, int *sys_errorp); 854 static boolean_t tcp_allow_connopt_set(int level, int name); 855 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 856 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 857 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 858 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 859 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 860 mblk_t *mblk); 861 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 862 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 863 uchar_t *ptr, uint_t len); 864 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 865 static boolean_t tcp_param_register(tcpparam_t *tcppa, int cnt); 866 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 867 caddr_t cp, cred_t *cr); 868 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 869 caddr_t cp, cred_t *cr); 870 static void tcp_iss_key_init(uint8_t *phrase, int len); 871 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 872 caddr_t cp, cred_t *cr); 873 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 874 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 875 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 876 static void tcp_reinit(tcp_t *tcp); 877 static void tcp_reinit_values(tcp_t *tcp); 878 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 879 tcp_t *thisstream, cred_t *cr); 880 881 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 882 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 883 static boolean_t tcp_send_rst_chk(void); 884 static void tcp_ss_rexmit(tcp_t *tcp); 885 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 886 static void tcp_process_options(tcp_t *, tcph_t *); 887 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 888 static void tcp_rsrv(queue_t *q); 889 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 890 static int tcp_snmp_state(tcp_t *tcp); 891 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 892 cred_t *cr); 893 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 894 cred_t *cr); 895 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 896 cred_t *cr); 897 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 898 cred_t *cr); 899 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 900 cred_t *cr); 901 static int tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, 902 caddr_t cp, cred_t *cr); 903 static int tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, 904 caddr_t cp, cred_t *cr); 905 static int tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, 906 cred_t *cr); 907 static void tcp_timer(void *arg); 908 static void tcp_timer_callback(void *); 909 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 910 boolean_t random); 911 static in_port_t tcp_get_next_priv_port(const tcp_t *); 912 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 913 void tcp_wput_accept(queue_t *q, mblk_t *mp); 914 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 915 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 916 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 917 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 918 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 919 const int num_sack_blk, int *usable, uint_t *snxt, 920 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 921 const int mdt_thres); 922 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 923 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 924 const int num_sack_blk, int *usable, uint_t *snxt, 925 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 926 const int mdt_thres); 927 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 928 int num_sack_blk); 929 static void tcp_wsrv(queue_t *q); 930 static int tcp_xmit_end(tcp_t *tcp); 931 static void tcp_ack_timer(void *arg); 932 static mblk_t *tcp_ack_mp(tcp_t *tcp); 933 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 934 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 935 zoneid_t zoneid); 936 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 937 uint32_t ack, int ctl); 938 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr); 939 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr); 940 static int setmaxps(queue_t *q, int maxpsz); 941 static void tcp_set_rto(tcp_t *, time_t); 942 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 943 boolean_t, boolean_t); 944 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 945 boolean_t ipsec_mctl); 946 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 947 char *opt, int optlen); 948 static int tcp_build_hdrs(queue_t *, tcp_t *); 949 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 950 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 951 tcph_t *tcph); 952 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 953 boolean_t tcp_reserved_port_add(int, in_port_t *, in_port_t *); 954 boolean_t tcp_reserved_port_del(in_port_t, in_port_t); 955 boolean_t tcp_reserved_port_check(in_port_t); 956 static tcp_t *tcp_alloc_temp_tcp(in_port_t); 957 static int tcp_reserved_port_list(queue_t *, mblk_t *, caddr_t, cred_t *); 958 static mblk_t *tcp_mdt_info_mp(mblk_t *); 959 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 960 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 961 const boolean_t, const uint32_t, const uint32_t, 962 const uint32_t, const uint32_t); 963 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 964 const uint_t, const uint_t, boolean_t *); 965 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 966 extern mblk_t *tcp_timermp_alloc(int); 967 extern void tcp_timermp_free(tcp_t *); 968 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 969 static void tcp_stop_lingering(tcp_t *tcp); 970 static void tcp_close_linger_timeout(void *arg); 971 void tcp_ddi_init(void); 972 void tcp_ddi_destroy(void); 973 static void tcp_kstat_init(void); 974 static void tcp_kstat_fini(void); 975 static int tcp_kstat_update(kstat_t *kp, int rw); 976 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 977 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 978 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 979 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 980 tcph_t *tcph, mblk_t *idmp); 981 static squeue_func_t tcp_squeue_switch(int); 982 983 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *); 984 static int tcp_close(queue_t *, int); 985 static int tcpclose_accept(queue_t *); 986 static int tcp_modclose(queue_t *); 987 static void tcp_wput_mod(queue_t *, mblk_t *); 988 989 static void tcp_squeue_add(squeue_t *); 990 static boolean_t tcp_zcopy_check(tcp_t *); 991 static void tcp_zcopy_notify(tcp_t *); 992 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 993 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 994 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 995 996 extern void tcp_kssl_input(tcp_t *, mblk_t *); 997 998 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 999 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 1000 1001 /* 1002 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 1003 * 1004 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 1005 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 1006 * (defined in tcp.h) needs to be filled in and passed into the kernel 1007 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 1008 * structure contains the four-tuple of a TCP connection and a range of TCP 1009 * states (specified by ac_start and ac_end). The use of wildcard addresses 1010 * and ports is allowed. Connections with a matching four tuple and a state 1011 * within the specified range will be aborted. The valid states for the 1012 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 1013 * inclusive. 1014 * 1015 * An application which has its connection aborted by this ioctl will receive 1016 * an error that is dependent on the connection state at the time of the abort. 1017 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 1018 * though a RST packet has been received. If the connection state is equal to 1019 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 1020 * and all resources associated with the connection will be freed. 1021 */ 1022 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 1023 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 1024 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 1025 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *); 1026 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 1027 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 1028 boolean_t); 1029 1030 static struct module_info tcp_rinfo = { 1031 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 1032 }; 1033 1034 static struct module_info tcp_winfo = { 1035 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 1036 }; 1037 1038 /* 1039 * Entry points for TCP as a module. It only allows SNMP requests 1040 * to pass through. 1041 */ 1042 struct qinit tcp_mod_rinit = { 1043 (pfi_t)putnext, NULL, tcp_open, ip_snmpmod_close, NULL, &tcp_rinfo, 1044 }; 1045 1046 struct qinit tcp_mod_winit = { 1047 (pfi_t)ip_snmpmod_wput, NULL, tcp_open, ip_snmpmod_close, NULL, 1048 &tcp_rinfo 1049 }; 1050 1051 /* 1052 * Entry points for TCP as a device. The normal case which supports 1053 * the TCP functionality. 1054 */ 1055 struct qinit tcp_rinit = { 1056 NULL, (pfi_t)tcp_rsrv, tcp_open, tcp_close, NULL, &tcp_rinfo 1057 }; 1058 1059 struct qinit tcp_winit = { 1060 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1061 }; 1062 1063 /* Initial entry point for TCP in socket mode. */ 1064 struct qinit tcp_sock_winit = { 1065 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1066 }; 1067 1068 /* 1069 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 1070 * an accept. Avoid allocating data structures since eager has already 1071 * been created. 1072 */ 1073 struct qinit tcp_acceptor_rinit = { 1074 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1075 }; 1076 1077 struct qinit tcp_acceptor_winit = { 1078 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1079 }; 1080 1081 /* 1082 * Entry points for TCP loopback (read side only) 1083 */ 1084 struct qinit tcp_loopback_rinit = { 1085 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_open, tcp_close, (pfi_t)0, 1086 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1087 }; 1088 1089 struct streamtab tcpinfo = { 1090 &tcp_rinit, &tcp_winit 1091 }; 1092 1093 extern squeue_func_t tcp_squeue_wput_proc; 1094 extern squeue_func_t tcp_squeue_timer_proc; 1095 1096 /* Protected by tcp_g_q_lock */ 1097 static queue_t *tcp_g_q; /* Default queue used during detached closes */ 1098 kmutex_t tcp_g_q_lock; 1099 1100 /* Protected by tcp_hsp_lock */ 1101 /* 1102 * XXX The host param mechanism should go away and instead we should use 1103 * the metrics associated with the routes to determine the default sndspace 1104 * and rcvspace. 1105 */ 1106 static tcp_hsp_t **tcp_hsp_hash; /* Hash table for HSPs */ 1107 krwlock_t tcp_hsp_lock; 1108 1109 /* 1110 * Extra privileged ports. In host byte order. 1111 * Protected by tcp_epriv_port_lock. 1112 */ 1113 #define TCP_NUM_EPRIV_PORTS 64 1114 static int tcp_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 1115 static uint16_t tcp_g_epriv_ports[TCP_NUM_EPRIV_PORTS] = { 2049, 4045 }; 1116 kmutex_t tcp_epriv_port_lock; 1117 1118 /* 1119 * The smallest anonymous port in the privileged port range which TCP 1120 * looks for free port. Use in the option TCP_ANONPRIVBIND. 1121 */ 1122 static in_port_t tcp_min_anonpriv_port = 512; 1123 1124 /* Only modified during _init and _fini thus no locking is needed. */ 1125 static caddr_t tcp_g_nd; /* Head of 'named dispatch' variable list */ 1126 1127 /* Hint not protected by any lock */ 1128 static uint_t tcp_next_port_to_try; 1129 1130 1131 /* TCP bind hash list - all tcp_t with state >= BOUND. */ 1132 tf_t tcp_bind_fanout[TCP_BIND_FANOUT_SIZE]; 1133 1134 /* TCP queue hash list - all tcp_t in case they will be an acceptor. */ 1135 static tf_t tcp_acceptor_fanout[TCP_FANOUT_SIZE]; 1136 1137 /* 1138 * TCP has a private interface for other kernel modules to reserve a 1139 * port range for them to use. Once reserved, TCP will not use any ports 1140 * in the range. This interface relies on the TCP_EXCLBIND feature. If 1141 * the semantics of TCP_EXCLBIND is changed, implementation of this interface 1142 * has to be verified. 1143 * 1144 * There can be TCP_RESERVED_PORTS_ARRAY_MAX_SIZE port ranges. Each port 1145 * range can cover at most TCP_RESERVED_PORTS_RANGE_MAX ports. A port 1146 * range is [port a, port b] inclusive. And each port range is between 1147 * TCP_LOWESET_RESERVED_PORT and TCP_LARGEST_RESERVED_PORT inclusive. 1148 * 1149 * Note that the default anonymous port range starts from 32768. There is 1150 * no port "collision" between that and the reserved port range. If there 1151 * is port collision (because the default smallest anonymous port is lowered 1152 * or some apps specifically bind to ports in the reserved port range), the 1153 * system may not be able to reserve a port range even there are enough 1154 * unbound ports as a reserved port range contains consecutive ports . 1155 */ 1156 #define TCP_RESERVED_PORTS_ARRAY_MAX_SIZE 5 1157 #define TCP_RESERVED_PORTS_RANGE_MAX 1000 1158 #define TCP_SMALLEST_RESERVED_PORT 10240 1159 #define TCP_LARGEST_RESERVED_PORT 20480 1160 1161 /* Structure to represent those reserved port ranges. */ 1162 typedef struct tcp_rport_s { 1163 in_port_t lo_port; 1164 in_port_t hi_port; 1165 tcp_t **temp_tcp_array; 1166 } tcp_rport_t; 1167 1168 /* The reserved port array. */ 1169 static tcp_rport_t tcp_reserved_port[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 1170 1171 /* Locks to protect the tcp_reserved_ports array. */ 1172 static krwlock_t tcp_reserved_port_lock; 1173 1174 /* The number of ranges in the array. */ 1175 uint32_t tcp_reserved_port_array_size = 0; 1176 1177 /* 1178 * MIB-2 stuff for SNMP 1179 * Note: tcpInErrs {tcp 15} is accumulated in ip.c 1180 */ 1181 mib2_tcp_t tcp_mib; /* SNMP fixed size info */ 1182 kstat_t *tcp_mibkp; /* kstat exporting tcp_mib data */ 1183 1184 boolean_t tcp_icmp_source_quench = B_FALSE; 1185 /* 1186 * Following assumes TPI alignment requirements stay along 32 bit 1187 * boundaries 1188 */ 1189 #define ROUNDUP32(x) \ 1190 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1191 1192 /* Template for response to info request. */ 1193 static struct T_info_ack tcp_g_t_info_ack = { 1194 T_INFO_ACK, /* PRIM_type */ 1195 0, /* TSDU_size */ 1196 T_INFINITE, /* ETSDU_size */ 1197 T_INVALID, /* CDATA_size */ 1198 T_INVALID, /* DDATA_size */ 1199 sizeof (sin_t), /* ADDR_size */ 1200 0, /* OPT_size - not initialized here */ 1201 TIDUSZ, /* TIDU_size */ 1202 T_COTS_ORD, /* SERV_type */ 1203 TCPS_IDLE, /* CURRENT_state */ 1204 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1205 }; 1206 1207 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1208 T_INFO_ACK, /* PRIM_type */ 1209 0, /* TSDU_size */ 1210 T_INFINITE, /* ETSDU_size */ 1211 T_INVALID, /* CDATA_size */ 1212 T_INVALID, /* DDATA_size */ 1213 sizeof (sin6_t), /* ADDR_size */ 1214 0, /* OPT_size - not initialized here */ 1215 TIDUSZ, /* TIDU_size */ 1216 T_COTS_ORD, /* SERV_type */ 1217 TCPS_IDLE, /* CURRENT_state */ 1218 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1219 }; 1220 1221 #define MS 1L 1222 #define SECONDS (1000 * MS) 1223 #define MINUTES (60 * SECONDS) 1224 #define HOURS (60 * MINUTES) 1225 #define DAYS (24 * HOURS) 1226 1227 #define PARAM_MAX (~(uint32_t)0) 1228 1229 /* Max size IP datagram is 64k - 1 */ 1230 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1231 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1232 /* Max of the above */ 1233 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1234 1235 /* Largest TCP port number */ 1236 #define TCP_MAX_PORT (64 * 1024 - 1) 1237 1238 /* 1239 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1240 * layer header. It has to be a multiple of 4. 1241 */ 1242 static tcpparam_t tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1243 #define tcp_wroff_xtra tcp_wroff_xtra_param.tcp_param_val 1244 1245 /* 1246 * All of these are alterable, within the min/max values given, at run time. 1247 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1248 * per the TCP spec. 1249 */ 1250 /* BEGIN CSTYLED */ 1251 tcpparam_t tcp_param_arr[] = { 1252 /*min max value name */ 1253 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1254 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1255 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1256 { 1, 1024, 1, "tcp_conn_req_min" }, 1257 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1258 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1259 { 0, 10, 0, "tcp_debug" }, 1260 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1261 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1262 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1263 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1264 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1265 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1266 { 1, 255, 64, "tcp_ipv4_ttl"}, 1267 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1268 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1269 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1270 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1271 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1272 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1273 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1274 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1275 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1276 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1277 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1278 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1279 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1280 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1281 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1282 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1283 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1284 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1285 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1286 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1287 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1288 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1289 { 0, TCP_MSS_MAX, 64, "tcp_co_min"}, 1290 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1291 /* 1292 * Question: What default value should I set for tcp_strong_iss? 1293 */ 1294 { 0, 2, 1, "tcp_strong_iss"}, 1295 { 0, 65536, 20, "tcp_rtt_updates"}, 1296 { 0, 1, 1, "tcp_wscale_always"}, 1297 { 0, 1, 0, "tcp_tstamp_always"}, 1298 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1299 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1300 { 0, 16, 2, "tcp_deferred_acks_max"}, 1301 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1302 { 1, 4, 4, "tcp_slow_start_initial"}, 1303 { 10*MS, 50*MS, 20*MS, "tcp_co_timer_interval"}, 1304 { 0, 2, 2, "tcp_sack_permitted"}, 1305 { 0, 1, 0, "tcp_trace"}, 1306 { 0, 1, 1, "tcp_compression_enabled"}, 1307 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1308 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1309 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1310 { 0, 1, 0, "tcp_rev_src_routes"}, 1311 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1312 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1313 { 0, 16, 8, "tcp_local_dacks_max"}, 1314 { 0, 2, 1, "tcp_ecn_permitted"}, 1315 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1316 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1317 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1318 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1319 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1320 }; 1321 /* END CSTYLED */ 1322 1323 /* 1324 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1325 * each header fragment in the header buffer. Each parameter value has 1326 * to be a multiple of 4 (32-bit aligned). 1327 */ 1328 static tcpparam_t tcp_mdt_head_param = { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1329 static tcpparam_t tcp_mdt_tail_param = { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1330 #define tcp_mdt_hdr_head_min tcp_mdt_head_param.tcp_param_val 1331 #define tcp_mdt_hdr_tail_min tcp_mdt_tail_param.tcp_param_val 1332 1333 /* 1334 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1335 * the maximum number of payload buffers associated per Multidata. 1336 */ 1337 static tcpparam_t tcp_mdt_max_pbufs_param = 1338 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1339 #define tcp_mdt_max_pbufs tcp_mdt_max_pbufs_param.tcp_param_val 1340 1341 /* Round up the value to the nearest mss. */ 1342 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1343 1344 /* 1345 * Set ECN capable transport (ECT) code point in IP header. 1346 * 1347 * Note that there are 2 ECT code points '01' and '10', which are called 1348 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1349 * point ECT(0) for TCP as described in RFC 2481. 1350 */ 1351 #define SET_ECT(tcp, iph) \ 1352 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1353 /* We need to clear the code point first. */ \ 1354 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1355 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1356 } else { \ 1357 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1358 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1359 } 1360 1361 /* 1362 * The format argument to pass to tcp_display(). 1363 * DISP_PORT_ONLY means that the returned string has only port info. 1364 * DISP_ADDR_AND_PORT means that the returned string also contains the 1365 * remote and local IP address. 1366 */ 1367 #define DISP_PORT_ONLY 1 1368 #define DISP_ADDR_AND_PORT 2 1369 1370 /* 1371 * This controls the rate some ndd info report functions can be used 1372 * by non-privileged users. It stores the last time such info is 1373 * requested. When those report functions are called again, this 1374 * is checked with the current time and compare with the ndd param 1375 * tcp_ndd_get_info_interval. 1376 */ 1377 static clock_t tcp_last_ndd_get_info_time = 0; 1378 #define NDD_TOO_QUICK_MSG \ 1379 "ndd get info rate too high for non-privileged users, try again " \ 1380 "later.\n" 1381 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1382 1383 #define IS_VMLOANED_MBLK(mp) \ 1384 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1385 1386 /* 1387 * These two variables control the rate for TCP to generate RSTs in 1388 * response to segments not belonging to any connections. We limit 1389 * TCP to sent out tcp_rst_sent_rate (ndd param) number of RSTs in 1390 * each 1 second interval. This is to protect TCP against DoS attack. 1391 */ 1392 static clock_t tcp_last_rst_intrvl; 1393 static uint32_t tcp_rst_cnt; 1394 1395 /* The number of RST not sent because of the rate limit. */ 1396 static uint32_t tcp_rst_unsent; 1397 1398 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1399 boolean_t tcp_mdt_chain = B_TRUE; 1400 1401 /* 1402 * MDT threshold in the form of effective send MSS multiplier; we take 1403 * the MDT path if the amount of unsent data exceeds the threshold value 1404 * (default threshold is 1*SMSS). 1405 */ 1406 uint_t tcp_mdt_smss_threshold = 1; 1407 1408 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1409 1410 /* 1411 * Forces all connections to obey the value of the tcp_maxpsz_multiplier 1412 * tunable settable via NDD. Otherwise, the per-connection behavior is 1413 * determined dynamically during tcp_adapt_ire(), which is the default. 1414 */ 1415 boolean_t tcp_static_maxpsz = B_FALSE; 1416 1417 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1418 uint32_t tcp_random_anon_port = 1; 1419 1420 /* 1421 * To reach to an eager in Q0 which can be dropped due to an incoming 1422 * new SYN request when Q0 is full, a new doubly linked list is 1423 * introduced. This list allows to select an eager from Q0 in O(1) time. 1424 * This is needed to avoid spending too much time walking through the 1425 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1426 * this new list has to be a member of Q0. 1427 * This list is headed by listener's tcp_t. When the list is empty, 1428 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1429 * of listener's tcp_t point to listener's tcp_t itself. 1430 * 1431 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1432 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1433 * These macros do not affect the eager's membership to Q0. 1434 */ 1435 1436 1437 #define MAKE_DROPPABLE(listener, eager) \ 1438 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1439 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1440 = (eager); \ 1441 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1442 (eager)->tcp_eager_next_drop_q0 = \ 1443 (listener)->tcp_eager_next_drop_q0; \ 1444 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1445 } 1446 1447 #define MAKE_UNDROPPABLE(eager) \ 1448 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1449 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1450 = (eager)->tcp_eager_prev_drop_q0; \ 1451 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1452 = (eager)->tcp_eager_next_drop_q0; \ 1453 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1454 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1455 } 1456 1457 /* 1458 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1459 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1460 * data, TCP will not respond with an ACK. RFC 793 requires that 1461 * TCP responds with an ACK for such a bogus ACK. By not following 1462 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1463 * an attacker successfully spoofs an acceptable segment to our 1464 * peer; or when our peer is "confused." 1465 */ 1466 uint32_t tcp_drop_ack_unsent_cnt = 10; 1467 1468 /* 1469 * Hook functions to enable cluster networking 1470 * On non-clustered systems these vectors must always be NULL. 1471 */ 1472 1473 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1474 uint8_t *laddrp, in_port_t lport) = NULL; 1475 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1476 uint8_t *laddrp, in_port_t lport) = NULL; 1477 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1478 uint8_t *laddrp, in_port_t lport, 1479 uint8_t *faddrp, in_port_t fport) = NULL; 1480 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1481 uint8_t *laddrp, in_port_t lport, 1482 uint8_t *faddrp, in_port_t fport) = NULL; 1483 1484 /* 1485 * The following are defined in ip.c 1486 */ 1487 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1488 uint8_t *laddrp); 1489 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1490 uint8_t *laddrp, uint8_t *faddrp); 1491 1492 #define CL_INET_CONNECT(tcp) { \ 1493 if (cl_inet_connect != NULL) { \ 1494 /* \ 1495 * Running in cluster mode - register active connection \ 1496 * information \ 1497 */ \ 1498 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1499 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1500 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1501 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1502 (in_port_t)(tcp)->tcp_lport, \ 1503 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1504 (in_port_t)(tcp)->tcp_fport); \ 1505 } \ 1506 } else { \ 1507 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1508 &(tcp)->tcp_ip6h->ip6_src)) {\ 1509 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1510 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1511 (in_port_t)(tcp)->tcp_lport, \ 1512 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1513 (in_port_t)(tcp)->tcp_fport); \ 1514 } \ 1515 } \ 1516 } \ 1517 } 1518 1519 #define CL_INET_DISCONNECT(tcp) { \ 1520 if (cl_inet_disconnect != NULL) { \ 1521 /* \ 1522 * Running in cluster mode - deregister active \ 1523 * connection information \ 1524 */ \ 1525 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1526 if ((tcp)->tcp_ip_src != 0) { \ 1527 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1528 AF_INET, \ 1529 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1530 (in_port_t)(tcp)->tcp_lport, \ 1531 (uint8_t *) \ 1532 (&((tcp)->tcp_ipha->ipha_dst)),\ 1533 (in_port_t)(tcp)->tcp_fport); \ 1534 } \ 1535 } else { \ 1536 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1537 &(tcp)->tcp_ip_src_v6)) { \ 1538 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1539 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1540 (in_port_t)(tcp)->tcp_lport, \ 1541 (uint8_t *) \ 1542 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1543 (in_port_t)(tcp)->tcp_fport); \ 1544 } \ 1545 } \ 1546 } \ 1547 } 1548 1549 /* 1550 * Cluster networking hook for traversing current connection list. 1551 * This routine is used to extract the current list of live connections 1552 * which must continue to to be dispatched to this node. 1553 */ 1554 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1555 1556 /* 1557 * Figure out the value of window scale opton. Note that the rwnd is 1558 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1559 * We cannot find the scale value and then do a round up of tcp_rwnd 1560 * because the scale value may not be correct after that. 1561 * 1562 * Set the compiler flag to make this function inline. 1563 */ 1564 static void 1565 tcp_set_ws_value(tcp_t *tcp) 1566 { 1567 int i; 1568 uint32_t rwnd = tcp->tcp_rwnd; 1569 1570 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1571 i++, rwnd >>= 1) 1572 ; 1573 tcp->tcp_rcv_ws = i; 1574 } 1575 1576 /* 1577 * Remove a connection from the list of detached TIME_WAIT connections. 1578 * It returns B_FALSE if it can't remove the connection from the list 1579 * as the connection has already been removed from the list due to an 1580 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1581 */ 1582 static boolean_t 1583 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1584 { 1585 boolean_t locked = B_FALSE; 1586 1587 if (tcp_time_wait == NULL) { 1588 tcp_time_wait = *((tcp_squeue_priv_t **) 1589 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1590 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1591 locked = B_TRUE; 1592 } 1593 1594 if (tcp->tcp_time_wait_expire == 0) { 1595 ASSERT(tcp->tcp_time_wait_next == NULL); 1596 ASSERT(tcp->tcp_time_wait_prev == NULL); 1597 if (locked) 1598 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1599 return (B_FALSE); 1600 } 1601 ASSERT(TCP_IS_DETACHED(tcp)); 1602 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1603 1604 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1605 ASSERT(tcp->tcp_time_wait_prev == NULL); 1606 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1607 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1608 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1609 NULL; 1610 } else { 1611 tcp_time_wait->tcp_time_wait_tail = NULL; 1612 } 1613 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1614 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1615 ASSERT(tcp->tcp_time_wait_next == NULL); 1616 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1617 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1618 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1619 } else { 1620 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1621 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1622 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1623 tcp->tcp_time_wait_next; 1624 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1625 tcp->tcp_time_wait_prev; 1626 } 1627 tcp->tcp_time_wait_next = NULL; 1628 tcp->tcp_time_wait_prev = NULL; 1629 tcp->tcp_time_wait_expire = 0; 1630 1631 if (locked) 1632 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1633 return (B_TRUE); 1634 } 1635 1636 /* 1637 * Add a connection to the list of detached TIME_WAIT connections 1638 * and set its time to expire. 1639 */ 1640 static void 1641 tcp_time_wait_append(tcp_t *tcp) 1642 { 1643 tcp_squeue_priv_t *tcp_time_wait = 1644 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1645 SQPRIVATE_TCP)); 1646 1647 tcp_timers_stop(tcp); 1648 1649 /* Freed above */ 1650 ASSERT(tcp->tcp_timer_tid == 0); 1651 ASSERT(tcp->tcp_ack_tid == 0); 1652 1653 /* must have happened at the time of detaching the tcp */ 1654 ASSERT(tcp->tcp_ptpahn == NULL); 1655 ASSERT(tcp->tcp_flow_stopped == 0); 1656 ASSERT(tcp->tcp_time_wait_next == NULL); 1657 ASSERT(tcp->tcp_time_wait_prev == NULL); 1658 ASSERT(tcp->tcp_time_wait_expire == NULL); 1659 ASSERT(tcp->tcp_listener == NULL); 1660 1661 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1662 /* 1663 * The value computed below in tcp->tcp_time_wait_expire may 1664 * appear negative or wrap around. That is ok since our 1665 * interest is only in the difference between the current lbolt 1666 * value and tcp->tcp_time_wait_expire. But the value should not 1667 * be zero, since it means the tcp is not in the TIME_WAIT list. 1668 * The corresponding comparison in tcp_time_wait_collector() uses 1669 * modular arithmetic. 1670 */ 1671 tcp->tcp_time_wait_expire += 1672 drv_usectohz(tcp_time_wait_interval * 1000); 1673 if (tcp->tcp_time_wait_expire == 0) 1674 tcp->tcp_time_wait_expire = 1; 1675 1676 ASSERT(TCP_IS_DETACHED(tcp)); 1677 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1678 ASSERT(tcp->tcp_time_wait_next == NULL); 1679 ASSERT(tcp->tcp_time_wait_prev == NULL); 1680 TCP_DBGSTAT(tcp_time_wait); 1681 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1682 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1683 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1684 tcp_time_wait->tcp_time_wait_head = tcp; 1685 } else { 1686 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1687 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1688 TCPS_TIME_WAIT); 1689 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1690 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1691 } 1692 tcp_time_wait->tcp_time_wait_tail = tcp; 1693 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1694 } 1695 1696 /* ARGSUSED */ 1697 void 1698 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1699 { 1700 conn_t *connp = (conn_t *)arg; 1701 tcp_t *tcp = connp->conn_tcp; 1702 1703 ASSERT(tcp != NULL); 1704 if (tcp->tcp_state == TCPS_CLOSED) { 1705 return; 1706 } 1707 1708 ASSERT((tcp->tcp_family == AF_INET && 1709 tcp->tcp_ipversion == IPV4_VERSION) || 1710 (tcp->tcp_family == AF_INET6 && 1711 (tcp->tcp_ipversion == IPV4_VERSION || 1712 tcp->tcp_ipversion == IPV6_VERSION))); 1713 ASSERT(!tcp->tcp_listener); 1714 1715 TCP_STAT(tcp_time_wait_reap); 1716 ASSERT(TCP_IS_DETACHED(tcp)); 1717 1718 /* 1719 * Because they have no upstream client to rebind or tcp_close() 1720 * them later, we axe the connection here and now. 1721 */ 1722 tcp_close_detached(tcp); 1723 } 1724 1725 void 1726 tcp_cleanup(tcp_t *tcp) 1727 { 1728 mblk_t *mp; 1729 char *tcp_iphc; 1730 int tcp_iphc_len; 1731 int tcp_hdr_grown; 1732 tcp_sack_info_t *tcp_sack_info; 1733 conn_t *connp = tcp->tcp_connp; 1734 1735 tcp_bind_hash_remove(tcp); 1736 tcp_free(tcp); 1737 1738 /* Release any SSL context */ 1739 if (tcp->tcp_kssl_ent != NULL) { 1740 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1741 tcp->tcp_kssl_ent = NULL; 1742 } 1743 1744 if (tcp->tcp_kssl_ctx != NULL) { 1745 kssl_release_ctx(tcp->tcp_kssl_ctx); 1746 tcp->tcp_kssl_ctx = NULL; 1747 } 1748 tcp->tcp_kssl_pending = B_FALSE; 1749 1750 conn_delete_ire(connp, NULL); 1751 if (connp->conn_flags & IPCL_TCPCONN) { 1752 if (connp->conn_latch != NULL) 1753 IPLATCH_REFRELE(connp->conn_latch); 1754 if (connp->conn_policy != NULL) 1755 IPPH_REFRELE(connp->conn_policy); 1756 } 1757 1758 /* 1759 * Since we will bzero the entire structure, we need to 1760 * remove it and reinsert it in global hash list. We 1761 * know the walkers can't get to this conn because we 1762 * had set CONDEMNED flag earlier and checked reference 1763 * under conn_lock so walker won't pick it and when we 1764 * go the ipcl_globalhash_remove() below, no walker 1765 * can get to it. 1766 */ 1767 ipcl_globalhash_remove(connp); 1768 1769 /* Save some state */ 1770 mp = tcp->tcp_timercache; 1771 1772 tcp_sack_info = tcp->tcp_sack_info; 1773 tcp_iphc = tcp->tcp_iphc; 1774 tcp_iphc_len = tcp->tcp_iphc_len; 1775 tcp_hdr_grown = tcp->tcp_hdr_grown; 1776 1777 if (connp->conn_cred != NULL) 1778 crfree(connp->conn_cred); 1779 if (connp->conn_peercred != NULL) 1780 crfree(connp->conn_peercred); 1781 bzero(connp, sizeof (conn_t)); 1782 bzero(tcp, sizeof (tcp_t)); 1783 1784 /* restore the state */ 1785 tcp->tcp_timercache = mp; 1786 1787 tcp->tcp_sack_info = tcp_sack_info; 1788 tcp->tcp_iphc = tcp_iphc; 1789 tcp->tcp_iphc_len = tcp_iphc_len; 1790 tcp->tcp_hdr_grown = tcp_hdr_grown; 1791 1792 1793 tcp->tcp_connp = connp; 1794 1795 connp->conn_tcp = tcp; 1796 connp->conn_flags = IPCL_TCPCONN; 1797 connp->conn_state_flags = CONN_INCIPIENT; 1798 connp->conn_ulp = IPPROTO_TCP; 1799 connp->conn_ref = 1; 1800 1801 ipcl_globalhash_insert(connp); 1802 } 1803 1804 /* 1805 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1806 * is done forwards from the head. 1807 */ 1808 /* ARGSUSED */ 1809 void 1810 tcp_time_wait_collector(void *arg) 1811 { 1812 tcp_t *tcp; 1813 clock_t now; 1814 mblk_t *mp; 1815 conn_t *connp; 1816 kmutex_t *lock; 1817 boolean_t removed; 1818 1819 squeue_t *sqp = (squeue_t *)arg; 1820 tcp_squeue_priv_t *tcp_time_wait = 1821 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1822 1823 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1824 tcp_time_wait->tcp_time_wait_tid = 0; 1825 1826 if (tcp_time_wait->tcp_free_list != NULL && 1827 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1828 TCP_STAT(tcp_freelist_cleanup); 1829 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1830 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1831 CONN_DEC_REF(tcp->tcp_connp); 1832 } 1833 tcp_time_wait->tcp_free_list_cnt = 0; 1834 } 1835 1836 /* 1837 * In order to reap time waits reliably, we should use a 1838 * source of time that is not adjustable by the user -- hence 1839 * the call to ddi_get_lbolt(). 1840 */ 1841 now = ddi_get_lbolt(); 1842 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1843 /* 1844 * Compare times using modular arithmetic, since 1845 * lbolt can wrapover. 1846 */ 1847 if ((now - tcp->tcp_time_wait_expire) < 0) { 1848 break; 1849 } 1850 1851 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1852 ASSERT(removed); 1853 1854 connp = tcp->tcp_connp; 1855 ASSERT(connp->conn_fanout != NULL); 1856 lock = &connp->conn_fanout->connf_lock; 1857 /* 1858 * This is essentially a TW reclaim fast path optimization for 1859 * performance where the timewait collector checks under the 1860 * fanout lock (so that no one else can get access to the 1861 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1862 * the classifier hash list. If ref count is indeed 2, we can 1863 * just remove the conn under the fanout lock and avoid 1864 * cleaning up the conn under the squeue, provided that 1865 * clustering callbacks are not enabled. If clustering is 1866 * enabled, we need to make the clustering callback before 1867 * setting the CONDEMNED flag and after dropping all locks and 1868 * so we forego this optimization and fall back to the slow 1869 * path. Also please see the comments in tcp_closei_local 1870 * regarding the refcnt logic. 1871 * 1872 * Since we are holding the tcp_time_wait_lock, its better 1873 * not to block on the fanout_lock because other connections 1874 * can't add themselves to time_wait list. So we do a 1875 * tryenter instead of mutex_enter. 1876 */ 1877 if (mutex_tryenter(lock)) { 1878 mutex_enter(&connp->conn_lock); 1879 if ((connp->conn_ref == 2) && 1880 (cl_inet_disconnect == NULL)) { 1881 ipcl_hash_remove_locked(connp, 1882 connp->conn_fanout); 1883 /* 1884 * Set the CONDEMNED flag now itself so that 1885 * the refcnt cannot increase due to any 1886 * walker. But we have still not cleaned up 1887 * conn_ire_cache. This is still ok since 1888 * we are going to clean it up in tcp_cleanup 1889 * immediately and any interface unplumb 1890 * thread will wait till the ire is blown away 1891 */ 1892 connp->conn_state_flags |= CONN_CONDEMNED; 1893 mutex_exit(lock); 1894 mutex_exit(&connp->conn_lock); 1895 if (tcp_time_wait->tcp_free_list_cnt < 1896 tcp_free_list_max_cnt) { 1897 /* Add to head of tcp_free_list */ 1898 mutex_exit( 1899 &tcp_time_wait->tcp_time_wait_lock); 1900 tcp_cleanup(tcp); 1901 mutex_enter( 1902 &tcp_time_wait->tcp_time_wait_lock); 1903 tcp->tcp_time_wait_next = 1904 tcp_time_wait->tcp_free_list; 1905 tcp_time_wait->tcp_free_list = tcp; 1906 tcp_time_wait->tcp_free_list_cnt++; 1907 continue; 1908 } else { 1909 /* Do not add to tcp_free_list */ 1910 mutex_exit( 1911 &tcp_time_wait->tcp_time_wait_lock); 1912 tcp_bind_hash_remove(tcp); 1913 conn_delete_ire(tcp->tcp_connp, NULL); 1914 CONN_DEC_REF(tcp->tcp_connp); 1915 } 1916 } else { 1917 CONN_INC_REF_LOCKED(connp); 1918 mutex_exit(lock); 1919 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1920 mutex_exit(&connp->conn_lock); 1921 /* 1922 * We can reuse the closemp here since conn has 1923 * detached (otherwise we wouldn't even be in 1924 * time_wait list). tcp_closemp_used can safely 1925 * be changed without taking a lock as no other 1926 * thread can concurrently access it at this 1927 * point in the connection lifecycle. We 1928 * increment tcp_closemp_used to record any 1929 * attempt to reuse tcp_closemp while it is 1930 * still in use. 1931 */ 1932 1933 if (tcp->tcp_closemp.b_prev == NULL) 1934 tcp->tcp_closemp_used = 1; 1935 else 1936 tcp->tcp_closemp_used++; 1937 1938 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1939 mp = &tcp->tcp_closemp; 1940 squeue_fill(connp->conn_sqp, mp, 1941 tcp_timewait_output, connp, 1942 SQTAG_TCP_TIMEWAIT); 1943 } 1944 } else { 1945 mutex_enter(&connp->conn_lock); 1946 CONN_INC_REF_LOCKED(connp); 1947 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1948 mutex_exit(&connp->conn_lock); 1949 /* 1950 * We can reuse the closemp here since conn has 1951 * detached (otherwise we wouldn't even be in 1952 * time_wait list). tcp_closemp_used can safely 1953 * be changed without taking a lock as no other 1954 * thread can concurrently access it at this 1955 * point in the connection lifecycle. We 1956 * increment tcp_closemp_used to record any 1957 * attempt to reuse tcp_closemp while it is 1958 * still in use. 1959 */ 1960 1961 if (tcp->tcp_closemp.b_prev == NULL) 1962 tcp->tcp_closemp_used = 1; 1963 else 1964 tcp->tcp_closemp_used++; 1965 1966 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1967 mp = &tcp->tcp_closemp; 1968 squeue_fill(connp->conn_sqp, mp, 1969 tcp_timewait_output, connp, 0); 1970 } 1971 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1972 } 1973 1974 if (tcp_time_wait->tcp_free_list != NULL) 1975 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1976 1977 tcp_time_wait->tcp_time_wait_tid = 1978 timeout(tcp_time_wait_collector, sqp, TCP_TIME_WAIT_DELAY); 1979 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1980 } 1981 1982 /* 1983 * Reply to a clients T_CONN_RES TPI message. This function 1984 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1985 * on the acceptor STREAM and processed in tcp_wput_accept(). 1986 * Read the block comment on top of tcp_conn_request(). 1987 */ 1988 static void 1989 tcp_accept(tcp_t *listener, mblk_t *mp) 1990 { 1991 tcp_t *acceptor; 1992 tcp_t *eager; 1993 tcp_t *tcp; 1994 struct T_conn_res *tcr; 1995 t_uscalar_t acceptor_id; 1996 t_scalar_t seqnum; 1997 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1998 mblk_t *ok_mp; 1999 mblk_t *mp1; 2000 2001 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 2002 tcp_err_ack(listener, mp, TPROTO, 0); 2003 return; 2004 } 2005 tcr = (struct T_conn_res *)mp->b_rptr; 2006 2007 /* 2008 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 2009 * read side queue of the streams device underneath us i.e. the 2010 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 2011 * look it up in the queue_hash. Under LP64 it sends down the 2012 * minor_t of the accepting endpoint. 2013 * 2014 * Once the acceptor/eager are modified (in tcp_accept_swap) the 2015 * fanout hash lock is held. 2016 * This prevents any thread from entering the acceptor queue from 2017 * below (since it has not been hard bound yet i.e. any inbound 2018 * packets will arrive on the listener or default tcp queue and 2019 * go through tcp_lookup). 2020 * The CONN_INC_REF will prevent the acceptor from closing. 2021 * 2022 * XXX It is still possible for a tli application to send down data 2023 * on the accepting stream while another thread calls t_accept. 2024 * This should not be a problem for well-behaved applications since 2025 * the T_OK_ACK is sent after the queue swapping is completed. 2026 * 2027 * If the accepting fd is the same as the listening fd, avoid 2028 * queue hash lookup since that will return an eager listener in a 2029 * already established state. 2030 */ 2031 acceptor_id = tcr->ACCEPTOR_id; 2032 mutex_enter(&listener->tcp_eager_lock); 2033 if (listener->tcp_acceptor_id == acceptor_id) { 2034 eager = listener->tcp_eager_next_q; 2035 /* only count how many T_CONN_INDs so don't count q0 */ 2036 if ((listener->tcp_conn_req_cnt_q != 1) || 2037 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 2038 mutex_exit(&listener->tcp_eager_lock); 2039 tcp_err_ack(listener, mp, TBADF, 0); 2040 return; 2041 } 2042 if (listener->tcp_conn_req_cnt_q0 != 0) { 2043 /* Throw away all the eagers on q0. */ 2044 tcp_eager_cleanup(listener, 1); 2045 } 2046 if (listener->tcp_syn_defense) { 2047 listener->tcp_syn_defense = B_FALSE; 2048 if (listener->tcp_ip_addr_cache != NULL) { 2049 kmem_free(listener->tcp_ip_addr_cache, 2050 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 2051 listener->tcp_ip_addr_cache = NULL; 2052 } 2053 } 2054 /* 2055 * Transfer tcp_conn_req_max to the eager so that when 2056 * a disconnect occurs we can revert the endpoint to the 2057 * listen state. 2058 */ 2059 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 2060 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 2061 /* 2062 * Get a reference on the acceptor just like the 2063 * tcp_acceptor_hash_lookup below. 2064 */ 2065 acceptor = listener; 2066 CONN_INC_REF(acceptor->tcp_connp); 2067 } else { 2068 acceptor = tcp_acceptor_hash_lookup(acceptor_id); 2069 if (acceptor == NULL) { 2070 if (listener->tcp_debug) { 2071 (void) strlog(TCP_MOD_ID, 0, 1, 2072 SL_ERROR|SL_TRACE, 2073 "tcp_accept: did not find acceptor 0x%x\n", 2074 acceptor_id); 2075 } 2076 mutex_exit(&listener->tcp_eager_lock); 2077 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 2078 return; 2079 } 2080 /* 2081 * Verify acceptor state. The acceptable states for an acceptor 2082 * include TCPS_IDLE and TCPS_BOUND. 2083 */ 2084 switch (acceptor->tcp_state) { 2085 case TCPS_IDLE: 2086 /* FALLTHRU */ 2087 case TCPS_BOUND: 2088 break; 2089 default: 2090 CONN_DEC_REF(acceptor->tcp_connp); 2091 mutex_exit(&listener->tcp_eager_lock); 2092 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2093 return; 2094 } 2095 } 2096 2097 /* The listener must be in TCPS_LISTEN */ 2098 if (listener->tcp_state != TCPS_LISTEN) { 2099 CONN_DEC_REF(acceptor->tcp_connp); 2100 mutex_exit(&listener->tcp_eager_lock); 2101 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2102 return; 2103 } 2104 2105 /* 2106 * Rendezvous with an eager connection request packet hanging off 2107 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2108 * tcp structure when the connection packet arrived in 2109 * tcp_conn_request(). 2110 */ 2111 seqnum = tcr->SEQ_number; 2112 eager = listener; 2113 do { 2114 eager = eager->tcp_eager_next_q; 2115 if (eager == NULL) { 2116 CONN_DEC_REF(acceptor->tcp_connp); 2117 mutex_exit(&listener->tcp_eager_lock); 2118 tcp_err_ack(listener, mp, TBADSEQ, 0); 2119 return; 2120 } 2121 } while (eager->tcp_conn_req_seqnum != seqnum); 2122 mutex_exit(&listener->tcp_eager_lock); 2123 2124 /* 2125 * At this point, both acceptor and listener have 2 ref 2126 * that they begin with. Acceptor has one additional ref 2127 * we placed in lookup while listener has 3 additional 2128 * ref for being behind the squeue (tcp_accept() is 2129 * done on listener's squeue); being in classifier hash; 2130 * and eager's ref on listener. 2131 */ 2132 ASSERT(listener->tcp_connp->conn_ref >= 5); 2133 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2134 2135 /* 2136 * The eager at this point is set in its own squeue and 2137 * could easily have been killed (tcp_accept_finish will 2138 * deal with that) because of a TH_RST so we can only 2139 * ASSERT for a single ref. 2140 */ 2141 ASSERT(eager->tcp_connp->conn_ref >= 1); 2142 2143 /* Pre allocate the stroptions mblk also */ 2144 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2145 if (opt_mp == NULL) { 2146 CONN_DEC_REF(acceptor->tcp_connp); 2147 CONN_DEC_REF(eager->tcp_connp); 2148 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2149 return; 2150 } 2151 DB_TYPE(opt_mp) = M_SETOPTS; 2152 opt_mp->b_wptr += sizeof (struct stroptions); 2153 2154 /* 2155 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2156 * from listener to acceptor. The message is chained on opt_mp 2157 * which will be sent onto eager's squeue. 2158 */ 2159 if (listener->tcp_bound_if != 0) { 2160 /* allocate optmgmt req */ 2161 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2162 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2163 sizeof (int)); 2164 if (mp1 != NULL) 2165 linkb(opt_mp, mp1); 2166 } 2167 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2168 uint_t on = 1; 2169 2170 /* allocate optmgmt req */ 2171 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2172 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2173 if (mp1 != NULL) 2174 linkb(opt_mp, mp1); 2175 } 2176 2177 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2178 if ((mp1 = copymsg(mp)) == NULL) { 2179 CONN_DEC_REF(acceptor->tcp_connp); 2180 CONN_DEC_REF(eager->tcp_connp); 2181 freemsg(opt_mp); 2182 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2183 return; 2184 } 2185 2186 tcr = (struct T_conn_res *)mp1->b_rptr; 2187 2188 /* 2189 * This is an expanded version of mi_tpi_ok_ack_alloc() 2190 * which allocates a larger mblk and appends the new 2191 * local address to the ok_ack. The address is copied by 2192 * soaccept() for getsockname(). 2193 */ 2194 { 2195 int extra; 2196 2197 extra = (eager->tcp_family == AF_INET) ? 2198 sizeof (sin_t) : sizeof (sin6_t); 2199 2200 /* 2201 * Try to re-use mp, if possible. Otherwise, allocate 2202 * an mblk and return it as ok_mp. In any case, mp 2203 * is no longer usable upon return. 2204 */ 2205 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2206 CONN_DEC_REF(acceptor->tcp_connp); 2207 CONN_DEC_REF(eager->tcp_connp); 2208 freemsg(opt_mp); 2209 /* Original mp has been freed by now, so use mp1 */ 2210 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2211 return; 2212 } 2213 2214 mp = NULL; /* We should never use mp after this point */ 2215 2216 switch (extra) { 2217 case sizeof (sin_t): { 2218 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2219 2220 ok_mp->b_wptr += extra; 2221 sin->sin_family = AF_INET; 2222 sin->sin_port = eager->tcp_lport; 2223 sin->sin_addr.s_addr = 2224 eager->tcp_ipha->ipha_src; 2225 break; 2226 } 2227 case sizeof (sin6_t): { 2228 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2229 2230 ok_mp->b_wptr += extra; 2231 sin6->sin6_family = AF_INET6; 2232 sin6->sin6_port = eager->tcp_lport; 2233 if (eager->tcp_ipversion == IPV4_VERSION) { 2234 sin6->sin6_flowinfo = 0; 2235 IN6_IPADDR_TO_V4MAPPED( 2236 eager->tcp_ipha->ipha_src, 2237 &sin6->sin6_addr); 2238 } else { 2239 ASSERT(eager->tcp_ip6h != NULL); 2240 sin6->sin6_flowinfo = 2241 eager->tcp_ip6h->ip6_vcf & 2242 ~IPV6_VERS_AND_FLOW_MASK; 2243 sin6->sin6_addr = 2244 eager->tcp_ip6h->ip6_src; 2245 } 2246 break; 2247 } 2248 default: 2249 break; 2250 } 2251 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2252 } 2253 2254 /* 2255 * If there are no options we know that the T_CONN_RES will 2256 * succeed. However, we can't send the T_OK_ACK upstream until 2257 * the tcp_accept_swap is done since it would be dangerous to 2258 * let the application start using the new fd prior to the swap. 2259 */ 2260 tcp_accept_swap(listener, acceptor, eager); 2261 2262 /* 2263 * tcp_accept_swap unlinks eager from listener but does not drop 2264 * the eager's reference on the listener. 2265 */ 2266 ASSERT(eager->tcp_listener == NULL); 2267 ASSERT(listener->tcp_connp->conn_ref >= 5); 2268 2269 /* 2270 * The eager is now associated with its own queue. Insert in 2271 * the hash so that the connection can be reused for a future 2272 * T_CONN_RES. 2273 */ 2274 tcp_acceptor_hash_insert(acceptor_id, eager); 2275 2276 /* 2277 * We now do the processing of options with T_CONN_RES. 2278 * We delay till now since we wanted to have queue to pass to 2279 * option processing routines that points back to the right 2280 * instance structure which does not happen until after 2281 * tcp_accept_swap(). 2282 * 2283 * Note: 2284 * The sanity of the logic here assumes that whatever options 2285 * are appropriate to inherit from listner=>eager are done 2286 * before this point, and whatever were to be overridden (or not) 2287 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2288 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2289 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2290 * This may not be true at this point in time but can be fixed 2291 * independently. This option processing code starts with 2292 * the instantiated acceptor instance and the final queue at 2293 * this point. 2294 */ 2295 2296 if (tcr->OPT_length != 0) { 2297 /* Options to process */ 2298 int t_error = 0; 2299 int sys_error = 0; 2300 int do_disconnect = 0; 2301 2302 if (tcp_conprim_opt_process(eager, mp1, 2303 &do_disconnect, &t_error, &sys_error) < 0) { 2304 eager->tcp_accept_error = 1; 2305 if (do_disconnect) { 2306 /* 2307 * An option failed which does not allow 2308 * connection to be accepted. 2309 * 2310 * We allow T_CONN_RES to succeed and 2311 * put a T_DISCON_IND on the eager queue. 2312 */ 2313 ASSERT(t_error == 0 && sys_error == 0); 2314 eager->tcp_send_discon_ind = 1; 2315 } else { 2316 ASSERT(t_error != 0); 2317 freemsg(ok_mp); 2318 /* 2319 * Original mp was either freed or set 2320 * to ok_mp above, so use mp1 instead. 2321 */ 2322 tcp_err_ack(listener, mp1, t_error, sys_error); 2323 goto finish; 2324 } 2325 } 2326 /* 2327 * Most likely success in setting options (except if 2328 * eager->tcp_send_discon_ind set). 2329 * mp1 option buffer represented by OPT_length/offset 2330 * potentially modified and contains results of setting 2331 * options at this point 2332 */ 2333 } 2334 2335 /* We no longer need mp1, since all options processing has passed */ 2336 freemsg(mp1); 2337 2338 putnext(listener->tcp_rq, ok_mp); 2339 2340 mutex_enter(&listener->tcp_eager_lock); 2341 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2342 tcp_t *tail; 2343 mblk_t *conn_ind; 2344 2345 /* 2346 * This path should not be executed if listener and 2347 * acceptor streams are the same. 2348 */ 2349 ASSERT(listener != acceptor); 2350 2351 tcp = listener->tcp_eager_prev_q0; 2352 /* 2353 * listener->tcp_eager_prev_q0 points to the TAIL of the 2354 * deferred T_conn_ind queue. We need to get to the head of 2355 * the queue in order to send up T_conn_ind the same order as 2356 * how the 3WHS is completed. 2357 */ 2358 while (tcp != listener) { 2359 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2360 break; 2361 else 2362 tcp = tcp->tcp_eager_prev_q0; 2363 } 2364 ASSERT(tcp != listener); 2365 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2366 ASSERT(conn_ind != NULL); 2367 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2368 2369 /* Move from q0 to q */ 2370 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2371 listener->tcp_conn_req_cnt_q0--; 2372 listener->tcp_conn_req_cnt_q++; 2373 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2374 tcp->tcp_eager_prev_q0; 2375 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2376 tcp->tcp_eager_next_q0; 2377 tcp->tcp_eager_prev_q0 = NULL; 2378 tcp->tcp_eager_next_q0 = NULL; 2379 tcp->tcp_conn_def_q0 = B_FALSE; 2380 2381 /* Make sure the tcp isn't in the list of droppables */ 2382 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2383 tcp->tcp_eager_prev_drop_q0 == NULL); 2384 2385 /* 2386 * Insert at end of the queue because sockfs sends 2387 * down T_CONN_RES in chronological order. Leaving 2388 * the older conn indications at front of the queue 2389 * helps reducing search time. 2390 */ 2391 tail = listener->tcp_eager_last_q; 2392 if (tail != NULL) 2393 tail->tcp_eager_next_q = tcp; 2394 else 2395 listener->tcp_eager_next_q = tcp; 2396 listener->tcp_eager_last_q = tcp; 2397 tcp->tcp_eager_next_q = NULL; 2398 mutex_exit(&listener->tcp_eager_lock); 2399 putnext(tcp->tcp_rq, conn_ind); 2400 } else { 2401 mutex_exit(&listener->tcp_eager_lock); 2402 } 2403 2404 /* 2405 * Done with the acceptor - free it 2406 * 2407 * Note: from this point on, no access to listener should be made 2408 * as listener can be equal to acceptor. 2409 */ 2410 finish: 2411 ASSERT(acceptor->tcp_detached); 2412 acceptor->tcp_rq = tcp_g_q; 2413 acceptor->tcp_wq = WR(tcp_g_q); 2414 (void) tcp_clean_death(acceptor, 0, 2); 2415 CONN_DEC_REF(acceptor->tcp_connp); 2416 2417 /* 2418 * In case we already received a FIN we have to make tcp_rput send 2419 * the ordrel_ind. This will also send up a window update if the window 2420 * has opened up. 2421 * 2422 * In the normal case of a successful connection acceptance 2423 * we give the O_T_BIND_REQ to the read side put procedure as an 2424 * indication that this was just accepted. This tells tcp_rput to 2425 * pass up any data queued in tcp_rcv_list. 2426 * 2427 * In the fringe case where options sent with T_CONN_RES failed and 2428 * we required, we would be indicating a T_DISCON_IND to blow 2429 * away this connection. 2430 */ 2431 2432 /* 2433 * XXX: we currently have a problem if XTI application closes the 2434 * acceptor stream in between. This problem exists in on10-gate also 2435 * and is well know but nothing can be done short of major rewrite 2436 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2437 * eager same squeue as listener (we can distinguish non socket 2438 * listeners at the time of handling a SYN in tcp_conn_request) 2439 * and do most of the work that tcp_accept_finish does here itself 2440 * and then get behind the acceptor squeue to access the acceptor 2441 * queue. 2442 */ 2443 /* 2444 * We already have a ref on tcp so no need to do one before squeue_fill 2445 */ 2446 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2447 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2448 } 2449 2450 /* 2451 * Swap information between the eager and acceptor for a TLI/XTI client. 2452 * The sockfs accept is done on the acceptor stream and control goes 2453 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2454 * called. In either case, both the eager and listener are in their own 2455 * perimeter (squeue) and the code has to deal with potential race. 2456 * 2457 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2458 */ 2459 static void 2460 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2461 { 2462 conn_t *econnp, *aconnp; 2463 2464 ASSERT(eager->tcp_rq == listener->tcp_rq); 2465 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2466 ASSERT(!eager->tcp_hard_bound); 2467 ASSERT(!TCP_IS_SOCKET(acceptor)); 2468 ASSERT(!TCP_IS_SOCKET(eager)); 2469 ASSERT(!TCP_IS_SOCKET(listener)); 2470 2471 acceptor->tcp_detached = B_TRUE; 2472 /* 2473 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2474 * the acceptor id. 2475 */ 2476 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2477 2478 /* remove eager from listen list... */ 2479 mutex_enter(&listener->tcp_eager_lock); 2480 tcp_eager_unlink(eager); 2481 ASSERT(eager->tcp_eager_next_q == NULL && 2482 eager->tcp_eager_last_q == NULL); 2483 ASSERT(eager->tcp_eager_next_q0 == NULL && 2484 eager->tcp_eager_prev_q0 == NULL); 2485 mutex_exit(&listener->tcp_eager_lock); 2486 eager->tcp_rq = acceptor->tcp_rq; 2487 eager->tcp_wq = acceptor->tcp_wq; 2488 2489 econnp = eager->tcp_connp; 2490 aconnp = acceptor->tcp_connp; 2491 2492 eager->tcp_rq->q_ptr = econnp; 2493 eager->tcp_wq->q_ptr = econnp; 2494 2495 /* 2496 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2497 * which might be a different squeue from our peer TCP instance. 2498 * For TCP Fusion, the peer expects that whenever tcp_detached is 2499 * clear, our TCP queues point to the acceptor's queues. Thus, use 2500 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2501 * above reach global visibility prior to the clearing of tcp_detached. 2502 */ 2503 membar_producer(); 2504 eager->tcp_detached = B_FALSE; 2505 2506 ASSERT(eager->tcp_ack_tid == 0); 2507 2508 econnp->conn_dev = aconnp->conn_dev; 2509 if (eager->tcp_cred != NULL) 2510 crfree(eager->tcp_cred); 2511 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2512 aconnp->conn_cred = NULL; 2513 2514 econnp->conn_zoneid = aconnp->conn_zoneid; 2515 econnp->conn_allzones = aconnp->conn_allzones; 2516 2517 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2518 aconnp->conn_mac_exempt = B_FALSE; 2519 2520 ASSERT(aconnp->conn_peercred == NULL); 2521 2522 /* Do the IPC initialization */ 2523 CONN_INC_REF(econnp); 2524 2525 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2526 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2527 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2528 econnp->conn_ulp = aconnp->conn_ulp; 2529 2530 /* Done with old IPC. Drop its ref on its connp */ 2531 CONN_DEC_REF(aconnp); 2532 } 2533 2534 2535 /* 2536 * Adapt to the information, such as rtt and rtt_sd, provided from the 2537 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2538 * 2539 * Checks for multicast and broadcast destination address. 2540 * Returns zero on failure; non-zero if ok. 2541 * 2542 * Note that the MSS calculation here is based on the info given in 2543 * the IRE. We do not do any calculation based on TCP options. They 2544 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2545 * knows which options to use. 2546 * 2547 * Note on how TCP gets its parameters for a connection. 2548 * 2549 * When a tcp_t structure is allocated, it gets all the default parameters. 2550 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2551 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2552 * default. But if there is an associated tcp_host_param, it will override 2553 * the metrics. 2554 * 2555 * An incoming SYN with a multicast or broadcast destination address, is dropped 2556 * in 1 of 2 places. 2557 * 2558 * 1. If the packet was received over the wire it is dropped in 2559 * ip_rput_process_broadcast() 2560 * 2561 * 2. If the packet was received through internal IP loopback, i.e. the packet 2562 * was generated and received on the same machine, it is dropped in 2563 * ip_wput_local() 2564 * 2565 * An incoming SYN with a multicast or broadcast source address is always 2566 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2567 * reject an attempt to connect to a broadcast or multicast (destination) 2568 * address. 2569 */ 2570 static int 2571 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2572 { 2573 tcp_hsp_t *hsp; 2574 ire_t *ire; 2575 ire_t *sire = NULL; 2576 iulp_t *ire_uinfo = NULL; 2577 uint32_t mss_max; 2578 uint32_t mss; 2579 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2580 conn_t *connp = tcp->tcp_connp; 2581 boolean_t ire_cacheable = B_FALSE; 2582 zoneid_t zoneid = connp->conn_zoneid; 2583 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2584 MATCH_IRE_SECATTR; 2585 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2586 ill_t *ill = NULL; 2587 boolean_t incoming = (ire_mp == NULL); 2588 2589 ASSERT(connp->conn_ire_cache == NULL); 2590 2591 if (tcp->tcp_ipversion == IPV4_VERSION) { 2592 2593 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2594 BUMP_MIB(&ip_mib, ipInDiscards); 2595 return (0); 2596 } 2597 /* 2598 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2599 * for the destination with the nexthop as gateway. 2600 * ire_ctable_lookup() is used because this particular 2601 * ire, if it exists, will be marked private. 2602 * If that is not available, use the interface ire 2603 * for the nexthop. 2604 * 2605 * TSol: tcp_update_label will detect label mismatches based 2606 * only on the destination's label, but that would not 2607 * detect label mismatches based on the security attributes 2608 * of routes or next hop gateway. Hence we need to pass the 2609 * label to ire_ftable_lookup below in order to locate the 2610 * right prefix (and/or) ire cache. Similarly we also need 2611 * pass the label to the ire_cache_lookup below to locate 2612 * the right ire that also matches on the label. 2613 */ 2614 if (tcp->tcp_connp->conn_nexthop_set) { 2615 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2616 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2617 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW); 2618 if (ire == NULL) { 2619 ire = ire_ftable_lookup( 2620 tcp->tcp_connp->conn_nexthop_v4, 2621 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2622 tsl, match_flags); 2623 if (ire == NULL) 2624 return (0); 2625 } else { 2626 ire_uinfo = &ire->ire_uinfo; 2627 } 2628 } else { 2629 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2630 zoneid, tsl); 2631 if (ire != NULL) { 2632 ire_cacheable = B_TRUE; 2633 ire_uinfo = (ire_mp != NULL) ? 2634 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2635 &ire->ire_uinfo; 2636 2637 } else { 2638 if (ire_mp == NULL) { 2639 ire = ire_ftable_lookup( 2640 tcp->tcp_connp->conn_rem, 2641 0, 0, 0, NULL, &sire, zoneid, 0, 2642 tsl, (MATCH_IRE_RECURSIVE | 2643 MATCH_IRE_DEFAULT)); 2644 if (ire == NULL) 2645 return (0); 2646 ire_uinfo = (sire != NULL) ? 2647 &sire->ire_uinfo : 2648 &ire->ire_uinfo; 2649 } else { 2650 ire = (ire_t *)ire_mp->b_rptr; 2651 ire_uinfo = 2652 &((ire_t *) 2653 ire_mp->b_rptr)->ire_uinfo; 2654 } 2655 } 2656 } 2657 ASSERT(ire != NULL); 2658 2659 if ((ire->ire_src_addr == INADDR_ANY) || 2660 (ire->ire_type & IRE_BROADCAST)) { 2661 /* 2662 * ire->ire_mp is non null when ire_mp passed in is used 2663 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2664 */ 2665 if (ire->ire_mp == NULL) 2666 ire_refrele(ire); 2667 if (sire != NULL) 2668 ire_refrele(sire); 2669 return (0); 2670 } 2671 2672 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2673 ipaddr_t src_addr; 2674 2675 /* 2676 * ip_bind_connected() has stored the correct source 2677 * address in conn_src. 2678 */ 2679 src_addr = tcp->tcp_connp->conn_src; 2680 tcp->tcp_ipha->ipha_src = src_addr; 2681 /* 2682 * Copy of the src addr. in tcp_t is needed 2683 * for the lookup funcs. 2684 */ 2685 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2686 } 2687 /* 2688 * Set the fragment bit so that IP will tell us if the MTU 2689 * should change. IP tells us the latest setting of 2690 * ip_path_mtu_discovery through ire_frag_flag. 2691 */ 2692 if (ip_path_mtu_discovery) { 2693 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2694 htons(IPH_DF); 2695 } 2696 /* 2697 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2698 * for IP_NEXTHOP. No cache ire has been found for the 2699 * destination and we are working with the nexthop's 2700 * interface ire. Since we need to forward all packets 2701 * to the nexthop first, we "blindly" set tcp_localnet 2702 * to false, eventhough the destination may also be 2703 * onlink. 2704 */ 2705 if (ire_uinfo == NULL) 2706 tcp->tcp_localnet = 0; 2707 else 2708 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2709 } else { 2710 /* 2711 * For incoming connection ire_mp = NULL 2712 * For outgoing connection ire_mp != NULL 2713 * Technically we should check conn_incoming_ill 2714 * when ire_mp is NULL and conn_outgoing_ill when 2715 * ire_mp is non-NULL. But this is performance 2716 * critical path and for IPV*_BOUND_IF, outgoing 2717 * and incoming ill are always set to the same value. 2718 */ 2719 ill_t *dst_ill = NULL; 2720 ipif_t *dst_ipif = NULL; 2721 2722 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2723 2724 if (connp->conn_outgoing_ill != NULL) { 2725 /* Outgoing or incoming path */ 2726 int err; 2727 2728 dst_ill = conn_get_held_ill(connp, 2729 &connp->conn_outgoing_ill, &err); 2730 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2731 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2732 return (0); 2733 } 2734 match_flags |= MATCH_IRE_ILL; 2735 dst_ipif = dst_ill->ill_ipif; 2736 } 2737 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2738 0, 0, dst_ipif, zoneid, tsl, match_flags); 2739 2740 if (ire != NULL) { 2741 ire_cacheable = B_TRUE; 2742 ire_uinfo = (ire_mp != NULL) ? 2743 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2744 &ire->ire_uinfo; 2745 } else { 2746 if (ire_mp == NULL) { 2747 ire = ire_ftable_lookup_v6( 2748 &tcp->tcp_connp->conn_remv6, 2749 0, 0, 0, dst_ipif, &sire, zoneid, 2750 0, tsl, match_flags); 2751 if (ire == NULL) { 2752 if (dst_ill != NULL) 2753 ill_refrele(dst_ill); 2754 return (0); 2755 } 2756 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2757 &ire->ire_uinfo; 2758 } else { 2759 ire = (ire_t *)ire_mp->b_rptr; 2760 ire_uinfo = 2761 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2762 } 2763 } 2764 if (dst_ill != NULL) 2765 ill_refrele(dst_ill); 2766 2767 ASSERT(ire != NULL); 2768 ASSERT(ire_uinfo != NULL); 2769 2770 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2771 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2772 /* 2773 * ire->ire_mp is non null when ire_mp passed in is used 2774 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2775 */ 2776 if (ire->ire_mp == NULL) 2777 ire_refrele(ire); 2778 if (sire != NULL) 2779 ire_refrele(sire); 2780 return (0); 2781 } 2782 2783 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2784 in6_addr_t src_addr; 2785 2786 /* 2787 * ip_bind_connected_v6() has stored the correct source 2788 * address per IPv6 addr. selection policy in 2789 * conn_src_v6. 2790 */ 2791 src_addr = tcp->tcp_connp->conn_srcv6; 2792 2793 tcp->tcp_ip6h->ip6_src = src_addr; 2794 /* 2795 * Copy of the src addr. in tcp_t is needed 2796 * for the lookup funcs. 2797 */ 2798 tcp->tcp_ip_src_v6 = src_addr; 2799 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2800 &connp->conn_srcv6)); 2801 } 2802 tcp->tcp_localnet = 2803 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2804 } 2805 2806 /* 2807 * This allows applications to fail quickly when connections are made 2808 * to dead hosts. Hosts can be labeled dead by adding a reject route 2809 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2810 */ 2811 if ((ire->ire_flags & RTF_REJECT) && 2812 (ire->ire_flags & RTF_PRIVATE)) 2813 goto error; 2814 2815 /* 2816 * Make use of the cached rtt and rtt_sd values to calculate the 2817 * initial RTO. Note that they are already initialized in 2818 * tcp_init_values(). 2819 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2820 * IP_NEXTHOP, but instead are using the interface ire for the 2821 * nexthop, then we do not use the ire_uinfo from that ire to 2822 * do any initializations. 2823 */ 2824 if (ire_uinfo != NULL) { 2825 if (ire_uinfo->iulp_rtt != 0) { 2826 clock_t rto; 2827 2828 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2829 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2830 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2831 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5); 2832 2833 if (rto > tcp_rexmit_interval_max) { 2834 tcp->tcp_rto = tcp_rexmit_interval_max; 2835 } else if (rto < tcp_rexmit_interval_min) { 2836 tcp->tcp_rto = tcp_rexmit_interval_min; 2837 } else { 2838 tcp->tcp_rto = rto; 2839 } 2840 } 2841 if (ire_uinfo->iulp_ssthresh != 0) 2842 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2843 else 2844 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2845 if (ire_uinfo->iulp_spipe > 0) { 2846 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2847 tcp_max_buf); 2848 if (tcp_snd_lowat_fraction != 0) 2849 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2850 tcp_snd_lowat_fraction; 2851 (void) tcp_maxpsz_set(tcp, B_TRUE); 2852 } 2853 /* 2854 * Note that up till now, acceptor always inherits receive 2855 * window from the listener. But if there is a metrics 2856 * associated with a host, we should use that instead of 2857 * inheriting it from listener. Thus we need to pass this 2858 * info back to the caller. 2859 */ 2860 if (ire_uinfo->iulp_rpipe > 0) { 2861 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, tcp_max_buf); 2862 } 2863 2864 if (ire_uinfo->iulp_rtomax > 0) { 2865 tcp->tcp_second_timer_threshold = 2866 ire_uinfo->iulp_rtomax; 2867 } 2868 2869 /* 2870 * Use the metric option settings, iulp_tstamp_ok and 2871 * iulp_wscale_ok, only for active open. What this means 2872 * is that if the other side uses timestamp or window 2873 * scale option, TCP will also use those options. That 2874 * is for passive open. If the application sets a 2875 * large window, window scale is enabled regardless of 2876 * the value in iulp_wscale_ok. This is the behavior 2877 * since 2.6. So we keep it. 2878 * The only case left in passive open processing is the 2879 * check for SACK. 2880 * For ECN, it should probably be like SACK. But the 2881 * current value is binary, so we treat it like the other 2882 * cases. The metric only controls active open.For passive 2883 * open, the ndd param, tcp_ecn_permitted, controls the 2884 * behavior. 2885 */ 2886 if (!tcp_detached) { 2887 /* 2888 * The if check means that the following can only 2889 * be turned on by the metrics only IRE, but not off. 2890 */ 2891 if (ire_uinfo->iulp_tstamp_ok) 2892 tcp->tcp_snd_ts_ok = B_TRUE; 2893 if (ire_uinfo->iulp_wscale_ok) 2894 tcp->tcp_snd_ws_ok = B_TRUE; 2895 if (ire_uinfo->iulp_sack == 2) 2896 tcp->tcp_snd_sack_ok = B_TRUE; 2897 if (ire_uinfo->iulp_ecn_ok) 2898 tcp->tcp_ecn_ok = B_TRUE; 2899 } else { 2900 /* 2901 * Passive open. 2902 * 2903 * As above, the if check means that SACK can only be 2904 * turned on by the metric only IRE. 2905 */ 2906 if (ire_uinfo->iulp_sack > 0) { 2907 tcp->tcp_snd_sack_ok = B_TRUE; 2908 } 2909 } 2910 } 2911 2912 2913 /* 2914 * XXX: Note that currently, ire_max_frag can be as small as 68 2915 * because of PMTUd. So tcp_mss may go to negative if combined 2916 * length of all those options exceeds 28 bytes. But because 2917 * of the tcp_mss_min check below, we may not have a problem if 2918 * tcp_mss_min is of a reasonable value. The default is 1 so 2919 * the negative problem still exists. And the check defeats PMTUd. 2920 * In fact, if PMTUd finds that the MSS should be smaller than 2921 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2922 * value. 2923 * 2924 * We do not deal with that now. All those problems related to 2925 * PMTUd will be fixed later. 2926 */ 2927 ASSERT(ire->ire_max_frag != 0); 2928 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2929 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2930 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2931 mss = MIN(mss, IPV6_MIN_MTU); 2932 } 2933 } 2934 2935 /* Sanity check for MSS value. */ 2936 if (tcp->tcp_ipversion == IPV4_VERSION) 2937 mss_max = tcp_mss_max_ipv4; 2938 else 2939 mss_max = tcp_mss_max_ipv6; 2940 2941 if (tcp->tcp_ipversion == IPV6_VERSION && 2942 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2943 /* 2944 * After receiving an ICMPv6 "packet too big" message with a 2945 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2946 * will insert a 8-byte fragment header in every packet; we 2947 * reduce the MSS by that amount here. 2948 */ 2949 mss -= sizeof (ip6_frag_t); 2950 } 2951 2952 if (tcp->tcp_ipsec_overhead == 0) 2953 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2954 2955 mss -= tcp->tcp_ipsec_overhead; 2956 2957 if (mss < tcp_mss_min) 2958 mss = tcp_mss_min; 2959 if (mss > mss_max) 2960 mss = mss_max; 2961 2962 /* Note that this is the maximum MSS, excluding all options. */ 2963 tcp->tcp_mss = mss; 2964 2965 /* 2966 * Initialize the ISS here now that we have the full connection ID. 2967 * The RFC 1948 method of initial sequence number generation requires 2968 * knowledge of the full connection ID before setting the ISS. 2969 */ 2970 2971 tcp_iss_init(tcp); 2972 2973 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2974 tcp->tcp_loopback = B_TRUE; 2975 2976 if (tcp->tcp_ipversion == IPV4_VERSION) { 2977 hsp = tcp_hsp_lookup(tcp->tcp_remote); 2978 } else { 2979 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6); 2980 } 2981 2982 if (hsp != NULL) { 2983 /* Only modify if we're going to make them bigger */ 2984 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2985 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2986 if (tcp_snd_lowat_fraction != 0) 2987 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2988 tcp_snd_lowat_fraction; 2989 } 2990 2991 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2992 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2993 } 2994 2995 /* Copy timestamp flag only for active open */ 2996 if (!tcp_detached) 2997 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2998 } 2999 3000 if (sire != NULL) 3001 IRE_REFRELE(sire); 3002 3003 /* 3004 * If we got an IRE_CACHE and an ILL, go through their properties; 3005 * otherwise, this is deferred until later when we have an IRE_CACHE. 3006 */ 3007 if (tcp->tcp_loopback || 3008 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 3009 /* 3010 * For incoming, see if this tcp may be MDT-capable. For 3011 * outgoing, this process has been taken care of through 3012 * tcp_rput_other. 3013 */ 3014 tcp_ire_ill_check(tcp, ire, ill, incoming); 3015 tcp->tcp_ire_ill_check_done = B_TRUE; 3016 } 3017 3018 mutex_enter(&connp->conn_lock); 3019 /* 3020 * Make sure that conn is not marked incipient 3021 * for incoming connections. A blind 3022 * removal of incipient flag is cheaper than 3023 * check and removal. 3024 */ 3025 connp->conn_state_flags &= ~CONN_INCIPIENT; 3026 3027 /* Must not cache forwarding table routes. */ 3028 if (ire_cacheable) { 3029 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 3030 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 3031 connp->conn_ire_cache = ire; 3032 IRE_UNTRACE_REF(ire); 3033 rw_exit(&ire->ire_bucket->irb_lock); 3034 mutex_exit(&connp->conn_lock); 3035 return (1); 3036 } 3037 rw_exit(&ire->ire_bucket->irb_lock); 3038 } 3039 mutex_exit(&connp->conn_lock); 3040 3041 if (ire->ire_mp == NULL) 3042 ire_refrele(ire); 3043 return (1); 3044 3045 error: 3046 if (ire->ire_mp == NULL) 3047 ire_refrele(ire); 3048 if (sire != NULL) 3049 ire_refrele(sire); 3050 return (0); 3051 } 3052 3053 /* 3054 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 3055 * O_T_BIND_REQ/T_BIND_REQ message. 3056 */ 3057 static void 3058 tcp_bind(tcp_t *tcp, mblk_t *mp) 3059 { 3060 sin_t *sin; 3061 sin6_t *sin6; 3062 mblk_t *mp1; 3063 in_port_t requested_port; 3064 in_port_t allocated_port; 3065 struct T_bind_req *tbr; 3066 boolean_t bind_to_req_port_only; 3067 boolean_t backlog_update = B_FALSE; 3068 boolean_t user_specified; 3069 in6_addr_t v6addr; 3070 ipaddr_t v4addr; 3071 uint_t origipversion; 3072 int err; 3073 queue_t *q = tcp->tcp_wq; 3074 conn_t *connp; 3075 mlp_type_t addrtype, mlptype; 3076 zone_t *zone; 3077 cred_t *cr; 3078 in_port_t mlp_port; 3079 3080 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3081 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3082 if (tcp->tcp_debug) { 3083 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3084 "tcp_bind: bad req, len %u", 3085 (uint_t)(mp->b_wptr - mp->b_rptr)); 3086 } 3087 tcp_err_ack(tcp, mp, TPROTO, 0); 3088 return; 3089 } 3090 /* Make sure the largest address fits */ 3091 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3092 if (mp1 == NULL) { 3093 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3094 return; 3095 } 3096 mp = mp1; 3097 tbr = (struct T_bind_req *)mp->b_rptr; 3098 if (tcp->tcp_state >= TCPS_BOUND) { 3099 if ((tcp->tcp_state == TCPS_BOUND || 3100 tcp->tcp_state == TCPS_LISTEN) && 3101 tcp->tcp_conn_req_max != tbr->CONIND_number && 3102 tbr->CONIND_number > 0) { 3103 /* 3104 * Handle listen() increasing CONIND_number. 3105 * This is more "liberal" then what the TPI spec 3106 * requires but is needed to avoid a t_unbind 3107 * when handling listen() since the port number 3108 * might be "stolen" between the unbind and bind. 3109 */ 3110 backlog_update = B_TRUE; 3111 goto do_bind; 3112 } 3113 if (tcp->tcp_debug) { 3114 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3115 "tcp_bind: bad state, %d", tcp->tcp_state); 3116 } 3117 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3118 return; 3119 } 3120 origipversion = tcp->tcp_ipversion; 3121 3122 switch (tbr->ADDR_length) { 3123 case 0: /* request for a generic port */ 3124 tbr->ADDR_offset = sizeof (struct T_bind_req); 3125 if (tcp->tcp_family == AF_INET) { 3126 tbr->ADDR_length = sizeof (sin_t); 3127 sin = (sin_t *)&tbr[1]; 3128 *sin = sin_null; 3129 sin->sin_family = AF_INET; 3130 mp->b_wptr = (uchar_t *)&sin[1]; 3131 tcp->tcp_ipversion = IPV4_VERSION; 3132 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3133 } else { 3134 ASSERT(tcp->tcp_family == AF_INET6); 3135 tbr->ADDR_length = sizeof (sin6_t); 3136 sin6 = (sin6_t *)&tbr[1]; 3137 *sin6 = sin6_null; 3138 sin6->sin6_family = AF_INET6; 3139 mp->b_wptr = (uchar_t *)&sin6[1]; 3140 tcp->tcp_ipversion = IPV6_VERSION; 3141 V6_SET_ZERO(v6addr); 3142 } 3143 requested_port = 0; 3144 break; 3145 3146 case sizeof (sin_t): /* Complete IPv4 address */ 3147 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3148 sizeof (sin_t)); 3149 if (sin == NULL || !OK_32PTR((char *)sin)) { 3150 if (tcp->tcp_debug) { 3151 (void) strlog(TCP_MOD_ID, 0, 1, 3152 SL_ERROR|SL_TRACE, 3153 "tcp_bind: bad address parameter, " 3154 "offset %d, len %d", 3155 tbr->ADDR_offset, tbr->ADDR_length); 3156 } 3157 tcp_err_ack(tcp, mp, TPROTO, 0); 3158 return; 3159 } 3160 /* 3161 * With sockets sockfs will accept bogus sin_family in 3162 * bind() and replace it with the family used in the socket 3163 * call. 3164 */ 3165 if (sin->sin_family != AF_INET || 3166 tcp->tcp_family != AF_INET) { 3167 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3168 return; 3169 } 3170 requested_port = ntohs(sin->sin_port); 3171 tcp->tcp_ipversion = IPV4_VERSION; 3172 v4addr = sin->sin_addr.s_addr; 3173 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3174 break; 3175 3176 case sizeof (sin6_t): /* Complete IPv6 address */ 3177 sin6 = (sin6_t *)mi_offset_param(mp, 3178 tbr->ADDR_offset, sizeof (sin6_t)); 3179 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3180 if (tcp->tcp_debug) { 3181 (void) strlog(TCP_MOD_ID, 0, 1, 3182 SL_ERROR|SL_TRACE, 3183 "tcp_bind: bad IPv6 address parameter, " 3184 "offset %d, len %d", tbr->ADDR_offset, 3185 tbr->ADDR_length); 3186 } 3187 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3188 return; 3189 } 3190 if (sin6->sin6_family != AF_INET6 || 3191 tcp->tcp_family != AF_INET6) { 3192 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3193 return; 3194 } 3195 requested_port = ntohs(sin6->sin6_port); 3196 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3197 IPV4_VERSION : IPV6_VERSION; 3198 v6addr = sin6->sin6_addr; 3199 break; 3200 3201 default: 3202 if (tcp->tcp_debug) { 3203 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3204 "tcp_bind: bad address length, %d", 3205 tbr->ADDR_length); 3206 } 3207 tcp_err_ack(tcp, mp, TBADADDR, 0); 3208 return; 3209 } 3210 tcp->tcp_bound_source_v6 = v6addr; 3211 3212 /* Check for change in ipversion */ 3213 if (origipversion != tcp->tcp_ipversion) { 3214 ASSERT(tcp->tcp_family == AF_INET6); 3215 err = tcp->tcp_ipversion == IPV6_VERSION ? 3216 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3217 if (err) { 3218 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3219 return; 3220 } 3221 } 3222 3223 /* 3224 * Initialize family specific fields. Copy of the src addr. 3225 * in tcp_t is needed for the lookup funcs. 3226 */ 3227 if (tcp->tcp_ipversion == IPV6_VERSION) { 3228 tcp->tcp_ip6h->ip6_src = v6addr; 3229 } else { 3230 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3231 } 3232 tcp->tcp_ip_src_v6 = v6addr; 3233 3234 /* 3235 * For O_T_BIND_REQ: 3236 * Verify that the target port/addr is available, or choose 3237 * another. 3238 * For T_BIND_REQ: 3239 * Verify that the target port/addr is available or fail. 3240 * In both cases when it succeeds the tcp is inserted in the 3241 * bind hash table. This ensures that the operation is atomic 3242 * under the lock on the hash bucket. 3243 */ 3244 bind_to_req_port_only = requested_port != 0 && 3245 tbr->PRIM_type != O_T_BIND_REQ; 3246 /* 3247 * Get a valid port (within the anonymous range and should not 3248 * be a privileged one) to use if the user has not given a port. 3249 * If multiple threads are here, they may all start with 3250 * with the same initial port. But, it should be fine as long as 3251 * tcp_bindi will ensure that no two threads will be assigned 3252 * the same port. 3253 * 3254 * NOTE: XXX If a privileged process asks for an anonymous port, we 3255 * still check for ports only in the range > tcp_smallest_non_priv_port, 3256 * unless TCP_ANONPRIVBIND option is set. 3257 */ 3258 mlptype = mlptSingle; 3259 mlp_port = requested_port; 3260 if (requested_port == 0) { 3261 requested_port = tcp->tcp_anon_priv_bind ? 3262 tcp_get_next_priv_port(tcp) : 3263 tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 3264 if (requested_port == 0) { 3265 tcp_err_ack(tcp, mp, TNOADDR, 0); 3266 return; 3267 } 3268 user_specified = B_FALSE; 3269 3270 /* 3271 * If the user went through one of the RPC interfaces to create 3272 * this socket and RPC is MLP in this zone, then give him an 3273 * anonymous MLP. 3274 */ 3275 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3276 connp = tcp->tcp_connp; 3277 if (connp->conn_anon_mlp && is_system_labeled()) { 3278 zone = crgetzone(cr); 3279 addrtype = tsol_mlp_addr_type(zone->zone_id, 3280 IPV6_VERSION, &v6addr); 3281 if (addrtype == mlptSingle) { 3282 tcp_err_ack(tcp, mp, TNOADDR, 0); 3283 return; 3284 } 3285 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3286 PMAPPORT, addrtype); 3287 mlp_port = PMAPPORT; 3288 } 3289 } else { 3290 int i; 3291 boolean_t priv = B_FALSE; 3292 3293 /* 3294 * If the requested_port is in the well-known privileged range, 3295 * verify that the stream was opened by a privileged user. 3296 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3297 * but instead the code relies on: 3298 * - the fact that the address of the array and its size never 3299 * changes 3300 * - the atomic assignment of the elements of the array 3301 */ 3302 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3303 if (requested_port < tcp_smallest_nonpriv_port) { 3304 priv = B_TRUE; 3305 } else { 3306 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 3307 if (requested_port == 3308 tcp_g_epriv_ports[i]) { 3309 priv = B_TRUE; 3310 break; 3311 } 3312 } 3313 } 3314 if (priv) { 3315 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3316 if (tcp->tcp_debug) { 3317 (void) strlog(TCP_MOD_ID, 0, 1, 3318 SL_ERROR|SL_TRACE, 3319 "tcp_bind: no priv for port %d", 3320 requested_port); 3321 } 3322 tcp_err_ack(tcp, mp, TACCES, 0); 3323 return; 3324 } 3325 } 3326 user_specified = B_TRUE; 3327 3328 connp = tcp->tcp_connp; 3329 if (is_system_labeled()) { 3330 zone = crgetzone(cr); 3331 addrtype = tsol_mlp_addr_type(zone->zone_id, 3332 IPV6_VERSION, &v6addr); 3333 if (addrtype == mlptSingle) { 3334 tcp_err_ack(tcp, mp, TNOADDR, 0); 3335 return; 3336 } 3337 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3338 requested_port, addrtype); 3339 } 3340 } 3341 3342 if (mlptype != mlptSingle) { 3343 if (secpolicy_net_bindmlp(cr) != 0) { 3344 if (tcp->tcp_debug) { 3345 (void) strlog(TCP_MOD_ID, 0, 1, 3346 SL_ERROR|SL_TRACE, 3347 "tcp_bind: no priv for multilevel port %d", 3348 requested_port); 3349 } 3350 tcp_err_ack(tcp, mp, TACCES, 0); 3351 return; 3352 } 3353 3354 /* 3355 * If we're specifically binding a shared IP address and the 3356 * port is MLP on shared addresses, then check to see if this 3357 * zone actually owns the MLP. Reject if not. 3358 */ 3359 if (mlptype == mlptShared && addrtype == mlptShared) { 3360 zoneid_t mlpzone; 3361 3362 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3363 htons(mlp_port)); 3364 if (connp->conn_zoneid != mlpzone) { 3365 if (tcp->tcp_debug) { 3366 (void) strlog(TCP_MOD_ID, 0, 1, 3367 SL_ERROR|SL_TRACE, 3368 "tcp_bind: attempt to bind port " 3369 "%d on shared addr in zone %d " 3370 "(should be %d)", 3371 mlp_port, connp->conn_zoneid, 3372 mlpzone); 3373 } 3374 tcp_err_ack(tcp, mp, TACCES, 0); 3375 return; 3376 } 3377 } 3378 3379 if (!user_specified) { 3380 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3381 requested_port, B_TRUE); 3382 if (err != 0) { 3383 if (tcp->tcp_debug) { 3384 (void) strlog(TCP_MOD_ID, 0, 1, 3385 SL_ERROR|SL_TRACE, 3386 "tcp_bind: cannot establish anon " 3387 "MLP for port %d", 3388 requested_port); 3389 } 3390 tcp_err_ack(tcp, mp, TSYSERR, err); 3391 return; 3392 } 3393 connp->conn_anon_port = B_TRUE; 3394 } 3395 connp->conn_mlp_type = mlptype; 3396 } 3397 3398 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3399 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3400 3401 if (allocated_port == 0) { 3402 connp->conn_mlp_type = mlptSingle; 3403 if (connp->conn_anon_port) { 3404 connp->conn_anon_port = B_FALSE; 3405 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3406 requested_port, B_FALSE); 3407 } 3408 if (bind_to_req_port_only) { 3409 if (tcp->tcp_debug) { 3410 (void) strlog(TCP_MOD_ID, 0, 1, 3411 SL_ERROR|SL_TRACE, 3412 "tcp_bind: requested addr busy"); 3413 } 3414 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3415 } else { 3416 /* If we are out of ports, fail the bind. */ 3417 if (tcp->tcp_debug) { 3418 (void) strlog(TCP_MOD_ID, 0, 1, 3419 SL_ERROR|SL_TRACE, 3420 "tcp_bind: out of ports?"); 3421 } 3422 tcp_err_ack(tcp, mp, TNOADDR, 0); 3423 } 3424 return; 3425 } 3426 ASSERT(tcp->tcp_state == TCPS_BOUND); 3427 do_bind: 3428 if (!backlog_update) { 3429 if (tcp->tcp_family == AF_INET) 3430 sin->sin_port = htons(allocated_port); 3431 else 3432 sin6->sin6_port = htons(allocated_port); 3433 } 3434 if (tcp->tcp_family == AF_INET) { 3435 if (tbr->CONIND_number != 0) { 3436 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3437 sizeof (sin_t)); 3438 } else { 3439 /* Just verify the local IP address */ 3440 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3441 } 3442 } else { 3443 if (tbr->CONIND_number != 0) { 3444 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3445 sizeof (sin6_t)); 3446 } else { 3447 /* Just verify the local IP address */ 3448 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3449 IPV6_ADDR_LEN); 3450 } 3451 } 3452 if (mp1 == NULL) { 3453 if (connp->conn_anon_port) { 3454 connp->conn_anon_port = B_FALSE; 3455 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3456 requested_port, B_FALSE); 3457 } 3458 connp->conn_mlp_type = mlptSingle; 3459 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3460 return; 3461 } 3462 3463 tbr->PRIM_type = T_BIND_ACK; 3464 mp->b_datap->db_type = M_PCPROTO; 3465 3466 /* Chain in the reply mp for tcp_rput() */ 3467 mp1->b_cont = mp; 3468 mp = mp1; 3469 3470 tcp->tcp_conn_req_max = tbr->CONIND_number; 3471 if (tcp->tcp_conn_req_max) { 3472 if (tcp->tcp_conn_req_max < tcp_conn_req_min) 3473 tcp->tcp_conn_req_max = tcp_conn_req_min; 3474 if (tcp->tcp_conn_req_max > tcp_conn_req_max_q) 3475 tcp->tcp_conn_req_max = tcp_conn_req_max_q; 3476 /* 3477 * If this is a listener, do not reset the eager list 3478 * and other stuffs. Note that we don't check if the 3479 * existing eager list meets the new tcp_conn_req_max 3480 * requirement. 3481 */ 3482 if (tcp->tcp_state != TCPS_LISTEN) { 3483 tcp->tcp_state = TCPS_LISTEN; 3484 /* Initialize the chain. Don't need the eager_lock */ 3485 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3486 tcp->tcp_eager_next_drop_q0 = tcp; 3487 tcp->tcp_eager_prev_drop_q0 = tcp; 3488 tcp->tcp_second_ctimer_threshold = 3489 tcp_ip_abort_linterval; 3490 } 3491 } 3492 3493 /* 3494 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3495 * processing continues in tcp_rput_other(). 3496 */ 3497 if (tcp->tcp_family == AF_INET6) { 3498 ASSERT(tcp->tcp_connp->conn_af_isv6); 3499 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3500 } else { 3501 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3502 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3503 } 3504 /* 3505 * If the bind cannot complete immediately 3506 * IP will arrange to call tcp_rput_other 3507 * when the bind completes. 3508 */ 3509 if (mp != NULL) { 3510 tcp_rput_other(tcp, mp); 3511 } else { 3512 /* 3513 * Bind will be resumed later. Need to ensure 3514 * that conn doesn't disappear when that happens. 3515 * This will be decremented in ip_resume_tcp_bind(). 3516 */ 3517 CONN_INC_REF(tcp->tcp_connp); 3518 } 3519 } 3520 3521 3522 /* 3523 * If the "bind_to_req_port_only" parameter is set, if the requested port 3524 * number is available, return it, If not return 0 3525 * 3526 * If "bind_to_req_port_only" parameter is not set and 3527 * If the requested port number is available, return it. If not, return 3528 * the first anonymous port we happen across. If no anonymous ports are 3529 * available, return 0. addr is the requested local address, if any. 3530 * 3531 * In either case, when succeeding update the tcp_t to record the port number 3532 * and insert it in the bind hash table. 3533 * 3534 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3535 * without setting SO_REUSEADDR. This is needed so that they 3536 * can be viewed as two independent transport protocols. 3537 */ 3538 static in_port_t 3539 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3540 int reuseaddr, boolean_t quick_connect, 3541 boolean_t bind_to_req_port_only, boolean_t user_specified) 3542 { 3543 /* number of times we have run around the loop */ 3544 int count = 0; 3545 /* maximum number of times to run around the loop */ 3546 int loopmax; 3547 conn_t *connp = tcp->tcp_connp; 3548 zoneid_t zoneid = connp->conn_zoneid; 3549 3550 /* 3551 * Lookup for free addresses is done in a loop and "loopmax" 3552 * influences how long we spin in the loop 3553 */ 3554 if (bind_to_req_port_only) { 3555 /* 3556 * If the requested port is busy, don't bother to look 3557 * for a new one. Setting loop maximum count to 1 has 3558 * that effect. 3559 */ 3560 loopmax = 1; 3561 } else { 3562 /* 3563 * If the requested port is busy, look for a free one 3564 * in the anonymous port range. 3565 * Set loopmax appropriately so that one does not look 3566 * forever in the case all of the anonymous ports are in use. 3567 */ 3568 if (tcp->tcp_anon_priv_bind) { 3569 /* 3570 * loopmax = 3571 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3572 */ 3573 loopmax = IPPORT_RESERVED - tcp_min_anonpriv_port; 3574 } else { 3575 loopmax = (tcp_largest_anon_port - 3576 tcp_smallest_anon_port + 1); 3577 } 3578 } 3579 do { 3580 uint16_t lport; 3581 tf_t *tbf; 3582 tcp_t *ltcp; 3583 conn_t *lconnp; 3584 3585 lport = htons(port); 3586 3587 /* 3588 * Ensure that the tcp_t is not currently in the bind hash. 3589 * Hold the lock on the hash bucket to ensure that 3590 * the duplicate check plus the insertion is an atomic 3591 * operation. 3592 * 3593 * This function does an inline lookup on the bind hash list 3594 * Make sure that we access only members of tcp_t 3595 * and that we don't look at tcp_tcp, since we are not 3596 * doing a CONN_INC_REF. 3597 */ 3598 tcp_bind_hash_remove(tcp); 3599 tbf = &tcp_bind_fanout[TCP_BIND_HASH(lport)]; 3600 mutex_enter(&tbf->tf_lock); 3601 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3602 ltcp = ltcp->tcp_bind_hash) { 3603 boolean_t not_socket; 3604 boolean_t exclbind; 3605 3606 if (lport != ltcp->tcp_lport) 3607 continue; 3608 3609 lconnp = ltcp->tcp_connp; 3610 3611 /* 3612 * On a labeled system, we must treat bindings to ports 3613 * on shared IP addresses by sockets with MAC exemption 3614 * privilege as being in all zones, as there's 3615 * otherwise no way to identify the right receiver. 3616 */ 3617 if (!IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) && 3618 !lconnp->conn_mac_exempt && 3619 !connp->conn_mac_exempt) 3620 continue; 3621 3622 /* 3623 * If TCP_EXCLBIND is set for either the bound or 3624 * binding endpoint, the semantics of bind 3625 * is changed according to the following. 3626 * 3627 * spec = specified address (v4 or v6) 3628 * unspec = unspecified address (v4 or v6) 3629 * A = specified addresses are different for endpoints 3630 * 3631 * bound bind to allowed 3632 * ------------------------------------- 3633 * unspec unspec no 3634 * unspec spec no 3635 * spec unspec no 3636 * spec spec yes if A 3637 * 3638 * For labeled systems, SO_MAC_EXEMPT behaves the same 3639 * as TCP_EXCLBIND, except that zoneid is ignored. 3640 * 3641 * Note: 3642 * 3643 * 1. Because of TLI semantics, an endpoint can go 3644 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3645 * TCPS_BOUND, depending on whether it is originally 3646 * a listener or not. That is why we need to check 3647 * for states greater than or equal to TCPS_BOUND 3648 * here. 3649 * 3650 * 2. Ideally, we should only check for state equals 3651 * to TCPS_LISTEN. And the following check should be 3652 * added. 3653 * 3654 * if (ltcp->tcp_state == TCPS_LISTEN || 3655 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3656 * ... 3657 * } 3658 * 3659 * The semantics will be changed to this. If the 3660 * endpoint on the list is in state not equal to 3661 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3662 * set, let the bind succeed. 3663 * 3664 * Because of (1), we cannot do that for TLI 3665 * endpoints. But we can do that for socket endpoints. 3666 * If in future, we can change this going back 3667 * semantics, we can use the above check for TLI also. 3668 */ 3669 not_socket = !(TCP_IS_SOCKET(ltcp) && 3670 TCP_IS_SOCKET(tcp)); 3671 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3672 3673 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3674 (exclbind && (not_socket || 3675 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3676 if (V6_OR_V4_INADDR_ANY( 3677 ltcp->tcp_bound_source_v6) || 3678 V6_OR_V4_INADDR_ANY(*laddr) || 3679 IN6_ARE_ADDR_EQUAL(laddr, 3680 <cp->tcp_bound_source_v6)) { 3681 break; 3682 } 3683 continue; 3684 } 3685 3686 /* 3687 * Check ipversion to allow IPv4 and IPv6 sockets to 3688 * have disjoint port number spaces, if *_EXCLBIND 3689 * is not set and only if the application binds to a 3690 * specific port. We use the same autoassigned port 3691 * number space for IPv4 and IPv6 sockets. 3692 */ 3693 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3694 bind_to_req_port_only) 3695 continue; 3696 3697 /* 3698 * Ideally, we should make sure that the source 3699 * address, remote address, and remote port in the 3700 * four tuple for this tcp-connection is unique. 3701 * However, trying to find out the local source 3702 * address would require too much code duplication 3703 * with IP, since IP needs needs to have that code 3704 * to support userland TCP implementations. 3705 */ 3706 if (quick_connect && 3707 (ltcp->tcp_state > TCPS_LISTEN) && 3708 ((tcp->tcp_fport != ltcp->tcp_fport) || 3709 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3710 <cp->tcp_remote_v6))) 3711 continue; 3712 3713 if (!reuseaddr) { 3714 /* 3715 * No socket option SO_REUSEADDR. 3716 * If existing port is bound to 3717 * a non-wildcard IP address 3718 * and the requesting stream is 3719 * bound to a distinct 3720 * different IP addresses 3721 * (non-wildcard, also), keep 3722 * going. 3723 */ 3724 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3725 !V6_OR_V4_INADDR_ANY( 3726 ltcp->tcp_bound_source_v6) && 3727 !IN6_ARE_ADDR_EQUAL(laddr, 3728 <cp->tcp_bound_source_v6)) 3729 continue; 3730 if (ltcp->tcp_state >= TCPS_BOUND) { 3731 /* 3732 * This port is being used and 3733 * its state is >= TCPS_BOUND, 3734 * so we can't bind to it. 3735 */ 3736 break; 3737 } 3738 } else { 3739 /* 3740 * socket option SO_REUSEADDR is set on the 3741 * binding tcp_t. 3742 * 3743 * If two streams are bound to 3744 * same IP address or both addr 3745 * and bound source are wildcards 3746 * (INADDR_ANY), we want to stop 3747 * searching. 3748 * We have found a match of IP source 3749 * address and source port, which is 3750 * refused regardless of the 3751 * SO_REUSEADDR setting, so we break. 3752 */ 3753 if (IN6_ARE_ADDR_EQUAL(laddr, 3754 <cp->tcp_bound_source_v6) && 3755 (ltcp->tcp_state == TCPS_LISTEN || 3756 ltcp->tcp_state == TCPS_BOUND)) 3757 break; 3758 } 3759 } 3760 if (ltcp != NULL) { 3761 /* The port number is busy */ 3762 mutex_exit(&tbf->tf_lock); 3763 } else { 3764 /* 3765 * This port is ours. Insert in fanout and mark as 3766 * bound to prevent others from getting the port 3767 * number. 3768 */ 3769 tcp->tcp_state = TCPS_BOUND; 3770 tcp->tcp_lport = htons(port); 3771 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3772 3773 ASSERT(&tcp_bind_fanout[TCP_BIND_HASH( 3774 tcp->tcp_lport)] == tbf); 3775 tcp_bind_hash_insert(tbf, tcp, 1); 3776 3777 mutex_exit(&tbf->tf_lock); 3778 3779 /* 3780 * We don't want tcp_next_port_to_try to "inherit" 3781 * a port number supplied by the user in a bind. 3782 */ 3783 if (user_specified) 3784 return (port); 3785 3786 /* 3787 * This is the only place where tcp_next_port_to_try 3788 * is updated. After the update, it may or may not 3789 * be in the valid range. 3790 */ 3791 if (!tcp->tcp_anon_priv_bind) 3792 tcp_next_port_to_try = port + 1; 3793 return (port); 3794 } 3795 3796 if (tcp->tcp_anon_priv_bind) { 3797 port = tcp_get_next_priv_port(tcp); 3798 } else { 3799 if (count == 0 && user_specified) { 3800 /* 3801 * We may have to return an anonymous port. So 3802 * get one to start with. 3803 */ 3804 port = 3805 tcp_update_next_port(tcp_next_port_to_try, 3806 tcp, B_TRUE); 3807 user_specified = B_FALSE; 3808 } else { 3809 port = tcp_update_next_port(port + 1, tcp, 3810 B_FALSE); 3811 } 3812 } 3813 if (port == 0) 3814 break; 3815 3816 /* 3817 * Don't let this loop run forever in the case where 3818 * all of the anonymous ports are in use. 3819 */ 3820 } while (++count < loopmax); 3821 return (0); 3822 } 3823 3824 /* 3825 * tcp_clean_death / tcp_close_detached must not be called more than once 3826 * on a tcp. Thus every function that potentially calls tcp_clean_death 3827 * must check for the tcp state before calling tcp_clean_death. 3828 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3829 * tcp_timer_handler, all check for the tcp state. 3830 */ 3831 /* ARGSUSED */ 3832 void 3833 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3834 { 3835 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3836 3837 freemsg(mp); 3838 if (tcp->tcp_state > TCPS_BOUND) 3839 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, ETIMEDOUT, 5); 3840 } 3841 3842 /* 3843 * We are dying for some reason. Try to do it gracefully. (May be called 3844 * as writer.) 3845 * 3846 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3847 * done by a service procedure). 3848 * TBD - Should the return value distinguish between the tcp_t being 3849 * freed and it being reinitialized? 3850 */ 3851 static int 3852 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3853 { 3854 mblk_t *mp; 3855 queue_t *q; 3856 3857 TCP_CLD_STAT(tag); 3858 3859 #if TCP_TAG_CLEAN_DEATH 3860 tcp->tcp_cleandeathtag = tag; 3861 #endif 3862 3863 if (tcp->tcp_fused) 3864 tcp_unfuse(tcp); 3865 3866 if (tcp->tcp_linger_tid != 0 && 3867 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3868 tcp_stop_lingering(tcp); 3869 } 3870 3871 ASSERT(tcp != NULL); 3872 ASSERT((tcp->tcp_family == AF_INET && 3873 tcp->tcp_ipversion == IPV4_VERSION) || 3874 (tcp->tcp_family == AF_INET6 && 3875 (tcp->tcp_ipversion == IPV4_VERSION || 3876 tcp->tcp_ipversion == IPV6_VERSION))); 3877 3878 if (TCP_IS_DETACHED(tcp)) { 3879 if (tcp->tcp_hard_binding) { 3880 /* 3881 * Its an eager that we are dealing with. We close the 3882 * eager but in case a conn_ind has already gone to the 3883 * listener, let tcp_accept_finish() send a discon_ind 3884 * to the listener and drop the last reference. If the 3885 * listener doesn't even know about the eager i.e. the 3886 * conn_ind hasn't gone up, blow away the eager and drop 3887 * the last reference as well. If the conn_ind has gone 3888 * up, state should be BOUND. tcp_accept_finish 3889 * will figure out that the connection has received a 3890 * RST and will send a DISCON_IND to the application. 3891 */ 3892 tcp_closei_local(tcp); 3893 if (!tcp->tcp_tconnind_started) { 3894 CONN_DEC_REF(tcp->tcp_connp); 3895 } else { 3896 tcp->tcp_state = TCPS_BOUND; 3897 } 3898 } else { 3899 tcp_close_detached(tcp); 3900 } 3901 return (0); 3902 } 3903 3904 TCP_STAT(tcp_clean_death_nondetached); 3905 3906 /* 3907 * If T_ORDREL_IND has not been sent yet (done when service routine 3908 * is run) postpone cleaning up the endpoint until service routine 3909 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3910 * client_errno since tcp_close uses the client_errno field. 3911 */ 3912 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3913 if (err != 0) 3914 tcp->tcp_client_errno = err; 3915 3916 tcp->tcp_deferred_clean_death = B_TRUE; 3917 return (-1); 3918 } 3919 3920 q = tcp->tcp_rq; 3921 3922 /* Trash all inbound data */ 3923 flushq(q, FLUSHALL); 3924 3925 /* 3926 * If we are at least part way open and there is error 3927 * (err==0 implies no error) 3928 * notify our client by a T_DISCON_IND. 3929 */ 3930 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3931 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3932 !TCP_IS_SOCKET(tcp)) { 3933 /* 3934 * Send M_FLUSH according to TPI. Because sockets will 3935 * (and must) ignore FLUSHR we do that only for TPI 3936 * endpoints and sockets in STREAMS mode. 3937 */ 3938 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3939 } 3940 if (tcp->tcp_debug) { 3941 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3942 "tcp_clean_death: discon err %d", err); 3943 } 3944 mp = mi_tpi_discon_ind(NULL, err, 0); 3945 if (mp != NULL) { 3946 putnext(q, mp); 3947 } else { 3948 if (tcp->tcp_debug) { 3949 (void) strlog(TCP_MOD_ID, 0, 1, 3950 SL_ERROR|SL_TRACE, 3951 "tcp_clean_death, sending M_ERROR"); 3952 } 3953 (void) putnextctl1(q, M_ERROR, EPROTO); 3954 } 3955 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3956 /* SYN_SENT or SYN_RCVD */ 3957 BUMP_MIB(&tcp_mib, tcpAttemptFails); 3958 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3959 /* ESTABLISHED or CLOSE_WAIT */ 3960 BUMP_MIB(&tcp_mib, tcpEstabResets); 3961 } 3962 } 3963 3964 tcp_reinit(tcp); 3965 return (-1); 3966 } 3967 3968 /* 3969 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3970 * to expire, stop the wait and finish the close. 3971 */ 3972 static void 3973 tcp_stop_lingering(tcp_t *tcp) 3974 { 3975 clock_t delta = 0; 3976 3977 tcp->tcp_linger_tid = 0; 3978 if (tcp->tcp_state > TCPS_LISTEN) { 3979 tcp_acceptor_hash_remove(tcp); 3980 if (tcp->tcp_flow_stopped) { 3981 tcp_clrqfull(tcp); 3982 } 3983 3984 if (tcp->tcp_timer_tid != 0) { 3985 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3986 tcp->tcp_timer_tid = 0; 3987 } 3988 /* 3989 * Need to cancel those timers which will not be used when 3990 * TCP is detached. This has to be done before the tcp_wq 3991 * is set to the global queue. 3992 */ 3993 tcp_timers_stop(tcp); 3994 3995 3996 tcp->tcp_detached = B_TRUE; 3997 tcp->tcp_rq = tcp_g_q; 3998 tcp->tcp_wq = WR(tcp_g_q); 3999 4000 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4001 tcp_time_wait_append(tcp); 4002 TCP_DBGSTAT(tcp_detach_time_wait); 4003 goto finish; 4004 } 4005 4006 /* 4007 * If delta is zero the timer event wasn't executed and was 4008 * successfully canceled. In this case we need to restart it 4009 * with the minimal delta possible. 4010 */ 4011 if (delta >= 0) { 4012 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4013 delta ? delta : 1); 4014 } 4015 } else { 4016 tcp_closei_local(tcp); 4017 CONN_DEC_REF(tcp->tcp_connp); 4018 } 4019 finish: 4020 /* Signal closing thread that it can complete close */ 4021 mutex_enter(&tcp->tcp_closelock); 4022 tcp->tcp_detached = B_TRUE; 4023 tcp->tcp_rq = tcp_g_q; 4024 tcp->tcp_wq = WR(tcp_g_q); 4025 tcp->tcp_closed = 1; 4026 cv_signal(&tcp->tcp_closecv); 4027 mutex_exit(&tcp->tcp_closelock); 4028 } 4029 4030 /* 4031 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 4032 * expires. 4033 */ 4034 static void 4035 tcp_close_linger_timeout(void *arg) 4036 { 4037 conn_t *connp = (conn_t *)arg; 4038 tcp_t *tcp = connp->conn_tcp; 4039 4040 tcp->tcp_client_errno = ETIMEDOUT; 4041 tcp_stop_lingering(tcp); 4042 } 4043 4044 static int 4045 tcp_close(queue_t *q, int flags) 4046 { 4047 conn_t *connp = Q_TO_CONN(q); 4048 tcp_t *tcp = connp->conn_tcp; 4049 mblk_t *mp = &tcp->tcp_closemp; 4050 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 4051 boolean_t linger_interrupted = B_FALSE; 4052 mblk_t *bp; 4053 4054 ASSERT(WR(q)->q_next == NULL); 4055 ASSERT(connp->conn_ref >= 2); 4056 ASSERT((connp->conn_flags & IPCL_TCPMOD) == 0); 4057 4058 /* 4059 * We are being closed as /dev/tcp or /dev/tcp6. 4060 * 4061 * Mark the conn as closing. ill_pending_mp_add will not 4062 * add any mp to the pending mp list, after this conn has 4063 * started closing. Same for sq_pending_mp_add 4064 */ 4065 mutex_enter(&connp->conn_lock); 4066 connp->conn_state_flags |= CONN_CLOSING; 4067 if (connp->conn_oper_pending_ill != NULL) 4068 conn_ioctl_cleanup_reqd = B_TRUE; 4069 CONN_INC_REF_LOCKED(connp); 4070 mutex_exit(&connp->conn_lock); 4071 tcp->tcp_closeflags = (uint8_t)flags; 4072 ASSERT(connp->conn_ref >= 3); 4073 4074 /* 4075 * tcp_closemp_used is used below without any protection of a lock 4076 * as we don't expect any one else to use it concurrently at this 4077 * point otherwise it would be a major defect, though we do 4078 * increment tcp_closemp_used to record any attempt to reuse 4079 * tcp_closemp while it is still in use. This would help debugging. 4080 */ 4081 4082 if (mp->b_prev == NULL) { 4083 tcp->tcp_closemp_used = 1; 4084 } else { 4085 tcp->tcp_closemp_used++; 4086 ASSERT(mp->b_prev == NULL); 4087 } 4088 4089 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4090 4091 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4092 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4093 4094 mutex_enter(&tcp->tcp_closelock); 4095 while (!tcp->tcp_closed) { 4096 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4097 /* 4098 * We got interrupted. Check if we are lingering, 4099 * if yes, post a message to stop and wait until 4100 * tcp_closed is set. If we aren't lingering, 4101 * just go back around. 4102 */ 4103 if (tcp->tcp_linger && 4104 tcp->tcp_lingertime > 0 && 4105 !linger_interrupted) { 4106 mutex_exit(&tcp->tcp_closelock); 4107 /* Entering squeue, bump ref count. */ 4108 CONN_INC_REF(connp); 4109 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4110 squeue_enter(connp->conn_sqp, bp, 4111 tcp_linger_interrupted, connp, 4112 SQTAG_IP_TCP_CLOSE); 4113 linger_interrupted = B_TRUE; 4114 mutex_enter(&tcp->tcp_closelock); 4115 } 4116 } 4117 } 4118 mutex_exit(&tcp->tcp_closelock); 4119 4120 /* 4121 * In the case of listener streams that have eagers in the q or q0 4122 * we wait for the eagers to drop their reference to us. tcp_rq and 4123 * tcp_wq of the eagers point to our queues. By waiting for the 4124 * refcnt to drop to 1, we are sure that the eagers have cleaned 4125 * up their queue pointers and also dropped their references to us. 4126 */ 4127 if (tcp->tcp_wait_for_eagers) { 4128 mutex_enter(&connp->conn_lock); 4129 while (connp->conn_ref != 1) { 4130 cv_wait(&connp->conn_cv, &connp->conn_lock); 4131 } 4132 mutex_exit(&connp->conn_lock); 4133 } 4134 /* 4135 * ioctl cleanup. The mp is queued in the 4136 * ill_pending_mp or in the sq_pending_mp. 4137 */ 4138 if (conn_ioctl_cleanup_reqd) 4139 conn_ioctl_cleanup(connp); 4140 4141 qprocsoff(q); 4142 inet_minor_free(ip_minor_arena, connp->conn_dev); 4143 4144 tcp->tcp_cpid = -1; 4145 4146 /* 4147 * Drop IP's reference on the conn. This is the last reference 4148 * on the connp if the state was less than established. If the 4149 * connection has gone into timewait state, then we will have 4150 * one ref for the TCP and one more ref (total of two) for the 4151 * classifier connected hash list (a timewait connections stays 4152 * in connected hash till closed). 4153 * 4154 * We can't assert the references because there might be other 4155 * transient reference places because of some walkers or queued 4156 * packets in squeue for the timewait state. 4157 */ 4158 CONN_DEC_REF(connp); 4159 q->q_ptr = WR(q)->q_ptr = NULL; 4160 return (0); 4161 } 4162 4163 static int 4164 tcpclose_accept(queue_t *q) 4165 { 4166 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4167 4168 /* 4169 * We had opened an acceptor STREAM for sockfs which is 4170 * now being closed due to some error. 4171 */ 4172 qprocsoff(q); 4173 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 4174 q->q_ptr = WR(q)->q_ptr = NULL; 4175 return (0); 4176 } 4177 4178 /* 4179 * Called by tcp_close() routine via squeue when lingering is 4180 * interrupted by a signal. 4181 */ 4182 4183 /* ARGSUSED */ 4184 static void 4185 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4186 { 4187 conn_t *connp = (conn_t *)arg; 4188 tcp_t *tcp = connp->conn_tcp; 4189 4190 freeb(mp); 4191 if (tcp->tcp_linger_tid != 0 && 4192 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4193 tcp_stop_lingering(tcp); 4194 tcp->tcp_client_errno = EINTR; 4195 } 4196 } 4197 4198 /* 4199 * Called by streams close routine via squeues when our client blows off her 4200 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4201 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4202 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4203 * acked. 4204 * 4205 * NOTE: tcp_close potentially returns error when lingering. 4206 * However, the stream head currently does not pass these errors 4207 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4208 * errors to the application (from tsleep()) and not errors 4209 * like ECONNRESET caused by receiving a reset packet. 4210 */ 4211 4212 /* ARGSUSED */ 4213 static void 4214 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4215 { 4216 char *msg; 4217 conn_t *connp = (conn_t *)arg; 4218 tcp_t *tcp = connp->conn_tcp; 4219 clock_t delta = 0; 4220 4221 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4222 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4223 4224 /* Cancel any pending timeout */ 4225 if (tcp->tcp_ordrelid != 0) { 4226 if (tcp->tcp_timeout) { 4227 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4228 } 4229 tcp->tcp_ordrelid = 0; 4230 tcp->tcp_timeout = B_FALSE; 4231 } 4232 4233 mutex_enter(&tcp->tcp_eager_lock); 4234 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4235 /* Cleanup for listener */ 4236 tcp_eager_cleanup(tcp, 0); 4237 tcp->tcp_wait_for_eagers = 1; 4238 } 4239 mutex_exit(&tcp->tcp_eager_lock); 4240 4241 connp->conn_mdt_ok = B_FALSE; 4242 tcp->tcp_mdt = B_FALSE; 4243 4244 msg = NULL; 4245 switch (tcp->tcp_state) { 4246 case TCPS_CLOSED: 4247 case TCPS_IDLE: 4248 case TCPS_BOUND: 4249 case TCPS_LISTEN: 4250 break; 4251 case TCPS_SYN_SENT: 4252 msg = "tcp_close, during connect"; 4253 break; 4254 case TCPS_SYN_RCVD: 4255 /* 4256 * Close during the connect 3-way handshake 4257 * but here there may or may not be pending data 4258 * already on queue. Process almost same as in 4259 * the ESTABLISHED state. 4260 */ 4261 /* FALLTHRU */ 4262 default: 4263 if (tcp->tcp_fused) 4264 tcp_unfuse(tcp); 4265 4266 /* 4267 * If SO_LINGER has set a zero linger time, abort the 4268 * connection with a reset. 4269 */ 4270 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4271 msg = "tcp_close, zero lingertime"; 4272 break; 4273 } 4274 4275 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4276 /* 4277 * Abort connection if there is unread data queued. 4278 */ 4279 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4280 msg = "tcp_close, unread data"; 4281 break; 4282 } 4283 /* 4284 * tcp_hard_bound is now cleared thus all packets go through 4285 * tcp_lookup. This fact is used by tcp_detach below. 4286 * 4287 * We have done a qwait() above which could have possibly 4288 * drained more messages in turn causing transition to a 4289 * different state. Check whether we have to do the rest 4290 * of the processing or not. 4291 */ 4292 if (tcp->tcp_state <= TCPS_LISTEN) 4293 break; 4294 4295 /* 4296 * Transmit the FIN before detaching the tcp_t. 4297 * After tcp_detach returns this queue/perimeter 4298 * no longer owns the tcp_t thus others can modify it. 4299 */ 4300 (void) tcp_xmit_end(tcp); 4301 4302 /* 4303 * If lingering on close then wait until the fin is acked, 4304 * the SO_LINGER time passes, or a reset is sent/received. 4305 */ 4306 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4307 !(tcp->tcp_fin_acked) && 4308 tcp->tcp_state >= TCPS_ESTABLISHED) { 4309 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4310 tcp->tcp_client_errno = EWOULDBLOCK; 4311 } else if (tcp->tcp_client_errno == 0) { 4312 4313 ASSERT(tcp->tcp_linger_tid == 0); 4314 4315 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4316 tcp_close_linger_timeout, 4317 tcp->tcp_lingertime * hz); 4318 4319 /* tcp_close_linger_timeout will finish close */ 4320 if (tcp->tcp_linger_tid == 0) 4321 tcp->tcp_client_errno = ENOSR; 4322 else 4323 return; 4324 } 4325 4326 /* 4327 * Check if we need to detach or just close 4328 * the instance. 4329 */ 4330 if (tcp->tcp_state <= TCPS_LISTEN) 4331 break; 4332 } 4333 4334 /* 4335 * Make sure that no other thread will access the tcp_rq of 4336 * this instance (through lookups etc.) as tcp_rq will go 4337 * away shortly. 4338 */ 4339 tcp_acceptor_hash_remove(tcp); 4340 4341 if (tcp->tcp_flow_stopped) { 4342 tcp_clrqfull(tcp); 4343 } 4344 4345 if (tcp->tcp_timer_tid != 0) { 4346 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4347 tcp->tcp_timer_tid = 0; 4348 } 4349 /* 4350 * Need to cancel those timers which will not be used when 4351 * TCP is detached. This has to be done before the tcp_wq 4352 * is set to the global queue. 4353 */ 4354 tcp_timers_stop(tcp); 4355 4356 tcp->tcp_detached = B_TRUE; 4357 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4358 tcp_time_wait_append(tcp); 4359 TCP_DBGSTAT(tcp_detach_time_wait); 4360 ASSERT(connp->conn_ref >= 3); 4361 goto finish; 4362 } 4363 4364 /* 4365 * If delta is zero the timer event wasn't executed and was 4366 * successfully canceled. In this case we need to restart it 4367 * with the minimal delta possible. 4368 */ 4369 if (delta >= 0) 4370 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4371 delta ? delta : 1); 4372 4373 ASSERT(connp->conn_ref >= 3); 4374 goto finish; 4375 } 4376 4377 /* Detach did not complete. Still need to remove q from stream. */ 4378 if (msg) { 4379 if (tcp->tcp_state == TCPS_ESTABLISHED || 4380 tcp->tcp_state == TCPS_CLOSE_WAIT) 4381 BUMP_MIB(&tcp_mib, tcpEstabResets); 4382 if (tcp->tcp_state == TCPS_SYN_SENT || 4383 tcp->tcp_state == TCPS_SYN_RCVD) 4384 BUMP_MIB(&tcp_mib, tcpAttemptFails); 4385 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4386 } 4387 4388 tcp_closei_local(tcp); 4389 CONN_DEC_REF(connp); 4390 ASSERT(connp->conn_ref >= 2); 4391 4392 finish: 4393 /* 4394 * Although packets are always processed on the correct 4395 * tcp's perimeter and access is serialized via squeue's, 4396 * IP still needs a queue when sending packets in time_wait 4397 * state so use WR(tcp_g_q) till ip_output() can be 4398 * changed to deal with just connp. For read side, we 4399 * could have set tcp_rq to NULL but there are some cases 4400 * in tcp_rput_data() from early days of this code which 4401 * do a putnext without checking if tcp is closed. Those 4402 * need to be identified before both tcp_rq and tcp_wq 4403 * can be set to NULL and tcp_q_q can disappear forever. 4404 */ 4405 mutex_enter(&tcp->tcp_closelock); 4406 /* 4407 * Don't change the queues in the case of a listener that has 4408 * eagers in its q or q0. It could surprise the eagers. 4409 * Instead wait for the eagers outside the squeue. 4410 */ 4411 if (!tcp->tcp_wait_for_eagers) { 4412 tcp->tcp_detached = B_TRUE; 4413 tcp->tcp_rq = tcp_g_q; 4414 tcp->tcp_wq = WR(tcp_g_q); 4415 } 4416 4417 /* Signal tcp_close() to finish closing. */ 4418 tcp->tcp_closed = 1; 4419 cv_signal(&tcp->tcp_closecv); 4420 mutex_exit(&tcp->tcp_closelock); 4421 } 4422 4423 4424 /* 4425 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4426 * Some stream heads get upset if they see these later on as anything but NULL. 4427 */ 4428 static void 4429 tcp_close_mpp(mblk_t **mpp) 4430 { 4431 mblk_t *mp; 4432 4433 if ((mp = *mpp) != NULL) { 4434 do { 4435 mp->b_next = NULL; 4436 mp->b_prev = NULL; 4437 } while ((mp = mp->b_cont) != NULL); 4438 4439 mp = *mpp; 4440 *mpp = NULL; 4441 freemsg(mp); 4442 } 4443 } 4444 4445 /* Do detached close. */ 4446 static void 4447 tcp_close_detached(tcp_t *tcp) 4448 { 4449 if (tcp->tcp_fused) 4450 tcp_unfuse(tcp); 4451 4452 /* 4453 * Clustering code serializes TCP disconnect callbacks and 4454 * cluster tcp list walks by blocking a TCP disconnect callback 4455 * if a cluster tcp list walk is in progress. This ensures 4456 * accurate accounting of TCPs in the cluster code even though 4457 * the TCP list walk itself is not atomic. 4458 */ 4459 tcp_closei_local(tcp); 4460 CONN_DEC_REF(tcp->tcp_connp); 4461 } 4462 4463 /* 4464 * Stop all TCP timers, and free the timer mblks if requested. 4465 */ 4466 void 4467 tcp_timers_stop(tcp_t *tcp) 4468 { 4469 if (tcp->tcp_timer_tid != 0) { 4470 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4471 tcp->tcp_timer_tid = 0; 4472 } 4473 if (tcp->tcp_ka_tid != 0) { 4474 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4475 tcp->tcp_ka_tid = 0; 4476 } 4477 if (tcp->tcp_ack_tid != 0) { 4478 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4479 tcp->tcp_ack_tid = 0; 4480 } 4481 if (tcp->tcp_push_tid != 0) { 4482 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4483 tcp->tcp_push_tid = 0; 4484 } 4485 } 4486 4487 /* 4488 * The tcp_t is going away. Remove it from all lists and set it 4489 * to TCPS_CLOSED. The freeing up of memory is deferred until 4490 * tcp_inactive. This is needed since a thread in tcp_rput might have 4491 * done a CONN_INC_REF on this structure before it was removed from the 4492 * hashes. 4493 */ 4494 static void 4495 tcp_closei_local(tcp_t *tcp) 4496 { 4497 ire_t *ire; 4498 conn_t *connp = tcp->tcp_connp; 4499 4500 if (!TCP_IS_SOCKET(tcp)) 4501 tcp_acceptor_hash_remove(tcp); 4502 4503 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 4504 tcp->tcp_ibsegs = 0; 4505 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 4506 tcp->tcp_obsegs = 0; 4507 4508 /* 4509 * If we are an eager connection hanging off a listener that 4510 * hasn't formally accepted the connection yet, get off his 4511 * list and blow off any data that we have accumulated. 4512 */ 4513 if (tcp->tcp_listener != NULL) { 4514 tcp_t *listener = tcp->tcp_listener; 4515 mutex_enter(&listener->tcp_eager_lock); 4516 /* 4517 * tcp_tconnind_started == B_TRUE means that the 4518 * conn_ind has already gone to listener. At 4519 * this point, eager will be closed but we 4520 * leave it in listeners eager list so that 4521 * if listener decides to close without doing 4522 * accept, we can clean this up. In tcp_wput_accept 4523 * we take care of the case of accept on closed 4524 * eager. 4525 */ 4526 if (!tcp->tcp_tconnind_started) { 4527 tcp_eager_unlink(tcp); 4528 mutex_exit(&listener->tcp_eager_lock); 4529 /* 4530 * We don't want to have any pointers to the 4531 * listener queue, after we have released our 4532 * reference on the listener 4533 */ 4534 tcp->tcp_rq = tcp_g_q; 4535 tcp->tcp_wq = WR(tcp_g_q); 4536 CONN_DEC_REF(listener->tcp_connp); 4537 } else { 4538 mutex_exit(&listener->tcp_eager_lock); 4539 } 4540 } 4541 4542 /* Stop all the timers */ 4543 tcp_timers_stop(tcp); 4544 4545 if (tcp->tcp_state == TCPS_LISTEN) { 4546 if (tcp->tcp_ip_addr_cache) { 4547 kmem_free((void *)tcp->tcp_ip_addr_cache, 4548 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4549 tcp->tcp_ip_addr_cache = NULL; 4550 } 4551 } 4552 if (tcp->tcp_flow_stopped) 4553 tcp_clrqfull(tcp); 4554 4555 tcp_bind_hash_remove(tcp); 4556 /* 4557 * If the tcp_time_wait_collector (which runs outside the squeue) 4558 * is trying to remove this tcp from the time wait list, we will 4559 * block in tcp_time_wait_remove while trying to acquire the 4560 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4561 * requires the ipcl_hash_remove to be ordered after the 4562 * tcp_time_wait_remove for the refcnt checks to work correctly. 4563 */ 4564 if (tcp->tcp_state == TCPS_TIME_WAIT) 4565 (void) tcp_time_wait_remove(tcp, NULL); 4566 CL_INET_DISCONNECT(tcp); 4567 ipcl_hash_remove(connp); 4568 4569 /* 4570 * Delete the cached ire in conn_ire_cache and also mark 4571 * the conn as CONDEMNED 4572 */ 4573 mutex_enter(&connp->conn_lock); 4574 connp->conn_state_flags |= CONN_CONDEMNED; 4575 ire = connp->conn_ire_cache; 4576 connp->conn_ire_cache = NULL; 4577 mutex_exit(&connp->conn_lock); 4578 if (ire != NULL) 4579 IRE_REFRELE_NOTR(ire); 4580 4581 /* Need to cleanup any pending ioctls */ 4582 ASSERT(tcp->tcp_time_wait_next == NULL); 4583 ASSERT(tcp->tcp_time_wait_prev == NULL); 4584 ASSERT(tcp->tcp_time_wait_expire == 0); 4585 tcp->tcp_state = TCPS_CLOSED; 4586 4587 /* Release any SSL context */ 4588 if (tcp->tcp_kssl_ent != NULL) { 4589 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4590 tcp->tcp_kssl_ent = NULL; 4591 } 4592 if (tcp->tcp_kssl_ctx != NULL) { 4593 kssl_release_ctx(tcp->tcp_kssl_ctx); 4594 tcp->tcp_kssl_ctx = NULL; 4595 } 4596 tcp->tcp_kssl_pending = B_FALSE; 4597 } 4598 4599 /* 4600 * tcp is dying (called from ipcl_conn_destroy and error cases). 4601 * Free the tcp_t in either case. 4602 */ 4603 void 4604 tcp_free(tcp_t *tcp) 4605 { 4606 mblk_t *mp; 4607 ip6_pkt_t *ipp; 4608 4609 ASSERT(tcp != NULL); 4610 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4611 4612 tcp->tcp_rq = NULL; 4613 tcp->tcp_wq = NULL; 4614 4615 tcp_close_mpp(&tcp->tcp_xmit_head); 4616 tcp_close_mpp(&tcp->tcp_reass_head); 4617 if (tcp->tcp_rcv_list != NULL) { 4618 /* Free b_next chain */ 4619 tcp_close_mpp(&tcp->tcp_rcv_list); 4620 } 4621 if ((mp = tcp->tcp_urp_mp) != NULL) { 4622 freemsg(mp); 4623 } 4624 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4625 freemsg(mp); 4626 } 4627 4628 if (tcp->tcp_fused_sigurg_mp != NULL) { 4629 freeb(tcp->tcp_fused_sigurg_mp); 4630 tcp->tcp_fused_sigurg_mp = NULL; 4631 } 4632 4633 if (tcp->tcp_sack_info != NULL) { 4634 if (tcp->tcp_notsack_list != NULL) { 4635 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4636 } 4637 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4638 } 4639 4640 if (tcp->tcp_hopopts != NULL) { 4641 mi_free(tcp->tcp_hopopts); 4642 tcp->tcp_hopopts = NULL; 4643 tcp->tcp_hopoptslen = 0; 4644 } 4645 ASSERT(tcp->tcp_hopoptslen == 0); 4646 if (tcp->tcp_dstopts != NULL) { 4647 mi_free(tcp->tcp_dstopts); 4648 tcp->tcp_dstopts = NULL; 4649 tcp->tcp_dstoptslen = 0; 4650 } 4651 ASSERT(tcp->tcp_dstoptslen == 0); 4652 if (tcp->tcp_rtdstopts != NULL) { 4653 mi_free(tcp->tcp_rtdstopts); 4654 tcp->tcp_rtdstopts = NULL; 4655 tcp->tcp_rtdstoptslen = 0; 4656 } 4657 ASSERT(tcp->tcp_rtdstoptslen == 0); 4658 if (tcp->tcp_rthdr != NULL) { 4659 mi_free(tcp->tcp_rthdr); 4660 tcp->tcp_rthdr = NULL; 4661 tcp->tcp_rthdrlen = 0; 4662 } 4663 ASSERT(tcp->tcp_rthdrlen == 0); 4664 4665 ipp = &tcp->tcp_sticky_ipp; 4666 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4667 IPPF_RTHDR)) 4668 ip6_pkt_free(ipp); 4669 4670 /* 4671 * Free memory associated with the tcp/ip header template. 4672 */ 4673 4674 if (tcp->tcp_iphc != NULL) 4675 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4676 4677 /* 4678 * Following is really a blowing away a union. 4679 * It happens to have exactly two members of identical size 4680 * the following code is enough. 4681 */ 4682 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4683 4684 if (tcp->tcp_tracebuf != NULL) { 4685 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4686 tcp->tcp_tracebuf = NULL; 4687 } 4688 } 4689 4690 4691 /* 4692 * Put a connection confirmation message upstream built from the 4693 * address information within 'iph' and 'tcph'. Report our success or failure. 4694 */ 4695 static boolean_t 4696 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4697 mblk_t **defermp) 4698 { 4699 sin_t sin; 4700 sin6_t sin6; 4701 mblk_t *mp; 4702 char *optp = NULL; 4703 int optlen = 0; 4704 cred_t *cr; 4705 4706 if (defermp != NULL) 4707 *defermp = NULL; 4708 4709 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4710 /* 4711 * Return in T_CONN_CON results of option negotiation through 4712 * the T_CONN_REQ. Note: If there is an real end-to-end option 4713 * negotiation, then what is received from remote end needs 4714 * to be taken into account but there is no such thing (yet?) 4715 * in our TCP/IP. 4716 * Note: We do not use mi_offset_param() here as 4717 * tcp_opts_conn_req contents do not directly come from 4718 * an application and are either generated in kernel or 4719 * from user input that was already verified. 4720 */ 4721 mp = tcp->tcp_conn.tcp_opts_conn_req; 4722 optp = (char *)(mp->b_rptr + 4723 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4724 optlen = (int) 4725 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4726 } 4727 4728 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4729 ipha_t *ipha = (ipha_t *)iphdr; 4730 4731 /* packet is IPv4 */ 4732 if (tcp->tcp_family == AF_INET) { 4733 sin = sin_null; 4734 sin.sin_addr.s_addr = ipha->ipha_src; 4735 sin.sin_port = *(uint16_t *)tcph->th_lport; 4736 sin.sin_family = AF_INET; 4737 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4738 (int)sizeof (sin_t), optp, optlen); 4739 } else { 4740 sin6 = sin6_null; 4741 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4742 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4743 sin6.sin6_family = AF_INET6; 4744 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4745 (int)sizeof (sin6_t), optp, optlen); 4746 4747 } 4748 } else { 4749 ip6_t *ip6h = (ip6_t *)iphdr; 4750 4751 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4752 ASSERT(tcp->tcp_family == AF_INET6); 4753 sin6 = sin6_null; 4754 sin6.sin6_addr = ip6h->ip6_src; 4755 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4756 sin6.sin6_family = AF_INET6; 4757 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4758 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4759 (int)sizeof (sin6_t), optp, optlen); 4760 } 4761 4762 if (!mp) 4763 return (B_FALSE); 4764 4765 if ((cr = DB_CRED(idmp)) != NULL) { 4766 mblk_setcred(mp, cr); 4767 DB_CPID(mp) = DB_CPID(idmp); 4768 } 4769 4770 if (defermp == NULL) 4771 putnext(tcp->tcp_rq, mp); 4772 else 4773 *defermp = mp; 4774 4775 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4776 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4777 return (B_TRUE); 4778 } 4779 4780 /* 4781 * Defense for the SYN attack - 4782 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4783 * one from the list of droppable eagers. This list is a subset of q0. 4784 * see comments before the definition of MAKE_DROPPABLE(). 4785 * 2. Don't drop a SYN request before its first timeout. This gives every 4786 * request at least til the first timeout to complete its 3-way handshake. 4787 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4788 * requests currently on the queue that has timed out. This will be used 4789 * as an indicator of whether an attack is under way, so that appropriate 4790 * actions can be taken. (It's incremented in tcp_timer() and decremented 4791 * either when eager goes into ESTABLISHED, or gets freed up.) 4792 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4793 * # of timeout drops back to <= q0len/32 => SYN alert off 4794 */ 4795 static boolean_t 4796 tcp_drop_q0(tcp_t *tcp) 4797 { 4798 tcp_t *eager; 4799 mblk_t *mp; 4800 4801 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4802 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4803 4804 /* Pick oldest eager from the list of droppable eagers */ 4805 eager = tcp->tcp_eager_prev_drop_q0; 4806 4807 /* If list is empty. return B_FALSE */ 4808 if (eager == tcp) { 4809 return (B_FALSE); 4810 } 4811 4812 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4813 if ((mp = allocb(0, BPRI_HI)) == NULL) 4814 return (B_FALSE); 4815 4816 /* 4817 * Take this eager out from the list of droppable eagers since we are 4818 * going to drop it. 4819 */ 4820 MAKE_UNDROPPABLE(eager); 4821 4822 if (tcp->tcp_debug) { 4823 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4824 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4825 " (%d pending) on %s, drop one", tcp_conn_req_max_q0, 4826 tcp->tcp_conn_req_cnt_q0, 4827 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4828 } 4829 4830 BUMP_MIB(&tcp_mib, tcpHalfOpenDrop); 4831 4832 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4833 CONN_INC_REF(eager->tcp_connp); 4834 4835 /* Mark the IRE created for this SYN request temporary */ 4836 tcp_ip_ire_mark_advice(eager); 4837 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4838 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4839 4840 return (B_TRUE); 4841 } 4842 4843 int 4844 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4845 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4846 { 4847 tcp_t *ltcp = lconnp->conn_tcp; 4848 tcp_t *tcp = connp->conn_tcp; 4849 mblk_t *tpi_mp; 4850 ipha_t *ipha; 4851 ip6_t *ip6h; 4852 sin6_t sin6; 4853 in6_addr_t v6dst; 4854 int err; 4855 int ifindex = 0; 4856 cred_t *cr; 4857 4858 if (ipvers == IPV4_VERSION) { 4859 ipha = (ipha_t *)mp->b_rptr; 4860 4861 connp->conn_send = ip_output; 4862 connp->conn_recv = tcp_input; 4863 4864 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4865 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4866 4867 sin6 = sin6_null; 4868 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4869 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4870 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4871 sin6.sin6_family = AF_INET6; 4872 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4873 lconnp->conn_zoneid); 4874 if (tcp->tcp_recvdstaddr) { 4875 sin6_t sin6d; 4876 4877 sin6d = sin6_null; 4878 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4879 &sin6d.sin6_addr); 4880 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4881 sin6d.sin6_family = AF_INET; 4882 tpi_mp = mi_tpi_extconn_ind(NULL, 4883 (char *)&sin6d, sizeof (sin6_t), 4884 (char *)&tcp, 4885 (t_scalar_t)sizeof (intptr_t), 4886 (char *)&sin6d, sizeof (sin6_t), 4887 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4888 } else { 4889 tpi_mp = mi_tpi_conn_ind(NULL, 4890 (char *)&sin6, sizeof (sin6_t), 4891 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4892 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4893 } 4894 } else { 4895 ip6h = (ip6_t *)mp->b_rptr; 4896 4897 connp->conn_send = ip_output_v6; 4898 connp->conn_recv = tcp_input; 4899 4900 connp->conn_srcv6 = ip6h->ip6_dst; 4901 connp->conn_remv6 = ip6h->ip6_src; 4902 4903 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4904 ifindex = (int)DB_CKSUMSTUFF(mp); 4905 DB_CKSUMSTUFF(mp) = 0; 4906 4907 sin6 = sin6_null; 4908 sin6.sin6_addr = ip6h->ip6_src; 4909 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4910 sin6.sin6_family = AF_INET6; 4911 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4912 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4913 lconnp->conn_zoneid); 4914 4915 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4916 /* Pass up the scope_id of remote addr */ 4917 sin6.sin6_scope_id = ifindex; 4918 } else { 4919 sin6.sin6_scope_id = 0; 4920 } 4921 if (tcp->tcp_recvdstaddr) { 4922 sin6_t sin6d; 4923 4924 sin6d = sin6_null; 4925 sin6.sin6_addr = ip6h->ip6_dst; 4926 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4927 sin6d.sin6_family = AF_INET; 4928 tpi_mp = mi_tpi_extconn_ind(NULL, 4929 (char *)&sin6d, sizeof (sin6_t), 4930 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4931 (char *)&sin6d, sizeof (sin6_t), 4932 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4933 } else { 4934 tpi_mp = mi_tpi_conn_ind(NULL, 4935 (char *)&sin6, sizeof (sin6_t), 4936 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4937 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4938 } 4939 } 4940 4941 if (tpi_mp == NULL) 4942 return (ENOMEM); 4943 4944 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4945 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4946 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4947 connp->conn_fully_bound = B_FALSE; 4948 4949 if (tcp_trace) 4950 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4951 4952 /* Inherit information from the "parent" */ 4953 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4954 tcp->tcp_family = ltcp->tcp_family; 4955 tcp->tcp_wq = ltcp->tcp_wq; 4956 tcp->tcp_rq = ltcp->tcp_rq; 4957 tcp->tcp_mss = tcp_mss_def_ipv6; 4958 tcp->tcp_detached = B_TRUE; 4959 if ((err = tcp_init_values(tcp)) != 0) { 4960 freemsg(tpi_mp); 4961 return (err); 4962 } 4963 4964 if (ipvers == IPV4_VERSION) { 4965 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4966 freemsg(tpi_mp); 4967 return (err); 4968 } 4969 ASSERT(tcp->tcp_ipha != NULL); 4970 } else { 4971 /* ifindex must be already set */ 4972 ASSERT(ifindex != 0); 4973 4974 if (ltcp->tcp_bound_if != 0) { 4975 /* 4976 * Set newtcp's bound_if equal to 4977 * listener's value. If ifindex is 4978 * not the same as ltcp->tcp_bound_if, 4979 * it must be a packet for the ipmp group 4980 * of interfaces 4981 */ 4982 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4983 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4984 tcp->tcp_bound_if = ifindex; 4985 } 4986 4987 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4988 tcp->tcp_recvifindex = 0; 4989 tcp->tcp_recvhops = 0xffffffffU; 4990 ASSERT(tcp->tcp_ip6h != NULL); 4991 } 4992 4993 tcp->tcp_lport = ltcp->tcp_lport; 4994 4995 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4996 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4997 /* 4998 * Listener had options of some sort; eager inherits. 4999 * Free up the eager template and allocate one 5000 * of the right size. 5001 */ 5002 if (tcp->tcp_hdr_grown) { 5003 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 5004 } else { 5005 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 5006 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 5007 } 5008 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 5009 KM_NOSLEEP); 5010 if (tcp->tcp_iphc == NULL) { 5011 tcp->tcp_iphc_len = 0; 5012 freemsg(tpi_mp); 5013 return (ENOMEM); 5014 } 5015 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 5016 tcp->tcp_hdr_grown = B_TRUE; 5017 } 5018 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5019 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5020 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5021 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 5022 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 5023 5024 /* 5025 * Copy the IP+TCP header template from listener to eager 5026 */ 5027 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5028 if (tcp->tcp_ipversion == IPV6_VERSION) { 5029 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 5030 IPPROTO_RAW) { 5031 tcp->tcp_ip6h = 5032 (ip6_t *)(tcp->tcp_iphc + 5033 sizeof (ip6i_t)); 5034 } else { 5035 tcp->tcp_ip6h = 5036 (ip6_t *)(tcp->tcp_iphc); 5037 } 5038 tcp->tcp_ipha = NULL; 5039 } else { 5040 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5041 tcp->tcp_ip6h = NULL; 5042 } 5043 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5044 tcp->tcp_ip_hdr_len); 5045 } else { 5046 /* 5047 * only valid case when ipversion of listener and 5048 * eager differ is when listener is IPv6 and 5049 * eager is IPv4. 5050 * Eager header template has been initialized to the 5051 * maximum v4 header sizes, which includes space for 5052 * TCP and IP options. 5053 */ 5054 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5055 (tcp->tcp_ipversion == IPV4_VERSION)); 5056 ASSERT(tcp->tcp_iphc_len >= 5057 TCP_MAX_COMBINED_HEADER_LENGTH); 5058 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5059 /* copy IP header fields individually */ 5060 tcp->tcp_ipha->ipha_ttl = 5061 ltcp->tcp_ip6h->ip6_hops; 5062 bcopy(ltcp->tcp_tcph->th_lport, 5063 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5064 } 5065 5066 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5067 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5068 sizeof (in_port_t)); 5069 5070 if (ltcp->tcp_lport == 0) { 5071 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5072 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5073 sizeof (in_port_t)); 5074 } 5075 5076 if (tcp->tcp_ipversion == IPV4_VERSION) { 5077 ASSERT(ipha != NULL); 5078 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5079 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5080 5081 /* Source routing option copyover (reverse it) */ 5082 if (tcp_rev_src_routes) 5083 tcp_opt_reverse(tcp, ipha); 5084 } else { 5085 ASSERT(ip6h != NULL); 5086 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5087 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5088 } 5089 5090 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5091 ASSERT(!tcp->tcp_tconnind_started); 5092 /* 5093 * If the SYN contains a credential, it's a loopback packet; attach 5094 * the credential to the TPI message. 5095 */ 5096 if ((cr = DB_CRED(idmp)) != NULL) { 5097 mblk_setcred(tpi_mp, cr); 5098 DB_CPID(tpi_mp) = DB_CPID(idmp); 5099 } 5100 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5101 5102 /* Inherit the listener's SSL protection state */ 5103 5104 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5105 kssl_hold_ent(tcp->tcp_kssl_ent); 5106 tcp->tcp_kssl_pending = B_TRUE; 5107 } 5108 5109 return (0); 5110 } 5111 5112 5113 int 5114 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5115 tcph_t *tcph, mblk_t *idmp) 5116 { 5117 tcp_t *ltcp = lconnp->conn_tcp; 5118 tcp_t *tcp = connp->conn_tcp; 5119 sin_t sin; 5120 mblk_t *tpi_mp = NULL; 5121 int err; 5122 cred_t *cr; 5123 5124 sin = sin_null; 5125 sin.sin_addr.s_addr = ipha->ipha_src; 5126 sin.sin_port = *(uint16_t *)tcph->th_lport; 5127 sin.sin_family = AF_INET; 5128 if (ltcp->tcp_recvdstaddr) { 5129 sin_t sind; 5130 5131 sind = sin_null; 5132 sind.sin_addr.s_addr = ipha->ipha_dst; 5133 sind.sin_port = *(uint16_t *)tcph->th_fport; 5134 sind.sin_family = AF_INET; 5135 tpi_mp = mi_tpi_extconn_ind(NULL, 5136 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5137 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5138 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5139 } else { 5140 tpi_mp = mi_tpi_conn_ind(NULL, 5141 (char *)&sin, sizeof (sin_t), 5142 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5143 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5144 } 5145 5146 if (tpi_mp == NULL) { 5147 return (ENOMEM); 5148 } 5149 5150 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5151 connp->conn_send = ip_output; 5152 connp->conn_recv = tcp_input; 5153 connp->conn_fully_bound = B_FALSE; 5154 5155 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5156 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5157 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5158 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5159 5160 if (tcp_trace) { 5161 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 5162 } 5163 5164 /* Inherit information from the "parent" */ 5165 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5166 tcp->tcp_family = ltcp->tcp_family; 5167 tcp->tcp_wq = ltcp->tcp_wq; 5168 tcp->tcp_rq = ltcp->tcp_rq; 5169 tcp->tcp_mss = tcp_mss_def_ipv4; 5170 tcp->tcp_detached = B_TRUE; 5171 if ((err = tcp_init_values(tcp)) != 0) { 5172 freemsg(tpi_mp); 5173 return (err); 5174 } 5175 5176 /* 5177 * Let's make sure that eager tcp template has enough space to 5178 * copy IPv4 listener's tcp template. Since the conn_t structure is 5179 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5180 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5181 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5182 * extension headers or with ip6i_t struct). Note that bcopy() below 5183 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5184 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5185 */ 5186 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5187 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5188 5189 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5190 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5191 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5192 tcp->tcp_ttl = ltcp->tcp_ttl; 5193 tcp->tcp_tos = ltcp->tcp_tos; 5194 5195 /* Copy the IP+TCP header template from listener to eager */ 5196 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5197 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5198 tcp->tcp_ip6h = NULL; 5199 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5200 tcp->tcp_ip_hdr_len); 5201 5202 /* Initialize the IP addresses and Ports */ 5203 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5204 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5205 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5206 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5207 5208 /* Source routing option copyover (reverse it) */ 5209 if (tcp_rev_src_routes) 5210 tcp_opt_reverse(tcp, ipha); 5211 5212 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5213 ASSERT(!tcp->tcp_tconnind_started); 5214 5215 /* 5216 * If the SYN contains a credential, it's a loopback packet; attach 5217 * the credential to the TPI message. 5218 */ 5219 if ((cr = DB_CRED(idmp)) != NULL) { 5220 mblk_setcred(tpi_mp, cr); 5221 DB_CPID(tpi_mp) = DB_CPID(idmp); 5222 } 5223 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5224 5225 /* Inherit the listener's SSL protection state */ 5226 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5227 kssl_hold_ent(tcp->tcp_kssl_ent); 5228 tcp->tcp_kssl_pending = B_TRUE; 5229 } 5230 5231 return (0); 5232 } 5233 5234 /* 5235 * sets up conn for ipsec. 5236 * if the first mblk is M_CTL it is consumed and mpp is updated. 5237 * in case of error mpp is freed. 5238 */ 5239 conn_t * 5240 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5241 { 5242 conn_t *connp = tcp->tcp_connp; 5243 conn_t *econnp; 5244 squeue_t *new_sqp; 5245 mblk_t *first_mp = *mpp; 5246 mblk_t *mp = *mpp; 5247 boolean_t mctl_present = B_FALSE; 5248 uint_t ipvers; 5249 5250 econnp = tcp_get_conn(sqp); 5251 if (econnp == NULL) { 5252 freemsg(first_mp); 5253 return (NULL); 5254 } 5255 if (DB_TYPE(mp) == M_CTL) { 5256 if (mp->b_cont == NULL || 5257 mp->b_cont->b_datap->db_type != M_DATA) { 5258 freemsg(first_mp); 5259 return (NULL); 5260 } 5261 mp = mp->b_cont; 5262 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5263 freemsg(first_mp); 5264 return (NULL); 5265 } 5266 5267 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5268 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5269 mctl_present = B_TRUE; 5270 } else { 5271 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5272 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5273 } 5274 5275 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5276 DB_CKSUMSTART(mp) = 0; 5277 5278 ASSERT(OK_32PTR(mp->b_rptr)); 5279 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5280 if (ipvers == IPV4_VERSION) { 5281 uint16_t *up; 5282 uint32_t ports; 5283 ipha_t *ipha; 5284 5285 ipha = (ipha_t *)mp->b_rptr; 5286 up = (uint16_t *)((uchar_t *)ipha + 5287 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5288 ports = *(uint32_t *)up; 5289 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5290 ipha->ipha_dst, ipha->ipha_src, ports); 5291 } else { 5292 uint16_t *up; 5293 uint32_t ports; 5294 uint16_t ip_hdr_len; 5295 uint8_t *nexthdrp; 5296 ip6_t *ip6h; 5297 tcph_t *tcph; 5298 5299 ip6h = (ip6_t *)mp->b_rptr; 5300 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5301 ip_hdr_len = IPV6_HDR_LEN; 5302 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5303 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5304 CONN_DEC_REF(econnp); 5305 freemsg(first_mp); 5306 return (NULL); 5307 } 5308 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5309 up = (uint16_t *)tcph->th_lport; 5310 ports = *(uint32_t *)up; 5311 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5312 ip6h->ip6_dst, ip6h->ip6_src, ports); 5313 } 5314 5315 /* 5316 * The caller already ensured that there is a sqp present. 5317 */ 5318 econnp->conn_sqp = new_sqp; 5319 5320 if (connp->conn_policy != NULL) { 5321 ipsec_in_t *ii; 5322 ii = (ipsec_in_t *)(first_mp->b_rptr); 5323 ASSERT(ii->ipsec_in_policy == NULL); 5324 IPPH_REFHOLD(connp->conn_policy); 5325 ii->ipsec_in_policy = connp->conn_policy; 5326 5327 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5328 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5329 CONN_DEC_REF(econnp); 5330 freemsg(first_mp); 5331 return (NULL); 5332 } 5333 } 5334 5335 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5336 CONN_DEC_REF(econnp); 5337 freemsg(first_mp); 5338 return (NULL); 5339 } 5340 5341 /* 5342 * If we know we have some policy, pass the "IPSEC" 5343 * options size TCP uses this adjust the MSS. 5344 */ 5345 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5346 if (mctl_present) { 5347 freeb(first_mp); 5348 *mpp = mp; 5349 } 5350 5351 return (econnp); 5352 } 5353 5354 /* 5355 * tcp_get_conn/tcp_free_conn 5356 * 5357 * tcp_get_conn is used to get a clean tcp connection structure. 5358 * It tries to reuse the connections put on the freelist by the 5359 * time_wait_collector failing which it goes to kmem_cache. This 5360 * way has two benefits compared to just allocating from and 5361 * freeing to kmem_cache. 5362 * 1) The time_wait_collector can free (which includes the cleanup) 5363 * outside the squeue. So when the interrupt comes, we have a clean 5364 * connection sitting in the freelist. Obviously, this buys us 5365 * performance. 5366 * 5367 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5368 * has multiple disadvantages - tying up the squeue during alloc, and the 5369 * fact that IPSec policy initialization has to happen here which 5370 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5371 * But allocating the conn/tcp in IP land is also not the best since 5372 * we can't check the 'q' and 'q0' which are protected by squeue and 5373 * blindly allocate memory which might have to be freed here if we are 5374 * not allowed to accept the connection. By using the freelist and 5375 * putting the conn/tcp back in freelist, we don't pay a penalty for 5376 * allocating memory without checking 'q/q0' and freeing it if we can't 5377 * accept the connection. 5378 * 5379 * Care should be taken to put the conn back in the same squeue's freelist 5380 * from which it was allocated. Best results are obtained if conn is 5381 * allocated from listener's squeue and freed to the same. Time wait 5382 * collector will free up the freelist is the connection ends up sitting 5383 * there for too long. 5384 */ 5385 void * 5386 tcp_get_conn(void *arg) 5387 { 5388 tcp_t *tcp = NULL; 5389 conn_t *connp = NULL; 5390 squeue_t *sqp = (squeue_t *)arg; 5391 tcp_squeue_priv_t *tcp_time_wait; 5392 5393 tcp_time_wait = 5394 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5395 5396 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5397 tcp = tcp_time_wait->tcp_free_list; 5398 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5399 if (tcp != NULL) { 5400 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5401 tcp_time_wait->tcp_free_list_cnt--; 5402 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5403 tcp->tcp_time_wait_next = NULL; 5404 connp = tcp->tcp_connp; 5405 connp->conn_flags |= IPCL_REUSED; 5406 return ((void *)connp); 5407 } 5408 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5409 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL) 5410 return (NULL); 5411 return ((void *)connp); 5412 } 5413 5414 /* 5415 * Update the cached label for the given tcp_t. This should be called once per 5416 * connection, and before any packets are sent or tcp_process_options is 5417 * invoked. Returns B_FALSE if the correct label could not be constructed. 5418 */ 5419 static boolean_t 5420 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5421 { 5422 conn_t *connp = tcp->tcp_connp; 5423 5424 if (tcp->tcp_ipversion == IPV4_VERSION) { 5425 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5426 int added; 5427 5428 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5429 connp->conn_mac_exempt) != 0) 5430 return (B_FALSE); 5431 5432 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5433 if (added == -1) 5434 return (B_FALSE); 5435 tcp->tcp_hdr_len += added; 5436 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5437 tcp->tcp_ip_hdr_len += added; 5438 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5439 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5440 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5441 tcp->tcp_hdr_len); 5442 if (added == -1) 5443 return (B_FALSE); 5444 tcp->tcp_hdr_len += added; 5445 tcp->tcp_tcph = (tcph_t *) 5446 ((uchar_t *)tcp->tcp_tcph + added); 5447 tcp->tcp_ip_hdr_len += added; 5448 } 5449 } else { 5450 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5451 5452 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5453 connp->conn_mac_exempt) != 0) 5454 return (B_FALSE); 5455 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5456 &tcp->tcp_label_len, optbuf) != 0) 5457 return (B_FALSE); 5458 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5459 return (B_FALSE); 5460 } 5461 5462 connp->conn_ulp_labeled = 1; 5463 5464 return (B_TRUE); 5465 } 5466 5467 /* BEGIN CSTYLED */ 5468 /* 5469 * 5470 * The sockfs ACCEPT path: 5471 * ======================= 5472 * 5473 * The eager is now established in its own perimeter as soon as SYN is 5474 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5475 * completes the accept processing on the acceptor STREAM. The sending 5476 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5477 * listener but a TLI/XTI listener completes the accept processing 5478 * on the listener perimeter. 5479 * 5480 * Common control flow for 3 way handshake: 5481 * ---------------------------------------- 5482 * 5483 * incoming SYN (listener perimeter) -> tcp_rput_data() 5484 * -> tcp_conn_request() 5485 * 5486 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5487 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5488 * 5489 * Sockfs ACCEPT Path: 5490 * ------------------- 5491 * 5492 * open acceptor stream (ip_tcpopen allocates tcp_wput_accept() 5493 * as STREAM entry point) 5494 * 5495 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5496 * 5497 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5498 * association (we are not behind eager's squeue but sockfs is protecting us 5499 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5500 * is changed to point at tcp_wput(). 5501 * 5502 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5503 * listener (done on listener's perimeter). 5504 * 5505 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5506 * accept. 5507 * 5508 * TLI/XTI client ACCEPT path: 5509 * --------------------------- 5510 * 5511 * soaccept() sends T_CONN_RES on the listener STREAM. 5512 * 5513 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5514 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5515 * 5516 * Locks: 5517 * ====== 5518 * 5519 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5520 * and listeners->tcp_eager_next_q. 5521 * 5522 * Referencing: 5523 * ============ 5524 * 5525 * 1) We start out in tcp_conn_request by eager placing a ref on 5526 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5527 * 5528 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5529 * doing so we place a ref on the eager. This ref is finally dropped at the 5530 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5531 * reference is dropped by the squeue framework. 5532 * 5533 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5534 * 5535 * The reference must be released by the same entity that added the reference 5536 * In the above scheme, the eager is the entity that adds and releases the 5537 * references. Note that tcp_accept_finish executes in the squeue of the eager 5538 * (albeit after it is attached to the acceptor stream). Though 1. executes 5539 * in the listener's squeue, the eager is nascent at this point and the 5540 * reference can be considered to have been added on behalf of the eager. 5541 * 5542 * Eager getting a Reset or listener closing: 5543 * ========================================== 5544 * 5545 * Once the listener and eager are linked, the listener never does the unlink. 5546 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5547 * a message on all eager perimeter. The eager then does the unlink, clears 5548 * any pointers to the listener's queue and drops the reference to the 5549 * listener. The listener waits in tcp_close outside the squeue until its 5550 * refcount has dropped to 1. This ensures that the listener has waited for 5551 * all eagers to clear their association with the listener. 5552 * 5553 * Similarly, if eager decides to go away, it can unlink itself and close. 5554 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5555 * the reference to eager is still valid because of the extra ref we put 5556 * in tcp_send_conn_ind. 5557 * 5558 * Listener can always locate the eager under the protection 5559 * of the listener->tcp_eager_lock, and then do a refhold 5560 * on the eager during the accept processing. 5561 * 5562 * The acceptor stream accesses the eager in the accept processing 5563 * based on the ref placed on eager before sending T_conn_ind. 5564 * The only entity that can negate this refhold is a listener close 5565 * which is mutually exclusive with an active acceptor stream. 5566 * 5567 * Eager's reference on the listener 5568 * =================================== 5569 * 5570 * If the accept happens (even on a closed eager) the eager drops its 5571 * reference on the listener at the start of tcp_accept_finish. If the 5572 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5573 * the reference is dropped in tcp_closei_local. If the listener closes, 5574 * the reference is dropped in tcp_eager_kill. In all cases the reference 5575 * is dropped while executing in the eager's context (squeue). 5576 */ 5577 /* END CSTYLED */ 5578 5579 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5580 5581 /* 5582 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5583 * tcp_rput_data will not see any SYN packets. 5584 */ 5585 /* ARGSUSED */ 5586 void 5587 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5588 { 5589 tcph_t *tcph; 5590 uint32_t seg_seq; 5591 tcp_t *eager; 5592 uint_t ipvers; 5593 ipha_t *ipha; 5594 ip6_t *ip6h; 5595 int err; 5596 conn_t *econnp = NULL; 5597 squeue_t *new_sqp; 5598 mblk_t *mp1; 5599 uint_t ip_hdr_len; 5600 conn_t *connp = (conn_t *)arg; 5601 tcp_t *tcp = connp->conn_tcp; 5602 ire_t *ire; 5603 cred_t *credp; 5604 5605 if (tcp->tcp_state != TCPS_LISTEN) 5606 goto error2; 5607 5608 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5609 5610 mutex_enter(&tcp->tcp_eager_lock); 5611 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5612 mutex_exit(&tcp->tcp_eager_lock); 5613 TCP_STAT(tcp_listendrop); 5614 BUMP_MIB(&tcp_mib, tcpListenDrop); 5615 if (tcp->tcp_debug) { 5616 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5617 "tcp_conn_request: listen backlog (max=%d) " 5618 "overflow (%d pending) on %s", 5619 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5620 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5621 } 5622 goto error2; 5623 } 5624 5625 if (tcp->tcp_conn_req_cnt_q0 >= 5626 tcp->tcp_conn_req_max + tcp_conn_req_max_q0) { 5627 /* 5628 * Q0 is full. Drop a pending half-open req from the queue 5629 * to make room for the new SYN req. Also mark the time we 5630 * drop a SYN. 5631 * 5632 * A more aggressive defense against SYN attack will 5633 * be to set the "tcp_syn_defense" flag now. 5634 */ 5635 TCP_STAT(tcp_listendropq0); 5636 tcp->tcp_last_rcv_lbolt = lbolt64; 5637 if (!tcp_drop_q0(tcp)) { 5638 mutex_exit(&tcp->tcp_eager_lock); 5639 BUMP_MIB(&tcp_mib, tcpListenDropQ0); 5640 if (tcp->tcp_debug) { 5641 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5642 "tcp_conn_request: listen half-open queue " 5643 "(max=%d) full (%d pending) on %s", 5644 tcp_conn_req_max_q0, 5645 tcp->tcp_conn_req_cnt_q0, 5646 tcp_display(tcp, NULL, 5647 DISP_PORT_ONLY)); 5648 } 5649 goto error2; 5650 } 5651 } 5652 mutex_exit(&tcp->tcp_eager_lock); 5653 5654 /* 5655 * IP adds STRUIO_EAGER and ensures that the received packet is 5656 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5657 * link local address. If IPSec is enabled, db_struioflag has 5658 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5659 * otherwise an error case if neither of them is set. 5660 */ 5661 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5662 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5663 DB_CKSUMSTART(mp) = 0; 5664 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5665 econnp = (conn_t *)tcp_get_conn(arg2); 5666 if (econnp == NULL) 5667 goto error2; 5668 econnp->conn_sqp = new_sqp; 5669 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5670 /* 5671 * mp is updated in tcp_get_ipsec_conn(). 5672 */ 5673 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5674 if (econnp == NULL) { 5675 /* 5676 * mp freed by tcp_get_ipsec_conn. 5677 */ 5678 return; 5679 } 5680 } else { 5681 goto error2; 5682 } 5683 5684 ASSERT(DB_TYPE(mp) == M_DATA); 5685 5686 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5687 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5688 ASSERT(OK_32PTR(mp->b_rptr)); 5689 if (ipvers == IPV4_VERSION) { 5690 ipha = (ipha_t *)mp->b_rptr; 5691 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5692 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5693 } else { 5694 ip6h = (ip6_t *)mp->b_rptr; 5695 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5696 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5697 } 5698 5699 if (tcp->tcp_family == AF_INET) { 5700 ASSERT(ipvers == IPV4_VERSION); 5701 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5702 } else { 5703 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5704 } 5705 5706 if (err) 5707 goto error3; 5708 5709 eager = econnp->conn_tcp; 5710 5711 /* Inherit various TCP parameters from the listener */ 5712 eager->tcp_naglim = tcp->tcp_naglim; 5713 eager->tcp_first_timer_threshold = 5714 tcp->tcp_first_timer_threshold; 5715 eager->tcp_second_timer_threshold = 5716 tcp->tcp_second_timer_threshold; 5717 5718 eager->tcp_first_ctimer_threshold = 5719 tcp->tcp_first_ctimer_threshold; 5720 eager->tcp_second_ctimer_threshold = 5721 tcp->tcp_second_ctimer_threshold; 5722 5723 /* 5724 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5725 * If it does not, the eager's receive window will be set to the 5726 * listener's receive window later in this function. 5727 */ 5728 eager->tcp_rwnd = 0; 5729 5730 /* 5731 * Inherit listener's tcp_init_cwnd. Need to do this before 5732 * calling tcp_process_options() where tcp_mss_set() is called 5733 * to set the initial cwnd. 5734 */ 5735 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5736 5737 /* 5738 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5739 * zone id before the accept is completed in tcp_wput_accept(). 5740 */ 5741 econnp->conn_zoneid = connp->conn_zoneid; 5742 econnp->conn_allzones = connp->conn_allzones; 5743 5744 /* Copy nexthop information from listener to eager */ 5745 if (connp->conn_nexthop_set) { 5746 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5747 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5748 } 5749 5750 /* 5751 * TSOL: tsol_input_proc() needs the eager's cred before the 5752 * eager is accepted 5753 */ 5754 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5755 crhold(credp); 5756 5757 /* 5758 * If the caller has the process-wide flag set, then default to MAC 5759 * exempt mode. This allows read-down to unlabeled hosts. 5760 */ 5761 if (getpflags(NET_MAC_AWARE, credp) != 0) 5762 econnp->conn_mac_exempt = B_TRUE; 5763 5764 if (is_system_labeled()) { 5765 cred_t *cr; 5766 5767 if (connp->conn_mlp_type != mlptSingle) { 5768 cr = econnp->conn_peercred = DB_CRED(mp); 5769 if (cr != NULL) 5770 crhold(cr); 5771 else 5772 cr = econnp->conn_cred; 5773 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5774 econnp, cred_t *, cr) 5775 } else { 5776 cr = econnp->conn_cred; 5777 DTRACE_PROBE2(syn_accept, conn_t *, 5778 econnp, cred_t *, cr) 5779 } 5780 5781 if (!tcp_update_label(eager, cr)) { 5782 DTRACE_PROBE3( 5783 tx__ip__log__error__connrequest__tcp, 5784 char *, "eager connp(1) label on SYN mp(2) failed", 5785 conn_t *, econnp, mblk_t *, mp); 5786 goto error3; 5787 } 5788 } 5789 5790 eager->tcp_hard_binding = B_TRUE; 5791 5792 tcp_bind_hash_insert(&tcp_bind_fanout[ 5793 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5794 5795 CL_INET_CONNECT(eager); 5796 5797 /* 5798 * No need to check for multicast destination since ip will only pass 5799 * up multicasts to those that have expressed interest 5800 * TODO: what about rejecting broadcasts? 5801 * Also check that source is not a multicast or broadcast address. 5802 */ 5803 eager->tcp_state = TCPS_SYN_RCVD; 5804 5805 5806 /* 5807 * There should be no ire in the mp as we are being called after 5808 * receiving the SYN. 5809 */ 5810 ASSERT(tcp_ire_mp(mp) == NULL); 5811 5812 /* 5813 * Adapt our mss, ttl, ... according to information provided in IRE. 5814 */ 5815 5816 if (tcp_adapt_ire(eager, NULL) == 0) { 5817 /* Undo the bind_hash_insert */ 5818 tcp_bind_hash_remove(eager); 5819 goto error3; 5820 } 5821 5822 /* Process all TCP options. */ 5823 tcp_process_options(eager, tcph); 5824 5825 /* Is the other end ECN capable? */ 5826 if (tcp_ecn_permitted >= 1 && 5827 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5828 eager->tcp_ecn_ok = B_TRUE; 5829 } 5830 5831 /* 5832 * listener->tcp_rq->q_hiwat should be the default window size or a 5833 * window size changed via SO_RCVBUF option. First round up the 5834 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5835 * scale option value if needed. Call tcp_rwnd_set() to finish the 5836 * setting. 5837 * 5838 * Note if there is a rpipe metric associated with the remote host, 5839 * we should not inherit receive window size from listener. 5840 */ 5841 eager->tcp_rwnd = MSS_ROUNDUP( 5842 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5843 eager->tcp_rwnd), eager->tcp_mss); 5844 if (eager->tcp_snd_ws_ok) 5845 tcp_set_ws_value(eager); 5846 /* 5847 * Note that this is the only place tcp_rwnd_set() is called for 5848 * accepting a connection. We need to call it here instead of 5849 * after the 3-way handshake because we need to tell the other 5850 * side our rwnd in the SYN-ACK segment. 5851 */ 5852 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5853 5854 /* 5855 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5856 * via soaccept()->soinheritoptions() which essentially applies 5857 * all the listener options to the new STREAM. The options that we 5858 * need to take care of are: 5859 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5860 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5861 * SO_SNDBUF, SO_RCVBUF. 5862 * 5863 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5864 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5865 * tcp_maxpsz_set() gets called later from 5866 * tcp_accept_finish(), the option takes effect. 5867 * 5868 */ 5869 /* Set the TCP options */ 5870 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5871 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5872 eager->tcp_oobinline = tcp->tcp_oobinline; 5873 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5874 eager->tcp_broadcast = tcp->tcp_broadcast; 5875 eager->tcp_useloopback = tcp->tcp_useloopback; 5876 eager->tcp_dontroute = tcp->tcp_dontroute; 5877 eager->tcp_linger = tcp->tcp_linger; 5878 eager->tcp_lingertime = tcp->tcp_lingertime; 5879 if (tcp->tcp_ka_enabled) 5880 eager->tcp_ka_enabled = 1; 5881 5882 /* Set the IP options */ 5883 econnp->conn_broadcast = connp->conn_broadcast; 5884 econnp->conn_loopback = connp->conn_loopback; 5885 econnp->conn_dontroute = connp->conn_dontroute; 5886 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5887 5888 /* Put a ref on the listener for the eager. */ 5889 CONN_INC_REF(connp); 5890 mutex_enter(&tcp->tcp_eager_lock); 5891 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5892 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5893 tcp->tcp_eager_next_q0 = eager; 5894 eager->tcp_eager_prev_q0 = tcp; 5895 5896 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5897 eager->tcp_listener = tcp; 5898 eager->tcp_saved_listener = tcp; 5899 5900 /* 5901 * Tag this detached tcp vector for later retrieval 5902 * by our listener client in tcp_accept(). 5903 */ 5904 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5905 tcp->tcp_conn_req_cnt_q0++; 5906 if (++tcp->tcp_conn_req_seqnum == -1) { 5907 /* 5908 * -1 is "special" and defined in TPI as something 5909 * that should never be used in T_CONN_IND 5910 */ 5911 ++tcp->tcp_conn_req_seqnum; 5912 } 5913 mutex_exit(&tcp->tcp_eager_lock); 5914 5915 if (tcp->tcp_syn_defense) { 5916 /* Don't drop the SYN that comes from a good IP source */ 5917 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5918 if (addr_cache != NULL && eager->tcp_remote == 5919 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5920 eager->tcp_dontdrop = B_TRUE; 5921 } 5922 } 5923 5924 /* 5925 * We need to insert the eager in its own perimeter but as soon 5926 * as we do that, we expose the eager to the classifier and 5927 * should not touch any field outside the eager's perimeter. 5928 * So do all the work necessary before inserting the eager 5929 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5930 * will succeed but undo everything if it fails. 5931 */ 5932 seg_seq = ABE32_TO_U32(tcph->th_seq); 5933 eager->tcp_irs = seg_seq; 5934 eager->tcp_rack = seg_seq; 5935 eager->tcp_rnxt = seg_seq + 1; 5936 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5937 BUMP_MIB(&tcp_mib, tcpPassiveOpens); 5938 eager->tcp_state = TCPS_SYN_RCVD; 5939 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5940 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5941 if (mp1 == NULL) 5942 goto error1; 5943 DB_CPID(mp1) = tcp->tcp_cpid; 5944 5945 /* 5946 * We need to start the rto timer. In normal case, we start 5947 * the timer after sending the packet on the wire (or at 5948 * least believing that packet was sent by waiting for 5949 * CALL_IP_WPUT() to return). Since this is the first packet 5950 * being sent on the wire for the eager, our initial tcp_rto 5951 * is at least tcp_rexmit_interval_min which is a fairly 5952 * large value to allow the algorithm to adjust slowly to large 5953 * fluctuations of RTT during first few transmissions. 5954 * 5955 * Starting the timer first and then sending the packet in this 5956 * case shouldn't make much difference since tcp_rexmit_interval_min 5957 * is of the order of several 100ms and starting the timer 5958 * first and then sending the packet will result in difference 5959 * of few micro seconds. 5960 * 5961 * Without this optimization, we are forced to hold the fanout 5962 * lock across the ipcl_bind_insert() and sending the packet 5963 * so that we don't race against an incoming packet (maybe RST) 5964 * for this eager. 5965 */ 5966 5967 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5968 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5969 5970 5971 /* 5972 * Insert the eager in its own perimeter now. We are ready to deal 5973 * with any packets on eager. 5974 */ 5975 if (eager->tcp_ipversion == IPV4_VERSION) { 5976 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5977 goto error; 5978 } 5979 } else { 5980 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5981 goto error; 5982 } 5983 } 5984 5985 /* mark conn as fully-bound */ 5986 econnp->conn_fully_bound = B_TRUE; 5987 5988 /* Send the SYN-ACK */ 5989 tcp_send_data(eager, eager->tcp_wq, mp1); 5990 freemsg(mp); 5991 5992 return; 5993 error: 5994 (void) TCP_TIMER_CANCEL(eager, eager->tcp_timer_tid); 5995 freemsg(mp1); 5996 error1: 5997 /* Undo what we did above */ 5998 mutex_enter(&tcp->tcp_eager_lock); 5999 tcp_eager_unlink(eager); 6000 mutex_exit(&tcp->tcp_eager_lock); 6001 /* Drop eager's reference on the listener */ 6002 CONN_DEC_REF(connp); 6003 6004 /* 6005 * Delete the cached ire in conn_ire_cache and also mark 6006 * the conn as CONDEMNED 6007 */ 6008 mutex_enter(&econnp->conn_lock); 6009 econnp->conn_state_flags |= CONN_CONDEMNED; 6010 ire = econnp->conn_ire_cache; 6011 econnp->conn_ire_cache = NULL; 6012 mutex_exit(&econnp->conn_lock); 6013 if (ire != NULL) 6014 IRE_REFRELE_NOTR(ire); 6015 6016 /* 6017 * tcp_accept_comm inserts the eager to the bind_hash 6018 * we need to remove it from the hash if ipcl_conn_insert 6019 * fails. 6020 */ 6021 tcp_bind_hash_remove(eager); 6022 /* Drop the eager ref placed in tcp_open_detached */ 6023 CONN_DEC_REF(econnp); 6024 6025 /* 6026 * If a connection already exists, send the mp to that connections so 6027 * that it can be appropriately dealt with. 6028 */ 6029 if ((econnp = ipcl_classify(mp, connp->conn_zoneid)) != NULL) { 6030 if (!IPCL_IS_CONNECTED(econnp)) { 6031 /* 6032 * Something bad happened. ipcl_conn_insert() 6033 * failed because a connection already existed 6034 * in connected hash but we can't find it 6035 * anymore (someone blew it away). Just 6036 * free this message and hopefully remote 6037 * will retransmit at which time the SYN can be 6038 * treated as a new connection or dealth with 6039 * a TH_RST if a connection already exists. 6040 */ 6041 CONN_DEC_REF(econnp); 6042 freemsg(mp); 6043 } else { 6044 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6045 econnp, SQTAG_TCP_CONN_REQ); 6046 } 6047 } else { 6048 /* Nobody wants this packet */ 6049 freemsg(mp); 6050 } 6051 return; 6052 error2: 6053 freemsg(mp); 6054 return; 6055 error3: 6056 CONN_DEC_REF(econnp); 6057 freemsg(mp); 6058 } 6059 6060 /* 6061 * In an ideal case of vertical partition in NUMA architecture, its 6062 * beneficial to have the listener and all the incoming connections 6063 * tied to the same squeue. The other constraint is that incoming 6064 * connections should be tied to the squeue attached to interrupted 6065 * CPU for obvious locality reason so this leaves the listener to 6066 * be tied to the same squeue. Our only problem is that when listener 6067 * is binding, the CPU that will get interrupted by the NIC whose 6068 * IP address the listener is binding to is not even known. So 6069 * the code below allows us to change that binding at the time the 6070 * CPU is interrupted by virtue of incoming connection's squeue. 6071 * 6072 * This is usefull only in case of a listener bound to a specific IP 6073 * address. For other kind of listeners, they get bound the 6074 * very first time and there is no attempt to rebind them. 6075 */ 6076 void 6077 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6078 { 6079 conn_t *connp = (conn_t *)arg; 6080 squeue_t *sqp = (squeue_t *)arg2; 6081 squeue_t *new_sqp; 6082 uint32_t conn_flags; 6083 6084 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6085 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6086 } else { 6087 goto done; 6088 } 6089 6090 if (connp->conn_fanout == NULL) 6091 goto done; 6092 6093 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6094 mutex_enter(&connp->conn_fanout->connf_lock); 6095 mutex_enter(&connp->conn_lock); 6096 /* 6097 * No one from read or write side can access us now 6098 * except for already queued packets on this squeue. 6099 * But since we haven't changed the squeue yet, they 6100 * can't execute. If they are processed after we have 6101 * changed the squeue, they are sent back to the 6102 * correct squeue down below. 6103 * But a listner close can race with processing of 6104 * incoming SYN. If incoming SYN processing changes 6105 * the squeue then the listener close which is waiting 6106 * to enter the squeue would operate on the wrong 6107 * squeue. Hence we don't change the squeue here unless 6108 * the refcount is exactly the minimum refcount. The 6109 * minimum refcount of 4 is counted as - 1 each for 6110 * TCP and IP, 1 for being in the classifier hash, and 6111 * 1 for the mblk being processed. 6112 */ 6113 6114 if (connp->conn_ref != 4 || 6115 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6116 mutex_exit(&connp->conn_lock); 6117 mutex_exit(&connp->conn_fanout->connf_lock); 6118 goto done; 6119 } 6120 if (connp->conn_sqp != new_sqp) { 6121 while (connp->conn_sqp != new_sqp) 6122 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6123 } 6124 6125 do { 6126 conn_flags = connp->conn_flags; 6127 conn_flags |= IPCL_FULLY_BOUND; 6128 (void) cas32(&connp->conn_flags, connp->conn_flags, 6129 conn_flags); 6130 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6131 6132 mutex_exit(&connp->conn_fanout->connf_lock); 6133 mutex_exit(&connp->conn_lock); 6134 } 6135 6136 done: 6137 if (connp->conn_sqp != sqp) { 6138 CONN_INC_REF(connp); 6139 squeue_fill(connp->conn_sqp, mp, 6140 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6141 } else { 6142 tcp_conn_request(connp, mp, sqp); 6143 } 6144 } 6145 6146 /* 6147 * Successful connect request processing begins when our client passes 6148 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6149 * our T_OK_ACK reply message upstream. The control flow looks like this: 6150 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6151 * upstream <- tcp_rput() <- IP 6152 * After various error checks are completed, tcp_connect() lays 6153 * the target address and port into the composite header template, 6154 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6155 * request followed by an IRE request, and passes the three mblk message 6156 * down to IP looking like this: 6157 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6158 * Processing continues in tcp_rput() when we receive the following message: 6159 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6160 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6161 * to fire off the connection request, and then passes the T_OK_ACK mblk 6162 * upstream that we filled in below. There are, of course, numerous 6163 * error conditions along the way which truncate the processing described 6164 * above. 6165 */ 6166 static void 6167 tcp_connect(tcp_t *tcp, mblk_t *mp) 6168 { 6169 sin_t *sin; 6170 sin6_t *sin6; 6171 queue_t *q = tcp->tcp_wq; 6172 struct T_conn_req *tcr; 6173 ipaddr_t *dstaddrp; 6174 in_port_t dstport; 6175 uint_t srcid; 6176 6177 tcr = (struct T_conn_req *)mp->b_rptr; 6178 6179 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6180 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6181 tcp_err_ack(tcp, mp, TPROTO, 0); 6182 return; 6183 } 6184 6185 /* 6186 * Determine packet type based on type of address passed in 6187 * the request should contain an IPv4 or IPv6 address. 6188 * Make sure that address family matches the type of 6189 * family of the the address passed down 6190 */ 6191 switch (tcr->DEST_length) { 6192 default: 6193 tcp_err_ack(tcp, mp, TBADADDR, 0); 6194 return; 6195 6196 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6197 /* 6198 * XXX: The check for valid DEST_length was not there 6199 * in earlier releases and some buggy 6200 * TLI apps (e.g Sybase) got away with not feeding 6201 * in sin_zero part of address. 6202 * We allow that bug to keep those buggy apps humming. 6203 * Test suites require the check on DEST_length. 6204 * We construct a new mblk with valid DEST_length 6205 * free the original so the rest of the code does 6206 * not have to keep track of this special shorter 6207 * length address case. 6208 */ 6209 mblk_t *nmp; 6210 struct T_conn_req *ntcr; 6211 sin_t *nsin; 6212 6213 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6214 tcr->OPT_length, BPRI_HI); 6215 if (nmp == NULL) { 6216 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6217 return; 6218 } 6219 ntcr = (struct T_conn_req *)nmp->b_rptr; 6220 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6221 ntcr->PRIM_type = T_CONN_REQ; 6222 ntcr->DEST_length = sizeof (sin_t); 6223 ntcr->DEST_offset = sizeof (struct T_conn_req); 6224 6225 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6226 *nsin = sin_null; 6227 /* Get pointer to shorter address to copy from original mp */ 6228 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6229 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6230 if (sin == NULL || !OK_32PTR((char *)sin)) { 6231 freemsg(nmp); 6232 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6233 return; 6234 } 6235 nsin->sin_family = sin->sin_family; 6236 nsin->sin_port = sin->sin_port; 6237 nsin->sin_addr = sin->sin_addr; 6238 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6239 nmp->b_wptr = (uchar_t *)&nsin[1]; 6240 if (tcr->OPT_length != 0) { 6241 ntcr->OPT_length = tcr->OPT_length; 6242 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6243 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6244 (uchar_t *)ntcr + ntcr->OPT_offset, 6245 tcr->OPT_length); 6246 nmp->b_wptr += tcr->OPT_length; 6247 } 6248 freemsg(mp); /* original mp freed */ 6249 mp = nmp; /* re-initialize original variables */ 6250 tcr = ntcr; 6251 } 6252 /* FALLTHRU */ 6253 6254 case sizeof (sin_t): 6255 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6256 sizeof (sin_t)); 6257 if (sin == NULL || !OK_32PTR((char *)sin)) { 6258 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6259 return; 6260 } 6261 if (tcp->tcp_family != AF_INET || 6262 sin->sin_family != AF_INET) { 6263 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6264 return; 6265 } 6266 if (sin->sin_port == 0) { 6267 tcp_err_ack(tcp, mp, TBADADDR, 0); 6268 return; 6269 } 6270 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6271 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6272 return; 6273 } 6274 6275 break; 6276 6277 case sizeof (sin6_t): 6278 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6279 sizeof (sin6_t)); 6280 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6281 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6282 return; 6283 } 6284 if (tcp->tcp_family != AF_INET6 || 6285 sin6->sin6_family != AF_INET6) { 6286 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6287 return; 6288 } 6289 if (sin6->sin6_port == 0) { 6290 tcp_err_ack(tcp, mp, TBADADDR, 0); 6291 return; 6292 } 6293 break; 6294 } 6295 /* 6296 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6297 * should key on their sequence number and cut them loose. 6298 */ 6299 6300 /* 6301 * If options passed in, feed it for verification and handling 6302 */ 6303 if (tcr->OPT_length != 0) { 6304 mblk_t *ok_mp; 6305 mblk_t *discon_mp; 6306 mblk_t *conn_opts_mp; 6307 int t_error, sys_error, do_disconnect; 6308 6309 conn_opts_mp = NULL; 6310 6311 if (tcp_conprim_opt_process(tcp, mp, 6312 &do_disconnect, &t_error, &sys_error) < 0) { 6313 if (do_disconnect) { 6314 ASSERT(t_error == 0 && sys_error == 0); 6315 discon_mp = mi_tpi_discon_ind(NULL, 6316 ECONNREFUSED, 0); 6317 if (!discon_mp) { 6318 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6319 TSYSERR, ENOMEM); 6320 return; 6321 } 6322 ok_mp = mi_tpi_ok_ack_alloc(mp); 6323 if (!ok_mp) { 6324 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6325 TSYSERR, ENOMEM); 6326 return; 6327 } 6328 qreply(q, ok_mp); 6329 qreply(q, discon_mp); /* no flush! */ 6330 } else { 6331 ASSERT(t_error != 0); 6332 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6333 sys_error); 6334 } 6335 return; 6336 } 6337 /* 6338 * Success in setting options, the mp option buffer represented 6339 * by OPT_length/offset has been potentially modified and 6340 * contains results of option processing. We copy it in 6341 * another mp to save it for potentially influencing returning 6342 * it in T_CONN_CONN. 6343 */ 6344 if (tcr->OPT_length != 0) { /* there are resulting options */ 6345 conn_opts_mp = copyb(mp); 6346 if (!conn_opts_mp) { 6347 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6348 TSYSERR, ENOMEM); 6349 return; 6350 } 6351 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6352 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6353 /* 6354 * Note: 6355 * These resulting option negotiation can include any 6356 * end-to-end negotiation options but there no such 6357 * thing (yet?) in our TCP/IP. 6358 */ 6359 } 6360 } 6361 6362 /* 6363 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6364 * make sure that the template IP header in the tcp structure is an 6365 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6366 * need to this before we call tcp_bindi() so that the port lookup 6367 * code will look for ports in the correct port space (IPv4 and 6368 * IPv6 have separate port spaces). 6369 */ 6370 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6371 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6372 int err = 0; 6373 6374 err = tcp_header_init_ipv4(tcp); 6375 if (err != 0) { 6376 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6377 goto connect_failed; 6378 } 6379 if (tcp->tcp_lport != 0) 6380 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6381 } 6382 6383 switch (tcp->tcp_state) { 6384 case TCPS_IDLE: 6385 /* 6386 * We support quick connect, refer to comments in 6387 * tcp_connect_*() 6388 */ 6389 /* FALLTHRU */ 6390 case TCPS_BOUND: 6391 case TCPS_LISTEN: 6392 if (tcp->tcp_family == AF_INET6) { 6393 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6394 tcp_connect_ipv6(tcp, mp, 6395 &sin6->sin6_addr, 6396 sin6->sin6_port, sin6->sin6_flowinfo, 6397 sin6->__sin6_src_id, sin6->sin6_scope_id); 6398 return; 6399 } 6400 /* 6401 * Destination adress is mapped IPv6 address. 6402 * Source bound address should be unspecified or 6403 * IPv6 mapped address as well. 6404 */ 6405 if (!IN6_IS_ADDR_UNSPECIFIED( 6406 &tcp->tcp_bound_source_v6) && 6407 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6408 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6409 EADDRNOTAVAIL); 6410 break; 6411 } 6412 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6413 dstport = sin6->sin6_port; 6414 srcid = sin6->__sin6_src_id; 6415 } else { 6416 dstaddrp = &sin->sin_addr.s_addr; 6417 dstport = sin->sin_port; 6418 srcid = 0; 6419 } 6420 6421 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6422 return; 6423 default: 6424 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6425 break; 6426 } 6427 /* 6428 * Note: Code below is the "failure" case 6429 */ 6430 /* return error ack and blow away saved option results if any */ 6431 connect_failed: 6432 if (mp != NULL) 6433 putnext(tcp->tcp_rq, mp); 6434 else { 6435 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6436 TSYSERR, ENOMEM); 6437 } 6438 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6439 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6440 } 6441 6442 /* 6443 * Handle connect to IPv4 destinations, including connections for AF_INET6 6444 * sockets connecting to IPv4 mapped IPv6 destinations. 6445 */ 6446 static void 6447 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6448 uint_t srcid) 6449 { 6450 tcph_t *tcph; 6451 mblk_t *mp1; 6452 ipaddr_t dstaddr = *dstaddrp; 6453 int32_t oldstate; 6454 uint16_t lport; 6455 6456 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6457 6458 /* Check for attempt to connect to INADDR_ANY */ 6459 if (dstaddr == INADDR_ANY) { 6460 /* 6461 * SunOS 4.x and 4.3 BSD allow an application 6462 * to connect a TCP socket to INADDR_ANY. 6463 * When they do this, the kernel picks the 6464 * address of one interface and uses it 6465 * instead. The kernel usually ends up 6466 * picking the address of the loopback 6467 * interface. This is an undocumented feature. 6468 * However, we provide the same thing here 6469 * in order to have source and binary 6470 * compatibility with SunOS 4.x. 6471 * Update the T_CONN_REQ (sin/sin6) since it is used to 6472 * generate the T_CONN_CON. 6473 */ 6474 dstaddr = htonl(INADDR_LOOPBACK); 6475 *dstaddrp = dstaddr; 6476 } 6477 6478 /* Handle __sin6_src_id if socket not bound to an IP address */ 6479 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6480 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6481 tcp->tcp_connp->conn_zoneid); 6482 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6483 tcp->tcp_ipha->ipha_src); 6484 } 6485 6486 /* 6487 * Don't let an endpoint connect to itself. Note that 6488 * the test here does not catch the case where the 6489 * source IP addr was left unspecified by the user. In 6490 * this case, the source addr is set in tcp_adapt_ire() 6491 * using the reply to the T_BIND message that we send 6492 * down to IP here and the check is repeated in tcp_rput_other. 6493 */ 6494 if (dstaddr == tcp->tcp_ipha->ipha_src && 6495 dstport == tcp->tcp_lport) { 6496 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6497 goto failed; 6498 } 6499 6500 tcp->tcp_ipha->ipha_dst = dstaddr; 6501 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6502 6503 /* 6504 * Massage a source route if any putting the first hop 6505 * in iph_dst. Compute a starting value for the checksum which 6506 * takes into account that the original iph_dst should be 6507 * included in the checksum but that ip will include the 6508 * first hop in the source route in the tcp checksum. 6509 */ 6510 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha); 6511 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6512 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6513 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6514 if ((int)tcp->tcp_sum < 0) 6515 tcp->tcp_sum--; 6516 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6517 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6518 (tcp->tcp_sum >> 16)); 6519 tcph = tcp->tcp_tcph; 6520 *(uint16_t *)tcph->th_fport = dstport; 6521 tcp->tcp_fport = dstport; 6522 6523 oldstate = tcp->tcp_state; 6524 /* 6525 * At this point the remote destination address and remote port fields 6526 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6527 * have to see which state tcp was in so we can take apropriate action. 6528 */ 6529 if (oldstate == TCPS_IDLE) { 6530 /* 6531 * We support a quick connect capability here, allowing 6532 * clients to transition directly from IDLE to SYN_SENT 6533 * tcp_bindi will pick an unused port, insert the connection 6534 * in the bind hash and transition to BOUND state. 6535 */ 6536 lport = tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 6537 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6538 B_FALSE, B_FALSE); 6539 if (lport == 0) { 6540 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6541 goto failed; 6542 } 6543 } 6544 tcp->tcp_state = TCPS_SYN_SENT; 6545 6546 /* 6547 * TODO: allow data with connect requests 6548 * by unlinking M_DATA trailers here and 6549 * linking them in behind the T_OK_ACK mblk. 6550 * The tcp_rput() bind ack handler would then 6551 * feed them to tcp_wput_data() rather than call 6552 * tcp_timer(). 6553 */ 6554 mp = mi_tpi_ok_ack_alloc(mp); 6555 if (!mp) { 6556 tcp->tcp_state = oldstate; 6557 goto failed; 6558 } 6559 if (tcp->tcp_family == AF_INET) { 6560 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6561 sizeof (ipa_conn_t)); 6562 } else { 6563 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6564 sizeof (ipa6_conn_t)); 6565 } 6566 if (mp1) { 6567 /* Hang onto the T_OK_ACK for later. */ 6568 linkb(mp1, mp); 6569 mblk_setcred(mp1, tcp->tcp_cred); 6570 if (tcp->tcp_family == AF_INET) 6571 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6572 else { 6573 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6574 &tcp->tcp_sticky_ipp); 6575 } 6576 BUMP_MIB(&tcp_mib, tcpActiveOpens); 6577 tcp->tcp_active_open = 1; 6578 /* 6579 * If the bind cannot complete immediately 6580 * IP will arrange to call tcp_rput_other 6581 * when the bind completes. 6582 */ 6583 if (mp1 != NULL) 6584 tcp_rput_other(tcp, mp1); 6585 return; 6586 } 6587 /* Error case */ 6588 tcp->tcp_state = oldstate; 6589 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6590 6591 failed: 6592 /* return error ack and blow away saved option results if any */ 6593 if (mp != NULL) 6594 putnext(tcp->tcp_rq, mp); 6595 else { 6596 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6597 TSYSERR, ENOMEM); 6598 } 6599 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6600 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6601 6602 } 6603 6604 /* 6605 * Handle connect to IPv6 destinations. 6606 */ 6607 static void 6608 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6609 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6610 { 6611 tcph_t *tcph; 6612 mblk_t *mp1; 6613 ip6_rthdr_t *rth; 6614 int32_t oldstate; 6615 uint16_t lport; 6616 6617 ASSERT(tcp->tcp_family == AF_INET6); 6618 6619 /* 6620 * If we're here, it means that the destination address is a native 6621 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6622 * reason why it might not be IPv6 is if the socket was bound to an 6623 * IPv4-mapped IPv6 address. 6624 */ 6625 if (tcp->tcp_ipversion != IPV6_VERSION) { 6626 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6627 goto failed; 6628 } 6629 6630 /* 6631 * Interpret a zero destination to mean loopback. 6632 * Update the T_CONN_REQ (sin/sin6) since it is used to 6633 * generate the T_CONN_CON. 6634 */ 6635 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6636 *dstaddrp = ipv6_loopback; 6637 } 6638 6639 /* Handle __sin6_src_id if socket not bound to an IP address */ 6640 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6641 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6642 tcp->tcp_connp->conn_zoneid); 6643 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6644 } 6645 6646 /* 6647 * Take care of the scope_id now and add ip6i_t 6648 * if ip6i_t is not already allocated through TCP 6649 * sticky options. At this point tcp_ip6h does not 6650 * have dst info, thus use dstaddrp. 6651 */ 6652 if (scope_id != 0 && 6653 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6654 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6655 ip6i_t *ip6i; 6656 6657 ipp->ipp_ifindex = scope_id; 6658 ip6i = (ip6i_t *)tcp->tcp_iphc; 6659 6660 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6661 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6662 /* Already allocated */ 6663 ip6i->ip6i_flags |= IP6I_IFINDEX; 6664 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6665 ipp->ipp_fields |= IPPF_SCOPE_ID; 6666 } else { 6667 int reterr; 6668 6669 ipp->ipp_fields |= IPPF_SCOPE_ID; 6670 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6671 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6672 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6673 if (reterr != 0) 6674 goto failed; 6675 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6676 } 6677 } 6678 6679 /* 6680 * Don't let an endpoint connect to itself. Note that 6681 * the test here does not catch the case where the 6682 * source IP addr was left unspecified by the user. In 6683 * this case, the source addr is set in tcp_adapt_ire() 6684 * using the reply to the T_BIND message that we send 6685 * down to IP here and the check is repeated in tcp_rput_other. 6686 */ 6687 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6688 (dstport == tcp->tcp_lport)) { 6689 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6690 goto failed; 6691 } 6692 6693 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6694 tcp->tcp_remote_v6 = *dstaddrp; 6695 tcp->tcp_ip6h->ip6_vcf = 6696 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6697 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6698 6699 6700 /* 6701 * Massage a routing header (if present) putting the first hop 6702 * in ip6_dst. Compute a starting value for the checksum which 6703 * takes into account that the original ip6_dst should be 6704 * included in the checksum but that ip will include the 6705 * first hop in the source route in the tcp checksum. 6706 */ 6707 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6708 if (rth != NULL) { 6709 6710 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth); 6711 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6712 (tcp->tcp_sum >> 16)); 6713 } else { 6714 tcp->tcp_sum = 0; 6715 } 6716 6717 tcph = tcp->tcp_tcph; 6718 *(uint16_t *)tcph->th_fport = dstport; 6719 tcp->tcp_fport = dstport; 6720 6721 oldstate = tcp->tcp_state; 6722 /* 6723 * At this point the remote destination address and remote port fields 6724 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6725 * have to see which state tcp was in so we can take apropriate action. 6726 */ 6727 if (oldstate == TCPS_IDLE) { 6728 /* 6729 * We support a quick connect capability here, allowing 6730 * clients to transition directly from IDLE to SYN_SENT 6731 * tcp_bindi will pick an unused port, insert the connection 6732 * in the bind hash and transition to BOUND state. 6733 */ 6734 lport = tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 6735 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6736 B_FALSE, B_FALSE); 6737 if (lport == 0) { 6738 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6739 goto failed; 6740 } 6741 } 6742 tcp->tcp_state = TCPS_SYN_SENT; 6743 /* 6744 * TODO: allow data with connect requests 6745 * by unlinking M_DATA trailers here and 6746 * linking them in behind the T_OK_ACK mblk. 6747 * The tcp_rput() bind ack handler would then 6748 * feed them to tcp_wput_data() rather than call 6749 * tcp_timer(). 6750 */ 6751 mp = mi_tpi_ok_ack_alloc(mp); 6752 if (!mp) { 6753 tcp->tcp_state = oldstate; 6754 goto failed; 6755 } 6756 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6757 if (mp1) { 6758 /* Hang onto the T_OK_ACK for later. */ 6759 linkb(mp1, mp); 6760 mblk_setcred(mp1, tcp->tcp_cred); 6761 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6762 &tcp->tcp_sticky_ipp); 6763 BUMP_MIB(&tcp_mib, tcpActiveOpens); 6764 tcp->tcp_active_open = 1; 6765 /* ip_bind_v6() may return ACK or ERROR */ 6766 if (mp1 != NULL) 6767 tcp_rput_other(tcp, mp1); 6768 return; 6769 } 6770 /* Error case */ 6771 tcp->tcp_state = oldstate; 6772 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6773 6774 failed: 6775 /* return error ack and blow away saved option results if any */ 6776 if (mp != NULL) 6777 putnext(tcp->tcp_rq, mp); 6778 else { 6779 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6780 TSYSERR, ENOMEM); 6781 } 6782 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6783 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6784 } 6785 6786 /* 6787 * We need a stream q for detached closing tcp connections 6788 * to use. Our client hereby indicates that this q is the 6789 * one to use. 6790 */ 6791 static void 6792 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6793 { 6794 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6795 queue_t *q = tcp->tcp_wq; 6796 6797 mp->b_datap->db_type = M_IOCACK; 6798 iocp->ioc_count = 0; 6799 mutex_enter(&tcp_g_q_lock); 6800 if (tcp_g_q != NULL) { 6801 mutex_exit(&tcp_g_q_lock); 6802 iocp->ioc_error = EALREADY; 6803 } else { 6804 mblk_t *mp1; 6805 6806 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6807 if (mp1 == NULL) { 6808 mutex_exit(&tcp_g_q_lock); 6809 iocp->ioc_error = ENOMEM; 6810 } else { 6811 tcp_g_q = tcp->tcp_rq; 6812 mutex_exit(&tcp_g_q_lock); 6813 iocp->ioc_error = 0; 6814 iocp->ioc_rval = 0; 6815 /* 6816 * We are passing tcp_sticky_ipp as NULL 6817 * as it is not useful for tcp_default queue 6818 */ 6819 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6820 if (mp1 != NULL) 6821 tcp_rput_other(tcp, mp1); 6822 } 6823 } 6824 qreply(q, mp); 6825 } 6826 6827 /* 6828 * Our client hereby directs us to reject the connection request 6829 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6830 * of sending the appropriate RST, not an ICMP error. 6831 */ 6832 static void 6833 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6834 { 6835 tcp_t *ltcp = NULL; 6836 t_scalar_t seqnum; 6837 conn_t *connp; 6838 6839 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6840 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6841 tcp_err_ack(tcp, mp, TPROTO, 0); 6842 return; 6843 } 6844 6845 /* 6846 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6847 * when the stream is in BOUND state. Do not send a reset, 6848 * since the destination IP address is not valid, and it can 6849 * be the initialized value of all zeros (broadcast address). 6850 * 6851 * If TCP has sent down a bind request to IP and has not 6852 * received the reply, reject the request. Otherwise, TCP 6853 * will be confused. 6854 */ 6855 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6856 if (tcp->tcp_debug) { 6857 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6858 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6859 } 6860 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6861 return; 6862 } 6863 6864 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6865 6866 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6867 6868 /* 6869 * According to TPI, for non-listeners, ignore seqnum 6870 * and disconnect. 6871 * Following interpretation of -1 seqnum is historical 6872 * and implied TPI ? (TPI only states that for T_CONN_IND, 6873 * a valid seqnum should not be -1). 6874 * 6875 * -1 means disconnect everything 6876 * regardless even on a listener. 6877 */ 6878 6879 int old_state = tcp->tcp_state; 6880 6881 /* 6882 * The connection can't be on the tcp_time_wait_head list 6883 * since it is not detached. 6884 */ 6885 ASSERT(tcp->tcp_time_wait_next == NULL); 6886 ASSERT(tcp->tcp_time_wait_prev == NULL); 6887 ASSERT(tcp->tcp_time_wait_expire == 0); 6888 ltcp = NULL; 6889 /* 6890 * If it used to be a listener, check to make sure no one else 6891 * has taken the port before switching back to LISTEN state. 6892 */ 6893 if (tcp->tcp_ipversion == IPV4_VERSION) { 6894 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6895 tcp->tcp_ipha->ipha_src, 6896 tcp->tcp_connp->conn_zoneid); 6897 if (connp != NULL) 6898 ltcp = connp->conn_tcp; 6899 } else { 6900 /* Allow tcp_bound_if listeners? */ 6901 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6902 &tcp->tcp_ip6h->ip6_src, 0, 6903 tcp->tcp_connp->conn_zoneid); 6904 if (connp != NULL) 6905 ltcp = connp->conn_tcp; 6906 } 6907 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6908 tcp->tcp_state = TCPS_LISTEN; 6909 } else if (old_state > TCPS_BOUND) { 6910 tcp->tcp_conn_req_max = 0; 6911 tcp->tcp_state = TCPS_BOUND; 6912 } 6913 if (ltcp != NULL) 6914 CONN_DEC_REF(ltcp->tcp_connp); 6915 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6916 BUMP_MIB(&tcp_mib, tcpAttemptFails); 6917 } else if (old_state == TCPS_ESTABLISHED || 6918 old_state == TCPS_CLOSE_WAIT) { 6919 BUMP_MIB(&tcp_mib, tcpEstabResets); 6920 } 6921 6922 if (tcp->tcp_fused) 6923 tcp_unfuse(tcp); 6924 6925 mutex_enter(&tcp->tcp_eager_lock); 6926 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6927 (tcp->tcp_conn_req_cnt_q != 0)) { 6928 tcp_eager_cleanup(tcp, 0); 6929 } 6930 mutex_exit(&tcp->tcp_eager_lock); 6931 6932 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6933 tcp->tcp_rnxt, TH_RST | TH_ACK); 6934 6935 tcp_reinit(tcp); 6936 6937 if (old_state >= TCPS_ESTABLISHED) { 6938 /* Send M_FLUSH according to TPI */ 6939 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6940 } 6941 mp = mi_tpi_ok_ack_alloc(mp); 6942 if (mp) 6943 putnext(tcp->tcp_rq, mp); 6944 return; 6945 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6946 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6947 return; 6948 } 6949 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6950 /* Send M_FLUSH according to TPI */ 6951 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6952 } 6953 mp = mi_tpi_ok_ack_alloc(mp); 6954 if (mp) 6955 putnext(tcp->tcp_rq, mp); 6956 } 6957 6958 /* 6959 * Diagnostic routine used to return a string associated with the tcp state. 6960 * Note that if the caller does not supply a buffer, it will use an internal 6961 * static string. This means that if multiple threads call this function at 6962 * the same time, output can be corrupted... Note also that this function 6963 * does not check the size of the supplied buffer. The caller has to make 6964 * sure that it is big enough. 6965 */ 6966 static char * 6967 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6968 { 6969 char buf1[30]; 6970 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6971 char *buf; 6972 char *cp; 6973 in6_addr_t local, remote; 6974 char local_addrbuf[INET6_ADDRSTRLEN]; 6975 char remote_addrbuf[INET6_ADDRSTRLEN]; 6976 6977 if (sup_buf != NULL) 6978 buf = sup_buf; 6979 else 6980 buf = priv_buf; 6981 6982 if (tcp == NULL) 6983 return ("NULL_TCP"); 6984 switch (tcp->tcp_state) { 6985 case TCPS_CLOSED: 6986 cp = "TCP_CLOSED"; 6987 break; 6988 case TCPS_IDLE: 6989 cp = "TCP_IDLE"; 6990 break; 6991 case TCPS_BOUND: 6992 cp = "TCP_BOUND"; 6993 break; 6994 case TCPS_LISTEN: 6995 cp = "TCP_LISTEN"; 6996 break; 6997 case TCPS_SYN_SENT: 6998 cp = "TCP_SYN_SENT"; 6999 break; 7000 case TCPS_SYN_RCVD: 7001 cp = "TCP_SYN_RCVD"; 7002 break; 7003 case TCPS_ESTABLISHED: 7004 cp = "TCP_ESTABLISHED"; 7005 break; 7006 case TCPS_CLOSE_WAIT: 7007 cp = "TCP_CLOSE_WAIT"; 7008 break; 7009 case TCPS_FIN_WAIT_1: 7010 cp = "TCP_FIN_WAIT_1"; 7011 break; 7012 case TCPS_CLOSING: 7013 cp = "TCP_CLOSING"; 7014 break; 7015 case TCPS_LAST_ACK: 7016 cp = "TCP_LAST_ACK"; 7017 break; 7018 case TCPS_FIN_WAIT_2: 7019 cp = "TCP_FIN_WAIT_2"; 7020 break; 7021 case TCPS_TIME_WAIT: 7022 cp = "TCP_TIME_WAIT"; 7023 break; 7024 default: 7025 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7026 cp = buf1; 7027 break; 7028 } 7029 switch (format) { 7030 case DISP_ADDR_AND_PORT: 7031 if (tcp->tcp_ipversion == IPV4_VERSION) { 7032 /* 7033 * Note that we use the remote address in the tcp_b 7034 * structure. This means that it will print out 7035 * the real destination address, not the next hop's 7036 * address if source routing is used. 7037 */ 7038 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7039 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7040 7041 } else { 7042 local = tcp->tcp_ip_src_v6; 7043 remote = tcp->tcp_remote_v6; 7044 } 7045 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7046 sizeof (local_addrbuf)); 7047 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7048 sizeof (remote_addrbuf)); 7049 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7050 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7051 ntohs(tcp->tcp_fport), cp); 7052 break; 7053 case DISP_PORT_ONLY: 7054 default: 7055 (void) mi_sprintf(buf, "[%u, %u] %s", 7056 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7057 break; 7058 } 7059 7060 return (buf); 7061 } 7062 7063 /* 7064 * Called via squeue to get on to eager's perimeter to send a 7065 * TH_RST. The listener wants the eager to disappear either 7066 * by means of tcp_eager_blowoff() or tcp_eager_cleanup() 7067 * being called. 7068 */ 7069 /* ARGSUSED */ 7070 void 7071 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7072 { 7073 conn_t *econnp = (conn_t *)arg; 7074 tcp_t *eager = econnp->conn_tcp; 7075 tcp_t *listener = eager->tcp_listener; 7076 7077 /* 7078 * We could be called because listener is closing. Since 7079 * the eager is using listener's queue's, its not safe. 7080 * Better use the default queue just to send the TH_RST 7081 * out. 7082 */ 7083 eager->tcp_rq = tcp_g_q; 7084 eager->tcp_wq = WR(tcp_g_q); 7085 7086 if (eager->tcp_state > TCPS_LISTEN) { 7087 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7088 eager, eager->tcp_snxt, 0, TH_RST); 7089 } 7090 7091 /* We are here because listener wants this eager gone */ 7092 if (listener != NULL) { 7093 mutex_enter(&listener->tcp_eager_lock); 7094 tcp_eager_unlink(eager); 7095 if (eager->tcp_tconnind_started) { 7096 /* 7097 * The eager has sent a conn_ind up to the 7098 * listener but listener decides to close 7099 * instead. We need to drop the extra ref 7100 * placed on eager in tcp_rput_data() before 7101 * sending the conn_ind to listener. 7102 */ 7103 CONN_DEC_REF(econnp); 7104 } 7105 mutex_exit(&listener->tcp_eager_lock); 7106 CONN_DEC_REF(listener->tcp_connp); 7107 } 7108 7109 if (eager->tcp_state > TCPS_BOUND) 7110 tcp_close_detached(eager); 7111 } 7112 7113 /* 7114 * Reset any eager connection hanging off this listener marked 7115 * with 'seqnum' and then reclaim it's resources. 7116 */ 7117 static boolean_t 7118 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7119 { 7120 tcp_t *eager; 7121 mblk_t *mp; 7122 7123 TCP_STAT(tcp_eager_blowoff_calls); 7124 eager = listener; 7125 mutex_enter(&listener->tcp_eager_lock); 7126 do { 7127 eager = eager->tcp_eager_next_q; 7128 if (eager == NULL) { 7129 mutex_exit(&listener->tcp_eager_lock); 7130 return (B_FALSE); 7131 } 7132 } while (eager->tcp_conn_req_seqnum != seqnum); 7133 7134 if (eager->tcp_closemp_used > 0) { 7135 mutex_exit(&listener->tcp_eager_lock); 7136 return (B_TRUE); 7137 } 7138 eager->tcp_closemp_used = 1; 7139 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7140 CONN_INC_REF(eager->tcp_connp); 7141 mutex_exit(&listener->tcp_eager_lock); 7142 mp = &eager->tcp_closemp; 7143 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7144 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7145 return (B_TRUE); 7146 } 7147 7148 /* 7149 * Reset any eager connection hanging off this listener 7150 * and then reclaim it's resources. 7151 */ 7152 static void 7153 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7154 { 7155 tcp_t *eager; 7156 mblk_t *mp; 7157 7158 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7159 7160 if (!q0_only) { 7161 /* First cleanup q */ 7162 TCP_STAT(tcp_eager_blowoff_q); 7163 eager = listener->tcp_eager_next_q; 7164 while (eager != NULL) { 7165 if (eager->tcp_closemp_used == 0) { 7166 eager->tcp_closemp_used = 1; 7167 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7168 CONN_INC_REF(eager->tcp_connp); 7169 mp = &eager->tcp_closemp; 7170 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7171 tcp_eager_kill, eager->tcp_connp, 7172 SQTAG_TCP_EAGER_CLEANUP); 7173 } 7174 eager = eager->tcp_eager_next_q; 7175 } 7176 } 7177 /* Then cleanup q0 */ 7178 TCP_STAT(tcp_eager_blowoff_q0); 7179 eager = listener->tcp_eager_next_q0; 7180 while (eager != listener) { 7181 if (eager->tcp_closemp_used == 0) { 7182 eager->tcp_closemp_used = 1; 7183 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7184 CONN_INC_REF(eager->tcp_connp); 7185 mp = &eager->tcp_closemp; 7186 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7187 tcp_eager_kill, eager->tcp_connp, 7188 SQTAG_TCP_EAGER_CLEANUP_Q0); 7189 } 7190 eager = eager->tcp_eager_next_q0; 7191 } 7192 } 7193 7194 /* 7195 * If we are an eager connection hanging off a listener that hasn't 7196 * formally accepted the connection yet, get off his list and blow off 7197 * any data that we have accumulated. 7198 */ 7199 static void 7200 tcp_eager_unlink(tcp_t *tcp) 7201 { 7202 tcp_t *listener = tcp->tcp_listener; 7203 7204 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7205 ASSERT(listener != NULL); 7206 if (tcp->tcp_eager_next_q0 != NULL) { 7207 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7208 7209 /* Remove the eager tcp from q0 */ 7210 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7211 tcp->tcp_eager_prev_q0; 7212 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7213 tcp->tcp_eager_next_q0; 7214 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7215 listener->tcp_conn_req_cnt_q0--; 7216 7217 tcp->tcp_eager_next_q0 = NULL; 7218 tcp->tcp_eager_prev_q0 = NULL; 7219 7220 /* 7221 * Take the eager out, if it is in the list of droppable 7222 * eagers. 7223 */ 7224 MAKE_UNDROPPABLE(tcp); 7225 7226 if (tcp->tcp_syn_rcvd_timeout != 0) { 7227 /* we have timed out before */ 7228 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7229 listener->tcp_syn_rcvd_timeout--; 7230 } 7231 } else { 7232 tcp_t **tcpp = &listener->tcp_eager_next_q; 7233 tcp_t *prev = NULL; 7234 7235 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7236 if (tcpp[0] == tcp) { 7237 if (listener->tcp_eager_last_q == tcp) { 7238 /* 7239 * If we are unlinking the last 7240 * element on the list, adjust 7241 * tail pointer. Set tail pointer 7242 * to nil when list is empty. 7243 */ 7244 ASSERT(tcp->tcp_eager_next_q == NULL); 7245 if (listener->tcp_eager_last_q == 7246 listener->tcp_eager_next_q) { 7247 listener->tcp_eager_last_q = 7248 NULL; 7249 } else { 7250 /* 7251 * We won't get here if there 7252 * is only one eager in the 7253 * list. 7254 */ 7255 ASSERT(prev != NULL); 7256 listener->tcp_eager_last_q = 7257 prev; 7258 } 7259 } 7260 tcpp[0] = tcp->tcp_eager_next_q; 7261 tcp->tcp_eager_next_q = NULL; 7262 tcp->tcp_eager_last_q = NULL; 7263 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7264 listener->tcp_conn_req_cnt_q--; 7265 break; 7266 } 7267 prev = tcpp[0]; 7268 } 7269 } 7270 tcp->tcp_listener = NULL; 7271 } 7272 7273 /* Shorthand to generate and send TPI error acks to our client */ 7274 static void 7275 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7276 { 7277 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7278 putnext(tcp->tcp_rq, mp); 7279 } 7280 7281 /* Shorthand to generate and send TPI error acks to our client */ 7282 static void 7283 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7284 int t_error, int sys_error) 7285 { 7286 struct T_error_ack *teackp; 7287 7288 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7289 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7290 teackp = (struct T_error_ack *)mp->b_rptr; 7291 teackp->ERROR_prim = primitive; 7292 teackp->TLI_error = t_error; 7293 teackp->UNIX_error = sys_error; 7294 putnext(tcp->tcp_rq, mp); 7295 } 7296 } 7297 7298 /* 7299 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7300 * but instead the code relies on: 7301 * - the fact that the address of the array and its size never changes 7302 * - the atomic assignment of the elements of the array 7303 */ 7304 /* ARGSUSED */ 7305 static int 7306 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7307 { 7308 int i; 7309 7310 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7311 if (tcp_g_epriv_ports[i] != 0) 7312 (void) mi_mpprintf(mp, "%d ", tcp_g_epriv_ports[i]); 7313 } 7314 return (0); 7315 } 7316 7317 /* 7318 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7319 * threads from changing it at the same time. 7320 */ 7321 /* ARGSUSED */ 7322 static int 7323 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7324 cred_t *cr) 7325 { 7326 long new_value; 7327 int i; 7328 7329 /* 7330 * Fail the request if the new value does not lie within the 7331 * port number limits. 7332 */ 7333 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7334 new_value <= 0 || new_value >= 65536) { 7335 return (EINVAL); 7336 } 7337 7338 mutex_enter(&tcp_epriv_port_lock); 7339 /* Check if the value is already in the list */ 7340 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7341 if (new_value == tcp_g_epriv_ports[i]) { 7342 mutex_exit(&tcp_epriv_port_lock); 7343 return (EEXIST); 7344 } 7345 } 7346 /* Find an empty slot */ 7347 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7348 if (tcp_g_epriv_ports[i] == 0) 7349 break; 7350 } 7351 if (i == tcp_g_num_epriv_ports) { 7352 mutex_exit(&tcp_epriv_port_lock); 7353 return (EOVERFLOW); 7354 } 7355 /* Set the new value */ 7356 tcp_g_epriv_ports[i] = (uint16_t)new_value; 7357 mutex_exit(&tcp_epriv_port_lock); 7358 return (0); 7359 } 7360 7361 /* 7362 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7363 * threads from changing it at the same time. 7364 */ 7365 /* ARGSUSED */ 7366 static int 7367 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7368 cred_t *cr) 7369 { 7370 long new_value; 7371 int i; 7372 7373 /* 7374 * Fail the request if the new value does not lie within the 7375 * port number limits. 7376 */ 7377 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7378 new_value >= 65536) { 7379 return (EINVAL); 7380 } 7381 7382 mutex_enter(&tcp_epriv_port_lock); 7383 /* Check that the value is already in the list */ 7384 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7385 if (tcp_g_epriv_ports[i] == new_value) 7386 break; 7387 } 7388 if (i == tcp_g_num_epriv_ports) { 7389 mutex_exit(&tcp_epriv_port_lock); 7390 return (ESRCH); 7391 } 7392 /* Clear the value */ 7393 tcp_g_epriv_ports[i] = 0; 7394 mutex_exit(&tcp_epriv_port_lock); 7395 return (0); 7396 } 7397 7398 /* Return the TPI/TLI equivalent of our current tcp_state */ 7399 static int 7400 tcp_tpistate(tcp_t *tcp) 7401 { 7402 switch (tcp->tcp_state) { 7403 case TCPS_IDLE: 7404 return (TS_UNBND); 7405 case TCPS_LISTEN: 7406 /* 7407 * Return whether there are outstanding T_CONN_IND waiting 7408 * for the matching T_CONN_RES. Therefore don't count q0. 7409 */ 7410 if (tcp->tcp_conn_req_cnt_q > 0) 7411 return (TS_WRES_CIND); 7412 else 7413 return (TS_IDLE); 7414 case TCPS_BOUND: 7415 return (TS_IDLE); 7416 case TCPS_SYN_SENT: 7417 return (TS_WCON_CREQ); 7418 case TCPS_SYN_RCVD: 7419 /* 7420 * Note: assumption: this has to the active open SYN_RCVD. 7421 * The passive instance is detached in SYN_RCVD stage of 7422 * incoming connection processing so we cannot get request 7423 * for T_info_ack on it. 7424 */ 7425 return (TS_WACK_CRES); 7426 case TCPS_ESTABLISHED: 7427 return (TS_DATA_XFER); 7428 case TCPS_CLOSE_WAIT: 7429 return (TS_WREQ_ORDREL); 7430 case TCPS_FIN_WAIT_1: 7431 return (TS_WIND_ORDREL); 7432 case TCPS_FIN_WAIT_2: 7433 return (TS_WIND_ORDREL); 7434 7435 case TCPS_CLOSING: 7436 case TCPS_LAST_ACK: 7437 case TCPS_TIME_WAIT: 7438 case TCPS_CLOSED: 7439 /* 7440 * Following TS_WACK_DREQ7 is a rendition of "not 7441 * yet TS_IDLE" TPI state. There is no best match to any 7442 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7443 * choose a value chosen that will map to TLI/XTI level 7444 * state of TSTATECHNG (state is process of changing) which 7445 * captures what this dummy state represents. 7446 */ 7447 return (TS_WACK_DREQ7); 7448 default: 7449 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7450 tcp->tcp_state, tcp_display(tcp, NULL, 7451 DISP_PORT_ONLY)); 7452 return (TS_UNBND); 7453 } 7454 } 7455 7456 static void 7457 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7458 { 7459 if (tcp->tcp_family == AF_INET6) 7460 *tia = tcp_g_t_info_ack_v6; 7461 else 7462 *tia = tcp_g_t_info_ack; 7463 tia->CURRENT_state = tcp_tpistate(tcp); 7464 tia->OPT_size = tcp_max_optsize; 7465 if (tcp->tcp_mss == 0) { 7466 /* Not yet set - tcp_open does not set mss */ 7467 if (tcp->tcp_ipversion == IPV4_VERSION) 7468 tia->TIDU_size = tcp_mss_def_ipv4; 7469 else 7470 tia->TIDU_size = tcp_mss_def_ipv6; 7471 } else { 7472 tia->TIDU_size = tcp->tcp_mss; 7473 } 7474 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7475 } 7476 7477 /* 7478 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7479 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7480 * tcp_g_t_info_ack. The current state of the stream is copied from 7481 * tcp_state. 7482 */ 7483 static void 7484 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7485 { 7486 t_uscalar_t cap_bits1; 7487 struct T_capability_ack *tcap; 7488 7489 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7490 freemsg(mp); 7491 return; 7492 } 7493 7494 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7495 7496 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7497 mp->b_datap->db_type, T_CAPABILITY_ACK); 7498 if (mp == NULL) 7499 return; 7500 7501 tcap = (struct T_capability_ack *)mp->b_rptr; 7502 tcap->CAP_bits1 = 0; 7503 7504 if (cap_bits1 & TC1_INFO) { 7505 tcp_copy_info(&tcap->INFO_ack, tcp); 7506 tcap->CAP_bits1 |= TC1_INFO; 7507 } 7508 7509 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7510 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7511 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7512 } 7513 7514 putnext(tcp->tcp_rq, mp); 7515 } 7516 7517 /* 7518 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7519 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7520 * The current state of the stream is copied from tcp_state. 7521 */ 7522 static void 7523 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7524 { 7525 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7526 T_INFO_ACK); 7527 if (!mp) { 7528 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7529 return; 7530 } 7531 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7532 putnext(tcp->tcp_rq, mp); 7533 } 7534 7535 /* Respond to the TPI addr request */ 7536 static void 7537 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7538 { 7539 sin_t *sin; 7540 mblk_t *ackmp; 7541 struct T_addr_ack *taa; 7542 7543 /* Make it large enough for worst case */ 7544 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7545 2 * sizeof (sin6_t), 1); 7546 if (ackmp == NULL) { 7547 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7548 return; 7549 } 7550 7551 if (tcp->tcp_ipversion == IPV6_VERSION) { 7552 tcp_addr_req_ipv6(tcp, ackmp); 7553 return; 7554 } 7555 taa = (struct T_addr_ack *)ackmp->b_rptr; 7556 7557 bzero(taa, sizeof (struct T_addr_ack)); 7558 ackmp->b_wptr = (uchar_t *)&taa[1]; 7559 7560 taa->PRIM_type = T_ADDR_ACK; 7561 ackmp->b_datap->db_type = M_PCPROTO; 7562 7563 /* 7564 * Note: Following code assumes 32 bit alignment of basic 7565 * data structures like sin_t and struct T_addr_ack. 7566 */ 7567 if (tcp->tcp_state >= TCPS_BOUND) { 7568 /* 7569 * Fill in local address 7570 */ 7571 taa->LOCADDR_length = sizeof (sin_t); 7572 taa->LOCADDR_offset = sizeof (*taa); 7573 7574 sin = (sin_t *)&taa[1]; 7575 7576 /* Fill zeroes and then intialize non-zero fields */ 7577 *sin = sin_null; 7578 7579 sin->sin_family = AF_INET; 7580 7581 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7582 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7583 7584 ackmp->b_wptr = (uchar_t *)&sin[1]; 7585 7586 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7587 /* 7588 * Fill in Remote address 7589 */ 7590 taa->REMADDR_length = sizeof (sin_t); 7591 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7592 taa->LOCADDR_length); 7593 7594 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7595 *sin = sin_null; 7596 sin->sin_family = AF_INET; 7597 sin->sin_addr.s_addr = tcp->tcp_remote; 7598 sin->sin_port = tcp->tcp_fport; 7599 7600 ackmp->b_wptr = (uchar_t *)&sin[1]; 7601 } 7602 } 7603 putnext(tcp->tcp_rq, ackmp); 7604 } 7605 7606 /* Assumes that tcp_addr_req gets enough space and alignment */ 7607 static void 7608 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7609 { 7610 sin6_t *sin6; 7611 struct T_addr_ack *taa; 7612 7613 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7614 ASSERT(OK_32PTR(ackmp->b_rptr)); 7615 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7616 2 * sizeof (sin6_t)); 7617 7618 taa = (struct T_addr_ack *)ackmp->b_rptr; 7619 7620 bzero(taa, sizeof (struct T_addr_ack)); 7621 ackmp->b_wptr = (uchar_t *)&taa[1]; 7622 7623 taa->PRIM_type = T_ADDR_ACK; 7624 ackmp->b_datap->db_type = M_PCPROTO; 7625 7626 /* 7627 * Note: Following code assumes 32 bit alignment of basic 7628 * data structures like sin6_t and struct T_addr_ack. 7629 */ 7630 if (tcp->tcp_state >= TCPS_BOUND) { 7631 /* 7632 * Fill in local address 7633 */ 7634 taa->LOCADDR_length = sizeof (sin6_t); 7635 taa->LOCADDR_offset = sizeof (*taa); 7636 7637 sin6 = (sin6_t *)&taa[1]; 7638 *sin6 = sin6_null; 7639 7640 sin6->sin6_family = AF_INET6; 7641 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7642 sin6->sin6_port = tcp->tcp_lport; 7643 7644 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7645 7646 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7647 /* 7648 * Fill in Remote address 7649 */ 7650 taa->REMADDR_length = sizeof (sin6_t); 7651 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7652 taa->LOCADDR_length); 7653 7654 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7655 *sin6 = sin6_null; 7656 sin6->sin6_family = AF_INET6; 7657 sin6->sin6_flowinfo = 7658 tcp->tcp_ip6h->ip6_vcf & 7659 ~IPV6_VERS_AND_FLOW_MASK; 7660 sin6->sin6_addr = tcp->tcp_remote_v6; 7661 sin6->sin6_port = tcp->tcp_fport; 7662 7663 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7664 } 7665 } 7666 putnext(tcp->tcp_rq, ackmp); 7667 } 7668 7669 /* 7670 * Handle reinitialization of a tcp structure. 7671 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7672 */ 7673 static void 7674 tcp_reinit(tcp_t *tcp) 7675 { 7676 mblk_t *mp; 7677 int err; 7678 7679 TCP_STAT(tcp_reinit_calls); 7680 7681 /* tcp_reinit should never be called for detached tcp_t's */ 7682 ASSERT(tcp->tcp_listener == NULL); 7683 ASSERT((tcp->tcp_family == AF_INET && 7684 tcp->tcp_ipversion == IPV4_VERSION) || 7685 (tcp->tcp_family == AF_INET6 && 7686 (tcp->tcp_ipversion == IPV4_VERSION || 7687 tcp->tcp_ipversion == IPV6_VERSION))); 7688 7689 /* Cancel outstanding timers */ 7690 tcp_timers_stop(tcp); 7691 7692 /* 7693 * Reset everything in the state vector, after updating global 7694 * MIB data from instance counters. 7695 */ 7696 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 7697 tcp->tcp_ibsegs = 0; 7698 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 7699 tcp->tcp_obsegs = 0; 7700 7701 tcp_close_mpp(&tcp->tcp_xmit_head); 7702 if (tcp->tcp_snd_zcopy_aware) 7703 tcp_zcopy_notify(tcp); 7704 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7705 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7706 if (tcp->tcp_flow_stopped && 7707 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7708 tcp_clrqfull(tcp); 7709 } 7710 tcp_close_mpp(&tcp->tcp_reass_head); 7711 tcp->tcp_reass_tail = NULL; 7712 if (tcp->tcp_rcv_list != NULL) { 7713 /* Free b_next chain */ 7714 tcp_close_mpp(&tcp->tcp_rcv_list); 7715 tcp->tcp_rcv_last_head = NULL; 7716 tcp->tcp_rcv_last_tail = NULL; 7717 tcp->tcp_rcv_cnt = 0; 7718 } 7719 tcp->tcp_rcv_last_tail = NULL; 7720 7721 if ((mp = tcp->tcp_urp_mp) != NULL) { 7722 freemsg(mp); 7723 tcp->tcp_urp_mp = NULL; 7724 } 7725 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7726 freemsg(mp); 7727 tcp->tcp_urp_mark_mp = NULL; 7728 } 7729 if (tcp->tcp_fused_sigurg_mp != NULL) { 7730 freeb(tcp->tcp_fused_sigurg_mp); 7731 tcp->tcp_fused_sigurg_mp = NULL; 7732 } 7733 7734 /* 7735 * Following is a union with two members which are 7736 * identical types and size so the following cleanup 7737 * is enough. 7738 */ 7739 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7740 7741 CL_INET_DISCONNECT(tcp); 7742 7743 /* 7744 * The connection can't be on the tcp_time_wait_head list 7745 * since it is not detached. 7746 */ 7747 ASSERT(tcp->tcp_time_wait_next == NULL); 7748 ASSERT(tcp->tcp_time_wait_prev == NULL); 7749 ASSERT(tcp->tcp_time_wait_expire == 0); 7750 7751 if (tcp->tcp_kssl_pending) { 7752 tcp->tcp_kssl_pending = B_FALSE; 7753 7754 /* Don't reset if the initialized by bind. */ 7755 if (tcp->tcp_kssl_ent != NULL) { 7756 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7757 KSSL_NO_PROXY); 7758 } 7759 } 7760 if (tcp->tcp_kssl_ctx != NULL) { 7761 kssl_release_ctx(tcp->tcp_kssl_ctx); 7762 tcp->tcp_kssl_ctx = NULL; 7763 } 7764 7765 /* 7766 * Reset/preserve other values 7767 */ 7768 tcp_reinit_values(tcp); 7769 ipcl_hash_remove(tcp->tcp_connp); 7770 conn_delete_ire(tcp->tcp_connp, NULL); 7771 7772 if (tcp->tcp_conn_req_max != 0) { 7773 /* 7774 * This is the case when a TLI program uses the same 7775 * transport end point to accept a connection. This 7776 * makes the TCP both a listener and acceptor. When 7777 * this connection is closed, we need to set the state 7778 * back to TCPS_LISTEN. Make sure that the eager list 7779 * is reinitialized. 7780 * 7781 * Note that this stream is still bound to the four 7782 * tuples of the previous connection in IP. If a new 7783 * SYN with different foreign address comes in, IP will 7784 * not find it and will send it to the global queue. In 7785 * the global queue, TCP will do a tcp_lookup_listener() 7786 * to find this stream. This works because this stream 7787 * is only removed from connected hash. 7788 * 7789 */ 7790 tcp->tcp_state = TCPS_LISTEN; 7791 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7792 tcp->tcp_eager_next_drop_q0 = tcp; 7793 tcp->tcp_eager_prev_drop_q0 = tcp; 7794 tcp->tcp_connp->conn_recv = tcp_conn_request; 7795 if (tcp->tcp_family == AF_INET6) { 7796 ASSERT(tcp->tcp_connp->conn_af_isv6); 7797 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7798 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7799 } else { 7800 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7801 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7802 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7803 } 7804 } else { 7805 tcp->tcp_state = TCPS_BOUND; 7806 } 7807 7808 /* 7809 * Initialize to default values 7810 * Can't fail since enough header template space already allocated 7811 * at open(). 7812 */ 7813 err = tcp_init_values(tcp); 7814 ASSERT(err == 0); 7815 /* Restore state in tcp_tcph */ 7816 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7817 if (tcp->tcp_ipversion == IPV4_VERSION) 7818 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7819 else 7820 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7821 /* 7822 * Copy of the src addr. in tcp_t is needed in tcp_t 7823 * since the lookup funcs can only lookup on tcp_t 7824 */ 7825 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7826 7827 ASSERT(tcp->tcp_ptpbhn != NULL); 7828 tcp->tcp_rq->q_hiwat = tcp_recv_hiwat; 7829 tcp->tcp_rwnd = tcp_recv_hiwat; 7830 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7831 tcp_mss_def_ipv6 : tcp_mss_def_ipv4; 7832 } 7833 7834 /* 7835 * Force values to zero that need be zero. 7836 * Do not touch values asociated with the BOUND or LISTEN state 7837 * since the connection will end up in that state after the reinit. 7838 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7839 * structure! 7840 */ 7841 static void 7842 tcp_reinit_values(tcp) 7843 tcp_t *tcp; 7844 { 7845 #ifndef lint 7846 #define DONTCARE(x) 7847 #define PRESERVE(x) 7848 #else 7849 #define DONTCARE(x) ((x) = (x)) 7850 #define PRESERVE(x) ((x) = (x)) 7851 #endif /* lint */ 7852 7853 PRESERVE(tcp->tcp_bind_hash); 7854 PRESERVE(tcp->tcp_ptpbhn); 7855 PRESERVE(tcp->tcp_acceptor_hash); 7856 PRESERVE(tcp->tcp_ptpahn); 7857 7858 /* Should be ASSERT NULL on these with new code! */ 7859 ASSERT(tcp->tcp_time_wait_next == NULL); 7860 ASSERT(tcp->tcp_time_wait_prev == NULL); 7861 ASSERT(tcp->tcp_time_wait_expire == 0); 7862 PRESERVE(tcp->tcp_state); 7863 PRESERVE(tcp->tcp_rq); 7864 PRESERVE(tcp->tcp_wq); 7865 7866 ASSERT(tcp->tcp_xmit_head == NULL); 7867 ASSERT(tcp->tcp_xmit_last == NULL); 7868 ASSERT(tcp->tcp_unsent == 0); 7869 ASSERT(tcp->tcp_xmit_tail == NULL); 7870 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7871 7872 tcp->tcp_snxt = 0; /* Displayed in mib */ 7873 tcp->tcp_suna = 0; /* Displayed in mib */ 7874 tcp->tcp_swnd = 0; 7875 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7876 7877 ASSERT(tcp->tcp_ibsegs == 0); 7878 ASSERT(tcp->tcp_obsegs == 0); 7879 7880 if (tcp->tcp_iphc != NULL) { 7881 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7882 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7883 } 7884 7885 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7886 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7887 DONTCARE(tcp->tcp_ipha); 7888 DONTCARE(tcp->tcp_ip6h); 7889 DONTCARE(tcp->tcp_ip_hdr_len); 7890 DONTCARE(tcp->tcp_tcph); 7891 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7892 tcp->tcp_valid_bits = 0; 7893 7894 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7895 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7896 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7897 tcp->tcp_last_rcv_lbolt = 0; 7898 7899 tcp->tcp_init_cwnd = 0; 7900 7901 tcp->tcp_urp_last_valid = 0; 7902 tcp->tcp_hard_binding = 0; 7903 tcp->tcp_hard_bound = 0; 7904 PRESERVE(tcp->tcp_cred); 7905 PRESERVE(tcp->tcp_cpid); 7906 PRESERVE(tcp->tcp_exclbind); 7907 7908 tcp->tcp_fin_acked = 0; 7909 tcp->tcp_fin_rcvd = 0; 7910 tcp->tcp_fin_sent = 0; 7911 tcp->tcp_ordrel_done = 0; 7912 7913 tcp->tcp_debug = 0; 7914 tcp->tcp_dontroute = 0; 7915 tcp->tcp_broadcast = 0; 7916 7917 tcp->tcp_useloopback = 0; 7918 tcp->tcp_reuseaddr = 0; 7919 tcp->tcp_oobinline = 0; 7920 tcp->tcp_dgram_errind = 0; 7921 7922 tcp->tcp_detached = 0; 7923 tcp->tcp_bind_pending = 0; 7924 tcp->tcp_unbind_pending = 0; 7925 tcp->tcp_deferred_clean_death = 0; 7926 7927 tcp->tcp_snd_ws_ok = B_FALSE; 7928 tcp->tcp_snd_ts_ok = B_FALSE; 7929 tcp->tcp_linger = 0; 7930 tcp->tcp_ka_enabled = 0; 7931 tcp->tcp_zero_win_probe = 0; 7932 7933 tcp->tcp_loopback = 0; 7934 tcp->tcp_localnet = 0; 7935 tcp->tcp_syn_defense = 0; 7936 tcp->tcp_set_timer = 0; 7937 7938 tcp->tcp_active_open = 0; 7939 ASSERT(tcp->tcp_timeout == B_FALSE); 7940 tcp->tcp_rexmit = B_FALSE; 7941 tcp->tcp_xmit_zc_clean = B_FALSE; 7942 7943 tcp->tcp_snd_sack_ok = B_FALSE; 7944 PRESERVE(tcp->tcp_recvdstaddr); 7945 tcp->tcp_hwcksum = B_FALSE; 7946 7947 tcp->tcp_ire_ill_check_done = B_FALSE; 7948 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7949 7950 tcp->tcp_mdt = B_FALSE; 7951 tcp->tcp_mdt_hdr_head = 0; 7952 tcp->tcp_mdt_hdr_tail = 0; 7953 7954 tcp->tcp_conn_def_q0 = 0; 7955 tcp->tcp_ip_forward_progress = B_FALSE; 7956 tcp->tcp_anon_priv_bind = 0; 7957 tcp->tcp_ecn_ok = B_FALSE; 7958 7959 tcp->tcp_cwr = B_FALSE; 7960 tcp->tcp_ecn_echo_on = B_FALSE; 7961 7962 if (tcp->tcp_sack_info != NULL) { 7963 if (tcp->tcp_notsack_list != NULL) { 7964 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7965 } 7966 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7967 tcp->tcp_sack_info = NULL; 7968 } 7969 7970 tcp->tcp_rcv_ws = 0; 7971 tcp->tcp_snd_ws = 0; 7972 tcp->tcp_ts_recent = 0; 7973 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7974 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7975 tcp->tcp_if_mtu = 0; 7976 7977 ASSERT(tcp->tcp_reass_head == NULL); 7978 ASSERT(tcp->tcp_reass_tail == NULL); 7979 7980 tcp->tcp_cwnd_cnt = 0; 7981 7982 ASSERT(tcp->tcp_rcv_list == NULL); 7983 ASSERT(tcp->tcp_rcv_last_head == NULL); 7984 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7985 ASSERT(tcp->tcp_rcv_cnt == 0); 7986 7987 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7988 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7989 tcp->tcp_csuna = 0; 7990 7991 tcp->tcp_rto = 0; /* Displayed in MIB */ 7992 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7993 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7994 tcp->tcp_rtt_update = 0; 7995 7996 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7997 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7998 7999 tcp->tcp_rack = 0; /* Displayed in mib */ 8000 tcp->tcp_rack_cnt = 0; 8001 tcp->tcp_rack_cur_max = 0; 8002 tcp->tcp_rack_abs_max = 0; 8003 8004 tcp->tcp_max_swnd = 0; 8005 8006 ASSERT(tcp->tcp_listener == NULL); 8007 8008 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8009 8010 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8011 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8012 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8013 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8014 8015 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8016 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8017 PRESERVE(tcp->tcp_conn_req_max); 8018 PRESERVE(tcp->tcp_conn_req_seqnum); 8019 8020 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8021 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8022 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8023 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8024 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8025 8026 tcp->tcp_lingertime = 0; 8027 8028 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8029 ASSERT(tcp->tcp_urp_mp == NULL); 8030 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8031 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8032 8033 ASSERT(tcp->tcp_eager_next_q == NULL); 8034 ASSERT(tcp->tcp_eager_last_q == NULL); 8035 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8036 tcp->tcp_eager_prev_q0 == NULL) || 8037 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8038 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8039 8040 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8041 tcp->tcp_eager_prev_drop_q0 == NULL) || 8042 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8043 8044 tcp->tcp_client_errno = 0; 8045 8046 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8047 8048 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8049 8050 PRESERVE(tcp->tcp_bound_source_v6); 8051 tcp->tcp_last_sent_len = 0; 8052 tcp->tcp_dupack_cnt = 0; 8053 8054 tcp->tcp_fport = 0; /* Displayed in MIB */ 8055 PRESERVE(tcp->tcp_lport); 8056 8057 PRESERVE(tcp->tcp_acceptor_lockp); 8058 8059 ASSERT(tcp->tcp_ordrelid == 0); 8060 PRESERVE(tcp->tcp_acceptor_id); 8061 DONTCARE(tcp->tcp_ipsec_overhead); 8062 8063 /* 8064 * If tcp_tracing flag is ON (i.e. We have a trace buffer 8065 * in tcp structure and now tracing), Re-initialize all 8066 * members of tcp_traceinfo. 8067 */ 8068 if (tcp->tcp_tracebuf != NULL) { 8069 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 8070 } 8071 8072 PRESERVE(tcp->tcp_family); 8073 if (tcp->tcp_family == AF_INET6) { 8074 tcp->tcp_ipversion = IPV6_VERSION; 8075 tcp->tcp_mss = tcp_mss_def_ipv6; 8076 } else { 8077 tcp->tcp_ipversion = IPV4_VERSION; 8078 tcp->tcp_mss = tcp_mss_def_ipv4; 8079 } 8080 8081 tcp->tcp_bound_if = 0; 8082 tcp->tcp_ipv6_recvancillary = 0; 8083 tcp->tcp_recvifindex = 0; 8084 tcp->tcp_recvhops = 0; 8085 tcp->tcp_closed = 0; 8086 tcp->tcp_cleandeathtag = 0; 8087 if (tcp->tcp_hopopts != NULL) { 8088 mi_free(tcp->tcp_hopopts); 8089 tcp->tcp_hopopts = NULL; 8090 tcp->tcp_hopoptslen = 0; 8091 } 8092 ASSERT(tcp->tcp_hopoptslen == 0); 8093 if (tcp->tcp_dstopts != NULL) { 8094 mi_free(tcp->tcp_dstopts); 8095 tcp->tcp_dstopts = NULL; 8096 tcp->tcp_dstoptslen = 0; 8097 } 8098 ASSERT(tcp->tcp_dstoptslen == 0); 8099 if (tcp->tcp_rtdstopts != NULL) { 8100 mi_free(tcp->tcp_rtdstopts); 8101 tcp->tcp_rtdstopts = NULL; 8102 tcp->tcp_rtdstoptslen = 0; 8103 } 8104 ASSERT(tcp->tcp_rtdstoptslen == 0); 8105 if (tcp->tcp_rthdr != NULL) { 8106 mi_free(tcp->tcp_rthdr); 8107 tcp->tcp_rthdr = NULL; 8108 tcp->tcp_rthdrlen = 0; 8109 } 8110 ASSERT(tcp->tcp_rthdrlen == 0); 8111 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8112 8113 /* Reset fusion-related fields */ 8114 tcp->tcp_fused = B_FALSE; 8115 tcp->tcp_unfusable = B_FALSE; 8116 tcp->tcp_fused_sigurg = B_FALSE; 8117 tcp->tcp_direct_sockfs = B_FALSE; 8118 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8119 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8120 tcp->tcp_loopback_peer = NULL; 8121 tcp->tcp_fuse_rcv_hiwater = 0; 8122 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8123 tcp->tcp_fuse_rcv_unread_cnt = 0; 8124 8125 tcp->tcp_in_ack_unsent = 0; 8126 tcp->tcp_cork = B_FALSE; 8127 tcp->tcp_tconnind_started = B_FALSE; 8128 8129 PRESERVE(tcp->tcp_squeue_bytes); 8130 8131 ASSERT(tcp->tcp_kssl_ctx == NULL); 8132 ASSERT(!tcp->tcp_kssl_pending); 8133 PRESERVE(tcp->tcp_kssl_ent); 8134 8135 tcp->tcp_closemp_used = 0; 8136 8137 #ifdef DEBUG 8138 DONTCARE(tcp->tcmp_stk[0]); 8139 #endif 8140 8141 8142 #undef DONTCARE 8143 #undef PRESERVE 8144 } 8145 8146 /* 8147 * Allocate necessary resources and initialize state vector. 8148 * Guaranteed not to fail so that when an error is returned, 8149 * the caller doesn't need to do any additional cleanup. 8150 */ 8151 int 8152 tcp_init(tcp_t *tcp, queue_t *q) 8153 { 8154 int err; 8155 8156 tcp->tcp_rq = q; 8157 tcp->tcp_wq = WR(q); 8158 tcp->tcp_state = TCPS_IDLE; 8159 if ((err = tcp_init_values(tcp)) != 0) 8160 tcp_timers_stop(tcp); 8161 return (err); 8162 } 8163 8164 static int 8165 tcp_init_values(tcp_t *tcp) 8166 { 8167 int err; 8168 8169 ASSERT((tcp->tcp_family == AF_INET && 8170 tcp->tcp_ipversion == IPV4_VERSION) || 8171 (tcp->tcp_family == AF_INET6 && 8172 (tcp->tcp_ipversion == IPV4_VERSION || 8173 tcp->tcp_ipversion == IPV6_VERSION))); 8174 8175 /* 8176 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8177 * will be close to tcp_rexmit_interval_initial. By doing this, we 8178 * allow the algorithm to adjust slowly to large fluctuations of RTT 8179 * during first few transmissions of a connection as seen in slow 8180 * links. 8181 */ 8182 tcp->tcp_rtt_sa = tcp_rexmit_interval_initial << 2; 8183 tcp->tcp_rtt_sd = tcp_rexmit_interval_initial >> 1; 8184 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8185 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8186 tcp_conn_grace_period; 8187 if (tcp->tcp_rto < tcp_rexmit_interval_min) 8188 tcp->tcp_rto = tcp_rexmit_interval_min; 8189 tcp->tcp_timer_backoff = 0; 8190 tcp->tcp_ms_we_have_waited = 0; 8191 tcp->tcp_last_recv_time = lbolt; 8192 tcp->tcp_cwnd_max = tcp_cwnd_max_; 8193 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8194 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8195 8196 tcp->tcp_maxpsz = tcp_maxpsz_multiplier; 8197 8198 tcp->tcp_first_timer_threshold = tcp_ip_notify_interval; 8199 tcp->tcp_first_ctimer_threshold = tcp_ip_notify_cinterval; 8200 tcp->tcp_second_timer_threshold = tcp_ip_abort_interval; 8201 /* 8202 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8203 * passive open. 8204 */ 8205 tcp->tcp_second_ctimer_threshold = tcp_ip_abort_cinterval; 8206 8207 tcp->tcp_naglim = tcp_naglim_def; 8208 8209 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8210 8211 tcp->tcp_mdt_hdr_head = 0; 8212 tcp->tcp_mdt_hdr_tail = 0; 8213 8214 /* Reset fusion-related fields */ 8215 tcp->tcp_fused = B_FALSE; 8216 tcp->tcp_unfusable = B_FALSE; 8217 tcp->tcp_fused_sigurg = B_FALSE; 8218 tcp->tcp_direct_sockfs = B_FALSE; 8219 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8220 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8221 tcp->tcp_loopback_peer = NULL; 8222 tcp->tcp_fuse_rcv_hiwater = 0; 8223 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8224 tcp->tcp_fuse_rcv_unread_cnt = 0; 8225 8226 /* Initialize the header template */ 8227 if (tcp->tcp_ipversion == IPV4_VERSION) { 8228 err = tcp_header_init_ipv4(tcp); 8229 } else { 8230 err = tcp_header_init_ipv6(tcp); 8231 } 8232 if (err) 8233 return (err); 8234 8235 /* 8236 * Init the window scale to the max so tcp_rwnd_set() won't pare 8237 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8238 */ 8239 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8240 tcp->tcp_xmit_lowater = tcp_xmit_lowat; 8241 tcp->tcp_xmit_hiwater = tcp_xmit_hiwat; 8242 8243 tcp->tcp_cork = B_FALSE; 8244 /* 8245 * Init the tcp_debug option. This value determines whether TCP 8246 * calls strlog() to print out debug messages. Doing this 8247 * initialization here means that this value is not inherited thru 8248 * tcp_reinit(). 8249 */ 8250 tcp->tcp_debug = tcp_dbg; 8251 8252 tcp->tcp_ka_interval = tcp_keepalive_interval; 8253 tcp->tcp_ka_abort_thres = tcp_keepalive_abort_interval; 8254 8255 return (0); 8256 } 8257 8258 /* 8259 * Initialize the IPv4 header. Loses any record of any IP options. 8260 */ 8261 static int 8262 tcp_header_init_ipv4(tcp_t *tcp) 8263 { 8264 tcph_t *tcph; 8265 uint32_t sum; 8266 conn_t *connp; 8267 8268 /* 8269 * This is a simple initialization. If there's 8270 * already a template, it should never be too small, 8271 * so reuse it. Otherwise, allocate space for the new one. 8272 */ 8273 if (tcp->tcp_iphc == NULL) { 8274 ASSERT(tcp->tcp_iphc_len == 0); 8275 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8276 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8277 if (tcp->tcp_iphc == NULL) { 8278 tcp->tcp_iphc_len = 0; 8279 return (ENOMEM); 8280 } 8281 } 8282 8283 /* options are gone; may need a new label */ 8284 connp = tcp->tcp_connp; 8285 connp->conn_mlp_type = mlptSingle; 8286 connp->conn_ulp_labeled = !is_system_labeled(); 8287 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8288 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8289 tcp->tcp_ip6h = NULL; 8290 tcp->tcp_ipversion = IPV4_VERSION; 8291 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8292 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8293 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8294 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8295 tcp->tcp_ipha->ipha_version_and_hdr_length 8296 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8297 tcp->tcp_ipha->ipha_ident = 0; 8298 8299 tcp->tcp_ttl = (uchar_t)tcp_ipv4_ttl; 8300 tcp->tcp_tos = 0; 8301 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8302 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcp_ipv4_ttl; 8303 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8304 8305 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8306 tcp->tcp_tcph = tcph; 8307 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8308 /* 8309 * IP wants our header length in the checksum field to 8310 * allow it to perform a single pseudo-header+checksum 8311 * calculation on behalf of TCP. 8312 * Include the adjustment for a source route once IP_OPTIONS is set. 8313 */ 8314 sum = sizeof (tcph_t) + tcp->tcp_sum; 8315 sum = (sum >> 16) + (sum & 0xFFFF); 8316 U16_TO_ABE16(sum, tcph->th_sum); 8317 return (0); 8318 } 8319 8320 /* 8321 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8322 */ 8323 static int 8324 tcp_header_init_ipv6(tcp_t *tcp) 8325 { 8326 tcph_t *tcph; 8327 uint32_t sum; 8328 conn_t *connp; 8329 8330 /* 8331 * This is a simple initialization. If there's 8332 * already a template, it should never be too small, 8333 * so reuse it. Otherwise, allocate space for the new one. 8334 * Ensure that there is enough space to "downgrade" the tcp_t 8335 * to an IPv4 tcp_t. This requires having space for a full load 8336 * of IPv4 options, as well as a full load of TCP options 8337 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8338 * than a v6 header and a TCP header with a full load of TCP options 8339 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8340 * We want to avoid reallocation in the "downgraded" case when 8341 * processing outbound IPv4 options. 8342 */ 8343 if (tcp->tcp_iphc == NULL) { 8344 ASSERT(tcp->tcp_iphc_len == 0); 8345 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8346 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8347 if (tcp->tcp_iphc == NULL) { 8348 tcp->tcp_iphc_len = 0; 8349 return (ENOMEM); 8350 } 8351 } 8352 8353 /* options are gone; may need a new label */ 8354 connp = tcp->tcp_connp; 8355 connp->conn_mlp_type = mlptSingle; 8356 connp->conn_ulp_labeled = !is_system_labeled(); 8357 8358 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8359 tcp->tcp_ipversion = IPV6_VERSION; 8360 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8361 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8362 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8363 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8364 tcp->tcp_ipha = NULL; 8365 8366 /* Initialize the header template */ 8367 8368 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8369 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8370 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8371 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcp_ipv6_hoplimit; 8372 8373 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8374 tcp->tcp_tcph = tcph; 8375 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8376 /* 8377 * IP wants our header length in the checksum field to 8378 * allow it to perform a single psuedo-header+checksum 8379 * calculation on behalf of TCP. 8380 * Include the adjustment for a source route when IPV6_RTHDR is set. 8381 */ 8382 sum = sizeof (tcph_t) + tcp->tcp_sum; 8383 sum = (sum >> 16) + (sum & 0xFFFF); 8384 U16_TO_ABE16(sum, tcph->th_sum); 8385 return (0); 8386 } 8387 8388 /* At minimum we need 4 bytes in the TCP header for the lookup */ 8389 #define ICMP_MIN_TCP_HDR 12 8390 8391 /* 8392 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8393 * passed up by IP. The message is always received on the correct tcp_t. 8394 * Assumes that IP has pulled up everything up to and including the ICMP header. 8395 */ 8396 void 8397 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8398 { 8399 icmph_t *icmph; 8400 ipha_t *ipha; 8401 int iph_hdr_length; 8402 tcph_t *tcph; 8403 boolean_t ipsec_mctl = B_FALSE; 8404 boolean_t secure; 8405 mblk_t *first_mp = mp; 8406 uint32_t new_mss; 8407 uint32_t ratio; 8408 size_t mp_size = MBLKL(mp); 8409 uint32_t seg_ack; 8410 uint32_t seg_seq; 8411 8412 /* Assume IP provides aligned packets - otherwise toss */ 8413 if (!OK_32PTR(mp->b_rptr)) { 8414 freemsg(mp); 8415 return; 8416 } 8417 8418 /* 8419 * Since ICMP errors are normal data marked with M_CTL when sent 8420 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8421 * packets starting with an ipsec_info_t, see ipsec_info.h. 8422 */ 8423 if ((mp_size == sizeof (ipsec_info_t)) && 8424 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8425 ASSERT(mp->b_cont != NULL); 8426 mp = mp->b_cont; 8427 /* IP should have done this */ 8428 ASSERT(OK_32PTR(mp->b_rptr)); 8429 mp_size = MBLKL(mp); 8430 ipsec_mctl = B_TRUE; 8431 } 8432 8433 /* 8434 * Verify that we have a complete outer IP header. If not, drop it. 8435 */ 8436 if (mp_size < sizeof (ipha_t)) { 8437 noticmpv4: 8438 freemsg(first_mp); 8439 return; 8440 } 8441 8442 ipha = (ipha_t *)mp->b_rptr; 8443 /* 8444 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8445 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8446 */ 8447 switch (IPH_HDR_VERSION(ipha)) { 8448 case IPV6_VERSION: 8449 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8450 return; 8451 case IPV4_VERSION: 8452 break; 8453 default: 8454 goto noticmpv4; 8455 } 8456 8457 /* Skip past the outer IP and ICMP headers */ 8458 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8459 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8460 /* 8461 * If we don't have the correct outer IP header length or if the ULP 8462 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8463 * send it upstream. 8464 */ 8465 if (iph_hdr_length < sizeof (ipha_t) || 8466 ipha->ipha_protocol != IPPROTO_ICMP || 8467 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8468 goto noticmpv4; 8469 } 8470 ipha = (ipha_t *)&icmph[1]; 8471 8472 /* Skip past the inner IP and find the ULP header */ 8473 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8474 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8475 /* 8476 * If we don't have the correct inner IP header length or if the ULP 8477 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8478 * bytes of TCP header, drop it. 8479 */ 8480 if (iph_hdr_length < sizeof (ipha_t) || 8481 ipha->ipha_protocol != IPPROTO_TCP || 8482 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8483 goto noticmpv4; 8484 } 8485 8486 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8487 if (ipsec_mctl) { 8488 secure = ipsec_in_is_secure(first_mp); 8489 } else { 8490 secure = B_FALSE; 8491 } 8492 if (secure) { 8493 /* 8494 * If we are willing to accept this in clear 8495 * we don't have to verify policy. 8496 */ 8497 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8498 if (!tcp_check_policy(tcp, first_mp, 8499 ipha, NULL, secure, ipsec_mctl)) { 8500 /* 8501 * tcp_check_policy called 8502 * ip_drop_packet() on failure. 8503 */ 8504 return; 8505 } 8506 } 8507 } 8508 } else if (ipsec_mctl) { 8509 /* 8510 * This is a hard_bound connection. IP has already 8511 * verified policy. We don't have to do it again. 8512 */ 8513 freeb(first_mp); 8514 first_mp = mp; 8515 ipsec_mctl = B_FALSE; 8516 } 8517 8518 seg_ack = ABE32_TO_U32(tcph->th_ack); 8519 seg_seq = ABE32_TO_U32(tcph->th_seq); 8520 /* 8521 * TCP SHOULD check that the TCP sequence number contained in 8522 * payload of the ICMP error message is within the range 8523 * SND.UNA <= SEG.SEQ < SND.NXT. and also SEG.ACK <= RECV.NXT 8524 */ 8525 if (SEQ_LT(seg_seq, tcp->tcp_suna) || 8526 SEQ_GEQ(seg_seq, tcp->tcp_snxt) || 8527 SEQ_GT(seg_ack, tcp->tcp_rnxt)) { 8528 /* 8529 * If the ICMP message is bogus, should we kill the 8530 * connection, or should we just drop the bogus ICMP 8531 * message? It would probably make more sense to just 8532 * drop the message so that if this one managed to get 8533 * in, the real connection should not suffer. 8534 */ 8535 goto noticmpv4; 8536 } 8537 8538 switch (icmph->icmph_type) { 8539 case ICMP_DEST_UNREACHABLE: 8540 switch (icmph->icmph_code) { 8541 case ICMP_FRAGMENTATION_NEEDED: 8542 /* 8543 * Reduce the MSS based on the new MTU. This will 8544 * eliminate any fragmentation locally. 8545 * N.B. There may well be some funny side-effects on 8546 * the local send policy and the remote receive policy. 8547 * Pending further research, we provide 8548 * tcp_ignore_path_mtu just in case this proves 8549 * disastrous somewhere. 8550 * 8551 * After updating the MSS, retransmit part of the 8552 * dropped segment using the new mss by calling 8553 * tcp_wput_data(). Need to adjust all those 8554 * params to make sure tcp_wput_data() work properly. 8555 */ 8556 if (tcp_ignore_path_mtu) 8557 break; 8558 8559 /* 8560 * Decrease the MSS by time stamp options 8561 * IP options and IPSEC options. tcp_hdr_len 8562 * includes time stamp option and IP option 8563 * length. 8564 */ 8565 8566 new_mss = ntohs(icmph->icmph_du_mtu) - 8567 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8568 8569 /* 8570 * Only update the MSS if the new one is 8571 * smaller than the previous one. This is 8572 * to avoid problems when getting multiple 8573 * ICMP errors for the same MTU. 8574 */ 8575 if (new_mss >= tcp->tcp_mss) 8576 break; 8577 8578 /* 8579 * Stop doing PMTU if new_mss is less than 68 8580 * or less than tcp_mss_min. 8581 * The value 68 comes from rfc 1191. 8582 */ 8583 if (new_mss < MAX(68, tcp_mss_min)) 8584 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8585 0; 8586 8587 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8588 ASSERT(ratio >= 1); 8589 tcp_mss_set(tcp, new_mss); 8590 8591 /* 8592 * Make sure we have something to 8593 * send. 8594 */ 8595 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8596 (tcp->tcp_xmit_head != NULL)) { 8597 /* 8598 * Shrink tcp_cwnd in 8599 * proportion to the old MSS/new MSS. 8600 */ 8601 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8602 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8603 (tcp->tcp_unsent == 0)) { 8604 tcp->tcp_rexmit_max = tcp->tcp_fss; 8605 } else { 8606 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8607 } 8608 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8609 tcp->tcp_rexmit = B_TRUE; 8610 tcp->tcp_dupack_cnt = 0; 8611 tcp->tcp_snd_burst = TCP_CWND_SS; 8612 tcp_ss_rexmit(tcp); 8613 } 8614 break; 8615 case ICMP_PORT_UNREACHABLE: 8616 case ICMP_PROTOCOL_UNREACHABLE: 8617 switch (tcp->tcp_state) { 8618 case TCPS_SYN_SENT: 8619 case TCPS_SYN_RCVD: 8620 /* 8621 * ICMP can snipe away incipient 8622 * TCP connections as long as 8623 * seq number is same as initial 8624 * send seq number. 8625 */ 8626 if (seg_seq == tcp->tcp_iss) { 8627 (void) tcp_clean_death(tcp, 8628 ECONNREFUSED, 6); 8629 } 8630 break; 8631 } 8632 break; 8633 case ICMP_HOST_UNREACHABLE: 8634 case ICMP_NET_UNREACHABLE: 8635 /* Record the error in case we finally time out. */ 8636 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8637 tcp->tcp_client_errno = EHOSTUNREACH; 8638 else 8639 tcp->tcp_client_errno = ENETUNREACH; 8640 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8641 if (tcp->tcp_listener != NULL && 8642 tcp->tcp_listener->tcp_syn_defense) { 8643 /* 8644 * Ditch the half-open connection if we 8645 * suspect a SYN attack is under way. 8646 */ 8647 tcp_ip_ire_mark_advice(tcp); 8648 (void) tcp_clean_death(tcp, 8649 tcp->tcp_client_errno, 7); 8650 } 8651 } 8652 break; 8653 default: 8654 break; 8655 } 8656 break; 8657 case ICMP_SOURCE_QUENCH: { 8658 /* 8659 * use a global boolean to control 8660 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8661 * The default is false. 8662 */ 8663 if (tcp_icmp_source_quench) { 8664 /* 8665 * Reduce the sending rate as if we got a 8666 * retransmit timeout 8667 */ 8668 uint32_t npkt; 8669 8670 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8671 tcp->tcp_mss; 8672 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8673 tcp->tcp_cwnd = tcp->tcp_mss; 8674 tcp->tcp_cwnd_cnt = 0; 8675 } 8676 break; 8677 } 8678 } 8679 freemsg(first_mp); 8680 } 8681 8682 /* 8683 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8684 * error messages passed up by IP. 8685 * Assumes that IP has pulled up all the extension headers as well 8686 * as the ICMPv6 header. 8687 */ 8688 static void 8689 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8690 { 8691 icmp6_t *icmp6; 8692 ip6_t *ip6h; 8693 uint16_t iph_hdr_length; 8694 tcpha_t *tcpha; 8695 uint8_t *nexthdrp; 8696 uint32_t new_mss; 8697 uint32_t ratio; 8698 boolean_t secure; 8699 mblk_t *first_mp = mp; 8700 size_t mp_size; 8701 uint32_t seg_ack; 8702 uint32_t seg_seq; 8703 8704 /* 8705 * The caller has determined if this is an IPSEC_IN packet and 8706 * set ipsec_mctl appropriately (see tcp_icmp_error). 8707 */ 8708 if (ipsec_mctl) 8709 mp = mp->b_cont; 8710 8711 mp_size = MBLKL(mp); 8712 8713 /* 8714 * Verify that we have a complete IP header. If not, send it upstream. 8715 */ 8716 if (mp_size < sizeof (ip6_t)) { 8717 noticmpv6: 8718 freemsg(first_mp); 8719 return; 8720 } 8721 8722 /* 8723 * Verify this is an ICMPV6 packet, else send it upstream. 8724 */ 8725 ip6h = (ip6_t *)mp->b_rptr; 8726 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8727 iph_hdr_length = IPV6_HDR_LEN; 8728 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8729 &nexthdrp) || 8730 *nexthdrp != IPPROTO_ICMPV6) { 8731 goto noticmpv6; 8732 } 8733 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8734 ip6h = (ip6_t *)&icmp6[1]; 8735 /* 8736 * Verify if we have a complete ICMP and inner IP header. 8737 */ 8738 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8739 goto noticmpv6; 8740 8741 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8742 goto noticmpv6; 8743 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8744 /* 8745 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8746 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8747 * packet. 8748 */ 8749 if ((*nexthdrp != IPPROTO_TCP) || 8750 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8751 goto noticmpv6; 8752 } 8753 8754 /* 8755 * ICMP errors come on the right queue or come on 8756 * listener/global queue for detached connections and 8757 * get switched to the right queue. If it comes on the 8758 * right queue, policy check has already been done by IP 8759 * and thus free the first_mp without verifying the policy. 8760 * If it has come for a non-hard bound connection, we need 8761 * to verify policy as IP may not have done it. 8762 */ 8763 if (!tcp->tcp_hard_bound) { 8764 if (ipsec_mctl) { 8765 secure = ipsec_in_is_secure(first_mp); 8766 } else { 8767 secure = B_FALSE; 8768 } 8769 if (secure) { 8770 /* 8771 * If we are willing to accept this in clear 8772 * we don't have to verify policy. 8773 */ 8774 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8775 if (!tcp_check_policy(tcp, first_mp, 8776 NULL, ip6h, secure, ipsec_mctl)) { 8777 /* 8778 * tcp_check_policy called 8779 * ip_drop_packet() on failure. 8780 */ 8781 return; 8782 } 8783 } 8784 } 8785 } else if (ipsec_mctl) { 8786 /* 8787 * This is a hard_bound connection. IP has already 8788 * verified policy. We don't have to do it again. 8789 */ 8790 freeb(first_mp); 8791 first_mp = mp; 8792 ipsec_mctl = B_FALSE; 8793 } 8794 8795 seg_ack = ntohl(tcpha->tha_ack); 8796 seg_seq = ntohl(tcpha->tha_seq); 8797 /* 8798 * TCP SHOULD check that the TCP sequence number contained in 8799 * payload of the ICMP error message is within the range 8800 * SND.UNA <= SEG.SEQ < SND.NXT. and also SEG.ACK <= RECV.NXT 8801 */ 8802 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt) || 8803 SEQ_GT(seg_ack, tcp->tcp_rnxt)) { 8804 /* 8805 * If the ICMP message is bogus, should we kill the 8806 * connection, or should we just drop the bogus ICMP 8807 * message? It would probably make more sense to just 8808 * drop the message so that if this one managed to get 8809 * in, the real connection should not suffer. 8810 */ 8811 goto noticmpv6; 8812 } 8813 8814 switch (icmp6->icmp6_type) { 8815 case ICMP6_PACKET_TOO_BIG: 8816 /* 8817 * Reduce the MSS based on the new MTU. This will 8818 * eliminate any fragmentation locally. 8819 * N.B. There may well be some funny side-effects on 8820 * the local send policy and the remote receive policy. 8821 * Pending further research, we provide 8822 * tcp_ignore_path_mtu just in case this proves 8823 * disastrous somewhere. 8824 * 8825 * After updating the MSS, retransmit part of the 8826 * dropped segment using the new mss by calling 8827 * tcp_wput_data(). Need to adjust all those 8828 * params to make sure tcp_wput_data() work properly. 8829 */ 8830 if (tcp_ignore_path_mtu) 8831 break; 8832 8833 /* 8834 * Decrease the MSS by time stamp options 8835 * IP options and IPSEC options. tcp_hdr_len 8836 * includes time stamp option and IP option 8837 * length. 8838 */ 8839 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8840 tcp->tcp_ipsec_overhead; 8841 8842 /* 8843 * Only update the MSS if the new one is 8844 * smaller than the previous one. This is 8845 * to avoid problems when getting multiple 8846 * ICMP errors for the same MTU. 8847 */ 8848 if (new_mss >= tcp->tcp_mss) 8849 break; 8850 8851 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8852 ASSERT(ratio >= 1); 8853 tcp_mss_set(tcp, new_mss); 8854 8855 /* 8856 * Make sure we have something to 8857 * send. 8858 */ 8859 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8860 (tcp->tcp_xmit_head != NULL)) { 8861 /* 8862 * Shrink tcp_cwnd in 8863 * proportion to the old MSS/new MSS. 8864 */ 8865 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8866 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8867 (tcp->tcp_unsent == 0)) { 8868 tcp->tcp_rexmit_max = tcp->tcp_fss; 8869 } else { 8870 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8871 } 8872 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8873 tcp->tcp_rexmit = B_TRUE; 8874 tcp->tcp_dupack_cnt = 0; 8875 tcp->tcp_snd_burst = TCP_CWND_SS; 8876 tcp_ss_rexmit(tcp); 8877 } 8878 break; 8879 8880 case ICMP6_DST_UNREACH: 8881 switch (icmp6->icmp6_code) { 8882 case ICMP6_DST_UNREACH_NOPORT: 8883 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8884 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8885 (seg_seq == tcp->tcp_iss)) { 8886 (void) tcp_clean_death(tcp, 8887 ECONNREFUSED, 8); 8888 } 8889 break; 8890 8891 case ICMP6_DST_UNREACH_ADMIN: 8892 case ICMP6_DST_UNREACH_NOROUTE: 8893 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8894 case ICMP6_DST_UNREACH_ADDR: 8895 /* Record the error in case we finally time out. */ 8896 tcp->tcp_client_errno = EHOSTUNREACH; 8897 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8898 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8899 (seg_seq == tcp->tcp_iss)) { 8900 if (tcp->tcp_listener != NULL && 8901 tcp->tcp_listener->tcp_syn_defense) { 8902 /* 8903 * Ditch the half-open connection if we 8904 * suspect a SYN attack is under way. 8905 */ 8906 tcp_ip_ire_mark_advice(tcp); 8907 (void) tcp_clean_death(tcp, 8908 tcp->tcp_client_errno, 9); 8909 } 8910 } 8911 8912 8913 break; 8914 default: 8915 break; 8916 } 8917 break; 8918 8919 case ICMP6_PARAM_PROB: 8920 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8921 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8922 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8923 (uchar_t *)nexthdrp) { 8924 if (tcp->tcp_state == TCPS_SYN_SENT || 8925 tcp->tcp_state == TCPS_SYN_RCVD) { 8926 (void) tcp_clean_death(tcp, 8927 ECONNREFUSED, 10); 8928 } 8929 break; 8930 } 8931 break; 8932 8933 case ICMP6_TIME_EXCEEDED: 8934 default: 8935 break; 8936 } 8937 freemsg(first_mp); 8938 } 8939 8940 /* 8941 * IP recognizes seven kinds of bind requests: 8942 * 8943 * - A zero-length address binds only to the protocol number. 8944 * 8945 * - A 4-byte address is treated as a request to 8946 * validate that the address is a valid local IPv4 8947 * address, appropriate for an application to bind to. 8948 * IP does the verification, but does not make any note 8949 * of the address at this time. 8950 * 8951 * - A 16-byte address contains is treated as a request 8952 * to validate a local IPv6 address, as the 4-byte 8953 * address case above. 8954 * 8955 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 8956 * use it for the inbound fanout of packets. 8957 * 8958 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 8959 * use it for the inbound fanout of packets. 8960 * 8961 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 8962 * information consisting of local and remote addresses 8963 * and ports. In this case, the addresses are both 8964 * validated as appropriate for this operation, and, if 8965 * so, the information is retained for use in the 8966 * inbound fanout. 8967 * 8968 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 8969 * fanout information, like the 12-byte case above. 8970 * 8971 * IP will also fill in the IRE request mblk with information 8972 * regarding our peer. In all cases, we notify IP of our protocol 8973 * type by appending a single protocol byte to the bind request. 8974 */ 8975 static mblk_t * 8976 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 8977 { 8978 char *cp; 8979 mblk_t *mp; 8980 struct T_bind_req *tbr; 8981 ipa_conn_t *ac; 8982 ipa6_conn_t *ac6; 8983 sin_t *sin; 8984 sin6_t *sin6; 8985 8986 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 8987 ASSERT((tcp->tcp_family == AF_INET && 8988 tcp->tcp_ipversion == IPV4_VERSION) || 8989 (tcp->tcp_family == AF_INET6 && 8990 (tcp->tcp_ipversion == IPV4_VERSION || 8991 tcp->tcp_ipversion == IPV6_VERSION))); 8992 8993 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 8994 if (!mp) 8995 return (mp); 8996 mp->b_datap->db_type = M_PROTO; 8997 tbr = (struct T_bind_req *)mp->b_rptr; 8998 tbr->PRIM_type = bind_prim; 8999 tbr->ADDR_offset = sizeof (*tbr); 9000 tbr->CONIND_number = 0; 9001 tbr->ADDR_length = addr_length; 9002 cp = (char *)&tbr[1]; 9003 switch (addr_length) { 9004 case sizeof (ipa_conn_t): 9005 ASSERT(tcp->tcp_family == AF_INET); 9006 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9007 9008 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9009 if (mp->b_cont == NULL) { 9010 freemsg(mp); 9011 return (NULL); 9012 } 9013 mp->b_cont->b_wptr += sizeof (ire_t); 9014 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9015 9016 /* cp known to be 32 bit aligned */ 9017 ac = (ipa_conn_t *)cp; 9018 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9019 ac->ac_faddr = tcp->tcp_remote; 9020 ac->ac_fport = tcp->tcp_fport; 9021 ac->ac_lport = tcp->tcp_lport; 9022 tcp->tcp_hard_binding = 1; 9023 break; 9024 9025 case sizeof (ipa6_conn_t): 9026 ASSERT(tcp->tcp_family == AF_INET6); 9027 9028 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9029 if (mp->b_cont == NULL) { 9030 freemsg(mp); 9031 return (NULL); 9032 } 9033 mp->b_cont->b_wptr += sizeof (ire_t); 9034 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9035 9036 /* cp known to be 32 bit aligned */ 9037 ac6 = (ipa6_conn_t *)cp; 9038 if (tcp->tcp_ipversion == IPV4_VERSION) { 9039 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9040 &ac6->ac6_laddr); 9041 } else { 9042 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9043 } 9044 ac6->ac6_faddr = tcp->tcp_remote_v6; 9045 ac6->ac6_fport = tcp->tcp_fport; 9046 ac6->ac6_lport = tcp->tcp_lport; 9047 tcp->tcp_hard_binding = 1; 9048 break; 9049 9050 case sizeof (sin_t): 9051 /* 9052 * NOTE: IPV6_ADDR_LEN also has same size. 9053 * Use family to discriminate. 9054 */ 9055 if (tcp->tcp_family == AF_INET) { 9056 sin = (sin_t *)cp; 9057 9058 *sin = sin_null; 9059 sin->sin_family = AF_INET; 9060 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9061 sin->sin_port = tcp->tcp_lport; 9062 break; 9063 } else { 9064 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9065 } 9066 break; 9067 9068 case sizeof (sin6_t): 9069 ASSERT(tcp->tcp_family == AF_INET6); 9070 sin6 = (sin6_t *)cp; 9071 9072 *sin6 = sin6_null; 9073 sin6->sin6_family = AF_INET6; 9074 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9075 sin6->sin6_port = tcp->tcp_lport; 9076 break; 9077 9078 case IP_ADDR_LEN: 9079 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9080 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9081 break; 9082 9083 } 9084 /* Add protocol number to end */ 9085 cp[addr_length] = (char)IPPROTO_TCP; 9086 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9087 return (mp); 9088 } 9089 9090 /* 9091 * Notify IP that we are having trouble with this connection. IP should 9092 * blow the IRE away and start over. 9093 */ 9094 static void 9095 tcp_ip_notify(tcp_t *tcp) 9096 { 9097 struct iocblk *iocp; 9098 ipid_t *ipid; 9099 mblk_t *mp; 9100 9101 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9102 if (tcp->tcp_ipversion == IPV6_VERSION) 9103 return; 9104 9105 mp = mkiocb(IP_IOCTL); 9106 if (mp == NULL) 9107 return; 9108 9109 iocp = (struct iocblk *)mp->b_rptr; 9110 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9111 9112 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9113 if (!mp->b_cont) { 9114 freeb(mp); 9115 return; 9116 } 9117 9118 ipid = (ipid_t *)mp->b_cont->b_rptr; 9119 mp->b_cont->b_wptr += iocp->ioc_count; 9120 bzero(ipid, sizeof (*ipid)); 9121 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9122 ipid->ipid_ire_type = IRE_CACHE; 9123 ipid->ipid_addr_offset = sizeof (ipid_t); 9124 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9125 /* 9126 * Note: in the case of source routing we want to blow away the 9127 * route to the first source route hop. 9128 */ 9129 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9130 sizeof (tcp->tcp_ipha->ipha_dst)); 9131 9132 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9133 } 9134 9135 /* Unlink and return any mblk that looks like it contains an ire */ 9136 static mblk_t * 9137 tcp_ire_mp(mblk_t *mp) 9138 { 9139 mblk_t *prev_mp; 9140 9141 for (;;) { 9142 prev_mp = mp; 9143 mp = mp->b_cont; 9144 if (mp == NULL) 9145 break; 9146 switch (DB_TYPE(mp)) { 9147 case IRE_DB_TYPE: 9148 case IRE_DB_REQ_TYPE: 9149 if (prev_mp != NULL) 9150 prev_mp->b_cont = mp->b_cont; 9151 mp->b_cont = NULL; 9152 return (mp); 9153 default: 9154 break; 9155 } 9156 } 9157 return (mp); 9158 } 9159 9160 /* 9161 * Timer callback routine for keepalive probe. We do a fake resend of 9162 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9163 * check to see if we have heard anything from the other end for the last 9164 * RTO period. If we have, set the timer to expire for another 9165 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9166 * RTO << 1 and check again when it expires. Keep exponentially increasing 9167 * the timeout if we have not heard from the other side. If for more than 9168 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9169 * kill the connection unless the keepalive abort threshold is 0. In 9170 * that case, we will probe "forever." 9171 */ 9172 static void 9173 tcp_keepalive_killer(void *arg) 9174 { 9175 mblk_t *mp; 9176 conn_t *connp = (conn_t *)arg; 9177 tcp_t *tcp = connp->conn_tcp; 9178 int32_t firetime; 9179 int32_t idletime; 9180 int32_t ka_intrvl; 9181 9182 tcp->tcp_ka_tid = 0; 9183 9184 if (tcp->tcp_fused) 9185 return; 9186 9187 BUMP_MIB(&tcp_mib, tcpTimKeepalive); 9188 ka_intrvl = tcp->tcp_ka_interval; 9189 9190 /* 9191 * Keepalive probe should only be sent if the application has not 9192 * done a close on the connection. 9193 */ 9194 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9195 return; 9196 } 9197 /* Timer fired too early, restart it. */ 9198 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9199 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9200 MSEC_TO_TICK(ka_intrvl)); 9201 return; 9202 } 9203 9204 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9205 /* 9206 * If we have not heard from the other side for a long 9207 * time, kill the connection unless the keepalive abort 9208 * threshold is 0. In that case, we will probe "forever." 9209 */ 9210 if (tcp->tcp_ka_abort_thres != 0 && 9211 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9212 BUMP_MIB(&tcp_mib, tcpTimKeepaliveDrop); 9213 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9214 tcp->tcp_client_errno : ETIMEDOUT, 11); 9215 return; 9216 } 9217 9218 if (tcp->tcp_snxt == tcp->tcp_suna && 9219 idletime >= ka_intrvl) { 9220 /* Fake resend of last ACKed byte. */ 9221 mblk_t *mp1 = allocb(1, BPRI_LO); 9222 9223 if (mp1 != NULL) { 9224 *mp1->b_wptr++ = '\0'; 9225 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9226 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9227 freeb(mp1); 9228 /* 9229 * if allocation failed, fall through to start the 9230 * timer back. 9231 */ 9232 if (mp != NULL) { 9233 TCP_RECORD_TRACE(tcp, mp, 9234 TCP_TRACE_SEND_PKT); 9235 tcp_send_data(tcp, tcp->tcp_wq, mp); 9236 BUMP_MIB(&tcp_mib, tcpTimKeepaliveProbe); 9237 if (tcp->tcp_ka_last_intrvl != 0) { 9238 /* 9239 * We should probe again at least 9240 * in ka_intrvl, but not more than 9241 * tcp_rexmit_interval_max. 9242 */ 9243 firetime = MIN(ka_intrvl - 1, 9244 tcp->tcp_ka_last_intrvl << 1); 9245 if (firetime > tcp_rexmit_interval_max) 9246 firetime = 9247 tcp_rexmit_interval_max; 9248 } else { 9249 firetime = tcp->tcp_rto; 9250 } 9251 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9252 tcp_keepalive_killer, 9253 MSEC_TO_TICK(firetime)); 9254 tcp->tcp_ka_last_intrvl = firetime; 9255 return; 9256 } 9257 } 9258 } else { 9259 tcp->tcp_ka_last_intrvl = 0; 9260 } 9261 9262 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9263 if ((firetime = ka_intrvl - idletime) < 0) { 9264 firetime = ka_intrvl; 9265 } 9266 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9267 MSEC_TO_TICK(firetime)); 9268 } 9269 9270 int 9271 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9272 { 9273 queue_t *q = tcp->tcp_rq; 9274 int32_t mss = tcp->tcp_mss; 9275 int maxpsz; 9276 9277 if (TCP_IS_DETACHED(tcp)) 9278 return (mss); 9279 9280 if (tcp->tcp_fused) { 9281 maxpsz = tcp_fuse_maxpsz_set(tcp); 9282 mss = INFPSZ; 9283 } else if (tcp->tcp_mdt || tcp->tcp_maxpsz == 0) { 9284 /* 9285 * Set the sd_qn_maxpsz according to the socket send buffer 9286 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9287 * instruct the stream head to copyin user data into contiguous 9288 * kernel-allocated buffers without breaking it up into smaller 9289 * chunks. We round up the buffer size to the nearest SMSS. 9290 */ 9291 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9292 if (tcp->tcp_kssl_ctx == NULL) 9293 mss = INFPSZ; 9294 else 9295 mss = SSL3_MAX_RECORD_LEN; 9296 } else { 9297 /* 9298 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9299 * (and a multiple of the mss). This instructs the stream 9300 * head to break down larger than SMSS writes into SMSS- 9301 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9302 */ 9303 maxpsz = tcp->tcp_maxpsz * mss; 9304 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9305 maxpsz = tcp->tcp_xmit_hiwater/2; 9306 /* Round up to nearest mss */ 9307 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9308 } 9309 } 9310 (void) setmaxps(q, maxpsz); 9311 tcp->tcp_wq->q_maxpsz = maxpsz; 9312 9313 if (set_maxblk) 9314 (void) mi_set_sth_maxblk(q, mss); 9315 9316 return (mss); 9317 } 9318 9319 /* 9320 * Extract option values from a tcp header. We put any found values into the 9321 * tcpopt struct and return a bitmask saying which options were found. 9322 */ 9323 static int 9324 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9325 { 9326 uchar_t *endp; 9327 int len; 9328 uint32_t mss; 9329 uchar_t *up = (uchar_t *)tcph; 9330 int found = 0; 9331 int32_t sack_len; 9332 tcp_seq sack_begin, sack_end; 9333 tcp_t *tcp; 9334 9335 endp = up + TCP_HDR_LENGTH(tcph); 9336 up += TCP_MIN_HEADER_LENGTH; 9337 while (up < endp) { 9338 len = endp - up; 9339 switch (*up) { 9340 case TCPOPT_EOL: 9341 break; 9342 9343 case TCPOPT_NOP: 9344 up++; 9345 continue; 9346 9347 case TCPOPT_MAXSEG: 9348 if (len < TCPOPT_MAXSEG_LEN || 9349 up[1] != TCPOPT_MAXSEG_LEN) 9350 break; 9351 9352 mss = BE16_TO_U16(up+2); 9353 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9354 tcpopt->tcp_opt_mss = mss; 9355 found |= TCP_OPT_MSS_PRESENT; 9356 9357 up += TCPOPT_MAXSEG_LEN; 9358 continue; 9359 9360 case TCPOPT_WSCALE: 9361 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9362 break; 9363 9364 if (up[2] > TCP_MAX_WINSHIFT) 9365 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9366 else 9367 tcpopt->tcp_opt_wscale = up[2]; 9368 found |= TCP_OPT_WSCALE_PRESENT; 9369 9370 up += TCPOPT_WS_LEN; 9371 continue; 9372 9373 case TCPOPT_SACK_PERMITTED: 9374 if (len < TCPOPT_SACK_OK_LEN || 9375 up[1] != TCPOPT_SACK_OK_LEN) 9376 break; 9377 found |= TCP_OPT_SACK_OK_PRESENT; 9378 up += TCPOPT_SACK_OK_LEN; 9379 continue; 9380 9381 case TCPOPT_SACK: 9382 if (len <= 2 || up[1] <= 2 || len < up[1]) 9383 break; 9384 9385 /* If TCP is not interested in SACK blks... */ 9386 if ((tcp = tcpopt->tcp) == NULL) { 9387 up += up[1]; 9388 continue; 9389 } 9390 sack_len = up[1] - TCPOPT_HEADER_LEN; 9391 up += TCPOPT_HEADER_LEN; 9392 9393 /* 9394 * If the list is empty, allocate one and assume 9395 * nothing is sack'ed. 9396 */ 9397 ASSERT(tcp->tcp_sack_info != NULL); 9398 if (tcp->tcp_notsack_list == NULL) { 9399 tcp_notsack_update(&(tcp->tcp_notsack_list), 9400 tcp->tcp_suna, tcp->tcp_snxt, 9401 &(tcp->tcp_num_notsack_blk), 9402 &(tcp->tcp_cnt_notsack_list)); 9403 9404 /* 9405 * Make sure tcp_notsack_list is not NULL. 9406 * This happens when kmem_alloc(KM_NOSLEEP) 9407 * returns NULL. 9408 */ 9409 if (tcp->tcp_notsack_list == NULL) { 9410 up += sack_len; 9411 continue; 9412 } 9413 tcp->tcp_fack = tcp->tcp_suna; 9414 } 9415 9416 while (sack_len > 0) { 9417 if (up + 8 > endp) { 9418 up = endp; 9419 break; 9420 } 9421 sack_begin = BE32_TO_U32(up); 9422 up += 4; 9423 sack_end = BE32_TO_U32(up); 9424 up += 4; 9425 sack_len -= 8; 9426 /* 9427 * Bounds checking. Make sure the SACK 9428 * info is within tcp_suna and tcp_snxt. 9429 * If this SACK blk is out of bound, ignore 9430 * it but continue to parse the following 9431 * blks. 9432 */ 9433 if (SEQ_LEQ(sack_end, sack_begin) || 9434 SEQ_LT(sack_begin, tcp->tcp_suna) || 9435 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9436 continue; 9437 } 9438 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9439 sack_begin, sack_end, 9440 &(tcp->tcp_num_notsack_blk), 9441 &(tcp->tcp_cnt_notsack_list)); 9442 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9443 tcp->tcp_fack = sack_end; 9444 } 9445 } 9446 found |= TCP_OPT_SACK_PRESENT; 9447 continue; 9448 9449 case TCPOPT_TSTAMP: 9450 if (len < TCPOPT_TSTAMP_LEN || 9451 up[1] != TCPOPT_TSTAMP_LEN) 9452 break; 9453 9454 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9455 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9456 9457 found |= TCP_OPT_TSTAMP_PRESENT; 9458 9459 up += TCPOPT_TSTAMP_LEN; 9460 continue; 9461 9462 default: 9463 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9464 break; 9465 up += up[1]; 9466 continue; 9467 } 9468 break; 9469 } 9470 return (found); 9471 } 9472 9473 /* 9474 * Set the mss associated with a particular tcp based on its current value, 9475 * and a new one passed in. Observe minimums and maximums, and reset 9476 * other state variables that we want to view as multiples of mss. 9477 * 9478 * This function is called in various places mainly because 9479 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9480 * other side's SYN/SYN-ACK packet arrives. 9481 * 2) PMTUd may get us a new MSS. 9482 * 3) If the other side stops sending us timestamp option, we need to 9483 * increase the MSS size to use the extra bytes available. 9484 */ 9485 static void 9486 tcp_mss_set(tcp_t *tcp, uint32_t mss) 9487 { 9488 uint32_t mss_max; 9489 9490 if (tcp->tcp_ipversion == IPV4_VERSION) 9491 mss_max = tcp_mss_max_ipv4; 9492 else 9493 mss_max = tcp_mss_max_ipv6; 9494 9495 if (mss < tcp_mss_min) 9496 mss = tcp_mss_min; 9497 if (mss > mss_max) 9498 mss = mss_max; 9499 /* 9500 * Unless naglim has been set by our client to 9501 * a non-mss value, force naglim to track mss. 9502 * This can help to aggregate small writes. 9503 */ 9504 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9505 tcp->tcp_naglim = mss; 9506 /* 9507 * TCP should be able to buffer at least 4 MSS data for obvious 9508 * performance reason. 9509 */ 9510 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9511 tcp->tcp_xmit_hiwater = mss << 2; 9512 9513 /* 9514 * Check if we need to apply the tcp_init_cwnd here. If 9515 * it is set and the MSS gets bigger (should not happen 9516 * normally), we need to adjust the resulting tcp_cwnd properly. 9517 * The new tcp_cwnd should not get bigger. 9518 */ 9519 if (tcp->tcp_init_cwnd == 0) { 9520 tcp->tcp_cwnd = MIN(tcp_slow_start_initial * mss, 9521 MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9522 } else { 9523 if (tcp->tcp_mss < mss) { 9524 tcp->tcp_cwnd = MAX(1, 9525 (tcp->tcp_init_cwnd * tcp->tcp_mss / mss)) * mss; 9526 } else { 9527 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9528 } 9529 } 9530 tcp->tcp_mss = mss; 9531 tcp->tcp_cwnd_cnt = 0; 9532 (void) tcp_maxpsz_set(tcp, B_TRUE); 9533 } 9534 9535 static int 9536 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9537 { 9538 tcp_t *tcp = NULL; 9539 conn_t *connp; 9540 int err; 9541 dev_t conn_dev; 9542 zoneid_t zoneid = getzoneid(); 9543 9544 /* 9545 * Special case for install: miniroot needs to be able to access files 9546 * via NFS as though it were always in the global zone. 9547 */ 9548 if (credp == kcred && nfs_global_client_only != 0) 9549 zoneid = GLOBAL_ZONEID; 9550 9551 if (q->q_ptr != NULL) 9552 return (0); 9553 9554 if (sflag == MODOPEN) { 9555 /* 9556 * This is a special case. The purpose of a modopen 9557 * is to allow just the T_SVR4_OPTMGMT_REQ to pass 9558 * through for MIB browsers. Everything else is failed. 9559 */ 9560 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt)); 9561 9562 if (connp == NULL) 9563 return (ENOMEM); 9564 9565 connp->conn_flags |= IPCL_TCPMOD; 9566 connp->conn_cred = credp; 9567 connp->conn_zoneid = zoneid; 9568 q->q_ptr = WR(q)->q_ptr = connp; 9569 crhold(credp); 9570 q->q_qinfo = &tcp_mod_rinit; 9571 WR(q)->q_qinfo = &tcp_mod_winit; 9572 qprocson(q); 9573 return (0); 9574 } 9575 9576 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) 9577 return (EBUSY); 9578 9579 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9580 9581 if (flag & SO_ACCEPTOR) { 9582 q->q_qinfo = &tcp_acceptor_rinit; 9583 q->q_ptr = (void *)conn_dev; 9584 WR(q)->q_qinfo = &tcp_acceptor_winit; 9585 WR(q)->q_ptr = (void *)conn_dev; 9586 qprocson(q); 9587 return (0); 9588 } 9589 9590 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt)); 9591 if (connp == NULL) { 9592 inet_minor_free(ip_minor_arena, conn_dev); 9593 q->q_ptr = NULL; 9594 return (ENOSR); 9595 } 9596 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9597 tcp = connp->conn_tcp; 9598 9599 q->q_ptr = WR(q)->q_ptr = connp; 9600 if (getmajor(*devp) == TCP6_MAJ) { 9601 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9602 connp->conn_send = ip_output_v6; 9603 connp->conn_af_isv6 = B_TRUE; 9604 connp->conn_pkt_isv6 = B_TRUE; 9605 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9606 tcp->tcp_ipversion = IPV6_VERSION; 9607 tcp->tcp_family = AF_INET6; 9608 tcp->tcp_mss = tcp_mss_def_ipv6; 9609 } else { 9610 connp->conn_flags |= IPCL_TCP4; 9611 connp->conn_send = ip_output; 9612 connp->conn_af_isv6 = B_FALSE; 9613 connp->conn_pkt_isv6 = B_FALSE; 9614 tcp->tcp_ipversion = IPV4_VERSION; 9615 tcp->tcp_family = AF_INET; 9616 tcp->tcp_mss = tcp_mss_def_ipv4; 9617 } 9618 9619 /* 9620 * TCP keeps a copy of cred for cache locality reasons but 9621 * we put a reference only once. If connp->conn_cred 9622 * becomes invalid, tcp_cred should also be set to NULL. 9623 */ 9624 tcp->tcp_cred = connp->conn_cred = credp; 9625 crhold(connp->conn_cred); 9626 tcp->tcp_cpid = curproc->p_pid; 9627 connp->conn_zoneid = zoneid; 9628 connp->conn_mlp_type = mlptSingle; 9629 connp->conn_ulp_labeled = !is_system_labeled(); 9630 9631 /* 9632 * If the caller has the process-wide flag set, then default to MAC 9633 * exempt mode. This allows read-down to unlabeled hosts. 9634 */ 9635 if (getpflags(NET_MAC_AWARE, credp) != 0) 9636 connp->conn_mac_exempt = B_TRUE; 9637 9638 connp->conn_dev = conn_dev; 9639 9640 ASSERT(q->q_qinfo == &tcp_rinit); 9641 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9642 9643 if (flag & SO_SOCKSTR) { 9644 /* 9645 * No need to insert a socket in tcp acceptor hash. 9646 * If it was a socket acceptor stream, we dealt with 9647 * it above. A socket listener can never accept a 9648 * connection and doesn't need acceptor_id. 9649 */ 9650 connp->conn_flags |= IPCL_SOCKET; 9651 tcp->tcp_issocket = 1; 9652 WR(q)->q_qinfo = &tcp_sock_winit; 9653 } else { 9654 #ifdef _ILP32 9655 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9656 #else 9657 tcp->tcp_acceptor_id = conn_dev; 9658 #endif /* _ILP32 */ 9659 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9660 } 9661 9662 if (tcp_trace) 9663 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9664 9665 err = tcp_init(tcp, q); 9666 if (err != 0) { 9667 inet_minor_free(ip_minor_arena, connp->conn_dev); 9668 tcp_acceptor_hash_remove(tcp); 9669 CONN_DEC_REF(connp); 9670 q->q_ptr = WR(q)->q_ptr = NULL; 9671 return (err); 9672 } 9673 9674 RD(q)->q_hiwat = tcp_recv_hiwat; 9675 tcp->tcp_rwnd = tcp_recv_hiwat; 9676 9677 /* Non-zero default values */ 9678 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9679 /* 9680 * Put the ref for TCP. Ref for IP was already put 9681 * by ipcl_conn_create. Also Make the conn_t globally 9682 * visible to walkers 9683 */ 9684 mutex_enter(&connp->conn_lock); 9685 CONN_INC_REF_LOCKED(connp); 9686 ASSERT(connp->conn_ref == 2); 9687 connp->conn_state_flags &= ~CONN_INCIPIENT; 9688 mutex_exit(&connp->conn_lock); 9689 9690 qprocson(q); 9691 return (0); 9692 } 9693 9694 /* 9695 * Some TCP options can be "set" by requesting them in the option 9696 * buffer. This is needed for XTI feature test though we do not 9697 * allow it in general. We interpret that this mechanism is more 9698 * applicable to OSI protocols and need not be allowed in general. 9699 * This routine filters out options for which it is not allowed (most) 9700 * and lets through those (few) for which it is. [ The XTI interface 9701 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9702 * ever implemented will have to be allowed here ]. 9703 */ 9704 static boolean_t 9705 tcp_allow_connopt_set(int level, int name) 9706 { 9707 9708 switch (level) { 9709 case IPPROTO_TCP: 9710 switch (name) { 9711 case TCP_NODELAY: 9712 return (B_TRUE); 9713 default: 9714 return (B_FALSE); 9715 } 9716 /*NOTREACHED*/ 9717 default: 9718 return (B_FALSE); 9719 } 9720 /*NOTREACHED*/ 9721 } 9722 9723 /* 9724 * This routine gets default values of certain options whose default 9725 * values are maintained by protocol specific code 9726 */ 9727 /* ARGSUSED */ 9728 int 9729 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9730 { 9731 int32_t *i1 = (int32_t *)ptr; 9732 9733 switch (level) { 9734 case IPPROTO_TCP: 9735 switch (name) { 9736 case TCP_NOTIFY_THRESHOLD: 9737 *i1 = tcp_ip_notify_interval; 9738 break; 9739 case TCP_ABORT_THRESHOLD: 9740 *i1 = tcp_ip_abort_interval; 9741 break; 9742 case TCP_CONN_NOTIFY_THRESHOLD: 9743 *i1 = tcp_ip_notify_cinterval; 9744 break; 9745 case TCP_CONN_ABORT_THRESHOLD: 9746 *i1 = tcp_ip_abort_cinterval; 9747 break; 9748 default: 9749 return (-1); 9750 } 9751 break; 9752 case IPPROTO_IP: 9753 switch (name) { 9754 case IP_TTL: 9755 *i1 = tcp_ipv4_ttl; 9756 break; 9757 default: 9758 return (-1); 9759 } 9760 break; 9761 case IPPROTO_IPV6: 9762 switch (name) { 9763 case IPV6_UNICAST_HOPS: 9764 *i1 = tcp_ipv6_hoplimit; 9765 break; 9766 default: 9767 return (-1); 9768 } 9769 break; 9770 default: 9771 return (-1); 9772 } 9773 return (sizeof (int)); 9774 } 9775 9776 9777 /* 9778 * TCP routine to get the values of options. 9779 */ 9780 int 9781 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9782 { 9783 int *i1 = (int *)ptr; 9784 conn_t *connp = Q_TO_CONN(q); 9785 tcp_t *tcp = connp->conn_tcp; 9786 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9787 9788 switch (level) { 9789 case SOL_SOCKET: 9790 switch (name) { 9791 case SO_LINGER: { 9792 struct linger *lgr = (struct linger *)ptr; 9793 9794 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9795 lgr->l_linger = tcp->tcp_lingertime; 9796 } 9797 return (sizeof (struct linger)); 9798 case SO_DEBUG: 9799 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9800 break; 9801 case SO_KEEPALIVE: 9802 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9803 break; 9804 case SO_DONTROUTE: 9805 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9806 break; 9807 case SO_USELOOPBACK: 9808 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9809 break; 9810 case SO_BROADCAST: 9811 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9812 break; 9813 case SO_REUSEADDR: 9814 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9815 break; 9816 case SO_OOBINLINE: 9817 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9818 break; 9819 case SO_DGRAM_ERRIND: 9820 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9821 break; 9822 case SO_TYPE: 9823 *i1 = SOCK_STREAM; 9824 break; 9825 case SO_SNDBUF: 9826 *i1 = tcp->tcp_xmit_hiwater; 9827 break; 9828 case SO_RCVBUF: 9829 *i1 = RD(q)->q_hiwat; 9830 break; 9831 case SO_SND_COPYAVOID: 9832 *i1 = tcp->tcp_snd_zcopy_on ? 9833 SO_SND_COPYAVOID : 0; 9834 break; 9835 case SO_ALLZONES: 9836 *i1 = connp->conn_allzones ? 1 : 0; 9837 break; 9838 case SO_ANON_MLP: 9839 *i1 = connp->conn_anon_mlp; 9840 break; 9841 case SO_MAC_EXEMPT: 9842 *i1 = connp->conn_mac_exempt; 9843 break; 9844 case SO_EXCLBIND: 9845 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9846 break; 9847 default: 9848 return (-1); 9849 } 9850 break; 9851 case IPPROTO_TCP: 9852 switch (name) { 9853 case TCP_NODELAY: 9854 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9855 break; 9856 case TCP_MAXSEG: 9857 *i1 = tcp->tcp_mss; 9858 break; 9859 case TCP_NOTIFY_THRESHOLD: 9860 *i1 = (int)tcp->tcp_first_timer_threshold; 9861 break; 9862 case TCP_ABORT_THRESHOLD: 9863 *i1 = tcp->tcp_second_timer_threshold; 9864 break; 9865 case TCP_CONN_NOTIFY_THRESHOLD: 9866 *i1 = tcp->tcp_first_ctimer_threshold; 9867 break; 9868 case TCP_CONN_ABORT_THRESHOLD: 9869 *i1 = tcp->tcp_second_ctimer_threshold; 9870 break; 9871 case TCP_RECVDSTADDR: 9872 *i1 = tcp->tcp_recvdstaddr; 9873 break; 9874 case TCP_ANONPRIVBIND: 9875 *i1 = tcp->tcp_anon_priv_bind; 9876 break; 9877 case TCP_EXCLBIND: 9878 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9879 break; 9880 case TCP_INIT_CWND: 9881 *i1 = tcp->tcp_init_cwnd; 9882 break; 9883 case TCP_KEEPALIVE_THRESHOLD: 9884 *i1 = tcp->tcp_ka_interval; 9885 break; 9886 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9887 *i1 = tcp->tcp_ka_abort_thres; 9888 break; 9889 case TCP_CORK: 9890 *i1 = tcp->tcp_cork; 9891 break; 9892 default: 9893 return (-1); 9894 } 9895 break; 9896 case IPPROTO_IP: 9897 if (tcp->tcp_family != AF_INET) 9898 return (-1); 9899 switch (name) { 9900 case IP_OPTIONS: 9901 case T_IP_OPTIONS: { 9902 /* 9903 * This is compatible with BSD in that in only return 9904 * the reverse source route with the final destination 9905 * as the last entry. The first 4 bytes of the option 9906 * will contain the final destination. 9907 */ 9908 int opt_len; 9909 9910 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9911 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9912 ASSERT(opt_len >= 0); 9913 /* Caller ensures enough space */ 9914 if (opt_len > 0) { 9915 /* 9916 * TODO: Do we have to handle getsockopt on an 9917 * initiator as well? 9918 */ 9919 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9920 } 9921 return (0); 9922 } 9923 case IP_TOS: 9924 case T_IP_TOS: 9925 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9926 break; 9927 case IP_TTL: 9928 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9929 break; 9930 case IP_NEXTHOP: 9931 /* Handled at IP level */ 9932 return (-EINVAL); 9933 default: 9934 return (-1); 9935 } 9936 break; 9937 case IPPROTO_IPV6: 9938 /* 9939 * IPPROTO_IPV6 options are only supported for sockets 9940 * that are using IPv6 on the wire. 9941 */ 9942 if (tcp->tcp_ipversion != IPV6_VERSION) { 9943 return (-1); 9944 } 9945 switch (name) { 9946 case IPV6_UNICAST_HOPS: 9947 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9948 break; /* goto sizeof (int) option return */ 9949 case IPV6_BOUND_IF: 9950 /* Zero if not set */ 9951 *i1 = tcp->tcp_bound_if; 9952 break; /* goto sizeof (int) option return */ 9953 case IPV6_RECVPKTINFO: 9954 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9955 *i1 = 1; 9956 else 9957 *i1 = 0; 9958 break; /* goto sizeof (int) option return */ 9959 case IPV6_RECVTCLASS: 9960 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9961 *i1 = 1; 9962 else 9963 *i1 = 0; 9964 break; /* goto sizeof (int) option return */ 9965 case IPV6_RECVHOPLIMIT: 9966 if (tcp->tcp_ipv6_recvancillary & 9967 TCP_IPV6_RECVHOPLIMIT) 9968 *i1 = 1; 9969 else 9970 *i1 = 0; 9971 break; /* goto sizeof (int) option return */ 9972 case IPV6_RECVHOPOPTS: 9973 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9974 *i1 = 1; 9975 else 9976 *i1 = 0; 9977 break; /* goto sizeof (int) option return */ 9978 case IPV6_RECVDSTOPTS: 9979 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9980 *i1 = 1; 9981 else 9982 *i1 = 0; 9983 break; /* goto sizeof (int) option return */ 9984 case _OLD_IPV6_RECVDSTOPTS: 9985 if (tcp->tcp_ipv6_recvancillary & 9986 TCP_OLD_IPV6_RECVDSTOPTS) 9987 *i1 = 1; 9988 else 9989 *i1 = 0; 9990 break; /* goto sizeof (int) option return */ 9991 case IPV6_RECVRTHDR: 9992 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9993 *i1 = 1; 9994 else 9995 *i1 = 0; 9996 break; /* goto sizeof (int) option return */ 9997 case IPV6_RECVRTHDRDSTOPTS: 9998 if (tcp->tcp_ipv6_recvancillary & 9999 TCP_IPV6_RECVRTDSTOPTS) 10000 *i1 = 1; 10001 else 10002 *i1 = 0; 10003 break; /* goto sizeof (int) option return */ 10004 case IPV6_PKTINFO: { 10005 /* XXX assumes that caller has room for max size! */ 10006 struct in6_pktinfo *pkti; 10007 10008 pkti = (struct in6_pktinfo *)ptr; 10009 if (ipp->ipp_fields & IPPF_IFINDEX) 10010 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10011 else 10012 pkti->ipi6_ifindex = 0; 10013 if (ipp->ipp_fields & IPPF_ADDR) 10014 pkti->ipi6_addr = ipp->ipp_addr; 10015 else 10016 pkti->ipi6_addr = ipv6_all_zeros; 10017 return (sizeof (struct in6_pktinfo)); 10018 } 10019 case IPV6_TCLASS: 10020 if (ipp->ipp_fields & IPPF_TCLASS) 10021 *i1 = ipp->ipp_tclass; 10022 else 10023 *i1 = IPV6_FLOW_TCLASS( 10024 IPV6_DEFAULT_VERS_AND_FLOW); 10025 break; /* goto sizeof (int) option return */ 10026 case IPV6_NEXTHOP: { 10027 sin6_t *sin6 = (sin6_t *)ptr; 10028 10029 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10030 return (0); 10031 *sin6 = sin6_null; 10032 sin6->sin6_family = AF_INET6; 10033 sin6->sin6_addr = ipp->ipp_nexthop; 10034 return (sizeof (sin6_t)); 10035 } 10036 case IPV6_HOPOPTS: 10037 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10038 return (0); 10039 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10040 return (0); 10041 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10042 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10043 if (tcp->tcp_label_len > 0) { 10044 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10045 ptr[1] = (ipp->ipp_hopoptslen - 10046 tcp->tcp_label_len + 7) / 8 - 1; 10047 } 10048 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10049 case IPV6_RTHDRDSTOPTS: 10050 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10051 return (0); 10052 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10053 return (ipp->ipp_rtdstoptslen); 10054 case IPV6_RTHDR: 10055 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10056 return (0); 10057 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10058 return (ipp->ipp_rthdrlen); 10059 case IPV6_DSTOPTS: 10060 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10061 return (0); 10062 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10063 return (ipp->ipp_dstoptslen); 10064 case IPV6_SRC_PREFERENCES: 10065 return (ip6_get_src_preferences(connp, 10066 (uint32_t *)ptr)); 10067 case IPV6_PATHMTU: { 10068 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10069 10070 if (tcp->tcp_state < TCPS_ESTABLISHED) 10071 return (-1); 10072 10073 return (ip_fill_mtuinfo(&connp->conn_remv6, 10074 connp->conn_fport, mtuinfo)); 10075 } 10076 default: 10077 return (-1); 10078 } 10079 break; 10080 default: 10081 return (-1); 10082 } 10083 return (sizeof (int)); 10084 } 10085 10086 /* 10087 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10088 * Parameters are assumed to be verified by the caller. 10089 */ 10090 /* ARGSUSED */ 10091 int 10092 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10093 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10094 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10095 { 10096 conn_t *connp = Q_TO_CONN(q); 10097 tcp_t *tcp = connp->conn_tcp; 10098 int *i1 = (int *)invalp; 10099 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10100 boolean_t checkonly; 10101 int reterr; 10102 10103 switch (optset_context) { 10104 case SETFN_OPTCOM_CHECKONLY: 10105 checkonly = B_TRUE; 10106 /* 10107 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10108 * inlen != 0 implies value supplied and 10109 * we have to "pretend" to set it. 10110 * inlen == 0 implies that there is no 10111 * value part in T_CHECK request and just validation 10112 * done elsewhere should be enough, we just return here. 10113 */ 10114 if (inlen == 0) { 10115 *outlenp = 0; 10116 return (0); 10117 } 10118 break; 10119 case SETFN_OPTCOM_NEGOTIATE: 10120 checkonly = B_FALSE; 10121 break; 10122 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10123 case SETFN_CONN_NEGOTIATE: 10124 checkonly = B_FALSE; 10125 /* 10126 * Negotiating local and "association-related" options 10127 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10128 * primitives is allowed by XTI, but we choose 10129 * to not implement this style negotiation for Internet 10130 * protocols (We interpret it is a must for OSI world but 10131 * optional for Internet protocols) for all options. 10132 * [ Will do only for the few options that enable test 10133 * suites that our XTI implementation of this feature 10134 * works for transports that do allow it ] 10135 */ 10136 if (!tcp_allow_connopt_set(level, name)) { 10137 *outlenp = 0; 10138 return (EINVAL); 10139 } 10140 break; 10141 default: 10142 /* 10143 * We should never get here 10144 */ 10145 *outlenp = 0; 10146 return (EINVAL); 10147 } 10148 10149 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10150 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10151 10152 /* 10153 * For TCP, we should have no ancillary data sent down 10154 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10155 * has to be zero. 10156 */ 10157 ASSERT(thisdg_attrs == NULL); 10158 10159 /* 10160 * For fixed length options, no sanity check 10161 * of passed in length is done. It is assumed *_optcom_req() 10162 * routines do the right thing. 10163 */ 10164 10165 switch (level) { 10166 case SOL_SOCKET: 10167 switch (name) { 10168 case SO_LINGER: { 10169 struct linger *lgr = (struct linger *)invalp; 10170 10171 if (!checkonly) { 10172 if (lgr->l_onoff) { 10173 tcp->tcp_linger = 1; 10174 tcp->tcp_lingertime = lgr->l_linger; 10175 } else { 10176 tcp->tcp_linger = 0; 10177 tcp->tcp_lingertime = 0; 10178 } 10179 /* struct copy */ 10180 *(struct linger *)outvalp = *lgr; 10181 } else { 10182 if (!lgr->l_onoff) { 10183 ((struct linger *)outvalp)->l_onoff = 0; 10184 ((struct linger *)outvalp)->l_linger = 0; 10185 } else { 10186 /* struct copy */ 10187 *(struct linger *)outvalp = *lgr; 10188 } 10189 } 10190 *outlenp = sizeof (struct linger); 10191 return (0); 10192 } 10193 case SO_DEBUG: 10194 if (!checkonly) 10195 tcp->tcp_debug = onoff; 10196 break; 10197 case SO_KEEPALIVE: 10198 if (checkonly) { 10199 /* T_CHECK case */ 10200 break; 10201 } 10202 10203 if (!onoff) { 10204 if (tcp->tcp_ka_enabled) { 10205 if (tcp->tcp_ka_tid != 0) { 10206 (void) TCP_TIMER_CANCEL(tcp, 10207 tcp->tcp_ka_tid); 10208 tcp->tcp_ka_tid = 0; 10209 } 10210 tcp->tcp_ka_enabled = 0; 10211 } 10212 break; 10213 } 10214 if (!tcp->tcp_ka_enabled) { 10215 /* Crank up the keepalive timer */ 10216 tcp->tcp_ka_last_intrvl = 0; 10217 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10218 tcp_keepalive_killer, 10219 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10220 tcp->tcp_ka_enabled = 1; 10221 } 10222 break; 10223 case SO_DONTROUTE: 10224 /* 10225 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10226 * only of interest to IP. We track them here only so 10227 * that we can report their current value. 10228 */ 10229 if (!checkonly) { 10230 tcp->tcp_dontroute = onoff; 10231 tcp->tcp_connp->conn_dontroute = onoff; 10232 } 10233 break; 10234 case SO_USELOOPBACK: 10235 if (!checkonly) { 10236 tcp->tcp_useloopback = onoff; 10237 tcp->tcp_connp->conn_loopback = onoff; 10238 } 10239 break; 10240 case SO_BROADCAST: 10241 if (!checkonly) { 10242 tcp->tcp_broadcast = onoff; 10243 tcp->tcp_connp->conn_broadcast = onoff; 10244 } 10245 break; 10246 case SO_REUSEADDR: 10247 if (!checkonly) { 10248 tcp->tcp_reuseaddr = onoff; 10249 tcp->tcp_connp->conn_reuseaddr = onoff; 10250 } 10251 break; 10252 case SO_OOBINLINE: 10253 if (!checkonly) 10254 tcp->tcp_oobinline = onoff; 10255 break; 10256 case SO_DGRAM_ERRIND: 10257 if (!checkonly) 10258 tcp->tcp_dgram_errind = onoff; 10259 break; 10260 case SO_SNDBUF: { 10261 tcp_t *peer_tcp; 10262 10263 if (*i1 > tcp_max_buf) { 10264 *outlenp = 0; 10265 return (ENOBUFS); 10266 } 10267 if (checkonly) 10268 break; 10269 10270 tcp->tcp_xmit_hiwater = *i1; 10271 if (tcp_snd_lowat_fraction != 0) 10272 tcp->tcp_xmit_lowater = 10273 tcp->tcp_xmit_hiwater / 10274 tcp_snd_lowat_fraction; 10275 (void) tcp_maxpsz_set(tcp, B_TRUE); 10276 /* 10277 * If we are flow-controlled, recheck the condition. 10278 * There are apps that increase SO_SNDBUF size when 10279 * flow-controlled (EWOULDBLOCK), and expect the flow 10280 * control condition to be lifted right away. 10281 * 10282 * For the fused tcp loopback case, in order to avoid 10283 * a race with the peer's tcp_fuse_rrw() we need to 10284 * hold its fuse_lock while accessing tcp_flow_stopped. 10285 */ 10286 peer_tcp = tcp->tcp_loopback_peer; 10287 ASSERT(!tcp->tcp_fused || peer_tcp != NULL); 10288 if (tcp->tcp_fused) 10289 mutex_enter(&peer_tcp->tcp_fuse_lock); 10290 10291 if (tcp->tcp_flow_stopped && 10292 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10293 tcp_clrqfull(tcp); 10294 } 10295 if (tcp->tcp_fused) 10296 mutex_exit(&peer_tcp->tcp_fuse_lock); 10297 break; 10298 } 10299 case SO_RCVBUF: 10300 if (*i1 > tcp_max_buf) { 10301 *outlenp = 0; 10302 return (ENOBUFS); 10303 } 10304 /* Silently ignore zero */ 10305 if (!checkonly && *i1 != 0) { 10306 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10307 (void) tcp_rwnd_set(tcp, *i1); 10308 } 10309 /* 10310 * XXX should we return the rwnd here 10311 * and tcp_opt_get ? 10312 */ 10313 break; 10314 case SO_SND_COPYAVOID: 10315 if (!checkonly) { 10316 /* we only allow enable at most once for now */ 10317 if (tcp->tcp_loopback || 10318 (!tcp->tcp_snd_zcopy_aware && 10319 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10320 *outlenp = 0; 10321 return (EOPNOTSUPP); 10322 } 10323 tcp->tcp_snd_zcopy_aware = 1; 10324 } 10325 break; 10326 case SO_ALLZONES: 10327 /* Handled at the IP level */ 10328 return (-EINVAL); 10329 case SO_ANON_MLP: 10330 if (!checkonly) { 10331 mutex_enter(&connp->conn_lock); 10332 connp->conn_anon_mlp = onoff; 10333 mutex_exit(&connp->conn_lock); 10334 } 10335 break; 10336 case SO_MAC_EXEMPT: 10337 if (secpolicy_net_mac_aware(cr) != 0 || 10338 IPCL_IS_BOUND(connp)) 10339 return (EACCES); 10340 if (!checkonly) { 10341 mutex_enter(&connp->conn_lock); 10342 connp->conn_mac_exempt = onoff; 10343 mutex_exit(&connp->conn_lock); 10344 } 10345 break; 10346 case SO_EXCLBIND: 10347 if (!checkonly) 10348 tcp->tcp_exclbind = onoff; 10349 break; 10350 default: 10351 *outlenp = 0; 10352 return (EINVAL); 10353 } 10354 break; 10355 case IPPROTO_TCP: 10356 switch (name) { 10357 case TCP_NODELAY: 10358 if (!checkonly) 10359 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10360 break; 10361 case TCP_NOTIFY_THRESHOLD: 10362 if (!checkonly) 10363 tcp->tcp_first_timer_threshold = *i1; 10364 break; 10365 case TCP_ABORT_THRESHOLD: 10366 if (!checkonly) 10367 tcp->tcp_second_timer_threshold = *i1; 10368 break; 10369 case TCP_CONN_NOTIFY_THRESHOLD: 10370 if (!checkonly) 10371 tcp->tcp_first_ctimer_threshold = *i1; 10372 break; 10373 case TCP_CONN_ABORT_THRESHOLD: 10374 if (!checkonly) 10375 tcp->tcp_second_ctimer_threshold = *i1; 10376 break; 10377 case TCP_RECVDSTADDR: 10378 if (tcp->tcp_state > TCPS_LISTEN) 10379 return (EOPNOTSUPP); 10380 if (!checkonly) 10381 tcp->tcp_recvdstaddr = onoff; 10382 break; 10383 case TCP_ANONPRIVBIND: 10384 if ((reterr = secpolicy_net_privaddr(cr, 0)) != 0) { 10385 *outlenp = 0; 10386 return (reterr); 10387 } 10388 if (!checkonly) { 10389 tcp->tcp_anon_priv_bind = onoff; 10390 } 10391 break; 10392 case TCP_EXCLBIND: 10393 if (!checkonly) 10394 tcp->tcp_exclbind = onoff; 10395 break; /* goto sizeof (int) option return */ 10396 case TCP_INIT_CWND: { 10397 uint32_t init_cwnd = *((uint32_t *)invalp); 10398 10399 if (checkonly) 10400 break; 10401 10402 /* 10403 * Only allow socket with network configuration 10404 * privilege to set the initial cwnd to be larger 10405 * than allowed by RFC 3390. 10406 */ 10407 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10408 tcp->tcp_init_cwnd = init_cwnd; 10409 break; 10410 } 10411 if ((reterr = secpolicy_net_config(cr, B_TRUE)) != 0) { 10412 *outlenp = 0; 10413 return (reterr); 10414 } 10415 if (init_cwnd > TCP_MAX_INIT_CWND) { 10416 *outlenp = 0; 10417 return (EINVAL); 10418 } 10419 tcp->tcp_init_cwnd = init_cwnd; 10420 break; 10421 } 10422 case TCP_KEEPALIVE_THRESHOLD: 10423 if (checkonly) 10424 break; 10425 10426 if (*i1 < tcp_keepalive_interval_low || 10427 *i1 > tcp_keepalive_interval_high) { 10428 *outlenp = 0; 10429 return (EINVAL); 10430 } 10431 if (*i1 != tcp->tcp_ka_interval) { 10432 tcp->tcp_ka_interval = *i1; 10433 /* 10434 * Check if we need to restart the 10435 * keepalive timer. 10436 */ 10437 if (tcp->tcp_ka_tid != 0) { 10438 ASSERT(tcp->tcp_ka_enabled); 10439 (void) TCP_TIMER_CANCEL(tcp, 10440 tcp->tcp_ka_tid); 10441 tcp->tcp_ka_last_intrvl = 0; 10442 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10443 tcp_keepalive_killer, 10444 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10445 } 10446 } 10447 break; 10448 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10449 if (!checkonly) { 10450 if (*i1 < tcp_keepalive_abort_interval_low || 10451 *i1 > tcp_keepalive_abort_interval_high) { 10452 *outlenp = 0; 10453 return (EINVAL); 10454 } 10455 tcp->tcp_ka_abort_thres = *i1; 10456 } 10457 break; 10458 case TCP_CORK: 10459 if (!checkonly) { 10460 /* 10461 * if tcp->tcp_cork was set and is now 10462 * being unset, we have to make sure that 10463 * the remaining data gets sent out. Also 10464 * unset tcp->tcp_cork so that tcp_wput_data() 10465 * can send data even if it is less than mss 10466 */ 10467 if (tcp->tcp_cork && onoff == 0 && 10468 tcp->tcp_unsent > 0) { 10469 tcp->tcp_cork = B_FALSE; 10470 tcp_wput_data(tcp, NULL, B_FALSE); 10471 } 10472 tcp->tcp_cork = onoff; 10473 } 10474 break; 10475 default: 10476 *outlenp = 0; 10477 return (EINVAL); 10478 } 10479 break; 10480 case IPPROTO_IP: 10481 if (tcp->tcp_family != AF_INET) { 10482 *outlenp = 0; 10483 return (ENOPROTOOPT); 10484 } 10485 switch (name) { 10486 case IP_OPTIONS: 10487 case T_IP_OPTIONS: 10488 reterr = tcp_opt_set_header(tcp, checkonly, 10489 invalp, inlen); 10490 if (reterr) { 10491 *outlenp = 0; 10492 return (reterr); 10493 } 10494 /* OK return - copy input buffer into output buffer */ 10495 if (invalp != outvalp) { 10496 /* don't trust bcopy for identical src/dst */ 10497 bcopy(invalp, outvalp, inlen); 10498 } 10499 *outlenp = inlen; 10500 return (0); 10501 case IP_TOS: 10502 case T_IP_TOS: 10503 if (!checkonly) { 10504 tcp->tcp_ipha->ipha_type_of_service = 10505 (uchar_t)*i1; 10506 tcp->tcp_tos = (uchar_t)*i1; 10507 } 10508 break; 10509 case IP_TTL: 10510 if (!checkonly) { 10511 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10512 tcp->tcp_ttl = (uchar_t)*i1; 10513 } 10514 break; 10515 case IP_BOUND_IF: 10516 case IP_NEXTHOP: 10517 /* Handled at the IP level */ 10518 return (-EINVAL); 10519 case IP_SEC_OPT: 10520 /* 10521 * We should not allow policy setting after 10522 * we start listening for connections. 10523 */ 10524 if (tcp->tcp_state == TCPS_LISTEN) { 10525 return (EINVAL); 10526 } else { 10527 /* Handled at the IP level */ 10528 return (-EINVAL); 10529 } 10530 default: 10531 *outlenp = 0; 10532 return (EINVAL); 10533 } 10534 break; 10535 case IPPROTO_IPV6: { 10536 ip6_pkt_t *ipp; 10537 10538 /* 10539 * IPPROTO_IPV6 options are only supported for sockets 10540 * that are using IPv6 on the wire. 10541 */ 10542 if (tcp->tcp_ipversion != IPV6_VERSION) { 10543 *outlenp = 0; 10544 return (ENOPROTOOPT); 10545 } 10546 /* 10547 * Only sticky options; no ancillary data 10548 */ 10549 ASSERT(thisdg_attrs == NULL); 10550 ipp = &tcp->tcp_sticky_ipp; 10551 10552 switch (name) { 10553 case IPV6_UNICAST_HOPS: 10554 /* -1 means use default */ 10555 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10556 *outlenp = 0; 10557 return (EINVAL); 10558 } 10559 if (!checkonly) { 10560 if (*i1 == -1) { 10561 tcp->tcp_ip6h->ip6_hops = 10562 ipp->ipp_unicast_hops = 10563 (uint8_t)tcp_ipv6_hoplimit; 10564 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10565 /* Pass modified value to IP. */ 10566 *i1 = tcp->tcp_ip6h->ip6_hops; 10567 } else { 10568 tcp->tcp_ip6h->ip6_hops = 10569 ipp->ipp_unicast_hops = 10570 (uint8_t)*i1; 10571 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10572 } 10573 reterr = tcp_build_hdrs(q, tcp); 10574 if (reterr != 0) 10575 return (reterr); 10576 } 10577 break; 10578 case IPV6_BOUND_IF: 10579 if (!checkonly) { 10580 int error = 0; 10581 10582 tcp->tcp_bound_if = *i1; 10583 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10584 B_TRUE, checkonly, level, name, mblk); 10585 if (error != 0) { 10586 *outlenp = 0; 10587 return (error); 10588 } 10589 } 10590 break; 10591 /* 10592 * Set boolean switches for ancillary data delivery 10593 */ 10594 case IPV6_RECVPKTINFO: 10595 if (!checkonly) { 10596 if (onoff) 10597 tcp->tcp_ipv6_recvancillary |= 10598 TCP_IPV6_RECVPKTINFO; 10599 else 10600 tcp->tcp_ipv6_recvancillary &= 10601 ~TCP_IPV6_RECVPKTINFO; 10602 /* Force it to be sent up with the next msg */ 10603 tcp->tcp_recvifindex = 0; 10604 } 10605 break; 10606 case IPV6_RECVTCLASS: 10607 if (!checkonly) { 10608 if (onoff) 10609 tcp->tcp_ipv6_recvancillary |= 10610 TCP_IPV6_RECVTCLASS; 10611 else 10612 tcp->tcp_ipv6_recvancillary &= 10613 ~TCP_IPV6_RECVTCLASS; 10614 } 10615 break; 10616 case IPV6_RECVHOPLIMIT: 10617 if (!checkonly) { 10618 if (onoff) 10619 tcp->tcp_ipv6_recvancillary |= 10620 TCP_IPV6_RECVHOPLIMIT; 10621 else 10622 tcp->tcp_ipv6_recvancillary &= 10623 ~TCP_IPV6_RECVHOPLIMIT; 10624 /* Force it to be sent up with the next msg */ 10625 tcp->tcp_recvhops = 0xffffffffU; 10626 } 10627 break; 10628 case IPV6_RECVHOPOPTS: 10629 if (!checkonly) { 10630 if (onoff) 10631 tcp->tcp_ipv6_recvancillary |= 10632 TCP_IPV6_RECVHOPOPTS; 10633 else 10634 tcp->tcp_ipv6_recvancillary &= 10635 ~TCP_IPV6_RECVHOPOPTS; 10636 } 10637 break; 10638 case IPV6_RECVDSTOPTS: 10639 if (!checkonly) { 10640 if (onoff) 10641 tcp->tcp_ipv6_recvancillary |= 10642 TCP_IPV6_RECVDSTOPTS; 10643 else 10644 tcp->tcp_ipv6_recvancillary &= 10645 ~TCP_IPV6_RECVDSTOPTS; 10646 } 10647 break; 10648 case _OLD_IPV6_RECVDSTOPTS: 10649 if (!checkonly) { 10650 if (onoff) 10651 tcp->tcp_ipv6_recvancillary |= 10652 TCP_OLD_IPV6_RECVDSTOPTS; 10653 else 10654 tcp->tcp_ipv6_recvancillary &= 10655 ~TCP_OLD_IPV6_RECVDSTOPTS; 10656 } 10657 break; 10658 case IPV6_RECVRTHDR: 10659 if (!checkonly) { 10660 if (onoff) 10661 tcp->tcp_ipv6_recvancillary |= 10662 TCP_IPV6_RECVRTHDR; 10663 else 10664 tcp->tcp_ipv6_recvancillary &= 10665 ~TCP_IPV6_RECVRTHDR; 10666 } 10667 break; 10668 case IPV6_RECVRTHDRDSTOPTS: 10669 if (!checkonly) { 10670 if (onoff) 10671 tcp->tcp_ipv6_recvancillary |= 10672 TCP_IPV6_RECVRTDSTOPTS; 10673 else 10674 tcp->tcp_ipv6_recvancillary &= 10675 ~TCP_IPV6_RECVRTDSTOPTS; 10676 } 10677 break; 10678 case IPV6_PKTINFO: 10679 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10680 return (EINVAL); 10681 if (checkonly) 10682 break; 10683 10684 if (inlen == 0) { 10685 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10686 } else { 10687 struct in6_pktinfo *pkti; 10688 10689 pkti = (struct in6_pktinfo *)invalp; 10690 /* 10691 * RFC 3542 states that ipi6_addr must be 10692 * the unspecified address when setting the 10693 * IPV6_PKTINFO sticky socket option on a 10694 * TCP socket. 10695 */ 10696 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10697 return (EINVAL); 10698 /* 10699 * ip6_set_pktinfo() validates the source 10700 * address and interface index. 10701 */ 10702 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10703 pkti, mblk); 10704 if (reterr != 0) 10705 return (reterr); 10706 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10707 ipp->ipp_addr = pkti->ipi6_addr; 10708 if (ipp->ipp_ifindex != 0) 10709 ipp->ipp_fields |= IPPF_IFINDEX; 10710 else 10711 ipp->ipp_fields &= ~IPPF_IFINDEX; 10712 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10713 ipp->ipp_fields |= IPPF_ADDR; 10714 else 10715 ipp->ipp_fields &= ~IPPF_ADDR; 10716 } 10717 reterr = tcp_build_hdrs(q, tcp); 10718 if (reterr != 0) 10719 return (reterr); 10720 break; 10721 case IPV6_TCLASS: 10722 if (inlen != 0 && inlen != sizeof (int)) 10723 return (EINVAL); 10724 if (checkonly) 10725 break; 10726 10727 if (inlen == 0) { 10728 ipp->ipp_fields &= ~IPPF_TCLASS; 10729 } else { 10730 if (*i1 > 255 || *i1 < -1) 10731 return (EINVAL); 10732 if (*i1 == -1) { 10733 ipp->ipp_tclass = 0; 10734 *i1 = 0; 10735 } else { 10736 ipp->ipp_tclass = *i1; 10737 } 10738 ipp->ipp_fields |= IPPF_TCLASS; 10739 } 10740 reterr = tcp_build_hdrs(q, tcp); 10741 if (reterr != 0) 10742 return (reterr); 10743 break; 10744 case IPV6_NEXTHOP: 10745 /* 10746 * IP will verify that the nexthop is reachable 10747 * and fail for sticky options. 10748 */ 10749 if (inlen != 0 && inlen != sizeof (sin6_t)) 10750 return (EINVAL); 10751 if (checkonly) 10752 break; 10753 10754 if (inlen == 0) { 10755 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10756 } else { 10757 sin6_t *sin6 = (sin6_t *)invalp; 10758 10759 if (sin6->sin6_family != AF_INET6) 10760 return (EAFNOSUPPORT); 10761 if (IN6_IS_ADDR_V4MAPPED( 10762 &sin6->sin6_addr)) 10763 return (EADDRNOTAVAIL); 10764 ipp->ipp_nexthop = sin6->sin6_addr; 10765 if (!IN6_IS_ADDR_UNSPECIFIED( 10766 &ipp->ipp_nexthop)) 10767 ipp->ipp_fields |= IPPF_NEXTHOP; 10768 else 10769 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10770 } 10771 reterr = tcp_build_hdrs(q, tcp); 10772 if (reterr != 0) 10773 return (reterr); 10774 break; 10775 case IPV6_HOPOPTS: { 10776 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10777 10778 /* 10779 * Sanity checks - minimum size, size a multiple of 10780 * eight bytes, and matching size passed in. 10781 */ 10782 if (inlen != 0 && 10783 inlen != (8 * (hopts->ip6h_len + 1))) 10784 return (EINVAL); 10785 10786 if (checkonly) 10787 break; 10788 10789 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10790 (uchar_t **)&ipp->ipp_hopopts, 10791 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10792 if (reterr != 0) 10793 return (reterr); 10794 if (ipp->ipp_hopoptslen == 0) 10795 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10796 else 10797 ipp->ipp_fields |= IPPF_HOPOPTS; 10798 reterr = tcp_build_hdrs(q, tcp); 10799 if (reterr != 0) 10800 return (reterr); 10801 break; 10802 } 10803 case IPV6_RTHDRDSTOPTS: { 10804 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10805 10806 /* 10807 * Sanity checks - minimum size, size a multiple of 10808 * eight bytes, and matching size passed in. 10809 */ 10810 if (inlen != 0 && 10811 inlen != (8 * (dopts->ip6d_len + 1))) 10812 return (EINVAL); 10813 10814 if (checkonly) 10815 break; 10816 10817 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10818 (uchar_t **)&ipp->ipp_rtdstopts, 10819 &ipp->ipp_rtdstoptslen, 0); 10820 if (reterr != 0) 10821 return (reterr); 10822 if (ipp->ipp_rtdstoptslen == 0) 10823 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10824 else 10825 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10826 reterr = tcp_build_hdrs(q, tcp); 10827 if (reterr != 0) 10828 return (reterr); 10829 break; 10830 } 10831 case IPV6_DSTOPTS: { 10832 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10833 10834 /* 10835 * Sanity checks - minimum size, size a multiple of 10836 * eight bytes, and matching size passed in. 10837 */ 10838 if (inlen != 0 && 10839 inlen != (8 * (dopts->ip6d_len + 1))) 10840 return (EINVAL); 10841 10842 if (checkonly) 10843 break; 10844 10845 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10846 (uchar_t **)&ipp->ipp_dstopts, 10847 &ipp->ipp_dstoptslen, 0); 10848 if (reterr != 0) 10849 return (reterr); 10850 if (ipp->ipp_dstoptslen == 0) 10851 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10852 else 10853 ipp->ipp_fields |= IPPF_DSTOPTS; 10854 reterr = tcp_build_hdrs(q, tcp); 10855 if (reterr != 0) 10856 return (reterr); 10857 break; 10858 } 10859 case IPV6_RTHDR: { 10860 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10861 10862 /* 10863 * Sanity checks - minimum size, size a multiple of 10864 * eight bytes, and matching size passed in. 10865 */ 10866 if (inlen != 0 && 10867 inlen != (8 * (rt->ip6r_len + 1))) 10868 return (EINVAL); 10869 10870 if (checkonly) 10871 break; 10872 10873 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10874 (uchar_t **)&ipp->ipp_rthdr, 10875 &ipp->ipp_rthdrlen, 0); 10876 if (reterr != 0) 10877 return (reterr); 10878 if (ipp->ipp_rthdrlen == 0) 10879 ipp->ipp_fields &= ~IPPF_RTHDR; 10880 else 10881 ipp->ipp_fields |= IPPF_RTHDR; 10882 reterr = tcp_build_hdrs(q, tcp); 10883 if (reterr != 0) 10884 return (reterr); 10885 break; 10886 } 10887 case IPV6_V6ONLY: 10888 if (!checkonly) 10889 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10890 break; 10891 case IPV6_USE_MIN_MTU: 10892 if (inlen != sizeof (int)) 10893 return (EINVAL); 10894 10895 if (*i1 < -1 || *i1 > 1) 10896 return (EINVAL); 10897 10898 if (checkonly) 10899 break; 10900 10901 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10902 ipp->ipp_use_min_mtu = *i1; 10903 break; 10904 case IPV6_BOUND_PIF: 10905 /* Handled at the IP level */ 10906 return (-EINVAL); 10907 case IPV6_SEC_OPT: 10908 /* 10909 * We should not allow policy setting after 10910 * we start listening for connections. 10911 */ 10912 if (tcp->tcp_state == TCPS_LISTEN) { 10913 return (EINVAL); 10914 } else { 10915 /* Handled at the IP level */ 10916 return (-EINVAL); 10917 } 10918 case IPV6_SRC_PREFERENCES: 10919 if (inlen != sizeof (uint32_t)) 10920 return (EINVAL); 10921 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10922 *(uint32_t *)invalp); 10923 if (reterr != 0) { 10924 *outlenp = 0; 10925 return (reterr); 10926 } 10927 break; 10928 default: 10929 *outlenp = 0; 10930 return (EINVAL); 10931 } 10932 break; 10933 } /* end IPPROTO_IPV6 */ 10934 default: 10935 *outlenp = 0; 10936 return (EINVAL); 10937 } 10938 /* 10939 * Common case of OK return with outval same as inval 10940 */ 10941 if (invalp != outvalp) { 10942 /* don't trust bcopy for identical src/dst */ 10943 (void) bcopy(invalp, outvalp, inlen); 10944 } 10945 *outlenp = inlen; 10946 return (0); 10947 } 10948 10949 /* 10950 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10951 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10952 * headers, and the maximum size tcp header (to avoid reallocation 10953 * on the fly for additional tcp options). 10954 * Returns failure if can't allocate memory. 10955 */ 10956 static int 10957 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 10958 { 10959 char *hdrs; 10960 uint_t hdrs_len; 10961 ip6i_t *ip6i; 10962 char buf[TCP_MAX_HDR_LENGTH]; 10963 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10964 in6_addr_t src, dst; 10965 10966 /* 10967 * save the existing tcp header and source/dest IP addresses 10968 */ 10969 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10970 src = tcp->tcp_ip6h->ip6_src; 10971 dst = tcp->tcp_ip6h->ip6_dst; 10972 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10973 ASSERT(hdrs_len != 0); 10974 if (hdrs_len > tcp->tcp_iphc_len) { 10975 /* Need to reallocate */ 10976 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10977 if (hdrs == NULL) 10978 return (ENOMEM); 10979 if (tcp->tcp_iphc != NULL) { 10980 if (tcp->tcp_hdr_grown) { 10981 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10982 } else { 10983 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10984 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10985 } 10986 tcp->tcp_iphc_len = 0; 10987 } 10988 ASSERT(tcp->tcp_iphc_len == 0); 10989 tcp->tcp_iphc = hdrs; 10990 tcp->tcp_iphc_len = hdrs_len; 10991 tcp->tcp_hdr_grown = B_TRUE; 10992 } 10993 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10994 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10995 10996 /* Set header fields not in ipp */ 10997 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10998 ip6i = (ip6i_t *)tcp->tcp_iphc; 10999 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11000 } else { 11001 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11002 } 11003 /* 11004 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11005 * 11006 * tcp->tcp_tcp_hdr_len doesn't change here. 11007 */ 11008 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11009 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11010 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11011 11012 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11013 11014 tcp->tcp_ip6h->ip6_src = src; 11015 tcp->tcp_ip6h->ip6_dst = dst; 11016 11017 /* 11018 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11019 * the default value for TCP. 11020 */ 11021 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11022 tcp->tcp_ip6h->ip6_hops = tcp_ipv6_hoplimit; 11023 11024 /* 11025 * If we're setting extension headers after a connection 11026 * has been established, and if we have a routing header 11027 * among the extension headers, call ip_massage_options_v6 to 11028 * manipulate the routing header/ip6_dst set the checksum 11029 * difference in the tcp header template. 11030 * (This happens in tcp_connect_ipv6 if the routing header 11031 * is set prior to the connect.) 11032 * Set the tcp_sum to zero first in case we've cleared a 11033 * routing header or don't have one at all. 11034 */ 11035 tcp->tcp_sum = 0; 11036 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11037 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11038 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11039 (uint8_t *)tcp->tcp_tcph); 11040 if (rth != NULL) { 11041 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11042 rth); 11043 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11044 (tcp->tcp_sum >> 16)); 11045 } 11046 } 11047 11048 /* Try to get everything in a single mblk */ 11049 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcp_wroff_xtra); 11050 return (0); 11051 } 11052 11053 /* 11054 * Transfer any source route option from ipha to buf/dst in reversed form. 11055 */ 11056 static int 11057 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11058 { 11059 ipoptp_t opts; 11060 uchar_t *opt; 11061 uint8_t optval; 11062 uint8_t optlen; 11063 uint32_t len = 0; 11064 11065 for (optval = ipoptp_first(&opts, ipha); 11066 optval != IPOPT_EOL; 11067 optval = ipoptp_next(&opts)) { 11068 opt = opts.ipoptp_cur; 11069 optlen = opts.ipoptp_len; 11070 switch (optval) { 11071 int off1, off2; 11072 case IPOPT_SSRR: 11073 case IPOPT_LSRR: 11074 11075 /* Reverse source route */ 11076 /* 11077 * First entry should be the next to last one in the 11078 * current source route (the last entry is our 11079 * address.) 11080 * The last entry should be the final destination. 11081 */ 11082 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11083 buf[IPOPT_OLEN] = (uint8_t)optlen; 11084 off1 = IPOPT_MINOFF_SR - 1; 11085 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11086 if (off2 < 0) { 11087 /* No entries in source route */ 11088 break; 11089 } 11090 bcopy(opt + off2, dst, IP_ADDR_LEN); 11091 /* 11092 * Note: use src since ipha has not had its src 11093 * and dst reversed (it is in the state it was 11094 * received. 11095 */ 11096 bcopy(&ipha->ipha_src, buf + off2, 11097 IP_ADDR_LEN); 11098 off2 -= IP_ADDR_LEN; 11099 11100 while (off2 > 0) { 11101 bcopy(opt + off2, buf + off1, 11102 IP_ADDR_LEN); 11103 off1 += IP_ADDR_LEN; 11104 off2 -= IP_ADDR_LEN; 11105 } 11106 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11107 buf += optlen; 11108 len += optlen; 11109 break; 11110 } 11111 } 11112 done: 11113 /* Pad the resulting options */ 11114 while (len & 0x3) { 11115 *buf++ = IPOPT_EOL; 11116 len++; 11117 } 11118 return (len); 11119 } 11120 11121 11122 /* 11123 * Extract and revert a source route from ipha (if any) 11124 * and then update the relevant fields in both tcp_t and the standard header. 11125 */ 11126 static void 11127 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11128 { 11129 char buf[TCP_MAX_HDR_LENGTH]; 11130 uint_t tcph_len; 11131 int len; 11132 11133 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11134 len = IPH_HDR_LENGTH(ipha); 11135 if (len == IP_SIMPLE_HDR_LENGTH) 11136 /* Nothing to do */ 11137 return; 11138 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11139 (len & 0x3)) 11140 return; 11141 11142 tcph_len = tcp->tcp_tcp_hdr_len; 11143 bcopy(tcp->tcp_tcph, buf, tcph_len); 11144 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11145 (tcp->tcp_ipha->ipha_dst & 0xffff); 11146 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11147 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11148 len += IP_SIMPLE_HDR_LENGTH; 11149 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11150 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11151 if ((int)tcp->tcp_sum < 0) 11152 tcp->tcp_sum--; 11153 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11154 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11155 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11156 bcopy(buf, tcp->tcp_tcph, tcph_len); 11157 tcp->tcp_ip_hdr_len = len; 11158 tcp->tcp_ipha->ipha_version_and_hdr_length = 11159 (IP_VERSION << 4) | (len >> 2); 11160 len += tcph_len; 11161 tcp->tcp_hdr_len = len; 11162 } 11163 11164 /* 11165 * Copy the standard header into its new location, 11166 * lay in the new options and then update the relevant 11167 * fields in both tcp_t and the standard header. 11168 */ 11169 static int 11170 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11171 { 11172 uint_t tcph_len; 11173 uint8_t *ip_optp; 11174 tcph_t *new_tcph; 11175 11176 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11177 return (EINVAL); 11178 11179 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11180 return (EINVAL); 11181 11182 if (checkonly) { 11183 /* 11184 * do not really set, just pretend to - T_CHECK 11185 */ 11186 return (0); 11187 } 11188 11189 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11190 if (tcp->tcp_label_len > 0) { 11191 int padlen; 11192 uint8_t opt; 11193 11194 /* convert list termination to no-ops */ 11195 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11196 ip_optp += ip_optp[IPOPT_OLEN]; 11197 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11198 while (--padlen >= 0) 11199 *ip_optp++ = opt; 11200 } 11201 tcph_len = tcp->tcp_tcp_hdr_len; 11202 new_tcph = (tcph_t *)(ip_optp + len); 11203 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11204 tcp->tcp_tcph = new_tcph; 11205 bcopy(ptr, ip_optp, len); 11206 11207 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11208 11209 tcp->tcp_ip_hdr_len = len; 11210 tcp->tcp_ipha->ipha_version_and_hdr_length = 11211 (IP_VERSION << 4) | (len >> 2); 11212 tcp->tcp_hdr_len = len + tcph_len; 11213 if (!TCP_IS_DETACHED(tcp)) { 11214 /* Always allocate room for all options. */ 11215 (void) mi_set_sth_wroff(tcp->tcp_rq, 11216 TCP_MAX_COMBINED_HEADER_LENGTH + tcp_wroff_xtra); 11217 } 11218 return (0); 11219 } 11220 11221 /* Get callback routine passed to nd_load by tcp_param_register */ 11222 /* ARGSUSED */ 11223 static int 11224 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11225 { 11226 tcpparam_t *tcppa = (tcpparam_t *)cp; 11227 11228 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11229 return (0); 11230 } 11231 11232 /* 11233 * Walk through the param array specified registering each element with the 11234 * named dispatch handler. 11235 */ 11236 static boolean_t 11237 tcp_param_register(tcpparam_t *tcppa, int cnt) 11238 { 11239 for (; cnt-- > 0; tcppa++) { 11240 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11241 if (!nd_load(&tcp_g_nd, tcppa->tcp_param_name, 11242 tcp_param_get, tcp_param_set, 11243 (caddr_t)tcppa)) { 11244 nd_free(&tcp_g_nd); 11245 return (B_FALSE); 11246 } 11247 } 11248 } 11249 if (!nd_load(&tcp_g_nd, tcp_wroff_xtra_param.tcp_param_name, 11250 tcp_param_get, tcp_param_set_aligned, 11251 (caddr_t)&tcp_wroff_xtra_param)) { 11252 nd_free(&tcp_g_nd); 11253 return (B_FALSE); 11254 } 11255 if (!nd_load(&tcp_g_nd, tcp_mdt_head_param.tcp_param_name, 11256 tcp_param_get, tcp_param_set_aligned, 11257 (caddr_t)&tcp_mdt_head_param)) { 11258 nd_free(&tcp_g_nd); 11259 return (B_FALSE); 11260 } 11261 if (!nd_load(&tcp_g_nd, tcp_mdt_tail_param.tcp_param_name, 11262 tcp_param_get, tcp_param_set_aligned, 11263 (caddr_t)&tcp_mdt_tail_param)) { 11264 nd_free(&tcp_g_nd); 11265 return (B_FALSE); 11266 } 11267 if (!nd_load(&tcp_g_nd, tcp_mdt_max_pbufs_param.tcp_param_name, 11268 tcp_param_get, tcp_param_set, 11269 (caddr_t)&tcp_mdt_max_pbufs_param)) { 11270 nd_free(&tcp_g_nd); 11271 return (B_FALSE); 11272 } 11273 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports", 11274 tcp_extra_priv_ports_get, NULL, NULL)) { 11275 nd_free(&tcp_g_nd); 11276 return (B_FALSE); 11277 } 11278 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports_add", 11279 NULL, tcp_extra_priv_ports_add, NULL)) { 11280 nd_free(&tcp_g_nd); 11281 return (B_FALSE); 11282 } 11283 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports_del", 11284 NULL, tcp_extra_priv_ports_del, NULL)) { 11285 nd_free(&tcp_g_nd); 11286 return (B_FALSE); 11287 } 11288 if (!nd_load(&tcp_g_nd, "tcp_status", tcp_status_report, NULL, 11289 NULL)) { 11290 nd_free(&tcp_g_nd); 11291 return (B_FALSE); 11292 } 11293 if (!nd_load(&tcp_g_nd, "tcp_bind_hash", tcp_bind_hash_report, 11294 NULL, NULL)) { 11295 nd_free(&tcp_g_nd); 11296 return (B_FALSE); 11297 } 11298 if (!nd_load(&tcp_g_nd, "tcp_listen_hash", tcp_listen_hash_report, 11299 NULL, NULL)) { 11300 nd_free(&tcp_g_nd); 11301 return (B_FALSE); 11302 } 11303 if (!nd_load(&tcp_g_nd, "tcp_conn_hash", tcp_conn_hash_report, 11304 NULL, NULL)) { 11305 nd_free(&tcp_g_nd); 11306 return (B_FALSE); 11307 } 11308 if (!nd_load(&tcp_g_nd, "tcp_acceptor_hash", tcp_acceptor_hash_report, 11309 NULL, NULL)) { 11310 nd_free(&tcp_g_nd); 11311 return (B_FALSE); 11312 } 11313 if (!nd_load(&tcp_g_nd, "tcp_host_param", tcp_host_param_report, 11314 tcp_host_param_set, NULL)) { 11315 nd_free(&tcp_g_nd); 11316 return (B_FALSE); 11317 } 11318 if (!nd_load(&tcp_g_nd, "tcp_host_param_ipv6", tcp_host_param_report, 11319 tcp_host_param_set_ipv6, NULL)) { 11320 nd_free(&tcp_g_nd); 11321 return (B_FALSE); 11322 } 11323 if (!nd_load(&tcp_g_nd, "tcp_1948_phrase", NULL, tcp_1948_phrase_set, 11324 NULL)) { 11325 nd_free(&tcp_g_nd); 11326 return (B_FALSE); 11327 } 11328 if (!nd_load(&tcp_g_nd, "tcp_reserved_port_list", 11329 tcp_reserved_port_list, NULL, NULL)) { 11330 nd_free(&tcp_g_nd); 11331 return (B_FALSE); 11332 } 11333 /* 11334 * Dummy ndd variables - only to convey obsolescence information 11335 * through printing of their name (no get or set routines) 11336 * XXX Remove in future releases ? 11337 */ 11338 if (!nd_load(&tcp_g_nd, 11339 "tcp_close_wait_interval(obsoleted - " 11340 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11341 nd_free(&tcp_g_nd); 11342 return (B_FALSE); 11343 } 11344 return (B_TRUE); 11345 } 11346 11347 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11348 /* ARGSUSED */ 11349 static int 11350 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11351 cred_t *cr) 11352 { 11353 long new_value; 11354 tcpparam_t *tcppa = (tcpparam_t *)cp; 11355 11356 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11357 new_value < tcppa->tcp_param_min || 11358 new_value > tcppa->tcp_param_max) { 11359 return (EINVAL); 11360 } 11361 /* 11362 * Need to make sure new_value is a multiple of 4. If it is not, 11363 * round it up. For future 64 bit requirement, we actually make it 11364 * a multiple of 8. 11365 */ 11366 if (new_value & 0x7) { 11367 new_value = (new_value & ~0x7) + 0x8; 11368 } 11369 tcppa->tcp_param_val = new_value; 11370 return (0); 11371 } 11372 11373 /* Set callback routine passed to nd_load by tcp_param_register */ 11374 /* ARGSUSED */ 11375 static int 11376 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11377 { 11378 long new_value; 11379 tcpparam_t *tcppa = (tcpparam_t *)cp; 11380 11381 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11382 new_value < tcppa->tcp_param_min || 11383 new_value > tcppa->tcp_param_max) { 11384 return (EINVAL); 11385 } 11386 tcppa->tcp_param_val = new_value; 11387 return (0); 11388 } 11389 11390 /* 11391 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11392 * is filled, return as much as we can. The message passed in may be 11393 * multi-part, chained using b_cont. "start" is the starting sequence 11394 * number for this piece. 11395 */ 11396 static mblk_t * 11397 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11398 { 11399 uint32_t end; 11400 mblk_t *mp1; 11401 mblk_t *mp2; 11402 mblk_t *next_mp; 11403 uint32_t u1; 11404 11405 /* Walk through all the new pieces. */ 11406 do { 11407 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11408 (uintptr_t)INT_MAX); 11409 end = start + (int)(mp->b_wptr - mp->b_rptr); 11410 next_mp = mp->b_cont; 11411 if (start == end) { 11412 /* Empty. Blast it. */ 11413 freeb(mp); 11414 continue; 11415 } 11416 mp->b_cont = NULL; 11417 TCP_REASS_SET_SEQ(mp, start); 11418 TCP_REASS_SET_END(mp, end); 11419 mp1 = tcp->tcp_reass_tail; 11420 if (!mp1) { 11421 tcp->tcp_reass_tail = mp; 11422 tcp->tcp_reass_head = mp; 11423 BUMP_MIB(&tcp_mib, tcpInDataUnorderSegs); 11424 UPDATE_MIB(&tcp_mib, 11425 tcpInDataUnorderBytes, end - start); 11426 continue; 11427 } 11428 /* New stuff completely beyond tail? */ 11429 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11430 /* Link it on end. */ 11431 mp1->b_cont = mp; 11432 tcp->tcp_reass_tail = mp; 11433 BUMP_MIB(&tcp_mib, tcpInDataUnorderSegs); 11434 UPDATE_MIB(&tcp_mib, 11435 tcpInDataUnorderBytes, end - start); 11436 continue; 11437 } 11438 mp1 = tcp->tcp_reass_head; 11439 u1 = TCP_REASS_SEQ(mp1); 11440 /* New stuff at the front? */ 11441 if (SEQ_LT(start, u1)) { 11442 /* Yes... Check for overlap. */ 11443 mp->b_cont = mp1; 11444 tcp->tcp_reass_head = mp; 11445 tcp_reass_elim_overlap(tcp, mp); 11446 continue; 11447 } 11448 /* 11449 * The new piece fits somewhere between the head and tail. 11450 * We find our slot, where mp1 precedes us and mp2 trails. 11451 */ 11452 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11453 u1 = TCP_REASS_SEQ(mp2); 11454 if (SEQ_LEQ(start, u1)) 11455 break; 11456 } 11457 /* Link ourselves in */ 11458 mp->b_cont = mp2; 11459 mp1->b_cont = mp; 11460 11461 /* Trim overlap with following mblk(s) first */ 11462 tcp_reass_elim_overlap(tcp, mp); 11463 11464 /* Trim overlap with preceding mblk */ 11465 tcp_reass_elim_overlap(tcp, mp1); 11466 11467 } while (start = end, mp = next_mp); 11468 mp1 = tcp->tcp_reass_head; 11469 /* Anything ready to go? */ 11470 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11471 return (NULL); 11472 /* Eat what we can off the queue */ 11473 for (;;) { 11474 mp = mp1->b_cont; 11475 end = TCP_REASS_END(mp1); 11476 TCP_REASS_SET_SEQ(mp1, 0); 11477 TCP_REASS_SET_END(mp1, 0); 11478 if (!mp) { 11479 tcp->tcp_reass_tail = NULL; 11480 break; 11481 } 11482 if (end != TCP_REASS_SEQ(mp)) { 11483 mp1->b_cont = NULL; 11484 break; 11485 } 11486 mp1 = mp; 11487 } 11488 mp1 = tcp->tcp_reass_head; 11489 tcp->tcp_reass_head = mp; 11490 return (mp1); 11491 } 11492 11493 /* Eliminate any overlap that mp may have over later mblks */ 11494 static void 11495 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11496 { 11497 uint32_t end; 11498 mblk_t *mp1; 11499 uint32_t u1; 11500 11501 end = TCP_REASS_END(mp); 11502 while ((mp1 = mp->b_cont) != NULL) { 11503 u1 = TCP_REASS_SEQ(mp1); 11504 if (!SEQ_GT(end, u1)) 11505 break; 11506 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11507 mp->b_wptr -= end - u1; 11508 TCP_REASS_SET_END(mp, u1); 11509 BUMP_MIB(&tcp_mib, tcpInDataPartDupSegs); 11510 UPDATE_MIB(&tcp_mib, tcpInDataPartDupBytes, end - u1); 11511 break; 11512 } 11513 mp->b_cont = mp1->b_cont; 11514 TCP_REASS_SET_SEQ(mp1, 0); 11515 TCP_REASS_SET_END(mp1, 0); 11516 freeb(mp1); 11517 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 11518 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, end - u1); 11519 } 11520 if (!mp1) 11521 tcp->tcp_reass_tail = mp; 11522 } 11523 11524 /* 11525 * Send up all messages queued on tcp_rcv_list. 11526 */ 11527 static uint_t 11528 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11529 { 11530 mblk_t *mp; 11531 uint_t ret = 0; 11532 uint_t thwin; 11533 #ifdef DEBUG 11534 uint_t cnt = 0; 11535 #endif 11536 /* Can't drain on an eager connection */ 11537 if (tcp->tcp_listener != NULL) 11538 return (ret); 11539 11540 /* 11541 * Handle two cases here: we are currently fused or we were 11542 * previously fused and have some urgent data to be delivered 11543 * upstream. The latter happens because we either ran out of 11544 * memory or were detached and therefore sending the SIGURG was 11545 * deferred until this point. In either case we pass control 11546 * over to tcp_fuse_rcv_drain() since it may need to complete 11547 * some work. 11548 */ 11549 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11550 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11551 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11552 &tcp->tcp_fused_sigurg_mp)) 11553 return (ret); 11554 } 11555 11556 while ((mp = tcp->tcp_rcv_list) != NULL) { 11557 tcp->tcp_rcv_list = mp->b_next; 11558 mp->b_next = NULL; 11559 #ifdef DEBUG 11560 cnt += msgdsize(mp); 11561 #endif 11562 /* Does this need SSL processing first? */ 11563 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11564 tcp_kssl_input(tcp, mp); 11565 continue; 11566 } 11567 putnext(q, mp); 11568 } 11569 ASSERT(cnt == tcp->tcp_rcv_cnt); 11570 tcp->tcp_rcv_last_head = NULL; 11571 tcp->tcp_rcv_last_tail = NULL; 11572 tcp->tcp_rcv_cnt = 0; 11573 11574 /* Learn the latest rwnd information that we sent to the other side. */ 11575 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11576 << tcp->tcp_rcv_ws; 11577 /* This is peer's calculated send window (our receive window). */ 11578 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11579 /* 11580 * Increase the receive window to max. But we need to do receiver 11581 * SWS avoidance. This means that we need to check the increase of 11582 * of receive window is at least 1 MSS. 11583 */ 11584 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11585 /* 11586 * If the window that the other side knows is less than max 11587 * deferred acks segments, send an update immediately. 11588 */ 11589 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11590 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 11591 ret = TH_ACK_NEEDED; 11592 } 11593 tcp->tcp_rwnd = q->q_hiwat; 11594 } 11595 /* No need for the push timer now. */ 11596 if (tcp->tcp_push_tid != 0) { 11597 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11598 tcp->tcp_push_tid = 0; 11599 } 11600 return (ret); 11601 } 11602 11603 /* 11604 * Queue data on tcp_rcv_list which is a b_next chain. 11605 * tcp_rcv_last_head/tail is the last element of this chain. 11606 * Each element of the chain is a b_cont chain. 11607 * 11608 * M_DATA messages are added to the current element. 11609 * Other messages are added as new (b_next) elements. 11610 */ 11611 void 11612 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11613 { 11614 ASSERT(seg_len == msgdsize(mp)); 11615 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11616 11617 if (tcp->tcp_rcv_list == NULL) { 11618 ASSERT(tcp->tcp_rcv_last_head == NULL); 11619 tcp->tcp_rcv_list = mp; 11620 tcp->tcp_rcv_last_head = mp; 11621 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11622 tcp->tcp_rcv_last_tail->b_cont = mp; 11623 } else { 11624 tcp->tcp_rcv_last_head->b_next = mp; 11625 tcp->tcp_rcv_last_head = mp; 11626 } 11627 11628 while (mp->b_cont) 11629 mp = mp->b_cont; 11630 11631 tcp->tcp_rcv_last_tail = mp; 11632 tcp->tcp_rcv_cnt += seg_len; 11633 tcp->tcp_rwnd -= seg_len; 11634 } 11635 11636 /* 11637 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11638 * 11639 * This is the default entry function into TCP on the read side. TCP is 11640 * always entered via squeue i.e. using squeue's for mutual exclusion. 11641 * When classifier does a lookup to find the tcp, it also puts a reference 11642 * on the conn structure associated so the tcp is guaranteed to exist 11643 * when we come here. We still need to check the state because it might 11644 * as well has been closed. The squeue processing function i.e. squeue_enter, 11645 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 11646 * CONN_DEC_REF. 11647 * 11648 * Apart from the default entry point, IP also sends packets directly to 11649 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11650 * connections. 11651 */ 11652 void 11653 tcp_input(void *arg, mblk_t *mp, void *arg2) 11654 { 11655 conn_t *connp = (conn_t *)arg; 11656 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11657 11658 /* arg2 is the sqp */ 11659 ASSERT(arg2 != NULL); 11660 ASSERT(mp != NULL); 11661 11662 /* 11663 * Don't accept any input on a closed tcp as this TCP logically does 11664 * not exist on the system. Don't proceed further with this TCP. 11665 * For eg. this packet could trigger another close of this tcp 11666 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11667 * tcp_clean_death / tcp_closei_local must be called at most once 11668 * on a TCP. In this case we need to refeed the packet into the 11669 * classifier and figure out where the packet should go. Need to 11670 * preserve the recv_ill somehow. Until we figure that out, for 11671 * now just drop the packet if we can't classify the packet. 11672 */ 11673 if (tcp->tcp_state == TCPS_CLOSED || 11674 tcp->tcp_state == TCPS_BOUND) { 11675 conn_t *new_connp; 11676 11677 new_connp = ipcl_classify(mp, connp->conn_zoneid); 11678 if (new_connp != NULL) { 11679 tcp_reinput(new_connp, mp, arg2); 11680 return; 11681 } 11682 /* We failed to classify. For now just drop the packet */ 11683 freemsg(mp); 11684 return; 11685 } 11686 11687 if (DB_TYPE(mp) == M_DATA) 11688 tcp_rput_data(connp, mp, arg2); 11689 else 11690 tcp_rput_common(tcp, mp); 11691 } 11692 11693 /* 11694 * The read side put procedure. 11695 * The packets passed up by ip are assume to be aligned according to 11696 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11697 */ 11698 static void 11699 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11700 { 11701 /* 11702 * tcp_rput_data() does not expect M_CTL except for the case 11703 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11704 * type. Need to make sure that any other M_CTLs don't make 11705 * it to tcp_rput_data since it is not expecting any and doesn't 11706 * check for it. 11707 */ 11708 if (DB_TYPE(mp) == M_CTL) { 11709 switch (*(uint32_t *)(mp->b_rptr)) { 11710 case TCP_IOC_ABORT_CONN: 11711 /* 11712 * Handle connection abort request. 11713 */ 11714 tcp_ioctl_abort_handler(tcp, mp); 11715 return; 11716 case IPSEC_IN: 11717 /* 11718 * Only secure icmp arrive in TCP and they 11719 * don't go through data path. 11720 */ 11721 tcp_icmp_error(tcp, mp); 11722 return; 11723 case IN_PKTINFO: 11724 /* 11725 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11726 * sockets that are receiving IPv4 traffic. tcp 11727 */ 11728 ASSERT(tcp->tcp_family == AF_INET6); 11729 ASSERT(tcp->tcp_ipv6_recvancillary & 11730 TCP_IPV6_RECVPKTINFO); 11731 tcp_rput_data(tcp->tcp_connp, mp, 11732 tcp->tcp_connp->conn_sqp); 11733 return; 11734 case MDT_IOC_INFO_UPDATE: 11735 /* 11736 * Handle Multidata information update; the 11737 * following routine will free the message. 11738 */ 11739 if (tcp->tcp_connp->conn_mdt_ok) { 11740 tcp_mdt_update(tcp, 11741 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11742 B_FALSE); 11743 } 11744 freemsg(mp); 11745 return; 11746 default: 11747 break; 11748 } 11749 } 11750 11751 /* No point processing the message if tcp is already closed */ 11752 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11753 freemsg(mp); 11754 return; 11755 } 11756 11757 tcp_rput_other(tcp, mp); 11758 } 11759 11760 11761 /* The minimum of smoothed mean deviation in RTO calculation. */ 11762 #define TCP_SD_MIN 400 11763 11764 /* 11765 * Set RTO for this connection. The formula is from Jacobson and Karels' 11766 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11767 * are the same as those in Appendix A.2 of that paper. 11768 * 11769 * m = new measurement 11770 * sa = smoothed RTT average (8 * average estimates). 11771 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11772 */ 11773 static void 11774 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11775 { 11776 long m = TICK_TO_MSEC(rtt); 11777 clock_t sa = tcp->tcp_rtt_sa; 11778 clock_t sv = tcp->tcp_rtt_sd; 11779 clock_t rto; 11780 11781 BUMP_MIB(&tcp_mib, tcpRttUpdate); 11782 tcp->tcp_rtt_update++; 11783 11784 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11785 if (sa != 0) { 11786 /* 11787 * Update average estimator: 11788 * new rtt = 7/8 old rtt + 1/8 Error 11789 */ 11790 11791 /* m is now Error in estimate. */ 11792 m -= sa >> 3; 11793 if ((sa += m) <= 0) { 11794 /* 11795 * Don't allow the smoothed average to be negative. 11796 * We use 0 to denote reinitialization of the 11797 * variables. 11798 */ 11799 sa = 1; 11800 } 11801 11802 /* 11803 * Update deviation estimator: 11804 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11805 */ 11806 if (m < 0) 11807 m = -m; 11808 m -= sv >> 2; 11809 sv += m; 11810 } else { 11811 /* 11812 * This follows BSD's implementation. So the reinitialized 11813 * RTO is 3 * m. We cannot go less than 2 because if the 11814 * link is bandwidth dominated, doubling the window size 11815 * during slow start means doubling the RTT. We want to be 11816 * more conservative when we reinitialize our estimates. 3 11817 * is just a convenient number. 11818 */ 11819 sa = m << 3; 11820 sv = m << 1; 11821 } 11822 if (sv < TCP_SD_MIN) { 11823 /* 11824 * We do not know that if sa captures the delay ACK 11825 * effect as in a long train of segments, a receiver 11826 * does not delay its ACKs. So set the minimum of sv 11827 * to be TCP_SD_MIN, which is default to 400 ms, twice 11828 * of BSD DATO. That means the minimum of mean 11829 * deviation is 100 ms. 11830 * 11831 */ 11832 sv = TCP_SD_MIN; 11833 } 11834 tcp->tcp_rtt_sa = sa; 11835 tcp->tcp_rtt_sd = sv; 11836 /* 11837 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11838 * 11839 * Add tcp_rexmit_interval extra in case of extreme environment 11840 * where the algorithm fails to work. The default value of 11841 * tcp_rexmit_interval_extra should be 0. 11842 * 11843 * As we use a finer grained clock than BSD and update 11844 * RTO for every ACKs, add in another .25 of RTT to the 11845 * deviation of RTO to accomodate burstiness of 1/4 of 11846 * window size. 11847 */ 11848 rto = (sa >> 3) + sv + tcp_rexmit_interval_extra + (sa >> 5); 11849 11850 if (rto > tcp_rexmit_interval_max) { 11851 tcp->tcp_rto = tcp_rexmit_interval_max; 11852 } else if (rto < tcp_rexmit_interval_min) { 11853 tcp->tcp_rto = tcp_rexmit_interval_min; 11854 } else { 11855 tcp->tcp_rto = rto; 11856 } 11857 11858 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11859 tcp->tcp_timer_backoff = 0; 11860 } 11861 11862 /* 11863 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11864 * send queue which starts at the given seq. no. 11865 * 11866 * Parameters: 11867 * tcp_t *tcp: the tcp instance pointer. 11868 * uint32_t seq: the starting seq. no of the requested segment. 11869 * int32_t *off: after the execution, *off will be the offset to 11870 * the returned mblk which points to the requested seq no. 11871 * It is the caller's responsibility to send in a non-null off. 11872 * 11873 * Return: 11874 * A mblk_t pointer pointing to the requested segment in send queue. 11875 */ 11876 static mblk_t * 11877 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11878 { 11879 int32_t cnt; 11880 mblk_t *mp; 11881 11882 /* Defensive coding. Make sure we don't send incorrect data. */ 11883 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 11884 return (NULL); 11885 11886 cnt = seq - tcp->tcp_suna; 11887 mp = tcp->tcp_xmit_head; 11888 while (cnt > 0 && mp != NULL) { 11889 cnt -= mp->b_wptr - mp->b_rptr; 11890 if (cnt < 0) { 11891 cnt += mp->b_wptr - mp->b_rptr; 11892 break; 11893 } 11894 mp = mp->b_cont; 11895 } 11896 ASSERT(mp != NULL); 11897 *off = cnt; 11898 return (mp); 11899 } 11900 11901 /* 11902 * This function handles all retransmissions if SACK is enabled for this 11903 * connection. First it calculates how many segments can be retransmitted 11904 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11905 * segments. A segment is eligible if sack_cnt for that segment is greater 11906 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11907 * all eligible segments, it checks to see if TCP can send some new segments 11908 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11909 * 11910 * Parameters: 11911 * tcp_t *tcp: the tcp structure of the connection. 11912 * uint_t *flags: in return, appropriate value will be set for 11913 * tcp_rput_data(). 11914 */ 11915 static void 11916 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11917 { 11918 notsack_blk_t *notsack_blk; 11919 int32_t usable_swnd; 11920 int32_t mss; 11921 uint32_t seg_len; 11922 mblk_t *xmit_mp; 11923 11924 ASSERT(tcp->tcp_sack_info != NULL); 11925 ASSERT(tcp->tcp_notsack_list != NULL); 11926 ASSERT(tcp->tcp_rexmit == B_FALSE); 11927 11928 /* Defensive coding in case there is a bug... */ 11929 if (tcp->tcp_notsack_list == NULL) { 11930 return; 11931 } 11932 notsack_blk = tcp->tcp_notsack_list; 11933 mss = tcp->tcp_mss; 11934 11935 /* 11936 * Limit the num of outstanding data in the network to be 11937 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11938 */ 11939 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11940 11941 /* At least retransmit 1 MSS of data. */ 11942 if (usable_swnd <= 0) { 11943 usable_swnd = mss; 11944 } 11945 11946 /* Make sure no new RTT samples will be taken. */ 11947 tcp->tcp_csuna = tcp->tcp_snxt; 11948 11949 notsack_blk = tcp->tcp_notsack_list; 11950 while (usable_swnd > 0) { 11951 mblk_t *snxt_mp, *tmp_mp; 11952 tcp_seq begin = tcp->tcp_sack_snxt; 11953 tcp_seq end; 11954 int32_t off; 11955 11956 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11957 if (SEQ_GT(notsack_blk->end, begin) && 11958 (notsack_blk->sack_cnt >= 11959 tcp_dupack_fast_retransmit)) { 11960 end = notsack_blk->end; 11961 if (SEQ_LT(begin, notsack_blk->begin)) { 11962 begin = notsack_blk->begin; 11963 } 11964 break; 11965 } 11966 } 11967 /* 11968 * All holes are filled. Manipulate tcp_cwnd to send more 11969 * if we can. Note that after the SACK recovery, tcp_cwnd is 11970 * set to tcp_cwnd_ssthresh. 11971 */ 11972 if (notsack_blk == NULL) { 11973 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11974 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11975 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11976 ASSERT(tcp->tcp_cwnd > 0); 11977 return; 11978 } else { 11979 usable_swnd = usable_swnd / mss; 11980 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 11981 MAX(usable_swnd * mss, mss); 11982 *flags |= TH_XMIT_NEEDED; 11983 return; 11984 } 11985 } 11986 11987 /* 11988 * Note that we may send more than usable_swnd allows here 11989 * because of round off, but no more than 1 MSS of data. 11990 */ 11991 seg_len = end - begin; 11992 if (seg_len > mss) 11993 seg_len = mss; 11994 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 11995 ASSERT(snxt_mp != NULL); 11996 /* This should not happen. Defensive coding again... */ 11997 if (snxt_mp == NULL) { 11998 return; 11999 } 12000 12001 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12002 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12003 if (xmit_mp == NULL) 12004 return; 12005 12006 usable_swnd -= seg_len; 12007 tcp->tcp_pipe += seg_len; 12008 tcp->tcp_sack_snxt = begin + seg_len; 12009 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 12010 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12011 12012 /* 12013 * Update the send timestamp to avoid false retransmission. 12014 */ 12015 snxt_mp->b_prev = (mblk_t *)lbolt; 12016 12017 BUMP_MIB(&tcp_mib, tcpRetransSegs); 12018 UPDATE_MIB(&tcp_mib, tcpRetransBytes, seg_len); 12019 BUMP_MIB(&tcp_mib, tcpOutSackRetransSegs); 12020 /* 12021 * Update tcp_rexmit_max to extend this SACK recovery phase. 12022 * This happens when new data sent during fast recovery is 12023 * also lost. If TCP retransmits those new data, it needs 12024 * to extend SACK recover phase to avoid starting another 12025 * fast retransmit/recovery unnecessarily. 12026 */ 12027 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12028 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12029 } 12030 } 12031 } 12032 12033 /* 12034 * This function handles policy checking at TCP level for non-hard_bound/ 12035 * detached connections. 12036 */ 12037 static boolean_t 12038 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12039 boolean_t secure, boolean_t mctl_present) 12040 { 12041 ipsec_latch_t *ipl = NULL; 12042 ipsec_action_t *act = NULL; 12043 mblk_t *data_mp; 12044 ipsec_in_t *ii; 12045 const char *reason; 12046 kstat_named_t *counter; 12047 12048 ASSERT(mctl_present || !secure); 12049 12050 ASSERT((ipha == NULL && ip6h != NULL) || 12051 (ip6h == NULL && ipha != NULL)); 12052 12053 /* 12054 * We don't necessarily have an ipsec_in_act action to verify 12055 * policy because of assymetrical policy where we have only 12056 * outbound policy and no inbound policy (possible with global 12057 * policy). 12058 */ 12059 if (!secure) { 12060 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12061 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12062 return (B_TRUE); 12063 ipsec_log_policy_failure(tcp->tcp_wq, IPSEC_POLICY_MISMATCH, 12064 "tcp_check_policy", ipha, ip6h, secure); 12065 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12066 &ipdrops_tcp_clear, &tcp_dropper); 12067 return (B_FALSE); 12068 } 12069 12070 /* 12071 * We have a secure packet. 12072 */ 12073 if (act == NULL) { 12074 ipsec_log_policy_failure(tcp->tcp_wq, 12075 IPSEC_POLICY_NOT_NEEDED, "tcp_check_policy", ipha, ip6h, 12076 secure); 12077 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12078 &ipdrops_tcp_secure, &tcp_dropper); 12079 return (B_FALSE); 12080 } 12081 12082 /* 12083 * XXX This whole routine is currently incorrect. ipl should 12084 * be set to the latch pointer, but is currently not set, so 12085 * we initialize it to NULL to avoid picking up random garbage. 12086 */ 12087 if (ipl == NULL) 12088 return (B_TRUE); 12089 12090 data_mp = first_mp->b_cont; 12091 12092 ii = (ipsec_in_t *)first_mp->b_rptr; 12093 12094 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12095 &counter, tcp->tcp_connp)) { 12096 BUMP_MIB(&ip_mib, ipsecInSucceeded); 12097 return (B_TRUE); 12098 } 12099 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12100 "tcp inbound policy mismatch: %s, packet dropped\n", 12101 reason); 12102 BUMP_MIB(&ip_mib, ipsecInFailed); 12103 12104 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, &tcp_dropper); 12105 return (B_FALSE); 12106 } 12107 12108 /* 12109 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12110 * retransmission after a timeout. 12111 * 12112 * To limit the number of duplicate segments, we limit the number of segment 12113 * to be sent in one time to tcp_snd_burst, the burst variable. 12114 */ 12115 static void 12116 tcp_ss_rexmit(tcp_t *tcp) 12117 { 12118 uint32_t snxt; 12119 uint32_t smax; 12120 int32_t win; 12121 int32_t mss; 12122 int32_t off; 12123 int32_t burst = tcp->tcp_snd_burst; 12124 mblk_t *snxt_mp; 12125 12126 /* 12127 * Note that tcp_rexmit can be set even though TCP has retransmitted 12128 * all unack'ed segments. 12129 */ 12130 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12131 smax = tcp->tcp_rexmit_max; 12132 snxt = tcp->tcp_rexmit_nxt; 12133 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12134 snxt = tcp->tcp_suna; 12135 } 12136 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12137 win -= snxt - tcp->tcp_suna; 12138 mss = tcp->tcp_mss; 12139 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12140 12141 while (SEQ_LT(snxt, smax) && (win > 0) && 12142 (burst > 0) && (snxt_mp != NULL)) { 12143 mblk_t *xmit_mp; 12144 mblk_t *old_snxt_mp = snxt_mp; 12145 uint32_t cnt = mss; 12146 12147 if (win < cnt) { 12148 cnt = win; 12149 } 12150 if (SEQ_GT(snxt + cnt, smax)) { 12151 cnt = smax - snxt; 12152 } 12153 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12154 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12155 if (xmit_mp == NULL) 12156 return; 12157 12158 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12159 12160 snxt += cnt; 12161 win -= cnt; 12162 /* 12163 * Update the send timestamp to avoid false 12164 * retransmission. 12165 */ 12166 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12167 BUMP_MIB(&tcp_mib, tcpRetransSegs); 12168 UPDATE_MIB(&tcp_mib, tcpRetransBytes, cnt); 12169 12170 tcp->tcp_rexmit_nxt = snxt; 12171 burst--; 12172 } 12173 /* 12174 * If we have transmitted all we have at the time 12175 * we started the retranmission, we can leave 12176 * the rest of the job to tcp_wput_data(). But we 12177 * need to check the send window first. If the 12178 * win is not 0, go on with tcp_wput_data(). 12179 */ 12180 if (SEQ_LT(snxt, smax) || win == 0) { 12181 return; 12182 } 12183 } 12184 /* Only call tcp_wput_data() if there is data to be sent. */ 12185 if (tcp->tcp_unsent) { 12186 tcp_wput_data(tcp, NULL, B_FALSE); 12187 } 12188 } 12189 12190 /* 12191 * Process all TCP option in SYN segment. Note that this function should 12192 * be called after tcp_adapt_ire() is called so that the necessary info 12193 * from IRE is already set in the tcp structure. 12194 * 12195 * This function sets up the correct tcp_mss value according to the 12196 * MSS option value and our header size. It also sets up the window scale 12197 * and timestamp values, and initialize SACK info blocks. But it does not 12198 * change receive window size after setting the tcp_mss value. The caller 12199 * should do the appropriate change. 12200 */ 12201 void 12202 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12203 { 12204 int options; 12205 tcp_opt_t tcpopt; 12206 uint32_t mss_max; 12207 char *tmp_tcph; 12208 12209 tcpopt.tcp = NULL; 12210 options = tcp_parse_options(tcph, &tcpopt); 12211 12212 /* 12213 * Process MSS option. Note that MSS option value does not account 12214 * for IP or TCP options. This means that it is equal to MTU - minimum 12215 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12216 * IPv6. 12217 */ 12218 if (!(options & TCP_OPT_MSS_PRESENT)) { 12219 if (tcp->tcp_ipversion == IPV4_VERSION) 12220 tcpopt.tcp_opt_mss = tcp_mss_def_ipv4; 12221 else 12222 tcpopt.tcp_opt_mss = tcp_mss_def_ipv6; 12223 } else { 12224 if (tcp->tcp_ipversion == IPV4_VERSION) 12225 mss_max = tcp_mss_max_ipv4; 12226 else 12227 mss_max = tcp_mss_max_ipv6; 12228 if (tcpopt.tcp_opt_mss < tcp_mss_min) 12229 tcpopt.tcp_opt_mss = tcp_mss_min; 12230 else if (tcpopt.tcp_opt_mss > mss_max) 12231 tcpopt.tcp_opt_mss = mss_max; 12232 } 12233 12234 /* Process Window Scale option. */ 12235 if (options & TCP_OPT_WSCALE_PRESENT) { 12236 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12237 tcp->tcp_snd_ws_ok = B_TRUE; 12238 } else { 12239 tcp->tcp_snd_ws = B_FALSE; 12240 tcp->tcp_snd_ws_ok = B_FALSE; 12241 tcp->tcp_rcv_ws = B_FALSE; 12242 } 12243 12244 /* Process Timestamp option. */ 12245 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12246 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12247 tmp_tcph = (char *)tcp->tcp_tcph; 12248 12249 tcp->tcp_snd_ts_ok = B_TRUE; 12250 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12251 tcp->tcp_last_rcv_lbolt = lbolt64; 12252 ASSERT(OK_32PTR(tmp_tcph)); 12253 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12254 12255 /* Fill in our template header with basic timestamp option. */ 12256 tmp_tcph += tcp->tcp_tcp_hdr_len; 12257 tmp_tcph[0] = TCPOPT_NOP; 12258 tmp_tcph[1] = TCPOPT_NOP; 12259 tmp_tcph[2] = TCPOPT_TSTAMP; 12260 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12261 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12262 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12263 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12264 } else { 12265 tcp->tcp_snd_ts_ok = B_FALSE; 12266 } 12267 12268 /* 12269 * Process SACK options. If SACK is enabled for this connection, 12270 * then allocate the SACK info structure. Note the following ways 12271 * when tcp_snd_sack_ok is set to true. 12272 * 12273 * For active connection: in tcp_adapt_ire() called in 12274 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12275 * is checked. 12276 * 12277 * For passive connection: in tcp_adapt_ire() called in 12278 * tcp_accept_comm(). 12279 * 12280 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12281 * That check makes sure that if we did not send a SACK OK option, 12282 * we will not enable SACK for this connection even though the other 12283 * side sends us SACK OK option. For active connection, the SACK 12284 * info structure has already been allocated. So we need to free 12285 * it if SACK is disabled. 12286 */ 12287 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12288 (tcp->tcp_snd_sack_ok || 12289 (tcp_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12290 /* This should be true only in the passive case. */ 12291 if (tcp->tcp_sack_info == NULL) { 12292 ASSERT(TCP_IS_DETACHED(tcp)); 12293 tcp->tcp_sack_info = 12294 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12295 } 12296 if (tcp->tcp_sack_info == NULL) { 12297 tcp->tcp_snd_sack_ok = B_FALSE; 12298 } else { 12299 tcp->tcp_snd_sack_ok = B_TRUE; 12300 if (tcp->tcp_snd_ts_ok) { 12301 tcp->tcp_max_sack_blk = 3; 12302 } else { 12303 tcp->tcp_max_sack_blk = 4; 12304 } 12305 } 12306 } else { 12307 /* 12308 * Resetting tcp_snd_sack_ok to B_FALSE so that 12309 * no SACK info will be used for this 12310 * connection. This assumes that SACK usage 12311 * permission is negotiated. This may need 12312 * to be changed once this is clarified. 12313 */ 12314 if (tcp->tcp_sack_info != NULL) { 12315 ASSERT(tcp->tcp_notsack_list == NULL); 12316 kmem_cache_free(tcp_sack_info_cache, 12317 tcp->tcp_sack_info); 12318 tcp->tcp_sack_info = NULL; 12319 } 12320 tcp->tcp_snd_sack_ok = B_FALSE; 12321 } 12322 12323 /* 12324 * Now we know the exact TCP/IP header length, subtract 12325 * that from tcp_mss to get our side's MSS. 12326 */ 12327 tcp->tcp_mss -= tcp->tcp_hdr_len; 12328 /* 12329 * Here we assume that the other side's header size will be equal to 12330 * our header size. We calculate the real MSS accordingly. Need to 12331 * take into additional stuffs IPsec puts in. 12332 * 12333 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12334 */ 12335 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12336 ((tcp->tcp_ipversion == IPV4_VERSION ? 12337 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12338 12339 /* 12340 * Set MSS to the smaller one of both ends of the connection. 12341 * We should not have called tcp_mss_set() before, but our 12342 * side of the MSS should have been set to a proper value 12343 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12344 * STREAM head parameters properly. 12345 * 12346 * If we have a larger-than-16-bit window but the other side 12347 * didn't want to do window scale, tcp_rwnd_set() will take 12348 * care of that. 12349 */ 12350 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss)); 12351 } 12352 12353 /* 12354 * Sends the T_CONN_IND to the listener. The caller calls this 12355 * functions via squeue to get inside the listener's perimeter 12356 * once the 3 way hand shake is done a T_CONN_IND needs to be 12357 * sent. As an optimization, the caller can call this directly 12358 * if listener's perimeter is same as eager's. 12359 */ 12360 /* ARGSUSED */ 12361 void 12362 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12363 { 12364 conn_t *lconnp = (conn_t *)arg; 12365 tcp_t *listener = lconnp->conn_tcp; 12366 tcp_t *tcp; 12367 struct T_conn_ind *conn_ind; 12368 ipaddr_t *addr_cache; 12369 boolean_t need_send_conn_ind = B_FALSE; 12370 12371 /* retrieve the eager */ 12372 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12373 ASSERT(conn_ind->OPT_offset != 0 && 12374 conn_ind->OPT_length == sizeof (intptr_t)); 12375 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12376 conn_ind->OPT_length); 12377 12378 /* 12379 * TLI/XTI applications will get confused by 12380 * sending eager as an option since it violates 12381 * the option semantics. So remove the eager as 12382 * option since TLI/XTI app doesn't need it anyway. 12383 */ 12384 if (!TCP_IS_SOCKET(listener)) { 12385 conn_ind->OPT_length = 0; 12386 conn_ind->OPT_offset = 0; 12387 } 12388 if (listener->tcp_state == TCPS_CLOSED || 12389 TCP_IS_DETACHED(listener)) { 12390 /* 12391 * If listener has closed, it would have caused a 12392 * a cleanup/blowoff to happen for the eager. We 12393 * just need to return. 12394 */ 12395 freemsg(mp); 12396 return; 12397 } 12398 12399 12400 /* 12401 * if the conn_req_q is full defer passing up the 12402 * T_CONN_IND until space is availabe after t_accept() 12403 * processing 12404 */ 12405 mutex_enter(&listener->tcp_eager_lock); 12406 12407 /* 12408 * Take the eager out, if it is in the list of droppable eagers 12409 * as we are here because the 3W handshake is over. 12410 */ 12411 MAKE_UNDROPPABLE(tcp); 12412 12413 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12414 tcp_t *tail; 12415 12416 /* 12417 * The eager already has an extra ref put in tcp_rput_data 12418 * so that it stays till accept comes back even though it 12419 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12420 */ 12421 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12422 listener->tcp_conn_req_cnt_q0--; 12423 listener->tcp_conn_req_cnt_q++; 12424 12425 /* Move from SYN_RCVD to ESTABLISHED list */ 12426 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12427 tcp->tcp_eager_prev_q0; 12428 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12429 tcp->tcp_eager_next_q0; 12430 tcp->tcp_eager_prev_q0 = NULL; 12431 tcp->tcp_eager_next_q0 = NULL; 12432 12433 /* 12434 * Insert at end of the queue because sockfs 12435 * sends down T_CONN_RES in chronological 12436 * order. Leaving the older conn indications 12437 * at front of the queue helps reducing search 12438 * time. 12439 */ 12440 tail = listener->tcp_eager_last_q; 12441 if (tail != NULL) 12442 tail->tcp_eager_next_q = tcp; 12443 else 12444 listener->tcp_eager_next_q = tcp; 12445 listener->tcp_eager_last_q = tcp; 12446 tcp->tcp_eager_next_q = NULL; 12447 /* 12448 * Delay sending up the T_conn_ind until we are 12449 * done with the eager. Once we have have sent up 12450 * the T_conn_ind, the accept can potentially complete 12451 * any time and release the refhold we have on the eager. 12452 */ 12453 need_send_conn_ind = B_TRUE; 12454 } else { 12455 /* 12456 * Defer connection on q0 and set deferred 12457 * connection bit true 12458 */ 12459 tcp->tcp_conn_def_q0 = B_TRUE; 12460 12461 /* take tcp out of q0 ... */ 12462 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12463 tcp->tcp_eager_next_q0; 12464 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12465 tcp->tcp_eager_prev_q0; 12466 12467 /* ... and place it at the end of q0 */ 12468 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12469 tcp->tcp_eager_next_q0 = listener; 12470 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12471 listener->tcp_eager_prev_q0 = tcp; 12472 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12473 } 12474 12475 /* we have timed out before */ 12476 if (tcp->tcp_syn_rcvd_timeout != 0) { 12477 tcp->tcp_syn_rcvd_timeout = 0; 12478 listener->tcp_syn_rcvd_timeout--; 12479 if (listener->tcp_syn_defense && 12480 listener->tcp_syn_rcvd_timeout <= 12481 (tcp_conn_req_max_q0 >> 5) && 12482 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12483 listener->tcp_last_rcv_lbolt)) { 12484 /* 12485 * Turn off the defense mode if we 12486 * believe the SYN attack is over. 12487 */ 12488 listener->tcp_syn_defense = B_FALSE; 12489 if (listener->tcp_ip_addr_cache) { 12490 kmem_free((void *)listener->tcp_ip_addr_cache, 12491 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12492 listener->tcp_ip_addr_cache = NULL; 12493 } 12494 } 12495 } 12496 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12497 if (addr_cache != NULL) { 12498 /* 12499 * We have finished a 3-way handshake with this 12500 * remote host. This proves the IP addr is good. 12501 * Cache it! 12502 */ 12503 addr_cache[IP_ADDR_CACHE_HASH( 12504 tcp->tcp_remote)] = tcp->tcp_remote; 12505 } 12506 mutex_exit(&listener->tcp_eager_lock); 12507 if (need_send_conn_ind) 12508 putnext(listener->tcp_rq, mp); 12509 } 12510 12511 mblk_t * 12512 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12513 uint_t *ifindexp, ip6_pkt_t *ippp) 12514 { 12515 in_pktinfo_t *pinfo; 12516 ip6_t *ip6h; 12517 uchar_t *rptr; 12518 mblk_t *first_mp = mp; 12519 boolean_t mctl_present = B_FALSE; 12520 uint_t ifindex = 0; 12521 ip6_pkt_t ipp; 12522 uint_t ipvers; 12523 uint_t ip_hdr_len; 12524 12525 rptr = mp->b_rptr; 12526 ASSERT(OK_32PTR(rptr)); 12527 ASSERT(tcp != NULL); 12528 ipp.ipp_fields = 0; 12529 12530 switch DB_TYPE(mp) { 12531 case M_CTL: 12532 mp = mp->b_cont; 12533 if (mp == NULL) { 12534 freemsg(first_mp); 12535 return (NULL); 12536 } 12537 if (DB_TYPE(mp) != M_DATA) { 12538 freemsg(first_mp); 12539 return (NULL); 12540 } 12541 mctl_present = B_TRUE; 12542 break; 12543 case M_DATA: 12544 break; 12545 default: 12546 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12547 freemsg(mp); 12548 return (NULL); 12549 } 12550 ipvers = IPH_HDR_VERSION(rptr); 12551 if (ipvers == IPV4_VERSION) { 12552 if (tcp == NULL) { 12553 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12554 goto done; 12555 } 12556 12557 ipp.ipp_fields |= IPPF_HOPLIMIT; 12558 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12559 12560 /* 12561 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12562 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12563 */ 12564 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12565 mctl_present) { 12566 pinfo = (in_pktinfo_t *)first_mp->b_rptr; 12567 if ((MBLKL(first_mp) == sizeof (in_pktinfo_t)) && 12568 (pinfo->in_pkt_ulp_type == IN_PKTINFO) && 12569 (pinfo->in_pkt_flags & IPF_RECVIF)) { 12570 ipp.ipp_fields |= IPPF_IFINDEX; 12571 ipp.ipp_ifindex = pinfo->in_pkt_ifindex; 12572 ifindex = pinfo->in_pkt_ifindex; 12573 } 12574 freeb(first_mp); 12575 mctl_present = B_FALSE; 12576 } 12577 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12578 } else { 12579 ip6h = (ip6_t *)rptr; 12580 12581 ASSERT(ipvers == IPV6_VERSION); 12582 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12583 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12584 ipp.ipp_hoplimit = ip6h->ip6_hops; 12585 12586 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12587 uint8_t nexthdrp; 12588 12589 /* Look for ifindex information */ 12590 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12591 ip6i_t *ip6i = (ip6i_t *)ip6h; 12592 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12593 BUMP_MIB(&ip_mib, tcpInErrs); 12594 freemsg(first_mp); 12595 return (NULL); 12596 } 12597 12598 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12599 ASSERT(ip6i->ip6i_ifindex != 0); 12600 ipp.ipp_fields |= IPPF_IFINDEX; 12601 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12602 ifindex = ip6i->ip6i_ifindex; 12603 } 12604 rptr = (uchar_t *)&ip6i[1]; 12605 mp->b_rptr = rptr; 12606 if (rptr == mp->b_wptr) { 12607 mblk_t *mp1; 12608 mp1 = mp->b_cont; 12609 freeb(mp); 12610 mp = mp1; 12611 rptr = mp->b_rptr; 12612 } 12613 if (MBLKL(mp) < IPV6_HDR_LEN + 12614 sizeof (tcph_t)) { 12615 BUMP_MIB(&ip_mib, tcpInErrs); 12616 freemsg(first_mp); 12617 return (NULL); 12618 } 12619 ip6h = (ip6_t *)rptr; 12620 } 12621 12622 /* 12623 * Find any potentially interesting extension headers 12624 * as well as the length of the IPv6 + extension 12625 * headers. 12626 */ 12627 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12628 /* Verify if this is a TCP packet */ 12629 if (nexthdrp != IPPROTO_TCP) { 12630 BUMP_MIB(&ip_mib, tcpInErrs); 12631 freemsg(first_mp); 12632 return (NULL); 12633 } 12634 } else { 12635 ip_hdr_len = IPV6_HDR_LEN; 12636 } 12637 } 12638 12639 done: 12640 if (ipversp != NULL) 12641 *ipversp = ipvers; 12642 if (ip_hdr_lenp != NULL) 12643 *ip_hdr_lenp = ip_hdr_len; 12644 if (ippp != NULL) 12645 *ippp = ipp; 12646 if (ifindexp != NULL) 12647 *ifindexp = ifindex; 12648 if (mctl_present) { 12649 freeb(first_mp); 12650 } 12651 return (mp); 12652 } 12653 12654 /* 12655 * Handle M_DATA messages from IP. Its called directly from IP via 12656 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12657 * in this path. 12658 * 12659 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12660 * v4 and v6), we are called through tcp_input() and a M_CTL can 12661 * be present for options but tcp_find_pktinfo() deals with it. We 12662 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12663 * 12664 * The first argument is always the connp/tcp to which the mp belongs. 12665 * There are no exceptions to this rule. The caller has already put 12666 * a reference on this connp/tcp and once tcp_rput_data() returns, 12667 * the squeue will do the refrele. 12668 * 12669 * The TH_SYN for the listener directly go to tcp_conn_request via 12670 * squeue. 12671 * 12672 * sqp: NULL = recursive, sqp != NULL means called from squeue 12673 */ 12674 void 12675 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12676 { 12677 int32_t bytes_acked; 12678 int32_t gap; 12679 mblk_t *mp1; 12680 uint_t flags; 12681 uint32_t new_swnd = 0; 12682 uchar_t *iphdr; 12683 uchar_t *rptr; 12684 int32_t rgap; 12685 uint32_t seg_ack; 12686 int seg_len; 12687 uint_t ip_hdr_len; 12688 uint32_t seg_seq; 12689 tcph_t *tcph; 12690 int urp; 12691 tcp_opt_t tcpopt; 12692 uint_t ipvers; 12693 ip6_pkt_t ipp; 12694 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12695 uint32_t cwnd; 12696 uint32_t add; 12697 int npkt; 12698 int mss; 12699 conn_t *connp = (conn_t *)arg; 12700 squeue_t *sqp = (squeue_t *)arg2; 12701 tcp_t *tcp = connp->conn_tcp; 12702 12703 /* 12704 * RST from fused tcp loopback peer should trigger an unfuse. 12705 */ 12706 if (tcp->tcp_fused) { 12707 TCP_STAT(tcp_fusion_aborted); 12708 tcp_unfuse(tcp); 12709 } 12710 12711 iphdr = mp->b_rptr; 12712 rptr = mp->b_rptr; 12713 ASSERT(OK_32PTR(rptr)); 12714 12715 /* 12716 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12717 * processing here. For rest call tcp_find_pktinfo to fill up the 12718 * necessary information. 12719 */ 12720 if (IPCL_IS_TCP4(connp)) { 12721 ipvers = IPV4_VERSION; 12722 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12723 } else { 12724 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12725 NULL, &ipp); 12726 if (mp == NULL) { 12727 TCP_STAT(tcp_rput_v6_error); 12728 return; 12729 } 12730 iphdr = mp->b_rptr; 12731 rptr = mp->b_rptr; 12732 } 12733 ASSERT(DB_TYPE(mp) == M_DATA); 12734 12735 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12736 seg_seq = ABE32_TO_U32(tcph->th_seq); 12737 seg_ack = ABE32_TO_U32(tcph->th_ack); 12738 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12739 seg_len = (int)(mp->b_wptr - rptr) - 12740 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12741 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12742 do { 12743 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12744 (uintptr_t)INT_MAX); 12745 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12746 } while ((mp1 = mp1->b_cont) != NULL && 12747 mp1->b_datap->db_type == M_DATA); 12748 } 12749 12750 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12751 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12752 seg_len, tcph); 12753 return; 12754 } 12755 12756 if (sqp != NULL) { 12757 /* 12758 * This is the correct place to update tcp_last_recv_time. Note 12759 * that it is also updated for tcp structure that belongs to 12760 * global and listener queues which do not really need updating. 12761 * But that should not cause any harm. And it is updated for 12762 * all kinds of incoming segments, not only for data segments. 12763 */ 12764 tcp->tcp_last_recv_time = lbolt; 12765 } 12766 12767 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12768 12769 BUMP_LOCAL(tcp->tcp_ibsegs); 12770 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 12771 12772 if ((flags & TH_URG) && sqp != NULL) { 12773 /* 12774 * TCP can't handle urgent pointers that arrive before 12775 * the connection has been accept()ed since it can't 12776 * buffer OOB data. Discard segment if this happens. 12777 * 12778 * Nor can it reassemble urgent pointers, so discard 12779 * if it's not the next segment expected. 12780 * 12781 * Otherwise, collapse chain into one mblk (discard if 12782 * that fails). This makes sure the headers, retransmitted 12783 * data, and new data all are in the same mblk. 12784 */ 12785 ASSERT(mp != NULL); 12786 if (tcp->tcp_listener || !pullupmsg(mp, -1)) { 12787 freemsg(mp); 12788 return; 12789 } 12790 /* Update pointers into message */ 12791 iphdr = rptr = mp->b_rptr; 12792 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12793 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12794 /* 12795 * Since we can't handle any data with this urgent 12796 * pointer that is out of sequence, we expunge 12797 * the data. This allows us to still register 12798 * the urgent mark and generate the M_PCSIG, 12799 * which we can do. 12800 */ 12801 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12802 seg_len = 0; 12803 } 12804 } 12805 12806 switch (tcp->tcp_state) { 12807 case TCPS_SYN_SENT: 12808 if (flags & TH_ACK) { 12809 /* 12810 * Note that our stack cannot send data before a 12811 * connection is established, therefore the 12812 * following check is valid. Otherwise, it has 12813 * to be changed. 12814 */ 12815 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12816 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12817 freemsg(mp); 12818 if (flags & TH_RST) 12819 return; 12820 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12821 tcp, seg_ack, 0, TH_RST); 12822 return; 12823 } 12824 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12825 } 12826 if (flags & TH_RST) { 12827 freemsg(mp); 12828 if (flags & TH_ACK) 12829 (void) tcp_clean_death(tcp, 12830 ECONNREFUSED, 13); 12831 return; 12832 } 12833 if (!(flags & TH_SYN)) { 12834 freemsg(mp); 12835 return; 12836 } 12837 12838 /* Process all TCP options. */ 12839 tcp_process_options(tcp, tcph); 12840 /* 12841 * The following changes our rwnd to be a multiple of the 12842 * MIN(peer MSS, our MSS) for performance reason. 12843 */ 12844 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 12845 tcp->tcp_mss)); 12846 12847 /* Is the other end ECN capable? */ 12848 if (tcp->tcp_ecn_ok) { 12849 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12850 tcp->tcp_ecn_ok = B_FALSE; 12851 } 12852 } 12853 /* 12854 * Clear ECN flags because it may interfere with later 12855 * processing. 12856 */ 12857 flags &= ~(TH_ECE|TH_CWR); 12858 12859 tcp->tcp_irs = seg_seq; 12860 tcp->tcp_rack = seg_seq; 12861 tcp->tcp_rnxt = seg_seq + 1; 12862 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12863 if (!TCP_IS_DETACHED(tcp)) { 12864 /* Allocate room for SACK options if needed. */ 12865 if (tcp->tcp_snd_sack_ok) { 12866 (void) mi_set_sth_wroff(tcp->tcp_rq, 12867 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 12868 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra)); 12869 } else { 12870 (void) mi_set_sth_wroff(tcp->tcp_rq, 12871 tcp->tcp_hdr_len + 12872 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra)); 12873 } 12874 } 12875 if (flags & TH_ACK) { 12876 /* 12877 * If we can't get the confirmation upstream, pretend 12878 * we didn't even see this one. 12879 * 12880 * XXX: how can we pretend we didn't see it if we 12881 * have updated rnxt et. al. 12882 * 12883 * For loopback we defer sending up the T_CONN_CON 12884 * until after some checks below. 12885 */ 12886 mp1 = NULL; 12887 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12888 tcp->tcp_loopback ? &mp1 : NULL)) { 12889 freemsg(mp); 12890 return; 12891 } 12892 /* SYN was acked - making progress */ 12893 if (tcp->tcp_ipversion == IPV6_VERSION) 12894 tcp->tcp_ip_forward_progress = B_TRUE; 12895 12896 /* One for the SYN */ 12897 tcp->tcp_suna = tcp->tcp_iss + 1; 12898 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12899 tcp->tcp_state = TCPS_ESTABLISHED; 12900 12901 /* 12902 * If SYN was retransmitted, need to reset all 12903 * retransmission info. This is because this 12904 * segment will be treated as a dup ACK. 12905 */ 12906 if (tcp->tcp_rexmit) { 12907 tcp->tcp_rexmit = B_FALSE; 12908 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12909 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12910 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12911 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12912 tcp->tcp_ms_we_have_waited = 0; 12913 12914 /* 12915 * Set tcp_cwnd back to 1 MSS, per 12916 * recommendation from 12917 * draft-floyd-incr-init-win-01.txt, 12918 * Increasing TCP's Initial Window. 12919 */ 12920 tcp->tcp_cwnd = tcp->tcp_mss; 12921 } 12922 12923 tcp->tcp_swl1 = seg_seq; 12924 tcp->tcp_swl2 = seg_ack; 12925 12926 new_swnd = BE16_TO_U16(tcph->th_win); 12927 tcp->tcp_swnd = new_swnd; 12928 if (new_swnd > tcp->tcp_max_swnd) 12929 tcp->tcp_max_swnd = new_swnd; 12930 12931 /* 12932 * Always send the three-way handshake ack immediately 12933 * in order to make the connection complete as soon as 12934 * possible on the accepting host. 12935 */ 12936 flags |= TH_ACK_NEEDED; 12937 12938 /* 12939 * Special case for loopback. At this point we have 12940 * received SYN-ACK from the remote endpoint. In 12941 * order to ensure that both endpoints reach the 12942 * fused state prior to any data exchange, the final 12943 * ACK needs to be sent before we indicate T_CONN_CON 12944 * to the module upstream. 12945 */ 12946 if (tcp->tcp_loopback) { 12947 mblk_t *ack_mp; 12948 12949 ASSERT(!tcp->tcp_unfusable); 12950 ASSERT(mp1 != NULL); 12951 /* 12952 * For loopback, we always get a pure SYN-ACK 12953 * and only need to send back the final ACK 12954 * with no data (this is because the other 12955 * tcp is ours and we don't do T/TCP). This 12956 * final ACK triggers the passive side to 12957 * perform fusion in ESTABLISHED state. 12958 */ 12959 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 12960 if (tcp->tcp_ack_tid != 0) { 12961 (void) TCP_TIMER_CANCEL(tcp, 12962 tcp->tcp_ack_tid); 12963 tcp->tcp_ack_tid = 0; 12964 } 12965 TCP_RECORD_TRACE(tcp, ack_mp, 12966 TCP_TRACE_SEND_PKT); 12967 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 12968 BUMP_LOCAL(tcp->tcp_obsegs); 12969 BUMP_MIB(&tcp_mib, tcpOutAck); 12970 12971 /* Send up T_CONN_CON */ 12972 putnext(tcp->tcp_rq, mp1); 12973 12974 freemsg(mp); 12975 return; 12976 } 12977 /* 12978 * Forget fusion; we need to handle more 12979 * complex cases below. Send the deferred 12980 * T_CONN_CON message upstream and proceed 12981 * as usual. Mark this tcp as not capable 12982 * of fusion. 12983 */ 12984 TCP_STAT(tcp_fusion_unfusable); 12985 tcp->tcp_unfusable = B_TRUE; 12986 putnext(tcp->tcp_rq, mp1); 12987 } 12988 12989 /* 12990 * Check to see if there is data to be sent. If 12991 * yes, set the transmit flag. Then check to see 12992 * if received data processing needs to be done. 12993 * If not, go straight to xmit_check. This short 12994 * cut is OK as we don't support T/TCP. 12995 */ 12996 if (tcp->tcp_unsent) 12997 flags |= TH_XMIT_NEEDED; 12998 12999 if (seg_len == 0 && !(flags & TH_URG)) { 13000 freemsg(mp); 13001 goto xmit_check; 13002 } 13003 13004 flags &= ~TH_SYN; 13005 seg_seq++; 13006 break; 13007 } 13008 tcp->tcp_state = TCPS_SYN_RCVD; 13009 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13010 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13011 if (mp1) { 13012 DB_CPID(mp1) = tcp->tcp_cpid; 13013 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 13014 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13015 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13016 } 13017 freemsg(mp); 13018 return; 13019 case TCPS_SYN_RCVD: 13020 if (flags & TH_ACK) { 13021 /* 13022 * In this state, a SYN|ACK packet is either bogus 13023 * because the other side must be ACKing our SYN which 13024 * indicates it has seen the ACK for their SYN and 13025 * shouldn't retransmit it or we're crossing SYNs 13026 * on active open. 13027 */ 13028 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13029 freemsg(mp); 13030 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13031 tcp, seg_ack, 0, TH_RST); 13032 return; 13033 } 13034 /* 13035 * NOTE: RFC 793 pg. 72 says this should be 13036 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13037 * but that would mean we have an ack that ignored 13038 * our SYN. 13039 */ 13040 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13041 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13042 freemsg(mp); 13043 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13044 tcp, seg_ack, 0, TH_RST); 13045 return; 13046 } 13047 } 13048 break; 13049 case TCPS_LISTEN: 13050 /* 13051 * Only a TLI listener can come through this path when a 13052 * acceptor is going back to be a listener and a packet 13053 * for the acceptor hits the classifier. For a socket 13054 * listener, this can never happen because a listener 13055 * can never accept connection on itself and hence a 13056 * socket acceptor can not go back to being a listener. 13057 */ 13058 ASSERT(!TCP_IS_SOCKET(tcp)); 13059 /*FALLTHRU*/ 13060 case TCPS_CLOSED: 13061 case TCPS_BOUND: { 13062 conn_t *new_connp; 13063 13064 new_connp = ipcl_classify(mp, connp->conn_zoneid); 13065 if (new_connp != NULL) { 13066 tcp_reinput(new_connp, mp, connp->conn_sqp); 13067 return; 13068 } 13069 /* We failed to classify. For now just drop the packet */ 13070 freemsg(mp); 13071 return; 13072 } 13073 case TCPS_IDLE: 13074 /* 13075 * Handle the case where the tcp_clean_death() has happened 13076 * on a connection (application hasn't closed yet) but a packet 13077 * was already queued on squeue before tcp_clean_death() 13078 * was processed. Calling tcp_clean_death() twice on same 13079 * connection can result in weird behaviour. 13080 */ 13081 freemsg(mp); 13082 return; 13083 default: 13084 break; 13085 } 13086 13087 /* 13088 * Already on the correct queue/perimeter. 13089 * If this is a detached connection and not an eager 13090 * connection hanging off a listener then new data 13091 * (past the FIN) will cause a reset. 13092 * We do a special check here where it 13093 * is out of the main line, rather than check 13094 * if we are detached every time we see new 13095 * data down below. 13096 */ 13097 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13098 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13099 BUMP_MIB(&tcp_mib, tcpInClosed); 13100 TCP_RECORD_TRACE(tcp, 13101 mp, TCP_TRACE_RECV_PKT); 13102 13103 freemsg(mp); 13104 /* 13105 * This could be an SSL closure alert. We're detached so just 13106 * acknowledge it this last time. 13107 */ 13108 if (tcp->tcp_kssl_ctx != NULL) { 13109 kssl_release_ctx(tcp->tcp_kssl_ctx); 13110 tcp->tcp_kssl_ctx = NULL; 13111 13112 tcp->tcp_rnxt += seg_len; 13113 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13114 flags |= TH_ACK_NEEDED; 13115 goto ack_check; 13116 } 13117 13118 tcp_xmit_ctl("new data when detached", tcp, 13119 tcp->tcp_snxt, 0, TH_RST); 13120 (void) tcp_clean_death(tcp, EPROTO, 12); 13121 return; 13122 } 13123 13124 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13125 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13126 new_swnd = BE16_TO_U16(tcph->th_win) << 13127 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13128 mss = tcp->tcp_mss; 13129 13130 if (tcp->tcp_snd_ts_ok) { 13131 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13132 /* 13133 * This segment is not acceptable. 13134 * Drop it and send back an ACK. 13135 */ 13136 freemsg(mp); 13137 flags |= TH_ACK_NEEDED; 13138 goto ack_check; 13139 } 13140 } else if (tcp->tcp_snd_sack_ok) { 13141 ASSERT(tcp->tcp_sack_info != NULL); 13142 tcpopt.tcp = tcp; 13143 /* 13144 * SACK info in already updated in tcp_parse_options. Ignore 13145 * all other TCP options... 13146 */ 13147 (void) tcp_parse_options(tcph, &tcpopt); 13148 } 13149 try_again:; 13150 gap = seg_seq - tcp->tcp_rnxt; 13151 rgap = tcp->tcp_rwnd - (gap + seg_len); 13152 /* 13153 * gap is the amount of sequence space between what we expect to see 13154 * and what we got for seg_seq. A positive value for gap means 13155 * something got lost. A negative value means we got some old stuff. 13156 */ 13157 if (gap < 0) { 13158 /* Old stuff present. Is the SYN in there? */ 13159 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13160 (seg_len != 0)) { 13161 flags &= ~TH_SYN; 13162 seg_seq++; 13163 urp--; 13164 /* Recompute the gaps after noting the SYN. */ 13165 goto try_again; 13166 } 13167 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 13168 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, 13169 (seg_len > -gap ? -gap : seg_len)); 13170 /* Remove the old stuff from seg_len. */ 13171 seg_len += gap; 13172 /* 13173 * Anything left? 13174 * Make sure to check for unack'd FIN when rest of data 13175 * has been previously ack'd. 13176 */ 13177 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13178 /* 13179 * Resets are only valid if they lie within our offered 13180 * window. If the RST bit is set, we just ignore this 13181 * segment. 13182 */ 13183 if (flags & TH_RST) { 13184 freemsg(mp); 13185 return; 13186 } 13187 13188 /* 13189 * The arriving of dup data packets indicate that we 13190 * may have postponed an ack for too long, or the other 13191 * side's RTT estimate is out of shape. Start acking 13192 * more often. 13193 */ 13194 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13195 tcp->tcp_rack_cnt >= 1 && 13196 tcp->tcp_rack_abs_max > 2) { 13197 tcp->tcp_rack_abs_max--; 13198 } 13199 tcp->tcp_rack_cur_max = 1; 13200 13201 /* 13202 * This segment is "unacceptable". None of its 13203 * sequence space lies within our advertized window. 13204 * 13205 * Adjust seg_len to the original value for tracing. 13206 */ 13207 seg_len -= gap; 13208 if (tcp->tcp_debug) { 13209 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13210 "tcp_rput: unacceptable, gap %d, rgap %d, " 13211 "flags 0x%x, seg_seq %u, seg_ack %u, " 13212 "seg_len %d, rnxt %u, snxt %u, %s", 13213 gap, rgap, flags, seg_seq, seg_ack, 13214 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13215 tcp_display(tcp, NULL, 13216 DISP_ADDR_AND_PORT)); 13217 } 13218 13219 /* 13220 * Arrange to send an ACK in response to the 13221 * unacceptable segment per RFC 793 page 69. There 13222 * is only one small difference between ours and the 13223 * acceptability test in the RFC - we accept ACK-only 13224 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13225 * will be generated. 13226 * 13227 * Note that we have to ACK an ACK-only packet at least 13228 * for stacks that send 0-length keep-alives with 13229 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13230 * section 4.2.3.6. As long as we don't ever generate 13231 * an unacceptable packet in response to an incoming 13232 * packet that is unacceptable, it should not cause 13233 * "ACK wars". 13234 */ 13235 flags |= TH_ACK_NEEDED; 13236 13237 /* 13238 * Continue processing this segment in order to use the 13239 * ACK information it contains, but skip all other 13240 * sequence-number processing. Processing the ACK 13241 * information is necessary in order to 13242 * re-synchronize connections that may have lost 13243 * synchronization. 13244 * 13245 * We clear seg_len and flag fields related to 13246 * sequence number processing as they are not 13247 * to be trusted for an unacceptable segment. 13248 */ 13249 seg_len = 0; 13250 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13251 goto process_ack; 13252 } 13253 13254 /* Fix seg_seq, and chew the gap off the front. */ 13255 seg_seq = tcp->tcp_rnxt; 13256 urp += gap; 13257 do { 13258 mblk_t *mp2; 13259 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13260 (uintptr_t)UINT_MAX); 13261 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13262 if (gap > 0) { 13263 mp->b_rptr = mp->b_wptr - gap; 13264 break; 13265 } 13266 mp2 = mp; 13267 mp = mp->b_cont; 13268 freeb(mp2); 13269 } while (gap < 0); 13270 /* 13271 * If the urgent data has already been acknowledged, we 13272 * should ignore TH_URG below 13273 */ 13274 if (urp < 0) 13275 flags &= ~TH_URG; 13276 } 13277 /* 13278 * rgap is the amount of stuff received out of window. A negative 13279 * value is the amount out of window. 13280 */ 13281 if (rgap < 0) { 13282 mblk_t *mp2; 13283 13284 if (tcp->tcp_rwnd == 0) { 13285 BUMP_MIB(&tcp_mib, tcpInWinProbe); 13286 } else { 13287 BUMP_MIB(&tcp_mib, tcpInDataPastWinSegs); 13288 UPDATE_MIB(&tcp_mib, tcpInDataPastWinBytes, -rgap); 13289 } 13290 13291 /* 13292 * seg_len does not include the FIN, so if more than 13293 * just the FIN is out of window, we act like we don't 13294 * see it. (If just the FIN is out of window, rgap 13295 * will be zero and we will go ahead and acknowledge 13296 * the FIN.) 13297 */ 13298 flags &= ~TH_FIN; 13299 13300 /* Fix seg_len and make sure there is something left. */ 13301 seg_len += rgap; 13302 if (seg_len <= 0) { 13303 /* 13304 * Resets are only valid if they lie within our offered 13305 * window. If the RST bit is set, we just ignore this 13306 * segment. 13307 */ 13308 if (flags & TH_RST) { 13309 freemsg(mp); 13310 return; 13311 } 13312 13313 /* Per RFC 793, we need to send back an ACK. */ 13314 flags |= TH_ACK_NEEDED; 13315 13316 /* 13317 * Send SIGURG as soon as possible i.e. even 13318 * if the TH_URG was delivered in a window probe 13319 * packet (which will be unacceptable). 13320 * 13321 * We generate a signal if none has been generated 13322 * for this connection or if this is a new urgent 13323 * byte. Also send a zero-length "unmarked" message 13324 * to inform SIOCATMARK that this is not the mark. 13325 * 13326 * tcp_urp_last_valid is cleared when the T_exdata_ind 13327 * is sent up. This plus the check for old data 13328 * (gap >= 0) handles the wraparound of the sequence 13329 * number space without having to always track the 13330 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13331 * this max in its rcv_up variable). 13332 * 13333 * This prevents duplicate SIGURGS due to a "late" 13334 * zero-window probe when the T_EXDATA_IND has already 13335 * been sent up. 13336 */ 13337 if ((flags & TH_URG) && 13338 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13339 tcp->tcp_urp_last))) { 13340 mp1 = allocb(0, BPRI_MED); 13341 if (mp1 == NULL) { 13342 freemsg(mp); 13343 return; 13344 } 13345 if (!TCP_IS_DETACHED(tcp) && 13346 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13347 SIGURG)) { 13348 /* Try again on the rexmit. */ 13349 freemsg(mp1); 13350 freemsg(mp); 13351 return; 13352 } 13353 /* 13354 * If the next byte would be the mark 13355 * then mark with MARKNEXT else mark 13356 * with NOTMARKNEXT. 13357 */ 13358 if (gap == 0 && urp == 0) 13359 mp1->b_flag |= MSGMARKNEXT; 13360 else 13361 mp1->b_flag |= MSGNOTMARKNEXT; 13362 freemsg(tcp->tcp_urp_mark_mp); 13363 tcp->tcp_urp_mark_mp = mp1; 13364 flags |= TH_SEND_URP_MARK; 13365 tcp->tcp_urp_last_valid = B_TRUE; 13366 tcp->tcp_urp_last = urp + seg_seq; 13367 } 13368 /* 13369 * If this is a zero window probe, continue to 13370 * process the ACK part. But we need to set seg_len 13371 * to 0 to avoid data processing. Otherwise just 13372 * drop the segment and send back an ACK. 13373 */ 13374 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13375 flags &= ~(TH_SYN | TH_URG); 13376 seg_len = 0; 13377 goto process_ack; 13378 } else { 13379 freemsg(mp); 13380 goto ack_check; 13381 } 13382 } 13383 /* Pitch out of window stuff off the end. */ 13384 rgap = seg_len; 13385 mp2 = mp; 13386 do { 13387 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13388 (uintptr_t)INT_MAX); 13389 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13390 if (rgap < 0) { 13391 mp2->b_wptr += rgap; 13392 if ((mp1 = mp2->b_cont) != NULL) { 13393 mp2->b_cont = NULL; 13394 freemsg(mp1); 13395 } 13396 break; 13397 } 13398 } while ((mp2 = mp2->b_cont) != NULL); 13399 } 13400 ok:; 13401 /* 13402 * TCP should check ECN info for segments inside the window only. 13403 * Therefore the check should be done here. 13404 */ 13405 if (tcp->tcp_ecn_ok) { 13406 if (flags & TH_CWR) { 13407 tcp->tcp_ecn_echo_on = B_FALSE; 13408 } 13409 /* 13410 * Note that both ECN_CE and CWR can be set in the 13411 * same segment. In this case, we once again turn 13412 * on ECN_ECHO. 13413 */ 13414 if (tcp->tcp_ipversion == IPV4_VERSION) { 13415 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13416 13417 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13418 tcp->tcp_ecn_echo_on = B_TRUE; 13419 } 13420 } else { 13421 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13422 13423 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13424 htonl(IPH_ECN_CE << 20)) { 13425 tcp->tcp_ecn_echo_on = B_TRUE; 13426 } 13427 } 13428 } 13429 13430 /* 13431 * Check whether we can update tcp_ts_recent. This test is 13432 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13433 * Extensions for High Performance: An Update", Internet Draft. 13434 */ 13435 if (tcp->tcp_snd_ts_ok && 13436 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13437 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13438 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13439 tcp->tcp_last_rcv_lbolt = lbolt64; 13440 } 13441 13442 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13443 /* 13444 * FIN in an out of order segment. We record this in 13445 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13446 * Clear the FIN so that any check on FIN flag will fail. 13447 * Remember that FIN also counts in the sequence number 13448 * space. So we need to ack out of order FIN only segments. 13449 */ 13450 if (flags & TH_FIN) { 13451 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13452 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13453 flags &= ~TH_FIN; 13454 flags |= TH_ACK_NEEDED; 13455 } 13456 if (seg_len > 0) { 13457 /* Fill in the SACK blk list. */ 13458 if (tcp->tcp_snd_sack_ok) { 13459 ASSERT(tcp->tcp_sack_info != NULL); 13460 tcp_sack_insert(tcp->tcp_sack_list, 13461 seg_seq, seg_seq + seg_len, 13462 &(tcp->tcp_num_sack_blk)); 13463 } 13464 13465 /* 13466 * Attempt reassembly and see if we have something 13467 * ready to go. 13468 */ 13469 mp = tcp_reass(tcp, mp, seg_seq); 13470 /* Always ack out of order packets */ 13471 flags |= TH_ACK_NEEDED | TH_PUSH; 13472 if (mp) { 13473 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13474 (uintptr_t)INT_MAX); 13475 seg_len = mp->b_cont ? msgdsize(mp) : 13476 (int)(mp->b_wptr - mp->b_rptr); 13477 seg_seq = tcp->tcp_rnxt; 13478 /* 13479 * A gap is filled and the seq num and len 13480 * of the gap match that of a previously 13481 * received FIN, put the FIN flag back in. 13482 */ 13483 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13484 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13485 flags |= TH_FIN; 13486 tcp->tcp_valid_bits &= 13487 ~TCP_OFO_FIN_VALID; 13488 } 13489 } else { 13490 /* 13491 * Keep going even with NULL mp. 13492 * There may be a useful ACK or something else 13493 * we don't want to miss. 13494 * 13495 * But TCP should not perform fast retransmit 13496 * because of the ack number. TCP uses 13497 * seg_len == 0 to determine if it is a pure 13498 * ACK. And this is not a pure ACK. 13499 */ 13500 seg_len = 0; 13501 ofo_seg = B_TRUE; 13502 } 13503 } 13504 } else if (seg_len > 0) { 13505 BUMP_MIB(&tcp_mib, tcpInDataInorderSegs); 13506 UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, seg_len); 13507 /* 13508 * If an out of order FIN was received before, and the seq 13509 * num and len of the new segment match that of the FIN, 13510 * put the FIN flag back in. 13511 */ 13512 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13513 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13514 flags |= TH_FIN; 13515 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13516 } 13517 } 13518 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13519 if (flags & TH_RST) { 13520 freemsg(mp); 13521 switch (tcp->tcp_state) { 13522 case TCPS_SYN_RCVD: 13523 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13524 break; 13525 case TCPS_ESTABLISHED: 13526 case TCPS_FIN_WAIT_1: 13527 case TCPS_FIN_WAIT_2: 13528 case TCPS_CLOSE_WAIT: 13529 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13530 break; 13531 case TCPS_CLOSING: 13532 case TCPS_LAST_ACK: 13533 (void) tcp_clean_death(tcp, 0, 16); 13534 break; 13535 default: 13536 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13537 (void) tcp_clean_death(tcp, ENXIO, 17); 13538 break; 13539 } 13540 return; 13541 } 13542 if (flags & TH_SYN) { 13543 /* 13544 * See RFC 793, Page 71 13545 * 13546 * The seq number must be in the window as it should 13547 * be "fixed" above. If it is outside window, it should 13548 * be already rejected. Note that we allow seg_seq to be 13549 * rnxt + rwnd because we want to accept 0 window probe. 13550 */ 13551 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13552 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13553 freemsg(mp); 13554 /* 13555 * If the ACK flag is not set, just use our snxt as the 13556 * seq number of the RST segment. 13557 */ 13558 if (!(flags & TH_ACK)) { 13559 seg_ack = tcp->tcp_snxt; 13560 } 13561 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13562 TH_RST|TH_ACK); 13563 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13564 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13565 return; 13566 } 13567 /* 13568 * urp could be -1 when the urp field in the packet is 0 13569 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13570 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13571 */ 13572 if (flags & TH_URG && urp >= 0) { 13573 if (!tcp->tcp_urp_last_valid || 13574 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13575 /* 13576 * If we haven't generated the signal yet for this 13577 * urgent pointer value, do it now. Also, send up a 13578 * zero-length M_DATA indicating whether or not this is 13579 * the mark. The latter is not needed when a 13580 * T_EXDATA_IND is sent up. However, if there are 13581 * allocation failures this code relies on the sender 13582 * retransmitting and the socket code for determining 13583 * the mark should not block waiting for the peer to 13584 * transmit. Thus, for simplicity we always send up the 13585 * mark indication. 13586 */ 13587 mp1 = allocb(0, BPRI_MED); 13588 if (mp1 == NULL) { 13589 freemsg(mp); 13590 return; 13591 } 13592 if (!TCP_IS_DETACHED(tcp) && 13593 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13594 /* Try again on the rexmit. */ 13595 freemsg(mp1); 13596 freemsg(mp); 13597 return; 13598 } 13599 /* 13600 * Mark with NOTMARKNEXT for now. 13601 * The code below will change this to MARKNEXT 13602 * if we are at the mark. 13603 * 13604 * If there are allocation failures (e.g. in dupmsg 13605 * below) the next time tcp_rput_data sees the urgent 13606 * segment it will send up the MSG*MARKNEXT message. 13607 */ 13608 mp1->b_flag |= MSGNOTMARKNEXT; 13609 freemsg(tcp->tcp_urp_mark_mp); 13610 tcp->tcp_urp_mark_mp = mp1; 13611 flags |= TH_SEND_URP_MARK; 13612 #ifdef DEBUG 13613 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13614 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13615 "last %x, %s", 13616 seg_seq, urp, tcp->tcp_urp_last, 13617 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13618 #endif /* DEBUG */ 13619 tcp->tcp_urp_last_valid = B_TRUE; 13620 tcp->tcp_urp_last = urp + seg_seq; 13621 } else if (tcp->tcp_urp_mark_mp != NULL) { 13622 /* 13623 * An allocation failure prevented the previous 13624 * tcp_rput_data from sending up the allocated 13625 * MSG*MARKNEXT message - send it up this time 13626 * around. 13627 */ 13628 flags |= TH_SEND_URP_MARK; 13629 } 13630 13631 /* 13632 * If the urgent byte is in this segment, make sure that it is 13633 * all by itself. This makes it much easier to deal with the 13634 * possibility of an allocation failure on the T_exdata_ind. 13635 * Note that seg_len is the number of bytes in the segment, and 13636 * urp is the offset into the segment of the urgent byte. 13637 * urp < seg_len means that the urgent byte is in this segment. 13638 */ 13639 if (urp < seg_len) { 13640 if (seg_len != 1) { 13641 uint32_t tmp_rnxt; 13642 /* 13643 * Break it up and feed it back in. 13644 * Re-attach the IP header. 13645 */ 13646 mp->b_rptr = iphdr; 13647 if (urp > 0) { 13648 /* 13649 * There is stuff before the urgent 13650 * byte. 13651 */ 13652 mp1 = dupmsg(mp); 13653 if (!mp1) { 13654 /* 13655 * Trim from urgent byte on. 13656 * The rest will come back. 13657 */ 13658 (void) adjmsg(mp, 13659 urp - seg_len); 13660 tcp_rput_data(connp, 13661 mp, NULL); 13662 return; 13663 } 13664 (void) adjmsg(mp1, urp - seg_len); 13665 /* Feed this piece back in. */ 13666 tmp_rnxt = tcp->tcp_rnxt; 13667 tcp_rput_data(connp, mp1, NULL); 13668 /* 13669 * If the data passed back in was not 13670 * processed (ie: bad ACK) sending 13671 * the remainder back in will cause a 13672 * loop. In this case, drop the 13673 * packet and let the sender try 13674 * sending a good packet. 13675 */ 13676 if (tmp_rnxt == tcp->tcp_rnxt) { 13677 freemsg(mp); 13678 return; 13679 } 13680 } 13681 if (urp != seg_len - 1) { 13682 uint32_t tmp_rnxt; 13683 /* 13684 * There is stuff after the urgent 13685 * byte. 13686 */ 13687 mp1 = dupmsg(mp); 13688 if (!mp1) { 13689 /* 13690 * Trim everything beyond the 13691 * urgent byte. The rest will 13692 * come back. 13693 */ 13694 (void) adjmsg(mp, 13695 urp + 1 - seg_len); 13696 tcp_rput_data(connp, 13697 mp, NULL); 13698 return; 13699 } 13700 (void) adjmsg(mp1, urp + 1 - seg_len); 13701 tmp_rnxt = tcp->tcp_rnxt; 13702 tcp_rput_data(connp, mp1, NULL); 13703 /* 13704 * If the data passed back in was not 13705 * processed (ie: bad ACK) sending 13706 * the remainder back in will cause a 13707 * loop. In this case, drop the 13708 * packet and let the sender try 13709 * sending a good packet. 13710 */ 13711 if (tmp_rnxt == tcp->tcp_rnxt) { 13712 freemsg(mp); 13713 return; 13714 } 13715 } 13716 tcp_rput_data(connp, mp, NULL); 13717 return; 13718 } 13719 /* 13720 * This segment contains only the urgent byte. We 13721 * have to allocate the T_exdata_ind, if we can. 13722 */ 13723 if (!tcp->tcp_urp_mp) { 13724 struct T_exdata_ind *tei; 13725 mp1 = allocb(sizeof (struct T_exdata_ind), 13726 BPRI_MED); 13727 if (!mp1) { 13728 /* 13729 * Sigh... It'll be back. 13730 * Generate any MSG*MARK message now. 13731 */ 13732 freemsg(mp); 13733 seg_len = 0; 13734 if (flags & TH_SEND_URP_MARK) { 13735 13736 13737 ASSERT(tcp->tcp_urp_mark_mp); 13738 tcp->tcp_urp_mark_mp->b_flag &= 13739 ~MSGNOTMARKNEXT; 13740 tcp->tcp_urp_mark_mp->b_flag |= 13741 MSGMARKNEXT; 13742 } 13743 goto ack_check; 13744 } 13745 mp1->b_datap->db_type = M_PROTO; 13746 tei = (struct T_exdata_ind *)mp1->b_rptr; 13747 tei->PRIM_type = T_EXDATA_IND; 13748 tei->MORE_flag = 0; 13749 mp1->b_wptr = (uchar_t *)&tei[1]; 13750 tcp->tcp_urp_mp = mp1; 13751 #ifdef DEBUG 13752 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13753 "tcp_rput: allocated exdata_ind %s", 13754 tcp_display(tcp, NULL, 13755 DISP_PORT_ONLY)); 13756 #endif /* DEBUG */ 13757 /* 13758 * There is no need to send a separate MSG*MARK 13759 * message since the T_EXDATA_IND will be sent 13760 * now. 13761 */ 13762 flags &= ~TH_SEND_URP_MARK; 13763 freemsg(tcp->tcp_urp_mark_mp); 13764 tcp->tcp_urp_mark_mp = NULL; 13765 } 13766 /* 13767 * Now we are all set. On the next putnext upstream, 13768 * tcp_urp_mp will be non-NULL and will get prepended 13769 * to what has to be this piece containing the urgent 13770 * byte. If for any reason we abort this segment below, 13771 * if it comes back, we will have this ready, or it 13772 * will get blown off in close. 13773 */ 13774 } else if (urp == seg_len) { 13775 /* 13776 * The urgent byte is the next byte after this sequence 13777 * number. If there is data it is marked with 13778 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13779 * since it is not needed. Otherwise, if the code 13780 * above just allocated a zero-length tcp_urp_mark_mp 13781 * message, that message is tagged with MSGMARKNEXT. 13782 * Sending up these MSGMARKNEXT messages makes 13783 * SIOCATMARK work correctly even though 13784 * the T_EXDATA_IND will not be sent up until the 13785 * urgent byte arrives. 13786 */ 13787 if (seg_len != 0) { 13788 flags |= TH_MARKNEXT_NEEDED; 13789 freemsg(tcp->tcp_urp_mark_mp); 13790 tcp->tcp_urp_mark_mp = NULL; 13791 flags &= ~TH_SEND_URP_MARK; 13792 } else if (tcp->tcp_urp_mark_mp != NULL) { 13793 flags |= TH_SEND_URP_MARK; 13794 tcp->tcp_urp_mark_mp->b_flag &= 13795 ~MSGNOTMARKNEXT; 13796 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13797 } 13798 #ifdef DEBUG 13799 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13800 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13801 seg_len, flags, 13802 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13803 #endif /* DEBUG */ 13804 } else { 13805 /* Data left until we hit mark */ 13806 #ifdef DEBUG 13807 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13808 "tcp_rput: URP %d bytes left, %s", 13809 urp - seg_len, tcp_display(tcp, NULL, 13810 DISP_PORT_ONLY)); 13811 #endif /* DEBUG */ 13812 } 13813 } 13814 13815 process_ack: 13816 if (!(flags & TH_ACK)) { 13817 freemsg(mp); 13818 goto xmit_check; 13819 } 13820 } 13821 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13822 13823 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13824 tcp->tcp_ip_forward_progress = B_TRUE; 13825 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13826 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13827 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13828 /* 3-way handshake complete - pass up the T_CONN_IND */ 13829 tcp_t *listener = tcp->tcp_listener; 13830 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13831 13832 tcp->tcp_tconnind_started = B_TRUE; 13833 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13834 /* 13835 * We are here means eager is fine but it can 13836 * get a TH_RST at any point between now and till 13837 * accept completes and disappear. We need to 13838 * ensure that reference to eager is valid after 13839 * we get out of eager's perimeter. So we do 13840 * an extra refhold. 13841 */ 13842 CONN_INC_REF(connp); 13843 13844 /* 13845 * The listener also exists because of the refhold 13846 * done in tcp_conn_request. Its possible that it 13847 * might have closed. We will check that once we 13848 * get inside listeners context. 13849 */ 13850 CONN_INC_REF(listener->tcp_connp); 13851 if (listener->tcp_connp->conn_sqp == 13852 connp->conn_sqp) { 13853 tcp_send_conn_ind(listener->tcp_connp, mp, 13854 listener->tcp_connp->conn_sqp); 13855 CONN_DEC_REF(listener->tcp_connp); 13856 } else if (!tcp->tcp_loopback) { 13857 squeue_fill(listener->tcp_connp->conn_sqp, mp, 13858 tcp_send_conn_ind, 13859 listener->tcp_connp, SQTAG_TCP_CONN_IND); 13860 } else { 13861 squeue_enter(listener->tcp_connp->conn_sqp, mp, 13862 tcp_send_conn_ind, listener->tcp_connp, 13863 SQTAG_TCP_CONN_IND); 13864 } 13865 } 13866 13867 if (tcp->tcp_active_open) { 13868 /* 13869 * We are seeing the final ack in the three way 13870 * hand shake of a active open'ed connection 13871 * so we must send up a T_CONN_CON 13872 */ 13873 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13874 freemsg(mp); 13875 return; 13876 } 13877 /* 13878 * Don't fuse the loopback endpoints for 13879 * simultaneous active opens. 13880 */ 13881 if (tcp->tcp_loopback) { 13882 TCP_STAT(tcp_fusion_unfusable); 13883 tcp->tcp_unfusable = B_TRUE; 13884 } 13885 } 13886 13887 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 13888 bytes_acked--; 13889 /* SYN was acked - making progress */ 13890 if (tcp->tcp_ipversion == IPV6_VERSION) 13891 tcp->tcp_ip_forward_progress = B_TRUE; 13892 13893 /* 13894 * If SYN was retransmitted, need to reset all 13895 * retransmission info as this segment will be 13896 * treated as a dup ACK. 13897 */ 13898 if (tcp->tcp_rexmit) { 13899 tcp->tcp_rexmit = B_FALSE; 13900 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13901 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13902 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13903 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13904 tcp->tcp_ms_we_have_waited = 0; 13905 tcp->tcp_cwnd = mss; 13906 } 13907 13908 /* 13909 * We set the send window to zero here. 13910 * This is needed if there is data to be 13911 * processed already on the queue. 13912 * Later (at swnd_update label), the 13913 * "new_swnd > tcp_swnd" condition is satisfied 13914 * the XMIT_NEEDED flag is set in the current 13915 * (SYN_RCVD) state. This ensures tcp_wput_data() is 13916 * called if there is already data on queue in 13917 * this state. 13918 */ 13919 tcp->tcp_swnd = 0; 13920 13921 if (new_swnd > tcp->tcp_max_swnd) 13922 tcp->tcp_max_swnd = new_swnd; 13923 tcp->tcp_swl1 = seg_seq; 13924 tcp->tcp_swl2 = seg_ack; 13925 tcp->tcp_state = TCPS_ESTABLISHED; 13926 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13927 13928 /* Fuse when both sides are in ESTABLISHED state */ 13929 if (tcp->tcp_loopback && do_tcp_fusion) 13930 tcp_fuse(tcp, iphdr, tcph); 13931 13932 } 13933 /* This code follows 4.4BSD-Lite2 mostly. */ 13934 if (bytes_acked < 0) 13935 goto est; 13936 13937 /* 13938 * If TCP is ECN capable and the congestion experience bit is 13939 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 13940 * done once per window (or more loosely, per RTT). 13941 */ 13942 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 13943 tcp->tcp_cwr = B_FALSE; 13944 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 13945 if (!tcp->tcp_cwr) { 13946 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 13947 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 13948 tcp->tcp_cwnd = npkt * mss; 13949 /* 13950 * If the cwnd is 0, use the timer to clock out 13951 * new segments. This is required by the ECN spec. 13952 */ 13953 if (npkt == 0) { 13954 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13955 /* 13956 * This makes sure that when the ACK comes 13957 * back, we will increase tcp_cwnd by 1 MSS. 13958 */ 13959 tcp->tcp_cwnd_cnt = 0; 13960 } 13961 tcp->tcp_cwr = B_TRUE; 13962 /* 13963 * This marks the end of the current window of in 13964 * flight data. That is why we don't use 13965 * tcp_suna + tcp_swnd. Only data in flight can 13966 * provide ECN info. 13967 */ 13968 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13969 tcp->tcp_ecn_cwr_sent = B_FALSE; 13970 } 13971 } 13972 13973 mp1 = tcp->tcp_xmit_head; 13974 if (bytes_acked == 0) { 13975 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 13976 int dupack_cnt; 13977 13978 BUMP_MIB(&tcp_mib, tcpInDupAck); 13979 /* 13980 * Fast retransmit. When we have seen exactly three 13981 * identical ACKs while we have unacked data 13982 * outstanding we take it as a hint that our peer 13983 * dropped something. 13984 * 13985 * If TCP is retransmitting, don't do fast retransmit. 13986 */ 13987 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 13988 ! tcp->tcp_rexmit) { 13989 /* Do Limited Transmit */ 13990 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 13991 tcp_dupack_fast_retransmit) { 13992 /* 13993 * RFC 3042 13994 * 13995 * What we need to do is temporarily 13996 * increase tcp_cwnd so that new 13997 * data can be sent if it is allowed 13998 * by the receive window (tcp_rwnd). 13999 * tcp_wput_data() will take care of 14000 * the rest. 14001 * 14002 * If the connection is SACK capable, 14003 * only do limited xmit when there 14004 * is SACK info. 14005 * 14006 * Note how tcp_cwnd is incremented. 14007 * The first dup ACK will increase 14008 * it by 1 MSS. The second dup ACK 14009 * will increase it by 2 MSS. This 14010 * means that only 1 new segment will 14011 * be sent for each dup ACK. 14012 */ 14013 if (tcp->tcp_unsent > 0 && 14014 (!tcp->tcp_snd_sack_ok || 14015 (tcp->tcp_snd_sack_ok && 14016 tcp->tcp_notsack_list != NULL))) { 14017 tcp->tcp_cwnd += mss << 14018 (tcp->tcp_dupack_cnt - 1); 14019 flags |= TH_LIMIT_XMIT; 14020 } 14021 } else if (dupack_cnt == 14022 tcp_dupack_fast_retransmit) { 14023 14024 /* 14025 * If we have reduced tcp_ssthresh 14026 * because of ECN, do not reduce it again 14027 * unless it is already one window of data 14028 * away. After one window of data, tcp_cwr 14029 * should then be cleared. Note that 14030 * for non ECN capable connection, tcp_cwr 14031 * should always be false. 14032 * 14033 * Adjust cwnd since the duplicate 14034 * ack indicates that a packet was 14035 * dropped (due to congestion.) 14036 */ 14037 if (!tcp->tcp_cwr) { 14038 npkt = ((tcp->tcp_snxt - 14039 tcp->tcp_suna) >> 1) / mss; 14040 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14041 mss; 14042 tcp->tcp_cwnd = (npkt + 14043 tcp->tcp_dupack_cnt) * mss; 14044 } 14045 if (tcp->tcp_ecn_ok) { 14046 tcp->tcp_cwr = B_TRUE; 14047 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14048 tcp->tcp_ecn_cwr_sent = B_FALSE; 14049 } 14050 14051 /* 14052 * We do Hoe's algorithm. Refer to her 14053 * paper "Improving the Start-up Behavior 14054 * of a Congestion Control Scheme for TCP," 14055 * appeared in SIGCOMM'96. 14056 * 14057 * Save highest seq no we have sent so far. 14058 * Be careful about the invisible FIN byte. 14059 */ 14060 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14061 (tcp->tcp_unsent == 0)) { 14062 tcp->tcp_rexmit_max = tcp->tcp_fss; 14063 } else { 14064 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14065 } 14066 14067 /* 14068 * Do not allow bursty traffic during. 14069 * fast recovery. Refer to Fall and Floyd's 14070 * paper "Simulation-based Comparisons of 14071 * Tahoe, Reno and SACK TCP" (in CCR?) 14072 * This is a best current practise. 14073 */ 14074 tcp->tcp_snd_burst = TCP_CWND_SS; 14075 14076 /* 14077 * For SACK: 14078 * Calculate tcp_pipe, which is the 14079 * estimated number of bytes in 14080 * network. 14081 * 14082 * tcp_fack is the highest sack'ed seq num 14083 * TCP has received. 14084 * 14085 * tcp_pipe is explained in the above quoted 14086 * Fall and Floyd's paper. tcp_fack is 14087 * explained in Mathis and Mahdavi's 14088 * "Forward Acknowledgment: Refining TCP 14089 * Congestion Control" in SIGCOMM '96. 14090 */ 14091 if (tcp->tcp_snd_sack_ok) { 14092 ASSERT(tcp->tcp_sack_info != NULL); 14093 if (tcp->tcp_notsack_list != NULL) { 14094 tcp->tcp_pipe = tcp->tcp_snxt - 14095 tcp->tcp_fack; 14096 tcp->tcp_sack_snxt = seg_ack; 14097 flags |= TH_NEED_SACK_REXMIT; 14098 } else { 14099 /* 14100 * Always initialize tcp_pipe 14101 * even though we don't have 14102 * any SACK info. If later 14103 * we get SACK info and 14104 * tcp_pipe is not initialized, 14105 * funny things will happen. 14106 */ 14107 tcp->tcp_pipe = 14108 tcp->tcp_cwnd_ssthresh; 14109 } 14110 } else { 14111 flags |= TH_REXMIT_NEEDED; 14112 } /* tcp_snd_sack_ok */ 14113 14114 } else { 14115 /* 14116 * Here we perform congestion 14117 * avoidance, but NOT slow start. 14118 * This is known as the Fast 14119 * Recovery Algorithm. 14120 */ 14121 if (tcp->tcp_snd_sack_ok && 14122 tcp->tcp_notsack_list != NULL) { 14123 flags |= TH_NEED_SACK_REXMIT; 14124 tcp->tcp_pipe -= mss; 14125 if (tcp->tcp_pipe < 0) 14126 tcp->tcp_pipe = 0; 14127 } else { 14128 /* 14129 * We know that one more packet has 14130 * left the pipe thus we can update 14131 * cwnd. 14132 */ 14133 cwnd = tcp->tcp_cwnd + mss; 14134 if (cwnd > tcp->tcp_cwnd_max) 14135 cwnd = tcp->tcp_cwnd_max; 14136 tcp->tcp_cwnd = cwnd; 14137 if (tcp->tcp_unsent > 0) 14138 flags |= TH_XMIT_NEEDED; 14139 } 14140 } 14141 } 14142 } else if (tcp->tcp_zero_win_probe) { 14143 /* 14144 * If the window has opened, need to arrange 14145 * to send additional data. 14146 */ 14147 if (new_swnd != 0) { 14148 /* tcp_suna != tcp_snxt */ 14149 /* Packet contains a window update */ 14150 BUMP_MIB(&tcp_mib, tcpInWinUpdate); 14151 tcp->tcp_zero_win_probe = 0; 14152 tcp->tcp_timer_backoff = 0; 14153 tcp->tcp_ms_we_have_waited = 0; 14154 14155 /* 14156 * Transmit starting with tcp_suna since 14157 * the one byte probe is not ack'ed. 14158 * If TCP has sent more than one identical 14159 * probe, tcp_rexmit will be set. That means 14160 * tcp_ss_rexmit() will send out the one 14161 * byte along with new data. Otherwise, 14162 * fake the retransmission. 14163 */ 14164 flags |= TH_XMIT_NEEDED; 14165 if (!tcp->tcp_rexmit) { 14166 tcp->tcp_rexmit = B_TRUE; 14167 tcp->tcp_dupack_cnt = 0; 14168 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14169 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14170 } 14171 } 14172 } 14173 goto swnd_update; 14174 } 14175 14176 /* 14177 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14178 * If the ACK value acks something that we have not yet sent, it might 14179 * be an old duplicate segment. Send an ACK to re-synchronize the 14180 * other side. 14181 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14182 * state is handled above, so we can always just drop the segment and 14183 * send an ACK here. 14184 * 14185 * Should we send ACKs in response to ACK only segments? 14186 */ 14187 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14188 BUMP_MIB(&tcp_mib, tcpInAckUnsent); 14189 /* drop the received segment */ 14190 freemsg(mp); 14191 14192 /* 14193 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14194 * greater than 0, check if the number of such 14195 * bogus ACks is greater than that count. If yes, 14196 * don't send back any ACK. This prevents TCP from 14197 * getting into an ACK storm if somehow an attacker 14198 * successfully spoofs an acceptable segment to our 14199 * peer. 14200 */ 14201 if (tcp_drop_ack_unsent_cnt > 0 && 14202 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14203 TCP_STAT(tcp_in_ack_unsent_drop); 14204 return; 14205 } 14206 mp = tcp_ack_mp(tcp); 14207 if (mp != NULL) { 14208 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 14209 BUMP_LOCAL(tcp->tcp_obsegs); 14210 BUMP_MIB(&tcp_mib, tcpOutAck); 14211 tcp_send_data(tcp, tcp->tcp_wq, mp); 14212 } 14213 return; 14214 } 14215 14216 /* 14217 * TCP gets a new ACK, update the notsack'ed list to delete those 14218 * blocks that are covered by this ACK. 14219 */ 14220 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14221 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14222 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14223 } 14224 14225 /* 14226 * If we got an ACK after fast retransmit, check to see 14227 * if it is a partial ACK. If it is not and the congestion 14228 * window was inflated to account for the other side's 14229 * cached packets, retract it. If it is, do Hoe's algorithm. 14230 */ 14231 if (tcp->tcp_dupack_cnt >= tcp_dupack_fast_retransmit) { 14232 ASSERT(tcp->tcp_rexmit == B_FALSE); 14233 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14234 tcp->tcp_dupack_cnt = 0; 14235 /* 14236 * Restore the orig tcp_cwnd_ssthresh after 14237 * fast retransmit phase. 14238 */ 14239 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14240 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14241 } 14242 tcp->tcp_rexmit_max = seg_ack; 14243 tcp->tcp_cwnd_cnt = 0; 14244 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14245 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14246 14247 /* 14248 * Remove all notsack info to avoid confusion with 14249 * the next fast retrasnmit/recovery phase. 14250 */ 14251 if (tcp->tcp_snd_sack_ok && 14252 tcp->tcp_notsack_list != NULL) { 14253 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14254 } 14255 } else { 14256 if (tcp->tcp_snd_sack_ok && 14257 tcp->tcp_notsack_list != NULL) { 14258 flags |= TH_NEED_SACK_REXMIT; 14259 tcp->tcp_pipe -= mss; 14260 if (tcp->tcp_pipe < 0) 14261 tcp->tcp_pipe = 0; 14262 } else { 14263 /* 14264 * Hoe's algorithm: 14265 * 14266 * Retransmit the unack'ed segment and 14267 * restart fast recovery. Note that we 14268 * need to scale back tcp_cwnd to the 14269 * original value when we started fast 14270 * recovery. This is to prevent overly 14271 * aggressive behaviour in sending new 14272 * segments. 14273 */ 14274 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14275 tcp_dupack_fast_retransmit * mss; 14276 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14277 flags |= TH_REXMIT_NEEDED; 14278 } 14279 } 14280 } else { 14281 tcp->tcp_dupack_cnt = 0; 14282 if (tcp->tcp_rexmit) { 14283 /* 14284 * TCP is retranmitting. If the ACK ack's all 14285 * outstanding data, update tcp_rexmit_max and 14286 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14287 * to the correct value. 14288 * 14289 * Note that SEQ_LEQ() is used. This is to avoid 14290 * unnecessary fast retransmit caused by dup ACKs 14291 * received when TCP does slow start retransmission 14292 * after a time out. During this phase, TCP may 14293 * send out segments which are already received. 14294 * This causes dup ACKs to be sent back. 14295 */ 14296 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14297 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14298 tcp->tcp_rexmit_nxt = seg_ack; 14299 } 14300 if (seg_ack != tcp->tcp_rexmit_max) { 14301 flags |= TH_XMIT_NEEDED; 14302 } 14303 } else { 14304 tcp->tcp_rexmit = B_FALSE; 14305 tcp->tcp_xmit_zc_clean = B_FALSE; 14306 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14307 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14308 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14309 } 14310 tcp->tcp_ms_we_have_waited = 0; 14311 } 14312 } 14313 14314 BUMP_MIB(&tcp_mib, tcpInAckSegs); 14315 UPDATE_MIB(&tcp_mib, tcpInAckBytes, bytes_acked); 14316 tcp->tcp_suna = seg_ack; 14317 if (tcp->tcp_zero_win_probe != 0) { 14318 tcp->tcp_zero_win_probe = 0; 14319 tcp->tcp_timer_backoff = 0; 14320 } 14321 14322 /* 14323 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14324 * Note that it cannot be the SYN being ack'ed. The code flow 14325 * will not reach here. 14326 */ 14327 if (mp1 == NULL) { 14328 goto fin_acked; 14329 } 14330 14331 /* 14332 * Update the congestion window. 14333 * 14334 * If TCP is not ECN capable or TCP is ECN capable but the 14335 * congestion experience bit is not set, increase the tcp_cwnd as 14336 * usual. 14337 */ 14338 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14339 cwnd = tcp->tcp_cwnd; 14340 add = mss; 14341 14342 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14343 /* 14344 * This is to prevent an increase of less than 1 MSS of 14345 * tcp_cwnd. With partial increase, tcp_wput_data() 14346 * may send out tinygrams in order to preserve mblk 14347 * boundaries. 14348 * 14349 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14350 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14351 * increased by 1 MSS for every RTTs. 14352 */ 14353 if (tcp->tcp_cwnd_cnt <= 0) { 14354 tcp->tcp_cwnd_cnt = cwnd + add; 14355 } else { 14356 tcp->tcp_cwnd_cnt -= add; 14357 add = 0; 14358 } 14359 } 14360 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14361 } 14362 14363 /* See if the latest urgent data has been acknowledged */ 14364 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14365 SEQ_GT(seg_ack, tcp->tcp_urg)) 14366 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14367 14368 /* Can we update the RTT estimates? */ 14369 if (tcp->tcp_snd_ts_ok) { 14370 /* Ignore zero timestamp echo-reply. */ 14371 if (tcpopt.tcp_opt_ts_ecr != 0) { 14372 tcp_set_rto(tcp, (int32_t)lbolt - 14373 (int32_t)tcpopt.tcp_opt_ts_ecr); 14374 } 14375 14376 /* If needed, restart the timer. */ 14377 if (tcp->tcp_set_timer == 1) { 14378 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14379 tcp->tcp_set_timer = 0; 14380 } 14381 /* 14382 * Update tcp_csuna in case the other side stops sending 14383 * us timestamps. 14384 */ 14385 tcp->tcp_csuna = tcp->tcp_snxt; 14386 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14387 /* 14388 * An ACK sequence we haven't seen before, so get the RTT 14389 * and update the RTO. But first check if the timestamp is 14390 * valid to use. 14391 */ 14392 if ((mp1->b_next != NULL) && 14393 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14394 tcp_set_rto(tcp, (int32_t)lbolt - 14395 (int32_t)(intptr_t)mp1->b_prev); 14396 else 14397 BUMP_MIB(&tcp_mib, tcpRttNoUpdate); 14398 14399 /* Remeber the last sequence to be ACKed */ 14400 tcp->tcp_csuna = seg_ack; 14401 if (tcp->tcp_set_timer == 1) { 14402 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14403 tcp->tcp_set_timer = 0; 14404 } 14405 } else { 14406 BUMP_MIB(&tcp_mib, tcpRttNoUpdate); 14407 } 14408 14409 /* Eat acknowledged bytes off the xmit queue. */ 14410 for (;;) { 14411 mblk_t *mp2; 14412 uchar_t *wptr; 14413 14414 wptr = mp1->b_wptr; 14415 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14416 bytes_acked -= (int)(wptr - mp1->b_rptr); 14417 if (bytes_acked < 0) { 14418 mp1->b_rptr = wptr + bytes_acked; 14419 /* 14420 * Set a new timestamp if all the bytes timed by the 14421 * old timestamp have been ack'ed. 14422 */ 14423 if (SEQ_GT(seg_ack, 14424 (uint32_t)(uintptr_t)(mp1->b_next))) { 14425 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14426 mp1->b_next = NULL; 14427 } 14428 break; 14429 } 14430 mp1->b_next = NULL; 14431 mp1->b_prev = NULL; 14432 mp2 = mp1; 14433 mp1 = mp1->b_cont; 14434 14435 /* 14436 * This notification is required for some zero-copy 14437 * clients to maintain a copy semantic. After the data 14438 * is ack'ed, client is safe to modify or reuse the buffer. 14439 */ 14440 if (tcp->tcp_snd_zcopy_aware && 14441 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14442 tcp_zcopy_notify(tcp); 14443 freeb(mp2); 14444 if (bytes_acked == 0) { 14445 if (mp1 == NULL) { 14446 /* Everything is ack'ed, clear the tail. */ 14447 tcp->tcp_xmit_tail = NULL; 14448 /* 14449 * Cancel the timer unless we are still 14450 * waiting for an ACK for the FIN packet. 14451 */ 14452 if (tcp->tcp_timer_tid != 0 && 14453 tcp->tcp_snxt == tcp->tcp_suna) { 14454 (void) TCP_TIMER_CANCEL(tcp, 14455 tcp->tcp_timer_tid); 14456 tcp->tcp_timer_tid = 0; 14457 } 14458 goto pre_swnd_update; 14459 } 14460 if (mp2 != tcp->tcp_xmit_tail) 14461 break; 14462 tcp->tcp_xmit_tail = mp1; 14463 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14464 (uintptr_t)INT_MAX); 14465 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14466 mp1->b_rptr); 14467 break; 14468 } 14469 if (mp1 == NULL) { 14470 /* 14471 * More was acked but there is nothing more 14472 * outstanding. This means that the FIN was 14473 * just acked or that we're talking to a clown. 14474 */ 14475 fin_acked: 14476 ASSERT(tcp->tcp_fin_sent); 14477 tcp->tcp_xmit_tail = NULL; 14478 if (tcp->tcp_fin_sent) { 14479 /* FIN was acked - making progress */ 14480 if (tcp->tcp_ipversion == IPV6_VERSION && 14481 !tcp->tcp_fin_acked) 14482 tcp->tcp_ip_forward_progress = B_TRUE; 14483 tcp->tcp_fin_acked = B_TRUE; 14484 if (tcp->tcp_linger_tid != 0 && 14485 TCP_TIMER_CANCEL(tcp, 14486 tcp->tcp_linger_tid) >= 0) { 14487 tcp_stop_lingering(tcp); 14488 } 14489 } else { 14490 /* 14491 * We should never get here because 14492 * we have already checked that the 14493 * number of bytes ack'ed should be 14494 * smaller than or equal to what we 14495 * have sent so far (it is the 14496 * acceptability check of the ACK). 14497 * We can only get here if the send 14498 * queue is corrupted. 14499 * 14500 * Terminate the connection and 14501 * panic the system. It is better 14502 * for us to panic instead of 14503 * continuing to avoid other disaster. 14504 */ 14505 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14506 tcp->tcp_rnxt, TH_RST|TH_ACK); 14507 panic("Memory corruption " 14508 "detected for connection %s.", 14509 tcp_display(tcp, NULL, 14510 DISP_ADDR_AND_PORT)); 14511 /*NOTREACHED*/ 14512 } 14513 goto pre_swnd_update; 14514 } 14515 ASSERT(mp2 != tcp->tcp_xmit_tail); 14516 } 14517 if (tcp->tcp_unsent) { 14518 flags |= TH_XMIT_NEEDED; 14519 } 14520 pre_swnd_update: 14521 tcp->tcp_xmit_head = mp1; 14522 swnd_update: 14523 /* 14524 * The following check is different from most other implementations. 14525 * For bi-directional transfer, when segments are dropped, the 14526 * "normal" check will not accept a window update in those 14527 * retransmitted segemnts. Failing to do that, TCP may send out 14528 * segments which are outside receiver's window. As TCP accepts 14529 * the ack in those retransmitted segments, if the window update in 14530 * the same segment is not accepted, TCP will incorrectly calculates 14531 * that it can send more segments. This can create a deadlock 14532 * with the receiver if its window becomes zero. 14533 */ 14534 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14535 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14536 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14537 /* 14538 * The criteria for update is: 14539 * 14540 * 1. the segment acknowledges some data. Or 14541 * 2. the segment is new, i.e. it has a higher seq num. Or 14542 * 3. the segment is not old and the advertised window is 14543 * larger than the previous advertised window. 14544 */ 14545 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14546 flags |= TH_XMIT_NEEDED; 14547 tcp->tcp_swnd = new_swnd; 14548 if (new_swnd > tcp->tcp_max_swnd) 14549 tcp->tcp_max_swnd = new_swnd; 14550 tcp->tcp_swl1 = seg_seq; 14551 tcp->tcp_swl2 = seg_ack; 14552 } 14553 est: 14554 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14555 14556 switch (tcp->tcp_state) { 14557 case TCPS_FIN_WAIT_1: 14558 if (tcp->tcp_fin_acked) { 14559 tcp->tcp_state = TCPS_FIN_WAIT_2; 14560 /* 14561 * We implement the non-standard BSD/SunOS 14562 * FIN_WAIT_2 flushing algorithm. 14563 * If there is no user attached to this 14564 * TCP endpoint, then this TCP struct 14565 * could hang around forever in FIN_WAIT_2 14566 * state if the peer forgets to send us 14567 * a FIN. To prevent this, we wait only 14568 * 2*MSL (a convenient time value) for 14569 * the FIN to arrive. If it doesn't show up, 14570 * we flush the TCP endpoint. This algorithm, 14571 * though a violation of RFC-793, has worked 14572 * for over 10 years in BSD systems. 14573 * Note: SunOS 4.x waits 675 seconds before 14574 * flushing the FIN_WAIT_2 connection. 14575 */ 14576 TCP_TIMER_RESTART(tcp, 14577 tcp_fin_wait_2_flush_interval); 14578 } 14579 break; 14580 case TCPS_FIN_WAIT_2: 14581 break; /* Shutdown hook? */ 14582 case TCPS_LAST_ACK: 14583 freemsg(mp); 14584 if (tcp->tcp_fin_acked) { 14585 (void) tcp_clean_death(tcp, 0, 19); 14586 return; 14587 } 14588 goto xmit_check; 14589 case TCPS_CLOSING: 14590 if (tcp->tcp_fin_acked) { 14591 tcp->tcp_state = TCPS_TIME_WAIT; 14592 /* 14593 * Unconditionally clear the exclusive binding 14594 * bit so this TIME-WAIT connection won't 14595 * interfere with new ones. 14596 */ 14597 tcp->tcp_exclbind = 0; 14598 if (!TCP_IS_DETACHED(tcp)) { 14599 TCP_TIMER_RESTART(tcp, 14600 tcp_time_wait_interval); 14601 } else { 14602 tcp_time_wait_append(tcp); 14603 TCP_DBGSTAT(tcp_rput_time_wait); 14604 } 14605 } 14606 /*FALLTHRU*/ 14607 case TCPS_CLOSE_WAIT: 14608 freemsg(mp); 14609 goto xmit_check; 14610 default: 14611 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14612 break; 14613 } 14614 } 14615 if (flags & TH_FIN) { 14616 /* Make sure we ack the fin */ 14617 flags |= TH_ACK_NEEDED; 14618 if (!tcp->tcp_fin_rcvd) { 14619 tcp->tcp_fin_rcvd = B_TRUE; 14620 tcp->tcp_rnxt++; 14621 tcph = tcp->tcp_tcph; 14622 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14623 14624 /* 14625 * Generate the ordrel_ind at the end unless we 14626 * are an eager guy. 14627 * In the eager case tcp_rsrv will do this when run 14628 * after tcp_accept is done. 14629 */ 14630 if (tcp->tcp_listener == NULL && 14631 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14632 flags |= TH_ORDREL_NEEDED; 14633 switch (tcp->tcp_state) { 14634 case TCPS_SYN_RCVD: 14635 case TCPS_ESTABLISHED: 14636 tcp->tcp_state = TCPS_CLOSE_WAIT; 14637 /* Keepalive? */ 14638 break; 14639 case TCPS_FIN_WAIT_1: 14640 if (!tcp->tcp_fin_acked) { 14641 tcp->tcp_state = TCPS_CLOSING; 14642 break; 14643 } 14644 /* FALLTHRU */ 14645 case TCPS_FIN_WAIT_2: 14646 tcp->tcp_state = TCPS_TIME_WAIT; 14647 /* 14648 * Unconditionally clear the exclusive binding 14649 * bit so this TIME-WAIT connection won't 14650 * interfere with new ones. 14651 */ 14652 tcp->tcp_exclbind = 0; 14653 if (!TCP_IS_DETACHED(tcp)) { 14654 TCP_TIMER_RESTART(tcp, 14655 tcp_time_wait_interval); 14656 } else { 14657 tcp_time_wait_append(tcp); 14658 TCP_DBGSTAT(tcp_rput_time_wait); 14659 } 14660 if (seg_len) { 14661 /* 14662 * implies data piggybacked on FIN. 14663 * break to handle data. 14664 */ 14665 break; 14666 } 14667 freemsg(mp); 14668 goto ack_check; 14669 } 14670 } 14671 } 14672 if (mp == NULL) 14673 goto xmit_check; 14674 if (seg_len == 0) { 14675 freemsg(mp); 14676 goto xmit_check; 14677 } 14678 if (mp->b_rptr == mp->b_wptr) { 14679 /* 14680 * The header has been consumed, so we remove the 14681 * zero-length mblk here. 14682 */ 14683 mp1 = mp; 14684 mp = mp->b_cont; 14685 freeb(mp1); 14686 } 14687 tcph = tcp->tcp_tcph; 14688 tcp->tcp_rack_cnt++; 14689 { 14690 uint32_t cur_max; 14691 14692 cur_max = tcp->tcp_rack_cur_max; 14693 if (tcp->tcp_rack_cnt >= cur_max) { 14694 /* 14695 * We have more unacked data than we should - send 14696 * an ACK now. 14697 */ 14698 flags |= TH_ACK_NEEDED; 14699 cur_max++; 14700 if (cur_max > tcp->tcp_rack_abs_max) 14701 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14702 else 14703 tcp->tcp_rack_cur_max = cur_max; 14704 } else if (TCP_IS_DETACHED(tcp)) { 14705 /* We don't have an ACK timer for detached TCP. */ 14706 flags |= TH_ACK_NEEDED; 14707 } else if (seg_len < mss) { 14708 /* 14709 * If we get a segment that is less than an mss, and we 14710 * already have unacknowledged data, and the amount 14711 * unacknowledged is not a multiple of mss, then we 14712 * better generate an ACK now. Otherwise, this may be 14713 * the tail piece of a transaction, and we would rather 14714 * wait for the response. 14715 */ 14716 uint32_t udif; 14717 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14718 (uintptr_t)INT_MAX); 14719 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14720 if (udif && (udif % mss)) 14721 flags |= TH_ACK_NEEDED; 14722 else 14723 flags |= TH_ACK_TIMER_NEEDED; 14724 } else { 14725 /* Start delayed ack timer */ 14726 flags |= TH_ACK_TIMER_NEEDED; 14727 } 14728 } 14729 tcp->tcp_rnxt += seg_len; 14730 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14731 14732 /* Update SACK list */ 14733 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14734 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14735 &(tcp->tcp_num_sack_blk)); 14736 } 14737 14738 if (tcp->tcp_urp_mp) { 14739 tcp->tcp_urp_mp->b_cont = mp; 14740 mp = tcp->tcp_urp_mp; 14741 tcp->tcp_urp_mp = NULL; 14742 /* Ready for a new signal. */ 14743 tcp->tcp_urp_last_valid = B_FALSE; 14744 #ifdef DEBUG 14745 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14746 "tcp_rput: sending exdata_ind %s", 14747 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14748 #endif /* DEBUG */ 14749 } 14750 14751 /* 14752 * Check for ancillary data changes compared to last segment. 14753 */ 14754 if (tcp->tcp_ipv6_recvancillary != 0) { 14755 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14756 if (mp == NULL) 14757 return; 14758 } 14759 14760 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14761 /* 14762 * Side queue inbound data until the accept happens. 14763 * tcp_accept/tcp_rput drains this when the accept happens. 14764 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14765 * T_EXDATA_IND) it is queued on b_next. 14766 * XXX Make urgent data use this. Requires: 14767 * Removing tcp_listener check for TH_URG 14768 * Making M_PCPROTO and MARK messages skip the eager case 14769 */ 14770 14771 if (tcp->tcp_kssl_pending) { 14772 tcp_kssl_input(tcp, mp); 14773 } else { 14774 tcp_rcv_enqueue(tcp, mp, seg_len); 14775 } 14776 } else { 14777 if (mp->b_datap->db_type != M_DATA || 14778 (flags & TH_MARKNEXT_NEEDED)) { 14779 if (tcp->tcp_rcv_list != NULL) { 14780 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14781 } 14782 ASSERT(tcp->tcp_rcv_list == NULL || 14783 tcp->tcp_fused_sigurg); 14784 if (flags & TH_MARKNEXT_NEEDED) { 14785 #ifdef DEBUG 14786 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14787 "tcp_rput: sending MSGMARKNEXT %s", 14788 tcp_display(tcp, NULL, 14789 DISP_PORT_ONLY)); 14790 #endif /* DEBUG */ 14791 mp->b_flag |= MSGMARKNEXT; 14792 flags &= ~TH_MARKNEXT_NEEDED; 14793 } 14794 14795 /* Does this need SSL processing first? */ 14796 if ((tcp->tcp_kssl_ctx != NULL) && 14797 (DB_TYPE(mp) == M_DATA)) { 14798 tcp_kssl_input(tcp, mp); 14799 } else { 14800 putnext(tcp->tcp_rq, mp); 14801 if (!canputnext(tcp->tcp_rq)) 14802 tcp->tcp_rwnd -= seg_len; 14803 } 14804 } else if ((flags & (TH_PUSH|TH_FIN)) || 14805 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 14806 if (tcp->tcp_rcv_list != NULL) { 14807 /* 14808 * Enqueue the new segment first and then 14809 * call tcp_rcv_drain() to send all data 14810 * up. The other way to do this is to 14811 * send all queued data up and then call 14812 * putnext() to send the new segment up. 14813 * This way can remove the else part later 14814 * on. 14815 * 14816 * We don't this to avoid one more call to 14817 * canputnext() as tcp_rcv_drain() needs to 14818 * call canputnext(). 14819 */ 14820 tcp_rcv_enqueue(tcp, mp, seg_len); 14821 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14822 } else { 14823 /* Does this need SSL processing first? */ 14824 if ((tcp->tcp_kssl_ctx != NULL) && 14825 (DB_TYPE(mp) == M_DATA)) { 14826 tcp_kssl_input(tcp, mp); 14827 } else { 14828 putnext(tcp->tcp_rq, mp); 14829 if (!canputnext(tcp->tcp_rq)) 14830 tcp->tcp_rwnd -= seg_len; 14831 } 14832 } 14833 } else { 14834 /* 14835 * Enqueue all packets when processing an mblk 14836 * from the co queue and also enqueue normal packets. 14837 */ 14838 tcp_rcv_enqueue(tcp, mp, seg_len); 14839 } 14840 /* 14841 * Make sure the timer is running if we have data waiting 14842 * for a push bit. This provides resiliency against 14843 * implementations that do not correctly generate push bits. 14844 */ 14845 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 14846 /* 14847 * The connection may be closed at this point, so don't 14848 * do anything for a detached tcp. 14849 */ 14850 if (!TCP_IS_DETACHED(tcp)) 14851 tcp->tcp_push_tid = TCP_TIMER(tcp, 14852 tcp_push_timer, 14853 MSEC_TO_TICK(tcp_push_timer_interval)); 14854 } 14855 } 14856 xmit_check: 14857 /* Is there anything left to do? */ 14858 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14859 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 14860 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 14861 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14862 goto done; 14863 14864 /* Any transmit work to do and a non-zero window? */ 14865 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 14866 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 14867 if (flags & TH_REXMIT_NEEDED) { 14868 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 14869 14870 BUMP_MIB(&tcp_mib, tcpOutFastRetrans); 14871 if (snd_size > mss) 14872 snd_size = mss; 14873 if (snd_size > tcp->tcp_swnd) 14874 snd_size = tcp->tcp_swnd; 14875 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 14876 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 14877 B_TRUE); 14878 14879 if (mp1 != NULL) { 14880 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14881 tcp->tcp_csuna = tcp->tcp_snxt; 14882 BUMP_MIB(&tcp_mib, tcpRetransSegs); 14883 UPDATE_MIB(&tcp_mib, tcpRetransBytes, snd_size); 14884 TCP_RECORD_TRACE(tcp, mp1, 14885 TCP_TRACE_SEND_PKT); 14886 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14887 } 14888 } 14889 if (flags & TH_NEED_SACK_REXMIT) { 14890 tcp_sack_rxmit(tcp, &flags); 14891 } 14892 /* 14893 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 14894 * out new segment. Note that tcp_rexmit should not be 14895 * set, otherwise TH_LIMIT_XMIT should not be set. 14896 */ 14897 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 14898 if (!tcp->tcp_rexmit) { 14899 tcp_wput_data(tcp, NULL, B_FALSE); 14900 } else { 14901 tcp_ss_rexmit(tcp); 14902 } 14903 } 14904 /* 14905 * Adjust tcp_cwnd back to normal value after sending 14906 * new data segments. 14907 */ 14908 if (flags & TH_LIMIT_XMIT) { 14909 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 14910 /* 14911 * This will restart the timer. Restarting the 14912 * timer is used to avoid a timeout before the 14913 * limited transmitted segment's ACK gets back. 14914 */ 14915 if (tcp->tcp_xmit_head != NULL) 14916 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14917 } 14918 14919 /* Anything more to do? */ 14920 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 14921 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14922 goto done; 14923 } 14924 ack_check: 14925 if (flags & TH_SEND_URP_MARK) { 14926 ASSERT(tcp->tcp_urp_mark_mp); 14927 /* 14928 * Send up any queued data and then send the mark message 14929 */ 14930 if (tcp->tcp_rcv_list != NULL) { 14931 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14932 } 14933 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14934 14935 mp1 = tcp->tcp_urp_mark_mp; 14936 tcp->tcp_urp_mark_mp = NULL; 14937 #ifdef DEBUG 14938 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14939 "tcp_rput: sending zero-length %s %s", 14940 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 14941 "MSGNOTMARKNEXT"), 14942 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14943 #endif /* DEBUG */ 14944 putnext(tcp->tcp_rq, mp1); 14945 flags &= ~TH_SEND_URP_MARK; 14946 } 14947 if (flags & TH_ACK_NEEDED) { 14948 /* 14949 * Time to send an ack for some reason. 14950 */ 14951 mp1 = tcp_ack_mp(tcp); 14952 14953 if (mp1 != NULL) { 14954 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 14955 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14956 BUMP_LOCAL(tcp->tcp_obsegs); 14957 BUMP_MIB(&tcp_mib, tcpOutAck); 14958 } 14959 if (tcp->tcp_ack_tid != 0) { 14960 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 14961 tcp->tcp_ack_tid = 0; 14962 } 14963 } 14964 if (flags & TH_ACK_TIMER_NEEDED) { 14965 /* 14966 * Arrange for deferred ACK or push wait timeout. 14967 * Start timer if it is not already running. 14968 */ 14969 if (tcp->tcp_ack_tid == 0) { 14970 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 14971 MSEC_TO_TICK(tcp->tcp_localnet ? 14972 (clock_t)tcp_local_dack_interval : 14973 (clock_t)tcp_deferred_ack_interval)); 14974 } 14975 } 14976 if (flags & TH_ORDREL_NEEDED) { 14977 /* 14978 * Send up the ordrel_ind unless we are an eager guy. 14979 * In the eager case tcp_rsrv will do this when run 14980 * after tcp_accept is done. 14981 */ 14982 ASSERT(tcp->tcp_listener == NULL); 14983 if (tcp->tcp_rcv_list != NULL) { 14984 /* 14985 * Push any mblk(s) enqueued from co processing. 14986 */ 14987 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14988 } 14989 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14990 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 14991 tcp->tcp_ordrel_done = B_TRUE; 14992 putnext(tcp->tcp_rq, mp1); 14993 if (tcp->tcp_deferred_clean_death) { 14994 /* 14995 * tcp_clean_death was deferred 14996 * for T_ORDREL_IND - do it now 14997 */ 14998 (void) tcp_clean_death(tcp, 14999 tcp->tcp_client_errno, 20); 15000 tcp->tcp_deferred_clean_death = B_FALSE; 15001 } 15002 } else { 15003 /* 15004 * Run the orderly release in the 15005 * service routine. 15006 */ 15007 qenable(tcp->tcp_rq); 15008 /* 15009 * Caveat(XXX): The machine may be so 15010 * overloaded that tcp_rsrv() is not scheduled 15011 * until after the endpoint has transitioned 15012 * to TCPS_TIME_WAIT 15013 * and tcp_time_wait_interval expires. Then 15014 * tcp_timer() will blow away state in tcp_t 15015 * and T_ORDREL_IND will never be delivered 15016 * upstream. Unlikely but potentially 15017 * a problem. 15018 */ 15019 } 15020 } 15021 done: 15022 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15023 } 15024 15025 /* 15026 * This function does PAWS protection check. Returns B_TRUE if the 15027 * segment passes the PAWS test, else returns B_FALSE. 15028 */ 15029 boolean_t 15030 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15031 { 15032 uint8_t flags; 15033 int options; 15034 uint8_t *up; 15035 15036 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15037 /* 15038 * If timestamp option is aligned nicely, get values inline, 15039 * otherwise call general routine to parse. Only do that 15040 * if timestamp is the only option. 15041 */ 15042 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15043 TCPOPT_REAL_TS_LEN && 15044 OK_32PTR((up = ((uint8_t *)tcph) + 15045 TCP_MIN_HEADER_LENGTH)) && 15046 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15047 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15048 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15049 15050 options = TCP_OPT_TSTAMP_PRESENT; 15051 } else { 15052 if (tcp->tcp_snd_sack_ok) { 15053 tcpoptp->tcp = tcp; 15054 } else { 15055 tcpoptp->tcp = NULL; 15056 } 15057 options = tcp_parse_options(tcph, tcpoptp); 15058 } 15059 15060 if (options & TCP_OPT_TSTAMP_PRESENT) { 15061 /* 15062 * Do PAWS per RFC 1323 section 4.2. Accept RST 15063 * regardless of the timestamp, page 18 RFC 1323.bis. 15064 */ 15065 if ((flags & TH_RST) == 0 && 15066 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15067 tcp->tcp_ts_recent)) { 15068 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15069 PAWS_TIMEOUT)) { 15070 /* This segment is not acceptable. */ 15071 return (B_FALSE); 15072 } else { 15073 /* 15074 * Connection has been idle for 15075 * too long. Reset the timestamp 15076 * and assume the segment is valid. 15077 */ 15078 tcp->tcp_ts_recent = 15079 tcpoptp->tcp_opt_ts_val; 15080 } 15081 } 15082 } else { 15083 /* 15084 * If we don't get a timestamp on every packet, we 15085 * figure we can't really trust 'em, so we stop sending 15086 * and parsing them. 15087 */ 15088 tcp->tcp_snd_ts_ok = B_FALSE; 15089 15090 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15091 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15092 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15093 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN); 15094 if (tcp->tcp_snd_sack_ok) { 15095 ASSERT(tcp->tcp_sack_info != NULL); 15096 tcp->tcp_max_sack_blk = 4; 15097 } 15098 } 15099 return (B_TRUE); 15100 } 15101 15102 /* 15103 * Attach ancillary data to a received TCP segments for the 15104 * ancillary pieces requested by the application that are 15105 * different than they were in the previous data segment. 15106 * 15107 * Save the "current" values once memory allocation is ok so that 15108 * when memory allocation fails we can just wait for the next data segment. 15109 */ 15110 static mblk_t * 15111 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15112 { 15113 struct T_optdata_ind *todi; 15114 int optlen; 15115 uchar_t *optptr; 15116 struct T_opthdr *toh; 15117 uint_t addflag; /* Which pieces to add */ 15118 mblk_t *mp1; 15119 15120 optlen = 0; 15121 addflag = 0; 15122 /* If app asked for pktinfo and the index has changed ... */ 15123 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15124 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15125 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15126 optlen += sizeof (struct T_opthdr) + 15127 sizeof (struct in6_pktinfo); 15128 addflag |= TCP_IPV6_RECVPKTINFO; 15129 } 15130 /* If app asked for hoplimit and it has changed ... */ 15131 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15132 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15133 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15134 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15135 addflag |= TCP_IPV6_RECVHOPLIMIT; 15136 } 15137 /* If app asked for tclass and it has changed ... */ 15138 if ((ipp->ipp_fields & IPPF_TCLASS) && 15139 ipp->ipp_tclass != tcp->tcp_recvtclass && 15140 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15141 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15142 addflag |= TCP_IPV6_RECVTCLASS; 15143 } 15144 /* 15145 * If app asked for hopbyhop headers and it has changed ... 15146 * For security labels, note that (1) security labels can't change on 15147 * a connected socket at all, (2) we're connected to at most one peer, 15148 * (3) if anything changes, then it must be some other extra option. 15149 */ 15150 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15151 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15152 (ipp->ipp_fields & IPPF_HOPOPTS), 15153 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15154 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15155 tcp->tcp_label_len; 15156 addflag |= TCP_IPV6_RECVHOPOPTS; 15157 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15158 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15159 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15160 return (mp); 15161 } 15162 /* If app asked for dst headers before routing headers ... */ 15163 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15164 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15165 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15166 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15167 optlen += sizeof (struct T_opthdr) + 15168 ipp->ipp_rtdstoptslen; 15169 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15170 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15171 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15172 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15173 return (mp); 15174 } 15175 /* If app asked for routing headers and it has changed ... */ 15176 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15177 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15178 (ipp->ipp_fields & IPPF_RTHDR), 15179 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15180 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15181 addflag |= TCP_IPV6_RECVRTHDR; 15182 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15183 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15184 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15185 return (mp); 15186 } 15187 /* If app asked for dest headers and it has changed ... */ 15188 if ((tcp->tcp_ipv6_recvancillary & 15189 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15190 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15191 (ipp->ipp_fields & IPPF_DSTOPTS), 15192 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15193 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15194 addflag |= TCP_IPV6_RECVDSTOPTS; 15195 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15196 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15197 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15198 return (mp); 15199 } 15200 15201 if (optlen == 0) { 15202 /* Nothing to add */ 15203 return (mp); 15204 } 15205 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15206 if (mp1 == NULL) { 15207 /* 15208 * Defer sending ancillary data until the next TCP segment 15209 * arrives. 15210 */ 15211 return (mp); 15212 } 15213 mp1->b_cont = mp; 15214 mp = mp1; 15215 mp->b_wptr += sizeof (*todi) + optlen; 15216 mp->b_datap->db_type = M_PROTO; 15217 todi = (struct T_optdata_ind *)mp->b_rptr; 15218 todi->PRIM_type = T_OPTDATA_IND; 15219 todi->DATA_flag = 1; /* MORE data */ 15220 todi->OPT_length = optlen; 15221 todi->OPT_offset = sizeof (*todi); 15222 optptr = (uchar_t *)&todi[1]; 15223 /* 15224 * If app asked for pktinfo and the index has changed ... 15225 * Note that the local address never changes for the connection. 15226 */ 15227 if (addflag & TCP_IPV6_RECVPKTINFO) { 15228 struct in6_pktinfo *pkti; 15229 15230 toh = (struct T_opthdr *)optptr; 15231 toh->level = IPPROTO_IPV6; 15232 toh->name = IPV6_PKTINFO; 15233 toh->len = sizeof (*toh) + sizeof (*pkti); 15234 toh->status = 0; 15235 optptr += sizeof (*toh); 15236 pkti = (struct in6_pktinfo *)optptr; 15237 if (tcp->tcp_ipversion == IPV6_VERSION) 15238 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15239 else 15240 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15241 &pkti->ipi6_addr); 15242 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15243 optptr += sizeof (*pkti); 15244 ASSERT(OK_32PTR(optptr)); 15245 /* Save as "last" value */ 15246 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15247 } 15248 /* If app asked for hoplimit and it has changed ... */ 15249 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15250 toh = (struct T_opthdr *)optptr; 15251 toh->level = IPPROTO_IPV6; 15252 toh->name = IPV6_HOPLIMIT; 15253 toh->len = sizeof (*toh) + sizeof (uint_t); 15254 toh->status = 0; 15255 optptr += sizeof (*toh); 15256 *(uint_t *)optptr = ipp->ipp_hoplimit; 15257 optptr += sizeof (uint_t); 15258 ASSERT(OK_32PTR(optptr)); 15259 /* Save as "last" value */ 15260 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15261 } 15262 /* If app asked for tclass and it has changed ... */ 15263 if (addflag & TCP_IPV6_RECVTCLASS) { 15264 toh = (struct T_opthdr *)optptr; 15265 toh->level = IPPROTO_IPV6; 15266 toh->name = IPV6_TCLASS; 15267 toh->len = sizeof (*toh) + sizeof (uint_t); 15268 toh->status = 0; 15269 optptr += sizeof (*toh); 15270 *(uint_t *)optptr = ipp->ipp_tclass; 15271 optptr += sizeof (uint_t); 15272 ASSERT(OK_32PTR(optptr)); 15273 /* Save as "last" value */ 15274 tcp->tcp_recvtclass = ipp->ipp_tclass; 15275 } 15276 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15277 toh = (struct T_opthdr *)optptr; 15278 toh->level = IPPROTO_IPV6; 15279 toh->name = IPV6_HOPOPTS; 15280 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15281 tcp->tcp_label_len; 15282 toh->status = 0; 15283 optptr += sizeof (*toh); 15284 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15285 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15286 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15287 ASSERT(OK_32PTR(optptr)); 15288 /* Save as last value */ 15289 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15290 (ipp->ipp_fields & IPPF_HOPOPTS), 15291 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15292 } 15293 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15294 toh = (struct T_opthdr *)optptr; 15295 toh->level = IPPROTO_IPV6; 15296 toh->name = IPV6_RTHDRDSTOPTS; 15297 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15298 toh->status = 0; 15299 optptr += sizeof (*toh); 15300 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15301 optptr += ipp->ipp_rtdstoptslen; 15302 ASSERT(OK_32PTR(optptr)); 15303 /* Save as last value */ 15304 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15305 &tcp->tcp_rtdstoptslen, 15306 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15307 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15308 } 15309 if (addflag & TCP_IPV6_RECVRTHDR) { 15310 toh = (struct T_opthdr *)optptr; 15311 toh->level = IPPROTO_IPV6; 15312 toh->name = IPV6_RTHDR; 15313 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15314 toh->status = 0; 15315 optptr += sizeof (*toh); 15316 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15317 optptr += ipp->ipp_rthdrlen; 15318 ASSERT(OK_32PTR(optptr)); 15319 /* Save as last value */ 15320 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15321 (ipp->ipp_fields & IPPF_RTHDR), 15322 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15323 } 15324 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15325 toh = (struct T_opthdr *)optptr; 15326 toh->level = IPPROTO_IPV6; 15327 toh->name = IPV6_DSTOPTS; 15328 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15329 toh->status = 0; 15330 optptr += sizeof (*toh); 15331 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15332 optptr += ipp->ipp_dstoptslen; 15333 ASSERT(OK_32PTR(optptr)); 15334 /* Save as last value */ 15335 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15336 (ipp->ipp_fields & IPPF_DSTOPTS), 15337 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15338 } 15339 ASSERT(optptr == mp->b_wptr); 15340 return (mp); 15341 } 15342 15343 15344 /* 15345 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15346 * or a "bad" IRE detected by tcp_adapt_ire. 15347 * We can't tell if the failure was due to the laddr or the faddr 15348 * thus we clear out all addresses and ports. 15349 */ 15350 static void 15351 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15352 { 15353 queue_t *q = tcp->tcp_rq; 15354 tcph_t *tcph; 15355 struct T_error_ack *tea; 15356 conn_t *connp = tcp->tcp_connp; 15357 15358 15359 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15360 15361 if (mp->b_cont) { 15362 freemsg(mp->b_cont); 15363 mp->b_cont = NULL; 15364 } 15365 tea = (struct T_error_ack *)mp->b_rptr; 15366 switch (tea->PRIM_type) { 15367 case T_BIND_ACK: 15368 /* 15369 * Need to unbind with classifier since we were just told that 15370 * our bind succeeded. 15371 */ 15372 tcp->tcp_hard_bound = B_FALSE; 15373 tcp->tcp_hard_binding = B_FALSE; 15374 15375 ipcl_hash_remove(connp); 15376 /* Reuse the mblk if possible */ 15377 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15378 sizeof (*tea)); 15379 mp->b_rptr = mp->b_datap->db_base; 15380 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15381 tea = (struct T_error_ack *)mp->b_rptr; 15382 tea->PRIM_type = T_ERROR_ACK; 15383 tea->TLI_error = TSYSERR; 15384 tea->UNIX_error = error; 15385 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15386 tea->ERROR_prim = T_CONN_REQ; 15387 } else { 15388 tea->ERROR_prim = O_T_BIND_REQ; 15389 } 15390 break; 15391 15392 case T_ERROR_ACK: 15393 if (tcp->tcp_state >= TCPS_SYN_SENT) 15394 tea->ERROR_prim = T_CONN_REQ; 15395 break; 15396 default: 15397 panic("tcp_bind_failed: unexpected TPI type"); 15398 /*NOTREACHED*/ 15399 } 15400 15401 tcp->tcp_state = TCPS_IDLE; 15402 if (tcp->tcp_ipversion == IPV4_VERSION) 15403 tcp->tcp_ipha->ipha_src = 0; 15404 else 15405 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15406 /* 15407 * Copy of the src addr. in tcp_t is needed since 15408 * the lookup funcs. can only look at tcp_t 15409 */ 15410 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15411 15412 tcph = tcp->tcp_tcph; 15413 tcph->th_lport[0] = 0; 15414 tcph->th_lport[1] = 0; 15415 tcp_bind_hash_remove(tcp); 15416 bzero(&connp->u_port, sizeof (connp->u_port)); 15417 /* blow away saved option results if any */ 15418 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15419 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15420 15421 conn_delete_ire(tcp->tcp_connp, NULL); 15422 putnext(q, mp); 15423 } 15424 15425 /* 15426 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15427 * messages. 15428 */ 15429 void 15430 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15431 { 15432 mblk_t *mp1; 15433 uchar_t *rptr = mp->b_rptr; 15434 queue_t *q = tcp->tcp_rq; 15435 struct T_error_ack *tea; 15436 uint32_t mss; 15437 mblk_t *syn_mp; 15438 mblk_t *mdti; 15439 int retval; 15440 mblk_t *ire_mp; 15441 15442 switch (mp->b_datap->db_type) { 15443 case M_PROTO: 15444 case M_PCPROTO: 15445 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15446 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15447 break; 15448 tea = (struct T_error_ack *)rptr; 15449 switch (tea->PRIM_type) { 15450 case T_BIND_ACK: 15451 /* 15452 * Adapt Multidata information, if any. The 15453 * following tcp_mdt_update routine will free 15454 * the message. 15455 */ 15456 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15457 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15458 b_rptr)->mdt_capab, B_TRUE); 15459 freemsg(mdti); 15460 } 15461 15462 /* Get the IRE, if we had requested for it */ 15463 ire_mp = tcp_ire_mp(mp); 15464 15465 if (tcp->tcp_hard_binding) { 15466 tcp->tcp_hard_binding = B_FALSE; 15467 tcp->tcp_hard_bound = B_TRUE; 15468 CL_INET_CONNECT(tcp); 15469 } else { 15470 if (ire_mp != NULL) 15471 freeb(ire_mp); 15472 goto after_syn_sent; 15473 } 15474 15475 retval = tcp_adapt_ire(tcp, ire_mp); 15476 if (ire_mp != NULL) 15477 freeb(ire_mp); 15478 if (retval == 0) { 15479 tcp_bind_failed(tcp, mp, 15480 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15481 ENETUNREACH : EADDRNOTAVAIL)); 15482 return; 15483 } 15484 /* 15485 * Don't let an endpoint connect to itself. 15486 * Also checked in tcp_connect() but that 15487 * check can't handle the case when the 15488 * local IP address is INADDR_ANY. 15489 */ 15490 if (tcp->tcp_ipversion == IPV4_VERSION) { 15491 if ((tcp->tcp_ipha->ipha_dst == 15492 tcp->tcp_ipha->ipha_src) && 15493 (BE16_EQL(tcp->tcp_tcph->th_lport, 15494 tcp->tcp_tcph->th_fport))) { 15495 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15496 return; 15497 } 15498 } else { 15499 if (IN6_ARE_ADDR_EQUAL( 15500 &tcp->tcp_ip6h->ip6_dst, 15501 &tcp->tcp_ip6h->ip6_src) && 15502 (BE16_EQL(tcp->tcp_tcph->th_lport, 15503 tcp->tcp_tcph->th_fport))) { 15504 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15505 return; 15506 } 15507 } 15508 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15509 /* 15510 * This should not be possible! Just for 15511 * defensive coding... 15512 */ 15513 if (tcp->tcp_state != TCPS_SYN_SENT) 15514 goto after_syn_sent; 15515 15516 if (is_system_labeled() && 15517 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 15518 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 15519 return; 15520 } 15521 15522 ASSERT(q == tcp->tcp_rq); 15523 /* 15524 * tcp_adapt_ire() does not adjust 15525 * for TCP/IP header length. 15526 */ 15527 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15528 15529 /* 15530 * Just make sure our rwnd is at 15531 * least tcp_recv_hiwat_mss * MSS 15532 * large, and round up to the nearest 15533 * MSS. 15534 * 15535 * We do the round up here because 15536 * we need to get the interface 15537 * MTU first before we can do the 15538 * round up. 15539 */ 15540 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15541 tcp_recv_hiwat_minmss * mss); 15542 q->q_hiwat = tcp->tcp_rwnd; 15543 tcp_set_ws_value(tcp); 15544 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15545 tcp->tcp_tcph->th_win); 15546 if (tcp->tcp_rcv_ws > 0 || tcp_wscale_always) 15547 tcp->tcp_snd_ws_ok = B_TRUE; 15548 15549 /* 15550 * Set tcp_snd_ts_ok to true 15551 * so that tcp_xmit_mp will 15552 * include the timestamp 15553 * option in the SYN segment. 15554 */ 15555 if (tcp_tstamp_always || 15556 (tcp->tcp_rcv_ws && tcp_tstamp_if_wscale)) { 15557 tcp->tcp_snd_ts_ok = B_TRUE; 15558 } 15559 15560 /* 15561 * tcp_snd_sack_ok can be set in 15562 * tcp_adapt_ire() if the sack metric 15563 * is set. So check it here also. 15564 */ 15565 if (tcp_sack_permitted == 2 || 15566 tcp->tcp_snd_sack_ok) { 15567 if (tcp->tcp_sack_info == NULL) { 15568 tcp->tcp_sack_info = 15569 kmem_cache_alloc(tcp_sack_info_cache, 15570 KM_SLEEP); 15571 } 15572 tcp->tcp_snd_sack_ok = B_TRUE; 15573 } 15574 15575 /* 15576 * Should we use ECN? Note that the current 15577 * default value (SunOS 5.9) of tcp_ecn_permitted 15578 * is 1. The reason for doing this is that there 15579 * are equipments out there that will drop ECN 15580 * enabled IP packets. Setting it to 1 avoids 15581 * compatibility problems. 15582 */ 15583 if (tcp_ecn_permitted == 2) 15584 tcp->tcp_ecn_ok = B_TRUE; 15585 15586 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15587 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15588 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15589 if (syn_mp) { 15590 cred_t *cr; 15591 pid_t pid; 15592 15593 /* 15594 * Obtain the credential from the 15595 * thread calling connect(); the credential 15596 * lives on in the second mblk which 15597 * originated from T_CONN_REQ and is echoed 15598 * with the T_BIND_ACK from ip. If none 15599 * can be found, default to the creator 15600 * of the socket. 15601 */ 15602 if (mp->b_cont == NULL || 15603 (cr = DB_CRED(mp->b_cont)) == NULL) { 15604 cr = tcp->tcp_cred; 15605 pid = tcp->tcp_cpid; 15606 } else { 15607 pid = DB_CPID(mp->b_cont); 15608 } 15609 15610 TCP_RECORD_TRACE(tcp, syn_mp, 15611 TCP_TRACE_SEND_PKT); 15612 mblk_setcred(syn_mp, cr); 15613 DB_CPID(syn_mp) = pid; 15614 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15615 } 15616 after_syn_sent: 15617 /* 15618 * A trailer mblk indicates a waiting client upstream. 15619 * We complete here the processing begun in 15620 * either tcp_bind() or tcp_connect() by passing 15621 * upstream the reply message they supplied. 15622 */ 15623 mp1 = mp; 15624 mp = mp->b_cont; 15625 freeb(mp1); 15626 if (mp) 15627 break; 15628 return; 15629 case T_ERROR_ACK: 15630 if (tcp->tcp_debug) { 15631 (void) strlog(TCP_MOD_ID, 0, 1, 15632 SL_TRACE|SL_ERROR, 15633 "tcp_rput_other: case T_ERROR_ACK, " 15634 "ERROR_prim == %d", 15635 tea->ERROR_prim); 15636 } 15637 switch (tea->ERROR_prim) { 15638 case O_T_BIND_REQ: 15639 case T_BIND_REQ: 15640 tcp_bind_failed(tcp, mp, 15641 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15642 ENETUNREACH : EADDRNOTAVAIL)); 15643 return; 15644 case T_UNBIND_REQ: 15645 tcp->tcp_hard_binding = B_FALSE; 15646 tcp->tcp_hard_bound = B_FALSE; 15647 if (mp->b_cont) { 15648 freemsg(mp->b_cont); 15649 mp->b_cont = NULL; 15650 } 15651 if (tcp->tcp_unbind_pending) 15652 tcp->tcp_unbind_pending = 0; 15653 else { 15654 /* From tcp_ip_unbind() - free */ 15655 freemsg(mp); 15656 return; 15657 } 15658 break; 15659 case T_SVR4_OPTMGMT_REQ: 15660 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15661 /* T_OPTMGMT_REQ generated by TCP */ 15662 printf("T_SVR4_OPTMGMT_REQ failed " 15663 "%d/%d - dropped (cnt %d)\n", 15664 tea->TLI_error, tea->UNIX_error, 15665 tcp->tcp_drop_opt_ack_cnt); 15666 freemsg(mp); 15667 tcp->tcp_drop_opt_ack_cnt--; 15668 return; 15669 } 15670 break; 15671 } 15672 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15673 tcp->tcp_drop_opt_ack_cnt > 0) { 15674 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15675 "- dropped (cnt %d)\n", 15676 tea->TLI_error, tea->UNIX_error, 15677 tcp->tcp_drop_opt_ack_cnt); 15678 freemsg(mp); 15679 tcp->tcp_drop_opt_ack_cnt--; 15680 return; 15681 } 15682 break; 15683 case T_OPTMGMT_ACK: 15684 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15685 /* T_OPTMGMT_REQ generated by TCP */ 15686 freemsg(mp); 15687 tcp->tcp_drop_opt_ack_cnt--; 15688 return; 15689 } 15690 break; 15691 default: 15692 break; 15693 } 15694 break; 15695 case M_CTL: 15696 /* 15697 * ICMP messages. 15698 */ 15699 tcp_icmp_error(tcp, mp); 15700 return; 15701 case M_FLUSH: 15702 if (*rptr & FLUSHR) 15703 flushq(q, FLUSHDATA); 15704 break; 15705 default: 15706 break; 15707 } 15708 /* 15709 * Make sure we set this bit before sending the ACK for 15710 * bind. Otherwise accept could possibly run and free 15711 * this tcp struct. 15712 */ 15713 putnext(q, mp); 15714 } 15715 15716 /* 15717 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15718 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15719 * tcp_rsrv() try again. 15720 */ 15721 static void 15722 tcp_ordrel_kick(void *arg) 15723 { 15724 conn_t *connp = (conn_t *)arg; 15725 tcp_t *tcp = connp->conn_tcp; 15726 15727 tcp->tcp_ordrelid = 0; 15728 tcp->tcp_timeout = B_FALSE; 15729 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15730 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15731 qenable(tcp->tcp_rq); 15732 } 15733 } 15734 15735 /* ARGSUSED */ 15736 static void 15737 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15738 { 15739 conn_t *connp = (conn_t *)arg; 15740 tcp_t *tcp = connp->conn_tcp; 15741 queue_t *q = tcp->tcp_rq; 15742 uint_t thwin; 15743 15744 freeb(mp); 15745 15746 TCP_STAT(tcp_rsrv_calls); 15747 15748 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15749 return; 15750 } 15751 15752 if (tcp->tcp_fused) { 15753 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15754 15755 ASSERT(tcp->tcp_fused); 15756 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15757 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15758 ASSERT(!TCP_IS_DETACHED(tcp)); 15759 ASSERT(tcp->tcp_connp->conn_sqp == 15760 peer_tcp->tcp_connp->conn_sqp); 15761 15762 /* 15763 * Normally we would not get backenabled in synchronous 15764 * streams mode, but in case this happens, we need to plug 15765 * synchronous streams during our drain to prevent a race 15766 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15767 */ 15768 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15769 if (tcp->tcp_rcv_list != NULL) 15770 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15771 15772 tcp_clrqfull(peer_tcp); 15773 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15774 TCP_STAT(tcp_fusion_backenabled); 15775 return; 15776 } 15777 15778 if (canputnext(q)) { 15779 tcp->tcp_rwnd = q->q_hiwat; 15780 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15781 << tcp->tcp_rcv_ws; 15782 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15783 /* 15784 * Send back a window update immediately if TCP is above 15785 * ESTABLISHED state and the increase of the rcv window 15786 * that the other side knows is at least 1 MSS after flow 15787 * control is lifted. 15788 */ 15789 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15790 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15791 tcp_xmit_ctl(NULL, tcp, 15792 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15793 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15794 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 15795 } 15796 } 15797 /* Handle a failure to allocate a T_ORDREL_IND here */ 15798 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15799 ASSERT(tcp->tcp_listener == NULL); 15800 if (tcp->tcp_rcv_list != NULL) { 15801 (void) tcp_rcv_drain(q, tcp); 15802 } 15803 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15804 mp = mi_tpi_ordrel_ind(); 15805 if (mp) { 15806 tcp->tcp_ordrel_done = B_TRUE; 15807 putnext(q, mp); 15808 if (tcp->tcp_deferred_clean_death) { 15809 /* 15810 * tcp_clean_death was deferred for 15811 * T_ORDREL_IND - do it now 15812 */ 15813 tcp->tcp_deferred_clean_death = B_FALSE; 15814 (void) tcp_clean_death(tcp, 15815 tcp->tcp_client_errno, 22); 15816 } 15817 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15818 /* 15819 * If there isn't already a timer running 15820 * start one. Use a 4 second 15821 * timer as a fallback since it can't fail. 15822 */ 15823 tcp->tcp_timeout = B_TRUE; 15824 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15825 MSEC_TO_TICK(4000)); 15826 } 15827 } 15828 } 15829 15830 /* 15831 * The read side service routine is called mostly when we get back-enabled as a 15832 * result of flow control relief. Since we don't actually queue anything in 15833 * TCP, we have no data to send out of here. What we do is clear the receive 15834 * window, and send out a window update. 15835 * This routine is also called to drive an orderly release message upstream 15836 * if the attempt in tcp_rput failed. 15837 */ 15838 static void 15839 tcp_rsrv(queue_t *q) 15840 { 15841 conn_t *connp = Q_TO_CONN(q); 15842 tcp_t *tcp = connp->conn_tcp; 15843 mblk_t *mp; 15844 15845 /* No code does a putq on the read side */ 15846 ASSERT(q->q_first == NULL); 15847 15848 /* Nothing to do for the default queue */ 15849 if (q == tcp_g_q) { 15850 return; 15851 } 15852 15853 mp = allocb(0, BPRI_HI); 15854 if (mp == NULL) { 15855 /* 15856 * We are under memory pressure. Return for now and we 15857 * we will be called again later. 15858 */ 15859 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15860 /* 15861 * If there isn't already a timer running 15862 * start one. Use a 4 second 15863 * timer as a fallback since it can't fail. 15864 */ 15865 tcp->tcp_timeout = B_TRUE; 15866 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15867 MSEC_TO_TICK(4000)); 15868 } 15869 return; 15870 } 15871 CONN_INC_REF(connp); 15872 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15873 SQTAG_TCP_RSRV); 15874 } 15875 15876 /* 15877 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15878 * We do not allow the receive window to shrink. After setting rwnd, 15879 * set the flow control hiwat of the stream. 15880 * 15881 * This function is called in 2 cases: 15882 * 15883 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15884 * connection (passive open) and in tcp_rput_data() for active connect. 15885 * This is called after tcp_mss_set() when the desired MSS value is known. 15886 * This makes sure that our window size is a mutiple of the other side's 15887 * MSS. 15888 * 2) Handling SO_RCVBUF option. 15889 * 15890 * It is ASSUMED that the requested size is a multiple of the current MSS. 15891 * 15892 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15893 * user requests so. 15894 */ 15895 static int 15896 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15897 { 15898 uint32_t mss = tcp->tcp_mss; 15899 uint32_t old_max_rwnd; 15900 uint32_t max_transmittable_rwnd; 15901 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15902 15903 if (tcp->tcp_fused) { 15904 size_t sth_hiwat; 15905 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15906 15907 ASSERT(peer_tcp != NULL); 15908 /* 15909 * Record the stream head's high water mark for 15910 * this endpoint; this is used for flow-control 15911 * purposes in tcp_fuse_output(). 15912 */ 15913 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15914 if (!tcp_detached) 15915 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 15916 15917 /* 15918 * In the fusion case, the maxpsz stream head value of 15919 * our peer is set according to its send buffer size 15920 * and our receive buffer size; since the latter may 15921 * have changed we need to update the peer's maxpsz. 15922 */ 15923 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15924 return (rwnd); 15925 } 15926 15927 if (tcp_detached) 15928 old_max_rwnd = tcp->tcp_rwnd; 15929 else 15930 old_max_rwnd = tcp->tcp_rq->q_hiwat; 15931 15932 /* 15933 * Insist on a receive window that is at least 15934 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15935 * funny TCP interactions of Nagle algorithm, SWS avoidance 15936 * and delayed acknowledgement. 15937 */ 15938 rwnd = MAX(rwnd, tcp_recv_hiwat_minmss * mss); 15939 15940 /* 15941 * If window size info has already been exchanged, TCP should not 15942 * shrink the window. Shrinking window is doable if done carefully. 15943 * We may add that support later. But so far there is not a real 15944 * need to do that. 15945 */ 15946 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15947 /* MSS may have changed, do a round up again. */ 15948 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15949 } 15950 15951 /* 15952 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15953 * can be applied even before the window scale option is decided. 15954 */ 15955 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15956 if (rwnd > max_transmittable_rwnd) { 15957 rwnd = max_transmittable_rwnd - 15958 (max_transmittable_rwnd % mss); 15959 if (rwnd < mss) 15960 rwnd = max_transmittable_rwnd; 15961 /* 15962 * If we're over the limit we may have to back down tcp_rwnd. 15963 * The increment below won't work for us. So we set all three 15964 * here and the increment below will have no effect. 15965 */ 15966 tcp->tcp_rwnd = old_max_rwnd = rwnd; 15967 } 15968 if (tcp->tcp_localnet) { 15969 tcp->tcp_rack_abs_max = 15970 MIN(tcp_local_dacks_max, rwnd / mss / 2); 15971 } else { 15972 /* 15973 * For a remote host on a different subnet (through a router), 15974 * we ack every other packet to be conforming to RFC1122. 15975 * tcp_deferred_acks_max is default to 2. 15976 */ 15977 tcp->tcp_rack_abs_max = 15978 MIN(tcp_deferred_acks_max, rwnd / mss / 2); 15979 } 15980 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 15981 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15982 else 15983 tcp->tcp_rack_cur_max = 0; 15984 /* 15985 * Increment the current rwnd by the amount the maximum grew (we 15986 * can not overwrite it since we might be in the middle of a 15987 * connection.) 15988 */ 15989 tcp->tcp_rwnd += rwnd - old_max_rwnd; 15990 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 15991 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 15992 tcp->tcp_cwnd_max = rwnd; 15993 15994 if (tcp_detached) 15995 return (rwnd); 15996 /* 15997 * We set the maximum receive window into rq->q_hiwat. 15998 * This is not actually used for flow control. 15999 */ 16000 tcp->tcp_rq->q_hiwat = rwnd; 16001 /* 16002 * Set the Stream head high water mark. This doesn't have to be 16003 * here, since we are simply using default values, but we would 16004 * prefer to choose these values algorithmically, with a likely 16005 * relationship to rwnd. 16006 */ 16007 (void) mi_set_sth_hiwat(tcp->tcp_rq, MAX(rwnd, tcp_sth_rcv_hiwat)); 16008 return (rwnd); 16009 } 16010 16011 /* 16012 * Return SNMP stuff in buffer in mpdata. 16013 */ 16014 int 16015 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16016 { 16017 mblk_t *mpdata; 16018 mblk_t *mp_conn_ctl = NULL; 16019 mblk_t *mp_conn_tail; 16020 mblk_t *mp_attr_ctl = NULL; 16021 mblk_t *mp_attr_tail; 16022 mblk_t *mp6_conn_ctl = NULL; 16023 mblk_t *mp6_conn_tail; 16024 mblk_t *mp6_attr_ctl = NULL; 16025 mblk_t *mp6_attr_tail; 16026 struct opthdr *optp; 16027 mib2_tcpConnEntry_t tce; 16028 mib2_tcp6ConnEntry_t tce6; 16029 mib2_transportMLPEntry_t mlp; 16030 connf_t *connfp; 16031 conn_t *connp; 16032 int i; 16033 boolean_t ispriv; 16034 zoneid_t zoneid; 16035 int v4_conn_idx; 16036 int v6_conn_idx; 16037 16038 if (mpctl == NULL || 16039 (mpdata = mpctl->b_cont) == NULL || 16040 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16041 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16042 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16043 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16044 freemsg(mp_conn_ctl); 16045 freemsg(mp_attr_ctl); 16046 freemsg(mp6_conn_ctl); 16047 freemsg(mp6_attr_ctl); 16048 return (0); 16049 } 16050 16051 /* build table of connections -- need count in fixed part */ 16052 SET_MIB(tcp_mib.tcpRtoAlgorithm, 4); /* vanj */ 16053 SET_MIB(tcp_mib.tcpRtoMin, tcp_rexmit_interval_min); 16054 SET_MIB(tcp_mib.tcpRtoMax, tcp_rexmit_interval_max); 16055 SET_MIB(tcp_mib.tcpMaxConn, -1); 16056 SET_MIB(tcp_mib.tcpCurrEstab, 0); 16057 16058 ispriv = 16059 secpolicy_net_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16060 zoneid = Q_TO_CONN(q)->conn_zoneid; 16061 16062 v4_conn_idx = v6_conn_idx = 0; 16063 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16064 16065 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16066 16067 connfp = &ipcl_globalhash_fanout[i]; 16068 16069 connp = NULL; 16070 16071 while ((connp = 16072 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16073 tcp_t *tcp; 16074 boolean_t needattr; 16075 16076 if (connp->conn_zoneid != zoneid) 16077 continue; /* not in this zone */ 16078 16079 tcp = connp->conn_tcp; 16080 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 16081 tcp->tcp_ibsegs = 0; 16082 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 16083 tcp->tcp_obsegs = 0; 16084 16085 tce6.tcp6ConnState = tce.tcpConnState = 16086 tcp_snmp_state(tcp); 16087 if (tce.tcpConnState == MIB2_TCP_established || 16088 tce.tcpConnState == MIB2_TCP_closeWait) 16089 BUMP_MIB(&tcp_mib, tcpCurrEstab); 16090 16091 needattr = B_FALSE; 16092 bzero(&mlp, sizeof (mlp)); 16093 if (connp->conn_mlp_type != mlptSingle) { 16094 if (connp->conn_mlp_type == mlptShared || 16095 connp->conn_mlp_type == mlptBoth) 16096 mlp.tme_flags |= MIB2_TMEF_SHARED; 16097 if (connp->conn_mlp_type == mlptPrivate || 16098 connp->conn_mlp_type == mlptBoth) 16099 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16100 needattr = B_TRUE; 16101 } 16102 if (connp->conn_peercred != NULL) { 16103 ts_label_t *tsl; 16104 16105 tsl = crgetlabel(connp->conn_peercred); 16106 mlp.tme_doi = label2doi(tsl); 16107 mlp.tme_label = *label2bslabel(tsl); 16108 needattr = B_TRUE; 16109 } 16110 16111 /* Create a message to report on IPv6 entries */ 16112 if (tcp->tcp_ipversion == IPV6_VERSION) { 16113 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16114 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16115 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16116 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16117 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16118 /* Don't want just anybody seeing these... */ 16119 if (ispriv) { 16120 tce6.tcp6ConnEntryInfo.ce_snxt = 16121 tcp->tcp_snxt; 16122 tce6.tcp6ConnEntryInfo.ce_suna = 16123 tcp->tcp_suna; 16124 tce6.tcp6ConnEntryInfo.ce_rnxt = 16125 tcp->tcp_rnxt; 16126 tce6.tcp6ConnEntryInfo.ce_rack = 16127 tcp->tcp_rack; 16128 } else { 16129 /* 16130 * Netstat, unfortunately, uses this to 16131 * get send/receive queue sizes. How to fix? 16132 * Why not compute the difference only? 16133 */ 16134 tce6.tcp6ConnEntryInfo.ce_snxt = 16135 tcp->tcp_snxt - tcp->tcp_suna; 16136 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16137 tce6.tcp6ConnEntryInfo.ce_rnxt = 16138 tcp->tcp_rnxt - tcp->tcp_rack; 16139 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16140 } 16141 16142 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16143 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16144 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16145 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16146 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16147 16148 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16149 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16150 16151 mlp.tme_connidx = v6_conn_idx++; 16152 if (needattr) 16153 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16154 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16155 } 16156 /* 16157 * Create an IPv4 table entry for IPv4 entries and also 16158 * for IPv6 entries which are bound to in6addr_any 16159 * but don't have IPV6_V6ONLY set. 16160 * (i.e. anything an IPv4 peer could connect to) 16161 */ 16162 if (tcp->tcp_ipversion == IPV4_VERSION || 16163 (tcp->tcp_state <= TCPS_LISTEN && 16164 !tcp->tcp_connp->conn_ipv6_v6only && 16165 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16166 if (tcp->tcp_ipversion == IPV6_VERSION) { 16167 tce.tcpConnRemAddress = INADDR_ANY; 16168 tce.tcpConnLocalAddress = INADDR_ANY; 16169 } else { 16170 tce.tcpConnRemAddress = 16171 tcp->tcp_remote; 16172 tce.tcpConnLocalAddress = 16173 tcp->tcp_ip_src; 16174 } 16175 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16176 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16177 /* Don't want just anybody seeing these... */ 16178 if (ispriv) { 16179 tce.tcpConnEntryInfo.ce_snxt = 16180 tcp->tcp_snxt; 16181 tce.tcpConnEntryInfo.ce_suna = 16182 tcp->tcp_suna; 16183 tce.tcpConnEntryInfo.ce_rnxt = 16184 tcp->tcp_rnxt; 16185 tce.tcpConnEntryInfo.ce_rack = 16186 tcp->tcp_rack; 16187 } else { 16188 /* 16189 * Netstat, unfortunately, uses this to 16190 * get send/receive queue sizes. How 16191 * to fix? 16192 * Why not compute the difference only? 16193 */ 16194 tce.tcpConnEntryInfo.ce_snxt = 16195 tcp->tcp_snxt - tcp->tcp_suna; 16196 tce.tcpConnEntryInfo.ce_suna = 0; 16197 tce.tcpConnEntryInfo.ce_rnxt = 16198 tcp->tcp_rnxt - tcp->tcp_rack; 16199 tce.tcpConnEntryInfo.ce_rack = 0; 16200 } 16201 16202 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16203 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16204 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16205 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16206 tce.tcpConnEntryInfo.ce_state = 16207 tcp->tcp_state; 16208 16209 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16210 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16211 16212 mlp.tme_connidx = v4_conn_idx++; 16213 if (needattr) 16214 (void) snmp_append_data2( 16215 mp_attr_ctl->b_cont, 16216 &mp_attr_tail, (char *)&mlp, 16217 sizeof (mlp)); 16218 } 16219 } 16220 } 16221 16222 /* fixed length structure for IPv4 and IPv6 counters */ 16223 SET_MIB(tcp_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16224 SET_MIB(tcp_mib.tcp6ConnTableSize, sizeof (mib2_tcp6ConnEntry_t)); 16225 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16226 optp->level = MIB2_TCP; 16227 optp->name = 0; 16228 (void) snmp_append_data(mpdata, (char *)&tcp_mib, sizeof (tcp_mib)); 16229 optp->len = msgdsize(mpdata); 16230 qreply(q, mpctl); 16231 16232 /* table of connections... */ 16233 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16234 sizeof (struct T_optmgmt_ack)]; 16235 optp->level = MIB2_TCP; 16236 optp->name = MIB2_TCP_CONN; 16237 optp->len = msgdsize(mp_conn_ctl->b_cont); 16238 qreply(q, mp_conn_ctl); 16239 16240 /* table of MLP attributes... */ 16241 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16242 sizeof (struct T_optmgmt_ack)]; 16243 optp->level = MIB2_TCP; 16244 optp->name = EXPER_XPORT_MLP; 16245 optp->len = msgdsize(mp_attr_ctl->b_cont); 16246 if (optp->len == 0) 16247 freemsg(mp_attr_ctl); 16248 else 16249 qreply(q, mp_attr_ctl); 16250 16251 /* table of IPv6 connections... */ 16252 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16253 sizeof (struct T_optmgmt_ack)]; 16254 optp->level = MIB2_TCP6; 16255 optp->name = MIB2_TCP6_CONN; 16256 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16257 qreply(q, mp6_conn_ctl); 16258 16259 /* table of IPv6 MLP attributes... */ 16260 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16261 sizeof (struct T_optmgmt_ack)]; 16262 optp->level = MIB2_TCP6; 16263 optp->name = EXPER_XPORT_MLP; 16264 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16265 if (optp->len == 0) 16266 freemsg(mp6_attr_ctl); 16267 else 16268 qreply(q, mp6_attr_ctl); 16269 return (1); 16270 } 16271 16272 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16273 /* ARGSUSED */ 16274 int 16275 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16276 { 16277 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16278 16279 switch (level) { 16280 case MIB2_TCP: 16281 switch (name) { 16282 case 13: 16283 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16284 return (0); 16285 /* TODO: delete entry defined by tce */ 16286 return (1); 16287 default: 16288 return (0); 16289 } 16290 default: 16291 return (1); 16292 } 16293 } 16294 16295 /* Translate TCP state to MIB2 TCP state. */ 16296 static int 16297 tcp_snmp_state(tcp_t *tcp) 16298 { 16299 if (tcp == NULL) 16300 return (0); 16301 16302 switch (tcp->tcp_state) { 16303 case TCPS_CLOSED: 16304 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16305 case TCPS_BOUND: 16306 return (MIB2_TCP_closed); 16307 case TCPS_LISTEN: 16308 return (MIB2_TCP_listen); 16309 case TCPS_SYN_SENT: 16310 return (MIB2_TCP_synSent); 16311 case TCPS_SYN_RCVD: 16312 return (MIB2_TCP_synReceived); 16313 case TCPS_ESTABLISHED: 16314 return (MIB2_TCP_established); 16315 case TCPS_CLOSE_WAIT: 16316 return (MIB2_TCP_closeWait); 16317 case TCPS_FIN_WAIT_1: 16318 return (MIB2_TCP_finWait1); 16319 case TCPS_CLOSING: 16320 return (MIB2_TCP_closing); 16321 case TCPS_LAST_ACK: 16322 return (MIB2_TCP_lastAck); 16323 case TCPS_FIN_WAIT_2: 16324 return (MIB2_TCP_finWait2); 16325 case TCPS_TIME_WAIT: 16326 return (MIB2_TCP_timeWait); 16327 default: 16328 return (0); 16329 } 16330 } 16331 16332 static char tcp_report_header[] = 16333 "TCP " MI_COL_HDRPAD_STR 16334 "zone dest snxt suna " 16335 "swnd rnxt rack rwnd rto mss w sw rw t " 16336 "recent [lport,fport] state"; 16337 16338 /* 16339 * TCP status report triggered via the Named Dispatch mechanism. 16340 */ 16341 /* ARGSUSED */ 16342 static void 16343 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16344 cred_t *cr) 16345 { 16346 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16347 boolean_t ispriv = secpolicy_net_config(cr, B_TRUE) == 0; 16348 char cflag; 16349 in6_addr_t v6dst; 16350 char buf[80]; 16351 uint_t print_len, buf_len; 16352 16353 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16354 if (buf_len <= 0) 16355 return; 16356 16357 if (hashval >= 0) 16358 (void) sprintf(hash, "%03d ", hashval); 16359 else 16360 hash[0] = '\0'; 16361 16362 /* 16363 * Note that we use the remote address in the tcp_b structure. 16364 * This means that it will print out the real destination address, 16365 * not the next hop's address if source routing is used. This 16366 * avoid the confusion on the output because user may not 16367 * know that source routing is used for a connection. 16368 */ 16369 if (tcp->tcp_ipversion == IPV4_VERSION) { 16370 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16371 } else { 16372 v6dst = tcp->tcp_remote_v6; 16373 } 16374 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16375 /* 16376 * the ispriv checks are so that normal users cannot determine 16377 * sequence number information using NDD. 16378 */ 16379 16380 if (TCP_IS_DETACHED(tcp)) 16381 cflag = '*'; 16382 else 16383 cflag = ' '; 16384 print_len = snprintf((char *)mp->b_wptr, buf_len, 16385 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16386 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16387 hash, 16388 (void *)tcp, 16389 tcp->tcp_connp->conn_zoneid, 16390 addrbuf, 16391 (ispriv) ? tcp->tcp_snxt : 0, 16392 (ispriv) ? tcp->tcp_suna : 0, 16393 tcp->tcp_swnd, 16394 (ispriv) ? tcp->tcp_rnxt : 0, 16395 (ispriv) ? tcp->tcp_rack : 0, 16396 tcp->tcp_rwnd, 16397 tcp->tcp_rto, 16398 tcp->tcp_mss, 16399 tcp->tcp_snd_ws_ok, 16400 tcp->tcp_snd_ws, 16401 tcp->tcp_rcv_ws, 16402 tcp->tcp_snd_ts_ok, 16403 tcp->tcp_ts_recent, 16404 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16405 if (print_len < buf_len) { 16406 ((mblk_t *)mp)->b_wptr += print_len; 16407 } else { 16408 ((mblk_t *)mp)->b_wptr += buf_len; 16409 } 16410 } 16411 16412 /* 16413 * TCP status report (for listeners only) triggered via the Named Dispatch 16414 * mechanism. 16415 */ 16416 /* ARGSUSED */ 16417 static void 16418 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16419 { 16420 char addrbuf[INET6_ADDRSTRLEN]; 16421 in6_addr_t v6dst; 16422 uint_t print_len, buf_len; 16423 16424 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16425 if (buf_len <= 0) 16426 return; 16427 16428 if (tcp->tcp_ipversion == IPV4_VERSION) { 16429 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16430 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16431 } else { 16432 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16433 addrbuf, sizeof (addrbuf)); 16434 } 16435 print_len = snprintf((char *)mp->b_wptr, buf_len, 16436 "%03d " 16437 MI_COL_PTRFMT_STR 16438 "%d %s %05u %08u %d/%d/%d%c\n", 16439 hashval, (void *)tcp, 16440 tcp->tcp_connp->conn_zoneid, 16441 addrbuf, 16442 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16443 tcp->tcp_conn_req_seqnum, 16444 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16445 tcp->tcp_conn_req_max, 16446 tcp->tcp_syn_defense ? '*' : ' '); 16447 if (print_len < buf_len) { 16448 ((mblk_t *)mp)->b_wptr += print_len; 16449 } else { 16450 ((mblk_t *)mp)->b_wptr += buf_len; 16451 } 16452 } 16453 16454 /* TCP status report triggered via the Named Dispatch mechanism. */ 16455 /* ARGSUSED */ 16456 static int 16457 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16458 { 16459 tcp_t *tcp; 16460 int i; 16461 conn_t *connp; 16462 connf_t *connfp; 16463 zoneid_t zoneid; 16464 16465 /* 16466 * Because of the ndd constraint, at most we can have 64K buffer 16467 * to put in all TCP info. So to be more efficient, just 16468 * allocate a 64K buffer here, assuming we need that large buffer. 16469 * This may be a problem as any user can read tcp_status. Therefore 16470 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16471 * This should be OK as normal users should not do this too often. 16472 */ 16473 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16474 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16475 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16476 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16477 return (0); 16478 } 16479 } 16480 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16481 /* The following may work even if we cannot get a large buf. */ 16482 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16483 return (0); 16484 } 16485 16486 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16487 16488 zoneid = Q_TO_CONN(q)->conn_zoneid; 16489 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16490 16491 connfp = &ipcl_globalhash_fanout[i]; 16492 16493 connp = NULL; 16494 16495 while ((connp = 16496 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16497 tcp = connp->conn_tcp; 16498 if (zoneid != GLOBAL_ZONEID && 16499 zoneid != connp->conn_zoneid) 16500 continue; 16501 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16502 cr); 16503 } 16504 16505 } 16506 16507 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16508 return (0); 16509 } 16510 16511 /* TCP status report triggered via the Named Dispatch mechanism. */ 16512 /* ARGSUSED */ 16513 static int 16514 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16515 { 16516 tf_t *tbf; 16517 tcp_t *tcp; 16518 int i; 16519 zoneid_t zoneid; 16520 16521 /* Refer to comments in tcp_status_report(). */ 16522 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16523 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16524 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16525 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16526 return (0); 16527 } 16528 } 16529 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16530 /* The following may work even if we cannot get a large buf. */ 16531 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16532 return (0); 16533 } 16534 16535 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16536 16537 zoneid = Q_TO_CONN(q)->conn_zoneid; 16538 16539 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 16540 tbf = &tcp_bind_fanout[i]; 16541 mutex_enter(&tbf->tf_lock); 16542 for (tcp = tbf->tf_tcp; tcp != NULL; 16543 tcp = tcp->tcp_bind_hash) { 16544 if (zoneid != GLOBAL_ZONEID && 16545 zoneid != tcp->tcp_connp->conn_zoneid) 16546 continue; 16547 CONN_INC_REF(tcp->tcp_connp); 16548 tcp_report_item(mp->b_cont, tcp, i, 16549 Q_TO_TCP(q), cr); 16550 CONN_DEC_REF(tcp->tcp_connp); 16551 } 16552 mutex_exit(&tbf->tf_lock); 16553 } 16554 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16555 return (0); 16556 } 16557 16558 /* TCP status report triggered via the Named Dispatch mechanism. */ 16559 /* ARGSUSED */ 16560 static int 16561 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16562 { 16563 connf_t *connfp; 16564 conn_t *connp; 16565 tcp_t *tcp; 16566 int i; 16567 zoneid_t zoneid; 16568 16569 /* Refer to comments in tcp_status_report(). */ 16570 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16571 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16572 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16573 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16574 return (0); 16575 } 16576 } 16577 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16578 /* The following may work even if we cannot get a large buf. */ 16579 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16580 return (0); 16581 } 16582 16583 (void) mi_mpprintf(mp, 16584 " TCP " MI_COL_HDRPAD_STR 16585 "zone IP addr port seqnum backlog (q0/q/max)"); 16586 16587 zoneid = Q_TO_CONN(q)->conn_zoneid; 16588 16589 for (i = 0; i < ipcl_bind_fanout_size; i++) { 16590 connfp = &ipcl_bind_fanout[i]; 16591 connp = NULL; 16592 while ((connp = 16593 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16594 tcp = connp->conn_tcp; 16595 if (zoneid != GLOBAL_ZONEID && 16596 zoneid != connp->conn_zoneid) 16597 continue; 16598 tcp_report_listener(mp->b_cont, tcp, i); 16599 } 16600 } 16601 16602 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16603 return (0); 16604 } 16605 16606 /* TCP status report triggered via the Named Dispatch mechanism. */ 16607 /* ARGSUSED */ 16608 static int 16609 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16610 { 16611 connf_t *connfp; 16612 conn_t *connp; 16613 tcp_t *tcp; 16614 int i; 16615 zoneid_t zoneid; 16616 16617 /* Refer to comments in tcp_status_report(). */ 16618 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16619 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16620 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16621 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16622 return (0); 16623 } 16624 } 16625 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16626 /* The following may work even if we cannot get a large buf. */ 16627 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16628 return (0); 16629 } 16630 16631 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16632 ipcl_conn_fanout_size); 16633 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16634 16635 zoneid = Q_TO_CONN(q)->conn_zoneid; 16636 16637 for (i = 0; i < ipcl_conn_fanout_size; i++) { 16638 connfp = &ipcl_conn_fanout[i]; 16639 connp = NULL; 16640 while ((connp = 16641 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16642 tcp = connp->conn_tcp; 16643 if (zoneid != GLOBAL_ZONEID && 16644 zoneid != connp->conn_zoneid) 16645 continue; 16646 tcp_report_item(mp->b_cont, tcp, i, 16647 Q_TO_TCP(q), cr); 16648 } 16649 } 16650 16651 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16652 return (0); 16653 } 16654 16655 /* TCP status report triggered via the Named Dispatch mechanism. */ 16656 /* ARGSUSED */ 16657 static int 16658 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16659 { 16660 tf_t *tf; 16661 tcp_t *tcp; 16662 int i; 16663 zoneid_t zoneid; 16664 16665 /* Refer to comments in tcp_status_report(). */ 16666 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16667 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16668 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16669 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16670 return (0); 16671 } 16672 } 16673 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16674 /* The following may work even if we cannot get a large buf. */ 16675 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16676 return (0); 16677 } 16678 16679 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16680 16681 zoneid = Q_TO_CONN(q)->conn_zoneid; 16682 16683 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 16684 tf = &tcp_acceptor_fanout[i]; 16685 mutex_enter(&tf->tf_lock); 16686 for (tcp = tf->tf_tcp; tcp != NULL; 16687 tcp = tcp->tcp_acceptor_hash) { 16688 if (zoneid != GLOBAL_ZONEID && 16689 zoneid != tcp->tcp_connp->conn_zoneid) 16690 continue; 16691 tcp_report_item(mp->b_cont, tcp, i, 16692 Q_TO_TCP(q), cr); 16693 } 16694 mutex_exit(&tf->tf_lock); 16695 } 16696 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16697 return (0); 16698 } 16699 16700 /* 16701 * tcp_timer is the timer service routine. It handles the retransmission, 16702 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16703 * from the state of the tcp instance what kind of action needs to be done 16704 * at the time it is called. 16705 */ 16706 static void 16707 tcp_timer(void *arg) 16708 { 16709 mblk_t *mp; 16710 clock_t first_threshold; 16711 clock_t second_threshold; 16712 clock_t ms; 16713 uint32_t mss; 16714 conn_t *connp = (conn_t *)arg; 16715 tcp_t *tcp = connp->conn_tcp; 16716 16717 tcp->tcp_timer_tid = 0; 16718 16719 if (tcp->tcp_fused) 16720 return; 16721 16722 first_threshold = tcp->tcp_first_timer_threshold; 16723 second_threshold = tcp->tcp_second_timer_threshold; 16724 switch (tcp->tcp_state) { 16725 case TCPS_IDLE: 16726 case TCPS_BOUND: 16727 case TCPS_LISTEN: 16728 return; 16729 case TCPS_SYN_RCVD: { 16730 tcp_t *listener = tcp->tcp_listener; 16731 16732 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16733 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16734 /* it's our first timeout */ 16735 tcp->tcp_syn_rcvd_timeout = 1; 16736 mutex_enter(&listener->tcp_eager_lock); 16737 listener->tcp_syn_rcvd_timeout++; 16738 if (!tcp->tcp_dontdrop && tcp->tcp_closemp_used == 0) { 16739 /* 16740 * Make this eager available for drop if we 16741 * need to drop one to accomodate a new 16742 * incoming SYN request. 16743 */ 16744 MAKE_DROPPABLE(listener, tcp); 16745 } 16746 if (!listener->tcp_syn_defense && 16747 (listener->tcp_syn_rcvd_timeout > 16748 (tcp_conn_req_max_q0 >> 2)) && 16749 (tcp_conn_req_max_q0 > 200)) { 16750 /* We may be under attack. Put on a defense. */ 16751 listener->tcp_syn_defense = B_TRUE; 16752 cmn_err(CE_WARN, "High TCP connect timeout " 16753 "rate! System (port %d) may be under a " 16754 "SYN flood attack!", 16755 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16756 16757 listener->tcp_ip_addr_cache = kmem_zalloc( 16758 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16759 KM_NOSLEEP); 16760 } 16761 mutex_exit(&listener->tcp_eager_lock); 16762 } else if (listener != NULL) { 16763 mutex_enter(&listener->tcp_eager_lock); 16764 tcp->tcp_syn_rcvd_timeout++; 16765 if (tcp->tcp_syn_rcvd_timeout > 1 && 16766 tcp->tcp_closemp_used == 0) { 16767 /* 16768 * This is our second timeout. Put the tcp in 16769 * the list of droppable eagers to allow it to 16770 * be dropped, if needed. We don't check 16771 * whether tcp_dontdrop is set or not to 16772 * protect ourselve from a SYN attack where a 16773 * remote host can spoof itself as one of the 16774 * good IP source and continue to hold 16775 * resources too long. 16776 */ 16777 MAKE_DROPPABLE(listener, tcp); 16778 } 16779 mutex_exit(&listener->tcp_eager_lock); 16780 } 16781 } 16782 /* FALLTHRU */ 16783 case TCPS_SYN_SENT: 16784 first_threshold = tcp->tcp_first_ctimer_threshold; 16785 second_threshold = tcp->tcp_second_ctimer_threshold; 16786 break; 16787 case TCPS_ESTABLISHED: 16788 case TCPS_FIN_WAIT_1: 16789 case TCPS_CLOSING: 16790 case TCPS_CLOSE_WAIT: 16791 case TCPS_LAST_ACK: 16792 /* If we have data to rexmit */ 16793 if (tcp->tcp_suna != tcp->tcp_snxt) { 16794 clock_t time_to_wait; 16795 16796 BUMP_MIB(&tcp_mib, tcpTimRetrans); 16797 if (!tcp->tcp_xmit_head) 16798 break; 16799 time_to_wait = lbolt - 16800 (clock_t)tcp->tcp_xmit_head->b_prev; 16801 time_to_wait = tcp->tcp_rto - 16802 TICK_TO_MSEC(time_to_wait); 16803 /* 16804 * If the timer fires too early, 1 clock tick earlier, 16805 * restart the timer. 16806 */ 16807 if (time_to_wait > msec_per_tick) { 16808 TCP_STAT(tcp_timer_fire_early); 16809 TCP_TIMER_RESTART(tcp, time_to_wait); 16810 return; 16811 } 16812 /* 16813 * When we probe zero windows, we force the swnd open. 16814 * If our peer acks with a closed window swnd will be 16815 * set to zero by tcp_rput(). As long as we are 16816 * receiving acks tcp_rput will 16817 * reset 'tcp_ms_we_have_waited' so as not to trip the 16818 * first and second interval actions. NOTE: the timer 16819 * interval is allowed to continue its exponential 16820 * backoff. 16821 */ 16822 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16823 if (tcp->tcp_debug) { 16824 (void) strlog(TCP_MOD_ID, 0, 1, 16825 SL_TRACE, "tcp_timer: zero win"); 16826 } 16827 } else { 16828 /* 16829 * After retransmission, we need to do 16830 * slow start. Set the ssthresh to one 16831 * half of current effective window and 16832 * cwnd to one MSS. Also reset 16833 * tcp_cwnd_cnt. 16834 * 16835 * Note that if tcp_ssthresh is reduced because 16836 * of ECN, do not reduce it again unless it is 16837 * already one window of data away (tcp_cwr 16838 * should then be cleared) or this is a 16839 * timeout for a retransmitted segment. 16840 */ 16841 uint32_t npkt; 16842 16843 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16844 npkt = ((tcp->tcp_timer_backoff ? 16845 tcp->tcp_cwnd_ssthresh : 16846 tcp->tcp_snxt - 16847 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16848 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16849 tcp->tcp_mss; 16850 } 16851 tcp->tcp_cwnd = tcp->tcp_mss; 16852 tcp->tcp_cwnd_cnt = 0; 16853 if (tcp->tcp_ecn_ok) { 16854 tcp->tcp_cwr = B_TRUE; 16855 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16856 tcp->tcp_ecn_cwr_sent = B_FALSE; 16857 } 16858 } 16859 break; 16860 } 16861 /* 16862 * We have something to send yet we cannot send. The 16863 * reason can be: 16864 * 16865 * 1. Zero send window: we need to do zero window probe. 16866 * 2. Zero cwnd: because of ECN, we need to "clock out 16867 * segments. 16868 * 3. SWS avoidance: receiver may have shrunk window, 16869 * reset our knowledge. 16870 * 16871 * Note that condition 2 can happen with either 1 or 16872 * 3. But 1 and 3 are exclusive. 16873 */ 16874 if (tcp->tcp_unsent != 0) { 16875 if (tcp->tcp_cwnd == 0) { 16876 /* 16877 * Set tcp_cwnd to 1 MSS so that a 16878 * new segment can be sent out. We 16879 * are "clocking out" new data when 16880 * the network is really congested. 16881 */ 16882 ASSERT(tcp->tcp_ecn_ok); 16883 tcp->tcp_cwnd = tcp->tcp_mss; 16884 } 16885 if (tcp->tcp_swnd == 0) { 16886 /* Extend window for zero window probe */ 16887 tcp->tcp_swnd++; 16888 tcp->tcp_zero_win_probe = B_TRUE; 16889 BUMP_MIB(&tcp_mib, tcpOutWinProbe); 16890 } else { 16891 /* 16892 * Handle timeout from sender SWS avoidance. 16893 * Reset our knowledge of the max send window 16894 * since the receiver might have reduced its 16895 * receive buffer. Avoid setting tcp_max_swnd 16896 * to one since that will essentially disable 16897 * the SWS checks. 16898 * 16899 * Note that since we don't have a SWS 16900 * state variable, if the timeout is set 16901 * for ECN but not for SWS, this 16902 * code will also be executed. This is 16903 * fine as tcp_max_swnd is updated 16904 * constantly and it will not affect 16905 * anything. 16906 */ 16907 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16908 } 16909 tcp_wput_data(tcp, NULL, B_FALSE); 16910 return; 16911 } 16912 /* Is there a FIN that needs to be to re retransmitted? */ 16913 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16914 !tcp->tcp_fin_acked) 16915 break; 16916 /* Nothing to do, return without restarting timer. */ 16917 TCP_STAT(tcp_timer_fire_miss); 16918 return; 16919 case TCPS_FIN_WAIT_2: 16920 /* 16921 * User closed the TCP endpoint and peer ACK'ed our FIN. 16922 * We waited some time for for peer's FIN, but it hasn't 16923 * arrived. We flush the connection now to avoid 16924 * case where the peer has rebooted. 16925 */ 16926 if (TCP_IS_DETACHED(tcp)) { 16927 (void) tcp_clean_death(tcp, 0, 23); 16928 } else { 16929 TCP_TIMER_RESTART(tcp, tcp_fin_wait_2_flush_interval); 16930 } 16931 return; 16932 case TCPS_TIME_WAIT: 16933 (void) tcp_clean_death(tcp, 0, 24); 16934 return; 16935 default: 16936 if (tcp->tcp_debug) { 16937 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16938 "tcp_timer: strange state (%d) %s", 16939 tcp->tcp_state, tcp_display(tcp, NULL, 16940 DISP_PORT_ONLY)); 16941 } 16942 return; 16943 } 16944 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16945 /* 16946 * For zero window probe, we need to send indefinitely, 16947 * unless we have not heard from the other side for some 16948 * time... 16949 */ 16950 if ((tcp->tcp_zero_win_probe == 0) || 16951 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16952 second_threshold)) { 16953 BUMP_MIB(&tcp_mib, tcpTimRetransDrop); 16954 /* 16955 * If TCP is in SYN_RCVD state, send back a 16956 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16957 * should be zero in TCPS_SYN_RCVD state. 16958 */ 16959 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16960 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16961 "in SYN_RCVD", 16962 tcp, tcp->tcp_snxt, 16963 tcp->tcp_rnxt, TH_RST | TH_ACK); 16964 } 16965 (void) tcp_clean_death(tcp, 16966 tcp->tcp_client_errno ? 16967 tcp->tcp_client_errno : ETIMEDOUT, 25); 16968 return; 16969 } else { 16970 /* 16971 * Set tcp_ms_we_have_waited to second_threshold 16972 * so that in next timeout, we will do the above 16973 * check (lbolt - tcp_last_recv_time). This is 16974 * also to avoid overflow. 16975 * 16976 * We don't need to decrement tcp_timer_backoff 16977 * to avoid overflow because it will be decremented 16978 * later if new timeout value is greater than 16979 * tcp_rexmit_interval_max. In the case when 16980 * tcp_rexmit_interval_max is greater than 16981 * second_threshold, it means that we will wait 16982 * longer than second_threshold to send the next 16983 * window probe. 16984 */ 16985 tcp->tcp_ms_we_have_waited = second_threshold; 16986 } 16987 } else if (ms > first_threshold) { 16988 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16989 tcp->tcp_xmit_head != NULL) { 16990 tcp->tcp_xmit_head = 16991 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16992 } 16993 /* 16994 * We have been retransmitting for too long... The RTT 16995 * we calculated is probably incorrect. Reinitialize it. 16996 * Need to compensate for 0 tcp_rtt_sa. Reset 16997 * tcp_rtt_update so that we won't accidentally cache a 16998 * bad value. But only do this if this is not a zero 16999 * window probe. 17000 */ 17001 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17002 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17003 (tcp->tcp_rtt_sa >> 5); 17004 tcp->tcp_rtt_sa = 0; 17005 tcp_ip_notify(tcp); 17006 tcp->tcp_rtt_update = 0; 17007 } 17008 } 17009 tcp->tcp_timer_backoff++; 17010 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17011 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17012 tcp_rexmit_interval_min) { 17013 /* 17014 * This means the original RTO is tcp_rexmit_interval_min. 17015 * So we will use tcp_rexmit_interval_min as the RTO value 17016 * and do the backoff. 17017 */ 17018 ms = tcp_rexmit_interval_min << tcp->tcp_timer_backoff; 17019 } else { 17020 ms <<= tcp->tcp_timer_backoff; 17021 } 17022 if (ms > tcp_rexmit_interval_max) { 17023 ms = tcp_rexmit_interval_max; 17024 /* 17025 * ms is at max, decrement tcp_timer_backoff to avoid 17026 * overflow. 17027 */ 17028 tcp->tcp_timer_backoff--; 17029 } 17030 tcp->tcp_ms_we_have_waited += ms; 17031 if (tcp->tcp_zero_win_probe == 0) { 17032 tcp->tcp_rto = ms; 17033 } 17034 TCP_TIMER_RESTART(tcp, ms); 17035 /* 17036 * This is after a timeout and tcp_rto is backed off. Set 17037 * tcp_set_timer to 1 so that next time RTO is updated, we will 17038 * restart the timer with a correct value. 17039 */ 17040 tcp->tcp_set_timer = 1; 17041 mss = tcp->tcp_snxt - tcp->tcp_suna; 17042 if (mss > tcp->tcp_mss) 17043 mss = tcp->tcp_mss; 17044 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17045 mss = tcp->tcp_swnd; 17046 17047 if ((mp = tcp->tcp_xmit_head) != NULL) 17048 mp->b_prev = (mblk_t *)lbolt; 17049 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17050 B_TRUE); 17051 17052 /* 17053 * When slow start after retransmission begins, start with 17054 * this seq no. tcp_rexmit_max marks the end of special slow 17055 * start phase. tcp_snd_burst controls how many segments 17056 * can be sent because of an ack. 17057 */ 17058 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17059 tcp->tcp_snd_burst = TCP_CWND_SS; 17060 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17061 (tcp->tcp_unsent == 0)) { 17062 tcp->tcp_rexmit_max = tcp->tcp_fss; 17063 } else { 17064 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17065 } 17066 tcp->tcp_rexmit = B_TRUE; 17067 tcp->tcp_dupack_cnt = 0; 17068 17069 /* 17070 * Remove all rexmit SACK blk to start from fresh. 17071 */ 17072 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17073 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17074 tcp->tcp_num_notsack_blk = 0; 17075 tcp->tcp_cnt_notsack_list = 0; 17076 } 17077 if (mp == NULL) { 17078 return; 17079 } 17080 /* Attach credentials to retransmitted initial SYNs. */ 17081 if (tcp->tcp_state == TCPS_SYN_SENT) { 17082 mblk_setcred(mp, tcp->tcp_cred); 17083 DB_CPID(mp) = tcp->tcp_cpid; 17084 } 17085 17086 tcp->tcp_csuna = tcp->tcp_snxt; 17087 BUMP_MIB(&tcp_mib, tcpRetransSegs); 17088 UPDATE_MIB(&tcp_mib, tcpRetransBytes, mss); 17089 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 17090 tcp_send_data(tcp, tcp->tcp_wq, mp); 17091 17092 } 17093 17094 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17095 static void 17096 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17097 { 17098 conn_t *connp; 17099 17100 switch (tcp->tcp_state) { 17101 case TCPS_BOUND: 17102 case TCPS_LISTEN: 17103 break; 17104 default: 17105 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17106 return; 17107 } 17108 17109 /* 17110 * Need to clean up all the eagers since after the unbind, segments 17111 * will no longer be delivered to this listener stream. 17112 */ 17113 mutex_enter(&tcp->tcp_eager_lock); 17114 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17115 tcp_eager_cleanup(tcp, 0); 17116 } 17117 mutex_exit(&tcp->tcp_eager_lock); 17118 17119 if (tcp->tcp_ipversion == IPV4_VERSION) { 17120 tcp->tcp_ipha->ipha_src = 0; 17121 } else { 17122 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17123 } 17124 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17125 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17126 tcp_bind_hash_remove(tcp); 17127 tcp->tcp_state = TCPS_IDLE; 17128 tcp->tcp_mdt = B_FALSE; 17129 /* Send M_FLUSH according to TPI */ 17130 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17131 connp = tcp->tcp_connp; 17132 connp->conn_mdt_ok = B_FALSE; 17133 ipcl_hash_remove(connp); 17134 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17135 mp = mi_tpi_ok_ack_alloc(mp); 17136 putnext(tcp->tcp_rq, mp); 17137 } 17138 17139 /* 17140 * Don't let port fall into the privileged range. 17141 * Since the extra privileged ports can be arbitrary we also 17142 * ensure that we exclude those from consideration. 17143 * tcp_g_epriv_ports is not sorted thus we loop over it until 17144 * there are no changes. 17145 * 17146 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17147 * but instead the code relies on: 17148 * - the fact that the address of the array and its size never changes 17149 * - the atomic assignment of the elements of the array 17150 * 17151 * Returns 0 if there are no more ports available. 17152 * 17153 * TS note: skip multilevel ports. 17154 */ 17155 static in_port_t 17156 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17157 { 17158 int i; 17159 boolean_t restart = B_FALSE; 17160 17161 if (random && tcp_random_anon_port != 0) { 17162 (void) random_get_pseudo_bytes((uint8_t *)&port, 17163 sizeof (in_port_t)); 17164 /* 17165 * Unless changed by a sys admin, the smallest anon port 17166 * is 32768 and the largest anon port is 65535. It is 17167 * very likely (50%) for the random port to be smaller 17168 * than the smallest anon port. When that happens, 17169 * add port % (anon port range) to the smallest anon 17170 * port to get the random port. It should fall into the 17171 * valid anon port range. 17172 */ 17173 if (port < tcp_smallest_anon_port) { 17174 port = tcp_smallest_anon_port + 17175 port % (tcp_largest_anon_port - 17176 tcp_smallest_anon_port); 17177 } 17178 } 17179 17180 retry: 17181 if (port < tcp_smallest_anon_port) 17182 port = (in_port_t)tcp_smallest_anon_port; 17183 17184 if (port > tcp_largest_anon_port) { 17185 if (restart) 17186 return (0); 17187 restart = B_TRUE; 17188 port = (in_port_t)tcp_smallest_anon_port; 17189 } 17190 17191 if (port < tcp_smallest_nonpriv_port) 17192 port = (in_port_t)tcp_smallest_nonpriv_port; 17193 17194 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 17195 if (port == tcp_g_epriv_ports[i]) { 17196 port++; 17197 /* 17198 * Make sure whether the port is in the 17199 * valid range. 17200 */ 17201 goto retry; 17202 } 17203 } 17204 if (is_system_labeled() && 17205 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17206 IPPROTO_TCP, B_TRUE)) != 0) { 17207 port = i; 17208 goto retry; 17209 } 17210 return (port); 17211 } 17212 17213 /* 17214 * Return the next anonymous port in the privileged port range for 17215 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17216 * downwards. This is the same behavior as documented in the userland 17217 * library call rresvport(3N). 17218 * 17219 * TS note: skip multilevel ports. 17220 */ 17221 static in_port_t 17222 tcp_get_next_priv_port(const tcp_t *tcp) 17223 { 17224 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17225 in_port_t nextport; 17226 boolean_t restart = B_FALSE; 17227 17228 retry: 17229 if (next_priv_port < tcp_min_anonpriv_port || 17230 next_priv_port >= IPPORT_RESERVED) { 17231 next_priv_port = IPPORT_RESERVED - 1; 17232 if (restart) 17233 return (0); 17234 restart = B_TRUE; 17235 } 17236 if (is_system_labeled() && 17237 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17238 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17239 next_priv_port = nextport; 17240 goto retry; 17241 } 17242 return (next_priv_port--); 17243 } 17244 17245 /* The write side r/w procedure. */ 17246 17247 #if CCS_STATS 17248 struct { 17249 struct { 17250 int64_t count, bytes; 17251 } tot, hit; 17252 } wrw_stats; 17253 #endif 17254 17255 /* 17256 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17257 * messages. 17258 */ 17259 /* ARGSUSED */ 17260 static void 17261 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17262 { 17263 conn_t *connp = (conn_t *)arg; 17264 tcp_t *tcp = connp->conn_tcp; 17265 queue_t *q = tcp->tcp_wq; 17266 17267 ASSERT(DB_TYPE(mp) != M_IOCTL); 17268 /* 17269 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17270 * Once the close starts, streamhead and sockfs will not let any data 17271 * packets come down (close ensures that there are no threads using the 17272 * queue and no new threads will come down) but since qprocsoff() 17273 * hasn't happened yet, a M_FLUSH or some non data message might 17274 * get reflected back (in response to our own FLUSHRW) and get 17275 * processed after tcp_close() is done. The conn would still be valid 17276 * because a ref would have added but we need to check the state 17277 * before actually processing the packet. 17278 */ 17279 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17280 freemsg(mp); 17281 return; 17282 } 17283 17284 switch (DB_TYPE(mp)) { 17285 case M_IOCDATA: 17286 tcp_wput_iocdata(tcp, mp); 17287 break; 17288 case M_FLUSH: 17289 tcp_wput_flush(tcp, mp); 17290 break; 17291 default: 17292 CALL_IP_WPUT(connp, q, mp); 17293 break; 17294 } 17295 } 17296 17297 /* 17298 * The TCP fast path write put procedure. 17299 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17300 */ 17301 /* ARGSUSED */ 17302 void 17303 tcp_output(void *arg, mblk_t *mp, void *arg2) 17304 { 17305 int len; 17306 int hdrlen; 17307 int plen; 17308 mblk_t *mp1; 17309 uchar_t *rptr; 17310 uint32_t snxt; 17311 tcph_t *tcph; 17312 struct datab *db; 17313 uint32_t suna; 17314 uint32_t mss; 17315 ipaddr_t *dst; 17316 ipaddr_t *src; 17317 uint32_t sum; 17318 int usable; 17319 conn_t *connp = (conn_t *)arg; 17320 tcp_t *tcp = connp->conn_tcp; 17321 uint32_t msize; 17322 17323 /* 17324 * Try and ASSERT the minimum possible references on the 17325 * conn early enough. Since we are executing on write side, 17326 * the connection is obviously not detached and that means 17327 * there is a ref each for TCP and IP. Since we are behind 17328 * the squeue, the minimum references needed are 3. If the 17329 * conn is in classifier hash list, there should be an 17330 * extra ref for that (we check both the possibilities). 17331 */ 17332 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17333 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17334 17335 ASSERT(DB_TYPE(mp) == M_DATA); 17336 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17337 17338 mutex_enter(&connp->conn_lock); 17339 tcp->tcp_squeue_bytes -= msize; 17340 mutex_exit(&connp->conn_lock); 17341 17342 /* Bypass tcp protocol for fused tcp loopback */ 17343 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17344 return; 17345 17346 mss = tcp->tcp_mss; 17347 if (tcp->tcp_xmit_zc_clean) 17348 mp = tcp_zcopy_backoff(tcp, mp, 0); 17349 17350 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17351 len = (int)(mp->b_wptr - mp->b_rptr); 17352 17353 /* 17354 * Criteria for fast path: 17355 * 17356 * 1. no unsent data 17357 * 2. single mblk in request 17358 * 3. connection established 17359 * 4. data in mblk 17360 * 5. len <= mss 17361 * 6. no tcp_valid bits 17362 */ 17363 if ((tcp->tcp_unsent != 0) || 17364 (tcp->tcp_cork) || 17365 (mp->b_cont != NULL) || 17366 (tcp->tcp_state != TCPS_ESTABLISHED) || 17367 (len == 0) || 17368 (len > mss) || 17369 (tcp->tcp_valid_bits != 0)) { 17370 tcp_wput_data(tcp, mp, B_FALSE); 17371 return; 17372 } 17373 17374 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17375 ASSERT(tcp->tcp_fin_sent == 0); 17376 17377 /* queue new packet onto retransmission queue */ 17378 if (tcp->tcp_xmit_head == NULL) { 17379 tcp->tcp_xmit_head = mp; 17380 } else { 17381 tcp->tcp_xmit_last->b_cont = mp; 17382 } 17383 tcp->tcp_xmit_last = mp; 17384 tcp->tcp_xmit_tail = mp; 17385 17386 /* find out how much we can send */ 17387 /* BEGIN CSTYLED */ 17388 /* 17389 * un-acked usable 17390 * |--------------|-----------------| 17391 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17392 */ 17393 /* END CSTYLED */ 17394 17395 /* start sending from tcp_snxt */ 17396 snxt = tcp->tcp_snxt; 17397 17398 /* 17399 * Check to see if this connection has been idled for some 17400 * time and no ACK is expected. If it is, we need to slow 17401 * start again to get back the connection's "self-clock" as 17402 * described in VJ's paper. 17403 * 17404 * Refer to the comment in tcp_mss_set() for the calculation 17405 * of tcp_cwnd after idle. 17406 */ 17407 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17408 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17409 SET_TCP_INIT_CWND(tcp, mss, tcp_slow_start_after_idle); 17410 } 17411 17412 usable = tcp->tcp_swnd; /* tcp window size */ 17413 if (usable > tcp->tcp_cwnd) 17414 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17415 usable -= snxt; /* subtract stuff already sent */ 17416 suna = tcp->tcp_suna; 17417 usable += suna; 17418 /* usable can be < 0 if the congestion window is smaller */ 17419 if (len > usable) { 17420 /* Can't send complete M_DATA in one shot */ 17421 goto slow; 17422 } 17423 17424 if (tcp->tcp_flow_stopped && 17425 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17426 tcp_clrqfull(tcp); 17427 } 17428 17429 /* 17430 * determine if anything to send (Nagle). 17431 * 17432 * 1. len < tcp_mss (i.e. small) 17433 * 2. unacknowledged data present 17434 * 3. len < nagle limit 17435 * 4. last packet sent < nagle limit (previous packet sent) 17436 */ 17437 if ((len < mss) && (snxt != suna) && 17438 (len < (int)tcp->tcp_naglim) && 17439 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17440 /* 17441 * This was the first unsent packet and normally 17442 * mss < xmit_hiwater so there is no need to worry 17443 * about flow control. The next packet will go 17444 * through the flow control check in tcp_wput_data(). 17445 */ 17446 /* leftover work from above */ 17447 tcp->tcp_unsent = len; 17448 tcp->tcp_xmit_tail_unsent = len; 17449 17450 return; 17451 } 17452 17453 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17454 17455 if (snxt == suna) { 17456 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17457 } 17458 17459 /* we have always sent something */ 17460 tcp->tcp_rack_cnt = 0; 17461 17462 tcp->tcp_snxt = snxt + len; 17463 tcp->tcp_rack = tcp->tcp_rnxt; 17464 17465 if ((mp1 = dupb(mp)) == 0) 17466 goto no_memory; 17467 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17468 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17469 17470 /* adjust tcp header information */ 17471 tcph = tcp->tcp_tcph; 17472 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17473 17474 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17475 sum = (sum >> 16) + (sum & 0xFFFF); 17476 U16_TO_ABE16(sum, tcph->th_sum); 17477 17478 U32_TO_ABE32(snxt, tcph->th_seq); 17479 17480 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 17481 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 17482 BUMP_LOCAL(tcp->tcp_obsegs); 17483 17484 /* Update the latest receive window size in TCP header. */ 17485 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17486 tcph->th_win); 17487 17488 tcp->tcp_last_sent_len = (ushort_t)len; 17489 17490 plen = len + tcp->tcp_hdr_len; 17491 17492 if (tcp->tcp_ipversion == IPV4_VERSION) { 17493 tcp->tcp_ipha->ipha_length = htons(plen); 17494 } else { 17495 tcp->tcp_ip6h->ip6_plen = htons(plen - 17496 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17497 } 17498 17499 /* see if we need to allocate a mblk for the headers */ 17500 hdrlen = tcp->tcp_hdr_len; 17501 rptr = mp1->b_rptr - hdrlen; 17502 db = mp1->b_datap; 17503 if ((db->db_ref != 2) || rptr < db->db_base || 17504 (!OK_32PTR(rptr))) { 17505 /* NOTE: we assume allocb returns an OK_32PTR */ 17506 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17507 tcp_wroff_xtra, BPRI_MED); 17508 if (!mp) { 17509 freemsg(mp1); 17510 goto no_memory; 17511 } 17512 mp->b_cont = mp1; 17513 mp1 = mp; 17514 /* Leave room for Link Level header */ 17515 /* hdrlen = tcp->tcp_hdr_len; */ 17516 rptr = &mp1->b_rptr[tcp_wroff_xtra]; 17517 mp1->b_wptr = &rptr[hdrlen]; 17518 } 17519 mp1->b_rptr = rptr; 17520 17521 /* Fill in the timestamp option. */ 17522 if (tcp->tcp_snd_ts_ok) { 17523 U32_TO_BE32((uint32_t)lbolt, 17524 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17525 U32_TO_BE32(tcp->tcp_ts_recent, 17526 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17527 } else { 17528 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17529 } 17530 17531 /* copy header into outgoing packet */ 17532 dst = (ipaddr_t *)rptr; 17533 src = (ipaddr_t *)tcp->tcp_iphc; 17534 dst[0] = src[0]; 17535 dst[1] = src[1]; 17536 dst[2] = src[2]; 17537 dst[3] = src[3]; 17538 dst[4] = src[4]; 17539 dst[5] = src[5]; 17540 dst[6] = src[6]; 17541 dst[7] = src[7]; 17542 dst[8] = src[8]; 17543 dst[9] = src[9]; 17544 if (hdrlen -= 40) { 17545 hdrlen >>= 2; 17546 dst += 10; 17547 src += 10; 17548 do { 17549 *dst++ = *src++; 17550 } while (--hdrlen); 17551 } 17552 17553 /* 17554 * Set the ECN info in the TCP header. Note that this 17555 * is not the template header. 17556 */ 17557 if (tcp->tcp_ecn_ok) { 17558 SET_ECT(tcp, rptr); 17559 17560 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17561 if (tcp->tcp_ecn_echo_on) 17562 tcph->th_flags[0] |= TH_ECE; 17563 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17564 tcph->th_flags[0] |= TH_CWR; 17565 tcp->tcp_ecn_cwr_sent = B_TRUE; 17566 } 17567 } 17568 17569 if (tcp->tcp_ip_forward_progress) { 17570 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17571 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17572 tcp->tcp_ip_forward_progress = B_FALSE; 17573 } 17574 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17575 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17576 return; 17577 17578 /* 17579 * If we ran out of memory, we pretend to have sent the packet 17580 * and that it was lost on the wire. 17581 */ 17582 no_memory: 17583 return; 17584 17585 slow: 17586 /* leftover work from above */ 17587 tcp->tcp_unsent = len; 17588 tcp->tcp_xmit_tail_unsent = len; 17589 tcp_wput_data(tcp, NULL, B_FALSE); 17590 } 17591 17592 /* 17593 * The function called through squeue to get behind eager's perimeter to 17594 * finish the accept processing. 17595 */ 17596 /* ARGSUSED */ 17597 void 17598 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17599 { 17600 conn_t *connp = (conn_t *)arg; 17601 tcp_t *tcp = connp->conn_tcp; 17602 queue_t *q = tcp->tcp_rq; 17603 mblk_t *mp1; 17604 mblk_t *stropt_mp = mp; 17605 struct stroptions *stropt; 17606 uint_t thwin; 17607 17608 /* 17609 * Drop the eager's ref on the listener, that was placed when 17610 * this eager began life in tcp_conn_request. 17611 */ 17612 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17613 17614 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17615 /* 17616 * Someone blewoff the eager before we could finish 17617 * the accept. 17618 * 17619 * The only reason eager exists it because we put in 17620 * a ref on it when conn ind went up. We need to send 17621 * a disconnect indication up while the last reference 17622 * on the eager will be dropped by the squeue when we 17623 * return. 17624 */ 17625 ASSERT(tcp->tcp_listener == NULL); 17626 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17627 struct T_discon_ind *tdi; 17628 17629 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17630 /* 17631 * Let us reuse the incoming mblk to avoid memory 17632 * allocation failure problems. We know that the 17633 * size of the incoming mblk i.e. stroptions is greater 17634 * than sizeof T_discon_ind. So the reallocb below 17635 * can't fail. 17636 */ 17637 freemsg(mp->b_cont); 17638 mp->b_cont = NULL; 17639 ASSERT(DB_REF(mp) == 1); 17640 mp = reallocb(mp, sizeof (struct T_discon_ind), 17641 B_FALSE); 17642 ASSERT(mp != NULL); 17643 DB_TYPE(mp) = M_PROTO; 17644 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17645 tdi = (struct T_discon_ind *)mp->b_rptr; 17646 if (tcp->tcp_issocket) { 17647 tdi->DISCON_reason = ECONNREFUSED; 17648 tdi->SEQ_number = 0; 17649 } else { 17650 tdi->DISCON_reason = ENOPROTOOPT; 17651 tdi->SEQ_number = 17652 tcp->tcp_conn_req_seqnum; 17653 } 17654 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17655 putnext(q, mp); 17656 } else { 17657 freemsg(mp); 17658 } 17659 if (tcp->tcp_hard_binding) { 17660 tcp->tcp_hard_binding = B_FALSE; 17661 tcp->tcp_hard_bound = B_TRUE; 17662 } 17663 tcp->tcp_detached = B_FALSE; 17664 return; 17665 } 17666 17667 mp1 = stropt_mp->b_cont; 17668 stropt_mp->b_cont = NULL; 17669 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17670 stropt = (struct stroptions *)stropt_mp->b_rptr; 17671 17672 while (mp1 != NULL) { 17673 mp = mp1; 17674 mp1 = mp1->b_cont; 17675 mp->b_cont = NULL; 17676 tcp->tcp_drop_opt_ack_cnt++; 17677 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17678 } 17679 mp = NULL; 17680 17681 /* 17682 * For a loopback connection with tcp_direct_sockfs on, note that 17683 * we don't have to protect tcp_rcv_list yet because synchronous 17684 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17685 * possibly race with us. 17686 */ 17687 17688 /* 17689 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17690 * properly. This is the first time we know of the acceptor' 17691 * queue. So we do it here. 17692 */ 17693 if (tcp->tcp_rcv_list == NULL) { 17694 /* 17695 * Recv queue is empty, tcp_rwnd should not have changed. 17696 * That means it should be equal to the listener's tcp_rwnd. 17697 */ 17698 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17699 } else { 17700 #ifdef DEBUG 17701 uint_t cnt = 0; 17702 17703 mp1 = tcp->tcp_rcv_list; 17704 while ((mp = mp1) != NULL) { 17705 mp1 = mp->b_next; 17706 cnt += msgdsize(mp); 17707 } 17708 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17709 #endif 17710 /* There is some data, add them back to get the max. */ 17711 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17712 } 17713 17714 stropt->so_flags = SO_HIWAT; 17715 stropt->so_hiwat = MAX(q->q_hiwat, tcp_sth_rcv_hiwat); 17716 17717 stropt->so_flags |= SO_MAXBLK; 17718 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17719 17720 /* 17721 * This is the first time we run on the correct 17722 * queue after tcp_accept. So fix all the q parameters 17723 * here. 17724 */ 17725 /* Allocate room for SACK options if needed. */ 17726 stropt->so_flags |= SO_WROFF; 17727 if (tcp->tcp_fused) { 17728 ASSERT(tcp->tcp_loopback); 17729 ASSERT(tcp->tcp_loopback_peer != NULL); 17730 /* 17731 * For fused tcp loopback, set the stream head's write 17732 * offset value to zero since we won't be needing any room 17733 * for TCP/IP headers. This would also improve performance 17734 * since it would reduce the amount of work done by kmem. 17735 * Non-fused tcp loopback case is handled separately below. 17736 */ 17737 stropt->so_wroff = 0; 17738 /* 17739 * Record the stream head's high water mark for this endpoint; 17740 * this is used for flow-control purposes in tcp_fuse_output(). 17741 */ 17742 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 17743 /* 17744 * Update the peer's transmit parameters according to 17745 * our recently calculated high water mark value. 17746 */ 17747 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17748 } else if (tcp->tcp_snd_sack_ok) { 17749 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17750 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra); 17751 } else { 17752 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17753 tcp_wroff_xtra); 17754 } 17755 17756 /* 17757 * If this is endpoint is handling SSL, then reserve extra 17758 * offset and space at the end. 17759 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17760 * overriding the previous setting. The extra cost of signing and 17761 * encrypting multiple MSS-size records (12 of them with Ethernet), 17762 * instead of a single contiguous one by the stream head 17763 * largely outweighs the statistical reduction of ACKs, when 17764 * applicable. The peer will also save on decyption and verification 17765 * costs. 17766 */ 17767 if (tcp->tcp_kssl_ctx != NULL) { 17768 stropt->so_wroff += SSL3_WROFFSET; 17769 17770 stropt->so_flags |= SO_TAIL; 17771 stropt->so_tail = SSL3_MAX_TAIL_LEN; 17772 17773 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 17774 } 17775 17776 /* Send the options up */ 17777 putnext(q, stropt_mp); 17778 17779 /* 17780 * Pass up any data and/or a fin that has been received. 17781 * 17782 * Adjust receive window in case it had decreased 17783 * (because there is data <=> tcp_rcv_list != NULL) 17784 * while the connection was detached. Note that 17785 * in case the eager was flow-controlled, w/o this 17786 * code, the rwnd may never open up again! 17787 */ 17788 if (tcp->tcp_rcv_list != NULL) { 17789 /* We drain directly in case of fused tcp loopback */ 17790 if (!tcp->tcp_fused && canputnext(q)) { 17791 tcp->tcp_rwnd = q->q_hiwat; 17792 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 17793 << tcp->tcp_rcv_ws; 17794 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 17795 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17796 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 17797 tcp_xmit_ctl(NULL, 17798 tcp, (tcp->tcp_swnd == 0) ? 17799 tcp->tcp_suna : tcp->tcp_snxt, 17800 tcp->tcp_rnxt, TH_ACK); 17801 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 17802 } 17803 17804 } 17805 (void) tcp_rcv_drain(q, tcp); 17806 17807 /* 17808 * For fused tcp loopback, back-enable peer endpoint 17809 * if it's currently flow-controlled. 17810 */ 17811 if (tcp->tcp_fused && 17812 tcp->tcp_loopback_peer->tcp_flow_stopped) { 17813 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17814 17815 ASSERT(peer_tcp != NULL); 17816 ASSERT(peer_tcp->tcp_fused); 17817 17818 tcp_clrqfull(peer_tcp); 17819 TCP_STAT(tcp_fusion_backenabled); 17820 } 17821 } 17822 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17823 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17824 mp = mi_tpi_ordrel_ind(); 17825 if (mp) { 17826 tcp->tcp_ordrel_done = B_TRUE; 17827 putnext(q, mp); 17828 if (tcp->tcp_deferred_clean_death) { 17829 /* 17830 * tcp_clean_death was deferred 17831 * for T_ORDREL_IND - do it now 17832 */ 17833 (void) tcp_clean_death(tcp, 17834 tcp->tcp_client_errno, 21); 17835 tcp->tcp_deferred_clean_death = B_FALSE; 17836 } 17837 } else { 17838 /* 17839 * Run the orderly release in the 17840 * service routine. 17841 */ 17842 qenable(q); 17843 } 17844 } 17845 if (tcp->tcp_hard_binding) { 17846 tcp->tcp_hard_binding = B_FALSE; 17847 tcp->tcp_hard_bound = B_TRUE; 17848 } 17849 17850 tcp->tcp_detached = B_FALSE; 17851 17852 /* We can enable synchronous streams now */ 17853 if (tcp->tcp_fused) { 17854 tcp_fuse_syncstr_enable_pair(tcp); 17855 } 17856 17857 if (tcp->tcp_ka_enabled) { 17858 tcp->tcp_ka_last_intrvl = 0; 17859 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17860 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17861 } 17862 17863 /* 17864 * At this point, eager is fully established and will 17865 * have the following references - 17866 * 17867 * 2 references for connection to exist (1 for TCP and 1 for IP). 17868 * 1 reference for the squeue which will be dropped by the squeue as 17869 * soon as this function returns. 17870 * There will be 1 additonal reference for being in classifier 17871 * hash list provided something bad hasn't happened. 17872 */ 17873 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17874 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17875 } 17876 17877 /* 17878 * The function called through squeue to get behind listener's perimeter to 17879 * send a deffered conn_ind. 17880 */ 17881 /* ARGSUSED */ 17882 void 17883 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17884 { 17885 conn_t *connp = (conn_t *)arg; 17886 tcp_t *listener = connp->conn_tcp; 17887 17888 if (listener->tcp_state == TCPS_CLOSED || 17889 TCP_IS_DETACHED(listener)) { 17890 /* 17891 * If listener has closed, it would have caused a 17892 * a cleanup/blowoff to happen for the eager. 17893 */ 17894 tcp_t *tcp; 17895 struct T_conn_ind *conn_ind; 17896 17897 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17898 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17899 conn_ind->OPT_length); 17900 /* 17901 * We need to drop the ref on eager that was put 17902 * tcp_rput_data() before trying to send the conn_ind 17903 * to listener. The conn_ind was deferred in tcp_send_conn_ind 17904 * and tcp_wput_accept() is sending this deferred conn_ind but 17905 * listener is closed so we drop the ref. 17906 */ 17907 CONN_DEC_REF(tcp->tcp_connp); 17908 freemsg(mp); 17909 return; 17910 } 17911 putnext(listener->tcp_rq, mp); 17912 } 17913 17914 17915 /* 17916 * This is the STREAMS entry point for T_CONN_RES coming down on 17917 * Acceptor STREAM when sockfs listener does accept processing. 17918 * Read the block comment on top pf tcp_conn_request(). 17919 */ 17920 void 17921 tcp_wput_accept(queue_t *q, mblk_t *mp) 17922 { 17923 queue_t *rq = RD(q); 17924 struct T_conn_res *conn_res; 17925 tcp_t *eager; 17926 tcp_t *listener; 17927 struct T_ok_ack *ok; 17928 t_scalar_t PRIM_type; 17929 mblk_t *opt_mp; 17930 conn_t *econnp; 17931 17932 ASSERT(DB_TYPE(mp) == M_PROTO); 17933 17934 conn_res = (struct T_conn_res *)mp->b_rptr; 17935 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17936 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17937 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17938 if (mp != NULL) 17939 putnext(rq, mp); 17940 return; 17941 } 17942 switch (conn_res->PRIM_type) { 17943 case O_T_CONN_RES: 17944 case T_CONN_RES: 17945 /* 17946 * We pass up an err ack if allocb fails. This will 17947 * cause sockfs to issue a T_DISCON_REQ which will cause 17948 * tcp_eager_blowoff to be called. sockfs will then call 17949 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17950 * we need to do the allocb up here because we have to 17951 * make sure rq->q_qinfo->qi_qclose still points to the 17952 * correct function (tcpclose_accept) in case allocb 17953 * fails. 17954 */ 17955 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17956 if (opt_mp == NULL) { 17957 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17958 if (mp != NULL) 17959 putnext(rq, mp); 17960 return; 17961 } 17962 17963 bcopy(mp->b_rptr + conn_res->OPT_offset, 17964 &eager, conn_res->OPT_length); 17965 PRIM_type = conn_res->PRIM_type; 17966 mp->b_datap->db_type = M_PCPROTO; 17967 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 17968 ok = (struct T_ok_ack *)mp->b_rptr; 17969 ok->PRIM_type = T_OK_ACK; 17970 ok->CORRECT_prim = PRIM_type; 17971 econnp = eager->tcp_connp; 17972 econnp->conn_dev = (dev_t)q->q_ptr; 17973 eager->tcp_rq = rq; 17974 eager->tcp_wq = q; 17975 rq->q_ptr = econnp; 17976 rq->q_qinfo = &tcp_rinit; 17977 q->q_ptr = econnp; 17978 q->q_qinfo = &tcp_winit; 17979 listener = eager->tcp_listener; 17980 eager->tcp_issocket = B_TRUE; 17981 17982 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17983 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 17984 17985 /* Put the ref for IP */ 17986 CONN_INC_REF(econnp); 17987 17988 /* 17989 * We should have minimum of 3 references on the conn 17990 * at this point. One each for TCP and IP and one for 17991 * the T_conn_ind that was sent up when the 3-way handshake 17992 * completed. In the normal case we would also have another 17993 * reference (making a total of 4) for the conn being in the 17994 * classifier hash list. However the eager could have received 17995 * an RST subsequently and tcp_closei_local could have removed 17996 * the eager from the classifier hash list, hence we can't 17997 * assert that reference. 17998 */ 17999 ASSERT(econnp->conn_ref >= 3); 18000 18001 /* 18002 * Send the new local address also up to sockfs. There 18003 * should already be enough space in the mp that came 18004 * down from soaccept(). 18005 */ 18006 if (eager->tcp_family == AF_INET) { 18007 sin_t *sin; 18008 18009 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18010 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18011 sin = (sin_t *)mp->b_wptr; 18012 mp->b_wptr += sizeof (sin_t); 18013 sin->sin_family = AF_INET; 18014 sin->sin_port = eager->tcp_lport; 18015 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18016 } else { 18017 sin6_t *sin6; 18018 18019 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18020 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18021 sin6 = (sin6_t *)mp->b_wptr; 18022 mp->b_wptr += sizeof (sin6_t); 18023 sin6->sin6_family = AF_INET6; 18024 sin6->sin6_port = eager->tcp_lport; 18025 if (eager->tcp_ipversion == IPV4_VERSION) { 18026 sin6->sin6_flowinfo = 0; 18027 IN6_IPADDR_TO_V4MAPPED( 18028 eager->tcp_ipha->ipha_src, 18029 &sin6->sin6_addr); 18030 } else { 18031 ASSERT(eager->tcp_ip6h != NULL); 18032 sin6->sin6_flowinfo = 18033 eager->tcp_ip6h->ip6_vcf & 18034 ~IPV6_VERS_AND_FLOW_MASK; 18035 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18036 } 18037 sin6->sin6_scope_id = 0; 18038 sin6->__sin6_src_id = 0; 18039 } 18040 18041 putnext(rq, mp); 18042 18043 opt_mp->b_datap->db_type = M_SETOPTS; 18044 opt_mp->b_wptr += sizeof (struct stroptions); 18045 18046 /* 18047 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18048 * from listener to acceptor. The message is chained on the 18049 * bind_mp which tcp_rput_other will send down to IP. 18050 */ 18051 if (listener->tcp_bound_if != 0) { 18052 /* allocate optmgmt req */ 18053 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18054 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18055 sizeof (int)); 18056 if (mp != NULL) 18057 linkb(opt_mp, mp); 18058 } 18059 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18060 uint_t on = 1; 18061 18062 /* allocate optmgmt req */ 18063 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18064 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18065 if (mp != NULL) 18066 linkb(opt_mp, mp); 18067 } 18068 18069 18070 mutex_enter(&listener->tcp_eager_lock); 18071 18072 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18073 18074 tcp_t *tail; 18075 tcp_t *tcp; 18076 mblk_t *mp1; 18077 18078 tcp = listener->tcp_eager_prev_q0; 18079 /* 18080 * listener->tcp_eager_prev_q0 points to the TAIL of the 18081 * deferred T_conn_ind queue. We need to get to the head 18082 * of the queue in order to send up T_conn_ind the same 18083 * order as how the 3WHS is completed. 18084 */ 18085 while (tcp != listener) { 18086 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18087 !tcp->tcp_kssl_pending) 18088 break; 18089 else 18090 tcp = tcp->tcp_eager_prev_q0; 18091 } 18092 /* None of the pending eagers can be sent up now */ 18093 if (tcp == listener) 18094 goto no_more_eagers; 18095 18096 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18097 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18098 /* Move from q0 to q */ 18099 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18100 listener->tcp_conn_req_cnt_q0--; 18101 listener->tcp_conn_req_cnt_q++; 18102 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18103 tcp->tcp_eager_prev_q0; 18104 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18105 tcp->tcp_eager_next_q0; 18106 tcp->tcp_eager_prev_q0 = NULL; 18107 tcp->tcp_eager_next_q0 = NULL; 18108 tcp->tcp_conn_def_q0 = B_FALSE; 18109 18110 /* Make sure the tcp isn't in the list of droppables */ 18111 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18112 tcp->tcp_eager_prev_drop_q0 == NULL); 18113 18114 /* 18115 * Insert at end of the queue because sockfs sends 18116 * down T_CONN_RES in chronological order. Leaving 18117 * the older conn indications at front of the queue 18118 * helps reducing search time. 18119 */ 18120 tail = listener->tcp_eager_last_q; 18121 if (tail != NULL) { 18122 tail->tcp_eager_next_q = tcp; 18123 } else { 18124 listener->tcp_eager_next_q = tcp; 18125 } 18126 listener->tcp_eager_last_q = tcp; 18127 tcp->tcp_eager_next_q = NULL; 18128 18129 /* Need to get inside the listener perimeter */ 18130 CONN_INC_REF(listener->tcp_connp); 18131 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18132 tcp_send_pending, listener->tcp_connp, 18133 SQTAG_TCP_SEND_PENDING); 18134 } 18135 no_more_eagers: 18136 tcp_eager_unlink(eager); 18137 mutex_exit(&listener->tcp_eager_lock); 18138 18139 /* 18140 * At this point, the eager is detached from the listener 18141 * but we still have an extra refs on eager (apart from the 18142 * usual tcp references). The ref was placed in tcp_rput_data 18143 * before sending the conn_ind in tcp_send_conn_ind. 18144 * The ref will be dropped in tcp_accept_finish(). 18145 */ 18146 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18147 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18148 return; 18149 default: 18150 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18151 if (mp != NULL) 18152 putnext(rq, mp); 18153 return; 18154 } 18155 } 18156 18157 void 18158 tcp_wput(queue_t *q, mblk_t *mp) 18159 { 18160 conn_t *connp = Q_TO_CONN(q); 18161 tcp_t *tcp; 18162 void (*output_proc)(); 18163 t_scalar_t type; 18164 uchar_t *rptr; 18165 struct iocblk *iocp; 18166 uint32_t msize; 18167 18168 ASSERT(connp->conn_ref >= 2); 18169 18170 switch (DB_TYPE(mp)) { 18171 case M_DATA: 18172 tcp = connp->conn_tcp; 18173 ASSERT(tcp != NULL); 18174 18175 msize = msgdsize(mp); 18176 18177 mutex_enter(&connp->conn_lock); 18178 CONN_INC_REF_LOCKED(connp); 18179 18180 tcp->tcp_squeue_bytes += msize; 18181 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18182 mutex_exit(&connp->conn_lock); 18183 tcp_setqfull(tcp); 18184 } else 18185 mutex_exit(&connp->conn_lock); 18186 18187 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18188 tcp_output, connp, SQTAG_TCP_OUTPUT); 18189 return; 18190 case M_PROTO: 18191 case M_PCPROTO: 18192 /* 18193 * if it is a snmp message, don't get behind the squeue 18194 */ 18195 tcp = connp->conn_tcp; 18196 rptr = mp->b_rptr; 18197 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18198 type = ((union T_primitives *)rptr)->type; 18199 } else { 18200 if (tcp->tcp_debug) { 18201 (void) strlog(TCP_MOD_ID, 0, 1, 18202 SL_ERROR|SL_TRACE, 18203 "tcp_wput_proto, dropping one..."); 18204 } 18205 freemsg(mp); 18206 return; 18207 } 18208 if (type == T_SVR4_OPTMGMT_REQ) { 18209 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18210 if (snmpcom_req(q, mp, tcp_snmp_set, tcp_snmp_get, 18211 cr)) { 18212 /* 18213 * This was a SNMP request 18214 */ 18215 return; 18216 } else { 18217 output_proc = tcp_wput_proto; 18218 } 18219 } else { 18220 output_proc = tcp_wput_proto; 18221 } 18222 break; 18223 case M_IOCTL: 18224 /* 18225 * Most ioctls can be processed right away without going via 18226 * squeues - process them right here. Those that do require 18227 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18228 * are processed by tcp_wput_ioctl(). 18229 */ 18230 iocp = (struct iocblk *)mp->b_rptr; 18231 tcp = connp->conn_tcp; 18232 18233 switch (iocp->ioc_cmd) { 18234 case TCP_IOC_ABORT_CONN: 18235 tcp_ioctl_abort_conn(q, mp); 18236 return; 18237 case TI_GETPEERNAME: 18238 if (tcp->tcp_state < TCPS_SYN_RCVD) { 18239 iocp->ioc_error = ENOTCONN; 18240 iocp->ioc_count = 0; 18241 mp->b_datap->db_type = M_IOCACK; 18242 qreply(q, mp); 18243 return; 18244 } 18245 /* FALLTHRU */ 18246 case TI_GETMYNAME: 18247 mi_copyin(q, mp, NULL, 18248 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18249 return; 18250 case ND_SET: 18251 /* nd_getset does the necessary checks */ 18252 case ND_GET: 18253 if (!nd_getset(q, tcp_g_nd, mp)) { 18254 CALL_IP_WPUT(connp, q, mp); 18255 return; 18256 } 18257 qreply(q, mp); 18258 return; 18259 case TCP_IOC_DEFAULT_Q: 18260 /* 18261 * Wants to be the default wq. Check the credentials 18262 * first, the rest is executed via squeue. 18263 */ 18264 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 18265 iocp->ioc_error = EPERM; 18266 iocp->ioc_count = 0; 18267 mp->b_datap->db_type = M_IOCACK; 18268 qreply(q, mp); 18269 return; 18270 } 18271 output_proc = tcp_wput_ioctl; 18272 break; 18273 default: 18274 output_proc = tcp_wput_ioctl; 18275 break; 18276 } 18277 break; 18278 default: 18279 output_proc = tcp_wput_nondata; 18280 break; 18281 } 18282 18283 CONN_INC_REF(connp); 18284 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18285 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18286 } 18287 18288 /* 18289 * Initial STREAMS write side put() procedure for sockets. It tries to 18290 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18291 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18292 * are handled by tcp_wput() as usual. 18293 * 18294 * All further messages will also be handled by tcp_wput() because we cannot 18295 * be sure that the above short cut is safe later. 18296 */ 18297 static void 18298 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18299 { 18300 conn_t *connp = Q_TO_CONN(wq); 18301 tcp_t *tcp = connp->conn_tcp; 18302 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18303 18304 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18305 wq->q_qinfo = &tcp_winit; 18306 18307 ASSERT(IPCL_IS_TCP(connp)); 18308 ASSERT(TCP_IS_SOCKET(tcp)); 18309 18310 if (DB_TYPE(mp) == M_PCPROTO && 18311 MBLKL(mp) == sizeof (struct T_capability_req) && 18312 car->PRIM_type == T_CAPABILITY_REQ) { 18313 tcp_capability_req(tcp, mp); 18314 return; 18315 } 18316 18317 tcp_wput(wq, mp); 18318 } 18319 18320 static boolean_t 18321 tcp_zcopy_check(tcp_t *tcp) 18322 { 18323 conn_t *connp = tcp->tcp_connp; 18324 ire_t *ire; 18325 boolean_t zc_enabled = B_FALSE; 18326 18327 if (do_tcpzcopy == 2) 18328 zc_enabled = B_TRUE; 18329 else if (tcp->tcp_ipversion == IPV4_VERSION && 18330 IPCL_IS_CONNECTED(connp) && 18331 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18332 connp->conn_dontroute == 0 && 18333 !connp->conn_nexthop_set && 18334 connp->conn_xmit_if_ill == NULL && 18335 connp->conn_nofailover_ill == NULL && 18336 do_tcpzcopy == 1) { 18337 /* 18338 * the checks above closely resemble the fast path checks 18339 * in tcp_send_data(). 18340 */ 18341 mutex_enter(&connp->conn_lock); 18342 ire = connp->conn_ire_cache; 18343 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18344 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18345 IRE_REFHOLD(ire); 18346 if (ire->ire_stq != NULL) { 18347 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18348 18349 zc_enabled = ill && (ill->ill_capabilities & 18350 ILL_CAPAB_ZEROCOPY) && 18351 (ill->ill_zerocopy_capab-> 18352 ill_zerocopy_flags != 0); 18353 } 18354 IRE_REFRELE(ire); 18355 } 18356 mutex_exit(&connp->conn_lock); 18357 } 18358 tcp->tcp_snd_zcopy_on = zc_enabled; 18359 if (!TCP_IS_DETACHED(tcp)) { 18360 if (zc_enabled) { 18361 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 18362 TCP_STAT(tcp_zcopy_on); 18363 } else { 18364 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18365 TCP_STAT(tcp_zcopy_off); 18366 } 18367 } 18368 return (zc_enabled); 18369 } 18370 18371 static mblk_t * 18372 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18373 { 18374 if (do_tcpzcopy == 2) 18375 return (bp); 18376 else if (tcp->tcp_snd_zcopy_on) { 18377 tcp->tcp_snd_zcopy_on = B_FALSE; 18378 if (!TCP_IS_DETACHED(tcp)) { 18379 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18380 TCP_STAT(tcp_zcopy_disable); 18381 } 18382 } 18383 return (tcp_zcopy_backoff(tcp, bp, 0)); 18384 } 18385 18386 /* 18387 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18388 * the original desballoca'ed segmapped mblk. 18389 */ 18390 static mblk_t * 18391 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18392 { 18393 mblk_t *head, *tail, *nbp; 18394 if (IS_VMLOANED_MBLK(bp)) { 18395 TCP_STAT(tcp_zcopy_backoff); 18396 if ((head = copyb(bp)) == NULL) { 18397 /* fail to backoff; leave it for the next backoff */ 18398 tcp->tcp_xmit_zc_clean = B_FALSE; 18399 return (bp); 18400 } 18401 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18402 if (fix_xmitlist) 18403 tcp_zcopy_notify(tcp); 18404 else 18405 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18406 } 18407 nbp = bp->b_cont; 18408 if (fix_xmitlist) { 18409 head->b_prev = bp->b_prev; 18410 head->b_next = bp->b_next; 18411 if (tcp->tcp_xmit_tail == bp) 18412 tcp->tcp_xmit_tail = head; 18413 } 18414 bp->b_next = NULL; 18415 bp->b_prev = NULL; 18416 freeb(bp); 18417 } else { 18418 head = bp; 18419 nbp = bp->b_cont; 18420 } 18421 tail = head; 18422 while (nbp) { 18423 if (IS_VMLOANED_MBLK(nbp)) { 18424 TCP_STAT(tcp_zcopy_backoff); 18425 if ((tail->b_cont = copyb(nbp)) == NULL) { 18426 tcp->tcp_xmit_zc_clean = B_FALSE; 18427 tail->b_cont = nbp; 18428 return (head); 18429 } 18430 tail = tail->b_cont; 18431 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18432 if (fix_xmitlist) 18433 tcp_zcopy_notify(tcp); 18434 else 18435 tail->b_datap->db_struioflag |= 18436 STRUIO_ZCNOTIFY; 18437 } 18438 bp = nbp; 18439 nbp = nbp->b_cont; 18440 if (fix_xmitlist) { 18441 tail->b_prev = bp->b_prev; 18442 tail->b_next = bp->b_next; 18443 if (tcp->tcp_xmit_tail == bp) 18444 tcp->tcp_xmit_tail = tail; 18445 } 18446 bp->b_next = NULL; 18447 bp->b_prev = NULL; 18448 freeb(bp); 18449 } else { 18450 tail->b_cont = nbp; 18451 tail = nbp; 18452 nbp = nbp->b_cont; 18453 } 18454 } 18455 if (fix_xmitlist) { 18456 tcp->tcp_xmit_last = tail; 18457 tcp->tcp_xmit_zc_clean = B_TRUE; 18458 } 18459 return (head); 18460 } 18461 18462 static void 18463 tcp_zcopy_notify(tcp_t *tcp) 18464 { 18465 struct stdata *stp; 18466 18467 if (tcp->tcp_detached) 18468 return; 18469 stp = STREAM(tcp->tcp_rq); 18470 mutex_enter(&stp->sd_lock); 18471 stp->sd_flag |= STZCNOTIFY; 18472 cv_broadcast(&stp->sd_zcopy_wait); 18473 mutex_exit(&stp->sd_lock); 18474 } 18475 18476 static void 18477 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18478 { 18479 ipha_t *ipha; 18480 ipaddr_t src; 18481 ipaddr_t dst; 18482 uint32_t cksum; 18483 ire_t *ire; 18484 uint16_t *up; 18485 ill_t *ill; 18486 conn_t *connp = tcp->tcp_connp; 18487 uint32_t hcksum_txflags = 0; 18488 mblk_t *ire_fp_mp; 18489 uint_t ire_fp_mp_len; 18490 18491 ASSERT(DB_TYPE(mp) == M_DATA); 18492 18493 if (DB_CRED(mp) == NULL) 18494 mblk_setcred(mp, CONN_CRED(connp)); 18495 18496 ipha = (ipha_t *)mp->b_rptr; 18497 src = ipha->ipha_src; 18498 dst = ipha->ipha_dst; 18499 18500 /* 18501 * Drop off fast path for IPv6 and also if options are present or 18502 * we need to resolve a TS label. 18503 */ 18504 if (tcp->tcp_ipversion != IPV4_VERSION || 18505 !IPCL_IS_CONNECTED(connp) || 18506 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18507 connp->conn_dontroute || 18508 connp->conn_nexthop_set || 18509 connp->conn_xmit_if_ill != NULL || 18510 connp->conn_nofailover_ill != NULL || 18511 !connp->conn_ulp_labeled || 18512 ipha->ipha_ident == IP_HDR_INCLUDED || 18513 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18514 IPP_ENABLED(IPP_LOCAL_OUT)) { 18515 if (tcp->tcp_snd_zcopy_aware) 18516 mp = tcp_zcopy_disable(tcp, mp); 18517 TCP_STAT(tcp_ip_send); 18518 CALL_IP_WPUT(connp, q, mp); 18519 return; 18520 } 18521 18522 mutex_enter(&connp->conn_lock); 18523 ire = connp->conn_ire_cache; 18524 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18525 if (ire != NULL && ire->ire_addr == dst && 18526 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18527 IRE_REFHOLD(ire); 18528 mutex_exit(&connp->conn_lock); 18529 } else { 18530 boolean_t cached = B_FALSE; 18531 18532 /* force a recheck later on */ 18533 tcp->tcp_ire_ill_check_done = B_FALSE; 18534 18535 TCP_DBGSTAT(tcp_ire_null1); 18536 connp->conn_ire_cache = NULL; 18537 mutex_exit(&connp->conn_lock); 18538 if (ire != NULL) 18539 IRE_REFRELE_NOTR(ire); 18540 ire = ire_cache_lookup(dst, connp->conn_zoneid, 18541 MBLK_GETLABEL(mp)); 18542 if (ire == NULL) { 18543 if (tcp->tcp_snd_zcopy_aware) 18544 mp = tcp_zcopy_backoff(tcp, mp, 0); 18545 TCP_STAT(tcp_ire_null); 18546 CALL_IP_WPUT(connp, q, mp); 18547 return; 18548 } 18549 IRE_REFHOLD_NOTR(ire); 18550 /* 18551 * Since we are inside the squeue, there cannot be another 18552 * thread in TCP trying to set the conn_ire_cache now. The 18553 * check for IRE_MARK_CONDEMNED ensures that an interface 18554 * unplumb thread has not yet started cleaning up the conns. 18555 * Hence we don't need to grab the conn lock. 18556 */ 18557 if (!(connp->conn_state_flags & CONN_CLOSING)) { 18558 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18559 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18560 connp->conn_ire_cache = ire; 18561 cached = B_TRUE; 18562 } 18563 rw_exit(&ire->ire_bucket->irb_lock); 18564 } 18565 18566 /* 18567 * We can continue to use the ire but since it was 18568 * not cached, we should drop the extra reference. 18569 */ 18570 if (!cached) 18571 IRE_REFRELE_NOTR(ire); 18572 18573 /* 18574 * Rampart note: no need to select a new label here, since 18575 * labels are not allowed to change during the life of a TCP 18576 * connection. 18577 */ 18578 } 18579 18580 /* 18581 * The following if case identifies whether or not 18582 * we are forced to take the slowpath. 18583 */ 18584 if (ire->ire_flags & RTF_MULTIRT || 18585 ire->ire_stq == NULL || 18586 ire->ire_max_frag < ntohs(ipha->ipha_length) || 18587 (ire->ire_nce != NULL && 18588 (ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18589 (ire_fp_mp_len = MBLKL(ire_fp_mp)) > MBLKHEAD(mp)) { 18590 if (tcp->tcp_snd_zcopy_aware) 18591 mp = tcp_zcopy_disable(tcp, mp); 18592 TCP_STAT(tcp_ip_ire_send); 18593 IRE_REFRELE(ire); 18594 CALL_IP_WPUT(connp, q, mp); 18595 return; 18596 } 18597 18598 ill = ire_to_ill(ire); 18599 if (connp->conn_outgoing_ill != NULL) { 18600 ill_t *conn_outgoing_ill = NULL; 18601 /* 18602 * Choose a good ill in the group to send the packets on. 18603 */ 18604 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18605 ill = ire_to_ill(ire); 18606 } 18607 ASSERT(ill != NULL); 18608 18609 if (!tcp->tcp_ire_ill_check_done) { 18610 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18611 tcp->tcp_ire_ill_check_done = B_TRUE; 18612 } 18613 18614 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18615 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18616 #ifndef _BIG_ENDIAN 18617 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18618 #endif 18619 18620 /* 18621 * Check to see if we need to re-enable MDT for this connection 18622 * because it was previously disabled due to changes in the ill; 18623 * note that by doing it here, this re-enabling only applies when 18624 * the packet is not dispatched through CALL_IP_WPUT(). 18625 * 18626 * That means for IPv4, it is worth re-enabling MDT for the fastpath 18627 * case, since that's how we ended up here. For IPv6, we do the 18628 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18629 */ 18630 if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18631 /* 18632 * Restore MDT for this connection, so that next time around 18633 * it is eligible to go through tcp_multisend() path again. 18634 */ 18635 TCP_STAT(tcp_mdt_conn_resumed1); 18636 tcp->tcp_mdt = B_TRUE; 18637 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18638 "interface %s\n", (void *)connp, ill->ill_name)); 18639 } 18640 18641 if (tcp->tcp_snd_zcopy_aware) { 18642 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18643 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18644 mp = tcp_zcopy_disable(tcp, mp); 18645 /* 18646 * we shouldn't need to reset ipha as the mp containing 18647 * ipha should never be a zero-copy mp. 18648 */ 18649 } 18650 18651 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18652 ASSERT(ill->ill_hcksum_capab != NULL); 18653 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18654 } 18655 18656 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18657 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18658 18659 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18660 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18661 18662 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18663 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18664 18665 /* Software checksum? */ 18666 if (DB_CKSUMFLAGS(mp) == 0) { 18667 TCP_STAT(tcp_out_sw_cksum); 18668 TCP_STAT_UPDATE(tcp_out_sw_cksum_bytes, 18669 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18670 } 18671 18672 ipha->ipha_fragment_offset_and_flags |= 18673 (uint32_t)htons(ire->ire_frag_flag); 18674 18675 /* Calculate IP header checksum if hardware isn't capable */ 18676 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18677 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18678 ((uint16_t *)ipha)[4]); 18679 } 18680 18681 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18682 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18683 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18684 18685 UPDATE_OB_PKT_COUNT(ire); 18686 ire->ire_last_used_time = lbolt; 18687 BUMP_MIB(&ip_mib, ipOutRequests); 18688 18689 if (ILL_DLS_CAPABLE(ill)) { 18690 /* 18691 * Send the packet directly to DLD, where it may be queued 18692 * depending on the availability of transmit resources at 18693 * the media layer. 18694 */ 18695 IP_DLS_ILL_TX(ill, ipha, mp); 18696 } else { 18697 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 18698 DTRACE_PROBE4(ip4__physical__out__start, 18699 ill_t *, NULL, ill_t *, out_ill, 18700 ipha_t *, ipha, mblk_t *, mp); 18701 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 18702 NULL, out_ill, ipha, mp, mp); 18703 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 18704 if (mp != NULL) 18705 putnext(ire->ire_stq, mp); 18706 } 18707 IRE_REFRELE(ire); 18708 } 18709 18710 /* 18711 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18712 * if the receiver shrinks the window, i.e. moves the right window to the 18713 * left, the we should not send new data, but should retransmit normally the 18714 * old unacked data between suna and suna + swnd. We might has sent data 18715 * that is now outside the new window, pretend that we didn't send it. 18716 */ 18717 static void 18718 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18719 { 18720 uint32_t snxt = tcp->tcp_snxt; 18721 mblk_t *xmit_tail; 18722 int32_t offset; 18723 18724 ASSERT(shrunk_count > 0); 18725 18726 /* Pretend we didn't send the data outside the window */ 18727 snxt -= shrunk_count; 18728 18729 /* Get the mblk and the offset in it per the shrunk window */ 18730 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 18731 18732 ASSERT(xmit_tail != NULL); 18733 18734 /* Reset all the values per the now shrunk window */ 18735 tcp->tcp_snxt = snxt; 18736 tcp->tcp_xmit_tail = xmit_tail; 18737 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 18738 offset; 18739 tcp->tcp_unsent += shrunk_count; 18740 18741 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18742 /* 18743 * Make sure the timer is running so that we will probe a zero 18744 * window. 18745 */ 18746 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18747 } 18748 18749 18750 /* 18751 * The TCP normal data output path. 18752 * NOTE: the logic of the fast path is duplicated from this function. 18753 */ 18754 static void 18755 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18756 { 18757 int len; 18758 mblk_t *local_time; 18759 mblk_t *mp1; 18760 uint32_t snxt; 18761 int tail_unsent; 18762 int tcpstate; 18763 int usable = 0; 18764 mblk_t *xmit_tail; 18765 queue_t *q = tcp->tcp_wq; 18766 int32_t mss; 18767 int32_t num_sack_blk = 0; 18768 int32_t tcp_hdr_len; 18769 int32_t tcp_tcp_hdr_len; 18770 int mdt_thres; 18771 int rc; 18772 18773 tcpstate = tcp->tcp_state; 18774 if (mp == NULL) { 18775 /* 18776 * tcp_wput_data() with NULL mp should only be called when 18777 * there is unsent data. 18778 */ 18779 ASSERT(tcp->tcp_unsent > 0); 18780 /* Really tacky... but we need this for detached closes. */ 18781 len = tcp->tcp_unsent; 18782 goto data_null; 18783 } 18784 18785 #if CCS_STATS 18786 wrw_stats.tot.count++; 18787 wrw_stats.tot.bytes += msgdsize(mp); 18788 #endif 18789 ASSERT(mp->b_datap->db_type == M_DATA); 18790 /* 18791 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18792 * or before a connection attempt has begun. 18793 */ 18794 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18795 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18796 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18797 #ifdef DEBUG 18798 cmn_err(CE_WARN, 18799 "tcp_wput_data: data after ordrel, %s", 18800 tcp_display(tcp, NULL, 18801 DISP_ADDR_AND_PORT)); 18802 #else 18803 if (tcp->tcp_debug) { 18804 (void) strlog(TCP_MOD_ID, 0, 1, 18805 SL_TRACE|SL_ERROR, 18806 "tcp_wput_data: data after ordrel, %s\n", 18807 tcp_display(tcp, NULL, 18808 DISP_ADDR_AND_PORT)); 18809 } 18810 #endif /* DEBUG */ 18811 } 18812 if (tcp->tcp_snd_zcopy_aware && 18813 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18814 tcp_zcopy_notify(tcp); 18815 freemsg(mp); 18816 if (tcp->tcp_flow_stopped && 18817 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18818 tcp_clrqfull(tcp); 18819 } 18820 return; 18821 } 18822 18823 /* Strip empties */ 18824 for (;;) { 18825 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18826 (uintptr_t)INT_MAX); 18827 len = (int)(mp->b_wptr - mp->b_rptr); 18828 if (len > 0) 18829 break; 18830 mp1 = mp; 18831 mp = mp->b_cont; 18832 freeb(mp1); 18833 if (!mp) { 18834 return; 18835 } 18836 } 18837 18838 /* If we are the first on the list ... */ 18839 if (tcp->tcp_xmit_head == NULL) { 18840 tcp->tcp_xmit_head = mp; 18841 tcp->tcp_xmit_tail = mp; 18842 tcp->tcp_xmit_tail_unsent = len; 18843 } else { 18844 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18845 struct datab *dp; 18846 18847 mp1 = tcp->tcp_xmit_last; 18848 if (len < tcp_tx_pull_len && 18849 (dp = mp1->b_datap)->db_ref == 1 && 18850 dp->db_lim - mp1->b_wptr >= len) { 18851 ASSERT(len > 0); 18852 ASSERT(!mp1->b_cont); 18853 if (len == 1) { 18854 *mp1->b_wptr++ = *mp->b_rptr; 18855 } else { 18856 bcopy(mp->b_rptr, mp1->b_wptr, len); 18857 mp1->b_wptr += len; 18858 } 18859 if (mp1 == tcp->tcp_xmit_tail) 18860 tcp->tcp_xmit_tail_unsent += len; 18861 mp1->b_cont = mp->b_cont; 18862 if (tcp->tcp_snd_zcopy_aware && 18863 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18864 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18865 freeb(mp); 18866 mp = mp1; 18867 } else { 18868 tcp->tcp_xmit_last->b_cont = mp; 18869 } 18870 len += tcp->tcp_unsent; 18871 } 18872 18873 /* Tack on however many more positive length mblks we have */ 18874 if ((mp1 = mp->b_cont) != NULL) { 18875 do { 18876 int tlen; 18877 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18878 (uintptr_t)INT_MAX); 18879 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18880 if (tlen <= 0) { 18881 mp->b_cont = mp1->b_cont; 18882 freeb(mp1); 18883 } else { 18884 len += tlen; 18885 mp = mp1; 18886 } 18887 } while ((mp1 = mp->b_cont) != NULL); 18888 } 18889 tcp->tcp_xmit_last = mp; 18890 tcp->tcp_unsent = len; 18891 18892 if (urgent) 18893 usable = 1; 18894 18895 data_null: 18896 snxt = tcp->tcp_snxt; 18897 xmit_tail = tcp->tcp_xmit_tail; 18898 tail_unsent = tcp->tcp_xmit_tail_unsent; 18899 18900 /* 18901 * Note that tcp_mss has been adjusted to take into account the 18902 * timestamp option if applicable. Because SACK options do not 18903 * appear in every TCP segments and they are of variable lengths, 18904 * they cannot be included in tcp_mss. Thus we need to calculate 18905 * the actual segment length when we need to send a segment which 18906 * includes SACK options. 18907 */ 18908 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 18909 int32_t opt_len; 18910 18911 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 18912 tcp->tcp_num_sack_blk); 18913 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 18914 2 + TCPOPT_HEADER_LEN; 18915 mss = tcp->tcp_mss - opt_len; 18916 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 18917 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 18918 } else { 18919 mss = tcp->tcp_mss; 18920 tcp_hdr_len = tcp->tcp_hdr_len; 18921 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 18922 } 18923 18924 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18925 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18926 SET_TCP_INIT_CWND(tcp, mss, tcp_slow_start_after_idle); 18927 } 18928 if (tcpstate == TCPS_SYN_RCVD) { 18929 /* 18930 * The three-way connection establishment handshake is not 18931 * complete yet. We want to queue the data for transmission 18932 * after entering ESTABLISHED state (RFC793). A jump to 18933 * "done" label effectively leaves data on the queue. 18934 */ 18935 goto done; 18936 } else { 18937 int usable_r; 18938 18939 /* 18940 * In the special case when cwnd is zero, which can only 18941 * happen if the connection is ECN capable, return now. 18942 * New segments is sent using tcp_timer(). The timer 18943 * is set in tcp_rput_data(). 18944 */ 18945 if (tcp->tcp_cwnd == 0) { 18946 /* 18947 * Note that tcp_cwnd is 0 before 3-way handshake is 18948 * finished. 18949 */ 18950 ASSERT(tcp->tcp_ecn_ok || 18951 tcp->tcp_state < TCPS_ESTABLISHED); 18952 return; 18953 } 18954 18955 /* NOTE: trouble if xmitting while SYN not acked? */ 18956 usable_r = snxt - tcp->tcp_suna; 18957 usable_r = tcp->tcp_swnd - usable_r; 18958 18959 /* 18960 * Check if the receiver has shrunk the window. If 18961 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 18962 * cannot be set as there is unsent data, so FIN cannot 18963 * be sent out. Otherwise, we need to take into account 18964 * of FIN as it consumes an "invisible" sequence number. 18965 */ 18966 ASSERT(tcp->tcp_fin_sent == 0); 18967 if (usable_r < 0) { 18968 /* 18969 * The receiver has shrunk the window and we have sent 18970 * -usable_r date beyond the window, re-adjust. 18971 * 18972 * If TCP window scaling is enabled, there can be 18973 * round down error as the advertised receive window 18974 * is actually right shifted n bits. This means that 18975 * the lower n bits info is wiped out. It will look 18976 * like the window is shrunk. Do a check here to 18977 * see if the shrunk amount is actually within the 18978 * error in window calculation. If it is, just 18979 * return. Note that this check is inside the 18980 * shrunk window check. This makes sure that even 18981 * though tcp_process_shrunk_swnd() is not called, 18982 * we will stop further processing. 18983 */ 18984 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 18985 tcp_process_shrunk_swnd(tcp, -usable_r); 18986 } 18987 return; 18988 } 18989 18990 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 18991 if (tcp->tcp_swnd > tcp->tcp_cwnd) 18992 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 18993 18994 /* usable = MIN(usable, unsent) */ 18995 if (usable_r > len) 18996 usable_r = len; 18997 18998 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 18999 if (usable_r > 0) { 19000 usable = usable_r; 19001 } else { 19002 /* Bypass all other unnecessary processing. */ 19003 goto done; 19004 } 19005 } 19006 19007 local_time = (mblk_t *)lbolt; 19008 19009 /* 19010 * "Our" Nagle Algorithm. This is not the same as in the old 19011 * BSD. This is more in line with the true intent of Nagle. 19012 * 19013 * The conditions are: 19014 * 1. The amount of unsent data (or amount of data which can be 19015 * sent, whichever is smaller) is less than Nagle limit. 19016 * 2. The last sent size is also less than Nagle limit. 19017 * 3. There is unack'ed data. 19018 * 4. Urgent pointer is not set. Send urgent data ignoring the 19019 * Nagle algorithm. This reduces the probability that urgent 19020 * bytes get "merged" together. 19021 * 5. The app has not closed the connection. This eliminates the 19022 * wait time of the receiving side waiting for the last piece of 19023 * (small) data. 19024 * 19025 * If all are satisified, exit without sending anything. Note 19026 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19027 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19028 * 4095). 19029 */ 19030 if (usable < (int)tcp->tcp_naglim && 19031 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19032 snxt != tcp->tcp_suna && 19033 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19034 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19035 goto done; 19036 } 19037 19038 if (tcp->tcp_cork) { 19039 /* 19040 * if the tcp->tcp_cork option is set, then we have to force 19041 * TCP not to send partial segment (smaller than MSS bytes). 19042 * We are calculating the usable now based on full mss and 19043 * will save the rest of remaining data for later. 19044 */ 19045 if (usable < mss) 19046 goto done; 19047 usable = (usable / mss) * mss; 19048 } 19049 19050 /* Update the latest receive window size in TCP header. */ 19051 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19052 tcp->tcp_tcph->th_win); 19053 19054 /* 19055 * Determine if it's worthwhile to attempt MDT, based on: 19056 * 19057 * 1. Simple TCP/IP{v4,v6} (no options). 19058 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19059 * 3. If the TCP connection is in ESTABLISHED state. 19060 * 4. The TCP is not detached. 19061 * 19062 * If any of the above conditions have changed during the 19063 * connection, stop using MDT and restore the stream head 19064 * parameters accordingly. 19065 */ 19066 if (tcp->tcp_mdt && 19067 ((tcp->tcp_ipversion == IPV4_VERSION && 19068 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19069 (tcp->tcp_ipversion == IPV6_VERSION && 19070 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19071 tcp->tcp_state != TCPS_ESTABLISHED || 19072 TCP_IS_DETACHED(tcp) || !CONN_IS_MD_FASTPATH(tcp->tcp_connp) || 19073 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19074 IPP_ENABLED(IPP_LOCAL_OUT))) { 19075 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19076 tcp->tcp_mdt = B_FALSE; 19077 19078 /* Anything other than detached is considered pathological */ 19079 if (!TCP_IS_DETACHED(tcp)) { 19080 TCP_STAT(tcp_mdt_conn_halted1); 19081 (void) tcp_maxpsz_set(tcp, B_TRUE); 19082 } 19083 } 19084 19085 /* Use MDT if sendable amount is greater than the threshold */ 19086 if (tcp->tcp_mdt && 19087 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19088 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19089 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19090 (tcp->tcp_valid_bits == 0 || 19091 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19092 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19093 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19094 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19095 local_time, mdt_thres); 19096 } else { 19097 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19098 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19099 local_time, INT_MAX); 19100 } 19101 19102 /* Pretend that all we were trying to send really got sent */ 19103 if (rc < 0 && tail_unsent < 0) { 19104 do { 19105 xmit_tail = xmit_tail->b_cont; 19106 xmit_tail->b_prev = local_time; 19107 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19108 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19109 tail_unsent += (int)(xmit_tail->b_wptr - 19110 xmit_tail->b_rptr); 19111 } while (tail_unsent < 0); 19112 } 19113 done:; 19114 tcp->tcp_xmit_tail = xmit_tail; 19115 tcp->tcp_xmit_tail_unsent = tail_unsent; 19116 len = tcp->tcp_snxt - snxt; 19117 if (len) { 19118 /* 19119 * If new data was sent, need to update the notsack 19120 * list, which is, afterall, data blocks that have 19121 * not been sack'ed by the receiver. New data is 19122 * not sack'ed. 19123 */ 19124 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19125 /* len is a negative value. */ 19126 tcp->tcp_pipe -= len; 19127 tcp_notsack_update(&(tcp->tcp_notsack_list), 19128 tcp->tcp_snxt, snxt, 19129 &(tcp->tcp_num_notsack_blk), 19130 &(tcp->tcp_cnt_notsack_list)); 19131 } 19132 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19133 tcp->tcp_rack = tcp->tcp_rnxt; 19134 tcp->tcp_rack_cnt = 0; 19135 if ((snxt + len) == tcp->tcp_suna) { 19136 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19137 } 19138 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19139 /* 19140 * Didn't send anything. Make sure the timer is running 19141 * so that we will probe a zero window. 19142 */ 19143 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19144 } 19145 /* Note that len is the amount we just sent but with a negative sign */ 19146 tcp->tcp_unsent += len; 19147 if (tcp->tcp_flow_stopped) { 19148 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19149 tcp_clrqfull(tcp); 19150 } 19151 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19152 tcp_setqfull(tcp); 19153 } 19154 } 19155 19156 /* 19157 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19158 * outgoing TCP header with the template header, as well as other 19159 * options such as time-stamp, ECN and/or SACK. 19160 */ 19161 static void 19162 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19163 { 19164 tcph_t *tcp_tmpl, *tcp_h; 19165 uint32_t *dst, *src; 19166 int hdrlen; 19167 19168 ASSERT(OK_32PTR(rptr)); 19169 19170 /* Template header */ 19171 tcp_tmpl = tcp->tcp_tcph; 19172 19173 /* Header of outgoing packet */ 19174 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19175 19176 /* dst and src are opaque 32-bit fields, used for copying */ 19177 dst = (uint32_t *)rptr; 19178 src = (uint32_t *)tcp->tcp_iphc; 19179 hdrlen = tcp->tcp_hdr_len; 19180 19181 /* Fill time-stamp option if needed */ 19182 if (tcp->tcp_snd_ts_ok) { 19183 U32_TO_BE32((uint32_t)now, 19184 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19185 U32_TO_BE32(tcp->tcp_ts_recent, 19186 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19187 } else { 19188 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19189 } 19190 19191 /* 19192 * Copy the template header; is this really more efficient than 19193 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19194 * but perhaps not for other scenarios. 19195 */ 19196 dst[0] = src[0]; 19197 dst[1] = src[1]; 19198 dst[2] = src[2]; 19199 dst[3] = src[3]; 19200 dst[4] = src[4]; 19201 dst[5] = src[5]; 19202 dst[6] = src[6]; 19203 dst[7] = src[7]; 19204 dst[8] = src[8]; 19205 dst[9] = src[9]; 19206 if (hdrlen -= 40) { 19207 hdrlen >>= 2; 19208 dst += 10; 19209 src += 10; 19210 do { 19211 *dst++ = *src++; 19212 } while (--hdrlen); 19213 } 19214 19215 /* 19216 * Set the ECN info in the TCP header if it is not a zero 19217 * window probe. Zero window probe is only sent in 19218 * tcp_wput_data() and tcp_timer(). 19219 */ 19220 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19221 SET_ECT(tcp, rptr); 19222 19223 if (tcp->tcp_ecn_echo_on) 19224 tcp_h->th_flags[0] |= TH_ECE; 19225 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19226 tcp_h->th_flags[0] |= TH_CWR; 19227 tcp->tcp_ecn_cwr_sent = B_TRUE; 19228 } 19229 } 19230 19231 /* Fill in SACK options */ 19232 if (num_sack_blk > 0) { 19233 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19234 sack_blk_t *tmp; 19235 int32_t i; 19236 19237 wptr[0] = TCPOPT_NOP; 19238 wptr[1] = TCPOPT_NOP; 19239 wptr[2] = TCPOPT_SACK; 19240 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19241 sizeof (sack_blk_t); 19242 wptr += TCPOPT_REAL_SACK_LEN; 19243 19244 tmp = tcp->tcp_sack_list; 19245 for (i = 0; i < num_sack_blk; i++) { 19246 U32_TO_BE32(tmp[i].begin, wptr); 19247 wptr += sizeof (tcp_seq); 19248 U32_TO_BE32(tmp[i].end, wptr); 19249 wptr += sizeof (tcp_seq); 19250 } 19251 tcp_h->th_offset_and_rsrvd[0] += 19252 ((num_sack_blk * 2 + 1) << 4); 19253 } 19254 } 19255 19256 /* 19257 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19258 * the destination address and SAP attribute, and if necessary, the 19259 * hardware checksum offload attribute to a Multidata message. 19260 */ 19261 static int 19262 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19263 const uint32_t start, const uint32_t stuff, const uint32_t end, 19264 const uint32_t flags) 19265 { 19266 /* Add global destination address & SAP attribute */ 19267 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19268 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19269 "destination address+SAP\n")); 19270 19271 if (dlmp != NULL) 19272 TCP_STAT(tcp_mdt_allocfail); 19273 return (-1); 19274 } 19275 19276 /* Add global hwcksum attribute */ 19277 if (hwcksum && 19278 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19279 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19280 "checksum attribute\n")); 19281 19282 TCP_STAT(tcp_mdt_allocfail); 19283 return (-1); 19284 } 19285 19286 return (0); 19287 } 19288 19289 /* 19290 * Smaller and private version of pdescinfo_t used specifically for TCP, 19291 * which allows for only two payload spans per packet. 19292 */ 19293 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19294 19295 /* 19296 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19297 * scheme, and returns one the following: 19298 * 19299 * -1 = failed allocation. 19300 * 0 = success; burst count reached, or usable send window is too small, 19301 * and that we'd rather wait until later before sending again. 19302 */ 19303 static int 19304 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19305 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19306 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19307 const int mdt_thres) 19308 { 19309 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19310 multidata_t *mmd; 19311 uint_t obsegs, obbytes, hdr_frag_sz; 19312 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19313 int num_burst_seg, max_pld; 19314 pdesc_t *pkt; 19315 tcp_pdescinfo_t tcp_pkt_info; 19316 pdescinfo_t *pkt_info; 19317 int pbuf_idx, pbuf_idx_nxt; 19318 int seg_len, len, spill, af; 19319 boolean_t add_buffer, zcopy, clusterwide; 19320 boolean_t buf_trunked = B_FALSE; 19321 boolean_t rconfirm = B_FALSE; 19322 boolean_t done = B_FALSE; 19323 uint32_t cksum; 19324 uint32_t hwcksum_flags; 19325 ire_t *ire; 19326 ill_t *ill; 19327 ipha_t *ipha; 19328 ip6_t *ip6h; 19329 ipaddr_t src, dst; 19330 ill_zerocopy_capab_t *zc_cap = NULL; 19331 uint16_t *up; 19332 int err; 19333 conn_t *connp; 19334 mblk_t *mp, *mp1, *fw_mp_head = NULL; 19335 uchar_t *pld_start; 19336 19337 #ifdef _BIG_ENDIAN 19338 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19339 #else 19340 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19341 #endif 19342 19343 #define PREP_NEW_MULTIDATA() { \ 19344 mmd = NULL; \ 19345 md_mp = md_hbuf = NULL; \ 19346 cur_hdr_off = 0; \ 19347 max_pld = tcp->tcp_mdt_max_pld; \ 19348 pbuf_idx = pbuf_idx_nxt = -1; \ 19349 add_buffer = B_TRUE; \ 19350 zcopy = B_FALSE; \ 19351 } 19352 19353 #define PREP_NEW_PBUF() { \ 19354 md_pbuf = md_pbuf_nxt = NULL; \ 19355 pbuf_idx = pbuf_idx_nxt = -1; \ 19356 cur_pld_off = 0; \ 19357 first_snxt = *snxt; \ 19358 ASSERT(*tail_unsent > 0); \ 19359 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19360 } 19361 19362 ASSERT(mdt_thres >= mss); 19363 ASSERT(*usable > 0 && *usable > mdt_thres); 19364 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19365 ASSERT(!TCP_IS_DETACHED(tcp)); 19366 ASSERT(tcp->tcp_valid_bits == 0 || 19367 tcp->tcp_valid_bits == TCP_FSS_VALID); 19368 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19369 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19370 (tcp->tcp_ipversion == IPV6_VERSION && 19371 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19372 19373 connp = tcp->tcp_connp; 19374 ASSERT(connp != NULL); 19375 ASSERT(CONN_IS_MD_FASTPATH(connp)); 19376 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19377 19378 /* 19379 * Note that tcp will only declare at most 2 payload spans per 19380 * packet, which is much lower than the maximum allowable number 19381 * of packet spans per Multidata. For this reason, we use the 19382 * privately declared and smaller descriptor info structure, in 19383 * order to save some stack space. 19384 */ 19385 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19386 19387 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19388 if (af == AF_INET) { 19389 dst = tcp->tcp_ipha->ipha_dst; 19390 src = tcp->tcp_ipha->ipha_src; 19391 ASSERT(!CLASSD(dst)); 19392 } 19393 ASSERT(af == AF_INET || 19394 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19395 19396 obsegs = obbytes = 0; 19397 num_burst_seg = tcp->tcp_snd_burst; 19398 md_mp_head = NULL; 19399 PREP_NEW_MULTIDATA(); 19400 19401 /* 19402 * Before we go on further, make sure there is an IRE that we can 19403 * use, and that the ILL supports MDT. Otherwise, there's no point 19404 * in proceeding any further, and we should just hand everything 19405 * off to the legacy path. 19406 */ 19407 mutex_enter(&connp->conn_lock); 19408 ire = connp->conn_ire_cache; 19409 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19410 if (ire != NULL && ((af == AF_INET && ire->ire_addr == dst) || 19411 (af == AF_INET6 && IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, 19412 &tcp->tcp_ip6h->ip6_dst))) && 19413 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19414 IRE_REFHOLD(ire); 19415 mutex_exit(&connp->conn_lock); 19416 } else { 19417 boolean_t cached = B_FALSE; 19418 ts_label_t *tsl; 19419 19420 /* force a recheck later on */ 19421 tcp->tcp_ire_ill_check_done = B_FALSE; 19422 19423 TCP_DBGSTAT(tcp_ire_null1); 19424 connp->conn_ire_cache = NULL; 19425 mutex_exit(&connp->conn_lock); 19426 19427 /* Release the old ire */ 19428 if (ire != NULL) 19429 IRE_REFRELE_NOTR(ire); 19430 19431 tsl = crgetlabel(CONN_CRED(connp)); 19432 ire = (af == AF_INET) ? 19433 ire_cache_lookup(dst, connp->conn_zoneid, tsl) : 19434 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19435 connp->conn_zoneid, tsl); 19436 19437 if (ire == NULL) { 19438 TCP_STAT(tcp_ire_null); 19439 goto legacy_send_no_md; 19440 } 19441 19442 IRE_REFHOLD_NOTR(ire); 19443 /* 19444 * Since we are inside the squeue, there cannot be another 19445 * thread in TCP trying to set the conn_ire_cache now. The 19446 * check for IRE_MARK_CONDEMNED ensures that an interface 19447 * unplumb thread has not yet started cleaning up the conns. 19448 * Hence we don't need to grab the conn lock. 19449 */ 19450 if (!(connp->conn_state_flags & CONN_CLOSING)) { 19451 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19452 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19453 connp->conn_ire_cache = ire; 19454 cached = B_TRUE; 19455 } 19456 rw_exit(&ire->ire_bucket->irb_lock); 19457 } 19458 19459 /* 19460 * We can continue to use the ire but since it was not 19461 * cached, we should drop the extra reference. 19462 */ 19463 if (!cached) 19464 IRE_REFRELE_NOTR(ire); 19465 } 19466 19467 ASSERT(ire != NULL); 19468 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19469 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19470 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19471 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19472 /* 19473 * If we do support loopback for MDT (which requires modifications 19474 * to the receiving paths), the following assertions should go away, 19475 * and we would be sending the Multidata to loopback conn later on. 19476 */ 19477 ASSERT(!IRE_IS_LOCAL(ire)); 19478 ASSERT(ire->ire_stq != NULL); 19479 19480 ill = ire_to_ill(ire); 19481 ASSERT(ill != NULL); 19482 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19483 19484 if (!tcp->tcp_ire_ill_check_done) { 19485 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19486 tcp->tcp_ire_ill_check_done = B_TRUE; 19487 } 19488 19489 /* 19490 * If the underlying interface conditions have changed, or if the 19491 * new interface does not support MDT, go back to legacy path. 19492 */ 19493 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19494 /* don't go through this path anymore for this connection */ 19495 TCP_STAT(tcp_mdt_conn_halted2); 19496 tcp->tcp_mdt = B_FALSE; 19497 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19498 "interface %s\n", (void *)connp, ill->ill_name)); 19499 /* IRE will be released prior to returning */ 19500 goto legacy_send_no_md; 19501 } 19502 19503 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19504 zc_cap = ill->ill_zerocopy_capab; 19505 19506 /* 19507 * Check if we can take tcp fast-path. Note that "incomplete" 19508 * ire's (where the link-layer for next hop is not resolved 19509 * or where the fast-path header in nce_fp_mp is not available 19510 * yet) are sent down the legacy (slow) path. 19511 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19512 */ 19513 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19514 /* IRE will be released prior to returning */ 19515 goto legacy_send_no_md; 19516 } 19517 19518 /* go to legacy path if interface doesn't support zerocopy */ 19519 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19520 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19521 /* IRE will be released prior to returning */ 19522 goto legacy_send_no_md; 19523 } 19524 19525 /* does the interface support hardware checksum offload? */ 19526 hwcksum_flags = 0; 19527 if (ILL_HCKSUM_CAPABLE(ill) && 19528 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19529 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19530 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19531 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19532 HCKSUM_IPHDRCKSUM) 19533 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19534 19535 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19536 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19537 hwcksum_flags |= HCK_FULLCKSUM; 19538 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19539 HCKSUM_INET_PARTIAL) 19540 hwcksum_flags |= HCK_PARTIALCKSUM; 19541 } 19542 19543 /* 19544 * Each header fragment consists of the leading extra space, 19545 * followed by the TCP/IP header, and the trailing extra space. 19546 * We make sure that each header fragment begins on a 32-bit 19547 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19548 * aligned in tcp_mdt_update). 19549 */ 19550 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19551 tcp->tcp_mdt_hdr_tail), 4); 19552 19553 /* are we starting from the beginning of data block? */ 19554 if (*tail_unsent == 0) { 19555 *xmit_tail = (*xmit_tail)->b_cont; 19556 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19557 *tail_unsent = (int)MBLKL(*xmit_tail); 19558 } 19559 19560 /* 19561 * Here we create one or more Multidata messages, each made up of 19562 * one header buffer and up to N payload buffers. This entire 19563 * operation is done within two loops: 19564 * 19565 * The outer loop mostly deals with creating the Multidata message, 19566 * as well as the header buffer that gets added to it. It also 19567 * links the Multidata messages together such that all of them can 19568 * be sent down to the lower layer in a single putnext call; this 19569 * linking behavior depends on the tcp_mdt_chain tunable. 19570 * 19571 * The inner loop takes an existing Multidata message, and adds 19572 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19573 * packetizes those buffers by filling up the corresponding header 19574 * buffer fragments with the proper IP and TCP headers, and by 19575 * describing the layout of each packet in the packet descriptors 19576 * that get added to the Multidata. 19577 */ 19578 do { 19579 /* 19580 * If usable send window is too small, or data blocks in 19581 * transmit list are smaller than our threshold (i.e. app 19582 * performs large writes followed by small ones), we hand 19583 * off the control over to the legacy path. Note that we'll 19584 * get back the control once it encounters a large block. 19585 */ 19586 if (*usable < mss || (*tail_unsent <= mdt_thres && 19587 (*xmit_tail)->b_cont != NULL && 19588 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19589 /* send down what we've got so far */ 19590 if (md_mp_head != NULL) { 19591 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19592 obsegs, obbytes, &rconfirm); 19593 } 19594 /* 19595 * Pass control over to tcp_send(), but tell it to 19596 * return to us once a large-size transmission is 19597 * possible. 19598 */ 19599 TCP_STAT(tcp_mdt_legacy_small); 19600 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19601 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19602 tail_unsent, xmit_tail, local_time, 19603 mdt_thres)) <= 0) { 19604 /* burst count reached, or alloc failed */ 19605 IRE_REFRELE(ire); 19606 return (err); 19607 } 19608 19609 /* tcp_send() may have sent everything, so check */ 19610 if (*usable <= 0) { 19611 IRE_REFRELE(ire); 19612 return (0); 19613 } 19614 19615 TCP_STAT(tcp_mdt_legacy_ret); 19616 /* 19617 * We may have delivered the Multidata, so make sure 19618 * to re-initialize before the next round. 19619 */ 19620 md_mp_head = NULL; 19621 obsegs = obbytes = 0; 19622 num_burst_seg = tcp->tcp_snd_burst; 19623 PREP_NEW_MULTIDATA(); 19624 19625 /* are we starting from the beginning of data block? */ 19626 if (*tail_unsent == 0) { 19627 *xmit_tail = (*xmit_tail)->b_cont; 19628 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19629 (uintptr_t)INT_MAX); 19630 *tail_unsent = (int)MBLKL(*xmit_tail); 19631 } 19632 } 19633 19634 /* 19635 * max_pld limits the number of mblks in tcp's transmit 19636 * queue that can be added to a Multidata message. Once 19637 * this counter reaches zero, no more additional mblks 19638 * can be added to it. What happens afterwards depends 19639 * on whether or not we are set to chain the Multidata 19640 * messages. If we are to link them together, reset 19641 * max_pld to its original value (tcp_mdt_max_pld) and 19642 * prepare to create a new Multidata message which will 19643 * get linked to md_mp_head. Else, leave it alone and 19644 * let the inner loop break on its own. 19645 */ 19646 if (tcp_mdt_chain && max_pld == 0) 19647 PREP_NEW_MULTIDATA(); 19648 19649 /* adding a payload buffer; re-initialize values */ 19650 if (add_buffer) 19651 PREP_NEW_PBUF(); 19652 19653 /* 19654 * If we don't have a Multidata, either because we just 19655 * (re)entered this outer loop, or after we branched off 19656 * to tcp_send above, setup the Multidata and header 19657 * buffer to be used. 19658 */ 19659 if (md_mp == NULL) { 19660 int md_hbuflen; 19661 uint32_t start, stuff; 19662 19663 /* 19664 * Calculate Multidata header buffer size large enough 19665 * to hold all of the headers that can possibly be 19666 * sent at this moment. We'd rather over-estimate 19667 * the size than running out of space; this is okay 19668 * since this buffer is small anyway. 19669 */ 19670 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19671 19672 /* 19673 * Start and stuff offset for partial hardware 19674 * checksum offload; these are currently for IPv4. 19675 * For full checksum offload, they are set to zero. 19676 */ 19677 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19678 if (af == AF_INET) { 19679 start = IP_SIMPLE_HDR_LENGTH; 19680 stuff = IP_SIMPLE_HDR_LENGTH + 19681 TCP_CHECKSUM_OFFSET; 19682 } else { 19683 start = IPV6_HDR_LEN; 19684 stuff = IPV6_HDR_LEN + 19685 TCP_CHECKSUM_OFFSET; 19686 } 19687 } else { 19688 start = stuff = 0; 19689 } 19690 19691 /* 19692 * Create the header buffer, Multidata, as well as 19693 * any necessary attributes (destination address, 19694 * SAP and hardware checksum offload) that should 19695 * be associated with the Multidata message. 19696 */ 19697 ASSERT(cur_hdr_off == 0); 19698 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19699 ((md_hbuf->b_wptr += md_hbuflen), 19700 (mmd = mmd_alloc(md_hbuf, &md_mp, 19701 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19702 /* fastpath mblk */ 19703 ire->ire_nce->nce_res_mp, 19704 /* hardware checksum enabled */ 19705 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19706 /* hardware checksum offsets */ 19707 start, stuff, 0, 19708 /* hardware checksum flag */ 19709 hwcksum_flags) != 0)) { 19710 legacy_send: 19711 if (md_mp != NULL) { 19712 /* Unlink message from the chain */ 19713 if (md_mp_head != NULL) { 19714 err = (intptr_t)rmvb(md_mp_head, 19715 md_mp); 19716 /* 19717 * We can't assert that rmvb 19718 * did not return -1, since we 19719 * may get here before linkb 19720 * happens. We do, however, 19721 * check if we just removed the 19722 * only element in the list. 19723 */ 19724 if (err == 0) 19725 md_mp_head = NULL; 19726 } 19727 /* md_hbuf gets freed automatically */ 19728 TCP_STAT(tcp_mdt_discarded); 19729 freeb(md_mp); 19730 } else { 19731 /* Either allocb or mmd_alloc failed */ 19732 TCP_STAT(tcp_mdt_allocfail); 19733 if (md_hbuf != NULL) 19734 freeb(md_hbuf); 19735 } 19736 19737 /* send down what we've got so far */ 19738 if (md_mp_head != NULL) { 19739 tcp_multisend_data(tcp, ire, ill, 19740 md_mp_head, obsegs, obbytes, 19741 &rconfirm); 19742 } 19743 legacy_send_no_md: 19744 if (ire != NULL) 19745 IRE_REFRELE(ire); 19746 /* 19747 * Too bad; let the legacy path handle this. 19748 * We specify INT_MAX for the threshold, since 19749 * we gave up with the Multidata processings 19750 * and let the old path have it all. 19751 */ 19752 TCP_STAT(tcp_mdt_legacy_all); 19753 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19754 tcp_tcp_hdr_len, num_sack_blk, usable, 19755 snxt, tail_unsent, xmit_tail, local_time, 19756 INT_MAX)); 19757 } 19758 19759 /* link to any existing ones, if applicable */ 19760 TCP_STAT(tcp_mdt_allocd); 19761 if (md_mp_head == NULL) { 19762 md_mp_head = md_mp; 19763 } else if (tcp_mdt_chain) { 19764 TCP_STAT(tcp_mdt_linked); 19765 linkb(md_mp_head, md_mp); 19766 } 19767 } 19768 19769 ASSERT(md_mp_head != NULL); 19770 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19771 ASSERT(md_mp != NULL && mmd != NULL); 19772 ASSERT(md_hbuf != NULL); 19773 19774 /* 19775 * Packetize the transmittable portion of the data block; 19776 * each data block is essentially added to the Multidata 19777 * as a payload buffer. We also deal with adding more 19778 * than one payload buffers, which happens when the remaining 19779 * packetized portion of the current payload buffer is less 19780 * than MSS, while the next data block in transmit queue 19781 * has enough data to make up for one. This "spillover" 19782 * case essentially creates a split-packet, where portions 19783 * of the packet's payload fragments may span across two 19784 * virtually discontiguous address blocks. 19785 */ 19786 seg_len = mss; 19787 do { 19788 len = seg_len; 19789 19790 ASSERT(len > 0); 19791 ASSERT(max_pld >= 0); 19792 ASSERT(!add_buffer || cur_pld_off == 0); 19793 19794 /* 19795 * First time around for this payload buffer; note 19796 * in the case of a spillover, the following has 19797 * been done prior to adding the split-packet 19798 * descriptor to Multidata, and we don't want to 19799 * repeat the process. 19800 */ 19801 if (add_buffer) { 19802 ASSERT(mmd != NULL); 19803 ASSERT(md_pbuf == NULL); 19804 ASSERT(md_pbuf_nxt == NULL); 19805 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19806 19807 /* 19808 * Have we reached the limit? We'd get to 19809 * this case when we're not chaining the 19810 * Multidata messages together, and since 19811 * we're done, terminate this loop. 19812 */ 19813 if (max_pld == 0) 19814 break; /* done */ 19815 19816 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19817 TCP_STAT(tcp_mdt_allocfail); 19818 goto legacy_send; /* out_of_mem */ 19819 } 19820 19821 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19822 zc_cap != NULL) { 19823 if (!ip_md_zcopy_attr(mmd, NULL, 19824 zc_cap->ill_zerocopy_flags)) { 19825 freeb(md_pbuf); 19826 TCP_STAT(tcp_mdt_allocfail); 19827 /* out_of_mem */ 19828 goto legacy_send; 19829 } 19830 zcopy = B_TRUE; 19831 } 19832 19833 md_pbuf->b_rptr += base_pld_off; 19834 19835 /* 19836 * Add a payload buffer to the Multidata; this 19837 * operation must not fail, or otherwise our 19838 * logic in this routine is broken. There 19839 * is no memory allocation done by the 19840 * routine, so any returned failure simply 19841 * tells us that we've done something wrong. 19842 * 19843 * A failure tells us that either we're adding 19844 * the same payload buffer more than once, or 19845 * we're trying to add more buffers than 19846 * allowed (max_pld calculation is wrong). 19847 * None of the above cases should happen, and 19848 * we panic because either there's horrible 19849 * heap corruption, and/or programming mistake. 19850 */ 19851 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19852 if (pbuf_idx < 0) { 19853 cmn_err(CE_PANIC, "tcp_multisend: " 19854 "payload buffer logic error " 19855 "detected for tcp %p mmd %p " 19856 "pbuf %p (%d)\n", 19857 (void *)tcp, (void *)mmd, 19858 (void *)md_pbuf, pbuf_idx); 19859 } 19860 19861 ASSERT(max_pld > 0); 19862 --max_pld; 19863 add_buffer = B_FALSE; 19864 } 19865 19866 ASSERT(md_mp_head != NULL); 19867 ASSERT(md_pbuf != NULL); 19868 ASSERT(md_pbuf_nxt == NULL); 19869 ASSERT(pbuf_idx != -1); 19870 ASSERT(pbuf_idx_nxt == -1); 19871 ASSERT(*usable > 0); 19872 19873 /* 19874 * We spillover to the next payload buffer only 19875 * if all of the following is true: 19876 * 19877 * 1. There is not enough data on the current 19878 * payload buffer to make up `len', 19879 * 2. We are allowed to send `len', 19880 * 3. The next payload buffer length is large 19881 * enough to accomodate `spill'. 19882 */ 19883 if ((spill = len - *tail_unsent) > 0 && 19884 *usable >= len && 19885 MBLKL((*xmit_tail)->b_cont) >= spill && 19886 max_pld > 0) { 19887 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19888 if (md_pbuf_nxt == NULL) { 19889 TCP_STAT(tcp_mdt_allocfail); 19890 goto legacy_send; /* out_of_mem */ 19891 } 19892 19893 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19894 zc_cap != NULL) { 19895 if (!ip_md_zcopy_attr(mmd, NULL, 19896 zc_cap->ill_zerocopy_flags)) { 19897 freeb(md_pbuf_nxt); 19898 TCP_STAT(tcp_mdt_allocfail); 19899 /* out_of_mem */ 19900 goto legacy_send; 19901 } 19902 zcopy = B_TRUE; 19903 } 19904 19905 /* 19906 * See comments above on the first call to 19907 * mmd_addpldbuf for explanation on the panic. 19908 */ 19909 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19910 if (pbuf_idx_nxt < 0) { 19911 panic("tcp_multisend: " 19912 "next payload buffer logic error " 19913 "detected for tcp %p mmd %p " 19914 "pbuf %p (%d)\n", 19915 (void *)tcp, (void *)mmd, 19916 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19917 } 19918 19919 ASSERT(max_pld > 0); 19920 --max_pld; 19921 } else if (spill > 0) { 19922 /* 19923 * If there's a spillover, but the following 19924 * xmit_tail couldn't give us enough octets 19925 * to reach "len", then stop the current 19926 * Multidata creation and let the legacy 19927 * tcp_send() path take over. We don't want 19928 * to send the tiny segment as part of this 19929 * Multidata for performance reasons; instead, 19930 * we let the legacy path deal with grouping 19931 * it with the subsequent small mblks. 19932 */ 19933 if (*usable >= len && 19934 MBLKL((*xmit_tail)->b_cont) < spill) { 19935 max_pld = 0; 19936 break; /* done */ 19937 } 19938 19939 /* 19940 * We can't spillover, and we are near 19941 * the end of the current payload buffer, 19942 * so send what's left. 19943 */ 19944 ASSERT(*tail_unsent > 0); 19945 len = *tail_unsent; 19946 } 19947 19948 /* tail_unsent is negated if there is a spillover */ 19949 *tail_unsent -= len; 19950 *usable -= len; 19951 ASSERT(*usable >= 0); 19952 19953 if (*usable < mss) 19954 seg_len = *usable; 19955 /* 19956 * Sender SWS avoidance; see comments in tcp_send(); 19957 * everything else is the same, except that we only 19958 * do this here if there is no more data to be sent 19959 * following the current xmit_tail. We don't check 19960 * for 1-byte urgent data because we shouldn't get 19961 * here if TCP_URG_VALID is set. 19962 */ 19963 if (*usable > 0 && *usable < mss && 19964 ((md_pbuf_nxt == NULL && 19965 (*xmit_tail)->b_cont == NULL) || 19966 (md_pbuf_nxt != NULL && 19967 (*xmit_tail)->b_cont->b_cont == NULL)) && 19968 seg_len < (tcp->tcp_max_swnd >> 1) && 19969 (tcp->tcp_unsent - 19970 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 19971 !tcp->tcp_zero_win_probe) { 19972 if ((*snxt + len) == tcp->tcp_snxt && 19973 (*snxt + len) == tcp->tcp_suna) { 19974 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19975 } 19976 done = B_TRUE; 19977 } 19978 19979 /* 19980 * Prime pump for IP's checksumming on our behalf; 19981 * include the adjustment for a source route if any. 19982 * Do this only for software/partial hardware checksum 19983 * offload, as this field gets zeroed out later for 19984 * the full hardware checksum offload case. 19985 */ 19986 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 19987 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19988 cksum = (cksum >> 16) + (cksum & 0xFFFF); 19989 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 19990 } 19991 19992 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 19993 *snxt += len; 19994 19995 tcp->tcp_tcph->th_flags[0] = TH_ACK; 19996 /* 19997 * We set the PUSH bit only if TCP has no more buffered 19998 * data to be transmitted (or if sender SWS avoidance 19999 * takes place), as opposed to setting it for every 20000 * last packet in the burst. 20001 */ 20002 if (done || 20003 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20004 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20005 20006 /* 20007 * Set FIN bit if this is our last segment; snxt 20008 * already includes its length, and it will not 20009 * be adjusted after this point. 20010 */ 20011 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20012 *snxt == tcp->tcp_fss) { 20013 if (!tcp->tcp_fin_acked) { 20014 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20015 BUMP_MIB(&tcp_mib, tcpOutControl); 20016 } 20017 if (!tcp->tcp_fin_sent) { 20018 tcp->tcp_fin_sent = B_TRUE; 20019 /* 20020 * tcp state must be ESTABLISHED 20021 * in order for us to get here in 20022 * the first place. 20023 */ 20024 tcp->tcp_state = TCPS_FIN_WAIT_1; 20025 20026 /* 20027 * Upon returning from this routine, 20028 * tcp_wput_data() will set tcp_snxt 20029 * to be equal to snxt + tcp_fin_sent. 20030 * This is essentially the same as 20031 * setting it to tcp_fss + 1. 20032 */ 20033 } 20034 } 20035 20036 tcp->tcp_last_sent_len = (ushort_t)len; 20037 20038 len += tcp_hdr_len; 20039 if (tcp->tcp_ipversion == IPV4_VERSION) 20040 tcp->tcp_ipha->ipha_length = htons(len); 20041 else 20042 tcp->tcp_ip6h->ip6_plen = htons(len - 20043 ((char *)&tcp->tcp_ip6h[1] - 20044 tcp->tcp_iphc)); 20045 20046 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20047 20048 /* setup header fragment */ 20049 PDESC_HDR_ADD(pkt_info, 20050 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20051 tcp->tcp_mdt_hdr_head, /* head room */ 20052 tcp_hdr_len, /* len */ 20053 tcp->tcp_mdt_hdr_tail); /* tail room */ 20054 20055 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20056 hdr_frag_sz); 20057 ASSERT(MBLKIN(md_hbuf, 20058 (pkt_info->hdr_base - md_hbuf->b_rptr), 20059 PDESC_HDRSIZE(pkt_info))); 20060 20061 /* setup first payload fragment */ 20062 PDESC_PLD_INIT(pkt_info); 20063 PDESC_PLD_SPAN_ADD(pkt_info, 20064 pbuf_idx, /* index */ 20065 md_pbuf->b_rptr + cur_pld_off, /* start */ 20066 tcp->tcp_last_sent_len); /* len */ 20067 20068 /* create a split-packet in case of a spillover */ 20069 if (md_pbuf_nxt != NULL) { 20070 ASSERT(spill > 0); 20071 ASSERT(pbuf_idx_nxt > pbuf_idx); 20072 ASSERT(!add_buffer); 20073 20074 md_pbuf = md_pbuf_nxt; 20075 md_pbuf_nxt = NULL; 20076 pbuf_idx = pbuf_idx_nxt; 20077 pbuf_idx_nxt = -1; 20078 cur_pld_off = spill; 20079 20080 /* trim out first payload fragment */ 20081 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20082 20083 /* setup second payload fragment */ 20084 PDESC_PLD_SPAN_ADD(pkt_info, 20085 pbuf_idx, /* index */ 20086 md_pbuf->b_rptr, /* start */ 20087 spill); /* len */ 20088 20089 if ((*xmit_tail)->b_next == NULL) { 20090 /* 20091 * Store the lbolt used for RTT 20092 * estimation. We can only record one 20093 * timestamp per mblk so we do it when 20094 * we reach the end of the payload 20095 * buffer. Also we only take a new 20096 * timestamp sample when the previous 20097 * timed data from the same mblk has 20098 * been ack'ed. 20099 */ 20100 (*xmit_tail)->b_prev = local_time; 20101 (*xmit_tail)->b_next = 20102 (mblk_t *)(uintptr_t)first_snxt; 20103 } 20104 20105 first_snxt = *snxt - spill; 20106 20107 /* 20108 * Advance xmit_tail; usable could be 0 by 20109 * the time we got here, but we made sure 20110 * above that we would only spillover to 20111 * the next data block if usable includes 20112 * the spilled-over amount prior to the 20113 * subtraction. Therefore, we are sure 20114 * that xmit_tail->b_cont can't be NULL. 20115 */ 20116 ASSERT((*xmit_tail)->b_cont != NULL); 20117 *xmit_tail = (*xmit_tail)->b_cont; 20118 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20119 (uintptr_t)INT_MAX); 20120 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20121 } else { 20122 cur_pld_off += tcp->tcp_last_sent_len; 20123 } 20124 20125 /* 20126 * Fill in the header using the template header, and 20127 * add options such as time-stamp, ECN and/or SACK, 20128 * as needed. 20129 */ 20130 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20131 (clock_t)local_time, num_sack_blk); 20132 20133 /* take care of some IP header businesses */ 20134 if (af == AF_INET) { 20135 ipha = (ipha_t *)pkt_info->hdr_rptr; 20136 20137 ASSERT(OK_32PTR((uchar_t *)ipha)); 20138 ASSERT(PDESC_HDRL(pkt_info) >= 20139 IP_SIMPLE_HDR_LENGTH); 20140 ASSERT(ipha->ipha_version_and_hdr_length == 20141 IP_SIMPLE_HDR_VERSION); 20142 20143 /* 20144 * Assign ident value for current packet; see 20145 * related comments in ip_wput_ire() about the 20146 * contract private interface with clustering 20147 * group. 20148 */ 20149 clusterwide = B_FALSE; 20150 if (cl_inet_ipident != NULL) { 20151 ASSERT(cl_inet_isclusterwide != NULL); 20152 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20153 AF_INET, 20154 (uint8_t *)(uintptr_t)src)) { 20155 ipha->ipha_ident = 20156 (*cl_inet_ipident) 20157 (IPPROTO_IP, AF_INET, 20158 (uint8_t *)(uintptr_t)src, 20159 (uint8_t *)(uintptr_t)dst); 20160 clusterwide = B_TRUE; 20161 } 20162 } 20163 20164 if (!clusterwide) { 20165 ipha->ipha_ident = (uint16_t) 20166 atomic_add_32_nv( 20167 &ire->ire_ident, 1); 20168 } 20169 #ifndef _BIG_ENDIAN 20170 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20171 (ipha->ipha_ident >> 8); 20172 #endif 20173 } else { 20174 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20175 20176 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20177 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20178 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20179 ASSERT(PDESC_HDRL(pkt_info) >= 20180 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20181 TCP_CHECKSUM_SIZE)); 20182 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20183 20184 if (tcp->tcp_ip_forward_progress) { 20185 rconfirm = B_TRUE; 20186 tcp->tcp_ip_forward_progress = B_FALSE; 20187 } 20188 } 20189 20190 /* at least one payload span, and at most two */ 20191 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20192 20193 /* add the packet descriptor to Multidata */ 20194 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20195 KM_NOSLEEP)) == NULL) { 20196 /* 20197 * Any failure other than ENOMEM indicates 20198 * that we have passed in invalid pkt_info 20199 * or parameters to mmd_addpdesc, which must 20200 * not happen. 20201 * 20202 * EINVAL is a result of failure on boundary 20203 * checks against the pkt_info contents. It 20204 * should not happen, and we panic because 20205 * either there's horrible heap corruption, 20206 * and/or programming mistake. 20207 */ 20208 if (err != ENOMEM) { 20209 cmn_err(CE_PANIC, "tcp_multisend: " 20210 "pdesc logic error detected for " 20211 "tcp %p mmd %p pinfo %p (%d)\n", 20212 (void *)tcp, (void *)mmd, 20213 (void *)pkt_info, err); 20214 } 20215 TCP_STAT(tcp_mdt_addpdescfail); 20216 goto legacy_send; /* out_of_mem */ 20217 } 20218 ASSERT(pkt != NULL); 20219 20220 /* calculate IP header and TCP checksums */ 20221 if (af == AF_INET) { 20222 /* calculate pseudo-header checksum */ 20223 cksum = (dst >> 16) + (dst & 0xFFFF) + 20224 (src >> 16) + (src & 0xFFFF); 20225 20226 /* offset for TCP header checksum */ 20227 up = IPH_TCPH_CHECKSUMP(ipha, 20228 IP_SIMPLE_HDR_LENGTH); 20229 } else { 20230 up = (uint16_t *)&ip6h->ip6_src; 20231 20232 /* calculate pseudo-header checksum */ 20233 cksum = up[0] + up[1] + up[2] + up[3] + 20234 up[4] + up[5] + up[6] + up[7] + 20235 up[8] + up[9] + up[10] + up[11] + 20236 up[12] + up[13] + up[14] + up[15]; 20237 20238 /* Fold the initial sum */ 20239 cksum = (cksum & 0xffff) + (cksum >> 16); 20240 20241 up = (uint16_t *)(((uchar_t *)ip6h) + 20242 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20243 } 20244 20245 if (hwcksum_flags & HCK_FULLCKSUM) { 20246 /* clear checksum field for hardware */ 20247 *up = 0; 20248 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20249 uint32_t sum; 20250 20251 /* pseudo-header checksumming */ 20252 sum = *up + cksum + IP_TCP_CSUM_COMP; 20253 sum = (sum & 0xFFFF) + (sum >> 16); 20254 *up = (sum & 0xFFFF) + (sum >> 16); 20255 } else { 20256 /* software checksumming */ 20257 TCP_STAT(tcp_out_sw_cksum); 20258 TCP_STAT_UPDATE(tcp_out_sw_cksum_bytes, 20259 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20260 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20261 cksum + IP_TCP_CSUM_COMP); 20262 if (*up == 0) 20263 *up = 0xFFFF; 20264 } 20265 20266 /* IPv4 header checksum */ 20267 if (af == AF_INET) { 20268 ipha->ipha_fragment_offset_and_flags |= 20269 (uint32_t)htons(ire->ire_frag_flag); 20270 20271 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20272 ipha->ipha_hdr_checksum = 0; 20273 } else { 20274 IP_HDR_CKSUM(ipha, cksum, 20275 ((uint32_t *)ipha)[0], 20276 ((uint16_t *)ipha)[4]); 20277 } 20278 } 20279 20280 if (af == AF_INET && HOOKS4_INTERESTED_PHYSICAL_OUT|| 20281 af == AF_INET6 && HOOKS6_INTERESTED_PHYSICAL_OUT) { 20282 /* build header(IP/TCP) mblk for this segment */ 20283 if ((mp = dupb(md_hbuf)) == NULL) 20284 goto legacy_send; 20285 20286 mp->b_rptr = pkt_info->hdr_rptr; 20287 mp->b_wptr = pkt_info->hdr_wptr; 20288 20289 /* build payload mblk for this segment */ 20290 if ((mp1 = dupb(*xmit_tail)) == NULL) { 20291 freemsg(mp); 20292 goto legacy_send; 20293 } 20294 mp1->b_wptr = md_pbuf->b_rptr + cur_pld_off; 20295 mp1->b_rptr = mp1->b_wptr - 20296 tcp->tcp_last_sent_len; 20297 linkb(mp, mp1); 20298 20299 pld_start = mp1->b_rptr; 20300 20301 if (af == AF_INET) { 20302 DTRACE_PROBE4( 20303 ip4__physical__out__start, 20304 ill_t *, NULL, 20305 ill_t *, ill, 20306 ipha_t *, ipha, 20307 mblk_t *, mp); 20308 FW_HOOKS(ip4_physical_out_event, 20309 ipv4firewall_physical_out, 20310 NULL, ill, ipha, mp, mp); 20311 DTRACE_PROBE1( 20312 ip4__physical__out__end, 20313 mblk_t *, mp); 20314 } else { 20315 DTRACE_PROBE4( 20316 ip6__physical__out_start, 20317 ill_t *, NULL, 20318 ill_t *, ill, 20319 ip6_t *, ip6h, 20320 mblk_t *, mp); 20321 FW_HOOKS6(ip6_physical_out_event, 20322 ipv6firewall_physical_out, 20323 NULL, ill, ip6h, mp, mp); 20324 DTRACE_PROBE1( 20325 ip6__physical__out__end, 20326 mblk_t *, mp); 20327 } 20328 20329 if (buf_trunked && mp != NULL) { 20330 /* 20331 * Need to pass it to normal path. 20332 */ 20333 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20334 } else if (mp == NULL || 20335 mp->b_rptr != pkt_info->hdr_rptr || 20336 mp->b_wptr != pkt_info->hdr_wptr || 20337 (mp1 = mp->b_cont) == NULL || 20338 mp1->b_rptr != pld_start || 20339 mp1->b_wptr != pld_start + 20340 tcp->tcp_last_sent_len || 20341 mp1->b_cont != NULL) { 20342 /* 20343 * Need to pass all packets of this 20344 * buffer to normal path, either when 20345 * packet is blocked, or when boundary 20346 * of header buffer or payload buffer 20347 * has been changed by FW_HOOKS[6]. 20348 */ 20349 buf_trunked = B_TRUE; 20350 if (md_mp_head != NULL) { 20351 err = (intptr_t)rmvb(md_mp_head, 20352 md_mp); 20353 if (err == 0) 20354 md_mp_head = NULL; 20355 } 20356 20357 /* send down what we've got so far */ 20358 if (md_mp_head != NULL) { 20359 tcp_multisend_data(tcp, ire, 20360 ill, md_mp_head, obsegs, 20361 obbytes, &rconfirm); 20362 } 20363 md_mp_head = NULL; 20364 20365 if (mp != NULL) 20366 CALL_IP_WPUT(tcp->tcp_connp, 20367 q, mp); 20368 20369 mp1 = fw_mp_head; 20370 do { 20371 mp = mp1; 20372 mp1 = mp1->b_next; 20373 mp->b_next = NULL; 20374 mp->b_prev = NULL; 20375 CALL_IP_WPUT(tcp->tcp_connp, 20376 q, mp); 20377 } while (mp1 != NULL); 20378 20379 fw_mp_head = NULL; 20380 } else { 20381 if (fw_mp_head == NULL) 20382 fw_mp_head = mp; 20383 else 20384 fw_mp_head->b_prev->b_next = mp; 20385 fw_mp_head->b_prev = mp; 20386 } 20387 } 20388 20389 /* advance header offset */ 20390 cur_hdr_off += hdr_frag_sz; 20391 20392 obbytes += tcp->tcp_last_sent_len; 20393 ++obsegs; 20394 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20395 *tail_unsent > 0); 20396 20397 if ((*xmit_tail)->b_next == NULL) { 20398 /* 20399 * Store the lbolt used for RTT estimation. We can only 20400 * record one timestamp per mblk so we do it when we 20401 * reach the end of the payload buffer. Also we only 20402 * take a new timestamp sample when the previous timed 20403 * data from the same mblk has been ack'ed. 20404 */ 20405 (*xmit_tail)->b_prev = local_time; 20406 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20407 } 20408 20409 ASSERT(*tail_unsent >= 0); 20410 if (*tail_unsent > 0) { 20411 /* 20412 * We got here because we broke out of the above 20413 * loop due to of one of the following cases: 20414 * 20415 * 1. len < adjusted MSS (i.e. small), 20416 * 2. Sender SWS avoidance, 20417 * 3. max_pld is zero. 20418 * 20419 * We are done for this Multidata, so trim our 20420 * last payload buffer (if any) accordingly. 20421 */ 20422 if (md_pbuf != NULL) 20423 md_pbuf->b_wptr -= *tail_unsent; 20424 } else if (*usable > 0) { 20425 *xmit_tail = (*xmit_tail)->b_cont; 20426 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20427 (uintptr_t)INT_MAX); 20428 *tail_unsent = (int)MBLKL(*xmit_tail); 20429 add_buffer = B_TRUE; 20430 } 20431 20432 while (fw_mp_head) { 20433 mp = fw_mp_head; 20434 fw_mp_head = fw_mp_head->b_next; 20435 mp->b_prev = mp->b_next = NULL; 20436 freemsg(mp); 20437 } 20438 if (buf_trunked) { 20439 TCP_STAT(tcp_mdt_discarded); 20440 freeb(md_mp); 20441 buf_trunked = B_FALSE; 20442 } 20443 } while (!done && *usable > 0 && num_burst_seg > 0 && 20444 (tcp_mdt_chain || max_pld > 0)); 20445 20446 if (md_mp_head != NULL) { 20447 /* send everything down */ 20448 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20449 &rconfirm); 20450 } 20451 20452 #undef PREP_NEW_MULTIDATA 20453 #undef PREP_NEW_PBUF 20454 #undef IPVER 20455 20456 IRE_REFRELE(ire); 20457 return (0); 20458 } 20459 20460 /* 20461 * A wrapper function for sending one or more Multidata messages down to 20462 * the module below ip; this routine does not release the reference of the 20463 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20464 */ 20465 static void 20466 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20467 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20468 { 20469 uint64_t delta; 20470 nce_t *nce; 20471 20472 ASSERT(ire != NULL && ill != NULL); 20473 ASSERT(ire->ire_stq != NULL); 20474 ASSERT(md_mp_head != NULL); 20475 ASSERT(rconfirm != NULL); 20476 20477 /* adjust MIBs and IRE timestamp */ 20478 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 20479 tcp->tcp_obsegs += obsegs; 20480 UPDATE_MIB(&tcp_mib, tcpOutDataSegs, obsegs); 20481 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, obbytes); 20482 TCP_STAT_UPDATE(tcp_mdt_pkt_out, obsegs); 20483 20484 if (tcp->tcp_ipversion == IPV4_VERSION) { 20485 TCP_STAT_UPDATE(tcp_mdt_pkt_out_v4, obsegs); 20486 UPDATE_MIB(&ip_mib, ipOutRequests, obsegs); 20487 } else { 20488 TCP_STAT_UPDATE(tcp_mdt_pkt_out_v6, obsegs); 20489 UPDATE_MIB(&ip6_mib, ipv6OutRequests, obsegs); 20490 } 20491 20492 ire->ire_ob_pkt_count += obsegs; 20493 if (ire->ire_ipif != NULL) 20494 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20495 ire->ire_last_used_time = lbolt; 20496 20497 /* send it down */ 20498 putnext(ire->ire_stq, md_mp_head); 20499 20500 /* we're done for TCP/IPv4 */ 20501 if (tcp->tcp_ipversion == IPV4_VERSION) 20502 return; 20503 20504 nce = ire->ire_nce; 20505 20506 ASSERT(nce != NULL); 20507 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20508 ASSERT(nce->nce_state != ND_INCOMPLETE); 20509 20510 /* reachability confirmation? */ 20511 if (*rconfirm) { 20512 nce->nce_last = TICK_TO_MSEC(lbolt64); 20513 if (nce->nce_state != ND_REACHABLE) { 20514 mutex_enter(&nce->nce_lock); 20515 nce->nce_state = ND_REACHABLE; 20516 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20517 mutex_exit(&nce->nce_lock); 20518 (void) untimeout(nce->nce_timeout_id); 20519 if (ip_debug > 2) { 20520 /* ip1dbg */ 20521 pr_addr_dbg("tcp_multisend_data: state " 20522 "for %s changed to REACHABLE\n", 20523 AF_INET6, &ire->ire_addr_v6); 20524 } 20525 } 20526 /* reset transport reachability confirmation */ 20527 *rconfirm = B_FALSE; 20528 } 20529 20530 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20531 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20532 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20533 20534 if (delta > (uint64_t)ill->ill_reachable_time) { 20535 mutex_enter(&nce->nce_lock); 20536 switch (nce->nce_state) { 20537 case ND_REACHABLE: 20538 case ND_STALE: 20539 /* 20540 * ND_REACHABLE is identical to ND_STALE in this 20541 * specific case. If reachable time has expired for 20542 * this neighbor (delta is greater than reachable 20543 * time), conceptually, the neighbor cache is no 20544 * longer in REACHABLE state, but already in STALE 20545 * state. So the correct transition here is to 20546 * ND_DELAY. 20547 */ 20548 nce->nce_state = ND_DELAY; 20549 mutex_exit(&nce->nce_lock); 20550 NDP_RESTART_TIMER(nce, delay_first_probe_time); 20551 if (ip_debug > 3) { 20552 /* ip2dbg */ 20553 pr_addr_dbg("tcp_multisend_data: state " 20554 "for %s changed to DELAY\n", 20555 AF_INET6, &ire->ire_addr_v6); 20556 } 20557 break; 20558 case ND_DELAY: 20559 case ND_PROBE: 20560 mutex_exit(&nce->nce_lock); 20561 /* Timers have already started */ 20562 break; 20563 case ND_UNREACHABLE: 20564 /* 20565 * ndp timer has detected that this nce is 20566 * unreachable and initiated deleting this nce 20567 * and all its associated IREs. This is a race 20568 * where we found the ire before it was deleted 20569 * and have just sent out a packet using this 20570 * unreachable nce. 20571 */ 20572 mutex_exit(&nce->nce_lock); 20573 break; 20574 default: 20575 ASSERT(0); 20576 } 20577 } 20578 } 20579 20580 /* 20581 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20582 * scheme, and returns one of the following: 20583 * 20584 * -1 = failed allocation. 20585 * 0 = success; burst count reached, or usable send window is too small, 20586 * and that we'd rather wait until later before sending again. 20587 * 1 = success; we are called from tcp_multisend(), and both usable send 20588 * window and tail_unsent are greater than the MDT threshold, and thus 20589 * Multidata Transmit should be used instead. 20590 */ 20591 static int 20592 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20593 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20594 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20595 const int mdt_thres) 20596 { 20597 int num_burst_seg = tcp->tcp_snd_burst; 20598 20599 for (;;) { 20600 struct datab *db; 20601 tcph_t *tcph; 20602 uint32_t sum; 20603 mblk_t *mp, *mp1; 20604 uchar_t *rptr; 20605 int len; 20606 20607 /* 20608 * If we're called by tcp_multisend(), and the amount of 20609 * sendable data as well as the size of current xmit_tail 20610 * is beyond the MDT threshold, return to the caller and 20611 * let the large data transmit be done using MDT. 20612 */ 20613 if (*usable > 0 && *usable > mdt_thres && 20614 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 20615 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 20616 ASSERT(tcp->tcp_mdt); 20617 return (1); /* success; do large send */ 20618 } 20619 20620 if (num_burst_seg-- == 0) 20621 break; /* success; burst count reached */ 20622 20623 len = mss; 20624 if (len > *usable) { 20625 len = *usable; 20626 if (len <= 0) { 20627 /* Terminate the loop */ 20628 break; /* success; too small */ 20629 } 20630 /* 20631 * Sender silly-window avoidance. 20632 * Ignore this if we are going to send a 20633 * zero window probe out. 20634 * 20635 * TODO: force data into microscopic window? 20636 * ==> (!pushed || (unsent > usable)) 20637 */ 20638 if (len < (tcp->tcp_max_swnd >> 1) && 20639 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 20640 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 20641 len == 1) && (! tcp->tcp_zero_win_probe)) { 20642 /* 20643 * If the retransmit timer is not running 20644 * we start it so that we will retransmit 20645 * in the case when the the receiver has 20646 * decremented the window. 20647 */ 20648 if (*snxt == tcp->tcp_snxt && 20649 *snxt == tcp->tcp_suna) { 20650 /* 20651 * We are not supposed to send 20652 * anything. So let's wait a little 20653 * bit longer before breaking SWS 20654 * avoidance. 20655 * 20656 * What should the value be? 20657 * Suggestion: MAX(init rexmit time, 20658 * tcp->tcp_rto) 20659 */ 20660 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20661 } 20662 break; /* success; too small */ 20663 } 20664 } 20665 20666 tcph = tcp->tcp_tcph; 20667 20668 *usable -= len; /* Approximate - can be adjusted later */ 20669 if (*usable > 0) 20670 tcph->th_flags[0] = TH_ACK; 20671 else 20672 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 20673 20674 /* 20675 * Prime pump for IP's checksumming on our behalf 20676 * Include the adjustment for a source route if any. 20677 */ 20678 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20679 sum = (sum >> 16) + (sum & 0xFFFF); 20680 U16_TO_ABE16(sum, tcph->th_sum); 20681 20682 U32_TO_ABE32(*snxt, tcph->th_seq); 20683 20684 /* 20685 * Branch off to tcp_xmit_mp() if any of the VALID bits is 20686 * set. For the case when TCP_FSS_VALID is the only valid 20687 * bit (normal active close), branch off only when we think 20688 * that the FIN flag needs to be set. Note for this case, 20689 * that (snxt + len) may not reflect the actual seg_len, 20690 * as len may be further reduced in tcp_xmit_mp(). If len 20691 * gets modified, we will end up here again. 20692 */ 20693 if (tcp->tcp_valid_bits != 0 && 20694 (tcp->tcp_valid_bits != TCP_FSS_VALID || 20695 ((*snxt + len) == tcp->tcp_fss))) { 20696 uchar_t *prev_rptr; 20697 uint32_t prev_snxt = tcp->tcp_snxt; 20698 20699 if (*tail_unsent == 0) { 20700 ASSERT((*xmit_tail)->b_cont != NULL); 20701 *xmit_tail = (*xmit_tail)->b_cont; 20702 prev_rptr = (*xmit_tail)->b_rptr; 20703 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20704 (*xmit_tail)->b_rptr); 20705 } else { 20706 prev_rptr = (*xmit_tail)->b_rptr; 20707 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 20708 *tail_unsent; 20709 } 20710 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 20711 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20712 /* Restore tcp_snxt so we get amount sent right. */ 20713 tcp->tcp_snxt = prev_snxt; 20714 if (prev_rptr == (*xmit_tail)->b_rptr) { 20715 /* 20716 * If the previous timestamp is still in use, 20717 * don't stomp on it. 20718 */ 20719 if ((*xmit_tail)->b_next == NULL) { 20720 (*xmit_tail)->b_prev = local_time; 20721 (*xmit_tail)->b_next = 20722 (mblk_t *)(uintptr_t)(*snxt); 20723 } 20724 } else 20725 (*xmit_tail)->b_rptr = prev_rptr; 20726 20727 if (mp == NULL) 20728 return (-1); 20729 mp1 = mp->b_cont; 20730 20731 tcp->tcp_last_sent_len = (ushort_t)len; 20732 while (mp1->b_cont) { 20733 *xmit_tail = (*xmit_tail)->b_cont; 20734 (*xmit_tail)->b_prev = local_time; 20735 (*xmit_tail)->b_next = 20736 (mblk_t *)(uintptr_t)(*snxt); 20737 mp1 = mp1->b_cont; 20738 } 20739 *snxt += len; 20740 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20741 BUMP_LOCAL(tcp->tcp_obsegs); 20742 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 20743 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 20744 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 20745 tcp_send_data(tcp, q, mp); 20746 continue; 20747 } 20748 20749 *snxt += len; /* Adjust later if we don't send all of len */ 20750 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 20751 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 20752 20753 if (*tail_unsent) { 20754 /* Are the bytes above us in flight? */ 20755 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20756 if (rptr != (*xmit_tail)->b_rptr) { 20757 *tail_unsent -= len; 20758 tcp->tcp_last_sent_len = (ushort_t)len; 20759 len += tcp_hdr_len; 20760 if (tcp->tcp_ipversion == IPV4_VERSION) 20761 tcp->tcp_ipha->ipha_length = htons(len); 20762 else 20763 tcp->tcp_ip6h->ip6_plen = 20764 htons(len - 20765 ((char *)&tcp->tcp_ip6h[1] - 20766 tcp->tcp_iphc)); 20767 mp = dupb(*xmit_tail); 20768 if (!mp) 20769 return (-1); /* out_of_mem */ 20770 mp->b_rptr = rptr; 20771 /* 20772 * If the old timestamp is no longer in use, 20773 * sample a new timestamp now. 20774 */ 20775 if ((*xmit_tail)->b_next == NULL) { 20776 (*xmit_tail)->b_prev = local_time; 20777 (*xmit_tail)->b_next = 20778 (mblk_t *)(uintptr_t)(*snxt-len); 20779 } 20780 goto must_alloc; 20781 } 20782 } else { 20783 *xmit_tail = (*xmit_tail)->b_cont; 20784 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 20785 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 20786 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20787 (*xmit_tail)->b_rptr); 20788 } 20789 20790 (*xmit_tail)->b_prev = local_time; 20791 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 20792 20793 *tail_unsent -= len; 20794 tcp->tcp_last_sent_len = (ushort_t)len; 20795 20796 len += tcp_hdr_len; 20797 if (tcp->tcp_ipversion == IPV4_VERSION) 20798 tcp->tcp_ipha->ipha_length = htons(len); 20799 else 20800 tcp->tcp_ip6h->ip6_plen = htons(len - 20801 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20802 20803 mp = dupb(*xmit_tail); 20804 if (!mp) 20805 return (-1); /* out_of_mem */ 20806 20807 len = tcp_hdr_len; 20808 /* 20809 * There are four reasons to allocate a new hdr mblk: 20810 * 1) The bytes above us are in use by another packet 20811 * 2) We don't have good alignment 20812 * 3) The mblk is being shared 20813 * 4) We don't have enough room for a header 20814 */ 20815 rptr = mp->b_rptr - len; 20816 if (!OK_32PTR(rptr) || 20817 ((db = mp->b_datap), db->db_ref != 2) || 20818 rptr < db->db_base) { 20819 /* NOTE: we assume allocb returns an OK_32PTR */ 20820 20821 must_alloc:; 20822 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 20823 tcp_wroff_xtra, BPRI_MED); 20824 if (!mp1) { 20825 freemsg(mp); 20826 return (-1); /* out_of_mem */ 20827 } 20828 mp1->b_cont = mp; 20829 mp = mp1; 20830 /* Leave room for Link Level header */ 20831 len = tcp_hdr_len; 20832 rptr = &mp->b_rptr[tcp_wroff_xtra]; 20833 mp->b_wptr = &rptr[len]; 20834 } 20835 20836 /* 20837 * Fill in the header using the template header, and add 20838 * options such as time-stamp, ECN and/or SACK, as needed. 20839 */ 20840 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 20841 20842 mp->b_rptr = rptr; 20843 20844 if (*tail_unsent) { 20845 int spill = *tail_unsent; 20846 20847 mp1 = mp->b_cont; 20848 if (!mp1) 20849 mp1 = mp; 20850 20851 /* 20852 * If we're a little short, tack on more mblks until 20853 * there is no more spillover. 20854 */ 20855 while (spill < 0) { 20856 mblk_t *nmp; 20857 int nmpsz; 20858 20859 nmp = (*xmit_tail)->b_cont; 20860 nmpsz = MBLKL(nmp); 20861 20862 /* 20863 * Excess data in mblk; can we split it? 20864 * If MDT is enabled for the connection, 20865 * keep on splitting as this is a transient 20866 * send path. 20867 */ 20868 if (!tcp->tcp_mdt && (spill + nmpsz > 0)) { 20869 /* 20870 * Don't split if stream head was 20871 * told to break up larger writes 20872 * into smaller ones. 20873 */ 20874 if (tcp->tcp_maxpsz > 0) 20875 break; 20876 20877 /* 20878 * Next mblk is less than SMSS/2 20879 * rounded up to nearest 64-byte; 20880 * let it get sent as part of the 20881 * next segment. 20882 */ 20883 if (tcp->tcp_localnet && 20884 !tcp->tcp_cork && 20885 (nmpsz < roundup((mss >> 1), 64))) 20886 break; 20887 } 20888 20889 *xmit_tail = nmp; 20890 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 20891 /* Stash for rtt use later */ 20892 (*xmit_tail)->b_prev = local_time; 20893 (*xmit_tail)->b_next = 20894 (mblk_t *)(uintptr_t)(*snxt - len); 20895 mp1->b_cont = dupb(*xmit_tail); 20896 mp1 = mp1->b_cont; 20897 20898 spill += nmpsz; 20899 if (mp1 == NULL) { 20900 *tail_unsent = spill; 20901 freemsg(mp); 20902 return (-1); /* out_of_mem */ 20903 } 20904 } 20905 20906 /* Trim back any surplus on the last mblk */ 20907 if (spill >= 0) { 20908 mp1->b_wptr -= spill; 20909 *tail_unsent = spill; 20910 } else { 20911 /* 20912 * We did not send everything we could in 20913 * order to remain within the b_cont limit. 20914 */ 20915 *usable -= spill; 20916 *snxt += spill; 20917 tcp->tcp_last_sent_len += spill; 20918 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, spill); 20919 /* 20920 * Adjust the checksum 20921 */ 20922 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 20923 sum += spill; 20924 sum = (sum >> 16) + (sum & 0xFFFF); 20925 U16_TO_ABE16(sum, tcph->th_sum); 20926 if (tcp->tcp_ipversion == IPV4_VERSION) { 20927 sum = ntohs( 20928 ((ipha_t *)rptr)->ipha_length) + 20929 spill; 20930 ((ipha_t *)rptr)->ipha_length = 20931 htons(sum); 20932 } else { 20933 sum = ntohs( 20934 ((ip6_t *)rptr)->ip6_plen) + 20935 spill; 20936 ((ip6_t *)rptr)->ip6_plen = 20937 htons(sum); 20938 } 20939 *tail_unsent = 0; 20940 } 20941 } 20942 if (tcp->tcp_ip_forward_progress) { 20943 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20944 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 20945 tcp->tcp_ip_forward_progress = B_FALSE; 20946 } 20947 20948 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 20949 tcp_send_data(tcp, q, mp); 20950 BUMP_LOCAL(tcp->tcp_obsegs); 20951 } 20952 20953 return (0); 20954 } 20955 20956 /* Unlink and return any mblk that looks like it contains a MDT info */ 20957 static mblk_t * 20958 tcp_mdt_info_mp(mblk_t *mp) 20959 { 20960 mblk_t *prev_mp; 20961 20962 for (;;) { 20963 prev_mp = mp; 20964 /* no more to process? */ 20965 if ((mp = mp->b_cont) == NULL) 20966 break; 20967 20968 switch (DB_TYPE(mp)) { 20969 case M_CTL: 20970 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 20971 continue; 20972 ASSERT(prev_mp != NULL); 20973 prev_mp->b_cont = mp->b_cont; 20974 mp->b_cont = NULL; 20975 return (mp); 20976 default: 20977 break; 20978 } 20979 } 20980 return (mp); 20981 } 20982 20983 /* MDT info update routine, called when IP notifies us about MDT */ 20984 static void 20985 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 20986 { 20987 boolean_t prev_state; 20988 20989 /* 20990 * IP is telling us to abort MDT on this connection? We know 20991 * this because the capability is only turned off when IP 20992 * encounters some pathological cases, e.g. link-layer change 20993 * where the new driver doesn't support MDT, or in situation 20994 * where MDT usage on the link-layer has been switched off. 20995 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 20996 * if the link-layer doesn't support MDT, and if it does, it 20997 * will indicate that the feature is to be turned on. 20998 */ 20999 prev_state = tcp->tcp_mdt; 21000 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21001 if (!tcp->tcp_mdt && !first) { 21002 TCP_STAT(tcp_mdt_conn_halted3); 21003 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21004 (void *)tcp->tcp_connp)); 21005 } 21006 21007 /* 21008 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21009 * so disable MDT otherwise. The checks are done here 21010 * and in tcp_wput_data(). 21011 */ 21012 if (tcp->tcp_mdt && 21013 (tcp->tcp_ipversion == IPV4_VERSION && 21014 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21015 (tcp->tcp_ipversion == IPV6_VERSION && 21016 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21017 tcp->tcp_mdt = B_FALSE; 21018 21019 if (tcp->tcp_mdt) { 21020 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21021 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21022 "version (%d), expected version is %d", 21023 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21024 tcp->tcp_mdt = B_FALSE; 21025 return; 21026 } 21027 21028 /* 21029 * We need the driver to be able to handle at least three 21030 * spans per packet in order for tcp MDT to be utilized. 21031 * The first is for the header portion, while the rest are 21032 * needed to handle a packet that straddles across two 21033 * virtually non-contiguous buffers; a typical tcp packet 21034 * therefore consists of only two spans. Note that we take 21035 * a zero as "don't care". 21036 */ 21037 if (mdt_capab->ill_mdt_span_limit > 0 && 21038 mdt_capab->ill_mdt_span_limit < 3) { 21039 tcp->tcp_mdt = B_FALSE; 21040 return; 21041 } 21042 21043 /* a zero means driver wants default value */ 21044 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21045 tcp_mdt_max_pbufs); 21046 if (tcp->tcp_mdt_max_pld == 0) 21047 tcp->tcp_mdt_max_pld = tcp_mdt_max_pbufs; 21048 21049 /* ensure 32-bit alignment */ 21050 tcp->tcp_mdt_hdr_head = roundup(MAX(tcp_mdt_hdr_head_min, 21051 mdt_capab->ill_mdt_hdr_head), 4); 21052 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcp_mdt_hdr_tail_min, 21053 mdt_capab->ill_mdt_hdr_tail), 4); 21054 21055 if (!first && !prev_state) { 21056 TCP_STAT(tcp_mdt_conn_resumed2); 21057 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21058 (void *)tcp->tcp_connp)); 21059 } 21060 } 21061 } 21062 21063 static void 21064 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_mdt) 21065 { 21066 conn_t *connp = tcp->tcp_connp; 21067 21068 ASSERT(ire != NULL); 21069 21070 /* 21071 * We may be in the fastpath here, and although we essentially do 21072 * similar checks as in ip_bind_connected{_v6}/ip_mdinfo_return, 21073 * we try to keep things as brief as possible. After all, these 21074 * are only best-effort checks, and we do more thorough ones prior 21075 * to calling tcp_multisend(). 21076 */ 21077 if (ip_multidata_outbound && check_mdt && 21078 !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21079 ill != NULL && ILL_MDT_CAPABLE(ill) && 21080 !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21081 !(ire->ire_flags & RTF_MULTIRT) && 21082 !IPP_ENABLED(IPP_LOCAL_OUT) && 21083 CONN_IS_MD_FASTPATH(connp)) { 21084 /* Remember the result */ 21085 connp->conn_mdt_ok = B_TRUE; 21086 21087 ASSERT(ill->ill_mdt_capab != NULL); 21088 if (!ill->ill_mdt_capab->ill_mdt_on) { 21089 /* 21090 * If MDT has been previously turned off in the past, 21091 * and we currently can do MDT (due to IPQoS policy 21092 * removal, etc.) then enable it for this interface. 21093 */ 21094 ill->ill_mdt_capab->ill_mdt_on = 1; 21095 ip1dbg(("tcp_ire_ill_check: connp %p enables MDT for " 21096 "interface %s\n", (void *)connp, ill->ill_name)); 21097 } 21098 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21099 } 21100 21101 /* 21102 * The goal is to reduce the number of generated tcp segments by 21103 * setting the maxpsz multiplier to 0; this will have an affect on 21104 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21105 * into each packet, up to SMSS bytes. Doing this reduces the number 21106 * of outbound segments and incoming ACKs, thus allowing for better 21107 * network and system performance. In contrast the legacy behavior 21108 * may result in sending less than SMSS size, because the last mblk 21109 * for some packets may have more data than needed to make up SMSS, 21110 * and the legacy code refused to "split" it. 21111 * 21112 * We apply the new behavior on following situations: 21113 * 21114 * 1) Loopback connections, 21115 * 2) Connections in which the remote peer is not on local subnet, 21116 * 3) Local subnet connections over the bge interface (see below). 21117 * 21118 * Ideally, we would like this behavior to apply for interfaces other 21119 * than bge. However, doing so would negatively impact drivers which 21120 * perform dynamic mapping and unmapping of DMA resources, which are 21121 * increased by setting the maxpsz multiplier to 0 (more mblks per 21122 * packet will be generated by tcp). The bge driver does not suffer 21123 * from this, as it copies the mblks into pre-mapped buffers, and 21124 * therefore does not require more I/O resources than before. 21125 * 21126 * Otherwise, this behavior is present on all network interfaces when 21127 * the destination endpoint is non-local, since reducing the number 21128 * of packets in general is good for the network. 21129 * 21130 * TODO We need to remove this hard-coded conditional for bge once 21131 * a better "self-tuning" mechanism, or a way to comprehend 21132 * the driver transmit strategy is devised. Until the solution 21133 * is found and well understood, we live with this hack. 21134 */ 21135 if (!tcp_static_maxpsz && 21136 (tcp->tcp_loopback || !tcp->tcp_localnet || 21137 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21138 /* override the default value */ 21139 tcp->tcp_maxpsz = 0; 21140 21141 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21142 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21143 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21144 } 21145 21146 /* set the stream head parameters accordingly */ 21147 (void) tcp_maxpsz_set(tcp, B_TRUE); 21148 } 21149 21150 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21151 static void 21152 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21153 { 21154 uchar_t fval = *mp->b_rptr; 21155 mblk_t *tail; 21156 queue_t *q = tcp->tcp_wq; 21157 21158 /* TODO: How should flush interact with urgent data? */ 21159 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21160 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21161 /* 21162 * Flush only data that has not yet been put on the wire. If 21163 * we flush data that we have already transmitted, life, as we 21164 * know it, may come to an end. 21165 */ 21166 tail = tcp->tcp_xmit_tail; 21167 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21168 tcp->tcp_xmit_tail_unsent = 0; 21169 tcp->tcp_unsent = 0; 21170 if (tail->b_wptr != tail->b_rptr) 21171 tail = tail->b_cont; 21172 if (tail) { 21173 mblk_t **excess = &tcp->tcp_xmit_head; 21174 for (;;) { 21175 mblk_t *mp1 = *excess; 21176 if (mp1 == tail) 21177 break; 21178 tcp->tcp_xmit_tail = mp1; 21179 tcp->tcp_xmit_last = mp1; 21180 excess = &mp1->b_cont; 21181 } 21182 *excess = NULL; 21183 tcp_close_mpp(&tail); 21184 if (tcp->tcp_snd_zcopy_aware) 21185 tcp_zcopy_notify(tcp); 21186 } 21187 /* 21188 * We have no unsent data, so unsent must be less than 21189 * tcp_xmit_lowater, so re-enable flow. 21190 */ 21191 if (tcp->tcp_flow_stopped) { 21192 tcp_clrqfull(tcp); 21193 } 21194 } 21195 /* 21196 * TODO: you can't just flush these, you have to increase rwnd for one 21197 * thing. For another, how should urgent data interact? 21198 */ 21199 if (fval & FLUSHR) { 21200 *mp->b_rptr = fval & ~FLUSHW; 21201 /* XXX */ 21202 qreply(q, mp); 21203 return; 21204 } 21205 freemsg(mp); 21206 } 21207 21208 /* 21209 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21210 * messages. 21211 */ 21212 static void 21213 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21214 { 21215 mblk_t *mp1; 21216 STRUCT_HANDLE(strbuf, sb); 21217 uint16_t port; 21218 queue_t *q = tcp->tcp_wq; 21219 in6_addr_t v6addr; 21220 ipaddr_t v4addr; 21221 uint32_t flowinfo = 0; 21222 int addrlen; 21223 21224 /* Make sure it is one of ours. */ 21225 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21226 case TI_GETMYNAME: 21227 case TI_GETPEERNAME: 21228 break; 21229 default: 21230 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21231 return; 21232 } 21233 switch (mi_copy_state(q, mp, &mp1)) { 21234 case -1: 21235 return; 21236 case MI_COPY_CASE(MI_COPY_IN, 1): 21237 break; 21238 case MI_COPY_CASE(MI_COPY_OUT, 1): 21239 /* Copy out the strbuf. */ 21240 mi_copyout(q, mp); 21241 return; 21242 case MI_COPY_CASE(MI_COPY_OUT, 2): 21243 /* All done. */ 21244 mi_copy_done(q, mp, 0); 21245 return; 21246 default: 21247 mi_copy_done(q, mp, EPROTO); 21248 return; 21249 } 21250 /* Check alignment of the strbuf */ 21251 if (!OK_32PTR(mp1->b_rptr)) { 21252 mi_copy_done(q, mp, EINVAL); 21253 return; 21254 } 21255 21256 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 21257 (void *)mp1->b_rptr); 21258 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21259 21260 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21261 mi_copy_done(q, mp, EINVAL); 21262 return; 21263 } 21264 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 21265 case TI_GETMYNAME: 21266 if (tcp->tcp_family == AF_INET) { 21267 if (tcp->tcp_ipversion == IPV4_VERSION) { 21268 v4addr = tcp->tcp_ipha->ipha_src; 21269 } else { 21270 /* can't return an address in this case */ 21271 v4addr = 0; 21272 } 21273 } else { 21274 /* tcp->tcp_family == AF_INET6 */ 21275 if (tcp->tcp_ipversion == IPV4_VERSION) { 21276 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 21277 &v6addr); 21278 } else { 21279 v6addr = tcp->tcp_ip6h->ip6_src; 21280 } 21281 } 21282 port = tcp->tcp_lport; 21283 break; 21284 case TI_GETPEERNAME: 21285 if (tcp->tcp_family == AF_INET) { 21286 if (tcp->tcp_ipversion == IPV4_VERSION) { 21287 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 21288 v4addr); 21289 } else { 21290 /* can't return an address in this case */ 21291 v4addr = 0; 21292 } 21293 } else { 21294 /* tcp->tcp_family == AF_INET6) */ 21295 v6addr = tcp->tcp_remote_v6; 21296 if (tcp->tcp_ipversion == IPV6_VERSION) { 21297 /* 21298 * No flowinfo if tcp->tcp_ipversion is v4. 21299 * 21300 * flowinfo was already initialized to zero 21301 * where it was declared above, so only 21302 * set it if ipversion is v6. 21303 */ 21304 flowinfo = tcp->tcp_ip6h->ip6_vcf & 21305 ~IPV6_VERS_AND_FLOW_MASK; 21306 } 21307 } 21308 port = tcp->tcp_fport; 21309 break; 21310 default: 21311 mi_copy_done(q, mp, EPROTO); 21312 return; 21313 } 21314 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21315 if (!mp1) 21316 return; 21317 21318 if (tcp->tcp_family == AF_INET) { 21319 sin_t *sin; 21320 21321 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 21322 sin = (sin_t *)mp1->b_rptr; 21323 mp1->b_wptr = (uchar_t *)&sin[1]; 21324 *sin = sin_null; 21325 sin->sin_family = AF_INET; 21326 sin->sin_addr.s_addr = v4addr; 21327 sin->sin_port = port; 21328 } else { 21329 /* tcp->tcp_family == AF_INET6 */ 21330 sin6_t *sin6; 21331 21332 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 21333 sin6 = (sin6_t *)mp1->b_rptr; 21334 mp1->b_wptr = (uchar_t *)&sin6[1]; 21335 *sin6 = sin6_null; 21336 sin6->sin6_family = AF_INET6; 21337 sin6->sin6_flowinfo = flowinfo; 21338 sin6->sin6_addr = v6addr; 21339 sin6->sin6_port = port; 21340 } 21341 /* Copy out the address */ 21342 mi_copyout(q, mp); 21343 } 21344 21345 /* 21346 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21347 * messages. 21348 */ 21349 /* ARGSUSED */ 21350 static void 21351 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21352 { 21353 conn_t *connp = (conn_t *)arg; 21354 tcp_t *tcp = connp->conn_tcp; 21355 queue_t *q = tcp->tcp_wq; 21356 struct iocblk *iocp; 21357 21358 ASSERT(DB_TYPE(mp) == M_IOCTL); 21359 /* 21360 * Try and ASSERT the minimum possible references on the 21361 * conn early enough. Since we are executing on write side, 21362 * the connection is obviously not detached and that means 21363 * there is a ref each for TCP and IP. Since we are behind 21364 * the squeue, the minimum references needed are 3. If the 21365 * conn is in classifier hash list, there should be an 21366 * extra ref for that (we check both the possibilities). 21367 */ 21368 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21369 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21370 21371 iocp = (struct iocblk *)mp->b_rptr; 21372 switch (iocp->ioc_cmd) { 21373 case TCP_IOC_DEFAULT_Q: 21374 /* Wants to be the default wq. */ 21375 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 21376 iocp->ioc_error = EPERM; 21377 iocp->ioc_count = 0; 21378 mp->b_datap->db_type = M_IOCACK; 21379 qreply(q, mp); 21380 return; 21381 } 21382 tcp_def_q_set(tcp, mp); 21383 return; 21384 case _SIOCSOCKFALLBACK: 21385 /* 21386 * Either sockmod is about to be popped and the socket 21387 * would now be treated as a plain stream, or a module 21388 * is about to be pushed so we could no longer use read- 21389 * side synchronous streams for fused loopback tcp. 21390 * Drain any queued data and disable direct sockfs 21391 * interface from now on. 21392 */ 21393 if (!tcp->tcp_issocket) { 21394 DB_TYPE(mp) = M_IOCNAK; 21395 iocp->ioc_error = EINVAL; 21396 } else { 21397 #ifdef _ILP32 21398 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 21399 #else 21400 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21401 #endif 21402 /* 21403 * Insert this socket into the acceptor hash. 21404 * We might need it for T_CONN_RES message 21405 */ 21406 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21407 21408 if (tcp->tcp_fused) { 21409 /* 21410 * This is a fused loopback tcp; disable 21411 * read-side synchronous streams interface 21412 * and drain any queued data. It is okay 21413 * to do this for non-synchronous streams 21414 * fused tcp as well. 21415 */ 21416 tcp_fuse_disable_pair(tcp, B_FALSE); 21417 } 21418 tcp->tcp_issocket = B_FALSE; 21419 TCP_STAT(tcp_sock_fallback); 21420 21421 DB_TYPE(mp) = M_IOCACK; 21422 iocp->ioc_error = 0; 21423 } 21424 iocp->ioc_count = 0; 21425 iocp->ioc_rval = 0; 21426 qreply(q, mp); 21427 return; 21428 } 21429 CALL_IP_WPUT(connp, q, mp); 21430 } 21431 21432 /* 21433 * This routine is called by tcp_wput() to handle all TPI requests. 21434 */ 21435 /* ARGSUSED */ 21436 static void 21437 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21438 { 21439 conn_t *connp = (conn_t *)arg; 21440 tcp_t *tcp = connp->conn_tcp; 21441 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21442 uchar_t *rptr; 21443 t_scalar_t type; 21444 int len; 21445 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 21446 21447 /* 21448 * Try and ASSERT the minimum possible references on the 21449 * conn early enough. Since we are executing on write side, 21450 * the connection is obviously not detached and that means 21451 * there is a ref each for TCP and IP. Since we are behind 21452 * the squeue, the minimum references needed are 3. If the 21453 * conn is in classifier hash list, there should be an 21454 * extra ref for that (we check both the possibilities). 21455 */ 21456 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21457 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21458 21459 rptr = mp->b_rptr; 21460 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21461 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 21462 type = ((union T_primitives *)rptr)->type; 21463 if (type == T_EXDATA_REQ) { 21464 uint32_t msize = msgdsize(mp->b_cont); 21465 21466 len = msize - 1; 21467 if (len < 0) { 21468 freemsg(mp); 21469 return; 21470 } 21471 /* 21472 * Try to force urgent data out on the wire. 21473 * Even if we have unsent data this will 21474 * at least send the urgent flag. 21475 * XXX does not handle more flag correctly. 21476 */ 21477 len += tcp->tcp_unsent; 21478 len += tcp->tcp_snxt; 21479 tcp->tcp_urg = len; 21480 tcp->tcp_valid_bits |= TCP_URG_VALID; 21481 21482 /* Bypass tcp protocol for fused tcp loopback */ 21483 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 21484 return; 21485 } else if (type != T_DATA_REQ) { 21486 goto non_urgent_data; 21487 } 21488 /* TODO: options, flags, ... from user */ 21489 /* Set length to zero for reclamation below */ 21490 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 21491 freeb(mp); 21492 return; 21493 } else { 21494 if (tcp->tcp_debug) { 21495 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21496 "tcp_wput_proto, dropping one..."); 21497 } 21498 freemsg(mp); 21499 return; 21500 } 21501 21502 non_urgent_data: 21503 21504 switch ((int)tprim->type) { 21505 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 21506 /* 21507 * save the kssl_ent_t from the next block, and convert this 21508 * back to a normal bind_req. 21509 */ 21510 if (mp->b_cont != NULL) { 21511 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 21512 21513 if (tcp->tcp_kssl_ent != NULL) { 21514 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 21515 KSSL_NO_PROXY); 21516 tcp->tcp_kssl_ent = NULL; 21517 } 21518 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 21519 sizeof (kssl_ent_t)); 21520 kssl_hold_ent(tcp->tcp_kssl_ent); 21521 freemsg(mp->b_cont); 21522 mp->b_cont = NULL; 21523 } 21524 tprim->type = T_BIND_REQ; 21525 21526 /* FALLTHROUGH */ 21527 case O_T_BIND_REQ: /* bind request */ 21528 case T_BIND_REQ: /* new semantics bind request */ 21529 tcp_bind(tcp, mp); 21530 break; 21531 case T_UNBIND_REQ: /* unbind request */ 21532 tcp_unbind(tcp, mp); 21533 break; 21534 case O_T_CONN_RES: /* old connection response XXX */ 21535 case T_CONN_RES: /* connection response */ 21536 tcp_accept(tcp, mp); 21537 break; 21538 case T_CONN_REQ: /* connection request */ 21539 tcp_connect(tcp, mp); 21540 break; 21541 case T_DISCON_REQ: /* disconnect request */ 21542 tcp_disconnect(tcp, mp); 21543 break; 21544 case T_CAPABILITY_REQ: 21545 tcp_capability_req(tcp, mp); /* capability request */ 21546 break; 21547 case T_INFO_REQ: /* information request */ 21548 tcp_info_req(tcp, mp); 21549 break; 21550 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 21551 /* Only IP is allowed to return meaningful value */ 21552 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 21553 break; 21554 case T_OPTMGMT_REQ: 21555 /* 21556 * Note: no support for snmpcom_req() through new 21557 * T_OPTMGMT_REQ. See comments in ip.c 21558 */ 21559 /* Only IP is allowed to return meaningful value */ 21560 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 21561 break; 21562 21563 case T_UNITDATA_REQ: /* unitdata request */ 21564 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21565 break; 21566 case T_ORDREL_REQ: /* orderly release req */ 21567 freemsg(mp); 21568 21569 if (tcp->tcp_fused) 21570 tcp_unfuse(tcp); 21571 21572 if (tcp_xmit_end(tcp) != 0) { 21573 /* 21574 * We were crossing FINs and got a reset from 21575 * the other side. Just ignore it. 21576 */ 21577 if (tcp->tcp_debug) { 21578 (void) strlog(TCP_MOD_ID, 0, 1, 21579 SL_ERROR|SL_TRACE, 21580 "tcp_wput_proto, T_ORDREL_REQ out of " 21581 "state %s", 21582 tcp_display(tcp, NULL, 21583 DISP_ADDR_AND_PORT)); 21584 } 21585 } 21586 break; 21587 case T_ADDR_REQ: 21588 tcp_addr_req(tcp, mp); 21589 break; 21590 default: 21591 if (tcp->tcp_debug) { 21592 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21593 "tcp_wput_proto, bogus TPI msg, type %d", 21594 tprim->type); 21595 } 21596 /* 21597 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 21598 * to recover. 21599 */ 21600 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21601 break; 21602 } 21603 } 21604 21605 /* 21606 * The TCP write service routine should never be called... 21607 */ 21608 /* ARGSUSED */ 21609 static void 21610 tcp_wsrv(queue_t *q) 21611 { 21612 TCP_STAT(tcp_wsrv_called); 21613 } 21614 21615 /* Non overlapping byte exchanger */ 21616 static void 21617 tcp_xchg(uchar_t *a, uchar_t *b, int len) 21618 { 21619 uchar_t uch; 21620 21621 while (len-- > 0) { 21622 uch = a[len]; 21623 a[len] = b[len]; 21624 b[len] = uch; 21625 } 21626 } 21627 21628 /* 21629 * Send out a control packet on the tcp connection specified. This routine 21630 * is typically called where we need a simple ACK or RST generated. 21631 */ 21632 static void 21633 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 21634 { 21635 uchar_t *rptr; 21636 tcph_t *tcph; 21637 ipha_t *ipha = NULL; 21638 ip6_t *ip6h = NULL; 21639 uint32_t sum; 21640 int tcp_hdr_len; 21641 int tcp_ip_hdr_len; 21642 mblk_t *mp; 21643 21644 /* 21645 * Save sum for use in source route later. 21646 */ 21647 ASSERT(tcp != NULL); 21648 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 21649 tcp_hdr_len = tcp->tcp_hdr_len; 21650 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 21651 21652 /* If a text string is passed in with the request, pass it to strlog. */ 21653 if (str != NULL && tcp->tcp_debug) { 21654 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21655 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 21656 str, seq, ack, ctl); 21657 } 21658 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 21659 BPRI_MED); 21660 if (mp == NULL) { 21661 return; 21662 } 21663 rptr = &mp->b_rptr[tcp_wroff_xtra]; 21664 mp->b_rptr = rptr; 21665 mp->b_wptr = &rptr[tcp_hdr_len]; 21666 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 21667 21668 if (tcp->tcp_ipversion == IPV4_VERSION) { 21669 ipha = (ipha_t *)rptr; 21670 ipha->ipha_length = htons(tcp_hdr_len); 21671 } else { 21672 ip6h = (ip6_t *)rptr; 21673 ASSERT(tcp != NULL); 21674 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 21675 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21676 } 21677 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 21678 tcph->th_flags[0] = (uint8_t)ctl; 21679 if (ctl & TH_RST) { 21680 BUMP_MIB(&tcp_mib, tcpOutRsts); 21681 BUMP_MIB(&tcp_mib, tcpOutControl); 21682 /* 21683 * Don't send TSopt w/ TH_RST packets per RFC 1323. 21684 */ 21685 if (tcp->tcp_snd_ts_ok && 21686 tcp->tcp_state > TCPS_SYN_SENT) { 21687 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 21688 *(mp->b_wptr) = TCPOPT_EOL; 21689 if (tcp->tcp_ipversion == IPV4_VERSION) { 21690 ipha->ipha_length = htons(tcp_hdr_len - 21691 TCPOPT_REAL_TS_LEN); 21692 } else { 21693 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 21694 TCPOPT_REAL_TS_LEN); 21695 } 21696 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 21697 sum -= TCPOPT_REAL_TS_LEN; 21698 } 21699 } 21700 if (ctl & TH_ACK) { 21701 if (tcp->tcp_snd_ts_ok) { 21702 U32_TO_BE32(lbolt, 21703 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 21704 U32_TO_BE32(tcp->tcp_ts_recent, 21705 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 21706 } 21707 21708 /* Update the latest receive window size in TCP header. */ 21709 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21710 tcph->th_win); 21711 tcp->tcp_rack = ack; 21712 tcp->tcp_rack_cnt = 0; 21713 BUMP_MIB(&tcp_mib, tcpOutAck); 21714 } 21715 BUMP_LOCAL(tcp->tcp_obsegs); 21716 U32_TO_BE32(seq, tcph->th_seq); 21717 U32_TO_BE32(ack, tcph->th_ack); 21718 /* 21719 * Include the adjustment for a source route if any. 21720 */ 21721 sum = (sum >> 16) + (sum & 0xFFFF); 21722 U16_TO_BE16(sum, tcph->th_sum); 21723 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21724 tcp_send_data(tcp, tcp->tcp_wq, mp); 21725 } 21726 21727 /* 21728 * If this routine returns B_TRUE, TCP can generate a RST in response 21729 * to a segment. If it returns B_FALSE, TCP should not respond. 21730 */ 21731 static boolean_t 21732 tcp_send_rst_chk(void) 21733 { 21734 clock_t now; 21735 21736 /* 21737 * TCP needs to protect itself from generating too many RSTs. 21738 * This can be a DoS attack by sending us random segments 21739 * soliciting RSTs. 21740 * 21741 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 21742 * in each 1 second interval. In this way, TCP still generate 21743 * RSTs in normal cases but when under attack, the impact is 21744 * limited. 21745 */ 21746 if (tcp_rst_sent_rate_enabled != 0) { 21747 now = lbolt; 21748 /* lbolt can wrap around. */ 21749 if ((tcp_last_rst_intrvl > now) || 21750 (TICK_TO_MSEC(now - tcp_last_rst_intrvl) > 1*SECONDS)) { 21751 tcp_last_rst_intrvl = now; 21752 tcp_rst_cnt = 1; 21753 } else if (++tcp_rst_cnt > tcp_rst_sent_rate) { 21754 return (B_FALSE); 21755 } 21756 } 21757 return (B_TRUE); 21758 } 21759 21760 /* 21761 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 21762 */ 21763 static void 21764 tcp_ip_ire_mark_advice(tcp_t *tcp) 21765 { 21766 mblk_t *mp; 21767 ipic_t *ipic; 21768 21769 if (tcp->tcp_ipversion == IPV4_VERSION) { 21770 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21771 &ipic); 21772 } else { 21773 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21774 &ipic); 21775 } 21776 if (mp == NULL) 21777 return; 21778 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21779 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21780 } 21781 21782 /* 21783 * Return an IP advice ioctl mblk and set ipic to be the pointer 21784 * to the advice structure. 21785 */ 21786 static mblk_t * 21787 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 21788 { 21789 struct iocblk *ioc; 21790 mblk_t *mp, *mp1; 21791 21792 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 21793 if (mp == NULL) 21794 return (NULL); 21795 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 21796 *ipic = (ipic_t *)mp->b_rptr; 21797 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 21798 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 21799 21800 bcopy(addr, *ipic + 1, addr_len); 21801 21802 (*ipic)->ipic_addr_length = addr_len; 21803 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 21804 21805 mp1 = mkiocb(IP_IOCTL); 21806 if (mp1 == NULL) { 21807 freemsg(mp); 21808 return (NULL); 21809 } 21810 mp1->b_cont = mp; 21811 ioc = (struct iocblk *)mp1->b_rptr; 21812 ioc->ioc_count = sizeof (ipic_t) + addr_len; 21813 21814 return (mp1); 21815 } 21816 21817 /* 21818 * Generate a reset based on an inbound packet for which there is no active 21819 * tcp state that we can find. 21820 * 21821 * IPSEC NOTE : Try to send the reply with the same protection as it came 21822 * in. We still have the ipsec_mp that the packet was attached to. Thus 21823 * the packet will go out at the same level of protection as it came in by 21824 * converting the IPSEC_IN to IPSEC_OUT. 21825 */ 21826 static void 21827 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 21828 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid) 21829 { 21830 ipha_t *ipha = NULL; 21831 ip6_t *ip6h = NULL; 21832 ushort_t len; 21833 tcph_t *tcph; 21834 int i; 21835 mblk_t *ipsec_mp; 21836 boolean_t mctl_present; 21837 ipic_t *ipic; 21838 ipaddr_t v4addr; 21839 in6_addr_t v6addr; 21840 int addr_len; 21841 void *addr; 21842 queue_t *q = tcp_g_q; 21843 tcp_t *tcp = Q_TO_TCP(q); 21844 cred_t *cr; 21845 mblk_t *nmp; 21846 21847 if (!tcp_send_rst_chk()) { 21848 tcp_rst_unsent++; 21849 freemsg(mp); 21850 return; 21851 } 21852 21853 if (mp->b_datap->db_type == M_CTL) { 21854 ipsec_mp = mp; 21855 mp = mp->b_cont; 21856 mctl_present = B_TRUE; 21857 } else { 21858 ipsec_mp = mp; 21859 mctl_present = B_FALSE; 21860 } 21861 21862 if (str && q && tcp_dbg) { 21863 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21864 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 21865 "flags 0x%x", 21866 str, seq, ack, ctl); 21867 } 21868 if (mp->b_datap->db_ref != 1) { 21869 mblk_t *mp1 = copyb(mp); 21870 freemsg(mp); 21871 mp = mp1; 21872 if (!mp) { 21873 if (mctl_present) 21874 freeb(ipsec_mp); 21875 return; 21876 } else { 21877 if (mctl_present) { 21878 ipsec_mp->b_cont = mp; 21879 } else { 21880 ipsec_mp = mp; 21881 } 21882 } 21883 } else if (mp->b_cont) { 21884 freemsg(mp->b_cont); 21885 mp->b_cont = NULL; 21886 } 21887 /* 21888 * We skip reversing source route here. 21889 * (for now we replace all IP options with EOL) 21890 */ 21891 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21892 ipha = (ipha_t *)mp->b_rptr; 21893 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 21894 mp->b_rptr[i] = IPOPT_EOL; 21895 /* 21896 * Make sure that src address isn't flagrantly invalid. 21897 * Not all broadcast address checking for the src address 21898 * is possible, since we don't know the netmask of the src 21899 * addr. No check for destination address is done, since 21900 * IP will not pass up a packet with a broadcast dest 21901 * address to TCP. Similar checks are done below for IPv6. 21902 */ 21903 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 21904 CLASSD(ipha->ipha_src)) { 21905 freemsg(ipsec_mp); 21906 BUMP_MIB(&ip_mib, ipInDiscards); 21907 return; 21908 } 21909 } else { 21910 ip6h = (ip6_t *)mp->b_rptr; 21911 21912 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 21913 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 21914 freemsg(ipsec_mp); 21915 BUMP_MIB(&ip6_mib, ipv6InDiscards); 21916 return; 21917 } 21918 21919 /* Remove any extension headers assuming partial overlay */ 21920 if (ip_hdr_len > IPV6_HDR_LEN) { 21921 uint8_t *to; 21922 21923 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 21924 ovbcopy(ip6h, to, IPV6_HDR_LEN); 21925 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 21926 ip_hdr_len = IPV6_HDR_LEN; 21927 ip6h = (ip6_t *)mp->b_rptr; 21928 ip6h->ip6_nxt = IPPROTO_TCP; 21929 } 21930 } 21931 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 21932 if (tcph->th_flags[0] & TH_RST) { 21933 freemsg(ipsec_mp); 21934 return; 21935 } 21936 tcph->th_offset_and_rsrvd[0] = (5 << 4); 21937 len = ip_hdr_len + sizeof (tcph_t); 21938 mp->b_wptr = &mp->b_rptr[len]; 21939 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21940 ipha->ipha_length = htons(len); 21941 /* Swap addresses */ 21942 v4addr = ipha->ipha_src; 21943 ipha->ipha_src = ipha->ipha_dst; 21944 ipha->ipha_dst = v4addr; 21945 ipha->ipha_ident = 0; 21946 ipha->ipha_ttl = (uchar_t)tcp_ipv4_ttl; 21947 addr_len = IP_ADDR_LEN; 21948 addr = &v4addr; 21949 } else { 21950 /* No ip6i_t in this case */ 21951 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 21952 /* Swap addresses */ 21953 v6addr = ip6h->ip6_src; 21954 ip6h->ip6_src = ip6h->ip6_dst; 21955 ip6h->ip6_dst = v6addr; 21956 ip6h->ip6_hops = (uchar_t)tcp_ipv6_hoplimit; 21957 addr_len = IPV6_ADDR_LEN; 21958 addr = &v6addr; 21959 } 21960 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 21961 U32_TO_BE32(ack, tcph->th_ack); 21962 U32_TO_BE32(seq, tcph->th_seq); 21963 U16_TO_BE16(0, tcph->th_win); 21964 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 21965 tcph->th_flags[0] = (uint8_t)ctl; 21966 if (ctl & TH_RST) { 21967 BUMP_MIB(&tcp_mib, tcpOutRsts); 21968 BUMP_MIB(&tcp_mib, tcpOutControl); 21969 } 21970 21971 /* IP trusts us to set up labels when required. */ 21972 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 21973 crgetlabel(cr) != NULL) { 21974 int err, adjust; 21975 21976 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 21977 err = tsol_check_label(cr, &mp, &adjust, 21978 tcp->tcp_connp->conn_mac_exempt); 21979 else 21980 err = tsol_check_label_v6(cr, &mp, &adjust, 21981 tcp->tcp_connp->conn_mac_exempt); 21982 if (mctl_present) 21983 ipsec_mp->b_cont = mp; 21984 else 21985 ipsec_mp = mp; 21986 if (err != 0) { 21987 freemsg(ipsec_mp); 21988 return; 21989 } 21990 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21991 ipha = (ipha_t *)mp->b_rptr; 21992 adjust += ntohs(ipha->ipha_length); 21993 ipha->ipha_length = htons(adjust); 21994 } else { 21995 ip6h = (ip6_t *)mp->b_rptr; 21996 } 21997 } 21998 21999 if (mctl_present) { 22000 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22001 22002 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22003 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22004 return; 22005 } 22006 } 22007 if (zoneid == ALL_ZONES) 22008 zoneid = GLOBAL_ZONEID; 22009 22010 /* Add the zoneid so ip_output routes it properly */ 22011 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid)) == NULL) { 22012 freemsg(ipsec_mp); 22013 return; 22014 } 22015 ipsec_mp = nmp; 22016 22017 /* 22018 * NOTE: one might consider tracing a TCP packet here, but 22019 * this function has no active TCP state and no tcp structure 22020 * that has a trace buffer. If we traced here, we would have 22021 * to keep a local trace buffer in tcp_record_trace(). 22022 * 22023 * TSol note: The mblk that contains the incoming packet was 22024 * reused by tcp_xmit_listener_reset, so it already contains 22025 * the right credentials and we don't need to call mblk_setcred. 22026 * Also the conn's cred is not right since it is associated 22027 * with tcp_g_q. 22028 */ 22029 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22030 22031 /* 22032 * Tell IP to mark the IRE used for this destination temporary. 22033 * This way, we can limit our exposure to DoS attack because IP 22034 * creates an IRE for each destination. If there are too many, 22035 * the time to do any routing lookup will be extremely long. And 22036 * the lookup can be in interrupt context. 22037 * 22038 * Note that in normal circumstances, this marking should not 22039 * affect anything. It would be nice if only 1 message is 22040 * needed to inform IP that the IRE created for this RST should 22041 * not be added to the cache table. But there is currently 22042 * not such communication mechanism between TCP and IP. So 22043 * the best we can do now is to send the advice ioctl to IP 22044 * to mark the IRE temporary. 22045 */ 22046 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22047 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22048 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22049 } 22050 } 22051 22052 /* 22053 * Initiate closedown sequence on an active connection. (May be called as 22054 * writer.) Return value zero for OK return, non-zero for error return. 22055 */ 22056 static int 22057 tcp_xmit_end(tcp_t *tcp) 22058 { 22059 ipic_t *ipic; 22060 mblk_t *mp; 22061 22062 if (tcp->tcp_state < TCPS_SYN_RCVD || 22063 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22064 /* 22065 * Invalid state, only states TCPS_SYN_RCVD, 22066 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22067 */ 22068 return (-1); 22069 } 22070 22071 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22072 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22073 /* 22074 * If there is nothing more unsent, send the FIN now. 22075 * Otherwise, it will go out with the last segment. 22076 */ 22077 if (tcp->tcp_unsent == 0) { 22078 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22079 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22080 22081 if (mp) { 22082 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22083 tcp_send_data(tcp, tcp->tcp_wq, mp); 22084 } else { 22085 /* 22086 * Couldn't allocate msg. Pretend we got it out. 22087 * Wait for rexmit timeout. 22088 */ 22089 tcp->tcp_snxt = tcp->tcp_fss + 1; 22090 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22091 } 22092 22093 /* 22094 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22095 * changed. 22096 */ 22097 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22098 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22099 } 22100 } else { 22101 /* 22102 * If tcp->tcp_cork is set, then the data will not get sent, 22103 * so we have to check that and unset it first. 22104 */ 22105 if (tcp->tcp_cork) 22106 tcp->tcp_cork = B_FALSE; 22107 tcp_wput_data(tcp, NULL, B_FALSE); 22108 } 22109 22110 /* 22111 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22112 * is 0, don't update the cache. 22113 */ 22114 if (tcp_rtt_updates == 0 || tcp->tcp_rtt_update < tcp_rtt_updates) 22115 return (0); 22116 22117 /* 22118 * NOTE: should not update if source routes i.e. if tcp_remote if 22119 * different from the destination. 22120 */ 22121 if (tcp->tcp_ipversion == IPV4_VERSION) { 22122 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22123 return (0); 22124 } 22125 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22126 &ipic); 22127 } else { 22128 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22129 &tcp->tcp_ip6h->ip6_dst))) { 22130 return (0); 22131 } 22132 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22133 &ipic); 22134 } 22135 22136 /* Record route attributes in the IRE for use by future connections. */ 22137 if (mp == NULL) 22138 return (0); 22139 22140 /* 22141 * We do not have a good algorithm to update ssthresh at this time. 22142 * So don't do any update. 22143 */ 22144 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22145 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22146 22147 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22148 return (0); 22149 } 22150 22151 /* 22152 * Generate a "no listener here" RST in response to an "unknown" segment. 22153 * Note that we are reusing the incoming mp to construct the outgoing 22154 * RST. 22155 */ 22156 void 22157 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid) 22158 { 22159 uchar_t *rptr; 22160 uint32_t seg_len; 22161 tcph_t *tcph; 22162 uint32_t seg_seq; 22163 uint32_t seg_ack; 22164 uint_t flags; 22165 mblk_t *ipsec_mp; 22166 ipha_t *ipha; 22167 ip6_t *ip6h; 22168 boolean_t mctl_present = B_FALSE; 22169 boolean_t check = B_TRUE; 22170 boolean_t policy_present; 22171 22172 TCP_STAT(tcp_no_listener); 22173 22174 ipsec_mp = mp; 22175 22176 if (mp->b_datap->db_type == M_CTL) { 22177 ipsec_in_t *ii; 22178 22179 mctl_present = B_TRUE; 22180 mp = mp->b_cont; 22181 22182 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22183 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22184 if (ii->ipsec_in_dont_check) { 22185 check = B_FALSE; 22186 if (!ii->ipsec_in_secure) { 22187 freeb(ipsec_mp); 22188 mctl_present = B_FALSE; 22189 ipsec_mp = mp; 22190 } 22191 } 22192 } 22193 22194 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22195 policy_present = ipsec_inbound_v4_policy_present; 22196 ipha = (ipha_t *)mp->b_rptr; 22197 ip6h = NULL; 22198 } else { 22199 policy_present = ipsec_inbound_v6_policy_present; 22200 ipha = NULL; 22201 ip6h = (ip6_t *)mp->b_rptr; 22202 } 22203 22204 if (check && policy_present) { 22205 /* 22206 * The conn_t parameter is NULL because we already know 22207 * nobody's home. 22208 */ 22209 ipsec_mp = ipsec_check_global_policy( 22210 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present); 22211 if (ipsec_mp == NULL) 22212 return; 22213 } 22214 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22215 DTRACE_PROBE2( 22216 tx__ip__log__error__nolistener__tcp, 22217 char *, "Could not reply with RST to mp(1)", 22218 mblk_t *, mp); 22219 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22220 freemsg(ipsec_mp); 22221 return; 22222 } 22223 22224 rptr = mp->b_rptr; 22225 22226 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22227 seg_seq = BE32_TO_U32(tcph->th_seq); 22228 seg_ack = BE32_TO_U32(tcph->th_ack); 22229 flags = tcph->th_flags[0]; 22230 22231 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22232 if (flags & TH_RST) { 22233 freemsg(ipsec_mp); 22234 } else if (flags & TH_ACK) { 22235 tcp_xmit_early_reset("no tcp, reset", 22236 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid); 22237 } else { 22238 if (flags & TH_SYN) { 22239 seg_len++; 22240 } else { 22241 /* 22242 * Here we violate the RFC. Note that a normal 22243 * TCP will never send a segment without the ACK 22244 * flag, except for RST or SYN segment. This 22245 * segment is neither. Just drop it on the 22246 * floor. 22247 */ 22248 freemsg(ipsec_mp); 22249 tcp_rst_unsent++; 22250 return; 22251 } 22252 22253 tcp_xmit_early_reset("no tcp, reset/ack", 22254 ipsec_mp, 0, seg_seq + seg_len, 22255 TH_RST | TH_ACK, ip_hdr_len, zoneid); 22256 } 22257 } 22258 22259 /* 22260 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22261 * ip and tcp header ready to pass down to IP. If the mp passed in is 22262 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22263 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22264 * otherwise it will dup partial mblks.) 22265 * Otherwise, an appropriate ACK packet will be generated. This 22266 * routine is not usually called to send new data for the first time. It 22267 * is mostly called out of the timer for retransmits, and to generate ACKs. 22268 * 22269 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22270 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22271 * of the original mblk chain will be returned in *offset and *end_mp. 22272 */ 22273 mblk_t * 22274 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22275 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22276 boolean_t rexmit) 22277 { 22278 int data_length; 22279 int32_t off = 0; 22280 uint_t flags; 22281 mblk_t *mp1; 22282 mblk_t *mp2; 22283 uchar_t *rptr; 22284 tcph_t *tcph; 22285 int32_t num_sack_blk = 0; 22286 int32_t sack_opt_len = 0; 22287 22288 /* Allocate for our maximum TCP header + link-level */ 22289 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 22290 BPRI_MED); 22291 if (!mp1) 22292 return (NULL); 22293 data_length = 0; 22294 22295 /* 22296 * Note that tcp_mss has been adjusted to take into account the 22297 * timestamp option if applicable. Because SACK options do not 22298 * appear in every TCP segments and they are of variable lengths, 22299 * they cannot be included in tcp_mss. Thus we need to calculate 22300 * the actual segment length when we need to send a segment which 22301 * includes SACK options. 22302 */ 22303 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22304 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22305 tcp->tcp_num_sack_blk); 22306 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22307 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22308 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22309 max_to_send -= sack_opt_len; 22310 } 22311 22312 if (offset != NULL) { 22313 off = *offset; 22314 /* We use offset as an indicator that end_mp is not NULL. */ 22315 *end_mp = NULL; 22316 } 22317 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22318 /* This could be faster with cooperation from downstream */ 22319 if (mp2 != mp1 && !sendall && 22320 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22321 max_to_send) 22322 /* 22323 * Don't send the next mblk since the whole mblk 22324 * does not fit. 22325 */ 22326 break; 22327 mp2->b_cont = dupb(mp); 22328 mp2 = mp2->b_cont; 22329 if (!mp2) { 22330 freemsg(mp1); 22331 return (NULL); 22332 } 22333 mp2->b_rptr += off; 22334 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22335 (uintptr_t)INT_MAX); 22336 22337 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22338 if (data_length > max_to_send) { 22339 mp2->b_wptr -= data_length - max_to_send; 22340 data_length = max_to_send; 22341 off = mp2->b_wptr - mp->b_rptr; 22342 break; 22343 } else { 22344 off = 0; 22345 } 22346 } 22347 if (offset != NULL) { 22348 *offset = off; 22349 *end_mp = mp; 22350 } 22351 if (seg_len != NULL) { 22352 *seg_len = data_length; 22353 } 22354 22355 /* Update the latest receive window size in TCP header. */ 22356 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22357 tcp->tcp_tcph->th_win); 22358 22359 rptr = mp1->b_rptr + tcp_wroff_xtra; 22360 mp1->b_rptr = rptr; 22361 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22362 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22363 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22364 U32_TO_ABE32(seq, tcph->th_seq); 22365 22366 /* 22367 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22368 * that this function was called from tcp_wput_data. Thus, when called 22369 * to retransmit data the setting of the PUSH bit may appear some 22370 * what random in that it might get set when it should not. This 22371 * should not pose any performance issues. 22372 */ 22373 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22374 tcp->tcp_unsent == data_length)) { 22375 flags = TH_ACK | TH_PUSH; 22376 } else { 22377 flags = TH_ACK; 22378 } 22379 22380 if (tcp->tcp_ecn_ok) { 22381 if (tcp->tcp_ecn_echo_on) 22382 flags |= TH_ECE; 22383 22384 /* 22385 * Only set ECT bit and ECN_CWR if a segment contains new data. 22386 * There is no TCP flow control for non-data segments, and 22387 * only data segment is transmitted reliably. 22388 */ 22389 if (data_length > 0 && !rexmit) { 22390 SET_ECT(tcp, rptr); 22391 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 22392 flags |= TH_CWR; 22393 tcp->tcp_ecn_cwr_sent = B_TRUE; 22394 } 22395 } 22396 } 22397 22398 if (tcp->tcp_valid_bits) { 22399 uint32_t u1; 22400 22401 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 22402 seq == tcp->tcp_iss) { 22403 uchar_t *wptr; 22404 22405 /* 22406 * If TCP_ISS_VALID and the seq number is tcp_iss, 22407 * TCP can only be in SYN-SENT, SYN-RCVD or 22408 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 22409 * our SYN is not ack'ed but the app closes this 22410 * TCP connection. 22411 */ 22412 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 22413 tcp->tcp_state == TCPS_SYN_RCVD || 22414 tcp->tcp_state == TCPS_FIN_WAIT_1); 22415 22416 /* 22417 * Tack on the MSS option. It is always needed 22418 * for both active and passive open. 22419 * 22420 * MSS option value should be interface MTU - MIN 22421 * TCP/IP header according to RFC 793 as it means 22422 * the maximum segment size TCP can receive. But 22423 * to get around some broken middle boxes/end hosts 22424 * out there, we allow the option value to be the 22425 * same as the MSS option size on the peer side. 22426 * In this way, the other side will not send 22427 * anything larger than they can receive. 22428 * 22429 * Note that for SYN_SENT state, the ndd param 22430 * tcp_use_smss_as_mss_opt has no effect as we 22431 * don't know the peer's MSS option value. So 22432 * the only case we need to take care of is in 22433 * SYN_RCVD state, which is done later. 22434 */ 22435 wptr = mp1->b_wptr; 22436 wptr[0] = TCPOPT_MAXSEG; 22437 wptr[1] = TCPOPT_MAXSEG_LEN; 22438 wptr += 2; 22439 u1 = tcp->tcp_if_mtu - 22440 (tcp->tcp_ipversion == IPV4_VERSION ? 22441 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 22442 TCP_MIN_HEADER_LENGTH; 22443 U16_TO_BE16(u1, wptr); 22444 mp1->b_wptr = wptr + 2; 22445 /* Update the offset to cover the additional word */ 22446 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22447 22448 /* 22449 * Note that the following way of filling in 22450 * TCP options are not optimal. Some NOPs can 22451 * be saved. But there is no need at this time 22452 * to optimize it. When it is needed, we will 22453 * do it. 22454 */ 22455 switch (tcp->tcp_state) { 22456 case TCPS_SYN_SENT: 22457 flags = TH_SYN; 22458 22459 if (tcp->tcp_snd_ts_ok) { 22460 uint32_t llbolt = (uint32_t)lbolt; 22461 22462 wptr = mp1->b_wptr; 22463 wptr[0] = TCPOPT_NOP; 22464 wptr[1] = TCPOPT_NOP; 22465 wptr[2] = TCPOPT_TSTAMP; 22466 wptr[3] = TCPOPT_TSTAMP_LEN; 22467 wptr += 4; 22468 U32_TO_BE32(llbolt, wptr); 22469 wptr += 4; 22470 ASSERT(tcp->tcp_ts_recent == 0); 22471 U32_TO_BE32(0L, wptr); 22472 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 22473 tcph->th_offset_and_rsrvd[0] += 22474 (3 << 4); 22475 } 22476 22477 /* 22478 * Set up all the bits to tell other side 22479 * we are ECN capable. 22480 */ 22481 if (tcp->tcp_ecn_ok) { 22482 flags |= (TH_ECE | TH_CWR); 22483 } 22484 break; 22485 case TCPS_SYN_RCVD: 22486 flags |= TH_SYN; 22487 22488 /* 22489 * Reset the MSS option value to be SMSS 22490 * We should probably add back the bytes 22491 * for timestamp option and IPsec. We 22492 * don't do that as this is a workaround 22493 * for broken middle boxes/end hosts, it 22494 * is better for us to be more cautious. 22495 * They may not take these things into 22496 * account in their SMSS calculation. Thus 22497 * the peer's calculated SMSS may be smaller 22498 * than what it can be. This should be OK. 22499 */ 22500 if (tcp_use_smss_as_mss_opt) { 22501 u1 = tcp->tcp_mss; 22502 U16_TO_BE16(u1, wptr); 22503 } 22504 22505 /* 22506 * If the other side is ECN capable, reply 22507 * that we are also ECN capable. 22508 */ 22509 if (tcp->tcp_ecn_ok) 22510 flags |= TH_ECE; 22511 break; 22512 default: 22513 /* 22514 * The above ASSERT() makes sure that this 22515 * must be FIN-WAIT-1 state. Our SYN has 22516 * not been ack'ed so retransmit it. 22517 */ 22518 flags |= TH_SYN; 22519 break; 22520 } 22521 22522 if (tcp->tcp_snd_ws_ok) { 22523 wptr = mp1->b_wptr; 22524 wptr[0] = TCPOPT_NOP; 22525 wptr[1] = TCPOPT_WSCALE; 22526 wptr[2] = TCPOPT_WS_LEN; 22527 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 22528 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 22529 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22530 } 22531 22532 if (tcp->tcp_snd_sack_ok) { 22533 wptr = mp1->b_wptr; 22534 wptr[0] = TCPOPT_NOP; 22535 wptr[1] = TCPOPT_NOP; 22536 wptr[2] = TCPOPT_SACK_PERMITTED; 22537 wptr[3] = TCPOPT_SACK_OK_LEN; 22538 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 22539 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22540 } 22541 22542 /* allocb() of adequate mblk assures space */ 22543 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 22544 (uintptr_t)INT_MAX); 22545 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 22546 /* 22547 * Get IP set to checksum on our behalf 22548 * Include the adjustment for a source route if any. 22549 */ 22550 u1 += tcp->tcp_sum; 22551 u1 = (u1 >> 16) + (u1 & 0xFFFF); 22552 U16_TO_BE16(u1, tcph->th_sum); 22553 BUMP_MIB(&tcp_mib, tcpOutControl); 22554 } 22555 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 22556 (seq + data_length) == tcp->tcp_fss) { 22557 if (!tcp->tcp_fin_acked) { 22558 flags |= TH_FIN; 22559 BUMP_MIB(&tcp_mib, tcpOutControl); 22560 } 22561 if (!tcp->tcp_fin_sent) { 22562 tcp->tcp_fin_sent = B_TRUE; 22563 switch (tcp->tcp_state) { 22564 case TCPS_SYN_RCVD: 22565 case TCPS_ESTABLISHED: 22566 tcp->tcp_state = TCPS_FIN_WAIT_1; 22567 break; 22568 case TCPS_CLOSE_WAIT: 22569 tcp->tcp_state = TCPS_LAST_ACK; 22570 break; 22571 } 22572 if (tcp->tcp_suna == tcp->tcp_snxt) 22573 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22574 tcp->tcp_snxt = tcp->tcp_fss + 1; 22575 } 22576 } 22577 /* 22578 * Note the trick here. u1 is unsigned. When tcp_urg 22579 * is smaller than seq, u1 will become a very huge value. 22580 * So the comparison will fail. Also note that tcp_urp 22581 * should be positive, see RFC 793 page 17. 22582 */ 22583 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 22584 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 22585 u1 < (uint32_t)(64 * 1024)) { 22586 flags |= TH_URG; 22587 BUMP_MIB(&tcp_mib, tcpOutUrg); 22588 U32_TO_ABE16(u1, tcph->th_urp); 22589 } 22590 } 22591 tcph->th_flags[0] = (uchar_t)flags; 22592 tcp->tcp_rack = tcp->tcp_rnxt; 22593 tcp->tcp_rack_cnt = 0; 22594 22595 if (tcp->tcp_snd_ts_ok) { 22596 if (tcp->tcp_state != TCPS_SYN_SENT) { 22597 uint32_t llbolt = (uint32_t)lbolt; 22598 22599 U32_TO_BE32(llbolt, 22600 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22601 U32_TO_BE32(tcp->tcp_ts_recent, 22602 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22603 } 22604 } 22605 22606 if (num_sack_blk > 0) { 22607 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22608 sack_blk_t *tmp; 22609 int32_t i; 22610 22611 wptr[0] = TCPOPT_NOP; 22612 wptr[1] = TCPOPT_NOP; 22613 wptr[2] = TCPOPT_SACK; 22614 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22615 sizeof (sack_blk_t); 22616 wptr += TCPOPT_REAL_SACK_LEN; 22617 22618 tmp = tcp->tcp_sack_list; 22619 for (i = 0; i < num_sack_blk; i++) { 22620 U32_TO_BE32(tmp[i].begin, wptr); 22621 wptr += sizeof (tcp_seq); 22622 U32_TO_BE32(tmp[i].end, wptr); 22623 wptr += sizeof (tcp_seq); 22624 } 22625 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 22626 } 22627 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22628 data_length += (int)(mp1->b_wptr - rptr); 22629 if (tcp->tcp_ipversion == IPV4_VERSION) { 22630 ((ipha_t *)rptr)->ipha_length = htons(data_length); 22631 } else { 22632 ip6_t *ip6 = (ip6_t *)(rptr + 22633 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22634 sizeof (ip6i_t) : 0)); 22635 22636 ip6->ip6_plen = htons(data_length - 22637 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22638 } 22639 22640 /* 22641 * Prime pump for IP 22642 * Include the adjustment for a source route if any. 22643 */ 22644 data_length -= tcp->tcp_ip_hdr_len; 22645 data_length += tcp->tcp_sum; 22646 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22647 U16_TO_ABE16(data_length, tcph->th_sum); 22648 if (tcp->tcp_ip_forward_progress) { 22649 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22650 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22651 tcp->tcp_ip_forward_progress = B_FALSE; 22652 } 22653 return (mp1); 22654 } 22655 22656 /* This function handles the push timeout. */ 22657 void 22658 tcp_push_timer(void *arg) 22659 { 22660 conn_t *connp = (conn_t *)arg; 22661 tcp_t *tcp = connp->conn_tcp; 22662 22663 TCP_DBGSTAT(tcp_push_timer_cnt); 22664 22665 ASSERT(tcp->tcp_listener == NULL); 22666 22667 /* 22668 * We need to plug synchronous streams during our drain to prevent 22669 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 22670 */ 22671 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 22672 tcp->tcp_push_tid = 0; 22673 if ((tcp->tcp_rcv_list != NULL) && 22674 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 22675 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 22676 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 22677 } 22678 22679 /* 22680 * This function handles delayed ACK timeout. 22681 */ 22682 static void 22683 tcp_ack_timer(void *arg) 22684 { 22685 conn_t *connp = (conn_t *)arg; 22686 tcp_t *tcp = connp->conn_tcp; 22687 mblk_t *mp; 22688 22689 TCP_DBGSTAT(tcp_ack_timer_cnt); 22690 22691 tcp->tcp_ack_tid = 0; 22692 22693 if (tcp->tcp_fused) 22694 return; 22695 22696 /* 22697 * Do not send ACK if there is no outstanding unack'ed data. 22698 */ 22699 if (tcp->tcp_rnxt == tcp->tcp_rack) { 22700 return; 22701 } 22702 22703 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 22704 /* 22705 * Make sure we don't allow deferred ACKs to result in 22706 * timer-based ACKing. If we have held off an ACK 22707 * when there was more than an mss here, and the timer 22708 * goes off, we have to worry about the possibility 22709 * that the sender isn't doing slow-start, or is out 22710 * of step with us for some other reason. We fall 22711 * permanently back in the direction of 22712 * ACK-every-other-packet as suggested in RFC 1122. 22713 */ 22714 if (tcp->tcp_rack_abs_max > 2) 22715 tcp->tcp_rack_abs_max--; 22716 tcp->tcp_rack_cur_max = 2; 22717 } 22718 mp = tcp_ack_mp(tcp); 22719 22720 if (mp != NULL) { 22721 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22722 BUMP_LOCAL(tcp->tcp_obsegs); 22723 BUMP_MIB(&tcp_mib, tcpOutAck); 22724 BUMP_MIB(&tcp_mib, tcpOutAckDelayed); 22725 tcp_send_data(tcp, tcp->tcp_wq, mp); 22726 } 22727 } 22728 22729 22730 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 22731 static mblk_t * 22732 tcp_ack_mp(tcp_t *tcp) 22733 { 22734 uint32_t seq_no; 22735 22736 /* 22737 * There are a few cases to be considered while setting the sequence no. 22738 * Essentially, we can come here while processing an unacceptable pkt 22739 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 22740 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 22741 * If we are here for a zero window probe, stick with suna. In all 22742 * other cases, we check if suna + swnd encompasses snxt and set 22743 * the sequence number to snxt, if so. If snxt falls outside the 22744 * window (the receiver probably shrunk its window), we will go with 22745 * suna + swnd, otherwise the sequence no will be unacceptable to the 22746 * receiver. 22747 */ 22748 if (tcp->tcp_zero_win_probe) { 22749 seq_no = tcp->tcp_suna; 22750 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 22751 ASSERT(tcp->tcp_swnd == 0); 22752 seq_no = tcp->tcp_snxt; 22753 } else { 22754 seq_no = SEQ_GT(tcp->tcp_snxt, 22755 (tcp->tcp_suna + tcp->tcp_swnd)) ? 22756 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 22757 } 22758 22759 if (tcp->tcp_valid_bits) { 22760 /* 22761 * For the complex case where we have to send some 22762 * controls (FIN or SYN), let tcp_xmit_mp do it. 22763 */ 22764 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 22765 NULL, B_FALSE)); 22766 } else { 22767 /* Generate a simple ACK */ 22768 int data_length; 22769 uchar_t *rptr; 22770 tcph_t *tcph; 22771 mblk_t *mp1; 22772 int32_t tcp_hdr_len; 22773 int32_t tcp_tcp_hdr_len; 22774 int32_t num_sack_blk = 0; 22775 int32_t sack_opt_len; 22776 22777 /* 22778 * Allocate space for TCP + IP headers 22779 * and link-level header 22780 */ 22781 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22782 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22783 tcp->tcp_num_sack_blk); 22784 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22785 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22786 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 22787 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 22788 } else { 22789 tcp_hdr_len = tcp->tcp_hdr_len; 22790 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 22791 } 22792 mp1 = allocb(tcp_hdr_len + tcp_wroff_xtra, BPRI_MED); 22793 if (!mp1) 22794 return (NULL); 22795 22796 /* Update the latest receive window size in TCP header. */ 22797 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22798 tcp->tcp_tcph->th_win); 22799 /* copy in prototype TCP + IP header */ 22800 rptr = mp1->b_rptr + tcp_wroff_xtra; 22801 mp1->b_rptr = rptr; 22802 mp1->b_wptr = rptr + tcp_hdr_len; 22803 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22804 22805 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22806 22807 /* Set the TCP sequence number. */ 22808 U32_TO_ABE32(seq_no, tcph->th_seq); 22809 22810 /* Set up the TCP flag field. */ 22811 tcph->th_flags[0] = (uchar_t)TH_ACK; 22812 if (tcp->tcp_ecn_echo_on) 22813 tcph->th_flags[0] |= TH_ECE; 22814 22815 tcp->tcp_rack = tcp->tcp_rnxt; 22816 tcp->tcp_rack_cnt = 0; 22817 22818 /* fill in timestamp option if in use */ 22819 if (tcp->tcp_snd_ts_ok) { 22820 uint32_t llbolt = (uint32_t)lbolt; 22821 22822 U32_TO_BE32(llbolt, 22823 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22824 U32_TO_BE32(tcp->tcp_ts_recent, 22825 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22826 } 22827 22828 /* Fill in SACK options */ 22829 if (num_sack_blk > 0) { 22830 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22831 sack_blk_t *tmp; 22832 int32_t i; 22833 22834 wptr[0] = TCPOPT_NOP; 22835 wptr[1] = TCPOPT_NOP; 22836 wptr[2] = TCPOPT_SACK; 22837 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22838 sizeof (sack_blk_t); 22839 wptr += TCPOPT_REAL_SACK_LEN; 22840 22841 tmp = tcp->tcp_sack_list; 22842 for (i = 0; i < num_sack_blk; i++) { 22843 U32_TO_BE32(tmp[i].begin, wptr); 22844 wptr += sizeof (tcp_seq); 22845 U32_TO_BE32(tmp[i].end, wptr); 22846 wptr += sizeof (tcp_seq); 22847 } 22848 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 22849 << 4); 22850 } 22851 22852 if (tcp->tcp_ipversion == IPV4_VERSION) { 22853 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 22854 } else { 22855 /* Check for ip6i_t header in sticky hdrs */ 22856 ip6_t *ip6 = (ip6_t *)(rptr + 22857 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22858 sizeof (ip6i_t) : 0)); 22859 22860 ip6->ip6_plen = htons(tcp_hdr_len - 22861 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22862 } 22863 22864 /* 22865 * Prime pump for checksum calculation in IP. Include the 22866 * adjustment for a source route if any. 22867 */ 22868 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 22869 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22870 U16_TO_ABE16(data_length, tcph->th_sum); 22871 22872 if (tcp->tcp_ip_forward_progress) { 22873 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22874 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22875 tcp->tcp_ip_forward_progress = B_FALSE; 22876 } 22877 return (mp1); 22878 } 22879 } 22880 22881 /* 22882 * To create a temporary tcp structure for inserting into bind hash list. 22883 * The parameter is assumed to be in network byte order, ready for use. 22884 */ 22885 /* ARGSUSED */ 22886 static tcp_t * 22887 tcp_alloc_temp_tcp(in_port_t port) 22888 { 22889 conn_t *connp; 22890 tcp_t *tcp; 22891 22892 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP); 22893 if (connp == NULL) 22894 return (NULL); 22895 22896 tcp = connp->conn_tcp; 22897 22898 /* 22899 * Only initialize the necessary info in those structures. Note 22900 * that since INADDR_ANY is all 0, we do not need to set 22901 * tcp_bound_source to INADDR_ANY here. 22902 */ 22903 tcp->tcp_state = TCPS_BOUND; 22904 tcp->tcp_lport = port; 22905 tcp->tcp_exclbind = 1; 22906 tcp->tcp_reserved_port = 1; 22907 22908 /* Just for place holding... */ 22909 tcp->tcp_ipversion = IPV4_VERSION; 22910 22911 return (tcp); 22912 } 22913 22914 /* 22915 * To remove a port range specified by lo_port and hi_port from the 22916 * reserved port ranges. This is one of the three public functions of 22917 * the reserved port interface. Note that a port range has to be removed 22918 * as a whole. Ports in a range cannot be removed individually. 22919 * 22920 * Params: 22921 * in_port_t lo_port: the beginning port of the reserved port range to 22922 * be deleted. 22923 * in_port_t hi_port: the ending port of the reserved port range to 22924 * be deleted. 22925 * 22926 * Return: 22927 * B_TRUE if the deletion is successful, B_FALSE otherwise. 22928 */ 22929 boolean_t 22930 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 22931 { 22932 int i, j; 22933 int size; 22934 tcp_t **temp_tcp_array; 22935 tcp_t *tcp; 22936 22937 rw_enter(&tcp_reserved_port_lock, RW_WRITER); 22938 22939 /* First make sure that the port ranage is indeed reserved. */ 22940 for (i = 0; i < tcp_reserved_port_array_size; i++) { 22941 if (tcp_reserved_port[i].lo_port == lo_port) { 22942 hi_port = tcp_reserved_port[i].hi_port; 22943 temp_tcp_array = tcp_reserved_port[i].temp_tcp_array; 22944 break; 22945 } 22946 } 22947 if (i == tcp_reserved_port_array_size) { 22948 rw_exit(&tcp_reserved_port_lock); 22949 return (B_FALSE); 22950 } 22951 22952 /* 22953 * Remove the range from the array. This simple loop is possible 22954 * because port ranges are inserted in ascending order. 22955 */ 22956 for (j = i; j < tcp_reserved_port_array_size - 1; j++) { 22957 tcp_reserved_port[j].lo_port = tcp_reserved_port[j+1].lo_port; 22958 tcp_reserved_port[j].hi_port = tcp_reserved_port[j+1].hi_port; 22959 tcp_reserved_port[j].temp_tcp_array = 22960 tcp_reserved_port[j+1].temp_tcp_array; 22961 } 22962 22963 /* Remove all the temporary tcp structures. */ 22964 size = hi_port - lo_port + 1; 22965 while (size > 0) { 22966 tcp = temp_tcp_array[size - 1]; 22967 ASSERT(tcp != NULL); 22968 tcp_bind_hash_remove(tcp); 22969 CONN_DEC_REF(tcp->tcp_connp); 22970 size--; 22971 } 22972 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 22973 tcp_reserved_port_array_size--; 22974 rw_exit(&tcp_reserved_port_lock); 22975 return (B_TRUE); 22976 } 22977 22978 /* 22979 * Macro to remove temporary tcp structure from the bind hash list. The 22980 * first parameter is the list of tcp to be removed. The second parameter 22981 * is the number of tcps in the array. 22982 */ 22983 #define TCP_TMP_TCP_REMOVE(tcp_array, num) \ 22984 { \ 22985 while ((num) > 0) { \ 22986 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 22987 tf_t *tbf; \ 22988 tcp_t *tcpnext; \ 22989 tbf = &tcp_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 22990 mutex_enter(&tbf->tf_lock); \ 22991 tcpnext = tcp->tcp_bind_hash; \ 22992 if (tcpnext) { \ 22993 tcpnext->tcp_ptpbhn = \ 22994 tcp->tcp_ptpbhn; \ 22995 } \ 22996 *tcp->tcp_ptpbhn = tcpnext; \ 22997 mutex_exit(&tbf->tf_lock); \ 22998 kmem_free(tcp, sizeof (tcp_t)); \ 22999 (tcp_array)[(num) - 1] = NULL; \ 23000 (num)--; \ 23001 } \ 23002 } 23003 23004 /* 23005 * The public interface for other modules to call to reserve a port range 23006 * in TCP. The caller passes in how large a port range it wants. TCP 23007 * will try to find a range and return it via lo_port and hi_port. This is 23008 * used by NCA's nca_conn_init. 23009 * NCA can only be used in the global zone so this only affects the global 23010 * zone's ports. 23011 * 23012 * Params: 23013 * int size: the size of the port range to be reserved. 23014 * in_port_t *lo_port (referenced): returns the beginning port of the 23015 * reserved port range added. 23016 * in_port_t *hi_port (referenced): returns the ending port of the 23017 * reserved port range added. 23018 * 23019 * Return: 23020 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 23021 */ 23022 boolean_t 23023 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 23024 { 23025 tcp_t *tcp; 23026 tcp_t *tmp_tcp; 23027 tcp_t **temp_tcp_array; 23028 tf_t *tbf; 23029 in_port_t net_port; 23030 in_port_t port; 23031 int32_t cur_size; 23032 int i, j; 23033 boolean_t used; 23034 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 23035 zoneid_t zoneid = GLOBAL_ZONEID; 23036 23037 /* Sanity check. */ 23038 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 23039 return (B_FALSE); 23040 } 23041 23042 rw_enter(&tcp_reserved_port_lock, RW_WRITER); 23043 if (tcp_reserved_port_array_size == TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 23044 rw_exit(&tcp_reserved_port_lock); 23045 return (B_FALSE); 23046 } 23047 23048 /* 23049 * Find the starting port to try. Since the port ranges are ordered 23050 * in the reserved port array, we can do a simple search here. 23051 */ 23052 *lo_port = TCP_SMALLEST_RESERVED_PORT; 23053 *hi_port = TCP_LARGEST_RESERVED_PORT; 23054 for (i = 0; i < tcp_reserved_port_array_size; 23055 *lo_port = tcp_reserved_port[i].hi_port + 1, i++) { 23056 if (tcp_reserved_port[i].lo_port - *lo_port >= size) { 23057 *hi_port = tcp_reserved_port[i].lo_port - 1; 23058 break; 23059 } 23060 } 23061 /* No available port range. */ 23062 if (i == tcp_reserved_port_array_size && *hi_port - *lo_port < size) { 23063 rw_exit(&tcp_reserved_port_lock); 23064 return (B_FALSE); 23065 } 23066 23067 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 23068 if (temp_tcp_array == NULL) { 23069 rw_exit(&tcp_reserved_port_lock); 23070 return (B_FALSE); 23071 } 23072 23073 /* Go thru the port range to see if some ports are already bound. */ 23074 for (port = *lo_port, cur_size = 0; 23075 cur_size < size && port <= *hi_port; 23076 cur_size++, port++) { 23077 used = B_FALSE; 23078 net_port = htons(port); 23079 tbf = &tcp_bind_fanout[TCP_BIND_HASH(net_port)]; 23080 mutex_enter(&tbf->tf_lock); 23081 for (tcp = tbf->tf_tcp; tcp != NULL; 23082 tcp = tcp->tcp_bind_hash) { 23083 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 23084 net_port == tcp->tcp_lport) { 23085 /* 23086 * A port is already bound. Search again 23087 * starting from port + 1. Release all 23088 * temporary tcps. 23089 */ 23090 mutex_exit(&tbf->tf_lock); 23091 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 23092 *lo_port = port + 1; 23093 cur_size = -1; 23094 used = B_TRUE; 23095 break; 23096 } 23097 } 23098 if (!used) { 23099 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port)) == NULL) { 23100 /* 23101 * Allocation failure. Just fail the request. 23102 * Need to remove all those temporary tcp 23103 * structures. 23104 */ 23105 mutex_exit(&tbf->tf_lock); 23106 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 23107 rw_exit(&tcp_reserved_port_lock); 23108 kmem_free(temp_tcp_array, 23109 (hi_port - lo_port + 1) * 23110 sizeof (tcp_t *)); 23111 return (B_FALSE); 23112 } 23113 temp_tcp_array[cur_size] = tmp_tcp; 23114 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 23115 mutex_exit(&tbf->tf_lock); 23116 } 23117 } 23118 23119 /* 23120 * The current range is not large enough. We can actually do another 23121 * search if this search is done between 2 reserved port ranges. But 23122 * for first release, we just stop here and return saying that no port 23123 * range is available. 23124 */ 23125 if (cur_size < size) { 23126 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 23127 rw_exit(&tcp_reserved_port_lock); 23128 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 23129 return (B_FALSE); 23130 } 23131 *hi_port = port - 1; 23132 23133 /* 23134 * Insert range into array in ascending order. Since this function 23135 * must not be called often, we choose to use the simplest method. 23136 * The above array should not consume excessive stack space as 23137 * the size must be very small. If in future releases, we find 23138 * that we should provide more reserved port ranges, this function 23139 * has to be modified to be more efficient. 23140 */ 23141 if (tcp_reserved_port_array_size == 0) { 23142 tcp_reserved_port[0].lo_port = *lo_port; 23143 tcp_reserved_port[0].hi_port = *hi_port; 23144 tcp_reserved_port[0].temp_tcp_array = temp_tcp_array; 23145 } else { 23146 for (i = 0, j = 0; i < tcp_reserved_port_array_size; i++, j++) { 23147 if (*lo_port < tcp_reserved_port[i].lo_port && i == j) { 23148 tmp_ports[j].lo_port = *lo_port; 23149 tmp_ports[j].hi_port = *hi_port; 23150 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23151 j++; 23152 } 23153 tmp_ports[j].lo_port = tcp_reserved_port[i].lo_port; 23154 tmp_ports[j].hi_port = tcp_reserved_port[i].hi_port; 23155 tmp_ports[j].temp_tcp_array = 23156 tcp_reserved_port[i].temp_tcp_array; 23157 } 23158 if (j == i) { 23159 tmp_ports[j].lo_port = *lo_port; 23160 tmp_ports[j].hi_port = *hi_port; 23161 tmp_ports[j].temp_tcp_array = temp_tcp_array; 23162 } 23163 bcopy(tmp_ports, tcp_reserved_port, sizeof (tmp_ports)); 23164 } 23165 tcp_reserved_port_array_size++; 23166 rw_exit(&tcp_reserved_port_lock); 23167 return (B_TRUE); 23168 } 23169 23170 /* 23171 * Check to see if a port is in any reserved port range. 23172 * 23173 * Params: 23174 * in_port_t port: the port to be verified. 23175 * 23176 * Return: 23177 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 23178 */ 23179 boolean_t 23180 tcp_reserved_port_check(in_port_t port) 23181 { 23182 int i; 23183 23184 rw_enter(&tcp_reserved_port_lock, RW_READER); 23185 for (i = 0; i < tcp_reserved_port_array_size; i++) { 23186 if (port >= tcp_reserved_port[i].lo_port || 23187 port <= tcp_reserved_port[i].hi_port) { 23188 rw_exit(&tcp_reserved_port_lock); 23189 return (B_TRUE); 23190 } 23191 } 23192 rw_exit(&tcp_reserved_port_lock); 23193 return (B_FALSE); 23194 } 23195 23196 /* 23197 * To list all reserved port ranges. This is the function to handle 23198 * ndd tcp_reserved_port_list. 23199 */ 23200 /* ARGSUSED */ 23201 static int 23202 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23203 { 23204 int i; 23205 23206 rw_enter(&tcp_reserved_port_lock, RW_READER); 23207 if (tcp_reserved_port_array_size > 0) 23208 (void) mi_mpprintf(mp, "The following ports are reserved:"); 23209 else 23210 (void) mi_mpprintf(mp, "No port is reserved."); 23211 for (i = 0; i < tcp_reserved_port_array_size; i++) { 23212 (void) mi_mpprintf(mp, "%d-%d", 23213 tcp_reserved_port[i].lo_port, tcp_reserved_port[i].hi_port); 23214 } 23215 rw_exit(&tcp_reserved_port_lock); 23216 return (0); 23217 } 23218 23219 /* 23220 * Hash list insertion routine for tcp_t structures. 23221 * Inserts entries with the ones bound to a specific IP address first 23222 * followed by those bound to INADDR_ANY. 23223 */ 23224 static void 23225 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23226 { 23227 tcp_t **tcpp; 23228 tcp_t *tcpnext; 23229 23230 if (tcp->tcp_ptpbhn != NULL) { 23231 ASSERT(!caller_holds_lock); 23232 tcp_bind_hash_remove(tcp); 23233 } 23234 tcpp = &tbf->tf_tcp; 23235 if (!caller_holds_lock) { 23236 mutex_enter(&tbf->tf_lock); 23237 } else { 23238 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23239 } 23240 tcpnext = tcpp[0]; 23241 if (tcpnext) { 23242 /* 23243 * If the new tcp bound to the INADDR_ANY address 23244 * and the first one in the list is not bound to 23245 * INADDR_ANY we skip all entries until we find the 23246 * first one bound to INADDR_ANY. 23247 * This makes sure that applications binding to a 23248 * specific address get preference over those binding to 23249 * INADDR_ANY. 23250 */ 23251 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23252 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23253 while ((tcpnext = tcpp[0]) != NULL && 23254 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23255 tcpp = &(tcpnext->tcp_bind_hash); 23256 if (tcpnext) 23257 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23258 } else 23259 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23260 } 23261 tcp->tcp_bind_hash = tcpnext; 23262 tcp->tcp_ptpbhn = tcpp; 23263 tcpp[0] = tcp; 23264 if (!caller_holds_lock) 23265 mutex_exit(&tbf->tf_lock); 23266 } 23267 23268 /* 23269 * Hash list removal routine for tcp_t structures. 23270 */ 23271 static void 23272 tcp_bind_hash_remove(tcp_t *tcp) 23273 { 23274 tcp_t *tcpnext; 23275 kmutex_t *lockp; 23276 23277 if (tcp->tcp_ptpbhn == NULL) 23278 return; 23279 23280 /* 23281 * Extract the lock pointer in case there are concurrent 23282 * hash_remove's for this instance. 23283 */ 23284 ASSERT(tcp->tcp_lport != 0); 23285 lockp = &tcp_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23286 23287 ASSERT(lockp != NULL); 23288 mutex_enter(lockp); 23289 if (tcp->tcp_ptpbhn) { 23290 tcpnext = tcp->tcp_bind_hash; 23291 if (tcpnext) { 23292 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23293 tcp->tcp_bind_hash = NULL; 23294 } 23295 *tcp->tcp_ptpbhn = tcpnext; 23296 tcp->tcp_ptpbhn = NULL; 23297 } 23298 mutex_exit(lockp); 23299 } 23300 23301 23302 /* 23303 * Hash list lookup routine for tcp_t structures. 23304 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23305 */ 23306 static tcp_t * 23307 tcp_acceptor_hash_lookup(t_uscalar_t id) 23308 { 23309 tf_t *tf; 23310 tcp_t *tcp; 23311 23312 tf = &tcp_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23313 mutex_enter(&tf->tf_lock); 23314 for (tcp = tf->tf_tcp; tcp != NULL; 23315 tcp = tcp->tcp_acceptor_hash) { 23316 if (tcp->tcp_acceptor_id == id) { 23317 CONN_INC_REF(tcp->tcp_connp); 23318 mutex_exit(&tf->tf_lock); 23319 return (tcp); 23320 } 23321 } 23322 mutex_exit(&tf->tf_lock); 23323 return (NULL); 23324 } 23325 23326 23327 /* 23328 * Hash list insertion routine for tcp_t structures. 23329 */ 23330 void 23331 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23332 { 23333 tf_t *tf; 23334 tcp_t **tcpp; 23335 tcp_t *tcpnext; 23336 23337 tf = &tcp_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23338 23339 if (tcp->tcp_ptpahn != NULL) 23340 tcp_acceptor_hash_remove(tcp); 23341 tcpp = &tf->tf_tcp; 23342 mutex_enter(&tf->tf_lock); 23343 tcpnext = tcpp[0]; 23344 if (tcpnext) 23345 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23346 tcp->tcp_acceptor_hash = tcpnext; 23347 tcp->tcp_ptpahn = tcpp; 23348 tcpp[0] = tcp; 23349 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23350 mutex_exit(&tf->tf_lock); 23351 } 23352 23353 /* 23354 * Hash list removal routine for tcp_t structures. 23355 */ 23356 static void 23357 tcp_acceptor_hash_remove(tcp_t *tcp) 23358 { 23359 tcp_t *tcpnext; 23360 kmutex_t *lockp; 23361 23362 /* 23363 * Extract the lock pointer in case there are concurrent 23364 * hash_remove's for this instance. 23365 */ 23366 lockp = tcp->tcp_acceptor_lockp; 23367 23368 if (tcp->tcp_ptpahn == NULL) 23369 return; 23370 23371 ASSERT(lockp != NULL); 23372 mutex_enter(lockp); 23373 if (tcp->tcp_ptpahn) { 23374 tcpnext = tcp->tcp_acceptor_hash; 23375 if (tcpnext) { 23376 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 23377 tcp->tcp_acceptor_hash = NULL; 23378 } 23379 *tcp->tcp_ptpahn = tcpnext; 23380 tcp->tcp_ptpahn = NULL; 23381 } 23382 mutex_exit(lockp); 23383 tcp->tcp_acceptor_lockp = NULL; 23384 } 23385 23386 /* ARGSUSED */ 23387 static int 23388 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 23389 { 23390 int error = 0; 23391 int retval; 23392 char *end; 23393 23394 tcp_hsp_t *hsp; 23395 tcp_hsp_t *hspprev; 23396 23397 ipaddr_t addr = 0; /* Address we're looking for */ 23398 in6_addr_t v6addr; /* Address we're looking for */ 23399 uint32_t hash; /* Hash of that address */ 23400 23401 /* 23402 * If the following variables are still zero after parsing the input 23403 * string, the user didn't specify them and we don't change them in 23404 * the HSP. 23405 */ 23406 23407 ipaddr_t mask = 0; /* Subnet mask */ 23408 in6_addr_t v6mask; 23409 long sendspace = 0; /* Send buffer size */ 23410 long recvspace = 0; /* Receive buffer size */ 23411 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 23412 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 23413 23414 rw_enter(&tcp_hsp_lock, RW_WRITER); 23415 23416 /* Parse and validate address */ 23417 if (af == AF_INET) { 23418 retval = inet_pton(af, value, &addr); 23419 if (retval == 1) 23420 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 23421 } else if (af == AF_INET6) { 23422 retval = inet_pton(af, value, &v6addr); 23423 } else { 23424 error = EINVAL; 23425 goto done; 23426 } 23427 if (retval == 0) { 23428 error = EINVAL; 23429 goto done; 23430 } 23431 23432 while ((*value) && *value != ' ') 23433 value++; 23434 23435 /* Parse individual keywords, set variables if found */ 23436 while (*value) { 23437 /* Skip leading blanks */ 23438 23439 while (*value == ' ' || *value == '\t') 23440 value++; 23441 23442 /* If at end of string, we're done */ 23443 23444 if (!*value) 23445 break; 23446 23447 /* We have a word, figure out what it is */ 23448 23449 if (strncmp("mask", value, 4) == 0) { 23450 value += 4; 23451 while (*value == ' ' || *value == '\t') 23452 value++; 23453 /* Parse subnet mask */ 23454 if (af == AF_INET) { 23455 retval = inet_pton(af, value, &mask); 23456 if (retval == 1) { 23457 V4MASK_TO_V6(mask, v6mask); 23458 } 23459 } else if (af == AF_INET6) { 23460 retval = inet_pton(af, value, &v6mask); 23461 } 23462 if (retval != 1) { 23463 error = EINVAL; 23464 goto done; 23465 } 23466 while ((*value) && *value != ' ') 23467 value++; 23468 } else if (strncmp("sendspace", value, 9) == 0) { 23469 value += 9; 23470 23471 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 23472 sendspace < TCP_XMIT_HIWATER || 23473 sendspace >= (1L<<30)) { 23474 error = EINVAL; 23475 goto done; 23476 } 23477 value = end; 23478 } else if (strncmp("recvspace", value, 9) == 0) { 23479 value += 9; 23480 23481 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 23482 recvspace < TCP_RECV_HIWATER || 23483 recvspace >= (1L<<30)) { 23484 error = EINVAL; 23485 goto done; 23486 } 23487 value = end; 23488 } else if (strncmp("timestamp", value, 9) == 0) { 23489 value += 9; 23490 23491 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 23492 timestamp < 0 || timestamp > 1) { 23493 error = EINVAL; 23494 goto done; 23495 } 23496 23497 /* 23498 * We increment timestamp so we know it's been set; 23499 * this is undone when we put it in the HSP 23500 */ 23501 timestamp++; 23502 value = end; 23503 } else if (strncmp("delete", value, 6) == 0) { 23504 value += 6; 23505 delete = B_TRUE; 23506 } else { 23507 error = EINVAL; 23508 goto done; 23509 } 23510 } 23511 23512 /* Hash address for lookup */ 23513 23514 hash = TCP_HSP_HASH(addr); 23515 23516 if (delete) { 23517 /* 23518 * Note that deletes don't return an error if the thing 23519 * we're trying to delete isn't there. 23520 */ 23521 if (tcp_hsp_hash == NULL) 23522 goto done; 23523 hsp = tcp_hsp_hash[hash]; 23524 23525 if (hsp) { 23526 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 23527 &v6addr)) { 23528 tcp_hsp_hash[hash] = hsp->tcp_hsp_next; 23529 mi_free((char *)hsp); 23530 } else { 23531 hspprev = hsp; 23532 while ((hsp = hsp->tcp_hsp_next) != NULL) { 23533 if (IN6_ARE_ADDR_EQUAL( 23534 &hsp->tcp_hsp_addr_v6, &v6addr)) { 23535 hspprev->tcp_hsp_next = 23536 hsp->tcp_hsp_next; 23537 mi_free((char *)hsp); 23538 break; 23539 } 23540 hspprev = hsp; 23541 } 23542 } 23543 } 23544 } else { 23545 /* 23546 * We're adding/modifying an HSP. If we haven't already done 23547 * so, allocate the hash table. 23548 */ 23549 23550 if (!tcp_hsp_hash) { 23551 tcp_hsp_hash = (tcp_hsp_t **) 23552 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 23553 if (!tcp_hsp_hash) { 23554 error = EINVAL; 23555 goto done; 23556 } 23557 } 23558 23559 /* Get head of hash chain */ 23560 23561 hsp = tcp_hsp_hash[hash]; 23562 23563 /* Try to find pre-existing hsp on hash chain */ 23564 /* Doesn't handle CIDR prefixes. */ 23565 while (hsp) { 23566 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 23567 break; 23568 hsp = hsp->tcp_hsp_next; 23569 } 23570 23571 /* 23572 * If we didn't, create one with default values and put it 23573 * at head of hash chain 23574 */ 23575 23576 if (!hsp) { 23577 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 23578 if (!hsp) { 23579 error = EINVAL; 23580 goto done; 23581 } 23582 hsp->tcp_hsp_next = tcp_hsp_hash[hash]; 23583 tcp_hsp_hash[hash] = hsp; 23584 } 23585 23586 /* Set values that the user asked us to change */ 23587 23588 hsp->tcp_hsp_addr_v6 = v6addr; 23589 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 23590 hsp->tcp_hsp_vers = IPV4_VERSION; 23591 else 23592 hsp->tcp_hsp_vers = IPV6_VERSION; 23593 hsp->tcp_hsp_subnet_v6 = v6mask; 23594 if (sendspace > 0) 23595 hsp->tcp_hsp_sendspace = sendspace; 23596 if (recvspace > 0) 23597 hsp->tcp_hsp_recvspace = recvspace; 23598 if (timestamp > 0) 23599 hsp->tcp_hsp_tstamp = timestamp - 1; 23600 } 23601 23602 done: 23603 rw_exit(&tcp_hsp_lock); 23604 return (error); 23605 } 23606 23607 /* Set callback routine passed to nd_load by tcp_param_register. */ 23608 /* ARGSUSED */ 23609 static int 23610 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 23611 { 23612 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 23613 } 23614 /* ARGSUSED */ 23615 static int 23616 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23617 cred_t *cr) 23618 { 23619 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 23620 } 23621 23622 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 23623 /* ARGSUSED */ 23624 static int 23625 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23626 { 23627 tcp_hsp_t *hsp; 23628 int i; 23629 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 23630 23631 rw_enter(&tcp_hsp_lock, RW_READER); 23632 (void) mi_mpprintf(mp, 23633 "Hash HSP " MI_COL_HDRPAD_STR 23634 "Address Subnet Mask Send Receive TStamp"); 23635 if (tcp_hsp_hash) { 23636 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 23637 hsp = tcp_hsp_hash[i]; 23638 while (hsp) { 23639 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 23640 (void) inet_ntop(AF_INET, 23641 &hsp->tcp_hsp_addr, 23642 addrbuf, sizeof (addrbuf)); 23643 (void) inet_ntop(AF_INET, 23644 &hsp->tcp_hsp_subnet, 23645 subnetbuf, sizeof (subnetbuf)); 23646 } else { 23647 (void) inet_ntop(AF_INET6, 23648 &hsp->tcp_hsp_addr_v6, 23649 addrbuf, sizeof (addrbuf)); 23650 (void) inet_ntop(AF_INET6, 23651 &hsp->tcp_hsp_subnet_v6, 23652 subnetbuf, sizeof (subnetbuf)); 23653 } 23654 (void) mi_mpprintf(mp, 23655 " %03d " MI_COL_PTRFMT_STR 23656 "%s %s %010d %010d %d", 23657 i, 23658 (void *)hsp, 23659 addrbuf, 23660 subnetbuf, 23661 hsp->tcp_hsp_sendspace, 23662 hsp->tcp_hsp_recvspace, 23663 hsp->tcp_hsp_tstamp); 23664 23665 hsp = hsp->tcp_hsp_next; 23666 } 23667 } 23668 } 23669 rw_exit(&tcp_hsp_lock); 23670 return (0); 23671 } 23672 23673 23674 /* Data for fast netmask macro used by tcp_hsp_lookup */ 23675 23676 static ipaddr_t netmasks[] = { 23677 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 23678 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 23679 }; 23680 23681 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 23682 23683 /* 23684 * XXX This routine should go away and instead we should use the metrics 23685 * associated with the routes to determine the default sndspace and rcvspace. 23686 */ 23687 static tcp_hsp_t * 23688 tcp_hsp_lookup(ipaddr_t addr) 23689 { 23690 tcp_hsp_t *hsp = NULL; 23691 23692 /* Quick check without acquiring the lock. */ 23693 if (tcp_hsp_hash == NULL) 23694 return (NULL); 23695 23696 rw_enter(&tcp_hsp_lock, RW_READER); 23697 23698 /* This routine finds the best-matching HSP for address addr. */ 23699 23700 if (tcp_hsp_hash) { 23701 int i; 23702 ipaddr_t srchaddr; 23703 tcp_hsp_t *hsp_net; 23704 23705 /* We do three passes: host, network, and subnet. */ 23706 23707 srchaddr = addr; 23708 23709 for (i = 1; i <= 3; i++) { 23710 /* Look for exact match on srchaddr */ 23711 23712 hsp = tcp_hsp_hash[TCP_HSP_HASH(srchaddr)]; 23713 while (hsp) { 23714 if (hsp->tcp_hsp_vers == IPV4_VERSION && 23715 hsp->tcp_hsp_addr == srchaddr) 23716 break; 23717 hsp = hsp->tcp_hsp_next; 23718 } 23719 ASSERT(hsp == NULL || 23720 hsp->tcp_hsp_vers == IPV4_VERSION); 23721 23722 /* 23723 * If this is the first pass: 23724 * If we found a match, great, return it. 23725 * If not, search for the network on the second pass. 23726 */ 23727 23728 if (i == 1) 23729 if (hsp) 23730 break; 23731 else 23732 { 23733 srchaddr = addr & netmask(addr); 23734 continue; 23735 } 23736 23737 /* 23738 * If this is the second pass: 23739 * If we found a match, but there's a subnet mask, 23740 * save the match but try again using the subnet 23741 * mask on the third pass. 23742 * Otherwise, return whatever we found. 23743 */ 23744 23745 if (i == 2) { 23746 if (hsp && hsp->tcp_hsp_subnet) { 23747 hsp_net = hsp; 23748 srchaddr = addr & hsp->tcp_hsp_subnet; 23749 continue; 23750 } else { 23751 break; 23752 } 23753 } 23754 23755 /* 23756 * This must be the third pass. If we didn't find 23757 * anything, return the saved network HSP instead. 23758 */ 23759 23760 if (!hsp) 23761 hsp = hsp_net; 23762 } 23763 } 23764 23765 rw_exit(&tcp_hsp_lock); 23766 return (hsp); 23767 } 23768 23769 /* 23770 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 23771 * match lookup. 23772 */ 23773 static tcp_hsp_t * 23774 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr) 23775 { 23776 tcp_hsp_t *hsp = NULL; 23777 23778 /* Quick check without acquiring the lock. */ 23779 if (tcp_hsp_hash == NULL) 23780 return (NULL); 23781 23782 rw_enter(&tcp_hsp_lock, RW_READER); 23783 23784 /* This routine finds the best-matching HSP for address addr. */ 23785 23786 if (tcp_hsp_hash) { 23787 int i; 23788 in6_addr_t v6srchaddr; 23789 tcp_hsp_t *hsp_net; 23790 23791 /* We do three passes: host, network, and subnet. */ 23792 23793 v6srchaddr = *v6addr; 23794 23795 for (i = 1; i <= 3; i++) { 23796 /* Look for exact match on srchaddr */ 23797 23798 hsp = tcp_hsp_hash[TCP_HSP_HASH( 23799 V4_PART_OF_V6(v6srchaddr))]; 23800 while (hsp) { 23801 if (hsp->tcp_hsp_vers == IPV6_VERSION && 23802 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 23803 &v6srchaddr)) 23804 break; 23805 hsp = hsp->tcp_hsp_next; 23806 } 23807 23808 /* 23809 * If this is the first pass: 23810 * If we found a match, great, return it. 23811 * If not, search for the network on the second pass. 23812 */ 23813 23814 if (i == 1) 23815 if (hsp) 23816 break; 23817 else { 23818 /* Assume a 64 bit mask */ 23819 v6srchaddr.s6_addr32[0] = 23820 v6addr->s6_addr32[0]; 23821 v6srchaddr.s6_addr32[1] = 23822 v6addr->s6_addr32[1]; 23823 v6srchaddr.s6_addr32[2] = 0; 23824 v6srchaddr.s6_addr32[3] = 0; 23825 continue; 23826 } 23827 23828 /* 23829 * If this is the second pass: 23830 * If we found a match, but there's a subnet mask, 23831 * save the match but try again using the subnet 23832 * mask on the third pass. 23833 * Otherwise, return whatever we found. 23834 */ 23835 23836 if (i == 2) { 23837 ASSERT(hsp == NULL || 23838 hsp->tcp_hsp_vers == IPV6_VERSION); 23839 if (hsp && 23840 !IN6_IS_ADDR_UNSPECIFIED( 23841 &hsp->tcp_hsp_subnet_v6)) { 23842 hsp_net = hsp; 23843 V6_MASK_COPY(*v6addr, 23844 hsp->tcp_hsp_subnet_v6, v6srchaddr); 23845 continue; 23846 } else { 23847 break; 23848 } 23849 } 23850 23851 /* 23852 * This must be the third pass. If we didn't find 23853 * anything, return the saved network HSP instead. 23854 */ 23855 23856 if (!hsp) 23857 hsp = hsp_net; 23858 } 23859 } 23860 23861 rw_exit(&tcp_hsp_lock); 23862 return (hsp); 23863 } 23864 23865 /* 23866 * Type three generator adapted from the random() function in 4.4 BSD: 23867 */ 23868 23869 /* 23870 * Copyright (c) 1983, 1993 23871 * The Regents of the University of California. All rights reserved. 23872 * 23873 * Redistribution and use in source and binary forms, with or without 23874 * modification, are permitted provided that the following conditions 23875 * are met: 23876 * 1. Redistributions of source code must retain the above copyright 23877 * notice, this list of conditions and the following disclaimer. 23878 * 2. Redistributions in binary form must reproduce the above copyright 23879 * notice, this list of conditions and the following disclaimer in the 23880 * documentation and/or other materials provided with the distribution. 23881 * 3. All advertising materials mentioning features or use of this software 23882 * must display the following acknowledgement: 23883 * This product includes software developed by the University of 23884 * California, Berkeley and its contributors. 23885 * 4. Neither the name of the University nor the names of its contributors 23886 * may be used to endorse or promote products derived from this software 23887 * without specific prior written permission. 23888 * 23889 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23890 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23891 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23892 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23893 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23894 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23895 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23896 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23897 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23898 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23899 * SUCH DAMAGE. 23900 */ 23901 23902 /* Type 3 -- x**31 + x**3 + 1 */ 23903 #define DEG_3 31 23904 #define SEP_3 3 23905 23906 23907 /* Protected by tcp_random_lock */ 23908 static int tcp_randtbl[DEG_3 + 1]; 23909 23910 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23911 static int *tcp_random_rptr = &tcp_randtbl[1]; 23912 23913 static int *tcp_random_state = &tcp_randtbl[1]; 23914 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23915 23916 kmutex_t tcp_random_lock; 23917 23918 void 23919 tcp_random_init(void) 23920 { 23921 int i; 23922 hrtime_t hrt; 23923 time_t wallclock; 23924 uint64_t result; 23925 23926 /* 23927 * Use high-res timer and current time for seed. Gethrtime() returns 23928 * a longlong, which may contain resolution down to nanoseconds. 23929 * The current time will either be a 32-bit or a 64-bit quantity. 23930 * XOR the two together in a 64-bit result variable. 23931 * Convert the result to a 32-bit value by multiplying the high-order 23932 * 32-bits by the low-order 32-bits. 23933 */ 23934 23935 hrt = gethrtime(); 23936 (void) drv_getparm(TIME, &wallclock); 23937 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23938 mutex_enter(&tcp_random_lock); 23939 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23940 (result & 0xffffffff); 23941 23942 for (i = 1; i < DEG_3; i++) 23943 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23944 + 12345; 23945 tcp_random_fptr = &tcp_random_state[SEP_3]; 23946 tcp_random_rptr = &tcp_random_state[0]; 23947 mutex_exit(&tcp_random_lock); 23948 for (i = 0; i < 10 * DEG_3; i++) 23949 (void) tcp_random(); 23950 } 23951 23952 /* 23953 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23954 * This range is selected to be approximately centered on TCP_ISS / 2, 23955 * and easy to compute. We get this value by generating a 32-bit random 23956 * number, selecting out the high-order 17 bits, and then adding one so 23957 * that we never return zero. 23958 */ 23959 int 23960 tcp_random(void) 23961 { 23962 int i; 23963 23964 mutex_enter(&tcp_random_lock); 23965 *tcp_random_fptr += *tcp_random_rptr; 23966 23967 /* 23968 * The high-order bits are more random than the low-order bits, 23969 * so we select out the high-order 17 bits and add one so that 23970 * we never return zero. 23971 */ 23972 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23973 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23974 tcp_random_fptr = tcp_random_state; 23975 ++tcp_random_rptr; 23976 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23977 tcp_random_rptr = tcp_random_state; 23978 23979 mutex_exit(&tcp_random_lock); 23980 return (i); 23981 } 23982 23983 /* 23984 * XXX This will go away when TPI is extended to send 23985 * info reqs to sockfs/timod ..... 23986 * Given a queue, set the max packet size for the write 23987 * side of the queue below stream head. This value is 23988 * cached on the stream head. 23989 * Returns 1 on success, 0 otherwise. 23990 */ 23991 static int 23992 setmaxps(queue_t *q, int maxpsz) 23993 { 23994 struct stdata *stp; 23995 queue_t *wq; 23996 stp = STREAM(q); 23997 23998 /* 23999 * At this point change of a queue parameter is not allowed 24000 * when a multiplexor is sitting on top. 24001 */ 24002 if (stp->sd_flag & STPLEX) 24003 return (0); 24004 24005 claimstr(stp->sd_wrq); 24006 wq = stp->sd_wrq->q_next; 24007 ASSERT(wq != NULL); 24008 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24009 releasestr(stp->sd_wrq); 24010 return (1); 24011 } 24012 24013 static int 24014 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24015 int *t_errorp, int *sys_errorp) 24016 { 24017 int error; 24018 int is_absreq_failure; 24019 t_scalar_t *opt_lenp; 24020 t_scalar_t opt_offset; 24021 int prim_type; 24022 struct T_conn_req *tcreqp; 24023 struct T_conn_res *tcresp; 24024 cred_t *cr; 24025 24026 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24027 24028 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24029 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24030 prim_type == T_CONN_RES); 24031 24032 switch (prim_type) { 24033 case T_CONN_REQ: 24034 tcreqp = (struct T_conn_req *)mp->b_rptr; 24035 opt_offset = tcreqp->OPT_offset; 24036 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24037 break; 24038 case O_T_CONN_RES: 24039 case T_CONN_RES: 24040 tcresp = (struct T_conn_res *)mp->b_rptr; 24041 opt_offset = tcresp->OPT_offset; 24042 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24043 break; 24044 } 24045 24046 *t_errorp = 0; 24047 *sys_errorp = 0; 24048 *do_disconnectp = 0; 24049 24050 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24051 opt_offset, cr, &tcp_opt_obj, 24052 NULL, &is_absreq_failure); 24053 24054 switch (error) { 24055 case 0: /* no error */ 24056 ASSERT(is_absreq_failure == 0); 24057 return (0); 24058 case ENOPROTOOPT: 24059 *t_errorp = TBADOPT; 24060 break; 24061 case EACCES: 24062 *t_errorp = TACCES; 24063 break; 24064 default: 24065 *t_errorp = TSYSERR; *sys_errorp = error; 24066 break; 24067 } 24068 if (is_absreq_failure != 0) { 24069 /* 24070 * The connection request should get the local ack 24071 * T_OK_ACK and then a T_DISCON_IND. 24072 */ 24073 *do_disconnectp = 1; 24074 } 24075 return (-1); 24076 } 24077 24078 /* 24079 * Split this function out so that if the secret changes, I'm okay. 24080 * 24081 * Initialize the tcp_iss_cookie and tcp_iss_key. 24082 */ 24083 24084 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24085 24086 static void 24087 tcp_iss_key_init(uint8_t *phrase, int len) 24088 { 24089 struct { 24090 int32_t current_time; 24091 uint32_t randnum; 24092 uint16_t pad; 24093 uint8_t ether[6]; 24094 uint8_t passwd[PASSWD_SIZE]; 24095 } tcp_iss_cookie; 24096 time_t t; 24097 24098 /* 24099 * Start with the current absolute time. 24100 */ 24101 (void) drv_getparm(TIME, &t); 24102 tcp_iss_cookie.current_time = t; 24103 24104 /* 24105 * XXX - Need a more random number per RFC 1750, not this crap. 24106 * OTOH, if what follows is pretty random, then I'm in better shape. 24107 */ 24108 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24109 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24110 24111 /* 24112 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24113 * as a good template. 24114 */ 24115 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24116 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24117 24118 /* 24119 * The pass-phrase. Normally this is supplied by user-called NDD. 24120 */ 24121 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24122 24123 /* 24124 * See 4010593 if this section becomes a problem again, 24125 * but the local ethernet address is useful here. 24126 */ 24127 (void) localetheraddr(NULL, 24128 (struct ether_addr *)&tcp_iss_cookie.ether); 24129 24130 /* 24131 * Hash 'em all together. The MD5Final is called per-connection. 24132 */ 24133 mutex_enter(&tcp_iss_key_lock); 24134 MD5Init(&tcp_iss_key); 24135 MD5Update(&tcp_iss_key, (uchar_t *)&tcp_iss_cookie, 24136 sizeof (tcp_iss_cookie)); 24137 mutex_exit(&tcp_iss_key_lock); 24138 } 24139 24140 /* 24141 * Set the RFC 1948 pass phrase 24142 */ 24143 /* ARGSUSED */ 24144 static int 24145 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24146 cred_t *cr) 24147 { 24148 /* 24149 * Basically, value contains a new pass phrase. Pass it along! 24150 */ 24151 tcp_iss_key_init((uint8_t *)value, strlen(value)); 24152 return (0); 24153 } 24154 24155 /* ARGSUSED */ 24156 static int 24157 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24158 { 24159 bzero(buf, sizeof (tcp_sack_info_t)); 24160 return (0); 24161 } 24162 24163 /* ARGSUSED */ 24164 static int 24165 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24166 { 24167 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24168 return (0); 24169 } 24170 24171 void 24172 tcp_ddi_init(void) 24173 { 24174 int i; 24175 24176 /* Initialize locks */ 24177 rw_init(&tcp_hsp_lock, NULL, RW_DEFAULT, NULL); 24178 mutex_init(&tcp_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24179 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24180 mutex_init(&tcp_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24181 mutex_init(&tcp_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24182 rw_init(&tcp_reserved_port_lock, NULL, RW_DEFAULT, NULL); 24183 24184 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 24185 mutex_init(&tcp_bind_fanout[i].tf_lock, NULL, 24186 MUTEX_DEFAULT, NULL); 24187 } 24188 24189 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 24190 mutex_init(&tcp_acceptor_fanout[i].tf_lock, NULL, 24191 MUTEX_DEFAULT, NULL); 24192 } 24193 24194 /* TCP's IPsec code calls the packet dropper. */ 24195 ip_drop_register(&tcp_dropper, "TCP IPsec policy enforcement"); 24196 24197 if (!tcp_g_nd) { 24198 if (!tcp_param_register(tcp_param_arr, A_CNT(tcp_param_arr))) { 24199 nd_free(&tcp_g_nd); 24200 } 24201 } 24202 24203 /* 24204 * Note: To really walk the device tree you need the devinfo 24205 * pointer to your device which is only available after probe/attach. 24206 * The following is safe only because it uses ddi_root_node() 24207 */ 24208 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24209 tcp_opt_obj.odb_opt_arr_cnt); 24210 24211 tcp_timercache = kmem_cache_create("tcp_timercache", 24212 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24213 NULL, NULL, NULL, NULL, NULL, 0); 24214 24215 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24216 sizeof (tcp_sack_info_t), 0, 24217 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24218 24219 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24220 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24221 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24222 24223 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 24224 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 24225 24226 ip_squeue_init(tcp_squeue_add); 24227 24228 /* Initialize the random number generator */ 24229 tcp_random_init(); 24230 24231 /* 24232 * Initialize RFC 1948 secret values. This will probably be reset once 24233 * by the boot scripts. 24234 * 24235 * Use NULL name, as the name is caught by the new lockstats. 24236 * 24237 * Initialize with some random, non-guessable string, like the global 24238 * T_INFO_ACK. 24239 */ 24240 24241 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24242 sizeof (tcp_g_t_info_ack)); 24243 24244 if ((tcp_kstat = kstat_create(TCP_MOD_NAME, 0, "tcpstat", 24245 "net", KSTAT_TYPE_NAMED, 24246 sizeof (tcp_statistics) / sizeof (kstat_named_t), 24247 KSTAT_FLAG_VIRTUAL)) != NULL) { 24248 tcp_kstat->ks_data = &tcp_statistics; 24249 kstat_install(tcp_kstat); 24250 } 24251 24252 tcp_kstat_init(); 24253 } 24254 24255 void 24256 tcp_ddi_destroy(void) 24257 { 24258 int i; 24259 24260 nd_free(&tcp_g_nd); 24261 24262 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 24263 mutex_destroy(&tcp_bind_fanout[i].tf_lock); 24264 } 24265 24266 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 24267 mutex_destroy(&tcp_acceptor_fanout[i].tf_lock); 24268 } 24269 24270 mutex_destroy(&tcp_iss_key_lock); 24271 rw_destroy(&tcp_hsp_lock); 24272 mutex_destroy(&tcp_g_q_lock); 24273 mutex_destroy(&tcp_random_lock); 24274 mutex_destroy(&tcp_epriv_port_lock); 24275 rw_destroy(&tcp_reserved_port_lock); 24276 24277 ip_drop_unregister(&tcp_dropper); 24278 24279 kmem_cache_destroy(tcp_timercache); 24280 kmem_cache_destroy(tcp_sack_info_cache); 24281 kmem_cache_destroy(tcp_iphc_cache); 24282 24283 tcp_kstat_fini(); 24284 } 24285 24286 /* 24287 * Generate ISS, taking into account NDD changes may happen halfway through. 24288 * (If the iss is not zero, set it.) 24289 */ 24290 24291 static void 24292 tcp_iss_init(tcp_t *tcp) 24293 { 24294 MD5_CTX context; 24295 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 24296 uint32_t answer[4]; 24297 24298 tcp_iss_incr_extra += (ISS_INCR >> 1); 24299 tcp->tcp_iss = tcp_iss_incr_extra; 24300 switch (tcp_strong_iss) { 24301 case 2: 24302 mutex_enter(&tcp_iss_key_lock); 24303 context = tcp_iss_key; 24304 mutex_exit(&tcp_iss_key_lock); 24305 arg.ports = tcp->tcp_ports; 24306 if (tcp->tcp_ipversion == IPV4_VERSION) { 24307 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 24308 &arg.src); 24309 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 24310 &arg.dst); 24311 } else { 24312 arg.src = tcp->tcp_ip6h->ip6_src; 24313 arg.dst = tcp->tcp_ip6h->ip6_dst; 24314 } 24315 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 24316 MD5Final((uchar_t *)answer, &context); 24317 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 24318 /* 24319 * Now that we've hashed into a unique per-connection sequence 24320 * space, add a random increment per strong_iss == 1. So I 24321 * guess we'll have to... 24322 */ 24323 /* FALLTHRU */ 24324 case 1: 24325 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 24326 break; 24327 default: 24328 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24329 break; 24330 } 24331 tcp->tcp_valid_bits = TCP_ISS_VALID; 24332 tcp->tcp_fss = tcp->tcp_iss - 1; 24333 tcp->tcp_suna = tcp->tcp_iss; 24334 tcp->tcp_snxt = tcp->tcp_iss + 1; 24335 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 24336 tcp->tcp_csuna = tcp->tcp_snxt; 24337 } 24338 24339 /* 24340 * Exported routine for extracting active tcp connection status. 24341 * 24342 * This is used by the Solaris Cluster Networking software to 24343 * gather a list of connections that need to be forwarded to 24344 * specific nodes in the cluster when configuration changes occur. 24345 * 24346 * The callback is invoked for each tcp_t structure. Returning 24347 * non-zero from the callback routine terminates the search. 24348 */ 24349 int 24350 cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg) 24351 { 24352 tcp_t *tcp; 24353 cl_tcp_info_t cl_tcpi; 24354 connf_t *connfp; 24355 conn_t *connp; 24356 int i; 24357 24358 ASSERT(callback != NULL); 24359 24360 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24361 24362 connfp = &ipcl_globalhash_fanout[i]; 24363 connp = NULL; 24364 24365 while ((connp = 24366 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24367 24368 tcp = connp->conn_tcp; 24369 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 24370 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 24371 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 24372 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 24373 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 24374 /* 24375 * The macros tcp_laddr and tcp_faddr give the IPv4 24376 * addresses. They are copied implicitly below as 24377 * mapped addresses. 24378 */ 24379 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 24380 if (tcp->tcp_ipversion == IPV4_VERSION) { 24381 cl_tcpi.cl_tcpi_faddr = 24382 tcp->tcp_ipha->ipha_dst; 24383 } else { 24384 cl_tcpi.cl_tcpi_faddr_v6 = 24385 tcp->tcp_ip6h->ip6_dst; 24386 } 24387 24388 /* 24389 * If the callback returns non-zero 24390 * we terminate the traversal. 24391 */ 24392 if ((*callback)(&cl_tcpi, arg) != 0) { 24393 CONN_DEC_REF(tcp->tcp_connp); 24394 return (1); 24395 } 24396 } 24397 } 24398 24399 return (0); 24400 } 24401 24402 /* 24403 * Macros used for accessing the different types of sockaddr 24404 * structures inside a tcp_ioc_abort_conn_t. 24405 */ 24406 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 24407 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 24408 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 24409 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 24410 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 24411 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 24412 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 24413 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 24414 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 24415 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 24416 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 24417 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 24418 24419 /* 24420 * Return the correct error code to mimic the behavior 24421 * of a connection reset. 24422 */ 24423 #define TCP_AC_GET_ERRCODE(state, err) { \ 24424 switch ((state)) { \ 24425 case TCPS_SYN_SENT: \ 24426 case TCPS_SYN_RCVD: \ 24427 (err) = ECONNREFUSED; \ 24428 break; \ 24429 case TCPS_ESTABLISHED: \ 24430 case TCPS_FIN_WAIT_1: \ 24431 case TCPS_FIN_WAIT_2: \ 24432 case TCPS_CLOSE_WAIT: \ 24433 (err) = ECONNRESET; \ 24434 break; \ 24435 case TCPS_CLOSING: \ 24436 case TCPS_LAST_ACK: \ 24437 case TCPS_TIME_WAIT: \ 24438 (err) = 0; \ 24439 break; \ 24440 default: \ 24441 (err) = ENXIO; \ 24442 } \ 24443 } 24444 24445 /* 24446 * Check if a tcp structure matches the info in acp. 24447 */ 24448 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 24449 (((acp)->ac_local.ss_family == AF_INET) ? \ 24450 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 24451 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 24452 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 24453 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 24454 (TCP_AC_V4LPORT((acp)) == 0 || \ 24455 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 24456 (TCP_AC_V4RPORT((acp)) == 0 || \ 24457 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24458 (acp)->ac_start <= (tcp)->tcp_state && \ 24459 (acp)->ac_end >= (tcp)->tcp_state) : \ 24460 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24461 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24462 &(tcp)->tcp_ip_src_v6)) && \ 24463 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24464 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24465 &(tcp)->tcp_remote_v6)) && \ 24466 (TCP_AC_V6LPORT((acp)) == 0 || \ 24467 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24468 (TCP_AC_V6RPORT((acp)) == 0 || \ 24469 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24470 (acp)->ac_start <= (tcp)->tcp_state && \ 24471 (acp)->ac_end >= (tcp)->tcp_state)) 24472 24473 #define TCP_AC_MATCH(acp, tcp) \ 24474 (((acp)->ac_zoneid == ALL_ZONES || \ 24475 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24476 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24477 24478 /* 24479 * Build a message containing a tcp_ioc_abort_conn_t structure 24480 * which is filled in with information from acp and tp. 24481 */ 24482 static mblk_t * 24483 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24484 { 24485 mblk_t *mp; 24486 tcp_ioc_abort_conn_t *tacp; 24487 24488 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24489 if (mp == NULL) 24490 return (NULL); 24491 24492 mp->b_datap->db_type = M_CTL; 24493 24494 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24495 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24496 sizeof (uint32_t)); 24497 24498 tacp->ac_start = acp->ac_start; 24499 tacp->ac_end = acp->ac_end; 24500 tacp->ac_zoneid = acp->ac_zoneid; 24501 24502 if (acp->ac_local.ss_family == AF_INET) { 24503 tacp->ac_local.ss_family = AF_INET; 24504 tacp->ac_remote.ss_family = AF_INET; 24505 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24506 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24507 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24508 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24509 } else { 24510 tacp->ac_local.ss_family = AF_INET6; 24511 tacp->ac_remote.ss_family = AF_INET6; 24512 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24513 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24514 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24515 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24516 } 24517 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24518 return (mp); 24519 } 24520 24521 /* 24522 * Print a tcp_ioc_abort_conn_t structure. 24523 */ 24524 static void 24525 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24526 { 24527 char lbuf[128]; 24528 char rbuf[128]; 24529 sa_family_t af; 24530 in_port_t lport, rport; 24531 ushort_t logflags; 24532 24533 af = acp->ac_local.ss_family; 24534 24535 if (af == AF_INET) { 24536 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24537 lbuf, 128); 24538 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24539 rbuf, 128); 24540 lport = ntohs(TCP_AC_V4LPORT(acp)); 24541 rport = ntohs(TCP_AC_V4RPORT(acp)); 24542 } else { 24543 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24544 lbuf, 128); 24545 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24546 rbuf, 128); 24547 lport = ntohs(TCP_AC_V6LPORT(acp)); 24548 rport = ntohs(TCP_AC_V6RPORT(acp)); 24549 } 24550 24551 logflags = SL_TRACE | SL_NOTE; 24552 /* 24553 * Don't print this message to the console if the operation was done 24554 * to a non-global zone. 24555 */ 24556 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24557 logflags |= SL_CONSOLE; 24558 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24559 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24560 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24561 acp->ac_start, acp->ac_end); 24562 } 24563 24564 /* 24565 * Called inside tcp_rput when a message built using 24566 * tcp_ioctl_abort_build_msg is put into a queue. 24567 * Note that when we get here there is no wildcard in acp any more. 24568 */ 24569 static void 24570 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24571 { 24572 tcp_ioc_abort_conn_t *acp; 24573 24574 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24575 if (tcp->tcp_state <= acp->ac_end) { 24576 /* 24577 * If we get here, we are already on the correct 24578 * squeue. This ioctl follows the following path 24579 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24580 * ->tcp_ioctl_abort->squeue_fill (if on a 24581 * different squeue) 24582 */ 24583 int errcode; 24584 24585 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24586 (void) tcp_clean_death(tcp, errcode, 26); 24587 } 24588 freemsg(mp); 24589 } 24590 24591 /* 24592 * Abort all matching connections on a hash chain. 24593 */ 24594 static int 24595 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24596 boolean_t exact) 24597 { 24598 int nmatch, err = 0; 24599 tcp_t *tcp; 24600 MBLKP mp, last, listhead = NULL; 24601 conn_t *tconnp; 24602 connf_t *connfp = &ipcl_conn_fanout[index]; 24603 24604 startover: 24605 nmatch = 0; 24606 24607 mutex_enter(&connfp->connf_lock); 24608 for (tconnp = connfp->connf_head; tconnp != NULL; 24609 tconnp = tconnp->conn_next) { 24610 tcp = tconnp->conn_tcp; 24611 if (TCP_AC_MATCH(acp, tcp)) { 24612 CONN_INC_REF(tcp->tcp_connp); 24613 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24614 if (mp == NULL) { 24615 err = ENOMEM; 24616 CONN_DEC_REF(tcp->tcp_connp); 24617 break; 24618 } 24619 mp->b_prev = (mblk_t *)tcp; 24620 24621 if (listhead == NULL) { 24622 listhead = mp; 24623 last = mp; 24624 } else { 24625 last->b_next = mp; 24626 last = mp; 24627 } 24628 nmatch++; 24629 if (exact) 24630 break; 24631 } 24632 24633 /* Avoid holding lock for too long. */ 24634 if (nmatch >= 500) 24635 break; 24636 } 24637 mutex_exit(&connfp->connf_lock); 24638 24639 /* Pass mp into the correct tcp */ 24640 while ((mp = listhead) != NULL) { 24641 listhead = listhead->b_next; 24642 tcp = (tcp_t *)mp->b_prev; 24643 mp->b_next = mp->b_prev = NULL; 24644 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 24645 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 24646 } 24647 24648 *count += nmatch; 24649 if (nmatch >= 500 && err == 0) 24650 goto startover; 24651 return (err); 24652 } 24653 24654 /* 24655 * Abort all connections that matches the attributes specified in acp. 24656 */ 24657 static int 24658 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp) 24659 { 24660 sa_family_t af; 24661 uint32_t ports; 24662 uint16_t *pports; 24663 int err = 0, count = 0; 24664 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24665 int index = -1; 24666 ushort_t logflags; 24667 24668 af = acp->ac_local.ss_family; 24669 24670 if (af == AF_INET) { 24671 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24672 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24673 pports = (uint16_t *)&ports; 24674 pports[1] = TCP_AC_V4LPORT(acp); 24675 pports[0] = TCP_AC_V4RPORT(acp); 24676 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24677 } 24678 } else { 24679 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 24680 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 24681 pports = (uint16_t *)&ports; 24682 pports[1] = TCP_AC_V6LPORT(acp); 24683 pports[0] = TCP_AC_V6RPORT(acp); 24684 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 24685 } 24686 } 24687 24688 /* 24689 * For cases where remote addr, local port, and remote port are non- 24690 * wildcards, tcp_ioctl_abort_bucket will only be called once. 24691 */ 24692 if (index != -1) { 24693 err = tcp_ioctl_abort_bucket(acp, index, 24694 &count, exact); 24695 } else { 24696 /* 24697 * loop through all entries for wildcard case 24698 */ 24699 for (index = 0; index < ipcl_conn_fanout_size; index++) { 24700 err = tcp_ioctl_abort_bucket(acp, index, 24701 &count, exact); 24702 if (err != 0) 24703 break; 24704 } 24705 } 24706 24707 logflags = SL_TRACE | SL_NOTE; 24708 /* 24709 * Don't print this message to the console if the operation was done 24710 * to a non-global zone. 24711 */ 24712 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24713 logflags |= SL_CONSOLE; 24714 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 24715 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 24716 if (err == 0 && count == 0) 24717 err = ENOENT; 24718 return (err); 24719 } 24720 24721 /* 24722 * Process the TCP_IOC_ABORT_CONN ioctl request. 24723 */ 24724 static void 24725 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 24726 { 24727 int err; 24728 IOCP iocp; 24729 MBLKP mp1; 24730 sa_family_t laf, raf; 24731 tcp_ioc_abort_conn_t *acp; 24732 zone_t *zptr; 24733 zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid; 24734 24735 iocp = (IOCP)mp->b_rptr; 24736 24737 if ((mp1 = mp->b_cont) == NULL || 24738 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 24739 err = EINVAL; 24740 goto out; 24741 } 24742 24743 /* check permissions */ 24744 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 24745 err = EPERM; 24746 goto out; 24747 } 24748 24749 if (mp1->b_cont != NULL) { 24750 freemsg(mp1->b_cont); 24751 mp1->b_cont = NULL; 24752 } 24753 24754 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 24755 laf = acp->ac_local.ss_family; 24756 raf = acp->ac_remote.ss_family; 24757 24758 /* check that a zone with the supplied zoneid exists */ 24759 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 24760 zptr = zone_find_by_id(zoneid); 24761 if (zptr != NULL) { 24762 zone_rele(zptr); 24763 } else { 24764 err = EINVAL; 24765 goto out; 24766 } 24767 } 24768 24769 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24770 acp->ac_start > acp->ac_end || laf != raf || 24771 (laf != AF_INET && laf != AF_INET6)) { 24772 err = EINVAL; 24773 goto out; 24774 } 24775 24776 tcp_ioctl_abort_dump(acp); 24777 err = tcp_ioctl_abort(acp); 24778 24779 out: 24780 if (mp1 != NULL) { 24781 freemsg(mp1); 24782 mp->b_cont = NULL; 24783 } 24784 24785 if (err != 0) 24786 miocnak(q, mp, 0, err); 24787 else 24788 miocack(q, mp, 0, 0); 24789 } 24790 24791 /* 24792 * tcp_time_wait_processing() handles processing of incoming packets when 24793 * the tcp is in the TIME_WAIT state. 24794 * A TIME_WAIT tcp that has an associated open TCP stream is never put 24795 * on the time wait list. 24796 */ 24797 void 24798 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 24799 uint32_t seg_ack, int seg_len, tcph_t *tcph) 24800 { 24801 int32_t bytes_acked; 24802 int32_t gap; 24803 int32_t rgap; 24804 tcp_opt_t tcpopt; 24805 uint_t flags; 24806 uint32_t new_swnd = 0; 24807 conn_t *connp; 24808 24809 BUMP_LOCAL(tcp->tcp_ibsegs); 24810 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 24811 24812 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 24813 new_swnd = BE16_TO_U16(tcph->th_win) << 24814 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 24815 if (tcp->tcp_snd_ts_ok) { 24816 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 24817 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24818 tcp->tcp_rnxt, TH_ACK); 24819 goto done; 24820 } 24821 } 24822 gap = seg_seq - tcp->tcp_rnxt; 24823 rgap = tcp->tcp_rwnd - (gap + seg_len); 24824 if (gap < 0) { 24825 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 24826 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, 24827 (seg_len > -gap ? -gap : seg_len)); 24828 seg_len += gap; 24829 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 24830 if (flags & TH_RST) { 24831 goto done; 24832 } 24833 if ((flags & TH_FIN) && seg_len == -1) { 24834 /* 24835 * When TCP receives a duplicate FIN in 24836 * TIME_WAIT state, restart the 2 MSL timer. 24837 * See page 73 in RFC 793. Make sure this TCP 24838 * is already on the TIME_WAIT list. If not, 24839 * just restart the timer. 24840 */ 24841 if (TCP_IS_DETACHED(tcp)) { 24842 if (tcp_time_wait_remove(tcp, NULL) == 24843 B_TRUE) { 24844 tcp_time_wait_append(tcp); 24845 TCP_DBGSTAT(tcp_rput_time_wait); 24846 } 24847 } else { 24848 ASSERT(tcp != NULL); 24849 TCP_TIMER_RESTART(tcp, 24850 tcp_time_wait_interval); 24851 } 24852 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24853 tcp->tcp_rnxt, TH_ACK); 24854 goto done; 24855 } 24856 flags |= TH_ACK_NEEDED; 24857 seg_len = 0; 24858 goto process_ack; 24859 } 24860 24861 /* Fix seg_seq, and chew the gap off the front. */ 24862 seg_seq = tcp->tcp_rnxt; 24863 } 24864 24865 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 24866 /* 24867 * Make sure that when we accept the connection, pick 24868 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 24869 * old connection. 24870 * 24871 * The next ISS generated is equal to tcp_iss_incr_extra 24872 * + ISS_INCR/2 + other components depending on the 24873 * value of tcp_strong_iss. We pre-calculate the new 24874 * ISS here and compare with tcp_snxt to determine if 24875 * we need to make adjustment to tcp_iss_incr_extra. 24876 * 24877 * The above calculation is ugly and is a 24878 * waste of CPU cycles... 24879 */ 24880 uint32_t new_iss = tcp_iss_incr_extra; 24881 int32_t adj; 24882 24883 switch (tcp_strong_iss) { 24884 case 2: { 24885 /* Add time and MD5 components. */ 24886 uint32_t answer[4]; 24887 struct { 24888 uint32_t ports; 24889 in6_addr_t src; 24890 in6_addr_t dst; 24891 } arg; 24892 MD5_CTX context; 24893 24894 mutex_enter(&tcp_iss_key_lock); 24895 context = tcp_iss_key; 24896 mutex_exit(&tcp_iss_key_lock); 24897 arg.ports = tcp->tcp_ports; 24898 /* We use MAPPED addresses in tcp_iss_init */ 24899 arg.src = tcp->tcp_ip_src_v6; 24900 if (tcp->tcp_ipversion == IPV4_VERSION) { 24901 IN6_IPADDR_TO_V4MAPPED( 24902 tcp->tcp_ipha->ipha_dst, 24903 &arg.dst); 24904 } else { 24905 arg.dst = 24906 tcp->tcp_ip6h->ip6_dst; 24907 } 24908 MD5Update(&context, (uchar_t *)&arg, 24909 sizeof (arg)); 24910 MD5Final((uchar_t *)answer, &context); 24911 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 24912 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 24913 break; 24914 } 24915 case 1: 24916 /* Add time component and min random (i.e. 1). */ 24917 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 24918 break; 24919 default: 24920 /* Add only time component. */ 24921 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24922 break; 24923 } 24924 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 24925 /* 24926 * New ISS not guaranteed to be ISS_INCR/2 24927 * ahead of the current tcp_snxt, so add the 24928 * difference to tcp_iss_incr_extra. 24929 */ 24930 tcp_iss_incr_extra += adj; 24931 } 24932 /* 24933 * If tcp_clean_death() can not perform the task now, 24934 * drop the SYN packet and let the other side re-xmit. 24935 * Otherwise pass the SYN packet back in, since the 24936 * old tcp state has been cleaned up or freed. 24937 */ 24938 if (tcp_clean_death(tcp, 0, 27) == -1) 24939 goto done; 24940 /* 24941 * We will come back to tcp_rput_data 24942 * on the global queue. Packets destined 24943 * for the global queue will be checked 24944 * with global policy. But the policy for 24945 * this packet has already been checked as 24946 * this was destined for the detached 24947 * connection. We need to bypass policy 24948 * check this time by attaching a dummy 24949 * ipsec_in with ipsec_in_dont_check set. 24950 */ 24951 if ((connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid)) != 24952 NULL) { 24953 TCP_STAT(tcp_time_wait_syn_success); 24954 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 24955 return; 24956 } 24957 goto done; 24958 } 24959 24960 /* 24961 * rgap is the amount of stuff received out of window. A negative 24962 * value is the amount out of window. 24963 */ 24964 if (rgap < 0) { 24965 BUMP_MIB(&tcp_mib, tcpInDataPastWinSegs); 24966 UPDATE_MIB(&tcp_mib, tcpInDataPastWinBytes, -rgap); 24967 /* Fix seg_len and make sure there is something left. */ 24968 seg_len += rgap; 24969 if (seg_len <= 0) { 24970 if (flags & TH_RST) { 24971 goto done; 24972 } 24973 flags |= TH_ACK_NEEDED; 24974 seg_len = 0; 24975 goto process_ack; 24976 } 24977 } 24978 /* 24979 * Check whether we can update tcp_ts_recent. This test is 24980 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 24981 * Extensions for High Performance: An Update", Internet Draft. 24982 */ 24983 if (tcp->tcp_snd_ts_ok && 24984 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 24985 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 24986 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 24987 tcp->tcp_last_rcv_lbolt = lbolt64; 24988 } 24989 24990 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 24991 /* Always ack out of order packets */ 24992 flags |= TH_ACK_NEEDED; 24993 seg_len = 0; 24994 } else if (seg_len > 0) { 24995 BUMP_MIB(&tcp_mib, tcpInClosed); 24996 BUMP_MIB(&tcp_mib, tcpInDataInorderSegs); 24997 UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, seg_len); 24998 } 24999 if (flags & TH_RST) { 25000 (void) tcp_clean_death(tcp, 0, 28); 25001 goto done; 25002 } 25003 if (flags & TH_SYN) { 25004 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25005 TH_RST|TH_ACK); 25006 /* 25007 * Do not delete the TCP structure if it is in 25008 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25009 */ 25010 goto done; 25011 } 25012 process_ack: 25013 if (flags & TH_ACK) { 25014 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25015 if (bytes_acked <= 0) { 25016 if (bytes_acked == 0 && seg_len == 0 && 25017 new_swnd == tcp->tcp_swnd) 25018 BUMP_MIB(&tcp_mib, tcpInDupAck); 25019 } else { 25020 /* Acks something not sent */ 25021 flags |= TH_ACK_NEEDED; 25022 } 25023 } 25024 if (flags & TH_ACK_NEEDED) { 25025 /* 25026 * Time to send an ack for some reason. 25027 */ 25028 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25029 tcp->tcp_rnxt, TH_ACK); 25030 } 25031 done: 25032 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25033 DB_CKSUMSTART(mp) = 0; 25034 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25035 TCP_STAT(tcp_time_wait_syn_fail); 25036 } 25037 freemsg(mp); 25038 } 25039 25040 /* 25041 * Allocate a T_SVR4_OPTMGMT_REQ. 25042 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 25043 * that tcp_rput_other can drop the acks. 25044 */ 25045 static mblk_t * 25046 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 25047 { 25048 mblk_t *mp; 25049 struct T_optmgmt_req *tor; 25050 struct opthdr *oh; 25051 uint_t size; 25052 char *optptr; 25053 25054 size = sizeof (*tor) + sizeof (*oh) + optlen; 25055 mp = allocb(size, BPRI_MED); 25056 if (mp == NULL) 25057 return (NULL); 25058 25059 mp->b_wptr += size; 25060 mp->b_datap->db_type = M_PROTO; 25061 tor = (struct T_optmgmt_req *)mp->b_rptr; 25062 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 25063 tor->MGMT_flags = T_NEGOTIATE; 25064 tor->OPT_length = sizeof (*oh) + optlen; 25065 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 25066 25067 oh = (struct opthdr *)&tor[1]; 25068 oh->level = level; 25069 oh->name = cmd; 25070 oh->len = optlen; 25071 if (optlen != 0) { 25072 optptr = (char *)&oh[1]; 25073 bcopy(opt, optptr, optlen); 25074 } 25075 return (mp); 25076 } 25077 25078 /* 25079 * TCP Timers Implementation. 25080 */ 25081 timeout_id_t 25082 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25083 { 25084 mblk_t *mp; 25085 tcp_timer_t *tcpt; 25086 tcp_t *tcp = connp->conn_tcp; 25087 25088 ASSERT(connp->conn_sqp != NULL); 25089 25090 TCP_DBGSTAT(tcp_timeout_calls); 25091 25092 if (tcp->tcp_timercache == NULL) { 25093 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25094 } else { 25095 TCP_DBGSTAT(tcp_timeout_cached_alloc); 25096 mp = tcp->tcp_timercache; 25097 tcp->tcp_timercache = mp->b_next; 25098 mp->b_next = NULL; 25099 ASSERT(mp->b_wptr == NULL); 25100 } 25101 25102 CONN_INC_REF(connp); 25103 tcpt = (tcp_timer_t *)mp->b_rptr; 25104 tcpt->connp = connp; 25105 tcpt->tcpt_proc = f; 25106 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 25107 return ((timeout_id_t)mp); 25108 } 25109 25110 static void 25111 tcp_timer_callback(void *arg) 25112 { 25113 mblk_t *mp = (mblk_t *)arg; 25114 tcp_timer_t *tcpt; 25115 conn_t *connp; 25116 25117 tcpt = (tcp_timer_t *)mp->b_rptr; 25118 connp = tcpt->connp; 25119 squeue_fill(connp->conn_sqp, mp, 25120 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 25121 } 25122 25123 static void 25124 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25125 { 25126 tcp_timer_t *tcpt; 25127 conn_t *connp = (conn_t *)arg; 25128 tcp_t *tcp = connp->conn_tcp; 25129 25130 tcpt = (tcp_timer_t *)mp->b_rptr; 25131 ASSERT(connp == tcpt->connp); 25132 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25133 25134 /* 25135 * If the TCP has reached the closed state, don't proceed any 25136 * further. This TCP logically does not exist on the system. 25137 * tcpt_proc could for example access queues, that have already 25138 * been qprocoff'ed off. Also see comments at the start of tcp_input 25139 */ 25140 if (tcp->tcp_state != TCPS_CLOSED) { 25141 (*tcpt->tcpt_proc)(connp); 25142 } else { 25143 tcp->tcp_timer_tid = 0; 25144 } 25145 tcp_timer_free(connp->conn_tcp, mp); 25146 } 25147 25148 /* 25149 * There is potential race with untimeout and the handler firing at the same 25150 * time. The mblock may be freed by the handler while we are trying to use 25151 * it. But since both should execute on the same squeue, this race should not 25152 * occur. 25153 */ 25154 clock_t 25155 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25156 { 25157 mblk_t *mp = (mblk_t *)id; 25158 tcp_timer_t *tcpt; 25159 clock_t delta; 25160 25161 TCP_DBGSTAT(tcp_timeout_cancel_reqs); 25162 25163 if (mp == NULL) 25164 return (-1); 25165 25166 tcpt = (tcp_timer_t *)mp->b_rptr; 25167 ASSERT(tcpt->connp == connp); 25168 25169 delta = untimeout(tcpt->tcpt_tid); 25170 25171 if (delta >= 0) { 25172 TCP_DBGSTAT(tcp_timeout_canceled); 25173 tcp_timer_free(connp->conn_tcp, mp); 25174 CONN_DEC_REF(connp); 25175 } 25176 25177 return (delta); 25178 } 25179 25180 /* 25181 * Allocate space for the timer event. The allocation looks like mblk, but it is 25182 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 25183 * 25184 * Dealing with failures: If we can't allocate from the timer cache we try 25185 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 25186 * points to b_rptr. 25187 * If we can't allocate anything using allocb_tryhard(), we perform a last 25188 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 25189 * save the actual allocation size in b_datap. 25190 */ 25191 mblk_t * 25192 tcp_timermp_alloc(int kmflags) 25193 { 25194 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 25195 kmflags & ~KM_PANIC); 25196 25197 if (mp != NULL) { 25198 mp->b_next = mp->b_prev = NULL; 25199 mp->b_rptr = (uchar_t *)(&mp[1]); 25200 mp->b_wptr = NULL; 25201 mp->b_datap = NULL; 25202 mp->b_queue = NULL; 25203 } else if (kmflags & KM_PANIC) { 25204 /* 25205 * Failed to allocate memory for the timer. Try allocating from 25206 * dblock caches. 25207 */ 25208 TCP_STAT(tcp_timermp_allocfail); 25209 mp = allocb_tryhard(sizeof (tcp_timer_t)); 25210 if (mp == NULL) { 25211 size_t size = 0; 25212 /* 25213 * Memory is really low. Try tryhard allocation. 25214 */ 25215 TCP_STAT(tcp_timermp_allocdblfail); 25216 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 25217 sizeof (tcp_timer_t), &size, kmflags); 25218 mp->b_rptr = (uchar_t *)(&mp[1]); 25219 mp->b_next = mp->b_prev = NULL; 25220 mp->b_wptr = (uchar_t *)-1; 25221 mp->b_datap = (dblk_t *)size; 25222 mp->b_queue = NULL; 25223 } 25224 ASSERT(mp->b_wptr != NULL); 25225 } 25226 TCP_DBGSTAT(tcp_timermp_alloced); 25227 25228 return (mp); 25229 } 25230 25231 /* 25232 * Free per-tcp timer cache. 25233 * It can only contain entries from tcp_timercache. 25234 */ 25235 void 25236 tcp_timermp_free(tcp_t *tcp) 25237 { 25238 mblk_t *mp; 25239 25240 while ((mp = tcp->tcp_timercache) != NULL) { 25241 ASSERT(mp->b_wptr == NULL); 25242 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 25243 kmem_cache_free(tcp_timercache, mp); 25244 } 25245 } 25246 25247 /* 25248 * Free timer event. Put it on the per-tcp timer cache if there is not too many 25249 * events there already (currently at most two events are cached). 25250 * If the event is not allocated from the timer cache, free it right away. 25251 */ 25252 static void 25253 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 25254 { 25255 mblk_t *mp1 = tcp->tcp_timercache; 25256 25257 if (mp->b_wptr != NULL) { 25258 /* 25259 * This allocation is not from a timer cache, free it right 25260 * away. 25261 */ 25262 if (mp->b_wptr != (uchar_t *)-1) 25263 freeb(mp); 25264 else 25265 kmem_free(mp, (size_t)mp->b_datap); 25266 } else if (mp1 == NULL || mp1->b_next == NULL) { 25267 /* Cache this timer block for future allocations */ 25268 mp->b_rptr = (uchar_t *)(&mp[1]); 25269 mp->b_next = mp1; 25270 tcp->tcp_timercache = mp; 25271 } else { 25272 kmem_cache_free(tcp_timercache, mp); 25273 TCP_DBGSTAT(tcp_timermp_freed); 25274 } 25275 } 25276 25277 /* 25278 * End of TCP Timers implementation. 25279 */ 25280 25281 /* 25282 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 25283 * on the specified backing STREAMS q. Note, the caller may make the 25284 * decision to call based on the tcp_t.tcp_flow_stopped value which 25285 * when check outside the q's lock is only an advisory check ... 25286 */ 25287 25288 void 25289 tcp_setqfull(tcp_t *tcp) 25290 { 25291 queue_t *q = tcp->tcp_wq; 25292 25293 if (!(q->q_flag & QFULL)) { 25294 mutex_enter(QLOCK(q)); 25295 if (!(q->q_flag & QFULL)) { 25296 /* still need to set QFULL */ 25297 q->q_flag |= QFULL; 25298 tcp->tcp_flow_stopped = B_TRUE; 25299 mutex_exit(QLOCK(q)); 25300 TCP_STAT(tcp_flwctl_on); 25301 } else { 25302 mutex_exit(QLOCK(q)); 25303 } 25304 } 25305 } 25306 25307 void 25308 tcp_clrqfull(tcp_t *tcp) 25309 { 25310 queue_t *q = tcp->tcp_wq; 25311 25312 if (q->q_flag & QFULL) { 25313 mutex_enter(QLOCK(q)); 25314 if (q->q_flag & QFULL) { 25315 q->q_flag &= ~QFULL; 25316 tcp->tcp_flow_stopped = B_FALSE; 25317 mutex_exit(QLOCK(q)); 25318 if (q->q_flag & QWANTW) 25319 qbackenable(q, 0); 25320 } else { 25321 mutex_exit(QLOCK(q)); 25322 } 25323 } 25324 } 25325 25326 /* 25327 * TCP Kstats implementation 25328 */ 25329 static void 25330 tcp_kstat_init(void) 25331 { 25332 tcp_named_kstat_t template = { 25333 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 25334 { "rtoMin", KSTAT_DATA_INT32, 0 }, 25335 { "rtoMax", KSTAT_DATA_INT32, 0 }, 25336 { "maxConn", KSTAT_DATA_INT32, 0 }, 25337 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 25338 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 25339 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 25340 { "estabResets", KSTAT_DATA_UINT32, 0 }, 25341 { "currEstab", KSTAT_DATA_UINT32, 0 }, 25342 { "inSegs", KSTAT_DATA_UINT32, 0 }, 25343 { "outSegs", KSTAT_DATA_UINT32, 0 }, 25344 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 25345 { "connTableSize", KSTAT_DATA_INT32, 0 }, 25346 { "outRsts", KSTAT_DATA_UINT32, 0 }, 25347 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 25348 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 25349 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 25350 { "outAck", KSTAT_DATA_UINT32, 0 }, 25351 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 25352 { "outUrg", KSTAT_DATA_UINT32, 0 }, 25353 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 25354 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 25355 { "outControl", KSTAT_DATA_UINT32, 0 }, 25356 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 25357 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 25358 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 25359 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 25360 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 25361 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 25362 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 25363 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 25364 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 25365 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 25366 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 25367 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 25368 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 25369 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 25370 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 25371 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 25372 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 25373 { "inClosed", KSTAT_DATA_UINT32, 0 }, 25374 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 25375 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 25376 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 25377 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 25378 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 25379 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 25380 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 25381 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 25382 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 25383 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 25384 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 25385 { "connTableSize6", KSTAT_DATA_INT32, 0 } 25386 }; 25387 25388 tcp_mibkp = kstat_create(TCP_MOD_NAME, 0, TCP_MOD_NAME, 25389 "mib2", KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0); 25390 25391 if (tcp_mibkp == NULL) 25392 return; 25393 25394 template.rtoAlgorithm.value.ui32 = 4; 25395 template.rtoMin.value.ui32 = tcp_rexmit_interval_min; 25396 template.rtoMax.value.ui32 = tcp_rexmit_interval_max; 25397 template.maxConn.value.i32 = -1; 25398 25399 bcopy(&template, tcp_mibkp->ks_data, sizeof (template)); 25400 25401 tcp_mibkp->ks_update = tcp_kstat_update; 25402 25403 kstat_install(tcp_mibkp); 25404 } 25405 25406 static void 25407 tcp_kstat_fini(void) 25408 { 25409 25410 if (tcp_mibkp != NULL) { 25411 kstat_delete(tcp_mibkp); 25412 tcp_mibkp = NULL; 25413 } 25414 } 25415 25416 static int 25417 tcp_kstat_update(kstat_t *kp, int rw) 25418 { 25419 tcp_named_kstat_t *tcpkp; 25420 tcp_t *tcp; 25421 connf_t *connfp; 25422 conn_t *connp; 25423 int i; 25424 25425 if (!kp || !kp->ks_data) 25426 return (EIO); 25427 25428 if (rw == KSTAT_WRITE) 25429 return (EACCES); 25430 25431 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 25432 25433 tcpkp->currEstab.value.ui32 = 0; 25434 25435 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25436 connfp = &ipcl_globalhash_fanout[i]; 25437 connp = NULL; 25438 while ((connp = 25439 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25440 tcp = connp->conn_tcp; 25441 switch (tcp_snmp_state(tcp)) { 25442 case MIB2_TCP_established: 25443 case MIB2_TCP_closeWait: 25444 tcpkp->currEstab.value.ui32++; 25445 break; 25446 } 25447 } 25448 } 25449 25450 tcpkp->activeOpens.value.ui32 = tcp_mib.tcpActiveOpens; 25451 tcpkp->passiveOpens.value.ui32 = tcp_mib.tcpPassiveOpens; 25452 tcpkp->attemptFails.value.ui32 = tcp_mib.tcpAttemptFails; 25453 tcpkp->estabResets.value.ui32 = tcp_mib.tcpEstabResets; 25454 tcpkp->inSegs.value.ui32 = tcp_mib.tcpInSegs; 25455 tcpkp->outSegs.value.ui32 = tcp_mib.tcpOutSegs; 25456 tcpkp->retransSegs.value.ui32 = tcp_mib.tcpRetransSegs; 25457 tcpkp->connTableSize.value.i32 = tcp_mib.tcpConnTableSize; 25458 tcpkp->outRsts.value.ui32 = tcp_mib.tcpOutRsts; 25459 tcpkp->outDataSegs.value.ui32 = tcp_mib.tcpOutDataSegs; 25460 tcpkp->outDataBytes.value.ui32 = tcp_mib.tcpOutDataBytes; 25461 tcpkp->retransBytes.value.ui32 = tcp_mib.tcpRetransBytes; 25462 tcpkp->outAck.value.ui32 = tcp_mib.tcpOutAck; 25463 tcpkp->outAckDelayed.value.ui32 = tcp_mib.tcpOutAckDelayed; 25464 tcpkp->outUrg.value.ui32 = tcp_mib.tcpOutUrg; 25465 tcpkp->outWinUpdate.value.ui32 = tcp_mib.tcpOutWinUpdate; 25466 tcpkp->outWinProbe.value.ui32 = tcp_mib.tcpOutWinProbe; 25467 tcpkp->outControl.value.ui32 = tcp_mib.tcpOutControl; 25468 tcpkp->outFastRetrans.value.ui32 = tcp_mib.tcpOutFastRetrans; 25469 tcpkp->inAckSegs.value.ui32 = tcp_mib.tcpInAckSegs; 25470 tcpkp->inAckBytes.value.ui32 = tcp_mib.tcpInAckBytes; 25471 tcpkp->inDupAck.value.ui32 = tcp_mib.tcpInDupAck; 25472 tcpkp->inAckUnsent.value.ui32 = tcp_mib.tcpInAckUnsent; 25473 tcpkp->inDataInorderSegs.value.ui32 = tcp_mib.tcpInDataInorderSegs; 25474 tcpkp->inDataInorderBytes.value.ui32 = tcp_mib.tcpInDataInorderBytes; 25475 tcpkp->inDataUnorderSegs.value.ui32 = tcp_mib.tcpInDataUnorderSegs; 25476 tcpkp->inDataUnorderBytes.value.ui32 = tcp_mib.tcpInDataUnorderBytes; 25477 tcpkp->inDataDupSegs.value.ui32 = tcp_mib.tcpInDataDupSegs; 25478 tcpkp->inDataDupBytes.value.ui32 = tcp_mib.tcpInDataDupBytes; 25479 tcpkp->inDataPartDupSegs.value.ui32 = tcp_mib.tcpInDataPartDupSegs; 25480 tcpkp->inDataPartDupBytes.value.ui32 = tcp_mib.tcpInDataPartDupBytes; 25481 tcpkp->inDataPastWinSegs.value.ui32 = tcp_mib.tcpInDataPastWinSegs; 25482 tcpkp->inDataPastWinBytes.value.ui32 = tcp_mib.tcpInDataPastWinBytes; 25483 tcpkp->inWinProbe.value.ui32 = tcp_mib.tcpInWinProbe; 25484 tcpkp->inWinUpdate.value.ui32 = tcp_mib.tcpInWinUpdate; 25485 tcpkp->inClosed.value.ui32 = tcp_mib.tcpInClosed; 25486 tcpkp->rttNoUpdate.value.ui32 = tcp_mib.tcpRttNoUpdate; 25487 tcpkp->rttUpdate.value.ui32 = tcp_mib.tcpRttUpdate; 25488 tcpkp->timRetrans.value.ui32 = tcp_mib.tcpTimRetrans; 25489 tcpkp->timRetransDrop.value.ui32 = tcp_mib.tcpTimRetransDrop; 25490 tcpkp->timKeepalive.value.ui32 = tcp_mib.tcpTimKeepalive; 25491 tcpkp->timKeepaliveProbe.value.ui32 = tcp_mib.tcpTimKeepaliveProbe; 25492 tcpkp->timKeepaliveDrop.value.ui32 = tcp_mib.tcpTimKeepaliveDrop; 25493 tcpkp->listenDrop.value.ui32 = tcp_mib.tcpListenDrop; 25494 tcpkp->listenDropQ0.value.ui32 = tcp_mib.tcpListenDropQ0; 25495 tcpkp->halfOpenDrop.value.ui32 = tcp_mib.tcpHalfOpenDrop; 25496 tcpkp->outSackRetransSegs.value.ui32 = tcp_mib.tcpOutSackRetransSegs; 25497 tcpkp->connTableSize6.value.i32 = tcp_mib.tcp6ConnTableSize; 25498 25499 return (0); 25500 } 25501 25502 void 25503 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25504 { 25505 uint16_t hdr_len; 25506 ipha_t *ipha; 25507 uint8_t *nexthdrp; 25508 tcph_t *tcph; 25509 25510 /* Already has an eager */ 25511 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25512 TCP_STAT(tcp_reinput_syn); 25513 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 25514 connp, SQTAG_TCP_REINPUT_EAGER); 25515 return; 25516 } 25517 25518 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25519 case IPV4_VERSION: 25520 ipha = (ipha_t *)mp->b_rptr; 25521 hdr_len = IPH_HDR_LENGTH(ipha); 25522 break; 25523 case IPV6_VERSION: 25524 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25525 &hdr_len, &nexthdrp)) { 25526 CONN_DEC_REF(connp); 25527 freemsg(mp); 25528 return; 25529 } 25530 break; 25531 } 25532 25533 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25534 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25535 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25536 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25537 } 25538 25539 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 25540 SQTAG_TCP_REINPUT); 25541 } 25542 25543 static squeue_func_t 25544 tcp_squeue_switch(int val) 25545 { 25546 squeue_func_t rval = squeue_fill; 25547 25548 switch (val) { 25549 case 1: 25550 rval = squeue_enter_nodrain; 25551 break; 25552 case 2: 25553 rval = squeue_enter; 25554 break; 25555 default: 25556 break; 25557 } 25558 return (rval); 25559 } 25560 25561 static void 25562 tcp_squeue_add(squeue_t *sqp) 25563 { 25564 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25565 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25566 25567 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25568 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 25569 sqp, TCP_TIME_WAIT_DELAY); 25570 if (tcp_free_list_max_cnt == 0) { 25571 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25572 max_ncpus : boot_max_ncpus); 25573 25574 /* 25575 * Limit number of entries to 1% of availble memory / tcp_ncpus 25576 */ 25577 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25578 (tcp_ncpus * sizeof (tcp_t) * 100); 25579 } 25580 tcp_time_wait->tcp_free_list_cnt = 0; 25581 } 25582