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/vtrace.h> 49 #include <sys/kmem.h> 50 #include <sys/ethernet.h> 51 #include <sys/cpuvar.h> 52 #include <sys/dlpi.h> 53 #include <sys/multidata.h> 54 #include <sys/multidata_impl.h> 55 #include <sys/pattr.h> 56 #include <sys/policy.h> 57 #include <sys/priv.h> 58 #include <sys/zone.h> 59 60 #include <sys/errno.h> 61 #include <sys/signal.h> 62 #include <sys/socket.h> 63 #include <sys/sockio.h> 64 #include <sys/isa_defs.h> 65 #include <sys/md5.h> 66 #include <sys/random.h> 67 #include <netinet/in.h> 68 #include <netinet/tcp.h> 69 #include <netinet/ip6.h> 70 #include <netinet/icmp6.h> 71 #include <net/if.h> 72 #include <net/route.h> 73 #include <inet/ipsec_impl.h> 74 75 #include <inet/common.h> 76 #include <inet/ip.h> 77 #include <inet/ip_impl.h> 78 #include <inet/ip6.h> 79 #include <inet/ip_ndp.h> 80 #include <inet/mi.h> 81 #include <inet/mib2.h> 82 #include <inet/nd.h> 83 #include <inet/optcom.h> 84 #include <inet/snmpcom.h> 85 #include <inet/kstatcom.h> 86 #include <inet/tcp.h> 87 #include <inet/tcp_impl.h> 88 #include <net/pfkeyv2.h> 89 #include <inet/ipsec_info.h> 90 #include <inet/ipdrop.h> 91 #include <inet/tcp_trace.h> 92 93 #include <inet/ipclassifier.h> 94 #include <inet/ip_ire.h> 95 #include <inet/ip_if.h> 96 #include <inet/ipp_common.h> 97 #include <sys/squeue.h> 98 #include <inet/kssl/ksslapi.h> 99 #include <sys/tsol/label.h> 100 #include <sys/tsol/tnet.h> 101 #include <sys/sdt.h> 102 #include <rpc/pmap_prot.h> 103 104 /* 105 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 106 * 107 * (Read the detailed design doc in PSARC case directory) 108 * 109 * The entire tcp state is contained in tcp_t and conn_t structure 110 * which are allocated in tandem using ipcl_conn_create() and passing 111 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 112 * the references on the tcp_t. The tcp_t structure is never compressed 113 * and packets always land on the correct TCP perimeter from the time 114 * eager is created till the time tcp_t dies (as such the old mentat 115 * TCP global queue is not used for detached state and no IPSEC checking 116 * is required). The global queue is still allocated to send out resets 117 * for connection which have no listeners and IP directly calls 118 * tcp_xmit_listeners_reset() which does any policy check. 119 * 120 * Protection and Synchronisation mechanism: 121 * 122 * The tcp data structure does not use any kind of lock for protecting 123 * its state but instead uses 'squeues' for mutual exclusion from various 124 * read and write side threads. To access a tcp member, the thread should 125 * always be behind squeue (via squeue_enter, squeue_enter_nodrain, or 126 * squeue_fill). Since the squeues allow a direct function call, caller 127 * can pass any tcp function having prototype of edesc_t as argument 128 * (different from traditional STREAMs model where packets come in only 129 * designated entry points). The list of functions that can be directly 130 * called via squeue are listed before the usual function prototype. 131 * 132 * Referencing: 133 * 134 * TCP is MT-Hot and we use a reference based scheme to make sure that the 135 * tcp structure doesn't disappear when its needed. When the application 136 * creates an outgoing connection or accepts an incoming connection, we 137 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 138 * The IP reference is just a symbolic reference since ip_tcpclose() 139 * looks at tcp structure after tcp_close_output() returns which could 140 * have dropped the last TCP reference. So as long as the connection is 141 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 142 * conn_t. The classifier puts its own reference when the connection is 143 * inserted in listen or connected hash. Anytime a thread needs to enter 144 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 145 * on write side or by doing a classify on read side and then puts a 146 * reference on the conn before doing squeue_enter/tryenter/fill. For 147 * read side, the classifier itself puts the reference under fanout lock 148 * to make sure that tcp can't disappear before it gets processed. The 149 * squeue will drop this reference automatically so the called function 150 * doesn't have to do a DEC_REF. 151 * 152 * Opening a new connection: 153 * 154 * The outgoing connection open is pretty simple. ip_tcpopen() does the 155 * work in creating the conn/tcp structure and initializing it. The 156 * squeue assignment is done based on the CPU the application 157 * is running on. So for outbound connections, processing is always done 158 * on application CPU which might be different from the incoming CPU 159 * being interrupted by the NIC. An optimal way would be to figure out 160 * the NIC <-> CPU binding at listen time, and assign the outgoing 161 * connection to the squeue attached to the CPU that will be interrupted 162 * for incoming packets (we know the NIC based on the bind IP address). 163 * This might seem like a problem if more data is going out but the 164 * fact is that in most cases the transmit is ACK driven transmit where 165 * the outgoing data normally sits on TCP's xmit queue waiting to be 166 * transmitted. 167 * 168 * Accepting a connection: 169 * 170 * This is a more interesting case because of various races involved in 171 * establishing a eager in its own perimeter. Read the meta comment on 172 * top of tcp_conn_request(). But briefly, the squeue is picked by 173 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 174 * 175 * Closing a connection: 176 * 177 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 178 * via squeue to do the close and mark the tcp as detached if the connection 179 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 180 * reference but tcp_close() drop IP's reference always. So if tcp was 181 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 182 * and 1 because it is in classifier's connected hash. This is the condition 183 * we use to determine that its OK to clean up the tcp outside of squeue 184 * when time wait expires (check the ref under fanout and conn_lock and 185 * if it is 2, remove it from fanout hash and kill it). 186 * 187 * Although close just drops the necessary references and marks the 188 * tcp_detached state, tcp_close needs to know the tcp_detached has been 189 * set (under squeue) before letting the STREAM go away (because a 190 * inbound packet might attempt to go up the STREAM while the close 191 * has happened and tcp_detached is not set). So a special lock and 192 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 193 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 194 * tcp_detached. 195 * 196 * Special provisions and fast paths: 197 * 198 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 199 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 200 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 201 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 202 * check to send packets directly to tcp_rput_data via squeue. Everyone 203 * else comes through tcp_input() on the read side. 204 * 205 * We also make special provisions for sockfs by marking tcp_issocket 206 * whenever we have only sockfs on top of TCP. This allows us to skip 207 * putting the tcp in acceptor hash since a sockfs listener can never 208 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 209 * since eager has already been allocated and the accept now happens 210 * on acceptor STREAM. There is a big blob of comment on top of 211 * tcp_conn_request explaining the new accept. When socket is POP'd, 212 * sockfs sends us an ioctl to mark the fact and we go back to old 213 * behaviour. Once tcp_issocket is unset, its never set for the 214 * life of that connection. 215 * 216 * IPsec notes : 217 * 218 * Since a packet is always executed on the correct TCP perimeter 219 * all IPsec processing is defered to IP including checking new 220 * connections and setting IPSEC policies for new connection. The 221 * only exception is tcp_xmit_listeners_reset() which is called 222 * directly from IP and needs to policy check to see if TH_RST 223 * can be sent out. 224 */ 225 226 extern major_t TCP6_MAJ; 227 228 /* 229 * Values for squeue switch: 230 * 1: squeue_enter_nodrain 231 * 2: squeue_enter 232 * 3: squeue_fill 233 */ 234 int tcp_squeue_close = 2; 235 int tcp_squeue_wput = 2; 236 237 squeue_func_t tcp_squeue_close_proc; 238 squeue_func_t tcp_squeue_wput_proc; 239 240 /* 241 * This controls how tiny a write must be before we try to copy it 242 * into the the mblk on the tail of the transmit queue. Not much 243 * speedup is observed for values larger than sixteen. Zero will 244 * disable the optimisation. 245 */ 246 int tcp_tx_pull_len = 16; 247 248 /* 249 * TCP Statistics. 250 * 251 * How TCP statistics work. 252 * 253 * There are two types of statistics invoked by two macros. 254 * 255 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 256 * supposed to be used in non MT-hot paths of the code. 257 * 258 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 259 * supposed to be used for DEBUG purposes and may be used on a hot path. 260 * 261 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 262 * (use "kstat tcp" to get them). 263 * 264 * There is also additional debugging facility that marks tcp_clean_death() 265 * instances and saves them in tcp_t structure. It is triggered by 266 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 267 * tcp_clean_death() calls that counts the number of times each tag was hit. It 268 * is triggered by TCP_CLD_COUNTERS define. 269 * 270 * How to add new counters. 271 * 272 * 1) Add a field in the tcp_stat structure describing your counter. 273 * 2) Add a line in tcp_statistics with the name of the counter. 274 * 275 * IMPORTANT!! - make sure that both are in sync !! 276 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 277 * 278 * Please avoid using private counters which are not kstat-exported. 279 * 280 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 281 * in tcp_t structure. 282 * 283 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 284 */ 285 286 #ifndef TCP_DEBUG_COUNTER 287 #ifdef DEBUG 288 #define TCP_DEBUG_COUNTER 1 289 #else 290 #define TCP_DEBUG_COUNTER 0 291 #endif 292 #endif 293 294 #define TCP_CLD_COUNTERS 0 295 296 #define TCP_TAG_CLEAN_DEATH 1 297 #define TCP_MAX_CLEAN_DEATH_TAG 32 298 299 #ifdef lint 300 static int _lint_dummy_; 301 #endif 302 303 #if TCP_CLD_COUNTERS 304 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 305 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 306 #elif defined(lint) 307 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 308 #else 309 #define TCP_CLD_STAT(x) 310 #endif 311 312 #if TCP_DEBUG_COUNTER 313 #define TCP_DBGSTAT(x) atomic_add_64(&(tcp_statistics.x.value.ui64), 1) 314 #elif defined(lint) 315 #define TCP_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 316 #else 317 #define TCP_DBGSTAT(x) 318 #endif 319 320 tcp_stat_t tcp_statistics = { 321 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 322 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 323 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 324 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 325 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 326 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 327 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 328 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 329 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 330 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 331 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 332 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 333 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 334 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 335 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 336 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 337 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 338 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 339 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 340 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 341 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 342 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 343 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 344 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 345 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 346 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 347 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 348 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 349 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 350 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 351 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 352 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 353 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 354 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 355 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 356 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 357 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 358 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 359 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 360 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 361 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 362 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 363 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 364 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 365 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 366 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 367 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 368 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 369 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 370 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 371 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 372 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 373 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 374 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 375 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 376 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 377 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 378 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 379 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 380 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 381 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 382 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 383 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 384 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 385 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 386 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 387 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 388 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 389 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 390 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 391 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 392 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 393 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 394 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 395 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 396 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 397 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 398 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 399 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 400 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 401 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 402 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 403 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 404 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 405 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 406 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 407 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 408 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 409 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 410 }; 411 412 static kstat_t *tcp_kstat; 413 414 /* 415 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 416 * tcp write side. 417 */ 418 #define CALL_IP_WPUT(connp, q, mp) { \ 419 ASSERT(((q)->q_flag & QREADR) == 0); \ 420 TCP_DBGSTAT(tcp_ip_output); \ 421 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 422 } 423 424 /* Macros for timestamp comparisons */ 425 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 426 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 427 428 /* 429 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 430 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 431 * by adding three components: a time component which grows by 1 every 4096 432 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 433 * a per-connection component which grows by 125000 for every new connection; 434 * and an "extra" component that grows by a random amount centered 435 * approximately on 64000. This causes the the ISS generator to cycle every 436 * 4.89 hours if no TCP connections are made, and faster if connections are 437 * made. 438 * 439 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 440 * components: a time component which grows by 250000 every second; and 441 * a per-connection component which grows by 125000 for every new connections. 442 * 443 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 444 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 445 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 446 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 447 * password. 448 */ 449 #define ISS_INCR 250000 450 #define ISS_NSEC_SHT 12 451 452 static uint32_t tcp_iss_incr_extra; /* Incremented for each connection */ 453 static kmutex_t tcp_iss_key_lock; 454 static MD5_CTX tcp_iss_key; 455 static sin_t sin_null; /* Zero address for quick clears */ 456 static sin6_t sin6_null; /* Zero address for quick clears */ 457 458 /* Packet dropper for TCP IPsec policy drops. */ 459 static ipdropper_t tcp_dropper; 460 461 /* 462 * This implementation follows the 4.3BSD interpretation of the urgent 463 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 464 * incompatible changes in protocols like telnet and rlogin. 465 */ 466 #define TCP_OLD_URP_INTERPRETATION 1 467 468 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 469 (TCP_IS_DETACHED(tcp) && \ 470 (!(tcp)->tcp_hard_binding)) 471 472 /* 473 * TCP reassembly macros. We hide starting and ending sequence numbers in 474 * b_next and b_prev of messages on the reassembly queue. The messages are 475 * chained using b_cont. These macros are used in tcp_reass() so we don't 476 * have to see the ugly casts and assignments. 477 */ 478 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 479 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 480 (mblk_t *)(uintptr_t)(u)) 481 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 482 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 483 (mblk_t *)(uintptr_t)(u)) 484 485 /* 486 * Implementation of TCP Timers. 487 * ============================= 488 * 489 * INTERFACE: 490 * 491 * There are two basic functions dealing with tcp timers: 492 * 493 * timeout_id_t tcp_timeout(connp, func, time) 494 * clock_t tcp_timeout_cancel(connp, timeout_id) 495 * TCP_TIMER_RESTART(tcp, intvl) 496 * 497 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 498 * after 'time' ticks passed. The function called by timeout() must adhere to 499 * the same restrictions as a driver soft interrupt handler - it must not sleep 500 * or call other functions that might sleep. The value returned is the opaque 501 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 502 * cancel the request. The call to tcp_timeout() may fail in which case it 503 * returns zero. This is different from the timeout(9F) function which never 504 * fails. 505 * 506 * The call-back function 'func' always receives 'connp' as its single 507 * argument. It is always executed in the squeue corresponding to the tcp 508 * structure. The tcp structure is guaranteed to be present at the time the 509 * call-back is called. 510 * 511 * NOTE: The call-back function 'func' is never called if tcp is in 512 * the TCPS_CLOSED state. 513 * 514 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 515 * request. locks acquired by the call-back routine should not be held across 516 * the call to tcp_timeout_cancel() or a deadlock may result. 517 * 518 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 519 * Otherwise, it returns an integer value greater than or equal to 0. In 520 * particular, if the call-back function is already placed on the squeue, it can 521 * not be canceled. 522 * 523 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 524 * within squeue context corresponding to the tcp instance. Since the 525 * call-back is also called via the same squeue, there are no race 526 * conditions described in untimeout(9F) manual page since all calls are 527 * strictly serialized. 528 * 529 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 530 * stored in tcp_timer_tid and starts a new one using 531 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 532 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 533 * field. 534 * 535 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 536 * call-back may still be called, so it is possible tcp_timer() will be 537 * called several times. This should not be a problem since tcp_timer() 538 * should always check the tcp instance state. 539 * 540 * 541 * IMPLEMENTATION: 542 * 543 * TCP timers are implemented using three-stage process. The call to 544 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 545 * when the timer expires. The tcp_timer_callback() arranges the call of the 546 * tcp_timer_handler() function via squeue corresponding to the tcp 547 * instance. The tcp_timer_handler() calls actual requested timeout call-back 548 * and passes tcp instance as an argument to it. Information is passed between 549 * stages using the tcp_timer_t structure which contains the connp pointer, the 550 * tcp call-back to call and the timeout id returned by the timeout(9F). 551 * 552 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 553 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 554 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 555 * returns the pointer to this mblk. 556 * 557 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 558 * looks like a normal mblk without actual dblk attached to it. 559 * 560 * To optimize performance each tcp instance holds a small cache of timer 561 * mblocks. In the current implementation it caches up to two timer mblocks per 562 * tcp instance. The cache is preserved over tcp frees and is only freed when 563 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 564 * timer processing happens on a corresponding squeue, the cache manipulation 565 * does not require any locks. Experiments show that majority of timer mblocks 566 * allocations are satisfied from the tcp cache and do not involve kmem calls. 567 * 568 * The tcp_timeout() places a refhold on the connp instance which guarantees 569 * that it will be present at the time the call-back function fires. The 570 * tcp_timer_handler() drops the reference after calling the call-back, so the 571 * call-back function does not need to manipulate the references explicitly. 572 */ 573 574 typedef struct tcp_timer_s { 575 conn_t *connp; 576 void (*tcpt_proc)(void *); 577 timeout_id_t tcpt_tid; 578 } tcp_timer_t; 579 580 static kmem_cache_t *tcp_timercache; 581 kmem_cache_t *tcp_sack_info_cache; 582 kmem_cache_t *tcp_iphc_cache; 583 584 /* 585 * For scalability, we must not run a timer for every TCP connection 586 * in TIME_WAIT state. To see why, consider (for time wait interval of 587 * 4 minutes): 588 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 589 * 590 * This list is ordered by time, so you need only delete from the head 591 * until you get to entries which aren't old enough to delete yet. 592 * The list consists of only the detached TIME_WAIT connections. 593 * 594 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 595 * becomes detached TIME_WAIT (either by changing the state and already 596 * being detached or the other way around). This means that the TIME_WAIT 597 * state can be extended (up to doubled) if the connection doesn't become 598 * detached for a long time. 599 * 600 * The list manipulations (including tcp_time_wait_next/prev) 601 * are protected by the tcp_time_wait_lock. The content of the 602 * detached TIME_WAIT connections is protected by the normal perimeters. 603 */ 604 605 typedef struct tcp_squeue_priv_s { 606 kmutex_t tcp_time_wait_lock; 607 /* Protects the next 3 globals */ 608 timeout_id_t tcp_time_wait_tid; 609 tcp_t *tcp_time_wait_head; 610 tcp_t *tcp_time_wait_tail; 611 tcp_t *tcp_free_list; 612 uint_t tcp_free_list_cnt; 613 } tcp_squeue_priv_t; 614 615 /* 616 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 617 * Running it every 5 seconds seems to give the best results. 618 */ 619 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 620 621 /* 622 * To prevent memory hog, limit the number of entries in tcp_free_list 623 * to 1% of available memory / number of cpus 624 */ 625 uint_t tcp_free_list_max_cnt = 0; 626 627 #define TCP_XMIT_LOWATER 4096 628 #define TCP_XMIT_HIWATER 49152 629 #define TCP_RECV_LOWATER 2048 630 #define TCP_RECV_HIWATER 49152 631 632 /* 633 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 634 */ 635 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 636 637 #define TIDUSZ 4096 /* transport interface data unit size */ 638 639 /* 640 * Bind hash list size and has function. It has to be a power of 2 for 641 * hashing. 642 */ 643 #define TCP_BIND_FANOUT_SIZE 512 644 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 645 /* 646 * Size of listen and acceptor hash list. It has to be a power of 2 for 647 * hashing. 648 */ 649 #define TCP_FANOUT_SIZE 256 650 651 #ifdef _ILP32 652 #define TCP_ACCEPTOR_HASH(accid) \ 653 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 654 #else 655 #define TCP_ACCEPTOR_HASH(accid) \ 656 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 657 #endif /* _ILP32 */ 658 659 #define IP_ADDR_CACHE_SIZE 2048 660 #define IP_ADDR_CACHE_HASH(faddr) \ 661 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 662 663 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 664 #define TCP_HSP_HASH_SIZE 256 665 666 #define TCP_HSP_HASH(addr) \ 667 (((addr>>24) ^ (addr >>16) ^ \ 668 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 669 670 /* 671 * TCP options struct returned from tcp_parse_options. 672 */ 673 typedef struct tcp_opt_s { 674 uint32_t tcp_opt_mss; 675 uint32_t tcp_opt_wscale; 676 uint32_t tcp_opt_ts_val; 677 uint32_t tcp_opt_ts_ecr; 678 tcp_t *tcp; 679 } tcp_opt_t; 680 681 /* 682 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 683 */ 684 685 #ifdef _BIG_ENDIAN 686 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 687 (TCPOPT_TSTAMP << 8) | 10) 688 #else 689 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 690 (TCPOPT_NOP << 8) | TCPOPT_NOP) 691 #endif 692 693 /* 694 * Flags returned from tcp_parse_options. 695 */ 696 #define TCP_OPT_MSS_PRESENT 1 697 #define TCP_OPT_WSCALE_PRESENT 2 698 #define TCP_OPT_TSTAMP_PRESENT 4 699 #define TCP_OPT_SACK_OK_PRESENT 8 700 #define TCP_OPT_SACK_PRESENT 16 701 702 /* TCP option length */ 703 #define TCPOPT_NOP_LEN 1 704 #define TCPOPT_MAXSEG_LEN 4 705 #define TCPOPT_WS_LEN 3 706 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 707 #define TCPOPT_TSTAMP_LEN 10 708 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 709 #define TCPOPT_SACK_OK_LEN 2 710 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 711 #define TCPOPT_REAL_SACK_LEN 4 712 #define TCPOPT_MAX_SACK_LEN 36 713 #define TCPOPT_HEADER_LEN 2 714 715 /* TCP cwnd burst factor. */ 716 #define TCP_CWND_INFINITE 65535 717 #define TCP_CWND_SS 3 718 #define TCP_CWND_NORMAL 5 719 720 /* Maximum TCP initial cwin (start/restart). */ 721 #define TCP_MAX_INIT_CWND 8 722 723 /* 724 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 725 * either tcp_slow_start_initial or tcp_slow_start_after idle 726 * depending on the caller. If the upper layer has not used the 727 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 728 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 729 * If the upper layer has changed set the tcp_init_cwnd, just use 730 * it to calculate the tcp_cwnd. 731 */ 732 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 733 { \ 734 if ((tcp)->tcp_init_cwnd == 0) { \ 735 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 736 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 737 } else { \ 738 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 739 } \ 740 tcp->tcp_cwnd_cnt = 0; \ 741 } 742 743 /* TCP Timer control structure */ 744 typedef struct tcpt_s { 745 pfv_t tcpt_pfv; /* The routine we are to call */ 746 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 747 } tcpt_t; 748 749 /* Host Specific Parameter structure */ 750 typedef struct tcp_hsp { 751 struct tcp_hsp *tcp_hsp_next; 752 in6_addr_t tcp_hsp_addr_v6; 753 in6_addr_t tcp_hsp_subnet_v6; 754 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 755 int32_t tcp_hsp_sendspace; 756 int32_t tcp_hsp_recvspace; 757 int32_t tcp_hsp_tstamp; 758 } tcp_hsp_t; 759 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 760 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 761 762 /* 763 * Functions called directly via squeue having a prototype of edesc_t. 764 */ 765 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 766 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 767 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 768 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 769 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 770 void tcp_input(void *arg, mblk_t *mp, void *arg2); 771 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 772 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 773 void tcp_output(void *arg, mblk_t *mp, void *arg2); 774 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 775 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 776 777 778 /* Prototype for TCP functions */ 779 static void tcp_random_init(void); 780 int tcp_random(void); 781 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 782 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 783 tcp_t *eager); 784 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 785 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 786 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 787 boolean_t user_specified); 788 static void tcp_closei_local(tcp_t *tcp); 789 static void tcp_close_detached(tcp_t *tcp); 790 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 791 mblk_t *idmp, mblk_t **defermp); 792 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 793 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 794 in_port_t dstport, uint_t srcid); 795 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 796 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 797 uint32_t scope_id); 798 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 799 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 800 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 801 static char *tcp_display(tcp_t *tcp, char *, char); 802 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 803 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 804 static void tcp_eager_unlink(tcp_t *tcp); 805 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 806 int unixerr); 807 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 808 int tlierr, int unixerr); 809 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 810 cred_t *cr); 811 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 812 char *value, caddr_t cp, cred_t *cr); 813 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 814 char *value, caddr_t cp, cred_t *cr); 815 static int tcp_tpistate(tcp_t *tcp); 816 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 817 int caller_holds_lock); 818 static void tcp_bind_hash_remove(tcp_t *tcp); 819 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id); 820 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 821 static void tcp_acceptor_hash_remove(tcp_t *tcp); 822 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 823 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 824 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 825 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 826 static int tcp_header_init_ipv4(tcp_t *tcp); 827 static int tcp_header_init_ipv6(tcp_t *tcp); 828 int tcp_init(tcp_t *tcp, queue_t *q); 829 static int tcp_init_values(tcp_t *tcp); 830 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 831 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 832 t_scalar_t addr_length); 833 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 834 static void tcp_ip_notify(tcp_t *tcp); 835 static mblk_t *tcp_ire_mp(mblk_t *mp); 836 static void tcp_iss_init(tcp_t *tcp); 837 static void tcp_keepalive_killer(void *arg); 838 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 839 static void tcp_mss_set(tcp_t *tcp, uint32_t size); 840 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 841 int *do_disconnectp, int *t_errorp, int *sys_errorp); 842 static boolean_t tcp_allow_connopt_set(int level, int name); 843 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 844 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 845 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 846 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 847 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 848 mblk_t *mblk); 849 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 850 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 851 uchar_t *ptr, uint_t len); 852 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 853 static boolean_t tcp_param_register(tcpparam_t *tcppa, int cnt); 854 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 855 caddr_t cp, cred_t *cr); 856 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 857 caddr_t cp, cred_t *cr); 858 static void tcp_iss_key_init(uint8_t *phrase, int len); 859 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 860 caddr_t cp, cred_t *cr); 861 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 862 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 863 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 864 static void tcp_reinit(tcp_t *tcp); 865 static void tcp_reinit_values(tcp_t *tcp); 866 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 867 tcp_t *thisstream, cred_t *cr); 868 869 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 870 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 871 static boolean_t tcp_send_rst_chk(void); 872 static void tcp_ss_rexmit(tcp_t *tcp); 873 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 874 static void tcp_process_options(tcp_t *, tcph_t *); 875 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 876 static void tcp_rsrv(queue_t *q); 877 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 878 static int tcp_snmp_state(tcp_t *tcp); 879 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 880 cred_t *cr); 881 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 882 cred_t *cr); 883 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 884 cred_t *cr); 885 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 886 cred_t *cr); 887 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 888 cred_t *cr); 889 static int tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, 890 caddr_t cp, cred_t *cr); 891 static int tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, 892 caddr_t cp, cred_t *cr); 893 static int tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, 894 cred_t *cr); 895 static void tcp_timer(void *arg); 896 static void tcp_timer_callback(void *); 897 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 898 boolean_t random); 899 static in_port_t tcp_get_next_priv_port(const tcp_t *); 900 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 901 void tcp_wput_accept(queue_t *q, mblk_t *mp); 902 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 903 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 904 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 905 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 906 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 907 const int num_sack_blk, int *usable, uint_t *snxt, 908 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 909 const int mdt_thres); 910 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 911 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 912 const int num_sack_blk, int *usable, uint_t *snxt, 913 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 914 const int mdt_thres); 915 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 916 int num_sack_blk); 917 static void tcp_wsrv(queue_t *q); 918 static int tcp_xmit_end(tcp_t *tcp); 919 static mblk_t *tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, 920 int32_t *offset, mblk_t **end_mp, uint32_t seq, 921 boolean_t sendall, uint32_t *seg_len, boolean_t rexmit); 922 static void tcp_ack_timer(void *arg); 923 static mblk_t *tcp_ack_mp(tcp_t *tcp); 924 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 925 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len); 926 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 927 uint32_t ack, int ctl); 928 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr); 929 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr); 930 static int setmaxps(queue_t *q, int maxpsz); 931 static void tcp_set_rto(tcp_t *, time_t); 932 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 933 boolean_t, boolean_t); 934 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 935 boolean_t ipsec_mctl); 936 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 937 char *opt, int optlen); 938 static int tcp_build_hdrs(queue_t *, tcp_t *); 939 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 940 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 941 tcph_t *tcph); 942 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 943 boolean_t tcp_reserved_port_add(int, in_port_t *, in_port_t *); 944 boolean_t tcp_reserved_port_del(in_port_t, in_port_t); 945 boolean_t tcp_reserved_port_check(in_port_t); 946 static tcp_t *tcp_alloc_temp_tcp(in_port_t); 947 static int tcp_reserved_port_list(queue_t *, mblk_t *, caddr_t, cred_t *); 948 static mblk_t *tcp_mdt_info_mp(mblk_t *); 949 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 950 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 951 const boolean_t, const uint32_t, const uint32_t, 952 const uint32_t, const uint32_t); 953 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 954 const uint_t, const uint_t, boolean_t *); 955 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 956 extern mblk_t *tcp_timermp_alloc(int); 957 extern void tcp_timermp_free(tcp_t *); 958 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 959 static void tcp_stop_lingering(tcp_t *tcp); 960 static void tcp_close_linger_timeout(void *arg); 961 void tcp_ddi_init(void); 962 void tcp_ddi_destroy(void); 963 static void tcp_kstat_init(void); 964 static void tcp_kstat_fini(void); 965 static int tcp_kstat_update(kstat_t *kp, int rw); 966 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 967 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 968 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 969 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 970 tcph_t *tcph, mblk_t *idmp); 971 static squeue_func_t tcp_squeue_switch(int); 972 973 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *); 974 static int tcp_close(queue_t *, int); 975 static int tcpclose_accept(queue_t *); 976 static int tcp_modclose(queue_t *); 977 static void tcp_wput_mod(queue_t *, mblk_t *); 978 979 static void tcp_squeue_add(squeue_t *); 980 static boolean_t tcp_zcopy_check(tcp_t *); 981 static void tcp_zcopy_notify(tcp_t *); 982 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 983 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 984 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 985 986 extern void tcp_kssl_input(tcp_t *, mblk_t *); 987 988 /* 989 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 990 * 991 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 992 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 993 * (defined in tcp.h) needs to be filled in and passed into the kernel 994 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 995 * structure contains the four-tuple of a TCP connection and a range of TCP 996 * states (specified by ac_start and ac_end). The use of wildcard addresses 997 * and ports is allowed. Connections with a matching four tuple and a state 998 * within the specified range will be aborted. The valid states for the 999 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 1000 * inclusive. 1001 * 1002 * An application which has its connection aborted by this ioctl will receive 1003 * an error that is dependent on the connection state at the time of the abort. 1004 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 1005 * though a RST packet has been received. If the connection state is equal to 1006 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 1007 * and all resources associated with the connection will be freed. 1008 */ 1009 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 1010 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 1011 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 1012 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *); 1013 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 1014 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 1015 boolean_t); 1016 1017 static struct module_info tcp_rinfo = { 1018 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 1019 }; 1020 1021 static struct module_info tcp_winfo = { 1022 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 1023 }; 1024 1025 /* 1026 * Entry points for TCP as a module. It only allows SNMP requests 1027 * to pass through. 1028 */ 1029 struct qinit tcp_mod_rinit = { 1030 (pfi_t)putnext, NULL, tcp_open, ip_snmpmod_close, NULL, &tcp_rinfo, 1031 }; 1032 1033 struct qinit tcp_mod_winit = { 1034 (pfi_t)ip_snmpmod_wput, NULL, tcp_open, ip_snmpmod_close, NULL, 1035 &tcp_rinfo 1036 }; 1037 1038 /* 1039 * Entry points for TCP as a device. The normal case which supports 1040 * the TCP functionality. 1041 */ 1042 struct qinit tcp_rinit = { 1043 NULL, (pfi_t)tcp_rsrv, tcp_open, tcp_close, NULL, &tcp_rinfo 1044 }; 1045 1046 struct qinit tcp_winit = { 1047 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1048 }; 1049 1050 /* Initial entry point for TCP in socket mode. */ 1051 struct qinit tcp_sock_winit = { 1052 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1053 }; 1054 1055 /* 1056 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 1057 * an accept. Avoid allocating data structures since eager has already 1058 * been created. 1059 */ 1060 struct qinit tcp_acceptor_rinit = { 1061 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1062 }; 1063 1064 struct qinit tcp_acceptor_winit = { 1065 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1066 }; 1067 1068 /* 1069 * Entry points for TCP loopback (read side only) 1070 */ 1071 struct qinit tcp_loopback_rinit = { 1072 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_open, tcp_close, (pfi_t)0, 1073 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1074 }; 1075 1076 struct streamtab tcpinfo = { 1077 &tcp_rinit, &tcp_winit 1078 }; 1079 1080 extern squeue_func_t tcp_squeue_wput_proc; 1081 extern squeue_func_t tcp_squeue_timer_proc; 1082 1083 /* Protected by tcp_g_q_lock */ 1084 static queue_t *tcp_g_q; /* Default queue used during detached closes */ 1085 kmutex_t tcp_g_q_lock; 1086 1087 /* Protected by tcp_hsp_lock */ 1088 /* 1089 * XXX The host param mechanism should go away and instead we should use 1090 * the metrics associated with the routes to determine the default sndspace 1091 * and rcvspace. 1092 */ 1093 static tcp_hsp_t **tcp_hsp_hash; /* Hash table for HSPs */ 1094 krwlock_t tcp_hsp_lock; 1095 1096 /* 1097 * Extra privileged ports. In host byte order. 1098 * Protected by tcp_epriv_port_lock. 1099 */ 1100 #define TCP_NUM_EPRIV_PORTS 64 1101 static int tcp_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 1102 static uint16_t tcp_g_epriv_ports[TCP_NUM_EPRIV_PORTS] = { 2049, 4045 }; 1103 kmutex_t tcp_epriv_port_lock; 1104 1105 /* 1106 * The smallest anonymous port in the privileged port range which TCP 1107 * looks for free port. Use in the option TCP_ANONPRIVBIND. 1108 */ 1109 static in_port_t tcp_min_anonpriv_port = 512; 1110 1111 /* Only modified during _init and _fini thus no locking is needed. */ 1112 static caddr_t tcp_g_nd; /* Head of 'named dispatch' variable list */ 1113 1114 /* Hint not protected by any lock */ 1115 static uint_t tcp_next_port_to_try; 1116 1117 1118 /* TCP bind hash list - all tcp_t with state >= BOUND. */ 1119 tf_t tcp_bind_fanout[TCP_BIND_FANOUT_SIZE]; 1120 1121 /* TCP queue hash list - all tcp_t in case they will be an acceptor. */ 1122 static tf_t tcp_acceptor_fanout[TCP_FANOUT_SIZE]; 1123 1124 /* 1125 * TCP has a private interface for other kernel modules to reserve a 1126 * port range for them to use. Once reserved, TCP will not use any ports 1127 * in the range. This interface relies on the TCP_EXCLBIND feature. If 1128 * the semantics of TCP_EXCLBIND is changed, implementation of this interface 1129 * has to be verified. 1130 * 1131 * There can be TCP_RESERVED_PORTS_ARRAY_MAX_SIZE port ranges. Each port 1132 * range can cover at most TCP_RESERVED_PORTS_RANGE_MAX ports. A port 1133 * range is [port a, port b] inclusive. And each port range is between 1134 * TCP_LOWESET_RESERVED_PORT and TCP_LARGEST_RESERVED_PORT inclusive. 1135 * 1136 * Note that the default anonymous port range starts from 32768. There is 1137 * no port "collision" between that and the reserved port range. If there 1138 * is port collision (because the default smallest anonymous port is lowered 1139 * or some apps specifically bind to ports in the reserved port range), the 1140 * system may not be able to reserve a port range even there are enough 1141 * unbound ports as a reserved port range contains consecutive ports . 1142 */ 1143 #define TCP_RESERVED_PORTS_ARRAY_MAX_SIZE 5 1144 #define TCP_RESERVED_PORTS_RANGE_MAX 1000 1145 #define TCP_SMALLEST_RESERVED_PORT 10240 1146 #define TCP_LARGEST_RESERVED_PORT 20480 1147 1148 /* Structure to represent those reserved port ranges. */ 1149 typedef struct tcp_rport_s { 1150 in_port_t lo_port; 1151 in_port_t hi_port; 1152 tcp_t **temp_tcp_array; 1153 } tcp_rport_t; 1154 1155 /* The reserved port array. */ 1156 static tcp_rport_t tcp_reserved_port[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 1157 1158 /* Locks to protect the tcp_reserved_ports array. */ 1159 static krwlock_t tcp_reserved_port_lock; 1160 1161 /* The number of ranges in the array. */ 1162 uint32_t tcp_reserved_port_array_size = 0; 1163 1164 /* 1165 * MIB-2 stuff for SNMP 1166 * Note: tcpInErrs {tcp 15} is accumulated in ip.c 1167 */ 1168 mib2_tcp_t tcp_mib; /* SNMP fixed size info */ 1169 kstat_t *tcp_mibkp; /* kstat exporting tcp_mib data */ 1170 1171 boolean_t tcp_icmp_source_quench = B_FALSE; 1172 /* 1173 * Following assumes TPI alignment requirements stay along 32 bit 1174 * boundaries 1175 */ 1176 #define ROUNDUP32(x) \ 1177 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1178 1179 /* Template for response to info request. */ 1180 static struct T_info_ack tcp_g_t_info_ack = { 1181 T_INFO_ACK, /* PRIM_type */ 1182 0, /* TSDU_size */ 1183 T_INFINITE, /* ETSDU_size */ 1184 T_INVALID, /* CDATA_size */ 1185 T_INVALID, /* DDATA_size */ 1186 sizeof (sin_t), /* ADDR_size */ 1187 0, /* OPT_size - not initialized here */ 1188 TIDUSZ, /* TIDU_size */ 1189 T_COTS_ORD, /* SERV_type */ 1190 TCPS_IDLE, /* CURRENT_state */ 1191 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1192 }; 1193 1194 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1195 T_INFO_ACK, /* PRIM_type */ 1196 0, /* TSDU_size */ 1197 T_INFINITE, /* ETSDU_size */ 1198 T_INVALID, /* CDATA_size */ 1199 T_INVALID, /* DDATA_size */ 1200 sizeof (sin6_t), /* ADDR_size */ 1201 0, /* OPT_size - not initialized here */ 1202 TIDUSZ, /* TIDU_size */ 1203 T_COTS_ORD, /* SERV_type */ 1204 TCPS_IDLE, /* CURRENT_state */ 1205 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1206 }; 1207 1208 #define MS 1L 1209 #define SECONDS (1000 * MS) 1210 #define MINUTES (60 * SECONDS) 1211 #define HOURS (60 * MINUTES) 1212 #define DAYS (24 * HOURS) 1213 1214 #define PARAM_MAX (~(uint32_t)0) 1215 1216 /* Max size IP datagram is 64k - 1 */ 1217 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1218 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1219 /* Max of the above */ 1220 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1221 1222 /* Largest TCP port number */ 1223 #define TCP_MAX_PORT (64 * 1024 - 1) 1224 1225 /* 1226 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1227 * layer header. It has to be a multiple of 4. 1228 */ 1229 static tcpparam_t tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1230 #define tcp_wroff_xtra tcp_wroff_xtra_param.tcp_param_val 1231 1232 /* 1233 * All of these are alterable, within the min/max values given, at run time. 1234 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1235 * per the TCP spec. 1236 */ 1237 /* BEGIN CSTYLED */ 1238 tcpparam_t tcp_param_arr[] = { 1239 /*min max value name */ 1240 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1241 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1242 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1243 { 1, 1024, 1, "tcp_conn_req_min" }, 1244 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1245 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1246 { 0, 10, 0, "tcp_debug" }, 1247 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1248 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1249 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1250 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1251 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1252 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1253 { 1, 255, 64, "tcp_ipv4_ttl"}, 1254 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1255 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1256 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1257 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1258 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1259 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1260 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1261 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1262 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1263 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1264 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1265 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1266 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1267 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1268 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1269 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1270 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1271 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1272 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1273 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1274 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1275 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1276 { 0, TCP_MSS_MAX, 64, "tcp_co_min"}, 1277 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1278 /* 1279 * Question: What default value should I set for tcp_strong_iss? 1280 */ 1281 { 0, 2, 1, "tcp_strong_iss"}, 1282 { 0, 65536, 20, "tcp_rtt_updates"}, 1283 { 0, 1, 1, "tcp_wscale_always"}, 1284 { 0, 1, 0, "tcp_tstamp_always"}, 1285 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1286 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1287 { 0, 16, 2, "tcp_deferred_acks_max"}, 1288 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1289 { 1, 4, 4, "tcp_slow_start_initial"}, 1290 { 10*MS, 50*MS, 20*MS, "tcp_co_timer_interval"}, 1291 { 0, 2, 2, "tcp_sack_permitted"}, 1292 { 0, 1, 0, "tcp_trace"}, 1293 { 0, 1, 1, "tcp_compression_enabled"}, 1294 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1295 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1296 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1297 { 0, 1, 0, "tcp_rev_src_routes"}, 1298 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1299 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1300 { 0, 16, 8, "tcp_local_dacks_max"}, 1301 { 0, 2, 1, "tcp_ecn_permitted"}, 1302 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1303 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1304 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1305 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1306 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1307 }; 1308 /* END CSTYLED */ 1309 1310 /* 1311 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1312 * each header fragment in the header buffer. Each parameter value has 1313 * to be a multiple of 4 (32-bit aligned). 1314 */ 1315 static tcpparam_t tcp_mdt_head_param = { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1316 static tcpparam_t tcp_mdt_tail_param = { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1317 #define tcp_mdt_hdr_head_min tcp_mdt_head_param.tcp_param_val 1318 #define tcp_mdt_hdr_tail_min tcp_mdt_tail_param.tcp_param_val 1319 1320 /* 1321 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1322 * the maximum number of payload buffers associated per Multidata. 1323 */ 1324 static tcpparam_t tcp_mdt_max_pbufs_param = 1325 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1326 #define tcp_mdt_max_pbufs tcp_mdt_max_pbufs_param.tcp_param_val 1327 1328 /* Round up the value to the nearest mss. */ 1329 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1330 1331 /* 1332 * Set ECN capable transport (ECT) code point in IP header. 1333 * 1334 * Note that there are 2 ECT code points '01' and '10', which are called 1335 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1336 * point ECT(0) for TCP as described in RFC 2481. 1337 */ 1338 #define SET_ECT(tcp, iph) \ 1339 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1340 /* We need to clear the code point first. */ \ 1341 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1342 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1343 } else { \ 1344 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1345 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1346 } 1347 1348 /* 1349 * The format argument to pass to tcp_display(). 1350 * DISP_PORT_ONLY means that the returned string has only port info. 1351 * DISP_ADDR_AND_PORT means that the returned string also contains the 1352 * remote and local IP address. 1353 */ 1354 #define DISP_PORT_ONLY 1 1355 #define DISP_ADDR_AND_PORT 2 1356 1357 /* 1358 * This controls the rate some ndd info report functions can be used 1359 * by non-privileged users. It stores the last time such info is 1360 * requested. When those report functions are called again, this 1361 * is checked with the current time and compare with the ndd param 1362 * tcp_ndd_get_info_interval. 1363 */ 1364 static clock_t tcp_last_ndd_get_info_time = 0; 1365 #define NDD_TOO_QUICK_MSG \ 1366 "ndd get info rate too high for non-privileged users, try again " \ 1367 "later.\n" 1368 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1369 1370 #define IS_VMLOANED_MBLK(mp) \ 1371 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1372 1373 /* 1374 * These two variables control the rate for TCP to generate RSTs in 1375 * response to segments not belonging to any connections. We limit 1376 * TCP to sent out tcp_rst_sent_rate (ndd param) number of RSTs in 1377 * each 1 second interval. This is to protect TCP against DoS attack. 1378 */ 1379 static clock_t tcp_last_rst_intrvl; 1380 static uint32_t tcp_rst_cnt; 1381 1382 /* The number of RST not sent because of the rate limit. */ 1383 static uint32_t tcp_rst_unsent; 1384 1385 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1386 boolean_t tcp_mdt_chain = B_TRUE; 1387 1388 /* 1389 * MDT threshold in the form of effective send MSS multiplier; we take 1390 * the MDT path if the amount of unsent data exceeds the threshold value 1391 * (default threshold is 1*SMSS). 1392 */ 1393 uint_t tcp_mdt_smss_threshold = 1; 1394 1395 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1396 1397 /* 1398 * Forces all connections to obey the value of the tcp_maxpsz_multiplier 1399 * tunable settable via NDD. Otherwise, the per-connection behavior is 1400 * determined dynamically during tcp_adapt_ire(), which is the default. 1401 */ 1402 boolean_t tcp_static_maxpsz = B_FALSE; 1403 1404 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1405 uint32_t tcp_random_anon_port = 1; 1406 1407 /* 1408 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1409 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1410 * data, TCP will not respond with an ACK. RFC 793 requires that 1411 * TCP responds with an ACK for such a bogus ACK. By not following 1412 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1413 * an attacker successfully spoofs an acceptable segment to our 1414 * peer; or when our peer is "confused." 1415 */ 1416 uint32_t tcp_drop_ack_unsent_cnt = 10; 1417 1418 /* 1419 * Hook functions to enable cluster networking 1420 * On non-clustered systems these vectors must always be NULL. 1421 */ 1422 1423 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1424 uint8_t *laddrp, in_port_t lport) = NULL; 1425 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1426 uint8_t *laddrp, in_port_t lport) = NULL; 1427 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1428 uint8_t *laddrp, in_port_t lport, 1429 uint8_t *faddrp, in_port_t fport) = NULL; 1430 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1431 uint8_t *laddrp, in_port_t lport, 1432 uint8_t *faddrp, in_port_t fport) = NULL; 1433 1434 /* 1435 * The following are defined in ip.c 1436 */ 1437 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1438 uint8_t *laddrp); 1439 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1440 uint8_t *laddrp, uint8_t *faddrp); 1441 1442 #define CL_INET_CONNECT(tcp) { \ 1443 if (cl_inet_connect != NULL) { \ 1444 /* \ 1445 * Running in cluster mode - register active connection \ 1446 * information \ 1447 */ \ 1448 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1449 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1450 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1451 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1452 (in_port_t)(tcp)->tcp_lport, \ 1453 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1454 (in_port_t)(tcp)->tcp_fport); \ 1455 } \ 1456 } else { \ 1457 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1458 &(tcp)->tcp_ip6h->ip6_src)) {\ 1459 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1460 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1461 (in_port_t)(tcp)->tcp_lport, \ 1462 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1463 (in_port_t)(tcp)->tcp_fport); \ 1464 } \ 1465 } \ 1466 } \ 1467 } 1468 1469 #define CL_INET_DISCONNECT(tcp) { \ 1470 if (cl_inet_disconnect != NULL) { \ 1471 /* \ 1472 * Running in cluster mode - deregister active \ 1473 * connection information \ 1474 */ \ 1475 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1476 if ((tcp)->tcp_ip_src != 0) { \ 1477 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1478 AF_INET, \ 1479 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1480 (in_port_t)(tcp)->tcp_lport, \ 1481 (uint8_t *) \ 1482 (&((tcp)->tcp_ipha->ipha_dst)),\ 1483 (in_port_t)(tcp)->tcp_fport); \ 1484 } \ 1485 } else { \ 1486 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1487 &(tcp)->tcp_ip_src_v6)) { \ 1488 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1489 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1490 (in_port_t)(tcp)->tcp_lport, \ 1491 (uint8_t *) \ 1492 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1493 (in_port_t)(tcp)->tcp_fport); \ 1494 } \ 1495 } \ 1496 } \ 1497 } 1498 1499 /* 1500 * Cluster networking hook for traversing current connection list. 1501 * This routine is used to extract the current list of live connections 1502 * which must continue to to be dispatched to this node. 1503 */ 1504 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1505 1506 /* 1507 * Figure out the value of window scale opton. Note that the rwnd is 1508 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1509 * We cannot find the scale value and then do a round up of tcp_rwnd 1510 * because the scale value may not be correct after that. 1511 * 1512 * Set the compiler flag to make this function inline. 1513 */ 1514 static void 1515 tcp_set_ws_value(tcp_t *tcp) 1516 { 1517 int i; 1518 uint32_t rwnd = tcp->tcp_rwnd; 1519 1520 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1521 i++, rwnd >>= 1) 1522 ; 1523 tcp->tcp_rcv_ws = i; 1524 } 1525 1526 /* 1527 * Remove a connection from the list of detached TIME_WAIT connections. 1528 */ 1529 static void 1530 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1531 { 1532 boolean_t locked = B_FALSE; 1533 1534 if (tcp_time_wait == NULL) { 1535 tcp_time_wait = *((tcp_squeue_priv_t **) 1536 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1537 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1538 locked = B_TRUE; 1539 } 1540 1541 if (tcp->tcp_time_wait_expire == 0) { 1542 ASSERT(tcp->tcp_time_wait_next == NULL); 1543 ASSERT(tcp->tcp_time_wait_prev == NULL); 1544 if (locked) 1545 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1546 return; 1547 } 1548 ASSERT(TCP_IS_DETACHED(tcp)); 1549 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1550 1551 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1552 ASSERT(tcp->tcp_time_wait_prev == NULL); 1553 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1554 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1555 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1556 NULL; 1557 } else { 1558 tcp_time_wait->tcp_time_wait_tail = NULL; 1559 } 1560 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1561 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1562 ASSERT(tcp->tcp_time_wait_next == NULL); 1563 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1564 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1565 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1566 } else { 1567 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1568 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1569 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1570 tcp->tcp_time_wait_next; 1571 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1572 tcp->tcp_time_wait_prev; 1573 } 1574 tcp->tcp_time_wait_next = NULL; 1575 tcp->tcp_time_wait_prev = NULL; 1576 tcp->tcp_time_wait_expire = 0; 1577 1578 if (locked) 1579 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1580 } 1581 1582 /* 1583 * Add a connection to the list of detached TIME_WAIT connections 1584 * and set its time to expire. 1585 */ 1586 static void 1587 tcp_time_wait_append(tcp_t *tcp) 1588 { 1589 tcp_squeue_priv_t *tcp_time_wait = 1590 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1591 SQPRIVATE_TCP)); 1592 1593 tcp_timers_stop(tcp); 1594 1595 /* Freed above */ 1596 ASSERT(tcp->tcp_timer_tid == 0); 1597 ASSERT(tcp->tcp_ack_tid == 0); 1598 1599 /* must have happened at the time of detaching the tcp */ 1600 ASSERT(tcp->tcp_ptpahn == NULL); 1601 ASSERT(tcp->tcp_flow_stopped == 0); 1602 ASSERT(tcp->tcp_time_wait_next == NULL); 1603 ASSERT(tcp->tcp_time_wait_prev == NULL); 1604 ASSERT(tcp->tcp_time_wait_expire == NULL); 1605 ASSERT(tcp->tcp_listener == NULL); 1606 1607 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1608 /* 1609 * The value computed below in tcp->tcp_time_wait_expire may 1610 * appear negative or wrap around. That is ok since our 1611 * interest is only in the difference between the current lbolt 1612 * value and tcp->tcp_time_wait_expire. But the value should not 1613 * be zero, since it means the tcp is not in the TIME_WAIT list. 1614 * The corresponding comparison in tcp_time_wait_collector() uses 1615 * modular arithmetic. 1616 */ 1617 tcp->tcp_time_wait_expire += 1618 drv_usectohz(tcp_time_wait_interval * 1000); 1619 if (tcp->tcp_time_wait_expire == 0) 1620 tcp->tcp_time_wait_expire = 1; 1621 1622 ASSERT(TCP_IS_DETACHED(tcp)); 1623 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1624 ASSERT(tcp->tcp_time_wait_next == NULL); 1625 ASSERT(tcp->tcp_time_wait_prev == NULL); 1626 TCP_DBGSTAT(tcp_time_wait); 1627 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1628 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1629 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1630 tcp_time_wait->tcp_time_wait_head = tcp; 1631 } else { 1632 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1633 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1634 TCPS_TIME_WAIT); 1635 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1636 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1637 } 1638 tcp_time_wait->tcp_time_wait_tail = tcp; 1639 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1640 } 1641 1642 /* ARGSUSED */ 1643 void 1644 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1645 { 1646 conn_t *connp = (conn_t *)arg; 1647 tcp_t *tcp = connp->conn_tcp; 1648 1649 ASSERT(tcp != NULL); 1650 if (tcp->tcp_state == TCPS_CLOSED) { 1651 return; 1652 } 1653 1654 ASSERT((tcp->tcp_family == AF_INET && 1655 tcp->tcp_ipversion == IPV4_VERSION) || 1656 (tcp->tcp_family == AF_INET6 && 1657 (tcp->tcp_ipversion == IPV4_VERSION || 1658 tcp->tcp_ipversion == IPV6_VERSION))); 1659 ASSERT(!tcp->tcp_listener); 1660 1661 TCP_STAT(tcp_time_wait_reap); 1662 ASSERT(TCP_IS_DETACHED(tcp)); 1663 1664 /* 1665 * Because they have no upstream client to rebind or tcp_close() 1666 * them later, we axe the connection here and now. 1667 */ 1668 tcp_close_detached(tcp); 1669 } 1670 1671 void 1672 tcp_cleanup(tcp_t *tcp) 1673 { 1674 mblk_t *mp; 1675 char *tcp_iphc; 1676 int tcp_iphc_len; 1677 int tcp_hdr_grown; 1678 tcp_sack_info_t *tcp_sack_info; 1679 conn_t *connp = tcp->tcp_connp; 1680 1681 tcp_bind_hash_remove(tcp); 1682 tcp_free(tcp); 1683 1684 /* Release any SSL context */ 1685 if (tcp->tcp_kssl_ent != NULL) { 1686 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1687 tcp->tcp_kssl_ent = NULL; 1688 } 1689 1690 if (tcp->tcp_kssl_ctx != NULL) { 1691 kssl_release_ctx(tcp->tcp_kssl_ctx); 1692 tcp->tcp_kssl_ctx = NULL; 1693 } 1694 tcp->tcp_kssl_pending = B_FALSE; 1695 1696 conn_delete_ire(connp, NULL); 1697 if (connp->conn_flags & IPCL_TCPCONN) { 1698 if (connp->conn_latch != NULL) 1699 IPLATCH_REFRELE(connp->conn_latch); 1700 if (connp->conn_policy != NULL) 1701 IPPH_REFRELE(connp->conn_policy); 1702 } 1703 1704 /* 1705 * Since we will bzero the entire structure, we need to 1706 * remove it and reinsert it in global hash list. We 1707 * know the walkers can't get to this conn because we 1708 * had set CONDEMNED flag earlier and checked reference 1709 * under conn_lock so walker won't pick it and when we 1710 * go the ipcl_globalhash_remove() below, no walker 1711 * can get to it. 1712 */ 1713 ipcl_globalhash_remove(connp); 1714 1715 /* Save some state */ 1716 mp = tcp->tcp_timercache; 1717 1718 tcp_sack_info = tcp->tcp_sack_info; 1719 tcp_iphc = tcp->tcp_iphc; 1720 tcp_iphc_len = tcp->tcp_iphc_len; 1721 tcp_hdr_grown = tcp->tcp_hdr_grown; 1722 1723 if (connp->conn_cred != NULL) 1724 crfree(connp->conn_cred); 1725 if (connp->conn_peercred != NULL) 1726 crfree(connp->conn_peercred); 1727 bzero(connp, sizeof (conn_t)); 1728 bzero(tcp, sizeof (tcp_t)); 1729 1730 /* restore the state */ 1731 tcp->tcp_timercache = mp; 1732 1733 tcp->tcp_sack_info = tcp_sack_info; 1734 tcp->tcp_iphc = tcp_iphc; 1735 tcp->tcp_iphc_len = tcp_iphc_len; 1736 tcp->tcp_hdr_grown = tcp_hdr_grown; 1737 1738 1739 tcp->tcp_connp = connp; 1740 1741 connp->conn_tcp = tcp; 1742 connp->conn_flags = IPCL_TCPCONN; 1743 connp->conn_state_flags = CONN_INCIPIENT; 1744 connp->conn_ulp = IPPROTO_TCP; 1745 connp->conn_ref = 1; 1746 1747 ipcl_globalhash_insert(connp); 1748 } 1749 1750 /* 1751 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1752 * is done forwards from the head. 1753 */ 1754 /* ARGSUSED */ 1755 void 1756 tcp_time_wait_collector(void *arg) 1757 { 1758 tcp_t *tcp; 1759 clock_t now; 1760 mblk_t *mp; 1761 conn_t *connp; 1762 kmutex_t *lock; 1763 1764 squeue_t *sqp = (squeue_t *)arg; 1765 tcp_squeue_priv_t *tcp_time_wait = 1766 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1767 1768 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1769 tcp_time_wait->tcp_time_wait_tid = 0; 1770 1771 if (tcp_time_wait->tcp_free_list != NULL && 1772 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1773 TCP_STAT(tcp_freelist_cleanup); 1774 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1775 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1776 CONN_DEC_REF(tcp->tcp_connp); 1777 } 1778 tcp_time_wait->tcp_free_list_cnt = 0; 1779 } 1780 1781 /* 1782 * In order to reap time waits reliably, we should use a 1783 * source of time that is not adjustable by the user -- hence 1784 * the call to ddi_get_lbolt(). 1785 */ 1786 now = ddi_get_lbolt(); 1787 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1788 /* 1789 * Compare times using modular arithmetic, since 1790 * lbolt can wrapover. 1791 */ 1792 if ((now - tcp->tcp_time_wait_expire) < 0) { 1793 break; 1794 } 1795 1796 tcp_time_wait_remove(tcp, tcp_time_wait); 1797 1798 connp = tcp->tcp_connp; 1799 ASSERT(connp->conn_fanout != NULL); 1800 lock = &connp->conn_fanout->connf_lock; 1801 /* 1802 * This is essentially a TW reclaim fast path optimization for 1803 * performance where the timewait collector checks under the 1804 * fanout lock (so that no one else can get access to the 1805 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1806 * the classifier hash list. If ref count is indeed 2, we can 1807 * just remove the conn under the fanout lock and avoid 1808 * cleaning up the conn under the squeue, provided that 1809 * clustering callbacks are not enabled. If clustering is 1810 * enabled, we need to make the clustering callback before 1811 * setting the CONDEMNED flag and after dropping all locks and 1812 * so we forego this optimization and fall back to the slow 1813 * path. Also please see the comments in tcp_closei_local 1814 * regarding the refcnt logic. 1815 * 1816 * Since we are holding the tcp_time_wait_lock, its better 1817 * not to block on the fanout_lock because other connections 1818 * can't add themselves to time_wait list. So we do a 1819 * tryenter instead of mutex_enter. 1820 */ 1821 if (mutex_tryenter(lock)) { 1822 mutex_enter(&connp->conn_lock); 1823 if ((connp->conn_ref == 2) && 1824 (cl_inet_disconnect == NULL)) { 1825 ipcl_hash_remove_locked(connp, 1826 connp->conn_fanout); 1827 /* 1828 * Set the CONDEMNED flag now itself so that 1829 * the refcnt cannot increase due to any 1830 * walker. But we have still not cleaned up 1831 * conn_ire_cache. This is still ok since 1832 * we are going to clean it up in tcp_cleanup 1833 * immediately and any interface unplumb 1834 * thread will wait till the ire is blown away 1835 */ 1836 connp->conn_state_flags |= CONN_CONDEMNED; 1837 mutex_exit(lock); 1838 mutex_exit(&connp->conn_lock); 1839 if (tcp_time_wait->tcp_free_list_cnt < 1840 tcp_free_list_max_cnt) { 1841 /* Add to head of tcp_free_list */ 1842 mutex_exit( 1843 &tcp_time_wait->tcp_time_wait_lock); 1844 tcp_cleanup(tcp); 1845 mutex_enter( 1846 &tcp_time_wait->tcp_time_wait_lock); 1847 tcp->tcp_time_wait_next = 1848 tcp_time_wait->tcp_free_list; 1849 tcp_time_wait->tcp_free_list = tcp; 1850 tcp_time_wait->tcp_free_list_cnt++; 1851 continue; 1852 } else { 1853 /* Do not add to tcp_free_list */ 1854 mutex_exit( 1855 &tcp_time_wait->tcp_time_wait_lock); 1856 tcp_bind_hash_remove(tcp); 1857 conn_delete_ire(tcp->tcp_connp, NULL); 1858 CONN_DEC_REF(tcp->tcp_connp); 1859 } 1860 } else { 1861 CONN_INC_REF_LOCKED(connp); 1862 mutex_exit(lock); 1863 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1864 mutex_exit(&connp->conn_lock); 1865 /* 1866 * We can reuse the closemp here since conn has 1867 * detached (otherwise we wouldn't even be in 1868 * time_wait list). 1869 */ 1870 mp = &tcp->tcp_closemp; 1871 squeue_fill(connp->conn_sqp, mp, 1872 tcp_timewait_output, connp, 1873 SQTAG_TCP_TIMEWAIT); 1874 } 1875 } else { 1876 mutex_enter(&connp->conn_lock); 1877 CONN_INC_REF_LOCKED(connp); 1878 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1879 mutex_exit(&connp->conn_lock); 1880 /* 1881 * We can reuse the closemp here since conn has 1882 * detached (otherwise we wouldn't even be in 1883 * time_wait list). 1884 */ 1885 mp = &tcp->tcp_closemp; 1886 squeue_fill(connp->conn_sqp, mp, 1887 tcp_timewait_output, connp, 0); 1888 } 1889 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1890 } 1891 1892 if (tcp_time_wait->tcp_free_list != NULL) 1893 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1894 1895 tcp_time_wait->tcp_time_wait_tid = 1896 timeout(tcp_time_wait_collector, sqp, TCP_TIME_WAIT_DELAY); 1897 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1898 } 1899 1900 /* 1901 * Reply to a clients T_CONN_RES TPI message. This function 1902 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1903 * on the acceptor STREAM and processed in tcp_wput_accept(). 1904 * Read the block comment on top of tcp_conn_request(). 1905 */ 1906 static void 1907 tcp_accept(tcp_t *listener, mblk_t *mp) 1908 { 1909 tcp_t *acceptor; 1910 tcp_t *eager; 1911 tcp_t *tcp; 1912 struct T_conn_res *tcr; 1913 t_uscalar_t acceptor_id; 1914 t_scalar_t seqnum; 1915 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1916 mblk_t *ok_mp; 1917 mblk_t *mp1; 1918 1919 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1920 tcp_err_ack(listener, mp, TPROTO, 0); 1921 return; 1922 } 1923 tcr = (struct T_conn_res *)mp->b_rptr; 1924 1925 /* 1926 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1927 * read side queue of the streams device underneath us i.e. the 1928 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1929 * look it up in the queue_hash. Under LP64 it sends down the 1930 * minor_t of the accepting endpoint. 1931 * 1932 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1933 * fanout hash lock is held. 1934 * This prevents any thread from entering the acceptor queue from 1935 * below (since it has not been hard bound yet i.e. any inbound 1936 * packets will arrive on the listener or default tcp queue and 1937 * go through tcp_lookup). 1938 * The CONN_INC_REF will prevent the acceptor from closing. 1939 * 1940 * XXX It is still possible for a tli application to send down data 1941 * on the accepting stream while another thread calls t_accept. 1942 * This should not be a problem for well-behaved applications since 1943 * the T_OK_ACK is sent after the queue swapping is completed. 1944 * 1945 * If the accepting fd is the same as the listening fd, avoid 1946 * queue hash lookup since that will return an eager listener in a 1947 * already established state. 1948 */ 1949 acceptor_id = tcr->ACCEPTOR_id; 1950 mutex_enter(&listener->tcp_eager_lock); 1951 if (listener->tcp_acceptor_id == acceptor_id) { 1952 eager = listener->tcp_eager_next_q; 1953 /* only count how many T_CONN_INDs so don't count q0 */ 1954 if ((listener->tcp_conn_req_cnt_q != 1) || 1955 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1956 mutex_exit(&listener->tcp_eager_lock); 1957 tcp_err_ack(listener, mp, TBADF, 0); 1958 return; 1959 } 1960 if (listener->tcp_conn_req_cnt_q0 != 0) { 1961 /* Throw away all the eagers on q0. */ 1962 tcp_eager_cleanup(listener, 1); 1963 } 1964 if (listener->tcp_syn_defense) { 1965 listener->tcp_syn_defense = B_FALSE; 1966 if (listener->tcp_ip_addr_cache != NULL) { 1967 kmem_free(listener->tcp_ip_addr_cache, 1968 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1969 listener->tcp_ip_addr_cache = NULL; 1970 } 1971 } 1972 /* 1973 * Transfer tcp_conn_req_max to the eager so that when 1974 * a disconnect occurs we can revert the endpoint to the 1975 * listen state. 1976 */ 1977 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1978 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1979 /* 1980 * Get a reference on the acceptor just like the 1981 * tcp_acceptor_hash_lookup below. 1982 */ 1983 acceptor = listener; 1984 CONN_INC_REF(acceptor->tcp_connp); 1985 } else { 1986 acceptor = tcp_acceptor_hash_lookup(acceptor_id); 1987 if (acceptor == NULL) { 1988 if (listener->tcp_debug) { 1989 (void) strlog(TCP_MOD_ID, 0, 1, 1990 SL_ERROR|SL_TRACE, 1991 "tcp_accept: did not find acceptor 0x%x\n", 1992 acceptor_id); 1993 } 1994 mutex_exit(&listener->tcp_eager_lock); 1995 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 1996 return; 1997 } 1998 /* 1999 * Verify acceptor state. The acceptable states for an acceptor 2000 * include TCPS_IDLE and TCPS_BOUND. 2001 */ 2002 switch (acceptor->tcp_state) { 2003 case TCPS_IDLE: 2004 /* FALLTHRU */ 2005 case TCPS_BOUND: 2006 break; 2007 default: 2008 CONN_DEC_REF(acceptor->tcp_connp); 2009 mutex_exit(&listener->tcp_eager_lock); 2010 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2011 return; 2012 } 2013 } 2014 2015 /* The listener must be in TCPS_LISTEN */ 2016 if (listener->tcp_state != TCPS_LISTEN) { 2017 CONN_DEC_REF(acceptor->tcp_connp); 2018 mutex_exit(&listener->tcp_eager_lock); 2019 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2020 return; 2021 } 2022 2023 /* 2024 * Rendezvous with an eager connection request packet hanging off 2025 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2026 * tcp structure when the connection packet arrived in 2027 * tcp_conn_request(). 2028 */ 2029 seqnum = tcr->SEQ_number; 2030 eager = listener; 2031 do { 2032 eager = eager->tcp_eager_next_q; 2033 if (eager == NULL) { 2034 CONN_DEC_REF(acceptor->tcp_connp); 2035 mutex_exit(&listener->tcp_eager_lock); 2036 tcp_err_ack(listener, mp, TBADSEQ, 0); 2037 return; 2038 } 2039 } while (eager->tcp_conn_req_seqnum != seqnum); 2040 mutex_exit(&listener->tcp_eager_lock); 2041 2042 /* 2043 * At this point, both acceptor and listener have 2 ref 2044 * that they begin with. Acceptor has one additional ref 2045 * we placed in lookup while listener has 3 additional 2046 * ref for being behind the squeue (tcp_accept() is 2047 * done on listener's squeue); being in classifier hash; 2048 * and eager's ref on listener. 2049 */ 2050 ASSERT(listener->tcp_connp->conn_ref >= 5); 2051 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2052 2053 /* 2054 * The eager at this point is set in its own squeue and 2055 * could easily have been killed (tcp_accept_finish will 2056 * deal with that) because of a TH_RST so we can only 2057 * ASSERT for a single ref. 2058 */ 2059 ASSERT(eager->tcp_connp->conn_ref >= 1); 2060 2061 /* Pre allocate the stroptions mblk also */ 2062 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2063 if (opt_mp == NULL) { 2064 CONN_DEC_REF(acceptor->tcp_connp); 2065 CONN_DEC_REF(eager->tcp_connp); 2066 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2067 return; 2068 } 2069 DB_TYPE(opt_mp) = M_SETOPTS; 2070 opt_mp->b_wptr += sizeof (struct stroptions); 2071 2072 /* 2073 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2074 * from listener to acceptor. The message is chained on opt_mp 2075 * which will be sent onto eager's squeue. 2076 */ 2077 if (listener->tcp_bound_if != 0) { 2078 /* allocate optmgmt req */ 2079 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2080 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2081 sizeof (int)); 2082 if (mp1 != NULL) 2083 linkb(opt_mp, mp1); 2084 } 2085 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2086 uint_t on = 1; 2087 2088 /* allocate optmgmt req */ 2089 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2090 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2091 if (mp1 != NULL) 2092 linkb(opt_mp, mp1); 2093 } 2094 2095 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2096 if ((mp1 = copymsg(mp)) == NULL) { 2097 CONN_DEC_REF(acceptor->tcp_connp); 2098 CONN_DEC_REF(eager->tcp_connp); 2099 freemsg(opt_mp); 2100 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2101 return; 2102 } 2103 2104 tcr = (struct T_conn_res *)mp1->b_rptr; 2105 2106 /* 2107 * This is an expanded version of mi_tpi_ok_ack_alloc() 2108 * which allocates a larger mblk and appends the new 2109 * local address to the ok_ack. The address is copied by 2110 * soaccept() for getsockname(). 2111 */ 2112 { 2113 int extra; 2114 2115 extra = (eager->tcp_family == AF_INET) ? 2116 sizeof (sin_t) : sizeof (sin6_t); 2117 2118 /* 2119 * Try to re-use mp, if possible. Otherwise, allocate 2120 * an mblk and return it as ok_mp. In any case, mp 2121 * is no longer usable upon return. 2122 */ 2123 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2124 CONN_DEC_REF(acceptor->tcp_connp); 2125 CONN_DEC_REF(eager->tcp_connp); 2126 freemsg(opt_mp); 2127 /* Original mp has been freed by now, so use mp1 */ 2128 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2129 return; 2130 } 2131 2132 mp = NULL; /* We should never use mp after this point */ 2133 2134 switch (extra) { 2135 case sizeof (sin_t): { 2136 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2137 2138 ok_mp->b_wptr += extra; 2139 sin->sin_family = AF_INET; 2140 sin->sin_port = eager->tcp_lport; 2141 sin->sin_addr.s_addr = 2142 eager->tcp_ipha->ipha_src; 2143 break; 2144 } 2145 case sizeof (sin6_t): { 2146 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2147 2148 ok_mp->b_wptr += extra; 2149 sin6->sin6_family = AF_INET6; 2150 sin6->sin6_port = eager->tcp_lport; 2151 if (eager->tcp_ipversion == IPV4_VERSION) { 2152 sin6->sin6_flowinfo = 0; 2153 IN6_IPADDR_TO_V4MAPPED( 2154 eager->tcp_ipha->ipha_src, 2155 &sin6->sin6_addr); 2156 } else { 2157 ASSERT(eager->tcp_ip6h != NULL); 2158 sin6->sin6_flowinfo = 2159 eager->tcp_ip6h->ip6_vcf & 2160 ~IPV6_VERS_AND_FLOW_MASK; 2161 sin6->sin6_addr = 2162 eager->tcp_ip6h->ip6_src; 2163 } 2164 break; 2165 } 2166 default: 2167 break; 2168 } 2169 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2170 } 2171 2172 /* 2173 * If there are no options we know that the T_CONN_RES will 2174 * succeed. However, we can't send the T_OK_ACK upstream until 2175 * the tcp_accept_swap is done since it would be dangerous to 2176 * let the application start using the new fd prior to the swap. 2177 */ 2178 tcp_accept_swap(listener, acceptor, eager); 2179 2180 /* 2181 * tcp_accept_swap unlinks eager from listener but does not drop 2182 * the eager's reference on the listener. 2183 */ 2184 ASSERT(eager->tcp_listener == NULL); 2185 ASSERT(listener->tcp_connp->conn_ref >= 5); 2186 2187 /* 2188 * The eager is now associated with its own queue. Insert in 2189 * the hash so that the connection can be reused for a future 2190 * T_CONN_RES. 2191 */ 2192 tcp_acceptor_hash_insert(acceptor_id, eager); 2193 2194 /* 2195 * We now do the processing of options with T_CONN_RES. 2196 * We delay till now since we wanted to have queue to pass to 2197 * option processing routines that points back to the right 2198 * instance structure which does not happen until after 2199 * tcp_accept_swap(). 2200 * 2201 * Note: 2202 * The sanity of the logic here assumes that whatever options 2203 * are appropriate to inherit from listner=>eager are done 2204 * before this point, and whatever were to be overridden (or not) 2205 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2206 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2207 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2208 * This may not be true at this point in time but can be fixed 2209 * independently. This option processing code starts with 2210 * the instantiated acceptor instance and the final queue at 2211 * this point. 2212 */ 2213 2214 if (tcr->OPT_length != 0) { 2215 /* Options to process */ 2216 int t_error = 0; 2217 int sys_error = 0; 2218 int do_disconnect = 0; 2219 2220 if (tcp_conprim_opt_process(eager, mp1, 2221 &do_disconnect, &t_error, &sys_error) < 0) { 2222 eager->tcp_accept_error = 1; 2223 if (do_disconnect) { 2224 /* 2225 * An option failed which does not allow 2226 * connection to be accepted. 2227 * 2228 * We allow T_CONN_RES to succeed and 2229 * put a T_DISCON_IND on the eager queue. 2230 */ 2231 ASSERT(t_error == 0 && sys_error == 0); 2232 eager->tcp_send_discon_ind = 1; 2233 } else { 2234 ASSERT(t_error != 0); 2235 freemsg(ok_mp); 2236 /* 2237 * Original mp was either freed or set 2238 * to ok_mp above, so use mp1 instead. 2239 */ 2240 tcp_err_ack(listener, mp1, t_error, sys_error); 2241 goto finish; 2242 } 2243 } 2244 /* 2245 * Most likely success in setting options (except if 2246 * eager->tcp_send_discon_ind set). 2247 * mp1 option buffer represented by OPT_length/offset 2248 * potentially modified and contains results of setting 2249 * options at this point 2250 */ 2251 } 2252 2253 /* We no longer need mp1, since all options processing has passed */ 2254 freemsg(mp1); 2255 2256 putnext(listener->tcp_rq, ok_mp); 2257 2258 mutex_enter(&listener->tcp_eager_lock); 2259 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2260 tcp_t *tail; 2261 mblk_t *conn_ind; 2262 2263 /* 2264 * This path should not be executed if listener and 2265 * acceptor streams are the same. 2266 */ 2267 ASSERT(listener != acceptor); 2268 2269 tcp = listener->tcp_eager_prev_q0; 2270 /* 2271 * listener->tcp_eager_prev_q0 points to the TAIL of the 2272 * deferred T_conn_ind queue. We need to get to the head of 2273 * the queue in order to send up T_conn_ind the same order as 2274 * how the 3WHS is completed. 2275 */ 2276 while (tcp != listener) { 2277 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2278 break; 2279 else 2280 tcp = tcp->tcp_eager_prev_q0; 2281 } 2282 ASSERT(tcp != listener); 2283 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2284 ASSERT(conn_ind != NULL); 2285 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2286 2287 /* Move from q0 to q */ 2288 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2289 listener->tcp_conn_req_cnt_q0--; 2290 listener->tcp_conn_req_cnt_q++; 2291 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2292 tcp->tcp_eager_prev_q0; 2293 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2294 tcp->tcp_eager_next_q0; 2295 tcp->tcp_eager_prev_q0 = NULL; 2296 tcp->tcp_eager_next_q0 = NULL; 2297 tcp->tcp_conn_def_q0 = B_FALSE; 2298 2299 /* 2300 * Insert at end of the queue because sockfs sends 2301 * down T_CONN_RES in chronological order. Leaving 2302 * the older conn indications at front of the queue 2303 * helps reducing search time. 2304 */ 2305 tail = listener->tcp_eager_last_q; 2306 if (tail != NULL) 2307 tail->tcp_eager_next_q = tcp; 2308 else 2309 listener->tcp_eager_next_q = tcp; 2310 listener->tcp_eager_last_q = tcp; 2311 tcp->tcp_eager_next_q = NULL; 2312 mutex_exit(&listener->tcp_eager_lock); 2313 putnext(tcp->tcp_rq, conn_ind); 2314 } else { 2315 mutex_exit(&listener->tcp_eager_lock); 2316 } 2317 2318 /* 2319 * Done with the acceptor - free it 2320 * 2321 * Note: from this point on, no access to listener should be made 2322 * as listener can be equal to acceptor. 2323 */ 2324 finish: 2325 ASSERT(acceptor->tcp_detached); 2326 acceptor->tcp_rq = tcp_g_q; 2327 acceptor->tcp_wq = WR(tcp_g_q); 2328 (void) tcp_clean_death(acceptor, 0, 2); 2329 CONN_DEC_REF(acceptor->tcp_connp); 2330 2331 /* 2332 * In case we already received a FIN we have to make tcp_rput send 2333 * the ordrel_ind. This will also send up a window update if the window 2334 * has opened up. 2335 * 2336 * In the normal case of a successful connection acceptance 2337 * we give the O_T_BIND_REQ to the read side put procedure as an 2338 * indication that this was just accepted. This tells tcp_rput to 2339 * pass up any data queued in tcp_rcv_list. 2340 * 2341 * In the fringe case where options sent with T_CONN_RES failed and 2342 * we required, we would be indicating a T_DISCON_IND to blow 2343 * away this connection. 2344 */ 2345 2346 /* 2347 * XXX: we currently have a problem if XTI application closes the 2348 * acceptor stream in between. This problem exists in on10-gate also 2349 * and is well know but nothing can be done short of major rewrite 2350 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2351 * eager same squeue as listener (we can distinguish non socket 2352 * listeners at the time of handling a SYN in tcp_conn_request) 2353 * and do most of the work that tcp_accept_finish does here itself 2354 * and then get behind the acceptor squeue to access the acceptor 2355 * queue. 2356 */ 2357 /* 2358 * We already have a ref on tcp so no need to do one before squeue_fill 2359 */ 2360 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2361 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2362 } 2363 2364 /* 2365 * Swap information between the eager and acceptor for a TLI/XTI client. 2366 * The sockfs accept is done on the acceptor stream and control goes 2367 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2368 * called. In either case, both the eager and listener are in their own 2369 * perimeter (squeue) and the code has to deal with potential race. 2370 * 2371 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2372 */ 2373 static void 2374 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2375 { 2376 conn_t *econnp, *aconnp; 2377 2378 ASSERT(eager->tcp_rq == listener->tcp_rq); 2379 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2380 ASSERT(!eager->tcp_hard_bound); 2381 ASSERT(!TCP_IS_SOCKET(acceptor)); 2382 ASSERT(!TCP_IS_SOCKET(eager)); 2383 ASSERT(!TCP_IS_SOCKET(listener)); 2384 2385 acceptor->tcp_detached = B_TRUE; 2386 /* 2387 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2388 * the acceptor id. 2389 */ 2390 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2391 2392 /* remove eager from listen list... */ 2393 mutex_enter(&listener->tcp_eager_lock); 2394 tcp_eager_unlink(eager); 2395 ASSERT(eager->tcp_eager_next_q == NULL && 2396 eager->tcp_eager_last_q == NULL); 2397 ASSERT(eager->tcp_eager_next_q0 == NULL && 2398 eager->tcp_eager_prev_q0 == NULL); 2399 mutex_exit(&listener->tcp_eager_lock); 2400 eager->tcp_rq = acceptor->tcp_rq; 2401 eager->tcp_wq = acceptor->tcp_wq; 2402 2403 econnp = eager->tcp_connp; 2404 aconnp = acceptor->tcp_connp; 2405 2406 eager->tcp_rq->q_ptr = econnp; 2407 eager->tcp_wq->q_ptr = econnp; 2408 2409 /* 2410 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2411 * which might be a different squeue from our peer TCP instance. 2412 * For TCP Fusion, the peer expects that whenever tcp_detached is 2413 * clear, our TCP queues point to the acceptor's queues. Thus, use 2414 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2415 * above reach global visibility prior to the clearing of tcp_detached. 2416 */ 2417 membar_producer(); 2418 eager->tcp_detached = B_FALSE; 2419 2420 ASSERT(eager->tcp_ack_tid == 0); 2421 2422 econnp->conn_dev = aconnp->conn_dev; 2423 if (eager->tcp_cred != NULL) 2424 crfree(eager->tcp_cred); 2425 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2426 econnp->conn_zoneid = aconnp->conn_zoneid; 2427 aconnp->conn_cred = NULL; 2428 2429 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2430 aconnp->conn_mac_exempt = B_FALSE; 2431 2432 ASSERT(aconnp->conn_peercred == NULL); 2433 2434 /* Do the IPC initialization */ 2435 CONN_INC_REF(econnp); 2436 2437 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2438 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2439 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2440 econnp->conn_ulp = aconnp->conn_ulp; 2441 2442 /* Done with old IPC. Drop its ref on its connp */ 2443 CONN_DEC_REF(aconnp); 2444 } 2445 2446 2447 /* 2448 * Adapt to the information, such as rtt and rtt_sd, provided from the 2449 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2450 * 2451 * Checks for multicast and broadcast destination address. 2452 * Returns zero on failure; non-zero if ok. 2453 * 2454 * Note that the MSS calculation here is based on the info given in 2455 * the IRE. We do not do any calculation based on TCP options. They 2456 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2457 * knows which options to use. 2458 * 2459 * Note on how TCP gets its parameters for a connection. 2460 * 2461 * When a tcp_t structure is allocated, it gets all the default parameters. 2462 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2463 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2464 * default. But if there is an associated tcp_host_param, it will override 2465 * the metrics. 2466 * 2467 * An incoming SYN with a multicast or broadcast destination address, is dropped 2468 * in 1 of 2 places. 2469 * 2470 * 1. If the packet was received over the wire it is dropped in 2471 * ip_rput_process_broadcast() 2472 * 2473 * 2. If the packet was received through internal IP loopback, i.e. the packet 2474 * was generated and received on the same machine, it is dropped in 2475 * ip_wput_local() 2476 * 2477 * An incoming SYN with a multicast or broadcast source address is always 2478 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2479 * reject an attempt to connect to a broadcast or multicast (destination) 2480 * address. 2481 */ 2482 static int 2483 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2484 { 2485 tcp_hsp_t *hsp; 2486 ire_t *ire; 2487 ire_t *sire = NULL; 2488 iulp_t *ire_uinfo = NULL; 2489 uint32_t mss_max; 2490 uint32_t mss; 2491 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2492 conn_t *connp = tcp->tcp_connp; 2493 boolean_t ire_cacheable = B_FALSE; 2494 zoneid_t zoneid = connp->conn_zoneid; 2495 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2496 MATCH_IRE_SECATTR; 2497 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2498 ill_t *ill = NULL; 2499 boolean_t incoming = (ire_mp == NULL); 2500 2501 ASSERT(connp->conn_ire_cache == NULL); 2502 2503 if (tcp->tcp_ipversion == IPV4_VERSION) { 2504 2505 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2506 BUMP_MIB(&ip_mib, ipInDiscards); 2507 return (0); 2508 } 2509 /* 2510 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2511 * for the destination with the nexthop as gateway. 2512 * ire_ctable_lookup() is used because this particular 2513 * ire, if it exists, will be marked private. 2514 * If that is not available, use the interface ire 2515 * for the nexthop. 2516 * 2517 * TSol: tcp_update_label will detect label mismatches based 2518 * only on the destination's label, but that would not 2519 * detect label mismatches based on the security attributes 2520 * of routes or next hop gateway. Hence we need to pass the 2521 * label to ire_ftable_lookup below in order to locate the 2522 * right prefix (and/or) ire cache. Similarly we also need 2523 * pass the label to the ire_cache_lookup below to locate 2524 * the right ire that also matches on the label. 2525 */ 2526 if (tcp->tcp_connp->conn_nexthop_set) { 2527 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2528 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2529 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW); 2530 if (ire == NULL) { 2531 ire = ire_ftable_lookup( 2532 tcp->tcp_connp->conn_nexthop_v4, 2533 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2534 tsl, match_flags); 2535 if (ire == NULL) 2536 return (0); 2537 } else { 2538 ire_uinfo = &ire->ire_uinfo; 2539 } 2540 } else { 2541 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2542 zoneid, tsl); 2543 if (ire != NULL) { 2544 ire_cacheable = B_TRUE; 2545 ire_uinfo = (ire_mp != NULL) ? 2546 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2547 &ire->ire_uinfo; 2548 2549 } else { 2550 if (ire_mp == NULL) { 2551 ire = ire_ftable_lookup( 2552 tcp->tcp_connp->conn_rem, 2553 0, 0, 0, NULL, &sire, zoneid, 0, 2554 tsl, (MATCH_IRE_RECURSIVE | 2555 MATCH_IRE_DEFAULT)); 2556 if (ire == NULL) 2557 return (0); 2558 ire_uinfo = (sire != NULL) ? 2559 &sire->ire_uinfo : 2560 &ire->ire_uinfo; 2561 } else { 2562 ire = (ire_t *)ire_mp->b_rptr; 2563 ire_uinfo = 2564 &((ire_t *) 2565 ire_mp->b_rptr)->ire_uinfo; 2566 } 2567 } 2568 } 2569 ASSERT(ire != NULL); 2570 2571 if ((ire->ire_src_addr == INADDR_ANY) || 2572 (ire->ire_type & IRE_BROADCAST)) { 2573 /* 2574 * ire->ire_mp is non null when ire_mp passed in is used 2575 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2576 */ 2577 if (ire->ire_mp == NULL) 2578 ire_refrele(ire); 2579 if (sire != NULL) 2580 ire_refrele(sire); 2581 return (0); 2582 } 2583 2584 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2585 ipaddr_t src_addr; 2586 2587 /* 2588 * ip_bind_connected() has stored the correct source 2589 * address in conn_src. 2590 */ 2591 src_addr = tcp->tcp_connp->conn_src; 2592 tcp->tcp_ipha->ipha_src = src_addr; 2593 /* 2594 * Copy of the src addr. in tcp_t is needed 2595 * for the lookup funcs. 2596 */ 2597 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2598 } 2599 /* 2600 * Set the fragment bit so that IP will tell us if the MTU 2601 * should change. IP tells us the latest setting of 2602 * ip_path_mtu_discovery through ire_frag_flag. 2603 */ 2604 if (ip_path_mtu_discovery) { 2605 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2606 htons(IPH_DF); 2607 } 2608 /* 2609 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2610 * for IP_NEXTHOP. No cache ire has been found for the 2611 * destination and we are working with the nexthop's 2612 * interface ire. Since we need to forward all packets 2613 * to the nexthop first, we "blindly" set tcp_localnet 2614 * to false, eventhough the destination may also be 2615 * onlink. 2616 */ 2617 if (ire_uinfo == NULL) 2618 tcp->tcp_localnet = 0; 2619 else 2620 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2621 } else { 2622 /* 2623 * For incoming connection ire_mp = NULL 2624 * For outgoing connection ire_mp != NULL 2625 * Technically we should check conn_incoming_ill 2626 * when ire_mp is NULL and conn_outgoing_ill when 2627 * ire_mp is non-NULL. But this is performance 2628 * critical path and for IPV*_BOUND_IF, outgoing 2629 * and incoming ill are always set to the same value. 2630 */ 2631 ill_t *dst_ill = NULL; 2632 ipif_t *dst_ipif = NULL; 2633 2634 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2635 2636 if (connp->conn_outgoing_ill != NULL) { 2637 /* Outgoing or incoming path */ 2638 int err; 2639 2640 dst_ill = conn_get_held_ill(connp, 2641 &connp->conn_outgoing_ill, &err); 2642 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2643 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2644 return (0); 2645 } 2646 match_flags |= MATCH_IRE_ILL; 2647 dst_ipif = dst_ill->ill_ipif; 2648 } 2649 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2650 0, 0, dst_ipif, zoneid, tsl, match_flags); 2651 2652 if (ire != NULL) { 2653 ire_cacheable = B_TRUE; 2654 ire_uinfo = (ire_mp != NULL) ? 2655 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2656 &ire->ire_uinfo; 2657 } else { 2658 if (ire_mp == NULL) { 2659 ire = ire_ftable_lookup_v6( 2660 &tcp->tcp_connp->conn_remv6, 2661 0, 0, 0, dst_ipif, &sire, zoneid, 2662 0, tsl, match_flags); 2663 if (ire == NULL) { 2664 if (dst_ill != NULL) 2665 ill_refrele(dst_ill); 2666 return (0); 2667 } 2668 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2669 &ire->ire_uinfo; 2670 } else { 2671 ire = (ire_t *)ire_mp->b_rptr; 2672 ire_uinfo = 2673 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2674 } 2675 } 2676 if (dst_ill != NULL) 2677 ill_refrele(dst_ill); 2678 2679 ASSERT(ire != NULL); 2680 ASSERT(ire_uinfo != NULL); 2681 2682 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2683 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2684 /* 2685 * ire->ire_mp is non null when ire_mp passed in is used 2686 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2687 */ 2688 if (ire->ire_mp == NULL) 2689 ire_refrele(ire); 2690 if (sire != NULL) 2691 ire_refrele(sire); 2692 return (0); 2693 } 2694 2695 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2696 in6_addr_t src_addr; 2697 2698 /* 2699 * ip_bind_connected_v6() has stored the correct source 2700 * address per IPv6 addr. selection policy in 2701 * conn_src_v6. 2702 */ 2703 src_addr = tcp->tcp_connp->conn_srcv6; 2704 2705 tcp->tcp_ip6h->ip6_src = src_addr; 2706 /* 2707 * Copy of the src addr. in tcp_t is needed 2708 * for the lookup funcs. 2709 */ 2710 tcp->tcp_ip_src_v6 = src_addr; 2711 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2712 &connp->conn_srcv6)); 2713 } 2714 tcp->tcp_localnet = 2715 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2716 } 2717 2718 /* 2719 * This allows applications to fail quickly when connections are made 2720 * to dead hosts. Hosts can be labeled dead by adding a reject route 2721 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2722 */ 2723 if ((ire->ire_flags & RTF_REJECT) && 2724 (ire->ire_flags & RTF_PRIVATE)) 2725 goto error; 2726 2727 /* 2728 * Make use of the cached rtt and rtt_sd values to calculate the 2729 * initial RTO. Note that they are already initialized in 2730 * tcp_init_values(). 2731 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2732 * IP_NEXTHOP, but instead are using the interface ire for the 2733 * nexthop, then we do not use the ire_uinfo from that ire to 2734 * do any initializations. 2735 */ 2736 if (ire_uinfo != NULL) { 2737 if (ire_uinfo->iulp_rtt != 0) { 2738 clock_t rto; 2739 2740 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2741 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2742 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2743 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5); 2744 2745 if (rto > tcp_rexmit_interval_max) { 2746 tcp->tcp_rto = tcp_rexmit_interval_max; 2747 } else if (rto < tcp_rexmit_interval_min) { 2748 tcp->tcp_rto = tcp_rexmit_interval_min; 2749 } else { 2750 tcp->tcp_rto = rto; 2751 } 2752 } 2753 if (ire_uinfo->iulp_ssthresh != 0) 2754 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2755 else 2756 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2757 if (ire_uinfo->iulp_spipe > 0) { 2758 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2759 tcp_max_buf); 2760 if (tcp_snd_lowat_fraction != 0) 2761 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2762 tcp_snd_lowat_fraction; 2763 (void) tcp_maxpsz_set(tcp, B_TRUE); 2764 } 2765 /* 2766 * Note that up till now, acceptor always inherits receive 2767 * window from the listener. But if there is a metrics 2768 * associated with a host, we should use that instead of 2769 * inheriting it from listener. Thus we need to pass this 2770 * info back to the caller. 2771 */ 2772 if (ire_uinfo->iulp_rpipe > 0) { 2773 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, tcp_max_buf); 2774 } 2775 2776 if (ire_uinfo->iulp_rtomax > 0) { 2777 tcp->tcp_second_timer_threshold = 2778 ire_uinfo->iulp_rtomax; 2779 } 2780 2781 /* 2782 * Use the metric option settings, iulp_tstamp_ok and 2783 * iulp_wscale_ok, only for active open. What this means 2784 * is that if the other side uses timestamp or window 2785 * scale option, TCP will also use those options. That 2786 * is for passive open. If the application sets a 2787 * large window, window scale is enabled regardless of 2788 * the value in iulp_wscale_ok. This is the behavior 2789 * since 2.6. So we keep it. 2790 * The only case left in passive open processing is the 2791 * check for SACK. 2792 * For ECN, it should probably be like SACK. But the 2793 * current value is binary, so we treat it like the other 2794 * cases. The metric only controls active open.For passive 2795 * open, the ndd param, tcp_ecn_permitted, controls the 2796 * behavior. 2797 */ 2798 if (!tcp_detached) { 2799 /* 2800 * The if check means that the following can only 2801 * be turned on by the metrics only IRE, but not off. 2802 */ 2803 if (ire_uinfo->iulp_tstamp_ok) 2804 tcp->tcp_snd_ts_ok = B_TRUE; 2805 if (ire_uinfo->iulp_wscale_ok) 2806 tcp->tcp_snd_ws_ok = B_TRUE; 2807 if (ire_uinfo->iulp_sack == 2) 2808 tcp->tcp_snd_sack_ok = B_TRUE; 2809 if (ire_uinfo->iulp_ecn_ok) 2810 tcp->tcp_ecn_ok = B_TRUE; 2811 } else { 2812 /* 2813 * Passive open. 2814 * 2815 * As above, the if check means that SACK can only be 2816 * turned on by the metric only IRE. 2817 */ 2818 if (ire_uinfo->iulp_sack > 0) { 2819 tcp->tcp_snd_sack_ok = B_TRUE; 2820 } 2821 } 2822 } 2823 2824 2825 /* 2826 * XXX: Note that currently, ire_max_frag can be as small as 68 2827 * because of PMTUd. So tcp_mss may go to negative if combined 2828 * length of all those options exceeds 28 bytes. But because 2829 * of the tcp_mss_min check below, we may not have a problem if 2830 * tcp_mss_min is of a reasonable value. The default is 1 so 2831 * the negative problem still exists. And the check defeats PMTUd. 2832 * In fact, if PMTUd finds that the MSS should be smaller than 2833 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2834 * value. 2835 * 2836 * We do not deal with that now. All those problems related to 2837 * PMTUd will be fixed later. 2838 */ 2839 ASSERT(ire->ire_max_frag != 0); 2840 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2841 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2842 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2843 mss = MIN(mss, IPV6_MIN_MTU); 2844 } 2845 } 2846 2847 /* Sanity check for MSS value. */ 2848 if (tcp->tcp_ipversion == IPV4_VERSION) 2849 mss_max = tcp_mss_max_ipv4; 2850 else 2851 mss_max = tcp_mss_max_ipv6; 2852 2853 if (tcp->tcp_ipversion == IPV6_VERSION && 2854 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2855 /* 2856 * After receiving an ICMPv6 "packet too big" message with a 2857 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2858 * will insert a 8-byte fragment header in every packet; we 2859 * reduce the MSS by that amount here. 2860 */ 2861 mss -= sizeof (ip6_frag_t); 2862 } 2863 2864 if (tcp->tcp_ipsec_overhead == 0) 2865 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2866 2867 mss -= tcp->tcp_ipsec_overhead; 2868 2869 if (mss < tcp_mss_min) 2870 mss = tcp_mss_min; 2871 if (mss > mss_max) 2872 mss = mss_max; 2873 2874 /* Note that this is the maximum MSS, excluding all options. */ 2875 tcp->tcp_mss = mss; 2876 2877 /* 2878 * Initialize the ISS here now that we have the full connection ID. 2879 * The RFC 1948 method of initial sequence number generation requires 2880 * knowledge of the full connection ID before setting the ISS. 2881 */ 2882 2883 tcp_iss_init(tcp); 2884 2885 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2886 tcp->tcp_loopback = B_TRUE; 2887 2888 if (tcp->tcp_ipversion == IPV4_VERSION) { 2889 hsp = tcp_hsp_lookup(tcp->tcp_remote); 2890 } else { 2891 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6); 2892 } 2893 2894 if (hsp != NULL) { 2895 /* Only modify if we're going to make them bigger */ 2896 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2897 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2898 if (tcp_snd_lowat_fraction != 0) 2899 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2900 tcp_snd_lowat_fraction; 2901 } 2902 2903 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2904 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2905 } 2906 2907 /* Copy timestamp flag only for active open */ 2908 if (!tcp_detached) 2909 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2910 } 2911 2912 if (sire != NULL) 2913 IRE_REFRELE(sire); 2914 2915 /* 2916 * If we got an IRE_CACHE and an ILL, go through their properties; 2917 * otherwise, this is deferred until later when we have an IRE_CACHE. 2918 */ 2919 if (tcp->tcp_loopback || 2920 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2921 /* 2922 * For incoming, see if this tcp may be MDT-capable. For 2923 * outgoing, this process has been taken care of through 2924 * tcp_rput_other. 2925 */ 2926 tcp_ire_ill_check(tcp, ire, ill, incoming); 2927 tcp->tcp_ire_ill_check_done = B_TRUE; 2928 } 2929 2930 mutex_enter(&connp->conn_lock); 2931 /* 2932 * Make sure that conn is not marked incipient 2933 * for incoming connections. A blind 2934 * removal of incipient flag is cheaper than 2935 * check and removal. 2936 */ 2937 connp->conn_state_flags &= ~CONN_INCIPIENT; 2938 2939 /* Must not cache forwarding table routes. */ 2940 if (ire_cacheable) { 2941 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2942 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2943 connp->conn_ire_cache = ire; 2944 IRE_UNTRACE_REF(ire); 2945 rw_exit(&ire->ire_bucket->irb_lock); 2946 mutex_exit(&connp->conn_lock); 2947 return (1); 2948 } 2949 rw_exit(&ire->ire_bucket->irb_lock); 2950 } 2951 mutex_exit(&connp->conn_lock); 2952 2953 if (ire->ire_mp == NULL) 2954 ire_refrele(ire); 2955 return (1); 2956 2957 error: 2958 if (ire->ire_mp == NULL) 2959 ire_refrele(ire); 2960 if (sire != NULL) 2961 ire_refrele(sire); 2962 return (0); 2963 } 2964 2965 /* 2966 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 2967 * O_T_BIND_REQ/T_BIND_REQ message. 2968 */ 2969 static void 2970 tcp_bind(tcp_t *tcp, mblk_t *mp) 2971 { 2972 sin_t *sin; 2973 sin6_t *sin6; 2974 mblk_t *mp1; 2975 in_port_t requested_port; 2976 in_port_t allocated_port; 2977 struct T_bind_req *tbr; 2978 boolean_t bind_to_req_port_only; 2979 boolean_t backlog_update = B_FALSE; 2980 boolean_t user_specified; 2981 in6_addr_t v6addr; 2982 ipaddr_t v4addr; 2983 uint_t origipversion; 2984 int err; 2985 queue_t *q = tcp->tcp_wq; 2986 conn_t *connp; 2987 mlp_type_t addrtype, mlptype; 2988 zone_t *zone; 2989 cred_t *cr; 2990 in_port_t mlp_port; 2991 2992 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 2993 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 2994 if (tcp->tcp_debug) { 2995 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 2996 "tcp_bind: bad req, len %u", 2997 (uint_t)(mp->b_wptr - mp->b_rptr)); 2998 } 2999 tcp_err_ack(tcp, mp, TPROTO, 0); 3000 return; 3001 } 3002 /* Make sure the largest address fits */ 3003 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3004 if (mp1 == NULL) { 3005 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3006 return; 3007 } 3008 mp = mp1; 3009 tbr = (struct T_bind_req *)mp->b_rptr; 3010 if (tcp->tcp_state >= TCPS_BOUND) { 3011 if ((tcp->tcp_state == TCPS_BOUND || 3012 tcp->tcp_state == TCPS_LISTEN) && 3013 tcp->tcp_conn_req_max != tbr->CONIND_number && 3014 tbr->CONIND_number > 0) { 3015 /* 3016 * Handle listen() increasing CONIND_number. 3017 * This is more "liberal" then what the TPI spec 3018 * requires but is needed to avoid a t_unbind 3019 * when handling listen() since the port number 3020 * might be "stolen" between the unbind and bind. 3021 */ 3022 backlog_update = B_TRUE; 3023 goto do_bind; 3024 } 3025 if (tcp->tcp_debug) { 3026 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3027 "tcp_bind: bad state, %d", tcp->tcp_state); 3028 } 3029 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3030 return; 3031 } 3032 origipversion = tcp->tcp_ipversion; 3033 3034 switch (tbr->ADDR_length) { 3035 case 0: /* request for a generic port */ 3036 tbr->ADDR_offset = sizeof (struct T_bind_req); 3037 if (tcp->tcp_family == AF_INET) { 3038 tbr->ADDR_length = sizeof (sin_t); 3039 sin = (sin_t *)&tbr[1]; 3040 *sin = sin_null; 3041 sin->sin_family = AF_INET; 3042 mp->b_wptr = (uchar_t *)&sin[1]; 3043 tcp->tcp_ipversion = IPV4_VERSION; 3044 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3045 } else { 3046 ASSERT(tcp->tcp_family == AF_INET6); 3047 tbr->ADDR_length = sizeof (sin6_t); 3048 sin6 = (sin6_t *)&tbr[1]; 3049 *sin6 = sin6_null; 3050 sin6->sin6_family = AF_INET6; 3051 mp->b_wptr = (uchar_t *)&sin6[1]; 3052 tcp->tcp_ipversion = IPV6_VERSION; 3053 V6_SET_ZERO(v6addr); 3054 } 3055 requested_port = 0; 3056 break; 3057 3058 case sizeof (sin_t): /* Complete IPv4 address */ 3059 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3060 sizeof (sin_t)); 3061 if (sin == NULL || !OK_32PTR((char *)sin)) { 3062 if (tcp->tcp_debug) { 3063 (void) strlog(TCP_MOD_ID, 0, 1, 3064 SL_ERROR|SL_TRACE, 3065 "tcp_bind: bad address parameter, " 3066 "offset %d, len %d", 3067 tbr->ADDR_offset, tbr->ADDR_length); 3068 } 3069 tcp_err_ack(tcp, mp, TPROTO, 0); 3070 return; 3071 } 3072 /* 3073 * With sockets sockfs will accept bogus sin_family in 3074 * bind() and replace it with the family used in the socket 3075 * call. 3076 */ 3077 if (sin->sin_family != AF_INET || 3078 tcp->tcp_family != AF_INET) { 3079 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3080 return; 3081 } 3082 requested_port = ntohs(sin->sin_port); 3083 tcp->tcp_ipversion = IPV4_VERSION; 3084 v4addr = sin->sin_addr.s_addr; 3085 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3086 break; 3087 3088 case sizeof (sin6_t): /* Complete IPv6 address */ 3089 sin6 = (sin6_t *)mi_offset_param(mp, 3090 tbr->ADDR_offset, sizeof (sin6_t)); 3091 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3092 if (tcp->tcp_debug) { 3093 (void) strlog(TCP_MOD_ID, 0, 1, 3094 SL_ERROR|SL_TRACE, 3095 "tcp_bind: bad IPv6 address parameter, " 3096 "offset %d, len %d", tbr->ADDR_offset, 3097 tbr->ADDR_length); 3098 } 3099 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3100 return; 3101 } 3102 if (sin6->sin6_family != AF_INET6 || 3103 tcp->tcp_family != AF_INET6) { 3104 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3105 return; 3106 } 3107 requested_port = ntohs(sin6->sin6_port); 3108 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3109 IPV4_VERSION : IPV6_VERSION; 3110 v6addr = sin6->sin6_addr; 3111 break; 3112 3113 default: 3114 if (tcp->tcp_debug) { 3115 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3116 "tcp_bind: bad address length, %d", 3117 tbr->ADDR_length); 3118 } 3119 tcp_err_ack(tcp, mp, TBADADDR, 0); 3120 return; 3121 } 3122 tcp->tcp_bound_source_v6 = v6addr; 3123 3124 /* Check for change in ipversion */ 3125 if (origipversion != tcp->tcp_ipversion) { 3126 ASSERT(tcp->tcp_family == AF_INET6); 3127 err = tcp->tcp_ipversion == IPV6_VERSION ? 3128 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3129 if (err) { 3130 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3131 return; 3132 } 3133 } 3134 3135 /* 3136 * Initialize family specific fields. Copy of the src addr. 3137 * in tcp_t is needed for the lookup funcs. 3138 */ 3139 if (tcp->tcp_ipversion == IPV6_VERSION) { 3140 tcp->tcp_ip6h->ip6_src = v6addr; 3141 } else { 3142 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3143 } 3144 tcp->tcp_ip_src_v6 = v6addr; 3145 3146 /* 3147 * For O_T_BIND_REQ: 3148 * Verify that the target port/addr is available, or choose 3149 * another. 3150 * For T_BIND_REQ: 3151 * Verify that the target port/addr is available or fail. 3152 * In both cases when it succeeds the tcp is inserted in the 3153 * bind hash table. This ensures that the operation is atomic 3154 * under the lock on the hash bucket. 3155 */ 3156 bind_to_req_port_only = requested_port != 0 && 3157 tbr->PRIM_type != O_T_BIND_REQ; 3158 /* 3159 * Get a valid port (within the anonymous range and should not 3160 * be a privileged one) to use if the user has not given a port. 3161 * If multiple threads are here, they may all start with 3162 * with the same initial port. But, it should be fine as long as 3163 * tcp_bindi will ensure that no two threads will be assigned 3164 * the same port. 3165 * 3166 * NOTE: XXX If a privileged process asks for an anonymous port, we 3167 * still check for ports only in the range > tcp_smallest_non_priv_port, 3168 * unless TCP_ANONPRIVBIND option is set. 3169 */ 3170 mlptype = mlptSingle; 3171 mlp_port = requested_port; 3172 if (requested_port == 0) { 3173 requested_port = tcp->tcp_anon_priv_bind ? 3174 tcp_get_next_priv_port(tcp) : 3175 tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 3176 if (requested_port == 0) { 3177 tcp_err_ack(tcp, mp, TNOADDR, 0); 3178 return; 3179 } 3180 user_specified = B_FALSE; 3181 3182 /* 3183 * If the user went through one of the RPC interfaces to create 3184 * this socket and RPC is MLP in this zone, then give him an 3185 * anonymous MLP. 3186 */ 3187 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3188 connp = tcp->tcp_connp; 3189 if (connp->conn_anon_mlp && is_system_labeled()) { 3190 zone = crgetzone(cr); 3191 addrtype = tsol_mlp_addr_type(zone->zone_id, 3192 IPV6_VERSION, &v6addr); 3193 if (addrtype == mlptSingle) { 3194 tcp_err_ack(tcp, mp, TNOADDR, 0); 3195 return; 3196 } 3197 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3198 PMAPPORT, addrtype); 3199 mlp_port = PMAPPORT; 3200 } 3201 } else { 3202 int i; 3203 boolean_t priv = B_FALSE; 3204 3205 /* 3206 * If the requested_port is in the well-known privileged range, 3207 * verify that the stream was opened by a privileged user. 3208 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3209 * but instead the code relies on: 3210 * - the fact that the address of the array and its size never 3211 * changes 3212 * - the atomic assignment of the elements of the array 3213 */ 3214 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3215 if (requested_port < tcp_smallest_nonpriv_port) { 3216 priv = B_TRUE; 3217 } else { 3218 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 3219 if (requested_port == 3220 tcp_g_epriv_ports[i]) { 3221 priv = B_TRUE; 3222 break; 3223 } 3224 } 3225 } 3226 if (priv) { 3227 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3228 if (tcp->tcp_debug) { 3229 (void) strlog(TCP_MOD_ID, 0, 1, 3230 SL_ERROR|SL_TRACE, 3231 "tcp_bind: no priv for port %d", 3232 requested_port); 3233 } 3234 tcp_err_ack(tcp, mp, TACCES, 0); 3235 return; 3236 } 3237 } 3238 user_specified = B_TRUE; 3239 3240 connp = tcp->tcp_connp; 3241 if (is_system_labeled()) { 3242 zone = crgetzone(cr); 3243 addrtype = tsol_mlp_addr_type(zone->zone_id, 3244 IPV6_VERSION, &v6addr); 3245 if (addrtype == mlptSingle) { 3246 tcp_err_ack(tcp, mp, TNOADDR, 0); 3247 return; 3248 } 3249 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3250 requested_port, addrtype); 3251 } 3252 } 3253 3254 if (mlptype != mlptSingle) { 3255 if (secpolicy_net_bindmlp(cr) != 0) { 3256 if (tcp->tcp_debug) { 3257 (void) strlog(TCP_MOD_ID, 0, 1, 3258 SL_ERROR|SL_TRACE, 3259 "tcp_bind: no priv for multilevel port %d", 3260 requested_port); 3261 } 3262 tcp_err_ack(tcp, mp, TACCES, 0); 3263 return; 3264 } 3265 3266 /* 3267 * If we're specifically binding a shared IP address and the 3268 * port is MLP on shared addresses, then check to see if this 3269 * zone actually owns the MLP. Reject if not. 3270 */ 3271 if (mlptype == mlptShared && addrtype == mlptShared) { 3272 zoneid_t mlpzone; 3273 3274 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3275 htons(mlp_port)); 3276 if (connp->conn_zoneid != mlpzone) { 3277 if (tcp->tcp_debug) { 3278 (void) strlog(TCP_MOD_ID, 0, 1, 3279 SL_ERROR|SL_TRACE, 3280 "tcp_bind: attempt to bind port " 3281 "%d on shared addr in zone %d " 3282 "(should be %d)", 3283 mlp_port, connp->conn_zoneid, 3284 mlpzone); 3285 } 3286 tcp_err_ack(tcp, mp, TACCES, 0); 3287 return; 3288 } 3289 } 3290 3291 if (!user_specified) { 3292 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3293 requested_port, B_TRUE); 3294 if (err != 0) { 3295 if (tcp->tcp_debug) { 3296 (void) strlog(TCP_MOD_ID, 0, 1, 3297 SL_ERROR|SL_TRACE, 3298 "tcp_bind: cannot establish anon " 3299 "MLP for port %d", 3300 requested_port); 3301 } 3302 tcp_err_ack(tcp, mp, TSYSERR, err); 3303 return; 3304 } 3305 connp->conn_anon_port = B_TRUE; 3306 } 3307 connp->conn_mlp_type = mlptype; 3308 } 3309 3310 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3311 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3312 3313 if (allocated_port == 0) { 3314 connp->conn_mlp_type = mlptSingle; 3315 if (connp->conn_anon_port) { 3316 connp->conn_anon_port = B_FALSE; 3317 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3318 requested_port, B_FALSE); 3319 } 3320 if (bind_to_req_port_only) { 3321 if (tcp->tcp_debug) { 3322 (void) strlog(TCP_MOD_ID, 0, 1, 3323 SL_ERROR|SL_TRACE, 3324 "tcp_bind: requested addr busy"); 3325 } 3326 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3327 } else { 3328 /* If we are out of ports, fail the bind. */ 3329 if (tcp->tcp_debug) { 3330 (void) strlog(TCP_MOD_ID, 0, 1, 3331 SL_ERROR|SL_TRACE, 3332 "tcp_bind: out of ports?"); 3333 } 3334 tcp_err_ack(tcp, mp, TNOADDR, 0); 3335 } 3336 return; 3337 } 3338 ASSERT(tcp->tcp_state == TCPS_BOUND); 3339 do_bind: 3340 if (!backlog_update) { 3341 if (tcp->tcp_family == AF_INET) 3342 sin->sin_port = htons(allocated_port); 3343 else 3344 sin6->sin6_port = htons(allocated_port); 3345 } 3346 if (tcp->tcp_family == AF_INET) { 3347 if (tbr->CONIND_number != 0) { 3348 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3349 sizeof (sin_t)); 3350 } else { 3351 /* Just verify the local IP address */ 3352 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3353 } 3354 } else { 3355 if (tbr->CONIND_number != 0) { 3356 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3357 sizeof (sin6_t)); 3358 } else { 3359 /* Just verify the local IP address */ 3360 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3361 IPV6_ADDR_LEN); 3362 } 3363 } 3364 if (mp1 == NULL) { 3365 if (connp->conn_anon_port) { 3366 connp->conn_anon_port = B_FALSE; 3367 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3368 requested_port, B_FALSE); 3369 } 3370 connp->conn_mlp_type = mlptSingle; 3371 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3372 return; 3373 } 3374 3375 tbr->PRIM_type = T_BIND_ACK; 3376 mp->b_datap->db_type = M_PCPROTO; 3377 3378 /* Chain in the reply mp for tcp_rput() */ 3379 mp1->b_cont = mp; 3380 mp = mp1; 3381 3382 tcp->tcp_conn_req_max = tbr->CONIND_number; 3383 if (tcp->tcp_conn_req_max) { 3384 if (tcp->tcp_conn_req_max < tcp_conn_req_min) 3385 tcp->tcp_conn_req_max = tcp_conn_req_min; 3386 if (tcp->tcp_conn_req_max > tcp_conn_req_max_q) 3387 tcp->tcp_conn_req_max = tcp_conn_req_max_q; 3388 /* 3389 * If this is a listener, do not reset the eager list 3390 * and other stuffs. Note that we don't check if the 3391 * existing eager list meets the new tcp_conn_req_max 3392 * requirement. 3393 */ 3394 if (tcp->tcp_state != TCPS_LISTEN) { 3395 tcp->tcp_state = TCPS_LISTEN; 3396 /* Initialize the chain. Don't need the eager_lock */ 3397 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3398 tcp->tcp_second_ctimer_threshold = 3399 tcp_ip_abort_linterval; 3400 } 3401 } 3402 3403 /* 3404 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3405 * processing continues in tcp_rput_other(). 3406 */ 3407 if (tcp->tcp_family == AF_INET6) { 3408 ASSERT(tcp->tcp_connp->conn_af_isv6); 3409 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3410 } else { 3411 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3412 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3413 } 3414 /* 3415 * If the bind cannot complete immediately 3416 * IP will arrange to call tcp_rput_other 3417 * when the bind completes. 3418 */ 3419 if (mp != NULL) { 3420 tcp_rput_other(tcp, mp); 3421 } else { 3422 /* 3423 * Bind will be resumed later. Need to ensure 3424 * that conn doesn't disappear when that happens. 3425 * This will be decremented in ip_resume_tcp_bind(). 3426 */ 3427 CONN_INC_REF(tcp->tcp_connp); 3428 } 3429 } 3430 3431 3432 /* 3433 * If the "bind_to_req_port_only" parameter is set, if the requested port 3434 * number is available, return it, If not return 0 3435 * 3436 * If "bind_to_req_port_only" parameter is not set and 3437 * If the requested port number is available, return it. If not, return 3438 * the first anonymous port we happen across. If no anonymous ports are 3439 * available, return 0. addr is the requested local address, if any. 3440 * 3441 * In either case, when succeeding update the tcp_t to record the port number 3442 * and insert it in the bind hash table. 3443 * 3444 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3445 * without setting SO_REUSEADDR. This is needed so that they 3446 * can be viewed as two independent transport protocols. 3447 */ 3448 static in_port_t 3449 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3450 int reuseaddr, boolean_t quick_connect, 3451 boolean_t bind_to_req_port_only, boolean_t user_specified) 3452 { 3453 /* number of times we have run around the loop */ 3454 int count = 0; 3455 /* maximum number of times to run around the loop */ 3456 int loopmax; 3457 conn_t *connp = tcp->tcp_connp; 3458 zoneid_t zoneid = connp->conn_zoneid; 3459 3460 /* 3461 * Lookup for free addresses is done in a loop and "loopmax" 3462 * influences how long we spin in the loop 3463 */ 3464 if (bind_to_req_port_only) { 3465 /* 3466 * If the requested port is busy, don't bother to look 3467 * for a new one. Setting loop maximum count to 1 has 3468 * that effect. 3469 */ 3470 loopmax = 1; 3471 } else { 3472 /* 3473 * If the requested port is busy, look for a free one 3474 * in the anonymous port range. 3475 * Set loopmax appropriately so that one does not look 3476 * forever in the case all of the anonymous ports are in use. 3477 */ 3478 if (tcp->tcp_anon_priv_bind) { 3479 /* 3480 * loopmax = 3481 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3482 */ 3483 loopmax = IPPORT_RESERVED - tcp_min_anonpriv_port; 3484 } else { 3485 loopmax = (tcp_largest_anon_port - 3486 tcp_smallest_anon_port + 1); 3487 } 3488 } 3489 do { 3490 uint16_t lport; 3491 tf_t *tbf; 3492 tcp_t *ltcp; 3493 conn_t *lconnp; 3494 3495 lport = htons(port); 3496 3497 /* 3498 * Ensure that the tcp_t is not currently in the bind hash. 3499 * Hold the lock on the hash bucket to ensure that 3500 * the duplicate check plus the insertion is an atomic 3501 * operation. 3502 * 3503 * This function does an inline lookup on the bind hash list 3504 * Make sure that we access only members of tcp_t 3505 * and that we don't look at tcp_tcp, since we are not 3506 * doing a CONN_INC_REF. 3507 */ 3508 tcp_bind_hash_remove(tcp); 3509 tbf = &tcp_bind_fanout[TCP_BIND_HASH(lport)]; 3510 mutex_enter(&tbf->tf_lock); 3511 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3512 ltcp = ltcp->tcp_bind_hash) { 3513 boolean_t not_socket; 3514 boolean_t exclbind; 3515 3516 if (lport != ltcp->tcp_lport) 3517 continue; 3518 3519 lconnp = ltcp->tcp_connp; 3520 3521 /* 3522 * On a labeled system, we must treat bindings to ports 3523 * on shared IP addresses by sockets with MAC exemption 3524 * privilege as being in all zones, as there's 3525 * otherwise no way to identify the right receiver. 3526 */ 3527 if (!IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) && 3528 !lconnp->conn_mac_exempt && 3529 !connp->conn_mac_exempt) 3530 continue; 3531 3532 /* 3533 * If TCP_EXCLBIND is set for either the bound or 3534 * binding endpoint, the semantics of bind 3535 * is changed according to the following. 3536 * 3537 * spec = specified address (v4 or v6) 3538 * unspec = unspecified address (v4 or v6) 3539 * A = specified addresses are different for endpoints 3540 * 3541 * bound bind to allowed 3542 * ------------------------------------- 3543 * unspec unspec no 3544 * unspec spec no 3545 * spec unspec no 3546 * spec spec yes if A 3547 * 3548 * For labeled systems, SO_MAC_EXEMPT behaves the same 3549 * as TCP_EXCLBIND, except that zoneid is ignored. 3550 * 3551 * Note: 3552 * 3553 * 1. Because of TLI semantics, an endpoint can go 3554 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3555 * TCPS_BOUND, depending on whether it is originally 3556 * a listener or not. That is why we need to check 3557 * for states greater than or equal to TCPS_BOUND 3558 * here. 3559 * 3560 * 2. Ideally, we should only check for state equals 3561 * to TCPS_LISTEN. And the following check should be 3562 * added. 3563 * 3564 * if (ltcp->tcp_state == TCPS_LISTEN || 3565 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3566 * ... 3567 * } 3568 * 3569 * The semantics will be changed to this. If the 3570 * endpoint on the list is in state not equal to 3571 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3572 * set, let the bind succeed. 3573 * 3574 * Because of (1), we cannot do that for TLI 3575 * endpoints. But we can do that for socket endpoints. 3576 * If in future, we can change this going back 3577 * semantics, we can use the above check for TLI also. 3578 */ 3579 not_socket = !(TCP_IS_SOCKET(ltcp) && 3580 TCP_IS_SOCKET(tcp)); 3581 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3582 3583 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3584 (exclbind && (not_socket || 3585 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3586 if (V6_OR_V4_INADDR_ANY( 3587 ltcp->tcp_bound_source_v6) || 3588 V6_OR_V4_INADDR_ANY(*laddr) || 3589 IN6_ARE_ADDR_EQUAL(laddr, 3590 <cp->tcp_bound_source_v6)) { 3591 break; 3592 } 3593 continue; 3594 } 3595 3596 /* 3597 * Check ipversion to allow IPv4 and IPv6 sockets to 3598 * have disjoint port number spaces, if *_EXCLBIND 3599 * is not set and only if the application binds to a 3600 * specific port. We use the same autoassigned port 3601 * number space for IPv4 and IPv6 sockets. 3602 */ 3603 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3604 bind_to_req_port_only) 3605 continue; 3606 3607 /* 3608 * Ideally, we should make sure that the source 3609 * address, remote address, and remote port in the 3610 * four tuple for this tcp-connection is unique. 3611 * However, trying to find out the local source 3612 * address would require too much code duplication 3613 * with IP, since IP needs needs to have that code 3614 * to support userland TCP implementations. 3615 */ 3616 if (quick_connect && 3617 (ltcp->tcp_state > TCPS_LISTEN) && 3618 ((tcp->tcp_fport != ltcp->tcp_fport) || 3619 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3620 <cp->tcp_remote_v6))) 3621 continue; 3622 3623 if (!reuseaddr) { 3624 /* 3625 * No socket option SO_REUSEADDR. 3626 * If existing port is bound to 3627 * a non-wildcard IP address 3628 * and the requesting stream is 3629 * bound to a distinct 3630 * different IP addresses 3631 * (non-wildcard, also), keep 3632 * going. 3633 */ 3634 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3635 !V6_OR_V4_INADDR_ANY( 3636 ltcp->tcp_bound_source_v6) && 3637 !IN6_ARE_ADDR_EQUAL(laddr, 3638 <cp->tcp_bound_source_v6)) 3639 continue; 3640 if (ltcp->tcp_state >= TCPS_BOUND) { 3641 /* 3642 * This port is being used and 3643 * its state is >= TCPS_BOUND, 3644 * so we can't bind to it. 3645 */ 3646 break; 3647 } 3648 } else { 3649 /* 3650 * socket option SO_REUSEADDR is set on the 3651 * binding tcp_t. 3652 * 3653 * If two streams are bound to 3654 * same IP address or both addr 3655 * and bound source are wildcards 3656 * (INADDR_ANY), we want to stop 3657 * searching. 3658 * We have found a match of IP source 3659 * address and source port, which is 3660 * refused regardless of the 3661 * SO_REUSEADDR setting, so we break. 3662 */ 3663 if (IN6_ARE_ADDR_EQUAL(laddr, 3664 <cp->tcp_bound_source_v6) && 3665 (ltcp->tcp_state == TCPS_LISTEN || 3666 ltcp->tcp_state == TCPS_BOUND)) 3667 break; 3668 } 3669 } 3670 if (ltcp != NULL) { 3671 /* The port number is busy */ 3672 mutex_exit(&tbf->tf_lock); 3673 } else { 3674 /* 3675 * This port is ours. Insert in fanout and mark as 3676 * bound to prevent others from getting the port 3677 * number. 3678 */ 3679 tcp->tcp_state = TCPS_BOUND; 3680 tcp->tcp_lport = htons(port); 3681 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3682 3683 ASSERT(&tcp_bind_fanout[TCP_BIND_HASH( 3684 tcp->tcp_lport)] == tbf); 3685 tcp_bind_hash_insert(tbf, tcp, 1); 3686 3687 mutex_exit(&tbf->tf_lock); 3688 3689 /* 3690 * We don't want tcp_next_port_to_try to "inherit" 3691 * a port number supplied by the user in a bind. 3692 */ 3693 if (user_specified) 3694 return (port); 3695 3696 /* 3697 * This is the only place where tcp_next_port_to_try 3698 * is updated. After the update, it may or may not 3699 * be in the valid range. 3700 */ 3701 if (!tcp->tcp_anon_priv_bind) 3702 tcp_next_port_to_try = port + 1; 3703 return (port); 3704 } 3705 3706 if (tcp->tcp_anon_priv_bind) { 3707 port = tcp_get_next_priv_port(tcp); 3708 } else { 3709 if (count == 0 && user_specified) { 3710 /* 3711 * We may have to return an anonymous port. So 3712 * get one to start with. 3713 */ 3714 port = 3715 tcp_update_next_port(tcp_next_port_to_try, 3716 tcp, B_TRUE); 3717 user_specified = B_FALSE; 3718 } else { 3719 port = tcp_update_next_port(port + 1, tcp, 3720 B_FALSE); 3721 } 3722 } 3723 if (port == 0) 3724 break; 3725 3726 /* 3727 * Don't let this loop run forever in the case where 3728 * all of the anonymous ports are in use. 3729 */ 3730 } while (++count < loopmax); 3731 return (0); 3732 } 3733 3734 /* 3735 * We are dying for some reason. Try to do it gracefully. (May be called 3736 * as writer.) 3737 * 3738 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3739 * done by a service procedure). 3740 * TBD - Should the return value distinguish between the tcp_t being 3741 * freed and it being reinitialized? 3742 */ 3743 static int 3744 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3745 { 3746 mblk_t *mp; 3747 queue_t *q; 3748 3749 TCP_CLD_STAT(tag); 3750 3751 #if TCP_TAG_CLEAN_DEATH 3752 tcp->tcp_cleandeathtag = tag; 3753 #endif 3754 3755 if (tcp->tcp_fused) 3756 tcp_unfuse(tcp); 3757 3758 if (tcp->tcp_linger_tid != 0 && 3759 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3760 tcp_stop_lingering(tcp); 3761 } 3762 3763 ASSERT(tcp != NULL); 3764 ASSERT((tcp->tcp_family == AF_INET && 3765 tcp->tcp_ipversion == IPV4_VERSION) || 3766 (tcp->tcp_family == AF_INET6 && 3767 (tcp->tcp_ipversion == IPV4_VERSION || 3768 tcp->tcp_ipversion == IPV6_VERSION))); 3769 3770 if (TCP_IS_DETACHED(tcp)) { 3771 if (tcp->tcp_hard_binding) { 3772 /* 3773 * Its an eager that we are dealing with. We close the 3774 * eager but in case a conn_ind has already gone to the 3775 * listener, let tcp_accept_finish() send a discon_ind 3776 * to the listener and drop the last reference. If the 3777 * listener doesn't even know about the eager i.e. the 3778 * conn_ind hasn't gone up, blow away the eager and drop 3779 * the last reference as well. If the conn_ind has gone 3780 * up, state should be BOUND. tcp_accept_finish 3781 * will figure out that the connection has received a 3782 * RST and will send a DISCON_IND to the application. 3783 */ 3784 tcp_closei_local(tcp); 3785 if (tcp->tcp_conn.tcp_eager_conn_ind != NULL) { 3786 CONN_DEC_REF(tcp->tcp_connp); 3787 } else { 3788 tcp->tcp_state = TCPS_BOUND; 3789 } 3790 } else { 3791 tcp_close_detached(tcp); 3792 } 3793 return (0); 3794 } 3795 3796 TCP_STAT(tcp_clean_death_nondetached); 3797 3798 /* 3799 * If T_ORDREL_IND has not been sent yet (done when service routine 3800 * is run) postpone cleaning up the endpoint until service routine 3801 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3802 * client_errno since tcp_close uses the client_errno field. 3803 */ 3804 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3805 if (err != 0) 3806 tcp->tcp_client_errno = err; 3807 3808 tcp->tcp_deferred_clean_death = B_TRUE; 3809 return (-1); 3810 } 3811 3812 q = tcp->tcp_rq; 3813 3814 /* Trash all inbound data */ 3815 flushq(q, FLUSHALL); 3816 3817 /* 3818 * If we are at least part way open and there is error 3819 * (err==0 implies no error) 3820 * notify our client by a T_DISCON_IND. 3821 */ 3822 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3823 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3824 !TCP_IS_SOCKET(tcp)) { 3825 /* 3826 * Send M_FLUSH according to TPI. Because sockets will 3827 * (and must) ignore FLUSHR we do that only for TPI 3828 * endpoints and sockets in STREAMS mode. 3829 */ 3830 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3831 } 3832 if (tcp->tcp_debug) { 3833 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3834 "tcp_clean_death: discon err %d", err); 3835 } 3836 mp = mi_tpi_discon_ind(NULL, err, 0); 3837 if (mp != NULL) { 3838 putnext(q, mp); 3839 } else { 3840 if (tcp->tcp_debug) { 3841 (void) strlog(TCP_MOD_ID, 0, 1, 3842 SL_ERROR|SL_TRACE, 3843 "tcp_clean_death, sending M_ERROR"); 3844 } 3845 (void) putnextctl1(q, M_ERROR, EPROTO); 3846 } 3847 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3848 /* SYN_SENT or SYN_RCVD */ 3849 BUMP_MIB(&tcp_mib, tcpAttemptFails); 3850 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3851 /* ESTABLISHED or CLOSE_WAIT */ 3852 BUMP_MIB(&tcp_mib, tcpEstabResets); 3853 } 3854 } 3855 3856 tcp_reinit(tcp); 3857 return (-1); 3858 } 3859 3860 /* 3861 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3862 * to expire, stop the wait and finish the close. 3863 */ 3864 static void 3865 tcp_stop_lingering(tcp_t *tcp) 3866 { 3867 clock_t delta = 0; 3868 3869 tcp->tcp_linger_tid = 0; 3870 if (tcp->tcp_state > TCPS_LISTEN) { 3871 tcp_acceptor_hash_remove(tcp); 3872 if (tcp->tcp_flow_stopped) { 3873 tcp_clrqfull(tcp); 3874 } 3875 3876 if (tcp->tcp_timer_tid != 0) { 3877 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3878 tcp->tcp_timer_tid = 0; 3879 } 3880 /* 3881 * Need to cancel those timers which will not be used when 3882 * TCP is detached. This has to be done before the tcp_wq 3883 * is set to the global queue. 3884 */ 3885 tcp_timers_stop(tcp); 3886 3887 3888 tcp->tcp_detached = B_TRUE; 3889 tcp->tcp_rq = tcp_g_q; 3890 tcp->tcp_wq = WR(tcp_g_q); 3891 3892 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3893 tcp_time_wait_append(tcp); 3894 TCP_DBGSTAT(tcp_detach_time_wait); 3895 goto finish; 3896 } 3897 3898 /* 3899 * If delta is zero the timer event wasn't executed and was 3900 * successfully canceled. In this case we need to restart it 3901 * with the minimal delta possible. 3902 */ 3903 if (delta >= 0) { 3904 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3905 delta ? delta : 1); 3906 } 3907 } else { 3908 tcp_closei_local(tcp); 3909 CONN_DEC_REF(tcp->tcp_connp); 3910 } 3911 finish: 3912 /* Signal closing thread that it can complete close */ 3913 mutex_enter(&tcp->tcp_closelock); 3914 tcp->tcp_detached = B_TRUE; 3915 tcp->tcp_rq = tcp_g_q; 3916 tcp->tcp_wq = WR(tcp_g_q); 3917 tcp->tcp_closed = 1; 3918 cv_signal(&tcp->tcp_closecv); 3919 mutex_exit(&tcp->tcp_closelock); 3920 } 3921 3922 /* 3923 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3924 * expires. 3925 */ 3926 static void 3927 tcp_close_linger_timeout(void *arg) 3928 { 3929 conn_t *connp = (conn_t *)arg; 3930 tcp_t *tcp = connp->conn_tcp; 3931 3932 tcp->tcp_client_errno = ETIMEDOUT; 3933 tcp_stop_lingering(tcp); 3934 } 3935 3936 static int 3937 tcp_close(queue_t *q, int flags) 3938 { 3939 conn_t *connp = Q_TO_CONN(q); 3940 tcp_t *tcp = connp->conn_tcp; 3941 mblk_t *mp = &tcp->tcp_closemp; 3942 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3943 3944 ASSERT(WR(q)->q_next == NULL); 3945 ASSERT(connp->conn_ref >= 2); 3946 ASSERT((connp->conn_flags & IPCL_TCPMOD) == 0); 3947 3948 /* 3949 * We are being closed as /dev/tcp or /dev/tcp6. 3950 * 3951 * Mark the conn as closing. ill_pending_mp_add will not 3952 * add any mp to the pending mp list, after this conn has 3953 * started closing. Same for sq_pending_mp_add 3954 */ 3955 mutex_enter(&connp->conn_lock); 3956 connp->conn_state_flags |= CONN_CLOSING; 3957 if (connp->conn_oper_pending_ill != NULL) 3958 conn_ioctl_cleanup_reqd = B_TRUE; 3959 CONN_INC_REF_LOCKED(connp); 3960 mutex_exit(&connp->conn_lock); 3961 tcp->tcp_closeflags = (uint8_t)flags; 3962 ASSERT(connp->conn_ref >= 3); 3963 3964 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 3965 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 3966 3967 mutex_enter(&tcp->tcp_closelock); 3968 3969 while (!tcp->tcp_closed) 3970 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3971 mutex_exit(&tcp->tcp_closelock); 3972 /* 3973 * In the case of listener streams that have eagers in the q or q0 3974 * we wait for the eagers to drop their reference to us. tcp_rq and 3975 * tcp_wq of the eagers point to our queues. By waiting for the 3976 * refcnt to drop to 1, we are sure that the eagers have cleaned 3977 * up their queue pointers and also dropped their references to us. 3978 */ 3979 if (tcp->tcp_wait_for_eagers) { 3980 mutex_enter(&connp->conn_lock); 3981 while (connp->conn_ref != 1) { 3982 cv_wait(&connp->conn_cv, &connp->conn_lock); 3983 } 3984 mutex_exit(&connp->conn_lock); 3985 } 3986 /* 3987 * ioctl cleanup. The mp is queued in the 3988 * ill_pending_mp or in the sq_pending_mp. 3989 */ 3990 if (conn_ioctl_cleanup_reqd) 3991 conn_ioctl_cleanup(connp); 3992 3993 qprocsoff(q); 3994 inet_minor_free(ip_minor_arena, connp->conn_dev); 3995 3996 tcp->tcp_cpid = -1; 3997 3998 /* 3999 * Drop IP's reference on the conn. This is the last reference 4000 * on the connp if the state was less than established. If the 4001 * connection has gone into timewait state, then we will have 4002 * one ref for the TCP and one more ref (total of two) for the 4003 * classifier connected hash list (a timewait connections stays 4004 * in connected hash till closed). 4005 * 4006 * We can't assert the references because there might be other 4007 * transient reference places because of some walkers or queued 4008 * packets in squeue for the timewait state. 4009 */ 4010 CONN_DEC_REF(connp); 4011 q->q_ptr = WR(q)->q_ptr = NULL; 4012 return (0); 4013 } 4014 4015 static int 4016 tcpclose_accept(queue_t *q) 4017 { 4018 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4019 4020 /* 4021 * We had opened an acceptor STREAM for sockfs which is 4022 * now being closed due to some error. 4023 */ 4024 qprocsoff(q); 4025 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 4026 q->q_ptr = WR(q)->q_ptr = NULL; 4027 return (0); 4028 } 4029 4030 4031 /* 4032 * Called by streams close routine via squeues when our client blows off her 4033 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4034 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4035 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4036 * acked. 4037 * 4038 * NOTE: tcp_close potentially returns error when lingering. 4039 * However, the stream head currently does not pass these errors 4040 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4041 * errors to the application (from tsleep()) and not errors 4042 * like ECONNRESET caused by receiving a reset packet. 4043 */ 4044 4045 /* ARGSUSED */ 4046 static void 4047 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4048 { 4049 char *msg; 4050 conn_t *connp = (conn_t *)arg; 4051 tcp_t *tcp = connp->conn_tcp; 4052 clock_t delta = 0; 4053 4054 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4055 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4056 4057 /* Cancel any pending timeout */ 4058 if (tcp->tcp_ordrelid != 0) { 4059 if (tcp->tcp_timeout) { 4060 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4061 } 4062 tcp->tcp_ordrelid = 0; 4063 tcp->tcp_timeout = B_FALSE; 4064 } 4065 4066 mutex_enter(&tcp->tcp_eager_lock); 4067 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4068 /* Cleanup for listener */ 4069 tcp_eager_cleanup(tcp, 0); 4070 tcp->tcp_wait_for_eagers = 1; 4071 } 4072 mutex_exit(&tcp->tcp_eager_lock); 4073 4074 connp->conn_mdt_ok = B_FALSE; 4075 tcp->tcp_mdt = B_FALSE; 4076 4077 msg = NULL; 4078 switch (tcp->tcp_state) { 4079 case TCPS_CLOSED: 4080 case TCPS_IDLE: 4081 case TCPS_BOUND: 4082 case TCPS_LISTEN: 4083 break; 4084 case TCPS_SYN_SENT: 4085 msg = "tcp_close, during connect"; 4086 break; 4087 case TCPS_SYN_RCVD: 4088 /* 4089 * Close during the connect 3-way handshake 4090 * but here there may or may not be pending data 4091 * already on queue. Process almost same as in 4092 * the ESTABLISHED state. 4093 */ 4094 /* FALLTHRU */ 4095 default: 4096 if (tcp->tcp_fused) 4097 tcp_unfuse(tcp); 4098 4099 /* 4100 * If SO_LINGER has set a zero linger time, abort the 4101 * connection with a reset. 4102 */ 4103 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4104 msg = "tcp_close, zero lingertime"; 4105 break; 4106 } 4107 4108 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4109 /* 4110 * Abort connection if there is unread data queued. 4111 */ 4112 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4113 msg = "tcp_close, unread data"; 4114 break; 4115 } 4116 /* 4117 * tcp_hard_bound is now cleared thus all packets go through 4118 * tcp_lookup. This fact is used by tcp_detach below. 4119 * 4120 * We have done a qwait() above which could have possibly 4121 * drained more messages in turn causing transition to a 4122 * different state. Check whether we have to do the rest 4123 * of the processing or not. 4124 */ 4125 if (tcp->tcp_state <= TCPS_LISTEN) 4126 break; 4127 4128 /* 4129 * Transmit the FIN before detaching the tcp_t. 4130 * After tcp_detach returns this queue/perimeter 4131 * no longer owns the tcp_t thus others can modify it. 4132 */ 4133 (void) tcp_xmit_end(tcp); 4134 4135 /* 4136 * If lingering on close then wait until the fin is acked, 4137 * the SO_LINGER time passes, or a reset is sent/received. 4138 */ 4139 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4140 !(tcp->tcp_fin_acked) && 4141 tcp->tcp_state >= TCPS_ESTABLISHED) { 4142 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4143 tcp->tcp_client_errno = EWOULDBLOCK; 4144 } else if (tcp->tcp_client_errno == 0) { 4145 4146 ASSERT(tcp->tcp_linger_tid == 0); 4147 4148 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4149 tcp_close_linger_timeout, 4150 tcp->tcp_lingertime * hz); 4151 4152 /* tcp_close_linger_timeout will finish close */ 4153 if (tcp->tcp_linger_tid == 0) 4154 tcp->tcp_client_errno = ENOSR; 4155 else 4156 return; 4157 } 4158 4159 /* 4160 * Check if we need to detach or just close 4161 * the instance. 4162 */ 4163 if (tcp->tcp_state <= TCPS_LISTEN) 4164 break; 4165 } 4166 4167 /* 4168 * Make sure that no other thread will access the tcp_rq of 4169 * this instance (through lookups etc.) as tcp_rq will go 4170 * away shortly. 4171 */ 4172 tcp_acceptor_hash_remove(tcp); 4173 4174 if (tcp->tcp_flow_stopped) { 4175 tcp_clrqfull(tcp); 4176 } 4177 4178 if (tcp->tcp_timer_tid != 0) { 4179 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4180 tcp->tcp_timer_tid = 0; 4181 } 4182 /* 4183 * Need to cancel those timers which will not be used when 4184 * TCP is detached. This has to be done before the tcp_wq 4185 * is set to the global queue. 4186 */ 4187 tcp_timers_stop(tcp); 4188 4189 tcp->tcp_detached = B_TRUE; 4190 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4191 tcp_time_wait_append(tcp); 4192 TCP_DBGSTAT(tcp_detach_time_wait); 4193 ASSERT(connp->conn_ref >= 3); 4194 goto finish; 4195 } 4196 4197 /* 4198 * If delta is zero the timer event wasn't executed and was 4199 * successfully canceled. In this case we need to restart it 4200 * with the minimal delta possible. 4201 */ 4202 if (delta >= 0) 4203 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4204 delta ? delta : 1); 4205 4206 ASSERT(connp->conn_ref >= 3); 4207 goto finish; 4208 } 4209 4210 /* Detach did not complete. Still need to remove q from stream. */ 4211 if (msg) { 4212 if (tcp->tcp_state == TCPS_ESTABLISHED || 4213 tcp->tcp_state == TCPS_CLOSE_WAIT) 4214 BUMP_MIB(&tcp_mib, tcpEstabResets); 4215 if (tcp->tcp_state == TCPS_SYN_SENT || 4216 tcp->tcp_state == TCPS_SYN_RCVD) 4217 BUMP_MIB(&tcp_mib, tcpAttemptFails); 4218 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4219 } 4220 4221 tcp_closei_local(tcp); 4222 CONN_DEC_REF(connp); 4223 ASSERT(connp->conn_ref >= 2); 4224 4225 finish: 4226 /* 4227 * Although packets are always processed on the correct 4228 * tcp's perimeter and access is serialized via squeue's, 4229 * IP still needs a queue when sending packets in time_wait 4230 * state so use WR(tcp_g_q) till ip_output() can be 4231 * changed to deal with just connp. For read side, we 4232 * could have set tcp_rq to NULL but there are some cases 4233 * in tcp_rput_data() from early days of this code which 4234 * do a putnext without checking if tcp is closed. Those 4235 * need to be identified before both tcp_rq and tcp_wq 4236 * can be set to NULL and tcp_q_q can disappear forever. 4237 */ 4238 mutex_enter(&tcp->tcp_closelock); 4239 /* 4240 * Don't change the queues in the case of a listener that has 4241 * eagers in its q or q0. It could surprise the eagers. 4242 * Instead wait for the eagers outside the squeue. 4243 */ 4244 if (!tcp->tcp_wait_for_eagers) { 4245 tcp->tcp_detached = B_TRUE; 4246 tcp->tcp_rq = tcp_g_q; 4247 tcp->tcp_wq = WR(tcp_g_q); 4248 } 4249 4250 /* Signal tcp_close() to finish closing. */ 4251 tcp->tcp_closed = 1; 4252 cv_signal(&tcp->tcp_closecv); 4253 mutex_exit(&tcp->tcp_closelock); 4254 } 4255 4256 4257 /* 4258 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4259 * Some stream heads get upset if they see these later on as anything but NULL. 4260 */ 4261 static void 4262 tcp_close_mpp(mblk_t **mpp) 4263 { 4264 mblk_t *mp; 4265 4266 if ((mp = *mpp) != NULL) { 4267 do { 4268 mp->b_next = NULL; 4269 mp->b_prev = NULL; 4270 } while ((mp = mp->b_cont) != NULL); 4271 4272 mp = *mpp; 4273 *mpp = NULL; 4274 freemsg(mp); 4275 } 4276 } 4277 4278 /* Do detached close. */ 4279 static void 4280 tcp_close_detached(tcp_t *tcp) 4281 { 4282 if (tcp->tcp_fused) 4283 tcp_unfuse(tcp); 4284 4285 /* 4286 * Clustering code serializes TCP disconnect callbacks and 4287 * cluster tcp list walks by blocking a TCP disconnect callback 4288 * if a cluster tcp list walk is in progress. This ensures 4289 * accurate accounting of TCPs in the cluster code even though 4290 * the TCP list walk itself is not atomic. 4291 */ 4292 tcp_closei_local(tcp); 4293 CONN_DEC_REF(tcp->tcp_connp); 4294 } 4295 4296 /* 4297 * Stop all TCP timers, and free the timer mblks if requested. 4298 */ 4299 void 4300 tcp_timers_stop(tcp_t *tcp) 4301 { 4302 if (tcp->tcp_timer_tid != 0) { 4303 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4304 tcp->tcp_timer_tid = 0; 4305 } 4306 if (tcp->tcp_ka_tid != 0) { 4307 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4308 tcp->tcp_ka_tid = 0; 4309 } 4310 if (tcp->tcp_ack_tid != 0) { 4311 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4312 tcp->tcp_ack_tid = 0; 4313 } 4314 if (tcp->tcp_push_tid != 0) { 4315 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4316 tcp->tcp_push_tid = 0; 4317 } 4318 } 4319 4320 /* 4321 * The tcp_t is going away. Remove it from all lists and set it 4322 * to TCPS_CLOSED. The freeing up of memory is deferred until 4323 * tcp_inactive. This is needed since a thread in tcp_rput might have 4324 * done a CONN_INC_REF on this structure before it was removed from the 4325 * hashes. 4326 */ 4327 static void 4328 tcp_closei_local(tcp_t *tcp) 4329 { 4330 ire_t *ire; 4331 conn_t *connp = tcp->tcp_connp; 4332 4333 if (!TCP_IS_SOCKET(tcp)) 4334 tcp_acceptor_hash_remove(tcp); 4335 4336 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 4337 tcp->tcp_ibsegs = 0; 4338 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 4339 tcp->tcp_obsegs = 0; 4340 4341 /* 4342 * If we are an eager connection hanging off a listener that 4343 * hasn't formally accepted the connection yet, get off his 4344 * list and blow off any data that we have accumulated. 4345 */ 4346 if (tcp->tcp_listener != NULL) { 4347 tcp_t *listener = tcp->tcp_listener; 4348 mutex_enter(&listener->tcp_eager_lock); 4349 /* 4350 * tcp_eager_conn_ind == NULL means that the 4351 * conn_ind has already gone to listener. At 4352 * this point, eager will be closed but we 4353 * leave it in listeners eager list so that 4354 * if listener decides to close without doing 4355 * accept, we can clean this up. In tcp_wput_accept 4356 * we take case of the case of accept on closed 4357 * eager. 4358 */ 4359 if (tcp->tcp_conn.tcp_eager_conn_ind != NULL) { 4360 tcp_eager_unlink(tcp); 4361 mutex_exit(&listener->tcp_eager_lock); 4362 /* 4363 * We don't want to have any pointers to the 4364 * listener queue, after we have released our 4365 * reference on the listener 4366 */ 4367 tcp->tcp_rq = tcp_g_q; 4368 tcp->tcp_wq = WR(tcp_g_q); 4369 CONN_DEC_REF(listener->tcp_connp); 4370 } else { 4371 mutex_exit(&listener->tcp_eager_lock); 4372 } 4373 } 4374 4375 /* Stop all the timers */ 4376 tcp_timers_stop(tcp); 4377 4378 if (tcp->tcp_state == TCPS_LISTEN) { 4379 if (tcp->tcp_ip_addr_cache) { 4380 kmem_free((void *)tcp->tcp_ip_addr_cache, 4381 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4382 tcp->tcp_ip_addr_cache = NULL; 4383 } 4384 } 4385 if (tcp->tcp_flow_stopped) 4386 tcp_clrqfull(tcp); 4387 4388 tcp_bind_hash_remove(tcp); 4389 /* 4390 * If the tcp_time_wait_collector (which runs outside the squeue) 4391 * is trying to remove this tcp from the time wait list, we will 4392 * block in tcp_time_wait_remove while trying to acquire the 4393 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4394 * requires the ipcl_hash_remove to be ordered after the 4395 * tcp_time_wait_remove for the refcnt checks to work correctly. 4396 */ 4397 if (tcp->tcp_state == TCPS_TIME_WAIT) 4398 tcp_time_wait_remove(tcp, NULL); 4399 CL_INET_DISCONNECT(tcp); 4400 ipcl_hash_remove(connp); 4401 4402 /* 4403 * Delete the cached ire in conn_ire_cache and also mark 4404 * the conn as CONDEMNED 4405 */ 4406 mutex_enter(&connp->conn_lock); 4407 connp->conn_state_flags |= CONN_CONDEMNED; 4408 ire = connp->conn_ire_cache; 4409 connp->conn_ire_cache = NULL; 4410 mutex_exit(&connp->conn_lock); 4411 if (ire != NULL) 4412 IRE_REFRELE_NOTR(ire); 4413 4414 /* Need to cleanup any pending ioctls */ 4415 ASSERT(tcp->tcp_time_wait_next == NULL); 4416 ASSERT(tcp->tcp_time_wait_prev == NULL); 4417 ASSERT(tcp->tcp_time_wait_expire == 0); 4418 tcp->tcp_state = TCPS_CLOSED; 4419 4420 /* Release any SSL context */ 4421 if (tcp->tcp_kssl_ent != NULL) { 4422 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4423 tcp->tcp_kssl_ent = NULL; 4424 } 4425 if (tcp->tcp_kssl_ctx != NULL) { 4426 kssl_release_ctx(tcp->tcp_kssl_ctx); 4427 tcp->tcp_kssl_ctx = NULL; 4428 } 4429 tcp->tcp_kssl_pending = B_FALSE; 4430 } 4431 4432 /* 4433 * tcp is dying (called from ipcl_conn_destroy and error cases). 4434 * Free the tcp_t in either case. 4435 */ 4436 void 4437 tcp_free(tcp_t *tcp) 4438 { 4439 mblk_t *mp; 4440 ip6_pkt_t *ipp; 4441 4442 ASSERT(tcp != NULL); 4443 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4444 4445 tcp->tcp_rq = NULL; 4446 tcp->tcp_wq = NULL; 4447 4448 tcp_close_mpp(&tcp->tcp_xmit_head); 4449 tcp_close_mpp(&tcp->tcp_reass_head); 4450 if (tcp->tcp_rcv_list != NULL) { 4451 /* Free b_next chain */ 4452 tcp_close_mpp(&tcp->tcp_rcv_list); 4453 } 4454 if ((mp = tcp->tcp_urp_mp) != NULL) { 4455 freemsg(mp); 4456 } 4457 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4458 freemsg(mp); 4459 } 4460 4461 if (tcp->tcp_fused_sigurg_mp != NULL) { 4462 freeb(tcp->tcp_fused_sigurg_mp); 4463 tcp->tcp_fused_sigurg_mp = NULL; 4464 } 4465 4466 if (tcp->tcp_sack_info != NULL) { 4467 if (tcp->tcp_notsack_list != NULL) { 4468 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4469 } 4470 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4471 } 4472 4473 if (tcp->tcp_hopopts != NULL) { 4474 mi_free(tcp->tcp_hopopts); 4475 tcp->tcp_hopopts = NULL; 4476 tcp->tcp_hopoptslen = 0; 4477 } 4478 ASSERT(tcp->tcp_hopoptslen == 0); 4479 if (tcp->tcp_dstopts != NULL) { 4480 mi_free(tcp->tcp_dstopts); 4481 tcp->tcp_dstopts = NULL; 4482 tcp->tcp_dstoptslen = 0; 4483 } 4484 ASSERT(tcp->tcp_dstoptslen == 0); 4485 if (tcp->tcp_rtdstopts != NULL) { 4486 mi_free(tcp->tcp_rtdstopts); 4487 tcp->tcp_rtdstopts = NULL; 4488 tcp->tcp_rtdstoptslen = 0; 4489 } 4490 ASSERT(tcp->tcp_rtdstoptslen == 0); 4491 if (tcp->tcp_rthdr != NULL) { 4492 mi_free(tcp->tcp_rthdr); 4493 tcp->tcp_rthdr = NULL; 4494 tcp->tcp_rthdrlen = 0; 4495 } 4496 ASSERT(tcp->tcp_rthdrlen == 0); 4497 4498 ipp = &tcp->tcp_sticky_ipp; 4499 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4500 IPPF_RTHDR)) 4501 ip6_pkt_free(ipp); 4502 4503 /* 4504 * Free memory associated with the tcp/ip header template. 4505 */ 4506 4507 if (tcp->tcp_iphc != NULL) 4508 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4509 4510 /* 4511 * Following is really a blowing away a union. 4512 * It happens to have exactly two members of identical size 4513 * the following code is enough. 4514 */ 4515 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4516 4517 if (tcp->tcp_tracebuf != NULL) { 4518 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4519 tcp->tcp_tracebuf = NULL; 4520 } 4521 } 4522 4523 4524 /* 4525 * Put a connection confirmation message upstream built from the 4526 * address information within 'iph' and 'tcph'. Report our success or failure. 4527 */ 4528 static boolean_t 4529 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4530 mblk_t **defermp) 4531 { 4532 sin_t sin; 4533 sin6_t sin6; 4534 mblk_t *mp; 4535 char *optp = NULL; 4536 int optlen = 0; 4537 cred_t *cr; 4538 4539 if (defermp != NULL) 4540 *defermp = NULL; 4541 4542 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4543 /* 4544 * Return in T_CONN_CON results of option negotiation through 4545 * the T_CONN_REQ. Note: If there is an real end-to-end option 4546 * negotiation, then what is received from remote end needs 4547 * to be taken into account but there is no such thing (yet?) 4548 * in our TCP/IP. 4549 * Note: We do not use mi_offset_param() here as 4550 * tcp_opts_conn_req contents do not directly come from 4551 * an application and are either generated in kernel or 4552 * from user input that was already verified. 4553 */ 4554 mp = tcp->tcp_conn.tcp_opts_conn_req; 4555 optp = (char *)(mp->b_rptr + 4556 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4557 optlen = (int) 4558 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4559 } 4560 4561 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4562 ipha_t *ipha = (ipha_t *)iphdr; 4563 4564 /* packet is IPv4 */ 4565 if (tcp->tcp_family == AF_INET) { 4566 sin = sin_null; 4567 sin.sin_addr.s_addr = ipha->ipha_src; 4568 sin.sin_port = *(uint16_t *)tcph->th_lport; 4569 sin.sin_family = AF_INET; 4570 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4571 (int)sizeof (sin_t), optp, optlen); 4572 } else { 4573 sin6 = sin6_null; 4574 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4575 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4576 sin6.sin6_family = AF_INET6; 4577 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4578 (int)sizeof (sin6_t), optp, optlen); 4579 4580 } 4581 } else { 4582 ip6_t *ip6h = (ip6_t *)iphdr; 4583 4584 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4585 ASSERT(tcp->tcp_family == AF_INET6); 4586 sin6 = sin6_null; 4587 sin6.sin6_addr = ip6h->ip6_src; 4588 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4589 sin6.sin6_family = AF_INET6; 4590 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4591 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4592 (int)sizeof (sin6_t), optp, optlen); 4593 } 4594 4595 if (!mp) 4596 return (B_FALSE); 4597 4598 if ((cr = DB_CRED(idmp)) != NULL) { 4599 mblk_setcred(mp, cr); 4600 DB_CPID(mp) = DB_CPID(idmp); 4601 } 4602 4603 if (defermp == NULL) 4604 putnext(tcp->tcp_rq, mp); 4605 else 4606 *defermp = mp; 4607 4608 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4609 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4610 return (B_TRUE); 4611 } 4612 4613 /* 4614 * Defense for the SYN attack - 4615 * 1. When q0 is full, drop from the tail (tcp_eager_prev_q0) the oldest 4616 * one that doesn't have the dontdrop bit set. 4617 * 2. Don't drop a SYN request before its first timeout. This gives every 4618 * request at least til the first timeout to complete its 3-way handshake. 4619 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4620 * requests currently on the queue that has timed out. This will be used 4621 * as an indicator of whether an attack is under way, so that appropriate 4622 * actions can be taken. (It's incremented in tcp_timer() and decremented 4623 * either when eager goes into ESTABLISHED, or gets freed up.) 4624 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4625 * # of timeout drops back to <= q0len/32 => SYN alert off 4626 */ 4627 static boolean_t 4628 tcp_drop_q0(tcp_t *tcp) 4629 { 4630 tcp_t *eager; 4631 4632 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4633 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4634 /* 4635 * New one is added after next_q0 so prev_q0 points to the oldest 4636 * Also do not drop any established connections that are deferred on 4637 * q0 due to q being full 4638 */ 4639 4640 eager = tcp->tcp_eager_prev_q0; 4641 while (eager->tcp_dontdrop || eager->tcp_conn_def_q0) { 4642 eager = eager->tcp_eager_prev_q0; 4643 if (eager == tcp) { 4644 eager = tcp->tcp_eager_prev_q0; 4645 break; 4646 } 4647 } 4648 if (eager->tcp_syn_rcvd_timeout == 0) 4649 return (B_FALSE); 4650 4651 if (tcp->tcp_debug) { 4652 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4653 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4654 " (%d pending) on %s, drop one", tcp_conn_req_max_q0, 4655 tcp->tcp_conn_req_cnt_q0, 4656 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4657 } 4658 4659 BUMP_MIB(&tcp_mib, tcpHalfOpenDrop); 4660 4661 /* 4662 * need to do refhold here because the selected eager could 4663 * be removed by someone else if we release the eager lock. 4664 */ 4665 CONN_INC_REF(eager->tcp_connp); 4666 mutex_exit(&tcp->tcp_eager_lock); 4667 4668 /* Mark the IRE created for this SYN request temporary */ 4669 tcp_ip_ire_mark_advice(eager); 4670 (void) tcp_clean_death(eager, ETIMEDOUT, 5); 4671 CONN_DEC_REF(eager->tcp_connp); 4672 4673 mutex_enter(&tcp->tcp_eager_lock); 4674 return (B_TRUE); 4675 } 4676 4677 int 4678 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4679 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4680 { 4681 tcp_t *ltcp = lconnp->conn_tcp; 4682 tcp_t *tcp = connp->conn_tcp; 4683 mblk_t *tpi_mp; 4684 ipha_t *ipha; 4685 ip6_t *ip6h; 4686 sin6_t sin6; 4687 in6_addr_t v6dst; 4688 int err; 4689 int ifindex = 0; 4690 cred_t *cr; 4691 4692 if (ipvers == IPV4_VERSION) { 4693 ipha = (ipha_t *)mp->b_rptr; 4694 4695 connp->conn_send = ip_output; 4696 connp->conn_recv = tcp_input; 4697 4698 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4699 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4700 4701 sin6 = sin6_null; 4702 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4703 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4704 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4705 sin6.sin6_family = AF_INET6; 4706 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4707 lconnp->conn_zoneid); 4708 if (tcp->tcp_recvdstaddr) { 4709 sin6_t sin6d; 4710 4711 sin6d = sin6_null; 4712 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4713 &sin6d.sin6_addr); 4714 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4715 sin6d.sin6_family = AF_INET; 4716 tpi_mp = mi_tpi_extconn_ind(NULL, 4717 (char *)&sin6d, sizeof (sin6_t), 4718 (char *)&tcp, 4719 (t_scalar_t)sizeof (intptr_t), 4720 (char *)&sin6d, sizeof (sin6_t), 4721 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4722 } else { 4723 tpi_mp = mi_tpi_conn_ind(NULL, 4724 (char *)&sin6, sizeof (sin6_t), 4725 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4726 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4727 } 4728 } else { 4729 ip6h = (ip6_t *)mp->b_rptr; 4730 4731 connp->conn_send = ip_output_v6; 4732 connp->conn_recv = tcp_input; 4733 4734 connp->conn_srcv6 = ip6h->ip6_dst; 4735 connp->conn_remv6 = ip6h->ip6_src; 4736 4737 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4738 ifindex = (int)DB_CKSUMSTUFF(mp); 4739 DB_CKSUMSTUFF(mp) = 0; 4740 4741 sin6 = sin6_null; 4742 sin6.sin6_addr = ip6h->ip6_src; 4743 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4744 sin6.sin6_family = AF_INET6; 4745 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4746 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4747 lconnp->conn_zoneid); 4748 4749 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4750 /* Pass up the scope_id of remote addr */ 4751 sin6.sin6_scope_id = ifindex; 4752 } else { 4753 sin6.sin6_scope_id = 0; 4754 } 4755 if (tcp->tcp_recvdstaddr) { 4756 sin6_t sin6d; 4757 4758 sin6d = sin6_null; 4759 sin6.sin6_addr = ip6h->ip6_dst; 4760 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4761 sin6d.sin6_family = AF_INET; 4762 tpi_mp = mi_tpi_extconn_ind(NULL, 4763 (char *)&sin6d, sizeof (sin6_t), 4764 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4765 (char *)&sin6d, sizeof (sin6_t), 4766 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4767 } else { 4768 tpi_mp = mi_tpi_conn_ind(NULL, 4769 (char *)&sin6, sizeof (sin6_t), 4770 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4771 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4772 } 4773 } 4774 4775 if (tpi_mp == NULL) 4776 return (ENOMEM); 4777 4778 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4779 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4780 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4781 connp->conn_fully_bound = B_FALSE; 4782 4783 if (tcp_trace) 4784 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4785 4786 /* Inherit information from the "parent" */ 4787 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4788 tcp->tcp_family = ltcp->tcp_family; 4789 tcp->tcp_wq = ltcp->tcp_wq; 4790 tcp->tcp_rq = ltcp->tcp_rq; 4791 tcp->tcp_mss = tcp_mss_def_ipv6; 4792 tcp->tcp_detached = B_TRUE; 4793 if ((err = tcp_init_values(tcp)) != 0) { 4794 freemsg(tpi_mp); 4795 return (err); 4796 } 4797 4798 if (ipvers == IPV4_VERSION) { 4799 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4800 freemsg(tpi_mp); 4801 return (err); 4802 } 4803 ASSERT(tcp->tcp_ipha != NULL); 4804 } else { 4805 /* ifindex must be already set */ 4806 ASSERT(ifindex != 0); 4807 4808 if (ltcp->tcp_bound_if != 0) { 4809 /* 4810 * Set newtcp's bound_if equal to 4811 * listener's value. If ifindex is 4812 * not the same as ltcp->tcp_bound_if, 4813 * it must be a packet for the ipmp group 4814 * of interfaces 4815 */ 4816 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4817 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4818 tcp->tcp_bound_if = ifindex; 4819 } 4820 4821 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4822 tcp->tcp_recvifindex = 0; 4823 tcp->tcp_recvhops = 0xffffffffU; 4824 ASSERT(tcp->tcp_ip6h != NULL); 4825 } 4826 4827 tcp->tcp_lport = ltcp->tcp_lport; 4828 4829 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4830 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4831 /* 4832 * Listener had options of some sort; eager inherits. 4833 * Free up the eager template and allocate one 4834 * of the right size. 4835 */ 4836 if (tcp->tcp_hdr_grown) { 4837 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4838 } else { 4839 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4840 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4841 } 4842 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4843 KM_NOSLEEP); 4844 if (tcp->tcp_iphc == NULL) { 4845 tcp->tcp_iphc_len = 0; 4846 freemsg(tpi_mp); 4847 return (ENOMEM); 4848 } 4849 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4850 tcp->tcp_hdr_grown = B_TRUE; 4851 } 4852 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4853 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4854 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4855 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4856 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4857 4858 /* 4859 * Copy the IP+TCP header template from listener to eager 4860 */ 4861 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4862 if (tcp->tcp_ipversion == IPV6_VERSION) { 4863 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4864 IPPROTO_RAW) { 4865 tcp->tcp_ip6h = 4866 (ip6_t *)(tcp->tcp_iphc + 4867 sizeof (ip6i_t)); 4868 } else { 4869 tcp->tcp_ip6h = 4870 (ip6_t *)(tcp->tcp_iphc); 4871 } 4872 tcp->tcp_ipha = NULL; 4873 } else { 4874 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4875 tcp->tcp_ip6h = NULL; 4876 } 4877 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4878 tcp->tcp_ip_hdr_len); 4879 } else { 4880 /* 4881 * only valid case when ipversion of listener and 4882 * eager differ is when listener is IPv6 and 4883 * eager is IPv4. 4884 * Eager header template has been initialized to the 4885 * maximum v4 header sizes, which includes space for 4886 * TCP and IP options. 4887 */ 4888 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4889 (tcp->tcp_ipversion == IPV4_VERSION)); 4890 ASSERT(tcp->tcp_iphc_len >= 4891 TCP_MAX_COMBINED_HEADER_LENGTH); 4892 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4893 /* copy IP header fields individually */ 4894 tcp->tcp_ipha->ipha_ttl = 4895 ltcp->tcp_ip6h->ip6_hops; 4896 bcopy(ltcp->tcp_tcph->th_lport, 4897 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4898 } 4899 4900 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4901 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4902 sizeof (in_port_t)); 4903 4904 if (ltcp->tcp_lport == 0) { 4905 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4906 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4907 sizeof (in_port_t)); 4908 } 4909 4910 if (tcp->tcp_ipversion == IPV4_VERSION) { 4911 ASSERT(ipha != NULL); 4912 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4913 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4914 4915 /* Source routing option copyover (reverse it) */ 4916 if (tcp_rev_src_routes) 4917 tcp_opt_reverse(tcp, ipha); 4918 } else { 4919 ASSERT(ip6h != NULL); 4920 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4921 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4922 } 4923 4924 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4925 /* 4926 * If the SYN contains a credential, it's a loopback packet; attach 4927 * the credential to the TPI message. 4928 */ 4929 if ((cr = DB_CRED(idmp)) != NULL) { 4930 mblk_setcred(tpi_mp, cr); 4931 DB_CPID(tpi_mp) = DB_CPID(idmp); 4932 } 4933 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4934 4935 /* Inherit the listener's SSL protection state */ 4936 4937 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4938 kssl_hold_ent(tcp->tcp_kssl_ent); 4939 tcp->tcp_kssl_pending = B_TRUE; 4940 } 4941 4942 return (0); 4943 } 4944 4945 4946 int 4947 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4948 tcph_t *tcph, mblk_t *idmp) 4949 { 4950 tcp_t *ltcp = lconnp->conn_tcp; 4951 tcp_t *tcp = connp->conn_tcp; 4952 sin_t sin; 4953 mblk_t *tpi_mp = NULL; 4954 int err; 4955 cred_t *cr; 4956 4957 sin = sin_null; 4958 sin.sin_addr.s_addr = ipha->ipha_src; 4959 sin.sin_port = *(uint16_t *)tcph->th_lport; 4960 sin.sin_family = AF_INET; 4961 if (ltcp->tcp_recvdstaddr) { 4962 sin_t sind; 4963 4964 sind = sin_null; 4965 sind.sin_addr.s_addr = ipha->ipha_dst; 4966 sind.sin_port = *(uint16_t *)tcph->th_fport; 4967 sind.sin_family = AF_INET; 4968 tpi_mp = mi_tpi_extconn_ind(NULL, 4969 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4970 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4971 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4972 } else { 4973 tpi_mp = mi_tpi_conn_ind(NULL, 4974 (char *)&sin, sizeof (sin_t), 4975 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4976 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4977 } 4978 4979 if (tpi_mp == NULL) { 4980 return (ENOMEM); 4981 } 4982 4983 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4984 connp->conn_send = ip_output; 4985 connp->conn_recv = tcp_input; 4986 connp->conn_fully_bound = B_FALSE; 4987 4988 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4989 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4990 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4991 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4992 4993 if (tcp_trace) { 4994 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4995 } 4996 4997 /* Inherit information from the "parent" */ 4998 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4999 tcp->tcp_family = ltcp->tcp_family; 5000 tcp->tcp_wq = ltcp->tcp_wq; 5001 tcp->tcp_rq = ltcp->tcp_rq; 5002 tcp->tcp_mss = tcp_mss_def_ipv4; 5003 tcp->tcp_detached = B_TRUE; 5004 if ((err = tcp_init_values(tcp)) != 0) { 5005 freemsg(tpi_mp); 5006 return (err); 5007 } 5008 5009 /* 5010 * Let's make sure that eager tcp template has enough space to 5011 * copy IPv4 listener's tcp template. Since the conn_t structure is 5012 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5013 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5014 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5015 * extension headers or with ip6i_t struct). Note that bcopy() below 5016 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5017 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5018 */ 5019 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5020 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5021 5022 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5023 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5024 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5025 tcp->tcp_ttl = ltcp->tcp_ttl; 5026 tcp->tcp_tos = ltcp->tcp_tos; 5027 5028 /* Copy the IP+TCP header template from listener to eager */ 5029 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5030 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5031 tcp->tcp_ip6h = NULL; 5032 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5033 tcp->tcp_ip_hdr_len); 5034 5035 /* Initialize the IP addresses and Ports */ 5036 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5037 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5038 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5039 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5040 5041 /* Source routing option copyover (reverse it) */ 5042 if (tcp_rev_src_routes) 5043 tcp_opt_reverse(tcp, ipha); 5044 5045 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5046 5047 /* 5048 * If the SYN contains a credential, it's a loopback packet; attach 5049 * the credential to the TPI message. 5050 */ 5051 if ((cr = DB_CRED(idmp)) != NULL) { 5052 mblk_setcred(tpi_mp, cr); 5053 DB_CPID(tpi_mp) = DB_CPID(idmp); 5054 } 5055 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5056 5057 /* Inherit the listener's SSL protection state */ 5058 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5059 kssl_hold_ent(tcp->tcp_kssl_ent); 5060 tcp->tcp_kssl_pending = B_TRUE; 5061 } 5062 5063 return (0); 5064 } 5065 5066 /* 5067 * sets up conn for ipsec. 5068 * if the first mblk is M_CTL it is consumed and mpp is updated. 5069 * in case of error mpp is freed. 5070 */ 5071 conn_t * 5072 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5073 { 5074 conn_t *connp = tcp->tcp_connp; 5075 conn_t *econnp; 5076 squeue_t *new_sqp; 5077 mblk_t *first_mp = *mpp; 5078 mblk_t *mp = *mpp; 5079 boolean_t mctl_present = B_FALSE; 5080 uint_t ipvers; 5081 5082 econnp = tcp_get_conn(sqp); 5083 if (econnp == NULL) { 5084 freemsg(first_mp); 5085 return (NULL); 5086 } 5087 if (DB_TYPE(mp) == M_CTL) { 5088 if (mp->b_cont == NULL || 5089 mp->b_cont->b_datap->db_type != M_DATA) { 5090 freemsg(first_mp); 5091 return (NULL); 5092 } 5093 mp = mp->b_cont; 5094 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5095 freemsg(first_mp); 5096 return (NULL); 5097 } 5098 5099 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5100 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5101 mctl_present = B_TRUE; 5102 } else { 5103 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5104 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5105 } 5106 5107 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5108 DB_CKSUMSTART(mp) = 0; 5109 5110 ASSERT(OK_32PTR(mp->b_rptr)); 5111 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5112 if (ipvers == IPV4_VERSION) { 5113 uint16_t *up; 5114 uint32_t ports; 5115 ipha_t *ipha; 5116 5117 ipha = (ipha_t *)mp->b_rptr; 5118 up = (uint16_t *)((uchar_t *)ipha + 5119 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5120 ports = *(uint32_t *)up; 5121 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5122 ipha->ipha_dst, ipha->ipha_src, ports); 5123 } else { 5124 uint16_t *up; 5125 uint32_t ports; 5126 uint16_t ip_hdr_len; 5127 uint8_t *nexthdrp; 5128 ip6_t *ip6h; 5129 tcph_t *tcph; 5130 5131 ip6h = (ip6_t *)mp->b_rptr; 5132 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5133 ip_hdr_len = IPV6_HDR_LEN; 5134 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5135 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5136 CONN_DEC_REF(econnp); 5137 freemsg(first_mp); 5138 return (NULL); 5139 } 5140 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5141 up = (uint16_t *)tcph->th_lport; 5142 ports = *(uint32_t *)up; 5143 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5144 ip6h->ip6_dst, ip6h->ip6_src, ports); 5145 } 5146 5147 /* 5148 * The caller already ensured that there is a sqp present. 5149 */ 5150 econnp->conn_sqp = new_sqp; 5151 5152 if (connp->conn_policy != NULL) { 5153 ipsec_in_t *ii; 5154 ii = (ipsec_in_t *)(first_mp->b_rptr); 5155 ASSERT(ii->ipsec_in_policy == NULL); 5156 IPPH_REFHOLD(connp->conn_policy); 5157 ii->ipsec_in_policy = connp->conn_policy; 5158 5159 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5160 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5161 CONN_DEC_REF(econnp); 5162 freemsg(first_mp); 5163 return (NULL); 5164 } 5165 } 5166 5167 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5168 CONN_DEC_REF(econnp); 5169 freemsg(first_mp); 5170 return (NULL); 5171 } 5172 5173 /* 5174 * If we know we have some policy, pass the "IPSEC" 5175 * options size TCP uses this adjust the MSS. 5176 */ 5177 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5178 if (mctl_present) { 5179 freeb(first_mp); 5180 *mpp = mp; 5181 } 5182 5183 return (econnp); 5184 } 5185 5186 /* 5187 * tcp_get_conn/tcp_free_conn 5188 * 5189 * tcp_get_conn is used to get a clean tcp connection structure. 5190 * It tries to reuse the connections put on the freelist by the 5191 * time_wait_collector failing which it goes to kmem_cache. This 5192 * way has two benefits compared to just allocating from and 5193 * freeing to kmem_cache. 5194 * 1) The time_wait_collector can free (which includes the cleanup) 5195 * outside the squeue. So when the interrupt comes, we have a clean 5196 * connection sitting in the freelist. Obviously, this buys us 5197 * performance. 5198 * 5199 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5200 * has multiple disadvantages - tying up the squeue during alloc, and the 5201 * fact that IPSec policy initialization has to happen here which 5202 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5203 * But allocating the conn/tcp in IP land is also not the best since 5204 * we can't check the 'q' and 'q0' which are protected by squeue and 5205 * blindly allocate memory which might have to be freed here if we are 5206 * not allowed to accept the connection. By using the freelist and 5207 * putting the conn/tcp back in freelist, we don't pay a penalty for 5208 * allocating memory without checking 'q/q0' and freeing it if we can't 5209 * accept the connection. 5210 * 5211 * Care should be taken to put the conn back in the same squeue's freelist 5212 * from which it was allocated. Best results are obtained if conn is 5213 * allocated from listener's squeue and freed to the same. Time wait 5214 * collector will free up the freelist is the connection ends up sitting 5215 * there for too long. 5216 */ 5217 void * 5218 tcp_get_conn(void *arg) 5219 { 5220 tcp_t *tcp = NULL; 5221 conn_t *connp = NULL; 5222 squeue_t *sqp = (squeue_t *)arg; 5223 tcp_squeue_priv_t *tcp_time_wait; 5224 5225 tcp_time_wait = 5226 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5227 5228 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5229 tcp = tcp_time_wait->tcp_free_list; 5230 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5231 if (tcp != NULL) { 5232 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5233 tcp_time_wait->tcp_free_list_cnt--; 5234 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5235 tcp->tcp_time_wait_next = NULL; 5236 connp = tcp->tcp_connp; 5237 connp->conn_flags |= IPCL_REUSED; 5238 return ((void *)connp); 5239 } 5240 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5241 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL) 5242 return (NULL); 5243 return ((void *)connp); 5244 } 5245 5246 /* 5247 * Update the cached label for the given tcp_t. This should be called once per 5248 * connection, and before any packets are sent or tcp_process_options is 5249 * invoked. Returns B_FALSE if the correct label could not be constructed. 5250 */ 5251 static boolean_t 5252 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5253 { 5254 conn_t *connp = tcp->tcp_connp; 5255 5256 if (tcp->tcp_ipversion == IPV4_VERSION) { 5257 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5258 int added; 5259 5260 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5261 connp->conn_mac_exempt) != 0) 5262 return (B_FALSE); 5263 5264 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5265 if (added == -1) 5266 return (B_FALSE); 5267 tcp->tcp_hdr_len += added; 5268 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5269 tcp->tcp_ip_hdr_len += added; 5270 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5271 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5272 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5273 tcp->tcp_hdr_len); 5274 if (added == -1) 5275 return (B_FALSE); 5276 tcp->tcp_hdr_len += added; 5277 tcp->tcp_tcph = (tcph_t *) 5278 ((uchar_t *)tcp->tcp_tcph + added); 5279 tcp->tcp_ip_hdr_len += added; 5280 } 5281 } else { 5282 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5283 5284 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5285 connp->conn_mac_exempt) != 0) 5286 return (B_FALSE); 5287 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5288 &tcp->tcp_label_len, optbuf) != 0) 5289 return (B_FALSE); 5290 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5291 return (B_FALSE); 5292 } 5293 5294 connp->conn_ulp_labeled = 1; 5295 5296 return (B_TRUE); 5297 } 5298 5299 /* BEGIN CSTYLED */ 5300 /* 5301 * 5302 * The sockfs ACCEPT path: 5303 * ======================= 5304 * 5305 * The eager is now established in its own perimeter as soon as SYN is 5306 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5307 * completes the accept processing on the acceptor STREAM. The sending 5308 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5309 * listener but a TLI/XTI listener completes the accept processing 5310 * on the listener perimeter. 5311 * 5312 * Common control flow for 3 way handshake: 5313 * ---------------------------------------- 5314 * 5315 * incoming SYN (listener perimeter) -> tcp_rput_data() 5316 * -> tcp_conn_request() 5317 * 5318 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5319 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5320 * 5321 * Sockfs ACCEPT Path: 5322 * ------------------- 5323 * 5324 * open acceptor stream (ip_tcpopen allocates tcp_wput_accept() 5325 * as STREAM entry point) 5326 * 5327 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5328 * 5329 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5330 * association (we are not behind eager's squeue but sockfs is protecting us 5331 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5332 * is changed to point at tcp_wput(). 5333 * 5334 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5335 * listener (done on listener's perimeter). 5336 * 5337 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5338 * accept. 5339 * 5340 * TLI/XTI client ACCEPT path: 5341 * --------------------------- 5342 * 5343 * soaccept() sends T_CONN_RES on the listener STREAM. 5344 * 5345 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5346 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5347 * 5348 * Locks: 5349 * ====== 5350 * 5351 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5352 * and listeners->tcp_eager_next_q. 5353 * 5354 * Referencing: 5355 * ============ 5356 * 5357 * 1) We start out in tcp_conn_request by eager placing a ref on 5358 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5359 * 5360 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5361 * doing so we place a ref on the eager. This ref is finally dropped at the 5362 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5363 * reference is dropped by the squeue framework. 5364 * 5365 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5366 * 5367 * The reference must be released by the same entity that added the reference 5368 * In the above scheme, the eager is the entity that adds and releases the 5369 * references. Note that tcp_accept_finish executes in the squeue of the eager 5370 * (albeit after it is attached to the acceptor stream). Though 1. executes 5371 * in the listener's squeue, the eager is nascent at this point and the 5372 * reference can be considered to have been added on behalf of the eager. 5373 * 5374 * Eager getting a Reset or listener closing: 5375 * ========================================== 5376 * 5377 * Once the listener and eager are linked, the listener never does the unlink. 5378 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5379 * a message on all eager perimeter. The eager then does the unlink, clears 5380 * any pointers to the listener's queue and drops the reference to the 5381 * listener. The listener waits in tcp_close outside the squeue until its 5382 * refcount has dropped to 1. This ensures that the listener has waited for 5383 * all eagers to clear their association with the listener. 5384 * 5385 * Similarly, if eager decides to go away, it can unlink itself and close. 5386 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5387 * the reference to eager is still valid because of the extra ref we put 5388 * in tcp_send_conn_ind. 5389 * 5390 * Listener can always locate the eager under the protection 5391 * of the listener->tcp_eager_lock, and then do a refhold 5392 * on the eager during the accept processing. 5393 * 5394 * The acceptor stream accesses the eager in the accept processing 5395 * based on the ref placed on eager before sending T_conn_ind. 5396 * The only entity that can negate this refhold is a listener close 5397 * which is mutually exclusive with an active acceptor stream. 5398 * 5399 * Eager's reference on the listener 5400 * =================================== 5401 * 5402 * If the accept happens (even on a closed eager) the eager drops its 5403 * reference on the listener at the start of tcp_accept_finish. If the 5404 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5405 * the reference is dropped in tcp_closei_local. If the listener closes, 5406 * the reference is dropped in tcp_eager_kill. In all cases the reference 5407 * is dropped while executing in the eager's context (squeue). 5408 */ 5409 /* END CSTYLED */ 5410 5411 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5412 5413 /* 5414 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5415 * tcp_rput_data will not see any SYN packets. 5416 */ 5417 /* ARGSUSED */ 5418 void 5419 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5420 { 5421 tcph_t *tcph; 5422 uint32_t seg_seq; 5423 tcp_t *eager; 5424 uint_t ipvers; 5425 ipha_t *ipha; 5426 ip6_t *ip6h; 5427 int err; 5428 conn_t *econnp = NULL; 5429 squeue_t *new_sqp; 5430 mblk_t *mp1; 5431 uint_t ip_hdr_len; 5432 conn_t *connp = (conn_t *)arg; 5433 tcp_t *tcp = connp->conn_tcp; 5434 ire_t *ire; 5435 cred_t *credp; 5436 5437 if (tcp->tcp_state != TCPS_LISTEN) 5438 goto error2; 5439 5440 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5441 5442 mutex_enter(&tcp->tcp_eager_lock); 5443 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5444 mutex_exit(&tcp->tcp_eager_lock); 5445 TCP_STAT(tcp_listendrop); 5446 BUMP_MIB(&tcp_mib, tcpListenDrop); 5447 if (tcp->tcp_debug) { 5448 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5449 "tcp_conn_request: listen backlog (max=%d) " 5450 "overflow (%d pending) on %s", 5451 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5452 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5453 } 5454 goto error2; 5455 } 5456 5457 if (tcp->tcp_conn_req_cnt_q0 >= 5458 tcp->tcp_conn_req_max + tcp_conn_req_max_q0) { 5459 /* 5460 * Q0 is full. Drop a pending half-open req from the queue 5461 * to make room for the new SYN req. Also mark the time we 5462 * drop a SYN. 5463 * 5464 * A more aggressive defense against SYN attack will 5465 * be to set the "tcp_syn_defense" flag now. 5466 */ 5467 TCP_STAT(tcp_listendropq0); 5468 tcp->tcp_last_rcv_lbolt = lbolt64; 5469 if (!tcp_drop_q0(tcp)) { 5470 mutex_exit(&tcp->tcp_eager_lock); 5471 BUMP_MIB(&tcp_mib, tcpListenDropQ0); 5472 if (tcp->tcp_debug) { 5473 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5474 "tcp_conn_request: listen half-open queue " 5475 "(max=%d) full (%d pending) on %s", 5476 tcp_conn_req_max_q0, 5477 tcp->tcp_conn_req_cnt_q0, 5478 tcp_display(tcp, NULL, 5479 DISP_PORT_ONLY)); 5480 } 5481 goto error2; 5482 } 5483 } 5484 mutex_exit(&tcp->tcp_eager_lock); 5485 5486 /* 5487 * IP adds STRUIO_EAGER and ensures that the received packet is 5488 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5489 * link local address. If IPSec is enabled, db_struioflag has 5490 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5491 * otherwise an error case if neither of them is set. 5492 */ 5493 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5494 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5495 DB_CKSUMSTART(mp) = 0; 5496 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5497 econnp = (conn_t *)tcp_get_conn(arg2); 5498 if (econnp == NULL) 5499 goto error2; 5500 econnp->conn_sqp = new_sqp; 5501 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5502 /* 5503 * mp is updated in tcp_get_ipsec_conn(). 5504 */ 5505 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5506 if (econnp == NULL) { 5507 /* 5508 * mp freed by tcp_get_ipsec_conn. 5509 */ 5510 return; 5511 } 5512 } else { 5513 goto error2; 5514 } 5515 5516 ASSERT(DB_TYPE(mp) == M_DATA); 5517 5518 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5519 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5520 ASSERT(OK_32PTR(mp->b_rptr)); 5521 if (ipvers == IPV4_VERSION) { 5522 ipha = (ipha_t *)mp->b_rptr; 5523 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5524 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5525 } else { 5526 ip6h = (ip6_t *)mp->b_rptr; 5527 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5528 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5529 } 5530 5531 if (tcp->tcp_family == AF_INET) { 5532 ASSERT(ipvers == IPV4_VERSION); 5533 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5534 } else { 5535 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5536 } 5537 5538 if (err) 5539 goto error3; 5540 5541 eager = econnp->conn_tcp; 5542 5543 /* Inherit various TCP parameters from the listener */ 5544 eager->tcp_naglim = tcp->tcp_naglim; 5545 eager->tcp_first_timer_threshold = 5546 tcp->tcp_first_timer_threshold; 5547 eager->tcp_second_timer_threshold = 5548 tcp->tcp_second_timer_threshold; 5549 5550 eager->tcp_first_ctimer_threshold = 5551 tcp->tcp_first_ctimer_threshold; 5552 eager->tcp_second_ctimer_threshold = 5553 tcp->tcp_second_ctimer_threshold; 5554 5555 /* 5556 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5557 * If it does not, the eager's receive window will be set to the 5558 * listener's receive window later in this function. 5559 */ 5560 eager->tcp_rwnd = 0; 5561 5562 /* 5563 * Inherit listener's tcp_init_cwnd. Need to do this before 5564 * calling tcp_process_options() where tcp_mss_set() is called 5565 * to set the initial cwnd. 5566 */ 5567 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5568 5569 /* 5570 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5571 * zone id before the accept is completed in tcp_wput_accept(). 5572 */ 5573 econnp->conn_zoneid = connp->conn_zoneid; 5574 5575 /* Copy nexthop information from listener to eager */ 5576 if (connp->conn_nexthop_set) { 5577 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5578 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5579 } 5580 5581 /* 5582 * TSOL: tsol_input_proc() needs the eager's cred before the 5583 * eager is accepted 5584 */ 5585 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5586 crhold(credp); 5587 5588 /* 5589 * If the caller has the process-wide flag set, then default to MAC 5590 * exempt mode. This allows read-down to unlabeled hosts. 5591 */ 5592 if (getpflags(NET_MAC_AWARE, credp) != 0) 5593 econnp->conn_mac_exempt = B_TRUE; 5594 5595 if (is_system_labeled()) { 5596 cred_t *cr; 5597 5598 if (connp->conn_mlp_type != mlptSingle) { 5599 cr = econnp->conn_peercred = DB_CRED(mp); 5600 if (cr != NULL) 5601 crhold(cr); 5602 else 5603 cr = econnp->conn_cred; 5604 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5605 econnp, cred_t *, cr) 5606 } else { 5607 cr = econnp->conn_cred; 5608 DTRACE_PROBE2(syn_accept, conn_t *, 5609 econnp, cred_t *, cr) 5610 } 5611 5612 if (!tcp_update_label(eager, cr)) { 5613 DTRACE_PROBE3( 5614 tx__ip__log__error__connrequest__tcp, 5615 char *, "eager connp(1) label on SYN mp(2) failed", 5616 conn_t *, econnp, mblk_t *, mp); 5617 goto error3; 5618 } 5619 } 5620 5621 eager->tcp_hard_binding = B_TRUE; 5622 5623 tcp_bind_hash_insert(&tcp_bind_fanout[ 5624 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5625 5626 CL_INET_CONNECT(eager); 5627 5628 /* 5629 * No need to check for multicast destination since ip will only pass 5630 * up multicasts to those that have expressed interest 5631 * TODO: what about rejecting broadcasts? 5632 * Also check that source is not a multicast or broadcast address. 5633 */ 5634 eager->tcp_state = TCPS_SYN_RCVD; 5635 5636 5637 /* 5638 * There should be no ire in the mp as we are being called after 5639 * receiving the SYN. 5640 */ 5641 ASSERT(tcp_ire_mp(mp) == NULL); 5642 5643 /* 5644 * Adapt our mss, ttl, ... according to information provided in IRE. 5645 */ 5646 5647 if (tcp_adapt_ire(eager, NULL) == 0) { 5648 /* Undo the bind_hash_insert */ 5649 tcp_bind_hash_remove(eager); 5650 goto error3; 5651 } 5652 5653 /* Process all TCP options. */ 5654 tcp_process_options(eager, tcph); 5655 5656 /* Is the other end ECN capable? */ 5657 if (tcp_ecn_permitted >= 1 && 5658 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5659 eager->tcp_ecn_ok = B_TRUE; 5660 } 5661 5662 /* 5663 * listener->tcp_rq->q_hiwat should be the default window size or a 5664 * window size changed via SO_RCVBUF option. First round up the 5665 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5666 * scale option value if needed. Call tcp_rwnd_set() to finish the 5667 * setting. 5668 * 5669 * Note if there is a rpipe metric associated with the remote host, 5670 * we should not inherit receive window size from listener. 5671 */ 5672 eager->tcp_rwnd = MSS_ROUNDUP( 5673 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5674 eager->tcp_rwnd), eager->tcp_mss); 5675 if (eager->tcp_snd_ws_ok) 5676 tcp_set_ws_value(eager); 5677 /* 5678 * Note that this is the only place tcp_rwnd_set() is called for 5679 * accepting a connection. We need to call it here instead of 5680 * after the 3-way handshake because we need to tell the other 5681 * side our rwnd in the SYN-ACK segment. 5682 */ 5683 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5684 5685 /* 5686 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5687 * via soaccept()->soinheritoptions() which essentially applies 5688 * all the listener options to the new STREAM. The options that we 5689 * need to take care of are: 5690 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5691 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5692 * SO_SNDBUF, SO_RCVBUF. 5693 * 5694 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5695 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5696 * tcp_maxpsz_set() gets called later from 5697 * tcp_accept_finish(), the option takes effect. 5698 * 5699 */ 5700 /* Set the TCP options */ 5701 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5702 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5703 eager->tcp_oobinline = tcp->tcp_oobinline; 5704 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5705 eager->tcp_broadcast = tcp->tcp_broadcast; 5706 eager->tcp_useloopback = tcp->tcp_useloopback; 5707 eager->tcp_dontroute = tcp->tcp_dontroute; 5708 eager->tcp_linger = tcp->tcp_linger; 5709 eager->tcp_lingertime = tcp->tcp_lingertime; 5710 if (tcp->tcp_ka_enabled) 5711 eager->tcp_ka_enabled = 1; 5712 5713 /* Set the IP options */ 5714 econnp->conn_broadcast = connp->conn_broadcast; 5715 econnp->conn_loopback = connp->conn_loopback; 5716 econnp->conn_dontroute = connp->conn_dontroute; 5717 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5718 5719 /* Put a ref on the listener for the eager. */ 5720 CONN_INC_REF(connp); 5721 mutex_enter(&tcp->tcp_eager_lock); 5722 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5723 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5724 tcp->tcp_eager_next_q0 = eager; 5725 eager->tcp_eager_prev_q0 = tcp; 5726 5727 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5728 eager->tcp_listener = tcp; 5729 eager->tcp_saved_listener = tcp; 5730 5731 /* 5732 * Tag this detached tcp vector for later retrieval 5733 * by our listener client in tcp_accept(). 5734 */ 5735 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5736 tcp->tcp_conn_req_cnt_q0++; 5737 if (++tcp->tcp_conn_req_seqnum == -1) { 5738 /* 5739 * -1 is "special" and defined in TPI as something 5740 * that should never be used in T_CONN_IND 5741 */ 5742 ++tcp->tcp_conn_req_seqnum; 5743 } 5744 mutex_exit(&tcp->tcp_eager_lock); 5745 5746 if (tcp->tcp_syn_defense) { 5747 /* Don't drop the SYN that comes from a good IP source */ 5748 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5749 if (addr_cache != NULL && eager->tcp_remote == 5750 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5751 eager->tcp_dontdrop = B_TRUE; 5752 } 5753 } 5754 5755 /* 5756 * We need to insert the eager in its own perimeter but as soon 5757 * as we do that, we expose the eager to the classifier and 5758 * should not touch any field outside the eager's perimeter. 5759 * So do all the work necessary before inserting the eager 5760 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5761 * will succeed but undo everything if it fails. 5762 */ 5763 seg_seq = ABE32_TO_U32(tcph->th_seq); 5764 eager->tcp_irs = seg_seq; 5765 eager->tcp_rack = seg_seq; 5766 eager->tcp_rnxt = seg_seq + 1; 5767 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5768 BUMP_MIB(&tcp_mib, tcpPassiveOpens); 5769 eager->tcp_state = TCPS_SYN_RCVD; 5770 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5771 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5772 if (mp1 == NULL) 5773 goto error1; 5774 DB_CPID(mp1) = tcp->tcp_cpid; 5775 5776 /* 5777 * We need to start the rto timer. In normal case, we start 5778 * the timer after sending the packet on the wire (or at 5779 * least believing that packet was sent by waiting for 5780 * CALL_IP_WPUT() to return). Since this is the first packet 5781 * being sent on the wire for the eager, our initial tcp_rto 5782 * is at least tcp_rexmit_interval_min which is a fairly 5783 * large value to allow the algorithm to adjust slowly to large 5784 * fluctuations of RTT during first few transmissions. 5785 * 5786 * Starting the timer first and then sending the packet in this 5787 * case shouldn't make much difference since tcp_rexmit_interval_min 5788 * is of the order of several 100ms and starting the timer 5789 * first and then sending the packet will result in difference 5790 * of few micro seconds. 5791 * 5792 * Without this optimization, we are forced to hold the fanout 5793 * lock across the ipcl_bind_insert() and sending the packet 5794 * so that we don't race against an incoming packet (maybe RST) 5795 * for this eager. 5796 */ 5797 5798 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5799 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5800 5801 5802 /* 5803 * Insert the eager in its own perimeter now. We are ready to deal 5804 * with any packets on eager. 5805 */ 5806 if (eager->tcp_ipversion == IPV4_VERSION) { 5807 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5808 goto error; 5809 } 5810 } else { 5811 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5812 goto error; 5813 } 5814 } 5815 5816 /* mark conn as fully-bound */ 5817 econnp->conn_fully_bound = B_TRUE; 5818 5819 /* Send the SYN-ACK */ 5820 tcp_send_data(eager, eager->tcp_wq, mp1); 5821 freemsg(mp); 5822 5823 return; 5824 error: 5825 (void) TCP_TIMER_CANCEL(eager, eager->tcp_timer_tid); 5826 freemsg(mp1); 5827 error1: 5828 /* Undo what we did above */ 5829 mutex_enter(&tcp->tcp_eager_lock); 5830 tcp_eager_unlink(eager); 5831 mutex_exit(&tcp->tcp_eager_lock); 5832 /* Drop eager's reference on the listener */ 5833 CONN_DEC_REF(connp); 5834 5835 /* 5836 * Delete the cached ire in conn_ire_cache and also mark 5837 * the conn as CONDEMNED 5838 */ 5839 mutex_enter(&econnp->conn_lock); 5840 econnp->conn_state_flags |= CONN_CONDEMNED; 5841 ire = econnp->conn_ire_cache; 5842 econnp->conn_ire_cache = NULL; 5843 mutex_exit(&econnp->conn_lock); 5844 if (ire != NULL) 5845 IRE_REFRELE_NOTR(ire); 5846 5847 /* 5848 * tcp_accept_comm inserts the eager to the bind_hash 5849 * we need to remove it from the hash if ipcl_conn_insert 5850 * fails. 5851 */ 5852 tcp_bind_hash_remove(eager); 5853 /* Drop the eager ref placed in tcp_open_detached */ 5854 CONN_DEC_REF(econnp); 5855 5856 /* 5857 * If a connection already exists, send the mp to that connections so 5858 * that it can be appropriately dealt with. 5859 */ 5860 if ((econnp = ipcl_classify(mp, connp->conn_zoneid)) != NULL) { 5861 if (!IPCL_IS_CONNECTED(econnp)) { 5862 /* 5863 * Something bad happened. ipcl_conn_insert() 5864 * failed because a connection already existed 5865 * in connected hash but we can't find it 5866 * anymore (someone blew it away). Just 5867 * free this message and hopefully remote 5868 * will retransmit at which time the SYN can be 5869 * treated as a new connection or dealth with 5870 * a TH_RST if a connection already exists. 5871 */ 5872 freemsg(mp); 5873 } else { 5874 squeue_fill(econnp->conn_sqp, mp, tcp_input, 5875 econnp, SQTAG_TCP_CONN_REQ); 5876 } 5877 } else { 5878 /* Nobody wants this packet */ 5879 freemsg(mp); 5880 } 5881 return; 5882 error2: 5883 freemsg(mp); 5884 return; 5885 error3: 5886 CONN_DEC_REF(econnp); 5887 freemsg(mp); 5888 } 5889 5890 /* 5891 * In an ideal case of vertical partition in NUMA architecture, its 5892 * beneficial to have the listener and all the incoming connections 5893 * tied to the same squeue. The other constraint is that incoming 5894 * connections should be tied to the squeue attached to interrupted 5895 * CPU for obvious locality reason so this leaves the listener to 5896 * be tied to the same squeue. Our only problem is that when listener 5897 * is binding, the CPU that will get interrupted by the NIC whose 5898 * IP address the listener is binding to is not even known. So 5899 * the code below allows us to change that binding at the time the 5900 * CPU is interrupted by virtue of incoming connection's squeue. 5901 * 5902 * This is usefull only in case of a listener bound to a specific IP 5903 * address. For other kind of listeners, they get bound the 5904 * very first time and there is no attempt to rebind them. 5905 */ 5906 void 5907 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5908 { 5909 conn_t *connp = (conn_t *)arg; 5910 squeue_t *sqp = (squeue_t *)arg2; 5911 squeue_t *new_sqp; 5912 uint32_t conn_flags; 5913 5914 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5915 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5916 } else { 5917 goto done; 5918 } 5919 5920 if (connp->conn_fanout == NULL) 5921 goto done; 5922 5923 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5924 mutex_enter(&connp->conn_fanout->connf_lock); 5925 mutex_enter(&connp->conn_lock); 5926 /* 5927 * No one from read or write side can access us now 5928 * except for already queued packets on this squeue. 5929 * But since we haven't changed the squeue yet, they 5930 * can't execute. If they are processed after we have 5931 * changed the squeue, they are sent back to the 5932 * correct squeue down below. 5933 */ 5934 if (connp->conn_sqp != new_sqp) { 5935 while (connp->conn_sqp != new_sqp) 5936 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5937 } 5938 5939 do { 5940 conn_flags = connp->conn_flags; 5941 conn_flags |= IPCL_FULLY_BOUND; 5942 (void) cas32(&connp->conn_flags, connp->conn_flags, 5943 conn_flags); 5944 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5945 5946 mutex_exit(&connp->conn_fanout->connf_lock); 5947 mutex_exit(&connp->conn_lock); 5948 } 5949 5950 done: 5951 if (connp->conn_sqp != sqp) { 5952 CONN_INC_REF(connp); 5953 squeue_fill(connp->conn_sqp, mp, 5954 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 5955 } else { 5956 tcp_conn_request(connp, mp, sqp); 5957 } 5958 } 5959 5960 /* 5961 * Successful connect request processing begins when our client passes 5962 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5963 * our T_OK_ACK reply message upstream. The control flow looks like this: 5964 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 5965 * upstream <- tcp_rput() <- IP 5966 * After various error checks are completed, tcp_connect() lays 5967 * the target address and port into the composite header template, 5968 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5969 * request followed by an IRE request, and passes the three mblk message 5970 * down to IP looking like this: 5971 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5972 * Processing continues in tcp_rput() when we receive the following message: 5973 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5974 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5975 * to fire off the connection request, and then passes the T_OK_ACK mblk 5976 * upstream that we filled in below. There are, of course, numerous 5977 * error conditions along the way which truncate the processing described 5978 * above. 5979 */ 5980 static void 5981 tcp_connect(tcp_t *tcp, mblk_t *mp) 5982 { 5983 sin_t *sin; 5984 sin6_t *sin6; 5985 queue_t *q = tcp->tcp_wq; 5986 struct T_conn_req *tcr; 5987 ipaddr_t *dstaddrp; 5988 in_port_t dstport; 5989 uint_t srcid; 5990 5991 tcr = (struct T_conn_req *)mp->b_rptr; 5992 5993 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5994 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5995 tcp_err_ack(tcp, mp, TPROTO, 0); 5996 return; 5997 } 5998 5999 /* 6000 * Determine packet type based on type of address passed in 6001 * the request should contain an IPv4 or IPv6 address. 6002 * Make sure that address family matches the type of 6003 * family of the the address passed down 6004 */ 6005 switch (tcr->DEST_length) { 6006 default: 6007 tcp_err_ack(tcp, mp, TBADADDR, 0); 6008 return; 6009 6010 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6011 /* 6012 * XXX: The check for valid DEST_length was not there 6013 * in earlier releases and some buggy 6014 * TLI apps (e.g Sybase) got away with not feeding 6015 * in sin_zero part of address. 6016 * We allow that bug to keep those buggy apps humming. 6017 * Test suites require the check on DEST_length. 6018 * We construct a new mblk with valid DEST_length 6019 * free the original so the rest of the code does 6020 * not have to keep track of this special shorter 6021 * length address case. 6022 */ 6023 mblk_t *nmp; 6024 struct T_conn_req *ntcr; 6025 sin_t *nsin; 6026 6027 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6028 tcr->OPT_length, BPRI_HI); 6029 if (nmp == NULL) { 6030 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6031 return; 6032 } 6033 ntcr = (struct T_conn_req *)nmp->b_rptr; 6034 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6035 ntcr->PRIM_type = T_CONN_REQ; 6036 ntcr->DEST_length = sizeof (sin_t); 6037 ntcr->DEST_offset = sizeof (struct T_conn_req); 6038 6039 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6040 *nsin = sin_null; 6041 /* Get pointer to shorter address to copy from original mp */ 6042 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6043 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6044 if (sin == NULL || !OK_32PTR((char *)sin)) { 6045 freemsg(nmp); 6046 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6047 return; 6048 } 6049 nsin->sin_family = sin->sin_family; 6050 nsin->sin_port = sin->sin_port; 6051 nsin->sin_addr = sin->sin_addr; 6052 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6053 nmp->b_wptr = (uchar_t *)&nsin[1]; 6054 if (tcr->OPT_length != 0) { 6055 ntcr->OPT_length = tcr->OPT_length; 6056 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6057 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6058 (uchar_t *)ntcr + ntcr->OPT_offset, 6059 tcr->OPT_length); 6060 nmp->b_wptr += tcr->OPT_length; 6061 } 6062 freemsg(mp); /* original mp freed */ 6063 mp = nmp; /* re-initialize original variables */ 6064 tcr = ntcr; 6065 } 6066 /* FALLTHRU */ 6067 6068 case sizeof (sin_t): 6069 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6070 sizeof (sin_t)); 6071 if (sin == NULL || !OK_32PTR((char *)sin)) { 6072 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6073 return; 6074 } 6075 if (tcp->tcp_family != AF_INET || 6076 sin->sin_family != AF_INET) { 6077 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6078 return; 6079 } 6080 if (sin->sin_port == 0) { 6081 tcp_err_ack(tcp, mp, TBADADDR, 0); 6082 return; 6083 } 6084 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6085 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6086 return; 6087 } 6088 6089 break; 6090 6091 case sizeof (sin6_t): 6092 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6093 sizeof (sin6_t)); 6094 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6095 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6096 return; 6097 } 6098 if (tcp->tcp_family != AF_INET6 || 6099 sin6->sin6_family != AF_INET6) { 6100 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6101 return; 6102 } 6103 if (sin6->sin6_port == 0) { 6104 tcp_err_ack(tcp, mp, TBADADDR, 0); 6105 return; 6106 } 6107 break; 6108 } 6109 /* 6110 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6111 * should key on their sequence number and cut them loose. 6112 */ 6113 6114 /* 6115 * If options passed in, feed it for verification and handling 6116 */ 6117 if (tcr->OPT_length != 0) { 6118 mblk_t *ok_mp; 6119 mblk_t *discon_mp; 6120 mblk_t *conn_opts_mp; 6121 int t_error, sys_error, do_disconnect; 6122 6123 conn_opts_mp = NULL; 6124 6125 if (tcp_conprim_opt_process(tcp, mp, 6126 &do_disconnect, &t_error, &sys_error) < 0) { 6127 if (do_disconnect) { 6128 ASSERT(t_error == 0 && sys_error == 0); 6129 discon_mp = mi_tpi_discon_ind(NULL, 6130 ECONNREFUSED, 0); 6131 if (!discon_mp) { 6132 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6133 TSYSERR, ENOMEM); 6134 return; 6135 } 6136 ok_mp = mi_tpi_ok_ack_alloc(mp); 6137 if (!ok_mp) { 6138 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6139 TSYSERR, ENOMEM); 6140 return; 6141 } 6142 qreply(q, ok_mp); 6143 qreply(q, discon_mp); /* no flush! */ 6144 } else { 6145 ASSERT(t_error != 0); 6146 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6147 sys_error); 6148 } 6149 return; 6150 } 6151 /* 6152 * Success in setting options, the mp option buffer represented 6153 * by OPT_length/offset has been potentially modified and 6154 * contains results of option processing. We copy it in 6155 * another mp to save it for potentially influencing returning 6156 * it in T_CONN_CONN. 6157 */ 6158 if (tcr->OPT_length != 0) { /* there are resulting options */ 6159 conn_opts_mp = copyb(mp); 6160 if (!conn_opts_mp) { 6161 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6162 TSYSERR, ENOMEM); 6163 return; 6164 } 6165 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6166 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6167 /* 6168 * Note: 6169 * These resulting option negotiation can include any 6170 * end-to-end negotiation options but there no such 6171 * thing (yet?) in our TCP/IP. 6172 */ 6173 } 6174 } 6175 6176 /* 6177 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6178 * make sure that the template IP header in the tcp structure is an 6179 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6180 * need to this before we call tcp_bindi() so that the port lookup 6181 * code will look for ports in the correct port space (IPv4 and 6182 * IPv6 have separate port spaces). 6183 */ 6184 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6185 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6186 int err = 0; 6187 6188 err = tcp_header_init_ipv4(tcp); 6189 if (err != 0) { 6190 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6191 goto connect_failed; 6192 } 6193 if (tcp->tcp_lport != 0) 6194 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6195 } 6196 6197 switch (tcp->tcp_state) { 6198 case TCPS_IDLE: 6199 /* 6200 * We support quick connect, refer to comments in 6201 * tcp_connect_*() 6202 */ 6203 /* FALLTHRU */ 6204 case TCPS_BOUND: 6205 case TCPS_LISTEN: 6206 if (tcp->tcp_family == AF_INET6) { 6207 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6208 tcp_connect_ipv6(tcp, mp, 6209 &sin6->sin6_addr, 6210 sin6->sin6_port, sin6->sin6_flowinfo, 6211 sin6->__sin6_src_id, sin6->sin6_scope_id); 6212 return; 6213 } 6214 /* 6215 * Destination adress is mapped IPv6 address. 6216 * Source bound address should be unspecified or 6217 * IPv6 mapped address as well. 6218 */ 6219 if (!IN6_IS_ADDR_UNSPECIFIED( 6220 &tcp->tcp_bound_source_v6) && 6221 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6222 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6223 EADDRNOTAVAIL); 6224 break; 6225 } 6226 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6227 dstport = sin6->sin6_port; 6228 srcid = sin6->__sin6_src_id; 6229 } else { 6230 dstaddrp = &sin->sin_addr.s_addr; 6231 dstport = sin->sin_port; 6232 srcid = 0; 6233 } 6234 6235 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6236 return; 6237 default: 6238 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6239 break; 6240 } 6241 /* 6242 * Note: Code below is the "failure" case 6243 */ 6244 /* return error ack and blow away saved option results if any */ 6245 connect_failed: 6246 if (mp != NULL) 6247 putnext(tcp->tcp_rq, mp); 6248 else { 6249 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6250 TSYSERR, ENOMEM); 6251 } 6252 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6253 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6254 } 6255 6256 /* 6257 * Handle connect to IPv4 destinations, including connections for AF_INET6 6258 * sockets connecting to IPv4 mapped IPv6 destinations. 6259 */ 6260 static void 6261 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6262 uint_t srcid) 6263 { 6264 tcph_t *tcph; 6265 mblk_t *mp1; 6266 ipaddr_t dstaddr = *dstaddrp; 6267 int32_t oldstate; 6268 uint16_t lport; 6269 6270 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6271 6272 /* Check for attempt to connect to INADDR_ANY */ 6273 if (dstaddr == INADDR_ANY) { 6274 /* 6275 * SunOS 4.x and 4.3 BSD allow an application 6276 * to connect a TCP socket to INADDR_ANY. 6277 * When they do this, the kernel picks the 6278 * address of one interface and uses it 6279 * instead. The kernel usually ends up 6280 * picking the address of the loopback 6281 * interface. This is an undocumented feature. 6282 * However, we provide the same thing here 6283 * in order to have source and binary 6284 * compatibility with SunOS 4.x. 6285 * Update the T_CONN_REQ (sin/sin6) since it is used to 6286 * generate the T_CONN_CON. 6287 */ 6288 dstaddr = htonl(INADDR_LOOPBACK); 6289 *dstaddrp = dstaddr; 6290 } 6291 6292 /* Handle __sin6_src_id if socket not bound to an IP address */ 6293 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6294 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6295 tcp->tcp_connp->conn_zoneid); 6296 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6297 tcp->tcp_ipha->ipha_src); 6298 } 6299 6300 /* 6301 * Don't let an endpoint connect to itself. Note that 6302 * the test here does not catch the case where the 6303 * source IP addr was left unspecified by the user. In 6304 * this case, the source addr is set in tcp_adapt_ire() 6305 * using the reply to the T_BIND message that we send 6306 * down to IP here and the check is repeated in tcp_rput_other. 6307 */ 6308 if (dstaddr == tcp->tcp_ipha->ipha_src && 6309 dstport == tcp->tcp_lport) { 6310 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6311 goto failed; 6312 } 6313 6314 tcp->tcp_ipha->ipha_dst = dstaddr; 6315 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6316 6317 /* 6318 * Massage a source route if any putting the first hop 6319 * in iph_dst. Compute a starting value for the checksum which 6320 * takes into account that the original iph_dst should be 6321 * included in the checksum but that ip will include the 6322 * first hop in the source route in the tcp checksum. 6323 */ 6324 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha); 6325 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6326 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6327 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6328 if ((int)tcp->tcp_sum < 0) 6329 tcp->tcp_sum--; 6330 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6331 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6332 (tcp->tcp_sum >> 16)); 6333 tcph = tcp->tcp_tcph; 6334 *(uint16_t *)tcph->th_fport = dstport; 6335 tcp->tcp_fport = dstport; 6336 6337 oldstate = tcp->tcp_state; 6338 /* 6339 * At this point the remote destination address and remote port fields 6340 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6341 * have to see which state tcp was in so we can take apropriate action. 6342 */ 6343 if (oldstate == TCPS_IDLE) { 6344 /* 6345 * We support a quick connect capability here, allowing 6346 * clients to transition directly from IDLE to SYN_SENT 6347 * tcp_bindi will pick an unused port, insert the connection 6348 * in the bind hash and transition to BOUND state. 6349 */ 6350 lport = tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 6351 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6352 B_FALSE, B_FALSE); 6353 if (lport == 0) { 6354 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6355 goto failed; 6356 } 6357 } 6358 tcp->tcp_state = TCPS_SYN_SENT; 6359 6360 /* 6361 * TODO: allow data with connect requests 6362 * by unlinking M_DATA trailers here and 6363 * linking them in behind the T_OK_ACK mblk. 6364 * The tcp_rput() bind ack handler would then 6365 * feed them to tcp_wput_data() rather than call 6366 * tcp_timer(). 6367 */ 6368 mp = mi_tpi_ok_ack_alloc(mp); 6369 if (!mp) { 6370 tcp->tcp_state = oldstate; 6371 goto failed; 6372 } 6373 if (tcp->tcp_family == AF_INET) { 6374 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6375 sizeof (ipa_conn_t)); 6376 } else { 6377 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6378 sizeof (ipa6_conn_t)); 6379 } 6380 if (mp1) { 6381 /* Hang onto the T_OK_ACK for later. */ 6382 linkb(mp1, mp); 6383 mblk_setcred(mp1, tcp->tcp_cred); 6384 if (tcp->tcp_family == AF_INET) 6385 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6386 else { 6387 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6388 &tcp->tcp_sticky_ipp); 6389 } 6390 BUMP_MIB(&tcp_mib, tcpActiveOpens); 6391 tcp->tcp_active_open = 1; 6392 /* 6393 * If the bind cannot complete immediately 6394 * IP will arrange to call tcp_rput_other 6395 * when the bind completes. 6396 */ 6397 if (mp1 != NULL) 6398 tcp_rput_other(tcp, mp1); 6399 return; 6400 } 6401 /* Error case */ 6402 tcp->tcp_state = oldstate; 6403 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6404 6405 failed: 6406 /* return error ack and blow away saved option results if any */ 6407 if (mp != NULL) 6408 putnext(tcp->tcp_rq, mp); 6409 else { 6410 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6411 TSYSERR, ENOMEM); 6412 } 6413 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6414 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6415 6416 } 6417 6418 /* 6419 * Handle connect to IPv6 destinations. 6420 */ 6421 static void 6422 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6423 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6424 { 6425 tcph_t *tcph; 6426 mblk_t *mp1; 6427 ip6_rthdr_t *rth; 6428 int32_t oldstate; 6429 uint16_t lport; 6430 6431 ASSERT(tcp->tcp_family == AF_INET6); 6432 6433 /* 6434 * If we're here, it means that the destination address is a native 6435 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6436 * reason why it might not be IPv6 is if the socket was bound to an 6437 * IPv4-mapped IPv6 address. 6438 */ 6439 if (tcp->tcp_ipversion != IPV6_VERSION) { 6440 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6441 goto failed; 6442 } 6443 6444 /* 6445 * Interpret a zero destination to mean loopback. 6446 * Update the T_CONN_REQ (sin/sin6) since it is used to 6447 * generate the T_CONN_CON. 6448 */ 6449 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6450 *dstaddrp = ipv6_loopback; 6451 } 6452 6453 /* Handle __sin6_src_id if socket not bound to an IP address */ 6454 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6455 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6456 tcp->tcp_connp->conn_zoneid); 6457 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6458 } 6459 6460 /* 6461 * Take care of the scope_id now and add ip6i_t 6462 * if ip6i_t is not already allocated through TCP 6463 * sticky options. At this point tcp_ip6h does not 6464 * have dst info, thus use dstaddrp. 6465 */ 6466 if (scope_id != 0 && 6467 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6468 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6469 ip6i_t *ip6i; 6470 6471 ipp->ipp_ifindex = scope_id; 6472 ip6i = (ip6i_t *)tcp->tcp_iphc; 6473 6474 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6475 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6476 /* Already allocated */ 6477 ip6i->ip6i_flags |= IP6I_IFINDEX; 6478 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6479 ipp->ipp_fields |= IPPF_SCOPE_ID; 6480 } else { 6481 int reterr; 6482 6483 ipp->ipp_fields |= IPPF_SCOPE_ID; 6484 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6485 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6486 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6487 if (reterr != 0) 6488 goto failed; 6489 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6490 } 6491 } 6492 6493 /* 6494 * Don't let an endpoint connect to itself. Note that 6495 * the test here does not catch the case where the 6496 * source IP addr was left unspecified by the user. In 6497 * this case, the source addr is set in tcp_adapt_ire() 6498 * using the reply to the T_BIND message that we send 6499 * down to IP here and the check is repeated in tcp_rput_other. 6500 */ 6501 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6502 (dstport == tcp->tcp_lport)) { 6503 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6504 goto failed; 6505 } 6506 6507 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6508 tcp->tcp_remote_v6 = *dstaddrp; 6509 tcp->tcp_ip6h->ip6_vcf = 6510 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6511 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6512 6513 6514 /* 6515 * Massage a routing header (if present) putting the first hop 6516 * in ip6_dst. Compute a starting value for the checksum which 6517 * takes into account that the original ip6_dst should be 6518 * included in the checksum but that ip will include the 6519 * first hop in the source route in the tcp checksum. 6520 */ 6521 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6522 if (rth != NULL) { 6523 6524 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth); 6525 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6526 (tcp->tcp_sum >> 16)); 6527 } else { 6528 tcp->tcp_sum = 0; 6529 } 6530 6531 tcph = tcp->tcp_tcph; 6532 *(uint16_t *)tcph->th_fport = dstport; 6533 tcp->tcp_fport = dstport; 6534 6535 oldstate = tcp->tcp_state; 6536 /* 6537 * At this point the remote destination address and remote port fields 6538 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6539 * have to see which state tcp was in so we can take apropriate action. 6540 */ 6541 if (oldstate == TCPS_IDLE) { 6542 /* 6543 * We support a quick connect capability here, allowing 6544 * clients to transition directly from IDLE to SYN_SENT 6545 * tcp_bindi will pick an unused port, insert the connection 6546 * in the bind hash and transition to BOUND state. 6547 */ 6548 lport = tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 6549 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6550 B_FALSE, B_FALSE); 6551 if (lport == 0) { 6552 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6553 goto failed; 6554 } 6555 } 6556 tcp->tcp_state = TCPS_SYN_SENT; 6557 /* 6558 * TODO: allow data with connect requests 6559 * by unlinking M_DATA trailers here and 6560 * linking them in behind the T_OK_ACK mblk. 6561 * The tcp_rput() bind ack handler would then 6562 * feed them to tcp_wput_data() rather than call 6563 * tcp_timer(). 6564 */ 6565 mp = mi_tpi_ok_ack_alloc(mp); 6566 if (!mp) { 6567 tcp->tcp_state = oldstate; 6568 goto failed; 6569 } 6570 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6571 if (mp1) { 6572 /* Hang onto the T_OK_ACK for later. */ 6573 linkb(mp1, mp); 6574 mblk_setcred(mp1, tcp->tcp_cred); 6575 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6576 &tcp->tcp_sticky_ipp); 6577 BUMP_MIB(&tcp_mib, tcpActiveOpens); 6578 tcp->tcp_active_open = 1; 6579 /* ip_bind_v6() may return ACK or ERROR */ 6580 if (mp1 != NULL) 6581 tcp_rput_other(tcp, mp1); 6582 return; 6583 } 6584 /* Error case */ 6585 tcp->tcp_state = oldstate; 6586 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6587 6588 failed: 6589 /* return error ack and blow away saved option results if any */ 6590 if (mp != NULL) 6591 putnext(tcp->tcp_rq, mp); 6592 else { 6593 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6594 TSYSERR, ENOMEM); 6595 } 6596 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6597 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6598 } 6599 6600 /* 6601 * We need a stream q for detached closing tcp connections 6602 * to use. Our client hereby indicates that this q is the 6603 * one to use. 6604 */ 6605 static void 6606 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6607 { 6608 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6609 queue_t *q = tcp->tcp_wq; 6610 6611 mp->b_datap->db_type = M_IOCACK; 6612 iocp->ioc_count = 0; 6613 mutex_enter(&tcp_g_q_lock); 6614 if (tcp_g_q != NULL) { 6615 mutex_exit(&tcp_g_q_lock); 6616 iocp->ioc_error = EALREADY; 6617 } else { 6618 mblk_t *mp1; 6619 6620 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6621 if (mp1 == NULL) { 6622 mutex_exit(&tcp_g_q_lock); 6623 iocp->ioc_error = ENOMEM; 6624 } else { 6625 tcp_g_q = tcp->tcp_rq; 6626 mutex_exit(&tcp_g_q_lock); 6627 iocp->ioc_error = 0; 6628 iocp->ioc_rval = 0; 6629 /* 6630 * We are passing tcp_sticky_ipp as NULL 6631 * as it is not useful for tcp_default queue 6632 */ 6633 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6634 if (mp1 != NULL) 6635 tcp_rput_other(tcp, mp1); 6636 } 6637 } 6638 qreply(q, mp); 6639 } 6640 6641 /* 6642 * Our client hereby directs us to reject the connection request 6643 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6644 * of sending the appropriate RST, not an ICMP error. 6645 */ 6646 static void 6647 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6648 { 6649 tcp_t *ltcp = NULL; 6650 t_scalar_t seqnum; 6651 conn_t *connp; 6652 6653 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6654 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6655 tcp_err_ack(tcp, mp, TPROTO, 0); 6656 return; 6657 } 6658 6659 /* 6660 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6661 * when the stream is in BOUND state. Do not send a reset, 6662 * since the destination IP address is not valid, and it can 6663 * be the initialized value of all zeros (broadcast address). 6664 * 6665 * If TCP has sent down a bind request to IP and has not 6666 * received the reply, reject the request. Otherwise, TCP 6667 * will be confused. 6668 */ 6669 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6670 if (tcp->tcp_debug) { 6671 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6672 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6673 } 6674 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6675 return; 6676 } 6677 6678 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6679 6680 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6681 6682 /* 6683 * According to TPI, for non-listeners, ignore seqnum 6684 * and disconnect. 6685 * Following interpretation of -1 seqnum is historical 6686 * and implied TPI ? (TPI only states that for T_CONN_IND, 6687 * a valid seqnum should not be -1). 6688 * 6689 * -1 means disconnect everything 6690 * regardless even on a listener. 6691 */ 6692 6693 int old_state = tcp->tcp_state; 6694 6695 /* 6696 * The connection can't be on the tcp_time_wait_head list 6697 * since it is not detached. 6698 */ 6699 ASSERT(tcp->tcp_time_wait_next == NULL); 6700 ASSERT(tcp->tcp_time_wait_prev == NULL); 6701 ASSERT(tcp->tcp_time_wait_expire == 0); 6702 ltcp = NULL; 6703 /* 6704 * If it used to be a listener, check to make sure no one else 6705 * has taken the port before switching back to LISTEN state. 6706 */ 6707 if (tcp->tcp_ipversion == IPV4_VERSION) { 6708 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6709 tcp->tcp_ipha->ipha_src, 6710 tcp->tcp_connp->conn_zoneid); 6711 if (connp != NULL) 6712 ltcp = connp->conn_tcp; 6713 } else { 6714 /* Allow tcp_bound_if listeners? */ 6715 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6716 &tcp->tcp_ip6h->ip6_src, 0, 6717 tcp->tcp_connp->conn_zoneid); 6718 if (connp != NULL) 6719 ltcp = connp->conn_tcp; 6720 } 6721 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6722 tcp->tcp_state = TCPS_LISTEN; 6723 } else if (old_state > TCPS_BOUND) { 6724 tcp->tcp_conn_req_max = 0; 6725 tcp->tcp_state = TCPS_BOUND; 6726 } 6727 if (ltcp != NULL) 6728 CONN_DEC_REF(ltcp->tcp_connp); 6729 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6730 BUMP_MIB(&tcp_mib, tcpAttemptFails); 6731 } else if (old_state == TCPS_ESTABLISHED || 6732 old_state == TCPS_CLOSE_WAIT) { 6733 BUMP_MIB(&tcp_mib, tcpEstabResets); 6734 } 6735 6736 if (tcp->tcp_fused) 6737 tcp_unfuse(tcp); 6738 6739 mutex_enter(&tcp->tcp_eager_lock); 6740 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6741 (tcp->tcp_conn_req_cnt_q != 0)) { 6742 tcp_eager_cleanup(tcp, 0); 6743 } 6744 mutex_exit(&tcp->tcp_eager_lock); 6745 6746 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6747 tcp->tcp_rnxt, TH_RST | TH_ACK); 6748 6749 tcp_reinit(tcp); 6750 6751 if (old_state >= TCPS_ESTABLISHED) { 6752 /* Send M_FLUSH according to TPI */ 6753 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6754 } 6755 mp = mi_tpi_ok_ack_alloc(mp); 6756 if (mp) 6757 putnext(tcp->tcp_rq, mp); 6758 return; 6759 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6760 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6761 return; 6762 } 6763 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6764 /* Send M_FLUSH according to TPI */ 6765 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6766 } 6767 mp = mi_tpi_ok_ack_alloc(mp); 6768 if (mp) 6769 putnext(tcp->tcp_rq, mp); 6770 } 6771 6772 /* 6773 * Diagnostic routine used to return a string associated with the tcp state. 6774 * Note that if the caller does not supply a buffer, it will use an internal 6775 * static string. This means that if multiple threads call this function at 6776 * the same time, output can be corrupted... Note also that this function 6777 * does not check the size of the supplied buffer. The caller has to make 6778 * sure that it is big enough. 6779 */ 6780 static char * 6781 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6782 { 6783 char buf1[30]; 6784 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6785 char *buf; 6786 char *cp; 6787 in6_addr_t local, remote; 6788 char local_addrbuf[INET6_ADDRSTRLEN]; 6789 char remote_addrbuf[INET6_ADDRSTRLEN]; 6790 6791 if (sup_buf != NULL) 6792 buf = sup_buf; 6793 else 6794 buf = priv_buf; 6795 6796 if (tcp == NULL) 6797 return ("NULL_TCP"); 6798 switch (tcp->tcp_state) { 6799 case TCPS_CLOSED: 6800 cp = "TCP_CLOSED"; 6801 break; 6802 case TCPS_IDLE: 6803 cp = "TCP_IDLE"; 6804 break; 6805 case TCPS_BOUND: 6806 cp = "TCP_BOUND"; 6807 break; 6808 case TCPS_LISTEN: 6809 cp = "TCP_LISTEN"; 6810 break; 6811 case TCPS_SYN_SENT: 6812 cp = "TCP_SYN_SENT"; 6813 break; 6814 case TCPS_SYN_RCVD: 6815 cp = "TCP_SYN_RCVD"; 6816 break; 6817 case TCPS_ESTABLISHED: 6818 cp = "TCP_ESTABLISHED"; 6819 break; 6820 case TCPS_CLOSE_WAIT: 6821 cp = "TCP_CLOSE_WAIT"; 6822 break; 6823 case TCPS_FIN_WAIT_1: 6824 cp = "TCP_FIN_WAIT_1"; 6825 break; 6826 case TCPS_CLOSING: 6827 cp = "TCP_CLOSING"; 6828 break; 6829 case TCPS_LAST_ACK: 6830 cp = "TCP_LAST_ACK"; 6831 break; 6832 case TCPS_FIN_WAIT_2: 6833 cp = "TCP_FIN_WAIT_2"; 6834 break; 6835 case TCPS_TIME_WAIT: 6836 cp = "TCP_TIME_WAIT"; 6837 break; 6838 default: 6839 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6840 cp = buf1; 6841 break; 6842 } 6843 switch (format) { 6844 case DISP_ADDR_AND_PORT: 6845 if (tcp->tcp_ipversion == IPV4_VERSION) { 6846 /* 6847 * Note that we use the remote address in the tcp_b 6848 * structure. This means that it will print out 6849 * the real destination address, not the next hop's 6850 * address if source routing is used. 6851 */ 6852 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6853 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6854 6855 } else { 6856 local = tcp->tcp_ip_src_v6; 6857 remote = tcp->tcp_remote_v6; 6858 } 6859 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6860 sizeof (local_addrbuf)); 6861 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6862 sizeof (remote_addrbuf)); 6863 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6864 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6865 ntohs(tcp->tcp_fport), cp); 6866 break; 6867 case DISP_PORT_ONLY: 6868 default: 6869 (void) mi_sprintf(buf, "[%u, %u] %s", 6870 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6871 break; 6872 } 6873 6874 return (buf); 6875 } 6876 6877 /* 6878 * Called via squeue to get on to eager's perimeter to send a 6879 * TH_RST. The listener wants the eager to disappear either 6880 * by means of tcp_eager_blowoff() or tcp_eager_cleanup() 6881 * being called. 6882 */ 6883 /* ARGSUSED */ 6884 void 6885 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6886 { 6887 conn_t *econnp = (conn_t *)arg; 6888 tcp_t *eager = econnp->conn_tcp; 6889 tcp_t *listener = eager->tcp_listener; 6890 6891 /* 6892 * We could be called because listener is closing. Since 6893 * the eager is using listener's queue's, its not safe. 6894 * Better use the default queue just to send the TH_RST 6895 * out. 6896 */ 6897 eager->tcp_rq = tcp_g_q; 6898 eager->tcp_wq = WR(tcp_g_q); 6899 6900 if (eager->tcp_state > TCPS_LISTEN) { 6901 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6902 eager, eager->tcp_snxt, 0, TH_RST); 6903 } 6904 6905 /* We are here because listener wants this eager gone */ 6906 if (listener != NULL) { 6907 mutex_enter(&listener->tcp_eager_lock); 6908 tcp_eager_unlink(eager); 6909 if (eager->tcp_conn.tcp_eager_conn_ind == NULL) { 6910 /* 6911 * The eager has sent a conn_ind up to the 6912 * listener but listener decides to close 6913 * instead. We need to drop the extra ref 6914 * placed on eager in tcp_rput_data() before 6915 * sending the conn_ind to listener. 6916 */ 6917 CONN_DEC_REF(econnp); 6918 } 6919 mutex_exit(&listener->tcp_eager_lock); 6920 CONN_DEC_REF(listener->tcp_connp); 6921 } 6922 6923 if (eager->tcp_state > TCPS_BOUND) 6924 tcp_close_detached(eager); 6925 } 6926 6927 /* 6928 * Reset any eager connection hanging off this listener marked 6929 * with 'seqnum' and then reclaim it's resources. 6930 */ 6931 static boolean_t 6932 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6933 { 6934 tcp_t *eager; 6935 mblk_t *mp; 6936 6937 TCP_STAT(tcp_eager_blowoff_calls); 6938 eager = listener; 6939 mutex_enter(&listener->tcp_eager_lock); 6940 do { 6941 eager = eager->tcp_eager_next_q; 6942 if (eager == NULL) { 6943 mutex_exit(&listener->tcp_eager_lock); 6944 return (B_FALSE); 6945 } 6946 } while (eager->tcp_conn_req_seqnum != seqnum); 6947 CONN_INC_REF(eager->tcp_connp); 6948 mutex_exit(&listener->tcp_eager_lock); 6949 mp = &eager->tcp_closemp; 6950 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6951 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 6952 return (B_TRUE); 6953 } 6954 6955 /* 6956 * Reset any eager connection hanging off this listener 6957 * and then reclaim it's resources. 6958 */ 6959 static void 6960 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6961 { 6962 tcp_t *eager; 6963 mblk_t *mp; 6964 6965 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6966 6967 if (!q0_only) { 6968 /* First cleanup q */ 6969 TCP_STAT(tcp_eager_blowoff_q); 6970 eager = listener->tcp_eager_next_q; 6971 while (eager != NULL) { 6972 CONN_INC_REF(eager->tcp_connp); 6973 mp = &eager->tcp_closemp; 6974 squeue_fill(eager->tcp_connp->conn_sqp, mp, 6975 tcp_eager_kill, eager->tcp_connp, 6976 SQTAG_TCP_EAGER_CLEANUP); 6977 eager = eager->tcp_eager_next_q; 6978 } 6979 } 6980 /* Then cleanup q0 */ 6981 TCP_STAT(tcp_eager_blowoff_q0); 6982 eager = listener->tcp_eager_next_q0; 6983 while (eager != listener) { 6984 CONN_INC_REF(eager->tcp_connp); 6985 mp = &eager->tcp_closemp; 6986 squeue_fill(eager->tcp_connp->conn_sqp, mp, 6987 tcp_eager_kill, eager->tcp_connp, 6988 SQTAG_TCP_EAGER_CLEANUP_Q0); 6989 eager = eager->tcp_eager_next_q0; 6990 } 6991 } 6992 6993 /* 6994 * If we are an eager connection hanging off a listener that hasn't 6995 * formally accepted the connection yet, get off his list and blow off 6996 * any data that we have accumulated. 6997 */ 6998 static void 6999 tcp_eager_unlink(tcp_t *tcp) 7000 { 7001 tcp_t *listener = tcp->tcp_listener; 7002 7003 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7004 ASSERT(listener != NULL); 7005 if (tcp->tcp_eager_next_q0 != NULL) { 7006 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7007 7008 /* Remove the eager tcp from q0 */ 7009 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7010 tcp->tcp_eager_prev_q0; 7011 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7012 tcp->tcp_eager_next_q0; 7013 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7014 listener->tcp_conn_req_cnt_q0--; 7015 7016 tcp->tcp_eager_next_q0 = NULL; 7017 tcp->tcp_eager_prev_q0 = NULL; 7018 7019 if (tcp->tcp_syn_rcvd_timeout != 0) { 7020 /* we have timed out before */ 7021 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7022 listener->tcp_syn_rcvd_timeout--; 7023 } 7024 } else { 7025 tcp_t **tcpp = &listener->tcp_eager_next_q; 7026 tcp_t *prev = NULL; 7027 7028 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7029 if (tcpp[0] == tcp) { 7030 if (listener->tcp_eager_last_q == tcp) { 7031 /* 7032 * If we are unlinking the last 7033 * element on the list, adjust 7034 * tail pointer. Set tail pointer 7035 * to nil when list is empty. 7036 */ 7037 ASSERT(tcp->tcp_eager_next_q == NULL); 7038 if (listener->tcp_eager_last_q == 7039 listener->tcp_eager_next_q) { 7040 listener->tcp_eager_last_q = 7041 NULL; 7042 } else { 7043 /* 7044 * We won't get here if there 7045 * is only one eager in the 7046 * list. 7047 */ 7048 ASSERT(prev != NULL); 7049 listener->tcp_eager_last_q = 7050 prev; 7051 } 7052 } 7053 tcpp[0] = tcp->tcp_eager_next_q; 7054 tcp->tcp_eager_next_q = NULL; 7055 tcp->tcp_eager_last_q = NULL; 7056 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7057 listener->tcp_conn_req_cnt_q--; 7058 break; 7059 } 7060 prev = tcpp[0]; 7061 } 7062 } 7063 tcp->tcp_listener = NULL; 7064 } 7065 7066 /* Shorthand to generate and send TPI error acks to our client */ 7067 static void 7068 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7069 { 7070 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7071 putnext(tcp->tcp_rq, mp); 7072 } 7073 7074 /* Shorthand to generate and send TPI error acks to our client */ 7075 static void 7076 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7077 int t_error, int sys_error) 7078 { 7079 struct T_error_ack *teackp; 7080 7081 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7082 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7083 teackp = (struct T_error_ack *)mp->b_rptr; 7084 teackp->ERROR_prim = primitive; 7085 teackp->TLI_error = t_error; 7086 teackp->UNIX_error = sys_error; 7087 putnext(tcp->tcp_rq, mp); 7088 } 7089 } 7090 7091 /* 7092 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7093 * but instead the code relies on: 7094 * - the fact that the address of the array and its size never changes 7095 * - the atomic assignment of the elements of the array 7096 */ 7097 /* ARGSUSED */ 7098 static int 7099 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7100 { 7101 int i; 7102 7103 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7104 if (tcp_g_epriv_ports[i] != 0) 7105 (void) mi_mpprintf(mp, "%d ", tcp_g_epriv_ports[i]); 7106 } 7107 return (0); 7108 } 7109 7110 /* 7111 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7112 * threads from changing it at the same time. 7113 */ 7114 /* ARGSUSED */ 7115 static int 7116 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7117 cred_t *cr) 7118 { 7119 long new_value; 7120 int i; 7121 7122 /* 7123 * Fail the request if the new value does not lie within the 7124 * port number limits. 7125 */ 7126 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7127 new_value <= 0 || new_value >= 65536) { 7128 return (EINVAL); 7129 } 7130 7131 mutex_enter(&tcp_epriv_port_lock); 7132 /* Check if the value is already in the list */ 7133 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7134 if (new_value == tcp_g_epriv_ports[i]) { 7135 mutex_exit(&tcp_epriv_port_lock); 7136 return (EEXIST); 7137 } 7138 } 7139 /* Find an empty slot */ 7140 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7141 if (tcp_g_epriv_ports[i] == 0) 7142 break; 7143 } 7144 if (i == tcp_g_num_epriv_ports) { 7145 mutex_exit(&tcp_epriv_port_lock); 7146 return (EOVERFLOW); 7147 } 7148 /* Set the new value */ 7149 tcp_g_epriv_ports[i] = (uint16_t)new_value; 7150 mutex_exit(&tcp_epriv_port_lock); 7151 return (0); 7152 } 7153 7154 /* 7155 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7156 * threads from changing it at the same time. 7157 */ 7158 /* ARGSUSED */ 7159 static int 7160 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7161 cred_t *cr) 7162 { 7163 long new_value; 7164 int i; 7165 7166 /* 7167 * Fail the request if the new value does not lie within the 7168 * port number limits. 7169 */ 7170 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7171 new_value >= 65536) { 7172 return (EINVAL); 7173 } 7174 7175 mutex_enter(&tcp_epriv_port_lock); 7176 /* Check that the value is already in the list */ 7177 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7178 if (tcp_g_epriv_ports[i] == new_value) 7179 break; 7180 } 7181 if (i == tcp_g_num_epriv_ports) { 7182 mutex_exit(&tcp_epriv_port_lock); 7183 return (ESRCH); 7184 } 7185 /* Clear the value */ 7186 tcp_g_epriv_ports[i] = 0; 7187 mutex_exit(&tcp_epriv_port_lock); 7188 return (0); 7189 } 7190 7191 /* Return the TPI/TLI equivalent of our current tcp_state */ 7192 static int 7193 tcp_tpistate(tcp_t *tcp) 7194 { 7195 switch (tcp->tcp_state) { 7196 case TCPS_IDLE: 7197 return (TS_UNBND); 7198 case TCPS_LISTEN: 7199 /* 7200 * Return whether there are outstanding T_CONN_IND waiting 7201 * for the matching T_CONN_RES. Therefore don't count q0. 7202 */ 7203 if (tcp->tcp_conn_req_cnt_q > 0) 7204 return (TS_WRES_CIND); 7205 else 7206 return (TS_IDLE); 7207 case TCPS_BOUND: 7208 return (TS_IDLE); 7209 case TCPS_SYN_SENT: 7210 return (TS_WCON_CREQ); 7211 case TCPS_SYN_RCVD: 7212 /* 7213 * Note: assumption: this has to the active open SYN_RCVD. 7214 * The passive instance is detached in SYN_RCVD stage of 7215 * incoming connection processing so we cannot get request 7216 * for T_info_ack on it. 7217 */ 7218 return (TS_WACK_CRES); 7219 case TCPS_ESTABLISHED: 7220 return (TS_DATA_XFER); 7221 case TCPS_CLOSE_WAIT: 7222 return (TS_WREQ_ORDREL); 7223 case TCPS_FIN_WAIT_1: 7224 return (TS_WIND_ORDREL); 7225 case TCPS_FIN_WAIT_2: 7226 return (TS_WIND_ORDREL); 7227 7228 case TCPS_CLOSING: 7229 case TCPS_LAST_ACK: 7230 case TCPS_TIME_WAIT: 7231 case TCPS_CLOSED: 7232 /* 7233 * Following TS_WACK_DREQ7 is a rendition of "not 7234 * yet TS_IDLE" TPI state. There is no best match to any 7235 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7236 * choose a value chosen that will map to TLI/XTI level 7237 * state of TSTATECHNG (state is process of changing) which 7238 * captures what this dummy state represents. 7239 */ 7240 return (TS_WACK_DREQ7); 7241 default: 7242 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7243 tcp->tcp_state, tcp_display(tcp, NULL, 7244 DISP_PORT_ONLY)); 7245 return (TS_UNBND); 7246 } 7247 } 7248 7249 static void 7250 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7251 { 7252 if (tcp->tcp_family == AF_INET6) 7253 *tia = tcp_g_t_info_ack_v6; 7254 else 7255 *tia = tcp_g_t_info_ack; 7256 tia->CURRENT_state = tcp_tpistate(tcp); 7257 tia->OPT_size = tcp_max_optsize; 7258 if (tcp->tcp_mss == 0) { 7259 /* Not yet set - tcp_open does not set mss */ 7260 if (tcp->tcp_ipversion == IPV4_VERSION) 7261 tia->TIDU_size = tcp_mss_def_ipv4; 7262 else 7263 tia->TIDU_size = tcp_mss_def_ipv6; 7264 } else { 7265 tia->TIDU_size = tcp->tcp_mss; 7266 } 7267 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7268 } 7269 7270 /* 7271 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7272 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7273 * tcp_g_t_info_ack. The current state of the stream is copied from 7274 * tcp_state. 7275 */ 7276 static void 7277 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7278 { 7279 t_uscalar_t cap_bits1; 7280 struct T_capability_ack *tcap; 7281 7282 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7283 freemsg(mp); 7284 return; 7285 } 7286 7287 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7288 7289 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7290 mp->b_datap->db_type, T_CAPABILITY_ACK); 7291 if (mp == NULL) 7292 return; 7293 7294 tcap = (struct T_capability_ack *)mp->b_rptr; 7295 tcap->CAP_bits1 = 0; 7296 7297 if (cap_bits1 & TC1_INFO) { 7298 tcp_copy_info(&tcap->INFO_ack, tcp); 7299 tcap->CAP_bits1 |= TC1_INFO; 7300 } 7301 7302 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7303 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7304 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7305 } 7306 7307 putnext(tcp->tcp_rq, mp); 7308 } 7309 7310 /* 7311 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7312 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7313 * The current state of the stream is copied from tcp_state. 7314 */ 7315 static void 7316 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7317 { 7318 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7319 T_INFO_ACK); 7320 if (!mp) { 7321 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7322 return; 7323 } 7324 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7325 putnext(tcp->tcp_rq, mp); 7326 } 7327 7328 /* Respond to the TPI addr request */ 7329 static void 7330 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7331 { 7332 sin_t *sin; 7333 mblk_t *ackmp; 7334 struct T_addr_ack *taa; 7335 7336 /* Make it large enough for worst case */ 7337 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7338 2 * sizeof (sin6_t), 1); 7339 if (ackmp == NULL) { 7340 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7341 return; 7342 } 7343 7344 if (tcp->tcp_ipversion == IPV6_VERSION) { 7345 tcp_addr_req_ipv6(tcp, ackmp); 7346 return; 7347 } 7348 taa = (struct T_addr_ack *)ackmp->b_rptr; 7349 7350 bzero(taa, sizeof (struct T_addr_ack)); 7351 ackmp->b_wptr = (uchar_t *)&taa[1]; 7352 7353 taa->PRIM_type = T_ADDR_ACK; 7354 ackmp->b_datap->db_type = M_PCPROTO; 7355 7356 /* 7357 * Note: Following code assumes 32 bit alignment of basic 7358 * data structures like sin_t and struct T_addr_ack. 7359 */ 7360 if (tcp->tcp_state >= TCPS_BOUND) { 7361 /* 7362 * Fill in local address 7363 */ 7364 taa->LOCADDR_length = sizeof (sin_t); 7365 taa->LOCADDR_offset = sizeof (*taa); 7366 7367 sin = (sin_t *)&taa[1]; 7368 7369 /* Fill zeroes and then intialize non-zero fields */ 7370 *sin = sin_null; 7371 7372 sin->sin_family = AF_INET; 7373 7374 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7375 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7376 7377 ackmp->b_wptr = (uchar_t *)&sin[1]; 7378 7379 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7380 /* 7381 * Fill in Remote address 7382 */ 7383 taa->REMADDR_length = sizeof (sin_t); 7384 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7385 taa->LOCADDR_length); 7386 7387 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7388 *sin = sin_null; 7389 sin->sin_family = AF_INET; 7390 sin->sin_addr.s_addr = tcp->tcp_remote; 7391 sin->sin_port = tcp->tcp_fport; 7392 7393 ackmp->b_wptr = (uchar_t *)&sin[1]; 7394 } 7395 } 7396 putnext(tcp->tcp_rq, ackmp); 7397 } 7398 7399 /* Assumes that tcp_addr_req gets enough space and alignment */ 7400 static void 7401 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7402 { 7403 sin6_t *sin6; 7404 struct T_addr_ack *taa; 7405 7406 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7407 ASSERT(OK_32PTR(ackmp->b_rptr)); 7408 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7409 2 * sizeof (sin6_t)); 7410 7411 taa = (struct T_addr_ack *)ackmp->b_rptr; 7412 7413 bzero(taa, sizeof (struct T_addr_ack)); 7414 ackmp->b_wptr = (uchar_t *)&taa[1]; 7415 7416 taa->PRIM_type = T_ADDR_ACK; 7417 ackmp->b_datap->db_type = M_PCPROTO; 7418 7419 /* 7420 * Note: Following code assumes 32 bit alignment of basic 7421 * data structures like sin6_t and struct T_addr_ack. 7422 */ 7423 if (tcp->tcp_state >= TCPS_BOUND) { 7424 /* 7425 * Fill in local address 7426 */ 7427 taa->LOCADDR_length = sizeof (sin6_t); 7428 taa->LOCADDR_offset = sizeof (*taa); 7429 7430 sin6 = (sin6_t *)&taa[1]; 7431 *sin6 = sin6_null; 7432 7433 sin6->sin6_family = AF_INET6; 7434 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7435 sin6->sin6_port = tcp->tcp_lport; 7436 7437 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7438 7439 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7440 /* 7441 * Fill in Remote address 7442 */ 7443 taa->REMADDR_length = sizeof (sin6_t); 7444 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7445 taa->LOCADDR_length); 7446 7447 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7448 *sin6 = sin6_null; 7449 sin6->sin6_family = AF_INET6; 7450 sin6->sin6_flowinfo = 7451 tcp->tcp_ip6h->ip6_vcf & 7452 ~IPV6_VERS_AND_FLOW_MASK; 7453 sin6->sin6_addr = tcp->tcp_remote_v6; 7454 sin6->sin6_port = tcp->tcp_fport; 7455 7456 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7457 } 7458 } 7459 putnext(tcp->tcp_rq, ackmp); 7460 } 7461 7462 /* 7463 * Handle reinitialization of a tcp structure. 7464 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7465 */ 7466 static void 7467 tcp_reinit(tcp_t *tcp) 7468 { 7469 mblk_t *mp; 7470 int err; 7471 7472 TCP_STAT(tcp_reinit_calls); 7473 7474 /* tcp_reinit should never be called for detached tcp_t's */ 7475 ASSERT(tcp->tcp_listener == NULL); 7476 ASSERT((tcp->tcp_family == AF_INET && 7477 tcp->tcp_ipversion == IPV4_VERSION) || 7478 (tcp->tcp_family == AF_INET6 && 7479 (tcp->tcp_ipversion == IPV4_VERSION || 7480 tcp->tcp_ipversion == IPV6_VERSION))); 7481 7482 /* Cancel outstanding timers */ 7483 tcp_timers_stop(tcp); 7484 7485 /* 7486 * Reset everything in the state vector, after updating global 7487 * MIB data from instance counters. 7488 */ 7489 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 7490 tcp->tcp_ibsegs = 0; 7491 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 7492 tcp->tcp_obsegs = 0; 7493 7494 tcp_close_mpp(&tcp->tcp_xmit_head); 7495 if (tcp->tcp_snd_zcopy_aware) 7496 tcp_zcopy_notify(tcp); 7497 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7498 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7499 if (tcp->tcp_flow_stopped && 7500 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7501 tcp_clrqfull(tcp); 7502 } 7503 tcp_close_mpp(&tcp->tcp_reass_head); 7504 tcp->tcp_reass_tail = NULL; 7505 if (tcp->tcp_rcv_list != NULL) { 7506 /* Free b_next chain */ 7507 tcp_close_mpp(&tcp->tcp_rcv_list); 7508 tcp->tcp_rcv_last_head = NULL; 7509 tcp->tcp_rcv_last_tail = NULL; 7510 tcp->tcp_rcv_cnt = 0; 7511 } 7512 tcp->tcp_rcv_last_tail = NULL; 7513 7514 if ((mp = tcp->tcp_urp_mp) != NULL) { 7515 freemsg(mp); 7516 tcp->tcp_urp_mp = NULL; 7517 } 7518 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7519 freemsg(mp); 7520 tcp->tcp_urp_mark_mp = NULL; 7521 } 7522 if (tcp->tcp_fused_sigurg_mp != NULL) { 7523 freeb(tcp->tcp_fused_sigurg_mp); 7524 tcp->tcp_fused_sigurg_mp = NULL; 7525 } 7526 7527 /* 7528 * Following is a union with two members which are 7529 * identical types and size so the following cleanup 7530 * is enough. 7531 */ 7532 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7533 7534 CL_INET_DISCONNECT(tcp); 7535 7536 /* 7537 * The connection can't be on the tcp_time_wait_head list 7538 * since it is not detached. 7539 */ 7540 ASSERT(tcp->tcp_time_wait_next == NULL); 7541 ASSERT(tcp->tcp_time_wait_prev == NULL); 7542 ASSERT(tcp->tcp_time_wait_expire == 0); 7543 7544 if (tcp->tcp_kssl_pending) { 7545 tcp->tcp_kssl_pending = B_FALSE; 7546 7547 /* Don't reset if the initialized by bind. */ 7548 if (tcp->tcp_kssl_ent != NULL) { 7549 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7550 KSSL_NO_PROXY); 7551 } 7552 } 7553 if (tcp->tcp_kssl_ctx != NULL) { 7554 kssl_release_ctx(tcp->tcp_kssl_ctx); 7555 tcp->tcp_kssl_ctx = NULL; 7556 } 7557 7558 /* 7559 * Reset/preserve other values 7560 */ 7561 tcp_reinit_values(tcp); 7562 ipcl_hash_remove(tcp->tcp_connp); 7563 conn_delete_ire(tcp->tcp_connp, NULL); 7564 7565 if (tcp->tcp_conn_req_max != 0) { 7566 /* 7567 * This is the case when a TLI program uses the same 7568 * transport end point to accept a connection. This 7569 * makes the TCP both a listener and acceptor. When 7570 * this connection is closed, we need to set the state 7571 * back to TCPS_LISTEN. Make sure that the eager list 7572 * is reinitialized. 7573 * 7574 * Note that this stream is still bound to the four 7575 * tuples of the previous connection in IP. If a new 7576 * SYN with different foreign address comes in, IP will 7577 * not find it and will send it to the global queue. In 7578 * the global queue, TCP will do a tcp_lookup_listener() 7579 * to find this stream. This works because this stream 7580 * is only removed from connected hash. 7581 * 7582 */ 7583 tcp->tcp_state = TCPS_LISTEN; 7584 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7585 tcp->tcp_connp->conn_recv = tcp_conn_request; 7586 if (tcp->tcp_family == AF_INET6) { 7587 ASSERT(tcp->tcp_connp->conn_af_isv6); 7588 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7589 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7590 } else { 7591 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7592 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7593 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7594 } 7595 } else { 7596 tcp->tcp_state = TCPS_BOUND; 7597 } 7598 7599 /* 7600 * Initialize to default values 7601 * Can't fail since enough header template space already allocated 7602 * at open(). 7603 */ 7604 err = tcp_init_values(tcp); 7605 ASSERT(err == 0); 7606 /* Restore state in tcp_tcph */ 7607 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7608 if (tcp->tcp_ipversion == IPV4_VERSION) 7609 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7610 else 7611 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7612 /* 7613 * Copy of the src addr. in tcp_t is needed in tcp_t 7614 * since the lookup funcs can only lookup on tcp_t 7615 */ 7616 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7617 7618 ASSERT(tcp->tcp_ptpbhn != NULL); 7619 tcp->tcp_rq->q_hiwat = tcp_recv_hiwat; 7620 tcp->tcp_rwnd = tcp_recv_hiwat; 7621 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7622 tcp_mss_def_ipv6 : tcp_mss_def_ipv4; 7623 } 7624 7625 /* 7626 * Force values to zero that need be zero. 7627 * Do not touch values asociated with the BOUND or LISTEN state 7628 * since the connection will end up in that state after the reinit. 7629 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7630 * structure! 7631 */ 7632 static void 7633 tcp_reinit_values(tcp) 7634 tcp_t *tcp; 7635 { 7636 #ifndef lint 7637 #define DONTCARE(x) 7638 #define PRESERVE(x) 7639 #else 7640 #define DONTCARE(x) ((x) = (x)) 7641 #define PRESERVE(x) ((x) = (x)) 7642 #endif /* lint */ 7643 7644 PRESERVE(tcp->tcp_bind_hash); 7645 PRESERVE(tcp->tcp_ptpbhn); 7646 PRESERVE(tcp->tcp_acceptor_hash); 7647 PRESERVE(tcp->tcp_ptpahn); 7648 7649 /* Should be ASSERT NULL on these with new code! */ 7650 ASSERT(tcp->tcp_time_wait_next == NULL); 7651 ASSERT(tcp->tcp_time_wait_prev == NULL); 7652 ASSERT(tcp->tcp_time_wait_expire == 0); 7653 PRESERVE(tcp->tcp_state); 7654 PRESERVE(tcp->tcp_rq); 7655 PRESERVE(tcp->tcp_wq); 7656 7657 ASSERT(tcp->tcp_xmit_head == NULL); 7658 ASSERT(tcp->tcp_xmit_last == NULL); 7659 ASSERT(tcp->tcp_unsent == 0); 7660 ASSERT(tcp->tcp_xmit_tail == NULL); 7661 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7662 7663 tcp->tcp_snxt = 0; /* Displayed in mib */ 7664 tcp->tcp_suna = 0; /* Displayed in mib */ 7665 tcp->tcp_swnd = 0; 7666 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7667 7668 ASSERT(tcp->tcp_ibsegs == 0); 7669 ASSERT(tcp->tcp_obsegs == 0); 7670 7671 if (tcp->tcp_iphc != NULL) { 7672 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7673 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7674 } 7675 7676 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7677 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7678 DONTCARE(tcp->tcp_ipha); 7679 DONTCARE(tcp->tcp_ip6h); 7680 DONTCARE(tcp->tcp_ip_hdr_len); 7681 DONTCARE(tcp->tcp_tcph); 7682 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7683 tcp->tcp_valid_bits = 0; 7684 7685 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7686 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7687 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7688 tcp->tcp_last_rcv_lbolt = 0; 7689 7690 tcp->tcp_init_cwnd = 0; 7691 7692 tcp->tcp_urp_last_valid = 0; 7693 tcp->tcp_hard_binding = 0; 7694 tcp->tcp_hard_bound = 0; 7695 PRESERVE(tcp->tcp_cred); 7696 PRESERVE(tcp->tcp_cpid); 7697 PRESERVE(tcp->tcp_exclbind); 7698 7699 tcp->tcp_fin_acked = 0; 7700 tcp->tcp_fin_rcvd = 0; 7701 tcp->tcp_fin_sent = 0; 7702 tcp->tcp_ordrel_done = 0; 7703 7704 tcp->tcp_debug = 0; 7705 tcp->tcp_dontroute = 0; 7706 tcp->tcp_broadcast = 0; 7707 7708 tcp->tcp_useloopback = 0; 7709 tcp->tcp_reuseaddr = 0; 7710 tcp->tcp_oobinline = 0; 7711 tcp->tcp_dgram_errind = 0; 7712 7713 tcp->tcp_detached = 0; 7714 tcp->tcp_bind_pending = 0; 7715 tcp->tcp_unbind_pending = 0; 7716 tcp->tcp_deferred_clean_death = 0; 7717 7718 tcp->tcp_snd_ws_ok = B_FALSE; 7719 tcp->tcp_snd_ts_ok = B_FALSE; 7720 tcp->tcp_linger = 0; 7721 tcp->tcp_ka_enabled = 0; 7722 tcp->tcp_zero_win_probe = 0; 7723 7724 tcp->tcp_loopback = 0; 7725 tcp->tcp_localnet = 0; 7726 tcp->tcp_syn_defense = 0; 7727 tcp->tcp_set_timer = 0; 7728 7729 tcp->tcp_active_open = 0; 7730 ASSERT(tcp->tcp_timeout == B_FALSE); 7731 tcp->tcp_rexmit = B_FALSE; 7732 tcp->tcp_xmit_zc_clean = B_FALSE; 7733 7734 tcp->tcp_snd_sack_ok = B_FALSE; 7735 PRESERVE(tcp->tcp_recvdstaddr); 7736 tcp->tcp_hwcksum = B_FALSE; 7737 7738 tcp->tcp_ire_ill_check_done = B_FALSE; 7739 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7740 7741 tcp->tcp_mdt = B_FALSE; 7742 tcp->tcp_mdt_hdr_head = 0; 7743 tcp->tcp_mdt_hdr_tail = 0; 7744 7745 tcp->tcp_conn_def_q0 = 0; 7746 tcp->tcp_ip_forward_progress = B_FALSE; 7747 tcp->tcp_anon_priv_bind = 0; 7748 tcp->tcp_ecn_ok = B_FALSE; 7749 7750 tcp->tcp_cwr = B_FALSE; 7751 tcp->tcp_ecn_echo_on = B_FALSE; 7752 7753 if (tcp->tcp_sack_info != NULL) { 7754 if (tcp->tcp_notsack_list != NULL) { 7755 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7756 } 7757 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7758 tcp->tcp_sack_info = NULL; 7759 } 7760 7761 tcp->tcp_rcv_ws = 0; 7762 tcp->tcp_snd_ws = 0; 7763 tcp->tcp_ts_recent = 0; 7764 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7765 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7766 tcp->tcp_if_mtu = 0; 7767 7768 ASSERT(tcp->tcp_reass_head == NULL); 7769 ASSERT(tcp->tcp_reass_tail == NULL); 7770 7771 tcp->tcp_cwnd_cnt = 0; 7772 7773 ASSERT(tcp->tcp_rcv_list == NULL); 7774 ASSERT(tcp->tcp_rcv_last_head == NULL); 7775 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7776 ASSERT(tcp->tcp_rcv_cnt == 0); 7777 7778 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7779 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7780 tcp->tcp_csuna = 0; 7781 7782 tcp->tcp_rto = 0; /* Displayed in MIB */ 7783 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7784 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7785 tcp->tcp_rtt_update = 0; 7786 7787 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7788 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7789 7790 tcp->tcp_rack = 0; /* Displayed in mib */ 7791 tcp->tcp_rack_cnt = 0; 7792 tcp->tcp_rack_cur_max = 0; 7793 tcp->tcp_rack_abs_max = 0; 7794 7795 tcp->tcp_max_swnd = 0; 7796 7797 ASSERT(tcp->tcp_listener == NULL); 7798 7799 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7800 7801 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7802 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7803 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7804 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7805 7806 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7807 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7808 PRESERVE(tcp->tcp_conn_req_max); 7809 PRESERVE(tcp->tcp_conn_req_seqnum); 7810 7811 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7812 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7813 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7814 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7815 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7816 7817 tcp->tcp_lingertime = 0; 7818 7819 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7820 ASSERT(tcp->tcp_urp_mp == NULL); 7821 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7822 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7823 7824 ASSERT(tcp->tcp_eager_next_q == NULL); 7825 ASSERT(tcp->tcp_eager_last_q == NULL); 7826 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7827 tcp->tcp_eager_prev_q0 == NULL) || 7828 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7829 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7830 7831 tcp->tcp_client_errno = 0; 7832 7833 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7834 7835 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7836 7837 PRESERVE(tcp->tcp_bound_source_v6); 7838 tcp->tcp_last_sent_len = 0; 7839 tcp->tcp_dupack_cnt = 0; 7840 7841 tcp->tcp_fport = 0; /* Displayed in MIB */ 7842 PRESERVE(tcp->tcp_lport); 7843 7844 PRESERVE(tcp->tcp_acceptor_lockp); 7845 7846 ASSERT(tcp->tcp_ordrelid == 0); 7847 PRESERVE(tcp->tcp_acceptor_id); 7848 DONTCARE(tcp->tcp_ipsec_overhead); 7849 7850 /* 7851 * If tcp_tracing flag is ON (i.e. We have a trace buffer 7852 * in tcp structure and now tracing), Re-initialize all 7853 * members of tcp_traceinfo. 7854 */ 7855 if (tcp->tcp_tracebuf != NULL) { 7856 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 7857 } 7858 7859 PRESERVE(tcp->tcp_family); 7860 if (tcp->tcp_family == AF_INET6) { 7861 tcp->tcp_ipversion = IPV6_VERSION; 7862 tcp->tcp_mss = tcp_mss_def_ipv6; 7863 } else { 7864 tcp->tcp_ipversion = IPV4_VERSION; 7865 tcp->tcp_mss = tcp_mss_def_ipv4; 7866 } 7867 7868 tcp->tcp_bound_if = 0; 7869 tcp->tcp_ipv6_recvancillary = 0; 7870 tcp->tcp_recvifindex = 0; 7871 tcp->tcp_recvhops = 0; 7872 tcp->tcp_closed = 0; 7873 tcp->tcp_cleandeathtag = 0; 7874 if (tcp->tcp_hopopts != NULL) { 7875 mi_free(tcp->tcp_hopopts); 7876 tcp->tcp_hopopts = NULL; 7877 tcp->tcp_hopoptslen = 0; 7878 } 7879 ASSERT(tcp->tcp_hopoptslen == 0); 7880 if (tcp->tcp_dstopts != NULL) { 7881 mi_free(tcp->tcp_dstopts); 7882 tcp->tcp_dstopts = NULL; 7883 tcp->tcp_dstoptslen = 0; 7884 } 7885 ASSERT(tcp->tcp_dstoptslen == 0); 7886 if (tcp->tcp_rtdstopts != NULL) { 7887 mi_free(tcp->tcp_rtdstopts); 7888 tcp->tcp_rtdstopts = NULL; 7889 tcp->tcp_rtdstoptslen = 0; 7890 } 7891 ASSERT(tcp->tcp_rtdstoptslen == 0); 7892 if (tcp->tcp_rthdr != NULL) { 7893 mi_free(tcp->tcp_rthdr); 7894 tcp->tcp_rthdr = NULL; 7895 tcp->tcp_rthdrlen = 0; 7896 } 7897 ASSERT(tcp->tcp_rthdrlen == 0); 7898 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7899 7900 /* Reset fusion-related fields */ 7901 tcp->tcp_fused = B_FALSE; 7902 tcp->tcp_unfusable = B_FALSE; 7903 tcp->tcp_fused_sigurg = B_FALSE; 7904 tcp->tcp_direct_sockfs = B_FALSE; 7905 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7906 tcp->tcp_loopback_peer = NULL; 7907 tcp->tcp_fuse_rcv_hiwater = 0; 7908 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7909 tcp->tcp_fuse_rcv_unread_cnt = 0; 7910 7911 tcp->tcp_in_ack_unsent = 0; 7912 tcp->tcp_cork = B_FALSE; 7913 7914 PRESERVE(tcp->tcp_squeue_bytes); 7915 7916 ASSERT(tcp->tcp_kssl_ctx == NULL); 7917 ASSERT(!tcp->tcp_kssl_pending); 7918 PRESERVE(tcp->tcp_kssl_ent); 7919 7920 #undef DONTCARE 7921 #undef PRESERVE 7922 } 7923 7924 /* 7925 * Allocate necessary resources and initialize state vector. 7926 * Guaranteed not to fail so that when an error is returned, 7927 * the caller doesn't need to do any additional cleanup. 7928 */ 7929 int 7930 tcp_init(tcp_t *tcp, queue_t *q) 7931 { 7932 int err; 7933 7934 tcp->tcp_rq = q; 7935 tcp->tcp_wq = WR(q); 7936 tcp->tcp_state = TCPS_IDLE; 7937 if ((err = tcp_init_values(tcp)) != 0) 7938 tcp_timers_stop(tcp); 7939 return (err); 7940 } 7941 7942 static int 7943 tcp_init_values(tcp_t *tcp) 7944 { 7945 int err; 7946 7947 ASSERT((tcp->tcp_family == AF_INET && 7948 tcp->tcp_ipversion == IPV4_VERSION) || 7949 (tcp->tcp_family == AF_INET6 && 7950 (tcp->tcp_ipversion == IPV4_VERSION || 7951 tcp->tcp_ipversion == IPV6_VERSION))); 7952 7953 /* 7954 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7955 * will be close to tcp_rexmit_interval_initial. By doing this, we 7956 * allow the algorithm to adjust slowly to large fluctuations of RTT 7957 * during first few transmissions of a connection as seen in slow 7958 * links. 7959 */ 7960 tcp->tcp_rtt_sa = tcp_rexmit_interval_initial << 2; 7961 tcp->tcp_rtt_sd = tcp_rexmit_interval_initial >> 1; 7962 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7963 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7964 tcp_conn_grace_period; 7965 if (tcp->tcp_rto < tcp_rexmit_interval_min) 7966 tcp->tcp_rto = tcp_rexmit_interval_min; 7967 tcp->tcp_timer_backoff = 0; 7968 tcp->tcp_ms_we_have_waited = 0; 7969 tcp->tcp_last_recv_time = lbolt; 7970 tcp->tcp_cwnd_max = tcp_cwnd_max_; 7971 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7972 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7973 7974 tcp->tcp_maxpsz = tcp_maxpsz_multiplier; 7975 7976 tcp->tcp_first_timer_threshold = tcp_ip_notify_interval; 7977 tcp->tcp_first_ctimer_threshold = tcp_ip_notify_cinterval; 7978 tcp->tcp_second_timer_threshold = tcp_ip_abort_interval; 7979 /* 7980 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7981 * passive open. 7982 */ 7983 tcp->tcp_second_ctimer_threshold = tcp_ip_abort_cinterval; 7984 7985 tcp->tcp_naglim = tcp_naglim_def; 7986 7987 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7988 7989 tcp->tcp_mdt_hdr_head = 0; 7990 tcp->tcp_mdt_hdr_tail = 0; 7991 7992 /* Reset fusion-related fields */ 7993 tcp->tcp_fused = B_FALSE; 7994 tcp->tcp_unfusable = B_FALSE; 7995 tcp->tcp_fused_sigurg = B_FALSE; 7996 tcp->tcp_direct_sockfs = B_FALSE; 7997 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7998 tcp->tcp_loopback_peer = NULL; 7999 tcp->tcp_fuse_rcv_hiwater = 0; 8000 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8001 tcp->tcp_fuse_rcv_unread_cnt = 0; 8002 8003 /* Initialize the header template */ 8004 if (tcp->tcp_ipversion == IPV4_VERSION) { 8005 err = tcp_header_init_ipv4(tcp); 8006 } else { 8007 err = tcp_header_init_ipv6(tcp); 8008 } 8009 if (err) 8010 return (err); 8011 8012 /* 8013 * Init the window scale to the max so tcp_rwnd_set() won't pare 8014 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8015 */ 8016 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8017 tcp->tcp_xmit_lowater = tcp_xmit_lowat; 8018 tcp->tcp_xmit_hiwater = tcp_xmit_hiwat; 8019 8020 tcp->tcp_cork = B_FALSE; 8021 /* 8022 * Init the tcp_debug option. This value determines whether TCP 8023 * calls strlog() to print out debug messages. Doing this 8024 * initialization here means that this value is not inherited thru 8025 * tcp_reinit(). 8026 */ 8027 tcp->tcp_debug = tcp_dbg; 8028 8029 tcp->tcp_ka_interval = tcp_keepalive_interval; 8030 tcp->tcp_ka_abort_thres = tcp_keepalive_abort_interval; 8031 8032 return (0); 8033 } 8034 8035 /* 8036 * Initialize the IPv4 header. Loses any record of any IP options. 8037 */ 8038 static int 8039 tcp_header_init_ipv4(tcp_t *tcp) 8040 { 8041 tcph_t *tcph; 8042 uint32_t sum; 8043 conn_t *connp; 8044 8045 /* 8046 * This is a simple initialization. If there's 8047 * already a template, it should never be too small, 8048 * so reuse it. Otherwise, allocate space for the new one. 8049 */ 8050 if (tcp->tcp_iphc == NULL) { 8051 ASSERT(tcp->tcp_iphc_len == 0); 8052 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8053 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8054 if (tcp->tcp_iphc == NULL) { 8055 tcp->tcp_iphc_len = 0; 8056 return (ENOMEM); 8057 } 8058 } 8059 8060 /* options are gone; may need a new label */ 8061 connp = tcp->tcp_connp; 8062 connp->conn_mlp_type = mlptSingle; 8063 connp->conn_ulp_labeled = !is_system_labeled(); 8064 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8065 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8066 tcp->tcp_ip6h = NULL; 8067 tcp->tcp_ipversion = IPV4_VERSION; 8068 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8069 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8070 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8071 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8072 tcp->tcp_ipha->ipha_version_and_hdr_length 8073 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8074 tcp->tcp_ipha->ipha_ident = 0; 8075 8076 tcp->tcp_ttl = (uchar_t)tcp_ipv4_ttl; 8077 tcp->tcp_tos = 0; 8078 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8079 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcp_ipv4_ttl; 8080 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8081 8082 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8083 tcp->tcp_tcph = tcph; 8084 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8085 /* 8086 * IP wants our header length in the checksum field to 8087 * allow it to perform a single pseudo-header+checksum 8088 * calculation on behalf of TCP. 8089 * Include the adjustment for a source route once IP_OPTIONS is set. 8090 */ 8091 sum = sizeof (tcph_t) + tcp->tcp_sum; 8092 sum = (sum >> 16) + (sum & 0xFFFF); 8093 U16_TO_ABE16(sum, tcph->th_sum); 8094 return (0); 8095 } 8096 8097 /* 8098 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8099 */ 8100 static int 8101 tcp_header_init_ipv6(tcp_t *tcp) 8102 { 8103 tcph_t *tcph; 8104 uint32_t sum; 8105 conn_t *connp; 8106 8107 /* 8108 * This is a simple initialization. If there's 8109 * already a template, it should never be too small, 8110 * so reuse it. Otherwise, allocate space for the new one. 8111 * Ensure that there is enough space to "downgrade" the tcp_t 8112 * to an IPv4 tcp_t. This requires having space for a full load 8113 * of IPv4 options, as well as a full load of TCP options 8114 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8115 * than a v6 header and a TCP header with a full load of TCP options 8116 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8117 * We want to avoid reallocation in the "downgraded" case when 8118 * processing outbound IPv4 options. 8119 */ 8120 if (tcp->tcp_iphc == NULL) { 8121 ASSERT(tcp->tcp_iphc_len == 0); 8122 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8123 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8124 if (tcp->tcp_iphc == NULL) { 8125 tcp->tcp_iphc_len = 0; 8126 return (ENOMEM); 8127 } 8128 } 8129 8130 /* options are gone; may need a new label */ 8131 connp = tcp->tcp_connp; 8132 connp->conn_mlp_type = mlptSingle; 8133 connp->conn_ulp_labeled = !is_system_labeled(); 8134 8135 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8136 tcp->tcp_ipversion = IPV6_VERSION; 8137 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8138 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8139 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8140 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8141 tcp->tcp_ipha = NULL; 8142 8143 /* Initialize the header template */ 8144 8145 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8146 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8147 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8148 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcp_ipv6_hoplimit; 8149 8150 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8151 tcp->tcp_tcph = tcph; 8152 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8153 /* 8154 * IP wants our header length in the checksum field to 8155 * allow it to perform a single psuedo-header+checksum 8156 * calculation on behalf of TCP. 8157 * Include the adjustment for a source route when IPV6_RTHDR is set. 8158 */ 8159 sum = sizeof (tcph_t) + tcp->tcp_sum; 8160 sum = (sum >> 16) + (sum & 0xFFFF); 8161 U16_TO_ABE16(sum, tcph->th_sum); 8162 return (0); 8163 } 8164 8165 /* At minimum we need 4 bytes in the TCP header for the lookup */ 8166 #define ICMP_MIN_TCP_HDR 12 8167 8168 /* 8169 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8170 * passed up by IP. The message is always received on the correct tcp_t. 8171 * Assumes that IP has pulled up everything up to and including the ICMP header. 8172 */ 8173 void 8174 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8175 { 8176 icmph_t *icmph; 8177 ipha_t *ipha; 8178 int iph_hdr_length; 8179 tcph_t *tcph; 8180 boolean_t ipsec_mctl = B_FALSE; 8181 boolean_t secure; 8182 mblk_t *first_mp = mp; 8183 uint32_t new_mss; 8184 uint32_t ratio; 8185 size_t mp_size = MBLKL(mp); 8186 uint32_t seg_ack; 8187 uint32_t seg_seq; 8188 8189 /* Assume IP provides aligned packets - otherwise toss */ 8190 if (!OK_32PTR(mp->b_rptr)) { 8191 freemsg(mp); 8192 return; 8193 } 8194 8195 /* 8196 * Since ICMP errors are normal data marked with M_CTL when sent 8197 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8198 * packets starting with an ipsec_info_t, see ipsec_info.h. 8199 */ 8200 if ((mp_size == sizeof (ipsec_info_t)) && 8201 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8202 ASSERT(mp->b_cont != NULL); 8203 mp = mp->b_cont; 8204 /* IP should have done this */ 8205 ASSERT(OK_32PTR(mp->b_rptr)); 8206 mp_size = MBLKL(mp); 8207 ipsec_mctl = B_TRUE; 8208 } 8209 8210 /* 8211 * Verify that we have a complete outer IP header. If not, drop it. 8212 */ 8213 if (mp_size < sizeof (ipha_t)) { 8214 noticmpv4: 8215 freemsg(first_mp); 8216 return; 8217 } 8218 8219 ipha = (ipha_t *)mp->b_rptr; 8220 /* 8221 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8222 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8223 */ 8224 switch (IPH_HDR_VERSION(ipha)) { 8225 case IPV6_VERSION: 8226 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8227 return; 8228 case IPV4_VERSION: 8229 break; 8230 default: 8231 goto noticmpv4; 8232 } 8233 8234 /* Skip past the outer IP and ICMP headers */ 8235 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8236 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8237 /* 8238 * If we don't have the correct outer IP header length or if the ULP 8239 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8240 * send it upstream. 8241 */ 8242 if (iph_hdr_length < sizeof (ipha_t) || 8243 ipha->ipha_protocol != IPPROTO_ICMP || 8244 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8245 goto noticmpv4; 8246 } 8247 ipha = (ipha_t *)&icmph[1]; 8248 8249 /* Skip past the inner IP and find the ULP header */ 8250 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8251 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8252 /* 8253 * If we don't have the correct inner IP header length or if the ULP 8254 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8255 * bytes of TCP header, drop it. 8256 */ 8257 if (iph_hdr_length < sizeof (ipha_t) || 8258 ipha->ipha_protocol != IPPROTO_TCP || 8259 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8260 goto noticmpv4; 8261 } 8262 8263 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8264 if (ipsec_mctl) { 8265 secure = ipsec_in_is_secure(first_mp); 8266 } else { 8267 secure = B_FALSE; 8268 } 8269 if (secure) { 8270 /* 8271 * If we are willing to accept this in clear 8272 * we don't have to verify policy. 8273 */ 8274 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8275 if (!tcp_check_policy(tcp, first_mp, 8276 ipha, NULL, secure, ipsec_mctl)) { 8277 /* 8278 * tcp_check_policy called 8279 * ip_drop_packet() on failure. 8280 */ 8281 return; 8282 } 8283 } 8284 } 8285 } else if (ipsec_mctl) { 8286 /* 8287 * This is a hard_bound connection. IP has already 8288 * verified policy. We don't have to do it again. 8289 */ 8290 freeb(first_mp); 8291 first_mp = mp; 8292 ipsec_mctl = B_FALSE; 8293 } 8294 8295 seg_ack = ABE32_TO_U32(tcph->th_ack); 8296 seg_seq = ABE32_TO_U32(tcph->th_seq); 8297 /* 8298 * TCP SHOULD check that the TCP sequence number contained in 8299 * payload of the ICMP error message is within the range 8300 * SND.UNA <= SEG.SEQ < SND.NXT. and also SEG.ACK <= RECV.NXT 8301 */ 8302 if (SEQ_LT(seg_seq, tcp->tcp_suna) || 8303 SEQ_GEQ(seg_seq, tcp->tcp_snxt) || 8304 SEQ_GT(seg_ack, tcp->tcp_rnxt)) { 8305 /* 8306 * If the ICMP message is bogus, should we kill the 8307 * connection, or should we just drop the bogus ICMP 8308 * message? It would probably make more sense to just 8309 * drop the message so that if this one managed to get 8310 * in, the real connection should not suffer. 8311 */ 8312 goto noticmpv4; 8313 } 8314 8315 switch (icmph->icmph_type) { 8316 case ICMP_DEST_UNREACHABLE: 8317 switch (icmph->icmph_code) { 8318 case ICMP_FRAGMENTATION_NEEDED: 8319 /* 8320 * Reduce the MSS based on the new MTU. This will 8321 * eliminate any fragmentation locally. 8322 * N.B. There may well be some funny side-effects on 8323 * the local send policy and the remote receive policy. 8324 * Pending further research, we provide 8325 * tcp_ignore_path_mtu just in case this proves 8326 * disastrous somewhere. 8327 * 8328 * After updating the MSS, retransmit part of the 8329 * dropped segment using the new mss by calling 8330 * tcp_wput_data(). Need to adjust all those 8331 * params to make sure tcp_wput_data() work properly. 8332 */ 8333 if (tcp_ignore_path_mtu) 8334 break; 8335 8336 /* 8337 * Decrease the MSS by time stamp options 8338 * IP options and IPSEC options. tcp_hdr_len 8339 * includes time stamp option and IP option 8340 * length. 8341 */ 8342 8343 new_mss = ntohs(icmph->icmph_du_mtu) - 8344 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8345 8346 /* 8347 * Only update the MSS if the new one is 8348 * smaller than the previous one. This is 8349 * to avoid problems when getting multiple 8350 * ICMP errors for the same MTU. 8351 */ 8352 if (new_mss >= tcp->tcp_mss) 8353 break; 8354 8355 /* 8356 * Stop doing PMTU if new_mss is less than 68 8357 * or less than tcp_mss_min. 8358 * The value 68 comes from rfc 1191. 8359 */ 8360 if (new_mss < MAX(68, tcp_mss_min)) 8361 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8362 0; 8363 8364 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8365 ASSERT(ratio >= 1); 8366 tcp_mss_set(tcp, new_mss); 8367 8368 /* 8369 * Make sure we have something to 8370 * send. 8371 */ 8372 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8373 (tcp->tcp_xmit_head != NULL)) { 8374 /* 8375 * Shrink tcp_cwnd in 8376 * proportion to the old MSS/new MSS. 8377 */ 8378 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8379 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8380 (tcp->tcp_unsent == 0)) { 8381 tcp->tcp_rexmit_max = tcp->tcp_fss; 8382 } else { 8383 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8384 } 8385 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8386 tcp->tcp_rexmit = B_TRUE; 8387 tcp->tcp_dupack_cnt = 0; 8388 tcp->tcp_snd_burst = TCP_CWND_SS; 8389 tcp_ss_rexmit(tcp); 8390 } 8391 break; 8392 case ICMP_PORT_UNREACHABLE: 8393 case ICMP_PROTOCOL_UNREACHABLE: 8394 switch (tcp->tcp_state) { 8395 case TCPS_SYN_SENT: 8396 case TCPS_SYN_RCVD: 8397 /* 8398 * ICMP can snipe away incipient 8399 * TCP connections as long as 8400 * seq number is same as initial 8401 * send seq number. 8402 */ 8403 if (seg_seq == tcp->tcp_iss) { 8404 (void) tcp_clean_death(tcp, 8405 ECONNREFUSED, 6); 8406 } 8407 break; 8408 } 8409 break; 8410 case ICMP_HOST_UNREACHABLE: 8411 case ICMP_NET_UNREACHABLE: 8412 /* Record the error in case we finally time out. */ 8413 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8414 tcp->tcp_client_errno = EHOSTUNREACH; 8415 else 8416 tcp->tcp_client_errno = ENETUNREACH; 8417 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8418 if (tcp->tcp_listener != NULL && 8419 tcp->tcp_listener->tcp_syn_defense) { 8420 /* 8421 * Ditch the half-open connection if we 8422 * suspect a SYN attack is under way. 8423 */ 8424 tcp_ip_ire_mark_advice(tcp); 8425 (void) tcp_clean_death(tcp, 8426 tcp->tcp_client_errno, 7); 8427 } 8428 } 8429 break; 8430 default: 8431 break; 8432 } 8433 break; 8434 case ICMP_SOURCE_QUENCH: { 8435 /* 8436 * use a global boolean to control 8437 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8438 * The default is false. 8439 */ 8440 if (tcp_icmp_source_quench) { 8441 /* 8442 * Reduce the sending rate as if we got a 8443 * retransmit timeout 8444 */ 8445 uint32_t npkt; 8446 8447 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8448 tcp->tcp_mss; 8449 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8450 tcp->tcp_cwnd = tcp->tcp_mss; 8451 tcp->tcp_cwnd_cnt = 0; 8452 } 8453 break; 8454 } 8455 } 8456 freemsg(first_mp); 8457 } 8458 8459 /* 8460 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8461 * error messages passed up by IP. 8462 * Assumes that IP has pulled up all the extension headers as well 8463 * as the ICMPv6 header. 8464 */ 8465 static void 8466 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8467 { 8468 icmp6_t *icmp6; 8469 ip6_t *ip6h; 8470 uint16_t iph_hdr_length; 8471 tcpha_t *tcpha; 8472 uint8_t *nexthdrp; 8473 uint32_t new_mss; 8474 uint32_t ratio; 8475 boolean_t secure; 8476 mblk_t *first_mp = mp; 8477 size_t mp_size; 8478 uint32_t seg_ack; 8479 uint32_t seg_seq; 8480 8481 /* 8482 * The caller has determined if this is an IPSEC_IN packet and 8483 * set ipsec_mctl appropriately (see tcp_icmp_error). 8484 */ 8485 if (ipsec_mctl) 8486 mp = mp->b_cont; 8487 8488 mp_size = MBLKL(mp); 8489 8490 /* 8491 * Verify that we have a complete IP header. If not, send it upstream. 8492 */ 8493 if (mp_size < sizeof (ip6_t)) { 8494 noticmpv6: 8495 freemsg(first_mp); 8496 return; 8497 } 8498 8499 /* 8500 * Verify this is an ICMPV6 packet, else send it upstream. 8501 */ 8502 ip6h = (ip6_t *)mp->b_rptr; 8503 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8504 iph_hdr_length = IPV6_HDR_LEN; 8505 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8506 &nexthdrp) || 8507 *nexthdrp != IPPROTO_ICMPV6) { 8508 goto noticmpv6; 8509 } 8510 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8511 ip6h = (ip6_t *)&icmp6[1]; 8512 /* 8513 * Verify if we have a complete ICMP and inner IP header. 8514 */ 8515 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8516 goto noticmpv6; 8517 8518 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8519 goto noticmpv6; 8520 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8521 /* 8522 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8523 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8524 * packet. 8525 */ 8526 if ((*nexthdrp != IPPROTO_TCP) || 8527 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8528 goto noticmpv6; 8529 } 8530 8531 /* 8532 * ICMP errors come on the right queue or come on 8533 * listener/global queue for detached connections and 8534 * get switched to the right queue. If it comes on the 8535 * right queue, policy check has already been done by IP 8536 * and thus free the first_mp without verifying the policy. 8537 * If it has come for a non-hard bound connection, we need 8538 * to verify policy as IP may not have done it. 8539 */ 8540 if (!tcp->tcp_hard_bound) { 8541 if (ipsec_mctl) { 8542 secure = ipsec_in_is_secure(first_mp); 8543 } else { 8544 secure = B_FALSE; 8545 } 8546 if (secure) { 8547 /* 8548 * If we are willing to accept this in clear 8549 * we don't have to verify policy. 8550 */ 8551 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8552 if (!tcp_check_policy(tcp, first_mp, 8553 NULL, ip6h, secure, ipsec_mctl)) { 8554 /* 8555 * tcp_check_policy called 8556 * ip_drop_packet() on failure. 8557 */ 8558 return; 8559 } 8560 } 8561 } 8562 } else if (ipsec_mctl) { 8563 /* 8564 * This is a hard_bound connection. IP has already 8565 * verified policy. We don't have to do it again. 8566 */ 8567 freeb(first_mp); 8568 first_mp = mp; 8569 ipsec_mctl = B_FALSE; 8570 } 8571 8572 seg_ack = ntohl(tcpha->tha_ack); 8573 seg_seq = ntohl(tcpha->tha_seq); 8574 /* 8575 * TCP SHOULD check that the TCP sequence number contained in 8576 * payload of the ICMP error message is within the range 8577 * SND.UNA <= SEG.SEQ < SND.NXT. and also SEG.ACK <= RECV.NXT 8578 */ 8579 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt) || 8580 SEQ_GT(seg_ack, tcp->tcp_rnxt)) { 8581 /* 8582 * If the ICMP message is bogus, should we kill the 8583 * connection, or should we just drop the bogus ICMP 8584 * message? It would probably make more sense to just 8585 * drop the message so that if this one managed to get 8586 * in, the real connection should not suffer. 8587 */ 8588 goto noticmpv6; 8589 } 8590 8591 switch (icmp6->icmp6_type) { 8592 case ICMP6_PACKET_TOO_BIG: 8593 /* 8594 * Reduce the MSS based on the new MTU. This will 8595 * eliminate any fragmentation locally. 8596 * N.B. There may well be some funny side-effects on 8597 * the local send policy and the remote receive policy. 8598 * Pending further research, we provide 8599 * tcp_ignore_path_mtu just in case this proves 8600 * disastrous somewhere. 8601 * 8602 * After updating the MSS, retransmit part of the 8603 * dropped segment using the new mss by calling 8604 * tcp_wput_data(). Need to adjust all those 8605 * params to make sure tcp_wput_data() work properly. 8606 */ 8607 if (tcp_ignore_path_mtu) 8608 break; 8609 8610 /* 8611 * Decrease the MSS by time stamp options 8612 * IP options and IPSEC options. tcp_hdr_len 8613 * includes time stamp option and IP option 8614 * length. 8615 */ 8616 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8617 tcp->tcp_ipsec_overhead; 8618 8619 /* 8620 * Only update the MSS if the new one is 8621 * smaller than the previous one. This is 8622 * to avoid problems when getting multiple 8623 * ICMP errors for the same MTU. 8624 */ 8625 if (new_mss >= tcp->tcp_mss) 8626 break; 8627 8628 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8629 ASSERT(ratio >= 1); 8630 tcp_mss_set(tcp, new_mss); 8631 8632 /* 8633 * Make sure we have something to 8634 * send. 8635 */ 8636 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8637 (tcp->tcp_xmit_head != NULL)) { 8638 /* 8639 * Shrink tcp_cwnd in 8640 * proportion to the old MSS/new MSS. 8641 */ 8642 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8643 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8644 (tcp->tcp_unsent == 0)) { 8645 tcp->tcp_rexmit_max = tcp->tcp_fss; 8646 } else { 8647 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8648 } 8649 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8650 tcp->tcp_rexmit = B_TRUE; 8651 tcp->tcp_dupack_cnt = 0; 8652 tcp->tcp_snd_burst = TCP_CWND_SS; 8653 tcp_ss_rexmit(tcp); 8654 } 8655 break; 8656 8657 case ICMP6_DST_UNREACH: 8658 switch (icmp6->icmp6_code) { 8659 case ICMP6_DST_UNREACH_NOPORT: 8660 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8661 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8662 (seg_seq == tcp->tcp_iss)) { 8663 (void) tcp_clean_death(tcp, 8664 ECONNREFUSED, 8); 8665 } 8666 break; 8667 8668 case ICMP6_DST_UNREACH_ADMIN: 8669 case ICMP6_DST_UNREACH_NOROUTE: 8670 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8671 case ICMP6_DST_UNREACH_ADDR: 8672 /* Record the error in case we finally time out. */ 8673 tcp->tcp_client_errno = EHOSTUNREACH; 8674 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8675 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8676 (seg_seq == tcp->tcp_iss)) { 8677 if (tcp->tcp_listener != NULL && 8678 tcp->tcp_listener->tcp_syn_defense) { 8679 /* 8680 * Ditch the half-open connection if we 8681 * suspect a SYN attack is under way. 8682 */ 8683 tcp_ip_ire_mark_advice(tcp); 8684 (void) tcp_clean_death(tcp, 8685 tcp->tcp_client_errno, 9); 8686 } 8687 } 8688 8689 8690 break; 8691 default: 8692 break; 8693 } 8694 break; 8695 8696 case ICMP6_PARAM_PROB: 8697 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8698 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8699 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8700 (uchar_t *)nexthdrp) { 8701 if (tcp->tcp_state == TCPS_SYN_SENT || 8702 tcp->tcp_state == TCPS_SYN_RCVD) { 8703 (void) tcp_clean_death(tcp, 8704 ECONNREFUSED, 10); 8705 } 8706 break; 8707 } 8708 break; 8709 8710 case ICMP6_TIME_EXCEEDED: 8711 default: 8712 break; 8713 } 8714 freemsg(first_mp); 8715 } 8716 8717 /* 8718 * IP recognizes seven kinds of bind requests: 8719 * 8720 * - A zero-length address binds only to the protocol number. 8721 * 8722 * - A 4-byte address is treated as a request to 8723 * validate that the address is a valid local IPv4 8724 * address, appropriate for an application to bind to. 8725 * IP does the verification, but does not make any note 8726 * of the address at this time. 8727 * 8728 * - A 16-byte address contains is treated as a request 8729 * to validate a local IPv6 address, as the 4-byte 8730 * address case above. 8731 * 8732 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 8733 * use it for the inbound fanout of packets. 8734 * 8735 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 8736 * use it for the inbound fanout of packets. 8737 * 8738 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 8739 * information consisting of local and remote addresses 8740 * and ports. In this case, the addresses are both 8741 * validated as appropriate for this operation, and, if 8742 * so, the information is retained for use in the 8743 * inbound fanout. 8744 * 8745 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 8746 * fanout information, like the 12-byte case above. 8747 * 8748 * IP will also fill in the IRE request mblk with information 8749 * regarding our peer. In all cases, we notify IP of our protocol 8750 * type by appending a single protocol byte to the bind request. 8751 */ 8752 static mblk_t * 8753 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 8754 { 8755 char *cp; 8756 mblk_t *mp; 8757 struct T_bind_req *tbr; 8758 ipa_conn_t *ac; 8759 ipa6_conn_t *ac6; 8760 sin_t *sin; 8761 sin6_t *sin6; 8762 8763 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 8764 ASSERT((tcp->tcp_family == AF_INET && 8765 tcp->tcp_ipversion == IPV4_VERSION) || 8766 (tcp->tcp_family == AF_INET6 && 8767 (tcp->tcp_ipversion == IPV4_VERSION || 8768 tcp->tcp_ipversion == IPV6_VERSION))); 8769 8770 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 8771 if (!mp) 8772 return (mp); 8773 mp->b_datap->db_type = M_PROTO; 8774 tbr = (struct T_bind_req *)mp->b_rptr; 8775 tbr->PRIM_type = bind_prim; 8776 tbr->ADDR_offset = sizeof (*tbr); 8777 tbr->CONIND_number = 0; 8778 tbr->ADDR_length = addr_length; 8779 cp = (char *)&tbr[1]; 8780 switch (addr_length) { 8781 case sizeof (ipa_conn_t): 8782 ASSERT(tcp->tcp_family == AF_INET); 8783 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 8784 8785 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 8786 if (mp->b_cont == NULL) { 8787 freemsg(mp); 8788 return (NULL); 8789 } 8790 mp->b_cont->b_wptr += sizeof (ire_t); 8791 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 8792 8793 /* cp known to be 32 bit aligned */ 8794 ac = (ipa_conn_t *)cp; 8795 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 8796 ac->ac_faddr = tcp->tcp_remote; 8797 ac->ac_fport = tcp->tcp_fport; 8798 ac->ac_lport = tcp->tcp_lport; 8799 tcp->tcp_hard_binding = 1; 8800 break; 8801 8802 case sizeof (ipa6_conn_t): 8803 ASSERT(tcp->tcp_family == AF_INET6); 8804 8805 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 8806 if (mp->b_cont == NULL) { 8807 freemsg(mp); 8808 return (NULL); 8809 } 8810 mp->b_cont->b_wptr += sizeof (ire_t); 8811 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 8812 8813 /* cp known to be 32 bit aligned */ 8814 ac6 = (ipa6_conn_t *)cp; 8815 if (tcp->tcp_ipversion == IPV4_VERSION) { 8816 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 8817 &ac6->ac6_laddr); 8818 } else { 8819 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 8820 } 8821 ac6->ac6_faddr = tcp->tcp_remote_v6; 8822 ac6->ac6_fport = tcp->tcp_fport; 8823 ac6->ac6_lport = tcp->tcp_lport; 8824 tcp->tcp_hard_binding = 1; 8825 break; 8826 8827 case sizeof (sin_t): 8828 /* 8829 * NOTE: IPV6_ADDR_LEN also has same size. 8830 * Use family to discriminate. 8831 */ 8832 if (tcp->tcp_family == AF_INET) { 8833 sin = (sin_t *)cp; 8834 8835 *sin = sin_null; 8836 sin->sin_family = AF_INET; 8837 sin->sin_addr.s_addr = tcp->tcp_bound_source; 8838 sin->sin_port = tcp->tcp_lport; 8839 break; 8840 } else { 8841 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 8842 } 8843 break; 8844 8845 case sizeof (sin6_t): 8846 ASSERT(tcp->tcp_family == AF_INET6); 8847 sin6 = (sin6_t *)cp; 8848 8849 *sin6 = sin6_null; 8850 sin6->sin6_family = AF_INET6; 8851 sin6->sin6_addr = tcp->tcp_bound_source_v6; 8852 sin6->sin6_port = tcp->tcp_lport; 8853 break; 8854 8855 case IP_ADDR_LEN: 8856 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 8857 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 8858 break; 8859 8860 } 8861 /* Add protocol number to end */ 8862 cp[addr_length] = (char)IPPROTO_TCP; 8863 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 8864 return (mp); 8865 } 8866 8867 /* 8868 * Notify IP that we are having trouble with this connection. IP should 8869 * blow the IRE away and start over. 8870 */ 8871 static void 8872 tcp_ip_notify(tcp_t *tcp) 8873 { 8874 struct iocblk *iocp; 8875 ipid_t *ipid; 8876 mblk_t *mp; 8877 8878 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8879 if (tcp->tcp_ipversion == IPV6_VERSION) 8880 return; 8881 8882 mp = mkiocb(IP_IOCTL); 8883 if (mp == NULL) 8884 return; 8885 8886 iocp = (struct iocblk *)mp->b_rptr; 8887 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8888 8889 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8890 if (!mp->b_cont) { 8891 freeb(mp); 8892 return; 8893 } 8894 8895 ipid = (ipid_t *)mp->b_cont->b_rptr; 8896 mp->b_cont->b_wptr += iocp->ioc_count; 8897 bzero(ipid, sizeof (*ipid)); 8898 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8899 ipid->ipid_ire_type = IRE_CACHE; 8900 ipid->ipid_addr_offset = sizeof (ipid_t); 8901 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8902 /* 8903 * Note: in the case of source routing we want to blow away the 8904 * route to the first source route hop. 8905 */ 8906 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8907 sizeof (tcp->tcp_ipha->ipha_dst)); 8908 8909 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8910 } 8911 8912 /* Unlink and return any mblk that looks like it contains an ire */ 8913 static mblk_t * 8914 tcp_ire_mp(mblk_t *mp) 8915 { 8916 mblk_t *prev_mp; 8917 8918 for (;;) { 8919 prev_mp = mp; 8920 mp = mp->b_cont; 8921 if (mp == NULL) 8922 break; 8923 switch (DB_TYPE(mp)) { 8924 case IRE_DB_TYPE: 8925 case IRE_DB_REQ_TYPE: 8926 if (prev_mp != NULL) 8927 prev_mp->b_cont = mp->b_cont; 8928 mp->b_cont = NULL; 8929 return (mp); 8930 default: 8931 break; 8932 } 8933 } 8934 return (mp); 8935 } 8936 8937 /* 8938 * Timer callback routine for keepalive probe. We do a fake resend of 8939 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8940 * check to see if we have heard anything from the other end for the last 8941 * RTO period. If we have, set the timer to expire for another 8942 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8943 * RTO << 1 and check again when it expires. Keep exponentially increasing 8944 * the timeout if we have not heard from the other side. If for more than 8945 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8946 * kill the connection unless the keepalive abort threshold is 0. In 8947 * that case, we will probe "forever." 8948 */ 8949 static void 8950 tcp_keepalive_killer(void *arg) 8951 { 8952 mblk_t *mp; 8953 conn_t *connp = (conn_t *)arg; 8954 tcp_t *tcp = connp->conn_tcp; 8955 int32_t firetime; 8956 int32_t idletime; 8957 int32_t ka_intrvl; 8958 8959 tcp->tcp_ka_tid = 0; 8960 8961 if (tcp->tcp_fused) 8962 return; 8963 8964 BUMP_MIB(&tcp_mib, tcpTimKeepalive); 8965 ka_intrvl = tcp->tcp_ka_interval; 8966 8967 /* 8968 * Keepalive probe should only be sent if the application has not 8969 * done a close on the connection. 8970 */ 8971 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8972 return; 8973 } 8974 /* Timer fired too early, restart it. */ 8975 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8976 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8977 MSEC_TO_TICK(ka_intrvl)); 8978 return; 8979 } 8980 8981 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8982 /* 8983 * If we have not heard from the other side for a long 8984 * time, kill the connection unless the keepalive abort 8985 * threshold is 0. In that case, we will probe "forever." 8986 */ 8987 if (tcp->tcp_ka_abort_thres != 0 && 8988 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8989 BUMP_MIB(&tcp_mib, tcpTimKeepaliveDrop); 8990 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8991 tcp->tcp_client_errno : ETIMEDOUT, 11); 8992 return; 8993 } 8994 8995 if (tcp->tcp_snxt == tcp->tcp_suna && 8996 idletime >= ka_intrvl) { 8997 /* Fake resend of last ACKed byte. */ 8998 mblk_t *mp1 = allocb(1, BPRI_LO); 8999 9000 if (mp1 != NULL) { 9001 *mp1->b_wptr++ = '\0'; 9002 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9003 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9004 freeb(mp1); 9005 /* 9006 * if allocation failed, fall through to start the 9007 * timer back. 9008 */ 9009 if (mp != NULL) { 9010 TCP_RECORD_TRACE(tcp, mp, 9011 TCP_TRACE_SEND_PKT); 9012 tcp_send_data(tcp, tcp->tcp_wq, mp); 9013 BUMP_MIB(&tcp_mib, tcpTimKeepaliveProbe); 9014 if (tcp->tcp_ka_last_intrvl != 0) { 9015 /* 9016 * We should probe again at least 9017 * in ka_intrvl, but not more than 9018 * tcp_rexmit_interval_max. 9019 */ 9020 firetime = MIN(ka_intrvl - 1, 9021 tcp->tcp_ka_last_intrvl << 1); 9022 if (firetime > tcp_rexmit_interval_max) 9023 firetime = 9024 tcp_rexmit_interval_max; 9025 } else { 9026 firetime = tcp->tcp_rto; 9027 } 9028 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9029 tcp_keepalive_killer, 9030 MSEC_TO_TICK(firetime)); 9031 tcp->tcp_ka_last_intrvl = firetime; 9032 return; 9033 } 9034 } 9035 } else { 9036 tcp->tcp_ka_last_intrvl = 0; 9037 } 9038 9039 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9040 if ((firetime = ka_intrvl - idletime) < 0) { 9041 firetime = ka_intrvl; 9042 } 9043 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9044 MSEC_TO_TICK(firetime)); 9045 } 9046 9047 int 9048 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9049 { 9050 queue_t *q = tcp->tcp_rq; 9051 int32_t mss = tcp->tcp_mss; 9052 int maxpsz; 9053 9054 if (TCP_IS_DETACHED(tcp)) 9055 return (mss); 9056 9057 if (tcp->tcp_fused) { 9058 maxpsz = tcp_fuse_maxpsz_set(tcp); 9059 mss = INFPSZ; 9060 } else if (tcp->tcp_mdt || tcp->tcp_maxpsz == 0) { 9061 /* 9062 * Set the sd_qn_maxpsz according to the socket send buffer 9063 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9064 * instruct the stream head to copyin user data into contiguous 9065 * kernel-allocated buffers without breaking it up into smaller 9066 * chunks. We round up the buffer size to the nearest SMSS. 9067 */ 9068 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9069 if (tcp->tcp_kssl_ctx == NULL) 9070 mss = INFPSZ; 9071 else 9072 mss = SSL3_MAX_RECORD_LEN; 9073 } else { 9074 /* 9075 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9076 * (and a multiple of the mss). This instructs the stream 9077 * head to break down larger than SMSS writes into SMSS- 9078 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9079 */ 9080 maxpsz = tcp->tcp_maxpsz * mss; 9081 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9082 maxpsz = tcp->tcp_xmit_hiwater/2; 9083 /* Round up to nearest mss */ 9084 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9085 } 9086 } 9087 (void) setmaxps(q, maxpsz); 9088 tcp->tcp_wq->q_maxpsz = maxpsz; 9089 9090 if (set_maxblk) 9091 (void) mi_set_sth_maxblk(q, mss); 9092 9093 return (mss); 9094 } 9095 9096 /* 9097 * Extract option values from a tcp header. We put any found values into the 9098 * tcpopt struct and return a bitmask saying which options were found. 9099 */ 9100 static int 9101 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9102 { 9103 uchar_t *endp; 9104 int len; 9105 uint32_t mss; 9106 uchar_t *up = (uchar_t *)tcph; 9107 int found = 0; 9108 int32_t sack_len; 9109 tcp_seq sack_begin, sack_end; 9110 tcp_t *tcp; 9111 9112 endp = up + TCP_HDR_LENGTH(tcph); 9113 up += TCP_MIN_HEADER_LENGTH; 9114 while (up < endp) { 9115 len = endp - up; 9116 switch (*up) { 9117 case TCPOPT_EOL: 9118 break; 9119 9120 case TCPOPT_NOP: 9121 up++; 9122 continue; 9123 9124 case TCPOPT_MAXSEG: 9125 if (len < TCPOPT_MAXSEG_LEN || 9126 up[1] != TCPOPT_MAXSEG_LEN) 9127 break; 9128 9129 mss = BE16_TO_U16(up+2); 9130 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9131 tcpopt->tcp_opt_mss = mss; 9132 found |= TCP_OPT_MSS_PRESENT; 9133 9134 up += TCPOPT_MAXSEG_LEN; 9135 continue; 9136 9137 case TCPOPT_WSCALE: 9138 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9139 break; 9140 9141 if (up[2] > TCP_MAX_WINSHIFT) 9142 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9143 else 9144 tcpopt->tcp_opt_wscale = up[2]; 9145 found |= TCP_OPT_WSCALE_PRESENT; 9146 9147 up += TCPOPT_WS_LEN; 9148 continue; 9149 9150 case TCPOPT_SACK_PERMITTED: 9151 if (len < TCPOPT_SACK_OK_LEN || 9152 up[1] != TCPOPT_SACK_OK_LEN) 9153 break; 9154 found |= TCP_OPT_SACK_OK_PRESENT; 9155 up += TCPOPT_SACK_OK_LEN; 9156 continue; 9157 9158 case TCPOPT_SACK: 9159 if (len <= 2 || up[1] <= 2 || len < up[1]) 9160 break; 9161 9162 /* If TCP is not interested in SACK blks... */ 9163 if ((tcp = tcpopt->tcp) == NULL) { 9164 up += up[1]; 9165 continue; 9166 } 9167 sack_len = up[1] - TCPOPT_HEADER_LEN; 9168 up += TCPOPT_HEADER_LEN; 9169 9170 /* 9171 * If the list is empty, allocate one and assume 9172 * nothing is sack'ed. 9173 */ 9174 ASSERT(tcp->tcp_sack_info != NULL); 9175 if (tcp->tcp_notsack_list == NULL) { 9176 tcp_notsack_update(&(tcp->tcp_notsack_list), 9177 tcp->tcp_suna, tcp->tcp_snxt, 9178 &(tcp->tcp_num_notsack_blk), 9179 &(tcp->tcp_cnt_notsack_list)); 9180 9181 /* 9182 * Make sure tcp_notsack_list is not NULL. 9183 * This happens when kmem_alloc(KM_NOSLEEP) 9184 * returns NULL. 9185 */ 9186 if (tcp->tcp_notsack_list == NULL) { 9187 up += sack_len; 9188 continue; 9189 } 9190 tcp->tcp_fack = tcp->tcp_suna; 9191 } 9192 9193 while (sack_len > 0) { 9194 if (up + 8 > endp) { 9195 up = endp; 9196 break; 9197 } 9198 sack_begin = BE32_TO_U32(up); 9199 up += 4; 9200 sack_end = BE32_TO_U32(up); 9201 up += 4; 9202 sack_len -= 8; 9203 /* 9204 * Bounds checking. Make sure the SACK 9205 * info is within tcp_suna and tcp_snxt. 9206 * If this SACK blk is out of bound, ignore 9207 * it but continue to parse the following 9208 * blks. 9209 */ 9210 if (SEQ_LEQ(sack_end, sack_begin) || 9211 SEQ_LT(sack_begin, tcp->tcp_suna) || 9212 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9213 continue; 9214 } 9215 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9216 sack_begin, sack_end, 9217 &(tcp->tcp_num_notsack_blk), 9218 &(tcp->tcp_cnt_notsack_list)); 9219 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9220 tcp->tcp_fack = sack_end; 9221 } 9222 } 9223 found |= TCP_OPT_SACK_PRESENT; 9224 continue; 9225 9226 case TCPOPT_TSTAMP: 9227 if (len < TCPOPT_TSTAMP_LEN || 9228 up[1] != TCPOPT_TSTAMP_LEN) 9229 break; 9230 9231 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9232 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9233 9234 found |= TCP_OPT_TSTAMP_PRESENT; 9235 9236 up += TCPOPT_TSTAMP_LEN; 9237 continue; 9238 9239 default: 9240 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9241 break; 9242 up += up[1]; 9243 continue; 9244 } 9245 break; 9246 } 9247 return (found); 9248 } 9249 9250 /* 9251 * Set the mss associated with a particular tcp based on its current value, 9252 * and a new one passed in. Observe minimums and maximums, and reset 9253 * other state variables that we want to view as multiples of mss. 9254 * 9255 * This function is called in various places mainly because 9256 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9257 * other side's SYN/SYN-ACK packet arrives. 9258 * 2) PMTUd may get us a new MSS. 9259 * 3) If the other side stops sending us timestamp option, we need to 9260 * increase the MSS size to use the extra bytes available. 9261 */ 9262 static void 9263 tcp_mss_set(tcp_t *tcp, uint32_t mss) 9264 { 9265 uint32_t mss_max; 9266 9267 if (tcp->tcp_ipversion == IPV4_VERSION) 9268 mss_max = tcp_mss_max_ipv4; 9269 else 9270 mss_max = tcp_mss_max_ipv6; 9271 9272 if (mss < tcp_mss_min) 9273 mss = tcp_mss_min; 9274 if (mss > mss_max) 9275 mss = mss_max; 9276 /* 9277 * Unless naglim has been set by our client to 9278 * a non-mss value, force naglim to track mss. 9279 * This can help to aggregate small writes. 9280 */ 9281 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9282 tcp->tcp_naglim = mss; 9283 /* 9284 * TCP should be able to buffer at least 4 MSS data for obvious 9285 * performance reason. 9286 */ 9287 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9288 tcp->tcp_xmit_hiwater = mss << 2; 9289 9290 /* 9291 * Check if we need to apply the tcp_init_cwnd here. If 9292 * it is set and the MSS gets bigger (should not happen 9293 * normally), we need to adjust the resulting tcp_cwnd properly. 9294 * The new tcp_cwnd should not get bigger. 9295 */ 9296 if (tcp->tcp_init_cwnd == 0) { 9297 tcp->tcp_cwnd = MIN(tcp_slow_start_initial * mss, 9298 MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9299 } else { 9300 if (tcp->tcp_mss < mss) { 9301 tcp->tcp_cwnd = MAX(1, 9302 (tcp->tcp_init_cwnd * tcp->tcp_mss / mss)) * mss; 9303 } else { 9304 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9305 } 9306 } 9307 tcp->tcp_mss = mss; 9308 tcp->tcp_cwnd_cnt = 0; 9309 (void) tcp_maxpsz_set(tcp, B_TRUE); 9310 } 9311 9312 static int 9313 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9314 { 9315 tcp_t *tcp = NULL; 9316 conn_t *connp; 9317 int err; 9318 dev_t conn_dev; 9319 zoneid_t zoneid = getzoneid(); 9320 9321 /* 9322 * Special case for install: miniroot needs to be able to access files 9323 * via NFS as though it were always in the global zone. 9324 */ 9325 if (credp == kcred && nfs_global_client_only != 0) 9326 zoneid = GLOBAL_ZONEID; 9327 9328 if (q->q_ptr != NULL) 9329 return (0); 9330 9331 if (sflag == MODOPEN) { 9332 /* 9333 * This is a special case. The purpose of a modopen 9334 * is to allow just the T_SVR4_OPTMGMT_REQ to pass 9335 * through for MIB browsers. Everything else is failed. 9336 */ 9337 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt)); 9338 9339 if (connp == NULL) 9340 return (ENOMEM); 9341 9342 connp->conn_flags |= IPCL_TCPMOD; 9343 connp->conn_cred = credp; 9344 connp->conn_zoneid = zoneid; 9345 q->q_ptr = WR(q)->q_ptr = connp; 9346 crhold(credp); 9347 q->q_qinfo = &tcp_mod_rinit; 9348 WR(q)->q_qinfo = &tcp_mod_winit; 9349 qprocson(q); 9350 return (0); 9351 } 9352 9353 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) 9354 return (EBUSY); 9355 9356 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9357 9358 if (flag & SO_ACCEPTOR) { 9359 q->q_qinfo = &tcp_acceptor_rinit; 9360 q->q_ptr = (void *)conn_dev; 9361 WR(q)->q_qinfo = &tcp_acceptor_winit; 9362 WR(q)->q_ptr = (void *)conn_dev; 9363 qprocson(q); 9364 return (0); 9365 } 9366 9367 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt)); 9368 if (connp == NULL) { 9369 inet_minor_free(ip_minor_arena, conn_dev); 9370 q->q_ptr = NULL; 9371 return (ENOSR); 9372 } 9373 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9374 tcp = connp->conn_tcp; 9375 9376 q->q_ptr = WR(q)->q_ptr = connp; 9377 if (getmajor(*devp) == TCP6_MAJ) { 9378 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9379 connp->conn_send = ip_output_v6; 9380 connp->conn_af_isv6 = B_TRUE; 9381 connp->conn_pkt_isv6 = B_TRUE; 9382 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9383 tcp->tcp_ipversion = IPV6_VERSION; 9384 tcp->tcp_family = AF_INET6; 9385 tcp->tcp_mss = tcp_mss_def_ipv6; 9386 } else { 9387 connp->conn_flags |= IPCL_TCP4; 9388 connp->conn_send = ip_output; 9389 connp->conn_af_isv6 = B_FALSE; 9390 connp->conn_pkt_isv6 = B_FALSE; 9391 tcp->tcp_ipversion = IPV4_VERSION; 9392 tcp->tcp_family = AF_INET; 9393 tcp->tcp_mss = tcp_mss_def_ipv4; 9394 } 9395 9396 /* 9397 * TCP keeps a copy of cred for cache locality reasons but 9398 * we put a reference only once. If connp->conn_cred 9399 * becomes invalid, tcp_cred should also be set to NULL. 9400 */ 9401 tcp->tcp_cred = connp->conn_cred = credp; 9402 crhold(connp->conn_cred); 9403 tcp->tcp_cpid = curproc->p_pid; 9404 connp->conn_zoneid = zoneid; 9405 connp->conn_mlp_type = mlptSingle; 9406 connp->conn_ulp_labeled = !is_system_labeled(); 9407 9408 /* 9409 * If the caller has the process-wide flag set, then default to MAC 9410 * exempt mode. This allows read-down to unlabeled hosts. 9411 */ 9412 if (getpflags(NET_MAC_AWARE, credp) != 0) 9413 connp->conn_mac_exempt = B_TRUE; 9414 9415 connp->conn_dev = conn_dev; 9416 9417 ASSERT(q->q_qinfo == &tcp_rinit); 9418 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9419 9420 if (flag & SO_SOCKSTR) { 9421 /* 9422 * No need to insert a socket in tcp acceptor hash. 9423 * If it was a socket acceptor stream, we dealt with 9424 * it above. A socket listener can never accept a 9425 * connection and doesn't need acceptor_id. 9426 */ 9427 connp->conn_flags |= IPCL_SOCKET; 9428 tcp->tcp_issocket = 1; 9429 WR(q)->q_qinfo = &tcp_sock_winit; 9430 } else { 9431 #ifdef _ILP32 9432 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9433 #else 9434 tcp->tcp_acceptor_id = conn_dev; 9435 #endif /* _ILP32 */ 9436 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9437 } 9438 9439 if (tcp_trace) 9440 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9441 9442 err = tcp_init(tcp, q); 9443 if (err != 0) { 9444 inet_minor_free(ip_minor_arena, connp->conn_dev); 9445 tcp_acceptor_hash_remove(tcp); 9446 CONN_DEC_REF(connp); 9447 q->q_ptr = WR(q)->q_ptr = NULL; 9448 return (err); 9449 } 9450 9451 RD(q)->q_hiwat = tcp_recv_hiwat; 9452 tcp->tcp_rwnd = tcp_recv_hiwat; 9453 9454 /* Non-zero default values */ 9455 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9456 /* 9457 * Put the ref for TCP. Ref for IP was already put 9458 * by ipcl_conn_create. Also Make the conn_t globally 9459 * visible to walkers 9460 */ 9461 mutex_enter(&connp->conn_lock); 9462 CONN_INC_REF_LOCKED(connp); 9463 ASSERT(connp->conn_ref == 2); 9464 connp->conn_state_flags &= ~CONN_INCIPIENT; 9465 mutex_exit(&connp->conn_lock); 9466 9467 qprocson(q); 9468 return (0); 9469 } 9470 9471 /* 9472 * Some TCP options can be "set" by requesting them in the option 9473 * buffer. This is needed for XTI feature test though we do not 9474 * allow it in general. We interpret that this mechanism is more 9475 * applicable to OSI protocols and need not be allowed in general. 9476 * This routine filters out options for which it is not allowed (most) 9477 * and lets through those (few) for which it is. [ The XTI interface 9478 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9479 * ever implemented will have to be allowed here ]. 9480 */ 9481 static boolean_t 9482 tcp_allow_connopt_set(int level, int name) 9483 { 9484 9485 switch (level) { 9486 case IPPROTO_TCP: 9487 switch (name) { 9488 case TCP_NODELAY: 9489 return (B_TRUE); 9490 default: 9491 return (B_FALSE); 9492 } 9493 /*NOTREACHED*/ 9494 default: 9495 return (B_FALSE); 9496 } 9497 /*NOTREACHED*/ 9498 } 9499 9500 /* 9501 * This routine gets default values of certain options whose default 9502 * values are maintained by protocol specific code 9503 */ 9504 /* ARGSUSED */ 9505 int 9506 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9507 { 9508 int32_t *i1 = (int32_t *)ptr; 9509 9510 switch (level) { 9511 case IPPROTO_TCP: 9512 switch (name) { 9513 case TCP_NOTIFY_THRESHOLD: 9514 *i1 = tcp_ip_notify_interval; 9515 break; 9516 case TCP_ABORT_THRESHOLD: 9517 *i1 = tcp_ip_abort_interval; 9518 break; 9519 case TCP_CONN_NOTIFY_THRESHOLD: 9520 *i1 = tcp_ip_notify_cinterval; 9521 break; 9522 case TCP_CONN_ABORT_THRESHOLD: 9523 *i1 = tcp_ip_abort_cinterval; 9524 break; 9525 default: 9526 return (-1); 9527 } 9528 break; 9529 case IPPROTO_IP: 9530 switch (name) { 9531 case IP_TTL: 9532 *i1 = tcp_ipv4_ttl; 9533 break; 9534 default: 9535 return (-1); 9536 } 9537 break; 9538 case IPPROTO_IPV6: 9539 switch (name) { 9540 case IPV6_UNICAST_HOPS: 9541 *i1 = tcp_ipv6_hoplimit; 9542 break; 9543 default: 9544 return (-1); 9545 } 9546 break; 9547 default: 9548 return (-1); 9549 } 9550 return (sizeof (int)); 9551 } 9552 9553 9554 /* 9555 * TCP routine to get the values of options. 9556 */ 9557 int 9558 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9559 { 9560 int *i1 = (int *)ptr; 9561 conn_t *connp = Q_TO_CONN(q); 9562 tcp_t *tcp = connp->conn_tcp; 9563 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9564 9565 switch (level) { 9566 case SOL_SOCKET: 9567 switch (name) { 9568 case SO_LINGER: { 9569 struct linger *lgr = (struct linger *)ptr; 9570 9571 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9572 lgr->l_linger = tcp->tcp_lingertime; 9573 } 9574 return (sizeof (struct linger)); 9575 case SO_DEBUG: 9576 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9577 break; 9578 case SO_KEEPALIVE: 9579 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9580 break; 9581 case SO_DONTROUTE: 9582 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9583 break; 9584 case SO_USELOOPBACK: 9585 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9586 break; 9587 case SO_BROADCAST: 9588 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9589 break; 9590 case SO_REUSEADDR: 9591 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9592 break; 9593 case SO_OOBINLINE: 9594 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9595 break; 9596 case SO_DGRAM_ERRIND: 9597 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9598 break; 9599 case SO_TYPE: 9600 *i1 = SOCK_STREAM; 9601 break; 9602 case SO_SNDBUF: 9603 *i1 = tcp->tcp_xmit_hiwater; 9604 break; 9605 case SO_RCVBUF: 9606 *i1 = RD(q)->q_hiwat; 9607 break; 9608 case SO_SND_COPYAVOID: 9609 *i1 = tcp->tcp_snd_zcopy_on ? 9610 SO_SND_COPYAVOID : 0; 9611 break; 9612 case SO_ALLZONES: 9613 *i1 = connp->conn_allzones ? 1 : 0; 9614 break; 9615 case SO_ANON_MLP: 9616 *i1 = connp->conn_anon_mlp; 9617 break; 9618 case SO_MAC_EXEMPT: 9619 *i1 = connp->conn_mac_exempt; 9620 break; 9621 case SO_EXCLBIND: 9622 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9623 break; 9624 default: 9625 return (-1); 9626 } 9627 break; 9628 case IPPROTO_TCP: 9629 switch (name) { 9630 case TCP_NODELAY: 9631 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9632 break; 9633 case TCP_MAXSEG: 9634 *i1 = tcp->tcp_mss; 9635 break; 9636 case TCP_NOTIFY_THRESHOLD: 9637 *i1 = (int)tcp->tcp_first_timer_threshold; 9638 break; 9639 case TCP_ABORT_THRESHOLD: 9640 *i1 = tcp->tcp_second_timer_threshold; 9641 break; 9642 case TCP_CONN_NOTIFY_THRESHOLD: 9643 *i1 = tcp->tcp_first_ctimer_threshold; 9644 break; 9645 case TCP_CONN_ABORT_THRESHOLD: 9646 *i1 = tcp->tcp_second_ctimer_threshold; 9647 break; 9648 case TCP_RECVDSTADDR: 9649 *i1 = tcp->tcp_recvdstaddr; 9650 break; 9651 case TCP_ANONPRIVBIND: 9652 *i1 = tcp->tcp_anon_priv_bind; 9653 break; 9654 case TCP_EXCLBIND: 9655 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9656 break; 9657 case TCP_INIT_CWND: 9658 *i1 = tcp->tcp_init_cwnd; 9659 break; 9660 case TCP_KEEPALIVE_THRESHOLD: 9661 *i1 = tcp->tcp_ka_interval; 9662 break; 9663 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9664 *i1 = tcp->tcp_ka_abort_thres; 9665 break; 9666 case TCP_CORK: 9667 *i1 = tcp->tcp_cork; 9668 break; 9669 default: 9670 return (-1); 9671 } 9672 break; 9673 case IPPROTO_IP: 9674 if (tcp->tcp_family != AF_INET) 9675 return (-1); 9676 switch (name) { 9677 case IP_OPTIONS: 9678 case T_IP_OPTIONS: { 9679 /* 9680 * This is compatible with BSD in that in only return 9681 * the reverse source route with the final destination 9682 * as the last entry. The first 4 bytes of the option 9683 * will contain the final destination. 9684 */ 9685 int opt_len; 9686 9687 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9688 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9689 ASSERT(opt_len >= 0); 9690 /* Caller ensures enough space */ 9691 if (opt_len > 0) { 9692 /* 9693 * TODO: Do we have to handle getsockopt on an 9694 * initiator as well? 9695 */ 9696 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9697 } 9698 return (0); 9699 } 9700 case IP_TOS: 9701 case T_IP_TOS: 9702 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9703 break; 9704 case IP_TTL: 9705 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9706 break; 9707 case IP_NEXTHOP: 9708 /* Handled at IP level */ 9709 return (-EINVAL); 9710 default: 9711 return (-1); 9712 } 9713 break; 9714 case IPPROTO_IPV6: 9715 /* 9716 * IPPROTO_IPV6 options are only supported for sockets 9717 * that are using IPv6 on the wire. 9718 */ 9719 if (tcp->tcp_ipversion != IPV6_VERSION) { 9720 return (-1); 9721 } 9722 switch (name) { 9723 case IPV6_UNICAST_HOPS: 9724 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9725 break; /* goto sizeof (int) option return */ 9726 case IPV6_BOUND_IF: 9727 /* Zero if not set */ 9728 *i1 = tcp->tcp_bound_if; 9729 break; /* goto sizeof (int) option return */ 9730 case IPV6_RECVPKTINFO: 9731 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9732 *i1 = 1; 9733 else 9734 *i1 = 0; 9735 break; /* goto sizeof (int) option return */ 9736 case IPV6_RECVTCLASS: 9737 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9738 *i1 = 1; 9739 else 9740 *i1 = 0; 9741 break; /* goto sizeof (int) option return */ 9742 case IPV6_RECVHOPLIMIT: 9743 if (tcp->tcp_ipv6_recvancillary & 9744 TCP_IPV6_RECVHOPLIMIT) 9745 *i1 = 1; 9746 else 9747 *i1 = 0; 9748 break; /* goto sizeof (int) option return */ 9749 case IPV6_RECVHOPOPTS: 9750 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9751 *i1 = 1; 9752 else 9753 *i1 = 0; 9754 break; /* goto sizeof (int) option return */ 9755 case IPV6_RECVDSTOPTS: 9756 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9757 *i1 = 1; 9758 else 9759 *i1 = 0; 9760 break; /* goto sizeof (int) option return */ 9761 case _OLD_IPV6_RECVDSTOPTS: 9762 if (tcp->tcp_ipv6_recvancillary & 9763 TCP_OLD_IPV6_RECVDSTOPTS) 9764 *i1 = 1; 9765 else 9766 *i1 = 0; 9767 break; /* goto sizeof (int) option return */ 9768 case IPV6_RECVRTHDR: 9769 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9770 *i1 = 1; 9771 else 9772 *i1 = 0; 9773 break; /* goto sizeof (int) option return */ 9774 case IPV6_RECVRTHDRDSTOPTS: 9775 if (tcp->tcp_ipv6_recvancillary & 9776 TCP_IPV6_RECVRTDSTOPTS) 9777 *i1 = 1; 9778 else 9779 *i1 = 0; 9780 break; /* goto sizeof (int) option return */ 9781 case IPV6_PKTINFO: { 9782 /* XXX assumes that caller has room for max size! */ 9783 struct in6_pktinfo *pkti; 9784 9785 pkti = (struct in6_pktinfo *)ptr; 9786 if (ipp->ipp_fields & IPPF_IFINDEX) 9787 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9788 else 9789 pkti->ipi6_ifindex = 0; 9790 if (ipp->ipp_fields & IPPF_ADDR) 9791 pkti->ipi6_addr = ipp->ipp_addr; 9792 else 9793 pkti->ipi6_addr = ipv6_all_zeros; 9794 return (sizeof (struct in6_pktinfo)); 9795 } 9796 case IPV6_TCLASS: 9797 if (ipp->ipp_fields & IPPF_TCLASS) 9798 *i1 = ipp->ipp_tclass; 9799 else 9800 *i1 = IPV6_FLOW_TCLASS( 9801 IPV6_DEFAULT_VERS_AND_FLOW); 9802 break; /* goto sizeof (int) option return */ 9803 case IPV6_NEXTHOP: { 9804 sin6_t *sin6 = (sin6_t *)ptr; 9805 9806 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9807 return (0); 9808 *sin6 = sin6_null; 9809 sin6->sin6_family = AF_INET6; 9810 sin6->sin6_addr = ipp->ipp_nexthop; 9811 return (sizeof (sin6_t)); 9812 } 9813 case IPV6_HOPOPTS: 9814 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9815 return (0); 9816 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9817 return (0); 9818 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9819 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9820 if (tcp->tcp_label_len > 0) { 9821 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9822 ptr[1] = (ipp->ipp_hopoptslen - 9823 tcp->tcp_label_len + 7) / 8 - 1; 9824 } 9825 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9826 case IPV6_RTHDRDSTOPTS: 9827 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9828 return (0); 9829 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9830 return (ipp->ipp_rtdstoptslen); 9831 case IPV6_RTHDR: 9832 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9833 return (0); 9834 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9835 return (ipp->ipp_rthdrlen); 9836 case IPV6_DSTOPTS: 9837 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9838 return (0); 9839 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9840 return (ipp->ipp_dstoptslen); 9841 case IPV6_SRC_PREFERENCES: 9842 return (ip6_get_src_preferences(connp, 9843 (uint32_t *)ptr)); 9844 case IPV6_PATHMTU: { 9845 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9846 9847 if (tcp->tcp_state < TCPS_ESTABLISHED) 9848 return (-1); 9849 9850 return (ip_fill_mtuinfo(&connp->conn_remv6, 9851 connp->conn_fport, mtuinfo)); 9852 } 9853 default: 9854 return (-1); 9855 } 9856 break; 9857 default: 9858 return (-1); 9859 } 9860 return (sizeof (int)); 9861 } 9862 9863 /* 9864 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9865 * Parameters are assumed to be verified by the caller. 9866 */ 9867 /* ARGSUSED */ 9868 int 9869 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 9870 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9871 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9872 { 9873 conn_t *connp = Q_TO_CONN(q); 9874 tcp_t *tcp = connp->conn_tcp; 9875 int *i1 = (int *)invalp; 9876 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9877 boolean_t checkonly; 9878 int reterr; 9879 9880 switch (optset_context) { 9881 case SETFN_OPTCOM_CHECKONLY: 9882 checkonly = B_TRUE; 9883 /* 9884 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9885 * inlen != 0 implies value supplied and 9886 * we have to "pretend" to set it. 9887 * inlen == 0 implies that there is no 9888 * value part in T_CHECK request and just validation 9889 * done elsewhere should be enough, we just return here. 9890 */ 9891 if (inlen == 0) { 9892 *outlenp = 0; 9893 return (0); 9894 } 9895 break; 9896 case SETFN_OPTCOM_NEGOTIATE: 9897 checkonly = B_FALSE; 9898 break; 9899 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9900 case SETFN_CONN_NEGOTIATE: 9901 checkonly = B_FALSE; 9902 /* 9903 * Negotiating local and "association-related" options 9904 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9905 * primitives is allowed by XTI, but we choose 9906 * to not implement this style negotiation for Internet 9907 * protocols (We interpret it is a must for OSI world but 9908 * optional for Internet protocols) for all options. 9909 * [ Will do only for the few options that enable test 9910 * suites that our XTI implementation of this feature 9911 * works for transports that do allow it ] 9912 */ 9913 if (!tcp_allow_connopt_set(level, name)) { 9914 *outlenp = 0; 9915 return (EINVAL); 9916 } 9917 break; 9918 default: 9919 /* 9920 * We should never get here 9921 */ 9922 *outlenp = 0; 9923 return (EINVAL); 9924 } 9925 9926 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9927 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9928 9929 /* 9930 * For TCP, we should have no ancillary data sent down 9931 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9932 * has to be zero. 9933 */ 9934 ASSERT(thisdg_attrs == NULL); 9935 9936 /* 9937 * For fixed length options, no sanity check 9938 * of passed in length is done. It is assumed *_optcom_req() 9939 * routines do the right thing. 9940 */ 9941 9942 switch (level) { 9943 case SOL_SOCKET: 9944 switch (name) { 9945 case SO_LINGER: { 9946 struct linger *lgr = (struct linger *)invalp; 9947 9948 if (!checkonly) { 9949 if (lgr->l_onoff) { 9950 tcp->tcp_linger = 1; 9951 tcp->tcp_lingertime = lgr->l_linger; 9952 } else { 9953 tcp->tcp_linger = 0; 9954 tcp->tcp_lingertime = 0; 9955 } 9956 /* struct copy */ 9957 *(struct linger *)outvalp = *lgr; 9958 } else { 9959 if (!lgr->l_onoff) { 9960 ((struct linger *)outvalp)->l_onoff = 0; 9961 ((struct linger *)outvalp)->l_linger = 0; 9962 } else { 9963 /* struct copy */ 9964 *(struct linger *)outvalp = *lgr; 9965 } 9966 } 9967 *outlenp = sizeof (struct linger); 9968 return (0); 9969 } 9970 case SO_DEBUG: 9971 if (!checkonly) 9972 tcp->tcp_debug = onoff; 9973 break; 9974 case SO_KEEPALIVE: 9975 if (checkonly) { 9976 /* T_CHECK case */ 9977 break; 9978 } 9979 9980 if (!onoff) { 9981 if (tcp->tcp_ka_enabled) { 9982 if (tcp->tcp_ka_tid != 0) { 9983 (void) TCP_TIMER_CANCEL(tcp, 9984 tcp->tcp_ka_tid); 9985 tcp->tcp_ka_tid = 0; 9986 } 9987 tcp->tcp_ka_enabled = 0; 9988 } 9989 break; 9990 } 9991 if (!tcp->tcp_ka_enabled) { 9992 /* Crank up the keepalive timer */ 9993 tcp->tcp_ka_last_intrvl = 0; 9994 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9995 tcp_keepalive_killer, 9996 MSEC_TO_TICK(tcp->tcp_ka_interval)); 9997 tcp->tcp_ka_enabled = 1; 9998 } 9999 break; 10000 case SO_DONTROUTE: 10001 /* 10002 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10003 * only of interest to IP. We track them here only so 10004 * that we can report their current value. 10005 */ 10006 if (!checkonly) { 10007 tcp->tcp_dontroute = onoff; 10008 tcp->tcp_connp->conn_dontroute = onoff; 10009 } 10010 break; 10011 case SO_USELOOPBACK: 10012 if (!checkonly) { 10013 tcp->tcp_useloopback = onoff; 10014 tcp->tcp_connp->conn_loopback = onoff; 10015 } 10016 break; 10017 case SO_BROADCAST: 10018 if (!checkonly) { 10019 tcp->tcp_broadcast = onoff; 10020 tcp->tcp_connp->conn_broadcast = onoff; 10021 } 10022 break; 10023 case SO_REUSEADDR: 10024 if (!checkonly) { 10025 tcp->tcp_reuseaddr = onoff; 10026 tcp->tcp_connp->conn_reuseaddr = onoff; 10027 } 10028 break; 10029 case SO_OOBINLINE: 10030 if (!checkonly) 10031 tcp->tcp_oobinline = onoff; 10032 break; 10033 case SO_DGRAM_ERRIND: 10034 if (!checkonly) 10035 tcp->tcp_dgram_errind = onoff; 10036 break; 10037 case SO_SNDBUF: { 10038 tcp_t *peer_tcp; 10039 10040 if (*i1 > tcp_max_buf) { 10041 *outlenp = 0; 10042 return (ENOBUFS); 10043 } 10044 if (checkonly) 10045 break; 10046 10047 tcp->tcp_xmit_hiwater = *i1; 10048 if (tcp_snd_lowat_fraction != 0) 10049 tcp->tcp_xmit_lowater = 10050 tcp->tcp_xmit_hiwater / 10051 tcp_snd_lowat_fraction; 10052 (void) tcp_maxpsz_set(tcp, B_TRUE); 10053 /* 10054 * If we are flow-controlled, recheck the condition. 10055 * There are apps that increase SO_SNDBUF size when 10056 * flow-controlled (EWOULDBLOCK), and expect the flow 10057 * control condition to be lifted right away. 10058 * 10059 * For the fused tcp loopback case, in order to avoid 10060 * a race with the peer's tcp_fuse_rrw() we need to 10061 * hold its fuse_lock while accessing tcp_flow_stopped. 10062 */ 10063 peer_tcp = tcp->tcp_loopback_peer; 10064 ASSERT(!tcp->tcp_fused || peer_tcp != NULL); 10065 if (tcp->tcp_fused) 10066 mutex_enter(&peer_tcp->tcp_fuse_lock); 10067 10068 if (tcp->tcp_flow_stopped && 10069 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10070 tcp_clrqfull(tcp); 10071 } 10072 if (tcp->tcp_fused) 10073 mutex_exit(&peer_tcp->tcp_fuse_lock); 10074 break; 10075 } 10076 case SO_RCVBUF: 10077 if (*i1 > tcp_max_buf) { 10078 *outlenp = 0; 10079 return (ENOBUFS); 10080 } 10081 /* Silently ignore zero */ 10082 if (!checkonly && *i1 != 0) { 10083 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10084 (void) tcp_rwnd_set(tcp, *i1); 10085 } 10086 /* 10087 * XXX should we return the rwnd here 10088 * and tcp_opt_get ? 10089 */ 10090 break; 10091 case SO_SND_COPYAVOID: 10092 if (!checkonly) { 10093 /* we only allow enable at most once for now */ 10094 if (tcp->tcp_loopback || 10095 (!tcp->tcp_snd_zcopy_aware && 10096 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10097 *outlenp = 0; 10098 return (EOPNOTSUPP); 10099 } 10100 tcp->tcp_snd_zcopy_aware = 1; 10101 } 10102 break; 10103 case SO_ALLZONES: 10104 /* Handled at the IP level */ 10105 return (-EINVAL); 10106 case SO_ANON_MLP: 10107 if (!checkonly) { 10108 mutex_enter(&connp->conn_lock); 10109 connp->conn_anon_mlp = onoff; 10110 mutex_exit(&connp->conn_lock); 10111 } 10112 break; 10113 case SO_MAC_EXEMPT: 10114 if (secpolicy_net_mac_aware(cr) != 0 || 10115 IPCL_IS_BOUND(connp)) 10116 return (EACCES); 10117 if (!checkonly) { 10118 mutex_enter(&connp->conn_lock); 10119 connp->conn_mac_exempt = onoff; 10120 mutex_exit(&connp->conn_lock); 10121 } 10122 break; 10123 case SO_EXCLBIND: 10124 if (!checkonly) 10125 tcp->tcp_exclbind = onoff; 10126 break; 10127 default: 10128 *outlenp = 0; 10129 return (EINVAL); 10130 } 10131 break; 10132 case IPPROTO_TCP: 10133 switch (name) { 10134 case TCP_NODELAY: 10135 if (!checkonly) 10136 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10137 break; 10138 case TCP_NOTIFY_THRESHOLD: 10139 if (!checkonly) 10140 tcp->tcp_first_timer_threshold = *i1; 10141 break; 10142 case TCP_ABORT_THRESHOLD: 10143 if (!checkonly) 10144 tcp->tcp_second_timer_threshold = *i1; 10145 break; 10146 case TCP_CONN_NOTIFY_THRESHOLD: 10147 if (!checkonly) 10148 tcp->tcp_first_ctimer_threshold = *i1; 10149 break; 10150 case TCP_CONN_ABORT_THRESHOLD: 10151 if (!checkonly) 10152 tcp->tcp_second_ctimer_threshold = *i1; 10153 break; 10154 case TCP_RECVDSTADDR: 10155 if (tcp->tcp_state > TCPS_LISTEN) 10156 return (EOPNOTSUPP); 10157 if (!checkonly) 10158 tcp->tcp_recvdstaddr = onoff; 10159 break; 10160 case TCP_ANONPRIVBIND: 10161 if ((reterr = secpolicy_net_privaddr(cr, 0)) != 0) { 10162 *outlenp = 0; 10163 return (reterr); 10164 } 10165 if (!checkonly) { 10166 tcp->tcp_anon_priv_bind = onoff; 10167 } 10168 break; 10169 case TCP_EXCLBIND: 10170 if (!checkonly) 10171 tcp->tcp_exclbind = onoff; 10172 break; /* goto sizeof (int) option return */ 10173 case TCP_INIT_CWND: { 10174 uint32_t init_cwnd = *((uint32_t *)invalp); 10175 10176 if (checkonly) 10177 break; 10178 10179 /* 10180 * Only allow socket with network configuration 10181 * privilege to set the initial cwnd to be larger 10182 * than allowed by RFC 3390. 10183 */ 10184 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10185 tcp->tcp_init_cwnd = init_cwnd; 10186 break; 10187 } 10188 if ((reterr = secpolicy_net_config(cr, B_TRUE)) != 0) { 10189 *outlenp = 0; 10190 return (reterr); 10191 } 10192 if (init_cwnd > TCP_MAX_INIT_CWND) { 10193 *outlenp = 0; 10194 return (EINVAL); 10195 } 10196 tcp->tcp_init_cwnd = init_cwnd; 10197 break; 10198 } 10199 case TCP_KEEPALIVE_THRESHOLD: 10200 if (checkonly) 10201 break; 10202 10203 if (*i1 < tcp_keepalive_interval_low || 10204 *i1 > tcp_keepalive_interval_high) { 10205 *outlenp = 0; 10206 return (EINVAL); 10207 } 10208 if (*i1 != tcp->tcp_ka_interval) { 10209 tcp->tcp_ka_interval = *i1; 10210 /* 10211 * Check if we need to restart the 10212 * keepalive timer. 10213 */ 10214 if (tcp->tcp_ka_tid != 0) { 10215 ASSERT(tcp->tcp_ka_enabled); 10216 (void) TCP_TIMER_CANCEL(tcp, 10217 tcp->tcp_ka_tid); 10218 tcp->tcp_ka_last_intrvl = 0; 10219 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10220 tcp_keepalive_killer, 10221 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10222 } 10223 } 10224 break; 10225 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10226 if (!checkonly) { 10227 if (*i1 < tcp_keepalive_abort_interval_low || 10228 *i1 > tcp_keepalive_abort_interval_high) { 10229 *outlenp = 0; 10230 return (EINVAL); 10231 } 10232 tcp->tcp_ka_abort_thres = *i1; 10233 } 10234 break; 10235 case TCP_CORK: 10236 if (!checkonly) { 10237 /* 10238 * if tcp->tcp_cork was set and is now 10239 * being unset, we have to make sure that 10240 * the remaining data gets sent out. Also 10241 * unset tcp->tcp_cork so that tcp_wput_data() 10242 * can send data even if it is less than mss 10243 */ 10244 if (tcp->tcp_cork && onoff == 0 && 10245 tcp->tcp_unsent > 0) { 10246 tcp->tcp_cork = B_FALSE; 10247 tcp_wput_data(tcp, NULL, B_FALSE); 10248 } 10249 tcp->tcp_cork = onoff; 10250 } 10251 break; 10252 default: 10253 *outlenp = 0; 10254 return (EINVAL); 10255 } 10256 break; 10257 case IPPROTO_IP: 10258 if (tcp->tcp_family != AF_INET) { 10259 *outlenp = 0; 10260 return (ENOPROTOOPT); 10261 } 10262 switch (name) { 10263 case IP_OPTIONS: 10264 case T_IP_OPTIONS: 10265 reterr = tcp_opt_set_header(tcp, checkonly, 10266 invalp, inlen); 10267 if (reterr) { 10268 *outlenp = 0; 10269 return (reterr); 10270 } 10271 /* OK return - copy input buffer into output buffer */ 10272 if (invalp != outvalp) { 10273 /* don't trust bcopy for identical src/dst */ 10274 bcopy(invalp, outvalp, inlen); 10275 } 10276 *outlenp = inlen; 10277 return (0); 10278 case IP_TOS: 10279 case T_IP_TOS: 10280 if (!checkonly) { 10281 tcp->tcp_ipha->ipha_type_of_service = 10282 (uchar_t)*i1; 10283 tcp->tcp_tos = (uchar_t)*i1; 10284 } 10285 break; 10286 case IP_TTL: 10287 if (!checkonly) { 10288 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10289 tcp->tcp_ttl = (uchar_t)*i1; 10290 } 10291 break; 10292 case IP_BOUND_IF: 10293 case IP_NEXTHOP: 10294 /* Handled at the IP level */ 10295 return (-EINVAL); 10296 case IP_SEC_OPT: 10297 /* 10298 * We should not allow policy setting after 10299 * we start listening for connections. 10300 */ 10301 if (tcp->tcp_state == TCPS_LISTEN) { 10302 return (EINVAL); 10303 } else { 10304 /* Handled at the IP level */ 10305 return (-EINVAL); 10306 } 10307 default: 10308 *outlenp = 0; 10309 return (EINVAL); 10310 } 10311 break; 10312 case IPPROTO_IPV6: { 10313 ip6_pkt_t *ipp; 10314 10315 /* 10316 * IPPROTO_IPV6 options are only supported for sockets 10317 * that are using IPv6 on the wire. 10318 */ 10319 if (tcp->tcp_ipversion != IPV6_VERSION) { 10320 *outlenp = 0; 10321 return (ENOPROTOOPT); 10322 } 10323 /* 10324 * Only sticky options; no ancillary data 10325 */ 10326 ASSERT(thisdg_attrs == NULL); 10327 ipp = &tcp->tcp_sticky_ipp; 10328 10329 switch (name) { 10330 case IPV6_UNICAST_HOPS: 10331 /* -1 means use default */ 10332 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10333 *outlenp = 0; 10334 return (EINVAL); 10335 } 10336 if (!checkonly) { 10337 if (*i1 == -1) { 10338 tcp->tcp_ip6h->ip6_hops = 10339 ipp->ipp_unicast_hops = 10340 (uint8_t)tcp_ipv6_hoplimit; 10341 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10342 /* Pass modified value to IP. */ 10343 *i1 = tcp->tcp_ip6h->ip6_hops; 10344 } else { 10345 tcp->tcp_ip6h->ip6_hops = 10346 ipp->ipp_unicast_hops = 10347 (uint8_t)*i1; 10348 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10349 } 10350 reterr = tcp_build_hdrs(q, tcp); 10351 if (reterr != 0) 10352 return (reterr); 10353 } 10354 break; 10355 case IPV6_BOUND_IF: 10356 if (!checkonly) { 10357 int error = 0; 10358 10359 tcp->tcp_bound_if = *i1; 10360 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10361 B_TRUE, checkonly, level, name, mblk); 10362 if (error != 0) { 10363 *outlenp = 0; 10364 return (error); 10365 } 10366 } 10367 break; 10368 /* 10369 * Set boolean switches for ancillary data delivery 10370 */ 10371 case IPV6_RECVPKTINFO: 10372 if (!checkonly) { 10373 if (onoff) 10374 tcp->tcp_ipv6_recvancillary |= 10375 TCP_IPV6_RECVPKTINFO; 10376 else 10377 tcp->tcp_ipv6_recvancillary &= 10378 ~TCP_IPV6_RECVPKTINFO; 10379 /* Force it to be sent up with the next msg */ 10380 tcp->tcp_recvifindex = 0; 10381 } 10382 break; 10383 case IPV6_RECVTCLASS: 10384 if (!checkonly) { 10385 if (onoff) 10386 tcp->tcp_ipv6_recvancillary |= 10387 TCP_IPV6_RECVTCLASS; 10388 else 10389 tcp->tcp_ipv6_recvancillary &= 10390 ~TCP_IPV6_RECVTCLASS; 10391 } 10392 break; 10393 case IPV6_RECVHOPLIMIT: 10394 if (!checkonly) { 10395 if (onoff) 10396 tcp->tcp_ipv6_recvancillary |= 10397 TCP_IPV6_RECVHOPLIMIT; 10398 else 10399 tcp->tcp_ipv6_recvancillary &= 10400 ~TCP_IPV6_RECVHOPLIMIT; 10401 /* Force it to be sent up with the next msg */ 10402 tcp->tcp_recvhops = 0xffffffffU; 10403 } 10404 break; 10405 case IPV6_RECVHOPOPTS: 10406 if (!checkonly) { 10407 if (onoff) 10408 tcp->tcp_ipv6_recvancillary |= 10409 TCP_IPV6_RECVHOPOPTS; 10410 else 10411 tcp->tcp_ipv6_recvancillary &= 10412 ~TCP_IPV6_RECVHOPOPTS; 10413 } 10414 break; 10415 case IPV6_RECVDSTOPTS: 10416 if (!checkonly) { 10417 if (onoff) 10418 tcp->tcp_ipv6_recvancillary |= 10419 TCP_IPV6_RECVDSTOPTS; 10420 else 10421 tcp->tcp_ipv6_recvancillary &= 10422 ~TCP_IPV6_RECVDSTOPTS; 10423 } 10424 break; 10425 case _OLD_IPV6_RECVDSTOPTS: 10426 if (!checkonly) { 10427 if (onoff) 10428 tcp->tcp_ipv6_recvancillary |= 10429 TCP_OLD_IPV6_RECVDSTOPTS; 10430 else 10431 tcp->tcp_ipv6_recvancillary &= 10432 ~TCP_OLD_IPV6_RECVDSTOPTS; 10433 } 10434 break; 10435 case IPV6_RECVRTHDR: 10436 if (!checkonly) { 10437 if (onoff) 10438 tcp->tcp_ipv6_recvancillary |= 10439 TCP_IPV6_RECVRTHDR; 10440 else 10441 tcp->tcp_ipv6_recvancillary &= 10442 ~TCP_IPV6_RECVRTHDR; 10443 } 10444 break; 10445 case IPV6_RECVRTHDRDSTOPTS: 10446 if (!checkonly) { 10447 if (onoff) 10448 tcp->tcp_ipv6_recvancillary |= 10449 TCP_IPV6_RECVRTDSTOPTS; 10450 else 10451 tcp->tcp_ipv6_recvancillary &= 10452 ~TCP_IPV6_RECVRTDSTOPTS; 10453 } 10454 break; 10455 case IPV6_PKTINFO: 10456 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10457 return (EINVAL); 10458 if (checkonly) 10459 break; 10460 10461 if (inlen == 0) { 10462 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10463 } else { 10464 struct in6_pktinfo *pkti; 10465 10466 pkti = (struct in6_pktinfo *)invalp; 10467 /* 10468 * RFC 3542 states that ipi6_addr must be 10469 * the unspecified address when setting the 10470 * IPV6_PKTINFO sticky socket option on a 10471 * TCP socket. 10472 */ 10473 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10474 return (EINVAL); 10475 /* 10476 * ip6_set_pktinfo() validates the source 10477 * address and interface index. 10478 */ 10479 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10480 pkti, mblk); 10481 if (reterr != 0) 10482 return (reterr); 10483 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10484 ipp->ipp_addr = pkti->ipi6_addr; 10485 if (ipp->ipp_ifindex != 0) 10486 ipp->ipp_fields |= IPPF_IFINDEX; 10487 else 10488 ipp->ipp_fields &= ~IPPF_IFINDEX; 10489 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10490 ipp->ipp_fields |= IPPF_ADDR; 10491 else 10492 ipp->ipp_fields &= ~IPPF_ADDR; 10493 } 10494 reterr = tcp_build_hdrs(q, tcp); 10495 if (reterr != 0) 10496 return (reterr); 10497 break; 10498 case IPV6_TCLASS: 10499 if (inlen != 0 && inlen != sizeof (int)) 10500 return (EINVAL); 10501 if (checkonly) 10502 break; 10503 10504 if (inlen == 0) { 10505 ipp->ipp_fields &= ~IPPF_TCLASS; 10506 } else { 10507 if (*i1 > 255 || *i1 < -1) 10508 return (EINVAL); 10509 if (*i1 == -1) { 10510 ipp->ipp_tclass = 0; 10511 *i1 = 0; 10512 } else { 10513 ipp->ipp_tclass = *i1; 10514 } 10515 ipp->ipp_fields |= IPPF_TCLASS; 10516 } 10517 reterr = tcp_build_hdrs(q, tcp); 10518 if (reterr != 0) 10519 return (reterr); 10520 break; 10521 case IPV6_NEXTHOP: 10522 /* 10523 * IP will verify that the nexthop is reachable 10524 * and fail for sticky options. 10525 */ 10526 if (inlen != 0 && inlen != sizeof (sin6_t)) 10527 return (EINVAL); 10528 if (checkonly) 10529 break; 10530 10531 if (inlen == 0) { 10532 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10533 } else { 10534 sin6_t *sin6 = (sin6_t *)invalp; 10535 10536 if (sin6->sin6_family != AF_INET6) 10537 return (EAFNOSUPPORT); 10538 if (IN6_IS_ADDR_V4MAPPED( 10539 &sin6->sin6_addr)) 10540 return (EADDRNOTAVAIL); 10541 ipp->ipp_nexthop = sin6->sin6_addr; 10542 if (!IN6_IS_ADDR_UNSPECIFIED( 10543 &ipp->ipp_nexthop)) 10544 ipp->ipp_fields |= IPPF_NEXTHOP; 10545 else 10546 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10547 } 10548 reterr = tcp_build_hdrs(q, tcp); 10549 if (reterr != 0) 10550 return (reterr); 10551 break; 10552 case IPV6_HOPOPTS: { 10553 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10554 10555 /* 10556 * Sanity checks - minimum size, size a multiple of 10557 * eight bytes, and matching size passed in. 10558 */ 10559 if (inlen != 0 && 10560 inlen != (8 * (hopts->ip6h_len + 1))) 10561 return (EINVAL); 10562 10563 if (checkonly) 10564 break; 10565 10566 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10567 (uchar_t **)&ipp->ipp_hopopts, 10568 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10569 if (reterr != 0) 10570 return (reterr); 10571 if (ipp->ipp_hopoptslen == 0) 10572 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10573 else 10574 ipp->ipp_fields |= IPPF_HOPOPTS; 10575 reterr = tcp_build_hdrs(q, tcp); 10576 if (reterr != 0) 10577 return (reterr); 10578 break; 10579 } 10580 case IPV6_RTHDRDSTOPTS: { 10581 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10582 10583 /* 10584 * Sanity checks - minimum size, size a multiple of 10585 * eight bytes, and matching size passed in. 10586 */ 10587 if (inlen != 0 && 10588 inlen != (8 * (dopts->ip6d_len + 1))) 10589 return (EINVAL); 10590 10591 if (checkonly) 10592 break; 10593 10594 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10595 (uchar_t **)&ipp->ipp_rtdstopts, 10596 &ipp->ipp_rtdstoptslen, 0); 10597 if (reterr != 0) 10598 return (reterr); 10599 if (ipp->ipp_rtdstoptslen == 0) 10600 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10601 else 10602 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10603 reterr = tcp_build_hdrs(q, tcp); 10604 if (reterr != 0) 10605 return (reterr); 10606 break; 10607 } 10608 case IPV6_DSTOPTS: { 10609 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10610 10611 /* 10612 * Sanity checks - minimum size, size a multiple of 10613 * eight bytes, and matching size passed in. 10614 */ 10615 if (inlen != 0 && 10616 inlen != (8 * (dopts->ip6d_len + 1))) 10617 return (EINVAL); 10618 10619 if (checkonly) 10620 break; 10621 10622 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10623 (uchar_t **)&ipp->ipp_dstopts, 10624 &ipp->ipp_dstoptslen, 0); 10625 if (reterr != 0) 10626 return (reterr); 10627 if (ipp->ipp_dstoptslen == 0) 10628 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10629 else 10630 ipp->ipp_fields |= IPPF_DSTOPTS; 10631 reterr = tcp_build_hdrs(q, tcp); 10632 if (reterr != 0) 10633 return (reterr); 10634 break; 10635 } 10636 case IPV6_RTHDR: { 10637 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10638 10639 /* 10640 * Sanity checks - minimum size, size a multiple of 10641 * eight bytes, and matching size passed in. 10642 */ 10643 if (inlen != 0 && 10644 inlen != (8 * (rt->ip6r_len + 1))) 10645 return (EINVAL); 10646 10647 if (checkonly) 10648 break; 10649 10650 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10651 (uchar_t **)&ipp->ipp_rthdr, 10652 &ipp->ipp_rthdrlen, 0); 10653 if (reterr != 0) 10654 return (reterr); 10655 if (ipp->ipp_rthdrlen == 0) 10656 ipp->ipp_fields &= ~IPPF_RTHDR; 10657 else 10658 ipp->ipp_fields |= IPPF_RTHDR; 10659 reterr = tcp_build_hdrs(q, tcp); 10660 if (reterr != 0) 10661 return (reterr); 10662 break; 10663 } 10664 case IPV6_V6ONLY: 10665 if (!checkonly) 10666 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10667 break; 10668 case IPV6_USE_MIN_MTU: 10669 if (inlen != sizeof (int)) 10670 return (EINVAL); 10671 10672 if (*i1 < -1 || *i1 > 1) 10673 return (EINVAL); 10674 10675 if (checkonly) 10676 break; 10677 10678 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10679 ipp->ipp_use_min_mtu = *i1; 10680 break; 10681 case IPV6_BOUND_PIF: 10682 /* Handled at the IP level */ 10683 return (-EINVAL); 10684 case IPV6_SEC_OPT: 10685 /* 10686 * We should not allow policy setting after 10687 * we start listening for connections. 10688 */ 10689 if (tcp->tcp_state == TCPS_LISTEN) { 10690 return (EINVAL); 10691 } else { 10692 /* Handled at the IP level */ 10693 return (-EINVAL); 10694 } 10695 case IPV6_SRC_PREFERENCES: 10696 if (inlen != sizeof (uint32_t)) 10697 return (EINVAL); 10698 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10699 *(uint32_t *)invalp); 10700 if (reterr != 0) { 10701 *outlenp = 0; 10702 return (reterr); 10703 } 10704 break; 10705 default: 10706 *outlenp = 0; 10707 return (EINVAL); 10708 } 10709 break; 10710 } /* end IPPROTO_IPV6 */ 10711 default: 10712 *outlenp = 0; 10713 return (EINVAL); 10714 } 10715 /* 10716 * Common case of OK return with outval same as inval 10717 */ 10718 if (invalp != outvalp) { 10719 /* don't trust bcopy for identical src/dst */ 10720 (void) bcopy(invalp, outvalp, inlen); 10721 } 10722 *outlenp = inlen; 10723 return (0); 10724 } 10725 10726 /* 10727 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10728 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10729 * headers, and the maximum size tcp header (to avoid reallocation 10730 * on the fly for additional tcp options). 10731 * Returns failure if can't allocate memory. 10732 */ 10733 static int 10734 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 10735 { 10736 char *hdrs; 10737 uint_t hdrs_len; 10738 ip6i_t *ip6i; 10739 char buf[TCP_MAX_HDR_LENGTH]; 10740 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10741 in6_addr_t src, dst; 10742 10743 /* 10744 * save the existing tcp header and source/dest IP addresses 10745 */ 10746 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10747 src = tcp->tcp_ip6h->ip6_src; 10748 dst = tcp->tcp_ip6h->ip6_dst; 10749 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10750 ASSERT(hdrs_len != 0); 10751 if (hdrs_len > tcp->tcp_iphc_len) { 10752 /* Need to reallocate */ 10753 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10754 if (hdrs == NULL) 10755 return (ENOMEM); 10756 if (tcp->tcp_iphc != NULL) { 10757 if (tcp->tcp_hdr_grown) { 10758 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10759 } else { 10760 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10761 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10762 } 10763 tcp->tcp_iphc_len = 0; 10764 } 10765 ASSERT(tcp->tcp_iphc_len == 0); 10766 tcp->tcp_iphc = hdrs; 10767 tcp->tcp_iphc_len = hdrs_len; 10768 tcp->tcp_hdr_grown = B_TRUE; 10769 } 10770 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10771 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10772 10773 /* Set header fields not in ipp */ 10774 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10775 ip6i = (ip6i_t *)tcp->tcp_iphc; 10776 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10777 } else { 10778 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10779 } 10780 /* 10781 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10782 * 10783 * tcp->tcp_tcp_hdr_len doesn't change here. 10784 */ 10785 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10786 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10787 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10788 10789 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10790 10791 tcp->tcp_ip6h->ip6_src = src; 10792 tcp->tcp_ip6h->ip6_dst = dst; 10793 10794 /* 10795 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10796 * the default value for TCP. 10797 */ 10798 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10799 tcp->tcp_ip6h->ip6_hops = tcp_ipv6_hoplimit; 10800 10801 /* 10802 * If we're setting extension headers after a connection 10803 * has been established, and if we have a routing header 10804 * among the extension headers, call ip_massage_options_v6 to 10805 * manipulate the routing header/ip6_dst set the checksum 10806 * difference in the tcp header template. 10807 * (This happens in tcp_connect_ipv6 if the routing header 10808 * is set prior to the connect.) 10809 * Set the tcp_sum to zero first in case we've cleared a 10810 * routing header or don't have one at all. 10811 */ 10812 tcp->tcp_sum = 0; 10813 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10814 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10815 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10816 (uint8_t *)tcp->tcp_tcph); 10817 if (rth != NULL) { 10818 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10819 rth); 10820 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10821 (tcp->tcp_sum >> 16)); 10822 } 10823 } 10824 10825 /* Try to get everything in a single mblk */ 10826 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcp_wroff_xtra); 10827 return (0); 10828 } 10829 10830 /* 10831 * Transfer any source route option from ipha to buf/dst in reversed form. 10832 */ 10833 static int 10834 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10835 { 10836 ipoptp_t opts; 10837 uchar_t *opt; 10838 uint8_t optval; 10839 uint8_t optlen; 10840 uint32_t len = 0; 10841 10842 for (optval = ipoptp_first(&opts, ipha); 10843 optval != IPOPT_EOL; 10844 optval = ipoptp_next(&opts)) { 10845 opt = opts.ipoptp_cur; 10846 optlen = opts.ipoptp_len; 10847 switch (optval) { 10848 int off1, off2; 10849 case IPOPT_SSRR: 10850 case IPOPT_LSRR: 10851 10852 /* Reverse source route */ 10853 /* 10854 * First entry should be the next to last one in the 10855 * current source route (the last entry is our 10856 * address.) 10857 * The last entry should be the final destination. 10858 */ 10859 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10860 buf[IPOPT_OLEN] = (uint8_t)optlen; 10861 off1 = IPOPT_MINOFF_SR - 1; 10862 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10863 if (off2 < 0) { 10864 /* No entries in source route */ 10865 break; 10866 } 10867 bcopy(opt + off2, dst, IP_ADDR_LEN); 10868 /* 10869 * Note: use src since ipha has not had its src 10870 * and dst reversed (it is in the state it was 10871 * received. 10872 */ 10873 bcopy(&ipha->ipha_src, buf + off2, 10874 IP_ADDR_LEN); 10875 off2 -= IP_ADDR_LEN; 10876 10877 while (off2 > 0) { 10878 bcopy(opt + off2, buf + off1, 10879 IP_ADDR_LEN); 10880 off1 += IP_ADDR_LEN; 10881 off2 -= IP_ADDR_LEN; 10882 } 10883 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 10884 buf += optlen; 10885 len += optlen; 10886 break; 10887 } 10888 } 10889 done: 10890 /* Pad the resulting options */ 10891 while (len & 0x3) { 10892 *buf++ = IPOPT_EOL; 10893 len++; 10894 } 10895 return (len); 10896 } 10897 10898 10899 /* 10900 * Extract and revert a source route from ipha (if any) 10901 * and then update the relevant fields in both tcp_t and the standard header. 10902 */ 10903 static void 10904 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 10905 { 10906 char buf[TCP_MAX_HDR_LENGTH]; 10907 uint_t tcph_len; 10908 int len; 10909 10910 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 10911 len = IPH_HDR_LENGTH(ipha); 10912 if (len == IP_SIMPLE_HDR_LENGTH) 10913 /* Nothing to do */ 10914 return; 10915 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 10916 (len & 0x3)) 10917 return; 10918 10919 tcph_len = tcp->tcp_tcp_hdr_len; 10920 bcopy(tcp->tcp_tcph, buf, tcph_len); 10921 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 10922 (tcp->tcp_ipha->ipha_dst & 0xffff); 10923 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 10924 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 10925 len += IP_SIMPLE_HDR_LENGTH; 10926 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 10927 (tcp->tcp_ipha->ipha_dst & 0xffff)); 10928 if ((int)tcp->tcp_sum < 0) 10929 tcp->tcp_sum--; 10930 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 10931 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 10932 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 10933 bcopy(buf, tcp->tcp_tcph, tcph_len); 10934 tcp->tcp_ip_hdr_len = len; 10935 tcp->tcp_ipha->ipha_version_and_hdr_length = 10936 (IP_VERSION << 4) | (len >> 2); 10937 len += tcph_len; 10938 tcp->tcp_hdr_len = len; 10939 } 10940 10941 /* 10942 * Copy the standard header into its new location, 10943 * lay in the new options and then update the relevant 10944 * fields in both tcp_t and the standard header. 10945 */ 10946 static int 10947 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 10948 { 10949 uint_t tcph_len; 10950 uint8_t *ip_optp; 10951 tcph_t *new_tcph; 10952 10953 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 10954 return (EINVAL); 10955 10956 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 10957 return (EINVAL); 10958 10959 if (checkonly) { 10960 /* 10961 * do not really set, just pretend to - T_CHECK 10962 */ 10963 return (0); 10964 } 10965 10966 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 10967 if (tcp->tcp_label_len > 0) { 10968 int padlen; 10969 uint8_t opt; 10970 10971 /* convert list termination to no-ops */ 10972 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 10973 ip_optp += ip_optp[IPOPT_OLEN]; 10974 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 10975 while (--padlen >= 0) 10976 *ip_optp++ = opt; 10977 } 10978 tcph_len = tcp->tcp_tcp_hdr_len; 10979 new_tcph = (tcph_t *)(ip_optp + len); 10980 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 10981 tcp->tcp_tcph = new_tcph; 10982 bcopy(ptr, ip_optp, len); 10983 10984 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 10985 10986 tcp->tcp_ip_hdr_len = len; 10987 tcp->tcp_ipha->ipha_version_and_hdr_length = 10988 (IP_VERSION << 4) | (len >> 2); 10989 tcp->tcp_hdr_len = len + tcph_len; 10990 if (!TCP_IS_DETACHED(tcp)) { 10991 /* Always allocate room for all options. */ 10992 (void) mi_set_sth_wroff(tcp->tcp_rq, 10993 TCP_MAX_COMBINED_HEADER_LENGTH + tcp_wroff_xtra); 10994 } 10995 return (0); 10996 } 10997 10998 /* Get callback routine passed to nd_load by tcp_param_register */ 10999 /* ARGSUSED */ 11000 static int 11001 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11002 { 11003 tcpparam_t *tcppa = (tcpparam_t *)cp; 11004 11005 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11006 return (0); 11007 } 11008 11009 /* 11010 * Walk through the param array specified registering each element with the 11011 * named dispatch handler. 11012 */ 11013 static boolean_t 11014 tcp_param_register(tcpparam_t *tcppa, int cnt) 11015 { 11016 for (; cnt-- > 0; tcppa++) { 11017 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11018 if (!nd_load(&tcp_g_nd, tcppa->tcp_param_name, 11019 tcp_param_get, tcp_param_set, 11020 (caddr_t)tcppa)) { 11021 nd_free(&tcp_g_nd); 11022 return (B_FALSE); 11023 } 11024 } 11025 } 11026 if (!nd_load(&tcp_g_nd, tcp_wroff_xtra_param.tcp_param_name, 11027 tcp_param_get, tcp_param_set_aligned, 11028 (caddr_t)&tcp_wroff_xtra_param)) { 11029 nd_free(&tcp_g_nd); 11030 return (B_FALSE); 11031 } 11032 if (!nd_load(&tcp_g_nd, tcp_mdt_head_param.tcp_param_name, 11033 tcp_param_get, tcp_param_set_aligned, 11034 (caddr_t)&tcp_mdt_head_param)) { 11035 nd_free(&tcp_g_nd); 11036 return (B_FALSE); 11037 } 11038 if (!nd_load(&tcp_g_nd, tcp_mdt_tail_param.tcp_param_name, 11039 tcp_param_get, tcp_param_set_aligned, 11040 (caddr_t)&tcp_mdt_tail_param)) { 11041 nd_free(&tcp_g_nd); 11042 return (B_FALSE); 11043 } 11044 if (!nd_load(&tcp_g_nd, tcp_mdt_max_pbufs_param.tcp_param_name, 11045 tcp_param_get, tcp_param_set, 11046 (caddr_t)&tcp_mdt_max_pbufs_param)) { 11047 nd_free(&tcp_g_nd); 11048 return (B_FALSE); 11049 } 11050 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports", 11051 tcp_extra_priv_ports_get, NULL, NULL)) { 11052 nd_free(&tcp_g_nd); 11053 return (B_FALSE); 11054 } 11055 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports_add", 11056 NULL, tcp_extra_priv_ports_add, NULL)) { 11057 nd_free(&tcp_g_nd); 11058 return (B_FALSE); 11059 } 11060 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports_del", 11061 NULL, tcp_extra_priv_ports_del, NULL)) { 11062 nd_free(&tcp_g_nd); 11063 return (B_FALSE); 11064 } 11065 if (!nd_load(&tcp_g_nd, "tcp_status", tcp_status_report, NULL, 11066 NULL)) { 11067 nd_free(&tcp_g_nd); 11068 return (B_FALSE); 11069 } 11070 if (!nd_load(&tcp_g_nd, "tcp_bind_hash", tcp_bind_hash_report, 11071 NULL, NULL)) { 11072 nd_free(&tcp_g_nd); 11073 return (B_FALSE); 11074 } 11075 if (!nd_load(&tcp_g_nd, "tcp_listen_hash", tcp_listen_hash_report, 11076 NULL, NULL)) { 11077 nd_free(&tcp_g_nd); 11078 return (B_FALSE); 11079 } 11080 if (!nd_load(&tcp_g_nd, "tcp_conn_hash", tcp_conn_hash_report, 11081 NULL, NULL)) { 11082 nd_free(&tcp_g_nd); 11083 return (B_FALSE); 11084 } 11085 if (!nd_load(&tcp_g_nd, "tcp_acceptor_hash", tcp_acceptor_hash_report, 11086 NULL, NULL)) { 11087 nd_free(&tcp_g_nd); 11088 return (B_FALSE); 11089 } 11090 if (!nd_load(&tcp_g_nd, "tcp_host_param", tcp_host_param_report, 11091 tcp_host_param_set, NULL)) { 11092 nd_free(&tcp_g_nd); 11093 return (B_FALSE); 11094 } 11095 if (!nd_load(&tcp_g_nd, "tcp_host_param_ipv6", tcp_host_param_report, 11096 tcp_host_param_set_ipv6, NULL)) { 11097 nd_free(&tcp_g_nd); 11098 return (B_FALSE); 11099 } 11100 if (!nd_load(&tcp_g_nd, "tcp_1948_phrase", NULL, tcp_1948_phrase_set, 11101 NULL)) { 11102 nd_free(&tcp_g_nd); 11103 return (B_FALSE); 11104 } 11105 if (!nd_load(&tcp_g_nd, "tcp_reserved_port_list", 11106 tcp_reserved_port_list, NULL, NULL)) { 11107 nd_free(&tcp_g_nd); 11108 return (B_FALSE); 11109 } 11110 /* 11111 * Dummy ndd variables - only to convey obsolescence information 11112 * through printing of their name (no get or set routines) 11113 * XXX Remove in future releases ? 11114 */ 11115 if (!nd_load(&tcp_g_nd, 11116 "tcp_close_wait_interval(obsoleted - " 11117 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11118 nd_free(&tcp_g_nd); 11119 return (B_FALSE); 11120 } 11121 return (B_TRUE); 11122 } 11123 11124 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11125 /* ARGSUSED */ 11126 static int 11127 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11128 cred_t *cr) 11129 { 11130 long new_value; 11131 tcpparam_t *tcppa = (tcpparam_t *)cp; 11132 11133 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11134 new_value < tcppa->tcp_param_min || 11135 new_value > tcppa->tcp_param_max) { 11136 return (EINVAL); 11137 } 11138 /* 11139 * Need to make sure new_value is a multiple of 4. If it is not, 11140 * round it up. For future 64 bit requirement, we actually make it 11141 * a multiple of 8. 11142 */ 11143 if (new_value & 0x7) { 11144 new_value = (new_value & ~0x7) + 0x8; 11145 } 11146 tcppa->tcp_param_val = new_value; 11147 return (0); 11148 } 11149 11150 /* Set callback routine passed to nd_load by tcp_param_register */ 11151 /* ARGSUSED */ 11152 static int 11153 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11154 { 11155 long new_value; 11156 tcpparam_t *tcppa = (tcpparam_t *)cp; 11157 11158 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11159 new_value < tcppa->tcp_param_min || 11160 new_value > tcppa->tcp_param_max) { 11161 return (EINVAL); 11162 } 11163 tcppa->tcp_param_val = new_value; 11164 return (0); 11165 } 11166 11167 /* 11168 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11169 * is filled, return as much as we can. The message passed in may be 11170 * multi-part, chained using b_cont. "start" is the starting sequence 11171 * number for this piece. 11172 */ 11173 static mblk_t * 11174 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11175 { 11176 uint32_t end; 11177 mblk_t *mp1; 11178 mblk_t *mp2; 11179 mblk_t *next_mp; 11180 uint32_t u1; 11181 11182 /* Walk through all the new pieces. */ 11183 do { 11184 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11185 (uintptr_t)INT_MAX); 11186 end = start + (int)(mp->b_wptr - mp->b_rptr); 11187 next_mp = mp->b_cont; 11188 if (start == end) { 11189 /* Empty. Blast it. */ 11190 freeb(mp); 11191 continue; 11192 } 11193 mp->b_cont = NULL; 11194 TCP_REASS_SET_SEQ(mp, start); 11195 TCP_REASS_SET_END(mp, end); 11196 mp1 = tcp->tcp_reass_tail; 11197 if (!mp1) { 11198 tcp->tcp_reass_tail = mp; 11199 tcp->tcp_reass_head = mp; 11200 BUMP_MIB(&tcp_mib, tcpInDataUnorderSegs); 11201 UPDATE_MIB(&tcp_mib, 11202 tcpInDataUnorderBytes, end - start); 11203 continue; 11204 } 11205 /* New stuff completely beyond tail? */ 11206 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11207 /* Link it on end. */ 11208 mp1->b_cont = mp; 11209 tcp->tcp_reass_tail = mp; 11210 BUMP_MIB(&tcp_mib, tcpInDataUnorderSegs); 11211 UPDATE_MIB(&tcp_mib, 11212 tcpInDataUnorderBytes, end - start); 11213 continue; 11214 } 11215 mp1 = tcp->tcp_reass_head; 11216 u1 = TCP_REASS_SEQ(mp1); 11217 /* New stuff at the front? */ 11218 if (SEQ_LT(start, u1)) { 11219 /* Yes... Check for overlap. */ 11220 mp->b_cont = mp1; 11221 tcp->tcp_reass_head = mp; 11222 tcp_reass_elim_overlap(tcp, mp); 11223 continue; 11224 } 11225 /* 11226 * The new piece fits somewhere between the head and tail. 11227 * We find our slot, where mp1 precedes us and mp2 trails. 11228 */ 11229 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11230 u1 = TCP_REASS_SEQ(mp2); 11231 if (SEQ_LEQ(start, u1)) 11232 break; 11233 } 11234 /* Link ourselves in */ 11235 mp->b_cont = mp2; 11236 mp1->b_cont = mp; 11237 11238 /* Trim overlap with following mblk(s) first */ 11239 tcp_reass_elim_overlap(tcp, mp); 11240 11241 /* Trim overlap with preceding mblk */ 11242 tcp_reass_elim_overlap(tcp, mp1); 11243 11244 } while (start = end, mp = next_mp); 11245 mp1 = tcp->tcp_reass_head; 11246 /* Anything ready to go? */ 11247 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11248 return (NULL); 11249 /* Eat what we can off the queue */ 11250 for (;;) { 11251 mp = mp1->b_cont; 11252 end = TCP_REASS_END(mp1); 11253 TCP_REASS_SET_SEQ(mp1, 0); 11254 TCP_REASS_SET_END(mp1, 0); 11255 if (!mp) { 11256 tcp->tcp_reass_tail = NULL; 11257 break; 11258 } 11259 if (end != TCP_REASS_SEQ(mp)) { 11260 mp1->b_cont = NULL; 11261 break; 11262 } 11263 mp1 = mp; 11264 } 11265 mp1 = tcp->tcp_reass_head; 11266 tcp->tcp_reass_head = mp; 11267 return (mp1); 11268 } 11269 11270 /* Eliminate any overlap that mp may have over later mblks */ 11271 static void 11272 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11273 { 11274 uint32_t end; 11275 mblk_t *mp1; 11276 uint32_t u1; 11277 11278 end = TCP_REASS_END(mp); 11279 while ((mp1 = mp->b_cont) != NULL) { 11280 u1 = TCP_REASS_SEQ(mp1); 11281 if (!SEQ_GT(end, u1)) 11282 break; 11283 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11284 mp->b_wptr -= end - u1; 11285 TCP_REASS_SET_END(mp, u1); 11286 BUMP_MIB(&tcp_mib, tcpInDataPartDupSegs); 11287 UPDATE_MIB(&tcp_mib, tcpInDataPartDupBytes, end - u1); 11288 break; 11289 } 11290 mp->b_cont = mp1->b_cont; 11291 TCP_REASS_SET_SEQ(mp1, 0); 11292 TCP_REASS_SET_END(mp1, 0); 11293 freeb(mp1); 11294 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 11295 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, end - u1); 11296 } 11297 if (!mp1) 11298 tcp->tcp_reass_tail = mp; 11299 } 11300 11301 /* 11302 * Send up all messages queued on tcp_rcv_list. 11303 */ 11304 static uint_t 11305 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11306 { 11307 mblk_t *mp; 11308 uint_t ret = 0; 11309 uint_t thwin; 11310 #ifdef DEBUG 11311 uint_t cnt = 0; 11312 #endif 11313 /* Can't drain on an eager connection */ 11314 if (tcp->tcp_listener != NULL) 11315 return (ret); 11316 11317 /* 11318 * Handle two cases here: we are currently fused or we were 11319 * previously fused and have some urgent data to be delivered 11320 * upstream. The latter happens because we either ran out of 11321 * memory or were detached and therefore sending the SIGURG was 11322 * deferred until this point. In either case we pass control 11323 * over to tcp_fuse_rcv_drain() since it may need to complete 11324 * some work. 11325 */ 11326 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11327 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11328 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11329 &tcp->tcp_fused_sigurg_mp)) 11330 return (ret); 11331 } 11332 11333 while ((mp = tcp->tcp_rcv_list) != NULL) { 11334 tcp->tcp_rcv_list = mp->b_next; 11335 mp->b_next = NULL; 11336 #ifdef DEBUG 11337 cnt += msgdsize(mp); 11338 #endif 11339 /* Does this need SSL processing first? */ 11340 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11341 tcp_kssl_input(tcp, mp); 11342 continue; 11343 } 11344 putnext(q, mp); 11345 } 11346 ASSERT(cnt == tcp->tcp_rcv_cnt); 11347 tcp->tcp_rcv_last_head = NULL; 11348 tcp->tcp_rcv_last_tail = NULL; 11349 tcp->tcp_rcv_cnt = 0; 11350 11351 /* Learn the latest rwnd information that we sent to the other side. */ 11352 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11353 << tcp->tcp_rcv_ws; 11354 /* This is peer's calculated send window (our receive window). */ 11355 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11356 /* 11357 * Increase the receive window to max. But we need to do receiver 11358 * SWS avoidance. This means that we need to check the increase of 11359 * of receive window is at least 1 MSS. 11360 */ 11361 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11362 /* 11363 * If the window that the other side knows is less than max 11364 * deferred acks segments, send an update immediately. 11365 */ 11366 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11367 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 11368 ret = TH_ACK_NEEDED; 11369 } 11370 tcp->tcp_rwnd = q->q_hiwat; 11371 } 11372 /* No need for the push timer now. */ 11373 if (tcp->tcp_push_tid != 0) { 11374 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11375 tcp->tcp_push_tid = 0; 11376 } 11377 return (ret); 11378 } 11379 11380 /* 11381 * Queue data on tcp_rcv_list which is a b_next chain. 11382 * tcp_rcv_last_head/tail is the last element of this chain. 11383 * Each element of the chain is a b_cont chain. 11384 * 11385 * M_DATA messages are added to the current element. 11386 * Other messages are added as new (b_next) elements. 11387 */ 11388 void 11389 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11390 { 11391 ASSERT(seg_len == msgdsize(mp)); 11392 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11393 11394 if (tcp->tcp_rcv_list == NULL) { 11395 ASSERT(tcp->tcp_rcv_last_head == NULL); 11396 tcp->tcp_rcv_list = mp; 11397 tcp->tcp_rcv_last_head = mp; 11398 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11399 tcp->tcp_rcv_last_tail->b_cont = mp; 11400 } else { 11401 tcp->tcp_rcv_last_head->b_next = mp; 11402 tcp->tcp_rcv_last_head = mp; 11403 } 11404 11405 while (mp->b_cont) 11406 mp = mp->b_cont; 11407 11408 tcp->tcp_rcv_last_tail = mp; 11409 tcp->tcp_rcv_cnt += seg_len; 11410 tcp->tcp_rwnd -= seg_len; 11411 } 11412 11413 /* 11414 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11415 * 11416 * This is the default entry function into TCP on the read side. TCP is 11417 * always entered via squeue i.e. using squeue's for mutual exclusion. 11418 * When classifier does a lookup to find the tcp, it also puts a reference 11419 * on the conn structure associated so the tcp is guaranteed to exist 11420 * when we come here. We still need to check the state because it might 11421 * as well has been closed. The squeue processing function i.e. squeue_enter, 11422 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 11423 * CONN_DEC_REF. 11424 * 11425 * Apart from the default entry point, IP also sends packets directly to 11426 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11427 * connections. 11428 */ 11429 void 11430 tcp_input(void *arg, mblk_t *mp, void *arg2) 11431 { 11432 conn_t *connp = (conn_t *)arg; 11433 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11434 11435 /* arg2 is the sqp */ 11436 ASSERT(arg2 != NULL); 11437 ASSERT(mp != NULL); 11438 11439 /* 11440 * Don't accept any input on a closed tcp as this TCP logically does 11441 * not exist on the system. Don't proceed further with this TCP. 11442 * For eg. this packet could trigger another close of this tcp 11443 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11444 * tcp_clean_death / tcp_closei_local must be called at most once 11445 * on a TCP. In this case we need to refeed the packet into the 11446 * classifier and figure out where the packet should go. Need to 11447 * preserve the recv_ill somehow. Until we figure that out, for 11448 * now just drop the packet if we can't classify the packet. 11449 */ 11450 if (tcp->tcp_state == TCPS_CLOSED || 11451 tcp->tcp_state == TCPS_BOUND) { 11452 conn_t *new_connp; 11453 11454 new_connp = ipcl_classify(mp, connp->conn_zoneid); 11455 if (new_connp != NULL) { 11456 tcp_reinput(new_connp, mp, arg2); 11457 return; 11458 } 11459 /* We failed to classify. For now just drop the packet */ 11460 freemsg(mp); 11461 return; 11462 } 11463 11464 if (DB_TYPE(mp) == M_DATA) 11465 tcp_rput_data(connp, mp, arg2); 11466 else 11467 tcp_rput_common(tcp, mp); 11468 } 11469 11470 /* 11471 * The read side put procedure. 11472 * The packets passed up by ip are assume to be aligned according to 11473 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11474 */ 11475 static void 11476 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11477 { 11478 /* 11479 * tcp_rput_data() does not expect M_CTL except for the case 11480 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11481 * type. Need to make sure that any other M_CTLs don't make 11482 * it to tcp_rput_data since it is not expecting any and doesn't 11483 * check for it. 11484 */ 11485 if (DB_TYPE(mp) == M_CTL) { 11486 switch (*(uint32_t *)(mp->b_rptr)) { 11487 case TCP_IOC_ABORT_CONN: 11488 /* 11489 * Handle connection abort request. 11490 */ 11491 tcp_ioctl_abort_handler(tcp, mp); 11492 return; 11493 case IPSEC_IN: 11494 /* 11495 * Only secure icmp arrive in TCP and they 11496 * don't go through data path. 11497 */ 11498 tcp_icmp_error(tcp, mp); 11499 return; 11500 case IN_PKTINFO: 11501 /* 11502 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11503 * sockets that are receiving IPv4 traffic. tcp 11504 */ 11505 ASSERT(tcp->tcp_family == AF_INET6); 11506 ASSERT(tcp->tcp_ipv6_recvancillary & 11507 TCP_IPV6_RECVPKTINFO); 11508 tcp_rput_data(tcp->tcp_connp, mp, 11509 tcp->tcp_connp->conn_sqp); 11510 return; 11511 case MDT_IOC_INFO_UPDATE: 11512 /* 11513 * Handle Multidata information update; the 11514 * following routine will free the message. 11515 */ 11516 if (tcp->tcp_connp->conn_mdt_ok) { 11517 tcp_mdt_update(tcp, 11518 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11519 B_FALSE); 11520 } 11521 freemsg(mp); 11522 return; 11523 default: 11524 break; 11525 } 11526 } 11527 11528 /* No point processing the message if tcp is already closed */ 11529 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11530 freemsg(mp); 11531 return; 11532 } 11533 11534 tcp_rput_other(tcp, mp); 11535 } 11536 11537 11538 /* The minimum of smoothed mean deviation in RTO calculation. */ 11539 #define TCP_SD_MIN 400 11540 11541 /* 11542 * Set RTO for this connection. The formula is from Jacobson and Karels' 11543 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11544 * are the same as those in Appendix A.2 of that paper. 11545 * 11546 * m = new measurement 11547 * sa = smoothed RTT average (8 * average estimates). 11548 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11549 */ 11550 static void 11551 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11552 { 11553 long m = TICK_TO_MSEC(rtt); 11554 clock_t sa = tcp->tcp_rtt_sa; 11555 clock_t sv = tcp->tcp_rtt_sd; 11556 clock_t rto; 11557 11558 BUMP_MIB(&tcp_mib, tcpRttUpdate); 11559 tcp->tcp_rtt_update++; 11560 11561 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11562 if (sa != 0) { 11563 /* 11564 * Update average estimator: 11565 * new rtt = 7/8 old rtt + 1/8 Error 11566 */ 11567 11568 /* m is now Error in estimate. */ 11569 m -= sa >> 3; 11570 if ((sa += m) <= 0) { 11571 /* 11572 * Don't allow the smoothed average to be negative. 11573 * We use 0 to denote reinitialization of the 11574 * variables. 11575 */ 11576 sa = 1; 11577 } 11578 11579 /* 11580 * Update deviation estimator: 11581 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11582 */ 11583 if (m < 0) 11584 m = -m; 11585 m -= sv >> 2; 11586 sv += m; 11587 } else { 11588 /* 11589 * This follows BSD's implementation. So the reinitialized 11590 * RTO is 3 * m. We cannot go less than 2 because if the 11591 * link is bandwidth dominated, doubling the window size 11592 * during slow start means doubling the RTT. We want to be 11593 * more conservative when we reinitialize our estimates. 3 11594 * is just a convenient number. 11595 */ 11596 sa = m << 3; 11597 sv = m << 1; 11598 } 11599 if (sv < TCP_SD_MIN) { 11600 /* 11601 * We do not know that if sa captures the delay ACK 11602 * effect as in a long train of segments, a receiver 11603 * does not delay its ACKs. So set the minimum of sv 11604 * to be TCP_SD_MIN, which is default to 400 ms, twice 11605 * of BSD DATO. That means the minimum of mean 11606 * deviation is 100 ms. 11607 * 11608 */ 11609 sv = TCP_SD_MIN; 11610 } 11611 tcp->tcp_rtt_sa = sa; 11612 tcp->tcp_rtt_sd = sv; 11613 /* 11614 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11615 * 11616 * Add tcp_rexmit_interval extra in case of extreme environment 11617 * where the algorithm fails to work. The default value of 11618 * tcp_rexmit_interval_extra should be 0. 11619 * 11620 * As we use a finer grained clock than BSD and update 11621 * RTO for every ACKs, add in another .25 of RTT to the 11622 * deviation of RTO to accomodate burstiness of 1/4 of 11623 * window size. 11624 */ 11625 rto = (sa >> 3) + sv + tcp_rexmit_interval_extra + (sa >> 5); 11626 11627 if (rto > tcp_rexmit_interval_max) { 11628 tcp->tcp_rto = tcp_rexmit_interval_max; 11629 } else if (rto < tcp_rexmit_interval_min) { 11630 tcp->tcp_rto = tcp_rexmit_interval_min; 11631 } else { 11632 tcp->tcp_rto = rto; 11633 } 11634 11635 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11636 tcp->tcp_timer_backoff = 0; 11637 } 11638 11639 /* 11640 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11641 * send queue which starts at the given seq. no. 11642 * 11643 * Parameters: 11644 * tcp_t *tcp: the tcp instance pointer. 11645 * uint32_t seq: the starting seq. no of the requested segment. 11646 * int32_t *off: after the execution, *off will be the offset to 11647 * the returned mblk which points to the requested seq no. 11648 * It is the caller's responsibility to send in a non-null off. 11649 * 11650 * Return: 11651 * A mblk_t pointer pointing to the requested segment in send queue. 11652 */ 11653 static mblk_t * 11654 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11655 { 11656 int32_t cnt; 11657 mblk_t *mp; 11658 11659 /* Defensive coding. Make sure we don't send incorrect data. */ 11660 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 11661 return (NULL); 11662 11663 cnt = seq - tcp->tcp_suna; 11664 mp = tcp->tcp_xmit_head; 11665 while (cnt > 0 && mp != NULL) { 11666 cnt -= mp->b_wptr - mp->b_rptr; 11667 if (cnt < 0) { 11668 cnt += mp->b_wptr - mp->b_rptr; 11669 break; 11670 } 11671 mp = mp->b_cont; 11672 } 11673 ASSERT(mp != NULL); 11674 *off = cnt; 11675 return (mp); 11676 } 11677 11678 /* 11679 * This function handles all retransmissions if SACK is enabled for this 11680 * connection. First it calculates how many segments can be retransmitted 11681 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11682 * segments. A segment is eligible if sack_cnt for that segment is greater 11683 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11684 * all eligible segments, it checks to see if TCP can send some new segments 11685 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11686 * 11687 * Parameters: 11688 * tcp_t *tcp: the tcp structure of the connection. 11689 * uint_t *flags: in return, appropriate value will be set for 11690 * tcp_rput_data(). 11691 */ 11692 static void 11693 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11694 { 11695 notsack_blk_t *notsack_blk; 11696 int32_t usable_swnd; 11697 int32_t mss; 11698 uint32_t seg_len; 11699 mblk_t *xmit_mp; 11700 11701 ASSERT(tcp->tcp_sack_info != NULL); 11702 ASSERT(tcp->tcp_notsack_list != NULL); 11703 ASSERT(tcp->tcp_rexmit == B_FALSE); 11704 11705 /* Defensive coding in case there is a bug... */ 11706 if (tcp->tcp_notsack_list == NULL) { 11707 return; 11708 } 11709 notsack_blk = tcp->tcp_notsack_list; 11710 mss = tcp->tcp_mss; 11711 11712 /* 11713 * Limit the num of outstanding data in the network to be 11714 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11715 */ 11716 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11717 11718 /* At least retransmit 1 MSS of data. */ 11719 if (usable_swnd <= 0) { 11720 usable_swnd = mss; 11721 } 11722 11723 /* Make sure no new RTT samples will be taken. */ 11724 tcp->tcp_csuna = tcp->tcp_snxt; 11725 11726 notsack_blk = tcp->tcp_notsack_list; 11727 while (usable_swnd > 0) { 11728 mblk_t *snxt_mp, *tmp_mp; 11729 tcp_seq begin = tcp->tcp_sack_snxt; 11730 tcp_seq end; 11731 int32_t off; 11732 11733 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11734 if (SEQ_GT(notsack_blk->end, begin) && 11735 (notsack_blk->sack_cnt >= 11736 tcp_dupack_fast_retransmit)) { 11737 end = notsack_blk->end; 11738 if (SEQ_LT(begin, notsack_blk->begin)) { 11739 begin = notsack_blk->begin; 11740 } 11741 break; 11742 } 11743 } 11744 /* 11745 * All holes are filled. Manipulate tcp_cwnd to send more 11746 * if we can. Note that after the SACK recovery, tcp_cwnd is 11747 * set to tcp_cwnd_ssthresh. 11748 */ 11749 if (notsack_blk == NULL) { 11750 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11751 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11752 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11753 ASSERT(tcp->tcp_cwnd > 0); 11754 return; 11755 } else { 11756 usable_swnd = usable_swnd / mss; 11757 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 11758 MAX(usable_swnd * mss, mss); 11759 *flags |= TH_XMIT_NEEDED; 11760 return; 11761 } 11762 } 11763 11764 /* 11765 * Note that we may send more than usable_swnd allows here 11766 * because of round off, but no more than 1 MSS of data. 11767 */ 11768 seg_len = end - begin; 11769 if (seg_len > mss) 11770 seg_len = mss; 11771 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 11772 ASSERT(snxt_mp != NULL); 11773 /* This should not happen. Defensive coding again... */ 11774 if (snxt_mp == NULL) { 11775 return; 11776 } 11777 11778 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 11779 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 11780 if (xmit_mp == NULL) 11781 return; 11782 11783 usable_swnd -= seg_len; 11784 tcp->tcp_pipe += seg_len; 11785 tcp->tcp_sack_snxt = begin + seg_len; 11786 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 11787 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11788 11789 /* 11790 * Update the send timestamp to avoid false retransmission. 11791 */ 11792 snxt_mp->b_prev = (mblk_t *)lbolt; 11793 11794 BUMP_MIB(&tcp_mib, tcpRetransSegs); 11795 UPDATE_MIB(&tcp_mib, tcpRetransBytes, seg_len); 11796 BUMP_MIB(&tcp_mib, tcpOutSackRetransSegs); 11797 /* 11798 * Update tcp_rexmit_max to extend this SACK recovery phase. 11799 * This happens when new data sent during fast recovery is 11800 * also lost. If TCP retransmits those new data, it needs 11801 * to extend SACK recover phase to avoid starting another 11802 * fast retransmit/recovery unnecessarily. 11803 */ 11804 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 11805 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 11806 } 11807 } 11808 } 11809 11810 /* 11811 * This function handles policy checking at TCP level for non-hard_bound/ 11812 * detached connections. 11813 */ 11814 static boolean_t 11815 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 11816 boolean_t secure, boolean_t mctl_present) 11817 { 11818 ipsec_latch_t *ipl = NULL; 11819 ipsec_action_t *act = NULL; 11820 mblk_t *data_mp; 11821 ipsec_in_t *ii; 11822 const char *reason; 11823 kstat_named_t *counter; 11824 11825 ASSERT(mctl_present || !secure); 11826 11827 ASSERT((ipha == NULL && ip6h != NULL) || 11828 (ip6h == NULL && ipha != NULL)); 11829 11830 /* 11831 * We don't necessarily have an ipsec_in_act action to verify 11832 * policy because of assymetrical policy where we have only 11833 * outbound policy and no inbound policy (possible with global 11834 * policy). 11835 */ 11836 if (!secure) { 11837 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 11838 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 11839 return (B_TRUE); 11840 ipsec_log_policy_failure(tcp->tcp_wq, IPSEC_POLICY_MISMATCH, 11841 "tcp_check_policy", ipha, ip6h, secure); 11842 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11843 &ipdrops_tcp_clear, &tcp_dropper); 11844 return (B_FALSE); 11845 } 11846 11847 /* 11848 * We have a secure packet. 11849 */ 11850 if (act == NULL) { 11851 ipsec_log_policy_failure(tcp->tcp_wq, 11852 IPSEC_POLICY_NOT_NEEDED, "tcp_check_policy", ipha, ip6h, 11853 secure); 11854 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11855 &ipdrops_tcp_secure, &tcp_dropper); 11856 return (B_FALSE); 11857 } 11858 11859 /* 11860 * XXX This whole routine is currently incorrect. ipl should 11861 * be set to the latch pointer, but is currently not set, so 11862 * we initialize it to NULL to avoid picking up random garbage. 11863 */ 11864 if (ipl == NULL) 11865 return (B_TRUE); 11866 11867 data_mp = first_mp->b_cont; 11868 11869 ii = (ipsec_in_t *)first_mp->b_rptr; 11870 11871 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 11872 &counter)) { 11873 BUMP_MIB(&ip_mib, ipsecInSucceeded); 11874 return (B_TRUE); 11875 } 11876 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 11877 "tcp inbound policy mismatch: %s, packet dropped\n", 11878 reason); 11879 BUMP_MIB(&ip_mib, ipsecInFailed); 11880 11881 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, &tcp_dropper); 11882 return (B_FALSE); 11883 } 11884 11885 /* 11886 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 11887 * retransmission after a timeout. 11888 * 11889 * To limit the number of duplicate segments, we limit the number of segment 11890 * to be sent in one time to tcp_snd_burst, the burst variable. 11891 */ 11892 static void 11893 tcp_ss_rexmit(tcp_t *tcp) 11894 { 11895 uint32_t snxt; 11896 uint32_t smax; 11897 int32_t win; 11898 int32_t mss; 11899 int32_t off; 11900 int32_t burst = tcp->tcp_snd_burst; 11901 mblk_t *snxt_mp; 11902 11903 /* 11904 * Note that tcp_rexmit can be set even though TCP has retransmitted 11905 * all unack'ed segments. 11906 */ 11907 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 11908 smax = tcp->tcp_rexmit_max; 11909 snxt = tcp->tcp_rexmit_nxt; 11910 if (SEQ_LT(snxt, tcp->tcp_suna)) { 11911 snxt = tcp->tcp_suna; 11912 } 11913 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 11914 win -= snxt - tcp->tcp_suna; 11915 mss = tcp->tcp_mss; 11916 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 11917 11918 while (SEQ_LT(snxt, smax) && (win > 0) && 11919 (burst > 0) && (snxt_mp != NULL)) { 11920 mblk_t *xmit_mp; 11921 mblk_t *old_snxt_mp = snxt_mp; 11922 uint32_t cnt = mss; 11923 11924 if (win < cnt) { 11925 cnt = win; 11926 } 11927 if (SEQ_GT(snxt + cnt, smax)) { 11928 cnt = smax - snxt; 11929 } 11930 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 11931 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 11932 if (xmit_mp == NULL) 11933 return; 11934 11935 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11936 11937 snxt += cnt; 11938 win -= cnt; 11939 /* 11940 * Update the send timestamp to avoid false 11941 * retransmission. 11942 */ 11943 old_snxt_mp->b_prev = (mblk_t *)lbolt; 11944 BUMP_MIB(&tcp_mib, tcpRetransSegs); 11945 UPDATE_MIB(&tcp_mib, tcpRetransBytes, cnt); 11946 11947 tcp->tcp_rexmit_nxt = snxt; 11948 burst--; 11949 } 11950 /* 11951 * If we have transmitted all we have at the time 11952 * we started the retranmission, we can leave 11953 * the rest of the job to tcp_wput_data(). But we 11954 * need to check the send window first. If the 11955 * win is not 0, go on with tcp_wput_data(). 11956 */ 11957 if (SEQ_LT(snxt, smax) || win == 0) { 11958 return; 11959 } 11960 } 11961 /* Only call tcp_wput_data() if there is data to be sent. */ 11962 if (tcp->tcp_unsent) { 11963 tcp_wput_data(tcp, NULL, B_FALSE); 11964 } 11965 } 11966 11967 /* 11968 * Process all TCP option in SYN segment. Note that this function should 11969 * be called after tcp_adapt_ire() is called so that the necessary info 11970 * from IRE is already set in the tcp structure. 11971 * 11972 * This function sets up the correct tcp_mss value according to the 11973 * MSS option value and our header size. It also sets up the window scale 11974 * and timestamp values, and initialize SACK info blocks. But it does not 11975 * change receive window size after setting the tcp_mss value. The caller 11976 * should do the appropriate change. 11977 */ 11978 void 11979 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 11980 { 11981 int options; 11982 tcp_opt_t tcpopt; 11983 uint32_t mss_max; 11984 char *tmp_tcph; 11985 11986 tcpopt.tcp = NULL; 11987 options = tcp_parse_options(tcph, &tcpopt); 11988 11989 /* 11990 * Process MSS option. Note that MSS option value does not account 11991 * for IP or TCP options. This means that it is equal to MTU - minimum 11992 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 11993 * IPv6. 11994 */ 11995 if (!(options & TCP_OPT_MSS_PRESENT)) { 11996 if (tcp->tcp_ipversion == IPV4_VERSION) 11997 tcpopt.tcp_opt_mss = tcp_mss_def_ipv4; 11998 else 11999 tcpopt.tcp_opt_mss = tcp_mss_def_ipv6; 12000 } else { 12001 if (tcp->tcp_ipversion == IPV4_VERSION) 12002 mss_max = tcp_mss_max_ipv4; 12003 else 12004 mss_max = tcp_mss_max_ipv6; 12005 if (tcpopt.tcp_opt_mss < tcp_mss_min) 12006 tcpopt.tcp_opt_mss = tcp_mss_min; 12007 else if (tcpopt.tcp_opt_mss > mss_max) 12008 tcpopt.tcp_opt_mss = mss_max; 12009 } 12010 12011 /* Process Window Scale option. */ 12012 if (options & TCP_OPT_WSCALE_PRESENT) { 12013 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12014 tcp->tcp_snd_ws_ok = B_TRUE; 12015 } else { 12016 tcp->tcp_snd_ws = B_FALSE; 12017 tcp->tcp_snd_ws_ok = B_FALSE; 12018 tcp->tcp_rcv_ws = B_FALSE; 12019 } 12020 12021 /* Process Timestamp option. */ 12022 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12023 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12024 tmp_tcph = (char *)tcp->tcp_tcph; 12025 12026 tcp->tcp_snd_ts_ok = B_TRUE; 12027 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12028 tcp->tcp_last_rcv_lbolt = lbolt64; 12029 ASSERT(OK_32PTR(tmp_tcph)); 12030 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12031 12032 /* Fill in our template header with basic timestamp option. */ 12033 tmp_tcph += tcp->tcp_tcp_hdr_len; 12034 tmp_tcph[0] = TCPOPT_NOP; 12035 tmp_tcph[1] = TCPOPT_NOP; 12036 tmp_tcph[2] = TCPOPT_TSTAMP; 12037 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12038 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12039 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12040 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12041 } else { 12042 tcp->tcp_snd_ts_ok = B_FALSE; 12043 } 12044 12045 /* 12046 * Process SACK options. If SACK is enabled for this connection, 12047 * then allocate the SACK info structure. Note the following ways 12048 * when tcp_snd_sack_ok is set to true. 12049 * 12050 * For active connection: in tcp_adapt_ire() called in 12051 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12052 * is checked. 12053 * 12054 * For passive connection: in tcp_adapt_ire() called in 12055 * tcp_accept_comm(). 12056 * 12057 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12058 * That check makes sure that if we did not send a SACK OK option, 12059 * we will not enable SACK for this connection even though the other 12060 * side sends us SACK OK option. For active connection, the SACK 12061 * info structure has already been allocated. So we need to free 12062 * it if SACK is disabled. 12063 */ 12064 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12065 (tcp->tcp_snd_sack_ok || 12066 (tcp_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12067 /* This should be true only in the passive case. */ 12068 if (tcp->tcp_sack_info == NULL) { 12069 ASSERT(TCP_IS_DETACHED(tcp)); 12070 tcp->tcp_sack_info = 12071 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12072 } 12073 if (tcp->tcp_sack_info == NULL) { 12074 tcp->tcp_snd_sack_ok = B_FALSE; 12075 } else { 12076 tcp->tcp_snd_sack_ok = B_TRUE; 12077 if (tcp->tcp_snd_ts_ok) { 12078 tcp->tcp_max_sack_blk = 3; 12079 } else { 12080 tcp->tcp_max_sack_blk = 4; 12081 } 12082 } 12083 } else { 12084 /* 12085 * Resetting tcp_snd_sack_ok to B_FALSE so that 12086 * no SACK info will be used for this 12087 * connection. This assumes that SACK usage 12088 * permission is negotiated. This may need 12089 * to be changed once this is clarified. 12090 */ 12091 if (tcp->tcp_sack_info != NULL) { 12092 ASSERT(tcp->tcp_notsack_list == NULL); 12093 kmem_cache_free(tcp_sack_info_cache, 12094 tcp->tcp_sack_info); 12095 tcp->tcp_sack_info = NULL; 12096 } 12097 tcp->tcp_snd_sack_ok = B_FALSE; 12098 } 12099 12100 /* 12101 * Now we know the exact TCP/IP header length, subtract 12102 * that from tcp_mss to get our side's MSS. 12103 */ 12104 tcp->tcp_mss -= tcp->tcp_hdr_len; 12105 /* 12106 * Here we assume that the other side's header size will be equal to 12107 * our header size. We calculate the real MSS accordingly. Need to 12108 * take into additional stuffs IPsec puts in. 12109 * 12110 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12111 */ 12112 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12113 ((tcp->tcp_ipversion == IPV4_VERSION ? 12114 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12115 12116 /* 12117 * Set MSS to the smaller one of both ends of the connection. 12118 * We should not have called tcp_mss_set() before, but our 12119 * side of the MSS should have been set to a proper value 12120 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12121 * STREAM head parameters properly. 12122 * 12123 * If we have a larger-than-16-bit window but the other side 12124 * didn't want to do window scale, tcp_rwnd_set() will take 12125 * care of that. 12126 */ 12127 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss)); 12128 } 12129 12130 /* 12131 * Sends the T_CONN_IND to the listener. The caller calls this 12132 * functions via squeue to get inside the listener's perimeter 12133 * once the 3 way hand shake is done a T_CONN_IND needs to be 12134 * sent. As an optimization, the caller can call this directly 12135 * if listener's perimeter is same as eager's. 12136 */ 12137 /* ARGSUSED */ 12138 void 12139 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12140 { 12141 conn_t *lconnp = (conn_t *)arg; 12142 tcp_t *listener = lconnp->conn_tcp; 12143 tcp_t *tcp; 12144 struct T_conn_ind *conn_ind; 12145 ipaddr_t *addr_cache; 12146 boolean_t need_send_conn_ind = B_FALSE; 12147 12148 /* retrieve the eager */ 12149 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12150 ASSERT(conn_ind->OPT_offset != 0 && 12151 conn_ind->OPT_length == sizeof (intptr_t)); 12152 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12153 conn_ind->OPT_length); 12154 12155 /* 12156 * TLI/XTI applications will get confused by 12157 * sending eager as an option since it violates 12158 * the option semantics. So remove the eager as 12159 * option since TLI/XTI app doesn't need it anyway. 12160 */ 12161 if (!TCP_IS_SOCKET(listener)) { 12162 conn_ind->OPT_length = 0; 12163 conn_ind->OPT_offset = 0; 12164 } 12165 if (listener->tcp_state == TCPS_CLOSED || 12166 TCP_IS_DETACHED(listener)) { 12167 /* 12168 * If listener has closed, it would have caused a 12169 * a cleanup/blowoff to happen for the eager. We 12170 * just need to return. 12171 */ 12172 freemsg(mp); 12173 return; 12174 } 12175 12176 12177 /* 12178 * if the conn_req_q is full defer passing up the 12179 * T_CONN_IND until space is availabe after t_accept() 12180 * processing 12181 */ 12182 mutex_enter(&listener->tcp_eager_lock); 12183 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12184 tcp_t *tail; 12185 12186 /* 12187 * The eager already has an extra ref put in tcp_rput_data 12188 * so that it stays till accept comes back even though it 12189 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12190 */ 12191 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12192 listener->tcp_conn_req_cnt_q0--; 12193 listener->tcp_conn_req_cnt_q++; 12194 12195 /* Move from SYN_RCVD to ESTABLISHED list */ 12196 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12197 tcp->tcp_eager_prev_q0; 12198 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12199 tcp->tcp_eager_next_q0; 12200 tcp->tcp_eager_prev_q0 = NULL; 12201 tcp->tcp_eager_next_q0 = NULL; 12202 12203 /* 12204 * Insert at end of the queue because sockfs 12205 * sends down T_CONN_RES in chronological 12206 * order. Leaving the older conn indications 12207 * at front of the queue helps reducing search 12208 * time. 12209 */ 12210 tail = listener->tcp_eager_last_q; 12211 if (tail != NULL) 12212 tail->tcp_eager_next_q = tcp; 12213 else 12214 listener->tcp_eager_next_q = tcp; 12215 listener->tcp_eager_last_q = tcp; 12216 tcp->tcp_eager_next_q = NULL; 12217 /* 12218 * Delay sending up the T_conn_ind until we are 12219 * done with the eager. Once we have have sent up 12220 * the T_conn_ind, the accept can potentially complete 12221 * any time and release the refhold we have on the eager. 12222 */ 12223 need_send_conn_ind = B_TRUE; 12224 } else { 12225 /* 12226 * Defer connection on q0 and set deferred 12227 * connection bit true 12228 */ 12229 tcp->tcp_conn_def_q0 = B_TRUE; 12230 12231 /* take tcp out of q0 ... */ 12232 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12233 tcp->tcp_eager_next_q0; 12234 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12235 tcp->tcp_eager_prev_q0; 12236 12237 /* ... and place it at the end of q0 */ 12238 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12239 tcp->tcp_eager_next_q0 = listener; 12240 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12241 listener->tcp_eager_prev_q0 = tcp; 12242 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12243 } 12244 12245 /* we have timed out before */ 12246 if (tcp->tcp_syn_rcvd_timeout != 0) { 12247 tcp->tcp_syn_rcvd_timeout = 0; 12248 listener->tcp_syn_rcvd_timeout--; 12249 if (listener->tcp_syn_defense && 12250 listener->tcp_syn_rcvd_timeout <= 12251 (tcp_conn_req_max_q0 >> 5) && 12252 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12253 listener->tcp_last_rcv_lbolt)) { 12254 /* 12255 * Turn off the defense mode if we 12256 * believe the SYN attack is over. 12257 */ 12258 listener->tcp_syn_defense = B_FALSE; 12259 if (listener->tcp_ip_addr_cache) { 12260 kmem_free((void *)listener->tcp_ip_addr_cache, 12261 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12262 listener->tcp_ip_addr_cache = NULL; 12263 } 12264 } 12265 } 12266 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12267 if (addr_cache != NULL) { 12268 /* 12269 * We have finished a 3-way handshake with this 12270 * remote host. This proves the IP addr is good. 12271 * Cache it! 12272 */ 12273 addr_cache[IP_ADDR_CACHE_HASH( 12274 tcp->tcp_remote)] = tcp->tcp_remote; 12275 } 12276 mutex_exit(&listener->tcp_eager_lock); 12277 if (need_send_conn_ind) 12278 putnext(listener->tcp_rq, mp); 12279 } 12280 12281 mblk_t * 12282 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12283 uint_t *ifindexp, ip6_pkt_t *ippp) 12284 { 12285 in_pktinfo_t *pinfo; 12286 ip6_t *ip6h; 12287 uchar_t *rptr; 12288 mblk_t *first_mp = mp; 12289 boolean_t mctl_present = B_FALSE; 12290 uint_t ifindex = 0; 12291 ip6_pkt_t ipp; 12292 uint_t ipvers; 12293 uint_t ip_hdr_len; 12294 12295 rptr = mp->b_rptr; 12296 ASSERT(OK_32PTR(rptr)); 12297 ASSERT(tcp != NULL); 12298 ipp.ipp_fields = 0; 12299 12300 switch DB_TYPE(mp) { 12301 case M_CTL: 12302 mp = mp->b_cont; 12303 if (mp == NULL) { 12304 freemsg(first_mp); 12305 return (NULL); 12306 } 12307 if (DB_TYPE(mp) != M_DATA) { 12308 freemsg(first_mp); 12309 return (NULL); 12310 } 12311 mctl_present = B_TRUE; 12312 break; 12313 case M_DATA: 12314 break; 12315 default: 12316 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12317 freemsg(mp); 12318 return (NULL); 12319 } 12320 ipvers = IPH_HDR_VERSION(rptr); 12321 if (ipvers == IPV4_VERSION) { 12322 if (tcp == NULL) { 12323 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12324 goto done; 12325 } 12326 12327 ipp.ipp_fields |= IPPF_HOPLIMIT; 12328 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12329 12330 /* 12331 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12332 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12333 */ 12334 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12335 mctl_present) { 12336 pinfo = (in_pktinfo_t *)first_mp->b_rptr; 12337 if ((MBLKL(first_mp) == sizeof (in_pktinfo_t)) && 12338 (pinfo->in_pkt_ulp_type == IN_PKTINFO) && 12339 (pinfo->in_pkt_flags & IPF_RECVIF)) { 12340 ipp.ipp_fields |= IPPF_IFINDEX; 12341 ipp.ipp_ifindex = pinfo->in_pkt_ifindex; 12342 ifindex = pinfo->in_pkt_ifindex; 12343 } 12344 freeb(first_mp); 12345 mctl_present = B_FALSE; 12346 } 12347 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12348 } else { 12349 ip6h = (ip6_t *)rptr; 12350 12351 ASSERT(ipvers == IPV6_VERSION); 12352 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12353 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12354 ipp.ipp_hoplimit = ip6h->ip6_hops; 12355 12356 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12357 uint8_t nexthdrp; 12358 12359 /* Look for ifindex information */ 12360 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12361 ip6i_t *ip6i = (ip6i_t *)ip6h; 12362 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12363 BUMP_MIB(&ip_mib, tcpInErrs); 12364 freemsg(first_mp); 12365 return (NULL); 12366 } 12367 12368 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12369 ASSERT(ip6i->ip6i_ifindex != 0); 12370 ipp.ipp_fields |= IPPF_IFINDEX; 12371 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12372 ifindex = ip6i->ip6i_ifindex; 12373 } 12374 rptr = (uchar_t *)&ip6i[1]; 12375 mp->b_rptr = rptr; 12376 if (rptr == mp->b_wptr) { 12377 mblk_t *mp1; 12378 mp1 = mp->b_cont; 12379 freeb(mp); 12380 mp = mp1; 12381 rptr = mp->b_rptr; 12382 } 12383 if (MBLKL(mp) < IPV6_HDR_LEN + 12384 sizeof (tcph_t)) { 12385 BUMP_MIB(&ip_mib, tcpInErrs); 12386 freemsg(first_mp); 12387 return (NULL); 12388 } 12389 ip6h = (ip6_t *)rptr; 12390 } 12391 12392 /* 12393 * Find any potentially interesting extension headers 12394 * as well as the length of the IPv6 + extension 12395 * headers. 12396 */ 12397 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12398 /* Verify if this is a TCP packet */ 12399 if (nexthdrp != IPPROTO_TCP) { 12400 BUMP_MIB(&ip_mib, tcpInErrs); 12401 freemsg(first_mp); 12402 return (NULL); 12403 } 12404 } else { 12405 ip_hdr_len = IPV6_HDR_LEN; 12406 } 12407 } 12408 12409 done: 12410 if (ipversp != NULL) 12411 *ipversp = ipvers; 12412 if (ip_hdr_lenp != NULL) 12413 *ip_hdr_lenp = ip_hdr_len; 12414 if (ippp != NULL) 12415 *ippp = ipp; 12416 if (ifindexp != NULL) 12417 *ifindexp = ifindex; 12418 if (mctl_present) { 12419 freeb(first_mp); 12420 } 12421 return (mp); 12422 } 12423 12424 /* 12425 * Handle M_DATA messages from IP. Its called directly from IP via 12426 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12427 * in this path. 12428 * 12429 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12430 * v4 and v6), we are called through tcp_input() and a M_CTL can 12431 * be present for options but tcp_find_pktinfo() deals with it. We 12432 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12433 * 12434 * The first argument is always the connp/tcp to which the mp belongs. 12435 * There are no exceptions to this rule. The caller has already put 12436 * a reference on this connp/tcp and once tcp_rput_data() returns, 12437 * the squeue will do the refrele. 12438 * 12439 * The TH_SYN for the listener directly go to tcp_conn_request via 12440 * squeue. 12441 * 12442 * sqp: NULL = recursive, sqp != NULL means called from squeue 12443 */ 12444 void 12445 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12446 { 12447 int32_t bytes_acked; 12448 int32_t gap; 12449 mblk_t *mp1; 12450 uint_t flags; 12451 uint32_t new_swnd = 0; 12452 uchar_t *iphdr; 12453 uchar_t *rptr; 12454 int32_t rgap; 12455 uint32_t seg_ack; 12456 int seg_len; 12457 uint_t ip_hdr_len; 12458 uint32_t seg_seq; 12459 tcph_t *tcph; 12460 int urp; 12461 tcp_opt_t tcpopt; 12462 uint_t ipvers; 12463 ip6_pkt_t ipp; 12464 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12465 uint32_t cwnd; 12466 uint32_t add; 12467 int npkt; 12468 int mss; 12469 conn_t *connp = (conn_t *)arg; 12470 squeue_t *sqp = (squeue_t *)arg2; 12471 tcp_t *tcp = connp->conn_tcp; 12472 12473 /* 12474 * RST from fused tcp loopback peer should trigger an unfuse. 12475 */ 12476 if (tcp->tcp_fused) { 12477 TCP_STAT(tcp_fusion_aborted); 12478 tcp_unfuse(tcp); 12479 } 12480 12481 iphdr = mp->b_rptr; 12482 rptr = mp->b_rptr; 12483 ASSERT(OK_32PTR(rptr)); 12484 12485 /* 12486 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12487 * processing here. For rest call tcp_find_pktinfo to fill up the 12488 * necessary information. 12489 */ 12490 if (IPCL_IS_TCP4(connp)) { 12491 ipvers = IPV4_VERSION; 12492 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12493 } else { 12494 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12495 NULL, &ipp); 12496 if (mp == NULL) { 12497 TCP_STAT(tcp_rput_v6_error); 12498 return; 12499 } 12500 iphdr = mp->b_rptr; 12501 rptr = mp->b_rptr; 12502 } 12503 ASSERT(DB_TYPE(mp) == M_DATA); 12504 12505 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12506 seg_seq = ABE32_TO_U32(tcph->th_seq); 12507 seg_ack = ABE32_TO_U32(tcph->th_ack); 12508 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12509 seg_len = (int)(mp->b_wptr - rptr) - 12510 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12511 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12512 do { 12513 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12514 (uintptr_t)INT_MAX); 12515 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12516 } while ((mp1 = mp1->b_cont) != NULL && 12517 mp1->b_datap->db_type == M_DATA); 12518 } 12519 12520 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12521 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12522 seg_len, tcph); 12523 return; 12524 } 12525 12526 if (sqp != NULL) { 12527 /* 12528 * This is the correct place to update tcp_last_recv_time. Note 12529 * that it is also updated for tcp structure that belongs to 12530 * global and listener queues which do not really need updating. 12531 * But that should not cause any harm. And it is updated for 12532 * all kinds of incoming segments, not only for data segments. 12533 */ 12534 tcp->tcp_last_recv_time = lbolt; 12535 } 12536 12537 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12538 12539 BUMP_LOCAL(tcp->tcp_ibsegs); 12540 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 12541 12542 if ((flags & TH_URG) && sqp != NULL) { 12543 /* 12544 * TCP can't handle urgent pointers that arrive before 12545 * the connection has been accept()ed since it can't 12546 * buffer OOB data. Discard segment if this happens. 12547 * 12548 * Nor can it reassemble urgent pointers, so discard 12549 * if it's not the next segment expected. 12550 * 12551 * Otherwise, collapse chain into one mblk (discard if 12552 * that fails). This makes sure the headers, retransmitted 12553 * data, and new data all are in the same mblk. 12554 */ 12555 ASSERT(mp != NULL); 12556 if (tcp->tcp_listener || !pullupmsg(mp, -1)) { 12557 freemsg(mp); 12558 return; 12559 } 12560 /* Update pointers into message */ 12561 iphdr = rptr = mp->b_rptr; 12562 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12563 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12564 /* 12565 * Since we can't handle any data with this urgent 12566 * pointer that is out of sequence, we expunge 12567 * the data. This allows us to still register 12568 * the urgent mark and generate the M_PCSIG, 12569 * which we can do. 12570 */ 12571 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12572 seg_len = 0; 12573 } 12574 } 12575 12576 switch (tcp->tcp_state) { 12577 case TCPS_SYN_SENT: 12578 if (flags & TH_ACK) { 12579 /* 12580 * Note that our stack cannot send data before a 12581 * connection is established, therefore the 12582 * following check is valid. Otherwise, it has 12583 * to be changed. 12584 */ 12585 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12586 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12587 freemsg(mp); 12588 if (flags & TH_RST) 12589 return; 12590 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12591 tcp, seg_ack, 0, TH_RST); 12592 return; 12593 } 12594 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12595 } 12596 if (flags & TH_RST) { 12597 freemsg(mp); 12598 if (flags & TH_ACK) 12599 (void) tcp_clean_death(tcp, 12600 ECONNREFUSED, 13); 12601 return; 12602 } 12603 if (!(flags & TH_SYN)) { 12604 freemsg(mp); 12605 return; 12606 } 12607 12608 /* Process all TCP options. */ 12609 tcp_process_options(tcp, tcph); 12610 /* 12611 * The following changes our rwnd to be a multiple of the 12612 * MIN(peer MSS, our MSS) for performance reason. 12613 */ 12614 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 12615 tcp->tcp_mss)); 12616 12617 /* Is the other end ECN capable? */ 12618 if (tcp->tcp_ecn_ok) { 12619 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12620 tcp->tcp_ecn_ok = B_FALSE; 12621 } 12622 } 12623 /* 12624 * Clear ECN flags because it may interfere with later 12625 * processing. 12626 */ 12627 flags &= ~(TH_ECE|TH_CWR); 12628 12629 tcp->tcp_irs = seg_seq; 12630 tcp->tcp_rack = seg_seq; 12631 tcp->tcp_rnxt = seg_seq + 1; 12632 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12633 if (!TCP_IS_DETACHED(tcp)) { 12634 /* Allocate room for SACK options if needed. */ 12635 if (tcp->tcp_snd_sack_ok) { 12636 (void) mi_set_sth_wroff(tcp->tcp_rq, 12637 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 12638 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra)); 12639 } else { 12640 (void) mi_set_sth_wroff(tcp->tcp_rq, 12641 tcp->tcp_hdr_len + 12642 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra)); 12643 } 12644 } 12645 if (flags & TH_ACK) { 12646 /* 12647 * If we can't get the confirmation upstream, pretend 12648 * we didn't even see this one. 12649 * 12650 * XXX: how can we pretend we didn't see it if we 12651 * have updated rnxt et. al. 12652 * 12653 * For loopback we defer sending up the T_CONN_CON 12654 * until after some checks below. 12655 */ 12656 mp1 = NULL; 12657 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12658 tcp->tcp_loopback ? &mp1 : NULL)) { 12659 freemsg(mp); 12660 return; 12661 } 12662 /* SYN was acked - making progress */ 12663 if (tcp->tcp_ipversion == IPV6_VERSION) 12664 tcp->tcp_ip_forward_progress = B_TRUE; 12665 12666 /* One for the SYN */ 12667 tcp->tcp_suna = tcp->tcp_iss + 1; 12668 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12669 tcp->tcp_state = TCPS_ESTABLISHED; 12670 12671 /* 12672 * If SYN was retransmitted, need to reset all 12673 * retransmission info. This is because this 12674 * segment will be treated as a dup ACK. 12675 */ 12676 if (tcp->tcp_rexmit) { 12677 tcp->tcp_rexmit = B_FALSE; 12678 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12679 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12680 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12681 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12682 tcp->tcp_ms_we_have_waited = 0; 12683 12684 /* 12685 * Set tcp_cwnd back to 1 MSS, per 12686 * recommendation from 12687 * draft-floyd-incr-init-win-01.txt, 12688 * Increasing TCP's Initial Window. 12689 */ 12690 tcp->tcp_cwnd = tcp->tcp_mss; 12691 } 12692 12693 tcp->tcp_swl1 = seg_seq; 12694 tcp->tcp_swl2 = seg_ack; 12695 12696 new_swnd = BE16_TO_U16(tcph->th_win); 12697 tcp->tcp_swnd = new_swnd; 12698 if (new_swnd > tcp->tcp_max_swnd) 12699 tcp->tcp_max_swnd = new_swnd; 12700 12701 /* 12702 * Always send the three-way handshake ack immediately 12703 * in order to make the connection complete as soon as 12704 * possible on the accepting host. 12705 */ 12706 flags |= TH_ACK_NEEDED; 12707 12708 /* 12709 * Special case for loopback. At this point we have 12710 * received SYN-ACK from the remote endpoint. In 12711 * order to ensure that both endpoints reach the 12712 * fused state prior to any data exchange, the final 12713 * ACK needs to be sent before we indicate T_CONN_CON 12714 * to the module upstream. 12715 */ 12716 if (tcp->tcp_loopback) { 12717 mblk_t *ack_mp; 12718 12719 ASSERT(!tcp->tcp_unfusable); 12720 ASSERT(mp1 != NULL); 12721 /* 12722 * For loopback, we always get a pure SYN-ACK 12723 * and only need to send back the final ACK 12724 * with no data (this is because the other 12725 * tcp is ours and we don't do T/TCP). This 12726 * final ACK triggers the passive side to 12727 * perform fusion in ESTABLISHED state. 12728 */ 12729 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 12730 if (tcp->tcp_ack_tid != 0) { 12731 (void) TCP_TIMER_CANCEL(tcp, 12732 tcp->tcp_ack_tid); 12733 tcp->tcp_ack_tid = 0; 12734 } 12735 TCP_RECORD_TRACE(tcp, ack_mp, 12736 TCP_TRACE_SEND_PKT); 12737 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 12738 BUMP_LOCAL(tcp->tcp_obsegs); 12739 BUMP_MIB(&tcp_mib, tcpOutAck); 12740 12741 /* Send up T_CONN_CON */ 12742 putnext(tcp->tcp_rq, mp1); 12743 12744 freemsg(mp); 12745 return; 12746 } 12747 /* 12748 * Forget fusion; we need to handle more 12749 * complex cases below. Send the deferred 12750 * T_CONN_CON message upstream and proceed 12751 * as usual. Mark this tcp as not capable 12752 * of fusion. 12753 */ 12754 TCP_STAT(tcp_fusion_unfusable); 12755 tcp->tcp_unfusable = B_TRUE; 12756 putnext(tcp->tcp_rq, mp1); 12757 } 12758 12759 /* 12760 * Check to see if there is data to be sent. If 12761 * yes, set the transmit flag. Then check to see 12762 * if received data processing needs to be done. 12763 * If not, go straight to xmit_check. This short 12764 * cut is OK as we don't support T/TCP. 12765 */ 12766 if (tcp->tcp_unsent) 12767 flags |= TH_XMIT_NEEDED; 12768 12769 if (seg_len == 0 && !(flags & TH_URG)) { 12770 freemsg(mp); 12771 goto xmit_check; 12772 } 12773 12774 flags &= ~TH_SYN; 12775 seg_seq++; 12776 break; 12777 } 12778 tcp->tcp_state = TCPS_SYN_RCVD; 12779 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 12780 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 12781 if (mp1) { 12782 DB_CPID(mp1) = tcp->tcp_cpid; 12783 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 12784 tcp_send_data(tcp, tcp->tcp_wq, mp1); 12785 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 12786 } 12787 freemsg(mp); 12788 return; 12789 case TCPS_SYN_RCVD: 12790 if (flags & TH_ACK) { 12791 /* 12792 * In this state, a SYN|ACK packet is either bogus 12793 * because the other side must be ACKing our SYN which 12794 * indicates it has seen the ACK for their SYN and 12795 * shouldn't retransmit it or we're crossing SYNs 12796 * on active open. 12797 */ 12798 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 12799 freemsg(mp); 12800 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 12801 tcp, seg_ack, 0, TH_RST); 12802 return; 12803 } 12804 /* 12805 * NOTE: RFC 793 pg. 72 says this should be 12806 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 12807 * but that would mean we have an ack that ignored 12808 * our SYN. 12809 */ 12810 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 12811 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12812 freemsg(mp); 12813 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 12814 tcp, seg_ack, 0, TH_RST); 12815 return; 12816 } 12817 } 12818 break; 12819 case TCPS_LISTEN: 12820 /* 12821 * Only a TLI listener can come through this path when a 12822 * acceptor is going back to be a listener and a packet 12823 * for the acceptor hits the classifier. For a socket 12824 * listener, this can never happen because a listener 12825 * can never accept connection on itself and hence a 12826 * socket acceptor can not go back to being a listener. 12827 */ 12828 ASSERT(!TCP_IS_SOCKET(tcp)); 12829 /*FALLTHRU*/ 12830 case TCPS_CLOSED: 12831 case TCPS_BOUND: { 12832 conn_t *new_connp; 12833 12834 new_connp = ipcl_classify(mp, connp->conn_zoneid); 12835 if (new_connp != NULL) { 12836 tcp_reinput(new_connp, mp, connp->conn_sqp); 12837 return; 12838 } 12839 /* We failed to classify. For now just drop the packet */ 12840 freemsg(mp); 12841 return; 12842 } 12843 case TCPS_IDLE: 12844 /* 12845 * Handle the case where the tcp_clean_death() has happened 12846 * on a connection (application hasn't closed yet) but a packet 12847 * was already queued on squeue before tcp_clean_death() 12848 * was processed. Calling tcp_clean_death() twice on same 12849 * connection can result in weird behaviour. 12850 */ 12851 freemsg(mp); 12852 return; 12853 default: 12854 break; 12855 } 12856 12857 /* 12858 * Already on the correct queue/perimeter. 12859 * If this is a detached connection and not an eager 12860 * connection hanging off a listener then new data 12861 * (past the FIN) will cause a reset. 12862 * We do a special check here where it 12863 * is out of the main line, rather than check 12864 * if we are detached every time we see new 12865 * data down below. 12866 */ 12867 if (TCP_IS_DETACHED_NONEAGER(tcp) && 12868 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 12869 BUMP_MIB(&tcp_mib, tcpInClosed); 12870 TCP_RECORD_TRACE(tcp, 12871 mp, TCP_TRACE_RECV_PKT); 12872 12873 freemsg(mp); 12874 /* 12875 * This could be an SSL closure alert. We're detached so just 12876 * acknowledge it this last time. 12877 */ 12878 if (tcp->tcp_kssl_ctx != NULL) { 12879 kssl_release_ctx(tcp->tcp_kssl_ctx); 12880 tcp->tcp_kssl_ctx = NULL; 12881 12882 tcp->tcp_rnxt += seg_len; 12883 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12884 flags |= TH_ACK_NEEDED; 12885 goto ack_check; 12886 } 12887 12888 tcp_xmit_ctl("new data when detached", tcp, 12889 tcp->tcp_snxt, 0, TH_RST); 12890 (void) tcp_clean_death(tcp, EPROTO, 12); 12891 return; 12892 } 12893 12894 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12895 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 12896 new_swnd = BE16_TO_U16(tcph->th_win) << 12897 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 12898 mss = tcp->tcp_mss; 12899 12900 if (tcp->tcp_snd_ts_ok) { 12901 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 12902 /* 12903 * This segment is not acceptable. 12904 * Drop it and send back an ACK. 12905 */ 12906 freemsg(mp); 12907 flags |= TH_ACK_NEEDED; 12908 goto ack_check; 12909 } 12910 } else if (tcp->tcp_snd_sack_ok) { 12911 ASSERT(tcp->tcp_sack_info != NULL); 12912 tcpopt.tcp = tcp; 12913 /* 12914 * SACK info in already updated in tcp_parse_options. Ignore 12915 * all other TCP options... 12916 */ 12917 (void) tcp_parse_options(tcph, &tcpopt); 12918 } 12919 try_again:; 12920 gap = seg_seq - tcp->tcp_rnxt; 12921 rgap = tcp->tcp_rwnd - (gap + seg_len); 12922 /* 12923 * gap is the amount of sequence space between what we expect to see 12924 * and what we got for seg_seq. A positive value for gap means 12925 * something got lost. A negative value means we got some old stuff. 12926 */ 12927 if (gap < 0) { 12928 /* Old stuff present. Is the SYN in there? */ 12929 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 12930 (seg_len != 0)) { 12931 flags &= ~TH_SYN; 12932 seg_seq++; 12933 urp--; 12934 /* Recompute the gaps after noting the SYN. */ 12935 goto try_again; 12936 } 12937 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 12938 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, 12939 (seg_len > -gap ? -gap : seg_len)); 12940 /* Remove the old stuff from seg_len. */ 12941 seg_len += gap; 12942 /* 12943 * Anything left? 12944 * Make sure to check for unack'd FIN when rest of data 12945 * has been previously ack'd. 12946 */ 12947 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 12948 /* 12949 * Resets are only valid if they lie within our offered 12950 * window. If the RST bit is set, we just ignore this 12951 * segment. 12952 */ 12953 if (flags & TH_RST) { 12954 freemsg(mp); 12955 return; 12956 } 12957 12958 /* 12959 * The arriving of dup data packets indicate that we 12960 * may have postponed an ack for too long, or the other 12961 * side's RTT estimate is out of shape. Start acking 12962 * more often. 12963 */ 12964 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 12965 tcp->tcp_rack_cnt >= 1 && 12966 tcp->tcp_rack_abs_max > 2) { 12967 tcp->tcp_rack_abs_max--; 12968 } 12969 tcp->tcp_rack_cur_max = 1; 12970 12971 /* 12972 * This segment is "unacceptable". None of its 12973 * sequence space lies within our advertized window. 12974 * 12975 * Adjust seg_len to the original value for tracing. 12976 */ 12977 seg_len -= gap; 12978 if (tcp->tcp_debug) { 12979 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 12980 "tcp_rput: unacceptable, gap %d, rgap %d, " 12981 "flags 0x%x, seg_seq %u, seg_ack %u, " 12982 "seg_len %d, rnxt %u, snxt %u, %s", 12983 gap, rgap, flags, seg_seq, seg_ack, 12984 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 12985 tcp_display(tcp, NULL, 12986 DISP_ADDR_AND_PORT)); 12987 } 12988 12989 /* 12990 * Arrange to send an ACK in response to the 12991 * unacceptable segment per RFC 793 page 69. There 12992 * is only one small difference between ours and the 12993 * acceptability test in the RFC - we accept ACK-only 12994 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 12995 * will be generated. 12996 * 12997 * Note that we have to ACK an ACK-only packet at least 12998 * for stacks that send 0-length keep-alives with 12999 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13000 * section 4.2.3.6. As long as we don't ever generate 13001 * an unacceptable packet in response to an incoming 13002 * packet that is unacceptable, it should not cause 13003 * "ACK wars". 13004 */ 13005 flags |= TH_ACK_NEEDED; 13006 13007 /* 13008 * Continue processing this segment in order to use the 13009 * ACK information it contains, but skip all other 13010 * sequence-number processing. Processing the ACK 13011 * information is necessary in order to 13012 * re-synchronize connections that may have lost 13013 * synchronization. 13014 * 13015 * We clear seg_len and flag fields related to 13016 * sequence number processing as they are not 13017 * to be trusted for an unacceptable segment. 13018 */ 13019 seg_len = 0; 13020 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13021 goto process_ack; 13022 } 13023 13024 /* Fix seg_seq, and chew the gap off the front. */ 13025 seg_seq = tcp->tcp_rnxt; 13026 urp += gap; 13027 do { 13028 mblk_t *mp2; 13029 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13030 (uintptr_t)UINT_MAX); 13031 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13032 if (gap > 0) { 13033 mp->b_rptr = mp->b_wptr - gap; 13034 break; 13035 } 13036 mp2 = mp; 13037 mp = mp->b_cont; 13038 freeb(mp2); 13039 } while (gap < 0); 13040 /* 13041 * If the urgent data has already been acknowledged, we 13042 * should ignore TH_URG below 13043 */ 13044 if (urp < 0) 13045 flags &= ~TH_URG; 13046 } 13047 /* 13048 * rgap is the amount of stuff received out of window. A negative 13049 * value is the amount out of window. 13050 */ 13051 if (rgap < 0) { 13052 mblk_t *mp2; 13053 13054 if (tcp->tcp_rwnd == 0) { 13055 BUMP_MIB(&tcp_mib, tcpInWinProbe); 13056 } else { 13057 BUMP_MIB(&tcp_mib, tcpInDataPastWinSegs); 13058 UPDATE_MIB(&tcp_mib, tcpInDataPastWinBytes, -rgap); 13059 } 13060 13061 /* 13062 * seg_len does not include the FIN, so if more than 13063 * just the FIN is out of window, we act like we don't 13064 * see it. (If just the FIN is out of window, rgap 13065 * will be zero and we will go ahead and acknowledge 13066 * the FIN.) 13067 */ 13068 flags &= ~TH_FIN; 13069 13070 /* Fix seg_len and make sure there is something left. */ 13071 seg_len += rgap; 13072 if (seg_len <= 0) { 13073 /* 13074 * Resets are only valid if they lie within our offered 13075 * window. If the RST bit is set, we just ignore this 13076 * segment. 13077 */ 13078 if (flags & TH_RST) { 13079 freemsg(mp); 13080 return; 13081 } 13082 13083 /* Per RFC 793, we need to send back an ACK. */ 13084 flags |= TH_ACK_NEEDED; 13085 13086 /* 13087 * Send SIGURG as soon as possible i.e. even 13088 * if the TH_URG was delivered in a window probe 13089 * packet (which will be unacceptable). 13090 * 13091 * We generate a signal if none has been generated 13092 * for this connection or if this is a new urgent 13093 * byte. Also send a zero-length "unmarked" message 13094 * to inform SIOCATMARK that this is not the mark. 13095 * 13096 * tcp_urp_last_valid is cleared when the T_exdata_ind 13097 * is sent up. This plus the check for old data 13098 * (gap >= 0) handles the wraparound of the sequence 13099 * number space without having to always track the 13100 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13101 * this max in its rcv_up variable). 13102 * 13103 * This prevents duplicate SIGURGS due to a "late" 13104 * zero-window probe when the T_EXDATA_IND has already 13105 * been sent up. 13106 */ 13107 if ((flags & TH_URG) && 13108 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13109 tcp->tcp_urp_last))) { 13110 mp1 = allocb(0, BPRI_MED); 13111 if (mp1 == NULL) { 13112 freemsg(mp); 13113 return; 13114 } 13115 if (!TCP_IS_DETACHED(tcp) && 13116 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13117 SIGURG)) { 13118 /* Try again on the rexmit. */ 13119 freemsg(mp1); 13120 freemsg(mp); 13121 return; 13122 } 13123 /* 13124 * If the next byte would be the mark 13125 * then mark with MARKNEXT else mark 13126 * with NOTMARKNEXT. 13127 */ 13128 if (gap == 0 && urp == 0) 13129 mp1->b_flag |= MSGMARKNEXT; 13130 else 13131 mp1->b_flag |= MSGNOTMARKNEXT; 13132 freemsg(tcp->tcp_urp_mark_mp); 13133 tcp->tcp_urp_mark_mp = mp1; 13134 flags |= TH_SEND_URP_MARK; 13135 tcp->tcp_urp_last_valid = B_TRUE; 13136 tcp->tcp_urp_last = urp + seg_seq; 13137 } 13138 /* 13139 * If this is a zero window probe, continue to 13140 * process the ACK part. But we need to set seg_len 13141 * to 0 to avoid data processing. Otherwise just 13142 * drop the segment and send back an ACK. 13143 */ 13144 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13145 flags &= ~(TH_SYN | TH_URG); 13146 seg_len = 0; 13147 goto process_ack; 13148 } else { 13149 freemsg(mp); 13150 goto ack_check; 13151 } 13152 } 13153 /* Pitch out of window stuff off the end. */ 13154 rgap = seg_len; 13155 mp2 = mp; 13156 do { 13157 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13158 (uintptr_t)INT_MAX); 13159 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13160 if (rgap < 0) { 13161 mp2->b_wptr += rgap; 13162 if ((mp1 = mp2->b_cont) != NULL) { 13163 mp2->b_cont = NULL; 13164 freemsg(mp1); 13165 } 13166 break; 13167 } 13168 } while ((mp2 = mp2->b_cont) != NULL); 13169 } 13170 ok:; 13171 /* 13172 * TCP should check ECN info for segments inside the window only. 13173 * Therefore the check should be done here. 13174 */ 13175 if (tcp->tcp_ecn_ok) { 13176 if (flags & TH_CWR) { 13177 tcp->tcp_ecn_echo_on = B_FALSE; 13178 } 13179 /* 13180 * Note that both ECN_CE and CWR can be set in the 13181 * same segment. In this case, we once again turn 13182 * on ECN_ECHO. 13183 */ 13184 if (tcp->tcp_ipversion == IPV4_VERSION) { 13185 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13186 13187 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13188 tcp->tcp_ecn_echo_on = B_TRUE; 13189 } 13190 } else { 13191 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13192 13193 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13194 htonl(IPH_ECN_CE << 20)) { 13195 tcp->tcp_ecn_echo_on = B_TRUE; 13196 } 13197 } 13198 } 13199 13200 /* 13201 * Check whether we can update tcp_ts_recent. This test is 13202 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13203 * Extensions for High Performance: An Update", Internet Draft. 13204 */ 13205 if (tcp->tcp_snd_ts_ok && 13206 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13207 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13208 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13209 tcp->tcp_last_rcv_lbolt = lbolt64; 13210 } 13211 13212 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13213 /* 13214 * FIN in an out of order segment. We record this in 13215 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13216 * Clear the FIN so that any check on FIN flag will fail. 13217 * Remember that FIN also counts in the sequence number 13218 * space. So we need to ack out of order FIN only segments. 13219 */ 13220 if (flags & TH_FIN) { 13221 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13222 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13223 flags &= ~TH_FIN; 13224 flags |= TH_ACK_NEEDED; 13225 } 13226 if (seg_len > 0) { 13227 /* Fill in the SACK blk list. */ 13228 if (tcp->tcp_snd_sack_ok) { 13229 ASSERT(tcp->tcp_sack_info != NULL); 13230 tcp_sack_insert(tcp->tcp_sack_list, 13231 seg_seq, seg_seq + seg_len, 13232 &(tcp->tcp_num_sack_blk)); 13233 } 13234 13235 /* 13236 * Attempt reassembly and see if we have something 13237 * ready to go. 13238 */ 13239 mp = tcp_reass(tcp, mp, seg_seq); 13240 /* Always ack out of order packets */ 13241 flags |= TH_ACK_NEEDED | TH_PUSH; 13242 if (mp) { 13243 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13244 (uintptr_t)INT_MAX); 13245 seg_len = mp->b_cont ? msgdsize(mp) : 13246 (int)(mp->b_wptr - mp->b_rptr); 13247 seg_seq = tcp->tcp_rnxt; 13248 /* 13249 * A gap is filled and the seq num and len 13250 * of the gap match that of a previously 13251 * received FIN, put the FIN flag back in. 13252 */ 13253 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13254 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13255 flags |= TH_FIN; 13256 tcp->tcp_valid_bits &= 13257 ~TCP_OFO_FIN_VALID; 13258 } 13259 } else { 13260 /* 13261 * Keep going even with NULL mp. 13262 * There may be a useful ACK or something else 13263 * we don't want to miss. 13264 * 13265 * But TCP should not perform fast retransmit 13266 * because of the ack number. TCP uses 13267 * seg_len == 0 to determine if it is a pure 13268 * ACK. And this is not a pure ACK. 13269 */ 13270 seg_len = 0; 13271 ofo_seg = B_TRUE; 13272 } 13273 } 13274 } else if (seg_len > 0) { 13275 BUMP_MIB(&tcp_mib, tcpInDataInorderSegs); 13276 UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, seg_len); 13277 /* 13278 * If an out of order FIN was received before, and the seq 13279 * num and len of the new segment match that of the FIN, 13280 * put the FIN flag back in. 13281 */ 13282 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13283 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13284 flags |= TH_FIN; 13285 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13286 } 13287 } 13288 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13289 if (flags & TH_RST) { 13290 freemsg(mp); 13291 switch (tcp->tcp_state) { 13292 case TCPS_SYN_RCVD: 13293 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13294 break; 13295 case TCPS_ESTABLISHED: 13296 case TCPS_FIN_WAIT_1: 13297 case TCPS_FIN_WAIT_2: 13298 case TCPS_CLOSE_WAIT: 13299 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13300 break; 13301 case TCPS_CLOSING: 13302 case TCPS_LAST_ACK: 13303 (void) tcp_clean_death(tcp, 0, 16); 13304 break; 13305 default: 13306 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13307 (void) tcp_clean_death(tcp, ENXIO, 17); 13308 break; 13309 } 13310 return; 13311 } 13312 if (flags & TH_SYN) { 13313 /* 13314 * See RFC 793, Page 71 13315 * 13316 * The seq number must be in the window as it should 13317 * be "fixed" above. If it is outside window, it should 13318 * be already rejected. Note that we allow seg_seq to be 13319 * rnxt + rwnd because we want to accept 0 window probe. 13320 */ 13321 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13322 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13323 freemsg(mp); 13324 /* 13325 * If the ACK flag is not set, just use our snxt as the 13326 * seq number of the RST segment. 13327 */ 13328 if (!(flags & TH_ACK)) { 13329 seg_ack = tcp->tcp_snxt; 13330 } 13331 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13332 TH_RST|TH_ACK); 13333 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13334 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13335 return; 13336 } 13337 /* 13338 * urp could be -1 when the urp field in the packet is 0 13339 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13340 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13341 */ 13342 if (flags & TH_URG && urp >= 0) { 13343 if (!tcp->tcp_urp_last_valid || 13344 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13345 /* 13346 * If we haven't generated the signal yet for this 13347 * urgent pointer value, do it now. Also, send up a 13348 * zero-length M_DATA indicating whether or not this is 13349 * the mark. The latter is not needed when a 13350 * T_EXDATA_IND is sent up. However, if there are 13351 * allocation failures this code relies on the sender 13352 * retransmitting and the socket code for determining 13353 * the mark should not block waiting for the peer to 13354 * transmit. Thus, for simplicity we always send up the 13355 * mark indication. 13356 */ 13357 mp1 = allocb(0, BPRI_MED); 13358 if (mp1 == NULL) { 13359 freemsg(mp); 13360 return; 13361 } 13362 if (!TCP_IS_DETACHED(tcp) && 13363 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13364 /* Try again on the rexmit. */ 13365 freemsg(mp1); 13366 freemsg(mp); 13367 return; 13368 } 13369 /* 13370 * Mark with NOTMARKNEXT for now. 13371 * The code below will change this to MARKNEXT 13372 * if we are at the mark. 13373 * 13374 * If there are allocation failures (e.g. in dupmsg 13375 * below) the next time tcp_rput_data sees the urgent 13376 * segment it will send up the MSG*MARKNEXT message. 13377 */ 13378 mp1->b_flag |= MSGNOTMARKNEXT; 13379 freemsg(tcp->tcp_urp_mark_mp); 13380 tcp->tcp_urp_mark_mp = mp1; 13381 flags |= TH_SEND_URP_MARK; 13382 #ifdef DEBUG 13383 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13384 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13385 "last %x, %s", 13386 seg_seq, urp, tcp->tcp_urp_last, 13387 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13388 #endif /* DEBUG */ 13389 tcp->tcp_urp_last_valid = B_TRUE; 13390 tcp->tcp_urp_last = urp + seg_seq; 13391 } else if (tcp->tcp_urp_mark_mp != NULL) { 13392 /* 13393 * An allocation failure prevented the previous 13394 * tcp_rput_data from sending up the allocated 13395 * MSG*MARKNEXT message - send it up this time 13396 * around. 13397 */ 13398 flags |= TH_SEND_URP_MARK; 13399 } 13400 13401 /* 13402 * If the urgent byte is in this segment, make sure that it is 13403 * all by itself. This makes it much easier to deal with the 13404 * possibility of an allocation failure on the T_exdata_ind. 13405 * Note that seg_len is the number of bytes in the segment, and 13406 * urp is the offset into the segment of the urgent byte. 13407 * urp < seg_len means that the urgent byte is in this segment. 13408 */ 13409 if (urp < seg_len) { 13410 if (seg_len != 1) { 13411 uint32_t tmp_rnxt; 13412 /* 13413 * Break it up and feed it back in. 13414 * Re-attach the IP header. 13415 */ 13416 mp->b_rptr = iphdr; 13417 if (urp > 0) { 13418 /* 13419 * There is stuff before the urgent 13420 * byte. 13421 */ 13422 mp1 = dupmsg(mp); 13423 if (!mp1) { 13424 /* 13425 * Trim from urgent byte on. 13426 * The rest will come back. 13427 */ 13428 (void) adjmsg(mp, 13429 urp - seg_len); 13430 tcp_rput_data(connp, 13431 mp, NULL); 13432 return; 13433 } 13434 (void) adjmsg(mp1, urp - seg_len); 13435 /* Feed this piece back in. */ 13436 tmp_rnxt = tcp->tcp_rnxt; 13437 tcp_rput_data(connp, mp1, NULL); 13438 /* 13439 * If the data passed back in was not 13440 * processed (ie: bad ACK) sending 13441 * the remainder back in will cause a 13442 * loop. In this case, drop the 13443 * packet and let the sender try 13444 * sending a good packet. 13445 */ 13446 if (tmp_rnxt == tcp->tcp_rnxt) { 13447 freemsg(mp); 13448 return; 13449 } 13450 } 13451 if (urp != seg_len - 1) { 13452 uint32_t tmp_rnxt; 13453 /* 13454 * There is stuff after the urgent 13455 * byte. 13456 */ 13457 mp1 = dupmsg(mp); 13458 if (!mp1) { 13459 /* 13460 * Trim everything beyond the 13461 * urgent byte. The rest will 13462 * come back. 13463 */ 13464 (void) adjmsg(mp, 13465 urp + 1 - seg_len); 13466 tcp_rput_data(connp, 13467 mp, NULL); 13468 return; 13469 } 13470 (void) adjmsg(mp1, urp + 1 - seg_len); 13471 tmp_rnxt = tcp->tcp_rnxt; 13472 tcp_rput_data(connp, mp1, NULL); 13473 /* 13474 * If the data passed back in was not 13475 * processed (ie: bad ACK) sending 13476 * the remainder back in will cause a 13477 * loop. In this case, drop the 13478 * packet and let the sender try 13479 * sending a good packet. 13480 */ 13481 if (tmp_rnxt == tcp->tcp_rnxt) { 13482 freemsg(mp); 13483 return; 13484 } 13485 } 13486 tcp_rput_data(connp, mp, NULL); 13487 return; 13488 } 13489 /* 13490 * This segment contains only the urgent byte. We 13491 * have to allocate the T_exdata_ind, if we can. 13492 */ 13493 if (!tcp->tcp_urp_mp) { 13494 struct T_exdata_ind *tei; 13495 mp1 = allocb(sizeof (struct T_exdata_ind), 13496 BPRI_MED); 13497 if (!mp1) { 13498 /* 13499 * Sigh... It'll be back. 13500 * Generate any MSG*MARK message now. 13501 */ 13502 freemsg(mp); 13503 seg_len = 0; 13504 if (flags & TH_SEND_URP_MARK) { 13505 13506 13507 ASSERT(tcp->tcp_urp_mark_mp); 13508 tcp->tcp_urp_mark_mp->b_flag &= 13509 ~MSGNOTMARKNEXT; 13510 tcp->tcp_urp_mark_mp->b_flag |= 13511 MSGMARKNEXT; 13512 } 13513 goto ack_check; 13514 } 13515 mp1->b_datap->db_type = M_PROTO; 13516 tei = (struct T_exdata_ind *)mp1->b_rptr; 13517 tei->PRIM_type = T_EXDATA_IND; 13518 tei->MORE_flag = 0; 13519 mp1->b_wptr = (uchar_t *)&tei[1]; 13520 tcp->tcp_urp_mp = mp1; 13521 #ifdef DEBUG 13522 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13523 "tcp_rput: allocated exdata_ind %s", 13524 tcp_display(tcp, NULL, 13525 DISP_PORT_ONLY)); 13526 #endif /* DEBUG */ 13527 /* 13528 * There is no need to send a separate MSG*MARK 13529 * message since the T_EXDATA_IND will be sent 13530 * now. 13531 */ 13532 flags &= ~TH_SEND_URP_MARK; 13533 freemsg(tcp->tcp_urp_mark_mp); 13534 tcp->tcp_urp_mark_mp = NULL; 13535 } 13536 /* 13537 * Now we are all set. On the next putnext upstream, 13538 * tcp_urp_mp will be non-NULL and will get prepended 13539 * to what has to be this piece containing the urgent 13540 * byte. If for any reason we abort this segment below, 13541 * if it comes back, we will have this ready, or it 13542 * will get blown off in close. 13543 */ 13544 } else if (urp == seg_len) { 13545 /* 13546 * The urgent byte is the next byte after this sequence 13547 * number. If there is data it is marked with 13548 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13549 * since it is not needed. Otherwise, if the code 13550 * above just allocated a zero-length tcp_urp_mark_mp 13551 * message, that message is tagged with MSGMARKNEXT. 13552 * Sending up these MSGMARKNEXT messages makes 13553 * SIOCATMARK work correctly even though 13554 * the T_EXDATA_IND will not be sent up until the 13555 * urgent byte arrives. 13556 */ 13557 if (seg_len != 0) { 13558 flags |= TH_MARKNEXT_NEEDED; 13559 freemsg(tcp->tcp_urp_mark_mp); 13560 tcp->tcp_urp_mark_mp = NULL; 13561 flags &= ~TH_SEND_URP_MARK; 13562 } else if (tcp->tcp_urp_mark_mp != NULL) { 13563 flags |= TH_SEND_URP_MARK; 13564 tcp->tcp_urp_mark_mp->b_flag &= 13565 ~MSGNOTMARKNEXT; 13566 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13567 } 13568 #ifdef DEBUG 13569 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13570 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13571 seg_len, flags, 13572 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13573 #endif /* DEBUG */ 13574 } else { 13575 /* Data left until we hit mark */ 13576 #ifdef DEBUG 13577 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13578 "tcp_rput: URP %d bytes left, %s", 13579 urp - seg_len, tcp_display(tcp, NULL, 13580 DISP_PORT_ONLY)); 13581 #endif /* DEBUG */ 13582 } 13583 } 13584 13585 process_ack: 13586 if (!(flags & TH_ACK)) { 13587 freemsg(mp); 13588 goto xmit_check; 13589 } 13590 } 13591 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13592 13593 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13594 tcp->tcp_ip_forward_progress = B_TRUE; 13595 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13596 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13597 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13598 /* 3-way handshake complete - pass up the T_CONN_IND */ 13599 tcp_t *listener = tcp->tcp_listener; 13600 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13601 13602 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13603 /* 13604 * We are here means eager is fine but it can 13605 * get a TH_RST at any point between now and till 13606 * accept completes and disappear. We need to 13607 * ensure that reference to eager is valid after 13608 * we get out of eager's perimeter. So we do 13609 * an extra refhold. 13610 */ 13611 CONN_INC_REF(connp); 13612 13613 /* 13614 * The listener also exists because of the refhold 13615 * done in tcp_conn_request. Its possible that it 13616 * might have closed. We will check that once we 13617 * get inside listeners context. 13618 */ 13619 CONN_INC_REF(listener->tcp_connp); 13620 if (listener->tcp_connp->conn_sqp == 13621 connp->conn_sqp) { 13622 tcp_send_conn_ind(listener->tcp_connp, mp, 13623 listener->tcp_connp->conn_sqp); 13624 CONN_DEC_REF(listener->tcp_connp); 13625 } else if (!tcp->tcp_loopback) { 13626 squeue_fill(listener->tcp_connp->conn_sqp, mp, 13627 tcp_send_conn_ind, 13628 listener->tcp_connp, SQTAG_TCP_CONN_IND); 13629 } else { 13630 squeue_enter(listener->tcp_connp->conn_sqp, mp, 13631 tcp_send_conn_ind, listener->tcp_connp, 13632 SQTAG_TCP_CONN_IND); 13633 } 13634 } 13635 13636 if (tcp->tcp_active_open) { 13637 /* 13638 * We are seeing the final ack in the three way 13639 * hand shake of a active open'ed connection 13640 * so we must send up a T_CONN_CON 13641 */ 13642 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13643 freemsg(mp); 13644 return; 13645 } 13646 /* 13647 * Don't fuse the loopback endpoints for 13648 * simultaneous active opens. 13649 */ 13650 if (tcp->tcp_loopback) { 13651 TCP_STAT(tcp_fusion_unfusable); 13652 tcp->tcp_unfusable = B_TRUE; 13653 } 13654 } 13655 13656 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 13657 bytes_acked--; 13658 /* SYN was acked - making progress */ 13659 if (tcp->tcp_ipversion == IPV6_VERSION) 13660 tcp->tcp_ip_forward_progress = B_TRUE; 13661 13662 /* 13663 * If SYN was retransmitted, need to reset all 13664 * retransmission info as this segment will be 13665 * treated as a dup ACK. 13666 */ 13667 if (tcp->tcp_rexmit) { 13668 tcp->tcp_rexmit = B_FALSE; 13669 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13670 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13671 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13672 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13673 tcp->tcp_ms_we_have_waited = 0; 13674 tcp->tcp_cwnd = mss; 13675 } 13676 13677 /* 13678 * We set the send window to zero here. 13679 * This is needed if there is data to be 13680 * processed already on the queue. 13681 * Later (at swnd_update label), the 13682 * "new_swnd > tcp_swnd" condition is satisfied 13683 * the XMIT_NEEDED flag is set in the current 13684 * (SYN_RCVD) state. This ensures tcp_wput_data() is 13685 * called if there is already data on queue in 13686 * this state. 13687 */ 13688 tcp->tcp_swnd = 0; 13689 13690 if (new_swnd > tcp->tcp_max_swnd) 13691 tcp->tcp_max_swnd = new_swnd; 13692 tcp->tcp_swl1 = seg_seq; 13693 tcp->tcp_swl2 = seg_ack; 13694 tcp->tcp_state = TCPS_ESTABLISHED; 13695 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13696 13697 /* Fuse when both sides are in ESTABLISHED state */ 13698 if (tcp->tcp_loopback && do_tcp_fusion) 13699 tcp_fuse(tcp, iphdr, tcph); 13700 13701 } 13702 /* This code follows 4.4BSD-Lite2 mostly. */ 13703 if (bytes_acked < 0) 13704 goto est; 13705 13706 /* 13707 * If TCP is ECN capable and the congestion experience bit is 13708 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 13709 * done once per window (or more loosely, per RTT). 13710 */ 13711 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 13712 tcp->tcp_cwr = B_FALSE; 13713 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 13714 if (!tcp->tcp_cwr) { 13715 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 13716 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 13717 tcp->tcp_cwnd = npkt * mss; 13718 /* 13719 * If the cwnd is 0, use the timer to clock out 13720 * new segments. This is required by the ECN spec. 13721 */ 13722 if (npkt == 0) { 13723 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13724 /* 13725 * This makes sure that when the ACK comes 13726 * back, we will increase tcp_cwnd by 1 MSS. 13727 */ 13728 tcp->tcp_cwnd_cnt = 0; 13729 } 13730 tcp->tcp_cwr = B_TRUE; 13731 /* 13732 * This marks the end of the current window of in 13733 * flight data. That is why we don't use 13734 * tcp_suna + tcp_swnd. Only data in flight can 13735 * provide ECN info. 13736 */ 13737 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13738 tcp->tcp_ecn_cwr_sent = B_FALSE; 13739 } 13740 } 13741 13742 mp1 = tcp->tcp_xmit_head; 13743 if (bytes_acked == 0) { 13744 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 13745 int dupack_cnt; 13746 13747 BUMP_MIB(&tcp_mib, tcpInDupAck); 13748 /* 13749 * Fast retransmit. When we have seen exactly three 13750 * identical ACKs while we have unacked data 13751 * outstanding we take it as a hint that our peer 13752 * dropped something. 13753 * 13754 * If TCP is retransmitting, don't do fast retransmit. 13755 */ 13756 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 13757 ! tcp->tcp_rexmit) { 13758 /* Do Limited Transmit */ 13759 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 13760 tcp_dupack_fast_retransmit) { 13761 /* 13762 * RFC 3042 13763 * 13764 * What we need to do is temporarily 13765 * increase tcp_cwnd so that new 13766 * data can be sent if it is allowed 13767 * by the receive window (tcp_rwnd). 13768 * tcp_wput_data() will take care of 13769 * the rest. 13770 * 13771 * If the connection is SACK capable, 13772 * only do limited xmit when there 13773 * is SACK info. 13774 * 13775 * Note how tcp_cwnd is incremented. 13776 * The first dup ACK will increase 13777 * it by 1 MSS. The second dup ACK 13778 * will increase it by 2 MSS. This 13779 * means that only 1 new segment will 13780 * be sent for each dup ACK. 13781 */ 13782 if (tcp->tcp_unsent > 0 && 13783 (!tcp->tcp_snd_sack_ok || 13784 (tcp->tcp_snd_sack_ok && 13785 tcp->tcp_notsack_list != NULL))) { 13786 tcp->tcp_cwnd += mss << 13787 (tcp->tcp_dupack_cnt - 1); 13788 flags |= TH_LIMIT_XMIT; 13789 } 13790 } else if (dupack_cnt == 13791 tcp_dupack_fast_retransmit) { 13792 13793 /* 13794 * If we have reduced tcp_ssthresh 13795 * because of ECN, do not reduce it again 13796 * unless it is already one window of data 13797 * away. After one window of data, tcp_cwr 13798 * should then be cleared. Note that 13799 * for non ECN capable connection, tcp_cwr 13800 * should always be false. 13801 * 13802 * Adjust cwnd since the duplicate 13803 * ack indicates that a packet was 13804 * dropped (due to congestion.) 13805 */ 13806 if (!tcp->tcp_cwr) { 13807 npkt = ((tcp->tcp_snxt - 13808 tcp->tcp_suna) >> 1) / mss; 13809 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 13810 mss; 13811 tcp->tcp_cwnd = (npkt + 13812 tcp->tcp_dupack_cnt) * mss; 13813 } 13814 if (tcp->tcp_ecn_ok) { 13815 tcp->tcp_cwr = B_TRUE; 13816 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13817 tcp->tcp_ecn_cwr_sent = B_FALSE; 13818 } 13819 13820 /* 13821 * We do Hoe's algorithm. Refer to her 13822 * paper "Improving the Start-up Behavior 13823 * of a Congestion Control Scheme for TCP," 13824 * appeared in SIGCOMM'96. 13825 * 13826 * Save highest seq no we have sent so far. 13827 * Be careful about the invisible FIN byte. 13828 */ 13829 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 13830 (tcp->tcp_unsent == 0)) { 13831 tcp->tcp_rexmit_max = tcp->tcp_fss; 13832 } else { 13833 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13834 } 13835 13836 /* 13837 * Do not allow bursty traffic during. 13838 * fast recovery. Refer to Fall and Floyd's 13839 * paper "Simulation-based Comparisons of 13840 * Tahoe, Reno and SACK TCP" (in CCR?) 13841 * This is a best current practise. 13842 */ 13843 tcp->tcp_snd_burst = TCP_CWND_SS; 13844 13845 /* 13846 * For SACK: 13847 * Calculate tcp_pipe, which is the 13848 * estimated number of bytes in 13849 * network. 13850 * 13851 * tcp_fack is the highest sack'ed seq num 13852 * TCP has received. 13853 * 13854 * tcp_pipe is explained in the above quoted 13855 * Fall and Floyd's paper. tcp_fack is 13856 * explained in Mathis and Mahdavi's 13857 * "Forward Acknowledgment: Refining TCP 13858 * Congestion Control" in SIGCOMM '96. 13859 */ 13860 if (tcp->tcp_snd_sack_ok) { 13861 ASSERT(tcp->tcp_sack_info != NULL); 13862 if (tcp->tcp_notsack_list != NULL) { 13863 tcp->tcp_pipe = tcp->tcp_snxt - 13864 tcp->tcp_fack; 13865 tcp->tcp_sack_snxt = seg_ack; 13866 flags |= TH_NEED_SACK_REXMIT; 13867 } else { 13868 /* 13869 * Always initialize tcp_pipe 13870 * even though we don't have 13871 * any SACK info. If later 13872 * we get SACK info and 13873 * tcp_pipe is not initialized, 13874 * funny things will happen. 13875 */ 13876 tcp->tcp_pipe = 13877 tcp->tcp_cwnd_ssthresh; 13878 } 13879 } else { 13880 flags |= TH_REXMIT_NEEDED; 13881 } /* tcp_snd_sack_ok */ 13882 13883 } else { 13884 /* 13885 * Here we perform congestion 13886 * avoidance, but NOT slow start. 13887 * This is known as the Fast 13888 * Recovery Algorithm. 13889 */ 13890 if (tcp->tcp_snd_sack_ok && 13891 tcp->tcp_notsack_list != NULL) { 13892 flags |= TH_NEED_SACK_REXMIT; 13893 tcp->tcp_pipe -= mss; 13894 if (tcp->tcp_pipe < 0) 13895 tcp->tcp_pipe = 0; 13896 } else { 13897 /* 13898 * We know that one more packet has 13899 * left the pipe thus we can update 13900 * cwnd. 13901 */ 13902 cwnd = tcp->tcp_cwnd + mss; 13903 if (cwnd > tcp->tcp_cwnd_max) 13904 cwnd = tcp->tcp_cwnd_max; 13905 tcp->tcp_cwnd = cwnd; 13906 if (tcp->tcp_unsent > 0) 13907 flags |= TH_XMIT_NEEDED; 13908 } 13909 } 13910 } 13911 } else if (tcp->tcp_zero_win_probe) { 13912 /* 13913 * If the window has opened, need to arrange 13914 * to send additional data. 13915 */ 13916 if (new_swnd != 0) { 13917 /* tcp_suna != tcp_snxt */ 13918 /* Packet contains a window update */ 13919 BUMP_MIB(&tcp_mib, tcpInWinUpdate); 13920 tcp->tcp_zero_win_probe = 0; 13921 tcp->tcp_timer_backoff = 0; 13922 tcp->tcp_ms_we_have_waited = 0; 13923 13924 /* 13925 * Transmit starting with tcp_suna since 13926 * the one byte probe is not ack'ed. 13927 * If TCP has sent more than one identical 13928 * probe, tcp_rexmit will be set. That means 13929 * tcp_ss_rexmit() will send out the one 13930 * byte along with new data. Otherwise, 13931 * fake the retransmission. 13932 */ 13933 flags |= TH_XMIT_NEEDED; 13934 if (!tcp->tcp_rexmit) { 13935 tcp->tcp_rexmit = B_TRUE; 13936 tcp->tcp_dupack_cnt = 0; 13937 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 13938 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 13939 } 13940 } 13941 } 13942 goto swnd_update; 13943 } 13944 13945 /* 13946 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 13947 * If the ACK value acks something that we have not yet sent, it might 13948 * be an old duplicate segment. Send an ACK to re-synchronize the 13949 * other side. 13950 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 13951 * state is handled above, so we can always just drop the segment and 13952 * send an ACK here. 13953 * 13954 * Should we send ACKs in response to ACK only segments? 13955 */ 13956 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13957 BUMP_MIB(&tcp_mib, tcpInAckUnsent); 13958 /* drop the received segment */ 13959 freemsg(mp); 13960 13961 /* 13962 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 13963 * greater than 0, check if the number of such 13964 * bogus ACks is greater than that count. If yes, 13965 * don't send back any ACK. This prevents TCP from 13966 * getting into an ACK storm if somehow an attacker 13967 * successfully spoofs an acceptable segment to our 13968 * peer. 13969 */ 13970 if (tcp_drop_ack_unsent_cnt > 0 && 13971 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 13972 TCP_STAT(tcp_in_ack_unsent_drop); 13973 return; 13974 } 13975 mp = tcp_ack_mp(tcp); 13976 if (mp != NULL) { 13977 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 13978 BUMP_LOCAL(tcp->tcp_obsegs); 13979 BUMP_MIB(&tcp_mib, tcpOutAck); 13980 tcp_send_data(tcp, tcp->tcp_wq, mp); 13981 } 13982 return; 13983 } 13984 13985 /* 13986 * TCP gets a new ACK, update the notsack'ed list to delete those 13987 * blocks that are covered by this ACK. 13988 */ 13989 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 13990 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 13991 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 13992 } 13993 13994 /* 13995 * If we got an ACK after fast retransmit, check to see 13996 * if it is a partial ACK. If it is not and the congestion 13997 * window was inflated to account for the other side's 13998 * cached packets, retract it. If it is, do Hoe's algorithm. 13999 */ 14000 if (tcp->tcp_dupack_cnt >= tcp_dupack_fast_retransmit) { 14001 ASSERT(tcp->tcp_rexmit == B_FALSE); 14002 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14003 tcp->tcp_dupack_cnt = 0; 14004 /* 14005 * Restore the orig tcp_cwnd_ssthresh after 14006 * fast retransmit phase. 14007 */ 14008 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14009 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14010 } 14011 tcp->tcp_rexmit_max = seg_ack; 14012 tcp->tcp_cwnd_cnt = 0; 14013 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14014 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14015 14016 /* 14017 * Remove all notsack info to avoid confusion with 14018 * the next fast retrasnmit/recovery phase. 14019 */ 14020 if (tcp->tcp_snd_sack_ok && 14021 tcp->tcp_notsack_list != NULL) { 14022 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14023 } 14024 } else { 14025 if (tcp->tcp_snd_sack_ok && 14026 tcp->tcp_notsack_list != NULL) { 14027 flags |= TH_NEED_SACK_REXMIT; 14028 tcp->tcp_pipe -= mss; 14029 if (tcp->tcp_pipe < 0) 14030 tcp->tcp_pipe = 0; 14031 } else { 14032 /* 14033 * Hoe's algorithm: 14034 * 14035 * Retransmit the unack'ed segment and 14036 * restart fast recovery. Note that we 14037 * need to scale back tcp_cwnd to the 14038 * original value when we started fast 14039 * recovery. This is to prevent overly 14040 * aggressive behaviour in sending new 14041 * segments. 14042 */ 14043 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14044 tcp_dupack_fast_retransmit * mss; 14045 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14046 flags |= TH_REXMIT_NEEDED; 14047 } 14048 } 14049 } else { 14050 tcp->tcp_dupack_cnt = 0; 14051 if (tcp->tcp_rexmit) { 14052 /* 14053 * TCP is retranmitting. If the ACK ack's all 14054 * outstanding data, update tcp_rexmit_max and 14055 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14056 * to the correct value. 14057 * 14058 * Note that SEQ_LEQ() is used. This is to avoid 14059 * unnecessary fast retransmit caused by dup ACKs 14060 * received when TCP does slow start retransmission 14061 * after a time out. During this phase, TCP may 14062 * send out segments which are already received. 14063 * This causes dup ACKs to be sent back. 14064 */ 14065 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14066 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14067 tcp->tcp_rexmit_nxt = seg_ack; 14068 } 14069 if (seg_ack != tcp->tcp_rexmit_max) { 14070 flags |= TH_XMIT_NEEDED; 14071 } 14072 } else { 14073 tcp->tcp_rexmit = B_FALSE; 14074 tcp->tcp_xmit_zc_clean = B_FALSE; 14075 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14076 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14077 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14078 } 14079 tcp->tcp_ms_we_have_waited = 0; 14080 } 14081 } 14082 14083 BUMP_MIB(&tcp_mib, tcpInAckSegs); 14084 UPDATE_MIB(&tcp_mib, tcpInAckBytes, bytes_acked); 14085 tcp->tcp_suna = seg_ack; 14086 if (tcp->tcp_zero_win_probe != 0) { 14087 tcp->tcp_zero_win_probe = 0; 14088 tcp->tcp_timer_backoff = 0; 14089 } 14090 14091 /* 14092 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14093 * Note that it cannot be the SYN being ack'ed. The code flow 14094 * will not reach here. 14095 */ 14096 if (mp1 == NULL) { 14097 goto fin_acked; 14098 } 14099 14100 /* 14101 * Update the congestion window. 14102 * 14103 * If TCP is not ECN capable or TCP is ECN capable but the 14104 * congestion experience bit is not set, increase the tcp_cwnd as 14105 * usual. 14106 */ 14107 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14108 cwnd = tcp->tcp_cwnd; 14109 add = mss; 14110 14111 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14112 /* 14113 * This is to prevent an increase of less than 1 MSS of 14114 * tcp_cwnd. With partial increase, tcp_wput_data() 14115 * may send out tinygrams in order to preserve mblk 14116 * boundaries. 14117 * 14118 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14119 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14120 * increased by 1 MSS for every RTTs. 14121 */ 14122 if (tcp->tcp_cwnd_cnt <= 0) { 14123 tcp->tcp_cwnd_cnt = cwnd + add; 14124 } else { 14125 tcp->tcp_cwnd_cnt -= add; 14126 add = 0; 14127 } 14128 } 14129 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14130 } 14131 14132 /* See if the latest urgent data has been acknowledged */ 14133 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14134 SEQ_GT(seg_ack, tcp->tcp_urg)) 14135 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14136 14137 /* Can we update the RTT estimates? */ 14138 if (tcp->tcp_snd_ts_ok) { 14139 /* Ignore zero timestamp echo-reply. */ 14140 if (tcpopt.tcp_opt_ts_ecr != 0) { 14141 tcp_set_rto(tcp, (int32_t)lbolt - 14142 (int32_t)tcpopt.tcp_opt_ts_ecr); 14143 } 14144 14145 /* If needed, restart the timer. */ 14146 if (tcp->tcp_set_timer == 1) { 14147 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14148 tcp->tcp_set_timer = 0; 14149 } 14150 /* 14151 * Update tcp_csuna in case the other side stops sending 14152 * us timestamps. 14153 */ 14154 tcp->tcp_csuna = tcp->tcp_snxt; 14155 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14156 /* 14157 * An ACK sequence we haven't seen before, so get the RTT 14158 * and update the RTO. But first check if the timestamp is 14159 * valid to use. 14160 */ 14161 if ((mp1->b_next != NULL) && 14162 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14163 tcp_set_rto(tcp, (int32_t)lbolt - 14164 (int32_t)(intptr_t)mp1->b_prev); 14165 else 14166 BUMP_MIB(&tcp_mib, tcpRttNoUpdate); 14167 14168 /* Remeber the last sequence to be ACKed */ 14169 tcp->tcp_csuna = seg_ack; 14170 if (tcp->tcp_set_timer == 1) { 14171 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14172 tcp->tcp_set_timer = 0; 14173 } 14174 } else { 14175 BUMP_MIB(&tcp_mib, tcpRttNoUpdate); 14176 } 14177 14178 /* Eat acknowledged bytes off the xmit queue. */ 14179 for (;;) { 14180 mblk_t *mp2; 14181 uchar_t *wptr; 14182 14183 wptr = mp1->b_wptr; 14184 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14185 bytes_acked -= (int)(wptr - mp1->b_rptr); 14186 if (bytes_acked < 0) { 14187 mp1->b_rptr = wptr + bytes_acked; 14188 /* 14189 * Set a new timestamp if all the bytes timed by the 14190 * old timestamp have been ack'ed. 14191 */ 14192 if (SEQ_GT(seg_ack, 14193 (uint32_t)(uintptr_t)(mp1->b_next))) { 14194 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14195 mp1->b_next = NULL; 14196 } 14197 break; 14198 } 14199 mp1->b_next = NULL; 14200 mp1->b_prev = NULL; 14201 mp2 = mp1; 14202 mp1 = mp1->b_cont; 14203 14204 /* 14205 * This notification is required for some zero-copy 14206 * clients to maintain a copy semantic. After the data 14207 * is ack'ed, client is safe to modify or reuse the buffer. 14208 */ 14209 if (tcp->tcp_snd_zcopy_aware && 14210 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14211 tcp_zcopy_notify(tcp); 14212 freeb(mp2); 14213 if (bytes_acked == 0) { 14214 if (mp1 == NULL) { 14215 /* Everything is ack'ed, clear the tail. */ 14216 tcp->tcp_xmit_tail = NULL; 14217 /* 14218 * Cancel the timer unless we are still 14219 * waiting for an ACK for the FIN packet. 14220 */ 14221 if (tcp->tcp_timer_tid != 0 && 14222 tcp->tcp_snxt == tcp->tcp_suna) { 14223 (void) TCP_TIMER_CANCEL(tcp, 14224 tcp->tcp_timer_tid); 14225 tcp->tcp_timer_tid = 0; 14226 } 14227 goto pre_swnd_update; 14228 } 14229 if (mp2 != tcp->tcp_xmit_tail) 14230 break; 14231 tcp->tcp_xmit_tail = mp1; 14232 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14233 (uintptr_t)INT_MAX); 14234 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14235 mp1->b_rptr); 14236 break; 14237 } 14238 if (mp1 == NULL) { 14239 /* 14240 * More was acked but there is nothing more 14241 * outstanding. This means that the FIN was 14242 * just acked or that we're talking to a clown. 14243 */ 14244 fin_acked: 14245 ASSERT(tcp->tcp_fin_sent); 14246 tcp->tcp_xmit_tail = NULL; 14247 if (tcp->tcp_fin_sent) { 14248 /* FIN was acked - making progress */ 14249 if (tcp->tcp_ipversion == IPV6_VERSION && 14250 !tcp->tcp_fin_acked) 14251 tcp->tcp_ip_forward_progress = B_TRUE; 14252 tcp->tcp_fin_acked = B_TRUE; 14253 if (tcp->tcp_linger_tid != 0 && 14254 TCP_TIMER_CANCEL(tcp, 14255 tcp->tcp_linger_tid) >= 0) { 14256 tcp_stop_lingering(tcp); 14257 } 14258 } else { 14259 /* 14260 * We should never get here because 14261 * we have already checked that the 14262 * number of bytes ack'ed should be 14263 * smaller than or equal to what we 14264 * have sent so far (it is the 14265 * acceptability check of the ACK). 14266 * We can only get here if the send 14267 * queue is corrupted. 14268 * 14269 * Terminate the connection and 14270 * panic the system. It is better 14271 * for us to panic instead of 14272 * continuing to avoid other disaster. 14273 */ 14274 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14275 tcp->tcp_rnxt, TH_RST|TH_ACK); 14276 panic("Memory corruption " 14277 "detected for connection %s.", 14278 tcp_display(tcp, NULL, 14279 DISP_ADDR_AND_PORT)); 14280 /*NOTREACHED*/ 14281 } 14282 goto pre_swnd_update; 14283 } 14284 ASSERT(mp2 != tcp->tcp_xmit_tail); 14285 } 14286 if (tcp->tcp_unsent) { 14287 flags |= TH_XMIT_NEEDED; 14288 } 14289 pre_swnd_update: 14290 tcp->tcp_xmit_head = mp1; 14291 swnd_update: 14292 /* 14293 * The following check is different from most other implementations. 14294 * For bi-directional transfer, when segments are dropped, the 14295 * "normal" check will not accept a window update in those 14296 * retransmitted segemnts. Failing to do that, TCP may send out 14297 * segments which are outside receiver's window. As TCP accepts 14298 * the ack in those retransmitted segments, if the window update in 14299 * the same segment is not accepted, TCP will incorrectly calculates 14300 * that it can send more segments. This can create a deadlock 14301 * with the receiver if its window becomes zero. 14302 */ 14303 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14304 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14305 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14306 /* 14307 * The criteria for update is: 14308 * 14309 * 1. the segment acknowledges some data. Or 14310 * 2. the segment is new, i.e. it has a higher seq num. Or 14311 * 3. the segment is not old and the advertised window is 14312 * larger than the previous advertised window. 14313 */ 14314 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14315 flags |= TH_XMIT_NEEDED; 14316 tcp->tcp_swnd = new_swnd; 14317 if (new_swnd > tcp->tcp_max_swnd) 14318 tcp->tcp_max_swnd = new_swnd; 14319 tcp->tcp_swl1 = seg_seq; 14320 tcp->tcp_swl2 = seg_ack; 14321 } 14322 est: 14323 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14324 14325 switch (tcp->tcp_state) { 14326 case TCPS_FIN_WAIT_1: 14327 if (tcp->tcp_fin_acked) { 14328 tcp->tcp_state = TCPS_FIN_WAIT_2; 14329 /* 14330 * We implement the non-standard BSD/SunOS 14331 * FIN_WAIT_2 flushing algorithm. 14332 * If there is no user attached to this 14333 * TCP endpoint, then this TCP struct 14334 * could hang around forever in FIN_WAIT_2 14335 * state if the peer forgets to send us 14336 * a FIN. To prevent this, we wait only 14337 * 2*MSL (a convenient time value) for 14338 * the FIN to arrive. If it doesn't show up, 14339 * we flush the TCP endpoint. This algorithm, 14340 * though a violation of RFC-793, has worked 14341 * for over 10 years in BSD systems. 14342 * Note: SunOS 4.x waits 675 seconds before 14343 * flushing the FIN_WAIT_2 connection. 14344 */ 14345 TCP_TIMER_RESTART(tcp, 14346 tcp_fin_wait_2_flush_interval); 14347 } 14348 break; 14349 case TCPS_FIN_WAIT_2: 14350 break; /* Shutdown hook? */ 14351 case TCPS_LAST_ACK: 14352 freemsg(mp); 14353 if (tcp->tcp_fin_acked) { 14354 (void) tcp_clean_death(tcp, 0, 19); 14355 return; 14356 } 14357 goto xmit_check; 14358 case TCPS_CLOSING: 14359 if (tcp->tcp_fin_acked) { 14360 tcp->tcp_state = TCPS_TIME_WAIT; 14361 /* 14362 * Unconditionally clear the exclusive binding 14363 * bit so this TIME-WAIT connection won't 14364 * interfere with new ones. 14365 */ 14366 tcp->tcp_exclbind = 0; 14367 if (!TCP_IS_DETACHED(tcp)) { 14368 TCP_TIMER_RESTART(tcp, 14369 tcp_time_wait_interval); 14370 } else { 14371 tcp_time_wait_append(tcp); 14372 TCP_DBGSTAT(tcp_rput_time_wait); 14373 } 14374 } 14375 /*FALLTHRU*/ 14376 case TCPS_CLOSE_WAIT: 14377 freemsg(mp); 14378 goto xmit_check; 14379 default: 14380 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14381 break; 14382 } 14383 } 14384 if (flags & TH_FIN) { 14385 /* Make sure we ack the fin */ 14386 flags |= TH_ACK_NEEDED; 14387 if (!tcp->tcp_fin_rcvd) { 14388 tcp->tcp_fin_rcvd = B_TRUE; 14389 tcp->tcp_rnxt++; 14390 tcph = tcp->tcp_tcph; 14391 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14392 14393 /* 14394 * Generate the ordrel_ind at the end unless we 14395 * are an eager guy. 14396 * In the eager case tcp_rsrv will do this when run 14397 * after tcp_accept is done. 14398 */ 14399 if (tcp->tcp_listener == NULL && 14400 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14401 flags |= TH_ORDREL_NEEDED; 14402 switch (tcp->tcp_state) { 14403 case TCPS_SYN_RCVD: 14404 case TCPS_ESTABLISHED: 14405 tcp->tcp_state = TCPS_CLOSE_WAIT; 14406 /* Keepalive? */ 14407 break; 14408 case TCPS_FIN_WAIT_1: 14409 if (!tcp->tcp_fin_acked) { 14410 tcp->tcp_state = TCPS_CLOSING; 14411 break; 14412 } 14413 /* FALLTHRU */ 14414 case TCPS_FIN_WAIT_2: 14415 tcp->tcp_state = TCPS_TIME_WAIT; 14416 /* 14417 * Unconditionally clear the exclusive binding 14418 * bit so this TIME-WAIT connection won't 14419 * interfere with new ones. 14420 */ 14421 tcp->tcp_exclbind = 0; 14422 if (!TCP_IS_DETACHED(tcp)) { 14423 TCP_TIMER_RESTART(tcp, 14424 tcp_time_wait_interval); 14425 } else { 14426 tcp_time_wait_append(tcp); 14427 TCP_DBGSTAT(tcp_rput_time_wait); 14428 } 14429 if (seg_len) { 14430 /* 14431 * implies data piggybacked on FIN. 14432 * break to handle data. 14433 */ 14434 break; 14435 } 14436 freemsg(mp); 14437 goto ack_check; 14438 } 14439 } 14440 } 14441 if (mp == NULL) 14442 goto xmit_check; 14443 if (seg_len == 0) { 14444 freemsg(mp); 14445 goto xmit_check; 14446 } 14447 if (mp->b_rptr == mp->b_wptr) { 14448 /* 14449 * The header has been consumed, so we remove the 14450 * zero-length mblk here. 14451 */ 14452 mp1 = mp; 14453 mp = mp->b_cont; 14454 freeb(mp1); 14455 } 14456 tcph = tcp->tcp_tcph; 14457 tcp->tcp_rack_cnt++; 14458 { 14459 uint32_t cur_max; 14460 14461 cur_max = tcp->tcp_rack_cur_max; 14462 if (tcp->tcp_rack_cnt >= cur_max) { 14463 /* 14464 * We have more unacked data than we should - send 14465 * an ACK now. 14466 */ 14467 flags |= TH_ACK_NEEDED; 14468 cur_max++; 14469 if (cur_max > tcp->tcp_rack_abs_max) 14470 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14471 else 14472 tcp->tcp_rack_cur_max = cur_max; 14473 } else if (TCP_IS_DETACHED(tcp)) { 14474 /* We don't have an ACK timer for detached TCP. */ 14475 flags |= TH_ACK_NEEDED; 14476 } else if (seg_len < mss) { 14477 /* 14478 * If we get a segment that is less than an mss, and we 14479 * already have unacknowledged data, and the amount 14480 * unacknowledged is not a multiple of mss, then we 14481 * better generate an ACK now. Otherwise, this may be 14482 * the tail piece of a transaction, and we would rather 14483 * wait for the response. 14484 */ 14485 uint32_t udif; 14486 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14487 (uintptr_t)INT_MAX); 14488 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14489 if (udif && (udif % mss)) 14490 flags |= TH_ACK_NEEDED; 14491 else 14492 flags |= TH_ACK_TIMER_NEEDED; 14493 } else { 14494 /* Start delayed ack timer */ 14495 flags |= TH_ACK_TIMER_NEEDED; 14496 } 14497 } 14498 tcp->tcp_rnxt += seg_len; 14499 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14500 14501 /* Update SACK list */ 14502 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14503 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14504 &(tcp->tcp_num_sack_blk)); 14505 } 14506 14507 if (tcp->tcp_urp_mp) { 14508 tcp->tcp_urp_mp->b_cont = mp; 14509 mp = tcp->tcp_urp_mp; 14510 tcp->tcp_urp_mp = NULL; 14511 /* Ready for a new signal. */ 14512 tcp->tcp_urp_last_valid = B_FALSE; 14513 #ifdef DEBUG 14514 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14515 "tcp_rput: sending exdata_ind %s", 14516 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14517 #endif /* DEBUG */ 14518 } 14519 14520 /* 14521 * Check for ancillary data changes compared to last segment. 14522 */ 14523 if (tcp->tcp_ipv6_recvancillary != 0) { 14524 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14525 if (mp == NULL) 14526 return; 14527 } 14528 14529 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14530 /* 14531 * Side queue inbound data until the accept happens. 14532 * tcp_accept/tcp_rput drains this when the accept happens. 14533 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14534 * T_EXDATA_IND) it is queued on b_next. 14535 * XXX Make urgent data use this. Requires: 14536 * Removing tcp_listener check for TH_URG 14537 * Making M_PCPROTO and MARK messages skip the eager case 14538 */ 14539 14540 if (tcp->tcp_kssl_pending) { 14541 tcp_kssl_input(tcp, mp); 14542 } else { 14543 tcp_rcv_enqueue(tcp, mp, seg_len); 14544 } 14545 } else { 14546 if (mp->b_datap->db_type != M_DATA || 14547 (flags & TH_MARKNEXT_NEEDED)) { 14548 if (tcp->tcp_rcv_list != NULL) { 14549 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14550 } 14551 ASSERT(tcp->tcp_rcv_list == NULL || 14552 tcp->tcp_fused_sigurg); 14553 if (flags & TH_MARKNEXT_NEEDED) { 14554 #ifdef DEBUG 14555 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14556 "tcp_rput: sending MSGMARKNEXT %s", 14557 tcp_display(tcp, NULL, 14558 DISP_PORT_ONLY)); 14559 #endif /* DEBUG */ 14560 mp->b_flag |= MSGMARKNEXT; 14561 flags &= ~TH_MARKNEXT_NEEDED; 14562 } 14563 14564 /* Does this need SSL processing first? */ 14565 if ((tcp->tcp_kssl_ctx != NULL) && 14566 (DB_TYPE(mp) == M_DATA)) { 14567 tcp_kssl_input(tcp, mp); 14568 } else { 14569 putnext(tcp->tcp_rq, mp); 14570 if (!canputnext(tcp->tcp_rq)) 14571 tcp->tcp_rwnd -= seg_len; 14572 } 14573 } else if ((flags & (TH_PUSH|TH_FIN)) || 14574 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 14575 if (tcp->tcp_rcv_list != NULL) { 14576 /* 14577 * Enqueue the new segment first and then 14578 * call tcp_rcv_drain() to send all data 14579 * up. The other way to do this is to 14580 * send all queued data up and then call 14581 * putnext() to send the new segment up. 14582 * This way can remove the else part later 14583 * on. 14584 * 14585 * We don't this to avoid one more call to 14586 * canputnext() as tcp_rcv_drain() needs to 14587 * call canputnext(). 14588 */ 14589 tcp_rcv_enqueue(tcp, mp, seg_len); 14590 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14591 } else { 14592 /* Does this need SSL processing first? */ 14593 if ((tcp->tcp_kssl_ctx != NULL) && 14594 (DB_TYPE(mp) == M_DATA)) { 14595 tcp_kssl_input(tcp, mp); 14596 } else { 14597 putnext(tcp->tcp_rq, mp); 14598 if (!canputnext(tcp->tcp_rq)) 14599 tcp->tcp_rwnd -= seg_len; 14600 } 14601 } 14602 } else { 14603 /* 14604 * Enqueue all packets when processing an mblk 14605 * from the co queue and also enqueue normal packets. 14606 */ 14607 tcp_rcv_enqueue(tcp, mp, seg_len); 14608 } 14609 /* 14610 * Make sure the timer is running if we have data waiting 14611 * for a push bit. This provides resiliency against 14612 * implementations that do not correctly generate push bits. 14613 */ 14614 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 14615 /* 14616 * The connection may be closed at this point, so don't 14617 * do anything for a detached tcp. 14618 */ 14619 if (!TCP_IS_DETACHED(tcp)) 14620 tcp->tcp_push_tid = TCP_TIMER(tcp, 14621 tcp_push_timer, 14622 MSEC_TO_TICK(tcp_push_timer_interval)); 14623 } 14624 } 14625 xmit_check: 14626 /* Is there anything left to do? */ 14627 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14628 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 14629 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 14630 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14631 goto done; 14632 14633 /* Any transmit work to do and a non-zero window? */ 14634 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 14635 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 14636 if (flags & TH_REXMIT_NEEDED) { 14637 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 14638 14639 BUMP_MIB(&tcp_mib, tcpOutFastRetrans); 14640 if (snd_size > mss) 14641 snd_size = mss; 14642 if (snd_size > tcp->tcp_swnd) 14643 snd_size = tcp->tcp_swnd; 14644 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 14645 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 14646 B_TRUE); 14647 14648 if (mp1 != NULL) { 14649 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14650 tcp->tcp_csuna = tcp->tcp_snxt; 14651 BUMP_MIB(&tcp_mib, tcpRetransSegs); 14652 UPDATE_MIB(&tcp_mib, tcpRetransBytes, snd_size); 14653 TCP_RECORD_TRACE(tcp, mp1, 14654 TCP_TRACE_SEND_PKT); 14655 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14656 } 14657 } 14658 if (flags & TH_NEED_SACK_REXMIT) { 14659 tcp_sack_rxmit(tcp, &flags); 14660 } 14661 /* 14662 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 14663 * out new segment. Note that tcp_rexmit should not be 14664 * set, otherwise TH_LIMIT_XMIT should not be set. 14665 */ 14666 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 14667 if (!tcp->tcp_rexmit) { 14668 tcp_wput_data(tcp, NULL, B_FALSE); 14669 } else { 14670 tcp_ss_rexmit(tcp); 14671 } 14672 } 14673 /* 14674 * Adjust tcp_cwnd back to normal value after sending 14675 * new data segments. 14676 */ 14677 if (flags & TH_LIMIT_XMIT) { 14678 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 14679 /* 14680 * This will restart the timer. Restarting the 14681 * timer is used to avoid a timeout before the 14682 * limited transmitted segment's ACK gets back. 14683 */ 14684 if (tcp->tcp_xmit_head != NULL) 14685 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14686 } 14687 14688 /* Anything more to do? */ 14689 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 14690 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14691 goto done; 14692 } 14693 ack_check: 14694 if (flags & TH_SEND_URP_MARK) { 14695 ASSERT(tcp->tcp_urp_mark_mp); 14696 /* 14697 * Send up any queued data and then send the mark message 14698 */ 14699 if (tcp->tcp_rcv_list != NULL) { 14700 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14701 } 14702 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14703 14704 mp1 = tcp->tcp_urp_mark_mp; 14705 tcp->tcp_urp_mark_mp = NULL; 14706 #ifdef DEBUG 14707 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14708 "tcp_rput: sending zero-length %s %s", 14709 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 14710 "MSGNOTMARKNEXT"), 14711 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14712 #endif /* DEBUG */ 14713 putnext(tcp->tcp_rq, mp1); 14714 flags &= ~TH_SEND_URP_MARK; 14715 } 14716 if (flags & TH_ACK_NEEDED) { 14717 /* 14718 * Time to send an ack for some reason. 14719 */ 14720 mp1 = tcp_ack_mp(tcp); 14721 14722 if (mp1 != NULL) { 14723 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 14724 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14725 BUMP_LOCAL(tcp->tcp_obsegs); 14726 BUMP_MIB(&tcp_mib, tcpOutAck); 14727 } 14728 if (tcp->tcp_ack_tid != 0) { 14729 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 14730 tcp->tcp_ack_tid = 0; 14731 } 14732 } 14733 if (flags & TH_ACK_TIMER_NEEDED) { 14734 /* 14735 * Arrange for deferred ACK or push wait timeout. 14736 * Start timer if it is not already running. 14737 */ 14738 if (tcp->tcp_ack_tid == 0) { 14739 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 14740 MSEC_TO_TICK(tcp->tcp_localnet ? 14741 (clock_t)tcp_local_dack_interval : 14742 (clock_t)tcp_deferred_ack_interval)); 14743 } 14744 } 14745 if (flags & TH_ORDREL_NEEDED) { 14746 /* 14747 * Send up the ordrel_ind unless we are an eager guy. 14748 * In the eager case tcp_rsrv will do this when run 14749 * after tcp_accept is done. 14750 */ 14751 ASSERT(tcp->tcp_listener == NULL); 14752 if (tcp->tcp_rcv_list != NULL) { 14753 /* 14754 * Push any mblk(s) enqueued from co processing. 14755 */ 14756 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14757 } 14758 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14759 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 14760 tcp->tcp_ordrel_done = B_TRUE; 14761 putnext(tcp->tcp_rq, mp1); 14762 if (tcp->tcp_deferred_clean_death) { 14763 /* 14764 * tcp_clean_death was deferred 14765 * for T_ORDREL_IND - do it now 14766 */ 14767 (void) tcp_clean_death(tcp, 14768 tcp->tcp_client_errno, 20); 14769 tcp->tcp_deferred_clean_death = B_FALSE; 14770 } 14771 } else { 14772 /* 14773 * Run the orderly release in the 14774 * service routine. 14775 */ 14776 qenable(tcp->tcp_rq); 14777 /* 14778 * Caveat(XXX): The machine may be so 14779 * overloaded that tcp_rsrv() is not scheduled 14780 * until after the endpoint has transitioned 14781 * to TCPS_TIME_WAIT 14782 * and tcp_time_wait_interval expires. Then 14783 * tcp_timer() will blow away state in tcp_t 14784 * and T_ORDREL_IND will never be delivered 14785 * upstream. Unlikely but potentially 14786 * a problem. 14787 */ 14788 } 14789 } 14790 done: 14791 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14792 } 14793 14794 /* 14795 * This function does PAWS protection check. Returns B_TRUE if the 14796 * segment passes the PAWS test, else returns B_FALSE. 14797 */ 14798 boolean_t 14799 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 14800 { 14801 uint8_t flags; 14802 int options; 14803 uint8_t *up; 14804 14805 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 14806 /* 14807 * If timestamp option is aligned nicely, get values inline, 14808 * otherwise call general routine to parse. Only do that 14809 * if timestamp is the only option. 14810 */ 14811 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 14812 TCPOPT_REAL_TS_LEN && 14813 OK_32PTR((up = ((uint8_t *)tcph) + 14814 TCP_MIN_HEADER_LENGTH)) && 14815 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 14816 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 14817 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 14818 14819 options = TCP_OPT_TSTAMP_PRESENT; 14820 } else { 14821 if (tcp->tcp_snd_sack_ok) { 14822 tcpoptp->tcp = tcp; 14823 } else { 14824 tcpoptp->tcp = NULL; 14825 } 14826 options = tcp_parse_options(tcph, tcpoptp); 14827 } 14828 14829 if (options & TCP_OPT_TSTAMP_PRESENT) { 14830 /* 14831 * Do PAWS per RFC 1323 section 4.2. Accept RST 14832 * regardless of the timestamp, page 18 RFC 1323.bis. 14833 */ 14834 if ((flags & TH_RST) == 0 && 14835 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 14836 tcp->tcp_ts_recent)) { 14837 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 14838 PAWS_TIMEOUT)) { 14839 /* This segment is not acceptable. */ 14840 return (B_FALSE); 14841 } else { 14842 /* 14843 * Connection has been idle for 14844 * too long. Reset the timestamp 14845 * and assume the segment is valid. 14846 */ 14847 tcp->tcp_ts_recent = 14848 tcpoptp->tcp_opt_ts_val; 14849 } 14850 } 14851 } else { 14852 /* 14853 * If we don't get a timestamp on every packet, we 14854 * figure we can't really trust 'em, so we stop sending 14855 * and parsing them. 14856 */ 14857 tcp->tcp_snd_ts_ok = B_FALSE; 14858 14859 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 14860 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 14861 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 14862 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN); 14863 if (tcp->tcp_snd_sack_ok) { 14864 ASSERT(tcp->tcp_sack_info != NULL); 14865 tcp->tcp_max_sack_blk = 4; 14866 } 14867 } 14868 return (B_TRUE); 14869 } 14870 14871 /* 14872 * Attach ancillary data to a received TCP segments for the 14873 * ancillary pieces requested by the application that are 14874 * different than they were in the previous data segment. 14875 * 14876 * Save the "current" values once memory allocation is ok so that 14877 * when memory allocation fails we can just wait for the next data segment. 14878 */ 14879 static mblk_t * 14880 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 14881 { 14882 struct T_optdata_ind *todi; 14883 int optlen; 14884 uchar_t *optptr; 14885 struct T_opthdr *toh; 14886 uint_t addflag; /* Which pieces to add */ 14887 mblk_t *mp1; 14888 14889 optlen = 0; 14890 addflag = 0; 14891 /* If app asked for pktinfo and the index has changed ... */ 14892 if ((ipp->ipp_fields & IPPF_IFINDEX) && 14893 ipp->ipp_ifindex != tcp->tcp_recvifindex && 14894 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 14895 optlen += sizeof (struct T_opthdr) + 14896 sizeof (struct in6_pktinfo); 14897 addflag |= TCP_IPV6_RECVPKTINFO; 14898 } 14899 /* If app asked for hoplimit and it has changed ... */ 14900 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 14901 ipp->ipp_hoplimit != tcp->tcp_recvhops && 14902 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 14903 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 14904 addflag |= TCP_IPV6_RECVHOPLIMIT; 14905 } 14906 /* If app asked for tclass and it has changed ... */ 14907 if ((ipp->ipp_fields & IPPF_TCLASS) && 14908 ipp->ipp_tclass != tcp->tcp_recvtclass && 14909 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 14910 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 14911 addflag |= TCP_IPV6_RECVTCLASS; 14912 } 14913 /* 14914 * If app asked for hopbyhop headers and it has changed ... 14915 * For security labels, note that (1) security labels can't change on 14916 * a connected socket at all, (2) we're connected to at most one peer, 14917 * (3) if anything changes, then it must be some other extra option. 14918 */ 14919 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 14920 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 14921 (ipp->ipp_fields & IPPF_HOPOPTS), 14922 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 14923 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 14924 tcp->tcp_label_len; 14925 addflag |= TCP_IPV6_RECVHOPOPTS; 14926 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 14927 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 14928 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 14929 return (mp); 14930 } 14931 /* If app asked for dst headers before routing headers ... */ 14932 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 14933 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 14934 (ipp->ipp_fields & IPPF_RTDSTOPTS), 14935 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 14936 optlen += sizeof (struct T_opthdr) + 14937 ipp->ipp_rtdstoptslen; 14938 addflag |= TCP_IPV6_RECVRTDSTOPTS; 14939 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 14940 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 14941 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 14942 return (mp); 14943 } 14944 /* If app asked for routing headers and it has changed ... */ 14945 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 14946 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 14947 (ipp->ipp_fields & IPPF_RTHDR), 14948 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 14949 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 14950 addflag |= TCP_IPV6_RECVRTHDR; 14951 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 14952 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 14953 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 14954 return (mp); 14955 } 14956 /* If app asked for dest headers and it has changed ... */ 14957 if ((tcp->tcp_ipv6_recvancillary & 14958 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 14959 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 14960 (ipp->ipp_fields & IPPF_DSTOPTS), 14961 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 14962 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 14963 addflag |= TCP_IPV6_RECVDSTOPTS; 14964 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 14965 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 14966 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 14967 return (mp); 14968 } 14969 14970 if (optlen == 0) { 14971 /* Nothing to add */ 14972 return (mp); 14973 } 14974 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 14975 if (mp1 == NULL) { 14976 /* 14977 * Defer sending ancillary data until the next TCP segment 14978 * arrives. 14979 */ 14980 return (mp); 14981 } 14982 mp1->b_cont = mp; 14983 mp = mp1; 14984 mp->b_wptr += sizeof (*todi) + optlen; 14985 mp->b_datap->db_type = M_PROTO; 14986 todi = (struct T_optdata_ind *)mp->b_rptr; 14987 todi->PRIM_type = T_OPTDATA_IND; 14988 todi->DATA_flag = 1; /* MORE data */ 14989 todi->OPT_length = optlen; 14990 todi->OPT_offset = sizeof (*todi); 14991 optptr = (uchar_t *)&todi[1]; 14992 /* 14993 * If app asked for pktinfo and the index has changed ... 14994 * Note that the local address never changes for the connection. 14995 */ 14996 if (addflag & TCP_IPV6_RECVPKTINFO) { 14997 struct in6_pktinfo *pkti; 14998 14999 toh = (struct T_opthdr *)optptr; 15000 toh->level = IPPROTO_IPV6; 15001 toh->name = IPV6_PKTINFO; 15002 toh->len = sizeof (*toh) + sizeof (*pkti); 15003 toh->status = 0; 15004 optptr += sizeof (*toh); 15005 pkti = (struct in6_pktinfo *)optptr; 15006 if (tcp->tcp_ipversion == IPV6_VERSION) 15007 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15008 else 15009 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15010 &pkti->ipi6_addr); 15011 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15012 optptr += sizeof (*pkti); 15013 ASSERT(OK_32PTR(optptr)); 15014 /* Save as "last" value */ 15015 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15016 } 15017 /* If app asked for hoplimit and it has changed ... */ 15018 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15019 toh = (struct T_opthdr *)optptr; 15020 toh->level = IPPROTO_IPV6; 15021 toh->name = IPV6_HOPLIMIT; 15022 toh->len = sizeof (*toh) + sizeof (uint_t); 15023 toh->status = 0; 15024 optptr += sizeof (*toh); 15025 *(uint_t *)optptr = ipp->ipp_hoplimit; 15026 optptr += sizeof (uint_t); 15027 ASSERT(OK_32PTR(optptr)); 15028 /* Save as "last" value */ 15029 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15030 } 15031 /* If app asked for tclass and it has changed ... */ 15032 if (addflag & TCP_IPV6_RECVTCLASS) { 15033 toh = (struct T_opthdr *)optptr; 15034 toh->level = IPPROTO_IPV6; 15035 toh->name = IPV6_TCLASS; 15036 toh->len = sizeof (*toh) + sizeof (uint_t); 15037 toh->status = 0; 15038 optptr += sizeof (*toh); 15039 *(uint_t *)optptr = ipp->ipp_tclass; 15040 optptr += sizeof (uint_t); 15041 ASSERT(OK_32PTR(optptr)); 15042 /* Save as "last" value */ 15043 tcp->tcp_recvtclass = ipp->ipp_tclass; 15044 } 15045 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15046 toh = (struct T_opthdr *)optptr; 15047 toh->level = IPPROTO_IPV6; 15048 toh->name = IPV6_HOPOPTS; 15049 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15050 tcp->tcp_label_len; 15051 toh->status = 0; 15052 optptr += sizeof (*toh); 15053 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15054 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15055 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15056 ASSERT(OK_32PTR(optptr)); 15057 /* Save as last value */ 15058 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15059 (ipp->ipp_fields & IPPF_HOPOPTS), 15060 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15061 } 15062 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15063 toh = (struct T_opthdr *)optptr; 15064 toh->level = IPPROTO_IPV6; 15065 toh->name = IPV6_RTHDRDSTOPTS; 15066 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15067 toh->status = 0; 15068 optptr += sizeof (*toh); 15069 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15070 optptr += ipp->ipp_rtdstoptslen; 15071 ASSERT(OK_32PTR(optptr)); 15072 /* Save as last value */ 15073 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15074 &tcp->tcp_rtdstoptslen, 15075 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15076 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15077 } 15078 if (addflag & TCP_IPV6_RECVRTHDR) { 15079 toh = (struct T_opthdr *)optptr; 15080 toh->level = IPPROTO_IPV6; 15081 toh->name = IPV6_RTHDR; 15082 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15083 toh->status = 0; 15084 optptr += sizeof (*toh); 15085 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15086 optptr += ipp->ipp_rthdrlen; 15087 ASSERT(OK_32PTR(optptr)); 15088 /* Save as last value */ 15089 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15090 (ipp->ipp_fields & IPPF_RTHDR), 15091 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15092 } 15093 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15094 toh = (struct T_opthdr *)optptr; 15095 toh->level = IPPROTO_IPV6; 15096 toh->name = IPV6_DSTOPTS; 15097 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15098 toh->status = 0; 15099 optptr += sizeof (*toh); 15100 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15101 optptr += ipp->ipp_dstoptslen; 15102 ASSERT(OK_32PTR(optptr)); 15103 /* Save as last value */ 15104 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15105 (ipp->ipp_fields & IPPF_DSTOPTS), 15106 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15107 } 15108 ASSERT(optptr == mp->b_wptr); 15109 return (mp); 15110 } 15111 15112 15113 /* 15114 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15115 * or a "bad" IRE detected by tcp_adapt_ire. 15116 * We can't tell if the failure was due to the laddr or the faddr 15117 * thus we clear out all addresses and ports. 15118 */ 15119 static void 15120 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15121 { 15122 queue_t *q = tcp->tcp_rq; 15123 tcph_t *tcph; 15124 struct T_error_ack *tea; 15125 conn_t *connp = tcp->tcp_connp; 15126 15127 15128 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15129 15130 if (mp->b_cont) { 15131 freemsg(mp->b_cont); 15132 mp->b_cont = NULL; 15133 } 15134 tea = (struct T_error_ack *)mp->b_rptr; 15135 switch (tea->PRIM_type) { 15136 case T_BIND_ACK: 15137 /* 15138 * Need to unbind with classifier since we were just told that 15139 * our bind succeeded. 15140 */ 15141 tcp->tcp_hard_bound = B_FALSE; 15142 tcp->tcp_hard_binding = B_FALSE; 15143 15144 ipcl_hash_remove(connp); 15145 /* Reuse the mblk if possible */ 15146 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15147 sizeof (*tea)); 15148 mp->b_rptr = mp->b_datap->db_base; 15149 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15150 tea = (struct T_error_ack *)mp->b_rptr; 15151 tea->PRIM_type = T_ERROR_ACK; 15152 tea->TLI_error = TSYSERR; 15153 tea->UNIX_error = error; 15154 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15155 tea->ERROR_prim = T_CONN_REQ; 15156 } else { 15157 tea->ERROR_prim = O_T_BIND_REQ; 15158 } 15159 break; 15160 15161 case T_ERROR_ACK: 15162 if (tcp->tcp_state >= TCPS_SYN_SENT) 15163 tea->ERROR_prim = T_CONN_REQ; 15164 break; 15165 default: 15166 panic("tcp_bind_failed: unexpected TPI type"); 15167 /*NOTREACHED*/ 15168 } 15169 15170 tcp->tcp_state = TCPS_IDLE; 15171 if (tcp->tcp_ipversion == IPV4_VERSION) 15172 tcp->tcp_ipha->ipha_src = 0; 15173 else 15174 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15175 /* 15176 * Copy of the src addr. in tcp_t is needed since 15177 * the lookup funcs. can only look at tcp_t 15178 */ 15179 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15180 15181 tcph = tcp->tcp_tcph; 15182 tcph->th_lport[0] = 0; 15183 tcph->th_lport[1] = 0; 15184 tcp_bind_hash_remove(tcp); 15185 bzero(&connp->u_port, sizeof (connp->u_port)); 15186 /* blow away saved option results if any */ 15187 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15188 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15189 15190 conn_delete_ire(tcp->tcp_connp, NULL); 15191 putnext(q, mp); 15192 } 15193 15194 /* 15195 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15196 * messages. 15197 */ 15198 void 15199 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15200 { 15201 mblk_t *mp1; 15202 uchar_t *rptr = mp->b_rptr; 15203 queue_t *q = tcp->tcp_rq; 15204 struct T_error_ack *tea; 15205 uint32_t mss; 15206 mblk_t *syn_mp; 15207 mblk_t *mdti; 15208 int retval; 15209 mblk_t *ire_mp; 15210 15211 switch (mp->b_datap->db_type) { 15212 case M_PROTO: 15213 case M_PCPROTO: 15214 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15215 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15216 break; 15217 tea = (struct T_error_ack *)rptr; 15218 switch (tea->PRIM_type) { 15219 case T_BIND_ACK: 15220 /* 15221 * Adapt Multidata information, if any. The 15222 * following tcp_mdt_update routine will free 15223 * the message. 15224 */ 15225 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15226 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15227 b_rptr)->mdt_capab, B_TRUE); 15228 freemsg(mdti); 15229 } 15230 15231 /* Get the IRE, if we had requested for it */ 15232 ire_mp = tcp_ire_mp(mp); 15233 15234 if (tcp->tcp_hard_binding) { 15235 tcp->tcp_hard_binding = B_FALSE; 15236 tcp->tcp_hard_bound = B_TRUE; 15237 CL_INET_CONNECT(tcp); 15238 } else { 15239 if (ire_mp != NULL) 15240 freeb(ire_mp); 15241 goto after_syn_sent; 15242 } 15243 15244 retval = tcp_adapt_ire(tcp, ire_mp); 15245 if (ire_mp != NULL) 15246 freeb(ire_mp); 15247 if (retval == 0) { 15248 tcp_bind_failed(tcp, mp, 15249 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15250 ENETUNREACH : EADDRNOTAVAIL)); 15251 return; 15252 } 15253 /* 15254 * Don't let an endpoint connect to itself. 15255 * Also checked in tcp_connect() but that 15256 * check can't handle the case when the 15257 * local IP address is INADDR_ANY. 15258 */ 15259 if (tcp->tcp_ipversion == IPV4_VERSION) { 15260 if ((tcp->tcp_ipha->ipha_dst == 15261 tcp->tcp_ipha->ipha_src) && 15262 (BE16_EQL(tcp->tcp_tcph->th_lport, 15263 tcp->tcp_tcph->th_fport))) { 15264 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15265 return; 15266 } 15267 } else { 15268 if (IN6_ARE_ADDR_EQUAL( 15269 &tcp->tcp_ip6h->ip6_dst, 15270 &tcp->tcp_ip6h->ip6_src) && 15271 (BE16_EQL(tcp->tcp_tcph->th_lport, 15272 tcp->tcp_tcph->th_fport))) { 15273 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15274 return; 15275 } 15276 } 15277 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15278 /* 15279 * This should not be possible! Just for 15280 * defensive coding... 15281 */ 15282 if (tcp->tcp_state != TCPS_SYN_SENT) 15283 goto after_syn_sent; 15284 15285 if (is_system_labeled() && 15286 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 15287 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 15288 return; 15289 } 15290 15291 ASSERT(q == tcp->tcp_rq); 15292 /* 15293 * tcp_adapt_ire() does not adjust 15294 * for TCP/IP header length. 15295 */ 15296 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15297 15298 /* 15299 * Just make sure our rwnd is at 15300 * least tcp_recv_hiwat_mss * MSS 15301 * large, and round up to the nearest 15302 * MSS. 15303 * 15304 * We do the round up here because 15305 * we need to get the interface 15306 * MTU first before we can do the 15307 * round up. 15308 */ 15309 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15310 tcp_recv_hiwat_minmss * mss); 15311 q->q_hiwat = tcp->tcp_rwnd; 15312 tcp_set_ws_value(tcp); 15313 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15314 tcp->tcp_tcph->th_win); 15315 if (tcp->tcp_rcv_ws > 0 || tcp_wscale_always) 15316 tcp->tcp_snd_ws_ok = B_TRUE; 15317 15318 /* 15319 * Set tcp_snd_ts_ok to true 15320 * so that tcp_xmit_mp will 15321 * include the timestamp 15322 * option in the SYN segment. 15323 */ 15324 if (tcp_tstamp_always || 15325 (tcp->tcp_rcv_ws && tcp_tstamp_if_wscale)) { 15326 tcp->tcp_snd_ts_ok = B_TRUE; 15327 } 15328 15329 /* 15330 * tcp_snd_sack_ok can be set in 15331 * tcp_adapt_ire() if the sack metric 15332 * is set. So check it here also. 15333 */ 15334 if (tcp_sack_permitted == 2 || 15335 tcp->tcp_snd_sack_ok) { 15336 if (tcp->tcp_sack_info == NULL) { 15337 tcp->tcp_sack_info = 15338 kmem_cache_alloc(tcp_sack_info_cache, 15339 KM_SLEEP); 15340 } 15341 tcp->tcp_snd_sack_ok = B_TRUE; 15342 } 15343 15344 /* 15345 * Should we use ECN? Note that the current 15346 * default value (SunOS 5.9) of tcp_ecn_permitted 15347 * is 1. The reason for doing this is that there 15348 * are equipments out there that will drop ECN 15349 * enabled IP packets. Setting it to 1 avoids 15350 * compatibility problems. 15351 */ 15352 if (tcp_ecn_permitted == 2) 15353 tcp->tcp_ecn_ok = B_TRUE; 15354 15355 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15356 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15357 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15358 if (syn_mp) { 15359 cred_t *cr; 15360 pid_t pid; 15361 15362 /* 15363 * Obtain the credential from the 15364 * thread calling connect(); the credential 15365 * lives on in the second mblk which 15366 * originated from T_CONN_REQ and is echoed 15367 * with the T_BIND_ACK from ip. If none 15368 * can be found, default to the creator 15369 * of the socket. 15370 */ 15371 if (mp->b_cont == NULL || 15372 (cr = DB_CRED(mp->b_cont)) == NULL) { 15373 cr = tcp->tcp_cred; 15374 pid = tcp->tcp_cpid; 15375 } else { 15376 pid = DB_CPID(mp->b_cont); 15377 } 15378 15379 TCP_RECORD_TRACE(tcp, syn_mp, 15380 TCP_TRACE_SEND_PKT); 15381 mblk_setcred(syn_mp, cr); 15382 DB_CPID(syn_mp) = pid; 15383 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15384 } 15385 after_syn_sent: 15386 /* 15387 * A trailer mblk indicates a waiting client upstream. 15388 * We complete here the processing begun in 15389 * either tcp_bind() or tcp_connect() by passing 15390 * upstream the reply message they supplied. 15391 */ 15392 mp1 = mp; 15393 mp = mp->b_cont; 15394 freeb(mp1); 15395 if (mp) 15396 break; 15397 return; 15398 case T_ERROR_ACK: 15399 if (tcp->tcp_debug) { 15400 (void) strlog(TCP_MOD_ID, 0, 1, 15401 SL_TRACE|SL_ERROR, 15402 "tcp_rput_other: case T_ERROR_ACK, " 15403 "ERROR_prim == %d", 15404 tea->ERROR_prim); 15405 } 15406 switch (tea->ERROR_prim) { 15407 case O_T_BIND_REQ: 15408 case T_BIND_REQ: 15409 tcp_bind_failed(tcp, mp, 15410 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15411 ENETUNREACH : EADDRNOTAVAIL)); 15412 return; 15413 case T_UNBIND_REQ: 15414 tcp->tcp_hard_binding = B_FALSE; 15415 tcp->tcp_hard_bound = B_FALSE; 15416 if (mp->b_cont) { 15417 freemsg(mp->b_cont); 15418 mp->b_cont = NULL; 15419 } 15420 if (tcp->tcp_unbind_pending) 15421 tcp->tcp_unbind_pending = 0; 15422 else { 15423 /* From tcp_ip_unbind() - free */ 15424 freemsg(mp); 15425 return; 15426 } 15427 break; 15428 case T_SVR4_OPTMGMT_REQ: 15429 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15430 /* T_OPTMGMT_REQ generated by TCP */ 15431 printf("T_SVR4_OPTMGMT_REQ failed " 15432 "%d/%d - dropped (cnt %d)\n", 15433 tea->TLI_error, tea->UNIX_error, 15434 tcp->tcp_drop_opt_ack_cnt); 15435 freemsg(mp); 15436 tcp->tcp_drop_opt_ack_cnt--; 15437 return; 15438 } 15439 break; 15440 } 15441 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15442 tcp->tcp_drop_opt_ack_cnt > 0) { 15443 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15444 "- dropped (cnt %d)\n", 15445 tea->TLI_error, tea->UNIX_error, 15446 tcp->tcp_drop_opt_ack_cnt); 15447 freemsg(mp); 15448 tcp->tcp_drop_opt_ack_cnt--; 15449 return; 15450 } 15451 break; 15452 case T_OPTMGMT_ACK: 15453 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15454 /* T_OPTMGMT_REQ generated by TCP */ 15455 freemsg(mp); 15456 tcp->tcp_drop_opt_ack_cnt--; 15457 return; 15458 } 15459 break; 15460 default: 15461 break; 15462 } 15463 break; 15464 case M_CTL: 15465 /* 15466 * ICMP messages. 15467 */ 15468 tcp_icmp_error(tcp, mp); 15469 return; 15470 case M_FLUSH: 15471 if (*rptr & FLUSHR) 15472 flushq(q, FLUSHDATA); 15473 break; 15474 default: 15475 break; 15476 } 15477 /* 15478 * Make sure we set this bit before sending the ACK for 15479 * bind. Otherwise accept could possibly run and free 15480 * this tcp struct. 15481 */ 15482 putnext(q, mp); 15483 } 15484 15485 /* 15486 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15487 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15488 * tcp_rsrv() try again. 15489 */ 15490 static void 15491 tcp_ordrel_kick(void *arg) 15492 { 15493 conn_t *connp = (conn_t *)arg; 15494 tcp_t *tcp = connp->conn_tcp; 15495 15496 tcp->tcp_ordrelid = 0; 15497 tcp->tcp_timeout = B_FALSE; 15498 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15499 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15500 qenable(tcp->tcp_rq); 15501 } 15502 } 15503 15504 /* ARGSUSED */ 15505 static void 15506 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15507 { 15508 conn_t *connp = (conn_t *)arg; 15509 tcp_t *tcp = connp->conn_tcp; 15510 queue_t *q = tcp->tcp_rq; 15511 uint_t thwin; 15512 15513 freeb(mp); 15514 15515 TCP_STAT(tcp_rsrv_calls); 15516 15517 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15518 return; 15519 } 15520 15521 if (tcp->tcp_fused) { 15522 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15523 15524 ASSERT(tcp->tcp_fused); 15525 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15526 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15527 ASSERT(!TCP_IS_DETACHED(tcp)); 15528 ASSERT(tcp->tcp_connp->conn_sqp == 15529 peer_tcp->tcp_connp->conn_sqp); 15530 15531 /* 15532 * Normally we would not get backenabled in synchronous 15533 * streams mode, but in case this happens, we need to stop 15534 * synchronous streams temporarily to prevent a race with 15535 * tcp_fuse_rrw() or tcp_fuse_rinfop(). It is safe to access 15536 * tcp_rcv_list here because those entry points will return 15537 * right away when synchronous streams is stopped. 15538 */ 15539 TCP_FUSE_SYNCSTR_STOP(tcp); 15540 if (tcp->tcp_rcv_list != NULL) 15541 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15542 15543 tcp_clrqfull(peer_tcp); 15544 TCP_FUSE_SYNCSTR_RESUME(tcp); 15545 TCP_STAT(tcp_fusion_backenabled); 15546 return; 15547 } 15548 15549 if (canputnext(q)) { 15550 tcp->tcp_rwnd = q->q_hiwat; 15551 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15552 << tcp->tcp_rcv_ws; 15553 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15554 /* 15555 * Send back a window update immediately if TCP is above 15556 * ESTABLISHED state and the increase of the rcv window 15557 * that the other side knows is at least 1 MSS after flow 15558 * control is lifted. 15559 */ 15560 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15561 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15562 tcp_xmit_ctl(NULL, tcp, 15563 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15564 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15565 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 15566 } 15567 } 15568 /* Handle a failure to allocate a T_ORDREL_IND here */ 15569 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15570 ASSERT(tcp->tcp_listener == NULL); 15571 if (tcp->tcp_rcv_list != NULL) { 15572 (void) tcp_rcv_drain(q, tcp); 15573 } 15574 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15575 mp = mi_tpi_ordrel_ind(); 15576 if (mp) { 15577 tcp->tcp_ordrel_done = B_TRUE; 15578 putnext(q, mp); 15579 if (tcp->tcp_deferred_clean_death) { 15580 /* 15581 * tcp_clean_death was deferred for 15582 * T_ORDREL_IND - do it now 15583 */ 15584 tcp->tcp_deferred_clean_death = B_FALSE; 15585 (void) tcp_clean_death(tcp, 15586 tcp->tcp_client_errno, 22); 15587 } 15588 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15589 /* 15590 * If there isn't already a timer running 15591 * start one. Use a 4 second 15592 * timer as a fallback since it can't fail. 15593 */ 15594 tcp->tcp_timeout = B_TRUE; 15595 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15596 MSEC_TO_TICK(4000)); 15597 } 15598 } 15599 } 15600 15601 /* 15602 * The read side service routine is called mostly when we get back-enabled as a 15603 * result of flow control relief. Since we don't actually queue anything in 15604 * TCP, we have no data to send out of here. What we do is clear the receive 15605 * window, and send out a window update. 15606 * This routine is also called to drive an orderly release message upstream 15607 * if the attempt in tcp_rput failed. 15608 */ 15609 static void 15610 tcp_rsrv(queue_t *q) 15611 { 15612 conn_t *connp = Q_TO_CONN(q); 15613 tcp_t *tcp = connp->conn_tcp; 15614 mblk_t *mp; 15615 15616 /* No code does a putq on the read side */ 15617 ASSERT(q->q_first == NULL); 15618 15619 /* Nothing to do for the default queue */ 15620 if (q == tcp_g_q) { 15621 return; 15622 } 15623 15624 mp = allocb(0, BPRI_HI); 15625 if (mp == NULL) { 15626 /* 15627 * We are under memory pressure. Return for now and we 15628 * we will be called again later. 15629 */ 15630 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15631 /* 15632 * If there isn't already a timer running 15633 * start one. Use a 4 second 15634 * timer as a fallback since it can't fail. 15635 */ 15636 tcp->tcp_timeout = B_TRUE; 15637 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15638 MSEC_TO_TICK(4000)); 15639 } 15640 return; 15641 } 15642 CONN_INC_REF(connp); 15643 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15644 SQTAG_TCP_RSRV); 15645 } 15646 15647 /* 15648 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15649 * We do not allow the receive window to shrink. After setting rwnd, 15650 * set the flow control hiwat of the stream. 15651 * 15652 * This function is called in 2 cases: 15653 * 15654 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15655 * connection (passive open) and in tcp_rput_data() for active connect. 15656 * This is called after tcp_mss_set() when the desired MSS value is known. 15657 * This makes sure that our window size is a mutiple of the other side's 15658 * MSS. 15659 * 2) Handling SO_RCVBUF option. 15660 * 15661 * It is ASSUMED that the requested size is a multiple of the current MSS. 15662 * 15663 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15664 * user requests so. 15665 */ 15666 static int 15667 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15668 { 15669 uint32_t mss = tcp->tcp_mss; 15670 uint32_t old_max_rwnd; 15671 uint32_t max_transmittable_rwnd; 15672 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15673 15674 if (tcp->tcp_fused) { 15675 size_t sth_hiwat; 15676 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15677 15678 ASSERT(peer_tcp != NULL); 15679 /* 15680 * Record the stream head's high water mark for 15681 * this endpoint; this is used for flow-control 15682 * purposes in tcp_fuse_output(). 15683 */ 15684 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15685 if (!tcp_detached) 15686 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 15687 15688 /* 15689 * In the fusion case, the maxpsz stream head value of 15690 * our peer is set according to its send buffer size 15691 * and our receive buffer size; since the latter may 15692 * have changed we need to update the peer's maxpsz. 15693 */ 15694 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15695 return (rwnd); 15696 } 15697 15698 if (tcp_detached) 15699 old_max_rwnd = tcp->tcp_rwnd; 15700 else 15701 old_max_rwnd = tcp->tcp_rq->q_hiwat; 15702 15703 /* 15704 * Insist on a receive window that is at least 15705 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15706 * funny TCP interactions of Nagle algorithm, SWS avoidance 15707 * and delayed acknowledgement. 15708 */ 15709 rwnd = MAX(rwnd, tcp_recv_hiwat_minmss * mss); 15710 15711 /* 15712 * If window size info has already been exchanged, TCP should not 15713 * shrink the window. Shrinking window is doable if done carefully. 15714 * We may add that support later. But so far there is not a real 15715 * need to do that. 15716 */ 15717 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15718 /* MSS may have changed, do a round up again. */ 15719 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15720 } 15721 15722 /* 15723 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15724 * can be applied even before the window scale option is decided. 15725 */ 15726 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15727 if (rwnd > max_transmittable_rwnd) { 15728 rwnd = max_transmittable_rwnd - 15729 (max_transmittable_rwnd % mss); 15730 if (rwnd < mss) 15731 rwnd = max_transmittable_rwnd; 15732 /* 15733 * If we're over the limit we may have to back down tcp_rwnd. 15734 * The increment below won't work for us. So we set all three 15735 * here and the increment below will have no effect. 15736 */ 15737 tcp->tcp_rwnd = old_max_rwnd = rwnd; 15738 } 15739 if (tcp->tcp_localnet) { 15740 tcp->tcp_rack_abs_max = 15741 MIN(tcp_local_dacks_max, rwnd / mss / 2); 15742 } else { 15743 /* 15744 * For a remote host on a different subnet (through a router), 15745 * we ack every other packet to be conforming to RFC1122. 15746 * tcp_deferred_acks_max is default to 2. 15747 */ 15748 tcp->tcp_rack_abs_max = 15749 MIN(tcp_deferred_acks_max, rwnd / mss / 2); 15750 } 15751 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 15752 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15753 else 15754 tcp->tcp_rack_cur_max = 0; 15755 /* 15756 * Increment the current rwnd by the amount the maximum grew (we 15757 * can not overwrite it since we might be in the middle of a 15758 * connection.) 15759 */ 15760 tcp->tcp_rwnd += rwnd - old_max_rwnd; 15761 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 15762 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 15763 tcp->tcp_cwnd_max = rwnd; 15764 15765 if (tcp_detached) 15766 return (rwnd); 15767 /* 15768 * We set the maximum receive window into rq->q_hiwat. 15769 * This is not actually used for flow control. 15770 */ 15771 tcp->tcp_rq->q_hiwat = rwnd; 15772 /* 15773 * Set the Stream head high water mark. This doesn't have to be 15774 * here, since we are simply using default values, but we would 15775 * prefer to choose these values algorithmically, with a likely 15776 * relationship to rwnd. 15777 */ 15778 (void) mi_set_sth_hiwat(tcp->tcp_rq, MAX(rwnd, tcp_sth_rcv_hiwat)); 15779 return (rwnd); 15780 } 15781 15782 /* 15783 * Return SNMP stuff in buffer in mpdata. 15784 */ 15785 int 15786 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 15787 { 15788 mblk_t *mpdata; 15789 mblk_t *mp_conn_ctl = NULL; 15790 mblk_t *mp_conn_tail; 15791 mblk_t *mp_attr_ctl = NULL; 15792 mblk_t *mp_attr_tail; 15793 mblk_t *mp6_conn_ctl = NULL; 15794 mblk_t *mp6_conn_tail; 15795 mblk_t *mp6_attr_ctl = NULL; 15796 mblk_t *mp6_attr_tail; 15797 struct opthdr *optp; 15798 mib2_tcpConnEntry_t tce; 15799 mib2_tcp6ConnEntry_t tce6; 15800 mib2_transportMLPEntry_t mlp; 15801 connf_t *connfp; 15802 conn_t *connp; 15803 int i; 15804 boolean_t ispriv; 15805 zoneid_t zoneid; 15806 int v4_conn_idx; 15807 int v6_conn_idx; 15808 15809 if (mpctl == NULL || 15810 (mpdata = mpctl->b_cont) == NULL || 15811 (mp_conn_ctl = copymsg(mpctl)) == NULL || 15812 (mp_attr_ctl = copymsg(mpctl)) == NULL || 15813 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 15814 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 15815 freemsg(mp_conn_ctl); 15816 freemsg(mp_attr_ctl); 15817 freemsg(mp6_conn_ctl); 15818 freemsg(mp6_attr_ctl); 15819 return (0); 15820 } 15821 15822 /* build table of connections -- need count in fixed part */ 15823 SET_MIB(tcp_mib.tcpRtoAlgorithm, 4); /* vanj */ 15824 SET_MIB(tcp_mib.tcpRtoMin, tcp_rexmit_interval_min); 15825 SET_MIB(tcp_mib.tcpRtoMax, tcp_rexmit_interval_max); 15826 SET_MIB(tcp_mib.tcpMaxConn, -1); 15827 SET_MIB(tcp_mib.tcpCurrEstab, 0); 15828 15829 ispriv = 15830 secpolicy_net_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 15831 zoneid = Q_TO_CONN(q)->conn_zoneid; 15832 15833 v4_conn_idx = v6_conn_idx = 0; 15834 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 15835 15836 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 15837 15838 connfp = &ipcl_globalhash_fanout[i]; 15839 15840 connp = NULL; 15841 15842 while ((connp = 15843 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 15844 tcp_t *tcp; 15845 boolean_t needattr; 15846 15847 if (connp->conn_zoneid != zoneid) 15848 continue; /* not in this zone */ 15849 15850 tcp = connp->conn_tcp; 15851 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 15852 tcp->tcp_ibsegs = 0; 15853 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 15854 tcp->tcp_obsegs = 0; 15855 15856 tce6.tcp6ConnState = tce.tcpConnState = 15857 tcp_snmp_state(tcp); 15858 if (tce.tcpConnState == MIB2_TCP_established || 15859 tce.tcpConnState == MIB2_TCP_closeWait) 15860 BUMP_MIB(&tcp_mib, tcpCurrEstab); 15861 15862 needattr = B_FALSE; 15863 bzero(&mlp, sizeof (mlp)); 15864 if (connp->conn_mlp_type != mlptSingle) { 15865 if (connp->conn_mlp_type == mlptShared || 15866 connp->conn_mlp_type == mlptBoth) 15867 mlp.tme_flags |= MIB2_TMEF_SHARED; 15868 if (connp->conn_mlp_type == mlptPrivate || 15869 connp->conn_mlp_type == mlptBoth) 15870 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 15871 needattr = B_TRUE; 15872 } 15873 if (connp->conn_peercred != NULL) { 15874 ts_label_t *tsl; 15875 15876 tsl = crgetlabel(connp->conn_peercred); 15877 mlp.tme_doi = label2doi(tsl); 15878 mlp.tme_label = *label2bslabel(tsl); 15879 needattr = B_TRUE; 15880 } 15881 15882 /* Create a message to report on IPv6 entries */ 15883 if (tcp->tcp_ipversion == IPV6_VERSION) { 15884 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 15885 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 15886 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 15887 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 15888 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 15889 /* Don't want just anybody seeing these... */ 15890 if (ispriv) { 15891 tce6.tcp6ConnEntryInfo.ce_snxt = 15892 tcp->tcp_snxt; 15893 tce6.tcp6ConnEntryInfo.ce_suna = 15894 tcp->tcp_suna; 15895 tce6.tcp6ConnEntryInfo.ce_rnxt = 15896 tcp->tcp_rnxt; 15897 tce6.tcp6ConnEntryInfo.ce_rack = 15898 tcp->tcp_rack; 15899 } else { 15900 /* 15901 * Netstat, unfortunately, uses this to 15902 * get send/receive queue sizes. How to fix? 15903 * Why not compute the difference only? 15904 */ 15905 tce6.tcp6ConnEntryInfo.ce_snxt = 15906 tcp->tcp_snxt - tcp->tcp_suna; 15907 tce6.tcp6ConnEntryInfo.ce_suna = 0; 15908 tce6.tcp6ConnEntryInfo.ce_rnxt = 15909 tcp->tcp_rnxt - tcp->tcp_rack; 15910 tce6.tcp6ConnEntryInfo.ce_rack = 0; 15911 } 15912 15913 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15914 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15915 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 15916 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 15917 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 15918 15919 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 15920 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 15921 15922 mlp.tme_connidx = v6_conn_idx++; 15923 if (needattr) 15924 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 15925 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 15926 } 15927 /* 15928 * Create an IPv4 table entry for IPv4 entries and also 15929 * for IPv6 entries which are bound to in6addr_any 15930 * but don't have IPV6_V6ONLY set. 15931 * (i.e. anything an IPv4 peer could connect to) 15932 */ 15933 if (tcp->tcp_ipversion == IPV4_VERSION || 15934 (tcp->tcp_state <= TCPS_LISTEN && 15935 !tcp->tcp_connp->conn_ipv6_v6only && 15936 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 15937 if (tcp->tcp_ipversion == IPV6_VERSION) { 15938 tce.tcpConnRemAddress = INADDR_ANY; 15939 tce.tcpConnLocalAddress = INADDR_ANY; 15940 } else { 15941 tce.tcpConnRemAddress = 15942 tcp->tcp_remote; 15943 tce.tcpConnLocalAddress = 15944 tcp->tcp_ip_src; 15945 } 15946 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 15947 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 15948 /* Don't want just anybody seeing these... */ 15949 if (ispriv) { 15950 tce.tcpConnEntryInfo.ce_snxt = 15951 tcp->tcp_snxt; 15952 tce.tcpConnEntryInfo.ce_suna = 15953 tcp->tcp_suna; 15954 tce.tcpConnEntryInfo.ce_rnxt = 15955 tcp->tcp_rnxt; 15956 tce.tcpConnEntryInfo.ce_rack = 15957 tcp->tcp_rack; 15958 } else { 15959 /* 15960 * Netstat, unfortunately, uses this to 15961 * get send/receive queue sizes. How 15962 * to fix? 15963 * Why not compute the difference only? 15964 */ 15965 tce.tcpConnEntryInfo.ce_snxt = 15966 tcp->tcp_snxt - tcp->tcp_suna; 15967 tce.tcpConnEntryInfo.ce_suna = 0; 15968 tce.tcpConnEntryInfo.ce_rnxt = 15969 tcp->tcp_rnxt - tcp->tcp_rack; 15970 tce.tcpConnEntryInfo.ce_rack = 0; 15971 } 15972 15973 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15974 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15975 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 15976 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 15977 tce.tcpConnEntryInfo.ce_state = 15978 tcp->tcp_state; 15979 15980 (void) snmp_append_data2(mp_conn_ctl->b_cont, 15981 &mp_conn_tail, (char *)&tce, sizeof (tce)); 15982 15983 mlp.tme_connidx = v4_conn_idx++; 15984 if (needattr) 15985 (void) snmp_append_data2( 15986 mp_attr_ctl->b_cont, 15987 &mp_attr_tail, (char *)&mlp, 15988 sizeof (mlp)); 15989 } 15990 } 15991 } 15992 15993 /* fixed length structure for IPv4 and IPv6 counters */ 15994 SET_MIB(tcp_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 15995 SET_MIB(tcp_mib.tcp6ConnTableSize, sizeof (mib2_tcp6ConnEntry_t)); 15996 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 15997 optp->level = MIB2_TCP; 15998 optp->name = 0; 15999 (void) snmp_append_data(mpdata, (char *)&tcp_mib, sizeof (tcp_mib)); 16000 optp->len = msgdsize(mpdata); 16001 qreply(q, mpctl); 16002 16003 /* table of connections... */ 16004 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16005 sizeof (struct T_optmgmt_ack)]; 16006 optp->level = MIB2_TCP; 16007 optp->name = MIB2_TCP_CONN; 16008 optp->len = msgdsize(mp_conn_ctl->b_cont); 16009 qreply(q, mp_conn_ctl); 16010 16011 /* table of MLP attributes... */ 16012 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16013 sizeof (struct T_optmgmt_ack)]; 16014 optp->level = MIB2_TCP; 16015 optp->name = EXPER_XPORT_MLP; 16016 optp->len = msgdsize(mp_attr_ctl->b_cont); 16017 if (optp->len == 0) 16018 freemsg(mp_attr_ctl); 16019 else 16020 qreply(q, mp_attr_ctl); 16021 16022 /* table of IPv6 connections... */ 16023 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16024 sizeof (struct T_optmgmt_ack)]; 16025 optp->level = MIB2_TCP6; 16026 optp->name = MIB2_TCP6_CONN; 16027 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16028 qreply(q, mp6_conn_ctl); 16029 16030 /* table of IPv6 MLP attributes... */ 16031 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16032 sizeof (struct T_optmgmt_ack)]; 16033 optp->level = MIB2_TCP6; 16034 optp->name = EXPER_XPORT_MLP; 16035 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16036 if (optp->len == 0) 16037 freemsg(mp6_attr_ctl); 16038 else 16039 qreply(q, mp6_attr_ctl); 16040 return (1); 16041 } 16042 16043 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16044 /* ARGSUSED */ 16045 int 16046 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16047 { 16048 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16049 16050 switch (level) { 16051 case MIB2_TCP: 16052 switch (name) { 16053 case 13: 16054 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16055 return (0); 16056 /* TODO: delete entry defined by tce */ 16057 return (1); 16058 default: 16059 return (0); 16060 } 16061 default: 16062 return (1); 16063 } 16064 } 16065 16066 /* Translate TCP state to MIB2 TCP state. */ 16067 static int 16068 tcp_snmp_state(tcp_t *tcp) 16069 { 16070 if (tcp == NULL) 16071 return (0); 16072 16073 switch (tcp->tcp_state) { 16074 case TCPS_CLOSED: 16075 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16076 case TCPS_BOUND: 16077 return (MIB2_TCP_closed); 16078 case TCPS_LISTEN: 16079 return (MIB2_TCP_listen); 16080 case TCPS_SYN_SENT: 16081 return (MIB2_TCP_synSent); 16082 case TCPS_SYN_RCVD: 16083 return (MIB2_TCP_synReceived); 16084 case TCPS_ESTABLISHED: 16085 return (MIB2_TCP_established); 16086 case TCPS_CLOSE_WAIT: 16087 return (MIB2_TCP_closeWait); 16088 case TCPS_FIN_WAIT_1: 16089 return (MIB2_TCP_finWait1); 16090 case TCPS_CLOSING: 16091 return (MIB2_TCP_closing); 16092 case TCPS_LAST_ACK: 16093 return (MIB2_TCP_lastAck); 16094 case TCPS_FIN_WAIT_2: 16095 return (MIB2_TCP_finWait2); 16096 case TCPS_TIME_WAIT: 16097 return (MIB2_TCP_timeWait); 16098 default: 16099 return (0); 16100 } 16101 } 16102 16103 static char tcp_report_header[] = 16104 "TCP " MI_COL_HDRPAD_STR 16105 "zone dest snxt suna " 16106 "swnd rnxt rack rwnd rto mss w sw rw t " 16107 "recent [lport,fport] state"; 16108 16109 /* 16110 * TCP status report triggered via the Named Dispatch mechanism. 16111 */ 16112 /* ARGSUSED */ 16113 static void 16114 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16115 cred_t *cr) 16116 { 16117 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16118 boolean_t ispriv = secpolicy_net_config(cr, B_TRUE) == 0; 16119 char cflag; 16120 in6_addr_t v6dst; 16121 char buf[80]; 16122 uint_t print_len, buf_len; 16123 16124 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16125 if (buf_len <= 0) 16126 return; 16127 16128 if (hashval >= 0) 16129 (void) sprintf(hash, "%03d ", hashval); 16130 else 16131 hash[0] = '\0'; 16132 16133 /* 16134 * Note that we use the remote address in the tcp_b structure. 16135 * This means that it will print out the real destination address, 16136 * not the next hop's address if source routing is used. This 16137 * avoid the confusion on the output because user may not 16138 * know that source routing is used for a connection. 16139 */ 16140 if (tcp->tcp_ipversion == IPV4_VERSION) { 16141 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16142 } else { 16143 v6dst = tcp->tcp_remote_v6; 16144 } 16145 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16146 /* 16147 * the ispriv checks are so that normal users cannot determine 16148 * sequence number information using NDD. 16149 */ 16150 16151 if (TCP_IS_DETACHED(tcp)) 16152 cflag = '*'; 16153 else 16154 cflag = ' '; 16155 print_len = snprintf((char *)mp->b_wptr, buf_len, 16156 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16157 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16158 hash, 16159 (void *)tcp, 16160 tcp->tcp_connp->conn_zoneid, 16161 addrbuf, 16162 (ispriv) ? tcp->tcp_snxt : 0, 16163 (ispriv) ? tcp->tcp_suna : 0, 16164 tcp->tcp_swnd, 16165 (ispriv) ? tcp->tcp_rnxt : 0, 16166 (ispriv) ? tcp->tcp_rack : 0, 16167 tcp->tcp_rwnd, 16168 tcp->tcp_rto, 16169 tcp->tcp_mss, 16170 tcp->tcp_snd_ws_ok, 16171 tcp->tcp_snd_ws, 16172 tcp->tcp_rcv_ws, 16173 tcp->tcp_snd_ts_ok, 16174 tcp->tcp_ts_recent, 16175 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16176 if (print_len < buf_len) { 16177 ((mblk_t *)mp)->b_wptr += print_len; 16178 } else { 16179 ((mblk_t *)mp)->b_wptr += buf_len; 16180 } 16181 } 16182 16183 /* 16184 * TCP status report (for listeners only) triggered via the Named Dispatch 16185 * mechanism. 16186 */ 16187 /* ARGSUSED */ 16188 static void 16189 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16190 { 16191 char addrbuf[INET6_ADDRSTRLEN]; 16192 in6_addr_t v6dst; 16193 uint_t print_len, buf_len; 16194 16195 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16196 if (buf_len <= 0) 16197 return; 16198 16199 if (tcp->tcp_ipversion == IPV4_VERSION) { 16200 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16201 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16202 } else { 16203 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16204 addrbuf, sizeof (addrbuf)); 16205 } 16206 print_len = snprintf((char *)mp->b_wptr, buf_len, 16207 "%03d " 16208 MI_COL_PTRFMT_STR 16209 "%d %s %05u %08u %d/%d/%d%c\n", 16210 hashval, (void *)tcp, 16211 tcp->tcp_connp->conn_zoneid, 16212 addrbuf, 16213 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16214 tcp->tcp_conn_req_seqnum, 16215 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16216 tcp->tcp_conn_req_max, 16217 tcp->tcp_syn_defense ? '*' : ' '); 16218 if (print_len < buf_len) { 16219 ((mblk_t *)mp)->b_wptr += print_len; 16220 } else { 16221 ((mblk_t *)mp)->b_wptr += buf_len; 16222 } 16223 } 16224 16225 /* TCP status report triggered via the Named Dispatch mechanism. */ 16226 /* ARGSUSED */ 16227 static int 16228 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16229 { 16230 tcp_t *tcp; 16231 int i; 16232 conn_t *connp; 16233 connf_t *connfp; 16234 zoneid_t zoneid; 16235 16236 /* 16237 * Because of the ndd constraint, at most we can have 64K buffer 16238 * to put in all TCP info. So to be more efficient, just 16239 * allocate a 64K buffer here, assuming we need that large buffer. 16240 * This may be a problem as any user can read tcp_status. Therefore 16241 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16242 * This should be OK as normal users should not do this too often. 16243 */ 16244 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16245 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16246 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16247 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16248 return (0); 16249 } 16250 } 16251 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16252 /* The following may work even if we cannot get a large buf. */ 16253 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16254 return (0); 16255 } 16256 16257 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16258 16259 zoneid = Q_TO_CONN(q)->conn_zoneid; 16260 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16261 16262 connfp = &ipcl_globalhash_fanout[i]; 16263 16264 connp = NULL; 16265 16266 while ((connp = 16267 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16268 tcp = connp->conn_tcp; 16269 if (zoneid != GLOBAL_ZONEID && 16270 zoneid != connp->conn_zoneid) 16271 continue; 16272 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16273 cr); 16274 } 16275 16276 } 16277 16278 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16279 return (0); 16280 } 16281 16282 /* TCP status report triggered via the Named Dispatch mechanism. */ 16283 /* ARGSUSED */ 16284 static int 16285 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16286 { 16287 tf_t *tbf; 16288 tcp_t *tcp; 16289 int i; 16290 zoneid_t zoneid; 16291 16292 /* Refer to comments in tcp_status_report(). */ 16293 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16294 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16295 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16296 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16297 return (0); 16298 } 16299 } 16300 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16301 /* The following may work even if we cannot get a large buf. */ 16302 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16303 return (0); 16304 } 16305 16306 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16307 16308 zoneid = Q_TO_CONN(q)->conn_zoneid; 16309 16310 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 16311 tbf = &tcp_bind_fanout[i]; 16312 mutex_enter(&tbf->tf_lock); 16313 for (tcp = tbf->tf_tcp; tcp != NULL; 16314 tcp = tcp->tcp_bind_hash) { 16315 if (zoneid != GLOBAL_ZONEID && 16316 zoneid != tcp->tcp_connp->conn_zoneid) 16317 continue; 16318 CONN_INC_REF(tcp->tcp_connp); 16319 tcp_report_item(mp->b_cont, tcp, i, 16320 Q_TO_TCP(q), cr); 16321 CONN_DEC_REF(tcp->tcp_connp); 16322 } 16323 mutex_exit(&tbf->tf_lock); 16324 } 16325 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16326 return (0); 16327 } 16328 16329 /* TCP status report triggered via the Named Dispatch mechanism. */ 16330 /* ARGSUSED */ 16331 static int 16332 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16333 { 16334 connf_t *connfp; 16335 conn_t *connp; 16336 tcp_t *tcp; 16337 int i; 16338 zoneid_t zoneid; 16339 16340 /* Refer to comments in tcp_status_report(). */ 16341 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16342 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16343 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16344 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16345 return (0); 16346 } 16347 } 16348 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16349 /* The following may work even if we cannot get a large buf. */ 16350 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16351 return (0); 16352 } 16353 16354 (void) mi_mpprintf(mp, 16355 " TCP " MI_COL_HDRPAD_STR 16356 "zone IP addr port seqnum backlog (q0/q/max)"); 16357 16358 zoneid = Q_TO_CONN(q)->conn_zoneid; 16359 16360 for (i = 0; i < ipcl_bind_fanout_size; i++) { 16361 connfp = &ipcl_bind_fanout[i]; 16362 connp = NULL; 16363 while ((connp = 16364 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16365 tcp = connp->conn_tcp; 16366 if (zoneid != GLOBAL_ZONEID && 16367 zoneid != connp->conn_zoneid) 16368 continue; 16369 tcp_report_listener(mp->b_cont, tcp, i); 16370 } 16371 } 16372 16373 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16374 return (0); 16375 } 16376 16377 /* TCP status report triggered via the Named Dispatch mechanism. */ 16378 /* ARGSUSED */ 16379 static int 16380 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16381 { 16382 connf_t *connfp; 16383 conn_t *connp; 16384 tcp_t *tcp; 16385 int i; 16386 zoneid_t zoneid; 16387 16388 /* Refer to comments in tcp_status_report(). */ 16389 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16390 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16391 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16392 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16393 return (0); 16394 } 16395 } 16396 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16397 /* The following may work even if we cannot get a large buf. */ 16398 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16399 return (0); 16400 } 16401 16402 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16403 ipcl_conn_fanout_size); 16404 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16405 16406 zoneid = Q_TO_CONN(q)->conn_zoneid; 16407 16408 for (i = 0; i < ipcl_conn_fanout_size; i++) { 16409 connfp = &ipcl_conn_fanout[i]; 16410 connp = NULL; 16411 while ((connp = 16412 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16413 tcp = connp->conn_tcp; 16414 if (zoneid != GLOBAL_ZONEID && 16415 zoneid != connp->conn_zoneid) 16416 continue; 16417 tcp_report_item(mp->b_cont, tcp, i, 16418 Q_TO_TCP(q), cr); 16419 } 16420 } 16421 16422 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16423 return (0); 16424 } 16425 16426 /* TCP status report triggered via the Named Dispatch mechanism. */ 16427 /* ARGSUSED */ 16428 static int 16429 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16430 { 16431 tf_t *tf; 16432 tcp_t *tcp; 16433 int i; 16434 zoneid_t zoneid; 16435 16436 /* Refer to comments in tcp_status_report(). */ 16437 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16438 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16439 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16440 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16441 return (0); 16442 } 16443 } 16444 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16445 /* The following may work even if we cannot get a large buf. */ 16446 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16447 return (0); 16448 } 16449 16450 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16451 16452 zoneid = Q_TO_CONN(q)->conn_zoneid; 16453 16454 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 16455 tf = &tcp_acceptor_fanout[i]; 16456 mutex_enter(&tf->tf_lock); 16457 for (tcp = tf->tf_tcp; tcp != NULL; 16458 tcp = tcp->tcp_acceptor_hash) { 16459 if (zoneid != GLOBAL_ZONEID && 16460 zoneid != tcp->tcp_connp->conn_zoneid) 16461 continue; 16462 tcp_report_item(mp->b_cont, tcp, i, 16463 Q_TO_TCP(q), cr); 16464 } 16465 mutex_exit(&tf->tf_lock); 16466 } 16467 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16468 return (0); 16469 } 16470 16471 /* 16472 * tcp_timer is the timer service routine. It handles the retransmission, 16473 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16474 * from the state of the tcp instance what kind of action needs to be done 16475 * at the time it is called. 16476 */ 16477 static void 16478 tcp_timer(void *arg) 16479 { 16480 mblk_t *mp; 16481 clock_t first_threshold; 16482 clock_t second_threshold; 16483 clock_t ms; 16484 uint32_t mss; 16485 conn_t *connp = (conn_t *)arg; 16486 tcp_t *tcp = connp->conn_tcp; 16487 16488 tcp->tcp_timer_tid = 0; 16489 16490 if (tcp->tcp_fused) 16491 return; 16492 16493 first_threshold = tcp->tcp_first_timer_threshold; 16494 second_threshold = tcp->tcp_second_timer_threshold; 16495 switch (tcp->tcp_state) { 16496 case TCPS_IDLE: 16497 case TCPS_BOUND: 16498 case TCPS_LISTEN: 16499 return; 16500 case TCPS_SYN_RCVD: { 16501 tcp_t *listener = tcp->tcp_listener; 16502 16503 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16504 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16505 /* it's our first timeout */ 16506 tcp->tcp_syn_rcvd_timeout = 1; 16507 mutex_enter(&listener->tcp_eager_lock); 16508 listener->tcp_syn_rcvd_timeout++; 16509 if (!listener->tcp_syn_defense && 16510 (listener->tcp_syn_rcvd_timeout > 16511 (tcp_conn_req_max_q0 >> 2)) && 16512 (tcp_conn_req_max_q0 > 200)) { 16513 /* We may be under attack. Put on a defense. */ 16514 listener->tcp_syn_defense = B_TRUE; 16515 cmn_err(CE_WARN, "High TCP connect timeout " 16516 "rate! System (port %d) may be under a " 16517 "SYN flood attack!", 16518 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16519 16520 listener->tcp_ip_addr_cache = kmem_zalloc( 16521 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16522 KM_NOSLEEP); 16523 } 16524 mutex_exit(&listener->tcp_eager_lock); 16525 } 16526 } 16527 /* FALLTHRU */ 16528 case TCPS_SYN_SENT: 16529 first_threshold = tcp->tcp_first_ctimer_threshold; 16530 second_threshold = tcp->tcp_second_ctimer_threshold; 16531 break; 16532 case TCPS_ESTABLISHED: 16533 case TCPS_FIN_WAIT_1: 16534 case TCPS_CLOSING: 16535 case TCPS_CLOSE_WAIT: 16536 case TCPS_LAST_ACK: 16537 /* If we have data to rexmit */ 16538 if (tcp->tcp_suna != tcp->tcp_snxt) { 16539 clock_t time_to_wait; 16540 16541 BUMP_MIB(&tcp_mib, tcpTimRetrans); 16542 if (!tcp->tcp_xmit_head) 16543 break; 16544 time_to_wait = lbolt - 16545 (clock_t)tcp->tcp_xmit_head->b_prev; 16546 time_to_wait = tcp->tcp_rto - 16547 TICK_TO_MSEC(time_to_wait); 16548 /* 16549 * If the timer fires too early, 1 clock tick earlier, 16550 * restart the timer. 16551 */ 16552 if (time_to_wait > msec_per_tick) { 16553 TCP_STAT(tcp_timer_fire_early); 16554 TCP_TIMER_RESTART(tcp, time_to_wait); 16555 return; 16556 } 16557 /* 16558 * When we probe zero windows, we force the swnd open. 16559 * If our peer acks with a closed window swnd will be 16560 * set to zero by tcp_rput(). As long as we are 16561 * receiving acks tcp_rput will 16562 * reset 'tcp_ms_we_have_waited' so as not to trip the 16563 * first and second interval actions. NOTE: the timer 16564 * interval is allowed to continue its exponential 16565 * backoff. 16566 */ 16567 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16568 if (tcp->tcp_debug) { 16569 (void) strlog(TCP_MOD_ID, 0, 1, 16570 SL_TRACE, "tcp_timer: zero win"); 16571 } 16572 } else { 16573 /* 16574 * After retransmission, we need to do 16575 * slow start. Set the ssthresh to one 16576 * half of current effective window and 16577 * cwnd to one MSS. Also reset 16578 * tcp_cwnd_cnt. 16579 * 16580 * Note that if tcp_ssthresh is reduced because 16581 * of ECN, do not reduce it again unless it is 16582 * already one window of data away (tcp_cwr 16583 * should then be cleared) or this is a 16584 * timeout for a retransmitted segment. 16585 */ 16586 uint32_t npkt; 16587 16588 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16589 npkt = ((tcp->tcp_timer_backoff ? 16590 tcp->tcp_cwnd_ssthresh : 16591 tcp->tcp_snxt - 16592 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16593 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16594 tcp->tcp_mss; 16595 } 16596 tcp->tcp_cwnd = tcp->tcp_mss; 16597 tcp->tcp_cwnd_cnt = 0; 16598 if (tcp->tcp_ecn_ok) { 16599 tcp->tcp_cwr = B_TRUE; 16600 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16601 tcp->tcp_ecn_cwr_sent = B_FALSE; 16602 } 16603 } 16604 break; 16605 } 16606 /* 16607 * We have something to send yet we cannot send. The 16608 * reason can be: 16609 * 16610 * 1. Zero send window: we need to do zero window probe. 16611 * 2. Zero cwnd: because of ECN, we need to "clock out 16612 * segments. 16613 * 3. SWS avoidance: receiver may have shrunk window, 16614 * reset our knowledge. 16615 * 16616 * Note that condition 2 can happen with either 1 or 16617 * 3. But 1 and 3 are exclusive. 16618 */ 16619 if (tcp->tcp_unsent != 0) { 16620 if (tcp->tcp_cwnd == 0) { 16621 /* 16622 * Set tcp_cwnd to 1 MSS so that a 16623 * new segment can be sent out. We 16624 * are "clocking out" new data when 16625 * the network is really congested. 16626 */ 16627 ASSERT(tcp->tcp_ecn_ok); 16628 tcp->tcp_cwnd = tcp->tcp_mss; 16629 } 16630 if (tcp->tcp_swnd == 0) { 16631 /* Extend window for zero window probe */ 16632 tcp->tcp_swnd++; 16633 tcp->tcp_zero_win_probe = B_TRUE; 16634 BUMP_MIB(&tcp_mib, tcpOutWinProbe); 16635 } else { 16636 /* 16637 * Handle timeout from sender SWS avoidance. 16638 * Reset our knowledge of the max send window 16639 * since the receiver might have reduced its 16640 * receive buffer. Avoid setting tcp_max_swnd 16641 * to one since that will essentially disable 16642 * the SWS checks. 16643 * 16644 * Note that since we don't have a SWS 16645 * state variable, if the timeout is set 16646 * for ECN but not for SWS, this 16647 * code will also be executed. This is 16648 * fine as tcp_max_swnd is updated 16649 * constantly and it will not affect 16650 * anything. 16651 */ 16652 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16653 } 16654 tcp_wput_data(tcp, NULL, B_FALSE); 16655 return; 16656 } 16657 /* Is there a FIN that needs to be to re retransmitted? */ 16658 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16659 !tcp->tcp_fin_acked) 16660 break; 16661 /* Nothing to do, return without restarting timer. */ 16662 TCP_STAT(tcp_timer_fire_miss); 16663 return; 16664 case TCPS_FIN_WAIT_2: 16665 /* 16666 * User closed the TCP endpoint and peer ACK'ed our FIN. 16667 * We waited some time for for peer's FIN, but it hasn't 16668 * arrived. We flush the connection now to avoid 16669 * case where the peer has rebooted. 16670 */ 16671 if (TCP_IS_DETACHED(tcp)) { 16672 (void) tcp_clean_death(tcp, 0, 23); 16673 } else { 16674 TCP_TIMER_RESTART(tcp, tcp_fin_wait_2_flush_interval); 16675 } 16676 return; 16677 case TCPS_TIME_WAIT: 16678 (void) tcp_clean_death(tcp, 0, 24); 16679 return; 16680 default: 16681 if (tcp->tcp_debug) { 16682 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16683 "tcp_timer: strange state (%d) %s", 16684 tcp->tcp_state, tcp_display(tcp, NULL, 16685 DISP_PORT_ONLY)); 16686 } 16687 return; 16688 } 16689 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16690 /* 16691 * For zero window probe, we need to send indefinitely, 16692 * unless we have not heard from the other side for some 16693 * time... 16694 */ 16695 if ((tcp->tcp_zero_win_probe == 0) || 16696 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16697 second_threshold)) { 16698 BUMP_MIB(&tcp_mib, tcpTimRetransDrop); 16699 /* 16700 * If TCP is in SYN_RCVD state, send back a 16701 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16702 * should be zero in TCPS_SYN_RCVD state. 16703 */ 16704 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16705 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16706 "in SYN_RCVD", 16707 tcp, tcp->tcp_snxt, 16708 tcp->tcp_rnxt, TH_RST | TH_ACK); 16709 } 16710 (void) tcp_clean_death(tcp, 16711 tcp->tcp_client_errno ? 16712 tcp->tcp_client_errno : ETIMEDOUT, 25); 16713 return; 16714 } else { 16715 /* 16716 * Set tcp_ms_we_have_waited to second_threshold 16717 * so that in next timeout, we will do the above 16718 * check (lbolt - tcp_last_recv_time). This is 16719 * also to avoid overflow. 16720 * 16721 * We don't need to decrement tcp_timer_backoff 16722 * to avoid overflow because it will be decremented 16723 * later if new timeout value is greater than 16724 * tcp_rexmit_interval_max. In the case when 16725 * tcp_rexmit_interval_max is greater than 16726 * second_threshold, it means that we will wait 16727 * longer than second_threshold to send the next 16728 * window probe. 16729 */ 16730 tcp->tcp_ms_we_have_waited = second_threshold; 16731 } 16732 } else if (ms > first_threshold) { 16733 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16734 tcp->tcp_xmit_head != NULL) { 16735 tcp->tcp_xmit_head = 16736 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16737 } 16738 /* 16739 * We have been retransmitting for too long... The RTT 16740 * we calculated is probably incorrect. Reinitialize it. 16741 * Need to compensate for 0 tcp_rtt_sa. Reset 16742 * tcp_rtt_update so that we won't accidentally cache a 16743 * bad value. But only do this if this is not a zero 16744 * window probe. 16745 */ 16746 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 16747 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 16748 (tcp->tcp_rtt_sa >> 5); 16749 tcp->tcp_rtt_sa = 0; 16750 tcp_ip_notify(tcp); 16751 tcp->tcp_rtt_update = 0; 16752 } 16753 } 16754 tcp->tcp_timer_backoff++; 16755 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 16756 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 16757 tcp_rexmit_interval_min) { 16758 /* 16759 * This means the original RTO is tcp_rexmit_interval_min. 16760 * So we will use tcp_rexmit_interval_min as the RTO value 16761 * and do the backoff. 16762 */ 16763 ms = tcp_rexmit_interval_min << tcp->tcp_timer_backoff; 16764 } else { 16765 ms <<= tcp->tcp_timer_backoff; 16766 } 16767 if (ms > tcp_rexmit_interval_max) { 16768 ms = tcp_rexmit_interval_max; 16769 /* 16770 * ms is at max, decrement tcp_timer_backoff to avoid 16771 * overflow. 16772 */ 16773 tcp->tcp_timer_backoff--; 16774 } 16775 tcp->tcp_ms_we_have_waited += ms; 16776 if (tcp->tcp_zero_win_probe == 0) { 16777 tcp->tcp_rto = ms; 16778 } 16779 TCP_TIMER_RESTART(tcp, ms); 16780 /* 16781 * This is after a timeout and tcp_rto is backed off. Set 16782 * tcp_set_timer to 1 so that next time RTO is updated, we will 16783 * restart the timer with a correct value. 16784 */ 16785 tcp->tcp_set_timer = 1; 16786 mss = tcp->tcp_snxt - tcp->tcp_suna; 16787 if (mss > tcp->tcp_mss) 16788 mss = tcp->tcp_mss; 16789 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 16790 mss = tcp->tcp_swnd; 16791 16792 if ((mp = tcp->tcp_xmit_head) != NULL) 16793 mp->b_prev = (mblk_t *)lbolt; 16794 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 16795 B_TRUE); 16796 16797 /* 16798 * When slow start after retransmission begins, start with 16799 * this seq no. tcp_rexmit_max marks the end of special slow 16800 * start phase. tcp_snd_burst controls how many segments 16801 * can be sent because of an ack. 16802 */ 16803 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 16804 tcp->tcp_snd_burst = TCP_CWND_SS; 16805 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16806 (tcp->tcp_unsent == 0)) { 16807 tcp->tcp_rexmit_max = tcp->tcp_fss; 16808 } else { 16809 tcp->tcp_rexmit_max = tcp->tcp_snxt; 16810 } 16811 tcp->tcp_rexmit = B_TRUE; 16812 tcp->tcp_dupack_cnt = 0; 16813 16814 /* 16815 * Remove all rexmit SACK blk to start from fresh. 16816 */ 16817 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 16818 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 16819 tcp->tcp_num_notsack_blk = 0; 16820 tcp->tcp_cnt_notsack_list = 0; 16821 } 16822 if (mp == NULL) { 16823 return; 16824 } 16825 /* Attach credentials to retransmitted initial SYNs. */ 16826 if (tcp->tcp_state == TCPS_SYN_SENT) { 16827 mblk_setcred(mp, tcp->tcp_cred); 16828 DB_CPID(mp) = tcp->tcp_cpid; 16829 } 16830 16831 tcp->tcp_csuna = tcp->tcp_snxt; 16832 BUMP_MIB(&tcp_mib, tcpRetransSegs); 16833 UPDATE_MIB(&tcp_mib, tcpRetransBytes, mss); 16834 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 16835 tcp_send_data(tcp, tcp->tcp_wq, mp); 16836 16837 } 16838 16839 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 16840 static void 16841 tcp_unbind(tcp_t *tcp, mblk_t *mp) 16842 { 16843 conn_t *connp; 16844 16845 switch (tcp->tcp_state) { 16846 case TCPS_BOUND: 16847 case TCPS_LISTEN: 16848 break; 16849 default: 16850 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 16851 return; 16852 } 16853 16854 /* 16855 * Need to clean up all the eagers since after the unbind, segments 16856 * will no longer be delivered to this listener stream. 16857 */ 16858 mutex_enter(&tcp->tcp_eager_lock); 16859 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 16860 tcp_eager_cleanup(tcp, 0); 16861 } 16862 mutex_exit(&tcp->tcp_eager_lock); 16863 16864 if (tcp->tcp_ipversion == IPV4_VERSION) { 16865 tcp->tcp_ipha->ipha_src = 0; 16866 } else { 16867 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16868 } 16869 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16870 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 16871 tcp_bind_hash_remove(tcp); 16872 tcp->tcp_state = TCPS_IDLE; 16873 tcp->tcp_mdt = B_FALSE; 16874 /* Send M_FLUSH according to TPI */ 16875 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 16876 connp = tcp->tcp_connp; 16877 connp->conn_mdt_ok = B_FALSE; 16878 ipcl_hash_remove(connp); 16879 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 16880 mp = mi_tpi_ok_ack_alloc(mp); 16881 putnext(tcp->tcp_rq, mp); 16882 } 16883 16884 /* 16885 * Don't let port fall into the privileged range. 16886 * Since the extra privileged ports can be arbitrary we also 16887 * ensure that we exclude those from consideration. 16888 * tcp_g_epriv_ports is not sorted thus we loop over it until 16889 * there are no changes. 16890 * 16891 * Note: No locks are held when inspecting tcp_g_*epriv_ports 16892 * but instead the code relies on: 16893 * - the fact that the address of the array and its size never changes 16894 * - the atomic assignment of the elements of the array 16895 * 16896 * Returns 0 if there are no more ports available. 16897 * 16898 * TS note: skip multilevel ports. 16899 */ 16900 static in_port_t 16901 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 16902 { 16903 int i; 16904 boolean_t restart = B_FALSE; 16905 16906 if (random && tcp_random_anon_port != 0) { 16907 (void) random_get_pseudo_bytes((uint8_t *)&port, 16908 sizeof (in_port_t)); 16909 /* 16910 * Unless changed by a sys admin, the smallest anon port 16911 * is 32768 and the largest anon port is 65535. It is 16912 * very likely (50%) for the random port to be smaller 16913 * than the smallest anon port. When that happens, 16914 * add port % (anon port range) to the smallest anon 16915 * port to get the random port. It should fall into the 16916 * valid anon port range. 16917 */ 16918 if (port < tcp_smallest_anon_port) { 16919 port = tcp_smallest_anon_port + 16920 port % (tcp_largest_anon_port - 16921 tcp_smallest_anon_port); 16922 } 16923 } 16924 16925 retry: 16926 if (port < tcp_smallest_anon_port) 16927 port = (in_port_t)tcp_smallest_anon_port; 16928 16929 if (port > tcp_largest_anon_port) { 16930 if (restart) 16931 return (0); 16932 restart = B_TRUE; 16933 port = (in_port_t)tcp_smallest_anon_port; 16934 } 16935 16936 if (port < tcp_smallest_nonpriv_port) 16937 port = (in_port_t)tcp_smallest_nonpriv_port; 16938 16939 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 16940 if (port == tcp_g_epriv_ports[i]) { 16941 port++; 16942 /* 16943 * Make sure whether the port is in the 16944 * valid range. 16945 */ 16946 goto retry; 16947 } 16948 } 16949 if (is_system_labeled() && 16950 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 16951 IPPROTO_TCP, B_TRUE)) != 0) { 16952 port = i; 16953 goto retry; 16954 } 16955 return (port); 16956 } 16957 16958 /* 16959 * Return the next anonymous port in the privileged port range for 16960 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 16961 * downwards. This is the same behavior as documented in the userland 16962 * library call rresvport(3N). 16963 * 16964 * TS note: skip multilevel ports. 16965 */ 16966 static in_port_t 16967 tcp_get_next_priv_port(const tcp_t *tcp) 16968 { 16969 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 16970 in_port_t nextport; 16971 boolean_t restart = B_FALSE; 16972 16973 retry: 16974 if (next_priv_port < tcp_min_anonpriv_port || 16975 next_priv_port >= IPPORT_RESERVED) { 16976 next_priv_port = IPPORT_RESERVED - 1; 16977 if (restart) 16978 return (0); 16979 restart = B_TRUE; 16980 } 16981 if (is_system_labeled() && 16982 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 16983 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 16984 next_priv_port = nextport; 16985 goto retry; 16986 } 16987 return (next_priv_port--); 16988 } 16989 16990 /* The write side r/w procedure. */ 16991 16992 #if CCS_STATS 16993 struct { 16994 struct { 16995 int64_t count, bytes; 16996 } tot, hit; 16997 } wrw_stats; 16998 #endif 16999 17000 /* 17001 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17002 * messages. 17003 */ 17004 /* ARGSUSED */ 17005 static void 17006 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17007 { 17008 conn_t *connp = (conn_t *)arg; 17009 tcp_t *tcp = connp->conn_tcp; 17010 queue_t *q = tcp->tcp_wq; 17011 17012 ASSERT(DB_TYPE(mp) != M_IOCTL); 17013 /* 17014 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17015 * Once the close starts, streamhead and sockfs will not let any data 17016 * packets come down (close ensures that there are no threads using the 17017 * queue and no new threads will come down) but since qprocsoff() 17018 * hasn't happened yet, a M_FLUSH or some non data message might 17019 * get reflected back (in response to our own FLUSHRW) and get 17020 * processed after tcp_close() is done. The conn would still be valid 17021 * because a ref would have added but we need to check the state 17022 * before actually processing the packet. 17023 */ 17024 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17025 freemsg(mp); 17026 return; 17027 } 17028 17029 switch (DB_TYPE(mp)) { 17030 case M_IOCDATA: 17031 tcp_wput_iocdata(tcp, mp); 17032 break; 17033 case M_FLUSH: 17034 tcp_wput_flush(tcp, mp); 17035 break; 17036 default: 17037 CALL_IP_WPUT(connp, q, mp); 17038 break; 17039 } 17040 } 17041 17042 /* 17043 * The TCP fast path write put procedure. 17044 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17045 */ 17046 /* ARGSUSED */ 17047 void 17048 tcp_output(void *arg, mblk_t *mp, void *arg2) 17049 { 17050 int len; 17051 int hdrlen; 17052 int plen; 17053 mblk_t *mp1; 17054 uchar_t *rptr; 17055 uint32_t snxt; 17056 tcph_t *tcph; 17057 struct datab *db; 17058 uint32_t suna; 17059 uint32_t mss; 17060 ipaddr_t *dst; 17061 ipaddr_t *src; 17062 uint32_t sum; 17063 int usable; 17064 conn_t *connp = (conn_t *)arg; 17065 tcp_t *tcp = connp->conn_tcp; 17066 uint32_t msize; 17067 17068 /* 17069 * Try and ASSERT the minimum possible references on the 17070 * conn early enough. Since we are executing on write side, 17071 * the connection is obviously not detached and that means 17072 * there is a ref each for TCP and IP. Since we are behind 17073 * the squeue, the minimum references needed are 3. If the 17074 * conn is in classifier hash list, there should be an 17075 * extra ref for that (we check both the possibilities). 17076 */ 17077 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17078 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17079 17080 ASSERT(DB_TYPE(mp) == M_DATA); 17081 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17082 17083 mutex_enter(&connp->conn_lock); 17084 tcp->tcp_squeue_bytes -= msize; 17085 mutex_exit(&connp->conn_lock); 17086 17087 /* Bypass tcp protocol for fused tcp loopback */ 17088 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17089 return; 17090 17091 mss = tcp->tcp_mss; 17092 if (tcp->tcp_xmit_zc_clean) 17093 mp = tcp_zcopy_backoff(tcp, mp, 0); 17094 17095 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17096 len = (int)(mp->b_wptr - mp->b_rptr); 17097 17098 /* 17099 * Criteria for fast path: 17100 * 17101 * 1. no unsent data 17102 * 2. single mblk in request 17103 * 3. connection established 17104 * 4. data in mblk 17105 * 5. len <= mss 17106 * 6. no tcp_valid bits 17107 */ 17108 if ((tcp->tcp_unsent != 0) || 17109 (tcp->tcp_cork) || 17110 (mp->b_cont != NULL) || 17111 (tcp->tcp_state != TCPS_ESTABLISHED) || 17112 (len == 0) || 17113 (len > mss) || 17114 (tcp->tcp_valid_bits != 0)) { 17115 tcp_wput_data(tcp, mp, B_FALSE); 17116 return; 17117 } 17118 17119 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17120 ASSERT(tcp->tcp_fin_sent == 0); 17121 17122 /* queue new packet onto retransmission queue */ 17123 if (tcp->tcp_xmit_head == NULL) { 17124 tcp->tcp_xmit_head = mp; 17125 } else { 17126 tcp->tcp_xmit_last->b_cont = mp; 17127 } 17128 tcp->tcp_xmit_last = mp; 17129 tcp->tcp_xmit_tail = mp; 17130 17131 /* find out how much we can send */ 17132 /* BEGIN CSTYLED */ 17133 /* 17134 * un-acked usable 17135 * |--------------|-----------------| 17136 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17137 */ 17138 /* END CSTYLED */ 17139 17140 /* start sending from tcp_snxt */ 17141 snxt = tcp->tcp_snxt; 17142 17143 /* 17144 * Check to see if this connection has been idled for some 17145 * time and no ACK is expected. If it is, we need to slow 17146 * start again to get back the connection's "self-clock" as 17147 * described in VJ's paper. 17148 * 17149 * Refer to the comment in tcp_mss_set() for the calculation 17150 * of tcp_cwnd after idle. 17151 */ 17152 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17153 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17154 SET_TCP_INIT_CWND(tcp, mss, tcp_slow_start_after_idle); 17155 } 17156 17157 usable = tcp->tcp_swnd; /* tcp window size */ 17158 if (usable > tcp->tcp_cwnd) 17159 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17160 usable -= snxt; /* subtract stuff already sent */ 17161 suna = tcp->tcp_suna; 17162 usable += suna; 17163 /* usable can be < 0 if the congestion window is smaller */ 17164 if (len > usable) { 17165 /* Can't send complete M_DATA in one shot */ 17166 goto slow; 17167 } 17168 17169 if (tcp->tcp_flow_stopped && 17170 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17171 tcp_clrqfull(tcp); 17172 } 17173 17174 /* 17175 * determine if anything to send (Nagle). 17176 * 17177 * 1. len < tcp_mss (i.e. small) 17178 * 2. unacknowledged data present 17179 * 3. len < nagle limit 17180 * 4. last packet sent < nagle limit (previous packet sent) 17181 */ 17182 if ((len < mss) && (snxt != suna) && 17183 (len < (int)tcp->tcp_naglim) && 17184 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17185 /* 17186 * This was the first unsent packet and normally 17187 * mss < xmit_hiwater so there is no need to worry 17188 * about flow control. The next packet will go 17189 * through the flow control check in tcp_wput_data(). 17190 */ 17191 /* leftover work from above */ 17192 tcp->tcp_unsent = len; 17193 tcp->tcp_xmit_tail_unsent = len; 17194 17195 return; 17196 } 17197 17198 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17199 17200 if (snxt == suna) { 17201 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17202 } 17203 17204 /* we have always sent something */ 17205 tcp->tcp_rack_cnt = 0; 17206 17207 tcp->tcp_snxt = snxt + len; 17208 tcp->tcp_rack = tcp->tcp_rnxt; 17209 17210 if ((mp1 = dupb(mp)) == 0) 17211 goto no_memory; 17212 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17213 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17214 17215 /* adjust tcp header information */ 17216 tcph = tcp->tcp_tcph; 17217 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17218 17219 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17220 sum = (sum >> 16) + (sum & 0xFFFF); 17221 U16_TO_ABE16(sum, tcph->th_sum); 17222 17223 U32_TO_ABE32(snxt, tcph->th_seq); 17224 17225 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 17226 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 17227 BUMP_LOCAL(tcp->tcp_obsegs); 17228 17229 /* Update the latest receive window size in TCP header. */ 17230 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17231 tcph->th_win); 17232 17233 tcp->tcp_last_sent_len = (ushort_t)len; 17234 17235 plen = len + tcp->tcp_hdr_len; 17236 17237 if (tcp->tcp_ipversion == IPV4_VERSION) { 17238 tcp->tcp_ipha->ipha_length = htons(plen); 17239 } else { 17240 tcp->tcp_ip6h->ip6_plen = htons(plen - 17241 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17242 } 17243 17244 /* see if we need to allocate a mblk for the headers */ 17245 hdrlen = tcp->tcp_hdr_len; 17246 rptr = mp1->b_rptr - hdrlen; 17247 db = mp1->b_datap; 17248 if ((db->db_ref != 2) || rptr < db->db_base || 17249 (!OK_32PTR(rptr))) { 17250 /* NOTE: we assume allocb returns an OK_32PTR */ 17251 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17252 tcp_wroff_xtra, BPRI_MED); 17253 if (!mp) { 17254 freemsg(mp1); 17255 goto no_memory; 17256 } 17257 mp->b_cont = mp1; 17258 mp1 = mp; 17259 /* Leave room for Link Level header */ 17260 /* hdrlen = tcp->tcp_hdr_len; */ 17261 rptr = &mp1->b_rptr[tcp_wroff_xtra]; 17262 mp1->b_wptr = &rptr[hdrlen]; 17263 } 17264 mp1->b_rptr = rptr; 17265 17266 /* Fill in the timestamp option. */ 17267 if (tcp->tcp_snd_ts_ok) { 17268 U32_TO_BE32((uint32_t)lbolt, 17269 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17270 U32_TO_BE32(tcp->tcp_ts_recent, 17271 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17272 } else { 17273 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17274 } 17275 17276 /* copy header into outgoing packet */ 17277 dst = (ipaddr_t *)rptr; 17278 src = (ipaddr_t *)tcp->tcp_iphc; 17279 dst[0] = src[0]; 17280 dst[1] = src[1]; 17281 dst[2] = src[2]; 17282 dst[3] = src[3]; 17283 dst[4] = src[4]; 17284 dst[5] = src[5]; 17285 dst[6] = src[6]; 17286 dst[7] = src[7]; 17287 dst[8] = src[8]; 17288 dst[9] = src[9]; 17289 if (hdrlen -= 40) { 17290 hdrlen >>= 2; 17291 dst += 10; 17292 src += 10; 17293 do { 17294 *dst++ = *src++; 17295 } while (--hdrlen); 17296 } 17297 17298 /* 17299 * Set the ECN info in the TCP header. Note that this 17300 * is not the template header. 17301 */ 17302 if (tcp->tcp_ecn_ok) { 17303 SET_ECT(tcp, rptr); 17304 17305 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17306 if (tcp->tcp_ecn_echo_on) 17307 tcph->th_flags[0] |= TH_ECE; 17308 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17309 tcph->th_flags[0] |= TH_CWR; 17310 tcp->tcp_ecn_cwr_sent = B_TRUE; 17311 } 17312 } 17313 17314 if (tcp->tcp_ip_forward_progress) { 17315 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17316 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17317 tcp->tcp_ip_forward_progress = B_FALSE; 17318 } 17319 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17320 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17321 return; 17322 17323 /* 17324 * If we ran out of memory, we pretend to have sent the packet 17325 * and that it was lost on the wire. 17326 */ 17327 no_memory: 17328 return; 17329 17330 slow: 17331 /* leftover work from above */ 17332 tcp->tcp_unsent = len; 17333 tcp->tcp_xmit_tail_unsent = len; 17334 tcp_wput_data(tcp, NULL, B_FALSE); 17335 } 17336 17337 /* 17338 * The function called through squeue to get behind eager's perimeter to 17339 * finish the accept processing. 17340 */ 17341 /* ARGSUSED */ 17342 void 17343 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17344 { 17345 conn_t *connp = (conn_t *)arg; 17346 tcp_t *tcp = connp->conn_tcp; 17347 queue_t *q = tcp->tcp_rq; 17348 mblk_t *mp1; 17349 mblk_t *stropt_mp = mp; 17350 struct stroptions *stropt; 17351 uint_t thwin; 17352 17353 /* 17354 * Drop the eager's ref on the listener, that was placed when 17355 * this eager began life in tcp_conn_request. 17356 */ 17357 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17358 17359 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17360 /* 17361 * Someone blewoff the eager before we could finish 17362 * the accept. 17363 * 17364 * The only reason eager exists it because we put in 17365 * a ref on it when conn ind went up. We need to send 17366 * a disconnect indication up while the last reference 17367 * on the eager will be dropped by the squeue when we 17368 * return. 17369 */ 17370 ASSERT(tcp->tcp_listener == NULL); 17371 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17372 struct T_discon_ind *tdi; 17373 17374 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17375 /* 17376 * Let us reuse the incoming mblk to avoid memory 17377 * allocation failure problems. We know that the 17378 * size of the incoming mblk i.e. stroptions is greater 17379 * than sizeof T_discon_ind. So the reallocb below 17380 * can't fail. 17381 */ 17382 freemsg(mp->b_cont); 17383 mp->b_cont = NULL; 17384 ASSERT(DB_REF(mp) == 1); 17385 mp = reallocb(mp, sizeof (struct T_discon_ind), 17386 B_FALSE); 17387 ASSERT(mp != NULL); 17388 DB_TYPE(mp) = M_PROTO; 17389 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17390 tdi = (struct T_discon_ind *)mp->b_rptr; 17391 if (tcp->tcp_issocket) { 17392 tdi->DISCON_reason = ECONNREFUSED; 17393 tdi->SEQ_number = 0; 17394 } else { 17395 tdi->DISCON_reason = ENOPROTOOPT; 17396 tdi->SEQ_number = 17397 tcp->tcp_conn_req_seqnum; 17398 } 17399 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17400 putnext(q, mp); 17401 } else { 17402 freemsg(mp); 17403 } 17404 if (tcp->tcp_hard_binding) { 17405 tcp->tcp_hard_binding = B_FALSE; 17406 tcp->tcp_hard_bound = B_TRUE; 17407 } 17408 tcp->tcp_detached = B_FALSE; 17409 return; 17410 } 17411 17412 mp1 = stropt_mp->b_cont; 17413 stropt_mp->b_cont = NULL; 17414 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17415 stropt = (struct stroptions *)stropt_mp->b_rptr; 17416 17417 while (mp1 != NULL) { 17418 mp = mp1; 17419 mp1 = mp1->b_cont; 17420 mp->b_cont = NULL; 17421 tcp->tcp_drop_opt_ack_cnt++; 17422 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17423 } 17424 mp = NULL; 17425 17426 /* 17427 * For a loopback connection with tcp_direct_sockfs on, note that 17428 * we don't have to protect tcp_rcv_list yet because synchronous 17429 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17430 * possibly race with us. 17431 */ 17432 17433 /* 17434 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17435 * properly. This is the first time we know of the acceptor' 17436 * queue. So we do it here. 17437 */ 17438 if (tcp->tcp_rcv_list == NULL) { 17439 /* 17440 * Recv queue is empty, tcp_rwnd should not have changed. 17441 * That means it should be equal to the listener's tcp_rwnd. 17442 */ 17443 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17444 } else { 17445 #ifdef DEBUG 17446 uint_t cnt = 0; 17447 17448 mp1 = tcp->tcp_rcv_list; 17449 while ((mp = mp1) != NULL) { 17450 mp1 = mp->b_next; 17451 cnt += msgdsize(mp); 17452 } 17453 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17454 #endif 17455 /* There is some data, add them back to get the max. */ 17456 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17457 } 17458 17459 stropt->so_flags = SO_HIWAT; 17460 stropt->so_hiwat = MAX(q->q_hiwat, tcp_sth_rcv_hiwat); 17461 17462 stropt->so_flags |= SO_MAXBLK; 17463 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17464 17465 /* 17466 * This is the first time we run on the correct 17467 * queue after tcp_accept. So fix all the q parameters 17468 * here. 17469 */ 17470 /* Allocate room for SACK options if needed. */ 17471 stropt->so_flags |= SO_WROFF; 17472 if (tcp->tcp_fused) { 17473 ASSERT(tcp->tcp_loopback); 17474 ASSERT(tcp->tcp_loopback_peer != NULL); 17475 /* 17476 * For fused tcp loopback, set the stream head's write 17477 * offset value to zero since we won't be needing any room 17478 * for TCP/IP headers. This would also improve performance 17479 * since it would reduce the amount of work done by kmem. 17480 * Non-fused tcp loopback case is handled separately below. 17481 */ 17482 stropt->so_wroff = 0; 17483 /* 17484 * Record the stream head's high water mark for this endpoint; 17485 * this is used for flow-control purposes in tcp_fuse_output(). 17486 */ 17487 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 17488 /* 17489 * Update the peer's transmit parameters according to 17490 * our recently calculated high water mark value. 17491 */ 17492 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17493 } else if (tcp->tcp_snd_sack_ok) { 17494 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17495 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra); 17496 } else { 17497 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17498 tcp_wroff_xtra); 17499 } 17500 17501 /* 17502 * If this is endpoint is handling SSL, then reserve extra 17503 * offset and space at the end. 17504 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17505 * overriding the previous setting. The extra cost of signing and 17506 * encrypting multiple MSS-size records (12 of them with Ethernet), 17507 * instead of a single contiguous one by the stream head 17508 * largely outweighs the statistical reduction of ACKs, when 17509 * applicable. The peer will also save on decyption and verification 17510 * costs. 17511 */ 17512 if (tcp->tcp_kssl_ctx != NULL) { 17513 stropt->so_wroff += SSL3_WROFFSET; 17514 17515 stropt->so_flags |= SO_TAIL; 17516 stropt->so_tail = SSL3_MAX_TAIL_LEN; 17517 17518 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 17519 } 17520 17521 /* Send the options up */ 17522 putnext(q, stropt_mp); 17523 17524 /* 17525 * Pass up any data and/or a fin that has been received. 17526 * 17527 * Adjust receive window in case it had decreased 17528 * (because there is data <=> tcp_rcv_list != NULL) 17529 * while the connection was detached. Note that 17530 * in case the eager was flow-controlled, w/o this 17531 * code, the rwnd may never open up again! 17532 */ 17533 if (tcp->tcp_rcv_list != NULL) { 17534 /* We drain directly in case of fused tcp loopback */ 17535 if (!tcp->tcp_fused && canputnext(q)) { 17536 tcp->tcp_rwnd = q->q_hiwat; 17537 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 17538 << tcp->tcp_rcv_ws; 17539 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 17540 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17541 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 17542 tcp_xmit_ctl(NULL, 17543 tcp, (tcp->tcp_swnd == 0) ? 17544 tcp->tcp_suna : tcp->tcp_snxt, 17545 tcp->tcp_rnxt, TH_ACK); 17546 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 17547 } 17548 17549 } 17550 (void) tcp_rcv_drain(q, tcp); 17551 17552 /* 17553 * For fused tcp loopback, back-enable peer endpoint 17554 * if it's currently flow-controlled. 17555 */ 17556 if (tcp->tcp_fused && 17557 tcp->tcp_loopback_peer->tcp_flow_stopped) { 17558 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17559 17560 ASSERT(peer_tcp != NULL); 17561 ASSERT(peer_tcp->tcp_fused); 17562 17563 tcp_clrqfull(peer_tcp); 17564 TCP_STAT(tcp_fusion_backenabled); 17565 } 17566 } 17567 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17568 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17569 mp = mi_tpi_ordrel_ind(); 17570 if (mp) { 17571 tcp->tcp_ordrel_done = B_TRUE; 17572 putnext(q, mp); 17573 if (tcp->tcp_deferred_clean_death) { 17574 /* 17575 * tcp_clean_death was deferred 17576 * for T_ORDREL_IND - do it now 17577 */ 17578 (void) tcp_clean_death(tcp, 17579 tcp->tcp_client_errno, 21); 17580 tcp->tcp_deferred_clean_death = B_FALSE; 17581 } 17582 } else { 17583 /* 17584 * Run the orderly release in the 17585 * service routine. 17586 */ 17587 qenable(q); 17588 } 17589 } 17590 if (tcp->tcp_hard_binding) { 17591 tcp->tcp_hard_binding = B_FALSE; 17592 tcp->tcp_hard_bound = B_TRUE; 17593 } 17594 17595 tcp->tcp_detached = B_FALSE; 17596 17597 /* We can enable synchronous streams now */ 17598 if (tcp->tcp_fused) { 17599 tcp_fuse_syncstr_enable_pair(tcp); 17600 } 17601 17602 if (tcp->tcp_ka_enabled) { 17603 tcp->tcp_ka_last_intrvl = 0; 17604 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17605 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17606 } 17607 17608 /* 17609 * At this point, eager is fully established and will 17610 * have the following references - 17611 * 17612 * 2 references for connection to exist (1 for TCP and 1 for IP). 17613 * 1 reference for the squeue which will be dropped by the squeue as 17614 * soon as this function returns. 17615 * There will be 1 additonal reference for being in classifier 17616 * hash list provided something bad hasn't happened. 17617 */ 17618 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17619 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17620 } 17621 17622 /* 17623 * The function called through squeue to get behind listener's perimeter to 17624 * send a deffered conn_ind. 17625 */ 17626 /* ARGSUSED */ 17627 void 17628 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17629 { 17630 conn_t *connp = (conn_t *)arg; 17631 tcp_t *listener = connp->conn_tcp; 17632 17633 if (listener->tcp_state == TCPS_CLOSED || 17634 TCP_IS_DETACHED(listener)) { 17635 /* 17636 * If listener has closed, it would have caused a 17637 * a cleanup/blowoff to happen for the eager. 17638 */ 17639 tcp_t *tcp; 17640 struct T_conn_ind *conn_ind; 17641 17642 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17643 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17644 conn_ind->OPT_length); 17645 /* 17646 * We need to drop the ref on eager that was put 17647 * tcp_rput_data() before trying to send the conn_ind 17648 * to listener. The conn_ind was deferred in tcp_send_conn_ind 17649 * and tcp_wput_accept() is sending this deferred conn_ind but 17650 * listener is closed so we drop the ref. 17651 */ 17652 CONN_DEC_REF(tcp->tcp_connp); 17653 freemsg(mp); 17654 return; 17655 } 17656 putnext(listener->tcp_rq, mp); 17657 } 17658 17659 17660 /* 17661 * This is the STREAMS entry point for T_CONN_RES coming down on 17662 * Acceptor STREAM when sockfs listener does accept processing. 17663 * Read the block comment on top pf tcp_conn_request(). 17664 */ 17665 void 17666 tcp_wput_accept(queue_t *q, mblk_t *mp) 17667 { 17668 queue_t *rq = RD(q); 17669 struct T_conn_res *conn_res; 17670 tcp_t *eager; 17671 tcp_t *listener; 17672 struct T_ok_ack *ok; 17673 t_scalar_t PRIM_type; 17674 mblk_t *opt_mp; 17675 conn_t *econnp; 17676 17677 ASSERT(DB_TYPE(mp) == M_PROTO); 17678 17679 conn_res = (struct T_conn_res *)mp->b_rptr; 17680 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17681 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17682 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17683 if (mp != NULL) 17684 putnext(rq, mp); 17685 return; 17686 } 17687 switch (conn_res->PRIM_type) { 17688 case O_T_CONN_RES: 17689 case T_CONN_RES: 17690 /* 17691 * We pass up an err ack if allocb fails. This will 17692 * cause sockfs to issue a T_DISCON_REQ which will cause 17693 * tcp_eager_blowoff to be called. sockfs will then call 17694 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17695 * we need to do the allocb up here because we have to 17696 * make sure rq->q_qinfo->qi_qclose still points to the 17697 * correct function (tcpclose_accept) in case allocb 17698 * fails. 17699 */ 17700 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17701 if (opt_mp == NULL) { 17702 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17703 if (mp != NULL) 17704 putnext(rq, mp); 17705 return; 17706 } 17707 17708 bcopy(mp->b_rptr + conn_res->OPT_offset, 17709 &eager, conn_res->OPT_length); 17710 PRIM_type = conn_res->PRIM_type; 17711 mp->b_datap->db_type = M_PCPROTO; 17712 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 17713 ok = (struct T_ok_ack *)mp->b_rptr; 17714 ok->PRIM_type = T_OK_ACK; 17715 ok->CORRECT_prim = PRIM_type; 17716 econnp = eager->tcp_connp; 17717 econnp->conn_dev = (dev_t)q->q_ptr; 17718 eager->tcp_rq = rq; 17719 eager->tcp_wq = q; 17720 rq->q_ptr = econnp; 17721 rq->q_qinfo = &tcp_rinit; 17722 q->q_ptr = econnp; 17723 q->q_qinfo = &tcp_winit; 17724 listener = eager->tcp_listener; 17725 eager->tcp_issocket = B_TRUE; 17726 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17727 17728 /* Put the ref for IP */ 17729 CONN_INC_REF(econnp); 17730 17731 /* 17732 * We should have minimum of 3 references on the conn 17733 * at this point. One each for TCP and IP and one for 17734 * the T_conn_ind that was sent up when the 3-way handshake 17735 * completed. In the normal case we would also have another 17736 * reference (making a total of 4) for the conn being in the 17737 * classifier hash list. However the eager could have received 17738 * an RST subsequently and tcp_closei_local could have removed 17739 * the eager from the classifier hash list, hence we can't 17740 * assert that reference. 17741 */ 17742 ASSERT(econnp->conn_ref >= 3); 17743 17744 /* 17745 * Send the new local address also up to sockfs. There 17746 * should already be enough space in the mp that came 17747 * down from soaccept(). 17748 */ 17749 if (eager->tcp_family == AF_INET) { 17750 sin_t *sin; 17751 17752 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17753 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 17754 sin = (sin_t *)mp->b_wptr; 17755 mp->b_wptr += sizeof (sin_t); 17756 sin->sin_family = AF_INET; 17757 sin->sin_port = eager->tcp_lport; 17758 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 17759 } else { 17760 sin6_t *sin6; 17761 17762 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17763 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 17764 sin6 = (sin6_t *)mp->b_wptr; 17765 mp->b_wptr += sizeof (sin6_t); 17766 sin6->sin6_family = AF_INET6; 17767 sin6->sin6_port = eager->tcp_lport; 17768 if (eager->tcp_ipversion == IPV4_VERSION) { 17769 sin6->sin6_flowinfo = 0; 17770 IN6_IPADDR_TO_V4MAPPED( 17771 eager->tcp_ipha->ipha_src, 17772 &sin6->sin6_addr); 17773 } else { 17774 ASSERT(eager->tcp_ip6h != NULL); 17775 sin6->sin6_flowinfo = 17776 eager->tcp_ip6h->ip6_vcf & 17777 ~IPV6_VERS_AND_FLOW_MASK; 17778 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 17779 } 17780 sin6->sin6_scope_id = 0; 17781 sin6->__sin6_src_id = 0; 17782 } 17783 17784 putnext(rq, mp); 17785 17786 opt_mp->b_datap->db_type = M_SETOPTS; 17787 opt_mp->b_wptr += sizeof (struct stroptions); 17788 17789 /* 17790 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 17791 * from listener to acceptor. The message is chained on the 17792 * bind_mp which tcp_rput_other will send down to IP. 17793 */ 17794 if (listener->tcp_bound_if != 0) { 17795 /* allocate optmgmt req */ 17796 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 17797 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 17798 sizeof (int)); 17799 if (mp != NULL) 17800 linkb(opt_mp, mp); 17801 } 17802 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 17803 uint_t on = 1; 17804 17805 /* allocate optmgmt req */ 17806 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 17807 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 17808 if (mp != NULL) 17809 linkb(opt_mp, mp); 17810 } 17811 17812 17813 mutex_enter(&listener->tcp_eager_lock); 17814 17815 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 17816 17817 tcp_t *tail; 17818 tcp_t *tcp; 17819 mblk_t *mp1; 17820 17821 tcp = listener->tcp_eager_prev_q0; 17822 /* 17823 * listener->tcp_eager_prev_q0 points to the TAIL of the 17824 * deferred T_conn_ind queue. We need to get to the head 17825 * of the queue in order to send up T_conn_ind the same 17826 * order as how the 3WHS is completed. 17827 */ 17828 while (tcp != listener) { 17829 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 17830 !tcp->tcp_kssl_pending) 17831 break; 17832 else 17833 tcp = tcp->tcp_eager_prev_q0; 17834 } 17835 /* None of the pending eagers can be sent up now */ 17836 if (tcp == listener) 17837 goto no_more_eagers; 17838 17839 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 17840 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17841 /* Move from q0 to q */ 17842 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 17843 listener->tcp_conn_req_cnt_q0--; 17844 listener->tcp_conn_req_cnt_q++; 17845 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 17846 tcp->tcp_eager_prev_q0; 17847 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 17848 tcp->tcp_eager_next_q0; 17849 tcp->tcp_eager_prev_q0 = NULL; 17850 tcp->tcp_eager_next_q0 = NULL; 17851 tcp->tcp_conn_def_q0 = B_FALSE; 17852 17853 /* 17854 * Insert at end of the queue because sockfs sends 17855 * down T_CONN_RES in chronological order. Leaving 17856 * the older conn indications at front of the queue 17857 * helps reducing search time. 17858 */ 17859 tail = listener->tcp_eager_last_q; 17860 if (tail != NULL) { 17861 tail->tcp_eager_next_q = tcp; 17862 } else { 17863 listener->tcp_eager_next_q = tcp; 17864 } 17865 listener->tcp_eager_last_q = tcp; 17866 tcp->tcp_eager_next_q = NULL; 17867 17868 /* Need to get inside the listener perimeter */ 17869 CONN_INC_REF(listener->tcp_connp); 17870 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 17871 tcp_send_pending, listener->tcp_connp, 17872 SQTAG_TCP_SEND_PENDING); 17873 } 17874 no_more_eagers: 17875 tcp_eager_unlink(eager); 17876 mutex_exit(&listener->tcp_eager_lock); 17877 17878 /* 17879 * At this point, the eager is detached from the listener 17880 * but we still have an extra refs on eager (apart from the 17881 * usual tcp references). The ref was placed in tcp_rput_data 17882 * before sending the conn_ind in tcp_send_conn_ind. 17883 * The ref will be dropped in tcp_accept_finish(). 17884 */ 17885 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 17886 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 17887 return; 17888 default: 17889 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 17890 if (mp != NULL) 17891 putnext(rq, mp); 17892 return; 17893 } 17894 } 17895 17896 void 17897 tcp_wput(queue_t *q, mblk_t *mp) 17898 { 17899 conn_t *connp = Q_TO_CONN(q); 17900 tcp_t *tcp; 17901 void (*output_proc)(); 17902 t_scalar_t type; 17903 uchar_t *rptr; 17904 struct iocblk *iocp; 17905 uint32_t msize; 17906 17907 ASSERT(connp->conn_ref >= 2); 17908 17909 switch (DB_TYPE(mp)) { 17910 case M_DATA: 17911 tcp = connp->conn_tcp; 17912 ASSERT(tcp != NULL); 17913 17914 msize = msgdsize(mp); 17915 17916 mutex_enter(&connp->conn_lock); 17917 CONN_INC_REF_LOCKED(connp); 17918 17919 tcp->tcp_squeue_bytes += msize; 17920 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 17921 mutex_exit(&connp->conn_lock); 17922 tcp_setqfull(tcp); 17923 } else 17924 mutex_exit(&connp->conn_lock); 17925 17926 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 17927 tcp_output, connp, SQTAG_TCP_OUTPUT); 17928 return; 17929 case M_PROTO: 17930 case M_PCPROTO: 17931 /* 17932 * if it is a snmp message, don't get behind the squeue 17933 */ 17934 tcp = connp->conn_tcp; 17935 rptr = mp->b_rptr; 17936 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 17937 type = ((union T_primitives *)rptr)->type; 17938 } else { 17939 if (tcp->tcp_debug) { 17940 (void) strlog(TCP_MOD_ID, 0, 1, 17941 SL_ERROR|SL_TRACE, 17942 "tcp_wput_proto, dropping one..."); 17943 } 17944 freemsg(mp); 17945 return; 17946 } 17947 if (type == T_SVR4_OPTMGMT_REQ) { 17948 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 17949 if (snmpcom_req(q, mp, tcp_snmp_set, tcp_snmp_get, 17950 cr)) { 17951 /* 17952 * This was a SNMP request 17953 */ 17954 return; 17955 } else { 17956 output_proc = tcp_wput_proto; 17957 } 17958 } else { 17959 output_proc = tcp_wput_proto; 17960 } 17961 break; 17962 case M_IOCTL: 17963 /* 17964 * Most ioctls can be processed right away without going via 17965 * squeues - process them right here. Those that do require 17966 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 17967 * are processed by tcp_wput_ioctl(). 17968 */ 17969 iocp = (struct iocblk *)mp->b_rptr; 17970 tcp = connp->conn_tcp; 17971 17972 switch (iocp->ioc_cmd) { 17973 case TCP_IOC_ABORT_CONN: 17974 tcp_ioctl_abort_conn(q, mp); 17975 return; 17976 case TI_GETPEERNAME: 17977 if (tcp->tcp_state < TCPS_SYN_RCVD) { 17978 iocp->ioc_error = ENOTCONN; 17979 iocp->ioc_count = 0; 17980 mp->b_datap->db_type = M_IOCACK; 17981 qreply(q, mp); 17982 return; 17983 } 17984 /* FALLTHRU */ 17985 case TI_GETMYNAME: 17986 mi_copyin(q, mp, NULL, 17987 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 17988 return; 17989 case ND_SET: 17990 /* nd_getset does the necessary checks */ 17991 case ND_GET: 17992 if (!nd_getset(q, tcp_g_nd, mp)) { 17993 CALL_IP_WPUT(connp, q, mp); 17994 return; 17995 } 17996 qreply(q, mp); 17997 return; 17998 case TCP_IOC_DEFAULT_Q: 17999 /* 18000 * Wants to be the default wq. Check the credentials 18001 * first, the rest is executed via squeue. 18002 */ 18003 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 18004 iocp->ioc_error = EPERM; 18005 iocp->ioc_count = 0; 18006 mp->b_datap->db_type = M_IOCACK; 18007 qreply(q, mp); 18008 return; 18009 } 18010 output_proc = tcp_wput_ioctl; 18011 break; 18012 default: 18013 output_proc = tcp_wput_ioctl; 18014 break; 18015 } 18016 break; 18017 default: 18018 output_proc = tcp_wput_nondata; 18019 break; 18020 } 18021 18022 CONN_INC_REF(connp); 18023 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18024 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18025 } 18026 18027 /* 18028 * Initial STREAMS write side put() procedure for sockets. It tries to 18029 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18030 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18031 * are handled by tcp_wput() as usual. 18032 * 18033 * All further messages will also be handled by tcp_wput() because we cannot 18034 * be sure that the above short cut is safe later. 18035 */ 18036 static void 18037 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18038 { 18039 conn_t *connp = Q_TO_CONN(wq); 18040 tcp_t *tcp = connp->conn_tcp; 18041 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18042 18043 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18044 wq->q_qinfo = &tcp_winit; 18045 18046 ASSERT(IPCL_IS_TCP(connp)); 18047 ASSERT(TCP_IS_SOCKET(tcp)); 18048 18049 if (DB_TYPE(mp) == M_PCPROTO && 18050 MBLKL(mp) == sizeof (struct T_capability_req) && 18051 car->PRIM_type == T_CAPABILITY_REQ) { 18052 tcp_capability_req(tcp, mp); 18053 return; 18054 } 18055 18056 tcp_wput(wq, mp); 18057 } 18058 18059 static boolean_t 18060 tcp_zcopy_check(tcp_t *tcp) 18061 { 18062 conn_t *connp = tcp->tcp_connp; 18063 ire_t *ire; 18064 boolean_t zc_enabled = B_FALSE; 18065 18066 if (do_tcpzcopy == 2) 18067 zc_enabled = B_TRUE; 18068 else if (tcp->tcp_ipversion == IPV4_VERSION && 18069 IPCL_IS_CONNECTED(connp) && 18070 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18071 connp->conn_dontroute == 0 && 18072 !connp->conn_nexthop_set && 18073 connp->conn_xmit_if_ill == NULL && 18074 connp->conn_nofailover_ill == NULL && 18075 do_tcpzcopy == 1) { 18076 /* 18077 * the checks above closely resemble the fast path checks 18078 * in tcp_send_data(). 18079 */ 18080 mutex_enter(&connp->conn_lock); 18081 ire = connp->conn_ire_cache; 18082 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18083 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18084 IRE_REFHOLD(ire); 18085 if (ire->ire_stq != NULL) { 18086 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18087 18088 zc_enabled = ill && (ill->ill_capabilities & 18089 ILL_CAPAB_ZEROCOPY) && 18090 (ill->ill_zerocopy_capab-> 18091 ill_zerocopy_flags != 0); 18092 } 18093 IRE_REFRELE(ire); 18094 } 18095 mutex_exit(&connp->conn_lock); 18096 } 18097 tcp->tcp_snd_zcopy_on = zc_enabled; 18098 if (!TCP_IS_DETACHED(tcp)) { 18099 if (zc_enabled) { 18100 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 18101 TCP_STAT(tcp_zcopy_on); 18102 } else { 18103 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18104 TCP_STAT(tcp_zcopy_off); 18105 } 18106 } 18107 return (zc_enabled); 18108 } 18109 18110 static mblk_t * 18111 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18112 { 18113 if (do_tcpzcopy == 2) 18114 return (bp); 18115 else if (tcp->tcp_snd_zcopy_on) { 18116 tcp->tcp_snd_zcopy_on = B_FALSE; 18117 if (!TCP_IS_DETACHED(tcp)) { 18118 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18119 TCP_STAT(tcp_zcopy_disable); 18120 } 18121 } 18122 return (tcp_zcopy_backoff(tcp, bp, 0)); 18123 } 18124 18125 /* 18126 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18127 * the original desballoca'ed segmapped mblk. 18128 */ 18129 static mblk_t * 18130 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18131 { 18132 mblk_t *head, *tail, *nbp; 18133 if (IS_VMLOANED_MBLK(bp)) { 18134 TCP_STAT(tcp_zcopy_backoff); 18135 if ((head = copyb(bp)) == NULL) { 18136 /* fail to backoff; leave it for the next backoff */ 18137 tcp->tcp_xmit_zc_clean = B_FALSE; 18138 return (bp); 18139 } 18140 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18141 if (fix_xmitlist) 18142 tcp_zcopy_notify(tcp); 18143 else 18144 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18145 } 18146 nbp = bp->b_cont; 18147 if (fix_xmitlist) { 18148 head->b_prev = bp->b_prev; 18149 head->b_next = bp->b_next; 18150 if (tcp->tcp_xmit_tail == bp) 18151 tcp->tcp_xmit_tail = head; 18152 } 18153 bp->b_next = NULL; 18154 bp->b_prev = NULL; 18155 freeb(bp); 18156 } else { 18157 head = bp; 18158 nbp = bp->b_cont; 18159 } 18160 tail = head; 18161 while (nbp) { 18162 if (IS_VMLOANED_MBLK(nbp)) { 18163 TCP_STAT(tcp_zcopy_backoff); 18164 if ((tail->b_cont = copyb(nbp)) == NULL) { 18165 tcp->tcp_xmit_zc_clean = B_FALSE; 18166 tail->b_cont = nbp; 18167 return (head); 18168 } 18169 tail = tail->b_cont; 18170 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18171 if (fix_xmitlist) 18172 tcp_zcopy_notify(tcp); 18173 else 18174 tail->b_datap->db_struioflag |= 18175 STRUIO_ZCNOTIFY; 18176 } 18177 bp = nbp; 18178 nbp = nbp->b_cont; 18179 if (fix_xmitlist) { 18180 tail->b_prev = bp->b_prev; 18181 tail->b_next = bp->b_next; 18182 if (tcp->tcp_xmit_tail == bp) 18183 tcp->tcp_xmit_tail = tail; 18184 } 18185 bp->b_next = NULL; 18186 bp->b_prev = NULL; 18187 freeb(bp); 18188 } else { 18189 tail->b_cont = nbp; 18190 tail = nbp; 18191 nbp = nbp->b_cont; 18192 } 18193 } 18194 if (fix_xmitlist) { 18195 tcp->tcp_xmit_last = tail; 18196 tcp->tcp_xmit_zc_clean = B_TRUE; 18197 } 18198 return (head); 18199 } 18200 18201 static void 18202 tcp_zcopy_notify(tcp_t *tcp) 18203 { 18204 struct stdata *stp; 18205 18206 if (tcp->tcp_detached) 18207 return; 18208 stp = STREAM(tcp->tcp_rq); 18209 mutex_enter(&stp->sd_lock); 18210 stp->sd_flag |= STZCNOTIFY; 18211 cv_broadcast(&stp->sd_zcopy_wait); 18212 mutex_exit(&stp->sd_lock); 18213 } 18214 18215 static void 18216 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18217 { 18218 ipha_t *ipha; 18219 ipaddr_t src; 18220 ipaddr_t dst; 18221 uint32_t cksum; 18222 ire_t *ire; 18223 uint16_t *up; 18224 ill_t *ill; 18225 conn_t *connp = tcp->tcp_connp; 18226 uint32_t hcksum_txflags = 0; 18227 mblk_t *ire_fp_mp; 18228 uint_t ire_fp_mp_len; 18229 18230 ASSERT(DB_TYPE(mp) == M_DATA); 18231 18232 if (DB_CRED(mp) == NULL) 18233 mblk_setcred(mp, CONN_CRED(connp)); 18234 18235 ipha = (ipha_t *)mp->b_rptr; 18236 src = ipha->ipha_src; 18237 dst = ipha->ipha_dst; 18238 18239 /* 18240 * Drop off fast path for IPv6 and also if options are present or 18241 * we need to resolve a TS label. 18242 */ 18243 if (tcp->tcp_ipversion != IPV4_VERSION || 18244 !IPCL_IS_CONNECTED(connp) || 18245 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18246 connp->conn_dontroute || 18247 connp->conn_nexthop_set || 18248 connp->conn_xmit_if_ill != NULL || 18249 connp->conn_nofailover_ill != NULL || 18250 !connp->conn_ulp_labeled || 18251 ipha->ipha_ident == IP_HDR_INCLUDED || 18252 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18253 IPP_ENABLED(IPP_LOCAL_OUT)) { 18254 if (tcp->tcp_snd_zcopy_aware) 18255 mp = tcp_zcopy_disable(tcp, mp); 18256 TCP_STAT(tcp_ip_send); 18257 CALL_IP_WPUT(connp, q, mp); 18258 return; 18259 } 18260 18261 mutex_enter(&connp->conn_lock); 18262 ire = connp->conn_ire_cache; 18263 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18264 if (ire != NULL && ire->ire_addr == dst && 18265 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18266 IRE_REFHOLD(ire); 18267 mutex_exit(&connp->conn_lock); 18268 } else { 18269 boolean_t cached = B_FALSE; 18270 18271 /* force a recheck later on */ 18272 tcp->tcp_ire_ill_check_done = B_FALSE; 18273 18274 TCP_DBGSTAT(tcp_ire_null1); 18275 connp->conn_ire_cache = NULL; 18276 mutex_exit(&connp->conn_lock); 18277 if (ire != NULL) 18278 IRE_REFRELE_NOTR(ire); 18279 ire = ire_cache_lookup(dst, connp->conn_zoneid, 18280 MBLK_GETLABEL(mp)); 18281 if (ire == NULL) { 18282 if (tcp->tcp_snd_zcopy_aware) 18283 mp = tcp_zcopy_backoff(tcp, mp, 0); 18284 TCP_STAT(tcp_ire_null); 18285 CALL_IP_WPUT(connp, q, mp); 18286 return; 18287 } 18288 IRE_REFHOLD_NOTR(ire); 18289 /* 18290 * Since we are inside the squeue, there cannot be another 18291 * thread in TCP trying to set the conn_ire_cache now. The 18292 * check for IRE_MARK_CONDEMNED ensures that an interface 18293 * unplumb thread has not yet started cleaning up the conns. 18294 * Hence we don't need to grab the conn lock. 18295 */ 18296 if (!(connp->conn_state_flags & CONN_CLOSING)) { 18297 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18298 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18299 connp->conn_ire_cache = ire; 18300 cached = B_TRUE; 18301 } 18302 rw_exit(&ire->ire_bucket->irb_lock); 18303 } 18304 18305 /* 18306 * We can continue to use the ire but since it was 18307 * not cached, we should drop the extra reference. 18308 */ 18309 if (!cached) 18310 IRE_REFRELE_NOTR(ire); 18311 18312 /* 18313 * Rampart note: no need to select a new label here, since 18314 * labels are not allowed to change during the life of a TCP 18315 * connection. 18316 */ 18317 } 18318 18319 if (ire->ire_flags & RTF_MULTIRT || 18320 ire->ire_stq == NULL || 18321 ire->ire_max_frag < ntohs(ipha->ipha_length) || 18322 (ire_fp_mp = ire->ire_fp_mp) == NULL || 18323 (ire_fp_mp_len = MBLKL(ire_fp_mp)) > MBLKHEAD(mp)) { 18324 if (tcp->tcp_snd_zcopy_aware) 18325 mp = tcp_zcopy_disable(tcp, mp); 18326 TCP_STAT(tcp_ip_ire_send); 18327 IRE_REFRELE(ire); 18328 CALL_IP_WPUT(connp, q, mp); 18329 return; 18330 } 18331 18332 ill = ire_to_ill(ire); 18333 if (connp->conn_outgoing_ill != NULL) { 18334 ill_t *conn_outgoing_ill = NULL; 18335 /* 18336 * Choose a good ill in the group to send the packets on. 18337 */ 18338 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18339 ill = ire_to_ill(ire); 18340 } 18341 ASSERT(ill != NULL); 18342 18343 if (!tcp->tcp_ire_ill_check_done) { 18344 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18345 tcp->tcp_ire_ill_check_done = B_TRUE; 18346 } 18347 18348 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18349 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18350 #ifndef _BIG_ENDIAN 18351 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18352 #endif 18353 18354 /* 18355 * Check to see if we need to re-enable MDT for this connection 18356 * because it was previously disabled due to changes in the ill; 18357 * note that by doing it here, this re-enabling only applies when 18358 * the packet is not dispatched through CALL_IP_WPUT(). 18359 * 18360 * That means for IPv4, it is worth re-enabling MDT for the fastpath 18361 * case, since that's how we ended up here. For IPv6, we do the 18362 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18363 */ 18364 if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18365 /* 18366 * Restore MDT for this connection, so that next time around 18367 * it is eligible to go through tcp_multisend() path again. 18368 */ 18369 TCP_STAT(tcp_mdt_conn_resumed1); 18370 tcp->tcp_mdt = B_TRUE; 18371 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18372 "interface %s\n", (void *)connp, ill->ill_name)); 18373 } 18374 18375 if (tcp->tcp_snd_zcopy_aware) { 18376 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18377 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18378 mp = tcp_zcopy_disable(tcp, mp); 18379 /* 18380 * we shouldn't need to reset ipha as the mp containing 18381 * ipha should never be a zero-copy mp. 18382 */ 18383 } 18384 18385 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18386 ASSERT(ill->ill_hcksum_capab != NULL); 18387 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18388 } 18389 18390 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18391 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18392 18393 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18394 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18395 18396 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18397 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18398 18399 /* Software checksum? */ 18400 if (DB_CKSUMFLAGS(mp) == 0) { 18401 TCP_STAT(tcp_out_sw_cksum); 18402 TCP_STAT_UPDATE(tcp_out_sw_cksum_bytes, 18403 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18404 } 18405 18406 ipha->ipha_fragment_offset_and_flags |= 18407 (uint32_t)htons(ire->ire_frag_flag); 18408 18409 /* Calculate IP header checksum if hardware isn't capable */ 18410 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18411 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18412 ((uint16_t *)ipha)[4]); 18413 } 18414 18415 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18416 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18417 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18418 18419 UPDATE_OB_PKT_COUNT(ire); 18420 ire->ire_last_used_time = lbolt; 18421 BUMP_MIB(&ip_mib, ipOutRequests); 18422 18423 if (ILL_DLS_CAPABLE(ill)) { 18424 /* 18425 * Send the packet directly to DLD, where it may be queued 18426 * depending on the availability of transmit resources at 18427 * the media layer. 18428 */ 18429 IP_DLS_ILL_TX(ill, mp); 18430 } else { 18431 putnext(ire->ire_stq, mp); 18432 } 18433 IRE_REFRELE(ire); 18434 } 18435 18436 /* 18437 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18438 * if the receiver shrinks the window, i.e. moves the right window to the 18439 * left, the we should not send new data, but should retransmit normally the 18440 * old unacked data between suna and suna + swnd. We might has sent data 18441 * that is now outside the new window, pretend that we didn't send it. 18442 */ 18443 static void 18444 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18445 { 18446 uint32_t snxt = tcp->tcp_snxt; 18447 mblk_t *xmit_tail; 18448 int32_t offset; 18449 18450 ASSERT(shrunk_count > 0); 18451 18452 /* Pretend we didn't send the data outside the window */ 18453 snxt -= shrunk_count; 18454 18455 /* Get the mblk and the offset in it per the shrunk window */ 18456 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 18457 18458 ASSERT(xmit_tail != NULL); 18459 18460 /* Reset all the values per the now shrunk window */ 18461 tcp->tcp_snxt = snxt; 18462 tcp->tcp_xmit_tail = xmit_tail; 18463 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 18464 offset; 18465 tcp->tcp_unsent += shrunk_count; 18466 18467 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18468 /* 18469 * Make sure the timer is running so that we will probe a zero 18470 * window. 18471 */ 18472 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18473 } 18474 18475 18476 /* 18477 * The TCP normal data output path. 18478 * NOTE: the logic of the fast path is duplicated from this function. 18479 */ 18480 static void 18481 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18482 { 18483 int len; 18484 mblk_t *local_time; 18485 mblk_t *mp1; 18486 uint32_t snxt; 18487 int tail_unsent; 18488 int tcpstate; 18489 int usable = 0; 18490 mblk_t *xmit_tail; 18491 queue_t *q = tcp->tcp_wq; 18492 int32_t mss; 18493 int32_t num_sack_blk = 0; 18494 int32_t tcp_hdr_len; 18495 int32_t tcp_tcp_hdr_len; 18496 int mdt_thres; 18497 int rc; 18498 18499 tcpstate = tcp->tcp_state; 18500 if (mp == NULL) { 18501 /* 18502 * tcp_wput_data() with NULL mp should only be called when 18503 * there is unsent data. 18504 */ 18505 ASSERT(tcp->tcp_unsent > 0); 18506 /* Really tacky... but we need this for detached closes. */ 18507 len = tcp->tcp_unsent; 18508 goto data_null; 18509 } 18510 18511 #if CCS_STATS 18512 wrw_stats.tot.count++; 18513 wrw_stats.tot.bytes += msgdsize(mp); 18514 #endif 18515 ASSERT(mp->b_datap->db_type == M_DATA); 18516 /* 18517 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18518 * or before a connection attempt has begun. 18519 */ 18520 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18521 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18522 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18523 #ifdef DEBUG 18524 cmn_err(CE_WARN, 18525 "tcp_wput_data: data after ordrel, %s", 18526 tcp_display(tcp, NULL, 18527 DISP_ADDR_AND_PORT)); 18528 #else 18529 if (tcp->tcp_debug) { 18530 (void) strlog(TCP_MOD_ID, 0, 1, 18531 SL_TRACE|SL_ERROR, 18532 "tcp_wput_data: data after ordrel, %s\n", 18533 tcp_display(tcp, NULL, 18534 DISP_ADDR_AND_PORT)); 18535 } 18536 #endif /* DEBUG */ 18537 } 18538 if (tcp->tcp_snd_zcopy_aware && 18539 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18540 tcp_zcopy_notify(tcp); 18541 freemsg(mp); 18542 if (tcp->tcp_flow_stopped && 18543 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18544 tcp_clrqfull(tcp); 18545 } 18546 return; 18547 } 18548 18549 /* Strip empties */ 18550 for (;;) { 18551 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18552 (uintptr_t)INT_MAX); 18553 len = (int)(mp->b_wptr - mp->b_rptr); 18554 if (len > 0) 18555 break; 18556 mp1 = mp; 18557 mp = mp->b_cont; 18558 freeb(mp1); 18559 if (!mp) { 18560 return; 18561 } 18562 } 18563 18564 /* If we are the first on the list ... */ 18565 if (tcp->tcp_xmit_head == NULL) { 18566 tcp->tcp_xmit_head = mp; 18567 tcp->tcp_xmit_tail = mp; 18568 tcp->tcp_xmit_tail_unsent = len; 18569 } else { 18570 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18571 struct datab *dp; 18572 18573 mp1 = tcp->tcp_xmit_last; 18574 if (len < tcp_tx_pull_len && 18575 (dp = mp1->b_datap)->db_ref == 1 && 18576 dp->db_lim - mp1->b_wptr >= len) { 18577 ASSERT(len > 0); 18578 ASSERT(!mp1->b_cont); 18579 if (len == 1) { 18580 *mp1->b_wptr++ = *mp->b_rptr; 18581 } else { 18582 bcopy(mp->b_rptr, mp1->b_wptr, len); 18583 mp1->b_wptr += len; 18584 } 18585 if (mp1 == tcp->tcp_xmit_tail) 18586 tcp->tcp_xmit_tail_unsent += len; 18587 mp1->b_cont = mp->b_cont; 18588 if (tcp->tcp_snd_zcopy_aware && 18589 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18590 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18591 freeb(mp); 18592 mp = mp1; 18593 } else { 18594 tcp->tcp_xmit_last->b_cont = mp; 18595 } 18596 len += tcp->tcp_unsent; 18597 } 18598 18599 /* Tack on however many more positive length mblks we have */ 18600 if ((mp1 = mp->b_cont) != NULL) { 18601 do { 18602 int tlen; 18603 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18604 (uintptr_t)INT_MAX); 18605 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18606 if (tlen <= 0) { 18607 mp->b_cont = mp1->b_cont; 18608 freeb(mp1); 18609 } else { 18610 len += tlen; 18611 mp = mp1; 18612 } 18613 } while ((mp1 = mp->b_cont) != NULL); 18614 } 18615 tcp->tcp_xmit_last = mp; 18616 tcp->tcp_unsent = len; 18617 18618 if (urgent) 18619 usable = 1; 18620 18621 data_null: 18622 snxt = tcp->tcp_snxt; 18623 xmit_tail = tcp->tcp_xmit_tail; 18624 tail_unsent = tcp->tcp_xmit_tail_unsent; 18625 18626 /* 18627 * Note that tcp_mss has been adjusted to take into account the 18628 * timestamp option if applicable. Because SACK options do not 18629 * appear in every TCP segments and they are of variable lengths, 18630 * they cannot be included in tcp_mss. Thus we need to calculate 18631 * the actual segment length when we need to send a segment which 18632 * includes SACK options. 18633 */ 18634 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 18635 int32_t opt_len; 18636 18637 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 18638 tcp->tcp_num_sack_blk); 18639 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 18640 2 + TCPOPT_HEADER_LEN; 18641 mss = tcp->tcp_mss - opt_len; 18642 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 18643 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 18644 } else { 18645 mss = tcp->tcp_mss; 18646 tcp_hdr_len = tcp->tcp_hdr_len; 18647 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 18648 } 18649 18650 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18651 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18652 SET_TCP_INIT_CWND(tcp, mss, tcp_slow_start_after_idle); 18653 } 18654 if (tcpstate == TCPS_SYN_RCVD) { 18655 /* 18656 * The three-way connection establishment handshake is not 18657 * complete yet. We want to queue the data for transmission 18658 * after entering ESTABLISHED state (RFC793). A jump to 18659 * "done" label effectively leaves data on the queue. 18660 */ 18661 goto done; 18662 } else { 18663 int usable_r; 18664 18665 /* 18666 * In the special case when cwnd is zero, which can only 18667 * happen if the connection is ECN capable, return now. 18668 * New segments is sent using tcp_timer(). The timer 18669 * is set in tcp_rput_data(). 18670 */ 18671 if (tcp->tcp_cwnd == 0) { 18672 /* 18673 * Note that tcp_cwnd is 0 before 3-way handshake is 18674 * finished. 18675 */ 18676 ASSERT(tcp->tcp_ecn_ok || 18677 tcp->tcp_state < TCPS_ESTABLISHED); 18678 return; 18679 } 18680 18681 /* NOTE: trouble if xmitting while SYN not acked? */ 18682 usable_r = snxt - tcp->tcp_suna; 18683 usable_r = tcp->tcp_swnd - usable_r; 18684 18685 /* 18686 * Check if the receiver has shrunk the window. If 18687 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 18688 * cannot be set as there is unsent data, so FIN cannot 18689 * be sent out. Otherwise, we need to take into account 18690 * of FIN as it consumes an "invisible" sequence number. 18691 */ 18692 ASSERT(tcp->tcp_fin_sent == 0); 18693 if (usable_r < 0) { 18694 /* 18695 * The receiver has shrunk the window and we have sent 18696 * -usable_r date beyond the window, re-adjust. 18697 * 18698 * If TCP window scaling is enabled, there can be 18699 * round down error as the advertised receive window 18700 * is actually right shifted n bits. This means that 18701 * the lower n bits info is wiped out. It will look 18702 * like the window is shrunk. Do a check here to 18703 * see if the shrunk amount is actually within the 18704 * error in window calculation. If it is, just 18705 * return. Note that this check is inside the 18706 * shrunk window check. This makes sure that even 18707 * though tcp_process_shrunk_swnd() is not called, 18708 * we will stop further processing. 18709 */ 18710 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 18711 tcp_process_shrunk_swnd(tcp, -usable_r); 18712 } 18713 return; 18714 } 18715 18716 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 18717 if (tcp->tcp_swnd > tcp->tcp_cwnd) 18718 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 18719 18720 /* usable = MIN(usable, unsent) */ 18721 if (usable_r > len) 18722 usable_r = len; 18723 18724 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 18725 if (usable_r > 0) { 18726 usable = usable_r; 18727 } else { 18728 /* Bypass all other unnecessary processing. */ 18729 goto done; 18730 } 18731 } 18732 18733 local_time = (mblk_t *)lbolt; 18734 18735 /* 18736 * "Our" Nagle Algorithm. This is not the same as in the old 18737 * BSD. This is more in line with the true intent of Nagle. 18738 * 18739 * The conditions are: 18740 * 1. The amount of unsent data (or amount of data which can be 18741 * sent, whichever is smaller) is less than Nagle limit. 18742 * 2. The last sent size is also less than Nagle limit. 18743 * 3. There is unack'ed data. 18744 * 4. Urgent pointer is not set. Send urgent data ignoring the 18745 * Nagle algorithm. This reduces the probability that urgent 18746 * bytes get "merged" together. 18747 * 5. The app has not closed the connection. This eliminates the 18748 * wait time of the receiving side waiting for the last piece of 18749 * (small) data. 18750 * 18751 * If all are satisified, exit without sending anything. Note 18752 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 18753 * the smaller of 1 MSS and global tcp_naglim_def (default to be 18754 * 4095). 18755 */ 18756 if (usable < (int)tcp->tcp_naglim && 18757 tcp->tcp_naglim > tcp->tcp_last_sent_len && 18758 snxt != tcp->tcp_suna && 18759 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 18760 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 18761 goto done; 18762 } 18763 18764 if (tcp->tcp_cork) { 18765 /* 18766 * if the tcp->tcp_cork option is set, then we have to force 18767 * TCP not to send partial segment (smaller than MSS bytes). 18768 * We are calculating the usable now based on full mss and 18769 * will save the rest of remaining data for later. 18770 */ 18771 if (usable < mss) 18772 goto done; 18773 usable = (usable / mss) * mss; 18774 } 18775 18776 /* Update the latest receive window size in TCP header. */ 18777 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18778 tcp->tcp_tcph->th_win); 18779 18780 /* 18781 * Determine if it's worthwhile to attempt MDT, based on: 18782 * 18783 * 1. Simple TCP/IP{v4,v6} (no options). 18784 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 18785 * 3. If the TCP connection is in ESTABLISHED state. 18786 * 4. The TCP is not detached. 18787 * 18788 * If any of the above conditions have changed during the 18789 * connection, stop using MDT and restore the stream head 18790 * parameters accordingly. 18791 */ 18792 if (tcp->tcp_mdt && 18793 ((tcp->tcp_ipversion == IPV4_VERSION && 18794 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 18795 (tcp->tcp_ipversion == IPV6_VERSION && 18796 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 18797 tcp->tcp_state != TCPS_ESTABLISHED || 18798 TCP_IS_DETACHED(tcp) || !CONN_IS_MD_FASTPATH(tcp->tcp_connp) || 18799 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 18800 IPP_ENABLED(IPP_LOCAL_OUT))) { 18801 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 18802 tcp->tcp_mdt = B_FALSE; 18803 18804 /* Anything other than detached is considered pathological */ 18805 if (!TCP_IS_DETACHED(tcp)) { 18806 TCP_STAT(tcp_mdt_conn_halted1); 18807 (void) tcp_maxpsz_set(tcp, B_TRUE); 18808 } 18809 } 18810 18811 /* Use MDT if sendable amount is greater than the threshold */ 18812 if (tcp->tcp_mdt && 18813 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 18814 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 18815 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 18816 (tcp->tcp_valid_bits == 0 || 18817 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 18818 ASSERT(tcp->tcp_connp->conn_mdt_ok); 18819 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18820 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18821 local_time, mdt_thres); 18822 } else { 18823 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18824 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18825 local_time, INT_MAX); 18826 } 18827 18828 /* Pretend that all we were trying to send really got sent */ 18829 if (rc < 0 && tail_unsent < 0) { 18830 do { 18831 xmit_tail = xmit_tail->b_cont; 18832 xmit_tail->b_prev = local_time; 18833 ASSERT((uintptr_t)(xmit_tail->b_wptr - 18834 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 18835 tail_unsent += (int)(xmit_tail->b_wptr - 18836 xmit_tail->b_rptr); 18837 } while (tail_unsent < 0); 18838 } 18839 done:; 18840 tcp->tcp_xmit_tail = xmit_tail; 18841 tcp->tcp_xmit_tail_unsent = tail_unsent; 18842 len = tcp->tcp_snxt - snxt; 18843 if (len) { 18844 /* 18845 * If new data was sent, need to update the notsack 18846 * list, which is, afterall, data blocks that have 18847 * not been sack'ed by the receiver. New data is 18848 * not sack'ed. 18849 */ 18850 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 18851 /* len is a negative value. */ 18852 tcp->tcp_pipe -= len; 18853 tcp_notsack_update(&(tcp->tcp_notsack_list), 18854 tcp->tcp_snxt, snxt, 18855 &(tcp->tcp_num_notsack_blk), 18856 &(tcp->tcp_cnt_notsack_list)); 18857 } 18858 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 18859 tcp->tcp_rack = tcp->tcp_rnxt; 18860 tcp->tcp_rack_cnt = 0; 18861 if ((snxt + len) == tcp->tcp_suna) { 18862 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18863 } 18864 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 18865 /* 18866 * Didn't send anything. Make sure the timer is running 18867 * so that we will probe a zero window. 18868 */ 18869 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18870 } 18871 /* Note that len is the amount we just sent but with a negative sign */ 18872 tcp->tcp_unsent += len; 18873 if (tcp->tcp_flow_stopped) { 18874 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18875 tcp_clrqfull(tcp); 18876 } 18877 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 18878 tcp_setqfull(tcp); 18879 } 18880 } 18881 18882 /* 18883 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 18884 * outgoing TCP header with the template header, as well as other 18885 * options such as time-stamp, ECN and/or SACK. 18886 */ 18887 static void 18888 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 18889 { 18890 tcph_t *tcp_tmpl, *tcp_h; 18891 uint32_t *dst, *src; 18892 int hdrlen; 18893 18894 ASSERT(OK_32PTR(rptr)); 18895 18896 /* Template header */ 18897 tcp_tmpl = tcp->tcp_tcph; 18898 18899 /* Header of outgoing packet */ 18900 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 18901 18902 /* dst and src are opaque 32-bit fields, used for copying */ 18903 dst = (uint32_t *)rptr; 18904 src = (uint32_t *)tcp->tcp_iphc; 18905 hdrlen = tcp->tcp_hdr_len; 18906 18907 /* Fill time-stamp option if needed */ 18908 if (tcp->tcp_snd_ts_ok) { 18909 U32_TO_BE32((uint32_t)now, 18910 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 18911 U32_TO_BE32(tcp->tcp_ts_recent, 18912 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 18913 } else { 18914 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 18915 } 18916 18917 /* 18918 * Copy the template header; is this really more efficient than 18919 * calling bcopy()? For simple IPv4/TCP, it may be the case, 18920 * but perhaps not for other scenarios. 18921 */ 18922 dst[0] = src[0]; 18923 dst[1] = src[1]; 18924 dst[2] = src[2]; 18925 dst[3] = src[3]; 18926 dst[4] = src[4]; 18927 dst[5] = src[5]; 18928 dst[6] = src[6]; 18929 dst[7] = src[7]; 18930 dst[8] = src[8]; 18931 dst[9] = src[9]; 18932 if (hdrlen -= 40) { 18933 hdrlen >>= 2; 18934 dst += 10; 18935 src += 10; 18936 do { 18937 *dst++ = *src++; 18938 } while (--hdrlen); 18939 } 18940 18941 /* 18942 * Set the ECN info in the TCP header if it is not a zero 18943 * window probe. Zero window probe is only sent in 18944 * tcp_wput_data() and tcp_timer(). 18945 */ 18946 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 18947 SET_ECT(tcp, rptr); 18948 18949 if (tcp->tcp_ecn_echo_on) 18950 tcp_h->th_flags[0] |= TH_ECE; 18951 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 18952 tcp_h->th_flags[0] |= TH_CWR; 18953 tcp->tcp_ecn_cwr_sent = B_TRUE; 18954 } 18955 } 18956 18957 /* Fill in SACK options */ 18958 if (num_sack_blk > 0) { 18959 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 18960 sack_blk_t *tmp; 18961 int32_t i; 18962 18963 wptr[0] = TCPOPT_NOP; 18964 wptr[1] = TCPOPT_NOP; 18965 wptr[2] = TCPOPT_SACK; 18966 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 18967 sizeof (sack_blk_t); 18968 wptr += TCPOPT_REAL_SACK_LEN; 18969 18970 tmp = tcp->tcp_sack_list; 18971 for (i = 0; i < num_sack_blk; i++) { 18972 U32_TO_BE32(tmp[i].begin, wptr); 18973 wptr += sizeof (tcp_seq); 18974 U32_TO_BE32(tmp[i].end, wptr); 18975 wptr += sizeof (tcp_seq); 18976 } 18977 tcp_h->th_offset_and_rsrvd[0] += 18978 ((num_sack_blk * 2 + 1) << 4); 18979 } 18980 } 18981 18982 /* 18983 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 18984 * the destination address and SAP attribute, and if necessary, the 18985 * hardware checksum offload attribute to a Multidata message. 18986 */ 18987 static int 18988 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 18989 const uint32_t start, const uint32_t stuff, const uint32_t end, 18990 const uint32_t flags) 18991 { 18992 /* Add global destination address & SAP attribute */ 18993 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 18994 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 18995 "destination address+SAP\n")); 18996 18997 if (dlmp != NULL) 18998 TCP_STAT(tcp_mdt_allocfail); 18999 return (-1); 19000 } 19001 19002 /* Add global hwcksum attribute */ 19003 if (hwcksum && 19004 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19005 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19006 "checksum attribute\n")); 19007 19008 TCP_STAT(tcp_mdt_allocfail); 19009 return (-1); 19010 } 19011 19012 return (0); 19013 } 19014 19015 /* 19016 * Smaller and private version of pdescinfo_t used specifically for TCP, 19017 * which allows for only two payload spans per packet. 19018 */ 19019 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19020 19021 /* 19022 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19023 * scheme, and returns one the following: 19024 * 19025 * -1 = failed allocation. 19026 * 0 = success; burst count reached, or usable send window is too small, 19027 * and that we'd rather wait until later before sending again. 19028 */ 19029 static int 19030 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19031 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19032 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19033 const int mdt_thres) 19034 { 19035 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19036 multidata_t *mmd; 19037 uint_t obsegs, obbytes, hdr_frag_sz; 19038 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19039 int num_burst_seg, max_pld; 19040 pdesc_t *pkt; 19041 tcp_pdescinfo_t tcp_pkt_info; 19042 pdescinfo_t *pkt_info; 19043 int pbuf_idx, pbuf_idx_nxt; 19044 int seg_len, len, spill, af; 19045 boolean_t add_buffer, zcopy, clusterwide; 19046 boolean_t rconfirm = B_FALSE; 19047 boolean_t done = B_FALSE; 19048 uint32_t cksum; 19049 uint32_t hwcksum_flags; 19050 ire_t *ire; 19051 ill_t *ill; 19052 ipha_t *ipha; 19053 ip6_t *ip6h; 19054 ipaddr_t src, dst; 19055 ill_zerocopy_capab_t *zc_cap = NULL; 19056 uint16_t *up; 19057 int err; 19058 conn_t *connp; 19059 19060 #ifdef _BIG_ENDIAN 19061 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19062 #else 19063 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19064 #endif 19065 19066 #define PREP_NEW_MULTIDATA() { \ 19067 mmd = NULL; \ 19068 md_mp = md_hbuf = NULL; \ 19069 cur_hdr_off = 0; \ 19070 max_pld = tcp->tcp_mdt_max_pld; \ 19071 pbuf_idx = pbuf_idx_nxt = -1; \ 19072 add_buffer = B_TRUE; \ 19073 zcopy = B_FALSE; \ 19074 } 19075 19076 #define PREP_NEW_PBUF() { \ 19077 md_pbuf = md_pbuf_nxt = NULL; \ 19078 pbuf_idx = pbuf_idx_nxt = -1; \ 19079 cur_pld_off = 0; \ 19080 first_snxt = *snxt; \ 19081 ASSERT(*tail_unsent > 0); \ 19082 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19083 } 19084 19085 ASSERT(mdt_thres >= mss); 19086 ASSERT(*usable > 0 && *usable > mdt_thres); 19087 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19088 ASSERT(!TCP_IS_DETACHED(tcp)); 19089 ASSERT(tcp->tcp_valid_bits == 0 || 19090 tcp->tcp_valid_bits == TCP_FSS_VALID); 19091 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19092 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19093 (tcp->tcp_ipversion == IPV6_VERSION && 19094 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19095 19096 connp = tcp->tcp_connp; 19097 ASSERT(connp != NULL); 19098 ASSERT(CONN_IS_MD_FASTPATH(connp)); 19099 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19100 19101 /* 19102 * Note that tcp will only declare at most 2 payload spans per 19103 * packet, which is much lower than the maximum allowable number 19104 * of packet spans per Multidata. For this reason, we use the 19105 * privately declared and smaller descriptor info structure, in 19106 * order to save some stack space. 19107 */ 19108 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19109 19110 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19111 if (af == AF_INET) { 19112 dst = tcp->tcp_ipha->ipha_dst; 19113 src = tcp->tcp_ipha->ipha_src; 19114 ASSERT(!CLASSD(dst)); 19115 } 19116 ASSERT(af == AF_INET || 19117 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19118 19119 obsegs = obbytes = 0; 19120 num_burst_seg = tcp->tcp_snd_burst; 19121 md_mp_head = NULL; 19122 PREP_NEW_MULTIDATA(); 19123 19124 /* 19125 * Before we go on further, make sure there is an IRE that we can 19126 * use, and that the ILL supports MDT. Otherwise, there's no point 19127 * in proceeding any further, and we should just hand everything 19128 * off to the legacy path. 19129 */ 19130 mutex_enter(&connp->conn_lock); 19131 ire = connp->conn_ire_cache; 19132 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19133 if (ire != NULL && ((af == AF_INET && ire->ire_addr == dst) || 19134 (af == AF_INET6 && IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, 19135 &tcp->tcp_ip6h->ip6_dst))) && 19136 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19137 IRE_REFHOLD(ire); 19138 mutex_exit(&connp->conn_lock); 19139 } else { 19140 boolean_t cached = B_FALSE; 19141 ts_label_t *tsl; 19142 19143 /* force a recheck later on */ 19144 tcp->tcp_ire_ill_check_done = B_FALSE; 19145 19146 TCP_DBGSTAT(tcp_ire_null1); 19147 connp->conn_ire_cache = NULL; 19148 mutex_exit(&connp->conn_lock); 19149 19150 /* Release the old ire */ 19151 if (ire != NULL) 19152 IRE_REFRELE_NOTR(ire); 19153 19154 tsl = crgetlabel(CONN_CRED(connp)); 19155 ire = (af == AF_INET) ? 19156 ire_cache_lookup(dst, connp->conn_zoneid, tsl) : 19157 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19158 connp->conn_zoneid, tsl); 19159 19160 if (ire == NULL) { 19161 TCP_STAT(tcp_ire_null); 19162 goto legacy_send_no_md; 19163 } 19164 19165 IRE_REFHOLD_NOTR(ire); 19166 /* 19167 * Since we are inside the squeue, there cannot be another 19168 * thread in TCP trying to set the conn_ire_cache now. The 19169 * check for IRE_MARK_CONDEMNED ensures that an interface 19170 * unplumb thread has not yet started cleaning up the conns. 19171 * Hence we don't need to grab the conn lock. 19172 */ 19173 if (!(connp->conn_state_flags & CONN_CLOSING)) { 19174 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19175 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19176 connp->conn_ire_cache = ire; 19177 cached = B_TRUE; 19178 } 19179 rw_exit(&ire->ire_bucket->irb_lock); 19180 } 19181 19182 /* 19183 * We can continue to use the ire but since it was not 19184 * cached, we should drop the extra reference. 19185 */ 19186 if (!cached) 19187 IRE_REFRELE_NOTR(ire); 19188 } 19189 19190 ASSERT(ire != NULL); 19191 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19192 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19193 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19194 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19195 /* 19196 * If we do support loopback for MDT (which requires modifications 19197 * to the receiving paths), the following assertions should go away, 19198 * and we would be sending the Multidata to loopback conn later on. 19199 */ 19200 ASSERT(!IRE_IS_LOCAL(ire)); 19201 ASSERT(ire->ire_stq != NULL); 19202 19203 ill = ire_to_ill(ire); 19204 ASSERT(ill != NULL); 19205 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19206 19207 if (!tcp->tcp_ire_ill_check_done) { 19208 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19209 tcp->tcp_ire_ill_check_done = B_TRUE; 19210 } 19211 19212 /* 19213 * If the underlying interface conditions have changed, or if the 19214 * new interface does not support MDT, go back to legacy path. 19215 */ 19216 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19217 /* don't go through this path anymore for this connection */ 19218 TCP_STAT(tcp_mdt_conn_halted2); 19219 tcp->tcp_mdt = B_FALSE; 19220 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19221 "interface %s\n", (void *)connp, ill->ill_name)); 19222 /* IRE will be released prior to returning */ 19223 goto legacy_send_no_md; 19224 } 19225 19226 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19227 zc_cap = ill->ill_zerocopy_capab; 19228 19229 /* go to legacy path if interface doesn't support zerocopy */ 19230 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19231 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19232 /* IRE will be released prior to returning */ 19233 goto legacy_send_no_md; 19234 } 19235 19236 /* does the interface support hardware checksum offload? */ 19237 hwcksum_flags = 0; 19238 if (ILL_HCKSUM_CAPABLE(ill) && 19239 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19240 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19241 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19242 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19243 HCKSUM_IPHDRCKSUM) 19244 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19245 19246 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19247 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19248 hwcksum_flags |= HCK_FULLCKSUM; 19249 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19250 HCKSUM_INET_PARTIAL) 19251 hwcksum_flags |= HCK_PARTIALCKSUM; 19252 } 19253 19254 /* 19255 * Each header fragment consists of the leading extra space, 19256 * followed by the TCP/IP header, and the trailing extra space. 19257 * We make sure that each header fragment begins on a 32-bit 19258 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19259 * aligned in tcp_mdt_update). 19260 */ 19261 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19262 tcp->tcp_mdt_hdr_tail), 4); 19263 19264 /* are we starting from the beginning of data block? */ 19265 if (*tail_unsent == 0) { 19266 *xmit_tail = (*xmit_tail)->b_cont; 19267 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19268 *tail_unsent = (int)MBLKL(*xmit_tail); 19269 } 19270 19271 /* 19272 * Here we create one or more Multidata messages, each made up of 19273 * one header buffer and up to N payload buffers. This entire 19274 * operation is done within two loops: 19275 * 19276 * The outer loop mostly deals with creating the Multidata message, 19277 * as well as the header buffer that gets added to it. It also 19278 * links the Multidata messages together such that all of them can 19279 * be sent down to the lower layer in a single putnext call; this 19280 * linking behavior depends on the tcp_mdt_chain tunable. 19281 * 19282 * The inner loop takes an existing Multidata message, and adds 19283 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19284 * packetizes those buffers by filling up the corresponding header 19285 * buffer fragments with the proper IP and TCP headers, and by 19286 * describing the layout of each packet in the packet descriptors 19287 * that get added to the Multidata. 19288 */ 19289 do { 19290 /* 19291 * If usable send window is too small, or data blocks in 19292 * transmit list are smaller than our threshold (i.e. app 19293 * performs large writes followed by small ones), we hand 19294 * off the control over to the legacy path. Note that we'll 19295 * get back the control once it encounters a large block. 19296 */ 19297 if (*usable < mss || (*tail_unsent <= mdt_thres && 19298 (*xmit_tail)->b_cont != NULL && 19299 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19300 /* send down what we've got so far */ 19301 if (md_mp_head != NULL) { 19302 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19303 obsegs, obbytes, &rconfirm); 19304 } 19305 /* 19306 * Pass control over to tcp_send(), but tell it to 19307 * return to us once a large-size transmission is 19308 * possible. 19309 */ 19310 TCP_STAT(tcp_mdt_legacy_small); 19311 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19312 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19313 tail_unsent, xmit_tail, local_time, 19314 mdt_thres)) <= 0) { 19315 /* burst count reached, or alloc failed */ 19316 IRE_REFRELE(ire); 19317 return (err); 19318 } 19319 19320 /* tcp_send() may have sent everything, so check */ 19321 if (*usable <= 0) { 19322 IRE_REFRELE(ire); 19323 return (0); 19324 } 19325 19326 TCP_STAT(tcp_mdt_legacy_ret); 19327 /* 19328 * We may have delivered the Multidata, so make sure 19329 * to re-initialize before the next round. 19330 */ 19331 md_mp_head = NULL; 19332 obsegs = obbytes = 0; 19333 num_burst_seg = tcp->tcp_snd_burst; 19334 PREP_NEW_MULTIDATA(); 19335 19336 /* are we starting from the beginning of data block? */ 19337 if (*tail_unsent == 0) { 19338 *xmit_tail = (*xmit_tail)->b_cont; 19339 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19340 (uintptr_t)INT_MAX); 19341 *tail_unsent = (int)MBLKL(*xmit_tail); 19342 } 19343 } 19344 19345 /* 19346 * max_pld limits the number of mblks in tcp's transmit 19347 * queue that can be added to a Multidata message. Once 19348 * this counter reaches zero, no more additional mblks 19349 * can be added to it. What happens afterwards depends 19350 * on whether or not we are set to chain the Multidata 19351 * messages. If we are to link them together, reset 19352 * max_pld to its original value (tcp_mdt_max_pld) and 19353 * prepare to create a new Multidata message which will 19354 * get linked to md_mp_head. Else, leave it alone and 19355 * let the inner loop break on its own. 19356 */ 19357 if (tcp_mdt_chain && max_pld == 0) 19358 PREP_NEW_MULTIDATA(); 19359 19360 /* adding a payload buffer; re-initialize values */ 19361 if (add_buffer) 19362 PREP_NEW_PBUF(); 19363 19364 /* 19365 * If we don't have a Multidata, either because we just 19366 * (re)entered this outer loop, or after we branched off 19367 * to tcp_send above, setup the Multidata and header 19368 * buffer to be used. 19369 */ 19370 if (md_mp == NULL) { 19371 int md_hbuflen; 19372 uint32_t start, stuff; 19373 19374 /* 19375 * Calculate Multidata header buffer size large enough 19376 * to hold all of the headers that can possibly be 19377 * sent at this moment. We'd rather over-estimate 19378 * the size than running out of space; this is okay 19379 * since this buffer is small anyway. 19380 */ 19381 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19382 19383 /* 19384 * Start and stuff offset for partial hardware 19385 * checksum offload; these are currently for IPv4. 19386 * For full checksum offload, they are set to zero. 19387 */ 19388 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19389 if (af == AF_INET) { 19390 start = IP_SIMPLE_HDR_LENGTH; 19391 stuff = IP_SIMPLE_HDR_LENGTH + 19392 TCP_CHECKSUM_OFFSET; 19393 } else { 19394 start = IPV6_HDR_LEN; 19395 stuff = IPV6_HDR_LEN + 19396 TCP_CHECKSUM_OFFSET; 19397 } 19398 } else { 19399 start = stuff = 0; 19400 } 19401 19402 /* 19403 * Create the header buffer, Multidata, as well as 19404 * any necessary attributes (destination address, 19405 * SAP and hardware checksum offload) that should 19406 * be associated with the Multidata message. 19407 */ 19408 ASSERT(cur_hdr_off == 0); 19409 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19410 ((md_hbuf->b_wptr += md_hbuflen), 19411 (mmd = mmd_alloc(md_hbuf, &md_mp, 19412 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19413 /* fastpath mblk */ 19414 (af == AF_INET) ? ire->ire_dlureq_mp : 19415 ire->ire_nce->nce_res_mp, 19416 /* hardware checksum enabled */ 19417 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19418 /* hardware checksum offsets */ 19419 start, stuff, 0, 19420 /* hardware checksum flag */ 19421 hwcksum_flags) != 0)) { 19422 legacy_send: 19423 if (md_mp != NULL) { 19424 /* Unlink message from the chain */ 19425 if (md_mp_head != NULL) { 19426 err = (intptr_t)rmvb(md_mp_head, 19427 md_mp); 19428 /* 19429 * We can't assert that rmvb 19430 * did not return -1, since we 19431 * may get here before linkb 19432 * happens. We do, however, 19433 * check if we just removed the 19434 * only element in the list. 19435 */ 19436 if (err == 0) 19437 md_mp_head = NULL; 19438 } 19439 /* md_hbuf gets freed automatically */ 19440 TCP_STAT(tcp_mdt_discarded); 19441 freeb(md_mp); 19442 } else { 19443 /* Either allocb or mmd_alloc failed */ 19444 TCP_STAT(tcp_mdt_allocfail); 19445 if (md_hbuf != NULL) 19446 freeb(md_hbuf); 19447 } 19448 19449 /* send down what we've got so far */ 19450 if (md_mp_head != NULL) { 19451 tcp_multisend_data(tcp, ire, ill, 19452 md_mp_head, obsegs, obbytes, 19453 &rconfirm); 19454 } 19455 legacy_send_no_md: 19456 if (ire != NULL) 19457 IRE_REFRELE(ire); 19458 /* 19459 * Too bad; let the legacy path handle this. 19460 * We specify INT_MAX for the threshold, since 19461 * we gave up with the Multidata processings 19462 * and let the old path have it all. 19463 */ 19464 TCP_STAT(tcp_mdt_legacy_all); 19465 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19466 tcp_tcp_hdr_len, num_sack_blk, usable, 19467 snxt, tail_unsent, xmit_tail, local_time, 19468 INT_MAX)); 19469 } 19470 19471 /* link to any existing ones, if applicable */ 19472 TCP_STAT(tcp_mdt_allocd); 19473 if (md_mp_head == NULL) { 19474 md_mp_head = md_mp; 19475 } else if (tcp_mdt_chain) { 19476 TCP_STAT(tcp_mdt_linked); 19477 linkb(md_mp_head, md_mp); 19478 } 19479 } 19480 19481 ASSERT(md_mp_head != NULL); 19482 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19483 ASSERT(md_mp != NULL && mmd != NULL); 19484 ASSERT(md_hbuf != NULL); 19485 19486 /* 19487 * Packetize the transmittable portion of the data block; 19488 * each data block is essentially added to the Multidata 19489 * as a payload buffer. We also deal with adding more 19490 * than one payload buffers, which happens when the remaining 19491 * packetized portion of the current payload buffer is less 19492 * than MSS, while the next data block in transmit queue 19493 * has enough data to make up for one. This "spillover" 19494 * case essentially creates a split-packet, where portions 19495 * of the packet's payload fragments may span across two 19496 * virtually discontiguous address blocks. 19497 */ 19498 seg_len = mss; 19499 do { 19500 len = seg_len; 19501 19502 ASSERT(len > 0); 19503 ASSERT(max_pld >= 0); 19504 ASSERT(!add_buffer || cur_pld_off == 0); 19505 19506 /* 19507 * First time around for this payload buffer; note 19508 * in the case of a spillover, the following has 19509 * been done prior to adding the split-packet 19510 * descriptor to Multidata, and we don't want to 19511 * repeat the process. 19512 */ 19513 if (add_buffer) { 19514 ASSERT(mmd != NULL); 19515 ASSERT(md_pbuf == NULL); 19516 ASSERT(md_pbuf_nxt == NULL); 19517 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19518 19519 /* 19520 * Have we reached the limit? We'd get to 19521 * this case when we're not chaining the 19522 * Multidata messages together, and since 19523 * we're done, terminate this loop. 19524 */ 19525 if (max_pld == 0) 19526 break; /* done */ 19527 19528 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19529 TCP_STAT(tcp_mdt_allocfail); 19530 goto legacy_send; /* out_of_mem */ 19531 } 19532 19533 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19534 zc_cap != NULL) { 19535 if (!ip_md_zcopy_attr(mmd, NULL, 19536 zc_cap->ill_zerocopy_flags)) { 19537 freeb(md_pbuf); 19538 TCP_STAT(tcp_mdt_allocfail); 19539 /* out_of_mem */ 19540 goto legacy_send; 19541 } 19542 zcopy = B_TRUE; 19543 } 19544 19545 md_pbuf->b_rptr += base_pld_off; 19546 19547 /* 19548 * Add a payload buffer to the Multidata; this 19549 * operation must not fail, or otherwise our 19550 * logic in this routine is broken. There 19551 * is no memory allocation done by the 19552 * routine, so any returned failure simply 19553 * tells us that we've done something wrong. 19554 * 19555 * A failure tells us that either we're adding 19556 * the same payload buffer more than once, or 19557 * we're trying to add more buffers than 19558 * allowed (max_pld calculation is wrong). 19559 * None of the above cases should happen, and 19560 * we panic because either there's horrible 19561 * heap corruption, and/or programming mistake. 19562 */ 19563 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19564 if (pbuf_idx < 0) { 19565 cmn_err(CE_PANIC, "tcp_multisend: " 19566 "payload buffer logic error " 19567 "detected for tcp %p mmd %p " 19568 "pbuf %p (%d)\n", 19569 (void *)tcp, (void *)mmd, 19570 (void *)md_pbuf, pbuf_idx); 19571 } 19572 19573 ASSERT(max_pld > 0); 19574 --max_pld; 19575 add_buffer = B_FALSE; 19576 } 19577 19578 ASSERT(md_mp_head != NULL); 19579 ASSERT(md_pbuf != NULL); 19580 ASSERT(md_pbuf_nxt == NULL); 19581 ASSERT(pbuf_idx != -1); 19582 ASSERT(pbuf_idx_nxt == -1); 19583 ASSERT(*usable > 0); 19584 19585 /* 19586 * We spillover to the next payload buffer only 19587 * if all of the following is true: 19588 * 19589 * 1. There is not enough data on the current 19590 * payload buffer to make up `len', 19591 * 2. We are allowed to send `len', 19592 * 3. The next payload buffer length is large 19593 * enough to accomodate `spill'. 19594 */ 19595 if ((spill = len - *tail_unsent) > 0 && 19596 *usable >= len && 19597 MBLKL((*xmit_tail)->b_cont) >= spill && 19598 max_pld > 0) { 19599 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19600 if (md_pbuf_nxt == NULL) { 19601 TCP_STAT(tcp_mdt_allocfail); 19602 goto legacy_send; /* out_of_mem */ 19603 } 19604 19605 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19606 zc_cap != NULL) { 19607 if (!ip_md_zcopy_attr(mmd, NULL, 19608 zc_cap->ill_zerocopy_flags)) { 19609 freeb(md_pbuf_nxt); 19610 TCP_STAT(tcp_mdt_allocfail); 19611 /* out_of_mem */ 19612 goto legacy_send; 19613 } 19614 zcopy = B_TRUE; 19615 } 19616 19617 /* 19618 * See comments above on the first call to 19619 * mmd_addpldbuf for explanation on the panic. 19620 */ 19621 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19622 if (pbuf_idx_nxt < 0) { 19623 panic("tcp_multisend: " 19624 "next payload buffer logic error " 19625 "detected for tcp %p mmd %p " 19626 "pbuf %p (%d)\n", 19627 (void *)tcp, (void *)mmd, 19628 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19629 } 19630 19631 ASSERT(max_pld > 0); 19632 --max_pld; 19633 } else if (spill > 0) { 19634 /* 19635 * If there's a spillover, but the following 19636 * xmit_tail couldn't give us enough octets 19637 * to reach "len", then stop the current 19638 * Multidata creation and let the legacy 19639 * tcp_send() path take over. We don't want 19640 * to send the tiny segment as part of this 19641 * Multidata for performance reasons; instead, 19642 * we let the legacy path deal with grouping 19643 * it with the subsequent small mblks. 19644 */ 19645 if (*usable >= len && 19646 MBLKL((*xmit_tail)->b_cont) < spill) { 19647 max_pld = 0; 19648 break; /* done */ 19649 } 19650 19651 /* 19652 * We can't spillover, and we are near 19653 * the end of the current payload buffer, 19654 * so send what's left. 19655 */ 19656 ASSERT(*tail_unsent > 0); 19657 len = *tail_unsent; 19658 } 19659 19660 /* tail_unsent is negated if there is a spillover */ 19661 *tail_unsent -= len; 19662 *usable -= len; 19663 ASSERT(*usable >= 0); 19664 19665 if (*usable < mss) 19666 seg_len = *usable; 19667 /* 19668 * Sender SWS avoidance; see comments in tcp_send(); 19669 * everything else is the same, except that we only 19670 * do this here if there is no more data to be sent 19671 * following the current xmit_tail. We don't check 19672 * for 1-byte urgent data because we shouldn't get 19673 * here if TCP_URG_VALID is set. 19674 */ 19675 if (*usable > 0 && *usable < mss && 19676 ((md_pbuf_nxt == NULL && 19677 (*xmit_tail)->b_cont == NULL) || 19678 (md_pbuf_nxt != NULL && 19679 (*xmit_tail)->b_cont->b_cont == NULL)) && 19680 seg_len < (tcp->tcp_max_swnd >> 1) && 19681 (tcp->tcp_unsent - 19682 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 19683 !tcp->tcp_zero_win_probe) { 19684 if ((*snxt + len) == tcp->tcp_snxt && 19685 (*snxt + len) == tcp->tcp_suna) { 19686 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19687 } 19688 done = B_TRUE; 19689 } 19690 19691 /* 19692 * Prime pump for IP's checksumming on our behalf; 19693 * include the adjustment for a source route if any. 19694 * Do this only for software/partial hardware checksum 19695 * offload, as this field gets zeroed out later for 19696 * the full hardware checksum offload case. 19697 */ 19698 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 19699 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19700 cksum = (cksum >> 16) + (cksum & 0xFFFF); 19701 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 19702 } 19703 19704 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 19705 *snxt += len; 19706 19707 tcp->tcp_tcph->th_flags[0] = TH_ACK; 19708 /* 19709 * We set the PUSH bit only if TCP has no more buffered 19710 * data to be transmitted (or if sender SWS avoidance 19711 * takes place), as opposed to setting it for every 19712 * last packet in the burst. 19713 */ 19714 if (done || 19715 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 19716 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 19717 19718 /* 19719 * Set FIN bit if this is our last segment; snxt 19720 * already includes its length, and it will not 19721 * be adjusted after this point. 19722 */ 19723 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 19724 *snxt == tcp->tcp_fss) { 19725 if (!tcp->tcp_fin_acked) { 19726 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 19727 BUMP_MIB(&tcp_mib, tcpOutControl); 19728 } 19729 if (!tcp->tcp_fin_sent) { 19730 tcp->tcp_fin_sent = B_TRUE; 19731 /* 19732 * tcp state must be ESTABLISHED 19733 * in order for us to get here in 19734 * the first place. 19735 */ 19736 tcp->tcp_state = TCPS_FIN_WAIT_1; 19737 19738 /* 19739 * Upon returning from this routine, 19740 * tcp_wput_data() will set tcp_snxt 19741 * to be equal to snxt + tcp_fin_sent. 19742 * This is essentially the same as 19743 * setting it to tcp_fss + 1. 19744 */ 19745 } 19746 } 19747 19748 tcp->tcp_last_sent_len = (ushort_t)len; 19749 19750 len += tcp_hdr_len; 19751 if (tcp->tcp_ipversion == IPV4_VERSION) 19752 tcp->tcp_ipha->ipha_length = htons(len); 19753 else 19754 tcp->tcp_ip6h->ip6_plen = htons(len - 19755 ((char *)&tcp->tcp_ip6h[1] - 19756 tcp->tcp_iphc)); 19757 19758 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 19759 19760 /* setup header fragment */ 19761 PDESC_HDR_ADD(pkt_info, 19762 md_hbuf->b_rptr + cur_hdr_off, /* base */ 19763 tcp->tcp_mdt_hdr_head, /* head room */ 19764 tcp_hdr_len, /* len */ 19765 tcp->tcp_mdt_hdr_tail); /* tail room */ 19766 19767 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 19768 hdr_frag_sz); 19769 ASSERT(MBLKIN(md_hbuf, 19770 (pkt_info->hdr_base - md_hbuf->b_rptr), 19771 PDESC_HDRSIZE(pkt_info))); 19772 19773 /* setup first payload fragment */ 19774 PDESC_PLD_INIT(pkt_info); 19775 PDESC_PLD_SPAN_ADD(pkt_info, 19776 pbuf_idx, /* index */ 19777 md_pbuf->b_rptr + cur_pld_off, /* start */ 19778 tcp->tcp_last_sent_len); /* len */ 19779 19780 /* create a split-packet in case of a spillover */ 19781 if (md_pbuf_nxt != NULL) { 19782 ASSERT(spill > 0); 19783 ASSERT(pbuf_idx_nxt > pbuf_idx); 19784 ASSERT(!add_buffer); 19785 19786 md_pbuf = md_pbuf_nxt; 19787 md_pbuf_nxt = NULL; 19788 pbuf_idx = pbuf_idx_nxt; 19789 pbuf_idx_nxt = -1; 19790 cur_pld_off = spill; 19791 19792 /* trim out first payload fragment */ 19793 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 19794 19795 /* setup second payload fragment */ 19796 PDESC_PLD_SPAN_ADD(pkt_info, 19797 pbuf_idx, /* index */ 19798 md_pbuf->b_rptr, /* start */ 19799 spill); /* len */ 19800 19801 if ((*xmit_tail)->b_next == NULL) { 19802 /* 19803 * Store the lbolt used for RTT 19804 * estimation. We can only record one 19805 * timestamp per mblk so we do it when 19806 * we reach the end of the payload 19807 * buffer. Also we only take a new 19808 * timestamp sample when the previous 19809 * timed data from the same mblk has 19810 * been ack'ed. 19811 */ 19812 (*xmit_tail)->b_prev = local_time; 19813 (*xmit_tail)->b_next = 19814 (mblk_t *)(uintptr_t)first_snxt; 19815 } 19816 19817 first_snxt = *snxt - spill; 19818 19819 /* 19820 * Advance xmit_tail; usable could be 0 by 19821 * the time we got here, but we made sure 19822 * above that we would only spillover to 19823 * the next data block if usable includes 19824 * the spilled-over amount prior to the 19825 * subtraction. Therefore, we are sure 19826 * that xmit_tail->b_cont can't be NULL. 19827 */ 19828 ASSERT((*xmit_tail)->b_cont != NULL); 19829 *xmit_tail = (*xmit_tail)->b_cont; 19830 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19831 (uintptr_t)INT_MAX); 19832 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 19833 } else { 19834 cur_pld_off += tcp->tcp_last_sent_len; 19835 } 19836 19837 /* 19838 * Fill in the header using the template header, and 19839 * add options such as time-stamp, ECN and/or SACK, 19840 * as needed. 19841 */ 19842 tcp_fill_header(tcp, pkt_info->hdr_rptr, 19843 (clock_t)local_time, num_sack_blk); 19844 19845 /* take care of some IP header businesses */ 19846 if (af == AF_INET) { 19847 ipha = (ipha_t *)pkt_info->hdr_rptr; 19848 19849 ASSERT(OK_32PTR((uchar_t *)ipha)); 19850 ASSERT(PDESC_HDRL(pkt_info) >= 19851 IP_SIMPLE_HDR_LENGTH); 19852 ASSERT(ipha->ipha_version_and_hdr_length == 19853 IP_SIMPLE_HDR_VERSION); 19854 19855 /* 19856 * Assign ident value for current packet; see 19857 * related comments in ip_wput_ire() about the 19858 * contract private interface with clustering 19859 * group. 19860 */ 19861 clusterwide = B_FALSE; 19862 if (cl_inet_ipident != NULL) { 19863 ASSERT(cl_inet_isclusterwide != NULL); 19864 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 19865 AF_INET, 19866 (uint8_t *)(uintptr_t)src)) { 19867 ipha->ipha_ident = 19868 (*cl_inet_ipident) 19869 (IPPROTO_IP, AF_INET, 19870 (uint8_t *)(uintptr_t)src, 19871 (uint8_t *)(uintptr_t)dst); 19872 clusterwide = B_TRUE; 19873 } 19874 } 19875 19876 if (!clusterwide) { 19877 ipha->ipha_ident = (uint16_t) 19878 atomic_add_32_nv( 19879 &ire->ire_ident, 1); 19880 } 19881 #ifndef _BIG_ENDIAN 19882 ipha->ipha_ident = (ipha->ipha_ident << 8) | 19883 (ipha->ipha_ident >> 8); 19884 #endif 19885 } else { 19886 ip6h = (ip6_t *)pkt_info->hdr_rptr; 19887 19888 ASSERT(OK_32PTR((uchar_t *)ip6h)); 19889 ASSERT(IPVER(ip6h) == IPV6_VERSION); 19890 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 19891 ASSERT(PDESC_HDRL(pkt_info) >= 19892 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 19893 TCP_CHECKSUM_SIZE)); 19894 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 19895 19896 if (tcp->tcp_ip_forward_progress) { 19897 rconfirm = B_TRUE; 19898 tcp->tcp_ip_forward_progress = B_FALSE; 19899 } 19900 } 19901 19902 /* at least one payload span, and at most two */ 19903 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 19904 19905 /* add the packet descriptor to Multidata */ 19906 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 19907 KM_NOSLEEP)) == NULL) { 19908 /* 19909 * Any failure other than ENOMEM indicates 19910 * that we have passed in invalid pkt_info 19911 * or parameters to mmd_addpdesc, which must 19912 * not happen. 19913 * 19914 * EINVAL is a result of failure on boundary 19915 * checks against the pkt_info contents. It 19916 * should not happen, and we panic because 19917 * either there's horrible heap corruption, 19918 * and/or programming mistake. 19919 */ 19920 if (err != ENOMEM) { 19921 cmn_err(CE_PANIC, "tcp_multisend: " 19922 "pdesc logic error detected for " 19923 "tcp %p mmd %p pinfo %p (%d)\n", 19924 (void *)tcp, (void *)mmd, 19925 (void *)pkt_info, err); 19926 } 19927 TCP_STAT(tcp_mdt_addpdescfail); 19928 goto legacy_send; /* out_of_mem */ 19929 } 19930 ASSERT(pkt != NULL); 19931 19932 /* calculate IP header and TCP checksums */ 19933 if (af == AF_INET) { 19934 /* calculate pseudo-header checksum */ 19935 cksum = (dst >> 16) + (dst & 0xFFFF) + 19936 (src >> 16) + (src & 0xFFFF); 19937 19938 /* offset for TCP header checksum */ 19939 up = IPH_TCPH_CHECKSUMP(ipha, 19940 IP_SIMPLE_HDR_LENGTH); 19941 } else { 19942 up = (uint16_t *)&ip6h->ip6_src; 19943 19944 /* calculate pseudo-header checksum */ 19945 cksum = up[0] + up[1] + up[2] + up[3] + 19946 up[4] + up[5] + up[6] + up[7] + 19947 up[8] + up[9] + up[10] + up[11] + 19948 up[12] + up[13] + up[14] + up[15]; 19949 19950 /* Fold the initial sum */ 19951 cksum = (cksum & 0xffff) + (cksum >> 16); 19952 19953 up = (uint16_t *)(((uchar_t *)ip6h) + 19954 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 19955 } 19956 19957 if (hwcksum_flags & HCK_FULLCKSUM) { 19958 /* clear checksum field for hardware */ 19959 *up = 0; 19960 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 19961 uint32_t sum; 19962 19963 /* pseudo-header checksumming */ 19964 sum = *up + cksum + IP_TCP_CSUM_COMP; 19965 sum = (sum & 0xFFFF) + (sum >> 16); 19966 *up = (sum & 0xFFFF) + (sum >> 16); 19967 } else { 19968 /* software checksumming */ 19969 TCP_STAT(tcp_out_sw_cksum); 19970 TCP_STAT_UPDATE(tcp_out_sw_cksum_bytes, 19971 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 19972 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 19973 cksum + IP_TCP_CSUM_COMP); 19974 if (*up == 0) 19975 *up = 0xFFFF; 19976 } 19977 19978 /* IPv4 header checksum */ 19979 if (af == AF_INET) { 19980 ipha->ipha_fragment_offset_and_flags |= 19981 (uint32_t)htons(ire->ire_frag_flag); 19982 19983 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 19984 ipha->ipha_hdr_checksum = 0; 19985 } else { 19986 IP_HDR_CKSUM(ipha, cksum, 19987 ((uint32_t *)ipha)[0], 19988 ((uint16_t *)ipha)[4]); 19989 } 19990 } 19991 19992 /* advance header offset */ 19993 cur_hdr_off += hdr_frag_sz; 19994 19995 obbytes += tcp->tcp_last_sent_len; 19996 ++obsegs; 19997 } while (!done && *usable > 0 && --num_burst_seg > 0 && 19998 *tail_unsent > 0); 19999 20000 if ((*xmit_tail)->b_next == NULL) { 20001 /* 20002 * Store the lbolt used for RTT estimation. We can only 20003 * record one timestamp per mblk so we do it when we 20004 * reach the end of the payload buffer. Also we only 20005 * take a new timestamp sample when the previous timed 20006 * data from the same mblk has been ack'ed. 20007 */ 20008 (*xmit_tail)->b_prev = local_time; 20009 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20010 } 20011 20012 ASSERT(*tail_unsent >= 0); 20013 if (*tail_unsent > 0) { 20014 /* 20015 * We got here because we broke out of the above 20016 * loop due to of one of the following cases: 20017 * 20018 * 1. len < adjusted MSS (i.e. small), 20019 * 2. Sender SWS avoidance, 20020 * 3. max_pld is zero. 20021 * 20022 * We are done for this Multidata, so trim our 20023 * last payload buffer (if any) accordingly. 20024 */ 20025 if (md_pbuf != NULL) 20026 md_pbuf->b_wptr -= *tail_unsent; 20027 } else if (*usable > 0) { 20028 *xmit_tail = (*xmit_tail)->b_cont; 20029 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20030 (uintptr_t)INT_MAX); 20031 *tail_unsent = (int)MBLKL(*xmit_tail); 20032 add_buffer = B_TRUE; 20033 } 20034 } while (!done && *usable > 0 && num_burst_seg > 0 && 20035 (tcp_mdt_chain || max_pld > 0)); 20036 20037 /* send everything down */ 20038 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20039 &rconfirm); 20040 20041 #undef PREP_NEW_MULTIDATA 20042 #undef PREP_NEW_PBUF 20043 #undef IPVER 20044 20045 IRE_REFRELE(ire); 20046 return (0); 20047 } 20048 20049 /* 20050 * A wrapper function for sending one or more Multidata messages down to 20051 * the module below ip; this routine does not release the reference of the 20052 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20053 */ 20054 static void 20055 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20056 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20057 { 20058 uint64_t delta; 20059 nce_t *nce; 20060 20061 ASSERT(ire != NULL && ill != NULL); 20062 ASSERT(ire->ire_stq != NULL); 20063 ASSERT(md_mp_head != NULL); 20064 ASSERT(rconfirm != NULL); 20065 20066 /* adjust MIBs and IRE timestamp */ 20067 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 20068 tcp->tcp_obsegs += obsegs; 20069 UPDATE_MIB(&tcp_mib, tcpOutDataSegs, obsegs); 20070 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, obbytes); 20071 TCP_STAT_UPDATE(tcp_mdt_pkt_out, obsegs); 20072 20073 if (tcp->tcp_ipversion == IPV4_VERSION) { 20074 TCP_STAT_UPDATE(tcp_mdt_pkt_out_v4, obsegs); 20075 UPDATE_MIB(&ip_mib, ipOutRequests, obsegs); 20076 } else { 20077 TCP_STAT_UPDATE(tcp_mdt_pkt_out_v6, obsegs); 20078 UPDATE_MIB(&ip6_mib, ipv6OutRequests, obsegs); 20079 } 20080 20081 ire->ire_ob_pkt_count += obsegs; 20082 if (ire->ire_ipif != NULL) 20083 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20084 ire->ire_last_used_time = lbolt; 20085 20086 /* send it down */ 20087 putnext(ire->ire_stq, md_mp_head); 20088 20089 /* we're done for TCP/IPv4 */ 20090 if (tcp->tcp_ipversion == IPV4_VERSION) 20091 return; 20092 20093 nce = ire->ire_nce; 20094 20095 ASSERT(nce != NULL); 20096 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20097 ASSERT(nce->nce_state != ND_INCOMPLETE); 20098 20099 /* reachability confirmation? */ 20100 if (*rconfirm) { 20101 nce->nce_last = TICK_TO_MSEC(lbolt64); 20102 if (nce->nce_state != ND_REACHABLE) { 20103 mutex_enter(&nce->nce_lock); 20104 nce->nce_state = ND_REACHABLE; 20105 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20106 mutex_exit(&nce->nce_lock); 20107 (void) untimeout(nce->nce_timeout_id); 20108 if (ip_debug > 2) { 20109 /* ip1dbg */ 20110 pr_addr_dbg("tcp_multisend_data: state " 20111 "for %s changed to REACHABLE\n", 20112 AF_INET6, &ire->ire_addr_v6); 20113 } 20114 } 20115 /* reset transport reachability confirmation */ 20116 *rconfirm = B_FALSE; 20117 } 20118 20119 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20120 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20121 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20122 20123 if (delta > (uint64_t)ill->ill_reachable_time) { 20124 mutex_enter(&nce->nce_lock); 20125 switch (nce->nce_state) { 20126 case ND_REACHABLE: 20127 case ND_STALE: 20128 /* 20129 * ND_REACHABLE is identical to ND_STALE in this 20130 * specific case. If reachable time has expired for 20131 * this neighbor (delta is greater than reachable 20132 * time), conceptually, the neighbor cache is no 20133 * longer in REACHABLE state, but already in STALE 20134 * state. So the correct transition here is to 20135 * ND_DELAY. 20136 */ 20137 nce->nce_state = ND_DELAY; 20138 mutex_exit(&nce->nce_lock); 20139 NDP_RESTART_TIMER(nce, delay_first_probe_time); 20140 if (ip_debug > 3) { 20141 /* ip2dbg */ 20142 pr_addr_dbg("tcp_multisend_data: state " 20143 "for %s changed to DELAY\n", 20144 AF_INET6, &ire->ire_addr_v6); 20145 } 20146 break; 20147 case ND_DELAY: 20148 case ND_PROBE: 20149 mutex_exit(&nce->nce_lock); 20150 /* Timers have already started */ 20151 break; 20152 case ND_UNREACHABLE: 20153 /* 20154 * ndp timer has detected that this nce is 20155 * unreachable and initiated deleting this nce 20156 * and all its associated IREs. This is a race 20157 * where we found the ire before it was deleted 20158 * and have just sent out a packet using this 20159 * unreachable nce. 20160 */ 20161 mutex_exit(&nce->nce_lock); 20162 break; 20163 default: 20164 ASSERT(0); 20165 } 20166 } 20167 } 20168 20169 /* 20170 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20171 * scheme, and returns one of the following: 20172 * 20173 * -1 = failed allocation. 20174 * 0 = success; burst count reached, or usable send window is too small, 20175 * and that we'd rather wait until later before sending again. 20176 * 1 = success; we are called from tcp_multisend(), and both usable send 20177 * window and tail_unsent are greater than the MDT threshold, and thus 20178 * Multidata Transmit should be used instead. 20179 */ 20180 static int 20181 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20182 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20183 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20184 const int mdt_thres) 20185 { 20186 int num_burst_seg = tcp->tcp_snd_burst; 20187 20188 for (;;) { 20189 struct datab *db; 20190 tcph_t *tcph; 20191 uint32_t sum; 20192 mblk_t *mp, *mp1; 20193 uchar_t *rptr; 20194 int len; 20195 20196 /* 20197 * If we're called by tcp_multisend(), and the amount of 20198 * sendable data as well as the size of current xmit_tail 20199 * is beyond the MDT threshold, return to the caller and 20200 * let the large data transmit be done using MDT. 20201 */ 20202 if (*usable > 0 && *usable > mdt_thres && 20203 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 20204 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 20205 ASSERT(tcp->tcp_mdt); 20206 return (1); /* success; do large send */ 20207 } 20208 20209 if (num_burst_seg-- == 0) 20210 break; /* success; burst count reached */ 20211 20212 len = mss; 20213 if (len > *usable) { 20214 len = *usable; 20215 if (len <= 0) { 20216 /* Terminate the loop */ 20217 break; /* success; too small */ 20218 } 20219 /* 20220 * Sender silly-window avoidance. 20221 * Ignore this if we are going to send a 20222 * zero window probe out. 20223 * 20224 * TODO: force data into microscopic window? 20225 * ==> (!pushed || (unsent > usable)) 20226 */ 20227 if (len < (tcp->tcp_max_swnd >> 1) && 20228 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 20229 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 20230 len == 1) && (! tcp->tcp_zero_win_probe)) { 20231 /* 20232 * If the retransmit timer is not running 20233 * we start it so that we will retransmit 20234 * in the case when the the receiver has 20235 * decremented the window. 20236 */ 20237 if (*snxt == tcp->tcp_snxt && 20238 *snxt == tcp->tcp_suna) { 20239 /* 20240 * We are not supposed to send 20241 * anything. So let's wait a little 20242 * bit longer before breaking SWS 20243 * avoidance. 20244 * 20245 * What should the value be? 20246 * Suggestion: MAX(init rexmit time, 20247 * tcp->tcp_rto) 20248 */ 20249 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20250 } 20251 break; /* success; too small */ 20252 } 20253 } 20254 20255 tcph = tcp->tcp_tcph; 20256 20257 *usable -= len; /* Approximate - can be adjusted later */ 20258 if (*usable > 0) 20259 tcph->th_flags[0] = TH_ACK; 20260 else 20261 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 20262 20263 /* 20264 * Prime pump for IP's checksumming on our behalf 20265 * Include the adjustment for a source route if any. 20266 */ 20267 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20268 sum = (sum >> 16) + (sum & 0xFFFF); 20269 U16_TO_ABE16(sum, tcph->th_sum); 20270 20271 U32_TO_ABE32(*snxt, tcph->th_seq); 20272 20273 /* 20274 * Branch off to tcp_xmit_mp() if any of the VALID bits is 20275 * set. For the case when TCP_FSS_VALID is the only valid 20276 * bit (normal active close), branch off only when we think 20277 * that the FIN flag needs to be set. Note for this case, 20278 * that (snxt + len) may not reflect the actual seg_len, 20279 * as len may be further reduced in tcp_xmit_mp(). If len 20280 * gets modified, we will end up here again. 20281 */ 20282 if (tcp->tcp_valid_bits != 0 && 20283 (tcp->tcp_valid_bits != TCP_FSS_VALID || 20284 ((*snxt + len) == tcp->tcp_fss))) { 20285 uchar_t *prev_rptr; 20286 uint32_t prev_snxt = tcp->tcp_snxt; 20287 20288 if (*tail_unsent == 0) { 20289 ASSERT((*xmit_tail)->b_cont != NULL); 20290 *xmit_tail = (*xmit_tail)->b_cont; 20291 prev_rptr = (*xmit_tail)->b_rptr; 20292 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20293 (*xmit_tail)->b_rptr); 20294 } else { 20295 prev_rptr = (*xmit_tail)->b_rptr; 20296 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 20297 *tail_unsent; 20298 } 20299 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 20300 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20301 /* Restore tcp_snxt so we get amount sent right. */ 20302 tcp->tcp_snxt = prev_snxt; 20303 if (prev_rptr == (*xmit_tail)->b_rptr) { 20304 /* 20305 * If the previous timestamp is still in use, 20306 * don't stomp on it. 20307 */ 20308 if ((*xmit_tail)->b_next == NULL) { 20309 (*xmit_tail)->b_prev = local_time; 20310 (*xmit_tail)->b_next = 20311 (mblk_t *)(uintptr_t)(*snxt); 20312 } 20313 } else 20314 (*xmit_tail)->b_rptr = prev_rptr; 20315 20316 if (mp == NULL) 20317 return (-1); 20318 mp1 = mp->b_cont; 20319 20320 tcp->tcp_last_sent_len = (ushort_t)len; 20321 while (mp1->b_cont) { 20322 *xmit_tail = (*xmit_tail)->b_cont; 20323 (*xmit_tail)->b_prev = local_time; 20324 (*xmit_tail)->b_next = 20325 (mblk_t *)(uintptr_t)(*snxt); 20326 mp1 = mp1->b_cont; 20327 } 20328 *snxt += len; 20329 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20330 BUMP_LOCAL(tcp->tcp_obsegs); 20331 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 20332 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 20333 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 20334 tcp_send_data(tcp, q, mp); 20335 continue; 20336 } 20337 20338 *snxt += len; /* Adjust later if we don't send all of len */ 20339 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 20340 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 20341 20342 if (*tail_unsent) { 20343 /* Are the bytes above us in flight? */ 20344 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20345 if (rptr != (*xmit_tail)->b_rptr) { 20346 *tail_unsent -= len; 20347 tcp->tcp_last_sent_len = (ushort_t)len; 20348 len += tcp_hdr_len; 20349 if (tcp->tcp_ipversion == IPV4_VERSION) 20350 tcp->tcp_ipha->ipha_length = htons(len); 20351 else 20352 tcp->tcp_ip6h->ip6_plen = 20353 htons(len - 20354 ((char *)&tcp->tcp_ip6h[1] - 20355 tcp->tcp_iphc)); 20356 mp = dupb(*xmit_tail); 20357 if (!mp) 20358 return (-1); /* out_of_mem */ 20359 mp->b_rptr = rptr; 20360 /* 20361 * If the old timestamp is no longer in use, 20362 * sample a new timestamp now. 20363 */ 20364 if ((*xmit_tail)->b_next == NULL) { 20365 (*xmit_tail)->b_prev = local_time; 20366 (*xmit_tail)->b_next = 20367 (mblk_t *)(uintptr_t)(*snxt-len); 20368 } 20369 goto must_alloc; 20370 } 20371 } else { 20372 *xmit_tail = (*xmit_tail)->b_cont; 20373 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 20374 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 20375 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20376 (*xmit_tail)->b_rptr); 20377 } 20378 20379 (*xmit_tail)->b_prev = local_time; 20380 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 20381 20382 *tail_unsent -= len; 20383 tcp->tcp_last_sent_len = (ushort_t)len; 20384 20385 len += tcp_hdr_len; 20386 if (tcp->tcp_ipversion == IPV4_VERSION) 20387 tcp->tcp_ipha->ipha_length = htons(len); 20388 else 20389 tcp->tcp_ip6h->ip6_plen = htons(len - 20390 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20391 20392 mp = dupb(*xmit_tail); 20393 if (!mp) 20394 return (-1); /* out_of_mem */ 20395 20396 len = tcp_hdr_len; 20397 /* 20398 * There are four reasons to allocate a new hdr mblk: 20399 * 1) The bytes above us are in use by another packet 20400 * 2) We don't have good alignment 20401 * 3) The mblk is being shared 20402 * 4) We don't have enough room for a header 20403 */ 20404 rptr = mp->b_rptr - len; 20405 if (!OK_32PTR(rptr) || 20406 ((db = mp->b_datap), db->db_ref != 2) || 20407 rptr < db->db_base) { 20408 /* NOTE: we assume allocb returns an OK_32PTR */ 20409 20410 must_alloc:; 20411 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 20412 tcp_wroff_xtra, BPRI_MED); 20413 if (!mp1) { 20414 freemsg(mp); 20415 return (-1); /* out_of_mem */ 20416 } 20417 mp1->b_cont = mp; 20418 mp = mp1; 20419 /* Leave room for Link Level header */ 20420 len = tcp_hdr_len; 20421 rptr = &mp->b_rptr[tcp_wroff_xtra]; 20422 mp->b_wptr = &rptr[len]; 20423 } 20424 20425 /* 20426 * Fill in the header using the template header, and add 20427 * options such as time-stamp, ECN and/or SACK, as needed. 20428 */ 20429 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 20430 20431 mp->b_rptr = rptr; 20432 20433 if (*tail_unsent) { 20434 int spill = *tail_unsent; 20435 20436 mp1 = mp->b_cont; 20437 if (!mp1) 20438 mp1 = mp; 20439 20440 /* 20441 * If we're a little short, tack on more mblks until 20442 * there is no more spillover. 20443 */ 20444 while (spill < 0) { 20445 mblk_t *nmp; 20446 int nmpsz; 20447 20448 nmp = (*xmit_tail)->b_cont; 20449 nmpsz = MBLKL(nmp); 20450 20451 /* 20452 * Excess data in mblk; can we split it? 20453 * If MDT is enabled for the connection, 20454 * keep on splitting as this is a transient 20455 * send path. 20456 */ 20457 if (!tcp->tcp_mdt && (spill + nmpsz > 0)) { 20458 /* 20459 * Don't split if stream head was 20460 * told to break up larger writes 20461 * into smaller ones. 20462 */ 20463 if (tcp->tcp_maxpsz > 0) 20464 break; 20465 20466 /* 20467 * Next mblk is less than SMSS/2 20468 * rounded up to nearest 64-byte; 20469 * let it get sent as part of the 20470 * next segment. 20471 */ 20472 if (tcp->tcp_localnet && 20473 !tcp->tcp_cork && 20474 (nmpsz < roundup((mss >> 1), 64))) 20475 break; 20476 } 20477 20478 *xmit_tail = nmp; 20479 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 20480 /* Stash for rtt use later */ 20481 (*xmit_tail)->b_prev = local_time; 20482 (*xmit_tail)->b_next = 20483 (mblk_t *)(uintptr_t)(*snxt - len); 20484 mp1->b_cont = dupb(*xmit_tail); 20485 mp1 = mp1->b_cont; 20486 20487 spill += nmpsz; 20488 if (mp1 == NULL) { 20489 *tail_unsent = spill; 20490 freemsg(mp); 20491 return (-1); /* out_of_mem */ 20492 } 20493 } 20494 20495 /* Trim back any surplus on the last mblk */ 20496 if (spill >= 0) { 20497 mp1->b_wptr -= spill; 20498 *tail_unsent = spill; 20499 } else { 20500 /* 20501 * We did not send everything we could in 20502 * order to remain within the b_cont limit. 20503 */ 20504 *usable -= spill; 20505 *snxt += spill; 20506 tcp->tcp_last_sent_len += spill; 20507 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, spill); 20508 /* 20509 * Adjust the checksum 20510 */ 20511 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 20512 sum += spill; 20513 sum = (sum >> 16) + (sum & 0xFFFF); 20514 U16_TO_ABE16(sum, tcph->th_sum); 20515 if (tcp->tcp_ipversion == IPV4_VERSION) { 20516 sum = ntohs( 20517 ((ipha_t *)rptr)->ipha_length) + 20518 spill; 20519 ((ipha_t *)rptr)->ipha_length = 20520 htons(sum); 20521 } else { 20522 sum = ntohs( 20523 ((ip6_t *)rptr)->ip6_plen) + 20524 spill; 20525 ((ip6_t *)rptr)->ip6_plen = 20526 htons(sum); 20527 } 20528 *tail_unsent = 0; 20529 } 20530 } 20531 if (tcp->tcp_ip_forward_progress) { 20532 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20533 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 20534 tcp->tcp_ip_forward_progress = B_FALSE; 20535 } 20536 20537 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 20538 tcp_send_data(tcp, q, mp); 20539 BUMP_LOCAL(tcp->tcp_obsegs); 20540 } 20541 20542 return (0); 20543 } 20544 20545 /* Unlink and return any mblk that looks like it contains a MDT info */ 20546 static mblk_t * 20547 tcp_mdt_info_mp(mblk_t *mp) 20548 { 20549 mblk_t *prev_mp; 20550 20551 for (;;) { 20552 prev_mp = mp; 20553 /* no more to process? */ 20554 if ((mp = mp->b_cont) == NULL) 20555 break; 20556 20557 switch (DB_TYPE(mp)) { 20558 case M_CTL: 20559 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 20560 continue; 20561 ASSERT(prev_mp != NULL); 20562 prev_mp->b_cont = mp->b_cont; 20563 mp->b_cont = NULL; 20564 return (mp); 20565 default: 20566 break; 20567 } 20568 } 20569 return (mp); 20570 } 20571 20572 /* MDT info update routine, called when IP notifies us about MDT */ 20573 static void 20574 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 20575 { 20576 boolean_t prev_state; 20577 20578 /* 20579 * IP is telling us to abort MDT on this connection? We know 20580 * this because the capability is only turned off when IP 20581 * encounters some pathological cases, e.g. link-layer change 20582 * where the new driver doesn't support MDT, or in situation 20583 * where MDT usage on the link-layer has been switched off. 20584 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 20585 * if the link-layer doesn't support MDT, and if it does, it 20586 * will indicate that the feature is to be turned on. 20587 */ 20588 prev_state = tcp->tcp_mdt; 20589 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 20590 if (!tcp->tcp_mdt && !first) { 20591 TCP_STAT(tcp_mdt_conn_halted3); 20592 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 20593 (void *)tcp->tcp_connp)); 20594 } 20595 20596 /* 20597 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 20598 * so disable MDT otherwise. The checks are done here 20599 * and in tcp_wput_data(). 20600 */ 20601 if (tcp->tcp_mdt && 20602 (tcp->tcp_ipversion == IPV4_VERSION && 20603 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 20604 (tcp->tcp_ipversion == IPV6_VERSION && 20605 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 20606 tcp->tcp_mdt = B_FALSE; 20607 20608 if (tcp->tcp_mdt) { 20609 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 20610 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 20611 "version (%d), expected version is %d", 20612 mdt_capab->ill_mdt_version, MDT_VERSION_2); 20613 tcp->tcp_mdt = B_FALSE; 20614 return; 20615 } 20616 20617 /* 20618 * We need the driver to be able to handle at least three 20619 * spans per packet in order for tcp MDT to be utilized. 20620 * The first is for the header portion, while the rest are 20621 * needed to handle a packet that straddles across two 20622 * virtually non-contiguous buffers; a typical tcp packet 20623 * therefore consists of only two spans. Note that we take 20624 * a zero as "don't care". 20625 */ 20626 if (mdt_capab->ill_mdt_span_limit > 0 && 20627 mdt_capab->ill_mdt_span_limit < 3) { 20628 tcp->tcp_mdt = B_FALSE; 20629 return; 20630 } 20631 20632 /* a zero means driver wants default value */ 20633 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 20634 tcp_mdt_max_pbufs); 20635 if (tcp->tcp_mdt_max_pld == 0) 20636 tcp->tcp_mdt_max_pld = tcp_mdt_max_pbufs; 20637 20638 /* ensure 32-bit alignment */ 20639 tcp->tcp_mdt_hdr_head = roundup(MAX(tcp_mdt_hdr_head_min, 20640 mdt_capab->ill_mdt_hdr_head), 4); 20641 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcp_mdt_hdr_tail_min, 20642 mdt_capab->ill_mdt_hdr_tail), 4); 20643 20644 if (!first && !prev_state) { 20645 TCP_STAT(tcp_mdt_conn_resumed2); 20646 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 20647 (void *)tcp->tcp_connp)); 20648 } 20649 } 20650 } 20651 20652 static void 20653 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_mdt) 20654 { 20655 conn_t *connp = tcp->tcp_connp; 20656 20657 ASSERT(ire != NULL); 20658 20659 /* 20660 * We may be in the fastpath here, and although we essentially do 20661 * similar checks as in ip_bind_connected{_v6}/ip_mdinfo_return, 20662 * we try to keep things as brief as possible. After all, these 20663 * are only best-effort checks, and we do more thorough ones prior 20664 * to calling tcp_multisend(). 20665 */ 20666 if (ip_multidata_outbound && check_mdt && 20667 !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 20668 ill != NULL && ILL_MDT_CAPABLE(ill) && 20669 !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 20670 !(ire->ire_flags & RTF_MULTIRT) && 20671 !IPP_ENABLED(IPP_LOCAL_OUT) && 20672 CONN_IS_MD_FASTPATH(connp)) { 20673 /* Remember the result */ 20674 connp->conn_mdt_ok = B_TRUE; 20675 20676 ASSERT(ill->ill_mdt_capab != NULL); 20677 if (!ill->ill_mdt_capab->ill_mdt_on) { 20678 /* 20679 * If MDT has been previously turned off in the past, 20680 * and we currently can do MDT (due to IPQoS policy 20681 * removal, etc.) then enable it for this interface. 20682 */ 20683 ill->ill_mdt_capab->ill_mdt_on = 1; 20684 ip1dbg(("tcp_ire_ill_check: connp %p enables MDT for " 20685 "interface %s\n", (void *)connp, ill->ill_name)); 20686 } 20687 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 20688 } 20689 20690 /* 20691 * The goal is to reduce the number of generated tcp segments by 20692 * setting the maxpsz multiplier to 0; this will have an affect on 20693 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 20694 * into each packet, up to SMSS bytes. Doing this reduces the number 20695 * of outbound segments and incoming ACKs, thus allowing for better 20696 * network and system performance. In contrast the legacy behavior 20697 * may result in sending less than SMSS size, because the last mblk 20698 * for some packets may have more data than needed to make up SMSS, 20699 * and the legacy code refused to "split" it. 20700 * 20701 * We apply the new behavior on following situations: 20702 * 20703 * 1) Loopback connections, 20704 * 2) Connections in which the remote peer is not on local subnet, 20705 * 3) Local subnet connections over the bge interface (see below). 20706 * 20707 * Ideally, we would like this behavior to apply for interfaces other 20708 * than bge. However, doing so would negatively impact drivers which 20709 * perform dynamic mapping and unmapping of DMA resources, which are 20710 * increased by setting the maxpsz multiplier to 0 (more mblks per 20711 * packet will be generated by tcp). The bge driver does not suffer 20712 * from this, as it copies the mblks into pre-mapped buffers, and 20713 * therefore does not require more I/O resources than before. 20714 * 20715 * Otherwise, this behavior is present on all network interfaces when 20716 * the destination endpoint is non-local, since reducing the number 20717 * of packets in general is good for the network. 20718 * 20719 * TODO We need to remove this hard-coded conditional for bge once 20720 * a better "self-tuning" mechanism, or a way to comprehend 20721 * the driver transmit strategy is devised. Until the solution 20722 * is found and well understood, we live with this hack. 20723 */ 20724 if (!tcp_static_maxpsz && 20725 (tcp->tcp_loopback || !tcp->tcp_localnet || 20726 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 20727 /* override the default value */ 20728 tcp->tcp_maxpsz = 0; 20729 20730 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 20731 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 20732 ill != NULL ? ill->ill_name : ipif_loopback_name)); 20733 } 20734 20735 /* set the stream head parameters accordingly */ 20736 (void) tcp_maxpsz_set(tcp, B_TRUE); 20737 } 20738 20739 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 20740 static void 20741 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 20742 { 20743 uchar_t fval = *mp->b_rptr; 20744 mblk_t *tail; 20745 queue_t *q = tcp->tcp_wq; 20746 20747 /* TODO: How should flush interact with urgent data? */ 20748 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 20749 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 20750 /* 20751 * Flush only data that has not yet been put on the wire. If 20752 * we flush data that we have already transmitted, life, as we 20753 * know it, may come to an end. 20754 */ 20755 tail = tcp->tcp_xmit_tail; 20756 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 20757 tcp->tcp_xmit_tail_unsent = 0; 20758 tcp->tcp_unsent = 0; 20759 if (tail->b_wptr != tail->b_rptr) 20760 tail = tail->b_cont; 20761 if (tail) { 20762 mblk_t **excess = &tcp->tcp_xmit_head; 20763 for (;;) { 20764 mblk_t *mp1 = *excess; 20765 if (mp1 == tail) 20766 break; 20767 tcp->tcp_xmit_tail = mp1; 20768 tcp->tcp_xmit_last = mp1; 20769 excess = &mp1->b_cont; 20770 } 20771 *excess = NULL; 20772 tcp_close_mpp(&tail); 20773 if (tcp->tcp_snd_zcopy_aware) 20774 tcp_zcopy_notify(tcp); 20775 } 20776 /* 20777 * We have no unsent data, so unsent must be less than 20778 * tcp_xmit_lowater, so re-enable flow. 20779 */ 20780 if (tcp->tcp_flow_stopped) { 20781 tcp_clrqfull(tcp); 20782 } 20783 } 20784 /* 20785 * TODO: you can't just flush these, you have to increase rwnd for one 20786 * thing. For another, how should urgent data interact? 20787 */ 20788 if (fval & FLUSHR) { 20789 *mp->b_rptr = fval & ~FLUSHW; 20790 /* XXX */ 20791 qreply(q, mp); 20792 return; 20793 } 20794 freemsg(mp); 20795 } 20796 20797 /* 20798 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 20799 * messages. 20800 */ 20801 static void 20802 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 20803 { 20804 mblk_t *mp1; 20805 STRUCT_HANDLE(strbuf, sb); 20806 uint16_t port; 20807 queue_t *q = tcp->tcp_wq; 20808 in6_addr_t v6addr; 20809 ipaddr_t v4addr; 20810 uint32_t flowinfo = 0; 20811 int addrlen; 20812 20813 /* Make sure it is one of ours. */ 20814 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 20815 case TI_GETMYNAME: 20816 case TI_GETPEERNAME: 20817 break; 20818 default: 20819 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20820 return; 20821 } 20822 switch (mi_copy_state(q, mp, &mp1)) { 20823 case -1: 20824 return; 20825 case MI_COPY_CASE(MI_COPY_IN, 1): 20826 break; 20827 case MI_COPY_CASE(MI_COPY_OUT, 1): 20828 /* Copy out the strbuf. */ 20829 mi_copyout(q, mp); 20830 return; 20831 case MI_COPY_CASE(MI_COPY_OUT, 2): 20832 /* All done. */ 20833 mi_copy_done(q, mp, 0); 20834 return; 20835 default: 20836 mi_copy_done(q, mp, EPROTO); 20837 return; 20838 } 20839 /* Check alignment of the strbuf */ 20840 if (!OK_32PTR(mp1->b_rptr)) { 20841 mi_copy_done(q, mp, EINVAL); 20842 return; 20843 } 20844 20845 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 20846 (void *)mp1->b_rptr); 20847 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 20848 20849 if (STRUCT_FGET(sb, maxlen) < addrlen) { 20850 mi_copy_done(q, mp, EINVAL); 20851 return; 20852 } 20853 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 20854 case TI_GETMYNAME: 20855 if (tcp->tcp_family == AF_INET) { 20856 if (tcp->tcp_ipversion == IPV4_VERSION) { 20857 v4addr = tcp->tcp_ipha->ipha_src; 20858 } else { 20859 /* can't return an address in this case */ 20860 v4addr = 0; 20861 } 20862 } else { 20863 /* tcp->tcp_family == AF_INET6 */ 20864 if (tcp->tcp_ipversion == IPV4_VERSION) { 20865 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 20866 &v6addr); 20867 } else { 20868 v6addr = tcp->tcp_ip6h->ip6_src; 20869 } 20870 } 20871 port = tcp->tcp_lport; 20872 break; 20873 case TI_GETPEERNAME: 20874 if (tcp->tcp_family == AF_INET) { 20875 if (tcp->tcp_ipversion == IPV4_VERSION) { 20876 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 20877 v4addr); 20878 } else { 20879 /* can't return an address in this case */ 20880 v4addr = 0; 20881 } 20882 } else { 20883 /* tcp->tcp_family == AF_INET6) */ 20884 v6addr = tcp->tcp_remote_v6; 20885 if (tcp->tcp_ipversion == IPV6_VERSION) { 20886 /* 20887 * No flowinfo if tcp->tcp_ipversion is v4. 20888 * 20889 * flowinfo was already initialized to zero 20890 * where it was declared above, so only 20891 * set it if ipversion is v6. 20892 */ 20893 flowinfo = tcp->tcp_ip6h->ip6_vcf & 20894 ~IPV6_VERS_AND_FLOW_MASK; 20895 } 20896 } 20897 port = tcp->tcp_fport; 20898 break; 20899 default: 20900 mi_copy_done(q, mp, EPROTO); 20901 return; 20902 } 20903 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 20904 if (!mp1) 20905 return; 20906 20907 if (tcp->tcp_family == AF_INET) { 20908 sin_t *sin; 20909 20910 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 20911 sin = (sin_t *)mp1->b_rptr; 20912 mp1->b_wptr = (uchar_t *)&sin[1]; 20913 *sin = sin_null; 20914 sin->sin_family = AF_INET; 20915 sin->sin_addr.s_addr = v4addr; 20916 sin->sin_port = port; 20917 } else { 20918 /* tcp->tcp_family == AF_INET6 */ 20919 sin6_t *sin6; 20920 20921 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 20922 sin6 = (sin6_t *)mp1->b_rptr; 20923 mp1->b_wptr = (uchar_t *)&sin6[1]; 20924 *sin6 = sin6_null; 20925 sin6->sin6_family = AF_INET6; 20926 sin6->sin6_flowinfo = flowinfo; 20927 sin6->sin6_addr = v6addr; 20928 sin6->sin6_port = port; 20929 } 20930 /* Copy out the address */ 20931 mi_copyout(q, mp); 20932 } 20933 20934 /* 20935 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 20936 * messages. 20937 */ 20938 /* ARGSUSED */ 20939 static void 20940 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 20941 { 20942 conn_t *connp = (conn_t *)arg; 20943 tcp_t *tcp = connp->conn_tcp; 20944 queue_t *q = tcp->tcp_wq; 20945 struct iocblk *iocp; 20946 20947 ASSERT(DB_TYPE(mp) == M_IOCTL); 20948 /* 20949 * Try and ASSERT the minimum possible references on the 20950 * conn early enough. Since we are executing on write side, 20951 * the connection is obviously not detached and that means 20952 * there is a ref each for TCP and IP. Since we are behind 20953 * the squeue, the minimum references needed are 3. If the 20954 * conn is in classifier hash list, there should be an 20955 * extra ref for that (we check both the possibilities). 20956 */ 20957 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 20958 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 20959 20960 iocp = (struct iocblk *)mp->b_rptr; 20961 switch (iocp->ioc_cmd) { 20962 case TCP_IOC_DEFAULT_Q: 20963 /* Wants to be the default wq. */ 20964 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 20965 iocp->ioc_error = EPERM; 20966 iocp->ioc_count = 0; 20967 mp->b_datap->db_type = M_IOCACK; 20968 qreply(q, mp); 20969 return; 20970 } 20971 tcp_def_q_set(tcp, mp); 20972 return; 20973 case _SIOCSOCKFALLBACK: 20974 /* 20975 * Either sockmod is about to be popped and the socket 20976 * would now be treated as a plain stream, or a module 20977 * is about to be pushed so we could no longer use read- 20978 * side synchronous streams for fused loopback tcp. 20979 * Drain any queued data and disable direct sockfs 20980 * interface from now on. 20981 */ 20982 if (!tcp->tcp_issocket) { 20983 DB_TYPE(mp) = M_IOCNAK; 20984 iocp->ioc_error = EINVAL; 20985 } else { 20986 #ifdef _ILP32 20987 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 20988 #else 20989 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 20990 #endif 20991 /* 20992 * Insert this socket into the acceptor hash. 20993 * We might need it for T_CONN_RES message 20994 */ 20995 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 20996 20997 if (tcp->tcp_fused) { 20998 /* 20999 * This is a fused loopback tcp; disable 21000 * read-side synchronous streams interface 21001 * and drain any queued data. It is okay 21002 * to do this for non-synchronous streams 21003 * fused tcp as well. 21004 */ 21005 tcp_fuse_disable_pair(tcp, B_FALSE); 21006 } 21007 tcp->tcp_issocket = B_FALSE; 21008 TCP_STAT(tcp_sock_fallback); 21009 21010 DB_TYPE(mp) = M_IOCACK; 21011 iocp->ioc_error = 0; 21012 } 21013 iocp->ioc_count = 0; 21014 iocp->ioc_rval = 0; 21015 qreply(q, mp); 21016 return; 21017 } 21018 CALL_IP_WPUT(connp, q, mp); 21019 } 21020 21021 /* 21022 * This routine is called by tcp_wput() to handle all TPI requests. 21023 */ 21024 /* ARGSUSED */ 21025 static void 21026 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21027 { 21028 conn_t *connp = (conn_t *)arg; 21029 tcp_t *tcp = connp->conn_tcp; 21030 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21031 uchar_t *rptr; 21032 t_scalar_t type; 21033 int len; 21034 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 21035 21036 /* 21037 * Try and ASSERT the minimum possible references on the 21038 * conn early enough. Since we are executing on write side, 21039 * the connection is obviously not detached and that means 21040 * there is a ref each for TCP and IP. Since we are behind 21041 * the squeue, the minimum references needed are 3. If the 21042 * conn is in classifier hash list, there should be an 21043 * extra ref for that (we check both the possibilities). 21044 */ 21045 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21046 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21047 21048 rptr = mp->b_rptr; 21049 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21050 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 21051 type = ((union T_primitives *)rptr)->type; 21052 if (type == T_EXDATA_REQ) { 21053 uint32_t msize = msgdsize(mp->b_cont); 21054 21055 len = msize - 1; 21056 if (len < 0) { 21057 freemsg(mp); 21058 return; 21059 } 21060 /* 21061 * Try to force urgent data out on the wire. 21062 * Even if we have unsent data this will 21063 * at least send the urgent flag. 21064 * XXX does not handle more flag correctly. 21065 */ 21066 len += tcp->tcp_unsent; 21067 len += tcp->tcp_snxt; 21068 tcp->tcp_urg = len; 21069 tcp->tcp_valid_bits |= TCP_URG_VALID; 21070 21071 /* Bypass tcp protocol for fused tcp loopback */ 21072 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 21073 return; 21074 } else if (type != T_DATA_REQ) { 21075 goto non_urgent_data; 21076 } 21077 /* TODO: options, flags, ... from user */ 21078 /* Set length to zero for reclamation below */ 21079 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 21080 freeb(mp); 21081 return; 21082 } else { 21083 if (tcp->tcp_debug) { 21084 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21085 "tcp_wput_proto, dropping one..."); 21086 } 21087 freemsg(mp); 21088 return; 21089 } 21090 21091 non_urgent_data: 21092 21093 switch ((int)tprim->type) { 21094 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 21095 /* 21096 * save the kssl_ent_t from the next block, and convert this 21097 * back to a normal bind_req. 21098 */ 21099 if (mp->b_cont != NULL) { 21100 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 21101 21102 if (tcp->tcp_kssl_ent != NULL) { 21103 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 21104 KSSL_NO_PROXY); 21105 tcp->tcp_kssl_ent = NULL; 21106 } 21107 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 21108 sizeof (kssl_ent_t)); 21109 kssl_hold_ent(tcp->tcp_kssl_ent); 21110 freemsg(mp->b_cont); 21111 mp->b_cont = NULL; 21112 } 21113 tprim->type = T_BIND_REQ; 21114 21115 /* FALLTHROUGH */ 21116 case O_T_BIND_REQ: /* bind request */ 21117 case T_BIND_REQ: /* new semantics bind request */ 21118 tcp_bind(tcp, mp); 21119 break; 21120 case T_UNBIND_REQ: /* unbind request */ 21121 tcp_unbind(tcp, mp); 21122 break; 21123 case O_T_CONN_RES: /* old connection response XXX */ 21124 case T_CONN_RES: /* connection response */ 21125 tcp_accept(tcp, mp); 21126 break; 21127 case T_CONN_REQ: /* connection request */ 21128 tcp_connect(tcp, mp); 21129 break; 21130 case T_DISCON_REQ: /* disconnect request */ 21131 tcp_disconnect(tcp, mp); 21132 break; 21133 case T_CAPABILITY_REQ: 21134 tcp_capability_req(tcp, mp); /* capability request */ 21135 break; 21136 case T_INFO_REQ: /* information request */ 21137 tcp_info_req(tcp, mp); 21138 break; 21139 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 21140 /* Only IP is allowed to return meaningful value */ 21141 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 21142 break; 21143 case T_OPTMGMT_REQ: 21144 /* 21145 * Note: no support for snmpcom_req() through new 21146 * T_OPTMGMT_REQ. See comments in ip.c 21147 */ 21148 /* Only IP is allowed to return meaningful value */ 21149 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 21150 break; 21151 21152 case T_UNITDATA_REQ: /* unitdata request */ 21153 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21154 break; 21155 case T_ORDREL_REQ: /* orderly release req */ 21156 freemsg(mp); 21157 21158 if (tcp->tcp_fused) 21159 tcp_unfuse(tcp); 21160 21161 if (tcp_xmit_end(tcp) != 0) { 21162 /* 21163 * We were crossing FINs and got a reset from 21164 * the other side. Just ignore it. 21165 */ 21166 if (tcp->tcp_debug) { 21167 (void) strlog(TCP_MOD_ID, 0, 1, 21168 SL_ERROR|SL_TRACE, 21169 "tcp_wput_proto, T_ORDREL_REQ out of " 21170 "state %s", 21171 tcp_display(tcp, NULL, 21172 DISP_ADDR_AND_PORT)); 21173 } 21174 } 21175 break; 21176 case T_ADDR_REQ: 21177 tcp_addr_req(tcp, mp); 21178 break; 21179 default: 21180 if (tcp->tcp_debug) { 21181 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21182 "tcp_wput_proto, bogus TPI msg, type %d", 21183 tprim->type); 21184 } 21185 /* 21186 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 21187 * to recover. 21188 */ 21189 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21190 break; 21191 } 21192 } 21193 21194 /* 21195 * The TCP write service routine should never be called... 21196 */ 21197 /* ARGSUSED */ 21198 static void 21199 tcp_wsrv(queue_t *q) 21200 { 21201 TCP_STAT(tcp_wsrv_called); 21202 } 21203 21204 /* Non overlapping byte exchanger */ 21205 static void 21206 tcp_xchg(uchar_t *a, uchar_t *b, int len) 21207 { 21208 uchar_t uch; 21209 21210 while (len-- > 0) { 21211 uch = a[len]; 21212 a[len] = b[len]; 21213 b[len] = uch; 21214 } 21215 } 21216 21217 /* 21218 * Send out a control packet on the tcp connection specified. This routine 21219 * is typically called where we need a simple ACK or RST generated. 21220 */ 21221 static void 21222 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 21223 { 21224 uchar_t *rptr; 21225 tcph_t *tcph; 21226 ipha_t *ipha = NULL; 21227 ip6_t *ip6h = NULL; 21228 uint32_t sum; 21229 int tcp_hdr_len; 21230 int tcp_ip_hdr_len; 21231 mblk_t *mp; 21232 21233 /* 21234 * Save sum for use in source route later. 21235 */ 21236 ASSERT(tcp != NULL); 21237 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 21238 tcp_hdr_len = tcp->tcp_hdr_len; 21239 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 21240 21241 /* If a text string is passed in with the request, pass it to strlog. */ 21242 if (str != NULL && tcp->tcp_debug) { 21243 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21244 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 21245 str, seq, ack, ctl); 21246 } 21247 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 21248 BPRI_MED); 21249 if (mp == NULL) { 21250 return; 21251 } 21252 rptr = &mp->b_rptr[tcp_wroff_xtra]; 21253 mp->b_rptr = rptr; 21254 mp->b_wptr = &rptr[tcp_hdr_len]; 21255 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 21256 21257 if (tcp->tcp_ipversion == IPV4_VERSION) { 21258 ipha = (ipha_t *)rptr; 21259 ipha->ipha_length = htons(tcp_hdr_len); 21260 } else { 21261 ip6h = (ip6_t *)rptr; 21262 ASSERT(tcp != NULL); 21263 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 21264 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21265 } 21266 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 21267 tcph->th_flags[0] = (uint8_t)ctl; 21268 if (ctl & TH_RST) { 21269 BUMP_MIB(&tcp_mib, tcpOutRsts); 21270 BUMP_MIB(&tcp_mib, tcpOutControl); 21271 /* 21272 * Don't send TSopt w/ TH_RST packets per RFC 1323. 21273 */ 21274 if (tcp->tcp_snd_ts_ok && 21275 tcp->tcp_state > TCPS_SYN_SENT) { 21276 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 21277 *(mp->b_wptr) = TCPOPT_EOL; 21278 if (tcp->tcp_ipversion == IPV4_VERSION) { 21279 ipha->ipha_length = htons(tcp_hdr_len - 21280 TCPOPT_REAL_TS_LEN); 21281 } else { 21282 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 21283 TCPOPT_REAL_TS_LEN); 21284 } 21285 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 21286 sum -= TCPOPT_REAL_TS_LEN; 21287 } 21288 } 21289 if (ctl & TH_ACK) { 21290 if (tcp->tcp_snd_ts_ok) { 21291 U32_TO_BE32(lbolt, 21292 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 21293 U32_TO_BE32(tcp->tcp_ts_recent, 21294 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 21295 } 21296 21297 /* Update the latest receive window size in TCP header. */ 21298 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21299 tcph->th_win); 21300 tcp->tcp_rack = ack; 21301 tcp->tcp_rack_cnt = 0; 21302 BUMP_MIB(&tcp_mib, tcpOutAck); 21303 } 21304 BUMP_LOCAL(tcp->tcp_obsegs); 21305 U32_TO_BE32(seq, tcph->th_seq); 21306 U32_TO_BE32(ack, tcph->th_ack); 21307 /* 21308 * Include the adjustment for a source route if any. 21309 */ 21310 sum = (sum >> 16) + (sum & 0xFFFF); 21311 U16_TO_BE16(sum, tcph->th_sum); 21312 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21313 tcp_send_data(tcp, tcp->tcp_wq, mp); 21314 } 21315 21316 /* 21317 * If this routine returns B_TRUE, TCP can generate a RST in response 21318 * to a segment. If it returns B_FALSE, TCP should not respond. 21319 */ 21320 static boolean_t 21321 tcp_send_rst_chk(void) 21322 { 21323 clock_t now; 21324 21325 /* 21326 * TCP needs to protect itself from generating too many RSTs. 21327 * This can be a DoS attack by sending us random segments 21328 * soliciting RSTs. 21329 * 21330 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 21331 * in each 1 second interval. In this way, TCP still generate 21332 * RSTs in normal cases but when under attack, the impact is 21333 * limited. 21334 */ 21335 if (tcp_rst_sent_rate_enabled != 0) { 21336 now = lbolt; 21337 /* lbolt can wrap around. */ 21338 if ((tcp_last_rst_intrvl > now) || 21339 (TICK_TO_MSEC(now - tcp_last_rst_intrvl) > 1*SECONDS)) { 21340 tcp_last_rst_intrvl = now; 21341 tcp_rst_cnt = 1; 21342 } else if (++tcp_rst_cnt > tcp_rst_sent_rate) { 21343 return (B_FALSE); 21344 } 21345 } 21346 return (B_TRUE); 21347 } 21348 21349 /* 21350 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 21351 */ 21352 static void 21353 tcp_ip_ire_mark_advice(tcp_t *tcp) 21354 { 21355 mblk_t *mp; 21356 ipic_t *ipic; 21357 21358 if (tcp->tcp_ipversion == IPV4_VERSION) { 21359 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21360 &ipic); 21361 } else { 21362 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21363 &ipic); 21364 } 21365 if (mp == NULL) 21366 return; 21367 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21368 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21369 } 21370 21371 /* 21372 * Return an IP advice ioctl mblk and set ipic to be the pointer 21373 * to the advice structure. 21374 */ 21375 static mblk_t * 21376 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 21377 { 21378 struct iocblk *ioc; 21379 mblk_t *mp, *mp1; 21380 21381 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 21382 if (mp == NULL) 21383 return (NULL); 21384 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 21385 *ipic = (ipic_t *)mp->b_rptr; 21386 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 21387 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 21388 21389 bcopy(addr, *ipic + 1, addr_len); 21390 21391 (*ipic)->ipic_addr_length = addr_len; 21392 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 21393 21394 mp1 = mkiocb(IP_IOCTL); 21395 if (mp1 == NULL) { 21396 freemsg(mp); 21397 return (NULL); 21398 } 21399 mp1->b_cont = mp; 21400 ioc = (struct iocblk *)mp1->b_rptr; 21401 ioc->ioc_count = sizeof (ipic_t) + addr_len; 21402 21403 return (mp1); 21404 } 21405 21406 /* 21407 * Generate a reset based on an inbound packet for which there is no active 21408 * tcp state that we can find. 21409 * 21410 * IPSEC NOTE : Try to send the reply with the same protection as it came 21411 * in. We still have the ipsec_mp that the packet was attached to. Thus 21412 * the packet will go out at the same level of protection as it came in by 21413 * converting the IPSEC_IN to IPSEC_OUT. 21414 */ 21415 static void 21416 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 21417 uint32_t ack, int ctl, uint_t ip_hdr_len) 21418 { 21419 ipha_t *ipha = NULL; 21420 ip6_t *ip6h = NULL; 21421 ushort_t len; 21422 tcph_t *tcph; 21423 int i; 21424 mblk_t *ipsec_mp; 21425 boolean_t mctl_present; 21426 ipic_t *ipic; 21427 ipaddr_t v4addr; 21428 in6_addr_t v6addr; 21429 int addr_len; 21430 void *addr; 21431 queue_t *q = tcp_g_q; 21432 tcp_t *tcp = Q_TO_TCP(q); 21433 cred_t *cr; 21434 21435 if (!tcp_send_rst_chk()) { 21436 tcp_rst_unsent++; 21437 freemsg(mp); 21438 return; 21439 } 21440 21441 if (mp->b_datap->db_type == M_CTL) { 21442 ipsec_mp = mp; 21443 mp = mp->b_cont; 21444 mctl_present = B_TRUE; 21445 } else { 21446 ipsec_mp = mp; 21447 mctl_present = B_FALSE; 21448 } 21449 21450 if (str && q && tcp_dbg) { 21451 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21452 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 21453 "flags 0x%x", 21454 str, seq, ack, ctl); 21455 } 21456 if (mp->b_datap->db_ref != 1) { 21457 mblk_t *mp1 = copyb(mp); 21458 freemsg(mp); 21459 mp = mp1; 21460 if (!mp) { 21461 if (mctl_present) 21462 freeb(ipsec_mp); 21463 return; 21464 } else { 21465 if (mctl_present) { 21466 ipsec_mp->b_cont = mp; 21467 } else { 21468 ipsec_mp = mp; 21469 } 21470 } 21471 } else if (mp->b_cont) { 21472 freemsg(mp->b_cont); 21473 mp->b_cont = NULL; 21474 } 21475 /* 21476 * We skip reversing source route here. 21477 * (for now we replace all IP options with EOL) 21478 */ 21479 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21480 ipha = (ipha_t *)mp->b_rptr; 21481 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 21482 mp->b_rptr[i] = IPOPT_EOL; 21483 /* 21484 * Make sure that src address isn't flagrantly invalid. 21485 * Not all broadcast address checking for the src address 21486 * is possible, since we don't know the netmask of the src 21487 * addr. No check for destination address is done, since 21488 * IP will not pass up a packet with a broadcast dest 21489 * address to TCP. Similar checks are done below for IPv6. 21490 */ 21491 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 21492 CLASSD(ipha->ipha_src)) { 21493 freemsg(ipsec_mp); 21494 BUMP_MIB(&ip_mib, ipInDiscards); 21495 return; 21496 } 21497 } else { 21498 ip6h = (ip6_t *)mp->b_rptr; 21499 21500 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 21501 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 21502 freemsg(ipsec_mp); 21503 BUMP_MIB(&ip6_mib, ipv6InDiscards); 21504 return; 21505 } 21506 21507 /* Remove any extension headers assuming partial overlay */ 21508 if (ip_hdr_len > IPV6_HDR_LEN) { 21509 uint8_t *to; 21510 21511 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 21512 ovbcopy(ip6h, to, IPV6_HDR_LEN); 21513 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 21514 ip_hdr_len = IPV6_HDR_LEN; 21515 ip6h = (ip6_t *)mp->b_rptr; 21516 ip6h->ip6_nxt = IPPROTO_TCP; 21517 } 21518 } 21519 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 21520 if (tcph->th_flags[0] & TH_RST) { 21521 freemsg(ipsec_mp); 21522 return; 21523 } 21524 tcph->th_offset_and_rsrvd[0] = (5 << 4); 21525 len = ip_hdr_len + sizeof (tcph_t); 21526 mp->b_wptr = &mp->b_rptr[len]; 21527 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21528 ipha->ipha_length = htons(len); 21529 /* Swap addresses */ 21530 v4addr = ipha->ipha_src; 21531 ipha->ipha_src = ipha->ipha_dst; 21532 ipha->ipha_dst = v4addr; 21533 ipha->ipha_ident = 0; 21534 ipha->ipha_ttl = (uchar_t)tcp_ipv4_ttl; 21535 addr_len = IP_ADDR_LEN; 21536 addr = &v4addr; 21537 } else { 21538 /* No ip6i_t in this case */ 21539 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 21540 /* Swap addresses */ 21541 v6addr = ip6h->ip6_src; 21542 ip6h->ip6_src = ip6h->ip6_dst; 21543 ip6h->ip6_dst = v6addr; 21544 ip6h->ip6_hops = (uchar_t)tcp_ipv6_hoplimit; 21545 addr_len = IPV6_ADDR_LEN; 21546 addr = &v6addr; 21547 } 21548 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 21549 U32_TO_BE32(ack, tcph->th_ack); 21550 U32_TO_BE32(seq, tcph->th_seq); 21551 U16_TO_BE16(0, tcph->th_win); 21552 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 21553 tcph->th_flags[0] = (uint8_t)ctl; 21554 if (ctl & TH_RST) { 21555 BUMP_MIB(&tcp_mib, tcpOutRsts); 21556 BUMP_MIB(&tcp_mib, tcpOutControl); 21557 } 21558 21559 /* IP trusts us to set up labels when required. */ 21560 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 21561 crgetlabel(cr) != NULL) { 21562 int err, adjust; 21563 21564 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 21565 err = tsol_check_label(cr, &mp, &adjust, 21566 tcp->tcp_connp->conn_mac_exempt); 21567 else 21568 err = tsol_check_label_v6(cr, &mp, &adjust, 21569 tcp->tcp_connp->conn_mac_exempt); 21570 if (mctl_present) 21571 ipsec_mp->b_cont = mp; 21572 else 21573 ipsec_mp = mp; 21574 if (err != 0) { 21575 freemsg(ipsec_mp); 21576 return; 21577 } 21578 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21579 ipha = (ipha_t *)mp->b_rptr; 21580 adjust += ntohs(ipha->ipha_length); 21581 ipha->ipha_length = htons(adjust); 21582 } else { 21583 ip6h = (ip6_t *)mp->b_rptr; 21584 } 21585 } 21586 21587 if (mctl_present) { 21588 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21589 21590 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21591 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 21592 return; 21593 } 21594 } 21595 /* 21596 * NOTE: one might consider tracing a TCP packet here, but 21597 * this function has no active TCP state and no tcp structure 21598 * that has a trace buffer. If we traced here, we would have 21599 * to keep a local trace buffer in tcp_record_trace(). 21600 * 21601 * TSol note: The mblk that contains the incoming packet was 21602 * reused by tcp_xmit_listener_reset, so it already contains 21603 * the right credentials and we don't need to call mblk_setcred. 21604 * Also the conn's cred is not right since it is associated 21605 * with tcp_g_q. 21606 */ 21607 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 21608 21609 /* 21610 * Tell IP to mark the IRE used for this destination temporary. 21611 * This way, we can limit our exposure to DoS attack because IP 21612 * creates an IRE for each destination. If there are too many, 21613 * the time to do any routing lookup will be extremely long. And 21614 * the lookup can be in interrupt context. 21615 * 21616 * Note that in normal circumstances, this marking should not 21617 * affect anything. It would be nice if only 1 message is 21618 * needed to inform IP that the IRE created for this RST should 21619 * not be added to the cache table. But there is currently 21620 * not such communication mechanism between TCP and IP. So 21621 * the best we can do now is to send the advice ioctl to IP 21622 * to mark the IRE temporary. 21623 */ 21624 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 21625 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21626 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21627 } 21628 } 21629 21630 /* 21631 * Initiate closedown sequence on an active connection. (May be called as 21632 * writer.) Return value zero for OK return, non-zero for error return. 21633 */ 21634 static int 21635 tcp_xmit_end(tcp_t *tcp) 21636 { 21637 ipic_t *ipic; 21638 mblk_t *mp; 21639 21640 if (tcp->tcp_state < TCPS_SYN_RCVD || 21641 tcp->tcp_state > TCPS_CLOSE_WAIT) { 21642 /* 21643 * Invalid state, only states TCPS_SYN_RCVD, 21644 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 21645 */ 21646 return (-1); 21647 } 21648 21649 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 21650 tcp->tcp_valid_bits |= TCP_FSS_VALID; 21651 /* 21652 * If there is nothing more unsent, send the FIN now. 21653 * Otherwise, it will go out with the last segment. 21654 */ 21655 if (tcp->tcp_unsent == 0) { 21656 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 21657 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 21658 21659 if (mp) { 21660 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21661 tcp_send_data(tcp, tcp->tcp_wq, mp); 21662 } else { 21663 /* 21664 * Couldn't allocate msg. Pretend we got it out. 21665 * Wait for rexmit timeout. 21666 */ 21667 tcp->tcp_snxt = tcp->tcp_fss + 1; 21668 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21669 } 21670 21671 /* 21672 * If needed, update tcp_rexmit_snxt as tcp_snxt is 21673 * changed. 21674 */ 21675 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 21676 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 21677 } 21678 } else { 21679 /* 21680 * If tcp->tcp_cork is set, then the data will not get sent, 21681 * so we have to check that and unset it first. 21682 */ 21683 if (tcp->tcp_cork) 21684 tcp->tcp_cork = B_FALSE; 21685 tcp_wput_data(tcp, NULL, B_FALSE); 21686 } 21687 21688 /* 21689 * If TCP does not get enough samples of RTT or tcp_rtt_updates 21690 * is 0, don't update the cache. 21691 */ 21692 if (tcp_rtt_updates == 0 || tcp->tcp_rtt_update < tcp_rtt_updates) 21693 return (0); 21694 21695 /* 21696 * NOTE: should not update if source routes i.e. if tcp_remote if 21697 * different from the destination. 21698 */ 21699 if (tcp->tcp_ipversion == IPV4_VERSION) { 21700 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 21701 return (0); 21702 } 21703 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21704 &ipic); 21705 } else { 21706 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 21707 &tcp->tcp_ip6h->ip6_dst))) { 21708 return (0); 21709 } 21710 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21711 &ipic); 21712 } 21713 21714 /* Record route attributes in the IRE for use by future connections. */ 21715 if (mp == NULL) 21716 return (0); 21717 21718 /* 21719 * We do not have a good algorithm to update ssthresh at this time. 21720 * So don't do any update. 21721 */ 21722 ipic->ipic_rtt = tcp->tcp_rtt_sa; 21723 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 21724 21725 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21726 return (0); 21727 } 21728 21729 /* 21730 * Generate a "no listener here" RST in response to an "unknown" segment. 21731 * Note that we are reusing the incoming mp to construct the outgoing 21732 * RST. 21733 */ 21734 void 21735 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len) 21736 { 21737 uchar_t *rptr; 21738 uint32_t seg_len; 21739 tcph_t *tcph; 21740 uint32_t seg_seq; 21741 uint32_t seg_ack; 21742 uint_t flags; 21743 mblk_t *ipsec_mp; 21744 ipha_t *ipha; 21745 ip6_t *ip6h; 21746 boolean_t mctl_present = B_FALSE; 21747 boolean_t check = B_TRUE; 21748 boolean_t policy_present; 21749 21750 TCP_STAT(tcp_no_listener); 21751 21752 ipsec_mp = mp; 21753 21754 if (mp->b_datap->db_type == M_CTL) { 21755 ipsec_in_t *ii; 21756 21757 mctl_present = B_TRUE; 21758 mp = mp->b_cont; 21759 21760 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21761 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21762 if (ii->ipsec_in_dont_check) { 21763 check = B_FALSE; 21764 if (!ii->ipsec_in_secure) { 21765 freeb(ipsec_mp); 21766 mctl_present = B_FALSE; 21767 ipsec_mp = mp; 21768 } 21769 } 21770 } 21771 21772 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21773 policy_present = ipsec_inbound_v4_policy_present; 21774 ipha = (ipha_t *)mp->b_rptr; 21775 ip6h = NULL; 21776 } else { 21777 policy_present = ipsec_inbound_v6_policy_present; 21778 ipha = NULL; 21779 ip6h = (ip6_t *)mp->b_rptr; 21780 } 21781 21782 if (check && policy_present) { 21783 /* 21784 * The conn_t parameter is NULL because we already know 21785 * nobody's home. 21786 */ 21787 ipsec_mp = ipsec_check_global_policy( 21788 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present); 21789 if (ipsec_mp == NULL) 21790 return; 21791 } 21792 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 21793 DTRACE_PROBE2( 21794 tx__ip__log__error__nolistener__tcp, 21795 char *, "Could not reply with RST to mp(1)", 21796 mblk_t *, mp); 21797 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 21798 freemsg(ipsec_mp); 21799 return; 21800 } 21801 21802 rptr = mp->b_rptr; 21803 21804 tcph = (tcph_t *)&rptr[ip_hdr_len]; 21805 seg_seq = BE32_TO_U32(tcph->th_seq); 21806 seg_ack = BE32_TO_U32(tcph->th_ack); 21807 flags = tcph->th_flags[0]; 21808 21809 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 21810 if (flags & TH_RST) { 21811 freemsg(ipsec_mp); 21812 } else if (flags & TH_ACK) { 21813 tcp_xmit_early_reset("no tcp, reset", 21814 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len); 21815 } else { 21816 if (flags & TH_SYN) { 21817 seg_len++; 21818 } else { 21819 /* 21820 * Here we violate the RFC. Note that a normal 21821 * TCP will never send a segment without the ACK 21822 * flag, except for RST or SYN segment. This 21823 * segment is neither. Just drop it on the 21824 * floor. 21825 */ 21826 freemsg(ipsec_mp); 21827 tcp_rst_unsent++; 21828 return; 21829 } 21830 21831 tcp_xmit_early_reset("no tcp, reset/ack", 21832 ipsec_mp, 0, seg_seq + seg_len, 21833 TH_RST | TH_ACK, ip_hdr_len); 21834 } 21835 } 21836 21837 /* 21838 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 21839 * ip and tcp header ready to pass down to IP. If the mp passed in is 21840 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 21841 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 21842 * otherwise it will dup partial mblks.) 21843 * Otherwise, an appropriate ACK packet will be generated. This 21844 * routine is not usually called to send new data for the first time. It 21845 * is mostly called out of the timer for retransmits, and to generate ACKs. 21846 * 21847 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 21848 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 21849 * of the original mblk chain will be returned in *offset and *end_mp. 21850 */ 21851 static mblk_t * 21852 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 21853 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 21854 boolean_t rexmit) 21855 { 21856 int data_length; 21857 int32_t off = 0; 21858 uint_t flags; 21859 mblk_t *mp1; 21860 mblk_t *mp2; 21861 uchar_t *rptr; 21862 tcph_t *tcph; 21863 int32_t num_sack_blk = 0; 21864 int32_t sack_opt_len = 0; 21865 21866 /* Allocate for our maximum TCP header + link-level */ 21867 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 21868 BPRI_MED); 21869 if (!mp1) 21870 return (NULL); 21871 data_length = 0; 21872 21873 /* 21874 * Note that tcp_mss has been adjusted to take into account the 21875 * timestamp option if applicable. Because SACK options do not 21876 * appear in every TCP segments and they are of variable lengths, 21877 * they cannot be included in tcp_mss. Thus we need to calculate 21878 * the actual segment length when we need to send a segment which 21879 * includes SACK options. 21880 */ 21881 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 21882 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 21883 tcp->tcp_num_sack_blk); 21884 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 21885 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 21886 if (max_to_send + sack_opt_len > tcp->tcp_mss) 21887 max_to_send -= sack_opt_len; 21888 } 21889 21890 if (offset != NULL) { 21891 off = *offset; 21892 /* We use offset as an indicator that end_mp is not NULL. */ 21893 *end_mp = NULL; 21894 } 21895 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 21896 /* This could be faster with cooperation from downstream */ 21897 if (mp2 != mp1 && !sendall && 21898 data_length + (int)(mp->b_wptr - mp->b_rptr) > 21899 max_to_send) 21900 /* 21901 * Don't send the next mblk since the whole mblk 21902 * does not fit. 21903 */ 21904 break; 21905 mp2->b_cont = dupb(mp); 21906 mp2 = mp2->b_cont; 21907 if (!mp2) { 21908 freemsg(mp1); 21909 return (NULL); 21910 } 21911 mp2->b_rptr += off; 21912 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 21913 (uintptr_t)INT_MAX); 21914 21915 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 21916 if (data_length > max_to_send) { 21917 mp2->b_wptr -= data_length - max_to_send; 21918 data_length = max_to_send; 21919 off = mp2->b_wptr - mp->b_rptr; 21920 break; 21921 } else { 21922 off = 0; 21923 } 21924 } 21925 if (offset != NULL) { 21926 *offset = off; 21927 *end_mp = mp; 21928 } 21929 if (seg_len != NULL) { 21930 *seg_len = data_length; 21931 } 21932 21933 /* Update the latest receive window size in TCP header. */ 21934 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21935 tcp->tcp_tcph->th_win); 21936 21937 rptr = mp1->b_rptr + tcp_wroff_xtra; 21938 mp1->b_rptr = rptr; 21939 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 21940 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 21941 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 21942 U32_TO_ABE32(seq, tcph->th_seq); 21943 21944 /* 21945 * Use tcp_unsent to determine if the PUSH bit should be used assumes 21946 * that this function was called from tcp_wput_data. Thus, when called 21947 * to retransmit data the setting of the PUSH bit may appear some 21948 * what random in that it might get set when it should not. This 21949 * should not pose any performance issues. 21950 */ 21951 if (data_length != 0 && (tcp->tcp_unsent == 0 || 21952 tcp->tcp_unsent == data_length)) { 21953 flags = TH_ACK | TH_PUSH; 21954 } else { 21955 flags = TH_ACK; 21956 } 21957 21958 if (tcp->tcp_ecn_ok) { 21959 if (tcp->tcp_ecn_echo_on) 21960 flags |= TH_ECE; 21961 21962 /* 21963 * Only set ECT bit and ECN_CWR if a segment contains new data. 21964 * There is no TCP flow control for non-data segments, and 21965 * only data segment is transmitted reliably. 21966 */ 21967 if (data_length > 0 && !rexmit) { 21968 SET_ECT(tcp, rptr); 21969 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 21970 flags |= TH_CWR; 21971 tcp->tcp_ecn_cwr_sent = B_TRUE; 21972 } 21973 } 21974 } 21975 21976 if (tcp->tcp_valid_bits) { 21977 uint32_t u1; 21978 21979 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 21980 seq == tcp->tcp_iss) { 21981 uchar_t *wptr; 21982 21983 /* 21984 * If TCP_ISS_VALID and the seq number is tcp_iss, 21985 * TCP can only be in SYN-SENT, SYN-RCVD or 21986 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 21987 * our SYN is not ack'ed but the app closes this 21988 * TCP connection. 21989 */ 21990 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 21991 tcp->tcp_state == TCPS_SYN_RCVD || 21992 tcp->tcp_state == TCPS_FIN_WAIT_1); 21993 21994 /* 21995 * Tack on the MSS option. It is always needed 21996 * for both active and passive open. 21997 * 21998 * MSS option value should be interface MTU - MIN 21999 * TCP/IP header according to RFC 793 as it means 22000 * the maximum segment size TCP can receive. But 22001 * to get around some broken middle boxes/end hosts 22002 * out there, we allow the option value to be the 22003 * same as the MSS option size on the peer side. 22004 * In this way, the other side will not send 22005 * anything larger than they can receive. 22006 * 22007 * Note that for SYN_SENT state, the ndd param 22008 * tcp_use_smss_as_mss_opt has no effect as we 22009 * don't know the peer's MSS option value. So 22010 * the only case we need to take care of is in 22011 * SYN_RCVD state, which is done later. 22012 */ 22013 wptr = mp1->b_wptr; 22014 wptr[0] = TCPOPT_MAXSEG; 22015 wptr[1] = TCPOPT_MAXSEG_LEN; 22016 wptr += 2; 22017 u1 = tcp->tcp_if_mtu - 22018 (tcp->tcp_ipversion == IPV4_VERSION ? 22019 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 22020 TCP_MIN_HEADER_LENGTH; 22021 U16_TO_BE16(u1, wptr); 22022 mp1->b_wptr = wptr + 2; 22023 /* Update the offset to cover the additional word */ 22024 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22025 22026 /* 22027 * Note that the following way of filling in 22028 * TCP options are not optimal. Some NOPs can 22029 * be saved. But there is no need at this time 22030 * to optimize it. When it is needed, we will 22031 * do it. 22032 */ 22033 switch (tcp->tcp_state) { 22034 case TCPS_SYN_SENT: 22035 flags = TH_SYN; 22036 22037 if (tcp->tcp_snd_ts_ok) { 22038 uint32_t llbolt = (uint32_t)lbolt; 22039 22040 wptr = mp1->b_wptr; 22041 wptr[0] = TCPOPT_NOP; 22042 wptr[1] = TCPOPT_NOP; 22043 wptr[2] = TCPOPT_TSTAMP; 22044 wptr[3] = TCPOPT_TSTAMP_LEN; 22045 wptr += 4; 22046 U32_TO_BE32(llbolt, wptr); 22047 wptr += 4; 22048 ASSERT(tcp->tcp_ts_recent == 0); 22049 U32_TO_BE32(0L, wptr); 22050 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 22051 tcph->th_offset_and_rsrvd[0] += 22052 (3 << 4); 22053 } 22054 22055 /* 22056 * Set up all the bits to tell other side 22057 * we are ECN capable. 22058 */ 22059 if (tcp->tcp_ecn_ok) { 22060 flags |= (TH_ECE | TH_CWR); 22061 } 22062 break; 22063 case TCPS_SYN_RCVD: 22064 flags |= TH_SYN; 22065 22066 /* 22067 * Reset the MSS option value to be SMSS 22068 * We should probably add back the bytes 22069 * for timestamp option and IPsec. We 22070 * don't do that as this is a workaround 22071 * for broken middle boxes/end hosts, it 22072 * is better for us to be more cautious. 22073 * They may not take these things into 22074 * account in their SMSS calculation. Thus 22075 * the peer's calculated SMSS may be smaller 22076 * than what it can be. This should be OK. 22077 */ 22078 if (tcp_use_smss_as_mss_opt) { 22079 u1 = tcp->tcp_mss; 22080 U16_TO_BE16(u1, wptr); 22081 } 22082 22083 /* 22084 * If the other side is ECN capable, reply 22085 * that we are also ECN capable. 22086 */ 22087 if (tcp->tcp_ecn_ok) 22088 flags |= TH_ECE; 22089 break; 22090 default: 22091 /* 22092 * The above ASSERT() makes sure that this 22093 * must be FIN-WAIT-1 state. Our SYN has 22094 * not been ack'ed so retransmit it. 22095 */ 22096 flags |= TH_SYN; 22097 break; 22098 } 22099 22100 if (tcp->tcp_snd_ws_ok) { 22101 wptr = mp1->b_wptr; 22102 wptr[0] = TCPOPT_NOP; 22103 wptr[1] = TCPOPT_WSCALE; 22104 wptr[2] = TCPOPT_WS_LEN; 22105 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 22106 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 22107 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22108 } 22109 22110 if (tcp->tcp_snd_sack_ok) { 22111 wptr = mp1->b_wptr; 22112 wptr[0] = TCPOPT_NOP; 22113 wptr[1] = TCPOPT_NOP; 22114 wptr[2] = TCPOPT_SACK_PERMITTED; 22115 wptr[3] = TCPOPT_SACK_OK_LEN; 22116 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 22117 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22118 } 22119 22120 /* allocb() of adequate mblk assures space */ 22121 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 22122 (uintptr_t)INT_MAX); 22123 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 22124 /* 22125 * Get IP set to checksum on our behalf 22126 * Include the adjustment for a source route if any. 22127 */ 22128 u1 += tcp->tcp_sum; 22129 u1 = (u1 >> 16) + (u1 & 0xFFFF); 22130 U16_TO_BE16(u1, tcph->th_sum); 22131 BUMP_MIB(&tcp_mib, tcpOutControl); 22132 } 22133 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 22134 (seq + data_length) == tcp->tcp_fss) { 22135 if (!tcp->tcp_fin_acked) { 22136 flags |= TH_FIN; 22137 BUMP_MIB(&tcp_mib, tcpOutControl); 22138 } 22139 if (!tcp->tcp_fin_sent) { 22140 tcp->tcp_fin_sent = B_TRUE; 22141 switch (tcp->tcp_state) { 22142 case TCPS_SYN_RCVD: 22143 case TCPS_ESTABLISHED: 22144 tcp->tcp_state = TCPS_FIN_WAIT_1; 22145 break; 22146 case TCPS_CLOSE_WAIT: 22147 tcp->tcp_state = TCPS_LAST_ACK; 22148 break; 22149 } 22150 if (tcp->tcp_suna == tcp->tcp_snxt) 22151 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22152 tcp->tcp_snxt = tcp->tcp_fss + 1; 22153 } 22154 } 22155 /* 22156 * Note the trick here. u1 is unsigned. When tcp_urg 22157 * is smaller than seq, u1 will become a very huge value. 22158 * So the comparison will fail. Also note that tcp_urp 22159 * should be positive, see RFC 793 page 17. 22160 */ 22161 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 22162 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 22163 u1 < (uint32_t)(64 * 1024)) { 22164 flags |= TH_URG; 22165 BUMP_MIB(&tcp_mib, tcpOutUrg); 22166 U32_TO_ABE16(u1, tcph->th_urp); 22167 } 22168 } 22169 tcph->th_flags[0] = (uchar_t)flags; 22170 tcp->tcp_rack = tcp->tcp_rnxt; 22171 tcp->tcp_rack_cnt = 0; 22172 22173 if (tcp->tcp_snd_ts_ok) { 22174 if (tcp->tcp_state != TCPS_SYN_SENT) { 22175 uint32_t llbolt = (uint32_t)lbolt; 22176 22177 U32_TO_BE32(llbolt, 22178 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22179 U32_TO_BE32(tcp->tcp_ts_recent, 22180 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22181 } 22182 } 22183 22184 if (num_sack_blk > 0) { 22185 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22186 sack_blk_t *tmp; 22187 int32_t i; 22188 22189 wptr[0] = TCPOPT_NOP; 22190 wptr[1] = TCPOPT_NOP; 22191 wptr[2] = TCPOPT_SACK; 22192 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22193 sizeof (sack_blk_t); 22194 wptr += TCPOPT_REAL_SACK_LEN; 22195 22196 tmp = tcp->tcp_sack_list; 22197 for (i = 0; i < num_sack_blk; i++) { 22198 U32_TO_BE32(tmp[i].begin, wptr); 22199 wptr += sizeof (tcp_seq); 22200 U32_TO_BE32(tmp[i].end, wptr); 22201 wptr += sizeof (tcp_seq); 22202 } 22203 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 22204 } 22205 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22206 data_length += (int)(mp1->b_wptr - rptr); 22207 if (tcp->tcp_ipversion == IPV4_VERSION) { 22208 ((ipha_t *)rptr)->ipha_length = htons(data_length); 22209 } else { 22210 ip6_t *ip6 = (ip6_t *)(rptr + 22211 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22212 sizeof (ip6i_t) : 0)); 22213 22214 ip6->ip6_plen = htons(data_length - 22215 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22216 } 22217 22218 /* 22219 * Prime pump for IP 22220 * Include the adjustment for a source route if any. 22221 */ 22222 data_length -= tcp->tcp_ip_hdr_len; 22223 data_length += tcp->tcp_sum; 22224 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22225 U16_TO_ABE16(data_length, tcph->th_sum); 22226 if (tcp->tcp_ip_forward_progress) { 22227 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22228 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22229 tcp->tcp_ip_forward_progress = B_FALSE; 22230 } 22231 return (mp1); 22232 } 22233 22234 /* This function handles the push timeout. */ 22235 void 22236 tcp_push_timer(void *arg) 22237 { 22238 conn_t *connp = (conn_t *)arg; 22239 tcp_t *tcp = connp->conn_tcp; 22240 22241 TCP_DBGSTAT(tcp_push_timer_cnt); 22242 22243 ASSERT(tcp->tcp_listener == NULL); 22244 22245 /* 22246 * We need to stop synchronous streams temporarily to prevent a race 22247 * with tcp_fuse_rrw() or tcp_fusion rinfop(). It is safe to access 22248 * tcp_rcv_list here because those entry points will return right 22249 * away when synchronous streams is stopped. 22250 */ 22251 TCP_FUSE_SYNCSTR_STOP(tcp); 22252 tcp->tcp_push_tid = 0; 22253 if ((tcp->tcp_rcv_list != NULL) && 22254 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 22255 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 22256 TCP_FUSE_SYNCSTR_RESUME(tcp); 22257 } 22258 22259 /* 22260 * This function handles delayed ACK timeout. 22261 */ 22262 static void 22263 tcp_ack_timer(void *arg) 22264 { 22265 conn_t *connp = (conn_t *)arg; 22266 tcp_t *tcp = connp->conn_tcp; 22267 mblk_t *mp; 22268 22269 TCP_DBGSTAT(tcp_ack_timer_cnt); 22270 22271 tcp->tcp_ack_tid = 0; 22272 22273 if (tcp->tcp_fused) 22274 return; 22275 22276 /* 22277 * Do not send ACK if there is no outstanding unack'ed data. 22278 */ 22279 if (tcp->tcp_rnxt == tcp->tcp_rack) { 22280 return; 22281 } 22282 22283 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 22284 /* 22285 * Make sure we don't allow deferred ACKs to result in 22286 * timer-based ACKing. If we have held off an ACK 22287 * when there was more than an mss here, and the timer 22288 * goes off, we have to worry about the possibility 22289 * that the sender isn't doing slow-start, or is out 22290 * of step with us for some other reason. We fall 22291 * permanently back in the direction of 22292 * ACK-every-other-packet as suggested in RFC 1122. 22293 */ 22294 if (tcp->tcp_rack_abs_max > 2) 22295 tcp->tcp_rack_abs_max--; 22296 tcp->tcp_rack_cur_max = 2; 22297 } 22298 mp = tcp_ack_mp(tcp); 22299 22300 if (mp != NULL) { 22301 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22302 BUMP_LOCAL(tcp->tcp_obsegs); 22303 BUMP_MIB(&tcp_mib, tcpOutAck); 22304 BUMP_MIB(&tcp_mib, tcpOutAckDelayed); 22305 tcp_send_data(tcp, tcp->tcp_wq, mp); 22306 } 22307 } 22308 22309 22310 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 22311 static mblk_t * 22312 tcp_ack_mp(tcp_t *tcp) 22313 { 22314 uint32_t seq_no; 22315 22316 /* 22317 * There are a few cases to be considered while setting the sequence no. 22318 * Essentially, we can come here while processing an unacceptable pkt 22319 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 22320 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 22321 * If we are here for a zero window probe, stick with suna. In all 22322 * other cases, we check if suna + swnd encompasses snxt and set 22323 * the sequence number to snxt, if so. If snxt falls outside the 22324 * window (the receiver probably shrunk its window), we will go with 22325 * suna + swnd, otherwise the sequence no will be unacceptable to the 22326 * receiver. 22327 */ 22328 if (tcp->tcp_zero_win_probe) { 22329 seq_no = tcp->tcp_suna; 22330 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 22331 ASSERT(tcp->tcp_swnd == 0); 22332 seq_no = tcp->tcp_snxt; 22333 } else { 22334 seq_no = SEQ_GT(tcp->tcp_snxt, 22335 (tcp->tcp_suna + tcp->tcp_swnd)) ? 22336 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 22337 } 22338 22339 if (tcp->tcp_valid_bits) { 22340 /* 22341 * For the complex case where we have to send some 22342 * controls (FIN or SYN), let tcp_xmit_mp do it. 22343 */ 22344 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 22345 NULL, B_FALSE)); 22346 } else { 22347 /* Generate a simple ACK */ 22348 int data_length; 22349 uchar_t *rptr; 22350 tcph_t *tcph; 22351 mblk_t *mp1; 22352 int32_t tcp_hdr_len; 22353 int32_t tcp_tcp_hdr_len; 22354 int32_t num_sack_blk = 0; 22355 int32_t sack_opt_len; 22356 22357 /* 22358 * Allocate space for TCP + IP headers 22359 * and link-level header 22360 */ 22361 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22362 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22363 tcp->tcp_num_sack_blk); 22364 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22365 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22366 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 22367 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 22368 } else { 22369 tcp_hdr_len = tcp->tcp_hdr_len; 22370 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 22371 } 22372 mp1 = allocb(tcp_hdr_len + tcp_wroff_xtra, BPRI_MED); 22373 if (!mp1) 22374 return (NULL); 22375 22376 /* Update the latest receive window size in TCP header. */ 22377 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22378 tcp->tcp_tcph->th_win); 22379 /* copy in prototype TCP + IP header */ 22380 rptr = mp1->b_rptr + tcp_wroff_xtra; 22381 mp1->b_rptr = rptr; 22382 mp1->b_wptr = rptr + tcp_hdr_len; 22383 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22384 22385 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22386 22387 /* Set the TCP sequence number. */ 22388 U32_TO_ABE32(seq_no, tcph->th_seq); 22389 22390 /* Set up the TCP flag field. */ 22391 tcph->th_flags[0] = (uchar_t)TH_ACK; 22392 if (tcp->tcp_ecn_echo_on) 22393 tcph->th_flags[0] |= TH_ECE; 22394 22395 tcp->tcp_rack = tcp->tcp_rnxt; 22396 tcp->tcp_rack_cnt = 0; 22397 22398 /* fill in timestamp option if in use */ 22399 if (tcp->tcp_snd_ts_ok) { 22400 uint32_t llbolt = (uint32_t)lbolt; 22401 22402 U32_TO_BE32(llbolt, 22403 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22404 U32_TO_BE32(tcp->tcp_ts_recent, 22405 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22406 } 22407 22408 /* Fill in SACK options */ 22409 if (num_sack_blk > 0) { 22410 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22411 sack_blk_t *tmp; 22412 int32_t i; 22413 22414 wptr[0] = TCPOPT_NOP; 22415 wptr[1] = TCPOPT_NOP; 22416 wptr[2] = TCPOPT_SACK; 22417 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22418 sizeof (sack_blk_t); 22419 wptr += TCPOPT_REAL_SACK_LEN; 22420 22421 tmp = tcp->tcp_sack_list; 22422 for (i = 0; i < num_sack_blk; i++) { 22423 U32_TO_BE32(tmp[i].begin, wptr); 22424 wptr += sizeof (tcp_seq); 22425 U32_TO_BE32(tmp[i].end, wptr); 22426 wptr += sizeof (tcp_seq); 22427 } 22428 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 22429 << 4); 22430 } 22431 22432 if (tcp->tcp_ipversion == IPV4_VERSION) { 22433 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 22434 } else { 22435 /* Check for ip6i_t header in sticky hdrs */ 22436 ip6_t *ip6 = (ip6_t *)(rptr + 22437 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22438 sizeof (ip6i_t) : 0)); 22439 22440 ip6->ip6_plen = htons(tcp_hdr_len - 22441 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22442 } 22443 22444 /* 22445 * Prime pump for checksum calculation in IP. Include the 22446 * adjustment for a source route if any. 22447 */ 22448 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 22449 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22450 U16_TO_ABE16(data_length, tcph->th_sum); 22451 22452 if (tcp->tcp_ip_forward_progress) { 22453 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22454 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22455 tcp->tcp_ip_forward_progress = B_FALSE; 22456 } 22457 return (mp1); 22458 } 22459 } 22460 22461 /* 22462 * To create a temporary tcp structure for inserting into bind hash list. 22463 * The parameter is assumed to be in network byte order, ready for use. 22464 */ 22465 /* ARGSUSED */ 22466 static tcp_t * 22467 tcp_alloc_temp_tcp(in_port_t port) 22468 { 22469 conn_t *connp; 22470 tcp_t *tcp; 22471 22472 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP); 22473 if (connp == NULL) 22474 return (NULL); 22475 22476 tcp = connp->conn_tcp; 22477 22478 /* 22479 * Only initialize the necessary info in those structures. Note 22480 * that since INADDR_ANY is all 0, we do not need to set 22481 * tcp_bound_source to INADDR_ANY here. 22482 */ 22483 tcp->tcp_state = TCPS_BOUND; 22484 tcp->tcp_lport = port; 22485 tcp->tcp_exclbind = 1; 22486 tcp->tcp_reserved_port = 1; 22487 22488 /* Just for place holding... */ 22489 tcp->tcp_ipversion = IPV4_VERSION; 22490 22491 return (tcp); 22492 } 22493 22494 /* 22495 * To remove a port range specified by lo_port and hi_port from the 22496 * reserved port ranges. This is one of the three public functions of 22497 * the reserved port interface. Note that a port range has to be removed 22498 * as a whole. Ports in a range cannot be removed individually. 22499 * 22500 * Params: 22501 * in_port_t lo_port: the beginning port of the reserved port range to 22502 * be deleted. 22503 * in_port_t hi_port: the ending port of the reserved port range to 22504 * be deleted. 22505 * 22506 * Return: 22507 * B_TRUE if the deletion is successful, B_FALSE otherwise. 22508 */ 22509 boolean_t 22510 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 22511 { 22512 int i, j; 22513 int size; 22514 tcp_t **temp_tcp_array; 22515 tcp_t *tcp; 22516 22517 rw_enter(&tcp_reserved_port_lock, RW_WRITER); 22518 22519 /* First make sure that the port ranage is indeed reserved. */ 22520 for (i = 0; i < tcp_reserved_port_array_size; i++) { 22521 if (tcp_reserved_port[i].lo_port == lo_port) { 22522 hi_port = tcp_reserved_port[i].hi_port; 22523 temp_tcp_array = tcp_reserved_port[i].temp_tcp_array; 22524 break; 22525 } 22526 } 22527 if (i == tcp_reserved_port_array_size) { 22528 rw_exit(&tcp_reserved_port_lock); 22529 return (B_FALSE); 22530 } 22531 22532 /* 22533 * Remove the range from the array. This simple loop is possible 22534 * because port ranges are inserted in ascending order. 22535 */ 22536 for (j = i; j < tcp_reserved_port_array_size - 1; j++) { 22537 tcp_reserved_port[j].lo_port = tcp_reserved_port[j+1].lo_port; 22538 tcp_reserved_port[j].hi_port = tcp_reserved_port[j+1].hi_port; 22539 tcp_reserved_port[j].temp_tcp_array = 22540 tcp_reserved_port[j+1].temp_tcp_array; 22541 } 22542 22543 /* Remove all the temporary tcp structures. */ 22544 size = hi_port - lo_port + 1; 22545 while (size > 0) { 22546 tcp = temp_tcp_array[size - 1]; 22547 ASSERT(tcp != NULL); 22548 tcp_bind_hash_remove(tcp); 22549 CONN_DEC_REF(tcp->tcp_connp); 22550 size--; 22551 } 22552 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 22553 tcp_reserved_port_array_size--; 22554 rw_exit(&tcp_reserved_port_lock); 22555 return (B_TRUE); 22556 } 22557 22558 /* 22559 * Macro to remove temporary tcp structure from the bind hash list. The 22560 * first parameter is the list of tcp to be removed. The second parameter 22561 * is the number of tcps in the array. 22562 */ 22563 #define TCP_TMP_TCP_REMOVE(tcp_array, num) \ 22564 { \ 22565 while ((num) > 0) { \ 22566 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 22567 tf_t *tbf; \ 22568 tcp_t *tcpnext; \ 22569 tbf = &tcp_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 22570 mutex_enter(&tbf->tf_lock); \ 22571 tcpnext = tcp->tcp_bind_hash; \ 22572 if (tcpnext) { \ 22573 tcpnext->tcp_ptpbhn = \ 22574 tcp->tcp_ptpbhn; \ 22575 } \ 22576 *tcp->tcp_ptpbhn = tcpnext; \ 22577 mutex_exit(&tbf->tf_lock); \ 22578 kmem_free(tcp, sizeof (tcp_t)); \ 22579 (tcp_array)[(num) - 1] = NULL; \ 22580 (num)--; \ 22581 } \ 22582 } 22583 22584 /* 22585 * The public interface for other modules to call to reserve a port range 22586 * in TCP. The caller passes in how large a port range it wants. TCP 22587 * will try to find a range and return it via lo_port and hi_port. This is 22588 * used by NCA's nca_conn_init. 22589 * NCA can only be used in the global zone so this only affects the global 22590 * zone's ports. 22591 * 22592 * Params: 22593 * int size: the size of the port range to be reserved. 22594 * in_port_t *lo_port (referenced): returns the beginning port of the 22595 * reserved port range added. 22596 * in_port_t *hi_port (referenced): returns the ending port of the 22597 * reserved port range added. 22598 * 22599 * Return: 22600 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 22601 */ 22602 boolean_t 22603 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 22604 { 22605 tcp_t *tcp; 22606 tcp_t *tmp_tcp; 22607 tcp_t **temp_tcp_array; 22608 tf_t *tbf; 22609 in_port_t net_port; 22610 in_port_t port; 22611 int32_t cur_size; 22612 int i, j; 22613 boolean_t used; 22614 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 22615 zoneid_t zoneid = GLOBAL_ZONEID; 22616 22617 /* Sanity check. */ 22618 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 22619 return (B_FALSE); 22620 } 22621 22622 rw_enter(&tcp_reserved_port_lock, RW_WRITER); 22623 if (tcp_reserved_port_array_size == TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 22624 rw_exit(&tcp_reserved_port_lock); 22625 return (B_FALSE); 22626 } 22627 22628 /* 22629 * Find the starting port to try. Since the port ranges are ordered 22630 * in the reserved port array, we can do a simple search here. 22631 */ 22632 *lo_port = TCP_SMALLEST_RESERVED_PORT; 22633 *hi_port = TCP_LARGEST_RESERVED_PORT; 22634 for (i = 0; i < tcp_reserved_port_array_size; 22635 *lo_port = tcp_reserved_port[i].hi_port + 1, i++) { 22636 if (tcp_reserved_port[i].lo_port - *lo_port >= size) { 22637 *hi_port = tcp_reserved_port[i].lo_port - 1; 22638 break; 22639 } 22640 } 22641 /* No available port range. */ 22642 if (i == tcp_reserved_port_array_size && *hi_port - *lo_port < size) { 22643 rw_exit(&tcp_reserved_port_lock); 22644 return (B_FALSE); 22645 } 22646 22647 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 22648 if (temp_tcp_array == NULL) { 22649 rw_exit(&tcp_reserved_port_lock); 22650 return (B_FALSE); 22651 } 22652 22653 /* Go thru the port range to see if some ports are already bound. */ 22654 for (port = *lo_port, cur_size = 0; 22655 cur_size < size && port <= *hi_port; 22656 cur_size++, port++) { 22657 used = B_FALSE; 22658 net_port = htons(port); 22659 tbf = &tcp_bind_fanout[TCP_BIND_HASH(net_port)]; 22660 mutex_enter(&tbf->tf_lock); 22661 for (tcp = tbf->tf_tcp; tcp != NULL; 22662 tcp = tcp->tcp_bind_hash) { 22663 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 22664 net_port == tcp->tcp_lport) { 22665 /* 22666 * A port is already bound. Search again 22667 * starting from port + 1. Release all 22668 * temporary tcps. 22669 */ 22670 mutex_exit(&tbf->tf_lock); 22671 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 22672 *lo_port = port + 1; 22673 cur_size = -1; 22674 used = B_TRUE; 22675 break; 22676 } 22677 } 22678 if (!used) { 22679 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port)) == NULL) { 22680 /* 22681 * Allocation failure. Just fail the request. 22682 * Need to remove all those temporary tcp 22683 * structures. 22684 */ 22685 mutex_exit(&tbf->tf_lock); 22686 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 22687 rw_exit(&tcp_reserved_port_lock); 22688 kmem_free(temp_tcp_array, 22689 (hi_port - lo_port + 1) * 22690 sizeof (tcp_t *)); 22691 return (B_FALSE); 22692 } 22693 temp_tcp_array[cur_size] = tmp_tcp; 22694 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 22695 mutex_exit(&tbf->tf_lock); 22696 } 22697 } 22698 22699 /* 22700 * The current range is not large enough. We can actually do another 22701 * search if this search is done between 2 reserved port ranges. But 22702 * for first release, we just stop here and return saying that no port 22703 * range is available. 22704 */ 22705 if (cur_size < size) { 22706 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 22707 rw_exit(&tcp_reserved_port_lock); 22708 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 22709 return (B_FALSE); 22710 } 22711 *hi_port = port - 1; 22712 22713 /* 22714 * Insert range into array in ascending order. Since this function 22715 * must not be called often, we choose to use the simplest method. 22716 * The above array should not consume excessive stack space as 22717 * the size must be very small. If in future releases, we find 22718 * that we should provide more reserved port ranges, this function 22719 * has to be modified to be more efficient. 22720 */ 22721 if (tcp_reserved_port_array_size == 0) { 22722 tcp_reserved_port[0].lo_port = *lo_port; 22723 tcp_reserved_port[0].hi_port = *hi_port; 22724 tcp_reserved_port[0].temp_tcp_array = temp_tcp_array; 22725 } else { 22726 for (i = 0, j = 0; i < tcp_reserved_port_array_size; i++, j++) { 22727 if (*lo_port < tcp_reserved_port[i].lo_port && i == j) { 22728 tmp_ports[j].lo_port = *lo_port; 22729 tmp_ports[j].hi_port = *hi_port; 22730 tmp_ports[j].temp_tcp_array = temp_tcp_array; 22731 j++; 22732 } 22733 tmp_ports[j].lo_port = tcp_reserved_port[i].lo_port; 22734 tmp_ports[j].hi_port = tcp_reserved_port[i].hi_port; 22735 tmp_ports[j].temp_tcp_array = 22736 tcp_reserved_port[i].temp_tcp_array; 22737 } 22738 if (j == i) { 22739 tmp_ports[j].lo_port = *lo_port; 22740 tmp_ports[j].hi_port = *hi_port; 22741 tmp_ports[j].temp_tcp_array = temp_tcp_array; 22742 } 22743 bcopy(tmp_ports, tcp_reserved_port, sizeof (tmp_ports)); 22744 } 22745 tcp_reserved_port_array_size++; 22746 rw_exit(&tcp_reserved_port_lock); 22747 return (B_TRUE); 22748 } 22749 22750 /* 22751 * Check to see if a port is in any reserved port range. 22752 * 22753 * Params: 22754 * in_port_t port: the port to be verified. 22755 * 22756 * Return: 22757 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 22758 */ 22759 boolean_t 22760 tcp_reserved_port_check(in_port_t port) 22761 { 22762 int i; 22763 22764 rw_enter(&tcp_reserved_port_lock, RW_READER); 22765 for (i = 0; i < tcp_reserved_port_array_size; i++) { 22766 if (port >= tcp_reserved_port[i].lo_port || 22767 port <= tcp_reserved_port[i].hi_port) { 22768 rw_exit(&tcp_reserved_port_lock); 22769 return (B_TRUE); 22770 } 22771 } 22772 rw_exit(&tcp_reserved_port_lock); 22773 return (B_FALSE); 22774 } 22775 22776 /* 22777 * To list all reserved port ranges. This is the function to handle 22778 * ndd tcp_reserved_port_list. 22779 */ 22780 /* ARGSUSED */ 22781 static int 22782 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 22783 { 22784 int i; 22785 22786 rw_enter(&tcp_reserved_port_lock, RW_READER); 22787 if (tcp_reserved_port_array_size > 0) 22788 (void) mi_mpprintf(mp, "The following ports are reserved:"); 22789 else 22790 (void) mi_mpprintf(mp, "No port is reserved."); 22791 for (i = 0; i < tcp_reserved_port_array_size; i++) { 22792 (void) mi_mpprintf(mp, "%d-%d", 22793 tcp_reserved_port[i].lo_port, tcp_reserved_port[i].hi_port); 22794 } 22795 rw_exit(&tcp_reserved_port_lock); 22796 return (0); 22797 } 22798 22799 /* 22800 * Hash list insertion routine for tcp_t structures. 22801 * Inserts entries with the ones bound to a specific IP address first 22802 * followed by those bound to INADDR_ANY. 22803 */ 22804 static void 22805 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 22806 { 22807 tcp_t **tcpp; 22808 tcp_t *tcpnext; 22809 22810 if (tcp->tcp_ptpbhn != NULL) { 22811 ASSERT(!caller_holds_lock); 22812 tcp_bind_hash_remove(tcp); 22813 } 22814 tcpp = &tbf->tf_tcp; 22815 if (!caller_holds_lock) { 22816 mutex_enter(&tbf->tf_lock); 22817 } else { 22818 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 22819 } 22820 tcpnext = tcpp[0]; 22821 if (tcpnext) { 22822 /* 22823 * If the new tcp bound to the INADDR_ANY address 22824 * and the first one in the list is not bound to 22825 * INADDR_ANY we skip all entries until we find the 22826 * first one bound to INADDR_ANY. 22827 * This makes sure that applications binding to a 22828 * specific address get preference over those binding to 22829 * INADDR_ANY. 22830 */ 22831 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 22832 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 22833 while ((tcpnext = tcpp[0]) != NULL && 22834 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 22835 tcpp = &(tcpnext->tcp_bind_hash); 22836 if (tcpnext) 22837 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 22838 } else 22839 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 22840 } 22841 tcp->tcp_bind_hash = tcpnext; 22842 tcp->tcp_ptpbhn = tcpp; 22843 tcpp[0] = tcp; 22844 if (!caller_holds_lock) 22845 mutex_exit(&tbf->tf_lock); 22846 } 22847 22848 /* 22849 * Hash list removal routine for tcp_t structures. 22850 */ 22851 static void 22852 tcp_bind_hash_remove(tcp_t *tcp) 22853 { 22854 tcp_t *tcpnext; 22855 kmutex_t *lockp; 22856 22857 if (tcp->tcp_ptpbhn == NULL) 22858 return; 22859 22860 /* 22861 * Extract the lock pointer in case there are concurrent 22862 * hash_remove's for this instance. 22863 */ 22864 ASSERT(tcp->tcp_lport != 0); 22865 lockp = &tcp_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 22866 22867 ASSERT(lockp != NULL); 22868 mutex_enter(lockp); 22869 if (tcp->tcp_ptpbhn) { 22870 tcpnext = tcp->tcp_bind_hash; 22871 if (tcpnext) { 22872 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 22873 tcp->tcp_bind_hash = NULL; 22874 } 22875 *tcp->tcp_ptpbhn = tcpnext; 22876 tcp->tcp_ptpbhn = NULL; 22877 } 22878 mutex_exit(lockp); 22879 } 22880 22881 22882 /* 22883 * Hash list lookup routine for tcp_t structures. 22884 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 22885 */ 22886 static tcp_t * 22887 tcp_acceptor_hash_lookup(t_uscalar_t id) 22888 { 22889 tf_t *tf; 22890 tcp_t *tcp; 22891 22892 tf = &tcp_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 22893 mutex_enter(&tf->tf_lock); 22894 for (tcp = tf->tf_tcp; tcp != NULL; 22895 tcp = tcp->tcp_acceptor_hash) { 22896 if (tcp->tcp_acceptor_id == id) { 22897 CONN_INC_REF(tcp->tcp_connp); 22898 mutex_exit(&tf->tf_lock); 22899 return (tcp); 22900 } 22901 } 22902 mutex_exit(&tf->tf_lock); 22903 return (NULL); 22904 } 22905 22906 22907 /* 22908 * Hash list insertion routine for tcp_t structures. 22909 */ 22910 void 22911 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 22912 { 22913 tf_t *tf; 22914 tcp_t **tcpp; 22915 tcp_t *tcpnext; 22916 22917 tf = &tcp_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 22918 22919 if (tcp->tcp_ptpahn != NULL) 22920 tcp_acceptor_hash_remove(tcp); 22921 tcpp = &tf->tf_tcp; 22922 mutex_enter(&tf->tf_lock); 22923 tcpnext = tcpp[0]; 22924 if (tcpnext) 22925 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 22926 tcp->tcp_acceptor_hash = tcpnext; 22927 tcp->tcp_ptpahn = tcpp; 22928 tcpp[0] = tcp; 22929 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 22930 mutex_exit(&tf->tf_lock); 22931 } 22932 22933 /* 22934 * Hash list removal routine for tcp_t structures. 22935 */ 22936 static void 22937 tcp_acceptor_hash_remove(tcp_t *tcp) 22938 { 22939 tcp_t *tcpnext; 22940 kmutex_t *lockp; 22941 22942 /* 22943 * Extract the lock pointer in case there are concurrent 22944 * hash_remove's for this instance. 22945 */ 22946 lockp = tcp->tcp_acceptor_lockp; 22947 22948 if (tcp->tcp_ptpahn == NULL) 22949 return; 22950 22951 ASSERT(lockp != NULL); 22952 mutex_enter(lockp); 22953 if (tcp->tcp_ptpahn) { 22954 tcpnext = tcp->tcp_acceptor_hash; 22955 if (tcpnext) { 22956 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 22957 tcp->tcp_acceptor_hash = NULL; 22958 } 22959 *tcp->tcp_ptpahn = tcpnext; 22960 tcp->tcp_ptpahn = NULL; 22961 } 22962 mutex_exit(lockp); 22963 tcp->tcp_acceptor_lockp = NULL; 22964 } 22965 22966 /* ARGSUSED */ 22967 static int 22968 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 22969 { 22970 int error = 0; 22971 int retval; 22972 char *end; 22973 22974 tcp_hsp_t *hsp; 22975 tcp_hsp_t *hspprev; 22976 22977 ipaddr_t addr = 0; /* Address we're looking for */ 22978 in6_addr_t v6addr; /* Address we're looking for */ 22979 uint32_t hash; /* Hash of that address */ 22980 22981 /* 22982 * If the following variables are still zero after parsing the input 22983 * string, the user didn't specify them and we don't change them in 22984 * the HSP. 22985 */ 22986 22987 ipaddr_t mask = 0; /* Subnet mask */ 22988 in6_addr_t v6mask; 22989 long sendspace = 0; /* Send buffer size */ 22990 long recvspace = 0; /* Receive buffer size */ 22991 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 22992 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 22993 22994 rw_enter(&tcp_hsp_lock, RW_WRITER); 22995 22996 /* Parse and validate address */ 22997 if (af == AF_INET) { 22998 retval = inet_pton(af, value, &addr); 22999 if (retval == 1) 23000 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 23001 } else if (af == AF_INET6) { 23002 retval = inet_pton(af, value, &v6addr); 23003 } else { 23004 error = EINVAL; 23005 goto done; 23006 } 23007 if (retval == 0) { 23008 error = EINVAL; 23009 goto done; 23010 } 23011 23012 while ((*value) && *value != ' ') 23013 value++; 23014 23015 /* Parse individual keywords, set variables if found */ 23016 while (*value) { 23017 /* Skip leading blanks */ 23018 23019 while (*value == ' ' || *value == '\t') 23020 value++; 23021 23022 /* If at end of string, we're done */ 23023 23024 if (!*value) 23025 break; 23026 23027 /* We have a word, figure out what it is */ 23028 23029 if (strncmp("mask", value, 4) == 0) { 23030 value += 4; 23031 while (*value == ' ' || *value == '\t') 23032 value++; 23033 /* Parse subnet mask */ 23034 if (af == AF_INET) { 23035 retval = inet_pton(af, value, &mask); 23036 if (retval == 1) { 23037 V4MASK_TO_V6(mask, v6mask); 23038 } 23039 } else if (af == AF_INET6) { 23040 retval = inet_pton(af, value, &v6mask); 23041 } 23042 if (retval != 1) { 23043 error = EINVAL; 23044 goto done; 23045 } 23046 while ((*value) && *value != ' ') 23047 value++; 23048 } else if (strncmp("sendspace", value, 9) == 0) { 23049 value += 9; 23050 23051 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 23052 sendspace < TCP_XMIT_HIWATER || 23053 sendspace >= (1L<<30)) { 23054 error = EINVAL; 23055 goto done; 23056 } 23057 value = end; 23058 } else if (strncmp("recvspace", value, 9) == 0) { 23059 value += 9; 23060 23061 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 23062 recvspace < TCP_RECV_HIWATER || 23063 recvspace >= (1L<<30)) { 23064 error = EINVAL; 23065 goto done; 23066 } 23067 value = end; 23068 } else if (strncmp("timestamp", value, 9) == 0) { 23069 value += 9; 23070 23071 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 23072 timestamp < 0 || timestamp > 1) { 23073 error = EINVAL; 23074 goto done; 23075 } 23076 23077 /* 23078 * We increment timestamp so we know it's been set; 23079 * this is undone when we put it in the HSP 23080 */ 23081 timestamp++; 23082 value = end; 23083 } else if (strncmp("delete", value, 6) == 0) { 23084 value += 6; 23085 delete = B_TRUE; 23086 } else { 23087 error = EINVAL; 23088 goto done; 23089 } 23090 } 23091 23092 /* Hash address for lookup */ 23093 23094 hash = TCP_HSP_HASH(addr); 23095 23096 if (delete) { 23097 /* 23098 * Note that deletes don't return an error if the thing 23099 * we're trying to delete isn't there. 23100 */ 23101 if (tcp_hsp_hash == NULL) 23102 goto done; 23103 hsp = tcp_hsp_hash[hash]; 23104 23105 if (hsp) { 23106 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 23107 &v6addr)) { 23108 tcp_hsp_hash[hash] = hsp->tcp_hsp_next; 23109 mi_free((char *)hsp); 23110 } else { 23111 hspprev = hsp; 23112 while ((hsp = hsp->tcp_hsp_next) != NULL) { 23113 if (IN6_ARE_ADDR_EQUAL( 23114 &hsp->tcp_hsp_addr_v6, &v6addr)) { 23115 hspprev->tcp_hsp_next = 23116 hsp->tcp_hsp_next; 23117 mi_free((char *)hsp); 23118 break; 23119 } 23120 hspprev = hsp; 23121 } 23122 } 23123 } 23124 } else { 23125 /* 23126 * We're adding/modifying an HSP. If we haven't already done 23127 * so, allocate the hash table. 23128 */ 23129 23130 if (!tcp_hsp_hash) { 23131 tcp_hsp_hash = (tcp_hsp_t **) 23132 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 23133 if (!tcp_hsp_hash) { 23134 error = EINVAL; 23135 goto done; 23136 } 23137 } 23138 23139 /* Get head of hash chain */ 23140 23141 hsp = tcp_hsp_hash[hash]; 23142 23143 /* Try to find pre-existing hsp on hash chain */ 23144 /* Doesn't handle CIDR prefixes. */ 23145 while (hsp) { 23146 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 23147 break; 23148 hsp = hsp->tcp_hsp_next; 23149 } 23150 23151 /* 23152 * If we didn't, create one with default values and put it 23153 * at head of hash chain 23154 */ 23155 23156 if (!hsp) { 23157 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 23158 if (!hsp) { 23159 error = EINVAL; 23160 goto done; 23161 } 23162 hsp->tcp_hsp_next = tcp_hsp_hash[hash]; 23163 tcp_hsp_hash[hash] = hsp; 23164 } 23165 23166 /* Set values that the user asked us to change */ 23167 23168 hsp->tcp_hsp_addr_v6 = v6addr; 23169 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 23170 hsp->tcp_hsp_vers = IPV4_VERSION; 23171 else 23172 hsp->tcp_hsp_vers = IPV6_VERSION; 23173 hsp->tcp_hsp_subnet_v6 = v6mask; 23174 if (sendspace > 0) 23175 hsp->tcp_hsp_sendspace = sendspace; 23176 if (recvspace > 0) 23177 hsp->tcp_hsp_recvspace = recvspace; 23178 if (timestamp > 0) 23179 hsp->tcp_hsp_tstamp = timestamp - 1; 23180 } 23181 23182 done: 23183 rw_exit(&tcp_hsp_lock); 23184 return (error); 23185 } 23186 23187 /* Set callback routine passed to nd_load by tcp_param_register. */ 23188 /* ARGSUSED */ 23189 static int 23190 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 23191 { 23192 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 23193 } 23194 /* ARGSUSED */ 23195 static int 23196 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23197 cred_t *cr) 23198 { 23199 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 23200 } 23201 23202 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 23203 /* ARGSUSED */ 23204 static int 23205 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23206 { 23207 tcp_hsp_t *hsp; 23208 int i; 23209 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 23210 23211 rw_enter(&tcp_hsp_lock, RW_READER); 23212 (void) mi_mpprintf(mp, 23213 "Hash HSP " MI_COL_HDRPAD_STR 23214 "Address Subnet Mask Send Receive TStamp"); 23215 if (tcp_hsp_hash) { 23216 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 23217 hsp = tcp_hsp_hash[i]; 23218 while (hsp) { 23219 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 23220 (void) inet_ntop(AF_INET, 23221 &hsp->tcp_hsp_addr, 23222 addrbuf, sizeof (addrbuf)); 23223 (void) inet_ntop(AF_INET, 23224 &hsp->tcp_hsp_subnet, 23225 subnetbuf, sizeof (subnetbuf)); 23226 } else { 23227 (void) inet_ntop(AF_INET6, 23228 &hsp->tcp_hsp_addr_v6, 23229 addrbuf, sizeof (addrbuf)); 23230 (void) inet_ntop(AF_INET6, 23231 &hsp->tcp_hsp_subnet_v6, 23232 subnetbuf, sizeof (subnetbuf)); 23233 } 23234 (void) mi_mpprintf(mp, 23235 " %03d " MI_COL_PTRFMT_STR 23236 "%s %s %010d %010d %d", 23237 i, 23238 (void *)hsp, 23239 addrbuf, 23240 subnetbuf, 23241 hsp->tcp_hsp_sendspace, 23242 hsp->tcp_hsp_recvspace, 23243 hsp->tcp_hsp_tstamp); 23244 23245 hsp = hsp->tcp_hsp_next; 23246 } 23247 } 23248 } 23249 rw_exit(&tcp_hsp_lock); 23250 return (0); 23251 } 23252 23253 23254 /* Data for fast netmask macro used by tcp_hsp_lookup */ 23255 23256 static ipaddr_t netmasks[] = { 23257 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 23258 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 23259 }; 23260 23261 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 23262 23263 /* 23264 * XXX This routine should go away and instead we should use the metrics 23265 * associated with the routes to determine the default sndspace and rcvspace. 23266 */ 23267 static tcp_hsp_t * 23268 tcp_hsp_lookup(ipaddr_t addr) 23269 { 23270 tcp_hsp_t *hsp = NULL; 23271 23272 /* Quick check without acquiring the lock. */ 23273 if (tcp_hsp_hash == NULL) 23274 return (NULL); 23275 23276 rw_enter(&tcp_hsp_lock, RW_READER); 23277 23278 /* This routine finds the best-matching HSP for address addr. */ 23279 23280 if (tcp_hsp_hash) { 23281 int i; 23282 ipaddr_t srchaddr; 23283 tcp_hsp_t *hsp_net; 23284 23285 /* We do three passes: host, network, and subnet. */ 23286 23287 srchaddr = addr; 23288 23289 for (i = 1; i <= 3; i++) { 23290 /* Look for exact match on srchaddr */ 23291 23292 hsp = tcp_hsp_hash[TCP_HSP_HASH(srchaddr)]; 23293 while (hsp) { 23294 if (hsp->tcp_hsp_vers == IPV4_VERSION && 23295 hsp->tcp_hsp_addr == srchaddr) 23296 break; 23297 hsp = hsp->tcp_hsp_next; 23298 } 23299 ASSERT(hsp == NULL || 23300 hsp->tcp_hsp_vers == IPV4_VERSION); 23301 23302 /* 23303 * If this is the first pass: 23304 * If we found a match, great, return it. 23305 * If not, search for the network on the second pass. 23306 */ 23307 23308 if (i == 1) 23309 if (hsp) 23310 break; 23311 else 23312 { 23313 srchaddr = addr & netmask(addr); 23314 continue; 23315 } 23316 23317 /* 23318 * If this is the second pass: 23319 * If we found a match, but there's a subnet mask, 23320 * save the match but try again using the subnet 23321 * mask on the third pass. 23322 * Otherwise, return whatever we found. 23323 */ 23324 23325 if (i == 2) { 23326 if (hsp && hsp->tcp_hsp_subnet) { 23327 hsp_net = hsp; 23328 srchaddr = addr & hsp->tcp_hsp_subnet; 23329 continue; 23330 } else { 23331 break; 23332 } 23333 } 23334 23335 /* 23336 * This must be the third pass. If we didn't find 23337 * anything, return the saved network HSP instead. 23338 */ 23339 23340 if (!hsp) 23341 hsp = hsp_net; 23342 } 23343 } 23344 23345 rw_exit(&tcp_hsp_lock); 23346 return (hsp); 23347 } 23348 23349 /* 23350 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 23351 * match lookup. 23352 */ 23353 static tcp_hsp_t * 23354 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr) 23355 { 23356 tcp_hsp_t *hsp = NULL; 23357 23358 /* Quick check without acquiring the lock. */ 23359 if (tcp_hsp_hash == NULL) 23360 return (NULL); 23361 23362 rw_enter(&tcp_hsp_lock, RW_READER); 23363 23364 /* This routine finds the best-matching HSP for address addr. */ 23365 23366 if (tcp_hsp_hash) { 23367 int i; 23368 in6_addr_t v6srchaddr; 23369 tcp_hsp_t *hsp_net; 23370 23371 /* We do three passes: host, network, and subnet. */ 23372 23373 v6srchaddr = *v6addr; 23374 23375 for (i = 1; i <= 3; i++) { 23376 /* Look for exact match on srchaddr */ 23377 23378 hsp = tcp_hsp_hash[TCP_HSP_HASH( 23379 V4_PART_OF_V6(v6srchaddr))]; 23380 while (hsp) { 23381 if (hsp->tcp_hsp_vers == IPV6_VERSION && 23382 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 23383 &v6srchaddr)) 23384 break; 23385 hsp = hsp->tcp_hsp_next; 23386 } 23387 23388 /* 23389 * If this is the first pass: 23390 * If we found a match, great, return it. 23391 * If not, search for the network on the second pass. 23392 */ 23393 23394 if (i == 1) 23395 if (hsp) 23396 break; 23397 else { 23398 /* Assume a 64 bit mask */ 23399 v6srchaddr.s6_addr32[0] = 23400 v6addr->s6_addr32[0]; 23401 v6srchaddr.s6_addr32[1] = 23402 v6addr->s6_addr32[1]; 23403 v6srchaddr.s6_addr32[2] = 0; 23404 v6srchaddr.s6_addr32[3] = 0; 23405 continue; 23406 } 23407 23408 /* 23409 * If this is the second pass: 23410 * If we found a match, but there's a subnet mask, 23411 * save the match but try again using the subnet 23412 * mask on the third pass. 23413 * Otherwise, return whatever we found. 23414 */ 23415 23416 if (i == 2) { 23417 ASSERT(hsp == NULL || 23418 hsp->tcp_hsp_vers == IPV6_VERSION); 23419 if (hsp && 23420 !IN6_IS_ADDR_UNSPECIFIED( 23421 &hsp->tcp_hsp_subnet_v6)) { 23422 hsp_net = hsp; 23423 V6_MASK_COPY(*v6addr, 23424 hsp->tcp_hsp_subnet_v6, v6srchaddr); 23425 continue; 23426 } else { 23427 break; 23428 } 23429 } 23430 23431 /* 23432 * This must be the third pass. If we didn't find 23433 * anything, return the saved network HSP instead. 23434 */ 23435 23436 if (!hsp) 23437 hsp = hsp_net; 23438 } 23439 } 23440 23441 rw_exit(&tcp_hsp_lock); 23442 return (hsp); 23443 } 23444 23445 /* 23446 * Type three generator adapted from the random() function in 4.4 BSD: 23447 */ 23448 23449 /* 23450 * Copyright (c) 1983, 1993 23451 * The Regents of the University of California. All rights reserved. 23452 * 23453 * Redistribution and use in source and binary forms, with or without 23454 * modification, are permitted provided that the following conditions 23455 * are met: 23456 * 1. Redistributions of source code must retain the above copyright 23457 * notice, this list of conditions and the following disclaimer. 23458 * 2. Redistributions in binary form must reproduce the above copyright 23459 * notice, this list of conditions and the following disclaimer in the 23460 * documentation and/or other materials provided with the distribution. 23461 * 3. All advertising materials mentioning features or use of this software 23462 * must display the following acknowledgement: 23463 * This product includes software developed by the University of 23464 * California, Berkeley and its contributors. 23465 * 4. Neither the name of the University nor the names of its contributors 23466 * may be used to endorse or promote products derived from this software 23467 * without specific prior written permission. 23468 * 23469 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23470 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23471 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23472 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23473 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23474 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23475 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23476 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23477 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23478 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23479 * SUCH DAMAGE. 23480 */ 23481 23482 /* Type 3 -- x**31 + x**3 + 1 */ 23483 #define DEG_3 31 23484 #define SEP_3 3 23485 23486 23487 /* Protected by tcp_random_lock */ 23488 static int tcp_randtbl[DEG_3 + 1]; 23489 23490 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23491 static int *tcp_random_rptr = &tcp_randtbl[1]; 23492 23493 static int *tcp_random_state = &tcp_randtbl[1]; 23494 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23495 23496 kmutex_t tcp_random_lock; 23497 23498 void 23499 tcp_random_init(void) 23500 { 23501 int i; 23502 hrtime_t hrt; 23503 time_t wallclock; 23504 uint64_t result; 23505 23506 /* 23507 * Use high-res timer and current time for seed. Gethrtime() returns 23508 * a longlong, which may contain resolution down to nanoseconds. 23509 * The current time will either be a 32-bit or a 64-bit quantity. 23510 * XOR the two together in a 64-bit result variable. 23511 * Convert the result to a 32-bit value by multiplying the high-order 23512 * 32-bits by the low-order 32-bits. 23513 */ 23514 23515 hrt = gethrtime(); 23516 (void) drv_getparm(TIME, &wallclock); 23517 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23518 mutex_enter(&tcp_random_lock); 23519 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23520 (result & 0xffffffff); 23521 23522 for (i = 1; i < DEG_3; i++) 23523 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23524 + 12345; 23525 tcp_random_fptr = &tcp_random_state[SEP_3]; 23526 tcp_random_rptr = &tcp_random_state[0]; 23527 mutex_exit(&tcp_random_lock); 23528 for (i = 0; i < 10 * DEG_3; i++) 23529 (void) tcp_random(); 23530 } 23531 23532 /* 23533 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23534 * This range is selected to be approximately centered on TCP_ISS / 2, 23535 * and easy to compute. We get this value by generating a 32-bit random 23536 * number, selecting out the high-order 17 bits, and then adding one so 23537 * that we never return zero. 23538 */ 23539 int 23540 tcp_random(void) 23541 { 23542 int i; 23543 23544 mutex_enter(&tcp_random_lock); 23545 *tcp_random_fptr += *tcp_random_rptr; 23546 23547 /* 23548 * The high-order bits are more random than the low-order bits, 23549 * so we select out the high-order 17 bits and add one so that 23550 * we never return zero. 23551 */ 23552 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23553 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23554 tcp_random_fptr = tcp_random_state; 23555 ++tcp_random_rptr; 23556 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23557 tcp_random_rptr = tcp_random_state; 23558 23559 mutex_exit(&tcp_random_lock); 23560 return (i); 23561 } 23562 23563 /* 23564 * XXX This will go away when TPI is extended to send 23565 * info reqs to sockfs/timod ..... 23566 * Given a queue, set the max packet size for the write 23567 * side of the queue below stream head. This value is 23568 * cached on the stream head. 23569 * Returns 1 on success, 0 otherwise. 23570 */ 23571 static int 23572 setmaxps(queue_t *q, int maxpsz) 23573 { 23574 struct stdata *stp; 23575 queue_t *wq; 23576 stp = STREAM(q); 23577 23578 /* 23579 * At this point change of a queue parameter is not allowed 23580 * when a multiplexor is sitting on top. 23581 */ 23582 if (stp->sd_flag & STPLEX) 23583 return (0); 23584 23585 claimstr(stp->sd_wrq); 23586 wq = stp->sd_wrq->q_next; 23587 ASSERT(wq != NULL); 23588 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 23589 releasestr(stp->sd_wrq); 23590 return (1); 23591 } 23592 23593 static int 23594 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 23595 int *t_errorp, int *sys_errorp) 23596 { 23597 int error; 23598 int is_absreq_failure; 23599 t_scalar_t *opt_lenp; 23600 t_scalar_t opt_offset; 23601 int prim_type; 23602 struct T_conn_req *tcreqp; 23603 struct T_conn_res *tcresp; 23604 cred_t *cr; 23605 23606 cr = DB_CREDDEF(mp, tcp->tcp_cred); 23607 23608 prim_type = ((union T_primitives *)mp->b_rptr)->type; 23609 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 23610 prim_type == T_CONN_RES); 23611 23612 switch (prim_type) { 23613 case T_CONN_REQ: 23614 tcreqp = (struct T_conn_req *)mp->b_rptr; 23615 opt_offset = tcreqp->OPT_offset; 23616 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 23617 break; 23618 case O_T_CONN_RES: 23619 case T_CONN_RES: 23620 tcresp = (struct T_conn_res *)mp->b_rptr; 23621 opt_offset = tcresp->OPT_offset; 23622 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 23623 break; 23624 } 23625 23626 *t_errorp = 0; 23627 *sys_errorp = 0; 23628 *do_disconnectp = 0; 23629 23630 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 23631 opt_offset, cr, &tcp_opt_obj, 23632 NULL, &is_absreq_failure); 23633 23634 switch (error) { 23635 case 0: /* no error */ 23636 ASSERT(is_absreq_failure == 0); 23637 return (0); 23638 case ENOPROTOOPT: 23639 *t_errorp = TBADOPT; 23640 break; 23641 case EACCES: 23642 *t_errorp = TACCES; 23643 break; 23644 default: 23645 *t_errorp = TSYSERR; *sys_errorp = error; 23646 break; 23647 } 23648 if (is_absreq_failure != 0) { 23649 /* 23650 * The connection request should get the local ack 23651 * T_OK_ACK and then a T_DISCON_IND. 23652 */ 23653 *do_disconnectp = 1; 23654 } 23655 return (-1); 23656 } 23657 23658 /* 23659 * Split this function out so that if the secret changes, I'm okay. 23660 * 23661 * Initialize the tcp_iss_cookie and tcp_iss_key. 23662 */ 23663 23664 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 23665 23666 static void 23667 tcp_iss_key_init(uint8_t *phrase, int len) 23668 { 23669 struct { 23670 int32_t current_time; 23671 uint32_t randnum; 23672 uint16_t pad; 23673 uint8_t ether[6]; 23674 uint8_t passwd[PASSWD_SIZE]; 23675 } tcp_iss_cookie; 23676 time_t t; 23677 23678 /* 23679 * Start with the current absolute time. 23680 */ 23681 (void) drv_getparm(TIME, &t); 23682 tcp_iss_cookie.current_time = t; 23683 23684 /* 23685 * XXX - Need a more random number per RFC 1750, not this crap. 23686 * OTOH, if what follows is pretty random, then I'm in better shape. 23687 */ 23688 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 23689 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 23690 23691 /* 23692 * The cpu_type_info is pretty non-random. Ugggh. It does serve 23693 * as a good template. 23694 */ 23695 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 23696 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 23697 23698 /* 23699 * The pass-phrase. Normally this is supplied by user-called NDD. 23700 */ 23701 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 23702 23703 /* 23704 * See 4010593 if this section becomes a problem again, 23705 * but the local ethernet address is useful here. 23706 */ 23707 (void) localetheraddr(NULL, 23708 (struct ether_addr *)&tcp_iss_cookie.ether); 23709 23710 /* 23711 * Hash 'em all together. The MD5Final is called per-connection. 23712 */ 23713 mutex_enter(&tcp_iss_key_lock); 23714 MD5Init(&tcp_iss_key); 23715 MD5Update(&tcp_iss_key, (uchar_t *)&tcp_iss_cookie, 23716 sizeof (tcp_iss_cookie)); 23717 mutex_exit(&tcp_iss_key_lock); 23718 } 23719 23720 /* 23721 * Set the RFC 1948 pass phrase 23722 */ 23723 /* ARGSUSED */ 23724 static int 23725 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23726 cred_t *cr) 23727 { 23728 /* 23729 * Basically, value contains a new pass phrase. Pass it along! 23730 */ 23731 tcp_iss_key_init((uint8_t *)value, strlen(value)); 23732 return (0); 23733 } 23734 23735 /* ARGSUSED */ 23736 static int 23737 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 23738 { 23739 bzero(buf, sizeof (tcp_sack_info_t)); 23740 return (0); 23741 } 23742 23743 /* ARGSUSED */ 23744 static int 23745 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 23746 { 23747 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 23748 return (0); 23749 } 23750 23751 void 23752 tcp_ddi_init(void) 23753 { 23754 int i; 23755 23756 /* Initialize locks */ 23757 rw_init(&tcp_hsp_lock, NULL, RW_DEFAULT, NULL); 23758 mutex_init(&tcp_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 23759 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 23760 mutex_init(&tcp_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 23761 mutex_init(&tcp_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 23762 rw_init(&tcp_reserved_port_lock, NULL, RW_DEFAULT, NULL); 23763 23764 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 23765 mutex_init(&tcp_bind_fanout[i].tf_lock, NULL, 23766 MUTEX_DEFAULT, NULL); 23767 } 23768 23769 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 23770 mutex_init(&tcp_acceptor_fanout[i].tf_lock, NULL, 23771 MUTEX_DEFAULT, NULL); 23772 } 23773 23774 /* TCP's IPsec code calls the packet dropper. */ 23775 ip_drop_register(&tcp_dropper, "TCP IPsec policy enforcement"); 23776 23777 if (!tcp_g_nd) { 23778 if (!tcp_param_register(tcp_param_arr, A_CNT(tcp_param_arr))) { 23779 nd_free(&tcp_g_nd); 23780 } 23781 } 23782 23783 /* 23784 * Note: To really walk the device tree you need the devinfo 23785 * pointer to your device which is only available after probe/attach. 23786 * The following is safe only because it uses ddi_root_node() 23787 */ 23788 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 23789 tcp_opt_obj.odb_opt_arr_cnt); 23790 23791 tcp_timercache = kmem_cache_create("tcp_timercache", 23792 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 23793 NULL, NULL, NULL, NULL, NULL, 0); 23794 23795 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 23796 sizeof (tcp_sack_info_t), 0, 23797 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 23798 23799 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 23800 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 23801 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 23802 23803 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 23804 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 23805 23806 ip_squeue_init(tcp_squeue_add); 23807 23808 /* Initialize the random number generator */ 23809 tcp_random_init(); 23810 23811 /* 23812 * Initialize RFC 1948 secret values. This will probably be reset once 23813 * by the boot scripts. 23814 * 23815 * Use NULL name, as the name is caught by the new lockstats. 23816 * 23817 * Initialize with some random, non-guessable string, like the global 23818 * T_INFO_ACK. 23819 */ 23820 23821 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 23822 sizeof (tcp_g_t_info_ack)); 23823 23824 if ((tcp_kstat = kstat_create(TCP_MOD_NAME, 0, "tcpstat", 23825 "net", KSTAT_TYPE_NAMED, 23826 sizeof (tcp_statistics) / sizeof (kstat_named_t), 23827 KSTAT_FLAG_VIRTUAL)) != NULL) { 23828 tcp_kstat->ks_data = &tcp_statistics; 23829 kstat_install(tcp_kstat); 23830 } 23831 23832 tcp_kstat_init(); 23833 } 23834 23835 void 23836 tcp_ddi_destroy(void) 23837 { 23838 int i; 23839 23840 nd_free(&tcp_g_nd); 23841 23842 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 23843 mutex_destroy(&tcp_bind_fanout[i].tf_lock); 23844 } 23845 23846 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 23847 mutex_destroy(&tcp_acceptor_fanout[i].tf_lock); 23848 } 23849 23850 mutex_destroy(&tcp_iss_key_lock); 23851 rw_destroy(&tcp_hsp_lock); 23852 mutex_destroy(&tcp_g_q_lock); 23853 mutex_destroy(&tcp_random_lock); 23854 mutex_destroy(&tcp_epriv_port_lock); 23855 rw_destroy(&tcp_reserved_port_lock); 23856 23857 ip_drop_unregister(&tcp_dropper); 23858 23859 kmem_cache_destroy(tcp_timercache); 23860 kmem_cache_destroy(tcp_sack_info_cache); 23861 kmem_cache_destroy(tcp_iphc_cache); 23862 23863 tcp_kstat_fini(); 23864 } 23865 23866 /* 23867 * Generate ISS, taking into account NDD changes may happen halfway through. 23868 * (If the iss is not zero, set it.) 23869 */ 23870 23871 static void 23872 tcp_iss_init(tcp_t *tcp) 23873 { 23874 MD5_CTX context; 23875 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 23876 uint32_t answer[4]; 23877 23878 tcp_iss_incr_extra += (ISS_INCR >> 1); 23879 tcp->tcp_iss = tcp_iss_incr_extra; 23880 switch (tcp_strong_iss) { 23881 case 2: 23882 mutex_enter(&tcp_iss_key_lock); 23883 context = tcp_iss_key; 23884 mutex_exit(&tcp_iss_key_lock); 23885 arg.ports = tcp->tcp_ports; 23886 if (tcp->tcp_ipversion == IPV4_VERSION) { 23887 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 23888 &arg.src); 23889 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 23890 &arg.dst); 23891 } else { 23892 arg.src = tcp->tcp_ip6h->ip6_src; 23893 arg.dst = tcp->tcp_ip6h->ip6_dst; 23894 } 23895 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 23896 MD5Final((uchar_t *)answer, &context); 23897 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 23898 /* 23899 * Now that we've hashed into a unique per-connection sequence 23900 * space, add a random increment per strong_iss == 1. So I 23901 * guess we'll have to... 23902 */ 23903 /* FALLTHRU */ 23904 case 1: 23905 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 23906 break; 23907 default: 23908 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 23909 break; 23910 } 23911 tcp->tcp_valid_bits = TCP_ISS_VALID; 23912 tcp->tcp_fss = tcp->tcp_iss - 1; 23913 tcp->tcp_suna = tcp->tcp_iss; 23914 tcp->tcp_snxt = tcp->tcp_iss + 1; 23915 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23916 tcp->tcp_csuna = tcp->tcp_snxt; 23917 } 23918 23919 /* 23920 * Exported routine for extracting active tcp connection status. 23921 * 23922 * This is used by the Solaris Cluster Networking software to 23923 * gather a list of connections that need to be forwarded to 23924 * specific nodes in the cluster when configuration changes occur. 23925 * 23926 * The callback is invoked for each tcp_t structure. Returning 23927 * non-zero from the callback routine terminates the search. 23928 */ 23929 int 23930 cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg) 23931 { 23932 tcp_t *tcp; 23933 cl_tcp_info_t cl_tcpi; 23934 connf_t *connfp; 23935 conn_t *connp; 23936 int i; 23937 23938 ASSERT(callback != NULL); 23939 23940 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 23941 23942 connfp = &ipcl_globalhash_fanout[i]; 23943 connp = NULL; 23944 23945 while ((connp = 23946 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 23947 23948 tcp = connp->conn_tcp; 23949 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 23950 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 23951 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 23952 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 23953 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 23954 /* 23955 * The macros tcp_laddr and tcp_faddr give the IPv4 23956 * addresses. They are copied implicitly below as 23957 * mapped addresses. 23958 */ 23959 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 23960 if (tcp->tcp_ipversion == IPV4_VERSION) { 23961 cl_tcpi.cl_tcpi_faddr = 23962 tcp->tcp_ipha->ipha_dst; 23963 } else { 23964 cl_tcpi.cl_tcpi_faddr_v6 = 23965 tcp->tcp_ip6h->ip6_dst; 23966 } 23967 23968 /* 23969 * If the callback returns non-zero 23970 * we terminate the traversal. 23971 */ 23972 if ((*callback)(&cl_tcpi, arg) != 0) { 23973 CONN_DEC_REF(tcp->tcp_connp); 23974 return (1); 23975 } 23976 } 23977 } 23978 23979 return (0); 23980 } 23981 23982 /* 23983 * Macros used for accessing the different types of sockaddr 23984 * structures inside a tcp_ioc_abort_conn_t. 23985 */ 23986 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 23987 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 23988 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 23989 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 23990 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 23991 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 23992 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 23993 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 23994 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 23995 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 23996 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 23997 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 23998 23999 /* 24000 * Return the correct error code to mimic the behavior 24001 * of a connection reset. 24002 */ 24003 #define TCP_AC_GET_ERRCODE(state, err) { \ 24004 switch ((state)) { \ 24005 case TCPS_SYN_SENT: \ 24006 case TCPS_SYN_RCVD: \ 24007 (err) = ECONNREFUSED; \ 24008 break; \ 24009 case TCPS_ESTABLISHED: \ 24010 case TCPS_FIN_WAIT_1: \ 24011 case TCPS_FIN_WAIT_2: \ 24012 case TCPS_CLOSE_WAIT: \ 24013 (err) = ECONNRESET; \ 24014 break; \ 24015 case TCPS_CLOSING: \ 24016 case TCPS_LAST_ACK: \ 24017 case TCPS_TIME_WAIT: \ 24018 (err) = 0; \ 24019 break; \ 24020 default: \ 24021 (err) = ENXIO; \ 24022 } \ 24023 } 24024 24025 /* 24026 * Check if a tcp structure matches the info in acp. 24027 */ 24028 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 24029 (((acp)->ac_local.ss_family == AF_INET) ? \ 24030 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 24031 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 24032 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 24033 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 24034 (TCP_AC_V4LPORT((acp)) == 0 || \ 24035 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 24036 (TCP_AC_V4RPORT((acp)) == 0 || \ 24037 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24038 (acp)->ac_start <= (tcp)->tcp_state && \ 24039 (acp)->ac_end >= (tcp)->tcp_state) : \ 24040 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24041 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24042 &(tcp)->tcp_ip_src_v6)) && \ 24043 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24044 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24045 &(tcp)->tcp_remote_v6)) && \ 24046 (TCP_AC_V6LPORT((acp)) == 0 || \ 24047 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24048 (TCP_AC_V6RPORT((acp)) == 0 || \ 24049 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24050 (acp)->ac_start <= (tcp)->tcp_state && \ 24051 (acp)->ac_end >= (tcp)->tcp_state)) 24052 24053 #define TCP_AC_MATCH(acp, tcp) \ 24054 (((acp)->ac_zoneid == ALL_ZONES || \ 24055 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24056 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24057 24058 /* 24059 * Build a message containing a tcp_ioc_abort_conn_t structure 24060 * which is filled in with information from acp and tp. 24061 */ 24062 static mblk_t * 24063 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24064 { 24065 mblk_t *mp; 24066 tcp_ioc_abort_conn_t *tacp; 24067 24068 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24069 if (mp == NULL) 24070 return (NULL); 24071 24072 mp->b_datap->db_type = M_CTL; 24073 24074 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24075 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24076 sizeof (uint32_t)); 24077 24078 tacp->ac_start = acp->ac_start; 24079 tacp->ac_end = acp->ac_end; 24080 tacp->ac_zoneid = acp->ac_zoneid; 24081 24082 if (acp->ac_local.ss_family == AF_INET) { 24083 tacp->ac_local.ss_family = AF_INET; 24084 tacp->ac_remote.ss_family = AF_INET; 24085 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24086 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24087 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24088 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24089 } else { 24090 tacp->ac_local.ss_family = AF_INET6; 24091 tacp->ac_remote.ss_family = AF_INET6; 24092 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24093 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24094 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24095 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24096 } 24097 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24098 return (mp); 24099 } 24100 24101 /* 24102 * Print a tcp_ioc_abort_conn_t structure. 24103 */ 24104 static void 24105 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24106 { 24107 char lbuf[128]; 24108 char rbuf[128]; 24109 sa_family_t af; 24110 in_port_t lport, rport; 24111 ushort_t logflags; 24112 24113 af = acp->ac_local.ss_family; 24114 24115 if (af == AF_INET) { 24116 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24117 lbuf, 128); 24118 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24119 rbuf, 128); 24120 lport = ntohs(TCP_AC_V4LPORT(acp)); 24121 rport = ntohs(TCP_AC_V4RPORT(acp)); 24122 } else { 24123 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24124 lbuf, 128); 24125 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24126 rbuf, 128); 24127 lport = ntohs(TCP_AC_V6LPORT(acp)); 24128 rport = ntohs(TCP_AC_V6RPORT(acp)); 24129 } 24130 24131 logflags = SL_TRACE | SL_NOTE; 24132 /* 24133 * Don't print this message to the console if the operation was done 24134 * to a non-global zone. 24135 */ 24136 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24137 logflags |= SL_CONSOLE; 24138 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24139 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24140 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24141 acp->ac_start, acp->ac_end); 24142 } 24143 24144 /* 24145 * Called inside tcp_rput when a message built using 24146 * tcp_ioctl_abort_build_msg is put into a queue. 24147 * Note that when we get here there is no wildcard in acp any more. 24148 */ 24149 static void 24150 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24151 { 24152 tcp_ioc_abort_conn_t *acp; 24153 24154 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24155 if (tcp->tcp_state <= acp->ac_end) { 24156 /* 24157 * If we get here, we are already on the correct 24158 * squeue. This ioctl follows the following path 24159 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24160 * ->tcp_ioctl_abort->squeue_fill (if on a 24161 * different squeue) 24162 */ 24163 int errcode; 24164 24165 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24166 (void) tcp_clean_death(tcp, errcode, 26); 24167 } 24168 freemsg(mp); 24169 } 24170 24171 /* 24172 * Abort all matching connections on a hash chain. 24173 */ 24174 static int 24175 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24176 boolean_t exact) 24177 { 24178 int nmatch, err = 0; 24179 tcp_t *tcp; 24180 MBLKP mp, last, listhead = NULL; 24181 conn_t *tconnp; 24182 connf_t *connfp = &ipcl_conn_fanout[index]; 24183 24184 startover: 24185 nmatch = 0; 24186 24187 mutex_enter(&connfp->connf_lock); 24188 for (tconnp = connfp->connf_head; tconnp != NULL; 24189 tconnp = tconnp->conn_next) { 24190 tcp = tconnp->conn_tcp; 24191 if (TCP_AC_MATCH(acp, tcp)) { 24192 CONN_INC_REF(tcp->tcp_connp); 24193 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24194 if (mp == NULL) { 24195 err = ENOMEM; 24196 CONN_DEC_REF(tcp->tcp_connp); 24197 break; 24198 } 24199 mp->b_prev = (mblk_t *)tcp; 24200 24201 if (listhead == NULL) { 24202 listhead = mp; 24203 last = mp; 24204 } else { 24205 last->b_next = mp; 24206 last = mp; 24207 } 24208 nmatch++; 24209 if (exact) 24210 break; 24211 } 24212 24213 /* Avoid holding lock for too long. */ 24214 if (nmatch >= 500) 24215 break; 24216 } 24217 mutex_exit(&connfp->connf_lock); 24218 24219 /* Pass mp into the correct tcp */ 24220 while ((mp = listhead) != NULL) { 24221 listhead = listhead->b_next; 24222 tcp = (tcp_t *)mp->b_prev; 24223 mp->b_next = mp->b_prev = NULL; 24224 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 24225 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 24226 } 24227 24228 *count += nmatch; 24229 if (nmatch >= 500 && err == 0) 24230 goto startover; 24231 return (err); 24232 } 24233 24234 /* 24235 * Abort all connections that matches the attributes specified in acp. 24236 */ 24237 static int 24238 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp) 24239 { 24240 sa_family_t af; 24241 uint32_t ports; 24242 uint16_t *pports; 24243 int err = 0, count = 0; 24244 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24245 int index = -1; 24246 ushort_t logflags; 24247 24248 af = acp->ac_local.ss_family; 24249 24250 if (af == AF_INET) { 24251 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24252 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24253 pports = (uint16_t *)&ports; 24254 pports[1] = TCP_AC_V4LPORT(acp); 24255 pports[0] = TCP_AC_V4RPORT(acp); 24256 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24257 } 24258 } else { 24259 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 24260 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 24261 pports = (uint16_t *)&ports; 24262 pports[1] = TCP_AC_V6LPORT(acp); 24263 pports[0] = TCP_AC_V6RPORT(acp); 24264 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 24265 } 24266 } 24267 24268 /* 24269 * For cases where remote addr, local port, and remote port are non- 24270 * wildcards, tcp_ioctl_abort_bucket will only be called once. 24271 */ 24272 if (index != -1) { 24273 err = tcp_ioctl_abort_bucket(acp, index, 24274 &count, exact); 24275 } else { 24276 /* 24277 * loop through all entries for wildcard case 24278 */ 24279 for (index = 0; index < ipcl_conn_fanout_size; index++) { 24280 err = tcp_ioctl_abort_bucket(acp, index, 24281 &count, exact); 24282 if (err != 0) 24283 break; 24284 } 24285 } 24286 24287 logflags = SL_TRACE | SL_NOTE; 24288 /* 24289 * Don't print this message to the console if the operation was done 24290 * to a non-global zone. 24291 */ 24292 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24293 logflags |= SL_CONSOLE; 24294 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 24295 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 24296 if (err == 0 && count == 0) 24297 err = ENOENT; 24298 return (err); 24299 } 24300 24301 /* 24302 * Process the TCP_IOC_ABORT_CONN ioctl request. 24303 */ 24304 static void 24305 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 24306 { 24307 int err; 24308 IOCP iocp; 24309 MBLKP mp1; 24310 sa_family_t laf, raf; 24311 tcp_ioc_abort_conn_t *acp; 24312 zone_t *zptr; 24313 zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid; 24314 24315 iocp = (IOCP)mp->b_rptr; 24316 24317 if ((mp1 = mp->b_cont) == NULL || 24318 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 24319 err = EINVAL; 24320 goto out; 24321 } 24322 24323 /* check permissions */ 24324 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 24325 err = EPERM; 24326 goto out; 24327 } 24328 24329 if (mp1->b_cont != NULL) { 24330 freemsg(mp1->b_cont); 24331 mp1->b_cont = NULL; 24332 } 24333 24334 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 24335 laf = acp->ac_local.ss_family; 24336 raf = acp->ac_remote.ss_family; 24337 24338 /* check that a zone with the supplied zoneid exists */ 24339 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 24340 zptr = zone_find_by_id(zoneid); 24341 if (zptr != NULL) { 24342 zone_rele(zptr); 24343 } else { 24344 err = EINVAL; 24345 goto out; 24346 } 24347 } 24348 24349 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24350 acp->ac_start > acp->ac_end || laf != raf || 24351 (laf != AF_INET && laf != AF_INET6)) { 24352 err = EINVAL; 24353 goto out; 24354 } 24355 24356 tcp_ioctl_abort_dump(acp); 24357 err = tcp_ioctl_abort(acp); 24358 24359 out: 24360 if (mp1 != NULL) { 24361 freemsg(mp1); 24362 mp->b_cont = NULL; 24363 } 24364 24365 if (err != 0) 24366 miocnak(q, mp, 0, err); 24367 else 24368 miocack(q, mp, 0, 0); 24369 } 24370 24371 /* 24372 * tcp_time_wait_processing() handles processing of incoming packets when 24373 * the tcp is in the TIME_WAIT state. 24374 * A TIME_WAIT tcp that has an associated open TCP stream is never put 24375 * on the time wait list. 24376 */ 24377 void 24378 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 24379 uint32_t seg_ack, int seg_len, tcph_t *tcph) 24380 { 24381 int32_t bytes_acked; 24382 int32_t gap; 24383 int32_t rgap; 24384 tcp_opt_t tcpopt; 24385 uint_t flags; 24386 uint32_t new_swnd = 0; 24387 conn_t *connp; 24388 24389 BUMP_LOCAL(tcp->tcp_ibsegs); 24390 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 24391 24392 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 24393 new_swnd = BE16_TO_U16(tcph->th_win) << 24394 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 24395 if (tcp->tcp_snd_ts_ok) { 24396 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 24397 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24398 tcp->tcp_rnxt, TH_ACK); 24399 goto done; 24400 } 24401 } 24402 gap = seg_seq - tcp->tcp_rnxt; 24403 rgap = tcp->tcp_rwnd - (gap + seg_len); 24404 if (gap < 0) { 24405 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 24406 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, 24407 (seg_len > -gap ? -gap : seg_len)); 24408 seg_len += gap; 24409 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 24410 if (flags & TH_RST) { 24411 goto done; 24412 } 24413 if ((flags & TH_FIN) && seg_len == -1) { 24414 /* 24415 * When TCP receives a duplicate FIN in 24416 * TIME_WAIT state, restart the 2 MSL timer. 24417 * See page 73 in RFC 793. Make sure this TCP 24418 * is already on the TIME_WAIT list. If not, 24419 * just restart the timer. 24420 */ 24421 if (TCP_IS_DETACHED(tcp)) { 24422 tcp_time_wait_remove(tcp, NULL); 24423 tcp_time_wait_append(tcp); 24424 TCP_DBGSTAT(tcp_rput_time_wait); 24425 } else { 24426 ASSERT(tcp != NULL); 24427 TCP_TIMER_RESTART(tcp, 24428 tcp_time_wait_interval); 24429 } 24430 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24431 tcp->tcp_rnxt, TH_ACK); 24432 goto done; 24433 } 24434 flags |= TH_ACK_NEEDED; 24435 seg_len = 0; 24436 goto process_ack; 24437 } 24438 24439 /* Fix seg_seq, and chew the gap off the front. */ 24440 seg_seq = tcp->tcp_rnxt; 24441 } 24442 24443 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 24444 /* 24445 * Make sure that when we accept the connection, pick 24446 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 24447 * old connection. 24448 * 24449 * The next ISS generated is equal to tcp_iss_incr_extra 24450 * + ISS_INCR/2 + other components depending on the 24451 * value of tcp_strong_iss. We pre-calculate the new 24452 * ISS here and compare with tcp_snxt to determine if 24453 * we need to make adjustment to tcp_iss_incr_extra. 24454 * 24455 * The above calculation is ugly and is a 24456 * waste of CPU cycles... 24457 */ 24458 uint32_t new_iss = tcp_iss_incr_extra; 24459 int32_t adj; 24460 24461 switch (tcp_strong_iss) { 24462 case 2: { 24463 /* Add time and MD5 components. */ 24464 uint32_t answer[4]; 24465 struct { 24466 uint32_t ports; 24467 in6_addr_t src; 24468 in6_addr_t dst; 24469 } arg; 24470 MD5_CTX context; 24471 24472 mutex_enter(&tcp_iss_key_lock); 24473 context = tcp_iss_key; 24474 mutex_exit(&tcp_iss_key_lock); 24475 arg.ports = tcp->tcp_ports; 24476 /* We use MAPPED addresses in tcp_iss_init */ 24477 arg.src = tcp->tcp_ip_src_v6; 24478 if (tcp->tcp_ipversion == IPV4_VERSION) { 24479 IN6_IPADDR_TO_V4MAPPED( 24480 tcp->tcp_ipha->ipha_dst, 24481 &arg.dst); 24482 } else { 24483 arg.dst = 24484 tcp->tcp_ip6h->ip6_dst; 24485 } 24486 MD5Update(&context, (uchar_t *)&arg, 24487 sizeof (arg)); 24488 MD5Final((uchar_t *)answer, &context); 24489 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 24490 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 24491 break; 24492 } 24493 case 1: 24494 /* Add time component and min random (i.e. 1). */ 24495 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 24496 break; 24497 default: 24498 /* Add only time component. */ 24499 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24500 break; 24501 } 24502 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 24503 /* 24504 * New ISS not guaranteed to be ISS_INCR/2 24505 * ahead of the current tcp_snxt, so add the 24506 * difference to tcp_iss_incr_extra. 24507 */ 24508 tcp_iss_incr_extra += adj; 24509 } 24510 /* 24511 * If tcp_clean_death() can not perform the task now, 24512 * drop the SYN packet and let the other side re-xmit. 24513 * Otherwise pass the SYN packet back in, since the 24514 * old tcp state has been cleaned up or freed. 24515 */ 24516 if (tcp_clean_death(tcp, 0, 27) == -1) 24517 goto done; 24518 /* 24519 * We will come back to tcp_rput_data 24520 * on the global queue. Packets destined 24521 * for the global queue will be checked 24522 * with global policy. But the policy for 24523 * this packet has already been checked as 24524 * this was destined for the detached 24525 * connection. We need to bypass policy 24526 * check this time by attaching a dummy 24527 * ipsec_in with ipsec_in_dont_check set. 24528 */ 24529 if ((connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid)) != 24530 NULL) { 24531 TCP_STAT(tcp_time_wait_syn_success); 24532 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 24533 return; 24534 } 24535 goto done; 24536 } 24537 24538 /* 24539 * rgap is the amount of stuff received out of window. A negative 24540 * value is the amount out of window. 24541 */ 24542 if (rgap < 0) { 24543 BUMP_MIB(&tcp_mib, tcpInDataPastWinSegs); 24544 UPDATE_MIB(&tcp_mib, tcpInDataPastWinBytes, -rgap); 24545 /* Fix seg_len and make sure there is something left. */ 24546 seg_len += rgap; 24547 if (seg_len <= 0) { 24548 if (flags & TH_RST) { 24549 goto done; 24550 } 24551 flags |= TH_ACK_NEEDED; 24552 seg_len = 0; 24553 goto process_ack; 24554 } 24555 } 24556 /* 24557 * Check whether we can update tcp_ts_recent. This test is 24558 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 24559 * Extensions for High Performance: An Update", Internet Draft. 24560 */ 24561 if (tcp->tcp_snd_ts_ok && 24562 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 24563 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 24564 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 24565 tcp->tcp_last_rcv_lbolt = lbolt64; 24566 } 24567 24568 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 24569 /* Always ack out of order packets */ 24570 flags |= TH_ACK_NEEDED; 24571 seg_len = 0; 24572 } else if (seg_len > 0) { 24573 BUMP_MIB(&tcp_mib, tcpInClosed); 24574 BUMP_MIB(&tcp_mib, tcpInDataInorderSegs); 24575 UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, seg_len); 24576 } 24577 if (flags & TH_RST) { 24578 (void) tcp_clean_death(tcp, 0, 28); 24579 goto done; 24580 } 24581 if (flags & TH_SYN) { 24582 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 24583 TH_RST|TH_ACK); 24584 /* 24585 * Do not delete the TCP structure if it is in 24586 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 24587 */ 24588 goto done; 24589 } 24590 process_ack: 24591 if (flags & TH_ACK) { 24592 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 24593 if (bytes_acked <= 0) { 24594 if (bytes_acked == 0 && seg_len == 0 && 24595 new_swnd == tcp->tcp_swnd) 24596 BUMP_MIB(&tcp_mib, tcpInDupAck); 24597 } else { 24598 /* Acks something not sent */ 24599 flags |= TH_ACK_NEEDED; 24600 } 24601 } 24602 if (flags & TH_ACK_NEEDED) { 24603 /* 24604 * Time to send an ack for some reason. 24605 */ 24606 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24607 tcp->tcp_rnxt, TH_ACK); 24608 } 24609 done: 24610 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 24611 DB_CKSUMSTART(mp) = 0; 24612 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 24613 TCP_STAT(tcp_time_wait_syn_fail); 24614 } 24615 freemsg(mp); 24616 } 24617 24618 /* 24619 * Allocate a T_SVR4_OPTMGMT_REQ. 24620 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 24621 * that tcp_rput_other can drop the acks. 24622 */ 24623 static mblk_t * 24624 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 24625 { 24626 mblk_t *mp; 24627 struct T_optmgmt_req *tor; 24628 struct opthdr *oh; 24629 uint_t size; 24630 char *optptr; 24631 24632 size = sizeof (*tor) + sizeof (*oh) + optlen; 24633 mp = allocb(size, BPRI_MED); 24634 if (mp == NULL) 24635 return (NULL); 24636 24637 mp->b_wptr += size; 24638 mp->b_datap->db_type = M_PROTO; 24639 tor = (struct T_optmgmt_req *)mp->b_rptr; 24640 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 24641 tor->MGMT_flags = T_NEGOTIATE; 24642 tor->OPT_length = sizeof (*oh) + optlen; 24643 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 24644 24645 oh = (struct opthdr *)&tor[1]; 24646 oh->level = level; 24647 oh->name = cmd; 24648 oh->len = optlen; 24649 if (optlen != 0) { 24650 optptr = (char *)&oh[1]; 24651 bcopy(opt, optptr, optlen); 24652 } 24653 return (mp); 24654 } 24655 24656 /* 24657 * TCP Timers Implementation. 24658 */ 24659 timeout_id_t 24660 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 24661 { 24662 mblk_t *mp; 24663 tcp_timer_t *tcpt; 24664 tcp_t *tcp = connp->conn_tcp; 24665 24666 ASSERT(connp->conn_sqp != NULL); 24667 24668 TCP_DBGSTAT(tcp_timeout_calls); 24669 24670 if (tcp->tcp_timercache == NULL) { 24671 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 24672 } else { 24673 TCP_DBGSTAT(tcp_timeout_cached_alloc); 24674 mp = tcp->tcp_timercache; 24675 tcp->tcp_timercache = mp->b_next; 24676 mp->b_next = NULL; 24677 ASSERT(mp->b_wptr == NULL); 24678 } 24679 24680 CONN_INC_REF(connp); 24681 tcpt = (tcp_timer_t *)mp->b_rptr; 24682 tcpt->connp = connp; 24683 tcpt->tcpt_proc = f; 24684 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 24685 return ((timeout_id_t)mp); 24686 } 24687 24688 static void 24689 tcp_timer_callback(void *arg) 24690 { 24691 mblk_t *mp = (mblk_t *)arg; 24692 tcp_timer_t *tcpt; 24693 conn_t *connp; 24694 24695 tcpt = (tcp_timer_t *)mp->b_rptr; 24696 connp = tcpt->connp; 24697 squeue_fill(connp->conn_sqp, mp, 24698 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 24699 } 24700 24701 static void 24702 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 24703 { 24704 tcp_timer_t *tcpt; 24705 conn_t *connp = (conn_t *)arg; 24706 tcp_t *tcp = connp->conn_tcp; 24707 24708 tcpt = (tcp_timer_t *)mp->b_rptr; 24709 ASSERT(connp == tcpt->connp); 24710 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 24711 24712 /* 24713 * If the TCP has reached the closed state, don't proceed any 24714 * further. This TCP logically does not exist on the system. 24715 * tcpt_proc could for example access queues, that have already 24716 * been qprocoff'ed off. Also see comments at the start of tcp_input 24717 */ 24718 if (tcp->tcp_state != TCPS_CLOSED) { 24719 (*tcpt->tcpt_proc)(connp); 24720 } else { 24721 tcp->tcp_timer_tid = 0; 24722 } 24723 tcp_timer_free(connp->conn_tcp, mp); 24724 } 24725 24726 /* 24727 * There is potential race with untimeout and the handler firing at the same 24728 * time. The mblock may be freed by the handler while we are trying to use 24729 * it. But since both should execute on the same squeue, this race should not 24730 * occur. 24731 */ 24732 clock_t 24733 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 24734 { 24735 mblk_t *mp = (mblk_t *)id; 24736 tcp_timer_t *tcpt; 24737 clock_t delta; 24738 24739 TCP_DBGSTAT(tcp_timeout_cancel_reqs); 24740 24741 if (mp == NULL) 24742 return (-1); 24743 24744 tcpt = (tcp_timer_t *)mp->b_rptr; 24745 ASSERT(tcpt->connp == connp); 24746 24747 delta = untimeout(tcpt->tcpt_tid); 24748 24749 if (delta >= 0) { 24750 TCP_DBGSTAT(tcp_timeout_canceled); 24751 tcp_timer_free(connp->conn_tcp, mp); 24752 CONN_DEC_REF(connp); 24753 } 24754 24755 return (delta); 24756 } 24757 24758 /* 24759 * Allocate space for the timer event. The allocation looks like mblk, but it is 24760 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 24761 * 24762 * Dealing with failures: If we can't allocate from the timer cache we try 24763 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 24764 * points to b_rptr. 24765 * If we can't allocate anything using allocb_tryhard(), we perform a last 24766 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 24767 * save the actual allocation size in b_datap. 24768 */ 24769 mblk_t * 24770 tcp_timermp_alloc(int kmflags) 24771 { 24772 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 24773 kmflags & ~KM_PANIC); 24774 24775 if (mp != NULL) { 24776 mp->b_next = mp->b_prev = NULL; 24777 mp->b_rptr = (uchar_t *)(&mp[1]); 24778 mp->b_wptr = NULL; 24779 mp->b_datap = NULL; 24780 mp->b_queue = NULL; 24781 } else if (kmflags & KM_PANIC) { 24782 /* 24783 * Failed to allocate memory for the timer. Try allocating from 24784 * dblock caches. 24785 */ 24786 TCP_STAT(tcp_timermp_allocfail); 24787 mp = allocb_tryhard(sizeof (tcp_timer_t)); 24788 if (mp == NULL) { 24789 size_t size = 0; 24790 /* 24791 * Memory is really low. Try tryhard allocation. 24792 */ 24793 TCP_STAT(tcp_timermp_allocdblfail); 24794 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 24795 sizeof (tcp_timer_t), &size, kmflags); 24796 mp->b_rptr = (uchar_t *)(&mp[1]); 24797 mp->b_next = mp->b_prev = NULL; 24798 mp->b_wptr = (uchar_t *)-1; 24799 mp->b_datap = (dblk_t *)size; 24800 mp->b_queue = NULL; 24801 } 24802 ASSERT(mp->b_wptr != NULL); 24803 } 24804 TCP_DBGSTAT(tcp_timermp_alloced); 24805 24806 return (mp); 24807 } 24808 24809 /* 24810 * Free per-tcp timer cache. 24811 * It can only contain entries from tcp_timercache. 24812 */ 24813 void 24814 tcp_timermp_free(tcp_t *tcp) 24815 { 24816 mblk_t *mp; 24817 24818 while ((mp = tcp->tcp_timercache) != NULL) { 24819 ASSERT(mp->b_wptr == NULL); 24820 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 24821 kmem_cache_free(tcp_timercache, mp); 24822 } 24823 } 24824 24825 /* 24826 * Free timer event. Put it on the per-tcp timer cache if there is not too many 24827 * events there already (currently at most two events are cached). 24828 * If the event is not allocated from the timer cache, free it right away. 24829 */ 24830 static void 24831 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 24832 { 24833 mblk_t *mp1 = tcp->tcp_timercache; 24834 24835 if (mp->b_wptr != NULL) { 24836 /* 24837 * This allocation is not from a timer cache, free it right 24838 * away. 24839 */ 24840 if (mp->b_wptr != (uchar_t *)-1) 24841 freeb(mp); 24842 else 24843 kmem_free(mp, (size_t)mp->b_datap); 24844 } else if (mp1 == NULL || mp1->b_next == NULL) { 24845 /* Cache this timer block for future allocations */ 24846 mp->b_rptr = (uchar_t *)(&mp[1]); 24847 mp->b_next = mp1; 24848 tcp->tcp_timercache = mp; 24849 } else { 24850 kmem_cache_free(tcp_timercache, mp); 24851 TCP_DBGSTAT(tcp_timermp_freed); 24852 } 24853 } 24854 24855 /* 24856 * End of TCP Timers implementation. 24857 */ 24858 24859 /* 24860 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 24861 * on the specified backing STREAMS q. Note, the caller may make the 24862 * decision to call based on the tcp_t.tcp_flow_stopped value which 24863 * when check outside the q's lock is only an advisory check ... 24864 */ 24865 24866 void 24867 tcp_setqfull(tcp_t *tcp) 24868 { 24869 queue_t *q = tcp->tcp_wq; 24870 24871 if (!(q->q_flag & QFULL)) { 24872 mutex_enter(QLOCK(q)); 24873 if (!(q->q_flag & QFULL)) { 24874 /* still need to set QFULL */ 24875 q->q_flag |= QFULL; 24876 tcp->tcp_flow_stopped = B_TRUE; 24877 mutex_exit(QLOCK(q)); 24878 TCP_STAT(tcp_flwctl_on); 24879 } else { 24880 mutex_exit(QLOCK(q)); 24881 } 24882 } 24883 } 24884 24885 void 24886 tcp_clrqfull(tcp_t *tcp) 24887 { 24888 queue_t *q = tcp->tcp_wq; 24889 24890 if (q->q_flag & QFULL) { 24891 mutex_enter(QLOCK(q)); 24892 if (q->q_flag & QFULL) { 24893 q->q_flag &= ~QFULL; 24894 tcp->tcp_flow_stopped = B_FALSE; 24895 mutex_exit(QLOCK(q)); 24896 if (q->q_flag & QWANTW) 24897 qbackenable(q, 0); 24898 } else { 24899 mutex_exit(QLOCK(q)); 24900 } 24901 } 24902 } 24903 24904 /* 24905 * TCP Kstats implementation 24906 */ 24907 static void 24908 tcp_kstat_init(void) 24909 { 24910 tcp_named_kstat_t template = { 24911 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 24912 { "rtoMin", KSTAT_DATA_INT32, 0 }, 24913 { "rtoMax", KSTAT_DATA_INT32, 0 }, 24914 { "maxConn", KSTAT_DATA_INT32, 0 }, 24915 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 24916 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 24917 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 24918 { "estabResets", KSTAT_DATA_UINT32, 0 }, 24919 { "currEstab", KSTAT_DATA_UINT32, 0 }, 24920 { "inSegs", KSTAT_DATA_UINT32, 0 }, 24921 { "outSegs", KSTAT_DATA_UINT32, 0 }, 24922 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 24923 { "connTableSize", KSTAT_DATA_INT32, 0 }, 24924 { "outRsts", KSTAT_DATA_UINT32, 0 }, 24925 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 24926 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 24927 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 24928 { "outAck", KSTAT_DATA_UINT32, 0 }, 24929 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 24930 { "outUrg", KSTAT_DATA_UINT32, 0 }, 24931 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 24932 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 24933 { "outControl", KSTAT_DATA_UINT32, 0 }, 24934 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 24935 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 24936 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 24937 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 24938 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 24939 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 24940 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 24941 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 24942 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 24943 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 24944 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 24945 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 24946 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 24947 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 24948 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 24949 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 24950 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 24951 { "inClosed", KSTAT_DATA_UINT32, 0 }, 24952 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 24953 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 24954 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 24955 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 24956 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 24957 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 24958 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 24959 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 24960 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 24961 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 24962 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 24963 { "connTableSize6", KSTAT_DATA_INT32, 0 } 24964 }; 24965 24966 tcp_mibkp = kstat_create(TCP_MOD_NAME, 0, TCP_MOD_NAME, 24967 "mib2", KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0); 24968 24969 if (tcp_mibkp == NULL) 24970 return; 24971 24972 template.rtoAlgorithm.value.ui32 = 4; 24973 template.rtoMin.value.ui32 = tcp_rexmit_interval_min; 24974 template.rtoMax.value.ui32 = tcp_rexmit_interval_max; 24975 template.maxConn.value.i32 = -1; 24976 24977 bcopy(&template, tcp_mibkp->ks_data, sizeof (template)); 24978 24979 tcp_mibkp->ks_update = tcp_kstat_update; 24980 24981 kstat_install(tcp_mibkp); 24982 } 24983 24984 static void 24985 tcp_kstat_fini(void) 24986 { 24987 24988 if (tcp_mibkp != NULL) { 24989 kstat_delete(tcp_mibkp); 24990 tcp_mibkp = NULL; 24991 } 24992 } 24993 24994 static int 24995 tcp_kstat_update(kstat_t *kp, int rw) 24996 { 24997 tcp_named_kstat_t *tcpkp; 24998 tcp_t *tcp; 24999 connf_t *connfp; 25000 conn_t *connp; 25001 int i; 25002 25003 if (!kp || !kp->ks_data) 25004 return (EIO); 25005 25006 if (rw == KSTAT_WRITE) 25007 return (EACCES); 25008 25009 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 25010 25011 tcpkp->currEstab.value.ui32 = 0; 25012 25013 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25014 connfp = &ipcl_globalhash_fanout[i]; 25015 connp = NULL; 25016 while ((connp = 25017 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25018 tcp = connp->conn_tcp; 25019 switch (tcp_snmp_state(tcp)) { 25020 case MIB2_TCP_established: 25021 case MIB2_TCP_closeWait: 25022 tcpkp->currEstab.value.ui32++; 25023 break; 25024 } 25025 } 25026 } 25027 25028 tcpkp->activeOpens.value.ui32 = tcp_mib.tcpActiveOpens; 25029 tcpkp->passiveOpens.value.ui32 = tcp_mib.tcpPassiveOpens; 25030 tcpkp->attemptFails.value.ui32 = tcp_mib.tcpAttemptFails; 25031 tcpkp->estabResets.value.ui32 = tcp_mib.tcpEstabResets; 25032 tcpkp->inSegs.value.ui32 = tcp_mib.tcpInSegs; 25033 tcpkp->outSegs.value.ui32 = tcp_mib.tcpOutSegs; 25034 tcpkp->retransSegs.value.ui32 = tcp_mib.tcpRetransSegs; 25035 tcpkp->connTableSize.value.i32 = tcp_mib.tcpConnTableSize; 25036 tcpkp->outRsts.value.ui32 = tcp_mib.tcpOutRsts; 25037 tcpkp->outDataSegs.value.ui32 = tcp_mib.tcpOutDataSegs; 25038 tcpkp->outDataBytes.value.ui32 = tcp_mib.tcpOutDataBytes; 25039 tcpkp->retransBytes.value.ui32 = tcp_mib.tcpRetransBytes; 25040 tcpkp->outAck.value.ui32 = tcp_mib.tcpOutAck; 25041 tcpkp->outAckDelayed.value.ui32 = tcp_mib.tcpOutAckDelayed; 25042 tcpkp->outUrg.value.ui32 = tcp_mib.tcpOutUrg; 25043 tcpkp->outWinUpdate.value.ui32 = tcp_mib.tcpOutWinUpdate; 25044 tcpkp->outWinProbe.value.ui32 = tcp_mib.tcpOutWinProbe; 25045 tcpkp->outControl.value.ui32 = tcp_mib.tcpOutControl; 25046 tcpkp->outFastRetrans.value.ui32 = tcp_mib.tcpOutFastRetrans; 25047 tcpkp->inAckSegs.value.ui32 = tcp_mib.tcpInAckSegs; 25048 tcpkp->inAckBytes.value.ui32 = tcp_mib.tcpInAckBytes; 25049 tcpkp->inDupAck.value.ui32 = tcp_mib.tcpInDupAck; 25050 tcpkp->inAckUnsent.value.ui32 = tcp_mib.tcpInAckUnsent; 25051 tcpkp->inDataInorderSegs.value.ui32 = tcp_mib.tcpInDataInorderSegs; 25052 tcpkp->inDataInorderBytes.value.ui32 = tcp_mib.tcpInDataInorderBytes; 25053 tcpkp->inDataUnorderSegs.value.ui32 = tcp_mib.tcpInDataUnorderSegs; 25054 tcpkp->inDataUnorderBytes.value.ui32 = tcp_mib.tcpInDataUnorderBytes; 25055 tcpkp->inDataDupSegs.value.ui32 = tcp_mib.tcpInDataDupSegs; 25056 tcpkp->inDataDupBytes.value.ui32 = tcp_mib.tcpInDataDupBytes; 25057 tcpkp->inDataPartDupSegs.value.ui32 = tcp_mib.tcpInDataPartDupSegs; 25058 tcpkp->inDataPartDupBytes.value.ui32 = tcp_mib.tcpInDataPartDupBytes; 25059 tcpkp->inDataPastWinSegs.value.ui32 = tcp_mib.tcpInDataPastWinSegs; 25060 tcpkp->inDataPastWinBytes.value.ui32 = tcp_mib.tcpInDataPastWinBytes; 25061 tcpkp->inWinProbe.value.ui32 = tcp_mib.tcpInWinProbe; 25062 tcpkp->inWinUpdate.value.ui32 = tcp_mib.tcpInWinUpdate; 25063 tcpkp->inClosed.value.ui32 = tcp_mib.tcpInClosed; 25064 tcpkp->rttNoUpdate.value.ui32 = tcp_mib.tcpRttNoUpdate; 25065 tcpkp->rttUpdate.value.ui32 = tcp_mib.tcpRttUpdate; 25066 tcpkp->timRetrans.value.ui32 = tcp_mib.tcpTimRetrans; 25067 tcpkp->timRetransDrop.value.ui32 = tcp_mib.tcpTimRetransDrop; 25068 tcpkp->timKeepalive.value.ui32 = tcp_mib.tcpTimKeepalive; 25069 tcpkp->timKeepaliveProbe.value.ui32 = tcp_mib.tcpTimKeepaliveProbe; 25070 tcpkp->timKeepaliveDrop.value.ui32 = tcp_mib.tcpTimKeepaliveDrop; 25071 tcpkp->listenDrop.value.ui32 = tcp_mib.tcpListenDrop; 25072 tcpkp->listenDropQ0.value.ui32 = tcp_mib.tcpListenDropQ0; 25073 tcpkp->halfOpenDrop.value.ui32 = tcp_mib.tcpHalfOpenDrop; 25074 tcpkp->outSackRetransSegs.value.ui32 = tcp_mib.tcpOutSackRetransSegs; 25075 tcpkp->connTableSize6.value.i32 = tcp_mib.tcp6ConnTableSize; 25076 25077 return (0); 25078 } 25079 25080 void 25081 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25082 { 25083 uint16_t hdr_len; 25084 ipha_t *ipha; 25085 uint8_t *nexthdrp; 25086 tcph_t *tcph; 25087 25088 /* Already has an eager */ 25089 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25090 TCP_STAT(tcp_reinput_syn); 25091 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 25092 connp, SQTAG_TCP_REINPUT_EAGER); 25093 return; 25094 } 25095 25096 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25097 case IPV4_VERSION: 25098 ipha = (ipha_t *)mp->b_rptr; 25099 hdr_len = IPH_HDR_LENGTH(ipha); 25100 break; 25101 case IPV6_VERSION: 25102 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25103 &hdr_len, &nexthdrp)) { 25104 CONN_DEC_REF(connp); 25105 freemsg(mp); 25106 return; 25107 } 25108 break; 25109 } 25110 25111 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25112 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25113 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25114 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25115 } 25116 25117 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 25118 SQTAG_TCP_REINPUT); 25119 } 25120 25121 static squeue_func_t 25122 tcp_squeue_switch(int val) 25123 { 25124 squeue_func_t rval = squeue_fill; 25125 25126 switch (val) { 25127 case 1: 25128 rval = squeue_enter_nodrain; 25129 break; 25130 case 2: 25131 rval = squeue_enter; 25132 break; 25133 default: 25134 break; 25135 } 25136 return (rval); 25137 } 25138 25139 static void 25140 tcp_squeue_add(squeue_t *sqp) 25141 { 25142 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25143 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25144 25145 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25146 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 25147 sqp, TCP_TIME_WAIT_DELAY); 25148 if (tcp_free_list_max_cnt == 0) { 25149 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25150 max_ncpus : boot_max_ncpus); 25151 25152 /* 25153 * Limit number of entries to 1% of availble memory / tcp_ncpus 25154 */ 25155 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25156 (tcp_ncpus * sizeof (tcp_t) * 100); 25157 } 25158 tcp_time_wait->tcp_free_list_cnt = 0; 25159 } 25160